asan_suppressions.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //===-- asan_suppressions.cpp ---------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of AddressSanitizer, an address sanity checker.
  10. //
  11. // Issue suppression and suppression-related functions.
  12. //===----------------------------------------------------------------------===//
  13. #include "asan_suppressions.h"
  14. #include "asan_stack.h"
  15. #include "sanitizer_common/sanitizer_placement_new.h"
  16. #include "sanitizer_common/sanitizer_suppressions.h"
  17. #include "sanitizer_common/sanitizer_symbolizer.h"
  18. namespace __asan {
  19. ALIGNED(64) static char suppression_placeholder[sizeof(SuppressionContext)];
  20. static SuppressionContext *suppression_ctx = nullptr;
  21. static const char kInterceptorName[] = "interceptor_name";
  22. static const char kInterceptorViaFunction[] = "interceptor_via_fun";
  23. static const char kInterceptorViaLibrary[] = "interceptor_via_lib";
  24. static const char kODRViolation[] = "odr_violation";
  25. static const char *kSuppressionTypes[] = {
  26. kInterceptorName, kInterceptorViaFunction, kInterceptorViaLibrary,
  27. kODRViolation};
  28. SANITIZER_INTERFACE_WEAK_DEF(const char *, __asan_default_suppressions, void) {
  29. return "";
  30. }
  31. void InitializeSuppressions() {
  32. CHECK_EQ(nullptr, suppression_ctx);
  33. suppression_ctx = new (suppression_placeholder)
  34. SuppressionContext(kSuppressionTypes, ARRAY_SIZE(kSuppressionTypes));
  35. suppression_ctx->ParseFromFile(flags()->suppressions);
  36. if (&__asan_default_suppressions)
  37. suppression_ctx->Parse(__asan_default_suppressions());
  38. }
  39. bool IsInterceptorSuppressed(const char *interceptor_name) {
  40. CHECK(suppression_ctx);
  41. Suppression *s;
  42. // Match "interceptor_name" suppressions.
  43. return suppression_ctx->Match(interceptor_name, kInterceptorName, &s);
  44. }
  45. bool HaveStackTraceBasedSuppressions() {
  46. CHECK(suppression_ctx);
  47. return suppression_ctx->HasSuppressionType(kInterceptorViaFunction) ||
  48. suppression_ctx->HasSuppressionType(kInterceptorViaLibrary);
  49. }
  50. bool IsODRViolationSuppressed(const char *global_var_name) {
  51. CHECK(suppression_ctx);
  52. Suppression *s;
  53. // Match "odr_violation" suppressions.
  54. return suppression_ctx->Match(global_var_name, kODRViolation, &s);
  55. }
  56. bool IsStackTraceSuppressed(const StackTrace *stack) {
  57. if (!HaveStackTraceBasedSuppressions())
  58. return false;
  59. CHECK(suppression_ctx);
  60. Symbolizer *symbolizer = Symbolizer::GetOrInit();
  61. Suppression *s;
  62. for (uptr i = 0; i < stack->size && stack->trace[i]; i++) {
  63. uptr addr = stack->trace[i];
  64. if (suppression_ctx->HasSuppressionType(kInterceptorViaLibrary)) {
  65. // Match "interceptor_via_lib" suppressions.
  66. if (const char *module_name = symbolizer->GetModuleNameForPc(addr))
  67. if (suppression_ctx->Match(module_name, kInterceptorViaLibrary, &s))
  68. return true;
  69. }
  70. if (suppression_ctx->HasSuppressionType(kInterceptorViaFunction)) {
  71. SymbolizedStack *frames = symbolizer->SymbolizePC(addr);
  72. CHECK(frames);
  73. for (SymbolizedStack *cur = frames; cur; cur = cur->next) {
  74. const char *function_name = cur->info.function;
  75. if (!function_name) {
  76. continue;
  77. }
  78. // Match "interceptor_via_fun" suppressions.
  79. if (suppression_ctx->Match(function_name, kInterceptorViaFunction,
  80. &s)) {
  81. frames->ClearAll();
  82. return true;
  83. }
  84. }
  85. frames->ClearAll();
  86. }
  87. }
  88. return false;
  89. }
  90. } // namespace __asan