2014-05-27 20:37:52 +08:00
|
|
|
//===-- sanitizer_coverage_mapping.cc -------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Mmap-based implementation of sanitizer coverage.
|
|
|
|
//
|
|
|
|
// This is part of the implementation of code coverage that does not require
|
|
|
|
// __sanitizer_cov_dump() call. Data is stored in 2 files per process.
|
|
|
|
//
|
|
|
|
// $pid.sancov.map describes process memory layout in the following text-based
|
|
|
|
// format:
|
|
|
|
// <pointer size in bits> // 1 line, 32 or 64
|
|
|
|
// <mapping start> <mapping end> <base address> <dso name> // repeated
|
|
|
|
// ...
|
|
|
|
// Mapping lines are NOT sorted. This file is updated every time memory layout
|
|
|
|
// is changed (i.e. in dlopen() and dlclose() interceptors).
|
|
|
|
//
|
|
|
|
// $pid.sancov.raw is a binary dump of PC values, sizeof(uptr) each. Again, not
|
|
|
|
// sorted. This file is extended by 64Kb at a time and mapped into memory. It
|
|
|
|
// contains one or more 0 words at the end, up to the next 64Kb aligned offset.
|
|
|
|
//
|
|
|
|
// To convert these 2 files to the usual .sancov format, run sancov.py rawunpack
|
|
|
|
// $pid.sancov.raw.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "sanitizer_allocator_internal.h"
|
|
|
|
#include "sanitizer_libc.h"
|
|
|
|
#include "sanitizer_procmaps.h"
|
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2014-06-04 20:13:54 +08:00
|
|
|
static const uptr kMaxTextSize = 64 * 1024;
|
2014-05-27 20:37:52 +08:00
|
|
|
|
2014-06-04 20:13:54 +08:00
|
|
|
struct CachedMapping {
|
|
|
|
public:
|
2014-06-05 22:38:53 +08:00
|
|
|
bool NeedsUpdate(uptr pc) {
|
2014-06-04 20:13:54 +08:00
|
|
|
int new_pid = internal_getpid();
|
2014-06-05 22:38:53 +08:00
|
|
|
if (last_pid == new_pid && pc && pc >= last_range_start &&
|
|
|
|
pc < last_range_end)
|
2014-06-04 20:13:54 +08:00
|
|
|
return false;
|
|
|
|
last_pid = new_pid;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-05 22:38:53 +08:00
|
|
|
void SetModuleRange(uptr start, uptr end) {
|
|
|
|
last_range_start = start;
|
|
|
|
last_range_end = end;
|
|
|
|
}
|
|
|
|
|
2014-06-04 20:13:54 +08:00
|
|
|
private:
|
2014-06-05 22:38:53 +08:00
|
|
|
uptr last_range_start, last_range_end;
|
2014-06-04 20:13:54 +08:00
|
|
|
int last_pid;
|
|
|
|
};
|
|
|
|
|
|
|
|
static CachedMapping cached_mapping;
|
2014-06-03 20:15:43 +08:00
|
|
|
static StaticSpinMutex mapping_mu;
|
|
|
|
|
2014-12-26 20:32:32 +08:00
|
|
|
void CovUpdateMapping(const char *coverage_dir, uptr caller_pc) {
|
|
|
|
if (!common_flags()->coverage_direct) return;
|
2014-05-27 20:37:52 +08:00
|
|
|
|
2014-06-03 20:15:43 +08:00
|
|
|
SpinMutexLock l(&mapping_mu);
|
2014-05-27 20:37:52 +08:00
|
|
|
|
2014-06-05 22:38:53 +08:00
|
|
|
if (!cached_mapping.NeedsUpdate(caller_pc))
|
|
|
|
return;
|
|
|
|
|
2014-06-03 20:15:43 +08:00
|
|
|
InternalScopedString text(kMaxTextSize);
|
2014-05-27 20:37:52 +08:00
|
|
|
|
2015-01-09 06:31:14 +08:00
|
|
|
{
|
|
|
|
InternalScopedBuffer<LoadedModule> modules(kMaxNumberOfModules);
|
|
|
|
CHECK(modules.data());
|
|
|
|
int n_modules = GetListOfModules(modules.data(), kMaxNumberOfModules,
|
|
|
|
/* filter */ 0);
|
|
|
|
|
|
|
|
text.append("%d\n", sizeof(uptr) * 8);
|
|
|
|
for (int i = 0; i < n_modules; ++i) {
|
|
|
|
const char *module_name = StripModuleName(modules[i].full_name());
|
|
|
|
uptr base = modules[i].base_address();
|
|
|
|
for (auto iter = modules[i].ranges(); iter.hasNext();) {
|
|
|
|
const auto *range = iter.next();
|
|
|
|
if (range->executable) {
|
|
|
|
uptr start = range->beg;
|
|
|
|
uptr end = range->end;
|
|
|
|
text.append("%zx %zx %zx %s\n", start, end, base, module_name);
|
|
|
|
if (caller_pc && caller_pc >= start && caller_pc < end)
|
|
|
|
cached_mapping.SetModuleRange(start, end);
|
|
|
|
}
|
2014-06-11 18:11:51 +08:00
|
|
|
}
|
2015-01-09 06:31:14 +08:00
|
|
|
modules[i].clear();
|
2014-05-27 20:37:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-03 20:15:43 +08:00
|
|
|
int err;
|
2014-12-26 20:32:32 +08:00
|
|
|
InternalScopedString tmp_path(64 + internal_strlen(coverage_dir));
|
2014-06-03 20:15:43 +08:00
|
|
|
uptr res = internal_snprintf((char *)tmp_path.data(), tmp_path.size(),
|
2014-12-26 20:32:32 +08:00
|
|
|
"%s/%zd.sancov.map.tmp", coverage_dir,
|
|
|
|
internal_getpid());
|
2014-06-03 20:15:43 +08:00
|
|
|
CHECK_LE(res, tmp_path.size());
|
2015-04-09 00:03:22 +08:00
|
|
|
fd_t map_fd = OpenFile(tmp_path.data(), WrOnly, &err);
|
|
|
|
if (map_fd == kInvalidFd) {
|
2015-04-01 02:16:42 +08:00
|
|
|
Report("Coverage: failed to open %s for writing: %d\n", tmp_path.data(),
|
2014-12-23 18:18:47 +08:00
|
|
|
err);
|
2014-06-03 20:15:43 +08:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2015-04-09 22:11:25 +08:00
|
|
|
if (!WriteToFile(map_fd, text.data(), text.length(), nullptr, &err)) {
|
2014-06-03 20:15:43 +08:00
|
|
|
Printf("sancov.map write failed: %d\n", err);
|
|
|
|
Die();
|
|
|
|
}
|
2015-04-09 20:37:05 +08:00
|
|
|
CloseFile(map_fd);
|
2014-05-27 20:37:52 +08:00
|
|
|
|
2014-12-26 20:32:32 +08:00
|
|
|
InternalScopedString path(64 + internal_strlen(coverage_dir));
|
2014-05-29 22:33:16 +08:00
|
|
|
res = internal_snprintf((char *)path.data(), path.size(), "%s/%zd.sancov.map",
|
2014-12-26 20:32:32 +08:00
|
|
|
coverage_dir, internal_getpid());
|
2014-05-29 22:33:16 +08:00
|
|
|
CHECK_LE(res, path.size());
|
2014-05-27 20:37:52 +08:00
|
|
|
res = internal_rename(tmp_path.data(), path.data());
|
|
|
|
if (internal_iserror(res, &err)) {
|
|
|
|
Printf("sancov.map rename failed: %d\n", err);
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __sanitizer
|