Skip to content

Instantly share code, notes, and snippets.

@charles-cooper
Last active January 16, 2024 14:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charles-cooper/0d774c61d479aa8d92545f314eec2b0f to your computer and use it in GitHub Desktop.
Save charles-cooper/0d774c61d479aa8d92545f314eec2b0f to your computer and use it in GitHub Desktop.
address sanitizer
# usage:
# install asan fork of vyper 0.3.10 (`pip install git+https://github.com/charles-cooper/vyper@asan`)
# change `import boa` to `import boa; import boa_asan` in scripts
from eth.exceptions import VMError
import boa.environment
class MemoryAccessViolation(VMError):
pass
def _unpack_asan_ptr(asan_ptr, size):
lo, hi, ptr = (asan_ptr >> 192), ((asan_ptr >> 128) & (2**64 - 1)), asan_ptr & (2**128 - 1)
if lo == hi == 0:
# raw pointers, not generated by the allocator
return asan_ptr
if lo >= hi:
raise MemoryAccessViolation(f"bad asan ptr. | {lo} | {ptr} | {hi} | ({asan_ptr}) |")
if ptr < lo:
raise MemoryAccessViolation(f"tried to access {ptr} but bounds are | {lo} | ... | {hi} |")
if ptr + size > hi:
raise MemoryAccessViolation(f"tried to access {ptr + size} but bounds are | {lo} | ... | {hi} |")
return ptr
# a py-evm eth.vm.Memory compatible implementation of memory
# which takes special pointers generated in vyper's `--asan`
# mode and sanitizes before every read and write
class SanitizingComputation(boa.environment.titanoboa_computation):
def extend_memory(self, asan_ptr, size):
start_position = _unpack_asan_ptr(asan_ptr, size)
super().extend_memory(start_position, size)
def memory_read(self, asan_ptr, size):
start_position = _unpack_asan_ptr(asan_ptr, size)
return super().memory_read(start_position, size)
def memory_read_bytes(self, asan_ptr, size):
start_position = _unpack_asan_ptr(asan_ptr, size)
return super().memory_read_bytes(start_position, size)
def memory_write(self, asan_ptr, size, value):
start_position = _unpack_asan_ptr(asan_ptr, size)
return super().memory_write(start_position, size, value)
# monkey patch
boa.environment.titanoboa_computation = SanitizingComputation
boa.env._init_vm() # reinitialize vm.state.computation_class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment