From ef07fe1b2811836ee4b5b30e94c74061bc66d06c Mon Sep 17 00:00:00 2001 From: Oneirical Date: Wed, 17 Jul 2024 11:40:02 -0400 Subject: [PATCH 1/3] rewrite pointer-auth-link-with-c to rmake --- .../tidy/src/allowed_run_make_makefiles.txt | 1 - .../pointer-auth-link-with-c/Makefile | 15 ---------- .../pointer-auth-link-with-c/rmake.rs | 28 +++++++++++++++++++ 3 files changed, 28 insertions(+), 16 deletions(-) delete mode 100644 tests/run-make/pointer-auth-link-with-c/Makefile create mode 100644 tests/run-make/pointer-auth-link-with-c/rmake.rs diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index 6b3533c25785f..57e52551deee4 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -60,7 +60,6 @@ run-make/pdb-buildinfo-cl-cmd/Makefile run-make/pgo-gen-lto/Makefile run-make/pgo-gen-no-imp-symbols/Makefile run-make/pgo-indirect-call-promotion/Makefile -run-make/pointer-auth-link-with-c/Makefile run-make/print-calling-conventions/Makefile run-make/print-target-list/Makefile run-make/raw-dylib-alt-calling-convention/Makefile diff --git a/tests/run-make/pointer-auth-link-with-c/Makefile b/tests/run-make/pointer-auth-link-with-c/Makefile deleted file mode 100644 index 8fcf10e2096cd..0000000000000 --- a/tests/run-make/pointer-auth-link-with-c/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -include ../tools.mk - -# only-aarch64 -# ignore-cross-compile - -all: - $(COMPILE_OBJ) $(TMPDIR)/test.o test.c - $(AR) rcs $(TMPDIR)/libtest.a $(TMPDIR)/test.o - $(RUSTC) --target $(TARGET) -Z branch-protection=bti,pac-ret,leaf test.rs - $(call RUN,test) - - $(COMPILE_OBJ) $(TMPDIR)/test.o test.c -mbranch-protection=bti+pac-ret+leaf - $(AR) rcs $(TMPDIR)/libtest.a $(TMPDIR)/test.o - $(RUSTC) --target $(TARGET) -Z branch-protection=bti,pac-ret,leaf test.rs - $(call RUN,test) diff --git a/tests/run-make/pointer-auth-link-with-c/rmake.rs b/tests/run-make/pointer-auth-link-with-c/rmake.rs new file mode 100644 index 0000000000000..960eafa546b85 --- /dev/null +++ b/tests/run-make/pointer-auth-link-with-c/rmake.rs @@ -0,0 +1,28 @@ +// `-Z branch protection` is an unstable compiler feature which adds pointer-authentication +// code (PAC), a useful hashing measure for verifying that pointers have not been modified. +// This test checks that compilation and execution is successful when this feature is activated, +// with some of its possible extra arguments (bti, pac-ret, leaf). +// See https://github.com/rust-lang/rust/pull/88354 + +//@ only-aarch64 +// Reason: branch protection is not supported on other architectures +//@ ignore-cross-compile +// Reason: the compiled binary is executed + +use run_make_support::{build_native_static_lib, cc, is_msvc, llvm_ar, run, rustc}; + +fn main() { + build_native_static_lib("test"); + rustc().arg("-Zbranch-protection=bti,pac-ret,leaf").input("test.rs").run(); + run("test"); + cc().arg("-v") + .arg("-c") + .out_exe("test") + .input("test.c") + .arg("-mbranch-protection=bti+pac-ret+leaf") + .run(); + let obj_file = if is_msvc() { "test.obj" } else { "test" }; + llvm_ar().obj_to_ar().output_input("libtest.a", &obj_file).run(); + rustc().arg("-Zbranch-protection=bti,pac-ret,leaf").input("test.rs").run(); + run("test"); +} From 63286d80a1822df32c2f18fb7968a1368d8da8d6 Mon Sep 17 00:00:00 2001 From: Oneirical Date: Wed, 17 Jul 2024 12:07:28 -0400 Subject: [PATCH 2/3] rewrite c-dynamic-rlib to rmake --- .../tidy/src/allowed_run_make_makefiles.txt | 1 - tests/run-make/c-dynamic-rlib/Makefile | 19 ---------------- tests/run-make/c-dynamic-rlib/rmake.rs | 22 +++++++++++++++++++ 3 files changed, 22 insertions(+), 20 deletions(-) delete mode 100644 tests/run-make/c-dynamic-rlib/Makefile create mode 100644 tests/run-make/c-dynamic-rlib/rmake.rs diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index 57e52551deee4..b85e5dfe3edc9 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -1,6 +1,5 @@ run-make/branch-protection-check-IBT/Makefile run-make/c-dynamic-dylib/Makefile -run-make/c-dynamic-rlib/Makefile run-make/c-unwind-abi-catch-lib-panic/Makefile run-make/cat-and-grep-sanity-check/Makefile run-make/cdylib-dylib-linkage/Makefile diff --git a/tests/run-make/c-dynamic-rlib/Makefile b/tests/run-make/c-dynamic-rlib/Makefile deleted file mode 100644 index 7b05e3d91a098..0000000000000 --- a/tests/run-make/c-dynamic-rlib/Makefile +++ /dev/null @@ -1,19 +0,0 @@ -# This test checks that dynamic Rust linking with C does not encounter any errors, with static dependencies given preference over dynamic. (This is the default behaviour.) -# See https://github.com/rust-lang/rust/issues/10434 - -# ignore-cross-compile -include ../tools.mk - -# ignore-apple -# -# This hits an assertion in the linker on older versions of osx apparently - -# This overrides the LD_LIBRARY_PATH for RUN -TARGET_RPATH_DIR:=$(TARGET_RPATH_DIR):$(TMPDIR) - -all: $(call DYLIB,cfoo) - $(RUSTC) foo.rs - $(RUSTC) bar.rs - $(call RUN,bar) - $(call REMOVE_DYLIBS,cfoo) - $(call FAIL,bar) diff --git a/tests/run-make/c-dynamic-rlib/rmake.rs b/tests/run-make/c-dynamic-rlib/rmake.rs new file mode 100644 index 0000000000000..c48b1edee9e69 --- /dev/null +++ b/tests/run-make/c-dynamic-rlib/rmake.rs @@ -0,0 +1,22 @@ +// This test checks that dynamic Rust linking with C does not encounter any errors in both +// compilation and execution, with static dependencies given preference over dynamic. +// (This is the default behaviour.) +// See https://github.com/rust-lang/rust/issues/10434 + +//@ ignore-cross-compile +// Reason: the compiled binary is executed + +//FIXME(Oneirical): test on apple because older versions of osx are failing apparently + +use run_make_support::{ + build_native_dynamic_lib, dynamic_lib_name, fs_wrapper, run, run_fail, rustc, +}; + +fn main() { + build_native_dynamic_lib("cfoo"); + rustc().input("foo.rs").run(); + rustc().input("bar.rs").run(); + run("bar"); + fs_wrapper::remove_file(dynamic_lib_name("cfoo")); + run_fail("bar"); +} From 710e7b0c7581efcbb2d913738d1ec17f0a726387 Mon Sep 17 00:00:00 2001 From: Oneirical Date: Wed, 17 Jul 2024 15:11:09 -0400 Subject: [PATCH 3/3] rewrite c-dynamic-dylib to rmake --- .../src/external_deps/c_build.rs | 37 ++++++++++++++++++- src/tools/run-make-support/src/fs.rs | 15 +++++++- src/tools/run-make-support/src/lib.rs | 2 +- .../tidy/src/allowed_run_make_makefiles.txt | 1 - tests/run-make/c-dynamic-dylib/Makefile | 16 -------- tests/run-make/c-dynamic-dylib/rmake.rs | 17 +++++++++ tests/run-make/c-dynamic-rlib/rmake.rs | 8 +--- 7 files changed, 69 insertions(+), 27 deletions(-) delete mode 100644 tests/run-make/c-dynamic-dylib/Makefile create mode 100644 tests/run-make/c-dynamic-dylib/rmake.rs diff --git a/src/tools/run-make-support/src/external_deps/c_build.rs b/src/tools/run-make-support/src/external_deps/c_build.rs index 35b2bf75c95f5..86c9c08183197 100644 --- a/src/tools/run-make-support/src/external_deps/c_build.rs +++ b/src/tools/run-make-support/src/external_deps/c_build.rs @@ -1,10 +1,13 @@ use std::path::PathBuf; -use crate::artifact_names::static_lib_name; +use super::cygpath::get_windows_path; +use crate::artifact_names::{dynamic_lib_name, static_lib_name}; use crate::external_deps::cc::cc; use crate::external_deps::llvm::llvm_ar; use crate::path_helpers::path; -use crate::targets::is_msvc; +use crate::targets::{is_darwin, is_msvc, is_windows}; + +// FIXME(Oneirical): These native build functions should take a Path-based generic. /// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name. #[track_caller] @@ -25,3 +28,33 @@ pub fn build_native_static_lib(lib_name: &str) -> PathBuf { llvm_ar().obj_to_ar().output_input(&lib_path, &obj_file).run(); path(lib_path) } + +/// Builds a dynamic lib. The filename is computed in a target-dependent manner, relying on +/// [`std::env::consts::DLL_PREFIX`] and [`std::env::consts::DLL_EXTENSION`]. +#[track_caller] +pub fn build_native_dynamic_lib(lib_name: &str) -> PathBuf { + let obj_file = if is_msvc() { format!("{lib_name}") } else { format!("{lib_name}.o") }; + let src = format!("{lib_name}.c"); + let lib_path = dynamic_lib_name(lib_name); + if is_msvc() { + cc().arg("-c").out_exe(&obj_file).input(src).run(); + } else { + cc().arg("-v").arg("-c").out_exe(&obj_file).input(src).run(); + }; + let obj_file = if is_msvc() { format!("{lib_name}.obj") } else { format!("{lib_name}.o") }; + if is_msvc() { + let mut out_arg = "-out:".to_owned(); + out_arg.push_str(&get_windows_path(&lib_path)); + cc().input(&obj_file).args(&["-link", "-dll", &out_arg]).run(); + } else if is_darwin() { + cc().out_exe(&lib_path).input(&obj_file).args(&["-dynamiclib", "-Wl,-dylib"]).run(); + } else if is_windows() { + cc().out_exe(&lib_path) + .input(&obj_file) + .args(&["-shared", &format!("-Wl,--out-implib={lib_path}.a")]) + .run(); + } else { + cc().out_exe(&lib_path).input(&obj_file).arg("-shared").run(); + } + path(lib_path) +} diff --git a/src/tools/run-make-support/src/fs.rs b/src/tools/run-make-support/src/fs.rs index f346e983aea8b..0a79616163308 100644 --- a/src/tools/run-make-support/src/fs.rs +++ b/src/tools/run-make-support/src/fs.rs @@ -1,5 +1,5 @@ use std::io; -use std::path::Path; +use std::path::{Path, PathBuf}; // FIXME(jieyouxu): modify create_symlink to panic on windows. @@ -176,3 +176,16 @@ pub fn set_permissions>(path: P, perm: std::fs::Permissions) { path.as_ref().display() )); } + +/// A function which prints all file names in the directory `dir` similarly to Unix's `ls`. +/// Useful for debugging. +/// Usage: `eprintln!("{:#?}", shallow_find_dir_entries(some_dir));` +#[track_caller] +pub fn shallow_find_dir_entries>(dir: P) -> Vec { + let paths = read_dir(dir); + let mut output = Vec::new(); + for path in paths { + output.push(path.unwrap().path()); + } + output +} diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index b85191970de62..a4bb9056346d9 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -43,7 +43,7 @@ pub use wasmparser; pub use external_deps::{c_build, cc, clang, htmldocck, llvm, python, rustc, rustdoc}; // These rely on external dependencies. -pub use c_build::build_native_static_lib; +pub use c_build::{build_native_dynamic_lib, build_native_static_lib}; pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc}; pub use clang::{clang, Clang}; pub use htmldocck::htmldocck; diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index b85e5dfe3edc9..88010f9390616 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -1,5 +1,4 @@ run-make/branch-protection-check-IBT/Makefile -run-make/c-dynamic-dylib/Makefile run-make/c-unwind-abi-catch-lib-panic/Makefile run-make/cat-and-grep-sanity-check/Makefile run-make/cdylib-dylib-linkage/Makefile diff --git a/tests/run-make/c-dynamic-dylib/Makefile b/tests/run-make/c-dynamic-dylib/Makefile deleted file mode 100644 index 39561b28222ca..0000000000000 --- a/tests/run-make/c-dynamic-dylib/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -# This test checks that dynamic Rust linking with C does not encounter any errors, with dynamic dependencies given preference over static. -# See https://github.com/rust-lang/rust/issues/10434 - -# ignore-cross-compile -include ../tools.mk - -# ignore-apple -# -# This hits an assertion in the linker on older versions of osx apparently - -all: $(call DYLIB,cfoo) - $(RUSTC) foo.rs -C prefer-dynamic - $(RUSTC) bar.rs - $(call RUN,bar) - $(call REMOVE_DYLIBS,cfoo) - $(call FAIL,bar) diff --git a/tests/run-make/c-dynamic-dylib/rmake.rs b/tests/run-make/c-dynamic-dylib/rmake.rs new file mode 100644 index 0000000000000..65b5e02abf087 --- /dev/null +++ b/tests/run-make/c-dynamic-dylib/rmake.rs @@ -0,0 +1,17 @@ +// This test checks that dynamic Rust linking with C does not encounter any errors in both +// compilation and execution, with dynamic dependencies given preference over static. +// See https://github.com/rust-lang/rust/issues/10434 + +//@ ignore-cross-compile +// Reason: the compiled binary is executed + +use run_make_support::{build_native_dynamic_lib, dynamic_lib_name, rfs, run, run_fail, rustc}; + +fn main() { + build_native_dynamic_lib("cfoo"); + rustc().input("foo.rs").arg("-Cprefer-dynamic").run(); + rustc().input("bar.rs").run(); + run("bar"); + rfs::remove_file(dynamic_lib_name("cfoo")); + run_fail("bar"); +} diff --git a/tests/run-make/c-dynamic-rlib/rmake.rs b/tests/run-make/c-dynamic-rlib/rmake.rs index c48b1edee9e69..b59887bbdd631 100644 --- a/tests/run-make/c-dynamic-rlib/rmake.rs +++ b/tests/run-make/c-dynamic-rlib/rmake.rs @@ -6,17 +6,13 @@ //@ ignore-cross-compile // Reason: the compiled binary is executed -//FIXME(Oneirical): test on apple because older versions of osx are failing apparently - -use run_make_support::{ - build_native_dynamic_lib, dynamic_lib_name, fs_wrapper, run, run_fail, rustc, -}; +use run_make_support::{build_native_dynamic_lib, dynamic_lib_name, rfs, run, run_fail, rustc}; fn main() { build_native_dynamic_lib("cfoo"); rustc().input("foo.rs").run(); rustc().input("bar.rs").run(); run("bar"); - fs_wrapper::remove_file(dynamic_lib_name("cfoo")); + rfs::remove_file(dynamic_lib_name("cfoo")); run_fail("bar"); }