From fedf80f5995e6fbdda65d37bef6282216fb020f6 Mon Sep 17 00:00:00 2001 From: Scott Haseley Date: Wed, 6 Apr 2016 13:53:21 -0400 Subject: [PATCH] Userland changes for test161/assignment 3. --- userland/testbin/bigfork/bigfork.c | 21 ++-- userland/testbin/ctest/ctest.c | 8 +- userland/testbin/huge/huge.c | 10 +- userland/testbin/matmult/matmult.c | 43 +++++--- userland/testbin/palin/palin.c | 5 +- userland/testbin/parallelvm/parallelvm.c | 41 +++----- userland/testbin/sbrktest/sbrktest.c | 121 +++++++++++++++++------ userland/testbin/sort/sort.c | 7 +- userland/testbin/zero/zero.c | 7 +- 9 files changed, 180 insertions(+), 83 deletions(-) diff --git a/userland/testbin/bigfork/bigfork.c b/userland/testbin/bigfork/bigfork.c index d5f8949..52315fd 100644 --- a/userland/testbin/bigfork/bigfork.c +++ b/userland/testbin/bigfork/bigfork.c @@ -44,6 +44,8 @@ #include #include #include +#include +#include #define BRANCHES 6 @@ -76,7 +78,9 @@ init(void) for (j=0; j 0) { failures += WEXITSTATUS(status); @@ -196,7 +204,6 @@ dotest(void) unsigned i, me; pid_t pids[BRANCHES]; int t; - char msg[128]; me = 0; for (i=0; i 0; ) { dowait(pids[i]); } + if (failures > 0) { tprintf("%u failures.\n", failures); + success(TEST161_FAIL, SECRET, "/testbin/bigfork"); } else { tprintf("Done.\n"); + success(TEST161_SUCCESS, SECRET, "/testbin/bigfork"); } } diff --git a/userland/testbin/ctest/ctest.c b/userland/testbin/ctest/ctest.c index b919ca9..103a15e 100644 --- a/userland/testbin/ctest/ctest.c +++ b/userland/testbin/ctest/ctest.c @@ -38,6 +38,7 @@ #include #include +#include /* * SIZE is the amount of memory used. @@ -90,12 +91,11 @@ main(int argc, char **argv) */ e = &array[0]; for (i=0; ie; } - tprintf("\nDone!\n"); + // Success is not crashing + success(TEST161_SUCCESS, SECRET, "/testbin/ctest"); return 0; } diff --git a/userland/testbin/huge/huge.c b/userland/testbin/huge/huge.c index 8e964c2..3ed3db6 100644 --- a/userland/testbin/huge/huge.c +++ b/userland/testbin/huge/huge.c @@ -39,6 +39,7 @@ #include #include +#include #define PageSize 4096 #define NumPages 512 @@ -54,30 +55,37 @@ main(void) /* move number in so that sparse[i][0]=i */ for (i=0; i=0; i--) { + TEST161_TPROGRESS(i); if (sparse[i][0]!=i+5) { tprintf("BAD NEWS!!! - your VM mechanism has a bug!\n"); + success(TEST161_FAIL, SECRET, "/testbin/huge"); exit(1); } } - tprintf("You passed!\n"); + nprintf("\n"); + success(TEST161_SUCCESS, SECRET, "/testbin/huge"); return 0; } diff --git a/userland/testbin/matmult/matmult.c b/userland/testbin/matmult/matmult.c index f6c4dd6..a64d85d 100644 --- a/userland/testbin/matmult/matmult.c +++ b/userland/testbin/matmult/matmult.c @@ -41,6 +41,7 @@ #include #include +#include #define Dim 72 /* sum total of the arrays doesn't fit in * physical memory @@ -58,22 +59,35 @@ main(void) { int i, j, k, r; - for (i = 0; i < Dim; i++) /* first initialize the matrices */ - for (j = 0; j < Dim; j++) { - A[i][j] = i; - B[i][j] = j; - C[i][j] = 0; + for (i = 0; i < Dim; i++) { /* first initialize the matrices */ + for (j = 0; j < Dim; j++) { + TEST161_TPROGRESS_N(i*Dim + j, 1000); + A[i][j] = i; + B[i][j] = j; + C[i][j] = 0; + } } + nprintf("\n"); - for (i = 0; i < Dim; i++) /* then multiply them together */ - for (j = 0; j < Dim; j++) - for (k = 0; k < Dim; k++) - T[i][j][k] = A[i][k] * B[k][j]; + for (i = 0; i < Dim; i++) { /* then multiply them together */ + for (j = 0; j < Dim; j++) { + for (k = 0; k < Dim; k++) { + TEST161_TPROGRESS_N(i*j*Dim*Dim + k, 50000); + T[i][j][k] = A[i][k] * B[k][j]; + } + } + } + nprintf("\n"); - for (i = 0; i < Dim; i++) - for (j = 0; j < Dim; j++) - for (k = 0; k < Dim; k++) - C[i][j] += T[i][j][k]; + for (i = 0; i < Dim; i++) { + for (j = 0; j < Dim; j++) { + for (k = 0; k < Dim; k++) { + TEST161_TPROGRESS_N(i*j*Dim*Dim + k, 50000); + C[i][j] += T[i][j][k]; + } + } + } + nprintf("\n"); r = 0; for (i = 0; i < Dim; i++) @@ -83,8 +97,11 @@ main(void) tprintf("answer is: %d (should be %d)\n", r, RIGHT); if (r != RIGHT) { tprintf("FAILED\n"); + success(TEST161_FAIL, SECRET, "/testbin/matmult"); return 1; } + tprintf("Passed.\n"); + success(TEST161_SUCCESS, SECRET, "/testbin/matmult"); return 0; } diff --git a/userland/testbin/palin/palin.c b/userland/testbin/palin/palin.c index 885dbe1..fe318b4 100644 --- a/userland/testbin/palin/palin.c +++ b/userland/testbin/palin/palin.c @@ -83,6 +83,7 @@ a canal - Panama! #include #include +#include char palindrome[8000] = "amanaplanacaretabanamyriadasumalacaliarahoopapintacatalpaagasanoil" @@ -186,13 +187,13 @@ main(void) end--; for (start = palindrome; start <= end; start++, end--) { - putchar('.'); if (*start != *end) { + success(TEST161_FAIL, SECRET, "/testbin/palin"); tprintf("NOT a palindrome\n"); return 0; } } - tprintf("IS a palindrome\n"); + success(TEST161_SUCCESS, SECRET, "/testbin/palin"); return 0; } diff --git a/userland/testbin/parallelvm/parallelvm.c b/userland/testbin/parallelvm/parallelvm.c index 5939caf..e5333ea 100644 --- a/userland/testbin/parallelvm/parallelvm.c +++ b/userland/testbin/parallelvm/parallelvm.c @@ -45,6 +45,8 @@ #include #include #include +#include +#include #define NJOBS 24 @@ -87,24 +89,6 @@ struct matrix { //////////////////////////////////////////////////////////// -/* - * Use this instead of just calling tprintf so we know each printout - * is atomic; this prevents the lines from getting intermingled. - */ -static -void -say(const char *fmt, ...) -{ - char buf[256]; - va_list ap; - va_start(ap, fmt); - vsnprintf(buf, sizeof(buf), fmt, ap); - va_end(ap); - write(STDOUT_FILENO, buf, strlen(buf)); -} - -//////////////////////////////////////////////////////////// - static void multiply(struct matrix *res, const struct matrix *m1, const struct matrix *m2) @@ -120,6 +104,7 @@ multiply(struct matrix *res, const struct matrix *m1, const struct matrix *m2) res->m_data[i][j] = val; } } + TEST161_TPROGRESS(0); } static @@ -201,18 +186,20 @@ go(int mynum) { int r; - say("Process %d (pid %d) starting computation...\n", mynum, + tsay("Process %d (pid %d) starting computation...\n", mynum, (int) getpid()); - computeall(mynum); r = answer(); if (r != right_answers[mynum]) { - say("Process %d answer %d: FAILED, should be %d\n", + tsay("Process %d answer %d: FAILED, should be %d\n", mynum, r, right_answers[mynum]); + success(TEST161_FAIL, SECRET, "/testbin/parallelvm"); exit(1); } - say("Process %d answer %d: passed\n", mynum, r); + + tsay("Process %d answer %d: passed\n", mynum, r); + nsay("\nProc %d OK\n", mynum); exit(0); } @@ -341,7 +328,7 @@ makeprocs(bool dowait) if (pids[i]==0) { /* child */ if (dowait) { - say("Process %d forked\n", i); + tsay("Process %d forked\n", i); semopen(&s1); semopen(&s2); semV(&s1, 1); @@ -356,9 +343,9 @@ makeprocs(bool dowait) if (dowait) { semopen(&s1); semopen(&s2); - say("Waiting for fork...\n"); + tsay("Waiting for fork...\n"); semP(&s1, NJOBS); - say("Starting computation.\n"); + tsay("Starting computation.\n"); semV(&s2, NJOBS); } @@ -378,10 +365,12 @@ makeprocs(bool dowait) } if (failcount>0) { - tprintf("%d subprocesses failed\n", failcount); + printf("%d subprocesses failed\n", failcount); exit(1); } + nprintf("\n"); tprintf("Test complete\n"); + success(TEST161_SUCCESS, SECRET, "/testbin/parallelvm"); semclose(&s1); semclose(&s2); diff --git a/userland/testbin/sbrktest/sbrktest.c b/userland/testbin/sbrktest/sbrktest.c index c9c3816..0327d56 100644 --- a/userland/testbin/sbrktest/sbrktest.c +++ b/userland/testbin/sbrktest/sbrktest.c @@ -35,6 +35,7 @@ #include #include #include +#include #define _PATH_RANDOM "random:" @@ -271,6 +272,37 @@ dosbrk(ssize_t size) return p; } +//////////////////////////////////////////////////////////// +// fork a child that segfaults + +typedef void (*segfault_fn)(void); + +static +void +expect_segfault(segfault_fn func) +{ + int status; + int result; + pid_t pid = dofork(); + + if (pid == 0) { + func(); // This exits + } else { + result = waitpid(pid, &status, 0); + if (result == -1) { + err(1, "waitpid"); + } + else if (WIFSIGNALED(status)) { + if (WTERMSIG(status) != 11) { + errx(1, "child: Signal %d", WTERMSIG(status)); + } + } + else { + errx(1, "child exited, expected segfault"); + } + } +} + //////////////////////////////////////////////////////////// // align the heap @@ -323,8 +355,7 @@ test1(void) if (checkpage(p, 0, false)) { errx(1, "FAILED: data corrupt"); } - - tprintf("Passed sbrk test 1.\n"); + success(TEST161_SUCCESS, SECRET, "/testbin/sbrktest"); } /* @@ -362,8 +393,7 @@ test2(void) errx(1, "FAILED: sbrk shrink didn't restore the heap " "(got %p, expected %p", q, op); } - - tprintf("Passed sbrk test 2.\n"); + success(TEST161_SUCCESS, SECRET, "/testbin/sbrktest"); } /* @@ -414,8 +444,7 @@ test3(void) errx(1, "FAILED: sbrk shrink didn't restore the heap " "(got %p, expected %p", q, op); } - - tprintf("Passed sbrk test 3.\n"); + success(TEST161_SUCCESS, SECRET, "/testbin/sbrktest"); } /* @@ -474,8 +503,7 @@ test4(void) errx(1, "FAILED: sbrk shrink didn't restore the heap " "(got %p, expected %p", q, op); } - - tprintf("Passed sbrk test 4.\n"); + success(TEST161_SUCCESS, SECRET, "/testbin/sbrktest"); } //////////////////////////////////////////////////////////// @@ -487,7 +515,7 @@ test4(void) */ static void -test5(void) +test5_helper(void) { void *p; @@ -497,13 +525,21 @@ test5(void) errx(1, "FAILED: I didn't crash"); } +static +void +test5(void) +{ + expect_segfault(test5_helper); + success(TEST161_SUCCESS, SECRET, "/testbin/sbrktest"); +} + /* * Allocates a page and checks that the next page past it is not * valid. (Crashes when successful.) */ static void -test6(void) +test6_helper(void) { void *p; @@ -514,13 +550,21 @@ test6(void) errx(1, "FAILED: I didn't crash"); } +static +void +test6(void) +{ + expect_segfault(test6_helper); + success(TEST161_SUCCESS, SECRET, "/testbin/sbrktest"); +} + /* * Allocates and frees a page and checks that the page freed is no * longer valid. (Crashes when successful.) */ static void -test7(void) +test7_helper(void) { void *p; @@ -532,6 +576,14 @@ test7(void) errx(1, "FAILED: I didn't crash"); } +static +void +test7(void) +{ + expect_segfault(test7_helper); + success(TEST161_SUCCESS, SECRET, "/testbin/sbrktest"); +} + /* * Allocates some pages, frees half of them, and checks that the page * past the new end of the heap is no longer valid. (Crashes when @@ -539,7 +591,7 @@ test7(void) */ static void -test8(void) +test8_helper(void) { void *p; @@ -551,6 +603,14 @@ test8(void) errx(1, "FAILED: I didn't crash"); } +static +void +test8(void) +{ + expect_segfault(test8_helper); + success(TEST161_SUCCESS, SECRET, "/testbin/sbrktest"); +} + //////////////////////////////////////////////////////////// // heap size @@ -600,12 +660,12 @@ test9(void) dot = pages / 64; for (i=0; i 0 && i % dot == 0) { - tprintf("."); + if (dot > 0) { + TEST161_LPROGRESS_N(i, dot); } } if (dot > 0) { - tprintf("\n"); + printf("\n"); } tprintf("Testing each page.\n"); @@ -618,12 +678,12 @@ test9(void) warnx("FAILED: data corrupt"); bad = true; } - if (dot > 0 && i % dot == 0) { - tprintf("."); + if (dot > 0) { + TEST161_LPROGRESS_N(i, dot); } } if (dot > 0) { - tprintf("\n"); + printf("\n"); } if (bad) { exit(1); @@ -641,6 +701,7 @@ test9(void) tprintf("And really freeing it.\n"); (void)dosbrk(-size); tprintf("Passed sbrk test 9 (all)\n"); + success(TEST161_SUCCESS, SECRET, "/testbin/sbrktest"); } /* @@ -692,6 +753,7 @@ test10(void) (void)dosbrk(-PAGE_SIZE); tprintf("Passed sbrk test 10.\n"); + success(TEST161_SUCCESS, SECRET, "/testbin/sbrktest"); } //////////////////////////////////////////////////////////// @@ -714,9 +776,7 @@ test11(void) tprintf("Touching the pages.\n"); for (i=0; i #include #include +#include /* Larger than physical memory */ #define SIZE (144*1024) @@ -54,6 +55,8 @@ * Also, quicksort has somewhat more interesting memory usage patterns. */ +static int iters; + static void sort(int *arr, int size) @@ -64,6 +67,8 @@ sort(int *arr, int size) if (size<2) { return; } + TEST161_LPROGRESS_N(iters, 4000); + ++iters; pivot = size/2; sort(arr, pivot); @@ -122,7 +127,7 @@ check(void) i, A[i], i+1, A[i+1]); } } - warnx("Passed."); + success(TEST161_SUCCESS, SECRET, "/testbin/sort"); } int diff --git a/userland/testbin/zero/zero.c b/userland/testbin/zero/zero.c index f093f0d..48c2426 100644 --- a/userland/testbin/zero/zero.c +++ b/userland/testbin/zero/zero.c @@ -39,6 +39,7 @@ #include #include #include +#include /* * Some initialized data. This is here to increase the chance that @@ -78,6 +79,7 @@ check_data(void) if (k != SUM_OF_DATA_STUFF) { warnx("My initialized data sums to the wrong value!"); warnx("Got: %u Expected: %u", k, SUM_OF_DATA_STUFF); + success(TEST161_FAIL, SECRET, "/testbin/zero"); errx(1, "FAILED"); } } @@ -94,6 +96,7 @@ check_bss(void) warnx("BSS entry at index %u (address %p) not zero!", i, &bss_stuff[i]); warnx("Found: 0x%x", bss_stuff[i]); + success(TEST161_FAIL, SECRET, "/testbin/zero"); errx(1, "FAILED"); } } @@ -115,6 +118,7 @@ check_sbrk(void) tprintf("I guess you haven't implemented sbrk yet.\n"); return; } + success(TEST161_FAIL, SECRET, "/testbin/zero"); err(1, "sbrk"); } @@ -124,6 +128,7 @@ check_sbrk(void) i, &base[i]); warnx("Got: 0x%x", (unsigned char)base[i]); warnx("Base of sbrk region: %p", base); + success(TEST161_FAIL, SECRET, "/testbin/zero"); errx(1, "FAILED"); } } @@ -140,6 +145,6 @@ main(void) tprintf("zero: phase 2: checking sbrk()\n"); check_sbrk(); - tprintf("zero: passed\n"); + success(TEST161_SUCCESS, SECRET, "/testbin/zero"); return 0; }