xref: /aosp_15_r20/external/compiler-rt/lib/asan/asan_suppressions.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- asan_suppressions.cc ----------------------------------------------===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is a part of AddressSanitizer, an address sanity checker.
11*7c3d14c8STreehugger Robot //
12*7c3d14c8STreehugger Robot // Issue suppression and suppression-related functions.
13*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
14*7c3d14c8STreehugger Robot 
15*7c3d14c8STreehugger Robot #include "asan_suppressions.h"
16*7c3d14c8STreehugger Robot 
17*7c3d14c8STreehugger Robot #include "asan_stack.h"
18*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_placement_new.h"
19*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_suppressions.h"
20*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_symbolizer.h"
21*7c3d14c8STreehugger Robot 
22*7c3d14c8STreehugger Robot namespace __asan {
23*7c3d14c8STreehugger Robot 
24*7c3d14c8STreehugger Robot ALIGNED(64) static char suppression_placeholder[sizeof(SuppressionContext)];
25*7c3d14c8STreehugger Robot static SuppressionContext *suppression_ctx = nullptr;
26*7c3d14c8STreehugger Robot static const char kInterceptorName[] = "interceptor_name";
27*7c3d14c8STreehugger Robot static const char kInterceptorViaFunction[] = "interceptor_via_fun";
28*7c3d14c8STreehugger Robot static const char kInterceptorViaLibrary[] = "interceptor_via_lib";
29*7c3d14c8STreehugger Robot static const char kODRViolation[] = "odr_violation";
30*7c3d14c8STreehugger Robot static const char *kSuppressionTypes[] = {
31*7c3d14c8STreehugger Robot     kInterceptorName, kInterceptorViaFunction, kInterceptorViaLibrary,
32*7c3d14c8STreehugger Robot     kODRViolation};
33*7c3d14c8STreehugger Robot 
34*7c3d14c8STreehugger Robot extern "C" {
35*7c3d14c8STreehugger Robot #if SANITIZER_SUPPORTS_WEAK_HOOKS
36*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
37*7c3d14c8STreehugger Robot const char *__asan_default_suppressions();
38*7c3d14c8STreehugger Robot #else
39*7c3d14c8STreehugger Robot // No week hooks, provide empty implementation.
40*7c3d14c8STreehugger Robot const char *__asan_default_suppressions() { return ""; }
41*7c3d14c8STreehugger Robot #endif  // SANITIZER_SUPPORTS_WEAK_HOOKS
42*7c3d14c8STreehugger Robot }  // extern "C"
43*7c3d14c8STreehugger Robot 
InitializeSuppressions()44*7c3d14c8STreehugger Robot void InitializeSuppressions() {
45*7c3d14c8STreehugger Robot   CHECK_EQ(nullptr, suppression_ctx);
46*7c3d14c8STreehugger Robot   suppression_ctx = new (suppression_placeholder)  // NOLINT
47*7c3d14c8STreehugger Robot       SuppressionContext(kSuppressionTypes, ARRAY_SIZE(kSuppressionTypes));
48*7c3d14c8STreehugger Robot   suppression_ctx->ParseFromFile(flags()->suppressions);
49*7c3d14c8STreehugger Robot   if (&__asan_default_suppressions)
50*7c3d14c8STreehugger Robot     suppression_ctx->Parse(__asan_default_suppressions());
51*7c3d14c8STreehugger Robot }
52*7c3d14c8STreehugger Robot 
IsInterceptorSuppressed(const char * interceptor_name)53*7c3d14c8STreehugger Robot bool IsInterceptorSuppressed(const char *interceptor_name) {
54*7c3d14c8STreehugger Robot   CHECK(suppression_ctx);
55*7c3d14c8STreehugger Robot   Suppression *s;
56*7c3d14c8STreehugger Robot   // Match "interceptor_name" suppressions.
57*7c3d14c8STreehugger Robot   return suppression_ctx->Match(interceptor_name, kInterceptorName, &s);
58*7c3d14c8STreehugger Robot }
59*7c3d14c8STreehugger Robot 
HaveStackTraceBasedSuppressions()60*7c3d14c8STreehugger Robot bool HaveStackTraceBasedSuppressions() {
61*7c3d14c8STreehugger Robot   CHECK(suppression_ctx);
62*7c3d14c8STreehugger Robot   return suppression_ctx->HasSuppressionType(kInterceptorViaFunction) ||
63*7c3d14c8STreehugger Robot          suppression_ctx->HasSuppressionType(kInterceptorViaLibrary);
64*7c3d14c8STreehugger Robot }
65*7c3d14c8STreehugger Robot 
IsODRViolationSuppressed(const char * global_var_name)66*7c3d14c8STreehugger Robot bool IsODRViolationSuppressed(const char *global_var_name) {
67*7c3d14c8STreehugger Robot   CHECK(suppression_ctx);
68*7c3d14c8STreehugger Robot   Suppression *s;
69*7c3d14c8STreehugger Robot   // Match "odr_violation" suppressions.
70*7c3d14c8STreehugger Robot   return suppression_ctx->Match(global_var_name, kODRViolation, &s);
71*7c3d14c8STreehugger Robot }
72*7c3d14c8STreehugger Robot 
IsStackTraceSuppressed(const StackTrace * stack)73*7c3d14c8STreehugger Robot bool IsStackTraceSuppressed(const StackTrace *stack) {
74*7c3d14c8STreehugger Robot   if (!HaveStackTraceBasedSuppressions())
75*7c3d14c8STreehugger Robot     return false;
76*7c3d14c8STreehugger Robot 
77*7c3d14c8STreehugger Robot   CHECK(suppression_ctx);
78*7c3d14c8STreehugger Robot   Symbolizer *symbolizer = Symbolizer::GetOrInit();
79*7c3d14c8STreehugger Robot   Suppression *s;
80*7c3d14c8STreehugger Robot   for (uptr i = 0; i < stack->size && stack->trace[i]; i++) {
81*7c3d14c8STreehugger Robot     uptr addr = stack->trace[i];
82*7c3d14c8STreehugger Robot 
83*7c3d14c8STreehugger Robot     if (suppression_ctx->HasSuppressionType(kInterceptorViaLibrary)) {
84*7c3d14c8STreehugger Robot       // Match "interceptor_via_lib" suppressions.
85*7c3d14c8STreehugger Robot       if (const char *module_name = symbolizer->GetModuleNameForPc(addr))
86*7c3d14c8STreehugger Robot         if (suppression_ctx->Match(module_name, kInterceptorViaLibrary, &s))
87*7c3d14c8STreehugger Robot           return true;
88*7c3d14c8STreehugger Robot     }
89*7c3d14c8STreehugger Robot 
90*7c3d14c8STreehugger Robot     if (suppression_ctx->HasSuppressionType(kInterceptorViaFunction)) {
91*7c3d14c8STreehugger Robot       SymbolizedStack *frames = symbolizer->SymbolizePC(addr);
92*7c3d14c8STreehugger Robot       CHECK(frames);
93*7c3d14c8STreehugger Robot       for (SymbolizedStack *cur = frames; cur; cur = cur->next) {
94*7c3d14c8STreehugger Robot         const char *function_name = cur->info.function;
95*7c3d14c8STreehugger Robot         if (!function_name) {
96*7c3d14c8STreehugger Robot           continue;
97*7c3d14c8STreehugger Robot         }
98*7c3d14c8STreehugger Robot         // Match "interceptor_via_fun" suppressions.
99*7c3d14c8STreehugger Robot         if (suppression_ctx->Match(function_name, kInterceptorViaFunction,
100*7c3d14c8STreehugger Robot                                    &s)) {
101*7c3d14c8STreehugger Robot           frames->ClearAll();
102*7c3d14c8STreehugger Robot           return true;
103*7c3d14c8STreehugger Robot         }
104*7c3d14c8STreehugger Robot       }
105*7c3d14c8STreehugger Robot       frames->ClearAll();
106*7c3d14c8STreehugger Robot     }
107*7c3d14c8STreehugger Robot   }
108*7c3d14c8STreehugger Robot   return false;
109*7c3d14c8STreehugger Robot }
110*7c3d14c8STreehugger Robot 
111*7c3d14c8STreehugger Robot } // namespace __asan
112