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[codegen]: fix false positive in risky call detection #4160

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 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
52 changes: 52 additions & 0 deletions tests/functional/codegen/types/test_dynamic_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1903,3 +1903,55 @@ def foo():
c = get_contract(code)
with tx_failed():
c.foo()


# should not trip risky overlap detector
def test_risky_call_precompile():
code = """
x: DynArray[uint256, 32]

@deploy
def __init__():
self.x = [1, 1, 1]

@internal
def bar() -> uint256:
self.x[0] = 1
return 0

@external
def foo() -> uint256:
self.x[
abi_decode(
slice(
concat(
abi_encode(
empty(uint256),
method_id=method_id("foo()")),
empty(bytes32)
), 0, 32),
(uint256)
)] = self.bar()
return 0
"""
assert compile_code(code) is not None


# should not trip risky overlap detector
def test_risky_call_precompile2():
code = """
x: DynArray[uint256, 32]

@internal
def bar() -> uint256:
a: DynArray[uint256, 100] = []
b: DynArray[uint256, 100] = []
a = b
return 0

@external
def foo() -> uint256:
c: uint256 = self.x[self.bar()]
return 0
"""
assert compile_code(code) is not None
16 changes: 15 additions & 1 deletion vyper/codegen/ir_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from vyper.semantics.types import VyperType
from vyper.utils import VALID_IR_MACROS, ceil32

PRECOMPILE_RANGE = (1, 10)
Copy link
Collaborator

Choose a reason for hiding this comment

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

might not be true for some L2s

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 thought about this (see also eip-7587) but i think it's ok.

  1. we can't predict every precompile that will be in use by an L2
  2. precompiles could have arbitrary effects
  3. our main goal here is to protect against precompiles which could be generated in the compiler. user-generated calls to precompiles are fair game (we can consider them "unsafe")


# Set default string representation for ints in IR output.
AS_HEX_DEFAULT = False

Expand Down Expand Up @@ -479,9 +481,21 @@ def variable_writes(self):

return ret

@property
def is_call_opcode(self):
return self.value in ("call", "staticcall", "delegatecall")

@property
def is_precompile_call(self):
assert self.is_call_opcode
target = self.args[1].value
lo, hi = PRECOMPILE_RANGE
return isinstance(target, int) and (lo <= target <= hi)

@cached_property
def contains_risky_call(self):
ret = self.value in ("call", "delegatecall", "staticcall", "create", "create2")
ret = self.value in ("create", "create2")
ret |= self.is_call_opcode and not self.is_precompile_call

for arg in self.args:
ret |= arg.contains_risky_call
Expand Down
Loading