From fc7cf48798c083c17a904c23ba12c033b208e16f Mon Sep 17 00:00:00 2001 From: Alexander Shabalin Date: Tue, 3 Nov 2020 15:49:27 +0300 Subject: [PATCH] Tests on migrating main thread and memory leaks with interop (#4494) --- .../backend.native/tests/build.gradle | 19 ++++++++++++++ .../tests/interop/memory_leaks/lib.kt | 10 +++++++ .../tests/interop/memory_leaks/main.cpp | 16 ++++++++++++ .../interop/migrating_main_thread/lib.kt | 16 ++++++++++++ .../interop/migrating_main_thread/main.cpp | 26 +++++++++++++++++++ 5 files changed, 87 insertions(+) create mode 100644 kotlin-native/backend.native/tests/interop/memory_leaks/lib.kt create mode 100644 kotlin-native/backend.native/tests/interop/memory_leaks/main.cpp create mode 100644 kotlin-native/backend.native/tests/interop/migrating_main_thread/lib.kt create mode 100644 kotlin-native/backend.native/tests/interop/migrating_main_thread/main.cpp diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index 7ed4bb224ef..1ee29e149fe 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -4343,6 +4343,25 @@ dynamicTest("interop_cleaners_leak") { flags = ['-Xopt-in=kotlin.native.internal.InternalForKotlinNative'] } +dynamicTest("interop_migrating_main_thread") { + // TODO: Fix it. + disabled = (project.testTarget != null && project.testTarget != project.hostName) + source = "interop/migrating_main_thread/lib.kt" + cSource = "$projectDir/interop/migrating_main_thread/main.cpp" + clangTool = "clang++" +} + +dynamicTest("interop_memory_leaks") { + // TODO: Fix it. + disabled = (project.testTarget != null && project.testTarget != project.hostName) || project.globalTestArgs.contains('-opt') || (project.testTarget == 'wasm32') // Needs debug build. + source = "interop/memory_leaks/lib.kt" + cSource = "$projectDir/interop/memory_leaks/main.cpp" + clangTool = "clang++" + flags = ['-g'] + expectedExitStatusChecker = { it != 0 } + outputChecker = { s -> s.contains("Memory leaks detected, 1 objects leaked!") } +} + task library_mismatch(type: KonanDriverTest) { // Does not work for cross targets yet. enabled = !(project.testTarget != null && project.target.name != project.hostName) diff --git a/kotlin-native/backend.native/tests/interop/memory_leaks/lib.kt b/kotlin-native/backend.native/tests/interop/memory_leaks/lib.kt new file mode 100644 index 00000000000..cfab7ec7202 --- /dev/null +++ b/kotlin-native/backend.native/tests/interop/memory_leaks/lib.kt @@ -0,0 +1,10 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +import kotlinx.cinterop.* + +fun leakMemory() { + StableRef.create(Any()) +} diff --git a/kotlin-native/backend.native/tests/interop/memory_leaks/main.cpp b/kotlin-native/backend.native/tests/interop/memory_leaks/main.cpp new file mode 100644 index 00000000000..ee9bbfc3613 --- /dev/null +++ b/kotlin-native/backend.native/tests/interop/memory_leaks/main.cpp @@ -0,0 +1,16 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +#include "testlib_api.h" + +#include + +int main() { + std::thread t([]() { + testlib_symbols()->kotlin.root.leakMemory(); + }); + t.join(); + return 0; +} diff --git a/kotlin-native/backend.native/tests/interop/migrating_main_thread/lib.kt b/kotlin-native/backend.native/tests/interop/migrating_main_thread/lib.kt new file mode 100644 index 00000000000..afbe1a9f296 --- /dev/null +++ b/kotlin-native/backend.native/tests/interop/migrating_main_thread/lib.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +class A { + var i = 0 +} + +private var globalA = A() + +fun writeToA(i: Int) { + globalA.i = i +} + +fun readFromA() = globalA.i diff --git a/kotlin-native/backend.native/tests/interop/migrating_main_thread/main.cpp b/kotlin-native/backend.native/tests/interop/migrating_main_thread/main.cpp new file mode 100644 index 00000000000..40cb53a1a98 --- /dev/null +++ b/kotlin-native/backend.native/tests/interop/migrating_main_thread/main.cpp @@ -0,0 +1,26 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +#include "testlib_api.h" + +#include +#include + +int main() { + std::thread main1([]() { + assert(testlib_symbols()->kotlin.root.readFromA() == 0); + testlib_symbols()->kotlin.root.writeToA(1); + assert(testlib_symbols()->kotlin.root.readFromA() == 1); + }); + main1.join(); + + std::thread main2([]() { + // Globals were reinitialized. + assert(testlib_symbols()->kotlin.root.readFromA() == 0); + }); + main2.join(); + + return 0; +}