| 1001 | } |
| 1002 | |
| 1003 | void UnityPatching::sendAddressesFromSignatures(std::pair<uintptr_t,uintptr_t> executablefile_segment, bool is_64bit) |
| 1004 | { |
| 1005 | /* We need to query the executable memory to make the search */ |
| 1006 | ptrdiff_t executable_size = executablefile_segment.second - executablefile_segment.first; |
| 1007 | void* executable_local_addr = mmap(nullptr, executable_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 1008 | |
| 1009 | if (executable_local_addr == MAP_FAILED) { |
| 1010 | std::cerr << "Could not map a segment of size " << executable_size << " to host the executable memory" << std::endl; |
| 1011 | } |
| 1012 | else { |
| 1013 | int ret = MemAccess::read(executable_local_addr, reinterpret_cast<void*>(executablefile_segment.first), executable_size); |
| 1014 | |
| 1015 | if (ret == -1) |
| 1016 | std::cerr << "Could not read the executable segment memory" << std::endl; |
| 1017 | |
| 1018 | const usig_t* signatures = is_64bit ? UNITY_SIGNATURES_64 : UNITY_SIGNATURES_32; |
| 1019 | |
| 1020 | for (int i=0; signatures[i].id != UNITY_FUNCS_LEN; i++) { |
| 1021 | const char* signature = signatures[i].signature; |
| 1022 | if (strlen(signature) == 0) |
| 1023 | continue; |
| 1024 | |
| 1025 | /* Get the function name. Not ideal */ |
| 1026 | const char* name = ""; |
| 1027 | for (int j=0; UNITY_SYMBOLS[j].id != UNITY_FUNCS_LEN; j++) { |
| 1028 | if (UNITY_SYMBOLS[j].id == signatures[i].id) { |
| 1029 | name = UNITY_SYMBOLS[j].name; |
| 1030 | break; |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | Signature sig; |
| 1035 | sig.fromIdaString(signature); |
| 1036 | |
| 1037 | ptrdiff_t func_offset; |
| 1038 | uintptr_t func_addr; |
| 1039 | int match_count = SigSearch::Search(static_cast<uint8_t*>(executable_local_addr), executable_size, sig, &func_offset); |
| 1040 | |
| 1041 | switch (match_count) { |
| 1042 | case 0: |
| 1043 | // std::cout << "Found no occurrence of signature " << signature << " associated with function symbol " << signature[i].symbol << std::endl; |
| 1044 | break; |
| 1045 | case 1: |
| 1046 | func_addr = executablefile_segment.first + func_offset; |
| 1047 | std::cout << "Found unique matching signature ("<< signature << ") for function " << name << " in address " << std::hex << (uintptr_t)func_addr << std::endl; |
| 1048 | sendMessage(MSGN_UNITY_ADDR); |
| 1049 | sendData(&signatures[i].id, sizeof(int)); |
| 1050 | sendData(&func_addr, sizeof(uintptr_t)); |
| 1051 | break; |
| 1052 | default: |
| 1053 | std::cout << "Found " << match_count << " occurrences of signature " << signature << " associated with function " << name << std::endl; |
| 1054 | break; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | munmap(executable_local_addr, executable_size); |
| 1059 | } |
| 1060 | } |
nothing calls this directly
no test coverage detected