Skip to content

Commit e53d549

Browse files
Enjia Mainashif
authored andcommitted
tests: mem_protect: add a test case of adding memory partition
Add a test case to validate when adding a new partition into a memory domain with over its maximum specified limit number, an assertion failure happens. Signed-off-by: Maksim Masalski <[email protected]> Signed-off-by: Enjia Mai <[email protected]>
1 parent 9de14e8 commit e53d549

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ void test_main(void)
7676
ztest_unit_test(test_create_new_higher_prio_thread_from_user),
7777
ztest_unit_test(test_create_new_invalid_prio_thread_from_user),
7878
ztest_unit_test(test_mark_thread_exit_uninitialized),
79+
ztest_unit_test(test_mem_part_assert_add_overmax),
7980
ztest_user_unit_test(test_kobject_init_error),
8081
ztest_unit_test(test_alloc_kobjects),
8182
ztest_unit_test(test_thread_alloc_out_of_idx),

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ void test_mem_domain_setup(void)
4848
struct k_mem_partition *parts[] = { &ro_part, &ztest_mem_partition };
4949

5050
num_rw_parts = max_parts - PARTS_USED;
51-
zassert_true(NUM_RW_PARTS >= num_rw_parts,
52-
"CONFIG_MAX_DOMAIN_PARTITIONS incorrectly tuned, %d should be at least %d",
53-
CONFIG_MAX_DOMAIN_PARTITIONS, max_parts);
51+
zassert_true(num_rw_parts <= NUM_RW_PARTS,
52+
"CONFIG_MAX_DOMAIN_PARTITIONS incorrectly tuned, %d should be at least %d",
53+
CONFIG_MAX_DOMAIN_PARTITIONS, max_parts);
5454
zassert_true(num_rw_parts > 0, "no free memory partitions");
5555

5656
k_mem_domain_init(&test_domain, ARRAY_SIZE(parts), parts);
@@ -383,7 +383,6 @@ void test_mem_part_overlap(void)
383383
k_mem_domain_add_partition(&test_domain, &overlap_part);
384384
}
385385

386-
387386
extern struct k_spinlock z_mem_domain_lock;
388387

389388
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)
399398
}
400399
}
401400

401+
static volatile uint8_t __aligned(MEM_REGION_ALLOC)
402+
exceed_buf[MEM_REGION_ALLOC];
403+
404+
K_MEM_PARTITION_DEFINE(exceed_part, exceed_buf, sizeof(exceed_buf),
405+
K_MEM_PARTITION_P_RW_U_RW);
406+
407+
/**
408+
* @brief Test system assert when adding memory partitions more than possible
409+
*
410+
* @details
411+
* - Add memory partitions one by one and more than architecture allows to add.
412+
* - When partitions added more than it is allowed by architecture, test that
413+
* system assert for that case works correctly.
414+
*
415+
* @ingroup kernel_memprotect_tests
416+
*/
417+
void test_mem_part_assert_add_overmax(void)
418+
{
419+
int max_parts = num_rw_parts + PARTS_USED;
420+
421+
/* Make sure the partitions of the domain is full, used in
422+
* previous test cases.
423+
*/
424+
zassert_equal(max_parts, arch_mem_domain_max_partitions_get(),
425+
"domain still have room of partitions(%d).",
426+
max_parts);
427+
428+
need_recover_spinlock = true;
429+
set_fault_valid(true);
430+
431+
/* Add one more partition will trigger assert due to exceeding */
432+
k_mem_domain_add_partition(&test_domain, &exceed_part);
433+
434+
/* should not reach here */
435+
ztest_test_fail();
436+
}
437+
438+
402439
#if defined(CONFIG_ASSERT)
403440
static volatile uint8_t __aligned(MEM_REGION_ALLOC) misc_buf[MEM_REGION_ALLOC];
404441
K_MEM_PARTITION_DEFINE(find_no_part, misc_buf, sizeof(misc_buf),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ extern void test_create_new_invalid_prio_thread_from_user(void);
5454
extern void test_mark_thread_exit_uninitialized(void);
5555
extern void test_krnl_obj_static_alloc_build_time(void);
5656
extern void test_mem_part_overlap(void);
57+
extern void test_mem_part_assert_add_overmax(void);
5758
extern void test_kobject_access_grant_error(void);
5859
extern void test_kobject_access_grant_error_user(void);
5960
extern void test_kobject_access_grant_error_user_null(void);

0 commit comments

Comments
 (0)