vmgen: fix two overflow bugs with set
s
#801
Merged
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.
Summary
Fix either the compiler crashing or the VM failing at run-time with an
over- or underflow defect for
incl
,contains
, and constructionoperations for
set
s where the element range crossed theint64
upperboundary.
The fixed bugs only affected VM bytecode generation and code running in
the VM.
Details
There were two related problems:
vmgen
logic performed all math with signed integervalues
set
operationalways happened via
SubInt
(subtract signed integer), ignoringthe signed-ness of the operands
When the lower bound of the elements' range was beyond the
int64
upperbound, this didn't cause problems: reinterpreting the integer bits
always yields negative values there. For example, for a
set[Low..High]
where
Low == high(uint64)-3
andHigh = high(uint)
, subtracting thelower inclusive-bound (-4) from the upper inclusive-bound (-1) both
didn't cause and overflow and also resulted in the correct value (3).
However, when
Low == uint64 high(int64)
andHigh == Low + 4
, thisdoesn't work. When the operand to an operation involving such sets was a
literal integer outside the
int64
range, the compiler crashed - whenit was a run-time value, the VM erroneously reported an overflow.
The logic for generating the code for loading set elements now uses
Int128
to get around over- and underflow problems, and theSubu
operation is emitted for offsetting unsigned run-time values.
In addition, generating the bytecode for loading literal unsigned
integers now happens via
loadInt
, meaning that the values are nowloaded via
LdImmInt
if they're less than 2^23. Previously, literalunsigned integers were always loaded via the slightly less efficient
LdConst
operation.