From 611c402e59ea15206a0f0c548cb7bf4b0373b7c4 Mon Sep 17 00:00:00 2001 From: Elena Lepilkina Date: Thu, 9 Dec 2021 11:26:26 +0300 Subject: [PATCH] [K/N] Fix building mimalloc with TSAN --- .../kotlin/bitcode/CompileToBitcode.kt | 18 ++++++++++++++---- kotlin-native/runtime/build.gradle.kts | 1 + 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/bitcode/CompileToBitcode.kt b/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/bitcode/CompileToBitcode.kt index 347ff2fbea3..e01fc5ece37 100644 --- a/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/bitcode/CompileToBitcode.kt +++ b/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/bitcode/CompileToBitcode.kt @@ -102,6 +102,9 @@ abstract class CompileToBitcode @Inject constructor( @Input @Optional var sanitizer: SanitizerKind? = null + @Input @Optional + val extraSanitizerArgs = mutableMapOf>() + private val targetDir: File get() { val sanitizerSuffix = when (sanitizer) { @@ -136,11 +139,18 @@ abstract class CompileToBitcode @Inject constructor( null -> listOf() SanitizerKind.ADDRESS -> listOf("-fsanitize=address") SanitizerKind.THREAD -> listOf("-fsanitize=thread") - } + } + (extraSanitizerArgs[sanitizer] ?: emptyList()) val languageFlags = when (language) { - Language.C -> - // Used flags provided by original build of allocator C code. - listOf("-std=gnu11", "-O3", "-Wall", "-Wextra", "-Werror") + Language.C -> { + listOf("-std=gnu11", "-Wall", "-Wextra", "-Werror") + + if (sanitizer != SanitizerKind.THREAD) { + // Used flags provided by original build of allocator C code. + listOf("-O3") + } else { + // Building with TSAN needs turning off extra optimizations. + listOf("-O1") + } + } Language.CPP -> listOfNotNull("-std=c++17", "-Werror", "-O2", "-fno-aligned-allocation", // TODO: Remove when all targets support aligned allocation in C++ runtime. diff --git a/kotlin-native/runtime/build.gradle.kts b/kotlin-native/runtime/build.gradle.kts index 76b5699f680..7ac4db3a3b2 100644 --- a/kotlin-native/runtime/build.gradle.kts +++ b/kotlin-native/runtime/build.gradle.kts @@ -77,6 +77,7 @@ bitcode { compilerArgs.addAll(listOf("-DKONAN_MI_MALLOC=1", "-Wno-unknown-pragmas", "-ftls-model=initial-exec", "-Wno-unused-function", "-Wno-error=atomic-alignment", "-Wno-unused-parameter" /* for windows 32*/)) + extraSanitizerArgs[SanitizerKind.THREAD] = listOf("-DMI_TSAN=1") headersDirs = files("$srcRoot/c/include") onlyIf { targetSupportsMimallocAllocator(target) }