I am writing the following code that will be able to change the stack of function call. but it always runs into segfault on printf. I debugged the code with assembly, the stack was successfully switched. it is the printf statement that created the segfault and not sure the reason. anybody has any clue what direction i should look into more?
Thanks.
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;
}