Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5102720

JNI does not do appropriate stack alignment

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 6
    • 6
    • hotspot
    • mustang
    • x86
    • linux

      Name: rmT116609 Date: 09/15/2004


      FULL PRODUCT VERSION :
      java version "1.5.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b51)
      Java HotSpot(TM) Client VM (build 1.5.0-beta2-b51, mixed mode, sharing)

      Also occurs in:
      java version "1.4.2_03"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_03-b02)
      Java HotSpot(TM) Client VM (build 1.4.2_03-b02, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
        Bug demonstrated on:
      Linux 2.4.23 -- Slackware 9.0
      Linux 2.6.5-7.108 SuSE 9.1

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      gcc version does not seem to matter:
      gcc (GCC) 3.2.2
      gcc (GCC) 3.3.3 (SuSE Linux)


      A DESCRIPTION OF THE PROBLEM :
      If 128bit types are used in native code (i.e. SSE) the JVM may sefault. I've traced this back to the JVM not doing stack alignment.

      GCC always leaves the stack at sp%16==8. The JVM does not follow this restriction. As a consequence, if the native code tries to store a large type to the stack and the JVM has left the stack pointer at sp%16!=8, the memory reference will be misaligned and cause a segmentation fault.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the sample JNI library (gcc -o libjt.so -O3 -msse -ggdb -shared jt.c)

      Compile the same c program (gcc -o t test.c -ljt -L.)

      No matter the type or number of local variables in test.c, it will run and have ebp%16==8 (LD_LIBRARY_PATH=. ./t)

      Compile the same java program (javac JT.java)

      By varring the number of method variables in JT.java, it will run successfully, or fail with a segfault. (java -Djava.library.path=. JT)


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      JT.java should run successfully regardless of the the number of local variables.
      ACTUAL -
      JT.java will fail with many combiniations of local variables.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      #
      # An unexpected error has been detected by HotSpot Virtual Machine:
      #
      # SIGSEGV (0xb) at pc=0x4092f77a, pid=14386, tid=16384
      #
      # Java VM: Java HotSpot(TM) Client VM (1.5.0-beta2-b51 mixed mode, sharing)
      # Problematic frame:
      # C [libjt.so+0x77a] Java_JT_doSSE+0x62
      #

      --------------- T H R E A D ---------------

      Current thread (0x0805aa40): JavaThread "main" [_thread_in_native, id=14386]

      siginfo:si_signo=11, si_errno=0, si_code=128, si_addr=0x00000000

      Registers:
      EAX=0x00000011, EBX=0xbfffd304, ECX=0x00000000, EDX=0x00000011
      ESP=0xbfffd2d4, EBP=0xbfffd31c, ESI=0x698c4900, EDI=0x0805aa40
      EIP=0x4092f77a, CR2=0x00000000, EFLAGS=0x00010246

        Top of Stack: (sp=0xbfffd2d4)
      0xbfffd2d4: 4092f815 bfffd304 0000000c 0805ba4c
      0xbfffd2e4: 0805ba50 0805ba60 0805aa40 43845bb8
      0xbfffd2f4: 40000000 40066666 400ccccd 40133333
      0xbfffd304: 3f800000 3f8ccccd 3f99999a 3fa66666
      0xbfffd314: bfffd348 698c4900 bfffd348 4384042b
      0xbfffd324: 0805aafc bfffd358 bfffd32c 698c4900
      0xbfffd334: bfffd358 698c4ab8 00000000 698c4900
      0xbfffd344: bfffd358 bfffd378 4383aa7b 00000000

      Instructions: (pc=0x4092f77a)
      0x4092f76a: 58 5a 8d 5d e8 53 68 15 f8 92 40 e8 32 99 71 c7
      0x4092f77a: 0f 28 13 0f 28 45 d8 0f 58 d0 0f 29 55 c8 83 ec

      Stack: [0xbfe01000,0xc0000000), sp=0xbfffd2d4, free space=2032k
      Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
      C [libjt.so+0x77a] Java_JT_doSSE+0x62
      j JT.doSSE()V+0
      j JT.main([Ljava/lang/String;)V+9
      v ~StubRoutines::call_stub
      V [libjvm.so+0x16827c]
      V [libjvm.so+0x256038]
      V [libjvm.so+0x1680af]
      V [libjvm.so+0x189a92]
      V [libjvm.so+0x17e988]
      C [java+0x17fe]
      C [libc.so.6+0x15bb4] __libc_start_main+0xa4

      Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
      j JT.doSSE()V+0
      j JT.main([Ljava/lang/String;)V+9
      v ~StubRoutines::call_stub

      --------------- P R O C E S S ---------------

      Java Threads: ( => current thread )
        0x080a0af0 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=14393]
        0x0809f670 JavaThread "CompilerThread0" daemon [_thread_blocked, id=14392]
        0x0809e5e8 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=14391]
        0x0809a168 JavaThread "Finalizer" daemon [_thread_blocked, id=14390]
        0x08099470 JavaThread "Reference Handler" daemon [_thread_blocked, id=14389]
      =>0x0805aa40 JavaThread "main" [_thread_in_native, id=14386]

      Other Threads:
        0x08094a10 VMThread [id=14388]
        0x080a2028 WatcherThread [id=14394]

      VM state:not at safepoint (normal execution)

      VM Mutex/Monitor currently owned by a thread: None

      Heap
       def new generation total 576K, used 161K [0x658c0000, 0x65960000, 0x65da0000)
        eden space 512K, 31% used [0x658c0000, 0x658e8770, 0x65940000)
        from space 64K, 0% used [0x65940000, 0x65940000, 0x65950000)
        to space 64K, 0% used [0x65950000, 0x65950000, 0x65960000)
       tenured generation total 1408K, used 0K [0x65da0000, 0x65f00000, 0x698c0000)
         the space 1408K, 0% used [0x65da0000, 0x65da0000, 0x65da0200, 0x65f00000)
       compacting perm gen total 8192K, used 18K [0x698c0000, 0x6a0c0000, 0x6d8c0000)
         the space 8192K, 0% used [0x698c0000, 0x698c4b30, 0x698c4c00, 0x6a0c0000)
          ro space 8192K, 68% used [0x6d8c0000, 0x6de333e8, 0x6de33400, 0x6e0c0000)
          rw space 12288K, 47% used [0x6e0c0000, 0x6e6795c0, 0x6e679600, 0x6ecc0000)

      Dynamic libraries:
      08048000-08056000 r-xp 00000000 09:14 861722 /usr/local/jdk1.5.0/bin/java
      08056000-08058000 rw-p 0000e000 09:14 861722 /usr/local/jdk1.5.0/bin/java
      08058000-08141000 rwxp 00000000 00:00 0
      40000000-40014000 r-xp 00000000 09:15 99366 /lib/ld-2.3.1.so
      40014000-40015000 rw-p 00014000 09:15 99366 /lib/ld-2.3.1.so
      40015000-40016000 r--p 00000000 00:00 0
      40016000-4001c000 r-xp 00000000 09:14 796816 /usr/local/jdk1.5.0/jre/lib/i38
      6/native_threads/libhpi.so
      4001c000-4001d000 rw-p 00006000 09:14 796816 /usr/local/jdk1.5.0/jre/lib/i38
      6/native_threads/libhpi.so
      4001d000-40025000 rw-s 00000000 09:11 126768 /tmp/hsperfdata_vandebo/14386
      40025000-40034000 r-xp 00000000 09:15 99382 /lib/libpthread-0.10.so
      40034000-40037000 rw-p 0000e000 09:15 99382 /lib/libpthread-0.10.so
      40037000-40078000 rw-p 00000000 00:00 0
      40078000-4007a000 r-xp 00000000 09:15 99371 /lib/libdl-2.3.1.so
      4007a000-4007b000 rw-p 00001000 09:15 99371 /lib/libdl-2.3.1.so
      4007b000-401a5000 r-xp 00000000 09:15 99369 /lib/libc-2.3.1.so
      401a5000-401aa000 rw-p 0012a000 09:15 99369 /lib/libc-2.3.1.so
      401aa000-401ae000 rw-p 00000000 00:00 0
      401ae000-404cb000 r-xp 00000000 09:14 342544 /usr/local/jdk1.5.0/jre/lib/i38
      6/client/libjvm.so
      404cb000-404e9000 rw-p 0031c000 09:14 342544 /usr/local/jdk1.5.0/jre/lib/i38
      6/client/libjvm.so
      404e9000-408fe000 rw-p 00000000 00:00 0
      408fe000-40920000 r-xp 00000000 09:15 99372 /lib/libm-2.3.1.so
      40920000-40921000 rw-p 00021000 09:15 99372 /lib/libm-2.3.1.so
      40921000-4092c000 r-xp 00000000 09:14 764371 /usr/local/jdk1.5.0/jre/lib/i386/libverify.so
      4092c000-4092d000 rw-p 0000b000 09:14 764371 /usr/local/jdk1.5.0/jre/lib/i386/libverify.so
      4092d000-4092f000 r--s 00000000 09:14 66166 /usr/local/jdk1.5.0/jre/lib/ext/dnsns.jar
      4092f000-40930000 r-xp 00000000 09:10 24444966 /home/vandebo/Panoramic/sse/ex/libjt.so
      40930000-40931000 rw-p 00000000 09:10 24444966 /home/vandebo/Panoramic/sse/ex/libjt.so
      40931000-40943000 r-xp 00000000 09:15 99374 /lib/libnsl-2.3.1.so
      40943000-40944000 rw-p 00011000 09:15 99374 /lib/libnsl-2.3.1.so
      40944000-40946000 rw-p 00000000 00:00 0
      40946000-40951000 r-xp 00000000 09:15 99375 /lib/libnss_compat-2.3.1.so
      40951000-40952000 rw-p 0000a000 09:15 99375 /lib/libnss_compat-2.3.1.so
      40952000-40972000 r-xp 00000000 09:14 764372 /usr/local/jdk1.5.0/jre/lib/i386/libjava.so
      40972000-40974000 rw-p 0001f000 09:14 764372 /usr/local/jdk1.5.0/jre/lib/i386/libjava.so
      40974000-40987000 r-xp 00000000 09:14 764374 /usr/local/jdk1.5.0/jre/lib/i386/libzip.so
      40987000-40989000 rw-p 00012000 09:14 764374 /usr/local/jdk1.5.0/jre/lib/i386/libzip.so
      40989000-42f02000 r--s 00000000 09:14 66294 /usr/local/jdk1.5.0/jre/lib/rt.jar
      42f02000-42f6a000 rw-p 00000000 00:00 0
      42f6a000-42fea000 r--s 00000000 09:14 66273 /usr/local/jdk1.5.0/jre/lib/jsse.jar
      42fea000-42ffe000 r--s 00000000 09:14 66168 /usr/local/jdk1.5.0/jre/lib/jce.jar
      42ffe000-43838000 r--s 00000000 09:14 66292 /usr/local/jdk1.5.0/jre/lib/charsets.jar
      43838000-4594e000 rwxp 00028000 00:00 0
      4594e000-45974000 r--s 00000000 09:14 66164 /usr/local/jdk1.5.0/jre/lib/ext/sunjce_provider.jar
      45974000-4599f000 r--s 00000000 09:14 66165 /usr/local/jdk1.5.0/jre/lib/ext/sunpkcs11.jar
      4599f000-45a63000 r--s 00000000 09:14 66167 /usr/local/jdk1.5.0/jre/lib/ext/localedata.jar
      45a63000-45a64000 rw-p 00000000 00:00 0
      658c0000-6d8c0000 rwxp 22150000 00:00 0
      6d8c0000-6de34000 r--s 00001000 09:14 342547 /usr/local/jdk1.5.0/jre/lib/i386/client/classes.jsa
      6de34000-6e0c0000 rwxp 2a6c4000 00:00 0
      6e0c0000-6e67a000 rw-p 00575000 09:14 342547 /usr/local/jdk1.5.0/jre/lib/i386/client/classes.jsa
      6e67a000-6ecc0000 rwxp 2af0a000 00:00 0
      6ecc0000-6ed8f000 rw-p 00b2f000 09:14 342547 /usr/local/jdk1.5.0/jre/lib/i386/client/classes.jsa
      6ed8f000-6f0c0000 rwxp 2b61f000 00:00 0
      6f0c0000-6f0c4000 r-xs 00bfe000 09:14 342547 /usr/local/jdk1.5.0/jre/lib/i386/client/classes.jsa
      6f0c4000-6f4c0000 rwxp 2b954000 00:00 0
      bea00000-bea01000 ---p 00000000 00:00 0
      bea01000-bec00000 rwxp 00001000 00:00 0
      bec01000-bec04000 ---p 00003000 00:00 0
      bec04000-bee00000 rwxp 00006000 00:00 0
      bee00000-bee04000 ---p 00000000 00:00 0
      bee04000-bf000000 rwxp 00004000 00:00 0
      bf001000-bf004000 ---p 00003000 00:00 0
      bf004000-bf200000 rwxp 00006000 00:00 0
      bf201000-bf204000 ---p 00003000 00:00 0
      bf204000-bf400000 rwxp 00006000 00:00 0
      bf401000-bf404000 ---p 00003000 00:00 0
      bf404000-bf600000 rwxp 00006000 00:00 0
      bf600000-bf601000 ---p 00000000 00:00 0
      bf601000-bf800000 rwxp 00001000 00:00 0
      bfe01000-bfe04000 ---p 00000000 00:00 0
      bfe04000-c0000000 rwxp ffe05000 00:00 0

      VM Arguments:
      jvm_args: -Djava.library.path=.
      java_command: JT

      Environment Variables:
      PATH=/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/usr/games:/usr/local/java/bin:/opt/www/htdig/bin:/opt/kde/bin:/usr/lib/qt-3.1.2/bin:/usr/share/texmf/bin
      LD_LIBRARY_PATH=/usr/local/jdk1.5.0/jre/lib/i386/client:/usr/local/jdk1.5.0/jre/lib/i386:/usr/local/jdk1.5.0/jre/../lib/i386
      SHELL=/bin/bash
      DISPLAY=:1.0


      --------------- S Y S T E M ---------------

      OS:Linux
      uname:Linux 2.4.23 #2 Mon Dec 15 00:20:59 PST 2003 i686
      libc:glibc 2.3.1 stable linuxthreads (fixed stack)
      rlimit: STACK 2044k, CORE 0k, NPROC 7168, NOFILE 1024, AS infinity
      load average:0.00 0.01 0.00
      CPU:total 1 family 6, cmov, cx8, fxsr, mmx, sse

      Memory: 4k page, physical 1033940k(31952k free), swap 1044004k(448848k free)

      vm_info: Java HotSpot(TM) Client VM (1.5.0-beta2-b51) for linux-x86, built on May 13 2004 03:07:37 by java_re with gcc 3.2.1-7a (J2SE release)


      REPRODUCIBILITY :
      This bug can be reproduced often.

      ---------- BEGIN SOURCE ----------
      // jt.c:
      #include<stdio.h>
      #include<xmmintrin.h>
      #include "JT.h"

      JNIEXPORT void JNICALL Java_JT_doSSE(JNIEnv * e, jobject o) {
              __attribute__ ((aligned (16))) float pa[] = {1.0, 1.1, 1.2, 1.3};
              __attribute__ ((aligned (16))) float pb[] = {2.0, 2.1, 2.2, 2.3};
              __attribute__ ((aligned (16))) float r[4];

              __m128 a, b, c;
              unsigned int ebp;
              asm("movl %%ebp,%%eax;" : "=a"(ebp));
              printf("EBP=%x (%u).\n",ebp,ebp&15);


              printf("a is at %x\n", (unsigned int)pa);
              a = _mm_load_ps(pa);
              b = _mm_load_ps(pb);

              c = _mm_add_ps(a,b);

              _mm_store_ps(r, c);

              printf("%f %f %f %f\n", r[0], r[1], r[2], r[3]);

      }

      // test.c:
      #include<stdio.h>

      int main(void) {
              int a =1;
              float b=0.0;
              char c = 'a';
              Java_JT_doSSE(NULL, NULL);
              return 0;
      }


      // JT.java:
      public class JT {
          public static void main(String[] args) {
            int a;
            int b;
              JT t = new JT();
              t.doSSE();
          }

          public JT() {
              System.loadLibrary("jt");
          }

          public native void doSSE();
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      One may be able to add extra local variables to get the alignment right, but that seems to be JVM specific.

      One might be able to use some inline assembler to fix the stack on entry to native code
      (Incident Review ID: 310808)
      ======================================================================
      ###@###.### 10/5/04 15:31 GMT

            bobv Bob Vandette (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: