Skip to content

Tests to check inconsistent assumptions involving __CPROVER_r_ok #5321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions regression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ add_subdirectory(goto-cc-file-local)
add_subdirectory(linking-goto-binaries)
add_subdirectory(symtab2gb)
add_subdirectory(validate-trace-xml-schema)
add_subdirectory(cbmc-primitives)

if(WITH_MEMORY_ANALYZER)
add_subdirectory(snapshot-harness)
Expand Down
3 changes: 2 additions & 1 deletion regression/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ DIRS = cbmc \
solver-hardness \
goto-ld \
validate-trace-xml-schema \
# Empty last line
cbmc-primitives \
# Empty last line

ifeq ($(OS),Windows_NT)
detected_OS := Windows
Expand Down
3 changes: 3 additions & 0 deletions regression/cbmc-primitives/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_test_pl_tests(
"$<TARGET_FILE:cbmc>"
)
18 changes: 18 additions & 0 deletions regression/cbmc-primitives/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
default: tests.log

test:
@../test.pl -e -p -c ../../../src/cbmc/cbmc

tests.log: ../test.pl
@../test.pl -e -p -c ../../../src/cbmc/cbmc

show:
@for dir in *; do \
if [ -d "$$dir" ]; then \
vim -o "$$dir/*.c" "$$dir/*.out"; \
fi; \
done;

clean:
find -name '*.out' -execdir $(RM) '{}' \;
$(RM) tests.log
9 changes: 9 additions & 0 deletions regression/cbmc-primitives/r_w_ok_bug/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <assert.h>
#include <stdlib.h>

void main()
{
char *p1;
__CPROVER_assume(__CPROVER_r_ok(p1 - 1, 1));
*p1;
}
9 changes: 9 additions & 0 deletions regression/cbmc-primitives/r_w_ok_bug/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
KNOWNBUG
main.c
--pointer-check --no-simplify --no-propagation
^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
--
Crashes during the flattening, issue #5328
16 changes: 16 additions & 0 deletions regression/cbmc-primitives/r_w_ok_inconsistent_dead/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <assert.h>
#include <stdlib.h>

