Skip to main content

Module cycle_guard

Module cycle_guard 

Source
Expand description

Windowed cycle-detection infinite-loop guard.

A general defense layer that complements the coarse size/count limits (Gullet::token_limit / pushback_limit) and the outermost RSS soft cap (stomach::check_timeout). Where those catch a runaway only after it has consumed millions of tokens or gigabytes of RSS, this guard spots the structure of a loop directly: a short window of items that repeats many times back-to-back.

Two instances run at different kernel levels (the project’s layered-guard philosophy): one over the gullet’s expansion stream (token fingerprints), one over the stomach’s accumulated digest list (box fingerprints). Either can terminate a runaway with a clean Fatal long before the RSS cap.

Algorithm (per the design directive): record a stream of u64 fingerprints in a fixed ring buffer. Periodically check whether the most recent items are periodic with some period W in 1..=MAX_WINDOW, repeated at least REPEAT times. The check is pure periodicity over the last W*REPEAT items (item[i] == item[i-W]), so it is phase/offset independent — the cycle need not align to any buffer boundary. The smallest matching period is reported.

Cost: detection is throttled to once per CHECK_EVERY pushes. Each period’s scan stops at its first mismatch, so a non-periodic stream costs about MAX_WINDOW compares per check; the windows a uniform tail rules out are skipped unscanned (CycleGuard::detect), so a long run of one item, which matches every period to the end, costs MAX_WINDOW compares too, not MAX_WINDOW^2 * REPEAT / 2. Callers further gate activation on an already-high item count so normal conversions pay nothing.

Structs§

CycleGuard
A windowed cycle detector over a stream of u64 fingerprints.
Persistence
How long a detected period has held, on the caller’s clock (boxes recorded, tokens read): from the first check that saw it, through every later check, to now. A check that sees none ends the run. The stomach’s guard Fatals on a period only once it has held for a bounded stretch (OD #538), so repeated data or a counter loop that ends inside the hold is no loop; one that runs past it still reads as one.
SpanHistory
The recent token spans behind the stomach’s boxes: how many tokens the gullet read for each box the stomach’s CycleGuard records. A box period alone cannot tell a loop from a long run over data: every point of a pgf plot[smooth] digests the same content-free boxes (tkz-grapheur’s 11,000-point plot read as a 6-box cycle). A loop repeats the same boxes from the same input; a run over data repeats them from input of varying length. So a detected box period is a loop only when the spans repeat too, with a period that is a multiple of the box period (Self::input_period).

Constants§

MAX_SPAN_PERIOD
The largest input period, in boxes, SpanHistory::input_period looks for: eight repetitions fill the ring.
MAX_WINDOW
Largest cycle period (in items) we look for. 32 catches a loop through a dozen-odd macros with their arguments (apacite’s \citeA/natbib name-call loop, 1909.02387, 2212.08776, ran to the 180 s wall clock at 10).
REPEAT
How many consecutive repetitions of a window constitute “infinite”.

Functions§

note_input_line
Fold the number of the line a \read/\readline on stream has just read into the next token’s fingerprint and the next box’s spans, with and without the register values: a loop over a file’s lines reads input that advances, as the Mouth’s does (OD #538), and is finite however equal the lines are. A read past the end is no line read.
note_value
Fold a register value just read (\ifnum\n<…, \advance\n) into the next token’s fingerprint: a loop whose tokens repeat but whose counters move is making progress, so it is no cycle. A token-list or pair value is no number and is skipped.
reset_value_salt
Clear every fold, at the start of a conversion: a reused thread (cortex_worker) must fingerprint the same document the same way.
take_input_salt
The lines folded by note_input_line alone since the stomach’s last call, cleared.
take_span_salt
The values folded by note_value and note_input_line since the stomach’s last call (SpanHistory::push), cleared.
take_value_salt
The values folded by note_value and note_input_line since the gullet’s last call, cleared.