diff options
| author | Robert Morris <rtm@csail.mit.edu> | 2020-08-07 15:06:43 -0400 | 
|---|---|---|
| committer | Frans Kaashoek <kaashoek@mit.edu> | 2020-08-10 11:19:10 -0400 | 
| commit | e3b7058907dff779cf94e23bf6bb84245faf481d (patch) | |
| tree | 897b1539a46d30b14a85965a9e90ecdb0286bcfe | |
| parent | a93321cb2547dbb48bf8ce9ad623ac19eefbecea (diff) | |
| download | xv6-labs-e3b7058907dff779cf94e23bf6bb84245faf481d.tar.gz xv6-labs-e3b7058907dff779cf94e23bf6bb84245faf481d.tar.bz2 xv6-labs-e3b7058907dff779cf94e23bf6bb84245faf481d.zip | |
streamline copyin/copyout code in usertests
fix bugs in read/write return values when there's an error
| -rw-r--r-- | kernel/console.c | 2 | ||||
| -rw-r--r-- | kernel/fs.c | 2 | ||||
| -rw-r--r-- | kernel/pipe.c | 2 | ||||
| -rw-r--r-- | user/usertests.c | 157 | 
4 files changed, 86 insertions, 77 deletions
| diff --git a/kernel/console.c b/kernel/console.c index 9a18cd9..2b1ed3c 100644 --- a/kernel/console.c +++ b/kernel/console.c @@ -76,7 +76,7 @@ consolewrite(int user_src, uint64 src, int n)    }    release(&cons.lock); -  return n; +  return i;  }  // diff --git a/kernel/fs.c b/kernel/fs.c index e33ec30..ec68cd7 100644 --- a/kernel/fs.c +++ b/kernel/fs.c @@ -472,7 +472,7 @@ readi(struct inode *ip, int user_dst, uint64 dst, uint off, uint n)      }      brelse(bp);    } -  return n; +  return tot;  }  // Write data to inode. diff --git a/kernel/pipe.c b/kernel/pipe.c index c066afb..7ed402d 100644 --- a/kernel/pipe.c +++ b/kernel/pipe.c @@ -96,7 +96,7 @@ pipewrite(struct pipe *pi, uint64 addr, int n)    }    wakeup(&pi->nread);    release(&pi->lock); -  return n; +  return i;  }  int diff --git a/user/usertests.c b/user/usertests.c index bdf6970..8eb4aab 100644 --- a/user/usertests.c +++ b/user/usertests.c @@ -23,88 +23,100 @@ char buf[BUFSZ];  char name[3];  void -copyin1(char *s) +copyin(char *s)  { -  int fd = open("copyin1", O_CREATE|O_WRONLY); -  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"); -} +  uint64 addrs[] = { 0x80000000LL, 0xffffffffffffffff }; -void -copyin2(char *s) -{ -  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); +  for(int ai = 0; ai < 2; ai++){ +    uint64 addr = addrs[ai]; +     +    int fd = open("copyin1", O_CREATE|O_WRONLY); +    if(fd < 0){ +      printf("open(copyin1) failed\n"); +      exit(1); +    } +    int n = write(fd, (void*)addr, 8192); +    if(n >= 0){ +      printf("write(fd, %p, 8192) returned %d, not -1\n", addr, n); +      exit(1); +    } +    close(fd); +    unlink("copyin1"); +     +    n = write(1, (char*)addr, 8192); +    if(n > 0){ +      printf("write(1, %p, 8192) returned %d, not -1 or 0\n", addr, n); +      exit(1); +    } +     +    int fds[2]; +    if(pipe(fds) < 0){ +      printf("pipe() failed\n"); +      exit(1); +    } +    n = write(fds[1], (char*)addr, 8192); +    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]);    } -  close(fd); -  unlink("copyin2");  }  void -copyout1(char *s) +copyout(char *s)  { -  int fd = open("README", 0); -  if(fd < 0){ -    printf("open(README) failed\n"); -    exit(1); -  } -  int n = read(fd, (void*)0x80000000LL, 8192); -  if(n >= 0){ -    printf("read(fd, 0x80000000LL, 8192) returned %d, not -1\n", n); -    exit(1); -  } -  close(fd); -} +  uint64 addrs[] = { 0x80000000LL, 0xffffffffffffffff }; -void -copyout2(char *s) -{ -  int fd = open("README", 0); -  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); -} +  for(int ai = 0; ai < 2; ai++){ +    uint64 addr = addrs[ai]; -void -copyinstr1(char *s) -{ -  int fd = open((char *)0x80000000LL, O_CREATE|O_WRONLY); -  if(fd >= 0){ -    printf("open(0x80000000) returned %d, not -1\n", fd); -    exit(1); +    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 -copyinstr2(char *s) +copyinstr(char *s)  { -  int fd = open((char *)0xffffffffffffffff, O_CREATE|O_WRONLY); -  if(fd >= 0){ -    printf("open(0xffffffffffffffff) returned %d, not -1\n", fd); -    exit(1); +  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 *);      char *s;    } tests[] = { -    {copyin1, "copyin1"}, -    {copyin2, "copyin2"}, -    {copyout1, "copyout1"}, -    {copyout2, "copyout2"}, -    {copyinstr1, "copyinstr1"}, -    {copyinstr2, "copyinstr2"}, +    {copyin, "copyin"}, +    {copyout, "copyout"}, +    {copyinstr, "copyinstr"},      {truncate1, "truncate1"},      {truncate2, "truncate2"},      {truncate3, "truncate3"}, | 
