From c04e1b6ce705c74b30821804824457d02d1a4538 Mon Sep 17 00:00:00 2001 From: Pavel Kunyavskiy Date: Tue, 15 Nov 2022 11:34:00 +0100 Subject: [PATCH] [K/N] Better fix for calls checker use-after-free in main thread deinit --- kotlin-native/runtime/src/mm/cpp/Memory.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/kotlin-native/runtime/src/mm/cpp/Memory.cpp b/kotlin-native/runtime/src/mm/cpp/Memory.cpp index 23620106cac..b5af13dbe83 100644 --- a/kotlin-native/runtime/src/mm/cpp/Memory.cpp +++ b/kotlin-native/runtime/src/mm/cpp/Memory.cpp @@ -113,12 +113,12 @@ extern "C" void DeinitMemory(MemoryState* state, bool destroyRuntime) { AssertThreadState(state, ThreadState::kNative); auto* node = mm::FromMemoryState(state); if (destroyRuntime) { - { - ThreadStateGuard guard(state, ThreadState::kRunnable); - node->Get()->gc().ScheduleAndWaitFullGCWithFinalizers(); - // TODO: Why not just destruct `GC` object and its thread data counterpart entirely? - mm::GlobalData::Instance().gc().StopFinalizerThreadIfRunning(); - } + ThreadStateGuard guard(state, ThreadState::kRunnable); + node->Get()->gc().ScheduleAndWaitFullGCWithFinalizers(); + // TODO: Why not just destruct `GC` object and its thread data counterpart entirely? + mm::GlobalData::Instance().gc().StopFinalizerThreadIfRunning(); + } + if (!konan::isOnThreadExitNotSetOrAlreadyStarted()) { // we can clear reference in advance, as Unregister function can't use it anyway mm::ThreadRegistry::ClearCurrentThreadData(); }