From 46c8e2165f578f0958cb4ea99c043b3e185f2ecc Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Wed, 23 Sep 2015 16:31:34 -0600 Subject: [PATCH] deps: backport 1f8555 from v8's upstream Original commit message: api: introduce SealHandleScope When debugging Handle leaks in io.js we found it very convenient to be able to Seal some specific (root in our case) scope to prevent Handle allocations in it, and easily find leakage. R=yangguo BUG= Review URL: https://codereview.chromium.org/1079713002 Cr-Commit-Position: refs/heads/master@{#27766} Should help us identify and fix Handle leaks in core and user-space code. PR-URL: https://github.com/nodejs/node/pull/3945 Reviewed-By: Fedor Indutny Reviewed-By: James Snell --- deps/v8/include/v8.h | 21 ++++++++++++++++++++ deps/v8/src/api.cc | 21 ++++++++++++++++++++ deps/v8/src/api.h | 5 +---- deps/v8/test/cctest/cctest.status | 1 + deps/v8/test/cctest/test-api.cc | 32 +++++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 4 deletions(-) diff --git a/deps/v8/include/v8.h b/deps/v8/include/v8.h index 8ae075797ce940..222404c01a5ff7 100644 --- a/deps/v8/include/v8.h +++ b/deps/v8/include/v8.h @@ -944,6 +944,27 @@ class ScriptOrigin { }; +class V8_EXPORT SealHandleScope { + public: + SealHandleScope(Isolate* isolate); + ~SealHandleScope(); + + private: + // Make it hard to create heap-allocated or illegal handle scopes by + // disallowing certain operations. + SealHandleScope(const SealHandleScope&); + void operator=(const SealHandleScope&); + void* operator new(size_t size); + void operator delete(void*, size_t); + + internal::Isolate* isolate_; + int prev_level_; + internal::Object** prev_limit_; +}; + + + + /** * A compiled JavaScript script, not yet tied to a Context. */ diff --git a/deps/v8/src/api.cc b/deps/v8/src/api.cc index e6597246eac687..9aa877cf4408c0 100644 --- a/deps/v8/src/api.cc +++ b/deps/v8/src/api.cc @@ -637,6 +637,27 @@ i::Object** EscapableHandleScope::Escape(i::Object** escape_value) { } +SealHandleScope::SealHandleScope(Isolate* isolate) { + i::Isolate* internal_isolate = reinterpret_cast(isolate); + + isolate_ = internal_isolate; + i::HandleScopeData* current = internal_isolate->handle_scope_data(); + prev_limit_ = current->limit; + current->limit = current->next; + prev_level_ = current->level; + current->level = 0; +} + + +SealHandleScope::~SealHandleScope() { + i::HandleScopeData* current = isolate_->handle_scope_data(); + DCHECK_EQ(0, current->level); + current->level = prev_level_; + DCHECK_EQ(current->next, current->limit); + current->limit = prev_limit_; +} + + void Context::Enter() { i::Handle env = Utils::OpenHandle(this); i::Isolate* isolate = env->GetIsolate(); diff --git a/deps/v8/src/api.h b/deps/v8/src/api.h index c87bd712efe2cf..f4ab1fc0ac7253 100644 --- a/deps/v8/src/api.h +++ b/deps/v8/src/api.h @@ -642,7 +642,7 @@ void HandleScopeImplementer::DeleteExtensions(internal::Object** prev_limit) { while (!blocks_.is_empty()) { internal::Object** block_start = blocks_.last(); internal::Object** block_limit = block_start + kHandleBlockSize; -#ifdef DEBUG + // SealHandleScope may make the prev_limit to point inside the block. if (block_start <= prev_limit && prev_limit <= block_limit) { #ifdef ENABLE_HANDLE_ZAPPING @@ -650,9 +650,6 @@ void HandleScopeImplementer::DeleteExtensions(internal::Object** prev_limit) { #endif break; } -#else - if (prev_limit == block_limit) break; -#endif blocks_.RemoveLast(); #ifdef ENABLE_HANDLE_ZAPPING diff --git a/deps/v8/test/cctest/cctest.status b/deps/v8/test/cctest/cctest.status index 60baaca081caed..b7a92dd8384b07 100644 --- a/deps/v8/test/cctest/cctest.status +++ b/deps/v8/test/cctest/cctest.status @@ -43,6 +43,7 @@ # they don't fail then test.py has failed. 'test-serialize/TestThatAlwaysFails': [FAIL], 'test-serialize/DependentTestThatAlwaysFails': [FAIL], + 'test-api/SealHandleScope': [FAIL], # This test always fails. It tests that LiveEdit causes abort when turned off. 'test-debug/LiveEditDisabled': [FAIL], diff --git a/deps/v8/test/cctest/test-api.cc b/deps/v8/test/cctest/test-api.cc index 9666a10fd7b409..1782fb427d4a3c 100644 --- a/deps/v8/test/cctest/test-api.cc +++ b/deps/v8/test/cctest/test-api.cc @@ -20625,6 +20625,38 @@ void CallCompletedCallbackException() { } +TEST(SealHandleScope) { + v8::Isolate* isolate = CcTest::isolate(); + v8::HandleScope handle_scope(isolate); + LocalContext env; + + v8::SealHandleScope seal(isolate); + + // Should fail + v8::Local obj = v8::Object::New(isolate); + + USE(obj); +} + + +TEST(SealHandleScopeNested) { + v8::Isolate* isolate = CcTest::isolate(); + v8::HandleScope handle_scope(isolate); + LocalContext env; + + v8::SealHandleScope seal(isolate); + + { + v8::HandleScope handle_scope(isolate); + + // Should work + v8::Local obj = v8::Object::New(isolate); + + USE(obj); + } +} + + TEST(CallCompletedCallbackOneException) { LocalContext env; v8::HandleScope scope(env->GetIsolate());