FillVolatileMemory 函式

FillVolatileMemory 函式會以指定的填滿值填滿記憶體區塊。

重要

某些資訊與發行前版本產品有關,在發行前版本產品可能經過大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。

參數

Param 目的地 [out]

要填滿之內存區塊起始位址的指標。

Param Length [in]

要填入的記憶體區塊大小,以位元組為單位。 此值必須小於目的地緩衝區的大小

Param Fill [in]

要填滿記憶體區塊的位元組值。

語法

volatile void*
  FillVolatileMemory (
    _Out_writes_bytes_all_(Length) volatile void* Destination,
    SIZE_T Length,
    INT Fill
  );

備註

此 API 存在以提供 FillMemory 行為(亦即,設定緩衝區的內容)的情況是開發人員必須確定設定作業發生(亦即不受編譯程式優化的約束)。 API 具有下列屬性:

  • API 無法辨識為編譯程式內部函數,因此編譯程式永遠不會優化呼叫(完全或以「對等」指令序列取代呼叫)。 這與 FillMemory 不同,這受限於各種編譯程序優化。
  • 當呼叫傳回時,已以所需的值覆寫緩衝區。 此函式記憶體存取 目的地 只會在函式內執行(亦即編譯程式無法將記憶體存取移出此函式)。
  • 如果平台允許,API 可能會執行未對齊的記憶體存取。
  • API 可能會在其作業中多次存取記憶體位置。

注意

此函式適用於所有版本的 Windows,而不只是最新版本。 您必須取用最新的 SDK,才能從 winbase.h 標頭取得函式宣告。 您也需要來自最新 SDK 的連結庫 (volatileaccessu.lib)。 不過,產生的二進位檔將會在舊版 Windows 上正常執行。

範例

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 cannot call FillMemory because
// if the compiler realizes that "SensitiveData" is not
// referenced again the compiler can remove the call to FillMemory.
// Instead we can call SecureZeroMemory2, ZeroVolatileMemory, or FillVolatileMemory
// (the former two are convenience wrappers around the latter). These
// calls will not be optimized away by the compiler.
// Note that SecureZeroMemory2 performs better than the old
// SecureZeroMemory API.

FillVolatileMemory(&SensitiveData, sizeof(SensitiveData), 0);

需求

最低支援的用戶端: Windows 11 Insider Preview 組建 TBD

標頭: winbase.h (包括 Winbase.h)

核心模式連結庫: volatileaccessk.lib

使用者模式連結庫: volatileaccessu.lib

另請參閱