flat assembler
Message board for the users of flat assembler.

Index > Windows > Heap Allocation.

Author
Thread Post new topic Reply to topic
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 13 Dec 2011, 08:54
Hello everyone!

I'm interested if there is some way to allocate heap without api calls ? Like stack allocation:
Code:
push ebp
mov ebp,esp
sub esp,n ;n = random number    

Could be heap allocated something like this ? I'm don't know enough to see how API calls work inside so.. just asking Smile Thank you.
Post 13 Dec 2011, 08:54
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20517
Location: In your JS exploiting you and your system
revolution 13 Dec 2011, 08:58
There are two problems with using the stack for heap space.

1) Your stack size is limited at thread startup. You can't arbitrarily expand it at will later.
2) You need to touch the stack in 4kB intervals to trigger the page guard so the OS will commit memory.

As long as you understand those things then you can use the stack for heap space.
Post 13 Dec 2011, 08:58
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 13 Dec 2011, 09:15
You don't understand me, I meant, how to manually do something like this API calls:
Code:
VirtualAlloc/Ex, malloc, etc..    
Post 13 Dec 2011, 09:15
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 13 Dec 2011, 09:17
Using stack as a variables memory is common practice. But there is a one big disadvantage - you can't free and resize the memory other way than LIFO. If you defined A, B, C, D, then you can resize/free only D. If you want to resize B, then you must free D and C, resize B and allocate C and D again.
Also, these variables can be only local and they can be changed only on the same nesting level where they have been defined.
The big advantage is that there is no fragmentation in the heap.
Post 13 Dec 2011, 09:17
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20517
Location: In your JS exploiting you and your system
revolution 13 Dec 2011, 09:18
Overflowz wrote:
You don't understand me, I meant, how to manually do something like this API calls:
Code:
VirtualAlloc/Ex, malloc, etc..    
You can't. The OS controls memory allocation. If your app needs memory it needs to ask for it in some way. Extending the stack is one way to ask for memory, any other type of memory and you must ask the OS.
Post 13 Dec 2011, 09:18
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 13 Dec 2011, 09:19
I get it, thank you guys Smile
Post 13 Dec 2011, 09:19
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1694
Location: Toronto, Canada
AsmGuru62 13 Dec 2011, 18:03
I just want to add that 'n' cannot be a random number - it must be a number divisible by 4 (a size of a DWORD). Some Win API may fail if you call them with mis-aligned ESP.

Also, 4Kb pages remark is applied if stack is not commited.
In FASM, you can add a line at the top of source (after PE GUI 4.0 declaration):
Code:
stack 100000h,100000h
    

It will reserve AND commit stack space for 1Mb, so you can allocate more than a page of room if needed.
Post 13 Dec 2011, 18:03
View user's profile Send private message Send e-mail Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 13 Dec 2011, 19:02
Is there any API call to reserve more stack size ? on runtime I mean.
Post 13 Dec 2011, 19:02
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20517
Location: In your JS exploiting you and your system
revolution 13 Dec 2011, 19:11
Overflowz wrote:
Is there any API call to reserve more stack size ? on runtime I mean.
You can create a new thread with a new stack size, but AFAIK there is nothing that will alter the currently running thread's stack reservation.
Above, I wrote:
1) Your stack size is limited at thread startup. You can't arbitrarily expand it at will later.
Post 13 Dec 2011, 19:11
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1694
Location: Toronto, Canada
AsmGuru62 13 Dec 2011, 19:30
A good question may be is: "why so much stack is needed?" - which will indicate a code design issue(s). I have built significant programs in FASM (my OOP IDE as an example) and I never needed more than 32Kb of stack and even that was a lot.
Post 13 Dec 2011, 19:30
View user's profile Send private message Send e-mail Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 14 Dec 2011, 08:07
I was thinking to use stack instead of heap.. Just I'm curious, nothing else.
Post 14 Dec 2011, 08:07
View user's profile Send private message 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.