Skip to content

Commit b87d408

Browse files
psmarshallCommit Bot
authored and
Commit Bot
committed
[heap-profiler] Fix a use-after-free when snapshots are deleted
If a caller starts the sampling heap profiler and takes a snapshot, and then deletes the snapshot before the sampling has completed, a use-after-free will occur on the StringsStorage pointer. The same issue applies for StartTrackingHeapObjects which shares the same StringsStorage object. Bug: v8:8373 Change-Id: I5d69d60d3f9465f9dd3b3bef107c204e0fda0643 Reviewed-on: https://chromium-review.googlesource.com/c/1301477 Commit-Queue: Peter Marshall <petermarshall@chromium.org> Reviewed-by: Alexei Filippov <alph@chromium.org> Cr-Commit-Position: refs/heads/master@{#57114}
1 parent 767b152 commit b87d408

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/profiler/heap-profiler.cc

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ HeapProfiler::~HeapProfiler() = default;
2323

2424
void HeapProfiler::DeleteAllSnapshots() {
2525
snapshots_.clear();
26-
names_.reset(new StringsStorage());
26+
MaybeClearStringsStorage();
2727
}
2828

29+
void HeapProfiler::MaybeClearStringsStorage() {
30+
if (snapshots_.empty() && !sampling_heap_profiler_ && !allocation_tracker_) {
31+
names_.reset(new StringsStorage());
32+
}
33+
}
2934

3035
void HeapProfiler::RemoveSnapshot(HeapSnapshot* snapshot) {
3136
snapshots_.erase(
@@ -126,6 +131,7 @@ bool HeapProfiler::StartSamplingHeapProfiler(
126131

127132
void HeapProfiler::StopSamplingHeapProfiler() {
128133
sampling_heap_profiler_.reset();
134+
MaybeClearStringsStorage();
129135
}
130136

131137

@@ -159,6 +165,7 @@ void HeapProfiler::StopHeapObjectsTracking() {
159165
ids_->StopHeapObjectsTracking();
160166
if (allocation_tracker_) {
161167
allocation_tracker_.reset();
168+
MaybeClearStringsStorage();
162169
heap()->RemoveHeapObjectAllocationTracker(this);
163170
}
164171
}

src/profiler/heap-profiler.h

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class HeapProfiler : public HeapObjectAllocationTracker {
9292
v8::PersistentValueVector<v8::Object>* objects);
9393

9494
private:
95+
void MaybeClearStringsStorage();
96+
9597
Heap* heap() const;
9698

9799
// Mapping from HeapObject addresses to objects' uids.

test/cctest/test-heap-profiler.cc

+42
Original file line numberDiff line numberDiff line change
@@ -3920,3 +3920,45 @@ TEST(WeakReference) {
39203920
const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
39213921
CHECK(ValidateSnapshot(snapshot));
39223922
}
3923+
3924+
TEST(Bug8373_1) {
3925+
LocalContext env;
3926+
v8::HandleScope scope(env->GetIsolate());
3927+
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
3928+
3929+
heap_profiler->StartSamplingHeapProfiler(100);
3930+
3931+
heap_profiler->TakeHeapSnapshot();
3932+
// Causes the StringsStorage to be deleted.
3933+
heap_profiler->DeleteAllHeapSnapshots();
3934+
3935+
// Triggers an allocation sample that tries to use the StringsStorage.
3936+
for (int i = 0; i < 2 * 1024; ++i) {
3937+
CompileRun(
3938+
"new Array(64);"
3939+
"new Uint8Array(16);");
3940+
}
3941+
3942+
heap_profiler->StopSamplingHeapProfiler();
3943+
}
3944+
3945+
TEST(Bug8373_2) {
3946+
LocalContext env;
3947+
v8::HandleScope scope(env->GetIsolate());
3948+
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
3949+
3950+
heap_profiler->StartTrackingHeapObjects(true);
3951+
3952+
heap_profiler->TakeHeapSnapshot();
3953+
// Causes the StringsStorage to be deleted.
3954+
heap_profiler->DeleteAllHeapSnapshots();
3955+
3956+
// Triggers an allocations that try to use the StringsStorage.
3957+
for (int i = 0; i < 2 * 1024; ++i) {
3958+
CompileRun(
3959+
"new Array(64);"
3960+
"new Uint8Array(16);");
3961+
}
3962+
3963+
heap_profiler->StopTrackingHeapObjects();
3964+
}

0 commit comments

Comments
 (0)