Skip to content

Use OBJECT_SIZE(p) for dynamic memory bounds checking #5247

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 1 commit into from
Feb 28, 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
17 changes: 17 additions & 0 deletions regression/cbmc/r_w_ok5/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <assert.h>
#include <stdlib.h>

void main()
{
char c[2];
assert(__CPROVER_r_ok(c, 2));
assert(!__CPROVER_r_ok(c, 2));
assert(__CPROVER_r_ok(c, 3));
assert(!__CPROVER_r_ok(c, 3));

char *p = malloc(2);
assert(__CPROVER_r_ok(c, 2));
assert(!__CPROVER_r_ok(c, 2));
assert(__CPROVER_r_ok(p, 3));
assert(!__CPROVER_r_ok(p, 3));
}
15 changes: 15 additions & 0 deletions regression/cbmc/r_w_ok5/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CORE
main.c

\[main.assertion.1\] .*: SUCCESS
\[main.assertion.2\] .*: FAILURE
\[main.assertion.3\] .*: FAILURE
\[main.assertion.4\] .*: SUCCESS
\[main.assertion.5\] .*: SUCCESS
\[main.assertion.6\] .*: FAILURE
\[main.assertion.7\] .*: FAILURE
\[main.assertion.8\] .*: SUCCESS
^EXIT=10$
^SIGNAL=0$
--
^warning: ignoring
Copy link
Contributor

Choose a reason for hiding this comment

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

⛏️ Still missing an explanation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this one is simple enough to not need an explanation

26 changes: 26 additions & 0 deletions regression/cbmc/r_w_ok6/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <assert.h>
#include <stdlib.h>

void main()
{
char *p;
int choice;

if(choice)
{
p = malloc(2);
}
else
{
p = malloc(3);
}

assert(__CPROVER_r_ok(p, 2));
assert(!__CPROVER_r_ok(p, 2));

assert(__CPROVER_r_ok(p, 3));
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice - not obvious so good to check it works

assert(!__CPROVER_r_ok(p, 3));

assert(__CPROVER_r_ok(p, 4));
assert(!__CPROVER_r_ok(p, 4));
}
16 changes: 16 additions & 0 deletions regression/cbmc/r_w_ok6/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CORE broken-smt-backend
main.c

\[main.assertion.1\] .*: SUCCESS
\[main.assertion.2\] .*: FAILURE
\[main.assertion.3\] .*: FAILURE
\[main.assertion.4\] .*: FAILURE
\[main.assertion.5\] .*: FAILURE
\[main.assertion.6\] .*: SUCCESS
^EXIT=10$
^SIGNAL=0$
--
^warning: ignoring
--
This test checks that __CPROVER_r_ok() gives the correct result when the given
pointer can point to different memory segments of different sizes
18 changes: 18 additions & 0 deletions regression/cbmc/r_w_ok7/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>

int main()
{
size_t x;
size_t y;
uint8_t *a;

__CPROVER_assume(x > 0);
__CPROVER_assume(y > x);

a = malloc(sizeof(uint8_t) * x);

assert(__CPROVER_w_ok(a, x));
assert(!__CPROVER_w_ok(a, y));
}
12 changes: 12 additions & 0 deletions regression/cbmc/r_w_ok7/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CORE
main.c

\[main.assertion.1\] .*: SUCCESS
\[main.assertion.2\] .*: SUCCESS
VERIFICATION SUCCESSFUL
^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
--
This test checks that __CPROVER_r_ok() works with nondet sizes
10 changes: 10 additions & 0 deletions regression/cbmc/r_w_ok8/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <assert.h>
#include <stdlib.h>

int main()
{
int *x = malloc(2);
int *y = malloc(2);
assert(!__CPROVER_r_ok(x, 3));
assert(__CPROVER_r_ok(x, 3) == __CPROVER_r_ok(y, 3));
Copy link
Contributor

Choose a reason for hiding this comment

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

❓ Not sure I understand the purpose of the test

Copy link
Contributor Author

@danpoe danpoe Feb 28, 2020

Choose a reason for hiding this comment

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

This was reported in issue #5194. It checks that two uses of __CPROVER_r_ok() can return false simultaneously. Previously only one call could return false at a time.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 Please add to the desc file 🙂

}
16 changes: 16 additions & 0 deletions regression/cbmc/r_w_ok8/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CORE
main.c

\[main.assertion.1\] .*: SUCCESS
\[main.assertion.2\] .*: SUCCESS
VERIFICATION SUCCESSFUL
^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
--
This test checks that two usages of the primitive __CPROVER_r_ok() can be false
simultaneously in the encoding of the program. Previously, at most one call
could be false at a time. This was imprecise, however, it was sufficient to
guarantee soundness when the __CPROVER_r_ok() primitive was used directly in an
assertion (i.e., as assert(__CPROVER_r_ok(p, size)). See issue #5194.
8 changes: 4 additions & 4 deletions src/analyses/goto_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,15 +1249,15 @@ goto_checkt::address_check(const exprt &address, const exprt &size)

if(flags.is_unknown() || flags.is_dynamic_heap())
{
const or_exprt dynamic_bounds_violation(
dynamic_object_lower_bound(address, nil_exprt()),
dynamic_object_upper_bound(address, ns, size));
const or_exprt object_bounds_violation(
object_lower_bound(address, nil_exprt()),
object_upper_bound(address, size));

conditions.push_back(conditiont(
or_exprt(
in_bounds_of_some_explicit_allocation,
implies_exprt(
malloc_object(address, ns), not_exprt(dynamic_bounds_violation))),
dynamic_object(address), not_exprt(object_bounds_violation))),
"pointer outside dynamic object bounds"));
}

Expand Down