]> git.ipfire.org Git - thirdparty/gcc.git/blame - libsanitizer/sanitizer_common/sanitizer_libignore.h
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
[thirdparty/gcc.git] / libsanitizer / sanitizer_common / sanitizer_libignore.h
CommitLineData
df77f0e4
KS
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
696d846a
MO
9// of dynamic libraries. LibIgnore can be initialized with several templates
10// of names of libraries to be ignored. It finds code ranges for the libraries;
df77f0e4
KS
11// and checks whether the provided PC value belongs to the code ranges.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef SANITIZER_LIBIGNORE_H
16#define SANITIZER_LIBIGNORE_H
17
18#include "sanitizer_internal_defs.h"
19#include "sanitizer_common.h"
df77f0e4
KS
20#include "sanitizer_atomic.h"
21#include "sanitizer_mutex.h"
22
23namespace __sanitizer {
24
25class LibIgnore {
26 public:
27 explicit LibIgnore(LinkerInitialized);
28
696d846a
MO
29 // Must be called during initialization.
30 void AddIgnoredLibrary(const char *name_templ);
5d3805fc
JJ
31 void IgnoreNoninstrumentedModules(bool enable) {
32 track_instrumented_libs_ = enable;
33 }
df77f0e4
KS
34
35 // Must be called after a new dynamic library is loaded.
36 void OnLibraryLoaded(const char *name);
37
38 // Must be called after a dynamic library is unloaded.
39 void OnLibraryUnloaded();
40
5d3805fc
JJ
41 // Checks whether the provided PC belongs to one of the ignored libraries or
42 // the PC should be ignored because it belongs to an non-instrumented module
43 // (when ignore_noninstrumented_modules=1). Also returns true via
44 // "pc_in_ignored_lib" if the PC is in an ignored library, false otherwise.
45 bool IsIgnored(uptr pc, bool *pc_in_ignored_lib) const;
46
47 // Checks whether the provided PC belongs to an instrumented module.
48 bool IsPcInstrumented(uptr pc) const;
df77f0e4
KS
49
50 private:
51 struct Lib {
52 char *templ;
53 char *name;
54 char *real_name; // target of symlink
55 bool loaded;
56 };
57
58 struct LibCodeRange {
59 uptr begin;
60 uptr end;
61 };
62
5d3805fc
JJ
63 inline bool IsInRange(uptr pc, const LibCodeRange &range) const {
64 return (pc >= range.begin && pc < range.end);
65 }
66
df77f0e4
KS
67 static const uptr kMaxLibs = 128;
68
69 // Hot part:
5d3805fc
JJ
70 atomic_uintptr_t ignored_ranges_count_;
71 LibCodeRange ignored_code_ranges_[kMaxLibs];
72
73 atomic_uintptr_t instrumented_ranges_count_;
74 LibCodeRange instrumented_code_ranges_[kMaxLibs];
df77f0e4
KS
75
76 // Cold part:
77 BlockingMutex mutex_;
78 uptr count_;
79 Lib libs_[kMaxLibs];
5d3805fc 80 bool track_instrumented_libs_;
df77f0e4
KS
81
82 // Disallow copying of LibIgnore objects.
83 LibIgnore(const LibIgnore&); // not implemented
84 void operator = (const LibIgnore&); // not implemented
85};
86
5d3805fc
JJ
87inline bool LibIgnore::IsIgnored(uptr pc, bool *pc_in_ignored_lib) const {
88 const uptr n = atomic_load(&ignored_ranges_count_, memory_order_acquire);
89 for (uptr i = 0; i < n; i++) {
90 if (IsInRange(pc, ignored_code_ranges_[i])) {
91 *pc_in_ignored_lib = true;
92 return true;
93 }
94 }
95 *pc_in_ignored_lib = false;
96 if (track_instrumented_libs_ && !IsPcInstrumented(pc))
97 return true;
98 return false;
99}
100
101inline bool LibIgnore::IsPcInstrumented(uptr pc) const {
102 const uptr n = atomic_load(&instrumented_ranges_count_, memory_order_acquire);
df77f0e4 103 for (uptr i = 0; i < n; i++) {
5d3805fc 104 if (IsInRange(pc, instrumented_code_ranges_[i]))
df77f0e4
KS
105 return true;
106 }
107 return false;
108}
109
110} // namespace __sanitizer
111
112#endif // SANITIZER_LIBIGNORE_H