Commit e6d8bdfa authored by Ross McIlroy's avatar Ross McIlroy Committed by Commit Bot

[Compile] Ensure CollectSourcePositions is context independent.

Also adds a NullContextScope for code which wants to ensure it is context
independent. Removes a workaround in V8ProfilerAgentImpl::startProfiling
which created a context due to CollectSourcePositions not being context
indpendent.

BUG=chromium:992063

Change-Id: I94c7eea6416dc64bc61fb8ff9cd945449a791a77
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1748693
Commit-Queue: Ross McIlroy <rmcilroy@chromium.org>
Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#63176}
parent 2021b171
......@@ -1181,6 +1181,9 @@ bool Compiler::CollectSourcePositions(Isolate* isolate,
DCHECK(shared_info->HasBytecodeArray());
DCHECK(!shared_info->GetBytecodeArray().HasSourcePositionTable());
// Source position collection should be context independent.
NullContextScope null_context_scope(isolate);
// Collecting source positions requires allocating a new source position
// table.
DCHECK(AllowHeapAllocation::IsAllowed());
......
......@@ -1936,6 +1936,14 @@ class V8_EXPORT_PRIVATE SaveAndSwitchContext : public SaveContext {
SaveAndSwitchContext(Isolate* isolate, Context new_context);
};
// A scope which sets the given isolate's context to null for its lifetime to
// ensure that code does not make assumptions on a context being available.
class NullContextScope : public SaveAndSwitchContext {
public:
explicit NullContextScope(Isolate* isolate)
: SaveAndSwitchContext(isolate, Context()) {}
};
class AssertNoContextChange {
#ifdef DEBUG
public:
......
......@@ -502,13 +502,6 @@ void V8ProfilerAgentImpl::startProfiling(const String16& title) {
m_state->integerProperty(ProfilerAgentState::samplingInterval, 0);
if (interval) m_profiler->SetSamplingInterval(interval);
}
// Create a new temporary context and enter it while starting profiling
// since this might involve collecting source positions and requires a
// context.
// TODO(992063): Remove this once parsing / collecting source positions has
// been properly made context independent.
v8::Local<v8::Context> tmpContext = v8::Context::New(m_isolate);
v8::Context::Scope scope(tmpContext);
++m_startedProfilesCount;
m_profiler->StartProfiling(toV8String(m_isolate, title), true);
}
......
......@@ -1034,7 +1034,7 @@ Handle<BytecodeArray> BytecodeGenerator::FinalizeBytecode(
#ifdef DEBUG
// Unoptimized compilation should be context-independent. Verify that we don't
// access the native context by nulling it out during finalization.
SaveAndSwitchContext save(isolate, Context());
NullContextScope null_context_scope(isolate);
#endif
AllocateDeferredConstants(isolate, script);
......
......@@ -1997,13 +1997,13 @@ HeapSnapshotGenerator::HeapSnapshotGenerator(
}
namespace {
class NullContextScope {
class NullContextForSnapshotScope {
public:
explicit NullContextScope(Isolate* isolate)
explicit NullContextForSnapshotScope(Isolate* isolate)
: isolate_(isolate), prev_(isolate->context()) {
isolate_->set_context(Context());
}
~NullContextScope() { isolate_->set_context(prev_); }
~NullContextForSnapshotScope() { isolate_->set_context(prev_); }
private:
Isolate* isolate_;
......@@ -2023,7 +2023,7 @@ bool HeapSnapshotGenerator::GenerateSnapshot() {
heap_->PreciseCollectAllGarbage(Heap::kNoGCFlags,
GarbageCollectionReason::kHeapProfiler);
NullContextScope null_context_scope(Isolate::FromHeap(heap_));
NullContextForSnapshotScope null_context_scope(Isolate::FromHeap(heap_));
#ifdef VERIFY_HEAP
Heap* debug_heap = heap_;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment