|
| 1 | +--- |
| 2 | +title: "Compiler Warning C5045 | Microsoft Docs" |
| 3 | +ms.custom: "" |
| 4 | +ms.date: "06/21/2018" |
| 5 | +ms.technology: ["cpp-diagnostics"] |
| 6 | +ms.topic: "error-reference" |
| 7 | +dev_langs: ["C++"] |
| 8 | +helpviewer_keywords: ["C5045"] |
| 9 | +author: "corob-msft" |
| 10 | +ms.author: "corob" |
| 11 | +ms.workload: ["cplusplus"] |
| 12 | +--- |
| 13 | +# Compiler Warning C5045 |
| 14 | + |
| 15 | +> Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified |
| 16 | +
|
| 17 | +## Remarks |
| 18 | + |
| 19 | +Warning C5045 lets you see what patterns in your code cause a Spectre mitigation, such as an LFENCE, to be inserted when the [/Qspectre](../../build/reference/qspectre.md) 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. |
| 20 | + |
| 21 | +This warning is new in Visual Studio 2017 version 15.7, and is off by default. Use [/Wall](../../build/reference/compiler-option-warning-level.md) to enable all warnings that are off by default, or __/w__*n*__5038__ 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](../../preprocessor/compiler-warnings-that-are-off-by-default.md). For information on how to disable warnings by compiler version, see [Compiler warnings by compiler version](compiler-warnings-by-compiler-version.md). |
| 22 | + |
| 23 | +## Example |
| 24 | + |
| 25 | +The following example raises warning C5045 when compiled by Visual Studio 2017 version 15.7 with the **/Wall** or the **/w35045** and **/W3** options: |
| 26 | + |
| 27 | +```cpp |
| 28 | +// C5045.cpp |
| 29 | +// Compile with: cl /EHsc /W3 /w35045 C5045.cpp |
| 30 | + |
| 31 | +int G, G1, G2; |
| 32 | + |
| 33 | +__forceinline |
| 34 | +int * bar(int **p, int i) |
| 35 | +{ |
| 36 | + return p[i]; |
| 37 | +} |
| 38 | + |
| 39 | +__forceinline |
| 40 | +void bar1(int ** p, int i) |
| 41 | +{ |
| 42 | + if (i < G1) { |
| 43 | + auto x = p[i]; // C5045: mitigation here |
| 44 | + G = *x; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +__forceinline |
| 49 | +void foo(int * p) |
| 50 | +{ |
| 51 | + G = *p; |
| 52 | +} |
| 53 | + |
| 54 | +void baz(int ** p, int i) |
| 55 | +{ |
| 56 | + if (i < G1) { |
| 57 | + foo(bar(p, i + G2)); |
| 58 | + } |
| 59 | + bar1(p, i); |
| 60 | +} |
| 61 | + |
| 62 | +int main() { } |
| 63 | +``` |
| 64 | +
|
| 65 | +The compiler output when the warning is enabled looks something like this: |
| 66 | +
|
| 67 | +```Output |
| 68 | +C:\Users\username\source\repos\C5045>cl /W3 /w35045 C5045.cpp |
| 69 | +Microsoft (R) C/C++ Optimizing Compiler Version 19.14.26431 for x86 |
| 70 | +Copyright (C) Microsoft Corporation. All rights reserved. |
| 71 | +
|
| 72 | +C5045.cpp |
| 73 | +c:\users\username\source\repos\c5045\c5045.cpp(16) : warning C5045: Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified |
| 74 | +c:\users\username\source\repos\c5045\c5045.cpp(15) : note: index 'i' range checked by comparison on this line |
| 75 | +c:\users\username\source\repos\c5045\c5045.cpp(17) : note: feeds memory load on this line |
| 76 | +Microsoft (R) Incremental Linker Version 14.14.26431.0 |
| 77 | +Copyright (C) Microsoft Corporation. All rights reserved. |
| 78 | +
|
| 79 | +/out:C5045.exe |
| 80 | +C5045.obj |
| 81 | +``` |
| 82 | + |
| 83 | +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. |
| 84 | + |
| 85 | +## See also |
| 86 | + |
| 87 | +[C++ Developer Guidance for Speculative Execution Side Channels](../../security/developer-guidance-speculative-execution.md) |
| 88 | +[/Qspectre](../../build/reference/qspectre.md) |
| 89 | +[spectre](../../cpp/spectre.md) |
0 commit comments