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