From 716d33964bccb802e4d30326d5f5fc451c561bfe Mon Sep 17 00:00:00 2001 From: Geoffrey Challen Date: Sun, 31 Jan 2016 09:53:13 -0500 Subject: [PATCH] Secret testing with multiplier working. --- kern/include/kern/secret.h | 3 +- kern/include/test.h | 21 +++++++++--- kern/lib/kprintf.c | 39 ---------------------- kern/test/lib.c | 52 ++++++++++++++++++++++++++++++ kern/test/synchprobs.c | 16 ++++----- kern/test/synchtest.c | 66 ++++++++++++++++---------------------- 6 files changed, 105 insertions(+), 92 deletions(-) diff --git a/kern/include/kern/secret.h b/kern/include/kern/secret.h index aa2c972..06f4e3f 100644 --- a/kern/include/kern/secret.h +++ b/kern/include/kern/secret.h @@ -44,6 +44,7 @@ * allows normally compilation and operation. */ -#define KERNEL_SECRET "" +#define SECRET_TESTING +#define SECRET 1755184289 #endif /* _SECRET_H_ */ diff --git a/kern/include/test.h b/kern/include/test.h index f9041e8..314c6f3 100644 --- a/kern/include/test.h +++ b/kern/include/test.h @@ -32,6 +32,7 @@ /* Get __PF() for declaring printf-like functions. */ #include +#include #include "opt-synchprobs.h" #include "opt-automationtest.h" @@ -165,16 +166,26 @@ int ll1test(int, char **); int ll16test(int, char **); #endif +#define SUCCESS 0 +#define FAIL 1 + +void success(bool, uint32_t, const char *); + void random_yielder(uint32_t); void random_spinner(uint32_t); /* - * Testing variants of kprintf. tprintf is silent during automated testing. - * sprintf prefixes the kernel secret to kprintf messages during automated - * testing. nprintf is not silent during automated testing. + * kprintf variants that do not (or only) print during automated testing. */ -int tkprintf(const char *format, ...) __PF(1,2); -int nkprintf(const char *format, ...) __PF(1,2); +#ifdef SECRET_TESTING +#define kprintf_t(...) kprintf(__VA_ARGS__) +#define kprintf_n(...) silent(__VA_ARGS__) +#else +#define kprintf_t(...) silent(__VA_ARGS__) +#define kprintf_n(...) kprintf(__VA_ARGS__) +#endif + +static inline void silent(const char * fmt, ...) { (void)fmt; }; #endif /* _TEST_H_ */ diff --git a/kern/lib/kprintf.c b/kern/lib/kprintf.c index e7264ab..2b04bbc 100644 --- a/kern/lib/kprintf.c +++ b/kern/lib/kprintf.c @@ -142,45 +142,6 @@ kprintf(const char *fmt, ...) return chars; } -/* - * kprintf variant that is quiet during automated testing - */ -int -tkprintf(const char *fmt, ...) -{ - int chars; - va_list ap; - - if (strcmp(KERNEL_SECRET, "") != 0) { - return 0; - } - - va_start(ap, fmt); - chars = __kprintf(fmt, ap); - va_end(ap); - - return chars; -} -/* - * kprintf variant that is quiet during non-automated testing - */ -int -nkprintf(const char *fmt, ...) -{ - int chars; - va_list ap; - - if (strcmp(KERNEL_SECRET, "") == 0) { - return 0; - } - - va_start(ap, fmt); - chars = __kprintf(fmt, ap); - va_end(ap); - - return chars; -} - /* * panic() is for fatal errors. It prints the printf arguments it's * passed and then halts the system. diff --git a/kern/test/lib.c b/kern/test/lib.c index 1521487..5f6a5a5 100644 --- a/kern/test/lib.c +++ b/kern/test/lib.c @@ -1,6 +1,58 @@ +#include #include #include #include +#include + +/* + * Main success function for kernel tests. Prints a multiple of the secret if + * the secret is non-zero and the test succeeded. Otherwise prints a random + * number. + * + * Ideally we would multiply the secret (a large prime) by another large prime + * to ensure that factoring was hard, but that would require either primality + * testing (slow) or augmenting sys161 with a prime number generator. This is + * sufficient for now to prevent replay attacks. + */ + +#define MIN_MULTIPLIER 0x80000000 + +#ifndef SECRET_TESTING +void +success(bool status, uint32_t secret, const char * name) { + (void)secret; + if (status == SUCCESS) { + kprintf("%s: SUCCESS\n", name); + } else { + kprintf("%s: FAIL\n", name); + } + return; +} +#else +void +success(bool status, uint32_t secret, const char * name) { + uint32_t multiplier; + // Make sure we can get large random numbers + KASSERT(randmax() == 0xffffffff); + while (1) { + multiplier = random(); + // We can at least remove the obvious non-primes... + if (multiplier % 2 == 0) { + continue; + } + if (multiplier > MIN_MULTIPLIER) { + break; + } + } + uint64_t big_secret = (uint64_t) secret * (uint64_t) multiplier; + if (status == SUCCESS) { + kprintf("%s: SUCCESS (%llu)\n", name, big_secret); + } else { + kprintf("%s: FAIL (%llu)\n", name, big_secret); + } + return; +} +#endif /* * Helper functions used by testing and problem driver code diff --git a/kern/test/synchprobs.c b/kern/test/synchprobs.c index b6ac2bf..e8768ec 100644 --- a/kern/test/synchprobs.c +++ b/kern/test/synchprobs.c @@ -78,13 +78,13 @@ void male_start(void) { random_yielder(PROBLEMS_MAX_YIELDER); random_spinner(PROBLEMS_MAX_SPINNER); - tkprintf("%s starting\n", curthread->t_name); + kprintf_n("%s starting\n", curthread->t_name); } void male_end(void) { random_yielder(PROBLEMS_MAX_YIELDER); random_spinner(PROBLEMS_MAX_SPINNER); - tkprintf("%s ending\n", curthread->t_name); + kprintf_n("%s ending\n", curthread->t_name); } static @@ -111,13 +111,13 @@ void female_start(void) { random_yielder(PROBLEMS_MAX_YIELDER); random_spinner(PROBLEMS_MAX_SPINNER); - tkprintf("%s starting\n", curthread->t_name); + kprintf_n("%s starting\n", curthread->t_name); } void female_end(void) { random_yielder(PROBLEMS_MAX_YIELDER); random_spinner(PROBLEMS_MAX_SPINNER); - tkprintf("%s ending\n", curthread->t_name); + kprintf_n("%s ending\n", curthread->t_name); } static @@ -144,13 +144,13 @@ void matchmaker_start(void) { random_yielder(PROBLEMS_MAX_YIELDER); random_spinner(PROBLEMS_MAX_SPINNER); - tkprintf("%s starting\n", curthread->t_name); + kprintf_n("%s starting\n", curthread->t_name); } void matchmaker_end(void) { random_yielder(PROBLEMS_MAX_YIELDER); random_spinner(PROBLEMS_MAX_SPINNER); - tkprintf("%s ending\n", curthread->t_name); + kprintf_n("%s ending\n", curthread->t_name); } #define NMATING 10 @@ -268,14 +268,14 @@ void inQuadrant(int quadrant) { random_yielder(PROBLEMS_MAX_YIELDER); random_spinner(PROBLEMS_MAX_SPINNER); - tkprintf("%s in quadrant %d\n", curthread->t_name, quadrant); + kprintf_n("%s in quadrant %d\n", curthread->t_name, quadrant); } void leaveIntersection() { random_yielder(PROBLEMS_MAX_YIELDER); random_spinner(PROBLEMS_MAX_SPINNER); - tkprintf("%s left the intersection\n", curthread->t_name); + kprintf_n("%s left the intersection\n", curthread->t_name); } #define NCARS 64 diff --git a/kern/test/synchtest.c b/kern/test/synchtest.c index 2ca6a21..6290921 100644 --- a/kern/test/synchtest.c +++ b/kern/test/synchtest.c @@ -40,9 +40,6 @@ #include #include -#define SUCCESS 0 -#define FAIL 1 - #define NSEMLOOPS 63 #define NLOCKLOOPS 120 #define NCVLOOPS 5 @@ -94,15 +91,6 @@ inititems(void) spinlock_init(&status_lock); } -static -void -success(bool status, const char *msg) { - if (status == SUCCESS) { - kprintf("%s%s: SUCCESS\n", KERNEL_SECRET, msg); - } else { - kprintf("%s%s: FAIL\n", KERNEL_SECRET, msg); - } -} static void @@ -117,10 +105,10 @@ semtestthread(void *junk, unsigned long num) random_yielder(4); P(testsem); semtest_current = num; - tkprintf("Thread %2lu: ", num); + kprintf_n("Thread %2lu: ", num); for (i=0; i