about summary refs log tree commit diff
path: root/frida_mode/test/dynamic/testinstr.c
diff options
context:
space:
mode:
authorYour Name <you@example.com>2022-05-12 16:54:40 +0100
committerYour Name <you@example.com>2022-05-12 16:54:40 +0100
commit3dc72ffb6be7f9acb9788d5cfa8d99c557418621 (patch)
tree2cc0db1d117f2990e772ef0adddd70276753e9bd /frida_mode/test/dynamic/testinstr.c
parentca361e1b6a7228e1f854aa6884cf98f4e9a21486 (diff)
downloadafl++-3dc72ffb6be7f9acb9788d5cfa8d99c557418621.tar.gz
Added test for prefetch validation
Diffstat (limited to 'frida_mode/test/dynamic/testinstr.c')
-rw-r--r--frida_mode/test/dynamic/testinstr.c98
1 files changed, 98 insertions, 0 deletions
diff --git a/frida_mode/test/dynamic/testinstr.c b/frida_mode/test/dynamic/testinstr.c
new file mode 100644
index 00000000..ad26d060
--- /dev/null
+++ b/frida_mode/test/dynamic/testinstr.c
@@ -0,0 +1,98 @@
+/*
+   american fuzzy lop++ - a trivial program to test the build
+   --------------------------------------------------------
+   Originally written by Michal Zalewski
+   Copyright 2014 Google Inc. All rights reserved.
+   Copyright 2019-2022 AFLplusplus Project. All rights reserved.
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at:
+     http://www.apache.org/licenses/LICENSE-2.0
+ */
+#include <dlfcn.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+typedef void (*fntestinstrlib)(char *buf, int len);
+
+void testinstr(char *buf, int len) {
+  void *lib = dlopen("testinstrlib.so", RTLD_NOW);
+  if (lib == NULL) {
+    puts("Library not found");
+    abort();
+  }
+
+  fntestinstrlib fn = (fntestinstrlib)(dlsym(lib, "testinstrlib"));
+  if (fn == NULL) {
+    puts("Function not found");
+    abort();
+  }
+
+  fn(buf, len);
+}
+
+int main(int argc, char **argv) {
+  char * file;
+  int    fd = -1;
+  off_t  len;
+  char * buf = NULL;
+  size_t n_read;
+  int    result = -1;
+
+  if (argc != 2) { return 1; }
+
+  do {
+    file = argv[1];
+    printf("file: %s\n", file);
+
+    dprintf(STDERR_FILENO, "Running: %s\n", file);
+
+    fd = open(file, O_RDONLY);
+    if (fd < 0) {
+      perror("open");
+      break;
+    }
+
+    len = lseek(fd, 0, SEEK_END);
+    if (len < 0) {
+      perror("lseek (SEEK_END)");
+      break;
+    }
+
+    if (lseek(fd, 0, SEEK_SET) != 0) {
+      perror("lseek (SEEK_SET)");
+      break;
+    }
+
+    printf("len: %ld\n", len);
+
+    buf = malloc(len);
+    if (buf == NULL) {
+      perror("malloc");
+      break;
+    }
+
+    n_read = read(fd, buf, len);
+    if (n_read != len) {
+      perror("read");
+      break;
+    }
+
+    dprintf(STDERR_FILENO, "Running:    %s: (%zd bytes)\n", file, n_read);
+
+    testinstr(buf, len);
+    dprintf(STDERR_FILENO, "Done:    %s: (%zd bytes)\n", file, n_read);
+
+    result = 0;
+
+  } while (false);
+
+  if (buf != NULL) { free(buf); }
+
+  if (fd != -1) { close(fd); }
+
+  return result;
+}