Compiling playground v0.0.1 (/playground)
Finished dev [unoptimized + debuginfo] target(s) in 1.40s
Running `target/debug/playground`
timeout: the monitored command dumped core
/playground/tools/entrypoint.sh: line 11: 8 Segmentation fault timeout --signal=KILL ${timeout} "$@"
However, if you do one of the following things, it will compile and run all right:
comment out '#[no_mangle]' , or
rename the 'free' function to something like 'freee' or whatever, just not name it 'free', or
comment out the 'main' function, or
comment out the 'drop(CString::from_raw(data))' part
Am I missing anything obvious here? is this 'free' name colliding with the 'free' word in C or something? but what does the 'main' or 'drop' part have anything to do, given the function is not even called. What is the logic behind this magic?
Yes, it overrides the libc's free, so it is called by the system allocator whenever something is freed on the heap, be it your own Box or something from the Rust runtime. See this:
Well, without main the program will not run at all, so there's nothing to crash. And without drop there's no invalid pointer access, so no reason to crash, just a couple of memory leaks.
Would it be a problem for Rust to disallow overriding library symbols like this, at least by default? There's probably technical issues since the platform controls the linker, which is what's really controlling this, but would there be design issues too? Surely this isn't desired and depended on, perhaps outside of [language_item]s
And indeed, if you #![warn(unsafe_code)], you'll find out it warns on the no_mangle:
warning: declaration of a `no_mangle` function
--> src/main.rs:6:1
|
6 | #[no_mangle]
| ^^^^^^^^^^^^
|
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them