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.
This commit is contained in:
bors 2019-06-30 08:42:25 +00:00
commit 1ec279f290
11 changed files with 66 additions and 27 deletions

View File

@ -622,11 +622,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let name = this.read_scalar(args[0])?.to_i32()?;
trace!("sysconf() called with name {}", name);
// Cache the sysconf integers via Miri's global cache.
// TODO: Cache the sysconf integers via Miri's global cache.
let paths = &[
(&["libc", "_SC_PAGESIZE"], Scalar::from_int(4096, dest.layout.size)),
(&["libc", "_SC_PAGESIZE"], Scalar::from_int(PAGE_SIZE, dest.layout.size)),
(&["libc", "_SC_GETPW_R_SIZE_MAX"], Scalar::from_int(-1, dest.layout.size)),
(&["libc", "_SC_NPROCESSORS_ONLN"], Scalar::from_int(1, dest.layout.size)),
(&["libc", "_SC_NPROCESSORS_ONLN"], Scalar::from_int(NUM_CPUS, dest.layout.size)),
];
let mut result = None;
for &(path, path_value) in paths {
@ -648,6 +648,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
}
"sched_getaffinity" => {
// Return an error; `num_cpus` then falls back to `sysconf`.
this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
}
"isatty" => {
this.write_null(dest)?;
}
@ -722,14 +727,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// Second argument is where we are supposed to write the stack size.
let ptr = this.deref_operand(args[1])?;
// Just any address.
let stack_addr = Scalar::from_int(0x80000, args[1].layout.size);
let stack_addr = Scalar::from_uint(STACK_ADDR, args[1].layout.size);
this.write_scalar(stack_addr, ptr.into())?;
// Return success (`0`).
this.write_null(dest)?;
}
"pthread_get_stackaddr_np" => {
// Just any address.
let stack_addr = Scalar::from_int(0x80000, dest.layout.size);
let stack_addr = Scalar::from_uint(STACK_ADDR, dest.layout.size);
this.write_scalar(stack_addr, dest)?;
}
@ -838,14 +843,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// Initialize with `0`.
this.memory_mut().get_mut(system_info_ptr.alloc_id)?
.write_repeat(tcx, system_info_ptr, 0, system_info.layout.size)?;
// Set number of processors to `1`.
// Set number of processors.
let dword_size = Size::from_bytes(4);
let offset = 2*dword_size + 3*tcx.pointer_size();
this.memory_mut().get_mut(system_info_ptr.alloc_id)?
.write_scalar(
tcx,
system_info_ptr.offset(offset, tcx)?,
Scalar::from_int(1, dword_size).into(),
Scalar::from_int(NUM_CPUS, dword_size).into(),
dword_size,
)?;
}

View File

@ -6,8 +6,7 @@ use rustc::mir::interpret::{AllocId, Pointer, InterpResult};
use rustc_mir::interpret::Memory;
use rustc_target::abi::Size;
use crate::stacked_borrows::Tag;
use crate::Evaluator;
use crate::{Evaluator, Tag, STACK_ADDR};
pub type MemoryExtra = RefCell<GlobalState>;
@ -27,11 +26,10 @@ pub struct GlobalState {
}
impl Default for GlobalState {
// FIXME: Query the page size in the future
fn default() -> Self {
GlobalState {
int_to_ptr_map: Vec::default(),
next_base_addr: 2u64.pow(16)
next_base_addr: STACK_ADDR,
}
}
}

View File

@ -37,7 +37,10 @@ pub use crate::range_map::RangeMap;
pub use crate::helpers::{EvalContextExt as HelpersEvalContextExt};
pub use crate::mono_hash_map::MonoHashMap;
pub use crate::stacked_borrows::{EvalContextExt as StackedBorEvalContextExt, Tag, Permission, Stack, Stacks, Item};
pub use crate::machine::{MemoryExtra, AllocExtra, MiriMemoryKind, Evaluator, MiriEvalContext, MiriEvalContextExt};
pub use crate::machine::{
PAGE_SIZE, STACK_ADDR, NUM_CPUS,
MemoryExtra, AllocExtra, MiriMemoryKind, Evaluator, MiriEvalContext, MiriEvalContextExt,
};
pub use crate::eval::{eval_main, create_ecx, MiriConfig};
/// Insert rustc arguments at the beginning of the argument list that Miri wants to be

View File

