streamline copyin/copyout code in usertests

fix bugs in read/write return values when there's an error
This commit is contained in:
Robert Morris 2020-08-07 15:06:43 -04:00 committed by Frans Kaashoek
parent a93321cb25
commit e3b7058907
4 changed files with 93 additions and 84 deletions

View file

@ -76,7 +76,7 @@ consolewrite(int user_src, uint64 src, int n)
} }
release(&cons.lock); release(&cons.lock);
return n; return i;
} }
// //

View file

@ -472,7 +472,7 @@ readi(struct inode *ip, int user_dst, uint64 dst, uint off, uint n)
} }
brelse(bp); brelse(bp);
} }
return n; return tot;
} }
// Write data to inode. // Write data to inode.

View file

@ -96,7 +96,7 @@ pipewrite(struct pipe *pi, uint64 addr, int n)
} }
wakeup(&pi->nread); wakeup(&pi->nread);
release(&pi->lock); release(&pi->lock);
return n; return i;
} }
int int

View file

@ -23,88 +23,100 @@ char buf[BUFSZ];
char name[3]; char name[3];
void void
copyin1(char *s) copyin(char *s)
{ {
int fd = open("copyin1", O_CREATE|O_WRONLY); uint64 addrs[] = { 0x80000000LL, 0xffffffffffffffff };
if(fd < 0){
printf("open(copyin1) failed\n");
exit(1);
}
int n = write(fd, (void*)0x80000000LL, 8192);
if(n >= 0){
printf("write(fd, 0x80000000LL, 8192) did not fail!\n");
exit(1);
}
close(fd);
unlink("copyin1");
}
void for(int ai = 0; ai < 2; ai++){
copyin2(char *s) uint64 addr = addrs[ai];
{
int fd = open("copyin2", O_CREATE|O_WRONLY);
if(fd < 0){
printf("open(copyin2) failed\n");
exit(1);
}
int n = write(fd, (void*)0xffffffffffffffffLL, 8192);
if(n >= 0){
printf("write(fd, 0xffffffffffffffffLL, 8192) did not fail!\n");
exit(1);
}
close(fd);
unlink("copyin2");
}
void int fd = open("copyin1", O_CREATE|O_WRONLY);
copyout1(char *s) if(fd < 0){
{ printf("open(copyin1) failed\n");
int fd = open("README", 0); exit(1);
if(fd < 0){ }
printf("open(README) failed\n"); int n = write(fd, (void*)addr, 8192);
exit(1); if(n >= 0){
} printf("write(fd, %p, 8192) returned %d, not -1\n", addr, n);
int n = read(fd, (void*)0x80000000LL, 8192); exit(1);
if(n >= 0){ }
printf("read(fd, 0x80000000LL, 8192) returned %d, not -1\n", n); close(fd);
exit(1); unlink("copyin1");
}
close(fd);
}
void n = write(1, (char*)addr, 8192);
copyout2(char *s) if(n > 0){
{ printf("write(1, %p, 8192) returned %d, not -1 or 0\n", addr, n);
int fd = open("README", 0); exit(1);
if(fd < 0){ }
printf("open(README) failed\n");
exit(1);
}
int n = read(fd, (void*)0xffffffffffffffffLL, 8192);
if(n >= 0){
printf("read(fd, 0xffffffffffffffff, 8192) returned %d, not -1\n", n);
exit(1);
}
close(fd);
}
void int fds[2];
copyinstr1(char *s) if(pipe(fds) < 0){
{ printf("pipe() failed\n");
int fd = open((char *)0x80000000LL, O_CREATE|O_WRONLY); exit(1);
if(fd >= 0){ }
printf("open(0x80000000) returned %d, not -1\n", fd); n = write(fds[1], (char*)addr, 8192);
exit(1); if(n > 0){
printf("write(pipe, %p, 8192) returned %d, not -1 or 0\n", addr, n);
exit(1);
}
close(fds[0]);
close(fds[1]);
} }
} }
void void
copyinstr2(char *s) copyout(char *s)
{ {
int fd = open((char *)0xffffffffffffffff, O_CREATE|O_WRONLY); uint64 addrs[] = { 0x80000000LL, 0xffffffffffffffff };
if(fd >= 0){
printf("open(0xffffffffffffffff) returned %d, not -1\n", fd); for(int ai = 0; ai < 2; ai++){
exit(1); uint64 addr = addrs[ai];
int fd = open("README", 0);
if(fd < 0){
printf("open(README) failed\n");
exit(1);
}
int n = read(fd, (void*)addr, 8192);
if(n > 0){
printf("read(fd, %p, 8192) returned %d, not -1 or 0\n", addr, n);
exit(1);
}
close(fd);
int fds[2];
if(pipe(fds) < 0){
printf("pipe() failed\n");
exit(1);
}
n = write(fds[1], "x", 1);
if(n != 1){
printf("pipe write failed\n");
exit(1);
}
n = read(fds[0], (void*)addr, 8192);
if(n > 0){
printf("read(pipe, %p, 8192) returned %d, not -1 or 0\n", addr, n);
exit(1);
}
close(fds[0]);
close(fds[1]);
}
}
void
copyinstr(char *s)
{
uint64 addrs[] = { 0x80000000LL, 0xffffffffffffffff };
for(int ai = 0; ai < 2; ai++){
uint64 addr = addrs[ai];
int fd = open((char *)addr, O_CREATE|O_WRONLY);
if(fd >= 0){
printf("open(%p) returned %d, not -1\n", addr, fd);
exit(1);
}
} }
} }
@ -2393,12 +2405,9 @@ main(int argc, char *argv[])
void (*f)(char *); void (*f)(char *);
char *s; char *s;
} tests[] = { } tests[] = {
{copyin1, "copyin1"}, {copyin, "copyin"},
{copyin2, "copyin2"}, {copyout, "copyout"},
{copyout1, "copyout1"}, {copyinstr, "copyinstr"},
{copyout2, "copyout2"},
{copyinstr1, "copyinstr1"},
{copyinstr2, "copyinstr2"},
{truncate1, "truncate1"}, {truncate1, "truncate1"},
{truncate2, "truncate2"}, {truncate2, "truncate2"},
{truncate3, "truncate3"}, {truncate3, "truncate3"},