-
Notifications
You must be signed in to change notification settings - Fork 13k
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
thread_local!
initialization code panics on some aarch64-apple-darwin
envs, works on others
#132704
Comments
Somewhat limited pre-analysis (can be safely ignored)
related caseearlier we had a similar issue with 0x102f569ec - core::ptr::replace::he9e2c42f9fe96d9b
at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ub_checks.rs:73:17
0x102f569ec - core::ptr::mut_ptr::<impl *mut T>::replace::hcc3425bab485c184
at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ptr/mut_ptr.rs:1560:18
0x102f569ec - std::sys::thread_local::lazy::LazyKeyInner<T>::take::h11414c1b64d26b2a
at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/sys/thread_local/mod.rs:102:30 which originated in some It was later confirmed that the issue stopped to manifest itself on exactly the same |
I tried going through the linked issues, but I couldn't figure out how you reproduce the issue (what commands do you run?). A hypothetical minimal reproducer based on the code you posted doesn't reproduce on my Mac (M2, macOS 14.7): use std::cell::RefCell;
#[repr(align(16))]
#[derive(Debug)]
struct MockedBlockchain {
_size: [u8; 2512],
}
thread_local! {
static BLOCKCHAIN_INTERFACE: RefCell<MockedBlockchain> = RefCell::new(MockedBlockchain {
_size: [1; 2512],
});
}
fn main() {
BLOCKCHAIN_INTERFACE.with(|b| println!("{b:?}"));
} So it sounds like it might be something else that is causing the issue? |
What do you mean by "unclarified"? Knowing that might help us to reproduce the issue? |
@madsmtm you might ask @peitalin from the linked issue about specifics of his environment, where he reproduced it. We didn't reproduce it on our macs either. The command sequence to run the same scenario which triggered panic would be # install cargo-near to $CARGO_HOME/bin
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/latest/download/cargo-near-installer.sh | sh
cargo near new test
cd test/
cargo test If the binary installer doesn't work for some reason, it can be installed from source by |
@madsmtm the code you tried is much simpler than the one from |
Linking possibly related https://rust-lang.zulipchat.com/#narrow/channel/122651-general/topic/Weird.20crash.20using.20thread.20locals.20Apple.20Silicon.20debug / zama-ai/tfhe-rs#1687. So I'll also ask:
|
@madsmtm I've just tried your example with My system specs are:
I've added a full backtrace of your code snippet and system specs here: |
Nice! I reproduced it now (both in a VM on macOS 13.5, and on macOS 14.7), it's a problem with the linker in Xcode 15.0 and Xcode 15.1, which unfortunately happens to be the latest supported on macOS 13.5. Snippet for reproducing
Copy this into # Command Line Tools for Xcode 15.0/15.1
# The latest Command Line Tools installation overwrites this path
rustc foo.rs -Clinker=/Library/Developer/CommandLineTools/usr/bin/ld && ./foo
# Xcode 15.0
rustc foo.rs -Clinker=/Applications/Xcode\ 15.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld && ./foo
# Xcode 15.1
rustc foo.rs -Clinker=/Applications/Xcode\ 15.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld && ./foo Things do seem to work in Rust 1.78 though, I'll run a bisection, and see if I can find the offending commit (to figure out if this is indeed a regression, and if there's anything we can do about it). |
Rust recently gained extra assertions in debug builds to detect Undefined Behavior caused by misaligned pointers. That is likely why this started failing. Already on older versions of Rust, this code had Undefined Behavior, and could be the cause of subtle misbehavior -- newer Rust just makes this misbehavior easier to detect. If the linker causes some pointer to be null/misaligned, that sounds like a critical bug in that linker. So it'd be good to know exactly which versions of the linker are affected. |
Cc @saethlin, your checks found another bug. :)
Is this issue documented/tracked somewhere public? Are there newer linker versions available? I don't know anything about macOS, I am just surprised that an OS released just one year ago wouldn't be able to run the latest version of the linker. EDIT: Actually https://developer.apple.com/documentation/xcode-release-notes/xcode-15_2-release-notes says that XCode 15.2 should work on macOS 13.5. |
It takes quite a lot of time to test these things, Xcode is large ;) But lemme write down exactly what I've tested so far:
And in between these there's a lot of betas and release candidates.
Yeah, I should've clarified that I only really tested the Command Line Tools (and no Command Line Tools for Xcode 15.2 exist). |
Not that I know of? Maybe rdar://110340167, though I don't have access to that myself, just guessing based on a similar issue found by googling?
With a newer Xcode, yes. Without, no (not practically, at least).
Xcode generally only wants to be run on the latest macOS release. Usually it works fine in older versions, but you have to edit a configuration file and run an "unsupported" build, so most people don't. Also linking possibly related #90959. |
You're probably right that this is / has been UB for a long time. A bisection led me to a6cdd81, which sounds very much like a red herring (the only thing of relevance that PR changed is a bit of panic code, which might explain why alignment would change between that and the previous commit). EDIT: Yeah, definitely a red herring, I've later found previous commits that also exhibit the problem. So not really something that can be bisected. I'll try to take a look at the difference in assembly between a version that succeeds and one that fails, but it's difficult, since it seems like it requires the standard library to be linked in a certain way (I couldn't trigger the issue with |
Yeah this looks exactly like what I previously poked at on Zulip, it's somewhat comforting that this seems to depend on the Apple Xcode bag of tools.
FWIW, the MIR inliner used to use item hash comparisons to prevent query cycles, which meant that literally any edit to a crate would cause the inliner to make different decisions. The only thing you can do in this scenario is turn off the MIR inliner with So you'd need to make |
That explains a lot, thanks for the tips!
Yup, couldn't reproduce this issue at all with |
I tried this code:
https://github.com/near/near-sdk-rs/blob/master/near-sdk/src/environment/mock/mod.rs#L11-L16
I expected to see this happen:
thread_local!
initialization code forRefCell::new(MockedBlockchain::new)
works
Instead, this happened:
on unclarified
aarch64-apple-darwin
environment(s)thread_local!
panics on https://doc.rust-lang.org/src/core/ptr/mod.rs.html#1277 .
The issue doesn't reproduce on other
aarch64-apple-darwin
real macs,and on
macos-14-arm64
github actions vm.Original issue: near/near-sdk-rs#1252
Meta
rustc --version --verbose
:backtrace from original issue:
near/near-sdk-rs#1252 (comment)
The text was updated successfully, but these errors were encountered: