Revision: 21997
Author:   [email protected]
Date:     Wed Jun 25 08:20:42 2014 UTC
Log:      Remove dependency from platform.h implementations on isolate

BUG=none
[email protected]
LOG=n

Review URL: https://codereview.chromium.org/347223004
http://code.google.com/p/v8/source/detail?r=21997

Modified:
 /branches/bleeding_edge/src/platform-posix.cc
 /branches/bleeding_edge/src/platform-win32.cc
 /branches/bleeding_edge/src/platform.h
 /branches/bleeding_edge/src/utils/random-number-generator.h
 /branches/bleeding_edge/src/v8.cc

=======================================
--- /branches/bleeding_edge/src/platform-posix.cc Fri Jun 20 08:40:11 2014 UTC +++ /branches/bleeding_edge/src/platform-posix.cc Wed Jun 25 08:20:42 2014 UTC
@@ -43,8 +43,9 @@

 #include "src/v8.h"

-#include "src/isolate-inl.h"
+#include "src/base/lazy-instance.h"
 #include "src/platform.h"
+#include "src/utils/random-number-generator.h"

 #ifdef V8_FAST_TLS_SUPPORTED
 #include "src/base/atomicops.h"
@@ -184,6 +185,15 @@
   mprotect(address, size, PROT_NONE);
 #endif
 }
+
+
+static base::LazyInstance<RandomNumberGenerator>::type
+    platform_random_number_generator = LAZY_INSTANCE_INITIALIZER;
+
+
+void OS::SetRandomSeed(int64_t seed) {
+  platform_random_number_generator.Pointer()->SetSeed(seed);
+}


 void* OS::GetRandomMmapAddr() {
@@ -198,42 +208,36 @@
   // Dynamic tools do not support custom mmap addresses.
   return NULL;
 #endif
-  Isolate* isolate = Isolate::UncheckedCurrent();
-  // Note that the current isolate isn't set up in a call path via
- // CpuFeatures::Probe. We don't care about randomization in this case because
-  // the code page is immediately freed.
-  if (isolate != NULL) {
-    uintptr_t raw_addr;
- isolate->random_number_generator()->NextBytes(&raw_addr, sizeof(raw_addr));
+  uintptr_t raw_addr;
+  platform_random_number_generator.Pointer()->NextBytes(&raw_addr,
+                                                        sizeof(raw_addr));
 #if V8_TARGET_ARCH_X64
- // Currently available CPUs have 48 bits of virtual addressing. Truncate
-    // the hint address to 46 bits to give the kernel a fighting chance of
-    // fulfilling our placement request.
-    raw_addr &= V8_UINT64_C(0x3ffffffff000);
+  // Currently available CPUs have 48 bits of virtual addressing.  Truncate
+  // the hint address to 46 bits to give the kernel a fighting chance of
+  // fulfilling our placement request.
+  raw_addr &= V8_UINT64_C(0x3ffffffff000);
 #else
-    raw_addr &= 0x3ffff000;
+  raw_addr &= 0x3ffff000;

 # ifdef __sun
- // For our Solaris/illumos mmap hint, we pick a random address in the bottom - // half of the top half of the address space (that is, the third quarter). - // Because we do not MAP_FIXED, this will be treated only as a hint -- the - // system will not fail to mmap() because something else happens to already - // be mapped at our random address. We deliberately set the hint high enough - // to get well above the system's break (that is, the heap); Solaris and - // illumos will try the hint and if that fails allocate as if there were - // no hint at all. The high hint prevents the break from getting hemmed in
-    // at low values, ceding half of the address space to the system heap.
-    raw_addr += 0x80000000;
+ // For our Solaris/illumos mmap hint, we pick a random address in the bottom + // half of the top half of the address space (that is, the third quarter). + // Because we do not MAP_FIXED, this will be treated only as a hint -- the + // system will not fail to mmap() because something else happens to already + // be mapped at our random address. We deliberately set the hint high enough
+  // to get well above the system's break (that is, the heap); Solaris and
+  // illumos will try the hint and if that fails allocate as if there were
+ // no hint at all. The high hint prevents the break from getting hemmed in
+  // at low values, ceding half of the address space to the system heap.
+  raw_addr += 0x80000000;
 # else
-    // The range 0x20000000 - 0x60000000 is relatively unpopulated across a
-    // variety of ASLR modes (PAE kernel, NX compat mode, etc) and on macos
-    // 10.6 and 10.7.
-    raw_addr += 0x20000000;
+  // The range 0x20000000 - 0x60000000 is relatively unpopulated across a
+  // variety of ASLR modes (PAE kernel, NX compat mode, etc) and on macos
+  // 10.6 and 10.7.
+  raw_addr += 0x20000000;
 # endif
 #endif
-    return reinterpret_cast<void*>(raw_addr);
-  }
-  return NULL;
+  return reinterpret_cast<void*>(raw_addr);
 }


=======================================
--- /branches/bleeding_edge/src/platform-win32.cc Fri Jun 13 16:43:27 2014 UTC +++ /branches/bleeding_edge/src/platform-win32.cc Wed Jun 25 08:20:42 2014 UTC
@@ -19,8 +19,9 @@

 #include "src/v8.h"

