Index: compiler-rt/trunk/lib/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/CMakeLists.txt +++ compiler-rt/trunk/lib/CMakeLists.txt @@ -36,6 +36,10 @@ endif() endfunction() +if(COMPILER_RT_HAS_SANITIZER_COMMON) + add_subdirectory(sancov) +endif() + if(COMPILER_RT_BUILD_SANITIZERS) compiler_rt_build_runtime(interception) Index: compiler-rt/trunk/lib/asan/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/asan/CMakeLists.txt +++ compiler-rt/trunk/lib/asan/CMakeLists.txt @@ -118,6 +118,7 @@ RTInterception RTSanitizerCommon RTSanitizerCommonLibc + RTSancovLibc RTLSanCommon RTUbsan CFLAGS ${ASAN_DYNAMIC_CFLAGS} @@ -131,6 +132,7 @@ RTInterception RTSanitizerCommon RTSanitizerCommonLibc + RTSancovLibc RTLSanCommon RTUbsan) Index: compiler-rt/trunk/lib/asan/tests/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/asan/tests/CMakeLists.txt +++ compiler-rt/trunk/lib/asan/tests/CMakeLists.txt @@ -281,6 +281,7 @@ $ $ $ + $ $ $) else() @@ -290,6 +291,7 @@ $ $ $ + $ $ $ $) @@ -342,6 +344,7 @@ $ $ $ + $ $ $ ${COMPILER_RT_GTEST_SOURCE} Index: compiler-rt/trunk/lib/cfi/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/cfi/CMakeLists.txt +++ compiler-rt/trunk/lib/cfi/CMakeLists.txt @@ -20,6 +20,7 @@ OBJECT_LIBS RTInterception RTSanitizerCommon RTSanitizerCommonLibc + RTSancovLibc CFLAGS ${CFI_CFLAGS} PARENT_TARGET cfi) add_compiler_rt_runtime(clang_rt.cfi_diag @@ -29,6 +30,7 @@ OBJECT_LIBS RTInterception RTSanitizerCommon RTSanitizerCommonLibc + RTSancovLibc RTUbsan CFLAGS ${CFI_CFLAGS} ${CFI_DIAG_CFLAGS} PARENT_TARGET cfi) Index: compiler-rt/trunk/lib/dfsan/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/dfsan/CMakeLists.txt +++ compiler-rt/trunk/lib/dfsan/CMakeLists.txt @@ -23,6 +23,7 @@ $ $ $ + $ CFLAGS ${DFSAN_CFLAGS} PARENT_TARGET dfsan) add_sanitizer_rt_symbols(clang_rt.dfsan Index: compiler-rt/trunk/lib/esan/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/esan/CMakeLists.txt +++ compiler-rt/trunk/lib/esan/CMakeLists.txt @@ -26,6 +26,7 @@ $ $ $ + $ CFLAGS ${ESAN_RTL_CFLAGS}) add_sanitizer_rt_symbols(clang_rt.esan ARCHS ${arch} Index: compiler-rt/trunk/lib/lsan/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/lsan/CMakeLists.txt +++ compiler-rt/trunk/lib/lsan/CMakeLists.txt @@ -32,6 +32,7 @@ $ $ $ + $ $ CFLAGS ${LSAN_CFLAGS} PARENT_TARGET lsan) Index: compiler-rt/trunk/lib/msan/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/msan/CMakeLists.txt +++ compiler-rt/trunk/lib/msan/CMakeLists.txt @@ -35,6 +35,7 @@ $ $ $ + $ $ CFLAGS ${MSAN_RTL_CFLAGS} PARENT_TARGET msan) Index: compiler-rt/trunk/lib/sancov/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/sancov/CMakeLists.txt +++ compiler-rt/trunk/lib/sancov/CMakeLists.txt @@ -0,0 +1,16 @@ +set(SANITIZER_CFLAGS ${SANITIZER_COMMON_CFLAGS}) +append_rtti_flag(OFF SANITIZER_CFLAGS) + +set(SANCOV_LIBCDEP_SOURCES + sancov_flags.cc + sanitizer_coverage_libcdep.cc) + +add_compiler_rt_object_libraries(RTSancovLibc + ${OS_OPTION} + ARCHS ${SANITIZER_COMMON_SUPPORTED_ARCH} + SOURCES ${SANCOV_LIBCDEP_SOURCES} + CFLAGS ${SANITIZER_CFLAGS} + DEFS ${SANITIZER_COMMON_DEFINITIONS}) + +include_directories(..) + Index: compiler-rt/trunk/lib/sancov/sancov_flags.h =================================================================== --- compiler-rt/trunk/lib/sancov/sancov_flags.h +++ compiler-rt/trunk/lib/sancov/sancov_flags.h @@ -0,0 +1,40 @@ +//===-- sancov_flags.h ------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Sanitizer Coverage runtime flags. +// +//===----------------------------------------------------------------------===// +#ifndef SANCOV_FLAGS_H +#define SANCOV_FLAGS_H + +#include "sanitizer_common/sanitizer_flag_parser.h" +#include "sanitizer_common/sanitizer_internal_defs.h" + +namespace __sancov { + +struct SancovFlags { +#define SANCOV_FLAG(Type, Name, DefaultValue, Description) Type Name; +#include "sancov_flags.inc" +#undef SANCOV_FLAG + + void SetDefaults(); +}; + +extern SancovFlags sancov_flags_dont_use_directly; + +inline SancovFlags* sancov_flags() { return &sancov_flags_dont_use_directly; } + +void InitializeSancovFlags(); + +extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE const char* +__sancov_default_options(); + +} // namespace __sancov + +#endif Index: compiler-rt/trunk/lib/sancov/sancov_flags.cc =================================================================== --- compiler-rt/trunk/lib/sancov/sancov_flags.cc +++ compiler-rt/trunk/lib/sancov/sancov_flags.cc @@ -0,0 +1,60 @@ +//===-- sancov_flags.cc -----------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Sanitizer Coverage runtime flags. +// +//===----------------------------------------------------------------------===// + +#include "sancov_flags.h" +#include "sanitizer_common/sanitizer_flag_parser.h" +#include "sanitizer_common/sanitizer_platform.h" + +#if !SANITIZER_LINUX +// other platforms do not have weak symbols out of the box. +extern "C" const char* __sancov_default_options() { return ""; } +#endif + +using namespace __sanitizer; + +namespace __sancov { + +SancovFlags sancov_flags_dont_use_directly; // use via flags(); + +void SancovFlags::SetDefaults() { +#define SANCOV_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue; +#include "sancov_flags.inc" +#undef SANCOV_FLAG +} + +static void RegisterSancovFlags(FlagParser *parser, SancovFlags *f) { +#define SANCOV_FLAG(Type, Name, DefaultValue, Description) \ + RegisterFlag(parser, #Name, Description, &f->Name); +#include "sancov_flags.inc" +#undef SANCOV_FLAG +} + +static const char *MaybeCallSancovDefaultOptions() { + return (&__sancov_default_options) ? __sancov_default_options() : ""; +} + +void InitializeSancovFlags() { + SancovFlags *f = sancov_flags(); + f->SetDefaults(); + + FlagParser parser; + RegisterSancovFlags(&parser, f); + + parser.ParseString(MaybeCallSancovDefaultOptions()); + parser.ParseString(GetEnv("SANCOV_OPTIONS")); + + ReportUnrecognizedFlags(); + if (f->help) parser.PrintFlagDescriptions(); +} + +} // namespace __sancov Index: compiler-rt/trunk/lib/sancov/sancov_flags.inc =================================================================== --- compiler-rt/trunk/lib/sancov/sancov_flags.inc +++ compiler-rt/trunk/lib/sancov/sancov_flags.inc @@ -0,0 +1,21 @@ +//===-- sancov_flags.inc ----------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Sanitizer Coverage runtime flags. +// +//===----------------------------------------------------------------------===// +#ifndef SANCOV_FLAG +#error "Defnine SANCOV_FLAG prior to including this file!" +#endif + +SANCOV_FLAG(bool, symbolize, true, + "If set, converage information will be symbolized by sancov tool " + "after dumping.") + +SANCOV_FLAG(bool, help, false, "Print flags help.") Index: compiler-rt/trunk/lib/sancov/sanitizer_coverage_libcdep.cc =================================================================== --- compiler-rt/trunk/lib/sancov/sanitizer_coverage_libcdep.cc +++ compiler-rt/trunk/lib/sancov/sanitizer_coverage_libcdep.cc @@ -0,0 +1,174 @@ +//===-- 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. + +#include "sancov_flags.h" +#include "sanitizer_common/sanitizer_allocator_internal.h" +#include "sanitizer_common/sanitizer_atomic.h" +#include "sanitizer_common/sanitizer_common.h" +#include "sanitizer_common/sanitizer_symbolizer.h" + +using namespace __sanitizer; + +using AddressRange = LoadedModule::AddressRange; + +namespace __sancov { +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); + Printf("SanitizerCoverage: %s %zd PCs written\n", file_path, len); +} + +static void SanitizerDumpCoverage(const uptr* unsorted_pcs, uptr len) { + if (!len) return; + + char* file_path = static_cast(InternalAlloc(kMaxPathLength)); + char* module_name = static_cast(InternalAlloc(kMaxPathLength)); + uptr* pcs = static_cast(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])) { + Printf("ERROR: bad pc %x\n", pc); + 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); + + if (sancov_flags()->symbolize) { + Printf("TODO(aizatsky): call sancov to symbolize\n"); + } +} + +// Collects trace-pc guard coverage. +// This class relies on zero-initialization. +class TracePcGuardController { + public: + void Initialize() { + CHECK(!initialized); + + initialized = true; + InitializeSancovFlags(); + + 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) { + atomic_uint32_t* guard_ptr = reinterpret_cast(guard); + u32 idx = atomic_exchange(guard_ptr, 0, memory_order_relaxed); + if (!idx) return; + // we start indices from 1. + pc_vector[idx - 1] = pc; + } + + void Dump() { + if (!initialized || !common_flags()->coverage) return; + __sanitizer_dump_coverage(pc_vector.data(), pc_vector.size()); + } + + private: + bool initialized; + InternalMmapVectorNoCtor pc_vector; +}; + +static TracePcGuardController pc_guard_controller; + +} // namespace +} // namespace __sancov + +extern "C" { +SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_dump_coverage( // NOLINT + const uptr* pcs, uptr len) { + return __sancov::SanitizerDumpCoverage(pcs, len); +} + +SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void +__sanitizer_cov_trace_pc_guard(u32* guard) { + if (!*guard) return; + __sancov::pc_guard_controller.TracePcGuard(guard, GET_CALLER_PC() - 1); +} + +SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void +__sanitizer_cov_trace_pc_guard_init(u32* start, u32* end) { + if (start == end || *start) return; + __sancov::pc_guard_controller.InitTracePcGuard(start, end); +} + +SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_dump_trace_pc_guard_coverage() { + __sancov::pc_guard_controller.Dump(); +} +} // extern "C" Index: compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt +++ compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt @@ -52,9 +52,7 @@ set(SANITIZER_LIBCDEP_SOURCES sanitizer_common_libcdep.cc - sancov_flags.cc sanitizer_coverage_libcdep.cc - sanitizer_coverage_libcdep_new.cc sanitizer_coverage_mapping_libcdep.cc sanitizer_linux_libcdep.cc sanitizer_posix_libcdep.cc Index: compiler-rt/trunk/lib/sanitizer_common/sancov_flags.h =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sancov_flags.h +++ compiler-rt/trunk/lib/sanitizer_common/sancov_flags.h @@ -1,40 +0,0 @@ -//===-- sancov_flags.h ------------------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// Sanitizer Coverage runtime flags. -// -//===----------------------------------------------------------------------===// -#ifndef SANCOV_FLAGS_H -#define SANCOV_FLAGS_H - -#include "sanitizer_flag_parser.h" -#include "sanitizer_internal_defs.h" - -namespace __sancov { - -struct SancovFlags { -#define SANCOV_FLAG(Type, Name, DefaultValue, Description) Type Name; -#include "sancov_flags.inc" -#undef SANCOV_FLAG - - void SetDefaults(); -}; - -extern SancovFlags sancov_flags_dont_use_directly; - -inline SancovFlags* sancov_flags() { return &sancov_flags_dont_use_directly; } - -void InitializeSancovFlags(); - -extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE const char* -__sancov_default_options(); - -} // namespace __sancov - -#endif Index: compiler-rt/trunk/lib/sanitizer_common/sancov_flags.cc =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sancov_flags.cc +++ compiler-rt/trunk/lib/sanitizer_common/sancov_flags.cc @@ -1,60 +0,0 @@ -//===-- sancov_flags.cc -----------------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// Sanitizer Coverage runtime flags. -// -//===----------------------------------------------------------------------===// - -#include "sancov_flags.h" -#include "sanitizer_flag_parser.h" -#include "sanitizer_platform.h" - -#if !SANITIZER_LINUX -// other platforms do not have weak symbols out of the box. -extern "C" const char* __sancov_default_options() { return ""; } -#endif - -using namespace __sanitizer; - -namespace __sancov { - -SancovFlags sancov_flags_dont_use_directly; // use via flags(); - -void SancovFlags::SetDefaults() { -#define SANCOV_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue; -#include "sancov_flags.inc" -#undef SANCOV_FLAG -} - -static void RegisterSancovFlags(FlagParser *parser, SancovFlags *f) { -#define SANCOV_FLAG(Type, Name, DefaultValue, Description) \ - RegisterFlag(parser, #Name, Description, &f->Name); -#include "sancov_flags.inc" -#undef SANCOV_FLAG -} - -static const char *MaybeCallSancovDefaultOptions() { - return (&__sancov_default_options) ? __sancov_default_options() : ""; -} - -void InitializeSancovFlags() { - SancovFlags *f = sancov_flags(); - f->SetDefaults(); - - FlagParser parser; - RegisterSancovFlags(&parser, f); - - parser.ParseString(MaybeCallSancovDefaultOptions()); - parser.ParseString(GetEnv("SANCOV_OPTIONS")); - - ReportUnrecognizedFlags(); - if (f->help) parser.PrintFlagDescriptions(); -} - -} // namespace __sancov Index: compiler-rt/trunk/lib/sanitizer_common/sancov_flags.inc =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sancov_flags.inc +++ compiler-rt/trunk/lib/sanitizer_common/sancov_flags.inc @@ -1,21 +0,0 @@ -//===-- sancov_flags.inc ----------------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// Sanitizer Coverage runtime flags. -// -//===----------------------------------------------------------------------===// -#ifndef SANCOV_FLAG -#error "Defnine SANCOV_FLAG prior to including this file!" -#endif - -SANCOV_FLAG(bool, symbolize, true, - "If set, converage information will be symbolized by sancov tool " - "after dumping.") - -SANCOV_FLAG(bool, help, false, "Print flags help.") Index: compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep.cc =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep.cc +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep.cc @@ -954,7 +954,9 @@ } SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump() { coverage_data.DumpAll(); +#if SANITIZER_LINUX __sanitizer_dump_trace_pc_guard_coverage(); +#endif } SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_module_init(s32 *guards, uptr npcs, u8 *counters, Index: compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cc =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cc +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cc @@ -1,174 +0,0 @@ -//===-- 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. - -#include "sancov_flags.h" -#include "sanitizer_allocator_internal.h" -#include "sanitizer_atomic.h" -#include "sanitizer_common.h" -#include "sanitizer_symbolizer.h" - -using namespace __sanitizer; - -using AddressRange = LoadedModule::AddressRange; - -namespace __sancov { -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); - Printf("SanitizerCoverage: %s %zd PCs written\n", file_path, len); -} - -static void SanitizerDumpCoverage(const uptr* unsorted_pcs, uptr len) { - if (!len) return; - - char* file_path = static_cast(InternalAlloc(kMaxPathLength)); - char* module_name = static_cast(InternalAlloc(kMaxPathLength)); - uptr* pcs = static_cast(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])) { - Printf("ERROR: bad pc %x\n", pc); - 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); - - if (sancov_flags()->symbolize) { - Printf("TODO(aizatsky): call sancov to symbolize\n"); - } -} - -// Collects trace-pc guard coverage. -// This class relies on zero-initialization. -class TracePcGuardController { - public: - void Initialize() { - CHECK(!initialized); - - initialized = true; - InitializeSancovFlags(); - - 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) { - atomic_uint32_t* guard_ptr = reinterpret_cast(guard); - u32 idx = atomic_exchange(guard_ptr, 0, memory_order_relaxed); - if (!idx) return; - // we start indices from 1. - pc_vector[idx - 1] = pc; - } - - void Dump() { - if (!initialized || !common_flags()->coverage) return; - __sanitizer_dump_coverage(pc_vector.data(), pc_vector.size()); - } - - private: - bool initialized; - InternalMmapVectorNoCtor pc_vector; -}; - -static TracePcGuardController pc_guard_controller; - -} // namespace -} // namespace __sancov - -extern "C" { -SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_dump_coverage( // NOLINT - const uptr* pcs, uptr len) { - return __sancov::SanitizerDumpCoverage(pcs, len); -} - -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void -__sanitizer_cov_trace_pc_guard(u32* guard) { - if (!*guard) return; - __sancov::pc_guard_controller.TracePcGuard(guard, GET_CALLER_PC() - 1); -} - -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void -__sanitizer_cov_trace_pc_guard_init(u32* start, u32* end) { - if (start == end || *start) return; - __sancov::pc_guard_controller.InitTracePcGuard(start, end); -} - -SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_dump_trace_pc_guard_coverage() { - __sancov::pc_guard_controller.Dump(); -} -} // extern "C" Index: compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt +++ compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt @@ -200,7 +200,8 @@ if(APPLE) add_sanitizer_common_lib("RTSanitizerCommon.test.osx" $ - $) + $ + $) else() if(CAN_TARGET_x86_64) add_sanitizer_common_lib("RTSanitizerCommon.test.nolibc.x86_64" @@ -210,7 +211,8 @@ foreach(arch ${SANITIZER_UNITTEST_SUPPORTED_ARCH}) add_sanitizer_common_lib("RTSanitizerCommon.test.${arch}" $ - $) + $ + $) endforeach() endif() foreach(arch ${SANITIZER_UNITTEST_SUPPORTED_ARCH}) @@ -224,7 +226,8 @@ ${SANITIZER_UNITTESTS} ${COMPILER_RT_GTEST_SOURCE} $ - $) + $ + $) set_target_compile_flags(SanitizerTest ${SANITIZER_COMMON_CFLAGS} ${SANITIZER_TEST_CFLAGS_COMMON}) Index: compiler-rt/trunk/lib/scudo/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/scudo/CMakeLists.txt +++ compiler-rt/trunk/lib/scudo/CMakeLists.txt @@ -29,6 +29,7 @@ $ $ $ + $ CFLAGS ${SCUDO_CFLAGS} PARENT_TARGET scudo) endforeach() Index: compiler-rt/trunk/lib/stats/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/stats/CMakeLists.txt +++ compiler-rt/trunk/lib/stats/CMakeLists.txt @@ -22,6 +22,7 @@ SOURCES stats.cc OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc + RTSancovLibc CFLAGS ${SANITIZER_COMMON_CFLAGS} LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS} PARENT_TARGET stats) Index: compiler-rt/trunk/lib/tsan/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/tsan/CMakeLists.txt +++ compiler-rt/trunk/lib/tsan/CMakeLists.txt @@ -119,6 +119,7 @@ OBJECT_LIBS RTInterception RTSanitizerCommon RTSanitizerCommonLibc + RTSancovLibc RTUbsan CFLAGS ${TSAN_RTL_CFLAGS} LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS} @@ -179,6 +180,7 @@ $ $ $ + $ $ CFLAGS ${TSAN_RTL_CFLAGS}) add_compiler_rt_runtime(clang_rt.tsan_cxx Index: compiler-rt/trunk/lib/tsan/dd/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/tsan/dd/CMakeLists.txt +++ compiler-rt/trunk/lib/tsan/dd/CMakeLists.txt @@ -26,6 +26,7 @@ $ $ $ + $ CFLAGS ${DD_CFLAGS} PARENT_TARGET dd) @@ -40,6 +41,7 @@ $ $ $ + $ LINK_LIBS ${DD_LINKLIBS} PARENT_TARGET dd) endif() Index: compiler-rt/trunk/lib/tsan/tests/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/tsan/tests/CMakeLists.txt +++ compiler-rt/trunk/lib/tsan/tests/CMakeLists.txt @@ -69,6 +69,7 @@ $ $ $ + $ $) set(TSAN_TEST_RUNTIME RTTsanTest.${testname}.${arch}) add_library(${TSAN_TEST_RUNTIME} STATIC ${TSAN_TEST_RUNTIME_OBJECTS}) Index: compiler-rt/trunk/lib/ubsan/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/ubsan/CMakeLists.txt +++ compiler-rt/trunk/lib/ubsan/CMakeLists.txt @@ -67,6 +67,7 @@ RTUbsan_standalone RTSanitizerCommon RTSanitizerCommonLibc + RTSancovLibc LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS} PARENT_TARGET ubsan) endif() @@ -102,6 +103,7 @@ ARCHS ${UBSAN_SUPPORTED_ARCH} OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc + RTSancovLibc RTUbsan RTUbsan_standalone CFLAGS ${UBSAN_CFLAGS} Index: compiler-rt/trunk/lib/xray/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/xray/CMakeLists.txt +++ compiler-rt/trunk/lib/xray/CMakeLists.txt @@ -51,7 +51,8 @@ set(XRAY_COMMON_RUNTIME_OBJECT_LIBS RTSanitizerCommon - RTSanitizerCommonLibc) + RTSanitizerCommonLibc + RTSancovLibc) foreach(arch ${XRAY_SUPPORTED_ARCH}) if(CAN_TARGET_${arch})