|
| 1 | +#!/usr/bin/python3 |
| 2 | +# Copyright 2022 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import atheris |
| 17 | +import os |
| 18 | +import sys |
| 19 | +import tempfile |
| 20 | +from tokenize import TokenError |
| 21 | +from zipfile import BadZipFile |
| 22 | + |
| 23 | +with atheris.instrument_imports(): |
| 24 | + import numpy as np |
| 25 | + |
| 26 | +def TestOneInput(input_bytes): |
| 27 | + with tempfile.NamedTemporaryFile(suffix=".npy", delete=False) as fd: |
| 28 | + fdp = atheris.FuzzedDataProvider(input_bytes) |
| 29 | + fd.write(fdp.ConsumeBytes(sys.maxsize)) |
| 30 | + tmpname = fd.name |
| 31 | + |
| 32 | + try: |
| 33 | + np.load(tmpname) |
| 34 | + # Catch all of the exceptions that are documented in help(np.load) |
| 35 | + except OSError: |
| 36 | + return |
| 37 | + except ValueError: |
| 38 | + return |
| 39 | + except EOFError: |
| 40 | + return |
| 41 | + except IndentationError: |
| 42 | + return |
| 43 | + except BadZipFile: |
| 44 | + return |
| 45 | + except TokenError: |
| 46 | + return |
| 47 | + finally: |
| 48 | + os.remove(tmpname) |
| 49 | + |
| 50 | +def main(): |
| 51 | + atheris.instrument_all() |
| 52 | + atheris.Setup(sys.argv, TestOneInput, enable_python_coverage=True) |
| 53 | + atheris.Fuzz() |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + main() |
0 commit comments