]> sourceware.org Git - newlib-cygwin.git/commitdiff
* heap.cc (eval_start_address): New static function to evaluate the
authorCorinna Vinschen <corinna@vinschen.de>
Thu, 21 Jul 2011 15:23:24 +0000 (15:23 +0000)
committerCorinna Vinschen <corinna@vinschen.de>
Thu, 21 Jul 2011 15:23:24 +0000 (15:23 +0000)
best start address for the application heap.
(heap_init): Call eval_start_address to fetch the start value for
start_address.  Move preceeding comment to eval_start_address.

winsup/cygwin/ChangeLog
winsup/cygwin/heap.cc

index 011d47bd6be4cfb1e63029a3460b27073d2eaf80..adc25c8a392921930af89d3ffe757d9af82f2c2b 100644 (file)
@@ -1,3 +1,10 @@
+2011-07-21  Corinna Vinschen  <corinna@vinschen.de>
+
+       * heap.cc (eval_start_address): New static function to evaluate the
+       best start address for the application heap.
+       (heap_init): Call eval_start_address to fetch the start value for
+       start_address.  Move preceeding comment to eval_start_address.
+
 2011-07-21  Yaakov Selkowitz  <yselkowitz@users.sourceforge.net>
 
        * cygwin.din (pthread_condattr_getclock): Export.
index ee27484c033d20e77da24f9b3becaba2e6f673d8..6d5032e5b0fd46bea144f9548bf5d9f429c2f72b 100644 (file)
@@ -25,6 +25,34 @@ static unsigned page_const;
 
 #define MINHEAP_SIZE (4 * 1024 * 1024)
 
+static uintptr_t
+eval_start_address ()
+{
+  /* Starting with Vista, Windows performs heap ASLR.  This spoils the entire
+     region below 0x20000000 for us, because that region is used by Windows
+     to randomize heap and stack addresses.  Therefore we put our heap into a
+     safe region starting at 0x20000000.  This should work right from the start
+     in 99% of the cases. */
+  uintptr_t start_address = 0x20000000L;
+  if (wincap.is_wow64 ())
+    {
+      /* However, if we're running on a 64 bit system, we test here if the
+        executable is large address aware.  If so, the application gets a
+        4 Gigs virtual address space, with almost all of the upper 2 Gigs
+        being unused by Windows (only PEB and TEBs are allocated here,
+        apparently).  So what we do here is to test if the large address
+        awareness flag is set in the file header and, if so, allocate our
+        heap in that region.  What we get are 1.999 Gigs free for heap,
+        thread stacks, and shared memory regions. */
+      PIMAGE_DOS_HEADER idh = (PIMAGE_DOS_HEADER) GetModuleHandle (NULL);
+      PIMAGE_NT_HEADERS32 inh = (PIMAGE_NT_HEADERS32)
+                               ((PBYTE) idh + idh->e_lfanew);
+      if (inh->FileHeader.Characteristics & IMAGE_FILE_LARGE_ADDRESS_AWARE)
+       start_address = 0x80000000L;
+    }
+  return start_address;
+}
+
 /* Initialize the heap at process start up.  */
 void
 heap_init ()
@@ -36,13 +64,7 @@ heap_init ()
   page_const = wincap.page_size ();
   if (!cygheap->user_heap.base)
     {
-      /* Starting with Vista, Windows performs heap ASLR.  This spoils
-        the entire region below 0x20000000 for us, because that region
-        is used by Windows to randomize heap and stack addresses.
-        Therefore we put our heap into a safe region starting at 0x20000000.
-        This should work right from the start in 99% of the cases.  But,
-        there's always a but.  Read on... */
-      uintptr_t start_address = 0x20000000L;
+      uintptr_t start_address = eval_start_address ();
       PVOID largest_found = NULL;
       size_t largest_found_size = 0;
       SIZE_T ret;
This page took 0.032796 seconds and 5 git commands to generate.