blob: 90646595ac036fcff66f6937b03e6b86a17089f5 [file] [log] [blame]
njn945ed2e2005-06-24 03:28:30 +00001
2/*--------------------------------------------------------------------*/
3/*--- Stack management. m_stacks.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
sewardj9ebd6e02007-01-08 06:01:59 +000010 Copyright (C) 2000-2007 Julian Seward
njn945ed2e2005-06-24 03:28:30 +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#include "pub_core_basics.h"
tomde3cf132005-11-10 14:24:08 +000032#include "pub_core_debuglog.h"
njn945ed2e2005-06-24 03:28:30 +000033#include "pub_core_libcprint.h"
34#include "pub_core_mallocfree.h"
35#include "pub_core_options.h"
36#include "pub_core_stacks.h"
37#include "pub_core_tooliface.h"
38
39/*
40 The stack
41 ~~~~~~~~~
42 The stack's segment seems to be dynamically extended downwards by
43 the kernel as the stack pointer moves down. Initially, a 1-page
44 (4k) stack is allocated. When SP moves below that for the first
45 time, presumably a page fault occurs. The kernel detects that the
46 faulting address is in the range from SP - VG_STACK_REDZONE_SZB
47 upwards to the current valid stack. It then extends the stack
48 segment downwards for enough to cover the faulting address, and
49 resumes the process (invisibly). The process is unaware of any of
50 this.
51
52 That means that Valgrind can't spot when the stack segment is being
53 extended. Fortunately, we want to precisely and continuously
54 update stack permissions around SP, so we need to spot all writes
55 to SP anyway.
56
57 The deal is: when SP is assigned a lower value, the stack is being
58 extended. Create suitably-permissioned pages to fill in any holes
59 between the old stack ptr and this one, if necessary. Then mark
60 all bytes in the area just "uncovered" by this SP change as
61 write-only.
62
63 When SP goes back up, mark the area receded over as unreadable and
64 unwritable.
65
66 Just to record the SP boundary conditions somewhere convenient:
67 SP - VG_STACK_REDZONE_SZB always points to the lowest live byte in
68 the stack. All addresses below SP - VG_STACK_REDZONE_SZB are not
69 live; those at and above it are.
70
71 We do not concern ourselves here with the VG_STACK_REDZONE_SZB
72 bias; that is handled by new_mem_stack/die_mem_stack.
73*/
74
75/*
76 * This structure holds information about the start and end addresses of
77 * registered stacks. There's always at least one stack registered:
78 * the main process stack. It will be the first stack registered and
79 * so will have a stack id of 0. The user does not need to register
80 * this stack: Valgrind does it automatically right before it starts
81 * running the client. No other stacks are automatically registered by
82 * Valgrind, however.
83 */
84typedef struct _Stack {
85 UWord id;
86 Addr start;
87 Addr end;
88 struct _Stack *next;
89} Stack;
90
91static Stack *stacks;
92static UWord next_id; /* Next id we hand out to a newly registered stack */
93
94/*
95 * These are the id, start and end values of the current stack. If the
96 * stack pointer falls outside the range of the current stack, we search
97 * the stacks list above for a matching stack.
98 */
tomde3cf132005-11-10 14:24:08 +000099static Stack *current_stack;
njn945ed2e2005-06-24 03:28:30 +0000100
101/* Find what stack an address falls into. */
njn4e72d202005-08-14 04:12:40 +0000102static Stack* find_stack_by_addr(Addr sp)
njn945ed2e2005-06-24 03:28:30 +0000103{
104 Stack *i = stacks;
njn4e72d202005-08-14 04:12:40 +0000105 while (i) {
106 if (sp >= i->start && sp <= i->end) {
107 return i;
njn945ed2e2005-06-24 03:28:30 +0000108 }
109 i = i->next;
110 }
njn4e72d202005-08-14 04:12:40 +0000111 return NULL;
njn945ed2e2005-06-24 03:28:30 +0000112}
113
114/*
115 * Register a new stack from start - end. This is invoked from the
116 * VALGRIND_STACK_REGISTER client request, and is also called just before
117 * we start the client running, to register the main process stack.
118 */
119UWord VG_(register_stack)(Addr start, Addr end)
120{
121 Stack *i;
sewardj45f4e7c2005-09-27 19:20:21 +0000122
njn945ed2e2005-06-24 03:28:30 +0000123 if (start > end) {
124 Addr t = end;
125 end = start;
126 start = t;
127 }
128
129 i = (Stack *)VG_(arena_malloc)(VG_AR_CORE, sizeof(Stack));
130 i->start = start;
131 i->end = end;
132 i->id = next_id++;
133 i->next = stacks;
134 stacks = i;
135
njn4e72d202005-08-14 04:12:40 +0000136 if (i->id == 0) {
tomde3cf132005-11-10 14:24:08 +0000137 current_stack = i;
njn945ed2e2005-06-24 03:28:30 +0000138 }
139
njn4818d732005-11-10 15:03:26 +0000140 VG_(debugLog)(2, "stacks", "register %p-%p as stack %lu\n",
141 (void*)start, (void*)end, i->id);
tomde3cf132005-11-10 14:24:08 +0000142
njn945ed2e2005-06-24 03:28:30 +0000143 return i->id;
144}
145
146/*
147 * Deregister a stack. This is invoked from the VALGRIND_STACK_DEREGISTER
148 * client request.
149 */
150void VG_(deregister_stack)(UWord id)
151{
152 Stack *i = stacks;
153 Stack *prev = NULL;
154
njn4818d732005-11-10 15:03:26 +0000155 VG_(debugLog)(2, "stacks", "deregister stack %lu\n", id);
tomde3cf132005-11-10 14:24:08 +0000156
sewardj0edccdd2007-11-17 02:05:57 +0000157 if (current_stack && current_stack->id == id) {
tomde3cf132005-11-10 14:24:08 +0000158 current_stack = NULL;
njn945ed2e2005-06-24 03:28:30 +0000159 }
160
161 while(i) {
162 if (i->id == id) {
163 if(prev == NULL) {
164 stacks = i->next;
165 } else {
166 prev->next = i->next;
167 }
168 VG_(arena_free)(VG_AR_CORE, i);
169 return;
170 }
171 prev = i;
172 i = i->next;
173 }
174}
175
176/*
177 * Change a stack. This is invoked from the VALGRIND_STACK_CHANGE client
178 * request and from the stack growth stuff the signals module when
179 * extending the main process stack.
180 */
181void VG_(change_stack)(UWord id, Addr start, Addr end)
182{
183 Stack *i = stacks;
184
njn4e72d202005-08-14 04:12:40 +0000185 while (i) {
njn945ed2e2005-06-24 03:28:30 +0000186 if (i->id == id) {
njn4818d732005-11-10 15:03:26 +0000187 VG_(debugLog)(2, "stacks", "change stack %lu from %p-%p to %p-%p\n",
188 id, (void*)i->start, (void*)i->end,
189 (void*)start, (void*)end);
njn945ed2e2005-06-24 03:28:30 +0000190 i->start = start;
191 i->end = end;
192 return;
193 }
194 i = i->next;
195 }
196}
197
198/* This function gets called if new_mem_stack and/or die_mem_stack are
199 tracked by the tool, and one of the specialised cases
200 (eg. new_mem_stack_4) isn't used in preference.
201*/
202VG_REGPARM(2)
203void VG_(unknown_SP_update)( Addr old_SP, Addr new_SP )
204{
205 static Int moans = 3;
206 Word delta = (Word)new_SP - (Word)old_SP;
207
208 /* Check if the stack pointer is still in the same stack as before. */
tomde3cf132005-11-10 14:24:08 +0000209 if (current_stack == NULL ||
210 new_SP < current_stack->start || new_SP > current_stack->end) {
njn4e72d202005-08-14 04:12:40 +0000211 Stack* new_stack = find_stack_by_addr(new_SP);
sewardj0edccdd2007-11-17 02:05:57 +0000212 if (new_stack
213 && (current_stack == NULL || new_stack->id != current_stack->id)) {
njn945ed2e2005-06-24 03:28:30 +0000214 /* The stack pointer is now in another stack. Update the current
215 stack information and return without doing anything else. */
tomde3cf132005-11-10 14:24:08 +0000216 current_stack = new_stack;
njn945ed2e2005-06-24 03:28:30 +0000217 return;
218 }
219 }
220
221 if (delta < -VG_(clo_max_stackframe) || VG_(clo_max_stackframe) < delta) {
222 /* SP has changed by more than some threshold amount (by
223 default, 2MB). We take this to mean that the application is
224 switching to a new stack, for whatever reason.
225
226 JRS 20021001: following discussions with John Regehr, if a stack
227 switch happens, it seems best not to mess at all with memory
228 permissions. Seems to work well with Netscape 4.X. Really the
229 only remaining difficulty is knowing exactly when a stack switch is
230 happening. */
231 if (VG_(clo_verbosity) > 0 && moans > 0) {
232 moans--;
233 VG_(message)(Vg_UserMsg,
234 "Warning: client switching stacks? "
235 "SP change: %p --> %p", old_SP, new_SP);
236 VG_(message)(Vg_UserMsg,
sewardj91b470c2007-08-28 17:03:01 +0000237 " to suppress, use: --max-stackframe=%ld or greater",
njn945ed2e2005-06-24 03:28:30 +0000238 (delta < 0 ? -delta : delta));
239 if (moans == 0)
240 VG_(message)(Vg_UserMsg,
241 " further instances of this message "
242 "will not be shown.");
243 }
244 } else if (delta < 0) {
245 VG_TRACK( new_mem_stack, new_SP, -delta );
246
247 } else if (delta > 0) {
248 VG_TRACK( die_mem_stack, old_SP, delta );
249 }
250}
251
252/*--------------------------------------------------------------------*/
253/*--- end ---*/
254/*--------------------------------------------------------------------*/
255