sanitizer_suppressions.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //===-- sanitizer_suppressions.cpp ----------------------------------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Suppression parsing/matching code.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_suppressions.h"
  13. #include "sanitizer_allocator_internal.h"
  14. #include "sanitizer_common.h"
  15. #include "sanitizer_flags.h"
  16. #include "sanitizer_file.h"
  17. #include "sanitizer_libc.h"
  18. #include "sanitizer_placement_new.h"
  19. namespace __sanitizer {
  20. SuppressionContext::SuppressionContext(const char *suppression_types[],
  21. int suppression_types_num)
  22. : suppression_types_(suppression_types),
  23. suppression_types_num_(suppression_types_num),
  24. can_parse_(true) {
  25. CHECK_LE(suppression_types_num_, kMaxSuppressionTypes);
  26. internal_memset(has_suppression_type_, 0, suppression_types_num_);
  27. }
  28. #if !SANITIZER_FUCHSIA
  29. static bool GetPathAssumingFileIsRelativeToExec(const char *file_path,
  30. /*out*/char *new_file_path,
  31. uptr new_file_path_size) {
  32. InternalMmapVector<char> exec(kMaxPathLength);
  33. if (ReadBinaryNameCached(exec.data(), exec.size())) {
  34. const char *file_name_pos = StripModuleName(exec.data());
  35. uptr path_to_exec_len = file_name_pos - exec.data();
  36. internal_strncat(new_file_path, exec.data(),
  37. Min(path_to_exec_len, new_file_path_size - 1));
  38. internal_strncat(new_file_path, file_path,
  39. new_file_path_size - internal_strlen(new_file_path) - 1);
  40. return true;
  41. }
  42. return false;
  43. }
  44. static const char *FindFile(const char *file_path,
  45. /*out*/char *new_file_path,
  46. uptr new_file_path_size) {
  47. // If we cannot find the file, check if its location is relative to
  48. // the location of the executable.
  49. if (!FileExists(file_path) && !IsAbsolutePath(file_path) &&
  50. GetPathAssumingFileIsRelativeToExec(file_path, new_file_path,
  51. new_file_path_size)) {
  52. return new_file_path;
  53. }
  54. return file_path;
  55. }
  56. #else
  57. static const char *FindFile(const char *file_path, char *, uptr) {
  58. return file_path;
  59. }
  60. #endif
  61. void SuppressionContext::ParseFromFile(const char *filename) {
  62. if (filename[0] == '\0')
  63. return;
  64. InternalMmapVector<char> new_file_path(kMaxPathLength);
  65. filename = FindFile(filename, new_file_path.data(), new_file_path.size());
  66. // Read the file.
  67. VPrintf(1, "%s: reading suppressions file at %s\n",
  68. SanitizerToolName, filename);
  69. char *file_contents;
  70. uptr buffer_size;
  71. uptr contents_size;
  72. if (!ReadFileToBuffer(filename, &file_contents, &buffer_size,
  73. &contents_size)) {
  74. Printf("%s: failed to read suppressions file '%s'\n", SanitizerToolName,
  75. filename);
  76. Die();
  77. }
  78. Parse(file_contents);
  79. UnmapOrDie(file_contents, contents_size);
  80. }
  81. bool SuppressionContext::Match(const char *str, const char *type,
  82. Suppression **s) {
  83. can_parse_ = false;
  84. if (!HasSuppressionType(type))
  85. return false;
  86. for (uptr i = 0; i < suppressions_.size(); i++) {
  87. Suppression &cur = suppressions_[i];
  88. if (0 == internal_strcmp(cur.type, type) && TemplateMatch(cur.templ, str)) {
  89. *s = &cur;
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. static const char *StripPrefix(const char *str, const char *prefix) {
  96. while (*str && *str == *prefix) {
  97. str++;
  98. prefix++;
  99. }
  100. if (!*prefix)
  101. return str;
  102. return 0;
  103. }
  104. void SuppressionContext::Parse(const char *str) {
  105. // Context must not mutate once Match has been called.
  106. CHECK(can_parse_);
  107. const char *line = str;
  108. while (line) {
  109. while (line[0] == ' ' || line[0] == '\t')
  110. line++;
  111. const char *end = internal_strchr(line, '\n');
  112. if (end == 0)
  113. end = line + internal_strlen(line);
  114. if (line != end && line[0] != '#') {
  115. const char *end2 = end;
  116. while (line != end2 &&
  117. (end2[-1] == ' ' || end2[-1] == '\t' || end2[-1] == '\r'))
  118. end2--;
  119. int type;
  120. for (type = 0; type < suppression_types_num_; type++) {
  121. const char *next_char = StripPrefix(line, suppression_types_[type]);
  122. if (next_char && *next_char == ':') {
  123. line = ++next_char;
  124. break;
  125. }
  126. }
  127. if (type == suppression_types_num_) {
  128. Printf("%s: failed to parse suppressions.\n", SanitizerToolName);
  129. Printf("Supported suppression types are:\n");
  130. for (type = 0; type < suppression_types_num_; type++)
  131. Printf("- %s\n", suppression_types_[type]);
  132. Die();
  133. }
  134. Suppression s;
  135. s.type = suppression_types_[type];
  136. s.templ = (char*)InternalAlloc(end2 - line + 1);
  137. internal_memcpy(s.templ, line, end2 - line);
  138. s.templ[end2 - line] = 0;
  139. suppressions_.push_back(s);
  140. has_suppression_type_[type] = true;
  141. }
  142. if (end[0] == 0)
  143. break;
  144. line = end + 1;
  145. }
  146. }
  147. uptr SuppressionContext::SuppressionCount() const {
  148. return suppressions_.size();
  149. }
  150. bool SuppressionContext::HasSuppressionType(const char *type) const {
  151. for (int i = 0; i < suppression_types_num_; i++) {
  152. if (0 == internal_strcmp(type, suppression_types_[i]))
  153. return has_suppression_type_[i];
  154. }
  155. return false;
  156. }
  157. const Suppression *SuppressionContext::SuppressionAt(uptr i) const {
  158. CHECK_LT(i, suppressions_.size());
  159. return &suppressions_[i];
  160. }
  161. void SuppressionContext::GetMatched(
  162. InternalMmapVector<Suppression *> *matched) {
  163. for (uptr i = 0; i < suppressions_.size(); i++)
  164. if (atomic_load_relaxed(&suppressions_[i].hit_count))
  165. matched->push_back(&suppressions_[i]);
  166. }
  167. } // namespace __sanitizer