Share via


編譯器警告 C5045

如果指定的 /Qspectre 參數,編譯器將會插入記憶體負載的 Spectre 風險降低

備註

警告 C5045 可讓您查看程式碼中哪些模式會導致指定 /Qspectre 編譯器選項時 插入 Spectre 風險降低,例如 LFENCE。 這可讓您識別哪些程式碼檔案受到安全性問題的影響。 此警告純粹是參考性的:在您使用 /Qspectre 參數重新編譯之前,不會插入風險降低。 C5045 的功能與 /Qspectre 參數無關 ,因此您可以在相同的編譯中使用它們。

此警告是 Visual Studio 2017 15.7 版的新功能,且預設為關閉。 使用 /Wall 啟用預設關閉的所有警告,或 /w n 5045 啟用 C5045 作為層級 n 警告。 在 IDE 中,預設警告層級為 /W3 ,而且可以在 [ 屬性頁 ] 對話方塊中啟用此警告。 開啟 [組態屬性 > C/C++ > 命令列 ],然後在 [ 其他選項 ] 方塊中新增 /w35045 ,然後選擇 [ 確定]。 如需詳細資訊,請參閱 預設 關閉的編譯器警告。 如需如何依編譯器版本停用警告的資訊,請參閱 編譯器版本的 編譯器警告。

範例

下列範例會在使用 /Wall 或 /w35045 和 /W3 選項編譯 Visual Studio 2017 15.7 版時引發警告 C5045

// C5045.cpp
// Compile with: cl /EHsc /W3 /w35045 C5045.cpp

int G, G1, G2;

__forceinline
int * bar(int **p, int i)
{
    return p[i];
}

__forceinline
void bar1(int ** p, int i)
{
    if (i < G1) {
        auto x = p[i]; // C5045: mitigation here
        G = *x;
    }
}

__forceinline
void foo(int * p)
{
    G = *p;
}

void baz(int ** p, int i)
{
    if (i < G1) {
        foo(bar(p, i + G2));
    }
    bar1(p, i);
}

int main() { }

啟用警告時,編譯器輸出看起來會像這樣:

C:\Users\username\source\repos\C5045>cl /W3 /w35045 C5045.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.14.26431 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

C5045.cpp
c:\users\username\source\repos\c5045\c5045.cpp(16) : warning C5045: Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified
c:\users\username\source\repos\c5045\c5045.cpp(15) : note: index 'i' range checked by comparison on this line
c:\users\username\source\repos\c5045\c5045.cpp(17) : note: feeds memory load on this line
Microsoft (R) Incremental Linker Version 14.14.26431.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:C5045.exe
C5045.obj

警告訊息顯示防護功能已插入第 16 行。 另請注意,需要緩和措施,因為第 15 行的索引 i 會饋送第 17 行的記憶體負載。 猜測是在橫條和酒吧1完成的,但風險降低在16線時有效。

另請參閱

理論式執行側邊通道的 C + + 開發人員指導
/Qspectre
spectre