aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorYifan Lu2016-11-23 14:34:33 -0600
committerYifan Lu2016-11-23 14:34:33 -0600
commitbd5ebb7a0a4e102731de72832f3e12e9f54d541a (patch)
treebef4f637a270d3f7d2551ac74a89e6b4bad8adec /test
parentFixed proper encoding of PUSH (STMDB) as ARM manual was wrong... (diff)
parentavoid UB (diff)
downloadsubstitute-bd5ebb7a0a4e102731de72832f3e12e9f54d541a.tar.gz
Merge branch 'master' of https://github.com/comex/substitute
Diffstat (limited to 'test')
-rw-r--r--test/lol.c100
-rw-r--r--test/test-transform-dis.c2
-rw-r--r--test/transform-dis-cases-arm.S15
-rw-r--r--test/transform-dis-cases-arm64.S9
-rw-r--r--test/transform-dis-cases-i386.S10
-rw-r--r--test/transform-dis-cases.h4
6 files changed, 129 insertions, 11 deletions
diff --git a/test/lol.c b/test/lol.c
new file mode 100644
index 0000000..846cb5d
--- /dev/null
+++ b/test/lol.c
@@ -0,0 +1,100 @@
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdio.h>
+#define IF_BOTHER_WITH_MODIFY(...) __VA_ARGS__
+#include "dis.h"
+
+typedef struct tc {
+ struct dis_ctx_base base;
+ struct arch_dis_ctx arch;
+} *tdis_ctx;
+#define P(x) P_##x
+#define DIS_MAY_MODIFY 0
+
+static enum {
+ NOPPY,
+ JUMPY,
+ BAD
+} type;
+
+NOINLINE UNUSED
+static void P_data(UNUSED struct tc *ctx, unsigned o0, unsigned o1, unsigned o2,
+ unsigned o3, unsigned out_mask) {
+ unsigned ops[] = {o0, o1, o2, o3};
+ type = NOPPY;
+ for (int i = 0; i < 4; i++) {
+ if (ops[i] != null_op && (out_mask & (1 << i))) {
+ if (ops[i] == 15) {
+ type = JUMPY;
+ break;
+ } else if (ops[i] != 12 && ops[i] != 9) {
+ type = BAD;
+ }
+ }
+ }
+}
+NOINLINE UNUSED
+static void P_pcrel(UNUSED struct tc *ctx, uint32_t dpc,
+ UNUSED struct arch_pcrel_info info) {
+ return P_data(ctx, info.reg, null_op, null_op, null_op, 1);
+}
+NOINLINE UNUSED
+static void P_thumb_it(UNUSED struct tc *ctx) {
+ type = NOPPY;
+}
+
+NOINLINE UNUSED
+static void P_ret(UNUSED struct tc *ctx) {
+ type = JUMPY;
+}
+
+NOINLINE UNUSED
+static void P_indirect_call(UNUSED struct tc *ctx) {
+ type = JUMPY;
+}
+
+NOINLINE UNUSED
+static void P_branch(UNUSED struct tc *ctx, uint64_t dpc, int cc) {
+ type = BAD;
+}
+
+NOINLINE UNUSED
+static void P_unidentified(UNUSED struct tc *ctx) {
+ type = BAD;
+}
+
+NOINLINE UNUSED
+static void P_bad(UNUSED struct tc *ctx) {
+ type = JUMPY;
+}
+
+#include "arm/dis-main.inc.h"
+
+int main(UNUSED int argc, char **argv) {
+ struct tc ctx;
+ ctx.base.pc = 0xdead0000;
+ memset(ctx.base.newop, 0, sizeof(ctx.base.newop));
+ ctx.base.modify = false;
+ for (uint32_t hi = 0; hi < (1 << 12); hi++) {
+ for (uint32_t lo = 0; lo < (1 << 13); lo++) {
+ uint32_t op = (0b1111 << 28) | (hi << 16) | (0b111 << 13) | lo;
+
+ if ((op & 0x0f100010) == 0x0e100010)
+ continue;
+
+ ctx.base.ptr = &op;
+ ctx.arch.pc_low_bit = false;
+ type = BAD;
+ P(dis)(&ctx);
+ if (type != JUMPY)
+ continue;
+ ctx.arch.pc_low_bit = true;
+ type = BAD;
+ P(dis)(&ctx);
+ if (type != NOPPY)
+ continue;
+ printf("%x\n", op);
+ }
+ }
+
+}
diff --git a/test/test-transform-dis.c b/test/test-transform-dis.c
index 98c98e3..d147a49 100644
--- a/test/test-transform-dis.c
+++ b/test/test-transform-dis.c
@@ -127,7 +127,7 @@ static void do_auto(uint8_t *in, size_t in_size, struct arch_dis_ctx arch) {
pc_trampoline,
&arch,
offsets,
- TRANSFORM_DIS_BAN_CALLS);
+ 0);//TRANSFORM_DIS_BAN_CALLS);
if (ret) {
if (expect_err) {
printf("OK\n");
diff --git a/test/transform-dis-cases-arm.S b/test/transform-dis-cases-arm.S
new file mode 100644
index 0000000..a9e01b1
--- /dev/null
+++ b/test/transform-dis-cases-arm.S
@@ -0,0 +1,15 @@
+#include "transform-dis-cases.h"
+#ifdef THUMB
+.thumb
+#endif
+
+
+#ifndef THUMB
+GIVEN blne 0f; nop; nop; 0:
+EXPECT beq 1f; movw lr, #0x000c; movt lr, #0xdead; blx lr; 1: nop; nop
+GIVEN bl 0f; 0:
+EXPECT movw lr, #0x0004; movt lr, #0xdead; blx lr
+#else
+GIVEN bl 0f; 0:
+EXPECT movw lr, #0x0005; movt lr, #0xdead; blx lr
+#endif
diff --git a/test/transform-dis-cases-arm64.S b/test/transform-dis-cases-arm64.S
index cc3bd4c..99a353b 100644
--- a/test/transform-dis-cases-arm64.S
+++ b/test/transform-dis-cases-arm64.S
@@ -1,6 +1,4 @@
-#define GIVEN .ascii "GIVEN";
-#define EXPECT .ascii "EXPECT";
-#define EXPECT_ERR .ascii "EXPECT_ERR";
+#include "transform-dis-cases.h"
/* yay clang, no semicolons allowed */
@@ -12,7 +10,10 @@ EXPECT
GIVEN
blr x5
nop
-EXPECT_ERR
+//EXPECT_ERR (with ban_calls)
+EXPECT
+ blr x5
+
GIVEN
cbnz x8, .+0x100
diff --git a/test/transform-dis-cases-i386.S b/test/transform-dis-cases-i386.S
index c02a044..a39ee02 100644
--- a/test/transform-dis-cases-i386.S
+++ b/test/transform-dis-cases-i386.S
@@ -1,6 +1,4 @@
-#define GIVEN .ascii "GIVEN";
-#define EXPECT .ascii "EXPECT";
-#define EXPECT_ERR .ascii "EXPECT_ERR";
+#include "transform-dis-cases.h"
GIVEN call 0f; 0: pop %edx
/* XXX the extra push isn't necessary in 32-bit mode */
@@ -10,9 +8,9 @@ GIVEN jmp 0f; 0: nop
EXPECT_ERR
GIVEN jne .+0x1000
-/* we expect to generate an unnecessarily long jump, so hardcode it the 0x10000
- * is because we pretend our trampoline is 0x10000 bytes before the original
- * function */
+/* we expect to generate an unnecessarily long jump, so hardcode it
+ * the 0x10000 is because we pretend our trampoline is 0x10000 bytes before the
+ * original function */
EXPECT 2: .byte 0x0f, 0x85; .long 2; jmp 1f; 0: jmp .+0x10000+0x1000-8; 1:
GIVEN loopne .+0x80
diff --git a/test/transform-dis-cases.h b/test/transform-dis-cases.h
new file mode 100644
index 0000000..94f39bd
--- /dev/null
+++ b/test/transform-dis-cases.h
@@ -0,0 +1,4 @@
+#pragma once
+#define GIVEN .ascii "GIVEN";
+#define EXPECT .ascii "EXPECT";
+#define EXPECT_ERR .ascii "EXPECT_ERR";