shithub: riscv

Download patch

ref: bff129628abfb6052b74c64dd5e4ea54cc12a1f1
parent: b12b052a7196b4ff50cf12ccd92e9cdd308aa82f
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Mon Jul 31 18:25:21 EDT 2023

*l: make sure Sym and Auto structs are fully initialized (thanks moodman)

The conversion from gethunk() to malloc() means
the allocation is not guaranteed to be zeroed,
so memset() explicitely when neccessary.

--- a/sys/src/cmd/1l/obj.c
+++ b/sys/src/cmd/1l/obj.c
@@ -498,6 +498,7 @@
 	}
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	u->link = curauto;
 	curauto = u;
 	u->asym = s;
@@ -581,7 +582,9 @@
 	int i, j, k;
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	s = malloc(sizeof(Sym));
+	memset(s, 0, sizeof(Sym));
 	s->name = malloc(2*(histfrogp+1) + 1);
 
 	u->asym = s;
@@ -591,6 +594,7 @@
 	curhist = u;
 
 	j = 1;
+	s->name[0] = 0;
 	for(i=0; i<histfrogp; i++) {
 		k = histfrog[i]->value;
 		s->name[j+0] = k>>8;
@@ -597,6 +601,8 @@
 		s->name[j+1] = k;
 		j += 2;
 	}
+	s->name[j+0] = 0;
+	s->name[j+1] = 0;
 }
 
 void
@@ -754,7 +760,7 @@
 		goto loop;
 	}
 
-	p = malloc(sizeof(Prog));
+	p = prg();
 	p->as = o;
 	p->line = bloc[2] | (bloc[3] << 8) | (bloc[4] << 16) | (bloc[5] << 24);
 	p->back = 2;
@@ -1015,13 +1021,12 @@
 			return s;
 
 	s = malloc(sizeof(Sym));
-	s->name = malloc(l + 1);
+	memset(s, 0, sizeof(Sym));
+	s->name = malloc(l);
 	memmove(s->name, symb, l);
 
 	s->link = hash[h];
-	s->type = 0;
 	s->version = v;
-	s->value = 0;
 	hash[h] = s;
 	nsymbol++;
 	return s;
@@ -1030,7 +1035,7 @@
 Prog*
 prg(void)
 {
-	Prog *p = (Prog*)malloc(sizeof(Prog));
+	Prog *p = malloc(sizeof(Prog));
 	*p = zprg;
 	return p;
 }
--- a/sys/src/cmd/2l/obj.c
+++ b/sys/src/cmd/2l/obj.c
@@ -513,6 +513,7 @@
 	}
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	u->link = curauto;
 	curauto = u;
 	u->asym = s;
@@ -596,7 +597,9 @@
 	int i, j, k;
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	s = malloc(sizeof(Sym));
+	memset(s, 0, sizeof(Sym));
 	s->name = malloc(2*(histfrogp+1) + 1);
 
 	u->asym = s;
@@ -606,6 +609,7 @@
 	curhist = u;
 
 	j = 1;
+	s->name[0] = 0;
 	for(i=0; i<histfrogp; i++) {
 		k = histfrog[i]->value;
 		s->name[j+0] = k>>8;
@@ -612,6 +616,8 @@
 		s->name[j+1] = k;
 		j += 2;
 	}
+	s->name[j+0] = 0;
+	s->name[j+1] = 0;
 }
 
 void
@@ -769,7 +775,7 @@
 		goto loop;
 	}
 
-	p = malloc(sizeof(Prog));
+	p = prg();
 	p->as = o;
 	p->line = bloc[2] | (bloc[3] << 8) | (bloc[4] << 16) | (bloc[5] << 24);
 	p->back = 2;
@@ -1039,13 +1045,12 @@
 			return s;
 
 	s = malloc(sizeof(Sym));
-	s->name = malloc(l + 1);
+	memset(s, 0, sizeof(Sym));
+	s->name = malloc(l);
 	memmove(s->name, symb, l);
 
 	s->link = hash[h];
-	s->type = 0;
 	s->version = v;
-	s->value = 0;
 	hash[h] = s;
 	nsymbol++;
 	return s;
--- a/sys/src/cmd/5l/obj.c
+++ b/sys/src/cmd/5l/obj.c
@@ -514,6 +514,7 @@
 		}
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	u->link = curauto;
 	curauto = u;
 	u->asym = s;
