Skip to content

Commit 0146325

Browse files
committed
Remove guesswork from getStackPointerGlobal. NFC
We were just assuming that if no `__stack_pointer` was imported then the first global must be the `__stack_pointer`. Instead we can just require that the global be names correctly in the name section. This will require an emscripten-side change to ensure names are generated when running this pass. Needed for fixing emscripten-core/emscripten#24964.
1 parent c37aca5 commit 0146325

7 files changed

Lines changed: 123 additions & 130 deletions

src/wasm/wasm-emscripten.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,10 @@ void addExportedFunction(Module& wasm, Function* function) {
4040
}
4141

4242
Global* getStackPointerGlobal(Module& wasm) {
43-
// Assumption: The stack pointer is either imported as __stack_pointer or
44-
// we just assume it's the first non-imported global.
45-
// TODO(sbc): Find a better way to discover the stack pointer. Perhaps the
46-
// linker could export it by name?
43+
// Assumption: The stack pointer is either be an imported global called
44+
// __stack_pointer or a defined global with that name.
4745
for (auto& g : wasm.globals) {
48-
if (g->imported() && g->base == STACK_POINTER) {
49-
return g.get();
50-
}
51-
}
52-
for (auto& g : wasm.globals) {
53-
if (!g->imported()) {
46+
if (g->base == STACK_POINTER || g->name == STACK_POINTER) {
5447
return g.get();
5548
}
5649
}

test/finalize/recursive_safe_stack.wat

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
(memory $0 2)
77
(data (i32.const 568) "%d:%d\n\00Result: %d\n\00")
88
(table $0 1 1 funcref)
9-
(global $global$0 (mut i32) (i32.const 66128))
9+
(global $__stack_pointer (mut i32) (i32.const 66128))
1010
(global $global$1 i32 (i32.const 66128))
1111
(global $global$2 i32 (i32.const 587))
1212
(export "memory" (memory $0))
@@ -18,10 +18,10 @@
1818
)
1919
(func $foo (; 2 ;) (type $0) (param $0 i32) (param $1 i32) (result i32)
2020
(local $2 i32)
21-
(global.set $global$0
21+
(global.set $__stack_pointer
2222
(local.tee $2
2323
(i32.sub
24-
(global.get $global$0)
24+
(global.get $__stack_pointer)
2525
(i32.const 16)
2626
)
2727
)
@@ -40,7 +40,7 @@
4040
(local.get $2)
4141
)
4242
)
43-
(global.set $global$0
43+
(global.set $__stack_pointer
4444
(i32.add
4545
(local.get $2)
4646
(i32.const 16)
@@ -53,10 +53,10 @@
5353
)
5454
(func $__original_main (; 3 ;) (type $2) (result i32)
5555
(local $0 i32)
56-
(global.set $global$0
56+
(global.set $__stack_pointer
5757
(local.tee $0
5858
(i32.sub
59-
(global.get $global$0)
59+
(global.get $__stack_pointer)
6060
(i32.const 16)
6161
)
6262
)
@@ -74,7 +74,7 @@
7474
(local.get $0)
7575
)
7676
)
77-
(global.set $global$0
77+
(global.set $__stack_pointer
7878
(i32.add
7979
(local.get $0)
8080
(i32.const 16)

test/finalize/recursive_safe_stack.wat.out

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
(type $4 (func (param i32 i32)))
77
(import "env" "printf" (func $printf (param i32 i32) (result i32)))
88
(import "env" "__handle_stack_overflow" (func $__handle_stack_overflow (param i32)))
9-
(global $global$0 (mut i32) (i32.const 66128))
9+
(global $__stack_pointer (mut i32) (i32.const 66128))
1010
(global $global$1 i32 (i32.const 66128))
1111
(global $global$2 i32 (i32.const 587))
1212
(global $__stack_base (mut i32) (i32.const 0))
@@ -33,7 +33,7 @@
3333
(local.tee $3
3434
(local.tee $2
3535
(i32.sub
36-
(global.get $global$0)
36+
(global.get $__stack_pointer)
3737
(i32.const 16)
3838
)
3939
)
@@ -51,7 +51,7 @@
5151
)
5252
)
5353
)
54-
(global.set $global$0
54+
(global.set $__stack_pointer
5555
(local.get $3)
5656
)
5757
)
@@ -92,7 +92,7 @@
9292
)
9393
)
9494
)
95-
(global.set $global$0
95+
(global.set $__stack_pointer
9696
(local.get $4)
9797
)
9898
)
@@ -112,7 +112,7 @@
112112
(local.tee $1
113113
(local.tee $0
114114
(i32.sub
115-
(global.get $global$0)
115+
(global.get $__stack_pointer)
116116
(i32.const 16)
117117
)
118118
)
@@ -130,7 +130,7 @@
130130
)
131131
)
132132
)
133-
(global.set $global$0
133+
(global.set $__stack_pointer
134134
(local.get $1)
135135
)
136136
)
@@ -170,7 +170,7 @@
170170
)
171171
)
172172
)
173-
(global.set $global$0
173+
(global.set $__stack_pointer
174174
(local.get $2)
175175
)
176176
)

