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