sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +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) { |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 43 | CHECK(!v8::V8::IsExecutionTerminating()); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 44 | v8::V8::TerminateExecution(); |
| 45 | return v8::Undefined(); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | v8::Handle<v8::Value> Fail(const v8::Arguments& args) { |
| 50 | CHECK(false); |
| 51 | return v8::Undefined(); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | v8::Handle<v8::Value> Loop(const v8::Arguments& args) { |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 56 | CHECK(!v8::V8::IsExecutionTerminating()); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 57 | v8::Handle<v8::String> source = |
| 58 | v8::String::New("try { doloop(); fail(); } catch(e) { fail(); }"); |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 59 | v8::Handle<v8::Value> result = v8::Script::Compile(source)->Run(); |
| 60 | CHECK(result.IsEmpty()); |
| 61 | CHECK(v8::V8::IsExecutionTerminating()); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 62 | return v8::Undefined(); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | v8::Handle<v8::Value> DoLoop(const v8::Arguments& args) { |
| 67 | v8::TryCatch try_catch; |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 68 | CHECK(!v8::V8::IsExecutionTerminating()); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 69 | v8::Script::Compile(v8::String::New("function f() {" |
| 70 | " var term = true;" |
| 71 | " try {" |
| 72 | " while(true) {" |
| 73 | " if (term) terminate();" |
| 74 | " term = false;" |
| 75 | " }" |
| 76 | " fail();" |
| 77 | " } catch(e) {" |
| 78 | " fail();" |
| 79 | " }" |
| 80 | "}" |
| 81 | "f()"))->Run(); |
| 82 | CHECK(try_catch.HasCaught()); |
| 83 | CHECK(try_catch.Exception()->IsNull()); |
| 84 | CHECK(try_catch.Message().IsEmpty()); |
| 85 | CHECK(!try_catch.CanContinue()); |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 86 | CHECK(v8::V8::IsExecutionTerminating()); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 87 | return v8::Undefined(); |
| 88 | } |
| 89 | |
| 90 | |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 91 | v8::Handle<v8::Value> DoLoopNoCall(const v8::Arguments& args) { |
| 92 | v8::TryCatch try_catch; |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 93 | CHECK(!v8::V8::IsExecutionTerminating()); |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 94 | v8::Script::Compile(v8::String::New("var term = true;" |
| 95 | "while(true) {" |
| 96 | " if (term) terminate();" |
| 97 | " term = false;" |
| 98 | "}"))->Run(); |
| 99 | CHECK(try_catch.HasCaught()); |
| 100 | CHECK(try_catch.Exception()->IsNull()); |
| 101 | CHECK(try_catch.Message().IsEmpty()); |
| 102 | CHECK(!try_catch.CanContinue()); |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 103 | CHECK(v8::V8::IsExecutionTerminating()); |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 104 | return v8::Undefined(); |
| 105 | } |
| 106 | |
| 107 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 108 | v8::Handle<v8::ObjectTemplate> CreateGlobalTemplate( |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 109 | v8::InvocationCallback terminate, |
| 110 | v8::InvocationCallback doloop) { |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 111 | v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
| 112 | global->Set(v8::String::New("terminate"), |
| 113 | v8::FunctionTemplate::New(terminate)); |
| 114 | global->Set(v8::String::New("fail"), v8::FunctionTemplate::New(Fail)); |
| 115 | global->Set(v8::String::New("loop"), v8::FunctionTemplate::New(Loop)); |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 116 | global->Set(v8::String::New("doloop"), v8::FunctionTemplate::New(doloop)); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 117 | return global; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | // Test that a single thread of JavaScript execution can terminate |
| 122 | // itself. |
| 123 | TEST(TerminateOnlyV8ThreadFromThreadItself) { |
| 124 | v8::HandleScope scope; |
| 125 | v8::Handle<v8::ObjectTemplate> global = |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 126 | CreateGlobalTemplate(TerminateCurrentThread, DoLoop); |
| 127 | v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); |
| 128 | v8::Context::Scope context_scope(context); |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 129 | CHECK(!v8::V8::IsExecutionTerminating()); |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 130 | // Run a loop that will be infinite if thread termination does not work. |
| 131 | v8::Handle<v8::String> source = |
| 132 | v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
| 133 | v8::Script::Compile(source)->Run(); |
| 134 | // Test that we can run the code again after thread termination. |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 135 | CHECK(!v8::V8::IsExecutionTerminating()); |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 136 | v8::Script::Compile(source)->Run(); |
| 137 | context.Dispose(); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | // Test that a single thread of JavaScript execution can terminate |
| 142 | // itself in a loop that performs no calls. |
| 143 | TEST(TerminateOnlyV8ThreadFromThreadItselfNoLoop) { |
| 144 | v8::HandleScope scope; |
| 145 | v8::Handle<v8::ObjectTemplate> global = |
| 146 | CreateGlobalTemplate(TerminateCurrentThread, DoLoopNoCall); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 147 | v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); |
| 148 | v8::Context::Scope context_scope(context); |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 149 | CHECK(!v8::V8::IsExecutionTerminating()); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 150 | // Run a loop that will be infinite if thread termination does not work. |
| 151 | v8::Handle<v8::String> source = |
| 152 | v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
| 153 | v8::Script::Compile(source)->Run(); |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 154 | CHECK(!v8::V8::IsExecutionTerminating()); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 155 | // Test that we can run the code again after thread termination. |
| 156 | v8::Script::Compile(source)->Run(); |
| 157 | context.Dispose(); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | class TerminatorThread : public v8::internal::Thread { |
| 162 | void Run() { |
| 163 | semaphore->Wait(); |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 164 | CHECK(!v8::V8::IsExecutionTerminating()); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 165 | v8::V8::TerminateExecution(); |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | |
| 170 | // Test that a single thread of JavaScript execution can be terminated |
| 171 | // from the side by another thread. |
| 172 | TEST(TerminateOnlyV8ThreadFromOtherThread) { |
| 173 | semaphore = v8::internal::OS::CreateSemaphore(0); |
| 174 | TerminatorThread thread; |
| 175 | thread.Start(); |
| 176 | |
| 177 | v8::HandleScope scope; |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 178 | v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(Signal, DoLoop); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 179 | v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); |
| 180 | v8::Context::Scope context_scope(context); |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 181 | CHECK(!v8::V8::IsExecutionTerminating()); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 182 | // Run a loop that will be infinite if thread termination does not work. |
| 183 | v8::Handle<v8::String> source = |
| 184 | v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
| 185 | v8::Script::Compile(source)->Run(); |
| 186 | |
| 187 | thread.Join(); |
| 188 | delete semaphore; |
| 189 | semaphore = NULL; |
| 190 | context.Dispose(); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | class LoopingThread : public v8::internal::Thread { |
| 195 | public: |
| 196 | void Run() { |
| 197 | v8::Locker locker; |
| 198 | v8::HandleScope scope; |
| 199 | v8_thread_id_ = v8::V8::GetCurrentThreadId(); |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 200 | v8::Handle<v8::ObjectTemplate> global = |
| 201 | CreateGlobalTemplate(Signal, DoLoop); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 202 | v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); |
| 203 | v8::Context::Scope context_scope(context); |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 204 | CHECK(!v8::V8::IsExecutionTerminating()); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 205 | // Run a loop that will be infinite if thread termination does not work. |
| 206 | v8::Handle<v8::String> source = |
| 207 | v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
| 208 | v8::Script::Compile(source)->Run(); |
| 209 | context.Dispose(); |
| 210 | } |
| 211 | |
| 212 | int GetV8ThreadId() { return v8_thread_id_; } |
| 213 | |
| 214 | private: |
| 215 | int v8_thread_id_; |
| 216 | }; |
| 217 | |
| 218 | |
| 219 | // Test that multiple threads using V8 can be terminated from another |
| 220 | // thread when using Lockers and preemption. |
| 221 | TEST(TerminateMultipleV8Threads) { |
| 222 | { |
| 223 | v8::Locker locker; |
| 224 | v8::V8::Initialize(); |
| 225 | v8::Locker::StartPreemption(1); |
| 226 | semaphore = v8::internal::OS::CreateSemaphore(0); |
| 227 | } |
| 228 | LoopingThread thread1; |
| 229 | thread1.Start(); |
| 230 | LoopingThread thread2; |
| 231 | thread2.Start(); |
| 232 | // Wait until both threads have signaled the semaphore. |
| 233 | semaphore->Wait(); |
| 234 | semaphore->Wait(); |
| 235 | { |
| 236 | v8::Locker locker; |
| 237 | v8::V8::TerminateExecution(thread1.GetV8ThreadId()); |
| 238 | v8::V8::TerminateExecution(thread2.GetV8ThreadId()); |
| 239 | } |
| 240 | thread1.Join(); |
| 241 | thread2.Join(); |
| 242 | |
| 243 | delete semaphore; |
| 244 | semaphore = NULL; |
| 245 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 246 | |
| 247 | |
| 248 | int call_count = 0; |
| 249 | |
| 250 | |
| 251 | v8::Handle<v8::Value> TerminateOrReturnObject(const v8::Arguments& args) { |
| 252 | if (++call_count == 10) { |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 253 | CHECK(!v8::V8::IsExecutionTerminating()); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 254 | v8::V8::TerminateExecution(); |
| 255 | return v8::Undefined(); |
| 256 | } |
| 257 | v8::Local<v8::Object> result = v8::Object::New(); |
| 258 | result->Set(v8::String::New("x"), v8::Integer::New(42)); |
| 259 | return result; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | v8::Handle<v8::Value> LoopGetProperty(const v8::Arguments& args) { |
| 264 | v8::TryCatch try_catch; |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 265 | CHECK(!v8::V8::IsExecutionTerminating()); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 266 | v8::Script::Compile(v8::String::New("function f() {" |
| 267 | " try {" |
| 268 | " while(true) {" |
| 269 | " terminate_or_return_object().x;" |
| 270 | " }" |
| 271 | " fail();" |
| 272 | " } catch(e) {" |
| 273 | " fail();" |
| 274 | " }" |
| 275 | "}" |
| 276 | "f()"))->Run(); |
| 277 | CHECK(try_catch.HasCaught()); |
| 278 | CHECK(try_catch.Exception()->IsNull()); |
| 279 | CHECK(try_catch.Message().IsEmpty()); |
| 280 | CHECK(!try_catch.CanContinue()); |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 281 | CHECK(v8::V8::IsExecutionTerminating()); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 282 | return v8::Undefined(); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | // Test that we correctly handle termination exceptions if they are |
| 287 | // triggered by the creation of error objects in connection with ICs. |
| 288 | TEST(TerminateLoadICException) { |
| 289 | v8::HandleScope scope; |
| 290 | v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
| 291 | global->Set(v8::String::New("terminate_or_return_object"), |
| 292 | v8::FunctionTemplate::New(TerminateOrReturnObject)); |
| 293 | global->Set(v8::String::New("fail"), v8::FunctionTemplate::New(Fail)); |
| 294 | global->Set(v8::String::New("loop"), |
| 295 | v8::FunctionTemplate::New(LoopGetProperty)); |
| 296 | |
| 297 | v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); |
| 298 | v8::Context::Scope context_scope(context); |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 299 | CHECK(!v8::V8::IsExecutionTerminating()); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 300 | // Run a loop that will be infinite if thread termination does not work. |
| 301 | v8::Handle<v8::String> source = |
| 302 | v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
| 303 | call_count = 0; |
| 304 | v8::Script::Compile(source)->Run(); |
| 305 | // Test that we can run the code again after thread termination. |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame^] | 306 | CHECK(!v8::V8::IsExecutionTerminating()); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 307 | call_count = 0; |
| 308 | v8::Script::Compile(source)->Run(); |
| 309 | context.Dispose(); |
| 310 | } |