SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // clang++ -std=c++17 -o jit jit.cpp -lcapstone
  2.  
  3. #include <capstone/capstone.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/mman.h>
  8.  
  9. class x86 {
  10.  protected:
  11.   uint64_t size = 0;
  12.   uint64_t cap = 32;
  13.   uint8_t *buf;
  14.  
  15.  public:
  16.   inline x86() {
  17.     cap = 32;
  18.     buf = new uint8_t[cap];
  19.   }
  20.  
  21.   template <typename T>
  22.   inline T read(uint64_t i) const {
  23.     return *(T *)(void *)(buf + i);
  24.   }
  25.  
  26.   template <typename T>
  27.   inline uint64_t write_to(uint64_t i, T val) const {
  28.     *(T *)(void *)(buf + i) = val;
  29.     return size;
  30.   }
  31.  
  32.   template <typename T>
  33.   inline uint64_t write(T val) {
  34.     if (size + sizeof(val) >= cap - 1) {
  35.       uint8_t *new_code = new uint8_t[cap * 2];
  36.       memcpy(new_code, buf, cap);
  37.       cap *= 2;
  38.       delete buf;
  39.       buf = new_code;
  40.     }
  41.     uint64_t addr = write_to(size, val);
  42.     size += sizeof(T);
  43.     return addr;
  44.   }
  45.  
  46.   inline void bytes(void) {}
  47.   inline void bytes(unsigned char c) { write<unsigned char>(c); }
  48.  
  49.   template <typename... Args>
  50.   inline void bytes(unsigned char c, Args const &... args) {
  51.     bytes(c);
  52.     bytes(args...);
  53.   }
  54.  
  55.   inline uint64_t get_size() { return size; }
  56.   inline uint64_t get_cap() { return cap; }
  57.  
  58.   inline void copy(void *dest) { memcpy(dest, buf, size); }
  59.  
  60.   inline int dump(void) {
  61.     csh handle;
  62.     cs_insn *insn;
  63.     size_t count;
  64.  
  65.     if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) return -1;
  66.     count = cs_disasm(handle, buf, size, 0x1000, 0, &insn);
  67.     if (count > 0) {
  68.       size_t j;
  69.       for (j = 0; j < count; j++) {
  70.         printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic,
  71.                insn[j].op_str);
  72.       }
  73.  
  74.       cs_free(insn, count);
  75.     } else
  76.       printf("ERROR: Failed to disassemble given code!\n");
  77.  
  78.     cs_close(&handle);
  79.     return 1;
  80.   }
  81. };
  82.  
  83. typedef unsigned char(bytefunc)(void);
  84.  
  85. int main() {
  86.   x86 code;
  87.  
  88.   code.bytes(0xB8);
  89.   code.write<int>(42);  // mov eax, 42
  90.  
  91.   code.bytes(0xC3);     // ret
  92.  
  93.   code.dump();
  94.  
  95.  
  96.   void *func_ptr = mmap(NULL, code.get_size(),
  97.                         PROT_READ | PROT_EXEC | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
  98.  
  99.   code.copy(func_ptr);
  100.  
  101.   auto func = (bytefunc *)func_ptr;
  102.  
  103.   unsigned char c = func();
  104.   printf("%d\n", c);
  105. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top