www.fgks.org   »   [go: up one dir, main page]

Skip to content

Commit

Permalink
msys2-runtime: Implement fixes for Wine
Browse files Browse the repository at this point in the history
This is intended to address msys2#682, and should eventually be upstreamed to
Cygwin to resolve https://bugs.winehq.org/show_bug.cgi?id=40528

0025: Do not directly use FastPebLock on the Wine code-path

Older versions of Wine did not populate this pointer, so instead call
the exported ntdll.dll function, which works correctly in all cases.

0026: Do not use FAST_CWD on Wine

The Cygwin cwd-handling code manipulates two internal Windows
structures, the Process Information Block and an undocumented global
dubbed FAST_CWD, in order to support some POSIX-like behaviours.

Wine doesn't have FAST_CWD, so disable that part of the manipulation
when Wine is detected.

0027: Unapplied logging change

This simply outputs some logs so that a crash or other bad behaviour can
be positively confirmed as being within or outside the CWD-override
support in Cygwin.

It should be dropped before applying, assuming everything else works.
  • Loading branch information
TBBle committed Apr 28, 2018
1 parent 324e213 commit 375689d
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
From 3c3e60fdef10970b6646c220a3e69f8c43fa13ef Mon Sep 17 00:00:00 2001
From: "Paul \"TBBle\" Hampson" <Paul.Hampson@Pobox.com>
Date: Sat, 28 Apr 2018 17:17:27 +1000
Subject: [PATCH 25/27] Use RtlAcquirePebLock instead of peb.FastPebLock

Older Wine versions did not use peb.FastPebLock. This is the only
code-path that Wine shoudl hit, so the other uses of peb.FastPebLock
have been retained.
---
winsup/cygwin/path.cc | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc
index cf6422341..1e1ba9c3d 100644
--- a/winsup/cygwin/path.cc
+++ b/winsup/cygwin/path.cc
@@ -4402,11 +4402,11 @@ cwdstuff::override_win32_cwd (bool init, ULONG old_dismount_count)
}
else if (upp_cwd_hdl == NULL)
return;
- RtlEnterCriticalSection (peb.FastPebLock);
+ RtlAcquirePebLock ();
fcwd_access_t::SetDirHandleFromBufferPointer(upp_cwd_str.Buffer, dir);
h = upp_cwd_hdl;
upp_cwd_hdl = dir;
- RtlLeaveCriticalSection (peb.FastPebLock);
+ RtlReleasePebLock ();
/* The handle on init is always a fresh one, not the handle inherited
from the parent process. We always have to close it here. */
NtClose (h);
--
2.16.2.windows.1

115 changes: 115 additions & 0 deletions msys2-runtime/0026-Disable-FAST_CWD-when-Wine-is-detected.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
From 5c7809d3d8fbfab64cbafa6f4fd6f56b37128e16 Mon Sep 17 00:00:00 2001
From: "Paul \"TBBle\" Hampson" <Paul.Hampson@Pobox.com>
Date: Fri, 27 Apr 2018 02:32:46 +1000
Subject: [PATCH 26/27] Disable FAST_CWD when Wine is detected

Wine doesn't use FAST_CWD internally, so manipulating the PEB is
sufficient for replacing the CWD Directory Handle.
---
winsup/cygwin/cygheap.h | 4 +++-
winsup/cygwin/path.cc | 29 ++++++++++++++++++++++++++++-
2 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/cygheap.h b/winsup/cygwin/cygheap.h
index abbf9ec07..ed58285e6 100644
--- a/winsup/cygwin/cygheap.h
+++ b/winsup/cygwin/cygheap.h
@@ -204,7 +204,8 @@ public:
enum fcwd_version_t {
FCWD_OLD,
FCWD_W7,
- FCWD_W8
+ FCWD_W8,
+ FCWD_DISABLED
};

