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
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.
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.
29 #include "test/cctest/cctest.h"
31 #include "src/base/platform/platform.h"
34 v8::base::Semaphore* semaphore = NULL;
37 void Signal(const v8::FunctionCallbackInfo<v8::Value>& args) {
42 void TerminateCurrentThread(const v8::FunctionCallbackInfo<v8::Value>& args) {
43 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
44 v8::V8::TerminateExecution(args.GetIsolate());
48 void Fail(const v8::FunctionCallbackInfo<v8::Value>& args) {
53 void Loop(const v8::FunctionCallbackInfo<v8::Value>& args) {
54 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
55 v8::Handle<v8::String> source = v8::String::NewFromUtf8(
56 args.GetIsolate(), "try { doloop(); fail(); } catch(e) { fail(); }");
57 v8::Handle<v8::Value> result = v8::Script::Compile(source)->Run();
58 CHECK(result.IsEmpty());
59 CHECK(v8::V8::IsExecutionTerminating(args.GetIsolate()));
63 void DoLoop(const v8::FunctionCallbackInfo<v8::Value>& args) {
64 v8::TryCatch try_catch;
65 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
66 v8::Script::Compile(v8::String::NewFromUtf8(args.GetIsolate(),
71 " if (term) terminate();"
80 CHECK(try_catch.HasCaught());
81 CHECK(try_catch.Exception()->IsNull());
82 CHECK(try_catch.Message().IsEmpty());
83 CHECK(!try_catch.CanContinue());
84 CHECK(v8::V8::IsExecutionTerminating(args.GetIsolate()));
88 void DoLoopNoCall(const v8::FunctionCallbackInfo<v8::Value>& args) {
89 v8::TryCatch try_catch;
90 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
91 v8::Script::Compile(v8::String::NewFromUtf8(args.GetIsolate(),
94 " if (term) terminate();"
97 CHECK(try_catch.HasCaught());
98 CHECK(try_catch.Exception()->IsNull());
99 CHECK(try_catch.Message().IsEmpty());
100 CHECK(!try_catch.CanContinue());
101 CHECK(v8::V8::IsExecutionTerminating(args.GetIsolate()));
105 v8::Handle<v8::ObjectTemplate> CreateGlobalTemplate(
106 v8::Isolate* isolate,
107 v8::FunctionCallback terminate,
108 v8::FunctionCallback doloop) {
109 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
110 global->Set(v8::String::NewFromUtf8(isolate, "terminate"),
111 v8::FunctionTemplate::New(isolate, terminate));
112 global->Set(v8::String::NewFromUtf8(isolate, "fail"),
113 v8::FunctionTemplate::New(isolate, Fail));
114 global->Set(v8::String::NewFromUtf8(isolate, "loop"),
115 v8::FunctionTemplate::New(isolate, Loop));
116 global->Set(v8::String::NewFromUtf8(isolate, "doloop"),
117 v8::FunctionTemplate::New(isolate, doloop));
122 // Test that a single thread of JavaScript execution can terminate
124 TEST(TerminateOnlyV8ThreadFromThreadItself) {
125 v8::HandleScope scope(CcTest::isolate());
126 v8::Handle<v8::ObjectTemplate> global =
127 CreateGlobalTemplate(CcTest::isolate(), TerminateCurrentThread, DoLoop);
128 v8::Handle<v8::Context> context =
129 v8::Context::New(CcTest::isolate(), NULL, global);
130 v8::Context::Scope context_scope(context);
131 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate()));
132 // Run a loop that will be infinite if thread termination does not work.
133 v8::Handle<v8::String> source = v8::String::NewFromUtf8(
134 CcTest::isolate(), "try { loop(); fail(); } catch(e) { fail(); }");
135 v8::Script::Compile(source)->Run();
136 // Test that we can run the code again after thread termination.
137 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate()));
138 v8::Script::Compile(source)->Run();
142 // Test that a single thread of JavaScript execution can terminate
143 // itself in a loop that performs no calls.
144 TEST(TerminateOnlyV8ThreadFromThreadItselfNoLoop) {
145 v8::HandleScope scope(CcTest::isolate());
146 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(
147 CcTest::isolate(), TerminateCurrentThread, DoLoopNoCall);
148 v8::Handle<v8::Context> context =
149 v8::Context::New(CcTest::isolate(), NULL, global);
150 v8::Context::Scope context_scope(context);
151 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate()));
152 // Run a loop that will be infinite if thread termination does not work.
153 v8::Handle<v8::String> source = v8::String::NewFromUtf8(
154 CcTest::isolate(), "try { loop(); fail(); } catch(e) { fail(); }");
155 v8::Script::Compile(source)->Run();
156 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate()));
157 // Test that we can run the code again after thread termination.
158 v8::Script::Compile(source)->Run();
162 class TerminatorThread : public v8::base::Thread {
164 explicit TerminatorThread(i::Isolate* isolate)
165 : Thread(Options("TerminatorThread")),
166 isolate_(reinterpret_cast<v8::Isolate*>(isolate)) {}
169 CHECK(!v8::V8::IsExecutionTerminating(isolate_));
170 v8::V8::TerminateExecution(isolate_);
174 v8::Isolate* isolate_;
178 // Test that a single thread of JavaScript execution can be terminated
179 // from the side by another thread.
180 TEST(TerminateOnlyV8ThreadFromOtherThread) {
181 semaphore = new v8::base::Semaphore(0);
182 TerminatorThread thread(CcTest::i_isolate());
185 v8::HandleScope scope(CcTest::isolate());
186 v8::Handle<v8::ObjectTemplate> global =
187 CreateGlobalTemplate(CcTest::isolate(), Signal, DoLoop);
188 v8::Handle<v8::Context> context =
189 v8::Context::New(CcTest::isolate(), NULL, global);
190 v8::Context::Scope context_scope(context);
191 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate()));
192 // Run a loop that will be infinite if thread termination does not work.
193 v8::Handle<v8::String> source = v8::String::NewFromUtf8(
194 CcTest::isolate(), "try { loop(); fail(); } catch(e) { fail(); }");
195 i::FLAG_turbo_osr = false; // TODO(titzer): interrupts in TF loops.
196 v8::Script::Compile(source)->Run();
207 void TerminateOrReturnObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
208 if (++call_count == 10) {
209 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
210 v8::V8::TerminateExecution(args.GetIsolate());
213 v8::Local<v8::Object> result = v8::Object::New(args.GetIsolate());
214 result->Set(v8::String::NewFromUtf8(args.GetIsolate(), "x"),
215 v8::Integer::New(args.GetIsolate(), 42));
216 args.GetReturnValue().Set(result);
220 void LoopGetProperty(const v8::FunctionCallbackInfo<v8::Value>& args) {
221 v8::TryCatch try_catch;
222 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
224 v8::String::NewFromUtf8(args.GetIsolate(),
228 " terminate_or_return_object().x;"
232 " (function() {})();" // trigger stack check.
237 CHECK(try_catch.HasCaught());
238 CHECK(try_catch.Exception()->IsNull());
239 CHECK(try_catch.Message().IsEmpty());
240 CHECK(!try_catch.CanContinue());
241 CHECK(v8::V8::IsExecutionTerminating(args.GetIsolate()));
245 // Test that we correctly handle termination exceptions if they are
246 // triggered by the creation of error objects in connection with ICs.
247 TEST(TerminateLoadICException) {
248 v8::Isolate* isolate = CcTest::isolate();
249 v8::HandleScope scope(isolate);
250 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
252 v8::String::NewFromUtf8(isolate, "terminate_or_return_object"),
253 v8::FunctionTemplate::New(isolate, TerminateOrReturnObject));
254 global->Set(v8::String::NewFromUtf8(isolate, "fail"),
255 v8::FunctionTemplate::New(isolate, Fail));
256 global->Set(v8::String::NewFromUtf8(isolate, "loop"),
257 v8::FunctionTemplate::New(isolate, LoopGetProperty));
259 v8::Handle<v8::Context> context =
260 v8::Context::New(isolate, NULL, global);
261 v8::Context::Scope context_scope(context);
262 CHECK(!v8::V8::IsExecutionTerminating(isolate));
263 // Run a loop that will be infinite if thread termination does not work.
264 v8::Handle<v8::String> source = v8::String::NewFromUtf8(
265 isolate, "try { loop(); fail(); } catch(e) { fail(); }");
267 v8::Script::Compile(source)->Run();
268 // Test that we can run the code again after thread termination.
269 CHECK(!v8::V8::IsExecutionTerminating(isolate));
271 v8::Script::Compile(source)->Run();
275 v8::Persistent<v8::String> reenter_script_1;
276 v8::Persistent<v8::String> reenter_script_2;
278 void ReenterAfterTermination(const v8::FunctionCallbackInfo<v8::Value>& args) {
279 v8::TryCatch try_catch;
280 v8::Isolate* isolate = args.GetIsolate();
281 CHECK(!v8::V8::IsExecutionTerminating(isolate));
282 v8::Local<v8::String> script =
283 v8::Local<v8::String>::New(isolate, reenter_script_1);
284 v8::Script::Compile(script)->Run();
285 CHECK(try_catch.HasCaught());
286 CHECK(try_catch.Exception()->IsNull());
287 CHECK(try_catch.Message().IsEmpty());
288 CHECK(!try_catch.CanContinue());
289 CHECK(v8::V8::IsExecutionTerminating(isolate));
290 script = v8::Local<v8::String>::New(isolate, reenter_script_2);
291 v8::Script::Compile(script)->Run();
295 // Test that reentry into V8 while the termination exception is still pending
296 // (has not yet unwound the 0-level JS frame) does not crash.
297 TEST(TerminateAndReenterFromThreadItself) {
298 v8::Isolate* isolate = CcTest::isolate();
299 v8::HandleScope scope(isolate);
300 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(
301 isolate, TerminateCurrentThread, ReenterAfterTermination);
302 v8::Handle<v8::Context> context =
303 v8::Context::New(isolate, NULL, global);
304 v8::Context::Scope context_scope(context);
305 CHECK(!v8::V8::IsExecutionTerminating());
306 // Create script strings upfront as it won't work when terminating.
307 reenter_script_1.Reset(isolate, v8_str(
312 " if (term) terminate();"
321 reenter_script_2.Reset(isolate, v8_str("function f() { fail(); } f()"));
322 CompileRun("try { loop(); fail(); } catch(e) { fail(); }");
323 CHECK(!v8::V8::IsExecutionTerminating(isolate));
324 // Check we can run JS again after termination.
325 CHECK(CompileRun("function f() { return true; } f()")->IsTrue());
326 reenter_script_1.Reset();
327 reenter_script_2.Reset();
331 void DoLoopCancelTerminate(const v8::FunctionCallbackInfo<v8::Value>& args) {
332 v8::TryCatch try_catch;
333 CHECK(!v8::V8::IsExecutionTerminating());
334 v8::Script::Compile(v8::String::NewFromUtf8(args.GetIsolate(),
337 " if (term) terminate();"
341 CHECK(try_catch.HasCaught());
342 CHECK(try_catch.Exception()->IsNull());
343 CHECK(try_catch.Message().IsEmpty());
344 CHECK(!try_catch.CanContinue());
345 CHECK(v8::V8::IsExecutionTerminating());
346 CHECK(try_catch.HasTerminated());
347 v8::V8::CancelTerminateExecution(CcTest::isolate());
348 CHECK(!v8::V8::IsExecutionTerminating());
352 // Test that a single thread of JavaScript execution can terminate
353 // itself and then resume execution.
354 TEST(TerminateCancelTerminateFromThreadItself) {
355 v8::Isolate* isolate = CcTest::isolate();
356 v8::HandleScope scope(isolate);
357 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(
358 isolate, TerminateCurrentThread, DoLoopCancelTerminate);
359 v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global);
360 v8::Context::Scope context_scope(context);
361 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate()));
362 v8::Handle<v8::String> source = v8::String::NewFromUtf8(
363 isolate, "try { doloop(); } catch(e) { fail(); } 'completed';");
364 // Check that execution completed with correct return value.
365 CHECK(v8::Script::Compile(source)->Run()->Equals(v8_str("completed")));
369 void MicrotaskShouldNotRun(const v8::FunctionCallbackInfo<v8::Value>& info) {
374 void MicrotaskLoopForever(const v8::FunctionCallbackInfo<v8::Value>& info) {
375 v8::Isolate* isolate = info.GetIsolate();
376 v8::HandleScope scope(isolate);
377 // Enqueue another should-not-run task to ensure we clean out the queue
378 // when we terminate.
379 isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskShouldNotRun));
380 i::FLAG_turbo_osr = false; // TODO(titzer): interrupts in TF loops.
381 CompileRun("terminate(); while (true) { }");
382 CHECK(v8::V8::IsExecutionTerminating());
386 TEST(TerminateFromOtherThreadWhileMicrotaskRunning) {
387 semaphore = new v8::base::Semaphore(0);
388 TerminatorThread thread(CcTest::i_isolate());
391 v8::Isolate* isolate = CcTest::isolate();
392 isolate->SetAutorunMicrotasks(false);
393 v8::HandleScope scope(isolate);
394 v8::Handle<v8::ObjectTemplate> global =
395 CreateGlobalTemplate(CcTest::isolate(), Signal, DoLoop);
396 v8::Handle<v8::Context> context =
397 v8::Context::New(CcTest::isolate(), NULL, global);
398 v8::Context::Scope context_scope(context);
399 isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskLoopForever));
400 // The second task should never be run because we bail out if we're
402 isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskShouldNotRun));
403 isolate->RunMicrotasks();
405 v8::V8::CancelTerminateExecution(isolate);
406 isolate->RunMicrotasks(); // should not run MicrotaskShouldNotRun
414 static int callback_counter = 0;
417 static void CounterCallback(v8::Isolate* isolate, void* data) {
422 TEST(PostponeTerminateException) {
423 v8::Isolate* isolate = CcTest::isolate();
424 v8::HandleScope scope(isolate);
425 v8::Handle<v8::ObjectTemplate> global =
426 CreateGlobalTemplate(CcTest::isolate(), TerminateCurrentThread, DoLoop);
427 v8::Handle<v8::Context> context =
428 v8::Context::New(CcTest::isolate(), NULL, global);
429 v8::Context::Scope context_scope(context);
431 v8::TryCatch try_catch;
432 static const char* terminate_and_loop =
433 "terminate(); for (var i = 0; i < 10000; i++);";
435 { // Postpone terminate execution interrupts.
436 i::PostponeInterruptsScope p1(CcTest::i_isolate(),
437 i::StackGuard::TERMINATE_EXECUTION) ;
439 // API interrupts should still be triggered.
440 CcTest::isolate()->RequestInterrupt(&CounterCallback, NULL);
441 CHECK_EQ(0, callback_counter);
442 CompileRun(terminate_and_loop);
443 CHECK(!try_catch.HasTerminated());
444 CHECK_EQ(1, callback_counter);
446 { // Postpone API interrupts as well.
447 i::PostponeInterruptsScope p2(CcTest::i_isolate(),
448 i::StackGuard::API_INTERRUPT);
450 // None of the two interrupts should trigger.
451 CcTest::isolate()->RequestInterrupt(&CounterCallback, NULL);
452 CompileRun(terminate_and_loop);
453 CHECK(!try_catch.HasTerminated());
454 CHECK_EQ(1, callback_counter);
457 // Now the previously requested API interrupt should trigger.
458 CompileRun(terminate_and_loop);
459 CHECK(!try_catch.HasTerminated());
460 CHECK_EQ(2, callback_counter);
463 // Now the previously requested terminate execution interrupt should trigger.
464 CompileRun("for (var i = 0; i < 10000; i++);");
465 CHECK(try_catch.HasTerminated());
466 CHECK_EQ(2, callback_counter);
470 TEST(ErrorObjectAfterTermination) {
471 v8::Isolate* isolate = CcTest::isolate();
472 v8::HandleScope scope(isolate);
473 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate());
474 v8::Context::Scope context_scope(context);
475 v8::V8::TerminateExecution(isolate);
476 v8::Local<v8::Value> error = v8::Exception::Error(v8_str("error"));
477 // TODO(yangguo): crbug/403509. Check for empty handle instead.
478 CHECK(error->IsUndefined());
482 void InnerTryCallTerminate(const v8::FunctionCallbackInfo<v8::Value>& args) {
483 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
484 v8::Handle<v8::Object> global = CcTest::global();
485 v8::Handle<v8::Function> loop =
486 v8::Handle<v8::Function>::Cast(global->Get(v8_str("loop")));
487 i::MaybeHandle<i::Object> result =
488 i::Execution::TryCall(v8::Utils::OpenHandle((*loop)),
489 v8::Utils::OpenHandle((*global)), 0, NULL, NULL);
490 CHECK(result.is_null());
491 // TryCall ignores terminate execution, but rerequests the interrupt.
492 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
493 CHECK(CompileRun("1 + 1;").IsEmpty());
497 TEST(TerminationInInnerTryCall) {
498 v8::Isolate* isolate = CcTest::isolate();
499 v8::HandleScope scope(isolate);
500 v8::Handle<v8::ObjectTemplate> global_template = CreateGlobalTemplate(
501 CcTest::isolate(), TerminateCurrentThread, DoLoopNoCall);
502 global_template->Set(
503 v8_str("inner_try_call_terminate"),
504 v8::FunctionTemplate::New(isolate, InnerTryCallTerminate));
505 v8::Handle<v8::Context> context =
506 v8::Context::New(CcTest::isolate(), NULL, global_template);
507 v8::Context::Scope context_scope(context);
509 v8::TryCatch try_catch;
510 CompileRun("inner_try_call_terminate()");
511 CHECK(try_catch.HasTerminated());
513 CHECK_EQ(4, CompileRun("2 + 2")->ToInt32()->Int32Value());
514 CHECK(!v8::V8::IsExecutionTerminating());
518 TEST(TerminateAndTryCall) {
519 i::FLAG_allow_natives_syntax = true;
520 v8::Isolate* isolate = CcTest::isolate();
521 v8::HandleScope scope(isolate);
522 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(
523 isolate, TerminateCurrentThread, DoLoopCancelTerminate);
524 v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global);
525 v8::Context::Scope context_scope(context);
526 CHECK(!v8::V8::IsExecutionTerminating(isolate));
527 v8::TryCatch try_catch;
528 CHECK(!v8::V8::IsExecutionTerminating(isolate));
529 // Terminate execution has been triggered inside TryCall, but re-requested
531 CHECK(CompileRun("terminate(); reference_error();").IsEmpty());
532 CHECK(try_catch.HasCaught());
533 CHECK(!v8::V8::IsExecutionTerminating(isolate));
534 CHECK(CcTest::global()->Get(v8_str("terminate"))->IsFunction());
535 // The first stack check after terminate has been re-requested fails.
536 CHECK(CompileRun("1 + 1").IsEmpty());
537 CHECK(!v8::V8::IsExecutionTerminating(isolate));
539 CHECK_EQ(4, CompileRun("2 + 2")->ToInt32()->Int32Value());
540 CHECK(!v8::V8::IsExecutionTerminating(isolate));