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

[WIP] Expose trace map to Python code #10

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ language: python
matrix:
include:
- python: "2.6"
env: CYTHON=0.19
env: CYTHON=0.19.1
- python: "2.7"
- python: "3.2"
env: CYTHON=0.19
env: CYTHON=0.19.1
- python: "3.3"
- python: "3.4"
- python: "3.5"
Expand Down
6 changes: 5 additions & 1 deletion afl.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ cdef extern from 'signal.h':
cdef extern from 'sys/shm.h':
unsigned char *shmat(int shmid, void *shmaddr, int shmflg)

trace_map = None
cdef unsigned char *afl_area = NULL
cdef unsigned int prev_location = 0

Expand Down Expand Up @@ -125,7 +126,7 @@ cdef bint init_done = False
cdef bint tstl_mode = False

cdef int _init(bint persistent_mode) except -1:
global afl_area, init_done, tstl_mode
global afl_area, trace_map, init_done, tstl_mode
tstl_mode = os.getenv('PYTHON_AFL_TSTL') is not None
use_forkserver = True
try:
Expand Down Expand Up @@ -182,6 +183,8 @@ cdef int _init(bint persistent_mode) except -1:
afl_area = shmat(int(afl_shm_id), NULL, 0)
if afl_area == <void*> -1:
PyErr_SetFromErrno(OSError)
cdef unsigned char[:] cy_trace_map = <unsigned char[:MAP_SIZE]> afl_area
trace_map = cy_trace_map
sys.settrace(trace)
return 0

Expand Down Expand Up @@ -238,6 +241,7 @@ def loop(max=None):
__all__ = [
'init',
'loop',
'trace_map',
]

# vim:ts=4 sts=4 sw=4 et
2 changes: 1 addition & 1 deletion doc/README
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Prerequisites
To build the module, you will need:

* Python 2.6+ or 3.2+
* Cython ≥ 0.19 (only at build time)
* Cython ≥ 0.19.1 (only at build time)

*py-afl-fuzz* requires AFL proper to be installed.

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def get_version():
author_email='jwilk@jwilk.net',
)

min_cython_version = '0.19'
min_cython_version = '0.19.1'
try:
import Cython
except ImportError:
Expand Down
39 changes: 39 additions & 0 deletions tests/target_trace_map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# encoding=UTF-8

# Copyright © 2018 Jakub Wilk <jwilk@jwilk.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import sys

import afl

# pylint: disable=unsupported-assignment-operation

def main():
s = sys.stdin.read()
s.encode('ASCII')
for c in s:
afl.trace_map[ord(c)] += 1

if __name__ == '__main__':
afl.init()
main()

# vim:ts=4 sts=4 sw=4 et
2 changes: 2 additions & 0 deletions tests/test_fuzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ def t(target):
)
yield t, 'target.py'
yield t, 'target_persistent.py'
if not dumb:
yield t, 'target_trace_map.py'

def test_fuzz_dumb():
if get_afl_version() < '1.95':
Expand Down
1 change: 1 addition & 0 deletions tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
exports = [
'init',
'loop',
'trace_map',
]

deprecated = [
Expand Down