Skip to content

Conversation

@kg
Copy link
Member

@kg kg commented May 17, 2023

The zero page is unallocated, which means that reading the string/array length fields of a null pointer will return a length of 0. The bounds check for getchr and getelema1 will thus fail, causing us to bail out as if we had done a null check. This lets us remove a compare and a branch in all cases.

Also skip emitting a const+add pair in a few places where we add offsets to pointers if we know the offset is 0, since it'll make traces slightly smaller.

@kg kg added arch-wasm WebAssembly architecture area-Codegen-Jiterpreter-mono labels May 17, 2023
@ghost ghost assigned kg May 17, 2023
@ghost
Copy link

ghost commented May 17, 2023

Tagging subscribers to 'arch-wasm': @lewing
See info in area-owners.md if you want to be subscribed.

Issue Details

The zero page is unallocated, which means that reading the string/array length fields of a null pointer will return a length of 0. The bounds check for getchr and getelema1 will thus fail, causing us to bail out as if we had done a null check. This lets us remove a compare and a branch in all cases.

Also skip emitting a const+add pair in a few places where we add offsets to pointers if we know the offset is 0, since it'll make traces slightly smaller.

Author: kg
Assignees: -
Labels:

arch-wasm, area-Codegen-Jiterpreter-mono

Milestone: -

@vargaz
Copy link
Contributor

vargaz commented May 17, 2023

Shouldn't reading a null ref throw a different exception ?

@kg
Copy link
Member Author

kg commented May 17, 2023

Shouldn't reading a null ref throw a different exception ?

On failure, we will bail out to the interpreter, and it will retry the opcode and do a null check.

I think we could apply this same optimization to AOT - if the bounds check fails, then null-check the array reference and throw the correct exception.

@kg
Copy link
Member Author

kg commented May 17, 2023

Since there's a risk this might cause obscure issues I think I'll merge it after we branch this preview. Based on emscripten-core/emscripten#19389 as long as we don't change our emscripten config, it should be safe in Release runtime builds but not in Debug ones.

@kg kg added the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label May 17, 2023
@kg kg marked this pull request as ready for review May 17, 2023 23:04
@lambdageek
Copy link
Member

Since there's a risk this might cause obscure issues I think I'll merge it after we branch this preview. Based on emscripten-core/emscripten#19389 as long as we don't change our emscripten config, it should be safe in Release runtime builds but not in Debug ones.

I'm a little bit concerned that release and debug builds will have different null-checking behavior.

Also users who set -p:WasmBuildNative=true and do Debug/Release builds of their app will have different null checking behavior too, right?

If we always set -sGLOBAL_BASE as suggested in the upstream issues, what would we be giving up? Have we discovered stack overflow bugs due to the wasm runtime trapping?

@kg kg force-pushed the wasm-jiterp-zero-page branch from ff1459e to b958446 Compare May 18, 2023 16:52
@kg
Copy link
Member Author

kg commented May 18, 2023

mono_wasm_is_zero_page_reserved should do the trick I think.

@lewing
Copy link
Member

lewing commented May 18, 2023

is this still no-merge?

@kg
Copy link
Member Author

kg commented May 18, 2023

is this still no-merge?

I don't want to add it to p5, I think it's risky.

@kg
Copy link
Member Author

kg commented May 25, 2023

While looking into one of Jerome's bug reports, I noticed that there was garbage data at offset 0 in the heap that shouldn't be there. Not sure how that scenario came to be, will try to understand whether something about this optimization is unsafe for a new or different reason.

@kg
Copy link
Member Author

kg commented May 27, 2023

While looking into one of Jerome's bug reports, I noticed that there was garbage data at offset 0 in the heap that shouldn't be there. Not sure how that scenario came to be, will try to understand whether something about this optimization is unsafe for a new or different reason.

Reproduced this in another build, and thankfully emscripten_stack_get_end() reports 0 in this case so there being random data at the start of memory wouldn't be a problem, this optimization would be disabled.

@kg kg removed the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label May 27, 2023
kg added 2 commits May 26, 2023 17:29
…d getchr

Disable zero page optimization in debug builds

Sense whether zero page optimizations are safe based on the location of the emscripten stack
@kg kg force-pushed the wasm-jiterp-zero-page branch from b958446 to 23ac0c7 Compare May 27, 2023 01:32
@kg kg requested review from lambdageek and vargaz May 27, 2023 01:45
@kg kg merged commit 9628be4 into dotnet:main May 31, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jul 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

arch-wasm WebAssembly architecture

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants