Skip to content

Fix cbmc crash on pointer checks of void pointer dereferences #5427

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
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
5 changes: 5 additions & 0 deletions regression/cbmc/void_pointer6/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
int main()
{
void *p;
*p;
}
10 changes: 10 additions & 0 deletions regression/cbmc/void_pointer6/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CORE
main.c
--pointer-check
^EXIT=10$
Copy link
Member

Choose a reason for hiding this comment

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

  • verification failed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not necessary I think as EXIT=10 already indicates that cbmc exited without error and verification failed.

^SIGNAL=0$
--
^warning: ignoring
--
Checks that cbmc does not crash with --pointer-check on dereference of an
invalid void pointer
6 changes: 6 additions & 0 deletions regression/cbmc/void_pointer7/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
int main()
{
int a = 0;
void *p = &a;
*p;
}
11 changes: 11 additions & 0 deletions regression/cbmc/void_pointer7/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
--
Checks that cbmc does not crash with --pointer-check on dereference of a valid
void pointer
20 changes: 17 additions & 3 deletions src/analyses/goto_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1164,10 +1164,24 @@ void goto_checkt::pointer_validity_check(

const exprt &pointer=expr.pointer();

auto size_of_expr_opt = size_of_expr(expr.type(), ns);
CHECK_RETURN(size_of_expr_opt.has_value());
exprt size;

auto conditions = address_check(pointer, size_of_expr_opt.value());
if(expr.type().id() == ID_empty)
{
// a dereference *p (with p being a pointer to void) is valid if p points to
// valid memory (of any size). the smallest possible size of the memory
// segment p could be pointing to is 1, hence we use this size for the
// address check
size = from_integer(1, size_type());

Choose a reason for hiding this comment

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

Should we change size_of_expr to return 1 here instead?

Copy link
Contributor Author

@danpoe danpoe Jul 20, 2020

Choose a reason for hiding this comment

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

Not sure. There are quite a number of uses in the codebase that check that the return value of size_of_expr() is not empty, and if it is throw an exception or have some other handling. Probably we should review those at some point, though that's out of scope of this PR.

Copy link
Member

Choose a reason for hiding this comment

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

Is the purpose of this to create an address check that fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No the address check should succeed. Basically *p with p being a pointer to void is valid as long as p points to valid memory, though then using the value would be undefined behavior.

Copy link
Member

Choose a reason for hiding this comment

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

Please explain the magic 1 in a comment.

}
else
{
auto size_of_expr_opt = size_of_expr(expr.type(), ns);
CHECK_RETURN(size_of_expr_opt.has_value());
size = size_of_expr_opt.value();
}

auto conditions = address_check(pointer, size);

for(const auto &c : conditions)
{
Expand Down