Using cygwin from JNI

Csaba Raduly
Wed Jul 4 13:32:00 GMT 2012

Hi all,

I'm trying to write a JNI wrapper to the Cygwin path conversion
functions (cygwin_conv_path &al), to be used from Eclipse.

Java code:
public final class cygconv {
        /** private constructor to disable instantiation */
        private cygconv() {

        public static native String win2cyg(final String path);

        public static void main(String[] args) {
                try {
                } catch (UnsatisfiedLinkError e) {
                        System.out.println(e.getMessage() + " which is
" + System.getProperty("java.library.path"));
                        throw e;

		for (int i = 0; i < args.length; ++i) {
			String cy = win2cyg(args[i]);
			System.out.println(args[i] + " -> " + cy);

C code:
typedef long long __int64;

#include "cygconv.h"
#include <stdio.h>
#include <malloc.h>
#include <sys/cygwin.h>

JNIEXPORT jstring JNICALL Java_cygconv_win2cyg(JNIEnv *env, jclass,
jstring input)
  puts("Going native!!!");
  const char *str;
  str = env->GetStringUTFChars(input, JNI_FALSE);

  char* cygstr = (char*)cygwin_create_path(CCP_WIN_A_TO_POSIX, str);
  env->ReleaseStringUTFChars(input, str);

  jstring retval = env->NewStringUTF(cygstr);

  return retval;

However, when I try to run it (using the commandline "java"
executable), there's an access violation when puts is called (or, if
that line is commented out, at cygwin_create_path).

# A fatal error has been detected by the Java Runtime Environment:
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x610d5404, pid=4964, tid=2324
# JRE version: 6.0_25-b06
# Java VM: Java HotSpot(TM) Client VM (20.0-b11 mixed mode windows-x86 )
# Problematic frame:
# C  [cygwin1.dll+0xd5404]
# If you would like to submit a bug report, please visit:
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.

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

Current thread (0x01a59800):  JavaThread "main" [_thread_in_native,
id=2324, stack(0x00370000,0x003c0000)]

siginfo: ExceptionCode=0xc0000005, writing address 0x00000018

EAX=0x00000018, EBX=0x003c0000, ECX=0x01a59928, EDX=0x003e1012
ESP=0x003bfc80, EBP=0x003bfc98, ESI=0x1404e760, EDI=0x01a59800
EIP=0x610d5404, EFLAGS=0x00010202

Top of Stack: (sp=0x003bfc80)
0x003bfc80:   00140000 1404e760 611387d0 610d5415
0x003bfc90:   003e2020 00a59800 003bfcd0 01ea9fc7
0x003bfca0:   01a59928 003bfcd8 003bfce0 003bfcd0
0x003bfcb0:   003bfcb0 1404e760 003bfce0 1404ef10
0x003bfcc0:   00000000 1404e760 00000000 003bfce0
0x003bfcd0:   003bfd04 01ea2f87 1404eac0 01ea8306
0x003bfce0:   1404f070 003bfce4 1404e859 003bfd10
0x003bfcf0:   1404ef10 00000000 1404e888 003bfce0

Instructions: (pc=0x610d5404)
0x610d53e4:   c0 74 07 e8 34 1d fb ff eb e4 b8 04 00 00 00 0f
0x610d53f4:   c1 83 2c da ff ff 8d 15 15 54 0d 61 87 54 24 0c
0x610d5404:   89 10 ff 83 20 da ff ff ff 8b 28 da ff ff 5a 5b
0x610d5414:   c3 50 53 64 8b 1d 04 00 00 00 b8 01 00 00 00 87

Register to memory mapping:

EAX=0x00000018 is an unknown value
EBX=0x003c0000 is pointing into the stack for thread: 0x01a59800
ECX=0x01a59928 is an unknown value
EDX=0x003e1012 is an unknown value
ESP=0x003bfc80 is pointing into the stack for thread: 0x01a59800
EBP=0x003bfc98 is pointing into the stack for thread: 0x01a59800
ESI=0x1404e760 is an oop
 - klass: {other class}
EDI=0x01a59800 is a thread

Stack: [0x00370000,0x003c0000],  sp=0x003bfc80,  free space=319k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [cygwin1.dll+0xd5404]  aclcheck+0xf754
j  cygconv.win2cyg(Ljava/lang/String;)Ljava/lang/String;+0
j  cygconv.main([Ljava/lang/String;)V+57
v  ~StubRoutines::call_stub
V  [jvm.dll+0xfad0b]
V  [jvm.dll+0x18c241]
V  [jvm.dll+0xfad8d]
V  [jvm.dll+0x957b6]
V  [jvm.dll+0x9d5f3]
C  [java.exe+0x2155]
C  [java.exe+0x85b4]
C  [kernel32.dll+0x4d0e9]  BaseThreadInitThunk+0x12
C  [ntdll.dll+0x41603]  RtlInitializeExceptionChain+0x63
C  [ntdll.dll+0x415d6]  RtlInitializeExceptionChain+0x36

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

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

Java Threads: ( => current thread )
  0x01ca3c00 JavaThread "Low Memory Detector" daemon [_thread_blocked,
id=4140, stack(0x17f90000,0x17fe0000)]
  0x01c90c00 JavaThread "C1 CompilerThread0" daemon [_thread_blocked,
id=4408, stack(0x17f40000,0x17f90000)]
  0x01c8b800 JavaThread "Attach Listener" daemon [_thread_blocked,
id=7660, stack(0x17ef0000,0x17f40000)]
  0x01c88800 JavaThread "Signal Dispatcher" daemon [_thread_blocked,
id=6112, stack(0x17ea0000,0x17ef0000)]
  0x01c5e800 JavaThread "Finalizer" daemon [_thread_blocked, id=1672,
  0x01c59c00 JavaThread "Reference Handler" daemon [_thread_blocked,
id=2396, stack(0x01db0000,0x01e00000)]
=>0x01a59800 JavaThread "main" [_thread_in_native, id=2324,

Other Threads:
  0x01c55800 VMThread [stack: 0x01d60000,0x01db0000] [id=7120]
  0x01cbf800 WatcherThread [stack: 0x181e0000,0x18230000] [id=744]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

 def new generation   total 4928K, used 281K [0x03ea0000, 0x043f0000,
  eden space 4416K,   6% used [0x03ea0000, 0x03ee6440, 0x042f0000)
  from space 512K,   0% used [0x042f0000, 0x042f0000, 0x04370000)
  to   space 512K,   0% used [0x04370000, 0x04370000, 0x043f0000)
 tenured generation   total 10944K, used 0K [0x093f0000, 0x09ea0000, 0x13ea0000)
   the space 10944K,   0% used [0x093f0000, 0x093f0000, 0x093f0200, 0x09ea0000)
 compacting perm gen  total 12288K, used 1724K [0x13ea0000,
0x14aa0000, 0x17ea0000)
   the space 12288K,  14% used [0x13ea0000, 0x1404f0c0, 0x1404f200, 0x14aa0000)
No shared spaces configured.

Code Cache  [0x01ea0000, 0x01f10000, 0x03ea0000)
 total_blobs=123 nmethods=2 adapters=58 free_code_cache=33109440

Dynamic libraries:
0x00400000 - 0x00424000 	C:\Program Files\Java\jdk1.6.0_25\bin\java.exe
0x778a0000 - 0x779c8000 	C:\Windows\system32\ntdll.dll
0x762e0000 - 0x763bc000 	C:\Windows\system32\kernel32.dll
0x76100000 - 0x761c6000 	C:\Windows\system32\ADVAPI32.dll
0x779e0000 - 0x77aa3000 	C:\Windows\system32\RPCRT4.dll
0x75f10000 - 0x75f4a000 	C:\Program Files\Citrix\system32\radeaphook.dll
0x75cf0000 - 0x75d9f000 	C:\Program Files\Citrix\system32\CtxSbxHook.DLL
0x763c0000 - 0x7645d000 	C:\Windows\system32\USER32.dll
0x77350000 - 0x7739b000 	C:\Windows\system32\GDI32.dll
0x75f80000 - 0x75f88000 	C:\Windows\system32\VERSION.dll
0x77520000 - 0x775ca000 	C:\Windows\system32\msvcrt.dll
0x75ac0000 - 0x75ce7000 	C:\Windows\system32\msi.dll
0x775d0000 - 0x77715000 	C:\Windows\system32\ole32.dll
0x76630000 - 0x7664e000 	C:\Windows\system32\IMM32.DLL
0x777d0000 - 0x77898000 	C:\Windows\system32\MSCTF.dll
0x779d0000 - 0x779d9000 	C:\Windows\system32\LPK.DLL
0x771a0000 - 0x7721d000 	C:\Windows\system32\USP10.dll
0x7c340000 - 0x7c396000 	C:\Program Files\Java\jdk1.6.0_25\jre\bin\msvcr71.dll
0x6d8a0000 - 0x6db4f000 	C:\Program
0x74af0000 - 0x74b22000 	C:\Windows\system32\WINMM.dll
0x761d0000 - 0x7625d000 	C:\Windows\system32\OLEAUT32.dll
0x74ab0000 - 0x74aee000 	C:\Windows\system32\OLEACC.dll
0x75eb0000 - 0x75edc000 	C:\Windows\system32\apphelp.dll
0x6d850000 - 0x6d85c000 	C:\Program Files\Java\jdk1.6.0_25\jre\bin\verify.dll
0x6d3d0000 - 0x6d3ef000 	C:\Program Files\Java\jdk1.6.0_25\jre\bin\java.dll
0x76060000 - 0x76067000 	C:\Windows\system32\PSAPI.DLL
0x6d890000 - 0x6d89f000 	C:\Program Files\Java\jdk1.6.0_25\jre\bin\zip.dll
0x003e0000 - 0x003f4000
0x61000000 - 0x61470000 	C:\cygwin17\bin\cygwin1.dll

VM Arguments:
jvm_args: -verbose:jni
java_command: cygconv
Launcher Type: SUN_STANDARD

Environment Variables:
Files\WIDCOMM\Bluetooth Software;C:\Program
PROCESSOR_IDENTIFIER=x86 Family 6 Model 37 Stepping 5, GenuineIntel

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

OS: Windows Vista Build 6002 Service Pack 2

CPU:total 4 (2 cores per cpu, 2 threads per core) family 6 model 37
stepping 5, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, sse4.1,
sse4.2, popcnt, ht

Memory: 4k page, physical 3127676k(1493176k free), swap 6467340k(4727808k free)

vm_info: Java HotSpot(TM) Client VM (20.0-b11) for windows-x86 JRE
(1.6.0_25-b06), built on Apr 14 2011 01:04:32 by "java_re" with MS
VC++ 7.1 (VS2003)

time: Wed Jul 04 13:00:40 2012
elapsed time: 0 seconds

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

I tried debugging the access violation, so I installed the latest
snapshot (2012-07-02 02:29:54) for its debug info:
 2784k 2012/07/04 C:\cygwin17\bin\cygwin1.dll - os=4.0 img=1.0 sys=4.0
                  "cygwin1.dll" v0.0 ts=2012/7/2 3:28
    Cygwin DLL version info:
        DLL version: 1.7.16
        DLL epoch: 19
        DLL old termios: 5
        DLL malloc env: 28
        Cygwin conv: 181
        API major: 0
        API minor: 261
        Shared data: 5
        DLL identifier: cygwin1
        Mount registry: 3
        Cygwin registry name: Cygwin
        Program options name: Program Options
        Installations name: Installations
        Cygdrive default prefix:
        Build date:
        Snapshot date: 20120702-02:25:02
        Shared id: cygwin1S5

Unfortunately, GDB does not stop at the access violation; it appears
to be preempted by the JVM: the JVM diagnostic is printed and the only
thing form GDB is
[Inferior 1 (process 6764) exited with code 01]

I tried to use WinDbg and Visual Studio Express, but all I got was a
raw stack showing an access violation at cygwin1.dll!610d5404()

If I compile a no-op C code (only the puts call) with -mno-cygwin then
it works, but that defeats the purpose of the exercise, using Cygwin

Any ideas how to get it working? What is [cygwin1.dll+0xd5404]
aclcheck+0xf754 ?

If I use the java launcher from the salma-hayek library (described
instead of java.exe, then the conversion can be seen working, but that
doesn't seem to be a workable option when the code is ultimately
loaded from Eclipse.

GCS a+ e++ d- C++ ULS$ L+$ !E- W++ P+++$ w++$ tv+ b++ DI D++ 5++
The Tao of math: The numbers you can count are not the real numbers.
Life is complex, with real and imaginary parts.
"Ok, it boots. Which means it must be bug-free and perfect. " -- Linus Torvalds
"People disagree with me. I just ignore them." -- Linus Torvalds

Problem reports:
Unsubscribe info:

More information about the Cygwin mailing list