1 //===-- sanitizer_suppressions.cpp ----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Suppression parsing/matching code.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_suppressions.h"
15 #include "sanitizer_allocator_internal.h"
16 #include "sanitizer_common.h"
17 #include "sanitizer_flags.h"
18 #include "sanitizer_file.h"
19 #include "sanitizer_libc.h"
20 #include "sanitizer_placement_new.h"
22 namespace __sanitizer
{
24 SuppressionContext::SuppressionContext(const char *suppression_types
[],
25 int suppression_types_num
)
26 : suppression_types_(suppression_types
),
27 suppression_types_num_(suppression_types_num
),
29 CHECK_LE(suppression_types_num_
, kMaxSuppressionTypes
);
30 internal_memset(has_suppression_type_
, 0, suppression_types_num_
);
33 #if !SANITIZER_FUCHSIA
34 static bool GetPathAssumingFileIsRelativeToExec(const char *file_path
,
35 /*out*/char *new_file_path
,
36 uptr new_file_path_size
) {
37 InternalMmapVector
<char> exec(kMaxPathLength
);
38 if (ReadBinaryNameCached(exec
.data(), exec
.size())) {
39 const char *file_name_pos
= StripModuleName(exec
.data());
40 uptr path_to_exec_len
= file_name_pos
- exec
.data();
41 internal_strncat(new_file_path
, exec
.data(),
42 Min(path_to_exec_len
, new_file_path_size
- 1));
43 internal_strncat(new_file_path
, file_path
,
44 new_file_path_size
- internal_strlen(new_file_path
) - 1);
50 static const char *FindFile(const char *file_path
,
51 /*out*/char *new_file_path
,
52 uptr new_file_path_size
) {
53 // If we cannot find the file, check if its location is relative to
54 // the location of the executable.
55 if (!FileExists(file_path
) && !IsAbsolutePath(file_path
) &&
56 GetPathAssumingFileIsRelativeToExec(file_path
, new_file_path
,
57 new_file_path_size
)) {
63 static const char *FindFile(const char *file_path
, char *, uptr
) {
68 void SuppressionContext::ParseFromFile(const char *filename
) {
69 if (filename
[0] == '\0')
72 InternalMmapVector
<char> new_file_path(kMaxPathLength
);
73 filename
= FindFile(filename
, new_file_path
.data(), new_file_path
.size());
76 VPrintf(1, "%s: reading suppressions file at %s\n",
77 SanitizerToolName
, filename
);
81 if (!ReadFileToBuffer(filename
, &file_contents
, &buffer_size
,
83 Printf("%s: failed to read suppressions file '%s'\n", SanitizerToolName
,
89 UnmapOrDie(file_contents
, buffer_size
);
92 bool SuppressionContext::Match(const char *str
, const char *type
,
95 if (!HasSuppressionType(type
))
97 for (uptr i
= 0; i
< suppressions_
.size(); i
++) {
98 Suppression
&cur
= suppressions_
[i
];
99 if (0 == internal_strcmp(cur
.type
, type
) && TemplateMatch(cur
.templ
, str
)) {
107 static const char *StripPrefix(const char *str
, const char *prefix
) {
108 while (*str
&& *str
== *prefix
) {
117 void SuppressionContext::Parse(const char *str
) {
118 // Context must not mutate once Match has been called.
120 const char *line
= str
;
122 while (line
[0] == ' ' || line
[0] == '\t')
124 const char *end
= internal_strchr(line
, '\n');
126 end
= line
+ internal_strlen(line
);
127 if (line
!= end
&& line
[0] != '#') {
128 const char *end2
= end
;
129 while (line
!= end2
&&
130 (end2
[-1] == ' ' || end2
[-1] == '\t' || end2
[-1] == '\r'))
133 for (type
= 0; type
< suppression_types_num_
; type
++) {
134 const char *next_char
= StripPrefix(line
, suppression_types_
[type
]);
135 if (next_char
&& *next_char
== ':') {
140 if (type
== suppression_types_num_
) {
141 Printf("%s: failed to parse suppressions.\n", SanitizerToolName
);
142 Printf("Supported suppression types are:\n");
143 for (type
= 0; type
< suppression_types_num_
; type
++)
144 Printf("- %s\n", suppression_types_
[type
]);
148 s
.type
= suppression_types_
[type
];
149 s
.templ
= (char*)InternalAlloc(end2
- line
+ 1);
150 internal_memcpy(s
.templ
, line
, end2
- line
);
151 s
.templ
[end2
- line
] = 0;
152 suppressions_
.push_back(s
);
153 has_suppression_type_
[type
] = true;
161 uptr
SuppressionContext::SuppressionCount() const {
162 return suppressions_
.size();
165 bool SuppressionContext::HasSuppressionType(const char *type
) const {
166 for (int i
= 0; i
< suppression_types_num_
; i
++) {
167 if (0 == internal_strcmp(type
, suppression_types_
[i
]))
168 return has_suppression_type_
[i
];
173 const Suppression
*SuppressionContext::SuppressionAt(uptr i
) const {
174 CHECK_LT(i
, suppressions_
.size());
175 return &suppressions_
[i
];
178 void SuppressionContext::GetMatched(
179 InternalMmapVector
<Suppression
*> *matched
) {
180 for (uptr i
= 0; i
< suppressions_
.size(); i
++)
181 if (atomic_load_relaxed(&suppressions_
[i
].hit_count
))
182 matched
->push_back(&suppressions_
[i
]);
185 } // namespace __sanitizer