2009-08-19 15:14:11 +00:00
|
|
|
// Copyright 2009 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
|
|
|
#include "test/cctest/cctest.h"
|
2009-08-19 15:14:11 +00:00
|
|
|
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/platform/platform.h"
|
2014-06-20 08:40:11 +00:00
|
|
|
|
2009-08-19 15:14:11 +00:00
|
|
|
|
2014-06-30 13:25:46 +00:00
|
|
|
v8::base::Semaphore* semaphore = NULL;
|
2009-08-19 15:14:11 +00:00
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void Signal(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2009-08-19 15:14:11 +00:00
|
|
|
semaphore->Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void TerminateCurrentThread(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
|
2013-09-19 07:33:45 +00:00
|
|
|
v8::V8::TerminateExecution(args.GetIsolate());
|
2009-08-19 15:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void Fail(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2009-08-19 15:14:11 +00:00
|
|
|
CHECK(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void Loop(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Handle<v8::String> source = v8::String::NewFromUtf8(
|
|
|
|
args.GetIsolate(), "try { doloop(); fail(); } catch(e) { fail(); }");
|
2010-03-04 12:13:04 +00:00
|
|
|
v8::Handle<v8::Value> result = v8::Script::Compile(source)->Run();
|
|
|
|
CHECK(result.IsEmpty());
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(v8::V8::IsExecutionTerminating(args.GetIsolate()));
|
2009-08-19 15:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void DoLoop(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::TryCatch try_catch;
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Script::Compile(v8::String::NewFromUtf8(args.GetIsolate(),
|
|
|
|
"function f() {"
|
|
|
|
" var term = true;"
|
|
|
|
" try {"
|
|
|
|
" while(true) {"
|
|
|
|
" if (term) terminate();"
|
|
|
|
" term = false;"
|
|
|
|
" }"
|
|
|
|
" fail();"
|
|
|
|
" } catch(e) {"
|
|
|
|
" fail();"
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
"f()"))->Run();
|
2009-08-19 15:14:11 +00:00
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(try_catch.Exception()->IsNull());
|
|
|
|
CHECK(try_catch.Message().IsEmpty());
|
|
|
|
CHECK(!try_catch.CanContinue());
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(v8::V8::IsExecutionTerminating(args.GetIsolate()));
|
2009-08-19 15:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void DoLoopNoCall(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2009-11-24 12:23:29 +00:00
|
|
|
v8::TryCatch try_catch;
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Script::Compile(v8::String::NewFromUtf8(args.GetIsolate(),
|
|
|
|
"var term = true;"
|
|
|
|
"while(true) {"
|
|
|
|
" if (term) terminate();"
|
|
|
|
" term = false;"
|
|
|
|
"}"))->Run();
|
2009-11-24 12:23:29 +00:00
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(try_catch.Exception()->IsNull());
|
|
|
|
CHECK(try_catch.Message().IsEmpty());
|
|
|
|
CHECK(!try_catch.CanContinue());
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(v8::V8::IsExecutionTerminating(args.GetIsolate()));
|
2009-11-24 12:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> CreateGlobalTemplate(
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Isolate* isolate,
|
2013-06-20 12:28:27 +00:00
|
|
|
v8::FunctionCallback terminate,
|
|
|
|
v8::FunctionCallback doloop) {
|
2014-01-08 06:53:31 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
|
2013-11-22 12:43:17 +00:00
|
|
|
global->Set(v8::String::NewFromUtf8(isolate, "terminate"),
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::FunctionTemplate::New(isolate, terminate));
|
2013-11-22 12:43:17 +00:00
|
|
|
global->Set(v8::String::NewFromUtf8(isolate, "fail"),
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::FunctionTemplate::New(isolate, Fail));
|
2013-11-22 12:43:17 +00:00
|
|
|
global->Set(v8::String::NewFromUtf8(isolate, "loop"),
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::FunctionTemplate::New(isolate, Loop));
|
2013-11-22 12:43:17 +00:00
|
|
|
global->Set(v8::String::NewFromUtf8(isolate, "doloop"),
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::FunctionTemplate::New(isolate, doloop));
|
2009-08-19 15:14:11 +00:00
|
|
|
return global;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test that a single thread of JavaScript execution can terminate
|
|
|
|
// itself.
|
|
|
|
TEST(TerminateOnlyV8ThreadFromThreadItself) {
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> global =
|
2013-11-22 12:43:17 +00:00
|
|
|
CreateGlobalTemplate(CcTest::isolate(), TerminateCurrentThread, DoLoop);
|
2013-05-02 20:18:42 +00:00
|
|
|
v8::Handle<v8::Context> context =
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::Context::New(CcTest::isolate(), NULL, global);
|
2009-11-24 12:23:29 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate()));
|
2009-11-24 12:23:29 +00:00
|
|
|
// Run a loop that will be infinite if thread termination does not work.
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Handle<v8::String> source = v8::String::NewFromUtf8(
|
|
|
|
CcTest::isolate(), "try { loop(); fail(); } catch(e) { fail(); }");
|
2009-11-24 12:23:29 +00:00
|
|
|
v8::Script::Compile(source)->Run();
|
|
|
|
// Test that we can run the code again after thread termination.
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate()));
|
2009-11-24 12:23:29 +00:00
|
|
|
v8::Script::Compile(source)->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test that a single thread of JavaScript execution can terminate
|
|
|
|
// itself in a loop that performs no calls.
|
|
|
|
TEST(TerminateOnlyV8ThreadFromThreadItselfNoLoop) {
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(
|
|
|
|
CcTest::isolate(), TerminateCurrentThread, DoLoopNoCall);
|
2013-05-02 20:18:42 +00:00
|
|
|
v8::Handle<v8::Context> context =
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::Context::New(CcTest::isolate(), NULL, global);
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate()));
|
2009-08-19 15:14:11 +00:00
|
|
|
// Run a loop that will be infinite if thread termination does not work.
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Handle<v8::String> source = v8::String::NewFromUtf8(
|
|
|
|
CcTest::isolate(), "try { loop(); fail(); } catch(e) { fail(); }");
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::Script::Compile(source)->Run();
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate()));
|
2009-08-19 15:14:11 +00:00
|
|
|
// Test that we can run the code again after thread termination.
|
|
|
|
v8::Script::Compile(source)->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-30 13:25:46 +00:00
|
|
|
class TerminatorThread : public v8::base::Thread {
|
2011-03-18 20:35:07 +00:00
|
|
|
public:
|
2011-06-10 09:36:35 +00:00
|
|
|
explicit TerminatorThread(i::Isolate* isolate)
|
2011-06-10 09:54:04 +00:00
|
|
|
: Thread("TerminatorThread"),
|
|
|
|
isolate_(reinterpret_cast<v8::Isolate*>(isolate)) { }
|
2009-08-19 15:14:11 +00:00
|
|
|
void Run() {
|
|
|
|
semaphore->Wait();
|
2011-06-10 09:54:04 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(isolate_));
|
|
|
|
v8::V8::TerminateExecution(isolate_);
|
2009-08-19 15:14:11 +00:00
|
|
|
}
|
2011-06-10 09:54:04 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
v8::Isolate* isolate_;
|
2009-08-19 15:14:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Test that a single thread of JavaScript execution can be terminated
|
|
|
|
// from the side by another thread.
|
|
|
|
TEST(TerminateOnlyV8ThreadFromOtherThread) {
|
2014-06-30 13:25:46 +00:00
|
|
|
semaphore = new v8::base::Semaphore(0);
|
2013-09-19 09:17:13 +00:00
|
|
|
TerminatorThread thread(CcTest::i_isolate());
|
2009-08-19 15:14:11 +00:00
|
|
|
thread.Start();
|
|
|
|
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> global =
|
|
|
|
CreateGlobalTemplate(CcTest::isolate(), Signal, DoLoop);
|
2013-05-02 20:18:42 +00:00
|
|
|
v8::Handle<v8::Context> context =
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::Context::New(CcTest::isolate(), NULL, global);
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate()));
|
2009-08-19 15:14:11 +00:00
|
|
|
// Run a loop that will be infinite if thread termination does not work.
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Handle<v8::String> source = v8::String::NewFromUtf8(
|
|
|
|
CcTest::isolate(), "try { loop(); fail(); } catch(e) { fail(); }");
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::Script::Compile(source)->Run();
|
|
|
|
|
|
|
|
thread.Join();
|
|
|
|
delete semaphore;
|
|
|
|
semaphore = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-01 13:55:45 +00:00
|
|
|
int call_count = 0;
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void TerminateOrReturnObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2009-09-01 13:55:45 +00:00
|
|
|
if (++call_count == 10) {
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
|
|
|
|
v8::V8::TerminateExecution(args.GetIsolate());
|
2013-06-20 12:28:27 +00:00
|
|
|
return;
|
2009-09-01 13:55:45 +00:00
|
|
|
}
|
2014-01-03 14:31:17 +00:00
|
|
|
v8::Local<v8::Object> result = v8::Object::New(args.GetIsolate());
|
2013-11-22 12:43:17 +00:00
|
|
|
result->Set(v8::String::NewFromUtf8(args.GetIsolate(), "x"),
|
2014-01-03 14:31:17 +00:00
|
|
|
v8::Integer::New(args.GetIsolate(), 42));
|
2013-06-20 12:28:27 +00:00
|
|
|
args.GetReturnValue().Set(result);
|
2009-09-01 13:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void LoopGetProperty(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2009-09-01 13:55:45 +00:00
|
|
|
v8::TryCatch try_catch;
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Script::Compile(
|
|
|
|
v8::String::NewFromUtf8(args.GetIsolate(),
|
|
|
|
"function f() {"
|
|
|
|
" try {"
|
|
|
|
" while(true) {"
|
|
|
|
" terminate_or_return_object().x;"
|
|
|
|
" }"
|
|
|
|
" fail();"
|
|
|
|
" } catch(e) {"
|
|
|
|
" fail();"
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
"f()"))->Run();
|
2009-09-01 13:55:45 +00:00
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(try_catch.Exception()->IsNull());
|
|
|
|
CHECK(try_catch.Message().IsEmpty());
|
|
|
|
CHECK(!try_catch.CanContinue());
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(v8::V8::IsExecutionTerminating(args.GetIsolate()));
|
2009-09-01 13:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test that we correctly handle termination exceptions if they are
|
|
|
|
// triggered by the creation of error objects in connection with ICs.
|
2013-09-20 10:52:20 +00:00
|
|
|
TEST(TerminateLoadICException) {
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2014-01-08 06:53:31 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
|
2013-11-22 12:43:17 +00:00
|
|
|
global->Set(
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::String::NewFromUtf8(isolate, "terminate_or_return_object"),
|
|
|
|
v8::FunctionTemplate::New(isolate, TerminateOrReturnObject));
|
|
|
|
global->Set(v8::String::NewFromUtf8(isolate, "fail"),
|
|
|
|
v8::FunctionTemplate::New(isolate, Fail));
|
|
|
|
global->Set(v8::String::NewFromUtf8(isolate, "loop"),
|
|
|
|
v8::FunctionTemplate::New(isolate, LoopGetProperty));
|
2009-09-01 13:55:45 +00:00
|
|
|
|
2013-05-02 20:18:42 +00:00
|
|
|
v8::Handle<v8::Context> context =
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::Context::New(isolate, NULL, global);
|
2009-09-01 13:55:45 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2013-12-18 10:31:42 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(isolate));
|
2009-09-01 13:55:45 +00:00
|
|
|
// Run a loop that will be infinite if thread termination does not work.
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Handle<v8::String> source = v8::String::NewFromUtf8(
|
2013-12-18 10:31:42 +00:00
|
|
|
isolate, "try { loop(); fail(); } catch(e) { fail(); }");
|
2009-09-01 13:55:45 +00:00
|
|
|
call_count = 0;
|
|
|
|
v8::Script::Compile(source)->Run();
|
|
|
|
// Test that we can run the code again after thread termination.
|
2013-12-18 10:31:42 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(isolate));
|
2009-09-01 13:55:45 +00:00
|
|
|
call_count = 0;
|
|
|
|
v8::Script::Compile(source)->Run();
|
|
|
|
}
|
2010-05-21 20:52:19 +00:00
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void ReenterAfterTermination(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2010-05-21 20:52:19 +00:00
|
|
|
v8::TryCatch try_catch;
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate()));
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Script::Compile(v8::String::NewFromUtf8(args.GetIsolate(),
|
|
|
|
"function f() {"
|
|
|
|
" var term = true;"
|
|
|
|
" try {"
|
|
|
|
" while(true) {"
|
|
|
|
" if (term) terminate();"
|
|
|
|
" term = false;"
|
|
|
|
" }"
|
|
|
|
" fail();"
|
|
|
|
" } catch(e) {"
|
|
|
|
" fail();"
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
"f()"))->Run();
|
2010-05-21 20:52:19 +00:00
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(try_catch.Exception()->IsNull());
|
|
|
|
CHECK(try_catch.Message().IsEmpty());
|
|
|
|
CHECK(!try_catch.CanContinue());
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(v8::V8::IsExecutionTerminating(args.GetIsolate()));
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Script::Compile(v8::String::NewFromUtf8(args.GetIsolate(),
|
|
|
|
"function f() { fail(); } f()"))
|
|
|
|
->Run();
|
2010-05-21 20:52:19 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2010-05-21 20:52:19 +00:00
|
|
|
// Test that reentry into V8 while the termination exception is still pending
|
|
|
|
// (has not yet unwound the 0-level JS frame) does not crash.
|
|
|
|
TEST(TerminateAndReenterFromThreadItself) {
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(
|
2013-12-18 10:31:42 +00:00
|
|
|
isolate, TerminateCurrentThread, ReenterAfterTermination);
|
2013-05-02 20:18:42 +00:00
|
|
|
v8::Handle<v8::Context> context =
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::Context::New(isolate, NULL, global);
|
2010-05-21 20:52:19 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Handle<v8::String> source = v8::String::NewFromUtf8(
|
2013-12-18 10:31:42 +00:00
|
|
|
isolate, "try { loop(); fail(); } catch(e) { fail(); }");
|
2010-05-21 20:52:19 +00:00
|
|
|
v8::Script::Compile(source)->Run();
|
2013-12-18 10:31:42 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(isolate));
|
2010-05-21 20:52:19 +00:00
|
|
|
// Check we can run JS again after termination.
|
2013-11-22 12:43:17 +00:00
|
|
|
CHECK(v8::Script::Compile(
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::String::NewFromUtf8(isolate,
|
2013-11-22 12:43:17 +00:00
|
|
|
"function f() { return true; }"
|
|
|
|
"f()"))
|
|
|
|
->Run()
|
|
|
|
->IsTrue());
|
2010-05-21 20:52:19 +00:00
|
|
|
}
|
2013-04-22 15:01:45 +00:00
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void DoLoopCancelTerminate(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-04-22 15:01:45 +00:00
|
|
|
v8::TryCatch try_catch;
|
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Script::Compile(v8::String::NewFromUtf8(args.GetIsolate(),
|
|
|
|
"var term = true;"
|
|
|
|
"while(true) {"
|
|
|
|
" if (term) terminate();"
|
|
|
|
" term = false;"
|
|
|
|
"}"
|
|
|
|
"fail();"))->Run();
|
2013-04-22 15:01:45 +00:00
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(try_catch.Exception()->IsNull());
|
|
|
|
CHECK(try_catch.Message().IsEmpty());
|
|
|
|
CHECK(!try_catch.CanContinue());
|
|
|
|
CHECK(v8::V8::IsExecutionTerminating());
|
|
|
|
CHECK(try_catch.HasTerminated());
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::V8::CancelTerminateExecution(CcTest::isolate());
|
2013-04-22 15:01:45 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2013-04-22 15:01:45 +00:00
|
|
|
// Test that a single thread of JavaScript execution can terminate
|
|
|
|
// itself and then resume execution.
|
|
|
|
TEST(TerminateCancelTerminateFromThreadItself) {
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
2013-09-10 06:43:23 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(
|
|
|
|
isolate, TerminateCurrentThread, DoLoopCancelTerminate);
|
2013-09-10 06:43:23 +00:00
|
|
|
v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global);
|
2013-04-22 15:01:45 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate()));
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Handle<v8::String> source = v8::String::NewFromUtf8(
|
|
|
|
isolate, "try { doloop(); } catch(e) { fail(); } 'completed';");
|
2013-04-22 15:01:45 +00:00
|
|
|
// Check that execution completed with correct return value.
|
|
|
|
CHECK(v8::Script::Compile(source)->Run()->Equals(v8_str("completed")));
|
|
|
|
}
|
2014-05-28 18:40:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
void MicrotaskShouldNotRun(const v8::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
|
CHECK(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MicrotaskLoopForever(const v8::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
|
v8::Isolate* isolate = info.GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
// Enqueue another should-not-run task to ensure we clean out the queue
|
|
|
|
// when we terminate.
|
|
|
|
isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskShouldNotRun));
|
|
|
|
CompileRun("terminate(); while (true) { }");
|
|
|
|
CHECK(v8::V8::IsExecutionTerminating());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(TerminateFromOtherThreadWhileMicrotaskRunning) {
|
2014-06-30 13:25:46 +00:00
|
|
|
semaphore = new v8::base::Semaphore(0);
|
2014-05-28 18:40:04 +00:00
|
|
|
TerminatorThread thread(CcTest::i_isolate());
|
|
|
|
thread.Start();
|
|
|
|
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
isolate->SetAutorunMicrotasks(false);
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Handle<v8::ObjectTemplate> global =
|
|
|
|
CreateGlobalTemplate(CcTest::isolate(), Signal, DoLoop);
|
|
|
|
v8::Handle<v8::Context> context =
|
|
|
|
v8::Context::New(CcTest::isolate(), NULL, global);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskLoopForever));
|
|
|
|
// The second task should never be run because we bail out if we're
|
|
|
|
// terminating.
|
|
|
|
isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskShouldNotRun));
|
|
|
|
isolate->RunMicrotasks();
|
|
|
|
|
|
|
|
v8::V8::CancelTerminateExecution(isolate);
|
|
|
|
isolate->RunMicrotasks(); // should not run MicrotaskShouldNotRun
|
|
|
|
|
|
|
|
thread.Join();
|
|
|
|
delete semaphore;
|
|
|
|
semaphore = NULL;
|
|
|
|
}
|
2014-07-02 08:05:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
static int callback_counter = 0;
|
|
|
|
|
|
|
|
|
|
|
|
static void CounterCallback(v8::Isolate* isolate, void* data) {
|
|
|
|
callback_counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(PostponeTerminateException) {
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Handle<v8::ObjectTemplate> global =
|
|
|
|
CreateGlobalTemplate(CcTest::isolate(), TerminateCurrentThread, DoLoop);
|
|
|
|
v8::Handle<v8::Context> context =
|
|
|
|
v8::Context::New(CcTest::isolate(), NULL, global);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
|
|
|
static const char* terminate_and_loop =
|
|
|
|
"terminate(); for (var i = 0; i < 10000; i++);";
|
|
|
|
|
|
|
|
{ // Postpone terminate execution interrupts.
|
|
|
|
i::PostponeInterruptsScope p1(CcTest::i_isolate(),
|
|
|
|
i::StackGuard::TERMINATE_EXECUTION) ;
|
|
|
|
|
|
|
|
// API interrupts should still be triggered.
|
|
|
|
CcTest::isolate()->RequestInterrupt(&CounterCallback, NULL);
|
|
|
|
CHECK_EQ(0, callback_counter);
|
|
|
|
CompileRun(terminate_and_loop);
|
|
|
|
CHECK(!try_catch.HasTerminated());
|
|
|
|
CHECK_EQ(1, callback_counter);
|
|
|
|
|
|
|
|
{ // Postpone API interrupts as well.
|
|
|
|
i::PostponeInterruptsScope p2(CcTest::i_isolate(),
|
|
|
|
i::StackGuard::API_INTERRUPT);
|
|
|
|
|
|
|
|
// None of the two interrupts should trigger.
|
|
|
|
CcTest::isolate()->RequestInterrupt(&CounterCallback, NULL);
|
|
|
|
CompileRun(terminate_and_loop);
|
|
|
|
CHECK(!try_catch.HasTerminated());
|
|
|
|
CHECK_EQ(1, callback_counter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now the previously requested API interrupt should trigger.
|
|
|
|
CompileRun(terminate_and_loop);
|
|
|
|
CHECK(!try_catch.HasTerminated());
|
|
|
|
CHECK_EQ(2, callback_counter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now the previously requested terminate execution interrupt should trigger.
|
|
|
|
CompileRun("for (var i = 0; i < 10000; i++);");
|
|
|
|
CHECK(try_catch.HasTerminated());
|
|
|
|
CHECK_EQ(2, callback_counter);
|
|
|
|
}
|