Skip to main content

miden_processor/fast/
mod.rs

1use alloc::{boxed::Box, sync::Arc, vec, vec::Vec};
2use core::{cmp::min, ops::ControlFlow};
3
4use miden_air::{Felt, trace::RowIndex};
5use miden_core::{
6    EMPTY_WORD, WORD_SIZE, Word, ZERO,
7    deferred::DeferredState,
8    mast::{ExecutableMastForest, MastForest},
9    program::{MIN_STACK_DEPTH, Program, StackInputs, StackOutputs},
10    utils::range,
11};
12use miden_mast_package::Package;
13
14use crate::{
15    AdviceInputs, AdviceProvider, ContextId, ExecutionError, ExecutionOptions, ProcessorState,
16    advice::AdviceError,
17    continuation_stack::{Continuation, ContinuationStack},
18    errors::MapExecErrNoCtx,
19    tracer::{OperationHelperRegisters, Tracer},
20};
21
22mod basic_block;
23mod execution_api;
24mod external;
25mod memory;
26mod operation;
27mod step;
28
29pub use basic_block::SystemEventError;
30pub use memory::Memory;
31pub use step::{BreakReason, ResumeContext};
32
33#[cfg(test)]
34mod tests;
35
36// CONSTANTS
37// ================================================================================================
38
39/// The initial size of the stack buffer.
40///
41/// Note: This value is much larger than it needs to be for the majority of programs. However, some
42/// existing programs need it, so we're forced to push it up (though this should be double-checked).
43/// At this high a value, we're starting to see some performance degradation on benchmarks. For
44/// example, the blake3 benchmark went from 285 MHz to 250 MHz (~10% degradation). Perhaps a better
45/// solution would be to make this value much smaller (~1000), and then fallback to a `Vec` if the
46/// stack overflows.
47const INITIAL_STACK_BUFFER_SIZE: usize = 6850;
48
49/// The initial position of the top of the stack in the stack buffer.
50///
51/// We place this value close to 0 because if a program hits the limit, it's much more likely to hit
52/// the upper bound than the lower bound, since hitting the lower bound only occurs when you drop
53/// 0's that were generated automatically to keep the stack depth at 16. In practice, if this
54/// occurs, it is most likely a bug.
55const INITIAL_STACK_TOP_IDX: usize = 250;
56
57/// Default maximum operand stack depth preserving the previous fixed-buffer ceiling.
58const DEFAULT_MAX_STACK_DEPTH: usize =
59    INITIAL_STACK_BUFFER_SIZE - INITIAL_STACK_TOP_IDX - 1 + MIN_STACK_DEPTH;
60
61const _: [(); 1] =
62    [(); (ExecutionOptions::DEFAULT_MAX_STACK_DEPTH == DEFAULT_MAX_STACK_DEPTH) as usize];
63
64/// The stack buffer index where the logical operand stack starts after reset/recenter.
65const STACK_BUFFER_BASE_IDX: usize = INITIAL_STACK_TOP_IDX - MIN_STACK_DEPTH;
66
67// FAST PROCESSOR
68// ================================================================================================
69
70/// A fast processor which doesn't generate any trace.
71///
72/// This processor is designed to be as fast as possible. Hence, it only keeps track of the current
73/// state of the processor (i.e. the stack, current clock cycle, current memory context, and free
74/// memory pointer).
75///
76/// # Stack Management
77/// A few key points about how the stack was designed for maximum performance:
78///
79/// - The stack starts with a fixed buffer size defined by `INITIAL_STACK_BUFFER_SIZE`.
80///     - This was observed to increase performance by at least 2x compared to using a `Vec` with
81///       `push()` & `pop()`.
82///     - We track the stack top and bottom using indices `stack_top_idx` and `stack_bot_idx`,
83///       respectively.
84/// - Since we are using a fixed-size buffer, we need to ensure that stack buffer accesses are not
85///   out of bounds. Naively, we could check for this on every access. However, every operation
86///   alters the stack depth by a predetermined amount, allowing us to precisely determine the
87///   minimum number of operations required to reach a stack buffer boundary, whether at the top or
88///   bottom.
89///     - For example, if the stack top is 10 elements away from the top boundary, and the stack
90///       bottom is 15 elements away from the bottom boundary, then we can safely execute 10
91///       operations that modify the stack depth with no bounds check.
92/// - When switching contexts (e.g., during a call or syscall), all elements past the first 16 are
93///   stored in `stack_overflow_save_stack`, and the stack is truncated to 16 elements. They will be
94///   restored when returning from the call or syscall.
95///
96/// # Clock Cycle Management
97/// - The clock cycle (`clk`) is managed in the same way as in `Process`. That is, it is incremented
98///   by 1 for every row that `Process` adds to the main trace.
99///     - It is important to do so because the clock cycle is used to determine the context ID for
100///       new execution contexts when using `call` or `dyncall`.
101#[derive(Debug)]
102pub struct FastProcessor {
103    /// The stack is stored in reverse order, so that the last element is at the top of the stack.
104    stack: Box<[Felt]>,
105    /// The index of the top of the stack.
106    stack_top_idx: usize,
107    /// The index of the bottom of the stack.
108    stack_bot_idx: usize,
109
110    /// The current clock cycle.
111    clk: RowIndex,
112
113    /// The current context ID.
114    ctx: ContextId,
115
116    /// The hash of the function that called into the current context, or `[ZERO, ZERO, ZERO,
117    /// ZERO]` if we are in the first context (i.e. when `system_call_state_stack` is empty).
118    caller_hash: Word,
119
120    /// The advice provider to be used during execution.
121    advice: AdviceProvider,
122
123    /// A map from (context_id, word_address) to the word stored starting at that memory location.
124    memory: Memory,
125
126    /// Stack of saved system state, used when starting a new execution context (from a `call`,
127    /// `syscall` or `dyncall`) to keep track of the previous `(ctx, caller_hash)` upon return.
128    /// Pushed in lockstep with `stack_overflow_save_stack`.
129    system_call_state_stack: Vec<SystemCallState>,
130
131    /// Stack of saved operand-stack overflows, used when starting a new execution context to keep
132    /// the elements that lived past the top 16 of the previous context. Pushed in lockstep with
133    /// `system_call_state_stack`.
134    stack_overflow_save_stack: Vec<Vec<Felt>>,
135
136    /// Running total of the number of field elements currently held across all suspended overflow
137    /// segments in `stack_overflow_save_stack`. Maintained in lockstep with that stack so the
138    /// aggregate operand-stack depth (active context plus all suspended overflow) can be bounded
139    /// by `ExecutionOptions::max_stack_depth()` in O(1) without summing every saved segment on
140    /// each push. See [`Self::ensure_stack_capacity_for_push`].
141    saved_overflow_len: usize,
142
143    /// Options for execution, including cycle limits, stack limits, advice map limits, and the
144    /// size of core trace fragments during execution.
145    options: ExecutionOptions,
146
147    /// Deferred witness accumulated during execution and returned for verifier rehydration.
148    deferred_state: DeferredState,
149}
150
151impl FastProcessor {
152    /// Packages the processor state after successful execution into a public result type.
153    #[inline(always)]
154    fn into_execution_output(self, stack: StackOutputs) -> ExecutionOutput {
155        ExecutionOutput {
156            stack,
157            advice: self.advice,
158            memory: self.memory,
159            deferred_state: self.deferred_state,
160        }
161    }
162
163    /// Converts the terminal result of a full execution run into [`ExecutionOutput`].
164    #[inline(always)]
165    fn execution_result_from_flow(
166        flow: ControlFlow<BreakReason<Arc<MastForest>>, StackOutputs>,
167        processor: Self,
168    ) -> Result<ExecutionOutput, ExecutionError> {
169        match flow {
170            ControlFlow::Continue(stack_outputs) => {
171                Ok(processor.into_execution_output(stack_outputs))
172            },
173            ControlFlow::Break(break_reason) => match break_reason {
174                BreakReason::Err(err) => Err(err),
175                BreakReason::Stopped(_) => {
176                    unreachable!("Execution never stops prematurely with NeverStopper")
177                },
178            },
179        }
180    }
181
182    /// Converts a testing-only execution result into stack outputs.
183    #[cfg(any(test, feature = "testing"))]
184    #[inline(always)]
185    fn stack_result_from_flow(
186        flow: ControlFlow<BreakReason<Arc<MastForest>>, StackOutputs>,
187    ) -> Result<StackOutputs, ExecutionError> {
188        match flow {
189            ControlFlow::Continue(stack_outputs) => Ok(stack_outputs),
190            ControlFlow::Break(break_reason) => match break_reason {
191                BreakReason::Err(err) => Err(err),
192                BreakReason::Stopped(_) => {
193                    unreachable!("Execution never stops prematurely with NeverStopper")
194                },
195            },
196        }
197    }
198
199    // CONSTRUCTORS
200    // ----------------------------------------------------------------------------------------------
201
202    /// Creates a new `FastProcessor` instance with the given stack inputs.
203    ///
204    /// By default, advice inputs are empty and execution options use their defaults.
205    ///
206    /// # Example
207    /// ```ignore
208    /// use miden_processor::FastProcessor;
209    ///
210    /// let processor = FastProcessor::new(stack_inputs)
211    ///     .with_advice(advice_inputs)
212    ///     .expect("advice inputs should fit advice map limits");
213    /// ```
214    ///
215    /// When using non-default advice map limits, prefer [`Self::new_with_options`] so the advice
216    /// inputs are validated against the intended execution options.
217    pub fn new(stack_inputs: StackInputs) -> Self {
218        Self::new_with_options(stack_inputs, AdviceInputs::default(), ExecutionOptions::default())
219            .expect("default processor initialization should fit default execution limits")
220    }
221
222    /// Sets the advice inputs for the processor.
223    ///
224    /// Advice inputs are loaded into the live advice provider immediately and are validated against
225    /// the processor's current [`ExecutionOptions`]. If the advice map needs non-default limits,
226    /// construct the processor with [`Self::new_with_options`] or call [`Self::with_options`]
227    /// before calling this method.
228    pub fn with_advice(mut self, advice_inputs: AdviceInputs) -> Result<Self, AdviceError> {
229        self.advice = AdviceProvider::new(advice_inputs, &self.options)?;
230        Ok(self)
231    }
232
233    /// Sets the execution options for the processor.
234    ///
235    /// Existing advice inputs are revalidated against the new options before they are applied. To
236    /// load advice inputs that require non-default advice map limits, call this before
237    /// [`Self::with_advice`] or use [`Self::new_with_options`]. The installed precompile registry
238    /// and any accumulated deferred state are preserved; only the remaining deferred-state
239    /// budget is updated to match the new options.
240    pub fn with_options(mut self, options: ExecutionOptions) -> Result<Self, AdviceError> {
241        self.advice.set_options(&options)?;
242        self.deferred_state.set_max_elements(options.max_deferred_elements());
243        self.memory.set_max_elements(options.max_memory_elements());
244        self.options = options;
245        Ok(self)
246    }
247
248    /// Constructor for creating a `FastProcessor` with all options specified at once.
249    ///
250    /// For a more fluent API, consider using `FastProcessor::new()` with builder methods.
251    pub fn new_with_options(
252        stack_inputs: StackInputs,
253        advice_inputs: AdviceInputs,
254        options: ExecutionOptions,
255    ) -> Result<Self, AdviceError> {
256        let stack_top_idx = INITIAL_STACK_TOP_IDX;
257        let stack = {
258            // Note: we use `Vec::into_boxed_slice()` here, since `Box::new([T; N])` first allocates
259            // the array on the stack, and then moves it to the heap. This might cause a
260            // stack overflow on some systems.
261            let mut stack = vec![ZERO; INITIAL_STACK_BUFFER_SIZE].into_boxed_slice();
262
263            // Copy inputs in reverse order so first element ends up at top of stack
264            for (i, &input) in stack_inputs.iter().enumerate() {
265                stack[stack_top_idx - 1 - i] = input;
266            }
267            stack
268        };
269
270        Ok(Self {
271            advice: AdviceProvider::new(advice_inputs, &options)?,
272            stack,
273            stack_top_idx,
274            stack_bot_idx: stack_top_idx - MIN_STACK_DEPTH,
275            clk: 0_u32.into(),
276            ctx: 0_u32.into(),
277            caller_hash: EMPTY_WORD,
278            memory: Memory::new(options.max_memory_elements()),
279            system_call_state_stack: Vec::new(),
280            stack_overflow_save_stack: Vec::new(),
281            saved_overflow_len: 0,
282            deferred_state: DeferredState::new(
283                Arc::new(miden_precompiles::registry()),
284                options.max_deferred_elements(),
285            )
286            .map_err(AdviceError::DeferredStateInitializationFailed)?,
287            options,
288        })
289    }
290
291    /// Returns the resume context to be used with the first call to `step_sync()`.
292    ///
293    /// This function asserts that `package` is of executable type - callers should ensure that it
294    /// is before calling.
295    pub fn get_initial_resume_context_for_package(
296        &mut self,
297        package: Arc<Package>,
298    ) -> Result<ResumeContext, ExecutionError> {
299        let program = package.unwrap_program();
300        let package_debug_info = package.debug_info()?.map(Arc::new);
301        let current_forest = program.mast_forest().clone();
302        self.advice.extend_map(current_forest.advice_map()).map_exec_err_no_ctx()?;
303
304        let entrypoint_source_node_id = package.entrypoint_source_node();
305        let continuation_stack = if let Some(debug_info) = package_debug_info.as_deref() {
306            Self::source_aware_continuation_stack(&program, debug_info, entrypoint_source_node_id)?
307        } else {
308            ContinuationStack::new(&program)
309        };
310
311        Ok(ResumeContext {
312            current_forest,
313            continuation_stack,
314            kernel: program.kernel().clone(),
315            package_debug_info,
316        })
317    }
318
319    /// Returns the resume context to be used with the first call to `step_sync()`.
320    pub fn get_initial_resume_context(
321        &mut self,
322        program: &Program,
323    ) -> Result<ResumeContext, ExecutionError> {
324        self.advice
325            .extend_map(program.mast_forest().advice_map())
326            .map_exec_err_no_ctx()?;
327
328        Ok(ResumeContext {
329            current_forest: program.mast_forest().clone(),
330            continuation_stack: ContinuationStack::new(program),
331            kernel: program.kernel().clone(),
332            package_debug_info: None,
333        })
334    }
335
336    // ACCESSORS
337    // -------------------------------------------------------------------------------------------
338
339    /// Returns the deferred witness accumulated during execution.
340    #[inline(always)]
341    pub fn deferred_state(&self) -> &DeferredState {
342        &self.deferred_state
343    }
344
345    #[inline(always)]
346    pub(super) fn deferred_state_mut(&mut self) -> &mut DeferredState {
347        &mut self.deferred_state
348    }
349
350    /// Returns the size of the stack.
351    #[inline(always)]
352    fn stack_size(&self) -> usize {
353        self.stack_top_idx - self.stack_bot_idx
354    }
355
356    /// Returns the stack, such that the top of the stack is at the last index of the returned
357    /// slice.
358    pub fn stack(&self) -> &[Felt] {
359        &self.stack[self.stack_bot_idx..self.stack_top_idx]
360    }
361
362    /// Returns the top 16 elements of the stack.
363    pub fn stack_top(&self) -> &[Felt] {
364        &self.stack[self.stack_top_idx - MIN_STACK_DEPTH..self.stack_top_idx]
365    }
366
367    /// Returns a mutable reference to the top 16 elements of the stack.
368    pub fn stack_top_mut(&mut self) -> &mut [Felt] {
369        &mut self.stack[self.stack_top_idx - MIN_STACK_DEPTH..self.stack_top_idx]
370    }
371
372    /// Returns the element on the stack at index `idx`.
373    ///
374    /// This method is only meant to be used to access the stack top by operation handlers, and
375    /// system event handlers.
376    ///
377    /// # Preconditions
378    /// - `idx` must be less than or equal to 15.
379    #[inline(always)]
380    pub fn stack_get(&self, idx: usize) -> Felt {
381        self.stack[self.stack_top_idx - idx - 1]
382    }
383
384    /// Same as [`Self::stack_get()`], but returns [`ZERO`] if `idx` falls below index 0 in the
385    /// stack buffer.
386    ///
387    /// Use this instead of `stack_get()` when `idx` may exceed 15.
388    #[inline(always)]
389    pub fn stack_get_safe(&self, idx: usize) -> Felt {
390        if idx < self.stack_top_idx {
391            self.stack[self.stack_top_idx - idx - 1]
392        } else {
393            ZERO
394        }
395    }
396
397    /// Mutable variant of `stack_get()`.
398    ///
399    /// This method is only meant to be used to access the stack top by operation handlers, and
400    /// system event handlers.
401    ///
402    /// # Preconditions
403    /// - `idx` must be less than or equal to 15.
404    #[inline(always)]
405    pub fn stack_get_mut(&mut self, idx: usize) -> &mut Felt {
406        &mut self.stack[self.stack_top_idx - idx - 1]
407    }
408
409    /// Returns the word on the stack starting at index `start_idx` in "stack order".
410    ///
411    /// For `start_idx=0` the top element of the stack will be at position 0 in the word.
412    ///
413    /// For example, if the stack looks like this:
414    ///
415    /// top                                                       bottom
416    /// v                                                           v
417    /// a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p
418    ///
419    /// Then
420    /// - `stack_get_word(0)` returns `[a, b, c, d]`,
421    /// - `stack_get_word(1)` returns `[b, c, d, e]`,
422    /// - etc.
423    ///
424    /// This method is only meant to be used to access the stack top by operation handlers, and
425    /// system event handlers.
426    ///
427    /// # Preconditions
428    /// - `start_idx` must be less than or equal to 12.
429    #[inline(always)]
430    pub fn stack_get_word(&self, start_idx: usize) -> Word {
431        // Ensure we have enough elements to form a complete word
432        debug_assert!(
433            start_idx + WORD_SIZE <= self.stack_depth() as usize,
434            "Not enough elements on stack to read word starting at index {start_idx}"
435        );
436
437        let word_start_idx = self.stack_top_idx - start_idx - WORD_SIZE;
438        let mut result: [Felt; WORD_SIZE] =
439            self.stack[range(word_start_idx, WORD_SIZE)].try_into().unwrap();
440        // Reverse so top of stack (idx 0) goes to word[0]
441        result.reverse();
442        result.into()
443    }
444
445    /// Same as [`Self::stack_get_word()`], but returns [`ZERO`] for any element that falls below
446    /// index 0 in the stack buffer.
447    ///
448    /// Use this instead of `stack_get_word()` when `start_idx + WORD_SIZE` may exceed
449    /// `stack_top_idx`.
450    #[inline(always)]
451    pub fn stack_get_word_safe(&self, start_idx: usize) -> Word {
452        let buf_end = self.stack_top_idx.saturating_sub(start_idx);
453        let buf_start = self.stack_top_idx.saturating_sub(start_idx.saturating_add(WORD_SIZE));
454        let num_elements_to_read_from_buf = buf_end - buf_start;
455
456        let mut result = [ZERO; WORD_SIZE];
457        if num_elements_to_read_from_buf == WORD_SIZE {
458            result.copy_from_slice(&self.stack[range(buf_start, WORD_SIZE)]);
459        } else if num_elements_to_read_from_buf > 0 {
460            let offset = WORD_SIZE - num_elements_to_read_from_buf;
461            result[offset..]
462                .copy_from_slice(&self.stack[range(buf_start, num_elements_to_read_from_buf)]);
463        }
464        result.reverse();
465
466        result.into()
467    }
468
469    /// Returns the number of elements on the stack in the current context.
470    #[inline(always)]
471    pub fn stack_depth(&self) -> u32 {
472        (self.stack_top_idx - self.stack_bot_idx) as u32
473    }
474
475    /// Returns a reference to the processor's memory.
476    pub fn memory(&self) -> &Memory {
477        &self.memory
478    }
479
480    /// Consumes the processor and returns the advice provider and memory.
481    pub fn into_parts(self) -> (AdviceProvider, Memory) {
482        (self.advice, self.memory)
483    }
484
485    /// Returns a reference to the execution options.
486    pub fn execution_options(&self) -> &ExecutionOptions {
487        &self.options
488    }
489
490    /// Returns a narrowed interface for reading and updating the processor state.
491    #[inline(always)]
492    pub fn state(&self) -> ProcessorState<'_> {
493        ProcessorState { processor: self }
494    }
495
496    // MUTATORS
497    // -------------------------------------------------------------------------------------------
498
499    /// Writes an element to the stack at the given index.
500    #[inline(always)]
501    pub fn stack_write(&mut self, idx: usize, element: Felt) {
502        self.stack[self.stack_top_idx - idx - 1] = element
503    }
504
505    /// Writes a word to the stack starting at the given index.
506    ///
507    /// `word[0]` goes to stack position start_idx (top), `word[1]` to start_idx+1, etc.
508    #[inline(always)]
509    pub fn stack_write_word(&mut self, start_idx: usize, word: &Word) {
510        debug_assert!(start_idx <= MIN_STACK_DEPTH - WORD_SIZE);
511
512        let word_start_idx = self.stack_top_idx - start_idx - 4;
513        let mut source: [Felt; WORD_SIZE] = (*word).into();
514        // Reverse so word[0] ends up at the top of stack (highest internal index)
515        source.reverse();
516        self.stack[range(word_start_idx, WORD_SIZE)].copy_from_slice(&source)
517    }
518
519    /// Swaps the elements at the given indices on the stack.
520    #[inline(always)]
521    pub fn stack_swap(&mut self, idx1: usize, idx2: usize) {
522        let a = self.stack_get(idx1);
523        let b = self.stack_get(idx2);
524        self.stack_write(idx1, b);
525        self.stack_write(idx2, a);
526    }
527
528    /// Increments the stack top pointer by 1.
529    ///
530    /// The bottom of the stack is never affected by this operation.
531    #[inline(always)]
532    fn increment_stack_size(&mut self) {
533        self.stack_top_idx += 1;
534    }
535
536    /// Ensures the internal stack storage can accommodate one additional logical stack element.
537    ///
538    /// The operand stack depth limit is the semantic resource bound; the buffer is only an
539    /// implementation detail. We therefore check the logical depth before allocating so a program
540    /// cannot force memory growth beyond `ExecutionOptions::max_stack_depth()`. When storage does
541    /// need to grow, it grows geometrically and remains heap-allocated as a boxed slice. A
542    /// `SmallVec` would put a useful inline buffer inside `FastProcessor`, and preallocating the
543    /// full limit would penalize ordinary programs. This policy is performance-sensitive and should
544    /// be benchmarked against the fixed-buffer baseline.
545    ///
546    /// The depth that is checked is the *aggregate* operand-stack depth: the active context's depth
547    /// plus every element held in suspended overflow segments (`saved_overflow_len`). A `call`,
548    /// `dyncall`, or `syscall` context switch hides the caller's overflow in
549    /// `stack_overflow_save_stack` rather than freeing it, so checking only the active context
550    /// would let a program nest context switches to accumulate `O(call_depth *
551    /// max_stack_depth)` hidden operand-stack memory while every live frame stayed within the
552    /// limit. Because a context switch merely moves elements between the active stack and the
553    /// saved overflow (it never creates elements), the aggregate is conserved across switches
554    /// and only grows on a push, so enforcing the bound here is sufficient to cap total
555    /// operand-stack memory.
556    #[inline(always)]
557    fn ensure_stack_capacity_for_push(&mut self) -> Result<(), ExecutionError> {
558        let depth = self.stack_size() + self.saved_overflow_len + 1;
559        let max = self.options.max_stack_depth();
560        if depth > max {
561            return Err(ExecutionError::StackDepthLimitExceeded { depth, max });
562        }
563
564        if self.stack_top_idx >= self.stack.len() - 1 {
565            self.grow_stack_buffer(self.stack_top_idx + 2);
566        }
567
568        Ok(())
569    }
570
571    fn ensure_stack_capacity_for_top_idx(&mut self, top_idx: usize) {
572        if top_idx >= self.stack.len() {
573            self.grow_stack_buffer(top_idx + 1);
574        }
575    }
576
577    fn grow_stack_buffer(&mut self, requested_min_len: usize) {
578        // The maximum allocation is tied to the logical operand stack depth, not to the current
579        // buffer position. Using `stack_bot_idx` here would make the allocation ceiling drift when
580        // the live stack has moved away from the initial base.
581        let max_len = STACK_BUFFER_BASE_IDX
582            .saturating_add(self.options.max_stack_depth())
583            .saturating_add(1);
584        let live_len = self.stack_size();
585
586        // Growth also recenters the live stack at the normal base. This keeps future push/drop
587        // behavior close to the fixed-buffer layout and avoids carrying unused prefix cells into
588        // the new allocation. The extra slot is for the next checked push that triggered growth.
589        let recentered_min_len = STACK_BUFFER_BASE_IDX.saturating_add(live_len).saturating_add(2);
590        debug_assert!(recentered_min_len <= max_len);
591
592        // Allocation growth is based on the stack's post-recentered live range, not the previous
593        // buffer length. The `requested_min_len` may be beyond the allocation cap when a shallow
594        // context is still positioned near the end of the old buffer; recentering the live stack is
595        // what makes that valid. The VM-visible requirements are that the live stack is restored at
596        // `STACK_BUFFER_BASE_IDX`, the post-recentered push slot is available, and allocation stays
597        // capped by the configured stack depth. The allocation size can differ from the previous
598        // doubling policy: normal push growth may allocate a couple of extra cells because of the
599        // spare push slot, while restoring a deep caller from a shallow callee may allocate only
600        // the requested restored range instead of doubling the old buffer. That smaller
601        // restore allocation is intentional, but it means future pushes can grow again
602        // sooner and should stay covered by benchmarks.
603        let new_len = recentered_min_len.saturating_mul(2).max(requested_min_len).min(max_len);
604        debug_assert!(new_len <= max_len);
605
606        let mut new_stack = vec![ZERO; new_len].into_boxed_slice();
607        let new_stack_bot_idx = STACK_BUFFER_BASE_IDX;
608        let new_stack_top_idx = new_stack_bot_idx + live_len;
609
610        // Only the active stack range carries VM state. Prefix/suffix cells are scratch storage and
611        // stay zeroed, which keeps growth proportional to the live depth instead of the old buffer
612        // length.
613        new_stack[new_stack_bot_idx..new_stack_top_idx]
614            .copy_from_slice(&self.stack[self.stack_bot_idx..self.stack_top_idx]);
615
616        self.stack = new_stack;
617        self.stack_bot_idx = new_stack_bot_idx;
618        self.stack_top_idx = new_stack_top_idx;
619    }
620
621    /// Decrements the stack top pointer by 1.
622    ///
623    /// The bottom of the stack is only decremented in cases where the stack depth would become less
624    /// than 16.
625    #[inline(always)]
626    fn decrement_stack_size(&mut self) {
627        if self.stack_top_idx == MIN_STACK_DEPTH {
628            // We no longer have any room in the stack buffer to decrement the stack size (which
629            // would cause the `stack_bot_idx` to go below 0). We therefore reset the stack to its
630            // original position.
631            self.reset_stack_in_buffer(INITIAL_STACK_TOP_IDX);
632        }
633
634        self.stack_top_idx -= 1;
635        self.stack_bot_idx = min(self.stack_bot_idx, self.stack_top_idx - MIN_STACK_DEPTH);
636    }
637
638    /// Resets the stack in the buffer to a new position, preserving the top 16 elements of the
639    /// stack.
640    ///
641    /// # Preconditions
642    /// - The stack is expected to have exactly 16 elements.
643    #[inline(always)]
644    fn reset_stack_in_buffer(&mut self, new_stack_top_idx: usize) {
645        debug_assert_eq!(self.stack_depth(), MIN_STACK_DEPTH as u32);
646
647        let new_stack_bot_idx = new_stack_top_idx - MIN_STACK_DEPTH;
648
649        // Copy stack to its new position
650        self.stack
651            .copy_within(self.stack_bot_idx..self.stack_top_idx, new_stack_bot_idx);
652
653        // Zero out stack below the new new_stack_bot_idx, since this is where overflow values
654        // come from, and are guaranteed to be ZERO. We don't need to zero out above
655        // `stack_top_idx`, since values there are never read before being written.
656        self.stack[0..new_stack_bot_idx].fill(ZERO);
657
658        // Update indices.
659        self.stack_bot_idx = new_stack_bot_idx;
660        self.stack_top_idx = new_stack_top_idx;
661    }
662}
663
664// EXECUTION OUTPUT
665// ===============================================================================================
666
667/// The output of a program execution, containing the state of the stack, advice provider, memory,
668/// and final deferred state at the end of execution.
669#[derive(Debug)]
670pub struct ExecutionOutput {
671    pub stack: StackOutputs,
672    pub advice: AdviceProvider,
673    pub memory: Memory,
674    pub deferred_state: DeferredState,
675}
676
677// SYSTEM CALL STATE
678// ===============================================================================================
679
680/// The system-state half of a saved execution context.
681///
682/// Used to keep track of the `(ctx, caller_hash)` pair that needs to be restored upon return from a
683/// `call`, `syscall` or `dyncall`.
684#[derive(Debug)]
685pub(super) struct SystemCallState {
686    pub ctx: ContextId,
687    pub caller_hash: Word,
688}
689
690// NOOP TRACER
691// ================================================================================================
692
693/// A [Tracer] that does nothing.
694pub struct NoopTracer;
695
696impl Tracer for NoopTracer {
697    type Processor = FastProcessor;
698    type Forest = Arc<MastForest>;
699
700    #[inline(always)]
701    fn start_clock_cycle(
702        &mut self,
703        _processor: &FastProcessor,
704        _continuation: Continuation<Arc<MastForest>>,
705        _continuation_stack: &ContinuationStack<Arc<MastForest>>,
706        _current_forest: &Arc<MastForest>,
707    ) {
708        // do nothing
709    }
710
711    #[inline(always)]
712    fn finalize_clock_cycle(
713        &mut self,
714        _processor: &FastProcessor,
715        _op_helper_registers: OperationHelperRegisters,
716        _current_forest: &Arc<MastForest>,
717    ) -> Result<(), ExecutionError> {
718        // do nothing
719        Ok(())
720    }
721}