cprover
mm_io.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Perform Memory-mapped I/O instrumentation
4 
5 Author: Daniel Kroening
6 
7 Date: April 2017
8 
9 \*******************************************************************/
10 
13 
14 #include "mm_io.h"
15 
16 #include <util/pointer_expr.h>
19 #include <util/replace_expr.h>
20 
21 #include "remove_returns.h"
22 
24  const exprt &src,
25  std::set<dereference_exprt> &dest)
26 {
27  src.visit_pre([&dest](const exprt &e) {
28  if(e.id() == ID_dereference)
29  dest.insert(to_dereference_expr(e));
30  });
31 }
32 
33 void mm_io(
34  const exprt &mm_io_r,
35  const exprt &mm_io_w,
36  goto_functionst::goto_functiont &goto_function,
37  const namespacet &ns)
38 {
39  for(goto_programt::instructionst::iterator it=
40  goto_function.body.instructions.begin();
41  it!=goto_function.body.instructions.end();
42  it++)
43  {
44  std::set<dereference_exprt> deref_expr_w, deref_expr_r;
45 
46  if(it->is_assign())
47  {
48  auto a = it->get_assign();
49  collect_deref_expr(a.rhs(), deref_expr_r);
50 
51  if(mm_io_r.is_not_nil())
52  {
53  if(deref_expr_r.size()==1)
54  {
55  const dereference_exprt &d=*deref_expr_r.begin();
56  source_locationt source_location=it->source_location;
57  const code_typet &ct=to_code_type(mm_io_r.type());
58 
59  irep_idt identifier=to_symbol_expr(mm_io_r).get_identifier();
60  auto return_value = return_value_symbol(identifier, ns);
61  if_exprt if_expr(integer_address(d.pointer()), return_value, d);
62  if(!replace_expr(d, if_expr, a.rhs()))
63  it->set_assign(a);
64 
65  const typet &pt=ct.parameters()[0].type();
66  const typet &st=ct.parameters()[1].type();
67  auto size_opt = size_of_expr(d.type(), ns);
68  CHECK_RETURN(size_opt.has_value());
69  const code_function_callt fc(
70  mm_io_r,
71  {typecast_exprt(d.pointer(), pt),
72  typecast_exprt(size_opt.value(), st)});
73  goto_function.body.insert_before_swap(it);
74  *it = goto_programt::make_function_call(fc, source_location);
75  it++;
76  }
77  }
78 
79  if(mm_io_w.is_not_nil())
80  {
81  if(a.lhs().id()==ID_dereference)
82  {
83  const dereference_exprt &d=to_dereference_expr(a.lhs());
84  source_locationt source_location=it->source_location;
85  const code_typet &ct=to_code_type(mm_io_w.type());
86  const typet &pt=ct.parameters()[0].type();
87  const typet &st=ct.parameters()[1].type();
88  const typet &vt=ct.parameters()[2].type();
89  auto size_opt = size_of_expr(d.type(), ns);
90  CHECK_RETURN(size_opt.has_value());
91  const code_function_callt fc(
92  mm_io_w,
93  {typecast_exprt(d.pointer(), pt),
94  typecast_exprt(size_opt.value(), st),
95  typecast_exprt(a.rhs(), vt)});
96  goto_function.body.insert_before_swap(it);
97  *it = goto_programt::make_function_call(fc, source_location);
98  it++;
99  }
100  }
101  }
102  }
103 }
104 
105 void mm_io(
106  const symbol_tablet &symbol_table,
107  goto_functionst &goto_functions)
108 {
109  const namespacet ns(symbol_table);
110  exprt mm_io_r=nil_exprt(), mm_io_w=nil_exprt();
111 
112  irep_idt id_r=CPROVER_PREFIX "mm_io_r";
113  irep_idt id_w=CPROVER_PREFIX "mm_io_w";
114 
115  auto maybe_symbol=symbol_table.lookup(id_r);
116  if(maybe_symbol)
117  mm_io_r=maybe_symbol->symbol_expr();
118 
119  maybe_symbol=symbol_table.lookup(id_w);
120  if(maybe_symbol)
121  mm_io_w=maybe_symbol->symbol_expr();
122 
123  for(auto & f : goto_functions.function_map)
124  mm_io(mm_io_r, mm_io_w, f.second, ns);
125 }
126 
127 void mm_io(goto_modelt &model)
128 {
129  mm_io(model.symbol_table, model.goto_functions);
130 }
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
pointer_offset_size.h
Pointer Logic.
symbol_tablet
The symbol table.
Definition: symbol_table.h:20
to_dereference_expr
const dereference_exprt & to_dereference_expr(const exprt &expr)
Cast an exprt to a dereference_exprt.
Definition: pointer_expr.h:442
CHECK_RETURN
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:496
typet
The type of an expression, extends irept.
Definition: type.h:28
mm_io
void mm_io(const exprt &mm_io_r, const exprt &mm_io_w, goto_functionst::goto_functiont &goto_function, const namespacet &ns)
Definition: mm_io.cpp:33
dereference_exprt
Operator to dereference a pointer.
Definition: pointer_expr.h:386
if_exprt
The trinary if-then-else operator.
Definition: std_expr.h:2086
pointer_predicates.h
Various predicates over pointers in programs.
exprt
Base class for all expressions.
Definition: expr.h:54
goto_modelt
Definition: goto_model.h:26
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:27
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
goto_programt::make_function_call
static instructiont make_function_call(const code_function_callt &_code, const source_locationt &l=source_locationt::nil())
Create a function call instruction.
Definition: goto_program.h:1033
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:82
code_function_callt
codet representation of a function call statement.
Definition: std_code.h:1215
irept::is_not_nil
bool is_not_nil() const
Definition: irep.h:391
collect_deref_expr
void collect_deref_expr(const exprt &src, std::set< dereference_exprt > &dest)
Definition: mm_io.cpp:23
to_code_type
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:738
mm_io.h
Perform Memory-mapped I/O instrumentation.
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition: std_expr.h:109
nil_exprt
The NIL expression.
Definition: std_expr.h:2734
dereference_exprt::pointer
exprt & pointer()
Definition: pointer_expr.h:399
symbolt::symbol_expr
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:122
pointer_expr.h
API to expression classes for Pointers.
integer_address
exprt integer_address(const exprt &pointer)
Definition: pointer_predicates.cpp:130
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:189
code_typet
Base type of functions.
Definition: std_types.h:533
irept::id
const irep_idt & id() const
Definition: irep.h:407
code_typet::parameters
const parameterst & parameters() const
Definition: std_types.h:649
remove_returns.h
Replace function returns by assignments to global variables.
source_locationt
Definition: source_location.h:20
goto_functionst::goto_functiont
::goto_functiont goto_functiont
Definition: goto_functions.h:25
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:23
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
replace_expr
bool replace_expr(const exprt &what, const exprt &by, exprt &dest)
Definition: replace_expr.cpp:12
CPROVER_PREFIX
#define CPROVER_PREFIX
Definition: cprover_prefix.h:14
exprt::visit_pre
void visit_pre(std::function< void(exprt &)>)
Definition: expr.cpp:272
return_value_symbol
symbol_exprt return_value_symbol(const irep_idt &identifier, const namespacet &ns)
produces the symbol that is used to store the return value of the function with the given identifier
Definition: remove_returns.cpp:416
symbol_table_baset::lookup
const symbolt * lookup(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
Definition: symbol_table_base.h:95
size_of_expr
optionalt< exprt > size_of_expr(const typet &type, const namespacet &ns)
Definition: pointer_offset_size.cpp:278
replace_expr.h
typecast_exprt
Semantic type conversion.
Definition: std_expr.h:1780
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30