Comment by gpderetta

2 days ago

I'm not a compiler writer, but I don't know how you would be able to implement any optimization while allowing arbitrary pointer forging and without whole-program analysis.

It's an interesting question.

Say you're working with assembly as your medium, on a von neumann machine. Writing to parts of the code section is expected behaviour. What can you optimise in such a world? Whatever cannot be observed. Which might mean replacing instructions with sequences of the same length, or it might mean you can't work out anything at all.

C is much more restricted. The "function code" isn't there, forging pointers to the middle of a function is not a thing, nor is writing to one to change the function. Thus the dataflow is much easier, be a little careful with addresses of starts of functions and you're good.

Likewise the stack pointer is hidden - you can't index into the caller's frame - so the compiler is free to choose where to put things. You can't even index into your own frame so any variable whose address is not taken can go into a register with no further thought.

That's the point of higher level languages, broadly. You rule out forms of introspection, which allows more stuff to change.

C++ has taken this too far with the object model in my opinion but the committee disagrees.

Why? What specific optimization do you have in mind that prevents me from doing an aligned 16/32/64-byte vector load that covers the address pointed to by a valid char*?

  • Casting a char pointer to a vector pointer and doing vector loads doesn't violate provenance, although it might violate TBAA.

    Regarding provenance, consider this:

      void bar();
      int foo() {
        int * ptr = malloc(sizeof(int));
        *ptr = 10;
        bar();
        int result = *ptr;
        free(ptr);
        return result;
      }
    

    If the compiler can track the lifetime of the dynamically allocated int, it can remove the allocation and covert this function to simply

      int foo() { 
          bar();
          return 10;
      }
    

    It can't if arbitrary code (for example inside bar()) can forge pointers to that memory location. The code can seem silly, but you could end up with something similar after inlining.

    • > It can't if arbitrary code (for example inside bar()) can forge pointers to that memory location.

      Yes. It absolutely can. What are you even talking about?

      C is not the Windows Start Menu. This habit of thinking it needs to do what it thinks I might expect instead of what I told it is deeply psychotic.

      5 replies →

  • Can't reply to the sibling comment, for some reason.

    If you don't know the extents of the object pointed to by the char*, using an aligned vector load can reach outside the bounds of the object. Keeping provenance makes that undefined behavior.

    Using integer arithmetic, and pointer-to-integer/integer-to-pointer conversions would make this implementation defined, and well defined in all of the hardware platforms where an aligned vector load can never possibly fail.

    So you can't do some optimizations to functions where this happens? Great. Do it. What else?

    As for why you'd want to do this. C makes strings null-terminated, and you can't know their extents without strlen first. So how do you implement strlen? Similarly your example. Seems great until you're the one implementing malloc.

    But I'm sure "let's create undefined behavior for a libc implemented in C" is a fine goal.

    • [when there is no reply button, you need to click on the date (i.e. N minutes ago) to get the reply box]

      I think your example would fall foul of reading beyond the end of an object in addition to pointer provenance. In your case the oob read is harmless as you do not expect any meaningful values for the extra bytes, but generally the compiler would not be able to give any guarantees about the content of the additional memory (or that the memory exists in the first place).

      This specific use case could be addressed by the standard, but vectors are already out of the standard, so in practice you use whatever extension you have to use and abide to whatever additional rule the compiler requires (of course this is often underspecified). For example, on GCC simd primitives already have carve-outs for TBAA.

      FWIW, a libc implementation in practice already must rely on compiler specific, beyond the standard behaviour anyway.

      2 replies →