-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #355 from Chia-Network/serialized-length-untrusted
`serialized_length_from_bytes()`
- Loading branch information
Showing
9 changed files
with
265 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use clvmr::allocator::Allocator; | ||
use clvmr::serde::node_from_bytes; | ||
use clvmr::serde::node_from_bytes_backrefs; | ||
use clvmr::serde::serialized_length_from_bytes; | ||
use clvmr::serde::serialized_length_from_bytes_trusted; | ||
use criterion::{criterion_group, criterion_main, Criterion, SamplingMode}; | ||
use std::include_bytes; | ||
use std::time::Instant; | ||
|
||
fn deserialize_benchmark(c: &mut Criterion) { | ||
let block = include_bytes!("block_af9c3d98.bin"); | ||
|
||
let mut group = c.benchmark_group("deserialize"); | ||
group.sample_size(10); | ||
group.sampling_mode(SamplingMode::Flat); | ||
|
||
group.bench_function("serialized_length_from_bytes", |b| { | ||
b.iter(|| { | ||
let start = Instant::now(); | ||
let _ = serialized_length_from_bytes(block); | ||
start.elapsed() | ||
}) | ||
}); | ||
|
||
group.bench_function("serialized_length_from_bytes_trusted", |b| { | ||
b.iter(|| { | ||
let start = Instant::now(); | ||
let _ = serialized_length_from_bytes_trusted(block); | ||
start.elapsed() | ||
}) | ||
}); | ||
|
||
let mut a = Allocator::new(); | ||
let iter_checkpoint = a.checkpoint(); | ||
|
||
group.bench_function("node_from_bytes_backrefs", |b| { | ||
b.iter(|| { | ||
a.restore_checkpoint(&iter_checkpoint); | ||
let start = Instant::now(); | ||
let _ = node_from_bytes_backrefs(&mut a, block); | ||
start.elapsed() | ||
}) | ||
}); | ||
|
||
group.bench_function("node_from_bytes", |b| { | ||
b.iter(|| { | ||
a.restore_checkpoint(&iter_checkpoint); | ||
let start = Instant::now(); | ||
let _ = node_from_bytes(&mut a, block); | ||
start.elapsed() | ||
}) | ||
}); | ||
|
||
group.finish(); | ||
} | ||
|
||
criterion_group!(deserialize, deserialize_benchmark); | ||
criterion_main!(deserialize); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,31 @@ | ||
#![no_main] | ||
use clvmr::serde::node_from_bytes_backrefs; | ||
use clvmr::serde::node_to_bytes; | ||
use clvmr::serde::serialized_length_from_bytes; | ||
use clvmr::Allocator; | ||
use libfuzzer_sys::fuzz_target; | ||
|
||
fuzz_target!(|data: &[u8]| { | ||
let _len = match serialized_length_from_bytes(data) { | ||
Err(_) => { | ||
return; | ||
let len = serialized_length_from_bytes(data); | ||
|
||
let mut allocator = Allocator::new(); | ||
let program = node_from_bytes_backrefs(&mut allocator, data); | ||
|
||
match (len, program) { | ||
(Ok(_), Ok(_)) => { | ||
// this is expected | ||
} | ||
(Err(_), Err(_)) => { | ||
// this is expected | ||
} | ||
(Ok(len), Err(e)) => { | ||
panic!("discrepancy between serialized_length and node_from_bytes_backrefs().\n {len}\n{e}"); | ||
} | ||
(Err(e), Ok(program)) => { | ||
panic!( | ||
"discrepancy between serialized_length and node_from_bytes_backrefs().\n {e}\n{:?}", | ||
node_to_bytes(&allocator, program) | ||
); | ||
} | ||
Ok(r) => r, | ||
}; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#![no_main] | ||
use clvmr::serde::serialized_length_from_bytes_trusted; | ||
use libfuzzer_sys::fuzz_target; | ||
|
||
fuzz_target!(|data: &[u8]| { | ||
let _len = match serialized_length_from_bytes_trusted(data) { | ||
Err(_) => { | ||
return; | ||
} | ||
Ok(r) => r, | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters