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