-#include "src/isolate-inl.h"
+#include "src/base/lazy-instance.h"
 #include "src/platform.h"
+#include "src/utils/random-number-generator.h"

 #ifdef _MSC_VER

@@ -706,33 +707,35 @@
   }
   return allocate_alignment;
 }
+
+
+static base::LazyInstance<RandomNumberGenerator>::type
+    platform_random_number_generator = LAZY_INSTANCE_INITIALIZER;
+
+
+void OS::SetRandomSeed(int64_t seed) {
+  platform_random_number_generator.Pointer()->SetSeed(seed);
+}


 void* OS::GetRandomMmapAddr() {
-  Isolate* isolate = Isolate::UncheckedCurrent();
-  // Note that the current isolate isn't set up in a call path via
- // CpuFeatures::Probe. We don't care about randomization in this case because
-  // the code page is immediately freed.
-  if (isolate != NULL) {
-    // The address range used to randomize RWX allocations in OS::Allocate
-    // Try not to map pages into the default range that windows loads DLLs
-    // Use a multiple of 64k to prevent committing unused memory.
-    // Note: This does not guarantee RWX regions will be within the
-    // range kAllocationRandomAddressMin to kAllocationRandomAddressMax
+  // The address range used to randomize RWX allocations in OS::Allocate
+  // Try not to map pages into the default range that windows loads DLLs
+  // Use a multiple of 64k to prevent committing unused memory.
+  // Note: This does not guarantee RWX regions will be within the
+  // range kAllocationRandomAddressMin to kAllocationRandomAddressMax
 #ifdef V8_HOST_ARCH_64_BIT
-    static const intptr_t kAllocationRandomAddressMin = 0x0000000080000000;
-    static const intptr_t kAllocationRandomAddressMax = 0x000003FFFFFF0000;
+  static const intptr_t kAllocationRandomAddressMin = 0x0000000080000000;
+  static const intptr_t kAllocationRandomAddressMax = 0x000003FFFFFF0000;
 #else
-    static const intptr_t kAllocationRandomAddressMin = 0x04000000;
-    static const intptr_t kAllocationRandomAddressMax = 0x3FFF0000;
+  static const intptr_t kAllocationRandomAddressMin = 0x04000000;
+  static const intptr_t kAllocationRandomAddressMax = 0x3FFF0000;
 #endif
-    uintptr_t address =
-        (isolate->random_number_generator()->NextInt() << kPageSizeBits) |
-        kAllocationRandomAddressMin;
-    address &= kAllocationRandomAddressMax;
-    return reinterpret_cast<void *>(address);
-  }
-  return NULL;
+  uintptr_t address =
+ (platform_random_number_generator.Pointer()->NextInt() << kPageSizeBits) |
+      kAllocationRandomAddressMin;
+  address &= kAllocationRandomAddressMax;
+  return reinterpret_cast<void *>(address);
 }


=======================================
--- /branches/bleeding_edge/src/platform.h      Fri Jun 13 16:43:27 2014 UTC
+++ /branches/bleeding_edge/src/platform.h      Wed Jun 25 08:20:42 2014 UTC
@@ -212,6 +212,10 @@
   // Assign memory as a guard page so that access will cause an exception.
   static void Guard(void* address, const size_t size);

+  // Set a fixed random seed for the random number generator used for
+  // GetRandomMmapAddr.
+  static void SetRandomSeed(int64_t seed);
+
   // Generate a random address to be used for hinting mmap().
   static void* GetRandomMmapAddr();

=======================================
--- /branches/bleeding_edge/src/utils/random-number-generator.h Tue Jun 17 16:27:19 2014 UTC +++ /branches/bleeding_edge/src/utils/random-number-generator.h Wed Jun 25 08:20:42 2014 UTC
@@ -71,13 +71,15 @@
   // Fills the elements of a specified array of bytes with random numbers.
   void NextBytes(void* buffer, size_t buflen);

+  // Override the current ssed.
+  void SetSeed(int64_t seed);
+
  private:
   static const int64_t kMultiplier = V8_2PART_UINT64_C(0x5, deece66d);
   static const int64_t kAddend = 0xb;
   static const int64_t kMask = V8_2PART_UINT64_C(0xffff, ffffffff);

   int Next(int bits) V8_WARN_UNUSED_RESULT;
-  void SetSeed(int64_t seed);

   int64_t seed_;
 };
=======================================
--- /branches/bleeding_edge/src/v8.cc   Fri Jun 20 08:40:11 2014 UTC
+++ /branches/bleeding_edge/src/v8.cc   Wed Jun 25 08:20:42 2014 UTC
@@ -89,6 +89,8 @@
     FLAG_gc_global = true;
     FLAG_max_semi_space_size = 1;
   }
+
+  if (FLAG_random_seed != 0) OS::SetRandomSeed(FLAG_random_seed);

 #ifdef V8_USE_DEFAULT_PLATFORM
   platform_ = new DefaultPlatform;

--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to