1//===-- copyprof.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/// This file implements the core CopyProf runtime initialization and callback
10/// functions inserted by the instrumentation passes.
11///
12//===----------------------------------------------------------------------===//
13
14#include "copyprof_allocator.h"
15#include "copyprof_interceptors.h"
16#include "copyprof_interface_internal.h"
17#include "copyprof_per_object_state.h"
18#include "copyprof_reporting.h"
19#include "copyprof_shadow.h"
20#include "copyprof_state.h"
21#include "sanitizer_common/sanitizer_common.h"
22#include "sanitizer_common/sanitizer_flags.h"
23#include "sanitizer_common/sanitizer_internal_defs.h"
24
25using namespace __copyprof;
26
27namespace __copyprof {
28
29// Whether CopyProf has been initialized.
30bool copyprof_is_initialized;
31// Whether CopyProf is currently initializing.
32bool copyprof_init_is_running;
33
34static void CheckUnwind() {
35 UNINITIALIZED BufferedStackTrace trace;
36 trace.Unwind(pc: StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(),
37 /*context=*/nullptr, request_fast: common_flags()->fast_unwind_on_check);
38 trace.Print();
39}
40
41static void Initialize() {
42 if (LIKELY(copyprof_is_initialized))
43 return;
44 SanitizerToolName = "CopyProf";
45 CHECK(!copyprof_init_is_running &&
46 "BUG: CopyProf Initialize() must not call itself.");
47 copyprof_init_is_running = true;
48 CacheBinaryName();
49 // CopyProf uses interception so ensure we're not statically linked.
50 __interception::DoesNotSupportStaticLinking();
51 SetCheckUnwindCallback(&CheckUnwind);
52 InitializePlatformEarly();
53 SetCommonFlagsDefaults();
54 InitializeCommonFlags();
55 InitializeInterceptors();
56 InitializeShadowMemory();
57 InitializePerObjectTracking();
58 InitializeCopyProfAllocator();
59 copyprof_init_is_running = false;
60 copyprof_is_initialized = true;
61}
62
63static void MaybeUpdateSmfContext(SmfContext context) {
64 // Only entering a top level special member function changes the current
65 // context. The context logically remains the same until control flow leaves
66 // the top level function.
67 if (__copyprof_state.construct_nesting_level == 0 &&
68 __copyprof_state.copy_nesting_level == 0 &&
69 __copyprof_state.destruct_nesting_level == 0) {
70 __copyprof_state.smf_context = context;
71 }
72}
73
74// Updates shadow memory for `[addr, addr + size)` according to the current SMF
75// context: no update in `DTOR` context (objects may mutate their memory during
76// destruction, but that must not invalidate its classification as an
77// unnecessary copy), marked as copy in `COPY` context, and marked as non-copy
78// otherwise.
79static void UpdateShadow(const void* addr, uptr size) {
80 if (UNLIKELY(__copyprof_state.smf_context == SmfContext::DTOR))
81 return;
82 MarkApplicationMemory(app_addr: addr, num_bytes: size,
83 is_copy: __copyprof_state.smf_context == SmfContext::COPY);
84}
85
86// Marks the shadow memory of an object that is being constructed. Unlike for
87// stores, this also happens in `DTOR` context (see `SmfContext`).
88static void MarkConstructedObject(const void* this_ptr, uptr obj_size) {
89 MarkApplicationMemory(
90 app_addr: this_ptr, num_bytes: obj_size,
91 is_copy: ShouldMarkNewMemoryAsCopy(ctx: __copyprof_state.smf_context));
92}
93
94static void CopyMemberFunctionEnter(const void* this_ptr, uptr obj_size) {
95 MaybeUpdateSmfContext(context: SmfContext::COPY);
96 // Currently, only top-level copies are reported, so only top-level objects
97 // get per-object state which is then removed when the corresponding top-level
98 // d'tor exits. A nested object copy is not individually marked as a copy and
99 // is typically destroyed by a nested d'tor, so its state would never be
100 // removed (from the object state map).
101 if (__copyprof_state.copy_nesting_level++ == 0 &&
102 __copyprof_state.smf_context == SmfContext::COPY) {
103 InitPerObjectState(this_ptr, static_size: obj_size);
104 __copyprof_state.current_this_ptr = this_ptr;
105 }
106}
107
108static void CopyMemberFunctionExit(const void* this_ptr, uptr obj_size) {
109 --__copyprof_state.copy_nesting_level;
110 MaybeUpdateSmfContext(context: SmfContext::NONE);
111}
112
113} // namespace __copyprof
114
115void __copyprof_init_once() { Initialize(); }
116
117void __copyprof_ctor_enter_callback(const void* this_ptr, uptr obj_size) {
118 MaybeUpdateSmfContext(context: SmfContext::CTOR);
119 ++__copyprof_state.construct_nesting_level;
120 MarkConstructedObject(this_ptr, obj_size);
121}
122
123void __copyprof_ctor_exit_callback(const void* this_ptr, uptr obj_size) {
124 --__copyprof_state.construct_nesting_level;
125 MaybeUpdateSmfContext(context: SmfContext::NONE);
126}
127
128void __copyprof_copy_ctor_enter_callback(const void* this_ptr,
129 const void* other_ptr, uptr obj_size) {
130 CopyMemberFunctionEnter(this_ptr, obj_size);
131 MarkConstructedObject(this_ptr, obj_size);
132}
133
134void __copyprof_copy_ctor_exit_callback(const void* this_ptr,
135 const void* other_ptr, uptr obj_size) {
136 CopyMemberFunctionExit(this_ptr, obj_size);
137}
138
139void __copyprof_copy_assign_op_enter_callback(const void* this_ptr,
140 const void* other_ptr,
141 uptr obj_size) {
142 CopyMemberFunctionEnter(this_ptr, obj_size);
143 // Unlike a c'tor, a copy assignment overwrites an existing object, so it is
144 // treated like a store.
145 UpdateShadow(addr: this_ptr, size: obj_size);
146}
147
148void __copyprof_copy_assign_op_exit_callback(const void* this_ptr,
149 const void* other_ptr,
150 uptr obj_size) {
151 CopyMemberFunctionExit(this_ptr, obj_size);
152}
153
154void __copyprof_dtor_enter_callback(const void* this_ptr, uptr obj_size) {
155 MaybeUpdateSmfContext(context: SmfContext::DTOR);
156 if (__copyprof_state.destruct_nesting_level++ == 0) {
157 // Control flow just entered the top-level d'tor. Optimistically mark
158 // `is_transitive_copy` as `true`. If any subsequent d'tor observes object
159 // memory marked as not copy, then the flag will be set to `false`.
160 __copyprof_state.is_transitive_copy = true;
161 }
162}
163
164void __copyprof_dtor_exit_callback(const void* this_ptr, uptr obj_size) {
165 --__copyprof_state.destruct_nesting_level;
166 MaybeUpdateSmfContext(context: SmfContext::NONE);
167 // If this is the top level-dtor and `this` is transitively marked as copy,
168 // then an object has been found whose transitively owned memory is marked as
169 // copy, so a report is logged.
170 __copyprof_state.is_transitive_copy &= IsMarkedAsCopy(app_addr: this_ptr, num_bytes: obj_size);
171 if (__copyprof_state.destruct_nesting_level == 0 &&
172 __copyprof_state.is_transitive_copy) {
173 LogCopyProfReport(GET_CALLER_PC(), GET_CURRENT_FRAME(),
174 obj_size: GetPerObjectTrackedSize(this_ptr),
175 did_allocate: GetPerObjectDidAllocate(this_ptr));
176 }
177 // Clean up per-object state when top-level destructor exits.
178 if (__copyprof_state.destruct_nesting_level == 0) {
179 RemovePerObjectState(this_ptr);
180 }
181}
182
183void __copyprof_store_callback(const void* addr, uptr size) {
184 UpdateShadow(addr, size);
185}
186