1 //===-- sanitizer_suppressions.cc -----------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Suppression parsing/matching code.
10 //===----------------------------------------------------------------------===//
12 #include "sanitizer_suppressions.h"
14 #include "sanitizer_allocator_internal.h"
15 #include "sanitizer_common.h"
16 #include "sanitizer_flags.h"
17 #include "sanitizer_libc.h"
18 #include "sanitizer_placement_new.h"
20 namespace __sanitizer
{
22 SuppressionContext::SuppressionContext(const char *suppression_types
[],
23 int suppression_types_num
)
24 : suppression_types_(suppression_types
),
25 suppression_types_num_(suppression_types_num
), suppressions_(1),
27 CHECK_LE(suppression_types_num_
, kMaxSuppressionTypes
);
28 internal_memset(has_suppression_type_
, 0, suppression_types_num_
);
31 static bool GetPathAssumingFileIsRelativeToExec(const char *file_path
,
32 /*out*/char *new_file_path
,
33 uptr new_file_path_size
) {
34 InternalScopedString
exec(kMaxPathLength
);
35 if (ReadBinaryNameCached(exec
.data(), exec
.size())) {
36 const char *file_name_pos
= StripModuleName(exec
.data());
37 uptr path_to_exec_len
= file_name_pos
- exec
.data();
38 internal_strncat(new_file_path
, exec
.data(),
39 Min(path_to_exec_len
, new_file_path_size
- 1));
40 internal_strncat(new_file_path
, file_path
,
41 new_file_path_size
- internal_strlen(new_file_path
) - 1);
47 void SuppressionContext::ParseFromFile(const char *filename
) {
48 if (filename
[0] == '\0')
51 // If we cannot find the file, check if its location is relative to
52 // the location of the executable.
53 InternalScopedString
new_file_path(kMaxPathLength
);
54 if (!FileExists(filename
) && !IsAbsolutePath(filename
) &&
55 GetPathAssumingFileIsRelativeToExec(filename
, new_file_path
.data(),
56 new_file_path
.size())) {
57 filename
= new_file_path
.data();
61 VPrintf(1, "%s: reading suppressions file at %s\n",
62 SanitizerToolName
, filename
);
66 if (!ReadFileToBuffer(filename
, &file_contents
, &buffer_size
,
68 Printf("%s: failed to read suppressions file '%s'\n", SanitizerToolName
,
76 bool SuppressionContext::Match(const char *str
, const char *type
,
79 if (!HasSuppressionType(type
))
81 for (uptr i
= 0; i
< suppressions_
.size(); i
++) {
82 Suppression
&cur
= suppressions_
[i
];
83 if (0 == internal_strcmp(cur
.type
, type
) && TemplateMatch(cur
.templ
, str
)) {
91 static const char *StripPrefix(const char *str
, const char *prefix
) {
92 while (str
&& *str
== *prefix
) {
101 void SuppressionContext::Parse(const char *str
) {
102 // Context must not mutate once Match has been called.
104 const char *line
= str
;
106 while (line
[0] == ' ' || line
[0] == '\t')
108 const char *end
= internal_strchr(line
, '\n');
110 end
= line
+ internal_strlen(line
);
111 if (line
!= end
&& line
[0] != '#') {
112 const char *end2
= end
;
113 while (line
!= end2
&&
114 (end2
[-1] == ' ' || end2
[-1] == '\t' || end2
[-1] == '\r'))
117 for (type
= 0; type
< suppression_types_num_
; type
++) {
118 const char *next_char
= StripPrefix(line
, suppression_types_
[type
]);
119 if (next_char
&& *next_char
== ':') {
124 if (type
== suppression_types_num_
) {
125 Printf("%s: failed to parse suppressions\n", SanitizerToolName
);
129 s
.type
= suppression_types_
[type
];
130 s
.templ
= (char*)InternalAlloc(end2
- line
+ 1);
131 internal_memcpy(s
.templ
, line
, end2
- line
);
132 s
.templ
[end2
- line
] = 0;
133 suppressions_
.push_back(s
);
134 has_suppression_type_
[type
] = true;
142 uptr
SuppressionContext::SuppressionCount() const {
143 return suppressions_
.size();
146 bool SuppressionContext::HasSuppressionType(const char *type
) const {
147 for (int i
= 0; i
< suppression_types_num_
; i
++) {
148 if (0 == internal_strcmp(type
, suppression_types_
[i
]))
149 return has_suppression_type_
[i
];
154 const Suppression
*SuppressionContext::SuppressionAt(uptr i
) const {
155 CHECK_LT(i
, suppressions_
.size());
156 return &suppressions_
[i
];
159 void SuppressionContext::GetMatched(
160 InternalMmapVector
<Suppression
*> *matched
) {
161 for (uptr i
= 0; i
< suppressions_
.size(); i
++)
162 if (atomic_load_relaxed(&suppressions_
[i
].hit_count
))
163 matched
->push_back(&suppressions_
[i
]);
166 } // namespace __sanitizer