Thread overview
[Issue 9025] New: core.thread.Fiber seems to crash on Win64
Nov 14, 2012
Manu
Nov 14, 2012
Walter Bright
Nov 15, 2012
Walter Bright
Nov 15, 2012
Walter Bright
Nov 15, 2012
Walter Bright
November 14, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9025

           Summary: core.thread.Fiber seems to crash on Win64
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: turkeyman@gmail.com


--- Comment #0 from Manu <turkeyman@gmail.com> 2012-11-14 09:19:15 PST ---
It seems core.thread.Fiber doesn't work under the new DMD for win64.
Crashes on call(), in fiber_switchcontext(), access violation, looks like an
alignment problem, since the pointer is valid.


fiber_switchContext:
000007FEE25509E0  push        rbp
000007FEE25509E1  mov         rbp,rsp
000007FEE25509E4  push        rbx
000007FEE25509E5  push        r12
000007FEE25509E7  push        r13
000007FEE25509E9  push        r14
000007FEE25509EB  push        r15
000007FEE25509ED  push        qword ptr gs:[fiber_switchContext+15h
(7FEE25509F5h)]  *** CRASH*** +15h? does 'push qword' support reading from
unaligned addresses like that?
000007FEE25509F5  push        qword ptr gs:[fiber_switchContext+25h
(7FEE2550A05h)]
000007FEE25509FD  push        qword ptr gs:[fiber_switchContext+35h
(7FEE2550A15h)]
000007FEE2550A05  mov         qword ptr [rdi],rsp
000007FEE2550A08  mov         rsp,rsi
000007FEE2550A0B  pop         qword ptr gs:[fiber_switchContext+43h
(7FEE2550A23h)]
000007FEE2550A13  pop         qword ptr gs:[fiber_switchContext+43h
(7FEE2550A23h)]
000007FEE2550A1B  pop         qword ptr gs:[fiber_switchContext+43h
(7FEE2550A23h)]
000007FEE2550A23  pop         r15
000007FEE2550A25  pop         r14
000007FEE2550A27  pop         r13
000007FEE2550A29  pop         r12
000007FEE2550A2B  pop         rbx
000007FEE2550A2C  pop         rbp
000007FEE2550A2D  pop         rcx
000007FEE2550A2E  jmp         rcx

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 14, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9025


Walter Bright <bugzilla@digitalmars.com> changed:

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


--- Comment #1 from Walter Bright <bugzilla@digitalmars.com> 2012-11-14 13:56:02 PST ---
qword means 4 bytes, and you can't push 4 bytes in 64 bit mode. Only 8 bytes.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9025



--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> 2012-11-15 00:01:38 PST ---
(In reply to comment #1)
> qword means 4 bytes, and you can't push 4 bytes in 64 bit mode. Only 8 bytes.

Aggh, that's wrong.

The source code is in src/core/thread.d, and looks like:


----------------------------------------------
                // save current stack state
                push RBP;
                mov  RBP, RSP;
                push RBX;
                push R12;
                push R13;
                push R14;
                push R15;
                push qword ptr GS:[0];
                push qword ptr GS:[8];
                push qword ptr GS:[16];

                // store oldp
                mov [RDI], RSP;
                // load newp to begin context switch
                mov RSP, RSI;

                // load saved state from new stack
                pop qword ptr GS:[16];
                pop qword ptr GS:[8];
                pop qword ptr GS:[0];
                pop R15;
                pop R14;
                pop R13;
                pop R12;
                pop RBX;
                pop RBP;

                // 'return' to complete switch
                pop RCX;
                jmp RCX;
----------------------------------------
So, if you could disassemble the code, it should be GS:[0], but I suspect the assembler made it an offset from the program counter.

To fix,
                 xor RAX,RAX
                 push qword ptr GS:[RAX]
                 push qwork ptr GS:8[RAX]
                 ... etc. ...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9025



--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> 2012-11-15 00:03:49 PST ---
Confirmed, the [RIP] addressing mode is the problem.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9025



--- Comment #4 from github-bugzilla@puremagic.com 2012-11-15 00:42:35 PST ---
Commit pushed to master at https://github.com/D-Programming-Language/druntime

https://github.com/D-Programming-Language/druntime/commit/b524769a1e3385dea96b2271ac14969d537db5c7 fix Issue 9025 - core.thread.Fiber seems to crash on Win64

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9025


Walter Bright <bugzilla@digitalmars.com> changed:

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


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------