[LLVMbugs] [Bug 16230] New: [-cxx-abi microsoft] C++ classes are not laid out in a MS compatible way

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Tue Jun 4 18:53:07 PDT 2013


http://llvm.org/bugs/show_bug.cgi?id=16230

            Bug ID: 16230
           Summary: [-cxx-abi microsoft] C++ classes are not laid out in a
                    MS compatible way
           Product: clang
           Version: unspecified
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: david.majnemer at gmail.com
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified

Consider the following code:
class Foo
{
    virtual void Hello() {}

    float f;     /* 4 bytes */
};

class Bar
{
    virtual void Hello() {}

    float f;     /* 4 bytes */
    double d;    /* 8 bytes */
};

class EmptyBase
{
protected:
    virtual ~EmptyBase() {}
};

class SmallBar : public EmptyBase
{
    virtual void Hello() {}

    float f;     /* 4 bytes */
    double d;    /* 8 bytes */
};

int main() {
   return sizeof(Foo) + sizeof(Bar) + sizeof(SmallBar);
}

compiling with: clang -cc1 -x c++ -triple i386-pc-win32 -fdump-record-layouts
-cxx-abi microsoft empty.cpp gives us:

*** Dumping AST Record Layout
   0 | class Foo
   0 |   (Foo vftable pointer)
   4 |   float f
     | [sizeof=8, dsize=8, align=4
     |  nvsize=8, nvalign=4]


*** Dumping AST Record Layout
   0 | class Bar
   0 |   (Bar vftable pointer)
   4 |   float f
   8 |   double d
     | [sizeof=16, dsize=16, align=8
     |  nvsize=16, nvalign=8]


*** Dumping AST Record Layout
   0 | class EmptyBase
   0 |   (EmptyBase vftable pointer)
     | [sizeof=4, dsize=4, align=4
     |  nvsize=4, nvalign=4]


*** Dumping AST Record Layout
   0 | class SmallBar
   0 |   class EmptyBase (primary base)
   0 |     (EmptyBase vftable pointer)
   4 |   float f
   8 |   double d
     | [sizeof=16, dsize=16, align=8
     |  nvsize=16, nvalign=8]


Bizarrely, Microsoft chose to lay Bar out such that there is a bunch of padding
between the vtable pointer and the first member iff you didn't inherit from an
empty base class.

This means that we correctly calculate the size for SmallBar (16 bytes) but
incorrectly for Bar which should be 24 bytes.

The following has a more detailed (and mildly profane) write up:
http://lolengine.net/blog/2012/10/21/the-stolen-bytes

A useful document linked off the page may be:
http://www.openrce.org/articles/files/jangrayhood.pdf

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20130605/a9273884/attachment.html>


More information about the llvm-bugs mailing list