Skip to content

Commit a5244d3

Browse files
committed
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. NOTE: Works only in Debug build now, but is still better than nothing. PR-URL: #1395 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent a4d8847 commit a5244d3

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed

deps/v8/include/v8.h

+18
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,24 @@ class ScriptOrigin {
10061006
Handle<Integer> script_id_;
10071007
};
10081008

1009+
class V8_EXPORT SealHandleScope {
1010+
public:
1011+
SealHandleScope(Isolate* isolate);
1012+
~SealHandleScope();
1013+
1014+
private:
1015+
// Make it hard to create heap-allocated or illegal handle scopes by
1016+
// disallowing certain operations.
1017+
SealHandleScope(const SealHandleScope&);
1018+
void operator=(const SealHandleScope&);
1019+
void* operator new(size_t size);
1020+
void operator delete(void*, size_t);
1021+
1022+
internal::Isolate* isolate_;
1023+
int prev_level_;
1024+
internal::Object** prev_limit_;
1025+
};
1026+
10091027

10101028
/**
10111029
* A compiled JavaScript script, not yet tied to a Context.

deps/v8/src/api.cc

+21
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,27 @@ i::Object** EscapableHandleScope::Escape(i::Object** escape_value) {
515515
}
516516

517517

518+
SealHandleScope::SealHandleScope(Isolate* isolate) {
519+
i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
520+
521+
isolate_ = internal_isolate;
522+
i::HandleScopeData* current = internal_isolate->handle_scope_data();
523+
prev_limit_ = current->limit;
524+
current->limit = current->next;
525+
prev_level_ = current->level;
526+
current->level = 0;
527+
}
528+
529+
530+
SealHandleScope::~SealHandleScope() {
531+
i::HandleScopeData* current = isolate_->handle_scope_data();
532+
DCHECK_EQ(0, current->level);
533+
current->level = prev_level_;
534+
DCHECK_EQ(current->next, current->limit);
535+
current->limit = prev_limit_;
536+
}
537+
538+
518539
void Context::Enter() {
519540
i::Handle<i::Context> env = Utils::OpenHandle(this);
520541
i::Isolate* isolate = env->GetIsolate();

deps/v8/src/api.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -650,17 +650,14 @@ void HandleScopeImplementer::DeleteExtensions(internal::Object** prev_limit) {
650650
while (!blocks_.is_empty()) {
651651
internal::Object** block_start = blocks_.last();
652652
internal::Object** block_limit = block_start + kHandleBlockSize;
653-
#ifdef DEBUG
653+
654654
// SealHandleScope may make the prev_limit to point inside the block.
655655
if (block_start <= prev_limit && prev_limit <= block_limit) {
656656
#ifdef ENABLE_HANDLE_ZAPPING
657657
internal::HandleScope::ZapRange(prev_limit, block_limit);
658658
#endif
659659
break;
660660
}
661-
#else
662-
if (prev_limit == block_limit) break;
663-
#endif
664661

665662
blocks_.RemoveLast();
666663
#ifdef ENABLE_HANDLE_ZAPPING

deps/v8/test/cctest/cctest.status

+1
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

+32
Original file line numberDiff line numberDiff line change
@@ -21895,6 +21895,38 @@ void CallCompletedCallbackException() {
2189521895
}
2189621896

2189721897

21898+
TEST(SealHandleScope) {
21899+
v8::Isolate* isolate = CcTest::isolate();
21900+
v8::HandleScope handle_scope(isolate);
21901+
LocalContext env;
21902+
21903+
v8::SealHandleScope seal(isolate);
21904+
21905+
// Should fail
21906+
v8::Local<v8::Object> obj = v8::Object::New(isolate);
21907+
21908+
USE(obj);
21909+
}
21910+
21911+
21912+
TEST(SealHandleScopeNested) {
21913+
v8::Isolate* isolate = CcTest::isolate();
21914+
v8::HandleScope handle_scope(isolate);
21915+
LocalContext env;
21916+
21917+
v8::SealHandleScope seal(isolate);
21918+
21919+
{
21920+
v8::HandleScope handle_scope(isolate);
21921+
21922+
// Should work
21923+
v8::Local<v8::Object> obj = v8::Object::New(isolate);
21924+
21925+
USE(obj);
21926+
}
21927+
}
21928+
21929+
2189821930
TEST(CallCompletedCallbackOneException) {
2189921931
LocalContext env;
2190021932
v8::HandleScope scope(env->GetIsolate());

0 commit comments

Comments
 (0)