Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It looks like some #[ctor] was missing #220

Closed
kookyleo opened this issue Jun 16, 2022 · 2 comments
Closed

It looks like some #[ctor] was missing #220

kookyleo opened this issue Jun 16, 2022 · 2 comments

Comments

@kookyleo
Copy link

I came across something that wasn't easy to understand,
When I referenced a crate that used rust-ctor, It seems that not all of the functions with the #[ctor] flag in Crate are executed, looks like something has been optimized out somewhere, but it seems normal to test crate alone.

I've written an example for simplicity, https://github.com/kookyleo/a-ctor-failed-case

git clone https://github.com/kookyleo/a-ctor-failed-case.git
cd a-ctor-failed-case
cargo run

And then you can see the weird results.

I got the same results at ubuntu@x86_64 and mac@m1

@mmastrac
Copy link
Owner

Interesting. Thanks for the repro. I haven't tested anything, but perhaps it has something to do with all the functions having the same name and contents. The compiler may be aliasing them all to the same function and leaving just a single implementation.

What I'd try as a next step is:

  1. Rename the functions and see if it fails again

  2. Change the contents of each function to be different (perhaps add a different constant in each one)

I'll see if I can get around to looking at this at some point.

@mmastrac
Copy link
Owner

I confirmed that this is because the other file contains no other used functions. Rust is pruning the module entirely, despite it being marked as used.

By adding a fake function "foo" to the bar.rs file and calling that from lib.rs, the counter is updated correctly. This is definitely an upstream problem!

facebook-github-bot pushed a commit to facebook/buck2 that referenced this issue May 15, 2023
Summary:
Work around [this issue](mmastrac/rust-ctor#220): linker on macos erases `#[ctor]` calls.

More details in [this workplace post](https://fb.workplace.com/groups/buck2dev/posts/3459190721035642).

All `LateBinding` for `buck2` binary are initialized from `main` functions, while each test crate still have `#[ctor]` which calls the initialization: this `#[ctor]` won't be erased because there's a dummy test next to it.

This workaround makes initialization verbose, and I'm not very happy about it. But it is safe: initialization is always performed before any buck2 implementation or tests, it either always work or never works.

Reviewed By: krallin

Differential Revision: D45845229

fbshipit-source-id: 96a4ff7d9161e45de3c698efb404047f892cf799
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants