shithub: front

Download patch

ref: bb5c5c1c35082bd1521ce1bd64db3c9f81468fb5
parent: 07c8c2c838256ea0b3b9dd6d5a70ba32ff3bafab
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sat Aug 16 09:01:43 EDT 2025

kernel: fill stack segments with 0xfefe... pattern

To aid detecting uninitialized variables (or missing
NUL termination of string buffers), fill the stack
segment with 0xfefe... pattern instead of zero.

--- a/sys/src/9/port/devsegment.c
+++ b/sys/src/9/port/devsegment.c
@@ -522,7 +522,7 @@
 			p->va = va;
 			va += BY2PG;
 			p->modref = 0;
-			zeropage(p);
+			fillpage(p, 0);
 			if(waserror()){
 				while(++p <= l)
 					freepages(p, p, 1);
--- a/sys/src/9/port/fault.c
+++ b/sys/src/9/port/fault.c
@@ -174,12 +174,13 @@
 		break;
 
 	case SG_BSS:
-	case SG_SHARED:			/* Zero fill on demand */
+	case SG_SHARED:			/* fill on demand */
 	case SG_STACK:
 		if(*pg == nil) {
-			new = newpage(1, &s, addr);
+			new = newpage(0, &s, addr);
 			if(s == nil)
 				return -1;
+			fillpage(new, (s->type&SG_TYPE)==SG_STACK? 0xfe: 0);
 			*pg = new;
 			s->used++;
 		}
--- a/sys/src/9/port/page.c
+++ b/sys/src/9/port/page.c
@@ -232,7 +232,7 @@
 	inittxtflush(p);
 
 	if(clear)
-		zeropage(p);
+		fillpage(p, 0);
 
 	return p;
 }
@@ -273,14 +273,17 @@
 	kunmap(kd);
 }
 
-void
-zeropage(Page *p)
+Page*
+fillpage(Page *p, int c)
 {
 	KMap *k;
 
-	k = kmap(p);
-	memset((void*)VA(k), 0, BY2PG);
-	kunmap(k);
+	if(p != nil){
+		k = kmap(p);
+		memset((void*)VA(k), c, BY2PG);
+		kunmap(k);
+	}
+	return p;
 }
 
 void
@@ -388,7 +391,7 @@
 	for(p = palloc.pages; p != pe; p++) {
 		if(p->modref & PG_PRIV){
 			incref(p);
-			zeropage(p);
+			fillpage(p, 0);
 			decref(p);
 		}
 	}
--- a/sys/src/9/port/portfns.h
+++ b/sys/src/9/port/portfns.h
@@ -416,7 +416,7 @@
 void*		xspanalloc(ulong, int, ulong);
 void		xsummary(void);
 void		yield(void);
-void		zeropage(Page*);
+Page*		fillpage(Page*, int);
 void		zeroprivatepages(void);
 Segment*	data2txt(Segment*);
 Segment*	dupseg(Segment**, int, int);
--- a/sys/src/9/port/sysproc.c
+++ b/sys/src/9/port/sysproc.c
@@ -541,6 +541,7 @@
 		memmove(charp, a, n);
 		charp += n;
 	}
+	*argv = nil;
 
 	/* copy args; easiest from new process's stack */
 	a = (char*)(tstk - nbytes);
--