|
| 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; |
| 5 | +using System.Buffers.Binary; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Runtime.InteropServices; |
| 9 | + |
| 10 | +namespace Microsoft.Diagnostics.DataContractReader.Tests.ContractDescriptor; |
| 11 | + |
| 12 | +internal unsafe class ContractDescriptorHelpers |
| 13 | +{ |
| 14 | + public static int Size(bool is64Bit) => is64Bit ? sizeof(ContractDescriptor64) : sizeof(ContractDescriptor32); |
| 15 | + |
| 16 | + public static void Fill(Span<byte> dest, MockTarget.Architecture arch, int jsonDescriptorSize, uint jsonDescriptorAddr, int pointerDataCount, uint pointerDataAddr) |
| 17 | + { |
| 18 | + if (arch.Is64Bit) |
| 19 | + { |
| 20 | + ContractDescriptor64.Fill(dest, arch.IsLittleEndian, jsonDescriptorSize, jsonDescriptorAddr, pointerDataCount, pointerDataAddr); |
| 21 | + } |
| 22 | + else |
| 23 | + { |
| 24 | + ContractDescriptor32.Fill(dest, arch.IsLittleEndian, jsonDescriptorSize, jsonDescriptorAddr, pointerDataCount, pointerDataAddr); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + private struct ContractDescriptor32 |
| 29 | + { |
| 30 | + public ulong Magic = BitConverter.ToUInt64("DNCCDAC\0"u8); |
| 31 | + public uint Flags = 0x2 /*32-bit*/ | 0x1; |
| 32 | + public uint DescriptorSize; |
| 33 | + public uint Descriptor; |
| 34 | + public uint PointerDataCount; |
| 35 | + public uint Pad0 = 0; |
| 36 | + public uint PointerData; |
| 37 | + |
| 38 | + public ContractDescriptor32() { } |
| 39 | + |
| 40 | + public static void Fill(Span<byte> dest, bool isLittleEndian, int jsonDescriptorSize, uint jsonDescriptorAddr, int pointerDataCount, uint pointerDataAddr) |
| 41 | + { |
| 42 | + ContractDescriptor32 descriptor = new() |
| 43 | + { |
| 44 | + DescriptorSize = (uint)jsonDescriptorSize, |
| 45 | + Descriptor = jsonDescriptorAddr, |
| 46 | + PointerDataCount = (uint)pointerDataCount, |
| 47 | + PointerData = pointerDataAddr, |
| 48 | + }; |
| 49 | + if (BitConverter.IsLittleEndian != isLittleEndian) |
| 50 | + descriptor.ReverseEndianness(); |
| 51 | + |
| 52 | + MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref descriptor, 1)).CopyTo(dest); |
| 53 | + } |
| 54 | + |
| 55 | + private void ReverseEndianness() |
| 56 | + { |
| 57 | + Magic = BinaryPrimitives.ReverseEndianness(Magic); |
| 58 | + Flags = BinaryPrimitives.ReverseEndianness(Flags); |
| 59 | + DescriptorSize = BinaryPrimitives.ReverseEndianness(DescriptorSize); |
| 60 | + Descriptor = BinaryPrimitives.ReverseEndianness(Descriptor); |
| 61 | + PointerDataCount = BinaryPrimitives.ReverseEndianness(PointerDataCount); |
| 62 | + Pad0 = BinaryPrimitives.ReverseEndianness(Pad0); |
| 63 | + PointerData = BinaryPrimitives.ReverseEndianness(PointerData); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private struct ContractDescriptor64 |
| 68 | + { |
| 69 | + public ulong Magic = BitConverter.ToUInt64("DNCCDAC\0"u8); |
| 70 | + public uint Flags = 0x1; |
| 71 | + public uint DescriptorSize; |
| 72 | + public ulong Descriptor; |
| 73 | + public uint PointerDataCount; |
| 74 | + public uint Pad0 = 0; |
| 75 | + public ulong PointerData; |
| 76 | + |
| 77 | + public ContractDescriptor64() { } |
| 78 | + |
| 79 | + public static void Fill(Span<byte> dest, bool isLittleEndian, int jsonDescriptorSize, uint jsonDescriptorAddr, int pointerDataCount, uint pointerDataAddr) |
| 80 | + { |
| 81 | + ContractDescriptor64 descriptor = new() |
| 82 | + { |
| 83 | + DescriptorSize = (uint)jsonDescriptorSize, |
| 84 | + Descriptor = jsonDescriptorAddr, |
| 85 | + PointerDataCount = (uint)pointerDataCount, |
| 86 | + PointerData = pointerDataAddr, |
| 87 | + }; |
| 88 | + if (BitConverter.IsLittleEndian != isLittleEndian) |
| 89 | + descriptor.ReverseEndianness(); |
| 90 | + |
| 91 | + MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref descriptor, 1)).CopyTo(dest); |
| 92 | + } |
| 93 | + |
| 94 | + private void ReverseEndianness() |
| 95 | + { |
| 96 | + Magic = BinaryPrimitives.ReverseEndianness(Magic); |
| 97 | + Flags = BinaryPrimitives.ReverseEndianness(Flags); |
| 98 | + DescriptorSize = BinaryPrimitives.ReverseEndianness(DescriptorSize); |
| 99 | + Descriptor = BinaryPrimitives.ReverseEndianness(Descriptor); |
| 100 | + PointerDataCount = BinaryPrimitives.ReverseEndianness(PointerDataCount); |
| 101 | + Pad0 = BinaryPrimitives.ReverseEndianness(Pad0); |
| 102 | + PointerData = BinaryPrimitives.ReverseEndianness(PointerData); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + #region JSON formatting |
| 107 | + private static string GetTypeJson(string name, Target.TypeInfo info) |
| 108 | + { |
| 109 | + string ret = string.Empty; |
| 110 | + List<string> fields = info.Size is null ? [] : [$"\"!\":{info.Size}"]; |
| 111 | + fields.AddRange(info.Fields.Select(f => $"\"{f.Key}\":{(f.Value.TypeName is null ? f.Value.Offset : $"[{f.Value.Offset},\"{f.Value.TypeName}\"]")}")); |
| 112 | + return $"\"{name}\":{{{string.Join(',', fields)}}}"; |
| 113 | + } |
| 114 | + |
| 115 | + public static string MakeTypesJson(IDictionary<DataType, Target.TypeInfo> types) |
| 116 | + { |
| 117 | + return string.Join(',', types.Select(t => GetTypeJson(t.Key.ToString(), t.Value))); |
| 118 | + } |
| 119 | + |
| 120 | + public static string MakeGlobalsJson(IEnumerable<(string Name, ulong Value, string? Type)> globals) |
| 121 | + { |
| 122 | + return MakeGlobalsJson(globals.Select(g => (g.Name, (ulong?)g.Value, (uint?)null, g.Type))); |
| 123 | + } |
| 124 | + |
| 125 | + public static string MakeGlobalsJson(IEnumerable<(string Name, ulong? Value, uint? IndirectIndex, string? Type)> globals) |
| 126 | + { |
| 127 | + return string.Join(',', globals.Select(FormatGlobal)); |
| 128 | + |
| 129 | + static string FormatGlobal((string Name, ulong? Value, uint? IndirectIndex, string? Type) global) |
| 130 | + { |
| 131 | + if (global.Value is ulong value) |
| 132 | + { |
| 133 | + return $"\"{global.Name}\": {FormatValue(value, global.Type)}"; |
| 134 | + } |
| 135 | + else if (global.IndirectIndex is uint index) |
| 136 | + { |
| 137 | + return $"\"{global.Name}\": {FormatIndirect(index, global.Type)}"; |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + throw new InvalidOperationException("Global must have a value or indirect index"); |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + static string FormatValue(ulong value, string? type) |
| 146 | + { |
| 147 | + return type is null ? $"{value}" : $"[{value},\"{type}\"]"; |
| 148 | + } |
| 149 | + static string FormatIndirect(uint value, string? type) |
| 150 | + { |
| 151 | + return type is null ? $"[{value}]" : $"[[{value}],\"{type}\"]"; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + #endregion JSON formatting |
| 156 | +} |
0 commit comments