Why the setjmp and longjmp I wrote can not work?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Why the setjmp and longjmp I wrote can not work?




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-08-05 11: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






[ Post a follow-up to this message ]



    Re: Why the setjmp and longjmp I wrote can not work?  
Paul Pluzhnikov


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-08-05 11: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.





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-08-05 11: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);
}






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 03:30 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register