November 05, 2020
https://issues.dlang.org/show_bug.cgi?id=21364

          Issue ID: 21364
           Summary: Passing a struct by-value adds padding that breaks
                    argument access within the function
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: alexander.breckel@gmail.com

test.d
---------------------
struct X {
        float x0;
        long  x1;
}

void foo(int bar, X x, int i1, int i2, int i3, int i4, int i5, int i6, int i7,
int i8, int i9) {
        import std.stdio;
        writeln(bar); // prints 0 instead of 1
}

void main() {
        X x = X();
        foo(1, x, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}
---------------------
$ dmd -ofapp test.d && ./app

The writeln prints 0 instead of 1.

$ dmd --version
DMD64 D Compiler v2.094.0


The call to foo is compiled to:
   9c9ec:       48 83 ec 08             sub    $0x8,%rsp
   9c9f0:       6a 01                   pushq  $0x1
   9c9f2:       ff 75 f8                pushq  -0x8(%rbp)
   9c9f5:       ff 75 f0                pushq  -0x10(%rbp)
   9c9f8:       48 83 ec 08             sub    $0x8,%rsp   <----- wrong !!!
   9c9fc:       6a 02                   pushq  $0x2
   9c9fe:       6a 03                   pushq  $0x3
   9ca00:       6a 04                   pushq  $0x4
   9ca02:       41 b9 05 00 00 00       mov    $0x5,%r9d
   9ca08:       41 b8 06 00 00 00       mov    $0x6,%r8d
   9ca0e:       b9 07 00 00 00          mov    $0x7,%ecx
   9ca13:       ba 08 00 00 00          mov    $0x8,%edx
   9ca18:       be 09 00 00 00          mov    $0x9,%esi
   9ca1d:       bf 0a 00 00 00          mov    $0xa,%edi
   9ca22:       e8 91 ff ff ff          callq  9c9b8
<_D4test3fooFiSQm1XiiiiiiiiiZv>

After pushing the contents of the struct value x onto the stack, rsp is additionally decreased by 8. However, when accessing the function argument later on this additional space of 8 bytes is not taken into account. Smells like an unnecessary alignment padding for struct by-value function call arguments. Either the padding itself is wrong, or the offset in the symbol table does not include the padding.

Any small deviation from the code above "fixes" the issue, e.g. removing function arguments from the back, or changing the "float" to "long", or adding extern (C).

--