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 all commits
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
12 changes: 11 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 @@ -46,6 +46,8 @@ class MemoryAllocator:

next_mem: int

_ALLOCATION_LIMIT: int = 2**64

def __init__(self, start_position: int = MemoryPositions.RESERVED_MEMORY):
"""
Initializer.
Expand Down Expand Up @@ -110,6 +112,14 @@ 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 >= self._ALLOCATION_LIMIT:
# this should not be caught
raise MemoryAllocationException(
f"Tried to allocate {self.size_of_mem} bytes! "
f"(limit is {self._ALLOCATION_LIMIT} (2**64) bytes)"
)

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