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.
This commit is contained in:
bjorn3 2023-07-14 14:50:21 +00:00
parent 77e24f90f5
commit 008be2d7b6
9 changed files with 52 additions and 30 deletions

View File

@ -511,7 +511,7 @@ fn extract_lib(
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 {
@ -539,7 +539,7 @@ fn extract_one(
&mut self,
m: FxHashMap<PathBuf, PathKind>,
flavor: CrateFlavor,
slot: &mut Option<(Svh, MetadataBlob)>,
slot: &mut Option<(PathBuf, Svh, MetadataBlob)>,
) -> 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 +550,9 @@ fn extract_one(
//
// 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()));
}
}
@ -602,7 +595,7 @@ fn extract_one(
}
};
// If we see multiple hashes, emit an error about duplicate candidates.
if slot.as_ref().is_some_and(|s| s.0 != hash) {
if slot.as_ref().is_some_and(|s| s.1 != hash) {
if let Some(candidates) = err_data {
return Err(CrateError::MultipleCandidates(
self.crate_name,
@ -610,8 +603,7 @@ fn extract_one(
candidates,
));
}
err_data = Some(vec![ret.as_ref().unwrap().0.clone()]);
*slot = None;
err_data = Some(vec![slot.take().unwrap().0]);
}
if let Some(candidates) = &mut err_data {
candidates.push(lib);
@ -644,7 +636,7 @@ fn extract_one(
continue;
}
}
*slot = Some((hash, metadata));
*slot = Some((lib.clone(), hash, metadata));
ret = Some((lib, kind));
}

View File

@ -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)

View File

@ -1,3 +0,0 @@
pub fn f() {
println!("dynamic");
}

View File

@ -1,3 +0,0 @@
pub fn f() {
println!("static");
}

View File

@ -0,0 +1 @@
pub fn f() {}

View File

@ -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

View File

@ -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)

View File

@ -0,0 +1 @@
pub fn bar() {}

View File

@ -0,0 +1,5 @@
extern crate bar;
fn main() {
bar::bar();
}