Skip to content

classInstanceSize is not a multiple of its classInstanceAlignment #21065

Closed

Description

@ibuclaw

https://godbolt.org/z/3sojhh1eh

This affects both C++ and D classes.

extern(C++) class CXX {
    ubyte[4] not_multiple_of_8;
}
extern(D) class D {
    ubyte[4] not_multiple_of_8;
}

static assert(__traits(classInstanceAlignment, CXX) == 8);
static assert(__traits(classInstanceSize, CXX) == 16);  // fails, 12

static assert(__traits(classInstanceAlignment, D) == 8);
static assert(__traits(classInstanceSize, D) == 24);  // fails, 20

Activity

  1. added
    Compiler:GDC
    Feature:extern (C/C++/Obj-C)
    Severity:rejects-valid
    on Mar 23, 2025
  2. added theissue type on Mar 23, 2025
  3. ibuclaw commented on Mar 23, 2025

    @ibuclaw
    MemberAuthor

    It's not as simple as adjusting the structsize of classes in finalizeSize, as derived classes are packed together.

    https://godbolt.org/z/vWf7vWj8z

  4. added a commit that references this issue on Mar 26, 2025
    4979907
  5. kinke commented on Mar 27, 2025

    @kinke
    Contributor

    Why is this a bug? There aren't any contiguous arrays of instances, so no need for any tail padding.

    Edit: Well, unless you are worried about D-allocated extern(C++) class refs being passed to C++, and C++ going ahead and doing a memcpy for the whole (C++-padded) instance size, or something along these lines (which noone has come across yet?).
    Edit2: Which seems unlikely, given that one uses D classes to represent polymorphic C++ classes only.

  6. TurkeyMan commented on Mar 27, 2025

    @TurkeyMan
    Contributor

    I actually do have arrays of class instances :P ... allocation pools cause this scenario.

  7. ibuclaw commented on Mar 27, 2025

    @ibuclaw
    MemberAuthor

    @kinke you can have arrays of class instances, or an array of T[1] to be precise.

    // Reduction from dnewarrayT in lifetime.d
    void* p = cast(void*)(0xdeadbeef);
    auto init = __traits(initSymbol, CXX);
    p[0 .. init.length] = init[];
    
  8. kinke commented on Mar 27, 2025

    @kinke
    Contributor

    No, not in D; in your example, you have a void[] only. So if an allocator has an assumption that allocated block sizes need to be a multiple of the desired alignment, it's IMO clearly that allocator's responsibility/problem. - Closures are another example for blocks where the size might not be a multiple of its alignment.

  9. 2 remaining items

  10. ibuclaw commented on Mar 28, 2025

    @ibuclaw
    MemberAuthor

    My understanding

    (p + init.length <= init.ptr)
    

    is interpreted as

    ((struct CXX*)p) + 1 <= init.ptr)
    

    At which point, the compiler notices this class type is in violation, as the pointer comparison has one address that is unaligned.

  11. kinke commented on Mar 28, 2025

    @kinke
    Contributor

    Interpreted where, in GDC only? There's a reason we use void[] for the init-symbol payload. :) And the helper function uses void[] exclusively too:

    void[] _d_arraycopy(size_t size, void[] from, void[] to)
    {
    debug(PRINTF) printf("f = %p,%zd, t = %p,%zd, size = %zd\n",
    from.ptr, from.length, to.ptr, to.length, size);
    enforceRawArraysConformable("copy", size, from, to);
    memcpy(to.ptr, from.ptr, to.length * size);
    return to;
    }

  12. kinke commented on Mar 28, 2025

    @kinke
    Contributor

    If you're doing custom bounds checks in GDC, maybe try abs(p - init.ptr) >= init.length to hopefully prevent the earlier const-folding? [Which is invalid for the current D semantics; I guess such a layout+alignment combo is not representable in C++, so maybe not handled in gcc.]

  13. ibuclaw commented on Mar 28, 2025

    @ibuclaw
    MemberAuthor

    Tricking the compiler is a futile experience. Just generate data structures that are compliant with architectures and their strict alignment requirements.

  14. ibuclaw commented on Mar 28, 2025

    @ibuclaw
    MemberAuthor

    Try compiling any example with -O2 -Warray-bounds. The error is what happens when you try to new any bad class via dnewclassT.

  15. kinke commented on Mar 28, 2025

    @kinke
    Contributor

    Well to me that's clearly a gcc/gdc bug, and that tail padding not needed to match any alignment requirements.

  16. ibuclaw commented on Mar 28, 2025

    @ibuclaw
    MemberAuthor

    Don't think so. When dealing with statics, it's not unreasonable for the optimizer to do CCP, nor to convert ptr + size into a memory reference of type typeof(class).

    https://d.godbolt.org/z/6jEaEWh1a

  17. ibuclaw commented on Mar 28, 2025

    @ibuclaw
    MemberAuthor

    Alternatively, all D classes - both extern(C++) and extern(D) - should be defined as packed then, meaning their underlying data alignment (classInstanceAlignment) is 1. This does not affect alignment of individual fields.

  18. kinke commented on Mar 28, 2025

    @kinke
    Contributor

    Again, this isn't a problem for DMD nor LDC, just for GDC. I'm not really sure the tail-padding 'elimination' is actually worth it, but just because the gcc optimizer can't cope with such 'exotic' aggregates IMO doesn't justify changing the D class ABI in general.

  19. kinke commented on Mar 28, 2025

    @kinke
    Contributor

    Alternatively, all D classes - both extern(C++) and extern(D) - should be defined as packed then, meaning their underlying data alignment (classInstanceAlignment) is 1.

    This is similar to what LDC does and what I mentioned above - we emit the IR struct as packed, meaning that the IR alignment is 1. But the frontend classInstanceAlignment matches the actual allocation alignments (e.g., for a scope stack allocation, we use an explicit align(8) IR attribute for a CXX instance). [And the IR align(1) (for the aggregate itself) doesn't affect/pessimize any field access, they are all appropriately aligned unless explicitly overridden with a D align(N).]

  20. ibuclaw commented on Mar 28, 2025

    @ibuclaw
    MemberAuthor

    align(N) class C{} is ignored by the front-end.

    It gets complicated when your mixing alignments. I immediately expect bus errors to occur on SPARC. But that will need to be regression tested first before anything else.

  21. kinke commented on Mar 28, 2025

    @kinke
    Contributor

    [I meant align(N) for the fields. E.g., class C { align(64) int a; } for an explicit over-alignment, or class C { int a; align(4) long b; } with an under-aligned b.]

  22. ibuclaw commented on May 11, 2025

    @ibuclaw
    MemberAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Compiler:GDCFeature:extern (C/C++/Obj-C)Severity:criticalSeverity:rejects-valid

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions