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/string_constant.h>
13 
15 
17 {
18  for(const auto &instruction : goto_program.instructions)
19  if(instruction.is_start_thread())
20  return true;
21 
22  return false;
23 }
24 
26 {
27  if(goto_program.instructions.empty())
28  return;
29 
30  // add assertion that all may flags for mutex-locked are gone
31  // at the end
33  end--;
34 
35  assert(end->is_end_function());
36 
37  source_locationt source_location=end->source_location;
38  irep_idt function=end->function;
39 
41 
42  const string_constantt mutex_locked_string("mutex-locked");
43 
44  binary_exprt get_may("get_may");
45 
46  // NULL is any
48  get_may.op1()=address_of_exprt(mutex_locked_string);
49 
50  end->make_assertion(not_exprt(get_may));
51 
52  end->source_location=source_location;
53  end->source_location.set_comment("mutexes must not be locked on thread exit");
54  end->function=function;
55 }
56 
58 {
59  // we'll look for START THREAD
60  std::set<irep_idt> thread_fkts;
61 
62  forall_goto_functions(f_it, goto_model.goto_functions)
63  {
64  if(has_start_thread(f_it->second.body))
65  {
66  // now look for functions called
67 
68  for(const auto &instruction : f_it->second.body.instructions)
69  if(instruction.is_function_call())
70  {
71  const exprt &function=
72  to_code_function_call(instruction.code).function();
73  if(function.id()==ID_symbol)
74  thread_fkts.insert(to_symbol_expr(function).get_identifier());
75  }
76  }
77  }
78 
79  // now instrument
80  for(const auto &fkt : thread_fkts)
82  goto_model.goto_functions.function_map[fkt].body);
83 }
84 
88  typet lock_type)
89 {
90  symbol_tablet::symbolst::const_iterator f_it=
91  symbol_table.symbols.find("__CPROVER_set_must");
92 
93  if(f_it==symbol_table.symbols.end())
94  return;
95 
97  {
98  if(it->is_assign())
99  {
100  const code_assignt &code_assign=
101  to_code_assign(it->code);
102 
103  if(code_assign.lhs().type()==lock_type)
104  {
106 
107  code_function_callt call;
108 
109  call.function()=f_it->second.symbol_expr();
110  call.arguments().resize(2);
111  call.arguments()[0]=address_of_exprt(code_assign.lhs());
112  call.arguments()[1]=address_of_exprt(string_constantt("mutex-init"));
113 
114  t->make_function_call(call);
115  t->source_location=it->source_location;
116  }
117  }
118  }
119 }
120 
122 {
123  // get pthread_mutex_lock
124 
125  symbol_tablet::symbolst::const_iterator f_it=
126  goto_model.symbol_table.symbols.find("pthread_mutex_lock");
127 
128  if(f_it==goto_model.symbol_table.symbols.end())
129  return;
130 
131  // get type of lock argument
132  code_typet code_type=to_code_type(to_code_type(f_it->second.type));
133  if(code_type.parameters().size()!=1)
134  return;
135 
136  typet lock_type=code_type.parameters()[0].type();
137 
138  if(lock_type.id()!=ID_pointer)
139  return;
140 
141  Forall_goto_functions(f_it, goto_model.goto_functions)
143  goto_model.symbol_table, f_it->second.body, lock_type.subtype());
144 }
The type of an expression.
Definition: type.h:22
Boolean negation.
Definition: std_expr.h:3228
Base type of functions.
Definition: std_types.h:764
void insert_before_swap(targett target)
Insertion that preserves jumps to "target".
Definition: goto_program.h:441
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:243
void mutex_init_instrumentation(const symbol_tablet &symbol_table, goto_programt &goto_program, typet lock_type)
exprt & op0()
Definition: expr.h:72
const code_typet & to_code_type(const typet &type)
Cast a generic typet to a code_typet.
Definition: std_types.h:993
The null pointer constant.
Definition: std_expr.h:4518
function_mapt function_map
typet & type()
Definition: expr.h:56
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:29
static bool has_start_thread(const goto_programt &goto_program)
const code_assignt & to_code_assign(const codet &code)
Definition: std_code.h:240
const irep_idt & id() const
Definition: irep.h:259
exprt & lhs()
Definition: std_code.h:209
Symbol Table + CFG.
argumentst & arguments()
Definition: std_code.h:890
A generic base class for binary expressions.
Definition: std_expr.h:649
instructionst::iterator targett
Definition: goto_program.h:397
The empty type.
Definition: std_types.h:54
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:403
exprt & op1()
Definition: expr.h:75
The symbol table.
Definition: symbol_table.h:19
targett insert_after(const_targett target)
Insertion after the given target.
Definition: goto_program.h:480
A function call.
Definition: std_code.h:858
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast a generic exprt to a symbol_exprt.
Definition: std_expr.h:210
dstringt has one field, an unsigned integer no which is an index into a static table of strings...
Definition: dstring.h:33
Operator to return the address of an object.
Definition: std_expr.h:3168
const symbolst & symbols
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:70
exprt & function()
Definition: std_code.h:878
Base class for all expressions.
Definition: expr.h:42
const parameterst & parameters() const
Definition: std_types.h:905
#define Forall_goto_functions(it, functions)
goto_programt & goto_program
Definition: cover.cpp:63
#define Forall_goto_program_instructions(it, program)
Definition: goto_program.h:740
const typet & subtype() const
Definition: type.h:33
#define forall_goto_functions(it, functions)
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:32
Assignment.
Definition: std_code.h:196
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:909
void thread_exit_instrumentation(goto_programt &goto_program)