Using Heap for Stack, possible?

Can i have several "stacks" on the heap and switch between them?
If possible could someone give a hint how to do it?

Comments

  • : Can i have several "stacks" on the heap and switch between them?
    : If possible could someone give a hint how to do it?
    :
    :

    Nothing to prevent you from doing that - alteast not locally (= at function level).
    Basically, you'd just set ESP to a heap-memory address. This will work as long as the Stack Segment is the same as the Data Segment (which is so for Windows).

    Note that you won't have any overflow checks - so it's up to the function itself to make sure it has enough memory allocated.

    So, pseudo-code:
    [code]
    Enter function
    Save original ESP on the heap
    Move the new stack address to ESP
    ...
    Use stack as you would normally for the rest of the function
    ...
    Restore the original ESP (from the heap)
    Return from function
    [/code]

    EDIT: Why would you want to do this? The function will become non-reentrant (eg no recursive calls). Any functions called from this function will work properly (as long as there is enough heap-stackmemory). However, you could not return from the function without restoring the stack.
    If you'd do this in some kind of main function:
    [code]
    _main:
    ... Set stack to an allocated location on the heap
    call _Function1
    ... Set stack to another allocation location on the heap
    call _Function2
    call _Function3
    ... Restore stack to what it was
    call _Function4
    ...
    ret
    [/code]
    Then each of the functions would have a different location for the stack, but I could not imagine what you would do with that, because before any function returns, the stack is 'unrolled'. That means that the stack is as clean after Function1 as it was before Function1.


    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • : EDIT: Why would you want to do this?
    My guess: to implement continuations. Beautiful. Evil. :-)

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories