From 62b3eea2715a9e67cdb0873faa0d802344bf7683 Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Tue, 24 Sep 2013 01:52:20 +0200 Subject: [PATCH] syssem*: eleminate redundant validaddr() checks validaddr looks up the segments for an address range and checks the flags and if the address range lies within bounds on the segments. as we'r going to lookup the segment in the syssem* syscalls anyway, we can do the checks ourselfs avoiding the double segment array lookups. the implication of this tho is that now a semaphore cannot span multiple segments. but this would be highly unusual given that segments are page aligned. --- sys/src/9/port/sysproc.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/sys/src/9/port/sysproc.c b/sys/src/9/port/sysproc.c index 9f670a5a0..fbad6942f 100644 --- a/sys/src/9/port/sysproc.c +++ b/sys/src/9/port/sysproc.c @@ -1109,13 +1109,15 @@ syssemacquire(ulong *arg) long *addr; Segment *s; - validaddr(arg[0], sizeof(long), 1); evenaddr(arg[0]); addr = (long*)arg[0]; block = arg[1]; - - if((s = seg(up, (ulong)addr, 0)) == nil) + + s = seg(up, (ulong)addr, 0); + if(s == nil || (s->type&SG_RONLY) != 0 || (ulong)addr+sizeof(long) > s->top){ + validaddr((ulong)addr, sizeof(long), 1); error(Ebadarg); + } if(*addr < 0) error(Ebadarg); return semacquire(s, addr, block); @@ -1128,13 +1130,15 @@ systsemacquire(ulong *arg) ulong ms; Segment *s; - validaddr(arg[0], sizeof(long), 1); evenaddr(arg[0]); addr = (long*)arg[0]; ms = arg[1]; - if((s = seg(up, (ulong)addr, 0)) == nil) + s = seg(up, (ulong)addr, 0); + if(s == nil || (s->type&SG_RONLY) != 0 || (ulong)addr+sizeof(long) > s->top){ + validaddr((ulong)addr, sizeof(long), 1); error(Ebadarg); + } if(*addr < 0) error(Ebadarg); return tsemacquire(s, addr, ms); @@ -1146,13 +1150,15 @@ syssemrelease(ulong *arg) long *addr, delta; Segment *s; - validaddr(arg[0], sizeof(long), 1); evenaddr(arg[0]); addr = (long*)arg[0]; delta = arg[1]; - if((s = seg(up, (ulong)addr, 0)) == nil) + s = seg(up, (ulong)addr, 0); + if(s == nil || (s->type&SG_RONLY) != 0 || (ulong)addr+sizeof(long) > s->top){ + validaddr((ulong)addr, sizeof(long), 1); error(Ebadarg); + } /* delta == 0 is a no-op, not a release */ if(delta < 0 || *addr < 0) error(Ebadarg);