Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements and simplification in corpus' handling randomisation code #22

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Improvements and simplification in corpus' handling randomisation code

- Remove useless casts to bool
- Use something faster than random.randint: this cuts in half the time spent
  in the random.py file, without raising anything else
- use random.choice where possible, instead of `mylist[_rand(len(mylist))]`
- Add some comments explaining the if…if…else structure, to prevent
  future refactoring from transforming them into an if…elif…else

On a local fuzzer, with 10000 iterations, this decrease the amount of time
spent in corpus' mutate function from ~0.2 seconds to ~0.1 seconds, for a total
run time of ~9 seconds, which seems to not be a lot, but since 8.60s is spent
in connection.py's poll function waiting for the fuzzer, it's a worthy
optimization.
  • Loading branch information
jvoisin committed Dec 31, 2019
commit 42bf24d43295b9daee852bf8d07c8fd1dac1b385
@@ -41,9 +41,14 @@ def length(self):

@staticmethod
def _rand(n):
if n == 1 or n == 0:
if n < 2:
return 0
return random.randint(0, n-1)

k = (n-1).bit_length()
r = random.getrandbits(k) # 0 <= r < 2**k
while r >= n:
r = random.getrandbits(k)
return r

# Exp2 generates n with probability 1/2^(n+1).
@staticmethod
@@ -97,7 +102,7 @@ def generate_input(self):
self.put(zero_test_case)
return zero_test_case
else:
buf = self._inputs[self._rand(len(self._inputs))]
buf = random.choice(self._inputs)
return self.mutate(buf)

def mutate(self, buf):
@@ -182,7 +187,7 @@ def mutate(self, buf):
continue
pos = self._rand(len(res))
v = self._rand(35) + 1
if bool(random.getrandbits(1)):
if random.getrandbits(1):
res[pos] = numpy.uint8(res[pos]) + numpy.uint8(v)
else:
res[pos] = numpy.uint8(res[pos]) - numpy.uint8(v)
@@ -193,9 +198,9 @@ def mutate(self, buf):
continue
pos = self._rand(len(res) - 1)
v = numpy.uint16(self._rand(35) + 1)
if bool(random.getrandbits(1)):
if random.getrandbits(1):
v = numpy.uint16(0) - v
if bool(random.getrandbits(1)):
if random.getrandbits(1): # pick the endianness
v = struct.pack('>H', v)
else:
v = struct.pack('<H', v)
@@ -208,9 +213,9 @@ def mutate(self, buf):
continue
pos = self._rand(len(res) - 3)
v = numpy.uint32(self._rand(35) + 1)
if bool(random.getrandbits(1)):
if random.getrandbits(1):
v = numpy.uint32(0) - v
if bool(random.getrandbits(1)):
if random.getrandbits(1): # pick the endianness
v = struct.pack('>I', v)
else:
v = struct.pack('<I', v)
@@ -223,9 +228,9 @@ def mutate(self, buf):
continue
pos = self._rand(len(res) - 7)
v = numpy.uint64(self._rand(35) + 1)
if bool(random.getrandbits(1)):
if random.getrandbits(1):
v = numpy.uint64(0) - v
if bool(random.getrandbits(1)):
if random.getrandbits(1): # pick the endianness
v = struct.pack('>Q', v)
else:
v = struct.pack('<Q', v)
@@ -237,15 +242,15 @@ def mutate(self, buf):
i -= 1
continue
pos = self._rand(len(res))
res[pos] = numpy.uint8(INTERESTING8[self._rand(len(INTERESTING8))])
res[pos] = numpy.uint8(random.choice(INTERESTING8))
elif x == 12:
# Replace an uint16 with an interesting value.
if len(res) < 2:
i -= 1
continue
pos = self._rand(len(res) - 1)
v = numpy.uint16(INTERESTING16[self._rand(len(INTERESTING16))])
if bool(random.getrandbits(1)):
v = numpy.uint16(random.choice(INTERESTING16))
if random.getrandbits(1): # pick the endianness
v = struct.pack('>H', v)
else:
v = struct.pack('<H', v)
@@ -257,8 +262,8 @@ def mutate(self, buf):
i -= 1
continue
pos = self._rand(len(res) - 3)
v = numpy.uint32(INTERESTING32[self._rand(len(INTERESTING32))])
if bool(random.getrandbits(1)):
v = numpy.uint32(random.choice(INTERESTING32))
if random.getrandbits(1): # pick the endianness
v = struct.pack('>I', v)
else:
v = struct.pack('<I', v)
@@ -270,7 +275,7 @@ def mutate(self, buf):
for k in range(len(res)):
if ord('0') <= res[k] <= ord('9'):
digits.append(k)
if len(digits) == 0:
if not digits:
i -= 1
continue
pos = self._rand(len(digits))
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.