Skip to content

Commit 53aab48

Browse files
authoredNov 17, 2023
fix: handle nest tostack in shadowstack pass (#2792)
1 parent 4f15024 commit 53aab48

File tree

3 files changed

+11
-20
lines changed

3 files changed

+11
-20
lines changed
 

‎src/passes/shadowstack.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,21 @@ type TempMap = Map<TypeRef,LocalIndex>;
159159

160160
/** Attempts to match the `__tostack(value)` pattern. Returns `value` if a match, otherwise `0`. */
161161
function matchPattern(module: Module, expr: ExpressionRef): ExpressionRef {
162-
if (
162+
let isFound = false;
163+
while (
163164
_BinaryenExpressionGetId(expr) == ExpressionId.Call &&
164165
module.readStringCached(_BinaryenCallGetTarget(expr)) == BuiltinNames.tostack
165166
) {
166167
assert(_BinaryenCallGetNumOperands(expr) == 1);
167-
return _BinaryenCallGetOperandAt(expr, 0);
168+
expr = _BinaryenCallGetOperandAt(expr, 0);
169+
isFound = true;
168170
}
169-
return 0;
171+
if (!isFound) return 0;
172+
return expr;
170173
}
171174

172175
/** Tests whether a `value` matched by `matchTostack` needs a slot. */
173-
function needsSlot(module: Module, value: ExpressionRef): bool {
176+
function needsSlot(value: ExpressionRef): bool {
174177
switch (_BinaryenExpressionGetId(value)) {
175178
// no need to stack null pointers
176179
case ExpressionId.Const: return !isConstZero(value);
@@ -344,7 +347,7 @@ export class ShadowStackPass extends Pass {
344347
let operand = operands[i];
345348
let match = matchPattern(module, operand);
346349
if (!match) continue;
347-
if (!needsSlot(module, match)) {
350+
if (!needsSlot(match)) {
348351
operands[i] = match;
349352
continue;
350353
}
@@ -434,7 +437,7 @@ export class ShadowStackPass extends Pass {
434437
let value = _BinaryenLocalSetGetValue(localSet);
435438
let match = matchPattern(module, value);
436439
if (!match) return;
437-
if (!needsSlot(module, match)) {
440+
if (!needsSlot(match)) {
438441
_BinaryenLocalSetSetValue(localSet, match);
439442
return;
440443
}

‎tests/compiler/resolve-unary.debug.wat

+2-6
Original file line numberDiff line numberDiff line change
@@ -3753,10 +3753,8 @@
37533753
call $~lib/builtins/abort
37543754
unreachable
37553755
end
3756-
global.get $~lib/memory/__stack_pointer
37573756
global.get $resolve-unary/bar
3758-
local.tee $6
3759-
i32.store
3757+
local.set $6
37603758
global.get $~lib/memory/__stack_pointer
37613759
local.get $6
37623760
i32.store offset=12
@@ -3789,10 +3787,8 @@
37893787
call $~lib/builtins/abort
37903788
unreachable
37913789
end
3792-
global.get $~lib/memory/__stack_pointer
37933790
global.get $resolve-unary/bar
3794-
local.tee $6
3795-
i32.store
3791+
local.set $6
37963792
global.get $~lib/memory/__stack_pointer
37973793
local.get $6
37983794
i32.store offset=12

‎tests/compiler/resolve-unary.release.wat

-8
Original file line numberDiff line numberDiff line change
@@ -2626,10 +2626,6 @@
26262626
global.get $~lib/memory/__stack_pointer
26272627
local.tee $0
26282628
global.get $resolve-unary/bar
2629-
local.tee $1
2630-
i32.store
2631-
local.get $0
2632-
local.get $1
26332629
i32.store offset=12
26342630
local.get $0
26352631
i32.const 3680
@@ -2655,10 +2651,6 @@
26552651
global.get $~lib/memory/__stack_pointer
26562652
local.tee $0
26572653
global.get $resolve-unary/bar
2658-
local.tee $1
2659-
i32.store
2660-
local.get $0
2661-
local.get $1
26622654
i32.store offset=12
26632655
local.get $0
26642656
i32.const 3712

0 commit comments

Comments
 (0)
Please sign in to comment.