blob: e0dbbf4e90fe80c90d159f1b10974e1ddf3cf8f7 [file] [log] [blame]
njn5c004e42002-11-18 11:04:50 +00001
2/*--------------------------------------------------------------------*/
3/*--- Code that is shared between MemCheck and AddrCheck. ---*/
njn43c799e2003-04-08 00:08:52 +00004/*--- mac_needs.c ---*/
njn5c004e42002-11-18 11:04:50 +00005/*--------------------------------------------------------------------*/
6
7/*
8 This file is part of MemCheck, a heavyweight Valgrind skin for
9 detecting memory errors, and AddrCheck, a lightweight Valgrind skin
10 for detecting memory errors.
11
njn0e1b5142003-04-15 14:58:06 +000012 Copyright (C) 2000-2003 Julian Seward
njn5c004e42002-11-18 11:04:50 +000013 jseward@acm.org
14
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation; either version 2 of the
18 License, or (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 02111-1307, USA.
29
30 The GNU General Public License is contained in the file COPYING.
31*/
32
33
njn43c799e2003-04-08 00:08:52 +000034#include "mac_shared.h"
njn5c004e42002-11-18 11:04:50 +000035
njn47363ab2003-04-21 13:24:40 +000036#include "memcheck.h" /* for VG_USERREQ__* */
37
njn5c004e42002-11-18 11:04:50 +000038/*------------------------------------------------------------*/
39/*--- Defns ---*/
40/*------------------------------------------------------------*/
41
42/* These many bytes below %ESP are considered addressible if we're
43 doing the --workaround-gcc296-bugs hack. */
44#define VG_GCC296_BUG_STACK_SLOP 1024
45
46/*------------------------------------------------------------*/
47/*--- Command line options ---*/
48/*------------------------------------------------------------*/
49
njn43c799e2003-04-08 00:08:52 +000050Bool MAC_(clo_partial_loads_ok) = True;
51Int MAC_(clo_freelist_vol) = 1000000;
52Bool MAC_(clo_leak_check) = False;
53VgRes MAC_(clo_leak_resolution) = Vg_LowRes;
54Bool MAC_(clo_show_reachable) = False;
55Bool MAC_(clo_workaround_gcc296_bugs) = False;
njn5c004e42002-11-18 11:04:50 +000056
njn43c799e2003-04-08 00:08:52 +000057Bool MAC_(process_common_cmd_line_option)(Char* arg)
njn5c004e42002-11-18 11:04:50 +000058{
njn43c799e2003-04-08 00:08:52 +000059 if (VG_CLO_STREQ(arg, "--partial-loads-ok=yes"))
60 MAC_(clo_partial_loads_ok) = True;
61 else if (VG_CLO_STREQ(arg, "--partial-loads-ok=no"))
62 MAC_(clo_partial_loads_ok) = False;
njn5c004e42002-11-18 11:04:50 +000063
njn43c799e2003-04-08 00:08:52 +000064 else if (VG_CLO_STREQN(15, arg, "--freelist-vol=")) {
65 MAC_(clo_freelist_vol) = (Int)VG_(atoll)(&arg[15]);
66 if (MAC_(clo_freelist_vol) < 0) MAC_(clo_freelist_vol) = 0;
njn5c004e42002-11-18 11:04:50 +000067 }
68
njn43c799e2003-04-08 00:08:52 +000069 else if (VG_CLO_STREQ(arg, "--leak-check=yes"))
70 MAC_(clo_leak_check) = True;
71 else if (VG_CLO_STREQ(arg, "--leak-check=no"))
72 MAC_(clo_leak_check) = False;
njn5c004e42002-11-18 11:04:50 +000073
njn43c799e2003-04-08 00:08:52 +000074 else if (VG_CLO_STREQ(arg, "--leak-resolution=low"))
75 MAC_(clo_leak_resolution) = Vg_LowRes;
76 else if (VG_CLO_STREQ(arg, "--leak-resolution=med"))
77 MAC_(clo_leak_resolution) = Vg_MedRes;
78 else if (VG_CLO_STREQ(arg, "--leak-resolution=high"))
79 MAC_(clo_leak_resolution) = Vg_HighRes;
njn5c004e42002-11-18 11:04:50 +000080
njn43c799e2003-04-08 00:08:52 +000081 else if (VG_CLO_STREQ(arg, "--show-reachable=yes"))
82 MAC_(clo_show_reachable) = True;
83 else if (VG_CLO_STREQ(arg, "--show-reachable=no"))
84 MAC_(clo_show_reachable) = False;
njn5c004e42002-11-18 11:04:50 +000085
njn43c799e2003-04-08 00:08:52 +000086 else if (VG_CLO_STREQ(arg, "--workaround-gcc296-bugs=yes"))
87 MAC_(clo_workaround_gcc296_bugs) = True;
88 else if (VG_CLO_STREQ(arg, "--workaround-gcc296-bugs=no"))
89 MAC_(clo_workaround_gcc296_bugs) = False;
njn5c004e42002-11-18 11:04:50 +000090
91 else
njn3e884182003-04-15 13:03:23 +000092 return VG_(replacement_malloc_process_cmd_line_option)(arg);
njn5c004e42002-11-18 11:04:50 +000093
94 return True;
njn43c799e2003-04-08 00:08:52 +000095}
njn5c004e42002-11-18 11:04:50 +000096
njn3e884182003-04-15 13:03:23 +000097void MAC_(print_common_usage)(void)
njn43c799e2003-04-08 00:08:52 +000098{
njn3e884182003-04-15 13:03:23 +000099 VG_(printf)(
100" --partial-loads-ok=no|yes too hard to explain here; see manual [yes]\n"
101" --freelist-vol=<number> volume of freed blocks queue [1000000]\n"
102" --leak-check=no|yes search for memory leaks at exit? [no]\n"
103" --leak-resolution=low|med|high how much bt merging in leak check [low]\n"
104" --show-reachable=no|yes show reachable blocks in leak check? [no]\n"
105" --workaround-gcc296-bugs=no|yes self explanatory [no]\n"
106 );
107 VG_(replacement_malloc_print_usage)();
njn43c799e2003-04-08 00:08:52 +0000108}
109
njn3e884182003-04-15 13:03:23 +0000110void MAC_(print_common_debug_usage)(void)
njn43c799e2003-04-08 00:08:52 +0000111{
njn3e884182003-04-15 13:03:23 +0000112 VG_(replacement_malloc_print_debug_usage)();
njn5c004e42002-11-18 11:04:50 +0000113}
114
115/*------------------------------------------------------------*/
116/*--- Comparing and printing errors ---*/
117/*------------------------------------------------------------*/
118
119static __inline__
120void clear_AddrInfo ( AddrInfo* ai )
121{
122 ai->akind = Unknown;
123 ai->blksize = 0;
124 ai->rwoffset = 0;
125 ai->lastchange = NULL;
126 ai->stack_tid = VG_INVALID_THREADID;
127 ai->maybe_gcc = False;
128}
129
njn43c799e2003-04-08 00:08:52 +0000130void MAC_(clear_MAC_Error) ( MAC_Error* err_extra )
njn5c004e42002-11-18 11:04:50 +0000131{
132 err_extra->axskind = ReadAxs;
133 err_extra->size = 0;
134 clear_AddrInfo ( &err_extra->addrinfo );
135 err_extra->isWrite = False;
136}
137
138__attribute__ ((unused))
139static Bool eq_AddrInfo ( VgRes res, AddrInfo* ai1, AddrInfo* ai2 )
140{
141 if (ai1->akind != Undescribed
142 && ai2->akind != Undescribed
143 && ai1->akind != ai2->akind)
144 return False;
145 if (ai1->akind == Freed || ai1->akind == Mallocd) {
146 if (ai1->blksize != ai2->blksize)
147 return False;
148 if (!VG_(eq_ExeContext)(res, ai1->lastchange, ai2->lastchange))
149 return False;
150 }
151 return True;
152}
153
154/* Compare error contexts, to detect duplicates. Note that if they
155 are otherwise the same, the faulting addrs and associated rwoffsets
156 are allowed to be different. */
157
158Bool SK_(eq_SkinError) ( VgRes res, Error* e1, Error* e2 )
159{
njn43c799e2003-04-08 00:08:52 +0000160 MAC_Error* e1_extra = VG_(get_error_extra)(e1);
161 MAC_Error* e2_extra = VG_(get_error_extra)(e2);
njn7cc53a82002-11-19 16:19:32 +0000162
163 /* Guaranteed by calling function */
164 sk_assert(VG_(get_error_kind)(e1) == VG_(get_error_kind)(e2));
njn5c004e42002-11-18 11:04:50 +0000165
166 switch (VG_(get_error_kind)(e1)) {
167 case CoreMemErr: {
168 Char *e1s, *e2s;
169 if (e1_extra->isWrite != e2_extra->isWrite) return False;
njn5c004e42002-11-18 11:04:50 +0000170 e1s = VG_(get_error_string)(e1);
171 e2s = VG_(get_error_string)(e2);
172 if (e1s == e2s) return True;
173 if (0 == VG_(strcmp)(e1s, e2s)) return True;
174 return False;
175 }
176
177 case UserErr:
178 case ParamErr:
179 if (e1_extra->isWrite != e2_extra->isWrite) return False;
180 if (VG_(get_error_kind)(e1) == ParamErr
181 && 0 != VG_(strcmp)(VG_(get_error_string)(e1),
182 VG_(get_error_string)(e2))) return False;
183 return True;
184
185 case FreeErr:
186 case FreeMismatchErr:
187 /* JRS 2002-Aug-26: comparing addrs seems overkill and can
188 cause excessive duplication of errors. Not even AddrErr
189 below does that. So don't compare either the .addr field
190 or the .addrinfo fields. */
191 /* if (e1->addr != e2->addr) return False; */
192 /* if (!eq_AddrInfo(res, &e1_extra->addrinfo, &e2_extra->addrinfo))
193 return False;
194 */
195 return True;
196
197 case AddrErr:
198 /* if (e1_extra->axskind != e2_extra->axskind) return False; */
199 if (e1_extra->size != e2_extra->size) return False;
200 /*
201 if (!eq_AddrInfo(res, &e1_extra->addrinfo, &e2_extra->addrinfo))
202 return False;
203 */
204 return True;
205
206 case ValueErr:
207 if (e1_extra->size != e2_extra->size) return False;
208 return True;
209
njn34419c12003-05-02 17:24:29 +0000210 case OverlapErr:
211 return True;
212
njn43c799e2003-04-08 00:08:52 +0000213 case LeakErr:
214 VG_(skin_panic)("Shouldn't get LeakErr in SK_(eq_SkinError),\n"
215 "since it's handled with VG_(unique_error)()!");
216
njn5c004e42002-11-18 11:04:50 +0000217 default:
218 VG_(printf)("Error:\n unknown error code %d\n",
219 VG_(get_error_kind)(e1));
220 VG_(skin_panic)("unknown error code in SK_(eq_SkinError)");
221 }
222}
223
njn43c799e2003-04-08 00:08:52 +0000224void MAC_(pp_AddrInfo) ( Addr a, AddrInfo* ai )
njn5c004e42002-11-18 11:04:50 +0000225{
226 switch (ai->akind) {
227 case Stack:
228 VG_(message)(Vg_UserMsg,
229 " Address 0x%x is on thread %d's stack",
230 a, ai->stack_tid);
231 break;
232 case Unknown:
233 if (ai->maybe_gcc) {
234 VG_(message)(Vg_UserMsg,
235 " Address 0x%x is just below %%esp. Possibly a bug in GCC/G++",
236 a);
237 VG_(message)(Vg_UserMsg,
238 " v 2.96 or 3.0.X. To suppress, use: --workaround-gcc296-bugs=yes");
239 } else {
240 VG_(message)(Vg_UserMsg,
241 " Address 0x%x is not stack'd, malloc'd or free'd", a);
242 }
243 break;
sewardja81709d2002-12-28 12:55:48 +0000244 case Freed: case Mallocd: case UserG: {
njn5c004e42002-11-18 11:04:50 +0000245 UInt delta;
246 UChar* relative;
247 if (ai->rwoffset < 0) {
248 delta = (UInt)(- ai->rwoffset);
249 relative = "before";
250 } else if (ai->rwoffset >= ai->blksize) {
251 delta = ai->rwoffset - ai->blksize;
252 relative = "after";
253 } else {
254 delta = ai->rwoffset;
255 relative = "inside";
256 }
sewardja81709d2002-12-28 12:55:48 +0000257 VG_(message)(Vg_UserMsg,
258 " Address 0x%x is %d bytes %s a block of size %d %s",
259 a, delta, relative,
260 ai->blksize,
261 ai->akind==Mallocd ? "alloc'd"
262 : ai->akind==Freed ? "free'd"
263 : "client-defined");
njn5c004e42002-11-18 11:04:50 +0000264 VG_(pp_ExeContext)(ai->lastchange);
265 break;
266 }
267 default:
njn43c799e2003-04-08 00:08:52 +0000268 VG_(skin_panic)("MAC_(pp_AddrInfo)");
269 }
270}
271
272/* This prints out the message for the error types where Memcheck and
273 Addrcheck have identical messages */
274void MAC_(pp_shared_SkinError) ( Error* err )
275{
276 MAC_Error* err_extra = VG_(get_error_extra)(err);
277
278 switch (VG_(get_error_kind)(err)) {
279 case FreeErr:
njn10785452003-05-20 16:38:24 +0000280 VG_(message)(Vg_UserMsg, "Invalid free() / delete / delete[]");
njn43c799e2003-04-08 00:08:52 +0000281 /* fall through */
282 case FreeMismatchErr:
283 if (VG_(get_error_kind)(err) == FreeMismatchErr)
284 VG_(message)(Vg_UserMsg,
285 "Mismatched free() / delete / delete []");
286 VG_(pp_ExeContext)( VG_(get_error_where)(err) );
287 MAC_(pp_AddrInfo)(VG_(get_error_address)(err), &err_extra->addrinfo);
288 break;
289
njn89409f82003-09-26 14:55:31 +0000290 case AddrErr:
291 switch (err_extra->axskind) {
292 case ReadAxs:
293 VG_(message)(Vg_UserMsg, "Invalid read of size %d",
294 err_extra->size );
295 break;
296 case WriteAxs:
297 VG_(message)(Vg_UserMsg, "Invalid write of size %d",
298 err_extra->size );
299 break;
300 case ExecAxs:
301 VG_(message)(Vg_UserMsg, "Jump to the invalid address "
302 "stated on the next line");
303 break;
304 default:
305 VG_(skin_panic)("SK_(pp_SkinError)(axskind)");
306 }
307 VG_(pp_ExeContext)( VG_(get_error_where)(err) );
308 MAC_(pp_AddrInfo)(VG_(get_error_address)(err), &err_extra->addrinfo);
309 break;
310
njnb6cae9f2003-09-04 20:50:47 +0000311 case OverlapErr: {
312 OverlapExtra* ov_extra = (OverlapExtra*)VG_(get_error_extra)(err);
313 if (ov_extra->len == -1)
314 VG_(message)(Vg_UserMsg,
315 "Source and destination overlap in %s(%p, %p)",
316 VG_(get_error_string)(err),
317 ov_extra->dst, ov_extra->src);
318 else
319 VG_(message)(Vg_UserMsg,
320 "Source and destination overlap in %s(%p, %p, %d)",
321 VG_(get_error_string)(err),
322 ov_extra->dst, ov_extra->src, ov_extra->len);
njn66fe05a2003-07-22 09:12:33 +0000323 VG_(pp_ExeContext)( VG_(get_error_where)(err) );
324 break;
njnb6cae9f2003-09-04 20:50:47 +0000325 }
njn43c799e2003-04-08 00:08:52 +0000326 case LeakErr: {
327 /* Totally abusing the types of these spare fields... oh well. */
328 UInt n_this_record = (UInt)VG_(get_error_address)(err);
329 UInt n_total_records = (UInt)VG_(get_error_string) (err);
330
331 MAC_(pp_LeakError)(err_extra, n_this_record, n_total_records);
332 break;
333 }
334
335 default:
336 VG_(printf)("Error:\n unknown Memcheck/Addrcheck error code %d\n",
337 VG_(get_error_kind)(err));
338 VG_(skin_panic)("unknown error code in MAC_(pp_shared_SkinError)");
njn5c004e42002-11-18 11:04:50 +0000339 }
340}
341
342/*------------------------------------------------------------*/
343/*--- Recording errors ---*/
344/*------------------------------------------------------------*/
345
njn43c799e2003-04-08 00:08:52 +0000346/* Additional description function for describe_addr(); used by
347 MemCheck for user blocks, which Addrcheck doesn't support. */
348Bool (*MAC_(describe_addr_supp)) ( Addr a, AddrInfo* ai ) = NULL;
349
njn43c799e2003-04-08 00:08:52 +0000350/* Describe an address as best you can, for error messages,
351 putting the result in ai. */
352static void describe_addr ( Addr a, AddrInfo* ai )
353{
njn3e884182003-04-15 13:03:23 +0000354 MAC_Chunk* sc;
355 ThreadId tid;
njn43c799e2003-04-08 00:08:52 +0000356
357 /* Nested functions, yeah. Need the lexical scoping of 'a'. */
njn3e884182003-04-15 13:03:23 +0000358
njn43c799e2003-04-08 00:08:52 +0000359 /* Closure for searching thread stacks */
360 Bool addr_is_in_bounds(Addr stack_min, Addr stack_max)
361 {
362 return (stack_min <= a && a <= stack_max);
363 }
njn3e884182003-04-15 13:03:23 +0000364 /* Closure for searching free'd list */
365 Bool addr_is_in_MAC_Chunk(MAC_Chunk* mc)
njn43c799e2003-04-08 00:08:52 +0000366 {
njn3e884182003-04-15 13:03:23 +0000367 return VG_(addr_is_in_block)( a, mc->data, mc->size );
368 }
369 /* Closure for searching malloc'd lists */
370 Bool addr_is_in_HashNode(VgHashNode* sh_ch)
371 {
372 return addr_is_in_MAC_Chunk( (MAC_Chunk*)sh_ch );
njn43c799e2003-04-08 00:08:52 +0000373 }
374
375 /* Perhaps it's a user-def'd block ? (only check if requested, though) */
376 if (NULL != MAC_(describe_addr_supp)) {
377 if (MAC_(describe_addr_supp)( a, ai ))
378 return;
379 }
380 /* Perhaps it's on a thread's stack? */
381 tid = VG_(first_matching_thread_stack)(addr_is_in_bounds);
382 if (tid != VG_INVALID_THREADID) {
383 ai->akind = Stack;
384 ai->stack_tid = tid;
385 return;
386 }
387 /* Search for a recently freed block which might bracket it. */
njn3e884182003-04-15 13:03:23 +0000388 sc = MAC_(first_matching_freed_MAC_Chunk)(addr_is_in_MAC_Chunk);
389 if (NULL != sc) {
njn43c799e2003-04-08 00:08:52 +0000390 ai->akind = Freed;
njn3e884182003-04-15 13:03:23 +0000391 ai->blksize = sc->size;
392 ai->rwoffset = (Int)a - (Int)sc->data;
393 ai->lastchange = sc->where;
njn43c799e2003-04-08 00:08:52 +0000394 return;
395 }
396 /* Search for a currently malloc'd block which might bracket it. */
njn3e884182003-04-15 13:03:23 +0000397 sc = (MAC_Chunk*)VG_(HT_first_match)(MAC_(malloc_list), addr_is_in_HashNode);
njn43c799e2003-04-08 00:08:52 +0000398 if (NULL != sc) {
399 ai->akind = Mallocd;
njn3e884182003-04-15 13:03:23 +0000400 ai->blksize = sc->size;
401 ai->rwoffset = (Int)(a) - (Int)sc->data;
402 ai->lastchange = sc->where;
njn43c799e2003-04-08 00:08:52 +0000403 return;
404 }
405 /* Clueless ... */
406 ai->akind = Unknown;
407 return;
408}
409
njn5c004e42002-11-18 11:04:50 +0000410/* Is this address within some small distance below %ESP? Used only
411 for the --workaround-gcc296-bugs kludge. */
412static Bool is_just_below_ESP( Addr esp, Addr aa )
413{
414 if ((UInt)esp > (UInt)aa
415 && ((UInt)esp - (UInt)aa) <= VG_GCC296_BUG_STACK_SLOP)
416 return True;
417 else
418 return False;
419}
420
sewardjaf48a602003-07-06 00:54:47 +0000421/* This one called from generated code and non-generated code. */
njn5c004e42002-11-18 11:04:50 +0000422
njn72718642003-07-24 08:45:32 +0000423void MAC_(record_address_error) ( ThreadId tid, Addr a, Int size,
sewardjaf48a602003-07-06 00:54:47 +0000424 Bool isWrite )
njn5c004e42002-11-18 11:04:50 +0000425{
njn43c799e2003-04-08 00:08:52 +0000426 MAC_Error err_extra;
427 Bool just_below_esp;
njn5c004e42002-11-18 11:04:50 +0000428
429 just_below_esp = is_just_below_ESP( VG_(get_stack_pointer)(), a );
430
431 /* If this is caused by an access immediately below %ESP, and the
432 user asks nicely, we just ignore it. */
njn43c799e2003-04-08 00:08:52 +0000433 if (MAC_(clo_workaround_gcc296_bugs) && just_below_esp)
njn5c004e42002-11-18 11:04:50 +0000434 return;
435
njn43c799e2003-04-08 00:08:52 +0000436 MAC_(clear_MAC_Error)( &err_extra );
njn5c004e42002-11-18 11:04:50 +0000437 err_extra.axskind = isWrite ? WriteAxs : ReadAxs;
438 err_extra.size = size;
439 err_extra.addrinfo.akind = Undescribed;
440 err_extra.addrinfo.maybe_gcc = just_below_esp;
njn72718642003-07-24 08:45:32 +0000441 VG_(maybe_record_error)( tid, AddrErr, a, /*s*/NULL, &err_extra );
njn5c004e42002-11-18 11:04:50 +0000442}
443
444/* These ones are called from non-generated code */
445
446/* This is for memory errors in pthread functions, as opposed to pthread API
447 errors which are found by the core. */
njn72718642003-07-24 08:45:32 +0000448void MAC_(record_core_mem_error) ( ThreadId tid, Bool isWrite, Char* msg )
njn5c004e42002-11-18 11:04:50 +0000449{
njn43c799e2003-04-08 00:08:52 +0000450 MAC_Error err_extra;
njn5c004e42002-11-18 11:04:50 +0000451
njn43c799e2003-04-08 00:08:52 +0000452 MAC_(clear_MAC_Error)( &err_extra );
njn5c004e42002-11-18 11:04:50 +0000453 err_extra.isWrite = isWrite;
njn72718642003-07-24 08:45:32 +0000454 VG_(maybe_record_error)( tid, CoreMemErr, /*addr*/0, msg, &err_extra );
njn5c004e42002-11-18 11:04:50 +0000455}
456
njn72718642003-07-24 08:45:32 +0000457void MAC_(record_param_error) ( ThreadId tid, Addr a, Bool isWrite,
njn5c004e42002-11-18 11:04:50 +0000458 Char* msg )
459{
njn43c799e2003-04-08 00:08:52 +0000460 MAC_Error err_extra;
njn5c004e42002-11-18 11:04:50 +0000461
njn72718642003-07-24 08:45:32 +0000462 sk_assert(VG_INVALID_THREADID != tid);
njn43c799e2003-04-08 00:08:52 +0000463 MAC_(clear_MAC_Error)( &err_extra );
njn5c004e42002-11-18 11:04:50 +0000464 err_extra.addrinfo.akind = Undescribed;
465 err_extra.isWrite = isWrite;
njn72718642003-07-24 08:45:32 +0000466 VG_(maybe_record_error)( tid, ParamErr, a, msg, &err_extra );
njn5c004e42002-11-18 11:04:50 +0000467}
468
njn72718642003-07-24 08:45:32 +0000469void MAC_(record_jump_error) ( ThreadId tid, Addr a )
njn5c004e42002-11-18 11:04:50 +0000470{
njn43c799e2003-04-08 00:08:52 +0000471 MAC_Error err_extra;
njn5c004e42002-11-18 11:04:50 +0000472
njn72718642003-07-24 08:45:32 +0000473 sk_assert(VG_INVALID_THREADID != tid);
njn43c799e2003-04-08 00:08:52 +0000474 MAC_(clear_MAC_Error)( &err_extra );
njn5c004e42002-11-18 11:04:50 +0000475 err_extra.axskind = ExecAxs;
476 err_extra.addrinfo.akind = Undescribed;
njn72718642003-07-24 08:45:32 +0000477 VG_(maybe_record_error)( tid, AddrErr, a, /*s*/NULL, &err_extra );
njn5c004e42002-11-18 11:04:50 +0000478}
479
njn72718642003-07-24 08:45:32 +0000480void MAC_(record_free_error) ( ThreadId tid, Addr a )
njn5c004e42002-11-18 11:04:50 +0000481{
njn43c799e2003-04-08 00:08:52 +0000482 MAC_Error err_extra;
njn5c004e42002-11-18 11:04:50 +0000483
njn72718642003-07-24 08:45:32 +0000484 sk_assert(VG_INVALID_THREADID != tid);
njn43c799e2003-04-08 00:08:52 +0000485 MAC_(clear_MAC_Error)( &err_extra );
njn5c004e42002-11-18 11:04:50 +0000486 err_extra.addrinfo.akind = Undescribed;
njn72718642003-07-24 08:45:32 +0000487 VG_(maybe_record_error)( tid, FreeErr, a, /*s*/NULL, &err_extra );
njn5c004e42002-11-18 11:04:50 +0000488}
489
njn72718642003-07-24 08:45:32 +0000490void MAC_(record_freemismatch_error) ( ThreadId tid, Addr a )
njn5c004e42002-11-18 11:04:50 +0000491{
njn43c799e2003-04-08 00:08:52 +0000492 MAC_Error err_extra;
njn5c004e42002-11-18 11:04:50 +0000493
njn72718642003-07-24 08:45:32 +0000494 sk_assert(VG_INVALID_THREADID != tid);
njn43c799e2003-04-08 00:08:52 +0000495 MAC_(clear_MAC_Error)( &err_extra );
njn5c004e42002-11-18 11:04:50 +0000496 err_extra.addrinfo.akind = Undescribed;
njn72718642003-07-24 08:45:32 +0000497 VG_(maybe_record_error)( tid, FreeMismatchErr, a, /*s*/NULL, &err_extra );
njn5c004e42002-11-18 11:04:50 +0000498}
499
njn66fe05a2003-07-22 09:12:33 +0000500
njn72718642003-07-24 08:45:32 +0000501// This one not passed a ThreadId, so it grabs it itself.
njnb6cae9f2003-09-04 20:50:47 +0000502void MAC_(record_overlap_error) ( Char* function, OverlapExtra* ov_extra )
njn66fe05a2003-07-22 09:12:33 +0000503{
njn72718642003-07-24 08:45:32 +0000504 VG_(maybe_record_error)( VG_(get_current_or_recent_tid)(),
njnb6cae9f2003-09-04 20:50:47 +0000505 OverlapErr, /*addr*/0, /*s*/function, ov_extra );
njn66fe05a2003-07-22 09:12:33 +0000506}
507
508
njnb6cae9f2003-09-04 20:50:47 +0000509/* Updates the copy with address info if necessary (but not for all errors). */
njn43c799e2003-04-08 00:08:52 +0000510UInt SK_(update_extra)( Error* err )
511{
njnb6cae9f2003-09-04 20:50:47 +0000512 switch (VG_(get_error_kind)(err)) {
513 case ValueErr:
514 case CoreMemErr:
515 case AddrErr:
516 case ParamErr:
517 case UserErr:
518 case FreeErr:
519 case FreeMismatchErr: {
520 MAC_Error* extra = (MAC_Error*)VG_(get_error_extra)(err);
521 if (extra != NULL && Undescribed == extra->addrinfo.akind) {
522 describe_addr ( VG_(get_error_address)(err), &(extra->addrinfo) );
523 }
524 return sizeof(MAC_Error);
525 }
njn43c799e2003-04-08 00:08:52 +0000526 /* Don't need to return the correct size -- LeakErrs are always shown with
527 VG_(unique_error)() so they're not copied anyway. */
njnb6cae9f2003-09-04 20:50:47 +0000528 case LeakErr: return 0;
529 case OverlapErr: return sizeof(OverlapExtra);
530 default: VG_(skin_panic)("update_extra: bad errkind");
njn43c799e2003-04-08 00:08:52 +0000531 }
njn43c799e2003-04-08 00:08:52 +0000532}
533
534
njn5c004e42002-11-18 11:04:50 +0000535/*------------------------------------------------------------*/
536/*--- Suppressions ---*/
537/*------------------------------------------------------------*/
538
njn43c799e2003-04-08 00:08:52 +0000539Bool MAC_(shared_recognised_suppression) ( Char* name, Supp* su )
540{
541 SuppKind skind;
542
543 if (VG_STREQ(name, "Param")) skind = ParamSupp;
544 else if (VG_STREQ(name, "CoreMem")) skind = CoreMemSupp;
545 else if (VG_STREQ(name, "Addr1")) skind = Addr1Supp;
546 else if (VG_STREQ(name, "Addr2")) skind = Addr2Supp;
547 else if (VG_STREQ(name, "Addr4")) skind = Addr4Supp;
548 else if (VG_STREQ(name, "Addr8")) skind = Addr8Supp;
njnc0616662003-06-12 09:58:41 +0000549 else if (VG_STREQ(name, "Addr16")) skind = Addr16Supp;
njn43c799e2003-04-08 00:08:52 +0000550 else if (VG_STREQ(name, "Free")) skind = FreeSupp;
551 else if (VG_STREQ(name, "Leak")) skind = LeakSupp;
njne9cab142003-09-19 08:10:07 +0000552 else if (VG_STREQ(name, "Overlap")) skind = OverlapSupp;
njn43c799e2003-04-08 00:08:52 +0000553 else
554 return False;
555
556 VG_(set_supp_kind)(su, skind);
557 return True;
558}
559
njn5c004e42002-11-18 11:04:50 +0000560Bool SK_(read_extra_suppression_info) ( Int fd, Char* buf, Int nBuf, Supp *su )
561{
562 Bool eof;
563
564 if (VG_(get_supp_kind)(su) == ParamSupp) {
565 eof = VG_(get_line) ( fd, buf, nBuf );
566 if (eof) return False;
567 VG_(set_supp_string)(su, VG_(strdup)(buf));
568 }
569 return True;
570}
571
sewardj99aac972002-12-26 01:53:45 +0000572Bool SK_(error_matches_suppression)(Error* err, Supp* su)
njn5c004e42002-11-18 11:04:50 +0000573{
sewardj05bcdcb2003-05-18 10:05:38 +0000574 Int su_size;
njn43c799e2003-04-08 00:08:52 +0000575 MAC_Error* err_extra = VG_(get_error_extra)(err);
576 ErrorKind ekind = VG_(get_error_kind )(err);
njn5c004e42002-11-18 11:04:50 +0000577
578 switch (VG_(get_supp_kind)(su)) {
579 case ParamSupp:
580 return (ekind == ParamErr
njn43c799e2003-04-08 00:08:52 +0000581 && VG_STREQ(VG_(get_error_string)(err),
582 VG_(get_supp_string)(su)));
njn5c004e42002-11-18 11:04:50 +0000583
584 case CoreMemSupp:
585 return (ekind == CoreMemErr
njn43c799e2003-04-08 00:08:52 +0000586 && VG_STREQ(VG_(get_error_string)(err),
587 VG_(get_supp_string)(su)));
njn5c004e42002-11-18 11:04:50 +0000588
589 case Value0Supp: su_size = 0; goto value_case;
590 case Value1Supp: su_size = 1; goto value_case;
591 case Value2Supp: su_size = 2; goto value_case;
592 case Value4Supp: su_size = 4; goto value_case;
593 case Value8Supp: su_size = 8; goto value_case;
njnc0616662003-06-12 09:58:41 +0000594 case Value16Supp:su_size =16; goto value_case;
njn5c004e42002-11-18 11:04:50 +0000595 value_case:
596 return (ekind == ValueErr && err_extra->size == su_size);
597
598 case Addr1Supp: su_size = 1; goto addr_case;
599 case Addr2Supp: su_size = 2; goto addr_case;
600 case Addr4Supp: su_size = 4; goto addr_case;
601 case Addr8Supp: su_size = 8; goto addr_case;
njnc0616662003-06-12 09:58:41 +0000602 case Addr16Supp:su_size =16; goto addr_case;
njn5c004e42002-11-18 11:04:50 +0000603 addr_case:
604 return (ekind == AddrErr && err_extra->size == su_size);
605
606 case FreeSupp:
607 return (ekind == FreeErr || ekind == FreeMismatchErr);
608
njn34419c12003-05-02 17:24:29 +0000609 case OverlapSupp:
610 return (ekind = OverlapErr);
611
sewardj4a19e2f2002-12-26 11:50:21 +0000612 case LeakSupp:
njn43c799e2003-04-08 00:08:52 +0000613 return (ekind == LeakErr);
sewardj4a19e2f2002-12-26 11:50:21 +0000614
njn5c004e42002-11-18 11:04:50 +0000615 default:
616 VG_(printf)("Error:\n"
617 " unknown suppression type %d\n",
618 VG_(get_supp_kind)(su));
619 VG_(skin_panic)("unknown suppression type in "
620 "SK_(error_matches_suppression)");
621 }
622}
623
njn43c799e2003-04-08 00:08:52 +0000624Char* SK_(get_error_name) ( Error* err )
625{
626 Char* s;
627 switch (VG_(get_error_kind)(err)) {
628 case ParamErr: return "Param";
629 case UserErr: return NULL; /* Can't suppress User errors */
630 case FreeMismatchErr: return "Free";
631 case FreeErr: return "Free";
632 case AddrErr:
633 switch ( ((MAC_Error*)VG_(get_error_extra)(err))->size ) {
634 case 1: return "Addr1";
635 case 2: return "Addr2";
636 case 4: return "Addr4";
637 case 8: return "Addr8";
njnc0616662003-06-12 09:58:41 +0000638 case 16: return "Addr16";
njn43c799e2003-04-08 00:08:52 +0000639 default: VG_(skin_panic)("unexpected size for Addr");
640 }
641
642 case ValueErr:
643 switch ( ((MAC_Error*)VG_(get_error_extra)(err))->size ) {
644 case 0: return "Cond";
645 case 1: return "Value1";
646 case 2: return "Value2";
647 case 4: return "Value4";
648 case 8: return "Value8";
njnc0616662003-06-12 09:58:41 +0000649 case 16: return "Value16";
njn43c799e2003-04-08 00:08:52 +0000650 default: VG_(skin_panic)("unexpected size for Value");
651 }
652 case CoreMemErr: return "CoreMem";
njn34419c12003-05-02 17:24:29 +0000653 case OverlapErr: return "Overlap";
njn43c799e2003-04-08 00:08:52 +0000654 case LeakErr: return "Leak";
655 default: VG_(skin_panic)("get_error_name: unexpected type");
656 }
657 VG_(printf)(s);
658}
659
660void SK_(print_extra_suppression_info) ( Error* err )
661{
662 if (ParamErr == VG_(get_error_kind)(err)) {
663 VG_(printf)(" %s\n", VG_(get_error_string)(err));
664 }
665}
njn5c004e42002-11-18 11:04:50 +0000666
667/*------------------------------------------------------------*/
668/*--- Crude profiling machinery. ---*/
669/*------------------------------------------------------------*/
670
671/* Event index. If just the name of the fn is given, this means the
672 number of calls to the fn. Otherwise it is the specified event.
673 Ones marked 'M' are MemCheck only. Ones marked 'A' are AddrCheck only.
674 The rest are shared.
675
676 10 alloc_secondary_map
677
678 20 get_abit
679M 21 get_vbyte
680 22 set_abit
681M 23 set_vbyte
682 24 get_abits4_ALIGNED
683M 25 get_vbytes4_ALIGNED
684
685 30 set_address_range_perms
686 31 set_address_range_perms(lower byte loop)
687 32 set_address_range_perms(quadword loop)
688 33 set_address_range_perms(upper byte loop)
689
690 35 make_noaccess
691 36 make_writable
692 37 make_readable
693A 38 make_accessible
694
695 40 copy_address_range_state
696 41 copy_address_range_state(byte loop)
697 42 check_writable
698 43 check_writable(byte loop)
699 44 check_readable
700 45 check_readable(byte loop)
701 46 check_readable_asciiz
702 47 check_readable_asciiz(byte loop)
703A 48 check_accessible
704A 49 check_accessible(byte loop)
705
706 50 make_noaccess_aligned
707 51 make_writable_aligned
708
709M 60 helperc_LOADV4
710M 61 helperc_STOREV4
711M 62 helperc_LOADV2
712M 63 helperc_STOREV2
713M 64 helperc_LOADV1
714M 65 helperc_STOREV1
715
716A 66 helperc_ACCESS4
717A 67 helperc_ACCESS2
718A 68 helperc_ACCESS1
719
720M 70 rim_rd_V4_SLOWLY
721M 71 rim_wr_V4_SLOWLY
722M 72 rim_rd_V2_SLOWLY
723M 73 rim_wr_V2_SLOWLY
724M 74 rim_rd_V1_SLOWLY
725M 75 rim_wr_V1_SLOWLY
726
727A 76 ACCESS4_SLOWLY
728A 77 ACCESS2_SLOWLY
729A 78 ACCESS1_SLOWLY
730
731 80 fpu_read
732 81 fpu_read aligned 4
733 82 fpu_read aligned 8
734 83 fpu_read 2
735 84 fpu_read 10/28/108
736
737M 85 fpu_write
738M 86 fpu_write aligned 4
739M 87 fpu_write aligned 8
740M 88 fpu_write 2
741M 89 fpu_write 10/28/108
742
743 90 fpu_access
744 91 fpu_access aligned 4
745 92 fpu_access aligned 8
746 93 fpu_access 2
747 94 fpu_access 10/28/108
748
749 100 fpu_access_check_SLOWLY
750 101 fpu_access_check_SLOWLY(byte loop)
njn9b007f62003-04-07 14:40:25 +0000751
752 110 new_mem_stack_4
753 111 new_mem_stack_8
754 112 new_mem_stack_12
755 113 new_mem_stack_16
756 114 new_mem_stack_32
757 115 new_mem_stack
758
759 120 die_mem_stack_4
760 121 die_mem_stack_8
761 122 die_mem_stack_12
762 123 die_mem_stack_16
763 124 die_mem_stack_32
764 125 die_mem_stack
njn5c004e42002-11-18 11:04:50 +0000765*/
766
njn43c799e2003-04-08 00:08:52 +0000767#ifdef MAC_PROFILE_MEMORY
njn5c004e42002-11-18 11:04:50 +0000768
njn43c799e2003-04-08 00:08:52 +0000769UInt MAC_(event_ctr)[N_PROF_EVENTS];
njn5c004e42002-11-18 11:04:50 +0000770
njnb4aee052003-04-15 14:09:58 +0000771static void init_prof_mem ( void )
njn5c004e42002-11-18 11:04:50 +0000772{
773 Int i;
774 for (i = 0; i < N_PROF_EVENTS; i++)
njn43c799e2003-04-08 00:08:52 +0000775 MAC_(event_ctr)[i] = 0;
njn5c004e42002-11-18 11:04:50 +0000776}
777
njnb4aee052003-04-15 14:09:58 +0000778static void done_prof_mem ( void )
njn5c004e42002-11-18 11:04:50 +0000779{
780 Int i;
781 for (i = 0; i < N_PROF_EVENTS; i++) {
782 if ((i % 10) == 0)
783 VG_(printf)("\n");
njn43c799e2003-04-08 00:08:52 +0000784 if (MAC_(event_ctr)[i] > 0)
785 VG_(printf)( "prof mem event %2d: %d\n", i, MAC_(event_ctr)[i] );
njn5c004e42002-11-18 11:04:50 +0000786 }
787 VG_(printf)("\n");
788}
789
790#else
791
njnb4aee052003-04-15 14:09:58 +0000792static void init_prof_mem ( void ) { }
793static void done_prof_mem ( void ) { }
njn5c004e42002-11-18 11:04:50 +0000794
njn5c004e42002-11-18 11:04:50 +0000795#endif
796
797/*------------------------------------------------------------*/
njn3e884182003-04-15 13:03:23 +0000798/*--- Common initialisation + finalisation ---*/
799/*------------------------------------------------------------*/
800
801void MAC_(common_pre_clo_init)(void)
802{
803 MAC_(malloc_list) = VG_(HT_construct)();
804 init_prof_mem();
805}
806
807void MAC_(common_fini)(void (*leak_check)(void))
808{
809 MAC_(print_malloc_stats)();
810
811 if (VG_(clo_verbosity) == 1) {
812 if (!MAC_(clo_leak_check))
813 VG_(message)(Vg_UserMsg,
814 "For a detailed leak analysis, rerun with: --leak-check=yes");
815
816 VG_(message)(Vg_UserMsg,
817 "For counts of detected errors, rerun with: -v");
818 }
819 if (MAC_(clo_leak_check)) leak_check();
820
821 done_prof_mem();
822}
823
824/*------------------------------------------------------------*/
njn47363ab2003-04-21 13:24:40 +0000825/*--- Common client request handling ---*/
826/*------------------------------------------------------------*/
827
njn72718642003-07-24 08:45:32 +0000828Bool MAC_(handle_common_client_requests)(ThreadId tid, UInt* arg, UInt* ret )
njn47363ab2003-04-21 13:24:40 +0000829{
njn10785452003-05-20 16:38:24 +0000830 UInt* argv = (UInt*)arg;
njn72718642003-07-24 08:45:32 +0000831
832 // Not using 'tid' here because MAC_(new_block)() and MAC_(handle_free)()
833 // grab it themselves. But what they grab should match 'tid', check
834 // this.
835 sk_assert(tid == VG_(get_current_or_recent_tid)());
njn47363ab2003-04-21 13:24:40 +0000836
837 switch (arg[0]) {
njn10785452003-05-20 16:38:24 +0000838 case VG_USERREQ__COUNT_LEAKS: { /* count leaked bytes */
839 UInt** argp = (UInt**)arg;
njne8b5c052003-07-22 22:03:58 +0000840 // MAC_(bytes_leaked) et al were set by the last leak check (or zero
841 // if no prior leak checks performed).
842 *argp[1] = MAC_(bytes_leaked);
843 *argp[2] = MAC_(bytes_dubious);
844 *argp[3] = MAC_(bytes_reachable);
845 *argp[4] = MAC_(bytes_suppressed);
njn47363ab2003-04-21 13:24:40 +0000846 *ret = 0;
847 return True;
njn10785452003-05-20 16:38:24 +0000848 }
849 case VG_USERREQ__MALLOCLIKE_BLOCK: {
850 Addr p = (Addr)argv[1];
851 UInt sizeB = argv[2];
852 UInt rzB = argv[3];
853 Bool is_zeroed = (Bool)argv[4];
njn47363ab2003-04-21 13:24:40 +0000854
njn72718642003-07-24 08:45:32 +0000855 MAC_(new_block) ( p, sizeB, rzB, is_zeroed, MAC_AllocCustom );
njn10785452003-05-20 16:38:24 +0000856 return True;
857 }
858 case VG_USERREQ__FREELIKE_BLOCK: {
859 Addr p = (Addr)argv[1];
860 UInt rzB = argv[2];
861
njn72718642003-07-24 08:45:32 +0000862 MAC_(handle_free) ( p, rzB, MAC_AllocCustom );
njn10785452003-05-20 16:38:24 +0000863 return True;
864 }
njn47363ab2003-04-21 13:24:40 +0000865 default:
866 return False;
867 }
868}
869
870/*------------------------------------------------------------*/
njn5c004e42002-11-18 11:04:50 +0000871/*--- Syscall wrappers ---*/
872/*------------------------------------------------------------*/
873
874void* SK_(pre_syscall) ( ThreadId tid, UInt syscallno, Bool isBlocking )
875{
876 Int sane = SK_(cheap_sanity_check)();
877 return (void*)sane;
878}
879
880void SK_(post_syscall) ( ThreadId tid, UInt syscallno,
881 void* pre_result, Int res, Bool isBlocking )
882{
883 Int sane_before_call = (Int)pre_result;
884 Bool sane_after_call = SK_(cheap_sanity_check)();
885
886 if ((Int)sane_before_call && (!sane_after_call)) {
887 VG_(message)(Vg_DebugMsg, "post-syscall: ");
888 VG_(message)(Vg_DebugMsg,
889 "probable sanity check failure for syscall number %d\n",
890 syscallno );
891 VG_(skin_panic)("aborting due to the above ... bye!");
892 }
893}
894
njn5c004e42002-11-18 11:04:50 +0000895/*--------------------------------------------------------------------*/
njn43c799e2003-04-08 00:08:52 +0000896/*--- end mac_needs.c ---*/
njn5c004e42002-11-18 11:04:50 +0000897/*--------------------------------------------------------------------*/