From 4316030908f68843ad23594530dba1801d2faf46 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Fri, 12 Aug 2022 21:58:08 +0000 Subject: [PATCH] Change `toWasmMemorySized` to NOT take a size argument --- lib/runtime/wasmer/helpers.go | 10 +++------- lib/runtime/wasmer/imports.go | 27 +++++++++++---------------- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/lib/runtime/wasmer/helpers.go b/lib/runtime/wasmer/helpers.go index ea894fc8d1c..54bfaa8d61b 100644 --- a/lib/runtime/wasmer/helpers.go +++ b/lib/runtime/wasmer/helpers.go @@ -47,16 +47,12 @@ func toWasmMemory(context wasmer.InstanceContext, data []byte) ( } // toWasmMemorySized copies a Go byte slice to wasm memory and returns the corresponding -// 32 bit pointer. -func toWasmMemorySized(context wasmer.InstanceContext, data []byte, size uint32) ( +// 32 bit pointer. Note the data must have a well known fixed length in the runtime. +func toWasmMemorySized(context wasmer.InstanceContext, data []byte) ( pointer uint32, err error) { - if int(size) != len(data) { - // Programming error - panic(fmt.Sprintf("data is %d bytes but size specified is %d", len(data), size)) - } - allocator := context.Data().(*runtime.Context).Allocator + size := uint32(len(data)) ptr, err := allocator.Allocate(size) if err != nil { return 0, fmt.Errorf("allocating: %w", err) diff --git a/lib/runtime/wasmer/imports.go b/lib/runtime/wasmer/imports.go index 9acf0b92e12..62d764a0f1b 100644 --- a/lib/runtime/wasmer/imports.go +++ b/lib/runtime/wasmer/imports.go @@ -257,7 +257,7 @@ func ext_crypto_ed25519_generate_version_1(context unsafe.Pointer, keyTypeID C.i return 0 } - ret, err := toWasmMemorySized(instanceContext, kp.Public().Encode(), 32) + ret, err := toWasmMemorySized(instanceContext, kp.Public().Encode()) if err != nil { logger.Warnf("failed to allocate memory: %s", err) return 0 @@ -576,7 +576,7 @@ func ext_crypto_sr25519_generate_version_1(context unsafe.Pointer, keyTypeID C.i return 0 } - ret, err := toWasmMemorySized(instanceContext, kp.Public().Encode(), 32) + ret, err := toWasmMemorySized(instanceContext, kp.Public().Encode()) if err != nil { logger.Errorf("failed to allocate memory: %s", err) return 0 @@ -1318,7 +1318,7 @@ func ext_hashing_blake2_128_version_1(context unsafe.Pointer, dataSpan C.int64_t "data 0x%x has hash 0x%x", data, hash) - out, err := toWasmMemorySized(instanceContext, hash, 16) + out, err := toWasmMemorySized(instanceContext, hash) if err != nil { logger.Errorf("failed to allocate: %s", err) return 0 @@ -1342,7 +1342,7 @@ func ext_hashing_blake2_256_version_1(context unsafe.Pointer, dataSpan C.int64_t logger.Debugf("data 0x%x has hash %s", data, hash) - out, err := toWasmMemorySized(instanceContext, hash[:], 32) + out, err := toWasmMemorySized(instanceContext, hash[:]) if err != nil { logger.Errorf("failed to allocate: %s", err) return 0 @@ -1366,7 +1366,7 @@ func ext_hashing_keccak_256_version_1(context unsafe.Pointer, dataSpan C.int64_t logger.Debugf("data 0x%x has hash %s", data, hash) - out, err := toWasmMemorySized(instanceContext, hash[:], 32) + out, err := toWasmMemorySized(instanceContext, hash[:]) if err != nil { logger.Errorf("failed to allocate: %s", err) return 0 @@ -1385,7 +1385,7 @@ func ext_hashing_sha2_256_version_1(context unsafe.Pointer, dataSpan C.int64_t) logger.Debugf("data 0x%x has hash %s", data, hash) - out, err := toWasmMemorySized(instanceContext, hash[:], 32) + out, err := toWasmMemorySized(instanceContext, hash[:]) if err != nil { logger.Errorf("failed to allocate: %s", err) return 0 @@ -1409,7 +1409,7 @@ func ext_hashing_twox_256_version_1(context unsafe.Pointer, dataSpan C.int64_t) logger.Debugf("data 0x%x has hash %s", data, hash) - out, err := toWasmMemorySized(instanceContext, hash[:], 32) + out, err := toWasmMemorySized(instanceContext, hash[:]) if err != nil { logger.Errorf("failed to allocate: %s", err) return 0 @@ -1434,7 +1434,7 @@ func ext_hashing_twox_128_version_1(context unsafe.Pointer, dataSpan C.int64_t) "data 0x%x hash hash 0x%x", data, hash) - out, err := toWasmMemorySized(instanceContext, hash, 16) + out, err := toWasmMemorySized(instanceContext, hash) if err != nil { logger.Errorf("failed to allocate: %s", err) return 0 @@ -1460,7 +1460,7 @@ func ext_hashing_twox_64_version_1(context unsafe.Pointer, dataSpan C.int64_t) C "data 0x%x has hash 0x%x", data, hash) - out, err := toWasmMemorySized(instanceContext, hash, 8) + out, err := toWasmMemorySized(instanceContext, hash) if err != nil { logger.Errorf("failed to allocate: %s", err) return 0 @@ -1632,13 +1632,8 @@ func ext_offchain_network_state_version_1(context unsafe.Pointer) C.int64_t { return 0 } - // copy network state length to memory writtenOut location - nsEncLen := uint32(len(nsEnc)) - buf := make([]byte, 4) - binary.LittleEndian.PutUint32(buf, nsEncLen) - // allocate memory for value and copy value to memory - ptr, err := toWasmMemorySized(instanceContext, nsEnc, nsEncLen) + ptr, err := toWasmMemorySized(instanceContext, nsEnc) if err != nil { logger.Errorf("failed to allocate memory: %s", err) return 0 @@ -1657,7 +1652,7 @@ func ext_offchain_random_seed_version_1(context unsafe.Pointer) C.int32_t { if err != nil { logger.Errorf("failed to generate random seed: %s", err) } - ptr, err := toWasmMemorySized(instanceContext, seed, 32) + ptr, err := toWasmMemorySized(instanceContext, seed) if err != nil { logger.Errorf("failed to allocate memory: %s", err) }