change the stack on the fly of c program -


i writing following code able change stack of function call. runs segfault on printf. debugged code assembly, stack switched. printf statement created segfault , not sure reason. has clue direction should more?

thanks.

char stack[4000000*4];   void foo(int ad) {     int = 100;     int sum = i*i + ad;     printf("stack changed %x\n", stack); }  /* in example, foo (and decendents) live on new stack */ void change_stack(void *newstack) {     void *ctx[5]; // jump buffer setjmp/longjmp.     if (0 == __builtin_longjmp(ctx)) {         ctx[2] = newstack; // switch stack           __builtin_longjmp(ctx, 1);/* here stack switched */     } else {     /* live on new stack, can pass parameters ? */     int ad = 20;     foo(ad);     } }  int main (int argc, char** argv) {   int = 10;   change_stack(stack);   printf("return, %d\n", i);   return 0; } 

you switch stacks without copying contents of old one. when change_stack returns, results undefined (it may, example, jump address null, causing segfault). also, things local variables undefined.

also, (assuming we're talking x86 here), stack pointer decremented on pushes. since new stack pointer assigned base (i.e. lowest) address of stack array, push decrease pointer outside of array, possibly resulting in segfault.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -