Edit

Share via


CopyFromModeAligned macro (usermode_accessors.h)

The CopyFromModeAligned macro safely copies data from specified-mode memory to kernel memory, with alignment checking.

Syntax

void CopyFromModeAligned(
   Destination,
   Source,
   Length,
   Mode,
   Alignment
);

Parameters

Destination

[out] A pointer to the kernel memory location where the data will be copied.

Source

[in] A pointer to the memory location from which to copy the data.

Length

[in] The number of bytes to copy.

Mode

[in] The processor mode that determines how the memory access is performed. Mode can be one of the following values.

Value Meaning
KernelMode Source points to kernel-mode memory. The macro performs a direct memory copy with memory_order_relaxed semantics.
UserMode Source points to user-mode memory. The macro raises an exception if Source doesn't point to user-mode memory; otherwise it performs a copy from the specified address with memory_order_relaxed semantics.

Alignment

[in] The alignment boundary that the source pointer must satisfy.

Return value

None

Remarks

This macro provides a safe way to copy data from either kernel or user-mode memory to kernel memory, with the copy mechanism determined by the specified processor mode and alignment verification. This allows for flexible memory operations that can adapt to different execution contexts while ensuring proper alignment requirements.

When Mode is KernelMode:

  • The macro performs a volatile copy using memory_order_relaxed semantics.

  • The macro isn't recognized as a compiler intrinsic so the compiler will never optimize away the call (either entirely or replace the call with an equivalent sequence of instructions).

  • When the call returns, the data has been copied from Source to Destination. This macro's memory accesses to the Source and Destination will only be performed within the function (for example, the compiler can't move memory accesses out of this function).

  • The macro might access memory locations more than once as part of its copy operation.

  • The macro doesn't support copy operations when Source and Destination overlap each other.

The macro raises a structured exception if the copy operation fails, such as when the source address is not valid for the specified mode, is not properly aligned according to the Alignment parameter, or is inaccessible.

This macro will never be optimized away by the compiler, nor will the compiler create additional accesses to this memory location before the macro is called or after the macro returns (unless the source code explicitly performs these accesses).

This macro works on all versions of Windows, not just the latest. You need to consume the latest WDK to get the function declaration from the usermode_accessors.h header. You also need the library (umaccess.lib) from the latest WDK. However, the resulting driver will run fine on older versions of Windows.

Requirements

Requirement Value
Minimum supported client See Remarks
Header usermode_accessors.h
Library umaccess.lib
IRQL Less than or equal to APC_LEVEL

See also

CopyFromMode

CopyFromUser

CopyFromUserAligned

CopyFromModeNonTemporal

CopyToMode