ConsistentAOTHomologousImage.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "ConsistentAOTHomologousImage.h"
  2. #include "vm/MetadataLock.h"
  3. #include "vm/GlobalMetadata.h"
  4. #include "vm/Class.h"
  5. #include "vm/Image.h"
  6. #include "vm/Exception.h"
  7. #include "vm/MetadataCache.h"
  8. #include "metadata/GenericMetadata.h"
  9. namespace hybridclr
  10. {
  11. namespace metadata
  12. {
  13. void ConsistentAOTHomologousImage::InitRuntimeMetadatas()
  14. {
  15. InitTypes();
  16. InitMethods();
  17. InitFields();
  18. }
  19. void ConsistentAOTHomologousImage::InitTypes()
  20. {
  21. const Table& typeDefTb = _rawImage->GetTable(TableType::TYPEDEF);
  22. uint32_t typeCount = typeDefTb.rowNum;
  23. _il2cppTypeForTypeDefs.resize(typeCount);
  24. _typeDefs.resize(typeCount);
  25. Il2CppImage* image = _aotAssembly->image;
  26. //if (image->typeCount != typeCount)
  27. //{
  28. // RaiseExecutionEngineException("image metadata not match");
  29. //}
  30. for (uint32_t index = 0; index < image->typeCount; index++)
  31. {
  32. Il2CppTypeDefinition* typeDef = (Il2CppTypeDefinition*)il2cpp::vm::MetadataCache::GetAssemblyTypeHandle(image, index);
  33. uint32_t rowIndex = DecodeTokenRowIndex(typeDef->token);
  34. IL2CPP_ASSERT(rowIndex > 0);
  35. if (rowIndex > typeCount)
  36. {
  37. continue;
  38. }
  39. TbTypeDef data = _rawImage->ReadTypeDef(rowIndex);
  40. uint32_t typeIndex = rowIndex - 1;
  41. _typeDefs[typeIndex] = typeDef;
  42. const Il2CppType* il2cppType = il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(typeDef->byvalTypeIndex);
  43. _il2cppTypeForTypeDefs[typeIndex] = il2cppType;
  44. const char* name1 = _rawImage->GetStringFromRawIndex(data.typeName);
  45. const char* name2 = il2cpp::vm::GlobalMetadata::GetStringFromIndex(typeDef->nameIndex);
  46. if (std::strcmp(name1, name2))
  47. {
  48. RaiseExecutionEngineException("metadata type not match");
  49. }
  50. const char* namespaze1 = _rawImage->GetStringFromRawIndex(data.typeNamespace);
  51. const char* namespaze2 = il2cpp::vm::GlobalMetadata::GetStringFromIndex(typeDef->namespaceIndex);
  52. if (std::strcmp(namespaze1, namespaze2))
  53. {
  54. RaiseExecutionEngineException("metadata type not match");
  55. }
  56. }
  57. }
  58. void ConsistentAOTHomologousImage::InitMethods()
  59. {
  60. const Table& methodTb = _rawImage->GetTable(TableType::METHOD);
  61. _methodDefs.resize(methodTb.rowNum);
  62. for (Il2CppTypeDefinition* type : _typeDefs)
  63. {
  64. for (uint16_t i = 0; i < type->method_count; i++)
  65. {
  66. const Il2CppMethodDefinition* methodDef = il2cpp::vm::GlobalMetadata::GetMethodDefinitionFromIndex(type->methodStart + i);
  67. uint32_t rowIndex = DecodeTokenRowIndex(methodDef->token);
  68. IL2CPP_ASSERT(rowIndex > 0 && rowIndex <= methodTb.rowNum);
  69. uint32_t methodIndex = rowIndex - 1;
  70. IL2CPP_ASSERT(_methodDefs[methodIndex] == nullptr);
  71. _methodDefs[methodIndex] = methodDef;
  72. TbMethod methodData = _rawImage->ReadMethod(rowIndex);
  73. const char* name1 = _rawImage->GetStringFromRawIndex(methodData.name);
  74. const char* name2 = il2cpp::vm::GlobalMetadata::GetStringFromIndex(methodDef->nameIndex);
  75. if (std::strcmp(name1, name2))
  76. {
  77. RaiseExecutionEngineException("metadata method not match");
  78. }
  79. }
  80. }
  81. }
  82. void ConsistentAOTHomologousImage::InitFields()
  83. {
  84. const Table& fieldTb = _rawImage->GetTable(TableType::FIELD);
  85. _fields.resize(fieldTb.rowNum);
  86. for (size_t i = 0; i < _typeDefs.size(); i++)
  87. {
  88. Il2CppTypeDefinition* type = _typeDefs[i];
  89. for (uint16_t j = 0; j < type->field_count; j++)
  90. {
  91. const Il2CppFieldDefinition* fieldDef = il2cpp::vm::GlobalMetadata::GetFieldDefinitionFromTypeDefAndFieldIndex(type, j);
  92. uint32_t rowIndex = DecodeTokenRowIndex(fieldDef->token);
  93. IL2CPP_ASSERT(rowIndex > 0);
  94. uint32_t fieldIndex = rowIndex - 1;
  95. IL2CPP_ASSERT(_fields[fieldIndex].fieldDef == nullptr);
  96. if (rowIndex >= fieldTb.rowNum)
  97. {
  98. continue;
  99. }
  100. _fields[fieldIndex] = { (uint32_t)i, fieldDef };
  101. TbField fieldData = _rawImage->ReadField(rowIndex);
  102. const char* name1 = _rawImage->GetStringFromRawIndex(fieldData.name);
  103. const char* name2 = il2cpp::vm::GlobalMetadata::GetStringFromIndex(fieldDef->nameIndex);
  104. if (std::strcmp(name1, name2))
  105. {
  106. RaiseExecutionEngineException("metadata field not match");
  107. }
  108. }
  109. }
  110. }
  111. MethodBody* ConsistentAOTHomologousImage::GetMethodBody(uint32_t token)
  112. {
  113. uint32_t rowIndex = DecodeTokenRowIndex(token);
  114. IL2CPP_ASSERT(rowIndex > 0);
  115. TbMethod methodData = _rawImage->ReadMethod(rowIndex);
  116. MethodBody* body = new (HYBRIDCLR_MALLOC_ZERO(sizeof(MethodBody))) MethodBody();
  117. ReadMethodBody(*_methodDefs[rowIndex - 1], methodData, *body);
  118. return body;
  119. }
  120. const Il2CppType* ConsistentAOTHomologousImage::GetIl2CppTypeFromRawTypeDefIndex(uint32_t index)
  121. {
  122. IL2CPP_ASSERT((size_t)index < _il2cppTypeForTypeDefs.size());
  123. return _il2cppTypeForTypeDefs[index];
  124. }
  125. Il2CppGenericContainer* ConsistentAOTHomologousImage::GetGenericContainerByRawIndex(uint32_t index)
  126. {
  127. return (Il2CppGenericContainer*)il2cpp::vm::GlobalMetadata::GetGenericContainerFromIndex(index);
  128. }
  129. Il2CppGenericContainer* ConsistentAOTHomologousImage::GetGenericContainerByTypeDefRawIndex(int32_t typeDefIndex)
  130. {
  131. Il2CppTypeDefinition* type = (Il2CppTypeDefinition*)il2cpp::vm::GlobalMetadata::GetTypeHandleFromIndex(typeDefIndex);
  132. return (Il2CppGenericContainer*)il2cpp::vm::GlobalMetadata::GetGenericContainerFromIndex(type->genericContainerIndex);
  133. }
  134. const Il2CppMethodDefinition* ConsistentAOTHomologousImage::GetMethodDefinitionFromRawIndex(uint32_t index)
  135. {
  136. IL2CPP_ASSERT((size_t)index < _methodDefs.size());
  137. return _methodDefs[index];
  138. }
  139. void ConsistentAOTHomologousImage::ReadFieldRefInfoFromFieldDefToken(uint32_t rowIndex, FieldRefInfo& ret)
  140. {
  141. IL2CPP_ASSERT(rowIndex > 0);
  142. AOTFieldData& fd = _fields[rowIndex - 1];
  143. ret.containerType = _il2cppTypeForTypeDefs[fd.typeDefIndex];
  144. ret.field = fd.fieldDef;
  145. }
  146. }
  147. }