| | | 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.Diagnostics; |
| | | 5 | | using System.Reflection.Internal; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Threading; |
| | | 9 | | |
| | | 10 | | namespace System.Reflection.Metadata.Ecma335 |
| | | 11 | | { |
| | | 12 | | internal struct StringHeap |
| | | 13 | | { |
| | | 14 | | private static string[]? s_virtualValues; |
| | | 15 | | |
| | | 16 | | internal readonly MemoryBlock Block; |
| | | 17 | | private VirtualHeap? _lazyVirtualHeap; |
| | | 18 | | |
| | | 19 | | internal StringHeap(MemoryBlock block, MetadataKind metadataKind) |
| | 0 | 20 | | { |
| | 0 | 21 | | _lazyVirtualHeap = null; |
| | | 22 | | |
| | 0 | 23 | | if (s_virtualValues == null && metadataKind != MetadataKind.Ecma335) |
| | 0 | 24 | | { |
| | | 25 | | // Note: |
| | | 26 | | // Virtual values shall not contain surrogates, otherwise StartsWith might be inconsistent |
| | | 27 | | // when comparing to a text that ends with a high surrogate. |
| | | 28 | | |
| | 0 | 29 | | var values = new string[(int)StringHandle.VirtualIndex.Count]; |
| | 0 | 30 | | values[(int)StringHandle.VirtualIndex.System_Runtime_WindowsRuntime] = "System.Runtime.WindowsRuntime"; |
| | 0 | 31 | | values[(int)StringHandle.VirtualIndex.System_Runtime] = "System.Runtime"; |
| | 0 | 32 | | values[(int)StringHandle.VirtualIndex.System_ObjectModel] = "System.ObjectModel"; |
| | 0 | 33 | | values[(int)StringHandle.VirtualIndex.System_Runtime_WindowsRuntime_UI_Xaml] = "System.Runtime.WindowsRu |
| | 0 | 34 | | values[(int)StringHandle.VirtualIndex.System_Runtime_InteropServices_WindowsRuntime] = "System.Runtime.I |
| | 0 | 35 | | values[(int)StringHandle.VirtualIndex.System_Numerics_Vectors] = "System.Numerics.Vectors"; |
| | | 36 | | |
| | 0 | 37 | | values[(int)StringHandle.VirtualIndex.Dispose] = "Dispose"; |
| | | 38 | | |
| | 0 | 39 | | values[(int)StringHandle.VirtualIndex.AttributeTargets] = "AttributeTargets"; |
| | 0 | 40 | | values[(int)StringHandle.VirtualIndex.AttributeUsageAttribute] = "AttributeUsageAttribute"; |
| | 0 | 41 | | values[(int)StringHandle.VirtualIndex.Color] = "Color"; |
| | 0 | 42 | | values[(int)StringHandle.VirtualIndex.CornerRadius] = "CornerRadius"; |
| | 0 | 43 | | values[(int)StringHandle.VirtualIndex.DateTimeOffset] = "DateTimeOffset"; |
| | 0 | 44 | | values[(int)StringHandle.VirtualIndex.Duration] = "Duration"; |
| | 0 | 45 | | values[(int)StringHandle.VirtualIndex.DurationType] = "DurationType"; |
| | 0 | 46 | | values[(int)StringHandle.VirtualIndex.EventHandler1] = "EventHandler`1"; |
| | 0 | 47 | | values[(int)StringHandle.VirtualIndex.EventRegistrationToken] = "EventRegistrationToken"; |
| | 0 | 48 | | values[(int)StringHandle.VirtualIndex.Exception] = "Exception"; |
| | 0 | 49 | | values[(int)StringHandle.VirtualIndex.GeneratorPosition] = "GeneratorPosition"; |
| | 0 | 50 | | values[(int)StringHandle.VirtualIndex.GridLength] = "GridLength"; |
| | 0 | 51 | | values[(int)StringHandle.VirtualIndex.GridUnitType] = "GridUnitType"; |
| | 0 | 52 | | values[(int)StringHandle.VirtualIndex.ICommand] = "ICommand"; |
| | 0 | 53 | | values[(int)StringHandle.VirtualIndex.IDictionary2] = "IDictionary`2"; |
| | 0 | 54 | | values[(int)StringHandle.VirtualIndex.IDisposable] = "IDisposable"; |
| | 0 | 55 | | values[(int)StringHandle.VirtualIndex.IEnumerable] = "IEnumerable"; |
| | 0 | 56 | | values[(int)StringHandle.VirtualIndex.IEnumerable1] = "IEnumerable`1"; |
| | 0 | 57 | | values[(int)StringHandle.VirtualIndex.IList] = "IList"; |
| | 0 | 58 | | values[(int)StringHandle.VirtualIndex.IList1] = "IList`1"; |
| | 0 | 59 | | values[(int)StringHandle.VirtualIndex.INotifyCollectionChanged] = "INotifyCollectionChanged"; |
| | 0 | 60 | | values[(int)StringHandle.VirtualIndex.INotifyPropertyChanged] = "INotifyPropertyChanged"; |
| | 0 | 61 | | values[(int)StringHandle.VirtualIndex.IReadOnlyDictionary2] = "IReadOnlyDictionary`2"; |
| | 0 | 62 | | values[(int)StringHandle.VirtualIndex.IReadOnlyList1] = "IReadOnlyList`1"; |
| | 0 | 63 | | values[(int)StringHandle.VirtualIndex.KeyTime] = "KeyTime"; |
| | 0 | 64 | | values[(int)StringHandle.VirtualIndex.KeyValuePair2] = "KeyValuePair`2"; |
| | 0 | 65 | | values[(int)StringHandle.VirtualIndex.Matrix] = "Matrix"; |
| | 0 | 66 | | values[(int)StringHandle.VirtualIndex.Matrix3D] = "Matrix3D"; |
| | 0 | 67 | | values[(int)StringHandle.VirtualIndex.Matrix3x2] = "Matrix3x2"; |
| | 0 | 68 | | values[(int)StringHandle.VirtualIndex.Matrix4x4] = "Matrix4x4"; |
| | 0 | 69 | | values[(int)StringHandle.VirtualIndex.NotifyCollectionChangedAction] = "NotifyCollectionChangedAction"; |
| | 0 | 70 | | values[(int)StringHandle.VirtualIndex.NotifyCollectionChangedEventArgs] = "NotifyCollectionChangedEventA |
| | 0 | 71 | | values[(int)StringHandle.VirtualIndex.NotifyCollectionChangedEventHandler] = "NotifyCollectionChangedEve |
| | 0 | 72 | | values[(int)StringHandle.VirtualIndex.Nullable1] = "Nullable`1"; |
| | 0 | 73 | | values[(int)StringHandle.VirtualIndex.Plane] = "Plane"; |
| | 0 | 74 | | values[(int)StringHandle.VirtualIndex.Point] = "Point"; |
| | 0 | 75 | | values[(int)StringHandle.VirtualIndex.PropertyChangedEventArgs] = "PropertyChangedEventArgs"; |
| | 0 | 76 | | values[(int)StringHandle.VirtualIndex.PropertyChangedEventHandler] = "PropertyChangedEventHandler"; |
| | 0 | 77 | | values[(int)StringHandle.VirtualIndex.Quaternion] = "Quaternion"; |
| | 0 | 78 | | values[(int)StringHandle.VirtualIndex.Rect] = "Rect"; |
| | 0 | 79 | | values[(int)StringHandle.VirtualIndex.RepeatBehavior] = "RepeatBehavior"; |
| | 0 | 80 | | values[(int)StringHandle.VirtualIndex.RepeatBehaviorType] = "RepeatBehaviorType"; |
| | 0 | 81 | | values[(int)StringHandle.VirtualIndex.Size] = "Size"; |
| | 0 | 82 | | values[(int)StringHandle.VirtualIndex.System] = "System"; |
| | 0 | 83 | | values[(int)StringHandle.VirtualIndex.System_Collections] = "System.Collections"; |
| | 0 | 84 | | values[(int)StringHandle.VirtualIndex.System_Collections_Generic] = "System.Collections.Generic"; |
| | 0 | 85 | | values[(int)StringHandle.VirtualIndex.System_Collections_Specialized] = "System.Collections.Specialized" |
| | 0 | 86 | | values[(int)StringHandle.VirtualIndex.System_ComponentModel] = "System.ComponentModel"; |
| | 0 | 87 | | values[(int)StringHandle.VirtualIndex.System_Numerics] = "System.Numerics"; |
| | 0 | 88 | | values[(int)StringHandle.VirtualIndex.System_Windows_Input] = "System.Windows.Input"; |
| | 0 | 89 | | values[(int)StringHandle.VirtualIndex.Thickness] = "Thickness"; |
| | 0 | 90 | | values[(int)StringHandle.VirtualIndex.TimeSpan] = "TimeSpan"; |
| | 0 | 91 | | values[(int)StringHandle.VirtualIndex.Type] = "Type"; |
| | 0 | 92 | | values[(int)StringHandle.VirtualIndex.Uri] = "Uri"; |
| | 0 | 93 | | values[(int)StringHandle.VirtualIndex.Vector2] = "Vector2"; |
| | 0 | 94 | | values[(int)StringHandle.VirtualIndex.Vector3] = "Vector3"; |
| | 0 | 95 | | values[(int)StringHandle.VirtualIndex.Vector4] = "Vector4"; |
| | 0 | 96 | | values[(int)StringHandle.VirtualIndex.Windows_Foundation] = "Windows.Foundation"; |
| | 0 | 97 | | values[(int)StringHandle.VirtualIndex.Windows_UI] = "Windows.UI"; |
| | 0 | 98 | | values[(int)StringHandle.VirtualIndex.Windows_UI_Xaml] = "Windows.UI.Xaml"; |
| | 0 | 99 | | values[(int)StringHandle.VirtualIndex.Windows_UI_Xaml_Controls_Primitives] = "Windows.UI.Xaml.Controls.P |
| | 0 | 100 | | values[(int)StringHandle.VirtualIndex.Windows_UI_Xaml_Media] = "Windows.UI.Xaml.Media"; |
| | 0 | 101 | | values[(int)StringHandle.VirtualIndex.Windows_UI_Xaml_Media_Animation] = "Windows.UI.Xaml.Media.Animatio |
| | 0 | 102 | | values[(int)StringHandle.VirtualIndex.Windows_UI_Xaml_Media_Media3D] = "Windows.UI.Xaml.Media.Media3D"; |
| | | 103 | | |
| | 0 | 104 | | s_virtualValues = values; |
| | 0 | 105 | | AssertFilled(); |
| | 0 | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | this.Block = TrimEnd(block); |
| | 0 | 109 | | } |
| | | 110 | | |
| | | 111 | | [Conditional("DEBUG")] |
| | | 112 | | private static void AssertFilled() |
| | 0 | 113 | | { |
| | 0 | 114 | | for (int i = 0; i < s_virtualValues!.Length; i++) |
| | 0 | 115 | | { |
| | 0 | 116 | | Debug.Assert(s_virtualValues[i] != null, $"Missing virtual value for StringHandle.VirtualIndex.{(StringH |
| | 0 | 117 | | } |
| | 0 | 118 | | } |
| | | 119 | | |
| | | 120 | | // Trims the alignment padding of the heap. |
| | | 121 | | // See StgStringPool::InitOnMem in ndp\clr\src\Utilcode\StgPool.cpp. |
| | | 122 | | |
| | | 123 | | // This is especially important for EnC. |
| | | 124 | | private static MemoryBlock TrimEnd(MemoryBlock block) |
| | 0 | 125 | | { |
| | 0 | 126 | | if (block.Length == 0) |
| | 0 | 127 | | { |
| | 0 | 128 | | return block; |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | int i = block.Length - 1; |
| | 0 | 132 | | while (i >= 0 && block.PeekByte(i) == 0) |
| | 0 | 133 | | { |
| | 0 | 134 | | i--; |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | // this shouldn't happen in valid metadata: |
| | 0 | 138 | | if (i == block.Length - 1) |
| | 0 | 139 | | { |
| | 0 | 140 | | return block; |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | // +1 for terminating \0 |
| | 0 | 144 | | return block.GetMemoryBlockAt(0, i + 2); |
| | 0 | 145 | | } |
| | | 146 | | |
| | | 147 | | internal string GetString(StringHandle handle, MetadataStringDecoder utf8Decoder) |
| | 0 | 148 | | { |
| | 0 | 149 | | return handle.IsVirtual ? GetVirtualHandleString(handle, utf8Decoder) : GetNonVirtualString(handle, utf8Deco |
| | 0 | 150 | | } |
| | | 151 | | |
| | | 152 | | internal MemoryBlock GetMemoryBlock(StringHandle handle) |
| | 0 | 153 | | { |
| | 0 | 154 | | return handle.IsVirtual ? GetVirtualHandleMemoryBlock(handle) : GetNonVirtualStringMemoryBlock(handle); |
| | 0 | 155 | | } |
| | | 156 | | |
| | | 157 | | internal static string GetVirtualString(StringHandle.VirtualIndex index) |
| | 0 | 158 | | { |
| | 0 | 159 | | return s_virtualValues![(int)index]; |
| | 0 | 160 | | } |
| | | 161 | | |
| | | 162 | | private string GetNonVirtualString(StringHandle handle, MetadataStringDecoder utf8Decoder, byte[]? prefixOpt) |
| | 0 | 163 | | { |
| | 0 | 164 | | Debug.Assert(handle.StringKind != StringKind.Virtual); |
| | | 165 | | |
| | 0 | 166 | | char otherTerminator = handle.StringKind == StringKind.DotTerminated ? '.' : '\0'; |
| | 0 | 167 | | return Block.PeekUtf8NullTerminated(handle.GetHeapOffset(), prefixOpt, utf8Decoder, out _, otherTerminator); |
| | 0 | 168 | | } |
| | | 169 | | |
| | | 170 | | private unsafe MemoryBlock GetNonVirtualStringMemoryBlock(StringHandle handle) |
| | 0 | 171 | | { |
| | 0 | 172 | | Debug.Assert(handle.StringKind != StringKind.Virtual); |
| | | 173 | | |
| | 0 | 174 | | char otherTerminator = handle.StringKind == StringKind.DotTerminated ? '.' : '\0'; |
| | 0 | 175 | | int offset = handle.GetHeapOffset(); |
| | 0 | 176 | | int length = Block.GetUtf8NullTerminatedLength(offset, out _, otherTerminator); |
| | | 177 | | |
| | 0 | 178 | | return new MemoryBlock(Block.Pointer + offset, length); |
| | 0 | 179 | | } |
| | | 180 | | |
| | | 181 | | private unsafe byte[] GetNonVirtualStringBytes(StringHandle handle, byte[] prefix) |
| | 0 | 182 | | { |
| | 0 | 183 | | Debug.Assert(handle.StringKind != StringKind.Virtual); |
| | | 184 | | |
| | 0 | 185 | | var block = GetNonVirtualStringMemoryBlock(handle); |
| | 0 | 186 | | var bytes = new byte[prefix.Length + block.Length]; |
| | 0 | 187 | | Buffer.BlockCopy(prefix, 0, bytes, 0, prefix.Length); |
| | 0 | 188 | | Marshal.Copy((IntPtr)block.Pointer, bytes, prefix.Length, block.Length); |
| | 0 | 189 | | return bytes; |
| | 0 | 190 | | } |
| | | 191 | | |
| | | 192 | | private string GetVirtualHandleString(StringHandle handle, MetadataStringDecoder utf8Decoder) |
| | 0 | 193 | | { |
| | 0 | 194 | | Debug.Assert(handle.IsVirtual); |
| | | 195 | | |
| | 0 | 196 | | return handle.StringKind switch |
| | 0 | 197 | | { |
| | 0 | 198 | | StringKind.Virtual => GetVirtualString(handle.GetVirtualIndex()), |
| | 0 | 199 | | StringKind.WinRTPrefixed => GetNonVirtualString(handle, utf8Decoder, MetadataReader.WinRTPrefix), |
| | 0 | 200 | | _ => throw ExceptionUtilities.UnexpectedValue(handle.StringKind), |
| | 0 | 201 | | }; |
| | 0 | 202 | | } |
| | | 203 | | |
| | | 204 | | private MemoryBlock GetVirtualHandleMemoryBlock(StringHandle handle) |
| | 0 | 205 | | { |
| | 0 | 206 | | Debug.Assert(handle.IsVirtual); |
| | 0 | 207 | | var heap = VirtualHeap.GetOrCreateVirtualHeap(ref _lazyVirtualHeap); |
| | | 208 | | |
| | 0 | 209 | | lock (heap) |
| | 0 | 210 | | { |
| | 0 | 211 | | if (!heap.TryGetMemoryBlock(handle.RawValue, out var block)) |
| | 0 | 212 | | { |
| | 0 | 213 | | byte[] bytes = handle.StringKind switch |
| | 0 | 214 | | { |
| | 0 | 215 | | StringKind.Virtual => Encoding.UTF8.GetBytes(GetVirtualString(handle.GetVirtualIndex())), |
| | 0 | 216 | | StringKind.WinRTPrefixed => GetNonVirtualStringBytes(handle, MetadataReader.WinRTPrefix), |
| | 0 | 217 | | _ => throw ExceptionUtilities.UnexpectedValue(handle.StringKind), |
| | 0 | 218 | | }; |
| | 0 | 219 | | block = heap.AddBlob(handle.RawValue, bytes); |
| | 0 | 220 | | } |
| | | 221 | | |
| | 0 | 222 | | return block; |
| | | 223 | | } |
| | 0 | 224 | | } |
| | | 225 | | |
| | | 226 | | internal BlobReader GetBlobReader(StringHandle handle) |
| | 0 | 227 | | { |
| | 0 | 228 | | return new BlobReader(GetMemoryBlock(handle)); |
| | 0 | 229 | | } |
| | | 230 | | |
| | | 231 | | internal StringHandle GetNextHandle(StringHandle handle) |
| | 0 | 232 | | { |
| | 0 | 233 | | if (handle.IsVirtual) |
| | 0 | 234 | | { |
| | 0 | 235 | | return default(StringHandle); |
| | | 236 | | } |
| | | 237 | | |
| | 0 | 238 | | int terminator = this.Block.IndexOf(0, handle.GetHeapOffset()); |
| | 0 | 239 | | if (terminator == -1 || terminator == Block.Length - 1) |
| | 0 | 240 | | { |
| | 0 | 241 | | return default(StringHandle); |
| | | 242 | | } |
| | | 243 | | |
| | 0 | 244 | | return StringHandle.FromOffset(terminator + 1); |
| | 0 | 245 | | } |
| | | 246 | | |
| | | 247 | | internal bool Equals(StringHandle handle, string value, MetadataStringDecoder utf8Decoder, bool ignoreCase) |
| | 0 | 248 | | { |
| | 0 | 249 | | Debug.Assert(value != null); |
| | | 250 | | |
| | 0 | 251 | | if (handle.IsVirtual) |
| | 0 | 252 | | { |
| | | 253 | | // TODO: This can allocate unnecessarily for <WinRT> prefixed handles. |
| | 0 | 254 | | return string.Equals(GetString(handle, utf8Decoder), value, ignoreCase ? StringComparison.OrdinalIgnoreC |
| | | 255 | | } |
| | | 256 | | |
| | 0 | 257 | | if (handle.IsNil) |
| | 0 | 258 | | { |
| | 0 | 259 | | return value.Length == 0; |
| | | 260 | | } |
| | | 261 | | |
| | 0 | 262 | | char otherTerminator = handle.StringKind == StringKind.DotTerminated ? '.' : '\0'; |
| | 0 | 263 | | return this.Block.Utf8NullTerminatedEquals(handle.GetHeapOffset(), value, utf8Decoder, otherTerminator, igno |
| | 0 | 264 | | } |
| | | 265 | | |
| | | 266 | | internal bool StartsWith(StringHandle handle, string value, MetadataStringDecoder utf8Decoder, bool ignoreCase) |
| | 0 | 267 | | { |
| | 0 | 268 | | Debug.Assert(value != null); |
| | | 269 | | |
| | 0 | 270 | | if (handle.IsVirtual) |
| | 0 | 271 | | { |
| | | 272 | | // TODO: This can allocate unnecessarily for <WinRT> prefixed handles. |
| | 0 | 273 | | return GetString(handle, utf8Decoder).StartsWith(value, ignoreCase ? StringComparison.OrdinalIgnoreCase |
| | | 274 | | } |
| | | 275 | | |
| | 0 | 276 | | if (handle.IsNil) |
| | 0 | 277 | | { |
| | 0 | 278 | | return value.Length == 0; |
| | | 279 | | } |
| | | 280 | | |
| | 0 | 281 | | char otherTerminator = handle.StringKind == StringKind.DotTerminated ? '.' : '\0'; |
| | 0 | 282 | | return this.Block.Utf8NullTerminatedStartsWith(handle.GetHeapOffset(), value, utf8Decoder, otherTerminator, |
| | 0 | 283 | | } |
| | | 284 | | |
| | | 285 | | /// <summary> |
| | | 286 | | /// Returns true if the given raw (non-virtual) handle represents the same string as given ASCII string. |
| | | 287 | | /// </summary> |
| | | 288 | | internal bool EqualsRaw(StringHandle rawHandle, string asciiString) |
| | 0 | 289 | | { |
| | 0 | 290 | | Debug.Assert(!rawHandle.IsVirtual); |
| | 0 | 291 | | Debug.Assert(rawHandle.StringKind != StringKind.DotTerminated, "Not supported"); |
| | 0 | 292 | | return this.Block.CompareUtf8NullTerminatedStringWithAsciiString(rawHandle.GetHeapOffset(), asciiString) == |
| | 0 | 293 | | } |
| | | 294 | | |
| | | 295 | | /// <summary> |
| | | 296 | | /// Returns the heap index of the given ASCII character or -1 if not found prior null terminator or end of heap. |
| | | 297 | | /// </summary> |
| | | 298 | | internal int IndexOfRaw(int startIndex, char asciiChar) |
| | 0 | 299 | | { |
| | 0 | 300 | | Debug.Assert(asciiChar != 0 && asciiChar <= 0x7f); |
| | 0 | 301 | | return this.Block.Utf8NullTerminatedOffsetOfAsciiChar(startIndex, asciiChar); |
| | 0 | 302 | | } |
| | | 303 | | |
| | | 304 | | /// <summary> |
| | | 305 | | /// Returns true if the given raw (non-virtual) handle represents a string that starts with given ASCII prefix. |
| | | 306 | | /// </summary> |
| | | 307 | | internal bool StartsWithRaw(StringHandle rawHandle, string asciiPrefix) |
| | 0 | 308 | | { |
| | 0 | 309 | | Debug.Assert(!rawHandle.IsVirtual); |
| | 0 | 310 | | Debug.Assert(rawHandle.StringKind != StringKind.DotTerminated, "Not supported"); |
| | 0 | 311 | | return this.Block.Utf8NullTerminatedStringStartsWithAsciiPrefix(rawHandle.GetHeapOffset(), asciiPrefix); |
| | 0 | 312 | | } |
| | | 313 | | |
| | | 314 | | /// <summary> |
| | | 315 | | /// Equivalent to Array.BinarySearch, searches for given raw (non-virtual) handle in given array of ASCII string |
| | | 316 | | /// </summary> |
| | | 317 | | internal int BinarySearchRaw(string[] asciiKeys, StringHandle rawHandle) |
| | 0 | 318 | | { |
| | 0 | 319 | | Debug.Assert(!rawHandle.IsVirtual); |
| | 0 | 320 | | Debug.Assert(rawHandle.StringKind != StringKind.DotTerminated, "Not supported"); |
| | 0 | 321 | | return this.Block.BinarySearch(asciiKeys, rawHandle.GetHeapOffset()); |
| | 0 | 322 | | } |
| | | 323 | | } |
| | | 324 | | } |
| | | 325 | | |