blob: 3fcbca9cfdc925e0d1d63a9aa884a75467318191 [file] [log] [blame]
Jens Geyer72a714e2025-08-26 22:12:07 +02001#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
Hasnain Lakhanid2743002025-08-25 14:22:15 -070020import glob
21import sys
22import os
23import atheris
24
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050025
Hasnain Lakhanid2743002025-08-25 14:22:15 -070026def setup_thrift_imports():
27 """Set up the Python path to include Thrift libraries and generated code."""
28
29 # For oss-fuzz, we need to package it using pyinstaller and set up paths properly
30 if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
31 print('running in a PyInstaller bundle')
32 sys.path.insert(0, "thrift_lib")
33 sys.path.insert(0, "gen-py")
34 else:
35 print('running in a normal Python process')
36 SCRIPT_DIR = os.path.realpath(os.path.dirname(__file__))
37 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(SCRIPT_DIR))))
38
39 for libpath in glob.glob(os.path.join(ROOT_DIR, 'lib', 'py', 'build', 'lib.*')):
40 for pattern in ('-%d.%d', '-%d%d'):
41 postfix = pattern % (sys.version_info[0], sys.version_info[1])
42 if libpath.endswith(postfix):
43 sys.path.insert(0, libpath)
44 break
45
46 gen_path = os.path.join(
47 os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "..", "gen-py"
48 )
49 sys.path.append(gen_path)
50 print(sys.path)
51
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050052
Hasnain Lakhanid2743002025-08-25 14:22:15 -070053setup_thrift_imports()
54
55from thrift.transport import TTransport
56from thrift.TSerialization import serialize, deserialize
57from fuzz.ttypes import FuzzTest
58
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050059
Hasnain Lakhanid2743002025-08-25 14:22:15 -070060def create_parser_fuzzer(protocol_factory_class):
61 """
62 Create a parser fuzzer function for a specific protocol.
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050063
Hasnain Lakhanid2743002025-08-25 14:22:15 -070064 Args:
65 protocol_factory_class: The Thrift protocol factory class to use
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050066
Hasnain Lakhanid2743002025-08-25 14:22:15 -070067 Returns:
68 A function that can be used with atheris.Setup()
69 """
70 def TestOneInput(data):
71 if len(data) < 2:
72 return
73
74 try:
75 # Create a memory buffer with the fuzzed data
76 buf = TTransport.TMemoryBuffer(data)
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050077 TTransport.TBufferedTransportFactory().getTransport(buf)
Hasnain Lakhanid2743002025-08-25 14:22:15 -070078 factory = protocol_factory_class(string_length_limit=1000, container_length_limit=1000)
79
80 # Try to deserialize the fuzzed data into the test class
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050081 deserialize(FuzzTest(), data, factory)
Hasnain Lakhanid2743002025-08-25 14:22:15 -070082
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050083 except Exception:
Hasnain Lakhanid2743002025-08-25 14:22:15 -070084 # We expect various exceptions during fuzzing
85 pass
86
87 return TestOneInput
88
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050089
Hasnain Lakhanid2743002025-08-25 14:22:15 -070090def create_roundtrip_fuzzer(protocol_factory_class):
91 """
92 Create a roundtrip fuzzer function for a specific protocol.
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050093
Hasnain Lakhanid2743002025-08-25 14:22:15 -070094 Args:
95 protocol_factory_class: The Thrift protocol factory class to use
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050096
Hasnain Lakhanid2743002025-08-25 14:22:15 -070097 Returns:
98 A function that can be used with atheris.Setup()
99 """
100 def TestOneInput(data):
101 if len(data) < 2:
102 return
103
104 try:
105 # Create a memory buffer with the fuzzed data
106 buf = TTransport.TMemoryBuffer(data)
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -0500107 TTransport.TBufferedTransportFactory().getTransport(buf)
Hasnain Lakhanid2743002025-08-25 14:22:15 -0700108 factory = protocol_factory_class(string_length_limit=1000, container_length_limit=1000)
109
110 # Try to deserialize the fuzzed data into the test class
111 test_instance = deserialize(FuzzTest(), data, factory)
112 # If deserialization succeeds, try to serialize it back
113 serialized = serialize(test_instance, factory)
114 # Deserialize again
115 deserialized = deserialize(FuzzTest(), serialized, factory)
116 # Verify the objects are equal after a second deserialization
117 assert test_instance == deserialized
118
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -0500119 except AssertionError:
120 raise
121 except Exception:
Hasnain Lakhanid2743002025-08-25 14:22:15 -0700122 # We expect various exceptions during fuzzing
123 pass
124
125 return TestOneInput
126
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -0500127
Hasnain Lakhanid2743002025-08-25 14:22:15 -0700128def _run_fuzzer(fuzzer_function):
129 """
130 Set up and run the fuzzer for a specific protocol.
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -0500131
Hasnain Lakhanid2743002025-08-25 14:22:15 -0700132 Args:
133 fuzzer_function: The fuzzer function to use
134 """
135 setup_thrift_imports()
136 atheris.instrument_all()
137 atheris.Setup(sys.argv, fuzzer_function, enable_python_coverage=True)
138 atheris.Fuzz()
139
140
141def run_roundtrip_fuzzer(protocol_factory_class):
142 """
143 Set up and run the fuzzer for a specific protocol.
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -0500144
Hasnain Lakhanid2743002025-08-25 14:22:15 -0700145 Args:
146 protocol_factory_class: The Thrift protocol factory class to use
147 """
148 _run_fuzzer(create_roundtrip_fuzzer(protocol_factory_class))
149
150
151def run_parser_fuzzer(protocol_factory_class):
152 """
153 Set up and run the fuzzer for a specific protocol.
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -0500154
Hasnain Lakhanid2743002025-08-25 14:22:15 -0700155 Args:
156 protocol_factory_class: The Thrift protocol factory class to use
157 """
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -0500158 _run_fuzzer(create_parser_fuzzer(protocol_factory_class))