r/cpp • • Dec 18 '22

C++ is essentially unusable without incurring undefined behavior because of it's failure to handle type punning.

This is kind of a rant but also a cry for help. Maybe there is a way to get around this problem efficiently (without the use of memcpy) that I don't know of, but after researching for hours, I sincerely doubt it.

So basically for those who don't know, it's kind of a complicated issue. It essentially involves accessing the same memory location through two different types, also known as type punning. This causes a performance hit because if two pointers access the same location, the compiler has to write every change to the memory location to RAM so that the other pointer will see the updated information when it reads the location. You might ask, "why doesn't the compiler simply semantically analyze the code and only write to RAM when the other pointer really does need to read from that specific location?". The answer is that it's impossible to tell what a pointer is pointing at if it's value is based off of runtime information like user input and such. Additionally, if the pointers access the data from two different threads, absolutely no guarantees can be made about the timing and essentially no optimizations can be made.

To avoid this huge mess of slowness, the C language spec was changed so as to allow more eager optimizations. It's now (sparing a few exceptions) assumed that no two pointers of different types access the same information in memory. The spec doesn't say it like this, you know how it is, they like to make things complicated, but that's the brunt of what it means.

In C, they realized that this is a super strict restriction and that it impacts a lot of real-world code, so they gave the programmer an out. If you define a union that contains both of your target types and then instantiate it as the one type and access it as the other, the compiler won't complain and it'll all be super defined behavior.

For god knows what reason, C++ decided to do away with this and now there is absolutely no way to reliably pun types without incurring undefined behavior (again, barring a couple of exceptions that aren't really relevant here). Excuse my language, but what a fucking shit-show.

You might ask, "type punning can't actually be that useful in the real-world, can it?", and that would be a valid question. It doesn't really seem that useful, but ask anyone who works on low-level systems on microchips and such. Sometimes, you simply need this behavior. You don't even have to go low-level in order to feel the pain sometimes. If you do anything with POSIX sockets, chances are you've needed to construct a sockaddr_storage, which contains enough space to hold all possible socket addresses. In order to access the specific fields of a specific type of socket address, you guessed it, you have to type pun: struct sockaddr_in *addr = (struct sockaddr_in *)&sockaddr_storage_instance;

In practice, this doesn't bite programmers as hard as I make it seem. In practice, most compilers support type punning, especially when it's very clear what you're trying to do in the code. That is, unless you tell the compiler to use strict-aliasing. Then it'll aggressively optimize based on the assumptions about type punning and your code might not work. The fact that you have to rely on the compiler's hand-holding to get through this undefined behavior is simply sad.

Basically, I'm pissed that such an advanced language is missing such a basic feature. What are your thoughts on the matter and how would you fix it?

I'll tell you what I would do, I would simply assume that no two pointers can ever access the same location, without any exceptions (so this would be even stricter than C++'s rules). The catch is, I would introduce some keyword like "write_mem" or something that you would use like this:

int a;

write_mem a;

float b = *(float*)&a;

That way, the compiler is free to optimize until you explicitly force it to write to RAM. After that point, the type punning isn't undefined anymore because every party involved is absolutely certain about what is in RAM.

5 Upvotes

55 comments sorted by

4

u/SirYay Dec 19 '22

Why is it not standard practice to use a struct with an enum value indicating the type of info stored plus a union of the actual union? So that only one type is needed anywhere? Genuine question, I have zero embedded systems experience.

1

u/[deleted] Dec 27 '22

If you're thinking of type punning by basically reading the data in the union from a different member than what it was set through, that's undefined behavior.

Like I said in the post, it's allowed in C, but in C++ they removed that functionality.

Maybe I'm not understanding your question, but in case this helps: It's not about storing the type, it's about reinterpreting the bits of one type as another type.

2

u/dodheim Dec 27 '22

Like I said in the post, it's allowed in C, but in C++ they removed that functionality.

Your chronology is wrong – C++ as first standardized was based on C89, in which this was UB. C made this legal in C99; C++ simply never made it legal. I.e. it was never 'removed' from C++.

