Compatibility with StackCompressor and StackLimitEvader.

This commit is contained in:
Daniel Kirchner
2021-08-17 19:00:28 +02:00
parent de7f26c15d
commit 8bd358074e
12 changed files with 381 additions and 67 deletions
+6 -3
View File
@@ -26,8 +26,8 @@
#include <libyul/Object.h>
#include <libyul/Exceptions.h>
#include <libyul/AsmParser.h>
#include <libyul/Utilities.h>
#include <libyul/backends/evm/AbstractAssembly.h>
#include <libevmasm/SemanticInformation.h>
#include <libevmasm/Instruction.h>
@@ -185,9 +185,12 @@ map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion _evmVe
FunctionCall const& _call,
AbstractAssembly& _assembly,
BuiltinContext&,
function<void(Expression const&)> _visitExpression
function<void(Expression const&)>
) {
visitArguments(_assembly, _call, _visitExpression);
yulAssert(_call.arguments.size() == 1, "");
Literal const* literal = get_if<Literal>(&_call.arguments.front());
yulAssert(literal, "");
_assembly.appendConstant(valueOfLiteral(*literal));
})
);
+1
View File
@@ -141,6 +141,7 @@ private:
[&](size_t _offset) { return _ops.sourceIsSame(sourceOffset, _offset); }
))
continue;
// Bring up the target slot that would otherwise become unreachable.
for (size_t targetOffset: ranges::views::iota(0u, _ops.targetSize()))
if (!_ops.targetIsArbitrary(targetOffset) && _ops.isCompatible(sourceOffset, targetOffset))
+130 -5
View File
@@ -38,6 +38,7 @@
#include <range/v3/view/map.hpp>
#include <range/v3/view/reverse.hpp>
#include <range/v3/view/take.hpp>
#include <range/v3/view/take_last.hpp>
#include <range/v3/view/transform.hpp>
using namespace solidity;
@@ -47,17 +48,53 @@ using namespace std;
StackLayout StackLayoutGenerator::run(CFG const& _cfg)
{
StackLayout stackLayout;
StackLayoutGenerator{stackLayout, {}}.processEntryPoint(*_cfg.entry);
{
StackLayoutGenerator generator{stackLayout};
generator.processEntryPoint(*_cfg.entry);
}
for (auto& functionInfo: _cfg.functionInfo | ranges::views::values)
StackLayoutGenerator{stackLayout, functionInfo.returnVariables}.processEntryPoint(*functionInfo.entry);
{
StackLayoutGenerator generator{stackLayout};
generator.processEntryPoint(*functionInfo.entry);
}
return stackLayout;
}
map<YulString, vector<StackLayoutGenerator::StackTooDeep>> StackLayoutGenerator::reportStackTooDeep(CFG const& _cfg)
{
map<YulString, vector<StackLayoutGenerator::StackTooDeep>> stackTooDeepErrors;
stackTooDeepErrors[YulString{}] = reportStackTooDeep(_cfg, YulString{});
for (auto const& function: _cfg.functions)
{
auto errors = reportStackTooDeep(_cfg, function->name);
if (!errors.empty())
stackTooDeepErrors[function->name] = errors;
}
return stackTooDeepErrors;
}
vector<StackLayoutGenerator::StackTooDeep> StackLayoutGenerator::reportStackTooDeep(CFG const& _cfg, YulString _functionName)
{
StackLayout stackLayout;
CFG::FunctionInfo const* functionInfo = nullptr;
if (!_functionName.empty())
{
for (auto&& [function, info]: _cfg.functionInfo)
if (info.function.name.str() == _functionName.str())
{
functionInfo = &info;
break;
}
yulAssert(functionInfo, "Function not found.");
}
StackLayoutGenerator::StackLayoutGenerator(StackLayout& _layout, vector<VariableSlot> _currentFunctionReturnVariables):
m_layout(_layout),
m_currentFunctionReturnVariables(move(_currentFunctionReturnVariables))
StackLayoutGenerator generator{stackLayout};
CFG::BasicBlock const* entry = functionInfo ? functionInfo->entry : _cfg.entry;
generator.processEntryPoint(*entry);
return generator.reportStackTooDeep(*entry);
}
StackLayoutGenerator::StackLayoutGenerator(StackLayout& _layout): m_layout(_layout)
{
}
@@ -481,8 +518,10 @@ Stack StackLayoutGenerator::combineStack(Stack const& _stack1, Stack const& _sta
Stack stack2Tail = _stack2 | ranges::views::drop(commonPrefix.size()) | ranges::to<Stack>;
if (stack1Tail.empty())
// TODO: check if compress stack is actually good here.
return commonPrefix + compressStack(stack2Tail);
if (stack2Tail.empty())
// TODO: check if compress stack is actually good here.
return commonPrefix + compressStack(stack1Tail);
Stack candidate;
@@ -492,6 +531,7 @@ Stack StackLayoutGenerator::combineStack(Stack const& _stack1, Stack const& _sta
for (auto slot: stack2Tail)
if (!util::findOffset(candidate, slot))
candidate.emplace_back(slot);
// TODO: check if compressing here is actually good.
cxx20::erase_if(candidate, [](StackSlot const& slot) {
return holds_alternative<LiteralSlot>(slot) || holds_alternative<FunctionCallReturnLabelSlot>(slot);
});
@@ -550,6 +590,91 @@ Stack StackLayoutGenerator::combineStack(Stack const& _stack1, Stack const& _sta
return commonPrefix + bestCandidate;
}
namespace
{
vector<StackLayoutGenerator::StackTooDeep> findStackTooDeep(Stack const& _source, Stack const& _target)
{
Stack currentStack = _source;
vector<StackLayoutGenerator::StackTooDeep> stackTooDeepErrors;
auto getVariableChoices = [](auto&& range) {
set<YulString> result;
for (auto const& slot: range)
if (auto const* variableSlot = get_if<VariableSlot>(&slot))
result.insert(variableSlot->variable.get().name);
return result;
};
::createStackLayout(currentStack, _target, [&](unsigned _i) {
if (_i > 16)
stackTooDeepErrors.emplace_back(StackLayoutGenerator::StackTooDeep{
_i - 16,
getVariableChoices(currentStack | ranges::views::take_last(_i + 1))
});
}, [&](StackSlot const& _slot) {
if (canBeFreelyGenerated(_slot))
return;
if (
auto depth = util::findOffset(currentStack | ranges::views::reverse, _slot);
depth && *depth >= 16
)
stackTooDeepErrors.emplace_back(StackLayoutGenerator::StackTooDeep{
*depth - 15,
getVariableChoices(currentStack | ranges::views::take_last(*depth + 1))
});
}, [&]() {});
return stackTooDeepErrors;
}
}
vector<StackLayoutGenerator::StackTooDeep> StackLayoutGenerator::reportStackTooDeep(CFG::BasicBlock const& _entry)
{
vector<StackTooDeep> stackTooDeepErrors;
util::BreadthFirstSearch<CFG::BasicBlock const*> breadthFirstSearch{{&_entry}};
breadthFirstSearch.run([&](CFG::BasicBlock const* _block, auto _addChild) {
Stack stack;
stack = m_layout.blockInfos.at(_block).entryLayout;
for (auto const& operation: _block->operations)
{
Stack& operationEntry = m_layout.operationEntryLayout.at(&operation);
stackTooDeepErrors += findStackTooDeep(stack, operationEntry);
stack = operationEntry;
for (size_t i = 0; i < operation.input.size(); i++)
stack.pop_back();
stack += operation.output;
}
// Do not create attempt to create the exit layout here, since the code generator will directly move to the
// target entry layout.
std::visit(util::GenericVisitor{
[&](CFG::BasicBlock::MainExit const&) {},
[&](CFG::BasicBlock::Jump const& _jump)
{
Stack const& targetLayout = m_layout.blockInfos.at(_jump.target).entryLayout;
stackTooDeepErrors += findStackTooDeep(stack, targetLayout);
if (!_jump.backwards)
_addChild(_jump.target);
},
[&](CFG::BasicBlock::ConditionalJump const& _conditionalJump)
{
for (Stack const& targetLayout: {
m_layout.blockInfos.at(_conditionalJump.zero).entryLayout,
m_layout.blockInfos.at(_conditionalJump.nonZero).entryLayout
})
stackTooDeepErrors += findStackTooDeep(stack, targetLayout);
_addChild(_conditionalJump.zero);
_addChild(_conditionalJump.nonZero);
},
[&](CFG::BasicBlock::FunctionReturn const&) {},
[&](CFG::BasicBlock::Terminated const&) {},
}, _block->exit);
});
return stackTooDeepErrors;
}
Stack StackLayoutGenerator::compressStack(Stack _stack)
{
optional<size_t> firstDupOffset;
+15 -2
View File
@@ -47,10 +47,20 @@ struct StackLayout
class StackLayoutGenerator
{
public:
struct StackTooDeep
{
/// Number of slots that need to be saved.
size_t deficit = 0;
/// Set of variables, eliminating which would decrease the stack deficit.
std::set<YulString> variableChoices;
};
static StackLayout run(CFG const& _cfg);
static std::map<YulString, std::vector<StackTooDeep>> reportStackTooDeep(CFG const& _cfg);
static std::vector<StackTooDeep> reportStackTooDeep(CFG const& _cfg, YulString _functionName);
private:
StackLayoutGenerator(StackLayout& _context, std::vector<VariableSlot> _currentFunctionReturnVariables);
StackLayoutGenerator(StackLayout& _context);
/// @returns the optimal entry stack layout, s.t. @a _operation can be applied to it and
/// the result can be transformed to @a _exitStack with minimal stack shuffling.
@@ -86,13 +96,16 @@ private:
/// stack shuffling when starting from the returned layout.
static Stack combineStack(Stack const& _stack1, Stack const& _stack2);
/// Walks through the CFG and reports and stack too deep errors that would occur when generating code for it
/// without countermeasures.
std::vector<StackTooDeep> reportStackTooDeep(CFG::BasicBlock const& _entry);
/// @returns a copy of @a _stack stripped of all duplicates and slots that can be freely generated.
/// Attempts to create a layout that requires a minimal amount of operations to reconstruct the original
/// stack @a _stack.
static Stack compressStack(Stack _stack);
StackLayout& m_layout;
std::vector<VariableSlot> const m_currentFunctionReturnVariables;
};
}