Skip to content

Commit

Permalink
Run fuzzer in CI
Browse files Browse the repository at this point in the history
As we do for `rust-bitcoin` run the fuzzer on each push and pull
request. Requires:

- Minor changes to fuzz target `encode_decode` to use current API
- Installing hongfuzz without default dependencies (`arbitrary`)
  • Loading branch information
tcharding committed Mar 22, 2023
1 parent bd4e8e2 commit ea84275
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/fuzz.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Fuzz

on: [push, pull_request]

jobs:

fuzz:
if: ${{ !github.event.act }}
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
fuzz_target: [decode_rnd, encode_decode]
steps:
- name: Install test dependencies
run: sudo apt-get update -y && sudo apt-get install -y binutils-dev libunwind8-dev libcurl4-openssl-dev libelf-dev libdw-dev cmake gcc libiberty-dev
- uses: actions/checkout@v2
- uses: actions/cache@v2
id: cache-fuzz
with:
path: |
~/.cargo/bin
fuzz/target
target
key: cache-${{ matrix.target }}-${{ hashFiles('**/Cargo.toml','**/Cargo.lock') }}
- name: Checkout Toolchain
uses: dtolnay/rust-toolchain@stable
- name: fuzz
run: cd fuzz && ./fuzz.sh "${{ matrix.fuzz_target }}"
- run: echo "${{ matrix.fuzz_target }}.rs" >executed_${{ matrix.fuzz_target }}
- uses: actions/upload-artifact@v2
with:
name: executed_${{ matrix.fuzz_target }}
path: executed_${{ matrix.fuzz_target }}

verify-execution:
if: ${{ !github.event.act }}
needs: fuzz
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/download-artifact@v2
- name: Display structure of downloaded files
run: ls -R
- run: find executed_* -type f -exec cat {} + | sort > executed
- run: ls bitcoin/fuzz/fuzz_targets | sort > expected
- run: diff expected executed
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ afl_fuzz = ["afl"]
honggfuzz_fuzz = ["honggfuzz"]

[dependencies]
honggfuzz = { version = "0.5", optional = true }
honggfuzz = { version = "0.5", default-features = false, optional = true }
afl = { version = "0.3", optional = true }
libc = "0.2"
bech32 = { path = ".." }
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
set -e
cargo install --force honggfuzz
cargo install --force honggfuzz --no-default-features
for TARGET in fuzz_targets/*; do
FILENAME=$(basename $TARGET)
FILE="${FILENAME%.*}"
Expand Down
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/encode_decode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate bech32;

use std::str::FromStr;
use std::convert::TryFrom;

fn do_test(data: &[u8]) {
if data.len() < 1 {
Expand All @@ -19,7 +19,7 @@ fn do_test(data: &[u8]) {

let dp = data[hrp_end..]
.iter()
.map(|b| bech32::u5::try_from_u8(b % 32).unwrap())
.map(|b| bech32::u5::try_from(b % 32).unwrap())
.collect::<Vec<_>>();

let variant = if data[0] > 0x0f {
Expand Down

0 comments on commit ea84275

Please sign in to comment.