-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
ctor not running for statically linked libraries #27
Comments
Could you confirm which version you are using (ie: paste your Cargo.toml here)? |
I tried to have a minimal repro and investigated further, and I resulted in a behaviour I don't understand. The repro is attached to this comment.
fn main() {
println!("Main execution");
#[cfg(feature = "assert")]
assert_eq!(1, unsafe {
lib::VALUE.load(std::sync::atomic::Ordering::Acquire)
});
}
[package]
name = "app"
version = "0.1.0"
edition = "2018"
[dependencies]
lib = { path = "../lib" }
#[macro_use]
extern crate ctor;
#[macro_use]
extern crate libc_print;
use std::sync::atomic::{AtomicUsize, Ordering};
pub static mut VALUE: AtomicUsize = AtomicUsize::new(0);
#[ctor]
fn startup() {
unsafe {
VALUE.fetch_add(1, Ordering::AcqRel);
}
libc_print!("Startup lib\r\n");
}
#[dtor]
fn tear_down() {
libc_print!("Tear down lib\r\n");
}
fn unused() {
let _ = unsafe { VALUE.load(Ordering::Acquire) };
}
[package]
name = "lib"
version = "0.1.0"
authors = ["Frédéric Vauchelles <fredpointzero@gmail.com>"]
edition = "2018"
[dependencies]
libc-print = "0.1.8"
ctor = "0.1.10" This is the strange behaviour I get:
So this is weird, I can't tell if this is working when it does not access the static variable. Also, I have my issue when using the crate
|
In my project using the The ctor and dtor of the library project are not executed until I access a static variable defined in the library from the main application. |
Interesting. I wonder if this is an unused symbol getting pruned. Thanks for the repro - I'll poke around. |
I confirmed this is definitely an issue. Looks like there's some sort of whole-module pruning going on. |
Hi, do you have any updates on this? |
Nothing yet. I was able to repro it with your steps, but I feel like this might be an LLVM/rustc bug. |
Hi, do you have any updates on this? Is this an LLVM/rustc bug as you suggested? |
I can reproduce this on macOS Mojave and Rust 1.39.0 as well. However, it seems that I am able to get the constructor to run at least with the following minimal application: //! lib.rs
#[ctor::ctor]
fn on_startup() {
println!("Starting up!");
}
#[ctor::dtor]
unsafe fn on_shutdown() {
libc::printf("Shutting down!\n\0".as_ptr() as *const i8);
}
pub fn unused() {} //! main.rs
use foo::unused;
fn main() {
unused();
println!("Running");
} The output produced by this application is:
If I comment out the
I was unable to get the destructor working with the extern "C" fn on_shutdown() {
unsafe { libc::printf("Shutting down!\n\0".as_ptr() as *const i8) };
} And then add the following call to unsafe { libc::atexit(on_shutdown) }; The application now works as expected:
In short, it seems that a few tweaks to the way your application is written will get the constructor and destructor to run:
I'm not sure what is going on, but I am also leaning towards the possibility of an issue with Rust or LLVM. |
I thought the whole point of the |
Just encountered this issue as well. In a debug build all 233 Sadly, @ebkalderon solution did not fix it for me, however I'm on Windows. It definitely feels like some sort of "whole-module pruning", as @mmastrac said, as in my case either none or all |
Would someone have some bandwidth to file an upstream bug? This definitely seems like a Rust core issue - |
This may be fixed now, however I'll need someone with the issue to attempt to repro. |
Marking as fixed as upstream is fixed. |
Hi,
I have an issue with the following setup:
When using
#[ctor]
attribute inside the crate lib, it is not called when running the binary built with app.Using a rust library dependency statically link it so it should also include the ctor function, but it does not seems to be the case.
Am I missing something or is this an unsupported use case?
The text was updated successfully, but these errors were encountered: