nightly only.Expand description
Memory protection utilities
Provides access to the memory locking system calls, such as mlock() and
mprotect() on UNIX-like systems, VirtualLock() and VirtualProtect() on
Windows. Similar to libsodium’s sodium_mlock and sodium_mprotect_*
functions.
On Linux, sets MADV_DONTDUMP with madvise() on locked regions.
The protected memory features leverage Rust’s Allocator API, which
requires nightly Rust. This crate must be built with the nightly feature
flag enabled to activate these features.
For details on the Allocator API, see:
https://github.com/rust-lang/rust/issues/32838
If the serde feature is enabled, the serde::Deserialize and
serde::Serialize traits will be implemented for HeapBytes and
HeapByteArray.
Example
use dryoc::protected::*;
// Create a read-only, locked region of memory
let readonly_locked = HeapBytes::from_slice_into_readonly_locked(b"some locked bytes")
.expect("failed to get locked bytes");
// ... now do stuff with `readonly_locked` ...
println!("{:?}", readonly_locked.as_slice());Protection features
The type safe API uses traits to guard against misuse of protected memory.
For example, memory that is set as read-only can be accessed with immutable
accessors (such as .as_slice() or .as_array()), but not with mutable
accessors like .as_mut_slice() or .as_mut_array().
use dryoc::protected::*;
// Create a read-only, locked region of memory
let readonly_locked = HeapBytes::from_slice_into_readonly_locked(b"some locked bytes")
.expect("failed to get locked bytes");
// Try to access the memory mutably
println!("{:?}", readonly_locked.as_mut_slice()); // fails to compile, cannot access mutablyMemory that has been protected as read-only or no-access will cause the
process to crash if you attempt to access the memory improperly. To test
this, try the following code (which requires an unsafe block):
use dryoc::protected::*;
// Create a read-only, locked region of memory
let readonly_locked = HeapBytes::from_slice_into_readonly_locked(b"some locked bytes")
.expect("failed to get locked bytes");
// Write to a protected region of memory, causing a crash.
unsafe {
std::ptr::write(readonly_locked.as_slice().as_ptr() as *mut u8, 0) // <- crash happens here
};Running the code above produces as signal: 10, SIGBUS: access to undefined memory panic.
Re-exports
Modules
Structs
Vec with custom Allocator
implementation.Vec with custom Allocator
implementation.