Description
This was found by @RalfJung in #45955.
alloc_system::System::alloc
calls libc::malloc
which is defined as:
extern {
pub fn malloc(size: size_t) -> *mut c_void;
}
When #[global_allocator]
is not used, the current default for executables is alloc_jemalloc
, which links jemalloc as configured in src/liballoc_jemalloc/build.rs
. On most platforms, this is without --with-jemalloc-prefix
, which causes jemalloc to define an unprefixed malloc
symbol that "overrides" libc’s and ends up being used by alloc_system
.
So alloc_system
doesn’t do what the name suggests, in this situation.
Like https://github.com/alexcrichton/jemallocator/issues/19 this problem will disappear when alloc_jemalloc
is eventually removed, but in the meantime alloc_system
doesn’t always do what the name says it does.
We stopped prefixing jemalloc symbols in #31460 in order to make LLVM use them. Could we perhaps only do this when compiling a compiler? Or perhaps the compiler could switch to using the jemallocator
crate, which would gain a Cargo feature flag to disable prefixing? It would do something like that anyway to keep that LLVM+jemalloc benefit when alloc_jemalloc
is removed and the default for executables is changed to alloc_system
.