From e14373756f18ba2e0a434747bdfb40d14e08d781 Mon Sep 17 00:00:00 2001 From: Richard Holland Date: Tue, 18 Jan 2022 10:33:00 +0000 Subject: [PATCH] working! --- src/ripple/app/tx/applyHook.h | 35 ++++++++++++++++++++---------- src/ripple/app/tx/applyHookMacro.h | 3 ++- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/ripple/app/tx/applyHook.h b/src/ripple/app/tx/applyHook.h index 1f8c64b41..66a88da4f 100644 --- a/src/ripple/app/tx/applyHook.h +++ b/src/ripple/app/tx/applyHook.h @@ -12,9 +12,8 @@ #include #include #include -#include #include - +#include namespace hook { @@ -408,7 +407,10 @@ namespace hook static WasmEdge_String cbakFunctionName = WasmEdge_StringCreateByCString("cbak"); static WasmEdge_String hookFunctionName = WasmEdge_StringCreateByCString("hook"); - /** + // see: lib/system/allocator.cpp + #define WasmEdge_kPageSize 65536ULL + + /** * HookExecutor is effectively a two-part function: * The first part sets up the Hook Api inside the wasm import, ready for use * (this is done during object construction.) @@ -428,7 +430,7 @@ namespace hook WasmEdge_ImportObjectContext* importObj; /** - * Validate that a web assembly blob can be loaded by wasmedge + * Validate that a web assembly blob can be loaded by wasmedge */ static std::optional validateWasm(const void* wasm, size_t len) { @@ -483,6 +485,21 @@ namespace hook WasmEdge_Value params[1] = { WasmEdge_ValueGenI32((int64_t)wasmParam) }; WasmEdge_Value returns[1]; + /* + printf("executing hook wasm:\n"); + for (int j = 0; j < len; j++) + { + if (j % 16 == 0) + printf("0x%08X:\t", j); + + printf("%02X%s", (reinterpret_cast(wasm))[j], + (j % 16 == 15 ? "\n" : + (j % 4 == 3 ? " " : + (j % 2 == 1 ? " " : "")))); + } + printf("\n----\n"); + */ + res = WasmEdge_VMRunWasmFromBuffer(vmCtx, reinterpret_cast(wasm), len, callback ? cbakFunctionName : hookFunctionName, @@ -500,14 +517,6 @@ namespace hook auto* statsCtx= WasmEdge_VMGetStatisticsContext(vmCtx); hookCtx.result.instructionCount = WasmEdge_StatisticsGetInstrCount(statsCtx); - - JLOG(j.trace()) - << "HookInfo[" << HC_ACC() << "]: " - << (hookCtx.result.exitType == hook_api::ExitType::ROLLBACK ? "ROLLBACK" : "ACCEPT") - << " RS: '" << hookCtx.result.exitReason.c_str() - << "' RC: " << hookCtx.result.exitCode; - - //WasmEdge_ValueGetI64(returns[0])); } } @@ -521,6 +530,8 @@ namespace hook { ctx.module = this; + WasmEdge_LogSetDebugLevel(); + ADD_HOOK_FUNCTION(_g, ctx); ADD_HOOK_FUNCTION(accept, ctx); ADD_HOOK_FUNCTION(rollback, ctx); diff --git a/src/ripple/app/tx/applyHookMacro.h b/src/ripple/app/tx/applyHookMacro.h index bb5eb9007..97f5b1b1f 100644 --- a/src/ripple/app/tx/applyHookMacro.h +++ b/src/ripple/app/tx/applyHookMacro.h @@ -145,7 +145,8 @@ [[maybe_unused]] auto& view = applyCtx.view();\ [[maybe_unused]] auto j = applyCtx.app.journal("View");\ [[maybe_unused]] unsigned char* memory = WasmEdge_MemoryInstanceGetPointer(&memoryCtx, 0, 0);\ - [[maybe_unused]] const uint64_t memory_length = WasmEdge_MemoryInstanceGetPageSize(&memoryCtx); + [[maybe_unused]] const uint64_t memory_length = WasmEdge_MemoryInstanceGetPageSize(&memoryCtx) * \ + WasmEdge_kPageSize; #define WRITE_WASM_MEMORY(bytes_written, guest_dst_ptr, guest_dst_len,\ host_src_ptr, host_src_len, host_memory_ptr, guest_memory_length)\