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

Add debug range checks for makeSetValue #19251

Merged
merged 1 commit into from
Apr 29, 2023
Merged

Add debug range checks for makeSetValue #19251

merged 1 commit into from
Apr 29, 2023

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Apr 25, 2023

Trying to store a value outside the range of the type being stored to can result in unexpected behaviour. For example, storing 256 to a u8 will end up storing 1.

I decided to write this as a followup to #19239 to make it extra clear what the expected behaviour of makeSetValue is.

Adding these checks discovered real bug in out library code in getaddrinfo.

I ran the full other and core test suite with these checks enabled at ASSERTIONS=1 before deciding to use ASSERTIONS=2 (at least for now).

@sbc100 sbc100 force-pushed the checked_makeSetValue branch 2 times, most recently from 9e8d169 to beb3478 Compare April 25, 2023 22:57
@sbc100 sbc100 requested a review from kripken April 25, 2023 22:57
@sbc100 sbc100 force-pushed the checked_makeSetValue branch 2 times, most recently from 8770c7c to 74b851d Compare April 25, 2023 23:04
Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

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

The finding here is good but I think maybe the assertions are excessive? It is a reasonable thing for an optimizer to do HEAP32[x] = doubleValueThatWeWantLowerBitsFrom, that is, rather than manually mask off the lower bits, depend on the semantics of the JS operation that does it automatically.

@sbc100
Copy link
Collaborator Author

sbc100 commented Apr 25, 2023

The finding here is good but I think maybe the assertions are excessive? It is a reasonable thing for an optimizer to do HEAP32[x] = doubleValueThatWeWantLowerBitsFrom, that is, rather than manually mask off the lower bits, depend on the semantics of the JS operation that does it automatically.

If you want to do HEAP32[x] = doubleValueThatWeWantLowerBitsFrom directly, you still could, although I think it would be a rather odd think to want to do.

These assertions only apply the makeSetValue() macro. And they only apply with ASSERTIONS=2.

We can always revert if we find genuine uses for the truncating/lossy behaviour in makeSetValue?

@sbc100
Copy link
Collaborator Author

sbc100 commented Apr 25, 2023

(also its hard to imagine more heavy user of makeSetValue than our codebase/testsuite.. so if we don't have any genuine uses for lossy/truncating behavior it seems likely nobody does)

Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

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

Fair enough, as this is just for makeSetValue the assertion seems reasonable, and it doesn't limit the optimizer later.

@@ -381,7 +392,8 @@ function makeSetValue(ptr, pos, value, type, noNeedFirst, ignore, align, sep) {
if (slab == 'HEAPU64' || slab == 'HEAP64') {
value = `BigInt(${value})`;
}
return slab + '[' + getHeapOffset(offset, type) + '] = ' + value;
var rtn = slab + '[' + getHeapOffset(offset, type) + '] = ' + value;
return rtn;
Copy link
Member

Choose a reason for hiding this comment

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

This change (adding rtn) looks unnecessary?

@sbc100 sbc100 force-pushed the checked_makeSetValue branch 2 times, most recently from 6c9459d to 6553053 Compare April 28, 2023 01:44
@sbc100 sbc100 enabled auto-merge (squash) April 28, 2023 02:01
Trying to store a value outside the range of the type being stored to
can result in unexpected behaviour.  For example, storing 256 to a u8
will end up storing 1.

Adding these checks discovered real bug in out library code in
`getaddrinfo`.

I ran the full other and core test suite with these checks enabled
at ASSERTIONS=1 before deciding to use ASSERTIONS=2, at least for now.
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.

2 participants