170 points · by luu · 8y ago · 82 comments
Edit: a good explanation of that macro is at http://lists.linuxcoding.com/kernel/2006-q3/msg17979.html :
"The reason for it is that gcc assumes that if you add something on to the address of a symbol, the resulting address is still inside the bounds of the symbol, and do optimizations based on that. The RELOC_HIDE macro is designed to prevent gcc knowing that the resulting pointer is obtained by adding an offset to the address of a symbol. As far as gcc knows, the resulting pointer could point to anything."
How can one C object span two symbols in the first place for this assumption to be invalid?
Because Linux isn’t written in C. It’s written in C, assembler, and linker script (.lds). In both assembly and linker script, one can lay out multiple symbols with a defined relationship. Unfortunately, C doesn’t have a way to say “give me a pointer to the object 10 bytes past the address p. Yes, I know it’s valid and it is not the same object as p.”
Right - I'm saying that if they need this macro then their program is not a defined C program in the first place. They should fix that, rather than trying to use a macro to work around the problem. The macro may break in the future if GCC adds other optimisations.
They call it a 'miscompilation' - how can you 'miscompile' something which is undefined!
char symbolA[10];
char symbolB[10];
symbolA[15] = 0; // actually writes to symbolB> gcc assumes that if you add something on to the address of a symbol, the resulting address is still inside the bounds of the symbol
I'm asking - how can you write a valid C program which adds something on to the address of a valid symbol and get something outside the bounds of the symbol?
Maybe we can one day get a `--std=simple-world` flag that forgoes a few optimizations and makes UB code do the "obvious" thing.
One way to implement my idea would be as pragmas that tell the compiler "don't optimize away this null check", "assume this variable can alias", "just emit the obvious ASM for memory access" or "put memory barriers around this code".
I don't see how this kind of "stupid memory access" would make e.g. SROA impossible. If I just access the memory around "this", sure, I would not see the layout I'd naively expect. But that's not what I want anyway. I want to see the memory as it is actually laid out.
Its more like we had this window of time when pointers were not weird and got too used to it.
It's strange that we put so much effort into learning all this arcane knowledge and now we're supposed to unlearn it...
I also don’t understand a reason behind dragging all that legacy into current standards. Does a real application of it make at least 0.1% of all usage? You cannot even buy a chip that implements segments, tagged pointers, etc etc. at least they could make a special mode where you can span across symbol whatever it means or see a pointer as a cpu sees it.
All this can be solved with simple ptr_untag(p), ptr_span(expr) and similar constructs, but instead we have resort to outsmarting the specific compiler logic or introducing ourself with complicated type systems. That went insane. Personally I just want my bytes in linear address space and a way to tell which bytes point to which and which do not. I liked asm and then C, they were two of my first three languages, but what C became today is just a horrible mess.
I do personally wish the C standard was changed a bit to have less undefined behavior, leaning towards getting programmer intent correct and not worrying (as much) about getting every possible optimization into the generated code.
Agreed. There are some particularly egregious ones where the behavior should at the very least be implementation defined.
TFA argues that converting to integers first sidesteps the issue but I'm not convinced (although I'm not a C standard lawyer). Surely if you convert a pointer to an integer, add an offset that's greater than the size of the original object and then convert that to a pointer you trigger UB? How else could this be defined? You can't really expect the compiler to figure out what you're doing when you're casting "random" integers as pointers.
Also the article mentions Rust in its introduction but in Rust anything surrounding pointers (including pointer arithmetics) is unsafe so I don't think there's any conflict whatsoever here.
https://internals.rust-lang.org/t/pointers-are-complicated-o...
"This summer, I will again work (amongst other things) on a “memory model” for Rust/MIR. However, before I can talk about the ideas I have for this year, I have to finally take the time and dispel the myth that “pointers are simple: they are just integers”. Both parts of this statement are false, at least in languages with unsafe features like Rust or C: Pointers are neither simple nor (just) integers.
I also want to define a piece of the memory model that has to be fixed before we can even talk about some of the more complex parts: Just what is the data that is stored in memory? It is organized in bytes, the minimal addressable unit and the smallest piece that can be accessed (at least on most platforms), but what are the possible values of a byte? Again, it turns out “it’s just an 8-bit integer” does not actually work as the answer."
Ralf Jung is a coauthor of the "What’s the difference between an integer and a pointer?" paper.
So, in order to generate faster programs, compilers are getting more and more agressive about reusing data that has already been copied into registers, hiding under the shadows of 'undefined behavior.' The standard doesn't indicate a defined way to mess with this data so I'll just keep using the copy I have.
Writing through an untracked pointer probably convinced the compiler to dump its register caches.
C used to be thought of as kind of a portable assembly language, but when the compiler is doing all these optimizations, there's a lot of room for clashing.
I’m a bit confused by this statement. I don’t know the C & C++ standards, but isn’t pointer arithmetic defined and allowed within the bounds of the heap allocation of the base pointer? Like, on an array for example, aren’t pointers required to “follow the same rules as pointer-sized integer values”, as long as you’re not indexing outside your array allocation?
Wouldn’t the issue here be more clearly stated as “don’t use pointer arithmetic to index outside the bounds of the base pointer’s allocation” rather than “never use pointer artithmetic”?
Dunno what it means for stack pointers though, probably the same except now you've made it so they can't be registers.
The fact that an Iterator, a pointer and an integer all tend to be different ways of looking at same integer value is more or less an implementation detail.
*x will be 7 in the end.
Mac:tmp $ clang -O3 mem1d.c ; ./a.out
diff = -96
x = 0x7f9f4fc00300, *x = 7
y = 0x7f9f4fc00360, *y = 5
p = 0x7f9f4fc00300, *p = 7
Mac:tmp $ diff mem1d.c mem2d.c
13c13
< int *p = (int *)(yi + diff);
---
> int *p = (int *)(yi - 96);
Mac:tmp $ clang -O3 mem2d.c ; ./a.out
diff = -96
x = 0x7fc2b5c00300, *x = 7
y = 0x7fc2b5c00360, *y = 5
p = 0x7fc2b5c00300, *p = 7
Mac:tmp$ clang --version
Apple LLVM version 10.0.0 (clang-1000.11.45.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posixDoesn't this depends on which allocations have been freed?