blob: 62da58ec039bb22241bcf60f286e3cdb697ea264 [file] [log] [blame]
bartbedfd232009-03-26 19:07:15 +00001/* -*- mode: C; c-basic-offset: 3; -*- */
sewardjaf44c822007-11-25 14:01:38 +00002/*
bart86562bd2009-02-16 19:43:56 +00003 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00004
bart86562bd2009-02-16 19:43:56 +00005 Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
sewardjaf44c822007-11-25 14:01:38 +00006
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
bart246fbf22009-02-15 14:46:17 +000041/* Local type definitions. */
sewardjaf44c822007-11-25 14:01:38 +000042
43typedef struct _DRD_Chunk {
bartbedfd232009-03-26 19:07:15 +000044 struct _DRD_Chunk* next;
45 Addr data; // ptr to actual block
46 SizeT size : (sizeof(UWord)*8)-2; //size requested; 30 or 62 bits
47 ExeContext* where; // where it was allocated
sewardjaf44c822007-11-25 14:01:38 +000048} DRD_Chunk;
49
bart246fbf22009-02-15 14:46:17 +000050
51/* Local variables. */
52
53static StartUsingMem DRD_(s_start_using_mem_callback);
54static StopUsingMem DRD_(s_stop_using_mem_callback);
sewardjaf44c822007-11-25 14:01:38 +000055/* Stats ... */
bart246fbf22009-02-15 14:46:17 +000056static SizeT DRD_(s_cmalloc_n_mallocs) = 0;
57static SizeT DRD_(s_cmalloc_n_frees) = 0;
58static SizeT DRD_(s_cmalloc_bs_mallocd) = 0;
59/* Record malloc'd blocks. */
60static VgHashTable DRD_(s_malloc_list) = NULL;
sewardjaf44c822007-11-25 14:01:38 +000061
62
63/*------------------------------------------------------------*/
64/*--- Tracking malloc'd and free'd blocks ---*/
65/*------------------------------------------------------------*/
66
bart246fbf22009-02-15 14:46:17 +000067/** Allocate its shadow chunk, put it on the appropriate list. */
68static DRD_Chunk* DRD_(create_chunk)(ThreadId tid, Addr p, SizeT size)
sewardjaf44c822007-11-25 14:01:38 +000069{
bartbedfd232009-03-26 19:07:15 +000070 DRD_Chunk* mc = VG_(malloc)("drd.malloc_wrappers.cDC.1",
71 sizeof(DRD_Chunk));
72 mc->data = p;
73 mc->size = size;
74 mc->where = VG_(record_ExeContext)(tid, 0);
sewardjaf44c822007-11-25 14:01:38 +000075
bartbedfd232009-03-26 19:07:15 +000076 return mc;
sewardjaf44c822007-11-25 14:01:38 +000077}
78
79/*------------------------------------------------------------*/
80/*--- client_malloc(), etc ---*/
81/*------------------------------------------------------------*/
82
83/* Allocate memory and note change in memory available */
84static
85__inline__
bart246fbf22009-02-15 14:46:17 +000086void* DRD_(new_block)(ThreadId tid,
87 SizeT size, SizeT align,
88 Bool is_zeroed)
sewardjaf44c822007-11-25 14:01:38 +000089{
bartbedfd232009-03-26 19:07:15 +000090 Addr p;
sewardjaf44c822007-11-25 14:01:38 +000091
bartbedfd232009-03-26 19:07:15 +000092 DRD_(s_cmalloc_n_mallocs) ++;
sewardjaf44c822007-11-25 14:01:38 +000093
bartbedfd232009-03-26 19:07:15 +000094 // Allocate and zero
95 p = (Addr)VG_(cli_malloc)(align, size);
96 if (!p) {
97 return NULL;
98 }
99 if (is_zeroed) VG_(memset)((void*)p, 0, size);
100 DRD_(s_start_using_mem_callback)(p, p + size, 0/*ec_uniq*/);
sewardjaf44c822007-11-25 14:01:38 +0000101
bartbedfd232009-03-26 19:07:15 +0000102 // Only update this stat if allocation succeeded.
103 DRD_(s_cmalloc_bs_mallocd) += size;
sewardjaf44c822007-11-25 14:01:38 +0000104
bartbedfd232009-03-26 19:07:15 +0000105 VG_(HT_add_node)(DRD_(s_malloc_list), DRD_(create_chunk)(tid, p, size));
sewardjaf44c822007-11-25 14:01:38 +0000106
bartbedfd232009-03-26 19:07:15 +0000107 return (void*)p;
sewardjaf44c822007-11-25 14:01:38 +0000108}
109
bart246fbf22009-02-15 14:46:17 +0000110static void* DRD_(malloc)(ThreadId tid, SizeT n)
sewardjaf44c822007-11-25 14:01:38 +0000111{
bartbedfd232009-03-26 19:07:15 +0000112 return DRD_(new_block)(tid, n, VG_(clo_alignment), /*is_zeroed*/False);
sewardjaf44c822007-11-25 14:01:38 +0000113}
114
bart246fbf22009-02-15 14:46:17 +0000115static void* DRD_(memalign)(ThreadId tid, SizeT align, SizeT n)
sewardjaf44c822007-11-25 14:01:38 +0000116{
bartbedfd232009-03-26 19:07:15 +0000117 return DRD_(new_block)(tid, n, align, /*is_zeroed*/False);
sewardjaf44c822007-11-25 14:01:38 +0000118}
119
bart246fbf22009-02-15 14:46:17 +0000120static void* DRD_(calloc)(ThreadId tid, SizeT nmemb, SizeT size1)
sewardjaf44c822007-11-25 14:01:38 +0000121{
bartbedfd232009-03-26 19:07:15 +0000122 return DRD_(new_block)(tid, nmemb*size1, VG_(clo_alignment),
123 /*is_zeroed*/True);
sewardjaf44c822007-11-25 14:01:38 +0000124}
125
bart246fbf22009-02-15 14:46:17 +0000126static __inline__ void DRD_(handle_free)(ThreadId tid, Addr p)
sewardjaf44c822007-11-25 14:01:38 +0000127{
bartbedfd232009-03-26 19:07:15 +0000128 DRD_Chunk* mc;
sewardjaf44c822007-11-25 14:01:38 +0000129
bartbedfd232009-03-26 19:07:15 +0000130 DRD_(s_cmalloc_n_frees)++;
sewardjaf44c822007-11-25 14:01:38 +0000131
bartbedfd232009-03-26 19:07:15 +0000132 mc = VG_(HT_remove)(DRD_(s_malloc_list), (UWord)p);
133 if (mc == NULL)
134 {
135 tl_assert(0);
136 }
137 else
138 {
139 tl_assert(p == mc->data);
140 if (mc->size > 0)
141 DRD_(s_stop_using_mem_callback)(mc->data, mc->size);
142 VG_(cli_free)((void*)p);
143 VG_(free)(mc);
144 }
sewardjaf44c822007-11-25 14:01:38 +0000145}
146
bart246fbf22009-02-15 14:46:17 +0000147static void DRD_(free)(ThreadId tid, void* p)
sewardjaf44c822007-11-25 14:01:38 +0000148{
bartbedfd232009-03-26 19:07:15 +0000149 DRD_(handle_free)(tid, (Addr)p);
sewardjaf44c822007-11-25 14:01:38 +0000150}
151
bart246fbf22009-02-15 14:46:17 +0000152static void* DRD_(realloc)(ThreadId tid, void* p_old, SizeT new_size)
sewardjaf44c822007-11-25 14:01:38 +0000153{
bartbedfd232009-03-26 19:07:15 +0000154 DRD_Chunk* mc;
155 void* p_new;
156 SizeT old_size;
sewardjaf44c822007-11-25 14:01:38 +0000157
bartbedfd232009-03-26 19:07:15 +0000158 DRD_(s_cmalloc_n_frees) ++;
159 DRD_(s_cmalloc_n_mallocs) ++;
160 DRD_(s_cmalloc_bs_mallocd) += new_size;
sewardjaf44c822007-11-25 14:01:38 +0000161
bartbedfd232009-03-26 19:07:15 +0000162 /* Remove the old block */
163 mc = VG_(HT_remove)(DRD_(s_malloc_list), (UWord)p_old);
164 if (mc == NULL) {
165 tl_assert(0);
166 return NULL;
167 }
sewardjaf44c822007-11-25 14:01:38 +0000168
bartbedfd232009-03-26 19:07:15 +0000169 old_size = mc->size;
sewardjaf44c822007-11-25 14:01:38 +0000170
bartbedfd232009-03-26 19:07:15 +0000171 if (old_size == new_size)
172 {
173 /* size unchanged */
174 mc->where = VG_(record_ExeContext)(tid, 0);
175 p_new = p_old;
sewardjaf44c822007-11-25 14:01:38 +0000176
bartbedfd232009-03-26 19:07:15 +0000177 }
178 else if (old_size > new_size)
179 {
180 /* new size is smaller */
181 DRD_(s_stop_using_mem_callback)(mc->data + new_size, old_size);
182 mc->size = new_size;
183 mc->where = VG_(record_ExeContext)(tid, 0);
184 p_new = p_old;
sewardjaf44c822007-11-25 14:01:38 +0000185
bartbedfd232009-03-26 19:07:15 +0000186 }
187 else
188 {
189 /* new size is bigger */
190 /* Get new memory */
191 const Addr a_new = (Addr)VG_(cli_malloc)(VG_(clo_alignment), new_size);
sewardjaf44c822007-11-25 14:01:38 +0000192
bartbedfd232009-03-26 19:07:15 +0000193 if (a_new)
194 {
195 /* Copy from old to new */
196 VG_(memcpy)((void*)a_new, p_old, mc->size);
sewardjaf44c822007-11-25 14:01:38 +0000197
bartbedfd232009-03-26 19:07:15 +0000198 /* Free old memory */
199 DRD_(s_stop_using_mem_callback)(mc->data, mc->size);
200 VG_(free)(mc);
sewardjaf44c822007-11-25 14:01:38 +0000201
bartbedfd232009-03-26 19:07:15 +0000202 // Allocate a new chunk.
203 mc = DRD_(create_chunk)(tid, a_new, new_size);
204 DRD_(s_start_using_mem_callback)(a_new, a_new + new_size,
205 0/*ec_uniq*/);
206 }
207 else
208 {
209 /* Allocation failed -- leave original block untouched. */
210 }
sewardjaf44c822007-11-25 14:01:38 +0000211
bartbedfd232009-03-26 19:07:15 +0000212 p_new = (void*)a_new;
213 }
sewardjaf44c822007-11-25 14:01:38 +0000214
bartbedfd232009-03-26 19:07:15 +0000215 // Now insert the new mc (with a possibly new 'data' field) into
216 // malloc_list. If this realloc() did not increase the memory size, we
217 // will have removed and then re-added mc unnecessarily. But that's ok
218 // because shrinking a block with realloc() is (presumably) much rarer
219 // than growing it, and this way simplifies the growing case.
220 VG_(HT_add_node)(DRD_(s_malloc_list), mc);
sewardjaf44c822007-11-25 14:01:38 +0000221
bartbedfd232009-03-26 19:07:15 +0000222 return p_new;
sewardjaf44c822007-11-25 14:01:38 +0000223}
224
bart246fbf22009-02-15 14:46:17 +0000225static void* DRD_(__builtin_new)(ThreadId tid, SizeT n)
sewardjaf44c822007-11-25 14:01:38 +0000226{
bartbedfd232009-03-26 19:07:15 +0000227 void* const result = DRD_(new_block)(tid, n, VG_(clo_alignment),
228 /*is_zeroed*/False);
229 //VG_(message)(Vg_DebugMsg, "__builtin_new(%d, %d) = %p", tid, n, result);
230 return result;
sewardjaf44c822007-11-25 14:01:38 +0000231}
232
bart246fbf22009-02-15 14:46:17 +0000233static void DRD_(__builtin_delete)(ThreadId tid, void* p)
sewardjaf44c822007-11-25 14:01:38 +0000234{
bartbedfd232009-03-26 19:07:15 +0000235 //VG_(message)(Vg_DebugMsg, "__builtin_delete(%d, %p)", tid, p);
236 DRD_(handle_free)(tid, (Addr)p);
sewardjaf44c822007-11-25 14:01:38 +0000237}
238
bart246fbf22009-02-15 14:46:17 +0000239static void* DRD_(__builtin_vec_new)(ThreadId tid, SizeT n)
sewardjaf44c822007-11-25 14:01:38 +0000240{
bartbedfd232009-03-26 19:07:15 +0000241 return DRD_(new_block)(tid, n, VG_(clo_alignment), /*is_zeroed*/False);
sewardjaf44c822007-11-25 14:01:38 +0000242}
243
bart246fbf22009-02-15 14:46:17 +0000244static void DRD_(__builtin_vec_delete)(ThreadId tid, void* p)
sewardjaf44c822007-11-25 14:01:38 +0000245{
bartbedfd232009-03-26 19:07:15 +0000246 DRD_(handle_free)(tid, (Addr)p);
sewardjaf44c822007-11-25 14:01:38 +0000247}
248
njn8b140de2009-02-17 04:31:18 +0000249static SizeT DRD_(malloc_usable_size) ( ThreadId tid, void* p )
250{
251 DRD_Chunk *mc = VG_(HT_lookup)( DRD_(s_malloc_list), (UWord)p );
252
253 // There may be slop, but pretend there isn't because only the asked-for
254 // area will have been shadowed properly.
255 return ( mc ? mc->size : 0 );
256}
257
bart246fbf22009-02-15 14:46:17 +0000258void DRD_(register_malloc_wrappers)(const StartUsingMem start_callback,
259 const StopUsingMem stop_callback)
sewardjaf44c822007-11-25 14:01:38 +0000260{
bartbedfd232009-03-26 19:07:15 +0000261 tl_assert(DRD_(s_malloc_list) == 0);
262 DRD_(s_malloc_list) = VG_(HT_construct)("drd_malloc_list"); // a big prime
263 tl_assert(DRD_(s_malloc_list) != 0);
264 tl_assert(start_callback);
265 tl_assert(stop_callback);
sewardjaf44c822007-11-25 14:01:38 +0000266
bartbedfd232009-03-26 19:07:15 +0000267 DRD_(s_start_using_mem_callback) = start_callback;
268 DRD_(s_stop_using_mem_callback) = stop_callback;
sewardjaf44c822007-11-25 14:01:38 +0000269
bartbedfd232009-03-26 19:07:15 +0000270 VG_(needs_malloc_replacement)(DRD_(malloc),
271 DRD_(__builtin_new),
272 DRD_(__builtin_vec_new),
273 DRD_(memalign),
274 DRD_(calloc),
275 DRD_(free),
276 DRD_(__builtin_delete),
277 DRD_(__builtin_vec_delete),
278 DRD_(realloc),
279 DRD_(malloc_usable_size),
280 0);
sewardjaf44c822007-11-25 14:01:38 +0000281}
282
bart246fbf22009-02-15 14:46:17 +0000283Bool DRD_(heap_addrinfo)(Addr const a,
284 Addr* const data,
285 SizeT* const size,
286 ExeContext** const where)
sewardjaf44c822007-11-25 14:01:38 +0000287{
bartbedfd232009-03-26 19:07:15 +0000288 DRD_Chunk* mc;
sewardjaf44c822007-11-25 14:01:38 +0000289
bartbedfd232009-03-26 19:07:15 +0000290 tl_assert(data);
291 tl_assert(size);
292 tl_assert(where);
sewardjaf44c822007-11-25 14:01:38 +0000293
bartbedfd232009-03-26 19:07:15 +0000294 VG_(HT_ResetIter)(DRD_(s_malloc_list));
295 while ((mc = VG_(HT_Next)(DRD_(s_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
bart246fbf22009-02-15 14:46:17 +0000312void DRD_(print_malloc_stats)(void)
sewardjaf44c822007-11-25 14:01:38 +0000313{
bartbedfd232009-03-26 19:07:15 +0000314 DRD_Chunk* mc;
315 SizeT nblocks = 0;
316 SizeT nbytes = 0;
sewardjaf44c822007-11-25 14:01:38 +0000317
bartbedfd232009-03-26 19:07:15 +0000318 if (VG_(clo_verbosity) == 0)
319 return;
320 if (VG_(clo_xml))
321 return;
sewardjaf44c822007-11-25 14:01:38 +0000322
bartbedfd232009-03-26 19:07:15 +0000323 /* Count memory still in use. */
324 VG_(HT_ResetIter)(DRD_(s_malloc_list));
325 while ((mc = VG_(HT_Next)(DRD_(s_malloc_list))))
326 {
327 nblocks++;
328 nbytes += mc->size;
329 }
sewardjaf44c822007-11-25 14:01:38 +0000330
bartbedfd232009-03-26 19:07:15 +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 DRD_(s_cmalloc_n_mallocs),
337 DRD_(s_cmalloc_n_frees), DRD_(s_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/*--------------------------------------------------------------------*/