ref: 2e3f01aecec2284af77579901e01883364210131
parent: fafcd2be9252bf274a25fd4601a0dcca3ee8a8b0
author: seh <seh@localhost>
date: Tue Nov 20 19:54:28 EST 2018
Make logging thread safe ;; use dolog() now instead of fprint(logfd, ...)
--- a/fuzz.h
+++ b/fuzz.h
@@ -11,7 +11,8 @@
#define NTYPES 4
// Global variables
-extern int logfd;
+extern int logfd;
+extern Lock loglck;
/*
For full list of syscalls:
@@ -157,5 +158,8 @@
// list.c → See list.h
// mutate.c → See mutate.h
+
+// main.c
+void dolog(char*, ...);
#endif
--- a/input.c
+++ b/input.c
@@ -28,7 +28,7 @@
break;
case sc_abort : // abort(void);
- fprint(logfd, "!! Someone called abort, don't do that.\n");
+ dolog("!! Someone called abort, don't do that.\n");
/* NOPE
// log the variables
log_call(sc);
@@ -1004,54 +1004,54 @@
void
log_call(caller *sc)
{
- fprint(logfd, "\nSystem Call: %s\n", sc->name);
- fprint(logfd, "\n\tRound #: %d\n", sc->round);
+ dolog("\nSystem Call: %s\n", sc->name);
+ dolog("\n\tRound #: %d\n", sc->round);
int x;
for (x = 0; x < (sc->inputs.size); x++) {
t_type* ele = lget(&(sc->inputs), x);
- fprint(logfd, "\n\t\t%s : ", callnames[ele->t]);
+ dolog("\n\t\t%s : ", callnames[ele->t]);
switch(ele->t) {
case t_int :
- fprint(logfd, "%d", *(int*) ele->var);
+ dolog("%d", *(int*) ele->var);
break;
case t_uint :
- fprint(logfd, "%d", *(unsigned int*) ele->var);
+ dolog("%d", *(unsigned int*) ele->var);
break;
case t_long :
- fprint(logfd, "%ld", *(long*) ele->var);
+ dolog("%ld", *(long*) ele->var);
break;
case t_ulong :
- fprint(logfd, "%lud", *(unsigned long*) ele->var);
+ dolog("%lud", *(unsigned long*) ele->var);
break;
case t_vlong :
- fprint(logfd, "%lld", *(long long*) ele->var);
+ dolog("%lld", *(long long*) ele->var);
break;
case t_longS :
- fprint(logfd, "%ld", *(long*) ele->var);
+ dolog("%ld", *(long*) ele->var);
break;
case t_DirS : //TODO : verify that this works; compiler warns against
- // fprint(logfd, "%s", (Dir*) ele->var);
+ // dolog("%s", (Dir*) ele->var);
break;
case t_charS :
// TODO -- segfaults
- // fprint(logfd, "%s", *(char**) ele->var);
+ // dolog("%s", *(char**) ele->var);
break;
case t_charSArr :
- //fprint(logfd, "%s", (char**) ele->var);
+ //dolog("%s", (char**) ele->var);
break;
case t_char :
- fprint(logfd, "%c", *(char*) ele->var);
+ dolog("%c", *(char*) ele->var);
break;
case t_uchar :
- fprint(logfd, "%c", *(unsigned char*) ele->var);
+ dolog("%c", *(unsigned char*) ele->var);
break;
default :
exits("Unknown input variable type!");
}
- fprint(logfd, "\n");
+ dolog("\n");
}
}
--- a/main.c
+++ b/main.c
@@ -1,7 +1,8 @@
#include "fuzz.h"
// Global variables are bad
-int logfd = -1; // fd of the log file, initialized in main
+int logfd = -1; // fd of the log file, initialized in main
+Lock loglck; // Lock for logger
// Commandline usage warning
void
@@ -11,6 +12,20 @@
exits("usage");
}
+// Perform locked logging operation -- wraps print
+void
+dolog(char *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+
+ lock(&loglck);
+ vfprint(logfd, fmt, args);
+ unlock(&loglck);
+
+ va_end(args);
+}
+
/* Prototypes */
void initsctable(void);
int name2index(char*);
@@ -42,7 +57,7 @@
#ifdef DEBUG
print("DEBUG index: %d\n", index);
#endif
- fprint(logfd, "Loading call: %s\n", *argv);
+ dolog("Loading call: %s\n", *argv);
ladd(&tofuzz, &syscalls[index]); // Might be dangerous, pls fix
}else{
print("Error: Invalid system call: %s\n", *argv);
@@ -59,10 +74,10 @@
// Operate for the desired number of rounds, -1 is infinite
for(i = 0; i < nrounds || nrounds < 0; i++){
int j;
- fprint(logfd, "== Begin round %d ==\n", i);
+ dolog("== Begin round %d ==\n", i);
for(j = 0; j < tofuzz.size; j++){
caller *fcall = (caller*) lget(&tofuzz, j);
- fprint(logfd, " Fuzzing: %s \n", fcall->name);
+ dolog(" Fuzzing: %s \n", fcall->name);
// Someone in here is calling exits inappropriately so forking.
int pid = rfork(RFFDG|RFREND|RFPROC|RFMEM);