Unix Programming - Why the setjmp and longjmp I wrote can not work?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > November 2005 > Why the setjmp and longjmp I wrote can not work?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Why the setjmp and longjmp I wrote can not work?
Zheng Da

2005-11-08, 6:29 pm

#include <stdio.h>

typedef struct __myjmp_buf
{
int efp;
int epc;
}myjmp_buf;


int mysetjmp(myjmp_buf env)
{
int reval=0;
__asm__("movl %%ebp,%0":"=r"(env[0].efp));
__asm__("movl $1f,%0\n\t"
"1:"
:"=r"(env[0].epc));
return reval;
}

void mylongjmp(myjmp_buf env , int val)
{
__asm__("movl %1,-4(%0)\n\t"
"movl %0,%%ebp\n\t"
"jmp %2"
::"r"(env[0].efp),
"r"(val),
"r"(env[0].epc));
}

myjmp_buf buf;

int test()
{
int i=0;
i++;
mylongjmp(buf , 1);
return 0;
}

int main()
{
if(mysetjmp(buf))
{
printf("return success\n");
}
printf("pc:%x,fp:%x\n" , buf[0].epc , buf[0].efp);
printf("main address:%x\n" , main);
test();
exit(0);
}
When the computer execute jmp %2, and gives me a segment fault.
I do not know why the address I save is a invalid address, and wonder
to know what should I do if I want that mysetjmp and mylongjmp can work

like setjmp and longjmp.
My platform is x86, the system I use is Fedora core1, and I compile the

program with gcc.
By the way, there is no error to compile it in my system but a warning
"indirect jmp
without '*' ", but I do not know what it means

Paul Pluzhnikov

2005-11-08, 6:29 pm

"Zheng Da" <zhengda1936@gmail.com> writes:

> #include <stdio.h>


Your program as posted doesn't compile:

junk.c: In function `mysetjmp':
junk.c:13: error: subscripted value is neither array nor pointer
junk.c:16: error: subscripted value is neither array nor pointer
.... etc ...

Fixing these ...

> When the computer execute jmp %2, and gives me a segment fault.


Nope. It crashes *before* it reaches that instruction.
Learn to use the debugger to figure that out.

> I do not know why the address I save is a invalid address, and wonder
> to know what should I do if I want that mysetjmp and mylongjmp can work


The 'mylongjmp' can't be reasonably implemented with inline assembly.
You'll have to use "pure" assembly if you are to have any chance
of success. Also note that setjmp/longjmp must save *all* registers.
At a minimum you must save/resore %esp in addition to %ebp.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Zheng Da

2005-11-08, 6:29 pm

> The 'mylongjmp' can't be reasonably implemented with inline assembly.
> You'll have to use "pure" assembly if you are to have any chance
> of success. Also note that setjmp/longjmp must save *all* registers.
> At a minimum you must save/resore %esp in addition to %ebp.

I never wrote the whole program by pure assembly before, and it seems
easier with inline assembly.
By the way, I must save all registers? I read the code of setjmp in
glibc, and did not find setjmp save all registers. But I did not find
longjmp in glibc, so I tried to write them by myself.
I have modified my program, and it seems to run well.But I am not sure
that it can really work well in all conditions.

typedef struct __myjmp_buf
{
int efp;
int epc;
int esp;
}myjmp_buf[1];

int mysetjmp(myjmp_buf env)
{
__asm__("movl (%%ebp),%0":"=r"(env[0].efp));
__asm__("movl %%esp,%0":"=r"(env[0].esp));
__asm__("movl 4(%%ebp),%0":"=r"(env[0].epc));
return 0;
}

void mylongjmp(myjmp_buf env , int val)
{
__asm__(
"movl %0,%%ebp\n\t"
"movl %3,%%esp\n\t"
"leave\n\t"
"movl %1,%%eax\n\t"
"jmp %2"
::"r"(env[0].efp),
"r"(val),
"r"(env[0].epc),
"r"(env[0].esp));
}

myjmp_buf buf;

int test()
{
int i=0;
i++;
mylongjmp(buf , 1);
return 0;
}

int test1()
{
int reval;

if(reval=mysetjmp(buf)){
printf("return success,return value is %d\n" , reval);
}
else
test();
}

int main()
{
test1();
exit(0);
}

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com