-
Notifications
You must be signed in to change notification settings - Fork 127
/
CustomAttributeArgumentReader.cs
155 lines (121 loc) · 5.15 KB
/
CustomAttributeArgumentReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System.Collections.Generic;
using System.Linq;
using AsmResolver.DotNet.Signatures.Types;
using AsmResolver.DotNet.Signatures.Types.Parsing;
using AsmResolver.IO;
using AsmResolver.PE.DotNet.Metadata.Tables.Rows;
namespace AsmResolver.DotNet.Signatures
{
// src/coreclr/src/vm/customattribute.cpp
internal struct CustomAttributeArgumentReader
{
public static CustomAttributeArgumentReader Create() => new(new List<object?>());
public CustomAttributeArgumentReader(IList<object?> elements)
{
Elements = elements;
IsNullArray = false;
}
public IList<object?> Elements
{
get;
}
public bool IsNullArray
{
get;
private set;
}
public void ReadValue(in BlobReadContext context, ref BinaryStreamReader reader, TypeSignature valueType)
{
var module = context.ReaderContext.ParentModule;
if (valueType.IsTypeOf("System", "Type"))
{
string? typeFullName = reader.ReadSerString();
var type = typeFullName is not null
? TypeNameParser.Parse(module, typeFullName)
: null;
Elements.Add(type);
return;
}
switch (valueType.ElementType)
{
case ElementType.Boolean:
Elements.Add(reader.ReadByte() == 1);
break;
case ElementType.Char:
Elements.Add((char) reader.ReadUInt16());
break;
case ElementType.R4:
Elements.Add(reader.ReadSingle());
break;
case ElementType.R8:
Elements.Add(reader.ReadDouble());
break;
case ElementType.I1:
Elements.Add(reader.ReadSByte());
break;
case ElementType.I2:
Elements.Add(reader.ReadInt16());
break;
case ElementType.I4:
Elements.Add(reader.ReadInt32());
break;
case ElementType.I8:
Elements.Add(reader.ReadInt64());
break;
case ElementType.U1:
Elements.Add(reader.ReadByte());
break;
case ElementType.U2:
Elements.Add(reader.ReadUInt16());
break;
case ElementType.U4:
Elements.Add(reader.ReadUInt32());
break;
case ElementType.U8:
Elements.Add(reader.ReadUInt64());
break;
case ElementType.String:
Elements.Add(reader.ReadSerString());
break;
case ElementType.Object:
var type = TypeSignature.ReadFieldOrPropType(context, ref reader);
var subReader = Create();
subReader.ReadValue(context, ref reader, type);
Elements.Add(new BoxedArgument(type, type.ElementType == ElementType.SzArray
? subReader.Elements.ToArray()
: subReader.Elements[0]));
break;
case ElementType.SzArray:
var arrayElementType = ((SzArrayTypeSignature) valueType).BaseType;
uint elementCount = reader.CanRead(sizeof(uint)) ? reader.ReadUInt32() : uint.MaxValue;
IsNullArray = elementCount == uint.MaxValue;
if (!IsNullArray)
{
for (uint i = 0; i < elementCount; i++)
ReadValue(context, ref reader, arrayElementType);
}
break;
case ElementType.Class:
case ElementType.Enum:
case ElementType.ValueType:
// Value is an enum, resolve it and get underlying type.
// If that fails, most enums are int32s, assume that is the case in an attempt to recover.
var enumTypeDef = module.MetadataResolver.ResolveType(valueType);
TypeSignature? underlyingType = null;
if (enumTypeDef is {IsEnum: true})
underlyingType = enumTypeDef.GetEnumUnderlyingType();
if (underlyingType is null)
{
context.ReaderContext.BadImage($"Underlying enum type {valueType} could not be resolved. Assuming System.Int32 for custom attribute argument.");
underlyingType = module.CorLibTypeFactory.Int32;
}
ReadValue(context, ref reader, underlyingType);
break;
default:
context.ReaderContext.NotSupported($"Unsupported element type {valueType.ElementType} in custom attribute argument.");
Elements.Add(null);
break;
}
}
}
}