@ -13,6 +13,11 @@ use rustc::mir;
use crate::*;
// Some global facts about the emulated machine.
pub const PAGE_SIZE: u64 = 4*1024; // FIXME: adjust to target architecture
pub const STACK_ADDR: u64 = 16*PAGE_SIZE; // not really about the "stack", but where we start assigning integer addresses to allocations
pub const NUM_CPUS: u64 = 1;
/// Extra memory kinds
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum MiriMemoryKind {

View File

@ -29,6 +29,7 @@ name = "cargo-miri-test"
version = "0.1.0"
dependencies = [
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -66,6 +67,14 @@ name = "libc"
version = "0.2.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "num_cpus"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ppv-lite86"
version = "0.2.5"
@ -148,6 +157,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum getrandom 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8d1dffef07351aafe6ef177e4dd2b8dcf503e6bc765dea3b0de9ed149a3db1ec"
"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14"
"checksum libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)" = "6281b86796ba5e4366000be6e9e18bf35580adf9e63fbe2294aadb587613a319"
"checksum num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bcef43580c035376c0705c42792c294b66974abbfd2789b511784023f71f3273"
"checksum ppv-lite86 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e3cbf9f658cdb5000fcf6f362b8ea2ba154b9f146a61c7a20d647034c6b6561b"
"checksum rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d47eab0e83d9693d40f825f86948aa16eff6750ead4bdffc4ab95b8b3a7f052c"
"checksum rand_chacha 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e193067942ef6f485a349a113329140d0ab9e2168ce92274499bb0e9a4190d9d"

View File

@ -9,3 +9,4 @@ byteorder = "1.0"
[dev-dependencies]
rand = { version = "0.7", features = ["small_rng"] }
num_cpus = "1.10.1"

View File

@ -8,7 +8,7 @@ and the working directory to contain the cargo-miri-test project.
import sys, subprocess, os
def fail(msg):
print("TEST FAIL: {}".format(msg))
print("\nTEST FAIL: {}".format(msg))
sys.exit(1)
def cargo_miri(cmd):
@ -57,7 +57,7 @@ def test_cargo_miri_test():
"test.stdout.ref", "test.stderr.ref"
)
test("cargo miri test (with filter)",
cargo_miri("test") + ["--", "--", "impl"],
cargo_miri("test") + ["--", "--", "le1"],
"test.stdout.ref2", "test.stderr.ref"
)
@ -66,5 +66,5 @@ os.chdir(os.path.dirname(os.path.realpath(__file__)))
test_cargo_miri_run()
test_cargo_miri_test()
print("TEST SUCCESSFUL!")
print("\nTEST SUCCESSFUL!")
sys.exit(0)

View File

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

View File

@ -5,9 +5,11 @@ test test::rng ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
running 2 tests
running 4 tests
test entropy_rng ... ok
test simple ... ok
test num_cpus ... ok
test simple1 ... ok
test simple2 ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

View File

@ -5,7 +5,7 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out
running 1 test
test simple ... ok
test simple1 ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 3 filtered out

View File

@ -3,13 +3,28 @@ use rand::{SeedableRng, Rng, rngs::SmallRng};
// Having more than 1 test does seem to make a difference
// (i.e., this calls ptr::swap which having just one test does not).
#[test]
fn simple() {
fn simple1() {
assert_eq!(4, 4);
}
#[test]
fn simple2() {
assert_ne!(42, 24);
}
// A test that won't work on miri (tests disabling tests)
#[cfg(not(miri))]
#[test]
fn does_not_work_on_miri() {
let x = 0u8;
assert!(&x as *const _ as usize % 4 < 4);
}
// We also use this to test some external crates, that we cannot depend on in the compiletest suite.
#[test]
fn entropy_rng() {
// Use this opportunity to test querying the RNG (needs an external crate, hence tested here and not in the compiletest suite)
// Try seeding with "real" entropy.
let mut rng = SmallRng::from_entropy();
let _val = rng.gen::<i32>();
let _val = rng.gen::<isize>();
@ -22,10 +37,7 @@ fn entropy_rng() {
let _val = rng.gen::<i128>();
}
// A test that won't work on miri
#[cfg(not(miri))]
#[test]
fn does_not_work_on_miri() {
let x = 0u8;
assert!(&x as *const _ as usize % 4 < 4);
fn num_cpus() {
assert_eq!(num_cpus::get(), 1);
}