Skip to content

Commit

Permalink
Release 0.6.4, fix unaligned test buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
djkoloski committed Aug 5, 2021
1 parent 0b0e500 commit ed3fdcd
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 145 deletions.
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,58 +21,70 @@ struct Test {
c: char,
}

#[repr(C, align(16))]
struct Aligned<const N: usize>([u8; N]);

macro_rules! bytes {
($($byte:literal,)*) => {
(&Aligned([$($byte,)*]).0 as &[u8]).as_ptr()
};
($($byte:literal),*) => {
bytes!($($byte,)*)
};
}

fn main() {
// This type is laid out as (u32, char, bool)
unsafe {
// These are valid bytes for (0, 'x', true)
Test::check_bytes(
&[
bytes![
0u8, 0u8, 0u8, 0u8, 0x78u8, 0u8, 0u8, 0u8,
1u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap();

// Changing the bytes for the u32 is OK, any bytes are a valid u32
Test::check_bytes(
&[
bytes![
42u8, 16u8, 20u8, 3u8, 0x78u8, 0u8, 0u8, 0u8,
1u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap();

// Characters outside the valid ranges are invalid
Test::check_bytes(
&[
bytes![
0u8, 0u8, 0u8, 0u8, 0x00u8, 0xd8u8, 0u8, 0u8,
1u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap_err();
Test::check_bytes(
&[
bytes![
0u8, 0u8, 0u8, 0u8, 0x00u8, 0x00u8, 0x11u8, 0u8,
1u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap_err();

// 0 is a valid boolean value (false) but 2 is not
Test::check_bytes(
&[
bytes![
0u8, 0u8, 0u8, 0u8, 0x78u8, 0u8, 0u8, 0u8,
0u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap();
Test::check_bytes(
&[
bytes![
0u8, 0u8, 0u8, 0u8, 0x78u8, 0u8, 0u8, 0u8,
2u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap_err();
}
}
```
```
4 changes: 2 additions & 2 deletions bytecheck/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bytecheck"
version = "0.6.3"
version = "0.6.4"
authors = ["David Koloski <djkoloski@gmail.com>"]
edition = "2018"
description = "Derive macro for bytecheck"
Expand All @@ -14,7 +14,7 @@ readme = "crates-io.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bytecheck_derive = { version = "=0.6.3", path = "../bytecheck_derive", default-features = false }
bytecheck_derive = { version = "=0.6.4", path = "../bytecheck_derive", default-features = false }
ptr_meta = "0.1"
simdutf8 = { version = "0.1", default-features = false, optional = true }

Expand Down
42 changes: 27 additions & 15 deletions bytecheck/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# bytecheck &emsp; [![Latest Version]][crates.io] [![License]][license path] [![requires: rustc 1.47+]][Rust 1.47]
# bytecheck &emsp; [![Latest Version]][crates.io] [![License]][license path] [![requires: rustc 1.52+]][Rust 1.52]

[Latest Version]: https://img.shields.io/crates/v/bytecheck.svg
[crates.io]: https://crates.io/crates/bytecheck
[License]: https://img.shields.io/badge/license-MIT-blue.svg
[license path]: https://github.com/djkoloski/bytecheck/blob/master/LICENSE
[requires: rustc 1.47+]: https://img.shields.io/badge/rustc-1.47+-lightgray.svg
[Rust 1.47]: https://blog.rust-lang.org/2020/10/08/Rust-1.47.html
[requires: rustc 1.52+]: https://img.shields.io/badge/rustc-1.52+-lightgray.svg
[Rust 1.52]: https://blog.rust-lang.org/2021/05/06/Rust-1.52.0.html

bytecheck is a type validation framework for Rust.

Expand All @@ -21,56 +21,68 @@ struct Test {
c: char,
}

#[repr(C, align(16))]
struct Aligned<const N: usize>([u8; N]);

macro_rules! bytes {
($($byte:literal,)*) => {
(&Aligned([$($byte,)*]).0 as &[u8]).as_ptr()
};
($($byte:literal),*) => {
bytes!($($byte,)*)
};
}

fn main() {
// This type is laid out as (u32, char, bool)
unsafe {
// These are valid bytes for (0, 'x', true)
Test::check_bytes(
&[
bytes![
0u8, 0u8, 0u8, 0u8, 0x78u8, 0u8, 0u8, 0u8,
1u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap();

// Changing the bytes for the u32 is OK, any bytes are a valid u32
Test::check_bytes(
&[
bytes![
42u8, 16u8, 20u8, 3u8, 0x78u8, 0u8, 0u8, 0u8,
1u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap();

// Characters outside the valid ranges are invalid
Test::check_bytes(
&[
bytes![
0u8, 0u8, 0u8, 0u8, 0x00u8, 0xd8u8, 0u8, 0u8,
1u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap_err();
Test::check_bytes(
&[
bytes![
0u8, 0u8, 0u8, 0u8, 0x00u8, 0x00u8, 0x11u8, 0u8,
1u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap_err();

// 0 is a valid boolean value (false) but 2 is not
Test::check_bytes(
&[
bytes![
0u8, 0u8, 0u8, 0u8, 0x78u8, 0u8, 0u8, 0u8,
0u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap();
Test::check_bytes(
&[
bytes![
0u8, 0u8, 0u8, 0u8, 0x78u8, 0u8, 0u8, 0u8,
2u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap_err();
}
Expand Down
36 changes: 24 additions & 12 deletions bytecheck/crates-io.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,68 @@ struct Test {
c: char,
}

#[repr(C, align(16))]
struct Aligned<const N: usize>([u8; N]);

macro_rules! bytes {
($($byte:literal,)*) => {
(&Aligned([$($byte,)*]).0 as &[u8]).as_ptr()
};
($($byte:literal),*) => {
bytes!($($byte,)*)
};
}

fn main() {
// This type is laid out as (u32, char, bool)
unsafe {
// These are valid bytes for (0, 'x', true)
Test::check_bytes(
&[
bytes![
0u8, 0u8, 0u8, 0u8, 0x78u8, 0u8, 0u8, 0u8,
1u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap();

// Changing the bytes for the u32 is OK, any bytes are a valid u32
Test::check_bytes(
&[
bytes![
42u8, 16u8, 20u8, 3u8, 0x78u8, 0u8, 0u8, 0u8,
1u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap();

// Characters outside the valid ranges are invalid
Test::check_bytes(
&[
bytes![
0u8, 0u8, 0u8, 0u8, 0x00u8, 0xd8u8, 0u8, 0u8,
1u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap_err();
Test::check_bytes(
&[
bytes![
0u8, 0u8, 0u8, 0u8, 0x00u8, 0x00u8, 0x11u8, 0u8,
1u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap_err();

// 0 is a valid boolean value (false) but 2 is not
Test::check_bytes(
&[
bytes![
0u8, 0u8, 0u8, 0u8, 0x78u8, 0u8, 0u8, 0u8,
0u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap();
Test::check_bytes(
&[
bytes![
0u8, 0u8, 0u8, 0u8, 0x78u8, 0u8, 0u8, 0u8,
2u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
] as *const u8,
].cast(),
&()
).unwrap_err();
}
Expand Down
35 changes: 23 additions & 12 deletions bytecheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,56 +29,67 @@
//! b: bool,
//! c: char,
//! }
//! #[repr(C, align(16))]
//! struct Aligned<const N: usize>([u8; N]);
//!
//! macro_rules! bytes {
//! ($($byte:literal,)*) => {
//! (&Aligned([$($byte,)*]).0 as &[u8]).as_ptr()
//! };
//! ($($byte:literal),*) => {
//! bytes!($($byte,)*)
//! };
//! }
//!
//! // This type is laid out as (u32, char, bool)
//! unsafe {
//! // These are valid bytes for (0, 'x', true)
//! Test::check_bytes(
//! (&[
//! bytes![
//! 0u8, 0u8, 0u8, 0u8, 0x78u8, 0u8, 0u8, 0u8,
//! 1u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
//! ] as *const u8).cast(),
//! ].cast(),
//! &mut ()
//! ).unwrap();
//!
//! // Changing the bytes for the u32 is OK, any bytes are a valid u32
//! Test::check_bytes(
//! (&[
//! bytes![
//! 42u8, 16u8, 20u8, 3u8, 0x78u8, 0u8, 0u8, 0u8,
//! 1u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
//! ] as *const u8).cast(),
//! ].cast(),
//! &mut ()
//! ).unwrap();
//!
//! // Characters outside the valid ranges are invalid
//! Test::check_bytes(
//! (&[
//! bytes![
//! 0u8, 0u8, 0u8, 0u8, 0x00u8, 0xd8u8, 0u8, 0u8,
//! 1u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
//! ] as *const u8).cast(),
//! ].cast(),
//! &mut ()
//! ).unwrap_err();
//! Test::check_bytes(
//! (&[
//! bytes![
//! 0u8, 0u8, 0u8, 0u8, 0x00u8, 0x00u8, 0x11u8, 0u8,
//! 1u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
//! ] as *const u8).cast(),
//! ].cast(),
//! &mut ()
//! ).unwrap_err();
//!
//! // 0 is a valid boolean value (false) but 2 is not
//! Test::check_bytes(
//! (&[
//! bytes![
//! 0u8, 0u8, 0u8, 0u8, 0x78u8, 0u8, 0u8, 0u8,
//! 0u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
//! ] as *const u8).cast(),
//! ].cast(),
//! &mut ()
//! ).unwrap();
//! Test::check_bytes(
//! (&[
//! bytes![
//! 0u8, 0u8, 0u8, 0u8, 0x78u8, 0u8, 0u8, 0u8,
//! 2u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8, 255u8
//! ] as *const u8).cast(),
//! ].cast(),
//! &mut ()
//! ).unwrap_err();
//! }
Expand Down
2 changes: 1 addition & 1 deletion bytecheck_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bytecheck_derive"
version = "0.6.3"
version = "0.6.4"
authors = ["David Koloski <djkoloski@gmail.com>"]
edition = "2018"
description = "Derive macro for bytecheck"
Expand Down
2 changes: 1 addition & 1 deletion bytecheck_test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bytecheck_test"
version = "0.6.1"
version = "0.6.4"
authors = ["David Koloski <djkoloski@gmail.com>"]
edition = "2018"
description = "Test suite for bytecheck crates"
Expand Down
Loading

0 comments on commit ed3fdcd

Please sign in to comment.