Bug 110655

Summary: [threads] 32 bit threaded applications crash on amd64 SMP kernel.
Product: Base System Reporter: Martin M. Mladenov <fm>
Component: amd64Assignee: freebsd-amd64 (Nobody) <amd64>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 6.2-RELEASE   
Hardware: Any   
OS: Any   

Description Martin M. Mladenov 2007-03-22 06:10:06 UTC
32 bit applications using the posix threads library crash when calling
pthread_create on an SMP amd64 kernel. This happens with existing binaries,
built natively on a x86 machine, as well as binaries built with cc -m32
on the amd64 system. 

  	 A backtrace for the example in the next section yields:
	
	Core was generated by `pthread-crash'.
	Program terminated with signal 11, Segmentation fault.
	Reading symbols from /usr/lib32/libpthread.so.2...(no debugging symbols found)...done.
	Loaded symbols for /usr/lib32/libpthread.so.2
	Reading symbols from /usr/lib32/libc.so.6...(no debugging symbols found)...done.
	Loaded symbols for /usr/lib32/libc.so.6
	Reading symbols from /libexec/ld-elf.so.1...(no debugging symbols found)...done.
	Loaded symbols for /libexec/ld-elf.so.1
	#0  0x0804d100 in ?? ()
	[New Thread 0x805301408053200 (runnable)]
	[New Thread 0x2809f1c808053000 (runnable)]
	Cannot get thread info: generic error
	(gdb) bt
	#0  0x0804d100 in ?? ()
	
This has been reproduced on two machines with completely different hardware.

Fix: 

Non known.
How-To-Repeat: The problem can be reproduced by the following code (pthread_crash.c):
        #include <stdio.h>
        #include <pthread.h>

        void *thread(void *data) {
          puts("Thread.");
          return NULL;
        }
 
        int main() {
          pthread_t pth;
          void *pv;
          pthread_create(&pth,NULL,thread,NULL);
          pthread_join(pth,&pv);
          return 0;
        }
        The code is compiled with:
        cc -m32 -B/usr/lib32 -pthread -o pthread-crash pthread-crash.c
	This code runs fine when compiled to 64 bit.
Comment 1 Peter Jeremy 2007-03-22 08:34:12 UTC
This problem also affects a non-SMP RELENG_6 kernel from 30th January.

-- 
Peter Jeremy
Comment 2 Andriy Gapon 2007-03-22 10:40:25 UTC
I can confirm this problem for 6.2-RELEASE-p2 amd64 UP system, for both
libpthread and libthr.
i386 simple test programs die with the following symptoms:

[libpthread]
program dies with sigsegv. What's interesting, when run under ktrace my
very short program sometimes completes successfully and sometime crashes
with the same symptoms. There is one strange line in ktrace:

 66443 thr_test CALL  kse_create(0x804b50c,0x1)
 66443 thr_test RET   kse_create 0
 66443 thr_test CALL
__sysctl(0xffffd7b8,0x2,0xffffd7c0,0xffffd7b4,0x2809c63c,0x18)
 66443 thr_test RET   __sysctl 0
 66443 thr_test CALL  __sysctl(0xffffd7c0,0x3,0xffffd86c,0xffffd870,0,0)
 66443 thr_test RET   __sysctl 0
 66443 thr_test CALL  break(0x805c000)
 66443 thr_test RET   break 0
 66443 thr_test CALL  mmap(0xffbfc000,0x101000,0x3,0x400,0xffffffff,0,0,0)
 66443 thr_test RET   mmap -4210688/0xffbfc000
 66443 thr_test CALL  mprotect(0xffbfc000,0x1000,0)
 66443 thr_test RET   mprotect 0
 66443 thr_test CALL  clock_gettime(0,0x8051f5c)
 66443 thr_test RET   clock_gettime 0
 66443 thr_test CALL  fstat(0x1,0xffcfc7c0)
 66443 thr_test RET   fstat 0
=================
 66443 thr_test RET   fork 0
=================
 66443 thr_test PSIG  SIGSEGV SIG_DFL
 66443 thr_test NAMI  "thr_test.core"


[libthr]
the problem here seems to be more obvious, but unexpected (at least for me):
 69654 thr_test CALL  sysarch(0xa,0xffffd700)
 69654 thr_test RET   sysarch 0
 69654 thr_test CALL  sigprocmask(0x3,0xffffd730,0xffffd720)
 69654 thr_test RET   sigprocmask 0
 69654 thr_test CALL  sigaction(0x20,0xffffd6d0,0)
 69654 thr_test RET   sigaction 0
 69654 thr_test CALL  sigprocmask(0x3,0xffffd720,0)
 69654 thr_test RET   sigprocmask 0
 69654 thr_test CALL  sigprocmask(0x1,0x28070a20,0xffffd760)
 69654 thr_test RET   sigprocmask 0
 69654 thr_test CALL  sigprocmask(0x3,0x28070a30,0)
 69654 thr_test RET   sigprocmask 0
 69654 thr_test CALL  _umtx_op
 69654 thr_test RET   _umtx_op -1 errno 78 Function not implemented
 69654 thr_test PSIG  SIGSYS SIG_DFL
 69654 thr_test NAMI  "thr_test.core"


-- 
Andriy Gapon
Comment 3 mladenov.martin 2007-03-26 14:02:56 UTC
Andriy Gapon <avg@icyb.net.ua> wrote:
 > There is one strange line in ktrace:
 > [...]
 > 66443 thr_test RET fork 0

http://lists.freebsd.org/pipermail/freebsd-threads/2007-February/ 
003858.html

According to this post upcalls from KSE are reported as returns from  
fork.
Comment 4 Andriy Gapon 2007-03-29 13:14:36 UTC
Thank you for the pointer, that explains it.
In this view I think that the most probable cause of the crash is binary
incompatibility between KSE structures in amd64 kernel and i386
userland, which is not so unexpected, unfortunately.


-- 
Andriy Gapon
Comment 5 jdw_list 2007-04-04 16:30:50 UTC
Would it be appropriate to follow up on this with the KSE folks or the 
i386-on-amd64 folks?  (Or is there some lucky individual around who 
knows about both?)

This bug is causing a lot of problems for us, and we're happy to do 
whatever we can to help fix it, but both subsystems are pretty much 
opaque to me, so I am at a loss as to how we can help.

Thanks,
Jeff
Comment 6 Jeff Wheelhouse 2007-06-05 21:49:59 UTC
We are still having this problem on 6.2-RELEASE-p4.

I have confirmed that using the example source code above with the thr 
library also does not work:

$ cc -m32 -B/usr/lib32 -lthr -o crash32 crash32.c
$ ./crash32
Bad system call(coredump)
$ cc -lthr -o crash32 crash32.c
$ ./crash32
Thread.
$

Thanks,
Jeff

-- 
Jeff Wheelhouse
jdw@wheelhouse.org
Comment 7 jdw_list 2007-06-06 22:11:25 UTC
Kris Kennaway wrote:
> -m32 doesn't DTRT on FreeBSD, can you confirm that this problem exists
> with a binary compiled by an i386 compiler?

I can.

$ uname -m
i386
$ cc -o crash32-thr crash32.c -lthr
$ cc -o crash32-pthread crash32.c -pthread
$ md5 crash32-thr crash32-pthread
MD5 (crash32-thr) = 91aea395dd2c5798d7cba9a216c4c227
MD5 (crash32-pthread) = a590f1c97e5ec11ddc94fa3d7f4bf167
$ ./crash32-thr
Thread.
$ ./crash32-pthread
Thread.
$

$ uname -m
amd64
$ md5 crash32-thr crash32-pthread
MD5 (crash32-thr) = 91aea395dd2c5798d7cba9a216c4c227
MD5 (crash32-pthread) = a590f1c97e5ec11ddc94fa3d7f4bf167
$ ./crash32-thr
Bad system call(coredump)
$ ./crash32-pthread
Memory fault(coredump)
$

Thanks,
Jeff
Comment 8 Kris Kennaway 2007-06-06 22:42:38 UTC
On Wed, Jun 06, 2007 at 05:11:25PM -0400, Jeff Wheelhouse wrote:
> Kris Kennaway wrote:
> >-m32 doesn't DTRT on FreeBSD, can you confirm that this problem exists
> >with a binary compiled by an i386 compiler?
> 
> I can.
> 
> $ uname -m
> i386
> $ cc -o crash32-thr crash32.c -lthr
> $ cc -o crash32-pthread crash32.c -pthread
> $ md5 crash32-thr crash32-pthread
> MD5 (crash32-thr) = 91aea395dd2c5798d7cba9a216c4c227
> MD5 (crash32-pthread) = a590f1c97e5ec11ddc94fa3d7f4bf167
> $ ./crash32-thr
> Thread.
> $ ./crash32-pthread
> Thread.
> $
> 
> $ uname -m
> amd64
> $ md5 crash32-thr crash32-pthread
> MD5 (crash32-thr) = 91aea395dd2c5798d7cba9a216c4c227
> MD5 (crash32-pthread) = a590f1c97e5ec11ddc94fa3d7f4bf167
> $ ./crash32-thr
> Bad system call(coredump)
> $ ./crash32-pthread
> Memory fault(coredump)
> $

OK, that's useful to know.  I ran into a problem myself that looked
similar but wanted to check first (I didn't isolate it to libthr yet).

Kris
Comment 9 John Baldwin freebsd_committer freebsd_triage 2008-01-18 14:09:39 UTC
6.3 should have freebsd32 compat for libthr and 7.0 should have freebsd32
compat for both libthr and libkse.  Can you retest on newer OSs?

-- 
John Baldwin
Comment 10 Rolf Grossmann 2009-04-17 18:17:20 UTC
It now works for me on FreeBSD 7.1 (GENERIC kernel, which includes 
options SMP, but single cpu):

$ uname -m
i386
$ cc -o crash32-thr crash32.c -lthr
$ cc -o crash32-pthread crash32.c -pthread
$ md5 crash32-thr crash32-pthread
MD5 (crash32-thr) = 30b58238379b4c3496413f22863c2e86
MD5 (crash32-pthread) = 734e11117fbfd63efb06376cf74430d5
$ ./crash32-thr
Thread.
$ ./crash32-pthread
Thread.

$ uname -m
amd64
$ md5 crash32-thr crash32-pthread
MD5 (crash32-thr) = 30b58238379b4c3496413f22863c2e86
MD5 (crash32-pthread) = 734e11117fbfd63efb06376cf74430d5
$ ./crash32-thr
Thread.
$ ./crash32-pthread
Thread.

Can we close this ticket?
Comment 11 John Baldwin freebsd_committer freebsd_triage 2009-04-20 13:47:45 UTC
State Changed
From-To: open->closed

This is fixed in 6.3 (libthr only) and 7.0+ (libthr + libkse).