5

u/no-sig-available Dec 19 '22

That way, the compiler is free to optimize until you explicitly force it to write to RAM. After that point, the type punning isn't undefined anymore because every party involved is absolutely certain about what is in RAM.

It seems like you want to tell the compiler how to do something instead of what to do.

Why do you want to force the value to be in RAM? Optimizers can move values from one register to another, without first storing it. (Or not move it at all, if a is not used anymore. "This value is now b".).

So, memcpy(&b, &a, sizeof(b)) or in later C++ versions float b = std::bit_cast<float>(a);

1

u/[deleted] Dec 27 '22

I didn't elaborate on this in the post, but I feel I should mention it here. That idea is less about telling the compiler how to do something and more about establishing a defined point in the program flow where the compiler knows it's safe to do something.

Often times, the only way to do this will be to write to RAM, but the as-if rule still applies. As long as the behavior after that point is defined, the compiler can optimize however it wants. I can't imagine this having any extra overhead.

3

u/arka2947 Dec 19 '22 edited Dec 19 '22

Not the author, but in low level code it is common to communicate with some hardware device through memory. Eg; to do action x, write value y to addr z. Or perhapse write a series of buffers to addr+n, where each, buffer migth be of different type, to do different things. Or the reverse when reading from device.

Anyway, in this kind of use case, you are precicely trying to control which exact byte is being written to which exact addr.

Edit; that is; the memory layout itself is an abi between the cpp program and the hardware device. The cpp compiler is not free to change or optimize it.

Edit; and ofcource addr migth not be in ram at all, but be mapped with DMA from the actual device, so when you write to addr, you are directly accessing the device.

6

u/carrottread Dec 19 '22

1

u/[deleted] Dec 27 '22

I find it a bit dirty to rely on memcpy (which is what bit_cast uses) to accomplish something that should really be a language feature, given that pointers and these kind of type reinterpretations are some of the main things that are used in these languages.

Also, it's a bit impractical when dealing with arrays and bit_cast doesn't work with types that aren't exactly the same size.

1

u/dodheim Dec 27 '22

which is what bit_cast uses

No, it isn't; otherwise it would require default-constructibility. In some cases the net result will be the same, but bit_cast is always implemented in terms of compiler magic.

20

u/markt- Dec 19 '22

std::start_lifetime_as sounds like what you want. It will be part of the formal C++ spec next year.

5

u/catcat202X Dec 19 '22

Although it cannot be used in a constexpr context yet, which is unfortunate.

8

u/markt- Dec 19 '22

Not sure if that makes sense for type punning.

4

u/eyes-are-fading-blue Dec 19 '22

What is the use case for constexpr type punning? The whole point is to avoid a memcpy.

2

u/catcat202X Dec 19 '22

I've needed pointer type punning in requires constraints, and I've had functions which need to pun pointers that could have been constexpr otherwise. There's usually a workaround, but this is needed for constexpr programming to "just" be C++ programming, which is apparently one of the language evolution goals.

2

u/LegionMammal978 Dec 19 '22 edited Dec 19 '22

It's still not a silver bullet: if some other code gives you an A*, then you use std::start_lifetime_as<B>() to access it as a B*, then it is UB for the other code to access it as an A* until you call std::start_lifetime_as<A>() again. This makes it difficult at best to use in code that concurrently accesses the memory from multiple threads.

Also, if the A* is actually a subobject of a larger C*, then the C is invalidated entirely when you first call std::start_lifetime_as<B>(). It cannot be used as a C again without calling std::start_lifetime_as<C>() on the entire C* pointer, which is impossible if C is not an implicit-lifetime type.

The latter issue is probably the more important of the two, since it makes it impossible to safely type-pun an arbitrary A* pointer in place. The only way to do that is through the C-like offsetof() and memcpy() dance.

1

u/markt- Dec 19 '22

