Skip to content

Commit 164ca3c

Browse files
numpy: Fuzz binary matrix loader (#13001)
This requires changing the amount of memory made available to the fuzzer, so also add the logic to copy fuzzer option files.
1 parent 4471771 commit 164ca3c

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

projects/numpy/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ RUN curl -LO https://bootstrap.pypa.io/get-pip.py && \
2727
python3 -m pip install --root-user-action=ignore atheris pyinstaller
2828
RUN git clone https://github.com/numpy/numpy && cd numpy && git submodule update --init
2929
WORKDIR $SRC
30-
COPY *.py build.sh $SRC/
30+
COPY *.options *.py build.sh $SRC/

projects/numpy/build.sh

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ unset LIB_FUZZING_ENGINE
2323

2424
python3 -m pip install .
2525

26+
cp $SRC/*.options $OUT/
27+
2628
mkdir -p $SRC/numpy-fuzzers
2729
cd $SRC/numpy-fuzzers
2830

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[libfuzzer]
2+
rss_limit_mb=6000

projects/numpy/fuzz_binary_loader.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)