From dima@bog.msu.su  Sun Mar  8 17:44:58 1998
Received: from sunny.bog.msu.su (sunny.bog.msu.su [158.250.20.1])
          by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id RAA18204
          for <FreeBSD-gnats-submit@freebsd.org>; Sun, 8 Mar 1998 17:44:55 -0800 (PST)
          (envelope-from dima@bog.msu.su)
Received: from localhost (dima@localhost)
         by sunny.bog.msu.su (8.8.8/8.8.8) with SMTP id EAA05707
         for <FreeBSD-gnats-submit@freebsd.org>; Mon, 9 Mar 1998 04:44:40 +0300 (MSK)
         (envelope-from dima@bog.msu.su)
Message-Id: <Pine.GSO.3.95.980309044220.5653A-100000@sunny.bog.msu.su>
Date: Mon, 9 Mar 1998 04:44:39 +0300 (MSK)
From: Dmitry Khrustalev <dima@bog.msu.su>
To: FreeBSD-gnats-submit@freebsd.org
Subject: setjmp wrapper in libc_r is broken

>Number:         5953
>Category:       bin
>Synopsis:       setjmp wrapper in libc_r is broken
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    jb
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Mar  8 17:50:01 PST 1998
>Closed-Date:    Tue Apr 28 22:52:46 PDT 1998
>Last-Modified:  Tue Apr 28 22:56:00 PDT 1998
>Originator:     Dmitry Khrustalev
>Release:        FreeBSD 3.0-CURRENT i386
>Organization:
>Environment:

	FreeBSD 3.0-CURRENT i386, using cc -pthread
        
>Description:

        environment saved by setjmp wrapper in lib/libc_r/uthread/uthread_setjmp.c is invalid after return from wrapper.
	
>How-To-Repeat:

this example will crash when compiled using cc -pthread:

#include <setjmp.h>

jmp_buf jb;
main () {
    setjmp(jb);
    longjmp(jb, 0);
}

>Fix:
	
    do not wrap setjmp and longjmp, do not rename setjmp/longjmp/
    sigsetjmp/siglongjmp then building libc_r.

rm lib/libc_r/uthread/uthread_setjmp.c
rm lib/libc_r/uthread/uthread_longjmp.c

diff -u -r lib.ORIG/libc/i386/gen/setjmp.S lib/libc/i386/gen/setjmp.S
--- lib.ORIG/libc/i386/gen/setjmp.S	Mon Mar  9 04:28:38 1998
+++ lib/libc/i386/gen/setjmp.S	Mon Mar  9 04:20:30 1998
@@ -54,11 +54,7 @@
 #include "DEFS.h"
 #include "SYS.h"
 
-#ifdef _THREAD_SAFE
-ENTRY(_thread_sys_setjmp)
-#else
 ENTRY(setjmp)
-#endif
 	PIC_PROLOGUE
 	pushl	$0
 #ifdef _THREAD_SAFE
@@ -81,11 +77,7 @@
 	xorl	%eax,%eax
 	ret
 
-#ifdef _THREAD_SAFE
-ENTRY(_thread_sys_longjmp)
-#else
 ENTRY(longjmp)
-#endif
 	movl	4(%esp),%edx
 	PIC_PROLOGUE
 	pushl	24(%edx)
diff -u -r lib.ORIG/libc/i386/gen/sigsetjmp.S lib/libc/i386/gen/sigsetjmp.S
--- lib.ORIG/libc/i386/gen/sigsetjmp.S	Mon Mar  9 04:28:47 1998
+++ lib/libc/i386/gen/sigsetjmp.S	Mon Mar  9 04:21:46 1998
@@ -59,11 +59,7 @@
  *	use sigreturn() if sigreturn() works.
  */
 
-#ifdef _THREAD_SAFE
-ENTRY(_thread_sys_sigsetjmp)
-#else
 ENTRY(sigsetjmp)
-#endif
 	movl	8(%esp),%eax
 	movl	4(%esp),%ecx 
 	movl	%eax,32(%ecx)
@@ -91,11 +87,7 @@
 	xorl	%eax,%eax
 	ret
 
-#ifdef _THREAD_SAFE
-ENTRY(_thread_sys_siglongjmp)
-#else
 ENTRY(siglongjmp)
-#endif
 	movl	4(%esp),%edx
 	cmpl	$0,32(%edx)
 	jz	2f
diff -u -r lib.ORIG/libc_r/uthread/Makefile.inc lib/libc_r/uthread/Makefile.inc
--- lib.ORIG/libc_r/uthread/Makefile.inc	Mon Mar  9 04:27:47 1998
+++ lib/libc_r/uthread/Makefile.inc	Mon Mar  9 04:30:27 1998
@@ -51,7 +51,6 @@
 	uthread_kern.c \
 	uthread_kill.c \
 	uthread_listen.c \
-	uthread_longjmp.c \
 	uthread_mattr_init.c \
 	uthread_mattr_kind_np.c \
 	uthread_multi_np.c \
@@ -70,7 +69,6 @@
 	uthread_self.c \
 	uthread_sendto.c \
 	uthread_seterrno.c \
-	uthread_setjmp.c \
 	uthread_setprio.c \
 	uthread_setsockopt.c \
 	uthread_shutdown.c \
diff -u -r lib.ORIG/libc_r/uthread/uthread_create.c lib/libc_r/uthread/uthread_create.c
--- lib.ORIG/libc_r/uthread/uthread_create.c	Mon Mar  9 04:27:45 1998
+++ lib/libc_r/uthread/uthread_create.c	Mon Mar  9 04:34:42 1998
@@ -107,7 +107,7 @@
 			}
 
 			/* Initialise the jump buffer: */
-			_thread_sys_setjmp(new_thread->saved_jmp_buf);
+			setjmp(new_thread->saved_jmp_buf);
 
 			/*
 			 * Set up new stack frame so that it looks like it
diff -u -r lib.ORIG/libc_r/uthread/uthread_kern.c lib/libc_r/uthread/uthread_kern.c
--- lib.ORIG/libc_r/uthread/uthread_kern.c	Mon Mar  9 04:27:46 1998
+++ lib/libc_r/uthread/uthread_kern.c	Mon Mar  9 04:34:19 1998
@@ -101,7 +101,7 @@
 		_thread_run->sig_saved = 1;
 	}
 	/* Save the state of the current thread: */
-	else if (_thread_sys_setjmp(_thread_run->saved_jmp_buf) != 0) {
+	else if (setjmp(_thread_run->saved_jmp_buf) != 0) {
 		/* Unblock signals (just in case): */
 		_thread_kern_sig_unblock(0);
 
@@ -810,7 +810,7 @@
 				 * was context switched out (by a longjmp to
 				 * a different thread): 
 				 */
-				_thread_sys_longjmp(_thread_run->saved_jmp_buf, 1);
+				longjmp(_thread_run->saved_jmp_buf, 1);
 			}
 
 			/* This point should not be reached. */

>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->jb 
Responsible-Changed-By: hoek 
Responsible-Changed-When: Sun Mar 22 21:52:19 PST 1998 
Responsible-Changed-Why:  
This is very similar to i386/4826, of course, except the originator 
is running -current (although i386/4826 is supposely not release-specific). 

Thready stuff. 
State-Changed-From-To: open->closed 
State-Changed-By: jb 
State-Changed-When: Tue Apr 28 22:52:46 PDT 1998 
State-Changed-Why:  
The wrappers for setjmp etc will be removed in a commit (soon!), but 
using them in a threaded program when the thread library uses this 
mechanism to schedule threads is likely to end in tears. Please wait 
for kernel threads for that! 
>Unformatted:
