6 ms·
I find it ironic in that C++ has the correct solution to this problem. If you need type-pruning, use reinterpret_cast<foo>(bar). Done and done. Ironic, because
by dragontamer 8y ago
I find it ironic in that C++ has the correct solution to this problem. If you need type-pruning, use reinterpret_cast<foo>(bar). Done and done. Ironic, because Linus's weapon of choice (C... or more specifically, GCC's particular implementation of C) would require far more expertise to use correctly.
Lets break it down.
In C, type-punning is NOT part of the language. Its technically "undefined behavior". There's an expectation that when you do:
short foo[2] = {1, 2} ;
*(int*)foo = 0x12345678;
assert(foo[0] == 0x5678 && foo[1] == 0x1234);
A little-endian machine will pass this assert. But this isn't guaranteed by the C standard! This is "undefined behavior". The C-standard allows a C compiler to assume that foo[0] and foo[1] are still == to 1 and 2 respectively. Which would cause the assert to fail. In GCC -O2 or -O3, this may happen, depending on how registers get mapped to the variables. (The canonical memory location changes, but should registers be updated when optimizations are enabled??)
When an optimizer can assume, and when it can't assume, "aliasing" is very much an undefined behavior within the C language.
-------------
In effect, Linus knows GCC inside and out. GCC guarantees that unions will ALWAYS work for this aliasing problem. But this requires knowledge above and beyond the C Standard.
Linus may be "hating" and "criticizing" the standard in this case. And I guess there's certainly a gap. But the general expectation that everyone knows the intricacies of GCC to properly understand the kernel code is misplaced IMO, and goes back to "Angry Linus yells at random dev unnecessarily" territory for me.
And instead of simply explaining this VERY simple fact (although super-obscure) that GCC makes unions safe against aliasing issues at the compiler level... Linus yells at the dev. Not fair IMO.
- bigcheesegs 8y agoDon't use reinterpret_cast for type-punning in c++, you'll end up with UB. You should instead use memcpy.
- ramshorns 8y agoWhat is reinterpret_cast for, if not type punning?
- tom_ 8y agoUnadjusted conversion between pointer types that have matching cv qualifiers.
- kazinator 8y agoPointers are converted in order that they be used. Though the uses may be undefined, there is value in the conversion having a well-defined syntax. In effect, C and C++ allow certain intents to be expressed in a common way, without requiring implementations to make those intents work. This is better than having compiler-specific syntax for that sort of thing.
- kazinator 8y agoNonsense. Aliasing unlike-typed objects via memcpy is equivalent to pointer aliasing. memcpy has void pointer arguments. You're relying on the addresses of the source and destination objects being converted to void pointer. All type punning is in the hands of the implementation. The language definition provides the syntax for it, which has the virtuel that all code which attempts to do type punning expresses it in the same manner. However, the language leaves it up to implementations to define whether type punning works, and with what caveats and restrictions.
- tedunangst 8y agoExcept using memcpy specifically does not violate aliasing rules, while aliases do.
- kazinator 8y agoIt absolutely does. E.g. you can't memcpy a uint64 to a double and expect well-defined behavior. There is some hand-waving in the definition of memcpy so that copying compatible objects is well-defined.
- comex 8y ago> E.g. you can't memcpy a uint64 to a double and expect well-defined behavior. Yes, you can. The behavior is implementation-defined but not undefined. (Well, it can trigger undefined behavior if the value corresponds to a signaling NaN, or if the implementation uses a nonstandard, non-IEEE format for doubles that has other "trap representations". But it's not otherwise undefined.) The basis for this is that the aliasing rule has an explicit exception for reading or writing to objects using char pointers, i.e. byte-by-byte, regardless of the object's type. This exception is in both the C standard: https://port70.net/~nsz/c/c11/n1570.html#6.5p7 https://port70.net/~nsz/c/c11/n1570.html#6.5p7 and the C++ standard: http://eel.is/c++draft/expr.prop#basic.lval-11.8 http://eel.is/c++draft/expr.prop#basic.lval-11.8 The memcpy function is defined as copying characters, so the exception applies to it too. Both standards also explicitly define that objects (at least of POD types) have byte representations and those representations are implementation-defined (as opposed to triggering undefined behavior if you depend on them). For C: > Except for bit-fields, objects are composed of contiguous sequences of one or more bytes, the number, order, and encoding of which are either explicitly specified or implementation-defined. https://port70.net/~nsz/c/c11/n1570.html#6.2.6.1 https://port70.net/~nsz/c/c11/n1570.html#6.2.6.1 C++: http://eel.is/c++draft/basic.types http://eel.is/c++draft/basic.types
- crumbshot 8y agoOr even better in this particular case, using std::variant to provide type-safe unions.
- jdoliner 8y ago> the general expectation that everyone knows the intricacies of GCC to properly understand the kernel code is misplaced IMO But this isn't just a person trying to properly understand the kernel code. It's someone who's trying to merge a change that's motivated by following a standard. If you're making a change to code based on a standard then yeah, you should know the intricacies of how the compiler implements that standard. Because, as Linus keeps coming back to, that's reality, that's what's actually going to impact users and other devs: the intricacies.
- dragontamer 8y agoHere's the change: https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git/commit/?h=dp-4.18-rc1&id=63dcc7090137a893322432e156d66be3ce104615 https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux... This change strictly makes the Linux kernel more in line with the C Standard, so it may compile better in CLang. There's literally nothing wrong here. The code is more correct and more conformant with the C-Standard. So why is Linus against it? Hint: he really isn't. Linus accepted the code anyway. I dunno, the more and more I look into this, the more I feel like its just another example of Linus unnecessarily flaming someone on the developer communication channels.
- jdoliner 8y agoWell Linus says that he's not against the code but the reasoning in the original post and I think his whole point is that neither of those arguments are good reasons to make a change. He doesn't specifically mention CLang, so I'm not sure how much he values compiling on that, but he clearly doesn't value conformation to the C Standard. Saying "This change strictly makes the Linux kernel more in line with the C Standard" and concluding there's literally nothing wrong here and that it's strictly more correct this way seems to be the opposite of Linus' opinion, and mine. It's fine to disagree with that, but don't act as if this is just unnecessary flaming by Linus without a purpose.
- dragontamer 8y ago> He doesn't specifically mention CLang This is from the overall discussion going on in this thread. Others have shown how union-aliasing fails to work in CLang throughout this thread: https://news.ycombinator.com/item?id=17242457 https://news.ycombinator.com/item?id=17242457 By removing the non-standard GCC-specific union{} methodology from the code, the contributor necessarily improved portability to CLang. That's just the facts.
- wilun 8y agoStop spreading complete bullshit and go read https://en.cppreference.com/w/cpp/language/reinterpret_cast https://en.cppreference.com/w/cpp/language/reinterpret_cast already. C++ has strict aliasing rules (probably even stricter than C) and reinterpret_cast can do absolutely nothing to workaround them. It is of no help because it certainly not allow you to access objects with a wrong type. It merely reinterpret the type of values, or values as far as the implementation is concerned (C++ reference, which are only pointers with syntactic sugar plus a few extra restrictions). In the C++ community, the current thinking is that memcpy allows to type pun, but IIRC the C++ standard is actually not even clear as for why this is the case, and in its current writing it might be that there is actually no way. Short of memcpy you need at least a placement new on a trivial type. And sometimes even other cases of insanity, like std::launder. Hell, sometimes you even need std::launder for the same type. Short of converting void* (or sufficiently large ints) to T* because of legacy API, I'm not sure reinterpret_cast has any portable use. But I'm sure you can NOT type pun with it if that would break the aliasing rules.
- mortehu 8y agoNote: When converting void* to T* you can use use static_cast, which is well-defined. For example int* x = static_cast<int*>(malloc(sizeof(int)));
- kazinator 8y agoBTW, I find this useful when programming in a C dialect that compiles as C: #ifdef __cplusplus #define strip_qual(TYPE, EXPR) (const_cast<TYPE>(EXPR)) #define convert(TYPE, EXPR) (static_cast<TYPE>(EXPR)) #define coerce(TYPE, EXPR) (reinterpret_cast<TYPE>(EXPR)) #else #define strip_qual(TYPE, EXPR) ((TYPE) (EXPR)) #define convert(TYPE, EXPR) ((TYPE) (EXPR)) #define coerce(TYPE, EXPR) ((TYPE) (EXPR)) #endif Very cool; you just use these macros for all your casts, and when building using a C++ compiler, you get additional diagnostics, like that you're accidentally stripping away a const-qualifier.
- pjmlp 8y agoYou just re-invented MFC macros for the C++ features that were being discussed for ISO C++89. https://docs.microsoft.com/en-us/cpp/mfc/reference/mfc-macros-and-globals https://docs.microsoft.com/en-us/cpp/mfc/reference/mfc-macro...
- Taniwha 8y ago*(int*)&foo ....
- mchahn 8y agoThat will also work, but the & isn't needed because foo is already a pointer address.
- kazinator 8y agoWhat you're missing is that *(int *)foo = 0x12345678 is valid C++ and equivalent to using reinterpret_cast. Here, this is the same thing (in C++) as *reinterpret_cast<int *>(foo) = 0x12345678 It is not well-defined behavior. C++ has just divided the behavior of the (TYPE) cast notation in terms of more specific kinds of casts which are mutually exclusive. If a qualifier is being removed, you must use const_cast, which will not change type. The C-style-cast notation automatically chooses the right combination of C++-style casts to do the job. So for instance a C style cast from "const char " to "void ": void foo(const void *arg) { static_cast<char *>(const_cast<void *>(arg)); } The same thing can be achieved with just: void foo(const void *arg) { (char *) arg; } So the "bad" thing about the C-style cast notation then is that no matter how you change how the inputs are declared elsewhere, it will just "do what you ask", in the spirit of the C language. Whereas the C++ style casts will diagnose if the input types change so that they are not applicable. E.g. reinterpret_cast is not so powerful that it will strip away qualifiers: void foo(const void *arg) { reinterpret_cast<char *>(arg); // error: stripped qualifier }
- ajross 8y ago> I find it ironic in that C++ has the correct solution to this problem. If you need type-pruning, use reinterpret_cast<foo>(bar). Done and done. If you need type-punning, use a union. Done and done. You're just making an appeal to authority here and deciding that The C++ Standards Committee (who give us one official way to do this) is a better authority than The GCC Authors (who give us another). And that's shortsighted and silly, because GCC (being working software) is if anything a more reliable and more accessible authority than any spec-writer will ever be. That's exactly the point Linus was making, albeit with more profanity.
- dragontamer 8y ago> And that's shortsighted and silly, because GCC (being working software) is if anything a more reliable and more accessible authority than any spec-writer will ever be. Well, until you decide to use CLang, ICC, or Visual Studio or something out there. The reason why people follow the standards is because they want their code to correctly work on more than just one compiler.
- ajross 8y agoNo, that's exactly backwards. In fact ICC and Clang (I have no idea about VS) both follow GCC on this particular feature, largely because important software like Linux relies on GCC's useful and practical feature set. See also support for GCC's struct initializer syntax, statement expressions, inline assembly constraint system, etc... GCC invented all this stuff, and software developers everywhere (Hi!) love them and use them, and the industry follows suit. Fundamentally you're still missing the point. Standards are important. Features are more important. The best standards are ones driven by features and not paper. GCC has been a far better, more innovative, and more practical driver of "standards" than any committee.
- Asooka 8y agoYou can't cast a short ptr to an int ptr safely even ignoring aliasing rules. What bites you is that ints have different alignment than shorts (2 vs 4), so while this works on e.g. x86 almost always [1], your code won't always work on machines that can't do unaligned int reads. There are a lot of sharp edges to C/++ and the community really is hurting for a good static analyzer that can at least save you from most of them. [1] almost always, because while a simple unaligned mov is safe, if the compiler unrolls and vectorises your loop, it is free to then use aligned sse read instructions, assuming that your int ptr is aligned, because pointers must be aligned according to their type. Then your program crashes, but only in optimised builds.