[mkbundle] Fix mapping length (#13061)
commit2f3f58c7f12433c6595b1f2f15fb48987964885e
authorJay Krell <jay.krell@cornell.edu>
Mon, 25 Feb 2019 14:48:04 +0000 (25 06:48 -0800)
committerLudovic Henry <luhenry@microsoft.com>
Mon, 25 Feb 2019 14:48:04 +0000 (25 09:48 -0500)
tree561af1a405a131e28cac82beac1e4dea9b790e2b
parente6d259b82ce60842fbb592788b6c6653b6009e6a
[mkbundle] Fix mapping length (#13061)

* https://github.com/mono/mono/issues/11643 mkbundle problem - Illegal byte sequence

The size of the mapping is the maximum file offset to map.
See https://docs.microsoft.com/en-us/windows/desktop/Memory/creating-a-file-mapping-object.

When I tried it, I got a different error.

If in the debugger you simulate this fix, it gets further.

Here is a small program to exercise and see the behavior:

```
// include <windows.h>
// include <stdio.h>
// include <stdlib.h>
//
// int main()
// {
//  // Create a 128K file.
//
//  HANDLE file = CreateFile("1", GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
//  printf("createfile %p %d\n", file, GetLastError());
//
//  HANDLE map = CreateFileMapping(file, 0, PAGE_READWRITE, 0, 1 << 17, 0);
//  printf("createmap %p %d\n", map, GetLastError());
//
//  CloseHandle(map);
//  CloseHandle(file);
//  system("dir");
//
//  // Attempt to map the second 64K of the 128K file, with a 64K mapping.
//
//  file = CreateFile("1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
//  printf("createfile %p %d\n", file, GetLastError());
//
//  map = CreateFileMapping(file, 0, PAGE_READWRITE, 0, 1 << 16, 0);
//  printf("createmap %p %d\n", map, GetLastError());
//
//   // This fails.
//
//  PVOID view = MapViewOfFile(map, FILE_MAP_WRITE, 0, 1 << 16, 1 << 16);
//  printf("view %p %d\n", view, GetLastError());
//
//  CloseHandle(map);
//
//  // Use a 128K mapping instead. It works.
//
//  map = CreateFileMapping(file, 0, PAGE_READWRITE, 0, 1 << 17, 0);
//  printf("createmap %p %d\n", map, GetLastError());
//
//  view = MapViewOfFile(map, FILE_MAP_WRITE, 0, 1 << 16, 1 << 16);
//  printf("view %p %d\n", view, GetLastError());
// }
```

Here is debugging mono and simulating the fix in the debugger (I did not build or test
the fix):

```

\bin\amd64\cdb C:\Users\jaykrell\Downloads\output\output.exe

0:000> bm kernel*!*createfilemap*
0:000> g

Breakpoint 2 hit
KERNELBASE!CreateFileMappingW:
00007ffd`0a449de0 4883ec48        sub     rsp,48h
0:000> dv
                  hFile = 0x00000000`00000200
lpFileMappingAttributes = 0x00000000`00000000
              flProtect = 2
      dwMaximumSizeHigh = 0
       dwMaximumSizeLow = 0x477000
                 lpName = 0x00000000`00000000 ""

0:000> bm kern*!*mapviewoffile*
0:000> g
Breakpoint 38 hit
KERNEL32!MapViewOfFileStub:
00007ffd`0b09bfa0 48ff2591c10500  jmp     qword ptr [KERNEL32!_imp_MapViewOfFile (00007ffd`0b0f8138)] ds:00007ffd`0b0f8138={KERNELBASE!MapViewOfFile (00007ffd`0a465ad0)}
0:000> dv
  hFileMappingObject = 0x00000000`00000204
     dwDesiredAccess = 4
    dwFileOffsetHigh = 0
     dwFileOffsetLow = 0x30000
dwNumberOfBytesToMap = 0x477000
0:000> gu
0:000> r rax
rax=0000000000000000
0:000> !gle
LastErrorValue: (Win32) 0x5 (5) - Access is denied.
LastStatusValue: (NTSTATUS) 0xc000001f - {Invalid Mapping}  An attempt was made to create a view for a section which is bigger than the section.

0:000> .restart
0:000> bm kernel*!*createfilemap*
0:000> g
0:000> dv
                  hFile = 0x00000000`00000220
lpFileMappingAttributes = 0x00000000`00000000
              flProtect = 2
      dwMaximumSizeHigh = 0
       dwMaximumSizeLow = 0x477000
                 lpName = 0x00000000`00000000 ""
0:000> ?? dwMaximumSizeLow += 0x30000
unsigned long 0x4a7000
0:000> bc *
0:000> g
Corlib not in sync with this runtime: The runtime did not find the mscorlib.dll it expected. Expected interface version CA4932AE-2294-4ECD-B863-BF98FDD84F33 but found 0CC970F6-6F25-4218-9427-3E4C3DC8DE33. Check that your runtime and class libraries are matching.
Loaded from: mscorlib.dll
Download a newer corlib or a newer runtime at http://www.mono-project.com/download.

```

* Restore useful part of comment.

* Restore dead code.

* Add to comment the other possibility that is being countered.

* Additional explanation.
mono/utils/mono-mmap-windows.c