acmel / dwarves

Pahole and the dwarves
GNU General Public License v2.0
189 stars 51 forks source link

class inherit from "enable_shared_from_this", incorrect reordering will occur #38

Open HealthyChina opened 1 year ago

HealthyChina commented 1 year ago

Example:

#include <iostream>
using namespace std;

class TTT
{
public:
    int a; // 4
    char b; // 4
};

class FFF
{
public:
    int c; // 4
};

class async_service_helper : public enable_shared_from_this<async_service_helper>
{
    virtual ~async_service_helper() { cout << "test" << endl; }

public:

    int i1;
    unsigned int i2;
    TTT* ptr_t;
    FFF* ptr_f;
};
acmel commented 1 year ago

Can you describe the incorrect reordering taking place? Consider cut and pasting here the output of the tool that you think is erroneous.

HealthyChina commented 1 year ago
----> async_service_helper ---->
/* Original struct: */
class async_service_helper : public enable_shared_from_this<async_service_helper> {

        /* XXX 8 bytes hole, try to pack */
public:

        /* class enable_shared_from_this<async_service_helper> <ancestor>; */ /*     8    16 */

        /* XXX 65512 bytes hole, try to pack */
        void async_service_helper(class async_service_helper *, const class async_service_helper  &);

        void async_service_helper(class async_service_helper *);

        int ()(void) * *           _vptr.async_service_helper; /*     0     8 */

        /* XXX 16 bytes hole, try to pack */
        virtual void ~async_service_helper(class async_service_helper *, int);

        class FFF *                ptr_f;                /*    24     8 */
        int                        i1;                   /*    32     4 */

        /* XXX 4 bytes hole, try to pack */

        class TTT *                ptr_t;                /*    40     8 */
        class GGG *                ptr_g;                /*    48     8 */
        unsigned int               i2;                   /*    56     4 */
        int Test(class async_service_helper *);

        /* size: 64, cachelines: 1, members: 7 */
        /* sum members: 40, holes: 4, sum holes: 65540 */
        /* padding: 4 */

        /* BRAIN FART ALERT! 64 bytes != 40 (member bytes) + 0 (member bits) + 65540 (byte holes) + 0 (bit holes), diff = -524160 bits */
};/* Moving 'i1' from after 'ptr_f' to after '_vptr.async_service_helper' */
/* Moving 'i2' from after 'ptr_g' to after 'i1' */
/* Moving 'ptr_g' from after 'ptr_t' to after 'i2' */
/* Final reorganized struct: */
class async_service_helper : public enable_shared_from_this<async_service_helper> {

        /* XXX 8 bytes hole, try to pack */
public:

        /* class enable_shared_from_this<async_service_helper> <ancestor>; */ /*     8    16 */

        /* XXX 65512 bytes hole, try to pack */

        int ()(void) * *           _vptr.async_service_helper; /*     0     8 */
        int                        i1;                   /*     8     4 */
        unsigned int               i2;                   /*    12     4 */
        class GGG *                ptr_g;                /*    16     8 */
        class FFF *                ptr_f;                /*    24     8 */
        class TTT *                ptr_t;                /*    32     8 */

        /* size: 40, cachelines: 1, members: 7 */
        /* sum members: 40, holes: 2, sum holes: 65520 */
        /* last cacheline: 40 bytes */

        /* BRAIN FART ALERT! 40 bytes != 40 (member bytes) + 0 (member bits) + 65520 (byte holes) + 0 (bit holes), diff = -524160 bits */
};
   /* saved 24 bytes! */
<---- async_service_helper <----

Reordering shouldn't start at 0, because the previous one is already occupied.(/ class enable_shared_from_this ; / / 8 16 /)