NativeSymbol.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "il2cpp-config.h"
  2. #include "il2cpp-class-internals.h"
  3. #include "os/Environment.h"
  4. #include "os/File.h"
  5. #include "os/Image.h"
  6. #include "os/Initialize.h"
  7. #include "os/LibraryLoader.h"
  8. #include "os/Locale.h"
  9. #include "os/Path.h"
  10. #include "utils/Collections.h"
  11. #include "utils/PathUtils.h"
  12. #include "utils/MemoryMappedFile.h"
  13. #include "utils/Runtime.h"
  14. #include "vm/Method.h"
  15. #include "vm-utils/Debugger.h"
  16. #include "vm-utils/DebugSymbolReader.h"
  17. #include "vm-utils/MethodDefinitionKey.h"
  18. #include "vm-utils/NativeSymbol.h"
  19. #include <string>
  20. #include <cstdlib>
  21. namespace il2cpp
  22. {
  23. namespace utils
  24. {
  25. #if IL2CPP_ENABLE_NATIVE_STACKTRACES
  26. static Il2CppMethodPointer MaskSpareBits(const Il2CppMethodPointer method)
  27. {
  28. return (Il2CppMethodPointer)((size_t)method & ~IL2CPP_POINTER_SPARE_BITS);
  29. }
  30. struct MethodInfoToMethodPointerConverter
  31. {
  32. Il2CppMethodPointer operator()(const MethodDefinitionKey& methodInfo) const
  33. {
  34. return MaskSpareBits(methodInfo.method);
  35. }
  36. };
  37. typedef il2cpp::utils::collections::ArrayValueMap<Il2CppMethodPointer, MethodDefinitionKey, MethodInfoToMethodPointerConverter> NativeMethodMap;
  38. static NativeMethodMap s_NativeMethods;
  39. struct NativeSymbolMutator
  40. {
  41. void operator()(MethodDefinitionKey* method)
  42. {
  43. // So, when a function is marked as noreturn, some compilers emit a call to that function and then
  44. // put the next function immediately after call instruction which means the return address on the stack
  45. // will appear to point to the wrong function. This messes up our stack walking as we now are confused
  46. // which method is actually on the stack. To work around this, we add "1" to each of the function addresses,
  47. // so each function appears to start 1 byte later which means the address on the stack will appear as if
  48. // it is pointing to function that called the no return function. This is okay because no function will
  49. // ever return to the first byte of another function.
  50. method->method = reinterpret_cast<Il2CppMethodPointer>(reinterpret_cast<intptr_t>(method->method) + 1);
  51. }
  52. };
  53. void NativeSymbol::RegisterMethods(const std::vector<MethodDefinitionKey>& managedMethods)
  54. {
  55. s_NativeMethods.assign(managedMethods);
  56. #if IL2CPP_MUTATE_METHOD_POINTERS
  57. NativeSymbolMutator mutator;
  58. s_NativeMethods.mutate(mutator);
  59. #endif
  60. }
  61. static const int k_ProbableMaximumManagedMethodSizeInBytes = 5000;
  62. static bool IsInstructionPointerProbablyInManagedMethod(Il2CppMethodPointer managedMethodStart, Il2CppMethodPointer instructionPointer)
  63. {
  64. if (std::abs((intptr_t)managedMethodStart - (intptr_t)instructionPointer) < k_ProbableMaximumManagedMethodSizeInBytes)
  65. return true;
  66. return false;
  67. }
  68. const VmMethod* NativeSymbol::GetMethodFromNativeSymbol(Il2CppMethodPointer nativeMethod)
  69. {
  70. // Address has to be above our base address
  71. if ((void*)nativeMethod < il2cpp::os::Image::GetImageBase())
  72. return NULL;
  73. // And within the addresses we have our methods in.
  74. if (il2cpp::os::Image::ManagedSectionExists())
  75. {
  76. if (!il2cpp::os::Image::IsInManagedSection((void*)nativeMethod))
  77. return NULL;
  78. }
  79. // Get the first symbol greater than the one we want, because our instruction pointer
  80. // probably won't be at the start of the method.
  81. NativeMethodMap::iterator methodAfterNativeMethod = s_NativeMethods.upper_bound(nativeMethod);
  82. // If method are all of the managed methods are in the same custom section of the binary, then assume we
  83. // will find the proper method, so the end iterator means we found the last method in this list. If we
  84. // don't have custom sections, then we may have actually not found the method. In that case, let's not
  85. // return a method we are unsure of.
  86. if (!il2cpp::os::Image::ManagedSectionExists())
  87. {
  88. if (methodAfterNativeMethod == s_NativeMethods.end())
  89. return NULL;
  90. if (!IsInstructionPointerProbablyInManagedMethod(methodAfterNativeMethod->method, nativeMethod))
  91. return NULL;
  92. }
  93. // Go back one to get the method we actually want.
  94. if (methodAfterNativeMethod != s_NativeMethods.begin())
  95. methodAfterNativeMethod--;
  96. return il2cpp::vm::MetadataCache::GetMethodInfoFromMethodHandle(methodAfterNativeMethod->methodHandle);
  97. }
  98. static void GetMethodDebugInfoWithSymbols(const MethodInfo* method, intptr_t methodPointer, int32_t size, Il2CppMethodDebugInfo& methodDebugInfo)
  99. {
  100. methodDebugInfo.methodPointer = (Il2CppMethodPointer)methodPointer;
  101. methodDebugInfo.code_size = size;
  102. // Get the source file name for the method in case usym file is available.
  103. #if !IL2CPP_MONO_DEBUGGER
  104. // Use usym file if debugging is not available
  105. il2cpp::utils::SourceLocation sourceLocation;
  106. if (il2cpp::utils::DebugSymbolReader::GetSourceLocation((void*)methodPointer, sourceLocation))
  107. {
  108. methodDebugInfo.file = sourceLocation.filePath;
  109. methodDebugInfo.line = sourceLocation.lineNumber;
  110. }
  111. #else
  112. const Il2CppSequencePoint* sequencePoint = il2cpp::utils::Debugger::GetSequenceFirstSequencePoint(method);
  113. if (sequencePoint != nullptr)
  114. {
  115. const Il2CppSequencePointSourceFile* sequencePointSourceFile = method->klass->image->codeGenModule->debuggerMetadata->sequencePointSourceFiles + sequencePoint->sourceFileIndex;
  116. methodDebugInfo.file = sequencePointSourceFile->file;
  117. methodDebugInfo.line = sequencePoint->lineStart;
  118. }
  119. #endif
  120. else
  121. {
  122. methodDebugInfo.file = NULL;
  123. methodDebugInfo.line = 0;
  124. }
  125. }
  126. #define IL2CPP_DEBUG_DUMP_NATIVE_SYMBOL_DATA 0
  127. #if IL2CPP_DEBUG_DUMP_NATIVE_SYMBOL_DATA
  128. static void DumpNativeSymbolData()
  129. {
  130. // You may want to change this to be a full path so it is easy to locate.
  131. FILE* dumpFile = fopen("nativeMethodsData.txt", "w");
  132. for (uint32_t i = 0; i < s_NativeMethods.size(); ++i)
  133. {
  134. const MethodDefinitionKey& methodKey = s_NativeMethods[i];
  135. void* address = methodKey.method;
  136. const MethodInfo* methodInfo = il2cpp::vm::MetadataCache::GetMethodInfoFromMethodHandle(methodKey.methodHandle);
  137. if (methodInfo != nullptr)
  138. fprintf(dumpFile, "%d\t[%p]:\t%s\n", i, address, vm::Method::GetFullName(methodInfo).c_str());
  139. }
  140. fclose(dumpFile);
  141. }
  142. #endif
  143. void NativeSymbol::GetAllManagedMethodsWithDebugInfo(void(*func)(const MethodInfo* method, Il2CppMethodDebugInfo* methodDebugInfo, void* userData), void* userData)
  144. {
  145. #if IL2CPP_DEBUG_DUMP_NATIVE_SYMBOL_DATA
  146. DumpNativeSymbolData();
  147. #endif
  148. // If we have no data, return as the logic below requires at least one method in the array.
  149. if (s_NativeMethods.size() == 0)
  150. return;
  151. // Store the last image so we can detect boundaries between methods from different images.
  152. const MethodInfo* prevMethod = nullptr;
  153. intptr_t prevMethodAddress = 0;
  154. size_t i = 0;
  155. // Iterate over all native methods which are registered from imaged and generic instances
  156. do
  157. {
  158. const MethodDefinitionKey& methodInfo = s_NativeMethods[i];
  159. intptr_t methodAddress = (intptr_t)methodInfo.method;
  160. // Skip unresolved methods
  161. if (methodAddress != 0)
  162. {
  163. // Get the method info for the method handle.
  164. // Note this will allocate memory for classes and methods.
  165. const MethodInfo* method = il2cpp::vm::MetadataCache::GetMethodInfoFromMethodHandle(methodInfo.methodHandle);
  166. // Report previous method with complete information
  167. if (prevMethodAddress != 0 && prevMethod != nullptr)
  168. {
  169. int32_t codeSize = (int32_t)(methodAddress - prevMethodAddress);
  170. // If the previous method is too far from the current one - we can have other native methods inbetween.
  171. // Use heuristic to estimate the size of the method - we expect managed method to be less than
  172. // k_ProbableMaximumManagedMethodSizeInBytes, but it still may happen that we have a native method
  173. // in the range even between methods in the same image.
  174. codeSize = std::min(k_ProbableMaximumManagedMethodSizeInBytes, codeSize);
  175. Il2CppMethodDebugInfo methodDebugInfo;
  176. GetMethodDebugInfoWithSymbols(prevMethod, prevMethodAddress, codeSize, methodDebugInfo);
  177. func(prevMethod, &methodDebugInfo, userData);
  178. }
  179. prevMethod = method;
  180. prevMethodAddress = methodAddress;
  181. }
  182. ++i;
  183. }
  184. while (i < s_NativeMethods.size());
  185. if (prevMethodAddress != 0 && prevMethod != nullptr)
  186. {
  187. // Report the last method using last method address.
  188. int32_t codeSize;
  189. // If there is a managed section we can use it to estimate the method size,
  190. // otherwise we use a heuristic.
  191. if (il2cpp::os::Image::ManagedSectionExists())
  192. {
  193. void *managedSectionStart, *managedSectionEnd;
  194. il2cpp::os::Image::GetManagedSectionStartAndEnd(managedSectionStart, managedSectionEnd);
  195. codeSize = std::min<int32_t>(k_ProbableMaximumManagedMethodSizeInBytes, (int32_t)((intptr_t)managedSectionEnd - prevMethodAddress));
  196. }
  197. else
  198. {
  199. codeSize = k_ProbableMaximumManagedMethodSizeInBytes;
  200. }
  201. Il2CppMethodDebugInfo methodDebugInfo;
  202. GetMethodDebugInfoWithSymbols(prevMethod, prevMethodAddress, codeSize, methodDebugInfo);
  203. func(prevMethod, &methodDebugInfo, userData);
  204. }
  205. }
  206. bool NativeSymbol::GetMethodDebugInfo(const MethodInfo* method, Il2CppMethodDebugInfo* methodDebugInfo)
  207. {
  208. Il2CppMethodPointer nativeMethod = method->virtualMethodPointer;
  209. if (il2cpp::os::Image::ManagedSectionExists())
  210. {
  211. if (!il2cpp::os::Image::IsInManagedSection((void*)nativeMethod))
  212. return false;
  213. }
  214. if (methodDebugInfo != NULL)
  215. {
  216. methodDebugInfo->methodPointer = method->virtualMethodPointer;
  217. methodDebugInfo->code_size = 0;
  218. methodDebugInfo->file = NULL;
  219. }
  220. return true;
  221. }
  222. #endif
  223. } /* namespace utils */
  224. } /* namespace il2cpp */