Skip to content

Commit 8474d78

Browse files
Enjia Mainashif
authored andcommitted
tests: memory protect: add some error test cases
Add some error case for initializing memory domain and removing memory partition. Signed-off-by: Enjia Mai <[email protected]>
1 parent f2c3028 commit 8474d78

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

tests/kernel/mem_protect/mem_protect/src/common.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88

99
ZTEST_BMEM volatile bool valid_fault;
1010

11+
extern void post_fatal_error_handler(unsigned int reason, const z_arch_esf_t *pEsf);
12+
1113
void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *pEsf)
1214
{
1315
printk("Caught system error -- reason %d %d\n", reason, valid_fault);
1416
if (valid_fault) {
1517
printk("fatal error expected as part of test case\n");
1618
valid_fault = false; /* reset back to normal */
19+
20+
post_fatal_error_handler(reason, pEsf);
1721
} else {
1822
printk("fatal error was unexpected, aborting\n");
1923
k_fatal_halt(reason);

tests/kernel/mem_protect/mem_protect/src/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ void test_main(void)
3434
ztest_unit_test(test_mem_domain_boot_threads),
3535
ztest_unit_test(test_mem_domain_migration),
3636
ztest_unit_test(test_mem_part_overlap),
37+
ztest_unit_test(test_mem_domain_init_fail),
38+
ztest_unit_test(test_mem_domain_remove_part_fail),
3739

3840
/* mem_partition.c */
3941
ztest_user_unit_test(test_mem_part_assign_bss_vars_zero),

tests/kernel/mem_protect/mem_protect/src/mem_domain.c

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ void test_mem_domain_migration(void)
328328
printk("migrate to new domain\n");
329329
k_mem_domain_add_thread(&test_domain, &child_thread);
330330

331+
/**TESTPOINT: add to existing domain will do nothing */
332+
k_mem_domain_add_thread(&test_domain, &child_thread);
333+
331334
/* set spin_done so the child thread completes */
332335
printk("set test completion\n");
333336
spin_done = true;
@@ -346,7 +349,7 @@ void test_mem_domain_migration(void)
346349
* - System testing
347350
*
348351
* Prerequisite Conditions:
349-
* - N/A
352+
* - N/A
350353
*
351354
* Input Specifications:
352355
* - N/A
@@ -379,3 +382,77 @@ void test_mem_part_overlap(void)
379382

380383
k_mem_domain_add_partition(&test_domain, &overlap_part);
381384
}
385+
386+
387+
extern struct k_spinlock z_mem_domain_lock;
388+
389+
static ZTEST_BMEM bool need_recover_spinlock;
390+
391+
static struct k_mem_domain test_domain_fail;
392+
393+
void post_fatal_error_handler(unsigned int reason, const z_arch_esf_t *pEsf)
394+
{
395+
if (need_recover_spinlock) {
396+
k_spin_release(&z_mem_domain_lock);
397+
398+
need_recover_spinlock = false;
399+
}
400+
}
401+
402+
#if defined(CONFIG_ASSERT)
403+
static volatile uint8_t __aligned(MEM_REGION_ALLOC) misc_buf[MEM_REGION_ALLOC];
404+
K_MEM_PARTITION_DEFINE(find_no_part, misc_buf, sizeof(misc_buf),
405+
K_MEM_PARTITION_P_RO_U_RO);
406+
407+
/**
408+
* @brief Test error case of removing memory partition fail
409+
*
410+
* @details Try to remove a partition not in the domain will cause
411+
* assertion, then triggher an expected fatal error.
412+
* And while the fatal error happened, the memory domain spinlock
413+
* is held, we need to release them to make other follow test case.
414+
*
415+
* @ingroup kernel_memprotect_tests
416+
*/
417+
void test_mem_domain_remove_part_fail(void)
418+
{
419+
struct k_mem_partition *no_parts = &find_no_part;
420+
421+
need_recover_spinlock = true;
422+
set_fault_valid(true);
423+
k_mem_domain_remove_partition(&test_domain, no_parts);
424+
}
425+
#else
426+
void test_mem_domain_remove_part_fail(void)
427+
{
428+
ztest_test_skip();
429+
}
430+
#endif
431+
432+
/**
433+
* @brief Test error case of initializing memory domain fail
434+
*
435+
* @details Try to initialize a domain with invalid partition, then see
436+
* if an expected fatal error happens.
437+
* And while the fatal error happened, the memory domain spinlock
438+
* is held, we need to release them to make other follow test case.
439+
*
440+
* @ingroup kernel_memprotect_tests
441+
*/
442+
void test_mem_domain_init_fail(void)
443+
{
444+
struct k_mem_partition *no_parts[] = {&ro_part, 0};
445+
446+
/* init another domain fail, expected fault happened */
447+
need_recover_spinlock = true;
448+
set_fault_valid(true);
449+
k_mem_domain_init(&test_domain_fail, ARRAY_SIZE(no_parts),
450+
no_parts);
451+
452+
/* For acrh which not CONFIG_ARCH_MEM_DOMAIN_DATA, if assert is off,
453+
* it will reach here.
454+
*/
455+
#if !defined(CONFIG_ASSERT) && defined(CONFIG_ARCH_MEM_DOMAIN_DATA)
456+
zassert_unreachable("should not be here");
457+
#endif
458+
}

tests/kernel/mem_protect/mem_protect/src/mem_protect.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ extern void test_mem_domain_remove_add_partition(void);
2121
extern void test_mem_domain_api_supervisor_only(void);
2222
extern void test_mem_domain_boot_threads(void);
2323
extern void test_mem_domain_migration(void);
24+
extern void test_mem_domain_init_fail(void);
25+
extern void test_mem_domain_remove_part_fail(void);
2426

2527
extern void test_macros_obtain_names_data_bss(void);
2628
extern void test_mem_part_assign_bss_vars_zero(void);

0 commit comments

Comments
 (0)