/* This class is used to store the CWD. The CWD storage in the
@@ -281,6 +282,7 @@ public:
void FillIn (HANDLE dir, PUNICODE_STRING name, ULONG old_dismount_count);
static void SetDirHandleFromBufferPointer (PWCHAR buf_p, HANDLE dir);
static void SetVersionFromPointer (PBYTE buf_p, bool is_buffer);
+ static void Disable (void);
};

class cwdstuff
diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc
index 1e1ba9c3d..936dc33cd 100644
--- a/winsup/cygwin/path.cc
+++ b/winsup/cygwin/path.cc
@@ -3947,6 +3947,7 @@ fcwd_access_t::SetFSCharacteristics (LONG val)
/* Special case FSCharacteristics. Didn't exist originally. */
switch (fast_cwd_version ())
{
+ case FCWD_DISABLED:
case FCWD_OLD:
break;
case FCWD_W7:
@@ -4033,6 +4034,8 @@ fcwd_access_t::SetDirHandleFromBufferPointer (PWCHAR buf_p, HANDLE dir)
fcwd_access_t *f_cwd;
switch (fast_cwd_version ())
{
+ case FCWD_DISABLED:
+ return;
case FCWD_OLD:
default:
f_cwd = (fcwd_access_t *)
@@ -4070,10 +4073,19 @@ fcwd_access_t::SetVersionFromPointer (PBYTE buf_p, bool is_buffer)
fast_cwd_version () = FCWD_OLD;
}

+void
+fcwd_access_t::Disable (void)
+{
+ fast_cwd_version () = FCWD_DISABLED;
+}
+
/* This function scans the code in ntdll.dll to find the address of the
global variable used to access the CWD. While the pointer is global,
it's not exported from the DLL, unfortunately. Therefore we have to
- use some knowledge to figure out the address. */
+ use some knowledge to figure out the address.
+ If we happen to be running under Wine, returns -1 to disable this
+ whole mechanism, as Wine's implementation does not have FAST_CWD.
+ */

#ifdef __x86_64__

@@ -4086,6 +4098,10 @@ find_fast_cwd_pointer ()
HMODULE ntdll = GetModuleHandle ("ntdll.dll");
if (!ntdll)
return NULL;
+ const uint8_t *wine_get_version = (const uint8_t *)
+ GetProcAddress (ntdll, "wine_get_version");
+ if (wine_get_version)
+ return (fcwd_access_t **) -1;
const uint8_t *get_dir = (const uint8_t *)
GetProcAddress (ntdll, "RtlGetCurrentDirectory_U");
const uint8_t *ent_crit = (const uint8_t *)
@@ -4185,6 +4201,10 @@ find_fast_cwd_pointer ()
HMODULE ntdll = GetModuleHandle ("ntdll.dll");
if (!ntdll)
return NULL;
+ const uint8_t *wine_get_version = (const uint8_t *)
+ GetProcAddress (ntdll, "wine_get_version");
+ if (wine_get_version)
+ return (fcwd_access_t **) -1;
const uint8_t *get_dir = (const uint8_t *)
GetProcAddress (ntdll, "RtlGetCurrentDirectory_U");
const uint8_t *ent_crit = (const uint8_t *)
@@ -4280,6 +4300,13 @@ find_fast_cwd ()
we have to make sure we know the version of the FAST_CWD structure
used on the system. */
fcwd_access_t **f_cwd_ptr = find_fast_cwd_pointer ();
+ if (f_cwd_ptr == (fcwd_access_t **) -1)
+ {
+ /* FAST_CWD is disabled with prejudice e.g., Wine. In effect, the
+ PEB is canonical, so changing that is sufficient */
+ fcwd_access_t::Disable ();
+ return NULL;
+ }
if (!f_cwd_ptr)
small_printf ("Cygwin WARNING:\n"
" Couldn't compute FAST_CWD pointer. This typically occurs if you're using\n"
--
2.16.2.windows.1

55 changes: 55 additions & 0 deletions msys2-runtime/0027-Debug-logs-for-Wine-FAST_CWD.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
From 9d981752f7cfb16eb6b6f008d87cec5ba68a1c14 Mon Sep 17 00:00:00 2001
From: "Paul \"TBBle\" Hampson" <Paul.Hampson@Pobox.com>
Date: Sat, 28 Apr 2018 17:19:08 +1000
Subject: [PATCH 27/27] Debug logs for Wine FAST_CWD

---
winsup/cygwin/path.cc | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc
index 936dc33cd..a77732a67 100644
--- a/winsup/cygwin/path.cc
+++ b/winsup/cygwin/path.cc
@@ -4304,6 +4304,7 @@ find_fast_cwd ()
{
/* FAST_CWD is disabled with prejudice e.g., Wine. In effect, the
PEB is canonical, so changing that is sufficient */
+ small_printf ("Wine detected, disabling FAST_CWD\n\n");
fcwd_access_t::Disable ();
return NULL;
}
@@ -4444,6 +4445,7 @@ cwdstuff::override_win32_cwd (bool init, ULONG old_dismount_count)
void
cwdstuff::init ()
{
+ small_printf ("cwdstff::init: start\n\n");
cwd_lock.init ("cwd_lock");

/* Cygwin processes inherit the cwd from their parent. If the win32 path
@@ -4459,12 +4461,14 @@ cwdstuff::init ()
/* Initially re-open the cwd to allow POSIX semantics. */
set (NULL, NULL);
}
+ small_printf ("cwdstff::init: done\n\n");
}

/* Chdir and fill out the elements of a cwdstuff struct. */
int
cwdstuff::set (path_conv *nat_cwd, const char *posix_cwd)
{
+ small_printf ("cwdstff::set: start\n\n");
NTSTATUS status;
UNICODE_STRING upath;
PEB &peb = *NtCurrentTeb ()->Peb;
@@ -4715,6 +4719,7 @@ cwdstuff::set (path_conv *nat_cwd, const char *posix_cwd)
stpcpy (posix, posix_cwd);

cwd_lock.release ();
+ small_printf ("cwdstff::set: done\n\n");
return 0;
}

--
2.16.2.windows.1

15 changes: 12 additions & 3 deletions msys2-runtime/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pkgbase=msys2-runtime
pkgname=('msys2-runtime' 'msys2-runtime-devel')
pkgver=2.10.0
pkgrel=3
pkgrel=4
pkgdesc="Cygwin POSIX emulation engine"
arch=('i686' 'x86_64')
url="https://www.cygwin.com/"
Expand Down Expand Up @@ -47,6 +47,9 @@ source=('msys2-runtime'::git://sourceware.org/git/newlib-cygwin.git#tag=cygwin-$
0022-environ.cc-New-facility-environment-variable-MSYS2_E.patch
0023-path.cc-Ignore-zero-length-exclusions.patch
0024-Cygwin-fix-build-with-GCC-7.patch
0025-Use-RtlAcquirePebLock-instead-of-peb.FastPebLock.patch
0026-Disable-FAST_CWD-when-Wine-is-detected.patch
0027-Debug-logs-for-Wine-FAST_CWD.patch
)
sha256sums=('SKIP'
'8aafdf4f62a52195a591ebe24ab671c35aa1d7c60381c8ff3526c4c1dd1fdf0e'
Expand All @@ -72,7 +75,10 @@ sha256sums=('SKIP'
'ac80823274db00b877f1e925426da1a709f00b211f77e30fe4448f5c6c31194a'
'a0fe210a8ae05f0a17a06ff79736d0908a6c3dc0fc6db444e5c4d050b84b779d'
'7cba394680f874b23fa77ea295d17f4b2a2a5f64bd376cd6afd6cf34b05f659b'
'6bf4d9abd0ec1cf3de0868df81792d3cdf4d857eb8002fd9dd8505f9541a8c29')
'6bf4d9abd0ec1cf3de0868df81792d3cdf4d857eb8002fd9dd8505f9541a8c29'
'd207bcede30a86539c12d5341d8521dc726b58fc5e236d4aab136f9c4d4a0fb6'
'0db4682119f3d5a1f21fceaa4e5854648fdc77b15c20581de95da4a8c67bdcd5'
'4a35633d98dd7c3be6b0026588c7fc040bd65018eb8cc502e5d37bc1a71c2ea2')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
Expand Down Expand Up @@ -128,7 +134,10 @@ prepare() {
0021-Add-debugging-for-strace-make_command_line.patch \
0022-environ.cc-New-facility-environment-variable-MSYS2_E.patch \
0023-path.cc-Ignore-zero-length-exclusions.patch \
0024-Cygwin-fix-build-with-GCC-7.patch
0024-Cygwin-fix-build-with-GCC-7.patch \
0025-Use-RtlAcquirePebLock-instead-of-peb.FastPebLock.patch \
0026-Disable-FAST_CWD-when-Wine-is-detected.patch
# 0027-Debug-logs-for-Wine-FAST_CWD.patch

}

Expand Down

0 comments on commit 375689d

Please sign in to comment.