blob: 58099d3782a5cb7e794acdf1fa583d498621f5e2 [file] [log] [blame]
sewardj55f9d1a2005-04-25 11:11:44 +00001
2/*--------------------------------------------------------------------*/
njn43b9a8a2005-05-10 04:37:01 +00003/*--- The address space manager. pub_core_aspacemgr.h ---*/
sewardj55f9d1a2005-04-25 11:11:44 +00004/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
sewardj0f157dd2013-10-18 14:27:36 +000010 Copyright (C) 2000-2013 Julian Seward
sewardj55f9d1a2005-04-25 11:11:44 +000011 jseward@acm.org
12
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
30
31#ifndef __PUB_CORE_ASPACEMGR_H
32#define __PUB_CORE_ASPACEMGR_H
33
34//--------------------------------------------------------------------
35// PURPOSE: This module deals with management of the entire process
36// address space. Almost everything depends upon it, including dynamic
37// memory management. Hence this module is almost completely
38// standalone; the only module it uses is m_debuglog. DO NOT CHANGE
39// THIS.
40//--------------------------------------------------------------------
41
njn4802b382005-06-11 04:58:29 +000042#include "pub_tool_aspacemgr.h"
sewardj55f9d1a2005-04-25 11:11:44 +000043
sewardj45f4e7c2005-09-27 19:20:21 +000044//--------------------------------------------------------------
45// Definition of address-space segments
46
47/* types SegKind, ShrinkMode and NSegment are described in
48 the tool-visible header file, not here. */
49
50
51//--------------------------------------------------------------
52// Initialisation
53
54/* Initialise the address space manager, setting up the initial
55 segment list, and reading /proc/self/maps into it. This must
56 be called before any other function.
57
58 Takes a pointer to the SP at the time V gained control. This is
59 taken to be the highest usable address (more or less). Based on
60 that (and general consultation of tea leaves, etc) return a
philippe38a74d22014-08-29 22:53:19 +000061 suggested end address (highest addressable byte) for the client's stack. */
sewardj45f4e7c2005-09-27 19:20:21 +000062extern Addr VG_(am_startup) ( Addr sp_at_startup );
63
64
65//--------------------------------------------------------------
66// Querying current status
67
sewardj45f4e7c2005-09-27 19:20:21 +000068
69/* Find the next segment along from 'here', if it is a file/anon/resvn
70 segment. */
florian3e798632012-11-24 19:41:54 +000071extern NSegment const* VG_(am_next_nsegment) ( const NSegment* here,
72 Bool fwds );
sewardj45f4e7c2005-09-27 19:20:21 +000073
florian67d46032015-01-23 19:55:31 +000074/* Is the area [start .. start+len-1] validly accessible by
75 valgrind with at least the permissions 'prot' ? To find out
76 simply if said area merely belongs to valgrind, pass
sewardj45f4e7c2005-09-27 19:20:21 +000077 VKI_PROT_NONE as 'prot'. Will return False if any part of the
florian67d46032015-01-23 19:55:31 +000078 area does not belong to valgrind or does not have at least
sewardj45f4e7c2005-09-27 19:20:21 +000079 the stated permissions. */
philippeadfff762014-04-20 22:10:24 +000080extern Bool VG_(am_is_valid_for_valgrind)
81 ( Addr start, SizeT len, UInt prot );
82
sewardj45f4e7c2005-09-27 19:20:21 +000083/* Variant of VG_(am_is_valid_for_client) which allows free areas to
84 be consider part of the client's addressable space. It also
85 considers reservations to be allowable, since from the client's
86 point of view they don't exist. */
87extern Bool VG_(am_is_valid_for_client_or_free_or_resvn)
88 ( Addr start, SizeT len, UInt prot );
89
90/* Trivial fn: return the total amount of space in anonymous mappings,
91 both for V and the client. Is used for printing stats in
92 out-of-memory messages. */
93extern ULong VG_(am_get_anonsize_total)( void );
94
95/* Show the segment array on the debug log, at given loglevel. */
floriandbb35842012-10-27 18:39:11 +000096extern void VG_(am_show_nsegments) ( Int logLevel, const HChar* who );
sewardj45f4e7c2005-09-27 19:20:21 +000097
sewardj45f4e7c2005-09-27 19:20:21 +000098/* VG_(am_get_segment_starts) is also part of this section, but its
99 prototype is tool-visible, hence not in this header file. */
100
101/* Sanity check: check that Valgrind and the kernel agree on the
102 address space layout. Prints offending segments and call point if
103 a discrepancy is detected, but does not abort the system. Returned
104 Bool is False if a discrepancy was found. */
105
106extern Bool VG_(am_do_sync_check) ( const HChar* fn,
107 const HChar* file, Int line );
108
sewardj45f4e7c2005-09-27 19:20:21 +0000109//--------------------------------------------------------------
110// Functions pertaining to the central query-notify mechanism
111// used to handle mmap/munmap/mprotect resulting from client
112// syscalls.
113
114/* Describes a request for VG_(am_get_advisory). */
115typedef
116 struct {
117 enum { MFixed, MHint, MAny } rkind;
118 Addr start;
119 Addr len;
120 }
121 MapRequest;
122
123/* Query aspacem to ask where a mapping should go. On success, the
124 advised placement is returned, and *ok is set to True. On failure,
125 zero is returned and *ok is set to False. Note that *ok must be
126 consulted by the caller to establish success or failure; that
127 cannot be established reliably from the returned value. If *ok is
128 set to False, it means aspacem has vetoed the mapping, and so the
129 caller should not proceed with it. */
130extern Addr VG_(am_get_advisory)
florian32971242014-10-23 17:47:15 +0000131 ( const MapRequest* req, Bool forClient, /*OUT*/Bool* ok );
sewardj45f4e7c2005-09-27 19:20:21 +0000132
133/* Convenience wrapper for VG_(am_get_advisory) for client floating or
134 fixed requests. If start is zero, a floating request is issued; if
135 nonzero, a fixed request at that address is issued. Same comments
136 about return values apply. */
137extern Addr VG_(am_get_advisory_client_simple)
138 ( Addr start, SizeT len, /*OUT*/Bool* ok );
139
philippe15e301e2011-12-22 13:25:58 +0000140/* Returns True if [start, start + len - 1] is covered by a single
141 free segment, otherwise returns False.
142 This allows to check the following case:
143 VG_(am_get_advisory_client_simple) (first arg == 0, meaning
144 this-or-nothing) is too lenient, and may allow us to trash
145 the next segment along. So make very sure that the proposed
146 new area really is free. This is perhaps overly
147 conservative, but it fixes #129866. */
148extern Bool VG_(am_covered_by_single_free_segment)
149 ( Addr start, SizeT len);
150
sewardj45f4e7c2005-09-27 19:20:21 +0000151/* Notifies aspacem that the client completed an mmap successfully.
152 The segment array is updated accordingly. If the returned Bool is
153 True, the caller should immediately discard translations from the
154 specified address range. */
155extern Bool VG_(am_notify_client_mmap)
sewardj274461d2005-10-02 17:01:41 +0000156 ( Addr a, SizeT len, UInt prot, UInt flags, Int fd, Off64T offset );
sewardj45f4e7c2005-09-27 19:20:21 +0000157
tom1340c352005-10-04 15:59:54 +0000158/* Notifies aspacem that the client completed a shmat successfully.
159 The segment array is updated accordingly. If the returned Bool is
160 True, the caller should immediately discard translations from the
161 specified address range. */
162extern Bool VG_(am_notify_client_shmat)( Addr a, SizeT len, UInt prot );
163
sewardj45f4e7c2005-09-27 19:20:21 +0000164/* Notifies aspacem that an mprotect was completed successfully. The
165 segment array is updated accordingly. Note, as with
166 VG_(am_notify_munmap), it is not the job of this function to reject
167 stupid mprotects, for example the client doing mprotect of
168 non-client areas. Such requests should be intercepted earlier, by
169 the syscall wrapper for mprotect. This function merely records
170 whatever it is told. If the returned Bool is True, the caller
171 should immediately discard translations from the specified address
172 range. */
173extern Bool VG_(am_notify_mprotect)( Addr start, SizeT len, UInt prot );
174
175/* Notifies aspacem that an munmap completed successfully. The
176 segment array is updated accordingly. As with
177 VG_(am_notify_munmap), we merely record the given info, and don't
178 check it for sensibleness. If the returned Bool is True, the
179 caller should immediately discard translations from the specified
180 address range. */
181extern Bool VG_(am_notify_munmap)( Addr start, SizeT len );
182
sewardj45f4e7c2005-09-27 19:20:21 +0000183/* Hand a raw mmap to the kernel, without aspacem updating the segment
184 array. THIS FUNCTION IS DANGEROUS -- it will cause aspacem's view
185 of the address space to diverge from that of the kernel. DO NOT
186 USE IT UNLESS YOU UNDERSTAND the request-notify model used by
187 aspacem. In short, DO NOT USE THIS FUNCTION. */
188extern SysRes VG_(am_do_mmap_NO_NOTIFY)
njn9506f0d2009-05-20 03:20:05 +0000189 ( Addr start, SizeT length, UInt prot, UInt flags, Int fd, Off64T offset);
sewardj45f4e7c2005-09-27 19:20:21 +0000190
191
192//--------------------------------------------------------------
193// Dealing with mappings which do not arise directly from the
194// simulation of the client. These are typically used for
195// loading the client and building its stack/data segment, before
196// execution begins. Also for V's own administrative use.
197
198/* --- --- --- map, unmap, protect --- --- --- */
199
200/* Map a file at a fixed address for the client, and update the
201 segment array accordingly. */
202extern SysRes VG_(am_mmap_file_fixed_client)
sewardj274461d2005-10-02 17:01:41 +0000203 ( Addr start, SizeT length, UInt prot, Int fd, Off64T offset );
njnf76d27a2009-05-28 01:53:07 +0000204extern SysRes VG_(am_mmap_named_file_fixed_client)
205 ( Addr start, SizeT length, UInt prot, Int fd, Off64T offset, const HChar *name );
sewardj45f4e7c2005-09-27 19:20:21 +0000206
207/* Map anonymously at a fixed address for the client, and update
208 the segment array accordingly. */
209extern SysRes VG_(am_mmap_anon_fixed_client)
210 ( Addr start, SizeT length, UInt prot );
211
sewardjfa2a2462006-10-17 01:30:07 +0000212
sewardj45f4e7c2005-09-27 19:20:21 +0000213/* Map anonymously at an unconstrained address for the client, and
214 update the segment array accordingly. */
215extern SysRes VG_(am_mmap_anon_float_client) ( SizeT length, Int prot );
216
217/* Map anonymously at an unconstrained address for V, and update the
218 segment array accordingly. This is fundamentally how V allocates
219 itself more address space when needed. */
220extern SysRes VG_(am_mmap_anon_float_valgrind)( SizeT cszB );
221
sewardj3b290482011-05-06 21:02:55 +0000222/* Map privately a file at an unconstrained address for V, and update the
sewardj45f4e7c2005-09-27 19:20:21 +0000223 segment array accordingly. This is used by V for transiently
224 mapping in object files to read their debug info. */
225extern SysRes VG_(am_mmap_file_float_valgrind)
sewardj274461d2005-10-02 17:01:41 +0000226 ( SizeT length, UInt prot, Int fd, Off64T offset );
sewardj45f4e7c2005-09-27 19:20:21 +0000227
sewardj3b290482011-05-06 21:02:55 +0000228/* Map shared a file at an unconstrained address for V, and update the
229 segment array accordingly. This is used by V for communicating
230 with vgdb. */
231extern SysRes VG_(am_shared_mmap_file_float_valgrind)
232 ( SizeT length, UInt prot, Int fd, Off64T offset );
233
florian2fa66ce2015-03-07 23:01:14 +0000234/* Convenience wrapper around VG_(am_mmap_anon_float_client) which also
235 marks the segment as containing the client heap. */
236extern SysRes VG_(am_mmap_client_heap) ( SizeT length, Int prot );
237
sewardj45f4e7c2005-09-27 19:20:21 +0000238/* Unmap the given address range and update the segment array
239 accordingly. This fails if the range isn't valid for the client.
240 If *need_discard is True after a successful return, the caller
241 should immediately discard translations from the specified address
242 range. */
243extern SysRes VG_(am_munmap_client)( /*OUT*/Bool* need_discard,
244 Addr start, SizeT length );
245
sewardj45f4e7c2005-09-27 19:20:21 +0000246/* Let (start,len) denote an area within a single Valgrind-owned
247 segment (anon or file). Change the ownership of [start, start+len)
248 to the client instead. Fails if (start,len) does not denote a
249 suitable segment. */
250extern Bool VG_(am_change_ownership_v_to_c)( Addr start, SizeT len );
251
florian2fa66ce2015-03-07 23:01:14 +0000252/* Set the 'hasT' bit on the segment containing ADDR indicating that
253 translations have or may have been taken from this segment. ADDR is
254 expected to belong to a client segment. */
255extern void VG_(am_set_segment_hasT)( Addr addr );
sewardjfa2a2462006-10-17 01:30:07 +0000256
sewardj45f4e7c2005-09-27 19:20:21 +0000257/* --- --- --- reservations --- --- --- */
258
259/* Create a reservation from START .. START+LENGTH-1, with the given
260 ShrinkMode. When checking whether the reservation can be created,
261 also ensure that at least abs(EXTRA) extra free bytes will remain
262 above (> 0) or below (< 0) the reservation.
263
264 The reservation will only be created if it, plus the extra-zone,
265 falls entirely within a single free segment. The returned Bool
266 indicates whether the creation succeeded. */
267extern Bool VG_(am_create_reservation)
268 ( Addr start, SizeT length, ShrinkMode smode, SSizeT extra );
269
florian888b8152015-02-26 16:07:12 +0000270/* ADDR is the start address of an anonymous client mapping. This fn extends
271 the mapping by DELTA bytes, taking the space from a reservation section
sewardj45f4e7c2005-09-27 19:20:21 +0000272 which must be adjacent. If DELTA is positive, the segment is
273 extended forwards in the address space, and the reservation must be
274 the next one along. If DELTA is negative, the segment is extended
275 backwards in the address space and the reservation must be the
sewardj6684d2a2005-09-28 01:46:31 +0000276 previous one. DELTA must be page aligned. abs(DELTA) must not
277 exceed the size of the reservation segment minus one page, that is,
278 the reservation segment after the operation must be at least one
florian888b8152015-02-26 16:07:12 +0000279 page long. The function returns a pointer to the resized segment. */
280extern const NSegment *VG_(am_extend_into_adjacent_reservation_client)
florian15fa8a22015-03-03 14:56:17 +0000281 ( Addr addr, SSizeT delta, /*OUT*/Bool *overflow );
sewardj45f4e7c2005-09-27 19:20:21 +0000282
283/* --- --- --- resizing/move a mapping --- --- --- */
284
floriandd7318b2015-02-25 10:06:06 +0000285/* This function grows a client mapping in place into an adjacent free segment.
286 ADDR is the client mapping's start address and DELTA, which must be page
287 aligned, is the growth amount. The function returns a pointer to the
288 resized segment. The function is used in support of mremap. */
289extern const NSegment *VG_(am_extend_map_client)( Addr addr, SizeT delta );
sewardj45f4e7c2005-09-27 19:20:21 +0000290
291/* Remap the old address range to the new address range. Fails if any
292 parameter is not page aligned, if the either size is zero, if any
293 wraparound is implied, if the old address range does not fall
294 entirely within a single segment, if the new address range overlaps
295 with the old one, or if the old address range is not a valid client
296 mapping. If *need_discard is True after a successful return, the
297 caller should immediately discard translations from both specified
298 address ranges. */
299extern Bool VG_(am_relocate_nooverlap_client)( /*OUT*/Bool* need_discard,
300 Addr old_addr, SizeT old_len,
301 Addr new_addr, SizeT new_len );
302
303//--------------------------------------------------------------
304// Valgrind (non-client) thread stacks. V itself runs on such
305// stacks. The address space manager provides and suitably
306// protects such stacks.
307
carllcae0cc22014-08-07 23:17:29 +0000308#if defined(VGP_ppc32_linux) \
309 || defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux) \
sewardjf0c12502014-01-12 12:54:00 +0000310 || defined(VGP_mips32_linux) || defined(VGP_mips64_linux) \
311 || defined(VGP_arm64_linux)
sewardje66f2e02006-12-30 17:45:08 +0000312# define VG_STACK_GUARD_SZB 65536 // 1 or 16 pages
sewardj9e0a6b82010-07-21 09:49:27 +0000313# define VG_STACK_ACTIVE_SZB (4096 * 256) // 1Mb
sewardje66f2e02006-12-30 17:45:08 +0000314#else
315# define VG_STACK_GUARD_SZB 8192 // 2 pages
sewardj9e0a6b82010-07-21 09:49:27 +0000316# define VG_STACK_ACTIVE_SZB (4096 * 256) // 1Mb
sewardje66f2e02006-12-30 17:45:08 +0000317#endif
sewardj45f4e7c2005-09-27 19:20:21 +0000318
319typedef
320 struct {
321 HChar bytes[VG_STACK_GUARD_SZB
322 + VG_STACK_ACTIVE_SZB
323 + VG_STACK_GUARD_SZB];
324 }
325 VgStack;
326
327
philippe17e76ec2014-04-20 19:50:13 +0000328/* Allocate and initialise a VgStack (anonymous valgrind space).
sewardj45f4e7c2005-09-27 19:20:21 +0000329 Protect the stack active area and the guard areas appropriately.
330 Returns NULL on failure, else the address of the bottom of the
331 stack. On success, also sets *initial_sp to what the stack pointer
332 should be set to. */
333
334extern VgStack* VG_(am_alloc_VgStack)( /*OUT*/Addr* initial_sp );
335
sewardj46dbd3f2010-09-08 08:30:31 +0000336/* Figure out how many bytes of the stack's active area have not been
337 used. Used for estimating if we are close to overflowing it. If
338 the free area is larger than 'limit', just return 'limit'. */
florian32971242014-10-23 17:47:15 +0000339extern SizeT VG_(am_get_VgStack_unused_szB)( const VgStack* stack,
340 SizeT limit );
sewardj45f4e7c2005-09-27 19:20:21 +0000341
njnf76d27a2009-05-28 01:53:07 +0000342// DDD: this is ugly
343#if defined(VGO_darwin)
344typedef
345 struct {
346 Bool is_added; // Added or removed seg?
347 Addr start;
348 SizeT end;
349 UInt prot; // Not used for removed segs.
350 Off64T offset; // Not used for removed segs.
351 }
352 ChangedSeg;
353
njnfd1b4612009-06-24 08:32:42 +0000354extern Bool VG_(get_changed_segments)(
njnf76d27a2009-05-28 01:53:07 +0000355 const HChar* when, const HChar* where, /*OUT*/ChangedSeg* css,
356 Int css_size, /*OUT*/Int* css_used);
357#endif
njne3f06352005-06-01 03:48:33 +0000358
sewardj55f9d1a2005-04-25 11:11:44 +0000359#endif // __PUB_CORE_ASPACEMGR_H
360
361/*--------------------------------------------------------------------*/
njnaf839f52005-06-23 03:27:57 +0000362/*--- end ---*/
sewardj55f9d1a2005-04-25 11:11:44 +0000363/*--------------------------------------------------------------------*/