Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limitation in Annotating Function Contracts for Pointers to dyn Trait #3763

Open
xsxszab opened this issue Dec 5, 2024 · 0 comments
Open
Labels
[C] Bug This is a bug. Something isn't working.

Comments

@xsxszab
Copy link

xsxszab commented Dec 5, 2024

Description
While this issue is not a direct bug in Kani, it impacts the usability of Kani in scenarios involving pointers to dyn Trait. Specifically, in challenge 3 of the verify-rust-std repository, we encountered this issue while verifying function contracts for <dyn Trait> pointee types.
Consider the following example:

trait TestTrait {}
struct TestStruct {value: i64,}
impl TestTrait for TestStruct {}

let test_struct = TestStruct { value: 42 };
let trait_object: &dyn TestTrait = &test_struct;
let test_ptr: *const dyn TestTrait = trait_object;
unsafe {test_ptr.byte_offset(2);}

This code runs successfully and calls the byte_offset method on a pointer to dyn TestTrait. However, when trying to annotate this method using the #[kani::proof_for_contract] attribute, as shown below:

#[kani::proof_for_contract(<*const dyn TestTrait>::byte_offset)]

The following compilation error occurs:

Failed to resolve `<*const dyn TestTrait>::byte_offset` for `proof_for_contract`: Expected a type, but found trait object paths `dyn TestTrait`

Analysis
The issue appears to stem from a limitation in Rust, where attributes can only accept constant expressions and cannot handle dynamic dispatch. Since dyn Trait involves dynamic dispatch, Rust cannot resolve it in the attribute's context.
To work around this, we have adopted the following approach:

  • Instead of annotating the method with pointers to dyn Trait, we annotate it with its concrete base type:
#[kani::proof_for_contract(<*const TestStruct>::byte_offset)]

While this workaround works, it diverges from the intent of verifying the contract directly for <*const dyn Trait>::byte_offset.

Questions

  1. Is the described workaround of annotating with the base type acceptable from Kani's perspective?
  2. Is there an alternative approach or future support planned that would enable direct annotation for pointers to dyn Trait?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[C] Bug This is a bug. Something isn't working.
Projects
None yet
Development

No branches or pull requests

1 participant