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

Refactor singlepass init stack assembly #2012

Merged
merged 8 commits into from
Jan 15, 2021

Conversation

slave5vw
Copy link

@slave5vw slave5vw commented Jan 13, 2021

Description

The old method generated 1 mov assembly instruction per stack slot.
This takes up 15 bytes * slots count size. So more local stack variables use, the larger the code size.
The suggested method is the same as c memset.
Initialize with a fixed instructions (24 bytes) using the rep stosq assembly instruction. It works on a range basis.
https://www.felixcloutier.com/x86/stos:stosb:stosw:stosd:stosq

However, in this way, the skip_stack_guard_page test fails.

if stackaddr - region::page::size() <= addr && addr < stackaddr + stacksize {

The reason is that DF Flag is 0, so it is accessed in reverse order and initialized.
However, The vm trap handler checks the last address and determines it as a heap oob without range.
the stack oob is correct, and the test condition fails because it is a stack oob occurrence.

I think there are two ways.

  1. improve stack oob check in VM trap handler
    -> Abstract. There is no information on the range of access, so this may not be possible.
  2. pushf/popf/std(set DF=1), and access to sequential order
    -> Not recommended. Unnecessarily increases assembly size.

So for now, I added it to ignores.txt like cranelift/llvm.

Review

  • Add a short description of the the change to the CHANGELOG.md file

@slave5vw slave5vw changed the title refactor singlepass init stack assembly Refactor singlepass init stack assembly Jan 13, 2021
Since the direction of rep stosq is in reverse order (DF=0),
The trap handler is judged as a heap oob exception based on the start address.
@brew0722 brew0722 force-pushed the feature/refactor-init-stack-assembly branch from 99ad186 to 71f4e15 Compare January 13, 2021 02:34
@losfair
Copy link
Contributor

losfair commented Jan 13, 2021

Thanks for the pull request!

I added stack probe for large allocations before rep stosq and it should fix the skip_stack_guard_page test.

@losfair
Copy link
Contributor

losfair commented Jan 13, 2021

bors try

1 similar comment
@losfair
Copy link
Contributor

losfair commented Jan 13, 2021

bors try

@bors
Copy link
Contributor

bors bot commented Jan 13, 2021

try

Already running a review

@syrusakbary
Copy link
Member

bors try-

@syrusakbary
Copy link
Member

bors try

bors bot added a commit that referenced this pull request Jan 13, 2021
@bors
Copy link
Contributor

bors bot commented Jan 13, 2021

Copy link
Contributor

@Hywan Hywan left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

lib/compiler-singlepass/src/machine.rs Outdated Show resolved Hide resolved
lib/compiler-singlepass/src/machine.rs Outdated Show resolved Hide resolved
@Hywan
Copy link
Contributor

Hywan commented Jan 14, 2021

@losfair Can you approve this PR if it's OK please? :-)

@syrusakbary
Copy link
Member

bors r+

bors bot added a commit that referenced this pull request Jan 14, 2021
2012: Refactor singlepass init stack assembly r=syrusakbary a=slave5vw

<!-- 
Prior to submitting a PR, review the CONTRIBUTING.md document for recommendations on how to test:
https://github.com/wasmerio/wasmer/blob/master/CONTRIBUTING.md#pull-requests

-->

# Description
<!-- 
Provide details regarding the change including motivation,
links to related issues, and the context of the PR.
-->
<!-- 
Prior to submitting a PR, review the CONTRIBUTING.md document for recommendations on how to test:
https://github.com/wasmerio/wasmer/blob/master/CONTRIBUTING.md#pull-requests

-->

The old method generated 1 mov assembly instruction per stack slot.
This takes up 15 bytes * slots count size. So more local stack variables use, the larger the code size.
The suggested method is the same as c memset.
Initialize with a fixed instructions (24 bytes) using the rep stosq assembly instruction. It works on a range basis.
https://www.felixcloutier.com/x86/stos:stosb:stosw:stosd:stosq

However, in this way, the skip_stack_guard_page test fails.
https://github.com/wasmerio/wasmer/blob/1b49fe8475e335c6cdbb702a7100ba044201afd0/lib/vm/src/trap/traphandlers.rs#L124
The reason is that DF Flag is 0, so it is accessed in reverse order and initialized.
However, The vm trap handler checks the last address and determines it as a heap oob without range.
the stack oob is correct, and the test condition fails because it is a stack oob occurrence.

I think there are two ways. 
1. improve stack oob check in VM trap handler 
-> Abstract. There is no information on the range of access, so this may not be possible.
2. pushf/popf/std(set DF=1), and access to sequential order
-> Not recommended. Unnecessarily increases assembly size.

So for now, I added it to ignores.txt like cranelift/llvm.


# Review

- [ ] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Jiyong Ha <[email protected]>
Co-authored-by: losfair <[email protected]>
Co-authored-by: Jiyong Ha <[email protected]>
Co-authored-by: Syrus Akbary <[email protected]>
@bors
Copy link
Contributor

bors bot commented Jan 14, 2021

Build failed:

@syrusakbary
Copy link
Member

bors r+

@bors
Copy link
Contributor

bors bot commented Jan 15, 2021

@bors bors bot merged commit 4edbde8 into wasmerio:master Jan 15, 2021
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.

5 participants