mimalloc icon indicating copy to clipboard operation
mimalloc copied to clipboard

Erroneous cast to mi_block_t from thread_free in double free check?

Open NicholasGorski opened this issue 6 years ago • 3 comments

I have a gRPC server that segfaults under heavy load, but only when using mimalloc.

When captured in a debugger, the (truncated) stack trace is:

#0  0x0000000000957b30 in mi_block_nextx (null=0x40003c00070, block=0x3, cookie=12204696699405432863)
    at external/mimalloc/include/mimalloc-internal.h:402
#1  0x0000000000957bc8 in mi_block_next (page=0x40003c00070, block=0x3) at external/mimalloc/include/mimalloc-internal.h:423
#2  0x00000000009636a4 in mi_list_contains (page=0x40003c00070, list=0x3, elem=0x40003c04000) at external/mimalloc/src/alloc.c:137
#3  0x000000000096377e in mi_check_is_double_freex (page=0x40003c00070, block=0x40003c04000, n=0x0) at external/mimalloc/src/alloc.c:150
#4  0x0000000000963819 in mi_check_is_double_free (page=0x40003c00070, block=0x40003c04000) at external/mimalloc/src/alloc.c:166
#5  0x0000000000964016 in mi_free (p=0x40003c04000) at external/mimalloc/src/alloc.c:344
#6  0x00000000007c22a4 in gpr_free (p=0x40003c04000) at external/com_github_grpc_grpc/src/core/lib/gpr/alloc.cc:52

Note that list=0x3. I believe the error is on this line of code in alloc.c:150, in this expression:

(const mi_block_t*)mi_atomic_read_ptr_relaxed(mi_atomic_cast(void*,&page->thread_free))

Looking around, the proper way to cast a thread_free to a mi_block_t ought to be mi_tf_block, which would have turned this into nullptr:

static inline mi_block_t* mi_tf_block(mi_thread_free_t tf) {
  return (mi_block_t*)(tf & ~0x03);
}

I'll give this a try locally, and can send over a PR.

NicholasGorski avatar Jan 09 '20 01:01 NicholasGorski

Thanks! This is in secure mode right (since it tries to detect a double free)? I think it is already fixed in the dev branch with commit d596f08 -- can you try running with the latest dev branch?

daanx avatar Jan 09 '20 01:01 daanx

Yup! Good timing, I made the equivalent change and indeed this fixes the problem. (Valgrind was also able to detect this immediately, FWIW.)

I'll use this patch for now to unblock myself and the next release will take care of things. Thanks!

NicholasGorski avatar Jan 09 '20 01:01 NicholasGorski

Thanks. dev is still a bit unstable at this moment, you may want to use dev-abandon; hope to release a new stable version soon though!

daanx avatar Jan 09 '20 02:01 daanx