Skip to content

Commit f3a98cc

Browse files
authored
Merge pull request MicrosoftDocs#954 from corob-msft/cr-C5045-spectre
Add Spectre warning C5045 docs
2 parents 65492b4 + dd21330 commit f3a98cc

File tree

4 files changed

+99
-6
lines changed

4 files changed

+99
-6
lines changed

docs/error-messages/compiler-warnings/TOC.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,4 @@
558558
## [Compiler Warning (level 1) C4997](compiler-warning-level-1-c4997.md)
559559
## [Compiler Warning (level 1) C4999](compiler-warning-level-1-c4999.md)
560560
## [Compiler Warning C5038](c5038.md)
561+
## [Compiler Warning C5045](c5045.md)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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)

docs/error-messages/compiler-warnings/compiler-warnings-c4800-through-c4999.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,4 @@ The articles in this section of the documentation explain a subset of the warnin
146146
|Compiler warning (level 3) C5042|'*declaration*': function declarations at block scope cannot be specified 'inline' in standard C++; remove 'inline' specifier|
147147
|Compiler warning (level 2) C5043|'*specification*': exception specification does not match previous declaration|
148148
|Compiler warning (level 4) C5044|An argument to command-line option *option* points to a path '*path*' that does not exist|
149+
|[Compiler warning C5045](c5045.md)|Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified|

docs/preprocessor/compiler-warnings-that-are-off-by-default.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,14 @@ The following warnings are turned off by default in Visual Studio 2015 and later
142142
|[C5038](../error-messages/compiler-warnings/c5038.md) (level 4)|data member '*member1*' will be initialized after data member '*member2*' <sup>15.3</sup>|
143143
|C5039 (level 4)|'*function*': pointer or reference to potentially throwing function passed to extern C function under -EHc. Undefined behavior may occur if this function throws an exception. <sup>15.5</sup>|
144144
|C5042 (level 3)|'*function*': function declarations at block scope cannot be specified 'inline' in standard C++; remove 'inline' specifier <sup>15.5</sup>|
145-
146-
<sup>14.1</sup> This warning is available starting in Visual Studio 2015 Update 1.<br>
147-
<sup>14.3</sup> This warning is available starting in Visual Studio 2015 Update 3.<br>
148-
<sup>15.3</sup> This warning is available starting in Visual Studio 2017 version 15.3.<br>
149-
<sup>15.5</sup> This warning is available starting in Visual Studio 2017 version 15.5.<br>
150-
<sup>Perm</sup> This warning is off unless the [/permissive-](../build/reference/permissive-standards-conformance.md) compiler option is set.
145+
|[C5045](../error-messages/compiler-warnings/c5045.md)|Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified <sup>15.7</sup>|
146+
147+
<sup>14.1</sup> This warning is available starting in Visual Studio 2015 Update 1.
148+
<sup>14.3</sup> This warning is available starting in Visual Studio 2015 Update 3.
149+
<sup>15.3</sup> This warning is available starting in Visual Studio 2017 version 15.3.
150+
<sup>15.5</sup> This warning is available starting in Visual Studio 2017 version 15.5.
151+
<sup>15.7</sup> This warning is available starting in Visual Studio 2017 version 15.7.
152+
<sup>Perm</sup> This warning is off unless the [/permissive-](../build/reference/permissive-standards-conformance.md) compiler option is set.
151153

152154
## Warnings off by default in earlier versions
153155

0 commit comments

Comments
 (0)