blob: 148de8225138126ade4b508f32150291c9af7d86 [file] [log] [blame]
njn5c004e42002-11-18 11:04:50 +00001
2/*--------------------------------------------------------------------*/
3/*--- Code that is shared between MemCheck and AddrCheck. ---*/
4/*--- mc_common.h ---*/
5/*--------------------------------------------------------------------*/
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
12 Copyright (C) 2000-2002 Julian Seward
13 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#ifndef __MC_COMMON_H
34#define __MC_COMMON_H
35
36#include "vg_skin.h"
37#include "mc_constants.h"
38
39/* The classification of a faulting address. */
40typedef
41 enum { Undescribed, /* as-yet unclassified */
42 Stack,
43 Unknown, /* classification yielded nothing useful */
44 Freed, Mallocd,
45 UserG, UserS /* MemCheck only */
46 }
47 AddrKind;
48
49/* Records info about a faulting address. */
50typedef
51 struct {
52 /* ALL */
53 AddrKind akind;
54 /* Freed, Mallocd */
55 Int blksize;
56 /* Freed, Mallocd */
57 Int rwoffset;
58 /* Freed, Mallocd */
59 ExeContext* lastchange;
60 /* Stack */
61 ThreadId stack_tid;
62 /* True if is just-below %esp -- could be a gcc bug. */
63 Bool maybe_gcc;
64 }
65 AddrInfo;
66
67typedef
68 enum {
69 /* Bad syscall params */
70 ParamSupp,
71 /* Memory errors in core (pthread ops, signal handling) */
72 CoreMemSupp,
73 /* Use of invalid values of given size (MemCheck only) */
74 Value0Supp, Value1Supp, Value2Supp, Value4Supp, Value8Supp,
75 /* Invalid read/write attempt at given size */
76 Addr1Supp, Addr2Supp, Addr4Supp, Addr8Supp,
77 /* Invalid or mismatching free */
sewardj99aac972002-12-26 01:53:45 +000078 FreeSupp,
79 /* Something to be suppressed in a leak check. */
80 LeakSupp
njn5c004e42002-11-18 11:04:50 +000081 }
82 MemCheckSuppKind;
83
84/* What kind of error it is. */
85typedef
86 enum { ValueErr, /* Memcheck only */
87 CoreMemErr,
88 AddrErr,
89 ParamErr, UserErr, /* behaves like an anonymous ParamErr */
90 FreeErr, FreeMismatchErr
91 }
92 MemCheckErrorKind;
93
94/* What kind of memory access is involved in the error? */
95typedef
96 enum { ReadAxs, WriteAxs, ExecAxs }
97 AxsKind;
98
99/* Extra context for memory errors */
100typedef
101 struct {
102 /* AddrErr */
103 AxsKind axskind;
104 /* AddrErr, ValueErr */
105 Int size;
106 /* AddrErr, FreeErr, FreeMismatchErr, ParamErr, UserErr */
107 AddrInfo addrinfo;
108 /* ParamErr, UserErr, CoreMemErr */
109 Bool isWrite;
110 }
111 MemCheckError;
112
113
114#ifdef VG_PROFILE_MEMORY
115
116#define PROF_EVENT(ev) \
117 do { sk_assert((ev) >= 0 && (ev) < N_PROF_EVENTS); \
118 MC_(event_ctr)[ev]++; \
119 } while (False);
120
121#else
122
123#define PROF_EVENT(ev) /* */
124
125#endif /* VG_PROFILE_MEMORY */
126
127#define IS_DISTINGUISHED_SM(smap) \
128 ((smap) == &distinguished_secondary_map)
129
130#define ENSURE_MAPPABLE(addr,caller) \
131 do { \
132 if (IS_DISTINGUISHED_SM(primary_map[(addr) >> 16])) { \
133 primary_map[(addr) >> 16] = alloc_secondary_map(caller); \
134 /* VG_(printf)("new 2map because of %p\n", addr); */ \
135 } \
136 } while(0)
137
138#define BITARR_SET(aaa_p,iii_p) \
139 do { \
140 UInt iii = (UInt)iii_p; \
141 UChar* aaa = (UChar*)aaa_p; \
142 aaa[iii >> 3] |= (1 << (iii & 7)); \
143 } while (0)
144
145#define BITARR_CLEAR(aaa_p,iii_p) \
146 do { \
147 UInt iii = (UInt)iii_p; \
148 UChar* aaa = (UChar*)aaa_p; \
149 aaa[iii >> 3] &= ~(1 << (iii & 7)); \
150 } while (0)
151
152#define BITARR_TEST(aaa_p,iii_p) \
153 (0 != (((UChar*)aaa_p)[ ((UInt)iii_p) >> 3 ] \
154 & (1 << (((UInt)iii_p) & 7)))) \
155
156
157#define VGM_BIT_VALID 0
158#define VGM_BIT_INVALID 1
159
160#define VGM_NIBBLE_VALID 0
161#define VGM_NIBBLE_INVALID 0xF
162
163#define VGM_BYTE_VALID 0
164#define VGM_BYTE_INVALID 0xFF
165
166#define VGM_WORD_VALID 0
167#define VGM_WORD_INVALID 0xFFFFFFFF
168
169#define VGM_EFLAGS_VALID 0xFFFFFFFE
170#define VGM_EFLAGS_INVALID 0xFFFFFFFF /* not used */
171
172/*------------------------------------------------------------*/
173/*--- Command line options + defaults ---*/
174/*------------------------------------------------------------*/
175
176/* Most are shared between MemCheck and AddrCheck, the last two are
177 MemCheck only (but here anyway for simplicity) */
178
179/* Allow loads from partially-valid addresses? default: YES */
180extern Bool MC_(clo_partial_loads_ok);
181
182/* Max volume of the freed blocks queue. */
183extern Int MC_(clo_freelist_vol);
184
185/* Do leak check at exit? default: NO */
186extern Bool MC_(clo_leak_check);
187
188/* How closely should we compare ExeContexts in leak records? default: 2 */
189extern VgRes MC_(clo_leak_resolution);
190
191/* In leak check, show reachable-but-not-freed blocks? default: NO */
192extern Bool MC_(clo_show_reachable);
193
194/* Assume accesses immediately below %esp are due to gcc-2.96 bugs.
195 * default: NO*/
196extern Bool MC_(clo_workaround_gcc296_bugs);
197
198/* DEBUG: clean up instrumented code? default: YES */
199extern Bool MC_(clo_cleanup);
200
njn5c004e42002-11-18 11:04:50 +0000201/* When instrumenting, omit some checks if tell-tale literals for
202 inlined strlen() are visible in the basic block. default: YES */
203extern Bool MC_(clo_avoid_strlen_errors);
204
njn5c004e42002-11-18 11:04:50 +0000205extern Bool MC_(process_common_cmd_line_option)(Char* arg);
206
sewardj99aac972002-12-26 01:53:45 +0000207
njn5c004e42002-11-18 11:04:50 +0000208/*------------------------------------------------------------*/
209/*--- Functions ---*/
210/*------------------------------------------------------------*/
211
212extern void MC_(set_where) ( ShadowChunk* sc, ExeContext* ec );
213extern ExeContext *MC_(get_where) ( ShadowChunk* sc );
214
215extern void MC_(pp_AddrInfo) ( Addr a, AddrInfo* ai );
216
217extern void MC_(clear_MemCheckError) ( MemCheckError* err_extra );
218
219extern void MC_(record_address_error) ( Addr a, Int size, Bool isWrite );
220extern void MC_(record_core_mem_error) ( ThreadState* tst, Bool isWrite,
221 Char* s );
222extern void MC_(record_param_error) ( ThreadState* tst, Addr a,
223 Bool isWriteLack, Char* msg );
224extern void MC_(record_jump_error) ( ThreadState* tst, Addr a );
225extern void MC_(record_free_error) ( ThreadState* tst, Addr a );
226extern void MC_(record_freemismatch_error)( ThreadState* tst, Addr a );
227
228extern void MC_(init_prof_mem) ( void );
229extern void MC_(done_prof_mem) ( void );
230
231extern Int MC_(count_freelist) ( void ) __attribute__ ((unused));
232extern void MC_(freelist_sanity) ( void ) __attribute__ ((unused));
233extern ShadowChunk* MC_(any_matching_freed_ShadowChunks)
234 ( Bool (*p)(ShadowChunk*) );
235
236#endif /* __MC_COMMON_H */
237
238/*--------------------------------------------------------------------*/
239/*--- end mc_common.h ---*/
240/*--------------------------------------------------------------------*/