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

Bump the coverage.py version, and minor refactoring #25

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

Always

Just for now

Next

Bump the coverage.py version, and minor refactoring

- Move the worker function to a dedicated file
- Bump the coverage.py version to the latest one
- Remove the monkey-patching for the previous version
  of coverage.py, since it doesn't work with the new
  one
  • Loading branch information
jvoisin committed Jan 2, 2020
commit 9971eaa160ae6e02c08b2df7eab830ca16b6df58
@@ -1,74 +1,19 @@
import os
import sys
import time
import sys
import psutil
import hashlib
import logging
import coverage
import functools
import multiprocessing as mp

from pythonfuzz import corpus
from pythonfuzz import corpus, worker

logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
logging.getLogger().setLevel(logging.DEBUG)

SAMPLING_WINDOW = 5 # IN SECONDS

if coverage.version.version_info <= (5, ):
# Since we're using an old version of coverage.py,
# we're monkey patching it a bit to improve the performances.

# Using memoization here gives +50% in performances, since this
# function triggers a lot of syscalls.
# See the benchmarks here:
# - https://github.com/fuzzitdev/pythonfuzz/issues/9
@functools.lru_cache(None)
def abs_file(path):
"""Return the absolute normalized form of `path`."""
try:
path = os.path.realpath(path)
except UnicodeError:
pass
path = os.path.abspath(path)
path = coverage.files.actual_path(path)
path = coverage.files.unicode_filename(path)
return path

coverage.files.abs_file = abs_file_cache


def worker(target, child_conn, close_fd_mask):
# Silence the fuzzee's noise
class DummyFile:
"""No-op to trash stdout away."""
def write(self, x):
pass
logging.captureWarnings(True)
logging.getLogger().setLevel(logging.CRITICAL)
if close_fd_mask & 1:
sys.stdout = DummyFile()
if close_fd_mask & 2:
sys.stderr = DummyFile()

cov = coverage.Coverage(branch=True, cover_pylib=True)
cov.start()
while True:
buf = child_conn.recv_bytes()
try:
target(buf)
except Exception as e:
logging.exception(e)
child_conn.send(e)
break
else:
total_coverage = 0
cov_data = cov.get_data()
for filename in cov_data._arcs:
total_coverage += len(cov_data._arcs[filename])
child_conn.send(total_coverage)


class Fuzzer(object):
def __init__(self,
@@ -124,7 +69,7 @@ def start(self):
logging.info("#0 READ units: {}".format(self._corpus.length))

parent_conn, child_conn = mp.Pipe()
self._p = mp.Process(target=worker, args=(self._target, child_conn, self._close_fd_mask))
self._p = mp.Process(target=worker.worker, args=(self._target, child_conn, self._close_fd_mask))
self._p.start()

while True:
@@ -0,0 +1,40 @@
import logging
import sys
import coverage

def worker(target, child_conn, close_fd_mask):
# Silence the fuzzee's noise
class DummyFile:
"""No-op to trash stdout away."""
def write(self, x):
pass
logging.captureWarnings(True)
logging.getLogger().setLevel(logging.CRITICAL)
if close_fd_mask & 1:
sys.stdout = DummyFile()
if close_fd_mask & 2:
sys.stderr = DummyFile()

i = 0

cov = coverage.Coverage(branch=True, cover_pylib=True, data_file='out.sqlite')
cov.start()
while True:
if i > 10000:
break
i += 1
buf = child_conn.recv_bytes()
try:
target(buf)
except Exception as e:
logging.exception(e)
child_conn.send(e)
break
else:
total_coverage = 0
cov_data = cov.get_data()
for filename in cov_data.measured_files():
total_coverage += len(cov_data.arcs(filename))
child_conn.send(total_coverage)


@@ -1,3 +1,3 @@
coverage==4.5.4
coverage==5.0
psutil==5.6.3
numpy==1.17.3
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.