Skip to content

Commit 1ec279f

Browse files
committed
Auto merge of #801 - RalfJung:num_cpus, r=RalfJung
support num_cpus crate and test that Also make some magic numbers into proper global constants.
2 parents 1522a47 + 019ad4b commit 1ec279f

File tree

11 files changed

+66
-27
lines changed

11 files changed

+66
-27
lines changed

src/fn_call.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -622,11 +622,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
622622
let name = this.read_scalar(args[0])?.to_i32()?;
623623

624624
trace!("sysconf() called with name {}", name);
625-
// Cache the sysconf integers via Miri's global cache.
625+
// TODO: Cache the sysconf integers via Miri's global cache.
626626
let paths = &[
627-
(&["libc", "_SC_PAGESIZE"], Scalar::from_int(4096, dest.layout.size)),
627+
(&["libc", "_SC_PAGESIZE"], Scalar::from_int(PAGE_SIZE, dest.layout.size)),
628628
(&["libc", "_SC_GETPW_R_SIZE_MAX"], Scalar::from_int(-1, dest.layout.size)),
629-
(&["libc", "_SC_NPROCESSORS_ONLN"], Scalar::from_int(1, dest.layout.size)),
629+
(&["libc", "_SC_NPROCESSORS_ONLN"], Scalar::from_int(NUM_CPUS, dest.layout.size)),
630630
];
631631
let mut result = None;
632632
for &(path, path_value) in paths {
@@ -648,6 +648,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
648648
}
649649
}
650650

651+
"sched_getaffinity" => {
652+
// Return an error; `num_cpus` then falls back to `sysconf`.
653+
this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
654+
}
655+
651656
"isatty" => {
652657
this.write_null(dest)?;
653658
}
@@ -722,14 +727,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
722727
// Second argument is where we are supposed to write the stack size.
723728
let ptr = this.deref_operand(args[1])?;
724729
// Just any address.
725-
let stack_addr = Scalar::from_int(0x80000, args[1].layout.size);
730+
let stack_addr = Scalar::from_uint(STACK_ADDR, args[1].layout.size);
726731
this.write_scalar(stack_addr, ptr.into())?;
727732
// Return success (`0`).
728733
this.write_null(dest)?;
729734
}
730735
"pthread_get_stackaddr_np" => {
731736
// Just any address.
732-
let stack_addr = Scalar::from_int(0x80000, dest.layout.size);
737+
let stack_addr = Scalar::from_uint(STACK_ADDR, dest.layout.size);
733738
this.write_scalar(stack_addr, dest)?;
734739
}
735740

@@ -838,14 +843,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
838843
// Initialize with `0`.
839844
this.memory_mut().get_mut(system_info_ptr.alloc_id)?
840845
.write_repeat(tcx, system_info_ptr, 0, system_info.layout.size)?;
841-
// Set number of processors to `1`.
846+
// Set number of processors.
842847
let dword_size = Size::from_bytes(4);
843848
let offset = 2*dword_size + 3*tcx.pointer_size();
844849
this.memory_mut().get_mut(system_info_ptr.alloc_id)?
845850
.write_scalar(
846851
tcx,
847852
system_info_ptr.offset(offset, tcx)?,
848-
Scalar::from_int(1, dword_size).into(),
853+
Scalar::from_int(NUM_CPUS, dword_size).into(),
849854
dword_size,
850855
)?;
851856
}

src/intptrcast.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use rustc::mir::interpret::{AllocId, Pointer, InterpResult};
66
use rustc_mir::interpret::Memory;
77
use rustc_target::abi::Size;
88

9-
use crate::stacked_borrows::Tag;
10-
use crate::Evaluator;
9+
use crate::{Evaluator, Tag, STACK_ADDR};
1110

1211
pub type MemoryExtra = RefCell<GlobalState>;
1312

@@ -27,11 +26,10 @@ pub struct GlobalState {
2726
}
2827

2928
impl Default for GlobalState {
30-
// FIXME: Query the page size in the future
3129
fn default() -> Self {
3230
GlobalState {
3331
int_to_ptr_map: Vec::default(),
34-
next_base_addr: 2u64.pow(16)
32+
next_base_addr: STACK_ADDR,
3533
}
3634
}
3735
}

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ pub use crate::range_map::RangeMap;
3737
pub use crate::helpers::{EvalContextExt as HelpersEvalContextExt};
3838
pub use crate::mono_hash_map::MonoHashMap;
3939
pub use crate::stacked_borrows::{EvalContextExt as StackedBorEvalContextExt, Tag, Permission, Stack, Stacks, Item};
40-
pub use crate::machine::{MemoryExtra, AllocExtra, MiriMemoryKind, Evaluator, MiriEvalContext, MiriEvalContextExt};
40+
pub use crate::machine::{
41+
PAGE_SIZE, STACK_ADDR, NUM_CPUS,
42+
MemoryExtra, AllocExtra, MiriMemoryKind, Evaluator, MiriEvalContext, MiriEvalContextExt,
43+
};
4144
pub use crate::eval::{eval_main, create_ecx, MiriConfig};
4245

