File tree Expand file tree Collapse file tree 6 files changed +105
-0
lines changed
regression/cbmc-incr-smt2/bitvector-bitwise-operators Expand file tree Collapse file tree 6 files changed +105
-0
lines changed Original file line number Diff line number Diff line change 1+ CORE
2+ bitwise_ops.c
3+ --incremental-smt2-solver 'z3 --smt2 -in' --slice-formula
4+ \[main\.assertion\.1\] line \d+ This is going to fail for bit-opposites: FAILURE
5+ \[main\.assertion\.2\] line \d+ This is going to hold for all values != 0: SUCCESS
6+ \[main\.assertion\.3\] line \d+ This is going to fail for the same value in A and B: FAILURE
7+ \[main\.assertion\.4\] line \d+ This will fail for the the same value in A and B: FAILURE
8+ ^EXIT=10$
9+ ^SIGNAL=0$
10+ --
11+ --
Original file line number Diff line number Diff line change 1+ int main ()
2+ {
3+ int a ;
4+ int b ;
5+
6+ __CPROVER_assume (a != 0 );
7+
8+ // This is going to be failing for values of `0000` and `1111` (as an example),
9+ // as the bitwise-& of that will produce 0, failing this assertion.
10+ __CPROVER_assert (a & b , "This is going to fail for bit-opposites" );
11+ // This will always be true, because bitwise-or allows a 1 at a bit
12+ // that either A or B have set as one, and with an assumption of
13+ // a != 0, there's always going to be at least 1 bit set, allowing
14+ // the assertion below to evaluate to true.
15+ __CPROVER_assert (a | b , "This is going to hold for all values != 0" );
16+ // This will fail for the same value, as an XOR of the bits will
17+ // result in `0`, resulting in the assertion failure.
18+ __CPROVER_assert (
19+ a ^ b , "This is going to fail for the same value in A and B" );
20+ // This will fail for the exact same value of A and B, as
21+ // NOT(A) will flip all the bits, resulting in the equality
22+ // below to be false for the assertion.
23+ __CPROVER_assert (~a == b , "This will fail for the the same value in A and B" );
24+ }
Original file line number Diff line number Diff line change 1+ int main ()
2+ {
3+ int starting_val = 1 ;
4+ int shifting_places ;
5+ __CPROVER_assume (shifting_places > 0 );
6+ __CPROVER_assume (shifting_places < 32 );
7+ int result = starting_val << shifting_places ;
8+
9+ __CPROVER_assert (result > 1 , "Shifted result should be greater than one" );
10+ }
Original file line number Diff line number Diff line change 1+ CORE
2+ shift_left.c
3+ --incremental-smt2-solver 'z3 --smt2 -in' --slice-formula
4+ \[main\.assertion\.1\] line \d Shifted result should be greater than one: FAILURE
5+ ^EXIT=10$
6+ ^SIGNAL=0$
7+ --
8+ --
Original file line number Diff line number Diff line change 1+ #include <stdint.h>
2+
3+ // In C, whether a right-shift is arithmetic or logical depends on the
4+ // original type being shifted. An unsigned value will be shifted to
5+ // the right in a logical manner (this assigns `0` to the leftmost bit).
6+ // If the type is signed, right shift will assign the sign bit to the
7+ // leftmost digit.
8+
9+ int main ()
10+ {
11+ int first ;
12+ uint8_t second ;
13+
14+ int place ;
15+ __CPROVER_assume (place >= 1 );
16+
17+ int result_signed = first >> place ;
18+ uint8_t result_unsigned = second >> place ;
19+
20+ // This assertion captures the intend of the expected behaviour of
21+ // bit-shifting an unsigned int (logical shift)
22+ __CPROVER_assert (
23+ result_unsigned != 64 ,
24+ "Right shifting a uint with leftmost bit set is a logical shift" );
25+ // The following assertions capture the expected behaviour of
26+ // a right logical (in the case of a signed positive int) and
27+ // arithmetic shift (in the case of a signed negative int).
28+ if (first >= 0 )
29+ {
30+ __CPROVER_assert (
31+ result_signed >= 0 ,
32+ "Right shifting a positive number has a lower bound of 0" );
33+ }
34+ else
35+ {
36+ __CPROVER_assert (
37+ result_signed <= -1 ,
38+ "Right shifting a negative number has a lower bound value of -1" );
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ CORE
2+ shift_right.c
3+ --incremental-smt2-solver 'z3 --smt2 -in' --slice-formula --trace
4+ \[main\.assertion\.1\] line \d+ Right shifting a uint with leftmost bit set is a logical shift: FAILURE
5+ \[main\.assertion\.2\] line \d+ Right shifting a positive number has a lower bound of 0: SUCCESS
6+ \[main\.assertion\.3\] line \d+ Right shifting a negative number has a lower bound value of -1: SUCCESS
7+ second=128
8+ result_unsigned=64
9+ ^EXIT=10$
10+ ^SIGNAL=0$
11+ --
12+ --
You can’t perform that action at this time.
0 commit comments