Skip to content

Commit ade1ce9

Browse files
committed
Some cleanup for the function pointer restriction feature
1 parent 1755915 commit ade1ce9

File tree

8 files changed

+30
-39
lines changed

8 files changed

+30
-39
lines changed

doc/cprover-manual/restrict-function-pointer.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ nothing other than `nullfs`; But perhaps due to the logic being too complicated
135135
symex ends up being unable to figure this out, so in the call to `fs_open()` we
136136
end up branching on all functions matching the signature of
137137
`filesystem_t::open`, which could be quite a few functions within the program.
138-
Worst of all, if it's address is ever taken in the program, as far as the "dumb"
138+
Worst of all, if its address is ever taken in the program, as far as the "dumb"
139139
function pointer removal is concerned it could be `fs_open()` itself due to it
140140
having a matching signature, leading to symex being forced to follow a
141141
potentially infinite recursion until its unwind limit.
@@ -168,6 +168,6 @@ restrictions.
168168

169169
**Note:** as of now, if something goes wrong during type checking (i.e. making
170170
sure that all function pointer replacements refer to functions in the symbol
171-
table that have the correct type), the error message will refer the command line
172-
option `--restrict-function-pointer` regardless of whether the restriction in
173-
question came from the command line or a file.
171+
table that have the correct type), the error message will refer to the command
172+
line option `--restrict-function-pointer` regardless of whether the restriction
173+
in question came from the command line or a file.

src/goto-instrument/goto_instrument_parse_options.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1867,7 +1867,7 @@ void goto_instrument_parse_optionst::help()
18671867
" --no-caching disable caching of intermediate results during transitive function inlining\n" // NOLINT(*)
18681868
" --log <file> log in json format which code segments were inlined, use with --function-inline\n" // NOLINT(*)
18691869
" --remove-function-pointers replace function pointers by case statement over function calls\n" // NOLINT(*)
1870-
RESTRICT_FUNCTION_POINTER_HELP
1870+
HELP_RESTRICT_FUNCTION_POINTER
18711871
HELP_REMOVE_CALLS_NO_BODY
18721872
HELP_REMOVE_CONST_FUNCTION_POINTERS
18731873
" --add-library add models of C library functions\n"

src/goto-programs/goto_program.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,27 +1170,27 @@ struct pointee_address_equalt
11701170
};
11711171

11721172
template <typename GotoFunctionT, typename PredicateT, typename HandlerT>
1173-
void for_each_goto_location_if(
1173+
void for_each_instruction_if(
11741174
GotoFunctionT &&goto_function,
11751175
PredicateT predicate,
1176-
HandlerT handle)
1176+
HandlerT handler)
11771177
{
11781178
auto &&instructions = goto_function.body.instructions;
11791179
for(auto it = instructions.begin(); it != instructions.end(); ++it)
11801180
{
11811181
if(predicate(it))
11821182
{
1183-
handle(it);
1183+
handler(it);
11841184
}
11851185
}
11861186
}
11871187

11881188
template <typename GotoFunctionT, typename HandlerT>
1189-
void for_each_goto_location(GotoFunctionT &&goto_function, HandlerT handle)
1189+
void for_each_instruction(GotoFunctionT &&goto_function, HandlerT handler)
11901190
{
11911191
using iterator_t = decltype(goto_function.body.instructions.begin());
1192-
for_each_goto_location_if(
1193-
goto_function, [](const iterator_t &) { return true; }, handle);
1192+
for_each_instruction_if(
1193+
goto_function, [](const iterator_t &) { return true; }, handler);
11941194
}
11951195

11961196
#define forall_goto_program_instructions(it, program) \

