Skip to content

Commit 90bc97e

Browse files
feat: make max traceback entries configurable
1 parent 00e06c3 commit 90bc97e

8 files changed

Lines changed: 47 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Both branches support Stwo prover opcodes (Blake2s, QM31) since v2.0.0.
1212

1313
#### Upcoming Changes
1414

15+
* feat: make max traceback entries configurable [#2370](https://github.com/starkware-libs/cairo-vm/pull/2370)
16+
1517
* feat: create and use VirtualMachineConfig [#2369](https://github.com/starkware-libs/cairo-vm/pull/2369)
1618

1719
* feat: consolidate CairoRunner ctors [#2368](https://github.com/starkware-libs/cairo-vm/pull/2368)

cairo-vm-cli/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use cairo_vm::vm::runners::cairo_pie::CairoPie;
1515
#[cfg(feature = "with_tracer")]
1616
use cairo_vm::vm::runners::cairo_runner::CairoRunner;
1717
use cairo_vm::vm::runners::cairo_runner::RunResources;
18+
use cairo_vm::vm::vm_core::DEFAULT_MAX_TRACEBACK_ENTRIES;
1819
#[cfg(feature = "with_tracer")]
1920
use cairo_vm_tracer::error::trace_data_errors::TraceDataError;
2021
#[cfg(feature = "with_tracer")]
@@ -157,6 +158,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<(), Error> {
157158
allow_missing_builtins: args.allow_missing_builtins,
158159
dynamic_layout_params: cairo_layout_params,
159160
disable_trace_padding: false,
161+
max_traceback_entries: DEFAULT_MAX_TRACEBACK_ENTRIES,
160162
};
161163

162164
let mut cairo_runner = match if args.run_from_cairo_pie {

cairo1-run/src/cairo_run.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use cairo_lang_sierra_type_size::get_type_size_map;
3434
use cairo_lang_utils::{
3535
bigint::BigIntAsHex, casts::IntoOrPanic, unordered_hash_map::UnorderedHashMap,
3636
};
37-
use cairo_vm::vm::runners::cairo_runner::CairoRunConfig;
37+
use cairo_vm::vm::{runners::cairo_runner::CairoRunConfig, vm_core::DEFAULT_MAX_TRACEBACK_ENTRIES};
3838
use std::{collections::HashMap, iter::Peekable};
3939

4040
use cairo_vm::{
@@ -113,6 +113,7 @@ pub struct Cairo1RunConfig<'a> {
113113
/// - It still pads each builtin segment to the next power of 2 (w.r.t the number of used
114114
/// instances of the builtin) compared to their sizes at the end of the execution.
115115
pub disable_trace_padding: bool,
116+
pub max_traceback_entries: u32,
116117
}
117118

118119
impl Default for Cairo1RunConfig<'_> {
@@ -128,6 +129,7 @@ impl Default for Cairo1RunConfig<'_> {
128129
append_return_values: false,
129130
dynamic_layout_params: None,
130131
disable_trace_padding: false,
132+
max_traceback_entries: DEFAULT_MAX_TRACEBACK_ENTRIES,
131133
}
132134
}
133135
}
@@ -150,6 +152,7 @@ impl Cairo1RunConfig<'_> {
150152
self.trace_enabled,
151153
self.disable_trace_padding,
152154
runner_mode,
155+
self.max_traceback_entries,
153156
)
154157
}
155158
}

cairo1-run/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use cairo_lang_compiler::{
55
};
66
use cairo_vm::cairo_run::{write_encoded_memory, write_encoded_trace};
77
use cairo_vm::types::layout::CairoLayoutParams;
8+
use cairo_vm::vm::vm_core::DEFAULT_MAX_TRACEBACK_ENTRIES;
89
use cairo_vm::{
910
air_public_input::PublicInputError, types::layout_name::LayoutName,
1011
vm::errors::trace_errors::TraceError, Felt252,
@@ -144,6 +145,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<Option<String>, Error> {
144145
append_return_values: args.append_return_values,
145146
dynamic_layout_params: cairo_layout_params,
146147
disable_trace_padding: false,
148+
max_traceback_entries: DEFAULT_MAX_TRACEBACK_ENTRIES,
147149
};
148150

149151
// Try to parse the file as a sierra program

vm/src/cairo_run.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::{
1616
},
1717
security::verify_secure_runner,
1818
trace::trace_entry::RelocatedTraceEntry,
19+
vm_core::DEFAULT_MAX_TRACEBACK_ENTRIES,
1920
},
2021
Felt252,
2122
};
@@ -51,6 +52,7 @@ pub struct Cairo0RunConfig<'a> {
5152
/// instances of the builtin) compared to their sizes at the end of the execution.
5253
pub disable_trace_padding: bool,
5354
pub allow_missing_builtins: Option<bool>,
55+
pub max_traceback_entries: u32,
5456
}
5557

