Skip to content

Update decoding of element/data segments in spec interpreter #1440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions interpreter/binary/decode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -951,41 +951,41 @@ let elem_kind s =
| _ -> error s (pos s - 1) "malformed element kind"

let elem s =
match vu32 s with
| 0x00l ->
match u8 s with
| 0x00 ->
let emode = at active_zero s in
let einit = vec (at elem_index) s in
{etype = FuncRefType; einit; emode}
| 0x01l ->
| 0x01 ->
let emode = at passive s in
let etype = elem_kind s in
let einit = vec (at elem_index) s in
{etype; einit; emode}
| 0x02l ->
| 0x02 ->
let emode = at active s in
let etype = elem_kind s in
let einit = vec (at elem_index) s in
{etype; einit; emode}
| 0x03l ->
| 0x03 ->
let emode = at declarative s in
let etype = elem_kind s in
let einit = vec (at elem_index) s in
{etype; einit; emode}
| 0x04l ->
| 0x04 ->
let emode = at active_zero s in
let einit = vec const s in
{etype = FuncRefType; einit; emode}
| 0x05l ->
| 0x05 ->
let emode = at passive s in
let etype = ref_type s in
let einit = vec const s in
{etype; einit; emode}
| 0x06l ->
| 0x06 ->
let emode = at active s in
let etype = ref_type s in
let einit = vec const s in
{etype; einit; emode}
| 0x07l ->
| 0x07 ->
let emode = at declarative s in
let etype = ref_type s in
let einit = vec const s in
Expand All @@ -999,16 +999,16 @@ let elem_section s =
(* Data section *)

let data s =
match vu32 s with
| 0x00l ->
match u8 s with
| 0x00 ->
let dmode = at active_zero s in
let dinit = string s in
{dinit; dmode}
| 0x01l ->
| 0x01 ->
let dmode = at passive s in
let dinit = string s in
{dinit; dmode}
| 0x02l ->
| 0x02 ->
let dmode = at active s in
let dinit = string s in
{dinit; dmode}
Expand Down
19 changes: 12 additions & 7 deletions test/core/binary-leb128.wast
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"\00asm" "\01\00\00\00"
"\05\03\01" ;; Memory section with 1 entry
"\00\00" ;; no max, minimum 0
"\0b\07\01" ;; Data section with 1 entry
"\0b\08\01" ;; Data section with 1 entry
"\02" ;; Data with explicit memory index
"\80\00" ;; Memory index 0, encoded with 2 bytes
"\41\00\0b\00" ;; (i32.const 0) with contents ""
)
Expand Down Expand Up @@ -236,7 +237,8 @@
"\00asm" "\01\00\00\00"
"\05\03\01" ;; Memory section with 1 entry
"\00\00" ;; no max, minimum 0
"\0b\0b\01" ;; Data section with 1 entry
"\0b\0c\01" ;; Data section with 1 entry
"\02" ;; Dat with explicit memory index
"\80\80\80\80\80\00" ;; Memory index 0 with one byte too many
"\41\00\0b\00" ;; (i32.const 0) with contents ""
)
Expand All @@ -247,9 +249,10 @@
"\00asm" "\01\00\00\00"
"\04\04\01" ;; Table section with 1 entry
"\70\00\00" ;; no max, minimum 0, funcref
"\09\0b\01" ;; Element section with 1 entry
"\09\0d\01" ;; Element section with 1 entry
"\02" ;; Element with explicit memory index
"\80\80\80\80\80\00" ;; Table index 0 with one byte too many
"\41\00\0b\00" ;; (i32.const 0) with no elements
"\41\00\0b\00\00" ;; (i32.const 0) with no func elements
)
"integer representation too long"
)
Expand Down Expand Up @@ -561,7 +564,8 @@
"\00asm" "\01\00\00\00"
"\05\03\01" ;; Memory section with 1 entry
"\00\00" ;; no max, minimum 0
"\0b\0a\01" ;; Data section with 1 entry
"\0b\0b\01" ;; Data section with 1 entry
"\02" ;; Data with explicit memory index
"\80\80\80\80\10" ;; Memory index 0 with unused bits set
"\41\00\0b\00" ;; (i32.const 0) with contents ""
)
Expand All @@ -572,9 +576,10 @@
"\00asm" "\01\00\00\00"
"\04\04\01" ;; Table section with 1 entry
"\70\00\00" ;; no max, minimum 0, funcref
"\09\0a\01" ;; Element section with 1 entry
"\09\0c\01" ;; Element section with 1 entry
"\02" ;; Element with explicit table index
"\80\80\80\80\10" ;; Table index 0 with unused bits set
"\41\00\0b\00" ;; (i32.const 0) with no elements
"\41\00\0b\00\00" ;; (i32.const 0) with no elements
)
"integer too large"
)
Expand Down
29 changes: 27 additions & 2 deletions test/core/binary.wast
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,24 @@
"\00asm" "\01\00\00\00"
"\05\03\01" ;; Memory section with 1 entry
"\00\00" ;; no max, minimum 0
"\0b\07\01" ;; Data section with 1 entry
"\80\00" ;; Memory index 0, encoded with 2 bytes
"\0b\08\01" ;; Data section with 1 entry
"\02\80\00" ;; Memory index 0, encoded with 2 bytes
"\41\00\0b\00" ;; (i32.const 0) with contents ""
)

;; This was a historically valid module in the MVP spec, but this is no longer
;; valid after the bulk-memory changes were merged in.
(assert_malformed
(module binary
"\00asm" "\01\00\00\00"
"\05\03\01" ;; Memory section with 1 entry
"\00\00" ;; no max, minimum 0
"\0b\07\01" ;; Data section with 1 entry
"\80\00" ;; Memory index 0, encoded with 2 bytes
"\41\00\0b\00" ;; (i32.const 0) with contents ""
)
"malformed data segment kind")

;; Element segment table index can have non-minimal length
(module binary
"\00asm" "\01\00\00\00"
Expand All @@ -160,6 +173,18 @@
"\41\00\0b\00\00" ;; (i32.const 0) with no elements
)

;; Element segment table index can have non-minimal length
(assert_malformed
(module binary
"\00asm" "\01\00\00\00"
"\04\04\01" ;; Table section with 1 entry
"\70\00\00" ;; no max, minimum 0, funcref
"\09\07\01" ;; Element section with 1 entry
"\80\00" ;; Table index 0, encoded with 2 bytes
"\41\00\0b\00" ;; (i32.const 0) with no elements
)
"malformed elements segment kind")

;; Type section with signed LEB128 encoded type
(assert_malformed
(module binary
Expand Down