Skip to content

Commit 0309388

Browse files
committed
[sanitizer] Support "alloc_dealloc_mismatch" suppressions
This adds a stack-based suppressions for alloc-dealloc-mismatch violations, using the function name to match.
1 parent c4ca87e commit 0309388

File tree

3 files changed

+56
-16
lines changed

3 files changed

+56
-16
lines changed

compiler-rt/lib/asan/asan_allocator.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "asan_poisoning.h"
2222
#include "asan_report.h"
2323
#include "asan_stack.h"
24+
#include "asan_suppressions.h"
2425
#include "asan_thread.h"
2526
#include "lsan/lsan_common.h"
2627
#include "sanitizer_common/sanitizer_allocator_checks.h"
@@ -732,7 +733,8 @@ struct Allocator {
732733
if (!AtomicallySetQuarantineFlagIfAllocated(m, ptr, stack)) return;
733734

734735
if (m->alloc_type != alloc_type) {
735-
if (atomic_load(&alloc_dealloc_mismatch, memory_order_acquire)) {
736+
if (atomic_load(&alloc_dealloc_mismatch, memory_order_acquire) &&
737+
!IsStackTraceSuppressed(stack)) {
736738
ReportAllocTypeMismatch((uptr)ptr, stack, (AllocType)m->alloc_type,
737739
(AllocType)alloc_type);
738740
}

compiler-rt/lib/asan/asan_suppressions.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ static const char kInterceptorName[] = "interceptor_name";
2626
static const char kInterceptorViaFunction[] = "interceptor_via_fun";
2727
static const char kInterceptorViaLibrary[] = "interceptor_via_lib";
2828
static const char kODRViolation[] = "odr_violation";
29+
static const char kAllocDeallocMismatch[] = "alloc_dealloc_mismatch";
2930
static const char *kSuppressionTypes[] = {
3031
kInterceptorName, kInterceptorViaFunction, kInterceptorViaLibrary,
31-
kODRViolation};
32+
kODRViolation, kAllocDeallocMismatch};
3233

3334
SANITIZER_INTERFACE_WEAK_DEF(const char *, __asan_default_suppressions, void) {
3435
return "";
@@ -52,7 +53,8 @@ bool IsInterceptorSuppressed(const char *interceptor_name) {
5253
bool HaveStackTraceBasedSuppressions() {
5354
CHECK(suppression_ctx);
5455
return suppression_ctx->HasSuppressionType(kInterceptorViaFunction) ||
55-
suppression_ctx->HasSuppressionType(kInterceptorViaLibrary);
56+
suppression_ctx->HasSuppressionType(kInterceptorViaLibrary) ||
57+
suppression_ctx->HasSuppressionType(kAllocDeallocMismatch);
5658
}
5759

5860
bool IsODRViolationSuppressed(const char *global_var_name) {
@@ -79,19 +81,22 @@ bool IsStackTraceSuppressed(const StackTrace *stack) {
7981
return true;
8082
}
8183

82-
if (suppression_ctx->HasSuppressionType(kInterceptorViaFunction)) {
83-
SymbolizedStackHolder symbolized_stack(symbolizer->SymbolizePC(addr));
84-
const SymbolizedStack *frames = symbolized_stack.get();
85-
CHECK(frames);
86-
for (const SymbolizedStack *cur = frames; cur; cur = cur->next) {
87-
const char *function_name = cur->info.function;
88-
if (!function_name) {
89-
continue;
90-
}
91-
// Match "interceptor_via_fun" suppressions.
92-
if (suppression_ctx->Match(function_name, kInterceptorViaFunction,
93-
&s)) {
94-
return true;
84+
const char *suppressions[] = {kInterceptorViaFunction,
85+
kAllocDeallocMismatch};
86+
for (const char *suppression : suppressions) {
87+
if (suppression_ctx->HasSuppressionType(suppression)) {
88+
SymbolizedStackHolder symbolized_stack(symbolizer->SymbolizePC(addr));
89+
const SymbolizedStack *frames = symbolized_stack.get();
90+
CHECK(frames);
91+
for (const SymbolizedStack *cur = frames; cur; cur = cur->next) {
92+
const char *function_name = cur->info.function;
93+
if (!function_name) {
94+
continue;
95+
}
96+
// Match suppressions.
97+
if (suppression_ctx->Match(function_name, suppression, &s)) {
98+
return true;
99+
}
95100
}
96101
}
97102
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Check that without suppressions, we catch the issue.
2+
// RUN: %clangxx_asan -O0 %s -o %t
3+
// RUN: not %run %t 2>&1 | FileCheck --check-prefix=CHECK-CRASH %s
4+
5+
// RUN: echo "alloc_dealloc_mismatch:function" > %t.supp
6+
// RUN: %clangxx_asan -O0 %s -o %t && %env_asan_opts=suppressions='"%t.supp"' %run %t 2>&1 | FileCheck --check-prefix=CHECK-IGNORE %s
7+
// RUN: %clangxx_asan -O3 %s -o %t && %env_asan_opts=suppressions='"%t.supp"' %run %t 2>&1 | FileCheck --check-prefix=CHECK-IGNORE %s
8+
9+
// XFAIL: android
10+
// UNSUPPORTED: ios
11+
12+
// FIXME: atos does not work for inlined functions, yet llvm-symbolizer
13+
// does not always work with debug info on Darwin.
14+
// UNSUPPORTED: darwin
15+
16+
#include <stdio.h>
17+
#include <stdlib.h>
18+
#include <string.h>
19+
20+
void function() {
21+
char *a = (char *)malloc(6);
22+
a[0] = '\0';
23+
size_t len = strlen(a);
24+
delete a; // BOOM
25+
fprintf(stderr, "strlen ignored, len = %zu\n", len);
26+
}
27+
28+
int main() { function(); }
29+
30+
// CHECK-CRASH: AddressSanitizer: alloc-dealloc-mismatch
31+
// CHECK-CRASH-NOT: strlen ignored
32+
// CHECK-IGNORE-NOT: AddressSanitizer: alloc-dealloc-mismatch
33+
// CHECK-IGNORE: strlen ignored

0 commit comments

Comments
 (0)