5658
impl Default for Cairo0RunConfig<'_> {
@@ -68,6 +70,7 @@ impl Default for Cairo0RunConfig<'_> {
6870
disable_trace_padding: false,
6971
allow_missing_builtins: None,
7072
dynamic_layout_params: None,
73+
max_traceback_entries: DEFAULT_MAX_TRACEBACK_ENTRIES,
7174
}
7275
}
7376
}
@@ -84,6 +87,7 @@ impl Cairo0RunConfig<'_> {
8487
self.trace_enabled,
8588
self.disable_trace_padding,
8689
runner_mode,
90+
self.max_traceback_entries,
8791
)
8892
}
8993
}
@@ -97,6 +101,7 @@ pub struct StwoCairoRunConfig {
97101
pub secure_run: bool,
98102
pub disable_trace_padding: bool,
99103
pub runner_mode: RunnerMode,
104+
pub max_traceback_entries: u32,
100105
}
101106

102107
impl Default for StwoCairoRunConfig {
@@ -109,6 +114,7 @@ impl Default for StwoCairoRunConfig {
109114
secure_run: true,
110115
disable_trace_padding: true,
111116
runner_mode: RunnerMode::ProofModeCanonical,
117+
max_traceback_entries: DEFAULT_MAX_TRACEBACK_ENTRIES,
112118
}
113119
}
114120
}
@@ -120,6 +126,7 @@ impl StwoCairoRunConfig {
120126
self.trace_enabled,
121127
self.disable_trace_padding,
122128
self.runner_mode.clone(),
129+
self.max_traceback_entries,
123130
)
124131
}
125132
}

vm/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ pub mod test_utils {
424424
($use_trace:expr) => {{
425425
crate::vm::vm_core::VirtualMachine::new(&crate::vm::vm_core::VirtualMachineConfig {
426426
trace_enabled: $use_trace,
427-
disable_trace_padding: false,
427+
..Default::default()
428428
})
429429
}};
430430
}

vm/src/vm/runners/cairo_runner.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ pub struct CairoRunConfig {
163163
trace_enabled: bool,
164164
disable_trace_padding: bool,
165165
runner_mode: RunnerMode,
166+
max_traceback_entries: u32,
166167
}
167168

168169
impl CairoRunConfig {
@@ -171,6 +172,7 @@ impl CairoRunConfig {
171172
trace_enabled: bool,
172173
disable_trace_padding: bool,
173174
runner_mode: RunnerMode,
175+
max_traceback_entries: u32,
174176
) -> Result<Self, RunnerError> {
175177
// `disable_trace_padding` can only be used in `proof_mode`, so we enforce this here to
176178
// avoid unintended behavior.
@@ -182,6 +184,7 @@ impl CairoRunConfig {
182184
trace_enabled,
183185
disable_trace_padding,
184186
runner_mode,
187+
max_traceback_entries,
185188
})
186189
}
187190
}
@@ -191,6 +194,7 @@ impl CairoRunConfig {
191194
VirtualMachineConfig {
192195
trace_enabled: self.trace_enabled,
193196
disable_trace_padding: self.disable_trace_padding,
197+
max_traceback_entries: self.max_traceback_entries,
194198
}
195199
}
196200
}
@@ -1734,6 +1738,7 @@ mod tests {
17341738
use crate::types::instance_definitions::bitwise_instance_def::CELLS_PER_BITWISE;
17351739
use crate::types::instance_definitions::keccak_instance_def::CELLS_PER_KECCAK;
17361740
use crate::types::layout_name::LayoutName;
1741+
use crate::vm::vm_core::DEFAULT_MAX_TRACEBACK_ENTRIES;
17371742
use crate::vm::vm_memory::memory::MemoryCell;
17381743
use std::collections::{HashMap, HashSet};
17391744

@@ -6050,6 +6055,7 @@ mod tests {
60506055
secure_run: true,
60516056
disable_trace_padding: true,
60526057
runner_mode: RunnerMode::ProofModeCanonical,
6058+
max_traceback_entries: DEFAULT_MAX_TRACEBACK_ENTRIES,
60536059
},
60546060
)
60556061
.unwrap();

