sanitizer_coverage_mapping_libcdep.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //===-- sanitizer_coverage_mapping.cc -------------------------------------===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // Mmap-based implementation of sanitizer coverage.
  9. //
  10. // This is part of the implementation of code coverage that does not require
  11. // __sanitizer_cov_dump() call. Data is stored in 2 files per process.
  12. //
  13. // $pid.sancov.map describes process memory layout in the following text-based
  14. // format:
  15. // <pointer size in bits> // 1 line, 32 or 64
  16. // <mapping start> <mapping end> <base address> <dso name> // repeated
  17. // ...
  18. // Mapping lines are NOT sorted. This file is updated every time memory layout
  19. // is changed (i.e. in dlopen() and dlclose() interceptors).
  20. //
  21. // $pid.sancov.raw is a binary dump of PC values, sizeof(uptr) each. Again, not
  22. // sorted. This file is extended by 64Kb at a time and mapped into memory. It
  23. // contains one or more 0 words at the end, up to the next 64Kb aligned offset.
  24. //
  25. // To convert these 2 files to the usual .sancov format, run sancov.py rawunpack
  26. // $pid.sancov.raw.
  27. //
  28. //===----------------------------------------------------------------------===//
  29. #include "sanitizer_allocator_internal.h"
  30. #include "sanitizer_libc.h"
  31. #include "sanitizer_procmaps.h"
  32. namespace __sanitizer {
  33. static const uptr kMaxNumberOfModules = 1 << 14;
  34. static const uptr kMaxTextSize = 64 * 1024;
  35. struct CachedMapping {
  36. public:
  37. bool NeedsUpdate(uptr pc) {
  38. int new_pid = internal_getpid();
  39. if (last_pid == new_pid && pc && pc >= last_range_start &&
  40. pc < last_range_end)
  41. return false;
  42. last_pid = new_pid;
  43. return true;
  44. }
  45. void SetModuleRange(uptr start, uptr end) {
  46. last_range_start = start;
  47. last_range_end = end;
  48. }
  49. private:
  50. uptr last_range_start, last_range_end;
  51. int last_pid;
  52. };
  53. static CachedMapping cached_mapping;
  54. static StaticSpinMutex mapping_mu;
  55. void CovUpdateMapping(uptr caller_pc) {
  56. if (!common_flags()->coverage || !common_flags()->coverage_direct) return;
  57. SpinMutexLock l(&mapping_mu);
  58. if (!cached_mapping.NeedsUpdate(caller_pc))
  59. return;
  60. InternalScopedString text(kMaxTextSize);
  61. InternalScopedBuffer<char> modules_data(kMaxNumberOfModules *
  62. sizeof(LoadedModule));
  63. LoadedModule *modules = (LoadedModule *)modules_data.data();
  64. CHECK(modules);
  65. int n_modules = GetListOfModules(modules, kMaxNumberOfModules,
  66. /* filter */ 0);
  67. text.append("%d\n", sizeof(uptr) * 8);
  68. for (int i = 0; i < n_modules; ++i) {
  69. const char *module_name = StripModuleName(modules[i].full_name());
  70. for (unsigned j = 0; j < modules[i].n_ranges(); ++j) {
  71. if (modules[i].address_range_executable(j)) {
  72. uptr start = modules[i].address_range_start(j);
  73. uptr end = modules[i].address_range_end(j);
  74. uptr base = modules[i].base_address();
  75. text.append("%zx %zx %zx %s\n", start, end, base, module_name);
  76. if (caller_pc && caller_pc >= start && caller_pc < end)
  77. cached_mapping.SetModuleRange(start, end);
  78. }
  79. }
  80. }
  81. int err;
  82. InternalScopedString tmp_path(64 +
  83. internal_strlen(common_flags()->coverage_dir));
  84. uptr res = internal_snprintf((char *)tmp_path.data(), tmp_path.size(),
  85. "%s/%zd.sancov.map.tmp", common_flags()->coverage_dir,
  86. internal_getpid());
  87. CHECK_LE(res, tmp_path.size());
  88. uptr map_fd = OpenFile(tmp_path.data(), true);
  89. if (internal_iserror(map_fd)) {
  90. Report(" Coverage: failed to open %s for writing\n", tmp_path.data());
  91. Die();
  92. }
  93. res = internal_write(map_fd, text.data(), text.length());
  94. if (internal_iserror(res, &err)) {
  95. Printf("sancov.map write failed: %d\n", err);
  96. Die();
  97. }
  98. internal_close(map_fd);
  99. InternalScopedString path(64 + internal_strlen(common_flags()->coverage_dir));
  100. res = internal_snprintf((char *)path.data(), path.size(), "%s/%zd.sancov.map",
  101. common_flags()->coverage_dir, internal_getpid());
  102. CHECK_LE(res, path.size());
  103. res = internal_rename(tmp_path.data(), path.data());
  104. if (internal_iserror(res, &err)) {
  105. Printf("sancov.map rename failed: %d\n", err);
  106. Die();
  107. }
  108. }
  109. } // namespace __sanitizer