-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathcall_stack_history.cpp
233 lines (206 loc) · 6.63 KB
/
call_stack_history.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*******************************************************************\
Module: Abstract Interpretation
Author: Martin Brain, [email protected]
\*******************************************************************/
/// \file
/// History for tracking the call stack and performing interprocedural analysis
#include "call_stack_history.h"
ai_history_baset::step_returnt call_stack_historyt::step(
locationt to,
const trace_sett &others,
trace_ptrt caller_hist) const
{
DATA_INVARIANT(current_stack != nullptr, "current_stack must exist");
cse_ptrt next_stack = nullptr;
// First construct what the new history would be.
// This requires special handling if this edge is a function call or return
if(current_stack->current_location->is_function_call())
{
locationt l_return = std::next(current_stack->current_location);
if(l_return->location_number == to->location_number)
{
// Is skipping the function call, only update the location
next_stack = cse_ptrt(
std::make_shared<call_stack_entryt>(l_return, current_stack->caller));
}
else
{
// An interprocedural (call) edge; add to the current stack
// If the recursion limit has been reached
// shorten the stack / merge with the most recent invocation
// before we extend
cse_ptrt shorten = current_stack;
if(has_recursion_limit())
{
unsigned int number_of_recursive_calls = 0;
cse_ptrt first_found = nullptr; // The most recent recursive call
// Iterate back through the call stack
for(cse_ptrt i = current_stack->caller; i != nullptr; i = i->caller)
{
if(
i->current_location->location_number ==
current_stack->current_location->location_number)
{
// Found a recursive instance
if(first_found == nullptr)
{
first_found = i;
}
++number_of_recursive_calls;
if(number_of_recursive_calls == recursion_limit)
{
shorten = first_found;
break;
}
}
}
}
next_stack = cse_ptrt(std::make_shared<call_stack_entryt>(to, shorten));
}
}
else if(current_stack->current_location->is_end_function())
{
if(current_stack->caller == nullptr)
{
// Trying to return but there was no caller?
// Refuse to do the transition outright
return std::make_pair(ai_history_baset::step_statust::BLOCKED, nullptr);
}
else
{
INVARIANT(
caller_hist != ai_history_baset::no_caller_history,
"return from function should have a caller");
// The expected call return site...
locationt l_caller_return =
std::next(current_stack->caller->current_location);
if(l_caller_return->location_number == to->location_number)
{
INVARIANT(
std::next(caller_hist->current_location())->location_number ==
l_caller_return->location_number,
"caller and caller_hist should be consistent");
// It is tempting to think that...
// INVARIANT(*(current_stack->caller) ==
// *(std::dynamic_pointer_cast<const call_stack_historyt>
// (caller_hist)->current_stack),
// "call stacks should match");
// and that we could just use caller_hist->current_stack here.
// This fails when you hit the recursion limit so that the
// caller_hist has a deep stack but *this has a shallow one.
// ... which is where we are going
next_stack = cse_ptrt(std::make_shared<call_stack_entryt>(
to, current_stack->caller->caller));
}
else
{
// Not possible to return to somewhere other than the call site
return std::make_pair(ai_history_baset::step_statust::BLOCKED, nullptr);
}
}
}
else
{
// Just update the location
next_stack =
cse_ptrt(std::make_shared<call_stack_entryt>(to, current_stack->caller));
}
INVARIANT(next_stack != nullptr, "All branches should initialise next_stack");
// Create the potential next history
trace_ptrt next(new call_stack_historyt(next_stack, recursion_limit));
// It would be nice to use make_shared here but ... that doesn't work with
// protected constructors
// If there is already an equivalent history, merge with that instead
auto it = others.find(next);
if(it == others.end())
{
return std::make_pair(ai_history_baset::step_statust::NEW, next);
}
else
{
return std::make_pair(ai_history_baset::step_statust::MERGED, *it);
}
}
bool call_stack_historyt::operator<(const ai_history_baset &op) const
{
auto other = dynamic_cast<const call_stack_historyt *>(&op);
PRECONDITION(other != nullptr);
return *(this->current_stack) < *(other->current_stack);
}
bool call_stack_historyt::operator==(const ai_history_baset &op) const
{
auto other = dynamic_cast<const call_stack_historyt *>(&op);
PRECONDITION(other != nullptr);
return *(this->current_stack) == *(other->current_stack);
}
void call_stack_historyt::output(std::ostream &out) const
{
out << "call_stack_history : stack "
<< current_stack->current_location->location_number;
cse_ptrt working = current_stack->caller;
while(working != nullptr)
{
out << " from " << working->current_location->location_number;
working = working->caller;
}
return;
}
bool call_stack_historyt::call_stack_entryt::
operator<(const call_stack_entryt &op) const
{
if(
this->current_location->location_number <
op.current_location->location_number)
{
return true;
}
else if(
this->current_location->location_number >
op.current_location->location_number)
{
return false;
}
else
{
INVARIANT(
this->current_location->location_number ==
op.current_location->location_number,
"Implied by if-then-else");
if(this->caller == op.caller)
{
return false; // Because they are equal
}
else if(this->caller == nullptr)
{
return true; // Shorter stacks are 'lower'
}
else if(op.caller == nullptr)
{
return false;
}
else
{
// Sort by the rest of the stack
return *(this->caller) < *(op.caller);
}
}
UNREACHABLE;
}
bool call_stack_historyt::call_stack_entryt::
operator==(const call_stack_entryt &op) const
{
if(
this->current_location->location_number ==
op.current_location->location_number)
{
if(this->caller == op.caller)
{
return true;
}
else if(this->caller != nullptr && op.caller != nullptr)
{
return *(this->caller) == *(op.caller);
}
}
return false;
}