-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfuzzeddataprovider.py
More file actions
244 lines (209 loc) · 8.51 KB
/
Copy pathfuzzeddataprovider.py
File metadata and controls
244 lines (209 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"""Pure-Python FuzzedDataProvider matching the atheris API.
This is a drop-in replacement for atheris.FuzzedDataProvider that requires
no native compilation. It matches atheris's consumption semantics:
- ConsumeBytes/ConsumeInt/ConsumeFloat/ConsumeUnicode consume from the FRONT
- ConsumeIntInRange/ConsumeBool/PickValueInList consume from the BACK
Reference: https://github.com/google/atheris
"""
import struct
class FuzzedDataProvider:
def __init__(self, data):
if not isinstance(data, (bytes, bytearray)):
raise TypeError("data must be bytes or bytearray")
self._data = bytes(data)
self._front = 0
self._back = len(self._data)
def remaining_bytes(self):
return max(0, self._back - self._front)
def buffer(self):
return self._data[self._front : self._back]
# -- Front-consuming methods (ConsumeBytes, ConsumeInt, etc.) --
def _consume_front(self, n):
n = min(n, self.remaining_bytes())
result = self._data[self._front : self._front + n]
self._front += n
return result
def ConsumeBytes(self, count):
count = max(0, int(count))
return self._consume_front(count)
def ConsumeInt(self, byte_count):
byte_count = max(0, int(byte_count))
raw = self._consume_front(byte_count)
if not raw:
return 0
val = int.from_bytes(raw, "little")
bits = len(raw) * 8
if val >= (1 << (bits - 1)):
val -= 1 << bits
return val
def ConsumeUInt(self, byte_count):
byte_count = max(0, int(byte_count))
raw = self._consume_front(byte_count)
if not raw:
return 0
return int.from_bytes(raw, "little")
def ConsumeFloat(self):
raw = self._consume_front(8)
if len(raw) < 8:
raw = raw + b"\x00" * (8 - len(raw))
return struct.unpack("<d", raw)[0]
def ConsumeRegularFloat(self):
val = self.ConsumeFloat()
if val != val or val == float("inf") or val == float("-inf"):
return 0.0
return val
def ConsumeUnicode(self, count):
count = max(0, int(count))
if count == 0 or self.remaining_bytes() == 0:
return ""
# First byte selects encoding mode (matching atheris behavior)
mode_byte = self._consume_front(1)
mode = mode_byte[0] if mode_byte else 0
if mode == 1:
# ASCII mode: one byte per character, masked to 0-127
raw = self._consume_front(count)
return "".join(chr(b & 0x7F) for b in raw)
elif mode == 2:
# UTF-16 mode: two bytes per character
raw = self._consume_front(count * 2)
chars = []
for i in range(0, len(raw) - 1, 2):
cp = int.from_bytes(raw[i : i + 2], "little")
chars.append(chr(cp))
return "".join(chars[:count])
else:
# UTF-32 mode: four bytes per character, clamped to valid range
raw = self._consume_front(count * 4)
chars = []
for i in range(0, len(raw) - 3, 4):
cp = int.from_bytes(raw[i : i + 4], "little") & 0x10FFFF
try:
chars.append(chr(cp))
except (ValueError, OverflowError):
chars.append(" ")
return "".join(chars[:count])
def ConsumeUnicodeNoSurrogates(self, count):
count = max(0, int(count))
if count == 0 or self.remaining_bytes() == 0:
return ""
mode_byte = self._consume_front(1)
mode = mode_byte[0] if mode_byte else 0
if mode == 1:
raw = self._consume_front(count)
return "".join(chr(b & 0x7F) for b in raw)
elif mode == 2:
raw = self._consume_front(count * 2)
chars = []
for i in range(0, len(raw) - 1, 2):
cp = int.from_bytes(raw[i : i + 2], "little")
if 0xD800 <= cp <= 0xDFFF:
cp -= 0xD800
chars.append(chr(cp))
return "".join(chars[:count])
else:
raw = self._consume_front(count * 4)
chars = []
for i in range(0, len(raw) - 3, 4):
cp = int.from_bytes(raw[i : i + 4], "little") & 0x10FFFF
if 0xD800 <= cp <= 0xDFFF:
cp -= 0xD800
try:
chars.append(chr(cp))
except (ValueError, OverflowError):
chars.append(" ")
return "".join(chars[:count])
def ConsumeString(self, count):
return self.ConsumeUnicode(count)
# -- Back-consuming methods (ConsumeIntInRange, ConsumeBool, etc.) --
def _consume_back(self, n):
n = min(n, self.remaining_bytes())
result = self._data[self._back - n : self._back]
self._back -= n
return result
def ConsumeIntInRange(self, lo, hi):
lo, hi = int(lo), int(hi)
if lo > hi:
lo, hi = hi, lo
if lo == hi:
return lo
rng = hi - lo
# Match LLVM: consume ceil(bits_needed/8) bytes from back
# LLVM loops while offset < sizeof(T)*8 && (range >> offset) > 0
nbytes = (rng.bit_length() + 7) // 8
raw = self._consume_back(nbytes)
if not raw:
return lo
# LLVM reads bytes from back as big-endian accumulation:
# result = (result << 8) | next_byte_from_back
# which equals int.from_bytes(reversed_bytes, 'big')
# But since _consume_back returns bytes in memory order and
# int.from_bytes(raw, 'little') produces the same value, we use that.
val = int.from_bytes(raw, "little")
return lo + (val % (rng + 1))
# Alias for LLVM naming compatibility
ConsumeIntegralInRange = ConsumeIntInRange
def ConsumeBool(self):
# Matches LLVM: 1 & ConsumeIntegral<uint8_t>()
# ConsumeIntegral<uint8_t>() = ConsumeIntegralInRange(0, 255)
return (self.ConsumeIntInRange(0, 255) & 1) == 1
def ConsumeProbability(self):
# Matches LLVM: ConsumeIntegral<uint64_t>() / UINT64_MAX
# ConsumeIntegral<uint64_t>() = ConsumeIntegralInRange(0, 2^64-1)
# When range == UINT64_MAX, no modulo is applied (special case)
raw = self._consume_back(8)
if not raw:
return 0.0
val = int.from_bytes(raw, "little")
return val / float((1 << 64) - 1)
def ConsumeFloatInRange(self, lo, hi):
lo, hi = float(lo), float(hi)
if lo > hi:
lo, hi = hi, lo
p = self.ConsumeProbability()
return lo + (hi - lo) * p
def PickValueInList(self, lst):
if not lst:
raise ValueError("list must not be empty")
idx = self.ConsumeIntInRange(0, len(lst) - 1)
return lst[idx]
# -- List methods --
def ConsumeIntList(self, count, byte_count):
count = max(0, int(count))
return [self.ConsumeInt(byte_count) for _ in range(count)]
def ConsumeIntListInRange(self, count, lo, hi):
count = max(0, int(count))
return [self.ConsumeIntInRange(lo, hi) for _ in range(count)]
def ConsumeFloatList(self, count):
count = max(0, int(count))
return [self.ConsumeFloat() for _ in range(count)]
def ConsumeFloatListInRange(self, count, lo, hi):
count = max(0, int(count))
return [self.ConsumeFloatInRange(lo, hi) for _ in range(count)]
def ConsumeProbabilityList(self, count):
count = max(0, int(count))
return [self.ConsumeProbability() for _ in range(count)]
def ConsumeRegularFloatList(self, count):
count = max(0, int(count))
return [self.ConsumeRegularFloat() for _ in range(count)]
# -- Arbitrary value --
_ANY_TYPE_INT = 0
_ANY_TYPE_FLOAT = 1
_ANY_TYPE_BOOL = 2
_ANY_TYPE_BYTES = 3
_ANY_TYPE_STRING = 4
_ANY_TYPE_NONE = 5
def ConsumeRandomValue(self):
"""Return a value of a randomly chosen primitive type."""
t = self.ConsumeIntInRange(self._ANY_TYPE_INT, self._ANY_TYPE_NONE)
if t == self._ANY_TYPE_INT:
return self.ConsumeInt(4)
elif t == self._ANY_TYPE_FLOAT:
return self.ConsumeFloat()
elif t == self._ANY_TYPE_BOOL:
return self.ConsumeBool()
elif t == self._ANY_TYPE_BYTES:
return self.ConsumeBytes(self.ConsumeIntInRange(0, 64))
elif t == self._ANY_TYPE_STRING:
return self.ConsumeUnicode(self.ConsumeIntInRange(0, 64))
else:
return None