<div dir="ltr">Recently, it appears that there was a bug introduced in chunk allocation. The bug is exposed by this small snippet of code:<div><br></div><div><font face="monospace, monospace">  void* mem = malloc(128*1024*1024);</font></div><div><font face="monospace, monospace">  printf("mem address %p\n", mem);</font></div><div><font face="monospace, monospace">  free(mem);</font></div><div><font face="monospace, monospace">  void* large_alloc = malloc(0x80000081UL);</font></div><div><span style="font-family:monospace,monospace">  printf("large mem %p\n", large_alloc);</span><br></div><div><font face="monospace, monospace">  free(large_alloc);</font></div><div><br></div><div>It looks like the bug is in the chunk_recycle code, in this piece of code:<br></div><div><br></div><div><div><font face="monospace, monospace">        if (new_addr != NULL) {</font></div><div><font face="monospace, monospace">                extent_node_t key;</font></div><div><font face="monospace, monospace">                extent_node_init(&key, arena, new_addr, alloc_size, false);</font></div><div><font face="monospace, monospace">                node = extent_tree_ad_search(chunks_ad, &key);</font></div><div><font face="monospace, monospace">        } else {</font></div><div><font face="monospace, monospace">                node = chunk_first_fit(arena, chunks_szad, chunks_ad,</font></div><div><font face="monospace, monospace">                    alloc_size);</font></div><div><font face="monospace, monospace">        }</font></div><div><font face="monospace, monospace">        if (node == NULL || (new_addr != NULL && extent_node_size_get(node) <</font></div><div><font face="monospace, monospace">            size)) {</font></div><div><font face="monospace, monospace">                malloc_mutex_unlock(&arena->chunks_mtx);</font></div><div><font face="monospace, monospace">                return (NULL);</font></div><div><font face="monospace, monospace">        }</font></div></div><div><br></div><div>The problem is that new_addr == NULL, so the size check is not performed. In my testing, removing the new_addr != NULL check fixes the problem, but I don't know if that's the correct change.</div><div><br></div><div>The first allocation after the free shows the problem, if you try and use the whole memory allocation it might segfault, or let you scribble all over someone else's memory.</div><div><br></div><div>Christopher Ferris</div><div>(<a href="mailto:cferris@google.com">cferris@google.com</a>)</div></div>