ref: 6d98d1414c11322eb919530405aebce01ee51c6f
parent: f6e3939a23bcb8bfc53baf6bb3231ea5afbd44d9
author: Ori Bernstein <[email protected]>
date: Sun Sep 14 19:15:44 EDT 2014
Add checking for buffer overruns.
--- a/6/isel.c
+++ b/6/isel.c
@@ -422,7 +422,7 @@
}
-static int isfunc(Isel *s, Node *n)
+static int isconstfunc(Isel *s, Node *n)
{
Node *d;
@@ -441,7 +441,7 @@
AsmOp op;
Loc *f;
- if (isfunc(s, n)) {
+ if (isconstfunc(s, n)) {
op = Icall;
f = locmeml(htget(s->globls, n), NULL, NULL, mode(n));
} else {
@@ -696,7 +696,7 @@
r = loc(s, n);
break;
case Ovar:
- if (isfunc(s, n)) {
+ if (isconstfunc(s, n)) {
r = locreg(ModeQ);
a = loc(s, n);
g(s, Ilea, a, r, NULL);
--- a/6/main.c
+++ b/6/main.c
@@ -6,6 +6,7 @@
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <err.h>
@@ -56,6 +57,7 @@
{
char *tmpdir;
char *base;
+ struct timeval tv;
tmpdir = getenv("TMPDIR");
if (!tmpdir)
@@ -65,7 +67,9 @@
base++;
else
base = path;
- snprintf(buf, bufsz, "%s/tmp%lx-%s%s", tmpdir, random(), base, suffix);
+ gettimeofday(&tv, NULL);
+ srandom(tv.tv_usec);
+ snprintf(buf, bufsz, "%s/tmp%lx%lx-%s%s", tmpdir, random(), (long)tv.tv_usec, base, suffix);
return buf;
}
@@ -88,7 +92,6 @@
int i;
Stab *globls;
char buf[1024];
-
while ((opt = getopt(argc, argv, "d:hSo:I:")) != -1) {
switch (opt) {
case 'o':
--- a/6/simp.c
+++ b/6/simp.c
@@ -68,6 +68,7 @@
static Type *tyintptr;
static Type *tyword;
static Type *tyvoid;
+static Node *abortfunc;
size_t alignto(size_t sz, Type *t)
{
@@ -859,9 +860,29 @@
return r;
}
+static void checkidx(Simp *s, Node *len, Node *idx)
+{
+ Node *cmp, *die;
+ Node *ok, *fail;
+
+ /* create expressions */
+ cmp = mkexpr(idx->line, Olt, ptrsized(s, idx), ptrsized(s, len), NULL);
+ cmp->expr.type = mktype(-1, Tybool);
+ ok = genlbl();
+ fail = genlbl();
+ die = mkexpr(idx->line, Ocall, abortfunc, NULL);
+ die->expr.type = mktype(-1, Tyvoid);
+
+ /* insert them */
+ cjmp(s, cmp, ok, fail);
+ append(s, fail);
+ append(s, die);
+ append(s, ok);
+}
+
static Node *idxaddr(Simp *s, Node *seq, Node *idx)
{
- Node *a, *t, *u, *v; /* temps */
+ Node *a, *t, *u, *v, *w; /* temps */
Node *r; /* result */
Type *ty;
size_t sz;
@@ -868,15 +889,19 @@
a = rval(s, seq, NULL);
ty = exprtype(seq)->sub[0];
- if (exprtype(seq)->type == Tyarray)
+ if (exprtype(seq)->type == Tyarray) {
t = addr(s, a, ty);
- else if (seq->expr.type->type == Tyslice)
+ w = exprtype(a)->asize;
+ } else if (seq->expr.type->type == Tyslice) {
t = load(addr(s, a, mktyptr(seq->line, ty)));
- else
+ w = slicelen(s, a);
+ } else {
die("Can't index type %s\n", tystr(seq->expr.type));
+ }
assert(t->expr.type->type == Typtr);
u = rval(s, idx, NULL);
u = ptrsized(s, u);
+ checkidx(s, w, u);
sz = tysize(ty);
v = mul(u, disp(seq->line, sz));
r = add(t, v);
@@ -1813,6 +1838,29 @@
free(name);
}
+static void initconsts(Htab *globls)
+{
+ Type *ty;
+ Node *name;
+ Node *dcl;
+
+ tyintptr = mktype(-1, Tyuint64);
+ tyword = mktype(-1, Tyuint);
+ tyvoid = mktype(-1, Tyvoid);
+
+ ty = mktyfunc(-1, NULL, 0, mktype(-1, Tyvoid));
+ name = mknsname(-1, "_rt", "abort_oob");
+ dcl = mkdecl(-1, name, ty);
+ dcl->decl.isconst = 1;
+ dcl->decl.isextern = 1;
+ htput(globls, dcl, asmname(dcl->decl.name));
+
+ abortfunc = mkexpr(-1, Ovar, name, NULL);
+ abortfunc->expr.type = ty;
+ abortfunc->expr.did = dcl->decl.did;
+ abortfunc->expr.isconst = 1;
+}
+
void gen(Node *file, char *out)
{
Htab *globls, *strtab;
@@ -1822,16 +1870,12 @@
size_t i;
FILE *fd;
- /* declare useful constants */
- tyintptr = mktype(-1, Tyuint64);
- tyword = mktype(-1, Tyuint);
- tyvoid = mktype(-1, Tyvoid);
-
fn = NULL;
nfn = 0;
blob = NULL;
nblob = 0;
globls = mkht(varhash, vareq);
+ initconsts(globls);
/* We need to define all global variables before use */
fillglobls(file->file.globls, globls);
--- a/rt/Makefile
+++ b/rt/Makefile
@@ -1,5 +1,5 @@
OBJ = _myrrt.o
-ASMSRC = start.s common.s
+ASMSRC = start.s common.s abort.s
all: _myrrt.o