Move the resource limits to the session in preparation for attributes configuring them

This commit is contained in:
Oliver Schneider 2018-01-30 11:18:37 +01:00
parent 0462d092d2
commit 65ed591197
No known key found for this signature in database
GPG Key ID: A69F8D225B3AD7D9
6 changed files with 22 additions and 33 deletions

View File

@ -103,6 +103,13 @@ pub struct Session {
/// The maximum length of types during monomorphization.
pub type_length_limit: Cell<usize>,
/// The maximum number of stackframes allowed in const eval
pub const_eval_stack_frame_limit: Cell<usize>,
/// The maximum number miri steps per constant
pub const_eval_step_limit: Cell<usize>,
/// The maximum number of virtual bytes per constant
pub const_eval_memory_limit: Cell<u64>,
/// The metadata::creader module may inject an allocator/panic_runtime
/// dependency if it didn't already find one, and this tracks what was
/// injected.
@ -1004,6 +1011,9 @@ pub fn build_session_(sopts: config::Options,
features: RefCell::new(None),
recursion_limit: Cell::new(64),
type_length_limit: Cell::new(1048576),
const_eval_stack_frame_limit: Cell::new(100),
const_eval_step_limit: Cell::new(1_000_000),
const_eval_memory_limit: Cell::new(100 * 1024 * 1024), // 100 MB
next_node_id: Cell::new(NodeId::new(1)),
injected_allocator: Cell::new(None),
allocator_kind: Cell::new(None),

View File

@ -24,8 +24,7 @@ pub fn mk_borrowck_eval_cx<'a, 'mir, 'tcx>(
) -> EvalResult<'tcx, EvalContext<'a, 'mir, 'tcx, CompileTimeEvaluator>> {
debug!("mk_borrowck_eval_cx: {:?}", instance);
let param_env = tcx.param_env(instance.def_id());
let limits = super::ResourceLimits::default();
let mut ecx = EvalContext::new(tcx, param_env, limits, CompileTimeEvaluator, ());
let mut ecx = EvalContext::new(tcx, param_env, CompileTimeEvaluator, ());
// insert a stack frame so any queries have the correct substs
ecx.push_stack_frame(
instance,
@ -43,8 +42,7 @@ pub fn mk_eval_cx<'a, 'tcx>(
param_env: ty::ParamEnv<'tcx>,
) -> EvalResult<'tcx, EvalContext<'a, 'tcx, 'tcx, CompileTimeEvaluator>> {
debug!("mk_eval_cx: {:?}, {:?}", instance, param_env);
let limits = super::ResourceLimits::default();
let mut ecx = EvalContext::new(tcx, param_env, limits, CompileTimeEvaluator, ());
let mut ecx = EvalContext::new(tcx, param_env, CompileTimeEvaluator, ());
let mir = ecx.load_mir(instance.def)?;
// insert a stack frame so any queries have the correct substs
ecx.push_stack_frame(
@ -95,8 +93,7 @@ fn eval_body_and_ecx<'a, 'mir, 'tcx>(
param_env: ty::ParamEnv<'tcx>,
) -> (EvalResult<'tcx, (Value, Pointer, Ty<'tcx>)>, EvalContext<'a, 'mir, 'tcx, CompileTimeEvaluator>) {
debug!("eval_body: {:?}, {:?}", cid, param_env);
let limits = super::ResourceLimits::default();
let mut ecx = EvalContext::new(tcx, param_env, limits, CompileTimeEvaluator, ());
let mut ecx = EvalContext::new(tcx, param_env, CompileTimeEvaluator, ());
let res = (|| {
let mut mir = match mir {
Some(mir) => mir,

View File

@ -43,7 +43,7 @@ pub struct EvalContext<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> {
/// The maximum number of operations that may be executed.
/// This prevents infinite loops and huge computations from freezing up const eval.
/// Remove once halting problem is solved.
pub(crate) steps_remaining: u64,
pub(crate) steps_remaining: usize,
}
/// A stack frame.
@ -102,23 +102,6 @@ pub enum StackPopCleanup {
None,
}
#[derive(Copy, Clone, Debug)]
pub struct ResourceLimits {
pub memory_size: u64,
pub step_limit: u64,
pub stack_limit: usize,
}
impl Default for ResourceLimits {
fn default() -> Self {
ResourceLimits {
memory_size: 100 * 1024 * 1024, // 100 MB
step_limit: 1_000_000,
stack_limit: 100,
}
}
}
#[derive(Copy, Clone, Debug)]
pub struct TyAndPacked<'tcx> {
pub ty: Ty<'tcx>,
@ -200,7 +183,6 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
pub fn new(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
param_env: ty::ParamEnv<'tcx>,
limits: ResourceLimits,
machine: M,
memory_data: M::MemoryData,
) -> Self {
@ -208,10 +190,10 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
machine,
tcx,
param_env,
memory: Memory::new(tcx, limits.memory_size, memory_data),
memory: Memory::new(tcx, memory_data),
stack: Vec::new(),
stack_limit: limits.stack_limit,
steps_remaining: limits.step_limit,
stack_limit: tcx.sess.const_eval_stack_frame_limit.get(),
steps_remaining: tcx.sess.const_eval_step_limit.get(),
}
}
@ -559,7 +541,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
}
Aggregate(ref kind, ref operands) => {
self.inc_step_counter_and_check_limit(operands.len() as u64)?;
self.inc_step_counter_and_check_limit(operands.len())?;
let (dest, active_field_index) = match **kind {
mir::AggregateKind::Adt(adt_def, variant_index, _, active_field_index) => {

View File

@ -55,14 +55,14 @@ pub struct Memory<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> {
}
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>, max_memory: u64, data: M::MemoryData) -> Self {
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>, data: M::MemoryData) -> Self {
Memory {
data,
alloc_kind: HashMap::new(),
alloc_map: HashMap::new(),
uninitialized_statics: HashMap::new(),
tcx,
memory_size: max_memory,
memory_size: tcx.sess.const_eval_memory_limit.get(),
memory_usage: 0,
cur_frame: usize::max_value(),
}

View File

@ -11,7 +11,7 @@ mod step;
mod terminator;
mod traits;
pub use self::eval_context::{EvalContext, Frame, ResourceLimits, StackPopCleanup,
pub use self::eval_context::{EvalContext, Frame, StackPopCleanup,
TyAndPacked, ValTy};
pub use self::place::{Place, PlaceExtra};

View File

@ -8,7 +8,7 @@ use rustc::mir::interpret::EvalResult;
use super::{EvalContext, Machine};
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
pub fn inc_step_counter_and_check_limit(&mut self, n: u64) -> EvalResult<'tcx> {
pub fn inc_step_counter_and_check_limit(&mut self, n: usize) -> EvalResult<'tcx> {
self.steps_remaining = self.steps_remaining.saturating_sub(n);
if self.steps_remaining > 0 {
Ok(())