Skip to content

opti: avoid set shadowstack for (call local.get) pattern #2731

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

Closed
wants to merge 3 commits into from

Conversation

HerrCai0907
Copy link
Member

optimize shadowstack
For (call local.get) pattern, local variable won't be changed in called function. So it is not needed to store the value of local.get in shadowstack

  • I've read the contributing guidelines
  • I've added my name and email to the NOTICE file

HerrCai0907 and others added 2 commits August 3, 2023 09:38
Co-authored-by: Max Graey <maxgraey@gmail.com>
Co-authored-by: Max Graey <maxgraey@gmail.com>
@HerrCai0907 HerrCai0907 requested a review from dcodeIO August 3, 2023 01:51
Copy link
Member

@dcodeIO dcodeIO left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from the comment below, I wonder if there is unsafe code in stdlib that assumes call operands to go to the stack. For instance

let foo = changetype<usize>(new Foo());
...
someFunc(changetype<Foo>(foo));

would be such a pattern, where the local foo is not placed on the shadow stack due to being typed as usize, and with the change the call wouldn't place the value on the shadow stack either, then risking that the value is collected too early. Not sure if this is actually done, but I guess we should reasonably check that this pattern isn't used?

@@ -170,10 +170,27 @@ function matchPattern(module: Module, expr: ExpressionRef): ExpressionRef {
}

/** Tests whether a `value` matched by `matchTostack` needs a slot. */
function needsSlot(module: Module, value: ExpressionRef): bool {
function needsSlotInCall(module: Module, value: ExpressionRef): bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of duplicating the function, needsSlot could take a third argument isCallOperand: bool = false perhaps, then returning !isCallOperand on local gets. Wdyt?

@HerrCai0907
Copy link
Member Author

HerrCai0907 commented Aug 4, 2023

I don't find this pattern is std.

And I think it will be danger only in this scenario:

class A {}

function f3(a: A): void {
  __unpin(changetype<usize>(a))
  // there 'a' is unsafe
}
export function f2callback(v: usize): void {
  f3(changetype<A>(v));
}

declare function f2(v: usize): void; // setTimeout(()=> f2callback, 100)

export function f1(): void {
  let a = new A();
  __pin(changetype<usize>(a));
  f2(changetype<usize>(a));
}

Wdyt? Maybe this optimization is not work.

@HerrCai0907 HerrCai0907 closed this Aug 9, 2023
@HerrCai0907 HerrCai0907 deleted the opti-gc branch October 16, 2023 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants