-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2852r0.html
546 lines (481 loc) · 18.1 KB
/
p2852r0.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
<!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>
Contract violation handling semantics for the contracts MVP
</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;
}
#hideins:checked ~ * ins, #hideins:checked ~ * ins * { display:none; visibility:hidden }
#hidedel:checked ~ * del, #hidedel:checked ~ * del * { display:none; visibility:hidden }
ins, ins *
{
text-decoration: underline;
color: #000000;
background-color:#C8FFC8
}
del, del *
{
text-decoration: line-through;
color: #000000;
background-color:#FFA0A0
}
nop, nop *
{
color: #000000;
background-color:#B0B0FF
}
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: #FFA0A0;
border: 1px solid #ECD7EC;
padding-left: 0.5empadding-right: 0.5em;
}
blockquote.stdnop
{
color: #000000;
background-color: #B0B0FF;
border: 1px solid #ECD7EC;
padding-left: 0.5empadding-right: 0.5em;
}
</style>
</head>
<body style="max-width: 8.5in">
<table id="header">
<tr>
<th>Document Number:</th>
<td>P2852R0</td>
</tr>
<tr>
<th>Date:</th>
<td>2023-04-24</td>
</tr>
<tr>
<th>Audience:</th>
<td>SG21</td>
</tr>
<tr>
<th>Reply-to:</th>
<td>Tom Honermann <[email protected]></td>
</tr>
</table>
<h1>Contract violation handling semantics for the contracts MVP</h1>
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#proposal">Proposal</a></li>
<li><a href="#design">Design considerations</a>
<ul>
<li><a href="#design-pred-uneval">Predicate is unevaluated</a></li>
<li><a href="#design-pred-true">Predicate evaluation yields a true result</a></li>
<li><a href="#design-pred-false">Predicate evaluation yields a false result</a></li>
<li><a href="#design-pred-ub">Predicate evaluation encounters undefined-behavior</a></li>
<li><a href="#design-exceptions">Predicate evaluation or violation handler throws</a></li>
<li><a href="#design-longjmp">Predicate evaluation or violation handler calls <tt>std::longjmp</tt></a></li>
</ul>
</li>
<li><a href="#implementation-exp">Implementation experience</a></li>
<li><a href="#ack">Acknowledgements</a></li>
<li><a href="#references">References</a></li>
<li><a href="#wording">Wording</a></li>
</ul>
<h1 id="introduction">Introduction</h1>
<p>
SG21 endeavors to specify a minimal contracts design that is both simple enough
to be confidently adopted for C++26 and sufficiently extensible to satisfy both
known and unknown future needs.
</p>
<p>
Recent SG21 discussions have been focussed on the semantics to ascribe to
behaviors that may occur during evaluation of contract predicates.
For details of these discussions, see the minutes and papers linked below for
recent SG21 telecons.
<ul>
<li><a href="https://wiki.edg.com/bin/view/Wg21telecons2023/Teleconference2023-03-09">2023-03-09 SG21 telecon</a>
<ul>
<li><a title="Unconditional termination is a serious problem"
href="https://wg21.link/p2698r0">P2698R0: Unconditional termination is a serious problem</a>
<sup><a title="Unconditional termination is a serious problem"
href="#ref_p2698r0">[P2698R0]</a></sup>
</li>
<li><a title="Not halting the program after detected contract violation"
href="https://wg21.link/p2784r0">P2784R0: Not halting the program after detected contract violation</a>
<sup><a title="Not halting the program after detected contract violation"
href="#ref_p2784r0">[P2784R0]</a></sup>
</li>
</ul>
</li>
<li><a href="https://wiki.edg.com/bin/view/Wg21telecons2023/Teleconference2023-03-23">2023-03-23 SG21 telecon</a>
<ul>
<li><a title="The idea behind the contracts MVP"
href="https://wg21.link/p2817r0">P2817R0: The idea behind the contracts MVP</a>
<sup><a title="The idea behind the contracts MVP"
href="#ref_p2817r0">[P2817R0]</a></sup>
</li>
</ul>
</li>
<li><a href="https://wiki.edg.com/bin/view/Wg21telecons2023/Teleconference2023-04-20">2023-04-20 SG21 telecon</a>
<ul>
<li><a title="Unconditional contract violation handling of any kind is a serious problem"
href="https://wg21.link/p2838r0">P2838R0: Unconditional contract violation handling of any kind is a serious problem</a>
<sup><a title="Unconditional contract violation handling of any kind is a serious problem"
href="#ref_p2838r0">[P2838R0]</a></sup>
</li>
<li><a title="Contract-Violation Handlers"
href="https://wg21.link/p2811r1">P2811R1: Contract-Violation Handlers</a>
<sup><a title="Contract-Violation Handlers"
href="#ref_p2811r1">[P2811R1]</a></sup>
</li>
</ul>
</li>
</ul>
</p>
<p>
SG21 has not yet established consensus regarding:
<ol>
<li>which behaviors that occur during predicate evaluation are
well-defined.</li>
<li>which behaviors that occur during execution of a violation handler are
well-defined.</li>
</ol>
For the behaviors enumerated in
<a title="Contract-Violation Handlers"
href="https://wg21.link/p2811r1">P2811R1</a><sup><a title="Contract-Violation Handlers"
href="#ref_p2811r1">[P2811R1]</a></sup>,
this paper proposes which ones to specify with well-defined semantics for the
MVP, and which ones to be relegated to unspecified or undefined behavior pending
further consideration.
</p>
<p>
It is not the intent of this paper to propose these semantics as the final set
for C++26. The intent is rather to specify well-defined semantics for those
cases for which strong consensus is established and to leave the rest to be
pursued in future papers. In the meantime, the use of unspecified behavior
leaves implementations free to experiment with semantics in the cases that are
not well-defined.
</p>
<p>
This paper does not discuss how violation handlers are defined or registered, or
the mechanics of their invocations; those concerns are covered by
<a title="Contract-Violation Handlers"
href="https://wg21.link/p2811r1">P2811R1</a><sup><a title="Contract-Violation Handlers"
href="#ref_p2811r1">[P2811R1]</a></sup>.
Likewise, this paper does not discuss build modes, the details of contract
checking enablement, optimization based on contract predicates or consequences
thereof, or one-definition rule (ODR) concerns regarding mixed contract
checking enablement across the translation units that comprise a program;
those concerns are also left to other papers.
</p>
<h1 id="proposal">Proposal</h1>
<p>
1. Adopt the contract violation handler specified in
<a title="Contract-Violation Handlers"
href="https://wg21.link/p2811r1">P2811R1</a><sup><a title="Contract-Violation Handlers"
href="#ref_p2811r1">[P2811R1]</a></sup>
with or without minor adjustments.
</p>
<p>
2. The implementation provided default contract violation handler is specified
to unconditionally call <tt>std::abort()</tt> after performing any
implementation-defined actions such as generating diagnostics.
</p>
<p>
3. A user-defined contract violation handler may call the implementation
provided default contract violation handler.
This ability enables a user-provided replacement handler to perform some
actions and then defer to the implementation provided handler for
implementation-specific generation of diagnostics.
The mechanism by which this can be done is TBD since
<a title="Contract-Violation Handlers"
href="https://wg21.link/p2811r1">P2811R1</a><sup><a title="Contract-Violation Handlers"
href="#ref_p2811r1">[P2811R1]</a></sup>
does not expose the implementation provided handler when a user-provided handler
is present.
</p>
<p>
4. The semantics of predicate evaluation and contract violation handler
invocation are specified as follows.
<table border="1">
<tr>
<th>Behavior</th>
<th>Specified semantics</th>
</tr>
<tr>
<td>Predicate evaluation is not performed.</td>
<td><b>well-defined</b>; execution continues.</td>
</tr>
<tr>
<td>Predicate evaluation yields a true result.</td>
<td><b>well-defined</b>; execution continues.</td>
</tr>
<tr>
<td>Predicate evaluation yields a false result.</td>
<td><b>well-defined</b>; the contract violation handler is invoked
and, if it returns, execution continues.</td>
</tr>
<tr>
<td>Predicate evaluation encounters undefined-behavior</td>
<td><b>undefined behavior</b>.</td>
</tr>
<tr>
<td>Predicate evaluation throws.</td>
<td><b>unspecified behavior</b>.</td>
</tr>
<tr>
<td>Predicate evaluation calls <tt>std::longjmp</tt></td>
<td><b>well-defined</b>; execution continues as specified by
<tt>std::longjmp</tt>.</td>
</tr>
<tr>
<td>Violation handler terminates<br/>
(calls <tt>std::abort()</tt>,
<tt>std::exit()</tt>,
<tt>std::quick_exit()</tt>,
<tt>std::terminate()</tt>,
etc...)
</td>
<td><b>well-defined</b>; the program terminates as specified by the called
function.</td>
</tr>
<tr>
<td>Violation handler returns</td>
<td><b>well-defined</b>; execution continues.</td>
</tr>
<tr>
<td>Violation hander throws</td>
<td><b>unspecified behavior</b>.</td>
</tr>
<tr>
<td>Violation handler calls <tt>std::longjmp</tt></td>
<td><b>well-defined</b>; execution continues as specified by
<tt>std::longjmp</tt>.</td>
</tr>
</table>
</p>
<h1 id="design">Design considerations</h1>
<p>
The following discussion provides the rationale for each of the proposed
semantics enumerated in the
<a href="#proposal">proposal</a>
section above.
</p>
<p>
The proposed design avoids the need for the C++ standard to acknowledge or
to specify build modes.
As always, implementations may offer options to optimize code generation
based on an assumption that a contract violation handler will not return
nor throw an exception.
</p>
<div style="margin-left: 1em;">
<h2 id="design-pred-uneval">Predicate is unevaluated</h2>
<p>
When predicate evaluation is not enabled, it seems clear that there is just
one reasonable choice of semantics; that execution enters the function body.
However, that does not mean that an implementation should completely ignore
predicates; logic errors that involve predicates can still be diagnosed and
can still affect observed program behavior.
The following example contains such an error; the precondition unconditionally
dereferences parameter <tt>p</tt> but the function body has behavior that is
conditional on <tt>p</tt> being a null pointer.
Implementors should be encouraged to diagnose such issues as a matter of QoI.
Static analysis tools commonly detect and report issues like this.
<div style="margin-left: 1em;">
<pre><code class="c++">int f(int *p)
PRE( *p < 5 )
{
if (!p)
return 0;
return *p;
}</code></pre>
</div>
</p>
<p>
Note that the logic error in the above example matches the one in the following
example using <tt>assert</tt>.
One of the benefits that contracts brings is that observance of the logic
error is not dependent on a build mode; the predicate above is always present
whereas the expression in the <tt>assert</tt> expression below is hidden from
the compiler when compiling with <tt>NDEBUG</tt> defined.
<div style="margin-left: 1em;">
<pre><code class="c++">int f(int *p)
{
assert( *p < 5 );
if (!p)
return 0;
return *p;
}</code></pre>
</div>
</p>
<h2 id="design-pred-true">Predicate evaluation yields a true result</h2>
<p>
When predicate evaluation is enabled and evaluation yields a true result, it
is likewise clear that the desired behavior is that execution enters the
function body.
The concerns about potential logic errors above are likewise applicable.
</p>
<h2 id="design-pred-false">Predicate evaluation yields a false result</h2>
<p>
When predicate evaluation is enabled and evaluation yields a false result,
this proposal calls for a contract violation handler to be invoked.
Unlike
<a title="Contract-Violation Handlers"
href="https://wg21.link/p2811r1">P2811R1</a><sup><a title="Contract-Violation Handlers"
href="#ref_p2811r1">[P2811R1]</a></sup>,
this proposal delegates all responsibility for handling the violation to
the handler.
If program termination is desired (the "enforce" or "Eval_and_abort" semantic
described in other papers), then it is up to the handler to perform
that termination (perhaps by calling the implementation-provided handler).
if it is desired to continue program execution (the "observe" or
"Eval_and_log" semantic), then the handler can simply return; this is consistent
with the behavior of a contract violation when contract checking is not enabled.
</p>
<h2 id="design-pred-ub">Predicate evaluation encounters undefined-behavior</h2>
<p>
It is unlikely to be surprising that the proposed semantic for
undefined-behavior is undefined-behavior.
This case is explicitly called out to acknowledge previous discussion of
<a title="Contracts for C++: Prioritizing Safety"
href="https://wg21.link/p2680r1">P2680R1</a><sup><a title="Contracts for C++: Prioritizing Safety"
href="#ref_p2680r1">[P2680R1]</a></sup>.
Though consensus for that proposal has not been established, the potential
for another proposal that seeks to constrain undefined-behavior within
contract predicates to attain consensus is well recognized.
In the meantime, this proposal reflects the status quo.
</p>
<h2 id="design-exceptions">Predicate evaluation or violation handler throws</h2>
<p>
It remains unclear what semantics are desired in the case of exceptions that
propagate from a predicate evaluation or violation hander invocation;
particularly with regard to a function declared with a <tt>noexcept</tt>
exception specification.
Does such an exception originate from within the function such that it
immediately hits the <tt>noexcept</tt> boundary?
Or is it considered to have been thrown from the caller side of the function
as is the case for exceptions thrown during parameter initialization?
If the latter, how does that interact with the <tt>noexcept</tt> operator?
Due to these open questions, this paper proposes that throwing an exception
from a predicate or a violation handler have unspecified behavior until
consensus is established for a direction as
<a title="Caller-side precondition checking, and Eval_and_throw"
href="https://wg21.link/p2780r0">P2780R0</a><sup><a title="Caller-side precondition checking, and Eval_and_throw"
href="#ref_p2780r0">[P2780R0]</a></sup>
seeks to do.
</p>
<h2 id="design-longjmp">Predicate evaluation or violation handler calls <tt>std::longjmp</tt></h2>
<p>
The open questions regarding exception handling don't apply to calls to
<tt>longjmp</tt> since questions of <tt>noexcept</tt> exception specifications
and the <tt>noexcept</tt> operator don't apply.
The absence of a reason to deviate <tt>longjmp</tt> behavior in contract
contexts therefore motivates making use in these contexts well-formed.
</p>
</div>
<h1 id="implementation-exp">Implementation experience</h1>
<p>
None.
</p>
<h1 id="ack">Acknowledgements</h1>
<p>
Thank you to Josua Berne for his excellent exposition of possible contract
violation behaviors in
<a title="Contract-Violation Handlers"
href="https://wg21.link/p2811r1">
P2811R1</a>
<sup><a title="Contract-Violation Handlers"
href="#ref_p2811r1">[P2811R1]</a></sup>.
</p>
<h1 id="references">References</h1>
<table id="references">
<tr>
<td id="ref_p2680r1"><sup>[P2680R1]</sup></td>
<td>
Gabriel Dos Reis,
"Contracts for C++: Prioritizing Safety", P2680R1, 2022.<br/>
<a href="https://wg21.link/p2680r1">
https://wg21.link/p2680r1</a></td>
</tr>
<tr>
<td id="ref_p2698r0"><sup>[P2698R0]</sup></td>
<td>
Bjarne Stroustrup,
"Unconditional termination is a serious problem", P2698R0, 2022.<br/>
<a href="https://wg21.link/p2698r0">
https://wg21.link/p2698r0</a></td>
</tr>
<tr>
<td id="ref_p2780r0"><sup>[P2780R0]</sup></td>
<td>
Ville Voutilainen,
"Caller-side precondition checking, and Eval_and_throw", P2780R0, 2023.<br/>
<a href="https://wg21.link/p2780r0">
https://wg21.link/p2780r0</a></td>
</tr>
<tr>
<td id="ref_p2784r0"><sup>[P2784R0]</sup></td>
<td>
Andrzej Krzemieński,
"Not halting the program after detected contract violation", P2784R0, 2023.<br/>
<a href="https://wg21.link/p2784r0">
https://wg21.link/p2784r0</a></td>
</tr>
<tr>
<td id="ref_p2811r1"><sup>[P2811R1]</sup></td>
<td>
Joshua Berne,
"Contract-Violation Handlers", P2811R1, 2023.<br/>
<a href="https://wg21.link/p2811r1">
https://wg21.link/p2811r1</a></td>
</tr>
<tr>
<td id="ref_p2817r0"><sup>[P2817R0]</sup></td>
<td>
Andrzej Krzemieński,
"The idea behind the contracts MVP", P2817R0, 2023.<br/>
<a href="https://wg21.link/p2817r0">
https://wg21.link/p2817r0</a></td>
</tr>
<tr>
<td id="ref_p2838r0"><sup>[P2838R0]</sup></td>
<td>
Ville Voutilainen,
"Unconditional contract violation handling of any kind is a serious problem", P2838R0, 2023.<br/>
<a href="https://wg21.link/p2838r0">
https://wg21.link/p2838r0</a></td>
</tr>
</table>
<h1 id="wording">Wording</h1>
<p>
Wording will be provided in a future revision contingent on SG21 establishing
consensus for a proposed direction.
</p>
</body>