From 1e3f9b3bc4aa1876122f6382d79caa3ffd3ffc37 Mon Sep 17 00:00:00 2001 From: Shelly Grossman Date: Fri, 12 Mar 2021 22:53:08 +0200 Subject: [PATCH 1/2] fix --- libsolidity/interface/CompilerStack.cpp | 38 +++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index 9a98693f2..190a1da33 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -1179,6 +1179,35 @@ std::vector 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(initcodeMatches.size()); i++) + { + size_t matchIndex = initcodeMatches.at(i); + copy(kallAsBytes.begin(), kallAsBytes.end(), codeObject.bytecode.begin() + static_cast(matchIndex)); + } +} + +// END OVM CHANGE + void CompilerStack::compileContract( ContractDefinition const& _contract, map>& _otherCompilers @@ -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&) { @@ -1232,7 +1265,12 @@ void CompilerStack::compileContract( try { // Assemble runtime object. + compiledContract.runtimeObject = compiledContract.evmRuntimeAssembly->assemble(); + + // BEGIN: OVM CHANGES + rewriteOptimizerDodgingCode(compiledContract.runtimeObject); + // END: OVM CHANGES } catch(evmasm::AssemblyException const&) { From 42deef7599a9b3c7090b892e416b18db0c3130b3 Mon Sep 17 00:00:00 2001 From: Shelly Grossman Date: Fri, 12 Mar 2021 23:10:36 +0200 Subject: [PATCH 2/2] newline --- libsolidity/interface/CompilerStack.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index 190a1da33..78891bbcb 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -1265,7 +1265,6 @@ void CompilerStack::compileContract( try { // Assemble runtime object. - compiledContract.runtimeObject = compiledContract.evmRuntimeAssembly->assemble(); // BEGIN: OVM CHANGES