From nobody@FreeBSD.ORG Fri Jun 18 16:37:21 1999
Return-Path: <nobody@FreeBSD.ORG>
Received: by hub.freebsd.org (Postfix, from userid 32767)
	id 94AC5150E9; Fri, 18 Jun 1999 16:37:21 -0700 (PDT)
Message-Id: <19990618233721.94AC5150E9@hub.freebsd.org>
Date: Fri, 18 Jun 1999 16:37:21 -0700 (PDT)
From: sheller@paymentnet.com
Sender: nobody@FreeBSD.ORG
To: freebsd-gnats-submit@freebsd.org
Subject: Segmentation violation when invoking JNI call to C from Java with PTHREADS
X-Send-Pr-Version: www-1.0

>Number:         12286
>Category:       i386
>Synopsis:       Segmentation violation when invoking JNI call to C from Java with PTHREADS
>Confidential:   no
>Severity:       critical
>Priority:       high
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Jun 18 16:40:01 PDT 1999
>Closed-Date:    Wed May 30 23:14:47 PDT 2001
>Last-Modified:  Wed May 30 23:34:53 PDT 2001
>Originator:     Steve Heller
>Release:        FreeBSD 3.1 RELEASE
>Organization:
PaymentNet, Inc.
>Environment:
FreeBSD yoda.paymentnet.com 3.1-RELEASE FreeBSD 3.1-RELEASE #0: Mon Feb 15 11:08:08 GMT 1999 jkh@usw3.freebsd.org:/usr/src/sys/compile/GENERIC  i386

% gcc --version
2.7.2.1


>Description:
When I link a static library which calls pthread routines into a
shared object file with gcc -shared -pthread, and then run the 
java compiled program which calls it through JNI with JDK 1.1.8
(green threads), (I had to add -lc_r to the link flags in order
to get pthread_mutex_lock() to be found when linking the .so file),
I get the following Segmentation violation:

