Path: blob/main/contrib/llvm-project/compiler-rt/lib/stats/stats_client.cpp
35262 views
//===-- stats_client.cpp --------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Sanitizer statistics gathering. Manages statistics for a module (executable9// or DSO) and registers statistics with the process.10//11// This is linked into each individual modle and cannot directly use functions12// declared in sanitizer_common.13//14//===----------------------------------------------------------------------===//1516#ifdef _WIN3217#define WIN32_LEAN_AND_MEAN18#include <windows.h>19#else20#include <dlfcn.h>21#endif22#include <stdint.h>23#include <stdio.h>2425#include "sanitizer_common/sanitizer_internal_defs.h"26#include "stats/stats.h"2728using namespace __sanitizer;2930namespace {3132void *LookupSymbolFromMain(const char *name) {33#ifdef _WIN3234return reinterpret_cast<void *>(GetProcAddress(GetModuleHandle(0), name));35#else36return dlsym(RTLD_DEFAULT, name);37#endif38}3940StatModule *list;4142struct RegisterSanStats {43unsigned module_id;4445RegisterSanStats() {46typedef unsigned (*reg_func_t)(StatModule **);47reg_func_t reg_func = reinterpret_cast<reg_func_t>(48LookupSymbolFromMain("__sanitizer_stats_register"));49if (reg_func)50module_id = reg_func(&list);51}5253~RegisterSanStats() {54typedef void (*unreg_func_t)(unsigned);55unreg_func_t unreg_func = reinterpret_cast<unreg_func_t>(56LookupSymbolFromMain("__sanitizer_stats_unregister"));57if (unreg_func)58unreg_func(module_id);59}60} reg;6162}6364extern "C" void __sanitizer_stat_init(StatModule *mod) {65mod->next = list;66list = mod;67}6869extern "C" void __sanitizer_stat_report(StatInfo *s) {70s->addr = GET_CALLER_PC();71#if defined(_WIN64) && !defined(__clang__)72uptr old_data = InterlockedIncrement64(reinterpret_cast<LONG64 *>(&s->data));73#elif defined(_WIN32) && !defined(__clang__)74uptr old_data = InterlockedIncrement(&s->data);75#else76uptr old_data = __sync_fetch_and_add(&s->data, 1);77#endif7879// Overflow check.80if (CountFromData(old_data + 1) == 0)81Trap();82}838485