Hi,
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:
#[no_mangle]
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);
assert(handle);
entry_t entry = dlsym(handle, "entry");
assert(entry);
for (int i = 0; 1; i++) {
entry(i);
dlclose(handle);
handle = dlopen(argv[1], RTLD_LAZY | RTLD_LOCAL | RTLD_FIRST);
assert(handle);
entry = dlsym(handle, "entry");
assert(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.