Index: lib/Transforms/Instrumentation/SanitizerCoverage.cpp =================================================================== --- lib/Transforms/Instrumentation/SanitizerCoverage.cpp +++ lib/Transforms/Instrumentation/SanitizerCoverage.cpp @@ -382,9 +382,13 @@ {IRB.CreatePointerCast(SecStart, Int32PtrTy), IRB.CreatePointerCast(SecEnd, Int32PtrTy)}); - // Use comdat to dedup CtorFunc. - CtorFunc->setComdat(M.getOrInsertComdat(SanCovModuleCtorName)); - appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority, CtorFunc); + if (TargetTriple.supportsCOMDAT()) { + // Use comdat to dedup CtorFunc. + CtorFunc->setComdat(M.getOrInsertComdat(SanCovModuleCtorName)); + appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority, CtorFunc); + } else { + appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority); + } } } else if (!Options.TracePC) { Function *CtorFunc; Index: test/Instrumentation/SanitizerCoverage/coverage.ll =================================================================== --- test/Instrumentation/SanitizerCoverage/coverage.ll +++ test/Instrumentation/SanitizerCoverage/coverage.ll @@ -7,7 +7,6 @@ ; RUN: opt < %s -sancov -sanitizer-coverage-level=3 -sanitizer-coverage-block-threshold=10 -S | FileCheck %s --check-prefix=CHECK3 ; RUN: opt < %s -sancov -sanitizer-coverage-level=4 -S | FileCheck %s --check-prefix=CHECK4 ; RUN: opt < %s -sancov -sanitizer-coverage-level=4 -sanitizer-coverage-trace-pc -S | FileCheck %s --check-prefix=CHECK_TRACE_PC -; RUN: opt < %s -sancov -sanitizer-coverage-level=4 -sanitizer-coverage-trace-pc-guard -S | FileCheck %s --check-prefix=CHECK_TRACE_PC_GUARD ; RUN: opt < %s -sancov -sanitizer-coverage-level=3 -sanitizer-coverage-8bit-counters=1 -S | FileCheck %s --check-prefix=CHECK-8BIT ; RUN: opt < %s -sancov -sanitizer-coverage-level=2 -sanitizer-coverage-block-threshold=10 \ @@ -131,18 +130,6 @@ ; CHECK_TRACE_PC: call void @__sanitizer_cov_trace_pc_indir ; CHECK_TRACE_PC: ret void -; CHECK_TRACE_PC_GUARD-LABEL: define void @foo -; CHECK_TRACE_PC_GUARD: call void @__sanitizer_cov_trace_pc -; CHECK_TRACE_PC_GUARD: call void asm sideeffect "", ""() -; CHECK_TRACE_PC_GUARD: ret void - -; CHECK_TRACE_PC_GUARD-LABEL: define void @CallViaVptr -; CHECK_TRACE_PC_GUARD: call void @__sanitizer_cov_trace_pc_indir -; CHECK_TRACE_PC_GUARD: call void @__sanitizer_cov_trace_pc_indir -; CHECK_TRACE_PC_GUARD: ret void - -; CHECK_TRACE_PC_GUARD-LABEL: define internal void @sancov.module_ctor() comdat - define void @call_unreachable() uwtable sanitize_address { entry: unreachable Index: test/Instrumentation/SanitizerCoverage/trace-pc-guard-comdat.ll =================================================================== --- /dev/null +++ test/Instrumentation/SanitizerCoverage/trace-pc-guard-comdat.ll @@ -0,0 +1,42 @@ +; RUN: opt < %s -sancov -sanitizer-coverage-level=4 -sanitizer-coverage-trace-pc-guard -S | FileCheck %s --check-prefix=CHECK_TRACE_PC_GUARD + +target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64" +target triple = "x86_64-unknown-linux-gnu" +define void @foo(i32* %a) sanitize_address { +entry: + %tobool = icmp eq i32* %a, null + br i1 %tobool, label %if.end, label %if.then + + if.then: ; preds = %entry + store i32 0, i32* %a, align 4 + br label %if.end + + if.end: ; preds = %entry, %if.then + ret void +} + +%struct.StructWithVptr = type { i32 (...)** } + +define void @CallViaVptr(%struct.StructWithVptr* %foo) uwtable sanitize_address { +entry: + %0 = bitcast %struct.StructWithVptr* %foo to void (%struct.StructWithVptr*)*** + %vtable = load void (%struct.StructWithVptr*)**, void (%struct.StructWithVptr*)*** %0, align 8 + %1 = load void (%struct.StructWithVptr*)*, void (%struct.StructWithVptr*)** %vtable, align 8 + tail call void %1(%struct.StructWithVptr* %foo) + tail call void %1(%struct.StructWithVptr* %foo) + tail call void asm sideeffect "", ""() + ret void +} + +; CHECK_TRACE_PC_GUARD-LABEL: define void @foo +; CHECK_TRACE_PC_GUARD: call void @__sanitizer_cov_trace_pc +; CHECK_TRACE_PC_GUARD: call void asm sideeffect "", ""() +; CHECK_TRACE_PC_GUARD: ret void + +; CHECK_TRACE_PC_GUARD-LABEL: define void @CallViaVptr +; CHECK_TRACE_PC_GUARD: call void @__sanitizer_cov_trace_pc_indir +; CHECK_TRACE_PC_GUARD: call void @__sanitizer_cov_trace_pc_indir +; CHECK_TRACE_PC_GUARD: ret void + +; CHECK_TRACE_PC_GUARD-LABEL: define internal void @sancov.module_ctor() comdat + Index: test/Instrumentation/SanitizerCoverage/trace-pc-guard-nocomdat.ll =================================================================== --- /dev/null +++ test/Instrumentation/SanitizerCoverage/trace-pc-guard-nocomdat.ll @@ -0,0 +1,42 @@ +; RUN: opt < %s -sancov -sanitizer-coverage-level=4 -sanitizer-coverage-trace-pc-guard -S | FileCheck %s --check-prefix=CHECK_TRACE_PC_GUARD + +target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64" +target triple = "x86_64-apple-darwin10.0.0" +define void @foo(i32* %a) sanitize_address { +entry: + %tobool = icmp eq i32* %a, null + br i1 %tobool, label %if.end, label %if.then + + if.then: ; preds = %entry + store i32 0, i32* %a, align 4 + br label %if.end + + if.end: ; preds = %entry, %if.then + ret void +} + +%struct.StructWithVptr = type { i32 (...)** } + +define void @CallViaVptr(%struct.StructWithVptr* %foo) uwtable sanitize_address { +entry: + %0 = bitcast %struct.StructWithVptr* %foo to void (%struct.StructWithVptr*)*** + %vtable = load void (%struct.StructWithVptr*)**, void (%struct.StructWithVptr*)*** %0, align 8 + %1 = load void (%struct.StructWithVptr*)*, void (%struct.StructWithVptr*)** %vtable, align 8 + tail call void %1(%struct.StructWithVptr* %foo) + tail call void %1(%struct.StructWithVptr* %foo) + tail call void asm sideeffect "", ""() + ret void +} + +; CHECK_TRACE_PC_GUARD-LABEL: define void @foo +; CHECK_TRACE_PC_GUARD: call void @__sanitizer_cov_trace_pc +; CHECK_TRACE_PC_GUARD: call void asm sideeffect "", ""() +; CHECK_TRACE_PC_GUARD: ret void + +; CHECK_TRACE_PC_GUARD-LABEL: define void @CallViaVptr +; CHECK_TRACE_PC_GUARD: call void @__sanitizer_cov_trace_pc_indir +; CHECK_TRACE_PC_GUARD: call void @__sanitizer_cov_trace_pc_indir +; CHECK_TRACE_PC_GUARD: ret void + +; CHECK_TRACE_PC_GUARD-LABEL: define internal void @sancov.module_ctor() { +