|
From: Tom H. <th...@cy...> - 2004-09-11 14:17:10
|
CVS commit by thughes:
Virtualise the stack rlimit for the main thread and make valgrind enforce
that limit when growing the stack. Also add a message when the stack in any
thread overflows.
CCMAIL: 738...@bu...
M +1 -0 core.h 1.23
M +5 -1 vg_main.c 1.211
M +2 -2 vg_scheduler.c 1.181
M +10 -1 vg_signals.c 1.86
M +15 -0 vg_syscalls.c 1.136
--- valgrind/coregrind/core.h #1.22:1.23
@@ -1244,4 +1244,5 @@ extern Addr VG_(valgrind_last); // Nb: l
extern vki_rlimit VG_(client_rlimit_data); /* client's original rlimit data */
+extern vki_rlimit VG_(client_rlimit_stack); /* client's original rlimit stack */
/* client executable file descriptor */
--- valgrind/coregrind/vg_main.c #1.210:1.211
@@ -114,4 +114,5 @@ Addr VG_(valgrind_last);
vki_rlimit VG_(client_rlimit_data);
+vki_rlimit VG_(client_rlimit_stack);
/* This is set early to indicate whether this CPU has the
@@ -2498,4 +2499,7 @@ int main(int argc, char **argv)
VG_(setrlimit)(VKI_RLIMIT_DATA, &zero);
+ // Get the current process stack rlimit.
+ VG_(getrlimit)(VKI_RLIMIT_STACK, &VG_(client_rlimit_stack));
+
//--------------------------------------------------------------
// Check we were launched by stage1
--- valgrind/coregrind/vg_scheduler.c #1.180:1.181
@@ -429,5 +429,5 @@ void VG_(scheduler_init) ( void )
= VG_(clstk_end) - 4;
VG_(threads)[tid_main].stack_base = VG_(clstk_base);
- VG_(threads)[tid_main].stack_size = VG_(clstk_end) - VG_(clstk_base);
+ VG_(threads)[tid_main].stack_size = VG_(client_rlimit_stack).rlim_cur;
/* So now ... */
--- valgrind/coregrind/vg_signals.c #1.85:1.86
@@ -2110,5 +2110,6 @@ void vg_sync_signalhandler ( Int sigNo,
*/
Addr base = PGROUNDDN(esp);
- if ((void*)-1 != VG_(mmap)((Char *)base, seg->addr - base,
+ if (seg->len + (seg->addr - base) <= VG_(threads)[tid].stack_size &&
+ (void*)-1 != VG_(mmap)((Char *)base, seg->addr - base,
VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC,
VKI_MAP_PRIVATE|VKI_MAP_FIXED|VKI_MAP_ANONYMOUS|VKI_MAP_CLIENT,
@@ -2139,4 +2140,12 @@ void vg_sync_signalhandler ( Int sigNo,
}
}
+
+ if (info->si_code == 1 && /* SEGV_MAPERR */
+ seg != NULL &&
+ fault >= esp &&
+ fault < seg->addr &&
+ (seg->flags & SF_STACK)) {
+ VG_(message)(Vg_UserMsg, "Stack overflow in thread %d", tid);
+ }
}
--- valgrind/coregrind/vg_syscalls.c #1.135:1.136
@@ -2347,4 +2347,8 @@ POST(getrlimit)
*((vki_rlimit *)arg2) = VG_(client_rlimit_data);
break;
+
+ case VKI_RLIMIT_STACK:
+ *((vki_rlimit *)arg2) = VG_(client_rlimit_stack);
+ break;
}
}
@@ -4693,4 +4697,15 @@ PRE(setrlimit)
res = 0;
}
+ else if (arg1 == VKI_RLIMIT_STACK && tid == 1) {
+ if (((vki_rlimit *)arg2)->rlim_cur > ((vki_rlimit *)arg2)->rlim_max ||
+ ((vki_rlimit *)arg2)->rlim_max > ((vki_rlimit *)arg2)->rlim_max) {
+ res = -VKI_EPERM;
+ }
+ else {
+ VG_(threads)[tid].stack_size = ((vki_rlimit *)arg2)->rlim_cur;
+ VG_(client_rlimit_stack) = *(vki_rlimit *)arg2;
+ res = 0;
+ }
+ }
}
|