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 v8::Script::Compile(source)->Run();
206 void TerminateOrReturnObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
207 if (++call_count == 10) {
208 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
209 v8::V8::TerminateExecution(args.GetIsolate());
212 v8::Local<v8::Object> result = v8::Object::New(args.GetIsolate());
213 result->Set(v8::String::NewFromUtf8(args.GetIsolate(), "x"),
214 v8::Integer::New(args.GetIsolate(), 42));
215 args.GetReturnValue().Set(result);
219 void LoopGetProperty(const v8::FunctionCallbackInfo<v8::Value>& args) {
220 v8::TryCatch try_catch;
221 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
223 v8::String::NewFromUtf8(args.GetIsolate(),
227 " terminate_or_return_object().x;"
235 CHECK(try_catch.HasCaught());
236 CHECK(try_catch.Exception()->IsNull());
237 CHECK(try_catch.Message().IsEmpty());
238 CHECK(!try_catch.CanContinue());
239 CHECK(v8::V8::IsExecutionTerminating(args.GetIsolate()));
243 // Test that we correctly handle termination exceptions if they are
244 // triggered by the creation of error objects in connection with ICs.
245 TEST(TerminateLoadICException) {
246 v8::Isolate* isolate = CcTest::isolate();
247 v8::HandleScope scope(isolate);
248 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
250 v8::String::NewFromUtf8(isolate, "terminate_or_return_object"),
251 v8::FunctionTemplate::New(isolate, TerminateOrReturnObject));
252 global->Set(v8::String::NewFromUtf8(isolate, "fail"),
253 v8::FunctionTemplate::New(isolate, Fail));
254 global->Set(v8::String::NewFromUtf8(isolate, "loop"),
255 v8::FunctionTemplate::New(isolate, LoopGetProperty));
257 v8::Handle<v8::Context> context =
258 v8::Context::New(isolate, NULL, global);
259 v8::Context::Scope context_scope(context);
260 CHECK(!v8::V8::IsExecutionTerminating(isolate));
261 // Run a loop that will be infinite if thread termination does not work.
262 v8::Handle<v8::String> source = v8::String::NewFromUtf8(
263 isolate, "try { loop(); fail(); } catch(e) { fail(); }");
265 v8::Script::Compile(source)->Run();
266 // Test that we can run the code again after thread termination.
267 CHECK(!v8::V8::IsExecutionTerminating(isolate));
269 v8::Script::Compile(source)->Run();
273 v8::Persistent<v8::String> reenter_script_1;
274 v8::Persistent<v8::String> reenter_script_2;
276 void ReenterAfterTermination(const v8::FunctionCallbackInfo<v8::Value>& args) {
277 v8::TryCatch try_catch;
278 v8::Isolate* isolate = args.GetIsolate();
279 CHECK(!v8::V8::IsExecutionTerminating(isolate));
280 v8::Local<v8::String> script =
281 v8::Local<v8::String>::New(isolate, reenter_script_1);
282 v8::Script::Compile(script)->Run();
283 CHECK(try_catch.HasCaught());
284 CHECK(try_catch.Exception()->IsNull());
285 CHECK(try_catch.Message().IsEmpty());
286 CHECK(!try_catch.CanContinue());
287 CHECK(v8::V8::IsExecutionTerminating(isolate));
288 script = v8::Local<v8::String>::New(isolate, reenter_script_2);
289 v8::Script::Compile(script)->Run();
293 // Test that reentry into V8 while the termination exception is still pending
294 // (has not yet unwound the 0-level JS frame) does not crash.
295 TEST(TerminateAndReenterFromThreadItself) {
296 v8::Isolate* isolate = CcTest::isolate();
297 v8::HandleScope scope(isolate);
298 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(
299 isolate, TerminateCurrentThread, ReenterAfterTermination);
300 v8::Handle<v8::Context> context =
301 v8::Context::New(isolate, NULL, global);
302 v8::Context::Scope context_scope(context);
303 CHECK(!v8::V8::IsExecutionTerminating());
304 // Create script strings upfront as it won't work when terminating.
305 reenter_script_1.Reset(isolate, v8_str(
310 " if (term) terminate();"
319 reenter_script_2.Reset(isolate, v8_str("function f() { fail(); } f()"));
320 CompileRun("try { loop(); fail(); } catch(e) { fail(); }");
321 CHECK(!v8::V8::IsExecutionTerminating(isolate));
322 // Check we can run JS again after termination.
323 CHECK(CompileRun("function f() { return true; } f()")->IsTrue());
324 reenter_script_1.Reset();
325 reenter_script_2.Reset();
329 void DoLoopCancelTerminate(const v8::FunctionCallbackInfo<v8::Value>& args) {
330 v8::TryCatch try_catch;
331 CHECK(!v8::V8::IsExecutionTerminating());
332 v8::Script::Compile(v8::String::NewFromUtf8(args.GetIsolate(),
335 " if (term) terminate();"
339 CHECK(try_catch.HasCaught());
340 CHECK(try_catch.Exception()->IsNull());
341 CHECK(try_catch.Message().IsEmpty());
342 CHECK(!try_catch.CanContinue());
343 CHECK(v8::V8::IsExecutionTerminating());
344 CHECK(try_catch.HasTerminated());
345 v8::V8::CancelTerminateExecution(CcTest::isolate());
346 CHECK(!v8::V8::IsExecutionTerminating());
350 // Test that a single thread of JavaScript execution can terminate
351 // itself and then resume execution.
352 TEST(TerminateCancelTerminateFromThreadItself) {
353 v8::Isolate* isolate = CcTest::isolate();
354 v8::HandleScope scope(isolate);
355 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(
356 isolate, TerminateCurrentThread, DoLoopCancelTerminate);
357 v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global);
358 v8::Context::Scope context_scope(context);
359 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate()));
360 v8::Handle<v8::String> source = v8::String::NewFromUtf8(
361 isolate, "try { doloop(); } catch(e) { fail(); } 'completed';");
362 // Check that execution completed with correct return value.
363 CHECK(v8::Script::Compile(source)->Run()->Equals(v8_str("completed")));
367 void MicrotaskShouldNotRun(const v8::FunctionCallbackInfo<v8::Value>& info) {
372 void MicrotaskLoopForever(const v8::FunctionCallbackInfo<v8::Value>& info) {
373 v8::Isolate* isolate = info.GetIsolate();
374 v8::HandleScope scope(isolate);
375 // Enqueue another should-not-run task to ensure we clean out the queue
376 // when we terminate.
377 isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskShouldNotRun));
378 CompileRun("terminate(); while (true) { }");
379 CHECK(v8::V8::IsExecutionTerminating());
383 TEST(TerminateFromOtherThreadWhileMicrotaskRunning) {
384 semaphore = new v8::base::Semaphore(0);
385 TerminatorThread thread(CcTest::i_isolate());
388 v8::Isolate* isolate = CcTest::isolate();
389 isolate->SetAutorunMicrotasks(false);
390 v8::HandleScope scope(isolate);
391 v8::Handle<v8::ObjectTemplate> global =
392 CreateGlobalTemplate(CcTest::isolate(), Signal, DoLoop);
393 v8::Handle<v8::Context> context =
394 v8::Context::New(CcTest::isolate(), NULL, global);
395 v8::Context::Scope context_scope(context);
396 isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskLoopForever));
397 // The second task should never be run because we bail out if we're
399 isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskShouldNotRun));
400 isolate->RunMicrotasks();
402 v8::V8::CancelTerminateExecution(isolate);
403 isolate->RunMicrotasks(); // should not run MicrotaskShouldNotRun
411 static int callback_counter = 0;
414 static void CounterCallback(v8::Isolate* isolate, void* data) {
419 TEST(PostponeTerminateException) {
420 v8::Isolate* isolate = CcTest::isolate();
421 v8::HandleScope scope(isolate);
422 v8::Handle<v8::ObjectTemplate> global =
423 CreateGlobalTemplate(CcTest::isolate(), TerminateCurrentThread, DoLoop);
424 v8::Handle<v8::Context> context =
425 v8::Context::New(CcTest::isolate(), NULL, global);
426 v8::Context::Scope context_scope(context);
428 v8::TryCatch try_catch;
429 static const char* terminate_and_loop =
430 "terminate(); for (var i = 0; i < 10000; i++);";
432 { // Postpone terminate execution interrupts.
433 i::PostponeInterruptsScope p1(CcTest::i_isolate(),
434 i::StackGuard::TERMINATE_EXECUTION) ;
436 // API interrupts should still be triggered.
437 CcTest::isolate()->RequestInterrupt(&CounterCallback, NULL);
438 CHECK_EQ(0, callback_counter);
439 CompileRun(terminate_and_loop);
440 CHECK(!try_catch.HasTerminated());
441 CHECK_EQ(1, callback_counter);
443 { // Postpone API interrupts as well.
444 i::PostponeInterruptsScope p2(CcTest::i_isolate(),
445 i::StackGuard::API_INTERRUPT);
447 // None of the two interrupts should trigger.
448 CcTest::isolate()->RequestInterrupt(&CounterCallback, NULL);
449 CompileRun(terminate_and_loop);
450 CHECK(!try_catch.HasTerminated());
451 CHECK_EQ(1, callback_counter);
454 // Now the previously requested API interrupt should trigger.
455 CompileRun(terminate_and_loop);
456 CHECK(!try_catch.HasTerminated());
457 CHECK_EQ(2, callback_counter);
460 // Now the previously requested terminate execution interrupt should trigger.
461 CompileRun("for (var i = 0; i < 10000; i++);");
462 CHECK(try_catch.HasTerminated());
463 CHECK_EQ(2, callback_counter);
467 TEST(ErrorObjectAfterTermination) {
468 v8::Isolate* isolate = CcTest::isolate();
469 v8::HandleScope scope(isolate);
470 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate());
471 v8::Context::Scope context_scope(context);
472 v8::V8::TerminateExecution(isolate);
473 v8::Local<v8::Value> error = v8::Exception::Error(v8_str("error"));
474 // TODO(yangguo): crbug/403509. Check for empty handle instead.
475 CHECK(error->IsUndefined());