about summary refs log tree commit diff
path: root/src/test/codegen
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2022-11-08 21:03:52 -0500
committerGitHub <noreply@github.com>2022-11-08 21:03:52 -0500
commit7521a974d34d24203b538a2d6d0b220c7c011fa6 (patch)
treed5f11bfe098ad93216424fced8939c3f7537f6d9 /src/test/codegen
parentbd4e608f7bdee9d283498e58617202aef30547b8 (diff)
parent296489c89268e56abb8f6050842d006b16ed4f09 (diff)
downloadrust-7521a974d34d24203b538a2d6d0b220c7c011fa6.tar.gz
rust-7521a974d34d24203b538a2d6d0b220c7c011fa6.zip
Rollup merge of #103353 - wesleywiser:fix_lld_thinlto_msvc, r=michaelwoerister
Fix Access Violation when using lld & ThinLTO on windows-msvc

Users report an AV at runtime of the compiled binary when using lld and ThinLTO on windows-msvc. The AV occurs when accessing a static value which is defined in one crate but used in another. Based on the disassembly of the cross-crate use, it appears that the use is not correctly linked with the definition and is instead assigned a garbage pointer value.

If we look at the symbol tables for each crates' obj file, we can see what is happening:

*lib.obj*:

```
COFF SYMBOL TABLE
...
00E 00000000 SECT2  notype       External     | _ZN10reproducer7memrchr2FN17h612b61ca0e168901E
...
```

*bin.obj*:

```
COFF SYMBOL TABLE
...
010 00000000 UNDEF  notype       External     | __imp__ZN10reproducer7memrchr2FN17h612b61ca0e168901E
...
```

The use of the symbol has the "import" style symbol name but the declaration doesn't generate any symbol with the same name. As a result, linking the files generates a warning from lld:

> rust-lld: warning: bin.obj: locally defined symbol imported: reproducer::memrchr::FN::h612b61ca0e168901 (defined in lib.obj) [LNK4217]

and the symbol reference remains undefined at runtime leading to the AV.

To fix this, we just need to detect that we are performing ThinLTO (and thus, static linking) and omit the `dllimport` attribute on the extern item in LLVM IR.

Fixes #81408
Diffstat (limited to 'src/test/codegen')
-rw-r--r--src/test/codegen/auxiliary/static_dllimport_aux.rs13
-rw-r--r--src/test/codegen/issue-81408-dllimport-thinlto-windows.rs15
2 files changed, 28 insertions, 0 deletions
diff --git a/src/test/codegen/auxiliary/static_dllimport_aux.rs b/src/test/codegen/auxiliary/static_dllimport_aux.rs
new file mode 100644
index 00000000000..afb0dc42f44
--- /dev/null
+++ b/src/test/codegen/auxiliary/static_dllimport_aux.rs
@@ -0,0 +1,13 @@
+use std::sync::atomic::{AtomicPtr, Ordering};
+
+#[inline(always)]
+pub fn memrchr() {
+    fn detect() {}
+
+    static CROSS_CRATE_STATIC_ITEM: AtomicPtr<()> = AtomicPtr::new(detect as *mut ());
+
+    unsafe {
+        let fun = CROSS_CRATE_STATIC_ITEM.load(Ordering::SeqCst);
+        std::mem::transmute::<*mut (), fn()>(fun)()
+    }
+}
diff --git a/src/test/codegen/issue-81408-dllimport-thinlto-windows.rs b/src/test/codegen/issue-81408-dllimport-thinlto-windows.rs
new file mode 100644
index 00000000000..0b6ab4f7ecb
--- /dev/null
+++ b/src/test/codegen/issue-81408-dllimport-thinlto-windows.rs
@@ -0,0 +1,15 @@
+// compile-flags: -O -C lto=thin -C prefer-dynamic=no
+// only-windows
+// aux-build:static_dllimport_aux.rs
+
+// Test that on Windows, when performing ThinLTO, we do not mark cross-crate static items with
+// dllimport because lld does not fix the symbol names for us.
+
+extern crate static_dllimport_aux;
+
+// CHECK-LABEL: @{{.+}}CROSS_CRATE_STATIC_ITEM{{.+}} =
+// CHECK-SAME: external local_unnamed_addr global %"{{.+}}AtomicPtr
+
+pub fn main() {
+    static_dllimport_aux::memrchr();
+}