Reviewers: Erik Corry,

Description:
Moved random generator state to global context.

Change Random to take global context, not isolate.

BUG=v8:864


Please review this at http://codereview.chromium.org/8162014/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M src/api.cc
  M src/arm/full-codegen-arm.cc
  M src/bootstrapper.cc
  M src/contexts.h
  M src/globals.h
  M src/ia32/full-codegen-ia32.cc
  M src/isolate.h
  M src/mips/full-codegen-mips.cc
  M src/objects.cc
  M src/v8.h
  M src/v8.cc
  M src/x64/full-codegen-x64.cc


Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index 7266390a42df0da865aa0213713f95d87cb218de..aac420b8dd1c7b5702c5796350207f5fb925190a 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -4073,8 +4073,9 @@ Persistent<Context> v8::Context::New(
   }
   // Leave V8.

-  if (env.is_null())
+  if (env.is_null()) {
     return Persistent<Context>();
+  }
   return Persistent<Context>(Utils::ToLocal(env));
 }

Index: src/arm/full-codegen-arm.cc
diff --git a/src/arm/full-codegen-arm.cc b/src/arm/full-codegen-arm.cc
index 2ee1594f8671227fad384b33ea3192ce531c3a73..dfcf3f61c177fb9513cff185bb9d62736bfffb71 100644
--- a/src/arm/full-codegen-arm.cc
+++ b/src/arm/full-codegen-arm.cc
@@ -2782,7 +2782,8 @@ void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
   // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
   if (CpuFeatures::IsSupported(VFP3)) {
     __ PrepareCallCFunction(1, r0);
-    __ mov(r0, Operand(ExternalReference::isolate_address()));
+    __ ldr(r0, ContextOperand(context_register(), Context::GLOBAL_INDEX));
+    __ ldr(r0, FieldMemOperand(r0, GlobalObject::kGlobalContextOffset));
__ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);

     CpuFeatures::Scope scope(VFP3);
@@ -2802,8 +2803,9 @@ void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
     __ mov(r0, r4);
   } else {
     __ PrepareCallCFunction(2, r0);
+    __ ldr(r1, ContextOperand(context_register(), Context::GLOBAL_INDEX));
     __ mov(r0, Operand(r4));
-    __ mov(r1, Operand(ExternalReference::isolate_address()));
+    __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalContextOffset));
     __ CallCFunction(
ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
   }
Index: src/bootstrapper.cc
diff --git a/src/bootstrapper.cc b/src/bootstrapper.cc
index dc722cb7493a87b4205557701099471c53712a70..d48fe036a9c4bcdaf0b0c5b7b2048a8ebcd61367 100644
--- a/src/bootstrapper.cc
+++ b/src/bootstrapper.cc
@@ -1221,6 +1221,14 @@ void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,

   // Initialize the data slot.
   global_context()->set_data(heap->undefined_value());
+
+  {
+    // Initialize the random seed slot.
+    Handle<ByteArray> zeroed_byte_array(
+        factory->NewByteArray(kRandomStateSize));
+    global_context()->set_random_seed(*zeroed_byte_array);
+    memset(zeroed_byte_array->GetDataStartAddress(), 0, kRandomStateSize);
+  }
 }


Index: src/contexts.h
diff --git a/src/contexts.h b/src/contexts.h
index b80475f0f7c4e199e1544eabed91920a60cdd65f..d9d375b653fe1531ebae64f40926db54b7063bdc 100644
--- a/src/contexts.h
+++ b/src/contexts.h
@@ -138,7 +138,8 @@ enum BindingFlags {
     to_complete_property_descriptor) \
   V(DERIVED_HAS_TRAP_INDEX, JSFunction, derived_has_trap) \
   V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap) \
-  V(DERIVED_SET_TRAP_INDEX, JSFunction, derived_set_trap)
+  V(DERIVED_SET_TRAP_INDEX, JSFunction, derived_set_trap) \
+  V(RANDOM_SEED_INDEX, ByteArray, random_seed)

 // JSFunctions are pairs (context, function code), sometimes also called
 // closures. A Context object is used to represent function contexts and
