1

我正在编写以下代码,它将能够更改函数调用的堆栈。但它总是在 printf 上遇到段错误。我用汇编调试了代码,堆栈切换成功。它是创建段错误的 printf 语句,但不确定原因。任何人都知道我应该进一步研究什么方向?

谢谢。

char stack[4000000*4]; 

void foo(int ad) {
    int i = 100;
    int sum = i*i + ad;
    printf("stack changed to %X\n", stack);
}

/* in this example, foo (and its decendents) live on a new stack */
void change_stack(void *newstack) {
    void *ctx[5]; // Jump buffer for setjmp/longjmp.
    if (0 == __builtin_longjmp(ctx)) {
        ctx[2] = newstack; // switch stack  
        __builtin_longjmp(ctx, 1);/* here stack is switched */
    } else {
    /* now live on new stack, can we pass parameters now ? */
    int ad = 20;
    foo(ad);
    }
}

int main (int argc, char** argv)
{
  int i = 10;
  change_stack(stack);
  printf("return, %d\n", i);
  return 0;
}
4

2 回答 2

2

您无需复制旧堆栈的内容即可切换堆栈。返回时change_stack,结果未定义(例如,它可能跳转到 address NULL,导致段错误)。此外,局部变量之类的东西也将是未定义的。

此外,(假设我们在这里谈论的是 x86),堆栈指针在推送时递减。由于您分配的新堆栈指针是stack数组的基地址(即最低),因此任何推送都会减少该数组之外的指针,也可能导致段错误。

于 2013-07-23T21:02:08.713 回答
1

对于仍然想玩堆栈并在 中遇到 SIGSEGV 的任何人printf,这里有一点很重要:

您可以在 下printf找到 的子程序之一。里面有一条指令:__GI___tcgetattr/path-to-glibc/sysdeps/unix/sysv/linux/tcgetattr.c

movdqa (%rsp),%xmm0

根据这个答案,它从源地址读取 16 字节对齐内存。所以你%rsp在切换堆栈的时候应该保持至少16字节地址对齐,否则你会得到一个SIGSEGV。

于 2021-02-03T13:40:28.077 回答