cprover
Loading...
Searching...
No Matches
symex_start_thread.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Symbolic Execution
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
11
12#include "goto_symex.h"
13
16
17#include "expr_skeleton.h"
18#include "path_storage.h"
19#include "symex_assign.h"
20
22{
23 if(!state.reachable)
24 return;
25
26 if(state.atomic_section_id != 0)
28 "spawning threads out of atomic sections is not allowed; "
29 "this would require amendments to ordering constraints",
30 state.source.pc->source_location());
31
32 // record this
33 target.spawn(state.guard.as_expr(), state.source);
34
35 const goto_programt::instructiont &instruction=*state.source.pc;
36
37 INVARIANT(instruction.targets.size() == 1, "start_thread expects one target");
38
39 goto_programt::const_targett thread_target=
40 instruction.get_target();
41
42 // put into thread vector
43 std::size_t t=state.threads.size();
44 state.threads.push_back(statet::threadt(guard_manager));
45 // statet::threadt &cur_thread=state.threads[state.source.thread_nr];
46 statet::threadt &new_thread=state.threads.back();
47 new_thread.pc=thread_target;
48 new_thread.guard=state.guard;
49 new_thread.call_stack.push_back(state.call_stack().top());
50 new_thread.call_stack.back().local_objects.clear();
51 new_thread.call_stack.back().goto_state_map.clear();
52 #if 0
53 new_thread.abstract_events=&(target.new_thread(cur_thread.abstract_events));
54 #endif
55
56 // create a copy of the local variables for the new thread
57 framet &frame = state.call_stack().top();
58
60 state.get_level2().current_names.get_view(view);
61
62 for(const auto &pair : view)
63 {
64 const irep_idt l1_o_id = pair.second.first.get_l1_object_identifier();
65
66 // could use iteration over local_objects as l1_o_id is prefix
67 if(frame.local_objects.find(l1_o_id)==frame.local_objects.end())
68 continue;
69
70 // get original name
71 ssa_exprt lhs(pair.second.first.get_original_expr());
72
73 // get L0 name for current thread
74 const renamedt<ssa_exprt, L0> l0_lhs = symex_level0(std::move(lhs), ns, t);
75 const irep_idt &l0_name = l0_lhs.get().get_identifier();
76 std::size_t l1_index = path_storage.get_unique_l1_index(l0_name, 0);
77 CHECK_RETURN(l1_index == 0);
78
79 // set up L1 name
80 state.level1.insert(l0_lhs, 0);
81
82 const ssa_exprt lhs_l1 = state.rename_ssa<L1>(l0_lhs.get(), ns).get();
83 const irep_idt l1_name = lhs_l1.get_l1_object_identifier();
84 // store it
85 new_thread.call_stack.back().local_objects.insert(l1_name);
86
87 // make copy
88 ssa_exprt rhs = pair.second.first;
89
90 exprt::operandst lhs_conditions;
91 state.record_events.push(false);
94 .assign_symbol(lhs_l1, expr_skeletont{}, rhs, lhs_conditions);
95 state.record_events.pop();
96 }
97
98 // initialize all variables marked thread-local
99 const symbol_tablet &symbol_table=ns.get_symbol_table();
100
101 for(const auto &symbol_pair : symbol_table.symbols)
102 {
103 const symbolt &symbol = symbol_pair.second;
104
105 if(!symbol.is_thread_local ||
106 !symbol.is_static_lifetime ||
107 (symbol.is_extern && symbol.value.is_nil()))
108 continue;
109
110 // get original name
111 ssa_exprt lhs(symbol.symbol_expr());
112
113 // get L0 name for current thread
114 lhs.set_level_0(t);
115
116 exprt rhs=symbol.value;
117 if(rhs.is_nil())
118 {
119 const auto zero = zero_initializer(symbol.type, symbol.location, ns);
120 CHECK_RETURN(zero.has_value());
121 rhs = *zero;
122 }
123
124 exprt::operandst lhs_conditions;
127 .assign_symbol(lhs, expr_skeletont{}, rhs, lhs_conditions);
128 }
129}
framet & top()
Definition: call_stack.h:17
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
Expression in which some part is missing and can be substituted for another expression.
Definition: expr_skeleton.h:26
Base class for all expressions.
Definition: expr.h:54
std::vector< exprt > operandst
Definition: expr.h:56
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:180
targetst targets
The list of successor instructions.
Definition: goto_program.h:403
targett get_target() const
Returns the first (and only) successor for the usual case of a single target.
Definition: goto_program.h:407
instructionst::const_iterator const_targett
Definition: goto_program.h:593
guardt guard
Definition: goto_state.h:58
bool reachable
Is this code reachable? If not we can take shortcuts such as not entering function calls,...
Definition: goto_state.h:62
unsigned atomic_section_id
Threads.
Definition: goto_state.h:76
const symex_level2t & get_level2() const
Definition: goto_state.h:45
Central data structure: state.
call_stackt & call_stack()
std::stack< bool > record_events
symex_level1t level1
symex_targett::sourcet source
std::vector< threadt > threads
NODISCARD renamedt< ssa_exprt, level > rename_ssa(ssa_exprt ssa, const namespacet &ns)
Version of rename which is specialized for SSA exprt.
path_storaget & path_storage
Symbolic execution paths to be resumed later.
Definition: goto_symex.h:788
symex_target_equationt & target
The equation that this execution is building up.
Definition: goto_symex.h:251
guard_managert & guard_manager
Used to create guards.
Definition: goto_symex.h:248
virtual void symex_start_thread(statet &state)
Symbolically execute a START_THREAD instruction.
namespacet ns
Initialized just before symbolic execution begins, to point to both outer_symbol_table and the symbol...
Definition: goto_symex.h:243
const symex_configt symex_config
The configuration to use for this symbolic execution.
Definition: goto_symex.h:170
exprt as_expr() const
Definition: guard_expr.h:46
Thrown when a goto program that's being processed is in an invalid format, for example passing the wr...
bool is_nil() const
Definition: irep.h:376
const symbol_table_baset & get_symbol_table() const
Return first symbol table registered with the namespace.
Definition: namespace.h:123
std::size_t get_unique_l1_index(const irep_idt &id, std::size_t minimum_index)
Provide a unique L1 index for a given id, starting from minimum_index.
Definition: path_storage.h:104
Wrapper for expressions or types which have been renamed up to a given level.
Definition: renamed.h:33
const underlyingt & get() const
Definition: renamed.h:40
std::vector< view_itemt > viewt
View of the key-value pairs in the map.
Definition: sharing_map.h:388
void get_view(V &view) const
Get a view of the elements in the map A view is a list of pairs with the components being const refer...
Expression providing an SSA-renamed symbol of expressions.
Definition: ssa_expr.h:17
const irep_idt get_l1_object_identifier() const
Definition: ssa_expr.cpp:169
void set_level_0(std::size_t i)
Definition: ssa_expr.cpp:180
const symbolst & symbols
Read-only field, used to look up symbols given their names.
The symbol table.
Definition: symbol_table.h:14
Symbol table entry.
Definition: symbol.h:28
bool is_extern
Definition: symbol.h:66
bool is_static_lifetime
Definition: symbol.h:65
bool is_thread_local
Definition: symbol.h:65
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:121
typet type
Type of symbol.
Definition: symbol.h:31
exprt value
Initial value of symbol.
Definition: symbol.h:34
Functor for symex assignment.
Definition: symex_assign.h:26
virtual void spawn(const exprt &guard, const sourcet &source)
Record spawning a new thread.
optionalt< exprt > zero_initializer(const typet &type, const source_locationt &source_location, const namespacet &ns)
Create the equivalent of zero for type type.
Expression Initialization.
Expression skeleton.
Symbolic Execution.
Storage of symbolic execution paths to resume.
@ L1
Definition: renamed.h:24
renamedt< ssa_exprt, L0 > symex_level0(ssa_exprt ssa_expr, const namespacet &ns, std::size_t thread_nr)
Set the level 0 renaming of SSA expressions.
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:423
Stack frames – these are used for function calls and for exceptions.
Definition: frame.h:22
std::set< irep_idt > local_objects
Definition: frame.h:40
goto_programt::const_targett pc
void insert(const renamedt< ssa_exprt, L0 > &ssa, std::size_t index)
Assume ssa is not already known.
symex_renaming_levelt current_names
goto_programt::const_targett pc
Definition: symex_target.h:42
Symbolic Execution of assignments.