llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Assertion failed in CGRecordLayoutBuilder.cpp:526 (Prior->Kind == MemberInfo::Field && !Prior->FD && "Only storage fields have tail padding!)

Open adibiagio opened this issue 12 years ago • 7 comments

Bugzilla Link 21040
Resolution FIXED
Resolved on Oct 22, 2015 13:06
Version trunk
OS Windows NT
CC @majnemer,@DougGregor,@hfinkel,@rnk

Extended Description

To reproduce the assertion failure,

////// struct A { int a0[0]; // zero-length array. };

struct B : private A { // assertion failure! int b; };

B bb; //////

CGrecordLayoutBuilder.cpp:526: void {anonymous}::CGRecordLowering::clipTailPadding(): Assertion `Prior->Kind == MemberInfo::Field && !Prior->FD && "Only storage fields have tail padding!" failed.

Note that if we replace the zero-length array within struct A with a C99-style flexible array, then the compiler doesn't assert. Instead, it emits the following " error: base class 'A' has a flexible array member'.

Example:

////// struct A { int a0[]; // c99 style flexible array. };

struct B : private A { // expected diagnostic error. int b; };

B bb; //////

The error is raised by Sema (see SemaDeclCXX.cpp at around line 1457). The comments in the code say that: "A class which contains a flexible array member is not suitable for use as a base class". The motivation in this case is: "if the layout determines that a base comes before the derived class, the flexible array member would index into the derived class".

My opinion is that we should treat zero-length arrays the same way as flexible arrays for the purpose of layout. This is because zero-length arrays are an alternative (deprecated) way to specify flexible arrays (see https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html for further details). At the very least, clang shouldn't assert.

adibiagio avatar Sep 23 '14 12:09 adibiagio

Fixed in r251036.

majnemer avatar Oct 22 '15 20:10 majnemer

A fix is out for review here: http://reviews.llvm.org/D5478

majnemer avatar Sep 24 '14 13:09 majnemer

We also get this related case wrong: struct V { struct { int x; int y[]; }; int z; } v;

majnemer avatar Sep 24 '14 11:09 majnemer

If we want to support this, IMO we should consistently model zero-length arrays at the end of structs as VLAs and raise a diagnostic.

I agree with you. It is not that we care that much about supporting it, but we just noticed it with our random code generator.

adibiagio avatar Sep 23 '14 21:09 adibiagio

Not sure if this can help (I am not a Clang expert), but I noticed that the reason why Clang fails in that particular example I posted is because method 'CGRecordLowering::clipTailPadding()' sees members of struct B in reverse order.

So, the first member in structure B appears to be 'int b', while the second member is 'struct A { int a0[0]; }'.

After further investigating on this, I noticed that when lowering class B, method 'CGRecordLowering::lower' firstly collects all the memebers of B (fields and base classes) and then it sorts members by their relative offset.

As far as I can see, this doesn't work in the pathological case where we have a base class with only zero-length array fields.

When lowering 'struct B', we find the following two members:

  • 'int b0' , at relative offset 0;
  • 'struct A { int a0[0]; }', at relative offset 0.

We end up with the ordered sequence: [ 'int b0', 'struct A { int a0[0]; }' ].

In method 'clipTailPadding()' this is what eventually triggers the assertion failure. If I try to workaround the problem in 'clipTailPadding()', later on clang asserts in method 'insertPadding()'; again, the problem is caused by the fact that members are visited in the wrong order.

I hope this helps...

adibiagio avatar Sep 23 '14 20:09 adibiagio

I've already begun to dig into this.

Things seem to be awfully broken here.

consider the following in c++ mode: struct A { union { int a0[]; }; int x; } b;

majnemer avatar Sep 23 '14 20:09 majnemer

If we want to support this, IMO we should consistently model zero-length arrays at the end of structs as VLAs and raise a diagnostic.

rnk avatar Sep 23 '14 20:09 rnk