Skip to content

Commit 24ea3e2

Browse files
committed
make native-lib support compile-time-optional, and centralize cfg usage
1 parent 8339590 commit 24ea3e2

File tree

9 files changed

+52
-37
lines changed

9 files changed

+52
-37
lines changed

src/tools/miri/Cargo.toml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ features = ['unprefixed_malloc_on_supported_platforms']
3838

3939
[target.'cfg(unix)'.dependencies]
4040
libc = "0.2"
41-
libffi = "4.0.0"
42-
libloading = "0.8"
41+
libffi = { version = "4.0.0", optional = true }
42+
libloading = { version = "0.8", optional = true }
43+
nix = { version = "0.30.1", features = ["mman", "ptrace", "signal"], optional = true }
4344

4445
[target.'cfg(target_os = "linux")'.dependencies]
45-
nix = { version = "0.30.1", features = ["mman", "ptrace", "signal"] }
46-
ipc-channel = "0.19.0"
47-
serde = { version = "1.0.219", features = ["derive"] }
48-
capstone = "0.13"
46+
ipc-channel = { version = "0.19.0", optional = true }
47+
serde = { version = "1.0.219", features = ["derive"], optional = true }
48+
capstone = { version = "0.13", optional = true }
4949

5050
[dev-dependencies]
5151
ui_test = "0.29.1"
@@ -64,11 +64,12 @@ name = "ui"
6464
harness = false
6565

6666
[features]
67-
default = ["stack-cache"]
67+
default = ["stack-cache", "native-lib"]
6868
genmc = []
6969
stack-cache = []
7070
stack-cache-consistency-check = ["stack-cache"]
7171
tracing = ["serde_json"]
72+
native-lib = ["dep:libffi", "dep:libloading", "dep:capstone", "dep:ipc-channel", "dep:nix", "dep:serde"]
7273

7374
[lints.rust.unexpected_cfgs]
7475
level = "warn"

src/tools/miri/src/alloc/alloc_bytes.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
use std::alloc::Layout;
22
use std::borrow::Cow;
3+
use std::cell::RefCell;
4+
use std::rc::Rc;
35
use std::{alloc, slice};
4-
#[cfg(target_os = "linux")]
5-
use std::{cell::RefCell, rc::Rc};
66

77
use rustc_abi::{Align, Size};
88
use rustc_middle::mir::interpret::AllocBytes;
99

10-
#[cfg(target_os = "linux")]
1110
use crate::alloc::isolated_alloc::IsolatedAlloc;
1211
use crate::helpers::ToU64 as _;
1312

1413
#[derive(Clone, Debug)]
1514
pub enum MiriAllocParams {
1615
Global,
17-
#[cfg(target_os = "linux")]
1816
Isolated(Rc<RefCell<IsolatedAlloc>>),
1917
}
2018

