From 3402a1a4d2d4c7ead69156c3d741fc9ae9c4d399 Mon Sep 17 00:00:00 2001 From: Alexander Shaposhnikov Date: Tue, 2 Jul 2024 16:57:30 -0700 Subject: [PATCH] [Clang] Enable nsan instrumentation pass (#97359) Enable nsan instrumentation pass --- clang/lib/CodeGen/BackendUtil.cpp | 4 ++++ clang/test/CodeGen/no-skipped-passes-O0-opt-bisect.c | 1 + clang/test/CodeGen/nsan-basic.c | 7 +++++++ clang/test/CodeGen/sanitizer-module-constructor.c | 1 + 4 files changed, 13 insertions(+) create mode 100644 clang/test/CodeGen/nsan-basic.c diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 22b593e8f2b7a2..4195bb87cf0ddb 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -76,6 +76,7 @@ #include "llvm/Transforms/Instrumentation/LowerAllowCheckPass.h" #include "llvm/Transforms/Instrumentation/MemProfiler.h" #include "llvm/Transforms/Instrumentation/MemorySanitizer.h" +#include "llvm/Transforms/Instrumentation/NumericalStabilitySanitizer.h" #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h" #include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h" #include "llvm/Transforms/Instrumentation/SanitizerCoverage.h" @@ -707,6 +708,9 @@ static void addSanitizers(const Triple &TargetTriple, MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass())); } + if (LangOpts.Sanitize.has(SanitizerKind::NumericalStability)) + MPM.addPass(NumericalStabilitySanitizerPass()); + auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) { if (LangOpts.Sanitize.has(Mask)) { bool UseGlobalGC = asanUseGlobalsGC(TargetTriple, CodeGenOpts); diff --git a/clang/test/CodeGen/no-skipped-passes-O0-opt-bisect.c b/clang/test/CodeGen/no-skipped-passes-O0-opt-bisect.c index 381803a00ede78..6d3e6359d37c77 100644 --- a/clang/test/CodeGen/no-skipped-passes-O0-opt-bisect.c +++ b/clang/test/CodeGen/no-skipped-passes-O0-opt-bisect.c @@ -9,6 +9,7 @@ // RUN: %clang_cc1 -triple x86_64-linux-gnu -O0 %s -fdebug-pass-manager -emit-llvm -o /dev/null -fsanitize=local-bounds 2>&1 | FileCheck %s // RUN: %clang_cc1 -triple x86_64-linux-gnu -O0 %s -fdebug-pass-manager -emit-llvm -o /dev/null -fsanitize=dataflow 2>&1 | FileCheck %s // RUN: %clang_cc1 -triple x86_64-linux-gnu -O0 %s -fdebug-pass-manager -emit-llvm -o /dev/null -fsanitize-coverage-trace-pc-guard 2>&1 | FileCheck %s +// RUN: %clang_cc1 -triple x86_64-linux-gnu -O0 %s -fdebug-pass-manager -emit-llvm -o /dev/null -fsanitize=numerical 2>&1 | FileCheck %s // RUN: %clang_cc1 -triple x86_64-linux-gnu -O0 %s -fdebug-pass-manager -emit-llvm -o /dev/null -fmemory-profile 2>&1 | FileCheck %s // RUN: %clang_cc1 -triple x86_64-linux-gnu -mllvm -opt-bisect-limit=0 %s -fdebug-pass-manager -emit-llvm -o /dev/null 2>&1 | FileCheck %s diff --git a/clang/test/CodeGen/nsan-basic.c b/clang/test/CodeGen/nsan-basic.c new file mode 100644 index 00000000000000..7aedaa38d881de --- /dev/null +++ b/clang/test/CodeGen/nsan-basic.c @@ -0,0 +1,7 @@ +// RUN: %clang_cc1 -triple x86_64 -emit-llvm -o - -fsanitize=numerical %s | FileCheck %s + +// CHECK: Function Attrs: noinline nounwind optnone sanitize_numerical_stability +float add(float x, float y) { + float z = x + y; + return z; +} diff --git a/clang/test/CodeGen/sanitizer-module-constructor.c b/clang/test/CodeGen/sanitizer-module-constructor.c index e4d08cde2620af..06dc57304a7e1e 100644 --- a/clang/test/CodeGen/sanitizer-module-constructor.c +++ b/clang/test/CodeGen/sanitizer-module-constructor.c @@ -1,6 +1,7 @@ // RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=address -O3 -emit-llvm -fdebug-pass-manager -o - %s 2>&1 | FileCheck %s // RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=thread -O3 -emit-llvm -fdebug-pass-manager -o - %s 2>&1 | FileCheck %s // RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=memory -O3 -emit-llvm -fdebug-pass-manager -o - %s 2>&1 | FileCheck %s +// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=numerical -O3 -emit-llvm -fdebug-pass-manager -o - %s 2>&1 | FileCheck %s // This is regression test for PR42877