This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 9e58127339d arch/sim: Add MM_FILL_ALLOCATIONS support to sim 
customized heap.
9e58127339d is described below

commit 9e58127339d620c2ac533595f897842ed784fab5
Author: yushuailong <[email protected]>
AuthorDate: Tue May 26 20:14:27 2026 +0800

    arch/sim: Add MM_FILL_ALLOCATIONS support to sim customized heap.
    
    When CONFIG_MM_UMM_CUSTOMIZE_MANAGER is enabled, the sim heap
    bypasses mm_heap/tlsf entirely and calls host malloc/free directly,
    so MM_FILL_ALLOCATIONS has no effect. Add fill pattern support
    directly in sim_ummheap.c:
    - malloc: fill user region with 0xaa
    - free: fill user region with 0x55
    - realloc: fill extended region with 0xaa
    
    This helps detect uninitialized reads in sim environment,
    which ASan does not support.
    
    Signed-off-by: yushuailong <[email protected]>
---
 arch/sim/src/sim/sim_ummheap.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/arch/sim/src/sim/sim_ummheap.c b/arch/sim/src/sim/sim_ummheap.c
index ea187b625ca..78626b53843 100644
--- a/arch/sim/src/sim/sim_ummheap.c
+++ b/arch/sim/src/sim/sim_ummheap.c
@@ -316,6 +316,10 @@ static void delay_free(struct mm_heap_s *heap, void *mem, 
bool delay)
   else
     {
       node = (struct mm_allocnode_s *)((uintptr_t)mem - MM_ALLOCNODE_SIZE);
+#ifdef CONFIG_MM_FILL_ALLOCATIONS
+      memset(mem, MM_FREE_MAGIC,
+             node->size - MM_ALLOCNODE_SIZE - node->padding);
+#endif
       update_stats(heap, mem, node->size, false);
       sched_note_heap(NOTE_HEAP_FREE, heap, mem, node->size, 0);
       host_free(node->allocmem);
@@ -329,6 +333,7 @@ static void *reallocate(void *oldmem, size_t alignment, 
size_t size)
   void *new_alloc_addr;
   struct mm_allocnode_s *old_node;
   size_t old_size;
+  size_t old_user_size;
   void *old_alloc_addr;
   void *mem = oldmem;
 
@@ -373,6 +378,10 @@ static void *reallocate(void *oldmem, size_t alignment, 
size_t size)
 
       mem = init_allocnode(g_mmheap, new_alloc_addr, aligned_size,
                            padding_size);
+#ifdef CONFIG_MM_FILL_ALLOCATIONS
+      memset(mem, MM_ALLOC_MAGIC,
+             aligned_size - MM_ALLOCNODE_SIZE - padding_size);
+#endif
       sched_note_heap(NOTE_HEAP_ALLOC, g_mmheap, mem, aligned_size, 0);
       return mem;
     }
@@ -391,6 +400,8 @@ static void *reallocate(void *oldmem, size_t alignment, 
size_t size)
              ((uintptr_t)oldmem - MM_ALLOCNODE_SIZE);
   old_alloc_addr = old_node->allocmem;
   old_size = old_node->size;
+  old_user_size = old_size - MM_ALLOCNODE_SIZE - old_node->padding;
+  UNUSED(old_user_size);
 
   update_stats(g_mmheap, oldmem, old_size, false);
 
@@ -411,12 +422,28 @@ static void *reallocate(void *oldmem, size_t alignment, 
size_t size)
       old_node->padding = padding_size;
       MM_ADD_BACKTRACE(g_mmheap, old_node);
       update_stats(g_mmheap, oldmem, aligned_size, true);
+#ifdef CONFIG_MM_FILL_ALLOCATIONS
+      if (aligned_size > old_size)
+        {
+          memset(oldmem + old_user_size, MM_ALLOC_MAGIC,
+                 aligned_size - old_size);
+        }
+#endif
+
       sched_note_heap(NOTE_HEAP_ALLOC, g_mmheap, mem, aligned_size, 0);
       return oldmem;
     }
 
   sched_note_heap(NOTE_HEAP_FREE, g_mmheap, oldmem, old_size, 0);
   mem = init_allocnode(g_mmheap, new_alloc_addr, aligned_size, padding_size);
+#ifdef CONFIG_MM_FILL_ALLOCATIONS
+  if (aligned_size > old_size)
+    {
+      memset(mem + old_user_size, MM_ALLOC_MAGIC,
+             aligned_size - old_size);
+    }
+#endif
+
   sched_note_heap(NOTE_HEAP_ALLOC, g_mmheap, mem, aligned_size, 0);
   return mem;
 }

Reply via email to