@@ -598,7 +599,9 @@
 	int i, j, k;
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	s = malloc(sizeof(Sym));
+	memset(s, 0, sizeof(Sym));
 	s->name = malloc(2*(histfrogp+1) + 1);
 
 	u->asym = s;
@@ -608,6 +611,7 @@
 	curhist = u;
 
 	j = 1;
+	s->name[0] = 0;
 	for(i=0; i<histfrogp; i++) {
 		k = histfrog[i]->value;
 		s->name[j+0] = k>>8;
@@ -614,6 +618,8 @@
 		s->name[j+1] = k;
 		j += 2;
 	}
+	s->name[j+0] = 0;
+	s->name[j+1] = 0;
 }
 
 void
@@ -798,7 +804,7 @@
 		goto loop;
 	}
 
-	p = malloc(sizeof(Prog));
+	p = prg();
 	p->as = o;
 	p->scond = bloc[1];
 	p->reg = bloc[2];
@@ -1081,15 +1087,12 @@
 			return s;
 
 	s = malloc(sizeof(Sym));
+	memset(s, 0, sizeof(Sym));
 	s->name = malloc(l);
 	memmove(s->name, symb, l);
 
 	s->link = hash[h];
-	s->type = 0;
 	s->version = v;
-	s->value = 0;
-	s->sig = 0;
-	s->dupok = 0;
 	hash[h] = s;
 	return s;
 }
--- a/sys/src/cmd/6l/obj.c
+++ b/sys/src/cmd/6l/obj.c
@@ -560,6 +560,7 @@
 	}
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	u->link = curauto;
 	curauto = u;
 	u->asym = s;
@@ -644,7 +645,9 @@
 	int i, j, k;
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	s = malloc(sizeof(Sym));
+	memset(s, 0, sizeof(Sym));
 	s->name = malloc(2*(histfrogp+1) + 1);
 
 	u->asym = s;
@@ -654,6 +657,7 @@
 	curhist = u;
 
 	j = 1;
+	s->name[0] = 0;
 	for(i=0; i<histfrogp; i++) {
 		k = histfrog[i]->value;
 		s->name[j+0] = k>>8;
@@ -660,6 +664,8 @@
 		s->name[j+1] = k;
 		j += 2;
 	}
+	s->name[j+0] = 0;
+	s->name[j+1] = 0;
 }
 
 void
@@ -850,7 +856,7 @@
 		goto loop;
 	}
 
-	p = malloc(sizeof(Prog));
+	p = prg();
 	p->as = o;
 	p->line = bloc[2] | (bloc[3] << 8) | (bloc[4] << 16) | (bloc[5] << 24);
 	p->back = 2;
@@ -1137,15 +1143,12 @@
 			return s;
 
 	s = malloc(sizeof(Sym));
-	s->name = malloc(l + 1);
+	memset(s, 0, sizeof(Sym));
+	s->name = malloc(l);
 	memmove(s->name, symb, l);
 
 	s->link = hash[h];
-	s->type = 0;
 	s->version = v;
-	s->value = 0;
-	s->sig = 0;
-	s->dupok = 0;
 	hash[h] = s;
 	nsymbol++;
 	return s;
--- a/sys/src/cmd/7l/obj.c
+++ b/sys/src/cmd/7l/obj.c
@@ -491,6 +491,7 @@
 		}
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	u->link = curauto;
 	curauto = u;
 	u->asym = s;
@@ -660,7 +661,9 @@
 	int i, j, k;
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	s = malloc(sizeof(Sym));
+	memset(s, 0, sizeof(Sym));
 	s->name = malloc(2*(histfrogp+1) + 1);
 
 	u->asym = s;
@@ -670,6 +673,7 @@
 	curhist = u;
 
 	j = 1;
+	s->name[0] = 0;
 	for(i=0; i<histfrogp; i++) {
 		k = histfrog[i]->value;
 		s->name[j+0] = k>>8;
@@ -676,6 +680,8 @@
 		s->name[j+1] = k;
 		j += 2;
 	}
+	s->name[j+0] = 0;
+	s->name[j+1] = 0;
 }
 
 void
@@ -785,7 +791,7 @@
 		goto loop;
 	}
 
-	p = malloc(sizeof(Prog));
+	p = prg();
 	p->as = o;
 	p->reg = bloc[2] & 0x3F;
 	if(bloc[2] & 0x80)
@@ -1116,15 +1122,12 @@
 			return s;
 
 	s = malloc(sizeof(Sym));
