28 points · by goranmoomin · 6y ago · 55 comments
Let me give an example here. Consider these two functions:
void a() {
int x = 5;
return x;
}
void b() {
return 5;
}
In a simplistic, pointers-are-integer model, these two functions are not equivalent, and it is illegal for any compiler to transform the first into the second. This is because declaring the variable x means it must have some associated storage, which can be accessed by some unknown integer name. Any pointer which points to that integer value can then observe its value, and deleting the store to that temporary value is a potentially observable change to a legal program.To permit this basic optimization, you need to have to some sort of rule that allows you to reason that a value whose address is never taken can never be legally accessed by a pointer. To actually effect this rule in semantics, you now have to start tracking extra metadata about what pointers can and cannot access... which is exactly the kind of model that's being criticized.
I've not read the C spec, so I may be terribly wrong here, but I'm inclined to disagree: x is not accessible outside of a() and cannot be read or modified by another part of the program, and so a compiler would be perfectly justified in not bothering to allocate storage for it.
> To permit this basic optimization, you need to have to some sort of rule that allows you to reason that a value whose address is never taken can never be legally accessed by a pointer.
Essentially, I'm contending that not only is the address of x never taken, it never can be taken, and therefore the optimization is safe.
(In practice, if x did happen to have stack space allocated for it and you knew what location it ought to have relative to the stack pointer, you could modify x. But insofar as I'm aware, C does not guarantee many things about the layout of memory or a particular calling convention anyway, and so you shouldn't rely on this. And that isn't changed by one's model of pointers.)
The C standard also doesn't define the memory layout of the stack, so there is no standard way to acquire the address of that variable. It's easy enough to see that compilers do not actually create storage for it: https://godbolt.org/z/cfE9o5
In C11 there is a thread aware memory model, but still no standard way to access that location, so compilers will assume it doesn't need to be created. https://godbolt.org/z/KTz9sT
Pointers being integers does not mean that writing garbage to any random integer you come up with has well-defined semantics. In any case, your example could be made much better by pointing out that any program could simply overwrite the stack return value; that has to be UB.
> To permit this basic optimization, you need to have to some sort of rule that allows you to reason that a value whose address is never taken can never be legally accessed by a pointer
This is true
>To actually effect this rule in semantics, you now have to start tracking extra metadata about what pointers can and cannot access
If you aren't writing to random values, you don't really need to track anything. foo(bar *) doesn't need to track anything, it's presumably part of the implicit contract that you allow foo to write to the passed in object.
The OP's viewpoint, which seems to be a fairly popular one, seems to be something like “why can't C be as intuitive as it was in the 1970's, didn't it work fine then?” Well, it probably still does work if you're happy with the level of optimisation a 1970's C compiler would give you (-O0 maybe?), and that's a valid choice! But compilers didn't get stricter about undefined behaviour for nothing, it's key to making those “zero-cost abstractions” actually have near-zero cost.
Isn't that arguably because Rust has even stricter aliasing rules? Not only are you not allowed to have differently-typed mutable references to the same memory, you (approximately) aren't allowed to have multiple mutable references to the same memory, period, even if they are the same type.
With a() as written, there's no legitimate way to obtain the address of x, so there is no such pointer.
Am I wrong about this?
Even if you modified the code of a() to pass out the address of x to a caller, they can't safely use that pointer and the actual behavior is undefined. x does not exist outside of a call to a().
Unless you return a reference to an integer that's been malloc'd.
In reality two functions are never and can never been equivalent--they can only be observationally equivalent.
> you need to have to some sort of rule that...whose address is never taken can never be legally accessed by a pointer.
Such a "rule" does exist. If the above is a C program, said C program must have undefined behavior (read: other pointer bugs) to observe x's value.
Programs with pointer bugs can do insanely weird things because they break all the abstractions of programming language and expose the memory layout.
The point of the article is that compilers shouldn't try to exploit undefined behavior in pursuit of more performance. The fact that you can observe optimization means that abstractions of the languages are broken. C and C++ are essentially the only languages in existence where rampant observation of optimizations is considered not a bug, but a feature.
I would really recommend reading the Ertl paper [http://www.complang.tuwien.ac.at/kps2015/proceedings/KPS_201...]. It covers this really well.
Honestly, the actual pointer model of C is probably easier to understand than a everything-can-randomly-change-anything pointers-are-integers model. Undefined behavior gets a bit of a bad rap, and that's arguably largely because of unnecessarily gratuitous usage of it (signed integer overflow and strict aliasing being the biggies). But undefined behavior is actually a contract where the programmer promises not to do certain things. For pointers, the contract boils down to the following:
* Do not violate strict aliasing (I'd call this an unnecessary rule, and it can be disabled in most compilers).
* Do not read memory you haven't initialized.
* Do not use memory after it's deallocated.
* Do not look outside the bounds of the object you are given.
* Do not look at memory you haven't been told about.
There is very little reason to ever need to violate these rules. In fact, violating these rules tends to result in hard-to-reason semantics: suddenly, people can read things they shouldn't be able to, or decide to do things like execute random code.
Your interpretation seems uncharitable. I imagine that you've interpreted the author's words literally, as somebody that has expert knowledge of compiler internals. Could the author really have meant to propose an "everything-can-randomly-change-anything pointers-are-integers" model?
Do not even try to look; computing a pointer that points outside the object it was created from is undefined.
Don't know about the language spec, but compilers do this transform all the time.
It is, in fact, quite infuriating if you are trying to use a debugger on the code to do a breakpoint on data change (memory change) and you can't because the value is solely held in a register.
It also doesn't help that the author is writing from a C++ perspective and not a C perspective.
That's great, but then you're not writing C anymore. When you write C (or indeed, almost any language in use today) you are writing code against an abstract machine, not against real hardware.
Pointers are integers on (most) real hardware. They are indisputably not integers in the C abstract machine.
The reason for this abstract machine existing is not to enable optimizations, it's to allow the code to be portable. If there was no abstract machine, you would have to reason about the correctness of your code for each target architecture independently. With an abstract machine, you can reason that your code is correct on the abstract machine, and the compiler can do the hard work of translating that to each target architecture in a way that preserves well-defined behaviour.
Now, given that the abstract machine exists, you could ask why it is so complicated, and the answer this time is for optimizations, and also to allow the translation to the target architecture to introduce as little overhead as possible.
I'm curious. Can you elaborate on "most"? The closest thing I know of to an exception is ARM's memory tagging extension stuff, [1] and I'm not sure if that qualifies because it looks like the actual checking of the tag is done by compiler-emitted assembly rather than automatically by the hardware. [2] I suppose it at least violates the idea that casting the pointer to an integer type doesn't change the value and still allows you to sort addresses via a simple <.
[1] https://security.googleblog.com/2019/08/adopting-arm-memory-...
[2] https://clang.llvm.org/docs/HardwareAssistedAddressSanitizer...
I'd argue that if you don't understand that you probably shouldn't be coding in C (I'll let others argue about C++)
(The usual model used by compilers is that pointers are not integers, but a pointer becomes an integer when it is casted to an integer. This prevents majority of pointers that are not casted to integers from escaping, which is important.)
I will not be holding my breath waiting for the committee to accept this though. The chance of this being accepted seems really low, as it has serious consequences for the meaning of the large amount of existing C code.
Hold up - there's also a tradeoff in assumptions you can make about well behaving code. It's _also_ an optimization in reading and internalizing the code.
It's worth adding that the compilers themselves aren't perfect - you can use LLVM's code to try and predict how your loop will perform (LLVM-MCA) and last time I checked it wasn't amazingly accurate.
The only thing that you can predict about compiled C code is the instructions in the C abstract machine, because that’s what C targets.