News for hackers. Stuff that matters.
Re:How expensive is integer-overflow trapping in C++? by asveikau on Thursday September 24, 2020 @02:37AM (#24575918)
Aborting the program on integer overflow seems so drastic. Say you do that in a server program. Then convincing it to overflow an integer somewhere (possibly somewhere where it is harmless) causes denial of service and all unrelated connections in the process die?

I guess languages with support for exceptions have a more natural action for this, that could be caught somewhere in a server's processing loop to not affect other clients.

Re:How expensive is integer-overflow trapping in C++? by alblue on Thursday September 24, 2020 @03:06AM (#24576092)
This is essentially Swift’s problem on the server. If you have a service running and serving hundreds of clients, then any one of those threads can cause a trivial panic if there’s an overflow and take the entire service down.

As a result, a number of use cases for swift on the server actually use heavyweight out of process threads (i.e. one process per client) so that the failure of one of them doesn’t take down the rest.

Unfortunately there’s no way of overriding the panic (is implemented as an in-line ud2 instruction rather than an overridable function) and while you can install a signal handler to catch it, resuming isn’t straightforward and there are some cases where you really can’t do anything about it (malloc failure).

This works great for single user devices where there’s only one app running at once and when it crashes the user restarts it, but doesn’t work in the server case unless you are using a cgi-bin/lambda pattern when each request is processed by a new process each time, which is what TF and the AWS lambda approaches do.

Re:How expensive is integer-overflow trapping in C++? by wahern on Thursday September 24, 2020 @03:45AM (#24576307)
This reminds me of Rust unconditionally ignoring SIGPIPE. Except in this case there's a non-trivial amount of pre-existing (and even future) code that assumes a process will be terminated if it attempts to write to a closed pipe. One of Rust's selling points is ease of FFI, so it's largely irrelevant that Rust APIs wouldn't let you ignore write errors. Rust breaks the environment for libraries that rely on the behavior.
Re:How expensive is integer-overflow trapping in C++? by Joker_vD on Thursday September 24, 2020 @05:19AM (#24576797)
It's a bad library design to rely on a process-global setting, because one day you'll have two libraries in your project that rely on opposite values of such a global setting, and then you'll start to see very funny things.

And the library design that relies on an application it's used in being killed by the OS so that the library don't have to tidy things up after itself or to properly handle error conditions is really bad. Don't you just like when one of your random indirect dependency, thrice-removed, decides to call std::abort() just because it's tired of living?

Re:How expensive is integer-overflow trapping in C++? by wahern on Thursday September 24, 2020 @05:42AM (#24576917)
I agree libraries shouldn't rely on process-global state. It's also bad library design to trigger signed overflow. In my libraries I try to handle both situations cleanly, but that's all beside the point.

There's also the problem that SIG_IGN handlers are inherited across exec. So Rust programs that exec other programs without restoring the default handler could cause problems in programs where relying on SIGPIPE is more reasonable. This is also true of posix_spawn: SIG_IGN handlers aren't implicitly reset.

Note that Rust doesn't restore any other handlers to their default disposition as far as I can tell. Do you write all your programs to reset signal disposition on startup? I'll admit I don't normally do that unless it's a daemon.

Re:How expensive is integer-overflow trapping in C++? by Joker_vD on Thursday September 24, 2020 @05:53AM (#24576977)
If I were to write a program that relied on "writing to a file might terminate me and that's exactly what I want" behaviour, then yeah, I absolutely would include a call to "signal(SIGPIPE, SIG_DFL)" in the main().

Of course, if I were to actually write a filter-like program (a la cat, head, grep, etc.), I would rather have just checked the return values of my write()s. It's -1? Time to call exit(errno).

Re:How expensive is integer-overflow trapping in C++? by zeotroph on Thursday September 24, 2020 @05:56AM (#24576997)
Yes, quite annoying, and I've been bitten by that before, but you are supposed to check the return of each write().

And I think the motivation is to write platform-agnostic code by default. SIGPIPE is only available on Unix, so you have to explicitly opt-in.

Re:How expensive is integer-overflow trapping in C++? by wahern on Thursday September 24, 2020 @06:05AM (#24577042)
The original impetus seems to have been for the now defunct green threading concurrency approach: https://github.com/rust-lang/rust/pull/13158/files

One of the post hoc rationales that seems to have cropped up is as you mentioned. But that logic only makes sense if you expect platform-specific code to be rare. I doubt that's the case: from my limited experience there seems to be quite a lot of Rust code written specifically for the Unix environment. Rust is a "systems" language after all; writing platform-specific applications, not simply platform-specific modules or wrappers, is to be expected.

It's a legitimate glitch in Rust. It happens. I don't understand the urge for people to defend Rust here. It violates well accepted norms in the world of systems programming--don't f'up the global environment without being asked. Indeed, part of the reason green threading failed was because it required too much magic interposed between the application and the native platform environment. Rust wasn't trying to go the route of Go.

Re:How expensive is integer-overflow trapping in C++? by saagarjha on Thursday September 24, 2020 @03:33AM (#24576237)
I’m curious if one could use a mildly-modified Swift compiler that converts all the different ways to abort execution into some sort of panic handler you can override would help work around this issue.
Re:How expensive is integer-overflow trapping in C++? by MaxBarraclough on Friday September 25, 2020 @05:01AM (#24587961)
As someone who doesn't know Swift, is there a reason it doesn't handle these errors by throwing exceptions? That approach works well for Java and .Net.
Re:How expensive is integer-overflow trapping in C++? by galad87 on Thursday September 24, 2020 @07:07AM (#24577380)
There is a compiler option to disable overflow/underflow checks, so unless the overflow is in some library outside your control, it's not actually an issue.
Re:How expensive is integer-overflow trapping in C++? by MaxBarraclough on Thursday September 24, 2020 @04:55AM (#24576666)
> Aborting the program on integer overflow seems so drastic.

Disagree. Overflowing of a signed integer type is undefined behaviour, which deserves to be taken seriously.

I was surprised to see that the article doesn't mention the signed/unsigned distinction, or undefined behaviour. Overflowing of a unsigned integer type is not a problem, it's defined to wrap around, and may be done intentionally.

Re:How expensive is integer-overflow trapping in C++? by UncleMeat on Thursday September 24, 2020 @08:27AM (#24577916)
> Overflowing of a unsigned integer type is not a problem, it's defined to wrap around, and may be done intentionally.

It is not a problem for the language, but it may be a problem for your program. Unsigned overflow can happily introduce vulns into your bounds checks.

Re:How expensive is integer-overflow trapping in C++? by scoutt on Thursday September 24, 2020 @09:33AM (#24578436)
I guess people overreact to "undefined behavior". If your software runs always on the same platform then you can know what the behavior would be. It might also be intentional and should not crash your program.

Undefined behavior is used when doing a specification for a compiler that will be used in many architectures and they cannot clearly specify what would happen on ALL architectures for a given operation, because the result might change among these architectures.

Undefined behavior doesn't mean that your CPU has either a chance to suddenly blow away or that it would gain self-awareness and destroy the world.

You can actually predict what would happen if you throw away a rock on earth. You cannot predict it for every planet in the universe, so theoretically "throwing a rock" produces undefined behavior.

Re:How expensive is integer-overflow trapping in C++? by jjnoakes on Thursday September 24, 2020 @10:07AM (#24578724)
> If your software runs always on the same platform then you can know what the behavior would be.

This is not necessarily true, and I consider it to be a harmful misconception.

I think people under-react to "undefined behavior".

What you think the platform would do if you overflowed an integer using machine instructions and what your C or C++ code ends up executing can be wildly different things because C and C++ compilers do everything in their power to optimize your code assuming your signed integers will never overflow. If they do, who knows what state your program is in or which machine instructions the compiler would have scheduled.

Re:How expensive is integer-overflow trapping in C++? by scoutt on Thursday September 24, 2020 @10:23AM (#24578865)
You are right. I might correct the sentence with "you can often know". Undefined behavior should be avoided but really is not that terrible.
Re:How expensive is integer-overflow trapping in C++? by jjnoakes on Thursday September 24, 2020 @10:49AM (#24579121)
We will have to agree to disagree.

Fully understanding what a program might do if there is undefined behavior is not trivial. It requires basically reviewing and fully understanding the output assembler for every single build.

Any change in system headers, compiler version, compiler flags, or source code might invalidate previous reviews.

And programs with lurking undefined behavior may not seem dangerous but sufficiently clever exploits can do quite amazing things with the smallest of attack surfaces.

Re:How expensive is integer-overflow trapping in C++? by scoutt on Thursday September 24, 2020 @11:59AM (#24579918)
Sure but in 17 years writing C for a living, with lines of code by the millions, and (embedded) devices running my code all over the world I still have to know about an integer overflow bug.

Sure, I don't always write bug-free code (as anyone), but when I say it's not so terrible I mean that UD should be considered a rare case, and it's not worth even a single night of quiet sleep, let alone switching languages or overreacting. Hey, I am more afraid of a flipping bit due to power supply noises.

And I don't want to say either that my code is perfect, or since it never happened to me it will never happen to you and that you can go around triggering UD everywhere since it's not a big deal. But I also like to warn young players that "boooo UD!" is just part of a FUD tactic for them to adopt some shiny new language.

Re:How expensive is integer-overflow trapping in C++? by jjnoakes on Thursday September 24, 2020 @01:44PM (#24581265)
There is a ton of software out there that is riddled with bugs. Much of it is run behind closed doors, isn't exposed to attack vectors like the internet, and happily churns along because no one is trying to attack it.

Lots of other software might be exposed to the internet or to malicious users but no one has stumbled across it and tried to exploit it yet.

Code like that may be ok for now simply because of the circumstances, but I don't think that's a pass to ignore UB or to discourage folks from trying to improve the status quo.

Because some day either the developer of that code, who is used to ignoring this kind of thing, might write some code that has to be more correct or it will get exploited. Or the code itself might get changed, or exposed to attackers, or copied into some other program.

These things happen and they are preventable. And you don't even have to switch languages - turning on sanitizers when you compile and fixing issues as they are found, and encouraging others to do the same, is better (in my opinion) than categorizing folks worrying about and working on this issue - in C and in other languages - as FUD spreaders.

Re:How expensive is integer-overflow trapping in C++? by scoutt on Thursday September 24, 2020 @04:31PM (#24583256)
There are worse things than can happen with bad wrote code other than security bugs. It doesn’t have to necessarily be exposed to the internet or users/attackers for bad things to happen. I truly believe that people just forgot this little fact.

Again, I don’t discourage people from preventing UD, but, again, it’s not a big deal. Especially is not something to be afraid of.

My code is as much as correct, verified, analyzed and tested as possible, and most of the times compliant with the most absurd certifications. Again, I still have to find a bug or fail a test because some strange UD.

Re:How expensive is integer-overflow trapping in C++? by MaxBarraclough on Thursday September 24, 2020 @05:18PM (#24583794)
> Especially is not something to be afraid of.

Disagree. UB in C can mean your program silently going off the rails in a way that isn't possible in many other languages. UB really can manifest in bizarre and scary ways on real platforms. [0][1][2][3] If it isn't manifesting that way in your case, that's just good fortune.

> I still have to find a bug or fail a test because some strange UD

I should hope your safety-critical code is not permitted to contain known instances of unintended undefined behaviour. (There can be exceptions where UB is truly intended. JIT compilation always relies on UB, for instance, not that this would apply to safety-critical code.)

If your program contains undefined behaviour, that means that at best, it works by coincidence. Safety-critical code should not work by coincidence, it should be correct by construction (to steal a term from the formal methods world).

Also, it's UB, not UD.

[0] https://devblogs.microsoft.com/oldnewthing/20140627-00/?p=63...

[1] https://cryptoservices.github.io/fde/2018/11/30/undefined-be...

[2] https://blog.regehr.org/archives/759

[3] https://blog.regehr.org/archives/767

Re:How expensive is integer-overflow trapping in C++? by scoutt on Thursday September 24, 2020 @07:15PM (#24584787)
Again, I am not saying "go ahead and don't care about UB". I know what it is; I know what it can do. Just don't make it a big deal. You (plural) make it sound like it's impossible to build a system with C or any other laguange that allows UB, because it probably runs by coincidence. Yet the world keeps spinning and super safe flashy languages are still running on top of UB-illed OS wrote in C.

> I should hope your safety-critical code is not permitted to contain known instances of unintended undefined behaviour.

It's not.

> Also, it's UB, not UD.

Now that you mention it I checked my other comments I wrote from my phone and noticed it changed UB with UD.

Re:How expensive is integer-overflow trapping in C++? by jjnoakes on Thursday September 24, 2020 @10:17PM (#24585940)
I don't think we are claiming what you say we are claiming.

I know I'm only responding because of your claim that you can know what your code does even in the face of undefined behavior.

I think this more accurately describes my position:

Code without undefined behavior can absolutely run just fine.

Code with undefined behavior is a problem waiting to happen.

Code with undefined behavior that happens to run the way you want is a happy accident. The longer it runs correctly the luckier you are.

Code in safety systems should not contain undefined behavior.

I would encourage everyone to be aware of undefined behavior, get rid of it when they know of it, and take steps to be proactive against it. For code in safety systems I would require these things if it were up to me.

Re:How expensive is integer-overflow trapping in C++? by jjnoakes on Thursday September 24, 2020 @06:41PM (#24584523)
The point is not to fear undefined behavior (though if my software was used in safety systems, I would fear undefined behavior).

The point is to eliminate it because it can cause problems in even the most well-tested code, either when the code inevitably operates outside the test parameters or when compilers or compile flags or system headers change.

Eliminate it both reactively and protectively. Not only in your own code, but encourage others to do the same.

Re:How expensive is integer-overflow trapping in C++? by pjmlp on Thursday September 24, 2020 @12:44PM (#24580575)
Would you feel the same way when we finally manage to get liability laws for security exploits, like we are now discussing in Germany?
Re:How expensive is integer-overflow trapping in C++? by scoutt on Thursday September 24, 2020 @04:12PM (#24583057)
If some of my systems fail, people might die. Should I still be worried for a liability?
Re:How expensive is integer-overflow trapping in C++? by pjmlp on Friday September 25, 2020 @12:57AM (#24586703)
As an professional Engineer I would be.
Re:How expensive is integer-overflow trapping in C++? by asddubs on Saturday September 26, 2020 @08:02AM (#24598145)
Re:How expensive is integer-overflow trapping in C++? by fbkr on Thursday September 24, 2020 @12:56PM (#24580732)
This is not correct, what you are thinking is implementation defined or maybe unspecified behavior.

Please do explain the behavior of calling an uninitialized function pointer on x86 if you insist.

Re:How expensive is integer-overflow trapping in C++? by MaxBarraclough on Thursday September 24, 2020 @01:09PM (#24580886)
As jjnoakes and fbkr already commented, this account of undefined behaviour is not correct. You've conflated it with unspecified behavior and implementation-defined behavior, and they're not the same beast. [0]

Undefined behaviour does not mean that the behaviour simply depends on your platform and is otherwise harmless. That's a widespread misconception, and a rather harmful one. You may see that a signed integer overflow appears to wrap harmlessly on your platform, but the compiler is under no obligation to consistently handle signed integer overflow in this way, unless its own documentation makes this guarantee.

> people overreact to "undefined behavior"

I have to agree with jjnoakes that people more commonly underreact to it.

> If your software runs always on the same platform then you can know what the behavior would be.

You cannot infer what non-standard guarantees your compiler is committed to. Unless the compiler guarantees to handle UB in a certain way, it is free to surprise you at any time.

> It might also be intentional

As mentioned above, intentional UB only makes sense if the compiler guarantees to give you the expected behaviour. If that's not the case, it's a bug.

Even then, it's probably best to write portable standards-compliant code.

Either way I'd hope to see a static-assert checking that the expected compiler is being used, loudly breaking the build if this check fails.

> should not crash your program.

This isn't the case. Dereferencing NULL may cause a segfault, terminating your process. Dividing by zero might cause a similar kind of explosion. Depending on platform, freeing a pointer twice might also immediately crash the program (the most helpful way for a platform to handle a double-free).

> Undefined behavior is used when doing a specification for a compiler that will be used in many architectures and they cannot clearly specify what would happen on ALL architectures for a given operation, because the result might change among these architectures.

Not always. The C rule about data-races being undefined behaviour, is intended to enable compiler optimisation, by permitting compilers to assume the absence of data-races. This rule would make sense even if C weren't intended to be portable across different hardware architectures.

> Undefined behavior doesn't mean that your CPU has either a chance to suddenly blow away

In principle, undefined behaviour could result in hardware damage, but this seems pretty unlikely unless you're writing power-management code for a motherboard, or something like that. Of course, if you're writing code for a medical system, industrial-control system, or avionics, then undefined behaviour might have terrible consequences.

Self-awareness would be permitted by the C standard, but, again, rather unlikely.

> You can actually predict what would happen if you throw away a rock on earth. You cannot predict it for every planet in the universe, so theoretically "throwing a rock" produces undefined behavior.

I don't see much value in this analogy. It's possible to build a fully deterministic programming language, with no undefined behaviour, and no platform-specific variation. C isn't designed that way, but this is a property of C, not a general fact of computation. It's also possible to write C code entirely devoid of undefined behaviour.

[0] https://stackoverflow.com/a/4105123/

Re:How expensive is integer-overflow trapping in C++? by masklinn on Friday September 25, 2020 @03:42AM (#24587529)
You’re you’re talking about the behaviour in C. Other langages are not C and can and do define other behaviours.

Overflow is neither wrapping nor UB in Swift, it’s an error. Overflow is never UB in Rust but it might be wrapping or a panic. Overflow doesn’t exist in Python or Erlang. I think overflow is IB in Haskell.

You get the point: that signed overflow is UB and unsigned wraps in C has no bearing on what other langages do.

Swift traps on overflow, regardless of signedness.

Re:How expensive is integer-overflow trapping in C++? by MaxBarraclough on Friday September 25, 2020 @04:52AM (#24587919)
This thread is about C++.
Re:How expensive is integer-overflow trapping in C++? by masklinn on Friday September 25, 2020 @11:23AM (#24590775)
This sub-thread's starting point was literally a comment on Swift's behaviour. Unless C++ has drastically changed and now abruptly aborts on overflow.
Re:How expensive is integer-overflow trapping in C++? by MaxBarraclough on Friday September 25, 2020 @11:35AM (#24590903)
I think you have this confused with the subthread under alblue's comment.
Re:How expensive is integer-overflow trapping in C++? by R0b0t1 on Thursday September 24, 2020 @11:18AM (#24579404)
Are you sure? IIRC overflow or underflow is always UB per the (earlier?) C standards; they did not expect a single popular representation system for numbers.
Re:How expensive is integer-overflow trapping in C++? by MaxBarraclough on Thursday September 24, 2020 @12:37PM (#24580480)
Yes, I'm quite sure. C mandates that unsigned integer types use the typical binary representation, and it mandates wrapping arithmetic. I don't know much about the early C standards, it's possible it wasn't always this way.

https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pa...

https://stackoverflow.com/a/18195756/

Re:How expensive is integer-overflow trapping in C++? by lmm on Thursday September 24, 2020 @05:09AM (#24576747)
> Aborting the program on integer overflow seems so drastic. Say you do that in a server program. Then convincing it to overflow an integer somewhere (possibly somewhere where it is harmless) causes denial of service and all unrelated connections in the process die?

If you don't abort then you probably have an arbitrary code execution vulnerability. Overflow is undefined behaviour in C++, your code is in an unanticipated state, a creative attacker can probably leverage that to do anything.

Re:How expensive is integer-overflow trapping in C++? by asveikau on Thursday September 24, 2020 @10:09AM (#24578741)
> If you don't abort then you probably have an arbitrary code execution vulnerability.

There are some patterns in which that is totally true, for example if an integer product is passed to malloc or realloc. Those cases tend to get extra scrutiny in security reviews because it is a well known class of issues. However, it is certainly not the case that every case is exploitable.

Re:How expensive is integer-overflow trapping in C++? by lmm on Thursday September 24, 2020 @07:58PM (#24585074)
Not every case is exploitable, but people are bad at reasoning about which cases are exploitable; a lot of DoS bugs that are initially thought to be harmless turn out to be exploitable. So I think it's important to treat any UB as exploitable until proven otherwise.
Re:How expensive is integer-overflow trapping in C++? by higerordermap on Thursday September 24, 2020 @06:33AM (#24577178)
He is saying it should raise an exception which can be caught somewhere up the stack.
Re:How expensive is integer-overflow trapping in C++? by asveikau on Thursday September 24, 2020 @11:14AM (#24579362)
Yes, but I would take minor issue in that I wasn't saying what should happen so much as what the options are given various tradeoffs.

There are programming communities that have criticisms of exceptions and would like to avoid them. I am sympathetic to that. But if you do have exceptions, this is a good candidate. If you don't have exceptions, making integer overflow trap seems problematic. Posix signals may be another solution, with its own problems and ugliness.

Re:How expensive is integer-overflow trapping in C++? by amelius on Thursday September 24, 2020 @05:28AM (#24576840)
Perhaps we can have both. E.g.

    __checked__ int a;           // aborts on overflow
    __throwing_checked__ int b;  // throws exception on overflow
You could have the same syntax for unsigned ints.
Re:How expensive is integer-overflow trapping in C++? by mhh__ on Thursday September 24, 2020 @06:06AM (#24577046)
D has checkedint which does this cleanly in a template
Re:How expensive is integer-overflow trapping in C++? by mhh__ on Thursday September 24, 2020 @06:05AM (#24577043)
That's the tradeoff at play here. Think of it like debugging a python program, it might fail hours after you start it but if it does fail it's failed for a reason (uncaught overflows can be very very not good)
Re:How expensive is integer-overflow trapping in C++? by ckwalsh on Thursday September 24, 2020 @03:09AM (#24576106)
In prod it's not a great idea, but in a test environment, perhaps handling simulated or shadow traffic, it's great.
Re:How expensive is integer-overflow trapping in C++? by gpderetta on Thursday September 24, 2020 @04:54AM (#24576661)
FWIW, division by zero already does the same thing.
Re:How expensive is integer-overflow trapping in C++? by asveikau on Thursday September 24, 2020 @10:12AM (#24578772)
And that's one reason that when I write code that divides and it's not known ahead of time, I usually check the denominator for zero.

Doing that for every mathematical operation would not be feasible.

Edit: hello good sir or madam downvoter, can someone explain why this is a controversial opinion? In languages where this error does not throw exceptions and in problem domains where the process really needs to stay up, reasonable caution would often mean you might manually check possible denominators for zero.