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

Value-dependent failure with vec! initialization #3772

Open
lancelet opened this issue Dec 11, 2024 · 2 comments
Open

Value-dependent failure with vec! initialization #3772

lancelet opened this issue Dec 11, 2024 · 2 comments
Labels
[C] Bug This is a bug. Something isn't working. T-User Tag user issues / requests

Comments

@lancelet
Copy link

I tried this code:

#[kani::proof]
fn vec_failure() {
    let value: u8 = 1;   /* set to zero and it passes */
    let count: u16 = kani::any();
    let vector: Vec<u8> = vec![value; count as usize];
}

using the following command line invocation:

cargo kani

with Kani version:

$ cargo kani --version
cargo-kani 0.56.0

$ kani --version
kani 0.56.0

When it is run with value = 1, it produces the following failure:

Check 168: std::intrinsics::write_bytes::<u8>.precondition_instance.1
	 - Status: FAILURE
	 - Description: "memset destination region writeable"
	 - Location: ../../../runner/.rustup/toolchains/nightly-2024-10-03-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/intrinsics.rs:3496:9 in function std::intrinsics::write_bytes::<u8>

But when run with value = 0, it succeeds.

I expected uniform behavior in both cases, but perhaps the intrinsics special-case zero somehow?

@lancelet lancelet added the [C] Bug This is a bug. Something isn't working. label Dec 11, 2024
@lancelet
Copy link
Author

lancelet commented Dec 11, 2024

Workaround

In case anyone encounters this... I found that I could get this to pass by mocking out std::intrinsics::write_bytes:

    fn mock_write_bytes<T>(dst: *mut T, val: u8, count: usize) {}

    #[kani::proof]
    #[kani::stub(std::intrinsics::write_bytes, mock_write_bytes)]
    fn vec_failure() {
        let value: u8 = 1; /* set to zero and it passes */
        let count: u16 = kani::any();
        let vector: Vec<u8> = vec![value; count as usize];
    }

And then run with:

cargo kani --harness vec_failure -Z stubbing

However, because stubbing requires --harness, this isn't very scalable.

@zhassan-aws
Copy link
Contributor

Hi @lancelet. Thanks for creating this issue. This is likely a duplicated of #90.

Thanks for the workaround!

@carolynzech carolynzech added the T-User Tag user issues / requests label Dec 13, 2024
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. T-User Tag user issues / requests
Projects
None yet
Development

No branches or pull requests

3 participants