@@ -56,7 +54,6 @@ impl Drop for MiriAllocBytes {
5654
unsafe {
5755
match self.params.clone() {
5856
MiriAllocParams::Global => alloc::dealloc(self.ptr, alloc_layout),
59-
#[cfg(target_os = "linux")]
6057
MiriAllocParams::Isolated(alloc) =>
6158
alloc.borrow_mut().dealloc(self.ptr, alloc_layout),
6259
}
@@ -123,7 +120,6 @@ impl AllocBytes for MiriAllocBytes {
123120
let alloc_fn = |layout, params: &MiriAllocParams| unsafe {
124121
match params {
125122
MiriAllocParams::Global => alloc::alloc(layout),
126-
#[cfg(target_os = "linux")]
127123
MiriAllocParams::Isolated(alloc) => alloc.borrow_mut().alloc(layout),
128124
}
129125
};
@@ -144,7 +140,6 @@ impl AllocBytes for MiriAllocBytes {
144140
let alloc_fn = |layout, params: &MiriAllocParams| unsafe {
145141
match params {
146142
MiriAllocParams::Global => alloc::alloc_zeroed(layout),
147-
#[cfg(target_os = "linux")]
148143
MiriAllocParams::Isolated(alloc) => alloc.borrow_mut().alloc_zeroed(layout),
149144
}
150145
};

src/tools/miri/src/alloc/mod.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
mod alloc_bytes;
2-
#[cfg(target_os = "linux")]
2+
#[cfg(all(unix, feature = "native-lib"))]
33
pub mod isolated_alloc;
4+
#[cfg(not(all(unix, feature = "native-lib")))]
5+
pub mod isolated_alloc {
6+
use std::alloc::Layout;
7+
8+
/// Stub allocator to avoid `cfg`s in the rest of Miri.
9+
#[derive(Debug)]
10+
pub struct IsolatedAlloc(!);
11+
12+
impl IsolatedAlloc {
13+
pub fn new() -> Self {
14+
unreachable!()
15+
}
16+
17+
pub unsafe fn alloc(&mut self, _layout: Layout) -> *mut u8 {
18+
match self.0 {}
19+
}
20+
21+
pub unsafe fn alloc_zeroed(&mut self, _layout: Layout) -> *mut u8 {
22+
match self.0 {}
23+
}
24+
25+
pub unsafe fn dealloc(&mut self, _ptr: *mut u8, _layout: Layout) {
26+
match self.0 {}
27+
}
28+
}
29+
}
430

531
pub use self::alloc_bytes::{MiriAllocBytes, MiriAllocParams};

src/tools/miri/src/bin/miri.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,10 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
335335
fn exit(exit_code: i32) -> ! {
336336
// Drop the tracing guard before exiting, so tracing calls are flushed correctly.
337337
deinit_loggers();
338-
// Make sure the supervisor knows about the code code.
339-
#[cfg(target_os = "linux")]
338+
// Make sure the supervisor knows about the exit code.
339+
#[cfg(all(unix, feature = "native-lib"))]
340340
miri::native_lib::register_retcode_sv(exit_code);
341+
// Actually exit.
341342
std::process::exit(exit_code);
342343
}
343344

@@ -754,7 +755,7 @@ fn main() {
754755
debug!("crate arguments: {:?}", miri_config.args);
755756
if !miri_config.native_lib.is_empty() && miri_config.native_lib_enable_tracing {
756757
// SAFETY: No other threads are running
757-
#[cfg(target_os = "linux")]
758+
#[cfg(all(unix, feature = "native-lib"))]
758759
if unsafe { miri::native_lib::init_sv() }.is_err() {
759760
eprintln!(
760761
"warning: The native-lib tracer could not be started. Is this an x86 Linux system, and does Miri have permissions to ptrace?\n\

src/tools/miri/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub use rustc_const_eval::interpret::{self, AllocMap, Provenance as _};
9797
use rustc_middle::{bug, span_bug};
9898
use tracing::{info, trace};
9999

100-
#[cfg(target_os = "linux")]
100+
#[cfg(all(unix, feature = "native-lib"))]
101101
pub mod native_lib {
102102
pub use crate::shims::{init_sv, register_retcode_sv};
103103
}

src/tools/miri/src/machine.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,6 @@ pub struct MiriMachine<'tcx> {
530530
pub(crate) rng: RefCell<StdRng>,
531531

532532
/// The allocator used for the machine's `AllocBytes` in native-libs mode.
533-
#[cfg(target_os = "linux")]
534533
pub(crate) allocator: Option<Rc<RefCell<crate::alloc::isolated_alloc::IsolatedAlloc>>>,
535534

536535
/// The allocation IDs to report when they are being allocated
@@ -554,9 +553,9 @@ pub struct MiriMachine<'tcx> {
554553
pub(crate) basic_block_count: u64,
555554

556555
/// Handle of the optional shared object file for native functions.
557-
#[cfg(unix)]
556+
#[cfg(all(unix, feature = "native-lib"))]
558557
pub native_lib: Vec<(libloading::Library, std::path::PathBuf)>,
559-
#[cfg(not(unix))]
558+
#[cfg(not(all(unix, feature = "native-lib")))]
560559
pub native_lib: Vec<!>,
561560

562561
/// Run a garbage collector for BorTags every N basic blocks.
@@ -603,7 +602,7 @@ pub struct MiriMachine<'tcx> {
603602
/// Remembers whether we already warned about an extern type with Stacked Borrows.
604603
pub(crate) sb_extern_type_warned: Cell<bool>,
605604
/// Remember whether we already warned about sharing memory with a native call.
606-
#[cfg(unix)]
605+
#[allow(unused)]
607606
pub(crate) native_call_mem_warned: Cell<bool>,
608607
/// Remembers which shims have already shown the warning about erroring in isolation.
609608
pub(crate) reject_in_isolation_warned: RefCell<FxHashSet<String>>,
@@ -718,7 +717,6 @@ impl<'tcx> MiriMachine<'tcx> {
718717
local_crates,
719718
extern_statics: FxHashMap::default(),
720719
rng: RefCell::new(rng),
721-
#[cfg(target_os = "linux")]
722720
allocator: if !config.native_lib.is_empty() {
723721
Some(Rc::new(RefCell::new(crate::alloc::isolated_alloc::IsolatedAlloc::new())))
724722
} else { None },
@@ -730,7 +728,7 @@ impl<'tcx> MiriMachine<'tcx> {
730728
report_progress: config.report_progress,
731729
basic_block_count: 0,
732730
monotonic_clock: MonotonicClock::new(config.isolated_op == IsolatedOp::Allow),
733-
#[cfg(unix)]
731+
#[cfg(all(unix, feature = "native-lib"))]
734732
native_lib: config.native_lib.iter().map(|lib_file_path| {
735733
let host_triple = rustc_session::config::host_tuple();
736734
let target_triple = tcx.sess.opts.target_triple.tuple();
@@ -752,9 +750,9 @@ impl<'tcx> MiriMachine<'tcx> {
752750
lib_file_path.clone(),
753751
)
754752
}).collect(),
755-
#[cfg(not(unix))]
753+
#[cfg(not(all(unix, feature = "native-lib")))]
756754
native_lib: config.native_lib.iter().map(|_| {
757-
panic!("calling functions from native libraries via FFI is only supported on Unix")
755+
panic!("calling functions from native libraries via FFI is not supported in this build of Miri")
758756
}).collect(),
759757
gc_interval: config.gc_interval,
760758
since_gc: 0,
@@ -771,7 +769,6 @@ impl<'tcx> MiriMachine<'tcx> {
771769
pthread_rwlock_sanity: Cell::new(false),
772770
pthread_condvar_sanity: Cell::new(false),
773771
sb_extern_type_warned: Cell::new(false),
774-
#[cfg(unix)]
775772
native_call_mem_warned: Cell::new(false),
776773
reject_in_isolation_warned: Default::default(),
777774
int2ptr_warned: Default::default(),
@@ -924,7 +921,6 @@ impl VisitProvenance for MiriMachine<'_> {
924921
backtrace_style: _,
925922
local_crates: _,
926923
rng: _,
927-
#[cfg(target_os = "linux")]
928924
allocator: _,
929925
tracked_alloc_ids: _,
930926
track_alloc_accesses: _,
@@ -949,7 +945,6 @@ impl VisitProvenance for MiriMachine<'_> {
949945
pthread_rwlock_sanity: _,
950946
pthread_condvar_sanity: _,
951947
sb_extern_type_warned: _,
952-
#[cfg(unix)]
953948
native_call_mem_warned: _,
954949
reject_in_isolation_warned: _,
955950
int2ptr_warned: _,
@@ -1817,13 +1812,10 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
18171812
fn get_default_alloc_params(&self) -> <Self::Bytes as AllocBytes>::AllocParams {
18181813
use crate::alloc::MiriAllocParams;
18191814

1820-
#[cfg(target_os = "linux")]
18211815
match &self.allocator {
18221816
Some(alloc) => MiriAllocParams::Isolated(alloc.clone()),
18231817
None => MiriAllocParams::Global,
18241818
}
1825-
#[cfg(not(target_os = "linux"))]
1826-
MiriAllocParams::Global
18271819
}
18281820

18291821
fn enter_trace_span(span: impl FnOnce() -> tracing::Span) -> impl EnteredTraceSpan {

src/tools/miri/src/shims/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
237237
let this = self.eval_context_mut();
238238

239239
// First deal with any external C functions in linked .so file.
240-
#[cfg(unix)]
240+
#[cfg(all(unix, feature = "native-lib"))]
241241
if !this.machine.native_lib.is_empty() {
242242
use crate::shims::native_lib::EvalContextExt as _;
243243
// An Ok(false) here means that the function being called was not exported

src/tools/miri/src/shims/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod aarch64;
44
mod alloc;
55
mod backtrace;
66
mod files;
7-
#[cfg(unix)]
7+
#[cfg(all(unix, feature = "native-lib"))]
88
mod native_lib;
99
mod unix;
1010
mod wasi;
@@ -23,7 +23,7 @@ pub mod tls;
2323
pub mod unwind;
2424

2525
pub use self::files::FdTable;
26-
#[cfg(target_os = "linux")]
26+
#[cfg(all(unix, feature = "native-lib"))]
2727
pub use self::native_lib::trace::{init_sv, register_retcode_sv};
2828
pub use self::unix::{DirTable, EpollInterestTable};
2929

src/tools/miri/tests/ui.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ fn main() -> Result<()> {
335335
ui(Mode::Panic, "tests/panic", &target, WithDependencies, tmpdir.path())?;
336336
ui(Mode::Fail, "tests/fail", &target, WithoutDependencies, tmpdir.path())?;
337337
ui(Mode::Fail, "tests/fail-dep", &target, WithDependencies, tmpdir.path())?;
338-
if cfg!(unix) && target == host {
338+
if cfg!(all(unix, feature = "native-lib")) && target == host {
339339
ui(Mode::Pass, "tests/native-lib/pass", &target, WithoutDependencies, tmpdir.path())?;
340340
ui(Mode::Fail, "tests/native-lib/fail", &target, WithoutDependencies, tmpdir.path())?;
341341
}

0 commit comments

Comments
 (0)