CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
validate_goto_model.cpp
Go to the documentation of this file.
1/*******************************************************************\
2Module: Validate Goto Programs
3
4Author: Diffblue
5
6Date: Oct 2018
7
8\*******************************************************************/
9
10#include "validate_goto_model.h"
11
12#include <util/pointer_expr.h>
13
14#include "goto_functions.h"
15
16namespace
17{
18class validate_goto_modelt
19{
20public:
21 using function_mapt = goto_functionst::function_mapt;
22
23 validate_goto_modelt(
24 const goto_functionst &goto_functions,
25 const validation_modet vm,
27
28private:
33 void entry_point_exists();
34
45
46 const validation_modet vm;
47 const function_mapt &function_map;
48};
49
50validate_goto_modelt::validate_goto_modelt(
51 const goto_functionst &goto_functions,
52 const validation_modet vm,
54 : vm{vm}, function_map{goto_functions.function_map}
55{
56 if(validation_options.entry_point_exists)
57 entry_point_exists();
58
60}
61
62void validate_goto_modelt::entry_point_exists()
63{
65 vm,
66 function_map.find(goto_functionst::entry_point()) != function_map.end(),
67 "an entry point must exist");
68}
69
70void validate_goto_modelt::check_called_functions()
71{
73 [this](const exprt &expr) {
74 if(expr.id() == ID_address_of)
75 {
76 const auto &pointee = to_address_of_expr(expr).object();
77
78 if(pointee.id() == ID_symbol && pointee.type().id() == ID_code)
79 {
80 const auto &identifier = to_symbol_expr(pointee).get_identifier();
81
83 vm,
84 function_map.find(identifier) != function_map.end(),
85 "every function whose address is taken must be in the "
86 "function map");
87 }
88 }
89 };
90
91 for(const auto &fun : function_map)
92 {
93 for(const auto &instr : fun.second.body.instructions)
94 {
95 // check functions that are called
96 if(instr.is_function_call())
97 {
98 // calls through function pointers are represented by dereference
99 // expressions
100 if(instr.call_function().id() == ID_dereference)
101 continue;
102
103 // the only other permitted expression is a symbol
105 vm,
106 instr.call_function().id() == ID_symbol &&
107 instr.call_function().type().id() == ID_code,
108 "function call expected to be code-typed symbol expression");
109
110 const irep_idt &identifier =
111 to_symbol_expr(instr.call_function()).get_identifier();
112
114 vm,
115 function_map.find(identifier) != function_map.end(),
116 "every function call callee must be in the function map");
117 }
118
119 // check functions of which the address is taken
120 const auto &src = static_cast<const exprt &>(instr.code());
122 }
123 }
124}
125
126} // namespace
127
129 const goto_functionst &goto_functions,
130 const validation_modet vm,
132{
133 validate_goto_modelt{goto_functions, vm, validation_options};
134}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
Base class for all expressions.
Definition expr.h:56
void visit_pre(std::function< void(exprt &)>)
Definition expr.cpp:227
A collection of goto functions.
std::map< irep_idt, goto_functiont > function_mapt
static irep_idt entry_point()
Get the identifier of the entry point to a goto model.
Goto Programs with Functions.
API to expression classes for Pointers.
const address_of_exprt & to_address_of_expr(const exprt &expr)
Cast an exprt to an address_of_exprt.
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition std_expr.h:272
#define DATA_CHECK(vm, condition, message)
This macro takes a condition which denotes a well-formedness criterion on goto programs,...
Definition validate.h:22
void validate_goto_model(const goto_functionst &goto_functions, const validation_modet vm, const goto_model_validation_optionst validation_options)
validation_modet