Skip to content

add test for #1909 #2610

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

Merged
merged 1 commit into from
Oct 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions tests/pass/issues/issue-miri-1909.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//@compile-flags: -Zmiri-permissive-provenance
#![deny(unsafe_op_in_unsafe_fn)]
//! This does some tricky ptr-int-casting.

use core::alloc::{GlobalAlloc, Layout};
use std::alloc::System;

/// # Safety
/// `ptr` must be valid for writes of `len` bytes
unsafe fn volatile_write_zeroize_mem(ptr: *mut u8, len: usize) {
for i in 0..len {
// ptr as usize + i can't overlow because `ptr` is valid for writes of `len`
let ptr_new: *mut u8 = ((ptr as usize) + i) as *mut u8;
// SAFETY: `ptr` is valid for writes of `len` bytes, so `ptr_new` is valid for a
// byte write
unsafe {
core::ptr::write_volatile(ptr_new, 0u8);
}
}
}

pub struct ZeroizeAlloc;

unsafe impl GlobalAlloc for ZeroizeAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
// SAFETY: uphold by caller
unsafe { System.alloc(layout) }
}

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
// securely wipe the deallocated memory
// SAFETY: `ptr` is valid for writes of `layout.size()` bytes since it was
// previously successfully allocated (by the safety assumption on this function)
// and not yet deallocated
unsafe {
volatile_write_zeroize_mem(ptr, layout.size());
}
// SAFETY: uphold by caller
unsafe { System.dealloc(ptr, layout) }
}

unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
// SAFETY: uphold by caller
unsafe { System.alloc_zeroed(layout) }
}
}

#[global_allocator]
static GLOBAL: ZeroizeAlloc = ZeroizeAlloc;

fn main() {
let layout = Layout::new::<[u8; 16]>();
let ptr = unsafe { std::alloc::alloc_zeroed(layout) };
unsafe {
std::alloc::dealloc(ptr, layout);
}
}