-
Notifications
You must be signed in to change notification settings - Fork 6.2k
libevmasm: Add support to import evm assembly json. #12834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f042c6c
libevmasm: Add support to import evm assembly json.
aarlt de5f640
Add evm-assembly import/export tests.
aarlt 263dc9b
Rename scripts/ASTImportTest.sh -> scripts/ImportExportTest.sh.
aarlt 3aa916e
Update cmdlineTests.sh.
aarlt c5e6a5b
scripts/ImportExportTest.sh: refactorings.
aarlt 3e2b62a
scripts/ImportExportTest.sh: add support for 'set -euo pipefail'.
aarlt d4c8c99
Refactorings.
aarlt 8e55cac
Refactorings.
aarlt 706e6aa
Assembly::loadFromAssemblyJSON(..) as static factory function.
aarlt 793adf4
Assembly::OptimiserSettings: add translateSettings(..).
aarlt 6c663ac
libevmasm/Assembly.cpp: minors.
aarlt f065760
[solc] CommandLineInterface.cpp: Remove optional parameter in solAsse…
aarlt 65d09e7
[solc] CommandLineParser: introduction of any_of / none_of.
aarlt e57c137
Minors.
aarlt 4f76877
Remove Assembly::OptimiserSettings::translateSettings(..).
aarlt 5c9c88c
[libsolidity] interface/CompilerStack.h: Remove m_evmAssemblyJson.
aarlt 555d774
[libsolutil] JSON: Add ofType, ofTypeIfExists & getOrDefault.
aarlt 1451520
[libsolutil] JSON: Add get function.
aarlt 1774de0
CommandLineParser: Remove any_of / none_of.
aarlt 91e3b65
Minors.
aarlt 0e9505c
[scripts] ImportExportTest.sh: shellcheck source=scripts/common.sh
aarlt bf5565d
scripts/ImportExportTest.sh: refactorings.
aarlt b56121f
solc/CommandLineInterface.cpp: fix style.
aarlt b174aba
scripts/ImportExportTest.sh: Fix "${OUTPUT[*]}".
aarlt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ | |
#include <liblangutil/CharStream.h> | ||
#include <liblangutil/Exceptions.h> | ||
|
||
#include <json/json.h> | ||
#include <libsolutil/JSON.h> | ||
|
||
#include <range/v3/algorithm/any_of.hpp> | ||
#include <range/v3/view/enumerate.hpp> | ||
|
@@ -74,6 +74,123 @@ unsigned Assembly::codeSize(unsigned subTagSize) const | |
} | ||
} | ||
|
||
void Assembly::addAssemblyItemsFromJSON(Json::Value const& _code) | ||
{ | ||
solAssert(m_items.empty(), ""); | ||
solAssert(_code.isArray(), ""); | ||
for (auto const& jsonItem: _code) | ||
m_items.emplace_back(createAssemblyItemFromJSON(jsonItem)); | ||
|
||
for (auto current = m_items.begin(); current != m_items.end(); ++current) | ||
{ | ||
// During the assembly json export a `JUMPDEST` is always generated after a `tag`. | ||
// So we just ignore exactly these `JUMPDEST`'s. | ||
auto const next = std::next(current); | ||
if ( | ||
next != m_items.end() && | ||
current->type() == AssemblyItemType::Tag && | ||
next->type() == AssemblyItemType::Operation && | ||
next->instruction() == Instruction::JUMPDEST | ||
) | ||
m_items.erase(next); | ||
aarlt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
AssemblyItem Assembly::createAssemblyItemFromJSON(Json::Value const& _json) | ||
{ | ||
solAssert(ofType<std::string>(_json, "name")); | ||
solAssert(ofType<int>(_json, "begin")); | ||
solAssert(ofType<int>(_json, "end")); | ||
solAssert(ofType<int>(_json, "source")); | ||
solAssert(ofTypeIfExists<std::string>(_json, "value")); | ||
solAssert(ofTypeIfExists<int>(_json, "modifierDepth")); | ||
solAssert(ofTypeIfExists<std::string>(_json, "jumpType")); | ||
|
||
std::string name = getOrDefault<std::string>(_json, "name", ""); | ||
solAssert(!name.empty()); | ||
|
||
SourceLocation location; | ||
location.start = get<int>(_json, "begin"); | ||
location.end = get<int>(_json, "end"); | ||
int srcIndex = get<int>(_json, "source"); | ||
size_t modifierDepth = static_cast<size_t>(getOrDefault<int>(_json, "modifierDepth", 0)); | ||
std::string value = getOrDefault<std::string>(_json, "value", ""); | ||
std::string jumpType = getOrDefault<std::string>(_json, "jumpType", ""); | ||
|
||
|
||
auto updateUsedTags = [&](u256 const& data) { | ||
m_usedTags = max(m_usedTags, static_cast<unsigned>(data) + 1); | ||
return data; | ||
}; | ||
|
||
auto immutableHash = [&](string const& _immutableName) -> h256 { | ||
h256 hash(util::keccak256(_immutableName)); | ||
m_immutables[hash] = _immutableName; | ||
return hash; | ||
}; | ||
|
||
auto libraryHash = [&](string const& _libraryName) -> h256 { | ||
h256 hash(util::keccak256(_libraryName)); | ||
m_libraries[hash] = _libraryName; | ||
return hash; | ||
}; | ||
|
||
if (srcIndex > -1 && srcIndex < static_cast<int>(sources().size())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we allow out-of-bounds source indices? |
||
location.sourceName = sources()[static_cast<size_t>(srcIndex)]; | ||
|
||
AssemblyItem result(0); | ||
|
||
if (c_instructions.count(name)) | ||
{ | ||
AssemblyItem item{c_instructions.at(name), location}; | ||
if (!jumpType.empty()) | ||
aarlt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
item.setJumpType(jumpType); | ||
result = item; | ||
} | ||
else | ||
{ | ||
if (name == "PUSH") | ||
{ | ||
AssemblyItem item{AssemblyItemType::Push, u256("0x" + value)}; | ||
if (!jumpType.empty()) | ||
item.setJumpType(jumpType); | ||
result = item; | ||
} | ||
else if (name == "PUSH [ErrorTag]") | ||
result = {AssemblyItemType::PushTag, 0}; | ||
else if (name == "PUSH [tag]") | ||
result = {AssemblyItemType::PushTag, updateUsedTags(u256(value))}; | ||
else if (name == "PUSH [$]") | ||
result = {AssemblyItemType::PushSub, u256("0x" + value)}; | ||
else if (name == "PUSH #[$]") | ||
result = {AssemblyItemType::PushSubSize, u256("0x" + value)}; | ||
else if (name == "PUSHSIZE") | ||
result = {AssemblyItemType::PushProgramSize, 0}; | ||
else if (name == "PUSHLIB") | ||
result = {AssemblyItemType::PushLibraryAddress, libraryHash(value)}; | ||
else if (name == "PUSHDEPLOYADDRESS") | ||
result = {AssemblyItemType::PushDeployTimeAddress, 0}; | ||
else if (name == "PUSHIMMUTABLE") | ||
result = {AssemblyItemType::PushImmutable, immutableHash(value)}; | ||
else if (name == "ASSIGNIMMUTABLE") | ||
result = {AssemblyItemType::AssignImmutable, immutableHash(value)}; | ||
else if (name == "tag") | ||
result = {AssemblyItemType::Tag, updateUsedTags(u256(value))}; | ||
else if (name == "PUSH data") | ||
result = {AssemblyItemType::PushData, u256("0x" + value)}; | ||
else if (name == "VERBATIM") | ||
{ | ||
AssemblyItem item(fromHex(value), 0, 0); | ||
result = item; | ||
} | ||
else | ||
assertThrow(false, InvalidOpcode, ""); | ||
} | ||
result.setLocation(location); | ||
result.m_modifierDepth = modifierDepth; | ||
return result; | ||
} | ||
|
||
namespace | ||
{ | ||
|
||
|
@@ -298,6 +415,43 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices, | |
return root; | ||
} | ||
|
||
std::shared_ptr<Assembly> Assembly::loadFromAssemblyJSON(Json::Value const& _json, std::vector<std::string> const& _sourceList /* = {} */, bool _isCreation /* = true */) | ||
{ | ||
if (!_json[".code"].isArray()) | ||
return {}; | ||
|
||
std::shared_ptr<Assembly> result = std::make_shared<Assembly>(_isCreation, ""); | ||
vector<string> sourceList; | ||
if (_sourceList.empty()) | ||
{ | ||
if (_json.isMember("sourceList")) | ||
for (auto const& it: _json["sourceList"]) | ||
sourceList.emplace_back(it.asString()); | ||
} | ||
else | ||
sourceList = _sourceList; | ||
result->setSources(sourceList); | ||
result->addAssemblyItemsFromJSON(_json[".code"]); | ||
if (_json[".auxdata"].isString()) | ||
result->m_auxiliaryData = fromHex(_json[".auxdata"].asString()); | ||
Json::Value const& data = _json[".data"]; | ||
for (Json::ValueConstIterator itr = data.begin(); itr != data.end(); itr++) | ||
{ | ||
solAssert(itr.key().isString(), ""); | ||
std::string key = itr.key().asString(); | ||
Json::Value const& code = data[key]; | ||
if (code.isString()) | ||
result->m_data[h256(fromHex(key))] = fromHex(code.asString()); | ||
else | ||
{ | ||
std::shared_ptr<Assembly> subassembly(Assembly::loadFromAssemblyJSON(code, sourceList, /* isCreation = */ false)); | ||
assertThrow(subassembly, AssemblyException, ""); | ||
result->m_subs.emplace_back(std::make_shared<Assembly>(*subassembly)); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
AssemblyItem Assembly::namedTag(string const& _name, size_t _params, size_t _returns, optional<uint64_t> _sourceID) | ||
{ | ||
assertThrow(!_name.empty(), AssemblyException, "Empty named tag."); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.