Calling Win32's Sleep function from assembly creates access violation error

ぃ、小莉子 提交于 2019-12-23 01:44:14

问题


I'm using MASM and Visual C++, and I'm compiling in x64. This is my C++ code:

// include directive
#include "stdafx.h"
// external functions
extern "C" int Asm();
// main function
int main()
{

    // call asm
    Asm();
    // get char, return success
    _getch();
    return EXIT_SUCCESS;
}

and my assembly code:

extern Sleep : proc
; code segment
.code
    ; assembly procedure
    Asm proc
        ; sleep for 1 second
        mov ecx, 1000   ; ecx = sleep time
        sub rsp, 8      ; 8 bytes of shadow space
        call Sleep      ; call sleep
        add rsp, 8      ; get rid of shadow space
        ; return
        ret
    Asm endp
end

Using breakpoints, I've pinpointed the line of code where the access violation occurs: right after the ret statement in my assembly code.

Extra info:

  • I'm using the fastcall convention to pass my parameters into Sleep (even though it is declared as stdcall), because from what I have read, x64 will always use the fastcall convention.

  • My Asm procedure compiles and executes with no errors when I get rid of the Sleep related code.

  • Even when I try to call Sleep with the stdcall convention, I still get an access violation error.

So obviously, my question is, how do I get rid of the access violation error, what am I doing wrong?

Edit:

This is the generated assembly for Sleep(500); in C++:

mov         ecx,1F4h  
call        qword ptr [__imp_Sleep (13F54B308h)]

This generated assembly is confusing me... it looks like fastcall because it moves the parameter into ecx, but at the same time it doesn't create any shadow space. And I have no clue what this means:
qword ptr [__imp_Sleep (13F54B308h)].

And again, edit, the full disassembly for main.

int main()
{
000000013F991020  push        rdi  
000000013F991022  sub         rsp,20h  
000000013F991026  mov         rdi,rsp  
000000013F991029  mov         ecx,8  
000000013F99102E  mov         eax,0CCCCCCCCh  
000000013F991033  rep stos    dword ptr [rdi]  
Sleep(500); // this here is the asm generated by the compiler!
000000013F991035  mov         ecx,1F4h  
000000013F99103A  call        qword ptr [__imp_Sleep (13F99B308h)]  
// call asm
Asm();
000000013F991040  call        @ILT+5(Asm) (13F99100Ah)  
// get char, return success
_getch();
000000013F991045  call        qword ptr [__imp__getch (13F99B540h)]  
return EXIT_SUCCESS;
000000013F99104B  xor         eax,eax  
}

回答1:


If Asm() were a normal C/C++ function, eg:

void Asm()
{
    Sleep(1000);
}

The following is what my x64 compiler generates for it:

Asm proc
    push rbp          ; re-aligns the stack to a 16-byte boundary (CALL pushed 8 bytes for the caller's return address) as well as prepares for setting up a stack frame
    sub rsp, 32       ; 32 bytes of shadow space
    mov rbp, rsp      ; finalizes the stack frame using the current stack pointer
    ; sleep for 1 second
    mov ecx, 1000     ; ecx = sleep time
    call Sleep        ; call sleep
    lea rsp, [rbp+32] ; get rid of shadow space
    pop rbp           ; clears the stack frame and sets the stack pointer back to the location of the caller's return address
    ret               ; return to caller
Asm endp

MSDN says:

The caller is responsible for allocating space for parameters to the callee, and must always allocate sufficient space for the 4 register parameters, even if the callee doesn’t have that many parameters.

Have a look at the following page for more information about how x64 uses the stack:

Stack Allocation



来源:https://stackoverflow.com/questions/15079997/calling-win32s-sleep-function-from-assembly-creates-access-violation-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!