-
-
Notifications
You must be signed in to change notification settings - Fork 804
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
fix: block memory allocation overflow #3639
Conversation
this fixes potential overflow bugs in pointer calculation by blocking memory allocation above a certain size. the size limit is set at `2**64`, which is the size of addressable memory on physical machines. practically, for EVM use cases, we could limit at a much smaller number (like `2**24`), but we want to allow for "exotic" targets which may allow much more addressable memory.
vyper/codegen/memory_allocator.py
Outdated
if self.size_of_mem >= 2**64: | ||
# this should not be caught | ||
raise MemoryAllocationException( | ||
"Tried to allocate {self.size_of_mem} bytes! (limit is 2**32 bytes)" |
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.
probably should make it a constant and use it here as an f-string (and in the if statement)
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, thanks
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, right, i wanted to do it that way in the error message because 2**32
is easier to read than 4294967296.
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, right, i wanted to do it that way in the error message because
2**32
is easier to read than 4294967296.
do the constant as a string and then parse it to int lol
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 think eval()
is a bit overkill here :P unless you are thinking there is a cleaner way of parsing the string?
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.
or do log2
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 don't know about that. that results in an error message like
vyper.exceptions.MemoryAllocationException: Tried to allocate 18446744073709551712 bytes! (limit is 2**64.0 bytes)
and since floating point math is not very precise, we get the same error message for instance with _ALLOCATION_LIMIT = 2**64 - 1
. so there is still the problem of drift between the error message and the value.
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.
In [5]: MEM_LIMIT = 2**64
In [6]: f"2**{math.log2(MEM_LIMIT):2.0f}"
Out[6]: '2**64'
Codecov Report
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. @@ Coverage Diff @@
## master #3639 +/- ##
==========================================
- Coverage 89.13% 89.09% -0.05%
==========================================
Files 86 86
Lines 11463 11467 +4
Branches 2606 2607 +1
==========================================
- Hits 10218 10216 -2
- Misses 826 831 +5
- Partials 419 420 +1
... and 1 file with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
What I did
fix #3637
How I did it
this fixes potential overflow bugs in pointer calculation by blocking memory allocation above a certain size. the size limit is set at
2**64
, which is the size of addressable memory on physical machines.practically, for EVM use cases, we could limit at a much smaller number (like
2**24
), but we want to allow for "exotic" targets which may allow much more addressable memory.How to verify it
Commit message
Commit message for the final, squashed PR. (Optional, but reviewers will appreciate it! Please see our commit message style guide for what we would ideally like to see in a commit message.)
Description for the changelog
Cute Animal Picture