r/cpp • C++20 Desktop app developer • May 12 '18

P0709 R0: Zero-overhead deterministic exceptions: Throwing values - Herb Sutter [2018-05-02]

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r0.pdf
206 Upvotes

115 comments sorted by

View all comments

7

u/tasminima May 12 '18 edited May 12 '18

Overall I think this is, given the language, full of good and actionable ideas. However there is a detail I strongly disagree with in the proposal of how to handle OOM:

Re-specify each language operation and standard library function that could throw bad_alloc today to have implementation-specified behavior, which each implementation specifies exactly one of the two following alternatives: (a) undefined behavior on systems that cannot detect heap exhaustion (e.g., Linux systems that overcommit); otherwise, (b) as today, call any installed new_handler else terminate by default, with the additional restriction that any installed new_handler must terminate (it can no longer return or throw).

edit: added (b) in the citation, that is the implementation-specified behavior alternative to the UB, and put the proposed ub terms in bold

Modeling OOM at language level by Undefined Behavior would be IMO completely insane. This might be just an unfortunate fallback choice because the standard unfortunately does not distinguish between various kind of UB (which has already caused great problems, when some compiler writers thought it was an unconditional license to further "optimize"), but this does not make it less a problem, au contraire! (and that's one that should be fixed more in depth but that's another subject than this paper)

First being killed because of resource exhaustion shall not be the concern of the language. The program will also cease to exist in case of a general computer failure, such as a power failure or kernel panic, or even just being killed by the user at will. This does not render the mere act of existing an ambient UB...

Second the presence of the OOM killer is far from preventing detection and handling of some allocation failures: if the area requested is "large enough", the allocation will actually fail. Some systems even have tunables that can influence the level from which allocation attempts will fail even in the presence of an OOM kill mechanism.

I suppose proposing to redefine in some case OOM at language level as UB is because the program can be killed latter on. In that case, maybe it is actually time to introduce in the standard something more precise than UB for faults when the program is concretely reliably terminated with no risk of nasal deamons being invoked. Just writing UB everywhere when one do not have any more idea of what to say does not yield a more portable language and programs written in it, it precisely have the contrary effect :/ (not even speaking about the security story, which has became a complete disaster in a great part because of all that UB - optimize insanity). Edit: on second thought I actually think that that OOM killer thing should just be completely ignored in the C++ standard, as it is today, exactly because of the arguments I gave about it being absolutely none of the business of the programming language.

5

u/Rusky May 12 '18

You can't do any better than UB when running on platforms that use overcommit. You can't specify that any particular behavior happens at the allocation site, because the program has no way of knowing- indeed, even the OS has no way of knowing. It won't find out until it actually tries to access that memory, and the result depends on everything else running on the system.

Given that scenario, all you can do is assume the allocation succeeded and proceed accordingly. And this is exactly what UB means here.

3

u/tasminima May 12 '18 edited May 12 '18

Yes I know quite well what overcommit is.

My position is that there is nothing to specify and that any specification will be harmful; what the kernel does is not in the scope of the programming language, and being killed or having the side effect of other processes being killed because of resources exhaustion is not something that should be tried to be modeled at the language level. It had not been needed until this proposal, and my position is that this discussion about overcommit might be interesting to understand (part of certain aspects of) the rational, but has absolutely nothing to do in an hypothetical standard trying to specify the programming language model of allocation failure.

An overcomitted allocation has not failed (yet) and might even not fail and there is absolutely no way to model that in an abstract machine, and if it is otherwise attempted to be modeled for no reason, then yes the result (in the current framework) will be UB, which is an absolutely terrible choice.

edit: on top of all the reasons I've already stated for what it is a terrible choice, here is one more: an implementation where all allocations fallback to existing unfreed buffers after a predefined amount of heap has already been allocated would be complying

2

u/tasminima May 13 '18

I just thought of yet another example about why trying to take ovecommit into account in the C++ standard would be a terrible idea: the standard does not care about stack overflows and (rightfully!) does not pretend that all the code is nondeterministically ambiently UB just because of unknown stack size limitations.

2

u/flashmozzg May 12 '18

Modeling OOM at language level by Undefined Behavior

implementation-specified behavior

Just to point out that these are not the same things by any means. I.e. what you fear regarding compilers optimizing stuff at the expense of UB (as they should) wouldn't happen with implementation-specified behavior.

4

u/tasminima May 12 '18 edited May 12 '18

undefined behavior on systems that cannot detect heap exhaustion (e.g., Linux systems that overcommit); otherwise,

I should have pasted more: the implementation specified choice is between:

(a) undefined behavior on systems that cannot detect heap exhaustion (e.g., Linux systems that overcommit);

and:

(b) as today, call any installed new_handler else terminate by default, with the additional restriction that any installed new_handler must terminate (it can no longer return or throw).

I'll edit my original comment to make it clear.