InterpreterImage.cpp 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609
  1. #include "InterpreterImage.h"
  2. #include <cstring>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include "il2cpp-class-internals.h"
  7. #include "vm/GlobalMetadata.h"
  8. #include "vm/Type.h"
  9. #include "vm/Field.h"
  10. #include "vm/Object.h"
  11. #include "vm/Runtime.h"
  12. #include "vm/Array.h"
  13. #include "vm/MetadataLock.h"
  14. #include "vm/MetadataCache.h"
  15. #include "vm/MetadataAlloc.h"
  16. #include "vm/String.h"
  17. #include "vm/Reflection.h"
  18. #include "metadata/FieldLayout.h"
  19. #include "metadata/Il2CppTypeCompare.h"
  20. #include "metadata/GenericMetadata.h"
  21. #if HYBRIDCLR_UNITY_2021_OR_NEW
  22. #include "metadata/CustomAttributeCreator.h"
  23. #endif
  24. #include "os/Atomic.h"
  25. #include "icalls/mscorlib/System/MonoCustomAttrs.h"
  26. #include "MetadataModule.h"
  27. #include "MetadataUtil.h"
  28. #include "ClassFieldLayoutCalculator.h"
  29. #include "MetadataPool.h"
  30. #include "../interpreter/Engine.h"
  31. #include "../interpreter/InterpreterModule.h"
  32. namespace hybridclr
  33. {
  34. namespace metadata
  35. {
  36. static uint32_t s_nextImageIndexByKind[4] = { (1u << kMetadataImageIndexExtraShiftBitsA), 0, 0, 0};
  37. InterpreterImage* InterpreterImage::s_images[kMaxMetadataImageCount] = {};
  38. static int32_t GetImageKindByDllLength(uint32_t dllLength)
  39. {
  40. uint32_t maxPossibleIndexValue = dllLength * 4;
  41. for (int32_t i = 3; i >= 0; i--)
  42. {
  43. if (maxPossibleIndexValue <= kMetadataIndexMaskArr[i])
  44. {
  45. return i;
  46. }
  47. }
  48. return -1;
  49. }
  50. void InterpreterImage::Initialize()
  51. {
  52. }
  53. uint32_t InterpreterImage::AllocImageIndex(uint32_t dllLength)
  54. {
  55. int32_t kind = GetImageKindByDllLength(dllLength);
  56. if (kind < 0)
  57. {
  58. return kInvalidImageIndex;
  59. }
  60. for (int32_t finalKind = kind; finalKind >= 0; finalKind--)
  61. {
  62. uint32_t newImageIndex = s_nextImageIndexByKind[finalKind];
  63. // 255 is preserved for invalid image index when kind is 3
  64. if (newImageIndex >= kMaxMetadataImageIndexWithoutKind - (finalKind == 3))
  65. {
  66. continue;
  67. }
  68. s_nextImageIndexByKind[finalKind] += (1u << kMetadataImageIndexExtraShiftBitsArr[finalKind]);
  69. return newImageIndex | ((uint32_t)finalKind << (kMetadataImageIndexBits - kMetadataKindBits));
  70. }
  71. return kInvalidImageIndex;
  72. }
  73. void InterpreterImage::RegisterImage(InterpreterImage* image)
  74. {
  75. il2cpp::os::Atomic::FullMemoryBarrier();
  76. IL2CPP_ASSERT(image->GetIndex() > 0);
  77. s_images[image->GetIndex()] = image;
  78. }
  79. void InterpreterImage::InitBasic(Il2CppImage* image)
  80. {
  81. SetIl2CppImage(image);
  82. RegisterImage(this);
  83. }
  84. void InterpreterImage::BuildIl2CppAssembly(Il2CppAssembly* ass)
  85. {
  86. ass->token = EncodeToken(TableType::ASSEMBLY, 1);
  87. ass->referencedAssemblyStart = EncodeWithIndex(1);
  88. ass->referencedAssemblyCount = _rawImage->GetTableRowNum(TableType::ASSEMBLYREF);
  89. TbAssembly data = _rawImage->ReadAssembly(1);
  90. auto& aname = ass->aname;
  91. aname.hash_alg = data.hashAlgId;
  92. aname.major = data.majorVersion;
  93. aname.minor = data.minorVersion;
  94. aname.build = data.buildNumber;
  95. aname.revision = data.revisionNumber;
  96. aname.flags = data.flags;
  97. aname.public_key = _rawImage->GetBlobFromRawIndex(data.publicKey);
  98. aname.name = _rawImage->GetStringFromRawIndex(data.name);
  99. aname.culture = _rawImage->GetStringFromRawIndex(data.culture);
  100. }
  101. void InterpreterImage::BuildIl2CppImage(Il2CppImage* image2)
  102. {
  103. image2->typeCount = _rawImage->GetTableRowNum(TableType::TYPEDEF);
  104. image2->exportedTypeCount = _rawImage->GetTableRowNum(TableType::EXPORTEDTYPE);
  105. image2->customAttributeCount = _rawImage->GetTableRowNum(TableType::CUSTOMATTRIBUTE);
  106. #if HYBRIDCLR_UNITY_2019
  107. image2->typeStart = EncodeWithIndex(0);
  108. image2->customAttributeStart = EncodeWithIndex(0);
  109. image2->entryPointIndex = EncodeWithIndexExcept0(_rawImage->GetEntryPointToken());
  110. image2->exportedTypeStart = EncodeWithIndex(0);
  111. #else
  112. Il2CppImageGlobalMetadata* metadataImage = (Il2CppImageGlobalMetadata*)HYBRIDCLR_METADATA_MALLOC(sizeof(Il2CppImageGlobalMetadata));
  113. metadataImage->typeStart = EncodeWithIndex(0);
  114. metadataImage->customAttributeStart = EncodeWithIndex(0);
  115. metadataImage->entryPointIndex = EncodeWithIndexExcept0(_rawImage->GetEntryPointToken());
  116. metadataImage->exportedTypeStart = EncodeWithIndex(0);
  117. metadataImage->image = image2;
  118. image2->metadataHandle = reinterpret_cast<Il2CppMetadataImageHandle>(metadataImage);
  119. #endif
  120. image2->nameToClassHashTable = nullptr;
  121. image2->codeGenModule = nullptr;
  122. image2->token = EncodeWithIndex(0); // TODO
  123. image2->dynamic = 0;
  124. }
  125. void InterpreterImage::InitRuntimeMetadatas()
  126. {
  127. IL2CPP_ASSERT(_rawImage->GetTable(TableType::EXPORTEDTYPE).rowNum == 0);
  128. InitGenericParamDefs0();
  129. InitTypeDefs_0();
  130. InitMethodDefs0();
  131. InitGenericParamDefs();
  132. InitNestedClass(); // must before typedefs1, because parent may be nested class
  133. InitTypeDefs_1();
  134. InitGenericParamConstraintDefs();
  135. InitParamDefs();
  136. InitMethodDefs();
  137. InitFieldDefs();
  138. InitFieldLayouts();
  139. InitFieldRVAs();
  140. InitBlittables();
  141. InitMethodImpls0();
  142. InitProperties();
  143. InitEvents();
  144. InitMethodSemantics();
  145. InitConsts();
  146. InitCustomAttributes();
  147. InitTypeDefs_2();
  148. InitClassLayouts();
  149. InitInterfaces();
  150. InitClass();
  151. InitVTables();
  152. Il2CppHashMap<const Il2CppType*, uint32_t, Il2CppTypeHashShallow, Il2CppTypeEqualityComparerShallow> temp;
  153. _type2Indexs.swap(temp);
  154. delete _paramRawIndex2ActualParamIndex;
  155. _paramRawIndex2ActualParamIndex = nullptr;
  156. }
  157. void InterpreterImage::InitTypeDefs_0()
  158. {
  159. const Table& typeDefTb = _rawImage->GetTable(TableType::TYPEDEF);
  160. _typesDefines.resize(typeDefTb.rowNum);
  161. _typeDetails.resize(typeDefTb.rowNum);
  162. for (uint32_t i = 0, n = typeDefTb.rowNum; i < n; i++)
  163. {
  164. Il2CppTypeDefinition& cur = _typesDefines[i];
  165. TypeDefinitionDetail& typeDetail = _typeDetails[i];
  166. typeDetail.typeSizes = {};
  167. uint32_t rowIndex = i + 1;
  168. TbTypeDef data = _rawImage->ReadTypeDef(rowIndex);
  169. cur = {};
  170. cur.genericContainerIndex = kGenericContainerIndexInvalid;
  171. cur.declaringTypeIndex = kTypeDefinitionIndexInvalid;
  172. cur.elementTypeIndex = kTypeDefinitionIndexInvalid;
  173. cur.token = EncodeToken(TableType::TYPEDEF, rowIndex);
  174. bool isValueType = data.extends && IsValueTypeFromToken(DecodeTypeDefOrRefOrSpecCodedIndexTableType(data.extends), DecodeTypeDefOrRefOrSpecCodedIndexRowIndex(data.extends));
  175. Il2CppType* cppType = MetadataMallocT<Il2CppType>();
  176. cppType->type = isValueType ? IL2CPP_TYPE_VALUETYPE : IL2CPP_TYPE_CLASS;
  177. SET_IL2CPPTYPE_VALUE_TYPE(*cppType, isValueType);
  178. cppType->data.typeHandle = (Il2CppMetadataTypeHandle)&cur;
  179. cur.byvalTypeIndex = AddIl2CppTypeCache(cppType);
  180. #if HYBRIDCLR_UNITY_2019
  181. Il2CppType* byRefType = MetadataMallocT<Il2CppType>();
  182. *byRefType = *cppType;
  183. byRefType->byref = 1;
  184. cur.byrefTypeIndex = AddIl2CppTypeCache(byRefType);
  185. #endif
  186. if (IsInterface(cur.flags))
  187. {
  188. cur.interfaceOffsetsStart = EncodeWithIndex(0);
  189. cur.interface_offsets_count = 0;
  190. cur.vtableStart = EncodeWithIndex(0);
  191. cur.vtable_count = 0;
  192. }
  193. else
  194. {
  195. cur.interfaceOffsetsStart = 0;
  196. cur.interface_offsets_count = 0;
  197. cur.vtableStart = 0;
  198. cur.vtable_count = 0;
  199. }
  200. }
  201. }
  202. void InterpreterImage::InitTypeDefs_1()
  203. {
  204. const Table& typeDefTb = _rawImage->GetTable(TableType::TYPEDEF);
  205. for (uint32_t i = 0, n = typeDefTb.rowNum; i < n; i++)
  206. {
  207. Il2CppTypeDefinition& last = _typesDefines[i > 0 ? i - 1 : 0];
  208. Il2CppTypeDefinition& cur = _typesDefines[i];
  209. uint32_t rowIndex = i + 1;
  210. TbTypeDef data = _rawImage->ReadTypeDef(rowIndex); // token from 1
  211. cur.flags = data.flags;
  212. cur.nameIndex = EncodeWithIndex(data.typeName);
  213. cur.namespaceIndex = EncodeWithIndex(data.typeNamespace);
  214. cur.fieldStart = EncodeWithIndex(data.fieldList - 1);
  215. cur.methodStart = EncodeWithIndex(data.methodList - 1);
  216. if (i > 0)
  217. {
  218. last.field_count = (uint16_t)(cur.fieldStart - last.fieldStart);
  219. last.method_count = (uint16_t)(cur.methodStart - last.methodStart);
  220. }
  221. if (i == n - 1)
  222. {
  223. cur.field_count = (uint16_t)(_rawImage->GetTableRowNum(TableType::FIELD) - DecodeMetadataIndex(cur.fieldStart));
  224. cur.method_count = (uint16_t)(_rawImage->GetTableRowNum(TableType::METHOD) - DecodeMetadataIndex(cur.methodStart));
  225. }
  226. if (data.extends != 0)
  227. {
  228. const Il2CppType* parentType = ReadTypeFromToken(GetGenericContainerByTypeDefinition(&cur), nullptr, DecodeTypeDefOrRefOrSpecCodedIndexTableType(data.extends), DecodeTypeDefOrRefOrSpecCodedIndexRowIndex(data.extends));
  229. if (parentType->type == IL2CPP_TYPE_CLASS || parentType->type == IL2CPP_TYPE_VALUETYPE)
  230. {
  231. Il2CppTypeDefinition* parentDef = (Il2CppTypeDefinition*)parentType->data.typeHandle;
  232. // FIXE ME . check mscorelib
  233. const char* parentNs = il2cpp::vm::GlobalMetadata::GetStringFromIndex(parentDef->namespaceIndex);
  234. if (std::strcmp(parentNs, "System") == 0)
  235. {
  236. const char* parentName = il2cpp::vm::GlobalMetadata::GetStringFromIndex(parentDef->nameIndex);
  237. if (std::strcmp(parentName, "Enum") == 0)
  238. {
  239. cur.bitfield |= (1 << (il2cpp::vm::kBitIsValueType - 1));
  240. cur.bitfield |= (1 << (il2cpp::vm::kBitIsEnum - 1));
  241. }
  242. else if (std::strcmp(parentName, "ValueType") == 0)
  243. {
  244. cur.bitfield |= (1 << (il2cpp::vm::kBitIsValueType - 1));
  245. }
  246. }
  247. }
  248. cur.parentIndex = AddIl2CppTypeCache(parentType);
  249. }
  250. else
  251. {
  252. cur.parentIndex = kInvalidIndex;
  253. }
  254. cur.elementTypeIndex = kInvalidIndex;
  255. }
  256. }
  257. void InterpreterImage::InitTypeDefs_2()
  258. {
  259. const Table& typeDefTb = _rawImage->GetTable(TableType::TYPEDEF);
  260. for (uint32_t i = 0, n = typeDefTb.rowNum; i < n; i++)
  261. {
  262. TbTypeDef data = _rawImage->ReadTypeDef(i + 1); // token from 1
  263. Il2CppTypeDefinition& last = _typesDefines[i > 0 ? i - 1 : 0];
  264. Il2CppTypeDefinition& cur = _typesDefines[i];
  265. uint32_t typeIndex = i; // type index start from 0, diff with field index ...
  266. // enum element_type ==
  267. if (IsEnumType(&cur))
  268. {
  269. cur.elementTypeIndex = _fieldDetails[DecodeMetadataIndex(cur.fieldStart)].fieldDef.typeIndex;
  270. }
  271. auto classLayoutRow = _classLayouts.find(typeIndex);
  272. uint16_t packingSize = 0;
  273. if (classLayoutRow != _classLayouts.end())
  274. {
  275. auto& layoutData = classLayoutRow->second;
  276. packingSize = layoutData.packingSize;
  277. }
  278. else
  279. {
  280. cur.bitfield |= (1 << (il2cpp::vm::kClassSizeIsDefault - 1));
  281. }
  282. if (packingSize != 0)
  283. {
  284. cur.bitfield |= ((uint32_t)il2cpp::vm::GlobalMetadata::ConvertPackingSizeToEnum((uint8_t)packingSize) << (il2cpp::vm::kPackingSize - 1));
  285. }
  286. else
  287. {
  288. cur.bitfield |= (1 << (il2cpp::vm::kPackingSizeIsDefault - 1));
  289. }
  290. }
  291. }
  292. void InterpreterImage::InitParamDefs()
  293. {
  294. const Table& tb = _rawImage->GetTable(TableType::PARAM);
  295. // extra 16 for not name params
  296. _params.reserve(tb.rowNum + 16);
  297. _paramRawIndex2ActualParamIndex = new std::vector<TypeIndex>(tb.rowNum);
  298. //for (uint32_t i = 0; i < tb.rowNum; i++)
  299. //{
  300. // uint32_t rowIndex = i + 1;
  301. // Il2CppParameterDefinition& pd = _params[i].paramDef;
  302. // TbParam data = _rawImage->ReadParam(rowIndex);
  303. // pd.nameIndex = EncodeWithIndex(data.name);
  304. // pd.token = EncodeToken(TableType::PARAM, rowIndex);
  305. // // pd.typeIndex 在InitMethodDefs中解析signature后填充。
  306. //}
  307. }
  308. void InterpreterImage::InitFieldDefs()
  309. {
  310. const Table& fieldTb = _rawImage->GetTable(TableType::FIELD);
  311. _fieldDetails.resize(fieldTb.rowNum);
  312. for (size_t i = 0; i < _typesDefines.size(); i++)
  313. {
  314. Il2CppTypeDefinition& typeDef = _typesDefines[i];
  315. uint32_t start = DecodeMetadataIndex(typeDef.fieldStart);
  316. for (uint32_t k = 0; k < typeDef.field_count; k++)
  317. {
  318. FieldDetail& fd = _fieldDetails[start + k];
  319. fd.typeDefIndex = (uint32_t)i;
  320. }
  321. }
  322. for (uint32_t i = 0, n = fieldTb.rowNum; i < n; i++)
  323. {
  324. FieldDetail& fd = _fieldDetails[i];
  325. Il2CppFieldDefinition& cur = fd.fieldDef;
  326. fd.offset = 0;
  327. fd.defaultValueIndex = kDefaultValueIndexNull;
  328. uint32_t rowIndex = i + 1;
  329. TbField data = _rawImage->ReadField(rowIndex);
  330. BlobReader br = _rawImage->GetBlobReaderByRawIndex(data.signature);
  331. FieldRefSig frs;
  332. ReadFieldRefSig(br, GetGenericContainerByTypeDefRawIndex(DecodeMetadataIndex(fd.typeDefIndex)), frs);
  333. if (data.flags != 0)
  334. {
  335. Il2CppType typeWithAttrs = *frs.type;
  336. typeWithAttrs.attrs = data.flags;
  337. frs.type = MetadataPool::GetPooledIl2CppType(typeWithAttrs);
  338. }
  339. //cur = {};
  340. cur.nameIndex = EncodeWithIndex(data.name);
  341. cur.token = EncodeToken(TableType::FIELD, rowIndex);
  342. cur.typeIndex = AddIl2CppTypeCache(frs.type);
  343. }
  344. }
  345. void InterpreterImage::InitFieldLayouts()
  346. {
  347. const Table& tb = _rawImage->GetTable(TableType::FIELDLAYOUT);
  348. for (uint32_t i = 0; i < tb.rowNum; i++)
  349. {
  350. TbFieldLayout data = _rawImage->ReadFieldLayout(i + 1);
  351. _fieldDetails[data.field - 1].offset = sizeof(Il2CppObject) + data.offset;
  352. }
  353. }
  354. void InterpreterImage::InitFieldRVAs()
  355. {
  356. const Table& tb = _rawImage->GetTable(TableType::FIELDRVA);
  357. for (uint32_t i = 0; i < tb.rowNum; i++)
  358. {
  359. TbFieldRVA data = _rawImage->ReadFieldRVA(i + 1);
  360. FieldDetail& fd = _fieldDetails[data.field - 1];
  361. fd.defaultValueIndex = (uint32_t)_fieldDefaultValues.size();
  362. Il2CppFieldDefaultValue fdv = {};
  363. fdv.fieldIndex = data.field - 1;
  364. fdv.typeIndex = fd.fieldDef.typeIndex;
  365. uint32_t dataImageOffset = (uint32_t)-1;
  366. bool ret = _rawImage->TranslateRVAToImageOffset(data.rva, dataImageOffset);
  367. IL2CPP_ASSERT(ret);
  368. #if HYBRIDCLR_UNITY_2021_OR_NEW
  369. fdv.dataIndex = (DefaultValueDataIndex)EncodeWithIndex(EncodeWithBlobSource(dataImageOffset, BlobSource::RAW_IMAGE));
  370. #else
  371. fdv.dataIndex = (DefaultValueDataIndex)EncodeWithIndex(dataImageOffset);
  372. #endif
  373. _fieldDefaultValues.push_back(fdv);
  374. }
  375. }
  376. void InterpreterImage::InitBlittables()
  377. {
  378. const Table& typeDefTb = _rawImage->GetTable(TableType::TYPEDEF);
  379. std::vector<bool> computFlags(typeDefTb.rowNum, false);
  380. for (uint32_t i = 0, n = typeDefTb.rowNum; i < n; i++)
  381. {
  382. ComputeBlittable(&_typesDefines[i], computFlags);
  383. }
  384. }
  385. void InterpreterImage::ComputeBlittable(Il2CppTypeDefinition* def, std::vector<bool>& computFlags)
  386. {
  387. if (DecodeImageIndex(def->byvalTypeIndex) != GetIndex())
  388. {
  389. return;
  390. }
  391. uint32_t typeIndex = GetTypeRawIndex(def);
  392. if (computFlags[typeIndex])
  393. {
  394. return;
  395. }
  396. computFlags[typeIndex] = true;
  397. const Il2CppType* type = GetIl2CppTypeFromRawIndex(DecodeMetadataIndex(def->byvalTypeIndex));
  398. const char* typeName = il2cpp::vm::GlobalMetadata::GetStringFromIndex(def->nameIndex);
  399. bool blittable = false;
  400. if (type->type == IL2CPP_TYPE_VALUETYPE)
  401. {
  402. blittable = true;
  403. for (int i = 0; i < def->field_count; i++)
  404. {
  405. const Il2CppFieldDefinition* field = GetFieldDefinitionFromRawIndex(DecodeMetadataIndex(def->fieldStart + i));
  406. const Il2CppType* fieldType = il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(field->typeIndex);
  407. if (!hybridclr::metadata::IsInstanceField(fieldType))
  408. {
  409. continue;
  410. }
  411. switch (fieldType->type)
  412. {
  413. case IL2CPP_TYPE_BOOLEAN:
  414. case IL2CPP_TYPE_CHAR:
  415. case IL2CPP_TYPE_I1:
  416. case IL2CPP_TYPE_U1:
  417. case IL2CPP_TYPE_I2:
  418. case IL2CPP_TYPE_U2:
  419. case IL2CPP_TYPE_I4:
  420. case IL2CPP_TYPE_U4:
  421. case IL2CPP_TYPE_I:
  422. case IL2CPP_TYPE_U:
  423. case IL2CPP_TYPE_I8:
  424. case IL2CPP_TYPE_U8:
  425. case IL2CPP_TYPE_R4:
  426. case IL2CPP_TYPE_R8:
  427. case IL2CPP_TYPE_PTR:
  428. case IL2CPP_TYPE_FNPTR:
  429. {
  430. break;
  431. }
  432. case IL2CPP_TYPE_VALUETYPE:
  433. {
  434. Il2CppTypeDefinition* fieldDef = (Il2CppTypeDefinition*)fieldType->data.typeHandle;
  435. ComputeBlittable(fieldDef, computFlags);
  436. blittable = fieldDef->bitfield & (1 << (il2cpp::vm::kBitIsBlittable - 1));
  437. break;
  438. }
  439. default:
  440. {
  441. blittable = false;
  442. }
  443. }
  444. if (!blittable)
  445. {
  446. break;
  447. }
  448. }
  449. }
  450. if (blittable)
  451. {
  452. def->bitfield |= (1 << (il2cpp::vm::kBitIsBlittable - 1));
  453. }
  454. }
  455. #if HYBRIDCLR_UNITY_2021_OR_NEW
  456. DefaultValueDataIndex InterpreterImage::ConvertConstValue(CustomAttributeDataWriter& writer, uint32_t blobIndex, const Il2CppType* type)
  457. {
  458. Il2CppTypeEnum ttype = type->type;
  459. if (ttype == IL2CPP_TYPE_CLASS)
  460. {
  461. return kDefaultValueIndexNull;
  462. }
  463. DefaultValueIndex retIndex = EncodeWithIndex(EncodeWithBlobSource((DefaultValueIndex)writer.Size(), BlobSource::CONVERTED_IL2CPP_FORMAT));
  464. BlobReader reader = _rawImage->GetBlobReaderByRawIndex(blobIndex);
  465. switch (type->type)
  466. {
  467. case IL2CPP_TYPE_BOOLEAN:
  468. case IL2CPP_TYPE_I1:
  469. case IL2CPP_TYPE_U1:
  470. {
  471. writer.Write(reader, 1);
  472. break;
  473. }
  474. case IL2CPP_TYPE_CHAR:
  475. case IL2CPP_TYPE_I2:
  476. case IL2CPP_TYPE_U2:
  477. {
  478. writer.Write(reader, 2);
  479. break;
  480. }
  481. case IL2CPP_TYPE_I4:
  482. {
  483. writer.WriteCompressedInt32((int32_t)reader.Read32());
  484. break;
  485. }
  486. case IL2CPP_TYPE_U4:
  487. {
  488. writer.WriteCompressedUint32(reader.Read32());
  489. break;
  490. }
  491. case IL2CPP_TYPE_R4:
  492. {
  493. writer.Write(reader, 4);
  494. break;
  495. }
  496. case IL2CPP_TYPE_I8:
  497. case IL2CPP_TYPE_U8:
  498. case IL2CPP_TYPE_R8:
  499. {
  500. writer.Write(reader, 8);
  501. break;
  502. }
  503. case IL2CPP_TYPE_STRING:
  504. {
  505. std::string str = il2cpp::utils::StringUtils::Utf16ToUtf8((const Il2CppChar*)reader.GetData(), reader.GetLength() / 2);
  506. writer.WriteCompressedInt32((int32_t)str.length());
  507. writer.WriteBytes((const uint8_t*)str.c_str(), (int32_t)str.length());
  508. break;
  509. }
  510. default:
  511. {
  512. RaiseExecutionEngineException("not supported const type");
  513. }
  514. }
  515. return retIndex;
  516. }
  517. #endif
  518. void InterpreterImage::InitConsts()
  519. {
  520. const Table& tb = _rawImage->GetTable(TableType::CONSTANT);
  521. for (uint32_t i = 0; i < tb.rowNum; i++)
  522. {
  523. TbConstant data = _rawImage->ReadConstant(i + 1);
  524. TableType parentType = DecodeHasConstantType(data.parent);
  525. uint32_t rowIndex = DecodeHashConstantIndex(data.parent);
  526. Il2CppType tempType = {};
  527. tempType.type = (Il2CppTypeEnum)data.type;
  528. const Il2CppType& type = *MetadataPool::GetPooledIl2CppType(tempType);
  529. TypeIndex dataTypeIndex = AddIl2CppTypeCache(&type);
  530. #if !HYBRIDCLR_UNITY_2021_OR_NEW
  531. bool isNullValue = type.type == IL2CPP_TYPE_CLASS;
  532. #endif
  533. switch (parentType)
  534. {
  535. case TableType::FIELD:
  536. {
  537. FieldDetail& fd = _fieldDetails[rowIndex - 1];
  538. fd.defaultValueIndex = (uint32_t)_fieldDefaultValues.size();
  539. Il2CppFieldDefaultValue fdv = {};
  540. fdv.fieldIndex = rowIndex - 1;
  541. fdv.typeIndex = dataTypeIndex;
  542. #if HYBRIDCLR_UNITY_2021_OR_NEW
  543. fdv.dataIndex = ConvertConstValue(_constValues, data.value, &type);
  544. #else
  545. uint32_t dataImageOffset = _rawImage->GetImageOffsetOfBlob(type.type, data.value);
  546. fdv.dataIndex = isNullValue ? kDefaultValueIndexNull : (DefaultValueDataIndex)EncodeWithIndex(dataImageOffset);
  547. #endif
  548. _fieldDefaultValues.push_back(fdv);
  549. break;
  550. }
  551. case TableType::PARAM:
  552. {
  553. int32_t actualIndex = (*_paramRawIndex2ActualParamIndex)[rowIndex - 1];
  554. ParamDetail& fd = _params[actualIndex];
  555. fd.defaultValueIndex = (uint32_t)_paramDefaultValues.size();
  556. Il2CppParameterDefaultValue pdv = {};
  557. pdv.typeIndex = dataTypeIndex;
  558. pdv.parameterIndex = fd.parameterIndex;
  559. #if HYBRIDCLR_UNITY_2021_OR_NEW
  560. pdv.dataIndex = ConvertConstValue(_constValues, data.value, &type);
  561. #else
  562. uint32_t dataImageOffset = _rawImage->GetImageOffsetOfBlob(type.type, data.value);
  563. pdv.dataIndex = isNullValue ? kDefaultValueIndexNull : (DefaultValueDataIndex)EncodeWithIndex(dataImageOffset);
  564. #endif
  565. _paramDefaultValues.push_back(pdv);
  566. break;
  567. }
  568. case TableType::PROPERTY:
  569. {
  570. RaiseNotSupportedException("not support property const");
  571. break;
  572. }
  573. default:
  574. {
  575. RaiseExecutionEngineException("not support const TableType");
  576. break;
  577. }
  578. }
  579. }
  580. }
  581. void InterpreterImage::InitCustomAttributes()
  582. {
  583. const Table& tb = _rawImage->GetTable(TableType::CUSTOMATTRIBUTE);
  584. _tokenCustomAttributes.reserve(tb.rowNum);
  585. uint32_t threadStaticMethodToken = 0;
  586. Il2CppCustomAttributeTypeRange* curTypeRange = nullptr;
  587. for (uint32_t rowIndex = 1; rowIndex <= tb.rowNum; rowIndex++)
  588. {
  589. TbCustomAttribute data = _rawImage->ReadCustomAttribute(rowIndex);
  590. TableType parentType = DecodeHasCustomAttributeCodedIndexTableType(data.parent);
  591. uint32_t parentRowIndex = DecodeHasCustomAttributeCodedIndexRowIndex(data.parent);
  592. uint32_t token = EncodeToken(parentType, parentRowIndex);
  593. if (curTypeRange == nullptr || curTypeRange->token != token)
  594. {
  595. IL2CPP_ASSERT(_tokenCustomAttributes.find(token) == _tokenCustomAttributes.end());
  596. int32_t attributeStartIndex = EncodeWithIndex((int32_t)_customAttribues.size());
  597. int32_t handleIndex = (int32_t)_customAttributeHandles.size();
  598. _tokenCustomAttributes[token] = { (int32_t)EncodeWithIndex(handleIndex), false, nullptr, nullptr };
  599. #ifdef HYBRIDCLR_UNITY_2021_OR_NEW
  600. _customAttributeHandles.push_back({ token, (uint32_t)attributeStartIndex });
  601. #else
  602. _customAttributeHandles.push_back({ token, attributeStartIndex, 0 });
  603. #endif
  604. curTypeRange = &_customAttributeHandles[handleIndex];
  605. }
  606. #if !HYBRIDCLR_UNITY_2021_OR_NEW
  607. ++curTypeRange->count;
  608. #endif
  609. TableType ctorMethodTableType = DecodeCustomAttributeTypeCodedIndexTableType(data.type);
  610. uint32_t ctorMethodRowIndex = DecodeCustomAttributeTypeCodedIndexRowIndex(data.type);
  611. uint32_t ctorMethodToken = EncodeToken(ctorMethodTableType, ctorMethodRowIndex);
  612. //CustomAttribute ca = { ctorMethodToken, data.value };
  613. //ca.value = data.value;
  614. //ReadMethodRefInfoFromToken(nullptr, nullptr, , ca.attrCtorMethod);
  615. _customAttribues.push_back({ ctorMethodToken, data.value });
  616. if (parentType == TableType::FIELD)
  617. {
  618. // try set thread static flags
  619. if (threadStaticMethodToken == 0)
  620. {
  621. if (IsThreadStaticCtorToken(ctorMethodTableType, ctorMethodRowIndex))
  622. {
  623. threadStaticMethodToken = ctorMethodToken;
  624. }
  625. }
  626. if (ctorMethodToken == threadStaticMethodToken)
  627. {
  628. IL2CPP_ASSERT(threadStaticMethodToken != 0);
  629. _fieldDetails[parentRowIndex - 1].offset = THREAD_LOCAL_STATIC_MASK;
  630. }
  631. }
  632. }
  633. IL2CPP_ASSERT(_tokenCustomAttributes.size() == _customAttributeHandles.size());
  634. #ifdef HYBRIDCLR_UNITY_2021_OR_NEW
  635. // add extra Il2CppCustomAttributeTypeRange for compute count
  636. _customAttributeHandles.push_back({ 0, EncodeWithIndex((int32_t)_customAttribues.size()) });
  637. #endif
  638. #if !HYBRIDCLR_UNITY_2022_OR_NEW
  639. _customAttribtesCaches.resize(_tokenCustomAttributes.size());
  640. #endif
  641. }
  642. #ifdef HYBRIDCLR_UNITY_2021_OR_NEW
  643. void InterpreterImage::InitCustomAttributeData(CustomAttributesInfo& cai, const Il2CppCustomAttributeTypeRange& dataRange)
  644. {
  645. il2cpp::os::FastAutoLock metaLock(&il2cpp::vm::g_MetadataLock);
  646. if (cai.inited)
  647. {
  648. return;
  649. }
  650. BuildCustomAttributesData(cai, dataRange);
  651. il2cpp::os::Atomic::FullMemoryBarrier();
  652. cai.inited = true;
  653. }
  654. void InterpreterImage::BuildCustomAttributesData(CustomAttributesInfo& cai, const Il2CppCustomAttributeTypeRange& curTypeRange)
  655. {
  656. hybridclr::interpreter::ExecutingInterpImageScope scope(hybridclr::interpreter::InterpreterModule::GetCurrentThreadMachineState(), this->_il2cppImage);
  657. _il2cppFormatCustomDataBlob.Reset();
  658. const Il2CppCustomAttributeDataRange& nextTypeRange = *(&curTypeRange + 1);
  659. uint32_t attrCount = nextTypeRange.startOffset - curTypeRange.startOffset;
  660. IL2CPP_ASSERT(attrCount > 0 && attrCount < 1024);
  661. _il2cppFormatCustomDataBlob.WriteAttributeCount(attrCount);
  662. int32_t attrStartOffset = DecodeMetadataIndex(curTypeRange.startOffset);
  663. int32_t methodIndexDataOffset = _il2cppFormatCustomDataBlob.Size();
  664. _il2cppFormatCustomDataBlob.Skip(attrCount * sizeof(int32_t));
  665. for (uint32_t i = 0; i < attrCount; i++)
  666. {
  667. const CustomAttribute& ca = _customAttribues[attrStartOffset + (int32_t)i];
  668. MethodRefInfo mri = {};
  669. ReadMethodRefInfoFromToken(nullptr, nullptr, DecodeTokenTableType(ca.ctorMethodToken), DecodeTokenRowIndex(ca.ctorMethodToken), mri);
  670. const MethodInfo* ctorMethod = GetMethodInfoFromMethodDef(mri.containerType, mri.methodDef);
  671. MethodIndex ctorIndex = il2cpp::vm::GlobalMetadata::GetMethodIndexFromDefinition(mri.methodDef);
  672. _il2cppFormatCustomDataBlob.WriteMethodIndex(methodIndexDataOffset, ctorIndex);
  673. methodIndexDataOffset += sizeof(int32_t);
  674. if (ca.value != 0)
  675. {
  676. BlobReader reader = _rawImage->GetBlobReaderByRawIndex(ca.value);
  677. ConvertILCustomAttributeData2Il2CppFormat(ctorMethod, reader);
  678. }
  679. else
  680. {
  681. IL2CPP_ASSERT(mri.methodDef->parameterCount == 0);
  682. _il2cppFormatCustomDataBlob.WriteCompressedUint32(0);
  683. _il2cppFormatCustomDataBlob.WriteCompressedUint32(0);
  684. _il2cppFormatCustomDataBlob.WriteCompressedUint32(0);
  685. }
  686. }
  687. void* resultData = HYBRIDCLR_MALLOC(_il2cppFormatCustomDataBlob.Size());
  688. std::memcpy(resultData, _il2cppFormatCustomDataBlob.Data(), _il2cppFormatCustomDataBlob.Size());
  689. cai.dataStartPtr = resultData;
  690. cai.dataEndPtr = (uint8_t*)resultData + _il2cppFormatCustomDataBlob.Size();
  691. }
  692. void InterpreterImage::WriteEncodeTypeEnum(CustomAttributeDataWriter& writer, const Il2CppType* type)
  693. {
  694. Il2CppClass* klass = il2cpp::vm::Class::FromIl2CppType(type);
  695. if (type->type == IL2CPP_TYPE_ENUM || klass->enumtype)
  696. {
  697. writer.WriteByte((byte)IL2CPP_TYPE_ENUM);
  698. int32_t typeIndex = type->type == IL2CPP_TYPE_CLASS || type->type == IL2CPP_TYPE_VALUETYPE ? ((Il2CppTypeDefinition*)type->data.typeHandle)->byvalTypeIndex : AddIl2CppTypeCache(type);
  699. writer.WriteCompressedInt32(typeIndex);
  700. }
  701. else if (klass == il2cpp_defaults.systemtype_class)
  702. {
  703. writer.WriteByte((byte)IL2CPP_TYPE_IL2CPP_TYPE_INDEX);
  704. }
  705. else
  706. {
  707. writer.WriteByte((uint8_t)type->type);
  708. }
  709. }
  710. void InterpreterImage::ConvertBoxedValue(CustomAttributeDataWriter& writer, BlobReader& reader, bool writeType)
  711. {
  712. uint64_t obj = 0;
  713. Il2CppType kind = {};
  714. ReadCustomAttributeFieldOrPropType(reader, kind);
  715. ConvertFixedArg(writer, reader, &kind, true);
  716. }
  717. void InterpreterImage::ConvertSystemType(CustomAttributeDataWriter& writer, BlobReader& reader, bool writeType)
  718. {
  719. if (writeType)
  720. {
  721. writer.WriteByte((byte)IL2CPP_TYPE_IL2CPP_TYPE_INDEX);
  722. }
  723. Il2CppString* fullName = ReadSerString(reader);
  724. if (!fullName)
  725. {
  726. writer.WriteCompressedInt32(-1);
  727. return;
  728. }
  729. Il2CppReflectionType* type = GetReflectionTypeFromName(fullName);
  730. if (!type)
  731. {
  732. std::string stdTypeName = il2cpp::utils::StringUtils::Utf16ToUtf8(fullName->chars);
  733. TEMP_FORMAT(errMsg, "CustomAttribute fixed arg type:System.Type fullName:'%s' not find", stdTypeName.c_str());
  734. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetTypeLoadException(errMsg));
  735. }
  736. Il2CppClass* klass = il2cpp::vm::Class::FromIl2CppType(type->type);
  737. if (!klass->generic_class && (Il2CppTypeDefinition*)klass->typeMetadataHandle)
  738. {
  739. writer.WriteCompressedInt32(((Il2CppTypeDefinition*)klass->typeMetadataHandle)->byvalTypeIndex);
  740. }
  741. else
  742. {
  743. writer.WriteCompressedInt32(AddIl2CppTypeCache(type->type));
  744. }
  745. }
  746. void InterpreterImage::ConvertFixedArg(CustomAttributeDataWriter& writer, BlobReader& reader, const Il2CppType* type, bool writeType)
  747. {
  748. switch (type->type)
  749. {
  750. case IL2CPP_TYPE_BOOLEAN:
  751. case IL2CPP_TYPE_I1:
  752. case IL2CPP_TYPE_U1:
  753. {
  754. if (writeType)
  755. {
  756. writer.WriteByte((uint8_t)type->type);
  757. }
  758. writer.Write(reader, 1);
  759. break;
  760. }
  761. case IL2CPP_TYPE_CHAR:
  762. case IL2CPP_TYPE_I2:
  763. case IL2CPP_TYPE_U2:
  764. {
  765. if (writeType)
  766. {
  767. writer.WriteByte((uint8_t)type->type);
  768. }
  769. writer.Write(reader, 2);
  770. break;
  771. }
  772. case IL2CPP_TYPE_I4:
  773. {
  774. if (writeType)
  775. {
  776. writer.WriteByte((uint8_t)type->type);
  777. }
  778. writer.WriteCompressedInt32((int32_t)reader.Read32());
  779. break;
  780. }
  781. case IL2CPP_TYPE_U4:
  782. {
  783. if (writeType)
  784. {
  785. writer.WriteByte((uint8_t)type->type);
  786. }
  787. writer.WriteCompressedUint32(reader.Read32());
  788. break;
  789. }
  790. case IL2CPP_TYPE_R4:
  791. {
  792. if (writeType)
  793. {
  794. writer.WriteByte((uint8_t)type->type);
  795. }
  796. writer.Write(reader, 4);
  797. break;
  798. }
  799. case IL2CPP_TYPE_I8:
  800. case IL2CPP_TYPE_U8:
  801. case IL2CPP_TYPE_R8:
  802. {
  803. if (writeType)
  804. {
  805. writer.WriteByte((uint8_t)type->type);
  806. }
  807. writer.Write(reader, 8);
  808. break;
  809. }
  810. case IL2CPP_TYPE_SZARRAY:
  811. {
  812. if (writeType)
  813. {
  814. writer.WriteByte((uint8_t)type->type);
  815. }
  816. int32_t numElem = (int32_t)reader.Read32();
  817. writer.WriteCompressedInt32(numElem);
  818. if (numElem != -1)
  819. {
  820. //Il2CppType kind = {};
  821. //ReadCustomAttributeFieldOrPropType(reader, kind);
  822. const Il2CppType* eleType = type->data.type;
  823. WriteEncodeTypeEnum(writer, eleType);
  824. if (eleType->type == IL2CPP_TYPE_OBJECT)
  825. {
  826. // kArrayTypeWithDifferentElements
  827. writer.WriteByte(1);
  828. for (uint16_t i = 0; i < numElem; i++)
  829. {
  830. ConvertBoxedValue(writer, reader, false);
  831. }
  832. }
  833. else
  834. {
  835. // all element type is same.
  836. writer.WriteByte(0);
  837. for (uint16_t i = 0; i < numElem; i++)
  838. {
  839. ConvertFixedArg(writer, reader, eleType, false);
  840. }
  841. }
  842. }
  843. break;
  844. }
  845. case IL2CPP_TYPE_STRING:
  846. {
  847. if (writeType)
  848. {
  849. writer.WriteByte((uint8_t)type->type);
  850. }
  851. byte b = reader.PeekByte();
  852. if (b == 0xFF)
  853. {
  854. reader.SkipByte();
  855. writer.WriteCompressedInt32(-1);
  856. }
  857. else if (b == 0)
  858. {
  859. reader.SkipByte();
  860. writer.WriteCompressedInt32(0);
  861. }
  862. else
  863. {
  864. const byte* beginDataPtr = reader.GetDataOfReadPosition();
  865. uint32_t len = reader.ReadCompressedUint32();
  866. writer.WriteCompressedInt32((int32_t)len);
  867. writer.WriteBytes(reader.GetDataOfReadPosition(), len);
  868. reader.SkipBytes(len);
  869. }
  870. break;
  871. }
  872. case IL2CPP_TYPE_OBJECT:
  873. {
  874. ConvertBoxedValue(writer, reader, writeType);
  875. //*(Il2CppObject**)data = ReadBoxedValue(reader);
  876. // FIXME memory barrier
  877. break;
  878. }
  879. case IL2CPP_TYPE_CLASS:
  880. {
  881. Il2CppClass* klass = il2cpp::vm::Class::FromIl2CppType(type);
  882. if (!klass)
  883. {
  884. RaiseExecutionEngineException("type not find");
  885. }
  886. if (klass == il2cpp_defaults.object_class)
  887. {
  888. ConvertBoxedValue(writer, reader, writeType);
  889. }
  890. else if (klass == il2cpp_defaults.systemtype_class)
  891. {
  892. ConvertSystemType(writer, reader, writeType);
  893. }
  894. else
  895. {
  896. TEMP_FORMAT(errMsg, "fixed arg type:%s.%s not support", klass->namespaze, klass->name);
  897. RaiseNotSupportedException(errMsg);
  898. }
  899. break;
  900. }
  901. case IL2CPP_TYPE_VALUETYPE:
  902. {
  903. Il2CppClass* klass = il2cpp::vm::Class::FromIl2CppType(type);
  904. if (writeType)
  905. {
  906. writer.WriteByte((byte)IL2CPP_TYPE_ENUM);
  907. IL2CPP_ASSERT(klass->enumtype);
  908. int32_t typeIndex = klass->generic_class ? AddIl2CppTypeCache(type) : ((Il2CppTypeDefinition*)type->data.typeHandle)->byvalTypeIndex;
  909. writer.WriteCompressedInt32(typeIndex);
  910. }
  911. ConvertFixedArg(writer, reader, &klass->element_class->byval_arg, false);
  912. break;
  913. }
  914. case IL2CPP_TYPE_SYSTEM_TYPE:
  915. {
  916. ConvertSystemType(writer, reader, true);
  917. break;
  918. }
  919. case IL2CPP_TYPE_BOXED_OBJECT:
  920. {
  921. uint8_t fieldOrPropType = reader.ReadByte();
  922. IL2CPP_ASSERT(fieldOrPropType == 0x51);
  923. ConvertBoxedValue(writer, reader, writeType);
  924. break;
  925. }
  926. case IL2CPP_TYPE_ENUM:
  927. {
  928. Il2CppClass* klass = il2cpp::vm::Class::FromIl2CppType(type);
  929. IL2CPP_ASSERT(klass->enumtype);
  930. if (writeType)
  931. {
  932. int32_t typeIndex = klass->generic_class ? AddIl2CppTypeCache(type) : ((Il2CppTypeDefinition*)type->data.typeHandle)->byvalTypeIndex;
  933. writer.WriteCompressedInt32(typeIndex);
  934. }
  935. ConvertFixedArg(writer, reader, &klass->element_class->byval_arg, false);
  936. break;
  937. }
  938. default:
  939. {
  940. RaiseExecutionEngineException("not support fixed argument type");
  941. }
  942. }
  943. }
  944. void InterpreterImage::GetFieldDeclaringTypeIndexAndFieldIndexByName(const Il2CppTypeDefinition* declaringType, const char* name, int32_t& typeIndex, int32_t& fieldIndex)
  945. {
  946. Il2CppClass* klass = il2cpp::vm::GlobalMetadata::GetTypeInfoFromHandle((Il2CppMetadataTypeHandle)declaringType);
  947. FieldInfo* field = il2cpp::vm::Class::GetFieldFromName(klass, name);
  948. if (!field)
  949. {
  950. RaiseExecutionEngineException("GetFieldDeclaringTypeIndexAndFieldIndexByName can't find field");
  951. }
  952. if (field->parent == klass)
  953. {
  954. typeIndex = kTypeDefinitionIndexInvalid;
  955. }
  956. else
  957. {
  958. klass = field->parent;
  959. if (klass->generic_class)
  960. {
  961. RaiseExecutionEngineException("GetFieldDeclaringTypeIndexAndFieldIndexByName doesn't support field of generic CustomAttribute");
  962. }
  963. typeIndex = il2cpp::vm::GlobalMetadata::GetIndexForTypeDefinition(klass);
  964. }
  965. fieldIndex = (int32_t)(field - klass->fields);
  966. }
  967. void InterpreterImage::GetPropertyDeclaringTypeIndexAndPropertyIndexByName(const Il2CppTypeDefinition* declaringType, const char* name, int32_t& typeIndex, int32_t& fieldIndex)
  968. {
  969. Il2CppClass* klass = il2cpp::vm::GlobalMetadata::GetTypeInfoFromHandle((Il2CppMetadataTypeHandle)declaringType);
  970. const PropertyInfo* propertyInfo = il2cpp::vm::Class::GetPropertyFromName(klass, name);
  971. if (!propertyInfo)
  972. {
  973. RaiseExecutionEngineException("GetFieldDeclaringTypeIndexAndFieldIndexByName can't find field");
  974. }
  975. if (propertyInfo->parent == klass)
  976. {
  977. typeIndex = kTypeDefinitionIndexInvalid;
  978. }
  979. else
  980. {
  981. klass = propertyInfo->parent;
  982. if (klass->generic_class)
  983. {
  984. RaiseExecutionEngineException("GetPropertyDeclaringTypeIndexAndPropertyIndexByName doesn't support field of generic CustomAttribute");
  985. }
  986. typeIndex = il2cpp::vm::GlobalMetadata::GetIndexForTypeDefinition(klass);
  987. }
  988. #if UNITY_ENGINE_TUANJIE
  989. fieldIndex = -1;
  990. for (int32_t i = 0; i < klass->property_count; i++)
  991. {
  992. if (klass->properties[i] == propertyInfo)
  993. {
  994. fieldIndex = i;
  995. break;;
  996. }
  997. }
  998. IL2CPP_ASSERT(fieldIndex != -1);
  999. #else
  1000. fieldIndex = (int32_t)(propertyInfo - klass->properties);
  1001. #endif
  1002. }
  1003. void InterpreterImage::ConvertILCustomAttributeData2Il2CppFormat(const MethodInfo* ctorMethod, BlobReader& reader)
  1004. {
  1005. uint16_t prolog = reader.Read16();
  1006. IL2CPP_ASSERT(prolog == 0x0001);
  1007. IL2CPP_ASSERT(!ctorMethod->is_generic);
  1008. _tempCtorArgBlob.Reset();
  1009. for (uint16_t i = 0; i < ctorMethod->parameters_count; i++)
  1010. {
  1011. const Il2CppType* paramType = GET_METHOD_PARAMETER_TYPE(ctorMethod->parameters[i]);
  1012. ConvertFixedArg(_tempCtorArgBlob, reader, paramType, true);
  1013. }
  1014. uint16_t numNamed = reader.Read16();
  1015. uint32_t fieldCount = 0;
  1016. uint32_t propertyCount = 0;
  1017. _tempFieldBlob.Reset();
  1018. _tempPropertyBlob.Reset();
  1019. const Il2CppTypeDefinition* declaringType = GetUnderlyingTypeDefinition(&ctorMethod->klass->byval_arg);
  1020. for (uint16_t idx = 0; idx < numNamed; idx++)
  1021. {
  1022. byte fieldOrPropTypeTag = reader.ReadByte();
  1023. IL2CPP_ASSERT(fieldOrPropTypeTag == 0x53 || fieldOrPropTypeTag == 0x54);
  1024. Il2CppType fieldOrPropType = {};
  1025. ReadCustomAttributeFieldOrPropType(reader, fieldOrPropType);
  1026. Il2CppString* fieldOrPropName = ReadSerString(reader);
  1027. std::string stdStrName = il2cpp::utils::StringUtils::Utf16ToUtf8(fieldOrPropName->chars);
  1028. const char* cstrName = stdStrName.c_str();
  1029. int32_t fieldOrPropertyDeclaringTypeIndex = kTypeIndexInvalid;
  1030. int32_t fieldOrPropertyIndex = 0;
  1031. if (fieldOrPropTypeTag == 0x53)
  1032. {
  1033. ++fieldCount;
  1034. ConvertFixedArg(_tempFieldBlob, reader, &fieldOrPropType, true);
  1035. GetFieldDeclaringTypeIndexAndFieldIndexByName(declaringType, cstrName, fieldOrPropertyDeclaringTypeIndex, fieldOrPropertyIndex);
  1036. if (fieldOrPropertyDeclaringTypeIndex == kTypeDefinitionIndexInvalid)
  1037. {
  1038. _tempFieldBlob.WriteCompressedInt32(fieldOrPropertyIndex);
  1039. }
  1040. else
  1041. {
  1042. _tempFieldBlob.WriteCompressedInt32(-fieldOrPropertyIndex - 1);
  1043. _tempFieldBlob.WriteCompressedUint32(fieldOrPropertyDeclaringTypeIndex);
  1044. }
  1045. }
  1046. else
  1047. {
  1048. ++propertyCount;
  1049. ConvertFixedArg(_tempPropertyBlob, reader, &fieldOrPropType, true);
  1050. GetPropertyDeclaringTypeIndexAndPropertyIndexByName(declaringType, cstrName, fieldOrPropertyDeclaringTypeIndex, fieldOrPropertyIndex);
  1051. if (fieldOrPropertyDeclaringTypeIndex == kTypeDefinitionIndexInvalid)
  1052. {
  1053. _tempPropertyBlob.WriteCompressedInt32(fieldOrPropertyIndex);
  1054. }
  1055. else
  1056. {
  1057. _tempPropertyBlob.WriteCompressedInt32(-fieldOrPropertyIndex - 1);
  1058. _tempPropertyBlob.WriteCompressedUint32(fieldOrPropertyDeclaringTypeIndex);
  1059. }
  1060. }
  1061. }
  1062. _il2cppFormatCustomDataBlob.WriteCompressedUint32(ctorMethod->parameters_count);
  1063. _il2cppFormatCustomDataBlob.WriteCompressedUint32(fieldCount);
  1064. _il2cppFormatCustomDataBlob.WriteCompressedUint32(propertyCount);
  1065. _il2cppFormatCustomDataBlob.Write(_tempCtorArgBlob);
  1066. _il2cppFormatCustomDataBlob.Write(_tempFieldBlob);
  1067. _il2cppFormatCustomDataBlob.Write(_tempPropertyBlob);
  1068. }
  1069. #endif
  1070. #if !HYBRIDCLR_UNITY_2021_OR_NEW
  1071. void InterpreterImage::ConstructCustomAttribute(BlobReader& reader, Il2CppObject* obj, const MethodInfo* ctorMethod)
  1072. {
  1073. uint16_t prolog = reader.Read16();
  1074. IL2CPP_ASSERT(prolog == 0x0001);
  1075. if (ctorMethod->parameters_count == 0)
  1076. {
  1077. il2cpp::vm::Runtime::Invoke(ctorMethod, obj, nullptr, nullptr);
  1078. }
  1079. else
  1080. {
  1081. int32_t argSize = sizeof(uint64_t) * ctorMethod->parameters_count;
  1082. uint64_t* argDatas = (uint64_t*)alloca(argSize);
  1083. std::memset(argDatas, 0, argSize);
  1084. void** argPtrs = (void**)alloca(sizeof(void*) * ctorMethod->parameters_count); // same with argDatas
  1085. for (uint8_t i = 0; i < ctorMethod->parameters_count; i++)
  1086. {
  1087. argPtrs[i] = argDatas + i;
  1088. const Il2CppType* paramType = GET_METHOD_PARAMETER_TYPE(ctorMethod->parameters[i]);
  1089. ReadFixedArg(reader, paramType, argDatas + i);
  1090. Il2CppClass* paramKlass = il2cpp::vm::Class::FromIl2CppType(paramType);
  1091. if (!IS_CLASS_VALUE_TYPE(paramKlass))
  1092. {
  1093. argPtrs[i] = (void*)argDatas[i];
  1094. }
  1095. }
  1096. il2cpp::vm::Runtime::Invoke(ctorMethod, obj, argPtrs, nullptr);
  1097. // clear ref. may not need. gc memory barrier
  1098. std::memset(argDatas, 0, argSize);
  1099. }
  1100. uint16_t numNamed = reader.Read16();
  1101. Il2CppClass* klass = obj->klass;
  1102. for (uint16_t idx = 0; idx < numNamed; idx++)
  1103. {
  1104. byte fieldOrPropTypeTag = reader.ReadByte();
  1105. IL2CPP_ASSERT(fieldOrPropTypeTag == 0x53 || fieldOrPropTypeTag == 0x54);
  1106. Il2CppType fieldOrPropType = {};
  1107. ReadCustomAttributeFieldOrPropType(reader, fieldOrPropType);
  1108. Il2CppString* fieldOrPropName = ReadSerString(reader);
  1109. std::string stdStrName = il2cpp::utils::StringUtils::Utf16ToUtf8(fieldOrPropName->chars);
  1110. const char* cstrName = stdStrName.c_str();
  1111. uint64_t value = 0;
  1112. ReadFixedArg(reader, &fieldOrPropType, &value);
  1113. if (fieldOrPropTypeTag == 0x53)
  1114. {
  1115. FieldInfo* field = il2cpp::vm::Class::GetFieldFromName(klass, cstrName);
  1116. if (!field)
  1117. {
  1118. TEMP_FORMAT(errMsg, "CustomAttribute field missing. klass:%s.%s field:%s", klass->namespaze, klass->name, cstrName);
  1119. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetTypeInitializationException(errMsg, nullptr));
  1120. }
  1121. Il2CppReflectionField* refField = il2cpp::vm::Reflection::GetFieldObject(klass, field);
  1122. IL2CPP_ASSERT(IsTypeEqual(&fieldOrPropType, field->type));
  1123. uint32_t fieldSize = GetTypeValueSize(&fieldOrPropType);
  1124. std::memcpy((byte*)obj + field->offset, &value, fieldSize);
  1125. //fixme MEMORY BARRIER
  1126. IL2CPP_ASSERT(refField);
  1127. }
  1128. else
  1129. {
  1130. const PropertyInfo* prop = il2cpp::vm::Class::GetPropertyFromName(klass, cstrName);
  1131. if (!prop)
  1132. {
  1133. TEMP_FORMAT(errMsg, "CustomAttribute property missing. klass:%s property:%s", klass->name, cstrName);
  1134. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetTypeInitializationException(errMsg, nullptr));
  1135. }
  1136. IL2CPP_ASSERT(IsTypeEqual(&fieldOrPropType, GET_METHOD_PARAMETER_TYPE(prop->set->parameters[0])));
  1137. Il2CppException* ex = nullptr;
  1138. Il2CppClass* propKlass = il2cpp::vm::Class::FromIl2CppType(&fieldOrPropType);
  1139. IL2CPP_ASSERT(propKlass);
  1140. void* args[] = { (IS_CLASS_VALUE_TYPE(propKlass) ? &value : (void*)value) };
  1141. il2cpp::vm::Runtime::Invoke(prop->set, obj, args, &ex);
  1142. if (ex)
  1143. {
  1144. il2cpp::vm::Exception::Raise(ex);
  1145. }
  1146. }
  1147. }
  1148. }
  1149. CustomAttributesCache* InterpreterImage::GenerateCustomAttributesCacheInternal(CustomAttributeIndex index)
  1150. {
  1151. IL2CPP_ASSERT(index != kCustomAttributeIndexInvalid);
  1152. CustomAttributesCache* cache = _customAttribtesCaches[index];
  1153. if (cache)
  1154. {
  1155. return cache;
  1156. }
  1157. IL2CPP_ASSERT(index < (CustomAttributeIndex)_customAttributeHandles.size());
  1158. Il2CppCustomAttributeTypeRange& typeRange = _customAttributeHandles[index];
  1159. il2cpp::os::FastAutoLock metaLock(&il2cpp::vm::g_MetadataLock);
  1160. cache = _customAttribtesCaches[index];
  1161. if (cache)
  1162. {
  1163. return cache;
  1164. }
  1165. hybridclr::interpreter::ExecutingInterpImageScope scope(hybridclr::interpreter::InterpreterModule::GetCurrentThreadMachineState(), this->_il2cppImage);
  1166. cache = (CustomAttributesCache*)IL2CPP_CALLOC(1, sizeof(CustomAttributesCache));
  1167. int32_t count;
  1168. #ifdef HYBRIDCLR_UNITY_2021_OR_NEW
  1169. count = (int32_t)(_customAttributeHandles[index + 1].startOffset - typeRange.startOffset);
  1170. #else
  1171. count = (int32_t)typeRange.count;
  1172. #endif
  1173. cache->count = count;
  1174. cache->attributes = (Il2CppObject**)il2cpp::gc::GarbageCollector::AllocateFixed(sizeof(Il2CppObject*) * count, 0);
  1175. int32_t start = DecodeMetadataIndex(GET_CUSTOM_ATTRIBUTE_TYPE_RANGE_START(typeRange));
  1176. for (int32_t i = 0; i < count; i++)
  1177. {
  1178. int32_t attrIndex = start + i;
  1179. IL2CPP_ASSERT(attrIndex >= 0 && attrIndex < (int32_t)_customAttribues.size());
  1180. CustomAttribute& ca = _customAttribues[attrIndex];
  1181. MethodRefInfo mri = {};
  1182. ReadMethodRefInfoFromToken(nullptr, nullptr, DecodeTokenTableType(ca.ctorMethodToken), DecodeTokenRowIndex(ca.ctorMethodToken), mri);
  1183. const MethodInfo* ctorMethod = GetMethodInfoFromMethodDef(mri.containerType, mri.methodDef);
  1184. IL2CPP_ASSERT(ctorMethod);
  1185. Il2CppClass* klass = ctorMethod->klass;
  1186. Il2CppObject* attr = il2cpp::vm::Object::New(klass);
  1187. Il2CppArray* paramArr = nullptr;
  1188. if (ca.value != 0)
  1189. {
  1190. BlobReader reader = _rawImage->GetBlobReaderByRawIndex(ca.value);
  1191. ConstructCustomAttribute(reader, attr, ctorMethod);
  1192. }
  1193. else
  1194. {
  1195. IL2CPP_ASSERT(ctorMethod->parameters_count == 0);
  1196. il2cpp::vm::Runtime::Invoke(ctorMethod, attr, nullptr, nullptr);
  1197. }
  1198. cache->attributes[i] = attr;
  1199. HYBRIDCLR_SET_WRITE_BARRIER((void**)cache->attributes + i);
  1200. }
  1201. il2cpp::os::Atomic::FullMemoryBarrier();
  1202. _customAttribtesCaches[index] = cache;
  1203. return cache;
  1204. }
  1205. #elif HYBRIDCLR_UNITY_2021
  1206. CustomAttributesCache* InterpreterImage::GenerateCustomAttributesCacheInternal(CustomAttributeIndex index)
  1207. {
  1208. IL2CPP_ASSERT(index != kCustomAttributeIndexInvalid);
  1209. CustomAttributesCache* cache = _customAttribtesCaches[index];
  1210. if (cache)
  1211. {
  1212. return cache;
  1213. }
  1214. IL2CPP_ASSERT(index < (CustomAttributeIndex)_customAttributeHandles.size());
  1215. Il2CppCustomAttributeTypeRange& typeRange = _customAttributeHandles[index];
  1216. cache = _customAttribtesCaches[index];
  1217. if (cache)
  1218. {
  1219. return cache;
  1220. }
  1221. hybridclr::interpreter::ExecutingInterpImageScope scope(hybridclr::interpreter::InterpreterModule::GetCurrentThreadMachineState(), this->_il2cppImage);
  1222. void* start;
  1223. void* end;
  1224. std::tie(start, end) = CreateCustomAttributeDataTuple(&typeRange);
  1225. IL2CPP_ASSERT(start && end);
  1226. il2cpp::metadata::CustomAttributeDataReader reader(start, end);
  1227. cache = (CustomAttributesCache*)IL2CPP_CALLOC(1, sizeof(CustomAttributesCache));
  1228. cache->count = (int)reader.GetCount();
  1229. cache->attributes = (Il2CppObject**)il2cpp::gc::GarbageCollector::AllocateFixed(sizeof(Il2CppObject*) * cache->count, 0);
  1230. il2cpp::metadata::CustomAttributeDataIterator iter = reader.GetDataIterator();
  1231. for (int i = 0; i < cache->count; i++)
  1232. {
  1233. Il2CppException* exc = NULL;
  1234. il2cpp::metadata::CustomAttributeCreator creator;
  1235. if (reader.VisitCustomAttributeData(_il2cppImage, &iter, &creator, &exc))
  1236. {
  1237. cache->attributes[i] = creator.GetAttribute(&exc);
  1238. HYBRIDCLR_SET_WRITE_BARRIER((void**)&cache->attributes[i]);
  1239. }
  1240. if (exc != NULL)
  1241. {
  1242. il2cpp::gc::GarbageCollector::FreeFixed(cache->attributes);
  1243. HYBRIDCLR_FREE(cache);
  1244. il2cpp::vm::Exception::Raise(exc);
  1245. }
  1246. }
  1247. il2cpp::os::FastAutoLock metaLock(&il2cpp::vm::g_MetadataLock);
  1248. CustomAttributesCache* original = _customAttribtesCaches[index];
  1249. if (original)
  1250. {
  1251. // A non-NULL return value indicates some other thread already generated this cache.
  1252. // We need to cleanup the resources we allocated
  1253. il2cpp::gc::GarbageCollector::FreeFixed(cache->attributes);
  1254. HYBRIDCLR_FREE(cache);
  1255. return original;
  1256. }
  1257. il2cpp::os::Atomic::FullMemoryBarrier();
  1258. _customAttribtesCaches[index] = cache;
  1259. return cache;
  1260. }
  1261. #endif
  1262. void InterpreterImage::InitMethodDefs0()
  1263. {
  1264. const Table& typeDefTb = _rawImage->GetTable(TableType::TYPEDEF);
  1265. const Table& methodTb = _rawImage->GetTable(TableType::METHOD);
  1266. _methodDefines.resize(methodTb.rowNum);
  1267. for (Il2CppMethodDefinition& md : _methodDefines)
  1268. {
  1269. md.genericContainerIndex = kGenericContainerIndexInvalid;
  1270. }
  1271. }
  1272. void InterpreterImage::InitMethodDefs()
  1273. {
  1274. const Table& typeDefTb = _rawImage->GetTable(TableType::TYPEDEF);
  1275. const Table& methodTb = _rawImage->GetTable(TableType::METHOD);
  1276. for (uint32_t i = 0, n = typeDefTb.rowNum; i < n; i++)
  1277. {
  1278. Il2CppTypeDefinition& typeDef = _typesDefines[i];
  1279. uint32_t rawMethodStart = DecodeMetadataIndex(typeDef.methodStart);
  1280. for (int m = 0; m < typeDef.method_count; m++)
  1281. {
  1282. Il2CppMethodDefinition& md = _methodDefines[rawMethodStart + m];
  1283. md.declaringType = EncodeWithIndex(i);
  1284. }
  1285. }
  1286. int32_t paramTableRowNum = _rawImage->GetTable(TableType::PARAM).rowNum;
  1287. for (uint32_t index = 0; index < methodTb.rowNum; index++)
  1288. {
  1289. Il2CppMethodDefinition& md = _methodDefines[index];
  1290. uint32_t rowIndex = index + 1;
  1291. TbMethod methodData = _rawImage->ReadMethod(rowIndex);
  1292. md.nameIndex = EncodeWithIndex(methodData.name);
  1293. md.parameterStart = methodData.paramList - 1;
  1294. //md.genericContainerIndex = kGenericContainerIndexInvalid;
  1295. md.token = EncodeToken(TableType::METHOD, rowIndex);
  1296. md.flags = methodData.flags;
  1297. md.iflags = methodData.implFlags;
  1298. md.slot = kInvalidIl2CppMethodSlot;
  1299. if (index > 0)
  1300. {
  1301. auto& last = _methodDefines[index - 1];
  1302. last.parameterCount = md.parameterStart - last.parameterStart;
  1303. }
  1304. if (index == methodTb.rowNum - 1)
  1305. {
  1306. md.parameterCount = (int)paramTableRowNum - (int32_t)md.parameterStart;
  1307. }
  1308. //MethodBody& body = _methodBodies[index];
  1309. //ReadMethodBody(md, methodData, body);
  1310. }
  1311. for (uint32_t i = 0, n = typeDefTb.rowNum; i < n; i++)
  1312. {
  1313. Il2CppTypeDefinition& typeDef = _typesDefines[i];
  1314. uint32_t rawMethodStart = DecodeMetadataIndex(typeDef.methodStart);
  1315. bool isInterface = IsInterface(typeDef.flags);
  1316. uint16_t slotIdx = 0;
  1317. for (int m = 0; m < typeDef.method_count; m++)
  1318. {
  1319. Il2CppMethodDefinition& md = _methodDefines[rawMethodStart + m];
  1320. const char* methodName = _rawImage->GetStringFromRawIndex(DecodeMetadataIndex(md.nameIndex));
  1321. if (!std::strcmp(methodName, ".cctor"))
  1322. {
  1323. typeDef.bitfield |= (1 << (il2cpp::vm::kBitHasStaticConstructor - 1));
  1324. }
  1325. if (!std::strcmp(methodName, "Finalize"))
  1326. {
  1327. typeDef.bitfield |= (1 << (il2cpp::vm::kBitHasFinalizer - 1));
  1328. }
  1329. if (isInterface && IsInstanceMethod(&md) && IsVirtualMethod(md.flags))
  1330. {
  1331. md.slot = slotIdx++;
  1332. }
  1333. // TODO 可以考虑优化一下,将 signature在前一步存到暂时不用的 returnType里
  1334. TbMethod methodData = _rawImage->ReadMethod(rawMethodStart + m + 1);
  1335. BlobReader methodSigReader = _rawImage->GetBlobReaderByRawIndex(methodData.signature);
  1336. uint32_t namedParamStart = md.parameterStart;
  1337. uint32_t namedParamCount = md.parameterCount;
  1338. uint32_t actualParamStart = (uint32_t)_params.size();
  1339. ReadMethodDefSig(
  1340. methodSigReader,
  1341. GetGenericContainerByTypeDefinition(&typeDef),
  1342. GetGenericContainerByRawIndex(DecodeMetadataIndex(md.genericContainerIndex)),
  1343. md,
  1344. _params);
  1345. uint32_t actualParamCount = (uint32_t)_params.size() - actualParamStart;
  1346. md.parameterStart = actualParamStart;
  1347. md.parameterCount = actualParamCount;
  1348. for (uint32_t paramRowIndex = namedParamStart + 1; paramRowIndex <= namedParamStart + namedParamCount; paramRowIndex++)
  1349. {
  1350. TbParam data = _rawImage->ReadParam(paramRowIndex);
  1351. if (data.sequence > 0)
  1352. {
  1353. int32_t actualParamIndex = actualParamStart + data.sequence - 1;
  1354. ParamDetail& paramDetail = _params[actualParamIndex];
  1355. Il2CppParameterDefinition& pd = paramDetail.paramDef;
  1356. IL2CPP_ASSERT(paramDetail.parameterIndex == data.sequence - 1);
  1357. pd.nameIndex = EncodeWithIndex(data.name);
  1358. pd.token = EncodeToken(TableType::PARAM, paramRowIndex);
  1359. (*_paramRawIndex2ActualParamIndex)[paramRowIndex - 1] = actualParamIndex;
  1360. if (data.flags)
  1361. {
  1362. const Il2CppType* fieldType = il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(pd.typeIndex);
  1363. Il2CppType* newType = MetadataPool::ShallowCloneIl2CppType(fieldType);
  1364. newType->attrs = data.flags;
  1365. //paramDetail.type = newType;
  1366. pd.typeIndex = AddIl2CppTypeCache(newType);
  1367. }
  1368. }
  1369. else
  1370. {
  1371. // data.sequence == 0 is for returnType.
  1372. // used for parent of CustomeAttributes of ReturnType
  1373. // il2cpp not support ReturnType CustomAttributes. so we just ignore it.
  1374. #if SUPPORT_METHOD_RETURN_TYPE_CUSTOM_ATTRIBUTE
  1375. md.returnParameterToken = EncodeToken(TableType::PARAM, paramRowIndex);
  1376. #endif
  1377. }
  1378. }
  1379. }
  1380. }
  1381. }
  1382. const il2cpp::utils::dynamic_array<MethodImpl> InterpreterImage::GetTypeMethodImplByTypeDefinition(const Il2CppTypeDefinition* typeDef)
  1383. {
  1384. uint32_t index = (uint32_t)(typeDef - &_typesDefines[0]);
  1385. IL2CPP_ASSERT(index < (uint32_t)_typeDetails.size());
  1386. TypeDefinitionDetail& tdd = _typeDetails[index];
  1387. il2cpp::utils::dynamic_array<MethodImpl> methodImpls(tdd.methodImplCount);
  1388. for (uint32_t i = 0; i < tdd.methodImplCount; i++)
  1389. {
  1390. uint32_t index = tdd.methodImplStart + i;
  1391. TbMethodImpl data = _rawImage->ReadMethodImpl(index + 1);
  1392. Il2CppTypeDefinition& typeDef = _typesDefines[data.classIdx - 1];
  1393. Il2CppGenericContainer* gc = GetGenericContainerByTypeDefinition(&typeDef);
  1394. MethodImpl& impl = methodImpls[i];
  1395. ReadMethodRefInfoFromToken(gc, nullptr, DecodeMethodDefOrRefCodedIndexTableType(data.methodBody), DecodeMethodDefOrRefCodedIndexRowIndex(data.methodBody), impl.body);
  1396. ReadMethodRefInfoFromToken(gc, nullptr, DecodeMethodDefOrRefCodedIndexTableType(data.methodDeclaration), DecodeMethodDefOrRefCodedIndexRowIndex(data.methodDeclaration), impl.declaration);
  1397. }
  1398. return methodImpls;
  1399. }
  1400. void InterpreterImage::InitMethodImpls0()
  1401. {
  1402. const Table& miTb = _rawImage->GetTable(TableType::METHODIMPL);
  1403. for (uint32_t i = 0; i < miTb.rowNum; i++)
  1404. {
  1405. TbMethodImpl data = _rawImage->ReadMethodImpl(i + 1);
  1406. uint32_t typeIndex = data.classIdx - 1;
  1407. TypeDefinitionDetail& tdd = _typeDetails[typeIndex];
  1408. Il2CppTypeDefinition& typeDef = _typesDefines[typeIndex];
  1409. Il2CppGenericContainer* gc = GetGenericContainerByTypeDefinition(&typeDef);
  1410. if (tdd.methodImplCount == 0)
  1411. {
  1412. tdd.methodImplStart = i;
  1413. }
  1414. ++tdd.methodImplCount;
  1415. //MethodImpl impl;
  1416. //ReadMethodRefInfoFromToken(gc, nullptr, DecodeMethodDefOrRefCodedIndexTableType(data.methodBody), DecodeMethodDefOrRefCodedIndexRowIndex(data.methodBody), impl.body);
  1417. //ReadMethodRefInfoFromToken(gc, nullptr, DecodeMethodDefOrRefCodedIndexTableType(data.methodDeclaration), DecodeMethodDefOrRefCodedIndexRowIndex(data.methodDeclaration), impl.declaration);
  1418. //tdd.methodImpls.push_back(impl);
  1419. }
  1420. }
  1421. void InterpreterImage::InitProperties()
  1422. {
  1423. const Table& propertyMapTb = _rawImage->GetTable(TableType::PROPERTYMAP);
  1424. const Table& propertyTb = _rawImage->GetTable(TableType::PROPERTY);
  1425. _propeties.reserve(propertyTb.rowNum);
  1426. for (uint32_t rowIndex = 1; rowIndex <= propertyTb.rowNum; rowIndex++)
  1427. {
  1428. TbProperty data = _rawImage->ReadProperty(rowIndex);
  1429. _propeties.push_back({ _rawImage->GetStringFromRawIndex(data.name), data.flags, data.type, 0, 0
  1430. , nullptr
  1431. , { (StringIndex)EncodeWithIndex(data.name), kMethodIndexInvalid, kMethodIndexInvalid, (uint32_t)data.flags, EncodeToken(TableType::PROPERTY, rowIndex)}
  1432. });
  1433. }
  1434. Il2CppTypeDefinition* last = nullptr;
  1435. for (uint32_t rowIndex = 1; rowIndex <= propertyMapTb.rowNum; rowIndex++)
  1436. {
  1437. TbPropertyMap data = _rawImage->ReadPropertyMap(rowIndex);
  1438. Il2CppTypeDefinition* typeDef = &_typesDefines[data.parent - 1];
  1439. typeDef->propertyStart = EncodeWithIndex(data.propertyList); // start from 1
  1440. if (last != nullptr)
  1441. {
  1442. last->property_count = data.propertyList - DecodeMetadataIndex(last->propertyStart);
  1443. }
  1444. last = typeDef;
  1445. }
  1446. if (last)
  1447. {
  1448. last->property_count = propertyTb.rowNum - DecodeMetadataIndex(last->propertyStart) + 1;
  1449. }
  1450. #if HYBRIDCLR_UNITY_2019
  1451. for (const Il2CppTypeDefinition& typeDef : _typesDefines)
  1452. {
  1453. if (typeDef.property_count == 0)
  1454. {
  1455. continue;
  1456. }
  1457. for (int32_t start = DecodeMetadataIndex(typeDef.propertyStart), i = 0; i < typeDef.property_count; i++)
  1458. {
  1459. _propeties[start + i - 1].declaringType = &typeDef;
  1460. }
  1461. }
  1462. #endif
  1463. }
  1464. void InterpreterImage::InitEvents()
  1465. {
  1466. const Table& eventMapTb = _rawImage->GetTable(TableType::EVENTMAP);
  1467. const Table& eventTb = _rawImage->GetTable(TableType::EVENT);
  1468. _events.reserve(eventTb.rowNum);
  1469. for (uint32_t rowIndex = 1; rowIndex <= eventTb.rowNum; rowIndex++)
  1470. {
  1471. TbEvent data = _rawImage->ReadEvent(rowIndex);
  1472. _events.push_back({ _rawImage->GetStringFromRawIndex(data.name), data.eventFlags, data.eventType, 0, 0, 0
  1473. #if HYBRIDCLR_UNITY_2019
  1474. , nullptr
  1475. , { (StringIndex)EncodeWithIndex(data.name), kTypeIndexInvalid, kMethodIndexInvalid, kMethodIndexInvalid, kMethodIndexInvalid, EncodeToken(TableType::EVENT, rowIndex)}
  1476. #endif
  1477. });
  1478. }
  1479. Il2CppTypeDefinition* last = nullptr;
  1480. for (uint32_t rowIndex = 1; rowIndex <= eventMapTb.rowNum; rowIndex++)
  1481. {
  1482. TbEventMap data = _rawImage->ReadEventMap(rowIndex);
  1483. Il2CppTypeDefinition* typeDef = &_typesDefines[data.parent - 1];
  1484. typeDef->eventStart = EncodeWithIndex(data.eventList); // start from 1
  1485. if (last != nullptr)
  1486. {
  1487. last->event_count = data.eventList - DecodeMetadataIndex(last->eventStart);
  1488. }
  1489. last = typeDef;
  1490. }
  1491. if (last)
  1492. {
  1493. last->event_count = eventTb.rowNum - DecodeMetadataIndex(last->eventStart) + 1;
  1494. }
  1495. #if HYBRIDCLR_UNITY_2019
  1496. for (const Il2CppTypeDefinition& typeDef : _typesDefines)
  1497. {
  1498. if (typeDef.event_count == 0)
  1499. {
  1500. continue;
  1501. }
  1502. for (int32_t start = DecodeMetadataIndex(typeDef.eventStart), i = 0; i < typeDef.event_count; i++)
  1503. {
  1504. EventDetail& ed = _events[start + i - 1];
  1505. ed.declaringType = &typeDef;
  1506. ed.il2cppDefinition.typeIndex = typeDef.byvalTypeIndex;
  1507. }
  1508. }
  1509. #endif
  1510. }
  1511. void InterpreterImage::InitMethodSemantics()
  1512. {
  1513. const Table& msTb = _rawImage->GetTable(TableType::METHODSEMANTICS);
  1514. for (uint32_t rowIndex = 1; rowIndex <= msTb.rowNum; rowIndex++)
  1515. {
  1516. TbMethodSemantics data = _rawImage->ReadMethodSemantics(rowIndex);
  1517. uint32_t method = data.method;
  1518. uint16_t semantics = data.semantics;
  1519. TableType tableType = DecodeHasSemanticsCodedIndexTableType(data.association);
  1520. uint32_t propertyOrEventIndex = DecodeHasSemanticsCodedIndexRowIndex(data.association) - 1;
  1521. if (semantics & (uint16_t)MethodSemanticsAttributes::Getter)
  1522. {
  1523. IL2CPP_ASSERT(tableType == TableType::PROPERTY);
  1524. PropertyDetail& pd = _propeties[propertyOrEventIndex];
  1525. pd.getterMethodIndex = method;
  1526. #if HYBRIDCLR_UNITY_2019
  1527. pd.il2cppDefinition.get = method - DecodeMetadataIndex(pd.declaringType->methodStart) - 1;
  1528. #endif
  1529. }
  1530. if (semantics & (uint16_t)MethodSemanticsAttributes::Setter)
  1531. {
  1532. IL2CPP_ASSERT(tableType == TableType::PROPERTY);
  1533. PropertyDetail& pd = _propeties[propertyOrEventIndex];
  1534. pd.setterMethodIndex = method;
  1535. #if HYBRIDCLR_UNITY_2019
  1536. pd.il2cppDefinition.set = method - DecodeMetadataIndex(pd.declaringType->methodStart) - 1;
  1537. #endif
  1538. }
  1539. if (semantics & (uint16_t)MethodSemanticsAttributes::AddOn)
  1540. {
  1541. IL2CPP_ASSERT(tableType == TableType::EVENT);
  1542. EventDetail& ed = _events[propertyOrEventIndex];
  1543. ed.addMethodIndex = method;
  1544. #if HYBRIDCLR_UNITY_2019
  1545. ed.il2cppDefinition.add = method - DecodeMetadataIndex(ed.declaringType->methodStart) - 1;
  1546. #endif
  1547. }
  1548. if (semantics & (uint16_t)MethodSemanticsAttributes::RemoveOn)
  1549. {
  1550. IL2CPP_ASSERT(tableType == TableType::EVENT);
  1551. EventDetail& ed = _events[propertyOrEventIndex];
  1552. ed.removeMethodIndex = method;
  1553. #if HYBRIDCLR_UNITY_2019
  1554. ed.il2cppDefinition.remove = method - DecodeMetadataIndex(ed.declaringType->methodStart) - 1;
  1555. #endif
  1556. }
  1557. if (semantics & (uint16_t)MethodSemanticsAttributes::Fire)
  1558. {
  1559. IL2CPP_ASSERT(tableType == TableType::EVENT);
  1560. EventDetail& ed = _events[propertyOrEventIndex];
  1561. ed.fireMethodIndex = method;
  1562. #if HYBRIDCLR_UNITY_2019
  1563. ed.il2cppDefinition.raise = method - DecodeMetadataIndex(ed.declaringType->methodStart) - 1;
  1564. #endif
  1565. }
  1566. }
  1567. }
  1568. struct EnclosingClassInfo
  1569. {
  1570. uint32_t enclosingTypeIndex; // rowIndex - 1
  1571. std::vector<uint32_t> nestedTypeIndexs;
  1572. };
  1573. void InterpreterImage::InitNestedClass()
  1574. {
  1575. const Table& nestedClassTb = _rawImage->GetTable(TableType::NESTEDCLASS);
  1576. _nestedTypeDefineIndexs.reserve(nestedClassTb.rowNum);
  1577. std::vector<EnclosingClassInfo> enclosingTypes;
  1578. for (uint32_t i = 0; i < nestedClassTb.rowNum; i++)
  1579. {
  1580. TbNestedClass data = _rawImage->ReadNestedClass(i + 1);
  1581. Il2CppTypeDefinition& nestedType = _typesDefines[data.nestedClass - 1];
  1582. Il2CppTypeDefinition& enclosingType = _typesDefines[data.enclosingClass - 1];
  1583. if (enclosingType.nested_type_count == 0)
  1584. {
  1585. // 此行代码不能删,用于标识 enclosingTypes的index
  1586. enclosingType.nestedTypesStart = (uint32_t)enclosingTypes.size();
  1587. enclosingTypes.push_back({ data.enclosingClass - 1 });
  1588. }
  1589. ++enclosingType.nested_type_count;
  1590. enclosingTypes[enclosingType.nestedTypesStart].nestedTypeIndexs.push_back(data.nestedClass - 1);
  1591. //_nestedTypeDefineIndexs.push_back(data.nestedClass - 1);
  1592. nestedType.declaringTypeIndex = enclosingType.byvalTypeIndex;
  1593. }
  1594. for (auto& enclosingType : enclosingTypes)
  1595. {
  1596. Il2CppTypeDefinition& enclosingTypeDef = _typesDefines[enclosingType.enclosingTypeIndex];
  1597. IL2CPP_ASSERT(enclosingType.nestedTypeIndexs.size() == (size_t)enclosingTypeDef.nested_type_count);
  1598. enclosingTypeDef.nestedTypesStart = (NestedTypeIndex)_nestedTypeDefineIndexs.size();
  1599. enclosingTypeDef.nested_type_count = (uint16_t)enclosingType.nestedTypeIndexs.size();
  1600. _nestedTypeDefineIndexs.insert(_nestedTypeDefineIndexs.end(), enclosingType.nestedTypeIndexs.begin(), enclosingType.nestedTypeIndexs.end());
  1601. }
  1602. }
  1603. void InterpreterImage::InitClassLayouts()
  1604. {
  1605. const Table& classLayoutTb = _rawImage->GetTable(TableType::CLASSLAYOUT);
  1606. for (uint32_t i = 0; i < classLayoutTb.rowNum; i++)
  1607. {
  1608. TbClassLayout data = _rawImage->ReadClassLayout(i + 1);
  1609. _classLayouts[data.parent - 1] = data;
  1610. if (data.classSize > 0)
  1611. {
  1612. Il2CppTypeDefinitionSizes& typeSizes = _typeDetails[data.parent - 1].typeSizes;
  1613. typeSizes.instance_size = data.classSize + sizeof(Il2CppObject);
  1614. }
  1615. }
  1616. ClassFieldLayoutCalculator calculator(this);
  1617. for (Il2CppTypeDefinition& type : _typesDefines)
  1618. {
  1619. const Il2CppType* il2cppType = GetIl2CppTypeFromTypeDefinition(&type);
  1620. calculator.CalcClassNotStaticFields(il2cppType);
  1621. }
  1622. for (TypeDefinitionDetail& type : _typeDetails)
  1623. {
  1624. const Il2CppTypeDefinition* typeDef = GetTypeDefinitionByTypeDetail(&type);
  1625. const Il2CppType* il2cppType = GetIl2CppTypeFromTypeDefinition(typeDef);
  1626. calculator.CalcClassStaticFields(il2cppType);
  1627. ClassLayoutInfo* layout = calculator.GetClassLayoutInfo(il2cppType);
  1628. auto& sizes = type.typeSizes;
  1629. sizes.native_size = layout->nativeSize;
  1630. if (typeDef->genericContainerIndex == kGenericContainerIndexInvalid)
  1631. {
  1632. sizes.static_fields_size = layout->staticFieldsSize;
  1633. sizes.thread_static_fields_size = layout->threadStaticFieldsSize;
  1634. }
  1635. else
  1636. {
  1637. sizes.static_fields_size = 0;
  1638. sizes.thread_static_fields_size = 0;
  1639. }
  1640. if (sizes.instance_size == 0)
  1641. {
  1642. sizes.instance_size = layout->instanceSize;
  1643. }
  1644. int32_t fieldStart = DecodeMetadataIndex(typeDef->fieldStart);
  1645. for (int32_t i = 0, end = typeDef->field_count; i < end ; i++)
  1646. {
  1647. FieldDetail& fd = _fieldDetails[fieldStart + i];
  1648. FieldLayout& fieldLayout = layout->fields[i];
  1649. if (fd.offset == 0)
  1650. {
  1651. fd.offset = fieldLayout.offset;
  1652. }
  1653. else if (fd.offset == THREAD_LOCAL_STATIC_MASK)
  1654. {
  1655. fd.offset = fieldLayout.offset;
  1656. }
  1657. else
  1658. {
  1659. IL2CPP_ASSERT(fd.offset == fieldLayout.offset);
  1660. int a = 0;
  1661. }
  1662. }
  1663. }
  1664. }
  1665. uint32_t InterpreterImage::AddIl2CppTypeCache(const Il2CppType* type)
  1666. {
  1667. auto it = _type2Indexs.find(type);
  1668. if (it != _type2Indexs.end())
  1669. {
  1670. return it->second;
  1671. }
  1672. uint32_t encodeIndex = EncodeWithIndex((uint32_t)_types.size());
  1673. _types.push_back(type);
  1674. _type2Indexs.insert({ type, encodeIndex });
  1675. return encodeIndex;
  1676. }
  1677. uint32_t InterpreterImage::AddIl2CppGenericContainers(Il2CppGenericContainer& geneContainer)
  1678. {
  1679. uint32_t index = (uint32_t)_genericContainers.size();
  1680. _genericContainers.push_back(geneContainer);
  1681. return EncodeWithIndex(index);
  1682. }
  1683. void InterpreterImage::InitClass()
  1684. {
  1685. const Table& typeDefTb = _rawImage->GetTable(TableType::TYPEDEF);
  1686. _classList.resize(typeDefTb.rowNum);
  1687. }
  1688. Il2CppClass* InterpreterImage::GetTypeInfoFromTypeDefinitionRawIndex(uint32_t index)
  1689. {
  1690. IL2CPP_ASSERT(index < _classList.size());
  1691. Il2CppClass* klass = _classList[index];
  1692. if (klass)
  1693. {
  1694. return klass;
  1695. }
  1696. il2cpp::os::FastAutoLock lock(&il2cpp::vm::g_MetadataLock);
  1697. klass = _classList[index];
  1698. if (klass)
  1699. {
  1700. return klass;
  1701. }
  1702. klass = il2cpp::vm::GlobalMetadata::FromTypeDefinition(EncodeWithIndex(index));
  1703. IL2CPP_ASSERT(klass->interfaces_count <= klass->interface_offsets_count || _typesDefines[index].interfaceOffsetsStart == 0);
  1704. il2cpp::os::Atomic::FullMemoryBarrier();
  1705. _classList[index] = klass;
  1706. return klass;
  1707. }
  1708. const Il2CppType* InterpreterImage::GetInterfaceFromGlobalOffset(TypeInterfaceIndex globalOffset)
  1709. {
  1710. IL2CPP_ASSERT((uint32_t)globalOffset < (uint32_t)_interfaceDefines.size());
  1711. TypeIndex typeIndex = _interfaceDefines[globalOffset];
  1712. if (typeIndex == kTypeIndexInvalid)
  1713. {
  1714. uint32_t rowIndex = globalOffset + 1;
  1715. TbInterfaceImpl data = _rawImage->ReadInterfaceImpl(rowIndex);
  1716. Il2CppTypeDefinition& typeDef = _typesDefines[data.classIdx - 1];
  1717. const Il2CppType* intType = ReadTypeFromToken(GetGenericContainerByTypeDefinition(&typeDef), nullptr,
  1718. DecodeTypeDefOrRefOrSpecCodedIndexTableType(data.interfaceIdx), DecodeTypeDefOrRefOrSpecCodedIndexRowIndex(data.interfaceIdx));
  1719. _interfaceDefines[globalOffset] = typeIndex = DecodeMetadataIndex(AddIl2CppTypeCache(intType));
  1720. }
  1721. return _types[typeIndex];
  1722. }
  1723. const Il2CppType* InterpreterImage::GetInterfaceFromIndex(const Il2CppClass* klass, TypeInterfaceIndex globalOffset)
  1724. {
  1725. return GetInterfaceFromGlobalOffset(globalOffset);
  1726. }
  1727. const Il2CppType* InterpreterImage::GetInterfaceFromOffset(const Il2CppClass* klass, TypeInterfaceIndex offset)
  1728. {
  1729. const Il2CppTypeDefinition* typeDef = (const Il2CppTypeDefinition*)(klass->typeMetadataHandle);
  1730. IL2CPP_ASSERT(typeDef);
  1731. return GetInterfaceFromOffset(typeDef, offset);
  1732. }
  1733. const Il2CppType* InterpreterImage::GetInterfaceFromOffset(const Il2CppTypeDefinition* typeDef, TypeInterfaceIndex offset)
  1734. {
  1735. uint32_t globalOffset = typeDef->interfacesStart + offset;
  1736. return GetInterfaceFromGlobalOffset(globalOffset);
  1737. }
  1738. Il2CppInterfaceOffsetInfo InterpreterImage::GetInterfaceOffsetInfo(const Il2CppTypeDefinition* typeDefine, TypeInterfaceOffsetIndex index)
  1739. {
  1740. uint32_t globalIndex = DecodeMetadataIndex((uint32_t)(typeDefine->interfaceOffsetsStart + index));
  1741. IL2CPP_ASSERT(globalIndex < (uint32_t)_interfaceOffsets.size());
  1742. InterfaceOffsetInfo& offsetPair = _interfaceOffsets[globalIndex];
  1743. return { offsetPair.type, (int32_t)offsetPair.offset };
  1744. }
  1745. Il2CppClass* InterpreterImage::GetNestedTypeFromOffset(const Il2CppTypeDefinition* typeDefine, TypeNestedTypeIndex offset)
  1746. {
  1747. uint32_t globalIndex = typeDefine->nestedTypesStart + offset;
  1748. IL2CPP_ASSERT(globalIndex < (uint32_t)_nestedTypeDefineIndexs.size());
  1749. uint32_t typeDefIndex = _nestedTypeDefineIndexs[globalIndex];
  1750. IL2CPP_ASSERT(typeDefIndex < (uint32_t)_typesDefines.size());
  1751. return il2cpp::vm::GlobalMetadata::GetTypeInfoFromHandle((Il2CppMetadataTypeHandle)&_typesDefines[typeDefIndex]);
  1752. }
  1753. Il2CppClass* InterpreterImage::GetNestedTypeFromOffset(const Il2CppClass* klass, TypeNestedTypeIndex offset)
  1754. {
  1755. return GetNestedTypeFromOffset((Il2CppTypeDefinition*)klass->typeMetadataHandle, offset);
  1756. }
  1757. Il2CppTypeDefinition* InterpreterImage::GetNestedTypes(Il2CppTypeDefinition* typeDefinition, void** iter)
  1758. {
  1759. if (_nestedTypeDefineIndexs.empty())
  1760. {
  1761. return nullptr;
  1762. }
  1763. const TypeDefinitionIndex* nestedTypeIndices = (const TypeDefinitionIndex*)(&_nestedTypeDefineIndexs[typeDefinition->nestedTypesStart]);
  1764. if (!*iter)
  1765. {
  1766. if (typeDefinition->nested_type_count == 0)
  1767. return NULL;
  1768. *iter = (void*)(nestedTypeIndices);
  1769. return &_typesDefines[nestedTypeIndices[0]];
  1770. }
  1771. TypeDefinitionIndex* nestedTypeAddress = (TypeDefinitionIndex*)*iter;
  1772. nestedTypeAddress++;
  1773. ptrdiff_t index = nestedTypeAddress - nestedTypeIndices;
  1774. if (index < typeDefinition->nested_type_count)
  1775. {
  1776. *iter = nestedTypeAddress;
  1777. return &_typesDefines[*nestedTypeAddress];
  1778. }
  1779. return NULL;
  1780. }
  1781. const Il2CppAssembly* InterpreterImage::GetReferencedAssembly(int32_t referencedAssemblyTableIndex, const Il2CppAssembly assembliesTable[], int assembliesCount)
  1782. {
  1783. auto& table = _rawImage->GetTable(TableType::ASSEMBLYREF);
  1784. IL2CPP_ASSERT((uint32_t)referencedAssemblyTableIndex < table.rowNum);
  1785. TbAssemblyRef assRef = _rawImage->ReadAssemblyRef(referencedAssemblyTableIndex + 1);
  1786. const char* refAssName = _rawImage->GetStringFromRawIndex(assRef.name);
  1787. const Il2CppAssembly* il2cppAssRef = il2cpp::vm::Assembly::GetLoadedAssembly(refAssName);
  1788. if (!il2cppAssRef)
  1789. {
  1790. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetDllNotFoundException(refAssName));
  1791. }
  1792. return il2cppAssRef;
  1793. }
  1794. void InterpreterImage::ReadFieldRefInfoFromFieldDefToken(uint32_t rowIndex, FieldRefInfo& ret)
  1795. {
  1796. IL2CPP_ASSERT(rowIndex > 0);
  1797. const FieldDetail& fd = GetFieldDetailFromRawIndex(rowIndex - 1);
  1798. ret.containerType = GetIl2CppTypeFromRawTypeDefIndex(DecodeMetadataIndex(fd.typeDefIndex));
  1799. ret.field = &fd.fieldDef;
  1800. }
  1801. void InterpreterImage::GetClassAndMethodGenericContainerFromGenericContainerIndex(GenericContainerIndex idx, const Il2CppGenericContainer*& klassGc, const Il2CppGenericContainer*& methodGc)
  1802. {
  1803. Il2CppGenericContainer* gc = GetGenericContainerByRawIndex(DecodeMetadataIndex(idx));
  1804. IL2CPP_ASSERT(gc);
  1805. if (gc->is_method)
  1806. {
  1807. const Il2CppMethodDefinition* methodDef = GetMethodDefinitionFromRawIndex(DecodeMetadataIndex(gc->ownerIndex));
  1808. klassGc = GetGenericContainerByTypeDefRawIndex(DecodeMetadataIndex(methodDef->declaringType));
  1809. methodGc = GetGenericContainerByRawIndex(DecodeMetadataIndex(methodDef->genericContainerIndex));
  1810. }
  1811. else
  1812. {
  1813. klassGc = gc;
  1814. methodGc = nullptr;
  1815. }
  1816. }
  1817. void InterpreterImage::InitGenericParamConstraintDefs()
  1818. {
  1819. const Table& tb = _rawImage->GetTable(TableType::GENERICPARAMCONSTRAINT);
  1820. _genericConstraints.resize(tb.rowNum, kTypeIndexInvalid);
  1821. for (uint32_t i = 0; i < tb.rowNum; i++)
  1822. {
  1823. uint32_t rowIndex = i + 1;
  1824. TbGenericParamConstraint data = _rawImage->ReadGenericParamConstraint(rowIndex);
  1825. Il2CppGenericParameter& genericParam = _genericParams[data.owner - 1];
  1826. if (genericParam.constraintsCount == 0)
  1827. {
  1828. genericParam.constraintsStart = EncodeWithIndex(i);
  1829. }
  1830. ++genericParam.constraintsCount;
  1831. //_genericConstraints[i] == kTypeIndexInvalid;
  1832. //Il2CppType paramCons = {};
  1833. //const Il2CppGenericContainer* klassGc;
  1834. //const Il2CppGenericContainer* methodGc;
  1835. //GetClassAndMethodGenericContainerFromGenericContainerIndex(genericParam.ownerIndex, klassGc, methodGc);
  1836. //ReadTypeFromToken(klassGc, methodGc, DecodeTypeDefOrRefOrSpecCodedIndexTableType(data.constraint), DecodeTypeDefOrRefOrSpecCodedIndexRowIndex(data.constraint), paramCons);
  1837. //_genericConstraints[i] = DecodeMetadataIndex(AddIl2CppTypeCache(paramCons));
  1838. }
  1839. }
  1840. void InterpreterImage::InitGenericParamDefs0()
  1841. {
  1842. const Table& tb = _rawImage->GetTable(TableType::GENERICPARAM);
  1843. _genericParams.resize(tb.rowNum);
  1844. }
  1845. void InterpreterImage::InitGenericParamDefs()
  1846. {
  1847. const Table& tb = _rawImage->GetTable(TableType::GENERICPARAM);
  1848. for (uint32_t i = 0; i < tb.rowNum; i++)
  1849. {
  1850. uint32_t rowIndex = i + 1;
  1851. TbGenericParam data = _rawImage->ReadGenericParam(rowIndex);
  1852. Il2CppGenericParameter& paramDef = _genericParams[i];
  1853. paramDef.num = data.number;
  1854. paramDef.flags = data.flags;
  1855. paramDef.nameIndex = EncodeWithIndex(data.name);
  1856. // constraintsStart 和 constrantsCount init at InitGenericParamConstrains() latter
  1857. TableType ownerType = DecodeTypeOrMethodDefCodedIndexTableType(data.owner);
  1858. uint32_t ownerIndex = DecodeTypeOrMethodDefCodedIndexRowIndex(data.owner);
  1859. IL2CPP_ASSERT(ownerIndex > 0);
  1860. Il2CppGenericContainer* geneContainer;
  1861. int32_t interIndex = ownerIndex - 1;
  1862. if (ownerType == TableType::TYPEDEF)
  1863. {
  1864. Il2CppTypeDefinition& typeDef = _typesDefines[interIndex];
  1865. if (typeDef.genericContainerIndex == kGenericContainerIndexInvalid)
  1866. {
  1867. Il2CppGenericContainer c = {};
  1868. c.ownerIndex = EncodeWithIndex(interIndex);
  1869. c.is_method = false;
  1870. typeDef.genericContainerIndex = AddIl2CppGenericContainers(c);
  1871. }
  1872. geneContainer = &_genericContainers[DecodeMetadataIndex(typeDef.genericContainerIndex)];
  1873. paramDef.ownerIndex = typeDef.genericContainerIndex;
  1874. }
  1875. else
  1876. {
  1877. Il2CppMethodDefinition& methodDef = _methodDefines[interIndex];
  1878. if (methodDef.genericContainerIndex == kGenericContainerIndexInvalid)
  1879. {
  1880. Il2CppGenericContainer c = {};
  1881. c.ownerIndex = EncodeWithIndex(interIndex);
  1882. c.is_method = true;
  1883. methodDef.genericContainerIndex = AddIl2CppGenericContainers(c);
  1884. }
  1885. geneContainer = &_genericContainers[DecodeMetadataIndex(methodDef.genericContainerIndex)];
  1886. paramDef.ownerIndex = methodDef.genericContainerIndex;
  1887. }
  1888. if (geneContainer->type_argc == 0)
  1889. {
  1890. geneContainer->genericParameterStart = EncodeWithIndex(i);
  1891. }
  1892. ++geneContainer->type_argc;
  1893. }
  1894. }
  1895. void InterpreterImage::InitInterfaces()
  1896. {
  1897. const Table& table = _rawImage->GetTable(TableType::INTERFACEIMPL);
  1898. // interface中只包含直接继承的interface,不包括来自父类的
  1899. // 此interface只在CastClass及Type.GetInterfaces()反射函数中
  1900. // 发挥作用,不在callvir中发挥作用。
  1901. // interfaceOffsets中包含了水平展开的所有interface(包括父类的)
  1902. _interfaceDefines.resize(table.rowNum, kTypeIndexInvalid);
  1903. uint32_t lastClassIdx = 0;
  1904. for (uint32_t i = 0; i < table.rowNum; i++)
  1905. {
  1906. uint32_t rowIndex = i + 1;
  1907. TbInterfaceImpl data = _rawImage->ReadInterfaceImpl(rowIndex);
  1908. Il2CppTypeDefinition& typeDef = _typesDefines[data.classIdx - 1];
  1909. //Il2CppType intType = {};
  1910. //ReadTypeFromToken(GetGenericContainerByTypeDefinition(&typeDef), nullptr,
  1911. // DecodeTypeDefOrRefOrSpecCodedIndexTableType(data.interfaceIdx), DecodeTypeDefOrRefOrSpecCodedIndexRowIndex(data.interfaceIdx), intType);
  1912. //_interfaceDefines[i] = DecodeMetadataIndex(AddIl2CppTypeCache(intType));
  1913. if (typeDef.interfaces_count == 0)
  1914. {
  1915. typeDef.interfacesStart = (InterfacesIndex)i;
  1916. }
  1917. else
  1918. {
  1919. // 必须连续
  1920. IL2CPP_ASSERT(data.classIdx == lastClassIdx);
  1921. }
  1922. ++typeDef.interfaces_count;
  1923. lastClassIdx = data.classIdx;
  1924. }
  1925. }
  1926. void InterpreterImage::ComputeVTable(TypeDefinitionDetail* tdd)
  1927. {
  1928. Il2CppTypeDefinition& typeDef = *GetTypeDefinitionByTypeDetail(tdd);
  1929. if (IsInterface(typeDef.flags) || typeDef.interfaceOffsetsStart != 0)
  1930. {
  1931. return;
  1932. }
  1933. if (typeDef.parentIndex != kInvalidIndex)
  1934. {
  1935. const Il2CppType* parentType = il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(typeDef.parentIndex);
  1936. const Il2CppTypeDefinition* parentTypeDef = GetUnderlyingTypeDefinition(parentType);
  1937. if (IsInterpreterType(parentTypeDef) && parentTypeDef->interfaceOffsetsStart == 0)
  1938. {
  1939. IL2CPP_ASSERT(DecodeImageIndex(parentTypeDef->byvalTypeIndex) == this->GetIndex());
  1940. int32_t typeDefIndex = GetTypeRawIndexByEncodedIl2CppTypeIndex(parentTypeDef->byvalTypeIndex);
  1941. ComputeVTable(&_typeDetails[typeDefIndex]);
  1942. }
  1943. }
  1944. const Il2CppType* type = GetIl2CppTypeFromRawIndex(DecodeMetadataIndex(typeDef.byvalTypeIndex));
  1945. VTableSetUp* typeTree = VTableSetUp::BuildByType(_cacheTrees, type);
  1946. uint32_t offsetsStart = (uint32_t)_interfaceOffsets.size();
  1947. auto& vms = typeTree->GetVirtualMethodImpls();
  1948. if (vms.empty())
  1949. {
  1950. tdd->vtable = nullptr;
  1951. tdd->vtableCount = 0;
  1952. }
  1953. else
  1954. {
  1955. tdd->vtable = (VirtualMethodImpl*)HYBRIDCLR_METADATA_CALLOC(vms.size(), sizeof(VirtualMethodImpl));
  1956. tdd->vtableCount = (uint32_t)vms.size();
  1957. std::memcpy(tdd->vtable, &vms[0], vms.size() * sizeof(VirtualMethodImpl));
  1958. }
  1959. auto& interfaceOffsetInfos = typeTree->GetInterfaceOffsetInfos();
  1960. for (auto ioi : interfaceOffsetInfos)
  1961. {
  1962. _interfaceOffsets.push_back({ ioi.type, ioi.offset });
  1963. }
  1964. typeDef.vtableStart = EncodeWithIndex(0);
  1965. typeDef.vtable_count = (uint16_t)vms.size();
  1966. typeDef.interfaceOffsetsStart = EncodeWithIndex(offsetsStart);
  1967. typeDef.interface_offsets_count = (uint16_t)interfaceOffsetInfos.size();
  1968. Il2CppClass* klass = _classList[GetTypeRawIndex(&typeDef)];
  1969. IL2CPP_ASSERT(!klass);
  1970. }
  1971. void InterpreterImage::InitVTables()
  1972. {
  1973. const Table& typeDefTb = _rawImage->GetTable(TableType::TYPEDEF);
  1974. for (TypeDefinitionDetail& td : _typeDetails)
  1975. {
  1976. ComputeVTable(&td);
  1977. }
  1978. for (auto& e : _cacheTrees)
  1979. {
  1980. e.second->~VTableSetUp();
  1981. HYBRIDCLR_FREE(e.second);
  1982. }
  1983. Il2CppType2TypeDeclaringTreeMap temp;
  1984. _cacheTrees.swap(temp);
  1985. }
  1986. // index => MethodDefinition -> DeclaringClass -> index - klass->methodStart -> MethodInfo*
  1987. const MethodInfo* InterpreterImage::GetMethodInfoFromMethodDefinitionRawIndex(uint32_t index)
  1988. {
  1989. IL2CPP_ASSERT((size_t)index <= _methodDefines.size());
  1990. const Il2CppMethodDefinition* methodDefinition = GetMethodDefinitionFromRawIndex(index);
  1991. const Il2CppTypeDefinition* typeDefinition = (const Il2CppTypeDefinition*)il2cpp::vm::GlobalMetadata::GetTypeHandleFromIndex(methodDefinition->declaringType);
  1992. int32_t indexInClass = index - DecodeMetadataIndex(typeDefinition->methodStart);
  1993. IL2CPP_ASSERT(indexInClass >= 0 && indexInClass < typeDefinition->method_count);
  1994. Il2CppClass* klass = il2cpp::vm::GlobalMetadata::GetTypeInfoFromHandle((Il2CppMetadataTypeHandle)typeDefinition);
  1995. il2cpp::vm::Class::SetupMethods(klass);
  1996. return klass->methods[indexInClass];
  1997. }
  1998. const MethodInfo* InterpreterImage::GetMethodInfoFromMethodDefinition(const Il2CppMethodDefinition* methodDef)
  1999. {
  2000. uint32_t rawIndex = (uint32_t)(methodDef - &_methodDefines[0]);
  2001. IL2CPP_ASSERT(rawIndex < (uint32_t)_methodDefines.size());
  2002. return GetMethodInfoFromMethodDefinitionRawIndex(rawIndex);
  2003. }
  2004. // typeDef vTableSlot -> type virtual method index -> MethodDefinition*
  2005. const Il2CppMethodDefinition* InterpreterImage::GetMethodDefinitionFromVTableSlot(const Il2CppTypeDefinition* typeDef, int32_t vTableSlot)
  2006. {
  2007. uint32_t typeDefIndex = GetTypeRawIndex(typeDef);
  2008. IL2CPP_ASSERT(typeDefIndex < (uint32_t)_typeDetails.size());
  2009. TypeDefinitionDetail& td = _typeDetails[typeDefIndex];
  2010. IL2CPP_ASSERT(vTableSlot >= 0 && vTableSlot < (int32_t)td.vtableCount);
  2011. VirtualMethodImpl& vmi = td.vtable[vTableSlot];
  2012. return vmi.method;
  2013. }
  2014. const MethodInfo* InterpreterImage::GetMethodInfoFromVTableSlot(const Il2CppClass* klass, int32_t vTableSlot)
  2015. {
  2016. IL2CPP_ASSERT(!klass->generic_class);
  2017. const Il2CppTypeDefinition* typeDef = (Il2CppTypeDefinition*)klass->typeMetadataHandle;
  2018. //const Il2CppMethodDefinition* methodDef = GetMethodDefinitionFromVTableSlot((Il2CppTypeDefinition*)klass->typeMetadataHandle, vTableSlot);
  2019. // FIX ME. why return null?
  2020. //IL2CPP_ASSERT(methodDef);
  2021. uint32_t typeDefIndex = GetTypeRawIndex(typeDef);
  2022. IL2CPP_ASSERT(typeDefIndex < (uint32_t)_typeDetails.size());
  2023. TypeDefinitionDetail& td = _typeDetails[typeDefIndex];
  2024. IL2CPP_ASSERT(vTableSlot >= 0 && vTableSlot < (int32_t)td.vtableCount);
  2025. VirtualMethodImpl& vmi = td.vtable[vTableSlot];
  2026. if (vmi.method)
  2027. {
  2028. if (vmi.method->declaringType == EncodeWithIndex(typeDefIndex))
  2029. {
  2030. return il2cpp::vm::GlobalMetadata::GetMethodInfoFromMethodHandle((Il2CppMetadataMethodDefinitionHandle)vmi.method);
  2031. }
  2032. else
  2033. {
  2034. Il2CppClass* implClass = il2cpp::vm::Class::FromIl2CppType(vmi.type);
  2035. IL2CPP_ASSERT(implClass != klass);
  2036. il2cpp::vm::Class::SetupMethods(implClass);
  2037. for (uint32_t i = 0; i < implClass->method_count; i++)
  2038. {
  2039. const MethodInfo* implMethod = implClass->methods[i];
  2040. if (implMethod->token == vmi.method->token)
  2041. {
  2042. return implMethod;
  2043. }
  2044. }
  2045. RaiseExecutionEngineException("not find vtable method");
  2046. }
  2047. }
  2048. return nullptr;
  2049. }
  2050. Il2CppMethodPointer InterpreterImage::GetAdjustorThunk(uint32_t token)
  2051. {
  2052. uint32_t methodIndex = DecodeTokenRowIndex(token) - 1;
  2053. IL2CPP_ASSERT(methodIndex < (uint32_t)_methodDefines.size());
  2054. const Il2CppMethodDefinition* methodDef = &_methodDefines[methodIndex];
  2055. return IsInstanceMethod(methodDef) ? hybridclr::interpreter::InterpreterModule::GetAdjustThunkMethodPointer(methodDef) : nullptr;
  2056. }
  2057. Il2CppMethodPointer InterpreterImage::GetMethodPointer(uint32_t token)
  2058. {
  2059. uint32_t methodIndex = DecodeTokenRowIndex(token) - 1;
  2060. IL2CPP_ASSERT(methodIndex < (uint32_t)_methodDefines.size());
  2061. const Il2CppMethodDefinition* methodDef = &_methodDefines[methodIndex];
  2062. return hybridclr::interpreter::InterpreterModule::GetMethodPointer(methodDef);
  2063. }
  2064. InvokerMethod InterpreterImage::GetMethodInvoker(uint32_t token)
  2065. {
  2066. uint32_t methodIndex = DecodeTokenRowIndex(token) - 1;
  2067. IL2CPP_ASSERT(methodIndex < (uint32_t)_methodDefines.size());
  2068. const Il2CppMethodDefinition* methodDef = &_methodDefines[methodIndex];
  2069. return hybridclr::interpreter::InterpreterModule::GetMethodInvoker(methodDef);
  2070. }
  2071. Il2CppString* InterpreterImage::ReadSerString(BlobReader& reader)
  2072. {
  2073. byte b = reader.PeekByte();
  2074. if (b == 0xFF)
  2075. {
  2076. reader.SkipByte();
  2077. return nullptr;
  2078. }
  2079. else if (b == 0)
  2080. {
  2081. reader.SkipByte();
  2082. return il2cpp::vm::String::Empty();
  2083. }
  2084. else
  2085. {
  2086. uint32_t len = reader.ReadCompressedUint32();
  2087. #if !HYBRIDCLR_UNITY_2021_OR_NEW
  2088. return il2cpp::vm::String::NewLen((char*)reader.GetAndSkipCurBytes(len), len);
  2089. #else
  2090. char* chars = (char*)reader.GetDataOfReadPosition();
  2091. reader.SkipBytes(len);
  2092. return il2cpp::vm::String::NewLen(chars, len);
  2093. #endif
  2094. }
  2095. }
  2096. #if HYBRIDCLR_UNITY_2021_OR_NEW
  2097. bool InterpreterImage::ReadUTF8SerString(BlobReader& reader, std::string& s)
  2098. {
  2099. byte b = reader.PeekByte();
  2100. if (b == 0xFF)
  2101. {
  2102. reader.SkipByte();
  2103. return false;
  2104. }
  2105. else if (b == 0)
  2106. {
  2107. reader.SkipByte();
  2108. s.clear();
  2109. return true;
  2110. }
  2111. else
  2112. {
  2113. uint32_t len = reader.ReadCompressedUint32();
  2114. char* chars = (char*)reader.GetDataOfReadPosition();
  2115. reader.SkipBytes(len);
  2116. s.assign(chars, len);
  2117. return true;
  2118. }
  2119. }
  2120. #endif
  2121. Il2CppReflectionType* InterpreterImage::ReadSystemType(BlobReader& reader)
  2122. {
  2123. Il2CppString* fullName = ReadSerString(reader);
  2124. if (!fullName)
  2125. {
  2126. return nullptr;
  2127. }
  2128. Il2CppReflectionType* type = GetReflectionTypeFromName(fullName);
  2129. if (!type)
  2130. {
  2131. std::string stdTypeName = il2cpp::utils::StringUtils::Utf16ToUtf8(fullName->chars);
  2132. TEMP_FORMAT(errMsg, "CustomAttribute fixed arg type:System.Type fullName:'%s' not find", stdTypeName.c_str());
  2133. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetTypeLoadException(errMsg));
  2134. }
  2135. return type;
  2136. }
  2137. Il2CppObject* InterpreterImage::ReadBoxedValue(BlobReader& reader)
  2138. {
  2139. uint64_t obj = 0;
  2140. Il2CppType kind = {};
  2141. ReadCustomAttributeFieldOrPropType(reader, kind);
  2142. ReadFixedArg(reader, &kind, &obj);
  2143. Il2CppClass* valueType = il2cpp::vm::Class::FromIl2CppType(&kind);
  2144. return il2cpp::vm::Object::Box(valueType, &obj);
  2145. }
  2146. void InterpreterImage::ReadFixedArg(BlobReader& reader, const Il2CppType* argType, void* data)
  2147. {
  2148. switch (argType->type)
  2149. {
  2150. case IL2CPP_TYPE_BOOLEAN:
  2151. {
  2152. *(byte*)data = reader.ReadByte();
  2153. break;
  2154. }
  2155. case IL2CPP_TYPE_CHAR:
  2156. {
  2157. *(uint16_t*)data = reader.Read16();
  2158. break;
  2159. }
  2160. case IL2CPP_TYPE_I1:
  2161. case IL2CPP_TYPE_U1:
  2162. {
  2163. *(byte*)data = reader.ReadByte();
  2164. break;
  2165. }
  2166. case IL2CPP_TYPE_I2:
  2167. case IL2CPP_TYPE_U2:
  2168. {
  2169. *(uint16_t*)data = reader.Read16();
  2170. break;
  2171. }
  2172. case IL2CPP_TYPE_I4:
  2173. case IL2CPP_TYPE_U4:
  2174. {
  2175. *(uint32_t*)data = reader.Read32();
  2176. break;
  2177. }
  2178. case IL2CPP_TYPE_I8:
  2179. case IL2CPP_TYPE_U8:
  2180. {
  2181. *(uint64_t*)data = reader.Read64();
  2182. break;
  2183. }
  2184. case IL2CPP_TYPE_R4:
  2185. {
  2186. *(float*)data = reader.ReadFloat();
  2187. break;
  2188. }
  2189. case IL2CPP_TYPE_R8:
  2190. {
  2191. *(double*)data = reader.ReadDouble();
  2192. break;
  2193. }
  2194. case IL2CPP_TYPE_SZARRAY:
  2195. {
  2196. uint32_t numElem = reader.Read32();
  2197. if (numElem != (uint32_t)-1)
  2198. {
  2199. Il2CppClass* arrKlass = il2cpp::vm::Class::FromIl2CppType(argType);
  2200. Il2CppArray* arr = il2cpp::vm::Array::New(il2cpp::vm::Class::GetElementClass(arrKlass), numElem);
  2201. for (uint16_t i = 0; i < numElem; i++)
  2202. {
  2203. ReadFixedArg(reader, argType->data.type, GET_ARRAY_ELEMENT_ADDRESS(arr, i, arr->klass->element_size));
  2204. }
  2205. *(void**)data = arr;
  2206. }
  2207. else
  2208. {
  2209. *(void**)data = nullptr;
  2210. }
  2211. HYBRIDCLR_SET_WRITE_BARRIER((void**)data);
  2212. break;
  2213. }
  2214. case IL2CPP_TYPE_STRING:
  2215. {
  2216. *(Il2CppString**)data = ReadSerString(reader);
  2217. HYBRIDCLR_SET_WRITE_BARRIER((void**)data);
  2218. break;
  2219. }
  2220. case IL2CPP_TYPE_OBJECT:
  2221. {
  2222. *(Il2CppObject**)data = ReadBoxedValue(reader);
  2223. HYBRIDCLR_SET_WRITE_BARRIER((void**)data);
  2224. break;
  2225. }
  2226. case IL2CPP_TYPE_CLASS:
  2227. {
  2228. Il2CppClass* klass = il2cpp::vm::Class::FromIl2CppType(argType);
  2229. if (!klass)
  2230. {
  2231. RaiseExecutionEngineException("type not find");
  2232. }
  2233. if (klass == il2cpp_defaults.object_class)
  2234. {
  2235. *(Il2CppObject**)data = ReadBoxedValue(reader);
  2236. }
  2237. else if (klass == il2cpp_defaults.systemtype_class)
  2238. {
  2239. *(Il2CppReflectionType**)data = ReadSystemType(reader);
  2240. }
  2241. else
  2242. {
  2243. TEMP_FORMAT(errMsg, "fixed arg type:%s.%s not support", klass->namespaze, klass->name);
  2244. RaiseNotSupportedException(errMsg);
  2245. }
  2246. HYBRIDCLR_SET_WRITE_BARRIER((void**)data);
  2247. break;
  2248. }
  2249. case IL2CPP_TYPE_VALUETYPE:
  2250. {
  2251. Il2CppClass* valueType = il2cpp::vm::Class::FromIl2CppType(argType);
  2252. IL2CPP_ASSERT(valueType->enumtype);
  2253. ReadFixedArg(reader, &valueType->element_class->byval_arg, data);
  2254. break;
  2255. }
  2256. case IL2CPP_TYPE_SYSTEM_TYPE:
  2257. {
  2258. *(Il2CppReflectionType**)data = ReadSystemType(reader);
  2259. HYBRIDCLR_SET_WRITE_BARRIER((void**)data);
  2260. break;
  2261. }
  2262. case IL2CPP_TYPE_BOXED_OBJECT:
  2263. {
  2264. uint8_t fieldOrPropType = reader.ReadByte();
  2265. IL2CPP_ASSERT(fieldOrPropType == 0x51);
  2266. *(Il2CppObject**)data = ReadBoxedValue(reader);
  2267. HYBRIDCLR_SET_WRITE_BARRIER((void**)data);
  2268. break;
  2269. }
  2270. case IL2CPP_TYPE_ENUM:
  2271. {
  2272. Il2CppClass* valueType = il2cpp::vm::Class::FromIl2CppType(argType);
  2273. IL2CPP_ASSERT(valueType->enumtype);
  2274. ReadFixedArg(reader, &valueType->element_class->byval_arg, data);
  2275. break;
  2276. }
  2277. default:
  2278. {
  2279. RaiseExecutionEngineException("not support fixed argument type");
  2280. }
  2281. }
  2282. }
  2283. void InterpreterImage::ReadCustomAttributeFieldOrPropType(BlobReader& reader, Il2CppType& type)
  2284. {
  2285. type.type = (Il2CppTypeEnum)reader.ReadByte();
  2286. switch (type.type)
  2287. {
  2288. case IL2CPP_TYPE_BOOLEAN:
  2289. case IL2CPP_TYPE_CHAR:
  2290. case IL2CPP_TYPE_I1:
  2291. case IL2CPP_TYPE_U1:
  2292. case IL2CPP_TYPE_I2:
  2293. case IL2CPP_TYPE_U2:
  2294. case IL2CPP_TYPE_I4:
  2295. case IL2CPP_TYPE_U4:
  2296. case IL2CPP_TYPE_I8:
  2297. case IL2CPP_TYPE_U8:
  2298. case IL2CPP_TYPE_R4:
  2299. case IL2CPP_TYPE_R8:
  2300. case IL2CPP_TYPE_STRING:
  2301. {
  2302. break;
  2303. }
  2304. case IL2CPP_TYPE_SZARRAY:
  2305. {
  2306. Il2CppType eleType = {};
  2307. ReadCustomAttributeFieldOrPropType(reader, eleType);
  2308. type.data.type = MetadataPool::GetPooledIl2CppType(eleType);
  2309. break;
  2310. }
  2311. case IL2CPP_TYPE_ENUM:
  2312. {
  2313. Il2CppString* enumTypeName = ReadSerString(reader);
  2314. Il2CppReflectionType* enumType = GetReflectionTypeFromName(enumTypeName);
  2315. if (!enumType)
  2316. {
  2317. std::string stdStrName = il2cpp::utils::StringUtils::Utf16ToUtf8(enumTypeName->chars);
  2318. TEMP_FORMAT(errMsg, "ReadCustomAttributeFieldOrPropType enum:'%s' not exists", stdStrName.c_str());
  2319. RaiseExecutionEngineException(errMsg);
  2320. }
  2321. type = *enumType->type;
  2322. break;
  2323. }
  2324. case IL2CPP_TYPE_SYSTEM_TYPE:
  2325. {
  2326. type = il2cpp_defaults.systemtype_class->byval_arg;
  2327. break;
  2328. }
  2329. case IL2CPP_TYPE_BOXED_OBJECT:
  2330. {
  2331. type = il2cpp_defaults.object_class->byval_arg;
  2332. break;
  2333. }
  2334. default:
  2335. {
  2336. TEMP_FORMAT(errMsg, "ReadCustomAttributeFieldOrPropType. image:%s unknown type:%d", GetIl2CppImage()->name, (int)type.type);
  2337. RaiseBadImageException(errMsg);
  2338. }
  2339. }
  2340. }
  2341. void InterpreterImage::ReadMethodDefSig(BlobReader& reader, const Il2CppGenericContainer* klassGenericContainer, const Il2CppGenericContainer* methodGenericContainer, Il2CppMethodDefinition& methodDef, std::vector<ParamDetail>& paramArr)
  2342. {
  2343. uint8_t rawSigFlags = reader.ReadByte();
  2344. if (rawSigFlags & (uint8_t)MethodSigFlags::GENERIC)
  2345. {
  2346. //IL2CPP_ASSERT(false);
  2347. uint32_t genParamCount = reader.ReadCompressedUint32();
  2348. Il2CppGenericContainer* gc = GetGenericContainerByRawIndex(DecodeMetadataIndex(methodDef.genericContainerIndex));
  2349. IL2CPP_ASSERT(gc->type_argc == genParamCount);
  2350. }
  2351. uint32_t paramCount = reader.ReadCompressedUint32();
  2352. //IL2CPP_ASSERT(paramCount >= methodDef.parameterCount);
  2353. const Il2CppType* returnType = ReadType(reader, klassGenericContainer, methodGenericContainer);
  2354. methodDef.returnType = AddIl2CppTypeCache(returnType);
  2355. int readParamNum = 0;
  2356. for (; reader.NonEmpty(); )
  2357. {
  2358. ParamDetail curParam = {};
  2359. const Il2CppType* type = ReadType(reader, klassGenericContainer, methodGenericContainer);
  2360. curParam.parameterIndex = readParamNum++;
  2361. curParam.paramDef.typeIndex = AddIl2CppTypeCache(type);
  2362. paramArr.push_back(curParam);
  2363. }
  2364. IL2CPP_ASSERT(readParamNum == (int)paramCount);
  2365. }
  2366. const Il2CppType* InterpreterImage::GetModuleIl2CppType(uint32_t moduleRowIndex, uint32_t typeNamespace, uint32_t typeName, bool raiseExceptionIfNotFound)
  2367. {
  2368. IL2CPP_ASSERT(moduleRowIndex == 1);
  2369. uint32_t encodedNamespaceIndex = EncodeWithIndex(typeNamespace);
  2370. uint32_t encodedNameIndex = EncodeWithIndex(typeName);
  2371. for (TypeDefinitionDetail& type : _typeDetails)
  2372. {
  2373. Il2CppTypeDefinition* typeDef = GetTypeDefinitionByTypeDetail(&type);
  2374. if (typeDef->namespaceIndex == encodedNamespaceIndex && typeDef->nameIndex == encodedNameIndex)
  2375. {
  2376. return GetIl2CppTypeFromTypeDefinition(typeDef);
  2377. }
  2378. }
  2379. if (!raiseExceptionIfNotFound)
  2380. {
  2381. return nullptr;
  2382. }
  2383. const char* typeNameStr = _rawImage->GetStringFromRawIndex(typeName);
  2384. const char* typeNamespaceStr = _rawImage->GetStringFromRawIndex(typeNamespace);
  2385. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetTypeLoadException(
  2386. CStringToStringView(typeNamespaceStr),
  2387. CStringToStringView(typeNameStr),
  2388. CStringToStringView(_il2cppImage->nameNoExt)));
  2389. return nullptr;
  2390. }
  2391. }
  2392. }