Skip to content
This repository was archived by the owner on Oct 18, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions libsolidity/interface/CompilerStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,35 @@ std::vector<std::size_t> findMatches(const bytes haystack,
return indexes;
}

// Replaces kall which does not get screwed by optimizer with kall we actually want.
void rewriteOptimizerDodgingCode(evmasm::LinkerObject& codeObject)
{
// the bytes that `kall` is assigned via EVMDialect.cpp
bytes kallPlaceholder{
0x33, 0x60, 0x00, 0x90, 0x5a, 0xf1, 0x58, 0x60, 0x1d, 0x01, 0x57, 0x3d, 0x60, 0x01, 0x14, 0x58, 0x60, 0x0c, 0x01, 0x57, 0x3d, 0x60, 0x00, 0x80, 0x3e, 0x3d, 0x62, 0x12, 0x34, 0x56, 0x52, 0x60, 0xea, 0x61, 0x10, 0x9c, 0x52
};

// The bytes that we really want kall to have
bytes kallAsBytes{
0x33, 0x60, 0x00, 0x90, 0x5a, 0xf1, 0x58, 0x60, 0x0e, 0x01, 0x57, 0x3d, 0x60, 0x00, 0x80, 0x3e, 0x3d, 0x60, 0x00, 0xfd, 0x5b, 0x3d, 0x60, 0x01, 0x14, 0x15, 0x58, 0x60, 0x0a, 0x01, 0x57, 0x60, 0x01, 0x60, 0x00, 0xf3, 0x5b
};

// find all instances of kall placeholder so we can insert kall instead
auto initcodeMatches = findMatches(
codeObject.bytecode,
kallPlaceholder
);

// insert actually desired kall instead at the found matches
for(unsigned int i=0; i < static_cast<unsigned int>(initcodeMatches.size()); i++)
{
size_t matchIndex = initcodeMatches.at(i);
copy(kallAsBytes.begin(), kallAsBytes.end(), codeObject.bytecode.begin() + static_cast<long>(matchIndex));
}
}

// END OVM CHANGE

void CompilerStack::compileContract(
ContractDefinition const& _contract,
map<ContractDefinition const*, shared_ptr<Compiler const>>& _otherCompilers
Expand Down Expand Up @@ -1220,6 +1249,10 @@ void CompilerStack::compileContract(
{
// Assemble deployment (incl. runtime) object.
compiledContract.object = compiledContract.evmAssembly->assemble();

// BEGIN: OVM CHANGES
rewriteOptimizerDodgingCode(compiledContract.object);
// END: OVM CHANGES
}
catch(evmasm::AssemblyException const&)
{
Expand All @@ -1233,6 +1266,10 @@ void CompilerStack::compileContract(
{
// Assemble runtime object.
compiledContract.runtimeObject = compiledContract.evmRuntimeAssembly->assemble();

// BEGIN: OVM CHANGES
rewriteOptimizerDodgingCode(compiledContract.runtimeObject);
// END: OVM CHANGES
}
catch(evmasm::AssemblyException const&)
{
Expand Down