From d5d2701b46ffcdb4b03b6f1c9eb34d892679ca5a Mon Sep 17 00:00:00 2001 From: Andreas Rossberg Date: Wed, 21 Oct 2026 14:24:06 +0200 Subject: [PATCH 1/2] [interpreter] Fix en/decoding of s33 --- interpreter/binary/decode.ml | 13 +++++++------ interpreter/binary/encode.ml | 15 ++++++++------- test/core/binary.wast | 10 ++++++++++ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/interpreter/binary/decode.ml b/interpreter/binary/decode.ml index f817ff2606..2b7b7197c2 100644 --- a/interpreter/binary/decode.ml +++ b/interpreter/binary/decode.ml @@ -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) @@ -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 = @@ -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 = @@ -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) @@ -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 @@ -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) diff --git a/interpreter/binary/encode.ml b/interpreter/binary/encode.ml index 884592772c..510d6894fe 100644 --- a/interpreter/binary/encode.ml +++ b/interpreter/binary/encode.ml @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) = diff --git a/test/core/binary.wast b/test/core/binary.wast index e328be3603..b254fcb43b 100644 --- a/test/core/binary.wast +++ b/test/core/binary.wast @@ -1232,3 +1232,13 @@ ) "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" +) From ea9ab61e1a156972eb78e16edf2b8076790915ca Mon Sep 17 00:00:00 2001 From: Andreas Rossberg Date: Wed, 21 Oct 2026 23:16:55 +0200 Subject: [PATCH 2/2] Another test --- test/core/binary.wast | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/core/binary.wast b/test/core/binary.wast index b254fcb43b..0c1cf1a966 100644 --- a/test/core/binary.wast +++ b/test/core/binary.wast @@ -1242,3 +1242,12 @@ ) "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" +)