Skip to content

Include pointer offset in counterexample output #3135

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
Oct 31, 2018
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
22 changes: 22 additions & 0 deletions regression/cbmc/trace_address_arithmetic1/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <assert.h>

typedef struct
{
int inta;
int intb;
} struct_t;

typedef union {
int intaa;
struct_t structbb;
} union_struct_t;

int main()
{
union_struct_t uuu;
char *ptr = (char *)&uuu.structbb.intb;
int A[2];
int *ip = &A[1];
uuu.structbb.intb = 1;
assert(0);
}
10 changes: 10 additions & 0 deletions regression/cbmc/trace_address_arithmetic1/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CORE
main.c
--trace
^EXIT=10$
^SIGNAL=0$
ptr=\(char \*\)&uuu!0@1 \+ 4(l+)? \(.*\)$
ip=A!0@1 \+ 1(l+)? \(.*\)$
^VERIFICATION FAILED$
--
^warning: ignoring
36 changes: 24 additions & 12 deletions src/solvers/flattening/pointer_logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Author: Daniel Kroening, kroening@kroening.com
#include <util/invariant.h>
#include <util/pointer_offset_size.h>
#include <util/prefix.h>
#include <util/simplify_expr.h>
#include <util/std_expr.h>

bool pointer_logict::is_dynamic_object(const exprt &expr) const
Expand Down Expand Up @@ -104,29 +105,40 @@ exprt pointer_logict::pointer_expr(
// https://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Pointer-Arith.html
if(subtype.id() == ID_empty)
subtype = char_type();
const exprt deep_object =
exprt deep_object =
get_subexpression_at_offset(object_expr, pointer.offset, subtype, ns);
CHECK_RETURN(deep_object.is_not_nil());
simplify(deep_object, ns);
if(deep_object.id() != byte_extract_id())
return address_of_exprt(deep_object, type);
return typecast_exprt::conditional_cast(
address_of_exprt(deep_object), type);

const byte_extract_exprt &be = to_byte_extract_expr(deep_object);
const address_of_exprt base(be.op());
if(be.offset().is_zero())
return address_of_exprt(be.op(), type);
return typecast_exprt::conditional_cast(base, type);

const auto subtype_bytes = pointer_offset_size(subtype, ns);
CHECK_RETURN(subtype_bytes.has_value() && *subtype_bytes > 0);
if(*subtype_bytes > pointer.offset)
const auto object_size = pointer_offset_size(be.op().type(), ns);
if(object_size.has_value() && *object_size <= 1)
{
return plus_exprt(
address_of_exprt(be.op(), pointer_type(char_type())),
from_integer(pointer.offset, index_type()));
return typecast_exprt::conditional_cast(
plus_exprt(base, from_integer(pointer.offset, pointer_diff_type())),
type);
}
else if(object_size.has_value() && pointer.offset % *object_size == 0)
{
return typecast_exprt::conditional_cast(
plus_exprt(
base, from_integer(pointer.offset / *object_size, pointer_diff_type())),
type);
Copy link
Member

Choose a reason for hiding this comment

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

You probably meant pointer.offset / *object_size

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ooops - that clearly shows that my test case is not good enough. Thanks for catching that! I'll extend my test.

}
else
{
return plus_exprt(
address_of_exprt(be.op(), pointer_type(char_type())),
from_integer(pointer.offset / *subtype_bytes, index_type()));
return typecast_exprt::conditional_cast(
plus_exprt(
typecast_exprt(base, pointer_type(char_type())),
from_integer(pointer.offset, pointer_diff_type())),
type);
}
}

Expand Down