user/usertests.c

3248 lines
1#include "kernel/param.h"2#include "kernel/types.h"3#include "kernel/stat.h"4#include "user/user.h"5#include "kernel/fs.h"6#include "kernel/fcntl.h"7#include "kernel/syscall.h"8#include "kernel/memlayout.h"9#include "kernel/riscv.h"1011//12// Tests xv6 system calls.  usertests without arguments runs them all13// and usertests <name> runs <name> test. The test runner creates for14// each test a process and based on the exit status of the process,15// the test runner reports "OK" or "FAILED".  Some tests result in16// kernel printing usertrap messages, which can be ignored if test17// prints "OK".18//1920#define BUFSZ  ((MAXOPBLOCKS+2)*BSIZE)2122char buf[BUFSZ];2324//25// Section with tests that run fairly quickly.  Use -q if you want to26// run just those.  Without -q usertests also runs the ones that take a27// fair amount of time.28//2930// what if you pass ridiculous pointers to system calls31// that read user memory with copyin?32void33copyin(char *s)34{35  uint64 addrs[] = { 0x80000000LL, 0x3fffffe000, 0x3ffffff000, 0x4000000000,36                     0xffffffffffffffff };3738  for(int ai = 0; ai < sizeof(addrs)/sizeof(addrs[0]); ai++){39    uint64 addr = addrs[ai];40    41    int fd = open("copyin1", O_CREATE|O_WRONLY);42    if(fd < 0){43      printf("open(copyin1) failed\n");44      exit(1);45    }46    int n = write(fd, (void*)addr, 8192);47    if(n >= 0){48      printf("write(fd, %p, 8192) returned %d, not -1\n", (void*)addr, n);49      exit(1);50    }51    close(fd);52    unlink("copyin1");53    54    n = write(1, (char*)addr, 8192);55    if(n > 0){56      printf("write(1, %p, 8192) returned %d, not -1 or 0\n", (void*)addr, n);57      exit(1);58    }59    60    int fds[2];61    if(pipe(fds) < 0){62      printf("pipe() failed\n");63      exit(1);64    }65    n = write(fds[1], (char*)addr, 8192);66    if(n > 0){67      printf("write(pipe, %p, 8192) returned %d, not -1 or 0\n", (void*)addr, n);68      exit(1);69    }70    close(fds[0]);71    close(fds[1]);72  }73}7475// what if you pass ridiculous pointers to system calls76// that write user memory with copyout?77void78copyout(char *s)79{80  uint64 addrs[] = { 0LL, 0x80000000LL, 0x3fffffe000, 0x3ffffff000, 0x4000000000,81                     0xffffffffffffffff };8283  for(int ai = 0; ai < sizeof(addrs)/sizeof(addrs[0]); ai++){84    uint64 addr = addrs[ai];8586    int fd = open("README", 0);87    if(fd < 0){88      printf("open(README) failed\n");89      exit(1);90    }91    int n = read(fd, (void*)addr, 8192);92    if(n > 0){93      printf("read(fd, %p, 8192) returned %d, not -1 or 0\n", (void*)addr, n);94      exit(1);95    }96    close(fd);9798    int fds[2];99    if(pipe(fds) < 0){100      printf("pipe() failed\n");101      exit(1);102    }103    n = write(fds[1], "x", 1);104    if(n != 1){105      printf("pipe write failed\n");106      exit(1);107    }108    n = read(fds[0], (void*)addr, 8192);109    if(n > 0){110      printf("read(pipe, %p, 8192) returned %d, not -1 or 0\n", (void*)addr, n);111      exit(1);112    }113    close(fds[0]);114    close(fds[1]);115  }116}117118// what if you pass ridiculous string pointers to system calls?119void120copyinstr1(char *s)121{122  uint64 addrs[] = { 0x80000000LL, 0x3fffffe000, 0x3ffffff000, 0x4000000000,123                     0xffffffffffffffff };124125  for(int ai = 0; ai < sizeof(addrs)/sizeof(addrs[0]); ai++){126    uint64 addr = addrs[ai];127128    int fd = open((char *)addr, O_CREATE|O_WRONLY);129    if(fd >= 0){130      printf("open(%p) returned %d, not -1\n", (void*)addr, fd);131      exit(1);132    }133  }134}135136// what if a string system call argument is exactly the size137// of the kernel buffer it is copied into, so that the null138// would fall just beyond the end of the kernel buffer?139void140copyinstr2(char *s)141{142  char b[MAXPATH+1];143144  for(int i = 0; i < MAXPATH; i++)145    b[i] = 'x';146  b[MAXPATH] = '\0';147  148  int ret = unlink(b);149  if(ret != -1){150    printf("unlink(%s) returned %d, not -1\n", b, ret);151    exit(1);152  }153154  int fd = open(b, O_CREATE | O_WRONLY);155  if(fd != -1){156    printf("open(%s) returned %d, not -1\n", b, fd);157    exit(1);158  }159160  ret = link(b, b);161  if(ret != -1){162    printf("link(%s, %s) returned %d, not -1\n", b, b, ret);163    exit(1);164  }165166  char *args[] = { "xx", 0 };167  ret = exec(b, args);168  if(ret != -1){169    printf("exec(%s) returned %d, not -1\n", b, fd);170    exit(1);171  }172173  int pid = fork();174  if(pid < 0){175    printf("fork failed\n");176    exit(1);177  }178  if(pid == 0){179    static char big[PGSIZE+1];180    for(int i = 0; i < PGSIZE; i++)181      big[i] = 'x';182    big[PGSIZE] = '\0';183    char *args2[] = { big, big, big, 0 };184    ret = exec("echo", args2);185    if(ret != -1){186      printf("exec(echo, BIG) returned %d, not -1\n", fd);187      exit(1);188    }189    exit(747); // OK190  }191192  int st = 0;193  wait(&st);194  if(st != 747){195    printf("exec(echo, BIG) succeeded, should have failed\n");196    exit(1);197  }198}199200// what if a string argument crosses over the end of last user page?201void202copyinstr3(char *s)203{204  sbrk(8192);205  uint64 top = (uint64) sbrk(0);206  if((top % PGSIZE) != 0){207    sbrk(PGSIZE - (top % PGSIZE));208  }209  top = (uint64) sbrk(0);210  if(top % PGSIZE){211    printf("oops\n");212    exit(1);213  }214215  char *b = (char *) (top - 1);216  *b = 'x';217218  int ret = unlink(b);219  if(ret != -1){220    printf("unlink(%s) returned %d, not -1\n", b, ret);221    exit(1);222  }223224  int fd = open(b, O_CREATE | O_WRONLY);225  if(fd != -1){226    printf("open(%s) returned %d, not -1\n", b, fd);227    exit(1);228  }229230  ret = link(b, b);231  if(ret != -1){232    printf("link(%s, %s) returned %d, not -1\n", b, b, ret);233    exit(1);234  }235236  char *args[] = { "xx", 0 };237  ret = exec(b, args);238  if(ret != -1){239    printf("exec(%s) returned %d, not -1\n", b, fd);240    exit(1);241  }242}243244// See if the kernel refuses to read/write user memory that the245// application doesn't have anymore, because it returned it.246void247rwsbrk(char *s)248{249  int fd, n;250  251  uint64 a = (uint64) sbrk(8192);252253  if(a == (uint64) SBRK_ERROR) {254    printf("sbrk(rwsbrk) failed\n");255    exit(1);256  }257  258  if (sbrk(-8192) == SBRK_ERROR) {259    printf("sbrk(rwsbrk) shrink failed\n");260    exit(1);261  }262263  fd = open("rwsbrk", O_CREATE|O_WRONLY);264  if(fd < 0){265    printf("open(rwsbrk) failed\n");266    exit(1);267  }268  n = write(fd, (void*)(a+PGSIZE), 1024);269  if(n >= 0){270    printf("write(fd, %p, 1024) returned %d, not -1\n", (void*)a+PGSIZE, n);271    exit(1);272  }273  close(fd);274  unlink("rwsbrk");275276  fd = open("README", O_RDONLY);277  if(fd < 0){278    printf("open(README) failed\n");279    exit(1);280  }281  n = read(fd, (void*)(a+PGSIZE), 10);282  if(n >= 0){283    printf("read(fd, %p, 10) returned %d, not -1\n", (void*)a+PGSIZE, n);284    exit(1);285  }286  close(fd);287  288  exit(0);289}290291// test O_TRUNC.292void293truncate1(char *s)294{295  char buf[32];296  297  unlink("truncfile");298  int fd1 = open("truncfile", O_CREATE|O_WRONLY|O_TRUNC);299  write(fd1, "abcd", 4);300  close(fd1);301302  int fd2 = open("truncfile", O_RDONLY);303  int n = read(fd2, buf, sizeof(buf));304  if(n != 4){305    printf("%s: read %d bytes, wanted 4\n", s, n);306    exit(1);307  }308309  fd1 = open("truncfile", O_WRONLY|O_TRUNC);310311  int fd3 = open("truncfile", O_RDONLY);312  n = read(fd3, buf, sizeof(buf));313  if(n != 0){314    printf("aaa fd3=%d\n", fd3);315    printf("%s: read %d bytes, wanted 0\n", s, n);316    exit(1);317  }318319  n = read(fd2, buf, sizeof(buf));320  if(n != 0){321    printf("bbb fd2=%d\n", fd2);322    printf("%s: read %d bytes, wanted 0\n", s, n);323    exit(1);324  }325  326  write(fd1, "abcdef", 6);327328  n = read(fd3, buf, sizeof(buf));329  if(n != 6){330    printf("%s: read %d bytes, wanted 6\n", s, n);331    exit(1);332  }333334  n = read(fd2, buf, sizeof(buf));335  if(n != 2){336    printf("%s: read %d bytes, wanted 2\n", s, n);337    exit(1);338  }339340  unlink("truncfile");341342  close(fd1);343  close(fd2);344  close(fd3);345}346347// write to an open FD whose file has just been truncated.348// this causes a write at an offset beyond the end of the file.349// such writes fail on xv6 (unlike POSIX) but at least350// they don't crash.351void352truncate2(char *s)353{354  unlink("truncfile");355356  int fd1 = open("truncfile", O_CREATE|O_TRUNC|O_WRONLY);357  write(fd1, "abcd", 4);358359  int fd2 = open("truncfile", O_TRUNC|O_WRONLY);360361  int n = write(fd1, "x", 1);362  if(n != -1){363    printf("%s: write returned %d, expected -1\n", s, n);364    exit(1);365  }366367  unlink("truncfile");368  close(fd1);369  close(fd2);370}371372void373truncate3(char *s)374{375  int pid, xstatus;376377  close(open("truncfile", O_CREATE|O_TRUNC|O_WRONLY));378  379  pid = fork();380  if(pid < 0){381    printf("%s: fork failed\n", s);382    exit(1);383  }384385  if(pid == 0){386    for(int i = 0; i < 100; i++){387      char buf[32];388      int fd = open("truncfile", O_WRONLY);389      if(fd < 0){390        printf("%s: open failed\n", s);391        exit(1);392      }393      int n = write(fd, "1234567890", 10);394      if(n != 10){395        printf("%s: write got %d, expected 10\n", s, n);396        exit(1);397      }398      close(fd);399      fd = open("truncfile", O_RDONLY);400      read(fd, buf, sizeof(buf));401      close(fd);402    }403    exit(0);404  }405406  for(int i = 0; i < 150; i++){407    int fd = open("truncfile", O_CREATE|O_WRONLY|O_TRUNC);408    if(fd < 0){409      printf("%s: open failed\n", s);410      exit(1);411    }412    int n = write(fd, "xxx", 3);413    if(n != 3){414      printf("%s: write got %d, expected 3\n", s, n);415      exit(1);416    }417    close(fd);418  }419420  wait(&xstatus);421  unlink("truncfile");422  exit(xstatus);423}424  425426// does chdir() call iput(p->cwd) in a transaction?427void428iputtest(char *s)429{430  if(mkdir("iputdir") < 0){431    printf("%s: mkdir failed\n", s);432    exit(1);433  }434  if(chdir("iputdir") < 0){435    printf("%s: chdir iputdir failed\n", s);436    exit(1);437  }438  if(unlink("../iputdir") < 0){439    printf("%s: unlink ../iputdir failed\n", s);440    exit(1);441  }442  if(chdir("/") < 0){443    printf("%s: chdir / failed\n", s);444    exit(1);445  }446}447448// does exit() call iput(p->cwd) in a transaction?449void450exitiputtest(char *s)451{452  int pid, xstatus;453454  pid = fork();455  if(pid < 0){456    printf("%s: fork failed\n", s);457    exit(1);458  }459  if(pid == 0){460    if(mkdir("iputdir") < 0){461      printf("%s: mkdir failed\n", s);462      exit(1);463    }464    if(chdir("iputdir") < 0){465      printf("%s: child chdir failed\n", s);466      exit(1);467    }468    if(unlink("../iputdir") < 0){469      printf("%s: unlink ../iputdir failed\n", s);470      exit(1);471    }472    exit(0);473  }474  wait(&xstatus);475  exit(xstatus);476}477478// does the error path in open() for attempt to write a479// directory call iput() in a transaction?480// needs a hacked kernel that pauses just after the namei()481// call in sys_open():482//    if((ip = namei(path)) == 0)483//      return -1;484//    {485//      int i;486//      for(i = 0; i < 10000; i++)487//        yield();488//    }489void490openiputtest(char *s)491{492  int pid, xstatus;493494  if(mkdir("oidir") < 0){495    printf("%s: mkdir oidir failed\n", s);496    exit(1);497  }498  pid = fork();499  if(pid < 0){500    printf("%s: fork failed\n", s);501    exit(1);502  }503  if(pid == 0){504    int fd = open("oidir", O_RDWR);505    if(fd >= 0){506      printf("%s: open directory for write succeeded\n", s);507      exit(1);508    }509    exit(0);510  }511  pause(1);512  if(unlink("oidir") != 0){513    printf("%s: unlink failed\n", s);514    exit(1);515  }516  wait(&xstatus);517  exit(xstatus);518}519520// simple file system tests521522void523opentest(char *s)524{525  int fd;526527  fd = open("echo", 0);528  if(fd < 0){529    printf("%s: open echo failed!\n", s);530    exit(1);531  }532  close(fd);533  fd = open("doesnotexist", 0);534  if(fd >= 0){535    printf("%s: open doesnotexist succeeded!\n", s);536    exit(1);537  }538}539540void541writetest(char *s)542{543  int fd;544  int i;545  enum { N=100, SZ=10 };546  547  fd = open("small", O_CREATE|O_RDWR);548  if(fd < 0){549    printf("%s: error: creat small failed!\n", s);550    exit(1);551  }552  for(i = 0; i < N; i++){553    if(write(fd, "aaaaaaaaaa", SZ) != SZ){554      printf("%s: error: write aa %d new file failed\n", s, i);555      exit(1);556    }557    if(write(fd, "bbbbbbbbbb", SZ) != SZ){558      printf("%s: error: write bb %d new file failed\n", s, i);559      exit(1);560    }561  }562  close(fd);563  fd = open("small", O_RDONLY);564  if(fd < 0){565    printf("%s: error: open small failed!\n", s);566    exit(1);567  }568  i = read(fd, buf, N*SZ*2);569  if(i != N*SZ*2){570    printf("%s: read failed\n", s);571    exit(1);572  }573  close(fd);574575  if(unlink("small") < 0){576    printf("%s: unlink small failed\n", s);577    exit(1);578  }579}580581void582writebig(char *s)583{584  int i, fd, n;585586  fd = open("big", O_CREATE|O_RDWR);587  if(fd < 0){588    printf("%s: error: creat big failed!\n", s);589    exit(1);590  }591592  for(i = 0; i < MAXFILE; i++){593    ((int*)buf)[0] = i;594    if(write(fd, buf, BSIZE) != BSIZE){595      printf("%s: error: write big file failed i=%d\n", s, i);596      exit(1);597    }598  }599600  close(fd);601602  fd = open("big", O_RDONLY);603  if(fd < 0){604    printf("%s: error: open big failed!\n", s);605    exit(1);606  }607608  n = 0;609  for(;;){610    i = read(fd, buf, BSIZE);611    if(i == 0){612      if(n != MAXFILE){613        printf("%s: read only %d blocks from big", s, n);614        exit(1);615      }616      break;617    } else if(i != BSIZE){618      printf("%s: read failed %d\n", s, i);619      exit(1);620    }621    if(((int*)buf)[0] != n){622      printf("%s: read content of block %d is %d\n", s,623             n, ((int*)buf)[0]);624      exit(1);625    }626    n++;627  }628  close(fd);629  if(unlink("big") < 0){630    printf("%s: unlink big failed\n", s);631    exit(1);632  }633}634635// many creates, followed by unlink test636void637createtest(char *s)638{639  int i, fd;640  enum { N=52 };641642  char name[3];643  name[0] = 'a';644  name[2] = '\0';645  for(i = 0; i < N; i++){646    name[1] = '0' + i;647    fd = open(name, O_CREATE|O_RDWR);648    close(fd);649  }650  name[0] = 'a';651  name[2] = '\0';652  for(i = 0; i < N; i++){653    name[1] = '0' + i;654    unlink(name);655  }656}657658void dirtest(char *s)659{660  if(mkdir("dir0") < 0){661    printf("%s: mkdir failed\n", s);662    exit(1);663  }664665  if(chdir("dir0") < 0){666    printf("%s: chdir dir0 failed\n", s);667    exit(1);668  }669670  if(chdir("..") < 0){671    printf("%s: chdir .. failed\n", s);672    exit(1);673  }674675  if(unlink("dir0") < 0){676    printf("%s: unlink dir0 failed\n", s);677    exit(1);678  }679}680681void682exectest(char *s)683{684  int fd, xstatus, pid;685  char *echoargv[] = { "echo", "OK", 0 };686  char buf[3];687688  unlink("echo-ok");689  pid = fork();690  if(pid < 0) {691     printf("%s: fork failed\n", s);692     exit(1);693  }694  if(pid == 0) {695    close(1);696    fd = open("echo-ok", O_CREATE|O_WRONLY);697    if(fd < 0) {698      printf("%s: create failed\n", s);699      exit(1);700    }701    if(fd != 1) {702      printf("%s: wrong fd\n", s);703      exit(1);704    }705    if(exec("echo", echoargv) < 0){706      printf("%s: exec echo failed\n", s);707      exit(1);708    }709    // won't get to here710  }711  if (wait(&xstatus) != pid) {712    printf("%s: wait failed!\n", s);713  }714  if(xstatus != 0)715    exit(xstatus);716717  fd = open("echo-ok", O_RDONLY);718  if(fd < 0) {719    printf("%s: open failed\n", s);720    exit(1);721  }722  if (read(fd, buf, 2) != 2) {723    printf("%s: read failed\n", s);724    exit(1);725  }726  unlink("echo-ok");727  if(buf[0] == 'O' && buf[1] == 'K')728    exit(0);729  else {730    printf("%s: wrong output\n", s);731    exit(1);732  }733734}735736// simple fork and pipe read/write737738void739pipe1(char *s)740{741  int fds[2], pid, xstatus;742  int seq, i, n, cc, total;743  enum { N=5, SZ=1033 };744  745  if(pipe(fds) != 0){746    printf("%s: pipe() failed\n", s);747    exit(1);748  }749  pid = fork();750  seq = 0;751  if(pid == 0){752    close(fds[0]);753    for(n = 0; n < N; n++){754      for(i = 0; i < SZ; i++)755        buf[i] = seq++;756      if(write(fds[1], buf, SZ) != SZ){757        printf("%s: pipe1 oops 1\n", s);758        exit(1);759      }760    }761    exit(0);762  } else if(pid > 0){763    close(fds[1]);764    total = 0;765    cc = 1;766    while((n = read(fds[0], buf, cc)) > 0){767      for(i = 0; i < n; i++){768        if((buf[i] & 0xff) != (seq++ & 0xff)){769          printf("%s: pipe1 oops 2\n", s);770          return;771        }772      }773      total += n;774      cc = cc * 2;775      if(cc > sizeof(buf))776        cc = sizeof(buf);777    }778    if(total != N * SZ){779      printf("%s: pipe1 oops 3 total %d\n", s, total);780      exit(1);781    }782    close(fds[0]);783    wait(&xstatus);784    exit(xstatus);785  } else {786    printf("%s: fork() failed\n", s);787    exit(1);788  }789}790791792// test if child is killed (status = -1)793void794killstatus(char *s)795{796  int xst;797  798  for(int i = 0; i < 100; i++){799    int pid1 = fork();800    if(pid1 < 0){801      printf("%s: fork failed\n", s);802      exit(1);803    }804    if(pid1 == 0){805      while(1) {806        getpid();807      }808      exit(0);809    }810    pause(1);811    kill(pid1);812    wait(&xst);813    if(xst != -1) {814       printf("%s: status should be -1\n", s);815       exit(1);816    }817  }818  exit(0);819}820821// meant to be run w/ at most two CPUs822void823preempt(char *s)824{825  int pid1, pid2, pid3;826  int pfds[2];827828  pid1 = fork();829  if(pid1 < 0) {830    printf("%s: fork failed", s);831    exit(1);832  }833  if(pid1 == 0)834    for(;;)835      ;836837  pid2 = fork();838  if(pid2 < 0) {839    printf("%s: fork failed\n", s);840    exit(1);841  }842  if(pid2 == 0)843    for(;;)844      ;845846  pipe(pfds);847  pid3 = fork();848  if(pid3 < 0) {849     printf("%s: fork failed\n", s);850     exit(1);851  }852  if(pid3 == 0){853    close(pfds[0]);854    if(write(pfds[1], "x", 1) != 1)855      printf("%s: preempt write error", s);856    close(pfds[1]);857    for(;;)858      ;859  }860861  close(pfds[1]);862  if(read(pfds[0], buf, sizeof(buf)) != 1){863    printf("%s: preempt read error", s);864    return;865  }866  close(pfds[0]);867  printf("kill... ");868  kill(pid1);869  kill(pid2);870  kill(pid3);871  printf("wait... ");872  wait(0);873  wait(0);874  wait(0);875}876877// try to find any races between exit and wait878void879exitwait(char *s)880{881  int i, pid;882883  for(i = 0; i < 100; i++){884    pid = fork();885    if(pid < 0){886      printf("%s: fork failed\n", s);887      exit(1);888    }889    if(pid){890      int xstate;891      if(wait(&xstate) != pid){892        printf("%s: wait wrong pid\n", s);893        exit(1);894      }895      if(i != xstate) {896        printf("%s: wait wrong exit status\n", s);897        exit(1);898      }899    } else {900      exit(i);901    }902  }903}904905// try to find races in the reparenting906// code that handles a parent exiting907// when it still has live children.908void909reparent(char *s)910{911  int master_pid = getpid();912  for(int i = 0; i < 200; i++){913    int pid = fork();914    if(pid < 0){915      printf("%s: fork failed\n", s);916      exit(1);917    }918    if(pid){919      if(wait(0) != pid){920        printf("%s: wait wrong pid\n", s);921        exit(1);922      }923    } else {924      int pid2 = fork();925      if(pid2 < 0){926        kill(master_pid);927        exit(1);928      }929      exit(0);930    }931  }932  exit(0);933}934935// what if two children exit() at the same time?936void937twochildren(char *s)938{939  for(int i = 0; i < 1000; i++){940    int pid1 = fork();941    if(pid1 < 0){942      printf("%s: fork failed\n", s);943      exit(1);944    }945    if(pid1 == 0){946      exit(0);947    } else {948      int pid2 = fork();949      if(pid2 < 0){950        printf("%s: fork failed\n", s);951        exit(1);952      }953      if(pid2 == 0){954        exit(0);955      } else {956        wait(0);957        wait(0);958      }959    }960  }961}962963// concurrent forks to try to expose locking bugs.964void965forkfork(char *s)966{967  enum { N=2 };968  969  for(int i = 0; i < N; i++){970    int pid = fork();971    if(pid < 0){972      printf("%s: fork failed", s);973      exit(1);974    }975    if(pid == 0){976      for(int j = 0; j < 200; j++){977        int pid1 = fork();978        if(pid1 < 0){979          exit(1);980        }981        if(pid1 == 0){982          exit(0);983        }984        wait(0);985      }986      exit(0);987    }988  }989990  int xstatus;991  for(int i = 0; i < N; i++){992    wait(&xstatus);993    if(xstatus != 0) {994      printf("%s: fork in child failed", s);995      exit(1);996    }997  }998}9991000void1001forkforkfork(char *s)1002{1003  unlink("stopforking");10041005  int pid = fork();1006  if(pid < 0){1007    printf("%s: fork failed", s);1008    exit(1);1009  }1010  if(pid == 0){1011    while(1){1012      int fd = open("stopforking", 0);1013      if(fd >= 0){1014        exit(0);1015      }1016      if(fork() < 0){1017        close(open("stopforking", O_CREATE|O_RDWR));1018      }1019    }10201021    exit(0);1022  }10231024  pause(20); // two seconds1025  close(open("stopforking", O_CREATE|O_RDWR));1026  wait(0);1027  pause(10); // one second1028}10291030// regression test. does reparent() violate the parent-then-child1031// locking order when giving away a child to init, so that exit()1032// deadlocks against init's wait()? also used to trigger a "panic:1033// release" due to exit() releasing a different p->parent->lock than1034// it acquired.1035void1036reparent2(char *s)1037{1038  for(int i = 0; i < 800; i++){1039    int pid1 = fork();1040    if(pid1 < 0){1041      printf("fork failed\n");1042      exit(1);1043    }1044    if(pid1 == 0){1045      fork();1046      fork();1047      exit(0);1048    }1049    wait(0);1050  }10511052  exit(0);1053}10541055// allocate all mem, free it, and allocate again1056void1057mem(char *s)1058{1059  void *m1, *m2;1060  int pid;10611062  if((pid = fork()) == 0){1063    m1 = 0;1064    while((m2 = malloc(10001)) != 0){1065      *(char**)m2 = m1;1066      m1 = m2;1067    }1068    while(m1){1069      m2 = *(char**)m1;1070      free(m1);1071      m1 = m2;1072    }1073    m1 = malloc(1024*20);1074    if(m1 == 0){1075      printf("%s: couldn't allocate mem?!!\n", s);1076      exit(1);1077    }1078    free(m1);1079    exit(0);1080  } else {1081    int xstatus;1082    wait(&xstatus);1083    if(xstatus == -1){1084      // probably page fault, so might be lazy lab,1085      // so OK.1086      exit(0);1087    }1088    exit(xstatus);1089  }1090}10911092// More file system tests10931094// two processes write to the same file descriptor1095// is the offset shared? does inode locking work?1096void1097sharedfd(char *s)1098{1099  int fd, pid, i, n, nc, np;1100  enum { N = 1000, SZ=10};1101  char buf[SZ];11021103  unlink("sharedfd");1104  fd = open("sharedfd", O_CREATE|O_RDWR);1105  if(fd < 0){1106    printf("%s: cannot open sharedfd for writing", s);1107    exit(1);1108  }1109  pid = fork();1110  memset(buf, pid==0?'c':'p', sizeof(buf));1111  for(i = 0; i < N; i++){1112    if(write(fd, buf, sizeof(buf)) != sizeof(buf)){1113      printf("%s: write sharedfd failed\n", s);1114      exit(1);1115    }1116  }1117  if(pid == 0) {1118    exit(0);1119  } else {1120    int xstatus;1121    wait(&xstatus);1122    if(xstatus != 0)1123      exit(xstatus);1124  }1125  1126  close(fd);1127  fd = open("sharedfd", 0);1128  if(fd < 0){1129    printf("%s: cannot open sharedfd for reading\n", s);1130    exit(1);1131  }1132  nc = np = 0;1133  while((n = read(fd, buf, sizeof(buf))) > 0){1134    for(i = 0; i < sizeof(buf); i++){1135      if(buf[i] == 'c')1136        nc++;1137      if(buf[i] == 'p')1138        np++;1139    }1140  }1141  close(fd);1142  unlink("sharedfd");1143  if(nc == N*SZ && np == N*SZ){1144    exit(0);1145  } else {1146    printf("%s: nc/np test fails\n", s);1147    exit(1);1148  }1149}11501151// four processes write different files at the same1152// time, to test block allocation.1153void1154fourfiles(char *s)1155{1156  int fd, pid, i, j, n, total, pi;1157  char *names[] = { "f0", "f1", "f2", "f3" };1158  char *fname;1159  enum { N=12, NCHILD=4, SZ=500 };1160  1161  for(pi = 0; pi < NCHILD; pi++){1162    fname = names[pi];1163    unlink(fname);11641165    pid = fork();1166    if(pid < 0){1167      printf("%s: fork failed\n", s);1168      exit(1);1169    }11701171    if(pid == 0){1172      fd = open(fname, O_CREATE | O_RDWR);1173      if(fd < 0){1174        printf("%s: create failed\n", s);1175        exit(1);1176      }11771178      memset(buf, '0'+pi, SZ);1179      for(i = 0; i < N; i++){1180        if((n = write(fd, buf, SZ)) != SZ){1181          printf("write failed %d\n", n);1182          exit(1);1183        }1184      }1185      exit(0);1186    }1187  }11881189  int xstatus;1190  for(pi = 0; pi < NCHILD; pi++){1191    wait(&xstatus);1192    if(xstatus != 0)1193      exit(xstatus);1194  }11951196  for(i = 0; i < NCHILD; i++){1197    fname = names[i];1198    fd = open(fname, 0);1199    total = 0;1200    while((n = read(fd, buf, sizeof(buf))) > 0){1201      for(j = 0; j < n; j++){1202        if(buf[j] != '0'+i){1203          printf("%s: wrong char\n", s);1204          exit(1);1205        }1206      }1207      total += n;1208    }1209    close(fd);1210    if(total != N*SZ){1211      printf("wrong length %d\n", total);1212      exit(1);1213    }1214    unlink(fname);1215  }1216}12171218// four processes create and delete different files in same directory1219void1220createdelete(char *s)1221{1222  enum { N = 20, NCHILD=4 };1223  int pid, i, fd, pi;1224  char name[32];12251226  for(pi = 0; pi < NCHILD; pi++){1227    pid = fork();1228    if(pid < 0){1229      printf("%s: fork failed\n", s);1230      exit(1);1231    }12321233    if(pid == 0){1234      name[0] = 'p' + pi;1235      name[2] = '\0';1236      for(i = 0; i < N; i++){1237        name[1] = '0' + i;1238        fd = open(name, O_CREATE | O_RDWR);1239        if(fd < 0){1240          printf("%s: create failed\n", s);1241          exit(1);1242        }1243        close(fd);1244        if(i > 0 && (i % 2 ) == 0){1245          name[1] = '0' + (i / 2);1246          if(unlink(name) < 0){1247            printf("%s: unlink failed\n", s);1248            exit(1);1249          }1250        }1251      }1252      exit(0);1253    }1254  }12551256  int xstatus;1257  for(pi = 0; pi < NCHILD; pi++){1258    wait(&xstatus);1259    if(xstatus != 0)1260      exit(1);1261  }12621263  name[0] = name[1] = name[2] = 0;1264  for(i = 0; i < N; i++){1265    for(pi = 0; pi < NCHILD; pi++){1266      name[0] = 'p' + pi;1267      name[1] = '0' + i;1268      fd = open(name, 0);1269      if((i == 0 || i >= N/2) && fd < 0){1270        printf("%s: oops createdelete %s didn't exist\n", s, name);1271        exit(1);1272      } else if((i >= 1 && i < N/2) && fd >= 0){1273        printf("%s: oops createdelete %s did exist\n", s, name);1274        exit(1);1275      }1276      if(fd >= 0)1277        close(fd);1278    }1279  }12801281  for(i = 0; i < N; i++){1282    for(pi = 0; pi < NCHILD; pi++){1283      name[0] = 'p' + pi;1284      name[1] = '0' + i;1285      unlink(name);1286    }1287  }1288}12891290// can I unlink a file and still read it?1291void1292unlinkread(char *s)1293{1294  enum { SZ = 5 };1295  int fd, fd1;12961297  fd = open("unlinkread", O_CREATE | O_RDWR);1298  if(fd < 0){1299    printf("%s: create unlinkread failed\n", s);1300    exit(1);1301  }1302  write(fd, "hello", SZ);1303  close(fd);13041305  fd = open("unlinkread", O_RDWR);1306  if(fd < 0){1307    printf("%s: open unlinkread failed\n", s);1308    exit(1);1309  }1310  if(unlink("unlinkread") != 0){1311    printf("%s: unlink unlinkread failed\n", s);1312    exit(1);1313  }13141315  fd1 = open("unlinkread", O_CREATE | O_RDWR);1316  write(fd1, "yyy", 3);1317  close(fd1);13181319  if(read(fd, buf, sizeof(buf)) != SZ){1320    printf("%s: unlinkread read failed", s);1321    exit(1);1322  }1323  if(buf[0] != 'h'){1324    printf("%s: unlinkread wrong data\n", s);1325    exit(1);1326  }1327  if(write(fd, buf, 10) != 10){1328    printf("%s: unlinkread write failed\n", s);1329    exit(1);1330  }1331  close(fd);1332  unlink("unlinkread");1333}13341335void1336linktest(char *s)1337{1338  enum { SZ = 5 };1339  int fd;13401341  unlink("lf1");1342  unlink("lf2");13431344  fd = open("lf1", O_CREATE|O_RDWR);1345  if(fd < 0){1346    printf("%s: create lf1 failed\n", s);1347    exit(1);1348  }1349  if(write(fd, "hello", SZ) != SZ){1350    printf("%s: write lf1 failed\n", s);1351    exit(1);1352  }1353  close(fd);13541355  if(link("lf1", "lf2") < 0){1356    printf("%s: link lf1 lf2 failed\n", s);1357    exit(1);1358  }1359  unlink("lf1");13601361  if(open("lf1", 0) >= 0){1362    printf("%s: unlinked lf1 but it is still there!\n", s);1363    exit(1);1364  }13651366  fd = open("lf2", 0);1367  if(fd < 0){1368    printf("%s: open lf2 failed\n", s);1369    exit(1);1370  }1371  if(read(fd, buf, sizeof(buf)) != SZ){1372    printf("%s: read lf2 failed\n", s);1373    exit(1);1374  }1375  close(fd);13761377  if(link("lf2", "lf2") >= 0){1378    printf("%s: link lf2 lf2 succeeded! oops\n", s);1379    exit(1);1380  }13811382  unlink("lf2");1383  if(link("lf2", "lf1") >= 0){1384    printf("%s: link non-existent succeeded! oops\n", s);1385    exit(1);1386  }13871388  if(link(".", "lf1") >= 0){1389    printf("%s: link . lf1 succeeded! oops\n", s);1390    exit(1);1391  }1392}13931394// test concurrent create/link/unlink of the same file1395void1396concreate(char *s)1397{1398  enum { N = 40 };1399  char file[3];1400  int i, pid, n, fd;1401  char fa[N];1402  struct {1403    ushort inum;1404    char name[DIRSIZ];1405  } de;14061407  file[0] = 'C';1408  file[2] = '\0';1409  for(i = 0; i < N; i++){1410    file[1] = '0' + i;1411    unlink(file);1412    pid = fork();1413    if(pid && (i % 3) == 1){1414      link("C0", file);1415    } else if(pid == 0 && (i % 5) == 1){1416      link("C0", file);1417    } else {1418      fd = open(file, O_CREATE | O_RDWR);1419      if(fd < 0){1420        printf("concreate create %s failed\n", file);1421        exit(1);1422      }1423      close(fd);1424    }1425    if(pid == 0) {1426      exit(0);1427    } else {1428      int xstatus;1429      wait(&xstatus);1430      if(xstatus != 0)1431        exit(1);1432    }1433  }14341435  memset(fa, 0, sizeof(fa));1436  fd = open(".", 0);1437  n = 0;1438  while(read(fd, &de, sizeof(de)) > 0){1439    if(de.inum == 0)1440      continue;1441    if(de.name[0] == 'C' && de.name[2] == '\0'){1442      i = de.name[1] - '0';1443      if(i < 0 || i >= sizeof(fa)){1444        printf("%s: concreate weird file %s\n", s, de.name);1445        exit(1);1446      }1447      if(fa[i]){1448        printf("%s: concreate duplicate file %s\n", s, de.name);1449        exit(1);1450      }1451      fa[i] = 1;1452      n++;1453    }1454  }1455  close(fd);14561457  if(n != N){1458    printf("%s: concreate not enough files in directory listing\n", s);1459    exit(1);1460  }14611462  for(i = 0; i < N; i++){1463    file[1] = '0' + i;1464    pid = fork();1465    if(pid < 0){1466      printf("%s: fork failed\n", s);1467      exit(1);1468    }1469    if(((i % 3) == 0 && pid == 0) ||1470       ((i % 3) == 1 && pid != 0)){1471      close(open(file, 0));1472      close(open(file, 0));1473      close(open(file, 0));1474      close(open(file, 0));1475      close(open(file, 0));1476      close(open(file, 0));1477    } else {1478      unlink(file);1479      unlink(file);1480      unlink(file);1481      unlink(file);1482      unlink(file);1483      unlink(file);1484    }1485    if(pid == 0)1486      exit(0);1487    else1488      wait(0);1489  }1490}14911492// another concurrent link/unlink/create test,1493// to look for deadlocks.1494void1495linkunlink(char *s)1496{1497  int pid, i;14981499  unlink("x");1500  pid = fork();1501  if(pid < 0){1502    printf("%s: fork failed\n", s);1503    exit(1);1504  }15051506  unsigned int x = (pid ? 1 : 97);1507  for(i = 0; i < 100; i++){1508    x = x * 1103515245 + 12345;1509    if((x % 3) == 0){1510      close(open("x", O_RDWR | O_CREATE));1511    } else if((x % 3) == 1){1512      link("cat", "x");1513    } else {1514      unlink("x");1515    }1516  }15171518  if(pid)1519    wait(0);1520  else1521    exit(0);1522}152315241525void1526subdir(char *s)1527{1528  int fd, cc;15291530  unlink("ff");1531  if(mkdir("dd") != 0){1532    printf("%s: mkdir dd failed\n", s);1533    exit(1);1534  }15351536  fd = open("dd/ff", O_CREATE | O_RDWR);1537  if(fd < 0){1538    printf("%s: create dd/ff failed\n", s);1539    exit(1);1540  }1541  write(fd, "ff", 2);1542  close(fd);15431544  if(unlink("dd") >= 0){1545    printf("%s: unlink dd (non-empty dir) succeeded!\n", s);1546    exit(1);1547  }15481549  if(mkdir("/dd/dd") != 0){1550    printf("%s: subdir mkdir dd/dd failed\n", s);1551    exit(1);1552  }15531554  fd = open("dd/dd/ff", O_CREATE | O_RDWR);1555  if(fd < 0){1556    printf("%s: create dd/dd/ff failed\n", s);1557    exit(1);1558  }1559  write(fd, "FF", 2);1560  close(fd);15611562  fd = open("dd/dd/../ff", 0);1563  if(fd < 0){1564    printf("%s: open dd/dd/../ff failed\n", s);1565    exit(1);1566  }1567  cc = read(fd, buf, sizeof(buf));1568  if(cc != 2 || buf[0] != 'f'){1569    printf("%s: dd/dd/../ff wrong content\n", s);1570    exit(1);1571  }1572  close(fd);15731574  if(link("dd/dd/ff", "dd/dd/ffff") != 0){1575    printf("%s: link dd/dd/ff dd/dd/ffff failed\n", s);1576    exit(1);1577  }15781579  if(unlink("dd/dd/ff") != 0){1580    printf("%s: unlink dd/dd/ff failed\n", s);1581    exit(1);1582  }1583  if(open("dd/dd/ff", O_RDONLY) >= 0){1584    printf("%s: open (unlinked) dd/dd/ff succeeded\n", s);1585    exit(1);1586  }15871588  if(chdir("dd") != 0){1589    printf("%s: chdir dd failed\n", s);1590    exit(1);1591  }1592  if(chdir("dd/../../dd") != 0){1593    printf("%s: chdir dd/../../dd failed\n", s);1594    exit(1);1595  }1596  if(chdir("dd/../../../dd") != 0){1597    printf("%s: chdir dd/../../../dd failed\n", s);1598    exit(1);1599  }1600  if(chdir("./..") != 0){1601    printf("%s: chdir ./.. failed\n", s);1602    exit(1);1603  }16041605  fd = open("dd/dd/ffff", 0);1606  if(fd < 0){1607    printf("%s: open dd/dd/ffff failed\n", s);1608    exit(1);1609  }1610  if(read(fd, buf, sizeof(buf)) != 2){1611    printf("%s: read dd/dd/ffff wrong len\n", s);1612    exit(1);1613  }1614  close(fd);16151616  if(open("dd/dd/ff", O_RDONLY) >= 0){1617    printf("%s: open (unlinked) dd/dd/ff succeeded!\n", s);1618    exit(1);1619  }16201621  if(open("dd/ff/ff", O_CREATE|O_RDWR) >= 0){1622    printf("%s: create dd/ff/ff succeeded!\n", s);1623    exit(1);1624  }1625  if(open("dd/xx/ff", O_CREATE|O_RDWR) >= 0){1626    printf("%s: create dd/xx/ff succeeded!\n", s);1627    exit(1);1628  }1629  if(open("dd", O_CREATE) >= 0){1630    printf("%s: create dd succeeded!\n", s);1631    exit(1);1632  }1633  if(open("dd", O_RDWR) >= 0){1634    printf("%s: open dd rdwr succeeded!\n", s);1635    exit(1);1636  }1637  if(open("dd", O_WRONLY) >= 0){1638    printf("%s: open dd wronly succeeded!\n", s);1639    exit(1);1640  }1641  if(link("dd/ff/ff", "dd/dd/xx") == 0){1642    printf("%s: link dd/ff/ff dd/dd/xx succeeded!\n", s);1643    exit(1);1644  }1645  if(link("dd/xx/ff", "dd/dd/xx") == 0){1646    printf("%s: link dd/xx/ff dd/dd/xx succeeded!\n", s);1647    exit(1);1648  }1649  if(link("dd/ff", "dd/dd/ffff") == 0){1650    printf("%s: link dd/ff dd/dd/ffff succeeded!\n", s);1651    exit(1);1652  }1653  if(mkdir("dd/ff/ff") == 0){1654    printf("%s: mkdir dd/ff/ff succeeded!\n", s);1655    exit(1);1656  }1657  if(mkdir("dd/xx/ff") == 0){1658    printf("%s: mkdir dd/xx/ff succeeded!\n", s);1659    exit(1);1660  }1661  if(mkdir("dd/dd/ffff") == 0){1662    printf("%s: mkdir dd/dd/ffff succeeded!\n", s);1663    exit(1);1664  }1665  if(unlink("dd/xx/ff") == 0){1666    printf("%s: unlink dd/xx/ff succeeded!\n", s);1667    exit(1);1668  }1669  if(unlink("dd/ff/ff") == 0){1670    printf("%s: unlink dd/ff/ff succeeded!\n", s);1671    exit(1);1672  }1673  if(chdir("dd/ff") == 0){1674    printf("%s: chdir dd/ff succeeded!\n", s);1675    exit(1);1676  }1677  if(chdir("dd/xx") == 0){1678    printf("%s: chdir dd/xx succeeded!\n", s);1679    exit(1);1680  }16811682  if(unlink("dd/dd/ffff") != 0){1683    printf("%s: unlink dd/dd/ff failed\n", s);1684    exit(1);1685  }1686  if(unlink("dd/ff") != 0){1687    printf("%s: unlink dd/ff failed\n", s);1688    exit(1);1689  }1690  if(unlink("dd") == 0){1691    printf("%s: unlink non-empty dd succeeded!\n", s);1692    exit(1);1693  }1694  if(unlink("dd/dd") < 0){1695    printf("%s: unlink dd/dd failed\n", s);1696    exit(1);1697  }1698  if(unlink("dd") < 0){1699    printf("%s: unlink dd failed\n", s);1700    exit(1);1701  }1702}17031704// test writes that are larger than the log.1705void1706bigwrite(char *s)1707{1708  int fd, sz;17091710  unlink("bigwrite");1711  for(sz = 499; sz < (MAXOPBLOCKS+2)*BSIZE; sz += 471){1712    fd = open("bigwrite", O_CREATE | O_RDWR);1713    if(fd < 0){1714      printf("%s: cannot create bigwrite\n", s);1715      exit(1);1716    }1717    int i;1718    for(i = 0; i < 2; i++){1719      int cc = write(fd, buf, sz);1720      if(cc != sz){1721        printf("%s: write(%d) ret %d\n", s, sz, cc);1722        exit(1);1723      }1724    }1725    close(fd);1726    unlink("bigwrite");1727  }1728}172917301731void1732bigfile(char *s)1733{1734  enum { N = 20, SZ=600 };1735  int fd, i, total, cc;17361737  unlink("bigfile.dat");1738  fd = open("bigfile.dat", O_CREATE | O_RDWR);1739  if(fd < 0){1740    printf("%s: cannot create bigfile", s);1741    exit(1);1742  }1743  for(i = 0; i < N; i++){1744    memset(buf, i, SZ);1745    if(write(fd, buf, SZ) != SZ){1746      printf("%s: write bigfile failed\n", s);1747      exit(1);1748    }1749  }1750  close(fd);17511752  fd = open("bigfile.dat", 0);1753  if(fd < 0){1754    printf("%s: cannot open bigfile\n", s);1755    exit(1);1756  }1757  total = 0;1758  for(i = 0; ; i++){1759    cc = read(fd, buf, SZ/2);1760    if(cc < 0){1761      printf("%s: read bigfile failed\n", s);1762      exit(1);1763    }1764    if(cc == 0)1765      break;1766    if(cc != SZ/2){1767      printf("%s: short read bigfile\n", s);1768      exit(1);1769    }1770    if(buf[0] != i/2 || buf[SZ/2-1] != i/2){1771      printf("%s: read bigfile wrong data\n", s);1772      exit(1);1773    }1774    total += cc;1775  }1776  close(fd);1777  if(total != N*SZ){1778    printf("%s: read bigfile wrong total\n", s);1779    exit(1);1780  }1781  unlink("bigfile.dat");1782}17831784void1785fourteen(char *s)1786{1787  int fd;17881789  // DIRSIZ is 14.17901791  if(mkdir("12345678901234") != 0){1792    printf("%s: mkdir 12345678901234 failed\n", s);1793    exit(1);1794  }1795  if(mkdir("12345678901234/123456789012345") != 0){1796    printf("%s: mkdir 12345678901234/123456789012345 failed\n", s);1797    exit(1);1798  }1799  fd = open("123456789012345/123456789012345/123456789012345", O_CREATE);1800  if(fd < 0){1801    printf("%s: create 123456789012345/123456789012345/123456789012345 failed\n", s);1802    exit(1);1803  }1804  close(fd);1805  fd = open("12345678901234/12345678901234/12345678901234", 0);1806  if(fd < 0){1807    printf("%s: open 12345678901234/12345678901234/12345678901234 failed\n", s);1808    exit(1);1809  }1810  close(fd);18111812  if(mkdir("12345678901234/12345678901234") == 0){1813    printf("%s: mkdir 12345678901234/12345678901234 succeeded!\n", s);1814    exit(1);1815  }1816  if(mkdir("123456789012345/12345678901234") == 0){1817    printf("%s: mkdir 12345678901234/123456789012345 succeeded!\n", s);1818    exit(1);1819  }18201821  // clean up1822  unlink("123456789012345/12345678901234");1823  unlink("12345678901234/12345678901234");1824  unlink("12345678901234/12345678901234/12345678901234");1825  unlink("123456789012345/123456789012345/123456789012345");1826  unlink("12345678901234/123456789012345");1827  unlink("12345678901234");1828}18291830void1831rmdot(char *s)1832{1833  if(mkdir("dots") != 0){1834    printf("%s: mkdir dots failed\n", s);1835    exit(1);1836  }1837  if(chdir("dots") != 0){1838    printf("%s: chdir dots failed\n", s);1839    exit(1);1840  }1841  if(unlink(".") == 0){1842    printf("%s: rm . worked!\n", s);1843    exit(1);1844  }1845  if(unlink("..") == 0){1846    printf("%s: rm .. worked!\n", s);1847    exit(1);1848  }1849  if(chdir("/") != 0){1850    printf("%s: chdir / failed\n", s);1851    exit(1);1852  }1853  if(unlink("dots/.") == 0){1854    printf("%s: unlink dots/. worked!\n", s);1855    exit(1);1856  }1857  if(unlink("dots/..") == 0){1858    printf("%s: unlink dots/.. worked!\n", s);1859    exit(1);1860  }1861  if(unlink("dots") != 0){1862    printf("%s: unlink dots failed!\n", s);1863    exit(1);1864  }1865}18661867void1868dirfile(char *s)1869{1870  int fd;18711872  fd = open("dirfile", O_CREATE);1873  if(fd < 0){1874    printf("%s: create dirfile failed\n", s);1875    exit(1);1876  }1877  close(fd);1878  if(chdir("dirfile") == 0){1879    printf("%s: chdir dirfile succeeded!\n", s);1880    exit(1);1881  }1882  fd = open("dirfile/xx", 0);1883  if(fd >= 0){1884    printf("%s: create dirfile/xx succeeded!\n", s);1885    exit(1);1886  }1887  fd = open("dirfile/xx", O_CREATE);1888  if(fd >= 0){1889    printf("%s: create dirfile/xx succeeded!\n", s);1890    exit(1);1891  }1892  if(mkdir("dirfile/xx") == 0){1893    printf("%s: mkdir dirfile/xx succeeded!\n", s);1894    exit(1);1895  }1896  if(unlink("dirfile/xx") == 0){1897    printf("%s: unlink dirfile/xx succeeded!\n", s);1898    exit(1);1899  }1900  if(link("README", "dirfile/xx") == 0){1901    printf("%s: link to dirfile/xx succeeded!\n", s);1902    exit(1);1903  }1904  if(unlink("dirfile") != 0){1905    printf("%s: unlink dirfile failed!\n", s);1906    exit(1);1907  }19081909  fd = open(".", O_RDWR);1910  if(fd >= 0){1911    printf("%s: open . for writing succeeded!\n", s);1912    exit(1);1913  }1914  fd = open(".", 0);1915  if(write(fd, "x", 1) > 0){1916    printf("%s: write . succeeded!\n", s);1917    exit(1);1918  }1919  close(fd);1920}19211922// test that iput() is called at the end of _namei().1923// also tests empty file names.1924void1925iref(char *s)1926{1927  int i, fd;19281929  for(i = 0; i < NINODE + 1; i++){1930    if(mkdir("irefd") != 0){1931      printf("%s: mkdir irefd failed\n", s);1932      exit(1);1933    }1934    if(chdir("irefd") != 0){1935      printf("%s: chdir irefd failed\n", s);1936      exit(1);1937    }19381939    mkdir("");1940    link("README", "");1941    fd = open("", O_CREATE);1942    if(fd >= 0)1943      close(fd);1944    fd = open("xx", O_CREATE);1945    if(fd >= 0)1946      close(fd);1947    unlink("xx");1948  }19491950  // clean up1951  for(i = 0; i < NINODE + 1; i++){1952    chdir("..");1953    unlink("irefd");1954  }19551956  chdir("/");1957}19581959// test that fork fails gracefully1960// the forktest binary also does this, but it runs out of proc entries first.1961// inside the bigger usertests binary, we run out of memory first.1962void1963forktest(char *s)1964{1965  enum{ N = 1000 };1966  int n, pid;19671968  for(n=0; n<N; n++){1969    pid = fork();1970    if(pid < 0)1971      break;1972    if(pid == 0)1973      exit(0);1974  }19751976  if (n == 0) {1977    printf("%s: no fork at all!\n", s);1978    exit(1);1979  }19801981  if(n == N){1982    printf("%s: fork claimed to work 1000 times!\n", s);1983    exit(1);1984  }19851986  for(; n > 0; n--){1987    if(wait(0) < 0){1988      printf("%s: wait stopped early\n", s);1989      exit(1);1990    }1991  }19921993  if(wait(0) != -1){1994    printf("%s: wait got too many\n", s);1995    exit(1);1996  }1997}19981999void2000sbrkbasic(char *s)2001{2002  enum { TOOMUCH=1024*1024*1024};2003  int i, pid, xstatus;2004  char *c, *a, *b;20052006  // does sbrk() return the expected failure value?2007  pid = fork();2008  if(pid < 0){2009    printf("fork failed in sbrkbasic\n");2010    exit(1);2011  }2012  if(pid == 0){2013    a = sbrk(TOOMUCH);2014    if(a == (char*)SBRK_ERROR){2015      // it's OK if this fails.2016      exit(0);2017    }2018    2019    for(b = a; b < a+TOOMUCH; b += PGSIZE){2020      *b = 99;2021    }2022    2023    // we should not get here! either sbrk(TOOMUCH)2024    // should have failed, or (with lazy allocation)2025    // a pagefault should have killed this process.2026    exit(1);2027  }20282029  wait(&xstatus);2030  if(xstatus == 1){2031    printf("%s: too much memory allocated!\n", s);2032    exit(1);2033  }20342035  // can one sbrk() less than a page?2036  a = sbrk(0);2037  for(i = 0; i < 5000; i++){2038    b = sbrk(1);2039    if(b != a){2040      printf("%s: sbrk test failed %d %p %p\n", s, i, a, b);2041      exit(1);2042    }2043    *b = 1;2044    a = b + 1;2045  }2046  pid = fork();2047  if(pid < 0){2048    printf("%s: sbrk test fork failed\n", s);2049    exit(1);2050  }2051  c = sbrk(1);2052  c = sbrk(1);2053  if(c != a + 1){2054    printf("%s: sbrk test failed post-fork\n", s);2055    exit(1);2056  }2057  if(pid == 0)2058    exit(0);2059  wait(&xstatus);2060  exit(xstatus);2061}20622063void2064sbrkmuch(char *s)2065{2066  enum { BIG=100*1024*1024 };2067  char *c, *oldbrk, *a, *lastaddr, *p;2068  uint64 amt;20692070  oldbrk = sbrk(0);20712072  // can one grow address space to something big?2073  a = sbrk(0);2074  amt = BIG - (uint64)a;2075  p = sbrk(amt);2076  if (p != a) {2077    printf("%s: sbrk test failed to grow big address space; enough phys mem?\n", s);2078    exit(1);2079  }20802081  lastaddr = (char*) (BIG-1);2082  *lastaddr = 99;20832084  // can one de-allocate?2085  a = sbrk(0);2086  c = sbrk(-PGSIZE);2087  if(c == (char*)SBRK_ERROR){2088    printf("%s: sbrk could not deallocate\n", s);2089    exit(1);2090  }2091  c = sbrk(0);2092  if(c != a - PGSIZE){2093    printf("%s: sbrk deallocation produced wrong address, a %p c %p\n", s, a, c);2094    exit(1);2095  }20962097  // can one re-allocate that page?2098  a = sbrk(0);2099  c = sbrk(PGSIZE);2100  if(c != a || sbrk(0) != a + PGSIZE){2101    printf("%s: sbrk re-allocation failed, a %p c %p\n", s, a, c);2102    exit(1);2103  }2104  if(*lastaddr == 99){2105    // should be zero2106    printf("%s: sbrk de-allocation didn't really deallocate\n", s);2107    exit(1);2108  }21092110  a = sbrk(0);2111  c = sbrk(-(sbrk(0) - oldbrk));2112  if(c != a){2113    printf("%s: sbrk downsize failed, a %p c %p\n", s, a, c);2114    exit(1);2115  }2116}21172118// can we read the kernel's memory?2119void2120kernmem(char *s)2121{2122  char *a;2123  int pid;21242125  for(a = (char*)(KERNBASE); a < (char*) (KERNBASE+2000000); a += 50000){2126    pid = fork();2127    if(pid < 0){2128      printf("%s: fork failed\n", s);2129      exit(1);2130    }2131    if(pid == 0){2132      printf("%s: oops could read %p = %x\n", s, a, *a);2133      exit(1);2134    }2135    int xstatus;2136    wait(&xstatus);2137    if(xstatus != -1)  // did kernel kill child?2138      exit(1);2139  }2140}21412142// user code should not be able to write to addresses above MAXVA.2143void2144MAXVAplus(char *s)2145{2146  volatile uint64 a = MAXVA;2147  for( ; a != 0; a <<= 1){2148    int pid;2149    pid = fork();2150    if(pid < 0){2151      printf("%s: fork failed\n", s);2152      exit(1);2153    }2154    if(pid == 0){2155      *(char*)a = 99;2156      printf("%s: oops wrote %p\n", s, (void*)a);2157      exit(1);2158    }2159    int xstatus;2160    wait(&xstatus);2161    if(xstatus != -1)  // did kernel kill child?2162      exit(1);2163  }2164}21652166// if we run the system out of memory, does it clean up the last2167// failed allocation?2168void2169sbrkfail(char *s)2170{2171  enum { BIG=100*1024*1024 };2172  int i, xstatus;2173  int fds[2];2174  char scratch;2175  char *c, *a;2176  int pids[10];2177  int pid;2178  int failed;21792180  failed = 0;2181  if(pipe(fds) != 0){2182    printf("%s: pipe() failed\n", s);2183    exit(1);2184  }2185  for(i = 0; i < sizeof(pids)/sizeof(pids[0]); i++){2186    if((pids[i] = fork()) == 0){2187      // allocate a lot of memory2188      if (sbrk(BIG - (uint64)sbrk(0)) ==  (char*)SBRK_ERROR)2189        write(fds[1], "0", 1);2190      else2191        write(fds[1], "1", 1);2192      // sit around until killed2193      for(;;) pause(1000);2194    }2195    if(pids[i] != -1) {2196      read(fds[0], &scratch, 1);2197      if(scratch == '0')2198        failed = 1;2199    }2200  }2201  if(!failed) {2202    printf("%s: no allocation failed; allocate more?\n", s);2203  }2204  2205  // if those failed allocations freed up the pages they did allocate,2206  // we'll be able to allocate here2207  c = sbrk(PGSIZE);2208  for(i = 0; i < sizeof(pids)/sizeof(pids[0]); i++){2209    if(pids[i] == -1)2210      continue;2211    kill(pids[i]);2212    wait(0);2213  }2214  if(c == (char*)SBRK_ERROR){2215    printf("%s: failed sbrk leaked memory\n", s);2216    exit(1);2217  }22182219  // test running fork with the above allocated page 2220  pid = fork();2221  if(pid < 0){2222    printf("%s: fork failed\n", s);2223    exit(1);2224  }2225  if(pid == 0){2226    // allocate a lot of memory. this should produce an error2227    a = sbrk(10*BIG);2228    if(a == (char*)SBRK_ERROR){2229      exit(0);2230    }   2231    printf("%s: allocate a lot of memory succeeded %d\n", s, 10*BIG);2232    exit(1);2233  }2234  wait(&xstatus);2235  if(xstatus != 0)2236    exit(1);2237}22382239  2240// test reads/writes from/to allocated memory2241void2242sbrkarg(char *s)2243{2244  char *a;2245  int fd, n;22462247  a = sbrk(PGSIZE);2248  fd = open("sbrk", O_CREATE|O_WRONLY);2249  unlink("sbrk");2250  if(fd < 0)  {2251    printf("%s: open sbrk failed\n", s);2252    exit(1);2253  }2254  if ((n = write(fd, a, PGSIZE)) < 0) {2255    printf("%s: write sbrk failed\n", s);2256    exit(1);2257  }2258  close(fd);22592260  // test writes to allocated memory2261  a = sbrk(PGSIZE);2262  if(pipe((int *) a) != 0){2263    printf("%s: pipe() failed\n", s);2264    exit(1);2265  } 2266}22672268void2269validatetest(char *s)2270{2271  int hi;2272  uint64 p;22732274  hi = 1100*1024;2275  for(p = 0; p <= (uint)hi; p += PGSIZE){2276    // try to crash the kernel by passing in a bad string pointer2277    if(link("nosuchfile", (char*)p) != -1){2278      printf("%s: link should not succeed\n", s);2279      exit(1);2280    }2281  }2282}22832284// does uninitialized data start out zero?2285char uninit[10000];2286void2287bsstest(char *s)2288{2289  int i;22902291  for(i = 0; i < sizeof(uninit); i++){2292    if(uninit[i] != '\0'){2293      printf("%s: bss test failed\n", s);2294      exit(1);2295    }2296  }2297}22982299// does exec return an error if the arguments2300// are larger than a page? or does it write2301// below the stack and wreck the instructions/data?2302void2303bigargtest(char *s)2304{2305  int pid, fd, xstatus;23062307  unlink("bigarg-ok");2308  pid = fork();2309  if(pid == 0){2310    static char *args[MAXARG];2311    int i;2312    char big[400];2313    memset(big, ' ', sizeof(big));2314    big[sizeof(big)-1] = '\0';2315    for(i = 0; i < MAXARG-1; i++)2316      args[i] = big;2317    args[MAXARG-1] = 0;2318    // this exec() should fail (and return) because the2319    // arguments are too large.2320    exec("echo", args);2321    fd = open("bigarg-ok", O_CREATE);2322    close(fd);2323    exit(0);2324  } else if(pid < 0){2325    printf("%s: bigargtest: fork failed\n", s);2326    exit(1);2327  }2328  2329  wait(&xstatus);2330  if(xstatus != 0)2331    exit(xstatus);2332  fd = open("bigarg-ok", 0);2333  if(fd < 0){2334    printf("%s: bigarg test failed!\n", s);2335    exit(1);2336  }2337  close(fd);2338}23392340// what happens when the file system runs out of blocks?2341// answer: balloc panics, so this test is not useful.2342void2343fsfull()2344{2345  int nfiles;2346  int fsblocks = 0;23472348  printf("fsfull test\n");23492350  for(nfiles = 0; ; nfiles++){2351    char name[64];2352    name[0] = 'f';2353    name[1] = '0' + nfiles / 1000;2354    name[2] = '0' + (nfiles % 1000) / 100;2355    name[3] = '0' + (nfiles % 100) / 10;2356    name[4] = '0' + (nfiles % 10);2357    name[5] = '\0';2358    printf("writing %s\n", name);2359    int fd = open(name, O_CREATE|O_RDWR);2360    if(fd < 0){2361      printf("open %s failed\n", name);2362      break;2363    }2364    int total = 0;2365    while(1){2366      int cc = write(fd, buf, BSIZE);2367      if(cc < BSIZE)2368        break;2369      total += cc;2370      fsblocks++;2371    }2372    printf("wrote %d bytes\n", total);2373    close(fd);2374    if(total == 0)2375      break;2376  }23772378  while(nfiles >= 0){2379    char name[64];2380    name[0] = 'f';2381    name[1] = '0' + nfiles / 1000;2382    name[2] = '0' + (nfiles % 1000) / 100;2383    name[3] = '0' + (nfiles % 100) / 10;2384    name[4] = '0' + (nfiles % 10);2385    name[5] = '\0';2386    unlink(name);2387    nfiles--;2388  }23892390  printf("fsfull test finished\n");2391}23922393void argptest(char *s)2394{2395  int fd;2396  fd = open("init", O_RDONLY);2397  if (fd < 0) {2398    printf("%s: open failed\n", s);2399    exit(1);2400  }2401  read(fd, sbrk(0) - 1, -1);2402  close(fd);2403}24042405// check that there's an invalid page beneath2406// the user stack, to catch stack overflow.2407void2408stacktest(char *s)2409{2410  int pid;2411  int xstatus;2412  2413  pid = fork();2414  if(pid == 0) {2415    char *sp = (char *) r_sp();2416    sp -= USERSTACK*PGSIZE;2417    // the *sp should cause a trap.2418    printf("%s: stacktest: read below stack %d\n", s, *sp);2419    exit(1);2420  } else if(pid < 0){2421    printf("%s: fork failed\n", s);2422    exit(1);2423  }2424  wait(&xstatus);2425  if(xstatus == -1)  // kernel killed child?2426    exit(0);2427  else2428    exit(xstatus);2429}24302431// check that writes to a few forbidden addresses2432// cause a fault, e.g. process's text and TRAMPOLINE.2433void2434nowrite(char *s)2435{2436  int pid;2437  int xstatus;2438  uint64 addrs[] = { 0, 0x80000000LL, 0x3fffffe000, 0x3ffffff000, 0x4000000000,2439                     0xffffffffffffffff };2440  2441  for(int ai = 0; ai < sizeof(addrs)/sizeof(addrs[0]); ai++){2442    pid = fork();2443    if(pid == 0) {2444      volatile int *addr = (int *) addrs[ai];2445      *addr = 10;2446      printf("%s: write to %p did not fail!\n", s, addr);2447      exit(0);2448    } else if(pid < 0){2449      printf("%s: fork failed\n", s);2450      exit(1);2451    }2452    wait(&xstatus);2453    if(xstatus == 0){2454      // kernel did not kill child!2455      exit(1);2456    }2457  }2458  exit(0);2459}24602461// regression test. copyin(), copyout(), and copyinstr() used to cast2462// the virtual page address to uint, which (with certain wild system2463// call arguments) resulted in a kernel page faults.2464void *big = (void*) 0xeaeb0b5b00002f5e;2465void2466pgbug(char *s)2467{2468  char *argv[1];2469  argv[0] = 0;2470  exec(big, argv);2471  pipe(big);24722473  exit(0);2474}24752476// regression test. does the kernel panic if a process sbrk()s its2477// size to be less than a page, or zero, or reduces the break by an2478// amount too small to cause a page to be freed?2479void2480sbrkbugs(char *s)2481{2482  int pid = fork();2483  if(pid < 0){2484    printf("fork failed\n");2485    exit(1);2486  }2487  if(pid == 0){2488    int sz = (uint64) sbrk(0);2489    // free all user memory; there used to be a bug that2490    // would not adjust p->sz correctly in this case,2491    // causing exit() to panic.2492    sbrk(-sz);2493    // user page fault here.2494    exit(0);2495  }2496  wait(0);24972498  pid = fork();2499  if(pid < 0){2500    printf("fork failed\n");2501    exit(1);2502  }2503  if(pid == 0){2504    int sz = (uint64) sbrk(0);2505    // set the break to somewhere in the very first2506    // page; there used to be a bug that would incorrectly2507    // free the first page.2508    sbrk(-(sz - 3500));2509    exit(0);2510  }2511  wait(0);25122513  pid = fork();2514  if(pid < 0){2515    printf("fork failed\n");2516    exit(1);2517  }2518  if(pid == 0){2519    // set the break in the middle of a page.2520    sbrk((10*PGSIZE + 2048) - (uint64)sbrk(0));25212522    // reduce the break a bit, but not enough to2523    // cause a page to be freed. this used to cause2524    // a panic.2525    sbrk(-10);25262527    exit(0);2528  }2529  wait(0);25302531  exit(0);2532}25332534// if process size was somewhat more than a page boundary, and then2535// shrunk to be somewhat less than that page boundary, can the kernel2536// still copyin() from addresses in the last page?2537void2538sbrklast(char *s)2539{2540  uint64 top = (uint64) sbrk(0);2541  if((top % PGSIZE) != 0)2542    sbrk(PGSIZE - (top % PGSIZE));2543  sbrk(PGSIZE);2544  sbrk(10);2545  sbrk(-20);2546  top = (uint64) sbrk(0);2547  char *p = (char *) (top - 64);2548  p[0] = 'x';2549  p[1] = '\0';2550  int fd = open(p, O_RDWR|O_CREATE);2551  write(fd, p, 1);2552  close(fd);2553  fd = open(p, O_RDWR);2554  p[0] = '\0';2555  read(fd, p, 1);2556  if(p[0] != 'x')2557    exit(1);2558}255925602561// does sbrk handle signed int32 wrap-around with2562// negative arguments?2563void2564sbrk8000(char *s)2565{2566  sbrk(0x80000004);2567  volatile char *top = sbrk(0);2568  *(top-1) = *(top-1) + 1;2569}2570257125722573// regression test. test whether exec() leaks memory if one of the2574// arguments is invalid. the test passes if the kernel doesn't panic.2575void2576badarg(char *s)2577{2578  for(int i = 0; i < 50000; i++){2579    char *argv[2];2580    argv[0] = (char*)0xffffffff;2581    argv[1] = 0;2582    exec("echo", argv);2583  }2584  2585  exit(0);2586}25872588#define REGION_SZ (1024 * 1024 * 1024)25892590// Touch a page every 64 pages, which with lazy allocation2591// causes one page to be allocated.2592void2593lazy_alloc(char *s)2594{2595  char *i, *prev_end, *new_end;2596  2597  prev_end = sbrklazy(REGION_SZ);2598  if (prev_end == (char *) SBRK_ERROR) {2599    printf("sbrklazy() failed\n");2600    exit(1);2601  }2602  new_end = prev_end + REGION_SZ;26032604  for (i = prev_end + PGSIZE; i < new_end; i += 64 * PGSIZE)2605    *(char **)i = i;26062607  for (i = prev_end + PGSIZE; i < new_end; i += 64 * PGSIZE) {2608    if (*(char **)i != i) {2609      printf("failed to read value from memory\n");2610      exit(1);2611    }2612  }26132614  exit(0);2615}26162617// Touch a page every 64 pages in region, which with lazy allocation2618// causes one page to be allocated. Check that freeing the region2619// frees the allocated pages.2620void2621lazy_unmap(char *s)2622{2623  int pid;2624  char *i, *prev_end, *new_end;26252626  prev_end = sbrklazy(REGION_SZ);2627  if (prev_end == (char*)SBRK_ERROR) {2628    printf("sbrklazy() failed\n");2629    exit(1);2630  }2631  new_end = prev_end + REGION_SZ;26322633  for (i = prev_end + PGSIZE; i < new_end; i += PGSIZE * PGSIZE)2634    *(char **)i = i;26352636  for (i = prev_end + PGSIZE; i < new_end; i += PGSIZE * PGSIZE) {2637    pid = fork();2638    if (pid < 0) {2639      printf("error forking\n");2640      exit(1);2641    } else if (pid == 0) {2642      sbrklazy(-1L * REGION_SZ);2643      *(char **)i = i;2644      exit(0);2645    } else {2646      int status;2647      wait(&status);2648      if (status == 0) {2649        printf("memory not unmapped\n");2650        exit(1);2651      }2652    }2653  }26542655  exit(0);2656}26572658void2659lazy_copy(char *s)2660{2661  // copyinstr on lazy page2662  {2663    char *p = sbrk(0);2664    sbrklazy(4*PGSIZE);2665    open(p + 8192, 0);2666  }2667  2668  {2669    void *xx = sbrk(0);2670    void *ret = sbrk(-(((uint64) xx)+1));2671    if(ret != xx){2672      printf("sbrk(sbrk(0)+1) returned %p, not old sz\n", ret);2673      exit(1);2674    }2675  }26762677  2678  // read() and write() to these addresses should fail.2679  unsigned long bad[] = {2680    0x3fffffc000,2681    0x3fffffd000,2682    0x3fffffe000,2683    0x3ffffff000,2684    0x4000000000,2685    0x8000000000,2686  };2687  for(int i = 0; i < sizeof(bad)/sizeof(bad[0]); i++){2688    int fd = open("README", 0);2689    if(fd < 0) { printf("cannot open README\n"); exit(1); }2690    if(read(fd, (char*)bad[i], 512) >= 0) { printf("read succeeded\n");  exit(1); }2691    close(fd);2692    fd = open("junk", O_CREATE|O_RDWR|O_TRUNC);2693    if(fd < 0) { printf("cannot open junk\n"); exit(1); }2694    if(write(fd, (char*)bad[i], 512) >= 0) { printf("write succeeded\n"); exit(1); }2695    close(fd);2696  }26972698  exit(0);2699}27002701void2702lazy_sbrk(char *s)2703{2704  // sbrk() takes just int, so take 2^30-sized steps towards MAXVA2705  char *p = sbrk(0);2706  while ((uint64)p < MAXVA-(1<<30)) {2707    p = sbrklazy(1<<30);2708    if (p < 0) {2709      printf("sbrklazy(%d) returned %p\n", 1<<30, p);2710      exit(1);2711    }27122713    p = sbrklazy(0);2714  }27152716  int n = TRAPFRAME-PGSIZE-(uint64)p;27172718  char *p1 = sbrklazy(n);2719  if (p1 < 0 || p1 != p) {2720    printf("sbrklazy(%d) returned %p, not expected %p\n", n, p1, p);2721    exit(1);2722  }27232724  p = sbrk(PGSIZE);2725  if (p < 0 || (uint64)p != TRAPFRAME-PGSIZE) {2726    printf("sbrk(%d) returned %p, not expected TRAPFRAME-PGSIZE\n", PGSIZE, p);2727    exit(1);2728  }27292730  p[0] = 1;2731  if (p[1] != 0) {2732    printf("sbrk() returned non-zero-filled memory\n");2733    exit(1);2734  }27352736  p = sbrk(1);2737  if ((uint64)p != -1) {2738    printf("sbrk(1) returned %p, expected error\n", p);2739    exit(1);2740  }27412742  p = sbrklazy(1);2743  if ((uint64)p != -1) {2744    printf("sbrklazy(1) returned %p, expected error\n", p);2745    exit(1);2746  }27472748  exit(0);2749}27502751struct test {2752  void (*f)(char *);2753  char *s;2754} quicktests[] = {2755  {copyin, "copyin"},2756  {copyout, "copyout"},2757  {copyinstr1, "copyinstr1"},2758  {copyinstr2, "copyinstr2"},2759  {copyinstr3, "copyinstr3"},2760  {rwsbrk, "rwsbrk" },2761  {truncate1, "truncate1"},2762  {truncate2, "truncate2"},2763  {truncate3, "truncate3"},2764  {openiputtest, "openiput"},2765  {exitiputtest, "exitiput"},2766  {iputtest, "iput"},2767  {opentest, "opentest"},2768  {writetest, "writetest"},2769  {writebig, "writebig"},2770  {createtest, "createtest"},2771  {dirtest, "dirtest"},2772  {exectest, "exectest"},2773  {pipe1, "pipe1"},2774  {killstatus, "killstatus"},2775  {preempt, "preempt"},2776  {exitwait, "exitwait"},2777  {reparent, "reparent" },2778  {twochildren, "twochildren"},2779  {forkfork, "forkfork"},2780  {forkforkfork, "forkforkfork"},2781  {reparent2, "reparent2"},2782  {mem, "mem"},2783  {sharedfd, "sharedfd"},2784  {fourfiles, "fourfiles"},2785  {createdelete, "createdelete"},2786  {unlinkread, "unlinkread"},2787  {linktest, "linktest"},2788  {concreate, "concreate"},2789  {linkunlink, "linkunlink"},2790  {subdir, "subdir"},2791  {bigwrite, "bigwrite"},2792  {bigfile, "bigfile"},2793  {fourteen, "fourteen"},2794  {rmdot, "rmdot"},2795  {dirfile, "dirfile"},2796  {iref, "iref"},2797  {forktest, "forktest"},2798  {sbrkbasic, "sbrkbasic"},2799  {sbrkmuch, "sbrkmuch"},2800  {kernmem, "kernmem"},2801  {MAXVAplus, "MAXVAplus"},2802  {sbrkfail, "sbrkfail"},2803  {sbrkarg, "sbrkarg"},2804  {validatetest, "validatetest"},2805  {bsstest, "bsstest"},2806  {bigargtest, "bigargtest"},2807  {argptest, "argptest"},2808  {stacktest, "stacktest"},2809  {nowrite, "nowrite"},2810  {pgbug, "pgbug" },2811  {sbrkbugs, "sbrkbugs" },2812  {sbrklast, "sbrklast"},2813  {sbrk8000, "sbrk8000"},2814  {badarg, "badarg" },2815  {lazy_alloc, "lazy_alloc"},2816  {lazy_unmap, "lazy_unmap"},2817  {lazy_copy, "lazy_copy"},2818  {lazy_sbrk, "lazy_sbrk"},2819  { 0, 0},2820};28212822//2823// Section with tests that take a fair bit of time2824//28252826// directory that uses indirect blocks2827void2828bigdir(char *s)2829{2830  enum { N = 500 };2831  int i, fd;2832  char name[10];28332834  unlink("bd");28352836  fd = open("bd", O_CREATE);2837  if(fd < 0){2838    printf("%s: bigdir create failed\n", s);2839    exit(1);2840  }2841  close(fd);28422843  for(i = 0; i < N; i++){2844    name[0] = 'x';2845    name[1] = '0' + (i / 64);2846    name[2] = '0' + (i % 64);2847    name[3] = '\0';2848    if(link("bd", name) != 0){2849      printf("%s: bigdir i=%d link(bd, %s) failed\n", s, i, name);2850      exit(1);2851    }2852  }28532854  unlink("bd");2855  for(i = 0; i < N; i++){2856    name[0] = 'x';2857    name[1] = '0' + (i / 64);2858    name[2] = '0' + (i % 64);2859    name[3] = '\0';2860    if(unlink(name) != 0){2861      printf("%s: bigdir unlink failed", s);2862      exit(1);2863    }2864  }2865}28662867// concurrent writes to try to provoke deadlock in the virtio disk2868// driver.2869void2870manywrites(char *s)2871{2872  int nchildren = 4;2873  int howmany = 30; // increase to look for deadlock2874  2875  for(int ci = 0; ci < nchildren; ci++){2876    int pid = fork();2877    if(pid < 0){2878      printf("fork failed\n");2879      exit(1);2880    }28812882    if(pid == 0){2883      char name[3];2884      name[0] = 'b';2885      name[1] = 'a' + ci;2886      name[2] = '\0';2887      unlink(name);2888      2889      for(int iters = 0; iters < howmany; iters++){2890        for(int i = 0; i < ci+1; i++){2891          int fd = open(name, O_CREATE | O_RDWR);2892          if(fd < 0){2893            printf("%s: cannot create %s\n", s, name);2894            exit(1);2895          }2896          int sz = sizeof(buf);2897          int cc = write(fd, buf, sz);2898          if(cc != sz){2899            printf("%s: write(%d) ret %d\n", s, sz, cc);2900            exit(1);2901          }2902          close(fd);2903        }2904        unlink(name);2905      }29062907      unlink(name);2908      exit(0);2909    }2910  }29112912  for(int ci = 0; ci < nchildren; ci++){2913    int st = 0;2914    wait(&st);2915    if(st != 0)2916      exit(st);2917  }2918  exit(0);2919}29202921// regression test. does write() with an invalid buffer pointer cause2922// a block to be allocated for a file that is then not freed when the2923// file is deleted? if the kernel has this bug, it will panic: balloc:2924// out of blocks. assumed_free may need to be raised to be more than2925// the number of free blocks. this test takes a long time.2926void2927badwrite(char *s)2928{2929  int assumed_free = 600;2930  2931  unlink("junk");2932  for(int i = 0; i < assumed_free; i++){2933    int fd = open("junk", O_CREATE|O_WRONLY);2934    if(fd < 0){2935      printf("open junk failed\n");2936      exit(1);2937    }2938    write(fd, (char*)0xffffffffffL, 1);2939    close(fd);2940    unlink("junk");2941  }29422943  int fd = open("junk", O_CREATE|O_WRONLY);2944  if(fd < 0){2945    printf("open junk failed\n");2946    exit(1);2947  }2948  if(write(fd, "x", 1) != 1){2949    printf("write failed\n");2950    exit(1);2951  }2952  close(fd);2953  unlink("junk");29542955  exit(0);2956}29572958// test the exec() code that cleans up if it runs out2959// of memory. it's really a test that such a condition2960// doesn't cause a panic.2961void2962execout(char *s)2963{2964  for(int avail = 0; avail < 15; avail++){2965    int pid = fork();2966    if(pid < 0){2967      printf("fork failed\n");2968      exit(1);2969    } else if(pid == 0){2970      // allocate all of memory.2971      while(1){2972        char *a = sbrk(PGSIZE);2973        if(a == SBRK_ERROR)2974          break;2975        *(a + PGSIZE - 1) = 1;2976      }29772978      // free a few pages, in order to let exec() make some2979      // progress.2980      for(int i = 0; i < avail; i++)2981        sbrk(-PGSIZE);2982      2983      close(1);2984      char *args[] = { "echo", "x", 0 };2985      exec("echo", args);2986      exit(0);2987    } else {2988      wait((int*)0);2989    }2990  }29912992  exit(0);2993}29942995// can the kernel tolerate running out of disk space?2996void2997diskfull(char *s)2998{2999  int fi;3000  int done = 0;30013002  unlink("diskfulldir");3003  3004  for(fi = 0; done == 0 && '0' + fi < 0177; fi++){3005    char name[32];3006    name[0] = 'b';3007    name[1] = 'i';3008    name[2] = 'g';3009    name[3] = '0' + fi;3010    name[4] = '\0';3011    unlink(name);3012    int fd = open(name, O_CREATE|O_RDWR|O_TRUNC);3013    if(fd < 0){3014      // oops, ran out of inodes before running out of blocks.3015      printf("%s: could not create file %s\n", s, name);3016      done = 1;3017      break;3018    }3019    for(int i = 0; i < MAXFILE; i++){3020      char buf[BSIZE];3021      if(write(fd, buf, BSIZE) != BSIZE){3022        done = 1;3023        close(fd);3024        break;3025      }3026    }3027    close(fd);3028  }30293030  // now that there are no free blocks, test that dirlink()3031  // merely fails (doesn't panic) if it can't extend3032  // directory content. one of these file creations3033  // is expected to fail.3034  int nzz = 128;3035  for(int i = 0; i < nzz; i++){3036    char name[32];3037    name[0] = 'z';3038    name[1] = 'z';3039    name[2] = '0' + (i / 32);3040    name[3] = '0' + (i % 32);3041    name[4] = '\0';3042    unlink(name);3043    int fd = open(name, O_CREATE|O_RDWR|O_TRUNC);3044    if(fd < 0)3045      break;3046    close(fd);3047  }30483049  // this mkdir() is expected to fail.3050  if(mkdir("diskfulldir") == 0)3051    printf("%s: mkdir(diskfulldir) unexpectedly succeeded!\n", s);30523053  unlink("diskfulldir");30543055  for(int i = 0; i < nzz; i++){3056    char name[32];3057    name[0] = 'z';3058    name[1] = 'z';3059    name[2] = '0' + (i / 32);3060    name[3] = '0' + (i % 32);3061    name[4] = '\0';3062    unlink(name);3063  }30643065  for(int i = 0; '0' + i < 0177; i++){3066    char name[32];3067    name[0] = 'b';3068    name[1] = 'i';3069    name[2] = 'g';3070    name[3] = '0' + i;3071    name[4] = '\0';3072    unlink(name);3073  }3074}30753076void3077outofinodes(char *s)3078{3079  int nzz = 32*32;3080  for(int i = 0; i < nzz; i++){3081    char name[32];3082    name[0] = 'z';3083    name[1] = 'z';3084    name[2] = '0' + (i / 32);3085    name[3] = '0' + (i % 32);3086    name[4] = '\0';3087    unlink(name);3088    int fd = open(name, O_CREATE|O_RDWR|O_TRUNC);3089    if(fd < 0){3090      // failure is eventually expected.3091      break;3092    }3093    close(fd);3094  }30953096  for(int i = 0; i < nzz; i++){3097    char name[32];3098    name[0] = 'z';3099    name[1] = 'z';3100    name[2] = '0' + (i / 32);3101    name[3] = '0' + (i % 32);3102    name[4] = '\0';3103    unlink(name);3104  }3105}31063107struct test slowtests[] = {3108  {bigdir, "bigdir"},3109  {manywrites, "manywrites"},3110  {badwrite, "badwrite" },3111  {execout, "execout"},3112  {diskfull, "diskfull"},3113  {outofinodes, "outofinodes"},3114    3115  { 0, 0},3116};31173118//3119// drive tests3120//31213122// run each test in its own process. run returns 1 if child's exit()3123// indicates success.3124int3125run(void f(char *), char *s) {3126  int pid;3127  int xstatus;31283129  printf("test %s: ", s);3130  if((pid = fork()) < 0) {3131    printf("runtest: fork error\n");3132    exit(1);3133  }3134  if(pid == 0) {3135    f(s);3136    exit(0);3137  } else {3138    wait(&xstatus);3139    if(xstatus != 0) 3140      printf("FAILED\n");3141    else3142      printf("OK\n");3143    return xstatus == 0;3144  }3145}31463147int3148runtests(struct test *tests, char *justone, int continuous) {3149  int ntests = 0;3150  for (struct test *t = tests; t->s != 0; t++) {3151    if((justone == 0) || strcmp(t->s, justone) == 0) {3152      ntests++;3153      if(!run(t->f, t->s)){3154        if(continuous != 2){3155          printf("SOME TESTS FAILED\n");3156          return -1;3157        }3158      }3159    }3160  }3161  return ntests;3162}316331643165// use sbrk() to count how many free physical memory pages there are.3166int3167countfree()3168{3169  int n = 0;3170  uint64 sz0 = (uint64)sbrk(0);3171  while(1){3172    char *a = sbrk(PGSIZE);3173    if(a == SBRK_ERROR){3174      break;3175    }3176    n += 1;3177  }3178  sbrk(-((uint64)sbrk(0) - sz0));  3179  return n;3180}31813182int3183drivetests(int quick, int continuous, char *justone) {3184  do {3185    printf("usertests starting\n");3186    int free0 = countfree();3187    int free1 = 0;3188    int ntests = 0;3189    int n;3190    n = runtests(quicktests, justone, continuous);3191    if (n < 0) {3192      if(continuous != 2) {3193        return 1;3194      }3195    } else {3196      ntests += n;3197    }3198    if(!quick) {3199      if (justone == 0)3200        printf("usertests slow tests starting\n");3201      n = runtests(slowtests, justone, continuous);3202      if (n < 0) {3203        if(continuous != 2) {3204          return 1;3205        }3206      } else {3207        ntests += n;3208      }3209    }3210    if((free1 = countfree()) < free0) {3211      printf("FAILED -- lost some free pages %d (out of %d)\n", free1, free0);3212      if(continuous != 2) {3213        return 1;3214      }3215    }3216    if (justone != 0 && ntests == 0) {3217      printf("NO TESTS EXECUTED\n");3218      return 1;3219    }3220  } while(continuous);3221  return 0;3222}32233224int3225main(int argc, char *argv[])3226{3227  int continuous = 0;3228  int quick = 0;3229  char *justone = 0;32303231  if(argc == 2 && strcmp(argv[1], "-q") == 0){3232    quick = 1;3233  } else if(argc == 2 && strcmp(argv[1], "-c") == 0){3234    continuous = 1;3235  } else if(argc == 2 && strcmp(argv[1], "-C") == 0){3236    continuous = 2;3237  } else if(argc == 2 && argv[1][0] != '-'){3238    justone = argv[1];3239  } else if(argc > 1){3240    printf("Usage: usertests [-c] [-C] [-q] [testname]\n");3241    exit(1);3242  }3243  if (drivetests(quick, continuous, justone)) {3244    exit(1);3245  }3246  printf("ALL TESTS PASSED\n");3247  exit(0);3248}3249