Skip to content

Commit 396a826

Browse files
authored
[PostEmscripten] Fix calcSegmentOffsets for large offsets (#6260)
Specifically offsets larger than 2^32 which were being interpreted misinterpreted here as very large int64_t values.
1 parent cf14a9f commit 396a826

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

src/passes/PostEmscripten.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,11 @@ static void calcSegmentOffsets(Module& wasm,
9595
return;
9696
}
9797
}
98-
auto it = offsets.find(curr->segment);
99-
if (it != offsets.end()) {
98+
if (offsets.find(curr->segment) != offsets.end()) {
10099
Fatal() << "Cannot get offset of passive segment initialized "
101100
"multiple times";
102101
}
103-
offsets[curr->segment] = dest->value.getInteger();
102+
offsets[curr->segment] = dest->value.getUnsigned();
104103
}
105104
} searcher(passiveOffsets);
106105
searcher.walkModule(&wasm);
@@ -317,7 +316,7 @@ struct PostEmscripten : public Pass {
317316
// The first operand is the function pointer index, which must be
318317
// constant if we are to optimize it statically.
319318
if (auto* index = curr->operands[0]->dynCast<Const>()) {
320-
size_t indexValue = index->value.getInteger();
319+
size_t indexValue = index->value.getUnsigned();
321320
if (indexValue >= flatTable.names.size()) {
322321
// UB can lead to indirect calls to invalid pointers.
323322
return;
Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
2-
;; RUN: wasm-opt %s --post-emscripten -S -o - | filecheck %s
2+
;; RUN: wasm-opt %s --enable-bulk-memory --post-emscripten -S -o - | filecheck %s
33

44
;; Checks that the start/stop exports are removed and that the data they
55
;; refer to is either zero'd out, or the segment emptied.
66

7+
;; Two of the segments used here are active, so their offsets are trivial
8+
;; to dirive. One segment is passive and its offset is derived from the
9+
;; memory.init instruction.
10+
11+
;; Explictly use a data address that is larger then INT_MAX to verify
12+
;; that these offset are correctly interpreted as unsigned.
13+
714
(module
15+
;; CHECK: (type $0 (func))
16+
817
;; CHECK: (global $em_asm_start i32 (i32.const 1000))
918
(global $em_asm_start i32 (i32.const 1000))
1019
;; CHECK: (global $em_asm_stop i32 (i32.const 1011))
1120
(global $em_asm_stop i32 (i32.const 1011))
12-
;; CHECK: (global $em_js_start i32 (i32.const 2006))
13-
(global $em_js_start i32 (i32.const 2006))
14-
;; CHECK: (global $em_js_stop i32 (i32.const 2015))
15-
(global $em_js_stop i32 (i32.const 2015))
21+
;; CHECK: (global $em_js_start i32 (i32.const -1294967290))
22+
(global $em_js_start i32 (i32.const 3000000006))
23+
;; CHECK: (global $em_js_stop i32 (i32.const -1294967281))
24+
(global $em_js_stop i32 (i32.const 3000000015))
1625
;; CHECK: (global $em_lib_deps_start i32 (i32.const 3000))
1726
(global $em_lib_deps_start i32 (i32.const 3000))
1827
;; CHECK: (global $em_lib_deps_stop i32 (i32.const 3009))
@@ -21,13 +30,12 @@
2130
(global $foo_start i32 (i32.const 4000))
2231
;; CHECK: (global $foo_stop i32 (i32.const 4015))
2332
(global $foo_stop i32 (i32.const 4015))
24-
(memory 10 10)
25-
;; CHECK: (memory $0 10 10)
26-
33+
;; CHECK: (memory $mem 10 10)
34+
(memory $mem 10 10)
2735
;; CHECK: (data $data1 (i32.const 1000) "")
2836
(data $data1 (i32.const 1000) "hello world")
29-
;; CHECK: (data $data2 (i32.const 2000) "hello \00\00\00\00\00\00\00\00\00 world")
30-
(data $data2 (i32.const 2000) "hello DELETE ME world")
37+
;; CHECK: (data $data2 "hello \00\00\00\00\00\00\00\00\00 world")
38+
(data $data2 "hello DELETE ME world")
3139
;; CHECK: (data $data3 (i32.const 3000) "")
3240
(data $data3 (i32.const 3000) "some deps")
3341
(export "__start_em_asm" (global $em_asm_start))
@@ -40,4 +48,18 @@
4048
(export "__start_foo" (global $foo_start))
4149
;; CHECK: (export "__stop_foo" (global $foo_stop))
4250
(export "__stop_foo" (global $foo_stop))
51+
;; CHECK: (func $meminit
52+
;; CHECK-NEXT: (memory.init $data2
53+
;; CHECK-NEXT: (i32.const -1294967296)
54+
;; CHECK-NEXT: (i32.const 0)
55+
;; CHECK-NEXT: (i32.const 21)
56+
;; CHECK-NEXT: )
57+
;; CHECK-NEXT: )
58+
(func $meminit
59+
(memory.init $mem $data2
60+
(i32.const 3000000000)
61+
(i32.const 0)
62+
(i32.const 21)
63+
)
64+
)
4365
)

0 commit comments

Comments
 (0)