Last active
January 16, 2024 14:36
-
-
Save charles-cooper/0d774c61d479aa8d92545f314eec2b0f to your computer and use it in GitHub Desktop.
address sanitizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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