+	memset(s, 0, sizeof(Sym));
 	s->name = malloc(l);
 	memmove(s->name, symb, l);
 
 	s->link = hash[h];
-	s->type = 0;
 	s->version = v;
-	s->value = 0;
-	s->sig = 0;
-	s->dupok = 0;
 	hash[h] = s;
 	return s;
 }
--- a/sys/src/cmd/8l/obj.c
+++ b/sys/src/cmd/8l/obj.c
@@ -548,6 +548,7 @@
 	}
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	u->link = curauto;
 	curauto = u;
 	u->asym = s;
@@ -632,7 +633,9 @@
 	int i, j, k;
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	s = malloc(sizeof(Sym));
+	memset(s, 0, sizeof(Sym));
 	s->name = malloc(2*(histfrogp+1) + 1);
 
 	u->asym = s;
@@ -642,6 +645,7 @@
 	curhist = u;
 
 	j = 1;
+	s->name[0] = 0;
 	for(i=0; i<histfrogp; i++) {
 		k = histfrog[i]->value;
 		s->name[j+0] = k>>8;
@@ -648,6 +652,8 @@
 		s->name[j+1] = k;
 		j += 2;
 	}
+	s->name[j+0] = 0;
+	s->name[j+1] = 0;
 }
 
 void
@@ -837,7 +843,7 @@
 		goto loop;
 	}
 
-	p = malloc(sizeof(Prog));
+	p = prg();
 	p->as = o;
 	p->line = bloc[2] | (bloc[3] << 8) | (bloc[4] << 16) | (bloc[5] << 24);
 	p->back = 2;
@@ -1112,15 +1118,12 @@
 			return s;
 
 	s = malloc(sizeof(Sym));
-	s->name = malloc(l + 1);
+	memset(s, 0, sizeof(Sym));
+	s->name = malloc(l);
 	memmove(s->name, symb, l);
 
 	s->link = hash[h];
-	s->type = 0;
 	s->version = v;
-	s->value = 0;
-	s->sig = 0;
-	s->dupok = 0;
 	hash[h] = s;
 	nsymbol++;
 	return s;
--- a/sys/src/cmd/kl/obj.c
+++ b/sys/src/cmd/kl/obj.c
@@ -417,7 +417,7 @@
 		}
 
 	u = malloc(sizeof(Auto));
-
+	memset(u, 0, sizeof(Auto));
 	u->link = curauto;
 	curauto = u;
 	u->asym = s;
@@ -503,7 +503,9 @@
 	int i, j, k;
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	s = malloc(sizeof(Sym));
+	memset(s, 0, sizeof(Sym));
 	s->name = malloc(2*(histfrogp+1) + 1);
 
 	u->asym = s;
@@ -513,6 +515,7 @@
 	curhist = u;
 
 	j = 1;
+	s->name[0] = 0;
 	for(i=0; i<histfrogp; i++) {
 		k = histfrog[i]->value;
 		s->name[j+0] = k>>8;
@@ -519,6 +522,8 @@
 		s->name[j+1] = k;
 		j += 2;
 	}
+	s->name[j+0] = 0;
+	s->name[j+1] = 0;
 }
 
 void
@@ -682,7 +687,7 @@
 		goto loop;
 	}
 
-	p = malloc(sizeof(Prog));
+	p = prg();
 	p->as = o;
 	p->reg = bloc[1] & 0x7f;
 	if(bloc[1] & 0x80)
@@ -935,14 +940,12 @@
 			return s;
 
 	s = malloc(sizeof(Sym));
-	s->name = malloc(l + 1);
+	memset(s, 0, sizeof(Sym));
+	s->name = malloc(l);
 	memmove(s->name, symb, l);
 
 	s->link = hash[h];
-	s->type = 0;
 	s->version = v;
-	s->value = 0;
-	s->dupok = 0;
 	hash[h] = s;
 	return s;
 }
--- a/sys/src/cmd/ql/obj.c
+++ b/sys/src/cmd/ql/obj.c
@@ -500,7 +500,7 @@
 		}
 
 	u = malloc(sizeof(Auto));
-
+	memset(u, 0, sizeof(Auto));
 	u->link = curauto;
 	curauto = u;
 	u->sym = s;
@@ -586,7 +586,9 @@
 	int i, j, k;
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	s = malloc(sizeof(Sym));
+	memset(s, 0, sizeof(Sym));
 	s->name = malloc(2*(histfrogp+1) + 1);
 
 	u->sym = s;
