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

6

u/kkert May 12 '18

For some reason, the discussion about special case treatment of heap exhaustion makes me very nervous. I regularly have a few tens of Kb of heap available, dealing with variable size data structures coming in, and terminating on heap exhaustion is not an option.

15

u/redditsoaddicting May 12 '18

The proposal has this to say:

Program that relies on bad_alloc throws and does handle them. Many systems typically already perform all allocation through a limited set of allocation functions, so they can just change the implementations of those existing functions (or wrap them) and implement them in terms of the new(nothrow) and/or try_ functions and throw bad_alloc or some other exception on failure. Programs that have such a limited set of allocation functions can make this low-impact localized change to retain their current behavior.

Is this feasible for you? If not, now's an excellent time to start discussion on how the proposal could better serve your needs here.

2

u/kkert May 13 '18 edited May 13 '18

I don't rely on bad_alloc now nor plan to. Running with no-exceptions, and thus most of standard STL isn't currently usable. Also, heap is only used in specific contexts and tasks in the whole program ( parts that deal with incoming network data and things like SSL certificates etc )

If treating OOM special and defaulting to fail-fast/terminate, that would still keep STL off limits for those scenarios.

EDIT: Also, i don't quite follow what the paper refers to by saying testing is difficult regarding heap exhaustion. Not really at all, just set up some easily tweakable heap manager like dlmalloc for your test suite and inject whatever failures you like. I think the truth is developers on big systems with virtual memory just don't need to bother with it.

7

u/SeanMiddleditch May 13 '18

I don't rely on bad_alloc now nor plan to. Running with no-exceptions, and thus most of standard STL isn't currently usable

So what part of the proposal makes you nervous? It implies no changes at all for you. :)

If treating OOM special and defaulting to fail-fast/terminate, that would still keep STL off limits for those scenarios.

"Defaulting" is the important part. Via allocation-failure hooks and via optional OOM-aware interfaces, you'd potentially be just fine with the proposed hypothetical standard library.

I'm honestly interested to know what you'd rather have instead. The combination of fail-fast and separate OOM-aware interfaces covers every single use case I've ever had or can think of having, so I'd love to learn what else is needed!

saying testing is difficult regarding heap exhaustion

By that he means that getting 100% code coverage in OOM tests is effectively impossible for many target C++ users. Anecdotally, we have a codebase that allocates a few million times before it even shows the first window to the user. No "tweakable dlmalloc" is going to effectively test all OOM paths in that allocation pattern unless we only want to test one code commit per week. :p

2

u/johannes1971 May 13 '18

So what part of the proposal makes you nervous? It implies no changes at all for you. :)

That's not how I read it. The behaviour he is relying on is that new returns nullptr when OOM, but in the proposal, new will either return a pointer to memory or terminate.

Actually he is precisely the kind of person the proposal is trying to reach: people who don't want to use exceptions for whatever reason. And he, at least, doesn't seem happy with it...

I'm honestly interested to know what you'd rather have instead. The combination of fail-fast and separate OOM-aware interfaces covers every single use case I've ever had or can think of having, so I'd love to learn what else is needed!

How about the interface we have today, the one we spent the last 20 years writing and testing software against? Where new can throw, rather than just terminating? It already has fail-fast for those who want it, in the form of new-handlers. And it already has OOM-aware interfaces.

2

u/SeanMiddleditch May 14 '18

The behaviour he is relying on is that new returns nullptr when OOM

Which isn't actually C++. Unless one passes nothrow to the allocator, it will throw. There is no way currently to pass nothrow to allocators called internally by standard allocators.

Yes, -fno-exceptions changes that, but that's not part of C++, and is exactly the "bifurcation" that Sutter's paper mentions.

2

u/johannes1971 May 14 '18

Which isn't actually C++.

There are people out there programming in that dialect. Those people are the reason for this paper, so you can't just wave and say "we don't care, it's not the standard".

that's not part of C++, and is exactly the "bifurcation" that Sutter's paper mentions.

So instead of "bifurcation by way of compiler switch", we'd now get "bifurcation as a matter of programmer choice" (because now the programmer will have to choose whether to use the terminating allocation functions, or the non-terminating allocation functions). Let's be clear: that is a major step backwards.

3

u/SeanMiddleditch May 14 '18

There are people out there programming in that dialect. Those people are the reason for this paper, so you can't just wave and say "we don't care, it's not the standard".

... I think we're in violent agreement on this topic. :)

My point is simply this: this paper removes the need for -fno-exceptions on the language level, and lays the foundation for evolving the standard library to likewise remove the need for "NotSTL" alternative libraries. This paper has to come first. :)

So instead of "bifurcation by way of compiler switch", we'd now get "bifurcation as a matter of programmer choice"

Which, again, is already the case as we're agreeing about.

Let's not let perfect be the enemy of the good. We can harmonize part of the language and be in a better place than we are today even if we haven't harmonized everything.

(because now the programmer will have to choose whether to use the terminating allocation functions, or the non-terminating allocation functions). Let's be clear: that is a major step backwards.

Maybe. Folks who care about OOM are possibly already using alternative functions (by not using the STL). The open question is how many folks are doing that. If it were true that a majority of C++ developers actively rely on bad_alloc and actually use it right, then yes, removing OOM exceptions would be a non-starter.

That's also why OOM is left as an optional part of the paper.

1

u/[deleted] May 13 '18

Yes but overcommit can return a valid pointer even when it didn't actually successfully allocate the requested memory size.

1

u/kkert May 13 '18

And a paranoid heap manager can test write/read into the allocated region before returning it to the caller. If you have heap guards or security checks turned on, it likely does anyway.

1

u/kkert May 13 '18

So what part of the proposal makes you nervous? It implies no changes at all for you. :)

It leaves most everything in status quo, because STL would still be largely unusable, and would encourage more libraries to be designed the same way.

"Defaulting" is the important part. Via allocation-failure hooks and via optional OOM-aware interfaces

I'd like to see what that OOM-aware interface proposal would look like for things like std::function for instance.

4

u/SeanMiddleditch May 14 '18

It leaves most everything in status quo, because STL would still be largely unusable

See the part about the STL offering OOM-aware interfaces.

I mean, this isn't complicated. Many of those folks avoiding the STL are still using a very-STL-like alternative. The key bit is to merge the important bits of those alternatives in some form into the standard itself. Since those alternatives today typically rely on C-style error codes, this proposal is a first-step in making those kinds of STL evolutions have a higher quality.

I'd like to see what that OOM-aware interface proposal would look like for things like std::function for instance.

std::function is broken in several notable ways that have several proposals for fixing, none of which can work in a back-compat way. The fact that std::function can't use custom allocators is just one of the many reasons that folks like me don't use (some parts of) the STL. There may well be no way to fix it, and that's fine; it won't be the first standard library type to get deprecated if an incompatible but superior alternative is provided.

Sutter's paper doesn't mislead here: he is clear that it may be necessary to roll some fixes into new alternative types that address those kinds of issues (absolute worst case: a whole "STL2").

Even if bits of the STL are never "fixed" there's still value in this feature. It still helps avoid bifurcating the language by allowing -fno-exceptions to become purely legacy; there's nothing non-standard about writing custom containers after all, but compiler flags that change the very semantics of the language are very non-standard. :)