Skip to content

Fix host code appearing in Wasm binaries #137457

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

Merged
merged 5 commits into from
Aug 23, 2025

Conversation

JayAndJef
Copy link
Contributor

@JayAndJef JayAndJef commented Feb 23, 2025

This is a direct fix for issue 132802.
Followed the outline as follows:

  • give a hard error in bootstrap when using gcc to compile for wasm
  • change our CI to use clang instead of gcc
  • add a test that compiling a sample program for wasm32-unknown doesn't give any linker warnings

The test-various ci job was also changed.

try-job: test-various
try-job: dist-various-1
try-job: dist-various-2
try-job: x86_64-msvc-1

@rustbot
Copy link
Collaborator

rustbot commented Feb 23, 2025

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Kobzol (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added A-run-make Area: port run-make Makefiles to rmake.rs A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-infra Relevant to the infrastructure team, which will review and decide on the PR/issue. labels Feb 23, 2025
@rustbot
Copy link
Collaborator

rustbot commented Feb 23, 2025

This PR modifies tests/run-make/. If this PR is trying to port a Makefile
run-make test to use rmake.rs, please update the
run-make port tracking issue
so we can track our progress. You can either modify the tracking issue
directly, or you can comment on the tracking issue and link this PR.

cc @jieyouxu


fn is_gcc_compiler(path: PathBuf, build: &Build) -> bool {
let cc_output = command(&path).arg("--version").run_capture_stdout(build).stdout();
cc_output.contains("GCC")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think this does what you want. on my machine, cc --version prints "cc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0", even though it's really gcc.

i am not sure how to check this ... maybe the rust equivalent of realpath $(which cc) | rg gcc? or we could check cc.is_like_clang(): https://docs.rs/cc/latest/cc/struct.Tool.html#method.is_like_clang

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to find a gcc-specific flag to run. If all else fails, we can try compiling a C program and check if some headers are defined.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When going down this path I might recommend something like clang -E -dM -x c /dev/null | rg -i clang which will print #define __clang__ 1. That's a test for clang rather than a test for "not gcc" but at this time only clang has support for wasm, so it might work as a detection mechanism nonetheless?

Although IMO rejecting compilers like this is a bit of a tricky business that's bound to have false positives and negatives so personally I'd probably just skip this part and rely on the test you added instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just be a UI test (tests/ui), which are cheaper to run than rmake (one thing to compile rather than 2-3). You'll just need the directives //@ only-wasm32 and //@ compile-flags: -Dlinker-messages.

Please make sure tests have a doc comment describing what they are testing as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I'll rewrite this next week - is there anything else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be clear - this should be a //@ build-pass test without an expected output, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds correct 👍

Copy link
Member

@Kobzol Kobzol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Detecting GCC would be also useful for other things, but it sounds a bit tricky. Left one nit, otherwise the bootstrap changes (modulo GCC detection, which isn't working yet) look fine. I don't know about WASM though, so someone else should look at that.

@@ -314,6 +314,17 @@ than building it.
.entry(*target)
.or_insert_with(|| Target::from_triple(&target.triple));

// compiler-rt c fallbacks for wasm cannot be built with gcc
if (target.triple == "wasm32-unknown-unknown" || target.triple == "wasm32v1-none") // bare metal targets without wasi sdk
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a method to TargetSelection named e.g. is_bare_wasm, or is_wasm_without_wasi or something like that that describes this family of targets?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If going down this compiler-detection route I'd recommend enabling this for all wasm targets, not just the unknown/none targets. The WASI targets have some auto-configuration but the same principle applies here where if gcc is used then that's a bug for WASI as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If going down this compiler-detection route I'd recommend enabling this for all wasm targets, not just the unknown/none targets.

Would this entail some sort of check to see if the wasi sdk is using gcc?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory (unless I'm misremembering something) nothing more than what's already here other than updating this conditional. The detected compiler for WASI targets should make its way to here and always be clang (wasi-sdk doesn't have gcc and won't for the forseeable future)

@jieyouxu
Copy link
Member

cc @alexcrichton (target maintainer for wasm32-unknown-unknown) in case you have any concerns/feedback

@tgross35
Copy link
Contributor

Detecting GCC would be also useful for other things, but it sounds a bit tricky. Left one nit, otherwise the bootstrap changes (modulo GCC detection, which isn't working yet) look fine. I don't know about WASM though, so someone else should look at that.

cc exposes this functionality, maybe we could wrap that rather than recreating it here?

@jieyouxu
Copy link
Member

cc exposes this functionality, maybe we could wrap that rather than recreating it here?

Do note that bootstrap cc unfortunately has to be pinned to a quite an old version, FWIW.

@JayAndJef
Copy link
Contributor Author

Hey yall, just wanted everyone to be aware I'm in a power outage so I probably can't work on this today.

@tgross35
Copy link
Contributor

Hey yall, just wanted everyone to be aware I'm in a power outage so I probably can't work on this today.

There is never any requirement to work on a PR every day, thanks for being active but take your time :)

@JayAndJef
Copy link
Contributor Author

There is never any requirement to work on a PR every day, thanks for being active but take your time :)

Thanks, I'll probably finish the changes on Sunday or so then

@jieyouxu jieyouxu added the O-wasm Target: WASM (WebAssembly), http://webassembly.org/ label Feb 27, 2025
@JayAndJef
Copy link
Contributor Author

I've changed the test to a UI test, and changed the compiler detection. I'm not sure about the consensus on that - are we still going use it? The commits for that can be scrapped if so

@Kobzol
Copy link
Member

Kobzol commented Mar 3, 2025

The UI and runtime tests sadly won't help that much for this issue, because it's possible that we might use Clang on test builders, but GCC on dist builders. But the bootstrap sanity check should hopefully do the trick.

@alexcrichton
Copy link
Member

For wasm specifically targets I think that Clang should be used everywhere because gcc doesn't have support for wasm. If any builder is using gcc for wasm that's definitely a bug that needs fixing (which the tests might help detect?)

@Kobzol
Copy link
Member

Kobzol commented Mar 3, 2025

Well, the dist builders only build stuff, it's not actually tested on most dist builders, which is why #132802 was a thing. So the UI/run-make tests are nice to have, but wouldn't help for this situation. But with the bootstrap sanity test I think it's fine.

You can r=me, unless you want to do more changes.

@alexcrichton
Copy link
Member

Oh, right, yes! (sorry your original comment is clearer now that I've woken up more...)

@JayAndJef
Copy link
Contributor Author

Great, so the sanity test is going to be kept?

@Kobzol
Copy link
Member

Kobzol commented Mar 3, 2025

Yeah, let's keep the tests. Let's see if CI works.

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 3, 2025
fix for issue 132802: x86 code in `wasm32-unknown-unknown` binaries

This is a direct fix for issue [132802](rust-lang#132802).
Followed the outline as follows:
> * give a hard error in bootstrap when using gcc to compile for wasm
> * change our CI to use clang instead of gcc
> * add a test that compiling a sample program for wasm32-unknown doesn't give any linker warnings

The `test-various` ci job was also changed.

try-job: test-various
try-job: dist-various-2
@bors

This comment was marked as outdated.

@rust-log-analyzer

This comment has been minimized.

@bors

This comment was marked as outdated.

@JayAndJef
Copy link
Contributor Author

makes me wonder if there's a better way to detect emcc specifically, or group it into clang

@tgross35
Copy link
Contributor

tgross35 commented Jul 3, 2025

That's probably worth an issue on the cc repo if you're up for writing one up.

@bors2 try

@rust-bors
Copy link

rust-bors bot commented Jul 3, 2025

⌛ Trying commit dee1b19 with merge a2d8ab0

To cancel the try build, run the command @bors2 try cancel.

rust-bors bot added a commit that referenced this pull request Jul 3, 2025
Fix host code appearing in Wasm binaries

<!-- homu-ignore:start -->
<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.

This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using

    r? <reviewer name>
-->
<!-- homu-ignore:end -->

This is a direct fix for issue [132802](#132802).
Followed the outline as follows:
> * give a hard error in bootstrap when using gcc to compile for wasm
> * change our CI to use clang instead of gcc
> * add a test that compiling a sample program for wasm32-unknown doesn't give any linker warnings

The `test-various` ci job was also changed.

try-job: test-various
try-job: dist-various-1
try-job: dist-various-2
try-job: x86_64-msvc-1
@rust-bors
Copy link

rust-bors bot commented Jul 4, 2025

☀️ Try build successful (CI)
Build commit: a2d8ab0 (a2d8ab005c442519d658a5e5c0a932f20c44071b, parent: 48aee7e383503c234cce4206dee9f19f57edb617)

Comment on lines -62 to +64
ENV WASM_TARGETS=wasm32-wasip1
ENV WASM_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $WASM_TARGETS \
ENV WASM_WASIP_TARGET=wasm32-wasip1
ENV WASM_WASIP_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $WASM_WASIP_TARGET \
Copy link
Contributor

@tgross35 tgross35 Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly is going on with the changes in this file btw? It looks like a variable rename, which should be inconsequential, plus installing gcc-multilib?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, why is this wasi build succeeding? We don't set CC_wasm32_wasip1 to Clang anywhere, which would mean GCC is the default.

I think it would be good to set the CC_* variables even if it works for some reason, to make it explicit that we want clang.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly is going on with the changes in this file btw? It looks like a variable rename, which should be inconsequential, plus installing gcc-multilib?

I'm not very sure about the variable changes... maybe I renamed it for clarity?
If I remember correctly installing gcc-multilib was in response to the script failing locally.

Actually, why is this wasi build succeeding? We don't set CC_wasm32_wasip1 to Clang anywhere, which would mean GCC is the default.

I think it would be good to set the CC_* variables even if it works for some reason, to make it explicit that we want clang.

wasm32-wasip1 depends on the wasi sdk, which is based on clang. You can see it installed on line 44-46 of the test-various Dockerfile.

@JayAndJef
Copy link
Contributor Author

how do we feel on this?

@tgross35
Copy link
Contributor

tgross35 commented Jul 7, 2025

Could you rename the vars in src/ci/docker/host-x86_64/test-various/Dockerfile to WASI_TARGET and WASI_SCRIPT? Not WASIP`. There is also a trailing space on line 63 of that file.

After that please squash and it LGTM, unless @Kobzol do you have anything else?

@Kobzol
Copy link
Member

Kobzol commented Jul 8, 2025

I haven't followed all the discussion here - do we have to use emscripten and cannot use clang?

@JayAndJef
Copy link
Contributor Author

I haven't followed all the discussion here - do we have to use emscripten and cannot use clang?

I'm pretty sure so, but to be safe maybe someone who works on that target could answer?

@Dylan-DPC Dylan-DPC removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 20, 2025
@Kobzol
Copy link
Member

Kobzol commented Aug 22, 2025

@bors try

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Aug 22, 2025
Fix host code appearing in Wasm binaries

try-job: test-various
try-job: dist-various-1
try-job: dist-various-2
try-job: x86_64-msvc-1
@rust-bors
Copy link

rust-bors bot commented Aug 22, 2025

☀️ Try build successful (CI)
Build commit: a3ad74e (a3ad74e9cd798d375d08e75010c3a7c07b99b8ef, parent: d20509c2a0c71b60aa2b51566e4d14920e8a1661)

@Kobzol
Copy link
Member

Kobzol commented Aug 22, 2025

Let's try it.

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 22, 2025

📌 Commit dee1b19 has been approved by Kobzol

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 Aug 22, 2025
|| build.config.rust_std_features.contains("compiler-builtins-c"))
{
let cc_tool = build.cc_tool(*target);
if !cc_tool.is_like_clang() && !cc_tool.path().ends_with("emcc") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to clarify, will this work on Windows if emcc.bat is used?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea 🤷 Are you able to check?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only tomorrow...

bors added a commit that referenced this pull request Aug 23, 2025
Rollup of 28 pull requests

Successful merges:

 - #132087 (Fix overly restrictive lifetime in `core::panic::Location::file` return type)
 - #137396 (Recover `param: Ty = EXPR`)
 - #137457 (Fix host code appearing in Wasm binaries)
 - #142185 (Convert moves of references to copies in ReferencePropagation)
 - #144648 (Implementation: `#[feature(nonpoison_rwlock)]`)
 - #144897 (print raw lifetime idents with r#)
 - #145218 ([Debuginfo] improve enum value formatting in LLDB for better readability)
 - #145380 (Add codegen-llvm regression tests)
 - #145573 (Add an experimental unsafe(force_target_feature) attribute.)
 - #145597 (resolve: Remove `ScopeSet::Late`)
 - #145633 (Fix some typos in LocalKey documentation)
 - #145641 (On E0277, point at type that doesn't implement bound)
 - #145669 (rustdoc-search: GUI tests check for `//` in URL)
 - #145695 (Introduce ProjectionElem::try_map.)
 - #145710 (Fix the ABI parameter inconsistency issue in debug.rs for LoongArch64)
 - #145726 (Experiment: Reborrow trait)
 - #145731 (Make raw pointers work in type-based search)
 - #145736 (triagebot: Update style team reviewers)
 - #145738 (Uplift rustc_mir_transform::coverage::counters::union_find to rustc_data_structures.)
 - #145742 (rustdoc js: Even more typechecking improvments )
 - #145743 (doc: fix some typos in comment)
 - #145745 (tests: Ignore basic-stepping.rs on LoongArch)
 - #145747 (Refactor lint buffering to avoid requiring a giant enum)
 - #145751 (fix(lexer): Allow '-' in the frontmatter infostring continue set)
 - #145761 (Add aarch64_be-unknown-hermit target)
 - #145762 (convert strings to symbols in attr diagnostics)
 - #145763 (Ship LLVM tools for the correct target when cross-compiling)
 - #145765 (Revert suggestions for missing methods in tuples)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 467c89c into rust-lang:master Aug 23, 2025
12 checks passed
@rustbot rustbot added this to the 1.91.0 milestone Aug 23, 2025
rust-timer added a commit that referenced this pull request Aug 23, 2025
Rollup merge of #137457 - JayAndJef:issue-132802-fix, r=Kobzol

Fix host code appearing in Wasm binaries

This is a direct fix for issue [132802](#132802).
Followed the outline as follows:
> * give a hard error in bootstrap when using gcc to compile for wasm
> * change our CI to use clang instead of gcc
> * add a test that compiling a sample program for wasm32-unknown doesn't give any linker warnings

The `test-various` ci job was also changed.

try-job: test-various
try-job: dist-various-1
try-job: dist-various-2
try-job: x86_64-msvc-1
@tgross35
Copy link
Contributor

Could you rename the vars in src/ci/docker/host-x86_64/test-various/Dockerfile to WASI_TARGET and WASI_SCRIPT? Not WASIP`. There is also a trailing space on line 63 of that file.

After that please squash and it LGTM, unless @Kobzol do you have anything else?

This all seems to have been missed (not the biggest deals)

@Kobzol
Copy link
Member

Kobzol commented Aug 23, 2025

Oops, sorry, I returned to this after a while and I thought it was done already. Do you want to do it as a follow-up @JayAndJef?

@JayAndJef
Copy link
Contributor Author

Sure, would that entail a new issue and pr?

@Kobzol
Copy link
Member

Kobzol commented Aug 23, 2025

PR is enough, no need for an issue.

github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Aug 24, 2025
Rollup of 28 pull requests

Successful merges:

 - rust-lang/rust#132087 (Fix overly restrictive lifetime in `core::panic::Location::file` return type)
 - rust-lang/rust#137396 (Recover `param: Ty = EXPR`)
 - rust-lang/rust#137457 (Fix host code appearing in Wasm binaries)
 - rust-lang/rust#142185 (Convert moves of references to copies in ReferencePropagation)
 - rust-lang/rust#144648 (Implementation: `#[feature(nonpoison_rwlock)]`)
 - rust-lang/rust#144897 (print raw lifetime idents with r#)
 - rust-lang/rust#145218 ([Debuginfo] improve enum value formatting in LLDB for better readability)
 - rust-lang/rust#145380 (Add codegen-llvm regression tests)
 - rust-lang/rust#145573 (Add an experimental unsafe(force_target_feature) attribute.)
 - rust-lang/rust#145597 (resolve: Remove `ScopeSet::Late`)
 - rust-lang/rust#145633 (Fix some typos in LocalKey documentation)
 - rust-lang/rust#145641 (On E0277, point at type that doesn't implement bound)
 - rust-lang/rust#145669 (rustdoc-search: GUI tests check for `//` in URL)
 - rust-lang/rust#145695 (Introduce ProjectionElem::try_map.)
 - rust-lang/rust#145710 (Fix the ABI parameter inconsistency issue in debug.rs for LoongArch64)
 - rust-lang/rust#145726 (Experiment: Reborrow trait)
 - rust-lang/rust#145731 (Make raw pointers work in type-based search)
 - rust-lang/rust#145736 (triagebot: Update style team reviewers)
 - rust-lang/rust#145738 (Uplift rustc_mir_transform::coverage::counters::union_find to rustc_data_structures.)
 - rust-lang/rust#145742 (rustdoc js: Even more typechecking improvments )
 - rust-lang/rust#145743 (doc: fix some typos in comment)
 - rust-lang/rust#145745 (tests: Ignore basic-stepping.rs on LoongArch)
 - rust-lang/rust#145747 (Refactor lint buffering to avoid requiring a giant enum)
 - rust-lang/rust#145751 (fix(lexer): Allow '-' in the frontmatter infostring continue set)
 - rust-lang/rust#145761 (Add aarch64_be-unknown-hermit target)
 - rust-lang/rust#145762 (convert strings to symbols in attr diagnostics)
 - rust-lang/rust#145763 (Ship LLVM tools for the correct target when cross-compiling)
 - rust-lang/rust#145765 (Revert suggestions for missing methods in tuples)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-run-make Area: port run-make Makefiles to rmake.rs A-testsuite Area: The testsuite used to check the correctness of rustc O-wasm Target: WASM (WebAssembly), http://webassembly.org/ S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-infra Relevant to the infrastructure team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.