| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Reflection.Internal; |
| | | 5 | | using System.Threading; |
| | | 6 | | |
| | | 7 | | namespace System.Reflection.Metadata.Ecma335 |
| | | 8 | | { |
| | | 9 | | internal struct BlobHeap |
| | | 10 | | { |
| | | 11 | | private static byte[][]? s_virtualValues; |
| | | 12 | | |
| | | 13 | | internal readonly MemoryBlock Block; |
| | | 14 | | private VirtualHeap? _lazyVirtualHeap; |
| | | 15 | | |
| | | 16 | | internal BlobHeap(MemoryBlock block, MetadataKind metadataKind) |
| | 0 | 17 | | { |
| | 0 | 18 | | _lazyVirtualHeap = null; |
| | 0 | 19 | | Block = block; |
| | | 20 | | |
| | 0 | 21 | | if (s_virtualValues == null && metadataKind != MetadataKind.Ecma335) |
| | 0 | 22 | | { |
| | 0 | 23 | | var blobs = new byte[(int)BlobHandle.VirtualIndex.Count][]; |
| | | 24 | | |
| | 0 | 25 | | blobs[(int)BlobHandle.VirtualIndex.ContractPublicKeyToken] = new byte[] |
| | 0 | 26 | | { |
| | 0 | 27 | | 0xB0, 0x3F, 0x5F, 0x7F, 0x11, 0xD5, 0x0A, 0x3A |
| | 0 | 28 | | }; |
| | | 29 | | |
| | 0 | 30 | | blobs[(int)BlobHandle.VirtualIndex.ContractPublicKey] = new byte[] |
| | 0 | 31 | | { |
| | 0 | 32 | | 0x00, 0x24, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00, 0x06, 0x02, 0x00, 0x00, |
| | 0 | 33 | | 0x00, 0x24, 0x00, 0x00, 0x52, 0x53, 0x41, 0x31, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, |
| | 0 | 34 | | 0x07, 0xD1, 0xFA, 0x57, 0xC4, 0xAE, 0xD9, 0xF0, 0xA3, 0x2E, 0x84, 0xAA, 0x0F, 0xAE, 0xFD, 0x0D, |
| | 0 | 35 | | 0xE9, 0xE8, 0xFD, 0x6A, 0xEC, 0x8F, 0x87, 0xFB, 0x03, 0x76, 0x6C, 0x83, 0x4C, 0x99, 0x92, 0x1E, |
| | 0 | 36 | | 0xB2, 0x3B, 0xE7, 0x9A, 0xD9, 0xD5, 0xDC, 0xC1, 0xDD, 0x9A, 0xD2, 0x36, 0x13, 0x21, 0x02, 0x90, |
| | 0 | 37 | | 0x0B, 0x72, 0x3C, 0xF9, 0x80, 0x95, 0x7F, 0xC4, 0xE1, 0x77, 0x10, 0x8F, 0xC6, 0x07, 0x77, 0x4F, |
| | 0 | 38 | | 0x29, 0xE8, 0x32, 0x0E, 0x92, 0xEA, 0x05, 0xEC, 0xE4, 0xE8, 0x21, 0xC0, 0xA5, 0xEF, 0xE8, 0xF1, |
| | 0 | 39 | | 0x64, 0x5C, 0x4C, 0x0C, 0x93, 0xC1, 0xAB, 0x99, 0x28, 0x5D, 0x62, 0x2C, 0xAA, 0x65, 0x2C, 0x1D, |
| | 0 | 40 | | 0xFA, 0xD6, 0x3D, 0x74, 0x5D, 0x6F, 0x2D, 0xE5, 0xF1, 0x7E, 0x5E, 0xAF, 0x0F, 0xC4, 0x96, 0x3D, |
| | 0 | 41 | | 0x26, 0x1C, 0x8A, 0x12, 0x43, 0x65, 0x18, 0x20, 0x6D, 0xC0, 0x93, 0x34, 0x4D, 0x5A, 0xD2, 0x93 |
| | 0 | 42 | | }; |
| | | 43 | | |
| | 0 | 44 | | blobs[(int)BlobHandle.VirtualIndex.AttributeUsage_AllowSingle] = new byte[] |
| | 0 | 45 | | { |
| | 0 | 46 | | // preamble: |
| | 0 | 47 | | 0x01, 0x00, |
| | 0 | 48 | | // target (template parameter): |
| | 0 | 49 | | 0x00, 0x00, 0x00, 0x00, |
| | 0 | 50 | | // named arg count: |
| | 0 | 51 | | 0x01, 0x00, |
| | 0 | 52 | | // SERIALIZATION_TYPE_PROPERTY |
| | 0 | 53 | | 0x54, |
| | 0 | 54 | | // ELEMENT_TYPE_BOOLEAN |
| | 0 | 55 | | 0x02, |
| | 0 | 56 | | // "AllowMultiple".Length |
| | 0 | 57 | | 0x0D, |
| | 0 | 58 | | // "AllowMultiple" |
| | 0 | 59 | | 0x41, 0x6C, 0x6C, 0x6F, 0x77, 0x4D, 0x75, 0x6C, 0x74, 0x69, 0x70, 0x6C, 0x65, |
| | 0 | 60 | | // false |
| | 0 | 61 | | 0x00 |
| | 0 | 62 | | }; |
| | | 63 | | |
| | 0 | 64 | | blobs[(int)BlobHandle.VirtualIndex.AttributeUsage_AllowMultiple] = new byte[] |
| | 0 | 65 | | { |
| | 0 | 66 | | // preamble: |
| | 0 | 67 | | 0x01, 0x00, |
| | 0 | 68 | | // target (template parameter): |
| | 0 | 69 | | 0x00, 0x00, 0x00, 0x00, |
| | 0 | 70 | | // named arg count: |
| | 0 | 71 | | 0x01, 0x00, |
| | 0 | 72 | | // SERIALIZATION_TYPE_PROPERTY |
| | 0 | 73 | | 0x54, |
| | 0 | 74 | | // ELEMENT_TYPE_BOOLEAN |
| | 0 | 75 | | 0x02, |
| | 0 | 76 | | // "AllowMultiple".Length |
| | 0 | 77 | | 0x0D, |
| | 0 | 78 | | // "AllowMultiple" |
| | 0 | 79 | | 0x41, 0x6C, 0x6C, 0x6F, 0x77, 0x4D, 0x75, 0x6C, 0x74, 0x69, 0x70, 0x6C, 0x65, |
| | 0 | 80 | | // true |
| | 0 | 81 | | 0x01 |
| | 0 | 82 | | }; |
| | | 83 | | |
| | 0 | 84 | | s_virtualValues = blobs; |
| | 0 | 85 | | } |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | internal byte[] GetBytes(BlobHandle handle, bool unique = true) |
| | 0 | 89 | | { |
| | 0 | 90 | | if (handle.IsVirtual) |
| | 0 | 91 | | { |
| | 0 | 92 | | return GetVirtualBlobBytes(handle, unique); |
| | | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | int offset = handle.GetHeapOffset(); |
| | | 96 | | int bytesRead; |
| | 0 | 97 | | int numberOfBytes = Block.PeekCompressedInteger(offset, out bytesRead); |
| | 0 | 98 | | if (numberOfBytes == BlobReader.InvalidCompressedInteger) |
| | 0 | 99 | | { |
| | 0 | 100 | | return Array.Empty<byte>(); |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | return Block.PeekBytes(offset + bytesRead, numberOfBytes); |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | internal MemoryBlock GetMemoryBlock(BlobHandle handle) |
| | 0 | 107 | | { |
| | 0 | 108 | | if (handle.IsVirtual) |
| | 0 | 109 | | { |
| | 0 | 110 | | return GetVirtualHandleMemoryBlock(handle); |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | int offset, size; |
| | 0 | 114 | | Block.PeekHeapValueOffsetAndSize(handle.GetHeapOffset(), out offset, out size); |
| | 0 | 115 | | return Block.GetMemoryBlockAt(offset, size); |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | private MemoryBlock GetVirtualHandleMemoryBlock(BlobHandle handle) |
| | 0 | 119 | | { |
| | 0 | 120 | | var heap = VirtualHeap.GetOrCreateVirtualHeap(ref _lazyVirtualHeap); |
| | | 121 | | |
| | 0 | 122 | | lock (heap) |
| | 0 | 123 | | { |
| | 0 | 124 | | if (!heap.TryGetMemoryBlock(handle.RawValue, out var block)) |
| | 0 | 125 | | { |
| | 0 | 126 | | block = heap.AddBlob(handle.RawValue, GetVirtualBlobBytes(handle, unique: false)); |
| | 0 | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | return block; |
| | | 130 | | } |
| | 0 | 131 | | } |
| | | 132 | | |
| | | 133 | | internal BlobReader GetBlobReader(BlobHandle handle) |
| | 0 | 134 | | { |
| | 0 | 135 | | return new BlobReader(GetMemoryBlock(handle)); |
| | 0 | 136 | | } |
| | | 137 | | |
| | | 138 | | internal BlobHandle GetNextHandle(BlobHandle handle) |
| | 0 | 139 | | { |
| | 0 | 140 | | if (handle.IsVirtual) |
| | 0 | 141 | | { |
| | 0 | 142 | | return default(BlobHandle); |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | int offset, size; |
| | 0 | 146 | | if (!Block.PeekHeapValueOffsetAndSize(handle.GetHeapOffset(), out offset, out size)) |
| | 0 | 147 | | { |
| | 0 | 148 | | return default(BlobHandle); |
| | | 149 | | } |
| | | 150 | | |
| | 0 | 151 | | int nextIndex = offset + size; |
| | 0 | 152 | | if (nextIndex >= Block.Length) |
| | 0 | 153 | | { |
| | 0 | 154 | | return default(BlobHandle); |
| | | 155 | | } |
| | | 156 | | |
| | 0 | 157 | | return BlobHandle.FromOffset(nextIndex); |
| | 0 | 158 | | } |
| | | 159 | | |
| | | 160 | | internal static byte[] GetVirtualBlobBytes(BlobHandle handle, bool unique) |
| | 0 | 161 | | { |
| | 0 | 162 | | BlobHandle.VirtualIndex index = handle.GetVirtualIndex(); |
| | 0 | 163 | | byte[] result = s_virtualValues![(int)index]; |
| | | 164 | | |
| | 0 | 165 | | switch (index) |
| | | 166 | | { |
| | | 167 | | case BlobHandle.VirtualIndex.AttributeUsage_AllowMultiple: |
| | | 168 | | case BlobHandle.VirtualIndex.AttributeUsage_AllowSingle: |
| | 0 | 169 | | result = (byte[])result.Clone(); |
| | 0 | 170 | | handle.SubstituteTemplateParameters(result); |
| | 0 | 171 | | break; |
| | | 172 | | |
| | | 173 | | default: |
| | 0 | 174 | | if (unique) |
| | 0 | 175 | | { |
| | 0 | 176 | | result = (byte[])result.Clone(); |
| | 0 | 177 | | } |
| | 0 | 178 | | break; |
| | | 179 | | } |
| | | 180 | | |
| | 0 | 181 | | return result; |
| | 0 | 182 | | } |
| | | 183 | | |
| | | 184 | | public string GetDocumentName(DocumentNameBlobHandle handle) |
| | 0 | 185 | | { |
| | 0 | 186 | | var blobReader = GetBlobReader(handle); |
| | | 187 | | |
| | | 188 | | // Spec: separator is an ASCII encoded character in range [0x01, 0x7F], or byte 0 to represent an empty sepa |
| | 0 | 189 | | int separator = blobReader.ReadByte(); |
| | 0 | 190 | | if (separator > 0x7f) |
| | 0 | 191 | | { |
| | 0 | 192 | | throw new BadImageFormatException(SR.InvalidDocumentName); |
| | | 193 | | } |
| | | 194 | | |
| | 0 | 195 | | var pooledBuilder = PooledStringBuilder.GetInstance(); |
| | 0 | 196 | | var builder = pooledBuilder.Builder; |
| | 0 | 197 | | bool isFirstPart = true; |
| | 0 | 198 | | while (blobReader.RemainingBytes > 0) |
| | 0 | 199 | | { |
| | 0 | 200 | | if (separator != 0 && !isFirstPart) |
| | 0 | 201 | | { |
| | 0 | 202 | | builder.Append((char)separator); |
| | 0 | 203 | | } |
| | | 204 | | |
| | 0 | 205 | | var partReader = GetBlobReader(blobReader.ReadBlobHandle()); |
| | | 206 | | |
| | 0 | 207 | | builder.Append(partReader.ReadUTF8(partReader.Length)); |
| | 0 | 208 | | isFirstPart = false; |
| | 0 | 209 | | } |
| | | 210 | | |
| | 0 | 211 | | return pooledBuilder.ToStringAndFree(); |
| | 0 | 212 | | } |
| | | 213 | | |
| | | 214 | | internal bool DocumentNameEquals(DocumentNameBlobHandle handle, string other, bool ignoreCase) |
| | 0 | 215 | | { |
| | 0 | 216 | | var blobReader = GetBlobReader(handle); |
| | | 217 | | |
| | | 218 | | // Spec: separator is an ASCII encoded character in range [0x01, 0x7F], or byte 0 to represent an empty sepa |
| | 0 | 219 | | int separator = blobReader.ReadByte(); |
| | 0 | 220 | | if (separator > 0x7f) |
| | 0 | 221 | | { |
| | 0 | 222 | | return false; |
| | | 223 | | } |
| | | 224 | | |
| | 0 | 225 | | int ignoreCaseMask = StringUtils.IgnoreCaseMask(ignoreCase); |
| | 0 | 226 | | int otherIndex = 0; |
| | 0 | 227 | | bool isFirstPart = true; |
| | 0 | 228 | | while (blobReader.RemainingBytes > 0) |
| | 0 | 229 | | { |
| | 0 | 230 | | if (separator != 0 && !isFirstPart) |
| | 0 | 231 | | { |
| | 0 | 232 | | if (otherIndex == other.Length || !StringUtils.IsEqualAscii(other[otherIndex], separator, ignoreCase |
| | 0 | 233 | | { |
| | 0 | 234 | | return false; |
| | | 235 | | } |
| | | 236 | | |
| | 0 | 237 | | otherIndex++; |
| | 0 | 238 | | } |
| | | 239 | | |
| | 0 | 240 | | var partBlock = GetMemoryBlock(blobReader.ReadBlobHandle()); |
| | | 241 | | |
| | | 242 | | int firstDifferenceIndex; |
| | 0 | 243 | | var result = partBlock.Utf8NullTerminatedFastCompare(0, other, otherIndex, out firstDifferenceIndex, ter |
| | 0 | 244 | | if (result == MemoryBlock.FastComparisonResult.Inconclusive) |
| | 0 | 245 | | { |
| | 0 | 246 | | return GetDocumentName(handle).Equals(other, ignoreCase ? StringComparison.OrdinalIgnoreCase : Strin |
| | | 247 | | } |
| | | 248 | | |
| | 0 | 249 | | if (result == MemoryBlock.FastComparisonResult.Unequal || |
| | 0 | 250 | | firstDifferenceIndex - otherIndex != partBlock.Length) |
| | 0 | 251 | | { |
| | 0 | 252 | | return false; |
| | | 253 | | } |
| | | 254 | | |
| | 0 | 255 | | otherIndex = firstDifferenceIndex; |
| | 0 | 256 | | isFirstPart = false; |
| | 0 | 257 | | } |
| | | 258 | | |
| | 0 | 259 | | return otherIndex == other.Length; |
| | 0 | 260 | | } |
| | | 261 | | } |
| | | 262 | | } |
| | | 263 | | |