@@ -258,6 +259,7 @@ class Context: public FixedArray {
     DERIVED_HAS_TRAP_INDEX,
     DERIVED_GET_TRAP_INDEX,
     DERIVED_SET_TRAP_INDEX,
+    RANDOM_SEED_INDEX,

     // Properties from here are treated as weak references by the full GC.
     // Scavenge treats them as strong references.
Index: src/globals.h
diff --git a/src/globals.h b/src/globals.h
index d0c78d6e22b7e82a6289c69015fde7faa7d66f0e..cbe7abdf664bff14ed4d9d048027b424017dba87 100644
--- a/src/globals.h
+++ b/src/globals.h
@@ -230,6 +230,9 @@ const int kPointerSize  = sizeof(void*);     // NOLINT

 const int kDoubleSizeLog2 = 3;

+// Size of the state of a the random number generator.
+const int kRandomStateSize = 2 * kIntSize;
+
 #if V8_HOST_ARCH_64_BIT
 const int kPointerSizeLog2 = 3;
 const intptr_t kIntptrSignBit = V8_INT64_C(0x8000000000000000);
Index: src/ia32/full-codegen-ia32.cc
diff --git a/src/ia32/full-codegen-ia32.cc b/src/ia32/full-codegen-ia32.cc
index d45a9cdaeea3ab3725152056bb3a87ecde43e77d..a543916d06f11397f8ef09a3d7b765c5a9a9dedd 100644
--- a/src/ia32/full-codegen-ia32.cc
+++ b/src/ia32/full-codegen-ia32.cc
@@ -2781,9 +2781,10 @@ void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
   __ bind(&heapnumber_allocated);

   __ PrepareCallCFunction(1, ebx);
-  __ mov(Operand(esp, 0), Immediate(ExternalReference::isolate_address()));
-  __ CallCFunction(ExternalReference::random_uint32_function(isolate()),
-                   1);
+  __ mov(eax, ContextOperand(context_register(), Context::GLOBAL_INDEX));
+  __ mov(eax, FieldOperand(eax, GlobalObject::kGlobalContextOffset));
+  __ mov(Operand(esp, 0), eax);
+ __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);

   // Convert 32 random bits in eax to 0.(32 random bits) in a double
   // by computing:
Index: src/isolate.h
diff --git a/src/isolate.h b/src/isolate.h
index 01ab04e60ac144d098e1977d267e80da36ff156a..462e508aa716754565c44cd5579668bd1f3c70fb 100644
--- a/src/isolate.h
+++ b/src/isolate.h
@@ -311,7 +311,6 @@ class HashMap;
V(int, bad_char_shift_table, kUC16AlphabetSize) \ V(int, good_suffix_shift_table, (kBMMaxShift + 1)) \ V(int, suffix_table, (kBMMaxShift + 1)) \ - V(uint32_t, random_seed, 2) \ V(uint32_t, private_random_seed, 2) \
   ISOLATE_INIT_DEBUG_ARRAY_LIST(V)

Index: src/mips/full-codegen-mips.cc
diff --git a/src/mips/full-codegen-mips.cc b/src/mips/full-codegen-mips.cc
index 57da2e7d111691a2a150626c020ca1c469bb8379..81e72bdf8603a12c88efa6c57ee3d19826a85ef9 100644
--- a/src/mips/full-codegen-mips.cc
+++ b/src/mips/full-codegen-mips.cc
@@ -2775,7 +2775,8 @@ void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
   // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
   if (CpuFeatures::IsSupported(FPU)) {
     __ PrepareCallCFunction(1, a0);
-    __ li(a0, Operand(ExternalReference::isolate_address()));
+    __ lw(a0, ContextOperand(cp, Context::GLOBAL_INDEX));
+    __ lw(a0, FieldOperand(a0, GlobalObject::kGlobalContextOffset));
__ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);


@@ -2793,7 +2794,8 @@ void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
   } else {
     __ PrepareCallCFunction(2, a0);
     __ mov(a0, s0);
-    __ li(a1, Operand(ExternalReference::isolate_address()));
+    __ lw(a1, ContextOperand(cp, Context::GLOBAL_INDEX));
+    __ lw(a1, FieldOperand(a1, GlobalObject::kGlobalContextOffset));
     __ CallCFunction(
ExternalReference::fill_heap_number_with_random_function(isolate()), 2);
   }
