Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Sep 29, 2024
1 parent 2e71a7f commit d430116
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 44 deletions.
2 changes: 0 additions & 2 deletions ci/miri_sb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@ rustup override set nightly
cargo miri setup

export MIRIFLAGS="-Zmiri-strict-provenance -Zmiri-disable-isolation -Zmiri-symbolic-alignment-check"
export RUSTFLAGS="--cfg test_$CONFIG_FLAGS"

cargo miri test --tests --target $TARGET --lib
2 changes: 0 additions & 2 deletions ci/miri_tb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ rustup override set nightly
cargo miri setup

export MIRIFLAGS="-Zmiri-strict-provenance -Zmiri-disable-isolation -Zmiri-symbolic-alignment-check -Zmiri-tree-borrows"
export RUSTFLAGS="--cfg test_$CONFIG_FLAGS"

cargo miri test --tests --target $TARGET --lib

6 changes: 3 additions & 3 deletions ci/sanitizer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ set -ex
export ASAN_OPTIONS="detect_odr_violation=0 detect_leaks=0"

# Run address sanitizer
RUSTFLAGS="-Z sanitizer=address --cfg all_tests" \
RUSTFLAGS="-Z sanitizer=address" \
cargo test --tests --target x86_64-unknown-linux-gnu --all-features

# Run leak sanitizer
RUSTFLAGS="-Z sanitizer=leak --cfg all_tests" \
RUSTFLAGS="-Z sanitizer=leak" \
cargo test --tests --target x86_64-unknown-linux-gnu --all-features

RUSTFLAGS="--cfg all_tests -Zsanitizer=memory -Zsanitizer-memory-track-origins" \
RUSTDOCFLAGS="-Zsanitizer=memory -Zsanitizer-memory-track-origins" \
cargo test -Zbuild-std --release --tests --target x86_64-unknown-linux-gnu --features memmap

# Run thread sanitizer
RUSTFLAGS="-Z sanitizer=thread --cfg all_tests" \
RUSTFLAGS="-Z sanitizer=thread" \
cargo -Zbuild-std test --tests --target x86_64-unknown-linux-gnu --all-features
28 changes: 28 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,32 @@ impl Error {
_ => unreachable!(),
}
}

#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[inline]
pub(crate) fn from_arena_io_err(e: std::io::Error) -> std::io::Error {
if e.to_string().starts_with("ARENA's magic version mismatch") {
bad_version()
} else {
e
}
}
}

#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[inline]
pub(crate) fn bad_magic_text() -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::InvalidData, "bad magic text")
}

#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[inline]
pub(crate) fn bad_magic_version() -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::InvalidData, "bad magic version")
}

#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[inline]
fn bad_version() -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::InvalidData, "bad version")
}
4 changes: 2 additions & 2 deletions src/log/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ pub trait MutableLog: Log + Mutable {
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
#[inline]
fn flush_range(&self, offset: usize, len: usize) -> std::io::Result<()> {
self.allocator().flush_range(offset, len)
self.allocator().flush_header_and_range(offset, len)
}

/// Asynchronously flushes outstanding memory map modifications in the range to disk.
Expand Down Expand Up @@ -693,7 +693,7 @@ pub trait MutableLog: Log + Mutable {
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
#[inline]
fn flush_async_range(&self, offset: usize, len: usize) -> std::io::Result<()> {
self.allocator().flush_async_range(offset, len)
self.allocator().flush_async_header_and_range(offset, len)
}
}

Expand Down
44 changes: 9 additions & 35 deletions src/options/open_options.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
use rarena_allocator::{either::Either, Allocator};

use super::{
write_header, Builder, Options, CURRENT_VERSION, HEADER_SIZE, MAGIC_TEXT, MAGIC_TEXT_SIZE,
super::error::{bad_magic_text, bad_magic_version, Error},
write_header, Builder, Options, HEADER_SIZE, MAGIC_TEXT, MAGIC_TEXT_SIZE,
};
use crate::{sealed::Constructor, Frozen, Mutable};

#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[inline]
fn bad_magic_text() -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::InvalidData, "bad magic text")
}

#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[inline]
fn bad_magic_version() -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::InvalidData, "bad magic version")
}

#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[inline]
fn bad_version() -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::InvalidData, "bad version")
}

impl Options {
/// Sets the option for read access.
///
Expand Down Expand Up @@ -623,15 +606,11 @@ impl<S> Builder<S> {
.to_arena_options()
.with_unify(true)
.map_with_path_builder::<C::Allocator, _, _>(path_builder)
.map_err(|e| e.map_right(Error::from_arena_io_err))
.and_then(|arena| {
Self::check_header(arena.reserved_slice(), magic_version).map_err(Either::Right)?;
let version = arena.magic_version();
if version != CURRENT_VERSION {
Err(Either::Right(bad_version()))
} else {
let log = C::construct(fid, arena, cks, opts.with_magic_version(magic_version));
Ok(log)
}
Self::check_header(arena.reserved_slice(), magic_version)
.map(|_| C::construct(fid, arena, cks, opts.with_magic_version(magic_version)))
.map_err(Either::Right)
})
}

Expand Down Expand Up @@ -728,21 +707,16 @@ impl<S> Builder<S> {
.to_arena_options()
.with_unify(true)
.map_mut::<C::Allocator, _>(path)
.map_err(Either::Right)
.map_err(|e| Either::Right(crate::error::Error::from_arena_io_err(e)))
.and_then(|arena| {
if !exist {
write_header(arena.reserved_slice_mut(), magic_version);
} else {
Self::check_header(arena.reserved_slice(), magic_version).map_err(Either::Right)?;
}

let version = arena.magic_version();
if version != CURRENT_VERSION {
Err(Either::Right(bad_version()))
} else {
let log = C::construct(fid, arena, cks, opts);
Ok(log)
}
let log = C::construct(fid, arena, cks, opts);
Ok(log)
})
}

Expand Down

0 comments on commit d430116

Please sign in to comment.