src/goto-programs/label_function_pointer_call_sites.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void label_function_pointer_call_sites(goto_modelt &goto_model)
1515
for(auto &goto_function : goto_model.goto_functions.function_map)
1616
{
1717
std::size_t function_pointer_call_counter = 0;
18-
for_each_goto_location_if(
18+
for_each_instruction_if(
1919
goto_function.second,
2020
[](const goto_programt::targett it) {
2121
return it->is_function_call() && can_cast_expr<dereference_exprt>(

src/goto-programs/label_function_pointer_call_sites.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ Author: Diffblue Ltd.
2121
/// only need to be able to handle these two cases.
2222
///
2323
/// It does this by replacing all CALL instructions to function pointers with an
24-
/// assignment to a function pointer variable with a name following the pattern
25-
/// [function_name].function_pointer_call.[N], where "N" is the nth call to a
26-
/// function pointer in the function "function_name".
24+
/// assignment to a new function pointer variable followed by a call to that new
25+
/// function pointer. The name of the introduced variable follows the pattern
26+
/// <function_name>.function_pointer_call.<N>, where N is the nth call to a
27+
/// function pointer in the function function_name.
2728
void label_function_pointer_call_sites(goto_modelt &goto_model);
2829

2930
#endif // CPROVER_GOTO_PROGRAMS_LABEL_FUNCTION_POINTER_CALL_SITES_H

src/goto-programs/restrict_function_pointers.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ source_locationt make_function_pointer_restriction_assertion_source_location(
103103
}
104104
comment << ']';
105105
}
106-
source_location.set_comment(string2id(comment.str()));
106+
source_location.set_comment(comment.str());
107107
source_location.set_property_class(ID_assertion);
108108
return source_location;
109109
}
110110

111111
template <typename Handler>
112112
void for_each_function_call(goto_functiont &goto_function, Handler handler)
113113
{
114-
for_each_goto_location_if(
114+
for_each_instruction_if(
115115
goto_function,
116116
[](goto_programt::targett target) { return target->is_function_call(); },
117117
handler);
@@ -148,9 +148,10 @@ void restrict_function_pointers(
148148
if(restriction_iterator != restrictions.restrictions.end())
149149
{
150150
auto const &restriction = *restriction_iterator;
151-
// if we can, we will replace uses of it by the
152-
// this is intentionally a copy because we're just about to change
153-
// the instruction this iterator points to
151+
// this is intentionally a copy because we're about to change the
152+
// instruction this iterator points to
153+
// if we can, we will replace uses of it by a case distinction over
154+
// given functions the function pointer can point to
154155
auto const original_function_call_instruction = *location;
155156
*location = goto_programt::make_assertion(
156157
false_exprt{},
@@ -279,15 +280,10 @@ parse_function_pointer_restrictions_from_command_line(
279280
"--" RESTRICT_FUNCTION_POINTER_OPT,
280281
restriction_format_message};
281282
}
282-
auto const target_names = ([&target_name_strings] {
283-
auto target_names = std::unordered_set<irep_idt>{};
284-
std::transform(
285-
target_name_strings.begin(),
286-
target_name_strings.end(),
287-
std::inserter(target_names, target_names.end()),
288-
string2id);
289-
return target_names;
290-
})();
283+
284+
std::unordered_set<irep_idt> target_names;
285+
target_names.insert(target_name_strings.begin(), target_name_strings.end());
286+
291287
for(auto const &target_name : target_names)
292288
{
293289
if(target_name == ID_empty_string)

src/goto-programs/restrict_function_pointers.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ Author: Diffblue Ltd.
3131
"function-pointer-restrictions-file"
3232

3333
#define OPT_RESTRICT_FUNCTION_POINTER \
34-
"(" RESTRICT_FUNCTION_POINTER_OPT \
35-
"):" \
34+
"(" RESTRICT_FUNCTION_POINTER_OPT "):" \
3635
"(" RESTRICT_FUNCTION_POINTER_FROM_FILE_OPT "):"
3736

38-
#define RESTRICT_FUNCTION_POINTER_HELP \
37+
#define HELP_RESTRICT_FUNCTION_POINTER \
3938
"--" RESTRICT_FUNCTION_POINTER_OPT \
4039
" <pointer_name>/<target[,targets]*>\n" \
4140
" restrict a function pointer to a set of possible targets\n" \
@@ -44,7 +43,7 @@ Author: Diffblue Ltd.
4443
" works for globals and function parameters right now\n" \
4544
"--" RESTRICT_FUNCTION_POINTER_FROM_FILE_OPT \
4645
" <file_name>\n" \
47-
" add from function pointer restrictions from file"
46+
" add function pointer restrictions from file"
4847

4948
void parse_function_pointer_restriction_options_from_cmdline(
5049
const cmdlinet &cmdline,
@@ -72,7 +71,7 @@ struct function_pointer_restrictionst
7271
void write_to_file(const std::string &filename) const;
7372
};
7473

75-
/// Apply a function pointer restrictions to a goto_model. Each restriction is a
74+
/// Apply function pointer restrictions to a goto_model. Each restriction is a
7675
/// mapping from a pointer name to a set of possible targets. Replace calls of
7776
/// these "restricted" pointers with a branch on the value of the function
7877
/// pointer, comparing it to the set of possible targets. This also adds an

src/util/irep.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ inline const std::string &id2string(const irep_idt &d)
5050
#endif
5151
}
5252

53-
inline irep_idt string2id(const std::string &id_string)
54-
{
55-
return irep_idt{id_string};
56-
}
57-
5853
inline const std::string &name2string(const irep_namet &n)
5954
{
6055
#ifdef USE_DSTRING

0 commit comments

Comments
 (0)