shithub: 5v

Download patch

ref: 790fcdabb61b76587b665486f2a3fccdae684986
parent: 5a514a681e4c2ab86f76509203a0a96d371d51aa
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Aug 30 13:57:08 EDT 2023

chk: fix realloc

--- a/arm.c
+++ b/arm.c
@@ -40,7 +40,7 @@
 	a = *(u32int*) vaddrnol(P->R[13] + 4 + 4 * n, 4, ARD);
 	if(a == 0)
 		return ~0;
-	return *(ulong*) vaddrnol(a-4, 4, ARD);
+	return *(ulong*) vaddrnol(a-4, 4, 0);
 }
 
 void
@@ -558,17 +558,15 @@
 
 	instr = *(u32int*) vaddr(P->R[15], 4, 0, &seg);
 	segunlock(seg);
-	if(fulltrace || vtrace) {
+	if(fulltrace) {
 		print("%d ", P->pid);
-		if(havesymbols) {
-			Symbol s;
-			char buf[512];
-			
-			if(findsym(P->R[15], CTEXT, &s) >= 0)
-				print("%s ", s.name);
-			if(fileline(buf, 512, P->R[15]) >= 0)
-				print("%s ", buf);
-		}
+		Symbol s;
+		char buf[512];
+		
+		if(findsym(P->R[15], CTEXT, &s) >= 0)
+			print("%s ", s.name);
+		if(fileline(buf, 512, P->R[15]) >= 0)
+			print("%s ", buf);
 		print("%.8ux %.8ux %c%c%c%c\n", P->R[15], instr,
 			(P->CPSR & flZ) ? 'Z' : ' ',
 			(P->CPSR & flC) ? 'C' : ' ',
--- a/chk.c
+++ b/chk.c
@@ -5,7 +5,6 @@
 #include <mach.h>
 #include "dat.h"
 #include "fns.h"
-extern int vtrace;
 
 void
 dumpmap(Segment *seg, u32int off, char *fmt, ...)
@@ -12,7 +11,7 @@
 {
 	char buf[16*1024], *p, *e;
 	va_list ap;
-	int i;
+	int i, c;
 
 	p = buf;
 	e = p + sizeof(buf)-1;
@@ -23,9 +22,13 @@
 		print("not tracing\n");
 		return;
 	}
-	for(i = (off&~0x4095)/8; i <= (seg->size + 7)/8; i++){
+	for(i = 0; i <= (seg->size + 7)/8; i++){
+		if(i >= off/8 && i+40 < off/8)
+			c = '*';
+		else
+			c = ' ';
 		if(i % 40 == 0)
-			p = seprint(p, e, "\n[%04x] ", seg->start+i*8);
+			p = seprint(p, e, "\n[%04x]%c", seg->start+i*8, c);
 		p = seprint(p, e, "%02ux", seg->shadow[i]);
 	}
 	*p++ = '\n';
@@ -55,7 +58,6 @@
 	for(end = off+len; off != end; off++){
 		if((seg->shadow[off>>3] & 1<<(off&7)) == 0){
 			print("invalid read of %#x at %#x\n", seg->start+off, P->R[15] - 4);
-dumpmap(seg, off, "broken");
 			rendezvous(&never, nil);
 			exits("bad access");
 		}
@@ -66,8 +68,6 @@
 {
 	u32int end;
 
-//print("seg: %#p\n", seg);
-//print("mark[shadow=%#p] %#x+%x [%#x+%#x]\n", seg->shadow, seg->start + off, len, seg->start, off);
 	for(end = off+len; off != end; off++)
 		seg->shadow[off>>3] |= (1<<(off&7));
 }
@@ -105,19 +105,31 @@
 	u32int oldoff, newoff;
 	Segment *seg0, *seg1;
 
-	oldp = vaddr(av[0], 0, 0, &seg0);
-	newp = vaddr(P->R[0], 0, 0, &seg1);
-	oldsz = av[1];
-	newsz = av[2];
-	oldoff = oldp - (uchar*)seg0->data;
-	newoff = newp - (uchar*)seg1->data;
+	oldp = nil;
+	newp = nil;
+	oldsz = 0;
+	newsz = 0;
+	oldoff = 0;
+	newoff = 0;
+	if(av[0] != 0){
+		oldp = vaddr(av[0], 0, 0, &seg0);
+		oldsz = av[1];
+		oldoff = oldp - (uchar*)seg0->data;
+	}
+	if(P->R[0] != 0){
+		newp = vaddr(P->R[0], 0, 0, &seg1);
+		newsz = av[2];
+		newoff = newp - (uchar*)seg1->data;
+	}
 	sz = (oldsz < newsz) ? oldsz : newsz;
 	if(mallocverbose)
 		dumpmap(seg0, oldp-(uchar*)seg0->start, "realloc %#x+%d => %#x+%d\n", av[0], av[1], P->R[0], av[2]);
-	if(oldp != newp)
+	if(oldp != newp){
 		memcpy(seg1->shadow+oldoff/8, seg0->shadow+newoff/8, (sz+7)/8);
+		markinvalid(seg0, oldoff, oldsz);
+	}
 	if(newsz > oldsz)
-		markinvalid(seg0, oldoff+oldsz, newsz-oldsz);
+		markinvalid(seg1, newoff+oldsz, newsz-oldsz);
 	if(mallocverbose)
 		dumpmap(seg1, newp-(uchar*)seg1->start, "postrealloc");
 	segunlock(seg0);
--- a/dat.h
+++ b/dat.h
@@ -97,7 +97,6 @@
 };
 
 #define fulltrace 0
-#define havesymbols 1
 #define ultraverbose 0
-#define mallocverbose 1
+#define mallocverbose 0
 #define systrace 0
--- a/proc.c
+++ b/proc.c
@@ -257,7 +257,6 @@
 		sysfatal("%r");
 	memset(bss->data, 0, bss->size);
 	P->R[15] = fp.entry;
-	if(havesymbols || check)
 	if(syminit(fd, &fp) < 0)
 		fprint(2, "initializing symbol table: %r\n");
 	if(check)
--- a/sys.c
+++ b/sys.c
@@ -102,6 +102,7 @@
 	if(check && (s32int)P->R[0] > 0){
 		vaddr(buf, 0, 0, &seg);
 		markvalid(seg, buf-seg->start, P->R[0]);
+		segunlock(seg);
 	}
 }
 
@@ -185,6 +186,7 @@
 	if(check && (s32int)P->R[0] > 0){
 		vaddr(edir, 0, 0, &seg);
 		markvalid(seg, edir-seg->start, P->R[0]);
+		segunlock(seg);
 	}
 }
 
@@ -208,6 +210,7 @@
 	if(check && (s32int)P->R[0] > 0){
 		vaddr(edir, 0, 0, &seg);
 		markvalid(seg, edir-seg->start, P->R[0]);
+		segunlock(seg);
 	}
 }
 
@@ -276,16 +279,16 @@
 	if(v < P->S[SEGBSS]->start)
 		sysfatal("bss length < 0, wtf?");
 	s = P->S[SEGBSS];
+	n = v - s->start;
+	o = s->size;
 	wlock(&s->rw);
-	s->dref = realloc(s->dref, v - s->start + sizeof(Ref));
+	s->dref = realloc(s->dref, n + sizeof(Ref));
 	if(s->dref == nil)
 		sysfatal("error reallocating");
 	s->data = s->dref + 1;
-	if(s->size < v - s->start)
-		memset((char*)s->data + s->size, 0, v - s->start - s->size);
+	if(o < n)
+		memset((char*)s->data + o, 0, n - o);
 	if(s->shadow != nil){
-		o = s->size;
-		n = (v - s->start);
 		s->shadow = realloc(s->shadow, n);
 		if(s->shadow == nil)
 			sysfatal("error reallocating");