diff --git a/build-logic/conventions/src/main/kotlin/com/datadoghq/native/config/ConfigurationPresets.kt b/build-logic/conventions/src/main/kotlin/com/datadoghq/native/config/ConfigurationPresets.kt index 3f91ca36c2..681f76a1bc 100644 --- a/build-logic/conventions/src/main/kotlin/com/datadoghq/native/config/ConfigurationPresets.kt +++ b/build-logic/conventions/src/main/kotlin/com/datadoghq/native/config/ConfigurationPresets.kt @@ -51,7 +51,7 @@ object ConfigurationPresets { configureTsan(this, currentPlatform, currentArch, version, rootDir, compiler) } register("fuzzer") { - configureFuzzer(this, currentPlatform, currentArch, version, rootDir) + configureFuzzer(this, currentPlatform, currentArch, version, rootDir, compiler) } } @@ -313,7 +313,8 @@ object ConfigurationPresets { platform: Platform, architecture: Architecture, version: String, - rootDir: File + rootDir: File, + compiler: String = "gcc" ) { config.platform.set(platform) config.architecture.set(architecture) @@ -339,7 +340,28 @@ object ConfigurationPresets { when (platform) { Platform.LINUX -> { config.compilerArgs.set(fuzzerCompilerArgs + commonLinuxCompilerArgs(version)) - config.linkerArgs.set(commonLinuxLinkerArgs() + fuzzerLinkerArgs) + + // commonLinuxLinkerArgs() carries -Wl,-z,defs, which forbids the + // undefined __asan_*/__ubsan_* symbols the instrumented objects + // reference. -fsanitize=address only links the runtime into + // executables, not shared libraries, so the fuzzer .so needs the + // runtime linked explicitly — same treatment (and rationale) as the + // asan config. On clang this resolves both __asan_* and __ubsan_* + // from one clang_rt.asan runtime; on gcc it adds -lubsan. + val libasan = PlatformUtils.locateLibasan(compiler) + val fuzzerRuntimeArgs = if (libasan != null) { + val asanLibDir = File(libasan).parent + val asanLibName = File(libasan).nameWithoutExtension.removePrefix("lib") + val ubsanLibs = if (asanLibName.startsWith("clang_rt")) emptyList() + else listOf("-lubsan") + listOf("-L$asanLibDir", "-l$asanLibName", + "-Wl,-rpath,$asanLibDir") + + ubsanLibs + + fuzzerLinkerArgs + } else { + fuzzerLinkerArgs + } + config.linkerArgs.set(commonLinuxLinkerArgs() + fuzzerRuntimeArgs) config.testEnvironment.apply { put("ASAN_OPTIONS", "allocator_may_return_null=1:detect_stack_use_after_return=0:handle_segv=0:abort_on_error=1:symbolize=1:suppressions=$rootDir/gradle/sanitizers/asan.supp")