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 <util/pointer_expr.h>
13 
14 #include "goto_functions.h"
15 
16 namespace
17 {
18 class validate_goto_modelt
19 {
20 public:
21  using function_mapt = goto_functionst::function_mapt;
22 
23  validate_goto_modelt(
24  const goto_functionst &goto_functions,
25  const validation_modet vm,
26  const goto_model_validation_optionst goto_model_validation_options);
27 
28 private:
33  void entry_point_exists();
34 
44  void check_called_functions();
45 
46  const validation_modet vm;
47  const function_mapt &function_map;
48 };
49 
50 validate_goto_modelt::validate_goto_modelt(
51  const goto_functionst &goto_functions,
52  const validation_modet vm,
53  const goto_model_validation_optionst validation_options)
54  : vm{vm}, function_map{goto_functions.function_map}
55 {
56  if(validation_options.entry_point_exists)
57  entry_point_exists();
58 
59  check_called_functions();
60 }
61 
62 void validate_goto_modelt::entry_point_exists()
63 {
64  DATA_CHECK(
65  vm,
66  function_map.find(goto_functionst::entry_point()) != function_map.end(),
67  "an entry point must exist");
68 }
69 
70 void validate_goto_modelt::check_called_functions()
71 {
72  auto test_for_function_address =
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 
82  DATA_CHECK(
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
104  DATA_CHECK(
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 
113  DATA_CHECK(
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());
121  src.visit_pre(test_for_function_address);
122  }
123  }
124 }
125 
126 } // namespace
127 
129  const goto_functionst &goto_functions,
130  const validation_modet vm,
131  const goto_model_validation_optionst validation_options)
132 {
133  validate_goto_modelt{goto_functions, vm, validation_options};
134 }
exprt & object()
Definition: pointer_expr.h:549
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.
const irep_idt & get_identifier() const
Definition: std_expr.h:160
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.
Definition: pointer_expr.h:577
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