rippled
Loading...
Searching...
No Matches
antithesis_instrumentation.h
1#pragma once
2
3/*
4This header file enables code coverage instrumentation. It is distributed with the Antithesis C++ SDK.
5
6This header file can be used in both C and C++ programs. (The rest of the SDK works only for C++ programs.)
7
8You should include it in a single .cpp or .c file.
9
10The instructions (such as required compiler flags) and usage guidance are found at https://antithesis.com/docs/using_antithesis/sdk/cpp/overview/.
11*/
12
13#include <unistd.h>
14#include <string.h>
15#include <dlfcn.h>
16#include <stdint.h>
17#include <stdio.h>
18#ifndef __cplusplus
19#include <stdbool.h>
20#include <stddef.h>
21#endif
22
23// If the libvoidstar(determ) library is present,
24// pass thru trace_pc_guard related callbacks to it
25typedef void (*trace_pc_guard_init_fn)(uint32_t *start, uint32_t *stop);
26typedef void (*trace_pc_guard_fn)(uint32_t *guard, uint64_t edge);
27
28static trace_pc_guard_init_fn trace_pc_guard_init = NULL;
29static trace_pc_guard_fn trace_pc_guard = NULL;
30static bool did_check_libvoidstar = false;
31static bool has_libvoidstar = false;
32
33static __attribute__((no_sanitize("coverage"))) void debug_message_out(const char *msg) {
34 (void)printf("%s\n", msg);
35 return;
36}
37
38extern
39#ifdef __cplusplus
40 "C"
41#endif
42__attribute__((no_sanitize("coverage"))) void antithesis_load_libvoidstar() {
43#ifdef __cplusplus
44 constexpr
45#endif
46 const char* LIB_PATH = "/usr/lib/libvoidstar.so";
47
48 if (did_check_libvoidstar) {
49 return;
50 }
51 debug_message_out("TRYING TO LOAD libvoidstar");
52 did_check_libvoidstar = true;
53 void* shared_lib = dlopen(LIB_PATH, RTLD_NOW);
54 if (!shared_lib) {
55 debug_message_out("Can not load the Antithesis native library");
56 return;
57 }
58
59 void* trace_pc_guard_init_sym = dlsym(shared_lib, "__sanitizer_cov_trace_pc_guard_init");
60 if (!trace_pc_guard_init_sym) {
61 debug_message_out("Can not forward calls to libvoidstar for __sanitizer_cov_trace_pc_guard_init");
62 return;
63 }
64
65 void* trace_pc_guard_sym = dlsym(shared_lib, "__sanitizer_cov_trace_pc_guard_internal");
66 if (!trace_pc_guard_sym) {
67 debug_message_out("Can not forward calls to libvoidstar for __sanitizer_cov_trace_pc_guard");
68 return;
69 }
70
71 trace_pc_guard_init = (trace_pc_guard_init_fn)(trace_pc_guard_init_sym);
72 trace_pc_guard = (trace_pc_guard_fn)(trace_pc_guard_sym);
73 has_libvoidstar = true;
74 debug_message_out("LOADED libvoidstar");
75}
76
77// The following symbols are indeed reserved identifiers, since we're implementing functions defined
78// in the compiler runtime. Not clear how to get Clang on board with that besides narrowly suppressing
79// the warning in this case. The sample code on the CoverageSanitizer documentation page fails this
80// warning!
81#pragma clang diagnostic push
82#pragma clang diagnostic ignored "-Wreserved-identifier"
83extern
84#ifdef __cplusplus
85 "C"
86#endif
87void __sanitizer_cov_trace_pc_guard_init(uint32_t *start, uint32_t *stop) {
88 debug_message_out("SDK forwarding to libvoidstar for __sanitizer_cov_trace_pc_guard_init()");
89 if (!did_check_libvoidstar) {
90 antithesis_load_libvoidstar();
91 }
92 if (has_libvoidstar) {
93 trace_pc_guard_init(start, stop);
94 }
95 return;
96}
97
98extern
99#ifdef __cplusplus
100 "C"
101#endif
102void __sanitizer_cov_trace_pc_guard( uint32_t *guard ) {
103 if (has_libvoidstar) {
104 uint64_t edge = (uint64_t)(__builtin_return_address(0));
105 trace_pc_guard(guard, edge);
106 } else {
107 if (guard) {
108 *guard = 0;
109 }
110 }
111 return;
112}
113#pragma clang diagnostic pop
T printf(T... args)