Skip to content

Change kani::any::<bool>() to make the non-det bit pattern more intuitive #1490

Closed

Description

@sanjit-bhat

TLDR

This issue proposes changing our implementation of kani::any::<bool>() from:

impl Arbitrary for bool {
#[inline(always)]
fn any() -> Self {
let byte = u8::any();
byte == 0
}
}

to something like

impl Arbitrary for bool {
    #[inline(always)]
    fn any() -> Self {
        let byte = u8::any();
        crate::assume(byte < 2);
        byte == 1
    }
}

Reasoning

Our current implementation of kani::any::<bool>() has the correct API semantics of a bool. I.e., that a bool can be either true/false. However, it doesn't map to the underlying bit pattern of Rust bools:

An object with the boolean type has a size and alignment of 1 each. The value false has the bit pattern 0x00 and the value true has the bit pattern 0x01. It is undefined behavior for an object with the boolean type to have any other bit pattern.

The Rust spec has the following differences:

  1. A true value is represented by 0x01, not 0x00.
  2. A false value is represented by only 0x00, not [0x01, 0xFF].

This difference has an impact when playing back deterministic test cases. In our tests for playing back bool's, we can never set an expected deterministic bit pattern for false. There are multiple possible bit patterns, and we don't know which one the solver will choose.

Callouts

The proposed change would add an extra kani::assume() for every bool generated. This might slow down verification time. We should performance benchmark this change before implementing it.

Activity

  1. changed the title [-]Make implementation of `kani::any::<bool>()` closer to the actual Rust bool[/-] [+]Make `kani::any::<bool>()` respect the actual Rust bool bit pattern[/+] on Aug 11, 2022
  2. tedinski commented on Aug 11, 2022

    @tedinski
    Contributor

    I don't quite get the "reasoning" section. The function returns only true or false (the result of the ==). The bits of the u8 don't really matter except that they're consistently interpreted.

    Test replay should see whatever bits for the u8 and just continue with the byte == 1 test, returning true or false.

  3. sanjit-bhat commented on Aug 11, 2022

    @sanjit-bhat
    ContributorAuthor

    @tedinski you're right that the bits are consistently interpreted in the current implementation.

    The bit patterns are relevant in the following test:

    https://github.com/model-checking/kani/pull/1488/files#diff-d6edfd434ccfd2ab84a988fbf2909ad26a6e1d7553f909d268a463e29faa9d03R9-R10

    with expected output here:

    https://github.com/model-checking/kani/pull/1488/files#diff-0f14a3452f9e948e47159f2f28eddffe6f7a951d8c87bea823ba980045a55906R7-R10

    This test asserts that the specific bit patterns are correct. Because of the bit pattern mismatch, we aren't able to test for false values. Since bools are used in Result and Option, this also limits our ability to test certain values for those types.

  4. zhassan-aws commented on Aug 11, 2022

    @zhassan-aws
    Contributor

    This sounds to me like a bug/limitation in the executable trace implementation? The underlying u8 value is not supposed to be exposed outside kani::any::<bool>().

  5. sanjit-bhat commented on Aug 11, 2022

    @sanjit-bhat
    ContributorAuthor

    @zhassan-aws It's a limitation of the test cases for the executable trace feature. This limitation might be removed in the future once I add additional test functionality to run the generated deterministic tests and check that the interpreted values are correct.

    However, is there any reason why we shouldn't do this? Have we tested the performance of the alternate implementation? IMO, we should be doing it the way of the alternate implementation (it's closer to how Rust implements bools), unless there's a significant loss in performance.

  6. celinval commented on Aug 11, 2022

    @celinval
    Contributor

    Do you mind if we edit the tittle? I don't think this is not a bug and kani::any::() does respect the actual Rust bool bit pattern.

    That said, if there is no performance impact, I don't mind changing this to match 0 / 1. I do agree that it might make it simple for users to read the trace (with --visualize) as well as the executable trace implementation.

  7. celinval commented on Aug 11, 2022

    @celinval
    Contributor

    Here is a test that shows that kani::any::<bool> does respect the Rust bool bit pattern:

    #[kani::proof]
    fn check_bool_bits() {
        let b : bool = kani::any();
        let bits = b as u8;
        assert!(bits == 0x0 || bits == 0x1);
    }
  8. sanjit-bhat commented on Aug 11, 2022

    @sanjit-bhat
    ContributorAuthor

    Hm, that verifies, even if I replace as with transmute.

    I'm confused why that's the case. I thought the underlying bit pattern (the let byte = u8::any()) can be any arbitrary u8. Why is it that these asserts work out?

  9. zhassan-aws commented on Aug 11, 2022

    @zhassan-aws
    Contributor

    I thought the underlying bit pattern (the let byte = u8::any()) can be any arbitrary u8.

    It definitely can be.

    Why is it that these asserts work out?

    Because kani::any::<bool>() does not return byte; it returns byte == 0 which can only be 0x00 or 0x01.

  10. changed the title [-]Make `kani::any::<bool>()` respect the actual Rust bool bit pattern[/-] [+]Change `kani::any::<bool>()` to make the non-det bit pattern more intuitive[/+] on Aug 11, 2022
  11. sanjit-bhat commented on Aug 11, 2022

    @sanjit-bhat
    ContributorAuthor

    Ah, that makes sense now. Thanks everyone for helping to clarify that!

    @tedinski @celinval @zhassan-aws I updated the title to reflect the fact that this issue is not a problem with the kani::any::<bool>() bit pattern. Rather, it's a usability improvement. If users inspect the output of --visualize or --gen-exe-trace, this change will make the non-det byte used to generate bools look more like the byte underlying Rust bools.

  12. self-assigned this
    on Aug 11, 2022
  13. moved this to In Progress in Kani v0.9on Aug 11, 2022
  14. sanjit-bhat commented on Aug 17, 2022

    @sanjit-bhat
    ContributorAuthor

    Performance eval of switching to new any::<bool>()

    Configurations:

    1. rectangle example: stretched_rectangle_can_hold_original_fixed
    2. firecracker: requirement_2642
    3. vecdeque-cve: minimal_example_with_cve_fixed

    For each configuration, I show numbers for a) verification time, b) elapsed (wall clock) time, and c) max resident set size. b) and c) are from gtime -v, where gtime is the GNU time program.

    Old bool

    1. rect example: 0.08s, 1.19s, 106mb
    2. firecracker: 3.06s, 4.77s, 127mb
    3. vecdeque-cve: 0.37s, 3.51s, 153mb

    New bool

    1. rect example: 0.09s, 1.25s, 110mb
    2. firecracker: 3.13s, 4.88s, 132mb
    3. vecdeque-cve: 0.39s, 3.62s, 146mb
  15. Repository owner moved this from In Progress to Done in Kani v0.9on Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions