blob: c541fc9ad50a5709b37b91923c080f6b5d8164af [file] [log] [blame]
njn5c004e42002-11-18 11:04:50 +00001
2/*--------------------------------------------------------------------*/
njn51d827b2005-05-09 01:02:08 +00003/*--- Declarations shared between Memcheck and Addrcheck. ---*/
njn43c799e2003-04-08 00:08:52 +00004/*--- 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
sewardj05fe85e2005-04-27 22:46:36 +0000206/* The number of entries in the primary map can be altered. However
207 we hardwire the assumption that each secondary map covers precisely
208 64k of address space. */
209#define SECONDARY_SIZE 65536 /* DO NOT CHANGE */
210#define SECONDARY_MASK (SECONDARY_SIZE-1) /* DO NOT CHANGE */
211
sewardj45d94cc2005-04-20 14:44:11 +0000212//zz #define SECONDARY_SHIFT 16
213//zz #define SECONDARY_SIZE (1 << SECONDARY_SHIFT)
214//zz #define SECONDARY_MASK (SECONDARY_SIZE - 1)
215//zz
216//zz #define PRIMARY_SIZE (1 << (32 - SECONDARY_SHIFT))
217//zz
218//zz #define SM_OFF(addr) ((addr) & SECONDARY_MASK)
219//zz #define PM_IDX(addr) ((addr) >> SECONDARY_SHIFT)
220/*
sewardjb5f6f512005-03-10 23:59:00 +0000221#define IS_DISTINGUISHED_SM(smap) \
222 ((smap) >= &distinguished_secondary_maps[0] && \
223 (smap) < &distinguished_secondary_maps[N_SECONDARY_MAPS])
224
225#define IS_DISTINGUISHED(addr) (IS_DISTINGUISHED_SM(primary_map[PM_IDX(addr)]))
njn5c004e42002-11-18 11:04:50 +0000226
njn43c799e2003-04-08 00:08:52 +0000227#define ENSURE_MAPPABLE(addr,caller) \
228 do { \
sewardjb5f6f512005-03-10 23:59:00 +0000229 if (IS_DISTINGUISHED(addr)) { \
230 primary_map[PM_IDX(addr)] = alloc_secondary_map(caller, primary_map[PM_IDX(addr)]); \
sewardj45d94cc2005-04-20 14:44:11 +0000231 if (0) VG_(printf)("new 2map because of %p\n", addr); \
njn43c799e2003-04-08 00:08:52 +0000232 } \
sewardj45d94cc2005-04-20 14:44:11 +0000233 } while(0)
234*/
njn5c004e42002-11-18 11:04:50 +0000235
236#define BITARR_SET(aaa_p,iii_p) \
237 do { \
sewardj45d94cc2005-04-20 14:44:11 +0000238 UWord iii = (UWord)iii_p; \
239 UChar* aaa = (UChar*)aaa_p; \
njn5c004e42002-11-18 11:04:50 +0000240 aaa[iii >> 3] |= (1 << (iii & 7)); \
241 } while (0)
242
243#define BITARR_CLEAR(aaa_p,iii_p) \
244 do { \
sewardj45d94cc2005-04-20 14:44:11 +0000245 UWord iii = (UWord)iii_p; \
246 UChar* aaa = (UChar*)aaa_p; \
njn5c004e42002-11-18 11:04:50 +0000247 aaa[iii >> 3] &= ~(1 << (iii & 7)); \
248 } while (0)
249
250#define BITARR_TEST(aaa_p,iii_p) \
sewardj45d94cc2005-04-20 14:44:11 +0000251 (0 != (((UChar*)aaa_p)[ ((UWord)iii_p) >> 3 ] \
252 & (1 << (((UWord)iii_p) & 7)))) \
253
254static inline
255void write_bit_array ( UChar* arr, UWord idx, UWord bit )
256{
257 UWord shift = idx & 7;
258 idx >>= 3;
259 bit &= 1;
260 arr[idx] = (arr[idx] & ~(1<<shift)) | (bit << shift);
261}
262
263static inline
264UWord read_bit_array ( UChar* arr, UWord idx )
265{
266 UWord shift = idx & 7;
267 idx >>= 3;
268 return 1 & (arr[idx] >> shift);
269}
njn5c004e42002-11-18 11:04:50 +0000270
271
sewardj45d94cc2005-04-20 14:44:11 +0000272#define VGM_BIT_VALID 0
273#define VGM_BIT_INVALID 1
njn5c004e42002-11-18 11:04:50 +0000274
sewardj45d94cc2005-04-20 14:44:11 +0000275#define VGM_NIBBLE_VALID 0
276#define VGM_NIBBLE_INVALID 0xF
njn5c004e42002-11-18 11:04:50 +0000277
sewardj45d94cc2005-04-20 14:44:11 +0000278#define VGM_BYTE_VALID 0
279#define VGM_BYTE_INVALID 0xFF
njn5c004e42002-11-18 11:04:50 +0000280
sewardj45d94cc2005-04-20 14:44:11 +0000281#define VGM_WORD32_VALID 0
282#define VGM_WORD32_INVALID 0xFFFFFFFF
njn5c004e42002-11-18 11:04:50 +0000283
sewardj45d94cc2005-04-20 14:44:11 +0000284#define VGM_WORD64_VALID 0ULL
285#define VGM_WORD64_INVALID 0xFFFFFFFFFFFFFFFFULL
286
njn5c004e42002-11-18 11:04:50 +0000287
288/*------------------------------------------------------------*/
289/*--- Command line options + defaults ---*/
290/*------------------------------------------------------------*/
291
njn43c799e2003-04-08 00:08:52 +0000292/* Memcheck defines a couple more. */
njn5c004e42002-11-18 11:04:50 +0000293
294/* Allow loads from partially-valid addresses? default: YES */
njn43c799e2003-04-08 00:08:52 +0000295extern Bool MAC_(clo_partial_loads_ok);
njn5c004e42002-11-18 11:04:50 +0000296
297/* Max volume of the freed blocks queue. */
njn43c799e2003-04-08 00:08:52 +0000298extern Int MAC_(clo_freelist_vol);
njn5c004e42002-11-18 11:04:50 +0000299
300/* Do leak check at exit? default: NO */
sewardjb5f6f512005-03-10 23:59:00 +0000301typedef
302 enum {
303 LC_Off,
304 LC_Summary,
305 LC_Full,
306 }
307 LeakCheckMode;
308
309extern LeakCheckMode MAC_(clo_leak_check);
njn5c004e42002-11-18 11:04:50 +0000310
311/* How closely should we compare ExeContexts in leak records? default: 2 */
njn43c799e2003-04-08 00:08:52 +0000312extern VgRes MAC_(clo_leak_resolution);
njn5c004e42002-11-18 11:04:50 +0000313
314/* In leak check, show reachable-but-not-freed blocks? default: NO */
njn43c799e2003-04-08 00:08:52 +0000315extern Bool MAC_(clo_show_reachable);
njn5c004e42002-11-18 11:04:50 +0000316
317/* Assume accesses immediately below %esp are due to gcc-2.96 bugs.
318 * default: NO*/
njn43c799e2003-04-08 00:08:52 +0000319extern Bool MAC_(clo_workaround_gcc296_bugs);
njn5c004e42002-11-18 11:04:50 +0000320
njn3e884182003-04-15 13:03:23 +0000321extern Bool MAC_(process_common_cmd_line_option) ( Char* arg );
322extern void MAC_(print_common_usage) ( void );
323extern void MAC_(print_common_debug_usage) ( void );
324
njn8a97c6d2005-03-31 04:37:24 +0000325/* We want a 16B redzone on heap blocks for Addrcheck and Memcheck */
njn51d827b2005-05-09 01:02:08 +0000326#define MAC_MALLOC_REDZONE_SZB 16
njn3e884182003-04-15 13:03:23 +0000327
328/*------------------------------------------------------------*/
329/*--- Variables ---*/
330/*------------------------------------------------------------*/
331
332/* For tracking malloc'd blocks */
333extern VgHashTable MAC_(malloc_list);
334
rjwalshbc0bb832004-06-19 18:12:36 +0000335/* For tracking memory pools. */
336extern VgHashTable MAC_(mempool_list);
337
nethercote7cc9c232004-01-21 15:08:04 +0000338/* Function pointers for the two tools to track interesting events. */
nethercote451eae92004-11-02 13:06:32 +0000339extern void (*MAC_(new_mem_heap)) ( Addr a, SizeT len, Bool is_inited );
340extern void (*MAC_(ban_mem_heap)) ( Addr a, SizeT len );
341extern void (*MAC_(die_mem_heap)) ( Addr a, SizeT len );
342extern void (*MAC_(copy_mem_heap))( Addr from, Addr to, SizeT len );
njn3e884182003-04-15 13:03:23 +0000343
sewardjecf8e102003-07-12 12:11:39 +0000344/* Function pointers for internal sanity checking. */
nethercote451eae92004-11-02 13:06:32 +0000345extern Bool (*MAC_(check_noaccess))( Addr a, SizeT len, Addr* bad_addr );
sewardjecf8e102003-07-12 12:11:39 +0000346
njn3e884182003-04-15 13:03:23 +0000347/* Used in describe_addr() */
348extern Bool (*MAC_(describe_addr_supp)) ( Addr a, AddrInfo* ai );
njn5c004e42002-11-18 11:04:50 +0000349
njn47363ab2003-04-21 13:24:40 +0000350/* For VALGRIND_COUNT_LEAKS client request */
njne8b5c052003-07-22 22:03:58 +0000351extern Int MAC_(bytes_leaked);
sewardjb5f6f512005-03-10 23:59:00 +0000352extern Int MAC_(bytes_indirect);
njne8b5c052003-07-22 22:03:58 +0000353extern Int MAC_(bytes_dubious);
354extern Int MAC_(bytes_reachable);
355extern Int MAC_(bytes_suppressed);
sewardj99aac972002-12-26 01:53:45 +0000356
njn5c004e42002-11-18 11:04:50 +0000357/*------------------------------------------------------------*/
358/*--- Functions ---*/
359/*------------------------------------------------------------*/
360
njn43c799e2003-04-08 00:08:52 +0000361extern void MAC_(pp_AddrInfo) ( Addr a, AddrInfo* ai );
njn5c004e42002-11-18 11:04:50 +0000362
njn43c799e2003-04-08 00:08:52 +0000363extern void MAC_(clear_MAC_Error) ( MAC_Error* err_extra );
njn5c004e42002-11-18 11:04:50 +0000364
njn51d827b2005-05-09 01:02:08 +0000365extern Bool MAC_(eq_Error) ( VgRes res, Error* e1, Error* e2 );
366extern UInt MAC_(update_extra)( Error* err );
367extern Bool MAC_(read_extra_suppression_info) ( Int fd, Char* buf, Int nBuf, Supp *su );
368extern Bool MAC_(error_matches_suppression)(Error* err, Supp* su);
369extern Char* MAC_(get_error_name) ( Error* err );
370extern void MAC_(print_extra_suppression_info) ( Error* err );
371
372extern Bool MAC_(shared_recognised_suppression) ( Char* name, Supp* su );
njn5c004e42002-11-18 11:04:50 +0000373
sewardj2a99cf62004-11-24 10:44:19 +0000374extern void* MAC_(new_block) ( ThreadId tid,
375 Addr p, SizeT size, SizeT align, UInt rzB,
nethercote57e36b32004-07-10 14:56:28 +0000376 Bool is_zeroed, MAC_AllocKind kind,
377 VgHashTable table);
sewardj2a99cf62004-11-24 10:44:19 +0000378
379extern void MAC_(handle_free) ( ThreadId tid,
380 Addr p, UInt rzB, MAC_AllocKind kind );
njn10785452003-05-20 16:38:24 +0000381
rjwalshbc0bb832004-06-19 18:12:36 +0000382extern void MAC_(create_mempool)(Addr pool, UInt rzB, Bool is_zeroed);
sewardj2a99cf62004-11-24 10:44:19 +0000383
rjwalshbc0bb832004-06-19 18:12:36 +0000384extern void MAC_(destroy_mempool)(Addr pool);
sewardj2a99cf62004-11-24 10:44:19 +0000385
386extern void MAC_(mempool_alloc)(ThreadId tid,
387 Addr pool, Addr addr, SizeT size);
388
rjwalshbc0bb832004-06-19 18:12:36 +0000389extern void MAC_(mempool_free)(Addr pool, Addr addr);
390
njn72718642003-07-24 08:45:32 +0000391extern void MAC_(record_address_error) ( ThreadId tid, Addr a,
sewardjaf48a602003-07-06 00:54:47 +0000392 Int size, Bool isWrite );
nethercote8b76fe52004-11-08 19:20:09 +0000393extern void MAC_(record_core_mem_error) ( ThreadId tid, Bool isUnaddr,
njn43c799e2003-04-08 00:08:52 +0000394 Char* s );
nethercote8b76fe52004-11-08 19:20:09 +0000395extern void MAC_(record_param_error) ( ThreadId tid, Addr a, Bool isReg,
396 Bool isUnaddr, Char* msg );
njn72718642003-07-24 08:45:32 +0000397extern void MAC_(record_jump_error) ( ThreadId tid, Addr a );
398extern void MAC_(record_free_error) ( ThreadId tid, Addr a );
399extern void MAC_(record_freemismatch_error)( ThreadId tid, Addr a );
sewardj2a99cf62004-11-24 10:44:19 +0000400extern void MAC_(record_overlap_error) ( ThreadId tid,
401 Char* function, OverlapExtra* oe );
rjwalshbc0bb832004-06-19 18:12:36 +0000402extern void MAC_(record_illegal_mempool_error) ( ThreadId tid, Addr pool );
njn5c004e42002-11-18 11:04:50 +0000403
njnb126f732004-11-22 17:57:07 +0000404extern void MAC_(pp_shared_Error) ( Error* err);
njn43c799e2003-04-08 00:08:52 +0000405
thughes4ad52d02004-06-27 17:37:21 +0000406extern MAC_Chunk* MAC_(first_matching_freed_MAC_Chunk)( Bool (*p)(MAC_Chunk*, void*), void* d );
njn43c799e2003-04-08 00:08:52 +0000407
njn3e884182003-04-15 13:03:23 +0000408extern void MAC_(common_pre_clo_init) ( void );
njnb8dca862005-03-14 02:42:44 +0000409extern void MAC_(common_fini) ( void (*leak_check)(ThreadId tid,
410 LeakCheckMode mode) );
njn3e884182003-04-15 13:03:23 +0000411
njn72718642003-07-24 08:45:32 +0000412extern Bool MAC_(handle_common_client_requests) ( ThreadId tid,
nethercoted1b64b22004-11-04 18:22:28 +0000413 UWord* arg_block, UWord* ret );
njn47363ab2003-04-21 13:24:40 +0000414
njn43c799e2003-04-08 00:08:52 +0000415/* For leak checking */
416extern void MAC_(pp_LeakError)(void* vl, UInt n_this_record,
417 UInt n_total_records);
418
njn86f12dc2005-03-14 01:16:05 +0000419extern void MAC_(print_malloc_stats) ( void );
420
njn43c799e2003-04-08 00:08:52 +0000421extern void MAC_(do_detect_memory_leaks) (
njnb8dca862005-03-14 02:42:44 +0000422 ThreadId tid, LeakCheckMode mode,
sewardj05fe85e2005-04-27 22:46:36 +0000423 Bool (*is_within_valid_secondary) ( Addr ),
424 Bool (*is_valid_aligned_word) ( Addr )
njn43c799e2003-04-08 00:08:52 +0000425 );
426
njn9fb73db2005-03-27 01:55:21 +0000427extern VGA_REGPARM(1) void MAC_(new_mem_stack_4) ( Addr old_ESP );
428extern VGA_REGPARM(1) void MAC_(die_mem_stack_4) ( Addr old_ESP );
429extern VGA_REGPARM(1) void MAC_(new_mem_stack_8) ( Addr old_ESP );
430extern VGA_REGPARM(1) void MAC_(die_mem_stack_8) ( Addr old_ESP );
431extern VGA_REGPARM(1) void MAC_(new_mem_stack_12) ( Addr old_ESP );
432extern VGA_REGPARM(1) void MAC_(die_mem_stack_12) ( Addr old_ESP );
433extern VGA_REGPARM(1) void MAC_(new_mem_stack_16) ( Addr old_ESP );
434extern VGA_REGPARM(1) void MAC_(die_mem_stack_16) ( Addr old_ESP );
435extern VGA_REGPARM(1) void MAC_(new_mem_stack_32) ( Addr old_ESP );
436extern VGA_REGPARM(1) void MAC_(die_mem_stack_32) ( Addr old_ESP );
437extern void MAC_(die_mem_stack) ( Addr a, SizeT len);
438extern void MAC_(new_mem_stack) ( Addr a, SizeT len);
njn9b007f62003-04-07 14:40:25 +0000439
njn51d827b2005-05-09 01:02:08 +0000440extern void* MAC_(malloc) ( ThreadId tid, SizeT n );
441extern void* MAC_(__builtin_new) ( ThreadId tid, SizeT n );
442extern void* MAC_(__builtin_vec_new) ( ThreadId tid, SizeT n );
443extern void* MAC_(memalign) ( ThreadId tid, SizeT align, SizeT n );
444extern void* MAC_(calloc) ( ThreadId tid, SizeT nmemb, SizeT size1 );
445extern void MAC_(free) ( ThreadId tid, void* p );
446extern void MAC_(__builtin_delete) ( ThreadId tid, void* p );
447extern void MAC_(__builtin_vec_delete) ( ThreadId tid, void* p );
448extern void* MAC_(realloc) ( ThreadId tid, void* p, SizeT new_size );
njn9b007f62003-04-07 14:40:25 +0000449
450/*------------------------------------------------------------*/
451/*--- Stack pointer adjustment ---*/
452/*------------------------------------------------------------*/
453
454/* Some noble preprocessor abuse, to enable Memcheck and Addrcheck to
njn3e884182003-04-15 13:03:23 +0000455 share this code, but call different functions.
njn9b007f62003-04-07 14:40:25 +0000456
457 Note that this code is executed very frequently and must be highly
458 optimised, which is why I resort to the preprocessor to achieve the
sewardj45d94cc2005-04-20 14:44:11 +0000459 factoring, rather than eg. using function pointers.
njn9b007f62003-04-07 14:40:25 +0000460*/
461
sewardj045a4052005-04-23 22:42:27 +0000462#define SP_UPDATE_HANDLERS(ALIGNED4_NEW, ALIGNED4_DIE, \
463 ALIGNED8_NEW, ALIGNED8_DIE, \
464 UNALIGNED_NEW, UNALIGNED_DIE) \
465 \
466void VGA_REGPARM(1) MAC_(new_mem_stack_4)(Addr new_SP) \
467{ \
468 PROF_EVENT(110, "new_mem_stack_4"); \
469 if (VG_IS_4_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000470 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SZB + new_SP ); \
sewardj045a4052005-04-23 22:42:27 +0000471 } else { \
njna60a7c12005-05-08 17:49:37 +0000472 UNALIGNED_NEW ( -VGA_STACK_REDZONE_SZB + new_SP, 4 ); \
sewardj045a4052005-04-23 22:42:27 +0000473 } \
474} \
475 \
476void VGA_REGPARM(1) MAC_(die_mem_stack_4)(Addr new_SP) \
477{ \
478 PROF_EVENT(120, "die_mem_stack_4"); \
479 if (VG_IS_4_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000480 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-4 ); \
sewardj045a4052005-04-23 22:42:27 +0000481 } else { \
njna60a7c12005-05-08 17:49:37 +0000482 UNALIGNED_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-4, 4 ); \
sewardj045a4052005-04-23 22:42:27 +0000483 } \
484} \
485 \
486void VGA_REGPARM(1) MAC_(new_mem_stack_8)(Addr new_SP) \
487{ \
488 PROF_EVENT(111, "new_mem_stack_8"); \
489 if (VG_IS_8_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000490 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SZB + new_SP ); \
sewardj045a4052005-04-23 22:42:27 +0000491 } else if (VG_IS_4_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000492 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SZB + new_SP ); \
493 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SZB + new_SP+4 ); \
sewardj045a4052005-04-23 22:42:27 +0000494 } else { \
njna60a7c12005-05-08 17:49:37 +0000495 UNALIGNED_NEW ( -VGA_STACK_REDZONE_SZB + new_SP, 8 ); \
sewardj045a4052005-04-23 22:42:27 +0000496 } \
497} \
498 \
499void VGA_REGPARM(1) MAC_(die_mem_stack_8)(Addr new_SP) \
500{ \
501 PROF_EVENT(121, "die_mem_stack_8"); \
502 if (VG_IS_8_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000503 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-8 ); \
sewardj045a4052005-04-23 22:42:27 +0000504 } else if (VG_IS_4_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000505 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-8 ); \
506 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-4 ); \
sewardj045a4052005-04-23 22:42:27 +0000507 } else { \
njna60a7c12005-05-08 17:49:37 +0000508 UNALIGNED_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-8, 8 ); \
sewardj045a4052005-04-23 22:42:27 +0000509 } \
510} \
511 \
512void VGA_REGPARM(1) MAC_(new_mem_stack_12)(Addr new_SP) \
513{ \
514 PROF_EVENT(112, "new_mem_stack_12"); \
515 if (VG_IS_8_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000516 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SZB + new_SP ); \
517 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SZB + new_SP+8 ); \
sewardj045a4052005-04-23 22:42:27 +0000518 } else if (VG_IS_4_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000519 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SZB + new_SP ); \
520 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SZB + new_SP+4 ); \
sewardj045a4052005-04-23 22:42:27 +0000521 } else { \
njna60a7c12005-05-08 17:49:37 +0000522 UNALIGNED_NEW ( -VGA_STACK_REDZONE_SZB + new_SP, 12 ); \
sewardj045a4052005-04-23 22:42:27 +0000523 } \
524} \
525 \
526void VGA_REGPARM(1) MAC_(die_mem_stack_12)(Addr new_SP) \
527{ \
528 PROF_EVENT(122, "die_mem_stack_12"); \
529 /* Note the -12 in the test */ \
530 if (VG_IS_8_ALIGNED(new_SP-12)) { \
njna60a7c12005-05-08 17:49:37 +0000531 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-12 ); \
532 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-4 ); \
sewardj045a4052005-04-23 22:42:27 +0000533 } else if (VG_IS_4_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000534 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-12 ); \
535 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-8 ); \
sewardj045a4052005-04-23 22:42:27 +0000536 } else { \
njna60a7c12005-05-08 17:49:37 +0000537 UNALIGNED_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-12, 12 ); \
sewardj045a4052005-04-23 22:42:27 +0000538 } \
539} \
540 \
541void VGA_REGPARM(1) MAC_(new_mem_stack_16)(Addr new_SP) \
542{ \
543 PROF_EVENT(113, "new_mem_stack_16"); \
544 if (VG_IS_8_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000545 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SZB + new_SP ); \
546 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SZB + new_SP+8 ); \
sewardj045a4052005-04-23 22:42:27 +0000547 } else if (VG_IS_4_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000548 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SZB + new_SP ); \
549 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SZB + new_SP+4 ); \
550 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SZB + new_SP+12 ); \
sewardj045a4052005-04-23 22:42:27 +0000551 } else { \
njna60a7c12005-05-08 17:49:37 +0000552 UNALIGNED_NEW ( -VGA_STACK_REDZONE_SZB + new_SP, 16 ); \
sewardj045a4052005-04-23 22:42:27 +0000553 } \
554} \
555 \
556void VGA_REGPARM(1) MAC_(die_mem_stack_16)(Addr new_SP) \
557{ \
558 PROF_EVENT(123, "die_mem_stack_16"); \
559 if (VG_IS_8_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000560 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-16 ); \
561 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-8 ); \
sewardj045a4052005-04-23 22:42:27 +0000562 } else if (VG_IS_4_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000563 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-16 ); \
564 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-12 ); \
565 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-4 ); \
sewardj045a4052005-04-23 22:42:27 +0000566 } else { \
njna60a7c12005-05-08 17:49:37 +0000567 UNALIGNED_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-16, 16 ); \
sewardj045a4052005-04-23 22:42:27 +0000568 } \
569} \
570 \
571void VGA_REGPARM(1) MAC_(new_mem_stack_32)(Addr new_SP) \
572{ \
573 PROF_EVENT(114, "new_mem_stack_32"); \
574 if (VG_IS_8_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000575 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SZB + new_SP ); \
576 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SZB + new_SP+8 ); \
577 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SZB + new_SP+16 ); \
578 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SZB + new_SP+24 ); \
sewardj045a4052005-04-23 22:42:27 +0000579 } else if (VG_IS_4_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000580 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SZB + new_SP ); \
581 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SZB + new_SP+4 ); \
582 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SZB + new_SP+12 ); \
583 ALIGNED8_NEW ( -VGA_STACK_REDZONE_SZB + new_SP+20 ); \
584 ALIGNED4_NEW ( -VGA_STACK_REDZONE_SZB + new_SP+28 ); \
sewardj045a4052005-04-23 22:42:27 +0000585 } else { \
njna60a7c12005-05-08 17:49:37 +0000586 UNALIGNED_NEW ( -VGA_STACK_REDZONE_SZB + new_SP, 32 ); \
sewardj045a4052005-04-23 22:42:27 +0000587 } \
588} \
589 \
590void VGA_REGPARM(1) MAC_(die_mem_stack_32)(Addr new_SP) \
591{ \
592 PROF_EVENT(124, "die_mem_stack_32"); \
593 if (VG_IS_8_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000594 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-32 ); \
595 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-24 ); \
596 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-16 ); \
597 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SZB + new_SP- 8 ); \
sewardj045a4052005-04-23 22:42:27 +0000598 } else if (VG_IS_4_ALIGNED(new_SP)) { \
njna60a7c12005-05-08 17:49:37 +0000599 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-32 ); \
600 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-28 ); \
601 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-20 ); \
602 ALIGNED8_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-12 ); \
603 ALIGNED4_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-4 ); \
sewardj045a4052005-04-23 22:42:27 +0000604 } else { \
njna60a7c12005-05-08 17:49:37 +0000605 UNALIGNED_DIE ( -VGA_STACK_REDZONE_SZB + new_SP-32, 32 ); \
sewardj045a4052005-04-23 22:42:27 +0000606 } \
607} \
608 \
609void MAC_(new_mem_stack) ( Addr a, SizeT len ) \
610{ \
611 PROF_EVENT(115, "new_mem_stack"); \
njna60a7c12005-05-08 17:49:37 +0000612 UNALIGNED_NEW ( -VGA_STACK_REDZONE_SZB + a, len ); \
sewardj045a4052005-04-23 22:42:27 +0000613} \
614 \
615void MAC_(die_mem_stack) ( Addr a, SizeT len ) \
616{ \
617 PROF_EVENT(125, "die_mem_stack"); \
njna60a7c12005-05-08 17:49:37 +0000618 UNALIGNED_DIE ( -VGA_STACK_REDZONE_SZB + a, len ); \
njn9b007f62003-04-07 14:40:25 +0000619}
620
njn43c799e2003-04-08 00:08:52 +0000621#endif /* __MAC_SHARED_H */
njn5c004e42002-11-18 11:04:50 +0000622
623/*--------------------------------------------------------------------*/
njn43c799e2003-04-08 00:08:52 +0000624/*--- end mac_shared.h ---*/
njn5c004e42002-11-18 11:04:50 +0000625/*--------------------------------------------------------------------*/