Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new ybinary parser #389

Merged
merged 18 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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
77 changes: 77 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ members = [
"libs/jwst-binding/jwst-swift",
"libs/jwst-binding/jwst-swift/jwst-swift-integrate",
# "libs/jwst-binding/jwst-wasm",
"libs/jwst-codec",
"libs/jwst-codec/fuzz",
"libs/jwst-logger",
"libs/jwst-rpc",
"libs/jwst-storage",
Expand Down
18 changes: 18 additions & 0 deletions libs/jwst-codec/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "jwst-codec"
version = "0.1.0"
authors = ["DarkSky <darksky2048@gmail.com>"]
edition = "2021"
license = "AGPL-3.0-only"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bitvec = "1.0.1"
byteorder = "1.4.3"
nom = "7.1.3"
serde_json = "1.0.94"

[dev-dependencies]
lib0 = "0.16.5"
serde = { version = "1.0.155", features = ["derive"] }
4 changes: 4 additions & 0 deletions libs/jwst-codec/fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
corpus
artifacts
coverage
43 changes: 43 additions & 0 deletions libs/jwst-codec/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[package]
name = "jwst-codec-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
lib0 = "0.16.5"

[dependencies.jwst-codec]
path = ".."

[profile.release]
debug = 1

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

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

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


[[bin]]
name = "u64_encode"
path = "fuzz_targets/u64_encode.rs"
test = false
doc = false
22 changes: 22 additions & 0 deletions libs/jwst-codec/fuzz/fuzz_targets/i64_decode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![no_main]

use jwst_codec::{read_var_i64, write_var_i64};
use lib0::encoding::Write;
use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: Vec<i64>| {
for i in data {
if i == i64::MIN {
continue;
}

let mut buf1 = Vec::new();
write_var_i64(&mut buf1, i).unwrap();

let mut buf2 = Vec::new();
buf2.write_var(i);

assert_eq!(read_var_i64(&buf1).unwrap().1, i);
assert_eq!(read_var_i64(&buf2).unwrap().1, i);
}
});
22 changes: 22 additions & 0 deletions libs/jwst-codec/fuzz/fuzz_targets/i64_encode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![no_main]

use jwst_codec::write_var_i64;
use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: Vec<i64>| {
use lib0::encoding::Write;

for i in data {
let mut buf1 = Vec::new();
write_var_i64(&mut buf1, i).unwrap();

// lib0 will crash at this point
if i == i64::MIN {
continue;
}
let mut buf2 = Vec::new();
buf2.write_var(i);

assert_eq!(buf1, buf2);
}
});
18 changes: 18 additions & 0 deletions libs/jwst-codec/fuzz/fuzz_targets/u64_decode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![no_main]

use jwst_codec::{read_var_u64, write_var_u64};
use lib0::encoding::Write;
use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: Vec<u64>| {
for i in data {
let mut buf1 = Vec::new();
write_var_u64(&mut buf1, i).unwrap();

let mut buf2 = Vec::new();
buf2.write_var(i);

assert_eq!(read_var_u64(&buf1).unwrap().1, i);
assert_eq!(read_var_u64(&buf2).unwrap().1, i);
}
});
17 changes: 17 additions & 0 deletions libs/jwst-codec/fuzz/fuzz_targets/u64_encode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![no_main]

use jwst_codec::write_var_u64;
use lib0::encoding::Write;
use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: Vec<u64>| {
for i in data {
let mut buf1 = Vec::new();
buf1.write_var(i);

let mut buf2 = Vec::new();
write_var_u64(&mut buf2, i).unwrap();

assert_eq!(buf1, buf2);
}
});
50 changes: 50 additions & 0 deletions libs/jwst-codec/src/codec/buffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use super::*;
use nom::bytes::complete::take;

pub fn read_var_buffer(input: &[u8]) -> IResult<&[u8], &[u8]> {
let (tail, len) = read_var_u64(input)?;
let (tail, val) = take(len)(tail)?;
Ok((tail, val))
}

#[cfg(test)]
mod tests {
use super::*;
use nom::{
error::{Error, ErrorKind},
Err,
};

#[test]
fn test_read_var_buffer() {
// Test case 1: valid input, buffer length = 5
let input = [0x05, 0x01, 0x02, 0x03, 0x04, 0x05];
let expected_output = [0x01, 0x02, 0x03, 0x04, 0x05];
let result = read_var_buffer(&input);
assert_eq!(result, Ok((&[][..], &expected_output[..])));

// Test case 2: truncated input, missing buffer
let input = [0x05, 0x01, 0x02, 0x03];
let result = read_var_buffer(&input);
assert_eq!(
result,
Err(Err::Error(Error::new(&input[1..], ErrorKind::Eof)))
);

// Test case 3: invalid input
let input = [0xFF, 0x01, 0x02, 0x03];
let result = read_var_buffer(&input);
assert_eq!(
result,
Err(Err::Error(Error::new(&input[2..], ErrorKind::Eof)))
);

// Test case 4: invalid var int encoding
let input = [0xFF, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01];
let result = read_var_buffer(&input);
assert_eq!(
result,
Err(Err::Error(Error::new(&input[7..], ErrorKind::Eof)))
);
}
}
Loading