From 38eb7919f8cd5623deb85c34cf27b6b766cd38bd Mon Sep 17 00:00:00 2001 From: Oscar Spencer Date: Wed, 21 Feb 2024 12:25:10 -0600 Subject: [PATCH 1/3] fix(stdlib): Pad strings/bytes marshal representation with zeros --- compiler/test/stdlib/marshal.test.gr | 2 ++ stdlib/marshal.gr | 3 +++ 2 files changed, 5 insertions(+) diff --git a/compiler/test/stdlib/marshal.test.gr b/compiler/test/stdlib/marshal.test.gr index bc5242acb..c75b125b9 100644 --- a/compiler/test/stdlib/marshal.test.gr +++ b/compiler/test/stdlib/marshal.test.gr @@ -91,3 +91,5 @@ let truncatedRecord = Bytes.slice( ) ) assert Result.isErr(unmarshal(truncatedRecord)) + +assert Marshal.marshal("🌾") == Marshal.marshal("🌾") diff --git a/stdlib/marshal.gr b/stdlib/marshal.gr index 4ee22f817..b2be56d42 100644 --- a/stdlib/marshal.gr +++ b/stdlib/marshal.gr @@ -24,6 +24,7 @@ module Marshal include "runtime/unsafe/wasmi32" use WasmI32.{ (+), + (-), (*), (&), (==), @@ -196,6 +197,7 @@ let rec marshalHeap = (heapPtr, buf, offset, valuesSeen) => { t when t == Tags._GRAIN_STRING_HEAP_TAG || t == Tags._GRAIN_BYTES_HEAP_TAG => { let size = 8n + load(heapPtr, 4n) Memory.copy(buf + offset, heapPtr, size) + Memory.fill(buf + offset + size, 0n, roundTo8(size) - size) roundTo8(offset + size) }, t when t == Tags._GRAIN_ADT_HEAP_TAG => { @@ -426,6 +428,7 @@ provide let marshal = value => { let valuePtr = fromGrain(value) let buf = allocateBytes(size(valuePtr)) marshal(valuePtr, buf + 8n) + ignore(value) toGrain(buf): Bytes } From 09d3456ba5b0fa3788c86056f5d33633baff21a9 Mon Sep 17 00:00:00 2001 From: Oscar Spencer Date: Sat, 2 Mar 2024 09:20:37 -0600 Subject: [PATCH 2/3] clear buffer before marshaling --- stdlib/marshal.gr | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/stdlib/marshal.gr b/stdlib/marshal.gr index 683aad09d..d414b8f22 100644 --- a/stdlib/marshal.gr +++ b/stdlib/marshal.gr @@ -196,7 +196,6 @@ let rec marshalHeap = (heapPtr, buf, offset, valuesSeen) => { t when t == Tags._GRAIN_STRING_HEAP_TAG || t == Tags._GRAIN_BYTES_HEAP_TAG => { let size = 8n + load(heapPtr, 4n) Memory.copy(buf + offset, heapPtr, size) - Memory.fill(buf + offset + size, 0n, roundTo8(size) - size) roundTo8(offset + size) }, t when t == Tags._GRAIN_ADT_HEAP_TAG => { @@ -425,7 +424,9 @@ let marshal = (value, buf) => { @unsafe provide let marshal = value => { let valuePtr = fromGrain(value) - let buf = allocateBytes(size(valuePtr)) + let size = size(valuePtr) + let buf = allocateBytes(size) + Memory.fill(buf + 8n, 0n, size) marshal(valuePtr, buf + 8n) ignore(value) toGrain(buf): Bytes From ff76c39837c7f7248f2c968d0917505c21f86d01 Mon Sep 17 00:00:00 2001 From: Oscar Spencer Date: Sat, 2 Mar 2024 09:22:48 -0600 Subject: [PATCH 3/3] remove unused import --- stdlib/marshal.gr | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/marshal.gr b/stdlib/marshal.gr index d414b8f22..8c088a1f8 100644 --- a/stdlib/marshal.gr +++ b/stdlib/marshal.gr @@ -24,7 +24,6 @@ module Marshal from "runtime/unsafe/wasmi32" include WasmI32 use WasmI32.{ (+), - (-), (*), (&), (==),