Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame^] | 1 | // Copyright 2009 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 | #include "platform.h" |
| 30 | #include "cctest.h" |
| 31 | |
| 32 | |
| 33 | v8::internal::Semaphore* semaphore = NULL; |
| 34 | |
| 35 | |
| 36 | v8::Handle<v8::Value> Signal(const v8::Arguments& args) { |
| 37 | semaphore->Signal(); |
| 38 | return v8::Undefined(); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | v8::Handle<v8::Value> TerminateCurrentThread(const v8::Arguments& args) { |
| 43 | v8::V8::TerminateExecution(); |
| 44 | return v8::Undefined(); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | v8::Handle<v8::Value> Fail(const v8::Arguments& args) { |
| 49 | CHECK(false); |
| 50 | return v8::Undefined(); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | v8::Handle<v8::Value> Loop(const v8::Arguments& args) { |
| 55 | v8::Handle<v8::String> source = |
| 56 | v8::String::New("try { doloop(); fail(); } catch(e) { fail(); }"); |
| 57 | v8::Script::Compile(source)->Run(); |
| 58 | return v8::Undefined(); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | v8::Handle<v8::Value> DoLoop(const v8::Arguments& args) { |
| 63 | v8::TryCatch try_catch; |
| 64 | v8::Script::Compile(v8::String::New("function f() {" |
| 65 | " var term = true;" |
| 66 | " try {" |
| 67 | " while(true) {" |
| 68 | " if (term) terminate();" |
| 69 | " term = false;" |
| 70 | " }" |
| 71 | " fail();" |
| 72 | " } catch(e) {" |
| 73 | " fail();" |
| 74 | " }" |
| 75 | "}" |
| 76 | "f()"))->Run(); |
| 77 | CHECK(try_catch.HasCaught()); |
| 78 | CHECK(try_catch.Exception()->IsNull()); |
| 79 | CHECK(try_catch.Message().IsEmpty()); |
| 80 | CHECK(!try_catch.CanContinue()); |
| 81 | return v8::Undefined(); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | v8::Handle<v8::ObjectTemplate> CreateGlobalTemplate( |
| 86 | v8::InvocationCallback terminate) { |
| 87 | v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
| 88 | global->Set(v8::String::New("terminate"), |
| 89 | v8::FunctionTemplate::New(terminate)); |
| 90 | global->Set(v8::String::New("fail"), v8::FunctionTemplate::New(Fail)); |
| 91 | global->Set(v8::String::New("loop"), v8::FunctionTemplate::New(Loop)); |
| 92 | global->Set(v8::String::New("doloop"), v8::FunctionTemplate::New(DoLoop)); |
| 93 | return global; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | // Test that a single thread of JavaScript execution can terminate |
| 98 | // itself. |
| 99 | TEST(TerminateOnlyV8ThreadFromThreadItself) { |
| 100 | v8::HandleScope scope; |
| 101 | v8::Handle<v8::ObjectTemplate> global = |
| 102 | CreateGlobalTemplate(TerminateCurrentThread); |
| 103 | v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); |
| 104 | v8::Context::Scope context_scope(context); |
| 105 | // Run a loop that will be infinite if thread termination does not work. |
| 106 | v8::Handle<v8::String> source = |
| 107 | v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
| 108 | v8::Script::Compile(source)->Run(); |
| 109 | // Test that we can run the code again after thread termination. |
| 110 | v8::Script::Compile(source)->Run(); |
| 111 | context.Dispose(); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | class TerminatorThread : public v8::internal::Thread { |
| 116 | void Run() { |
| 117 | semaphore->Wait(); |
| 118 | v8::V8::TerminateExecution(); |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | |
| 123 | // Test that a single thread of JavaScript execution can be terminated |
| 124 | // from the side by another thread. |
| 125 | TEST(TerminateOnlyV8ThreadFromOtherThread) { |
| 126 | semaphore = v8::internal::OS::CreateSemaphore(0); |
| 127 | TerminatorThread thread; |
| 128 | thread.Start(); |
| 129 | |
| 130 | v8::HandleScope scope; |
| 131 | v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(Signal); |
| 132 | v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); |
| 133 | v8::Context::Scope context_scope(context); |
| 134 | // Run a loop that will be infinite if thread termination does not work. |
| 135 | v8::Handle<v8::String> source = |
| 136 | v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
| 137 | v8::Script::Compile(source)->Run(); |
| 138 | |
| 139 | thread.Join(); |
| 140 | delete semaphore; |
| 141 | semaphore = NULL; |
| 142 | context.Dispose(); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | class LoopingThread : public v8::internal::Thread { |
| 147 | public: |
| 148 | void Run() { |
| 149 | v8::Locker locker; |
| 150 | v8::HandleScope scope; |
| 151 | v8_thread_id_ = v8::V8::GetCurrentThreadId(); |
| 152 | v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(Signal); |
| 153 | v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); |
| 154 | v8::Context::Scope context_scope(context); |
| 155 | // Run a loop that will be infinite if thread termination does not work. |
| 156 | v8::Handle<v8::String> source = |
| 157 | v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
| 158 | v8::Script::Compile(source)->Run(); |
| 159 | context.Dispose(); |
| 160 | } |
| 161 | |
| 162 | int GetV8ThreadId() { return v8_thread_id_; } |
| 163 | |
| 164 | private: |
| 165 | int v8_thread_id_; |
| 166 | }; |
| 167 | |
| 168 | |
| 169 | // Test that multiple threads using V8 can be terminated from another |
| 170 | // thread when using Lockers and preemption. |
| 171 | TEST(TerminateMultipleV8Threads) { |
| 172 | { |
| 173 | v8::Locker locker; |
| 174 | v8::V8::Initialize(); |
| 175 | v8::Locker::StartPreemption(1); |
| 176 | semaphore = v8::internal::OS::CreateSemaphore(0); |
| 177 | } |
| 178 | LoopingThread thread1; |
| 179 | thread1.Start(); |
| 180 | LoopingThread thread2; |
| 181 | thread2.Start(); |
| 182 | // Wait until both threads have signaled the semaphore. |
| 183 | semaphore->Wait(); |
| 184 | semaphore->Wait(); |
| 185 | { |
| 186 | v8::Locker locker; |
| 187 | v8::V8::TerminateExecution(thread1.GetV8ThreadId()); |
| 188 | v8::V8::TerminateExecution(thread2.GetV8ThreadId()); |
| 189 | } |
| 190 | thread1.Join(); |
| 191 | thread2.Join(); |
| 192 | |
| 193 | delete semaphore; |
| 194 | semaphore = NULL; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | int call_count = 0; |
| 199 | |
| 200 | |
| 201 | v8::Handle<v8::Value> TerminateOrReturnObject(const v8::Arguments& args) { |
| 202 | if (++call_count == 10) { |
| 203 | v8::V8::TerminateExecution(); |
| 204 | return v8::Undefined(); |
| 205 | } |
| 206 | v8::Local<v8::Object> result = v8::Object::New(); |
| 207 | result->Set(v8::String::New("x"), v8::Integer::New(42)); |
| 208 | return result; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | v8::Handle<v8::Value> LoopGetProperty(const v8::Arguments& args) { |
| 213 | v8::TryCatch try_catch; |
| 214 | v8::Script::Compile(v8::String::New("function f() {" |
| 215 | " try {" |
| 216 | " while(true) {" |
| 217 | " terminate_or_return_object().x;" |
| 218 | " }" |
| 219 | " fail();" |
| 220 | " } catch(e) {" |
| 221 | " fail();" |
| 222 | " }" |
| 223 | "}" |
| 224 | "f()"))->Run(); |
| 225 | CHECK(try_catch.HasCaught()); |
| 226 | CHECK(try_catch.Exception()->IsNull()); |
| 227 | CHECK(try_catch.Message().IsEmpty()); |
| 228 | CHECK(!try_catch.CanContinue()); |
| 229 | return v8::Undefined(); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | // Test that we correctly handle termination exceptions if they are |
| 234 | // triggered by the creation of error objects in connection with ICs. |
| 235 | TEST(TerminateLoadICException) { |
| 236 | v8::HandleScope scope; |
| 237 | v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
| 238 | global->Set(v8::String::New("terminate_or_return_object"), |
| 239 | v8::FunctionTemplate::New(TerminateOrReturnObject)); |
| 240 | global->Set(v8::String::New("fail"), v8::FunctionTemplate::New(Fail)); |
| 241 | global->Set(v8::String::New("loop"), |
| 242 | v8::FunctionTemplate::New(LoopGetProperty)); |
| 243 | |
| 244 | v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); |
| 245 | v8::Context::Scope context_scope(context); |
| 246 | // Run a loop that will be infinite if thread termination does not work. |
| 247 | v8::Handle<v8::String> source = |
| 248 | v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
| 249 | call_count = 0; |
| 250 | v8::Script::Compile(source)->Run(); |
| 251 | // Test that we can run the code again after thread termination. |
| 252 | call_count = 0; |
| 253 | v8::Script::Compile(source)->Run(); |
| 254 | context.Dispose(); |
| 255 | } |