@@ -596,6 +598,7 @@
 	curhist = u;
 
 	j = 1;
+	s->name[0] = 0;
 	for(i=0; i<histfrogp; i++) {
 		k = histfrog[i]->value;
 		s->name[j+0] = k>>8;
@@ -602,6 +605,8 @@
 		s->name[j+1] = k;
 		j += 2;
 	}
+	s->name[j+0] = 0;
+	s->name[j+1] = 0;
 }
 
 void
@@ -790,7 +795,7 @@
 		goto loop;
 	}
 
-	p = malloc(sizeof(Prog));
+	p = prg();
 	p->as = o;
 	p->reg = bloc[2] & 0x3f;
 	if(bloc[2] & 0x80)
@@ -1068,15 +1073,12 @@
 			return s;
 
 	s = malloc(sizeof(Sym));
-	s->name = malloc(l + 1);
+	memset(s, 0, sizeof(Sym));
+	s->name = malloc(l);
 	memmove(s->name, symb, l);
 
 	s->link = hash[h];
-	s->type = 0;
 	s->version = v;
-	s->value = 0;
-	s->sig = 0;
-	s->dupok = 0;
 	hash[h] = s;
 	return s;
 }
--- a/sys/src/cmd/tl/l.h
+++ b/sys/src/cmd/tl/l.h
@@ -278,7 +278,6 @@
 EXTERN	char*	libraryobj[50];
 EXTERN	int	libraryp;
 EXTERN	int	xrefresolv;
-EXTERN	char*	hunk;
 EXTERN	char	inuxi1[1];
 EXTERN	char	inuxi2[2];
 EXTERN	char	inuxi4[4];
@@ -286,7 +285,6 @@
 EXTERN	long	lcsize;
 EXTERN	char	literal[32];
 EXTERN	int	nerrors;
-EXTERN	long	nhunk;
 EXTERN	long	instoffset;
 EXTERN	Opcross	opcross[8];
 EXTERN	Oprang	oprange[ALAST];
@@ -297,7 +295,6 @@
 EXTERN	long	symsize;
 EXTERN	Prog*	textp;
 EXTERN	long	textsize;
-EXTERN	long	thunk;
 EXTERN	int	version;
 EXTERN	char	xcmp[C_GOK+1][C_GOK+1];
 EXTERN	Prog	zprg;
@@ -377,7 +374,6 @@
 void	export(void);
 int	find1(long, int);
 void	follow(void);
-void	gethunk(void);
 void	histtoauto(void);
 void	hputl(int);
 double	ieeedtod(Ieee*);
--- a/sys/src/cmd/tl/obj.c
+++ b/sys/src/cmd/tl/obj.c
@@ -279,7 +279,6 @@
 	}
 	if(debug['v']) {
 		Bprint(&bso, "%5.2f cpu time\n", cputime());
-		Bprint(&bso, "%ld memory used\n", thunk);
 		Bprint(&bso, "%d sizeof adr\n", sizeof(Adr));
 		Bprint(&bso, "%d sizeof prog\n", sizeof(Prog));
 	}
@@ -496,23 +495,13 @@
 		break;
 
 	case D_SCONST:
-		while(nhunk < NSNAME)
-			gethunk();
-		a->sval = (char*)hunk;
-		nhunk -= NSNAME;
-		hunk += NSNAME;
-
+		a->sval = malloc(NSNAME);
 		memmove(a->sval, p+4, NSNAME);
 		c += NSNAME;
 		break;
 
 	case D_FCONST:
-		while(nhunk < sizeof(Ieee))
-			gethunk();
-		a->ieee = (Ieee*)hunk;
-		nhunk -= NSNAME;
-		hunk += NSNAME;
-
+		a->ieee = malloc(sizeof(Ieee));
 		a->ieee->l = p[4] | (p[5]<<8) |
 			(p[6]<<16) | (p[7]<<24);
 		a->ieee->h = p[8] | (p[9]<<8) |
@@ -536,12 +525,8 @@
 			return c;
 		}
 
-	while(nhunk < sizeof(Auto))
-		gethunk();
-	u = (Auto*)hunk;
-	nhunk -= sizeof(Auto);
-	hunk += sizeof(Auto);
-
+	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	u->link = curauto;
 	curauto = u;
 	u->asym = s;
