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

Avoid __cxa_thread_atexit_impl on Emscripten #106779

Merged
merged 2 commits into from
Jan 26, 2023
Merged

Conversation

RReverser
Copy link
Contributor

@RReverser RReverser commented Jan 12, 2023

See discussion in both issues.

The TL;DR is that weak linkage causes LLVM to produce broken Wasm, presumably due to pointer mismatch. The code is casting a void pointer to a function pointer with specific signature, but Wasm is very strict about function pointer compatibility, so the resulting code is invalid.

Ideally LLVM should catch this earlier in the process rather than emit invalid Wasm, but it currently doesn't and this is an easy and valid fix, given that Emcripten doesn't have __cxa_thread_atexit_impl these days anyway.

Unfortunately, I can't add a regression test as even after looking into this issue for a long time, I couldn't reproduce it with any minimal Rust example, only with extracted LLVM IR or on a large project involving Rust + C++.

 - Fixes rust-lang#91628.
 - Fixes emscripten-core/emscripten#15722.

See discussion in both issues.

The TL;DR is that weak linkage causes LLVM to produce broken Wasm, presumably due to pointer mismatch. The code is casting a void pointer to a function pointer with specific signature, but Wasm is very strict about function pointer compatibility, so the resulting code is invalid.

Ideally LLVM should catch this earlier in the process rather than emit invalid Wasm, but it currently doesn't and this is an easy and valid fix, given that Emcripten doesn't have `__cxa_thread_atexit_impl` these days anyway.

Unfortunately, I can't add a regression test as even after looking into this issue for a long time, I couldn't reproduce it with any minimal Rust example, only with extracted LLVM IR or on a large project involving Rust + C++.

r? @alexcrichton
@rustbot
Copy link
Collaborator

rustbot commented Jan 12, 2023

Failed to set assignee to alexcrichton: invalid assignee

Note: Only org members, users with write permissions, or people who have commented on the PR may be assigned.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 12, 2023
@rustbot
Copy link
Collaborator

rustbot commented Jan 12, 2023

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@Noratrieb
Copy link
Member

r? libs

@RReverser
Copy link
Contributor Author

For those curious, also raised an LLVM issue for the underlying problem llvm/llvm-project#60003, but this Rust-specific fix should be valid regardless.

@Mark-Simulacrum
Copy link
Member

@bors r+

Seems okay.

@bors
Copy link
Contributor

bors commented Jan 14, 2023

📌 Commit 6155b9a has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 14, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 14, 2023
Avoid __cxa_thread_atexit_impl on Emscripten

 - Fixes rust-lang#91628.
 - Fixes emscripten-core/emscripten#15722.

See discussion in both issues.

The TL;DR is that weak linkage causes LLVM to produce broken Wasm, presumably due to pointer mismatch. The code is casting a void pointer to a function pointer with specific signature, but Wasm is very strict about function pointer compatibility, so the resulting code is invalid.

Ideally LLVM should catch this earlier in the process rather than emit invalid Wasm, but it currently doesn't and this is an easy and valid fix, given that Emcripten doesn't have `__cxa_thread_atexit_impl` these days anyway.

Unfortunately, I can't add a regression test as even after looking into this issue for a long time, I couldn't reproduce it with any minimal Rust example, only with extracted LLVM IR or on a large project involving Rust + C++.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 14, 2023
Avoid __cxa_thread_atexit_impl on Emscripten

 - Fixes rust-lang#91628.
 - Fixes emscripten-core/emscripten#15722.

See discussion in both issues.

The TL;DR is that weak linkage causes LLVM to produce broken Wasm, presumably due to pointer mismatch. The code is casting a void pointer to a function pointer with specific signature, but Wasm is very strict about function pointer compatibility, so the resulting code is invalid.

Ideally LLVM should catch this earlier in the process rather than emit invalid Wasm, but it currently doesn't and this is an easy and valid fix, given that Emcripten doesn't have `__cxa_thread_atexit_impl` these days anyway.

Unfortunately, I can't add a regression test as even after looking into this issue for a long time, I couldn't reproduce it with any minimal Rust example, only with extracted LLVM IR or on a large project involving Rust + C++.
@matthiaskrgr
Copy link
Member

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 14, 2023
@RReverser
Copy link
Contributor Author

A little odd that I didn't see it unused in my testing, but okay, re-added the #[cfg_attr(target_family = "wasm", allow(unused))] attribute in the new place.

@RReverser
Copy link
Contributor Author

r? @Mark-Simulacrum @matthiaskrgr

@rustbot
Copy link
Collaborator

rustbot commented Jan 23, 2023

Could not assign reviewer from: Mark-Simulacrum.
User(s) Mark-Simulacrum are either the PR author or are already assigned, and there are no other candidates.
Use r? to specify someone else to assign.

@RReverser
Copy link
Contributor Author

@Mark-Simulacrum Sorry, not sure whether to trigger re-review somehow or just ask again.

@Mark-Simulacrum
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Jan 24, 2023

📌 Commit a41c5f9 has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Jan 24, 2023

🌲 The tree is currently closed for pull requests below priority 10. This pull request will be tested once the tree is reopened.

@bors bors removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jan 24, 2023
@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jan 24, 2023
@Mark-Simulacrum
Copy link
Member

@rustbot ready in the future will update labels to waiting-on-review.

bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 26, 2023
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#97373 (impl DispatchFromDyn for Cell and UnsafeCell)
 - rust-lang#106625 (Remove backwards compat for LLVM 12 coverage format)
 - rust-lang#106779 (Avoid __cxa_thread_atexit_impl on Emscripten)
 - rust-lang#106811 (Append .dwp to the binary filename instead of replacing the existing extension.)
 - rust-lang#106836 (Remove optimistic spinning from `mpsc::SyncSender`)
 - rust-lang#106946 (implement Hash for proc_macro::LineColumn)
 - rust-lang#107074 (remove unnecessary check for opaque types)
 - rust-lang#107287 (Improve fn pointer notes)
 - rust-lang#107304 (Use `can_eq` to compare types for default assoc type error)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit cc92bdb into rust-lang:master Jan 26, 2023
@rustbot rustbot added this to the 1.69.0 milestone Jan 26, 2023
@RReverser RReverser deleted the patch-2 branch February 4, 2023 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
6 participants