shithub: scc

Download patch

ref: 9bef6a793a84d5cb723d0c45fdd78e906e63dd98
parent: 048182fd990731f3ca1bc6fd9c551bae62e9eda0
author: Roberto E. Vargas Caballero <[email protected]>
date: Fri Sep 8 14:31:47 EDT 2017

[as] Add address overflow check

--- a/as/as.h
+++ b/as/as.h
@@ -55,3 +55,4 @@
 extern Ins instab[];
 extern Op optab[];
 extern int pass;
+extern TUINT maxaddr;
--- a/as/main.c
+++ b/as/main.c
@@ -50,6 +50,7 @@
 	Ins *ins;
 	Op *op, *lim;
 	Arg *args;
+	TUINT pc, curpc;
 	
 	ins = bsearch(text, instab, nr_ins, sizeof(Ins), cmp);
 
@@ -69,10 +70,25 @@
 		return;
 	}
 	(*op->format)(op, args);
+
+	pc = cursec->pc;
+	curpc = cursec->curpc;
+
 	cursec->curpc += op->size;
 	cursec->pc += op->size;
+
+	if (pass == 2)
+		return;
+
 	if (cursec->pc > cursec->max)
 		cursec->max = cursec->pc;
+
+	if (pc > cursec->pc ||
+	    curpc > cursec->curpc ||
+	    cursec->curpc > maxaddr ||
+	    cursec->pc > maxaddr) {
+		die("address overflow");
+	}
 }
 
 int
--- a/as/target/i386/ins.c
+++ b/as/target/i386/ins.c
@@ -3,6 +3,8 @@
 #include "../../as.h"
 #include "ins.h"
 
+TUINT maxaddr = ((TUINT) 1 << 32) -1;
+
 void
 direct(Op *op, Arg *args)
 {