CopyFromModeAligned - NtDoc

Native API online documentation, based on the System Informer (formerly Process Hacker) phnt headers
// usermode_accessors.h

#define CopyFromModeAligned(Destination, Source, Length, Mode, Alignment)                           \
            do {                                                                                    \
                if ((Mode) != KernelMode) {                                                         \
                    ExProbeAlignment((Source), (Length), (Alignment));                              \
                }                                                                                   \
                CopyFromMode((Destination), (Source), (Length), (Mode));                            \
            } while (0)
View the official Windows Driver Kit DDI reference

NtDoc

No description available.

Windows Driver Kit DDI reference (nf-usermode_accessors-copyfrommodealigned)

Description

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

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.

Syntax

#define CopyFromModeAligned(Destination, Source, Length, Mode, Alignment)                           \
            do {                                                                                    \
                if ((Mode) != KernelMode) {                                                         \
                    ExProbeAlignment((Source), (Length), (Alignment));                              \
                }                                                                                   \
                CopyFromMode((Destination), (Source), (Length), (Mode));                            \
            } while (0)

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 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.

If you are copying from a fixed-sized structure, you should use ReadStructFromModeAligned instead to avoid the risk of passing the wrong size.

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.

See also

CopyFromMode

CopyFromUser

CopyFromUserAligned

CopyFromModeNonTemporal

CopyToMode