38

When I read ISO/IEC 9899:1999 (see:6.5.2.3), I saw an example like this (emphasis mine) :

The following is not a valid fragment (because the union type is not visible within function f):

struct t1 { int m; };
struct t2 { int m; };
int f(struct t1 * p1, struct t2 * p2)
{
      if (p1->m < 0)
            p2->m = -p2->m;
      return p1->m;
}
int g()
{
      union {
            struct t1 s1;
            struct t2 s2;
      } u;
      /* ... */
      return f(&u.s1, &u.s2);
}

I found no errors and warnings when I tested.

My question is: Why is this fragment invalid?

4
  • 6
    f can assume that p1 != p2 because they point to different types. and with optimization - read p1->m value in register and return this register. it assume that p2->m = -p2->m not modify p1->m what is wrong. union here only way make p1==p2
    – RbMm
    Sep 26, 2018 at 7:37
  • 4
    I took the liberty of transcribing the image to text, I hope I didn't make any typos. The original is visible in the edit history: stackoverflow.com/revisions/52511896/1
    – ilkkachu
    Sep 26, 2018 at 9:48
  • 4
    note that there are many invalid programs that compile cleanly. IN fact the reason there is so much text in C standards about what is valid , invalid, UB, .... is because you cannot rely on the compiler to simply detect and reject them
    – pm100
    Sep 26, 2018 at 23:44
  • simple fix... define the union before the first function Oct 2, 2018 at 20:42

3 Answers 3

33

The example attempts to illustrate the paragraph beforehand1 (emphasis mine):

6.5.2.3 ¶6

One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the completed type of the union is visible. Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members.

Since f is declared before g, and furthermore the unnamed union type is local to g, there is no questioning the union type isn't visible in f.

The example doesn't show how u is initialized, but assuming the last written to member is u.s2.m, the function has undefined behavior because it inspects p1->m without the common initial sequence guarantee being in effect.

Same goes the other way, if it's u.s1.m that was last written to before the function call, than accessing p2->m is undefined behavior.

Note that f itself is not invalid. It's a perfectly reasonable function definition. The undefined behavior stems from passing into it &u.s1 and &u.s2 as arguments. That is what's causing undefined behavior.


1 - I'm quoting n1570, the C11 standard draft. But the specification should be the same, subject only to moving a paragraph or two up/down.

14
  • 1
    So would changing f to take int* and passing in &u.s1.m and u.s2.m make it valid? Because then it’s g doing the struct accessing.
    – Zastai
    Sep 26, 2018 at 7:36
  • 5
    @Zastai - You know, I'm not sure. I think that's a good question that deserves to stand on its own. Post it with the the language-lawyer tag. It should be interesting. Sep 26, 2018 at 7:37
  • 2
    @haccks - Yes. That's why GCC and Clang go to town on this function under strict aliasing. They have no reason to assume the two unrelated type may alias. Because normally they can't. Technically this is all handled under the "effective type" set of clauses. But here's another example. Here the compiler knows aliasing is possible, so it's not being too aggressive. Sep 26, 2018 at 11:32
  • 1
    @haccks - §6.5 ¶7. Those are all the valid ways to alias the same object. So in the original example p1 and p2 may be assumed to not alias the same object (even though they do in a valid way through the union). The code example I gave you in the previous comment must be conservative since the union contains the struct as a member, so things may be aliasing. I think the common sub-sequence guarantee is phrased like that to play along with the effective type requirements. Sep 26, 2018 at 12:16
  • 1
    @StoryTeller; Thanks a lot for explaining it and being patient :)
    – haccks
    Sep 26, 2018 at 12:24
27

Here is the strict aliasing rule in action: one assumption made by the C (or C++) compiler, is that dereferencing pointers to objects of different types will never refer to the same memory location (i.e. alias each other.)

This function

int f(struct t1* p1, struct t2* p2);

