-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Do not allow LLVM to increase a TLS's alignment on macOS. #51828
Conversation
@bors try |
[DO NOT MERGE] Disable SIMD in mem::swap on macOS Tries to address the various TLS segfault on macOS 10.10.
☀️ Test successful - status-travis |
f2422bd
to
8c6328c
Compare
@bors try |
⌛ Trying commit 8c6328c278eb5ccb63a5606856d3cf547866be2f with merge 21c06fce0627d729df71d090dc65c60826f04b65... |
💔 Test failed - status-travis |
This comment has been minimized.
This comment has been minimized.
8c6328c
to
a075ac2
Compare
@bors try |
⌛ Trying commit a075ac2631d37e74d62eea73c7480d836c5c765b with merge 7eed9fcb944a0205e2c38dd96f19918edf90747c... |
☀️ Test successful - status-travis |
Would it be possible to localize this fix to TLS? Something like If possible it'd be great here to only fix the TLS side of things and perhaps have extra code on OSX that allocates more space (maybe like via a union?) and manually aligns things instead of relying on dyld to align things |
alternatively, if there's an llvm option to disable the "bump alignment of globals if it would be beneficial" transformation, that would be simpler and more reliable. I haven't checked, if there's no option for it already then writing that patch and getting it accepted and backporting it probably isn't worthwhile. |
I'd love to have a TLS-only solution, but I don't know how 😄. But I've verified that this does solve the various crashes, so consider this a backup plan. For self reference: minimal repro for the forced 32-byte alignment problem: #![crate_type = "lib"]
#![feature(stdsimd)]
use std::{ptr, simd};
static mut STATIC_VAR: [u8; 32] = [0; 32];
pub unsafe fn f(x: &mut [u8; 32]) {
let t = simd::u8x32::load_unaligned_unchecked(x);
ptr::copy_nonoverlapping(STATIC_VAR.as_ptr(), x.as_mut_ptr(), x.len());
t.store_unaligned_unchecked(&mut STATIC_VAR);
} Changing |
@eddyb @alexcrichton @rkruppe Found the place where the alignment increase can be inhibited (well it's pretty obvious afterthought) The function is already specialized for ELF, so it should be trivial to specialize it for Mach-O TLS. diff --git a/lib/IR/Globals.cpp b/lib/IR/Globals.cpp
index da1b6c5e0c9..a742c4af4fd 100644
--- a/lib/IR/Globals.cpp
+++ b/lib/IR/Globals.cpp
@@ -250,6 +250,19 @@ bool GlobalValue::canIncreaseAlignment() const {
if (isELF && hasDefaultVisibility() && !hasLocalLinkage())
return false;
+ // The dynamic linker on macOS 10.10 has a bug (radar 24221680):
+ // it does not respect the alignment given on the TLS section.
+ // It is thus unsafe to increase the alignment since the runtime
+ // would violate the assumption and may cause segmentation fault.
+ //
+ // See also:
+ // - <https://github.com/ldc-developers/ldc/issues/1252>
+ // - <https://github.com/rust-lang/rust/issues/44056>
+ bool isMachO =
+ (!Parent || Triple(Parent->getTargetTriple()).isOSBinFormatMachO());
+ if (isMachO && isThreadLocal())
+ return false;
+
return true;
} How should we proceed? (An alternate solution without modifying LLVM would be (a) always set a |
a075ac2
to
fdce8db
Compare
@kennytm nice find! Perhaps we could try shoving everything into something like a |
@alexcrichton The section name must be The linker error
Explicitly setting the section name to #[thread_local]
static mut ZERO_ARRAY: [u32; 250_000] = [0; 250_000];
// adds 1 MB to the compiled output unless placed into __DATA,__thread_bss These TLS should be placed in |
That can be detected easily (the miri allocations are accessible and you can access individual bytes), and we should probably be doing this in general |
☀️ Test successful - status-appveyor, status-travis |
We might want to backport this into beta. |
I've nominated for @rust-lang/compiler to discuss this for beta and stable backport acceptance (note that we now have a companion tag, stable-nominated). |
This is not a clean stable backport and the changes are not trivial AFAICT. |
We decided that we would backport to beta but not stable -- the stable backport is rather delicate. |
FWIW, this introduced an extremely subtle bug: even if all the "bytes" are 0, there might still be pointers in this allocation and then the 0 just means the offset is 0, but a relocation needs to be emitted. See #62655 (comment). |
I was wondering if this change means that |
Just want to mention that (this PR)? apparently causes issues with mlua on macOS using LuaJIT backend. What is weird, everything was working fine on Rust < 1.72. Probably at this stage I'll just exclude test that causes problems on macos. |
This addresses the various TLS segfault on macOS 10.10.
Fix #51794.
Fix #51758.
Fix #50867.
Fix #48866.
Fix #46355.
Fix #44056.