Skip to content

Commit f8cc13a

Browse files
kroeningtautschnig
authored andcommitted
make pointer_offset_exprt unsigned
This commit changes the type of __CPROVER_POINTER_OFFSET from ssize_t to size_t. To match, relational operators on pointers are now unsigned, to enable p+PTRDIFF_MAX+1 > p. The rationale is subtle. The ISO C 11 standard 6.5.8 par 5 suggests that a pair of pointers can be compared if either the pointers point into the object, or one beyond the end of the sequence. The behavior in the case of a pointer that points before the sequence is undefined. There is a similar narrative for pointer arithmetic. A pointer with an offset that has a set most significant bit should thus compare "less than" a pointer with an offset that has a zero MSB. This suggests an unsigned encoding.
1 parent 9b6d003 commit f8cc13a

File tree

20 files changed

+134
-58
lines changed

20 files changed

+134
-58
lines changed

regression/cbmc-incr-smt2/pointers-conversions/byte_update.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Assumption:
1010
file byte_update\.c line \d+ function main
1111
offset >= 0u && offset < 4u
1212
State \d+ file byte_update\.c function main line \d+ thread \d+
13-
byte_extract_little_endian\(x, \(signed long( long)? int\)offset, uint8_t\)=1 \(00000001\)
13+
byte_extract_little_endian\(x, \(.*\)offset, uint8_t\)=1 \(00000001\)
1414
Violated property:
1515
file byte_update.c function main line \d+ thread \d+
1616
assertion x != 256u

regression/cbmc-library/free-01/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CORE
22
main.c
33
--pointer-check --bounds-check --stop-on-fail
4-
free argument must be (NULL or valid pointer|dynamic object)
4+
free argument (must be (NULL or valid pointer|dynamic object)|has offset zero)
55
^EXIT=10$
66
^SIGNAL=0$
77
^VERIFICATION FAILED$

regression/cbmc-library/strlen-02/test-c-with-string-abstraction.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CORE
22
test.c
33
--string-abstraction --show-goto-functions
4-
ASSIGN strlen#return_value := cast\(cast\(\*strlen::s::s#str\.length, signedbv\[.*\]\) - pointer_offset\(strlen::s\), unsignedbv\[.*\]\)
4+
ASSIGN strlen#return_value := \*strlen::s::s#str\.length - pointer_offset\(strlen::s\)
55
^EXIT=0$
66
^SIGNAL=0$
77
--

regression/cbmc-primitives/pointer-offset-01/test-no-cp.desc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ main.c
33
--no-simplify --no-propagation
44
^EXIT=10$
55
^SIGNAL=0$
6-
\[main.assertion.1\] line \d+ assertion __CPROVER_POINTER_OFFSET\(p\) >= 0: FAILURE
6+
\[main.assertion.1\] line \d+ assertion __CPROVER_POINTER_OFFSET\(p\) >= 0: SUCCESS
77
\[main.assertion.2\] line \d+ assertion __CPROVER_POINTER_OFFSET\(p\) < 0: FAILURE
88
--
99
^warning: ignoring
1010
--
11-
Check that both positive and negative offsets can be chosen for uninitialized
12-
pointers. We use --no-simplify and --no-propagation to ensure that the case is
13-
not solved by the constant propagation and thus tests the constraint encoding.
11+
Check that positive offsets can be chosen for uninitialized pointers. We
12+
use --no-simplify and --no-propagation to ensure that the case is not solved
13+
by the constant propagation and thus tests the constraint encoding.

regression/cbmc-primitives/pointer-offset-01/test.desc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ main.c
33

44
^EXIT=10$
55
^SIGNAL=0$
6-
\[main.assertion.1\] line \d+ assertion __CPROVER_POINTER_OFFSET\(p\) >= 0: FAILURE
6+
\[main.assertion.1\] line \d+ assertion __CPROVER_POINTER_OFFSET\(p\) >= 0: SUCCESS
77
\[main.assertion.2\] line \d+ assertion __CPROVER_POINTER_OFFSET\(p\) < 0: FAILURE
88
--
99
^warning: ignoring
1010
--
11-
For uninitialised pointers, CBMC chooses a nondeterministic value (though no memory
12-
is allocated). Since the offset of pointers is signed, negative offsets should be
13-
able to be chosen (along with positive ones) non-deterministically.
14-
`__CPROVER_POINTER_OFFSET` is the CBMC primitive that gets the pointer offset
15-
from the base address of the object. This test guards this, and especially
16-
against the case where we could only observe some cases where offsets were only
17-
positive (in some CI configurations, for instance).
11+
For uninitialised pointers, CBMC chooses a nondeterministic value (though no
12+
memory is allocated). Since the offset of pointers is unsigned, negative
13+
offsets cannot be chosen. `__CPROVER_POINTER_OFFSET` is the CBMC primitive
14+
that gets the pointer offset from the base address of the object. This test
15+
guards this.

