From 4bab7ec18376c6db179b3196469599500be6a1aa Mon Sep 17 00:00:00 2001 From: Zachary Dremann Date: Thu, 19 Sep 2024 14:38:26 -0400 Subject: [PATCH 1/2] feat(fuzz): fuzz deserialize better The fuzzer will do a better job finding interesing inputs if we make the type of deserization a branch rather than always trying all types for every input. --- fuzz/Cargo.lock | 2 +- fuzz/fuzz_targets/deserialize.rs | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index c46bc9b..328d703 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -51,7 +51,7 @@ dependencies = [ [[package]] name = "croaring-sys" -version = "4.1.1" +version = "4.1.2" dependencies = [ "cc", ] diff --git a/fuzz/fuzz_targets/deserialize.rs b/fuzz/fuzz_targets/deserialize.rs index 18bf810..1356b54 100644 --- a/fuzz/fuzz_targets/deserialize.rs +++ b/fuzz/fuzz_targets/deserialize.rs @@ -2,6 +2,7 @@ use croaring::{Bitmap, Bitmap64, Native, Portable}; use libfuzzer_sys::fuzz_target; +use libfuzzer_sys::arbitrary::{self, Arbitrary}; fn check_bitmap(input: &[u8]) { let bitmap = Bitmap::try_deserialize::(input); @@ -63,8 +64,18 @@ fn check_bitmap64(input: &[u8]) { } } -fuzz_target!(|input: &[u8]| { - check_bitmap::(input); - check_bitmap::(input); - check_bitmap64::(input); +#[derive(Arbitrary, Debug)] +enum BitmapType { + Portable32, + Native32, + Portable64, +} + +fuzz_target!(|input: (BitmapType, &[u8])| { + let (ty, input) = input; + match ty { + BitmapType::Portable32 => check_bitmap::(input), + BitmapType::Native32 => check_bitmap::(input), + BitmapType::Portable64 => check_bitmap64::(input), + } }); From 0bcef52c0be4353d6ce10c64d2aae6b7971b1eb7 Mon Sep 17 00:00:00 2001 From: Zachary Dremann Date: Wed, 4 Sep 2024 23:59:00 -0400 Subject: [PATCH 2/2] feat: add From for Bitmap64 --- croaring/Cargo.toml | 2 +- croaring/src/bitmap/mod.rs | 2 +- croaring/src/bitmap64/ops.rs | 8 +++++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/croaring/Cargo.toml b/croaring/Cargo.toml index 2eacddc..32220f4 100644 --- a/croaring/Cargo.toml +++ b/croaring/Cargo.toml @@ -25,7 +25,7 @@ roaring = "0.10" criterion = { version = "0.5", features = ["html_reports"] } [dependencies] -ffi = { package = "croaring-sys", path = "../croaring-sys", version = "~4.1.1" } +ffi = { package = "croaring-sys", path = "../croaring-sys", version = "~4.1.4" } [[bench]] name = "benches" diff --git a/croaring/src/bitmap/mod.rs b/croaring/src/bitmap/mod.rs index 417c04a..b38fc98 100644 --- a/croaring/src/bitmap/mod.rs +++ b/croaring/src/bitmap/mod.rs @@ -67,7 +67,7 @@ use core::marker::PhantomData; // &BitmapView and &Bitmap #[repr(transparent)] pub struct Bitmap { - bitmap: ffi::roaring_bitmap_t, + pub(crate) bitmap: ffi::roaring_bitmap_t, } unsafe impl Sync for Bitmap {} diff --git a/croaring/src/bitmap64/ops.rs b/croaring/src/bitmap64/ops.rs index dc60609..4a22403 100644 --- a/croaring/src/bitmap64/ops.rs +++ b/croaring/src/bitmap64/ops.rs @@ -1,4 +1,4 @@ -use crate::Bitmap64; +use crate::{Bitmap, Bitmap64}; use core::fmt; use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Sub, SubAssign}; use ffi::roaring64_bitmap_copy; @@ -45,6 +45,12 @@ impl From<&'_ [u64]> for Bitmap64 { } } +impl From for Bitmap64 { + fn from(mut value: Bitmap) -> Self { + unsafe { Self::take_heap(ffi::roaring64_bitmap_move_from_roaring32(&mut value.bitmap)) } + } +} + impl From<[u64; N]> for Bitmap64 { #[inline] #[doc(alias = "roaring64_bitmap_of_ptr")]