|
| 1 | +# Atheris fuzzing utilities written by Bailey Capuano |
| 2 | +import io |
| 3 | +import tempfile |
| 4 | +import atheris |
| 5 | +import contextlib |
| 6 | +from typing import List, Set, Dict, Tuple, Any |
| 7 | + |
| 8 | + |
| 9 | +def _handle_type(fdp: atheris.FuzzedDataProvider, ty_queue: List[type]) -> Any: |
| 10 | + """ |
| 11 | + Handles the fuzzing of a single type. |
| 12 | + :param fdp: FuzzedDataProvider object |
| 13 | + :param ty_queue: The current stack of types to be used for fuzzing |
| 14 | + :return: The fuzzed element |
| 15 | + """ |
| 16 | + if not ty_queue: |
| 17 | + return None |
| 18 | + ty = ty_queue.pop(0) |
| 19 | + if ty is bytes: |
| 20 | + return fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, 100)) |
| 21 | + elif ty is bytearray: |
| 22 | + return bytearray(fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, 100))) |
| 23 | + elif ty is str: |
| 24 | + return fdp.ConsumeUnicodeNoSurrogates(fdp.ConsumeIntInRange(0, 100)) |
| 25 | + elif ty is float: |
| 26 | + return fdp.ConsumeRegularFloat() |
| 27 | + elif ty is bool: |
| 28 | + return fdp.ConsumeBool() |
| 29 | + elif ty is int: |
| 30 | + return fdp.ConsumeInt(4) |
| 31 | + elif ty is dict: |
| 32 | + return build_fuzz_dict(fdp, ty_queue) |
| 33 | + elif ty is list: |
| 34 | + return build_fuzz_list(fdp, ty_queue) |
| 35 | + elif ty is set: |
| 36 | + return build_fuzz_set(fdp, ty_queue) |
| 37 | + elif ty is tuple: |
| 38 | + return build_fuzz_tuple(fdp, ty_queue) |
| 39 | + else: |
| 40 | + return None |
| 41 | + |
| 42 | + |
| 43 | +def build_fuzz_list(fdp: atheris.FuzzedDataProvider, ty_queue: List[type]) -> List[Any]: |
| 44 | + """ |
| 45 | + Builds a list with fuzzer-defined elements. |
| 46 | + :param fdp: FuzzedDataProvider object |
| 47 | + :param ty_queue: The current stack of types to be used for fuzzing |
| 48 | + :return: The list |
| 49 | + """ |
| 50 | + if not ty_queue: |
| 51 | + return [] |
| 52 | + elem_count = fdp.ConsumeIntInRange(1, 5) |
| 53 | + gen_list = [] |
| 54 | + |
| 55 | + for _ in range(elem_count): |
| 56 | + passed_queue = ty_queue.copy() |
| 57 | + elem = _handle_type(fdp, passed_queue) |
| 58 | + if elem is not None: |
| 59 | + gen_list.append(elem) |
| 60 | + ty_queue.pop(0) # Pop elem type |
| 61 | + |
| 62 | + return gen_list |
| 63 | + |
| 64 | + |
| 65 | +def build_fuzz_set(fdp: atheris.FuzzedDataProvider, ty_queue: List[type]) -> Set[Any]: |
| 66 | + """ |
| 67 | + Builds a set with fuzzer-defined elements. |
| 68 | + :param fdp: FuzzedDataProvider object |
| 69 | + :param ty_queue: The current stack of types to be used for fuzzing |
| 70 | + :return: The set |
| 71 | + """ |
| 72 | + if not ty_queue: |
| 73 | + return set() |
| 74 | + ty_queue.insert(0, list) |
| 75 | + |
| 76 | + fuzz_list = _handle_type(fdp, ty_queue) |
| 77 | + return set(fuzz_list) |
| 78 | + |
| 79 | + |
| 80 | +def build_fuzz_tuple(fdp: atheris.FuzzedDataProvider, ty_queue: List[type]) -> Tuple[Any]: |
| 81 | + """ |
| 82 | + Builds a tuple with fuzzer-defined elements. |
| 83 | + :param fdp: FuzzedDataProvider object |
| 84 | + :param ty_queue: The current stack of types to be used for fuzzing |
| 85 | + :return: The tuple |
| 86 | + """ |
| 87 | + if not ty_queue: |
| 88 | + return tuple() |
| 89 | + ty_queue.insert(0, list) |
| 90 | + |
| 91 | + fuzz_list = _handle_type(fdp, ty_queue) |
| 92 | + return tuple(fuzz_list) |
| 93 | + |
| 94 | + |
| 95 | +def build_fuzz_dict(fdp: atheris.FuzzedDataProvider, ty_queue: List[type]) -> Dict[Any, Any]: |
| 96 | + """ |
| 97 | + Builds a dictionary with fuzzer-defined keys and values. |
| 98 | + :param fdp: FuzzedDataProvider object |
| 99 | + :param ty_queue: The current stack of types to be used for fuzzing |
| 100 | + :return: The dictionary |
| 101 | + """ |
| 102 | + if not ty_queue: |
| 103 | + return {} |
| 104 | + |
| 105 | + ty_queue.insert(0, list) # handle key |
| 106 | + key_list = _handle_type(fdp, ty_queue) |
| 107 | + ty_queue.insert(0, list) # handle key |
| 108 | + val_list = _handle_type(fdp, ty_queue) |
| 109 | + |
| 110 | + # Shrink lists to match |
| 111 | + if len(key_list) > len(val_list): |
| 112 | + key_list = key_list[:len(val_list)] |
| 113 | + elif len(val_list) > len(key_list): |
| 114 | + val_list = val_list[:len(key_list)] |
| 115 | + |
| 116 | + return dict(zip(key_list, val_list)) |
| 117 | + |
| 118 | + |
| 119 | +class EnhancedFuzzedDataProvider(atheris.FuzzedDataProvider): |
| 120 | + def ConsumeRandomBytes(self) -> bytes: |
| 121 | + return self.ConsumeBytes(self.ConsumeIntInRange(0, self.remaining_bytes())) |
| 122 | + |
| 123 | + def ConsumeRandomString(self) -> str: |
| 124 | + return self.ConsumeUnicodeNoSurrogates(self.ConsumeIntInRange(0, self.remaining_bytes())) |
| 125 | + |
| 126 | + def ConsumeRemainingString(self) -> str: |
| 127 | + return self.ConsumeUnicodeNoSurrogates(self.remaining_bytes()) |
| 128 | + |
| 129 | + def ConsumeRemainingBytes(self) -> bytes: |
| 130 | + return self.ConsumeBytes(self.remaining_bytes()) |
| 131 | + |
| 132 | + @contextlib.contextmanager |
| 133 | + def ConsumeMemoryFile(self, all_data: bool = False, as_bytes: bool = True) -> io.BytesIO: |
| 134 | + if all_data: |
| 135 | + file_data = self.ConsumeRemainingBytes() if as_bytes else self.ConsumeRemainingString() |
| 136 | + else: |
| 137 | + file_data = self.ConsumeRandomBytes() if as_bytes else self.ConsumeRandomString() |
| 138 | + |
| 139 | + file = io.BytesIO(file_data) if as_bytes else io.StringIO(file_data) |
| 140 | + yield file |
| 141 | + file.close() |
| 142 | + |
| 143 | + @contextlib.contextmanager |
| 144 | + def ConsumeTemporaryFile(self, suffix: str, all_data: bool = False, as_bytes: bool = True) -> io.StringIO: |
| 145 | + if all_data: |
| 146 | + file_data = self.ConsumeRemainingBytes() if as_bytes else self.ConsumeRemainingString() |
| 147 | + else: |
| 148 | + file_data = self.ConsumeRandomBytes() if as_bytes else self.ConsumeRandomString() |
| 149 | + |
| 150 | + mode = 'w+b' if as_bytes else 'w+' |
| 151 | + tfile = tempfile.NamedTemporaryFile(mode=mode, suffix=suffix) |
| 152 | + tfile.write(file_data) |
| 153 | + tfile.seek(0) |
| 154 | + tfile.flush() |
| 155 | + yield tfile.name |
| 156 | + tfile.close() |
0 commit comments