Skip to content

Commit be933ca

Browse files
committed
Get function pointer targets in json format.
Add --get-fp-values option to goto-analyzer for all callsites in json format to be fed to goto-instrument.
1 parent 8e606ac commit be933ca

File tree

6 files changed

+127
-13
lines changed

6 files changed

+127
-13
lines changed

src/analyses/variable-sensitivity/abstract_enviroment.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ class abstract_environmentt
7777

7878
abstract_object_statisticst gather_statistics(const namespacet &ns) const;
7979

80-
protected:
81-
bool bottom;
82-
8380
// We may need to break out more of these cases into these
8481
virtual abstract_object_pointert eval_expression(
8582
const exprt &e, const namespacet &ns) const;
8683

84+
protected:
85+
bool bottom;
86+
8787
sharing_mapt<map_keyt, abstract_object_pointert> map;
8888

8989
private:

src/analyses/variable-sensitivity/variable_sensitivity_domain.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ class variable_sensitivity_domaint:public ai_domain_baset
114114
bool is_bottom() const override;
115115
bool is_top() const override;
116116

117-
protected:
118117
virtual abstract_object_pointert eval(
119118
const exprt &expr, const namespacet &ns) const
120119
{

src/goto-analyzer/goto_analyzer_parse_options.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Author: Daniel Kroening, [email protected]
3131
#include <goto-programs/goto_convert_functions.h>
3232
#include <goto-programs/goto_inline.h>
3333
#include <goto-programs/initialize_goto_model.h>
34+
#include <goto-programs/label_function_pointer_call_sites.h>
3435
#include <goto-programs/link_to_library.h>
3536
#include <goto-programs/read_goto_binary.h>
3637
#include <goto-programs/remove_complex.h>
@@ -64,6 +65,7 @@ Author: Daniel Kroening, [email protected]
6465
#include <util/version.h>
6566

6667
#include "show_on_source.h"
68+
#include "show_function_pointer_values.h"
6769
#include "static_show_domain.h"
6870
#include "static_simplifier.h"
6971
#include "static_verifier.h"
@@ -331,6 +333,9 @@ void goto_analyzer_parse_optionst::get_command_line_options(optionst &options)
331333
options.set_option("data-dependencies", cmdline.isset("data-dependencies"));
332334
options.set_option("interval", cmdline.isset("interval-values"));
333335
options.set_option("value-set", cmdline.isset("value-set"));
336+
if(cmdline.isset("get-fp-values")) {
337+
options.set_option("function-pointer-values", cmdline.get_value("get-fp-values"));
338+
}
334339
}
335340
else if(cmdline.isset("dependence-graph-vs"))
336341
{
@@ -693,6 +698,12 @@ int goto_analyzer_parse_optionst::perform_analysis(const optionst &options)
693698
show_on_source(goto_model, *analyzer, ui_message_handler);
694699
return CPROVER_EXIT_SUCCESS;
695700
}
701+
else if(options.is_set("function-pointer-values"))
702+
{
703+
std::string filename = options.get_option("function-pointer-values");
704+
print_function_pointer_values(goto_model, *analyzer, options, ui_message_handler, filename);
705+
return CPROVER_EXIT_SUCCESS;
706+
}
696707
else if(options.get_bool_option("verify"))
697708
{
698709
result = static_verifier(
@@ -760,6 +771,10 @@ bool goto_analyzer_parse_optionst::process_goto_program(
760771
link_to_library(goto_model, ui_message_handler, cprover_c_library_factory);
761772
#endif
762773

774+
if(options.is_set("function-pointer-values")) {
775+
label_function_pointer_call_sites(goto_model);
776+
}
777+
763778
// remove function pointers
764779
log.status() << "Removing function pointers and virtual functions"
765780
<< messaget::eom;
@@ -775,14 +790,6 @@ bool goto_analyzer_parse_optionst::process_goto_program(
775790
remove_vector(goto_model);
776791
remove_complex(goto_model);
777792

778-
#if 0
779-
// add generic checks
780-
log.status() << "Generic Property Instrumentation" << messaget::eom;
781-
goto_check(options, goto_model);
782-
#else
783-
(void)options; // unused parameter
784-
#endif
785-
786793
// recalculate numbers, etc.
787794
goto_model.goto_functions.update();
788795

src/goto-analyzer/goto_analyzer_parse_options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class optionst;
152152
"(non-null)(show-non-null)" \
153153
"(variable)" \
154154
"(variable-sensitivity)" \
155-
"(pointers)(arrays)(structs)(value-set)(data-dependencies)" \
155+
"(pointers)(arrays)(structs)(value-set)(data-dependencies)(get-fp-values):" \
156156
"(constants)" \
157157
"(dependence-graph)" \
158158
"(dependence-graph-vs)" \
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Module: Show function pointer values
2+
// Author: Fotis Koutoulakis, [email protected]
3+
// Copyright: Diffblue Ltd, 2020
4+
5+
#include "show_function_pointer_values.h"
6+
7+
#include <util/message.h>
8+
#include <util/namespace.h>
9+
10+
#include <goto-programs/goto_model.h>
11+
#include <goto-programs/restrict_function_pointers.h>
12+
13+
#include <analyses/ai.h>
14+
#include <analyses/variable-sensitivity/abstract_enviroment.h>
15+
#include <analyses/variable-sensitivity/variable_sensitivity_domain.h>
16+
#include <analyses/variable-sensitivity/value_set_abstract_object.h>
17+
18+
#include <regex>
19+
#include <string>
20+
21+
void print_function_pointer_values(
22+
const goto_modelt &goto_model,
23+
const ai_baset &ai,
24+
const optionst &options,
25+
message_handlert &message_handler,
26+
std::string filename)
27+
{
28+
function_pointer_restrictionst::restrictionst map;
29+
namespacet ns(goto_model.symbol_table);
30+
31+
messaget m(message_handler);
32+
m.status() << "Getting function pointer targets at specific callsites" << messaget::eom;
33+
34+
for(auto const &function_entry : goto_model.goto_functions.function_map)
35+
{
36+
for_each_goto_location(function_entry.second, [&](ai_baset::locationt instruction_location)
37+
{
38+
auto is_assign = instruction_location->is_assign();
39+
if (is_assign)
40+
{
41+
auto assign_stmt = instruction_location->get_assign();
42+
auto lhs = assign_stmt.lhs();
43+
std::string identifier =
44+
id2string(to_symbol_expr(lhs).get_identifier());
45+
// <name of function>.function_pointer_call.<integer>
46+
std::regex name(R"(\w+\.function_pointer_call\.\d+)");
47+
if (std::regex_match(identifier, name)) {
48+
auto const state_after_unique_ptr = ai.abstract_state_after(instruction_location);
49+
PRECONDITION(state_after_unique_ptr != nullptr);
50+
auto state_after =
51+
dynamic_cast<variable_sensitivity_domaint *>(
52+
state_after_unique_ptr.get());
53+
CHECK_RETURN(state_after != nullptr);
54+
auto const lhs_identifier = to_symbol_expr(lhs).get_identifier();
55+
56+
if (state_after->is_top())
57+
{
58+
// for top, we don't add restrictions (i.e. normal function pointer removal should take care of this).
59+
}
60+
else if (state_after->is_bottom())
61+
{
62+
// call site is unreachable, so this is empty set
63+
map[lhs_identifier] = {};
64+
}
65+
else
66+
{
67+
auto evaluated_object =
68+
state_after->eval(lhs, ns).get();
69+
auto value_set_object =
70+
(value_set_abstract_objectt *) evaluated_object;
71+
auto values = value_set_object->get_values();
72+
auto get_function_pointer_name = [](const exprt &function_pointer) {
73+
return to_symbol_expr(to_address_of_expr(function_pointer).op()).get_identifier();
74+
};
75+
76+
for (const auto &elem : values)
77+
{
78+
map[lhs_identifier].insert(get_function_pointer_name(elem));
79+
}
80+
}
81+
}
82+
}
83+
});
84+
}
85+
function_pointer_restrictionst{std::move(map)}.write_to_file(filename);
86+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Module: Show function pointer values
2+
// Author: Fotis Koutoulakis, [email protected]
3+
// Copyright: Diffblue Ltd, 2020
4+
5+
#pragma once
6+
7+
#include <string>
8+
9+
class goto_modelt;
10+
class ai_baset;
11+
class optionst;
12+
class message_handlert;
13+
14+
/// Print the values of function pointer call sites as JSON to a file.
15+
/// Requires label_function_pointer_call_sites and the ai to have already
16+
/// run on the goto model to work properly.
17+
void print_function_pointer_values(
18+
const goto_modelt &goto_model,
19+
const ai_baset &ai,
20+
const optionst &options,
21+
message_handlert &message_handler,
22+
std::string filename);

0 commit comments

Comments
 (0)