28template <
typename Handler,
typename GotoFunctionT>
29void for_each_function_call(GotoFunctionT &&goto_function, Handler handler)
31 using targett =
decltype(goto_function.body.instructions.begin());
34 [](targett target) {
return target->is_function_call(); },
38static void restrict_function_pointer(
54 const auto &original_function = location->call_function();
61 auto const &called_function_pointer =
64 auto const &pointer_symbol =
to_symbol_expr(called_function_pointer);
65 auto const restriction_iterator =
66 restrictions.
restrictions.find(pointer_symbol.get_identifier());
68 if(restriction_iterator == restrictions.
restrictions.end())
72 std::unordered_set<symbol_exprt, irep_hash> candidates;
73 for(
const auto &candidate : restriction_iterator->second)
74 candidates.insert(ns.lookup(candidate).symbol_expr());
89 std::string correct_format)
90 : reason(
std::move(reason)), correct_format(
std::move(correct_format))
98 res +=
"Invalid restriction";
99 res +=
"\nReason: " +
reason;
115 auto const function_pointer_sym =
117 if(function_pointer_sym ==
nullptr)
120 " not found in the symbol table"};
122 auto const &function_pointer_type = function_pointer_sym->type;
123 if(function_pointer_type.id() != ID_pointer)
128 auto const &function_type =
130 if(function_type.id() != ID_code)
136 for(
auto const &function_pointer_target : restriction.second)
138 auto const function_pointer_target_sym =
140 if(function_pointer_target_sym ==
nullptr)
143 "symbol not found: " +
id2string(function_pointer_target)};
145 auto const &function_pointer_target_type =
146 function_pointer_target_sym->type;
147 if(function_pointer_target_type.id() != ID_code)
150 "not a function: " +
id2string(function_pointer_target)};
160 "type mismatch: `" +
id2string(restriction.first) +
"' points to `" +
161 type2c(function_type, ns) +
"', but restriction `" +
162 id2string(function_pointer_target) +
"' has type `" +
163 type2c(function_pointer_target_type, ns) +
"'"};
175 options, goto_model, message_handler);
184 restrict_function_pointer(
228 for(
auto const &restriction : rhs)
230 auto emplace_result = result.emplace(restriction.first, restriction.second);
231 if(!emplace_result.second)
233 for(
auto const &target : restriction.second)
235 emplace_result.first->second.insert(target);
245 const std::list<std::string> &restriction_opts,
246 const std::string &option,
249 auto function_pointer_restrictions =
252 for(
const std::string &restriction_opt : restriction_opts)
254 const auto restriction =
257 const bool inserted = function_pointer_restrictions
258 .emplace(restriction.first, restriction.second)
264 "function pointer restriction for `" +
id2string(restriction.first) +
265 "' was specified twice"};
269 return function_pointer_restrictions;
274 const std::list<std::string> &restriction_opts,
283 const std::list<std::string> &filenames,
289 for(
auto const &filename : filenames)
295 std::move(merged_restrictions),
restrictions.restrictions);
298 return merged_restrictions;
306 const std::string &candidate,
309 const auto last_dot = candidate.rfind(
'.');
311 last_dot == std::string::npos || last_dot + 1 == candidate.size() ||
312 isdigit(candidate[last_dot + 1]))
317 std::string pointer_name = candidate;
319 const auto function_id = pointer_name.substr(0, last_dot);
320 const auto label = pointer_name.substr(last_dot + 1);
327 for(
const auto &instruction : it->second.body.instructions)
331 instruction.labels.begin(), instruction.labels.end(), label) !=
332 instruction.labels.end())
334 location = instruction.source_location();
338 instruction.is_function_call() &&
339 instruction.call_function().id() == ID_dereference &&
340 location.has_value() && instruction.source_location() == *location)
342 auto const &called_function_pointer =
354 "non-existent pointer name " + pointer_name,
355 "pointers should be identifiers or <function_name>.<label>"};
363 const std::string &restriction_opt,
364 const std::string &option,
369 auto const pointer_name_end = restriction_opt.find(
'/');
370 auto const restriction_format_message =
371 "the format for restrictions is "
372 "<pointer_name>/<target[,more_targets]*>";
374 if(pointer_name_end == std::string::npos)
377 restriction_opt +
"'",
378 restriction_format_message};
381 if(pointer_name_end == restriction_opt.size())
384 "couldn't find names of targets after '/' in `" + restriction_opt +
"'",
385 restriction_format_message};
388 if(pointer_name_end == 0)
391 "couldn't find target name before '/' in `" + restriction_opt +
"'"};
395 restriction_opt.substr(0, pointer_name_end), goto_model);
397 auto const target_names_substring =
398 restriction_opt.substr(pointer_name_end + 1);
399 auto const target_name_strings =
split_string(target_names_substring,
',');
401 if(target_name_strings.size() == 1 && target_name_strings[0].empty())
404 "missing target list for function pointer restriction " + pointer_name,
405 restriction_format_message};
408 std::unordered_set<irep_idt> target_names;
409 target_names.insert(target_name_strings.begin(), target_name_strings.end());
411 for(
auto const &target_name : target_names)
413 if(target_name == ID_empty_string)
416 "leading or trailing comma in restrictions for `" + pointer_name +
"'",
417 restriction_format_message);
421 return std::make_pair(pointer_name, target_names);
432 const exprt &function = location->call_function();
441 auto const &function_pointer_call_site =
448 function_pointer_call_site.get_identifier(),
449 "called function pointer must have been assigned at the previous location");
456 const auto restriction = by_name_restrictions.find(rhs.get_identifier());
458 if(restriction != by_name_restrictions.end())
462 function_pointer_call_site.get_identifier(), restriction->second));
473 auto const restriction_opts =
479 commandline_restrictions =
481 restriction_opts, goto_model);
483 goto_model, commandline_restrictions);
494 auto const restriction_file_opts =
497 restriction_file_opts, goto_model, message_handler);
508 auto const restriction_name_opts =
511 restriction_name_opts, goto_model);
521 commandline_restrictions,
531 if(!
json.is_object())
538 std::string pointer_name =
541 if(!restriction.second.is_array())
543 throw deserialization_exceptiont{
"Value of " + restriction.first +
546 auto possible_targets = std::unordered_set<irep_idt>{};
551 std::inserter(possible_targets, possible_targets.end()),
552 [&](
const jsont &array_element) {
553 if(!array_element.is_string())
555 throw deserialization_exceptiont{
556 "Value of " + restriction.first +
557 "contains a non-string array element"};
559 return irep_idt{to_json_string(array_element).value};
561 return possible_targets;
569 const std::string &filename,
573 auto inFile = std::ifstream{filename};
579 "failed to read function pointer restrictions from " + filename};
587 auto function_pointer_restrictions_json =
jsont{};
588 auto &restrictions_json_object =
593 auto &targets_array =
595 for(
auto const &target : restriction.second)
601 return function_pointer_restrictions_json;
605 const std::string &filename)
const
607 auto function_pointer_restrictions_json =
to_json();
609 auto outFile = std::ofstream{filename};
614 " for writing function pointer restrictions"};
617 function_pointer_restrictions_json.output(outFile);
624 const std::list<std::string> &restriction_name_opts,
629 restriction_name_opts,
636 for_each_function_call(
638 const auto restriction = get_by_name_restriction(
639 goto_function.second, by_name_restrictions, it);
643 restrictions.insert(*restriction);
static abstract_object_pointert transform(const exprt &expr, const std::vector< abstract_object_pointert > &operands, const abstract_environmentt &environment, const namespacet &ns)
virtual bool isset(char option) const
const std::list< std::string > & get_values(const std::string &option) const
Thrown when failing to deserialize a value from some low level format, like JSON or raw bytes.
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Base class for all expressions.
static function_pointer_restrictionst from_options(const optionst &options, const goto_modelt &goto_model, message_handlert &message_handler)
Parse function pointer restrictions from command line.
static restrictionst parse_function_pointer_restrictions_from_command_line(const std::list< std::string > &restriction_opts, const goto_modelt &goto_model)
static restrictionst get_function_pointer_by_name_restrictions(const std::list< std::string > &restriction_name_opts, const goto_modelt &goto_model)
Get function pointer restrictions from restrictions with named pointers.
const restrictionst restrictions
static restrictionst parse_function_pointer_restrictions(const std::list< std::string > &restriction_opts, const std::string &option, const goto_modelt &goto_model)
static restrictionst merge_function_pointer_restrictions(restrictionst lhs, const restrictionst &rhs)
static restrictiont parse_function_pointer_restriction(const std::string &restriction_opt, const std::string &option, const goto_modelt &goto_model)
static function_pointer_restrictionst read_from_file(const std::string &filename, const goto_modelt &goto_model, message_handlert &message_handler)
static optionalt< restrictiont > get_by_name_restriction(const goto_functiont &goto_function, const function_pointer_restrictionst::restrictionst &by_name_restrictions, const goto_programt::const_targett &location)
restrictionst::value_type restrictiont
std::unordered_map< irep_idt, std::unordered_set< irep_idt > > restrictionst
static restrictionst parse_function_pointer_restrictions_from_file(const std::list< std::string > &filenames, const goto_modelt &goto_model, message_handlert &message_handler)
void write_to_file(const std::string &filename) const
static void typecheck_function_pointer_restrictions(const goto_modelt &goto_model, const restrictionst &restrictions)
static function_pointer_restrictionst from_json(const jsont &json, const goto_modelt &goto_model)
function_mapt function_map
A goto function, consisting of function body (see body) and parameter identifiers (see parameter_iden...
symbol_tablet symbol_table
Symbol table.
goto_functionst goto_functions
GOTO functions.
A generic container class for the GOTO intermediate representation of one function.
instructionst::iterator targett
instructionst::const_iterator const_targett
Thrown when users pass incorrect command line arguments, for example passing no files to analysis or ...
std::string correct_format
invalid_restriction_exceptiont(std::string reason, std::string correct_format="")
std::string what() const override
A human readable description of what went wrong.
jsont & push_back(const jsont &json)
json_arrayt & make_array()
json_objectt & make_object()
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
void set_option(const std::string &option, const bool value)
const value_listt & get_list_option(const std::string &option) const
const typet & base_type() const
The type of the data what we point to.
const irep_idt & get_identifier() const
const symbolt * lookup(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
Thrown when some external system fails unexpectedly.
std::string type2c(const typet &type, const namespacet &ns, const expr2c_configurationt &configuration)
void for_each_instruction_if(GotoFunctionT &&goto_function, PredicateT predicate, HandlerT handler)
const std::string & id2string(const irep_idt &d)
json_objectt & to_json_object(jsont &json)
json_arrayt & to_json_array(jsont &json)
bool parse_json(std::istream &in, const std::string &filename, message_handlert &message_handler, jsont &dest)
nonstd::optional< T > optionalt
API to expression classes for Pointers.
bool can_cast_expr< dereference_exprt >(const exprt &base)
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
const dereference_exprt & to_dereference_expr(const exprt &expr)
Cast an exprt to a dereference_exprt.
static void json(json_objectT &result, const irep_idt &property_id, const property_infot &property_info)
bool function_is_type_compatible(bool return_value_used, const code_typet &call_type, const code_typet &function_type, const namespacet &ns)
Returns true iff call_type can be converted to produce a function call of the same type as function_t...
Remove Indirect Function Calls.
static std::string resolve_pointer_name(const std::string &candidate, const goto_modelt &goto_model)
Parse candidate to distinguish whether it refers to a function pointer symbol directly (as produced b...
void parse_function_pointer_restriction_options_from_cmdline(const cmdlinet &cmdline, optionst &options)
void restrict_function_pointers(message_handlert &message_handler, goto_modelt &goto_model, const optionst &options)
Apply function pointer restrictions to a goto_model.
Given goto functions and a list of function parameters or globals that are function pointers with lis...
#define RESTRICT_FUNCTION_POINTER_OPT
#define RESTRICT_FUNCTION_POINTER_FROM_FILE_OPT
#define RESTRICT_FUNCTION_POINTER_BY_NAME_OPT
static optionalt< smt_termt > get_identifier(const exprt &expr, const std::unordered_map< exprt, smt_identifier_termt, irep_hash > &expression_handle_identifiers, const std::unordered_map< exprt, smt_identifier_termt, irep_hash > &expression_identifiers)
#define PRECONDITION(CONDITION)
bool can_cast_expr< symbol_exprt >(const exprt &base)
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
void split_string(const std::string &s, char delim, std::vector< std::string > &result, bool strip, bool remove_empty)
void remove_function_pointer(goto_programt &goto_program, goto_programt::targett target, const std::set< symbol_exprt > &functions, goto_modelt &goto_model)