#include "afl-0.68b/config.h"
//#define USE_FIFO_HACK
-
+#define ENABLE_LOG
typedef unsigned char u8;
typedef unsigned int u32;
static native_word afl_temp = 0;
static native_word afl_fork_pid = 0;
-static FILE *logfile;
+static int ctl_fd;
+static int st_fd;
+
+static const module_data_t *moduleinfos[100];
+static int modulecount = 0;
+#ifdef ENABLE_LOG
+static FILE *logfile;
#define LOG(s) do { fprintf(logfile, s); fflush(logfile); } while(0)
#define LOG1(s, a1) do { fprintf(logfile, s, a1); fflush(logfile); } while(0)
+#else
+#define LOG(s) do {} while(0)
+#define LOG1(s, a1) do {} while(0)
+#endif
-int ctl_fd;
-int st_fd;
static void
event_exit(void)
{
LOG("event_exit()\n");
+
+#ifdef ENABLE_LOG
fclose(logfile);
+#endif
//drx_exit();
//drwrap_exit();
static void afl_die()
{
LOG("afl_die()\n");
+#ifdef ENABLE_LOG
fclose(logfile);
+#endif
exit(0);
}
static void
afl_forkserver()
{
- LOG("writing hello message to st pipe\n");
+ //LOG("writing hello message to st pipe\n");
int n = write(st_fd, &afl_temp, 4);
- LOG1("write returned %d\n", n);
+ //LOG1("write returned %d\n", n);
if (n != 4) {
close(ctl_fd);
close(st_fd);
- LOG("returning\n");
+ //LOG("returning\n");
return;
}
while (1)
{
- LOG("waiting for parent reading from ctl pipe\n");
+ //LOG("waiting for parent reading from ctl pipe\n");
n = read(ctl_fd, &afl_temp, 4);
- LOG1("read returned %d\n", n);
+ //LOG1("read returned %d\n", n);
if (n != 4)
afl_die();
- LOG("forking\n");
+ //LOG("forking\n");
afl_fork_pid = fork();
if (afl_fork_pid < 0)
afl_die();
if (afl_fork_pid != 0) // parent
{
- LOG1("parent: writing PID %d to st pipe\n", (int)afl_fork_pid);
+ //LOG1("parent: writing PID %d to st pipe\n", (int)afl_fork_pid);
// write PID
n = write(st_fd, &afl_fork_pid, 4);
- LOG1("write returned %d\n", n);
+ //LOG1("write returned %d\n", n);
// wait for PID
- LOG("parent: calling waitpid\n");
+ //LOG("parent: calling waitpid\n");
int err = waitpid(afl_fork_pid, (int*)&afl_temp, WUNTRACED);
if (err <= 0)
afl_die();
- LOG("parent: writing wait status after waitpid\n");
+ //LOG("parent: writing wait status after waitpid\n");
// write wait status
write(st_fd, &afl_temp, 4);
}
else // child
{
- LOG("child: closing descriptors");
+ //LOG("child: closing descriptors\n");
close(ctl_fd);
close(st_fd);
break;
event_basic_block(void *drcontext, void *tag, instrlist_t *bb,
bool for_trace, bool translating)
{
+ app_pc pc = instr_get_app_pc(instrlist_first(bb));
+
+ // skip instrumentation for basic blocks in other modules
+ for (int i=1; i<modulecount; ++i)
+ {
+ if (dr_module_contains_addr(moduleinfos[i], pc))
+ return DR_EMIT_DEFAULT;
+ }
+
instr_t *instr = instrlist_first(bb);
native_word random_id = random() % MAP_SIZE;
dr_insert_clean_call(drcontext, bb, instr, afl_maybe_log, false, 1, OPND_CREATE_INTPTR(random_id));
return DR_EMIT_DEFAULT;
}
+void event_module_load(void *drcontext, const module_data_t *info, bool loaded)
+{
+ LOG1("event_module_load: %s\n", info->full_path);
+ moduleinfos[modulecount++] = dr_copy_module_data(info);
+}
+
DR_EXPORT void
dr_init(client_id_t id)
dr_register_exit_event(event_exit);
dr_register_bb_event(event_basic_block);
+ dr_register_module_load_event(event_module_load);
-//#ifdef UNIX
-// dr_register_fork_init_event(event_fork);
-//#endif
-// drmgr_register_module_load_event(event_module_load);
-// drmgr_register_module_unload_event(event_module_unload);
+
+#ifdef ENABLE_LOG
logfile = fopen("/tmp/afl.txt", "w");
+#endif
LOG("log file created in dr_init()\n");