Skip to content

Commit e917031

Browse files
authored
AFL fuzzing (#91)
Authored-by: Jayant Krishnamurthy <jkrishnamurthy@jumptrading.com>
1 parent 9f98d16 commit e917031

File tree

6 files changed

+439
-0
lines changed

6 files changed

+439
-0
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,11 @@ target_link_libraries( leader_stats ${PC_DEP} )
110110

111111
add_test( test_unit test_unit )
112112
add_test( test_net test_net )
113+
114+
115+
#
116+
# fuzz testing application
117+
#
118+
119+
add_executable( fuzz pctest/fuzz.cpp )
120+
target_link_libraries( fuzz ${PC_DEP} )

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# pyth-client
22
client API for on-chain pyth programs
33

4+
### Build Instructions
5+
46
```
57
# depends on openssl
68
apt install libssl-dev
@@ -23,3 +25,39 @@ make
2325
# run unit tests
2426
ctest
2527
```
28+
29+
### Fuzzing
30+
31+
Build a docker image for running fuzz tests:
32+
33+
```
34+
docker build . --platform linux/amd64 -f docker/fuzz/Dockerfile -t pyth-fuzz
35+
```
36+
37+
Each fuzz test is invoked via an argument to the `fuzz` command-line program,
38+
and has a corresponding set of test cases in the subdirectory with the same name as the test.
39+
You can run these tests using a command like:
40+
41+
```
42+
docker run -t \
43+
--platform linux/amd64 \
44+
-v "$(pwd)"/findings:/home/pyth/pyth-client/findings \
45+
pyth-fuzz \
46+
sh -c "./afl/afl-fuzz -i ./pyth-client/pyth/tests/fuzz/add/testcases -o ./pyth-client/findings ./pyth-client/build/fuzz add"
47+
```
48+
49+
This command will run the `add` fuzz test on the tests cases in `pyth/tests/fuzz/add/testcases`, saving any outputs to `findings/`.
50+
Note that `findings/` is shared between the host machine and the docker container, so you can inspect any error cases
51+
by looking in that subdirectory on the host.
52+
53+
If you find an error case that you want to investigate further, you can run the program on the failing input using something like:
54+
55+
```
56+
docker run -t \
57+
--platform linux/amd64 \
58+
-v "$(pwd)"/findings:/home/pyth/pyth-client/findings \
59+
pyth-fuzz \
60+
sh -c "./pyth-client/build/fuzz add < ./pyth-client/findings/crashes/id\:000000\,sig\:06\,src\:000000\,op\:flip1\,pos\:0"
61+
```
62+
63+
in this example, `id\:000000\,sig\:06\,src\:000000\,op\:flip1\,pos\:0` is the file containing the failing input.

docker/fuzz/Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM ubuntu:20.04
2+
3+
ENV DEBIAN_FRONTEND=noninteractive
4+
RUN apt-get update
5+
RUN apt-get install -qq cmake curl git libzstd1 libzstd-dev python3-pytest sudo zlib1g zlib1g-dev libssl-dev clang llvm
6+
7+
# Grant sudo access to pyth user
8+
RUN echo "pyth ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
9+
10+
RUN useradd -m pyth
11+
USER pyth
12+
WORKDIR /home/pyth
13+
14+
# Install AFL
15+
ENV CC=clang
16+
ENV CXX=clang++
17+
RUN git clone https://github.com/google/AFL.git --branch v2.57b afl
18+
RUN cd afl && make && cd llvm_mode && make && cd /home/pyth
19+
20+
# Build everything with the AFL compilers
21+
ENV CC=/home/pyth/afl/afl-clang-fast
22+
ENV CXX=/home/pyth/afl/afl-clang-fast++
23+
24+
COPY --chown=pyth:pyth . pyth-client/
25+
RUN cd pyth-client && mkdir build && cd build && cmake .. && make

0 commit comments

Comments
 (0)