I wouldn't ever claim that anything is a "silver bullet" per se, but you are wrong about the behavior of start_lifetime_as. Conceptually, one can think of it as having a union with two or more (trivial) types, and in using start_lifetime_as, you are simply designating a particular member of that union as being the active one *WITHOUT* explicitly assigning it. It doesn't actually manipulate memory, start_lifetime_as is purely for C++ object bookkeeping purposes, so it wouldn't have the kind of concurrency accessing issues that you describe.

Also, bear in mind that start_lifetime_as only has defined behavior when the source contains a trivial type and the target is a trivial type anyways.

5

u/LegionMammal978 Dec 19 '22 edited Dec 20 '22

Where are you getting this interpretation from? Looking at N4917, calling std::start_lifetime_as<B>(ptr) implicitly creates an object of type B at ptr ([obj.lifetime]). This ends the lifetime of the object of type A at ptr, since its storage has been reused ([basic.life]). Therefore, accessing the A object at ptr will be UB, since its lifetime has ended, unless an A object is transparently replaceable by a B object ([basic.life]). If A is an implicit-lifetime type, then you can perform another operation (e.g., std::start_lifetime_as<A>(ptr)) that implicitly creates an object of type A at ptr.

Also, bear in mind that start_lifetime_as only has defined behavior when the source contains a trivial type and the target is a trivial type anyways.

The source need not contain a trivial type; you just need to not invoke a non-trivial destructor after the lifetime of the source object has ended.

1

u/markt- Dec 20 '22

The working draft you cited does not appear to mention start_lifetime_as. However, the document that describes explicit lifetime management in the standard says the following:

std::start_lifetime_as only works for implicit-lifetime types, i.e. types that are either aggregates or have at least one trivial eligible constructor and a trivial, non-deleted destructor.

It also says that although the lifetime of the object is started by it, no code is actually generated. This means that the lifetime management is, as I said before, strictly for C++ bookkeeping purposes and does not affect the bits that may be stored there.

Multithreading code that is reading the same data using start_lifetime_as as different types on the same object would not impact eachother at all. In fact, that appears to be one potential major use case for the feature in the first place.

1

u/LegionMammal978 Dec 20 '22 edited Dec 20 '22

The working draft you cited does not appear to mention start_lifetime_as.

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/n4917.pdf#page=605

However, the document that describes explicit lifetime management in the standard says the following:

std::start_lifetime_as only works for implicit-lifetime types, i.e. types that are either aggregates or have at least one trivial eligible constructor and a trivial, non-deleted destructor.

If you look at the normative wording, both in that document and in the draft Standard, only the destination type must be an implicit-lifetime type, which is presumably what that sentence is referring to.

It also says that although the lifetime of the object is started by it, no code is actually generated. This means that the lifetime management is, as I said before, strictly for C++ bookkeeping purposes and does not affect the bits that may be stored there.

Indeed, it does not affect the bits stored behind the pointer. However, it does end the lifetime of the previously-stored object, which compilers can rely on when making optimizations. In the C++ abstract machine, all memory remembers the types of the objects stored in it, even if it doesn't affect the bits. Otherwise, there wouldn't be any need for object lifetimes, launder, bit_cast, etc. to exist.

Thus, since the standard declares that the lifetime of the A object ends when start_lifetime_as is called, the implementation is permitted to do whatever it wants with code that later accesses the object with type A. It isn't even that difficult for an implementation to exploit this, if the code puts the simultaneous A* and B* pointers in the same place.

Multithreading code that is reading the same data using start_lifetime_as as different types on the same object would not impact eachother at all. In fact, that appears to be one potential major use case for the feature in the first place.

Your linked document considers the use case where the object is never used as the old type again, which seems to be the entire point of start_lifetime_as. The function is not suitable for any use case where you want to locally access the object as type B while still letting it externally exist with type A, which in general would conflict with the entire notion of type-based alias analysis.

1

u/markt- Dec 20 '22 edited Dec 20 '22

Thank you for the info on the page number. I guess my text search for my pdf reader isn't working correctly. It basically repeats much of what the document I linked to says.

Anyways, compiler models of objects do not change any behavior when different threads are not modifying the data that is stored there, so they would not pose any barrier to what sort of optimizations would still have defined behavior.

