From owner-freebsd-hackers Sun Jan 3 11:40:27 1999 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id LAA29285 for freebsd-hackers-outgoing; Sun, 3 Jan 1999 11:40:27 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from apollo.backplane.com (apollo.backplane.com [209.157.86.2]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id LAA29277 for ; Sun, 3 Jan 1999 11:40:25 -0800 (PST) (envelope-from dillon@apollo.backplane.com) Received: (from dillon@localhost) by apollo.backplane.com (8.9.1/8.9.1) id LAA51723; Sun, 3 Jan 1999 11:39:37 -0800 (PST) (envelope-from dillon) Date: Sun, 3 Jan 1999 11:39:37 -0800 (PST) From: Matthew Dillon Message-Id: <199901031939.LAA51723@apollo.backplane.com> To: Chia-liang Kao Cc: freebsd-hackers@FreeBSD.ORG Subject: Re: setjmp/longjmp corrupts stack? Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG :Hi, : :I have a little program attached below causing SIGSEGV. But the :program works out (partially, see below) dramatically if the function :being called in main() (haha()) changes to hehe(). : :In my trace record, the stack corrupted right after longjmp to j2. :But if I change the haha() in main() to hehe(), although the result is :as expected, the stack is somewhat corrupted too. Like the following: :... What you are doing is totally illegal. You cannot push down the stack, setjmp, return from that context, then longjmp() back down into it. The only legal way to use setjmp()/longjmp() is for the context that you setjmp() to remain *valid* from the point setjmp() is called to the point longjmp() is called to get back to it. In the code below you setjmp j1 in main, then call haha which setjmp's j2 and longjmp's back to j1 (which is ok), but that means you've popped the haha() context. Then your main function longjmp's back to j2, inside haha(), which is now totally illegal because that context was popped when you longjmp'd out of it the first time. -Matt :Regards, :CLK : :====================== :#include :#include : :jmp_buf j1, j2; : :void :haha() :{ : int r; : static int cnt; : /* ... */ : printf("send\n"); : if(!(r =setjmp(j2))) { : /* go back */ : longjmp(j1, ++cnt); : } : /* resume */ : printf("resume\n"); : return; :} : :void :hehe() :{ : haha(); :} : :int :main() :{ : int r; : if((r = setjmp(j1))) { : printf("jmp %d\n", r); : if(r == 1) : longjmp(j2, 1); : else : exit(0); : } : printf("main\n"); : haha(); : printf("after longjmp\n"); : return 0; :} : :To Unsubscribe: send mail to majordomo@FreeBSD.org :with "unsubscribe freebsd-hackers" in the body of the message : Matthew Dillon Engineering, HiWay Technologies, Inc. & BEST Internet Communications & God knows what else. (Please include original email in any response) To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message