From 35f5986fbfdaf480463808037be60173b33e6fc4 Mon Sep 17 00:00:00 2001 From: Jacob Gadikian Date: Tue, 24 Dec 2024 11:56:17 +0700 Subject: [PATCH] improve debug --- internal/runtime/hostfunctions.go | 32 ++++++++++++++++--------------- lib_libwasmvm_test.go | 2 +- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/internal/runtime/hostfunctions.go b/internal/runtime/hostfunctions.go index 22bce83e5..4624cfd7c 100644 --- a/internal/runtime/hostfunctions.go +++ b/internal/runtime/hostfunctions.go @@ -713,22 +713,16 @@ func hostEd25519BatchVerify(ctx context.Context, mod api.Module, msgs_ptr, sigs_ // hostDebug implements debug func hostDebug(ctx context.Context, mod api.Module, msgPtr uint32) { mem := mod.Memory() - - // Read message from memory (null-terminated string) - var msg []byte - offset := msgPtr - for { - // Read one byte at a time - b, err := readMemory(mem, offset, 1) - if err != nil || len(b) == 0 || b[0] == 0 { - break - } - msg = append(msg, b[0]) - offset++ + msg, err := readMemory(mem, msgPtr, 1024) // Read up to 1024 bytes + if err != nil { + return } - - // Print debug message - fmt.Printf("Debug: %s\n", string(msg)) + // Find null terminator + length := 0 + for length < len(msg) && msg[length] != 0 { + length++ + } + fmt.Printf("Debug: %s\n", string(msg[:length])) } // hostQueryChain implements query_chain with signature (req_ptr i32) -> i32 @@ -829,6 +823,14 @@ func RegisterHostFunctions(runtime wazero.Runtime, env *RuntimeEnvironment) (waz WithParameterNames("key_ptr", "key_len", "val_ptr", "val_len"). Export("db_set") + builder.NewFunctionBuilder(). + WithFunc(func(ctx context.Context, m api.Module, keyPtr, keyLen, valPtr, valLen uint32) { + ctx = context.WithValue(ctx, envKey, env) + hostSet(ctx, m, keyPtr, keyLen, valPtr, valLen) + }). + WithParameterNames("key_ptr", "key_len", "val_ptr", "val_len"). + Export("db_write") + // Register interface_version_8 function builder.NewFunctionBuilder(). WithFunc(func(ctx context.Context, m api.Module) { diff --git a/lib_libwasmvm_test.go b/lib_libwasmvm_test.go index 1651960c6..704de4323 100644 --- a/lib_libwasmvm_test.go +++ b/lib_libwasmvm_test.go @@ -16,7 +16,7 @@ import ( const ( TESTING_PRINT_DEBUG = false TESTING_GAS_LIMIT = uint64(500_000_000_000) // ~0.5ms - TESTING_MEMORY_LIMIT = 32 // MiB + TESTING_MEMORY_LIMIT = 64 // MiB TESTING_CACHE_SIZE = 100 // MiB )