amd64_emulator.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "amd64_emulator.hpp"
  3. #include "exceptions/key_exception.hpp"
  4. #include "resource_traits/unicorn/unicorn_alloc.hpp"
  5. #define NKG_CURRENT_SOURCE_FILE() u8".\\navicat-patcher\\amd64_emulator.cpp"
  6. #define NKG_CURRENT_SOURCE_LINE() __LINE__
  7. namespace nkg {
  8. void amd64_emulator::_unicorn_hookcode_cb_stub(uc_engine* uc, uint64_t address, uint32_t size, void* user_data) {
  9. auto hook_stub_ctx = reinterpret_cast<hook_stub_context_t*>(user_data);
  10. auto& hook_callback = std::any_cast<std::function<hookcode_cb_t>&>(hook_stub_ctx->self->m_unicorn_hook_callbacks[hook_stub_ctx->unicorn_hook_handle]);
  11. hook_callback(address, size);
  12. }
  13. void amd64_emulator::_unicorn_hookmem_cb_stub(uc_engine* uc, uc_mem_type type, uint64_t address, int size, int64_t value, void* user_data) {
  14. auto hook_stub_ctx = reinterpret_cast<hook_stub_context_t*>(user_data);
  15. auto& hook_callback = std::any_cast<std::function<hookmem_cb_t>&>(hook_stub_ctx->self->m_unicorn_hook_callbacks[hook_stub_ctx->unicorn_hook_handle]);
  16. hook_callback(type, address, static_cast<unsigned int>(size), value);
  17. }
  18. bool amd64_emulator::_unicorn_eventmem_cb_stub(uc_engine* uc, uc_mem_type type, uint64_t address, int size, int64_t value, void* user_data) {
  19. auto hook_stub_ctx = reinterpret_cast<hook_stub_context_t*>(user_data);
  20. auto& hook_callback = std::any_cast<std::function<eventmem_cb_t>&>(hook_stub_ctx->self->m_unicorn_hook_callbacks[hook_stub_ctx->unicorn_hook_handle]);
  21. return hook_callback(type, address, static_cast<unsigned int>(size), value);
  22. }
  23. amd64_emulator::amd64_emulator() {
  24. auto err = uc_open(UC_ARCH_X86, UC_MODE_64, m_unicorn_engine.unsafe_addressof());
  25. if (err != UC_ERR_OK) {
  26. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, u8"uc_open failed.");
  27. }
  28. }
  29. void amd64_emulator::reg_read(int regid, void* value) {
  30. auto err = uc_reg_read(m_unicorn_engine.get(), regid, value);
  31. if (err != UC_ERR_OK) {
  32. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, u8"uc_reg_read failed.");
  33. }
  34. }
  35. void amd64_emulator::reg_write(int regid, const void* value) {
  36. auto err = uc_reg_write(m_unicorn_engine.get(), regid, value);
  37. if (err != UC_ERR_OK) {
  38. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, u8"uc_reg_write failed.");
  39. }
  40. }
  41. uint64_t amd64_emulator::msr_read(uint32_t rid) {
  42. uc_x86_msr msr;
  43. msr.rid = rid;
  44. auto err = uc_reg_read(m_unicorn_engine.get(), UC_X86_REG_MSR, &msr);
  45. if (err != UC_ERR_OK) {
  46. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, u8"uc_reg_write failed.");
  47. }
  48. return msr.value;
  49. }
  50. void amd64_emulator::msr_write(uint32_t rid, uint64_t value) {
  51. uc_x86_msr msr;
  52. msr.rid = rid;
  53. msr.value = value;
  54. auto err = uc_reg_write(m_unicorn_engine.get(), UC_X86_REG_MSR, &msr);
  55. if (err != UC_ERR_OK) {
  56. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, u8"uc_reg_write failed.");
  57. }
  58. }
  59. void amd64_emulator::mem_map(uint64_t address, size_t size, uint32_t perms) {
  60. auto err = uc_mem_map(m_unicorn_engine.get(), address, size, perms);
  61. if (err != UC_ERR_OK) {
  62. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, u8"uc_mem_map failed.");
  63. }
  64. }
  65. void amd64_emulator::mem_unmap(uint64_t address, size_t size) {
  66. auto err = uc_mem_unmap(m_unicorn_engine.get(), address, size);
  67. if (err != UC_ERR_OK) {
  68. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, u8"uc_mem_unmap failed.");
  69. }
  70. }
  71. void amd64_emulator::mem_read(uint64_t address, void* buf, size_t size) {
  72. auto err = uc_mem_read(m_unicorn_engine.get(), address, buf, size);
  73. if (err != UC_ERR_OK) {
  74. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, u8"uc_mem_read failed.");
  75. }
  76. }
  77. std::vector<uint8_t> amd64_emulator::mem_read(uint64_t address, size_t size) {
  78. std::vector<uint8_t> ret_buf(size);
  79. auto err = uc_mem_read(m_unicorn_engine.get(), address, ret_buf.data(), ret_buf.size());
  80. if (err != UC_ERR_OK) {
  81. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, u8"uc_mem_read failed.");
  82. }
  83. return ret_buf;
  84. }
  85. void amd64_emulator::mem_write(uint64_t address, const void* buf, size_t size) {
  86. auto err = uc_mem_write(m_unicorn_engine.get(), address, buf, size);
  87. if (err != UC_ERR_OK) {
  88. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, u8"uc_mem_write failed.");
  89. }
  90. }
  91. void amd64_emulator::mem_write(uint64_t address, const std::vector<uint8_t>& buf) {
  92. mem_write(address, buf.data(), buf.size());
  93. }
  94. bool amd64_emulator::is_address_mapped(uint64_t address) {
  95. resource_wrapper mapped_regions{ resource_traits::unicorn::unicorn_alloc{} };
  96. uint32_t mapped_regions_num;
  97. auto err = uc_mem_regions(m_unicorn_engine.get(), mapped_regions.unsafe_addressof<uc_mem_region*>(), &mapped_regions_num);
  98. if (err != UC_ERR_OK) {
  99. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, u8"uc_mem_regions failed.");
  100. }
  101. for (size_t i = 0; i < mapped_regions_num; ++i) {
  102. auto& region = mapped_regions.as<uc_mem_region*>()[i];
  103. if (region.begin <= address && address <= region.end) {
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. void amd64_emulator::hook_del(uc_hook hook_handle) {
  110. auto iter_of_hook_stub_ctxs = m_unicorn_hook_stub_ctxs.find(hook_handle);
  111. if (iter_of_hook_stub_ctxs == m_unicorn_hook_stub_ctxs.end()) {
  112. throw exceptions::key_exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"Target hook is not found.");
  113. }
  114. auto iter_of_hook_callbacks = m_unicorn_hook_callbacks.find(hook_handle);
  115. if (iter_of_hook_callbacks != m_unicorn_hook_callbacks.end()) {
  116. auto err = uc_hook_del(m_unicorn_engine.get(), hook_handle);
  117. if (err) {
  118. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, u8"hook_del failed.");
  119. }
  120. m_unicorn_hook_callbacks.erase(iter_of_hook_callbacks);
  121. m_unicorn_hook_stub_ctxs.erase(iter_of_hook_stub_ctxs);
  122. return;
  123. }
  124. __builtin_unreachable();
  125. }
  126. void amd64_emulator::emu_start(uint64_t begin_address, uint64_t end_address, uint64_t timeout, size_t count) {
  127. auto err = uc_emu_start(m_unicorn_engine.get(), begin_address, end_address, timeout, count);
  128. if (err != UC_ERR_OK) {
  129. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, u8"emu_start failed.");
  130. }
  131. }
  132. void amd64_emulator::emu_stop() {
  133. auto err = uc_emu_stop(m_unicorn_engine.get());
  134. if (err != UC_ERR_OK) {
  135. throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, u8"uc_emu_stop failed.");
  136. }
  137. }
  138. //void amd64_emulator::create_gdt_entry(uint64_t gdt_entry_address, uint32_t base, uint32_t limit, uint8_t access_byte, uint8_t flags) {
  139. // struct {
  140. // uint16_t limit0;
  141. // uint16_t base0;
  142. // uint8_t base1;
  143. // uint8_t access_byte;
  144. // uint8_t limit1 : 4;
  145. // uint8_t flags : 4;
  146. // uint8_t base2;
  147. // } segment_descriptor;
  148. // static_assert(sizeof(segment_descriptor) == 8);
  149. // segment_descriptor.limit0 = limit & 0xffff;
  150. // segment_descriptor.base0 = base & 0xffff;
  151. // segment_descriptor.base1 = (base >> 16) & 0xff;
  152. // segment_descriptor.access_byte = access_byte;
  153. // segment_descriptor.limit1 = (limit >> 16) & 0xf;
  154. // segment_descriptor.flags = flags & 0xf;
  155. // segment_descriptor.base2 = (base >> 24) & 0xff;
  156. // auto err = uc_mem_write(m_unicorn_engine.get(), gdt_entry_address, &segment_descriptor, sizeof(segment_descriptor));
  157. // if (err != UC_ERR_OK) {
  158. // throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, u8"uc_mem_write failed.");
  159. // }
  160. //}
  161. }
  162. #undef NKG_CURRENT_SOURCE_LINE
  163. #undef NKG_CURRENT_SOURCE_FILE