|
| 1 | +"""Atheris fuzz target: ContractFunctionParameters ABI encoding.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import sys |
| 6 | + |
| 7 | +import atheris |
| 8 | + |
| 9 | + |
| 10 | +with atheris.instrument_imports(): |
| 11 | + from eth_abi.exceptions import EncodingTypeError, ValueOutOfBounds |
| 12 | + |
| 13 | + from hiero_sdk_python import ContractFunctionParameters |
| 14 | + |
| 15 | + |
| 16 | +def TestOneInput(data: bytes) -> None: |
| 17 | + """Feed arbitrary bytes into ContractFunctionParameters encoding paths.""" |
| 18 | + fdp = atheris.FuzzedDataProvider(data) |
| 19 | + choice = fdp.ConsumeIntInRange(0, 9) |
| 20 | + params = ContractFunctionParameters() |
| 21 | + if choice == 0: |
| 22 | + params.add_bool(fdp.ConsumeBool()) |
| 23 | + elif choice == 1: |
| 24 | + # add_address expects a 20-byte hex string or bytes |
| 25 | + params.add_address(fdp.ConsumeBytes(20).hex()) |
| 26 | + elif choice == 2: |
| 27 | + params.add_string(fdp.ConsumeUnicodeNoSurrogates(256)) |
| 28 | + elif choice == 3: |
| 29 | + params.add_bytes(fdp.ConsumeBytes(256)) |
| 30 | + elif choice == 4: |
| 31 | + params.add_bytes32(fdp.ConsumeBytes(32)) |
| 32 | + elif choice == 5: |
| 33 | + count = fdp.ConsumeIntInRange(0, 8) |
| 34 | + params.add_bool_array([fdp.ConsumeBool() for _ in range(count)]) |
| 35 | + elif choice == 6: |
| 36 | + count = fdp.ConsumeIntInRange(0, 8) |
| 37 | + params.add_string_array([fdp.ConsumeUnicodeNoSurrogates(64) for _ in range(count)]) |
| 38 | + elif choice == 7: |
| 39 | + count = fdp.ConsumeIntInRange(0, 8) |
| 40 | + params.add_bytes_array([fdp.ConsumeBytes(32) for _ in range(count)]) |
| 41 | + elif choice == 8: |
| 42 | + count = fdp.ConsumeIntInRange(0, 8) |
| 43 | + params.add_address_array([fdp.ConsumeBytes(20).hex() for _ in range(count)]) |
| 44 | + else: |
| 45 | + count = fdp.ConsumeIntInRange(0, 8) |
| 46 | + params.add_bytes32_array([fdp.ConsumeBytes(32) for _ in range(count)]) |
| 47 | + try: |
| 48 | + params.to_bytes() |
| 49 | + except (TypeError, ValueError, EncodingTypeError, ValueOutOfBounds): |
| 50 | + # Malformed fuzz input can trigger expected validation/ABI encoding failures. |
| 51 | + pass |
| 52 | + |
| 53 | + |
| 54 | +atheris.Setup(sys.argv, TestOneInput) |
| 55 | +atheris.Fuzz() |
0 commit comments