6 ms·
Undefined Behavior in LLVM [pdf]
- jackweirdy 11y agoint main() { int *p = (int*)malloc(sizeof(int)); int *q = (int*)realloc(p, sizeof(int)); *p = 1; *q = 2; if (p == q) printf("%d %d\n", *p, *q); } $ clang -O realloc.c ; ./a.out 1 2 That's about as eyebrow-raising as you can get!
- chrisseaton 11y agoI would have thought that the optimiser would see that p and q are aliased after the `p == q` condition and so not use the written values after that. But yeah aliasing that the compiler can't see (if it's not doing a stamp based on the equality condition) I can understand why that's undefined as what else would you want the compiler to do? Treat all memory as volatile? That would destroy performance.
- derefr 11y agoMy default answer would be "disallow usage of p after the realloc." Which is why I like Rust, I guess.
- chrisseaton 11y agoBut I believe the compiler has no knowledge of the semantics of these functions to do that.
- gpderetta 11y agoThe behaviour of those functions is defined by the standard. The compiler is aware that realloc invalidates its pointer parameter.
- masklinn 11y ago> The compiler is aware that realloc invalidates its pointer parameter. According to the standard, realloc doesn't invalidate its pointer parameter, since it can return the existing pointer (if the resizing could be done in-place) and can not touch the existing pointer (if the reallocation fails).
- gpderetta 11y agoAccording to the latest C standard draft that I could find, the semantics of realloc are that it deallocates the old object and allocates a new one. Of course, as an intended optimization, the new pointer value might have the same value as the old one, but that's not part of the semantics. The only case in which the old pointer is not invalidated is if realloc fails. As far as I know, in practice, at least with GCC, but likely clang as well, the compiler doesn't tracks the invalidation, but it simply assumes that any non null pointer returned by realloc (or any allocation function in fact) does not alias any other existing pointer in the program.
- mhogomchungu 11y agoThe pointer is invalidated only if the object is moved and the object is not always moved and hence the pointer is not always invalidated. http://pubs.opengroup.org/onlinepubs/7908799/xsh/realloc.html http://pubs.opengroup.org/onlinepubs/7908799/xsh/realloc.htm...
- derefr 11y agoDo note that I wasn't proposing to invalidate the memory-address handle contained in p; but rather to lexically invalidate p itself, such that it would no longer be legal for it to occur as an rvalue until a new value was assigned to it as an lvalue. You know, like Rust.
- shultays 11y agoIn this case it is easy to see that p is no longer valid but what about other pointers that points to the same location but not trivially visible for compilers? It won't be possible without run time checks or memory system like the one in rust.
- mhogomchungu 11y agoA pointer of type "void star" can be assigned to a pointer of any type and hence those casts are not necessary. Having them doesnt hurt,but they add clutter and shows lack of sufficient understanding of properties of pointers of type "void star". First argument to realloc takes a pointer of type "void star" and you passed in a pointer of type "int star" without casting it and this suggests you did the casts out of habit and i think its time to break the habit :-) ps: had to write "void star" because the "star" character was not showing up for some reason.
- userbinator 11y agoshows lack of sufficient understanding of properties of pointers ...or shows someone more familiar with C++ than C. Implicit casts between pointer-to-void and other pointer types are not allowed in C++.
- willvarfar 11y agoOne thing I miss from this presentation is good war story examples of unexpected or horrid things happening from UB causing the compiler to omit critical code. Here are some examples: http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-... Anyone got any more? Please, anecdotes help explain this! :D
- Kristine1975 11y agoMore examples in this paper with the title "Undefined Behavior: What Happened to My Code?": https://homes.cs.washington.edu/~akcheung/papers/apsys12.html https://homes.cs.washington.edu/~akcheung/papers/apsys12.htm...
- joosters 11y agoWhenever a story about UB comes up on hacker news, the comments usually fill with both the predictable "Don't use C" comments and also a lot of ill-informed ranting about how stupid all this UB is. For the first group of commenters, please don't bother posting again, just search HN and you can find a lifetime's supply of C love/hate arguments. For the second group of people, I'd recommend reading two fantastic guides to UB: http://blog.regehr.org/archives/213 http://blog.regehr.org/archives/213 http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-... These should be enough to explain why UB is trickier than you think...
- lmm 11y agoPlenty of topics are discussed repeatedly here. I won't stop telling people to quit C any more than I'd stop telling them to quit smoking.
- kleiba 11y ago...or telling them to stop posting on HN.
- derefr 11y agoBut why type it yourself? Someone's probably argued it better than you ever can, and you can just link to that. It's not like you'll come up with any novel arguments at this point, or even novel presentations of existing arguments. It's all been said, and said well.
- pjmlp 11y agoI have been doing it since 1992, surely won't stop now. :)
- dmm 11y agoI'm writing some tools to capture video streams, doing demuxing/muxing, decoding, etc. All of the libraries that do these things are written in C. Using another language, aside from c++, would involve using some kind of ffi mapping, made by me or someone else. So now I not only have to understand the semantics of c and the libraries I'm using I have to understand the semantics of the ffi mapping. Using the ffmpeg libraries I've had to dig into the source to figure out what it's doing[0]. If I'm using python(or whatever) now I have to do that and figure out what the mapping is doing. And I'm still not safe from segfaults and memory leaks, they're just harder to find and understand. Soft realtime is trivial in C. If I'm capturing a video stream at 10fps I have about 100ms to do everything to handle a frame. This is very easy to do in c. If I'm using a garbage collected language I have 100ms-X, where X is the longest garbage collection pause. I tried doing a similar project in java and found it very difficult. Using any library, for example to output a jpeg, created tons of garbage, resulting in long gc pauses. I tried a bunch of different -XXasdf options that helped but to really solving the problem would involve reimplementing a bunch of stuff to reuse buffers and avoid garbage. I freely admit this may be because of my ignorance of how to do it correctly but again it's trivial to avoid these pauses in c. So I ask sincerely what should I do? I'll gladly throw out the years of painfully acquired c knowledge if you have a better solution for me today. [0] I wonder why avformat_find_stream_info is detecting the from framerate? Good luck :) http://ffmpeg.org/doxygen/trunk/libavformat_2utils_8c_source.html#l03123 http://ffmpeg.org/doxygen/trunk/libavformat_2utils_8c_source...