2020-07-02 10:48:20 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libyul/optimiser/StackLimitEvader.h>
|
|
|
|
#include <libyul/optimiser/CallGraphGenerator.h>
|
|
|
|
#include <libyul/optimiser/FunctionCallFinder.h>
|
|
|
|
#include <libyul/optimiser/NameDispenser.h>
|
2021-11-04 14:40:57 +00:00
|
|
|
#include <libyul/optimiser/NameCollector.h>
|
2020-07-02 10:48:20 +00:00
|
|
|
#include <libyul/optimiser/StackToMemoryMover.h>
|
2021-08-12 15:17:21 +00:00
|
|
|
#include <libyul/backends/evm/ControlFlowGraphBuilder.h>
|
2020-07-02 10:48:20 +00:00
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
2021-08-12 15:17:21 +00:00
|
|
|
#include <libyul/AsmAnalysis.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2021-08-12 15:17:21 +00:00
|
|
|
#include <libyul/CompilabilityChecker.h>
|
2020-07-02 10:48:20 +00:00
|
|
|
#include <libyul/Exceptions.h>
|
|
|
|
#include <libyul/Object.h>
|
|
|
|
#include <libyul/Utilities.h>
|
|
|
|
#include <libsolutil/Algorithms.h>
|
|
|
|
#include <libsolutil/CommonData.h>
|
|
|
|
|
2021-08-12 15:17:21 +00:00
|
|
|
#include <range/v3/range/conversion.hpp>
|
2020-10-13 01:18:21 +00:00
|
|
|
#include <range/v3/view/concat.hpp>
|
|
|
|
#include <range/v3/view/take.hpp>
|
|
|
|
|
2020-07-02 10:48:20 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2020-09-17 15:25:37 +00:00
|
|
|
/**
|
|
|
|
* Walks the call graph using a Depth-First-Search assigning memory slots to variables.
|
|
|
|
* - The leaves of the call graph will get the lowest slot, increasing towards the root.
|
|
|
|
* - ``slotsRequiredForFunction`` maps a function to the number of slots it requires (which is also the
|
|
|
|
* next available slot that can be used by another function that calls this function).
|
|
|
|
* - For each function starting from the root of the call graph:
|
|
|
|
* - Visit all children that are not already visited.
|
|
|
|
* - Determine the maximum value ``n`` of the values of ``slotsRequiredForFunction`` among the children.
|
|
|
|
* - If the function itself contains variables that need memory slots, but is contained in a cycle,
|
|
|
|
* abort the process as failure.
|
|
|
|
* - If not, assign each variable its slot starting from ``n`` (incrementing it).
|
|
|
|
* - Assign ``n`` to ``slotsRequiredForFunction`` of the function.
|
|
|
|
*/
|
2020-07-02 10:48:20 +00:00
|
|
|
struct MemoryOffsetAllocator
|
|
|
|
{
|
|
|
|
uint64_t run(YulString _function = YulString{})
|
|
|
|
{
|
2020-09-17 15:25:37 +00:00
|
|
|
if (slotsRequiredForFunction.count(_function))
|
|
|
|
return slotsRequiredForFunction[_function];
|
2020-07-02 10:48:20 +00:00
|
|
|
|
|
|
|
// Assign to zero early to guard against recursive calls.
|
2020-09-17 15:25:37 +00:00
|
|
|
slotsRequiredForFunction[_function] = 0;
|
2020-07-02 10:48:20 +00:00
|
|
|
|
2020-09-17 15:25:37 +00:00
|
|
|
uint64_t requiredSlots = 0;
|
2020-07-02 10:48:20 +00:00
|
|
|
if (callGraph.count(_function))
|
|
|
|
for (YulString child: callGraph.at(_function))
|
2020-09-17 15:25:37 +00:00
|
|
|
requiredSlots = std::max(run(child), requiredSlots);
|
2020-07-02 10:48:20 +00:00
|
|
|
|
2020-10-13 01:18:21 +00:00
|
|
|
if (auto const* unreachables = util::valueOrNullptr(unreachableVariables, _function))
|
2020-07-02 10:48:20 +00:00
|
|
|
{
|
2020-10-13 01:18:21 +00:00
|
|
|
if (FunctionDefinition const* functionDefinition = util::valueOrDefault(functionDefinitions, _function, nullptr, util::allow_copy))
|
|
|
|
if (
|
|
|
|
size_t totalArgCount = functionDefinition->returnVariables.size() + functionDefinition->parameters.size();
|
|
|
|
totalArgCount > 16
|
|
|
|
)
|
|
|
|
for (TypedName const& var: ranges::concat_view(
|
|
|
|
functionDefinition->parameters,
|
|
|
|
functionDefinition->returnVariables
|
|
|
|
) | ranges::views::take(totalArgCount - 16))
|
|
|
|
slotAllocations[var.name] = requiredSlots++;
|
|
|
|
|
|
|
|
// Assign slots for all variables that become unreachable in the function body, if the above did not
|
|
|
|
// assign a slot for them already.
|
|
|
|
for (YulString variable: *unreachables)
|
|
|
|
// The empty case is a function with too many arguments or return values,
|
|
|
|
// which was already handled above.
|
|
|
|
if (!variable.empty() && !slotAllocations.count(variable))
|
2020-10-06 08:38:26 +00:00
|
|
|
slotAllocations[variable] = requiredSlots++;
|
2020-07-02 10:48:20 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 15:25:37 +00:00
|
|
|
return slotsRequiredForFunction[_function] = requiredSlots;
|
2020-07-02 10:48:20 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 01:18:21 +00:00
|
|
|
/// Maps function names to the set of unreachable variables in that function.
|
|
|
|
/// An empty variable name means that the function has too many arguments or return variables.
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, std::vector<YulString>> const& unreachableVariables;
|
2020-10-13 01:18:21 +00:00
|
|
|
/// The graph of immediate function calls of all functions.
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, std::vector<YulString>> const& callGraph;
|
2020-10-13 01:18:21 +00:00
|
|
|
/// Maps the name of each user-defined function to its definition.
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, FunctionDefinition const*> const& functionDefinitions;
|
2020-07-02 10:48:20 +00:00
|
|
|
|
2020-10-13 01:18:21 +00:00
|
|
|
/// Maps variable names to the memory slot the respective variable is assigned.
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, uint64_t> slotAllocations{};
|
2020-10-13 01:18:21 +00:00
|
|
|
/// Maps function names to the number of memory slots the respective function requires.
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, uint64_t> slotsRequiredForFunction{};
|
2020-07-02 10:48:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
u256 literalArgumentValue(FunctionCall const& _call)
|
|
|
|
{
|
|
|
|
yulAssert(_call.arguments.size() == 1, "");
|
|
|
|
Literal const* literal = std::get_if<Literal>(&_call.arguments.front());
|
|
|
|
yulAssert(literal && literal->kind == LiteralKind::Number, "");
|
|
|
|
return valueOfLiteral(*literal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:17:21 +00:00
|
|
|
void StackLimitEvader::run(
|
|
|
|
OptimiserStepContext& _context,
|
|
|
|
Object& _object
|
|
|
|
)
|
|
|
|
{
|
|
|
|
auto const* evmDialect = dynamic_cast<EVMDialect const*>(&_context.dialect);
|
|
|
|
yulAssert(
|
|
|
|
evmDialect && evmDialect->providesObjectAccess(),
|
|
|
|
"StackLimitEvader can only be run on objects using the EVMDialect with object access."
|
|
|
|
);
|
|
|
|
if (evmDialect && evmDialect->evmVersion().canOverchargeGasForCall())
|
|
|
|
{
|
|
|
|
yul::AsmAnalysisInfo analysisInfo = yul::AsmAnalyzer::analyzeStrictAssertCorrect(*evmDialect, _object);
|
2023-08-23 02:03:44 +00:00
|
|
|
std::unique_ptr<CFG> cfg = ControlFlowGraphBuilder::build(analysisInfo, *evmDialect, *_object.code);
|
2021-08-12 15:17:21 +00:00
|
|
|
run(_context, _object, StackLayoutGenerator::reportStackTooDeep(*cfg));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
run(_context, _object, CompilabilityChecker{
|
|
|
|
_context.dialect,
|
|
|
|
_object,
|
|
|
|
true
|
|
|
|
}.unreachableVariables);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackLimitEvader::run(
|
|
|
|
OptimiserStepContext& _context,
|
|
|
|
Object& _object,
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, std::vector<StackLayoutGenerator::StackTooDeep>> const& _stackTooDeepErrors
|
2021-08-12 15:17:21 +00:00
|
|
|
)
|
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, std::vector<YulString>> unreachableVariables;
|
2021-08-12 15:17:21 +00:00
|
|
|
for (auto&& [function, stackTooDeepErrors]: _stackTooDeepErrors)
|
2023-06-07 13:55:22 +00:00
|
|
|
{
|
|
|
|
auto& unreachables = unreachableVariables[function];
|
2021-08-12 15:17:21 +00:00
|
|
|
// TODO: choose wisely.
|
|
|
|
for (auto const& stackTooDeepError: stackTooDeepErrors)
|
2023-06-07 13:55:22 +00:00
|
|
|
for (auto variable: stackTooDeepError.variableChoices | ranges::views::take(stackTooDeepError.deficit))
|
|
|
|
if (!util::contains(unreachables, variable))
|
|
|
|
unreachables.emplace_back(variable);
|
|
|
|
}
|
2021-08-12 15:17:21 +00:00
|
|
|
run(_context, _object, unreachableVariables);
|
|
|
|
}
|
|
|
|
|
2020-07-02 10:48:20 +00:00
|
|
|
void StackLimitEvader::run(
|
|
|
|
OptimiserStepContext& _context,
|
|
|
|
Object& _object,
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, std::vector<YulString>> const& _unreachableVariables
|
2020-07-02 10:48:20 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
yulAssert(_object.code, "");
|
|
|
|
auto const* evmDialect = dynamic_cast<EVMDialect const*>(&_context.dialect);
|
|
|
|
yulAssert(
|
|
|
|
evmDialect && evmDialect->providesObjectAccess(),
|
|
|
|
"StackLimitEvader can only be run on objects using the EVMDialect with object access."
|
|
|
|
);
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::vector<FunctionCall*> memoryGuardCalls = FunctionCallFinder::run(
|
2020-07-02 10:48:20 +00:00
|
|
|
*_object.code,
|
|
|
|
"memoryguard"_yulstring
|
|
|
|
);
|
|
|
|
// Do not optimise, if no ``memoryguard`` call is found.
|
|
|
|
if (memoryGuardCalls.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Make sure all calls to ``memoryguard`` we found have the same value as argument (otherwise, abort).
|
|
|
|
u256 reservedMemory = literalArgumentValue(*memoryGuardCalls.front());
|
2020-10-13 01:18:21 +00:00
|
|
|
yulAssert(reservedMemory < u256(1) << 32 - 1, "");
|
|
|
|
|
2020-09-17 15:25:37 +00:00
|
|
|
for (FunctionCall const* memoryGuardCall: memoryGuardCalls)
|
|
|
|
if (reservedMemory != literalArgumentValue(*memoryGuardCall))
|
2020-07-02 10:48:20 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
CallGraph callGraph = CallGraphGenerator::callGraph(*_object.code);
|
|
|
|
|
|
|
|
// We cannot move variables in recursive functions to fixed memory offsets.
|
|
|
|
for (YulString function: callGraph.recursiveFunctions())
|
|
|
|
if (_unreachableVariables.count(function))
|
|
|
|
return;
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, FunctionDefinition const*> functionDefinitions = allFunctionDefinitions(*_object.code);
|
2020-10-13 01:18:21 +00:00
|
|
|
|
|
|
|
MemoryOffsetAllocator memoryOffsetAllocator{_unreachableVariables, callGraph.functionCalls, functionDefinitions};
|
2020-07-02 10:48:20 +00:00
|
|
|
uint64_t requiredSlots = memoryOffsetAllocator.run();
|
2020-10-13 01:18:21 +00:00
|
|
|
yulAssert(requiredSlots < (uint64_t(1) << 32) - 1, "");
|
2020-07-02 10:48:20 +00:00
|
|
|
|
2020-09-17 15:36:18 +00:00
|
|
|
StackToMemoryMover::run(_context, reservedMemory, memoryOffsetAllocator.slotAllocations, requiredSlots, *_object.code);
|
2020-09-17 15:25:37 +00:00
|
|
|
|
2020-07-02 10:48:20 +00:00
|
|
|
reservedMemory += 32 * requiredSlots;
|
2020-09-17 15:25:37 +00:00
|
|
|
for (FunctionCall* memoryGuardCall: FunctionCallFinder::run(*_object.code, "memoryguard"_yulstring))
|
2020-07-02 10:48:20 +00:00
|
|
|
{
|
|
|
|
Literal* literal = std::get_if<Literal>(&memoryGuardCall->arguments.front());
|
|
|
|
yulAssert(literal && literal->kind == LiteralKind::Number, "");
|
2021-09-16 14:33:28 +00:00
|
|
|
literal->value = YulString{toCompactHexWithPrefix(reservedMemory)};
|
2020-07-02 10:48:20 +00:00
|
|
|
}
|
|
|
|
}
|