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.
|
|
|
|
|
2016-09-01 12:01:33 +00:00
|
|
|
#include "src/api.h"
|
|
|
|
#include "src/isolate.h"
|
2017-01-09 13:43:28 +00:00
|
|
|
#include "src/objects-inl.h"
|
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) {
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!args.GetIsolate()->IsExecutionTerminating());
|
|
|
|
args.GetIsolate()->TerminateExecution();
|
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) {
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!args.GetIsolate()->IsExecutionTerminating());
|
|
|
|
v8::MaybeLocal<v8::Value> result =
|
|
|
|
CompileRun(args.GetIsolate()->GetCurrentContext(),
|
|
|
|
"try { doloop(); fail(); } catch(e) { fail(); }");
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(result.IsEmpty());
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(args.GetIsolate()->IsExecutionTerminating());
|
2009-08-19 15:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void DoLoop(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2015-05-28 12:49:31 +00:00
|
|
|
v8::TryCatch try_catch(args.GetIsolate());
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!args.GetIsolate()->IsExecutionTerminating());
|
|
|
|
v8::MaybeLocal<v8::Value> result =
|
|
|
|
CompileRun(args.GetIsolate()->GetCurrentContext(),
|
|
|
|
"function f() {"
|
|
|
|
" var term = true;"
|
|
|
|
" try {"
|
|
|
|
" while(true) {"
|
|
|
|
" if (term) terminate();"
|
|
|
|
" term = false;"
|
|
|
|
" }"
|
|
|
|
" fail();"
|
|
|
|
" } catch(e) {"
|
|
|
|
" fail();"
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
"f()");
|
|
|
|
CHECK(result.IsEmpty());
|
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());
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(args.GetIsolate()->IsExecutionTerminating());
|
2009-08-19 15:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void DoLoopNoCall(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2015-05-28 12:49:31 +00:00
|
|
|
v8::TryCatch try_catch(args.GetIsolate());
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!args.GetIsolate()->IsExecutionTerminating());
|
|
|
|
v8::MaybeLocal<v8::Value> result =
|
|
|
|
CompileRun(args.GetIsolate()->GetCurrentContext(),
|
|
|
|
"var term = true;"
|
|
|
|
"while(true) {"
|
|
|
|
" if (term) terminate();"
|
|
|
|
" term = false;"
|
|
|
|
"}");
|
|
|
|
CHECK(result.IsEmpty());
|
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());
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(args.GetIsolate()->IsExecutionTerminating());
|
2009-11-24 12:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> CreateGlobalTemplate(
|
|
|
|
v8::Isolate* isolate, v8::FunctionCallback terminate,
|
2013-06-20 12:28:27 +00:00
|
|
|
v8::FunctionCallback doloop) {
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
|
|
|
|
global->Set(v8_str("terminate"),
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::FunctionTemplate::New(isolate, terminate));
|
2015-09-21 10:34:44 +00:00
|
|
|
global->Set(v8_str("fail"), v8::FunctionTemplate::New(isolate, Fail));
|
|
|
|
global->Set(v8_str("loop"), v8::FunctionTemplate::New(isolate, Loop));
|
|
|
|
global->Set(v8_str("doloop"), 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());
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global =
|
2013-11-22 12:43:17 +00:00
|
|
|
CreateGlobalTemplate(CcTest::isolate(), TerminateCurrentThread, DoLoop);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<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);
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!CcTest::isolate()->IsExecutionTerminating());
|
2009-11-24 12:23:29 +00:00
|
|
|
// Run a loop that will be infinite if thread termination does not work.
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::MaybeLocal<v8::Value> result =
|
|
|
|
CompileRun(CcTest::isolate()->GetCurrentContext(),
|
|
|
|
"try { loop(); fail(); } catch(e) { fail(); }");
|
|
|
|
CHECK(result.IsEmpty());
|
2009-11-24 12:23:29 +00:00
|
|
|
// Test that we can run the code again after thread termination.
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!CcTest::isolate()->IsExecutionTerminating());
|
|
|
|
result = CompileRun(CcTest::isolate()->GetCurrentContext(),
|
|
|
|
"try { loop(); fail(); } catch(e) { fail(); }");
|
|
|
|
CHECK(result.IsEmpty());
|
2009-11-24 12:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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());
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global = CreateGlobalTemplate(
|
2013-11-22 12:43:17 +00:00
|
|
|
CcTest::isolate(), TerminateCurrentThread, DoLoopNoCall);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<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);
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!CcTest::isolate()->IsExecutionTerminating());
|
2009-08-19 15:14:11 +00:00
|
|
|
// Run a loop that will be infinite if thread termination does not work.
|
2015-09-21 10:34:44 +00:00
|
|
|
static const char* source = "try { loop(); fail(); } catch(e) { fail(); }";
|
|
|
|
v8::MaybeLocal<v8::Value> result =
|
|
|
|
CompileRun(CcTest::isolate()->GetCurrentContext(), source);
|
|
|
|
CHECK(result.IsEmpty());
|
|
|
|
CHECK(!CcTest::isolate()->IsExecutionTerminating());
|
2009-08-19 15:14:11 +00:00
|
|
|
// Test that we can run the code again after thread termination.
|
2015-09-21 10:34:44 +00:00
|
|
|
result = CompileRun(CcTest::isolate()->GetCurrentContext(), source);
|
|
|
|
CHECK(result.IsEmpty());
|
2009-08-19 15:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|
2014-08-12 13:33:35 +00:00
|
|
|
: Thread(Options("TerminatorThread")),
|
|
|
|
isolate_(reinterpret_cast<v8::Isolate*>(isolate)) {}
|
2009-08-19 15:14:11 +00:00
|
|
|
void Run() {
|
|
|
|
semaphore->Wait();
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!isolate_->IsExecutionTerminating());
|
|
|
|
isolate_->TerminateExecution();
|
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());
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global =
|
2013-11-22 12:43:17 +00:00
|
|
|
CreateGlobalTemplate(CcTest::isolate(), Signal, DoLoop);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<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);
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!CcTest::isolate()->IsExecutionTerminating());
|
2009-08-19 15:14:11 +00:00
|
|
|
// Run a loop that will be infinite if thread termination does not work.
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::MaybeLocal<v8::Value> result =
|
|
|
|
CompileRun(CcTest::isolate()->GetCurrentContext(),
|
|
|
|
"try { loop(); fail(); } catch(e) { fail(); }");
|
|
|
|
CHECK(result.IsEmpty());
|
2009-08-19 15:14:11 +00:00
|
|
|
thread.Join();
|
|
|
|
delete semaphore;
|
|
|
|
semaphore = NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-07 11:43:41 +00:00
|
|
|
// Test that execution can be terminated from within JSON.stringify.
|
|
|
|
TEST(TerminateJsonStringify) {
|
|
|
|
semaphore = new v8::base::Semaphore(0);
|
|
|
|
TerminatorThread thread(CcTest::i_isolate());
|
|
|
|
thread.Start();
|
|
|
|
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::ObjectTemplate> global =
|
|
|
|
CreateGlobalTemplate(CcTest::isolate(), Signal, DoLoop);
|
|
|
|
v8::Local<v8::Context> context =
|
|
|
|
v8::Context::New(CcTest::isolate(), NULL, global);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
CHECK(!CcTest::isolate()->IsExecutionTerminating());
|
|
|
|
v8::MaybeLocal<v8::Value> result =
|
|
|
|
CompileRun(CcTest::isolate()->GetCurrentContext(),
|
|
|
|
"var x = [];"
|
|
|
|
"x[2**31]=1;"
|
|
|
|
"terminate();"
|
|
|
|
"JSON.stringify(x);"
|
|
|
|
"fail();");
|
|
|
|
CHECK(result.IsEmpty());
|
|
|
|
thread.Join();
|
|
|
|
delete semaphore;
|
|
|
|
semaphore = NULL;
|
|
|
|
}
|
2009-08-19 15:14:11 +00:00
|
|
|
|
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) {
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!args.GetIsolate()->IsExecutionTerminating());
|
|
|
|
args.GetIsolate()->TerminateExecution();
|
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());
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Maybe<bool> val =
|
|
|
|
result->Set(args.GetIsolate()->GetCurrentContext(), v8_str("x"),
|
|
|
|
v8::Integer::New(args.GetIsolate(), 42));
|
|
|
|
CHECK(val.FromJust());
|
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) {
|
2015-05-28 12:49:31 +00:00
|
|
|
v8::TryCatch try_catch(args.GetIsolate());
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!args.GetIsolate()->IsExecutionTerminating());
|
|
|
|
v8::MaybeLocal<v8::Value> result =
|
|
|
|
CompileRun(args.GetIsolate()->GetCurrentContext(),
|
|
|
|
"function f() {"
|
|
|
|
" try {"
|
|
|
|
" while(true) {"
|
|
|
|
" terminate_or_return_object().x;"
|
|
|
|
" }"
|
|
|
|
" fail();"
|
|
|
|
" } catch(e) {"
|
|
|
|
" (function() {})();" // trigger stack check.
|
|
|
|
" fail();"
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
"f()");
|
|
|
|
CHECK(result.IsEmpty());
|
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());
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(args.GetIsolate()->IsExecutionTerminating());
|
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);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
|
|
|
|
global->Set(v8_str("terminate_or_return_object"),
|
|
|
|
v8::FunctionTemplate::New(isolate, TerminateOrReturnObject));
|
|
|
|
global->Set(v8_str("fail"), v8::FunctionTemplate::New(isolate, Fail));
|
|
|
|
global->Set(v8_str("loop"),
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::FunctionTemplate::New(isolate, LoopGetProperty));
|
2009-09-01 13:55:45 +00:00
|
|
|
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global);
|
2009-09-01 13:55:45 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!isolate->IsExecutionTerminating());
|
2009-09-01 13:55:45 +00:00
|
|
|
// Run a loop that will be infinite if thread termination does not work.
|
2015-09-21 10:34:44 +00:00
|
|
|
static const char* source = "try { loop(); fail(); } catch(e) { fail(); }";
|
2009-09-01 13:55:45 +00:00
|
|
|
call_count = 0;
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::MaybeLocal<v8::Value> result =
|
|
|
|
CompileRun(isolate->GetCurrentContext(), source);
|
|
|
|
CHECK(result.IsEmpty());
|
2009-09-01 13:55:45 +00:00
|
|
|
// Test that we can run the code again after thread termination.
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!isolate->IsExecutionTerminating());
|
2009-09-01 13:55:45 +00:00
|
|
|
call_count = 0;
|
2015-09-21 10:34:44 +00:00
|
|
|
result = CompileRun(isolate->GetCurrentContext(), source);
|
|
|
|
CHECK(result.IsEmpty());
|
2009-09-01 13:55:45 +00:00
|
|
|
}
|
2010-05-21 20:52:19 +00:00
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2015-02-11 10:21:18 +00:00
|
|
|
v8::Persistent<v8::String> reenter_script_1;
|
|
|
|
v8::Persistent<v8::String> reenter_script_2;
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void ReenterAfterTermination(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2015-05-28 12:49:31 +00:00
|
|
|
v8::TryCatch try_catch(args.GetIsolate());
|
2015-02-11 10:21:18 +00:00
|
|
|
v8::Isolate* isolate = args.GetIsolate();
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!isolate->IsExecutionTerminating());
|
2015-02-11 10:21:18 +00:00
|
|
|
v8::Local<v8::String> script =
|
|
|
|
v8::Local<v8::String>::New(isolate, reenter_script_1);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::MaybeLocal<v8::Value> result = CompileRun(script);
|
|
|
|
CHECK(result.IsEmpty());
|
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());
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(isolate->IsExecutionTerminating());
|
2015-02-11 10:21:18 +00:00
|
|
|
script = v8::Local<v8::String>::New(isolate, reenter_script_2);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::MaybeLocal<v8::Script> compiled_script =
|
|
|
|
v8::Script::Compile(isolate->GetCurrentContext(), script);
|
|
|
|
CHECK(compiled_script.IsEmpty());
|
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);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global = CreateGlobalTemplate(
|
2013-12-18 10:31:42 +00:00
|
|
|
isolate, TerminateCurrentThread, ReenterAfterTermination);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global);
|
2010-05-21 20:52:19 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!v8::Isolate::GetCurrent()->IsExecutionTerminating());
|
2015-02-11 10:21:18 +00:00
|
|
|
// Create script strings upfront as it won't work when terminating.
|
|
|
|
reenter_script_1.Reset(isolate, v8_str(
|
|
|
|
"function f() {"
|
|
|
|
" var term = true;"
|
|
|
|
" try {"
|
|
|
|
" while(true) {"
|
|
|
|
" if (term) terminate();"
|
|
|
|
" term = false;"
|
|
|
|
" }"
|
|
|
|
" fail();"
|
|
|
|
" } catch(e) {"
|
|
|
|
" fail();"
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
"f()"));
|
|
|
|
reenter_script_2.Reset(isolate, v8_str("function f() { fail(); } f()"));
|
|
|
|
CompileRun("try { loop(); fail(); } catch(e) { fail(); }");
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!isolate->IsExecutionTerminating());
|
2010-05-21 20:52:19 +00:00
|
|
|
// Check we can run JS again after termination.
|
2015-02-11 10:21:18 +00:00
|
|
|
CHECK(CompileRun("function f() { return true; } f()")->IsTrue());
|
|
|
|
reenter_script_1.Reset();
|
|
|
|
reenter_script_2.Reset();
|
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) {
|
2015-05-28 12:49:31 +00:00
|
|
|
v8::TryCatch try_catch(args.GetIsolate());
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!v8::Isolate::GetCurrent()->IsExecutionTerminating());
|
|
|
|
v8::MaybeLocal<v8::Value> result =
|
|
|
|
CompileRun(args.GetIsolate()->GetCurrentContext(),
|
|
|
|
"var term = true;"
|
|
|
|
"while(true) {"
|
|
|
|
" if (term) terminate();"
|
|
|
|
" term = false;"
|
|
|
|
"}"
|
|
|
|
"fail();");
|
|
|
|
CHECK(result.IsEmpty());
|
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());
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(v8::Isolate::GetCurrent()->IsExecutionTerminating());
|
2013-04-22 15:01:45 +00:00
|
|
|
CHECK(try_catch.HasTerminated());
|
2015-09-21 10:34:44 +00:00
|
|
|
CcTest::isolate()->CancelTerminateExecution();
|
|
|
|
CHECK(!v8::Isolate::GetCurrent()->IsExecutionTerminating());
|
2013-04-22 15:01:45 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global = CreateGlobalTemplate(
|
2013-11-22 12:43:17 +00:00
|
|
|
isolate, TerminateCurrentThread, DoLoopCancelTerminate);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global);
|
2013-04-22 15:01:45 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!CcTest::isolate()->IsExecutionTerminating());
|
2013-04-22 15:01:45 +00:00
|
|
|
// Check that execution completed with correct return value.
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::Value> result =
|
|
|
|
CompileRun(isolate->GetCurrentContext(),
|
|
|
|
"try { doloop(); } catch(e) { fail(); } 'completed';")
|
|
|
|
.ToLocalChecked();
|
|
|
|
CHECK(result->Equals(isolate->GetCurrentContext(), v8_str("completed"))
|
|
|
|
.FromJust());
|
2013-04-22 15:01:45 +00:00
|
|
|
}
|
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.
|
2015-09-21 10:34:44 +00:00
|
|
|
isolate->EnqueueMicrotask(
|
|
|
|
v8::Function::New(isolate->GetCurrentContext(), MicrotaskShouldNotRun)
|
|
|
|
.ToLocalChecked());
|
2014-05-28 18:40:04 +00:00
|
|
|
CompileRun("terminate(); while (true) { }");
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(v8::Isolate::GetCurrent()->IsExecutionTerminating());
|
2014-05-28 18:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
2016-03-04 17:41:36 +00:00
|
|
|
isolate->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
|
2014-05-28 18:40:04 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global =
|
2014-05-28 18:40:04 +00:00
|
|
|
CreateGlobalTemplate(CcTest::isolate(), Signal, DoLoop);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::Context> context =
|
2014-05-28 18:40:04 +00:00
|
|
|
v8::Context::New(CcTest::isolate(), NULL, global);
|
|
|
|
v8::Context::Scope context_scope(context);
|
2015-09-21 10:34:44 +00:00
|
|
|
isolate->EnqueueMicrotask(
|
|
|
|
v8::Function::New(isolate->GetCurrentContext(), MicrotaskLoopForever)
|
|
|
|
.ToLocalChecked());
|
2014-05-28 18:40:04 +00:00
|
|
|
// The second task should never be run because we bail out if we're
|
|
|
|
// terminating.
|
2015-09-21 10:34:44 +00:00
|
|
|
isolate->EnqueueMicrotask(
|
|
|
|
v8::Function::New(isolate->GetCurrentContext(), MicrotaskShouldNotRun)
|
|
|
|
.ToLocalChecked());
|
2014-05-28 18:40:04 +00:00
|
|
|
isolate->RunMicrotasks();
|
|
|
|
|
2015-09-21 10:34:44 +00:00
|
|
|
isolate->CancelTerminateExecution();
|
2014-05-28 18:40:04 +00:00
|
|
|
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);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global =
|
2014-07-02 08:05:40 +00:00
|
|
|
CreateGlobalTemplate(CcTest::isolate(), TerminateCurrentThread, DoLoop);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::Context> context =
|
2014-07-02 08:05:40 +00:00
|
|
|
v8::Context::New(CcTest::isolate(), NULL, global);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
2015-05-28 12:49:31 +00:00
|
|
|
v8::TryCatch try_catch(isolate);
|
2014-07-02 08:05:40 +00:00
|
|
|
static const char* terminate_and_loop =
|
|
|
|
"terminate(); for (var i = 0; i < 10000; i++);";
|
|
|
|
|
|
|
|
{ // Postpone terminate execution interrupts.
|
|
|
|
i::PostponeInterruptsScope p1(CcTest::i_isolate(),
|
2015-09-28 08:18:29 +00:00
|
|
|
i::StackGuard::TERMINATE_EXECUTION);
|
2014-07-02 08:05:40 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2014-09-01 09:11:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(ErrorObjectAfterTermination) {
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::Context> context = v8::Context::New(CcTest::isolate());
|
2014-09-01 09:11:44 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2015-09-21 10:34:44 +00:00
|
|
|
isolate->TerminateExecution();
|
2014-09-01 09:11:44 +00:00
|
|
|
v8::Local<v8::Value> error = v8::Exception::Error(v8_str("error"));
|
2016-07-20 13:02:36 +00:00
|
|
|
CHECK(error->IsNativeError());
|
2014-09-01 09:11:44 +00:00
|
|
|
}
|
2015-02-24 08:30:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
void InnerTryCallTerminate(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!args.GetIsolate()->IsExecutionTerminating());
|
|
|
|
v8::Local<v8::Object> global = CcTest::global();
|
|
|
|
v8::Local<v8::Function> loop = v8::Local<v8::Function>::Cast(
|
|
|
|
global->Get(CcTest::isolate()->GetCurrentContext(), v8_str("loop"))
|
|
|
|
.ToLocalChecked());
|
2017-01-17 13:01:03 +00:00
|
|
|
i::MaybeHandle<i::Object> exception;
|
2015-02-24 08:30:22 +00:00
|
|
|
i::MaybeHandle<i::Object> result =
|
2015-10-23 12:26:49 +00:00
|
|
|
i::Execution::TryCall(CcTest::i_isolate(), v8::Utils::OpenHandle((*loop)),
|
2017-01-17 13:01:03 +00:00
|
|
|
v8::Utils::OpenHandle((*global)), 0, nullptr,
|
|
|
|
i::Execution::MessageHandling::kReport, &exception);
|
2015-02-24 08:30:22 +00:00
|
|
|
CHECK(result.is_null());
|
|
|
|
// TryCall ignores terminate execution, but rerequests the interrupt.
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!args.GetIsolate()->IsExecutionTerminating());
|
2015-02-24 08:30:22 +00:00
|
|
|
CHECK(CompileRun("1 + 1;").IsEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(TerminationInInnerTryCall) {
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global_template = CreateGlobalTemplate(
|
2015-02-24 08:30:22 +00:00
|
|
|
CcTest::isolate(), TerminateCurrentThread, DoLoopNoCall);
|
|
|
|
global_template->Set(
|
|
|
|
v8_str("inner_try_call_terminate"),
|
|
|
|
v8::FunctionTemplate::New(isolate, InnerTryCallTerminate));
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::Context> context =
|
2015-02-24 08:30:22 +00:00
|
|
|
v8::Context::New(CcTest::isolate(), NULL, global_template);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
{
|
2015-05-28 12:49:31 +00:00
|
|
|
v8::TryCatch try_catch(isolate);
|
2015-02-24 08:30:22 +00:00
|
|
|
CompileRun("inner_try_call_terminate()");
|
|
|
|
CHECK(try_catch.HasTerminated());
|
|
|
|
}
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Maybe<int32_t> result = CompileRun("2 + 2")->Int32Value(
|
|
|
|
v8::Isolate::GetCurrent()->GetCurrentContext());
|
|
|
|
CHECK_EQ(4, result.FromJust());
|
|
|
|
CHECK(!v8::Isolate::GetCurrent()->IsExecutionTerminating());
|
2015-02-24 08:30:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(TerminateAndTryCall) {
|
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global = CreateGlobalTemplate(
|
2015-02-24 08:30:22 +00:00
|
|
|
isolate, TerminateCurrentThread, DoLoopCancelTerminate);
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global);
|
2015-02-24 08:30:22 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!isolate->IsExecutionTerminating());
|
2015-05-28 12:49:31 +00:00
|
|
|
v8::TryCatch try_catch(isolate);
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!isolate->IsExecutionTerminating());
|
2015-02-24 08:30:22 +00:00
|
|
|
// Terminate execution has been triggered inside TryCall, but re-requested
|
|
|
|
// to trigger later.
|
|
|
|
CHECK(CompileRun("terminate(); reference_error();").IsEmpty());
|
|
|
|
CHECK(try_catch.HasCaught());
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!isolate->IsExecutionTerminating());
|
|
|
|
v8::Local<v8::Value> value =
|
|
|
|
CcTest::global()
|
|
|
|
->Get(isolate->GetCurrentContext(), v8_str("terminate"))
|
|
|
|
.ToLocalChecked();
|
|
|
|
CHECK(value->IsFunction());
|
2015-02-24 08:30:22 +00:00
|
|
|
// The first stack check after terminate has been re-requested fails.
|
|
|
|
CHECK(CompileRun("1 + 1").IsEmpty());
|
2015-09-21 10:34:44 +00:00
|
|
|
CHECK(!isolate->IsExecutionTerminating());
|
2015-02-24 08:30:22 +00:00
|
|
|
// V8 then recovers.
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Maybe<int32_t> result = CompileRun("2 + 2")->Int32Value(
|
|
|
|
v8::Isolate::GetCurrent()->GetCurrentContext());
|
|
|
|
CHECK_EQ(4, result.FromJust());
|
|
|
|
CHECK(!isolate->IsExecutionTerminating());
|
2015-02-24 08:30:22 +00:00
|
|
|
}
|
2017-08-31 12:26:27 +00:00
|
|
|
|
|
|
|
class ConsoleImpl : public v8::debug::ConsoleDelegate {
|
|
|
|
private:
|
|
|
|
void Log(const v8::debug::ConsoleCallArguments& args,
|
|
|
|
const v8::debug::ConsoleContext&) override {
|
|
|
|
CompileRun("1 + 1");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(TerminateConsole) {
|
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
ConsoleImpl console;
|
|
|
|
v8::debug::SetConsoleDelegate(isolate, &console);
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::ObjectTemplate> global = CreateGlobalTemplate(
|
|
|
|
isolate, TerminateCurrentThread, DoLoopCancelTerminate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
CHECK(!isolate->IsExecutionTerminating());
|
|
|
|
v8::TryCatch try_catch(isolate);
|
|
|
|
CHECK(!isolate->IsExecutionTerminating());
|
|
|
|
CHECK(CompileRun("terminate(); console.log(); fail();").IsEmpty());
|
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(!isolate->IsExecutionTerminating());
|
|
|
|
}
|