-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathdisassembler.h
119 lines (101 loc) · 3.42 KB
/
disassembler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
Copyright (C) 2017 Sergej Schumilo
This file is part of QEMU-PT (kAFL).
QEMU-PT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QEMU-PT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QEMU-PT. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DISASSEMBLER_H
#define DISASSEMBLER_H
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/time.h>
#include <inttypes.h>
#include <capstone/capstone.h>
#include <capstone/x86.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <map>
#include <string>
#include <assert.h>
#include "tnt_cache.h"
typedef struct{
uint16_t opcode;
uint8_t modrm;
uint8_t opcode_prefix;
} cofi_ins;
typedef enum cofi_types{
COFI_TYPE_CONDITIONAL_BRANCH,
COFI_TYPE_UNCONDITIONAL_DIRECT_BRANCH,
COFI_TYPE_INDIRECT_BRANCH,
COFI_TYPE_NEAR_RET,
COFI_TYPE_FAR_TRANSFERS,
NO_COFI_TYPE
} cofi_type;
//#define DEBUG_COFI_INST
typedef struct _cofi_inst_t {
cofi_type type;
uint64_t bb_start_addr;
uint64_t inst_addr;
uint64_t target_addr;
struct _cofi_inst_t* next_cofi;
#ifdef DEBUG_COFI_INST
std::string dis_inst;
#endif
} cofi_inst_t;
class i_cofi_map {
protected:
uint64_t base_address;
uint32_t code_size;
uint32_t decoded_size = 0;
public:
i_cofi_map(uint64_t base_address, uint32_t code_size) : base_address(base_address), code_size(code_size) {}
void set_decode_info(uint64_t decoded_addr, uint64_t decoded_size);
double complete_percentage() { return (double) decoded_size * 100 / code_size; }
};
class std_cofi_map : public i_cofi_map {
std::map<uint64_t, cofi_inst_t*> map_data;
public:
std_cofi_map() : i_cofi_map(0, 0) {}
std_cofi_map(uint64_t base_address, uint32_t code_size) : i_cofi_map(base_address, code_size) {}
bool contains(uint64_t addr) { return map_data.find(addr) != map_data.end(); }
inline void set(uint64_t addr, cofi_inst_t* cofi_obj) { map_data[addr] = cofi_obj; }
inline cofi_inst_t* get(uint64_t addr) {
if(contains(addr)) return map_data[addr];
return nullptr;
}
};
class my_cofi_map : public i_cofi_map {
cofi_inst_t** map_data;
public:
my_cofi_map(uint64_t base_address, uint32_t code_size);
~my_cofi_map();
//inline cofi_inst_t*& operator [](uint64_t addr) {
// return map_data[addr-base_address];
//}
bool contains(uint64_t addr) {
return map_data[addr-base_address] != nullptr;
}
inline void set(uint64_t addr, cofi_inst_t* cofi_obj) {
assert(addr >= base_address && addr < base_address + code_size);
map_data[addr-base_address] = cofi_obj;
}
inline cofi_inst_t* get(uint64_t addr) {
if(addr < base_address || addr >= base_address + code_size) return nullptr;
return map_data[addr-base_address];
}
};
//typedef std::map<uint64_t, cofi_inst_t*> cofi_map_t;
typedef my_cofi_map cofi_map_t;
uint32_t disassemble_binary(const uint8_t* code, uint64_t base_address, uint64_t& code_size, cofi_map_t& cofi_map);
#endif