Skip to content

Commit

Permalink
fix wasm builds
Browse files Browse the repository at this point in the history
  • Loading branch information
odesenfans committed Mar 1, 2024
1 parent dd9f287 commit 792ed8e
Showing 1 changed file with 54 additions and 68 deletions.
122 changes: 54 additions & 68 deletions vm/src/vm/runners/cairo_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ impl CairoRunner {
instance_def.n_parts,
included,
)
.into(),
.into(),
);
}
}
Expand Down Expand Up @@ -489,13 +489,7 @@ impl CairoRunner {
Ok(end)
}

///Initializes state for running a program from the main() entrypoint.
///If self.is_proof_mode() == True, the execution starts from the start label rather then the main() function.
///Returns the value of the program counter after returning from main.
fn initialize_main_entrypoint(
&mut self,
vm: &mut VirtualMachine,
) -> Result<Relocatable, RunnerError> {
fn prepare_builtins_initial_stack(&self, vm: &VirtualMachine) -> Vec<MaybeRelocatable> {
let mut stack = Vec::new();

// Build a name -> builtin map for easier lookup
Expand All @@ -507,7 +501,7 @@ impl CairoRunner {

// For each builtin in the program, set up the corresponding initial stack.
// Builtins required in the program but not set up in the VM (i.e. when specifying
// `allow_missing_builtins` are set to (0, 0).
// `allow_missing_builtins`) are set to (0, 0).
for builtin_name in self.program.builtins.iter() {
if let Some(builtin_runner) = vm_builtin_map.get(builtin_name.name()) {
stack.append(&mut builtin_runner.initial_stack());
Expand All @@ -516,6 +510,18 @@ impl CairoRunner {
}
}

stack
}

///Initializes state for running a program from the main() entrypoint.
///If self.is_proof_mode() == True, the execution starts from the start label rather then the main() function.
///Returns the value of the program counter after returning from main.
fn initialize_main_entrypoint(
&mut self,
vm: &mut VirtualMachine,
) -> Result<Relocatable, RunnerError> {
let mut stack = self.prepare_builtins_initial_stack(vm);

if self.is_proof_mode() {
// In canonical proof mode, add the dummy last fp and pc to the public memory, so that the verifier can enforce

Expand Down Expand Up @@ -578,10 +584,10 @@ impl CairoRunner {
self.initial_ap = self.initial_fp;
return Ok(self.program_base.as_ref().ok_or(RunnerError::NoProgBase)?
+ self
.program
.shared_program_data
.end
.ok_or(RunnerError::NoProgramEnd)?);
.program
.shared_program_data
.end
.ok_or(RunnerError::NoProgramEnd)?);
}

let return_fp = vm.segments.add();
Expand Down Expand Up @@ -655,11 +661,11 @@ impl CairoRunner {
) -> Result<(), VirtualMachineError> {
let references = &self.program.shared_program_data.reference_manager;
#[cfg(not(feature = "extensive_hints"))]
let hint_data = self.get_hint_data(references, hint_processor)?;
let hint_data = self.get_hint_data(references, hint_processor)?;
#[cfg(feature = "extensive_hints")]
let mut hint_data = self.get_hint_data(references, hint_processor)?;
let mut hint_data = self.get_hint_data(references, hint_processor)?;
#[cfg(feature = "extensive_hints")]
let mut hint_ranges = self
let mut hint_ranges = self
.program
.shared_program_data
.hints_collection
Expand All @@ -672,9 +678,9 @@ impl CairoRunner {
hint_processor,
&mut self.exec_scopes,
#[cfg(feature = "extensive_hints")]
&mut hint_data,
&mut hint_data,
#[cfg(not(feature = "extensive_hints"))]
self.program
self.program
.shared_program_data
.hints_collection
.get_hint_range_for_pc(vm.run_context.pc.offset)
Expand All @@ -683,7 +689,7 @@ impl CairoRunner {
})
.unwrap_or(&[]),
#[cfg(feature = "extensive_hints")]
&mut hint_ranges,
&mut hint_ranges,
&self.program.constants,
)?;

Expand All @@ -706,18 +712,18 @@ impl CairoRunner {
) -> Result<(), VirtualMachineError> {
let references = &self.program.shared_program_data.reference_manager;
#[cfg(not(feature = "extensive_hints"))]
let hint_data = self.get_hint_data(references, hint_processor)?;
let hint_data = self.get_hint_data(references, hint_processor)?;
#[cfg(feature = "extensive_hints")]
let mut hint_data = self.get_hint_data(references, hint_processor)?;
let mut hint_data = self.get_hint_data(references, hint_processor)?;
#[cfg(feature = "extensive_hints")]
let mut hint_ranges = self
let mut hint_ranges = self
.program
.shared_program_data
.hints_collection
.hints_ranges
.clone();
#[cfg(not(feature = "extensive_hints"))]
let hint_data = &self
let hint_data = &self
.program
.shared_program_data
.hints_collection
Expand All @@ -736,11 +742,11 @@ impl CairoRunner {
hint_processor,
&mut self.exec_scopes,
#[cfg(feature = "extensive_hints")]
&mut hint_data,
&mut hint_data,
#[cfg(not(feature = "extensive_hints"))]
hint_data,
hint_data,
#[cfg(feature = "extensive_hints")]
&mut hint_ranges,
&mut hint_ranges,
&self.program.constants,
)?;
}
Expand Down Expand Up @@ -800,7 +806,7 @@ impl CairoRunner {
(rc_max - rc_min) as usize,
))),
)
.into());
.into());
}

