No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CustomAttributeDataReader.cpp 13KB

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