Skip to content

Commit fde4ca3

Browse files
authored
gh-2616 Fix wrong validation of code metadata function index (#2617)
fixes #2616
1 parent a55fb94 commit fde4ca3

File tree

3 files changed

+127
-3
lines changed

3 files changed

+127
-3
lines changed

src/binary-reader-ir.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,8 +1703,11 @@ Result BinaryReaderIR::OnCodeMetadataFuncCount(Index count) {
17031703
}
17041704

17051705
Result BinaryReaderIR::OnCodeMetadataCount(Index function_index, Index count) {
1706-
code_metadata_queue_.push_func(module_->funcs[function_index]);
1707-
return Result::Ok;
1706+
if (function_index < module_->funcs.size()) {
1707+
code_metadata_queue_.push_func(module_->funcs[function_index]);
1708+
return Result::Ok;
1709+
}
1710+
return Result::Error;
17081711
}
17091712

17101713
Result BinaryReaderIR::OnCodeMetadata(Offset offset,

src/binary-reader.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2439,7 +2439,7 @@ Result BinaryReader::ReadCodeMetadataSection(std::string_view name,
24392439
Index last_function_index = kInvalidIndex;
24402440
for (Index i = 0; i < num_functions; ++i) {
24412441
Index function_index;
2442-
CHECK_RESULT(ReadCount(&function_index, "function index"));
2442+
CHECK_RESULT(ReadIndex(&function_index, "function index"));
24432443
ERROR_UNLESS(function_index >= num_func_imports_,
24442444
"function import can't have metadata (got %" PRIindex ")",
24452445
function_index);

test/parse/branch-hints.txt

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
;;; TOOL: run-objdump
2+
;;; ARGS0: --enable-annotations --enable-code-metadata
3+
;;; ARGS1: -x -j "metadata.code.branch_hint"
4+
;; combined from https://github.com/bytecodealliance/wasm-tools/tree/6ed178840fae8599312ab14c2cad6faea9f80bf1/tests/cli/branch-hinting
5+
(module
6+
(func $main (result i32)
7+
(local i32 i32 i32 i32)
8+
i32.const 0
9+
local.tee 2
10+
local.set 3
11+
loop ;; label = @1
12+
local.get 2
13+
i32.const 50000
14+
i32.eq
15+
(@metadata.code.branch_hint "\00") if ;; label = @2
16+
i32.const 1
17+
local.set 3
18+
end
19+
local.get 2
20+
i32.const 1
21+
i32.add
22+
local.tee 2
23+
i32.const 100000
24+
i32.ne
25+
(@metadata.code.branch_hint "\01") br_if 0 (;@1;)
26+
end
27+
local.get 3)
28+
29+
(func
30+
i32.const 0
31+
(@metadata.code.branch_hint "\00")
32+
if
33+
end)
34+
35+
36+
(func
37+
i32.const 0
38+
(@metadata.code.branch_hint "\00")
39+
if
40+
i32.const 0
41+
(@metadata.code.branch_hint "\01")
42+
br_if 0
43+
end)
44+
45+
(func
46+
i32.const 0
47+
(@metadata.code.branch_hint "\01")
48+
if
49+
i32.const 0
50+
(@metadata.code.branch_hint "\00")
51+
br_if 0
52+
end)
53+
)
54+
55+
(;; STDOUT ;;;
56+
branch-hints.wasm: file format wasm 0x1
57+
Section Details:
58+
Custom:
59+
- name: "metadata.code.branch_hint"
60+
- func[0]:
61+
- meta[12]:
62+
- 0000000: 00 .
63+
- meta[25]:
64+
- 0000000: 01 .
65+
- func[1]:
66+
- meta[3]:
67+
- 0000000: 00 .
68+
- func[2]:
69+
- meta[3]:
70+
- 0000000: 00 .
71+
- meta[7]:
72+
- 0000000: 01 .
73+
- func[3]:
74+
- meta[3]:
75+
- 0000000: 01 .
76+
- meta[7]:
77+
- 0000000: 00 .
78+
Code Disassembly:
79+
000057 func[0]:
80+
000058: 04 7f | local[0..3] type=i32
81+
00005a: 41 00 | i32.const 0
82+
00005c: 22 02 | local.tee 2
83+
00005e: 21 03 | local.set 3
84+
000060: 03 40 | loop
85+
000062: 20 02 | local.get 2
86+
000064: 41 d0 86 03 | i32.const 50000
87+
000068: 46 | i32.eq
88+
000069: 04 40 | if
89+
00006b: 41 01 | i32.const 1
90+
00006d: 21 03 | local.set 3
91+
00006f: 0b | end
92+
000070: 20 02 | local.get 2
93+
000072: 41 01 | i32.const 1
94+
000074: 6a | i32.add
95+
000075: 22 02 | local.tee 2
96+
000077: 41 a0 8d 06 | i32.const 100000
97+
00007b: 47 | i32.ne
98+
00007c: 0d 00 | br_if 0
99+
00007e: 0b | end
100+
00007f: 20 03 | local.get 3
101+
000081: 0b | end
102+
000083 func[1]:
103+
000084: 41 00 | i32.const 0
104+
000086: 04 40 | if
105+
000088: 0b | end
106+
000089: 0b | end
107+
00008b func[2]:
108+
00008c: 41 00 | i32.const 0
109+
00008e: 04 40 | if
110+
000090: 41 00 | i32.const 0
111+
000092: 0d 00 | br_if 0
112+
000094: 0b | end
113+
000095: 0b | end
114+
000097 func[3]:
115+
000098: 41 00 | i32.const 0
116+
00009a: 04 40 | if
117+
00009c: 41 00 | i32.const 0
118+
00009e: 0d 00 | br_if 0
119+
0000a0: 0b | end
120+
0000a1: 0b | end
121+
;;; STDOUT ;;)

0 commit comments

Comments
 (0)