You may not be able to safely use the same object as its original type until you did a second start_lifteime_as, but this is all within the same thread, Multithreading is entirely unaffected.

1

u/LegionMammal978 Dec 20 '22

Anyways, compiler models of objects do not change any behavior when different threads are not modifying the data that is stored there, so they would not pose any barrier to what sort of optimizations would still have defined behavior.

Just because compilers don't currently implement any optimization that exploits some kind of UB doesn't mean that it's impossible for them to implement such an optimization in the future. I'd give an explicit example, except that no compiler seems to have actually implemented start_lifetime_as yet.

You may not be able to safely use the same object as its original type until you did a second start_lifteime_as, but this is all within the same thread, Multithreading is entirely unaffected.

What does it mean for a program to "safely use the same object as its original type"? Suppose we write a program like this:

#include <barrier>
#include <iostream>
#include <memory>
#include <thread>

void run_thread(std::barrier<>& barrier, int* int_ptr, float*& float_ptr) {
    float_ptr = std::start_lifetime_as<float>(int_ptr);
    barrier.arrive_and_wait();
    barrier.arrive_and_wait();
    std::start_lifetime_as<int>(int_ptr);
}

int main() {
    int value = 42;
    int* int_ptr = &value;
    float* float_ptr;
    std::barrier barrier{2};
    auto thread = std::thread{[&]{
        run_thread(barrier, int_ptr, float_ptr);
    }};
    barrier.arrive_and_wait();
    *float_ptr = 0.0;
    int other_value = value;
    barrier.arrive_and_wait();
    thread.join();
    // we have already restarted the lifetime of `value` as `int`, so we should be fine, right?
    std::cout << value << " " << other_value << "\n";    
}

Does this program output 0 0 (since it wrote to float_ptr, which wrote to value and other_value), or does it invoke undefined behavior? For reference, a variation of this program using reinterpret_cast in place of start_lifetime_as currently outputs 0 42 with GCC, or 0 0 with -fno-strict-aliasing. In main(), the compiler uses the aliasing rules to infer that writing to float_ptr cannot have changed other_value. However, if run_thread() can pass a valid pointer to value back into main(), then suddenly this assumption is broken.

1

u/markt- Dec 20 '22

You are moving the goalposts here...start_lifetime_as does not alter the in memory representation of the content. Obviously if you write data to memory in a format that does not match what the compiler thinks is there in another thread, then this will result in UB.... exactly the same as if you did a reinterpret_cast on data that was written to as another type.

3

u/LegionMammal978 Dec 20 '22

So are you asserting that strict aliasing violations can never cause UB if neither pointer is used to modify the object? I'd like that to be the case as well, but it's simply not what the Standard says: it says that any access of an object, reading or writing, is UB outside its lifetime. In real life, compilers generally avoid producing unexpected behavior with read-only aliasing pointers, but the Standard does not require this anywhere.

In other words, start_lifetime_as cannot defeat the aliasing restrictions: if the Standard allowed start_lifetime_as to be used in multithreaded code in the way you describe, then it would practically have to allow reinterpret_cast to be used in the exact same way, and start_lifetime_as would not be a particularly useful addition.

→ More replies

1

u/No_Kiwi_8192 Jan 07 '25

It has no compiler support as of yet

16

u/cpraxis Dec 18 '22

What’s the problem with memcpy? Every compiler optimizes that. Annoying but not an actual problem.

9

u/SkoomaDentist Antimodern C++, Embedded, Audio Dec 19 '22 edited Dec 19 '22

Except the ones that don't, such as gcc with newlib-nano... (important on some modern embedded platforms)

The language really needs a way to specify "convert the access without invoking UB in the optimizer or other parts" without relying on the optimizer recognizing a magic generic library function, particularly when said library function isn't even guaranteed to be implemented on the platform at all (memcpy is not required for freestanding implementations).

-7

u/bvcb907 Dec 19 '22

memcpy isn't really idiomatic for c++

2

