Redigera

Dela via


Compiler Warning C5045

Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified

Remarks

Warning C5045 lets you see what patterns in your code cause a Spectre mitigation, such as an LFENCE, to be inserted when the /Qspectre compiler option is specified. This lets you identify which code files are affected by the security issue. This warning is purely informational: the mitigation is not inserted until you recompile using the /Qspectre switch. The functionality of C5045 is independent of the /Qspectre switch, so you can use them both in the same compilation.

This warning is new in Visual Studio 2017 version 15.7, and is off by default. Use /Wall to enable all warnings that are off by default, or /wn5045 to enable C5045 as a level n warning. In the IDE, the default warning level is /W3 and this warning can be enabled in the project Property Pages dialog. Open Configuration Properties > C/C++ > Command Line and in the Additional options box, add /w35045, then choose OK. For more information, see Compiler warnings that are off by default. For information on how to disable warnings by compiler version, see Compiler warnings by compiler version.

Example

The following example raises warning C5045 when compiled by Visual Studio 2017 version 15.7 with the /Wall or the /w35045 and /W3 options:

// 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() { }

The compiler output when the warning is enabled looks something like this:

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

The warning messages show that a mitigation would have been inserted on line 16. It also notes that the mitigation is needed because the index i on line 15 feeds the memory load on line 17. The speculation is done across bar and bar1 but the mitigation is effective when placed at line 16.

See also

C++ Developer Guidance for Speculative Execution Side Channels
/Qspectre
spectre