blob: 8aa9322d63007ad7433690c8e79cc1ddd7605d64 [file] [log] [blame]
njn5c004e42002-11-18 11:04:50 +00001
2/*--------------------------------------------------------------------*/
njn43c799e2003-04-08 00:08:52 +00003/*--- Declarations shared between MemCheck and AddrCheck. ---*/
4/*--- mac_shared.h ---*/
njn5c004e42002-11-18 11:04:50 +00005/*--------------------------------------------------------------------*/
6
7/*
nethercote137bc552003-11-14 17:47:54 +00008 This file is part of MemCheck, a heavyweight Valgrind tool for
9 detecting memory errors, and AddrCheck, a lightweight Valgrind tool
njn5c004e42002-11-18 11:04:50 +000010 for detecting memory errors.
11
njn53612422005-03-12 16:22:54 +000012 Copyright (C) 2000-2005 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
njn43c799e2003-04-08 00:08:52 +000033/* Note: This header contains the declarations shared between
34 Addrcheck and Memcheck, and is #included by both. */
35
36#ifndef __MAC_SHARED_H
37#define __MAC_SHARED_H
njn5c004e42002-11-18 11:04:50 +000038
nethercote46063202004-09-02 08:51:43 +000039#include "tool.h"
njn43c799e2003-04-08 00:08:52 +000040
41#define MAC_(str) VGAPPEND(vgMAC_,str)
njn5c004e42002-11-18 11:04:50 +000042
njn9b007f62003-04-07 14:40:25 +000043/*------------------------------------------------------------*/
44/*--- Errors and suppressions ---*/
45/*------------------------------------------------------------*/
46
njn5c004e42002-11-18 11:04:50 +000047/* The classification of a faulting address. */
48typedef
njn43c799e2003-04-08 00:08:52 +000049 enum {
nethercote8b76fe52004-11-08 19:20:09 +000050 Undescribed, // as-yet unclassified
njn43c799e2003-04-08 00:08:52 +000051 Stack,
nethercote8b76fe52004-11-08 19:20:09 +000052 Unknown, // classification yielded nothing useful
njn43c799e2003-04-08 00:08:52 +000053 Freed, Mallocd,
nethercote8b76fe52004-11-08 19:20:09 +000054 UserG, // in a user-defined block
55 Mempool, // in a mempool
56 Register, // in a register; for Param errors only
njn5c004e42002-11-18 11:04:50 +000057 }
58 AddrKind;
59
60/* Records info about a faulting address. */
61typedef
nethercote05675c82004-08-04 10:37:49 +000062 struct { // Used by:
63 AddrKind akind; // ALL
nethercote928a5f72004-11-03 18:10:37 +000064 SizeT blksize; // Freed, Mallocd
nethercote8b76fe52004-11-08 19:20:09 +000065 OffT rwoffset; // Freed, Mallocd
nethercote05675c82004-08-04 10:37:49 +000066 ExeContext* lastchange; // Freed, Mallocd
67 ThreadId stack_tid; // Stack
sewardjb5f6f512005-03-10 23:59:00 +000068 const Char *desc; // UserG
nethercote05675c82004-08-04 10:37:49 +000069 Bool maybe_gcc; // True if just below %esp -- could be a gcc bug.
njn5c004e42002-11-18 11:04:50 +000070 }
71 AddrInfo;
72
73typedef
74 enum {
nethercote05675c82004-08-04 10:37:49 +000075 ParamSupp, // Bad syscall params
76 CoreMemSupp, // Memory errors in core (pthread ops, signal handling)
77
78 // Use of invalid values of given size (MemCheck only)
njnc0616662003-06-12 09:58:41 +000079 Value0Supp, Value1Supp, Value2Supp, Value4Supp, Value8Supp, Value16Supp,
nethercote05675c82004-08-04 10:37:49 +000080
81 // Invalid read/write attempt at given size
njnc0616662003-06-12 09:58:41 +000082 Addr1Supp, Addr2Supp, Addr4Supp, Addr8Supp, Addr16Supp,
nethercote05675c82004-08-04 10:37:49 +000083
84 FreeSupp, // Invalid or mismatching free
85 OverlapSupp, // Overlapping blocks in memcpy(), strcpy(), etc
86 LeakSupp, // Something to be suppressed in a leak check.
87 MempoolSupp, // Memory pool suppression.
njn5c004e42002-11-18 11:04:50 +000088 }
njn43c799e2003-04-08 00:08:52 +000089 MAC_SuppKind;
njn5c004e42002-11-18 11:04:50 +000090
91/* What kind of error it is. */
92typedef
93 enum { ValueErr, /* Memcheck only */
94 CoreMemErr,
95 AddrErr,
96 ParamErr, UserErr, /* behaves like an anonymous ParamErr */
njn43c799e2003-04-08 00:08:52 +000097 FreeErr, FreeMismatchErr,
njn7201b2a2003-08-19 12:16:05 +000098 OverlapErr,
rjwalshbc0bb832004-06-19 18:12:36 +000099 LeakErr,
100 IllegalMempoolErr,
njn5c004e42002-11-18 11:04:50 +0000101 }
njn43c799e2003-04-08 00:08:52 +0000102 MAC_ErrorKind;
njn5c004e42002-11-18 11:04:50 +0000103
104/* What kind of memory access is involved in the error? */
105typedef
106 enum { ReadAxs, WriteAxs, ExecAxs }
107 AxsKind;
108
109/* Extra context for memory errors */
110typedef
nethercote05675c82004-08-04 10:37:49 +0000111 struct { // Used by:
112 AxsKind axskind; // AddrErr
113 Int size; // AddrErr, ValueErr
114 AddrInfo addrinfo; // {Addr,Free,FreeMismatch,Param,User}Err
nethercote8b76fe52004-11-08 19:20:09 +0000115 Bool isUnaddr; // {CoreMem,Param,User}Err
njn5c004e42002-11-18 11:04:50 +0000116 }
njn43c799e2003-04-08 00:08:52 +0000117 MAC_Error;
njn5c004e42002-11-18 11:04:50 +0000118
njnb6cae9f2003-09-04 20:50:47 +0000119/* Extra info for overlap errors */
120typedef
121 struct {
122 Addr src;
123 Addr dst;
124 Int len; // -1 if unused
125 }
126 OverlapExtra;
127
njn3e884182003-04-15 13:03:23 +0000128/* For malloc()/new/new[] vs. free()/delete/delete[] mismatch checking. */
129typedef
130 enum {
131 MAC_AllocMalloc = 0,
132 MAC_AllocNew = 1,
njn10785452003-05-20 16:38:24 +0000133 MAC_AllocNewVec = 2,
134 MAC_AllocCustom = 3
njn3e884182003-04-15 13:03:23 +0000135 }
136 MAC_AllocKind;
137
138/* Nb: first two fields must match core's VgHashNode. */
139typedef
140 struct _MAC_Chunk {
141 struct _MAC_Chunk* next;
nethercote05675c82004-08-04 10:37:49 +0000142 Addr data; // ptr to actual block
njnb729e0d2004-12-01 00:19:38 +0000143 SizeT size : (sizeof(UWord)*8)-2; // size requested; 30 or 62 bits
nethercote05675c82004-08-04 10:37:49 +0000144 MAC_AllocKind allockind : 2; // which wrapper did the allocation
145 ExeContext* where; // where it was allocated
njn3e884182003-04-15 13:03:23 +0000146 }
147 MAC_Chunk;
148
rjwalshbc0bb832004-06-19 18:12:36 +0000149/* Memory pool. Nb: first two fields must match core's VgHashNode. */
150typedef
151 struct _MAC_Mempool {
152 struct _MAC_Mempool* next;
nethercote05675c82004-08-04 10:37:49 +0000153 Addr pool; // pool identifier
njnb729e0d2004-12-01 00:19:38 +0000154 SizeT rzB; // pool red-zone size
nethercote05675c82004-08-04 10:37:49 +0000155 Bool is_zeroed; // allocations from this pool are zeroed
156 VgHashTable chunks; // chunks associated with this pool
rjwalshbc0bb832004-06-19 18:12:36 +0000157 }
158 MAC_Mempool;
159
160
njn9b007f62003-04-07 14:40:25 +0000161/*------------------------------------------------------------*/
nethercote7cc9c232004-01-21 15:08:04 +0000162/*--- Profiling of tools and memory events ---*/
njn9b007f62003-04-07 14:40:25 +0000163/*------------------------------------------------------------*/
164
165typedef
166 enum {
167 VgpCheckMem = VgpFini+1,
168 VgpSetMem,
169 VgpESPAdj
170 }
nethercote7cc9c232004-01-21 15:08:04 +0000171 VgpToolCC;
njn9b007f62003-04-07 14:40:25 +0000172
173/* Define to collect detailed performance info. */
sewardj23eb2fd2005-04-22 16:29:19 +0000174/* #define MAC_PROFILE_MEMORY */
njn5c004e42002-11-18 11:04:50 +0000175
njn43c799e2003-04-08 00:08:52 +0000176#ifdef MAC_PROFILE_MEMORY
sewardjc1a2cda2005-04-21 17:34:00 +0000177# define N_PROF_EVENTS 500
njn5c004e42002-11-18 11:04:50 +0000178
sewardjc1a2cda2005-04-21 17:34:00 +0000179extern UInt MAC_(event_ctr)[N_PROF_EVENTS];
180extern HChar* MAC_(event_ctr_name)[N_PROF_EVENTS];
njn9b007f62003-04-07 14:40:25 +0000181
sewardjc1a2cda2005-04-21 17:34:00 +0000182# define PROF_EVENT(ev, name) \
183 do { tl_assert((ev) >= 0 && (ev) < N_PROF_EVENTS); \
184 /* crude and inaccurate check to ensure the same */ \
185 /* event isn't being used with > 1 name */ \
186 if (MAC_(event_ctr_name)[ev]) \
187 tl_assert(name == MAC_(event_ctr_name)[ev]); \
188 MAC_(event_ctr)[ev]++; \
189 MAC_(event_ctr_name)[ev] = (name); \
njn5c004e42002-11-18 11:04:50 +0000190 } while (False);
191
192#else
193
sewardjc1a2cda2005-04-21 17:34:00 +0000194# define PROF_EVENT(ev, name) /* */
njn5c004e42002-11-18 11:04:50 +0000195
njn43c799e2003-04-08 00:08:52 +0000196#endif /* MAC_PROFILE_MEMORY */
njn5c004e42002-11-18 11:04:50 +0000197
njn9b007f62003-04-07 14:40:25 +0000198
sewardj23eb2fd2005-04-22 16:29:19 +0000199/*------------------------------------------------------------*/
200/*--- V and A bits (Victoria & Albert ?) ---*/
201/*------------------------------------------------------------*/
202
203/* expand 1 bit -> 8 */
204#define BIT_TO_BYTE(b) ((~(((UChar)(b) & 1) - 1)) & 0xFF)
205
sewardj45d94cc2005-04-20 14:44:11 +0000206//zz #define SECONDARY_SHIFT 16
207//zz #define SECONDARY_SIZE (1 << SECONDARY_SHIFT)
208//zz #define SECONDARY_MASK (SECONDARY_SIZE - 1)
209//zz
210//zz #define PRIMARY_SIZE (1 << (32 - SECONDARY_SHIFT))
211//zz
212//zz #define SM_OFF(addr) ((addr) & SECONDARY_MASK)
213//zz #define PM_IDX(addr) ((addr) >> SECONDARY_SHIFT)
214/*
sewardjb5f6f512005-03-10 23:59:00 +0000215#define IS_DISTINGUISHED_SM(smap) \
216 ((smap) >= &distinguished_secondary_maps[0] && \
217 (smap) < &distinguished_secondary_maps[N_SECONDARY_MAPS])
218
219#define IS_DISTINGUISHED(addr) (IS_DISTINGUISHED_SM(primary_map[PM_IDX(addr)]))
njn5c004e42002-11-18 11:04:50 +0000220
njn43c799e2003-04-08 00:08:52 +0000221#define ENSURE_MAPPABLE(addr,caller) \
222 do { \
sewardjb5f6f512005-03-10 23:59:00 +0000223 if (IS_DISTINGUISHED(addr)) { \
224 primary_map[PM_IDX(addr)] = alloc_secondary_map(caller, primary_map[PM_IDX(addr)]); \
sewardj45d94cc2005-04-20 14:44:11 +0000225 if (0) VG_(printf)("new 2map because of %p\n", addr); \
njn43c799e2003-04-08 00:08:52 +0000226 } \
sewardj45d94cc2005-04-20 14:44:11 +0000227 } while(0)
228*/
njn5c004e42002-11-18 11:04:50 +0000229
230#define BITARR_SET(aaa_p,iii_p) \
231 do { \
sewardj45d94cc2005-04-20 14:44:11 +0000232 UWord iii = (UWord)iii_p; \
233 UChar* aaa = (UChar*)aaa_p; \
njn5c004e42002-11-18 11:04:50 +0000234 aaa[iii >> 3] |= (1 << (iii & 7)); \
235 } while (0)
236
237#define BITARR_CLEAR(aaa_p,iii_p) \
238 do { \
sewardj45d94cc2005-04-20 14:44:11 +0000239 UWord iii = (UWord)iii_p; \
240 UChar* aaa = (UChar*)aaa_p; \
njn5c004e42002-11-18 11:04:50 +0000241 aaa[iii >> 3] &= ~(1 << (iii & 7)); \
242 } while (0)
243
244#define BITARR_TEST(aaa_p,iii_p) \
sewardj45d94cc2005-04-20 14:44:11 +0000245 (0 != (((UChar*)aaa_p)[ ((UWord)iii_p) >> 3 ] \
246 & (1 << (((UWord)iii_p) & 7)))) \
247
248static inline
249void write_bit_array ( UChar* arr, UWord idx, UWord bit )
250{
251 UWord shift = idx & 7;
252 idx >>= 3;
253 bit &= 1;
254 arr[idx] = (arr[idx] & ~(1<<shift)) | (bit << shift);
255}
256
257static inline
258UWord read_bit_array ( UChar* arr, UWord idx )
259{
260 UWord shift = idx & 7;
261 idx >>= 3;
262 return 1 & (arr[idx] >> shift);
263}
njn5c004e42002-11-18 11:04:50 +0000264
265
sewardj45d94cc2005-04-20 14:44:11 +0000266#define VGM_BIT_VALID 0
267#define VGM_BIT_INVALID 1
njn5c004e42002-11-18 11:04:50 +0000268
sewardj45d94cc2005-04-20 14:44:11 +0000269#define VGM_NIBBLE_VALID 0
270#define VGM_NIBBLE_INVALID 0xF
njn5c004e42002-11-18 11:04:50 +0000271
sewardj45d94cc2005-04-20 14:44:11 +0000272#define VGM_BYTE_VALID 0
273#define VGM_BYTE_INVALID 0xFF
njn5c004e42002-11-18 11:04:50 +0000274
sewardj45d94cc2005-04-20 14:44:11 +0000275#define VGM_WORD32_VALID 0
276#define VGM_WORD32_INVALID 0xFFFFFFFF
njn5c004e42002-11-18 11:04:50 +0000277
sewardj45d94cc2005-04-20 14:44:11 +0000278#define VGM_WORD64_VALID 0ULL
279#define VGM_WORD64_INVALID 0xFFFFFFFFFFFFFFFFULL
280
njn5c004e42002-11-18 11:04:50 +0000281
282/*------------------------------------------------------------*/
283/*--- Command line options + defaults ---*/
284/*------------------------------------------------------------*/
285
njn43c799e2003-04-08 00:08:52 +0000286/* Memcheck defines a couple more. */
njn5c004e42002-11-18 11:04:50 +0000287
288/* Allow loads from partially-valid addresses? default: YES */
njn43c799e2003-04-08 00:08:52 +0000289extern Bool MAC_(clo_partial_loads_ok);
njn5c004e42002-11-18 11:04:50 +0000290
291/* Max volume of the freed blocks queue. */
njn43c799e2003-04-08 00:08:52 +0000292extern Int MAC_(clo_freelist_vol);
njn5c004e42002-11-18 11:04:50 +0000293
294/* Do leak check at exit? default: NO */
sewardjb5f6f512005-03-10 23:59:00 +0000295typedef
296 enum {
297 LC_Off,
298 LC_Summary,
299 LC_Full,
300 }
301 LeakCheckMode;
302
303extern LeakCheckMode MAC_(clo_leak_check);
njn5c004e42002-11-18 11:04:50 +0000304
305/* How closely should we compare ExeContexts in leak records? default: 2 */
njn43c799e2003-04-08 00:08:52 +0000306extern VgRes MAC_(clo_leak_resolution);
njn5c004e42002-11-18 11:04:50 +0000307
308/* In leak check, show reachable-but-not-freed blocks? default: NO */
njn43c799e2003-04-08 00:08:52 +0000309extern Bool MAC_(clo_show_reachable);
njn5c004e42002-11-18 11:04:50 +0000310
311/* Assume accesses immediately below %esp are due to gcc-2.96 bugs.
312 * default: NO*/
njn43c799e2003-04-08 00:08:52 +0000313extern Bool MAC_(clo_workaround_gcc296_bugs);
njn5c004e42002-11-18 11:04:50 +0000314
njn3e884182003-04-15 13:03:23 +0000315extern Bool MAC_(process_common_cmd_line_option) ( Char* arg );
316extern void MAC_(print_common_usage) ( void );
317extern void MAC_(print_common_debug_usage) ( void );
318
njn8a97c6d2005-03-31 04:37:24 +0000319/* We want a 16B redzone on heap blocks for Addrcheck and Memcheck */
320#define MALLOC_REDZONE_SZB 16
njn3e884182003-04-15 13:03:23 +0000321
322/*------------------------------------------------------------*/
323/*--- Variables ---*/
324/*------------------------------------------------------------*/
325
326/* For tracking malloc'd blocks */
327extern VgHashTable MAC_(malloc_list);
328
rjwalshbc0bb832004-06-19 18:12:36 +0000329/* For tracking memory pools. */
330extern VgHashTable MAC_(mempool_list);
331
nethercote7cc9c232004-01-21 15:08:04 +0000332/* Function pointers for the two tools to track interesting events. */
nethercote451eae92004-11-02 13:06:32 +0000333extern void (*MAC_(new_mem_heap)) ( Addr a, SizeT len, Bool is_inited );
334extern void (*MAC_(ban_mem_heap)) ( Addr a, SizeT len );
335extern void (*MAC_(die_mem_heap)) ( Addr a, SizeT len );
336extern void (*MAC_(copy_mem_heap))( Addr from, Addr to, SizeT len );
njn3e884182003-04-15 13:03:23 +0000337
sewardjecf8e102003-07-12 12:11:39 +0000338/* Function pointers for internal sanity checking. */
nethercote451eae92004-11-02 13:06:32 +0000339extern Bool (*MAC_(check_noaccess))( Addr a, SizeT len, Addr* bad_addr );
sewardjecf8e102003-07-12 12:11:39 +0000340
njn3e884182003-04-15 13:03:23 +0000341/* Used in describe_addr() */
342extern Bool (*MAC_(describe_addr_supp)) ( Addr a, AddrInfo* ai );
njn5c004e42002-11-18 11:04:50 +0000343
njn47363ab2003-04-21 13:24:40 +0000344/* For VALGRIND_COUNT_LEAKS client request */
njne8b5c052003-07-22 22:03:58 +0000345extern Int MAC_(bytes_leaked);
sewardjb5f6f512005-03-10 23:59:00 +0000346extern Int MAC_(bytes_indirect);
njne8b5c052003-07-22 22:03:58 +0000347extern Int MAC_(bytes_dubious);
348extern Int MAC_(bytes_reachable);
349extern Int MAC_(bytes_suppressed);
sewardj99aac972002-12-26 01:53:45 +0000350
njn5c004e42002-11-18 11:04:50 +0000351/*------------------------------------------------------------*/
352/*--- Functions ---*/
353/*------------------------------------------------------------*/
354
njn43c799e2003-04-08 00:08:52 +0000355extern void MAC_(pp_AddrInfo) ( Addr a, AddrInfo* ai );
njn5c004e42002-11-18 11:04:50 +0000356
njn43c799e2003-04-08 00:08:52 +0000357extern void MAC_(clear_MAC_Error) ( MAC_Error* err_extra );
njn5c004e42002-11-18 11:04:50 +0000358
njn43c799e2003-04-08 00:08:52 +0000359extern Bool MAC_(shared_recognised_suppression) ( Char* name, Supp* su );
njn5c004e42002-11-18 11:04:50 +0000360
sewardj2a99cf62004-11-24 10:44:19 +0000361extern void* MAC_(new_block) ( ThreadId tid,
362 Addr p, SizeT size, SizeT align, UInt rzB,
nethercote57e36b32004-07-10 14:56:28 +0000363 Bool is_zeroed, MAC_AllocKind kind,
364 VgHashTable table);
sewardj2a99cf62004-11-24 10:44:19 +0000365
366extern void MAC_(handle_free) ( ThreadId tid,
367 Addr p, UInt rzB, MAC_AllocKind kind );
njn10785452003-05-20 16:38:24 +0000368
rjwalshbc0bb832004-06-19 18:12:36 +0000369extern void MAC_(create_mempool)(Addr pool, UInt rzB, Bool is_zeroed);
sewardj2a99cf62004-11-24 10:44:19 +0000370
rjwalshbc0bb832004-06-19 18:12:36 +0000371extern void MAC_(destroy_mempool)(Addr pool);
sewardj2a99cf62004-11-24 10:44:19 +0000372
373extern void MAC_(mempool_alloc)(ThreadId tid,
374 Addr pool, Addr addr, SizeT size);
375
rjwalshbc0bb832004-06-19 18:12:36 +0000376extern void MAC_(mempool_free)(Addr pool, Addr addr);
377
njn72718642003-07-24 08:45:32 +0000378extern void MAC_(record_address_error) ( ThreadId tid, Addr a,
sewardjaf48a602003-07-06 00:54:47 +0000379 Int size, Bool isWrite );
nethercote8b76fe52004-11-08 19:20:09 +0000380extern void MAC_(record_core_mem_error) ( ThreadId tid, Bool isUnaddr,
njn43c799e2003-04-08 00:08:52 +0000381 Char* s );
nethercote8b76fe52004-11-08 19:20:09 +0000382extern void MAC_(record_param_error) ( ThreadId tid, Addr a, Bool isReg,
383 Bool isUnaddr, Char* msg );
njn72718642003-07-24 08:45:32 +0000384extern void MAC_(record_jump_error) ( ThreadId tid, Addr a );
385extern void MAC_(record_free_error) ( ThreadId tid, Addr a );
386extern void MAC_(record_freemismatch_error)( ThreadId tid, Addr a );
sewardj2a99cf62004-11-24 10:44:19 +0000387extern void MAC_(record_overlap_error) ( ThreadId tid,
388 Char* function, OverlapExtra* oe );
rjwalshbc0bb832004-06-19 18:12:36 +0000389extern void MAC_(record_illegal_mempool_error) ( ThreadId tid, Addr pool );
njn5c004e42002-11-18 11:04:50 +0000390
njnb126f732004-11-22 17:57:07 +0000391extern void MAC_(pp_shared_Error) ( Error* err);
njn43c799e2003-04-08 00:08:52 +0000392
thughes4ad52d02004-06-27 17:37:21 +0000393extern MAC_Chunk* MAC_(first_matching_freed_MAC_Chunk)( Bool (*p)(MAC_Chunk*, void*), void* d );
njn43c799e2003-04-08 00:08:52 +0000394
njn3e884182003-04-15 13:03:23 +0000395extern void MAC_(common_pre_clo_init) ( void );
njnb8dca862005-03-14 02:42:44 +0000396extern void MAC_(common_fini) ( void (*leak_check)(ThreadId tid,
397 LeakCheckMode mode) );
njn3e884182003-04-15 13:03:23 +0000398
njn72718642003-07-24 08:45:32 +0000399extern Bool MAC_(handle_common_client_requests) ( ThreadId tid,
nethercoted1b64b22004-11-04 18:22:28 +0000400 UWord* arg_block, UWord* ret );
njn47363ab2003-04-21 13:24:40 +0000401
njn43c799e2003-04-08 00:08:52 +0000402/* For leak checking */
403extern void MAC_(pp_LeakError)(void* vl, UInt n_this_record,
404 UInt n_total_records);
405
njn86f12dc2005-03-14 01:16:05 +0000406extern void MAC_(print_malloc_stats) ( void );
407
njn43c799e2003-04-08 00:08:52 +0000408extern void MAC_(do_detect_memory_leaks) (
njnb8dca862005-03-14 02:42:44 +0000409 ThreadId tid, LeakCheckMode mode,
sewardjb5f6f512005-03-10 23:59:00 +0000410 Bool (*is_valid_64k_chunk) ( UInt ),
411 Bool (*is_valid_address) ( Addr )
njn43c799e2003-04-08 00:08:52 +0000412 );
413
njn9fb73db2005-03-27 01:55:21 +0000414extern VGA_REGPARM(1) void MAC_(new_mem_stack_4) ( Addr old_ESP );
415extern VGA_REGPARM(1) void MAC_(die_mem_stack_4) ( Addr old_ESP );
416extern VGA_REGPARM(1) void MAC_(new_mem_stack_8) ( Addr old_ESP );
417extern VGA_REGPARM(1) void MAC_(die_mem_stack_8) ( Addr old_ESP );
418extern VGA_REGPARM(1) void MAC_(new_mem_stack_12) ( Addr old_ESP );
419extern VGA_REGPARM(1) void MAC_(die_mem_stack_12) ( Addr old_ESP );
420extern VGA_REGPARM(1) void MAC_(new_mem_stack_16) ( Addr old_ESP );
421extern VGA_REGPARM(1) void MAC_(die_mem_stack_16) ( Addr old_ESP );
422extern VGA_REGPARM(1) void MAC_(new_mem_stack_32) ( Addr old_ESP );
423extern VGA_REGPARM(1) void MAC_(die_mem_stack_32) ( Addr old_ESP );
424extern void MAC_(die_mem_stack) ( Addr a, SizeT len);
425extern void MAC_(new_mem_stack) ( Addr a, SizeT len);
njn9b007f62003-04-07 14:40:25 +0000426
427
428/*------------------------------------------------------------*/
429/*--- Stack pointer adjustment ---*/
430/*------------------------------------------------------------*/
431
432/* Some noble preprocessor abuse, to enable Memcheck and Addrcheck to
njn3e884182003-04-15 13:03:23 +0000433 share this code, but call different functions.
njn9b007f62003-04-07 14:40:25 +0000434
435 Note that this code is executed very frequently and must be highly
436 optimised, which is why I resort to the preprocessor to achieve the
sewardj45d94cc2005-04-20 14:44:11 +0000437 factoring, rather than eg. using function pointers.
njn9b007f62003-04-07 14:40:25 +0000438*/
439
sewardj045a4052005-04-23 22:42:27 +0000440#define SP_UPDATE_HANDLERS(ALIGNED4_NEW, ALIGNED4_DIE, \
441 ALIGNED8_NEW, ALIGNED8_DIE, \
442 UNALIGNED_NEW, UNALIGNED_DIE) \
443 \
444void VGA_REGPARM(1) MAC_(new_mem_stack_4)(Addr new_SP) \
445{ \
446 PROF_EVENT(110, "new_mem_stack_4"); \
447 if (VG_IS_4_ALIGNED(new_SP)) { \
448 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP ); \
449 } else { \
450 UNALIGNED_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP, 4 ); \
451 } \
452} \
453 \
454void VGA_REGPARM(1) MAC_(die_mem_stack_4)(Addr new_SP) \
455{ \
456 PROF_EVENT(120, "die_mem_stack_4"); \
457 if (VG_IS_4_ALIGNED(new_SP)) { \
458 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-4 ); \
459 } else { \
460 UNALIGNED_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-4, 4 ); \
461 } \
462} \
463 \
464void VGA_REGPARM(1) MAC_(new_mem_stack_8)(Addr new_SP) \
465{ \
466 PROF_EVENT(111, "new_mem_stack_8"); \
467 if (VG_IS_8_ALIGNED(new_SP)) { \
468 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP ); \
469 } else if (VG_IS_4_ALIGNED(new_SP)) { \
470 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP ); \
471 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP+4 ); \
472 } else { \
473 UNALIGNED_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP, 8 ); \
474 } \
475} \
476 \
477void VGA_REGPARM(1) MAC_(die_mem_stack_8)(Addr new_SP) \
478{ \
479 PROF_EVENT(121, "die_mem_stack_8"); \
480 if (VG_IS_8_ALIGNED(new_SP)) { \
481 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-8 ); \
482 } else if (VG_IS_4_ALIGNED(new_SP)) { \
483 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-8 ); \
484 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-4 ); \
485 } else { \
486 UNALIGNED_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-8, 8 ); \
487 } \
488} \
489 \
490void VGA_REGPARM(1) MAC_(new_mem_stack_12)(Addr new_SP) \
491{ \
492 PROF_EVENT(112, "new_mem_stack_12"); \
493 if (VG_IS_8_ALIGNED(new_SP)) { \
494 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP ); \
495 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP+8 ); \
496 } else if (VG_IS_4_ALIGNED(new_SP)) { \
497 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP ); \
498 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP+4 ); \
499 } else { \
500 UNALIGNED_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP, 12 ); \
501 } \
502} \
503 \
504void VGA_REGPARM(1) MAC_(die_mem_stack_12)(Addr new_SP) \
505{ \
506 PROF_EVENT(122, "die_mem_stack_12"); \
507 /* Note the -12 in the test */ \
508 if (VG_IS_8_ALIGNED(new_SP-12)) { \
509 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-12 ); \
510 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-4 ); \
511 } else if (VG_IS_4_ALIGNED(new_SP)) { \
512 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-12 ); \
513 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-8 ); \
514 } else { \
515 UNALIGNED_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-12, 12 ); \
516 } \
517} \
518 \
519void VGA_REGPARM(1) MAC_(new_mem_stack_16)(Addr new_SP) \
520{ \
521 PROF_EVENT(113, "new_mem_stack_16"); \
522 if (VG_IS_8_ALIGNED(new_SP)) { \
523 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP ); \
524 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP+8 ); \
525 } else if (VG_IS_4_ALIGNED(new_SP)) { \
526 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP ); \
527 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP+4 ); \
528 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP+12 ); \
529 } else { \
530 UNALIGNED_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP, 16 ); \
531 } \
532} \
533 \
534void VGA_REGPARM(1) MAC_(die_mem_stack_16)(Addr new_SP) \
535{ \
536 PROF_EVENT(123, "die_mem_stack_16"); \
537 if (VG_IS_8_ALIGNED(new_SP)) { \
538 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-16 ); \
539 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-8 ); \
540 } else if (VG_IS_4_ALIGNED(new_SP)) { \
541 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-16 ); \
542 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-12 ); \
543 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-4 ); \
544 } else { \
545 UNALIGNED_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-16, 16 ); \
546 } \
547} \
548 \
549void VGA_REGPARM(1) MAC_(new_mem_stack_32)(Addr new_SP) \
550{ \
551 PROF_EVENT(114, "new_mem_stack_32"); \
552 if (VG_IS_8_ALIGNED(new_SP)) { \
553 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP ); \
554 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP+8 ); \
555 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP+16 ); \
556 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP+24 ); \
557 } else if (VG_IS_4_ALIGNED(new_SP)) { \
558 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP ); \
559 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP+4 ); \
560 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP+12 ); \
561 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP+20 ); \
562 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP+28 ); \
563 } else { \
564 UNALIGNED_NEW ( -VGA_STACK_REDZONE_SIZE + new_SP, 32 ); \
565 } \
566} \
567 \
568void VGA_REGPARM(1) MAC_(die_mem_stack_32)(Addr new_SP) \
569{ \
570 PROF_EVENT(124, "die_mem_stack_32"); \
571 if (VG_IS_8_ALIGNED(new_SP)) { \
572 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-32 ); \
573 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-24 ); \
574 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-16 ); \
575 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP- 8 ); \
576 } else if (VG_IS_4_ALIGNED(new_SP)) { \
577 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-32 ); \
578 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-28 ); \
579 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-20 ); \
580 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-12 ); \
581 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-4 ); \
582 } else { \
583 UNALIGNED_DIE ( -VGA_STACK_REDZONE_SIZE + new_SP-32, 32 ); \
584 } \
585} \
586 \
587void MAC_(new_mem_stack) ( Addr a, SizeT len ) \
588{ \
589 PROF_EVENT(115, "new_mem_stack"); \
590 UNALIGNED_NEW ( -VGA_STACK_REDZONE_SIZE + a, len ); \
591} \
592 \
593void MAC_(die_mem_stack) ( Addr a, SizeT len ) \
594{ \
595 PROF_EVENT(125, "die_mem_stack"); \
596 UNALIGNED_DIE ( -VGA_STACK_REDZONE_SIZE + a, len ); \
njn9b007f62003-04-07 14:40:25 +0000597}
598
njn43c799e2003-04-08 00:08:52 +0000599#endif /* __MAC_SHARED_H */
njn5c004e42002-11-18 11:04:50 +0000600
601/*--------------------------------------------------------------------*/
njn43c799e2003-04-08 00:08:52 +0000602/*--- end mac_shared.h ---*/
njn5c004e42002-11-18 11:04:50 +0000603/*--------------------------------------------------------------------*/