Ok(())
Expand Down Expand Up @@ -849,7 +855,7 @@ impl CairoRunner {
diluted_usage_upper_bound,
))),
)
.into());
.into());
}

Ok(())
Expand Down Expand Up @@ -880,8 +886,7 @@ impl CairoRunner {
match self.check_used_cells(vm) {
Ok(_) => break,
Err(e) => match e {
VirtualMachineError::Memory(MemoryError::InsufficientAllocatedCells(_)) => {
}
VirtualMachineError::Memory(MemoryError::InsufficientAllocatedCells(_)) => {}
e => return Err(e),
},
}
Expand Down Expand Up @@ -1190,7 +1195,7 @@ impl CairoRunner {
total_memory_units,
instance._public_memory_fraction,
)
.into());
.into());

Check warning on line 1198 in vm/src/vm/runners/cairo_runner.rs

View check run for this annotation

Codecov / codecov/patch

vm/src/vm/runners/cairo_runner.rs#L1198

Added line #L1198 was not covered by tests
}

let instruction_memory_units = 4 * vm_current_step_u32;
Expand Down Expand Up @@ -1257,25 +1262,8 @@ impl CairoRunner {
if !self.run_ended {
return Err(RunnerError::ReadReturnValuesNoEndRun);
}
let mut pointer = vm.get_ap();
let mut vm_builtin_map: HashMap<_, _> = vm
.builtin_runners
.iter_mut()
.map(|builtin| (builtin.name(), builtin))
.collect();

for builtin_name in ORDERED_BUILTINS.iter().rev() {
if let Some(builtin_runner) = vm_builtin_map.get_mut(builtin_name.name()) {
pointer = (*builtin_runner).final_stack(&vm.segments, pointer)?;
} else {
// If `allow_missing_builtins` is set, it is possible for a builtin to appear
// in the program and be absent from the VM. In this case, simply decrement
// the stack pointer.
if self.program.builtins.contains(builtin_name) {
pointer = (pointer - 1)?;
}
}
}
let stack_ptr = vm.get_ap();
let stack_ptr = self.get_builtins_final_stack(vm, stack_ptr)?;

if self.segments_finalized {
return Err(RunnerError::FailedAddingReturnValues);
Expand All @@ -1285,7 +1273,7 @@ impl CairoRunner {
.execution_base
.as_ref()
.ok_or(RunnerError::NoExecBase)?;
let begin = pointer.offset - exec_base.offset;
let begin = stack_ptr.offset - exec_base.offset;
let ap = vm.get_ap();
let end = ap.offset - exec_base.offset;
self.execution_public_memory
Expand Down Expand Up @@ -1317,10 +1305,8 @@ impl CairoRunner {
pub fn get_builtins_final_stack(
&self,
vm: &mut VirtualMachine,
stack_ptr: Relocatable,
mut stack_ptr: Relocatable,
) -> Result<Relocatable, RunnerError> {
let mut stack_ptr = Relocatable::from(&stack_ptr);

let mut vm_builtin_map: HashMap<_, _> = vm
.builtin_runners
.iter_mut()
Expand Down Expand Up @@ -1922,7 +1908,7 @@ mod tests {
include_bytes!("../../../../cairo_programs/fibonacci.json"),
Some("main"),
)
.unwrap();
.unwrap();

let mut cairo_runner = cairo_runner!(program);
let mut vm = vm!();
Expand Down Expand Up @@ -3807,7 +3793,7 @@ mod tests {
include_bytes!("../../../../cairo_programs/proof_programs/fibonacci.json"),
Some("main"),
)
.unwrap();
.unwrap();

let mut hint_processor = BuiltinHintProcessor::new_empty();
let mut cairo_runner = cairo_runner!(program, "all_cairo", true);
Expand Down Expand Up @@ -4884,7 +4870,7 @@ mod tests {
include_bytes!("../../../../cairo_programs/example_program.json"),
None,
)
.unwrap();
.unwrap();
let mut cairo_runner = cairo_runner!(program);
let mut vm = vm!(true); //this true expression dictates that the trace is enabled
let mut hint_processor = BuiltinHintProcessor::new_empty();
Expand Down Expand Up @@ -4955,7 +4941,7 @@ mod tests {
include_bytes!("../../../../cairo_programs/bitwise_builtin_test.json"),
None,
)
.unwrap();
.unwrap();
let mut cairo_runner = cairo_runner!(program);
let mut vm = vm!(true); //this true expression dictates that the trace is enabled
let mut hint_processor = BuiltinHintProcessor::new_empty();
Expand Down Expand Up @@ -5074,7 +5060,7 @@ mod tests {
include_bytes!("../../../../cairo_programs/bad_programs/error_msg_function.json"),
None,
)
.unwrap();
.unwrap();
let mut cairo_runner = cairo_runner!(program);
let mut vm = vm!(true); //this true expression dictates that the trace is enabled
let mut hint_processor = BuiltinHintProcessor::new_empty();
Expand Down Expand Up @@ -5118,7 +5104,7 @@ mod tests {
include_bytes!("../../../../cairo_programs/assert_le_felt_hint.json"),
Some("main"),
)
.unwrap();
.unwrap();
let mut runner = cairo_runner!(program);
let mut vm = vm!();
let end = runner.initialize(&mut vm, false).unwrap();
Expand All @@ -5141,7 +5127,7 @@ mod tests {
include_bytes!("../../../../cairo_programs/integration.json"),
Some("main"),
)
.unwrap();
.unwrap();
let mut runner = cairo_runner!(program);
let mut vm = vm!();
let end = runner.initialize(&mut vm, false).unwrap();
Expand All @@ -5164,7 +5150,7 @@ mod tests {
include_bytes!("../../../../cairo_programs/fibonacci.json"),
Some("main"),
)
.unwrap();
.unwrap();
let mut runner = cairo_runner!(program);
let mut vm = vm!();
let end = runner.initialize(&mut vm, false).unwrap();
Expand All @@ -5187,7 +5173,7 @@ mod tests {
include_bytes!("../../../../cairo_programs/integration.json"),
Some("main"),
)
.unwrap();
.unwrap();
let mut runner = cairo_runner!(program);
let mut vm = vm!();
let end = runner.initialize(&mut vm, false).unwrap();
Expand Down Expand Up @@ -5275,7 +5261,7 @@ mod tests {
include_bytes!("../../../../cairo_programs/fibonacci.json"),
Some("main"),
)
.unwrap();
.unwrap();
let mut runner = cairo_runner!(program);
let mut vm = vm!();
let end = runner.initialize(&mut vm, false).unwrap();
Expand All @@ -5294,7 +5280,7 @@ mod tests {
include_bytes!("../../../../cairo_programs/fibonacci.json"),
Some("main"),
)
.unwrap();
.unwrap();
let mut runner = cairo_runner!(program);
let mut vm = vm!();
let end = runner.initialize(&mut vm, false).unwrap();
Expand All @@ -5315,7 +5301,7 @@ mod tests {
include_bytes!("../../../../cairo_programs/fibonacci.json"),
Some("main"),
)
.unwrap();
.unwrap();
let mut runner = cairo_runner!(program);
let mut vm = vm!();
let end = runner.initialize(&mut vm, false).unwrap();
Expand All @@ -5336,7 +5322,7 @@ mod tests {
include_bytes!("../../../../cairo_programs/fibonacci.json"),
Some("main"),
)
.unwrap();
.unwrap();
let mut runner = cairo_runner!(program);
let mut vm = vm!();
let end = runner.initialize(&mut vm, false).unwrap();
Expand Down Expand Up @@ -5525,7 +5511,7 @@ mod tests {
},
&mut BuiltinHintProcessor::new_empty(),
)
.unwrap();
.unwrap();
let air_private_input = runner.get_air_private_input(&vm);
assert!(air_private_input.0[HASH_BUILTIN_NAME].is_empty());
assert!(air_private_input.0[RANGE_CHECK_BUILTIN_NAME].is_empty());
Expand Down

0 comments on commit 792ed8e

Please sign in to comment.