summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--conf/lab.mk2
-rwxr-xr-xgrade-lab-cow55
-rw-r--r--user/cowtest.c197
3 files changed, 253 insertions, 1 deletions
diff --git a/conf/lab.mk b/conf/lab.mk
index 2b9988d..629f978 100644
--- a/conf/lab.mk
+++ b/conf/lab.mk
@@ -1 +1 @@
-LAB=traps
+LAB=cow
diff --git a/grade-lab-cow b/grade-lab-cow
new file mode 100755
index 0000000..08b6990
--- /dev/null
+++ b/grade-lab-cow
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+
+import re
+from gradelib import *
+
+r = Runner(save("xv6.out"))
+
+@test(0, "running cowtest")
+def test_cowtest():
+ r.run_qemu(shell_script([
+ 'cowtest'
+ ]))
+
+@test(30, "simple", parent=test_cowtest)
+def test_simple():
+ matches = re.findall("^simple: ok$", r.qemu.output, re.M)
+ assert_equal(len(matches), 2, "Number of appearances of 'simple: ok'")
+
+@test(30, "three", parent=test_cowtest)
+def test_three():
+ matches = re.findall("^three: ok$", r.qemu.output, re.M)
+ assert_equal(len(matches), 3, "Number of appearances of 'three: ok'")
+
+@test(20, "file", parent=test_cowtest)
+def test_file():
+ r.match('^file: ok$')
+
+@test(0, "usertests")
+def test_usertests():
+ r.run_qemu(shell_script([
+ 'usertests -q'
+ ]), timeout=1000)
+ r.match('^ALL TESTS PASSED$')
+
+def usertest_check(testcase, nextcase, output):
+ if not re.search(r'\ntest {}: [\s\S]*OK\ntest {}'.format(testcase, nextcase), output):
+ raise AssertionError('Failed ' + testcase)
+
+@test(5, "usertests: copyin", parent=test_usertests)
+def test_sbrkbugs():
+ usertest_check("copyin", "copyout", r.qemu.output)
+
+@test(5, "usertests: copyout", parent=test_usertests)
+def test_sbrkbugs():
+ usertest_check("copyout", "copyinstr1", r.qemu.output)
+
+@test(19, "usertests: all tests", parent=test_usertests)
+def test_usertests_all():
+ r.match('^ALL TESTS PASSED$')
+
+@test(1, "time")
+def test_time():
+ check_time()
+
+run_tests()
diff --git a/user/cowtest.c b/user/cowtest.c
new file mode 100644
index 0000000..29b918f
--- /dev/null
+++ b/user/cowtest.c
@@ -0,0 +1,197 @@
+//
+// tests for copy-on-write fork() assignment.
+//
+
+#include "kernel/types.h"
+#include "kernel/memlayout.h"
+#include "user/user.h"
+
+// allocate more than half of physical memory,
+// then fork. this will fail in the default
+// kernel, which does not support copy-on-write.
+void
+simpletest()
+{
+ uint64 phys_size = PHYSTOP - KERNBASE;
+ int sz = (phys_size / 3) * 2;
+
+ printf("simple: ");
+
+ char *p = sbrk(sz);
+ if(p == (char*)0xffffffffffffffffL){
+ printf("sbrk(%d) failed\n", sz);
+ exit(-1);
+ }
+
+ for(char *q = p; q < p + sz; q += 4096){
+ *(int*)q = getpid();
+ }
+
+ int pid = fork();
+ if(pid < 0){
+ printf("fork() failed\n");
+ exit(-1);
+ }
+
+ if(pid == 0)
+ exit(0);
+
+ wait(0);
+
+ if(sbrk(-sz) == (char*)0xffffffffffffffffL){
+ printf("sbrk(-%d) failed\n", sz);
+ exit(-1);
+ }
+
+ printf("ok\n");
+}
+
+// three processes all write COW memory.
+// this causes more than half of physical memory
+// to be allocated, so it also checks whether
+// copied pages are freed.
+void
+threetest()
+{
+ uint64 phys_size = PHYSTOP - KERNBASE;
+ int sz = phys_size / 4;
+ int pid1, pid2;
+
+ printf("three: ");
+
+ char *p = sbrk(sz);
+ if(p == (char*)0xffffffffffffffffL){
+ printf("sbrk(%d) failed\n", sz);
+ exit(-1);
+ }
+
+ pid1 = fork();
+ if(pid1 < 0){
+ printf("fork failed\n");
+ exit(-1);
+ }
+ if(pid1 == 0){
+ pid2 = fork();
+ if(pid2 < 0){
+ printf("fork failed");
+ exit(-1);
+ }
+ if(pid2 == 0){
+ for(char *q = p; q < p + (sz/5)*4; q += 4096){
+ *(int*)q = getpid();
+ }
+ for(char *q = p; q < p + (sz/5)*4; q += 4096){
+ if(*(int*)q != getpid()){
+ printf("wrong content\n");
+ exit(-1);
+ }
+ }
+ exit(-1);
+ }
+ for(char *q = p; q < p + (sz/2); q += 4096){
+ *(int*)q = 9999;
+ }
+ exit(0);
+ }
+
+ for(char *q = p; q < p + sz; q += 4096){
+ *(int*)q = getpid();
+ }
+
+ wait(0);
+
+ sleep(1);
+
+ for(char *q = p; q < p + sz; q += 4096){
+ if(*(int*)q != getpid()){
+ printf("wrong content\n");
+ exit(-1);
+ }
+ }
+
+ if(sbrk(-sz) == (char*)0xffffffffffffffffL){
+ printf("sbrk(-%d) failed\n", sz);
+ exit(-1);
+ }
+
+ printf("ok\n");
+}
+
+char junk1[4096];
+int fds[2];
+char junk2[4096];
+char buf[4096];
+char junk3[4096];
+
+// test whether copyout() simulates COW faults.
+void
+filetest()
+{
+ printf("file: ");
+
+ buf[0] = 99;
+
+ for(int i = 0; i < 4; i++){
+ if(pipe(fds) != 0){
+ printf("pipe() failed\n");
+ exit(-1);
+ }
+ int pid = fork();
+ if(pid < 0){
+ printf("fork failed\n");
+ exit(-1);
+ }
+ if(pid == 0){
+ sleep(1);
+ if(read(fds[0], buf, sizeof(i)) != sizeof(i)){
+ printf("error: read failed\n");
+ exit(1);
+ }
+ sleep(1);
+ int j = *(int*)buf;
+ if(j != i){
+ printf("error: read the wrong value\n");
+ exit(1);
+ }
+ exit(0);
+ }
+ if(write(fds[1], &i, sizeof(i)) != sizeof(i)){
+ printf("error: write failed\n");
+ exit(-1);
+ }
+ }
+
+ int xstatus = 0;
+ for(int i = 0; i < 4; i++) {
+ wait(&xstatus);
+ if(xstatus != 0) {
+ exit(1);
+ }
+ }
+
+ if(buf[0] != 99){
+ printf("error: child overwrote parent\n");
+ exit(1);
+ }
+
+ printf("ok\n");
+}
+
+int
+main(int argc, char *argv[])
+{
+ simpletest();
+
+ // check that the first simpletest() freed the physical memory.
+ simpletest();
+
+ threetest();
+ threetest();
+ threetest();
+
+ filetest();
+
+ printf("ALL COW TESTS PASSED\n");
+
+ exit(0);
+}