|
1 | 1 | # Fuzzlib |
2 | | -Fuzzlib is an unopinionated Solidity library designed for both stateful and stateless fuzzing of smart contracts. It provides a collection of useful functions and libraries designed to streamline fuzzing harness development with Echidna, Medusa, and Foundry, making the process easier and more efficient. |
3 | 2 |
|
4 | | -**Fuzzlib is still in alpha and under active development with potential future breaking changes!** |
| 3 | +General purpose unopinionated Solidity fuzzing library for stateful and stateless fuzzing. Compatible with Echidna, Medusa, and Foundry. |
5 | 4 |
|
6 | | -## Branches |
7 | | -* `main`: Latest `fl.` namespace |
8 | | -* `v0_2`: Old namespace version for backwards compatibility |
| 5 | +Provides common utilities for fuzz testing through a simple `fl` namespace: assertions, value clamping, logging, math operations, and more. |
9 | 6 |
|
| 7 | +## Key Features |
10 | 8 |
|
11 | | -## Known limitations |
12 | | -- Signed integer clamping is limited to int128 range to avoid overflow issues in range calculations |
| 9 | +- **Basic Assertions**: Helpers for common test conditions and equality checks |
| 10 | +- **Advanced Assertions**: Utilities like error handling via `errAllow` for expected failures |
| 11 | +- **Value Clamping**: Clamp values to ranges with uniform distribution for better fuzzing |
| 12 | +- **Logging Utilities**: Unified logging for debugging and tracing |
| 13 | +- **Math Utilities**: Operations like min, max, absolute value, and difference calculations |
| 14 | +- **Random Utilities**: Fisher-Yates array shuffling |
| 15 | +- **Function Call Helpers**: Utilities for making function calls with actor pranking |
| 16 | +- **Comprehensive Testing**: Extensive test suite with both unit and fuzz tests |
| 17 | +- **Well-Documented**: Clear and complete, following OpenZeppelin-style conventions |
13 | 18 |
|
| 19 | +## Installation |
14 | 20 |
|
| 21 | +### Using Foundry |
| 22 | + |
| 23 | +```bash |
| 24 | +forge install perimetersec/fuzzlib |
| 25 | +``` |
| 26 | + |
| 27 | +### Using npm |
| 28 | + |
| 29 | +```bash |
| 30 | +npm install @perimetersec/fuzzlib |
| 31 | +``` |
| 32 | + |
| 33 | +Add to your `remappings.txt`: |
| 34 | +``` |
| 35 | +fuzzlib/=lib/fuzzlib/src/ |
| 36 | +``` |
| 37 | + |
| 38 | +## Quick Start |
| 39 | + |
| 40 | +Create a simple fuzzing test by extending `FuzzBase`: |
| 41 | + |
| 42 | +```solidity |
| 43 | +import {FuzzBase} from "fuzzlib/FuzzBase.sol"; |
| 44 | +
|
| 45 | +contract MyFuzzer is FuzzBase { |
| 46 | + function testMath(uint256 a, uint256 b) public { |
| 47 | + // Clamp inputs to reasonable ranges |
| 48 | + uint256 x = fl.clamp(a, 0, 1000); |
| 49 | + uint256 y = fl.clamp(b, 0, 1000); |
| 50 | + |
| 51 | + // Log for debugging |
| 52 | + fl.log("Testing max function. x =", x); |
| 53 | + fl.log("Testing max function. y =", y); |
| 54 | + |
| 55 | + // Test mathematical properties |
| 56 | + fl.gte(fl.max(x, y), x, "Max should be >= x"); |
| 57 | + fl.gte(fl.max(x, y), y, "Max should be >= y"); |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | + |
| 63 | +## Function Reference |
| 64 | + |
| 65 | +### Basic Assertions |
| 66 | + |
| 67 | +```solidity |
| 68 | +// Fundamental assertions |
| 69 | +fl.t(exists, "Property X exists"); |
| 70 | +fl.eq(result, 100, "Result should equal 100"); |
| 71 | +fl.neq(userA, userB, "Users should be different"); |
| 72 | +
|
| 73 | +// Comparison assertions |
| 74 | +fl.gt(balance, 1000, "Balance should be greater than 1000"); |
| 75 | +fl.gte(amount, 50, "Amount should be greater than or equal to 50"); |
| 76 | +fl.lt(fee, 100, "Fee should be less than 100"); |
| 77 | +fl.lte(price, 500, "Price should be less than or equal to 500"); |
| 78 | +``` |
| 79 | + |
| 80 | +### Advanced Assertions |
| 81 | + |
| 82 | +```solidity |
| 83 | +// Allow specific require messages |
| 84 | +string[] memory allowedMessages = new string[](1); |
| 85 | +allowedMessages[0] = "Insufficient balance"; |
| 86 | +fl.errAllow(errorData, allowedMessages, "Message X should be allowed"); |
| 87 | +
|
| 88 | +// Allow specific custom errors |
| 89 | +bytes4[] memory allowedErrors = new bytes4[](1); |
| 90 | +allowedErrors[0] = CustomError.selector; |
| 91 | +fl.errAllow(errorSelector, allowedErrors, "Error X should be allowed"); |
| 92 | +
|
| 93 | +// Combined error handling |
| 94 | +fl.errAllow(errorData, allowedMessages, allowedErrors, "Either should be allowed"); |
| 95 | +``` |
| 96 | + |
| 97 | +### Value Clamping |
| 98 | + |
| 99 | +```solidity |
| 100 | +// Value clamping with uniform distribution |
| 101 | +uint256 clamped = fl.clamp(inputValue, 0, 100); |
| 102 | +
|
| 103 | +// Clamp to greater than value |
| 104 | +uint256 clampedGt = fl.clampGt(inputValue, 50); |
| 105 | +
|
| 106 | +// Clamp to greater than or equal |
| 107 | +uint256 clampedGte = fl.clampGte(inputValue, 50); |
| 108 | +
|
| 109 | +// Clamp to less than value |
| 110 | +uint256 clampedLt = fl.clampLt(inputValue, 100); |
| 111 | +
|
| 112 | +// Clamp to less than or equal |
| 113 | +uint256 clampedLte = fl.clampLte(inputValue, 100); |
| 114 | +``` |
| 115 | + |
| 116 | +### Logging |
| 117 | + |
| 118 | +```solidity |
| 119 | +// Simple logging |
| 120 | +fl.log("Testing scenario"); |
| 121 | +
|
| 122 | +// Logging with values |
| 123 | +fl.log("Balance:", balance); |
| 124 | +fl.log("User count:", 42); |
| 125 | +
|
| 126 | +// Failure logging |
| 127 | +fl.logFail("This test failed"); |
| 128 | +fl.logFail("Invalid amount:", amount); |
| 129 | +``` |
| 130 | + |
| 131 | +### Math Utilities |
| 132 | + |
| 133 | +```solidity |
| 134 | +// Min/max operations |
| 135 | +uint256 maximum = fl.max(150, 300); |
| 136 | +int256 minimum = fl.min(-50, 25); |
| 137 | +
|
| 138 | +// Absolute value and difference |
| 139 | +uint256 absolute = fl.abs(-42); |
| 140 | +uint256 difference = fl.diff(100, 75); |
| 141 | +``` |
| 142 | + |
| 143 | +### Random Utilities |
| 144 | + |
| 145 | +```solidity |
| 146 | +// Shuffle arrays |
| 147 | +uint256[] memory array = new uint256[](10); |
| 148 | +fl.shuffleArray(array, entropy); |
| 149 | +``` |
| 150 | + |
| 151 | +### Function Call Helpers |
| 152 | + |
| 153 | +```solidity |
| 154 | +// Make function calls |
| 155 | +bytes memory result = fl.doFunctionCall( |
| 156 | + address(target), |
| 157 | + abi.encodeWithSignature("getValue()"), |
| 158 | + msg.sender // actor |
| 159 | +); |
| 160 | +
|
| 161 | +// Calls with automatic pranking |
| 162 | +(bool success, bytes memory data) = fl.doFunctionCall( |
| 163 | + address(target), |
| 164 | + abi.encodeWithSignature("transfer(address,uint256)", recipient, amount), |
| 165 | + sender |
| 166 | +); |
| 167 | +
|
| 168 | +// Static calls (view functions) |
| 169 | +(bool success, bytes memory data) = fl.doFunctionStaticCall( |
| 170 | + address(target), |
| 171 | + abi.encodeWithSignature("balanceOf(address)", user) |
| 172 | +); |
| 173 | +``` |
| 174 | + |
| 175 | +## Known Limitations |
| 176 | + |
| 177 | +- **Signed Integer Clamping**: Limited to `int128` range to avoid overflow issues in range calculations |
| 178 | +- **Gas Optimization**: Library prioritizes functionality over gas optimization |
| 179 | +- **Function Selector Clashing**: If the error selector clashes with `Error(string)` when using errAllow, unexpected behavior may happen |
| 180 | + |
| 181 | + |
| 182 | +## Roadmap |
| 183 | + |
| 184 | +- [ ] Support for more platforms |
| 185 | +- [ ] Add more helper functions |
| 186 | +- [ ] Performance optimizations |
| 187 | + |
| 188 | + |
| 189 | +## Contributing |
| 190 | + |
| 191 | +We welcome contributions! Please see our [Contributing Guidelines](CONTRIBUTING.md) for details. |
| 192 | + |
| 193 | + |
| 194 | +## License |
| 195 | + |
| 196 | +This project is licensed under the GNU General Public License v3.0. See the [LICENSE](LICENSE) file for details. |
| 197 | + |
| 198 | +Some portions of this code are modified from [Crytic Properties](https://github.com/crytic/properties/blob/main/contracts/util/PropertiesHelper.sol), which is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0). |
| 199 | + |
| 200 | +Some portions of this code are modified from [OpenZeppelin Contracts](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeCast.sol), which is licensed under the MIT License. |
| 201 | + |
| 202 | + |
| 203 | +## Disclaimer |
| 204 | + |
| 205 | +This software is provided as-is without warranty. The main branch contains new and experimental features that may be unstable. For production use, we recommend using official tagged releases which have been thoroughly tested. While we are not responsible for any bugs or issues, we maintain a bug bounty program that applies to official releases only. |
0 commit comments