Skip to content

Commit 20adc75

Browse files
committed
Auto merge of rust-lang#2562 - pvdrz:miri-num-cpus, r=RalfJung
Add flag to specify the number of cpus Apparently you can't rename a branch from github's website without it closing all your PRs with that branch. So this is just rust-lang#2545
2 parents e8683f5 + 9ce9dae commit 20adc75

File tree

8 files changed

+33
-8
lines changed

8 files changed

+33
-8
lines changed

src/tools/miri/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ environment variable. We first document the most relevant and most commonly used
293293
value of forwarded variables stays the same. Has no effect if `-Zmiri-disable-isolation` is set.
294294
* `-Zmiri-ignore-leaks` disables the memory leak checker, and also allows some
295295
remaining threads to exist when the main thread exits.
296+
* `-Zmiri-num-cpus` states the number of available CPUs to be reported by miri. By default, the
297+
number of available CPUs is `1`. Note that this flag does not affect how miri handles threads in
298+
any way.
296299
* `-Zmiri-permissive-provenance` disables the warning for integer-to-pointer casts and
297300
[`ptr::from_exposed_addr`](https://doc.rust-lang.org/nightly/std/ptr/fn.from_exposed_addr.html).
298301
This will necessarily miss some bugs as those operations are not efficiently and accurately
@@ -357,7 +360,7 @@ to Miri failing to detect cases of undefined behavior in a program.
357360
This is **work in progress**; currently, only integer arguments and return values are
358361
supported (and no, pointer/integer casts to work around this limitation will not work;
359362
they will fail horribly). It also only works on unix hosts for now.
360-
Follow [the discussion on supporting other types](https://github.com/rust-lang/miri/issues/2365).
363+
Follow [the discussion on supporting other types](https://github.com/rust-lang/miri/issues/2365).
361364
* `-Zmiri-measureme=<name>` enables `measureme` profiling for the interpreted program.
362365
This can be used to find which parts of your program are executing slowly under Miri.
363366
The profile is written out to a file with the prefix `<name>`, and can be processed
@@ -387,7 +390,7 @@ to Miri failing to detect cases of undefined behavior in a program.
387390
Borrows "protectors". Specifying this argument multiple times does not overwrite the previous
388391
values, instead it appends its values to the list. Listing an id multiple times has no effect.
389392
* `-Zmiri-track-pointer-tag=<tag1>,<tag2>,...` shows a backtrace when a given pointer tag
390-
is created and when (if ever) it is popped from a borrow stack (which is where the tag becomes invalid
393+
is created and when (if ever) it is popped from a borrow stack (which is where the tag becomes invalid
391394
and any future use of it will error). This helps you in finding out why UB is
392395
happening and where in your code would be a good place to look for it.
393396
Specifying this argument multiple times does not overwrite the previous

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,13 @@ fn main() {
550550
} else {
551551
show_error!("-Zmiri-extern-so-file `{}` does not exist", filename);
552552
}
553+
} else if let Some(param) = arg.strip_prefix("-Zmiri-num-cpus=") {
554+
let num_cpus = match param.parse::<u32>() {
555+
Ok(i) => i,
556+
Err(err) => show_error!("-Zmiri-num-cpus requires a `u32`: {}", err),
557+
};
558+
559+
miri_config.num_cpus = num_cpus;
553560
} else {
554561
// Forward to rustc.
555562
rustc_args.push(arg);

src/tools/miri/src/eval.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ pub struct MiriConfig {
132132
pub external_so_file: Option<PathBuf>,
133133
/// Run a garbage collector for SbTags every N basic blocks.
134134
pub gc_interval: u32,
135+
/// The number of CPUs to be reported by miri.
136+
pub num_cpus: u32,
135137
}
136138

137139
impl Default for MiriConfig {
@@ -164,6 +166,7 @@ impl Default for MiriConfig {
164166
retag_fields: false,
165167
external_so_file: None,
166168
gc_interval: 10_000,
169+
num_cpus: 1,
167170
}
168171
}
169172
}

src/tools/miri/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub use crate::helpers::{CurrentSpan, EvalContextExt as HelpersEvalContextExt};
104104
pub use crate::intptrcast::ProvenanceMode;
105105
pub use crate::machine::{
106106
AllocExtra, FrameData, MiriInterpCx, MiriInterpCxExt, MiriMachine, MiriMemoryKind, Provenance,
107-
ProvenanceExtra, NUM_CPUS, PAGE_SIZE, STACK_ADDR, STACK_SIZE,
107+
ProvenanceExtra, PAGE_SIZE, STACK_ADDR, STACK_SIZE,
108108
};
109109
pub use crate::mono_hash_map::MonoHashMap;
110110
pub use crate::operator::EvalContextExt as OperatorEvalContextExt;

src/tools/miri/src/machine.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use crate::{
3535
pub const PAGE_SIZE: u64 = 4 * 1024; // FIXME: adjust to target architecture
3636
pub const STACK_ADDR: u64 = 32 * PAGE_SIZE; // not really about the "stack", but where we start assigning integer addresses to allocations
3737
pub const STACK_SIZE: u64 = 16 * PAGE_SIZE; // whatever
38-
pub const NUM_CPUS: u64 = 1;
3938

4039
/// Extra data stored with each stack frame
4140
pub struct FrameData<'tcx> {
@@ -410,6 +409,8 @@ pub struct MiriMachine<'mir, 'tcx> {
410409
pub(crate) gc_interval: u32,
411410
/// The number of blocks that passed since the last SbTag GC pass.
412411
pub(crate) since_gc: u32,
412+
/// The number of CPUs to be reported by miri.
413+
pub(crate) num_cpus: u32,
413414
}
414415

415416
impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
@@ -489,6 +490,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
489490
}),
490491
gc_interval: config.gc_interval,
491492
since_gc: 0,
493+
num_cpus: config.num_cpus,
492494
}
493495
}
494496

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
225225
"sysconf" => {
226226
let [name] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
227227
let name = this.read_scalar(name)?.to_i32()?;
228-
229228
// FIXME: Which of these are POSIX, and which are GNU/Linux?
230229
// At least the names seem to all also exist on macOS.
231230
let sysconfs: &[(&str, fn(&MiriInterpCx<'_, '_>) -> Scalar<Provenance>)] = &[
232231
("_SC_PAGESIZE", |this| Scalar::from_int(PAGE_SIZE, this.pointer_size())),
233-
("_SC_NPROCESSORS_CONF", |this| Scalar::from_int(NUM_CPUS, this.pointer_size())),
234-
("_SC_NPROCESSORS_ONLN", |this| Scalar::from_int(NUM_CPUS, this.pointer_size())),
232+
("_SC_NPROCESSORS_CONF", |this| Scalar::from_int(this.machine.num_cpus, this.pointer_size())),
233+
("_SC_NPROCESSORS_ONLN", |this| Scalar::from_int(this.machine.num_cpus, this.pointer_size())),
235234
// 512 seems to be a reasonable default. The value is not critical, in
236235
// the sense that getpwuid_r takes and checks the buffer length.
237236
("_SC_GETPW_R_SIZE_MAX", |this| Scalar::from_int(512, this.pointer_size()))

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
163163
)?;
164164
// Set number of processors.
165165
let num_cpus = system_info.offset(field_offsets[6], dword_layout, &this.tcx)?;
166-
this.write_scalar(Scalar::from_int(NUM_CPUS, dword_layout.size), &num_cpus.into())?;
166+
this.write_scalar(
167+
Scalar::from_int(this.machine.num_cpus, dword_layout.size),
168+
&num_cpus.into(),
169+
)?;
167170
}
168171

169172
// Thread-local storage
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@compile-flags: -Zmiri-num-cpus=1024
2+
3+
use std::num::NonZeroUsize;
4+
use std::thread::available_parallelism;
5+
6+
fn main() {
7+
assert_eq!(available_parallelism().unwrap(), NonZeroUsize::new(1024).unwrap());
8+
}

0 commit comments

Comments
 (0)