Skip to content

Commit 289e8ca

Browse files
committed
preparing fuzz
Signed-off-by: JadKHaddad <[email protected]>
1 parent 761d6ac commit 289e8ca

File tree

9 files changed

+112
-15
lines changed

9 files changed

+112
-15
lines changed

bmp180/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ async = ["dep:embedded-hal-async"]
1515
blocking = ["dep:embedded-hal"]
1616
log = ["dep:log"]
1717
i-know-what-i-am-doing = []
18+
fuzz = ["blocking"]
1819

1920
[dependencies]
2021
embedded-hal-async = { version = "1.0.0", optional = true }

bmp180/fuzz/Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bmp180/fuzz/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ members = ["."]
1818
debug = 1
1919

2020
[[bin]]
21-
name = "write_read"
22-
path = "fuzz_targets/write_read.rs"
21+
name = "init_update"
22+
path = "fuzz_targets/init_update.rs"
2323
test = false
2424
doc = false
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![no_main]
2+
3+
use bmp180::{
4+
fuzz::{FuzzDelay, FuzzI2C},
5+
BlockingBMP180, BlockingInitBMP180, UninitBMP180,
6+
};
7+
8+
use libfuzzer_sys::fuzz_target;
9+
10+
fuzz_target!(|data: &[u8]| {
11+
let fuzz_i2c = FuzzI2C::new(data);
12+
13+
let mut bmp180 = UninitBMP180::builder(fuzz_i2c, FuzzDelay {})
14+
.build()
15+
.initialize()
16+
.unwrap();
17+
18+
bmp180.update().unwrap();
19+
});

bmp180/fuzz/fuzz_targets/write_read.rs

-5
This file was deleted.

bmp180/src/device/mod.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,14 @@ mod impl_blocking {
312312
use embedded_hal::{delay::DelayNs, i2c::I2c};
313313

314314
use crate::{
315-
device::register::Register, functionality::blocking::BlockingBMP180, tri, BaseBMP180,
315+
device::register::Register,
316+
functionality::{blocking::BlockingBMP180, PrivateUninitBMP180},
317+
tri, BaseBMP180, BlockingInitBMP180, UninitBMP180,
316318
};
317319

318320
use super::{calibration::Calibration, BMP180};
319321

320-
impl<I2C, DELAY> BlockingBMP180<I2C, DELAY> for BMP180<I2C, DELAY>
322+
impl<I2C, DELAY> BlockingInitBMP180<I2C, DELAY> for UninitBMP180<I2C, DELAY>
321323
where
322324
I2C: I2c,
323325
DELAY: DelayNs,
@@ -343,6 +345,14 @@ mod impl_blocking {
343345

344346
Ok(Calibration::from_slice(&data))
345347
}
348+
}
349+
350+
impl<I2C, DELAY> BlockingBMP180<I2C, DELAY> for BMP180<I2C, DELAY>
351+
where
352+
I2C: I2c,
353+
DELAY: DelayNs,
354+
{
355+
type Error = I2C::Error;
346356

347357
fn read_raw_temperature(&mut self) -> Result<i16, Self::Error> {
348358
tri!(self.i2c.write(

bmp180/src/functionality/blocking.rs

-6
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ pub trait BlockingBMP180<I2C, DELAY>: BaseBMP180<I2C, DELAY> {
1212
/// Error type that can occur during blocking operations.
1313
type Error;
1414

15-
/// Read device ID.
16-
fn read_id(&mut self) -> Result<u8, Self::Error>;
17-
18-
/// Read calibration data.
19-
fn read_calibration(&mut self) -> Result<Calibration, Self::Error>;
20-
2115
/// Read raw temperature.
2216
fn read_raw_temperature(&mut self) -> Result<i16, Self::Error>;
2317

bmp180/src/fuzz.rs

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//! Fuzzing utilities.
2+
3+
use core::convert::Infallible;
4+
5+
use crate::device::{id::Id, register::Register};
6+
7+
/// Fuzzing delay.
8+
///
9+
/// Does nothing.
10+
pub struct FuzzDelay;
11+
12+
impl embedded_hal::delay::DelayNs for FuzzDelay {
13+
fn delay_ns(&mut self, _: u32) {}
14+
}
15+
16+
/// Fuzzing I2C.
17+
///
18+
/// Responds with the correct id. Erverything else is random.
19+
pub struct FuzzI2C<'data> {
20+
/// Data to respond with.
21+
data: &'data [u8],
22+
23+
/// Check if the current write is to the id register.
24+
is_id_write: bool,
25+
}
26+
27+
impl<'data> FuzzI2C<'data> {
28+
/// Create a new `FuzzI2C`.
29+
pub fn new(data: &'data [u8]) -> Self {
30+
Self {
31+
data,
32+
is_id_write: false,
33+
}
34+
}
35+
}
36+
37+
impl embedded_hal::i2c::ErrorType for FuzzI2C<'_> {
38+
type Error = Infallible;
39+
}
40+
41+
impl embedded_hal::i2c::I2c for FuzzI2C<'_> {
42+
fn transaction(
43+
&mut self,
44+
_address: u8,
45+
operations: &mut [embedded_hal::i2c::Operation<'_>],
46+
) -> Result<(), Self::Error> {
47+
for operation in operations {
48+
match operation {
49+
embedded_hal::i2c::Operation::Write(write) => {
50+
if write[0] == Register::ChipId as u8 {
51+
self.is_id_write = true;
52+
} else {
53+
self.is_id_write = false;
54+
}
55+
}
56+
embedded_hal::i2c::Operation::Read(read) => {
57+
if self.is_id_write {
58+
read[0] = Id::Valid as u8;
59+
} else {
60+
if self.data.len() == read.len() {
61+
read.copy_from_slice(self.data);
62+
} else if self.data.len() < read.len() {
63+
read[..self.data.len()].copy_from_slice(self.data);
64+
} else {
65+
read.copy_from_slice(&self.data[..read.len()]);
66+
}
67+
}
68+
}
69+
}
70+
}
71+
72+
Ok(())
73+
}
74+
}

bmp180/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ pub use crate::functionality::asynchronous::{AsyncBMP180, AsyncInitBMP180};
3030
#[cfg(feature = "blocking")]
3131
pub use crate::functionality::blocking::{BlockingBMP180, BlockingInitBMP180};
3232

33+
// #[cfg(feature = "fuzz")]
34+
pub mod fuzz;
35+
3336
/// Our custom `try!` macro aka `?`, to get rid of [`core::convert::From`]/[`core::convert::Into`] used by the `?` operator.
3437
macro_rules! tri {
3538
($e:expr $(,)?) => {

0 commit comments

Comments
 (0)