vm/src/vm/vm_core.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use super::errors::runner_errors::RunnerError;
4444
use super::runners::builtin_runner::{ModBuiltinRunner, RC_N_PARTS_STANDARD};
4545
use super::runners::cairo_pie::CairoPie;
4646

47-
const MAX_TRACEBACK_ENTRIES: u32 = 20;
47+
pub const DEFAULT_MAX_TRACEBACK_ENTRIES: u32 = 20;
4848

4949
#[derive(PartialEq, Eq, Debug)]
5050
pub struct Operands {
@@ -106,10 +106,20 @@ impl DeducedOperands {
106106
}
107107
}
108108

109-
#[derive(Default)]
110109
pub struct VirtualMachineConfig {
111110
pub trace_enabled: bool,
112111
pub disable_trace_padding: bool,
112+
pub max_traceback_entries: u32,
113+
}
114+
115+
impl Default for VirtualMachineConfig {
116+
fn default() -> Self {
117+
Self {
118+
trace_enabled: false,
119+
disable_trace_padding: false,
120+
max_traceback_entries: DEFAULT_MAX_TRACEBACK_ENTRIES,
121+
}
122+
}
113123
}
114124

115125
pub struct VirtualMachine {
@@ -139,6 +149,7 @@ pub struct VirtualMachine {
139149
#[cfg(feature = "test_utils")]
140150
pub(crate) hooks: Option<Box<dyn crate::vm::hooks::StepHooks>>,
141151
pub(crate) relocation_table: Option<Vec<usize>>,
152+
pub(crate) max_traceback_entries: u32,
142153
}
143154

144155
impl Default for VirtualMachine {
@@ -177,6 +188,7 @@ impl VirtualMachine {
177188
#[cfg(feature = "test_utils")]
178189
hooks: None,
179190
relocation_table: None,
191+
max_traceback_entries: config.max_traceback_entries,
180192
}
181193
}
182194

@@ -947,7 +959,7 @@ impl VirtualMachine {
947959
let mut entries = Vec::<(Relocatable, Relocatable)>::new();
948960
let mut fp = Relocatable::from((1, self.run_context.fp));
949961
// Fetch the fp and pc traceback entries
950-
for _ in 0..MAX_TRACEBACK_ENTRIES {
962+
for _ in 0..self.max_traceback_entries {
951963
// Get return pc
952964
let ret_pc = match (fp - 1)
953965
.ok()
@@ -1396,6 +1408,7 @@ pub struct VirtualMachineBuilder {
13961408
run_finished: bool,
13971409
#[cfg(feature = "test_utils")]
13981410
pub(crate) hooks: Option<Box<dyn crate::vm::hooks::StepHooks>>,
1411+
pub(crate) max_traceback_entries: u32,
13991412
}
14001413

14011414
impl Default for VirtualMachineBuilder {
@@ -1416,6 +1429,7 @@ impl Default for VirtualMachineBuilder {
14161429
run_finished: false,
14171430
#[cfg(feature = "test_utils")]
14181431
hooks: None,
1432+
max_traceback_entries: DEFAULT_MAX_TRACEBACK_ENTRIES,
14191433
}
14201434
}
14211435
}
@@ -1465,6 +1479,11 @@ impl VirtualMachineBuilder {
14651479
self
14661480
}
14671481

1482+
pub fn max_traceback_entries(mut self, max_traceback_entries: u32) -> VirtualMachineBuilder {
1483+
self.max_traceback_entries = max_traceback_entries;
1484+
self
1485+
}
1486+
14681487
pub fn build(self) -> VirtualMachine {
14691488
VirtualMachine {
14701489
run_context: self.run_context,
@@ -1482,6 +1501,7 @@ impl VirtualMachineBuilder {
14821501
hooks: self.hooks,
14831502
relocation_table: None,
14841503
disable_trace_padding: false,
1504+
max_traceback_entries: self.max_traceback_entries,
14851505
}
14861506
}
14871507
}

0 commit comments

Comments
 (0)