Skip to content
Merged
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
13 changes: 7 additions & 6 deletions interpreter/binary/decode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ let u32 s = I32.of_int_u (I64.to_int_u (uN 32 s))
let u64 s = uN 64 s
let s7 s = I64.to_int_s (sN 7 s)
let s32 s = I32.of_int_s (I64.to_int_s (sN 32 s))
let s33 s = Convert.I32_.wrap_i64 (sN 33 s)
let s33 s = sN 33 s
let s64 s = sN 64 s
let f32 s = F32.of_bits (word32 s)
let f64 s = F64.of_bits (word64 s)
Expand Down Expand Up @@ -149,6 +149,7 @@ open Types

let zero s = expect 0x00 s "zero byte expected"
let idx s = u32 s
let idx32 s = Convert.I64_.extend_i32_u (idx s)
let laneidx s = u8 s

let mutability s =
Expand All @@ -160,7 +161,7 @@ let mutability s =
let typeuse idx s =
let pos = pos s in
match idx s with
| i when i >= 0l -> Idx i
| i when i >= 0L -> Idx (Convert.I32_.wrap_i64 i)
| _ -> error s pos "malformed type index"

let numtype s =
Expand Down Expand Up @@ -262,11 +263,11 @@ let subtype s =
match peek s with
| Some i when i = -0x30 land 0x7f ->
skip 1 s;
let uts = vec (typeuse u32) s in
let uts = vec (typeuse idx32) s in
SubT (NoFinal, uts, comptype s)
| Some i when i = -0x31 land 0x7f ->
skip 1 s;
let uts = vec (typeuse u32) s in
let uts = vec (typeuse idx32) s in
SubT (Final, uts, comptype s)
| _ -> SubT (Final, [], comptype s)

Expand All @@ -287,7 +288,7 @@ let limits uN s =

let tagtype s =
zero s;
TagT (typeuse idx s)
TagT (typeuse idx32 s)

let globaltype s =
let t = valtype s in
Expand All @@ -305,7 +306,7 @@ let tabletype s =

let externtype s =
match byte s with
| 0x00 -> ExternFuncT (typeuse idx s)
| 0x00 -> ExternFuncT (typeuse idx32 s)
| 0x01 -> ExternTableT (tabletype s)
| 0x02 -> ExternMemoryT (memorytype s)
| 0x03 -> ExternGlobalT (globaltype s)
Expand Down
15 changes: 8 additions & 7 deletions interpreter/binary/encode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ struct
let u32 i = u64 Int64.(logand (of_int32 i) 0xffffffffL)
let s7 i = s64 (Int64.of_int i)
let s32 i = s64 (Int64.of_int32 i)
let s33 i = s64 (Convert.I64_.extend_i32_s i)
let s33 i = s64 i
let f32 x = word32 (F32.to_bits x)
let f64 x = word64 (F64.to_bits x)
let v128 v = String.iter (put s) (V128.to_bits v)
Expand Down Expand Up @@ -100,13 +100,14 @@ struct
open Source

let idx x = u32 x.it
let idx32 x = u32 (Convert.I32_.wrap_i64 x)

let mutability = function
| Cons -> byte 0
| Var -> byte 1

let typeuse idx = function
| Idx x -> idx x
| Idx x -> idx (Convert.I64_.extend_i32_u x)
| Rec _ | Def _ -> assert false

let numtype = function
Expand Down Expand Up @@ -176,8 +177,8 @@ struct

let subtype = function
| SubT (Final, [], ct) -> comptype ct
| SubT (Final, uts, ct) -> s7 (-0x31); vec (typeuse u32) uts; comptype ct
| SubT (NoFinal, uts, ct) -> s7 (-0x30); vec (typeuse u32) uts; comptype ct
| SubT (Final, uts, ct) -> s7 (-0x31); vec (typeuse idx32) uts; comptype ct
| SubT (NoFinal, uts, ct) -> s7 (-0x30); vec (typeuse idx32) uts; comptype ct

let rectype = function
| RecT [st] -> subtype st
Expand All @@ -188,7 +189,7 @@ struct
byte flags; u64 min; opt u64 max

let tagtype = function
| TagT ut -> u32 0x00l; typeuse u32 ut
| TagT ut -> u32 0x00l; typeuse idx32 ut

let globaltype = function
| GlobalT (mut, t) -> valtype t; mutability mut
Expand All @@ -200,7 +201,7 @@ struct
| TableT (at, lim, t) -> reftype t; limits at lim

let externtype = function
| ExternFuncT ut -> byte 0x00; typeuse u32 ut
| ExternFuncT ut -> byte 0x00; typeuse idx32 ut
| ExternTableT tt -> byte 0x01; tabletype tt
| ExternMemoryT mt -> byte 0x02; memorytype mt
| ExternGlobalT gt -> byte 0x03; globaltype gt
Expand Down Expand Up @@ -228,7 +229,7 @@ struct

let blocktype = function
| VarBlockType x -> typeuse s33 (Idx x.it)
| ValBlockType None -> s33 (-0x40l)
| ValBlockType None -> s33 (-0x40L)
| ValBlockType (Some t) -> valtype t

let local (n, loc) =
Expand Down
19 changes: 19 additions & 0 deletions test/core/binary.wast

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gemini suggests that we could also add an assert_malformed test for a negative s33 (e.g. -0x1_0000_0000 encoded as \80\80\80\80\70), which would previously wrap to 0l.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Original file line number Diff line number Diff line change
Expand Up @@ -1232,3 +1232,22 @@
)
"illegal opcode ff"
)

;; Decoding of s33
(assert_invalid
(module binary
"\00asm" "\01\00\00\00"
"\01\0a\01" ;; type section with 1 entry
"\60\01\63\80\80\80\80\08\00" ;; (func (param (ref null 0x8000_0000)))
)
"unknown type"
)

(assert_malformed
(module binary
"\00asm" "\01\00\00\00"
"\01\0a\01" ;; type section with 1 entry
"\60\01\63\80\80\80\80\70\00" ;; (func (param (ref null -0x1_0000_0000)))
)
"integer representation"
)
Loading