|
| 1 | +# Fuzzing lib_c_list |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This fuzzer tests the generic linked list library (`lib_c_list`) for memory safety issues, edge cases, and potential crashes. It exercises all list operations including insertion, removal, sorting, and traversal. |
| 6 | + |
| 7 | +## Operations Tested |
| 8 | + |
| 9 | +The fuzzer uses the lower 4 bits of each input byte to select operations: |
| 10 | + |
| 11 | +| Op Code | Operation | Description | |
| 12 | +|---------|-------------------|------------------------------------------------| |
| 13 | +| 0x00 | `push_front` | Add node at the beginning | |
| 14 | +| 0x01 | `push_back` | Add node at the end | |
| 15 | +| 0x02 | `pop_front` | Remove first node | |
| 16 | +| 0x03 | `pop_back` | Remove last node | |
| 17 | +| 0x04 | `insert_after` | Insert node after reference node | |
| 18 | +| 0x05 | `insert_before` | Insert node before reference node | |
| 19 | +| 0x06 | `remove` | Remove specific node | |
| 20 | +| 0x07 | `clear` | Remove all nodes | |
| 21 | +| 0x08 | `sort` | Sort list by node ID | |
| 22 | +| 0x09 | `size` | Get list size | |
| 23 | +| 0x0A | `traverse` | Traverse and verify integrity (cycle detection)| |
| 24 | + |
| 25 | +## Features |
| 26 | + |
| 27 | +### Safety Checks |
| 28 | + |
| 29 | +- **Cycle detection**: Prevents infinite loops in traversal |
| 30 | +- **Node tracking**: All allocated nodes are tracked for cleanup |
| 31 | +- **Memory leak prevention**: Automatic cleanup at end of fuzzing iteration |
| 32 | +- **Integrity verification**: Validates list structure during traversal |
| 33 | + |
| 34 | +### Test Data |
| 35 | + |
| 36 | +Each node contains: |
| 37 | + |
| 38 | +- Unique ID (auto-incremented) |
| 39 | +- 16 bytes of fuzzer-provided data |
| 40 | +- Standard list node structure |
| 41 | + |
| 42 | +### Limits |
| 43 | + |
| 44 | +- `MAX_NODES`: 1000 (prevents excessive memory usage) |
| 45 | +- `MAX_TRACKERS`: 100 (limits number of tracked nodes) |
| 46 | +- Maximum input length: 256 bytes (configurable) |
| 47 | + |
| 48 | +## Building |
| 49 | + |
| 50 | +From the SDK root: |
| 51 | + |
| 52 | +```bash |
| 53 | +cd fuzzing |
| 54 | +mkdir -p build && cd build |
| 55 | +cmake -S .. -B . -DCMAKE_C_COMPILER=clang -DSANITIZER=address -DBOLOS_SDK=/path/to/sdk |
| 56 | +cmake --build . --target fuzz_c_list |
| 57 | +``` |
| 58 | + |
| 59 | +## Running |
| 60 | + |
| 61 | +### Basic run |
| 62 | + |
| 63 | +```bash |
| 64 | +./fuzz_c_list |
| 65 | +``` |
| 66 | + |
| 67 | +### With specific options |
| 68 | + |
| 69 | +```bash |
| 70 | +# Run for 10000 iterations |
| 71 | +./fuzz_c_list -runs=10000 |
| 72 | + |
| 73 | +# Limit input size to 128 bytes |
| 74 | +./fuzz_c_list -max_len=128 |
| 75 | + |
| 76 | +# Use corpus directory |
| 77 | +./fuzz_c_list corpus/ |
| 78 | + |
| 79 | +# Timeout per input (in seconds) |
| 80 | +./fuzz_c_list -timeout=10 |
| 81 | +``` |
| 82 | + |
| 83 | +### Using the helper script |
| 84 | + |
| 85 | +```bash |
| 86 | +cd /path/to/sdk/fuzzing |
| 87 | +./local_run.sh --build=1 --fuzzer=build/fuzz_c_list --j=4 --run-fuzzer=1 --BOLOS_SDK=/path/to/sdk |
| 88 | +``` |
| 89 | + |
| 90 | +## Corpus |
| 91 | + |
| 92 | +Initial corpus files can be placed in: |
| 93 | + |
| 94 | +```bash |
| 95 | +fuzzing/harness/fuzz_c_list/ |
| 96 | +``` |
| 97 | + |
| 98 | +Example corpus files: |
| 99 | + |
| 100 | +- Simple operations: `\x00\x00...` (push_front operations) |
| 101 | +- Mixed operations: `\x00...\x01...\x02` (push_front, push_back, pop_front) |
| 102 | +- Complex sequences: Various operation combinations |
| 103 | + |
| 104 | +## Debugging |
| 105 | + |
| 106 | +Enable debug output by uncommenting `#define DEBUG_CRASH` in `fuzzer_c_list.c`: |
| 107 | + |
| 108 | +```c |
| 109 | +#define DEBUG_CRASH |
| 110 | +``` |
| 111 | + |
| 112 | +This will print: |
| 113 | + |
| 114 | +- Node creation/deletion |
| 115 | +- Operation execution |
| 116 | +- List size changes |
| 117 | +- Cleanup operations |
| 118 | + |
| 119 | +## Crash Analysis |
| 120 | + |
| 121 | +If a crash is found: |
| 122 | + |
| 123 | +1. The fuzzer will save the crashing input to `crash-*` or `leak-*` files |
| 124 | +2. Reproduce the crash: |
| 125 | + |
| 126 | + ```bash |
| 127 | + ./fuzz_c_list crash-HASH |
| 128 | + ``` |
| 129 | + |
| 130 | +3. Debug with AddressSanitizer output |
| 131 | +4. Fix the issue in `lib_c_list/c_list.c` |
| 132 | +5. Verify fix by re-running fuzzer |
| 133 | + |
| 134 | +## Expected Behavior |
| 135 | + |
| 136 | +The fuzzer should: |
| 137 | + |
| 138 | +- ✅ Handle all operations safely |
| 139 | +- ✅ Prevent memory leaks (all nodes cleaned up) |
| 140 | +- ✅ Detect invalid operations (return false) |
| 141 | +- ✅ Handle edge cases (empty list, single node, etc.) |
| 142 | +- ✅ Maintain list integrity (no cycles, no corruption) |
| 143 | + |
| 144 | +## Known Issues |
| 145 | + |
| 146 | +None currently. If you find a crash, please report it! |
| 147 | + |
| 148 | +## Coverage |
| 149 | + |
| 150 | +To generate coverage report: |
| 151 | + |
| 152 | +```bash |
| 153 | +./local_run.sh --build=1 --fuzzer=build/fuzz_c_list --compute-coverage=1 --BOLOS_SDK=/path/to/sdk |
| 154 | +``` |
| 155 | + |
| 156 | +Coverage files will be in `fuzzing/out/coverage/`. |
0 commit comments