From d6d9677c93a6d5e710af704156f9a9dee3c66016 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Str=C3=B6mb=C3=A4ck?= Date: Thu, 7 May 2020 16:16:06 +0200 Subject: Added a few more parameter-validating tests. --- src/tests/filst/Make.tests | 2 +- src/tests/filst/sc-bad-align.c | 45 +++++++++++++++++++++++++++++++++++++++ src/tests/filst/sc-bad-align.ck | 9 ++++++++ src/tests/filst/sc-bad-exit.c | 47 +++++++++++++++++++++++++++++++++++++++++ src/tests/filst/sc-bad-exit.ck | 9 ++++++++ src/tests/filst/sc-bad-write.c | 4 ++-- 6 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 src/tests/filst/sc-bad-align.c create mode 100644 src/tests/filst/sc-bad-align.ck create mode 100644 src/tests/filst/sc-bad-exit.c create mode 100644 src/tests/filst/sc-bad-exit.ck (limited to 'src/tests') diff --git a/src/tests/filst/Make.tests b/src/tests/filst/Make.tests index dfd179f..c9ad119 100644 --- a/src/tests/filst/Make.tests +++ b/src/tests/filst/Make.tests @@ -3,7 +3,7 @@ tests/%.output: FSDISK = 2 tests/%.output: PUTFILES = $(filter-out os.dsk, $^) -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_TESTS = $(addprefix tests/filst/,sc-bad-write sc-bad-close sc-bad-nr-1 sc-bad-nr-2 sc-bad-nr-3 sc-bad-align sc-bad-exit) tests/filst_PROGS = $(tests/filst_TESTS) diff --git a/src/tests/filst/sc-bad-align.c b/src/tests/filst/sc-bad-align.c new file mode 100644 index 0000000..51b0065 --- /dev/null +++ b/src/tests/filst/sc-bad-align.c @@ -0,0 +1,45 @@ +#include +#include +#include "tests/lib.h" +#include "tests/main.h" + +/** + * From threads/vaddr.h: + */ +#define BITMASK(SHIFT, CNT) (((1ul << (CNT)) - 1) << (SHIFT)) + +#define PGSHIFT 0 /* Index of first offset bit. */ +#define PGBITS 12 /* Number of offset bits. */ +#define PGSIZE (1 << PGBITS) /* Bytes in a page. */ +#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); +} + +/** + * External symbol which address is the first address after all data in the BSS segment. + */ +extern int _end_bss; + +void test_main(void) +{ + // Get the address of the first unmapped page in the process. + unsigned page = (unsigned)pg_round_up(&_end_bss); + + // Reserve space for a part of the syscall number (1 out of 4 bytes). + unsigned base = page - 1; + + // Call a syscall. + asm volatile ( + "movl %%esp, %%edi;" // Save the stack pointer in case we survive. + "movl %0, %%esp;" // Set stack pointer. + "movb $0, (%%esp);" // Set the first byte of the syscall number to zero. + "int $0x30;" // Trigger syscall. + "movl %%edi, %%esp;" // Restore the old stack if we survivied. + : + : "r" (base) + : "%esp", "%eax", "%edi"); + + fail("should have died."); +} diff --git a/src/tests/filst/sc-bad-align.ck b/src/tests/filst/sc-bad-align.ck new file mode 100644 index 0000000..aeb7020 --- /dev/null +++ b/src/tests/filst/sc-bad-align.ck @@ -0,0 +1,9 @@ +# -*- perl -*- +use strict; +use warnings; +use tests::tests; +check_expected ([<<'EOF']); +(sc-bad-align) begin +sc-bad-align: exit(-1) +EOF +pass; diff --git a/src/tests/filst/sc-bad-exit.c b/src/tests/filst/sc-bad-exit.c new file mode 100644 index 0000000..19945fc --- /dev/null +++ b/src/tests/filst/sc-bad-exit.c @@ -0,0 +1,47 @@ +#include +#include +#include "tests/lib.h" +#include "tests/main.h" + +/** + * From threads/vaddr.h: + */ +#define BITMASK(SHIFT, CNT) (((1ul << (CNT)) - 1) << (SHIFT)) + +#define PGSHIFT 0 /* Index of first offset bit. */ +#define PGBITS 12 /* Number of offset bits. */ +#define PGSIZE (1 << PGBITS) /* Bytes in a page. */ +#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); +} + +/** + * External symbol which address is the first address after all data in the BSS segment. + */ +extern int _end_bss; + +void test_main(void) +{ + // Get the address of the first unmapped page in the process. + unsigned page = (unsigned)pg_round_up(&_end_bss); + + // Reserve space for the syscall number and a part of the exit code (1 out of 4 bytes). + unsigned base = page - sizeof(int) - 1; + + // Call a syscall. + asm volatile ( + "movl %%esp, %%edi;" // Save the stack pointer in case we survive. + "movl %0, %%esp;" // Set stack pointer. + "movl %1, (%%esp);" // Try to call SYS_EXIT + "movb $1, 4(%%esp);" // Set the first byte of the syscall number to zero. + "int $0x30;" // Trigger syscall. + "movl %%edi, %%esp;" // Restore the old stack if we survivied. + : + : "r" (base), + "i" (SYS_EXIT) + : "%esp", "%eax", "%edi"); + + fail("should have died."); +} diff --git a/src/tests/filst/sc-bad-exit.ck b/src/tests/filst/sc-bad-exit.ck new file mode 100644 index 0000000..1e6fa95 --- /dev/null +++ b/src/tests/filst/sc-bad-exit.ck @@ -0,0 +1,9 @@ +# -*- perl -*- +use strict; +use warnings; +use tests::tests; +check_expected ([<<'EOF']); +(sc-bad-exit) begin +sc-bad-exit: exit(-1) +EOF +pass; diff --git a/src/tests/filst/sc-bad-write.c b/src/tests/filst/sc-bad-write.c index 18e8f4d..c6f23f7 100644 --- a/src/tests/filst/sc-bad-write.c +++ b/src/tests/filst/sc-bad-write.c @@ -31,7 +31,7 @@ const char *FAIL = "FAIL\n"; void test_main(void) { - // Get the addres of the first unmapped page in the system. + // Get the address of the first unmapped page in the process. unsigned page = (unsigned)pg_round_up(&_end_bss); // Reserve space for 4 parameters. @@ -44,7 +44,7 @@ void test_main(void) "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 + "movl $6, 12(%%esp);" // Write length of string "int $0x30;" "movl %%edi, %%esp;" // Restore esp. : -- cgit v1.2.1