Rust library loaded and unloaded with dlopen/dlclose crashes on macOS


What are you allowed to do in a rust library that you dlopen and dlclose? How should you build such a library (with/without the system allocator, as a dylib or a cdylib)?

I'm trying to experiment with putting my rust code in a library to be able to reload it without having to restart the application. As an initial experiment I wrote a small library with a single function that prints and a main driver that opens the library, calls it's function and closes the library.

Here is the library:
pub extern "C" fn entry(i: i32) {
println!("Reanimator! {}", i);

and here is the main program (written in C, I also have a Rust version, but that is longer and exhibits the same problem):
#include <dlfcn.h>
#include <stdlib.h>
#include <assert.h>

typedef void (*entry_t)(int);
int main(int argc, char **argv) {
    if (argc < 2) abort();
    void *handle = dlopen(argv[1], RTLD_LAZY | RTLD_LOCAL | RTLD_FIRST);
    entry_t entry = dlsym(handle, "entry");
    for (int i = 0; 1; i++) {
        handle = dlopen(argv[1], RTLD_LAZY | RTLD_LOCAL | RTLD_FIRST);
        entry = dlsym(handle, "entry");

When I run the main program it crashes:
Reanimator! 0
Reanimator! 1
Reanimator! 2
Reanimator! 3
Reanimator! 4
Reanimator! 5
Reanimator! 6
Reanimator! 7
Reanimator! 8
Reanimator! 9
Reanimator! 10
Reanimator! 11
Reanimator! 12
Reanimator! 13
Reanimator! 14
Reanimator! 15
Reanimator! 16
Reanimator! 17
Reanimator! 18
Reanimator! 19
Reanimator! 20
Reanimator! 21
Reanimator! 22
Reanimator! 23
Reanimator! 24
Reanimator! 25
Reanimator! 26
Reanimator! 27
Reanimator! 28
Reanimator! 29
Reanimator! 30
Reanimator! 31
main(37209,0x7fffcdc343c0) malloc: *** error for object 0x7fe49ff00b28: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

I tried running it in the debugger (rust-lldb), but I don't really know what to look for, and I don't know how to get source code for the stdlib.

You could run it in Valgrind to find more about the bad memory allocation (although Apple hasn't released sources for macOS Sierra, so Valgrind is only available for older OS X, or try Linux if the crash happens regardless of platform)


I just tried on Linux, and do not get the same problem, and there valgrind reports no errors. However, I still get the error on macOS.

I don't even know if I'm doing anything wrong, or if I'm running into a bug.

Just FYI, this is incorrect: was updated for Sierra back in November.

May or may not be related to this