From 416eca5b132dcfb7b80f348dab779fdae885d72c Mon Sep 17 00:00:00 2001 From: Andrew Boie Date: Fri, 14 Jul 2017 20:31:15 -0700 Subject: [PATCH] tests: fatal: enable x86 MMU stack protection Show that this mechanism can detect stack overflows with the guard page. We only do it once since are are in an alternate IA HW task after it happens. Signed-off-by: Andrew Boie --- tests/kernel/fatal/prj-x86-mmu.conf | 3 +++ tests/kernel/fatal/prj.conf | 1 + tests/kernel/fatal/src/main.c | 28 ++++++++++++++++++++-------- tests/kernel/fatal/testcase.yaml | 5 +++++ 4 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 tests/kernel/fatal/prj-x86-mmu.conf diff --git a/tests/kernel/fatal/prj-x86-mmu.conf b/tests/kernel/fatal/prj-x86-mmu.conf new file mode 100644 index 00000000000..2bdf1ff4717 --- /dev/null +++ b/tests/kernel/fatal/prj-x86-mmu.conf @@ -0,0 +1,3 @@ +CONFIG_X86_MMU=y +CONFIG_X86_STACK_PROTECTION=y + diff --git a/tests/kernel/fatal/prj.conf b/tests/kernel/fatal/prj.conf index 1bf885697e9..3c5ffb4ec28 100644 --- a/tests/kernel/fatal/prj.conf +++ b/tests/kernel/fatal/prj.conf @@ -1,2 +1,3 @@ CONFIG_STACK_SENTINEL=y +CONFIG_X86_STACK_PROTECTION=n diff --git a/tests/kernel/fatal/src/main.c b/tests/kernel/fatal/src/main.c index f96b6c2ce2c..805452a7f7d 100644 --- a/tests/kernel/fatal/src/main.c +++ b/tests/kernel/fatal/src/main.c @@ -123,6 +123,8 @@ void stack_thread2(void) void main(void) { + int expected_reason; + rv = TC_PASS; TC_START("test_fatal"); @@ -195,9 +197,19 @@ void main(void) (k_thread_entry_t)stack_thread1, NULL, NULL, NULL, K_PRIO_PREEMPT(PRIORITY), 0, K_NO_WAIT); - if (crash_reason != _NANO_ERR_STACK_CHK_FAIL) { + +#ifdef CONFIG_X86_STACK_PROTECTION + /* x86 double-faults when stack overflow in kernel mode, we're + * in an alternate IA HW task when we finish the test + */ + expected_reason = _NANO_ERR_KERNEL_PANIC; +#else + expected_reason = _NANO_ERR_STACK_CHK_FAIL; +#endif + + if (crash_reason != expected_reason) { TC_ERROR("bad reason code got %d expected %d\n", - crash_reason, _NANO_ERR_KERNEL_PANIC); + crash_reason, expected_reason); rv = TC_FAIL; } if (rv == TC_FAIL) { @@ -207,19 +219,18 @@ void main(void) TC_PRINT("PASS\n"); } - TC_PRINT("test stack overflow - swap\n"); #ifdef CONFIG_STACK_SENTINEL + /* Stack sentinel has to be invoked, make sure it happens during + * a context switch + */ + TC_PRINT("test stack overflow - swap\n"); k_thread_create(&alt_thread, overflow_stack, OVERFLOW_STACKSIZE, -#else - k_thread_create(&alt_thread, alt_stack, - K_THREAD_STACK_SIZEOF(alt_stack), -#endif (k_thread_entry_t)stack_thread2, NULL, NULL, NULL, K_PRIO_PREEMPT(PRIORITY), 0, K_NO_WAIT); if (crash_reason != _NANO_ERR_STACK_CHK_FAIL) { TC_ERROR("bad reason code got %d expected %d\n", - crash_reason, _NANO_ERR_KERNEL_PANIC); + crash_reason, _NANO_ERR_STACK_CHK_FAIL); rv = TC_FAIL; } if (rv == TC_FAIL) { @@ -228,6 +239,7 @@ void main(void) } else { TC_PRINT("PASS\n"); } +#endif out: TC_END_RESULT(rv); TC_END_REPORT(rv); diff --git a/tests/kernel/fatal/testcase.yaml b/tests/kernel/fatal/testcase.yaml index c53377176ba..c8a0b002e27 100644 --- a/tests/kernel/fatal/testcase.yaml +++ b/tests/kernel/fatal/testcase.yaml @@ -6,3 +6,8 @@ tests: arch_whitelist: arc extra_args: CONF_FILE=prj-arc.conf tags: core ignore_faults +- x86-mmu-stack-checking: + platform_whitelist: qemu_x86 qemu_x86_iamcu + extra_args: CONF_FILE=prj-x86-mmu.conf + tags: core ignore_faults +