Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle nest tostack in shadowstack pass #2792

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/passes/shadowstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,21 @@ type TempMap = Map<TypeRef,LocalIndex>;

/** Attempts to match the `__tostack(value)` pattern. Returns `value` if a match, otherwise `0`. */
function matchPattern(module: Module, expr: ExpressionRef): ExpressionRef {
if (
let isFound = false;
while (
_BinaryenExpressionGetId(expr) == ExpressionId.Call &&
module.readStringCached(_BinaryenCallGetTarget(expr)) == BuiltinNames.tostack
) {
assert(_BinaryenCallGetNumOperands(expr) == 1);
return _BinaryenCallGetOperandAt(expr, 0);
expr = _BinaryenCallGetOperandAt(expr, 0);
isFound = true;
}
return 0;
if (!isFound) return 0;
return expr;
}

/** Tests whether a `value` matched by `matchTostack` needs a slot. */
function needsSlot(module: Module, value: ExpressionRef): bool {
function needsSlot(value: ExpressionRef): bool {
switch (_BinaryenExpressionGetId(value)) {
// no need to stack null pointers
case ExpressionId.Const: return !isConstZero(value);
Expand Down Expand Up @@ -344,7 +347,7 @@ export class ShadowStackPass extends Pass {
let operand = operands[i];
let match = matchPattern(module, operand);
if (!match) continue;
if (!needsSlot(module, match)) {
if (!needsSlot(match)) {
operands[i] = match;
continue;
}
Expand Down Expand Up @@ -434,7 +437,7 @@ export class ShadowStackPass extends Pass {
let value = _BinaryenLocalSetGetValue(localSet);
let match = matchPattern(module, value);
if (!match) return;
if (!needsSlot(module, match)) {
if (!needsSlot(match)) {
_BinaryenLocalSetSetValue(localSet, match);
return;
}
Expand Down
8 changes: 2 additions & 6 deletions tests/compiler/resolve-unary.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3753,10 +3753,8 @@
call $~lib/builtins/abort
unreachable
end
global.get $~lib/memory/__stack_pointer
global.get $resolve-unary/bar
local.tee $6
i32.store
local.set $6
global.get $~lib/memory/__stack_pointer
local.get $6
i32.store offset=12
Expand Down Expand Up @@ -3789,10 +3787,8 @@
call $~lib/builtins/abort
unreachable
end
global.get $~lib/memory/__stack_pointer
global.get $resolve-unary/bar
local.tee $6
i32.store
local.set $6
global.get $~lib/memory/__stack_pointer
local.get $6
i32.store offset=12
Expand Down
8 changes: 0 additions & 8 deletions tests/compiler/resolve-unary.release.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2626,10 +2626,6 @@
global.get $~lib/memory/__stack_pointer
local.tee $0
global.get $resolve-unary/bar
local.tee $1
i32.store
local.get $0
local.get $1
i32.store offset=12
local.get $0
i32.const 3680
Expand All @@ -2655,10 +2651,6 @@
global.get $~lib/memory/__stack_pointer
local.tee $0
global.get $resolve-unary/bar
local.tee $1
i32.store
local.get $0
local.get $1
i32.store offset=12
local.get $0
i32.const 3712
Expand Down