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