summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenzo Colitti <lorenzo@google.com>2016-05-24 04:59:35 +0000
committerandroid-build-merger <android-build-merger@google.com>2016-05-24 04:59:35 +0000
commitae7070075ebcbc8e262bc8897616f4b671e38d46 (patch)
tree96a68d32bbbee71d276488470ba3d6e5872e1b92
parent63269f85d445700a637be6297e7083259189a460 (diff)
parent1017073e88c658cb7b88c8272b396d7998d61b13 (diff)
downloadapf-ae7070075ebcbc8e262bc8897616f4b671e38d46.tar.gz
Make the APF disassembler print pass and drop labels in jumps.
am: 1017073e88 * commit '1017073e88c658cb7b88c8272b396d7998d61b13': Make the APF disassembler print pass and drop labels in jumps. Change-Id: Ic60b78bea98891f4d36a95f43a467e5845125d6b
-rw-r--r--Android.mk20
-rw-r--r--apf_disassembler.c39
2 files changed, 36 insertions, 23 deletions
diff --git a/Android.mk b/Android.mk
index 39576e6..c32bb21 100644
--- a/Android.mk
+++ b/Android.mk
@@ -1,17 +1,29 @@
# Copyright 2016 The Android Open Source Project
LOCAL_PATH:= $(call my-dir)
+
+APF_CFLAGS := -DAPF_FRAME_HEADER_SIZE=14 \
+ -Wall \
+ -Werror
+
include $(CLEAR_VARS)
LOCAL_INCLUDES += $(LOCAL_PATH)
-LOCAL_CFLAGS += \
- -DAPF_FRAME_HEADER_SIZE=14 \
- -Wall \
- -Werror
+LOCAL_CFLAGS += $(APF_CFLAGS)
LOCAL_SRC_FILES += apf_interpreter.c
LOCAL_MODULE:= libapf
include $(BUILD_STATIC_LIBRARY)
+
+
+include $(CLEAR_VARS)
+
+LOCAL_CFLAGS += $(APF_CFLAGS)
+LOCAL_SRC_FILES += apf_disassembler.c
+LOCAL_MODULE := apf_disassembler
+LOCAL_MODULE_TAGS := debug
+
+include $(BUILD_HOST_EXECUTABLE)
diff --git a/apf_disassembler.c b/apf_disassembler.c
index 7f47b6d..8fa200a 100644
--- a/apf_disassembler.c
+++ b/apf_disassembler.c
@@ -52,15 +52,24 @@ static const char* opcode_names [] = {
[JNEBS_OPCODE] = "jnebs",
};
+static void print_jump_target(uint32_t target, uint32_t program_len) {
+ if (target == program_len) {
+ printf("pass");
+ } else if (target == program_len + 1) {
+ printf("drop");
+ } else {
+ printf("%u", target);
+ }
+}
+
// Disassembles an APF program. A hex dump of the program is supplied on stdin.
//
// NOTE: This is a simple debugging tool not meant for shipping or production use. It is by no
// means hardened against malicious input and contains known vulnerabilities.
//
// Example usage:
-// cc apf_disassemlber.c
-// adb shell dumpsys wifi ipmanager | sed '/Last program:/,+1!d;/Last program:/d;s/[ ]*//' | ./a.out
-int main(int argc, char* argv[]) {
+// adb shell dumpsys wifi ipmanager | sed '/Last program:/,+1!d;/Last program:/d;s/[ ]*//' | out/host/linux-x86/bin/apf_disassembler
+int main(void) {
uint32_t program_len = 0;
uint8_t program[10000];
@@ -70,18 +79,9 @@ int main(int argc, char* argv[]) {
program[program_len++] = byte;
}
- // Program counter.
- uint32_t pc = 0;
- while (pc < program_len + 2) {
+ for (uint32_t pc = 0; pc < program_len;) {
printf("%8u: ", pc);
const uint8_t bytecode = program[pc++];
- if (pc == (program_len + 1)) {
- printf("pass\n");
- continue;
- } else if (pc == (program_len + 2)) {
- printf("drop\n");
- continue;
- }
const uint32_t opcode = EXTRACT_OPCODE(bytecode);
#define PRINT_OPCODE() print_opcode(opcode_names[opcode])
const uint32_t reg_num = EXTRACT_REGISTER(bytecode);
@@ -113,7 +113,7 @@ int main(int argc, char* argv[]) {
break;
case JMP_OPCODE:
PRINT_OPCODE();
- printf("%u", pc + imm);
+ print_jump_target(pc + imm, program_len);
break;
case JEQ_OPCODE:
case JNE_OPCODE:
@@ -126,22 +126,23 @@ int main(int argc, char* argv[]) {
// Load second immediate field.
uint32_t cmp_imm = 0;
if (reg_num == 1) {
- printf("r1");
+ printf("r1, ");
} else if (len_field == 0) {
- printf("0");
+ printf("0, ");
} else {
uint32_t cmp_imm_len = 1 << (len_field - 1);
uint32_t i;
for (i = 0; i < cmp_imm_len; i++)
cmp_imm = (cmp_imm << 8) | program[pc++];
- printf("0x%x", cmp_imm);
+ printf("0x%x, ", cmp_imm);
}
if (opcode == JNEBS_OPCODE) {
- printf(", %u, ", pc + imm + cmp_imm);
+ print_jump_target(pc + imm + cmp_imm, program_len);
+ printf(", ");
while (cmp_imm--)
printf("%02x", program[pc++]);
} else {
- printf(", %u", pc + imm);
+ print_jump_target(pc + imm, program_len);
}
break;
}