about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-07-20 09:00:10 +0000
committerbors <bors@rust-lang.org>2023-07-20 09:00:10 +0000
commitb14fd2359f47fb9a14bbfe55359db4bb3af11861 (patch)
treed6bd24632382dc8cbea7e8dc0bd79dd3874cdf5b
parentc67cb3e577bdd4de640eb11d96cd5ef5afe0eb0b (diff)
parent8c9a8b63c9dbdd92991c3c3213fb52a03d8df757 (diff)
downloadrust-b14fd2359f47fb9a14bbfe55359db4bb3af11861.tar.gz
rust-b14fd2359f47fb9a14bbfe55359db4bb3af11861.zip
Auto merge of #113695 - bjorn3:fix_rlib_cdylib_metadata_handling, r=pnkfelix,petrochenkov
Verify that all crate sources are in sync

This ensures that rustc will not attempt to link against a cdylib as if it is a rust dylib when an rlib for the same crate is available. Previously rustc didn't actually check if any further formats of a crate which has been loaded are of the same version and if they are actually valid. This caused a cdylib to be interpreted as rust dylib as soon as the corresponding rlib was loaded. As cdylibs don't export any rust symbols, linking would fail if rustc decides to link against the cdylib rather than the rlib.

Two crates depended on the previous behavior by separately compiling a test crate as both rlib and dylib. These have been changed to capture their original spirit to the best of my ability while still working when rustc verifies that all crates are in sync. It is unlikely that build systems depend on the current behavior and in any case we are taking a lot of measures to ensure that any change to either the source or the compilation options (including crate type) results in rustc rejecting it as incompatible. We merely didn't do this check here for now obsolete perf reasons.

