-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbbbbr0-poisoned-values.html
302 lines (247 loc) · 7.44 KB
/
dbbbbr0-poisoned-values.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!doctype html public "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<head>
<title>Poisoned values</title>
<style type="text/css">
pre {
display: inline;
}
table#header th,
table#header td
{
text-align: left;
}
table#references th,
table#references td
{
vertical-align: top;
}
ins, ins * { text-decoration:none; font-weight:bold; background-color:#A0FFA0 }
del, del * { text-decoration:line-through; background-color:#FFA0A0 }
#hidedel:checked ~ * del, #hidedel:checked ~ * del * { display:none; visibility:hidden }
blockquote
{
color: #000000;
background-color: #F1F1F1;
border: 1px solid #D1D1D1;
padding-left: 0.5em;
padding-right: 0.5em;
}
blockquote.stdins
{
text-decoration: underline;
color: #000000;
background-color: #C8FFC8;
border: 1px solid #B3EBB3;
padding: 0.5em;
}
blockquote.stddel
{
text-decoration: line-through;
color: #000000;
background-color: #FFEBFF;
border: 1px solid #ECD7EC;
padding-left: 0.5empadding-right: 0.5em;
}
blockquote.quote
{
margin-top: 0em;
margin-left: 0em;
border-style: solid;
background-color: lemonchiffon;
color: #000000;
border: 1px solid black;
}
div.compare {
padding-left: 40px;
display: table; /* undo float:left effect */
}
div.compare_item {
float: left;
margin: 2px;
}
</style>
</head>
<body>
<table id="header">
<tr>
<th>Document Number</th>
<td>DXXXXR0 <em>Draft</em></td>
</tr>
<tr>
<th>Date:</th>
<td>2021-06-13</td>
</tr>
<tr>
<th>Audience:</th>
<td>SG12<br/>
SSRG<br/>
Evolution Working Group</td>
</tr>
<tr>
<th>Reply-to:</th>
<td>Tom Honermann <tom@honermann.net></td>
</tr>
</table>
<p>
<strong>Abstract:</strong>
Use of uninitialized variables or data members is a well known source of
undefined behavior (UB) in C++.
Tools like
<a title="MemorySanitizer"
href="https://clang.llvm.org/docs/MemorySanitizer.html">MemorySanitizer</a>
<sup><a title="MemorySanitizer"
href="#ref_msan">[MSAN]</a></sup>
rely on UB to detect use of uninitialized memory and provide a diagnostic
(tools are free to assume that UB is never intentional).
Programmers that use these tools may resist adding initializers when a default
value is not desirable since doing so interferes with the ability to diagnose
an unintended use before an appropriate value has been determined and assigned.
Compilers and static analysis tools diagnose uninitialized variables and data
members as potential sources of UB; these diagnostics are undesriable when
omission of an initializer is intentional.
Failure to identify and correct use of uninitialized memory results in a
program that exhibits UB at run-time.
The observed behavior may not be consistent if the uninitialized memory contents
differ from one execution of the program to another; this complicates debugging.
Enclosed is a proposal to allow a programmer to provide a "poisoned" or
"tainted" value in an initializer or expression so as to avoid UB without
compromising the ability of tools to diagnose use of such values.
</p>
<h1>
Poisoned values
</h1>
<ul>
<li><a href="#introduction">
Introduction</a></li>
<li><a href="#motivation">
Motivation</a></li>
<li><a href="#design_options">
Design Options</a></li>
<li><a href="#proposal">
Proposal</a></li>
<li><a href="#implementation_exp">
Implementation Experience</a></li>
<li><a href="#acknowledgements">
Acknowledgements</a></li>
<li><a href="#references">
References</a></li>
<li><a href="#wording">
Formal Wording</a></li>
</ul>
<h1 id="introduction">Introduction</h1>
<p>
Original proposal message:
<a href="https://lists.isocpp.org/std-proposals/2021/06/2682.php">https://lists.isocpp.org/std-proposals/2021/06/2682.php</a>
</p>
<h1 id="motivation">Motivation</h1>
<p>
Discuss LLVM and Coverity.
<a href="https://lists.llvm.org/pipermail/llvm-dev/2021-June/151036.html">https://lists.llvm.org/pipermail/llvm-dev/2021-June/151036.html</a>
Apparently my replies to the mailing list don't preserve quoting properly :(
</p>
<h1 id="design_options">Design Options</h1>
<p>
<ul>
<li>A keyword?</li>
<li>A magic library function or intrinsic?</li>
<li>An attribute? Would need to be able to annotate expressions.</li>
</ul>
</p>
<p>
Should support for other kinds of tainted values be supported?
For example, the ability to mark the buffer filled by a call to
<tt>fread()</tt>?
If so, support is needed to enable inspection of tainted values by a sanitizer
and to declare the values no longer tainted.
</p>
<p>
Example:
<blockquote><pre style="display:inline;">
struct S {
S() {}
S(int v) : dm(v) {}
// dm may only be used if a value was provided during construction.
int dm = POISON(-1);
};
int f(bool b) {
S s = b ? S() : S(42);
// The following access of s.dm violates preconditions if b is true.
// Would like MemorySanitizer to diagnose as an access of an uninitialized
// data member. But if MemorySanitizer is not enabled, then would like to
// reliably return -1.
return s.dm;
}
</pre></blockquote>
</p>
<p>
It may be useful to annotate a portion of an initializer:
<blockquote><pre style="display:inline;">
struct aggregate {
const char *desc;
int value;
};
aggregate a = { "the thing", POISON(-1) };
</pre></blockquote>
</p>
<p>
Likewise, applying poison outside of initialization context could be useful:
<blockquote><pre style="display:inline;">
int *p = new int;
...
delete p;
p = POISON(nullptr);
</pre></blockquote>
</p>
<p>
Poison must be propagated, but copying it must not be considered a defect.
According to
<a href="https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwis_vv2wZLxAhU2ElkFHVApDj0QFjAAegQIAxAD&url=https%3A%2F%2Fresearch.google.com%2Fpubs%2Farchive%2F43308.pdf&usg=AOvVaw1ptDBQhYiiv4wb7SFV3s8R">this paper</a>
, this is consistent with how MemorySanitizer
works.
When copying a poisoned value, it propagates the poison.
<blockquote><pre style="display:inline;">
struct S {
S() {}
S(int v) : dm(v) {}
// dm may only be used if a value was provided during construction.
int dm = POISON(-1);
};
</pre></blockquote>
</p>
<h1 id="proposal">Proposal</h1>
<h1 id="implementation_exp">Implementation Experience</h1>
<h1 id="acknowledgements">Acknowledgements</h1>
<p>
Thank you to Ville Voutilainen for initial proposal feedback.
</p>
<h1 id="references">References</h1>
<table id="references">
<tr>
<td id="ref_n4885"><sup>[N4885]</sup></td>
<td>
"Working Draft, Standard for Programming Language C++", N4885, 2021.<br/>
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/n4885.pdf">
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/n4885.pdf</a></td>
</tr>
<tr>
<td id="ref_msan"><sup>[MSAN]</sup></td>
<td>
The Clang Team,
"MemorySanitizer", 2021.<br/>
<a href="https://clang.llvm.org/docs/MemorySanitizer.html">
https://clang.llvm.org/docs/MemorySanitizer.html</a></td>
</tr>
</table>
<h1 id="wording">Formal Wording</h1>
<input type="checkbox" id="hidedel">Hide deleted text</input>
<p>
These changes are relative to
<a title="Working Draft, Standard for Programming Language C++"
href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/n4885.pdf">
N4885</a>
<sup><a title="Working Draft, Standard for Programming Language C++"
href="#ref_n4885">
[N4885]</a></sup>
</p>
</body>