switching up/down the stack with getcontext/setcontext

馋奶兔 提交于 2019-12-07 02:56:33

问题


I am trying to understand if getcontext/setcontext will work correctly in a specific scenario.

I can see how setcontext() can be used to unwind the stack back to a certain place in history.

#include <stdio.h>
#include <ucontext.h>

int  rollback = 0;
ucontext_t context;

void func(void)
{
    setcontext(cp);
}

int main(void)
{
    getcontext(&context);
    if (rollback == 0)
    {
        printf("getcontext has been called\n");
        rollback++;
        func();
    }
    else
    {
        printf("setcontext has been called\n");
    }
}

But I was wondering if after an unwind you can re-wind back to a place that was in the future? I suppose this depends on the getcontext() call captures a copy of the stack and I can't find the exact details in the documentation.

#include <stdio.h>
#include <ucontext.h>

int  rollback     = 0;
int  backToFuture = 0;
ucontext_t context;
ucontext_t futureContext;

void func(void)
{
    // Some complex calc
    if (some-condition)
    {
        getcontext(&futureContext);  // After returning I want to come back
                                     // here to carry on with my work.
        if (backToFuture == 0)
        {
            setcontext(&context);  // rewind to get stuff-done
        }
    }
    // Finishe work
}

int main(void)
{
    getcontext(&context);
    if (rollback == 0)
    {
        printf("getcontext has been called\n");
        rollback++;
        func();

        // eventually always return here.
    }
    else
    {
        printf("setcontext has been called\n");
        // Do specialized work that needed to be done
        // May involve function calls.
        // 
        // I worry that anything the adds new stack frames
        // will disrupt the saved state of futureContext
        //
        // But without detailed information I can not be sure
        // if this is an allowed senario.
        backToFuture = 1;
        setcontext(&futureContext);
    }
}

回答1:


getcontext doesn't copy stack, it only dumps registers (including stack pointer) and a little context data like signal mask, etc.

When you jump down the stack it invalidates the top context. Even if you won't do any function calls think about the signal handler that can execute there. If you want to jump between two stacks you need to makecontext.

I added variable that demonstrates that your code is invalid:

void func(void)
{
    // Some complex calc
    if (1)
    {
        volatile int neverChange = 1;
        getcontext(&futureContext);  // After returning I want to come back
                                     // here to carry on with my work.
        printf("neverchange = %d\n", neverChange);
        if (backToFuture == 0)
        {
            setcontext(&context);  // rewind to get stuff-done
        }
    }
    // Finishe work
}

On my machine it results in:

getcontext has been called
neverchange = 1
setcontext has been called
neverchange = 32767


来源:https://stackoverflow.com/questions/15115480/switching-up-down-the-stack-with-getcontext-setcontext

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!