Skip to content

Commit 4157478

Browse files
committed
Add fuzzer for mmap module
1 parent 71ede86 commit 4157478

3 files changed

Lines changed: 95 additions & 2 deletions

File tree

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
all : fuzzer-html fuzzer-email fuzzer-httpclient fuzzer-json fuzzer-difflib fuzzer-csv fuzzer-decode fuzzer-ast fuzzer-tarfile fuzzer-tarfile-hypothesis fuzzer-zipfile fuzzer-zipfile-hypothesis fuzzer-re fuzzer-configparser fuzzer-tomllib fuzzer-plistlib fuzzer-xml fuzzer-zoneinfo
1+
all : fuzzer-html fuzzer-email fuzzer-httpclient fuzzer-json fuzzer-difflib fuzzer-csv fuzzer-decode fuzzer-ast fuzzer-tarfile fuzzer-tarfile-hypothesis fuzzer-zipfile fuzzer-zipfile-hypothesis fuzzer-re fuzzer-configparser fuzzer-tomllib fuzzer-plistlib fuzzer-xml fuzzer-zoneinfo fuzzer-mmap
22

33
PYTHON_CONFIG_PATH=$(CPYTHON_INSTALL_PATH)/bin/python3-config
44
CXXFLAGS += $(shell $(PYTHON_CONFIG_PATH) --cflags)
5-
LDFLAGS += -rdynamic $(shell $(PYTHON_CONFIG_PATH) --ldflags --embed)
5+
LDFLAGS += -rdynamic $(shell $(PYTHON_CONFIG_PATH) --ldflags --embed) $(CPYTHON_MODLIBS) -Wl,--allow-multiple-definition
66

77
fuzzer-html:
88
clang++ $(CXXFLAGS) $(LIB_FUZZING_ENGINE) -std=c++17 fuzzer.cpp -DPYTHON_HARNESS_PATH="\"html.py\"" -ldl $(LDFLAGS) -o fuzzer-html
@@ -40,3 +40,6 @@ fuzzer-xml:
4040
clang++ $(CXXFLAGS) $(LIB_FUZZING_ENGINE) -std=c++17 fuzzer.cpp -DPYTHON_HARNESS_PATH="\"xml.py\"" -ldl $(LDFLAGS) -o fuzzer-xml
4141
fuzzer-zoneinfo:
4242
clang++ $(CXXFLAGS) $(LIB_FUZZING_ENGINE) -std=c++17 fuzzer.cpp -DPYTHON_HARNESS_PATH="\"zoneinfo.py\"" -ldl $(LDFLAGS) -o fuzzer-zoneinfo
43+
44+
fuzzer-mmap:
45+
clang++ $(CXXFLAGS) $(LIB_FUZZING_ENGINE) -std=c++17 fuzzer.cpp -DPYTHON_HARNESS_PATH="\"mmap.py\"" -ldl $(LDFLAGS) -o fuzzer-mmap

fuzz_targets.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ email email.py
77
html html.py
88
httpclient httpclient.py
99
json json.py
10+
mmap mmap.py
1011
plistlib plist.py
1112
re re.py
1213
tarfile tarfile.py

mmap.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
from fuzzeddataprovider import FuzzedDataProvider
2+
import os
3+
import mmap
4+
import tempfile
5+
6+
OP_FIND = 0
7+
OP_RFIND = 1
8+
OP_READ = 2
9+
OP_READLINE = 3
10+
OP_SEEK = 4
11+
OP_GETITEM = 5
12+
OP_WRITE = 6
13+
OP_SETITEM = 7
14+
OP_MOVE = 8
15+
OP_FLUSH = 9
16+
17+
_OP_MAX = OP_FLUSH
18+
19+
# Fuzzes the mmap C module (Modules/mmapmodule.c). Creates a temporary
20+
# file-backed mmap and exercises find, rfind, seek, read, readline,
21+
# getitem, write, setitem, move, and flush operations with fuzzed
22+
# offsets, sizes, and byte content.
23+
def FuzzerRunOne(FuzzerInput):
24+
if len(FuzzerInput) < 1 or len(FuzzerInput) > 0x10000:
25+
return
26+
fdp = FuzzedDataProvider(FuzzerInput)
27+
init_size = fdp.ConsumeIntInRange(1, min(fdp.remaining_bytes(), 4096)) if fdp.remaining_bytes() > 0 else 0
28+
if init_size == 0:
29+
return
30+
init_data = fdp.ConsumeBytes(init_size)
31+
tmpname = None
32+
try:
33+
with tempfile.NamedTemporaryFile(delete=False) as tmp:
34+
tmpname = tmp.name
35+
tmp.write(init_data)
36+
tmp.flush()
37+
38+
with open(tmpname, 'r+b') as f:
39+
mm = mmap.mmap(f.fileno(), 0)
40+
num_ops = fdp.ConsumeIntInRange(1, 10)
41+
for _ in range(num_ops):
42+
if fdp.remaining_bytes() == 0:
43+
break
44+
op = fdp.ConsumeIntInRange(0, _OP_MAX)
45+
if op == OP_FIND:
46+
needle = fdp.ConsumeBytes(fdp.ConsumeIntInRange(1, min(fdp.remaining_bytes(), 20)))
47+
mm.find(needle)
48+
elif op == OP_RFIND:
49+
needle = fdp.ConsumeBytes(fdp.ConsumeIntInRange(1, min(fdp.remaining_bytes(), 20)))
50+
mm.rfind(needle)
51+
elif op == OP_READ:
52+
mm.seek(0)
53+
mm.read(fdp.ConsumeIntInRange(0, len(mm)))
54+
elif op == OP_READLINE:
55+
mm.seek(0)
56+
mm.readline()
57+
elif op == OP_SEEK:
58+
pos = fdp.ConsumeIntInRange(0, max(0, len(mm) - 1))
59+
mm.seek(pos)
60+
elif op == OP_GETITEM:
61+
if len(mm) > 0:
62+
idx = fdp.ConsumeIntInRange(0, len(mm) - 1)
63+
_ = mm[idx]
64+
elif op == OP_WRITE:
65+
data = fdp.ConsumeBytes(fdp.ConsumeIntInRange(1, min(fdp.remaining_bytes(), 50)))
66+
pos = fdp.ConsumeIntInRange(0, max(0, len(mm) - len(data)))
67+
mm.seek(pos)
68+
mm.write(data)
69+
elif op == OP_SETITEM:
70+
if len(mm) > 0:
71+
idx = fdp.ConsumeIntInRange(0, len(mm) - 1)
72+
mm[idx] = fdp.ConsumeInt(1)
73+
elif op == OP_MOVE:
74+
if len(mm) > 1:
75+
count = fdp.ConsumeIntInRange(1, len(mm) // 2)
76+
src = fdp.ConsumeIntInRange(0, len(mm) - count)
77+
dest = fdp.ConsumeIntInRange(0, len(mm) - count)
78+
mm.move(dest, src, count)
79+
elif op == OP_FLUSH:
80+
mm.flush()
81+
mm.close()
82+
except Exception:
83+
pass
84+
finally:
85+
if tmpname:
86+
try:
87+
os.unlink(tmpname)
88+
except Exception:
89+
pass

0 commit comments

Comments
 (0)