public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: add memory_layout.h
Date: Fri, 28 Oct 2022 14:27:21 +0000 (GMT)	[thread overview]
Message-ID: <20221028142725.77A48385D0C0@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=c0776fa7bade123d5b33e44a9282f12dc3d2e10e

commit c0776fa7bade123d5b33e44a9282f12dc3d2e10e
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Wed Oct 26 21:06:54 2022 +0200

    Cygwin: add memory_layout.h
    
    Collect all info about memory layout in one header file, so
    the mem layout is documented in one logical place and not
    in heap.cc arbitrarily.
    
    Use info from this file throughout.
    
    This is to prepare for ASLR support.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/create_posix_thread.cc         |  4 --
 winsup/cygwin/local_includes/memory_layout.h | 59 ++++++++++++++++++++++++++++
 winsup/cygwin/local_includes/mmap_alloc.h    |  6 +--
 winsup/cygwin/mm/heap.cc                     | 18 +--------
 4 files changed, 62 insertions(+), 25 deletions(-)

diff --git a/winsup/cygwin/create_posix_thread.cc b/winsup/cygwin/create_posix_thread.cc
index 534600fd1..8e06099e4 100644
--- a/winsup/cygwin/create_posix_thread.cc
+++ b/winsup/cygwin/create_posix_thread.cc
@@ -107,10 +107,6 @@ pthread_wrapper (PVOID arg)
   api_fatal ("Dumb thinko in pthread handling.  Whip the developer.");
 }
 
-/* The memory region used for thread stacks. The memory layout is outlined
-   in heap.cc, function eval_start_address(). */
-#define THREAD_STORAGE_LOW	0x600000000L
-#define THREAD_STORAGE_HIGH	0x800000000L
 /* We provide the stacks always in 1 Megabyte slots */
 #define THREAD_STACK_SLOT	0x000100000L	/* 1 Meg */
 /* Maximum stack size returned from the pool. */
