Skip to content

Commit 268b6c3

Browse files
committed
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 6a7b97c commit 268b6c3

File tree

14 files changed

+73
-48
lines changed

14 files changed

+73
-48
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/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/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

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

src/ansi-c/cprover_builtin_headers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void __CPROVER_loop_entry(const void *);
5959
__CPROVER_bool __CPROVER_LIVE_OBJECT(const void *);
6060
__CPROVER_bool __CPROVER_WRITEABLE_OBJECT(const void *);
6161
__CPROVER_size_t __CPROVER_POINTER_OBJECT(const void *);
62-
__CPROVER_ssize_t __CPROVER_POINTER_OFFSET(const void *);
62+
__CPROVER_size_t __CPROVER_POINTER_OFFSET(const void *);
6363
__CPROVER_size_t __CPROVER_OBJECT_SIZE(const void *);
6464
__CPROVER_bool __CPROVER_DYNAMIC_OBJECT(const void *);
6565
__CPROVER_bool __CPROVER_pointer_in_range(const void *, const void *, const void *);

src/ansi-c/goto_check_c.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ void goto_check_ct::bounds_check_index(
15711571
effective_offset, p_offset.type())};
15721572
}
15731573

1574-
exprt zero = from_integer(0, ode.offset().type());
1574+
exprt zero = from_integer(0, effective_offset.type());
15751575

15761576
// the final offset must not be negative
15771577
binary_relation_exprt inequality(

src/ansi-c/library/cprover_contracts.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ __CPROVER_HIDE:;
786786
size < __CPROVER_max_malloc_size,
787787
"CAR size is less than __CPROVER_max_malloc_size");
788788

789-
__CPROVER_ssize_t offset = __CPROVER_POINTER_OFFSET(ptr);
789+
__CPROVER_size_t offset = __CPROVER_POINTER_OFFSET(ptr);
790790

791791
__CPROVER_assert(
792792
!(offset > 0) |

src/pointer-analysis/value_set_dereference.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,9 @@ value_set_dereferencet::valuet value_set_dereferencet::build_reference_to(
517517

518518
const index_exprt index_expr(
519519
symbol_expr,
520-
pointer_offset(pointer_expr),
520+
typecast_exprt::conditional_cast(
521+
pointer_offset(pointer_expr),
522+
to_array_type(memory_symbol.type).index_type()),
521523
to_array_type(memory_symbol.type).element_type());
522524

523525
valuet result;
@@ -532,7 +534,9 @@ value_set_dereferencet::valuet value_set_dereferencet::build_reference_to(
532534
{
533535
const index_exprt index_expr(
534536
symbol_expr,
535-
pointer_offset(pointer_expr),
537+
typecast_exprt::conditional_cast(
538+
pointer_offset(pointer_expr),
539+
to_array_type(memory_symbol.type).index_type()),
536540
to_array_type(memory_symbol.type).element_type());
537541

538542
valuet result;
@@ -768,7 +772,11 @@ bool value_set_dereferencet::memory_model_bytes(
768772
{
769773
// yes, can use 'index', but possibly need to convert
770774
result = typecast_exprt::conditional_cast(
771-
index_exprt(value, offset, to_array_type(from_type).element_type()),
775+
index_exprt(
776+
value,
777+
typecast_exprt::conditional_cast(
778+
offset, to_array_type(from_type).index_type()),
779+
to_array_type(from_type).element_type()),
772780
to_type);
773781
}
774782
else

0 commit comments

Comments
 (0)