News for hackers. Stuff that matters.
Re:A bug story: data alignment on x86 by ogoffart on Monday November 07, 2016 @11:31AM (#12892311)
Again someone who relies on undefined behavior. Casting pointer of wrong alignement is not a platform specific behavior, it's an undefined behavior. Relying on it is an error.

The author did not know "What Every C Programmer Should Know About Undefined Behavior": http://blog.llvm.org/2011/05/what-every-c-programmer-should-...

Another good link about that: http://blog.regehr.org/archives/213

Re:A bug story: data alignment on x86 by loup-vaillant on Monday November 07, 2016 @02:15PM (#12893977)
Again someone blaming the victim. I'm kind of sick of this.

While true, your comment doesn't get at the root of the problem. The obvious fact here is that there is a mismatch between the C standard and how the users really use it. The less obvious fact/opinion/fallacy, is that it is not automatically the user's fault.

The standard could be wrong.

Sure, there are reasons why such and such behaviour ended up undefined. Those reason are sometimes weak however: some behaviour ended up undefined on all platforms because some of them couldn't handle it reasonably. The alignment bug here is such an example.

To this day I don't understand why undefined behaviour wasn't specified on a platform-by-platform basis. We already have implementation defined behaviour, after all. I guess this is because it lets compiler writers unify their front-ends and optimizers, but as a result, we cannot use our platforms to their fullest potential.

Re:A bug story: data alignment on x86 by bertit on Monday November 07, 2016 @05:48PM (#12895958)
Well, it is blaming someone who does not know how to use its tools. If I had writen a blog post aboug how I spent one day trying to fix a leak and then discovered that I always had to call free() after malloc(); would it still be victim blaming than blame me for not knowing the basic of C?

> The alignment bug here is such an example.

It is not since it allows vectorisation. The error was to assume that what one writes in C translates directly to assembly.

Re:A bug story: data alignment on x86 by benchaney on Monday November 07, 2016 @09:40PM (#12897168)
You are missing the point. The standard could be wrong, but it is still the standard. You still have an obligation to conform to it if you are writing C code. It is a prescriptive document, not a descriptive one. It can only be wrong to the extent that it can make poor decisions. Regardless of what it says it is authoritative.
Re:A bug story: data alignment on x86 by loup-vaillant on Tuesday November 08, 2016 @01:03PM (#12902473)
Did I gave the impression we don't need to conform to that standard? That wasn't my intention. I just wanted to point out the standard is sometimes stupid.

Of course we have to conform to the standard, however crazy. The only alternative is forking the language itself.

Re:A bug story: data alignment on x86 by saynsedit on Monday November 07, 2016 @06:34PM (#12896285)
Undefined behavior is one of the most intelligent things the C designers did when designing the language.

It's a fact of life that not all syntactic forms will have meaning. What's sqrt(-1)? Trick question, There's no meaningful answer (in terms of real numbers alone)! Why should anyone specify if it crashes the program, returns 0, throws an exception, etc.? Who cares? garbage in, garbage out.

Another example, "the floor had a pretty day with his melted spaceship" that is a grammatically well formed sentence but what does it mean? Don't answer that!

Re:A bug story: data alignment on x86 by loup-vaillant on Tuesday November 08, 2016 @01:12PM (#12902538)
> Undefined behavior is one of the most intelligent things the C designers did when designing the language.

I agree, actually. They just went too far. Too many things are undefined for no good reason. Even sqrt(-1) is debatable, by the way: if your platform provides an efficient way to trap, it should probably trap, and the compiler should not assume it will never happen.

And if you want crazy optimizations, consider introducing unsafe assertions into the language. That is, arbitrary boolean expressions the compiler is allowed to assume will always return true.

Re:A bug story: data alignment on x86 by saynsedit on Thursday November 10, 2016 @12:06PM (#12922116)
I don't necessarily disagree that they went too far though I assume they probably had good reason at the time in most cases.

I'd prefer it if the decision to trap or not were an option to the compiler.

It would be nice if there were a GUARANTEE() macro so that the programmer could specify conditions that would never happen even in a production build like: GUARANTEE(n >= 0). Also if trapping was enabled, it would trap at runtime. This is a nice post about that idea http://blog.regehr.org/archives/1096

Re:A bug story: data alignment on x86 by acqq on Monday November 07, 2016 @04:47PM (#12895488)
> The alignment bug here is such an example.

I would even claim that the language on all platforms definitely should be able to specify the "inconvenient" alignments, but that the platforms which can produce the fast code should. If you actually need some suboptimal alignment, you'll need it no matter if the '"language lawyers" cry now. I'd agree, the standard is potentially wrong for not having the possibility. It should be visually obvious that it's not optimal, but it shouldn't be too ugly to use it.

It's better to be able to declare something uniformly than to have pages of #ifdefs or to write a lot of ugly code.

This article and the comments here are good example. The article doesn't end there, it hopefully ends here in the comments, as qb45 claims that there is actually a declarative way for gcc, which at least on one version (the one which he tried) did produce the correct code:

https://news.ycombinator.com/item?id=12890429

But also see the other comments where some question if it realy works across the gcc versions.

Re:A bug story: data alignment on x86 by MaulingMonkey on Monday November 07, 2016 @05:42PM (#12895912)
> Again someone blaming the victim. I'm kind of sick of this.

C and C++ blame the victim - as do, in practice, compilers for them - by labeling what they've done "undefined behavior" and telling you to do better. I'm kind of sick of C and C++ too. Plenty better than I have attempted to argue the case for optimizers to hold back and behave saner, to stop their abuse. They have failed. I've done plenty of arguing against choosing C or C++ for projects that do not need it. I have failed. I can educate on undefined behavior, that victims may attempt to avoid and combat their abuse. Maybe someday they will break the cycle, and I will have had a hand in enabling them.

While perhaps ogoffart does not perfectly outline that maybe the language is at fault for some UB, ogoffart's links make the case. TIL about yet another source of undefined behavior:

>>> An unmatched ‘ or ” character is encountered on a logical source line during tokenization.

>> With all due respect to the C standard committee, this is just lazy.

My understanding is that this:

  int main() { return !"
Could launch NetHack. Disgusting.
Re:A bug story: data alignment on x86 by glandium on Monday November 07, 2016 @05:56PM (#12896017)
> by labeling what they've done "undefined behavior" and telling you to do better.

If only they did that. But no, they instead label what they've done "undefined behavior", and silently perform optimizations that may or may not break their program entirely. Double emphasis on silently.

Re:A bug story: data alignment on x86 by oldmanjay on Monday November 07, 2016 @06:55PM (#12896404)
There are no victims in engineering. Such phraseology should be saved for disciplines where emotion trumps reason, like politics.
Re:A bug story: data alignment on x86 by loup-vaillant on Tuesday November 08, 2016 @12:50PM (#12902357)
I disagree.

Engineering is full of needlessly awkward tools that end up being misused because of their useless warts. Every time that happens, the engineer that uses it is kind of a victim. And of course, there are the end users, who end up irradiated, spied upon, or robbed because of a technical failure allowed by needlessly unsafe tools.

(Big emphasis on "needlessly". Sometimes, the requirements are so stringent that only the unsafe tools do the job. Embedded environments, AAA games, or video encoders come to mind. Most of the time though, safer, less efficient tools are more than enough.)

Re:A bug story: data alignment on x86 by Sagiri on Monday November 07, 2016 @12:04PM (#12892624)
If a particular compiler specified that casting pointers of wrong alignments causes a segfault, it'd be perfectly acceptable to rely on that behavior. The standard would consider it UB, but that compiler has defined that behavior sufficiently.

Note, though, that a compiler simply doing a particular thing now isn't good enough to specify it in the sense that I mean. The compiler writers would have to explain (in a blog post or the like) the behavior and that they plan to keep that behavior in all future versions.

Re:A bug story: data alignment on x86 by Someone on Monday November 07, 2016 @01:10PM (#12893322)
Technically, if a compiler specifies how it handles undefined behavior (as opposed to implementation-defined behavior, which it must define), it stops being a C compiler :-)

I also think it is fairly unlikely that any C compiler will say anything about how it handles undefined behaviour because it would mean it has to generate awfully inefficient code. For example, a compiler could not optimize away most pointer dereferencing code if it promised that dereferencing odd addressses segfaults.

Yes, such checks might add at most a few percent to a normal program's running time, but add in all the other corner cases (int overflow, boundary checks, etc.) that also dat a few percent, amd before you know it your program runs at half the speed it could run at. If you find that acceptable, you shouldn't be writing C in the 21st century.

Re:A bug story: data alignment on x86 by to3m on Monday November 07, 2016 @01:27PM (#12893488)
The standard explicitly permits documented behaviour, even though it doesn't have to, because "undefined" covers that option, along with anything else you care to imagine: http://port70.net/~nsz/c/c11/n1570.html#3.4.3
Re:A bug story: data alignment on x86 by MaulingMonkey on Monday November 07, 2016 @12:26PM (#12892844)
> If a particular compiler specified that casting pointers of wrong alignments causes a segfault, it'd be perfectly acceptable to rely on that behavior.

This is a great way to make your programs "fun" to port to new platforms with new compilers in terrifyingly subtle ways. I prefer not to recommend this approach to solving specific cases of undefined behavior, although if you happen to disable strict aliasing (with e.g. -fno-strict-aliasing) as an additional layer of defensive paranoia, I'm not necessarily against that.

Re:A bug story: data alignment on x86 by Someone on Monday November 07, 2016 @12:56PM (#12893170)
You can also defensively add a quick test to your program's startup code and unit tests. Startup will take a tiny bit longer, but those porting your code will be thankful if they hit the problem, double so if you manage to emit a useful diagnostic.
Re:A bug story: data alignment on x86 by bertit on Monday November 07, 2016 @05:53PM (#12895992)
That would not work. Since the test may not cause any problems. While the compiler might find more optimzation opportuinities in the real program. (Just as the begining of the function did not have problem, but the for loop had.)

Just don't use undefined behavior.

Re:A bug story: data alignment on x86 by Someone on Tuesday November 08, 2016 @04:23PM (#12904067)
1. You write a few versions of a program.

2. Users report that it started crashing sometimes in version V.

3. After lots of debugging, you discover an input that reliably crashes your program after 30 minutes.

4. A bit later, you discover that your compiler started compiling function f so that it no longer works with unaligned data/buffers of exactly 8 bytes/whatever.

5. At the start of main, you add a dummy call to f with data that reliably crashes if your compiler decides to do that optimization again.

6. The program has become worse: it now always crashes, independent of its input, but you don't have to wait 30 minutes before finding out. That makes it way less likely that you ship a binary again that has the problem. It also makes it easier to tweak source code/compiler flags/whatever until the problem disappears.

Is that perfect? Absolutely not, it is more something of last resort, but depending on the costs of crashing versus those of sometimes crashing half-way through a run, it can be an improvement.

(this technique also can be used when your code hits compiler bugs)

Re:A bug story: data alignment on x86 by Sagiri on Monday November 07, 2016 @12:30PM (#12892884)
Well, yeah. If you have any reason to suspect that you will need to run your code on other platforms or compile with other compilers, don't do compiler-specific things. Just do it cross-platform the first time.

Hell, even if you don't think you'll ever need to do that, you should still avoid doing things that are platform or compiler specific.

Re:A bug story: data alignment on x86 by GlitchMr on Monday November 07, 2016 @12:56PM (#12893165)
The thing is, many compilers just assume that undefined behaviour won't happen, without defining any particular behaviour. And testing for something like pointer alignment on architectures that silently allow pointer misalignment is really, really expensive.

That said, you can use -fsanitize=undefined to verify correctness of a program (as far specification is concerned). Just be prepared for it being a bit slow.

Re:A bug story: data alignment on x86 by Jweb_Guru on Monday November 07, 2016 @12:59PM (#12893202)
It's not possible to determine all possible undefined behavior in a C program. -fsanitize=undefined is best-effort.
Re:A bug story: data alignment on x86 by scott_s on Monday November 07, 2016 @02:07PM (#12893888)
I believe you are confusing undefined behavior and implementation defined behavior. Undefined behavior is illegal under all compilers, and all bets are off if you do it. Implementation defined behavior is always legal, but different compilers are allowed to do different things.
Re:A bug story: data alignment on x86 by Sagiri on Tuesday November 08, 2016 @12:53PM (#12902379)
I'm not.

Undefined behavior is 'anything goes'. An implementation can choose a particular behavior that you can rely on for a particular case of UB, because, if the only rule is that 'anything goes', it doesn't violate that rule.

I'll admit that compilers don't generally do that - because specifying it could lead to fewer optimizations. But I did say "if", and there's no reason they couldn't do so in principle.

One could imagine a compiler with an extremely strict debug mode that traps on a number of situations that the standard deems undefined behavior via a segfault, in order to help people avoid relying on UB. Again - saying something like "casting misaligned pointers causes a segfault on [system]" would in no way violate a standard that says "casting misaligned pointers can do anything", because segfaulting falls under the umbrella of anything.

I think you're misinterpreting the fact that the results of undefined behavior can be ignored by a compiler for a requirement that it must be ignored by a compiler.

Re:A bug story: data alignment on x86 by startling on Monday November 07, 2016 @06:48PM (#12896363)
Undefined behavior is not illegal. The compiler can do anything with undefined behavior, including exactly what the author expected.
Re:A bug story: data alignment on x86 by scott_s on Tuesday November 08, 2016 @09:42AM (#12900578)
It is illegal, for any reasonable definition of illegal. See my comment from earlier in the year: https://news.ycombinator.com/item?id=10840497
Re:A bug story: data alignment on x86 by startling on Tuesday November 08, 2016 @06:11PM (#12904794)
"Illegal" seems stronger to me than "not strictly conforming", I guess (and so does "well-formed", for that matter). But I think we basically agree.
Re:A bug story: data alignment on x86 by konstmonst on Monday November 07, 2016 @12:19PM (#12892779)
"If a particular compiler specified that casting pointers of wrong alignments causes a segfault" Sure it does. It says using undefined behaviour can cause daemons fly out of your nose, among other things (including but not limited to segfaulting).
Re:A bug story: data alignment on x86 by Sagiri on Monday November 07, 2016 @12:23PM (#12892820)
If the semantics of casting pointers of wrong alignments were defined to be demons flying out of your nose, it would be perfectly acceptable to rely on that behavior.
Re:A bug story: data alignment on x86 by junke on Monday November 07, 2016 @12:39PM (#12892991)
If.

(famous reply from Sparta, Laconia, to Philip II of Macedon)