Skip to content

Commit

Permalink
Fix MIRI errors
Browse files Browse the repository at this point in the history
Signed-off-by: Tin Švagelj <[email protected]>
  • Loading branch information
Caellian committed Mar 22, 2024
1 parent bcd5dd7 commit e6c4569
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
os: [ubuntu-latest, macos-latest, windows-latest]
rust: ["stable", "nightly"]
no_std: [true, false]
debug: [true, false]
Expand Down Expand Up @@ -92,4 +92,4 @@ jobs:
components: miri
override: true
- run: cargo miri setup
- run: cargo miri test --all-features --verbose --color=always
- run: cargo miri test --all-features --verbose
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ ptr_metadata = []
error_in_core = []
allocator_api = []

[dependencies]
sptr = "0.3.2"

[dev-dependencies]
byteorder = "1.4"

Expand Down
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ fn main() {
.stdout;
let version = String::from_utf8_lossy(&output);
if version.contains("nightly") {
println!("cargo:rustc-cfg=NIGHTLY")
println!("cargo:rustc-cfg=nightly")
}
}
2 changes: 1 addition & 1 deletion examples/default_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ fn main() {

// All stored data gets cleaned up once `memory` goes out of scope, or we
// can forget it existed:
memory.forget();
// memory.forget();
}
2 changes: 1 addition & 1 deletion examples/unsafe_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ fn main() {

// All stored data gets cleaned up once `memory` goes out of scope, or we
// can forget it existed:
memory.forget();
// memory.forget();
}
36 changes: 24 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#![allow(incomplete_features)]
#![allow(unstable_name_collisions)]
#![cfg_attr(feature = "no_std", no_std)]
#![cfg_attr(feature = "ptr_metadata", feature(ptr_metadata, unsize))]
#![cfg_attr(feature = "error_in_core", feature(error_in_core))]
#![cfg_attr(feature = "allocator_api", feature(allocator_api))]
#![cfg_attr(all(doc, feature = "NIGHTLY"), feature(doc_auto_cfg))]
#![cfg_attr(all(doc, nightly), feature(doc_auto_cfg))]
#![cfg_attr(nightly, feature(strict_provenance))]
#![cfg_attr(nightly, warn(fuzzy_provenance_casts))]
#![warn(missing_docs)]
#![doc = include_str!("../doc/crate.md")]

Expand Down Expand Up @@ -465,10 +468,17 @@ impl<Impl: ImplDetails<A>, A: ManageMemory> ContiguousMemory<Impl, A> {
/// let mut s: ContiguousMemory = ContiguousMemory::new();
///
/// assert!(s.try_grow_to(1024).is_ok());
/// ```
///
/// The method returns an error if the system can't reserve requested
/// memory:
/// ```should_panic
/// # use contiguous_mem::ContiguousMemory;
/// # let mut s: ContiguousMemory = ContiguousMemory::new();
///
/// let required_size: usize = usize::MAX; // bad read?
/// // can't allocate all addressable memory
/// assert!(s.try_grow_to(required_size).is_err());
/// assert!(s.try_grow_to(required_size).is_ok()); // PANIC!
/// ```
pub fn try_grow_to(&mut self, new_capacity: usize) -> Result<Option<MemoryBase>, MemoryError> {
let mut base = WritableInner::write(&self.inner.base).unwrap();
Expand All @@ -480,10 +490,10 @@ impl<Impl: ImplDetails<A>, A: ManageMemory> ContiguousMemory<Impl, A> {
return Ok(None);
};

let prev_base = *base;
base.address = unsafe { self.inner.alloc.grow(prev_base, new_capacity)? };
let new_addr = unsafe { self.inner.alloc.grow(*base, new_capacity)? };

Ok(if base.address != prev_base.address {
Ok(if new_addr != base.address {
base.address = new_addr;
Some(*base)
} else {
None
Expand Down Expand Up @@ -656,15 +666,17 @@ impl<Impl: ImplDetails<A>, A: ManageMemory> ContiguousMemory<Impl, A> {
///
/// assert!(s.try_reserve_exact(1024).is_ok());
/// assert_eq!(s.capacity(), 1024);
/// ```
///
/// let el_count: usize = 42;
/// let el_size: usize = 288230376151711744; // bad read?
/// The method returns an error if the system can't reserve requested
/// memory:
/// ```should_panic
/// # use contiguous_mem::ContiguousMemory;
/// # let mut s: ContiguousMemory = ContiguousMemory::new();
///
/// let mut required_size: usize = 0;
/// for i in 0..el_count {
/// required_size += el_size;
/// }
/// assert!(s.try_reserve_exact(required_size).is_err());
/// let required_size: usize = usize::MAX; // bad read?
/// // can't allocate all addressable memory
/// assert!(s.try_reserve_exact(required_size).is_ok()); // PANIC!
/// ```
pub fn try_reserve_exact(
&mut self,
Expand Down
4 changes: 2 additions & 2 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,8 @@ pub trait ManageMemory {
unsafe fn grow(&self, base: MemoryBase, new_size: usize) -> Result<BaseAddress, MemoryError>;
}

unsafe fn some_non_null_slice(data: *const u8, len: usize) -> Option<NonNull<[u8]>> {
Some(NonNull::from(core::slice::from_raw_parts(data, len)))
unsafe fn some_non_null_slice(data: *mut u8, len: usize) -> Option<NonNull<[u8]>> {
Some(NonNull::from(core::slice::from_raw_parts_mut(data, len)))
}

/// Default [memory manager](ManageMemory) that uses the methods exposed by
Expand Down
6 changes: 4 additions & 2 deletions src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use core::fmt::Display;

use sptr::Strict;

use crate::raw::BaseAddress;

/// Represents a range of bytes.
Expand Down Expand Up @@ -133,12 +135,12 @@ impl ByteRange {

#[inline]
pub(crate) fn offset_base<T>(&self, addr: BaseAddress) -> Option<*mut T> {
addr.map(|it| (it.as_ptr() as *mut u8 as usize + self.0) as *mut T)
addr.map(|it| (it.as_ptr() as *const u8).map_addr(|addr| addr + self.0) as *mut T)
}

#[inline]
pub(crate) unsafe fn offset_base_unwrap<T>(&self, addr: BaseAddress) -> *mut T {
(unsafe { addr.unwrap_unchecked().as_ptr() } as *mut u8 as usize + self.0) as *mut T
(addr.unwrap_unchecked().as_ptr() as *mut u8).map_addr(|addr| addr + self.0) as *mut T
}
}

Expand Down

0 comments on commit e6c4569

Please sign in to comment.