Skip to content

Commit

Permalink
fix: block memory allocation overflow
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
charles-cooper committed Oct 4, 2023
1 parent 9136169 commit 9c71339
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
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)"
)

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

0 comments on commit 9c71339

Please sign in to comment.