diff --git a/tests/kernel/mem_protect/mem_protect/src/main.c b/tests/kernel/mem_protect/mem_protect/src/main.c index 134ab87d820..1bdaf157ffc 100644 --- a/tests/kernel/mem_protect/mem_protect/src/main.c +++ b/tests/kernel/mem_protect/mem_protect/src/main.c @@ -76,6 +76,7 @@ void test_main(void) ztest_unit_test(test_create_new_higher_prio_thread_from_user), ztest_unit_test(test_create_new_invalid_prio_thread_from_user), ztest_unit_test(test_mark_thread_exit_uninitialized), + ztest_unit_test(test_mem_part_assert_add_overmax), ztest_user_unit_test(test_kobject_init_error), ztest_unit_test(test_alloc_kobjects), ztest_unit_test(test_thread_alloc_out_of_idx), diff --git a/tests/kernel/mem_protect/mem_protect/src/mem_domain.c b/tests/kernel/mem_protect/mem_protect/src/mem_domain.c index 272e16e454e..2ca7ec3807c 100644 --- a/tests/kernel/mem_protect/mem_protect/src/mem_domain.c +++ b/tests/kernel/mem_protect/mem_protect/src/mem_domain.c @@ -48,9 +48,9 @@ void test_mem_domain_setup(void) struct k_mem_partition *parts[] = { &ro_part, &ztest_mem_partition }; num_rw_parts = max_parts - PARTS_USED; - zassert_true(NUM_RW_PARTS >= num_rw_parts, - "CONFIG_MAX_DOMAIN_PARTITIONS incorrectly tuned, %d should be at least %d", - CONFIG_MAX_DOMAIN_PARTITIONS, max_parts); + zassert_true(num_rw_parts <= NUM_RW_PARTS, + "CONFIG_MAX_DOMAIN_PARTITIONS incorrectly tuned, %d should be at least %d", + CONFIG_MAX_DOMAIN_PARTITIONS, max_parts); zassert_true(num_rw_parts > 0, "no free memory partitions"); k_mem_domain_init(&test_domain, ARRAY_SIZE(parts), parts); @@ -383,7 +383,6 @@ void test_mem_part_overlap(void) k_mem_domain_add_partition(&test_domain, &overlap_part); } - extern struct k_spinlock z_mem_domain_lock; static ZTEST_BMEM bool need_recover_spinlock; @@ -399,6 +398,44 @@ void post_fatal_error_handler(unsigned int reason, const z_arch_esf_t *pEsf) } } +static volatile uint8_t __aligned(MEM_REGION_ALLOC) + exceed_buf[MEM_REGION_ALLOC]; + +K_MEM_PARTITION_DEFINE(exceed_part, exceed_buf, sizeof(exceed_buf), + K_MEM_PARTITION_P_RW_U_RW); + +/** + * @brief Test system assert when adding memory partitions more than possible + * + * @details + * - Add memory partitions one by one and more than architecture allows to add. + * - When partitions added more than it is allowed by architecture, test that + * system assert for that case works correctly. + * + * @ingroup kernel_memprotect_tests + */ +void test_mem_part_assert_add_overmax(void) +{ + int max_parts = num_rw_parts + PARTS_USED; + + /* Make sure the partitions of the domain is full, used in + * previous test cases. + */ + zassert_equal(max_parts, arch_mem_domain_max_partitions_get(), + "domain still have room of partitions(%d).", + max_parts); + + need_recover_spinlock = true; + set_fault_valid(true); + + /* Add one more partition will trigger assert due to exceeding */ + k_mem_domain_add_partition(&test_domain, &exceed_part); + + /* should not reach here */ + ztest_test_fail(); +} + + #if defined(CONFIG_ASSERT) static volatile uint8_t __aligned(MEM_REGION_ALLOC) misc_buf[MEM_REGION_ALLOC]; K_MEM_PARTITION_DEFINE(find_no_part, misc_buf, sizeof(misc_buf), diff --git a/tests/kernel/mem_protect/mem_protect/src/mem_protect.h b/tests/kernel/mem_protect/mem_protect/src/mem_protect.h index 8597298a934..88bbc0c5a4e 100644 --- a/tests/kernel/mem_protect/mem_protect/src/mem_protect.h +++ b/tests/kernel/mem_protect/mem_protect/src/mem_protect.h @@ -54,6 +54,7 @@ extern void test_create_new_invalid_prio_thread_from_user(void); extern void test_mark_thread_exit_uninitialized(void); extern void test_krnl_obj_static_alloc_build_time(void); extern void test_mem_part_overlap(void); +extern void test_mem_part_assert_add_overmax(void); extern void test_kobject_access_grant_error(void); extern void test_kobject_access_grant_error_user(void); extern void test_kobject_access_grant_error_user_null(void);