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 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
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
22 changes: 21 additions & 1 deletion vyper/codegen/ir_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,29 @@

return ret

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

@property
def is_precompile_call(self):
# whitelist for "safe" calls
assert self.is_call_opcode
target = self.args[1].value
PRECOMPILES = (

Check warning on line 491 in vyper/codegen/ir_node.py

View check run for this annotation

Codecov / codecov/patch

vyper/codegen/ir_node.py#L489-L491

Added lines #L489 - L491 were not covered by tests
0x01, # builtin ecrecover
0x02, # builtin sha256
0x04, # identify precompile
0x06, # builtin ecadd
0x07, # builtin ecmul
0x000000000000000000636F6E736F6C652E6C6F67, # builtin print
)
return isinstance(target, int) and target in PRECOMPILES

Check warning on line 499 in vyper/codegen/ir_node.py

View check run for this annotation

Codecov / codecov/patch

vyper/codegen/ir_node.py#L499

Added line #L499 was not covered by tests

@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