Skip to content

Commit 236ca7a

Browse files
committed
Merge #294: Add optional arguments budget and env to run_program test helper
7529af3 Add optional arguments budget and env to run_program test helper (Michael Zaikin) Pull request description: `run_program` test helper is quite useful because it allows to run programs in (almost) the same way they would be run in Elements/Liquid node. The execution behaviour should ideally be identical to `BitMachine`, but there is a possibility of a divergence and using this helper is a good way to catch it. This PR adds two optional arguments to allow specifying actual budget and environment. This prevents segmentation fault for some programs. ACKs for top commit: apoelstra: ACK 7529af3; successfully ran local tests; thanks! Tree-SHA512: d7bffcdb65285a5a12d76a9f5c6e7753444f2cb6e5a3d1e365c73c52ad4991cbdac7667d15c16741a0ce7fbebfddc505a687e4d457ca457c9a54ecffb1a8c584
2 parents 567d88e + 7529af3 commit 236ca7a

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

fuzz/fuzz_targets/c_rust_merkle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn do_test(data: &[u8]) {
2727
};
2828

2929
let (program, witness) = data.split_at(prog_len);
30-
let c_result = run_program(program, witness, TestUpTo::CheckOneOne);
30+
let c_result = run_program(program, witness, TestUpTo::CheckOneOne, None, None);
3131

3232
let prog_iter = BitIter::from(program);
3333
let wit_iter = BitIter::from(witness);

simplicity-sys/src/tests/mod.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::tests::ffi::{
1616
type_inference::simplicity_mallocTypeInference,
1717
SimplicityErr,
1818
};
19+
use crate::CElementsTxEnv;
1920

2021
/// The result of parsing, typechecking, and running a Simplicity program
2122
/// through the C FFI
@@ -72,7 +73,8 @@ pub fn run_test(
7273
target_ihr: &[u32; 8],
7374
cost_bound: ubounded,
7475
) {
75-
let result = run_program(program, witness, TestUpTo::Everything).expect("running program");
76+
let result =
77+
run_program(program, witness, TestUpTo::Everything, None, None).expect("running program");
7678
assert_eq!(result.amr, CSha256Midstate { s: *target_amr });
7779
assert_eq!(result.cmr, CSha256Midstate { s: *target_cmr });
7880
assert_eq!(result.ihr, CSha256Midstate { s: *target_ihr });
@@ -90,7 +92,8 @@ pub fn run_test_fail(
9092
target_ihr: &[u32; 8],
9193
cost_bound: ubounded,
9294
) {
93-
let result = run_program(program, witness, TestUpTo::Everything).expect("running program");
95+
let result =
96+
run_program(program, witness, TestUpTo::Everything, None, None).expect("running program");
9497
assert_eq!(result.amr, CSha256Midstate { s: *target_amr });
9598
assert_eq!(result.cmr, CSha256Midstate { s: *target_cmr });
9699
assert_eq!(result.ihr, CSha256Midstate { s: *target_ihr });
@@ -110,10 +113,13 @@ impl Drop for FreeOnDrop {
110113
/// Run a program and return data about it
111114
///
112115
/// This is mostly a direct port of `run_program` in C `tests.c`.
116+
/// WARNING: empty environment could lead to a crash (depending on which jets are used).
113117
pub fn run_program(
114118
program: &[u8],
115119
witness: &[u8],
116120
test_up_to: TestUpTo,
121+
budget: Option<u32>,
122+
env: Option<&CElementsTxEnv>,
117123
) -> Result<TestOutput, SimplicityErr> {
118124
let mut result = TestOutput {
119125
amr: CSha256Midstate::default(),
@@ -266,8 +272,9 @@ pub fn run_program(
266272
}
267273

268274
// 9. Run the program
269-
result.eval_result =
270-
simplicity_evalTCOProgram(dag, type_dag, len, ptr::null(), ptr::null());
275+
let budget_ptr = budget.map(|b| b as *const _).unwrap_or(ptr::null());
276+
let env_ptr = env.map(|e| e as *const _).unwrap_or(ptr::null());
277+
result.eval_result = simplicity_evalTCOProgram(dag, type_dag, len, budget_ptr, env_ptr);
271278
}
272279

273280
Ok(result)

src/node/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,14 @@ mod tests {
723723
fn check_merkle_roots(test: &TestData) {
724724
let prog = BitIter::from(test.prog.as_slice());
725725
let witness = BitIter::from(test.witness.as_slice());
726-
ffi::tests::run_program(&test.prog, &test.witness, ffi::tests::TestUpTo::CheckOneOne)
727-
.unwrap();
726+
ffi::tests::run_program(
727+
&test.prog,
728+
&test.witness,
729+
ffi::tests::TestUpTo::CheckOneOne,
730+
None,
731+
None,
732+
)
733+
.unwrap();
728734
let prog = RedeemNode::<Elements>::decode(prog, witness).unwrap();
729735
assert_eq!(prog.cmr().to_byte_array(), test.cmr);
730736
assert_eq!(prog.amr().to_byte_array(), test.amr);

0 commit comments

Comments
 (0)