verity_memory::ops::read

Function read_memory

Source
pub unsafe fn read_memory<T: Copy>(
    address: *const T,
) -> Result<T, ReadMemoryError>
Expand description

Reads a value from the specified memory address with the specified type.

§Safety

This function is unsafe because it dereferences a raw pointer, which could lead to undefined behavior if the pointer is invalid.

§Type Parameters

  • T: The type of value to read. It must implement the Copy trait.

§Parameters

  • address: A raw pointer to the memory location from which to read.

§Returns

  • Ok(T): The value read from the specified memory address if successful.
  • Err(ReadMemoryError): Returns an error if the pointer is null, misaligned, or the read operation fails.

§Errors

  • ReadMemoryError::NullPointer: If the provided pointer is null.
  • ReadMemoryError::InvalidAlignment: If the provided pointer is not correctly aligned for the type T.
  • ReadMemoryError::FailedToChangeProtection: If changing the memory protection fails.
  • ReadMemoryError::FailedToRestoreProtection: If restoring the memory protection fails.
  • ReadMemoryError::InvalidAccess: If there is an error during the read operation.

§Example

use verity_memory::ops::read;
let address: *const i32 = 0x12345678 as *const i32;
let result = unsafe { read::read_memory(address) };
match result {
    Ok(value) => println!("Value read: {}", value),
    Err(e) => println!("Failed to read memory: {:?}", e),
}