Skip to content

Feature nondet string initialization [depends-on: #3750] #3572

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

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions regression/cbmc/pointer-to-string-function-parameters/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <assert.h>
#include <stddef.h>
#include <string.h>

int test(char *string, size_t size)
{
for(size_t ix = 0; ix + 1 < size; ++ix)
{
char c = string[ix];
// characters except the last should fall in printable range
assert(c == '\n' | c == '\r' | c == '\t' | (c >= 32 && c <= 126));
}
assert(strlen(string) + 1 == size);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CORE
test.c
--function test --pointers-to-treat-as-string string --associated-array-sizes string:size --pointer-check --unwind 10
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
1 change: 1 addition & 0 deletions src/ansi-c/ansi_c_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Author: Daniel Kroening, [email protected]
"(pointers-to-treat-as-array):" \
Copy link
Contributor

Choose a reason for hiding this comment

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

pointers-to-treat-as-array -> pointers-to-treat-as-arrays (and throughout the PR)

"(associated-array-sizes):" \
"(max-dynamic-array-size):" \
"(pointers-to-treat-as-string):"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing help

Copy link
Contributor

Choose a reason for hiding this comment

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

pointers-to-treat-as-string -> pointers-to-treat-as-strings (and throughout the PR)


#define HELP_ANSI_C_LANGUAGE \
" --max-nondet-tree-depth N limit size of nondet (e.g. input) object tree;\n" /* NOLINT(*) */\
Expand Down
108 changes: 100 additions & 8 deletions src/ansi-c/c_nondet_symbol_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ class symbol_factoryt
std::size_t depth,
const recursion_sett &recursion_set);

void gen_nondet_string_member_initialization(
code_blockt &assignments,
const exprt &array,
const exprt &array_size,
std::size_t depth,
const recursion_sett &recursion_set);

private:
/// Add a new variable symbol to the symbol table
/// \param type: The type of the new variable
Expand Down Expand Up @@ -216,14 +223,29 @@ void symbol_factoryt::gen_nondet_init(
using std::placeholders::_5;
if(object_factory_params.should_be_treated_as_array(symbol_name))
{
gen_array_initialization_t gen_array_initialization = std::bind(
&symbol_factoryt::gen_nondet_array_member_initialization,
this,
_1,
_2,
_3,
_4,
_5);
gen_array_initialization_t gen_array_initialization;
if(object_factory_params.should_be_treated_as_string(symbol_name))
{
gen_array_initialization = std::bind(
&symbol_factoryt::gen_nondet_string_member_initialization,
this,
_1,
_2,
_3,
_4,
_5);
}
else
{
gen_array_initialization = std::bind(
&symbol_factoryt::gen_nondet_array_member_initialization,
this,
_1,
_2,
_3,
_4,
_5);
}
gen_nondet_size_array_init(
assignments,
symbol_expr,
Expand Down Expand Up @@ -524,6 +546,76 @@ void symbol_factoryt::gen_nondet_array_member_initialization(
assignments.add(std::move(array_member_init));
}

void symbol_factoryt::gen_nondet_string_member_initialization(
code_blockt &assignments,
const exprt &array,
const exprt &array_size,
std::size_t depth,
const symbol_factoryt::recursion_sett &recursion_set)
{
// for(size_t ix = 0; ix + 1 < array_size; ++ix) {
// assume(arr[ix] == '\n'
// || arr[ix] == '\t'
// || arr[ix] == '\r'
// || (32 <= arr[ix] && arr[ix] <= 126));
// }

auto const &array_index_symbol =
new_tmp_symbol(size_type(), CPROVER_PREFIX "array_index");
auto array_member_init = code_fort{};

array_member_init.init() = code_assignt{array_index_symbol.symbol_expr(),
from_integer(0, size_type())};

array_member_init.cond() = binary_exprt{
plus_exprt{array_index_symbol.symbol_expr(), from_integer(1, size_type())},
ID_lt,
array_size,
bool_typet{}};

auto const array_member = dereference_exprt{
plus_exprt{array, array_index_symbol.symbol_expr(), array.type()}};

auto array_member_init_body = code_blockt{};
array_member_init_body.add(code_assumet{typecast_exprt{
bitor_exprt{
typecast_exprt{equal_exprt{array_member, from_integer('\n', char_type())},
signed_int_type()},
bitor_exprt{
typecast_exprt{
equal_exprt{array_member, from_integer('\t', char_type())},
signed_int_type()},
bitor_exprt{
typecast_exprt{
equal_exprt{array_member, from_integer('\r', char_type())},
signed_int_type()},
bitand_exprt{typecast_exprt{
binary_relation_exprt{
from_integer(32, char_type()), ID_le, array_member},
signed_int_type()},
typecast_exprt{
binary_relation_exprt{
array_member, ID_le, from_integer(126, char_type())},
signed_int_type()}},
}}},
bool_typet{}}});
array_member_init_body.add(
code_assignt{array_index_symbol.symbol_expr(),
plus_exprt{array_index_symbol.symbol_expr(),
from_integer(1, size_type()),
size_type()}});
array_member_init.body() = std::move(array_member_init_body);
assignments.add(std::move(array_member_init));

// array[array_size - 1] = '\0';
assignments.add(
code_assignt{dereference_exprt{plus_exprt{
array,
minus_exprt{array_size, from_integer(1, size_type())},
array.type()}},
from_integer(0, char_type())});
}

const symbolt &
symbol_factoryt::gen_malloc_function(const irep_idt &malloc_symbol_name)
{
Expand Down
30 changes: 27 additions & 3 deletions src/ansi-c/c_object_factory_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ void parse_c_object_factory_options(const cmdlinet &cmdline, optionst &options)
"pointers-to-treat-as-array",
cmdline.get_comma_separated_values("pointers-to-treat-as-array"));
}
if(cmdline.isset("pointers-to-treat-as-string"))
{
options.set_option(
"pointers-to-treat-as-string",
cmdline.get_comma_separated_values("pointers-to-treat-as-string"));
}
if(cmdline.isset("associated-array-sizes"))
{
options.set_option(
Expand All @@ -38,6 +44,13 @@ void parse_c_object_factory_options(const cmdlinet &cmdline, optionst &options)
}
}

bool c_object_factory_parameterst::should_be_treated_as_array(irep_idt id) const
{
return pointers_to_treat_as_array.find(id) !=
pointers_to_treat_as_array.end() ||
should_be_treated_as_string(id);
}

void c_object_factory_parameterst::set(const optionst &options)
{
object_factory_parameterst::set(options);
Expand All @@ -50,6 +63,16 @@ void c_object_factory_parameterst::set(const optionst &options)
this->pointers_to_treat_as_array, this->pointers_to_treat_as_array.end()),
id2string);

auto const &pointers_to_treat_as_string =
options.get_list_option("pointers-to-treat-as-string");
std::transform(
std::begin(pointers_to_treat_as_string),
std::end(pointers_to_treat_as_string),
std::inserter(
this->pointers_to_treat_as_string,
this->pointers_to_treat_as_string.end()),
id2string);

if(options.is_set("max-dynamic-array-size"))
{
max_dynamic_array_size =
Expand Down Expand Up @@ -117,8 +140,9 @@ optionalt<irep_idt> c_object_factory_parameterst::get_associated_size_variable(
array_name_to_associated_array_size_variable, array_id);
}

bool c_object_factory_parameterst::should_be_treated_as_array(irep_idt id) const
bool c_object_factory_parameterst::should_be_treated_as_string(
irep_idt id) const
{
return pointers_to_treat_as_array.find(id) !=
pointers_to_treat_as_array.end();
return pointers_to_treat_as_string.find(id) !=
pointers_to_treat_as_string.end();
}
2 changes: 2 additions & 0 deletions src/ansi-c/c_object_factory_parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct c_object_factory_parameterst final : public object_factory_parameterst
}

bool should_be_treated_as_array(irep_idt id) const;
bool should_be_treated_as_string(irep_idt id) const;
bool is_array_size_parameter(irep_idt id) const;
optionalt<irep_idt> get_associated_size_variable(irep_idt array_id) const;

Expand All @@ -38,6 +39,7 @@ struct c_object_factory_parameterst final : public object_factory_parameterst
std::set<irep_idt> pointers_to_treat_as_array;
std::set<irep_idt> variables_that_hold_array_sizes;
std::map<irep_idt, irep_idt> array_name_to_associated_array_size_variable;
std::set<irep_idt> pointers_to_treat_as_string;
};

/// Parse the c object factory parameters from a given command line
Expand Down