diff --git a/mm/mm_heap/mm_free.c b/mm/mm_heap/mm_free.c index 894719b056fa0..16b275cc0f3ba 100644 --- a/mm/mm_heap/mm_free.c +++ b/mm/mm_heap/mm_free.c @@ -72,6 +72,9 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem) FAR struct mm_freenode_s *node; FAR struct mm_freenode_s *prev; FAR struct mm_freenode_s *next; +#ifdef CONFIG_MM_KASAN + size_t memsize; +#endif int ret; UNUSED(ret); @@ -84,12 +87,8 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem) return; } - kasan_poison(mem, mm_malloc_size(mem)); - if (mm_takesemaphore(heap) == false) { - kasan_unpoison(mem, mm_malloc_size(mem)); - /* Meet -ESRCH return, which means we are in situations * during context switching(See mm_takesemaphore() & getpid()). * Then add to the delay list. @@ -99,6 +98,10 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem) return; } +#ifdef CONFIG_MM_KASAN + memsize = mm_malloc_size(mem); +#endif + DEBUGASSERT(mm_heapmember(heap, mem)); /* Map the memory chunk into a free node */ @@ -175,5 +178,10 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem) /* Add the merged node to the nodelist */ mm_addfreechunk(heap, node); + +#ifdef CONFIG_MM_KASAN + kasan_poison(mem, memsize); +#endif + mm_givesemaphore(heap); }