flat assembler
Message board for the users of flat assembler.
Index
> Main > Swapping the stack |
Author |
|
revolution 09 Nov 2008, 15:43
esp must always be in a stack page else the system exception handling code will fail your function call due to (what it thinks is) a corrupt stack pointer.
|
|||
09 Nov 2008, 15:43 |
|
kas 09 Nov 2008, 15:57
Ah!... thanks revolution.
So the os checks where esp is actually pointing. Damn. Which begs a second question... how does one allocate a 'stack page' from the system? I've looked through my win32 api ref and googled but I don't seem to be able to find any help on stack page allocation. Do you or anyone else have any information (or even code example) about this? |
|||
09 Nov 2008, 15:57 |
|
revolution 09 Nov 2008, 16:01
kas wrote: how does one allocate a 'stack page' from the system? Code: sub esp,4092 ;but not more. Since we need to activate the page guard exception push eax ;and repeat until you have the desired stack space |
|||
09 Nov 2008, 16:01 |
|
kas 09 Nov 2008, 16:03
Thanks again for a very fast answer. I'll try using your suggestion.
Kas. |
|||
09 Nov 2008, 16:03 |
|
kas 09 Nov 2008, 16:22
Hi again,
Ok I've tried using that strategy in the example and the c call works. However, am I right in understanding that (say for a simple user threading scheme) where one has multiple stacks - all user threads must share an (extended) system default stack? In other words - is there no way to allocate separate and continuous user thread structures - like any other kind of data definition/allocation? Thanks, Kas |
|||
09 Nov 2008, 16:22 |
|
baldr 09 Nov 2008, 20:20
revolution,
Do you mean that default #PF handler commits guard pages only in thread's stack range? May be custom SE handler will do the trick, I'll try to write an example. |
|||
09 Nov 2008, 20:20 |
|
kas 09 Nov 2008, 21:13
Hi revolution,
Thanks for replying again. I wasn't thinking about full OS threads - just a cooperative psuedo-threading setup. i.e. where 2 or more procedures (in the same process/thread) swapped control between each other. Ideally I'd want each psuedo-thread would have its own little stack- defined in it's own structure. So the question I was asking was whether - in order to do this - we had to partition the OS's default stack or whether there was some way to ask the OS "hey now use this bit of memory I've allocated over here as your stack". Kas. |
|||
09 Nov 2008, 21:13 |
|
baldr 09 Nov 2008, 21:33
|
|||
09 Nov 2008, 21:33 |
|
kas 09 Nov 2008, 21:53
baldr, it's a fascinating demo... though I just can't understand why the calls you make (in that demo) don't result in the same problem as my earlier posted example?
Why doesn't the OS (as revolution explained) notice that you're not using the OS defined stack and treat it as a possible corrupt stack access - instead of just working !? |
|||
09 Nov 2008, 21:53 |
|
baldr 09 Nov 2008, 22:35
kas,
OS doesn't. And doesn't have to. It just provide you growing stack, no matter how are you using it (or don't use at all). Your code is failed because of alignment. Code: test_string2 db "I'm using the OLD stack!",13,10,0 align 4; here goes the important part SYSTEM_EBP dd 0 |
|||
09 Nov 2008, 22:35 |
|
asmcoder 09 Nov 2008, 22:58
[content deleted]
Last edited by asmcoder on 14 Aug 2009, 14:55; edited 1 time in total |
|||
09 Nov 2008, 22:58 |
|
kas 09 Nov 2008, 22:59
Oh my god... I thought I knew what I was doing... and that 'align' was just useful to improve CPU read/throughput speed i.e. performance.
It never occured to me that it (or rather the lack of...) might directly cause a fault!!! Thanks again baldr for your help, Kas. |
|||
09 Nov 2008, 22:59 |
|
baldr 09 Nov 2008, 23:07
kas,
Probably not fault (CPU-wise) at all, may be msvcrt does something strange. asmcoder, The same as above. Luckily your strings align new_stack, ret misaligns it. |
|||
09 Nov 2008, 23:07 |
|
revolution 10 Nov 2008, 01:35
Each thread has only one stack allocated by the OS. The OS will expand the stack downwards, as it is used, by 4096 bytes at a time using the guard page mechanism of the CPU.
The reason that you should use the OS stack (and not your own piece of memory) is because of the exception handling. Many (not all) OS functions will allocate a new SEH frame before doing the operations. The SEH allocation code will check that ESP is within the proper OS allocated stack and fail the call if it finds otherwise. If you are lucky to call a function that does not make an SEH frame then you can use your own stack anywhere in memory, but your code could easily break without warning on another OS version or after the next patch Tuesday or service pack update. And yes, of course you should always align your memory access no matter if they are on the stack or not. |
|||
10 Nov 2008, 01:35 |
|
kas 10 Nov 2008, 02:31
Thanks for the info revolution about how OS functions and stack interdepend.
The align advice fixed the code - but I guess it was just a matter of time before this issue bit me too! |
|||
10 Nov 2008, 02:31 |
|
asmcoder 10 Nov 2008, 09:57
[content deleted]
|
|||
10 Nov 2008, 09:57 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.