From 68c4eda1c24d56f3108648c5b1855664d7f0e8cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Str=C3=B6mb=C3=A4ck?= Date: Wed, 18 Mar 2020 12:28:10 +0100 Subject: Added new tests for the validating the syscall number. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Filip Strömbäck --- src/tests/filst/Make.tests | 8 ++--- src/tests/filst/sc-bad-close.c | 64 ++++++++++++++++----------------- src/tests/filst/sc-bad-nr-1.c | 24 +++++++++++++ src/tests/filst/sc-bad-nr-1.ck | 15 ++++++++ src/tests/filst/sc-bad-nr-2.c | 24 +++++++++++++ src/tests/filst/sc-bad-nr-2.ck | 15 ++++++++ src/tests/filst/sc-bad-nr-3.c | 24 +++++++++++++ src/tests/filst/sc-bad-nr-3.ck | 15 ++++++++ src/tests/filst/sc-bad-write.c | 80 +++++++++++++++++++++--------------------- 9 files changed, 193 insertions(+), 76 deletions(-) create mode 100644 src/tests/filst/sc-bad-nr-1.c create mode 100644 src/tests/filst/sc-bad-nr-1.ck create mode 100644 src/tests/filst/sc-bad-nr-2.c create mode 100644 src/tests/filst/sc-bad-nr-2.ck create mode 100644 src/tests/filst/sc-bad-nr-3.c create mode 100644 src/tests/filst/sc-bad-nr-3.ck (limited to 'src/tests') diff --git a/src/tests/filst/Make.tests b/src/tests/filst/Make.tests index 4c59dc5..dfd179f 100644 --- a/src/tests/filst/Make.tests +++ b/src/tests/filst/Make.tests @@ -3,12 +3,12 @@ tests/%.output: FSDISK = 2 tests/%.output: PUTFILES = $(filter-out os.dsk, $^) -tests/filst_TESTS = $(addprefix tests/filst/,sc-bad-write sc-bad-close) +tests/filst_TESTS = $(addprefix tests/filst/,sc-bad-write sc-bad-close sc-bad-nr-1 sc-bad-nr-2 sc-bad-nr-3) tests/filst_PROGS = $(tests/filst_TESTS) -tests/filst/sc-bad-write_SRC = tests/filst/sc-bad-write.c tests/main.c -tests/filst/sc-bad-close_SRC = tests/filst/sc-bad-close.c tests/main.c - +# Semi-automatic magic. +$(foreach prog,$(tests/filst_PROGS),$(eval $(prog)_SRC += $(prog).c)) +$(foreach prog,$(tests/filst_PROGS),$(eval $(prog)_SRC += tests/main.c)) $(foreach prog,$(tests/filst_PROGS),$(eval $(prog)_SRC += tests/lib.c)) diff --git a/src/tests/filst/sc-bad-close.c b/src/tests/filst/sc-bad-close.c index 8331817..17f874a 100644 --- a/src/tests/filst/sc-bad-close.c +++ b/src/tests/filst/sc-bad-close.c @@ -25,43 +25,43 @@ extern int _end_bss; void test_main(void) { - // Get the addres of the first unmapped page in the system. - unsigned page = (unsigned)pg_round_up(&_end_bss); + // Get the addres of the first unmapped page in the system. + unsigned page = (unsigned)pg_round_up(&_end_bss); - // Reserve space for 2 parameters. - unsigned base = page - sizeof(int) * 2; + // Reserve space for 2 parameters. + unsigned base = page - sizeof(int) * 2; - // Call write() with space for 4 parameters (should be fine). - asm volatile ( - "movl %%esp, %%edi;" - "movl %0, %%esp;" // Set stack pointer to right below page boundary. - "movl %1, (%%esp);" // Try to call SYS_CLOSE - "movl $8, 4(%%esp);" // Close fileno #8 - "int $0x30;" - "movl %%edi, %%esp;" // Restore esp. - : - : "r" (base), - "i" (SYS_CLOSE) - : "%esp", "%eax", "%edi"); + // Call write() with space for 4 parameters (should be fine). + asm volatile ( + "movl %%esp, %%edi;" + "movl %0, %%esp;" // Set stack pointer to right below page boundary. + "movl %1, (%%esp);" // Try to call SYS_CLOSE + "movl $8, 4(%%esp);" // Close fileno #8 + "int $0x30;" + "movl %%edi, %%esp;" // Restore esp. + : + : "r" (base), + "i" (SYS_CLOSE) + : "%esp", "%eax", "%edi"); - write(STDOUT_FILENO, "OK\n", 3); + write(STDOUT_FILENO, "OK\n", 3); - // Reserve space for 1 parameter (open requires 2). - base = page - sizeof(int) * 1; + // Reserve space for 1 parameter (open requires 2). + base = page - sizeof(int) * 1; - // Call write() with space for 3 parameters (the kernel should kill us for doing this). - asm volatile ( - "movl %%esp, %%edi;" - "movl %0, %%esp;" // Set stack pointer to right below page boundary. - "movl %1, (%%esp);" // Try to call SYS_CLOSE - // "movl $8, 4(%%esp);" // Close fileno #8 - "int $0x30;" - "movl %%edi, %%esp;" // Restore esp in case we do not crash (as we should). - : - : "r" (base), - "i" (SYS_CLOSE) - : "%esp", "%eax", "%edi"); + // Call write() with space for 3 parameters (the kernel should kill us for doing this). + asm volatile ( + "movl %%esp, %%edi;" + "movl %0, %%esp;" // Set stack pointer to right below page boundary. + "movl %1, (%%esp);" // Try to call SYS_CLOSE + // "movl $8, 4(%%esp);" // Close fileno #8 + "int $0x30;" + "movl %%edi, %%esp;" // Restore esp in case we do not crash (as we should). + : + : "r" (base), + "i" (SYS_CLOSE) + : "%esp", "%eax", "%edi"); - fail("should have died."); + fail("should have died."); } diff --git a/src/tests/filst/sc-bad-nr-1.c b/src/tests/filst/sc-bad-nr-1.c new file mode 100644 index 0000000..4735dfb --- /dev/null +++ b/src/tests/filst/sc-bad-nr-1.c @@ -0,0 +1,24 @@ +#include +#include "tests/lib.h" +#include "tests/main.h" + +// From "lib/user/syscall.c", but modified to push some zeros on the stack +// first, to get a better output for the tests if the stack error message +// remains. +#define syscall0(NUMBER) \ + ({ \ + int retval; \ + asm volatile \ + ("pushl $0; pushl $0; pushl %[number]; int $0x30; addl $4, %%esp" \ + : "=a" (retval) \ + : [number] "i" (NUMBER) \ + : "memory"); \ + retval; \ + }) + +void test_main(void) +{ + // Call a syscall that is one larger than the largest syscall number. + syscall0(SYS_NUMBER_OF_CALLS); + fail("Should have failed."); +} diff --git a/src/tests/filst/sc-bad-nr-1.ck b/src/tests/filst/sc-bad-nr-1.ck new file mode 100644 index 0000000..89b640c --- /dev/null +++ b/src/tests/filst/sc-bad-nr-1.ck @@ -0,0 +1,15 @@ +# -*- perl -*- +use strict; +use warnings; +use tests::tests; +check_expected ([<<'EOF', <<'EOF']); +(sc-bad-nr-1) begin +sc-bad-nr-1: exit(-1) +EOF +(sc-bad-nr-1) begin +Executed an unknown system call! +Stack top + 0: 21 +Stack top + 1: 0 +sc-bad-nr-1: exit(-1) +EOF +pass; diff --git a/src/tests/filst/sc-bad-nr-2.c b/src/tests/filst/sc-bad-nr-2.c new file mode 100644 index 0000000..35fc49e --- /dev/null +++ b/src/tests/filst/sc-bad-nr-2.c @@ -0,0 +1,24 @@ +#include +#include "tests/lib.h" +#include "tests/main.h" + +// From "lib/user/syscall.c", but modified to push some zeros on the stack +// first, to get a better output for the tests if the stack error message +// remains. +#define syscall0(NUMBER) \ + ({ \ + int retval; \ + asm volatile \ + ("pushl $0; pushl $0; pushl %[number]; int $0x30; addl $4, %%esp" \ + : "=a" (retval) \ + : [number] "i" (NUMBER) \ + : "memory"); \ + retval; \ + }) + +void test_main(void) +{ + // Call a syscall that is quite a bit larger than the maximum syscall number. + syscall0(1024 * 1024); + fail("Should have failed."); +} diff --git a/src/tests/filst/sc-bad-nr-2.ck b/src/tests/filst/sc-bad-nr-2.ck new file mode 100644 index 0000000..6580527 --- /dev/null +++ b/src/tests/filst/sc-bad-nr-2.ck @@ -0,0 +1,15 @@ +# -*- perl -*- +use strict; +use warnings; +use tests::tests; +check_expected ([<<'EOF', <<'EOF']); +(sc-bad-nr-2) begin +sc-bad-nr-2: exit(-1) +EOF +(sc-bad-nr-2) begin +Executed an unknown system call! +Stack top + 0: 1048576 +Stack top + 1: 0 +sc-bad-nr-2: exit(-1) +EOF +pass; diff --git a/src/tests/filst/sc-bad-nr-3.c b/src/tests/filst/sc-bad-nr-3.c new file mode 100644 index 0000000..6cde22b --- /dev/null +++ b/src/tests/filst/sc-bad-nr-3.c @@ -0,0 +1,24 @@ +#include +#include "tests/lib.h" +#include "tests/main.h" + +// From "lib/user/syscall.c", but modified to push some zeros on the stack +// first, to get a better output for the tests if the stack error message +// remains. +#define syscall0(NUMBER) \ + ({ \ + int retval; \ + asm volatile \ + ("pushl $0; pushl $0; pushl %[number]; int $0x30; addl $4, %%esp" \ + : "=a" (retval) \ + : [number] "i" (NUMBER) \ + : "memory"); \ + retval; \ + }) + +void test_main(void) +{ + // Call a negative syscall. + syscall0(-1); + fail("Should have failed."); +} diff --git a/src/tests/filst/sc-bad-nr-3.ck b/src/tests/filst/sc-bad-nr-3.ck new file mode 100644 index 0000000..650f050 --- /dev/null +++ b/src/tests/filst/sc-bad-nr-3.ck @@ -0,0 +1,15 @@ +# -*- perl -*- +use strict; +use warnings; +use tests::tests; +check_expected ([<<'EOF', <<'EOF']); +(sc-bad-nr-3) begin +sc-bad-nr-3: exit(-1) +EOF +(sc-bad-nr-3) begin +Executed an unknown system call! +Stack top + 0: -1 +Stack top + 1: 0 +sc-bad-nr-3: exit(-1) +EOF +pass; diff --git a/src/tests/filst/sc-bad-write.c b/src/tests/filst/sc-bad-write.c index fbd6260..18e8f4d 100644 --- a/src/tests/filst/sc-bad-write.c +++ b/src/tests/filst/sc-bad-write.c @@ -15,7 +15,7 @@ #define PGMASK BITMASK(PGSHIFT, PGBITS) /* Page offset bits (0:12). */ static inline void *pg_round_up (const void *va) { - return (void *) (((uintptr_t) va + PGSIZE - 1) & ~PGMASK); + return (void *) (((uintptr_t) va + PGSIZE - 1) & ~PGMASK); } /** @@ -31,49 +31,49 @@ const char *FAIL = "FAIL\n"; void test_main(void) { - // Get the addres of the first unmapped page in the system. - unsigned page = (unsigned)pg_round_up(&_end_bss); + // Get the addres of the first unmapped page in the system. + unsigned page = (unsigned)pg_round_up(&_end_bss); - // Reserve space for 4 parameters. - unsigned base = page - sizeof(int) * 4; + // Reserve space for 4 parameters. + unsigned base = page - sizeof(int) * 4; - // Call write() with space for 4 parameters (should be fine). - asm volatile ( - "movl %%esp, %%edi;" - "movl %0, %%esp;" // Set stack pointer to right below page boundary. - "movl %1, (%%esp);" // Try to call SYS_WRITE - "movl %2, 4(%%esp);" // Write to STDOUT - "movl %3, 8(%%esp);" // Load buffer. - "movl $6, 12(%%esp);" // Write length of string - "int $0x30;" - "movl %%edi, %%esp;" // Restore esp. - : - : "r" (base), - "i" (SYS_WRITE), - "i" (STDOUT_FILENO), - "r" (WORKS) - : "%esp", "%eax", "%edi"); + // Call write() with space for 4 parameters (should be fine). + asm volatile ( + "movl %%esp, %%edi;" + "movl %0, %%esp;" // Set stack pointer to right below page boundary. + "movl %1, (%%esp);" // Try to call SYS_WRITE + "movl %2, 4(%%esp);" // Write to STDOUT + "movl %3, 8(%%esp);" // Load buffer. + "movl $6, 12(%%esp);" // Write length of string + "int $0x30;" + "movl %%edi, %%esp;" // Restore esp. + : + : "r" (base), + "i" (SYS_WRITE), + "i" (STDOUT_FILENO), + "r" (WORKS) + : "%esp", "%eax", "%edi"); - // Reserve space for 3 parameters (write requires 4). - base = page - sizeof(int) * 3; + // Reserve space for 3 parameters (write requires 4). + base = page - sizeof(int) * 3; - // Call write() with space for 3 parameters (the kernel should kill us for doing this). - asm volatile ( - "movl %%esp, %%edi;" - "movl %0, %%esp;" // Set stack pointer to right below page boundary. - "movl %1, (%%esp);" // Try to call SYS_WRITE - "movl %2, 4(%%esp);" // Write to STDOUT - "movl %3, 8(%%esp);" // Load buffer. - //"movl $5, 12(%%esp);" // Can not write the last parameter as we would get a pagefault. - "int $0x30;" - "movl %%edi, %%esp;" // Restore esp in case we do not crash (as we should). - : - : "r" (base), - "i" (SYS_WRITE), - "i" (STDOUT_FILENO), - "r" (FAIL) - : "%esp", "%eax", "%edi"); + // Call write() with space for 3 parameters (the kernel should kill us for doing this). + asm volatile ( + "movl %%esp, %%edi;" + "movl %0, %%esp;" // Set stack pointer to right below page boundary. + "movl %1, (%%esp);" // Try to call SYS_WRITE + "movl %2, 4(%%esp);" // Write to STDOUT + "movl %3, 8(%%esp);" // Load buffer. + //"movl $5, 12(%%esp);" // Can not write the last parameter as we would get a pagefault. + "int $0x30;" + "movl %%edi, %%esp;" // Restore esp in case we do not crash (as we should). + : + : "r" (base), + "i" (SYS_WRITE), + "i" (STDOUT_FILENO), + "r" (FAIL) + : "%esp", "%eax", "%edi"); - fail("should have died."); + fail("should have died."); } -- cgit v1.2.1