diff --git a/winsup/cygwin/local_includes/memory_layout.h b/winsup/cygwin/local_includes/memory_layout.h
new file mode 100644
index 000000000..77ab61984
--- /dev/null
+++ b/winsup/cygwin/local_includes/memory_layout.h
@@ -0,0 +1,59 @@
+/* memory_layout.h: document all addresses crucial to the fixed memory
+		    layout of Cygwin processes.
+
+This file is part of Cygwin.
+
+This software is a copyrighted work licensed under the terms of the
+Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
+details. */
+
+/* We use fixed addresses outside the low 32 bit arena, which is exclusively
+   used by the OS now:
+     - The executable starts at 0x1:00400000L
+     - The Cygwin DLL starts at 0x1:80040000L
+     - Rebased DLLs are located from 0x2:00000000L up to 0x4:00000000L
+     - auto-image-based DLLs are located from 0x4:00000000L up to 0x6:00000000L
+     - Thread stacks are located from 0x6:00000000L up to 0x8:00000000L.
+     - So the heap starts at 0x8:00000000L. */
+
+  /* TODO: Make Cygwin work with ASLR.
+     - The executable starts at 0x1:00400000L
+     - Rebased non-ASLRed DLLs from 0x2:00000000L up to 0x4:00000000L
+     - auto-image-based non-ASLRed DLLs from 0x4:00000000L up to 0x6:00000000L
+     - Thread stacks are located from 0x6:00000000L up to 0x8:00000000L.
+     - cygheap from 0x8:00000000L up to 0xa:00000000L.
+     - So the heap starts at 0xa:00000000L. */
+
+/* This is where the Cygwin executables are loaded to. */
+#define EXECUTABLE_ADDRESS		0x100400000UL
+
+/* Fixed address set by the linker. The Cygwin DLL will have this address set
+   in the DOS header. Keep this area free with ASLR, for the case where
+   dynamicbase is accidentally not set in the PE/COFF header of the DLL. */
+#define CYGWIN_DLL_ADDRESS		0x180040000UL
+
+/* Rebased DLLs are located in this 16 Gigs arena.  Will be kept for
+   backward compatibility. */
+#define REBASED_DLL_STORAGE_LOW		0x200000000UL
+#define REBASED_DLL_STORAGE_HIGH	0x400000000UL
+
+/* Auto-image-based DLLs are located in this 16 Gigs arena.  This is used
+   by the linker to set a default address for DLLs. */
+#define AUTOBASED_DLL_STORAGE_LOW	0x400000000UL
+#define AUTOBASED_DLL_STORAGE_HIGH	0x600000000UL
+
+/* Storage area for thread stacks. */
+#define THREAD_STORAGE_LOW		0x600000000UL
+#define THREAD_STORAGE_HIGH		0x800000000UL
+
+/* This is where the user heap starts.  There's no defined end address.
+   The user heap pontentially grows into the mmap arena.  However,
+   the user heap grows upwar4ds and the mmap arena grows downwards,
+   so there's not much chance to meet unluckily. */
+#define USERHEAP_START			0x800000000UL
+
+/* The memory region used for memory maps.
+   Up to Win 8 only 44 bit address space, 48 bit starting witrh 8.1, so
+   the max value is variable. */
+#define MMAP_STORAGE_LOW	0x001000000000L /* Leave ~32 Gigs for heap. */
+#define MMAP_STORAGE_HIGH       wincap.mmap_storage_high ()
diff --git a/winsup/cygwin/local_includes/mmap_alloc.h b/winsup/cygwin/local_includes/mmap_alloc.h
index 8d6aebcaf..86a42aee1 100644
--- a/winsup/cygwin/local_includes/mmap_alloc.h
+++ b/winsup/cygwin/local_includes/mmap_alloc.h
@@ -1,8 +1,4 @@
-/* The memory region used for memory maps */
-#define MMAP_STORAGE_LOW	0x001000000000L	/* Leave 32 Gigs for heap. */
-/* Up to Win 8 only supporting 44 bit address space, starting with Win 8.1
-   48 bit address space. */
-#define MMAP_STORAGE_HIGH	wincap.mmap_storage_high ()
+#include "memory_layout.h"
 
 class mmap_allocator
 {
diff --git a/winsup/cygwin/mm/heap.cc b/winsup/cygwin/mm/heap.cc
index 14c42e45c..5b24a197f 100644
--- a/winsup/cygwin/mm/heap.cc
+++ b/winsup/cygwin/mm/heap.cc
@@ -15,6 +15,7 @@ details. */
 #include "cygheap.h"
 #include "child_info.h"
 #include "ntdll.h"
+#include "memory_layout.h"
 #include <sys/param.h>
 
 #define assert(x)
@@ -26,21 +27,6 @@ static ptrdiff_t page_const;
 /* Chunksize of subsequent heap reservations. */
 #define RAISEHEAP_SIZE (1 * 1024 * 1024)
 
-static uintptr_t
-eval_start_address ()
-{
-  /* We choose a fixed address outside the low 32 bit arena, which is
-     exclusively used by the OS now:
-     - The executable starts at 0x1:00400000L
-     - The Cygwin DLL starts at 0x1:80040000L
-     - Rebased DLLs are located from 0x2:00000000L up to 0x4:00000000L
-     - auto-image-based DLLs are located from 0x4:00000000L up to 0x6:00000000L
-     - Thread stacks are located from 0x6:00000000L up to 0x8:00000000L.
-     - So the heap starts at 0x8:00000000L. */
-  uintptr_t start_address = 0x800000000L;
-  return start_address;
-}
-
 static SIZE_T
 eval_initial_heap_size ()
 {
@@ -77,7 +63,7 @@ user_heap_info::init ()
   page_const = wincap.page_size ();
   if (!base)
     {
-      uintptr_t start_address = eval_start_address ();
+      uintptr_t start_address = USERHEAP_START;
       PVOID largest_found = NULL;
       SIZE_T largest_found_size = 0;
       SIZE_T ret;

                 reply	other threads:[~2022-10-28 14:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221028142725.77A48385D0C0@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).