Bug 64535 - Base class layout violation (loss of member`s alignment)
Base class layout violation (loss of member`s alignment)
Product: Red Hat Linux
Classification: Retired
Component: gcc3 (Show other bugs)
ia64 Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
Depends On:
  Show dependency treegraph
Reported: 2002-05-07 11:35 EDT by Eugeny Belov
Modified: 2007-04-18 12:42 EDT (History)
1 user (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2004-10-02 16:05:56 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)
Testcase (1.11 KB, text/plain)
2002-05-07 11:39 EDT, Eugeny Belov
no flags Details

  None (edit)
Description Eugeny Belov 2002-05-07 11:35:55 EDT
From Bugzilla Helper:
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)

Description of problem:
Here is the small testcase attached that shows the problem. In words, we have:

class C2 {
     ptrdiff_t a;
     int b;

This class will get alignment of 8 bytes. It means that there should be 4 extra 
padding bytes at the end of the class object, after member int x_ which size is 
only 4 bytes; In practice it is.

class D : public C2 {
   virtual void foo () {}
   int x_; 

The situation here is that C2::x_ member inside the class D object loses its 
alignment, those 4 padding bytes disappeared, instead of them we see the D::x_ 
member. So, the alignment of whole class is correct, but such layout violates 
the C++ ABI (http://www.codesourcery.com/cxx-abi/abi.html#layout) which says :
For non-POD class types C, assume that all component types (i.e. proper base 
classes and non-static data member types) have been laid out, defining size, 
data size, non-virtual size, alignment, and non-virtual alignment. 
  i.e. layout of C2 members should be permanent whenever C2 is used as base or 
  So, the result should like: those padding bytes for C2::x_ should be left, 
D::x_ should be also padded to make class D alignment equal to 8.

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:
Compile attached testcase with g++3 and run it.

Actual Results:  Testcase output:
Part 0 - Passed
Part 1,2 - Failed

Expected Results:  Testcase output:
Part 0,1,2 - Passed

Additional info:
Comment 1 Eugeny Belov 2002-05-07 11:39:16 EDT
Created attachment 56524 [details]
Comment 2 Grigory Zagorodnev 2002-05-20 05:34:42 EDT
As I can see, the actuall reason of this failure is that the G++ does not 
perform finalization step when processing class C2. As a result we see the 
wrong class layout.
--- C++ ABI -----------------
Chapter 2: Data Layout 
2.4 Non-POD Class Types 
IV. Finalization 
Round sizeof(C) up to a non-zero multiple of align(C). 
This happens for base classes only. If you make D's data member of type C2 
you'll see it correctly finalized and paded.
Comment 3 Alan Cox 2002-12-15 15:14:19 EST
3.2 g++ correctly warns about the pointer abuse

vp1test.cpp: In function `int main()':
vp1test.cpp:46: warning: invalid offsetof from non-POD type `class D'; use
   pointer to member instead
Comment 4 Richard Henderson 2004-10-02 16:05:56 EDT
True that the offsetof abuse is incorrect, but it's also true that the
layout problem has been fixed in gcc 3.2.  The test case does run

Part 0 - Passed
Part 1 - Passed
Part 2 - Passed

and gdb confirms that the layout of a variable of type D

(gdb) p d
$1 = {<C2> = {a = 0, b = 0}, _vptr.D = 0x0, x_ = 0}
(gdb) p sizeof(d)
$2 = 32

Note You need to log in before you can comment on or make changes to this bug.