News for hackers. Stuff that matters.
Re:“Unexplainable” core dump (2011) by ericbarrett on Tuesday January 03, 2023 @03:55PM (#34237904)
This is great. Reminds me of a crash I saw early in my career. It was a null-pointer exception, except it occurred right after confirming the address was non-null. This was on a single core with a non-preemptible kernel. So the processor just took the wrong branch! There was simply no other explanation.
Re:“Unexplainable” core dump (2011) by mcculley on Tuesday January 03, 2023 @04:40PM (#34238480)
What hardware/platform was this? I worked on AIX on POWER a long time ago and it had to map the zero page read-only just to support speculative execution of dereferencing the NULL pointer, if I remember right.

If you were on a platform that did this wrong, speculative execution could have been dereferencing the NULL pointer.

Re:“Unexplainable” core dump (2011) by ericbarrett on Tuesday January 03, 2023 @09:05PM (#34241015)
It was a DEC Alpha chip, over two decades ago. (Or MIPS?) Pretty sure there was no speculative execution. In any case, the senior engineer I was working with would have known that—getting to "it just did the wrong thing" was our final and least satisfying conclusion!
Re:“Unexplainable” core dump (2011) by mcculley on Tuesday January 03, 2023 @09:25PM (#34241178)
The DEC Alpha had speculative execution.

I found lots of bugs porting code to it. It was my first 64-bit platform.

Re:“Unexplainable” core dump (2011) by thewebcount on Wednesday January 04, 2023 @01:59AM (#34242874)
I found a bug in the compiler on it! I was writing some fractal generating code, and found that something I did caused the compiler to output the error message: “Dave doesn’t think this should compile. Please send an email to dave<someone-or-other>@dec.com”. So I did. They replied that they found the problem and had a fix for it.
Re:“Unexplainable” core dump (2011) by lmm on Wednesday January 04, 2023 @12:44AM (#34242438)
The Alpha does quite aggressive instruction reordering, you need to use more memory barriers than x86 in parallel code for example.
Re:“Unexplainable” core dump (2011) by jrpelkonen on Tuesday January 03, 2023 @04:24PM (#34238279)
Interesting, how did you fix it? Negate the comparison with an appropriate comment?
Re:“Unexplainable” core dump (2011) by ericbarrett on Tuesday January 03, 2023 @09:02PM (#34240985)
There was no fix! It was a one-off hardware error.
Re:“Unexplainable” core dump (2011) by Jiro on Tuesday January 03, 2023 @04:55PM (#34238647)
Are you sure the compiler didn't say "since having a null pointer gives undefined behavior, we can optimize out the part that confirms the address is non-null"?
Re:“Unexplainable” core dump (2011) by logicchop on Tuesday January 03, 2023 @05:03PM (#34238747)
This is likely your answer. C++ story. I worked at a large company that had a "no exceptions" policy and a custom operator new. If a new expression failed it would return nullptr instead of throwing. So lots of people wrote "checking" code to make sure the result wasn't nullptr, except that the compiler would always just elide that code since the standard mandates that the result cannot be nullptr. Many weird crashes ensued.
Re:“Unexplainable” core dump (2011) by aw1621107 on Tuesday January 03, 2023 @05:06PM (#34238789)
There are non-throwing operator new overloads that can return nullptr, but I'm not sure if those are a relatively recent development. Did the non-throwing operator new overloads not exist at the time?
Re:“Unexplainable” core dump (2011) by logicchop on Tuesday January 03, 2023 @05:14PM (#34238895)
Hard to say. Most of the uses probably predated the custom operator new and so nobody thought about it. Not to mention the places you cannot sneak into to switch to std::nothrow.
Re:“Unexplainable” core dump (2011) by aw1621107 on Wednesday January 04, 2023 @12:12AM (#34242270)
Ah, that's fair. Didn't think of code that couldn't be changed.
Re:“Unexplainable” core dump (2011) by bregma on Tuesday January 03, 2023 @07:52PM (#34240366)
`new (nothrow)` was in C++98 and in ARM C++ before that.
Re:“Unexplainable” core dump (2011) by aw1621107 on Wednesday January 04, 2023 @12:12AM (#34242267)
Ah, I didn't know that. Thanks!
Re:“Unexplainable” core dump (2011) by taneq on Tuesday January 03, 2023 @08:47PM (#34240865)
The idea that a compiler should just silently omit anything that it’s pretty sure won’t be needed is one of the most bafflingly daft decisions I’ve ever encountered.

If a supplier sent you out-of-spec parts because they didn’t think your spec was actually important, you’d call them fraudulent, not clever.

Re:“Unexplainable” core dump (2011) by tialaramex on Tuesday January 03, 2023 @10:15PM (#34241569)
> The idea that a compiler should just silently omit anything that it’s pretty sure won’t be needed is one of the most bafflingly daft decisions I’ve ever encountered.

It's not merely "pretty sure" - the language is specifically defined this way. C++ in particular requires as a minimum standard from practitioners a perfect knowledge of the vast, complex language standard. Anything less and you'll write a program which is ill-formed or has undefined behaviour, ie your program is nonsense.

Re:“Unexplainable” core dump (2011) by BHSPitMonkey on Tuesday January 03, 2023 @11:18PM (#34242005)
I think the key word here is "silently"; It would be one thing if the compiler informed the developer it was going to skip a statement (and said "if you really want this statement kept in, add a preprocessor directive here")
Re:“Unexplainable” core dump (2011) by tialaramex on Wednesday January 04, 2023 @05:57AM (#34244193)
Redundant NULL checks happen all over the place. So the result of your revised requirement would be a huge pile of useless diagnostics. Whereupon, as with similar diagnostics C++ programmers demand a way to switch them off because they're annoying, then they're back to being annoyed that the compiler didn't do what they expected.
Re:“Unexplainable” core dump (2011) by taneq on Thursday January 05, 2023 @04:01AM (#34257582)
Yeah, that's the problem.
Re:“Unexplainable” core dump (2011) by tomasreimers on Tuesday January 03, 2023 @10:33PM (#34241687)
Are there any good tools to see how the compiler is rewriting your code? Almost a compiler coupled with a decompiler to show me the diff of what I wrote and what's happening?
Re:“Unexplainable” core dump (2011) by Smaug123 on Tuesday January 03, 2023 @06:06PM (#34239439)
(You almost certainly know this, but I presume the parent doesn't.)

GCC can be passed `-fno-delete-null-pointer-checks` precisely to prevent this. Linux uses it, see e.g. https://lkml.org/lkml/2018/4/4/601 where it's discussed.

Re:“Unexplainable” core dump (2011) by krackers on Tuesday January 03, 2023 @08:30PM (#34240719)
Wait, the compiler can do that? I thought only dereferencing a null pointer gives undefined behavior, just checking if a pointer is 0 or not shold be valid?

Yeah based on https://joelaro.wordpress.com/2015/09/30/gcc-optimization-fd... the assumption is that if the pointer was previously dereferenced, it can be assumed to be non-null, and hence all subsequent null-checks can be elided.

Based on https://news.ycombinator.com/item?id=17360772, the case in which this actually makes a difference is an address offset like

   int *n = &param->length
The compiler knows the structure so it can just inline the offset, but the optimizer considers this as being non-null.
Re:“Unexplainable” core dump (2011) by 3836293648 on Wednesday January 04, 2023 @12:51AM (#34242472)
You can check if the address is 0, but the value of the nullptr is implementation definied behaviour. You can have a system where the null pointer is 4.

And address 0 isn't too special on a machine level either. Embedded systems have valid data to read and write there.

Re:“Unexplainable” core dump (2011) by ericbarrett on Tuesday January 03, 2023 @09:01PM (#34240979)
Yes. This was upwards of 20 years ago on a DEC Alpha, and we were inspecting the compiled build in gdb using the core file from the crash.
Re:“Unexplainable” core dump (2011) by JoeAltmaier on Tuesday January 03, 2023 @04:59PM (#34238694)
Got to read the assembly to really know what happened.

E.g. if the architecture has pointers with non-address bits (modes or segments or whatever) and those bits were set yet the rest of the address was 'null', and the check was for 'all bits zero' then you could conceivably get that situation.