From a51f7e07d70915debd698bcd6eb6bf1a51e1d3fa Mon Sep 17 00:00:00 2001 From: Yuri Iozzelli Date: Thu, 8 May 2025 15:39:24 +0200 Subject: [PATCH 1/2] interpreter: fix function start offset of metadata.code.branch_hint The source information of functions consider them starting from the first byte of their size, while code metadata sections consider the start to be the first byte after the size, so we have to account for that. Co-authored-by: Andreas Rossberg --- interpreter/custom/handler_branch_hint.ml | 29 ++++++++++++++--------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/interpreter/custom/handler_branch_hint.ml b/interpreter/custom/handler_branch_hint.ml index 1f46d5de..0d40ddca 100644 --- a/interpreter/custom/handler_branch_hint.ml +++ b/interpreter/custom/handler_branch_hint.ml @@ -65,6 +65,7 @@ let get_nth_inst locs idx = | Some (_, i) -> Some i | None -> None + (* Decoding *) (* TODO: make Decode module reusable instead of duplicating code *) @@ -144,21 +145,27 @@ let decode_hint locs foff s = let decode_func_hints locs foff = decode_vec (decode_hint locs foff) -let decode_func m s = +let get_func_start_off f bs = + let s = stream bs in + skip f.at.left.column s; + let _ = decode_u32 s in + pos s + +let decode_func m bs s = let fidx = decode_u32 s in let f = get_func m fidx in - let foff = Int32.of_int f.at.left.column in + let foff = Int32.of_int (get_func_start_off f bs) in let locs = flatten_instr_locs f.it.body in let hs = decode_func_hints locs foff s in (fidx, List.rev hs) -let decode_funcs m s = - let fs = decode_vec (decode_func m) s in +let decode_funcs m bs s = + let fs = decode_vec (decode_func m bs) s in IdxMap.add_seq (List.to_seq fs) IdxMap.empty -let decode m _ custom = +let decode m bs custom = let s = stream custom.it.content in - try { func_hints = decode_funcs m s } @@ custom.at + try { func_hints = decode_funcs m bs s } @@ custom.at with EOS -> decode_error (pos s) "unexpected end of name section" (* Encoding *) @@ -200,22 +207,22 @@ let encode_hint locs foff buf h = let encode_func_hints buf locs foff = encode_vec buf (encode_hint locs foff) -let encode_func m buf t = +let encode_func m bs buf t = let fidx, hs = t in encode_u32 buf fidx; let f = get_func m fidx in - let foff = f.at.left.column in + let foff = get_func_start_off f bs in let locs = flatten_instr_locs f.it.body in encode_func_hints buf locs foff hs -let encode_funcs buf m fhs = - encode_vec buf (encode_func m) (List.of_seq (IdxMap.to_seq fhs)) +let encode_funcs buf m bs fhs = + encode_vec buf (encode_func m bs) (List.of_seq (IdxMap.to_seq fhs)) let encode m bs sec = let { func_hints } = sec.it in let m2 = Decode.decode "" bs in let buf = Buffer.create 200 in - encode_funcs buf m2 func_hints; + encode_funcs buf m2 bs func_hints; let content = Buffer.contents buf in { name = Utf8.decode "metadata.code.branch_hint"; From 020593dc587259e4624d079c0b6b184f4686faab Mon Sep 17 00:00:00 2001 From: Yuri Iozzelli Date: Thu, 8 May 2025 17:02:32 +0200 Subject: [PATCH 2/2] branch hinting: add a binary test --- test/custom/metadata.code.branch_hint/branch_hint.wast | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/custom/metadata.code.branch_hint/branch_hint.wast b/test/custom/metadata.code.branch_hint/branch_hint.wast index f3c63b32..5e2625b1 100644 --- a/test/custom/metadata.code.branch_hint/branch_hint.wast +++ b/test/custom/metadata.code.branch_hint/branch_hint.wast @@ -47,6 +47,14 @@ ) ) +(module binary + "\00asm" "\01\00\00\00" + "\01\85\80\80\80\00\01\60\01\7f\00" + "\03\82\80\80\80\00\01\00\00" + "\a0\80\80\80\00\19\6d\65\74\61\64\61\74\61\2e\63\6f\64\65\2e\62\72\61\6e\63\68\5f\68\69\6e\74\01\00\01\05\01\00" + "\0a\8f\80\80\80\00\01\89\80\80\80\00\00\02\40\41\00\0d\00\0b\0b" +) + (assert_malformed_custom (module quote "(func $test2 (type 0)" @@ -97,3 +105,4 @@ ) "@metadata.code.branch_hint annotation: invalid target" ) +