Home | History | Annotate | Download | only in src
      1 // Copyright 2011 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 #include "v8.h"
     29 
     30 #include "isolate.h"
     31 #include "bootstrapper.h"
     32 #include "debug.h"
     33 #include "deoptimizer.h"
     34 #include "heap-profiler.h"
     35 #include "hydrogen.h"
     36 #include "lithium-allocator.h"
     37 #include "log.h"
     38 #include "runtime-profiler.h"
     39 #include "serialize.h"
     40 
     41 namespace v8 {
     42 namespace internal {
     43 
     44 static Mutex* init_once_mutex = OS::CreateMutex();
     45 static bool init_once_called = false;
     46 
     47 bool V8::is_running_ = false;
     48 bool V8::has_been_setup_ = false;
     49 bool V8::has_been_disposed_ = false;
     50 bool V8::has_fatal_error_ = false;
     51 bool V8::use_crankshaft_ = true;
     52 
     53 
     54 bool V8::Initialize(Deserializer* des) {
     55   InitializeOncePerProcess();
     56 
     57   // The current thread may not yet had entered an isolate to run.
     58   // Note the Isolate::Current() may be non-null because for various
     59   // initialization purposes an initializing thread may be assigned an isolate
     60   // but not actually enter it.
     61   if (i::Isolate::CurrentPerIsolateThreadData() == NULL) {
     62     i::Isolate::EnterDefaultIsolate();
     63   }
     64 
     65   ASSERT(i::Isolate::CurrentPerIsolateThreadData() != NULL);
     66   ASSERT(i::Isolate::CurrentPerIsolateThreadData()->thread_id().Equals(
     67            i::ThreadId::Current()));
     68   ASSERT(i::Isolate::CurrentPerIsolateThreadData()->isolate() ==
     69          i::Isolate::Current());
     70 
     71   if (IsDead()) return false;
     72 
     73   Isolate* isolate = Isolate::Current();
     74   if (isolate->IsInitialized()) return true;
     75 
     76   is_running_ = true;
     77   has_been_setup_ = true;
     78   has_fatal_error_ = false;
     79   has_been_disposed_ = false;
     80 
     81   return isolate->Init(des);
     82 }
     83 
     84 
     85 void V8::SetFatalError() {
     86   is_running_ = false;
     87   has_fatal_error_ = true;
     88 }
     89 
     90 
     91 void V8::TearDown() {
     92   Isolate* isolate = Isolate::Current();
     93   ASSERT(isolate->IsDefaultIsolate());
     94 
     95   if (!has_been_setup_ || has_been_disposed_) return;
     96   isolate->TearDown();
     97 
     98   is_running_ = false;
     99   has_been_disposed_ = true;
    100 }
    101 
    102 
    103 static uint32_t random_seed() {
    104   if (FLAG_random_seed == 0) {
    105     return random();
    106   }
    107   return FLAG_random_seed;
    108 }
    109 
    110 
    111 typedef struct {
    112   uint32_t hi;
    113   uint32_t lo;
    114 } random_state;
    115 
    116 
    117 // Random number generator using George Marsaglia's MWC algorithm.
    118 static uint32_t random_base(random_state *state) {
    119   // Initialize seed using the system random(). If one of the seeds
    120   // should ever become zero again, or if random() returns zero, we
    121   // avoid getting stuck with zero bits in hi or lo by re-initializing
    122   // them on demand.
    123   if (state->hi == 0) state->hi = random_seed();
    124   if (state->lo == 0) state->lo = random_seed();
    125 
    126   // Mix the bits.
    127   state->hi = 36969 * (state->hi & 0xFFFF) + (state->hi >> 16);
    128   state->lo = 18273 * (state->lo & 0xFFFF) + (state->lo >> 16);
    129   return (state->hi << 16) + (state->lo & 0xFFFF);
    130 }
    131 
    132 
    133 // Used by JavaScript APIs
    134 uint32_t V8::Random(Isolate* isolate) {
    135   ASSERT(isolate == Isolate::Current());
    136   // TODO(isolates): move lo and hi to isolate
    137   static random_state state = {0, 0};
    138   return random_base(&state);
    139 }
    140 
    141 
    142 // Used internally by the JIT and memory allocator for security
    143 // purposes. So, we keep a different state to prevent informations
    144 // leaks that could be used in an exploit.
    145 uint32_t V8::RandomPrivate(Isolate* isolate) {
    146   ASSERT(isolate == Isolate::Current());
    147   // TODO(isolates): move lo and hi to isolate
    148   static random_state state = {0, 0};
    149   return random_base(&state);
    150 }
    151 
    152 
    153 bool V8::IdleNotification() {
    154   // Returning true tells the caller that there is no need to call
    155   // IdleNotification again.
    156   if (!FLAG_use_idle_notification) return true;
    157 
    158   // Tell the heap that it may want to adjust.
    159   return HEAP->IdleNotification();
    160 }
    161 
    162 
    163 // Use a union type to avoid type-aliasing optimizations in GCC.
    164 typedef union {
    165   double double_value;
    166   uint64_t uint64_t_value;
    167 } double_int_union;
    168 
    169 
    170 Object* V8::FillHeapNumberWithRandom(Object* heap_number, Isolate* isolate) {
    171   uint64_t random_bits = Random(isolate);
    172   // Make a double* from address (heap_number + sizeof(double)).
    173   double_int_union* r = reinterpret_cast<double_int_union*>(
    174       reinterpret_cast<char*>(heap_number) +
    175       HeapNumber::kValueOffset - kHeapObjectTag);
    176   // Convert 32 random bits to 0.(32 random bits) in a double
    177   // by computing:
    178   // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
    179   const double binary_million = 1048576.0;
    180   r->double_value = binary_million;
    181   r->uint64_t_value |=  random_bits;
    182   r->double_value -= binary_million;
    183 
    184   return heap_number;
    185 }
    186 
    187 
    188 void V8::InitializeOncePerProcess() {
    189   ScopedLock lock(init_once_mutex);
    190   if (init_once_called) return;
    191   init_once_called = true;
    192 
    193   // Setup the platform OS support.
    194   OS::Setup();
    195 
    196   use_crankshaft_ = FLAG_crankshaft;
    197 
    198   if (Serializer::enabled()) {
    199     use_crankshaft_ = false;
    200   }
    201 
    202   CPU::Setup();
    203   if (!CPU::SupportsCrankshaft()) {
    204     use_crankshaft_ = false;
    205   }
    206 
    207   RuntimeProfiler::GlobalSetup();
    208 
    209   // Peephole optimization might interfere with deoptimization.
    210   FLAG_peephole_optimization = !use_crankshaft_;
    211 }
    212 
    213 } }  // namespace v8::internal
    214