Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
flot92 opened this issue Nov 27, 2020 · 2 comments
Open

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

flot92 opened this issue Nov 27, 2020 · 2 comments

Comments

@flot92
Copy link

flot92 commented Nov 27, 2020

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
Copy link
Author

flot92 commented Nov 27, 2020

I just realized it is an duplicate to... #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 :)

@stefan-zobel
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants