diff --git a/external/wamr/patches/ripp_metering.patch b/external/wamr/patches/ripp_metering.patch index 5a41b125b7..5e09eec8df 100644 --- a/external/wamr/patches/ripp_metering.patch +++ b/external/wamr/patches/ripp_metering.patch @@ -102,31 +102,28 @@ index 49a17a96..19a85980 100644 * an index in both functions runtime instance lists * of interpreter mode and aot mode diff --git a/core/iwasm/common/wasm_exec_env.c b/core/iwasm/common/wasm_exec_env.c -index 47752950..d5821c4f 100644 +index 47752950..5f26d886 100644 --- a/core/iwasm/common/wasm_exec_env.c +++ b/core/iwasm/common/wasm_exec_env.c -@@ -86,7 +86,9 @@ wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst, +@@ -86,7 +86,7 @@ wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst, #endif #if WASM_ENABLE_INSTRUCTION_METERING != 0 - exec_env->instructions_to_execute = -1; + exec_env->instructions_to_execute = INT64_MAX; -+ for(int i = 0; i < 256; ++i) -+ exec_env->instructions_schedule[i] = 1; #endif return exec_env; diff --git a/core/iwasm/common/wasm_exec_env.h b/core/iwasm/common/wasm_exec_env.h -index 5d80312f..2713a092 100644 +index 5d80312f..b2ecce2e 100644 --- a/core/iwasm/common/wasm_exec_env.h +++ b/core/iwasm/common/wasm_exec_env.h -@@ -89,7 +89,8 @@ typedef struct WASMExecEnv { +@@ -89,7 +89,7 @@ typedef struct WASMExecEnv { #if WASM_ENABLE_INSTRUCTION_METERING != 0 /* instructions to execute */ - int instructions_to_execute; + int64 instructions_to_execute; -+ int64 instructions_schedule[256]; #endif #if WASM_ENABLE_FAST_JIT != 0 @@ -214,10 +211,10 @@ index 9a6afee1..0fe4739f 100644 bool diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c -index dcee0aea..10c516d6 100644 +index dcee0aea..a1f7866e 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c -@@ -2288,10 +2288,26 @@ wasm_runtime_access_exce_check_guard_page() +@@ -2288,10 +2288,18 @@ wasm_runtime_access_exce_check_guard_page() #if WASM_ENABLE_INSTRUCTION_METERING != 0 void wasm_runtime_set_instruction_count_limit(WASMExecEnv *exec_env, @@ -233,19 +230,11 @@ index dcee0aea..10c516d6 100644 +wasm_runtime_get_instruction_count_limit(WASMExecEnv *exec_env) +{ + return exec_env->instructions_to_execute; -+} -+ -+void -+wasm_runtime_set_instruction_schedule(WASMExecEnv *exec_env, -+ int64 const *instructions_schedule) -+{ -+ for(int i = 0; i < 256; ++i) -+ exec_env->instructions_schedule[i] = instructions_schedule[i]; +} #endif WASMFuncType * -@@ -7348,7 +7364,7 @@ wasm_runtime_is_import_func_linked(const char *module_name, +@@ -7348,7 +7356,7 @@ wasm_runtime_is_import_func_linked(const char *module_name, const char *func_name) { return wasm_native_resolve_symbol(module_name, func_name, NULL, NULL, NULL, @@ -254,7 +243,7 @@ index dcee0aea..10c516d6 100644 } bool -@@ -7805,13 +7821,14 @@ wasm_runtime_get_module_name(wasm_module_t module) +@@ -7805,13 +7813,14 @@ wasm_runtime_get_module_name(wasm_module_t module) bool wasm_runtime_detect_native_stack_overflow(WASMExecEnv *exec_env) { @@ -270,7 +259,7 @@ index dcee0aea..10c516d6 100644 uint32 page_size = os_getpagesize(); uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT; boundary = boundary + page_size * guard_page_count; -@@ -7821,6 +7838,7 @@ wasm_runtime_detect_native_stack_overflow(WASMExecEnv *exec_env) +@@ -7821,6 +7830,7 @@ wasm_runtime_detect_native_stack_overflow(WASMExecEnv *exec_env) "native stack overflow"); return false; } @@ -278,7 +267,7 @@ index dcee0aea..10c516d6 100644 return true; } -@@ -7843,7 +7861,7 @@ wasm_runtime_detect_native_stack_overflow_size(WASMExecEnv *exec_env, +@@ -7843,7 +7853,7 @@ wasm_runtime_detect_native_stack_overflow_size(WASMExecEnv *exec_env, boundary = boundary - WASM_STACK_GUARD_SIZE + requested_size; if ((uint8 *)&boundary < boundary) { wasm_runtime_set_exception(wasm_runtime_get_module_inst(exec_env), @@ -288,10 +277,10 @@ index dcee0aea..10c516d6 100644 } return true; diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h -index 64a6cd79..f4c55e2f 100644 +index 64a6cd79..cb674edc 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h -@@ -795,7 +795,14 @@ wasm_runtime_set_native_stack_boundary(WASMExecEnv *exec_env, +@@ -795,7 +795,10 @@ wasm_runtime_set_native_stack_boundary(WASMExecEnv *exec_env, /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN void wasm_runtime_set_instruction_count_limit(WASMExecEnv *exec_env, @@ -299,10 +288,6 @@ index 64a6cd79..f4c55e2f 100644 + int64 instructions_to_execute); +WASM_RUNTIME_API_EXTERN int64 +wasm_runtime_get_instruction_count_limit(WASMExecEnv *exec_env); -+ -+WASM_RUNTIME_API_EXTERN void -+wasm_runtime_set_instruction_schedule(WASMExecEnv *exec_env, -+ int64 const *instructions_schedule); + #endif @@ -404,7 +389,7 @@ index ddc0b15b..3a707878 100644 #if WASM_ENABLE_TAGS != 0 diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c -index 1e98b0fa..ae24ff8b 100644 +index 1e98b0fa..db6278c5 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -1569,13 +1569,14 @@ get_global_addr(uint8 *global_data, WASMGlobalInstance *global) @@ -420,8 +405,8 @@ index 1e98b0fa..ae24ff8b 100644 - instructions_left--; +#define CHECK_INSTRUCTION_LIMIT() \ + do { \ -+ instructions_left -= instructions_schedule[opcode]; \ -+ if (instructions_left < 0) { \ ++ --instructions_left; \ ++ if (instructions_left <= 0) { \ + wasm_set_exception(module, "instruction limit exceeded"); \ + goto got_exception; \ + } \ @@ -429,20 +414,20 @@ index 1e98b0fa..ae24ff8b 100644 #else #define CHECK_INSTRUCTION_LIMIT() (void)0 #endif -@@ -1625,9 +1626,11 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, +@@ -1625,10 +1626,9 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, uint32 cache_index, type_index, param_cell_num, cell_num; #if WASM_ENABLE_INSTRUCTION_METERING != 0 - int instructions_left = -1; +- if (exec_env) { + int64 instructions_left = INT64_MAX; -+ int64 const *instructions_schedule = NULL; - if (exec_env) { ++ if (exec_env) instructions_left = exec_env->instructions_to_execute; -+ instructions_schedule = exec_env->instructions_schedule; - } +- } #endif -@@ -6885,6 +6888,11 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, + #if WASM_ENABLE_EXCE_HANDLING != 0 +@@ -6885,6 +6885,11 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, FREE_FRAME(exec_env, frame); wasm_exec_env_set_cur_frame(exec_env, prev_frame); @@ -454,7 +439,7 @@ index 1e98b0fa..ae24ff8b 100644 if (!prev_frame->ip) { /* Called from native. */ return; -@@ -6925,6 +6933,12 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, +@@ -6925,6 +6930,12 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, } #endif SYNC_ALL_TO_FRAME(); @@ -468,7 +453,7 @@ index 1e98b0fa..ae24ff8b 100644 #if WASM_ENABLE_LABELS_AS_VALUES == 0 diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c -index 4e5edf41..04c39e1f 100644 +index 4e5edf41..37b36c17 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -106,14 +106,14 @@ typedef float64 CellType_F64; @@ -485,7 +470,7 @@ index 4e5edf41..04c39e1f 100644 - +#define CHECK_INSTRUCTION_LIMIT() \ + do { \ -+ instructions_left -= instructions_schedule[opcode]; \ ++ --instructions_left; \ + if (instructions_left < 0) { \ + wasm_set_exception(module, "instruction limit exceeded"); \ + goto got_exception; \ @@ -532,46 +517,20 @@ index 4e5edf41..04c39e1f 100644 #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */ -@@ -1508,6 +1507,25 @@ get_global_addr(uint8 *global_data, WASMGlobalInstance *global) - #endif - } - -+static int64 const def_instructions_schedule[256] = { -+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, -+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, -+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, -+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, -+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, -+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, -+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, -+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, -+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, -+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, -+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, -+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, -+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, -+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, -+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, -+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 -+}; -+ - static void - wasm_interp_call_func_bytecode(WASMModuleInstance *module, - WASMExecEnv *exec_env, -@@ -1556,9 +1574,11 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, +@@ -1556,10 +1555,9 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, uint8 opcode = 0, local_type, *global_addr; #if WASM_ENABLE_INSTRUCTION_METERING != 0 - int instructions_left = -1; +- if (exec_env) { + int64 instructions_left = INT64_MAX; -+ int64 const *instructions_schedule = def_instructions_schedule; - if (exec_env) { ++ if (exec_env) instructions_left = exec_env->instructions_to_execute; -+ instructions_schedule = exec_env->instructions_schedule; - } +- } #endif #if !defined(OS_ENABLE_HW_BOUND_CHECK) \ -@@ -7694,6 +7714,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, + || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 +@@ -7694,6 +7692,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, { wasm_interp_call_func_native(module, exec_env, cur_func, prev_frame); @@ -579,7 +538,7 @@ index 4e5edf41..04c39e1f 100644 } #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0 -@@ -7806,6 +7827,11 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, +@@ -7806,6 +7805,11 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, FREE_FRAME(exec_env, frame); wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame); @@ -591,7 +550,7 @@ index 4e5edf41..04c39e1f 100644 if (!prev_frame->ip) /* Called from native. */ return; -@@ -7834,6 +7860,10 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, +@@ -7834,6 +7838,10 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, got_exception: SYNC_ALL_TO_FRAME(); diff --git a/include/xrpl/protocol/Protocol.h b/include/xrpl/protocol/Protocol.h index ab95017db1..e4592d4889 100644 --- a/include/xrpl/protocol/Protocol.h +++ b/include/xrpl/protocol/Protocol.h @@ -130,6 +130,9 @@ std::uint8_t constexpr maxAssetCheckDepth = 5; * Wasm code */ std::size_t constexpr maxWasmDataLength = 4 * 1024; +/** The maximum length of a parameters passed from Wasm code*/ +std::size_t constexpr maxWasmParamLength = 1024; + /** A ledger index. */ using LedgerIndex = std::uint32_t; diff --git a/src/test/app/Escrow_test.cpp b/src/test/app/Escrow_test.cpp index 00a5302913..1ade21eb59 100644 --- a/src/test/app/Escrow_test.cpp +++ b/src/test/app/Escrow_test.cpp @@ -2032,8 +2032,8 @@ struct Escrow_test : public beast::unit_test::suite // pub fn finish() -> bool { // unsafe { host_lib::getLedgerSqn() >= 5} // } - static auto wasmHex = ledgerSqnHex; - std::uint32_t constexpr allowance = 4; + auto const& wasmHex = ledgerSqnHex; + std::uint32_t const allowance = 64; { // basic FinishFunction situation @@ -2179,19 +2179,19 @@ struct Escrow_test : public beast::unit_test::suite // finish time hasn't passed, function fails env(escrow::finish(carol, alice, seq), - escrow::comp_allowance(4), + escrow::comp_allowance(allowance), fee(txnFees + 1), ter(tecNO_PERMISSION)); env.close(); // finish time hasn't passed, function succeeds for (; env.now() < ts; env.close()) env(escrow::finish(carol, alice, seq), - escrow::comp_allowance(4), + escrow::comp_allowance(allowance), fee(txnFees + 2), ter(tecNO_PERMISSION)); env(escrow::finish(carol, alice, seq), - escrow::comp_allowance(4), + escrow::comp_allowance(allowance), fee(txnFees + 1), ter(tesSUCCESS)); @@ -2300,8 +2300,7 @@ struct Escrow_test : public beast::unit_test::suite env.require(balance(alice, XRP(4000) - txnFees)); env.require(balance(carol, XRP(5000))); - // TODO: figure out why this can't be 2412 - auto const allowance = 3'600; + auto const allowance = 20'000; // FinishAfter time hasn't passed env(escrow::finish(carol, alice, seq), @@ -2343,7 +2342,7 @@ struct Escrow_test : public beast::unit_test::suite auto const txMeta = env.meta(); if (BEAST_EXPECT(txMeta->isFieldPresent(sfGasUsed))) - BEAST_EXPECT(txMeta->getFieldU32(sfGasUsed) == 487); + BEAST_EXPECT(txMeta->getFieldU32(sfGasUsed) == 10817); env.close(); BEAST_EXPECT((*env.le(alice))[sfOwnerCount] == 0); diff --git a/src/test/app/Wasm_test.cpp b/src/test/app/Wasm_test.cpp index 52d3083561..e5d0a9f29f 100644 --- a/src/test/app/Wasm_test.cpp +++ b/src/test/app/Wasm_test.cpp @@ -1268,7 +1268,7 @@ struct Wasm_test : public beast::unit_test::suite auto re = runEscrowWasm(wasm, funcName, {}, &nfs, 100'000); if (BEAST_EXPECT(re.has_value())) { - BEAST_EXPECT(!re->result && (re->cost == 487)); + BEAST_EXPECT(!re->result && (re->cost == 10817)); // std::cout << "good case result " << re.value().result // << " cost: " << re.value().cost << std::endl; } @@ -1285,7 +1285,7 @@ struct Wasm_test : public beast::unit_test::suite auto re = runEscrowWasm(wasm, funcName, {}, &nfs, 100000); if (BEAST_EXPECT(re.has_value())) { - BEAST_EXPECT(!re->result && (re->cost == 487)); + BEAST_EXPECT(!re->result && (re->cost == 10817)); // std::cout << "bad case (current time < escrow_finish_after " // "time) result " // << re.value().result << " cost: " << @@ -1309,7 +1309,7 @@ struct Wasm_test : public beast::unit_test::suite BadTestHostFunctions nfs(env); std::string funcName("finish"); auto re = runEscrowWasm(wasm, funcName, {}, &nfs, 100000); - BEAST_EXPECT(re.has_value() && !re->result && (re->cost == 23)); + BEAST_EXPECT(re.has_value() && !re->result && (re->cost == 93)); // std::cout << "bad case (access nonexistent field) result " // << re.error() << std::endl; } @@ -1329,7 +1329,7 @@ struct Wasm_test : public beast::unit_test::suite BadTestHostFunctions nfs(env); std::string funcName("finish"); auto re = runEscrowWasm(wasm, funcName, {}, &nfs, 100000); - BEAST_EXPECT(re.has_value() && !re->result && (re->cost == 23)); + BEAST_EXPECT(re.has_value() && !re->result && (re->cost == 93)); // std::cout << "bad case (more than MAX_PAGES) result " // << re.error() << std::endl; } @@ -1406,7 +1406,7 @@ struct Wasm_test : public beast::unit_test::suite auto re = runEscrowWasm(wasm, funcName, {}, &nfs, 100'000); if (BEAST_EXPECT(re.has_value())) { - BEAST_EXPECT(re->result && (re->cost == 80)); + BEAST_EXPECT(re->result && (re->cost == 6570)); // std::cout << "good case result " << re.value().result // << " cost: " << re.value().cost << std::endl; } @@ -1428,13 +1428,78 @@ struct Wasm_test : public beast::unit_test::suite auto re = runEscrowWasm(wasm, funcName, {}, &nfs, 100'000); if (BEAST_EXPECT(re.has_value())) { - BEAST_EXPECT(re->result && (re->cost == 138)); + BEAST_EXPECT(re->result && (re->cost == 16558)); // std::cout << "good case result " << re.value().result // << " cost: " << re.value().cost << std::endl; } } } + void + testHFCost() + { + testcase("wasm test host functions cost"); + + std::string const funcName("finish"); + + using namespace test::jtx; + + Env env(*this); + { + std::string const wasmHex = hostFunctions2Hex; + std::string const wasmStr = boost::algorithm::unhex(wasmHex); + std::vector const wasm(wasmStr.begin(), wasmStr.end()); + + auto& engine = WasmEngine::instance(); + + TestHostFunctions hfs(env, 0); + std::vector imp = createWasmImport(&hfs); + for (auto& i : imp) + i.gas = 0; + + auto re = engine.run( + wasm, funcName, {}, imp, &hfs, 1000'000, env.journal); + + if (BEAST_EXPECT(re.has_value())) + { + // std::cout << ", ret: " << re->result + // << ", gas spent: " << re->cost << std::endl; + BEAST_EXPECT(re->result && (re->cost == 138)); + } + + env.close(); + } + + env.close(); + env.close(); + env.close(); + env.close(); + env.close(); + + { + std::string const wasmHex = hostFunctions2Hex; + std::string const wasmStr = boost::algorithm::unhex(wasmHex); + std::vector const wasm(wasmStr.begin(), wasmStr.end()); + + auto& engine = WasmEngine::instance(); + + TestHostFunctions hfs(env, 0); + std::vector const imp = createWasmImport(&hfs); + + auto re = engine.run( + wasm, funcName, {}, imp, &hfs, 1000'000, env.journal); + + if (BEAST_EXPECT(re.has_value())) + { + // std::cout << ", ret: " << re->result + // << ", gas spent: " << re->cost << std::endl; + BEAST_EXPECT(re->result && (re->cost == 16558)); + } + + env.close(); + } + } + void perfTest() { @@ -1551,6 +1616,8 @@ struct Wasm_test : public beast::unit_test::suite // testWasmSP1Verifier(); testWasmBG16Verifier(); + testHFCost(); + // TODO: fix result testEscrowWasmDN1(); testEscrowWasmDN2(); diff --git a/src/xrpld/app/misc/WasmHostFuncWrapper.cpp b/src/xrpld/app/misc/WasmHostFuncWrapper.cpp index 0e1f886259..0c50273362 100644 --- a/src/xrpld/app/misc/WasmHostFuncWrapper.cpp +++ b/src/xrpld/app/misc/WasmHostFuncWrapper.cpp @@ -527,6 +527,11 @@ computeSha512HalfHash_wrap( auto* hf = reinterpret_cast(env); auto const* rt = reinterpret_cast(hf->getRT()); + if (params->data[1].of.i32 > maxWasmDataLength) + { + RET(HF_ERR_DATA_FIELD_TOO_LARGE); + } + auto const r = getData(rt, params->data[0].of.i32, params->data[1].of.i32); if (!r) { @@ -726,6 +731,11 @@ trace_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results) auto* hf = reinterpret_cast(env); auto const* rt = reinterpret_cast(hf->getRT()); + if (params->data[1].of.i32 + params->data[3].of.i32 > maxWasmDataLength) + { + RET(HF_ERR_DATA_FIELD_TOO_LARGE); + } + auto const msg = getDataString(rt, params->data[0].of.i32, params->data[1].of.i32); if (!msg) @@ -750,6 +760,11 @@ traceNum_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results) auto* hf = reinterpret_cast(env); auto const* rt = reinterpret_cast(hf->getRT()); + if (params->data[1].of.i32 > maxWasmDataLength) + { + RET(HF_ERR_DATA_FIELD_TOO_LARGE); + } + auto const msg = getDataString(rt, params->data[0].of.i32, params->data[1].of.i32); if (!msg) diff --git a/src/xrpld/app/misc/WasmVM.cpp b/src/xrpld/app/misc/WasmVM.cpp index b21007b9d8..3f7d61c349 100644 --- a/src/xrpld/app/misc/WasmVM.cpp +++ b/src/xrpld/app/misc/WasmVM.cpp @@ -29,75 +29,49 @@ namespace ripple { -static std::vector -createImports(HostFunctions* hfs) +std::vector +createWasmImport(HostFunctions* hfs) { - std::vector imports; + std::vector i; if (hfs) { - // TODO: remove after escrow_test wasm module will be updated - WASM_IMPORT_FUNC2(imports, getLedgerSqnOld, "getLedgerSqn", hfs); + // clang-format off - WASM_IMPORT_FUNC2(imports, getLedgerSqn, "get_ledger_sqn", hfs); - WASM_IMPORT_FUNC2( - imports, getParentLedgerTime, "get_parent_ledger_time", hfs); - WASM_IMPORT_FUNC2( - imports, getParentLedgerHash, "get_parent_ledger_hash", hfs); - WASM_IMPORT_FUNC2(imports, cacheLedgerObj, "cache_ledger_obj", hfs); - WASM_IMPORT_FUNC2(imports, getTxField, "get_tx_field", hfs); - WASM_IMPORT_FUNC2( - imports, - getCurrentLedgerObjField, - "get_current_ledger_obj_field", - hfs); - WASM_IMPORT_FUNC2( - imports, getLedgerObjField, "get_ledger_obj_field", hfs); - WASM_IMPORT_FUNC2( - imports, getTxNestedField, "get_tx_nested_field", hfs); - WASM_IMPORT_FUNC2( - imports, - getCurrentLedgerObjNestedField, - "get_current_ledger_obj_nested_field", - hfs); - WASM_IMPORT_FUNC2( - imports, - getLedgerObjNestedField, - "get_ledger_obj_nested_field", - hfs); - WASM_IMPORT_FUNC2(imports, getTxArrayLen, "get_tx_array_len", hfs); - WASM_IMPORT_FUNC2( - imports, - getCurrentLedgerObjArrayLen, - "get_current_ledger_obj_array_len", - hfs); - WASM_IMPORT_FUNC2( - imports, getLedgerObjArrayLen, "get_ledger_obj_array_len", hfs); - WASM_IMPORT_FUNC2( - imports, getTxNestedArrayLen, "get_tx_nested_array_len", hfs); - WASM_IMPORT_FUNC2( - imports, - getCurrentLedgerObjNestedArrayLen, - "get_current_ledger_obj_nested_array_len", - hfs); - WASM_IMPORT_FUNC2( - imports, - getLedgerObjNestedArrayLen, - "get_ledger_obj_nested_array_len", - hfs); - WASM_IMPORT_FUNC2(imports, updateData, "update_data", hfs); - WASM_IMPORT_FUNC2( - imports, computeSha512HalfHash, "compute_sha512_half", hfs); - WASM_IMPORT_FUNC2(imports, accountKeylet, "account_keylet", hfs); - WASM_IMPORT_FUNC2(imports, credentialKeylet, "credential_keylet", hfs); - WASM_IMPORT_FUNC2(imports, escrowKeylet, "escrow_keylet", hfs); - WASM_IMPORT_FUNC2(imports, oracleKeylet, "oracle_keylet", hfs); - WASM_IMPORT_FUNC2(imports, getNFT, "get_NFT", hfs); - WASM_IMPORT_FUNC(imports, trace, hfs); - WASM_IMPORT_FUNC2(imports, traceNum, "trace_num", hfs); + // TODO: remove after escrow_test wasm module will be updated + WASM_IMPORT_FUNC2(i, getLedgerSqnOld, "getLedgerSqn", hfs, 60); + + + WASM_IMPORT_FUNC2(i, getLedgerSqn, "get_ledger_sqn", hfs, 60); + WASM_IMPORT_FUNC2(i, getParentLedgerTime, "get_parent_ledger_time", hfs, 60); + WASM_IMPORT_FUNC2(i, getParentLedgerHash, "get_parent_ledger_hash", hfs, 60); + WASM_IMPORT_FUNC2(i, cacheLedgerObj, "cache_ledger_obj", hfs, 5000); + WASM_IMPORT_FUNC2(i, getTxField, "get_tx_field", hfs, 70); + WASM_IMPORT_FUNC2(i, getCurrentLedgerObjField, "get_current_ledger_obj_field", hfs, 70); + WASM_IMPORT_FUNC2(i, getLedgerObjField, "get_ledger_obj_field", hfs, 70); + WASM_IMPORT_FUNC2(i, getTxNestedField, "get_tx_nested_field", hfs, 110); + WASM_IMPORT_FUNC2(i, getCurrentLedgerObjNestedField, "get_current_ledger_obj_nested_field", hfs, 110); + WASM_IMPORT_FUNC2(i, getLedgerObjNestedField, "get_ledger_obj_nested_field", hfs, 110); + WASM_IMPORT_FUNC2(i, getTxArrayLen, "get_tx_array_len", hfs, 40); + WASM_IMPORT_FUNC2(i, getCurrentLedgerObjArrayLen, "get_current_ledger_obj_array_len", hfs, 40); + WASM_IMPORT_FUNC2(i, getLedgerObjArrayLen, "get_ledger_obj_array_len", hfs, 40); + WASM_IMPORT_FUNC2(i, getTxNestedArrayLen, "get_tx_nested_array_len", hfs, 70); + WASM_IMPORT_FUNC2(i, getCurrentLedgerObjNestedArrayLen, "get_current_ledger_obj_nested_array_len", hfs, 70); + WASM_IMPORT_FUNC2(i, getLedgerObjNestedArrayLen, "get_ledger_obj_nested_array_len", hfs, 70); + WASM_IMPORT_FUNC2(i, updateData, "update_data", hfs, 1000); + WASM_IMPORT_FUNC2(i, computeSha512HalfHash, "compute_sha512_half", hfs, 2000); + WASM_IMPORT_FUNC2(i, accountKeylet, "account_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, credentialKeylet, "credential_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, escrowKeylet, "escrow_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, oracleKeylet, "oracle_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, getNFT, "get_NFT", hfs, 1000); + WASM_IMPORT_FUNC (i, trace, hfs, 500); + WASM_IMPORT_FUNC2(i, traceNum, "trace_num", hfs, 500); + + // clang-format on } - return imports; + return i; } Expected @@ -117,7 +91,7 @@ runEscrowWasm( wasmCode, funcName, params, - createImports(hfs), + createWasmImport(hfs), hfs, gasLimit, hfs ? hfs->getJournal() : j); @@ -125,14 +99,14 @@ runEscrowWasm( // std::cout << "runEscrowWasm, mod size: " << wasmCode.size() // << ", gasLimit: " << gasLimit << ", funcName: " << funcName; - if (!ret.has_value()) + if (!ret) { // std::cout << ", error: " << ret.error() << std::endl; return Unexpected(ret.error()); } - // std::cout << ", ret: " << ret->result << ", gas spent: " << ret->cost - // << std::endl; + // std::cout << ", ret: " << ret->result << ", gas spent: " << ret->cost << + // std::endl; return EscrowResult{ret->result > 0, ret->cost}; } @@ -152,7 +126,7 @@ preflightEscrowWasm( wasmCode, funcName, params, - createImports(hfs), + createWasmImport(hfs), hfs ? hfs->getJournal() : j); return ret; diff --git a/src/xrpld/app/misc/WasmVM.h b/src/xrpld/app/misc/WasmVM.h index 7c68a18696..c4db2fedd5 100644 --- a/src/xrpld/app/misc/WasmVM.h +++ b/src/xrpld/app/misc/WasmVM.h @@ -86,6 +86,9 @@ public: //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +std::vector +createWasmImport(HostFunctions* hfs); + Expected runEscrowWasm( Bytes const& wasmCode,