Skip to content

Generate function bodies with nondet return values #3457

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 2 commits into from
Jan 23, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <assert.h>

const int func();

void main()
{
assert(func() == 0);
assert(func() != 0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c
--generate-function-body func --generate-function-body-options nondet-return
^SIGNAL=0$
\[main.assertion.1\] line \d+ assertion func\(\) == 0: FAILURE
\[main.assertion.2\] line \d+ assertion func\(\) != 0: FAILURE
--
^warning: ignoring
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <assert.h>
#include <stdlib.h>

int **func();

void main()
{
int **p = func();

assert(p != NULL);
assert(*p != NULL);

assert(**p == 0);
assert(**p != 0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CORE
main.c
--generate-function-body func --generate-function-body-options nondet-return --min-null-tree-depth 10 --max-nondet-tree-depth 5
^SIGNAL=0$
\[main.assertion.1\] line \d+ assertion p != .*(0|(NULL))\)?: SUCCESS
\[main.assertion.2\] line \d+ assertion \*p != .*(0|(NULL))\)?: SUCCESS
\[main.assertion.3\] line \d+ assertion \*\*p == 0: FAILURE
\[main.assertion.4\] line \d+ assertion \*\*p != 0: FAILURE
--
^warning: ignoring
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <assert.h>

int func();

void main()
{
assert(func() == 0);
assert(func() != 0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c
--generate-function-body func --generate-function-body-options nondet-return
^SIGNAL=0$
\[main.assertion.1\] line \d+ assertion func\(\) == 0: FAILURE
\[main.assertion.2\] line \d+ assertion func\(\) != 0: FAILURE
--
^warning: ignoring
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <assert.h>
#include <stdlib.h>

typedef struct st
{
struct st *next;
int data;
} st_t;

st_t dummy;

st_t *func();

void main()
{
st_t *st = func();

assert(st != NULL);

assert(st->next != NULL);
assert(st->next->next != NULL);
assert(st->next->next->next == NULL);

assert(st != &dummy);
assert(st->next != &dummy);
assert(st->next->next != &dummy);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c
--generate-function-body func --generate-function-body-options nondet-return --min-null-tree-depth 10 --max-nondet-tree-depth 3
^EXIT=0$
^SIGNAL=0$
VERIFICATION SUCCESSFUL
--
^warning: ignoring
40 changes: 38 additions & 2 deletions src/goto-instrument/generate_function_bodies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Author: Diffblue Ltd.

#include <util/arith_tools.h>
#include <util/format_expr.h>
#include <util/fresh_symbol.h>
#include <util/make_unique.h>
#include <util/string_utils.h>

Expand Down Expand Up @@ -271,11 +272,46 @@ class havoc_generate_function_bodiest : public generate_function_bodiest

if(function.type.return_type() != void_typet())
{
typet type(function.type.return_type());
type.remove(ID_C_constant);

symbolt &aux_symbol = get_fresh_aux_symbol(
type,
id2string(function_name),
"return_value",
function_symbol.location,
ID_C,
symbol_table);

aux_symbol.is_static_lifetime = false;

auto decl_instruction = add_instruction();
decl_instruction->make_decl();
decl_instruction->code = code_declt(aux_symbol.symbol_expr());

goto_programt dest;

havoc_expr_rec(
aux_symbol.symbol_expr(),
0,
function_symbol.location,
symbol_table,
dest);

function.body.destructive_append(dest);

exprt return_expr = typecast_exprt::conditional_cast(
aux_symbol.symbol_expr(), function.type.return_type());

auto return_instruction = add_instruction();
return_instruction->make_return();
return_instruction->code = code_returnt(side_effect_expr_nondett(
function.type.return_type(), function_symbol.location));
return_instruction->code = code_returnt(return_expr);

auto dead_instruction = add_instruction();
dead_instruction->make_dead();
dead_instruction->code = code_deadt(aux_symbol.symbol_expr());
}

auto end_function_instruction = add_instruction();
end_function_instruction->make_end_function();

Expand Down