assumes that p1 != p2 because they formally point to different types. As a result the optimizatier may assume that p2->m = -p2->m; have no effect on p1->m; it can first read the value of p1->m to a register, compare it with 0, if it compare less than 0, then do p2->m = -p2->m; and finally return the register value unchanged!

The union here is the only way to make p1 == p2 on binary level because all union member have the same address.

Another example:

struct t1 { int m; };
struct t2 { int m; };

int f(struct t1* p1, struct t2* p2)
{
    if (p1->m < 0) p2->m = -p2->m;
    return p1->m;
}

int g()
{
    union {
        struct t1 s1;
        struct t2 s2;
    } u;
    u.s1.m = -1;
    return f(&u.s1, &u.s2);
}

What must g return? +1 according to common sense (we change -1 to +1 in f). But if we look at gcc's generate assembly with -O1 optimization

f:
        cmp     DWORD PTR [rdi], 0
        js      .L3
.L2:
        mov     eax, DWORD PTR [rdi]
        ret
.L3:
        neg     DWORD PTR [rsi]
        jmp     .L2
g:
        mov     eax, 1
        ret

So far all is as excepted. But when we try it with -O2

f:
        mov     eax, DWORD PTR [rdi]
        test    eax, eax
        js      .L4
        ret
.L4:
        neg     DWORD PTR [rsi]
        ret
g:
        mov     eax, -1
        ret

The return value is now a hardcoded -1

This is because f at the beginning caches the value of p1->m in the eax register (mov eax, DWORD PTR [rdi]) and does not reread it after p2->m = -p2->m; (neg DWORD PTR [rsi]) - it returns eax unchanged.


union here used only for all non-static data members of a union object have the same address. as result &u.s1 == &u.s2.

is somebody not understand assembler code, can show in c/c++ how strict aliasing affect f code:

int f(struct t1* p1, struct t2* p2)
{
    int a = p1->m;
    if (a < 0) p2->m = -p2->m;
    return a; 
}

compiler cache p1->m value in local var a (actually in register of course) and return it , despite p2->m = -p2->m; change p1->m. but compiler assume that p1 memory not affected, because it assume that p2 point to another memory which not overlap with p1

so with different compilers and different optimization level the same source code can return different values (-1 or +1). so and undefined behavior as is

7
  • @StoryTeller - yes, the same (-1) with clang. but with msvc and icc always +1 returned (no strict aliacing here)
    – RbMm
    Sep 26, 2018 at 8:47
  • 1
    Just as an addendum to this, it's worth mentioning that declaring the arguments to f() as volatile will prevent this optimisation. Technically this is still undefined behaviour according to the standard, but most compilers should come up with a sane answer. Why anyone would want to write such bizarre code is beyond me, though. :-/
    – Graham
    Sep 26, 2018 at 12:00
  • The purpose of the Standard is to ensure that even compilers that are too primitive to recognize at the call site that a call to someFunction(&myUnion->member1) might interact with other members in the union which are used by the caller, could recognize in the called function that such interactions may occur. In practice, gcc and clang don't reliably recognize such interactions in any case, even when complete union declarations are visible, and when the compiler is able to see that the structures sharing the Common Initial Sequence are in fact members of the same array of union objects.
    – supercat
    Sep 26, 2018 at 23:44
  • @supercat - from my view unions here unrelated at all. this play role compiler optimization (in f) based on assumption that pointers to 2 different type can not overlap. so modification memory by p2 pointer can not affect memory of p1. union here used only for format 2 pointers with different type but point to same memory. so while we can access and inactive member of union if it "similar" to active, use pointers to 2 union member at once, even if it have "common initial sequence" (even full equal) lead to this ub
    – RbMm
    Sep 27, 2018 at 0:07
  • @supercat i think faster compilers must assume that 2 pointers to 2 types that have "common initial sequence" can point to the same memory. the t1 and t2 is such types. so will be logical (from my opinion) that compilers assume that such pointers can alias. but now gcc/clang not do this. also i be serious redesign(extend) term what is "Common Initial Sequence"
    – RbMm
    Sep 27, 2018 at 0:38
