Skip to content

Validity checking of void pointers in contracts #6375

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

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 21 additions & 0 deletions regression/contracts/assigns_enforce_21/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <assert.h>
#include <stdlib.h>

int x = 0;

void foo(void *y) __CPROVER_assigns(*y) __CPROVER_assigns(x)
{
x = 2;
int *bar = (int *)y;
*bar = 2;
}

int main()
{
int *y = malloc(sizeof(*y));
*y = 0;
foo(y);
assert(x == 2);
assert(*y == 2);
return 0;
}
9 changes: 9 additions & 0 deletions regression/contracts/assigns_enforce_21/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
main.c
--enforce-contract foo _ --pointer-check
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
--
Checks whether contract enforcement does not try to dereference void pointers.
13 changes: 11 additions & 2 deletions src/goto-instrument/contracts/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Date: September 2021
#include "utils.h"

#include <util/pointer_expr.h>
#include <util/pointer_offset_size.h>
#include <util/pointer_predicates.h>

static void append_safe_havoc_code_for_expr(
Expand Down Expand Up @@ -59,8 +60,16 @@ exprt all_dereferences_are_valid(const exprt &expr, const namespacet &ns)
exprt::operandst validity_checks;

if(expr.id() == ID_dereference)
validity_checks.push_back(
good_pointer_def(to_dereference_expr(expr).pointer(), ns));
{
const auto &pointer = to_dereference_expr(expr).pointer();
const auto opt_size_of_deref =
size_of_expr(to_pointer_type(pointer.type()).subtype(), ns);

if(opt_size_of_deref.has_value())
validity_checks.push_back(good_pointer_def(pointer, ns));
else
validity_checks.push_back(false_exprt());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you add a comment here this could happen in case of void pointers?

}

for(const auto &op : expr.operands())
validity_checks.push_back(all_dereferences_are_valid(op, ns));
Expand Down
5 changes: 4 additions & 1 deletion src/util/pointer_predicates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ exprt good_pointer_def(
const typet &dereference_type=pointer_type.subtype();

const auto size_of_expr_opt = size_of_expr(dereference_type, ns);
CHECK_RETURN(size_of_expr_opt.has_value());
PRECONDITION_WITH_DIAGNOSTICS(
size_of_expr_opt.has_value(),
"Could not compute sizeof on pointer's dereference type. "
"It may be a void pointer.");

const exprt good_dynamic = not_exprt{deallocated(pointer, ns)};

Expand Down