CustomAttributeDataReader.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #include "CustomAttributeDataReader.h"
  2. #include "il2cpp-metadata.h"
  3. #include "gc/WriteBarrier.h"
  4. #include "utils/MemoryRead.h"
  5. #include "vm-utils/BlobReader.h"
  6. #include "vm/Class.h"
  7. #include "vm/Exception.h"
  8. #include "vm/GlobalMetadata.h"
  9. #include "vm/MetadataCache.h"
  10. // Custom attribute metadata format
  11. //
  12. // Custom attribute data is tightly packed and is not stored aligned
  13. // it must be read with the helpers in MemoryRead
  14. //
  15. // Header:
  16. // 1 Compressed uint32: Count of attributes types
  17. // n uint32: Attribute constructor indexes
  18. //
  19. // Argument data immediate follows the header
  20. // There is no size data stored for arguments they must be serialized
  21. // out as they are read. This relies the writing code exactly matching
  22. // the reading code. Or else data will be read at the wrong offsets.
  23. //
  24. // Argument data
  25. // 1 Compressed uint32: Count of constructor arguments
  26. // n Blob data, variable sized: Argument data
  27. // 1 Compressed uint32: Count of field arguments
  28. // n Blob data, variable sized: Field argument data
  29. // Each field data ends with a compressed int32 of the field index on the type,
  30. // If the field index is negative, a compressed uint32_t with the declaring type index follows
  31. // 1 Compressed uint32: Count of property arguments
  32. // n Blob data, variable sized: Property argument data
  33. // Each property data ends with a compressed int32 of the property index on the type
  34. // If the property index is negative, a compressed uint32_t with the declaring type index follows
  35. // An example format is:
  36. //
  37. // 0x02 - Count of custom attribute constructors (compressed uint32_t)
  38. // 0x0010023f - Method definition index for ctor1
  39. // 0x02001fc1 - Method definition index for ctor2
  40. // 0x02 - Constructor argument count for ctor1 (compressed uint32_t)
  41. // 0x01 - Property count for ctor1 (compressed uint32_t)
  42. // 0x04 (2) - argument 1 type code (compressed int32_t)
  43. // .... - argument 1 data
  44. // 0x00 - Field for ctor1 (compressed uint32_t)
  45. // 0x01 - Property count for ctor1 (compressed uint32_t)
  46. // .... - argument 1 data
  47. // 0x55 - property type code (enum) (compressed uint32_t)
  48. // 0x023F - type index for enum type (compressed uint32_t))
  49. // .... - property 1 data
  50. // 0x02 - Constructor argument count for ctor2 (compressed uint32_t)
  51. // 0x02 - Field argument count for ctor2 (compressed uint32_t)
  52. // 0x00 - Property count for ctor2
  53. // 0x03 - argument 1 type code (compressed uint32_t)
  54. // .... - argument 1 data
  55. // 0x04 - argument 2 type code (compressed uint32_t)
  56. // .... - argument 2 data
  57. // 0x04 - field 1 type code (compressed uint32_t)
  58. // .... - field 1 data
  59. // 0x02 (1) - field 1 field index (compressed int32_t)
  60. // 0x04 - field 2 type code (compressed uint32_t)
  61. // .... - field 2 data
  62. // 0x03 (-1) - field 2 field index (compressed int32_t)
  63. // 0x023E - field 2 declaring type index (compressed int32_t)
  64. // [Start of next custom attribute data]
  65. static void SetInvalidDataException(Il2CppException** exc)
  66. {
  67. il2cpp::gc::WriteBarrier::GenericStore(exc, il2cpp::vm::Exception::GetCustomAttributeFormatException("Binary format of the specified custom attribute was invalid."));
  68. }
  69. static bool ReadAttributeDataValue(const Il2CppImage* image, const char** buffer, il2cpp::metadata::CustomAttributeArgument* arg, Il2CppException** exc, bool deserializedManagedObjects)
  70. {
  71. const Il2CppTypeEnum type = il2cpp::utils::BlobReader::ReadEncodedTypeEnum(image, buffer, &arg->klass);
  72. if (!il2cpp::utils::BlobReader::GetConstantValueFromBlob(image, type, buffer, &arg->data, deserializedManagedObjects))
  73. {
  74. SetInvalidDataException(exc);
  75. return false;
  76. }
  77. if (deserializedManagedObjects && type == IL2CPP_TYPE_SZARRAY && arg->data.obj != NULL)
  78. {
  79. // For arrays get the actual array class, not just System.Array
  80. arg->klass = ((Il2CppArray*)arg->data.obj)->klass;
  81. }
  82. return true;
  83. }
  84. namespace il2cpp
  85. {
  86. namespace metadata
  87. {
  88. CustomAttributeDataReader::CustomAttributeDataReader(const Il2CppImage* image, const void* buffer, const void* bufferEnd) :
  89. image(image), bufferStart((const char*)buffer), bufferEnd((const char*)bufferEnd)
  90. {
  91. if (bufferStart != NULL)
  92. count = utils::ReadCompressedUInt32(&bufferStart);
  93. else
  94. count = 0;
  95. }
  96. // private, used by CustomAttributeDataReader::ReadCustomAttributeData(const MethodInfo* ctor, const void* dataStart, uint32_t dataLength, CustomAttributeData* data, Il2CppException** exc)
  97. CustomAttributeDataReader::CustomAttributeDataReader(const Il2CppImage* image, const char* dataStart, uint32_t dataLength) :
  98. image(image), bufferStart(dataStart), bufferEnd(dataStart + dataLength), count(0)
  99. {
  100. }
  101. uint32_t CustomAttributeDataReader::GetCount(const CustomAttributeFilter& filter) const
  102. {
  103. uint32_t count = 0;
  104. const char* ctorBuffer = bufferStart;
  105. const MethodInfo* ctor;
  106. while (IterateAttributeCtorsImpl(&ctor, &ctorBuffer))
  107. {
  108. if (filter(ctor))
  109. count++;
  110. }
  111. return count;
  112. }
  113. CustomAttributeCtorIterator CustomAttributeDataReader::GetCtorIterator() const
  114. {
  115. return CustomAttributeCtorIterator(bufferStart);
  116. }
  117. CustomAttributeCtorIterator CustomAttributeDataReader::GetCtorIterator(const CustomAttributeFilter& filter) const
  118. {
  119. return CustomAttributeCtorIterator(bufferStart, filter);
  120. }
  121. CustomAttributeDataIterator CustomAttributeDataReader::GetDataIterator() const
  122. {
  123. return CustomAttributeDataIterator(bufferStart, GetDataBufferStart());
  124. }
  125. CustomAttributeDataIterator CustomAttributeDataReader::GetDataIterator(const CustomAttributeFilter& filter) const
  126. {
  127. return CustomAttributeDataIterator(bufferStart, GetDataBufferStart(), filter);
  128. }
  129. const char* CustomAttributeDataReader::GetDataBufferStart() const
  130. {
  131. return (const char*)(((uint32_t*)bufferStart) + count);
  132. }
  133. bool CustomAttributeDataReader::IterateAttributeCtorsImpl(const MethodInfo** attributeCtor, const char** ctorBuffer) const
  134. {
  135. if (*ctorBuffer < GetDataBufferStart())
  136. {
  137. MethodIndex ctorIndex = utils::Read32(ctorBuffer);
  138. *attributeCtor = il2cpp::vm::MetadataCache::GetMethodInfoFromMethodDefinitionIndex(image, ctorIndex);
  139. return true;
  140. }
  141. *attributeCtor = NULL;
  142. return false;
  143. }
  144. bool CustomAttributeDataReader::IterateAttributeCtors(const MethodInfo** attributeCtor, CustomAttributeCtorIterator* iter) const
  145. {
  146. while (IterateAttributeCtorsImpl(attributeCtor, &iter->ctorBuffer))
  147. {
  148. if (iter->filter(*attributeCtor))
  149. return true;
  150. }
  151. return false;
  152. }
  153. bool CustomAttributeDataReader::ReadLazyCustomAttributeData(LazyCustomAttributeData* data, CustomAttributeDataIterator* iter, Il2CppException** exc) const
  154. {
  155. if (!IterateAttributeCtorsImpl(&data->ctor, &iter->ctorBuffer))
  156. return false;
  157. data->dataStart = (void*)iter->dataBuffer;
  158. if (!ReadPastCustomAttribute(data->ctor, iter, exc))
  159. return false;
  160. data->dataLength = (uint32_t)((char*)iter->dataBuffer - (char*)data->dataStart);
  161. return true;
  162. }
  163. bool CustomAttributeDataReader::VisitCustomAttributeData(const Il2CppImage* image, const MethodInfo* ctor, const void* dataStart, uint32_t dataLength, CustomAttributeReaderVisitor* visitor, Il2CppException** exc)
  164. {
  165. CustomAttributeDataReader reader = CustomAttributeDataReader(image, (const char*)dataStart, dataLength);
  166. CustomAttributeDataIterator iter = CustomAttributeDataIterator(NULL, reader.bufferStart);
  167. return reader.ReadAndVisitCustomAttributeData(ctor, &iter, visitor, exc);
  168. }
  169. bool CustomAttributeDataReader::VisitCustomAttributeData(CustomAttributeDataIterator* iter, CustomAttributeReaderVisitor* visitor, Il2CppException** exc) const
  170. {
  171. const MethodInfo* ctor;
  172. while (IterateAttributeCtorsImpl(&ctor, &iter->ctorBuffer))
  173. {
  174. bool shouldProcessThisAttr = iter->filter(ctor);
  175. if (shouldProcessThisAttr)
  176. return ReadAndVisitCustomAttributeData(ctor, iter, visitor, exc);
  177. if (!ReadPastCustomAttribute(ctor, iter, exc))
  178. return false;
  179. }
  180. return false;
  181. }
  182. static std::tuple<const Il2CppClass*, int32_t> ReadCustomAttributeNamedArgumentClassAndIndex(const char** dataBuffer, const Il2CppClass* attrClass)
  183. {
  184. int32_t memberIndex = utils::ReadCompressedInt32(dataBuffer);
  185. if (memberIndex >= 0)
  186. return std::make_tuple(attrClass, memberIndex);
  187. memberIndex = -(memberIndex + 1);
  188. TypeDefinitionIndex typeIndex = utils::ReadCompressedUInt32(dataBuffer);
  189. Il2CppClass* declaringClass = il2cpp::vm::GlobalMetadata::GetTypeInfoFromTypeDefinitionIndex(typeIndex);
  190. IL2CPP_ASSERT(declaringClass == attrClass || il2cpp::vm::Class::IsSubclassOf(const_cast<Il2CppClass*>(attrClass), declaringClass, false));
  191. return std::make_tuple(declaringClass, memberIndex);
  192. }
  193. bool CustomAttributeDataReader::ReadPastCustomAttribute(const MethodInfo* ctor, CustomAttributeDataIterator* iter, Il2CppException** exc) const
  194. {
  195. CustomAttributeReaderVisitor nullVisitor;
  196. return ReadAndVisitCustomAttributeImpl(ctor, iter, &nullVisitor, exc, false);
  197. }
  198. bool CustomAttributeDataReader::ReadAndVisitCustomAttributeData(const MethodInfo* ctor, CustomAttributeDataIterator* iter, CustomAttributeReaderVisitor* visitor, Il2CppException** exc) const
  199. {
  200. return ReadAndVisitCustomAttributeImpl(ctor, iter, visitor, exc, true);
  201. }
  202. bool CustomAttributeDataReader::ReadAndVisitCustomAttributeImpl(const MethodInfo* ctor, CustomAttributeDataIterator* iter, CustomAttributeReaderVisitor* visitor, Il2CppException** exc, bool deserializedManagedObjects) const
  203. {
  204. il2cpp::gc::WriteBarrier::GenericStoreNull(exc);
  205. Il2CppClass* attrClass = ctor->klass;
  206. uint32_t argumentCount = utils::ReadCompressedUInt32(&iter->dataBuffer);
  207. IL2CPP_ASSERT(iter->dataBuffer <= bufferEnd);
  208. uint32_t fieldCount = utils::ReadCompressedUInt32(&iter->dataBuffer);
  209. IL2CPP_ASSERT(iter->dataBuffer <= bufferEnd);
  210. uint32_t propertyCount = utils::ReadCompressedUInt32(&iter->dataBuffer);
  211. IL2CPP_ASSERT(iter->dataBuffer <= bufferEnd);
  212. if (iter->dataBuffer > bufferEnd)
  213. {
  214. // This should never happen
  215. IL2CPP_ASSERT(false);
  216. SetInvalidDataException(exc);
  217. return false;
  218. }
  219. visitor->MoveNext(ctor);
  220. visitor->VisitArgumentSizes(argumentCount, fieldCount, propertyCount);
  221. // CustomAttributeArgument may contain GC allocated types
  222. // So it either needs to be allocated on the stack or on the GC heap
  223. // Since these are arguments that would be passed to a method call, we assume that we're safe to stack allocate them
  224. CustomAttributeArgument* args = (CustomAttributeArgument*)alloca(argumentCount * sizeof(CustomAttributeArgument));
  225. for (uint32_t i = 0; i < argumentCount; i++)
  226. {
  227. if (!ReadAttributeDataValue(image, &iter->dataBuffer, args + i, exc, deserializedManagedObjects))
  228. return false;
  229. IL2CPP_ASSERT(iter->dataBuffer <= bufferEnd);
  230. visitor->VisitArgument(args[i], i);
  231. }
  232. visitor->VisitCtor(ctor, args, argumentCount);
  233. if (fieldCount > 0 || propertyCount > 0)
  234. vm::Class::Init(attrClass);
  235. for (uint32_t i = 0; i < fieldCount; i++)
  236. {
  237. CustomAttributeFieldArgument field = { 0 };
  238. if (!ReadAttributeDataValue(image, &iter->dataBuffer, &field.arg, exc, deserializedManagedObjects))
  239. return false;
  240. const Il2CppClass* klass;
  241. TypeFieldIndex fieldIndex;
  242. std::tie(klass, fieldIndex) = ReadCustomAttributeNamedArgumentClassAndIndex(&iter->dataBuffer, attrClass);
  243. IL2CPP_ASSERT(iter->dataBuffer <= bufferEnd);
  244. IL2CPP_ASSERT(fieldIndex < klass->field_count);
  245. field.field = &klass->fields[fieldIndex];
  246. visitor->VisitField(field, i);
  247. }
  248. for (uint32_t i = 0; i < propertyCount; i++)
  249. {
  250. CustomAttributePropertyArgument propArg = { 0 };
  251. if (!ReadAttributeDataValue(image, &iter->dataBuffer, &propArg.arg, exc, deserializedManagedObjects))
  252. return false;
  253. const Il2CppClass* klass;
  254. TypePropertyIndex propertyIndex;
  255. std::tie(klass, propertyIndex) = ReadCustomAttributeNamedArgumentClassAndIndex(&iter->dataBuffer, attrClass);
  256. IL2CPP_ASSERT(iter->dataBuffer <= bufferEnd);
  257. IL2CPP_ASSERT(propertyIndex < klass->property_count);
  258. propArg.prop = il2cpp::vm::Class::GetOrSetupOneProperty(const_cast<Il2CppClass*>(klass), propertyIndex);
  259. visitor->VisitProperty(propArg, i);
  260. }
  261. return true;
  262. }
  263. }
  264. }