2016-12-13 07:45:38 +08:00
|
|
|
//===-- sanitizer_coverage_libcdep_new.cc ---------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Sanitizer Coverage Controller for Trace PC Guard.
|
|
|
|
|
2017-08-02 15:51:38 +08:00
|
|
|
#include "sanitizer_platform.h"
|
|
|
|
|
|
|
|
#if !SANITIZER_FUCHSIA
|
2017-01-07 03:22:50 +08:00
|
|
|
#include "sancov_flags.h"
|
2017-01-12 09:37:35 +08:00
|
|
|
#include "sanitizer_allocator_internal.h"
|
|
|
|
#include "sanitizer_atomic.h"
|
|
|
|
#include "sanitizer_common.h"
|
2017-07-22 09:46:40 +08:00
|
|
|
#include "sanitizer_file.h"
|
2017-01-12 09:37:35 +08:00
|
|
|
#include "sanitizer_symbolizer.h"
|
2016-12-13 07:45:38 +08:00
|
|
|
|
|
|
|
using namespace __sanitizer;
|
|
|
|
|
|
|
|
using AddressRange = LoadedModule::AddressRange;
|
|
|
|
|
2017-01-07 03:22:50 +08:00
|
|
|
namespace __sancov {
|
2016-12-13 07:45:38 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
static const u64 Magic64 = 0xC0BFFFFFFFFFFF64ULL;
|
|
|
|
static const u64 Magic32 = 0xC0BFFFFFFFFFFF32ULL;
|
|
|
|
static const u64 Magic = SANITIZER_WORDSIZE == 64 ? Magic64 : Magic32;
|
|
|
|
|
|
|
|
static fd_t OpenFile(const char* path) {
|
|
|
|
error_t err;
|
|
|
|
fd_t fd = OpenFile(path, WrOnly, &err);
|
|
|
|
if (fd == kInvalidFd)
|
|
|
|
Report("SanitizerCoverage: failed to open %s for writing (reason: %d)\n",
|
|
|
|
path, err);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GetCoverageFilename(char* path, const char* name,
|
|
|
|
const char* extension) {
|
|
|
|
CHECK(name);
|
|
|
|
internal_snprintf(path, kMaxPathLength, "%s/%s.%zd.%s",
|
|
|
|
common_flags()->coverage_dir, name, internal_getpid(),
|
|
|
|
extension);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WriteModuleCoverage(char* file_path, const char* module_name,
|
|
|
|
const uptr* pcs, uptr len) {
|
|
|
|
GetCoverageFilename(file_path, StripModuleName(module_name), "sancov");
|
|
|
|
fd_t fd = OpenFile(file_path);
|
|
|
|
WriteToFile(fd, &Magic, sizeof(Magic));
|
|
|
|
WriteToFile(fd, pcs, len * sizeof(*pcs));
|
|
|
|
CloseFile(fd);
|
2017-06-01 02:26:32 +08:00
|
|
|
Printf("SanitizerCoverage: %s: %zd PCs written\n", file_path, len);
|
2016-12-13 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void SanitizerDumpCoverage(const uptr* unsorted_pcs, uptr len) {
|
|
|
|
if (!len) return;
|
|
|
|
|
|
|
|
char* file_path = static_cast<char*>(InternalAlloc(kMaxPathLength));
|
|
|
|
char* module_name = static_cast<char*>(InternalAlloc(kMaxPathLength));
|
|
|
|
uptr* pcs = static_cast<uptr*>(InternalAlloc(len * sizeof(uptr)));
|
|
|
|
|
|
|
|
internal_memcpy(pcs, unsorted_pcs, len * sizeof(uptr));
|
|
|
|
SortArray(pcs, len);
|
|
|
|
|
|
|
|
bool module_found = false;
|
|
|
|
uptr last_base = 0;
|
|
|
|
uptr module_start_idx = 0;
|
|
|
|
|
|
|
|
for (uptr i = 0; i < len; ++i) {
|
|
|
|
const uptr pc = pcs[i];
|
|
|
|
if (!pc) continue;
|
|
|
|
|
|
|
|
if (!__sanitizer_get_module_and_offset_for_pc(pc, nullptr, 0, &pcs[i])) {
|
2017-06-01 02:26:32 +08:00
|
|
|
Printf("ERROR: unknown pc 0x%x (may happen if dlclose is used)\n", pc);
|
2016-12-13 07:45:38 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uptr module_base = pc - pcs[i];
|
|
|
|
|
|
|
|
if (module_base != last_base || !module_found) {
|
|
|
|
if (module_found) {
|
|
|
|
WriteModuleCoverage(file_path, module_name, &pcs[module_start_idx],
|
|
|
|
i - module_start_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
last_base = module_base;
|
|
|
|
module_start_idx = i;
|
|
|
|
module_found = true;
|
|
|
|
__sanitizer_get_module_and_offset_for_pc(pc, module_name, kMaxPathLength,
|
|
|
|
&pcs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (module_found) {
|
|
|
|
WriteModuleCoverage(file_path, module_name, &pcs[module_start_idx],
|
|
|
|
len - module_start_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
InternalFree(file_path);
|
|
|
|
InternalFree(module_name);
|
|
|
|
InternalFree(pcs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collects trace-pc guard coverage.
|
|
|
|
// This class relies on zero-initialization.
|
|
|
|
class TracePcGuardController {
|
|
|
|
public:
|
|
|
|
void Initialize() {
|
|
|
|
CHECK(!initialized);
|
|
|
|
|
|
|
|
initialized = true;
|
2017-01-07 03:22:50 +08:00
|
|
|
InitializeSancovFlags();
|
|
|
|
|
2016-12-13 07:45:38 +08:00
|
|
|
pc_vector.Initialize(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitTracePcGuard(u32* start, u32* end) {
|
|
|
|
if (!initialized) Initialize();
|
|
|
|
CHECK(!*start);
|
|
|
|
CHECK_NE(start, end);
|
|
|
|
|
|
|
|
u32 i = pc_vector.size();
|
|
|
|
for (u32* p = start; p < end; p++) *p = ++i;
|
|
|
|
pc_vector.resize(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TracePcGuard(u32* guard, uptr pc) {
|
2017-07-28 07:22:37 +08:00
|
|
|
u32 idx = *guard;
|
2016-12-13 07:45:38 +08:00
|
|
|
if (!idx) return;
|
|
|
|
// we start indices from 1.
|
2017-07-28 07:22:37 +08:00
|
|
|
atomic_uintptr_t* pc_ptr =
|
|
|
|
reinterpret_cast<atomic_uintptr_t*>(&pc_vector[idx - 1]);
|
|
|
|
if (atomic_load(pc_ptr, memory_order_relaxed) == 0)
|
|
|
|
atomic_store(pc_ptr, pc, memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reset() {
|
|
|
|
internal_memset(&pc_vector[0], 0, sizeof(pc_vector[0]) * pc_vector.size());
|
2016-12-13 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Dump() {
|
2016-12-16 01:30:58 +08:00
|
|
|
if (!initialized || !common_flags()->coverage) return;
|
2016-12-13 07:45:38 +08:00
|
|
|
__sanitizer_dump_coverage(pc_vector.data(), pc_vector.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool initialized;
|
|
|
|
InternalMmapVectorNoCtor<uptr> pc_vector;
|
|
|
|
};
|
|
|
|
|
|
|
|
static TracePcGuardController pc_guard_controller;
|
|
|
|
|
2016-12-16 02:54:25 +08:00
|
|
|
} // namespace
|
2017-01-07 03:22:50 +08:00
|
|
|
} // namespace __sancov
|
2016-12-13 07:45:38 +08:00
|
|
|
|
2017-06-02 08:52:35 +08:00
|
|
|
namespace __sanitizer {
|
|
|
|
void InitializeCoverage(bool enabled, const char *dir) {
|
|
|
|
static bool coverage_enabled = false;
|
|
|
|
if (coverage_enabled)
|
|
|
|
return; // May happen if two sanitizer enable coverage in the same process.
|
|
|
|
coverage_enabled = enabled;
|
|
|
|
Atexit(__sanitizer_cov_dump);
|
|
|
|
AddDieCallback(__sanitizer_cov_dump);
|
|
|
|
}
|
|
|
|
} // namespace __sanitizer
|
|
|
|
|
2016-12-13 07:45:38 +08:00
|
|
|
extern "C" {
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_dump_coverage( // NOLINT
|
|
|
|
const uptr* pcs, uptr len) {
|
2017-01-07 03:22:50 +08:00
|
|
|
return __sancov::SanitizerDumpCoverage(pcs, len);
|
2016-12-13 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
2017-01-29 13:44:59 +08:00
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_guard, u32* guard) {
|
2016-12-13 07:45:38 +08:00
|
|
|
if (!*guard) return;
|
2017-01-07 03:22:50 +08:00
|
|
|
__sancov::pc_guard_controller.TracePcGuard(guard, GET_CALLER_PC() - 1);
|
2016-12-13 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
2017-01-29 13:44:59 +08:00
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_guard_init,
|
|
|
|
u32* start, u32* end) {
|
2016-12-13 07:45:38 +08:00
|
|
|
if (start == end || *start) return;
|
2017-01-07 03:22:50 +08:00
|
|
|
__sancov::pc_guard_controller.InitTracePcGuard(start, end);
|
2016-12-13 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_dump_trace_pc_guard_coverage() {
|
2017-01-07 03:22:50 +08:00
|
|
|
__sancov::pc_guard_controller.Dump();
|
2016-12-13 07:45:38 +08:00
|
|
|
}
|
2017-06-02 08:52:35 +08:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump() {
|
|
|
|
__sanitizer_dump_trace_pc_guard_coverage();
|
|
|
|
}
|
2017-07-28 07:22:37 +08:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_reset() {
|
|
|
|
__sancov::pc_guard_controller.Reset();
|
|
|
|
}
|
2017-06-02 08:52:35 +08:00
|
|
|
// Default empty implementations (weak). Users should redefine them.
|
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp, void) {}
|
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp1, void) {}
|
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp2, void) {}
|
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp4, void) {}
|
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp8, void) {}
|
2017-08-10 22:22:57 +08:00
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp1, void) {}
|
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp2, void) {}
|
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp4, void) {}
|
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp8, void) {}
|
2017-06-02 08:52:35 +08:00
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_switch, void) {}
|
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_div4, void) {}
|
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_div8, void) {}
|
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_gep, void) {}
|
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_indir, void) {}
|
2017-08-01 12:24:05 +08:00
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_8bit_counters_init, void) {}
|
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_pcs_init, void) {}
|
2016-12-13 07:45:38 +08:00
|
|
|
} // extern "C"
|
2017-08-31 06:49:31 +08:00
|
|
|
// Weak definition for code instrumented with -fsanitize-coverage=stack-depth
|
|
|
|
// and later linked with code containing a strong definition.
|
|
|
|
// E.g., -fsanitize=fuzzer-no-link
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
|
|
|
|
SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE uptr __sancov_lowest_stack;
|
2017-08-02 15:51:38 +08:00
|
|
|
|
|
|
|
#endif // !SANITIZER_FUCHSIA
|