Skip to content

Commit d3410f4

Browse files
authored
fix: Fix lifetime confusion in shadow stack pass (#2596)
1 parent 0e0a3c1 commit d3410f4

File tree

161 files changed

+385356
-300110
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+385356
-300110
lines changed

Diff for: src/passes/shadowstack.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ function needsSlot(module: Module, value: ExpressionRef): bool {
174174
switch (_BinaryenExpressionGetId(value)) {
175175
// no need to stack null pointers
176176
case ExpressionId.Const: return !isConstZero(value);
177-
// already kept in another slot
178-
case ExpressionId.LocalGet:
179-
case ExpressionId.LocalSet: return false; // tee
177+
// note: can't omit a slot when assigning from another local since the other
178+
// local might have shorter lifetime and become reassigned, say in a loop,
179+
// then no longer holding on to the previous value in its stack slot.
180180
}
181181
return true;
182182
}

Diff for: tests/compiler/NonNullable.debug.wat

+134-15
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,44 @@
142142
i32.const 0
143143
return
144144
)
145+
(func $~start (type $none_=>_none)
146+
call $start:NonNullable
147+
)
148+
(func $~stack_check (type $none_=>_none)
149+
global.get $~lib/memory/__stack_pointer
150+
global.get $~lib/memory/__data_end
151+
i32.lt_s
152+
if
153+
i32.const 33152
154+
i32.const 33200
155+
i32.const 1
156+
i32.const 1
157+
call $~lib/builtins/abort
158+
unreachable
159+
end
160+
)
145161
(func $~lib/string/String.__eq (type $i32_i32_=>_i32) (param $left i32) (param $right i32) (result i32)
146162
(local $leftLength i32)
163+
(local $3 i32)
164+
global.get $~lib/memory/__stack_pointer
165+
i32.const 8
166+
i32.sub
167+
global.set $~lib/memory/__stack_pointer
168+
call $~stack_check
169+
global.get $~lib/memory/__stack_pointer
170+
i64.const 0
171+
i64.store $0
147172
local.get $left
148173
local.get $right
149174
i32.eq
150175
if
151176
i32.const 1
177+
local.set $3
178+
global.get $~lib/memory/__stack_pointer
179+
i32.const 8
180+
i32.add
181+
global.set $~lib/memory/__stack_pointer
182+
local.get $3
152183
return
153184
end
154185
local.get $left
@@ -163,40 +194,117 @@
163194
end
164195
if
165196
i32.const 0
197+
local.set $3
198+
global.get $~lib/memory/__stack_pointer
199+
i32.const 8
200+
i32.add
201+
global.set $~lib/memory/__stack_pointer
202+
local.get $3
166203
return
167204
end
168205
local.get $left
206+
local.set $3
207+
global.get $~lib/memory/__stack_pointer
208+
local.get $3
209+
i32.store $0
210+
local.get $3
169211
call $~lib/string/String#get:length
170212
local.set $leftLength
171213
local.get $leftLength
172214
local.get $right
215+
local.set $3
216+
global.get $~lib/memory/__stack_pointer
217+
local.get $3
218+
i32.store $0
219+
local.get $3
173220
call $~lib/string/String#get:length
174221
i32.ne
175222
if
176223
i32.const 0
224+
local.set $3
225+
global.get $~lib/memory/__stack_pointer
226+
i32.const 8
227+
i32.add
228+
global.set $~lib/memory/__stack_pointer
229+
local.get $3
177230
return
178231
end
179232
local.get $left
233+
local.set $3
234+
global.get $~lib/memory/__stack_pointer
235+
local.get $3
236+
i32.store $0
237+
local.get $3
180238
i32.const 0
181239
local.get $right
240+
local.set $3
241+
global.get $~lib/memory/__stack_pointer
242+
local.get $3
243+
i32.store $0 offset=4
244+
local.get $3
182245
i32.const 0
183246
local.get $leftLength
184247
call $~lib/util/string/compareImpl
185248
i32.eqz
249+
local.set $3
250+
global.get $~lib/memory/__stack_pointer
251+
i32.const 8
252+
i32.add
253+
global.set $~lib/memory/__stack_pointer
254+
local.get $3
186255
return
187256
)
188257
(func $~lib/string/String.__ne (type $i32_i32_=>_i32) (param $left i32) (param $right i32) (result i32)
258+
(local $2 i32)
259+
global.get $~lib/memory/__stack_pointer
260+
i32.const 8
261+
i32.sub
262+
global.set $~lib/memory/__stack_pointer
263+
call $~stack_check
264+
global.get $~lib/memory/__stack_pointer
265+
i64.const 0
266+
i64.store $0
189267
local.get $left
268+
local.set $2
269+
global.get $~lib/memory/__stack_pointer
270+
local.get $2
271+
i32.store $0
272+
local.get $2
190273
local.get $right
274+
local.set $2
275+
global.get $~lib/memory/__stack_pointer
276+
local.get $2
277+
i32.store $0 offset=4
278+
local.get $2
191279
call $~lib/string/String.__eq
192280
i32.eqz
281+
local.set $2
282+
global.get $~lib/memory/__stack_pointer
283+
i32.const 8
284+
i32.add
285+
global.set $~lib/memory/__stack_pointer
286+
local.get $2
193287
return
194288
)
195289
(func $NonNullable/assertNonNull<~lib/string/String> (type $i32_=>_none) (param $t i32)
290+
(local $1 i32)
291+
global.get $~lib/memory/__stack_pointer
292+
i32.const 4
293+
i32.sub
294+
global.set $~lib/memory/__stack_pointer
295+
call $~stack_check
296+
global.get $~lib/memory/__stack_pointer
297+
i32.const 0
298+
i32.store $0
196299
i32.const 0
197300
i32.eqz
198301
drop
199302
local.get $t
303+
local.set $1
304+
global.get $~lib/memory/__stack_pointer
305+
local.get $1
306+
i32.store $0
307+
local.get $1
200308
i32.const 0
201309
call $~lib/string/String.__ne
202310
i32.eqz
@@ -208,31 +316,42 @@
208316
call $~lib/builtins/abort
209317
unreachable
210318
end
319+
global.get $~lib/memory/__stack_pointer
320+
i32.const 4
321+
i32.add
322+
global.set $~lib/memory/__stack_pointer
211323
)
212324
(func $NonNullable/safetyCheck<~lib/string/String|null> (type $i32_=>_none) (param $t i32)
325+
(local $1 i32)
326+
global.get $~lib/memory/__stack_pointer
327+
i32.const 4
328+
i32.sub
329+
global.set $~lib/memory/__stack_pointer
330+
call $~stack_check
331+
global.get $~lib/memory/__stack_pointer
332+
i32.const 0
333+
i32.store $0
213334
local.get $t
335+
local.set $1
336+
global.get $~lib/memory/__stack_pointer
337+
local.get $1
338+
i32.store $0
339+
local.get $1
214340
i32.const 0
215341
call $~lib/string/String.__ne
216342
if
217343
local.get $t
344+
local.set $1
345+
global.get $~lib/memory/__stack_pointer
346+
local.get $1
347+
i32.store $0
348+
local.get $1
218349
call $NonNullable/assertNonNull<~lib/string/String>
219350
end
220-
)
221-
(func $~start (type $none_=>_none)
222-
call $start:NonNullable
223-
)
224-
(func $~stack_check (type $none_=>_none)
225351
global.get $~lib/memory/__stack_pointer
226-
global.get $~lib/memory/__data_end
227-
i32.lt_s
228-
if
229-
i32.const 33152
230-
i32.const 33200
231-
i32.const 1
232-
i32.const 1
233-
call $~lib/builtins/abort
234-
unreachable
235-
end
352+
i32.const 4
353+
i32.add
354+
global.set $~lib/memory/__stack_pointer
236355
)
237356
(func $start:NonNullable (type $none_=>_none)
238357
(local $0 i32)

0 commit comments

Comments
 (0)