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 | /* * Copyright (c) 2020 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <ztest.h> #include <irq_offload.h> #include <syscall_handler.h> #include <ztest_error_hook.h> #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE) #define THREAD_TEST_PRIORITY 5 static K_THREAD_STACK_DEFINE(tstack, STACK_SIZE); static struct k_thread tdata; static ZTEST_BMEM int case_type; /* A semaphore using inside irq_offload */ extern struct k_sem offload_sem; /* test case type */ enum { ZTEST_CATCH_FATAL_ACCESS_NULL, ZTEST_CATCH_FATAL_ILLEAGAL_INSTRUCTION, ZTEST_CATCH_FATAL_DIVIDE_ZERO, ZTEST_CATCH_FATAL_K_PANIC, ZTEST_CATCH_FATAL_K_OOPS, ZTEST_CATCH_FATAL_IN_ISR, ZTEST_CATCH_ASSERT_FAIL, ZTEST_CATCH_ASSERT_IN_ISR, ZTEST_CATCH_USER_FATAL_Z_OOPS, ZTEST_ERROR_MAX } error_case_type; static void trigger_assert_fail(void *a) { /* trigger an assert fail condition */ __ASSERT(a != NULL, "parameter a should not be NULL!"); } static void trigger_fault_illeagl_instuction(void) { void *a = NULL; /* execute an illeagal instructions */ ((void(*)(void))&a)(); } static void trigger_fault_access_null(void) { void *a = NULL; /* access a null of address */ int b = *((int *)a); printk("b is %d\n", b); } static void trigger_fault_divide_zero(void) { int a = 1; int b = 0; /* divde zero */ a = a / b; printk("a is %d\n", a); } static void trigger_fault_oops(void) { k_oops(); } static void trigger_fault_panic(void) { k_panic(); } static void release_offload_sem(void) { /* Semaphore used inside irq_offload need to be * released after assert or fault happened. */ k_sem_give(&offload_sem); } /* This is the fatal error hook that allow you to do actions after * fatal error happened. This is optional, you can choose to define * this yourself. If not, it will use the default one. */ void ztest_post_fatal_error_hook(unsigned int reason, const z_arch_esf_t *pEsf) { switch (case_type) { case ZTEST_CATCH_FATAL_ACCESS_NULL: case ZTEST_CATCH_FATAL_ILLEAGAL_INSTRUCTION: case ZTEST_CATCH_FATAL_DIVIDE_ZERO: case ZTEST_CATCH_FATAL_K_PANIC: case ZTEST_CATCH_FATAL_K_OOPS: case ZTEST_CATCH_USER_FATAL_Z_OOPS: zassert_true(true, NULL); break; /* Unfortunately, the case of trigger a fatal error * inside ISR context still cannot be dealed with, * So please don't use it this way. */ case ZTEST_CATCH_FATAL_IN_ISR: zassert_true(false, NULL); break; default: zassert_true(false, NULL); break; } } /* This is the assert fail post hook that allow you to do actions after * assert fail happened. This is optional, you can choose to define * this yourself. If not, it will use the default one. */ void ztest_post_assert_fail_hook(void) { switch (case_type) { case ZTEST_CATCH_ASSERT_FAIL: ztest_test_pass(); break; case ZTEST_CATCH_ASSERT_IN_ISR: release_offload_sem(); ztest_test_pass(); break; default: ztest_test_fail(); break; } } static void tThread_entry(void *p1, void *p2, void *p3) { int sub_type = *(int *)p1; printk("case type is %d\n", case_type); ztest_set_fault_valid(false); switch (sub_type) { case ZTEST_CATCH_FATAL_ACCESS_NULL: ztest_set_fault_valid(true); trigger_fault_access_null(); break; case ZTEST_CATCH_FATAL_ILLEAGAL_INSTRUCTION: ztest_set_fault_valid(true); trigger_fault_illeagl_instuction(); break; case ZTEST_CATCH_FATAL_DIVIDE_ZERO: ztest_set_fault_valid(true); trigger_fault_divide_zero(); break; case ZTEST_CATCH_FATAL_K_PANIC: ztest_set_fault_valid(true); trigger_fault_panic(); break; case ZTEST_CATCH_FATAL_K_OOPS: ztest_set_fault_valid(true); trigger_fault_oops(); break; default: break; } /* should not reach here */ ztest_test_fail(); } static int run_trigger_thread(int i) { int ret; uint32_t perm = K_INHERIT_PERMS; case_type = i; if (_is_user_context()) { perm = perm | K_USER; } k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, (k_thread_entry_t)tThread_entry, (void *)&case_type, NULL, NULL, K_PRIO_PREEMPT(THREAD_TEST_PRIORITY), perm, K_NO_WAIT); ret = k_thread_join(tid, K_FOREVER); return ret; } /** * @brief Test if a fatal error can be catched * * @details Valid a fatal error we triggered in thread context works. * If the fatal error happened and the program enter assert_post_handler, * that means fatal error triggered as expected. */ void test_catch_fatal_error(void) { #if defined(CONFIG_ARCH_HAS_USERSPACE) run_trigger_thread(ZTEST_CATCH_FATAL_ACCESS_NULL); run_trigger_thread(ZTEST_CATCH_FATAL_ILLEAGAL_INSTRUCTION); run_trigger_thread(ZTEST_CATCH_FATAL_DIVIDE_ZERO); #endif run_trigger_thread(ZTEST_CATCH_FATAL_K_PANIC); run_trigger_thread(ZTEST_CATCH_FATAL_K_OOPS); } /** * @brief Test if catching an assert works * * @details Valid the assert in thread context works or not. If the assert * fail happened and the program enter assert_post_handler, that means * assert works as expected. */ void test_catch_assert_fail(void) { case_type = ZTEST_CATCH_ASSERT_FAIL; ztest_set_assert_valid(false); ztest_set_assert_valid(true); trigger_assert_fail(NULL); ztest_test_fail(); } /* a handler using by irq_offload */ static void tIsr_assert(const void *p) { ztest_set_assert_valid(true); trigger_assert_fail(NULL); } /** * @brief Test if an assert fail works in ISR context * * @details Valid the assert in ISR context works or not. If the assert * fail happened and the program enter assert_post_handler, that means * assert works as expected. */ void test_catch_assert_in_isr(void) { case_type = ZTEST_CATCH_ASSERT_IN_ISR; irq_offload(tIsr_assert, NULL); } #if defined(CONFIG_USERSPACE) static void trigger_z_oops(void *a) { Z_OOPS(*((bool *)a)); } /** * @brief Test if a z_oops can be catched * * @details Valid a z_oops we triggered in thread context works. * If the z_oops happened and the program enter our handler, * that means z_oops triggered as expected. This test only for * userspace. */ void test_catch_z_oops(void) { case_type = ZTEST_CATCH_USER_FATAL_Z_OOPS; ztest_set_fault_valid(true); trigger_z_oops((void *)false); } #endif void test_main(void) { #if defined(CONFIG_USERSPACE) k_thread_access_grant(k_current_get(), &tdata, &tstack); ztest_test_suite(error_hook_tests, ztest_user_unit_test(test_catch_assert_fail), ztest_user_unit_test(test_catch_fatal_error), ztest_unit_test(test_catch_assert_in_isr), ztest_user_unit_test(test_catch_z_oops) ); ztest_run_test_suite(error_hook_tests); #else ztest_test_suite(error_hook_tests, ztest_unit_test(test_catch_fatal_error), ztest_unit_test(test_catch_assert_fail), ztest_unit_test(test_catch_assert_in_isr) ); ztest_run_test_suite(error_hook_tests); #endif } |