Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Aug 21, 2024
1 parent c5dfb21 commit c4706f2
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::collections::BTreeMap;

#[derive(Clone, Debug, Default, Serialize)]
pub struct ProgramTrace {
states: Vec<StateDump>,
pub states: Vec<StateDump>,
// TODO: Syscall data.
}

Expand All @@ -23,8 +23,8 @@ impl ProgramTrace {

#[derive(Clone, Debug)]
pub struct StateDump {
statement_idx: StatementIdx,
items: BTreeMap<u64, Value>,
pub statement_idx: StatementIdx,
pub items: BTreeMap<u64, Value>,
}

impl StateDump {
Expand Down
4 changes: 2 additions & 2 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ impl<S: StarknetSyscallHandler> VirtualMachine<S> {
let state_snapshot = frame.state.get_mut().clone();

debug!(
"Evaluating statement {} ({})",
frame.pc.0, &self.program.statements[frame.pc.0]
"Evaluating statement {} ({}) (values: \n{:#?}\n)",
frame.pc.0, &self.program.statements[frame.pc.0], state_snapshot
);
match &self.program.statements[frame.pc.0] {
GenStatement::Invocation(invocation) => {
Expand Down
2 changes: 1 addition & 1 deletion src/vm/uint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn eval_equal(
panic!()
};

EvalAction::NormalBranch((lhs != rhs) as usize, smallvec![])
EvalAction::NormalBranch((lhs == rhs) as usize, smallvec![])
}

pub fn eval_is_zero(
Expand Down
2 changes: 1 addition & 1 deletion src/vm/uint32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn eval_equal(
panic!()
};

EvalAction::NormalBranch((lhs != rhs) as usize, smallvec![])
EvalAction::NormalBranch((lhs == rhs) as usize, smallvec![])
}

pub fn eval_is_zero(
Expand Down
2 changes: 1 addition & 1 deletion src/vm/uint64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn eval_equal(
panic!()
};

EvalAction::NormalBranch((lhs != rhs) as usize, smallvec![])
EvalAction::NormalBranch((lhs == rhs) as usize, smallvec![])
}

pub fn eval_is_zero(
Expand Down
2 changes: 1 addition & 1 deletion src/vm/uint8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn eval_equal(
panic!()
};

EvalAction::NormalBranch((lhs != rhs) as usize, smallvec![])
EvalAction::NormalBranch((lhs == rhs) as usize, smallvec![])
}

pub fn eval_is_zero(
Expand Down
133 changes: 133 additions & 0 deletions tests/libfuncs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
use std::{path::Path, sync::Arc};

use cairo_lang_compiler::{compile_cairo_project_at_path, CompilerConfig};
use cairo_lang_sierra::{
extensions::{core::CoreTypeConcrete, starknet::StarkNetTypeConcrete},
program::{GenFunction, Program, StatementIdx},
};
use sierra_emu::{ProgramTrace, StateDump, Value, VirtualMachine};

fn run_program(path: &str, func_name: &str, args: &[Value]) -> Vec<Value> {
let path = Path::new(path);

let sierra_program = Arc::new(
compile_cairo_project_at_path(
path,
CompilerConfig {
replace_ids: true,
..Default::default()
},
)
.unwrap(),
);

let function = find_entry_point_by_name(&sierra_program, func_name).unwrap();

let mut vm = VirtualMachine::new(sierra_program.clone());

let mut args = args.iter().cloned();
let initial_gas = 1000000;

vm.push_frame(
function.id.clone(),
function
.signature
.param_types
.iter()
.map(|type_id| {
let type_info = vm.registry().get_type(type_id).unwrap();
match type_info {
CoreTypeConcrete::GasBuiltin(_) => Value::U128(initial_gas),
CoreTypeConcrete::StarkNet(StarkNetTypeConcrete::System(_)) => Value::Unit,
CoreTypeConcrete::RangeCheck(_)
| CoreTypeConcrete::Pedersen(_)
| CoreTypeConcrete::Poseidon(_)
| CoreTypeConcrete::Bitwise(_)
| CoreTypeConcrete::BuiltinCosts(_)
| CoreTypeConcrete::SegmentArena(_) => Value::Unit,
_ => args.next().unwrap(),
}
})
.collect::<Vec<_>>(),
);

let mut trace = ProgramTrace::new();

while let Some((statement_idx, state)) = vm.step() {
trace.push(StateDump::new(statement_idx, state));
}

trace
.states
.last()
.unwrap()
.items
.values()
.cloned()
.collect()
}

#[test]
fn test_u32_overflow() {
let r = run_program(
"tests/tests/test_u32.cairo",
"test_u32::test_u32::run_test",
&[Value::U32(2), Value::U32(2)],
);
assert!(matches!(
r[1],
Value::Enum {
self_ty: _,
index: 0,
payload: _
}
));

let r = run_program(
"tests/tests/test_u32.cairo",
"test_u32::test_u32::run_test",
&[Value::U32(2), Value::U32(3)],
);
assert!(matches!(
r[1],
Value::Enum {
self_ty: _,
index: 1,
payload: _
}
));

let r = run_program(
"tests/tests/test_u32.cairo",
"test_u32::test_u32::run_test",
&[Value::U32(0), Value::U32(0)],
);
assert!(matches!(
r[1],
Value::Enum {
self_ty: _,
index: 0,
payload: _
}
));
}

pub fn find_entry_point_by_idx(
program: &Program,
entry_point_idx: usize,
) -> Option<&GenFunction<StatementIdx>> {
program
.funcs
.iter()
.find(|x| x.id.id == entry_point_idx as u64)
}

pub fn find_entry_point_by_name<'a>(
program: &'a Program,
name: &str,
) -> Option<&'a GenFunction<StatementIdx>> {
program
.funcs
.iter()
.find(|x| x.id.debug_name.as_ref().map(|x| x.as_str()) == Some(name))
}
3 changes: 3 additions & 0 deletions tests/tests/test_u32.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn run_test(lhs: u32, rhs: u32) -> u32 {
lhs - rhs
}

0 comments on commit c4706f2

Please sign in to comment.