10b57cec5SDimitry Andric //===-- sanitizer_libignore.h -----------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // LibIgnore allows to ignore all interceptors called from a particular set
100b57cec5SDimitry Andric // of dynamic libraries. LibIgnore can be initialized with several templates
110b57cec5SDimitry Andric // of names of libraries to be ignored. It finds code ranges for the libraries;
120b57cec5SDimitry Andric // and checks whether the provided PC value belongs to the code ranges.
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #ifndef SANITIZER_LIBIGNORE_H
170b57cec5SDimitry Andric #define SANITIZER_LIBIGNORE_H
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric #include "sanitizer_internal_defs.h"
200b57cec5SDimitry Andric #include "sanitizer_common.h"
210b57cec5SDimitry Andric #include "sanitizer_atomic.h"
220b57cec5SDimitry Andric #include "sanitizer_mutex.h"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric namespace __sanitizer {
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric class LibIgnore {
270b57cec5SDimitry Andric  public:
280b57cec5SDimitry Andric   explicit LibIgnore(LinkerInitialized);
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric   // Must be called during initialization.
310b57cec5SDimitry Andric   void AddIgnoredLibrary(const char *name_templ);
IgnoreNoninstrumentedModules(bool enable)320b57cec5SDimitry Andric   void IgnoreNoninstrumentedModules(bool enable) {
330b57cec5SDimitry Andric     track_instrumented_libs_ = enable;
340b57cec5SDimitry Andric   }
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric   // Must be called after a new dynamic library is loaded.
370b57cec5SDimitry Andric   void OnLibraryLoaded(const char *name);
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric   // Must be called after a dynamic library is unloaded.
400b57cec5SDimitry Andric   void OnLibraryUnloaded();
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric   // Checks whether the provided PC belongs to one of the ignored libraries or
430b57cec5SDimitry Andric   // the PC should be ignored because it belongs to an non-instrumented module
440b57cec5SDimitry Andric   // (when ignore_noninstrumented_modules=1). Also returns true via
450b57cec5SDimitry Andric   // "pc_in_ignored_lib" if the PC is in an ignored library, false otherwise.
460b57cec5SDimitry Andric   bool IsIgnored(uptr pc, bool *pc_in_ignored_lib) const;
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric   // Checks whether the provided PC belongs to an instrumented module.
490b57cec5SDimitry Andric   bool IsPcInstrumented(uptr pc) const;
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric  private:
520b57cec5SDimitry Andric   struct Lib {
530b57cec5SDimitry Andric     char *templ;
540b57cec5SDimitry Andric     char *name;
550b57cec5SDimitry Andric     char *real_name;  // target of symlink
560b57cec5SDimitry Andric     bool loaded;
570b57cec5SDimitry Andric   };
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   struct LibCodeRange {
600b57cec5SDimitry Andric     uptr begin;
610b57cec5SDimitry Andric     uptr end;
620b57cec5SDimitry Andric   };
630b57cec5SDimitry Andric 
IsInRange(uptr pc,const LibCodeRange & range)640b57cec5SDimitry Andric   inline bool IsInRange(uptr pc, const LibCodeRange &range) const {
650b57cec5SDimitry Andric     return (pc >= range.begin && pc < range.end);
660b57cec5SDimitry Andric   }
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   static const uptr kMaxIgnoredRanges = 128;
690b57cec5SDimitry Andric   static const uptr kMaxInstrumentedRanges = 1024;
700b57cec5SDimitry Andric   static const uptr kMaxLibs = 1024;
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric   // Hot part:
730b57cec5SDimitry Andric   atomic_uintptr_t ignored_ranges_count_;
740b57cec5SDimitry Andric   LibCodeRange ignored_code_ranges_[kMaxIgnoredRanges];
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric   atomic_uintptr_t instrumented_ranges_count_;
770b57cec5SDimitry Andric   LibCodeRange instrumented_code_ranges_[kMaxInstrumentedRanges];
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric   // Cold part:
80349cc55cSDimitry Andric   Mutex mutex_;
810b57cec5SDimitry Andric   uptr count_;
820b57cec5SDimitry Andric   Lib libs_[kMaxLibs];
830b57cec5SDimitry Andric   bool track_instrumented_libs_;
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   // Disallow copying of LibIgnore objects.
860b57cec5SDimitry Andric   LibIgnore(const LibIgnore&);  // not implemented
870b57cec5SDimitry Andric   void operator = (const LibIgnore&);  // not implemented
880b57cec5SDimitry Andric };
890b57cec5SDimitry Andric 
IsIgnored(uptr pc,bool * pc_in_ignored_lib)900b57cec5SDimitry Andric inline bool LibIgnore::IsIgnored(uptr pc, bool *pc_in_ignored_lib) const {
910b57cec5SDimitry Andric   const uptr n = atomic_load(&ignored_ranges_count_, memory_order_acquire);
920b57cec5SDimitry Andric   for (uptr i = 0; i < n; i++) {
930b57cec5SDimitry Andric     if (IsInRange(pc, ignored_code_ranges_[i])) {
940b57cec5SDimitry Andric       *pc_in_ignored_lib = true;
950b57cec5SDimitry Andric       return true;
960b57cec5SDimitry Andric     }
970b57cec5SDimitry Andric   }
980b57cec5SDimitry Andric   *pc_in_ignored_lib = false;
990b57cec5SDimitry Andric   if (track_instrumented_libs_ && !IsPcInstrumented(pc))
1000b57cec5SDimitry Andric     return true;
1010b57cec5SDimitry Andric   return false;
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric 
IsPcInstrumented(uptr pc)1040b57cec5SDimitry Andric inline bool LibIgnore::IsPcInstrumented(uptr pc) const {
1050b57cec5SDimitry Andric   const uptr n = atomic_load(&instrumented_ranges_count_, memory_order_acquire);
1060b57cec5SDimitry Andric   for (uptr i = 0; i < n; i++) {
1070b57cec5SDimitry Andric     if (IsInRange(pc, instrumented_code_ranges_[i]))
1080b57cec5SDimitry Andric       return true;
1090b57cec5SDimitry Andric   }
1100b57cec5SDimitry Andric   return false;
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric }  // namespace __sanitizer
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric #endif  // SANITIZER_LIBIGNORE_H
116