4346
/// Insert rustc arguments at the beginning of the argument list that Miri wants to be

src/machine.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ use rustc::mir;
1313

1414
use crate::*;
1515

16+
// Some global facts about the emulated machine.
17+
pub const PAGE_SIZE: u64 = 4*1024; // FIXME: adjust to target architecture
18+
pub const STACK_ADDR: u64 = 16*PAGE_SIZE; // not really about the "stack", but where we start assigning integer addresses to allocations
19+
pub const NUM_CPUS: u64 = 1;
20+
1621
/// Extra memory kinds
1722
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
1823
pub enum MiriMemoryKind {

test-cargo-miri/Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-cargo-miri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ byteorder = "1.0"
99

1010
[dev-dependencies]
1111
rand = { version = "0.7", features = ["small_rng"] }
12+
num_cpus = "1.10.1"

test-cargo-miri/run-test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import sys, subprocess, os
99

1010
def fail(msg):
11-
print("TEST FAIL: {}".format(msg))
11+
print("\nTEST FAIL: {}".format(msg))
1212
sys.exit(1)
1313

1414
def cargo_miri(cmd):
@@ -57,7 +57,7 @@ def test_cargo_miri_test():
5757
"test.stdout.ref", "test.stderr.ref"
5858
)
5959
test("cargo miri test (with filter)",
60-
cargo_miri("test") + ["--", "--", "impl"],
60+
cargo_miri("test") + ["--", "--", "le1"],
6161
"test.stdout.ref2", "test.stderr.ref"
6262
)
6363

@@ -66,5 +66,5 @@ def test_cargo_miri_test():
6666
test_cargo_miri_run()
6767
test_cargo_miri_test()
6868

69-
print("TEST SUCCESSFUL!")
69+
print("\nTEST SUCCESSFUL!")
7070
sys.exit(0)

test-cargo-miri/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use byteorder::{BigEndian, ByteOrder};
22

33
fn main() {
4+
// Exercise external crate, printing to stdout.
45
let buf = &[1,2,3,4];
56
let n = <BigEndian as ByteOrder>::read_u32(buf);
67
assert_eq!(n, 0x01020304);
78
println!("{:#010x}", n);
9+
10+
// Access program arguments, printing to stderr.
811
for arg in std::env::args() {
912
eprintln!("{}", arg);
1013
}

test-cargo-miri/test.stdout.ref

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ test test::rng ... ok
55
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
66

77

8-
running 2 tests
8+
running 4 tests
99
test entropy_rng ... ok
10-
test simple ... ok
10+
test num_cpus ... ok
11+
test simple1 ... ok
12+
test simple2 ... ok
1113

12-
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
14+
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
1315

test-cargo-miri/test.stdout.ref2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out
55

66

77
running 1 test
8-
test simple ... ok
8+
test simple1 ... ok
99

10-
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out
10+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 3 filtered out
1111

test-cargo-miri/tests/test.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,28 @@ use rand::{SeedableRng, Rng, rngs::SmallRng};
33
// Having more than 1 test does seem to make a difference
44
// (i.e., this calls ptr::swap which having just one test does not).
55
#[test]
6-
fn simple() {
6+
fn simple1() {
77
assert_eq!(4, 4);
88
}
99

10+
#[test]
11+
fn simple2() {
12+
assert_ne!(42, 24);
13+
}
14+
15+
// A test that won't work on miri (tests disabling tests)
16+
#[cfg(not(miri))]
17+
#[test]
18+
fn does_not_work_on_miri() {
19+
let x = 0u8;
20+
assert!(&x as *const _ as usize % 4 < 4);
21+
}
22+
23+
// We also use this to test some external crates, that we cannot depend on in the compiletest suite.
24+
1025
#[test]
1126
fn entropy_rng() {
12-
// Use this opportunity to test querying the RNG (needs an external crate, hence tested here and not in the compiletest suite)
27+
// Try seeding with "real" entropy.
1328
let mut rng = SmallRng::from_entropy();
1429
let _val = rng.gen::<i32>();
1530
let _val = rng.gen::<isize>();
@@ -22,10 +37,7 @@ fn entropy_rng() {
2237
let _val = rng.gen::<i128>();
2338
}
2439

25-
// A test that won't work on miri
26-
#[cfg(not(miri))]
2740
#[test]
28-
fn does_not_work_on_miri() {
29-
let x = 0u8;
30-
assert!(&x as *const _ as usize % 4 < 4);
41+
fn num_cpus() {
42+
assert_eq!(num_cpus::get(), 1);
3143
}

0 commit comments

Comments
 (0)