Bug 24184 – [REG 2.103] Segmentation fault accessing variable with align(N) > platform stack alignment

Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2023-10-11T16:02:18Z
Last change time
2023-11-20T11:13:26Z
Keywords
backend, industry, pull, wrong-code
Assigned to
No Owner
Creator
Iain Buclaw
See also
https://issues.dlang.org/show_bug.cgi?id=16098

Comments

Comment #0 by ibuclaw — 2023-10-11T16:02:18Z
void stage3(alias dg)(ubyte[]) { bool skipSpaces() { dg(); return false; } skipSpaces; } ubyte[] singleThreadJsonImpl(alias dg)(ubyte[] table) { align(64) ubyte[] vector; stage3!(() => vector)(table); return table; } ubyte[] singleThreadJsonText() { return singleThreadJsonImpl!(data => true)([]); } void deserializeJson() { singleThreadJsonText(); } void main() { deserializeJson(); }
Comment #1 by ibuclaw — 2023-10-11T16:03:40Z
Comment #2 by ibuclaw — 2023-10-11T16:07:53Z
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.
Comment #3 by bugzilla — 2023-11-15T09:06:01Z
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().
Comment #4 by dlang-bot — 2023-11-16T02:20:51Z
@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
Comment #5 by dlang-bot — 2023-11-16T10:46:38Z
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
Comment #6 by dlang-bot — 2023-11-20T11:13:26Z
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