shithub: fuzz

Download patch

ref: e2381aa3502c40930e44230c5d74a5beb1099197
parent: 1248c98dee886270a67a6c5dadfefb8cfa87e700
author: seh <seh@localhost>
date: Tue Nov 27 09:34:22 EST 2018

migrate to bio and sysfatal for prints

--- a/fuzz.h
+++ b/fuzz.h
@@ -3,10 +3,15 @@
 
 #include <u.h>
 #include <libc.h>
+#include <bio.h>
 #include "list.h"
 #include "mutate.h"
 
 
+// Macros
+// Sysfatal if not syscall not implemented
+#define noimpl() sysfatal("Error: Syscall not implemented!");
+
 // Max signed int value
 #define MAXINT 2147483647
 
@@ -16,7 +21,8 @@
 
 // Global variables
 // In main.c
-extern	int		logfd;
+extern	Biobuf*	logbp;
+extern	Biobuf*	hjbp;
 extern	Lock	loglck;
 extern	Lock	rnglck;
 
--- a/input.c
+++ b/input.c
@@ -201,8 +201,7 @@
 			break;
 		case sc_execl :			//	execl(char* : ...);
 			//TODO - not sure what to do with variable # of parameters
-			fprint(2, "Error: Syscall not implemented!\n");
-			exits("SYSCALL NOT IMPLEMENTED");
+			noimpl();
 			break;
 		case sc_fork :			//	fork(void);
 			// log the variables
@@ -355,8 +354,7 @@
 			break;
 		case sc_notify :		//	notify(void(*)(void* : char*));
 			//TODO - this sc takes a function pointer, we don't have infrastructure for that
-			fprint(2, "Error: Syscall not implemented!\n");
-			exits("SYSCALL NOT IMPLEMENTED");
+			noimpl();
 			break;
 		case sc_open :			//	open(char* : int);
 			// mutate the input
@@ -1012,12 +1010,10 @@
 			break;
 		case sc_werrstr :		//	werrstr(char* : ...);
 			//TODO - not sure what to do with variable # of parameters
-			fprint(2, "Error: Syscall not implemented!\n");
-			exits("SYSCALL NOT IMPLEMENTED");
+			noimpl();
 			break;
 		default:
-			fprint(2, "Error: Unknown system call encountered!\n");
-			exits("Unknown system call");
+			sysfatal("Error: Unknown system call encountered: %d", sc->c);
 		
 	}
 }
@@ -1026,7 +1022,7 @@
 void
 log_call(caller *sc)
 {
-	dolog("\nSystem Call: %s\n", sc->name);
+	// dolog("\nSystem Call: %s\n", sc->name);
 	// legacy since this is printed elsewhere
 	//dolog("\n\tRound #: %d\n", sc->round);
 	dolog("Arguments:\n");
@@ -1089,8 +1085,7 @@
 				dolog("skipping over…\n");
 				break;
 			default :
-				fprint(2, "Error: Encountered unknown input variable type!\n");
-				exits("Unknown input variable type!");
+				sysfatal("Error: Encountered unknown input variable type: %d", ele->t);
 		}
 		dolog("\n");
 	}
@@ -1100,10 +1095,8 @@
 void
 hjsync()
 {
-	// open file and write to sync disk
-	int hjfs = open("/srv/hjfs.cmd", OWRITE);
-	fprint(hjfs, "sync\n");
-	close(hjfs);
+	// open file and write to sync disk -- maybe make buffered i/o
+	Bprint(hjbp, "sync\n");
 }
 
 // Init callnames here, is extern in fuzz.h
--- a/main.c
+++ b/main.c
@@ -1,9 +1,12 @@
 #include "fuzz.h"
 
 // Global variables are bad
-int		logfd = -1; // fd of the log file, initialized in main
+Biobuf*	logbp;		// Bio buffer of the log file, initialized in main
+Biobuf*	stdout;		// ^^ for stdout ;; fd=1
+Biobuf*	hjbp;		// ^^ for #s/hjfs.cmd
 Lock	loglck;		// Lock for logger
 Lock	rnglck;		// Lock for rng
+char*	logname = "./fuzz.log";	// Name of log file
 
 // Commandline usage warning
 void
@@ -21,7 +24,7 @@
 	va_start(args, fmt);
 
 	lock(&loglck);
-	vfprint(logfd, fmt, args);
+	Bvprint(logbp, fmt, args);
 	unlock(&loglck);
 
 	va_end(args);
@@ -35,7 +38,7 @@
 	va_list args;
 	va_start(args, fmt);
 
-	vfprint(1, fmt, args);
+	Bvprint(stdout, fmt, args);
 
 	va_end(args);
 	#endif
@@ -66,6 +69,7 @@
 	int nrounds = -1, i;
 	List tofuzz = mklist() ; // List of syscall table ID's to fuzz
 	char* arg;
+	stdout = Bfdopen(1, OWRITE);
 
 	ARGBEGIN{
 		case 'n':
@@ -90,10 +94,20 @@
 	if(strcmp(*argv, "?") == 0){
 		int i;
 		for(i = 0; i < NCALLS; i++)
-			print("%s\n", callnames[i]);
+			Bprint(stdout, "%s\n", callnames[i]);
 		exits("Listing all known system calls");
 	}
 	
+	// Set up buffered output
+	int logfd = create(logname, OWRITE, 0777);
+	if(logfd < 0)
+		sysfatal("Error: Failed to create/open log file %s.", logname);
+
+	logbp = Bfdopen(logfd, OWRITE);
+	
+	int hjfd = open("#s/hjfs.cmd", OWRITE);
+	hjbp = Bfdopen(hjfd, OWRITE);
+	
 	// Acquire a list of calls specified by spaces (fuzz -n 1 read write seek)
 	for(;*argv;argv++){
 		int index;
@@ -103,17 +117,9 @@
 			
 			dolog("Loading call: %s\n", *argv);
 			ladd(&tofuzz, &syscalls[index]); // Might be dangerous, pls fix
-		}else{
-			fprint(2, "Error: Invalid system call: %s\n", *argv);
-			exits("Encountered invalid syscall");
-		}
+		}else
+			sysfatal("Error: Invalid system call: %s", *argv);
 	}
-	
-	logfd = create("./fuzz.log", OWRITE, 0777);
-	if(logfd < 0){
-		fprint(2, "Error: Failed to create/open log file.");
-		exits("log file create fail");
-	}
 
 	int fuzz_seed = truerand();
 	srand(fuzz_seed);
@@ -137,8 +143,17 @@
 		}	
 	}
 
-	fprint(2, "Fuzz ending…\n");
+	Bprint(stdout, "Fuzz ending…\n");
+
+	// Clean up
+	Bflush(logbp);
+	Bflush(stdout);
+	Bterm(stdout);
+	Bterm(logbp);
 	close(logfd);
+	Bflush(hjbp);
+	Bterm(hjbp);
+	close(hjfd);
 	exits(nil);
 }