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

Specify dlltool prefix when generating import libs #107752

Merged
merged 1 commit into from
Feb 9, 2023

Conversation

riverar
Copy link
Contributor

@riverar riverar commented Feb 7, 2023

Ref: #106610 (comment)

tl;dr: This PR adds an explicit dlltool temporary filename prefix. The prefix resolves a race condition by ensuring dlltool temporary files are siloed in an appropriate/unique Rust temporary directory.


GNU dlltool, as part of its import library generation logic, uses a bunch of temporary files on disk. In the interest of deterministic build runs, dlltool supports deterministic temporary filenames. The temporary filename prefix is automatically generated internally or can be explicitly specified via a --temp-prefix argument.

GNU dlltool 2.38 (that ships with x86_64-12.2.0-release-posix-seh-rt_v10-rev0 installed during CI) generates a prefix based on the target library name (source). The tool writes to files such as target_dll_h.s and target_dll_s00203.o in the current working directory.

This presents a problem when multiple instances of rustc_codegen_llvm are running to generate an import library (as part of the raw_dylib feature) for the same target library (e.g. kernel32) (source). That is, dlltool instances race and may overwrite or delete files belonging to each other.

GNU dlltool 2.39+ (not used in Rust CI) generates a prefix based on the output library path (source). The tool, when invoked as part of rustc_codegen_llvm, writes to files at paths such as C_Users_Foo_AppData_Local_Temp_rustcOFqhXZ_target_lib_h.s. (The output library path is normalized and non-alphanumeric characters are replaced with underscores.)

@rustbot
Copy link
Collaborator

rustbot commented Feb 7, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @petrochenkov (or someone else) soon.

Please see the contribution instructions for more information.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 7, 2023
@ChrisDenton
Copy link
Member

cc @mati865

@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Feb 7, 2023

📌 Commit c825e08 has been approved by petrochenkov

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 Feb 7, 2023
compiler-errors added a commit to compiler-errors/rust that referenced this pull request Feb 9, 2023
…efix, r=petrochenkov

Specify dlltool prefix when generating import libs

Ref: rust-lang#106610 (comment)

tl;dr: This PR adds an explicit dlltool temporary filename prefix. The prefix resolves a race condition by ensuring dlltool temporary files are siloed in an appropriate/unique Rust temporary directory.

---

GNU dlltool, as part of its import library generation logic, uses a bunch of temporary files on disk. In the interest of deterministic build runs, dlltool supports deterministic temporary filenames. The temporary filename prefix is automatically generated internally or can be explicitly specified via a `--temp-prefix` argument.

GNU dlltool **2.38** (that ships with `x86_64-12.2.0-release-posix-seh-rt_v10-rev0` [installed during CI](https://github.com/rust-lang/rust/blob/master/src/ci/scripts/install-mingw.sh)) generates a prefix based on the target library name ([source](https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=binutils/dlltool.c;h=d95bf3f5470b999fa3b30bc887791859f48d81d1;hb=20756b0fbe065a84710aa38f2457563b57546440#l3992)). The tool writes to files such as `target_dll_h.s` and `target_dll_s00203.o` in the current working directory.

This presents a problem when multiple instances of rustc_codegen_llvm are running to generate an import library (as part of the raw_dylib feature) for the same target library (e.g. kernel32) ([source](https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/back/archive.rs#L185-L196)). That is, dlltool instances race and may overwrite or delete files belonging to each other.

GNU dlltool **2.39**+ (not used in Rust CI) generates a prefix based on the output library path ([source](https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=binutils/dlltool.c;h=e2af20847009945b4c61a6fef08268fbb4429715;hb=b51c2fec1da205ea3e7354cbb3e253018d64873c#l3992)). The tool, when invoked as part of rustc_codegen_llvm, writes to files at paths such as `C_Users_Foo_AppData_Local_Temp_rustcOFqhXZ_target_lib_h.s`. (The output library path is normalized and non-alphanumeric characters are replaced with underscores.)
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 9, 2023
…iaskrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#107446 (Migrate some of `rustc_parse` to derive diagnostics)
 - rust-lang#107752 (Specify dlltool prefix when generating import libs)
 - rust-lang#107808 (bootstrap.py: fix build-failure message)
 - rust-lang#107834 (create symlink for legacy rustfmt path)
 - rust-lang#107835 (use idiomatic formatting)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit a621769 into rust-lang:master Feb 9, 2023
@rustbot rustbot added this to the 1.69.0 milestone Feb 9, 2023
@riverar riverar deleted the rafael/gnu_dlltool_temp_prefix branch February 9, 2023 16:03
@jfgoog
Copy link
Contributor

jfgoog commented Jun 13, 2023

The --temp-prefix flag is not supported by llvm-dlltool, so this change breaks building windows rustc using a clang/llvm toolchain.

@jfgoog
Copy link
Contributor

jfgoog commented Jun 13, 2023

It looks like llvm-dlltool is not susceptible to this race condition, although it does use random temp files. A fix to recognize and ignore this flag has been upstreamed as https://reviews.llvm.org/D152361.

@mati865
Copy link
Contributor

mati865 commented Jun 13, 2023

FYI if you build tier 3 windows-gnullvm target then no dlltool will be used and LLVM API will be used instead.

@jfgoog
Copy link
Contributor

jfgoog commented Jun 14, 2023

That's good to know. I think it's better for us to stick to pc-windows-gnu for now as it's better supported. We have some workarounds, including cherrypicking the llvm patch into our toolchain build.

@jfgoog
Copy link
Contributor

jfgoog commented Jun 14, 2023

I think I'm going to play around with gnullvm a bit. It's an interesting option. Thanks for the suggestion.

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-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants