Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 30 | #include "isolate.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 31 | #include "bootstrapper.h" |
| 32 | #include "debug.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 33 | #include "deoptimizer.h" |
| 34 | #include "heap-profiler.h" |
| 35 | #include "hydrogen.h" |
| 36 | #include "lithium-allocator.h" |
| 37 | #include "log.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 38 | #include "runtime-profiler.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | #include "serialize.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 40 | |
| 41 | namespace v8 { |
| 42 | namespace internal { |
| 43 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 44 | static Mutex* init_once_mutex = OS::CreateMutex(); |
| 45 | static bool init_once_called = false; |
| 46 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 51 | bool V8::use_crankshaft_ = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 53 | static Mutex* entropy_mutex = OS::CreateMutex(); |
| 54 | static EntropySource entropy_source; |
| 55 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 56 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 57 | bool V8::Initialize(Deserializer* des) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 58 | InitializeOncePerProcess(); |
| 59 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 60 | // The current thread may not yet had entered an isolate to run. |
| 61 | // Note the Isolate::Current() may be non-null because for various |
| 62 | // initialization purposes an initializing thread may be assigned an isolate |
| 63 | // but not actually enter it. |
| 64 | if (i::Isolate::CurrentPerIsolateThreadData() == NULL) { |
| 65 | i::Isolate::EnterDefaultIsolate(); |
| 66 | } |
| 67 | |
| 68 | ASSERT(i::Isolate::CurrentPerIsolateThreadData() != NULL); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 69 | ASSERT(i::Isolate::CurrentPerIsolateThreadData()->thread_id().Equals( |
| 70 | i::ThreadId::Current())); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 71 | ASSERT(i::Isolate::CurrentPerIsolateThreadData()->isolate() == |
| 72 | i::Isolate::Current()); |
| 73 | |
| 74 | if (IsDead()) return false; |
| 75 | |
| 76 | Isolate* isolate = Isolate::Current(); |
| 77 | if (isolate->IsInitialized()) return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 78 | |
| 79 | is_running_ = true; |
| 80 | has_been_setup_ = true; |
| 81 | has_fatal_error_ = false; |
| 82 | has_been_disposed_ = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 83 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 84 | return isolate->Init(des); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
| 88 | void V8::SetFatalError() { |
| 89 | is_running_ = false; |
| 90 | has_fatal_error_ = true; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | void V8::TearDown() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 95 | Isolate* isolate = Isolate::Current(); |
| 96 | ASSERT(isolate->IsDefaultIsolate()); |
| 97 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 98 | if (!has_been_setup_ || has_been_disposed_) return; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 99 | isolate->TearDown(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 100 | |
| 101 | is_running_ = false; |
| 102 | has_been_disposed_ = true; |
| 103 | } |
| 104 | |
| 105 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 106 | static void seed_random(uint32_t* state) { |
| 107 | for (int i = 0; i < 2; ++i) { |
| 108 | if (FLAG_random_seed != 0) { |
| 109 | state[i] = FLAG_random_seed; |
| 110 | } else if (entropy_source != NULL) { |
| 111 | uint32_t val; |
| 112 | ScopedLock lock(entropy_mutex); |
| 113 | entropy_source(reinterpret_cast<unsigned char*>(&val), sizeof(uint32_t)); |
| 114 | state[i] = val; |
| 115 | } else { |
| 116 | state[i] = random(); |
| 117 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 118 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 122 | // Random number generator using George Marsaglia's MWC algorithm. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 123 | static uint32_t random_base(uint32_t* state) { |
| 124 | // Initialize seed using the system random(). |
| 125 | // No non-zero seed will ever become zero again. |
| 126 | if (state[0] == 0) seed_random(state); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 127 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 128 | // Mix the bits. Never replaces state[i] with 0 if it is nonzero. |
| 129 | state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16); |
| 130 | state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16); |
| 131 | |
| 132 | return (state[0] << 14) + (state[1] & 0x3FFFF); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | void V8::SetEntropySource(EntropySource source) { |
| 137 | entropy_source = source; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
| 141 | // Used by JavaScript APIs |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 142 | uint32_t V8::Random(Isolate* isolate) { |
| 143 | ASSERT(isolate == Isolate::Current()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 144 | return random_base(isolate->random_seed()); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | |
| 148 | // Used internally by the JIT and memory allocator for security |
| 149 | // purposes. So, we keep a different state to prevent informations |
| 150 | // leaks that could be used in an exploit. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 151 | uint32_t V8::RandomPrivate(Isolate* isolate) { |
| 152 | ASSERT(isolate == Isolate::Current()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 153 | return random_base(isolate->private_random_seed()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 157 | bool V8::IdleNotification() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 158 | // Returning true tells the caller that there is no need to call |
| 159 | // IdleNotification again. |
| 160 | if (!FLAG_use_idle_notification) return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 161 | |
| 162 | // Tell the heap that it may want to adjust. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 163 | return HEAP->IdleNotification(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 167 | // Use a union type to avoid type-aliasing optimizations in GCC. |
| 168 | typedef union { |
| 169 | double double_value; |
| 170 | uint64_t uint64_t_value; |
| 171 | } double_int_union; |
| 172 | |
| 173 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 174 | Object* V8::FillHeapNumberWithRandom(Object* heap_number, Isolate* isolate) { |
| 175 | uint64_t random_bits = Random(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 176 | // Make a double* from address (heap_number + sizeof(double)). |
| 177 | double_int_union* r = reinterpret_cast<double_int_union*>( |
| 178 | reinterpret_cast<char*>(heap_number) + |
| 179 | HeapNumber::kValueOffset - kHeapObjectTag); |
| 180 | // Convert 32 random bits to 0.(32 random bits) in a double |
| 181 | // by computing: |
| 182 | // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). |
| 183 | const double binary_million = 1048576.0; |
| 184 | r->double_value = binary_million; |
| 185 | r->uint64_t_value |= random_bits; |
| 186 | r->double_value -= binary_million; |
| 187 | |
| 188 | return heap_number; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 191 | |
| 192 | void V8::InitializeOncePerProcess() { |
| 193 | ScopedLock lock(init_once_mutex); |
| 194 | if (init_once_called) return; |
| 195 | init_once_called = true; |
| 196 | |
| 197 | // Setup the platform OS support. |
| 198 | OS::Setup(); |
| 199 | |
| 200 | use_crankshaft_ = FLAG_crankshaft; |
| 201 | |
| 202 | if (Serializer::enabled()) { |
| 203 | use_crankshaft_ = false; |
| 204 | } |
| 205 | |
| 206 | CPU::Setup(); |
| 207 | if (!CPU::SupportsCrankshaft()) { |
| 208 | use_crankshaft_ = false; |
| 209 | } |
| 210 | |
| 211 | RuntimeProfiler::GlobalSetup(); |
| 212 | |
| 213 | // Peephole optimization might interfere with deoptimization. |
| 214 | FLAG_peephole_optimization = !use_crankshaft_; |
| 215 | } |
| 216 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 217 | } } // namespace v8::internal |