Skip to content

Commit 88df3dc

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.
1 parent ff74931 commit 88df3dc

File tree

6 files changed

+91
-18
lines changed

6 files changed

+91
-18
lines changed

deps/v8/include/v8.h

Lines changed: 18 additions & 0 deletions
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

Lines changed: 21 additions & 0 deletions
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

Lines changed: 1 addition & 4 deletions
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

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
@@ -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());

src/node.cc

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ using v8::ObjectTemplate;
101101
using v8::Promise;
102102
using v8::PromiseRejectMessage;
103103
using v8::PropertyCallbackInfo;
104+
using v8::SealHandleScope;
104105
using v8::String;
105106
using v8::TryCatch;
106107
using v8::Uint32;
@@ -3822,22 +3823,25 @@ static void StartNodeInstance(void* arg) {
38223823
if (instance_data->use_debug_agent())
38233824
EnableDebug(env);
38243825

3825-
bool more;
3826-
do {
3827-
v8::platform::PumpMessageLoop(default_platform, isolate);
3828-
more = uv_run(env->event_loop(), UV_RUN_ONCE);
3829-
3830-
if (more == false) {
3826+
{
3827+
SealHandleScope seal(isolate);
3828+
bool more;
3829+
do {
38313830
v8::platform::PumpMessageLoop(default_platform, isolate);
3832-
EmitBeforeExit(env);
3831+
more = uv_run(env->event_loop(), UV_RUN_ONCE);
38333832

3834-
// Emit `beforeExit` if the loop became alive either after emitting
3835-
// event, or after running some callbacks.
3836-
more = uv_loop_alive(env->event_loop());
3837-
if (uv_run(env->event_loop(), UV_RUN_NOWAIT) != 0)
3838-
more = true;
3839-
}
3840-
} while (more == true);
3833+
if (more == false) {
3834+
v8::platform::PumpMessageLoop(default_platform, isolate);
3835+
EmitBeforeExit(env);
3836+
3837+
// Emit `beforeExit` if the loop became alive either after emitting
3838+
// event, or after running some callbacks.
3839+
more = uv_loop_alive(env->event_loop());
3840+
if (uv_run(env->event_loop(), UV_RUN_NOWAIT) != 0)
3841+
more = true;
3842+
}
3843+
} while (more == true);
3844+
}
38413845

38423846
int exit_code = EmitExit(env);
38433847
if (instance_data->is_main())

0 commit comments

Comments
 (0)