You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Change kani::any::<bool>() to make the non-det bit pattern more intuitive #1490
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:
A true value is represented by 0x01, not 0x00.
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.
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
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.
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.
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>().
@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.
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.
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?
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
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.
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.
Activity
[-]Make implementation of `kani::any::<bool>()` closer to the actual Rust bool[/-][+]Make `kani::any::<bool>()` respect the actual Rust bool bit pattern[/+]tedinski commentedon Aug 11, 2022
I don't quite get the "reasoning" section. The function returns only true or false (the result of the
==). The bits of theu8don't really matter except that they're consistently interpreted.Test replay should see whatever bits for the
u8and just continue with thebyte == 1test, returning true or false.sanjit-bhat commentedon Aug 11, 2022
@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
falsevalues. Sinceboolsare used inResultandOption, this also limits our ability to test certain values for those types.zhassan-aws commentedon Aug 11, 2022
This sounds to me like a bug/limitation in the executable trace implementation? The underlying
u8value is not supposed to be exposed outsidekani::any::<bool>().sanjit-bhat commentedon Aug 11, 2022
@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.celinval commentedon Aug 11, 2022
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.
celinval commentedon Aug 11, 2022
Here is a test that shows that
kani::any::<bool>does respect the Rust bool bit pattern:sanjit-bhat commentedon Aug 11, 2022
Hm, that verifies, even if I replace
aswithtransmute.I'm confused why that's the case. I thought the underlying bit pattern (the
let byte = u8::any()) can be any arbitraryu8. Why is it that theseassertswork out?zhassan-aws commentedon Aug 11, 2022
It definitely can be.
Because
kani::any::<bool>()does not returnbyte; it returnsbyte == 0which can only be0x00or0x01.[-]Make `kani::any::<bool>()` respect the actual Rust bool bit pattern[/-][+]Change `kani::any::<bool>()` to make the non-det bit pattern more intuitive[/+]sanjit-bhat commentedon Aug 11, 2022
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--visualizeor--gen-exe-trace, this change will make the non-det byte used to generatebools look more like the byte underlying Rustbools.sanjit-bhat commentedon Aug 17, 2022
Performance eval of switching to new
any::<bool>()Configurations:
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, wheregtimeis the GNUtimeprogram.Old bool
New bool