cprover
thread_instrumentation.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
10 
11 #include <util/c_types.h>
12 #include <util/pointer_expr.h>
13 #include <util/string_constant.h>
14 
16 
17 static bool has_start_thread(const goto_programt &goto_program)
18 {
19  for(const auto &instruction : goto_program.instructions)
20  if(instruction.is_start_thread())
21  return true;
22 
23  return false;
24 }
25 
27 {
28  if(goto_program.instructions.empty())
29  return;
30 
31  // add assertion that all may flags for mutex-locked are gone
32  // at the end
33  goto_programt::targett end=goto_program.instructions.end();
34  end--;
35 
36  assert(end->is_end_function());
37 
38  source_locationt source_location=end->source_location;
39 
40  goto_program.insert_before_swap(end);
41 
42  const string_constantt mutex_locked_string("mutex-locked");
43 
44  // NULL is any
45  binary_predicate_exprt get_may{
47  ID_get_may,
48  address_of_exprt(mutex_locked_string)};
49 
50  *end = goto_programt::make_assertion(not_exprt(get_may), source_location);
51 
52  end->source_location.set_comment("mutexes must not be locked on thread exit");
53 }
54 
56 {
57  // we'll look for START THREAD
58  std::set<irep_idt> thread_fkts;
59 
60  for(const auto &gf_entry : goto_model.goto_functions.function_map)
61  {
62  if(has_start_thread(gf_entry.second.body))
63  {
64  // now look for functions called
65 
66  for(const auto &instruction : gf_entry.second.body.instructions)
67  if(instruction.is_function_call())
68  {
69  const exprt &function=
70  to_code_function_call(instruction.code).function();
71  if(function.id()==ID_symbol)
72  thread_fkts.insert(to_symbol_expr(function).get_identifier());
73  }
74  }
75  }
76 
77  // now instrument
78  for(const auto &fkt : thread_fkts)
80  goto_model.goto_functions.function_map[fkt].body);
81 }
82 
84  const symbol_tablet &symbol_table,
85  goto_programt &goto_program,
86  typet lock_type)
87 {
88  symbol_tablet::symbolst::const_iterator f_it =
89  symbol_table.symbols.find(CPROVER_PREFIX "set_must");
90 
91  if(f_it==symbol_table.symbols.end())
92  return;
93 
94  Forall_goto_program_instructions(it, goto_program)
95  {
96  if(it->is_assign())
97  {
98  const code_assignt &code_assign=
99  to_code_assign(it->code);
100 
101  if(code_assign.lhs().type()==lock_type)
102  {
103  const code_function_callt call(
104  f_it->second.symbol_expr(),
105  {address_of_exprt(code_assign.lhs()),
106  address_of_exprt(string_constantt("mutex-init"))});
107 
108  goto_program.insert_after(
110  }
111  }
112  }
113 }
114 
116 {
117  // get pthread_mutex_lock
118 
119  symbol_tablet::symbolst::const_iterator lock_entry =
120  goto_model.symbol_table.symbols.find("pthread_mutex_lock");
121 
122  if(lock_entry == goto_model.symbol_table.symbols.end())
123  return;
124 
125  // get type of lock argument
126  code_typet code_type = to_code_type(to_code_type(lock_entry->second.type));
127  if(code_type.parameters().size()!=1)
128  return;
129 
130  typet lock_type=code_type.parameters()[0].type();
131 
132  if(lock_type.id()!=ID_pointer)
133  return;
134 
135  for(auto &gf_entry : goto_model.goto_functions.function_map)
136  {
138  goto_model.symbol_table,
139  gf_entry.second.body,
140  to_pointer_type(lock_type).subtype());
141  }
142 }
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:243
Operator to return the address of an object.
Definition: pointer_expr.h:200
A base class for expressions that are predicates, i.e., Boolean-typed, and that take exactly two argu...
Definition: std_expr.h:644
A codet representing an assignment in the program.
Definition: std_code.h:295
exprt & lhs()
Definition: std_code.h:312
codet representation of a function call statement.
Definition: std_code.h:1215
exprt & function()
Definition: std_code.h:1250
Base type of functions.
Definition: std_types.h:744
const parameterst & parameters() const
Definition: std_types.h:860
The empty type.
Definition: std_types.h:46
Base class for all expressions.
Definition: expr.h:54
const source_locationt & source_location() const
Definition: expr.h:234
typet & type()
Return the type of the expression.
Definition: expr.h:82
function_mapt function_map
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:74
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:556
void insert_before_swap(targett target)
Insertion that preserves jumps to "target".
Definition: goto_program.h:577
instructionst::iterator targett
Definition: goto_program.h:550
targett insert_after(const_targett target)
Insertion after the instruction pointed-to by the given instruction iterator target.
Definition: goto_program.h:626
static instructiont make_function_call(const code_function_callt &_code, const source_locationt &l=source_locationt::nil())
Create a function call instruction.
static instructiont make_assertion(const exprt &g, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:864
const irep_idt & id() const
Definition: irep.h:407
Boolean negation.
Definition: std_expr.h:2042
The null pointer constant.
Definition: std_expr.h:2751
const symbolst & symbols
Read-only field, used to look up symbols given their names.
The symbol table.
Definition: symbol_table.h:20
The type of an expression, extends irept.
Definition: type.h:28
#define CPROVER_PREFIX
Symbol Table + CFG.
#define Forall_goto_program_instructions(it, program)
API to expression classes for Pointers.
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:1326
const code_assignt & to_code_assign(const codet &code)
Definition: std_code.h:383
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:190
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:949
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
Definition: std_types.h:1533
void thread_exit_instrumentation(goto_programt &goto_program)
void mutex_init_instrumentation(const symbol_tablet &symbol_table, goto_programt &goto_program, typet lock_type)
static bool has_start_thread(const goto_programt &goto_program)