Bug 14179 – Posix x86_64 varargs prolog clobbers RDX

Status
RESOLVED
Resolution
FIXED
Severity
blocker
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2015-02-14T22:15:00Z
Last change time
2017-07-19T17:38:31Z
Keywords
pull, wrong-code
Assigned to
nobody
Creator
yebblies

Comments

Comment #0 by yebblies — 2015-02-14T22:15:03Z
The compiler generates the required prolog for varargs functions, saving the registers and filling the __va_argsave structure. When RDX (int reg 3) is used for a named argument it is not saved into the varargs save space, and is used as a temp register, discarding the argument value. ================= Prolog description ==================== /* Generate code to move any arguments passed in registers into * the stack variable __va_argsave, * so we can reference it via pointers through va_arg(). * struct __va_argsave_t { * size_t[6] regs; * real[8] fpregs; * uint offset_regs; * uint offset_fpregs; * void* stack_args; * void* reg_args; * } * The MOVAPS instructions seg fault if data is not aligned on * 16 bytes, so this gives us a nice check to ensure no mistakes. MOV voff+0*8[RBP],EDI MOV voff+1*8[RBP],ESI MOV voff+2*8[RBP],RDX MOV voff+3*8[RBP],RCX MOV voff+4*8[RBP],R8 MOV voff+5*8[RBP],R9 MOVZX EAX,AL // AL = 0..8, # of XMM registers used SHL EAX,2 // 4 bytes for each MOVAPS LEA RDX,offset L2[RIP] SUB RDX,RAX LEA RAX,voff+6*8+0x7F[RBP] JMP EDX MOVAPS -0x0F[RAX],XMM7 // only save XMM registers if actually used MOVAPS -0x1F[RAX],XMM6 MOVAPS -0x2F[RAX],XMM5 MOVAPS -0x3F[RAX],XMM4 MOVAPS -0x4F[RAX],XMM3 MOVAPS -0x5F[RAX],XMM2 MOVAPS -0x6F[RAX],XMM1 MOVAPS -0x7F[RAX],XMM0 L2: MOV 1[RAX],offset_regs // set __va_argsave.offset_regs MOV 5[RAX],offset_fpregs // set __va_argsave.offset_fpregs LEA RDX, Para.size+Para.offset[RBP] MOV 9[RAX],RDX // set __va_argsave.stack_args SUB RAX,6*8+0x7F // point to start of __va_argsave MOV 6*8+8*16+4+4+8[RAX],RAX // set __va_argsave.reg_args */ ================= Test case ==================== import core.stdc.stdarg; struct Loc { const(char)* filename; uint linnum; uint charnum; } extern(C++) const(char)* func(Loc loc, const(char)* string, ...) { return string; } void main() { const(char)* s = "hello"; assert(func(Loc(), s) == s); }
Comment #1 by yebblies — 2015-02-14T22:30:35Z
Comment #2 by pro.mathias.lang — 2015-02-14T22:37:42Z
Comment #3 by yebblies — 2015-02-14T22:46:35Z
(In reply to Mathias LANG from comment #2) > Could it be related to https://issues.dlang.org/show_bug.cgi?id=13769 ? I don't think so, you need 3 'INTEGER' parameters to use RDX and the test case in that bug never uses more than one.
Comment #4 by github-bugzilla — 2015-02-19T08:07:34Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/0687872a11386f5cb9da0d88af59cf460badf2d7 Fix Issue 14179 - Posix x86_64 varargs prolog clobbers RDX RDX is used in the prolog but may be used to pass named arguments. When used for a named argument, store in R11 during prolog. https://github.com/D-Programming-Language/dmd/commit/d08a9afeb7670aae4a6a7c74518b94a937bd96a6 Merge pull request #4413 from yebblies/issue14179 Issue 14179 - Posix x86_64 varargs prolog clobbers RDX
Comment #5 by github-bugzilla — 2015-02-20T00:16:33Z
Commit pushed to 2.067 at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/353cbbed6451f6a21c8d776443db72130c78e879 Merge pull request #4413 from yebblies/issue14179 Issue 14179 - Posix x86_64 varargs prolog clobbers RDX
Comment #6 by github-bugzilla — 2015-02-21T09:11:35Z
Comment #7 by github-bugzilla — 2015-04-11T12:24:40Z
Comment #8 by github-bugzilla — 2015-06-17T21:00:48Z
Comment #9 by github-bugzilla — 2017-07-19T17:38:31Z
Commits pushed to dmd-cxx at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/0687872a11386f5cb9da0d88af59cf460badf2d7 Fix Issue 14179 - Posix x86_64 varargs prolog clobbers RDX https://github.com/dlang/dmd/commit/d08a9afeb7670aae4a6a7c74518b94a937bd96a6 Merge pull request #4413 from yebblies/issue14179