[clang] [llvm] [sancov] Introduce optional callback for stack-depth tracking (PR #138323)

via cfe-commits cfe-commits at lists.llvm.org
Fri May 2 11:48:50 PDT 2025


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff HEAD~1 HEAD --extensions cpp,h -- clang/include/clang/Driver/SanitizerArgs.h clang/lib/CodeGen/BackendUtil.cpp clang/lib/Driver/SanitizerArgs.cpp llvm/include/llvm/Transforms/Utils/Instrumentation.h llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/clang/lib/Driver/SanitizerArgs.cpp b/clang/lib/Driver/SanitizerArgs.cpp
index 414f68e44..da4ea6293 100644
--- a/clang/lib/Driver/SanitizerArgs.cpp
+++ b/clang/lib/Driver/SanitizerArgs.cpp
@@ -752,13 +752,13 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
       clang::diag::err_drv_malformed_sanitizer_ignorelist, DiagnoseErrors);
 
   // Verify that -fsanitize-coverage-stack-depth-callback-min is >= 0.
-  if (Arg *A = Args.getLastArg(options::OPT_fsanitize_coverage_stack_depth_callback_min_EQ)) {
-      StringRef S = A->getValue();
-      if (S.getAsInteger(0, StackDepthCallbackMin) || StackDepthCallbackMin < 0) {
-          if (DiagnoseErrors)
-            D.Diag(clang::diag::err_drv_invalid_value)
-                << A->getAsString(Args) << S;
-      }
+  if (Arg *A = Args.getLastArg(
+          options::OPT_fsanitize_coverage_stack_depth_callback_min_EQ)) {
+    StringRef S = A->getValue();
+    if (S.getAsInteger(0, StackDepthCallbackMin) || StackDepthCallbackMin < 0) {
+      if (DiagnoseErrors)
+        D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
+    }
   }
 
   // Parse -f[no-]sanitize-memory-track-origins[=level] options.
@@ -1280,8 +1280,9 @@ void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
                         CoverageIgnorelistFiles);
 
   if (StackDepthCallbackMin)
-    CmdArgs.push_back(Args.MakeArgString("-fsanitize-coverage-stack-depth-callback-min=" +
-                                         Twine(StackDepthCallbackMin)));
+    CmdArgs.push_back(
+        Args.MakeArgString("-fsanitize-coverage-stack-depth-callback-min=" +
+                           Twine(StackDepthCallbackMin)));
 
   if (!GPUSanitize) {
     // Translate available BinaryMetadataFeatures to corresponding clang-cc1
diff --git a/llvm/include/llvm/Transforms/Utils/Instrumentation.h b/llvm/include/llvm/Transforms/Utils/Instrumentation.h
index 0b2ccf618..f2bc6854a 100644
--- a/llvm/include/llvm/Transforms/Utils/Instrumentation.h
+++ b/llvm/include/llvm/Transforms/Utils/Instrumentation.h
@@ -158,7 +158,7 @@ struct SanitizerCoverageOptions {
   bool PCTable = false;
   bool NoPrune = false;
   bool StackDepth = false;
-  int  StackDepthCallbackMin = 0;
+  int StackDepthCallbackMin = 0;
   bool TraceLoads = false;
   bool TraceStores = false;
   bool CollectControlFlow = false;
diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
index b8212b42b..d70d1aab0 100644
--- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
+++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
@@ -157,7 +157,7 @@ static cl::opt<bool> ClStackDepth("sanitizer-coverage-stack-depth",
 static cl::opt<int> ClStackDepthCallbackMin(
     "sanitizer-coverage-stack-depth-callback-min",
     cl::desc("max stack depth tracing should use callback and only when "
-	     "stack depth more than specified"),
+             "stack depth more than specified"),
     cl::Hidden);
 
 static cl::opt<bool>
@@ -525,7 +525,8 @@ bool ModuleSanitizerCoverage::instrumentModule() {
   SanCovTracePCGuard =
       M.getOrInsertFunction(SanCovTracePCGuardName, VoidTy, PtrTy);
 
-  SanCovStackDepthCallback = M.getOrInsertFunction(SanCovStackDepthCallbackName, VoidTy);
+  SanCovStackDepthCallback =
+      M.getOrInsertFunction(SanCovStackDepthCallbackName, VoidTy);
 
   for (auto &F : M)
     instrumentFunction(F);
@@ -1099,15 +1100,15 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
 
       // Make an estimate on the stack usage.
       for (auto &I : F.getEntryBlock()) {
-          if (auto *AI = dyn_cast<AllocaInst>(&I)) {
-              if (AI->isStaticAlloca()) {
-                  uint32_t TypeSize = DL.getTypeAllocSize(AI->getAllocatedType());
-                  EstimatedStackSize += TypeSize;
-              } else {
-                  // Over-estimate dynamic sizes.
-                  EstimatedStackSize += 4096;
-              }
+        if (auto *AI = dyn_cast<AllocaInst>(&I)) {
+          if (AI->isStaticAlloca()) {
+            uint32_t TypeSize = DL.getTypeAllocSize(AI->getAllocatedType());
+            EstimatedStackSize += TypeSize;
+          } else {
+            // Over-estimate dynamic sizes.
+            EstimatedStackSize += 4096;
           }
+        }
       }
 
       if (EstimatedStackSize >= Options.StackDepthCallbackMin)

``````````

</details>


https://github.com/llvm/llvm-project/pull/138323


More information about the cfe-commits mailing list