method_index.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "method_index.h"
  2. #include <util/generic/yexception.h>
  3. #include <util/string/hex.h>
  4. namespace NYql {
  5. size_t GetMethodPtrIndex(uintptr_t ptr) {
  6. #ifdef _win_
  7. size_t offset;
  8. if (memcmp((void*)ptr, "\x48\x8B\x01\xFF", 4) == 0) {
  9. if (*(ui8*)(ptr + 4) == 0x60) {
  10. offset = *(ui8*)(ptr + 5);
  11. } else if (*(ui8*)(ptr + 4) == 0xa0) {
  12. offset = *(ui32*)(ptr + 5);
  13. } else {
  14. ythrow yexception() << "Unsupported code: " << HexEncode((char*)ptr + 4, 1);
  15. }
  16. } else if (memcmp((void*)ptr, "\x50\x48\x89\x0c\x24\x48\x8b\x0c\x24\x48\x8b\x01\x48\x8b", 14) == 0) {
  17. if (*(ui8*)(ptr + 14) == 0x40) {
  18. offset = *(ui8*)(ptr + 15);
  19. } else if (*(ui8*)(ptr + 14) == 0x80) {
  20. offset = *(ui32*)(ptr + 15);
  21. } else {
  22. ythrow yexception() << "Unsupported code: " << HexEncode((char*)ptr + 14, 1);
  23. }
  24. } else if (memcmp((void*)ptr, "\x48\x8b\x01\x48\x8b", 5) == 0) {
  25. if (*(ui8*)(ptr + 5) == 0x40) {
  26. offset = *(ui8*)(ptr + 6);
  27. } else if (*(ui8*)(ptr + 5) == 0x80) {
  28. offset = *(ui32*)(ptr + 6);
  29. } else {
  30. ythrow yexception() << "Unsupported code: " << HexEncode((char*)ptr + 5, 1);
  31. }
  32. } else {
  33. ythrow yexception() << "Unsupported code: " << HexEncode((char*)ptr, 16);
  34. }
  35. return offset / 8 + 1;
  36. #else
  37. return ptr >> 3;
  38. #endif
  39. }
  40. }