@@ -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-
387386extern struct k_spinlock z_mem_domain_lock ;
388387
389388static 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 )
403440static volatile uint8_t __aligned (MEM_REGION_ALLOC ) misc_buf [MEM_REGION_ALLOC ];
404441K_MEM_PARTITION_DEFINE (find_no_part , misc_buf , sizeof (misc_buf ),
0 commit comments