Search code examples
javawindows-7java-native-interface

JNI is crashing the java in windows 7


I am porting a code from linux to windows and since there is no support for Linux shared memory segments in Java. Hence, we are using the Java Native Interface (JNI) to access shared memory.It is working fine on linux platform .

Code for shmget:

JNIEXPORT jint JNICALL Java_emulatorinterface_communication_shm_SharedMem_shmget
(JNIEnv * env, jobject jobj, jint COUNT,jint MaxNumJavaThreads,jint EmuThreadsPerJavaThread,
        jlong coremap, jint pid) {

    uint64_t mask = coremap;

    int size;//=sizeof(packet)*(COUNT+5)*MaxNumJavaThreads*EmuThreadsPerJavaThread;

    char str[50];
    int ptr;

#ifdef _WIN32
    HANDLE hMapFile;
#endif
    printf("hello");
    size=sizeof(packet)*(COUNT+5)*MaxNumJavaThreads*EmuThreadsPerJavaThread;
    /*if (sched_setaffinity(0, sizeof(mask), (cpu_set_t *)&mask) <0) {
        perror("sched_setaffinity");
    }*/


    //set the global variables
    gCOUNT = COUNT;
    gMaxNumJavaThreads = MaxNumJavaThreads;
    gEmuThreadsPerJavaThread = EmuThreadsPerJavaThread;

    //size1 is the number of packets needed in the segment.

#ifdef _WIN32

    _itoa(pid,str,10);


    hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,
        NULL, PAGE_READWRITE,
        0,32, str);

    if (hMapFile == NULL)
    {

         exit(1);
    }
    CloseHandle(hMapFile);

    hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,
        NULL, PAGE_READWRITE,
        0,size, str);

    if (hMapFile == NULL)
    {
        printf("Unable to create a shared mem file.");
        exit(1);
    }
    if (hMapFile < 0)
    {
        return (-1);
    }

    /*
    if((hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,
        NULL, PAGE_READWRITE,
        0,size, str))<0)
    {
        printf("error window %d\n",size);
        return (-1);
    }
    */
    ptr=*((int*)(hMapFile));

    return ptr;          ///////////////error in return type 
    #else

If i am trying to use any other function in JNI ,my java crashes.

error

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000007feedf911be, pid=4696, tid=4216
#
# JRE version: Java(TM) SE Runtime Environment (8.0_77-b03) (build 1.8.0_77-b03)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.77-b03 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [JNIShm.dll+0x11be]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\workspace\tejasMinor\hs_err_pid4696.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

Can anyone point out where is the problem ?


Solution

  • You can't dereference an HANDLE to a File Mapping Object. Just make you function return the handle, as in:

    return (int)hMapFile;
    

    On 64 bits Windows (with a 64 bits JRE/JNI) the HANDLE type is 64 bits wide, but only the lower 32 bits are significant, so that's OK.

    Side notes:

    1. Don't use hMapFile < 0. If the API fails, the return value is NULL.
    2. Are you sure you need to try at first with a 32 bytes file, then close it? Why?
    3. You may want to call GetLastError and test the result against ERROR_ALREADY_EXISTS (if you want to be able to detect that condition)