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: guard against state-modifying expressions in range expression #3546

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 10 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
23 changes: 12 additions & 11 deletions tests/parser/exceptions/test_constancy_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,6 @@ def foo() -> int128:
def foo() -> int128:
x: address = create_minimal_proxy_to(0x1234567890123456789012345678901234567890, value=9)
return 5""",
# test constancy in range expressions
"""
glob: int128
@internal
def foo() -> int128:
self.glob += 1
return 5
@external
def bar():
for i in range(self.foo(), self.foo() + 1):
pass""",
"""
glob: int128
@internal
Expand Down Expand Up @@ -120,6 +109,18 @@ def foo(f: Foo) -> Foo:
f.a[1] = [0, 1]
return f
""",
# test constancy in range expressions
"""
glob: int128
@internal
def foo() -> int128:
self.glob += 1
return 5
@external
def bar():
for i in range(self.foo(), self.foo() + 1):
pass
""",
],
)
def test_immutability_violations(bad_code):
Expand Down
144 changes: 143 additions & 1 deletion tests/parser/syntax/test_for_range.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from vyper import compiler
from vyper.exceptions import StructureException
from vyper.exceptions import ImmutableViolation, StructureException

fail_list = [
(
Expand All @@ -12,6 +12,108 @@ def foo():
pass
""",
StructureException,
),
(
"""
interface A:
def foo()-> uint256: nonpayable

@external
def bar(x:address):
a: A = A(x)
for i in range(a.foo(), bound=12):
pass
""",
ImmutableViolation,
),
(
"""
interface A:
def foo()-> uint256: nonpayable

@external
def bar(x:address):
a: A = A(x)
for i in range(max(a.foo(), 123), bound=12):
pass
""",
ImmutableViolation,
),
(
"""
interface A:
def foo()-> uint256: nonpayable

@external
def bar(x:address):
a: A = A(x)
for i in range(a.foo(), a.foo() + 1):
pass
""",
ImmutableViolation,
),
(
"""
interface A:
def foo()-> uint256: nonpayable

@external
def bar(x:address):
a: A = A(x)
for i in range(min(a.foo(), 123), min(a.foo(), 123) + 1):
pass
""",
ImmutableViolation,
),
# Cannot call `pop()` in for range because it modifies state
(
"""
arr: DynArray[uint256, 10]
@external
def test()-> (DynArray[uint256, 6], DynArray[uint256, 10]):
b: DynArray[uint256, 6] = []
self.arr = [1,0]
for i in range(self.arr.pop(), self.arr.pop() + 2):
b.append(i)
return b, self.arr
""",
ImmutableViolation,
),
(
"""
@external
def bar(x:address):
for i in range(1 if raw_call(x, b'', revert_on_failure=False) else 2 , bound = 12):
pass
""",
ImmutableViolation,
),
(
"""
@external
def foo(a: address):
for i in range(1 if convert(create_minimal_proxy_to(a), uint256) > 2 else 2, bound=12):
pass
""",
ImmutableViolation,
),
(
"""
@external
def foo(a: address):
for i in range(1 if convert(create_copy_of(a), uint256) > 2 else 2, bound=12):
pass
""",
ImmutableViolation,
),
(
"""
@external
def foo(a: address):
for i in range(1 if convert(create_from_blueprint(a), uint256) > 2 else 2, bound=12):
pass
""",
ImmutableViolation,
)
]

Expand Down Expand Up @@ -51,6 +153,46 @@ def kick_foos():
for foo in self.foos:
foo.kick()
""",
"""
interface A:
def foo()-> uint256: view

@external
def bar(x:address):
a: A = A(x)
for i in range(a.foo(), bound=12):
pass
""",
"""
interface A:
def foo()-> uint256: view

@external
def bar(x:address):
a: A = A(x)
for i in range(max(a.foo(), 123), bound=12):
pass
""",
"""
interface A:
def foo()-> uint256: view

@external
def bar(x:address):
a: A = A(x)
for i in range(a.foo(), a.foo() + 1):
pass
""",
"""
interface A:
def foo()-> uint256: view

@external
def bar(x:address):
a: A = A(x)
for i in range(min(a.foo(), 123), min(a.foo(), 123) + 1):
pass
""",
]


Expand Down
22 changes: 22 additions & 0 deletions vyper/semantics/analysis/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,28 @@ def visit_For(self, node):
if not type_list:
raise TypeMismatch("Iterator values are of different types", node.iter)

# Check for state-modifying expressions in `range` expression
range_call_nodes = node.iter.get_descendants(vy_ast.Call)
for call_node in range_call_nodes:
call_type = get_exact_type_from_node(call_node.func)
disallowed_builtins = (
"raw_call",
"create_minimal_proxy_to",
"create_copy_of",
"create_from_blueprint",
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think a better option is to add the is_modifying attribute from member functions to builtins, which would allow us to check both member functions and builtin functions at the same time. Thoughts?

if (
# state-modifying internal and external calls
(isinstance(call_type, ContractFunctionT) and call_type.is_mutable)
# `pop` on dynamic arrays
or (isinstance(call_type, MemberFunctionT) and call_type.is_modifying)
# state-modifying builtin functions
or call_node.get("func.id") in disallowed_builtins
):
raise ImmutableViolation(
"Cannot call state-modifying functions for `range` expression", call_node
)

else:
# iteration over a variable or literal list
if isinstance(node.iter, vy_ast.List) and len(node.iter.elements) == 0:
Expand Down