casaroli commented on code in PR #22180:
URL: https://github.com/apache/nuttx/pull/22180#discussion_r4665489475


##########
mm/tlsf/mm_tlsf.c:
##########
@@ -62,8 +62,24 @@
 struct mm_delaynode_s
 {
   FAR struct mm_delaynode_s *flink;
+  mm_delaycb_t cb;  /* Optional callback, NULL means free */

Review Comment:
   `struct mm_delaynode_s` is now 16 bytes on a 64-bit CPU, but the smallest 
tlsf pool block is `tlsf_align_size()` bytes (line 1176), 8 there. 
`umm_delayfree()` of a `malloc(8)` block writes `cb` into the next block: on 
qemu-armv8a:nsh with tlsf and `CONFIG_MM_HEAP_MEMPOOL_THRESHOLD=64`, the next 
block, filled with 0xaa, reads 00 after `umm_delayfree()`.
   
   Fix: make the smallest pool block at least `sizeof(struct mm_delaynode_s)` 
(here and for `CONFIG_MM_MIN_BLKSIZE` in mm_initialize.c), or assert it in 
`mm_delayfreecb()`.



##########
mm/mm_heap/mm_free.c:
##########
@@ -41,9 +41,11 @@
  * Private Functions
  ****************************************************************************/
 
-static void add_delaylist(FAR struct mm_heap_s *heap, FAR void *mem)
+static void add_delaylist(FAR struct mm_heap_s *heap, FAR void *mem,
+                          mm_delaycb_t cb, bool asan_check)
 {
 #if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
+  FAR struct mm_delayhead_s *delay = get_delayhead(heap);

Review Comment:
   `get_delayhead()` reads `this_cpu()` before `mm_lock_irq()`, and 
`mm_lock_irq()` only disables the interrupts of this CPU (mm_lock.c:142). A 
task preempted between the two lines can go on on another CPU and change the 
list of the first CPU while that CPU changes it too: a block is lost or freed 
twice. Before this PR `this_cpu()` was read after `mm_lock_irq()`.
   
   Fix: call `get_delayhead()` after `mm_lock_irq()`, here, in free_delaylist() 
(mm_malloc.c:63) and in mm_tlsf.c:377 and :462.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to