-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Simplify and fix SAFE_HEAP #24291
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
RReverser
merged 9 commits into
emscripten-core:main
from
RReverser:simplify-and-fix-safe-heap
May 13, 2025
Merged
Simplify and fix SAFE_HEAP #24291
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a8874fa
Add failing test for i64 + SAFE_HEAP
RReverser 946bfd1
Simplify `SAFE_HEAP` implementation
RReverser ba4146b
Rebaseline
RReverser 34b458e
Fixups
RReverser d3f196e
Format
RReverser eba471f
Also remove obsolete (get|set)Value_safe helpers
RReverser 2a0b10c
Reset codesize files
RReverser 00de2cf
Skip test on SAFE_HEAP + ASan together
RReverser 7946613
Fix test
RReverser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,65 +1,57 @@ | ||
| SAFE_HEAP_STORE(x, 1, 1); | ||
| SAFE_HEAP_STORE(HEAP8, x, 1); | ||
|
|
||
| SAFE_HEAP_STORE(x * 2, 2, 2); | ||
| SAFE_HEAP_STORE(HEAP16, x, 2); | ||
|
|
||
| SAFE_HEAP_STORE(x * 4, 3, 4); | ||
| SAFE_HEAP_STORE(HEAP32, x, 3); | ||
|
|
||
| SAFE_HEAP_STORE(x, 4, 1); | ||
| SAFE_HEAP_STORE(HEAPU8, x, 4); | ||
|
|
||
| SAFE_HEAP_STORE(x * 2, 5, 2); | ||
| SAFE_HEAP_STORE(HEAPU16, x, 5); | ||
|
|
||
| SAFE_HEAP_STORE(x * 4, 6, 4); | ||
| SAFE_HEAP_STORE(HEAPU32, x, 6); | ||
|
|
||
| SAFE_HEAP_STORE_D(x * 4, 7, 4); | ||
| SAFE_HEAP_STORE(HEAPF32, x, 7); | ||
|
|
||
| SAFE_HEAP_STORE_D(x * 8, 8, 8); | ||
| SAFE_HEAP_STORE(HEAPF64, x, 8); | ||
|
|
||
| SAFE_HEAP_STORE(x * 8, 9n, 8); | ||
| SAFE_HEAP_STORE(HEAP64, x, 9n); | ||
|
|
||
| SAFE_HEAP_STORE(x * 8, 10n, 8); | ||
| SAFE_HEAP_STORE(HEAPU64, x, 10n); | ||
|
|
||
| a1 = SAFE_HEAP_LOAD(x, 1, 0); | ||
| a1 = SAFE_HEAP_LOAD(HEAP8, x); | ||
|
|
||
| a2 = SAFE_HEAP_LOAD(x * 2, 2, 0); | ||
| a2 = SAFE_HEAP_LOAD(HEAP16, x); | ||
|
|
||
| a3 = SAFE_HEAP_LOAD(x * 4, 4, 0); | ||
| a3 = SAFE_HEAP_LOAD(HEAP32, x); | ||
|
|
||
| a4 = SAFE_HEAP_LOAD(x, 1, 1); | ||
| a4 = SAFE_HEAP_LOAD(HEAPU8, x); | ||
|
|
||
| a5 = SAFE_HEAP_LOAD(x * 2, 2, 1); | ||
| a5 = SAFE_HEAP_LOAD(HEAPU16, x); | ||
|
|
||
| a6 = SAFE_HEAP_LOAD(x * 4, 4, 1); | ||
| a6 = SAFE_HEAP_LOAD(HEAPU32, x); | ||
|
|
||
| a7 = SAFE_HEAP_LOAD_D(x * 4, 4, 0); | ||
| a7 = SAFE_HEAP_LOAD(HEAPF32, x); | ||
|
|
||
| a8 = SAFE_HEAP_LOAD_D(x * 8, 8, 0); | ||
| a8 = SAFE_HEAP_LOAD(HEAPF64, x); | ||
|
|
||
| a9 = SAFE_HEAP_LOAD(x * 8, 8, 0); | ||
| a9 = SAFE_HEAP_LOAD(HEAP64, x); | ||
|
|
||
| a10 = SAFE_HEAP_LOAD(x * 8, 8, 1); | ||
| a10 = SAFE_HEAP_LOAD(HEAPU64, x); | ||
|
|
||
| foo = SAFE_HEAP_STORE(1337, 42, 1); | ||
| foo = SAFE_HEAP_STORE(HEAPU8, 1337, 42); | ||
|
|
||
| SAFE_HEAP_LOAD(bar(SAFE_HEAP_LOAD_D(5 * 8, 8, 0)) * 2, 2, 0); | ||
| SAFE_HEAP_LOAD(HEAP16, bar(SAFE_HEAP_LOAD(HEAPF64, 5))); | ||
|
|
||
| SAFE_HEAP_STORE_D(x * 4, SAFE_HEAP_LOAD(y * 4, 4, 0), 4); | ||
| SAFE_HEAP_STORE(HEAPF32, x, SAFE_HEAP_LOAD(HEAP32, y)); | ||
|
|
||
| function SAFE_HEAP_FOO(ptr) { | ||
| return HEAP8[ptr]; | ||
| } | ||
|
|
||
| function setValue_safe(ptr) { | ||
| return HEAP8[ptr]; | ||
| } | ||
|
|
||
| function getValue_safe(ptr) { | ||
| return HEAP8[ptr]; | ||
| } | ||
|
|
||
| function somethingElse() { | ||
| return SAFE_HEAP_LOAD(ptr, 1, 0); | ||
| return SAFE_HEAP_LOAD(HEAP8, ptr); | ||
| } | ||
|
|
||
| HEAP8.length; | ||
|
|
||
| SAFE_HEAP_LOAD(length, 1, 0); | ||
| SAFE_HEAP_LOAD(HEAP8, length); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happened to the alignment checks?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer necessary because we pass in an index in the corresponding array instead of pre-multiplied pointer.
Those were already guaranteed not to be hit since we always did transform like
HEAP32[i]->SAFE_HEAP_LOAD(i * 4, 4, ...)and then function would just check thati * 4is divisible by4, which is always true, but now it's just more obvious since we skip the premultiplication.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I was also concerned about directly-emitted
SAFE_HEAP_*calls from parseTools (not from the acorn transform), but I see we removed those a while ago, which I forgot...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah, I did also notice that
emscripten/site/source/docs/api_reference/preamble.js.rst
Lines 381 to 391 in 976b43d
Maybe that section should be just removed at this point instead of trying in sync, since it's pretty outdated and not rendered in docs anyway?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, yeah, best to just remove that bit, good find.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. I was mainly curious in context of those no-op checks we are commenting on, as I assume they used to do something useful in the long past, so technically a regression happened at some point... but idk when. As you say, it's probably not urgent.
Problem I'm thinking about, a lot of code has form like
HEAP32[somePtr >> 2]and such, whether handcrafted or generated viamakeGetValue, and in those cases there is no guaranteesomePtris already correctly aligned.JS transform could detect at least this common form of access and check the alignment. Or maybe at least
makeGetValue/getHeapOffsetitself could insert those checks whenSAFE_HEAPis enabled.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I don't think the old code handled this did it? It just checked the value of
ptr >> 2at runtime, right? So there is no regression here is there?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be clear neither the old new the version the JS transform do this though do they?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you mean before/after this PR, then yeah.
But judging by the removed code, it used to in the long past. And, well, even if it didn't, just saying it would be a useful addition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this isn't a recent regression. But it may have regressed a long time ago, or never worked... Anyhow, alignment checks from JS are pretty minor, not worth worrying about.