Commit Diff


commit - cdd61ab0aee825e93688ae5d2cfb86f14baad858
commit + a3785ca2cc1c8693d89ebe44216acf781a634718
blob - 2f391bff61ed15b45eee2fa4e5a195187bf92a7f
blob + 3fbf0afe6d9fe063b1d5a74531b3c71c57b4c922
--- src/libthread/386.c
+++ src/libthread/386.c
@@ -1,8 +1,23 @@
 #include "threadimpl.h"
+/*
+ * To use this you need some patches to Valgrind that
+ * let it help out with detecting stack overflow. 
+ */
+#define USEVALGRIND 0
+#ifdef USEVALGRIND
+#include <valgrind/memcheck.h>
+#endif
 
 static void
 launcher386(void (*f)(void *arg), void *arg)
 {
+	Proc *p;
+	Thread *t;
+
+	p = _threadgetproc();
+	t = p->thread;
+	_threadstacklimit(t->stk);
+
 	(*f)(arg);
 	threadexits(nil);
 }
@@ -19,3 +34,24 @@ _threadinitstack(Thread *t, void (*f)(void*), void *ar
 	t->sched.sp = (ulong)tos - 8;		/* old PC and new PC */
 }
 
+void
+_threadinswitch(int enter)
+{
+	USED(enter);
+#ifdef USEVALGRIND
+	if(enter)
+		VALGRIND_SET_STACK_LIMIT(0, 0, 1);
+	else
+		VALGRIND_SET_STACK_LIMIT(0, 0, 0);
+#endif
+}
+
+void
+_threadstacklimit(void *addr)
+{
+	USED(addr);
+
+#ifdef USEVALGRIND
+	VALGRIND_SET_STACK_LIMIT(1, addr, 0);
+#endif
+}
blob - 35e2ab6f87f746e3acc9434f2e5571c0184b4145
blob + c815ac580e08bc7de133b32a147b8d1fcc277e88
--- src/libthread/asm-FreeBSD-386.s
+++ src/libthread/asm-FreeBSD-386.s
@@ -17,6 +17,9 @@ _setlabel:
 .type	_gotolabel,@function
 
 _gotolabel:
+	pushl $1
+	call _threadinswitch
+	popl %eax
 	movl	4(%esp), %edx
 	movl	0(%edx), %ecx
 	movl	4(%edx), %ebx
@@ -24,9 +27,12 @@ _gotolabel:
 	movl	12(%edx), %ebp
 	movl	16(%edx), %esi
 	movl	20(%edx), %edi
+	movl	%ecx, 0(%esp)
+	pushl $0
+	call _threadinswitch
+	popl %eax
 	xorl	%eax, %eax
 	incl	%eax
-	movl	%ecx, 0(%esp)
 	ret
 
 
blob - 48fe78c7d9c4470dc9526f7976c9a2e2a1899c5b
blob + ce27896b1faf0da07814a52757d367e8598435ee
--- src/libthread/channel.c
+++ src/libthread/channel.c
@@ -277,7 +277,7 @@ static void
 channelsize(Channel *c, int sz)
 {
 	if(c->e != sz){
-		fprint(2, "expected channel with elements of size %d, got size %d",
+		fprint(2, "expected channel with elements of size %d, got size %d\n",
 			sz, c->e);
 		abort();
 	}
blob - 20ab5a8d59071aee9a8f6afa43b6a405420a7e2d
blob + a76682b26afbc7e9c912de7f29c692293ce8eb2a
--- src/libthread/mkfile
+++ src/libthread/mkfile
@@ -53,3 +53,6 @@ trend: trend.$O $PLAN9/lib/$LIB
 
 CLEANFILES=$CLEANFILES tprimes texec
 
+asm-Linux-386.$O: asm-FreeBSD-386.s
+asm-NetBSD-386.$O: asm-FreeBSD-386.s
+asm-OpenBSD-386.$O: asm-FreeBSD-386.s
blob - d193d6ce4e6ee5b8b4c9efb3648a0f520c81c08a
blob + 57d597baf4ae1887f8bab9aede2e6f00ac7cff5d
--- src/libthread/sched.c
+++ src/libthread/sched.c
@@ -166,14 +166,15 @@ Resched:
 	p = _threadgetproc();
 //fprint(2, "p %p\n", p);
 	if((t = p->thread) != nil){
-		if((ulong)&p < (ulong)t->stk){	/* stack overflow */
-			fprint(2, "stack overflow %lux %lux\n", (ulong)&p, (ulong)t->stk);
+		if((ulong)&p < (ulong)t->stk+512){	/* stack overflow waiting to happen */
+			fprint(2, "stack overflow: stack at %lux, limit at %lux\n", (ulong)&p, (ulong)t->stk);
 			abort();
 		}
 	//	_threaddebug(DBGSCHED, "pausing, state=%s set %p goto %p",
 	//		psstate(t->state), &t->sched, &p->sched);
 		if(_setlabel(&t->sched)==0)
 			_gotolabel(&p->sched);
+		_threadstacklimit(t->stk);
 		return;
 	}else{
 		t = runthread(p);
blob - 06ed671de53a7577defa3a92f0b60957a2e69734
blob + 373164adbeb1a1836544dd31c83fc1bcc3e44a96
--- src/libthread/threadimpl.h
+++ src/libthread/threadimpl.h
@@ -219,4 +219,4 @@ extern int _threadgetpid(void);
 extern void _threadmemset(void*, int, int);
 extern void _threaddebugmemset(void*, int, int);
 extern int _threadprocs;
-
+extern void _threadstacklimit(void*);
blob - 9426bdfad7ac69a191256ee27ce4d3dc3569463f
blob + e028f982076010724a04dd9dad4a28d9eee16ca0
--- src/libthread/tprimes.c
+++ src/libthread/tprimes.c
@@ -1,6 +1,12 @@
-#include <lib9.h>
+#include <u.h>
+#include <libc.h>
 #include <thread.h>
 
+enum
+{
+	STACK = 8192
+};
+
 int quiet;
 int goal;
 int buffer;
@@ -19,7 +25,7 @@ primethread(void *arg)
 	if(!quiet)
 		print("%d\n", p);
 	nc = chancreate(sizeof(ulong), buffer);
-	(*fn)(primethread, nc, 8192);
+	(*fn)(primethread, nc, STACK);
 	for(;;){
 		i = recvul(c);
 		if(i%p)
@@ -56,7 +62,7 @@ threadmain(int argc, char **argv)
 		goal = 100;
 
 	c = chancreate(sizeof(ulong), buffer);
-	(*fn)(primethread, c, 8192);
+	(*fn)(primethread, c, STACK);
 	for(i=2;; i++)
 		sendul(c, i);
 }