From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id E21133858D28; Wed, 18 Jan 2023 12:38:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E21133858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1674045510; bh=EU7US4x9KgQ7Y8rmcb5c1wyC8ZvrGqW+TygN4Z02gOs=; h=From:To:Subject:Date:From; b=C8yDgYSbyOh9jK2aG5EkG793LD5voJ8IZg5jtd9Vk6rGQfwhh5c7e0RPdaI7q5stx BUjzU8xdNJWpL01ubyHsOOYIdA+0NswrSbpCoS/YE0N5nWBvjUatu8FZP6GBTsTHKS O9JKCx5Q+tqck8S37/0Zlme5UI4CHF1/b9ACdcHA= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/main] Cygwin: drop fixed addresses for standard shared regions X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/main X-Git-Oldrev: 9ddd48ee1b8d736ebbd0b0bdf146ecf96774cd8a X-Git-Newrev: 31302b267b86a9e9190dd260f2ff4f0c74410bf3 Message-Id: <20230118123830.E21133858D28@sourceware.org> Date: Wed, 18 Jan 2023 12:38:30 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D31302b267b8= 6a9e9190dd260f2ff4f0c74410bf3 commit 31302b267b86a9e9190dd260f2ff4f0c74410bf3 Author: Corinna Vinschen AuthorDate: Wed Jan 18 13:16:50 2023 +0100 Commit: Corinna Vinschen CommitDate: Wed Jan 18 13:16:50 2023 +0100 Cygwin: drop fixed addresses for standard shared regions =20 With the previous commit 9ddd48ee1b8d ("Cygwin: /proc//maps: print real shared region addresses"), the real addresses of the standard shared regions (cygwin, user, myself, shared console) are read from the printed process itself. We don't need fixed addresses anymore, so drop the definitions and simplify open_shared. =20 Signed-off-by: Corinna Vinschen Diff: --- winsup/cygwin/local_includes/memory_layout.h | 9 +---- winsup/cygwin/mm/shared.cc | 60 ++++++++----------------= ---- 2 files changed, 18 insertions(+), 51 deletions(-) diff --git a/winsup/cygwin/local_includes/memory_layout.h b/winsup/cygwin/l= ocal_includes/memory_layout.h index 21e00c1bcf55..c763442a9ef4 100644 --- a/winsup/cygwin/local_includes/memory_layout.h +++ b/winsup/cygwin/local_includes/memory_layout.h @@ -16,16 +16,9 @@ details. */ dynamicbase is accidentally not set in the PE/COFF header of the DLL. */ #define CYGWIN_DLL_ADDRESS 0x180040000UL =20 -/* Default addresses of required standard shared regions (Cygwin shared, - user shared, myself, shared console). */ -#define CYGWIN_REGION_ADDRESS 0x1a0000000UL -#define USER_REGION_ADDRESS 0x1a1000000UL -#define MYSELF_REGION_ADDRESS 0x1a2000000UL -#define SHARED_CONSOLE_REGION_ADDRESS 0x1a3000000UL - /* Area for non-fixed-address Cygwin-specific shared memory regions. Fall= back for standard shared regions if the can't load at their default address.= */ -#define SHARED_REGIONS_ADDRESS_LOW 0x1a4000000UL +#define SHARED_REGIONS_ADDRESS_LOW 0x1a0000000UL #define SHARED_REGIONS_ADDRESS_HIGH 0x200000000UL =20 /* Rebased DLLs are located in this 16 Gigs arena. Will be kept for diff --git a/winsup/cygwin/mm/shared.cc b/winsup/cygwin/mm/shared.cc index d7d6547ec3bb..eb798c2ddb5f 100644 --- a/winsup/cygwin/mm/shared.cc +++ b/winsup/cygwin/mm/shared.cc @@ -113,15 +113,6 @@ shared_name (WCHAR *ret_buf, const WCHAR *str, int num) #define page_const ((ptrdiff_t) 65535) #define pround(n) ((ptrdiff_t)(((n) + page_const) & ~page_const)) =20 -/* FIXME: With ASLR, maybe we should ASLR the shared regions, too? */ -static uintptr_t region_address[] =3D -{ - CYGWIN_REGION_ADDRESS, /* SH_CYGWIN_SHARED */ - USER_REGION_ADDRESS, /* SH_USER_SHARED */ - MYSELF_REGION_ADDRESS, /* SH_MYSELF */ - SHARED_CONSOLE_REGION_ADDRESS, /* SH_SHARED_CONSOLE */ - 0 -}; static NO_COPY uintptr_t next_address =3D SHARED_REGIONS_ADDRESS_LOW; =20 void * @@ -139,7 +130,7 @@ open_shared (const WCHAR *name, int n, HANDLE& shared_h= , DWORD size, { WCHAR map_buf[MAX_PATH]; WCHAR *mapname =3D NULL; - void *shared =3D NULL; + void *shared; void *addr; =20 created =3D false; @@ -166,47 +157,30 @@ open_shared (const WCHAR *name, int n, HANDLE& shared= _h, DWORD size, return NULL; } =20 - if (m < SH_TOTAL_SIZE && !dynamically_loaded) - { - /* Fixed regions. Don't do that if Cygwin gets dynamically loaded. - The process loading the DLL might be configured with High-Entropy - ASLR. Chances for collisions are pretty high. + /* Locate shared regions in the area between SHARED_REGIONS_ADDRESS_LOW + and SHARED_REGIONS_ADDRESS_HIGH, retrying until we have a slot. + Don't use MapViewOfFile3 (loader deadlock during fork. */ + bool loop =3D false; =20 - Note that we don't actually *need* fixed addresses. The only - advantage is reproducibility to help /proc//maps along. */ - addr =3D (void *) region_address[m]; + addr =3D (void *) next_address; + do + { shared =3D MapViewOfFileEx (shared_h, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0, addr); - } - /* Also catch the unlikely case that a fixed region can't be mapped at t= he - fixed address. */ - if (!shared) - { - /* Locate shared regions in the area between SHARED_REGIONS_ADDRESS_= LOW - and SHARED_REGIONS_ADDRESS_HIGH, retrying until we have a slot. - Don't use MapViewOfFile3 (loader deadlock during fork. */ - bool loop =3D false; - - addr =3D (void *) next_address; - do + if (!shared) { - shared =3D MapViewOfFileEx (shared_h, FILE_MAP_READ | FILE_MAP_WRITE, - 0, 0, 0, addr); - if (!shared) + next_address +=3D wincap.allocation_granularity (); + if (next_address >=3D SHARED_REGIONS_ADDRESS_HIGH) { - next_address +=3D wincap.allocation_granularity (); - if (next_address >=3D SHARED_REGIONS_ADDRESS_HIGH) - { - if (loop) - break; - next_address =3D SHARED_REGIONS_ADDRESS_LOW; - loop =3D true; - } - addr =3D (void *) next_address; + if (loop) + break; + next_address =3D SHARED_REGIONS_ADDRESS_LOW; + loop =3D true; } + addr =3D (void *) next_address; } - while (!shared); } + while (!shared); =20 if (!shared) api_fatal ("MapViewOfFileEx '%W'(%p, size %u, m %d, created %d), %E. "