If fake 16 bit dll is found by create_mapping and status is set to STATUS_IMAGE_INVALID_WIN_16, do not release the mapping, but return it with WineFakeDll flag set to allow ntdll properly discover this dll by NtCreateSection and handle by find_dll_file.
Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=51564 Signed-off-by: Oleh Nykyforchyn oleh.nyk@gmail.com --- server/mapping.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)
diff --git a/server/mapping.c b/server/mapping.c index 93dae94b7c4..8016feaf432 100644 --- a/server/mapping.c +++ b/server/mapping.c @@ -697,6 +697,13 @@ static unsigned int get_image_params( struct mapping *mapping, file_pos_t file_s mz_size = size; pos = mz.dos.e_lfanew;
+ mapping->image.image_flags = 0; + mapping->image.loader_flags = 0; + if (mz_size == sizeof(mz) && !memcmp( mz.buffer, builtin_signature, sizeof(builtin_signature) )) + mapping->image.image_flags |= IMAGE_FLAGS_WineBuiltin; + else if (mz_size == sizeof(mz) && !memcmp( mz.buffer, fakedll_signature, sizeof(fakedll_signature) )) + mapping->image.image_flags |= IMAGE_FLAGS_WineFakeDll; + size = pread( unix_fd, &nt, sizeof(nt), pos ); if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) return STATUS_INVALID_IMAGE_PROTECT; /* zero out Optional header in the case it's not present or partial */ @@ -707,7 +714,11 @@ static unsigned int get_image_params( struct mapping *mapping, file_pos_t file_s { IMAGE_OS2_HEADER *os2 = (IMAGE_OS2_HEADER *)&nt; if (os2->ne_magic != IMAGE_OS2_SIGNATURE) return STATUS_INVALID_IMAGE_PROTECT; - if (os2->ne_exetyp == 2) return STATUS_INVALID_IMAGE_WIN_16; + if (os2->ne_exetyp == 2) + { + mapping->image.machine = IMAGE_FILE_MACHINE_I386; + return STATUS_INVALID_IMAGE_WIN_16; + } if (os2->ne_exetyp == 5) return STATUS_INVALID_IMAGE_PROTECT; return STATUS_INVALID_IMAGE_NE_FORMAT; } @@ -737,7 +748,6 @@ static unsigned int get_image_params( struct mapping *mapping, file_pos_t file_s nt.opt.hdr32.SectionAlignment & page_mask); mapping->image.header_size = nt.opt.hdr32.SizeOfHeaders; mapping->image.checksum = nt.opt.hdr32.CheckSum; - mapping->image.image_flags = 0; if (nt.opt.hdr32.SectionAlignment & page_mask) mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat; if ((nt.opt.hdr32.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) && @@ -769,7 +779,6 @@ static unsigned int get_image_params( struct mapping *mapping, file_pos_t file_s nt.opt.hdr64.SectionAlignment & page_mask); mapping->image.header_size = nt.opt.hdr64.SizeOfHeaders; mapping->image.checksum = nt.opt.hdr64.CheckSum; - mapping->image.image_flags = 0; if (nt.opt.hdr64.SectionAlignment & page_mask) mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat; if ((nt.opt.hdr64.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) && @@ -788,10 +797,6 @@ static unsigned int get_image_params( struct mapping *mapping, file_pos_t file_s mapping->image.zerobits = 0; /* FIXME */ mapping->image.file_size = file_size; mapping->image.loader_flags = clr_va && clr_size; - if (mz_size == sizeof(mz) && !memcmp( mz.buffer, builtin_signature, sizeof(builtin_signature) )) - mapping->image.image_flags |= IMAGE_FLAGS_WineBuiltin; - else if (mz_size == sizeof(mz) && !memcmp( mz.buffer, fakedll_signature, sizeof(fakedll_signature) )) - mapping->image.image_flags |= IMAGE_FLAGS_WineFakeDll;
/* load the section headers */
@@ -920,7 +925,11 @@ static struct mapping *create_mapping( struct object *root, const struct unicode unsigned int err = get_image_params( mapping, st.st_size, unix_fd ); if (!err) return mapping; set_error( err ); - goto error; + /* return fake mapping to discover fake 16 bit dll */ + if ((err == STATUS_INVALID_IMAGE_WIN_16) && (mapping->image.image_flags & IMAGE_FLAGS_WineFakeDll)) + return mapping; + else + goto error; } if (!mapping->size) { @@ -1106,6 +1115,12 @@ struct object *create_user_data_mapping( struct object *root, const struct unico
if (!(mapping = create_mapping( root, name, attr, sizeof(KSHARED_USER_DATA), SEC_COMMIT, 0, FILE_READ_DATA | FILE_WRITE_DATA, sd ))) return NULL; + /* FIXME: is this necessary? */ + if ((get_error() == STATUS_INVALID_IMAGE_WIN_16) && (mapping->image.image_flags & IMAGE_FLAGS_WineFakeDll)) + { + release_object( mapping ); + return NULL; + } ptr = mmap( NULL, mapping->size, PROT_WRITE, MAP_SHARED, get_unix_fd( mapping->fd ), 0 ); if (ptr != MAP_FAILED) {
If NtCreateSection returns STATUS_INVALID_IMAGE_WIN_16 with WineFakeDll flag in image_info, stop search by open_dll_file and replace the status with STATUS_INVALID_IMAGE_NOT_MZ to make find_builtin_dll try .so library instead. Replace "-windows", if found in the path, with "-unix", and append ".so". -- Now Wine can't start any 16 bit app because if fails to load krnl386.exe16. To load any 16 bit dll, ntdll uses find_builtin_without_file -> open_dll_file -> NtCreateSection which makes a request ( create_mapping ) to wineserver, which in turn fails with STATUS_INVALID_IMAGE_WIN_16 and returns no mapping.
Upon a previously submitted patch to server, for a fake 16 bit dll create_mapping returns a fake mapping with WineFakeDll flag set.
Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=51564 Signed-off-by: Oleh Nykyforchyn oleh.nyk@gmail.com --- dlls/ntdll/loader.c | 65 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 4 deletions(-)
diff --git a/dlls/ntdll/loader.c b/dlls/ntdll/loader.c index 797d72aedb6..6befe778c90 100644 --- a/dlls/ntdll/loader.c +++ b/dlls/ntdll/loader.c @@ -2315,10 +2315,24 @@ static NTSTATUS open_dll_file( UNICODE_STRING *nt_name, WINE_MODREF **pwm, HANDL status = NtCreateSection( mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_EXECUTE, NULL, &size, PAGE_EXECUTE_READ, SEC_IMAGE, handle ); - if (!status) + if (!status || (status == STATUS_INVALID_IMAGE_WIN_16)) { NtQuerySection( *mapping, SectionImageInformation, image_info, sizeof(*image_info), NULL ); - if (!is_valid_binary( handle, image_info )) + if (status == STATUS_INVALID_IMAGE_WIN_16) + { + if ((image_info->DUMMYUNIONNAME.DUMMYSTRUCTNAME.WineFakeDll)) + { + TRACE( "Found fake 16 bit %s, search complete\n", debugstr_us(nt_name)); + } + else + { + TRACE( "%s is 16 bit, but not fake, continuing search\n", debugstr_us(nt_name)); + status = STATUS_IMAGE_MACHINE_TYPE_MISMATCH; + NtClose( *mapping ); + *mapping = NULL; + } + } + else if (!is_valid_binary( handle, image_info )) { TRACE( "%s is for arch %x, continuing search\n", debugstr_us(nt_name), image_info->Machine ); status = STATUS_IMAGE_MACHINE_TYPE_MISMATCH; @@ -2723,15 +2737,19 @@ static NTSTATUS find_builtin_without_file( const WCHAR *name, UNICODE_STRING *ne if (found_image) status = STATUS_IMAGE_MACHINE_TYPE_MISMATCH;
done: - RtlFreeUnicodeString( new_name ); + if (!status) { + RtlFreeUnicodeString( new_name ); new_name->Length = (4 + wcslen(system_dir) + wcslen(name)) * sizeof(WCHAR); new_name->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, new_name->Length + sizeof(WCHAR) ); wcscpy( new_name->Buffer, L"\??\" ); wcscat( new_name->Buffer, system_dir ); wcscat( new_name->Buffer, name ); } + else if (status != STATUS_INVALID_IMAGE_WIN_16) + RtlFreeUnicodeString( new_name ); + return status; }
@@ -2800,7 +2818,7 @@ static NTSTATUS find_dll_file( const WCHAR *load_path, const WCHAR *libname, con UNICODE_STRING *nt_name, WINE_MODREF **pwm, HANDLE *mapping, SECTION_IMAGE_INFORMATION *image_info, struct file_id *id ) { - WCHAR *ext, *dllname; + WCHAR *ext, *dllname, *ch; NTSTATUS status; ULONG wow64_old_value = 0;
@@ -2854,7 +2872,46 @@ static NTSTATUS find_dll_file( const WCHAR *load_path, const WCHAR *libname, con
/* 16-bit files can't be loaded from the prefix */ if (status && libname[0] && libname[1] && !wcscmp( libname + wcslen(libname) - 2, L"16" ) && !contains_path( libname )) + { status = find_builtin_without_file( libname, nt_name, pwm, mapping, image_info, id ); + if ((status == STATUS_INVALID_IMAGE_WIN_16) && image_info->DUMMYUNIONNAME.DUMMYSTRUCTNAME.WineFakeDll) + { + TRACE( "Name %s ", debugstr_us(nt_name) ); + ch =wcsstr( nt_name->Buffer, L"-windows\" ); + if (ch) + { + *(++ch) = L'u'; + *(++ch) = L'n'; + *(++ch) = L'i'; + *(++ch) = L'x'; + ch++; + do *ch = *(ch + 3); + while (*(ch++)); + nt_name->Length -= 3 * sizeof(WCHAR); + } + if ((nt_name->Length < 3 * sizeof(WCHAR)) || wcsncmp( nt_name->Buffer + (nt_name->Length / sizeof(WCHAR) - 3 ), L".so", 3)) + { + if (ch) + { + wcscat( nt_name->Buffer, L".so" ); + nt_name->Length += 3 * sizeof(WCHAR); + } + else + { + if (!(ch = RtlAllocateHeap( GetProcessHeap(), 0, + (nt_name->Length + 4 * sizeof(WCHAR))))) + return STATUS_NO_MEMORY; + wcscpy( ch, nt_name->Buffer ); + wcscat( ch, L".so" ); + RtlFreeHeap( GetProcessHeap(), 0, nt_name->Buffer ); + nt_name->Buffer = ch; + nt_name->Length += 3 * sizeof(WCHAR); + } + } + TRACE( " converted to %s\n", debugstr_us(nt_name) ); + status = STATUS_INVALID_IMAGE_NOT_MZ; + } + }
if (status == STATUS_IMAGE_MACHINE_TYPE_MISMATCH) status = STATUS_INVALID_IMAGE_FORMAT;
Oleh Nykyforchyn oleh.nyk@gmail.com writes:
If fake 16 bit dll is found by create_mapping and status is set to STATUS_IMAGE_INVALID_WIN_16, do not release the mapping, but return it with WineFakeDll flag set to allow ntdll properly discover this dll by NtCreateSection and handle by find_dll_file.
That's very hackish. Besides, NtCreateSection is a public API, you can't change its behavior like that.