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

fix: block memory allocation overflow #3639

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion vyper/codegen/memory_allocator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from vyper.exceptions import CompilerPanic
from vyper.exceptions import CompilerPanic, MemoryAllocationException
from vyper.utils import MemoryPositions


Expand Down Expand Up @@ -110,6 +110,13 @@ def _expand_memory(self, size: int) -> int:
before_value = self.next_mem
self.next_mem += size
self.size_of_mem = max(self.size_of_mem, self.next_mem)

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)"
Copy link
Member

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)

Copy link
Member Author

Choose a reason for hiding this comment

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

good point, thanks

Copy link
Member Author

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.

Copy link
Member

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

Copy link
Member Author

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?

Copy link
Member

Choose a reason for hiding this comment

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

or do log2

Copy link
Member Author

@charles-cooper charles-cooper Oct 4, 2023

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.

Copy link
Member

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'

)

return before_value

def deallocate_memory(self, pos: int, size: int) -> None:
Expand Down
4 changes: 4 additions & 0 deletions vyper/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ class StorageLayoutException(VyperException):
"""Invalid slot for the storage layout overrides"""


class MemoryAllocationException(VyperException):
"""Tried to allocate too much memory"""


class JSONError(Exception):

"""Invalid compiler input JSON."""
Expand Down
Loading