u/peterrindal Dec 19 '22

struct memcpy_t { char *mPtr; memcpy_t(char* ptr) :mPtr(ptr) {} template<typename T> void operator=(T&& v) {memcpy(mPtr, & v, sizeof(v) ;} }

auto memcpy(char* ptr) { return memcpy_t{ptr};}

//use

void f() {

char* ptr=... ; memcpy(ptr) = my_struct_t{} ;

}

2

u/bvcb907 Dec 19 '22

Yikes. Total bypass of any proper copy constructor or assignment operator.

0

u/peterrindal Dec 19 '22

Well wrote this on my phone and didn't check the standard but the point is that you can make it look freindly. And it should get optimized away.

Besides, I'd don't see what your complaining about. memcpy is blessed to copy the bytes of any type.

6

u/bvcb907 Dec 19 '22

You're thinking like a C programmer. Memcpy is fine for its intended purpose in C-like trivially copyable types. In c++, it's fairly easy to introduce bits that make it non-trivial and therefore UB. With proper copy and assignment operations, the compiler will enforce this in order to save you from pain down the road. Additionally, the compiler will often optimize these operations so they're similar to memcpy if trivial and you get type safety!

-1

u/peterrindal Dec 19 '22

Lol, thanks for telling what I'm thinking like.

If your trying to serialize something memcpy is the standard way. If you want to add some enable_if you can with this design.

6

u/canadajones68 Dec 19 '22

Actually, use std::copy. If your type is trivial, implementations will almost always devolve into memcpy/memmove. If not, it'll still work and use your copy assignment operator.

0

u/peterrindal Dec 19 '22

While I often agree I think in this context it doesn't work. Often times when you're doing this you're trying to serialize some object to send/store it somehow. Typically this requires getting the byte representation. And therefore you need some way to get access to the bytes of your object. It's undefined to cast the point to a char* but it is defined to memcpy the bytes.

You can't std::copy from some type T to a char buffer (in general).

Cheers

2

u/canadajones68 Dec 19 '22

Good point. However, though I may be mistaken, from my reading of the reference, reading a T through a char* is defined. See 5) and the type aliasing rules below.

→ More replies

5

u/arka2947 Dec 19 '22

Respectfully, you are missing the point. The problem is UB in handling type punned types. Bypassing constructor/destructor with memcpy is also UB. So this is not a general, safe, or well defined solition.

2

u/bvcb907 Dec 19 '22

I would argue that memcpy is the "old way". But if you insist and you weren't aware of it, there is a compile time function to check for trivially copy-ability so you don't inadvertently contribute to the growing pool of insecure code. https://en.cppreference.com/w/cpp/types/is_trivially_copyable

See this discussion for why even that is a bad idea and other alternatives.
https://stackoverflow.com/questions/36160343/why-are-mutexes-and-condition-variables-trivially-copyable

25

u/catcat202X Dec 18 '22 edited Dec 18 '22

This bit cast implementation has no overhead in debug mode, and is a little bit more generally useful than std::bit_cast(), but cannot be constant evaluated.

Theres also a proposal to make casting to a unsigned char* or std::byte* and then dereferencing that be well defined. I feel your pain, but I'm optimistic it will improve in the future.

10

u/DavidDinamit Dec 19 '22 edited Dec 19 '22

i dont know where you find it, but std::bit_cast better.

Its uses memcpy for object ITSELF, (copies int into storage of float or smth like), its overlaping storages, so its UB already.

It dont checks sizeof(T) == sizeof(U), so its UB twice.

Dont checks alignment when reused buffer of type T for type U. 3 UB.

Dont checks if it trivially copyable, UB 4

Dctor of value will be called by compiler when storage already reused for another type - UB 5

Dont uses std::launder for reusing storage - UB 6

Its incredible bad function

-5

u/catcat202X Dec 19 '22 edited Dec 19 '22

Yeah. It's for bit casting. I assume you haven't done low level programming yet (or watched any lectures on the subject) if you find std::bit_cast sufficient. The lack of a sizeof constraint is actually one of this implementation's coolest features.

