blob: e9cbbf84de1f32f1e4b428920621b37608701408 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 bart.vanassche@gmail.com
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
21
22 The GNU General Public License is contained in the file COPYING.
23*/
24
25
26#include "drd_malloc_wrappers.h"
27#include "drd_thread.h"
28#include "pub_tool_basics.h"
29#include "pub_tool_execontext.h"
30#include "pub_tool_hashtable.h"
31#include "pub_tool_libcassert.h"
32#include "pub_tool_libcbase.h"
33#include "pub_tool_libcprint.h"
34#include "pub_tool_mallocfree.h"
35#include "pub_tool_options.h"
36#include "pub_tool_replacemalloc.h"
37#include "pub_tool_threadstate.h"
38#include "pub_tool_tooliface.h"
39
40
41/*------------------------------------------------------------*/
barte8a274c2008-04-20 08:33:10 +000042/*--- Definitions ---*/
sewardjaf44c822007-11-25 14:01:38 +000043/*------------------------------------------------------------*/
44
45
46typedef struct _DRD_Chunk {
bart3772a982008-03-15 08:11:03 +000047 struct _DRD_Chunk* next;
48 Addr data; // ptr to actual block
49 SizeT size : (sizeof(UWord)*8)-2; //size requested; 30 or 62 bits
50 ExeContext* where; // where it was allocated
sewardjaf44c822007-11-25 14:01:38 +000051} DRD_Chunk;
52
53static StartUsingMem s_start_using_mem_callback;
54static StopUsingMem s_stop_using_mem_callback;
55/* Stats ... */
56static SizeT cmalloc_n_mallocs = 0;
57static SizeT cmalloc_n_frees = 0;
58static SizeT cmalloc_bs_mallocd = 0;
59
60
61/*------------------------------------------------------------*/
62/*--- Tracking malloc'd and free'd blocks ---*/
63/*------------------------------------------------------------*/
64
65/* Record malloc'd blocks. */
66static VgHashTable drd_malloc_list = NULL;
67
68
69/* Allocate its shadow chunk, put it on the appropriate list. */
70static
71DRD_Chunk* create_DRD_Chunk(ThreadId tid, Addr p, SizeT size)
72{
bart3772a982008-03-15 08:11:03 +000073 DRD_Chunk* mc = VG_(malloc)(sizeof(DRD_Chunk));
74 mc->data = p;
75 mc->size = size;
76 mc->where = VG_(record_ExeContext)(tid, 0);
sewardjaf44c822007-11-25 14:01:38 +000077
bart3772a982008-03-15 08:11:03 +000078 return mc;
sewardjaf44c822007-11-25 14:01:38 +000079}
80
81/*------------------------------------------------------------*/
82/*--- client_malloc(), etc ---*/
83/*------------------------------------------------------------*/
84
85/* Allocate memory and note change in memory available */
86static
87__inline__
88void* drd_new_block(ThreadId tid,
89 SizeT size, SizeT align,
90 Bool is_zeroed)
91{
bart3772a982008-03-15 08:11:03 +000092 Addr p;
sewardjaf44c822007-11-25 14:01:38 +000093
bart3772a982008-03-15 08:11:03 +000094 cmalloc_n_mallocs ++;
sewardjaf44c822007-11-25 14:01:38 +000095
bart3772a982008-03-15 08:11:03 +000096 // Allocate and zero
97 p = (Addr)VG_(cli_malloc)(align, size);
98 if (!p) {
99 return NULL;
100 }
101 if (is_zeroed) VG_(memset)((void*)p, 0, size);
sewardj7cf4e6b2008-05-01 20:24:26 +0000102 s_start_using_mem_callback(p, p + size, 0/*ec_uniq*/);
sewardjaf44c822007-11-25 14:01:38 +0000103
bart3772a982008-03-15 08:11:03 +0000104 // Only update this stat if allocation succeeded.
105 cmalloc_bs_mallocd += size;
sewardjaf44c822007-11-25 14:01:38 +0000106
bart3772a982008-03-15 08:11:03 +0000107 VG_(HT_add_node)(drd_malloc_list, create_DRD_Chunk(tid, p, size));
sewardjaf44c822007-11-25 14:01:38 +0000108
bart3772a982008-03-15 08:11:03 +0000109 return (void*)p;
sewardjaf44c822007-11-25 14:01:38 +0000110}
111
112static
113void* drd_malloc(ThreadId tid, SizeT n)
114{
bart3772a982008-03-15 08:11:03 +0000115 return drd_new_block(tid, n, VG_(clo_alignment), /*is_zeroed*/False);
sewardjaf44c822007-11-25 14:01:38 +0000116}
117
118static
119void* drd_memalign(ThreadId tid, SizeT align, SizeT n)
120{
bart3772a982008-03-15 08:11:03 +0000121 return drd_new_block(tid, n, align, /*is_zeroed*/False);
sewardjaf44c822007-11-25 14:01:38 +0000122}
123
124static
125void* drd_calloc(ThreadId tid, SizeT nmemb, SizeT size1)
126{
bart3772a982008-03-15 08:11:03 +0000127 return drd_new_block(tid, nmemb*size1, VG_(clo_alignment),
128 /*is_zeroed*/True);
sewardjaf44c822007-11-25 14:01:38 +0000129}
130
131static
132__inline__
133void drd_handle_free(ThreadId tid, Addr p)
134{
bart3772a982008-03-15 08:11:03 +0000135 DRD_Chunk* mc;
sewardjaf44c822007-11-25 14:01:38 +0000136
bart3772a982008-03-15 08:11:03 +0000137 cmalloc_n_frees++;
sewardjaf44c822007-11-25 14:01:38 +0000138
bart3772a982008-03-15 08:11:03 +0000139 mc = VG_(HT_remove)(drd_malloc_list, (UWord)p);
140 if (mc == NULL)
141 {
142 tl_assert(0);
143 }
144 else
145 {
barte8a274c2008-04-20 08:33:10 +0000146 tl_assert(p == mc->data);
bart84595c02008-03-22 17:35:28 +0000147 if (mc->size > 0)
148 s_stop_using_mem_callback(mc->data, mc->size);
barte8a274c2008-04-20 08:33:10 +0000149 VG_(cli_free)((void*)p);
bart3772a982008-03-15 08:11:03 +0000150 VG_(free)(mc);
151 }
sewardjaf44c822007-11-25 14:01:38 +0000152}
153
154static
155void drd_free(ThreadId tid, void* p)
156{
bart3772a982008-03-15 08:11:03 +0000157 drd_handle_free(tid, (Addr)p);
sewardjaf44c822007-11-25 14:01:38 +0000158}
159
160static
161void* drd_realloc(ThreadId tid, void* p_old, SizeT new_size)
162{
bart3772a982008-03-15 08:11:03 +0000163 DRD_Chunk* mc;
164 void* p_new;
165 SizeT old_size;
sewardjaf44c822007-11-25 14:01:38 +0000166
bart3772a982008-03-15 08:11:03 +0000167 cmalloc_n_frees ++;
168 cmalloc_n_mallocs ++;
169 cmalloc_bs_mallocd += new_size;
sewardjaf44c822007-11-25 14:01:38 +0000170
bart3772a982008-03-15 08:11:03 +0000171 /* Remove the old block */
172 mc = VG_(HT_remove)(drd_malloc_list, (UWord)p_old);
173 if (mc == NULL) {
174 tl_assert(0);
175 return NULL;
176 }
sewardjaf44c822007-11-25 14:01:38 +0000177
bart3772a982008-03-15 08:11:03 +0000178 old_size = mc->size;
sewardjaf44c822007-11-25 14:01:38 +0000179
bart3772a982008-03-15 08:11:03 +0000180 if (old_size == new_size)
181 {
182 /* size unchanged */
183 mc->where = VG_(record_ExeContext)(tid, 0);
184 p_new = p_old;
sewardjaf44c822007-11-25 14:01:38 +0000185
bart3772a982008-03-15 08:11:03 +0000186 }
187 else if (old_size > new_size)
188 {
189 /* new size is smaller */
190 s_stop_using_mem_callback(mc->data + new_size, old_size);
191 mc->size = new_size;
192 mc->where = VG_(record_ExeContext)(tid, 0);
193 p_new = p_old;
sewardjaf44c822007-11-25 14:01:38 +0000194
bart3772a982008-03-15 08:11:03 +0000195 }
196 else
197 {
198 /* new size is bigger */
199 /* Get new memory */
200 const Addr a_new = (Addr)VG_(cli_malloc)(VG_(clo_alignment), new_size);
sewardjaf44c822007-11-25 14:01:38 +0000201
bart3772a982008-03-15 08:11:03 +0000202 if (a_new)
203 {
204 /* Copy from old to new */
205 VG_(memcpy)((void*)a_new, p_old, mc->size);
sewardjaf44c822007-11-25 14:01:38 +0000206
bart3772a982008-03-15 08:11:03 +0000207 /* Free old memory */
208 s_stop_using_mem_callback(mc->data, mc->size);
209 VG_(free)(mc);
sewardjaf44c822007-11-25 14:01:38 +0000210
bart3772a982008-03-15 08:11:03 +0000211 // Allocate a new chunk.
212 mc = create_DRD_Chunk(tid, a_new, new_size);
sewardj7cf4e6b2008-05-01 20:24:26 +0000213 s_start_using_mem_callback(a_new, a_new + new_size, 0/*ec_uniq*/);
bart3772a982008-03-15 08:11:03 +0000214 }
215 else
216 {
217 /* Allocation failed -- leave original block untouched. */
218 }
sewardjaf44c822007-11-25 14:01:38 +0000219
bart3772a982008-03-15 08:11:03 +0000220 p_new = (void*)a_new;
221 }
sewardjaf44c822007-11-25 14:01:38 +0000222
bart3772a982008-03-15 08:11:03 +0000223 // Now insert the new mc (with a possibly new 'data' field) into
224 // malloc_list. If this realloc() did not increase the memory size, we
225 // will have removed and then re-added mc unnecessarily. But that's ok
226 // because shrinking a block with realloc() is (presumably) much rarer
227 // than growing it, and this way simplifies the growing case.
228 VG_(HT_add_node)(drd_malloc_list, mc);
sewardjaf44c822007-11-25 14:01:38 +0000229
bart3772a982008-03-15 08:11:03 +0000230 return p_new;
sewardjaf44c822007-11-25 14:01:38 +0000231}
232
233static
234void* drd___builtin_new(ThreadId tid, SizeT n)
235{
bart3772a982008-03-15 08:11:03 +0000236 void* const result = drd_new_block(tid, n, VG_(clo_alignment), /*is_zeroed*/False);
237 //VG_(message)(Vg_DebugMsg, "__builtin_new(%d, %d) = %p", tid, n, result);
238 return result;
sewardjaf44c822007-11-25 14:01:38 +0000239}
240
241static
242void drd___builtin_delete(ThreadId tid, void* p)
243{
bart3772a982008-03-15 08:11:03 +0000244 //VG_(message)(Vg_DebugMsg, "__builtin_delete(%d, %p)", tid, p);
245 drd_handle_free(tid, (Addr)p);
sewardjaf44c822007-11-25 14:01:38 +0000246}
247
248static
249void* drd___builtin_vec_new(ThreadId tid, SizeT n)
250{
bart3772a982008-03-15 08:11:03 +0000251 return drd_new_block(tid, n, VG_(clo_alignment), /*is_zeroed*/False);
sewardjaf44c822007-11-25 14:01:38 +0000252}
253
254static
255void drd___builtin_vec_delete(ThreadId tid, void* p)
256{
bart3772a982008-03-15 08:11:03 +0000257 drd_handle_free(tid, (Addr)p);
sewardjaf44c822007-11-25 14:01:38 +0000258}
259
260void drd_register_malloc_wrappers(const StartUsingMem start_using_mem_callback,
261 const StopUsingMem stop_using_mem_callback)
262{
bart3772a982008-03-15 08:11:03 +0000263 tl_assert(drd_malloc_list == 0);
264 drd_malloc_list = VG_(HT_construct)("drd_malloc_list"); // a big prime
265 tl_assert(drd_malloc_list != 0);
266 tl_assert(stop_using_mem_callback);
sewardjaf44c822007-11-25 14:01:38 +0000267
bart3772a982008-03-15 08:11:03 +0000268 s_start_using_mem_callback = start_using_mem_callback;
269 s_stop_using_mem_callback = stop_using_mem_callback;
sewardjaf44c822007-11-25 14:01:38 +0000270
bart3772a982008-03-15 08:11:03 +0000271 VG_(needs_malloc_replacement)(drd_malloc,
272 drd___builtin_new,
273 drd___builtin_vec_new,
274 drd_memalign,
275 drd_calloc,
276 drd_free,
277 drd___builtin_delete,
278 drd___builtin_vec_delete,
279 drd_realloc,
280 0);
sewardjaf44c822007-11-25 14:01:38 +0000281}
282
283Bool drd_heap_addrinfo(Addr const a,
284 Addr* const data,
285 SizeT* const size,
286 ExeContext** const where)
287{
bart3772a982008-03-15 08:11:03 +0000288 DRD_Chunk* mc;
sewardjaf44c822007-11-25 14:01:38 +0000289
bart3772a982008-03-15 08:11:03 +0000290 tl_assert(data);
291 tl_assert(size);
292 tl_assert(where);
sewardjaf44c822007-11-25 14:01:38 +0000293
bart3772a982008-03-15 08:11:03 +0000294 VG_(HT_ResetIter)(drd_malloc_list);
295 while ((mc = VG_(HT_Next)(drd_malloc_list)))
296 {
297 if (mc->data <= a && a < mc->data + mc->size)
298 {
299 *data = mc->data;
300 *size = mc->size;
301 *where = mc->where;
302 return True;
303 }
304 }
305 return False;
sewardjaf44c822007-11-25 14:01:38 +0000306}
307
308/*------------------------------------------------------------*/
309/*--- Statistics printing ---*/
310/*------------------------------------------------------------*/
311
312void drd_print_malloc_stats(void)
313{
bart3772a982008-03-15 08:11:03 +0000314 DRD_Chunk* mc;
315 SizeT nblocks = 0;
316 SizeT nbytes = 0;
sewardjaf44c822007-11-25 14:01:38 +0000317
bart3772a982008-03-15 08:11:03 +0000318 if (VG_(clo_verbosity) == 0)
319 return;
320 if (VG_(clo_xml))
321 return;
sewardjaf44c822007-11-25 14:01:38 +0000322
bart3772a982008-03-15 08:11:03 +0000323 /* Count memory still in use. */
324 VG_(HT_ResetIter)(drd_malloc_list);
325 while ((mc = VG_(HT_Next)(drd_malloc_list)))
326 {
327 nblocks++;
328 nbytes += mc->size;
329 }
sewardjaf44c822007-11-25 14:01:38 +0000330
bart3772a982008-03-15 08:11:03 +0000331 VG_(message)(Vg_DebugMsg,
332 "malloc/free: in use at exit: %lu bytes in %lu blocks.",
333 nbytes, nblocks);
334 VG_(message)(Vg_DebugMsg,
335 "malloc/free: %lu allocs, %lu frees, %lu bytes allocated.",
336 cmalloc_n_mallocs,
337 cmalloc_n_frees, cmalloc_bs_mallocd);
338 if (VG_(clo_verbosity) > 1)
339 VG_(message)(Vg_DebugMsg, " ");
sewardjaf44c822007-11-25 14:01:38 +0000340}
341
342/*--------------------------------------------------------------------*/
343/*--- end ---*/
344/*--------------------------------------------------------------------*/