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

mkbudle problem - Illegal byte sequence #11643

Closed
PetrSomek opened this issue Nov 12, 2018 · 15 comments · Fixed by #13061

Comments

@PetrSomek
Copy link

@PetrSomek PetrSomek commented Nov 12, 2018

I have a problem with mkbundle. I need to make one big executable from my .NET application. My application is just a simple 'Hello world' example, with Console.out("hello") command in main method.

Stable version of mono (5.16) is not working due the issue #8225
But this should be fixed in night release - so I have tried night version:

mkbundle -o output.exe -v --simple TestConsole.exe
OS is: Windows
Sources: 1 Auto-dependencies: True
Attempting to load assembly: TestConsole.exe
Assembly TestConsole.exe loaded successfully.
Attempting to load assembly from: .\I18N.West.dll
Attempting to load assembly from: C:\Program Files\Mono\lib\mono\4.5\I18N.West.dll
Attempting to load assembly from: .\I18N.dll
Attempting to load assembly from: C:\Program Files\Mono\lib\mono\4.5\I18N.dll
Attempting to load assembly from: .\mscorlib
Attempting to load assembly from: C:\Program Files\Mono\lib\mono\4.5\mscorlib
Attempting to load assembly from: .\mscorlib
Attempting to load assembly from: C:\Program Files\Mono\lib\mono\4.5\mscorlib
Attempting to load assembly from: .\I18N
Attempting to load assembly from: C:\Program Files\Mono\lib\mono\4.5\I18N
Attempting to load assembly from: .\mscorlib
Attempting to load assembly from: C:\Program Files\Mono\lib\mono\4.5\mscorlib
Using runtime: C:\Program Files\Mono\bin\mono.exe
At 0 with input 155648
At 30000 with input 4608
Assembly: E:\temp\mono\TestConsole.exe
At 32000 with input 4501504
Assembly: C:\Program Files\Mono\lib\mono\4.5\mscorlib.dll
At 47e000 with input 72192
Assembly: C:\Program Files\Mono\lib\mono\4.5\I18N.West.dll
At 490000 with input 39424
Assembly: C:\Program Files\Mono\lib\mono\4.5\I18N.dll
At 49a000 with input 3127
systemconfig: C:\Program Files\Mono\etc\mono\config
At 49b000 with input 34366
machineconfig: C:\Program Files\Mono\etc\mono\4.5\machine.config
assembly:TestConsole.exe at (196608, 4608)
assembly:mscorlib.dll at (204800, 4501504)
assembly:I18N.West.dll at (4710400, 72192)
assembly:I18N.dll at (4784128, 39424)
systemconfig: at (4825088, 3127)
machineconfig: at (4829184, 34366)
Generated output.exe

When I try to execute generated bundled exe I'm getting:
Error mapping file: Illegal byte sequence

Steps to Reproduce

  1. Download/install night build of mono
  2. mkbundle -o output.exe -v --simple TestConsole.exe
  3. Execute output.exe

Current Behavior

"Error mapping file: Illegal byte sequence" appears after execution of generated output.exe

Expected Behavior

Working output.exe

On which platforms did you notice this

[ ] macOS
[ ] Linux
[ x] Windows

Version Used:5.21

Stacktrace

Please paste the stack trace here if available.
@PetrSomek

This comment has been minimized.

Copy link
Author

@PetrSomek PetrSomek commented Nov 12, 2018

Attaching generated output.exe
output.zip

@CrazyAlex25

This comment has been minimized.

Copy link

@CrazyAlex25 CrazyAlex25 commented Dec 3, 2018

