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 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 73 | DRD_Chunk* mc = VG_(malloc)(sizeof(DRD_Chunk)); |
| 74 | mc->data = p; |
| 75 | mc->size = size; |
| 76 | mc->where = VG_(record_ExeContext)(tid, 0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 77 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 78 | return mc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /*------------------------------------------------------------*/ |
| 82 | /*--- client_malloc(), etc ---*/ |
| 83 | /*------------------------------------------------------------*/ |
| 84 | |
| 85 | /* Allocate memory and note change in memory available */ |
| 86 | static |
| 87 | __inline__ |
| 88 | void* drd_new_block(ThreadId tid, |
| 89 | SizeT size, SizeT align, |
| 90 | Bool is_zeroed) |
| 91 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 92 | Addr p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 93 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 94 | cmalloc_n_mallocs ++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 95 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 96 | // 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); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 102 | s_start_using_mem_callback(p, p + size, 0/*ec_uniq*/); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 103 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 104 | // Only update this stat if allocation succeeded. |
| 105 | cmalloc_bs_mallocd += size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 106 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 107 | VG_(HT_add_node)(drd_malloc_list, create_DRD_Chunk(tid, p, size)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 108 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 109 | return (void*)p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static |
| 113 | void* drd_malloc(ThreadId tid, SizeT n) |
| 114 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 115 | return drd_new_block(tid, n, VG_(clo_alignment), /*is_zeroed*/False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static |
| 119 | void* drd_memalign(ThreadId tid, SizeT align, SizeT n) |
| 120 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 121 | return drd_new_block(tid, n, align, /*is_zeroed*/False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static |
| 125 | void* drd_calloc(ThreadId tid, SizeT nmemb, SizeT size1) |
| 126 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 127 | return drd_new_block(tid, nmemb*size1, VG_(clo_alignment), |
| 128 | /*is_zeroed*/True); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static |
| 132 | __inline__ |
| 133 | void drd_handle_free(ThreadId tid, Addr p) |
| 134 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 135 | DRD_Chunk* mc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 136 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 137 | cmalloc_n_frees++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 138 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 139 | mc = VG_(HT_remove)(drd_malloc_list, (UWord)p); |
| 140 | if (mc == NULL) |
| 141 | { |
| 142 | tl_assert(0); |
| 143 | } |
| 144 | else |
| 145 | { |
bart | e8a274c | 2008-04-20 08:33:10 +0000 | [diff] [blame] | 146 | tl_assert(p == mc->data); |
bart | 84595c0 | 2008-03-22 17:35:28 +0000 | [diff] [blame] | 147 | if (mc->size > 0) |
| 148 | s_stop_using_mem_callback(mc->data, mc->size); |
bart | e8a274c | 2008-04-20 08:33:10 +0000 | [diff] [blame] | 149 | VG_(cli_free)((void*)p); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 150 | VG_(free)(mc); |
| 151 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static |
| 155 | void drd_free(ThreadId tid, void* p) |
| 156 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 157 | drd_handle_free(tid, (Addr)p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static |
| 161 | void* drd_realloc(ThreadId tid, void* p_old, SizeT new_size) |
| 162 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 163 | DRD_Chunk* mc; |
| 164 | void* p_new; |
| 165 | SizeT old_size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 166 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 167 | cmalloc_n_frees ++; |
| 168 | cmalloc_n_mallocs ++; |
| 169 | cmalloc_bs_mallocd += new_size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 170 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 171 | /* 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 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 177 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 178 | old_size = mc->size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 179 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 180 | if (old_size == new_size) |
| 181 | { |
| 182 | /* size unchanged */ |
| 183 | mc->where = VG_(record_ExeContext)(tid, 0); |
| 184 | p_new = p_old; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 185 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 186 | } |
| 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; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 194 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 195 | } |
| 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); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 201 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 202 | if (a_new) |
| 203 | { |
| 204 | /* Copy from old to new */ |
| 205 | VG_(memcpy)((void*)a_new, p_old, mc->size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 206 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 207 | /* Free old memory */ |
| 208 | s_stop_using_mem_callback(mc->data, mc->size); |
| 209 | VG_(free)(mc); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 210 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 211 | // Allocate a new chunk. |
| 212 | mc = create_DRD_Chunk(tid, a_new, new_size); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 213 | 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] | 214 | } |
| 215 | else |
| 216 | { |
| 217 | /* Allocation failed -- leave original block untouched. */ |
| 218 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 219 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 220 | p_new = (void*)a_new; |
| 221 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 222 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 223 | // 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); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 229 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 230 | return p_new; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static |
| 234 | void* drd___builtin_new(ThreadId tid, SizeT n) |
| 235 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 236 | 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; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static |
| 242 | void drd___builtin_delete(ThreadId tid, void* p) |
| 243 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 244 | //VG_(message)(Vg_DebugMsg, "__builtin_delete(%d, %p)", tid, p); |
| 245 | drd_handle_free(tid, (Addr)p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static |
| 249 | void* drd___builtin_vec_new(ThreadId tid, SizeT n) |
| 250 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 251 | return drd_new_block(tid, n, VG_(clo_alignment), /*is_zeroed*/False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static |
| 255 | void drd___builtin_vec_delete(ThreadId tid, void* p) |
| 256 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 257 | drd_handle_free(tid, (Addr)p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void drd_register_malloc_wrappers(const StartUsingMem start_using_mem_callback, |
| 261 | const StopUsingMem stop_using_mem_callback) |
| 262 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 263 | 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); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 267 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 268 | s_start_using_mem_callback = start_using_mem_callback; |
| 269 | s_stop_using_mem_callback = stop_using_mem_callback; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 270 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 271 | 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); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | Bool drd_heap_addrinfo(Addr const a, |
| 284 | Addr* const data, |
| 285 | SizeT* const size, |
| 286 | ExeContext** const where) |
| 287 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 288 | DRD_Chunk* mc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 289 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 290 | tl_assert(data); |
| 291 | tl_assert(size); |
| 292 | tl_assert(where); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 293 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 294 | 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; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /*------------------------------------------------------------*/ |
| 309 | /*--- Statistics printing ---*/ |
| 310 | /*------------------------------------------------------------*/ |
| 311 | |
| 312 | void drd_print_malloc_stats(void) |
| 313 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 314 | DRD_Chunk* mc; |
| 315 | SizeT nblocks = 0; |
| 316 | SizeT nbytes = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 317 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 318 | if (VG_(clo_verbosity) == 0) |
| 319 | return; |
| 320 | if (VG_(clo_xml)) |
| 321 | return; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 322 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 323 | /* 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 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 330 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 331 | 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, " "); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /*--------------------------------------------------------------------*/ |
| 343 | /*--- end ---*/ |
| 344 | /*--------------------------------------------------------------------*/ |