Question regarding DLL-export and problems with the stack menu

User Tag List

Results 1 to 9 of 9
  1. #1
    schlumpf's Avatar Retired Noggit Developer

    Reputation
    755
    Join Date
    Nov 2006
    Posts
    2,759
    Thanks G/R
    0/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question regarding DLL-export and problems with the stack

    Why does

    Code:
    extern "C" __declspec(dllexport) bool ScanFake(int *a, int, int, int)
    {
    	*a = 0;
    	return true;
    }
    compile to
    Code:
    sub proc near
    arg_0= dword ptr  4
    
    mov     eax, [esp+arg_0]
    and     dword ptr [eax], 0
    mov     al, 1
    retn
    sub endp
    instead of

    Code:
    sub proc near
    arg_0= dword ptr  4
    
    push    ebp
    mov     ebp, esp
    mov     ecx, [ebp+arg_0]
    mov	[ecx],0
    mov     al, 1
    pop     ebp
    retn
    sub endp
    Any thoughts how I can get the compiler (VC++) to modify the stack so the four pushed ints will be off it? As soon as I add a __asm{ add esp, 16 }, it will the stack management. But that will lead to ****ing it up again. (Since it will pop too much then.

    Code:
    push    ebp
    mov     ebp, esp
    mov     eax, [ebp+arg_0]
    and     dword ptr [eax], 0
    add     esp, 10h
    mov     al, 1
    pop     ebp
    retn
    Well: Which parameters do I have to change to get the compiler to add push ebp etc.?

    Question regarding DLL-export and problems with the stack
  2. #2
    corderoy's Avatar Member
    Reputation
    7
    Join Date
    May 2008
    Posts
    17
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the term you are looking for is "calling convention", as well as "__cdecl" and "__stdcall". Check those out at google or here
    to get more info.

  3. #3
    schlumpf's Avatar Retired Noggit Developer

    Reputation
    755
    Join Date
    Nov 2006
    Posts
    2,759
    Thanks G/R
    0/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, but why is it automatically changing as soon as I modify the stack by hand?

    Thanks for giving me that hint. Forgot about it since I use a macro to do the type of the function.

  4. #4
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1356
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's probably not bothering with a stack frame because the function is so tiny that it can be optimized out. The point of a stack frame is to make it easier to access both local vars and params. You don't have any local vars so it can just grab the params off ESP without backing it up and doing a sub for you local vars.

    Argh afk. I'll finish explaining later.

  5. #5
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1356
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right. First off, if you want to do any manual stack modifications you MUST use __declspec(naked) otherwise you'll **** up a whole range of things.

    Also, the first function is output the way it is because of optimizations. It dereferences the pointer and uses an AND to zero it out. Then it just returns true. It doesn't bother doing and movs/subs because there's no need, the function is just too simple to make it worthwhile.

    EDIT: Also, you don't need to change params to get a stack frame. Try adding about 10-20 bytes of local variabels and see how it changes. Optimization settings will make a difference too.

  6. #6
    schlumpf's Avatar Retired Noggit Developer

    Reputation
    755
    Join Date
    Nov 2006
    Posts
    2,759
    Thanks G/R
    0/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I can understand optimization, but if I pass four arguments, why wont it pop them again? When will it add code to pop them? As soon, as I change the value of them or use them for something?

    The problem was, that I was unable to change the call resulting in four pushs and no pops.

  7. #7
    kynox's Avatar Account not activated by Email
    Reputation
    830
    Join Date
    Dec 2006
    Posts
    888
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you not read corderoy's post when he directed you to calling conventions?

    __cdecl requires the caller to fix the stack, __stdcall requires the callee to fix the stack.

  8. #8
    schlumpf's Avatar Retired Noggit Developer

    Reputation
    755
    Join Date
    Nov 2006
    Posts
    2,759
    Thanks G/R
    0/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As soon as I tried to fix the stack, it automatically changed to stdcall. This was my problem.

  9. #9
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1356
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's because you're dumping inline ASM into a function that modifies the stack without ensuring that it's 'legal' to do so. Not only were you not marking the calling convention correctly, if you want to set up a custom stack frame you need to mark the function as naked.

    I see what you meant now by the original function but as kynox said, you were probably using __cdecl, which is the default C++ calling convention for most major compilers. If you want to use __stdcall declare your function like this:

    void __stdcal Foo(int P1, int P2, int P3, int P4)
    {
    // code goes here
    }

    Fairly simple stuff. Its all documented under the calling conventions section of MSDN or any decent reversing book.


Similar Threads

  1. fixing eyes and teeth problem with the raptor to devilsaur
    By CoolG in forum WoW ME Questions and Requests
    Replies: 2
    Last Post: 06-28-2009, 03:14 AM
  2. [Question] Problems with the new WoW update.
    By Aradroth in forum WoW ME Questions and Requests
    Replies: 6
    Last Post: 01-23-2008, 09:40 PM
  3. [Question] Having problems with the fix(23)
    By Jo_Vo in forum WoW ME Questions and Requests
    Replies: 0
    Last Post: 11-26-2007, 08:06 AM
  4. problem with the website
    By aznboy in forum World of Warcraft Emulator Servers
    Replies: 2
    Last Post: 11-09-2007, 11:05 PM
  5. Problem with the vote thing
    By Cyrex in forum Community Chat
    Replies: 0
    Last Post: 03-01-2007, 11:59 PM
All times are GMT -5. The time now is 05:13 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search