aboutsummaryrefslogtreecommitdiff
path: root/frida_mode/src
diff options
context:
space:
mode:
Diffstat (limited to 'frida_mode/src')
-rw-r--r--frida_mode/src/instrument/instrument.c53
-rw-r--r--frida_mode/src/instrument/instrument_debug.c129
-rw-r--r--frida_mode/src/main.c7
-rw-r--r--frida_mode/src/output.c45
-rw-r--r--frida_mode/src/persistent/persistent.c34
-rw-r--r--frida_mode/src/persistent/persistent_arm32.c7
-rw-r--r--frida_mode/src/persistent/persistent_arm64.c7
-rw-r--r--frida_mode/src/persistent/persistent_x64.c19
-rw-r--r--frida_mode/src/persistent/persistent_x86.c15
-rw-r--r--frida_mode/src/ranges.c69
-rw-r--r--frida_mode/src/stats/stats.c208
-rw-r--r--frida_mode/src/stats/stats_arm.c36
-rw-r--r--frida_mode/src/stats/stats_arm64.c36
-rw-r--r--frida_mode/src/stats/stats_x64.c307
-rw-r--r--frida_mode/src/stats/stats_x86.c36
-rw-r--r--frida_mode/src/util.c13
16 files changed, 990 insertions, 31 deletions
diff --git a/frida_mode/src/instrument/instrument.c b/frida_mode/src/instrument/instrument.c
index 67eadc3f..cd1ac0be 100644
--- a/frida_mode/src/instrument/instrument.c
+++ b/frida_mode/src/instrument/instrument.c
@@ -13,6 +13,7 @@
#include "prefetch.h"
#include "ranges.h"
#include "stalker.h"
+#include "stats.h"
#include "util.h"
static gboolean tracing = false;
@@ -47,7 +48,7 @@ __attribute__((hot)) static void on_basic_block(GumCpuContext *context,
"x, previous_pc: 0x%016" G_GINT64_MODIFIER "x\n",
current_pc, previous_pc);
- IGNORED_RERURN(write(STDOUT_FILENO, buffer, len + 1));
+ IGNORED_RETURN(write(STDOUT_FILENO, buffer, len + 1));
}
@@ -79,17 +80,52 @@ static void instr_basic_block(GumStalkerIterator *iterator,
const cs_insn *instr;
gboolean begin = TRUE;
+ gboolean excluded;
+
while (gum_stalker_iterator_next(iterator, &instr)) {
if (instr->address == entry_start) { entry_prologue(iterator, output); }
if (instr->address == persistent_start) { persistent_prologue(output); }
-
- if (begin) {
+ if (instr->address == persistent_ret) { persistent_epilogue(output); }
+
+ /*
+ * Until we reach AFL_ENTRYPOINT (assumed to be main if not specified) or
+ * AFL_FRIDA_PERSISTENT_ADDR (if specified), we don't mark our ranges
+ * excluded as we wish to remain inside stalker at all times so that we can
+ * instrument our entry point and persistent loop (if present). This allows
+ * the user to exclude ranges which would be traversed between main and the
+ * AFL_ENTRYPOINT, but which they don't want included in their coverage
+ * information when fuzzing.
+ *
+ * Since we have no means to discard the instrumented copies of blocks
+ * (setting the trust threshold simply causes a new copy to be made on each
+ * execution), we instead ensure that we honour the additional
+ * instrumentation requested (e.g. coverage, asan and complog) when a block
+ * is compiled no matter where we are during initialization. We will end up
+ * re-using these blocks if the code under test calls a block which is also
+ * used during initialization.
+ *
+ * Coverage data generated during initialization isn't a problem since the
+ * map is zeroed each time the target is forked or each time the persistent
+ * loop is run.
+ *
+ * Lastly, we don't enable pre-fetching back to the parent until we reach
+ * our AFL_ENTRYPOINT, since it is not until then that we start the
+ * fork-server and thus start executing in the child.
+ */
+ excluded = range_is_excluded(GSIZE_TO_POINTER(instr->address));
+
+ stats_collect(instr, begin);
+
+ if (unlikely(begin)) {
+
+ instrument_debug_start(instr->address, output);
prefetch_write(GSIZE_TO_POINTER(instr->address));
- if (!range_is_excluded(GSIZE_TO_POINTER(instr->address))) {
- if (optimize) {
+ if (likely(!excluded)) {
+
+ if (likely(optimize)) {
instrument_coverage_optimize(instr, output);
@@ -106,7 +142,9 @@ static void instr_basic_block(GumStalkerIterator *iterator,
}
- if (!range_is_excluded(GSIZE_TO_POINTER(instr->address))) {
+ instrument_debug_instruction(instr->address, instr->size);
+
+ if (likely(!excluded)) {
asan_instrument(instr, iterator);
cmplog_instrument(instr, iterator);
@@ -117,6 +155,8 @@ static void instr_basic_block(GumStalkerIterator *iterator,
}
+ instrument_debug_end(output);
+
}
void instrument_init(void) {
@@ -144,6 +184,7 @@ void instrument_init(void) {
transformer =
gum_stalker_transformer_make_from_callback(instr_basic_block, NULL, NULL);
+ instrument_debug_init();
asan_init();
cmplog_init();
diff --git a/frida_mode/src/instrument/instrument_debug.c b/frida_mode/src/instrument/instrument_debug.c
new file mode 100644
index 00000000..be72ef89
--- /dev/null
+++ b/frida_mode/src/instrument/instrument_debug.c
@@ -0,0 +1,129 @@
+#include <fcntl.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "frida-gum.h"
+
+#include "debug.h"
+
+#include "util.h"
+
+static int debugging_fd = -1;
+static gpointer instrument_gen_start = NULL;
+
+static void instrument_debug(char *format, ...) {
+
+ va_list ap;
+ char buffer[4096] = {0};
+ int ret;
+ int len;
+
+ va_start(ap, format);
+ ret = vsnprintf(buffer, sizeof(buffer) - 1, format, ap);
+ va_end(ap);
+
+ if (ret < 0) { return; }
+
+ len = strnlen(buffer, sizeof(buffer));
+
+ IGNORED_RETURN(write(debugging_fd, buffer, len));
+
+}
+
+static void instrument_disasm(guint8 *code, guint size) {
+
+ csh capstone;
+ cs_err err;
+ cs_insn *insn;
+ size_t count, i;
+
+ err = cs_open(GUM_DEFAULT_CS_ARCH,
+ GUM_DEFAULT_CS_MODE | GUM_DEFAULT_CS_ENDIAN, &capstone);
+ g_assert(err == CS_ERR_OK);
+
+ count = cs_disasm(capstone, code, size, GPOINTER_TO_SIZE(code), 0, &insn);
+ g_assert(insn != NULL);
+
+ for (i = 0; i != count; i++) {
+
+ instrument_debug("\t0x%" G_GINT64_MODIFIER "x\t%s %s\n", insn[i].address,
+ insn[i].mnemonic, insn[i].op_str);
+
+ }
+
+ cs_free(insn, count);
+
+ cs_close(&capstone);
+
+}
+
+static gpointer instrument_cur(GumStalkerOutput *output) {
+
+#if defined(__i386__) || defined(__x86_64__)
+ return gum_x86_writer_cur(output->writer.x86);
+#elif defined(__aarch64__)
+ return gum_arm64_writer_cur(output->writer.arm64);
+#elif defined(__arm__)
+ return gum_arm_writer_cur(output->writer.arm);
+#else
+ #error "Unsupported architecture"
+#endif
+
+}
+
+void instrument_debug_init(void) {
+
+ char *filename = getenv("AFL_FRIDA_INST_DEBUG_FILE");
+ OKF("Instrumentation debugging - enabled [%c]", filename == NULL ? ' ' : 'X');
+
+ if (filename == NULL) { return; }
+
+ OKF("Instrumentation debugging - file [%s]", filename);
+
+ if (filename == NULL) { return; }
+
+ char *path = g_canonicalize_filename(filename, g_get_current_dir());
+
+ OKF("Instrumentation debugging - path [%s]", path);
+
+ debugging_fd = open(path, O_RDWR | O_CREAT | O_TRUNC,
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
+
+ if (debugging_fd < 0) { FATAL("Failed to open stats file '%s'", path); }
+
+ g_free(path);
+
+}
+
+void instrument_debug_start(uint64_t address, GumStalkerOutput *output) {
+
+ if (likely(debugging_fd < 0)) { return; }
+
+ instrument_gen_start = instrument_cur(output);
+
+ instrument_debug("\n\n***\n\nCreating block for 0x%" G_GINT64_MODIFIER "x:\n",
+ address);
+
+}
+
+void instrument_debug_instruction(uint64_t address, uint16_t size) {
+
+ if (likely(debugging_fd < 0)) { return; }
+ uint8_t *start = (uint8_t *)GSIZE_TO_POINTER(address);
+ instrument_disasm(start, size);
+
+}
+
+void instrument_debug_end(GumStalkerOutput *output) {
+
+ if (likely(debugging_fd < 0)) { return; }
+ gpointer instrument_gen_end = instrument_cur(output);
+ uint16_t size = GPOINTER_TO_SIZE(instrument_gen_end) -
+ GPOINTER_TO_SIZE(instrument_gen_start);
+
+ instrument_debug("\nGenerated block %p\n", instrument_gen_start);
+ instrument_disasm(instrument_gen_start, size);
+
+}
+
diff --git a/frida_mode/src/main.c b/frida_mode/src/main.c
index 21073cbe..1ab9993f 100644
--- a/frida_mode/src/main.c
+++ b/frida_mode/src/main.c
@@ -1,3 +1,4 @@
+#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
@@ -18,10 +19,12 @@
#include "instrument.h"
#include "interceptor.h"
#include "lib.h"
+#include "output.h"
#include "persistent.h"
#include "prefetch.h"
#include "ranges.h"
#include "stalker.h"
+#include "stats.h"
#include "util.h"
#ifdef __APPLE__
@@ -58,10 +61,10 @@ static void on_main_os(int argc, char **argv, char **envp) {
static void on_main_os(int argc, char **argv, char **envp) {
UNUSED_PARAMETER(argc);
-
/* Personality doesn't affect the current process, it only takes effect on
* evec */
int persona = personality(ADDR_NO_RANDOMIZE);
+ if (persona == -1) { WARNF("Failed to set ADDR_NO_RANDOMIZE: %d", errno); }
if ((persona & ADDR_NO_RANDOMIZE) == 0) { execvpe(argv[0], argv, envp); }
GumInterceptor *interceptor = gum_interceptor_obtain();
@@ -94,9 +97,11 @@ void afl_frida_start() {
lib_init();
entry_init();
instrument_init();
+ output_init();
persistent_init();
prefetch_init();
ranges_init();
+ stats_init();
void *fork_addr =
GSIZE_TO_POINTER(gum_module_find_export_by_name(NULL, "fork"));
diff --git a/frida_mode/src/output.c b/frida_mode/src/output.c
new file mode 100644
index 00000000..8a222b25
--- /dev/null
+++ b/frida_mode/src/output.c
@@ -0,0 +1,45 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "frida-gum.h"
+
+#include "debug.h"
+
+#include "output.h"
+
+static int output_fd = -1;
+
+static void output_redirect(int fd, char *variable) {
+
+ char *filename = getenv(variable);
+ char *path = NULL;
+
+ if (filename == NULL) { return; }
+
+ path = g_canonicalize_filename(filename, g_get_current_dir());
+
+ OKF("Redirect %d -> '%s'", fd, path);
+
+ output_fd = open(path, O_RDWR | O_CREAT | O_TRUNC,
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
+
+ g_free(path);
+
+ if (output_fd < 0) { FATAL("Failed to open fd(%d) error %d", fd, errno); }
+
+ if (dup2(output_fd, fd) < 0) {
+
+ FATAL("Failed to set fd(%d) error %d", fd, errno);
+
+ }
+
+}
+
+void output_init(void) {
+
+ output_redirect(STDOUT_FILENO, "AFL_FRIDA_OUTPUT_STDOUT");
+ output_redirect(STDERR_FILENO, "AFL_FRIDA_OUTPUT_STDERR");
+
+}
+
diff --git a/frida_mode/src/persistent/persistent.c b/frida_mode/src/persistent/persistent.c
index 918ff153..2ec5b9cc 100644
--- a/frida_mode/src/persistent/persistent.c
+++ b/frida_mode/src/persistent/persistent.c
@@ -12,6 +12,9 @@ int __afl_sharedmem_fuzzing = 0;
afl_persistent_hook_fn hook = NULL;
guint64 persistent_start = 0;
guint64 persistent_count = 0;
+guint64 persistent_ret = 0;
+guint64 persistent_ret_offset = 0;
+gboolean persistent_debug = FALSE;
void persistent_init(void) {
@@ -19,12 +22,36 @@ void persistent_init(void) {
persistent_start = util_read_address("AFL_FRIDA_PERSISTENT_ADDR");
persistent_count = util_read_num("AFL_FRIDA_PERSISTENT_CNT");
+ persistent_ret = util_read_address("AFL_FRIDA_PERSISTENT_RET");
+ persistent_ret_offset =
+ util_read_address("AFL_FRIDA_PERSISTENT_RETADDR_OFFSET");
+
+ if (getenv("AFL_FRIDA_PERSISTENT_DEBUG") != NULL) { persistent_debug = TRUE; }
+
+ if (persistent_count != 0 && persistent_start == 0) {
- if (persistent_count != 0 && persistent_start == 0)
FATAL(
"AFL_FRIDA_PERSISTENT_ADDR must be specified if "
"AFL_FRIDA_PERSISTENT_CNT is");
+ }
+
+ if (persistent_ret != 0 && persistent_start == 0) {
+
+ FATAL(
+ "AFL_FRIDA_PERSISTENT_ADDR must be specified if "
+ "AFL_FRIDA_PERSISTENT_RET is");
+
+ }
+
+ if (persistent_ret_offset != 0 && persistent_ret == 0) {
+
+ FATAL(
+ "AFL_FRIDA_PERSISTENT_RET must be specified if "
+ "AFL_FRIDA_PERSISTENT_RETADDR_OFFSET is");
+
+ }
+
if (persistent_start != 0 && persistent_count == 0) persistent_count = 1000;
if (persistent_count != 0 && persistent_count < 100)
@@ -39,6 +66,11 @@ void persistent_init(void) {
persistent_start == 0 ? ' ' : 'X', persistent_count);
OKF("Instrumentation - hook [%s]", hook_name);
+ OKF("Instrumentation - persistent ret [%c] (0x%016" G_GINT64_MODIFIER "X)",
+ persistent_ret == 0 ? ' ' : 'X', persistent_ret);
+ OKF("Instrumentation - persistent ret offset [%c] (%" G_GINT64_MODIFIER "d)",
+ persistent_ret_offset == 0 ? ' ' : 'X', persistent_ret_offset);
+
if (hook_name != NULL) {
void *hook_obj = dlopen(hook_name, RTLD_NOW);
diff --git a/frida_mode/src/persistent/persistent_arm32.c b/frida_mode/src/persistent/persistent_arm32.c
index bc021ff3..6a3c06fa 100644
--- a/frida_mode/src/persistent/persistent_arm32.c
+++ b/frida_mode/src/persistent/persistent_arm32.c
@@ -68,5 +68,12 @@ void persistent_prologue(GumStalkerOutput *output) {
}
+void persistent_epilogue(GumStalkerOutput *output) {
+
+ UNUSED_PARAMETER(output);
+ FATAL("Persistent mode not supported on this architecture");
+
+}
+
#endif
diff --git a/frida_mode/src/persistent/persistent_arm64.c b/frida_mode/src/persistent/persistent_arm64.c
index c198da69..1215d8da 100644
--- a/frida_mode/src/persistent/persistent_arm64.c
+++ b/frida_mode/src/persistent/persistent_arm64.c
@@ -111,5 +111,12 @@ void persistent_prologue(GumStalkerOutput *output) {
}
+void persistent_epilogue(GumStalkerOutput *output) {
+
+ UNUSED_PARAMETER(output);
+ FATAL("Persistent mode not supported on this architecture");
+
+}
+
#endif
diff --git a/frida_mode/src/persistent/persistent_x64.c b/frida_mode/src/persistent/persistent_x64.c
index aa772b7f..4c495d47 100644
--- a/frida_mode/src/persistent/persistent_x64.c
+++ b/frida_mode/src/persistent/persistent_x64.c
@@ -1,9 +1,11 @@
#include "frida-gum.h"
#include "config.h"
+#include "debug.h"
#include "instrument.h"
#include "persistent.h"
+#include "util.h"
#if defined(__x86_64__)
@@ -264,7 +266,6 @@ void persistent_prologue(GumStalkerOutput *output) {
GumX86Writer *cw = output->writer.x86;
gconstpointer loop = cw->code + 1;
- // gum_x86_writer_put_breakpoint(cw);
/* Stack must be 16-byte aligned per ABI */
instrument_persitent_save_regs(cw, &saved_regs);
@@ -288,7 +289,9 @@ void persistent_prologue(GumStalkerOutput *output) {
instrument_persitent_restore_regs(cw, &saved_regs);
gconstpointer original = cw->code + 1;
/* call original */
+
gum_x86_writer_put_call_near_label(cw, original);
+
/* jmp loop */
gum_x86_writer_put_jmp_near_label(cw, loop);
@@ -300,9 +303,23 @@ void persistent_prologue(GumStalkerOutput *output) {
/* original: */
gum_x86_writer_put_label(cw, original);
+ if (persistent_debug) { gum_x86_writer_put_breakpoint(cw); }
+
gum_x86_writer_flush(cw);
}
+void persistent_epilogue(GumStalkerOutput *output) {
+
+ GumX86Writer *cw = output->writer.x86;
+
+ if (persistent_debug) { gum_x86_writer_put_breakpoint(cw); }
+
+ gum_x86_writer_put_lea_reg_reg_offset(cw, GUM_REG_RSP, GUM_REG_RSP,
+ persistent_ret_offset);
+ gum_x86_writer_put_ret(cw);
+
+}
+
#endif
diff --git a/frida_mode/src/persistent/persistent_x86.c b/frida_mode/src/persistent/persistent_x86.c
index 20a3dc42..b30dfadf 100644
--- a/frida_mode/src/persistent/persistent_x86.c
+++ b/frida_mode/src/persistent/persistent_x86.c
@@ -244,9 +244,24 @@ void persistent_prologue(GumStalkerOutput *output) {
/* original: */
gum_x86_writer_put_label(cw, original);
+ if (persistent_debug) { gum_x86_writer_put_breakpoint(cw); }
+
gum_x86_writer_flush(cw);
}
+void persistent_epilogue(GumStalkerOutput *output) {
+
+ GumX86Writer *cw = output->writer.x86;
+
+ if (persistent_debug) { gum_x86_writer_put_breakpoint(cw); }
+
+ gum_x86_writer_put_lea_reg_reg_offset(cw, GUM_REG_ESP, GUM_REG_ESP,
+ persistent_ret_offset);
+
+ gum_x86_writer_put_ret(cw);
+
+}
+
#endif
diff --git a/frida_mode/src/ranges.c b/frida_mode/src/ranges.c
index e3f09f9e..ef25b371 100644
--- a/frida_mode/src/ranges.c
+++ b/frida_mode/src/ranges.c
@@ -480,15 +480,40 @@ static GArray *merge_ranges(GArray *a) {
}
+static gboolean exclude_ranges_callback(const GumRangeDetails *details,
+ gpointer user_data) {
+
+ UNUSED_PARAMETER(user_data);
+ gchar * name;
+ gboolean found;
+ GumStalker *stalker;
+ if (details->file == NULL) { return TRUE; }
+ name = g_path_get_basename(details->file->path);
+
+ found = (g_strcmp0(name, "afl-frida-trace.so") == 0);
+ g_free(name);
+ if (!found) { return TRUE; }
+
+ stalker = stalker_get();
+ gum_stalker_exclude(stalker, details->range);
+
+ return FALSE;
+
+}
+
+static void ranges_exclude_self(void) {
+
+ gum_process_enumerate_ranges(GUM_PAGE_EXECUTE, exclude_ranges_callback, NULL);
+
+}
+
void ranges_init(void) {
- GumMemoryRange ri;
- GArray * step1;
- GArray * step2;
- GArray * step3;
- GArray * step4;
- GumMemoryRange *r;
- GumStalker * stalker;
+ GumMemoryRange ri;
+ GArray * step1;
+ GArray * step2;
+ GArray * step3;
+ GArray * step4;
if (getenv("AFL_FRIDA_DEBUG_MAPS") != NULL) {
@@ -535,20 +560,16 @@ void ranges_init(void) {
ranges = merge_ranges(step4);
print_ranges("final", ranges);
- stalker = stalker_get();
-
- for (guint i = 0; i < ranges->len; i++) {
-
- r = &g_array_index(ranges, GumMemoryRange, i);
- gum_stalker_exclude(stalker, r);
-
- }
-
g_array_free(step4, TRUE);
g_array_free(step3, TRUE);
g_array_free(step2, TRUE);
g_array_free(step1, TRUE);
+ /* *NEVER* stalk the stalker, only bad things will ever come of this! */
+ ranges_exclude_self();
+
+ ranges_exclude();
+
}
gboolean range_is_excluded(gpointer address) {
@@ -572,3 +593,19 @@ gboolean range_is_excluded(gpointer address) {
}
+void ranges_exclude() {
+
+ GumMemoryRange *r;
+ GumStalker * stalker = stalker_get();
+
+ OKF("Excluding ranges");
+
+ for (guint i = 0; i < ranges->len; i++) {
+
+ r = &g_array_index(ranges, GumMemoryRange, i);
+ gum_stalker_exclude(stalker, r);
+
+ }
+
+}
+
diff --git a/frida_mode/src/stats/stats.c b/frida_mode/src/stats/stats.c
new file mode 100644
index 00000000..890a8d6b
--- /dev/null
+++ b/frida_mode/src/stats/stats.c
@@ -0,0 +1,208 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/shm.h>
+#include <sys/mman.h>
+
+#include "frida-gum.h"
+
+#include "config.h"
+#include "debug.h"
+#include "util.h"
+
+#include "stats.h"
+
+#define MICRO_TO_SEC 1000000
+
+stats_data_header_t *stats_data = NULL;
+
+static int stats_parent_pid = -1;
+static int stats_fd = -1;
+static gboolean stats_transitions = FALSE;
+static guint64 stats_interval = 0;
+
+void stats_init(void) {
+
+ stats_parent_pid = getpid();
+ char *filename = getenv("AFL_FRIDA_STATS_FILE");
+ stats_interval = util_read_num("AFL_FRIDA_STATS_INTERVAL");
+ if (getenv("AFL_FRIDA_STATS_TRANSITIONS") != NULL) {
+
+ stats_transitions = TRUE;
+
+ }
+
+ OKF("Stats - file [%s]", filename);
+ OKF("Stats - interval [%" G_GINT64_MODIFIER "u]", stats_interval);
+
+ if (stats_interval != 0 && filename == NULL) {
+
+ FATAL(
+ "AFL_FRIDA_STATS_FILE must be specified if "
+ "AFL_FRIDA_STATS_INTERVAL is");
+
+ }
+
+ if (stats_interval == 0) { stats_interval = 10; }
+
+ if (filename == NULL) { return; }
+
+ if (!stats_is_supported_arch()) {
+
+ FATAL("Stats is not supported on this architecture");
+
+ }
+
+ char *path = NULL;
+
+ if (filename == NULL) { return; }
+
+ if (stats_transitions) { gum_stalker_set_counters_enabled(TRUE); }
+
+ path = g_canonicalize_filename(filename, g_get_current_dir());
+
+ OKF("Stats - path [%s]", path);
+
+ stats_fd = open(path, O_RDWR | O_CREAT | O_TRUNC,
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
+
+ if (stats_fd < 0) { FATAL("Failed to open stats file '%s'", path); }
+
+ g_free(path);
+
+ size_t data_size = stats_data_size_arch();
+
+ int shm_id = shmget(IPC_PRIVATE, data_size, IPC_CREAT | IPC_EXCL | 0600);
+ if (shm_id < 0) { FATAL("shm_id < 0 - errno: %d\n", errno); }
+
+ stats_data = shmat(shm_id, NULL, 0);
+ g_assert(stats_data != MAP_FAILED);
+
+ /*
+ * Configure the shared memory region to be removed once the process dies.
+ */
+ if (shmctl(shm_id, IPC_RMID, NULL) < 0) {
+
+ FATAL("shmctl (IPC_RMID) < 0 - errno: %d\n", errno);
+
+ }
+
+ /* Clear it, not sure it's necessary, just seems like good practice */
+ memset(stats_data, '\0', data_size);
+
+}
+
+void stats_vprint(int fd, char *format, va_list ap) {
+
+ char buffer[4096] = {0};
+ int ret;
+ int len;
+
+ if(vsnprintf(buffer, sizeof(buffer) - 1, format, ap) < 0) { return; }
+
+ len = strnlen(buffer, sizeof(buffer));
+ IGNORED_RETURN(write(fd, buffer, len));
+
+}
+
+void stats_print_fd(int fd, char *format, ...) {
+
+ va_list ap;
+ va_start(ap, format);
+ stats_vprint(fd, format, ap);
+ va_end(ap);
+
+}
+
+void stats_print(char *format, ...) {
+
+ va_list ap;
+ va_start(ap, format);
+ stats_vprint(stats_fd, format, ap);
+ va_end(ap);
+
+}
+
+void stats_write(void) {
+
+ if (stats_parent_pid == getpid()) { return; }
+
+ GDateTime *date_time = g_date_time_new_now_local();
+ char *date_time_string = g_date_time_format(date_time, "%Y-%m-%e %H:%M:%S");
+
+ stats_print("stats\n");
+ stats_print("-----\n");
+
+ stats_print("Index: %" G_GINT64_MODIFIER "u\n",
+ stats_data->stats_idx++);
+ stats_print("Pid: %d\n", getpid());
+ stats_print("Time: %s\n", date_time_string);
+ stats_print("Blocks: %" G_GINT64_MODIFIER "u\n",
+ stats_data->num_blocks);
+ stats_print("Instructions: %" G_GINT64_MODIFIER "u\n",
+ stats_data->num_instructions);
+ stats_print("Avg Instructions / Block: %" G_GINT64_MODIFIER "u\n",
+ stats_data->num_instructions / stats_data->num_blocks);
+
+ stats_print("\n");
+
+ g_free(date_time_string);
+ g_date_time_unref(date_time);
+
+ stats_write_arch();
+
+ if (stats_transitions) {
+
+ GDateTime *date_time = g_date_time_new_now_local();
+ char *date_time_string = g_date_time_format(date_time, "%Y-%m-%e %H:%M:%S");
+
+ stats_print_fd(STDERR_FILENO, "stats\n");
+ stats_print_fd(STDERR_FILENO, "-----\n");
+ stats_print_fd(STDERR_FILENO, "Index: %" G_GINT64_MODIFIER "u\n",
+ stats_data->transitions_idx++);
+ stats_print_fd(STDERR_FILENO, "Pid: %d\n", getpid());
+ stats_print_fd(STDERR_FILENO, "Time: %s\n", date_time_string);
+
+ g_free(date_time_string);
+ g_date_time_unref(date_time);
+ gum_stalker_dump_counters();
+
+ }
+
+}
+
+static void stats_maybe_write(void) {
+
+ guint64 current_time;
+
+ if (stats_interval == 0) { return; }
+
+ current_time = g_get_monotonic_time();
+
+ if ((current_time - stats_data->stats_last_time) >
+ (stats_interval * MICRO_TO_SEC)) {
+
+ stats_write();
+ stats_data->stats_last_time = current_time;
+
+ }
+
+}
+
+void stats_collect(const cs_insn *instr, gboolean begin) {
+
+ UNUSED_PARAMETER(instr);
+ UNUSED_PARAMETER(begin);
+
+ if (stats_fd < 0) { return; }
+
+ if (begin) { stats_data->num_blocks++; }
+ stats_data->num_instructions++;
+
+ stats_collect_arch(instr);
+
+ stats_maybe_write();
+
+}
+
diff --git a/frida_mode/src/stats/stats_arm.c b/frida_mode/src/stats/stats_arm.c
new file mode 100644
index 00000000..7eea7f91
--- /dev/null
+++ b/frida_mode/src/stats/stats_arm.c
@@ -0,0 +1,36 @@
+#include "frida-gum.h"
+
+#include "debug.h"
+
+#include "stats.h"
+#include "util.h"
+
+#if defined(__arm__)
+
+gboolean stats_is_supported_arch(void) {
+
+ return FALSE;
+
+}
+
+size_t stats_data_size_arch(void) {
+
+ FATAL("Stats not supported on this architecture");
+
+}
+
+void stats_write_arch(void) {
+
+ FATAL("Stats not supported on this architecture");
+
+}
+
+void stats_collect_arch(const cs_insn *instr) {
+
+ UNUSED_PARAMETER(instr);
+ FATAL("Stats not supported on this architecture");
+
+}
+
+#endif
+
diff --git a/frida_mode/src/stats/stats_arm64.c b/frida_mode/src/stats/stats_arm64.c
new file mode 100644
index 00000000..592af87a
--- /dev/null
+++ b/frida_mode/src/stats/stats_arm64.c
@@ -0,0 +1,36 @@
+#include "frida-gum.h"
+
+#include "debug.h"
+
+#include "stats.h"
+#include "util.h"
+
+#if defined(__aarch64__)
+
+gboolean stats_is_supported_arch(void) {
+
+ return FALSE;
+
+}
+
+size_t stats_data_size_arch(void) {
+
+ FATAL("Stats not supported on this architecture");
+
+}
+
+void stats_write_arch(void) {
+
+ FATAL("Stats not supported on this architecture");
+
+}
+
+void stats_collect_arch(const cs_insn *instr) {
+
+ UNUSED_PARAMETER(instr);
+ FATAL("Stats not supported on this architecture");
+
+}
+
+#endif
+
diff --git a/frida_mode/src/stats/stats_x64.c b/frida_mode/src/stats/stats_x64.c
new file mode 100644
index 00000000..c3e8742a
--- /dev/null
+++ b/frida_mode/src/stats/stats_x64.c
@@ -0,0 +1,307 @@
+#include "frida-gum.h"
+
+#include "debug.h"
+
+#include "ranges.h"
+#include "stats.h"
+#include "util.h"
+
+#if defined(__x86_64__)
+
+typedef struct {
+
+ stats_data_header_t header;
+
+ guint64 num_call_imm;
+ guint64 num_call_imm_excluded;
+ guint64 num_call_reg;
+ guint64 num_call_mem;
+
+ guint64 num_jmp_imm;
+ guint64 num_jmp_reg;
+ guint64 num_jmp_mem;
+
+ guint64 num_jmp_cond_imm;
+ guint64 num_jmp_cond_reg;
+ guint64 num_jmp_cond_mem;
+
+ guint64 num_jmp_cond_jcxz;
+
+ guint64 num_ret;
+
+ guint64 num_rip_relative;
+
+} stats_data_arch_t;
+
+gboolean stats_is_supported_arch(void) {
+
+ return TRUE;
+
+}
+
+size_t stats_data_size_arch(void) {
+
+ return sizeof(stats_data_arch_t);
+
+}
+
+void stats_write_arch(void) {
+
+ stats_data_arch_t *stats_data_arch = (stats_data_arch_t *)stats_data;
+ guint64 num_instructions = stats_data_arch->header.num_instructions;
+
+ stats_print(
+ "Call Immediates: %" G_GINT64_MODIFIER
+ "u "
+ "(%3.2f%%)\n",
+ stats_data_arch->num_call_imm,
+ ((float)(stats_data_arch->num_call_imm * 100) / num_instructions));
+ stats_print("Call Immediates Excluded: %" G_GINT64_MODIFIER
+ "u "
+ "(%3.2f%%)\n",
+ stats_data_arch->num_call_imm_excluded,
+ ((float)(stats_data_arch->num_call_imm_excluded * 100) /
+ num_instructions));
+ stats_print(
+ "Call Register: %" G_GINT64_MODIFIER
+ "u "
+ "(%3.2f%%)\n",
+ stats_data_arch->num_call_reg,
+ ((float)(stats_data_arch->num_call_reg * 100) / num_instructions));
+ stats_print(
+ "Call Memory: %" G_GINT64_MODIFIER
+ "u "
+ "(%3.2f%%)\n",
+ stats_data_arch->num_call_mem,
+ ((float)(stats_data_arch->num_call_mem * 100) / num_instructions));
+
+ stats_print("\n");
+
+ stats_print("Jump Immediates: %" G_GINT64_MODIFIER
+ "u "
+ "(%3.2f%%)\n",
+ stats_data_arch->num_jmp_imm,
+ ((float)(stats_data_arch->num_jmp_imm * 100) / num_instructions));
+ stats_print("Jump Register: %" G_GINT64_MODIFIER
+ "u "
+ "(%3.2f%%)\n",
+ stats_data_arch->num_jmp_reg,
+ ((float)(stats_data_arch->num_jmp_reg * 100) / num_instructions));
+ stats_print("Jump Memory: %" G_GINT64_MODIFIER
+ "u "
+ "(%3.2f%%)\n",
+ stats_data_arch->num_jmp_mem,
+ ((float)(stats_data_arch->num_jmp_mem * 100) / num_instructions));
+
+ stats_print("\n");
+
+ stats_print(
+ "Conditional Jump Immediates: %" G_GINT64_MODIFIER
+ "u "
+ "(%3.2f%%)\n",
+ stats_data_arch->num_jmp_cond_imm,
+ ((float)(stats_data_arch->num_jmp_cond_imm * 100) / num_instructions));
+ stats_print(
+ "Conditional Jump CX Immediate: %" G_GINT64_MODIFIER
+ "u "
+ "(%3.2f%%)\n",
+ stats_data_arch->num_jmp_cond_jcxz,
+ ((float)(stats_data_arch->num_jmp_cond_jcxz * 100) / num_instructions));
+ stats_print(
+ "Conditional Jump Register: %" G_GINT64_MODIFIER
+ "u "
+ "(%3.2f%%)\n",
+ stats_data_arch->num_jmp_cond_reg,
+ ((float)(stats_data_arch->num_jmp_cond_reg * 100) / num_instructions));
+ stats_print(
+ "Conditional Jump Memory: %" G_GINT64_MODIFIER
+ "u "
+ "(%3.2f%%)\n",
+ stats_data_arch->num_jmp_cond_mem,
+ ((float)(stats_data_arch->num_jmp_cond_mem * 100) / num_instructions));
+
+ stats_print("\n");
+
+ stats_print("Returns: %" G_GINT64_MODIFIER
+ "u "
+ "(%3.2f%%)\n",
+ stats_data_arch->num_ret,
+ (stats_data_arch->num_ret * 100 / num_instructions));
+
+ stats_print("\n");
+
+ stats_print("Rip Relative: %" G_GINT64_MODIFIER
+ "u "
+ "(%3.2f%%)\n",
+ stats_data_arch->num_rip_relative,
+ (stats_data_arch->num_rip_relative * 100 / num_instructions));
+
+ stats_print("\n");
+ stats_print("\n");
+
+}
+
+static x86_op_type stats_get_operand_type(const cs_insn *instr) {
+
+ cs_x86 * x86 = &instr->detail->x86;
+ cs_x86_op *operand;
+
+ if (x86->op_count != 1) {
+
+ FATAL("Unexpected operand count (%d): %s %s\n", x86->op_count,
+ instr->mnemonic, instr->op_str);
+
+ }
+
+ operand = &x86->operands[0];
+
+ return operand->type;
+
+}
+
+static void stats_collect_call_imm_excluded_arch(const cs_insn *instr) {
+
+ stats_data_arch_t *stats_data_arch = (stats_data_arch_t *)stats_data;
+ cs_x86 * x86 = &instr->detail->x86;
+ cs_x86_op * operand = &x86->operands[0];
+
+ if (range_is_excluded((gpointer)operand->imm)) {
+
+ stats_data_arch->num_call_imm_excluded++;
+
+ }
+
+}
+
+static void stats_collect_call_arch(const cs_insn *instr) {
+
+ stats_data_arch_t *stats_data_arch = (stats_data_arch_t *)stats_data;
+ x86_op_type type = stats_get_operand_type(instr);
+ switch (type) {
+
+ case X86_OP_IMM:
+ stats_data_arch->num_call_imm++;
+ stats_collect_call_imm_excluded_arch(instr);
+ break;
+ case X86_OP_REG:
+ stats_data_arch->num_call_reg++;
+ break;
+ case X86_OP_MEM:
+ stats_data_arch->num_call_mem++;
+ break;
+ default:
+ FATAL("Invalid operand type: %s %s\n", instr->mnemonic, instr->op_str);
+
+ }
+
+}
+
+static void stats_collect_jump_arch(const cs_insn *instr) {
+
+ stats_data_arch_t *stats_data_arch = (stats_data_arch_t *)stats_data;
+ x86_op_type type = stats_get_operand_type(instr);
+ switch (type) {
+
+ case X86_OP_IMM:
+ stats_data_arch->num_jmp_imm++;
+ break;
+ case X86_OP_REG:
+ stats_data_arch->num_jmp_reg++;
+ break;
+ case X86_OP_MEM:
+ stats_data_arch->num_jmp_mem++;
+ break;
+ default:
+ FATAL("Invalid operand type: %s %s\n", instr->mnemonic, instr->op_str);
+
+ }
+
+}
+
+static void stats_collect_jump_cond_arch(const cs_insn *instr) {
+
+ stats_data_arch_t *stats_data_arch = (stats_data_arch_t *)stats_data;
+ x86_op_type type = stats_get_operand_type(instr);
+ switch (type) {
+
+ case X86_OP_IMM:
+ stats_data_arch->num_jmp_cond_imm++;
+ break;
+ case X86_OP_REG:
+ stats_data_arch->num_jmp_cond_reg++;
+ break;
+ case X86_OP_MEM:
+ stats_data_arch->num_jmp_cond_mem++;
+ break;
+ default:
+ FATAL("Invalid operand type: %s %s\n", instr->mnemonic, instr->op_str);
+
+ }
+
+}
+
+static void stats_collect_rip_relative_arch(const cs_insn *instr) {
+
+ stats_data_arch_t *stats_data_arch = (stats_data_arch_t *)stats_data;
+ cs_x86 * x86 = &instr->detail->x86;
+ guint mod;
+ guint rm;
+
+ if (x86->encoding.modrm_offset == 0) { return; }
+
+ mod = (x86->modrm & 0xc0) >> 6;
+ if (mod != 0) { return; }
+
+ rm = (x86->modrm & 0x07) >> 0;
+ if (rm != 5) { return; }
+
+ stats_data_arch->num_rip_relative++;
+
+}
+
+void stats_collect_arch(const cs_insn *instr) {
+
+ stats_data_arch_t *stats_data_arch = (stats_data_arch_t *)stats_data;
+ switch (instr->id) {
+
+ case X86_INS_CALL:
+ stats_collect_call_arch(instr);
+ break;
+ case X86_INS_JMP:
+ stats_collect_jump_arch(instr);
+ break;
+ case X86_INS_JA:
+ case X86_INS_JAE:
+ case X86_INS_JB:
+ case X86_INS_JBE:
+ case X86_INS_JE:
+ case X86_INS_JG:
+ case X86_INS_JGE:
+ case X86_INS_JL:
+ case X86_INS_JLE:
+ case X86_INS_JNE:
+ case X86_INS_JNO:
+ case X86_INS_JNP:
+ case X86_INS_JNS:
+ case X86_INS_JO:
+ case X86_INS_JP:
+ case X86_INS_JS:
+ stats_collect_jump_cond_arch(instr);
+ break;
+ case X86_INS_JECXZ:
+ case X86_INS_JRCXZ:
+ stats_data_arch->num_jmp_cond_jcxz++;
+ break;
+ case X86_INS_RET:
+ stats_data_arch->num_ret++;
+ break;
+ default:
+ stats_collect_rip_relative_arch(instr);
+ break;
+
+ }
+
+}
+
+#endif
+
diff --git a/frida_mode/src/stats/stats_x86.c b/frida_mode/src/stats/stats_x86.c
new file mode 100644
index 00000000..1906e809
--- /dev/null
+++ b/frida_mode/src/stats/stats_x86.c
@@ -0,0 +1,36 @@
+#include "frida-gum.h"
+
+#include "debug.h"
+
+#include "stats.h"
+#include "util.h"
+
+#if defined(__i386__)
+
+gboolean stats_is_supported_arch(void) {
+
+ return FALSE;
+
+}
+
+size_t stats_data_size_arch(void) {
+
+ FATAL("Stats not supported on this architecture");
+
+}
+
+void stats_write_arch(void) {
+
+ FATAL("Stats not supported on this architecture");
+
+}
+
+void stats_collect_arch(const cs_insn *instr) {
+
+ UNUSED_PARAMETER(instr);
+ FATAL("Stats not supported on this architecture");
+
+}
+
+#endif
+
diff --git a/frida_mode/src/util.c b/frida_mode/src/util.c
index 86b94970..09e8a58b 100644
--- a/frida_mode/src/util.c
+++ b/frida_mode/src/util.c
@@ -10,7 +10,7 @@ guint64 util_read_address(char *key) {
if (!g_str_has_prefix(value_str, "0x")) {
- FATAL("Invalid address should have 0x prefix: %s\n", value_str);
+ FATAL("Invalid address should have 0x prefix: %s=%s\n", key, value_str);
}
@@ -20,8 +20,8 @@ guint64 util_read_address(char *key) {
if (!g_ascii_isxdigit(*c)) {
- FATAL("Invalid address not formed of hex digits: %s ('%c')\n", value_str,
- *c);
+ FATAL("Invalid address not formed of hex digits: %s=%s ('%c')\n", key,
+ value_str, *c);
}
@@ -30,7 +30,7 @@ guint64 util_read_address(char *key) {
guint64 value = g_ascii_strtoull(value_str2, NULL, 16);
if (value == 0) {
- FATAL("Invalid address failed hex conversion: %s\n", value_str2);
+ FATAL("Invalid address failed hex conversion: %s=%s\n", key, value_str2);
}
@@ -48,7 +48,8 @@ guint64 util_read_num(char *key) {
if (!g_ascii_isdigit(*c)) {
- FATAL("Invalid address not formed of decimal digits: %s\n", value_str);
+ FATAL("Invalid address not formed of decimal digits: %s=%s\n", key,
+ value_str);
}
@@ -57,7 +58,7 @@ guint64 util_read_num(char *key) {
guint64 value = g_ascii_strtoull(value_str, NULL, 10);
if (value == 0) {
- FATAL("Invalid address failed numeric conversion: %s\n", value_str);
+ FATAL("Invalid address failed numeric conversion: %s=%s\n", key, value_str);
}