From 19b02f2509e92cc3d7a3a2017425c4542b9b6a18 Mon Sep 17 00:00:00 2001 From: Tommy Volk Date: Fri, 20 Mar 2026 21:40:19 -0500 Subject: [PATCH] fix(tests): add missing `min_cost` parameter to `evalTCOExpression` FFI binding The C function `evalTCOExpression` gained a `minCost` parameter (between `len` and `budget`), but the Rust FFI binding was not updated to match. This caused undefined behavior due to argument mismatch in the foreign function call. Changes: - Add `min_cost: ubounded` parameter to `simplicity_evalTCOExpression` extern declaration to match the C signature. - Pass `0` as `min_cost` in the `simplicity_evalTCOProgram` wrapper, matching the C inline `evalTCOProgram` helper in eval.h. - Fix `budget.map(|b| b as *const _)` to `budget.as_ref().map(|b| b as *const _)` in `run_program` to avoid moving the `Option` value and instead take a reference to its contents, producing a valid pointer to the stack-local budget value. Co-Authored-By: Claude Opus 4.6 (1M context) --- simplicity-sys/src/tests/ffi.rs | 2 ++ simplicity-sys/src/tests/mod.rs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/simplicity-sys/src/tests/ffi.rs b/simplicity-sys/src/tests/ffi.rs index 25cc1be5..410925b0 100644 --- a/simplicity-sys/src/tests/ffi.rs +++ b/simplicity-sys/src/tests/ffi.rs @@ -398,6 +398,7 @@ pub mod eval { dag: *const CDagNode, type_dag: *mut CType, len: c_size_t, + min_cost: ubounded, budget: *const ubounded, env: *const elements::CTxEnv, ) -> SimplicityErr; @@ -444,6 +445,7 @@ pub mod eval { dag, type_dag, len, + 0, budget, env, ) diff --git a/simplicity-sys/src/tests/mod.rs b/simplicity-sys/src/tests/mod.rs index 83071d86..bf8a9eb2 100644 --- a/simplicity-sys/src/tests/mod.rs +++ b/simplicity-sys/src/tests/mod.rs @@ -317,7 +317,7 @@ pub fn run_program( } // 9. Run the program - let budget_ptr = budget.map(|b| b as *const _).unwrap_or(ptr::null()); + let budget_ptr = budget.as_ref().map(|b| b as *const _).unwrap_or(ptr::null()); let env_ptr = env.map(|e| e as *const _).unwrap_or(ptr::null()); result.eval_result = simplicity_evalTCOProgram(dag, type_dag, len, budget_ptr, env_ptr); }