[compiler-rt] [sanitizer] Support "alloc_dealloc_mismatch" suppressions (PR #124197)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 24 14:02:12 PST 2025


https://github.com/andrewjcg updated https://github.com/llvm/llvm-project/pull/124197

>From 43d999ae4a758c929761a2df189a1e50a36cd6c0 Mon Sep 17 00:00:00 2001
From: Andrew Gallagher <agallagher at fb.com>
Date: Thu, 23 Jan 2025 12:38:00 -0800
Subject: [PATCH] [sanitizer] Support "alloc_dealloc_mismatch" suppressions

This adds a stack-based suppressions for alloc-dealloc-mismatch
violations, using the function name to match.
---
 compiler-rt/lib/asan/asan_allocator.cpp       |  4 ++-
 compiler-rt/lib/asan/asan_suppressions.cpp    | 18 ++++++++-----
 .../suppressions-alloc-dealloc-mismatch.cpp   | 26 +++++++++++++++++++
 3 files changed, 41 insertions(+), 7 deletions(-)
 create mode 100644 compiler-rt/test/asan/TestCases/suppressions-alloc-dealloc-mismatch.cpp

diff --git a/compiler-rt/lib/asan/asan_allocator.cpp b/compiler-rt/lib/asan/asan_allocator.cpp
index 9e66f77217ec6b..a5f939b4c2f8aa 100644
--- a/compiler-rt/lib/asan/asan_allocator.cpp
+++ b/compiler-rt/lib/asan/asan_allocator.cpp
@@ -21,6 +21,7 @@
 #include "asan_poisoning.h"
 #include "asan_report.h"
 #include "asan_stack.h"
+#include "asan_suppressions.h"
 #include "asan_thread.h"
 #include "lsan/lsan_common.h"
 #include "sanitizer_common/sanitizer_allocator_checks.h"
@@ -732,7 +733,8 @@ struct Allocator {
     if (!AtomicallySetQuarantineFlagIfAllocated(m, ptr, stack)) return;
 
     if (m->alloc_type != alloc_type) {
-      if (atomic_load(&alloc_dealloc_mismatch, memory_order_acquire)) {
+      if (atomic_load(&alloc_dealloc_mismatch, memory_order_acquire) &&
+          !IsStackTraceSuppressed(stack)) {
         ReportAllocTypeMismatch((uptr)ptr, stack, (AllocType)m->alloc_type,
                                 (AllocType)alloc_type);
       }
diff --git a/compiler-rt/lib/asan/asan_suppressions.cpp b/compiler-rt/lib/asan/asan_suppressions.cpp
index 94289d14d7e780..9f69c1e309d08e 100644
--- a/compiler-rt/lib/asan/asan_suppressions.cpp
+++ b/compiler-rt/lib/asan/asan_suppressions.cpp
@@ -26,9 +26,10 @@ static const char kInterceptorName[] = "interceptor_name";
 static const char kInterceptorViaFunction[] = "interceptor_via_fun";
 static const char kInterceptorViaLibrary[] = "interceptor_via_lib";
 static const char kODRViolation[] = "odr_violation";
+static const char kAllocDeallocMismatch[] = "alloc_dealloc_mismatch";
 static const char *kSuppressionTypes[] = {
     kInterceptorName, kInterceptorViaFunction, kInterceptorViaLibrary,
-    kODRViolation};
+    kODRViolation, kAllocDeallocMismatch};
 
 SANITIZER_INTERFACE_WEAK_DEF(const char *, __asan_default_suppressions, void) {
   return "";
@@ -52,7 +53,8 @@ bool IsInterceptorSuppressed(const char *interceptor_name) {
 bool HaveStackTraceBasedSuppressions() {
   CHECK(suppression_ctx);
   return suppression_ctx->HasSuppressionType(kInterceptorViaFunction) ||
-         suppression_ctx->HasSuppressionType(kInterceptorViaLibrary);
+         suppression_ctx->HasSuppressionType(kInterceptorViaLibrary) ||
+         suppression_ctx->HasSuppressionType(kAllocDeallocMismatch);
 }
 
 bool IsODRViolationSuppressed(const char *global_var_name) {
@@ -79,7 +81,12 @@ bool IsStackTraceSuppressed(const StackTrace *stack) {
           return true;
     }
 
-    if (suppression_ctx->HasSuppressionType(kInterceptorViaFunction)) {
+    const char *suppressions[] = {kInterceptorViaFunction,
+                                  kAllocDeallocMismatch};
+    for (const char *suppression : suppressions) {
+      if (!suppression_ctx->HasSuppressionType(suppression)) {
+        continue;
+      }
       SymbolizedStackHolder symbolized_stack(symbolizer->SymbolizePC(addr));
       const SymbolizedStack *frames = symbolized_stack.get();
       CHECK(frames);
@@ -88,9 +95,8 @@ bool IsStackTraceSuppressed(const StackTrace *stack) {
         if (!function_name) {
           continue;
         }
-        // Match "interceptor_via_fun" suppressions.
-        if (suppression_ctx->Match(function_name, kInterceptorViaFunction,
-                                   &s)) {
+        // Match suppressions.
+        if (suppression_ctx->Match(function_name, suppression, &s)) {
           return true;
         }
       }
diff --git a/compiler-rt/test/asan/TestCases/suppressions-alloc-dealloc-mismatch.cpp b/compiler-rt/test/asan/TestCases/suppressions-alloc-dealloc-mismatch.cpp
new file mode 100644
index 00000000000000..fe88a5d0c9bf15
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/suppressions-alloc-dealloc-mismatch.cpp
@@ -0,0 +1,26 @@
+// Check that without suppressions, we catch the issue.
+// RUN: %clangxx_asan -O0 %s -o %t
+// RUN: not %run %t 2>&1 | FileCheck --check-prefix=CHECK-CRASH %s
+
+// RUN: echo "alloc_dealloc_mismatch:function" > %t.supp
+// RUN: %clangxx_asan -O0 %s -o %t && %env_asan_opts=suppressions='"%t.supp"' %run %t 2>&1 | FileCheck --check-prefix=CHECK-IGNORE %s
+// RUN: %clangxx_asan -O3 %s -o %t && %env_asan_opts=suppressions='"%t.supp"' %run %t 2>&1 | FileCheck --check-prefix=CHECK-IGNORE %s
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void function() {
+  char *a = (char *)malloc(6);
+  a[0] = '\0';
+  size_t len = strlen(a);
+  delete a; // BOOM
+  fprintf(stderr, "strlen ignored, len = %zu\n", len);
+}
+
+int main() { function(); }
+
+// CHECK-CRASH: AddressSanitizer: alloc-dealloc-mismatch
+// CHECK-CRASH-NOT: strlen ignored
+// CHECK-IGNORE-NOT: AddressSanitizer: alloc-dealloc-mismatch
+// CHECK-IGNORE: strlen ignored



More information about the llvm-commits mailing list