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 {