Skip to content

Commit 4a6f373

Browse files
Stavbegabrielbosio
authored andcommitted
add relocate trace flag to the runner
1 parent 3d3cd4a commit 4a6f373

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

cairo-vm-cli/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<(), Error> {
189189
entrypoint: &args.entrypoint,
190190
trace_enabled,
191191
relocate_mem: args.memory_file.is_some() || args.air_public_input.is_some(),
192+
relocate_trace: trace_enabled,
192193
layout: args.layout,
193194
proof_mode: args.proof_mode,
194195
secure_run: args.secure_run,

cairo1-run/src/cairo_run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ pub fn cairo_run_program(
341341
}
342342
}
343343

344-
runner.relocate(true)?;
344+
runner.relocate(true, true)?;
345345

346346
Ok((runner, return_values, serialized_output))
347347
}

vm/src/cairo_run.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ pub struct CairoRunConfig<'a> {
2727
#[cfg_attr(feature = "test_utils", arbitrary(value = "main"))]
2828
pub entrypoint: &'a str,
2929
pub trace_enabled: bool,
30+
/// Relocate memory if `true`, otherwise memory is not relocated.
3031
pub relocate_mem: bool,
32+
// When `relocate_trace` is set to `false`, the trace will not be relocated even if `trace_enabled` is `true`.
33+
pub relocate_trace: bool,
3134
pub layout: LayoutName,
3235
/// The `dynamic_layout_params` argument should only be used with dynamic layout.
3336
/// It is ignored otherwise.
@@ -51,6 +54,8 @@ impl Default for CairoRunConfig<'_> {
5154
entrypoint: "main",
5255
trace_enabled: false,
5356
relocate_mem: false,
57+
// Set to true to match expected behavior: trace is relocated only if trace_enabled is true.
58+
relocate_trace: true,
5459
layout: LayoutName::plain,
5560
proof_mode: false,
5661
secure_run: None,
@@ -113,7 +118,10 @@ pub fn cairo_run_program_with_initial_scope(
113118
if secure_run {
114119
verify_secure_runner(&cairo_runner, true, None)?;
115120
}
116-
cairo_runner.relocate(cairo_run_config.relocate_mem)?;
121+
cairo_runner.relocate(
122+
cairo_run_config.relocate_mem,
123+
cairo_run_config.relocate_trace,
124+
)?;
117125

118126
Ok(cairo_runner)
119127
}
@@ -218,7 +226,10 @@ pub fn cairo_run_pie(
218226
// Check that the Cairo PIE produced by this run is compatible with the Cairo PIE received
219227
cairo_runner.get_cairo_pie()?.check_pie_compatibility(pie)?;
220228
}
221-
cairo_runner.relocate(cairo_run_config.relocate_mem)?;
229+
cairo_runner.relocate(
230+
cairo_run_config.relocate_mem,
231+
cairo_run_config.relocate_trace,
232+
)?;
222233

223234
Ok(cairo_runner)
224235
}
@@ -267,7 +278,10 @@ pub fn cairo_run_fuzzed_program(
267278
if secure_run {
268279
verify_secure_runner(&cairo_runner, true, None)?;
269280
}
270-
cairo_runner.relocate(cairo_run_config.relocate_mem)?;
281+
cairo_runner.relocate(
282+
cairo_run_config.relocate_mem,
283+
cairo_run_config.relocate_trace,
284+
)?;
271285

272286
Ok(cairo_runner)
273287
}
@@ -367,7 +381,7 @@ mod tests {
367381

368382
let end = cairo_runner.initialize(false).unwrap();
369383
assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_ok());
370-
assert!(cairo_runner.relocate(true).is_ok());
384+
assert!(cairo_runner.relocate(true, true).is_ok());
371385
// `main` returns without doing nothing, but `not_main` sets `[ap]` to `1`
372386
// Memory location was found empirically and simply hardcoded
373387
assert_eq!(cairo_runner.relocated_memory[2], Some(Felt252::from(123)));
@@ -433,7 +447,7 @@ mod tests {
433447
let mut hint_processor = BuiltinHintProcessor::new_empty();
434448
let mut cairo_runner = run_test_program(program_content, &mut hint_processor).unwrap();
435449

436-
assert!(cairo_runner.relocate(false).is_ok());
450+
assert!(cairo_runner.relocate(false, true).is_ok());
437451

438452
let trace_entries = cairo_runner.relocated_trace.unwrap();
439453
let mut buffer = [0; 24];
@@ -457,7 +471,7 @@ mod tests {
457471
let mut cairo_runner = run_test_program(program_content, &mut hint_processor).unwrap();
458472

459473
// relocate memory so we can dump it to file
460-
assert!(cairo_runner.relocate(true).is_ok());
474+
assert!(cairo_runner.relocate(true, true).is_ok());
461475

462476
let mut buffer = [0; 120];
463477
let mut buff_writer = SliceWriter::new(&mut buffer);
@@ -481,7 +495,7 @@ mod tests {
481495
let mut cairo_runner = cairo_runner!(program);
482496
let end = cairo_runner.initialize(false).unwrap();
483497
assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_ok());
484-
assert!(cairo_runner.relocate(false).is_ok());
498+
assert!(cairo_runner.relocate(false, false).is_ok());
485499
assert!(cairo_runner.relocated_trace.is_none());
486500
}
487501

vm/src/vm/runners/cairo_runner.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -984,9 +984,9 @@ impl CairoRunner {
984984
Ok(())
985985
}
986986

987-
pub fn relocate(&mut self, relocate_mem: bool) -> Result<(), TraceError> {
987+
pub fn relocate(&mut self, relocate_mem: bool, relocate_trace: bool) -> Result<(), TraceError> {
988988
self.vm.segments.compute_effective_sizes();
989-
if !relocate_mem && self.vm.trace.is_none() {
989+
if !relocate_mem && (self.vm.trace.is_none() || !relocate_trace) {
990990
return Ok(());
991991
}
992992
// relocate_segments can fail if compute_effective_sizes is not called before.
@@ -1002,7 +1002,7 @@ impl CairoRunner {
10021002
return Err(TraceError::MemoryError(memory_error));
10031003
}
10041004
}
1005-
if self.vm.trace.is_some() {
1005+
if self.vm.trace.is_some() && relocate_trace {
10061006
self.relocate_trace(&relocation_table)?;
10071007
}
10081008
self.vm.relocation_table = Some(relocation_table);

0 commit comments

Comments
 (0)