flat assembler
Message board for the users of flat assembler.

Index > Main > malloc stdcall?!

Author
Thread Post new topic Reply to topic
zxcv
Guest




zxcv
always (ehm, for to weeks) i was using malloc in cdecl.
But today, my program crashed and i didnt know why for a long time. I cutted almost all my code, and this is what left:
Image
malloc is from msvcrt, and i thought that all functions from there are cdecl.
Post 23 Dec 2007, 06:07
Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17342
Location: In your JS exploiting you and your system
revolution
I strongly doubt that malloc is anything but cdecl. Perhaps a mistake in your code elsewhere is causing your problem?
Post 23 Dec 2007, 12:16
View user's profile Send private message Visit poster's website Reply with quote
zxcv
Guest




zxcv
no!


Code:
format pe console
section '.code' code executable readable
push 400
call [malloc]
call [malloc]
add esp, 4
ret
section '.idata' import data readable
dd 0,0,0,RVA msvcrt_name,RVA msvcrt_table
dd 0,0,0,0,0
msvcrt_table:
malloc dd RVA _malloc
dd 0
msvcrt_name db 'msvcrt.dll',0
_malloc db 0,0,'malloc',0    


Crashed! Windows error reporting and drwtsn32.exe
Post 23 Dec 2007, 12:43
Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17342
Location: In your JS exploiting you and your system
revolution
Works for me! WinXP32 SP2. Even Olly show no stack adjustment from malloc.

Maybe you have a virus?
Post 23 Dec 2007, 12:51
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Works for me too.

Original code (in first post) doesn't remove arguments from stack.
Post 23 Dec 2007, 13:09
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
BTW, it is a mistake to reuse parameters, C language does not forbids you from using the arguments as variables on your functions so the second call to malloc is not guaranteed to recieve 400. This does not explains the error but I wanted to tell it anyway Razz


Last edited by LocoDelAssembly on 23 Dec 2007, 17:24; edited 1 time in total
Post 23 Dec 2007, 15:33
View user's profile Send private message Reply with quote
zxcv
Guest




zxcv
why i cant reuse parameter if function is cdecl? please explain it to me why
many times i used _sleep and it worked.
Post 23 Dec 2007, 16:59
Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Quote:
why i cant reuse parameter if function is cdecl?

Because function can overwrite the value on stack, like:
Code:
malloc:
    add [esp+4], sizeof_block_header
    ...
    


Quote:
please explain it to me why many times i used _sleep and it worked.

elementary logic: if it works limited number of times, that doesn't mean it will work always. Smile
Post 23 Dec 2007, 17:11
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
[edit]Same explanation as vid but in HLL[/edit]
Suppose that the function you call is the following
Code:
void do_nothing_but_overwrite_params(int param)
{
  param += param;

  return;
}    


If later you do
Code:
push 5
call do_nothing_but_overwrite_params
call do_nothing_but_overwrite_params
    

The first call recieves 5 and the second 10. However not all functions writes to its parameters but you can't rely on that because the prototype is just "type func(type param, etc)" and nothing there says that the parameter on the stack will be preserved.
Post 23 Dec 2007, 17:22
View user's profile Send private message Reply with quote
zxcv
Guest




zxcv
ok, but does any of winapi functions contain this trap?
Post 24 Dec 2007, 09:24
Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17342
Location: In your JS exploiting you and your system
revolution
zxcv wrote:
ok, but does any of winapi functions contain this trap?
Firstly you are using the crt so the api is not relevant[1]. But ignoring that and assuming you mean the crt, then one would have to respond by asking which version? Each separate build may behave differently from the others. The only way to know would be to test each and every version one-by-one, this includes future versions also. A tedious task. It is much easier to make one's code conform the the standards and not worry about unexpected side effects.

[1] There are two api functions that are cdecl, one star will be given to anyone that can name them from memory without checking the docs. Smile

Edit by Loco: The "contest" is here
Post 24 Dec 2007, 10:18
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.