Skip to content

Ensures that our instrumentation is actually added and executed in UI testing. #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ permissions:
defaults:
run:
shell: bash
concurrency:
group: build
cancel-in-progress: true
jobs:
fmt:
runs-on: macos-latest
Expand All @@ -34,18 +31,15 @@ jobs:
build:
needs: [fmt]
strategy:
fail-fast: true
fail-fast: false
matrix:
config:
- arch: x86 Linux
os: ubuntu-latest
install_cmd: sudo apt-get install ninja-build
- arch: ARM64 macOS
os: macos-latest
install_cmd: brew install ninja
- arch: ARM64 Linux
os: ubuntu-24.04-arm
install_cmd: sudo apt-get install ninja-build
# - arch: ARM64 Linux
# os: ubuntu-24.04-arm
runs-on: '${{ matrix.config.os }}'
name: 'Build (${{ matrix.config.arch }})'
steps:
Expand All @@ -54,7 +48,6 @@ jobs:
fetch-depth: 0
- name: Install dependencies
run: |
${{matrix.config.install_cmd}}
rustup component add llvm-tools-preview
cp src/bootstrap/defaults/config.bsan.dev.toml config.toml
- name: Upstream
Expand All @@ -64,5 +57,5 @@ jobs:
- name: Unit Tests
run: ./x.py test --stage 1 src/tools/bsan/bsan-rt
- name: UI Tests
run: ./x.py test --stage 1 src/tools/bsan/bsan-driver
run: ./x.py test --stage 2 src/tools/bsan/bsan-driver

37 changes: 20 additions & 17 deletions compiler/rustc_codegen_ssa/src/mir/statement.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use rustc_middle::mir::{self, NonDivergingIntrinsic /* , PlaceKind*/};
use rustc_middle::mir::{self, NonDivergingIntrinsic, PlaceKind};
use rustc_middle::span_bug;
use tracing::instrument;

//use super::operand::OperandValue;
use super::operand::OperandValue;
use super::{FunctionCx, LocalRef};
use crate::mir::place::PlaceValue;
use crate::traits::*;

impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
Expand Down Expand Up @@ -88,9 +89,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let src = src_val.immediate();
bx.memcpy(dst, align, src, align, bytes, crate::MemFlags::empty());
}

/*mir::StatementKind::Retag(retag_kind, box ref place) => {

mir::StatementKind::Retag(retag_kind, box ref place) => {
if self.cx.sess().emit_retags() {
let place_value = if let Some(index) = place.as_local() {
match self.locals[index] {
Expand All @@ -103,27 +102,31 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
place
);
}
LocalRef::Operand(op) => match op.val {
OperandValue::Ref(r) => r,
OperandValue::Immediate(_) | OperandValue::Pair(_, _) => {
let mono_ty = self.monomorphized_place_ty(place.as_ref());
if mono_ty.is_any_ptr() {
op.deref(bx.cx()).val
} else {
return;
LocalRef::Operand(op) => {
let mono_ty = self.monomorphized_place_ty(place.as_ref());
if mono_ty.is_any_ptr() {
match op.val {
OperandValue::Ref(r) => r,
OperandValue::Immediate(llval) => {
PlaceValue::new_sized(llval, op.layout.align.abi)
}
OperandValue::Pair(llptr, _) => {
PlaceValue::new_sized(llptr, op.layout.align.abi)
}
OperandValue::ZeroSized => return,
}
} else {
return;
}
OperandValue::ZeroSized => return,
},
}
}
} else {
self.codegen_place(bx, place.as_ref()).val
};
bx.retag(place_value, PlaceKind::Default, retag_kind);
}
}*/
}
mir::StatementKind::FakeRead(..)
| mir::StatementKind::Retag(..)
| mir::StatementKind::AscribeUserType(..)
| mir::StatementKind::ConstEvalCounter
| mir::StatementKind::PlaceMention(..)
Expand Down
21 changes: 21 additions & 0 deletions src/bootstrap/defaults/config.bsan.ci.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
change-id = 132494

[build]
build-stage = 1
test-stage = 1
docs = false
extended = true
tools = [
"bsan-rt",
"bsan",
"cargo-bsan",
]

[rust]
description = "BorrowSanitizer"
llvm-tools = true

[llvm]
download-ci-llvm = false
clang = true
link-shared = true
2 changes: 1 addition & 1 deletion src/bootstrap/defaults/config.bsan.dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ docs = false
extended = true
tools = [
"bsan-rt",
"bsan",
"cargo-bsan",
"bsan-driver"
]

