修訂 | 3538ba34e0e415105d3fe235605e6dba1597ad98 (tree) |
---|---|
時間 | 2019-10-01 02:29:07 |
作者 | Kjetil Oftedal <oftedal@gmai...> |
Commiter | Waldemar Brodkorb |
malloc: Add missing locks for some paths (valloc/memalign/posix_memalign)
The internal heap structures were not protected properly in
memalign(). If multiple threads were concurrently allocating memory and
one of them were requesting aligned memory via valloc,memalign or
posix_memalign the internal heap data structures could be corrupted.
Signed-off-by: Kjetil Oftedal <oftedal@gmail.com>
@@ -77,7 +77,9 @@ memalign (size_t alignment, size_t size) | ||
77 | 77 | init_size = addr - tot_addr; |
78 | 78 | } |
79 | 79 | |
80 | + __heap_lock (&__malloc_heap_lock); | |
80 | 81 | __heap_free (heap, base, init_size); |
82 | + __heap_unlock (&__malloc_heap_lock); | |
81 | 83 | |
82 | 84 | /* Remember that we've freed the initial part of MEM. */ |
83 | 85 | base += init_size; |
@@ -85,9 +87,11 @@ memalign (size_t alignment, size_t size) | ||
85 | 87 | |
86 | 88 | /* Return the end part of MEM to the heap, unless it's too small. */ |
87 | 89 | end_addr = addr + size; |
88 | - if (end_addr + MALLOC_REALLOC_MIN_FREE_SIZE < tot_end_addr) | |
90 | + if (end_addr + MALLOC_REALLOC_MIN_FREE_SIZE < tot_end_addr) { | |
91 | + __heap_lock (&__malloc_heap_lock); | |
89 | 92 | __heap_free (heap, (void *)end_addr, tot_end_addr - end_addr); |
90 | - else | |
93 | + __heap_unlock (&__malloc_heap_lock); | |
94 | + } else | |
91 | 95 | /* We didn't free the end, so include it in the size. */ |
92 | 96 | end_addr = tot_end_addr; |
93 | 97 |