Debian Codegraph @

src/preload/sysprof-memory-collector.c

raw
1 /* sysprof-memory-collector.c
2 *
3 * Copyright 2020 Christian Hergert <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #include "config.h"
22
23 #include <dlfcn.h>
24 #include <glib.h>
25 #include <sched.h>
26 #include <stdlib.h>
27 #include <sys/syscall.h>
28 #include <sys/types.h>
29 #include <sysprof-capture.h>
30 #include <unistd.h>
31
32 #include "backtrace-helper.h"
33
34 #include "gconstructor.h"
35
36 typedef void *(* RealMalloc) (size_t);
37 typedef void (* RealFree) (void *);
38 typedef void *(* RealCalloc) (size_t, size_t);
39 typedef void *(* RealRealloc) (void *, size_t);
40 typedef void *(* RealAlignedAlloc) (size_t, size_t);
41 typedef int (* RealPosixMemalign) (void **, size_t, size_t);
42 typedef void *(* RealMemalign) (size_t, size_t);
43
44 typedef struct
45 {
46 char buf[4092];
47 int off;
48 } ScratchAlloc;
49
50 static void hook_memtable (void);
51 static void *scratch_malloc (size_t);
52 static void *scratch_realloc (void *, size_t);
53 static void *scratch_calloc (size_t, size_t);
54 static void scratch_free (void *);
55
56 static int collector_ready;
57 static int hooked;
58 static ScratchAlloc scratch;
59 static RealCalloc real_calloc = scratch_calloc;
60 static RealFree real_free = scratch_free;
61 static RealMalloc real_malloc = scratch_malloc;
62 static RealRealloc real_realloc = scratch_realloc;
63 static RealAlignedAlloc real_aligned_alloc;
64 static RealPosixMemalign real_posix_memalign;
65 static RealMemalign real_memalign;
66
67 #if defined (G_HAS_CONSTRUCTORS)
68 # ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
69 # pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(collector_init_ctor)
70 # endif
71 G_DEFINE_CONSTRUCTOR(collector_init_ctor)
72 #else
73 # error Your platform/compiler is missing constructor support
74 #endif
75
76 static void
77 collector_init_ctor (void)
78 {
79 backtrace_init ();
80 sysprof_collector_init ();
81 collector_ready = TRUE;
82 }
83
84 static void *
85 scratch_malloc (size_t size)
86 {
87 hook_memtable ();
88 return real_malloc (size);
89 }
90
91 static void *
92 scratch_realloc (void *ptr,
93 size_t size)
94 {
95 hook_memtable ();
96 return real_realloc (ptr, size);
97 }
98
99 static void *
100 scratch_calloc (size_t nmemb,
101 size_t size)
102 {
103 void *ret;
104
105 /* re-entrant, but forces early hook in case calloc is
106 * called before any of our other hooks.
107 */
108 if (!hooked)
109 hook_memtable ();
110
111 size *= nmemb;
112 ret = &scratch.buf[scratch.off];
113 scratch.off += size;
114
115 return ret;
116 }
117
118 static void
119 scratch_free (void *ptr)
120 {
121 }
122
123 static void
124 hook_memtable (void)
125 {
126 if (hooked)
127 return;
128
129 hooked = 1;
130
131 real_calloc = dlsym (RTLD_NEXT, "calloc");
132 real_free = dlsym (RTLD_NEXT, "free");
133 real_malloc = dlsym (RTLD_NEXT, "malloc");
134 real_realloc = dlsym (RTLD_NEXT, "realloc");
135 real_aligned_alloc = dlsym (RTLD_NEXT, "aligned_alloc");
136 real_posix_memalign = dlsym (RTLD_NEXT, "posix_memalign");
137 real_memalign = dlsym (RTLD_NEXT, "memalign");
138
139 unsetenv ("LD_PRELOAD");
140 }
141
142 static inline void
143 track_malloc (void *ptr,
144 size_t size)
145 {
146 if G_LIKELY (ptr && collector_ready)
147 sysprof_collector_allocate (GPOINTER_TO_SIZE (ptr),
148 size,
149 backtrace_func,
150 NULL);
151 }
152
153 static inline void
154 track_free (void *ptr)
155 {
156 if G_LIKELY (ptr && collector_ready)
157 sysprof_collector_allocate (GPOINTER_TO_SIZE (ptr),
158 0,
159 NULL,
160 NULL);
161 }
162
163 void *
164 malloc (size_t size)
165 {
166 void *ret = real_malloc (size);
167 track_malloc (ret, size);
168 return ret;
169 }
170
171 void *
172 calloc (size_t nmemb,
173 size_t size)
174 {
175 void *ret = real_calloc (nmemb, size);
176 track_malloc (ret, size);
177 return ret;
178 }
179
180 void *
181 realloc (void *ptr,
182 size_t size)
183 {
184 void *ret = real_realloc (ptr, size);
185 track_free (ptr);
186 track_malloc (ret, size);
187 return ret;
188 }
189
190 void
191 free (void *ptr)
192 {
193 if G_LIKELY (ptr < (void *)scratch.buf ||
194 ptr >= (void *)&scratch.buf[sizeof scratch.buf])
195 {
196 real_free (ptr);
197 track_free (ptr);
198 }
199 }
200
201 void *
202 aligned_alloc (size_t alignment,
203 size_t size)
204 {
205 void *ret = real_aligned_alloc (alignment, size);
206 track_malloc (ret, size);
207 return ret;
208 }
209
210 int
211 posix_memalign (void **memptr,
212 size_t alignment,
213 size_t size)
214 {
215 int ret = real_posix_memalign (memptr, alignment, size);
216 track_malloc (*memptr, size);
217 return ret;
218 }
219
220 void *
221 memalign (size_t alignment,
222 size_t size)
223 {
224 void *ret = real_memalign (alignment, size);
225 track_malloc (ret, size);
226 return ret;
227 }