CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mm_io.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Perform Memory-mapped I/O instrumentation
4
5Author: Daniel Kroening
6
7Date: April 2017
8
9\*******************************************************************/
10
13
14#include "mm_io.h"
15
16#include <util/fresh_symbol.h>
17#include <util/message.h>
18#include <util/pointer_expr.h>
21#include <util/replace_expr.h>
22
23#include "goto_model.h"
24
25#include <set>
26
27class mm_iot
28{
29public:
30 explicit mm_iot(symbol_table_baset &symbol_table);
31
32 void mm_io(goto_functionst::goto_functiont &goto_function);
33
34 std::size_t reads_replaced = 0;
35 std::size_t writes_replaced = 0;
36
37protected:
38 const irep_idt id_r = CPROVER_PREFIX "mm_io_r";
39 const irep_idt id_w = CPROVER_PREFIX "mm_io_w";
40
45};
46
48 : ns(symbol_table),
49 mm_io_r(nil_exprt{}),
50 mm_io_r_value(nil_exprt{}),
51 mm_io_w(nil_exprt{})
52{
53 if(const auto mm_io_r_symbol = symbol_table.lookup(id_r))
54 {
55 mm_io_r = mm_io_r_symbol->symbol_expr();
56
58 to_code_type(mm_io_r.type()).return_type(),
59 id2string(id_r) + "$value",
60 id2string(id_r) + "$value",
61 mm_io_r_symbol->location,
62 mm_io_r_symbol->mode,
63 symbol_table)
64 .symbol_expr();
65 }
66
67 if(const auto mm_io_w_symbol = symbol_table.lookup(id_w))
68 mm_io_w = mm_io_w_symbol->symbol_expr();
69}
70
71static std::set<dereference_exprt> collect_deref_expr(const exprt &src)
72{
73 std::set<dereference_exprt> collected;
74 src.visit_pre([&collected](const exprt &e) {
75 if(e.id() == ID_dereference)
77 });
78 return collected;
79}
80
82{
83 // return early when there is nothing to be done
84 if(mm_io_r.is_nil() && mm_io_w.is_nil())
85 return;
86
87 for(auto it = goto_function.body.instructions.begin();
88 it != goto_function.body.instructions.end();
89 it++)
90 {
91 if(!it->is_assign())
92 continue;
93
94 auto &a_lhs = it->assign_lhs();
95 auto &a_rhs = it->assign_rhs_nonconst();
97
99 {
100 if(deref_expr_r.size() == 1)
101 {
102 const dereference_exprt &d = *deref_expr_r.begin();
103 source_locationt source_location = it->source_location();
105
109 d);
111
112 const typet &pt = ct.parameters()[0].type();
113 const typet &st = ct.parameters()[1].type();
114 auto size_opt = size_of_expr(d.type(), ns);
115 CHECK_RETURN(size_opt.has_value());
118 mm_io_r,
120 typecast_exprt(size_opt.value(), st)},
121 source_location);
122 goto_function.body.insert_before_swap(it, call);
124 it++;
125 }
126 }
127
128 if(mm_io_w.is_not_nil())
129 {
130 if(a_lhs.id() == ID_dereference)
131 {
133 source_locationt source_location = it->source_location();
135 const typet &pt = ct.parameters()[0].type();
136 const typet &st = ct.parameters()[1].type();
137 const typet &vt = ct.parameters()[2].type();
138 auto size_opt = size_of_expr(d.type(), ns);
139 CHECK_RETURN(size_opt.has_value());
141 mm_io_w,
143 typecast_exprt(size_opt.value(), st),
145 goto_function.body.insert_before_swap(it);
146 *it = goto_programt::make_function_call(fc, source_location);
148 it++;
149 }
150 }
151 }
152}
153
154void mm_io(
155 symbol_tablet &symbol_table,
156 goto_functionst &goto_functions,
157 message_handlert &message_handler)
158{
159 mm_iot rewrite{symbol_table};
160
161 for(auto &f : goto_functions.function_map)
162 rewrite.mm_io(f.second);
163
164 if(rewrite.reads_replaced || rewrite.writes_replaced)
165 {
166 messaget log{message_handler};
167 log.status() << "Replaced MMIO operations: " << rewrite.reads_replaced
168 << " read(s), " << rewrite.writes_replaced << " write(s)"
169 << messaget::eom;
170 }
171}
172
173void mm_io(goto_modelt &model, message_handlert &message_handler)
174{
175 mm_io(model.symbol_table, model.goto_functions, message_handler);
176}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
goto_instruction_codet representation of a function call statement.
Base type of functions.
Definition std_types.h:583
Operator to dereference a pointer.
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
typet & type()
Return the type of the expression.
Definition expr.h:84
A collection of goto functions.
function_mapt function_map
::goto_functiont goto_functiont
symbol_tablet symbol_table
Symbol table.
Definition goto_model.h:31
goto_functionst goto_functions
GOTO functions.
Definition goto_model.h:34
static instructiont make_function_call(const code_function_callt &_code, const source_locationt &l=source_locationt::nil())
Create a function call instruction.
The trinary if-then-else operator.
Definition std_expr.h:2497
bool is_not_nil() const
Definition irep.h:372
const irep_idt & id() const
Definition irep.h:388
bool is_nil() const
Definition irep.h:368
Class that provides messages with a built-in verbosity 'level'.
Definition message.h:154
static eomt eom
Definition message.h:289
const irep_idt id_r
Definition mm_io.cpp:38
const irep_idt id_w
Definition mm_io.cpp:39
std::size_t reads_replaced
Definition mm_io.cpp:34
exprt mm_io_r
Definition mm_io.cpp:42
mm_iot(symbol_table_baset &symbol_table)
Definition mm_io.cpp:47
std::size_t writes_replaced
Definition mm_io.cpp:35
exprt mm_io_r_value
Definition mm_io.cpp:43
void mm_io(goto_functionst::goto_functiont &goto_function)
Definition mm_io.cpp:81
exprt mm_io_w
Definition mm_io.cpp:44
const namespacet ns
Definition mm_io.cpp:41
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition namespace.h:91
The NIL expression.
Definition std_expr.h:3208
The symbol table base class interface.
const symbolt * lookup(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
The symbol table.
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition symbol.cpp:121
Semantic type conversion.
Definition std_expr.h:2073
static exprt conditional_cast(const exprt &expr, const typet &type)
Definition std_expr.h:2081
The type of an expression, extends irept.
Definition type.h:29
#define CPROVER_PREFIX
symbolt & get_fresh_aux_symbol(const typet &type, const std::string &name_prefix, const std::string &basename_prefix, const source_locationt &source_location, const irep_idt &symbol_mode, const namespacet &ns, symbol_table_baset &symbol_table)
Installs a fresh-named symbol with respect to the given namespace ns with the requested name pattern ...
Fresh auxiliary symbol creation.
Symbol Table + CFG.
const std::string & id2string(const irep_idt &d)
Definition irep.h:44
double log(double x)
Definition math.c:2449
void mm_io(symbol_tablet &symbol_table, goto_functionst &goto_functions, message_handlert &message_handler)
Definition mm_io.cpp:154
static std::set< dereference_exprt > collect_deref_expr(const exprt &src)
Definition mm_io.cpp:71
Perform Memory-mapped I/O instrumentation.
API to expression classes for Pointers.
const dereference_exprt & to_dereference_expr(const exprt &expr)
Cast an exprt to a dereference_exprt.
std::optional< exprt > size_of_expr(const typet &type, const namespacet &ns)
Pointer Logic.
exprt integer_address(const exprt &pointer)
Various predicates over pointers in programs.
bool replace_expr(const exprt &what, const exprt &by, exprt &dest)
#define CHECK_RETURN(CONDITION)
Definition invariant.h:495
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition std_types.h:788