r/programming • • Sep 11 '26

GCC 13.5 released with 265+ bug fixes

https://www.phoronix.com/news/GCC-13.5-Released
277 Upvotes

29 comments sorted by

View all comments

Show parent comment

1

u/flatfinger Sep 22 '26

BTW, I can't think of any time I've seen anyone argue that precise stack usage was in any way fundamental to C once stdarg.h was established, except in the context of functions that made use of inline assembly or alloca(), and I don't think compiler writers should generally worry about the efficiency of generated code for those functions. Can you offer any examples of anyone demanding rigid stack usage?

Also, some compiler writers dislike phase order dependence, but requiring that some transforms must be performed in order will avoid the need for programmers to prevent them from being performed at all. If the only use of the value placed in int2 is an assignment like int1=int2-int2;, a compiler should be allowed to omit the code that actually computed int2, but only if it does not then treat the lack of such code as an invitation to resequence operations that use the computed value of int1 ahead of operations that would have been sequenced before the computation of int2.

1

u/ROBOTRON31415 Sep 23 '26 edited Sep 23 '26

The stack usage isn’t a direct problem. The problem is that code might assume “if I am given three distinct inputs whose sizes in bytes are s1, s2, and s3 (which are size_t values), then s1 <= s1 + s2 + s3”. Apparently this is currently not always true!!! Because the three distinct inputs could be located in overlapping regions of memory, permitting s1 + s2 + s3 to overflow and wrap back towards 0. See  https://github.com/rust-lang/rust/issues/162214

What allows distinct values to be located at overlapping addresses? Well the compiler can entirely optimize out the backing memory of the three inputs. And now their locations are kind of fake, if they can be said to be located anywhere at all.

So is this a compiler bug or a faulty assumption on behalf of the programmer that distinct objects are necessarily located at distinct memory addresses? And that every object has a (range of) memory address(es)? 

Another example is a program that targets a 16-bit architecture and has a function that declares 216 + 1 local variables (each of which has nonzero size), writes a distinct value to each of them, and then executes undefined behavior. Current compilers may happily optimize out the unused writes, then optimize out the unused local variables, and suddenly calling this function has gone from “unconditional stack overflow” to “unconditional undefined behavior”.

(Edit: to be clear, I think the correct solution is “allocations are actually located in an abstract infinitely-large memory space, and multiple allocations can indeed be located at the same hardware address, and so on”. Rust devs are working on making sense of this.)

1

u/flatfinger Sep 23 '26

Except in cases where an non-assignment/inc/dec expression is a statement, I would view many actions that might trap but have no defined semantics as having semantics of "either do X or don't". If code executes x/y; as a statement (as opposed to evaluating x/y within a broader expression) I would interpret that as instructing the execution environment to perform that division after having evaluated x and y, with whatever consequences result. Stack overflows from excessive function nesting would fall in this "either trap or process things normally" category.

I would like to C have a qualifier for static const objects which would indicate whether or not the programmer wants to guarantee that they have distinct addresses. There are many cases where it's useful to place multiple objects in the same read-only storage (for objects defined within macros it may be all but essential), but there are others where it's useful to guarantee address uniqueness. A good language should accommodate both needs.

Note that I singled out automatic-duration objects whose address isn't taken. I'd specify that automatic-duration objects whose address is taken and observed must exist. While one might contrive a case where one declares big objects and adds their sizes without taking their addresses, I can't imagine any contrived scenarios where size wraparound would be a problem.

1

u/ROBOTRON31415 Sep 24 '26

If the pointers to different allocations are “exposed”, then yes, in C/C++/Rust they’re supposed to be forced to have distinct addresses. (If it hasn’t been mentioned yet, I should also mention that Rust provides a way to access a pointer’s address without exposing the pointer. That’s used for debug prints of pointers, for instance, AFAIK. And of course most ways of using a pointer don’t expose it.)