From 35ff131608bd7e04420f7be70abbe472dc99c140 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Wed, 15 Sep 2021 20:15:00 +0700 Subject: [PATCH] fixup! fixup! [K/N][New MM] Disallow getting thread data for detached threads --- kotlin-native/runtime/src/legacymm/cpp/Memory.cpp | 1 - kotlin-native/runtime/src/main/cpp/Memory.h | 3 ++- kotlin-native/runtime/src/main/cpp/MemoryTest.cpp | 6 +++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp b/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp index 7b21bed9ddb..dee4d9cc378 100644 --- a/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp +++ b/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp @@ -3739,7 +3739,6 @@ ALWAYS_INLINE void kotlin::AssertThreadState(MemoryState* thread, std::initializ } MemoryState* kotlin::mm::GetMemoryState() noexcept { - RuntimeAssert(memoryState != nullptr, "Thread is not attached to the runtime"); return ::memoryState; } diff --git a/kotlin-native/runtime/src/main/cpp/Memory.h b/kotlin-native/runtime/src/main/cpp/Memory.h index bd4667532fd..215aa55ecfe 100644 --- a/kotlin-native/runtime/src/main/cpp/Memory.h +++ b/kotlin-native/runtime/src/main/cpp/Memory.h @@ -389,7 +389,8 @@ namespace kotlin { namespace mm { // Returns the MemoryState for the current thread. -// The current thread must be attached to the runtime. +// For the new MM, the current thread must be attached to the runtime. +// For the legacy MM, returns nullptr if called on a thread that is not attached to the runtime. // Try not to use it very often, as (1) thread local access can be slow on some platforms, // (2) TLS gets deallocated before our thread destruction hooks run. MemoryState* GetMemoryState() noexcept; diff --git a/kotlin-native/runtime/src/main/cpp/MemoryTest.cpp b/kotlin-native/runtime/src/main/cpp/MemoryTest.cpp index d275aa690de..332ab2ad099 100644 --- a/kotlin-native/runtime/src/main/cpp/MemoryTest.cpp +++ b/kotlin-native/runtime/src/main/cpp/MemoryTest.cpp @@ -12,7 +12,11 @@ using namespace kotlin; TEST(MemoryStateTestDeathTest, GetMemoryStateForUnregisteredThread) { - EXPECT_DEATH(mm::GetMemoryState(), "Thread is not attached to the runtime"); + if (CurrentMemoryModel == MemoryModel::kExperimental) { + EXPECT_DEATH(mm::GetMemoryState(), "Thread is not attached to the runtime"); + } else { + EXPECT_EQ(mm::GetMemoryState(), nullptr); + } } TEST(MemoryStateTest, GetMemoryStateForRegisteredThread) {