]> git.lizzy.rs Git - rust.git/commitdiff
Move the resource limits to the session in preparation for attributes configuring...
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 30 Jan 2018 10:18:37 +0000 (11:18 +0100)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Thu, 8 Mar 2018 07:34:15 +0000 (08:34 +0100)
src/librustc/session/mod.rs
src/librustc_mir/interpret/const_eval.rs
src/librustc_mir/interpret/eval_context.rs
src/librustc_mir/interpret/memory.rs
src/librustc_mir/interpret/mod.rs
src/librustc_mir/interpret/step.rs

index 5e9eeb973007f26f71fc0c366d1d4cdad6e6a1e8..232344a036783a9eeeb5e129f78c8f486546a8c9 100644 (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),
index c2f9690b9f19604094a064e82e1697a5951001a7..e216f5c4d8a8218c8803f93cb3680b58dac85f8a 100644 (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,
index 71450f4266576e82c6adcb62a8b867da0c796e20..676f99ea6be0c19526954dcd52c100b877017c34 100644 (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 @@ pub fn new(
             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 @@ pub(super) fn eval_rvalue_into_place(
             }
 
             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) => {
index e67c36779111b4bbcf23c64434218445c5c650cd..da8ff11e3d0a9bca728944e687426e446db18053 100644 (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(),
         }
index f23ba90fd4c3ffb0234e3a666e25e86c33c2d9e6..8e0158569a86768dbfe6c74adda391aaf5d232be 100644 (file)
@@ -11,7 +11,7 @@
 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};
index 21e81ff668ea58e26badaedfc47fd78339fffbbf..94fe3d1c67b8fc634aba5e2640a7292113bd8d6c 100644 (file)
@@ -8,7 +8,7 @@
 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(())