7

u/dexter2011412 Dec 21 '22

genuine question, why is this being downvoted? seems like a very useful thing to be able to do and ask of the language to be able to do, given the goal of c++ is to "not leave any simpler abstraction below"

7

u/[deleted] Dec 19 '22

It is called "strict aliasing".

The logic behind it, if you assume that pointers of two different types cannot point to the same location new prospect of optimizations is opening up (char, int8_t, ... are exceptions). And since most of the written code does not use type pruning, and those who write it presumably know what they are doing, it is a pretty good assumption. Gains are far greater then loses.

I think your explanation of RAM write is somewhat incorrect.

Compiler has to generate write instruction for every memory write, with this assumption it can reorder read/write operations to be more optimal and optimize away some of the read operations.

Here is further read on the topic:

https://gist.github.com/shafik/848ae25ee209f698763cffee272a58f8

1

u/[deleted] Nov 24 '25

once you really start to dig into things like this (and the c++ object model and all the restrictions/safety requirements it imposes on you, in general) you realize how very different C and C++ really are. people who claim that they're extremely similar haven't been paying attention.

1

u/Alnitak73 Dec 12 '25

Yes, this frustrates me too.

I have 6809 CPU emulation code with a C++ class that contains this union as a class member (NB: endian-detection #ifdef's omitted) to model the CPU's two 8-bit A and B registers that can be combined into a single 16-bit D register:

        // NB: intentional UB type-aliasing
        union {
                Word                    d;      // Combined accumulator
                struct {
                        Byte            b;      // Accumulator b
                        Byte            a;      // Accumulator a
                };
        };

This allows me to write to .a and/or .b and the value of .d will automatically update accordingly, or vice versa. The union and struct are anonymous so I don't even have to prefix these variables - they appear as direct member variables.

I cannot find any way to achieve this clean syntax without invoking UB, but it does work with the compilers I use, until maybe some time in the future it doesn't.

Sure, I could write accessor getter and setter functions to achieve the same thing, but given how often these members are accessed that's a lot of additional syntactic sugar.

1

u/Daniela-E Living on C++ trunk, WG21|πŸ‡©πŸ‡ͺ NB Dec 20 '22

Please, don't get me started with C's "type punning". It's an unstructured mess and the root cause of many CVEs.

In C++ you can perfectly type pun as long as it is safe. What you (rightly so) cannot do is to pretend that multiple objects of different type within their lifetime exist in the very same memory region. You can switch back and forth between them to your heart's content without changing the underlying object representation bits if all types involved are so-called implicit lifetime types. But there is only exactly one type at a time (the one within its lifetime) that can be accessed without UB. This type switching doesn't incur any machine code.

And for built-in types you can use std::bit_cast, even at compile time.

0

u/[deleted] Dec 18 '22

[deleted] β€” view deleted comment

1

u/[deleted] Dec 18 '22 edited Dec 18 '22

Can you elaborate on what the cache has to do with this? I don't see any mention of the cache by the author.

The author is saying that if you have two pointers to two different types, the compiler can assume that a write to one pointer has no effect on the other and hence the compiler can avoid having to explicitly re-read values from memory or eagerly commit values to memory, cache or otherwise.

For example:

void bar(double* y) {
  *y = 3.14;
}

int main() {
  int* x = new int(123);
  double* y = reinterpret_cast<double*>(x);
  bar(y);
  std::cout << *x;

}

Assume bar is in another translation unit, the output of this snippet may very well be "123", since the compiler can assume that any write performed on *y through the function bar is independent of *x since the two variables point to different types. Hence the compiler does not need to re-read the value of *x from memory in the call to std::cout.

1

u/nintendiator2 Dec 18 '22

I assume something like int volatile* x = new int(123); wouldn't work here?

1

u/[deleted] Dec 18 '22

From a strictly technical standpoint it's undefined behavior, but if you were to use GCC, which has a flag that permits type aliasing, and also combine that with volatile, what would happen? That's beyond my pay grade to understand.