Skip to content

Commit 4fe2f00

Browse files
yuri91rossberg
andcommitted
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 <rossberg@mpi-sws.org>
1 parent 8028a0f commit 4fe2f00

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

interpreter/custom/handler_branch_hint.ml

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ let get_nth_inst locs idx =
6565
| Some (_, i) -> Some i
6666
| None -> None
6767

68+
6869
(* Decoding *)
6970

7071
(* TODO: make Decode module reusable instead of duplicating code *)
@@ -144,21 +145,27 @@ let decode_hint locs foff s =
144145

145146
let decode_func_hints locs foff = decode_vec (decode_hint locs foff)
146147

147-
let decode_func m s =
148+
let get_func_start_off f bs =
149+
let s = stream bs in
150+
skip f.at.left.column s;
151+
let _ = decode_u32 s in
152+
pos s
153+
154+
let decode_func m bs s =
148155
let fidx = decode_u32 s in
149156
let f = get_func m fidx in
150-
let foff = Int32.of_int f.at.left.column in
157+
let foff = Int32.of_int (get_func_start_off f bs) in
151158
let locs = flatten_instr_locs f.it.body in
152159
let hs = decode_func_hints locs foff s in
153160
(fidx, List.rev hs)
154161

155-
let decode_funcs m s =
156-
let fs = decode_vec (decode_func m) s in
162+
let decode_funcs m bs s =
163+
let fs = decode_vec (decode_func m bs) s in
157164
IdxMap.add_seq (List.to_seq fs) IdxMap.empty
158165

159-
let decode m _ custom =
166+
let decode m bs custom =
160167
let s = stream custom.it.content in
161-
try { func_hints = decode_funcs m s } @@ custom.at
168+
try { func_hints = decode_funcs m bs s } @@ custom.at
162169
with EOS -> decode_error (pos s) "unexpected end of name section"
163170

164171
(* Encoding *)
@@ -200,22 +207,22 @@ let encode_hint locs foff buf h =
200207

201208
let encode_func_hints buf locs foff = encode_vec buf (encode_hint locs foff)
202209

203-
let encode_func m buf t =
210+
let encode_func m bs buf t =
204211
let fidx, hs = t in
205212
encode_u32 buf fidx;
206213
let f = get_func m fidx in
207-
let foff = f.at.left.column in
214+
let foff = get_func_start_off f bs in
208215
let locs = flatten_instr_locs f.it.body in
209216
encode_func_hints buf locs foff hs
210217

211-
let encode_funcs buf m fhs =
212-
encode_vec buf (encode_func m) (List.of_seq (IdxMap.to_seq fhs))
218+
let encode_funcs buf m bs fhs =
219+
encode_vec buf (encode_func m bs) (List.of_seq (IdxMap.to_seq fhs))
213220

214221
let encode m bs sec =
215222
let { func_hints } = sec.it in
216223
let m2 = Decode.decode "" bs in
217224
let buf = Buffer.create 200 in
218-
encode_funcs buf m2 func_hints;
225+
encode_funcs buf m2 bs func_hints;
219226
let content = Buffer.contents buf in
220227
{
221228
name = Utf8.decode "metadata.code.branch_hint";

0 commit comments

Comments
 (0)