Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// The functions to instrument are ordered by when their names were first
// interned. printf is a predefined identifier (ID_printf in
// util/irep_ids.def), so that it is instrumented, and gets its write set
// parameter, before the loop assigns of loop_fn are inferred.
int a;

int printf(int *p, ...)
{
*p = 1;
return 0;
}

void loop_fn(unsigned n)
{
unsigned i = 0;
while(i < n)
// clang-format off
__CPROVER_loop_invariant(i <= n)
__CPROVER_decreases(n - i)
// clang-format on
{
printf(&a, 0);
i++;
}
}

int main(void)
{
unsigned n;
loop_fn(n);
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CORE dfcc-only
main.c
--dfcc main --apply-loop-contracts
^\[printf\.assigns\.\d+\] line \d+ Check that \*p is assignable: SUCCESS$
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^Invariant check failed
--
Loop assigns inference inlines the callees of loop_fn before loop_fn's calls
are instrumented. When printf has already been instrumented, the arguments of
the call need a placeholder for its write set parameter: otherwise &a binds to
the write set parameter and 0 to p, so that the inferred loop assigns miss a.
28 changes: 28 additions & 0 deletions regression/goto-instrument/inline_19/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// The functions are called without a prototype in scope, so the arguments
// keep their own types. Symbolic execution reinterprets the enum argument
// and rejects the double argument; the inliner must not fail on either.
enum E
{
A = 3
};

int as_int();
int from_double();

int main()
{
enum E e = A;
__CPROVER_assert(as_int(e) == 3, "enum argument reinterpreted");
__CPROVER_assert(from_double(1.5) == 1, "double argument is nondet");
return 0;
}

int as_int(int x)
{
return x;
}

int from_double(int x)
{
return x;
}
15 changes: 15 additions & 0 deletions regression/goto-instrument/inline_19/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CORE
main.c
--inline
^\[(main\.)?assertion\.1\] .*enum argument reinterpreted: SUCCESS$
^\[(main\.)?assertion\.2\] .*double argument is nondet: FAILURE$
argument type does not match parameter 'x', inserting non-deterministic value
^EXIT=10$
^SIGNAL=0$
--
^Invariant check failed
--
Calls whose argument types differ from the parameter types made the inliner
fail an invariant. Arguments that symbolic execution reinterprets (here an
enum) must give the same result after inlining; other arguments (here a
double) are replaced by a non-deterministic value.
42 changes: 41 additions & 1 deletion src/goto-programs/goto_inline_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,35 @@ Author: Daniel Kroening, kroening@kroening.com
#include <iostream>
#endif

#include <util/arith_tools.h>
#include <util/byte_operators.h>
#include <util/c_types.h>
#include <util/cprover_prefix.h>
#include <util/invariant.h>
#include <util/namespace.h>
#include <util/std_code.h>
#include <util/symbol.h>

/// \return True if goto_symext::parameter_assignments reinterprets the bits
/// of an argument of type \p argument_type that is bound to a parameter of
/// the different type \p parameter_type.
static bool can_reinterpret_argument(
const typet &parameter_type,
const typet &argument_type)
{
const auto is_reinterpretable = [](const typet &type)
{
return type.id() == ID_signedbv || type.id() == ID_unsignedbv ||
type.id() == ID_c_enum_tag || type.id() == ID_bool ||
type.id() == ID_pointer || type.id() == ID_union ||
type.id() == ID_union_tag;
};

return is_reinterpretable(parameter_type) &&
(is_reinterpretable(argument_type) ||
argument_type.id() == ID_c_bit_field);
}

void goto_inlinet::parameter_assignments(
const goto_programt::targett target,
const irep_idt &function_name, // name of called function
Expand Down Expand Up @@ -99,9 +122,26 @@ void goto_inlinet::parameter_assignments(
{
actual = typecast_exprt(actual, f_partype);
}
else if(can_reinterpret_argument(f_partype, f_acttype))
{
// Reinterpret the bits, as goto_symext::parameter_assignments does,
// so that inlining does not change the result of the analysis.
actual = make_byte_extract(
actual, from_integer(0, c_index_type()), f_partype);
}
else
{
UNREACHABLE;
// An argument that symbolic execution would reject, for example a
// floating-point or struct argument bound to an integer parameter
// when no prototype was in scope: over-approximate it by a
// non-deterministic value.
log.warning().source_location = source_location;
log.warning() << "call to '" << function_name << "': "
<< "argument type does not match parameter '"
<< symbol.base_name << "', "
<< "inserting non-deterministic value" << messaget::eom;

actual = side_effect_expr_nondett(parameter_type, source_location);
}
}

Expand Down
Loading