larray icon indicating copy to clipboard operation
larray copied to clipboard

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

Open flot92 opened this issue 4 years ago • 2 comments

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;

flot92 avatar Nov 27 '20 15:11 flot92

I just realized it is an duplicate to... https://github.com/xerial/larray/issues/58 As mentioned there, the fix there is not included in the newest Maven 0.4.1 version though.

The way I patch the current larray-mmap-0.4.1.jar (if someone needs a quick fixed version someday)

Create new folder with:
LArrayNative.c
LArrayNative.h

Inside the Folder:

x86_64-w64-mingw32-gcc.exe -c -I"C:\Program Files\Java\jdk1.8.0_261\include" -I"C:\Program Files\Java\jdk1.8.0_261\include\win32" LArrayNative.c

x86_64-w64-mingw32-gcc.exe -shared -o larray.dll LArrayNative.o


Open larray-mmap-0.4.1.jar with 7-zip, replace "xerial\larray\native\Windows\amd64\larray.dll" with your newly created one

Thanks for sharing the whole project :)

flot92 avatar Nov 27 '20 19:11 flot92

FWIW, in line 126 result = FlushViewOfFile(a, (DWORD)size);, size should be also be cast to SIZE_T, not DWORD.

stefan-zobel avatar Nov 30 '20 15:11 stefan-zobel