Skip to content

Commit dc037dc

Browse files
committed
Add fuzzer for json_decode module
1 parent 71ede86 commit dc037dc

3 files changed

Lines changed: 41 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-json-decode
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-json-decode:
45+
clang++ $(CXXFLAGS) $(LIB_FUZZING_ENGINE) -std=c++17 fuzzer.cpp -DPYTHON_HARNESS_PATH="\"json_decode.py\"" -ldl $(LDFLAGS) -o fuzzer-json-decode

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+
json-decode json_decode.py
1011
plistlib plist.py
1112
re re.py
1213
tarfile tarfile.py

json_decode.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from fuzzeddataprovider import FuzzedDataProvider
2+
import json
3+
4+
LOADS = 0
5+
DECODER_DECODE = 1
6+
DECODER_RAW_DECODE = 2
7+
8+
9+
# Fuzzes the _json C module's decoding paths (Modules/_json.c).
10+
# Exercises json.loads(), JSONDecoder.decode(), and
11+
# JSONDecoder.raw_decode() with fuzzed byte input decoded as latin-1.
12+
def FuzzerRunOne(FuzzerInput):
13+
if len(FuzzerInput) < 1 or len(FuzzerInput) > 0x100000:
14+
return
15+
fdp = FuzzedDataProvider(FuzzerInput)
16+
target = fdp.ConsumeIntInRange(DECODER_DECODE, DECODER_RAW_DECODE)
17+
n = (
18+
fdp.ConsumeIntInRange(1, min(fdp.remaining_bytes(), 10000))
19+
if fdp.remaining_bytes() > 0
20+
else 0
21+
)
22+
if n == 0:
23+
return
24+
s = fdp.ConsumeBytes(n).decode("latin-1")
25+
try:
26+
if target == DECODER_DECODE:
27+
dec = json.JSONDecoder()
28+
dec.decode(s)
29+
elif target == DECODER_RAW_DECODE:
30+
dec = json.JSONDecoder()
31+
dec.raw_decode(s)
32+
except (json.JSONDecodeError, ValueError, RecursionError):
33+
pass
34+
except Exception:
35+
pass

0 commit comments

Comments
 (0)