Skip to content

Commit c746b2b

Browse files
committed
Fix MIRI errors
Signed-off-by: Tin Švagelj <tin.svagelj@live.com>
1 parent bcd5dd7 commit c746b2b

8 files changed

Lines changed: 38 additions & 21 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
test:
1212
strategy:
1313
matrix:
14-
os: [ubuntu-latest, windows-latest, macos-latest]
14+
os: [ubuntu-latest, macos-latest, windows-latest]
1515
rust: ["stable", "nightly"]
1616
no_std: [true, false]
1717
debug: [true, false]
@@ -92,4 +92,4 @@ jobs:
9292
components: miri
9393
override: true
9494
- run: cargo miri setup
95-
- run: cargo miri test --all-features --verbose --color=always
95+
- run: cargo miri test --all-features --verbose

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ ptr_metadata = []
3838
error_in_core = []
3939
allocator_api = []
4040

41+
[dependencies]
42+
sptr = "0.3.2"
43+
4144
[dev-dependencies]
4245
byteorder = "1.4"
4346

build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ fn main() {
88
.stdout;
99
let version = String::from_utf8_lossy(&output);
1010
if version.contains("nightly") {
11-
println!("cargo:rustc-cfg=NIGHTLY")
11+
println!("cargo:rustc-cfg=nightly")
1212
}
1313
}

examples/default_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ fn main() {
2020

2121
// All stored data gets cleaned up once `memory` goes out of scope, or we
2222
// can forget it existed:
23-
memory.forget();
23+
// memory.forget();
2424
}

examples/unsafe_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ fn main() {
2626

2727
// All stored data gets cleaned up once `memory` goes out of scope, or we
2828
// can forget it existed:
29-
memory.forget();
29+
// memory.forget();
3030
}

src/lib.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#![allow(incomplete_features)]
2+
#![allow(unstable_name_collisions)]
23
#![cfg_attr(feature = "no_std", no_std)]
34
#![cfg_attr(feature = "ptr_metadata", feature(ptr_metadata, unsize))]
45
#![cfg_attr(feature = "error_in_core", feature(error_in_core))]
56
#![cfg_attr(feature = "allocator_api", feature(allocator_api))]
6-
#![cfg_attr(all(doc, feature = "NIGHTLY"), feature(doc_auto_cfg))]
7+
#![cfg_attr(all(doc, nightly), feature(doc_auto_cfg))]
8+
#![cfg_attr(nightly, feature(strict_provenance))]
9+
#![cfg_attr(nightly, warn(fuzzy_provenance_casts))]
710
#![warn(missing_docs)]
811
#![doc = include_str!("../doc/crate.md")]
912

@@ -465,10 +468,17 @@ impl<Impl: ImplDetails<A>, A: ManageMemory> ContiguousMemory<Impl, A> {
465468
/// let mut s: ContiguousMemory = ContiguousMemory::new();
466469
///
467470
/// assert!(s.try_grow_to(1024).is_ok());
471+
/// ```
472+
///
473+
/// The method returns an error if the system can't reserve requested
474+
/// memory:
475+
/// ```should_panic
476+
/// # use contiguous_mem::ContiguousMemory;
477+
/// # let mut s: ContiguousMemory = ContiguousMemory::new();
468478
///
469479
/// let required_size: usize = usize::MAX; // bad read?
470480
/// // can't allocate all addressable memory
471-
/// assert!(s.try_grow_to(required_size).is_err());
481+
/// assert!(s.try_grow_to(required_size).is_ok()); // PANIC!
472482
/// ```
473483
pub fn try_grow_to(&mut self, new_capacity: usize) -> Result<Option<MemoryBase>, MemoryError> {
474484
let mut base = WritableInner::write(&self.inner.base).unwrap();
@@ -480,10 +490,10 @@ impl<Impl: ImplDetails<A>, A: ManageMemory> ContiguousMemory<Impl, A> {
480490
return Ok(None);
481491
};
482492

483-
let prev_base = *base;
484-
base.address = unsafe { self.inner.alloc.grow(prev_base, new_capacity)? };
493+
let new_addr = unsafe { self.inner.alloc.grow(*base, new_capacity)? };
485494

486-
Ok(if base.address != prev_base.address {
495+
Ok(if new_addr != base.address {
496+
base.address = new_addr;
487497
Some(*base)
488498
} else {
489499
None
@@ -656,15 +666,17 @@ impl<Impl: ImplDetails<A>, A: ManageMemory> ContiguousMemory<Impl, A> {
656666
///
657667
/// assert!(s.try_reserve_exact(1024).is_ok());
658668
/// assert_eq!(s.capacity(), 1024);
669+
/// ```
659670
///
660-
/// let el_count: usize = 42;
661-
/// let el_size: usize = 288230376151711744; // bad read?
671+
/// The method returns an error if the system can't reserve requested
672+
/// memory:
673+
/// ```should_panic
674+
/// # use contiguous_mem::ContiguousMemory;
675+
/// # let mut s: ContiguousMemory = ContiguousMemory::new();
662676
///
663-
/// let mut required_size: usize = 0;
664-
/// for i in 0..el_count {
665-
/// required_size += el_size;
666-
/// }
667-
/// assert!(s.try_reserve_exact(required_size).is_err());
677+
/// let required_size: usize = usize::MAX; // bad read?
678+
/// // can't allocate all addressable memory
679+
/// assert!(s.try_reserve_exact(required_size).is_ok()); // PANIC!
668680
/// ```
669681
pub fn try_reserve_exact(
670682
&mut self,

src/memory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,8 @@ pub trait ManageMemory {
462462
unsafe fn grow(&self, base: MemoryBase, new_size: usize) -> Result<BaseAddress, MemoryError>;
463463
}
464464

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

469469
/// Default [memory manager](ManageMemory) that uses the methods exposed by

src/range.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,14 @@ impl ByteRange {
133133

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

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

0 commit comments

Comments
 (0)