[ewasm] Display custom sections as hex string in the text output

This commit is contained in:
Alex Beregszaszi 2020-11-09 21:18:23 +00:00 committed by Kamil Śliwak
parent e5396e42c3
commit 6f6bcdd526
4 changed files with 35 additions and 13 deletions

View File

@ -19,10 +19,13 @@
* Component that transforms internal Wasm representation to text. * Component that transforms internal Wasm representation to text.
*/ */
#include <libyul/backends/wasm/BinaryTransform.h>
#include <libyul/backends/wasm/TextTransform.h> #include <libyul/backends/wasm/TextTransform.h>
#include <libyul/Exceptions.h> #include <libyul/Exceptions.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/Keccak256.h>
#include <libsolutil/StringUtils.h> #include <libsolutil/StringUtils.h>
#include <libsolutil/Visitor.h> #include <libsolutil/Visitor.h>
@ -39,16 +42,27 @@ using namespace solidity::util;
string TextTransform::run(wasm::Module const& _module) string TextTransform::run(wasm::Module const& _module)
{ {
string ret = "(module\n"; string ret = "(module\n";
for (auto const& sub: _module.subModules) for (auto const& [name, module]: _module.subModules)
ret += ret +=
" ;; sub-module \"" + " ;; custom section for sub-module\n"
sub.first + " ;; The Keccak-256 hash of the text representation of \"" +
"\" will be encoded as custom section in binary here, but is skipped in text mode.\n"; name +
for (auto const& data: _module.customSections) "\": " +
toHex(keccak256(run(module))) +
"\n"
" ;; (@custom \"" +
name +
"\" \"" +
toHex(BinaryTransform::run(module)) +
"\")\n";
for (auto const& [name, data]: _module.customSections)
ret += ret +=
" ;; custom-section \"" + " ;; custom section for data\n"
data.first + " ;; (@custom \"" +
"\" will be encoded as custom section in binary here, but is skipped in text mode.\n"; name +
"\" \"" +
toHex(data) +
"\")\n";
for (wasm::FunctionImport const& imp: _module.imports) for (wasm::FunctionImport const& imp: _module.imports)
{ {
ret += " (import \"" + imp.module + "\" \"" + imp.externalName + "\" (func $" + imp.internalName; ret += " (import \"" + imp.module + "\" \"" + imp.externalName + "\" (func $" + imp.internalName;

View File

@ -1,5 +1,7 @@
{"contracts":{"A":{"C":{"ewasm":{"wast":"(module {"contracts":{"A":{"C":{"ewasm":{"wast":"(module
;; sub-module \"C_2_deployed\" will be encoded as custom section in binary here, but is skipped in text mode. ;; custom section for sub-module
;; The Keccak-256 hash of the text representation of \"C_2_deployed\": f03f5b9154b9eb6803a947177e38e92e2860de95e90ba0e75eb71a58f18ed589
;; (@custom \"C_2_deployed\" \"0061736d0100000001160460000060017e017e60047e7e7e7e017f60027f7f0002130108657468657265756d067265766572740003030504000201010503010001060100071102066d656d6f72790200046d61696e00010ab60204ca0104017e027f057e037f02404200210020002000200042c00010022101200141c0006a210220022001490440000b20001003421086210320032000421088100384422086210420042000422088100484210520022005370000200241086a2005370000200241106a20053700004280011003421086210620064280014210881003844220862107200241186a2007428001422088100484370000200020002000200010022108200020002000200010022109200941c0006a210a200a2009490440000b200a200810000b0b2901017f024042002000200184200284520440000b42002003422088520440000b2003a721040b20040b1f01017e024020004208864280fe0383200042088842ff01838421010b20010b1e01027e02402000100342108621022002200042108810038421010b20010b\")
(import \"ethereum\" \"codeCopy\" (func $eth.codeCopy (param i32 i32 i32))) (import \"ethereum\" \"codeCopy\" (func $eth.codeCopy (param i32 i32 i32)))
(import \"ethereum\" \"revert\" (func $eth.revert (param i32 i32))) (import \"ethereum\" \"revert\" (func $eth.revert (param i32 i32)))
(import \"ethereum\" \"getCallValue\" (func $eth.getCallValue (param i32))) (import \"ethereum\" \"getCallValue\" (func $eth.getCallValue (param i32)))

View File

@ -9,8 +9,11 @@ object "a" {
// ---- // ----
// Text: // Text:
// (module // (module
// ;; sub-module "sub" will be encoded as custom section in binary here, but is skipped in text mode. // ;; custom section for sub-module
// ;; custom-section "str" will be encoded as custom section in binary here, but is skipped in text mode. // ;; The Keccak-256 hash of the text representation of "sub": 78ac3419d75c8d6f42f663717b8e964eeb994d77ff175145133084422dbd23d7
// ;; (@custom "sub" "0061736d01000000010401600000020100030201000503010001060100071102066d656d6f72790200046d61696e00000a0801060002401a0b0b")
// ;; custom section for data
// ;; (@custom "str" "48656c6c6f2c20576f726c6421")
// (memory $memory (export "memory") 1) // (memory $memory (export "memory") 1)
// //
// ) // )

View File

@ -58,8 +58,11 @@ object "A" {
// ---- // ----
// Text: // Text:
// (module // (module
// ;; sub-module "B" will be encoded as custom section in binary here, but is skipped in text mode. // ;; custom section for sub-module
// ;; custom-section "data1" will be encoded as custom section in binary here, but is skipped in text mode. // ;; The Keccak-256 hash of the text representation of "B": 1eeffe5bc8d8819350ead60cc71ccd92c223cf52a908330db53461eb9ac89b62
// ;; (@custom "B" "0061736d01000000010401600000020100030201000503010001060100071102066d656d6f72790200046d61696e0000007b01430061736d01000000010401600000020100030201000503010001060100071102066d656d6f72790200046d61696e0000003c01440061736d01000000010401600000020100030201000503010001060100071102066d656d6f72790200046d61696e00000a080106000240000b0b0a0901070002401a1a0b0b003c01450061736d01000000010401600000020100030201000503010001060100071102066d656d6f72790200046d61696e00000a080106000240000b0b0a0b01090002401a1a1a1a0b0b")
// ;; custom section for data
// ;; (@custom "data1" "48656c6c6f2c20576f726c6421")
// (memory $memory (export "memory") 1) // (memory $memory (export "memory") 1)
// (export "main" (func $main)) // (export "main" (func $main))
// //