@@ -626,7 +611,9 @@
 	int i, j, k;
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	s = malloc(sizeof(Sym));
+	memset(s, 0, sizeof(Sym));
 	s->name = malloc(2*(histfrogp+1) + 1);
 
 	u->asym = s;
@@ -636,6 +623,7 @@
 	curhist = u;
 
 	j = 1;
+	s->name[0] = 0;
 	for(i=0; i<histfrogp; i++) {
 		k = histfrog[i]->value;
 		s->name[j+0] = k>>8;
@@ -642,6 +630,8 @@
 		s->name[j+1] = k;
 		j += 2;
 	}
+	s->name[j+0] = 0;
+	s->name[j+1] = 0;
 }
 
 void
@@ -828,12 +818,7 @@
 		goto loop;
 	}
 
-	if(nhunk < sizeof(Prog))
-		gethunk();
-	p = (Prog*)hunk;
-	nhunk -= sizeof(Prog);
-	hunk += sizeof(Prog);
-
+	p = prg();
 	p->as = o;
 	p->scond = bloc[1];
 	p->reg = bloc[2];
@@ -1138,22 +1123,13 @@
 		if(memcmp(s->name, symb, l) == 0)
 			return s;
 
-	while(nhunk < sizeof(Sym))
-		gethunk();
-	s = (Sym*)hunk;
-	nhunk -= sizeof(Sym);
-	hunk += sizeof(Sym);
-
+	s = malloc(sizeof(Sym));
+	memset(s, 0, sizeof(Sym));
 	s->name = malloc(l);
 	memmove(s->name, symb, l);
 
 	s->link = hash[h];
-	s->type = 0;
 	s->version = v;
-	s->value = 0;
-	s->sig = 0;
-	s->used = s->thumb = s->foreign = s->fnptr = 0;
-	s->use = nil;
 	hash[h] = s;
 	return s;
 }
@@ -1161,38 +1137,9 @@
 Prog*
 prg(void)
 {
-	Prog *p;
-
-	while(nhunk < sizeof(Prog))
-		gethunk();
-	p = (Prog*)hunk;
-	nhunk -= sizeof(Prog);
-	hunk += sizeof(Prog);
-
+	Prog *p = malloc(sizeof(Prog));
 	*p = zprg;
 	return p;
-}
-
-void
-gethunk(void)
-{
-	char *h;
-	long nh;
-
-	nh = NHUNK;
-	if(thunk >= 5L*NHUNK) {
-		nh = 5L*NHUNK;
-		if(thunk >= 25L*NHUNK)
-			nh = 25L*NHUNK;
-	}
-	h = malloc(nh);
-	if(h == (char*)-1) {
-		diag("out of memory");
-		errorexit();
-	}
-	hunk = h;
-	nhunk = nh;
-	thunk += nh;
 }
 
 void
--- a/sys/src/cmd/vl/obj.c
+++ b/sys/src/cmd/vl/obj.c
@@ -469,6 +469,7 @@
 		}
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	u->link = curauto;
 	curauto = u;
 	u->asym = s;
@@ -553,7 +554,9 @@
 	int i, j, k;
 
 	u = malloc(sizeof(Auto));
+	memset(u, 0, sizeof(Auto));
 	s = malloc(sizeof(Sym));
+	memset(s, 0, sizeof(Sym));
 	s->name = malloc(2*(histfrogp+1) + 1);
 
 	u->asym = s;
@@ -563,6 +566,7 @@
 	curhist = u;
 
 	j = 1;
+	s->name[0] = 0;
 	for(i=0; i<histfrogp; i++) {
 		k = histfrog[i]->value;
 		s->name[j+0] = k>>8;
@@ -569,6 +573,8 @@
 		s->name[j+1] = k;
 		j += 2;
 	}
+	s->name[j+0] = 0;
+	s->name[j+1] = 0;
 }
 
 void
@@ -732,7 +738,7 @@
 		goto loop;
 	}
 
-	p = malloc(sizeof(Prog));
+	p = prg();
 	p->as = o;
 	p->reg = bloc[1] & 0x7f;
 	if(bloc[1] & 0x80)
@@ -1005,14 +1011,12 @@
 			return s;
 
 	s = malloc(sizeof(Sym));
+	memset(s, 0, sizeof(Sym));
 	s->name = malloc(l);
 	memmove(s->name, symb, l);
 
 	s->link = hash[h];
-	s->type = 0;
 	s->version = v;
-	s->value = 0;
-	s->dupok = 0;
 	hash[h] = s;
 	return s;
 }