test/finalize/safe_stack_standalone-wasm.wat

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
(memory $0 2)
77
(data (i32.const 568) "%d:%d\n\00Result: %d\n\00")
88
(table $0 1 1 funcref)
9-
(global $global$0 (mut i32) (i32.const 66128))
9+
(global $__stack_pointer (mut i32) (i32.const 66128))
1010
(global $global$1 i32 (i32.const 66128))
1111
(global $global$2 i32 (i32.const 587))
1212
(export "memory" (memory $0))
@@ -18,10 +18,10 @@
1818
)
1919
(func $foo (; 2 ;) (type $0) (param $0 i32) (param $1 i32) (result i32)
2020
(local $2 i32)
21-
(global.set $global$0
21+
(global.set $__stack_pointer
2222
(local.tee $2
2323
(i32.sub
24-
(global.get $global$0)
24+
(global.get $__stack_pointer)
2525
(i32.const 16)
2626
)
2727
)
@@ -40,7 +40,7 @@
4040
(local.get $2)
4141
)
4242
)
43-
(global.set $global$0
43+
(global.set $__stack_pointer
4444
(i32.add
4545
(local.get $2)
4646
(i32.const 16)
@@ -53,10 +53,10 @@
5353
)
5454
(func $__original_main (; 3 ;) (type $2) (result i32)
5555
(local $0 i32)
56-
(global.set $global$0
56+
(global.set $__stack_pointer
5757
(local.tee $0
5858
(i32.sub
59-
(global.get $global$0)
59+
(global.get $__stack_pointer)
6060
(i32.const 16)
6161
)
6262
)
@@ -74,7 +74,7 @@
7474
(local.get $0)
7575
)
7676
)
77-
(global.set $global$0
77+
(global.set $__stack_pointer
7878
(i32.add
7979
(local.get $0)
8080
(i32.const 16)

test/finalize/safe_stack_standalone-wasm.wat.out

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
(type $2 (func (result i32)))
55
(type $3 (func (param i32 i32)))
66
(import "env" "printf" (func $printf (param i32 i32) (result i32)))
7-
(global $global$0 (mut i32) (i32.const 66128))
7+
(global $__stack_pointer (mut i32) (i32.const 66128))
88
(global $global$1 i32 (i32.const 66128))
99
(global $global$2 i32 (i32.const 587))
1010
(global $__stack_base (mut i32) (i32.const 0))
@@ -31,7 +31,7 @@
3131
(local.tee $3
3232
(local.tee $2
3333
(i32.sub
34-
(global.get $global$0)
34+
(global.get $__stack_pointer)
3535
(i32.const 16)
3636
)
3737
)
@@ -47,7 +47,7 @@
4747
(unreachable)
4848
)
4949
)
50-
(global.set $global$0
50+
(global.set $__stack_pointer
5151
(local.get $3)
5252
)
5353
)
@@ -86,7 +86,7 @@
8686
(unreachable)
8787
)
8888
)
89-
(global.set $global$0
89+
(global.set $__stack_pointer
9090
(local.get $4)
9191
)
9292
)
@@ -106,7 +106,7 @@
106106
(local.tee $1
107107
(local.tee $0
108108
(i32.sub
109-
(global.get $global$0)
109+
(global.get $__stack_pointer)
110110
(i32.const 16)
111111
)
112112
)
@@ -122,7 +122,7 @@
122122
(unreachable)
123123
)
124124
)
125-
(global.set $global$0
125+
(global.set $__stack_pointer
126126
(local.get $1)
127127
)
128128
)
@@ -160,7 +160,7 @@
160160
(unreachable)
161161
)
162162
)
163-
(global.set $global$0
163+
(global.set $__stack_pointer
164164
(local.get $2)
165165
)
166166
)

0 commit comments

Comments
 (0)