Skip to content

Commit 873daa9

Browse files
authored
Merge pull request #5321 from danpoe/tests/pointer-primitives
Tests to check inconsistent assumptions involving __CPROVER_r_ok
2 parents ac0402f + b1be232 commit 873daa9

File tree

30 files changed

+360
-1
lines changed

30 files changed

+360
-1
lines changed

regression/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ add_subdirectory(goto-cc-file-local)
5656
add_subdirectory(linking-goto-binaries)
5757
add_subdirectory(symtab2gb)
5858
add_subdirectory(validate-trace-xml-schema)
59+
add_subdirectory(cbmc-primitives)
5960

6061
if(WITH_MEMORY_ANALYZER)
6162
add_subdirectory(snapshot-harness)

regression/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ DIRS = cbmc \
3333
solver-hardness \
3434
goto-ld \
3535
validate-trace-xml-schema \
36-
# Empty last line
36+
cbmc-primitives \
37+
# Empty last line
3738

3839
ifeq ($(OS),Windows_NT)
3940
detected_OS := Windows
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_test_pl_tests(
2+
"$<TARGET_FILE:cbmc>"
3+
)

regression/cbmc-primitives/Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
default: tests.log
2+
3+
test:
4+
@../test.pl -e -p -c ../../../src/cbmc/cbmc
5+
6+
tests.log: ../test.pl
7+
@../test.pl -e -p -c ../../../src/cbmc/cbmc
8+
9+
show:
10+
@for dir in *; do \
11+
if [ -d "$$dir" ]; then \
12+
vim -o "$$dir/*.c" "$$dir/*.out"; \
13+
fi; \
14+
done;
15+
16+
clean:
17+
find -name '*.out' -execdir $(RM) '{}' \;
18+
$(RM) tests.log
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
void main()
5+
{
6+
char *p1;
7+
__CPROVER_assume(__CPROVER_r_ok(p1 - 1, 1));
8+
*p1;
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
KNOWNBUG
2+
main.c
3+
--pointer-check --no-simplify --no-propagation
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring
8+
--
9+
Crashes during the flattening, issue #5328
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
void main()
5+
{
6+
char *p;
7+
8+
{
9+
char c;
10+
p = &c;
11+
}
12+
13+
__CPROVER_assume(__CPROVER_r_ok(p, 1));
14+
assert(0); // fails
15+
*p; // succeeds
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FUTURE
2+
main.c
3+
--pointer-check --no-simplify --no-propagation
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Tests that an inconsistent assumption involving a pointer to an out of scope
11+
variable and __CPROVER_r_ok() can be detected via assert(0). We use
12+
--no-simplify and --no-propagation to ensure that the case is not solved by the
13+
constant propagation and thus tests the constraint encoding.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FUTURE
2+
main.c
3+
--function test_global_pointer --pointer-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Tests that an inconsistent assumption involving a pointer to an out of scope
11+
variable and __CPROVER_r_ok() can be detected via assert(0).
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
void main()
5+
{
6+
char *p = malloc(1);
7+
free(p);
8+
9+
__CPROVER_assume(__CPROVER_r_ok(p, 1));
10+
assert(0); // fails
11+
*p; // succeeds
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FUTURE
2+
main.c
3+
--pointer-check --no-simplify --no-propagation
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Tests that an inconsistent assumption involving a pointer initialized to an
11+
integer address not pointing to memory and __CPROVER_r_ok() can be detected via
12+
assert(0). 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.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FUTURE
2+
main.c
3+
--pointer-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Tests that an inconsistent assumption involving a pointer initialized to an
11+
integer address not pointing to memory and __CPROVER_r_ok() can be detected via
12+
assert(0).
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
void main()
5+
{
6+
// pointer with object bits = 123 and offset = 123
7+
// this is to test with a pointer that does not point to valid memory, but the
8+
// value of which is otherwise not special in any way (like for example a null
9+
// pointer)
10+
char *p = (size_t)123 << (sizeof(char *) * 8 - 8) | 123;
11+
__CPROVER_assume(__CPROVER_r_ok(p, 10));
12+
assert(0); // fails
13+
*p; // succeeds
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FUTURE
2+
main.c
3+
--pointer-check --no-simplify --no-propagation
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Tests that an inconsistent assumption involving a pointer initialized to an
11+
integer address not pointing to memory and __CPROVER_r_ok() can be detected via
12+
assert(0). 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.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FUTURE
2+
main.c
3+
--pointer-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Tests that an inconsistent assumption involving a pointer initialized to an
11+
integer address not pointing to memory and __CPROVER_r_ok() can be detected via
12+
assert(0).
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
void main()
5+
{
6+
// special value of the invalid pointer (object bits = 1 and offset = 0), as
7+
// checked for by __CPROVER_is_invalid_pointer()
8+
char *p = (size_t)1 << (sizeof(char *) * 8 - 8);
9+
__CPROVER_assume(__CPROVER_r_ok(p, 10));
10+
assert(0); // fails
11+
*p; // succeeds
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CORE
2+
main.c
3+
--pointer-check --no-simplify --no-propagation
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Tests that an inconsistent assumption involving the special invalid pointer
11+
value and __CPROVER_r_ok() can be detected via assert(0). We use --no-simplify
12+
and --no-propagation to ensure that the case is not solved by the constant
13+
propagation and thus tests the constraint encoding.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--pointer-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Tests that an inconsistent assumption involving the special invalid pointer
11+
value and __CPROVER_r_ok() can be detected via assert(0).
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <assert.h>
2+
3+
char *nondet();
4+
5+
void main()
6+
{
7+
char *p = nondet();
8+
__CPROVER_assume(__CPROVER_r_ok(p, 10));
9+
assert(0); // fails
10+
*p; // succeeds
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FUTURE
2+
main.c
3+
--pointer-check --no-simplify --no-propagation
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Tests that an inconsistent assumption involving a nondet pointer and
11+
__CPROVER_r_ok() can be detected via assert(0). We use --no-simplify and
12+
--no-propagation to ensure that the case is not solved by the constant
13+
propagation and thus tests the constraint encoding.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FUTURE
2+
main.c
3+
--pointer-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Tests that an inconsistent assumption involving a nondet pointer and
11+
__CPROVER_r_ok() can be detected via assert(0).
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
void main()
5+
{
6+
char *p = NULL;
7+
assert(!__CPROVER_r_ok(p, 10));
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
main.c
3+
--pointer-check --no-simplify --no-propagation
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Tests that __CPROVER_r_ok() on a null pointers is false. We use --no-simplify
11+
and --no-propagation to ensure that the case is not solved by the constant
12+
propagation and thus tests the constraint encoding.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--pointer-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Tests that __CPROVER_r_ok() on a null pointers is false
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
char c1;
5+
6+
void main()
7+
{
8+
char c2;
9+
10+
char *p1 = &c1;
11+
assert(__CPROVER_r_ok(p1, 1));
12+
13+
char *p2 = &c2;
14+
assert(__CPROVER_r_ok(p2, 1));
15+
16+
char *p3 = malloc(1);
17+
__CPROVER_assume(p1 != NULL);
18+
assert(__CPROVER_r_ok(p3, 1));
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CORE
2+
main.c
3+
--pointer-check --no-simplify --no-propagation
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Tests that __CPROVER_r_ok() is true for valid pointers to global, local, or
11+
malloced memory. We use --no-simplify and --no-propagation to ensure that the
12+
case is not solved by the constant propagation and thus tests the constraint
13+
encoding.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--pointer-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Tests that __CPROVER_r_ok() is true for valid pointers to global, local, or
11+
malloced memory
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
char c1;
5+
6+
void main()
7+
{
8+
// We check the conditions for a pointer to global static memory (p1), a
9+
// pointer to local memory (p2), and a pointer to dynamic memory (p3).
10+
// For each pointer, we check:
11+
// (1) whether it is unsafe to access one byte more than was allocated
12+
// (2) whether it is unsafe to access one byte before the allocated memory
13+
// (3) whether it is unsafe to access one byte after the allocated memory
14+
15+
char c2;
16+
17+
char *p1 = &c1;
18+
19+
assert(!__CPROVER_r_ok(p1, 2));
20+
assert(!__CPROVER_r_ok(p1 - 1, 1));
21+
assert(!__CPROVER_r_ok(p1 + 1, 1));
22+
23+
char *p2 = &c2;
24+
25+
assert(!__CPROVER_r_ok(p2, 2));
26+
assert(!__CPROVER_r_ok(p2 - 1, 1));
27+
assert(!__CPROVER_r_ok(p2 + 1, 1));
28+
29+
char *p3 = malloc(1);
30+
31+
assert(!__CPROVER_r_ok(p3, 2));
32+
assert(!__CPROVER_r_ok(p3 - 1, 1));
33+
assert(!__CPROVER_r_ok(p3 + 1, 1));
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
KNOWNBUG
2+
main.c
3+
--pointer-check --no-simplify --no-propagation
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Tests that __CPROVER_r_ok() is false for a pointer to a valid memory object,
11+
though potentially with an offset that is out of bounds. We use --no-simplify
12+
and --no-propagation to ensure that the case is not solved by the constant
13+
propagation and thus tests the constraint encoding.

0 commit comments

Comments
 (0)