Skip to content

Commit

Permalink
add more robust fuzzing, better slice errors
Browse files Browse the repository at this point in the history
  • Loading branch information
iFrostizz committed Nov 10, 2023
1 parent b3ad481 commit 162d6b3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
20 changes: 20 additions & 0 deletions tests/functional/builtins/codegen/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import pytest
from hypothesis import given, settings

from vyper import ast as vy_ast
from vyper.builtins import functions as vy_fn
from vyper.compiler.settings import OptimizationLevel
from vyper.exceptions import ArgumentException, TypeMismatch

Expand Down Expand Up @@ -473,3 +475,21 @@ def test_comptime(get_contract, code, result):
assert ret.hex() == result
else:
assert ret == result


error_slice = [
"slice(0x00, 0, 1)",
'slice("why hello! how are you?", 32, 1)',
'slice("why hello! how are you?", -1, 1)',
'slice("why hello! how are you?", 4, 0)',
'slice("why hello! how are you?", 0, 33)',
'slice("why hello! how are you?", 16, 17)',
]


@pytest.mark.parametrize("code", error_slice)
def test_slice_error(code):
vyper_ast = vy_ast.parse_to_ast(code)
old_node = vyper_ast.body[0].value
with pytest.raises(ArgumentException):
vy_fn.DISPATCH_TABLE["slice"].evaluate(old_node)
67 changes: 62 additions & 5 deletions tests/functional/builtins/folding/test_slice.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import string

import pytest
from hypothesis import given, settings
from hypothesis import strategies as st
Expand All @@ -7,6 +9,10 @@
from vyper.exceptions import ArgumentException


def normalize_bytes(data):
return bytes(int.from_bytes(data, "big"))


@pytest.mark.fuzzing
@settings(max_examples=50)
@given(
Expand All @@ -33,11 +39,7 @@ def foo(a: bytes32) -> Bytes[{le}]:

s *= 2
le *= 2
assert (
int.from_bytes(contract.foo(a), "big")
== int.from_bytes(new_node.value, "big")
== int(a[2:][s : (s + le)], 16)
)
assert normalize_bytes(contract.foo(a)) == new_node.value == bytes.fromhex(a[2:][s : (s + le)])


@pytest.mark.fuzzing
Expand All @@ -61,3 +63,58 @@ def test_slice_bytesnot32(a, s, le):
old_node = vyper_ast.body[0].value
with pytest.raises(ArgumentException):
vy_fn.DISPATCH_TABLE["slice"].evaluate(old_node)


@pytest.mark.fuzzing
@settings(max_examples=50)
@given(
a=st.binary(min_size=1, max_size=100),
s=st.integers(min_value=0, max_value=99),
le=st.integers(min_value=1, max_value=100),
)
def test_slice_dynbytes(get_contract, a, s, le):
s = s % len(a)
le = min(len(a), len(a) - s, le)

source = f"""
@external
def foo(a: Bytes[100]) -> Bytes[{le}]:
return slice(a, {s}, {le})
"""
contract = get_contract(source)

vyper_ast = vy_ast.parse_to_ast(f"slice({a}, {s}, {le})")
old_node = vyper_ast.body[0].value
new_node = vy_fn.DISPATCH_TABLE["slice"].evaluate(old_node)

assert contract.foo(a) == new_node.value == a[s : (s + le)]


valid_char = [
char for char in string.printable if char not in (string.whitespace.replace(" ", "") + '"\\')
]


@pytest.mark.fuzzing
@settings(max_examples=50)
@given(
a=st.text(alphabet=valid_char, min_size=1, max_size=100),
s=st.integers(min_value=0, max_value=99),
le=st.integers(min_value=1, max_value=100),
)
def test_slice_string(get_contract, a, s, le):
s = s % len(a)
le = min(len(a), len(a) - s, le)

source = f"""
@external
def foo(a: String[100]) -> String[{le}]:
return slice(a, {s}, {le})
"""
contract = get_contract(source)

vyper_ast = vy_ast.parse_to_ast(f'slice("{a}", {s}, {le})')
old_node = vyper_ast.body[0].value
new_node = vy_fn.DISPATCH_TABLE["slice"].evaluate(old_node)

assert contract.foo(a) == new_node.value == a[s : (s + le)]
1 change: 0 additions & 1 deletion vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ def evaluate(self, node):
else:
length = len(lit.value) // 2 - 1
if length != 32:
# raise UnfoldableNode
raise ArgumentException("Length can only be of 32", lit)
st_val *= 2
le_val *= 2
Expand Down

0 comments on commit 162d6b3

Please sign in to comment.