Skip to content

Commit 9745eca

Browse files
indutnytargos
authored andcommitted
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@{nodejs#27766} Should help us identify and fix Handle leaks in core and user-space code. NOTE: Works only in Debug build now, but is still better than nothing. PR-URL: nodejs#1395 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 2b67ca5 commit 9745eca

5 files changed

Lines changed: 73 additions & 4 deletions

File tree

deps/v8/include/v8.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,24 @@ class ScriptOrigin {
10721072
Handle<Value> source_map_url_;
10731073
};
10741074

1075+
class V8_EXPORT SealHandleScope {
1076+
public:
1077+
SealHandleScope(Isolate* isolate);
1078+
~SealHandleScope();
1079+
1080+
private:
1081+
// Make it hard to create heap-allocated or illegal handle scopes by
1082+
// disallowing certain operations.
1083+
SealHandleScope(const SealHandleScope&);
1084+
void operator=(const SealHandleScope&);
1085+
void* operator new(size_t size);
1086+
void operator delete(void*, size_t);
1087+
1088+
internal::Isolate* isolate_;
1089+
int prev_level_;
1090+
internal::Object** prev_limit_;
1091+
};
1092+
10751093

10761094
/**
10771095
* A compiled JavaScript script, not yet tied to a Context.

deps/v8/src/api.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,27 @@ i::Object** EscapableHandleScope::Escape(i::Object** escape_value) {
682682
}
683683

684684

685+
SealHandleScope::SealHandleScope(Isolate* isolate) {
686+
i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
687+
688+
isolate_ = internal_isolate;
689+
i::HandleScopeData* current = internal_isolate->handle_scope_data();
690+
prev_limit_ = current->limit;
691+
current->limit = current->next;
692+
prev_level_ = current->level;
693+
current->level = 0;
694+
}
695+
696+
697+
SealHandleScope::~SealHandleScope() {
698+
i::HandleScopeData* current = isolate_->handle_scope_data();
699+
DCHECK_EQ(0, current->level);
700+
current->level = prev_level_;
701+
DCHECK_EQ(current->next, current->limit);
702+
current->limit = prev_limit_;
703+
}
704+
705+
685706
void Context::Enter() {
686707
i::Handle<i::Context> env = Utils::OpenHandle(this);
687708
i::Isolate* isolate = env->GetIsolate();

deps/v8/src/api.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -661,17 +661,14 @@ void HandleScopeImplementer::DeleteExtensions(internal::Object** prev_limit) {
661661
while (!blocks_.is_empty()) {
662662
internal::Object** block_start = blocks_.last();
663663
internal::Object** block_limit = block_start + kHandleBlockSize;
664-
#ifdef DEBUG
664+
665665
// SealHandleScope may make the prev_limit to point inside the block.
666666
if (block_start <= prev_limit && prev_limit <= block_limit) {
667667
#ifdef ENABLE_HANDLE_ZAPPING
668668
internal::HandleScope::ZapRange(prev_limit, block_limit);
669669
#endif
670670
break;
671671
}
672-
#else
673-
if (prev_limit == block_limit) break;
674-
#endif
675672

676673
blocks_.RemoveLast();
677674
#ifdef ENABLE_HANDLE_ZAPPING

deps/v8/test/cctest/cctest.status

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
# they don't fail then test.py has failed.
4141
'test-serialize/TestThatAlwaysFails': [FAIL],
4242
'test-serialize/DependentTestThatAlwaysFails': [FAIL],
43+
'test-api/SealHandleScope': [FAIL],
4344

4445
# This test always fails. It tests that LiveEdit causes abort when turned off.
4546
'test-debug/LiveEditDisabled': [FAIL],

deps/v8/test/cctest/test-api.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18829,6 +18829,38 @@ void CallCompletedCallbackException() {
1882918829
}
1883018830

1883118831

18832+
TEST(SealHandleScope) {
18833+
v8::Isolate* isolate = CcTest::isolate();
18834+
v8::HandleScope handle_scope(isolate);
18835+
LocalContext env;
18836+
18837+
v8::SealHandleScope seal(isolate);
18838+
18839+
// Should fail
18840+
v8::Local<v8::Object> obj = v8::Object::New(isolate);
18841+
18842+
USE(obj);
18843+
}
18844+
18845+
18846+
TEST(SealHandleScopeNested) {
18847+
v8::Isolate* isolate = CcTest::isolate();
18848+
v8::HandleScope handle_scope(isolate);
18849+
LocalContext env;
18850+
18851+
v8::SealHandleScope seal(isolate);
18852+
18853+
{
18854+
v8::HandleScope handle_scope(isolate);
18855+
18856+
// Should work
18857+
v8::Local<v8::Object> obj = v8::Object::New(isolate);
18858+
18859+
USE(obj);
18860+
}
18861+
}
18862+
18863+
1883218864
TEST(CallCompletedCallbackOneException) {
1883318865
LocalContext env;
1883418866
v8::HandleScope scope(env->GetIsolate());

0 commit comments

Comments
 (0)