Skip to content

Commit b3b04b8

Browse files
Avoid overflow in step counter
This removes the `usize` argument to `inc_step_counter`. Now, the step counter increments by exactly one for every terminator evaluated. After `STEPS_UNTIL_DETECTOR_ENABLED` steps elapse, the detector is run every `DETECTOR_SNAPSHOT_PERIOD` steps. The step counter is only kept modulo this period.
1 parent 647ba29 commit b3b04b8

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

src/librustc_mir/interpret/eval_context.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ pub struct EvalContext<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> {
4343
/// The maximum number of stack frames allowed
4444
pub(crate) stack_limit: usize,
4545

46-
/// The number of terminators to be evaluated before enabling the infinite
47-
/// loop detector.
48-
pub(crate) steps_until_detector_enabled: isize,
46+
/// When this value is negative, it indicates the number of interpreter
47+
/// steps *until* the loop detector is enabled. When it is positive, it is
48+
/// the number of steps after the detector has been enabled modulo the loop
49+
/// detector period.
50+
pub(crate) steps_since_detector_enabled: isize,
4951

5052
pub(crate) loop_detector: InfiniteLoopDetector<'a, 'mir, 'tcx, M>,
5153
}
@@ -148,14 +150,15 @@ type EvalSnapshot<'a, 'mir, 'tcx, M>
148150
pub(crate) struct InfiniteLoopDetector<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> {
149151
/// The set of all `EvalSnapshot` *hashes* observed by this detector.
150152
///
151-
/// When a collision occurs in this table, we store the full snapshot in `snapshots`.
153+
/// When a collision occurs in this table, we store the full snapshot in
154+
/// `snapshots`.
152155
hashes: FxHashSet<u64>,
153156

154157
/// The set of all `EvalSnapshot`s observed by this detector.
155158
///
156-
/// An `EvalSnapshot` will only be fully cloned once it has caused a collision in `hashes`. As
157-
/// a result, the detector must observe at least *two* full cycles of an infinite loop before
158-
/// it triggers.
159+
/// An `EvalSnapshot` will only be fully cloned once it has caused a
160+
/// collision in `hashes`. As a result, the detector must observe at least
161+
/// *two* full cycles of an infinite loop before it triggers.
159162
snapshots: FxHashSet<EvalSnapshot<'a, 'mir, 'tcx, M>>,
160163
}
161164

@@ -291,7 +294,7 @@ impl<'c, 'b, 'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> LayoutOf
291294
}
292295
}
293296

294-
const MAX_TERMINATORS: isize = 1_000_000;
297+
const STEPS_UNTIL_DETECTOR_ENABLED: isize = 1_000_000;
295298

296299
impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
297300
pub fn new(
@@ -310,16 +313,16 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
310313
stack: Vec::new(),
311314
stack_limit: tcx.sess.const_eval_stack_frame_limit,
312315
loop_detector: Default::default(),
313-
steps_until_detector_enabled: MAX_TERMINATORS,
316+
steps_since_detector_enabled: -STEPS_UNTIL_DETECTOR_ENABLED,
314317
}
315318
}
316319

317320
pub(crate) fn with_fresh_body<F: FnOnce(&mut Self) -> R, R>(&mut self, f: F) -> R {
318321
let stack = mem::replace(&mut self.stack, Vec::new());
319-
let steps = mem::replace(&mut self.steps_until_detector_enabled, MAX_TERMINATORS);
322+
let steps = mem::replace(&mut self.steps_since_detector_enabled, -STEPS_UNTIL_DETECTOR_ENABLED);
320323
let r = f(self);
321324
self.stack = stack;
322-
self.steps_until_detector_enabled = steps;
325+
self.steps_since_detector_enabled = steps;
323326
r
324327
}
325328

@@ -661,8 +664,6 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
661664
}
662665

663666
Aggregate(ref kind, ref operands) => {
664-
self.inc_step_counter_and_detect_loops(operands.len())?;
665-
666667
let (dest, active_field_index) = match **kind {
667668
mir::AggregateKind::Adt(adt_def, variant_index, _, active_field_index) => {
668669
self.write_discriminant_value(dest_ty, dest, variant_index)?;

src/librustc_mir/interpret/step.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ use super::{EvalContext, Machine};
1212
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
1313
where M: Clone + Eq + Hash,
1414
{
15-
/// Returns `true` if the loop detector should take a snapshot during the current step.
16-
pub fn is_loop_detector_scheduled(&self) -> bool {
15+
pub fn inc_step_counter_and_detect_loops(&mut self) -> EvalResult<'tcx, ()> {
1716
/// The number of steps between loop detector snapshots.
1817
/// Should be a power of two for performance reasons.
19-
const DETECTOR_SNAPSHOT_PERIOD: isize = 1 << 8;
18+
const DETECTOR_SNAPSHOT_PERIOD: isize = 256;
2019

21-
let steps = self.steps_until_detector_enabled;
22-
steps <= 0 && steps % DETECTOR_SNAPSHOT_PERIOD == 0
23-
}
20+
{
21+
let steps = &mut self.steps_since_detector_enabled;
2422

25-
pub fn inc_step_counter_and_detect_loops(&mut self, n: usize) -> EvalResult<'tcx, ()> {
26-
// TODO: Remove `as` cast
27-
self.steps_until_detector_enabled =
28-
self.steps_until_detector_enabled.saturating_sub(n as isize);
23+
*steps += 1;
24+
if *steps < 0 {
25+
return Ok(());
26+
}
2927

30-
if !self.is_loop_detector_scheduled() {
31-
return Ok(());
28+
*steps %= DETECTOR_SNAPSHOT_PERIOD;
29+
if *steps != 0 {
30+
return Ok(());
31+
}
3232
}
3333

3434
if self.loop_detector.is_empty() {
@@ -61,7 +61,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
6161
return Ok(true);
6262
}
6363

64-
self.inc_step_counter_and_detect_loops(1)?;
64+
self.inc_step_counter_and_detect_loops()?;
6565

6666
let terminator = basic_block.terminator();
6767
assert_eq!(old_frames, self.cur_frame());

0 commit comments

Comments
 (0)