forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionCallGraph.cpp
351 lines (287 loc) · 12.9 KB
/
FunctionCallGraph.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/analysis/FunctionCallGraph.h>
#include <libsolutil/StringUtils.h>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/reverse.hpp>
#include <range/v3/view/transform.hpp>
using namespace solidity::frontend;
using namespace solidity::util;
CallGraph FunctionCallGraphBuilder::buildCreationGraph(ContractDefinition const& _contract)
{
FunctionCallGraphBuilder builder(_contract);
solAssert(builder.m_currentNode == CallGraph::Node(CallGraph::SpecialNode::Entry), "");
// Create graph for constructor, state vars, etc
for (ContractDefinition const* base: _contract.annotation().linearizedBaseContracts | ranges::views::reverse)
{
// The constructor and functions called in state variable initial assignments should have
// an edge from Entry
builder.m_currentNode = CallGraph::SpecialNode::Entry;
for (auto const* stateVar: base->stateVariables())
if (!stateVar->isConstant())
stateVar->accept(builder);
if (base->constructor())
{
builder.functionReferenced(*base->constructor());
// Constructors and functions called in state variable initializers have an edge either from
// the previous class in linearized order or from Entry if there's no class before.
builder.m_currentNode = base->constructor();
}
// Functions called from the inheritance specifier should have an edge from the constructor
// for consistency with functions called from constructor modifiers.
for (auto const& inheritanceSpecifier: base->baseContracts())
inheritanceSpecifier->accept(builder);
}
builder.m_currentNode = CallGraph::SpecialNode::Entry;
builder.processQueue();
return std::move(builder.m_graph);
}
CallGraph FunctionCallGraphBuilder::buildDeployedGraph(
ContractDefinition const& _contract,
CallGraph const& _creationGraph
)
{
FunctionCallGraphBuilder builder(_contract);
solAssert(builder.m_currentNode == CallGraph::Node(CallGraph::SpecialNode::Entry), "");
auto getSecondElement = [](auto const& _tuple){ return std::get<1>(_tuple); };
// Create graph for all publicly reachable functions
for (FunctionTypePointer functionType: _contract.interfaceFunctionList() | ranges::views::transform(getSecondElement))
{
auto const* function = dynamic_cast<FunctionDefinition const*>(&functionType->declaration());
auto const* variable = dynamic_cast<VariableDeclaration const*>(&functionType->declaration());
if (function)
builder.functionReferenced(*function);
else
// If it's not a function, it must be a getter of a public variable; we ignore those
solAssert(variable, "");
}
if (_contract.fallbackFunction())
builder.functionReferenced(*_contract.fallbackFunction());
if (_contract.receiveFunction())
builder.functionReferenced(*_contract.receiveFunction());
// All functions present in internal dispatch at creation time could potentially be pointers
// assigned to state variables and as such may be reachable after deployment as well.
builder.m_currentNode = CallGraph::SpecialNode::InternalDispatch;
std::set<CallGraph::Node, CallGraph::CompareByID> defaultNode;
for (CallGraph::Node const& dispatchTarget: util::valueOrDefault(_creationGraph.edges, CallGraph::SpecialNode::InternalDispatch, defaultNode))
{
solAssert(!std::holds_alternative<CallGraph::SpecialNode>(dispatchTarget), "");
solAssert(std::get<CallableDeclaration const*>(dispatchTarget) != nullptr, "");
// Visit the callable to add not only it but also everything it calls too
builder.functionReferenced(*std::get<CallableDeclaration const*>(dispatchTarget), false);
}
builder.m_currentNode = CallGraph::SpecialNode::Entry;
builder.processQueue();
return std::move(builder.m_graph);
}
bool FunctionCallGraphBuilder::visit(FunctionCall const& _functionCall)
{
if (*_functionCall.annotation().kind != FunctionCallKind::FunctionCall)
return true;
auto const* functionType = dynamic_cast<FunctionType const*>(_functionCall.expression().annotation().type);
solAssert(functionType, "");
if (functionType->kind() == FunctionType::Kind::Internal && !_functionCall.expression().annotation().calledDirectly)
// If it's not a direct call, we don't really know which function will be called (it may even
// change at runtime). All we can do is to add an edge to the dispatch which in turn has
// edges to all functions could possibly be called.
add(m_currentNode, CallGraph::SpecialNode::InternalDispatch);
else if (functionType->kind() == FunctionType::Kind::Error)
m_graph.usedErrors.insert(&dynamic_cast<ErrorDefinition const&>(functionType->declaration()));
return true;
}
bool FunctionCallGraphBuilder::visit(EmitStatement const& _emitStatement)
{
auto const* functionType = dynamic_cast<FunctionType const*>(_emitStatement.eventCall().expression().annotation().type);
solAssert(functionType, "");
m_graph.emittedEvents.insert(&dynamic_cast<EventDefinition const&>(functionType->declaration()));
return true;
}
bool FunctionCallGraphBuilder::visit(Identifier const& _identifier)
{
if (auto const* variable = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration))
{
if (variable->isConstant())
{
solAssert(variable->isStateVariable() || variable->isFileLevelVariable(), "");
variable->accept(*this);
}
}
else if (auto const* callable = dynamic_cast<CallableDeclaration const*>(_identifier.annotation().referencedDeclaration))
{
solAssert(*_identifier.annotation().requiredLookup == VirtualLookup::Virtual, "");
auto funType = dynamic_cast<FunctionType const*>(_identifier.annotation().type);
// For events kind() == Event, so we have an extra check here
if (funType && funType->kind() == FunctionType::Kind::Internal)
functionReferenced(callable->resolveVirtual(m_contract), _identifier.annotation().calledDirectly);
}
return true;
}
bool FunctionCallGraphBuilder::visit(MemberAccess const& _memberAccess)
{
Type const* exprType = _memberAccess.expression().annotation().type;
ASTString const& memberName = _memberAccess.memberName();
if (auto magicType = dynamic_cast<MagicType const*>(exprType))
if (magicType->kind() == MagicType::Kind::MetaType && (
memberName == "creationCode" || memberName == "runtimeCode"
))
{
ContractType const& accessedContractType = dynamic_cast<ContractType const&>(*magicType->typeArgument());
m_graph.bytecodeDependency.emplace(&accessedContractType.contractDefinition(), &_memberAccess);
}
auto functionType = dynamic_cast<FunctionType const*>(_memberAccess.annotation().type);
auto functionDef = dynamic_cast<FunctionDefinition const*>(_memberAccess.annotation().referencedDeclaration);
if (!functionType || !functionDef || functionType->kind() != FunctionType::Kind::Internal)
return true;
// Super functions
if (*_memberAccess.annotation().requiredLookup == VirtualLookup::Super)
{
if (auto const* typeType = dynamic_cast<TypeType const*>(exprType))
if (auto const contractType = dynamic_cast<ContractType const*>(typeType->actualType()))
{
solAssert(contractType->isSuper(), "");
functionDef = &functionDef->resolveVirtual(
m_contract,
contractType->contractDefinition().superContract(m_contract)
);
}
}
else
solAssert(*_memberAccess.annotation().requiredLookup == VirtualLookup::Static, "");
functionReferenced(*functionDef, _memberAccess.annotation().calledDirectly);
return true;
}
bool FunctionCallGraphBuilder::visit(BinaryOperation const& _binaryOperation)
{
if (*_binaryOperation.annotation().userDefinedFunction != nullptr)
functionReferenced(**_binaryOperation.annotation().userDefinedFunction, true /* called directly */);
return true;
}
bool FunctionCallGraphBuilder::visit(UnaryOperation const& _unaryOperation)
{
if (*_unaryOperation.annotation().userDefinedFunction != nullptr)
functionReferenced(**_unaryOperation.annotation().userDefinedFunction, true /* called directly */);
return true;
}
bool FunctionCallGraphBuilder::visit(ModifierInvocation const& _modifierInvocation)
{
if (auto const* modifier = dynamic_cast<ModifierDefinition const*>(_modifierInvocation.name().annotation().referencedDeclaration))
{
VirtualLookup const& requiredLookup = *_modifierInvocation.name().annotation().requiredLookup;
if (requiredLookup == VirtualLookup::Virtual)
functionReferenced(modifier->resolveVirtual(m_contract));
else
{
solAssert(requiredLookup == VirtualLookup::Static, "");
functionReferenced(*modifier);
}
}
return true;
}
bool FunctionCallGraphBuilder::visit(NewExpression const& _newExpression)
{
if (ContractType const* contractType = dynamic_cast<ContractType const*>(_newExpression.typeName().annotation().type))
m_graph.bytecodeDependency.emplace(&contractType->contractDefinition(), &_newExpression);
return true;
}
void FunctionCallGraphBuilder::enqueueCallable(CallableDeclaration const& _callable)
{
if (!m_graph.edges.count(&_callable))
{
m_visitQueue.push_back(&_callable);
// Insert the callable to the graph (with no edges coming out of it) to mark it as visited.
m_graph.edges.insert({CallGraph::Node(&_callable), {}});
}
}
void FunctionCallGraphBuilder::processQueue()
{
solAssert(m_currentNode == CallGraph::Node(CallGraph::SpecialNode::Entry), "Visit queue is already being processed.");
while (!m_visitQueue.empty())
{
m_currentNode = m_visitQueue.front();
solAssert(std::holds_alternative<CallableDeclaration const*>(m_currentNode), "");
m_visitQueue.pop_front();
std::get<CallableDeclaration const*>(m_currentNode)->accept(*this);
}
m_currentNode = CallGraph::SpecialNode::Entry;
}
void FunctionCallGraphBuilder::add(CallGraph::Node _caller, CallGraph::Node _callee)
{
m_graph.edges[_caller].insert(_callee);
}
void FunctionCallGraphBuilder::functionReferenced(CallableDeclaration const& _callable, bool _calledDirectly)
{
if (_calledDirectly)
{
solAssert(
std::holds_alternative<CallGraph::SpecialNode>(m_currentNode) || m_graph.edges.count(m_currentNode) > 0,
"Adding an edge from a node that has not been visited yet."
);
add(m_currentNode, &_callable);
}
else
add(CallGraph::SpecialNode::InternalDispatch, &_callable);
enqueueCallable(_callable);
}
std::ostream& solidity::frontend::operator<<(std::ostream& _out, CallGraph::Node const& _node)
{
if (std::holds_alternative<CallGraph::SpecialNode>(_node))
switch (std::get<CallGraph::SpecialNode>(_node))
{
case CallGraph::SpecialNode::InternalDispatch:
_out << "InternalDispatch";
break;
case CallGraph::SpecialNode::Entry:
_out << "Entry";
break;
default:
solAssert(false, "Invalid SpecialNode type");
}
else
{
solAssert(std::holds_alternative<CallableDeclaration const*>(_node), "");
auto const* callableDeclaration = std::get<CallableDeclaration const*>(_node);
solAssert(callableDeclaration, "");
auto const* function = dynamic_cast<FunctionDefinition const *>(callableDeclaration);
auto const* event = dynamic_cast<EventDefinition const *>(callableDeclaration);
auto const* modifier = dynamic_cast<ModifierDefinition const *>(callableDeclaration);
auto typeToString = [](auto const& _var) -> std::string { return _var->type()->toString(true); };
std::vector<std::string> parameters = callableDeclaration->parameters() | ranges::views::transform(typeToString) | ranges::to<std::vector<std::string>>();
std::string scopeName;
if (!function || !function->isFree())
{
solAssert(callableDeclaration->annotation().scope, "");
auto const* parentContract = dynamic_cast<ContractDefinition const*>(callableDeclaration->annotation().scope);
solAssert(parentContract, "");
scopeName = parentContract->name();
}
if (function && function->isFree())
_out << "function " << function->name() << "(" << joinHumanReadable(parameters, ",") << ")";
else if (function && function->isConstructor())
_out << "constructor of " << scopeName;
else if (function && function->isFallback())
_out << "fallback of " << scopeName;
else if (function && function->isReceive())
_out << "receive of " << scopeName;
else if (function)
_out << "function " << scopeName << "." << function->name() << "(" << joinHumanReadable(parameters, ",") << ")";
else if (event)
_out << "event " << scopeName << "." << event->name() << "(" << joinHumanReadable(parameters, ",") << ")";
else if (modifier)
_out << "modifier " << scopeName << "." << modifier->name();
else
solAssert(false, "Unexpected AST node type in function call graph");
}
return _out;
}