Skip to content

Commit d7f56e9

Browse files
committed
add relocate trace flag to the runner
1 parent 4226352 commit d7f56e9

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-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: 20 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,7 @@ impl Default for CairoRunConfig<'_> {
5154
entrypoint: "main",
5255
trace_enabled: false,
5356
relocate_mem: false,
57+
relocate_trace: false,
5458
layout: LayoutName::plain,
5559
proof_mode: false,
5660
secure_run: None,
@@ -113,7 +117,10 @@ pub fn cairo_run_program_with_initial_scope(
113117
if secure_run {
114118
verify_secure_runner(&cairo_runner, true, None)?;
115119
}
116-
cairo_runner.relocate(cairo_run_config.relocate_mem)?;
120+
cairo_runner.relocate(
121+
cairo_run_config.relocate_mem,
122+
cairo_run_config.relocate_trace,
123+
)?;
117124

118125
Ok(cairo_runner)
119126
}
@@ -218,7 +225,10 @@ pub fn cairo_run_pie(
218225
// Check that the Cairo PIE produced by this run is compatible with the Cairo PIE received
219226
cairo_runner.get_cairo_pie()?.check_pie_compatibility(pie)?;
220227
}
221-
cairo_runner.relocate(cairo_run_config.relocate_mem)?;
228+
cairo_runner.relocate(
229+
cairo_run_config.relocate_mem,
230+
cairo_run_config.relocate_trace,
231+
)?;
222232

223233
Ok(cairo_runner)
224234
}
@@ -267,7 +277,10 @@ pub fn cairo_run_fuzzed_program(
267277
if secure_run {
268278
verify_secure_runner(&cairo_runner, true, None)?;
269279
}
270-
cairo_runner.relocate(cairo_run_config.relocate_mem)?;
280+
cairo_runner.relocate(
281+
cairo_run_config.relocate_mem,
282+
cairo_run_config.relocate_trace,
283+
)?;
271284

272285
Ok(cairo_runner)
273286
}
@@ -367,7 +380,7 @@ mod tests {
367380

368381
let end = cairo_runner.initialize(false).unwrap();
369382
assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_ok());
370-
assert!(cairo_runner.relocate(true).is_ok());
383+
assert!(cairo_runner.relocate(true, true).is_ok());
371384
// `main` returns without doing nothing, but `not_main` sets `[ap]` to `1`
372385
// Memory location was found empirically and simply hardcoded
373386
assert_eq!(cairo_runner.relocated_memory[2], Some(Felt252::from(123)));
@@ -433,7 +446,7 @@ mod tests {
433446
let mut hint_processor = BuiltinHintProcessor::new_empty();
434447
let mut cairo_runner = run_test_program(program_content, &mut hint_processor).unwrap();
435448

436-
assert!(cairo_runner.relocate(false).is_ok());
449+
assert!(cairo_runner.relocate(false, true).is_ok());
437450

438451
let trace_entries = cairo_runner.relocated_trace.unwrap();
439452
let mut buffer = [0; 24];
@@ -457,7 +470,7 @@ mod tests {
457470
let mut cairo_runner = run_test_program(program_content, &mut hint_processor).unwrap();
458471

459472
// relocate memory so we can dump it to file
460-
assert!(cairo_runner.relocate(true).is_ok());
473+
assert!(cairo_runner.relocate(true, true).is_ok());
461474

462475
let mut buffer = [0; 120];
463476
let mut buff_writer = SliceWriter::new(&mut buffer);
@@ -481,7 +494,7 @@ mod tests {
481494
let mut cairo_runner = cairo_runner!(program);
482495
let end = cairo_runner.initialize(false).unwrap();
483496
assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_ok());
484-
assert!(cairo_runner.relocate(false).is_ok());
497+
assert!(cairo_runner.relocate(false, false).is_ok());
485498
assert!(cairo_runner.relocated_trace.is_none());
486499
}
487500

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)