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