microsoft / microsoft/mimalloc

Crash when tracking enabled

Open
#1,211 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
13.4k
Forks
1.2k
Avg merge
4d 45m
Merged PRs (30d)
13

Description

We have a huge project that uses mimalloc, linked via static object override (i.e. we put `mimalloc.o` as the first object in the list).
We made the following code change locally: (I'm writing here a simplified but working version)

```
// track.h

#else
// no tracking

#include

#define MI_TRACK_ENABLED 1
#define MI_TRACK_HEAP_DESTROY 1
#define MI_TRACK_TOOL "stderr"

#define mi_track_malloc_size(p,reqsize,size,zero) (void)write(2, "A", 1)
#define mi_track_free_size(p,_size) (void)write(2, "F", 1)
```

curiously, this crashes both in linux and freebsd, both with CXX on and off, but works on MacOs.
the executable prints a lot of "A"s and "F"s (so tracking _does_ work) but at some point, another library that we link invokes the default `free`:

(here's the output of lldb in linux)

```
munmap_chunk(): invalid pointer

* thread #1, name = 'test', stop reason = signal SIGABRT
* frame #0: 0x0000fa0eceb93c18 libc.so.6`__pthread_kill_implementation(threadid=274941449560096, signo=6, no_tid=) at pthread_kill.c:44:76
frame #1: 0x0000fa0eceb4a8ac libc.so.6`__GI_raise(sig=6) at raise.c:26:13
...
frame #4: 0x0000fa0eceb9e1bc libc.so.6`malloc_printerr(str="") at malloc.c:5660:3
frame #5: 0x0000fa0eceb9e3bc libc.so.6`munmap_chunk(p=0x000005358a67c3d0) at malloc.c:3054:5
frame #6: 0x0000fa0eceba2a34 libc.so.6`__GI___libc_free(mem=) at malloc.c:3375:7
...
frame #12: 0x0000fa0ecefee470 libhs.so.5`hs_compile_multi + 132
...
```

I suspect that this behaviour is due to the following line:
```
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__) && !MI_TRACK_ENABLED
// gcc, clang: use aliasing to alias the exported function to one of our `mi_` functions
```
but I'm not really sure this is intentional.
What's precisely the role of MI_TRACK_ENABLED?

thanks in advance for the help.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with track.h and the conditional around MI_TRACK_ENABLED, then reproduce the static mimalloc object override on Linux or FreeBSD with tracking enabled. Inspect the default free path shown in the lldb trace through hs_compile_multi; done means establishing the intended role of MI_TRACK_ENABLED and documenting or correcting the behavior that causes the invalid-pointer crash.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.