llvm-project
83 строки · 2.2 Кб
1//===-- stats_client.cpp --------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Sanitizer statistics gathering. Manages statistics for a module (executable
10// or DSO) and registers statistics with the process.
11//
12// This is linked into each individual modle and cannot directly use functions
13// declared in sanitizer_common.
14//
15//===----------------------------------------------------------------------===//
16
17#ifdef _WIN3218#define WIN32_LEAN_AND_MEAN19#include <windows.h>20#else21#include <dlfcn.h>22#endif23#include <stdint.h>24#include <stdio.h>25
26#include "sanitizer_common/sanitizer_internal_defs.h"27#include "stats/stats.h"28
29using namespace __sanitizer;30
31namespace {32
33void *LookupSymbolFromMain(const char *name) {34#ifdef _WIN3235return reinterpret_cast<void *>(GetProcAddress(GetModuleHandle(0), name));36#else37return dlsym(RTLD_DEFAULT, name);38#endif39}
40
41StatModule *list;42
43struct RegisterSanStats {44unsigned module_id;45
46RegisterSanStats() {47typedef unsigned (*reg_func_t)(StatModule **);48reg_func_t reg_func = reinterpret_cast<reg_func_t>(49LookupSymbolFromMain("__sanitizer_stats_register"));50if (reg_func)51module_id = reg_func(&list);52}53
54~RegisterSanStats() {55typedef void (*unreg_func_t)(unsigned);56unreg_func_t unreg_func = reinterpret_cast<unreg_func_t>(57LookupSymbolFromMain("__sanitizer_stats_unregister"));58if (unreg_func)59unreg_func(module_id);60}61} reg;62
63}
64
65extern "C" void __sanitizer_stat_init(StatModule *mod) {66mod->next = list;67list = mod;68}
69
70extern "C" void __sanitizer_stat_report(StatInfo *s) {71s->addr = GET_CALLER_PC();72#if defined(_WIN64) && !defined(__clang__)73uptr old_data = InterlockedIncrement64(reinterpret_cast<LONG64 *>(&s->data));74#elif defined(_WIN32) && !defined(__clang__)75uptr old_data = InterlockedIncrement(&s->data);76#else77uptr old_data = __sync_fetch_and_add(&s->data, 1);78#endif79
80// Overflow check.81if (CountFromData(old_data + 1) == 0)82Trap();83}
84