Skip to content

Commit f5ffa06

Browse files
committed
Use FNV32A for code location hashing.
1 parent 722478b commit f5ffa06

File tree

6 files changed

+33
-15
lines changed

6 files changed

+33
-15
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ install:
2020
script:
2121
- python setup.py install
2222
- export PATH="$PWD/deps/bin:$PATH"
23-
- echo 0 | py-afl-showmap -q -o out0 python test.py
24-
- echo 1 | py-afl-showmap -q -o out1 python test.py
23+
- echo 0 | afl-showmap -q -o out0 python test.py
24+
- echo 1 | afl-showmap -q -o out1 python test.py
2525
- '! diff -u out0 out1'
2626
- find -name "*.rst" | xargs -L1 -t -I{} rst2xml.py --strict {} /dev/null
2727

afl.pyx

+23-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# SOFTWARE.
2020

2121
#cython: autotestdict=False
22+
#cython: c_string_encoding=default
2223

2324
'''
2425
American fuzzy lop fork server and instrumentation for pure-Python code
@@ -38,17 +39,38 @@ DEF MAP_SIZE = 1 << MAP_SIZE_POW2
3839

3940
from cpython.exc cimport PyErr_SetFromErrno
4041
from libc cimport errno
42+
from libc.string cimport strlen
43+
from libc.stdint cimport uint32_t
44+
from libc.stddef cimport size_t
4145

4246
cdef extern from 'sys/shm.h':
4347
unsigned char *shmat(int shmid, void *shmaddr, int shmflg)
4448

4549
cdef unsigned char *afl_area = NULL
4650
cdef unsigned long prev_location = 0
4751

52+
DEF FNV32_INIT = 0x811C9DC5U
53+
DEF FNV32_PRIME = 0x01000193U
54+
55+
cdef inline uint32_t fnv32a(const char *key, size_t len, size_t offset):
56+
# 32-bit Fowler–Noll–Vo hash function
57+
cdef uint32_t h = FNV32_INIT
58+
while len > 0:
59+
h ^= <unsigned char> key[0];
60+
h *= FNV32_PRIME
61+
len -= 1
62+
key += 1
63+
while offset > 0:
64+
h ^= <unsigned char> offset;
65+
h *= FNV32_PRIME
66+
offset >>= 8
67+
return h
68+
4869
def trace(frame, event, arg):
4970
global prev_location
5071
cdef unsigned long location, offset
51-
location = hash((frame.f_code.co_filename, frame.f_lineno))
72+
cdef const char * filename = frame.f_code.co_filename
73+
location = fnv32a(filename, strlen(filename), frame.f_lineno)
5274
location %= MAP_SIZE
5375
offset = location ^ prev_location
5476
prev_location = location // 2

doc/changelog

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
python-afl (0.3) UNRELEASED; urgency=low
22

33
* Implement persistent mode.
4-
* Don't verify the value of PYTHONHASHSEED.
5-
Having it set is not strictly necessary, although the py-afl-* tools still
6-
set it to 0.
7-
* Remove the AflError class. It was only used for PYTHONHASHSEED check.
4+
* Don't rely on the Python hash() function for computing code location
5+
identifiers.
6+
* Don't set PYTHONHASHSEED in py-afl-fuzz.
7+
* Remove the py-afl-showmap command.
8+
afl-showmap proper can be now used for Python code.
9+
* Remove the AflError class. It was only used for checking PYTHONHASHSEED.
810
* Run Cython only in those setup.py commands that actually build extensions.
911

10-
-- Jakub Wilk <jwilk@jwilk.net> Sat, 29 Aug 2015 23:41:49 +0200
12+
-- Jakub Wilk <jwilk@jwilk.net> Sun, 30 Aug 2015 18:48:52 +0200
1113

1214
python-afl (0.2.1) unstable; urgency=low
1315

py-afl-fuzz

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/bin/sh
2-
export PYTHONHASHSEED=0
32
export AFL_SKIP_CHECKS=1 # AFL << 1.20b
43
export AFL_SKIP_BIN_CHECK=1 # AFL >= 1.20b
54
export AFL_DUMB_FORKSRV=1

py-afl-showmap

-5
This file was deleted.

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __iter__(self):
7272
author='Jakub Wilk',
7373
author_email='jwilk@jwilk.net',
7474
ext_modules=lazylist(cython_build.cythonize('afl.pyx')),
75-
scripts=['py-afl-fuzz', 'py-afl-showmap'],
75+
scripts=['py-afl-fuzz'],
7676
)
7777

7878
# vim:ts=4 sts=4 sw=4 et

0 commit comments

Comments
 (0)