(cd jni; java pntransjava test.paymentnet.com 443 "TRXTYPE=S&TENDER=C&PWD=g0edel
1685&USER=pnprodtest&ACCT=5105105105105100&EXPDATE=1299&AMT=27.33" 30;  cd ..)
SIGSEGV   11*  segmentation violation

Full thread dump:
    "Finalizer thread" (TID:0x28454210, sys_thread_t:0x29258f00, state:R) prio=1
    "Async Garbage Collector" (TID:0x28454258, sys_thread_t:0x29237f00, state:R)
 prio=1
    "Idle thread" (TID:0x284542a0, sys_thread_t:0x29216f00, state:R) prio=0
    "Clock" (TID:0x28454088, sys_thread_t:0x291f5f00, state:CW) prio=12
    "main" (TID:0x284540b0, sys_thread_t:0x8067700, state:R) prio=5 *current thr
ead*
        pntransjava.main(pntransjava.java:59)
Monitor Cache Dump:
Registered Monitor Dump:
    Thread queue lock: <unowned>
    Name and type hash table lock: <unowned>
    String intern lock: <unowned>
    JNI pinning lock: <unowned>
    JNI global reference lock: <unowned>
    BinClass lock: <unowned>
    Class loading lock: <unowned>
    Java stack lock: <unowned>

                                                                        
>How-To-Repeat:
Write code that makes calls to pthread routines, such as pthread_mutex_lock(), and link in a static library.

Write your .java file, run javah to create the .h file for inclusion
in your .c file, write the .c file to implement the functions from the
.h file.  Run javac on the .java file, and compile the .c file.  The
.c file functions in turn call a function in the static library which
calls pthread_mutex_lock().

Then run the java program which makes a call to the C function.  The
error occurs then.
>Fix:
On FreeBSD 2.2.7 RELEASE, I had a slightly different but seemingly
similar problem which reported an Alarm clock error.  I fixed it there
by copying a libc_r.so.3.0 file to /usr/lib from another machine 
which was running FreeBSD 2.2.8 STABLE.  However, I think that this
file was AOUT format for FreeBSD 2.2.X, and it does not work on the
FreeBSD 3.1 RELEASE platform that I have, since it is in ELF format.

Does anyone either know what my problem is, or have a working version
of libc_r.so.3 (ELF format) on FreeBSD 3.1?
  
Or do I need to upgrade my gcc from 2.7.2.1 to 2.8.1?


>Release-Note:
>Audit-Trail:

From: Steve Heller <sheller@PaymentNet.com>
To: "'freebsd-gnats-submit@freebsd.org'" <freebsd-gnats-submit@freebsd.org>,
	Steve Heller <sheller@PaymentNet.com>
Cc: "'freebsd-java@freebsd.org'" <freebsd-java@freebsd.org>
Subject: Re: i386/12286: Segmentation violation when invoking JNI call to 
	C from Java with PTHREADS
Date: Thu, 24 Jun 1999 17:03:37 -0700

 Ok,
 
 I have done some more investigation into this.  I think there may be some
 problem
 in the mutual exclusion/locking in the kernel (since I think that is where
 the call
 to _thread_sys_connect() resolves to as I cannot find it anywhere in the
 libraries):
 
 1) I pulled down the source for stable, which I thought was 3.2 STABLE,
 which 
 had a date of 5/18/99 from the ftp site.  Then I did a make world and
 reboot, and 
 noticed that uname still shows 3.1 RELEASE, but the problem changed to an 
 indication of an illegal instruction with JNI and multi-threading.
 
 2) Then I reviewed the freebsd-stable mailing list, and found that on
 6/22/99,
 there was a message about thread patches since the thread libraries in 
 stable's libc_r were broken.  This indicated a replacement at:
 
     ftp://ftp.pcnet.com/users/eischen/FreeBSD/uthread.tgz
 
 (I renamed this file to uthread.tar.gz, then did tar xvzf on it).  I pulled
 this file
 down and rebuilt libc_r and copied it to /usr/lib, after placing some debug
 printf statements in uthread_connect.c to make sure its version of connect
 was being called (it was).  
 
 The result is that when I use multithreading and make calls to socket(),
 connect(), 
 shutdown() and read(), without using JNI, everything works fine.  
 
 But when I make calls to socket(), connect(), shutdown() and read() through
 JNI, 
 I get the same error that I originally reported.  For the case of connect(),
 I changed 
 calls to socket() to _thread_sys_socket() and shutdown() to
 _thread_sys_shutdown(),
 and added debug printf's to the connect code, and ran it.  
 
 From the debug output, it made it past the _FD_LOCK() call, but not past the
 
 _thread_sys_connect() call before the Abort trap - core dumped message.
 
 Following is 1) the uthread_connect.c source code with debug printf's, and
 2) the debug output when the problem occurred with JNI:
 
 1) Here is my version of the uthread_connect.c source code with debug
 printf's:
 
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <fcntl.h>
 #ifdef _THREAD_SAFE
 #include <pthread.h>
 #include "pthread_private.h"
 
 int
 connect(int fd, const struct sockaddr * name, int namelen)
 {
         struct sockaddr tmpname;
         int             errnolen, ret, tmpnamelen;
 
         printf("libc_r connect: Entered...\n");
 
         if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
         	    printf("libc_r connect: After _FD_LOCK()...\n");
                 if ((ret = _thread_sys_connect(fd, name, namelen)) < 0) {
         		printf("libc_r connect: After
 _thread_sys_connect()...\n");
                         if (!(_thread_fd_table[fd]->flags & O_NONBLOCK) &&
                         ((errno == EWOULDBLOCK) || (errno == EINPROGRESS) ||
                          (errno == EALREADY) || (errno == EAGAIN))) {
         			printf("libc_r connect: After
 _thread_fd_table()...\n");
                                 _thread_run->data.fd.fd = fd;
 
                                 /* Set the timeout: */
                                 _thread_kern_set_timeout(NULL);
                                 _thread_kern_sched_state(PS_FDW_WAIT,
 __FILE__,
 __LINE__);
 
                                 tmpnamelen = sizeof(tmpname);
                                 /* 0 now lets see if it really worked */
                                 if (((ret = _thread_sys_getpeername(fd,
 &tmpname
 , &tmpnamelen)) < 0) && (errno == ENOTCONN)) {
 
                                         /*
                                          * Get the error, this function
                                          * should not fail
                                          */
                                         errnolen = sizeof(errno);
                                         _thread_sys_getsockopt(fd,
 SOL_SOCKET, S
 O_ERROR, &errno, &errnolen);
                                 }
                         } else {
                                 ret = -1;
                         }
                 }
 
                  _FD_UNLOCK(fd, FD_RDWR);
         }
         printf("libc_r connect: Exiting...\n");
         return (ret);
 }
 #endif
 
 2) Here is the corresponding debug output:
 
 (cd jni; java pntransjava 167.216.177.37 443
 "TRXTYPE=S&TENDER=C&PWD=g0edel1685&
 USER=pnprodtest&ACCT=5105105105105100&EXPDATE=1299&AMT=27.33" 30;  cd ..)
 SIGSEGV   11*  segmentation violation
 
 Full thread dump:
     "Finalizer thread" (TID:0x28454210, sys_thread_t:0x29258f00, state:R)
 prio=1
     "Async Garbage Collector" (TID:0x28454258, sys_thread_t:0x29237f00,
 state:R)
  prio=1
     "Idle thread" (TID:0x284542a0, sys_thread_t:0x29216f00, state:R) prio=0
     "Clock" (TID:0x28454088, sys_thread_t:0x291f5f00, state:CW) prio=12
     "main" (TID:0x284540b0, sys_thread_t:0x8067700, state:R) prio=5 *current
 thr
 ead*
         pntransjava.main(pntransjava.java:59)
 Monitor Cache Dump:
 Registered Monitor Dump:
     Thread queue lock: <unowned>
     Name and type hash table lock: <unowned>
     String intern lock: <unowned>
     JNI pinning lock: <unowned>
     JNI global reference lock: <unowned>
     BinClass lock: <unowned>
     Class loading lock: <unowned>
     Java stack lock: <unowned>
     Code rewrite lock: <unowned>
     Heap lock: <unowned>
     Has finalization queue lock: <unowned>
     Finalize me queue lock: <unowned>
     Monitor IO lock: <unowned>
     Child death monitor: <unowned>
     Event monitor: <unowned>
     I/O monitor: <unowned>
     Alarm monitor: <unowned>
         Waiting to be notified:
             "Clock" (0x291f5f00)
     Memory allocation lock: <unowned>
     Monitor registry: owner "main" (0x8067700, 1 entry)
 Thread Alarm Q:
 ProcessPNTransaction() entered...
 About to call CommonProcessTransaction()...
 CommonProcessTransaction() entered...
 About to call inet_addr()...
 Returned from call to inet_addr()...
 ipaddr: 632412327...
 sock:7
 finish setsockopt call connect.
 libc_r connect: Entered...
 libc_r connect: After _FD_LOCK()...
 Abort trap - core dumped
 *** Error code 134
 
 Stop.
 
 
 
 
State-Changed-From-To: open->closed 
State-Changed-By: unfurl 
State-Changed-When: Wed May 30 23:14:47 PDT 2001 
State-Changed-Why:  
The items mentioned in this PR are so old the PR is no longer relevant. 
Please submit another PR if this is still a problem. 


http://www.FreeBSD.org/cgi/query-pr.cgi?pr=12286 
>Unformatted:
