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

[WIP] Deny OOM, embrace try_reserve #448

Open
wants to merge 13 commits into
base: trunk
Choose a base branch
from
Open
6 changes: 3 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@
"uses": "actions-rs/cargo@v1",
"with": {
"command": "check",
"args": "--all-features"
"args": "--features std,derive"
},
"name": "Run `cargo check`"
},
@@ -158,7 +158,7 @@ fi",
"uses": "actions-rs/cargo@v1",
"with": {
"command": "clippy",
"args": "--all-features -- -D warnings"
"args": "--features std,derive -- -D warnings"
},
"name": "Run `cargo clippy`"
}
@@ -213,7 +213,7 @@ fi",
"uses": "actions-rs/tarpaulin@v0.1",
"with": {
"version": "0.19.1",
"args": "--all --all-features"
"args": "--all --features std,derive"
}
},
{
70 changes: 70 additions & 0 deletions .github/workflows/strict_oom.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"name": "strict OOM",
"on": {
"push": {
"branches": [
"trunk",
"v*.x",
"ci/*"
]
},
"pull_request": {
"branches": [
"trunk",
"v*.x"
]
}
},
"jobs": {
"no_oom": {
"name": "Strict OOM checks",
"runs-on": "ubuntu-latest",
"steps": [
{
"uses": "actions/checkout@v2",
"name": "Checkout"
},
{
"uses": "actions-rs/toolchain@v1",
"with": {
"profile": "minimal",
"toolchain": "nightly",
"components": "rust-src",
"override": true
},
"name": "Install Rust nightly"
},
{
"run": "cargo build --no-default-features --features unstable-strict-oom-checks -Z build-std=core,alloc --target x86_64-unknown-linux-gnu",
"env": {
"RUSTFLAGS": "--cfg no_global_oom_handling"
}
}
]
},
"miri": {
"name": "MIRI",
"runs-on": "ubuntu-latest",
"steps": [
{
"uses": "actions/checkout@v2",
"name": "Checkout"
},
{
"run": "rustup toolchain install nightly --component miri \n
rustup override set nightly \n
cargo miri setup",
"name": "Install Rust nightly"
},
{
"run": "cargo miri test",
"name": "Default features"
},
{
"run": "cargo miri test --no-default-features --features unstable-strict-oom-checks",
"name": "Strict OOM check"
}
]
}
}
}
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -30,6 +30,9 @@ std = ["alloc", "serde?/std"]
alloc = ["serde?/alloc"]
derive = ["bincode_derive"]

# experimental strict OOM checks, requires nightly features
unstable-strict-oom-checks = ["alloc"]

[dependencies]
bincode_derive = { path = "derive", version = "2.0.0-rc.3", optional = true }
serde = { version = "1.0", default-features = false, optional = true }
52 changes: 52 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//! Errors that can be encounting by Encoding and Decoding.

#[cfg(feature = "alloc")]
use alloc::collections::TryReserveError;
#[cfg(all(feature = "alloc", feature = "unstable-strict-oom-checks"))]
use core::alloc::AllocError;

/// Errors that can be encountered by encoding a type
#[non_exhaustive]
#[derive(Debug)]
@@ -51,6 +56,10 @@ pub enum EncodeError {
time: std::boxed::Box<std::time::SystemTime>,
},

/// bincode failed to allocate enough memory
#[cfg(feature = "alloc")]
OutOfMemory(OutOfMemory),

#[cfg(feature = "serde")]
/// A serde-specific error that occurred while decoding.
Serde(crate::features::serde::EncodeError),
@@ -188,6 +197,49 @@ pub enum DecodeError {
#[cfg(feature = "serde")]
/// A serde-specific error that occurred while decoding.
Serde(crate::features::serde::DecodeError),

/// bincode failed to allocate enough memory
#[cfg(feature = "alloc")]
OutOfMemory(OutOfMemory),
}

/// A wrapper to make all the out of memory errors consistent
#[cfg(feature = "alloc")]
#[derive(Debug)]
pub enum OutOfMemory {
/// Failed to reserve an entry
TryReserve(TryReserveError),
#[cfg(feature = "unstable-strict-oom-checks")]
/// Failed to allocate memory
Alloc(AllocError),
}

#[cfg(feature = "alloc")]
impl From<TryReserveError> for DecodeError {
fn from(e: TryReserveError) -> Self {
Self::OutOfMemory(OutOfMemory::TryReserve(e))
}
}

#[cfg(feature = "alloc")]
impl From<TryReserveError> for EncodeError {
fn from(e: TryReserveError) -> Self {
Self::OutOfMemory(OutOfMemory::TryReserve(e))
}
}

#[cfg(all(feature = "alloc", feature = "unstable-strict-oom-checks"))]
impl From<AllocError> for DecodeError {
fn from(e: AllocError) -> Self {
Self::OutOfMemory(OutOfMemory::Alloc(e))
}
}

#[cfg(all(feature = "alloc", feature = "unstable-strict-oom-checks"))]
impl From<AllocError> for EncodeError {
fn from(e: AllocError) -> Self {
Self::OutOfMemory(OutOfMemory::Alloc(e))
}
}

impl core::fmt::Display for DecodeError {
Loading
Loading