sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 3 | /*--- For when the client advises Valgrind about memory ---*/ |
| 4 | /*--- permissions. ---*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 5 | /*--- mc_clientreqs.c ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6 | /*--------------------------------------------------------------------*/ |
| 7 | |
| 8 | /* |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 9 | This file is part of MemCheck, a heavyweight Valgrind tool for |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 10 | detecting memory errors. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 11 | |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 12 | Copyright (C) 2000-2004 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 13 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 30 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 33 | #include "mc_include.h" |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 34 | |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 35 | #include "memcheck.h" /* for VG_USERREQ__* */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 36 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 37 | |
| 38 | /*------------------------------------------------------------*/ |
| 39 | /*--- General client block management. ---*/ |
| 40 | /*------------------------------------------------------------*/ |
| 41 | |
| 42 | /* This is managed as an expanding array of client block descriptors. |
| 43 | Indices of live descriptors are issued to the client, so it can ask |
| 44 | to free them later. Therefore we cannot slide live entries down |
| 45 | over dead ones. Instead we must use free/inuse flags and scan for |
| 46 | an empty slot at allocation time. This in turn means allocation is |
| 47 | relatively expensive, so we hope this does not happen too often. |
| 48 | */ |
| 49 | |
| 50 | typedef |
| 51 | enum { CG_NotInUse, CG_NoAccess, CG_Writable, CG_Readable } |
| 52 | CGenBlockKind; |
| 53 | |
| 54 | typedef |
| 55 | struct { |
| 56 | Addr start; |
| 57 | UInt size; |
| 58 | ExeContext* where; |
| 59 | CGenBlockKind kind; |
| 60 | } |
| 61 | CGenBlock; |
| 62 | |
| 63 | /* This subsystem is self-initialising. */ |
| 64 | static UInt vg_cgb_size = 0; |
| 65 | static UInt vg_cgb_used = 0; |
| 66 | static CGenBlock* vg_cgbs = NULL; |
| 67 | |
| 68 | /* Stats for this subsystem. */ |
| 69 | static UInt vg_cgb_used_MAX = 0; /* Max in use. */ |
| 70 | static UInt vg_cgb_allocs = 0; /* Number of allocs. */ |
| 71 | static UInt vg_cgb_discards = 0; /* Number of discards. */ |
| 72 | static UInt vg_cgb_search = 0; /* Number of searches. */ |
| 73 | |
| 74 | |
| 75 | static |
| 76 | Int vg_alloc_client_block ( void ) |
| 77 | { |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 78 | UInt i, sz_new; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 79 | CGenBlock* cgbs_new; |
| 80 | |
| 81 | vg_cgb_allocs++; |
| 82 | |
| 83 | for (i = 0; i < vg_cgb_used; i++) { |
| 84 | vg_cgb_search++; |
| 85 | if (vg_cgbs[i].kind == CG_NotInUse) |
| 86 | return i; |
| 87 | } |
| 88 | |
| 89 | /* Not found. Try to allocate one at the end. */ |
| 90 | if (vg_cgb_used < vg_cgb_size) { |
| 91 | vg_cgb_used++; |
| 92 | return vg_cgb_used-1; |
| 93 | } |
| 94 | |
| 95 | /* Ok, we have to allocate a new one. */ |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 96 | sk_assert(vg_cgb_used == vg_cgb_size); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 97 | sz_new = (vg_cgbs == NULL) ? 10 : (2 * vg_cgb_size); |
| 98 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 99 | cgbs_new = VG_(malloc)( sz_new * sizeof(CGenBlock) ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 100 | for (i = 0; i < vg_cgb_used; i++) |
| 101 | cgbs_new[i] = vg_cgbs[i]; |
| 102 | |
| 103 | if (vg_cgbs != NULL) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 104 | VG_(free)( vg_cgbs ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 105 | vg_cgbs = cgbs_new; |
| 106 | |
| 107 | vg_cgb_size = sz_new; |
| 108 | vg_cgb_used++; |
| 109 | if (vg_cgb_used > vg_cgb_used_MAX) |
| 110 | vg_cgb_used_MAX = vg_cgb_used; |
| 111 | return vg_cgb_used-1; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /*------------------------------------------------------------*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 116 | /*--- Externally visible functions. ---*/ |
| 117 | /*------------------------------------------------------------*/ |
| 118 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 119 | void MC_(show_client_block_stats) ( void ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 120 | { |
| 121 | VG_(message)(Vg_DebugMsg, |
| 122 | "general CBs: %d allocs, %d discards, %d maxinuse, %d search", |
| 123 | vg_cgb_allocs, vg_cgb_discards, vg_cgb_used_MAX, vg_cgb_search |
| 124 | ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 125 | } |
| 126 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 127 | Bool MC_(client_perm_maybe_describe)( Addr a, AddrInfo* ai ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 128 | { |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 129 | UInt i; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 130 | /* VG_(printf)("try to identify %d\n", a); */ |
| 131 | |
sewardj | a81709d | 2002-12-28 12:55:48 +0000 | [diff] [blame] | 132 | /* Perhaps it's a general block ? */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 133 | for (i = 0; i < vg_cgb_used; i++) { |
| 134 | if (vg_cgbs[i].kind == CG_NotInUse) |
| 135 | continue; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 136 | if (VG_(addr_is_in_block)(a, vg_cgbs[i].start, vg_cgbs[i].size)) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 137 | ai->akind = UserG; |
| 138 | ai->blksize = vg_cgbs[i].size; |
| 139 | ai->rwoffset = (Int)(a) - (Int)(vg_cgbs[i].start); |
| 140 | ai->lastchange = vg_cgbs[i].where; |
| 141 | return True; |
| 142 | } |
| 143 | } |
| 144 | return False; |
| 145 | } |
| 146 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 147 | Bool SK_(handle_client_request) ( ThreadId tid, UInt* arg, UInt* ret ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 148 | { |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 149 | Int i; |
| 150 | Bool ok; |
| 151 | Addr bad_addr; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 152 | |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 153 | if (!VG_IS_SKIN_USERREQ('M','C',arg[0]) |
| 154 | && VG_USERREQ__MALLOCLIKE_BLOCK != arg[0] |
| 155 | && VG_USERREQ__FREELIKE_BLOCK != arg[0]) |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 156 | return False; |
| 157 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 158 | switch (arg[0]) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 159 | case VG_USERREQ__CHECK_WRITABLE: /* check writable */ |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 160 | ok = MC_(check_writable) ( arg[1], arg[2], &bad_addr ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 161 | if (!ok) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 162 | MC_(record_user_error) ( tid, bad_addr, True ); |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 163 | *ret = ok ? (UInt)NULL : bad_addr; |
| 164 | break; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 165 | |
| 166 | case VG_USERREQ__CHECK_READABLE: /* check readable */ |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 167 | ok = MC_(check_readable) ( arg[1], arg[2], &bad_addr ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 168 | if (!ok) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 169 | MC_(record_user_error) ( tid, bad_addr, False ); |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 170 | *ret = ok ? (UInt)NULL : bad_addr; |
| 171 | break; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 172 | |
| 173 | case VG_USERREQ__DO_LEAK_CHECK: |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 174 | MC_(detect_memory_leaks)(); |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 175 | *ret = 0; /* return value is meaningless */ |
| 176 | break; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 177 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 178 | case VG_USERREQ__MAKE_NOACCESS: /* make no access */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 179 | i = vg_alloc_client_block(); |
| 180 | /* VG_(printf)("allocated %d %p\n", i, vg_cgbs); */ |
| 181 | vg_cgbs[i].kind = CG_NoAccess; |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 182 | vg_cgbs[i].start = arg[1]; |
| 183 | vg_cgbs[i].size = arg[2]; |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 184 | vg_cgbs[i].where = VG_(get_ExeContext) ( tid ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 185 | MC_(make_noaccess) ( arg[1], arg[2] ); |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 186 | *ret = i; |
| 187 | break; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 188 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 189 | case VG_USERREQ__MAKE_WRITABLE: /* make writable */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 190 | i = vg_alloc_client_block(); |
| 191 | vg_cgbs[i].kind = CG_Writable; |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 192 | vg_cgbs[i].start = arg[1]; |
| 193 | vg_cgbs[i].size = arg[2]; |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 194 | vg_cgbs[i].where = VG_(get_ExeContext) ( tid ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 195 | MC_(make_writable) ( arg[1], arg[2] ); |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 196 | *ret = i; |
| 197 | break; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 198 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 199 | case VG_USERREQ__MAKE_READABLE: /* make readable */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 200 | i = vg_alloc_client_block(); |
| 201 | vg_cgbs[i].kind = CG_Readable; |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 202 | vg_cgbs[i].start = arg[1]; |
| 203 | vg_cgbs[i].size = arg[2]; |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 204 | vg_cgbs[i].where = VG_(get_ExeContext) ( tid ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 205 | MC_(make_readable) ( arg[1], arg[2] ); |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 206 | *ret = i; |
| 207 | break; |
| 208 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 209 | case VG_USERREQ__DISCARD: /* discard */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 210 | if (vg_cgbs == NULL |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 211 | || arg[2] >= vg_cgb_used || vg_cgbs[arg[2]].kind == CG_NotInUse) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 212 | return 1; |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 213 | sk_assert(arg[2] >= 0 && arg[2] < vg_cgb_used); |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 214 | vg_cgbs[arg[2]].kind = CG_NotInUse; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 215 | vg_cgb_discards++; |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 216 | *ret = 0; |
| 217 | break; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 218 | |
sewardj | ee07084 | 2003-07-05 17:53:55 +0000 | [diff] [blame] | 219 | case VG_USERREQ__GET_VBITS: |
| 220 | /* Returns: 1 == OK, 2 == alignment error, 3 == addressing |
| 221 | error. */ |
| 222 | /* VG_(printf)("get_vbits %p %p %d\n", arg[1], arg[2], arg[3] ); */ |
| 223 | *ret = MC_(get_or_set_vbits_for_client) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 224 | ( tid, arg[1], arg[2], arg[3], False /* get them */ ); |
sewardj | ee07084 | 2003-07-05 17:53:55 +0000 | [diff] [blame] | 225 | break; |
| 226 | |
| 227 | case VG_USERREQ__SET_VBITS: |
| 228 | /* Returns: 1 == OK, 2 == alignment error, 3 == addressing |
| 229 | error. */ |
| 230 | /* VG_(printf)("set_vbits %p %p %d\n", arg[1], arg[2], arg[3] ); */ |
| 231 | *ret = MC_(get_or_set_vbits_for_client) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 232 | ( tid, arg[1], arg[2], arg[3], True /* set them */ ); |
sewardj | ee07084 | 2003-07-05 17:53:55 +0000 | [diff] [blame] | 233 | break; |
| 234 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 235 | case _VG_USERREQ__MEMCHECK_GET_RECORD_OVERLAP: |
| 236 | *ret = (Addr)MAC_(record_overlap_error); |
| 237 | break; |
| 238 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 239 | default: |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 240 | if (MAC_(handle_common_client_requests)(tid, arg, ret )) { |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 241 | return True; |
| 242 | } else { |
| 243 | VG_(message)(Vg_UserMsg, |
| 244 | "Warning: unknown memcheck client request code %d", |
| 245 | arg[0]); |
| 246 | return False; |
| 247 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 248 | } |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 249 | return True; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
| 253 | /*--------------------------------------------------------------------*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 254 | /*--- end mc_clientreqs.c ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 255 | /*--------------------------------------------------------------------*/ |