Fixes https://github.com/rust-lang/rust/issues/10786
Fixes https://github.com/rust-lang/rust/issues/82151
Fixes https://github.com/rust-lang/rust/issues/82972
Closes https://github.com/bevy-cheatbook/bevy-cheatbook/issues/114
-rw-r--r--Cargo.lock1
-rw-r--r--compiler/rustc_codegen_ssa/Cargo.toml1
-rw-r--r--compiler/rustc_codegen_ssa/src/back/metadata.rs20
-rw-r--r--compiler/rustc_metadata/src/locator.rs49
-rw-r--r--tests/run-make/extern-flag-pathless/Makefile27
-rw-r--r--tests/run-make/extern-flag-pathless/bar-dynamic.rs3
-rw-r--r--tests/run-make/extern-flag-pathless/bar-static.rs3
-rw-r--r--tests/run-make/extern-flag-pathless/bar.rs1
-rw-r--r--tests/run-make/mixing-libs/Makefile8
-rw-r--r--tests/run-make/no-cdylib-as-rdylib/Makefile16
-rw-r--r--tests/run-make/no-cdylib-as-rdylib/bar.rs1
-rw-r--r--tests/run-make/no-cdylib-as-rdylib/foo.rs5
-rw-r--r--tests/run-make/rmeta-preferred/Makefile16
-rw-r--r--tests/run-make/rmeta-preferred/lib.rs (renamed from tests/ui/rmeta/rmeta-rpass.rs)8
-rw-r--r--tests/run-make/rmeta-preferred/rmeta_aux.rs3
-rw-r--r--tests/ui/rmeta/auxiliary/rmeta-rlib-rpass.rs8
-rw-r--r--tests/ui/rmeta/auxiliary/rmeta-rmeta.rs9
17 files changed, 100 insertions, 79 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 01445e97ae7..8c2c3f5e628 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3367,7 +3367,6 @@ dependencies = [
  "rustc_type_ir",
  "serde_json",
  "smallvec",
- "snap",
  "tempfile",
  "thorin-dwp",
  "tracing",
diff --git a/compiler/rustc_codegen_ssa/Cargo.toml b/compiler/rustc_codegen_ssa/Cargo.toml
index 984efa21044..6582fd62387 100644
--- a/compiler/rustc_codegen_ssa/Cargo.toml
+++ b/compiler/rustc_codegen_ssa/Cargo.toml
@@ -17,7 +17,6 @@ tempfile = "3.2"
 thorin-dwp = "0.6"
 pathdiff = "0.2.0"
 serde_json = "1.0.59"
-snap = "1"
 smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
 regex = "1.4"
 
diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs
index e8b8665e39d..c4bb51edade 100644
--- a/compiler/rustc_codegen_ssa/src/back/metadata.rs
+++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs
@@ -10,8 +10,6 @@ use object::{
     ObjectSymbol, SectionFlags, SectionKind, SymbolFlags, SymbolKind, SymbolScope,
 };
 
-use snap::write::FrameEncoder;
-
 use rustc_data_structures::memmap::Mmap;
 use rustc_data_structures::owned_slice::{try_slice_owned, OwnedSlice};
 use rustc_metadata::fs::METADATA_FILENAME;
@@ -481,19 +479,15 @@ pub fn create_compressed_metadata_file(
     metadata: &EncodedMetadata,
     symbol_name: &str,
 ) -> Vec<u8> {
-    let mut compressed = rustc_metadata::METADATA_HEADER.to_vec();
-    // Our length will be backfilled once we're done writing
-    compressed.write_all(&[0; 4]).unwrap();
-    FrameEncoder::new(&mut compressed).write_all(metadata.raw_data()).unwrap();
-    let meta_len = rustc_metadata::METADATA_HEADER.len();
-    let data_len = (compressed.len() - meta_len - 4) as u32;
-    compressed[meta_len..meta_len + 4].copy_from_slice(&data_len.to_be_bytes());
+    let mut packed_metadata = rustc_metadata::METADATA_HEADER.to_vec();
+    packed_metadata.write_all(&(metadata.raw_data().len() as u32).to_be_bytes()).unwrap();
+    packed_metadata.extend(metadata.raw_data());
 
     let Some(mut file) = create_object_file(sess) else {
-        return compressed.to_vec();
+        return packed_metadata.to_vec();
     };
     if file.format() == BinaryFormat::Xcoff {
-        return create_compressed_metadata_file_for_xcoff(file, &compressed, symbol_name);
+        return create_compressed_metadata_file_for_xcoff(file, &packed_metadata, symbol_name);
     }
     let section = file.add_section(
         file.segment_name(StandardSegment::Data).to_vec(),
@@ -507,14 +501,14 @@ pub fn create_compressed_metadata_file(
         }
         _ => {}
     };
-    let offset = file.append_section_data(section, &compressed, 1);
+    let offset = file.append_section_data(section, &packed_metadata, 1);
 
     // For MachO and probably PE this is necessary to prevent the linker from throwing away the
     // .rustc section. For ELF this isn't necessary, but it also doesn't harm.
     file.add_symbol(Symbol {
         name: symbol_name.as_bytes().to_vec(),
         value: offset,
-        size: compressed.len() as u64,
+        size: packed_metadata.len() as u64,
         kind: SymbolKind::Data,
         scope: SymbolScope::Dynamic,
         weak: false,
diff --git a/compiler/rustc_metadata/src/locator.rs b/compiler/rustc_metadata/src/locator.rs
index a1511c4b570..44195996762 100644
--- a/compiler/rustc_metadata/src/locator.rs
+++ b/compiler/rustc_metadata/src/locator.rs
@@ -511,7 +511,7 @@ impl<'a> CrateLocator<'a> {
             rlib: self.extract_one(rlibs, CrateFlavor::Rlib, &mut slot)?,
             dylib: self.extract_one(dylibs, CrateFlavor::Dylib, &mut slot)?,
         };
-        Ok(slot.map(|(svh, metadata)| (svh, Library { source, metadata })))
+        Ok(slot.map(|(svh, metadata, _)| (svh, Library { source, metadata })))
     }
 
     fn needs_crate_flavor(&self, flavor: CrateFlavor) -> bool {
@@ -535,11 +535,13 @@ impl<'a> CrateLocator<'a> {
     // read the metadata from it if `*slot` is `None`. If the metadata couldn't
     // be read, it is assumed that the file isn't a valid rust library (no
     // errors are emitted).
+    //
+    // The `PathBuf` in `slot` will only be used for diagnostic purposes.
     fn extract_one(
         &mut self,
         m: FxHashMap<PathBuf, PathKind>,
         flavor: CrateFlavor,
-        slot: &mut Option<(Svh, MetadataBlob)>,
+        slot: &mut Option<(Svh, MetadataBlob, PathBuf)>,
     ) -> Result<Option<(PathBuf, PathKind)>, CrateError> {
         // If we are producing an rlib, and we've already loaded metadata, then
         // we should not attempt to discover further crate sources (unless we're
@@ -550,16 +552,9 @@ impl<'a> CrateLocator<'a> {
         //
         // See also #68149 which provides more detail on why emitting the
         // dependency on the rlib is a bad thing.
-        //
-        // We currently do not verify that these other sources are even in sync,
-        // and this is arguably a bug (see #10786), but because reading metadata
-        // is quite slow (especially from dylibs) we currently do not read it
-        // from the other crate sources.
         if slot.is_some() {
             if m.is_empty() || !self.needs_crate_flavor(flavor) {
                 return Ok(None);
-            } else if m.len() == 1 {
-                return Ok(Some(m.into_iter().next().unwrap()));
             }
         }
 
@@ -610,8 +605,7 @@ impl<'a> CrateLocator<'a> {
                         candidates,
                     ));
                 }
-                err_data = Some(vec![ret.as_ref().unwrap().0.clone()]);
-                *slot = None;
+                err_data = Some(vec![slot.take().unwrap().2]);
             }
             if let Some(candidates) = &mut err_data {
                 candidates.push(lib);
@@ -644,7 +638,7 @@ impl<'a> CrateLocator<'a> {
                     continue;
                 }
             }
-            *slot = Some((hash, metadata));
+            *slot = Some((hash, metadata, lib.clone()));
             ret = Some((lib, kind));
         }
 
@@ -814,19 +808,26 @@ fn get_metadata_section<'p>(
             let compressed_len = u32::from_be_bytes(len_bytes) as usize;
 
             // Header is okay -> inflate the actual metadata
-            let compressed_bytes = &buf[data_start..(data_start + compressed_len)];
-            debug!("inflating {} bytes of compressed metadata", compressed_bytes.len());
-            // Assume the decompressed data will be at least the size of the compressed data, so we
-            // don't have to grow the buffer as much.
-            let mut inflated = Vec::with_capacity(compressed_bytes.len());
-            FrameDecoder::new(compressed_bytes).read_to_end(&mut inflated).map_err(|_| {
-                MetadataError::LoadFailure(format!(
-                    "failed to decompress metadata: {}",
-                    filename.display()
-                ))
-            })?;
+            let compressed_bytes = buf.slice(|buf| &buf[data_start..(data_start + compressed_len)]);
+            if &compressed_bytes[..cmp::min(METADATA_HEADER.len(), compressed_bytes.len())]
+                == METADATA_HEADER
+            {
+                // The metadata was not actually compressed.
+                compressed_bytes
+            } else {
+                debug!("inflating {} bytes of compressed metadata", compressed_bytes.len());
+                // Assume the decompressed data will be at least the size of the compressed data, so we
+                // don't have to grow the buffer as much.
+                let mut inflated = Vec::with_capacity(compressed_bytes.len());
+                FrameDecoder::new(&*compressed_bytes).read_to_end(&mut inflated).map_err(|_| {
+                    MetadataError::LoadFailure(format!(
+                        "failed to decompress metadata: {}",
+                        filename.display()
+                    ))
+                })?;
 
-            slice_owned(inflated, Deref::deref)
+                slice_owned(inflated, Deref::deref)
+            }
         }
         CrateFlavor::Rmeta => {
             // mmap the file, because only a small fraction of it is read.
diff --git a/tests/run-make/extern-flag-pathless/Makefile b/tests/run-make/extern-flag-pathless/Makefile
index 701bfcd28c8..36b374e0d2e 100644
--- a/tests/run-make/extern-flag-pathless/Makefile
+++ b/tests/run-make/extern-flag-pathless/Makefile
@@ -3,17 +3,32 @@ include ../tools.mk
 
 # Test mixing pathless --extern with paths.
 
+# Test for static linking by checking that the binary runs if the dylib
+# is removed and test for dynamic linking by checking that the binary
+# fails to run if the dylib is removed.
+
 all:
-	$(RUSTC) bar-static.rs --crate-name=bar --crate-type=rlib
-	$(RUSTC) bar-dynamic.rs --crate-name=bar --crate-type=dylib -C prefer-dynamic
+	$(RUSTC) bar.rs --crate-type=rlib --crate-type=dylib -Cprefer-dynamic
+
 	# rlib preferred over dylib
 	$(RUSTC) foo.rs --extern bar
-	$(call RUN,foo) | $(CGREP) 'static'
+	mv $(call DYLIB,bar) $(TMPDIR)/bar.tmp
+	$(call RUN,foo)
+	mv $(TMPDIR)/bar.tmp $(call DYLIB,bar)
+
 	$(RUSTC) foo.rs --extern bar=$(TMPDIR)/libbar.rlib --extern bar
-	$(call RUN,foo) | $(CGREP) 'static'
+	mv $(call DYLIB,bar) $(TMPDIR)/bar.tmp
+	$(call RUN,foo)
+	mv $(TMPDIR)/bar.tmp $(call DYLIB,bar)
+
 	# explicit --extern overrides pathless
 	$(RUSTC) foo.rs --extern bar=$(call DYLIB,bar) --extern bar
-	$(call RUN,foo) | $(CGREP) 'dynamic'
+	mv $(call DYLIB,bar) $(TMPDIR)/bar.tmp
+	$(call FAIL,foo)
+	mv $(TMPDIR)/bar.tmp $(call DYLIB,bar)
+
 	# prefer-dynamic does what it says
 	$(RUSTC) foo.rs --extern bar -C prefer-dynamic
-	$(call RUN,foo) | $(CGREP) 'dynamic'
+	mv $(call DYLIB,bar) $(TMPDIR)/bar.tmp
+	$(call FAIL,foo)
+	mv $(TMPDIR)/bar.tmp $(call DYLIB,bar)
diff --git a/tests/run-make/extern-flag-pathless/bar-dynamic.rs b/tests/run-make/extern-flag-pathless/bar-dynamic.rs
deleted file mode 100644
index e2d68d517ff..00000000000
--- a/tests/run-make/extern-flag-pathless/bar-dynamic.rs
+++ /dev/null
@@ -1,3 +0,0 @@
-pub fn f() {
-    println!("dynamic");
-}
diff --git a/tests/run-make/extern-flag-pathless/bar-static.rs b/tests/run-make/extern-flag-pathless/bar-static.rs
deleted file mode 100644
index 240d8bde4d1..00000000000
--- a/tests/run-make/extern-flag-pathless/bar-static.rs
+++ /dev/null
@@ -1,3 +0,0 @@
-pub fn f() {
-    println!("static");
-}
diff --git a/tests/run-make/extern-flag-pathless/bar.rs b/tests/run-make/extern-flag-pathless/bar.rs
new file mode 100644
index 00000000000..cdc6c27d800
--- /dev/null
+++ b/tests/run-make/extern-flag-pathless/bar.rs
@@ -0,0 +1 @@
+pub fn f() {}
diff --git a/tests/run-make/mixing-libs/Makefile b/tests/run-make/mixing-libs/Makefile
index e8262b28401..459db0dfdb2 100644
--- a/tests/run-make/mixing-libs/Makefile
+++ b/tests/run-make/mixing-libs/Makefile
@@ -2,9 +2,7 @@
 include ../tools.mk
 
 all:
-	$(RUSTC) rlib.rs
-	$(RUSTC) dylib.rs
-	$(RUSTC) rlib.rs --crate-type=dylib
-	$(RUSTC) dylib.rs
-	$(call REMOVE_DYLIBS,rlib)
+	$(RUSTC) rlib.rs --crate-type=rlib --crate-type=dylib
+	$(RUSTC) dylib.rs # no -Cprefer-dynamic so statically linking librlib.rlib
+	$(call REMOVE_DYLIBS,rlib) # remove librlib.so to test that prog.rs doesn't get confused about the removed dylib version of librlib
 	$(RUSTC) prog.rs && exit 1 || exit 0
diff --git a/tests/run-make/no-cdylib-as-rdylib/Makefile b/tests/run-make/no-cdylib-as-rdylib/Makefile
new file mode 100644
index 00000000000..4d2be0aea91
--- /dev/null
+++ b/tests/run-make/no-cdylib-as-rdylib/Makefile
@@ -0,0 +1,16 @@
+# ignore-cross-compile
+include ../tools.mk
+
+# Test that rustc will not attempt to link against a cdylib as if
+# it is a rust dylib when an rlib for the same crate is available.
+# Previously rustc didn't actually check if any further formats of
+# a crate which has been loaded are of the same version and if
+# they are actually valid. This caused a cdylib to be interpreted
+# as rust dylib as soon as the corresponding rlib was loaded. As
+# cdylibs don't export any rust symbols, linking would fail if
+# rustc decides to link against the cdylib rather than the rlib.
+
+all:
+	$(RUSTC) bar.rs --crate-type=rlib --crate-type=cdylib
+	$(RUSTC) foo.rs -C prefer-dynamic
+	$(call RUN,foo)
diff --git a/tests/run-make/no-cdylib-as-rdylib/bar.rs b/tests/run-make/no-cdylib-as-rdylib/bar.rs
new file mode 100644
index 00000000000..c5c0bc606cd
--- /dev/null
+++ b/tests/run-make/no-cdylib-as-rdylib/bar.rs
@@ -0,0 +1 @@
+pub fn bar() {}
diff --git a/tests/run-make/no-cdylib-as-rdylib/foo.rs b/tests/run-make/no-cdylib-as-rdylib/foo.rs
new file mode 100644
index 00000000000..8d68535e3b6
--- /dev/null
+++ b/tests/run-make/no-cdylib-as-rdylib/foo.rs
@@ -0,0 +1,5 @@
+extern crate bar;
+
+fn main() {
+    bar::bar();
+}
diff --git a/tests/run-make/rmeta-preferred/Makefile b/tests/run-make/rmeta-preferred/Makefile
new file mode 100644
index 00000000000..3bf12cced29
--- /dev/null
+++ b/tests/run-make/rmeta-preferred/Makefile
@@ -0,0 +1,16 @@
+# ignore-cross-compile
+include ../tools.mk
+
+# Test that using rlibs and rmeta dep crates work together. Specifically, that
+# there can be both an rmeta and an rlib file and rustc will prefer the rmeta
+# file.
+#
+# This behavior is simply making sure this doesn't accidentally change; in this
+# case we want to make sure that the rlib isn't being used as that would cause
+# bugs in -Zbinary-dep-depinfo (see #68298).
+
+all:
+	$(RUSTC) rmeta_aux.rs --crate-type=rlib --emit link,metadata
+	$(RUSTC) lib.rs --crate-type=rlib --emit dep-info -Zbinary-dep-depinfo
+	$(CGREP) "librmeta_aux.rmeta" < $(TMPDIR)/lib.d
+	$(CGREP) -v "librmeta_aux.rlib" < $(TMPDIR)/lib.d
diff --git a/tests/ui/rmeta/rmeta-rpass.rs b/tests/run-make/rmeta-preferred/lib.rs
index 173a6a394eb..d0b81a0628a 100644
--- a/tests/ui/rmeta/rmeta-rpass.rs
+++ b/tests/run-make/rmeta-preferred/lib.rs
@@ -1,4 +1,3 @@
-// run-pass
 // Test that using rlibs and rmeta dep crates work together. Specifically, that
 // there can be both an rmeta and an rlib file and rustc will prefer the rmeta
 // file.
@@ -7,12 +6,9 @@
 // case we want to make sure that the rlib isn't being used as that would cause
 // bugs in -Zbinary-dep-depinfo (see #68298).
 
-// aux-build:rmeta-rmeta.rs
-// aux-build:rmeta-rlib-rpass.rs
-
 extern crate rmeta_aux;
 use rmeta_aux::Foo;
 
-pub fn main() {
-    let _ = Foo { field2: 42 };
+pub fn foo() {
+    let _ = Foo { field: 42 };
 }
diff --git a/tests/run-make/rmeta-preferred/rmeta_aux.rs b/tests/run-make/rmeta-preferred/rmeta_aux.rs
new file mode 100644
index 00000000000..3f7a12b5054
--- /dev/null
+++ b/tests/run-make/rmeta-preferred/rmeta_aux.rs
@@ -0,0 +1,3 @@
+pub struct Foo {
+    pub field: i32,
+}
diff --git a/tests/ui/rmeta/auxiliary/rmeta-rlib-rpass.rs b/tests/ui/rmeta/auxiliary/rmeta-rlib-rpass.rs
deleted file mode 100644
index f5e8c3d2a5c..00000000000
--- a/tests/ui/rmeta/auxiliary/rmeta-rlib-rpass.rs
+++ /dev/null
@@ -1,8 +0,0 @@
-// no-prefer-dynamic
-
-#![crate_type="rlib"]
-#![crate_name="rmeta_aux"]
-
-pub struct Foo {
-    pub field: i32,
-}
diff --git a/tests/ui/rmeta/auxiliary/rmeta-rmeta.rs b/tests/ui/rmeta/auxiliary/rmeta-rmeta.rs
deleted file mode 100644
index 4a6d055a81f..00000000000
--- a/tests/ui/rmeta/auxiliary/rmeta-rmeta.rs
+++ /dev/null
@@ -1,9 +0,0 @@
-// no-prefer-dynamic
-// compile-flags: --emit=metadata
-
-#![crate_type="rlib"]
-#![crate_name="rmeta_aux"]
-
-pub struct Foo {
-    pub field2: i32,
-}