title: Why does strlen cause SIGSEGV when called from JNI code (64bit)?
body: <p>I am a bit baffled by a bug I am currently experiencing while trying to implement some logic using C and C++ in combination with JNI and Java.</p>
  
  <p>I distilled it to a simple example, which causes the crash every time when compiled for 64 bit on Linux. When using 32bit, it works ok. :S</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;time.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;string.h&gt;
  
  char * strlenTest()
  {
      int size = 100;
      char * string = (char *) malloc(size * sizeof(char));
      int i;
      for ( i = 0; i &lt; 50; i++)
      {
          string[i] = 'a';
      }
      string[50] = '\0';
      printf("string = '%s'\n", string);
      int length = strlen(string);
      printf("string length = %d\n", length);
      return string;
  }
  </code></pre>
  
  <p>And the calling method that crashes:</p>
  
  <pre><code>JNIEXPORT jstring JNICALL Java_foo(JNIEnv *env,
          jobject thiz, jstring dexPath) {
      printf("calling test()\n");
      char * test = strlenTest();
      printf("calling strlen in jni\n");
      int testLength = strlen(test);
      printf("length:%d\n", testLength);
      }
  </code></pre>
  
  <p>Output:
  64 bit:</p>
  
  <pre><code>calling test()
  string = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  string length = 50
  calling strlen in jni
  #
  # A fatal error has been detected by the Java Runtime Environment:
  #
  #  SIGSEGV (0xb) at pc=0x00007f63cbefb6fa, pid=9563, tid=140066607630080
  #
  # JRE version: OpenJDK Runtime Environment (7.0_79-b14) (build 1.7.0_79-b14)
  # Java VM: OpenJDK 64-Bit Server VM (24.79-b02 mixed mode linux-amd64 compressed oops)
  # Derivative: IcedTea 2.5.5
  # Distribution: Ubuntu Vivid Vervet (development branch), package 7u79-2.5.5-0ubuntu1
  # Problematic frame:
  # C  [libc.so.6+0x8b6fa]  strlen+0x2a
  </code></pre>
  
  <p>32bit:</p>
  
  <pre><code>calling test()
  string = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  string length = 50
  calling strlen in jni
  length:50
  </code></pre>
  
  <p>Makefile:</p>
  
  <pre><code>MARCH=-m64
  
  CXX      = g++
  CXXFLAGS =  -ansi -g -O0 $(MARCH) -fPIC  -I../.. -I/usr/lib/jvm/java-7-openjdk-amd64/include -I/usr/include -Dunix -Wformat=0
  CC       = gcc
  CCFLAGS  = -g  -O0 -fPIC $(MARCH) -I../.. -I/usr/lib/jvm/java-7-openjdk-amd64/include -I/usr/include -Dunix -Wformat=0
  OBJS     = bugtest.o dexFileParser.o miniunz.o unzip.o ioapi.o 
  
  libname.so : $(OBJS)
      $(CXX) -o $@ $(OBJS) -lz  $(MARCH) -shared
  
  %.o : %.cpp
      $(CXX) -c $(CXXFLAGS) $&lt; 
  
  %.o : %.c
      $(CC) -c $(CCFLAGS) $&lt;
  
  clean: 
      rm *.o
  </code></pre>
  
  <p>The weird thing is, <code>strlen</code> works in the method itself,but when the <code>char *</code> is returned to the JNI C function, it somehow crashes, and only in 64 bit.</p>
  
  <p>I really don't understand what is going on. Can someone point out the error?</p>