cprover
symex_start_thread.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Symbolic Execution
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include "goto_symex.h"
13 
14 #include <util/expr_initializer.h>
15 
17 {
18  if(state.guard.is_false())
19  return;
20 
21  // we don't allow spawning threads out of atomic sections
22  // this would require amendments to ordering constraints
23  if(state.atomic_section_id!=0)
24  throw "start_thread in atomic section detected";
25 
26  // record this
27  target.spawn(state.guard.as_expr(), state.source);
28 
29  const goto_programt::instructiont &instruction=*state.source.pc;
30 
31  if(instruction.targets.size()!=1)
32  throw "start_thread expects one target";
33 
34  goto_programt::const_targett thread_target=
35  instruction.get_target();
36 
37  // put into thread vector
38  std::size_t t=state.threads.size();
39  state.threads.push_back(statet::threadt());
40  // statet::threadt &cur_thread=state.threads[state.source.thread_nr];
41  statet::threadt &new_thread=state.threads.back();
42  new_thread.pc=thread_target;
43  new_thread.guard=state.guard;
44  new_thread.call_stack.push_back(state.top());
45  new_thread.call_stack.back().local_objects.clear();
46  new_thread.call_stack.back().goto_state_map.clear();
47  #if 0
48  new_thread.abstract_events=&(target.new_thread(cur_thread.abstract_events));
49  #endif
50 
51  // create a copy of the local variables for the new thread
52  statet::framet &frame=state.top();
53 
54  for(goto_symex_statet::renaming_levelt::current_namest::const_iterator
55  c_it=state.level2.current_names.begin();
56  c_it!=state.level2.current_names.end();
57  ++c_it)
58  {
59  const irep_idt l1_o_id=c_it->second.first.get_l1_object_identifier();
60  // could use iteration over local_objects as l1_o_id is prefix
61  if(frame.local_objects.find(l1_o_id)==frame.local_objects.end())
62  continue;
63 
64  // get original name
65  ssa_exprt lhs(c_it->second.first.get_original_expr());
66 
67  // get L0 name for current thread
68  lhs.set_level_0(t);
69 
70  // set up L1 name
71  if(!state.level1.current_names.insert(
72  std::make_pair(lhs.get_l1_object_identifier(),
73  std::make_pair(lhs, 0))).second)
75  state.rename(lhs, ns, goto_symex_statet::L1);
76  const irep_idt l1_name=lhs.get_l1_object_identifier();
77  // store it
78  state.l1_history.insert(l1_name);
79  new_thread.call_stack.back().local_objects.insert(l1_name);
80 
81  // make copy
82  ssa_exprt rhs=c_it->second.first;
83 
84  guardt guard;
85  const bool record_events=state.record_events;
86  state.record_events=false;
88  state,
89  lhs,
90  nil_exprt(),
91  rhs,
92  guard,
94  state.record_events=record_events;
95  }
96 
97  // initialize all variables marked thread-local
98  const symbol_tablet &symbol_table=ns.get_symbol_table();
99 
100  for(const auto &symbol_pair : symbol_table.symbols)
101  {
102  const symbolt &symbol = symbol_pair.second;
103 
104  if(!symbol.is_thread_local ||
105  !symbol.is_static_lifetime ||
106  (symbol.is_extern && symbol.value.is_nil()))
107  continue;
108 
109  // get original name
110  ssa_exprt lhs(symbol.symbol_expr());
111 
112  // get L0 name for current thread
113  lhs.set_level_0(t);
114 
115  exprt rhs=symbol.value;
116  if(rhs.is_nil())
117  rhs=zero_initializer(symbol.type, symbol.location, ns);
118 
119  guardt guard;
121  state,
122  lhs,
123  nil_exprt(),
124  rhs,
125  guard,
127  }
128 }
exprt as_expr() const
Definition: guard.h:43
virtual void spawn(const exprt &guard, const sourcet &source)
spawn a new thread
bool is_nil() const
Definition: irep.h:102
bool is_thread_local
Definition: symbol.h:67
goto_programt::const_targett pc
Definition: symex_target.h:32
const symbol_tablet & get_symbol_table() const
Definition: namespace.h:106
void rename(exprt &expr, const namespacet &ns, levelt level=L2)
bool is_false() const
Definition: expr.cpp:131
exprt value
Initial value of symbol.
Definition: symbol.h:37
Definition: guard.h:19
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
Definition: symbol.h:30
targett get_target() const
Returns the first (and only) successor for the usual case of a single target.
Definition: goto_program.h:202
bool is_static_lifetime
Definition: symbol.h:67
targetst targets
The list of successor instructions.
Definition: goto_program.h:198
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:173
goto_symex_statet::level2t level2
namespacet ns
Initialized just before symbolic execution begins, to point to both outer_symbol_table and the symbol...
Definition: goto_symex.h:237
Expression Initialization.
void set_level_0(unsigned i)
Definition: ssa_expr.h:83
class symbol_exprt symbol_expr() const
produces a symbol_exprt for a symbol
Definition: symbol.cpp:111
The NIL expression.
Definition: std_expr.h:4510
Symbolic Execution.
symex_target_equationt & target
Definition: goto_symex.h:238
The symbol table.
Definition: symbol_table.h:19
instructionst::const_iterator const_targett
Definition: goto_program.h:398
goto_symex_statet::level1t level1
const symbolst & symbols
exprt zero_initializer(const typet &type, const source_locationt &source_location, const namespacet &ns, message_handlert &message_handler)
bool is_extern
Definition: symbol.h:68
typet type
Type of symbol.
Definition: symbol.h:34
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:40
Base class for all expressions.
Definition: expr.h:42
l1_historyt l1_history
#define UNREACHABLE
Definition: invariant.h:250
virtual void symex_start_thread(statet &)
goto_programt::const_targett pc
Expression providing an SSA-renamed symbol of expressions.
Definition: ssa_expr.h:17
symex_targett::sourcet source
void symex_assign_symbol(statet &, const ssa_exprt &lhs, const exprt &full_lhs, const exprt &rhs, guardt &, assignment_typet)