Skip to content

Fix race condition in clang_macro_fallback#3369

Open
hkalbasi wants to merge 1 commit into
rust-lang:mainfrom
hkalbasi:fix-race-condition
Open

Fix race condition in clang_macro_fallback#3369
hkalbasi wants to merge 1 commit into
rust-lang:mainfrom
hkalbasi:fix-race-condition

Conversation

@hkalbasi

@hkalbasi hkalbasi commented May 5, 2026

Copy link
Copy Markdown
Member

Resurrection of #3111
Github does not allow reopening force pushed PRs.

Comment thread bindgen/clang.rs Outdated
use crate::ir::context::BindgenContext;
use clang_sys::*;
use std::cmp;
use std::os::unix::ffi::OsStrExt;

@emilio emilio May 5, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This probably needs to be #[cfg]d to build on windows right?

View changes since the review

Comment thread bindgen/clang.rs
@hkalbasi hkalbasi force-pushed the fix-race-condition branch from d1bc925 to 8cd1b34 Compare May 5, 2026 19:56
Comment thread bindgen/clang.rs
}

fn path_to_cstring(path: &Path) -> CString {
CString::new(path.as_os_str().as_bytes()).unwrap()

@emilio emilio May 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm actually, I think you do need to_string_lossy().as_bytes() or so, as_os_str().as_bytes() would have a bunch of nulls on windows right?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is now to_string_lossy.

@hkalbasi hkalbasi force-pushed the fix-race-condition branch from 8cd1b34 to 8f6fb47 Compare May 24, 2026 15:11
@hkalbasi

Copy link
Copy Markdown
Member Author

@emilio anything else to change on this PR?

Comment thread bindgen/clang.rs Outdated
pub(crate) struct FallbackTranslationUnit {
file_path: String,
pch_path: String,
temp_dir: TempDir,

@armoha armoha Jul 6, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this is minor but the fields of a struct are dropped in declaration order. So I think temp_dir should be last field so it's dropped after disposing translation unit by clang.

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I guess TranslationUnit does not depend on the directory, but I made the change anyway. Better safe than sorry.

@hkalbasi hkalbasi force-pushed the fix-race-condition branch from 8f6fb47 to fc93834 Compare July 6, 2026 20:02

@emilio emilio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ojeda can you confirm this is fine on the kernel side? Or do we need to keep the clang_macro_fallback_build_dir option?

If so this patch needs to become a bit more complicated I suppose.

Looks good to me other than that.

View changes since this review

Comment thread bindgen/clang.rs
}

fn path_to_cstring(path: &Path) -> CString {
CString::new(path.to_string_lossy().as_bytes()).unwrap()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I guess this still doesn't deal correctly with non-utf-8 paths. On unix we could just plumb the bytes, on Windows... well, sucks to suck I guess.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is it now good? Is there any non-unix and non-windows os supported by bindgen? That would get the to_string_lossy version.

@hkalbasi hkalbasi force-pushed the fix-race-condition branch from fc93834 to 8126d07 Compare July 7, 2026 23:53
@ojeda

ojeda commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

@ojeda can you confirm this is fine on the kernel side? Or do we need to keep the clang_macro_fallback_build_dir option?

If so this patch needs to become a bit more complicated I suppose.

Thanks for the ping Emilio -- we didn't get to use --clang-macro-fallback yet, so it is fine in that sense, but perhaps others are already using it? I guess a softer option for those projects could be to make the flag a no-op, deprecating it.

Does this patch create files alongside the source file or just in that TempDir? If it is in a temporary directory outside the source tree, then I guess it may be fine. Otherwise, if it creates files within the source tree, then it will be a problem for projects that support the source tree being in a read-only location.

Having said that, it is usually a good idea to be able to control where temporary files go, and other compilers (including rustc) provide that. Would it complicate the change a lot?

I hope that helps.

@hkalbasi

hkalbasi commented Jul 8, 2026

Copy link
Copy Markdown
Member Author

Does this patch create files alongside the source file or just in that TempDir? If it is in a temporary directory outside the source tree, then I guess it may be fine. Otherwise, if it creates files within the source tree, then it will be a problem for projects that support the source tree being in a read-only location.

The version before this patch did this, but this patch ideally fix that too.

Having said that, it is usually a good idea to be able to control where temporary files go, and other compilers (including rustc) provide that. Would it complicate the change a lot?

You can control it using $TMPDIR, is a custom mechanism really needed?

@ojeda

ojeda commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

You can control it using $TMPDIR, is a custom mechanism really needed?

I think that may be fine for most projects -- could we document it then?

(At the end of the day the real cost is usually supporting whatever way is decided, not so much the particular mechanism)

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

Successfully merging this pull request may close these issues.

4 participants