[vm] Fix -Werror=use-after-free in isolate shutdown.

TEST=gcc 12
Change-Id: Ia4aba7b35ee12677799a9aa289a250ae7035ba6d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/259140
Reviewed-by: Siva Annamalai <asiva@google.com>
Commit-Queue: Ryan Macnak <rmacnak@google.com>
This commit is contained in:
Ryan Macnak 2022-09-21 17:52:01 +00:00 committed by Commit Bot
parent a0b9951a3e
commit 1bbb01211d
4 changed files with 12 additions and 5 deletions

View file

@ -13,6 +13,8 @@
#error Do not include assembler_ia32.h directly; use assembler.h instead.
#endif
#include <functional>
#include "platform/assert.h"
#include "platform/utils.h"
#include "vm/compiler/assembler/assembler_base.h"

View file

@ -469,7 +469,7 @@ void IsolateGroup::UnregisterIsolate(Isolate* isolate) {
}
}
bool IsolateGroup::UnregisterIsolateDecrementCount(Isolate* isolate) {
bool IsolateGroup::UnregisterIsolateDecrementCount() {
SafepointWriteRwLocker ml(Thread::Current(), isolates_lock_.get());
isolate_count_--;
return isolate_count_ == 0;
@ -2706,8 +2706,7 @@ void Isolate::LowLevelCleanup(Isolate* isolate) {
}
}
const bool shutdown_group =
isolate_group->UnregisterIsolateDecrementCount(isolate);
const bool shutdown_group = isolate_group->UnregisterIsolateDecrementCount();
if (shutdown_group) {
KernelIsolate::NotifyAboutIsolateGroupShutdown(isolate_group);

View file

@ -336,7 +336,7 @@ class IsolateGroup : public IntrusiveDListEntry<IsolateGroup> {
void UnregisterIsolate(Isolate* isolate);
// Returns `true` if this was the last isolate and the caller is responsible
// for deleting the isolate group.
bool UnregisterIsolateDecrementCount(Isolate* isolate);
bool UnregisterIsolateDecrementCount();
bool ContainsOnlyOneIsolate();

View file

@ -112,6 +112,11 @@ UNIT_TEST_CASE(FreeUnseenMemoryMallocHookTest) {
EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes());
}
DART_NOINLINE
static void* IgnoreUseAfterFree(void* x) {
return x;
}
VM_UNIT_TEST_CASE(StackTraceMallocHookSimpleTest) {
EnableMallocHooksAndStacksScope scope;
@ -119,8 +124,9 @@ VM_UNIT_TEST_CASE(StackTraceMallocHookSimpleTest) {
Sample* sample = MallocHooks::GetSample(var);
EXPECT(sample != NULL);
void* lookup_var = IgnoreUseAfterFree(var);
free(var);
sample = MallocHooks::GetSample(var);
sample = MallocHooks::GetSample(lookup_var);
EXPECT(sample == NULL);
}