sanitizer_suppressions.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. }
  80. bool SuppressionContext::Match(const char *str, const char *type,
  81. Suppression **s) {
  82. can_parse_ = false;
  83. if (!HasSuppressionType(type))
  84. return false;
  85. for (uptr i = 0; i < suppressions_.size(); i++) {
  86. Suppression &cur = suppressions_[i];
  87. if (0 == internal_strcmp(cur.type, type) && TemplateMatch(cur.templ, str)) {
  88. *s = &cur;
  89. return true;
  90. }
  91. }
  92. return false;
  93. }
  94. static const char *StripPrefix(const char *str, const char *prefix) {
  95. while (*str && *str == *prefix) {
  96. str++;
  97. prefix++;
  98. }
  99. if (!*prefix)
  100. return str;
  101. return 0;
  102. }
  103. void SuppressionContext::Parse(const char *str) {
  104. // Context must not mutate once Match has been called.
  105. CHECK(can_parse_);
  106. const char *line = str;
  107. while (line) {
  108. while (line[0] == ' ' || line[0] == '\t')
  109. line++;
  110. const char *end = internal_strchr(line, '\n');
  111. if (end == 0)
  112. end = line + internal_strlen(line);
  113. if (line != end && line[0] != '#') {
  114. const char *end2 = end;
  115. while (line != end2 &&
  116. (end2[-1] == ' ' || end2[-1] == '\t' || end2[-1] == '\r'))
  117. end2--;
  118. int type;
  119. for (type = 0; type < suppression_types_num_; type++) {
  120. const char *next_char = StripPrefix(line, suppression_types_[type]);
  121. if (next_char && *next_char == ':') {
  122. line = ++next_char;
  123. break;
  124. }
  125. }
  126. if (type == suppression_types_num_) {
  127. Printf("%s: failed to parse suppressions\n", SanitizerToolName);
  128. Die();
  129. }
  130. Suppression s;
  131. s.type = suppression_types_[type];
  132. s.templ = (char*)InternalAlloc(end2 - line + 1);
  133. internal_memcpy(s.templ, line, end2 - line);
  134. s.templ[end2 - line] = 0;
  135. suppressions_.push_back(s);
  136. has_suppression_type_[type] = true;
  137. }
  138. if (end[0] == 0)
  139. break;
  140. line = end + 1;
  141. }
  142. }
  143. uptr SuppressionContext::SuppressionCount() const {
  144. return suppressions_.size();
  145. }
  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];
  150. }
  151. return false;
  152. }
  153. const Suppression *SuppressionContext::SuppressionAt(uptr i) const {
  154. CHECK_LT(i, suppressions_.size());
  155. return &suppressions_[i];
  156. }
  157. void SuppressionContext::GetMatched(
  158. InternalMmapVector<Suppression *> *matched) {
  159. for (uptr i = 0; i < suppressions_.size(); i++)
  160. if (atomic_load_relaxed(&suppressions_[i].hit_count))
  161. matched->push_back(&suppressions_[i]);
  162. }
  163. } // namespace __sanitizer