Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | /*
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <ztest.h>
#include <tc_util.h>
#include <kernel_structs.h>
#include <irq_offload.h>
#include <kswap.h>
#if defined(CONFIG_USERSPACE)
#include <syscall_handler.h>
#include "test_syscalls.h"
#endif
#if defined(CONFIG_X86) && defined(CONFIG_X86_MMU)
#define STACKSIZE (8192)
#else
#define STACKSIZE (2048 + CONFIG_TEST_EXTRA_STACKSIZE)
#endif
#define MAIN_PRIORITY 7
#define PRIORITY 5
static K_THREAD_STACK_DEFINE(alt_stack, STACKSIZE);
#if defined(CONFIG_STACK_SENTINEL) && !defined(CONFIG_ARCH_POSIX)
#define OVERFLOW_STACKSIZE (STACKSIZE / 2)
static k_thread_stack_t *overflow_stack =
alt_stack + (STACKSIZE - OVERFLOW_STACKSIZE);
#else
#if defined(CONFIG_USERSPACE) && defined(CONFIG_ARC)
/* for ARC, privilege stack is merged into defined stack */
#define OVERFLOW_STACKSIZE (STACKSIZE + CONFIG_PRIVILEGED_STACK_SIZE)
#else
#define OVERFLOW_STACKSIZE STACKSIZE
#endif
#endif
static struct k_thread alt_thread;
volatile int rv;
static ZTEST_DMEM volatile int expected_reason = -1;
void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *pEsf)
{
TC_PRINT("Caught system error -- reason %d\n", reason);
if (expected_reason == -1) {
printk("Was not expecting a crash\n");
k_fatal_halt(reason);
}
if (k_current_get() != &alt_thread) {
printk("Wrong thread crashed\n");
k_fatal_halt(reason);
}
if (reason != expected_reason) {
printk("Wrong crash type got %d expected %d\n", reason,
expected_reason);
k_fatal_halt(reason);
}
expected_reason = -1;
}
void alt_thread1(void)
{
expected_reason = K_ERR_CPU_EXCEPTION;
#if defined(CONFIG_X86)
__asm__ volatile ("ud2");
#elif defined(CONFIG_NIOS2)
__asm__ volatile ("trap");
#elif defined(CONFIG_ARC)
__asm__ volatile ("swi");
#else
/* Triggers usage fault on ARM, illegal instruction on RISCV
* and xtensa
*/
{
volatile long illegal = 0;
((void(*)(void))&illegal)();
}
#endif
rv = TC_FAIL;
}
void alt_thread2(void)
{
unsigned int key;
expected_reason = K_ERR_KERNEL_OOPS;
key = irq_lock();
k_oops();
TC_ERROR("SHOULD NEVER SEE THIS\n");
rv = TC_FAIL;
irq_unlock(key);
}
void alt_thread3(void)
{
unsigned int key;
expected_reason = K_ERR_KERNEL_PANIC;
key = irq_lock();
k_panic();
TC_ERROR("SHOULD NEVER SEE THIS\n");
rv = TC_FAIL;
irq_unlock(key);
}
void alt_thread4(void)
{
expected_reason = K_ERR_KERNEL_PANIC;
__ASSERT(0, "intentionally failed assertion");
rv = TC_FAIL;
}
void alt_thread5(void)
{
unsigned int key;
expected_reason = INT_MAX;
key = irq_lock();
z_except_reason(INT_MAX);
TC_ERROR("SHOULD NEVER SEE THIS\n");
rv = TC_FAIL;
irq_unlock(key);
}
#ifndef CONFIG_ARCH_POSIX
#ifdef CONFIG_STACK_SENTINEL
void blow_up_stack(void)
{
char buf[OVERFLOW_STACKSIZE];
expected_reason = K_ERR_STACK_CHK_FAIL;
TC_PRINT("posting %zu bytes of junk to stack...\n", sizeof(buf));
(void)memset(buf, 0xbb, sizeof(buf));
}
#else
/* stack sentinel doesn't catch it in time before it trashes the entire kernel
*/
int stack_smasher(int val)
{
return stack_smasher(val * 2) + stack_smasher(val * 3);
}
void blow_up_stack(void)
{
expected_reason = K_ERR_STACK_CHK_FAIL;
stack_smasher(37);
}
#if defined(CONFIG_USERSPACE)
void z_impl_blow_up_priv_stack(void)
{
blow_up_stack();
}
static inline void z_vrfy_blow_up_priv_stack(void)
{
z_impl_blow_up_priv_stack();
}
#include <syscalls/blow_up_priv_stack_mrsh.c>
#endif /* CONFIG_USERSPACE */
#endif /* CONFIG_STACK_SENTINEL */
void stack_sentinel_timer(void)
{
/* We need to guarantee that we receive an interrupt, so set a
* k_timer and spin until we die. Spinning alone won't work
* on a tickless kernel.
*/
static struct k_timer timer;
blow_up_stack();
k_timer_init(&timer, NULL, NULL);
k_timer_start(&timer, K_MSEC(1), K_NO_WAIT);
while (true) {
}
}
void stack_sentinel_swap(void)
{
unsigned int key = irq_lock();
/* Test that stack overflow check due to swap works */
blow_up_stack();
TC_PRINT("swapping...\n");
z_swap_unlocked();
TC_ERROR("should never see this\n");
rv = TC_FAIL;
irq_unlock(key);
}
void stack_hw_overflow(void)
{
/* Test that HW stack overflow check works */
blow_up_stack();
TC_ERROR("should never see this\n");
rv = TC_FAIL;
}
#if defined(CONFIG_USERSPACE)
void user_priv_stack_hw_overflow(void)
{
/* Test that HW stack overflow check works
* on a user thread's privilege stack.
*/
blow_up_priv_stack();
TC_ERROR("should never see this\n");
rv = TC_FAIL;
}
#endif /* CONFIG_USERSPACE */
void check_stack_overflow(void *handler, u32_t flags)
{
#ifdef CONFIG_STACK_SENTINEL
/* When testing stack sentinel feature, the overflow stack is a
* smaller section of alt_stack near the end.
* In this way when it gets overflowed by blow_up_stack() we don't
* corrupt anything else and prevent the test case from completing.
*/
k_thread_create(&alt_thread, overflow_stack, OVERFLOW_STACKSIZE,
#else
k_thread_create(&alt_thread, alt_stack,
K_THREAD_STACK_SIZEOF(alt_stack),
#endif /* CONFIG_STACK_SENTINEL */
(k_thread_entry_t)handler,
NULL, NULL, NULL, K_PRIO_PREEMPT(PRIORITY), flags,
K_NO_WAIT);
zassert_not_equal(rv, TC_FAIL, "thread was not aborted");
}
#endif /* !CONFIG_ARCH_POSIX */
/**
* @brief Test the kernel fatal error handling works correctly
* @details Manually trigger the crash with various ways and check
* that the kernel is handling that properly or not. Also the crash reason
* should match. Check for stack sentinel feature by overflowing the
* thread's stack and check for the exception.
*
* @ingroup kernel_common_tests
*/
void test_fatal(void)
{
rv = TC_PASS;
/*
* Main thread(test_main) priority was 10 but ztest thread runs at
* priority -1. To run the test smoothly make both main and ztest
* threads run at same priority level.
*/
k_thread_priority_set(_current, K_PRIO_PREEMPT(MAIN_PRIORITY));
#ifndef CONFIG_ARCH_POSIX
TC_PRINT("test alt thread 1: generic CPU exception\n");
k_thread_create(&alt_thread, alt_stack,
K_THREAD_STACK_SIZEOF(alt_stack),
(k_thread_entry_t)alt_thread1,
NULL, NULL, NULL, K_PRIO_COOP(PRIORITY), 0,
K_NO_WAIT);
zassert_not_equal(rv, TC_FAIL, "thread was not aborted");
#else
/*
* We want the native OS to handle segfaults so we can debug it
* with the normal linux tools
*/
TC_PRINT("test alt thread 1: skipped for POSIX ARCH\n");
#endif
TC_PRINT("test alt thread 2: initiate kernel oops\n");
k_thread_create(&alt_thread, alt_stack,
K_THREAD_STACK_SIZEOF(alt_stack),
(k_thread_entry_t)alt_thread2,
NULL, NULL, NULL, K_PRIO_COOP(PRIORITY), 0,
K_NO_WAIT);
k_thread_abort(&alt_thread);
zassert_not_equal(rv, TC_FAIL, "thread was not aborted");
TC_PRINT("test alt thread 3: initiate kernel panic\n");
k_thread_create(&alt_thread, alt_stack,
K_THREAD_STACK_SIZEOF(alt_stack),
(k_thread_entry_t)alt_thread3,
NULL, NULL, NULL, K_PRIO_COOP(PRIORITY), 0,
K_NO_WAIT);
k_thread_abort(&alt_thread);
zassert_not_equal(rv, TC_FAIL, "thread was not aborted");
TC_PRINT("test alt thread 4: fail assertion\n");
k_thread_create(&alt_thread, alt_stack,
K_THREAD_STACK_SIZEOF(alt_stack),
(k_thread_entry_t)alt_thread4,
NULL, NULL, NULL, K_PRIO_COOP(PRIORITY), 0,
K_NO_WAIT);
k_thread_abort(&alt_thread);
zassert_not_equal(rv, TC_FAIL, "thread was not aborted");
TC_PRINT("test alt thread 5: initiate arbitrary SW exception\n");
k_thread_create(&alt_thread, alt_stack,
K_THREAD_STACK_SIZEOF(alt_stack),
(k_thread_entry_t)alt_thread5,
NULL, NULL, NULL, K_PRIO_COOP(PRIORITY), 0,
K_NO_WAIT);
k_thread_abort(&alt_thread);
zassert_not_equal(rv, TC_FAIL, "thread was not aborted");
#ifndef CONFIG_ARCH_POSIX
#ifdef CONFIG_STACK_SENTINEL
TC_PRINT("test stack sentinel overflow - timer irq\n");
check_stack_overflow(stack_sentinel_timer, 0);
TC_PRINT("test stack sentinel overflow - swap\n");
check_stack_overflow(stack_sentinel_swap, 0);
#endif /* CONFIG_STACK_SENTINEL */
#ifdef CONFIG_HW_STACK_PROTECTION
/* HW based stack overflow detection.
* Do this twice to show that HW-based solutions work more than
* once.
*/
TC_PRINT("test stack HW-based overflow - supervisor 1\n");
check_stack_overflow(stack_hw_overflow, 0);
TC_PRINT("test stack HW-based overflow - supervisor 2\n");
check_stack_overflow(stack_hw_overflow, 0);
#if defined(CONFIG_FPU) && defined(CONFIG_FPU_SHARING)
TC_PRINT("test stack HW-based overflow (FPU thread) - supervisor 1\n");
check_stack_overflow(stack_hw_overflow, K_FP_REGS);
TC_PRINT("test stack HW-based overflow (FPU thread) - supervisor 2\n");
check_stack_overflow(stack_hw_overflow, K_FP_REGS);
#endif /* CONFIG_FPU && CONFIG_FPU_SHARING */
#endif /* CONFIG_HW_STACK_PROTECTION */
#ifdef CONFIG_USERSPACE
TC_PRINT("test stack HW-based overflow - user 1\n");
check_stack_overflow(stack_hw_overflow, K_USER);
TC_PRINT("test stack HW-based overflow - user 2\n");
check_stack_overflow(stack_hw_overflow, K_USER);
TC_PRINT("test stack HW-based overflow - user priv stack 1\n");
check_stack_overflow(user_priv_stack_hw_overflow, K_USER);
TC_PRINT("test stack HW-based overflow - user priv stack 2\n");
check_stack_overflow(user_priv_stack_hw_overflow, K_USER);
#if defined(CONFIG_FPU) && defined(CONFIG_FPU_SHARING)
TC_PRINT("test stack HW-based overflow (FPU thread) - user 1\n");
check_stack_overflow(stack_hw_overflow, K_USER | K_FP_REGS);
TC_PRINT("test stack HW-based overflow (FPU thread) - user 2\n");
check_stack_overflow(stack_hw_overflow, K_USER | K_FP_REGS);
#endif /* CONFIG_FPU && CONFIG_FPU_SHARING */
#endif /* CONFIG_USERSPACE */
#endif /* !CONFIG_ARCH_POSIX */
}
/*test case main entry*/
void test_main(void)
{
ztest_test_suite(fatal,
ztest_unit_test(test_fatal));
ztest_run_test_suite(fatal);
}
|