binlex
decompiler.h
1 #ifndef DECOMPILER_H
2 #define DECOMPILER_H
3 
4 #include <stdint.h>
5 #include <capstone/capstone.h>
6 #include "common.h"
7 #include "json.h"
8 #include "decompilerbase.h"
9 
10 #ifdef _WIN32
11 #define BINLEX_EXPORT __declspec(dllexport)
12 #else
13 #define BINLEX_EXPORT
14 #endif
15 
16 #define DECOMPILER_MAX_SECTIONS 256
17 #define SHA256_PRINTABLE_SIZE 65 /* including NULL terminator */
18 
19 #define DECOMPILER_VISITED_QUEUED 0
20 #define DECOMPILER_VISITED_ANALYZED 1
21 
22 #define DECOMPILER_GPU_MODE_CUDA 0
23 #define DECOMPILER_GPU_MODE_OPENCL 1
24 
25 typedef enum DECOMPILER_OPERAND_TYPE {
26  DECOMPILER_OPERAND_TYPE_BLOCK = 0,
27  DECOMPILER_OPERAND_TYPE_FUNCTION = 1,
28  DECOMPILER_OPERAND_TYPE_UNSET = 2
29 } DECOMPILER_OPERAND_TYPE;
30 
31 using json = nlohmann::json;
32 
33 namespace binlex {
34  class Decompiler : public DecompilerBase {
35  private:
36  typedef struct worker {
37  csh handle;
38  cs_err error;
39  uint64_t pc;
40  const uint8_t *code;
41  size_t code_size;
42  } worker;
43  typedef struct{
44  uint index;
45  cs_arch arch;
46  cs_mode mode;
47  void *sections;
48  } worker_args;
49  public:
50  struct Trait {
51  char *type;
52  string tmp_bytes;
53  char *bytes;
54  string tmp_trait;
55  char *trait;
56  uint edges;
57  uint blocks;
58  uint instructions;
59  uint size;
60  uint offset;
61  uint invalid_instructions;
62  uint cyclomatic_complexity;
63  uint average_instructions_per_block;
64  float bytes_entropy;
65  float trait_entropy;
66  char bytes_sha256[SHA256_PRINTABLE_SIZE];
67  char trait_sha256[SHA256_PRINTABLE_SIZE];
68  };
69  struct Section {
70  char *cpu;
71  bool instructions;
72  uint offset;
73  vector<struct Trait> traits;
74  void *data;
75  size_t data_size;
76  set<uint64_t> coverage;
77  map<uint64_t, uint> addresses;
78  map<uint64_t, int> visited;
79  queue<uint64_t> discovered;
80  };
81  static cs_arch arch;
82  static cs_mode mode;
83  struct Section sections[DECOMPILER_MAX_SECTIONS];
84  BINLEX_EXPORT Decompiler(const binlex::File &firef);
91  BINLEX_EXPORT bool Setup(cs_arch architecture, cs_mode mode_type);
96  BINLEX_EXPORT void SetInstructions(bool instructions, uint index);
102  BINLEX_EXPORT void* CreateTraitsForSection(uint index);
110  BINLEX_EXPORT static void AddDiscoveredBlock(uint64_t address, struct Section *sections, uint index);
118  BINLEX_EXPORT static void CollectOperands(cs_insn* insn, int operand_type, struct Section *sections, uint index);
125  BINLEX_EXPORT static uint CollectInsn(cs_insn* insn, struct Section *sections, uint index);
133  BINLEX_EXPORT void LinearDisassemble(void* data, size_t data_size, size_t offset, uint index);
141  BINLEX_EXPORT void Decompile(void* data, size_t data_size, size_t offset, uint index);
142  //void Seek(uint64_t address, size_t data_size, uint index);
150  BINLEX_EXPORT static void AppendTrait(struct Trait *trait, struct Section *sections, uint index);
151  BINLEX_EXPORT void FreeTraits(uint index);
157  BINLEX_EXPORT static bool IsNopInsn(cs_insn *ins);
163  BINLEX_EXPORT static bool IsSemanticNopInsn(cs_insn *ins);
169  BINLEX_EXPORT static bool IsTrapInsn(cs_insn *ins);
175  BINLEX_EXPORT static bool IsPrivInsn(cs_insn *ins);
181  BINLEX_EXPORT static bool IsEndInsn(cs_insn *insn);
187  BINLEX_EXPORT static uint IsConditionalInsn(cs_insn *insn);
193  BINLEX_EXPORT static uint64_t MaxAddress(set<uint64_t> coverage);
199  BINLEX_EXPORT static bool IsFunction(map<uint64_t, uint> &addresses, uint64_t address);
205  BINLEX_EXPORT static bool IsBlock(map<uint64_t, uint> &addresses, uint64_t address);
211  BINLEX_EXPORT static bool IsVisited(map<uint64_t, int> &visited, uint64_t address);
217  BINLEX_EXPORT bool IsAddress(map<uint64_t, uint> &addresses, uint64_t address, uint index);
223  BINLEX_EXPORT static bool IsWildcardInsn(cs_insn *insn);
229  BINLEX_EXPORT static string WildcardInsn(cs_insn *insn);
234  BINLEX_EXPORT static void ClearTrait(struct Trait *trait);
240  BINLEX_EXPORT json GetTrait(struct Trait &trait);
245  vector<json> GetTraits();
246  BINLEX_EXPORT static void * TraitWorker(void *args);
247  BINLEX_EXPORT static void * FinalizeTrait(struct Trait &trait);
248  BINLEX_EXPORT void AppendQueue(set<uint64_t> &addresses, uint operand_type, uint index);
249  //void Seek(uint offset, uint index);
250  BINLEX_EXPORT ~Decompiler();
251  };
252 }
253 #endif
binlex::Decompiler::CollectInsn
static BINLEX_EXPORT uint CollectInsn(cs_insn *insn, struct Section *sections, uint index)
binlex::Decompiler::Setup
BINLEX_EXPORT bool Setup(cs_arch architecture, cs_mode mode_type)
binlex::Decompiler::IsBlock
static BINLEX_EXPORT bool IsBlock(map< uint64_t, uint > &addresses, uint64_t address)
binlex::Decompiler::IsPrivInsn
static BINLEX_EXPORT bool IsPrivInsn(cs_insn *ins)
binlex::Decompiler::Section
Definition: decompiler.h:69
binlex::Decompiler::IsAddress
BINLEX_EXPORT bool IsAddress(map< uint64_t, uint > &addresses, uint64_t address, uint index)
binlex::Decompiler::IsEndInsn
static BINLEX_EXPORT bool IsEndInsn(cs_insn *insn)
binlex::Decompiler::IsWildcardInsn
static BINLEX_EXPORT bool IsWildcardInsn(cs_insn *insn)
binlex::Decompiler::MaxAddress
static BINLEX_EXPORT uint64_t MaxAddress(set< uint64_t > coverage)
binlex::Decompiler::Decompile
BINLEX_EXPORT void Decompile(void *data, size_t data_size, size_t offset, uint index)
binlex::Decompiler::AddDiscoveredBlock
static BINLEX_EXPORT void AddDiscoveredBlock(uint64_t address, struct Section *sections, uint index)
binlex::Decompiler::CollectOperands
static BINLEX_EXPORT void CollectOperands(cs_insn *insn, int operand_type, struct Section *sections, uint index)
binlex::Decompiler
Definition: decompiler.h:34
binlex::Decompiler::Trait
Definition: decompiler.h:50
binlex::Decompiler::GetTraits
vector< json > GetTraits()
binlex::Decompiler::SetInstructions
BINLEX_EXPORT void SetInstructions(bool instructions, uint index)
binlex::Decompiler::IsSemanticNopInsn
static BINLEX_EXPORT bool IsSemanticNopInsn(cs_insn *ins)
binlex::Decompiler::IsConditionalInsn
static BINLEX_EXPORT uint IsConditionalInsn(cs_insn *insn)
binlex::DecompilerBase
Definition: decompilerbase.h:12
binlex::Decompiler::IsFunction
static BINLEX_EXPORT bool IsFunction(map< uint64_t, uint > &addresses, uint64_t address)
binlex::Decompiler::CreateTraitsForSection
BINLEX_EXPORT void * CreateTraitsForSection(uint index)
binlex::Decompiler::GetTrait
BINLEX_EXPORT json GetTrait(struct Trait &trait)
binlex::File
Definition: file.h:14
binlex::Decompiler::IsTrapInsn
static BINLEX_EXPORT bool IsTrapInsn(cs_insn *ins)
binlex::Decompiler::LinearDisassemble
BINLEX_EXPORT void LinearDisassemble(void *data, size_t data_size, size_t offset, uint index)
binlex::Decompiler::IsVisited
static BINLEX_EXPORT bool IsVisited(map< uint64_t, int > &visited, uint64_t address)
binlex::Decompiler::IsNopInsn
static BINLEX_EXPORT bool IsNopInsn(cs_insn *ins)
binlex::Decompiler::ClearTrait
static BINLEX_EXPORT void ClearTrait(struct Trait *trait)
binlex
the binlex namespace
binlex::Decompiler::WildcardInsn
static BINLEX_EXPORT string WildcardInsn(cs_insn *insn)
binlex::Decompiler::AppendTrait
static BINLEX_EXPORT void AppendTrait(struct Trait *trait, struct Section *sections, uint index)