Skip to content

Commit

Permalink
Change toWasmMemorySized to NOT take a size argument
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Aug 15, 2022
1 parent 9b15cfc commit 4316030
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
10 changes: 3 additions & 7 deletions lib/runtime/wasmer/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 11 additions & 16 deletions lib/runtime/wasmer/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down

0 comments on commit 4316030

Please sign in to comment.