Platform: Win 7 Pro 64bit and Win 10 Pro 64bit
Tested mono version:

  • mono-5.16.0.220-gtksharp-2.12.45-win32-0 (I know about #8225 and #8884)
  • mono-5.18.0.216-gtksharp-2.12.45-win32-0
  • mono-5.21.0.595-gtksharp-2.12.45-win32-0

Source code

    class Program
    {
        [DllImport("advapi32.dll", SetLastError = true)]
        static extern bool GetUserName(System.Text.StringBuilder sb, ref Int32 length);

        static void Main(string[] args)
        {
            var len = 10;
            var sb = new StringBuilder(len);
            GetUserName(sb, ref len );
            Console.WriteLine(sb.ToString());
        }
    }

Compiled as

  • Release\x86
  • Release\AnyCPU
  • Debug\x86
  • Debug\AnyCPU
D:\Downloads\mono5.21\Mono\bin>mkbundle --simple -v -o D:\Projects\portable_test\portable_test\mono_night.exe D:\Projects\portable_test\portable_test\bin\x86\Release\portable_test.exe
OS is: Windows
Sources: 1 Auto-dependencies: True
Attempting to load assembly: D:\Projects\portable_test\portable_test\bin\x86\Release\portable_test.exe
Assembly D:\Projects\portable_test\portable_test\bin\x86\Release\portable_test.exe loaded successfully.
Attempting to load assembly from: .\I18N.West.dll
Attempting to load assembly from: D:\Downloads\mono5.21\Mono\lib\mono\4.5\I18N.West.dll
Attempting to load assembly from: .\I18N.dll
Attempting to load assembly from: D:\Downloads\mono5.21\Mono\lib\mono\4.5\I18N.dll
Attempting to load assembly from: .\mscorlib
Attempting to load assembly from: D:\Downloads\mono5.21\Mono\lib\mono\4.5\mscorlib
Attempting to load assembly from: .\mscorlib
Attempting to load assembly from: D:\Downloads\mono5.21\Mono\lib\mono\4.5\mscorlib
Attempting to load assembly from: .\I18N
Attempting to load assembly from: D:\Downloads\mono5.21\Mono\lib\mono\4.5\I18N
Attempting to load assembly from: .\mscorlib
Attempting to load assembly from: D:\Downloads\mono5.21\Mono\lib\mono\4.5\mscorlib
Using runtime: D:\Downloads\mono5.21\Mono\bin\mono.exe
At 0 with input 124928
At 20000 with input 4608
     Assembly: D:\Projects\portable_test\portable_test\bin\x86\Release\portable_test.exe
At 22000 with input 187
       Config: D:\Projects\portable_test\portable_test\bin\x86\Release\portable_test.exe.config
At 23000 with input 4528128
     Assembly: D:\Downloads\mono5.21\Mono\lib\mono\4.5\mscorlib.dll
At 475000 with input 72192
     Assembly: D:\Downloads\mono5.21\Mono\lib\mono\4.5\I18N.West.dll
At 487000 with input 39424
     Assembly: D:\Downloads\mono5.21\Mono\lib\mono\4.5\I18N.dll
At 491000 with input 3127
systemconfig: D:\Downloads\mono5.21\Mono\etc\mono\config
At 492000 with input 34366
machineconfig: D:\Downloads\mono5.21\Mono\etc\mono\4.5\machine.config
assembly:portable_test.exe at (131072, 4608)
config:portable_test.exe at (139264, 187)
assembly:mscorlib.dll at (143360, 4528128)
assembly:I18N.West.dll at (4673536, 72192)
assembly:I18N.dll at (4747264, 39424)
systemconfig: at (4788224, 3127)
machineconfig: at (4792320, 34366)
Generated D:\Projects\portable_test\portable_test\mono_night.exe

D:\Projects\portable_test\portable_test>mono_night.exe
Error mapping file: Illegal byte sequence

mono_night(release x86).zip

@CrazyAlex25

This comment has been minimized.

Copy link

@CrazyAlex25 CrazyAlex25 commented Feb 19, 2019

mono-6.1.0.170-gtksharp-2.12.45-win32-0 is same error;

@jaykrell

This comment has been minimized.

Copy link
Collaborator

@jaykrell jaykrell commented Feb 19, 2019

Worse/different:

mono_night.exe - System Error
The code execution cannot proceed because mono-2.0-sgen.dll was not found. Reinstalling the program may fix this problem. 
@jaykrell

This comment has been minimized.

Copy link
Collaborator

@jaykrell jaykrell commented Feb 19, 2019

Both of the provided ..zip files, presumably mkbundle output, depend on mono-2.0-sgen.dll, and fail to run on a system w/o mono installed.

@CrazyAlex25

This comment has been minimized.

Copy link

@CrazyAlex25 CrazyAlex25 commented Feb 19, 2019

Copy mono-2.0-sgen.dll to folder where you .exe

@jaykrell

This comment has been minimized.

Copy link
Collaborator

@jaykrell jaykrell commented Feb 19, 2019

But mkbundle output isn't supposed to require mono be installed at all, right?
The .zips provided, one is x86, one is amd64. I'll install those monos and see.

@jaykrell

This comment has been minimized.

Copy link
Collaborator

@jaykrell jaykrell commented Feb 19, 2019

C:\Users\jaykrell\Downloads\output\output.exe
Error mapping file: mono_file_map_error failed file:C:\Users\jaykrell\Downloads\output\output.exe length:0x477000 offset:0x30000 function:MapViewOfFile error:Access is denied.(0x5)
@CrazyAlex25

This comment has been minimized.

Copy link

@CrazyAlex25 CrazyAlex25 commented Feb 19, 2019

Mono must be installed in host system. It needed for create bundle.
mkbundle with --simple flag just join mono.exe and dependens assemblies.
For example

Using runtime: D:\Downloads\mono5.21\Mono\bin\mono.exe
# in output exe in this address will be contains mono.exe
At 0 with input 124928
# next just join dependends
At 20000 with input 4608
     Assembly: D:\Projects\portable_test\portable_test\bin\x86\Release\portable_test.exe
At 22000 with input 187
       Config: D:\Projects\portable_test\portable_test\bin\x86\Release\portable_test.exe.config
At 23000 with input 4528128
     Assembly: D:\Downloads\mono5.21\Mono\lib\mono\4.5\mscorlib.dll
At 475000 with input 72192
     Assembly: D:\Downloads\mono5.21\Mono\lib\mono\4.5\I18N.West.dll
At 487000 with input 39424
     Assembly: D:\Downloads\mono5.21\Mono\lib\mono\4.5\I18N.dll
At 491000 with input 3127
systemconfig: D:\Downloads\mono5.21\Mono\etc\mono\config
At 492000 with input 34366
.....

But for run mono.exe needed mono-2.0-sgen.dll

@jaykrell

This comment has been minimized.

Copy link
Collaborator

@jaykrell jaykrell commented Feb 19, 2019

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


PAGE_READONLY

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`00000230
     dwDesiredAccess = 4
    dwFileOffsetHigh = 0
     dwFileOffsetLow = 0x30000
dwNumberOfBytesToMap = 0x477000

C:\s>type 1.c
#include <windows.h>

xFILE_MAP_READ FILE_MAP_READ
xFILE_MAP_COPY FILE_MAP_COPY
xFILE_MAP_WRITE FILE_MAP_WRITE


C:\s>cl -E 1.c | findstr FILE_MAP

1.c
xFILE_MAP_READ 0x0004
xFILE_MAP_COPY 0x00000001
xFILE_MAP_WRITE 0x0002

The offset/size looks suspicious.

Your error message looks suspicious: We aren't supposed to produce
errors that look like that.

@jaykrell

This comment has been minimized.

Copy link
Collaborator

@jaykrell jaykrell commented Feb 19, 2019

Right.
So if in the call CreateFileMappingW, in the debugger, you increase dwMaximumSizeLow by the later value of dwFileOffsetLow, then it gets further and reports:

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.

So the problem is clear.

@CrazyAlex25

This comment has been minimized.

Copy link

@CrazyAlex25 CrazyAlex25 commented Feb 19, 2019

set PATH=%PATH%;%CD%\Mono\bin
mkbundle --simple -o test123 --cross mono-5.10.0-debian-7-x64 Test.exe ^
--library libs\libmono-btls-shared.so ^
--library libs\libMonoSupportW.so ^
--library libs\libMonoPosixHelper.so ^
--library libs\libpng12.so.0 ^
--library libs\libgif.so.4 ^
--library libs\libtiff.so.4 ^
--library libs\libgdiplus.so.0 

And its work with ubuntu 18.04, ubuntu 16.04, astralinux 1.5, astralinux 1.6
But not work if target windows. Maybe incorrect code align?

@jaykrell

This comment has been minimized.

Copy link
Collaborator

@jaykrell jaykrell commented Feb 19, 2019

Here is the help mail and virtual explanation I sent:

The notion of the size of a filemapping object seems confusing.
I've just always mapped entire files so ignored it.

Is the filemapping size, the maximum view size?
Or is the maximum offset of the file that can be mapped?

It seems more natural that is the maximum view size.
So the system wouldn't have to manage much information for a small view over a large file.

i.e.

Let's say I have a 128k file.
And I wish to map the second half.

Do I CreateFileMapping(size=128K) or CreateFileMapping(size=64k)?

Let's try it:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
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");

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());

PVOID view = MapViewOfFile(map, FILE_MAP_WRITE, 0, 1 << 16, 1 << 16);
printf("view %p %d\n", view, GetLastError());

CloseHandle(map);

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());
}

So the answer seems to be that the mapping describes the maximum file offset, not the maximum view size.
Right?
Improve the documentation?


Thank you,
 - Jay
@jaykrell

This comment has been minimized.

Copy link
Collaborator

@jaykrell jaykrell commented Feb 19, 2019

i.e. mono_file_map_error needs to consider length to beview_length and compute a separate filemapping_length which is offset + view_length. I'll submit a PR shortly.

@CrazyAlex25

This comment has been minimized.

Copy link

@CrazyAlex25 CrazyAlex25 commented Feb 19, 2019

@jaykrell Thx.

jaykrell added a commit to jaykrell/mono that referenced this issue Feb 19, 2019
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:

```
// The notions of mapping length and view length are confusing.
// One might think that the mapping length is the maximum view length
// that will be created from it, but it is actually the maximum mapped
// file offset. I do not want to believe this either, but this small
// test shows it:
//
// 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:

```

\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.

```
jaykrell added a commit to jaykrell/mono that referenced this issue Feb 19, 2019
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:

```
// The notions of mapping length and view length are confusing.
// One might think that the mapping length is the maximum view length
// that will be created from it, but it is actually the maximum mapped
// file offset. I do not want to believe this either, but this small
// test shows it:
//
// 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.

```
jaykrell added a commit to jaykrell/mono that referenced this issue Feb 19, 2019
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:

```
// The notions of mapping length and view length are confusing.
// One might think that the mapping length is the maximum view length
// that will be created from it, but it is actually the maximum mapped
// file offset. I do not want to believe this either, but this small
// test shows it:
//
// 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.

```
jaykrell added a commit to jaykrell/mono that referenced this issue Feb 19, 2019
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:

```
// The notions of mapping length and view length are confusing.
// One might think that the mapping length is the maximum view length
// that will be created from it, but it is actually the maximum mapped
// file offset. I do not want to believe this either, but this small
// test shows it:
//
// 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());
// }
```

Furthermore, it can be reasoned, that if this was the maximum view size,
it would be one DWORD parameter originally, later redefined as SIZE_T.
There'd be no need for 64bits of size on a 32bit system.

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.

```
jaykrell added a commit to jaykrell/mono that referenced this issue Feb 19, 2019
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.

```
luhenry added a commit that referenced this issue Feb 25, 2019
* #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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.