sanitizer_libignore.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //===-- sanitizer_libignore.h -----------------------------------*- C++ -*-===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // LibIgnore allows to ignore all interceptors called from a particular set
  9. // of dynamic libraries. LibIgnore remembers all "called_from_lib" suppressions
  10. // from the provided SuppressionContext; finds code ranges for the libraries;
  11. // and checks whether the provided PC value belongs to the code ranges.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef SANITIZER_LIBIGNORE_H
  15. #define SANITIZER_LIBIGNORE_H
  16. #include "sanitizer_internal_defs.h"
  17. #include "sanitizer_common.h"
  18. #include "sanitizer_suppressions.h"
  19. #include "sanitizer_atomic.h"
  20. #include "sanitizer_mutex.h"
  21. namespace __sanitizer {
  22. class LibIgnore {
  23. public:
  24. explicit LibIgnore(LinkerInitialized);
  25. // Fetches all "called_from_lib" suppressions from the SuppressionContext.
  26. void Init(const SuppressionContext &supp);
  27. // Must be called after a new dynamic library is loaded.
  28. void OnLibraryLoaded(const char *name);
  29. // Must be called after a dynamic library is unloaded.
  30. void OnLibraryUnloaded();
  31. // Checks whether the provided PC belongs to one of the ignored libraries.
  32. bool IsIgnored(uptr pc) const;
  33. private:
  34. struct Lib {
  35. char *templ;
  36. char *name;
  37. char *real_name; // target of symlink
  38. bool loaded;
  39. };
  40. struct LibCodeRange {
  41. uptr begin;
  42. uptr end;
  43. };
  44. static const uptr kMaxLibs = 128;
  45. // Hot part:
  46. atomic_uintptr_t loaded_count_;
  47. LibCodeRange code_ranges_[kMaxLibs];
  48. // Cold part:
  49. BlockingMutex mutex_;
  50. uptr count_;
  51. Lib libs_[kMaxLibs];
  52. // Disallow copying of LibIgnore objects.
  53. LibIgnore(const LibIgnore&); // not implemented
  54. void operator = (const LibIgnore&); // not implemented
  55. };
  56. inline bool LibIgnore::IsIgnored(uptr pc) const {
  57. const uptr n = atomic_load(&loaded_count_, memory_order_acquire);
  58. for (uptr i = 0; i < n; i++) {
  59. if (pc >= code_ranges_[i].begin && pc < code_ranges_[i].end)
  60. return true;
  61. }
  62. return false;
  63. }
  64. } // namespace __sanitizer
  65. #endif // SANITIZER_LIBIGNORE_H