CustomAttributeDataReader.cpp 11 KB

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