sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2008 Bart Van Assche |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 5 | 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 | /*------------------------------------------------------------*/ |
bart | e8a274c | 2008-04-20 08:33:10 +0000 | [diff] [blame] | 42 | /*--- Definitions ---*/ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 43 | /*------------------------------------------------------------*/ |
| 44 | |
| 45 | |
| 46 | typedef struct _DRD_Chunk { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 47 | 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 |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 51 | } DRD_Chunk; |
| 52 | |
| 53 | static StartUsingMem s_start_using_mem_callback; |
| 54 | static StopUsingMem s_stop_using_mem_callback; |
| 55 | /* Stats ... */ |
| 56 | static SizeT cmalloc_n_mallocs = 0; |
| 57 | static SizeT cmalloc_n_frees = 0; |
| 58 | static SizeT cmalloc_bs_mallocd = 0; |
| 59 | |
| 60 | |
| 61 | /*------------------------------------------------------------*/ |
| 62 | /*--- Tracking malloc'd and free'd blocks ---*/ |
| 63 | /*------------------------------------------------------------*/ |
| 64 | |
| 65 | /* Record malloc'd blocks. */ |
| 66 | static VgHashTable drd_malloc_list = NULL; |
| 67 | |
| 68 | |
| 69 | /* Allocate its shadow chunk, put it on the appropriate list. */ |
| 70 | static |
| 71 | DRD_Chunk* create_DRD_Chunk(ThreadId tid, Addr p, SizeT size) |
| 72 | { |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 73 | DRD_Chunk* mc = VG_(malloc)("drd.malloc_wrappers.cDC.1", |
| 74 | sizeof(DRD_Chunk)); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 75 | mc->data = p; |
| 76 | mc->size = size; |
| 77 | mc->where = VG_(record_ExeContext)(tid, 0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 78 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 79 | return mc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /*------------------------------------------------------------*/ |
| 83 | /*--- client_malloc(), etc ---*/ |
| 84 | /*------------------------------------------------------------*/ |
| 85 | |
| 86 | /* Allocate memory and note change in memory available */ |
| 87 | static |
| 88 | __inline__ |
| 89 | void* drd_new_block(ThreadId tid, |
| 90 | SizeT size, SizeT align, |
| 91 | Bool is_zeroed) |
| 92 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 93 | Addr p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 94 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 95 | cmalloc_n_mallocs ++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 96 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 97 | // Allocate and zero |
| 98 | p = (Addr)VG_(cli_malloc)(align, size); |
| 99 | if (!p) { |
| 100 | return NULL; |
| 101 | } |
| 102 | if (is_zeroed) VG_(memset)((void*)p, 0, size); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 103 | s_start_using_mem_callback(p, p + size, 0/*ec_uniq*/); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 104 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 105 | // Only update this stat if allocation succeeded. |
| 106 | cmalloc_bs_mallocd += size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 107 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 108 | VG_(HT_add_node)(drd_malloc_list, create_DRD_Chunk(tid, p, size)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 109 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 110 | return (void*)p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static |
| 114 | void* drd_malloc(ThreadId tid, SizeT n) |
| 115 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 116 | return drd_new_block(tid, n, VG_(clo_alignment), /*is_zeroed*/False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static |
| 120 | void* drd_memalign(ThreadId tid, SizeT align, SizeT n) |
| 121 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 122 | return drd_new_block(tid, n, align, /*is_zeroed*/False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static |
| 126 | void* drd_calloc(ThreadId tid, SizeT nmemb, SizeT size1) |
| 127 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 128 | return drd_new_block(tid, nmemb*size1, VG_(clo_alignment), |
| 129 | /*is_zeroed*/True); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static |
| 133 | __inline__ |
| 134 | void drd_handle_free(ThreadId tid, Addr p) |
| 135 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 136 | DRD_Chunk* mc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 137 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 138 | cmalloc_n_frees++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 139 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 140 | mc = VG_(HT_remove)(drd_malloc_list, (UWord)p); |
| 141 | if (mc == NULL) |
| 142 | { |
| 143 | tl_assert(0); |
| 144 | } |
| 145 | else |
| 146 | { |
bart | e8a274c | 2008-04-20 08:33:10 +0000 | [diff] [blame] | 147 | tl_assert(p == mc->data); |
bart | 84595c0 | 2008-03-22 17:35:28 +0000 | [diff] [blame] | 148 | if (mc->size > 0) |
| 149 | s_stop_using_mem_callback(mc->data, mc->size); |
bart | e8a274c | 2008-04-20 08:33:10 +0000 | [diff] [blame] | 150 | VG_(cli_free)((void*)p); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 151 | VG_(free)(mc); |
| 152 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static |
| 156 | void drd_free(ThreadId tid, void* p) |
| 157 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 158 | drd_handle_free(tid, (Addr)p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static |
| 162 | void* drd_realloc(ThreadId tid, void* p_old, SizeT new_size) |
| 163 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 164 | DRD_Chunk* mc; |
| 165 | void* p_new; |
| 166 | SizeT old_size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 167 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 168 | cmalloc_n_frees ++; |
| 169 | cmalloc_n_mallocs ++; |
| 170 | cmalloc_bs_mallocd += new_size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 171 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 172 | /* Remove the old block */ |
| 173 | mc = VG_(HT_remove)(drd_malloc_list, (UWord)p_old); |
| 174 | if (mc == NULL) { |
| 175 | tl_assert(0); |
| 176 | return NULL; |
| 177 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 178 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 179 | old_size = mc->size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 180 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 181 | if (old_size == new_size) |
| 182 | { |
| 183 | /* size unchanged */ |
| 184 | mc->where = VG_(record_ExeContext)(tid, 0); |
| 185 | p_new = p_old; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 186 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 187 | } |
| 188 | else if (old_size > new_size) |
| 189 | { |
| 190 | /* new size is smaller */ |
| 191 | s_stop_using_mem_callback(mc->data + new_size, old_size); |
| 192 | mc->size = new_size; |
| 193 | mc->where = VG_(record_ExeContext)(tid, 0); |
| 194 | p_new = p_old; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 195 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 196 | } |
| 197 | else |
| 198 | { |
| 199 | /* new size is bigger */ |
| 200 | /* Get new memory */ |
| 201 | const Addr a_new = (Addr)VG_(cli_malloc)(VG_(clo_alignment), new_size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 202 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 203 | if (a_new) |
| 204 | { |
| 205 | /* Copy from old to new */ |
| 206 | VG_(memcpy)((void*)a_new, p_old, mc->size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 207 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 208 | /* Free old memory */ |
| 209 | s_stop_using_mem_callback(mc->data, mc->size); |
| 210 | VG_(free)(mc); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 211 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 212 | // Allocate a new chunk. |
| 213 | mc = create_DRD_Chunk(tid, a_new, new_size); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 214 | s_start_using_mem_callback(a_new, a_new + new_size, 0/*ec_uniq*/); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 215 | } |
| 216 | else |
| 217 | { |
| 218 | /* Allocation failed -- leave original block untouched. */ |
| 219 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 220 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 221 | p_new = (void*)a_new; |
| 222 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 223 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 224 | // Now insert the new mc (with a possibly new 'data' field) into |
| 225 | // malloc_list. If this realloc() did not increase the memory size, we |
| 226 | // will have removed and then re-added mc unnecessarily. But that's ok |
| 227 | // because shrinking a block with realloc() is (presumably) much rarer |
| 228 | // than growing it, and this way simplifies the growing case. |
| 229 | VG_(HT_add_node)(drd_malloc_list, mc); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 230 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 231 | return p_new; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static |
| 235 | void* drd___builtin_new(ThreadId tid, SizeT n) |
| 236 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 237 | void* const result = drd_new_block(tid, n, VG_(clo_alignment), /*is_zeroed*/False); |
| 238 | //VG_(message)(Vg_DebugMsg, "__builtin_new(%d, %d) = %p", tid, n, result); |
| 239 | return result; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static |
| 243 | void drd___builtin_delete(ThreadId tid, void* p) |
| 244 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 245 | //VG_(message)(Vg_DebugMsg, "__builtin_delete(%d, %p)", tid, p); |
| 246 | drd_handle_free(tid, (Addr)p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static |
| 250 | void* drd___builtin_vec_new(ThreadId tid, SizeT n) |
| 251 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 252 | return drd_new_block(tid, n, VG_(clo_alignment), /*is_zeroed*/False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static |
| 256 | void drd___builtin_vec_delete(ThreadId tid, void* p) |
| 257 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 258 | drd_handle_free(tid, (Addr)p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void drd_register_malloc_wrappers(const StartUsingMem start_using_mem_callback, |
| 262 | const StopUsingMem stop_using_mem_callback) |
| 263 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 264 | tl_assert(drd_malloc_list == 0); |
| 265 | drd_malloc_list = VG_(HT_construct)("drd_malloc_list"); // a big prime |
| 266 | tl_assert(drd_malloc_list != 0); |
| 267 | tl_assert(stop_using_mem_callback); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 268 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 269 | s_start_using_mem_callback = start_using_mem_callback; |
| 270 | s_stop_using_mem_callback = stop_using_mem_callback; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 271 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 272 | VG_(needs_malloc_replacement)(drd_malloc, |
| 273 | drd___builtin_new, |
| 274 | drd___builtin_vec_new, |
| 275 | drd_memalign, |
| 276 | drd_calloc, |
| 277 | drd_free, |
| 278 | drd___builtin_delete, |
| 279 | drd___builtin_vec_delete, |
| 280 | drd_realloc, |
| 281 | 0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | Bool drd_heap_addrinfo(Addr const a, |
| 285 | Addr* const data, |
| 286 | SizeT* const size, |
| 287 | ExeContext** const where) |
| 288 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 289 | DRD_Chunk* mc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 290 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 291 | tl_assert(data); |
| 292 | tl_assert(size); |
| 293 | tl_assert(where); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 294 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 295 | VG_(HT_ResetIter)(drd_malloc_list); |
| 296 | while ((mc = VG_(HT_Next)(drd_malloc_list))) |
| 297 | { |
| 298 | if (mc->data <= a && a < mc->data + mc->size) |
| 299 | { |
| 300 | *data = mc->data; |
| 301 | *size = mc->size; |
| 302 | *where = mc->where; |
| 303 | return True; |
| 304 | } |
| 305 | } |
| 306 | return False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /*------------------------------------------------------------*/ |
| 310 | /*--- Statistics printing ---*/ |
| 311 | /*------------------------------------------------------------*/ |
| 312 | |
| 313 | void drd_print_malloc_stats(void) |
| 314 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 315 | DRD_Chunk* mc; |
| 316 | SizeT nblocks = 0; |
| 317 | SizeT nbytes = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 318 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 319 | if (VG_(clo_verbosity) == 0) |
| 320 | return; |
| 321 | if (VG_(clo_xml)) |
| 322 | return; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 323 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 324 | /* Count memory still in use. */ |
| 325 | VG_(HT_ResetIter)(drd_malloc_list); |
| 326 | while ((mc = VG_(HT_Next)(drd_malloc_list))) |
| 327 | { |
| 328 | nblocks++; |
| 329 | nbytes += mc->size; |
| 330 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 331 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 332 | VG_(message)(Vg_DebugMsg, |
| 333 | "malloc/free: in use at exit: %lu bytes in %lu blocks.", |
| 334 | nbytes, nblocks); |
| 335 | VG_(message)(Vg_DebugMsg, |
| 336 | "malloc/free: %lu allocs, %lu frees, %lu bytes allocated.", |
| 337 | cmalloc_n_mallocs, |
| 338 | cmalloc_n_frees, cmalloc_bs_mallocd); |
| 339 | if (VG_(clo_verbosity) > 1) |
| 340 | VG_(message)(Vg_DebugMsg, " "); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /*--------------------------------------------------------------------*/ |
| 344 | /*--- end ---*/ |
| 345 | /*--------------------------------------------------------------------*/ |