verity_memory::ops::write

Function write_memory

Source
pub unsafe fn write_memory<T: Copy>(
    dest_ptr: *mut T,
    value: T,
) -> Result<(), WriteMemoryError>
Expand description

Writes a value of type T to the specified memory location.

§Safety

This function is unsafe because it directly manipulates raw pointers, which can cause undefined behavior if the pointer is invalid or points to memory that is not writable.

§Parameters

  • dest_ptr: A mutable pointer to the destination memory where the value will be written.
  • value: The value to write at the destination memory.

§Returns

  • Ok(()) if the value was successfully written to memory.
  • Err(WriteMemoryError) if an error occurred, such as a null pointer or invalid alignment.

§Errors

  • WriteMemoryError::NullPointer if dest_ptr is null.
  • WriteMemoryError::InvalidAlignment if dest_ptr is not correctly aligned.
  • WriteMemoryError::FailedToChangeProtection if memory protection could not be modified.
  • WriteMemoryError::FailedToRestoreProtection if memory protection could not be restored.

§Example

use verity_memory::ops::write;
unsafe {
    let mut value: i32 = 42;
    let result = write::write_memory(&mut value as *mut i32, 100);
    assert!(result.is_ok());
    assert_eq!(value, 100);
}