Skip to content

Commit f932900

Browse files
Add numerical sanitizer
1 parent 9fd1c41 commit f932900

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+7863
-4
lines changed

clang/include/clang/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ FEATURE(thread_sanitizer, LangOpts.Sanitize.has(SanitizerKind::Thread))
102102
FEATURE(dataflow_sanitizer, LangOpts.Sanitize.has(SanitizerKind::DataFlow))
103103
FEATURE(scudo, LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))
104104
FEATURE(ptrauth_intrinsics, LangOpts.PointerAuthIntrinsics)
105+
FEATURE(numericalstability_sanitizer, LangOpts.Sanitize.has(SanitizerKind::NumericalStability))
105106
FEATURE(swiftasynccc,
106107
PP.getTargetInfo().checkCallingConvention(CC_SwiftAsync) ==
107108
clang::TargetInfo::CCCR_OK)

clang/include/clang/Basic/Sanitizers.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ SANITIZER("fuzzer-no-link", FuzzerNoLink)
7676
// ThreadSanitizer
7777
SANITIZER("thread", Thread)
7878

79+
// Numerical stability sanitizer.
80+
SANITIZER("numerical", NumericalStability)
81+
7982
// LeakSanitizer
8083
SANITIZER("leak", Leak)
8184

clang/include/clang/Driver/SanitizerArgs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ class SanitizerArgs {
103103
bool needsCfiDiagRt() const;
104104
bool needsStatsRt() const { return Stats; }
105105
bool needsScudoRt() const { return Sanitizers.has(SanitizerKind::Scudo); }
106+
bool needsNsanRt() const {
107+
return Sanitizers.has(SanitizerKind::NumericalStability);
108+
}
106109

107110
bool hasMemTag() const {
108111
return hasMemtagHeap() || hasMemtagStack() || hasMemtagGlobals();

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#include "llvm/Transforms/Instrumentation/KCFI.h"
7676
#include "llvm/Transforms/Instrumentation/MemProfiler.h"
7777
#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
78+
#include "llvm/Transforms/Instrumentation/NumericalStabilitySanitizer.h"
7879
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
7980
#include "llvm/Transforms/Instrumentation/RemoveTrapsPass.h"
8081
#include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h"
@@ -734,6 +735,12 @@ static void addSanitizers(const Triple &TargetTriple,
734735
if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
735736
MPM.addPass(DataFlowSanitizerPass(LangOpts.NoSanitizeFiles));
736737
}
738+
739+
if (LangOpts.Sanitize.has(SanitizerKind::NumericalStability)) {
740+
MPM.addPass(NumericalStabilitySanitizerPass());
741+
MPM.addPass(
742+
createModuleToFunctionPassAdaptor(NumericalStabilitySanitizerPass()));
743+
}
737744
};
738745
if (ClSanitizeOnOptimizerEarlyEP) {
739746
PB.registerOptimizerEarlyEPCallback(

clang/lib/CodeGen/CGDeclCXX.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@ llvm::Function *CodeGenModule::CreateGlobalInitOrCleanUpFunction(
476476
!isInNoSanitizeList(SanitizerKind::Thread, Fn, Loc))
477477
Fn->addFnAttr(llvm::Attribute::SanitizeThread);
478478

479+
if (getLangOpts().Sanitize.has(SanitizerKind::NumericalStability) &&
480+
!isInNoSanitizeList(SanitizerKind::NumericalStability, Fn, Loc))
481+
Fn->addFnAttr(llvm::Attribute::SanitizeNumericalStability);
482+
479483
if (getLangOpts().Sanitize.has(SanitizerKind::Memory) &&
480484
!isInNoSanitizeList(SanitizerKind::Memory, Fn, Loc))
481485
Fn->addFnAttr(llvm::Attribute::SanitizeMemory);

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,8 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
795795
Fn->addFnAttr(llvm::Attribute::SanitizeMemTag);
796796
if (SanOpts.has(SanitizerKind::Thread))
797797
Fn->addFnAttr(llvm::Attribute::SanitizeThread);
798+
if (SanOpts.has(SanitizerKind::NumericalStability))
799+
Fn->addFnAttr(llvm::Attribute::SanitizeNumericalStability);
798800
if (SanOpts.hasOneOf(SanitizerKind::Memory | SanitizerKind::KernelMemory))
799801
Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
800802
}

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ static const SanitizerMask NotAllowedWithExecuteOnly =
4141
SanitizerKind::Function | SanitizerKind::KCFI;
4242
static const SanitizerMask NeedsUnwindTables =
4343
SanitizerKind::Address | SanitizerKind::HWAddress | SanitizerKind::Thread |
44-
SanitizerKind::Memory | SanitizerKind::DataFlow;
44+
SanitizerKind::Memory | SanitizerKind::DataFlow |
45+
SanitizerKind::NumericalStability;
4546
static const SanitizerMask SupportsCoverage =
4647
SanitizerKind::Address | SanitizerKind::HWAddress |
4748
SanitizerKind::KernelAddress | SanitizerKind::KernelHWAddress |
@@ -53,7 +54,8 @@ static const SanitizerMask SupportsCoverage =
5354
SanitizerKind::DataFlow | SanitizerKind::Fuzzer |
5455
SanitizerKind::FuzzerNoLink | SanitizerKind::FloatDivideByZero |
5556
SanitizerKind::SafeStack | SanitizerKind::ShadowCallStack |
56-
SanitizerKind::Thread | SanitizerKind::ObjCCast | SanitizerKind::KCFI;
57+
SanitizerKind::Thread | SanitizerKind::ObjCCast | SanitizerKind::KCFI |
58+
SanitizerKind::NumericalStability;
5759
static const SanitizerMask RecoverableByDefault =
5860
SanitizerKind::Undefined | SanitizerKind::Integer |
5961
SanitizerKind::ImplicitConversion | SanitizerKind::Nullability |
@@ -175,6 +177,7 @@ static void addDefaultIgnorelists(const Driver &D, SanitizerMask Kinds,
175177
{"hwasan_ignorelist.txt", SanitizerKind::HWAddress},
176178
{"memtag_ignorelist.txt", SanitizerKind::MemTag},
177179
{"msan_ignorelist.txt", SanitizerKind::Memory},
180+
{"nsan_ignorelist.txt", SanitizerKind::NumericalStability},
178181
{"tsan_ignorelist.txt", SanitizerKind::Thread},
179182
{"dfsan_abilist.txt", SanitizerKind::DataFlow},
180183
{"cfi_ignorelist.txt", SanitizerKind::CFI},

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,11 +1526,14 @@ collectSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
15261526
if (SanArgs.linkCXXRuntimes())
15271527
StaticRuntimes.push_back("msan_cxx");
15281528
}
1529+
if (SanArgs.needsNsanRt())
1530+
StaticRuntimes.push_back("nsan");
15291531
if (!SanArgs.needsSharedRt() && SanArgs.needsTsanRt()) {
15301532
StaticRuntimes.push_back("tsan");
15311533
if (SanArgs.linkCXXRuntimes())
15321534
StaticRuntimes.push_back("tsan_cxx");
15331535
}
1536+
15341537
if (!SanArgs.needsSharedRt() && SanArgs.needsUbsanRt()) {
15351538
if (SanArgs.requiresMinimalRuntime()) {
15361539
StaticRuntimes.push_back("ubsan_minimal");

clang/lib/Driver/ToolChains/Linux.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,10 @@ SanitizerMask Linux::getSupportedSanitizers() const {
825825
if (IsX86_64 || IsAArch64) {
826826
Res |= SanitizerKind::KernelHWAddress;
827827
}
828+
if (IsX86_64) {
829+
Res |= SanitizerKind::NumericalStability;
830+
}
831+
828832
// Work around "Cannot represent a difference across sections".
829833
if (getTriple().getArch() == llvm::Triple::ppc64)
830834
Res &= ~SanitizerKind::Function;

clang/runtime/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ if(LLVM_BUILD_EXTERNAL_COMPILER_RT AND EXISTS ${COMPILER_RT_SRC_ROOT}/)
122122
COMPONENT compiler-rt)
123123

124124
# Add top-level targets that build specific compiler-rt runtimes.
125-
set(COMPILER_RT_RUNTIMES fuzzer asan builtins dfsan lsan msan profile tsan ubsan ubsan-minimal)
125+
set(COMPILER_RT_RUNTIMES fuzzer asan builtins dfsan lsan msan nsan profile tsan ubsan ubsan-minimal)
126126
foreach(runtime ${COMPILER_RT_RUNTIMES})
127127
get_ext_project_build_command(build_runtime_cmd ${runtime})
128128
add_custom_target(${runtime}
@@ -149,6 +149,7 @@ if(LLVM_BUILD_EXTERNAL_COMPILER_RT AND EXISTS ${COMPILER_RT_SRC_ROOT}/)
149149
check-hwasan
150150
check-lsan
151151
check-msan
152+
check-nsan
152153
check-profile
153154
check-safestack
154155
check-sanitizer

0 commit comments

Comments
 (0)