Skip to content

Commit

Permalink
Run tests with Miri (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
ia0 committed Dec 18, 2022
1 parent 804669e commit 5c7924d
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 53 deletions.
51 changes: 28 additions & 23 deletions .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: test --manifest-path=lib/Cargo.toml
command: miri
toolchain: nightly
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/Cargo.toml"
Expand Down Expand Up @@ -320,57 +325,57 @@ jobs:
toolchain: stable
- uses: actions-rs/toolchain@v1
with:
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/Cargo.toml"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/Cargo.toml --release"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/Cargo.toml --no-default-features --features=alloc"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/Cargo.toml --release --no-default-features --features=alloc"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/Cargo.toml --no-default-features"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/Cargo.toml --release --no-default-features"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/macro/internal/Cargo.toml"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/macro/internal/Cargo.toml --release"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/macro/Cargo.toml"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/macro/Cargo.toml --release"
command: build
toolchain: "1.46"
toolchain: "1.47"
windows:
runs-on: windows-latest
steps:
Expand Down Expand Up @@ -503,54 +508,54 @@ jobs:
toolchain: stable
- uses: actions-rs/toolchain@v1
with:
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/Cargo.toml"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/Cargo.toml --release"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/Cargo.toml --no-default-features --features=alloc"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/Cargo.toml --release --no-default-features --features=alloc"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/Cargo.toml --no-default-features"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/Cargo.toml --release --no-default-features"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/macro/internal/Cargo.toml"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/macro/internal/Cargo.toml --release"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/macro/Cargo.toml"
command: build
toolchain: "1.46"
toolchain: "1.47"
- uses: actions-rs/cargo@v1
with:
args: "--manifest-path=lib/macro/Cargo.toml --release"
command: build
toolchain: "1.46"
toolchain: "1.47"
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
2 changes: 1 addition & 1 deletion lib/macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.12"
authors = ["Julien Cretin <cretin@google.com>"]
license = "MIT"
edition = "2018"
rust-version = "1.46"
rust-version = "1.47"
keywords = ["no_std", "base64", "base32", "hex", "macro"]
categories = ["encoding", "no-std"]
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion lib/macro/internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.10"
authors = ["Julien Cretin <cretin@google.com>"]
license = "MIT"
edition = "2018"
rust-version = "1.46"
rust-version = "1.47"
description = "Internal library for data-encoding-macro"
readme = "README.md"
repository = "https://github.com/ia0/data-encoding"
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
25 changes: 18 additions & 7 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enum Toolchain {
#[strum(serialize = "stable")]
Stable,

#[strum(serialize = "1.46")]
#[strum(serialize = "1.47")]
Msrv,
}

Expand Down 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 @@ -199,12 +203,14 @@ impl Instruction {
step.uses = Some("actions-rs/cargo@v1".to_owned());
step.with.insert("toolchain".to_owned(), toolchain.to_string());
step.with.insert("command".to_owned(), self.cmd.to_owned());
let mut args = format!("--manifest-path={dir}/Cargo.toml");
for arg in &self.args {
args.push(' ');
args.push_str(&shell_escape::escape(arg.into()));
let mut args: Vec<Cow<str>> =
vec![format!("--manifest-path={dir}/Cargo.toml").into()];
args.extend(self.args.iter().map(|x| shell_escape::escape(x.into())));
if self.cmd == "miri" {
// Miri expects the sub-command before the cargo options.
args.rotate_left(1);
}
step.with.insert("args".to_owned(), args);
step.with.insert("args".to_owned(), args.join(" "));
}
Executor::Shell => {
let mut cmd = format!("cd {} && ", shell_escape::escape(dir.to_string().into()));
Expand Down Expand Up @@ -367,6 +373,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 +436,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 5c7924d

Please sign in to comment.