void main()
{
char *p;

{
char c;
p = &c;
}

__CPROVER_assume(__CPROVER_r_ok(p, 1));
assert(0); // fails
*p; // succeeds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FUTURE
main.c
--pointer-check --no-simplify --no-propagation
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Tests that an inconsistent assumption involving a pointer to an out of scope
variable and __CPROVER_r_ok() can be detected via assert(0). We use
--no-simplify and --no-propagation to ensure that the case is not solved by the
constant propagation and thus tests the constraint encoding.
11 changes: 11 additions & 0 deletions regression/cbmc-primitives/r_w_ok_inconsistent_dead/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FUTURE
main.c
--function test_global_pointer --pointer-check
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Tests that an inconsistent assumption involving a pointer to an out of scope
variable and __CPROVER_r_ok() can be detected via assert(0).
12 changes: 12 additions & 0 deletions regression/cbmc-primitives/r_w_ok_inconsistent_deallocated/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <assert.h>
#include <stdlib.h>

void main()
{
char *p = malloc(1);
free(p);

__CPROVER_assume(__CPROVER_r_ok(p, 1));
assert(0); // fails
*p; // succeeds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FUTURE
main.c
--pointer-check --no-simplify --no-propagation
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Tests that an inconsistent assumption involving a pointer initialized to an
integer address not pointing to memory and __CPROVER_r_ok() can be detected via
assert(0). We use --no-simplify and --no-propagation to ensure that the case is
not solved by the constant propagation and thus tests the constraint encoding.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FUTURE
main.c
--pointer-check
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Tests that an inconsistent assumption involving a pointer initialized to an
integer address not pointing to memory and __CPROVER_r_ok() can be detected via
assert(0).
14 changes: 14 additions & 0 deletions regression/cbmc-primitives/r_w_ok_inconsistent_integer/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <assert.h>
#include <stdlib.h>

void main()
{
// pointer with object bits = 123 and offset = 123
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the improvement - as the reader, I'm still left with the question of why you're constructing such a pointer. I think this would still be improved with a comment as to why this is an interesting pointer. Is it because it tricks cbmc into thinking it is valid?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's a pointer that doesn't point to valid memory but isn't special in any other way. Added a description.

// this is to test with a pointer that does not point to valid memory, but the
// value of which is otherwise not special in any way (like for example a null
// pointer)
char *p = (size_t)123 << (sizeof(char *) * 8 - 8) | 123;
__CPROVER_assume(__CPROVER_r_ok(p, 10));
assert(0); // fails
*p; // succeeds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FUTURE
main.c
--pointer-check --no-simplify --no-propagation
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Tests that an inconsistent assumption involving a pointer initialized to an
integer address not pointing to memory and __CPROVER_r_ok() can be detected via
assert(0). We use --no-simplify and --no-propagation to ensure that the case is
not solved by the constant propagation and thus tests the constraint encoding.
12 changes: 12 additions & 0 deletions regression/cbmc-primitives/r_w_ok_inconsistent_integer/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FUTURE
main.c
--pointer-check
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Tests that an inconsistent assumption involving a pointer initialized to an
integer address not pointing to memory and __CPROVER_r_ok() can be detected via
assert(0).
12 changes: 12 additions & 0 deletions regression/cbmc-primitives/r_w_ok_inconsistent_invalid/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <assert.h>
#include <stdlib.h>

void main()
{
// special value of the invalid pointer (object bits = 1 and offset = 0), as
// checked for by __CPROVER_is_invalid_pointer()
char *p = (size_t)1 << (sizeof(char *) * 8 - 8);
__CPROVER_assume(__CPROVER_r_ok(p, 10));
assert(0); // fails
*p; // succeeds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CORE
main.c
--pointer-check --no-simplify --no-propagation
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Tests that an inconsistent assumption involving the special invalid pointer
value and __CPROVER_r_ok() can be detected via assert(0). We use --no-simplify
and --no-propagation to ensure that the case is not solved by the constant
propagation and thus tests the constraint encoding.
11 changes: 11 additions & 0 deletions regression/cbmc-primitives/r_w_ok_inconsistent_invalid/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CORE
main.c
--pointer-check
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Tests that an inconsistent assumption involving the special invalid pointer
value and __CPROVER_r_ok() can be detected via assert(0).
11 changes: 11 additions & 0 deletions regression/cbmc-primitives/r_w_ok_inconsistent_nondet/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <assert.h>

char *nondet();

void main()
{
char *p = nondet();
__CPROVER_assume(__CPROVER_r_ok(p, 10));
assert(0); // fails
*p; // succeeds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FUTURE
main.c
--pointer-check --no-simplify --no-propagation
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Tests that an inconsistent assumption involving a nondet pointer and
__CPROVER_r_ok() can be detected via assert(0). We use --no-simplify and
--no-propagation to ensure that the case is not solved by the constant
propagation and thus tests the constraint encoding.
11 changes: 11 additions & 0 deletions regression/cbmc-primitives/r_w_ok_inconsistent_nondet/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FUTURE
main.c
--pointer-check
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Tests that an inconsistent assumption involving a nondet pointer and
__CPROVER_r_ok() can be detected via assert(0).
8 changes: 8 additions & 0 deletions regression/cbmc-primitives/r_w_ok_null/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <assert.h>
#include <stdlib.h>

void main()
{
char *p = NULL;
assert(!__CPROVER_r_ok(p, 10));
}
12 changes: 12 additions & 0 deletions regression/cbmc-primitives/r_w_ok_null/test-no-cp.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CORE
main.c
--pointer-check --no-simplify --no-propagation
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Tests that __CPROVER_r_ok() on a null pointers is false. We use --no-simplify
and --no-propagation to ensure that the case is not solved by the constant
propagation and thus tests the constraint encoding.
10 changes: 10 additions & 0 deletions regression/cbmc-primitives/r_w_ok_null/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CORE
main.c
--pointer-check
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Tests that __CPROVER_r_ok() on a null pointers is false
19 changes: 19 additions & 0 deletions regression/cbmc-primitives/r_w_ok_valid/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <assert.h>
#include <stdlib.h>

char c1;

void main()
{
char c2;

char *p1 = &c1;
assert(__CPROVER_r_ok(p1, 1));

char *p2 = &c2;
assert(__CPROVER_r_ok(p2, 1));

char *p3 = malloc(1);
__CPROVER_assume(p1 != NULL);
assert(__CPROVER_r_ok(p3, 1));
}
13 changes: 13 additions & 0 deletions regression/cbmc-primitives/r_w_ok_valid/test-no-cp.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CORE
main.c
--pointer-check --no-simplify --no-propagation
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Tests that __CPROVER_r_ok() is true for valid pointers to global, local, or
malloced memory. We use --no-simplify and --no-propagation to ensure that the
case is not solved by the constant propagation and thus tests the constraint
encoding.
11 changes: 11 additions & 0 deletions regression/cbmc-primitives/r_w_ok_valid/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CORE
main.c
--pointer-check
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Tests that __CPROVER_r_ok() is true for valid pointers to global, local, or
malloced memory
34 changes: 34 additions & 0 deletions regression/cbmc-primitives/r_w_ok_valid_negated/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <assert.h>
#include <stdlib.h>

char c1;

void main()
{
// We check the conditions for a pointer to global static memory (p1), a
// pointer to local memory (p2), and a pointer to dynamic memory (p3).
// For each pointer, we check:
// (1) whether it is unsafe to access one byte more than was allocated
// (2) whether it is unsafe to access one byte before the allocated memory
// (3) whether it is unsafe to access one byte after the allocated memory

char c2;

char *p1 = &c1;

assert(!__CPROVER_r_ok(p1, 2));
assert(!__CPROVER_r_ok(p1 - 1, 1));
assert(!__CPROVER_r_ok(p1 + 1, 1));

char *p2 = &c2;

assert(!__CPROVER_r_ok(p2, 2));
assert(!__CPROVER_r_ok(p2 - 1, 1));
assert(!__CPROVER_r_ok(p2 + 1, 1));

char *p3 = malloc(1);

assert(!__CPROVER_r_ok(p3, 2));
assert(!__CPROVER_r_ok(p3 - 1, 1));
assert(!__CPROVER_r_ok(p3 + 1, 1));
Comment on lines +17 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is much better imo 💚

}
13 changes: 13 additions & 0 deletions regression/cbmc-primitives/r_w_ok_valid_negated/test-no-cp.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
KNOWNBUG
main.c
--pointer-check --no-simplify --no-propagation
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Tests that __CPROVER_r_ok() is false for a pointer to a valid memory object,
though potentially with an offset that is out of bounds. We use --no-simplify
and --no-propagation to ensure that the case is not solved by the constant
propagation and thus tests the constraint encoding.
Loading