Skip to content

Commit 6ac6b6b

Browse files
authored
Merge pull request WebAssembly#34 from dhil/wasmfx-merge
Merge with WebAssembly/main and wasm-3.0 branch.
2 parents 9b85d1e + 8d8b835 commit 6ac6b6b

File tree

5 files changed

+144
-19
lines changed

5 files changed

+144
-19
lines changed

.github/workflows/mirror-to-master.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

document/core/binary/modules.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ It decodes into a vector of :ref:`element segments <syntax-elem>` that represent
378378
379379
.. note::
380380
The initial integer can be interpreted as a bitfield.
381-
Bit 0 indicates a passive or declarative segment,
381+
Bit 0 distinguishes a passive or declarative segment from an active segment,
382382
bit 1 indicates the presence of an explicit table index for an active segment and otherwise distinguishes passive from declarative segments,
383383
bit 2 indicates the use of element type and element :ref:`expressions <binary-expr>` instead of element kind and element indices.
384384

interpreter/binary/decode.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ let memop s =
327327
let has_var = Int32.logand flags 0x40l <> 0l in
328328
let x = if has_var then at var s else Source.(0l @@ no_region) in
329329
let align = Int32.(to_int (logand flags 0x3fl)) in
330+
require (align < 32) s pos "malformed memop alignment";
330331
let offset = u32 s in
331332
x, align, offset
332333

test/core/align.wast

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,3 +864,120 @@
864864
(assert_trap (invoke "store" (i32.const 65532) (i64.const -1)) "out of bounds memory access")
865865
;; No memory was changed
866866
(assert_return (invoke "load" (i32.const 65532)) (i32.const 0))
867+
868+
;; Test invalid alignment values that may cause overflow when parsed.
869+
;; These use the binary format, because it stores alignment as a base-2 exponent.
870+
871+
;; Signed 32-bit overflow
872+
(assert_invalid
873+
(module binary
874+
"\00asm" "\01\00\00\00"
875+
"\01\04\01\60\00\00" ;; Type section: 1 type
876+
"\03\02\01\00" ;; Function section: 1 function
877+
"\05\03\01\00\01" ;; Memory section: 1 memory
878+
"\0a\0a\01" ;; Code section: 1 function
879+
880+
;; function 0
881+
"\08\00"
882+
"\41\00" ;; i32.const 0
883+
"\28\1f\00" ;; i32.load offset=0 align=2**31
884+
"\1a" ;; drop
885+
"\0b" ;; end
886+
)
887+
"alignment must not be larger than natural"
888+
)
889+
890+
;; Unsigned 32-bit overflow
891+
(assert_malformed
892+
(module binary
893+
"\00asm" "\01\00\00\00"
894+
"\01\04\01\60\00\00" ;; Type section: 1 type
895+
"\03\02\01\00" ;; Function section: 1 function
896+
"\05\03\01\00\01" ;; Memory section: 1 memory
897+
"\0a\0a\01" ;; Code section: 1 function
898+
899+
;; function 0
900+
"\08\00"
901+
"\41\00" ;; i32.const 0
902+
"\28\20\00" ;; i32.load offset=0 align=2**32
903+
"\1a" ;; drop
904+
"\0b" ;; end
905+
)
906+
"malformed memop alignment"
907+
)
908+
909+
;; 32-bit out of range
910+
(assert_malformed
911+
(module binary
912+
"\00asm" "\01\00\00\00"
913+
"\01\04\01\60\00\00" ;; Type section: 1 type
914+
"\03\02\01\00" ;; Function section: 1 function
915+
"\05\03\01\00\01" ;; Memory section: 1 memory
916+
"\0a\0a\01" ;; Code section: 1 function
917+
918+
;; function 0
919+
"\08\00"
920+
"\41\00" ;; i32.const 0
921+
"\28\21\00" ;; i32.load offset=0 align=2**33
922+
"\1a" ;; drop
923+
"\0b" ;; end
924+
)
925+
"malformed memop alignment"
926+
)
927+
928+
;; Signed 64-bit overflow
929+
(assert_malformed
930+
(module binary
931+
"\00asm" "\01\00\00\00"
932+
"\01\04\01\60\00\00" ;; Type section: 1 type
933+
"\03\02\01\00" ;; Function section: 1 function
934+
"\05\03\01\00\01" ;; Memory section: 1 memory
935+
"\0a\0a\01" ;; Code section: 1 function
936+
937+
;; function 0
938+
"\08\00"
939+
"\41\00" ;; i32.const 0
940+
"\28\3f\00" ;; i32.load offset=0 align=2**63
941+
"\1a" ;; drop
942+
"\0b" ;; end
943+
)
944+
"malformed memop alignment"
945+
)
946+
947+
;; Unsigned 64-bit overflow
948+
(assert_invalid
949+
(module binary
950+
"\00asm" "\01\00\00\00"
951+
"\01\04\01\60\00\00" ;; Type section: 1 type
952+
"\03\02\01\00" ;; Function section: 1 function
953+
"\05\03\01\00\01" ;; Memory section: 1 memory
954+
"\0a\0a\01" ;; Code section: 1 function
955+
956+
;; function 0
957+
"\08\00"
958+
"\41\00" ;; i32.const 0
959+
"\28\40\00" ;; i32.load offset=0 align=2**64 (parsed as align=0, memidx present)
960+
"\1a" ;; drop
961+
"\0b" ;; end
962+
)
963+
"type mismatch"
964+
)
965+
966+
;; 64-bit out of range
967+
(assert_invalid
968+
(module binary
969+
"\00asm" "\01\00\00\00"
970+
"\01\04\01\60\00\00" ;; Type section: 1 type
971+
"\03\02\01\00" ;; Function section: 1 function
972+
"\05\03\01\00\01" ;; Memory section: 1 memory
973+
"\0a\0a\01" ;; Code section: 1 function
974+
975+
;; function 0
976+
"\08\00"
977+
"\41\00" ;; i32.const 0
978+
"\28\41\00" ;; i32.load offset=0 align=2**65 (parsed as align=1, memidx present)
979+
"\1a" ;; drop
980+
"\0b" ;; end
981+
)
982+
"type mismatch"
983+
)

test/core/memory.wast

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,28 @@
237237
"(import \"\" \"\" (memory $foo 1))"
238238
"(import \"\" \"\" (memory $foo 1))")
239239
"duplicate memory")
240+
241+
;; Test that exporting random globals does not change a memory's semantics.
242+
243+
(module
244+
(memory (export "memory") 1 1)
245+
246+
;; These should not change the behavior of memory accesses.
247+
(global (export "__data_end") i32 (i32.const 10000))
248+
(global (export "__stack_top") i32 (i32.const 10000))
249+
(global (export "__heap_base") i32 (i32.const 10000))
250+
251+
(func (export "load") (param i32) (result i32)
252+
(i32.load8_u (local.get 0))
253+
)
254+
)
255+
256+
;; None of these memory accesses should trap.
257+
(assert_return (invoke "load" (i32.const 0)) (i32.const 0))
258+
(assert_return (invoke "load" (i32.const 10000)) (i32.const 0))
259+
(assert_return (invoke "load" (i32.const 20000)) (i32.const 0))
260+
(assert_return (invoke "load" (i32.const 30000)) (i32.const 0))
261+
(assert_return (invoke "load" (i32.const 40000)) (i32.const 0))
262+
(assert_return (invoke "load" (i32.const 50000)) (i32.const 0))
263+
(assert_return (invoke "load" (i32.const 60000)) (i32.const 0))
264+
(assert_return (invoke "load" (i32.const 65535)) (i32.const 0))

0 commit comments

Comments
 (0)