3

One of the major purposes of the Common Initial Sequence rule is to allow functions to operate on many similar structures interchangeably. Requiring that compilers presume that any function which acts upon a structure might change the corresponding member in any other structure that shares a common initial sequence, however, would have impaired useful optimizations.

Although most code which relies upon the Common Initial Sequence guarantees makes use of a few easily recognizable patterns, e.g.

struct genericFoo {int size; short mode; };
struct fancyFoo {int size; short mode, biz, boz, baz; };
struct bigFoo {int size; short mode; char payload[5000]; };

union anyKindOfFoo {struct genericFoo genericFoo;
  struct fancyFoo fancyFoo;
  struct bigFoo bigFoo;};

...
if (readSharedMemberOfGenericFoo( myUnion->genericFoo ))
  accessThingAsFancyFoo( myUnion->fancyFoo );
return readSharedMemberOfGenericFoo( myUnion->genericFoo );

revisiting the union between calls to functions that act on different union members, the authors of the Standard specified that visibility of the union type within the called function should be the determining factor for whether functions should recognize the possibility that an access to e.g. field mode of a FancyFoo might affect field mode of a genericFoo. The requirement to have a union containing all types of structures whose address might be passed to readSharedMemberOfGeneric in the same compilation unit as that function makes the Common Initial Sequence rule less useful than it would otherwise be, but would make at least allow some patterns like the above usable.

The authors of gcc and clang thought that treating union declarations as an indication that the types involved might be involved in constructs like the above would be an impractical impediment to optimization, however, and figured that since the Standard doesn't require them to support such constructs via other means, they'll simply not support them at all. Consequently, the real requirement for code that would need to exploit the Common Initial Sequence guarantees in any meaningful fashion is not to ensure that a union type declaration is visible, but to ensure that clang and gcc are invoked with the -fno-strict-aliasing flag. Also including a visible union declaration when practical wouldn't hurt, but it is neither necessary nor sufficient to ensure correct behavior from gcc and clang.

12
  • you try say that if we have S1* p1; and S2* p2; , S1 and S2 have common initial sequence and exist visible declaration union U { S1 s1; S2 s2; }; compiler can not assume that p1 != p2, but if remove U declaration - compiler already can assume that p1 != p2 because it different types (despite CIS) and do optimization based on this ? from my opinion this not very good/logical
    – RbMm
    Sep 27, 2018 at 1:01
  • from one side of standard If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined.. and case when we access object via type which have CIS type (even with full CS type) here formal not listed ? so UB ? but from another side in union paragraph CIS rule. but when we access "not active" union member - we access the stored value of an object with dynamic type of "active" member - and from my look here contradiction in rules
    – RbMm
    Sep 27, 2018 at 1:26
  • @RbMm: The only way I can see for 6.5p7 to make any sense is to recognize that it is only meant to apply in cases which involve aliasing as written, and that accessing an object with a pointer that is freshly derived from it isn't "aliasing". In the "bad" CIS example in the Standard, if both pointers identify members of the same union object, whichever one had been derived first would have ceased to be "fresh" when the other was derived from that union. Clang and gcc, however, will rewrite examples that don't use aliasing so as to add aliasing which they then can't handle.
    – supercat
    Sep 27, 2018 at 2:58
  • @RbMm: If accesses via freshly-derived pointers are recognized as accesses to the objects from which they are derived, then few programs would need permission to access structures and unions with unrelated lvalues of member types (or character types, for that matter!), and thus the lack of blanket permission would make sense. Unfortunately, since the authors of the Standard didn't say that quality compilers should recognize pointer derivation (probably thinking the footnote about aliasing would suffice), clang and gcc don't bother.
    – supercat
    Sep 27, 2018 at 3:10
  • from another side - are this code formal correct ? here 3 different members of union have not CIS but by sense - all 3 is pointers, have equal memory layout, so we can read any of this after assign/change one
    – RbMm
    Sep 27, 2018 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.