Date: Mon, 24 Sep 2001 21:26:07 -0700 (PDT) From: Matt Dillon <dillon@earth.backplane.com> To: Peter Wemm <peter@wemm.org> Cc: Ian Dowse <iedowse@maths.tcd.ie>, Julian Elischer <julian@elischer.org>, hackers@FreeBSD.ORG Subject: Patch to test kstack usage. Message-ID: <200109250426.f8P4Q7a99088@earth.backplane.com>
next in thread | raw e-mail | index | archive | help
This isn't perfect but it should be a good start in regards to
testing kstack use. This patch is against -stable. It reports
kernel stack use on process exit and will generate a 'Kernel stack
underflow' message if it detects an underflow. It doesn't panic,
so for a fun time you can leave UPAGES at 2 and watch in horror.
note: make sure you make depend before making a new kernel, or use
buildkernel.
-Matt
Index: sys/user.h
===================================================================
RCS file: /home/ncvs/src/sys/sys/user.h,v
retrieving revision 1.24
diff -u -r1.24 user.h
--- sys/user.h 1999/12/29 04:24:49 1.24
+++ sys/user.h 2001/09/25 03:41:04
@@ -109,9 +109,13 @@
* Remaining fields only for core dump and/or ptrace--
* not valid at other times!
*/
+ u_int32_t u_guard2; /* guard the base of the kstack */
struct kinfo_proc u_kproc; /* proc + eproc */
struct md_coredump u_md; /* machine dependent glop */
+ u_int32_t u_guard; /* guard the base of the kstack */
};
+
+#define U_GUARD_MAGIC 0x51A2C3D4
/*
* Redefinitions to make the debuggers happy for now... This subterfuge
Index: kern/init_main.c
===================================================================
RCS file: /home/ncvs/src/sys/kern/init_main.c,v
retrieving revision 1.134.2.6
diff -u -r1.134.2.6 init_main.c
--- kern/init_main.c 2001/06/15 09:37:55 1.134.2.6
+++ kern/init_main.c 2001/09/25 01:39:05
@@ -358,6 +358,7 @@
*/
p->p_stats = &p->p_addr->u_stats;
p->p_sigacts = &p->p_addr->u_sigacts;
+ p->p_addr->u_guard = U_GUARD_MAGIC; /* bottom of kernel stack */
/*
* Charge root for one process.
Index: kern/kern_exit.c
===================================================================
RCS file: /home/ncvs/src/sys/kern/kern_exit.c,v
retrieving revision 1.92.2.5
diff -u -r1.92.2.5 kern_exit.c
--- kern/kern_exit.c 2001/07/27 14:06:01 1.92.2.5
+++ kern/kern_exit.c 2001/09/25 04:09:32
@@ -123,6 +123,16 @@
WTERMSIG(rv), WEXITSTATUS(rv));
panic("Going nowhere without my init!");
}
+ {
+ int *ua;
+ int *addrend = (int *)((char *)p->p_addr + UPAGES * PAGE_SIZE);
+ for (ua = &p->p_addr->u_guard + 1; ua < addrend; ++ua) {
+ if (*ua != 0x11111111)
+ break;
+ }
+ printf("process %d exit kstackuse %d\n",
+ p->p_pid, (char *)addrend - (char *)ua);
+ }
aio_proc_rundown(p);
Index: kern/kern_synch.c
===================================================================
RCS file: /home/ncvs/src/sys/kern/kern_synch.c,v
retrieving revision 1.87.2.3
diff -u -r1.87.2.3 kern_synch.c
--- kern/kern_synch.c 2000/12/31 22:10:45 1.87.2.3
+++ kern/kern_synch.c 2001/09/25 02:54:46
@@ -44,13 +44,17 @@
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
+#include <sys/lock.h>
#include <sys/kernel.h>
#include <sys/signalvar.h>
#include <sys/resourcevar.h>
#include <sys/vmmeter.h>
#include <sys/sysctl.h>
#include <vm/vm.h>
+#include <vm/pmap.h>
+#include <vm/vm_map.h>
#include <vm/vm_extern.h>
+#include <sys/user.h>
#ifdef KTRACE
#include <sys/uio.h>
#include <sys/ktrace.h>
@@ -792,6 +796,13 @@
register struct proc *p = curproc; /* XXX */
register struct rlimit *rlim;
int x;
+
+ /*
+ * Check to see if the kernel stack underflowed (XXX)
+ */
+ if (p->p_addr->u_guard != U_GUARD_MAGIC) {
+ printf("Kernel stack underflow! %p %p %08x\n", p, p->p_addr, p->p_addr->u_guard);
+ }
/*
* XXX this spl is almost unnecessary. It is partly to allow for
Index: i386/i386/pmap.c
===================================================================
RCS file: /home/ncvs/src/sys/i386/i386/pmap.c,v
retrieving revision 1.250.2.10
diff -u -r1.250.2.10 pmap.c
--- i386/i386/pmap.c 2001/07/30 23:27:59 1.250.2.10
+++ i386/i386/pmap.c 2001/09/25 04:03:52
@@ -891,6 +891,7 @@
}
if (updateneeded)
invltlb();
+ memset(up, 0x11, UPAGES * PAGE_SIZE);
}
/*
Index: i386/include/param.h
===================================================================
RCS file: /home/ncvs/src/sys/i386/include/param.h,v
retrieving revision 1.54.2.5
diff -u -r1.54.2.5 param.h
--- i386/include/param.h 2001/09/15 00:50:36 1.54.2.5
+++ i386/include/param.h 2001/09/25 03:41:11
@@ -110,7 +110,7 @@
#define MAXDUMPPGS (DFLTPHYS/PAGE_SIZE)
#define IOPAGES 2 /* pages of i/o permission bitmap */
-#define UPAGES 2 /* pages of u-area */
+#define UPAGES 4 /* pages of u-area */
/*
* Ceiling on amount of swblock kva space.
Index: vm/vm_glue.c
===================================================================
RCS file: /home/ncvs/src/sys/vm/vm_glue.c,v
retrieving revision 1.94.2.1
diff -u -r1.94.2.1 vm_glue.c
--- vm/vm_glue.c 2000/08/02 22:15:09 1.94.2.1
+++ vm/vm_glue.c 2001/09/25 03:52:01
@@ -265,6 +265,11 @@
((caddr_t) &up->u_stats.pstat_endcopy -
(caddr_t) &up->u_stats.pstat_startcopy));
+ /*
+ * Kernel stack guard (detection only unfortunately)
+ */
+ up->u_guard = U_GUARD_MAGIC;
+ up->u_guard2 = U_GUARD_MAGIC;
/*
* cpu_fork will copy and update the pcb, set up the kernel stack,
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200109250426.f8P4Q7a99088>
