Thread overview
[Issue 24184] [REG 2.103] Segmentation fault accessing variable with align(N) > platform stack alignment
Oct 11, 2023
Iain Buclaw
Oct 11, 2023
Iain Buclaw
Oct 11, 2023
Iain Buclaw
Nov 15, 2023
Walter Bright
Nov 16, 2023
Dlang Bot
Nov 16, 2023
Dlang Bot
Nov 20, 2023
Dlang Bot
October 11, 2023
https://issues.dlang.org/show_bug.cgi?id=24184

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |backend, wrong-code
                 CC|                            |ibuclaw@gdcproject.org
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=16098

--
October 11, 2023
https://issues.dlang.org/show_bug.cgi?id=24184

--- Comment #1 from Iain Buclaw <ibuclaw@gdcproject.org> ---
Introduced by https://github.com/dlang/dmd/pull/14764

--
October 11, 2023
https://issues.dlang.org/show_bug.cgi?id=24184

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |industry

--- Comment #2 from Iain Buclaw <ibuclaw@gdcproject.org> ---
Before the referenced PR, the explicit alignment was ignored.  So this is sort of a regression. At the very least, code compiled and ran without issue before the change, even if the alignment of the frame variable was wrong.

--
November 15, 2023
https://issues.dlang.org/show_bug.cgi?id=24184

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com

--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> ---
I've boiled this down to:

  ubyte[] text() {
    void xxx() { }

    return single!(xxx)(null);
  }

  ubyte[] single(alias xxx)(ubyte[] table) {
    align(64) ubyte[] vector;

    ubyte[] abc() { return vector; }

    stage!(abc)();

    return table;
  }

  void stage(alias abc)() {
    abc();
  }

which compiles to:

  text:
                push    RBP
                mov     RBP,RSP
                xor     ESI,ESI
                xor     EDX,EDX
                xor     EDI,EDI         // context pointer for xxx
                call    single
                pop     RBP
                ret

  xxx:
                ret

  single:
                push    RBP
                mov     RBP,RSP
                sub     RSP,0B0h
                mov     -020h[RBP],RDI
                mov     -010h[RBP],RSI
                mov     -8[RBP],RDX
                lea     RAX,-031h[RBP]
                and     EAX,0FFFFFFC0h
                mov     0FFFFFF50h[RBP],RAX
                mov     RCX,0FFFFFF50h[RBP]
                mov     qword ptr [RCX],0
                mov     qword ptr 8[RCX],0
                lea     RDI,-020h[RBP]      // context pointer for xxx, not
single
                call    stage
                mov     RDX,-8[RBP]
                mov     RAX,-010h[RBP]
                leave
                ret

  abc:
                push    RBP
                mov     RBP,RSP
                sub     RSP,010h
                mov     -8[RBP],RDI
                mov     RAX,0FFFFFF50h[RDI]
                mov     RDX,8[RAX]
                mov     RAX,[RAX]
                leave
                ret

  stage:
                push    RBP
                mov     RBP,RSP
                sub     RSP,010h
                mov     -8[RBP],RDI
                call    abc
                leave
                ret

The error is in the LEA, which loads RDI with the context pointer for xxx(),
when it should be initializing RDI with RBP, the context pointer for single().

--
November 16, 2023
https://issues.dlang.org/show_bug.cgi?id=24184

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #4 from Dlang Bot <dlang-bot@dlang.rocks> ---
@WalterBright created dlang/dmd pull request #15820 "fix Issue 24184 - [REG 2.103] Segmentation fault accessing variable w…" fixing this issue:

- fix Issue 24184 - [REG 2.103] Segmentation fault accessing variable with align(N) > platform stack alignment

https://github.com/dlang/dmd/pull/15820

--
November 16, 2023
https://issues.dlang.org/show_bug.cgi?id=24184

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #5 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dmd pull request #15820 "fix Issue 24184 - [REG 2.103] Segmentation fault accessing variable w…" was merged into stable:

- 16e24ae55347a2808747bdb2d60f0e3bd28ae283 by Walter Bright:
  fix Issue 24184 - [REG 2.103] Segmentation fault accessing variable with
align(N) > platform stack alignment

https://github.com/dlang/dmd/pull/15820

--
November 20, 2023
https://issues.dlang.org/show_bug.cgi?id=24184

--- Comment #6 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dmd pull request #15829 "merge stable" was merged into master:

- 891cf59b1fd4118cb8c0c02258a9a54e6bb11529 by Walter Bright:
  fix Issue 24184 - [REG 2.103] Segmentation fault accessing variable with
align(N) > platform stack alignment (#15820)

https://github.com/dlang/dmd/pull/15829

--