regression/cbmc/address_space_size_limit3/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CORE thorough-smt-backend
22
main.i
3-
--32 --little-endian --object-bits 25 --pointer-check
3+
--32 --little-endian --object-bits 26 --pointer-check
44
^EXIT=10$
55
^SIGNAL=0$
66
^VERIFICATION FAILED$

regression/cbmc/memory_allocation2/test.desc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ main.c
33
--bounds-check
44
^EXIT=10$
55
^SIGNAL=0$
6-
^\[main\.array_bounds\.[1-5]\] .*: SUCCESS$
7-
^\[main\.array_bounds\.[67]\] line 38 array.buffer (dynamic object )?upper bound in buffers\[\(signed long (long )?int\)0\]->buffer\[\(signed long (long )?int\)100\]: FAILURE$
8-
^\*\* 1 of 6 failed
6+
^\[main\.array_bounds\.[1-2]\] .*: SUCCESS$
7+
^\[main\.array_bounds\.3\] line 38 array.buffer (dynamic object )?upper bound in buffers\[\(signed long (long )?int\)0\]->buffer\[\(signed long (long )?int\)100\]: FAILURE$
8+
^\*\* 1 of 3 failed
99
^VERIFICATION FAILED$
1010
--
1111
^warning: ignoring
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
int main(void)
5+
{
6+
size_t size;
7+
size_t max_malloc_size = __CPROVER_max_malloc_size;
8+
__CPROVER_assume(0 <= size && size <= max_malloc_size);
9+
char *lb = malloc(size);
10+
size_t offset_lb = __CPROVER_POINTER_OFFSET(lb);
11+
size_t object_lb = __CPROVER_POINTER_OBJECT(lb);
12+
13+
char *ub = lb + size;
14+
size_t offset_ub = __CPROVER_POINTER_OFFSET(ub);
15+
size_t object_ub = __CPROVER_POINTER_OBJECT(ub);
16+
17+
char *ubp1 = lb + size + 1;
18+
size_t offset_ubp1 = __CPROVER_POINTER_OFFSET(ubp1);
19+
size_t object_ubp1 = __CPROVER_POINTER_OBJECT(ubp1);
20+
21+
assert(object_ub == object_lb); // proved
22+
assert(object_ubp1 == object_lb); // proved
23+
assert(ubp1 > ub); // proved
24+
assert(offset_ubp1 > offset_ub); // proved
25+
assert(offset_ubp1 == offset_ub + 1); // falsified
26+
27+
size_t idx;
28+
if(idx < size)
29+
{
30+
char *lb_idx = lb + idx;
31+
void *dummy_ub = ub;
32+
assert(lb <= lb_idx); // proved
33+
assert(lb_idx <= ub); // proved
34+
}
35+
36+
// to produce a trace so that we can observe some intermittent values
37+
assert(size < max_malloc_size);
38+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CORE
2+
main.c
3+
--trace
4+
^\s*ub.*=\(char \*\)&dynamic_object \+ \d+
5+
^\s*offset_ubp1=\d+ul* \(00000000 1[0 ]+1\)$
6+
^VERIFICATION FAILED$
7+
^EXIT=10$
8+
^SIGNAL=0$
9+
--
10+
^warning: ignoring
11+
^\s*ub.*=\(char \*\)&dynamic_object \+ -\d+
12+
^\s*offset_ubp1=\d+ul* \(11111111 1
13+
--
14+
Verifies that all offsets use unsigned arithmetic.

regression/contracts/no_redudant_checks/test.desc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ main.c
2323
^\[main.pointer_arithmetic.15\].*: SUCCESS
2424
^\[main.pointer_arithmetic.16\].*: SUCCESS
2525
^\[main.pointer_arithmetic.17\].*: SUCCESS
26-
^\*\* 0 of 20 failed \(1 iterations\)
26+
^\[main.pointer_arithmetic.18\].*: SUCCESS
27+
^\*\* 0 of 21 failed \(1 iterations\)
2728
^VERIFICATION SUCCESSFUL$
2829
--
2930
^\[main.overflow.\d+\].*: FAILURE
@@ -35,7 +36,7 @@ Checks that assertions generated by the first --pointer-overflow-check:
3536
We expect 20 assertions to be generated:
3637
In the goto-instrument run:
3738
- 2 for using malloc
38-
- 17 caused by --pointer-overflow-check
39+
- 18 caused by --pointer-overflow-check
3940

4041
In the final cbmc run:
4142
- 0 caused by --pointer-overflow-check

0 commit comments

Comments
 (0)