diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index b664c2d20cd..18bbc95fe38 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -1239,6 +1239,14 @@ if (PlatformInfo.isAppleTarget(project)) { it.headers "$projectDir/interop/objc/overridabilityCondition/lib.h" it.extraOpts "-Xcompile-source", "$projectDir/interop/objc/overridabilityCondition/lib.m" } + createInterop("kt63423_dispose_on_main_stress") { + it.defFile 'interop/objc/kt63423_dispose_on_main_stress/objclib.def' + it.headers "$projectDir/interop/objc/kt63423_dispose_on_main_stress/objclib.h" + it.extraOpts "-Xcompile-source", "$projectDir/interop/objc/kt63423_dispose_on_main_stress/objclib.m" + it.extraOpts "-Xsource-compiler-option", "-DNS_FORMAT_ARGUMENT(A)=" + it.extraOpts "-Xsource-compiler-option", "-fobjc-arc" + it.extraOpts "-Xsource-compiler-option", "-ObjC++" + } } createInterop("withSpaces") { @@ -1848,6 +1856,18 @@ if (PlatformInfo.isAppleTarget(project)) { source = 'interop/kt59167/main.kt' interop = 'kt59167' } + + interopTest("interop_objc_kt63423_dispose_on_main_stress") { + // Test depends on macOS-specific AppKit + enabled = (project.testTarget == 'macos_x64' || project.testTarget == 'macos_arm64' || project.testTarget == null) + && !isNoopGC // requires some GC + source = 'interop/objc/kt63423_dispose_on_main_stress/main.kt' + interop = "kt63423_dispose_on_main_stress" + flags = [ + '-opt-in=kotlin.native.internal.InternalForKotlinNative', // MemoryUsageInfo is internal + '-Xbinary=gcSchedulerType=manual', // This test requires very careful timing when GC can happen + ] + } } tasks.register("KT-50983", KonanDriverTest) { diff --git a/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/main.kt b/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/main.kt new file mode 100644 index 00000000000..97449917fb1 --- /dev/null +++ b/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/main.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ +@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class) + +import objclib.* + +import kotlin.native.internal.MemoryUsageInfo +import kotlin.test.* +import kotlinx.cinterop.* + +class PeakRSSChecker(private val rssDiffLimitBytes: Long) { + // On Linux, the child process might immediately commit the same amount of memory as the parent. + // So, measure difference between peak RSS measurements. + private val initialBytes = MemoryUsageInfo.peakResidentSetSizeBytes.also { + check(it != 0L) { "Error trying to obtain peak RSS. Check if current platform is supported" } + } + + fun check(): Long { + val diffBytes = MemoryUsageInfo.peakResidentSetSizeBytes - initialBytes + check(diffBytes <= rssDiffLimitBytes) { "Increased peak RSS by $diffBytes bytes which is more than $rssDiffLimitBytes" } + return diffBytes + } +} + +fun alloc(): Unit = autoreleasepool { + OnDestroyHook() + Unit +} + +fun waitDestruction() { + assertTrue(isMainThread()) + kotlin.native.internal.GC.collect() + spin() +} + +fun main() = startApp { + repeat(500000) { + alloc() + } + val peakRSSChecker = PeakRSSChecker(10_000_000L) // ~10MiB allowed difference for running finalizers + waitDestruction() + peakRSSChecker.check() +} \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.def b/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.def new file mode 100644 index 00000000000..e6ac22942a9 --- /dev/null +++ b/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.def @@ -0,0 +1,3 @@ +language = Objective-C +headerFilter = **/objclib.h +linkerOpts = -framework AppKit \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.h b/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.h new file mode 100644 index 00000000000..d2bacdc8b1d --- /dev/null +++ b/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.h @@ -0,0 +1,17 @@ +#include + +@interface OnDestroyHook : NSObject +- (instancetype)init; +@end + +#ifdef __cplusplus +extern "C" { +#endif + +void startApp(void (^task)()); +BOOL isMainThread(); +void spin(); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.m b/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.m new file mode 100644 index 00000000000..0b6f1c6d405 --- /dev/null +++ b/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.m @@ -0,0 +1,58 @@ +#include "objclib.h" + +#include +#include +#include +#import +#import +#import + +std::map dictionary; + +@implementation OnDestroyHook +- (instancetype)init { + if (self = [super init]) { + dictionary[(uintptr_t)self] = true; + } + return self; +} + +- (void)dealloc { + dictionary[(uintptr_t)self] = false; +} + +@end + +extern "C" void startApp(void (^task)()) { + dispatch_async(dispatch_get_main_queue(), ^{ + // At this point all other scheduled main queue tasks were already executed. + // Executing via performBlock to allow a recursive run loop in `spin()`. + [[NSRunLoop currentRunLoop] performBlock:^{ + task(); + [NSApp terminate:NULL]; + }]; + }); + [[NSApplication sharedApplication] run]; +} + +extern "C" BOOL isMainThread() { + return [NSThread isMainThread]; +} + +extern "C" void spin() { + if ([NSRunLoop currentRunLoop] != [NSRunLoop mainRunLoop]) { + fprintf(stderr, "Must spin main run loop\n"); + exit(1); + } + while (true) { + [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; + bool done = true; + for (auto kvp : dictionary) { + if (kvp.second) { + done = false; + break; + } + } + if (done) return; + } +}