Index: src/objects.cc
diff --git a/src/objects.cc b/src/objects.cc
index b4db9e5605a9212851a4b46cd4fbefadde2d8685..95c66acfcd46e291e234019811e9e5eb633511bc 100644
--- a/src/objects.cc
+++ b/src/objects.cc
@@ -3295,7 +3295,7 @@ Smi* JSReceiver::GenerateIdentityHash() {
   do {
     // Generate a random 32-bit hash value but limit range to fit
     // within a smi.
-    hash_value = V8::Random(isolate) & Smi::kMaxValue;
+    hash_value = V8::RandomPrivate(isolate) & Smi::kMaxValue;
     attempts++;
   } while (hash_value == 0 && attempts < 30);
   hash_value = hash_value != 0 ? hash_value : 1;  // never return 0
Index: src/v8.cc
diff --git a/src/v8.cc b/src/v8.cc
index aebcc2f1d945104b5708673920737d6a80c8e621..ca43e166354921f47165b83750dc8e12fb1b8631 100644
--- a/src/v8.cc
+++ b/src/v8.cc
@@ -141,9 +141,10 @@ void V8::SetEntropySource(EntropySource source) {


 // Used by JavaScript APIs
-uint32_t V8::Random(Isolate* isolate) {
-  ASSERT(isolate == Isolate::Current());
-  return random_base(isolate->random_seed());
+uint32_t V8::Random(Context* context) {
+  ASSERT(context->IsGlobalContext());
+  ByteArray* seed = context->random_seed();
+ return random_base(reinterpret_cast<uint32_t*>(seed->GetDataStartAddress()));
 }


@@ -173,8 +174,9 @@ typedef union {
 } double_int_union;


-Object* V8::FillHeapNumberWithRandom(Object* heap_number, Isolate* isolate) {
-  uint64_t random_bits = Random(isolate);
+Object* V8::FillHeapNumberWithRandom(Object* heap_number,
+                                     Context* context) {
+  uint64_t random_bits = Random(context);
   // Make a double* from address (heap_number + sizeof(double)).
   double_int_union* r = reinterpret_cast<double_int_union*>(
       reinterpret_cast<char*>(heap_number) +
Index: src/v8.h
diff --git a/src/v8.h b/src/v8.h
index 2e039d429fa16b2b4a0200dc0f68ad6fcea31890..01feefce6ff3f475a1ca35e4c540eda11f485c30 100644
--- a/src/v8.h
+++ b/src/v8.h
@@ -96,14 +96,14 @@ class V8 : public AllStatic {
   // generation.
   static void SetEntropySource(EntropySource source);
   // Random number generation support. Not cryptographically safe.
-  static uint32_t Random(Isolate* isolate);
+  static uint32_t Random(Context* context);
   // We use random numbers internally in memory allocation and in the
   // compilers for security. In order to prevent information leaks we
   // use a separate random state for internal random number
   // generation.
   static uint32_t RandomPrivate(Isolate* isolate);
   static Object* FillHeapNumberWithRandom(Object* heap_number,
-                                          Isolate* isolate);
+                                          Context* context);

   // Idle notification directly from the API.
   static bool IdleNotification();
Index: src/x64/full-codegen-x64.cc
diff --git a/src/x64/full-codegen-x64.cc b/src/x64/full-codegen-x64.cc
index 1d9b8fffc2ba31442de8df8b9edddc25620ef549..d2775129e40fde3de16ddc5e49174c4e261f6ed5 100644
--- a/src/x64/full-codegen-x64.cc
+++ b/src/x64/full-codegen-x64.cc
@@ -2658,9 +2658,12 @@ void FullCodeGenerator::EmitRandomHeapNumber(ZoneList<Expression*>* args) {
   // The fresh HeapNumber is in rbx, which is callee-save on both x64 ABIs.
   __ PrepareCallCFunction(1);
 #ifdef _WIN64
-  __ LoadAddress(rcx, ExternalReference::isolate_address());
+  __ movq(rcx, ContextOperand(context_register(), Context::GLOBAL_INDEX));
+  __ movq(rcx, FieldOperand(rcx, GlobalObject::kGlobalContextOffset));
+
 #else
-  __ LoadAddress(rdi, ExternalReference::isolate_address());
+  __ movq(rdi, ContextOperand(context_register(), Context::GLOBAL_INDEX));
+  __ movq(rdi, FieldOperand(rdi, GlobalObject::kGlobalContextOffset));
 #endif
__ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);



--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to