Skip to content
Open
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
39 changes: 39 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,45 @@ jobs:
- name: Run miri
run: MIRIFLAGS=-Zmiri-ignore-leaks cargo miri test --features="alloc,defmt,mpmc_large,portable-atomic-critical-section,serde,ufmt,bytes"

# Run cargo-fuzz tests on nightly
testfuzz:
name: testfuzz
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Cache cargo dependencies
uses: actions/cache@v3
with:
path: |
- ~/.cargo/bin/
- ~/.cargo/registry/index/
- ~/.cargo/registry/cache/
- ~/.cargo/git/db/
key: ${{ runner.OS }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.OS }}-cargo-

- name: Cache build output dependencies
uses: actions/cache@v3
with:
path: target
key: ${{ runner.OS }}-build-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.OS }}-build-

- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly

- name: Run cargo-fuzz test
run:
- cargo install --locked cargo-fuzz
- cargo +nightly fuzz run fuzz_vec -- -runs=16384 -max_len=4096
- cargo +nightly fuzz run fuzz_string -- -runs=16384 -max_len=4096

# Run cargo test
test:
name: test
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Make MSRV of 1.87.0 explicit.

### Added

- Added `Vec` fuzz test harness + CI runner.

## [v0.9.1] - 2025-08-19

### Added
Expand Down
4 changes: 4 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
corpus
artifacts
coverage
28 changes: 28 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "heapless-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"

[dependencies.heapless]
path = ".."

[[bin]]
name = "fuzz_vec"
path = "fuzz_targets/fuzz_vec.rs"
test = false
doc = false
bench = false

[[bin]]
name = "fuzz_string"
path = "fuzz_targets/fuzz_string.rs"
test = false
doc = false
bench = false
41 changes: 41 additions & 0 deletions fuzz/fuzz_targets/fuzz_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#![no_main]

use libfuzzer_sys::fuzz_target;

use heapless::{String, Vec};

fn test_string<const N: usize>(data: &[u8]) {
let v16_be = data
.chunks_exact(2)
.map(|c| u16::from_be_bytes([c[0], c[1]]))
.collect::<Vec<u16, N>>();

let v16_le = data
.chunks_exact(2)
.map(|c| u16::from_le_bytes([c[0], c[1]]))
.collect::<Vec<u16, N>>();

String::<N>::from_utf16(v16_be.as_slice()).ok();
String::<N>::from_utf16(v16_le.as_slice()).ok();

Vec::<u8, N>::from_slice(data)
.map_err(|_| ())
.and_then(|v| String::<N>::from_utf8(v).map_err(|_| ()))
.ok();
}

fuzz_target!(|data: &[u8]| {
match data.len() {
0 => (),
len if len <= 16 => test_string::<16>(data),
len if len <= 32 => test_string::<32>(data),
len if len <= 64 => test_string::<64>(data),
len if len <= 128 => test_string::<128>(data),
len if len <= 256 => test_string::<256>(data),
len if len <= 512 => test_string::<512>(data),
len if len <= 1024 => test_string::<1024>(data),
len if len <= 2048 => test_string::<2048>(data),
len if len <= 4096 => test_string::<4096>(data),
_ => (),
}
});
27 changes: 27 additions & 0 deletions fuzz/fuzz_targets/fuzz_vec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![no_main]

use libfuzzer_sys::fuzz_target;

use heapless::Vec;

fn test_vec<const N: usize>(data: &[u8]) {
if let Ok(vec) = Vec::<u8, N>::from_slice(data) {
assert_eq!(vec.as_slice(), data);
}
}

fuzz_target!(|data: &[u8]| {
match data.len() {
0 => (),
len if len <= 16 => test_vec::<16>(data),
len if len <= 32 => test_vec::<32>(data),
len if len <= 64 => test_vec::<64>(data),
len if len <= 128 => test_vec::<128>(data),
len if len <= 256 => test_vec::<256>(data),
len if len <= 512 => test_vec::<512>(data),
len if len <= 1024 => test_vec::<1024>(data),
len if len <= 2048 => test_vec::<2048>(data),
len if len <= 4096 => test_vec::<4096>(data),
_ => (),
}
});