RtlSetVolatileMemory 函数 (wdm.h)

The RtlSetVolatileMemory function provides RtlFillMemory behavior (for example, setting the contents of a buffer) in situations where the developer needs to be sure that the setting operation occurs (for example, isn't subject to compiler optimizations). 该函数返回指向已填充内存的指针。

Syntax

volatile void * RtlSetVolatileMemory(
  [out] volatile void *Destination,
  [in]  int           Fill,
  [in]  size_t        Length
);

Parameters

[out] Destination

指向要填充的内存块起始地址的指针。

[in] Fill

要填充内存块的字节值。

[in] Length

要填充的内存块的大小(以字节为单位)。 This value must be less than the size of the Destination buffer.

Return value

Returns a pointer to the filled memory block (Destination).

Remarks

  • 函数不能识别为编译器内部函数,因此编译器永远不会优化调用(完全或用等效指令序列替换调用)。 This differs from RtlFillMemory which is subject to various compiler optimizations.

  • 调用返回时,缓冲区已被所需值覆盖。 This function's memory accesses to the Destination will only be performed within the function (for example, the compiler can't move memory accesses out of this function).

  • 如果平台允许它,该函数可能会执行未对齐的内存访问。

  • 该函数在作过程中可能会多次访问内存位置。

此函数适用于所有版本的 Windows,而不仅仅是最新版本。 需要使用最新的 WDK 从 wdm.h 标头获取函数声明。 还需要最新的 WDK 中的库(volatileaccessk.lib)。 但是,生成的驱动程序将在较旧版本的 Windows 上运行。

Example

UCHAR SensitiveData[100];

// Imagine we temporarily store some sensitive cryptographic
// material in a buffer.

StoreCryptographicKey(&SensitiveData);

DoCryptographicOperation(&SensitiveData);

// Now that we are done using the sensitive data we want to
// erase it from the stack. We can use RtlSetVolatileMemory
// to fill the buffer and get a pointer to the filled memory.
// This call will not be optimized away by the compiler.

volatile VOID* clearedBuffer = RtlSetVolatileMemory(&SensitiveData, 0, sizeof(SensitiveData));

Requirements

Requirement Value
最低支持的客户端 See Remarks
Header wdm.h (包括 Wdm.h)
Library volatileaccessk.lib (内核模式), volatileaccessu.lib (用户模式)

See also

RtlFillMemory

RtlFillVolatileMemory