Debian Codegraph @

src/source/libscalene.cpp

raw symbols SCIP
1 #cxx . . $ `<file>/src/source/libscalene.cpp`/define SCALENE_DISABLE_SIGNALScxx . . $ `src/source/libscalene.cpp:1:9`! 0 // for debugging only
2
3 #if !defined(_WIN32)
4 #include <unistd.h>
5 #endif
6
7 #include <heaplayers.h>cxx . . $ `<file>/vendor/Heap-Layers/heaplayers.h`/
8 #include <signal.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include <cstddef>
13
14 #include "common.hpp"cxx . . $ `<file>/src/include/common.hpp`/
15 #include "heapredirect.h"cxx . . $ `<file>/vendor/Heap-Layers/wrappers/heapredirect.h`/
16 #include "memcpysampler.hpp"cxx . . $ `<file>/src/include/memcpysampler.hpp`/
17 #include "sampleheap.hpp"cxx . . $ `<file>/src/include/sampleheap.hpp`/
18 #include "scaleneheader.hpp"cxx . . $ `<file>/src/include/scaleneheader.hpp`/
19
20 #if defined(__APPLE__)
21 #include "macinterpose.h"
22 #endif
23
24 // Allocate exactly one system heap.
25 using BaseHeapcxx . . $ BaseHeap# = HLcxx . . $ HL/::OneHeapcxx . . $ HL/OneHeap#<HLcxx . . $ HL/::SysMallocHeapcxx . . $ HL/SysMallocHeap#>;
26
27 // For use by the replacement printf routines (see
28 // https://github.com/mpaland/printf)
29 extern "C" void _putchar(char ch) { ::write(1, (void *)&ch, 1); }
30
31 constexpr uint64_tcxx . . $ uint64_t# DefaultAllocationSamplingRatecxx . . $ DefaultAllocationSamplingRate. =
32 1 * 10485767ULL; // was 1 * 1549351ULL;
33 constexpr uint64_tcxx . . $ uint64_t# MemcpySamplingRatecxx . . $ MemcpySamplingRate. = DefaultAllocationSamplingRatecxx . . $ DefaultAllocationSamplingRate. * 7;
34
35 /**
36 * @brief the replacement heap for sampling purposes
37 *
38 */
39 class CustomHeapTypecxx . . $ CustomHeapType#
40 : public HLcxx . . $ HL/::ThreadSpecificHeapcxx . . $ HL/ThreadSpecificHeap#<
41 SampleHeapcxx . . $ SampleHeap#<DefaultAllocationSamplingRatecxx . . $ DefaultAllocationSamplingRate., BaseHeapcxx . . $ BaseHeap#>> {
42 using supercxx . . $ CustomHeapType#super# = HLcxx . . $ HL/::ThreadSpecificHeapcxx . . $ HL/ThreadSpecificHeap#<
43 SampleHeapcxx . . $ SampleHeap#<DefaultAllocationSamplingRatecxx . . $ DefaultAllocationSamplingRate., BaseHeapcxx . . $ BaseHeap#>>;
44
45 public:
46 void lockcxx . . $ CustomHeapType#lock(49f6e7a06ebc5aa8).() {}
47 void unlockcxx . . $ CustomHeapType#unlock(49f6e7a06ebc5aa8).() {}
48 };
49
50 HEAP_REDIRECTcxx . . $ `vendor/Heap-Layers/wrappers/heapredirect.h:215:9`!(CustomHeapType, 8 * 1024 * 1024);
51
52 /**
53 * @brief Get the static MemcpySampler object
54 *
55 * @return auto& the singleton sampling object
56 */
57 auto &getSamplercxx . . $ getSampler(41db3cfd0f1c24d6).() {
58 static MemcpySamplercxx . . $ MemcpySampler#<MemcpySamplingRatecxx . . $ MemcpySamplingRate.> msampcxx . . $ MemcpySampler#MemcpySampler(49f6e7a06ebc5aa8).;
59 return msamp;
60 }
61
62 #if defined(__APPLE__)
63 #define LOCAL_PREFIX(x) xx##x
64 #else
65 #define LOCAL_PREFIXcxx . . $ `src/source/libscalene.cpp:65:9`!(x) x
66 #endif
67
68 extern "C" ATTRIBUTE_EXPORTcxx . . $ `src/include/common.hpp:14:9`! void *LOCAL_PREFIXcxx . . $ `src/source/libscalene.cpp:65:9`!(memcpy)(void *dst,
69 const void *src,
70 size_t n) {
71 auto result = getSamplercxx . . $ getSampler(41db3cfd0f1c24d6).().memcpycxx . . $ MemcpySampler#memcpy(cc817cb175f282d).(dst, src, n);
72 return result;
73 }
74
75 extern "C" ATTRIBUTE_EXPORTcxx . . $ `src/include/common.hpp:14:9`! void *LOCAL_PREFIXcxx . . $ `src/source/libscalene.cpp:65:9`!(memmove)(void *dst,
76 const void *src,
77 size_t n) {
78 auto result = getSamplercxx . . $ getSampler(41db3cfd0f1c24d6).().memmovecxx . . $ MemcpySampler#memmove(cc817cb175f282d).(dst, src, n);
79 return result;
80 }
81
82 extern "C" ATTRIBUTE_EXPORTcxx . . $ `src/include/common.hpp:14:9`! char *LOCAL_PREFIXcxx . . $ `src/source/libscalene.cpp:65:9`!(strcpy)(char *dst,
83 const char *src) {
84 auto result = getSamplercxx . . $ getSampler(41db3cfd0f1c24d6).().strcpycxx . . $ MemcpySampler#strcpy(b7b3a83273496486).(dst, src);
85 return result;
86 }
87
88 // Intercept local allocation for tracking when using the (fast, built-in)
89 // pymalloc allocator.
90
91 #if !defined(_WIN32)
92
93 #include <Python.h>
94
95 #define DL_FUNCTIONcxx . . $ `src/source/libscalene.cpp:95:9`!(name) \
96 static decltype(name) *dl##name = (decltype(name) *)dlsym(RTLD_DEFAULTcxx . . $ `<external>/16223394379662886929/dlfcn.h:50:9`!, #name)
97
98 // Maximum size allocated internally by pymalloc;
99 // aka "SMALL_REQUEST_THRESHOLD" in cpython/Objects/obmalloc.c
100 #define PYMALLOC_MAX_SIZEcxx . . $ `src/source/libscalene.cpp:100:9`! 512
101
102 /**
103 * @brief replace local Python allocators with our own sampling variants
104 *
105 * @tparam Domain the Python domain of allocator we replace
106 */
107
108 template <PyMemAllocatorDomain Domain>
109 class MakeLocalAllocatorcxx . . $ MakeLocalAllocator# {
110 public:
111 MakeLocalAllocatorcxx . . $ MakeLocalAllocator#`MakeLocalAllocator<Domain>`(49f6e7a06ebc5aa8).() {
112 localAlloccxx . . $ MakeLocalAllocator#localAlloc. = {.ctx = nullptr,
113 .malloc = local_malloccxx . . $ MakeLocalAllocator#local_malloc(417ab160f6baadc6).,
114 .calloc = local_calloccxx . . $ MakeLocalAllocator#local_calloc(dfa4a9ebc9436eff).,
115 .realloc = local_realloccxx . . $ MakeLocalAllocator#local_realloc(9c6744c5c9224d04).,
116 .free = local_freecxx . . $ MakeLocalAllocator#local_free(6c58c5e2437d25a2).};
117
118 DL_FUNCTIONcxx . . $ `src/source/libscalene.cpp:95:9`!(PyMem_GetAllocator);
119 DL_FUNCTIONcxx . . $ `src/source/libscalene.cpp:95:9`!(PyMem_SetAllocator);
120
121 if (dlPyMem_GetAllocator != nullptr && dlPyMem_SetAllocator != nullptr) {
122 // if these aren't found, chances are we were preloaded onto something
123 // other than Python
124 dlPyMem_GetAllocator(Domain, get_original_allocatorcxx . . $ MakeLocalAllocator#get_original_allocator(75e8ba9e8a0b0355).());
125 dlPyMem_SetAllocator(Domain, &localAlloccxx . . $ MakeLocalAllocator#localAlloc.);
126 }
127 }
128
129 private:
130 /// @brief the actual allocator we use to satisfy object allocations
131 PyMemAllocatorEx localAlloccxx . . $ MakeLocalAllocator#localAlloc.;
132
133 static inline PyMemAllocatorEx *get_original_allocatorcxx . . $ MakeLocalAllocator#get_original_allocator(75e8ba9e8a0b0355).() {
134 // poor man's "static inline" member
135 static PyMemAllocatorEx original_allocator;
136 return &original_allocator;
137 }
138
139 static inline void *local_malloccxx . . $ MakeLocalAllocator#local_malloc(417ab160f6baadc6).(void *ctx, size_t len) {
140 MallocRecursionGuardcxx . . $ MallocRecursionGuard# mcxx . . $ MallocRecursionGuard#MallocRecursionGuard(49f6e7a06ebc5aa8).;
141 #if 1
142 // Ensure all allocation requests are multiples of eight,
143 // mirroring the actual allocation sizes employed by pymalloc
144 // (See https://github.com/python/cpython/blob/main/Objects/obmalloc.c#L807)
145 if (len <= PYMALLOC_MAX_SIZEcxx . . $ `src/source/libscalene.cpp:100:9`!) {
146 if (unlikelycxx . . $ `src/include/common.hpp:8:9`!(len == 0)) {
147 // Handle 0.
148 len = 8;
149 }
150 len = (len + 7) & ~7;
151 }
152 #endif
153 #if USE_HEADERScxx . . $ `src/include/scaleneheader.hpp:16:9`!
154 void *buf = nullptr;
155 const auto allocSize = len + sizeof(ScaleneHeadercxx . . $ ScaleneHeader#);
156 buf = get_original_allocatorcxx . . $ MakeLocalAllocator#get_original_allocator(75e8ba9e8a0b0355).()->malloc(get_original_allocatorcxx . . $ MakeLocalAllocator#get_original_allocator(75e8ba9e8a0b0355).()->ctx,
157 allocSize);
158 auto *header = new (buf) ScaleneHeadercxx . . $ ScaleneHeader#(len);
159 class Nadacxx . . $ MakeLocalAllocator#local_malloc(417ab160f6baadc6).Nada# {};
160 #else
161 auto *header = (ScaleneHeader *)get_original_allocator()->malloc(
162 get_original_allocator()->ctx, len);
163 #endif
164 assertcxx . . $ `/usr/include/assert.h:50:10`!(header); // We expect this to always succeed.
165 if (!m.wasInMalloccxx . . $ MallocRecursionGuard#wasInMalloc(50ce9a9e25b4a850).()) {
166 TheHeapWrappercxx . . $ TheHeapWrapper#::register_malloccxx . . $ HL/HeapWrapper#register_malloc(3bdecba87b0fe3d5).(len, ScaleneHeadercxx . . $ ScaleneHeader#::getObjectcxx . . $ ScaleneHeader#getObject(130a85129859ac6a).(header));
167 }
168
169 static_assert(
170 SampleHeapcxx . . $ SampleHeap#<1, HL::NullHeap<Nada>>::NEWLINE >cxx . . $ std/`operator>`(39b98f4fe21ae589). PYMALLOC_MAX_SIZEcxx . . $ `src/source/libscalene.cpp:100:9`!,
171 "NEWLINE must be greater than PYMALLOC_MAX_SIZE.");
172 #if USE_HEADERScxx . . $ `src/include/scaleneheader.hpp:16:9`!
173 assertcxx . . $ `/usr/include/assert.h:50:10`!((size_t)ScaleneHeader::getObject(header) - (size_t)header >=
174 sizeof(ScaleneHeader));
175 #ifndef NDEBUGcxx . . $ `<built-in>:458:9`!
176 if (ScaleneHeader::getSize(ScaleneHeader::getObject(header)) < len) {
177 printf_("Size mismatch: %lu %lu\n",
178 ScaleneHeader::getSize(ScaleneHeader::getObject(header)), len);
179 }
180 #endif
181 assertcxx . . $ `/usr/include/assert.h:50:10`!(ScaleneHeader::getSize(ScaleneHeader::getObject(header)) >= len);
182 #endif
183 return ScaleneHeadercxx . . $ ScaleneHeader#::getObjectcxx . . $ ScaleneHeader#getObject(130a85129859ac6a).(header);
184 }
185
186 static inline void local_freecxx . . $ MakeLocalAllocator#local_free(6c58c5e2437d25a2).(void *ctx, void *ptr) {
187 // ignore nullptr
188 if (ptr) {
189 MallocRecursionGuardcxx . . $ MallocRecursionGuard# mcxx . . $ MallocRecursionGuard#MallocRecursionGuard(49f6e7a06ebc5aa8).;
190 const auto sz = ScaleneHeadercxx . . $ ScaleneHeader#::getSizecxx . . $ ScaleneHeader#getSize(751e5d26f8574c32).(ptr);
191
192 if (!m.wasInMalloccxx . . $ MallocRecursionGuard#wasInMalloc(50ce9a9e25b4a850).()) {
193 TheHeapWrappercxx . . $ TheHeapWrapper#::register_freecxx . . $ HL/HeapWrapper#register_free(3bdecba87b0fe3d5).(sz, ptr);
194 }
195 get_original_allocatorcxx . . $ MakeLocalAllocator#get_original_allocator(75e8ba9e8a0b0355).()->free(get_original_allocatorcxx . . $ MakeLocalAllocator#get_original_allocator(75e8ba9e8a0b0355).()->ctx,
196 ScaleneHeadercxx . . $ ScaleneHeader#::getHeadercxx . . $ ScaleneHeader#getHeader(9053c1a020bd1a96).(ptr));
197 }
198 }
199
200 static inline void *local_realloccxx . . $ MakeLocalAllocator#local_realloc(9c6744c5c9224d04).(void *ctx, void *ptr, size_t new_size) {
201 if (new_size < 8) {
202 new_size = 8;
203 }
204 if (!ptr) {
205 return local_malloccxx . . $ MakeLocalAllocator#local_malloc(417ab160f6baadc6).(ctx, new_size);
206 }
207 MallocRecursionGuardcxx . . $ MallocRecursionGuard# mcxx . . $ MallocRecursionGuard#MallocRecursionGuard(49f6e7a06ebc5aa8).;
208 const auto sz = ScaleneHeadercxx . . $ ScaleneHeader#::getSizecxx . . $ ScaleneHeader#getSize(751e5d26f8574c32).(ptr);
209 void *p = nullptr;
210 const auto allocSize = new_size + sizeof(ScaleneHeadercxx . . $ ScaleneHeader#);
211 void *buf = get_original_allocatorcxx . . $ MakeLocalAllocator#get_original_allocator(75e8ba9e8a0b0355).()->realloc(get_original_allocatorcxx . . $ MakeLocalAllocator#get_original_allocator(75e8ba9e8a0b0355).()->ctx,
212 ScaleneHeadercxx . . $ ScaleneHeader#::getHeadercxx . . $ ScaleneHeader#getHeader(9053c1a020bd1a96).(ptr),
213 allocSize);
214 ScaleneHeadercxx . . $ ScaleneHeader# *result = new (buf) ScaleneHeadercxx . . $ ScaleneHeader#(new_size);
215 if (result && !m.wasInMalloccxx . . $ MallocRecursionGuard#wasInMalloc(50ce9a9e25b4a850).()) {
216 if (sz < new_size) {
217 TheHeapWrappercxx . . $ TheHeapWrapper#::register_malloccxx . . $ HL/HeapWrapper#register_malloc(3bdecba87b0fe3d5).(new_size - sz,
218 ScaleneHeadercxx . . $ ScaleneHeader#::getObjectcxx . . $ ScaleneHeader#getObject(130a85129859ac6a).(result));
219 } else if (sz > new_size) {
220 TheHeapWrappercxx . . $ TheHeapWrapper#::register_freecxx . . $ HL/HeapWrapper#register_free(3bdecba87b0fe3d5).(sz - new_size, ptr);
221 }
222 }
223 ScaleneHeadercxx . . $ ScaleneHeader#::setSizecxx . . $ ScaleneHeader#setSize(5500c4293b1bbef0).(ScaleneHeadercxx . . $ ScaleneHeader#::getObjectcxx . . $ ScaleneHeader#getObject(130a85129859ac6a).(result), new_size);
224 p = ScaleneHeadercxx . . $ ScaleneHeader#::getObjectcxx . . $ ScaleneHeader#getObject(130a85129859ac6a).(result);
225 return p;
226 }
227
228 static inline void *local_calloccxx . . $ MakeLocalAllocator#local_calloc(dfa4a9ebc9436eff).(void *ctx, size_t nelem, size_t elsize) {
229 const auto nbytes = nelem * elsize;
230 void *obj = local_malloccxx . . $ MakeLocalAllocator#local_malloc(417ab160f6baadc6).(ctx, nbytes);
231 if (true) { // obj) {
232 memset(obj, 0, nbytes);
233 }
234 return obj;
235 }
236
237 private:
238 static constexpr size_t MAGIC_NUMBERcxx . . $ MakeLocalAllocator#MAGIC_NUMBER. = 0x01020304;
239 };
240
241 // from pywhere.hpp
242 decltype(p_whereInPythoncxx . . $ p_whereInPython.)
243 __attribute((visibility("default"))) p_whereInPythoncxx . . $ p_whereInPython.{nullptr};
244
245 stdcxx . . $ std/::atomic_boolcxx . . $ std/atomic_bool# __attribute((visibility("default"))) p_scalene_donecxx . . $ p_scalene_done.{true};
246
247 static MakeLocalAllocatorcxx . . $ MakeLocalAllocator#<PYMEM_DOMAIN_MEM> l_memcxx . . $ l_mem.;
248 static MakeLocalAllocatorcxx . . $ MakeLocalAllocator#<PYMEM_DOMAIN_OBJ> l_objcxx . . $ l_obj.;
249
250 #if defined(__APPLE__)
251 MAC_INTERPOSE(xxmemcpy, memcpy);
252 MAC_INTERPOSE(xxmemmove, memmove);
253 MAC_INTERPOSE(xxstrcpy, strcpy);
254 #endif
255
256 #endif