about summary refs log tree commit diff
path: root/tests/run-make
diff options
context:
space:
mode:
authorbjorn3 <17426603+bjorn3@users.noreply.github.com>2023-07-14 14:50:21 +0000
committerbjorn3 <17426603+bjorn3@users.noreply.github.com>2023-07-19 14:47:05 +0000
commit008be2d7b6cc974e66e9e145854965cb508f6b3c (patch)
treeed370d0de8c39a1a45eb3036c0318c7dd240f8fd /tests/run-make
parent77e24f90f599070af2d8051ef9adad7fe528dd78 (diff)
downloadrust-008be2d7b6cc974e66e9e145854965cb508f6b3c.tar.gz
rust-008be2d7b6cc974e66e9e145854965cb508f6b3c.zip
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.
Diffstat (limited to 'tests/run-make')
-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
8 files changed, 47 insertions, 17 deletions
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();
+}