[rust]
Expand Down
37 changes: 21 additions & 16 deletions src/bootstrap/src/core/build_steps/bsan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct BsanRT {
pub target: TargetSelection,
}
impl Step for BsanRT {
type Output = Option<SanitizerRuntime>;
type Output = PathBuf;
const DEFAULT: bool = true;
const ONLY_HOSTS: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
Expand Down Expand Up @@ -53,26 +53,34 @@ impl Step for BsanRT {
exit!(1);
}

let bindir = builder.sysroot_target_bindir(compiler, target);
let out_dir = builder.native_dir(self.target).join("sanitizers");

let cpp_runtime = supports_bsan(&out_dir, self.target, &builder.config.channel);
if cpp_runtime.is_none() {
return None;
eprintln!("ERROR: BorrowSanitizer is not supported for target {}.", self.target);
exit!(1);
}

let cpp_runtime = cpp_runtime.unwrap();
let LlvmResult { llvm_config, .. } = builder.ensure(Llvm { target: builder.config.build });

let compiler_rt_dir = builder.src.join("src/llvm-project/compiler-rt");
if !compiler_rt_dir.exists() {
return None;
}

let lib_name = cpp_runtime.name;

// Like other sanitizer runtimes, we want to install this runtime into
// both rustlib and the root of the sysroot libdir
let rust_runtime_path = builder.ensure(BsanRTCore { compiler, target });
let rust_runtime_parent_dir = rust_runtime_path.parent().unwrap();

let libdir = builder.sysroot_target_libdir(compiler, target);
let rustc_libdir = builder.rustc_libdir(compiler);

let dst = libdir.join(&lib_name);
let rustc_dst = rustc_libdir.join(&lib_name);

if builder.config.dry_run() {
return Some(cpp_runtime);
return rustc_dst;
}
// On targets that build BSAN as a static runtime, we need to manually add in the object files
// for the Rust runtime using llvm-ar (see below). If the C++ sources haven't changed, then CMake
Expand Down Expand Up @@ -136,13 +144,15 @@ impl Step for BsanRT {
// way to declare an external static archive (libbsan_rt.a) as a build dependency
// of another static archive (libclang-rt-<arch>.bsan.a) in CMake—at least, not if
// the external archive contains an unknown, varying quantity of object files.
if !is_dylib(&cpp_runtime.name) {
if !is_dylib(&lib_name) {
let temp_dir = builder.build.tempdir().join("bsan-rt");
if temp_dir.exists() {
fs::remove_dir_all(&temp_dir).unwrap();
}
fs::create_dir_all(&temp_dir).unwrap();

let bindir = builder.sysroot_target_bindir(compiler, target);

// Since our Rust runtime depends on core,
// we need to remove all global symbols except for
// our API endpoints to avoid clashing with users' programs.
Expand Down Expand Up @@ -176,22 +186,17 @@ impl Step for BsanRT {
.run(builder);
}

let libdir = builder.sysroot_target_libdir(compiler, target);
let dst = libdir.join(&cpp_runtime.name);
builder.copy_link(&cpp_runtime.path, &dst);

if target.contains("-apple-") {
// Update the library’s install name to reflect that it has been renamed.
apple_darwin_update_library_name(
builder,
&dst,
&format!("@rpath/{}", &cpp_runtime.name),
);
apple_darwin_update_library_name(builder, &dst, &format!("@rpath/{}", &lib_name));
// Upon renaming the install name, the code signature of the file will invalidate,
// so we will sign it again.
apple_darwin_sign_file(builder, &dst);
}
Some(cpp_runtime)
builder.copy_link(&dst, &rustc_dst);
rustc_dst
}
}

Expand Down
58 changes: 57 additions & 1 deletion src/bootstrap/src/core/build_steps/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::{env, fs};
use object::BinaryFormat;
use object::read::archive::ArchiveFile;

use crate::core::build_steps::bsan::BsanRT;
use crate::core::build_steps::doc::DocumentationFormat;
use crate::core::build_steps::tool::{self, Tool};
use crate::core::build_steps::vendor::default_paths_to_vendor;
Expand Down Expand Up @@ -1256,6 +1257,60 @@ impl Step for Clippy {
}
}

#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
pub struct Bsan {
pub compiler: Compiler,
pub target: TargetSelection,
}

impl Step for Bsan {
type Output = Option<GeneratedTarball>;
const DEFAULT: bool = true;
const ONLY_HOSTS: bool = true;

fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let default = should_build_extended_tool(run.builder, "bsan-driver");
run.alias("bsan").default_condition(default)
}

fn make_run(run: RunConfig<'_>) {
run.builder.ensure(Bsan {
compiler: run.builder.compiler_for(
run.builder.top_stage,
run.builder.config.build,
run.target,
),
target: run.target,
});
}

fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
// This prevents bsan from being built for "dist" or "install"
// on the stable/beta channels. It is a nightly-only tool and should
// not be included.
if !builder.build.unstable_features() {
return None;
}
let compiler = self.compiler;
let target = self.target;

let bsanrt = builder.ensure(BsanRT { compiler, target });
let bsandriver =
builder.ensure(tool::BsanDriver { compiler, target, extra_features: Vec::new() });
let cargobsan =
builder.ensure(tool::CargoBsan { compiler, target, extra_features: Vec::new() });

let mut tarball = Tarball::new(builder, "bsan", &target.triple);
tarball.set_overlay(OverlayKind::Bsan);
tarball.is_preview(true);
tarball.add_file(bsandriver, "bin", 0o755);
tarball.add_file(cargobsan, "bin", 0o755);
tarball.add_file(bsanrt, "lib", 0o755);
tarball.add_legal_and_readme_to("share/doc/bsan");
Some(tarball.generate())
}
}

#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
pub struct Miri {
pub compiler: Compiler,
Expand Down Expand Up @@ -1504,6 +1559,7 @@ impl Step for Extended {
add_component!("llvm-components" => LlvmTools { target });
add_component!("clippy" => Clippy { compiler, target });
add_component!("miri" => Miri { compiler, target });
add_component!("bsan" => Bsan { compiler, target });
add_component!("analysis" => Analysis { compiler, target });
add_component!("rustc-codegen-cranelift" => CodegenBackend {
compiler: builder.compiler(stage, target),
Expand Down Expand Up @@ -1909,7 +1965,7 @@ impl Step for Extended {
cmd.arg("-dMiriDir=miri");
}
if built_tools.contains("bsan") {
cmd.arg("-dBsanDir=miri");
cmd.arg("-dBsanDir=bsan");
}
if target.is_windows_gnu() {
cmd.arg("-dGccDir=rust-mingw");
Expand Down
Loading