Skip to content

Optimize shadow stack pass #1667

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

Merged
merged 4 commits into from
Feb 22, 2021
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
36 changes: 24 additions & 12 deletions src/passes/shadowstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,26 @@ type SlotMap = Map<LocalIndex,SlotIndex>;
type TempMap = Map<NativeType,LocalIndex>;

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

/** Tests whether a `value` matched by `matchTostack` needs a slot. */
function needsSlot(module: Module, value: ExpressionRef): bool {
switch (_BinaryenExpressionGetId(value)) {
// no need to stack null pointers
case ExpressionId.Const: return !isConstZero(value);
// already kept in another slot
case ExpressionId.LocalGet:
case ExpressionId.LocalSet: return false; // tee
}
return true;
}

/** Instruments a module with a shadow stack for precise GC. */
export class ShadowStackPass extends Pass {
/** Stack frame slots, per function. */
Expand Down Expand Up @@ -320,9 +332,9 @@ export class ShadowStackPass extends Pass {
var numSlots = 0;
for (let i = 0, k = operands.length; i < k; ++i) {
let operand = operands[i];
let match = matchTostack(module, operand);
let match = matchPattern(module, operand);
if (!match) continue;
if (isConstZero(match)) {
if (!needsSlot(module, match)) {
operands[i] = match;
continue;
}
Expand Down Expand Up @@ -366,10 +378,10 @@ export class ShadowStackPass extends Pass {
operands[i] = _BinaryenCallGetOperandAt(call, i);
}
let numSlots = this.updateCallOperands(operands);
for (let i = 0, k = operands.length; i < k; ++i) {
_BinaryenCallSetOperandAt(call, i, operands[i]);
}
if (numSlots) {
for (let i = 0, k = operands.length; i < k; ++i) {
_BinaryenCallSetOperandAt(call, i, operands[i]);
}
// Reserve these slots for us so nested calls use their own
this.callSlotOffset += numSlots;
}
Expand All @@ -390,10 +402,10 @@ export class ShadowStackPass extends Pass {
operands[i] = _BinaryenCallIndirectGetOperandAt(callIndirect, i);
}
let numSlots = this.updateCallOperands(operands);
for (let i = 0, k = operands.length; i < k; ++i) {
_BinaryenCallIndirectSetOperandAt(callIndirect, i, operands[i]);
}
if (numSlots) {
for (let i = 0, k = operands.length; i < k; ++i) {
_BinaryenCallIndirectSetOperandAt(callIndirect, i, operands[i]);
}
// Reserve these slots for us so nested calls use their own
this.callSlotOffset += numSlots;
}
Expand All @@ -408,14 +420,14 @@ export class ShadowStackPass extends Pass {

/** @override */
visitLocalSet(localSet: ExpressionRef): void {
let module = this.module;
let value = _BinaryenLocalSetGetValue(localSet);
let match = matchTostack(this.module, value);
let match = matchPattern(module, value);
if (!match) return;
if (isConstZero(match)) {
if (!needsSlot(module, match)) {
_BinaryenLocalSetSetValue(localSet, match);
return;
}
var module = this.module;
let index = _BinaryenLocalSetGetIndex(localSet);
let slotIndex = this.noteSlot(this.currentFunction, index);
let stmts = new Array<ExpressionRef>();
Expand Down
71 changes: 13 additions & 58 deletions tests/compiler/assert-nonnull.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,6 @@
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.sub
global.set $~lib/memory/__stack_pointer
call $~stack_check
global.get $~lib/memory/__stack_pointer
i32.const 0
i32.store
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store
local.get $0
i32.eqz
if
Expand All @@ -109,10 +98,6 @@
i32.const 4
i32.add
global.set $~lib/memory/__stack_pointer
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.add
global.set $~lib/memory/__stack_pointer
local.get $0
)
(func $export:assert-nonnull/testObj (param $0 i32) (result i32)
Expand All @@ -124,17 +109,6 @@
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.sub
global.set $~lib/memory/__stack_pointer
call $~stack_check
global.get $~lib/memory/__stack_pointer
i32.const 0
i32.store
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store
local.get $0
i32.eqz
if
Expand All @@ -151,10 +125,6 @@
i32.const 4
i32.add
global.set $~lib/memory/__stack_pointer
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.add
global.set $~lib/memory/__stack_pointer
)
(func $export:assert-nonnull/testProp (param $0 i32) (result i32)
global.get $~lib/memory/__stack_pointer
Expand Down Expand Up @@ -208,16 +178,13 @@
local.get $0
i32.store
global.get $~lib/memory/__stack_pointer
i32.const 8
i32.const 4
i32.sub
global.set $~lib/memory/__stack_pointer
call $~stack_check
global.get $~lib/memory/__stack_pointer
i64.const 0
i64.store
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store offset=4
i32.const 0
i32.store
local.get $0
i32.eqz
if
Expand Down Expand Up @@ -271,7 +238,7 @@
i32.add
global.set $~lib/memory/__stack_pointer
global.get $~lib/memory/__stack_pointer
i32.const 8
i32.const 4
i32.add
global.set $~lib/memory/__stack_pointer
global.get $~lib/memory/__stack_pointer
Expand All @@ -290,21 +257,18 @@
local.get $0
i32.store
global.get $~lib/memory/__stack_pointer
i32.const 8
i32.const 4
i32.sub
global.set $~lib/memory/__stack_pointer
call $~stack_check
global.get $~lib/memory/__stack_pointer
i64.const 0
i64.store
global.get $~lib/memory/__stack_pointer
local.get $0
i32.const 0
i32.store
global.get $~lib/memory/__stack_pointer
local.get $0
call $~lib/array/Array<assert-nonnull/Foo|null>#__get
local.tee $0
i32.store offset=4
i32.store
local.get $0
i32.eqz
if
Expand All @@ -316,7 +280,7 @@
unreachable
end
global.get $~lib/memory/__stack_pointer
i32.const 8
i32.const 4
i32.add
global.set $~lib/memory/__stack_pointer
global.get $~lib/memory/__stack_pointer
Expand Down Expand Up @@ -348,9 +312,6 @@
local.set $1
global.get $~lib/memory/__stack_pointer
local.set $2
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store offset=4
block $__inlined_func$assert-nonnull/testAll
block $folding-inner0
local.get $0
Expand Down Expand Up @@ -417,9 +378,6 @@
local.set $1
global.get $~lib/memory/__stack_pointer
local.set $2
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store offset=4
block $__inlined_func$assert-nonnull/testAll2
block $folding-inner0
local.get $0
Expand Down Expand Up @@ -491,17 +449,14 @@
local.get $0
i32.store
global.get $~lib/memory/__stack_pointer
i32.const 8
i32.const 4
i32.sub
global.set $~lib/memory/__stack_pointer
call $~stack_check
global.get $~lib/memory/__stack_pointer
i64.const 0
i64.store
global.get $~lib/memory/__stack_pointer
global.get $~lib/memory/__stack_pointer
local.get $0
i32.const 0
i32.store
global.get $~lib/memory/__stack_pointer
local.get $0
i32.eqz
if
Expand All @@ -513,12 +468,12 @@
unreachable
end
local.get $0
i32.store offset=4
i32.store
local.get $0
i32.load
call_indirect (type $none_=>_i32)
global.get $~lib/memory/__stack_pointer
i32.const 8
i32.const 4
i32.add
global.set $~lib/memory/__stack_pointer
global.get $~lib/memory/__stack_pointer
Expand Down
Loading