Skip to content

LArrayNative.mmap on Win64 not working for files > 4 GB #74

@flot92

Description

@flot92

The LArrayNative.mmap is not working for files > 4GB on windows 64 bit

I just spend way too many hours finding the problem crashing my jvm all of a sudden... So I want to document the problem here.

All you get back is the pointer therefore, in java, you are not aware of the mapping beeing smaller, till you violate memory access.

The C code uses the win32 int mapping also for the win64 bit version.

I will try to get it running.

// LArrayNative.c

JNIEXPORT jlong JNICALL Java_xerial_larray_impl_LArrayNative_mmap
  (JNIEnv *env, jclass cls, jlong fd, jint mode, jlong offset, jlong size)
{
#if defined(_WIN32) || defined(_WIN64)
  void *mapAddress = 0;
  jlong maxSize = offset + size;

the following should probably be long for the 64 bit case

  jint lowLen = (jint) (maxSize);
  jint highLen = (jint) (maxSize >> 32);
  jint lowOffset = (jint) offset;
  jint highOffset = (jint) (offset >> 32);
  HANDLE fileHandle = (HANDLE) fd;
  HANDLE mapping;
  DWORD mapAccess = FILE_MAP_READ;
  DWORD fileProtect = PAGE_READONLY;
  BOOL result;
  if (mode == 0) {
    fileProtect = PAGE_READONLY;
    mapAccess = FILE_MAP_READ;
  } else if (mode == 1) {
    fileProtect = PAGE_READWRITE;
    mapAccess = FILE_MAP_WRITE;
  } else if (mode == 2) {
    fileProtect = PAGE_WRITECOPY;
    mapAccess = FILE_MAP_COPY;
  }

highLen, lowLen, highOffset, lowOffset would need to be 64 bit.

  mapping = CreateFileMapping(fileHandle, NULL, fileProtect, highLen, lowLen, NULL);
  mapAddress = MapViewOfFile(mapping, mapAccess, highOffset, lowOffset, (size_t) size);
  result = CloseHandle(mapping);
  return (jlong) mapAddress;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions