From 8548d17921ab6b4bd455c0268b33f03b1f76a7a6 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Fri, 15 Oct 2021 15:40:28 -0500 Subject: [PATCH 1/3] Update cleos wasm_to_wast to not check limits to support larger wasm. Also improve error reporting of wasm to wast errors. --- .../chain/include/eosio/chain/wast_to_wasm.hpp | 2 +- libraries/chain/wast_to_wasm.cpp | 18 ++++++++++++++---- programs/cleos/main.cpp | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/libraries/chain/include/eosio/chain/wast_to_wasm.hpp b/libraries/chain/include/eosio/chain/wast_to_wasm.hpp index eb478d7653a..79466272ecc 100644 --- a/libraries/chain/include/eosio/chain/wast_to_wasm.hpp +++ b/libraries/chain/include/eosio/chain/wast_to_wasm.hpp @@ -6,6 +6,6 @@ namespace eosio { namespace chain { std::vector wast_to_wasm( const std::string& wast ); std::string wasm_to_wast( const std::vector& wasm, bool strip_names ); -std::string wasm_to_wast( const uint8_t* data, uint64_t size, bool strip_names ); +std::string wasm_to_wast( const uint8_t* data, uint64_t size, bool strip_names, bool check_limits = true ); } } /// eosio::chain diff --git a/libraries/chain/wast_to_wasm.cpp b/libraries/chain/wast_to_wasm.cpp index 0527d57f604..cefd9525d35 100644 --- a/libraries/chain/wast_to_wasm.cpp +++ b/libraries/chain/wast_to_wasm.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include namespace eosio { namespace chain { @@ -62,8 +63,13 @@ namespace eosio { namespace chain { return wasm_to_wast( wasm.data(), wasm.size(), strip_names ); } /// wasm_to_wast - std::string wasm_to_wast( const uint8_t* data, uint64_t size, bool strip_names ) - { try { + std::string wasm_to_wast( const uint8_t* data, uint64_t size, bool strip_names, bool check_limits ) + { + try { + auto reset_check_limits = fc::make_scoped_exit([old_value=WASM::check_limits](){ + WASM::check_limits = old_value; + }); + WASM::check_limits = check_limits; IR::Module module; Serialization::MemoryInputStream stream((const U8*)data,size); WASM::serialize(stream,module); @@ -71,7 +77,11 @@ namespace eosio { namespace chain { module.userSections.clear(); // Print the module to WAST. return WAST::print(module); - } FC_CAPTURE_AND_RETHROW() } /// wasm_to_wast - + } catch(const Serialization::FatalSerializationException& e) { + EOS_ASSERT( false, wasm_exception, "error converting to wast: ${msg}", ("msg",e.message) ); + } catch(const IR::ValidationException& e) { + EOS_ASSERT( false, wasm_exception, "error converting to wast: ${msg}", ("msg",e.message) ); + } FC_CAPTURE_AND_RETHROW() + } } } // eosio::chain diff --git a/programs/cleos/main.cpp b/programs/cleos/main.cpp index 54484a1261a..ef931ccfe07 100644 --- a/programs/cleos/main.cpp +++ b/programs/cleos/main.cpp @@ -2774,7 +2774,7 @@ int main( int argc, char** argv ) { wasm = string(wasm_v.begin(), wasm_v.end()); if(!code_as_wasm && wasm_v.size()) - wast = wasm_to_wast((const uint8_t*)wasm_v.data(), wasm_v.size(), false); + wast = wasm_to_wast((const uint8_t*)wasm_v.data(), wasm_v.size(), false, false); abi_def abi_d; if(abi_serializer::to_abi(abi_v, abi_d)) From 84fc6eb6868d709c820520d817b4898672394825 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Fri, 15 Oct 2021 16:13:48 -0500 Subject: [PATCH 2/3] Remove conversion of wasm to wast --- programs/cleos/main.cpp | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/programs/cleos/main.cpp b/programs/cleos/main.cpp index ef931ccfe07..1e5ef4f2687 100644 --- a/programs/cleos/main.cpp +++ b/programs/cleos/main.cpp @@ -80,7 +80,6 @@ Usage: ./cleos create account [OPTIONS] creator name OwnerKey ActiveKey #include #include -#include #include #include #include @@ -2754,14 +2753,14 @@ int main( int argc, char** argv ) { // get code string codeFilename; string abiFilename; - bool code_as_wasm = false; + bool code_as_wasm = true; auto getCode = get->add_subcommand("code", localized("Retrieve the code and ABI for an account")); getCode->add_option("name", accountName, localized("The name of the account whose code should be retrieved"))->required(); - getCode->add_option("-c,--code",codeFilename, localized("The name of the file to save the contract .wast/wasm to") ); + getCode->add_option("-c,--code",codeFilename, localized("The name of the file to save the contract wasm to") ); getCode->add_option("-a,--abi",abiFilename, localized("The name of the file to save the contract .abi to") ); - getCode->add_flag("--wasm", code_as_wasm, localized("Save contract as wasm")); + getCode->add_flag("--wasm", code_as_wasm, localized("Save contract as wasm (ignored, default)")); getCode->callback([&] { - string code_hash, wasm, wast, abi; + string code_hash, wasm, abi; try { const auto result = call(get_raw_code_and_abi_func, fc::mutable_variant_object("account_name", accountName)); const std::vector wasm_v = result["wasm"].as_blob().data; @@ -2773,8 +2772,6 @@ int main( int argc, char** argv ) { code_hash = (string)hash; wasm = string(wasm_v.begin(), wasm_v.end()); - if(!code_as_wasm && wasm_v.size()) - wast = wasm_to_wast((const uint8_t*)wasm_v.data(), wasm_v.size(), false, false); abi_def abi_d; if(abi_serializer::to_abi(abi_v, abi_d)) @@ -2784,25 +2781,18 @@ int main( int argc, char** argv ) { //see if this is an old nodeos that doesn't support get_raw_code_and_abi const auto old_result = call(get_code_func, fc::mutable_variant_object("account_name", accountName)("code_as_wasm",code_as_wasm)); code_hash = old_result["code_hash"].as_string(); - if(code_as_wasm) { - wasm = old_result["wasm"].as_string(); - std::cout << localized("Warning: communicating to older ${n} which returns malformed binary wasm", ("n", node_executable_name)) << std::endl; - } - else - wast = old_result["wast"].as_string(); + wasm = old_result["wasm"].as_string(); + std::cout << localized("Warning: communicating to older ${n} which returns malformed binary wasm", ("n", node_executable_name)) << std::endl; abi = fc::json::to_pretty_string(old_result["abi"]); } std::cout << localized("code hash: ${code_hash}", ("code_hash", code_hash)) << std::endl; if( codeFilename.size() ){ - std::cout << localized("saving ${type} to ${codeFilename}", ("type", (code_as_wasm ? "wasm" : "wast"))("codeFilename", codeFilename)) << std::endl; + std::cout << localized("saving wasm to ${codeFilename}", ("codeFilename", codeFilename)) << std::endl; std::ofstream out( codeFilename.c_str() ); - if(code_as_wasm) - out << wasm; - else - out << wast; + out << wasm; } if( abiFilename.size() ) { std::cout << localized("saving abi to ${abiFilename}", ("abiFilename", abiFilename)) << std::endl; From 746e622196bddc4fda4a304f20b96c181fc7d1a5 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Sat, 16 Oct 2021 08:34:22 -0500 Subject: [PATCH 3/3] Remove no longer used wasm_to_wast --- .../include/eosio/chain/wast_to_wasm.hpp | 2 -- libraries/chain/wast_to_wasm.cpp | 26 ------------------- unittests/wasm_tests.cpp | 15 ++++++----- 3 files changed, 8 insertions(+), 35 deletions(-) diff --git a/libraries/chain/include/eosio/chain/wast_to_wasm.hpp b/libraries/chain/include/eosio/chain/wast_to_wasm.hpp index 79466272ecc..d601bbf5cef 100644 --- a/libraries/chain/include/eosio/chain/wast_to_wasm.hpp +++ b/libraries/chain/include/eosio/chain/wast_to_wasm.hpp @@ -5,7 +5,5 @@ namespace eosio { namespace chain { std::vector wast_to_wasm( const std::string& wast ); -std::string wasm_to_wast( const std::vector& wasm, bool strip_names ); -std::string wasm_to_wast( const uint8_t* data, uint64_t size, bool strip_names, bool check_limits = true ); } } /// eosio::chain diff --git a/libraries/chain/wast_to_wasm.cpp b/libraries/chain/wast_to_wasm.cpp index cefd9525d35..ed4769a0eee 100644 --- a/libraries/chain/wast_to_wasm.cpp +++ b/libraries/chain/wast_to_wasm.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -59,29 +58,4 @@ namespace eosio { namespace chain { } FC_CAPTURE_AND_RETHROW( (wast) ) } /// wast_to_wasm - std::string wasm_to_wast( const std::vector& wasm, bool strip_names ) { - return wasm_to_wast( wasm.data(), wasm.size(), strip_names ); - } /// wasm_to_wast - - std::string wasm_to_wast( const uint8_t* data, uint64_t size, bool strip_names, bool check_limits ) - { - try { - auto reset_check_limits = fc::make_scoped_exit([old_value=WASM::check_limits](){ - WASM::check_limits = old_value; - }); - WASM::check_limits = check_limits; - IR::Module module; - Serialization::MemoryInputStream stream((const U8*)data,size); - WASM::serialize(stream,module); - if(strip_names) - module.userSections.clear(); - // Print the module to WAST. - return WAST::print(module); - } catch(const Serialization::FatalSerializationException& e) { - EOS_ASSERT( false, wasm_exception, "error converting to wast: ${msg}", ("msg",e.message) ); - } catch(const IR::ValidationException& e) { - EOS_ASSERT( false, wasm_exception, "error converting to wast: ${msg}", ("msg",e.message) ); - } FC_CAPTURE_AND_RETHROW() - } - } } // eosio::chain diff --git a/unittests/wasm_tests.cpp b/unittests/wasm_tests.cpp index 7aa77f96bcb..e6dc374799d 100644 --- a/unittests/wasm_tests.cpp +++ b/unittests/wasm_tests.cpp @@ -1665,17 +1665,18 @@ BOOST_FIXTURE_TEST_CASE( fuzz, TESTER ) try { vector wasm(g80k_deep_loop_with_voidData, g80k_deep_loop_with_voidData + g80k_deep_loop_with_voidSize); BOOST_CHECK_THROW(set_code("fuzzy"_n, wasm), wasm_exception); } + { + vector wasm(ggetcode_deepindentData, ggetcode_deepindentData + ggetcode_deepindentSize); + set_code( "fuzzy"_n, wasm ); + } + { + vector wasm(gindent_mismatchData, gindent_mismatchData + gindent_mismatchSize); + set_code( "fuzzy"_n, wasm ); + } produce_blocks(1); } FC_LOG_AND_RETHROW() -BOOST_FIXTURE_TEST_CASE( getcode_checks, TESTER ) try { - vector wasm(ggetcode_deepindentData, ggetcode_deepindentData + ggetcode_deepindentSize); - wasm_to_wast( wasm.data(), wasm.size(), true ); - vector wasmx(gindent_mismatchData, gindent_mismatchData + gindent_mismatchSize); - wasm_to_wast( wasmx.data(), wasmx.size(), true ); -} FC_LOG_AND_RETHROW() - BOOST_FIXTURE_TEST_CASE( big_maligned_host_ptr, TESTER ) try { produce_blocks(2); create_accounts( {"bigmaligned"_n} );