Skip to content

Commit 68aec41

Browse files
authored
Add free(0) to codegen of loop contracts (#3637)
Add `free(0)` to the codegen result of loop contracts to avoid dropping the body of `free`, which is required by DFCC. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
1 parent 1aad10c commit 68aec41

File tree

6 files changed

+13
-24
lines changed

6 files changed

+13
-24
lines changed

kani-compiler/src/codegen_cprover_gotoc/overrides/hooks.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,19 @@ impl GotocHook for LoopInvariantRegister {
573573
) -> Stmt {
574574
let loc = gcx.codegen_span_stable(span);
575575
let func_exp = gcx.codegen_func_expr(instance, loc);
576-
577-
Stmt::goto(bb_label(target.unwrap()), loc)
578-
.with_loop_contracts(func_exp.call(fargs).cast_to(Type::CInteger(CIntType::Bool)))
576+
// Add `free(0)` to make sure the body of `free` won't be dropped to
577+
// satisfy the requirement of DFCC.
578+
Stmt::block(
579+
vec![
580+
BuiltinFn::Free
581+
.call(vec![Expr::pointer_constant(0, Type::void_pointer())], loc)
582+
.as_stmt(loc),
583+
Stmt::goto(bb_label(target.unwrap()), loc).with_loop_contracts(
584+
func_exp.call(fargs).cast_to(Type::CInteger(CIntType::Bool)),
585+
),
586+
],
587+
loc,
588+
)
579589
}
580590
}
581591

tests/expected/loop-contract/count_zero.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,5 @@ const fn count_zero(slice: &[u8]) -> usize {
2727

2828
#[kani::proof]
2929
pub fn check_counter() {
30-
// Needed to avoid having `free` be removed as unused function. This is
31-
// because DFCC contract enforcement assumes that a definition for `free`
32-
// exists.
33-
let _ = Box::new(10);
3430
assert_eq!(count_zero(&[1, 2, 3]), 0)
3531
}

tests/expected/loop-contract/memchar_naive.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313

1414
#[kani::proof]
1515
fn memchar_naive_harness() {
16-
// Needed to avoid having `free` be removed as unused function. This is
17-
// because DFCC contract enforcement assumes that a definition for `free`
18-
// exists.
19-
let _ = Box::new(10);
2016
let text = [1, 2, 3, 4, 5];
2117
let x = 5;
2218
let mut i = 0;

tests/expected/loop-contract/multiple_loops.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ fn simple_while_loops() {
4444

4545
#[kani::proof]
4646
fn multiple_loops_harness() {
47-
// Needed to avoid having `free` be removed as unused function. This is
48-
// because DFCC contract enforcement assumes that a definition for `free`
49-
// exists.
50-
let _ = Box::new(10);
5147
multiple_loops();
5248
simple_while_loops();
5349
}

tests/expected/loop-contract/simple_while_loop.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@
99
#![feature(proc_macro_hygiene)]
1010

1111
#[kani::proof]
12-
#[kani::solver(kissat)]
1312
fn simple_while_loop_harness() {
14-
// Needed to avoid having `free` be removed as unused function. This is
15-
// because DFCC contract enforcement assumes that a definition for `free`
16-
// exists.
17-
let _ = Box::new(10);
1813
let mut x: u8 = kani::any_where(|i| *i >= 2);
1914

2015
#[kani::loop_invariant(x >= 2)]

tests/expected/loop-contract/small_slice_eq.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ unsafe fn small_slice_eq(x: &[u8], y: &[u8]) -> bool {
4444

4545
#[kani::proof]
4646
fn small_slice_eq_harness() {
47-
// Needed to avoid having `free` be removed as unused function. This is
48-
// because DFCC contract enforcement assumes that a definition for `free`
49-
// exists.
50-
let _ = Box::new(10);
5147
const ARR_SIZE: usize = 2000;
5248
let x: [u8; ARR_SIZE] = kani::any();
5349
let y: [u8; ARR_SIZE] = kani::any();

0 commit comments

Comments
 (0)