Skip to content

Take __CPROVER_allocated_memory regions into account for --bounds-check #4256

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 22, 2019
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
39 changes: 39 additions & 0 deletions regression/cbmc/memory_allocation2/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#define _cbmc_printf2(str, var) \
{ \
unsigned int ValueOf_##str = (unsigned int)var; \
}

#define BUFFER_SIZE 100
typedef struct
{
int buffer[BUFFER_SIZE];
} buffert;

#define BUF_BASE 0x1000
#define BUF0_BASE (BUF_BASE + 0x00000)
#define BUF1_BASE (BUF_BASE + 0x10000)
#define BUF2_BASE (BUF_BASE + 0x20000)
#define BUF3_BASE (BUF_BASE + 0x30000)

#define BUF0 ((buffert *)BUF0_BASE)
#define BUF1 ((buffert *)BUF1_BASE)
#define BUF2 ((buffert *)BUF2_BASE)
#define BUF3 ((buffert *)BUF3_BASE)

static buffert(*const buffers[4]) = {BUF0, BUF1, BUF2, BUF3};

main()
{
__CPROVER_allocated_memory(BUF0_BASE, sizeof(buffert));
__CPROVER_allocated_memory(BUF1_BASE, sizeof(buffert));
__CPROVER_allocated_memory(BUF2_BASE, sizeof(buffert));
__CPROVER_allocated_memory(BUF3_BASE, sizeof(buffert));

_cbmc_printf2(sizeof_buffers, sizeof(buffers));
_cbmc_printf2(sizeof_buffers_0, sizeof(buffers[0]));
_cbmc_printf2(sizeof_buffer, sizeof(buffers[0]->buffer));

buffers[0]->buffer[0];
buffers[0]->buffer[BUFFER_SIZE - 1];
buffers[0]->buffer[BUFFER_SIZE]; // should be out-of-bounds
}
11 changes: 11 additions & 0 deletions regression/cbmc/memory_allocation2/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CORE broken-smt-backend
main.c
--bounds-check
^EXIT=10$
^SIGNAL=0$
^\[main\.array_bounds\.[1-5]\] .*: SUCCESS$
^\[main\.array_bounds\.[67]\] line 38 array.buffer (dynamic object )?upper bound in buffers\[\(signed long (long )?int\)0\]->buffer\[\(signed long (long )?int\)100\]: FAILURE$
^\*\* 2 of 7 failed
^VERIFICATION FAILED$
--
^warning: ignoring
27 changes: 23 additions & 4 deletions src/analyses/goto_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class goto_checkt
void goto_checkt::collect_allocations(
const goto_functionst &goto_functions)
{
if(!enable_pointer_check)
if(!enable_pointer_check && !enable_bounds_check)
return;

forall_goto_functions(itf, goto_functions)
Expand Down Expand Up @@ -1296,10 +1296,29 @@ void goto_checkt::bounds_check(

binary_relation_exprt inequality(effective_offset, ID_lt, size_casted);

exprt::operandst alloc_disjuncts;
for(const auto &a : allocations)
{
typecast_exprt int_ptr{pointer, a.first.type()};

binary_relation_exprt lower_bound_check{a.first, ID_le, int_ptr};

plus_exprt upper_bound{
int_ptr,
typecast_exprt::conditional_cast(ode.offset(), int_ptr.type())};

binary_relation_exprt upper_bound_check{
std::move(upper_bound), ID_lt, plus_exprt{a.first, a.second}};

alloc_disjuncts.push_back(
and_exprt{std::move(lower_bound_check), std::move(upper_bound_check)});
}

exprt in_bounds_of_some_explicit_allocation = disjunction(alloc_disjuncts);

or_exprt precond(
and_exprt(
dynamic_object(pointer),
not_exprt(malloc_object(pointer, ns))),
std::move(in_bounds_of_some_explicit_allocation),
and_exprt(dynamic_object(pointer), not_exprt(malloc_object(pointer, ns))),
inequality);

add_guarded_claim(
Expand Down