mm: Using Macros Instead of Memory to Fill Labels

Signed-off-by: wangmingrong <wangmingrong@xiaomi.com>
This commit is contained in:
wangmingrong 2024-03-04 12:17:29 +08:00 committed by Xiang Xiao
parent 737b9c843d
commit d2fd043575
7 changed files with 26 additions and 8 deletions

View File

@ -139,6 +139,10 @@
#define MM_DUMP_LEAK(dump, pid) \
((dump) == PID_MM_LEAK && (pid) >= 0 && nxsched_get_tcb(pid) == NULL)
#define MM_INIT_MAGIC 0xcc
#define MM_ALLOC_MAGIC 0xaa
#define MM_FREE_MAGIC 0x55
/****************************************************************************
* Public Types
****************************************************************************/

View File

@ -322,8 +322,10 @@ config MM_FILL_ALLOCATIONS
bool "Fill allocations with debug value"
default n
---help---
Fill all malloc() allocations with 0xAA. This helps
detecting uninitialized variable errors.
Fill all malloc() allocations with MM_ALLOC_MAGIC.
Fill all add_addregion() with MM_INIT_MAGIC.
Fill all free() with MM_FREE_MAGIC.
This helps detecting uninitialized variable errors.
config MM_BACKTRACE
int "The depth of backtrace"

View File

@ -274,7 +274,7 @@ retry:
}
#ifdef CONFIG_MM_FILL_ALLOCATIONS
memset(blk, 0xaa, pool->blocksize);
memset(blk, MM_ALLOC_MAGIC, pool->blocksize);
#endif
#if CONFIG_MM_BACKTRACE >= 0
@ -317,7 +317,7 @@ void mempool_free(FAR struct mempool_s *pool, FAR void *blk)
#endif
#ifdef CONFIG_MM_FILL_ALLOCATIONS
memset(blk, 0x55, pool->blocksize);
memset(blk, MM_FREE_MAGIC, pool->blocksize);
#endif
if (pool->interruptsize > blocksize)

View File

@ -90,7 +90,7 @@ void mm_delayfree(FAR struct mm_heap_s *heap, FAR void *mem, bool delay)
}
#ifdef CONFIG_MM_FILL_ALLOCATIONS
memset(mem, 0x55, mm_malloc_size(heap, mem));
memset(mem, MM_FREE_MAGIC, mm_malloc_size(heap, mem));
#endif
kasan_poison(mem, mm_malloc_size(heap, mem));

View File

@ -129,6 +129,12 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart,
DEBUGASSERT(heapsize <= MMSIZE_MAX + 1);
#endif
#ifdef CONFIG_MM_FILL_ALLOCATIONS
/* Use the fill value to mark uninitialized user memory */
memset(heapstart, MM_INIT_MAGIC, heapsize);
#endif
/* Register to KASan for access check */
kasan_register(heapstart, &heapsize);

View File

@ -305,7 +305,7 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
MM_ADD_BACKTRACE(heap, node);
kasan_unpoison(ret, mm_malloc_size(heap, ret));
#ifdef CONFIG_MM_FILL_ALLOCATIONS
memset(ret, 0xaa, alignsize - MM_ALLOCNODE_OVERHEAD);
memset(ret, MM_ALLOC_MAGIC, alignsize - MM_ALLOCNODE_OVERHEAD);
#endif
#ifdef CONFIG_DEBUG_MM
minfo("Allocated %p, size %zu\n", ret, alignsize);

View File

@ -503,7 +503,7 @@ static void mm_delayfree(FAR struct mm_heap_s *heap, FAR void *mem,
if (mm_lock(heap) == 0)
{
#ifdef CONFIG_MM_FILL_ALLOCATIONS
memset(mem, 0x55, mm_malloc_size(heap, mem));
memset(mem, MM_FREE_MAGIC, mm_malloc_size(heap, mem));
#endif
kasan_poison(mem, mm_malloc_size(heap, mem));
@ -575,6 +575,12 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart,
# define idx 0
#endif
#ifdef CONFIG_MM_FILL_ALLOCATIONS
/* Use the fill value to mark uninitialized user memory */
memset(heapstart, 0xcc, heapsize);
#endif
/* Register to KASan for access check */
kasan_register(heapstart, &heapsize);
@ -1144,7 +1150,7 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
kasan_unpoison(ret, mm_malloc_size(heap, ret));
#ifdef CONFIG_MM_FILL_ALLOCATIONS
memset(ret, 0xaa, mm_malloc_size(heap, ret));
memset(ret, 0xaa, nodesize);
#endif
}