Skip to content

[WIP][DO NOT MERGE][Clang][Driver] Emit warning when -fsanitize-trap=<...> is passed without associated -fsanitize=<...> #147997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/include/clang/Basic/DiagnosticDriverKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -874,4 +874,9 @@ def warn_drv_openacc_without_cir
: Warning<"OpenACC directives will result in no runtime behavior; use "
"-fclangir to enable runtime effect">,
InGroup<SourceUsesOpenACC>;

def warn_drv_sanitize_trap_mismatch : Warning<
"-fsanitize-trap=%0 has no effect because the \"%0\" sanitizer is disabled; "
"consider passing \"fsanitize=%0\" to enable the sanitizer">,
InGroup<SanitizeTrapMismatch>;
}
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticGroups.td
Original file line number Diff line number Diff line change
Expand Up @@ -1752,3 +1752,6 @@ def ExplicitSpecializationStorageClass : DiagGroup<"explicit-specialization-stor

// A warning for options that enable a feature that is not yet complete
def ExperimentalOption : DiagGroup<"experimental-option">;

// Warnings for sanitizer arguments
def SanitizeTrapMismatch : DiagGroup<"sanitize-trap-mismatch">;
41 changes: 41 additions & 0 deletions clang/lib/Driver/SanitizerArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,30 @@ parseSanitizeSkipHotCutoffArgs(const Driver &D, const llvm::opt::ArgList &Args,
return Cutoffs;
}

// Given a set of mismatched bits, TrapOnly (bits the user asked to trap but
// that aren’t actually enabled), emit a warning based on -fsanitize-trap=NAME
static void diagnoseTrapOnly(const Driver &D, SanitizerMask &TrapOnly) {
// One pass for sanitizer groupings
#define SANITIZER(NAME, ID)
#define SANITIZER_GROUP(NAME, ID, ALIAS) \
if (TrapOnly & SanitizerKind::ID##Group) { \
D.Diag(diag::warn_drv_sanitize_trap_mismatch) << NAME; \
TrapOnly &= ~SanitizerKind::ID##Group; \
TrapOnly &= ~SanitizerKind::ID; \
}
#include "clang/Basic/Sanitizers.def"

#undef SANITIZER_GROUP
// One pass for individual sanitizer names/leaves
#define SANITIZER_GROUP(NAME, ID, ALIAS)
#define SANITIZER(NAME, ID) \
if (TrapOnly & SanitizerKind::ID) { \
D.Diag(diag::warn_drv_sanitize_trap_mismatch) << NAME; \
TrapOnly &= ~SanitizerKind::ID; \
}
#include "clang/Basic/Sanitizers.def"
}

bool SanitizerArgs::needsFuzzerInterceptors() const {
return needsFuzzer() && !needsAsanRt() && !needsTsanRt() && !needsMsanRt();
}
Expand Down Expand Up @@ -730,6 +754,23 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
options::OPT_fno_sanitize_recover_EQ);
RecoverableKinds &= Kinds;

// FIXME: `DiagnoseErrors` name seems a little wrong as we emit warnings here,
// not errors but that seems to be done elsewhere in this method.

// Parse any -fsanitize-trap=<...> flags the user provided, then
// diagnose any which do not have a matching -fsanitize=<...>
if (DiagnoseErrors) {
// parseSanitizeTrapArgs was not used because it sets a TrappingDefault
// which causes the emission of warnings beyond what the user entered
SanitizerMask ExplicitTrap = parseSanitizeArgs(
D, Args, false, {}, {}, {}, options::OPT_fsanitize_trap_EQ,
options::OPT_fno_sanitize_trap_EQ);
SanitizerMask TrapOnly = ExplicitTrap & ~Kinds;

if (TrapOnly)
diagnoseTrapOnly(D, TrapOnly);
}

TrappingKinds &= Kinds;
RecoverableKinds &= ~TrappingKinds;

Expand Down
9 changes: 9 additions & 0 deletions clang/test/Driver/fsanitize-trap-mismatch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %clang -S -emit-llvm -g -O0 %s -fsanitize-trap=undefined -o - 2>&1 | FileCheck %s --check-prefix=WARN
// RUN: %clang -S -emit-llvm -g -O0 %s -fsanitize=undefined -fsanitize-trap=undefined -o - 2>&1 | FileCheck %s --check-prefix=NOWARN
// RUN: %clang -S -emit-llvm -g -O0 %s -fsanitize-trap=undefined -Wno-sanitize-trap-mismatch -o - 2>&1 | FileCheck %s --check-prefix=SUPPRESS
//
// WARN: warning: -fsanitize-trap=undefined has no effect because the "undefined" sanitizer is disabled; consider passing "fsanitize=undefined" to enable the sanitizer
// NOWARN-NOT: warning: -fsanitize-trap=undefined has no effect because the "undefined" sanitizer is disabled; consider passing "fsanitize=undefined" to enable the sanitizer
// SUPPRESS-NOT: warning: -fsanitize-trap=undefined has no effect because the "undefined" sanitizer is disabled; consider passing "fsanitize=undefined" to enable the sanitizer

int main(void) { return 0; }