Skip to content

Conversation

ArtSin
Copy link
Contributor

@ArtSin ArtSin commented Apr 21, 2025

When using -fsanitize-trap with a sanitizer group that doesn't support trapping, an empty argument is passed to err_drv_unsupported_option_argument. Use new toStringWithGroups for the diagnostic.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Apr 21, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 21, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-driver

Author: Sinkevich Artem (ArtSin)

Changes

When using -fsanitize-trap with a sanitizer group that doesn't support trapping, an empty argument is passed to err_drv_unsupported_option_argument. Expand groups for the diagnostic.


Full diff: https://github.com/llvm/llvm-project/pull/136549.diff

2 Files Affected:

  • (modified) clang/lib/Driver/SanitizerArgs.cpp (+2-2)
  • (modified) clang/test/Driver/fsanitize.c (+3)
diff --git a/clang/lib/Driver/SanitizerArgs.cpp b/clang/lib/Driver/SanitizerArgs.cpp
index f27cb813012f2..05ca2656c6522 100644
--- a/clang/lib/Driver/SanitizerArgs.cpp
+++ b/clang/lib/Driver/SanitizerArgs.cpp
@@ -286,7 +286,7 @@ parseSanitizeArgs(const Driver &D, const llvm::opt::ArgList &Args,
               Add & AlwaysOut & ~DiagnosedAlwaysOutViolations) {
         if (DiagnoseErrors) {
           SanitizerSet SetToDiagnose;
-          SetToDiagnose.Mask |= KindsToDiagnose;
+          SetToDiagnose.Mask |= expandSanitizerGroups(KindsToDiagnose);
           D.Diag(diag::err_drv_unsupported_option_argument)
               << Arg->getSpelling() << toString(SetToDiagnose);
           DiagnosedAlwaysOutViolations |= KindsToDiagnose;
@@ -302,7 +302,7 @@ parseSanitizeArgs(const Driver &D, const llvm::opt::ArgList &Args,
               Remove & AlwaysIn & ~DiagnosedAlwaysInViolations) {
         if (DiagnoseErrors) {
           SanitizerSet SetToDiagnose;
-          SetToDiagnose.Mask |= KindsToDiagnose;
+          SetToDiagnose.Mask |= expandSanitizerGroups(KindsToDiagnose);
           D.Diag(diag::err_drv_unsupported_option_argument)
               << Arg->getSpelling() << toString(SetToDiagnose);
           DiagnosedAlwaysInViolations |= KindsToDiagnose;
diff --git a/clang/test/Driver/fsanitize.c b/clang/test/Driver/fsanitize.c
index c154e339941f2..434e6fd8c4a15 100644
--- a/clang/test/Driver/fsanitize.c
+++ b/clang/test/Driver/fsanitize.c
@@ -259,6 +259,9 @@
 // RUN: not %clang --target=aarch64-linux -fsanitize=memtag -I +mte %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SANMT-NOMT-1
 // CHECK-SANMT-NOMT-1: '-fsanitize=memtag-stack' requires hardware support (+memtag)
 
+// RUN: not %clang --target=aarch64-linux-android31 -fsanitize-trap=memtag -march=armv8-a+memtag -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SANMT-TRAP
+// CHECK-SANMT-TRAP: error: unsupported argument 'memtag-stack,memtag-heap,memtag-globals' to option '-fsanitize-trap='
+
 // RUN: %clang --target=x86_64-linux-gnu -fsanitize=address -fsanitize-address-use-after-scope %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-USE-AFTER-SCOPE
 // RUN: %clang_cl --target=x86_64-windows -fsanitize=address -fsanitize-address-use-after-scope -### -- %s 2>&1 | FileCheck %s --check-prefix=CHECK-USE-AFTER-SCOPE
 // CHECK-USE-AFTER-SCOPE: -cc1{{.*}}-fsanitize-address-use-after-scope

@ArtSin
Copy link
Contributor Author

ArtSin commented Apr 21, 2025

PTAL @vitalybuka

@ArtSin ArtSin force-pushed the fix-fsanitize-trap-group-error branch from 459d375 to f5c8b4e Compare May 6, 2025 08:59
@ArtSin
Copy link
Contributor Author

ArtSin commented May 8, 2025

PTAL @thurstond

@ArtSin
Copy link
Contributor Author

ArtSin commented May 14, 2025

Ping @vitalybuka

@thurstond
Copy link
Contributor

thurstond commented May 14, 2025

Sorry, I didn't notice being tagged earlier. It didn't show up in my review queue since the "Request Review" wasn't used.


Thanks for the patch! I agree this is an improvement over the empty output, but it's arguably confusing to tell the user unsupported argument 'memtag-stack,memtag-heap,memtag-globals' when they had actually specified -fsanitize-trap=memtag (for memtag the relation is obvious, but it can be more obscure for other sanitizer groups). Perhaps a better fix would be to correct the toString function:

static std::string toString(const clang::SanitizerSet &Sanitizers) {
  std::string Res;
#define SANITIZER(NAME, ID)                                                    \
  if (Sanitizers.has(SanitizerKind::ID)) {                                     \
    if (!Res.empty())                                                          \
      Res += ",";                                                              \
    Res += NAME;                                                               \
  }
#include "clang/Basic/Sanitizers.def"
  return Res;
}

(clang/lib/Driver/SanitizerArgs.cpp)

so that it works for SANITIZER_GROUP as well instead of only SANITIZER.

// CHECK-SANMT-NOMT-1: '-fsanitize=memtag-stack' requires hardware support (+memtag)

// RUN: not %clang --target=aarch64-linux-android31 -fsanitize-trap=memtag -march=armv8-a+memtag -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SANMT-TRAP
// CHECK-SANMT-TRAP: error: unsupported argument 'memtag-stack,memtag-heap,memtag-globals' to option '-fsanitize-trap='
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: precommit this test to show that it currently prints
unsupported argument '' to option '-fsanitize-trap='

@thurstond thurstond self-requested a review May 14, 2025 19:41
…tize-trap

When using `-fsanitize-trap` with a sanitizer group that doesn't support
trapping, an empty argument is passed to `err_drv_unsupported_option_argument`.
Use new `toStringWithGroups` for the diagnostic.
@ArtSin ArtSin force-pushed the fix-fsanitize-trap-group-error branch from f5c8b4e to 626d7e1 Compare May 15, 2025 08:23
@ArtSin
Copy link
Contributor Author

ArtSin commented May 15, 2025

Perhaps a better fix would be to correct the toString function

I agree, but adding groups to toString breaks its other uses. What do you think about separate toStringWithGroups?

@thurstond
Copy link
Contributor

Perhaps a better fix would be to correct the toString function

I agree, but adding groups to toString breaks its other uses. What do you think about separate toStringWithGroups?

Hmm, that's a good point. I like your toStringWithGroups idea!

@ArtSin
Copy link
Contributor Author

ArtSin commented May 15, 2025

@thurstond can you please merge this? I don't have write access.

@thurstond
Copy link
Contributor

@thurstond can you please merge this? I don't have write access.

Sure! Thanks for the fix :-)

@thurstond thurstond merged commit 1acac5c into llvm:main May 15, 2025
11 checks passed
@ArtSin ArtSin deleted the fix-fsanitize-trap-group-error branch May 15, 2025 18:22
@llvm-ci
Copy link
Collaborator

llvm-ci commented May 15, 2025

LLVM Buildbot has detected a new failure on builder llvm-clang-x86_64-gcc-ubuntu running on sie-linux-worker3 while building clang at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/174/builds/17824

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
...
PASS: lit :: allow-retries.py (90143 of 90152)
PASS: lit :: discovery.py (90144 of 90152)
PASS: lit :: shtest-external-shell-kill.py (90145 of 90152)
PASS: lit :: googletest-timeout.py (90146 of 90152)
PASS: lit :: selecting.py (90147 of 90152)
PASS: lit :: shtest-timeout.py (90148 of 90152)
PASS: lit :: max-time.py (90149 of 90152)
PASS: lit :: shtest-shell.py (90150 of 90152)
PASS: lit :: shtest-define.py (90151 of 90152)
TIMEOUT: AddressSanitizer-x86_64-linux :: TestCases/asan_lsan_deadlock.cpp (90152 of 90152)
******************** TEST 'AddressSanitizer-x86_64-linux :: TestCases/asan_lsan_deadlock.cpp' FAILED ********************
Exit Code: -9
Timeout: Reached timeout of 900 seconds

Command Output (stderr):
--
/home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/./bin/clang  --driver-mode=g++ -fsanitize=address -mno-omit-leaf-frame-pointer -fno-omit-frame-pointer -fno-optimize-sibling-calls -gline-tables-only  -m64  -O0 /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/llvm-project/compiler-rt/test/asan/TestCases/asan_lsan_deadlock.cpp -o /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64LinuxConfig/TestCases/Output/asan_lsan_deadlock.cpp.tmp # RUN: at line 4
+ /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/./bin/clang --driver-mode=g++ -fsanitize=address -mno-omit-leaf-frame-pointer -fno-omit-frame-pointer -fno-optimize-sibling-calls -gline-tables-only -m64 -O0 /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/llvm-project/compiler-rt/test/asan/TestCases/asan_lsan_deadlock.cpp -o /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64LinuxConfig/TestCases/Output/asan_lsan_deadlock.cpp.tmp
env ASAN_OPTIONS=detect_leaks=1 not  /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64LinuxConfig/TestCases/Output/asan_lsan_deadlock.cpp.tmp 2>&1 | FileCheck /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/llvm-project/compiler-rt/test/asan/TestCases/asan_lsan_deadlock.cpp # RUN: at line 5
+ env ASAN_OPTIONS=detect_leaks=1 not /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64LinuxConfig/TestCases/Output/asan_lsan_deadlock.cpp.tmp
+ FileCheck /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/llvm-project/compiler-rt/test/asan/TestCases/asan_lsan_deadlock.cpp

--

********************
********************
Timed Out Tests (1):
  AddressSanitizer-x86_64-linux :: TestCases/asan_lsan_deadlock.cpp


Testing Time: 1159.48s

Total Discovered Tests: 124690
  Skipped          :     38 (0.03%)
  Unsupported      :   2680 (2.15%)
  Passed           : 121681 (97.59%)
  Expectedly Failed:    290 (0.23%)
  Timed Out        :      1 (0.00%)
FAILED: CMakeFiles/check-all /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/CMakeFiles/check-all 
cd /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build && /usr/bin/python3.8 /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/./bin/llvm-lit --verbose --timeout=900 --param USE_Z3_SOLVER=0 /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/utils/mlgo-utils /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/projects/cross-project-tests /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/tools/lld/test /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/tools/clang/tools/extra/include-cleaner/test /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/tools/clang/tools/extra/test /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/tools/clang/test @/home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/lit.tests /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/utils/lit /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/test
ninja: build stopped: subcommand failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants