Skip to content

Commit

Permalink
Run tests with Miri
Browse files Browse the repository at this point in the history
  • Loading branch information
ia0 committed Dec 18, 2022
1 parent 804669e commit fc119cb
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 23 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
components: "clippy,rustfmt"
components: "clippy,miri,rustfmt"
toolchain: nightly
- uses: actions-rs/cargo@v1
with:
Expand Down Expand Up @@ -209,6 +209,11 @@ jobs:
args: "--manifest-path=www/Cargo.toml"
command: test
toolchain: nightly
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/Cargo.toml test"
command: miri
toolchain: nightly
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/Cargo.toml"
Expand Down
5 changes: 5 additions & 0 deletions lib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Changelog

### Minor

- Bump MSRV from 1.46 to 1.47

### Patch

- Use TryInto for slice to array conversion
- Update documentation with reference to attack vectors

## 2.3.3
Expand Down
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "2.3.3"
authors = ["Julien Cretin <git@ia0.eu>"]
license = "MIT"
edition = "2018"
rust-version = "1.46"
rust-version = "1.47"
keywords = ["no_std", "base64", "base32", "hex"]
categories = ["encoding", "no-std"]
readme = "README.md"
Expand Down
10 changes: 3 additions & 7 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ use alloc::string::String;
use alloc::vec;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::convert::TryInto;

macro_rules! check {
($e: expr, $c: expr) => {
Expand Down Expand Up @@ -251,11 +252,6 @@ unsafe fn chunk_mut_unchecked(x: &mut [u8], n: usize, i: usize) -> &mut [u8] {
core::slice::from_raw_parts_mut(ptr, n)
}

unsafe fn as_array(x: &[u8]) -> &[u8; 256] {
debug_assert_eq!(x.len(), 256);
&*(x.as_ptr() as *const [u8; 256])
}

fn div_ceil(x: usize, m: usize) -> usize {
(x + m - 1) / m
}
Expand Down Expand Up @@ -1220,11 +1216,11 @@ impl Default for Specification {

impl Encoding {
fn sym(&self) -> &[u8; 256] {
unsafe { as_array(&self.0[0 .. 256]) }
self.0[0 .. 256].try_into().unwrap()
}

fn val(&self) -> &[u8; 256] {
unsafe { as_array(&self.0[256 .. 512]) }
self.0[256 .. 512].try_into().unwrap()
}

fn pad(&self) -> Option<u8> {
Expand Down
21 changes: 10 additions & 11 deletions lib/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,16 @@ fn ignore() {
let no_pad = spec.encoding().unwrap();
spec.padding = Some('=');
let padded = spec.encoding().unwrap();
if cfg!(debug_assertions) {
forall(&no_pad, b"0 ", 14);
forall(&no_pad, b"0 .", 9);
forall(&padded, b"0= ", 9);
forall(&padded, b"0= .", 7);
} else {
forall(&no_pad, b"0 ", 18);
forall(&no_pad, b"0 .", 11);
forall(&padded, b"0= ", 11);
forall(&padded, b"0= .", 9);
}
#[cfg(miri)]
const MAX: [usize; 4] = [4, 3, 3, 2];
#[cfg(all(not(miri), debug_assertions))]
const MAX: [usize; 4] = [14, 9, 9, 7];
#[cfg(all(not(miri), not(debug_assertions)))]
const MAX: [usize; 4] = [18, 11, 11, 9];
forall(&no_pad, b"0 ", MAX[0]);
forall(&no_pad, b"0 .", MAX[1]);
forall(&padded, b"0= ", MAX[2]);
forall(&padded, b"0= .", MAX[3]);
assert_eq!(padded.decode(b"000=====").unwrap(), [0]);
assert_eq!(padded.decode(b"000 =====").unwrap(), [0]);
assert_eq!(padded.decode(b"000000==").unwrap(), [0, 0]);
Expand Down
1 change: 0 additions & 1 deletion nostd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ authors = ["Julien Cretin <cretin@google.com>"]
edition = "2021"
license = "MIT"
publish = false
resolver = "2"

[features]
alloc = ["data-encoding/alloc"]
Expand Down
2 changes: 1 addition & 1 deletion nostd/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(lang_items, default_alloc_error_handler)]
#![no_std]
#![no_main]
#![feature(lang_items)]

use core::fmt::Write;

Expand Down
11 changes: 10 additions & 1 deletion xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ enum Task {
#[strum(serialize = "test")]
Test,

#[strum(serialize = "miri")]
Miri,

#[strum(serialize = "bench")]
Bench,

Expand All @@ -108,8 +111,9 @@ impl Action {
let default_args: &[&str] = match (self.task, self.dir) {
(Task::Format, _) => &["--", "--check"],
(Task::Clippy, _) => &["--", "--deny=warnings"],
(Task::Audit, _) => &["--deny=warnings"],
(Task::Build, Dir::Nostd) => &["--release"],
(Task::Miri, _) => &["test"],
(Task::Audit, _) => &["--deny=warnings"],
_ => &[],
};
instructions += Instruction {
Expand Down Expand Up @@ -367,6 +371,7 @@ impl Flags {
.filter_map(|x| match x.task {
Task::Format => Some("rustfmt"),
Task::Clippy => Some("clippy"),
Task::Miri => Some("miri"),
_ => None,
})
.collect();
Expand Down Expand Up @@ -429,6 +434,10 @@ impl Actions {
// Clippy is currently broken on cmp and www.
continue;
}
if task == Task::Miri && !matches!(dir, Dir::Lib) {
// Miri is slow, so only run where it matters.
continue;
}
if task == Task::Bench && !matches!(dir, Dir::Lib | Dir::Bin) {
// Bench is only supported for lib and bin.
continue;
Expand Down

0 comments on commit fc119cb

Please sign in to comment.