Skip to content

Commit 82893c5

Browse files
authored
dyn Trait proof for contracts of byte_offset_from (#196)
Towards #76 This pull request implements proof for contracts for `byte_offset_from` verifying `dyn Trait` pointee types. Both const and mut versions are included.
1 parent 16a155a commit 82893c5

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

library/core/src/ptr/const_ptr.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -2536,7 +2536,8 @@ mod verify {
25362536
// Trait used exclusively for implementing proofs for contracts for `dyn Trait` type.
25372537
trait TestTrait {}
25382538

2539-
// Struct used exclusively for implementing proofs for contracts for `dyn Trait` type.
2539+
// Struct used exclusively for implementing proof for contracts for `dyn Trait` type.
2540+
#[cfg_attr(kani, derive(kani::Arbitrary))]
25402541
struct TestStruct {
25412542
value: i64,
25422543
}
@@ -2770,4 +2771,30 @@ mod verify {
27702771
generate_const_byte_offset_from_slice_harness!(i64, check_const_byte_offset_from_i64_slice);
27712772
generate_const_byte_offset_from_slice_harness!(i128, check_const_byte_offset_from_i128_slice);
27722773
generate_const_byte_offset_from_slice_harness!(isize, check_const_byte_offset_from_isize_slice);
2774+
2775+
// tracking issue: https://github.com/model-checking/kani/issues/3763
2776+
// Workaround: Directly verifying the method `<*const dyn TestTrait>::byte_offset_from`
2777+
// causes a compilation error. As a workaround, the proof is annotated with the
2778+
// underlying struct type instead.
2779+
#[kani::proof_for_contract(<*const TestStruct>::byte_offset_from)]
2780+
pub fn check_const_byte_offset_from_dyn() {
2781+
const gen_size: usize = mem::size_of::<TestStruct>();
2782+
// Since the pointer generator cannot directly create pointers to `dyn Trait`,
2783+
// we first generate a pointer to the underlying struct and then cast it to a `dyn Trait` pointer.
2784+
let mut generator_caller = PointerGenerator::<gen_size>::new();
2785+
let mut generator_input = PointerGenerator::<gen_size>::new();
2786+
let ptr_caller: *const TestStruct = generator_caller.any_in_bounds().ptr;
2787+
let ptr_input: *const TestStruct = if kani::any() {
2788+
generator_caller.any_alloc_status().ptr
2789+
} else {
2790+
generator_input.any_alloc_status().ptr
2791+
};
2792+
2793+
let ptr_caller = ptr_caller as *const dyn TestTrait;
2794+
let ptr_input = ptr_input as *const dyn TestTrait;
2795+
2796+
unsafe {
2797+
ptr_caller.byte_offset_from(ptr_input);
2798+
}
2799+
}
27732800
}

library/core/src/ptr/mut_ptr.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -2763,6 +2763,7 @@ mod verify {
27632763
trait TestTrait {}
27642764

27652765
// Struct used exclusively for implementing proofs for contracts for `dyn Trait` type.
2766+
#[cfg_attr(kani, derive(kani::Arbitrary))]
27662767
struct TestStruct {
27672768
value: i64,
27682769
}
@@ -2775,7 +2776,7 @@ mod verify {
27752776
/// - `$proof_name`: Specifies the name of the generated proof for contract.
27762777
macro_rules! gen_mut_byte_arith_harness_for_dyn {
27772778
(byte_offset, $proof_name:ident) => {
2778-
//tracking issue: https://github.com/model-checking/kani/issues/3763
2779+
// tracking issue: https://github.com/model-checking/kani/issues/3763
27792780
// Workaround: Directly verifying the method `<*mut dyn TestTrait>::byte_offset`
27802781
// causes a compilation error. As a workaround, the proof is annotated with the
27812782
// underlying struct type instead.
@@ -2793,7 +2794,7 @@ mod verify {
27932794
}
27942795
};
27952796
($fn_name: ident, $proof_name:ident) => {
2796-
//tracking issue: https://github.com/model-checking/kani/issues/3763
2797+
// tracking issue: https://github.com/model-checking/kani/issues/3763
27972798
// Workaround: Directly verifying the method `<*mut dyn TestTrait>::$fn_name`
27982799
// causes a compilation error. As a workaround, the proof is annotated with the
27992800
// underlying struct type instead.
@@ -3013,4 +3014,30 @@ mod verify {
30133014
generate_mut_byte_offset_from_slice_harness!(i64, check_mut_byte_offset_from_i64_slice);
30143015
generate_mut_byte_offset_from_slice_harness!(i128, check_mut_byte_offset_from_i128_slice);
30153016
generate_mut_byte_offset_from_slice_harness!(isize, check_mut_byte_offset_from_isize_slice);
3017+
3018+
// tracking issue: https://github.com/model-checking/kani/issues/3763
3019+
// Workaround: Directly verifying the method `<*mut dyn TestTrait>::byte_offset_from`
3020+
// causes a compilation error. As a workaround, the proof is annotated with the
3021+
// underlying struct type instead.
3022+
#[kani::proof_for_contract(<*mut TestStruct>::byte_offset_from)]
3023+
pub fn check_mut_byte_offset_from_dyn() {
3024+
const gen_size: usize = mem::size_of::<TestStruct>();
3025+
// Since the pointer generator cannot directly create pointers to `dyn Trait`,
3026+
// we first generate a pointer to the underlying struct and then cast it to a `dyn Trait` pointer.
3027+
let mut generator_caller = PointerGenerator::<gen_size>::new();
3028+
let mut generator_input = PointerGenerator::<gen_size>::new();
3029+
let ptr_caller: *mut TestStruct = generator_caller.any_in_bounds().ptr;
3030+
let ptr_input: *mut TestStruct = if kani::any() {
3031+
generator_caller.any_alloc_status().ptr
3032+
} else {
3033+
generator_input.any_alloc_status().ptr
3034+
};
3035+
3036+
let ptr_caller = ptr_caller as *mut dyn TestTrait;
3037+
let ptr_input = ptr_input as *mut dyn TestTrait;
3038+
3039+
unsafe {
3040+
ptr_caller.byte_offset_from(ptr_input);
3041+
}
3042+
}
30163043
}

0 commit comments

Comments
 (0)