diff --git a/tracer/src/Datadog.Trace.Trimming/build/Datadog.Trace.Trimming.xml b/tracer/src/Datadog.Trace.Trimming/build/Datadog.Trace.Trimming.xml index 9fe755988489..4b3d857538e9 100644 --- a/tracer/src/Datadog.Trace.Trimming/build/Datadog.Trace.Trimming.xml +++ b/tracer/src/Datadog.Trace.Trimming/build/Datadog.Trace.Trimming.xml @@ -184,7 +184,6 @@ - @@ -320,7 +319,6 @@ - @@ -500,12 +498,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - @@ -690,7 +780,6 @@ - @@ -715,7 +804,6 @@ - @@ -728,7 +816,6 @@ - @@ -747,11 +834,9 @@ - - @@ -774,7 +859,6 @@ - @@ -826,7 +910,6 @@ - @@ -834,7 +917,6 @@ - @@ -972,7 +1054,6 @@ - @@ -987,7 +1068,6 @@ - diff --git a/tracer/src/Datadog.Trace/Datadog.Trace.csproj b/tracer/src/Datadog.Trace/Datadog.Trace.csproj index 1fe94e44f7f6..bbf6b3f55345 100644 --- a/tracer/src/Datadog.Trace/Datadog.Trace.csproj +++ b/tracer/src/Datadog.Trace/Datadog.Trace.csproj @@ -100,9 +100,8 @@ - - Vendors/System.Collections.Immutable/**;Vendors/System.Memory/**;Vendors/System.Runtime.CompilerServices.Unsafe/** + Vendors/System.Collections.Immutable/**;Vendors/System.Memory/**;Vendors/System.Private.CoreLib/**;Vendors/System.Reflection.Metadata/**;Vendors/System.Runtime.CompilerServices.Unsafe/** diff --git a/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/CachedItems.cs b/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/CachedItems.cs index d3c06be2c3d2..9cd05fd4100e 100644 --- a/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/CachedItems.cs +++ b/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/CachedItems.cs @@ -11,7 +11,6 @@ using System.Threading; using System.Threading.Tasks; using Datadog.Trace.Vendors.MessagePack; -using Fnv1aHash = Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Internal.Hash; #nullable enable namespace Datadog.Trace.Debugger.ExceptionAutoInstrumentation diff --git a/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/ExceptionNormalizer.cs b/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/ExceptionNormalizer.cs index 32355a2355ae..47d31349abf6 100644 --- a/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/ExceptionNormalizer.cs +++ b/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/ExceptionNormalizer.cs @@ -6,7 +6,6 @@ using System; using System.Collections.Generic; using System.Runtime.CompilerServices; -using Fnv1aHash = Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Internal.Hash; #nullable enable namespace Datadog.Trace.Debugger.ExceptionAutoInstrumentation @@ -33,7 +32,7 @@ internal int NormalizeAndHashException(string exceptionString, string outerExcep throw new ArgumentException(@"Exception string cannot be null or empty", nameof(exceptionString)); } - var fnvHashCode = HashLine(outerExceptionType.AsSpan(), Fnv1aHash.FnvOffsetBias); + var fnvHashCode = HashLine(outerExceptionType.AsSpan(), SimpleHash.FnvOffsetBias); if (innerExceptionType != null) { @@ -93,7 +92,7 @@ protected virtual int HashLine(ReadOnlySpan line, int fnvHashCode) { for (var i = 0; i < line.Length; i++) { - fnvHashCode = Fnv1aHash.Combine((uint)line[i], fnvHashCode); + fnvHashCode = SimpleHash.Combine((uint)line[i], fnvHashCode); } return fnvHashCode; diff --git a/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/ExceptionProbeProcessor.cs b/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/ExceptionProbeProcessor.cs index 9fb03617295a..fa83a9080278 100644 --- a/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/ExceptionProbeProcessor.cs +++ b/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/ExceptionProbeProcessor.cs @@ -16,7 +16,6 @@ using Datadog.Trace.Debugger.Sink.Models; using Datadog.Trace.Debugger.Snapshots; using Datadog.Trace.Logging; -using Fnv1aHash = Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Internal.Hash; #nullable enable namespace Datadog.Trace.Debugger.ExceptionAutoInstrumentation @@ -115,17 +114,17 @@ private bool EnsureLeaveHashComputed() Array.Reverse(installedProbes); } - var hash = Fnv1aHash.FnvOffsetBias; + var hash = SimpleHash.FnvOffsetBias; if (installedProbes.Length != 0) { foreach (var probe in installedProbes) { - hash = Fnv1aHash.Combine(probe.Method.Method.MetadataToken, hash); + hash = SimpleHash.Combine(probe.Method.Method.MetadataToken, hash); } } - return Fnv1aHash.Combine(ExceptionReplayProcessor.Method.Method.MetadataToken, hash); + return SimpleHash.Combine(ExceptionReplayProcessor.Method.Method.MetadataToken, hash); } internal void InvalidateEnterLeave() diff --git a/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/ExceptionReplayProcessor.cs b/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/ExceptionReplayProcessor.cs index 41303e5a4621..6aaae639f33d 100644 --- a/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/ExceptionReplayProcessor.cs +++ b/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/ExceptionReplayProcessor.cs @@ -9,7 +9,6 @@ using Datadog.Trace.Debugger.Instrumentation.Collections; using Datadog.Trace.Debugger.Snapshots; using Datadog.Trace.Logging; -using Fnv1aHash = Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Internal.Hash; #nullable enable namespace Datadog.Trace.Debugger.ExceptionAutoInstrumentation @@ -71,7 +70,7 @@ public bool Process(ref CaptureInfo info, IDebuggerSnapshotC case MethodState.EntryAsync: shadowStack = ShadowStackHolder.EnsureShadowStackEnabled(); var currentFrame = shadowStack.CurrentStackFrameNode; - snapshotCreator.EnterHash = Fnv1aHash.Combine(info.Method.MetadataToken, shadowStack.CurrentStackFrameNode?.EnterSequenceHash ?? Fnv1aHash.FnvOffsetBias); + snapshotCreator.EnterHash = SimpleHash.Combine(info.Method.MetadataToken, shadowStack.CurrentStackFrameNode?.EnterSequenceHash ?? SimpleHash.FnvOffsetBias); if (currentFrame?.Method == info.Method && _isMisleadingMethod) { diff --git a/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/FakeTrackedStackFrameNode.cs b/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/FakeTrackedStackFrameNode.cs index 5f0670b63fc9..4ccc775a9849 100644 --- a/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/FakeTrackedStackFrameNode.cs +++ b/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/FakeTrackedStackFrameNode.cs @@ -10,7 +10,6 @@ using Datadog.Trace.Debugger.Expressions; using Datadog.Trace.Debugger.Instrumentation.Collections; using Datadog.Trace.Debugger.Snapshots; -using Fnv1aHash = Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Internal.Hash; #nullable enable namespace Datadog.Trace.Debugger.ExceptionAutoInstrumentation @@ -35,7 +34,7 @@ protected override int ComputeLeaveSequenceHash() return firstChild.LeaveSequenceHash; } - return Fnv1aHash.Combine(Method.MetadataToken, Fnv1aHash.FnvOffsetBias); + return SimpleHash.Combine(Method.MetadataToken, SimpleHash.FnvOffsetBias); } } diff --git a/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/TrackedStackFrameNode.cs b/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/TrackedStackFrameNode.cs index 49a8ccc0d40d..91aefb787724 100644 --- a/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/TrackedStackFrameNode.cs +++ b/tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/TrackedStackFrameNode.cs @@ -10,7 +10,6 @@ using Datadog.Trace.Debugger.Expressions; using Datadog.Trace.Debugger.Instrumentation.Collections; using Datadog.Trace.Debugger.Snapshots; -using Fnv1aHash = Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Internal.Hash; #nullable enable namespace Datadog.Trace.Debugger.ExceptionAutoInstrumentation @@ -54,7 +53,7 @@ public int SequenceHash { get { - return Fnv1aHash.Combine(EnterSequenceHash, LeaveSequenceHash); + return SimpleHash.Combine(EnterSequenceHash, LeaveSequenceHash); } } @@ -222,7 +221,7 @@ internal void AddScopeMember(string name, Type type, T value, ScopeMemberKind protected virtual int ComputeEnterSequenceHash() { - return Fnv1aHash.Combine(Method.MetadataToken, _parent?.EnterSequenceHash ?? Fnv1aHash.FnvOffsetBias); + return SimpleHash.Combine(Method.MetadataToken, _parent?.EnterSequenceHash ?? SimpleHash.FnvOffsetBias); } /// @@ -237,10 +236,10 @@ protected virtual int ComputeLeaveSequenceHash() if (ActiveChildNodes?.Count > 0) { var firstChild = ActiveChildNodes.First(); - return Fnv1aHash.Combine(Method.MetadataToken, firstChild.LeaveSequenceHash); + return SimpleHash.Combine(Method.MetadataToken, firstChild.LeaveSequenceHash); } - return Fnv1aHash.Combine(Method.MetadataToken, Fnv1aHash.FnvOffsetBias); + return SimpleHash.Combine(Method.MetadataToken, SimpleHash.FnvOffsetBias); } } diff --git a/tracer/src/Datadog.Trace/Debugger/SimpleHash.cs b/tracer/src/Datadog.Trace/Debugger/SimpleHash.cs new file mode 100644 index 000000000000..ba09556b098d --- /dev/null +++ b/tracer/src/Datadog.Trace/Debugger/SimpleHash.cs @@ -0,0 +1,36 @@ +// +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. +// + +#nullable enable + +// A vendored version of System.Reflection.Internal.Hash + +namespace Datadog.Trace.Debugger; + +internal static class SimpleHash +{ + /// + /// The offset bias value used in the FNV-1a algorithm + /// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function + /// Note however, that the Combine methods in this class + /// do _not_ implement the FNV-1a algorithm. + /// + internal const int FnvOffsetBias = unchecked((int)2166136261); + + internal static int Combine(int data, int initialHash) + { + return unchecked((initialHash * (int)0xA5555529) + data); + } + + internal static int Combine(uint data, int initialHash) + { + return unchecked((initialHash * (int)0xA5555529) + (int)data); + } + + internal static int Combine(bool data, int initialHash) + { + return Combine(initialHash, data ? 1 : 0); + } +} diff --git a/tracer/src/Datadog.Trace/Debugger/SpanCodeOrigin/EndpointDetector.cs b/tracer/src/Datadog.Trace/Debugger/SpanCodeOrigin/EndpointDetector.cs index 1dba428e478d..3edd07bda21d 100644 --- a/tracer/src/Datadog.Trace/Debugger/SpanCodeOrigin/EndpointDetector.cs +++ b/tracer/src/Datadog.Trace/Debugger/SpanCodeOrigin/EndpointDetector.cs @@ -11,8 +11,14 @@ using System.Reflection; using Datadog.Trace.Debugger.Symbols; using Datadog.Trace.Pdb; + +#if NETCOREAPP +using System.Reflection.Metadata; +using System.Reflection.Metadata.Ecma335; +#else using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Metadata; using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Metadata.Ecma335; +#endif namespace Datadog.Trace.Debugger.SpanCodeOrigin; diff --git a/tracer/src/Datadog.Trace/Debugger/Symbols/AsyncStateMachineAttributeTypeProvider.cs b/tracer/src/Datadog.Trace/Debugger/Symbols/AsyncStateMachineAttributeTypeProvider.cs index 1c0ba101a58b..b9258eea678e 100644 --- a/tracer/src/Datadog.Trace/Debugger/Symbols/AsyncStateMachineAttributeTypeProvider.cs +++ b/tracer/src/Datadog.Trace/Debugger/Symbols/AsyncStateMachineAttributeTypeProvider.cs @@ -6,7 +6,12 @@ #nullable enable using System.Runtime.CompilerServices; + +#if NETCOREAPP +using System.Reflection.Metadata; +#else using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Metadata; +#endif namespace Datadog.Trace.Debugger.Symbols; diff --git a/tracer/src/Datadog.Trace/Debugger/Symbols/SymbolExtractor.cs b/tracer/src/Datadog.Trace/Debugger/Symbols/SymbolExtractor.cs index 976999328b88..46e6072b2fd6 100644 --- a/tracer/src/Datadog.Trace/Debugger/Symbols/SymbolExtractor.cs +++ b/tracer/src/Datadog.Trace/Debugger/Symbols/SymbolExtractor.cs @@ -10,8 +10,14 @@ using Datadog.Trace.Debugger.Symbols.Model; using Datadog.Trace.Logging; using Datadog.Trace.Pdb; -using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Metadata; // keep vendored versions for now because we access internal members + +#if NETCOREAPP +using System.Reflection.Metadata; +using System.Reflection.Metadata.Ecma335; +#else +using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Metadata; using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Metadata.Ecma335; +#endif namespace Datadog.Trace.Debugger.Symbols { @@ -165,7 +171,7 @@ private bool TryGetClassSymbols(TypeDefinitionHandle typeDefinitionHandle, out M return false; } - if (DatadogMetadataReader.IsCompilerGeneratedAttributeDefinedOnType(typeDefinitionHandle.RowId)) + if (DatadogMetadataReader.IsCompilerGeneratedAttributeDefinedOnType(MetadataTokens.GetToken(typeDefinitionHandle))) { return false; } @@ -350,7 +356,7 @@ private void PopulateNestedNotCompileGeneratedClassScope(ImmutableArray= closureMethods!.Length) @@ -726,7 +732,7 @@ void PopulateClosureMethod(MethodDefinition generatedMethod, TypeDefinition owne } } - protected virtual bool TryCreateMethodScopeForGeneratedMethod(MethodDefinition method, MethodDefinition generatedMethod, TypeDefinition nestedType, out Model.Scope methodScope) + protected virtual bool TryCreateMethodScopeForGeneratedMethod(MethodDefinitionHandle methodHandle, MethodDefinition method, MethodDefinitionHandle generatedMethodHandle, MethodDefinition generatedMethod, TypeDefinition nestedType, out Model.Scope methodScope) { methodScope = default; return false; diff --git a/tracer/src/Datadog.Trace/Debugger/Symbols/SymbolPdbExtractor.cs b/tracer/src/Datadog.Trace/Debugger/Symbols/SymbolPdbExtractor.cs index 6533a0a6154e..7e7eeff8ae1a 100644 --- a/tracer/src/Datadog.Trace/Debugger/Symbols/SymbolPdbExtractor.cs +++ b/tracer/src/Datadog.Trace/Debugger/Symbols/SymbolPdbExtractor.cs @@ -9,8 +9,14 @@ using System.Runtime.InteropServices; using Datadog.Trace.Debugger.Symbols.Model; using Datadog.Trace.Pdb; -using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Metadata; // keep vendored versions for now because we access internal members + +#if NETCOREAPP +using System.Reflection.Metadata; +using System.Reflection.Metadata.Ecma335; +#else +using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Metadata; using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Metadata.Ecma335; +#endif namespace Datadog.Trace.Debugger.Symbols; @@ -23,14 +29,14 @@ internal SymbolPdbExtractor(DatadogMetadataReader pdbReader, string assemblyName { } - protected override bool TryCreateMethodScope(TypeDefinition type, MethodDefinition method, out Model.Scope methodScope) + protected override bool TryCreateMethodScope(TypeDefinition type, MethodDefinitionHandle methodHandle, MethodDefinition method, out Model.Scope methodScope) { - if (!base.TryCreateMethodScope(type, method, out methodScope)) + if (!base.TryCreateMethodScope(type, methodHandle, method, out methodScope)) { return false; } - using var memory = DatadogMetadataReader.GetMethodSequencePointsAsMemoryOwner(MetadataTokens.GetToken(method.Handle), false, out var count); + using var memory = DatadogMetadataReader.GetMethodSequencePointsAsMemoryOwner(MetadataTokens.GetToken(methodHandle), false, out var count); if (memory == null || count == 0) { return true; @@ -55,7 +61,7 @@ protected override bool TryCreateMethodScope(TypeDefinition type, MethodDefiniti methodScope.LanguageSpecifics = ls; } - var localScopes = GetLocalSymbols(method, sequencePoints, methodScope); + var localScopes = GetLocalSymbols(methodHandle, method, sequencePoints, methodScope); methodScope.Scopes = ConcatMethodScopes(methodScope.Scopes ?? null, localScopes); return true; } @@ -88,7 +94,7 @@ private SourceLocationInfo GetSourceLocationInfo(ReadOnlySpan sequencePoints, Model.Scope methodScope) + private Model.Scope[]? GetLocalSymbols(MethodDefinitionHandle methodHandle, MethodDefinition methodDefinition, ReadOnlySpan sequencePoints, Model.Scope methodScope) { List? scopes = null; var generatedClassPrefix = GeneratedClassPrefix.AsSpan(); - var methodToken = MetadataTokens.GetToken(methodDefinition.Handle); + var methodToken = MetadataTokens.GetToken(methodHandle); if (DatadogMetadataReader.GetAsyncAndClosureCustomDebugInfo(methodToken).StateMachineHoistedLocal && DatadogMetadataReader.IsCompilerGeneratedAttributeDefinedOnType(MetadataTokens.GetToken(methodDefinition.GetDeclaringType()))) { diff --git a/tracer/src/Datadog.Trace/Debugger/Symbols/SymbolsExtensions.cs b/tracer/src/Datadog.Trace/Debugger/Symbols/SymbolsExtensions.cs index a06e68544e11..437249cfdd7e 100644 --- a/tracer/src/Datadog.Trace/Debugger/Symbols/SymbolsExtensions.cs +++ b/tracer/src/Datadog.Trace/Debugger/Symbols/SymbolsExtensions.cs @@ -6,9 +6,14 @@ #nullable enable using System.Reflection; -using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Metadata; // keep vendored versions for now because we access internal members using Datadog.Trace.Vendors.dnlib.DotNet.Pdb.Symbols; +#if NETCOREAPP +using System.Reflection.Metadata; +#else +using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Metadata; +#endif + namespace Datadog.Trace.Debugger.Symbols; internal static class SymbolsExtensions diff --git a/tracer/src/Datadog.Trace/Debugger/Symbols/TypeProvider.cs b/tracer/src/Datadog.Trace/Debugger/Symbols/TypeProvider.cs index 8fd6d0609c69..a6e3f42a1a35 100644 --- a/tracer/src/Datadog.Trace/Debugger/Symbols/TypeProvider.cs +++ b/tracer/src/Datadog.Trace/Debugger/Symbols/TypeProvider.cs @@ -6,7 +6,12 @@ #nullable enable using Datadog.Trace.Util; -using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Metadata; // keep vendored versions for now because we access internal members + +#if NETCOREAPP +using System.Reflection.Metadata; +#else +using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Metadata; +#endif namespace Datadog.Trace.Debugger.Symbols { diff --git a/tracer/src/Datadog.Trace/Iast/IastModule.cs b/tracer/src/Datadog.Trace/Iast/IastModule.cs index 0fcd3d9d3de5..54cb2dce91eb 100644 --- a/tracer/src/Datadog.Trace/Iast/IastModule.cs +++ b/tracer/src/Datadog.Trace/Iast/IastModule.cs @@ -28,7 +28,6 @@ using Datadog.Trace.Logging; using Datadog.Trace.Sampling; using Datadog.Trace.Tagging; -using Datadog.Trace.VendoredMicrosoftCode.System; using static Datadog.Trace.Configuration.ConfigurationKeys; using static Datadog.Trace.Telemetry.Metrics.MetricTags; diff --git a/tracer/src/Datadog.Trace/OpenTelemetry/OtlpMapper.cs b/tracer/src/Datadog.Trace/OpenTelemetry/OtlpMapper.cs index 3b4e68c7beee..9c89fc7934be 100644 --- a/tracer/src/Datadog.Trace/OpenTelemetry/OtlpMapper.cs +++ b/tracer/src/Datadog.Trace/OpenTelemetry/OtlpMapper.cs @@ -14,7 +14,6 @@ using Datadog.Trace.Processors; using Datadog.Trace.Tagging; using Datadog.Trace.Telemetry.Metrics; -using Datadog.Trace.VendoredMicrosoftCode.System; using Datadog.Trace.Vendors.Datadog.Sketches; using Datadog.Trace.Vendors.MessagePack; using Datadog.Trace.Vendors.Newtonsoft.Json; diff --git a/tracer/src/Datadog.Trace/PDBs/DatadogMetadataReader.cs b/tracer/src/Datadog.Trace/PDBs/DatadogMetadataReader.cs index 2e181c82ec31..7b5bd2c7d703 100644 --- a/tracer/src/Datadog.Trace/PDBs/DatadogMetadataReader.cs +++ b/tracer/src/Datadog.Trace/PDBs/DatadogMetadataReader.cs @@ -15,10 +15,15 @@ using Datadog.Trace.Debugger.Symbols; using Datadog.Trace.Logging; -// keep vendored versions for now because we access internal members +#if NETCOREAPP +using System.Reflection.Metadata; +using System.Reflection.Metadata.Ecma335; +using System.Reflection.PortableExecutable; +#else using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Metadata; using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Metadata.Ecma335; using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.PortableExecutable; +#endif namespace Datadog.Trace.Pdb { @@ -67,6 +72,11 @@ private DatadogMetadataReader(PEReader peReader, MetadataReader metadataReader, internal static int RidOf(int metadataToken) => metadataToken & RidMask; + internal static MethodDefinitionHandle GetMethodDefHandle(int methodToken) + { + return MetadataTokens.MethodDefinitionHandle(RidOf(methodToken)); + } + internal static DatadogMetadataReader? CreatePdbReader(Assembly? assembly) { if (assembly == null || string.IsNullOrEmpty(assembly.Location)) @@ -250,16 +260,17 @@ private int GetLocalVariablesCount(MethodDefinition method) if (PdbReader != null) { - var methodDef = GetMethodDef(methodToken); - if (methodDef.Handle.IsNil) + var methodDefHandle = GetMethodDefHandle(methodToken); + if (methodDefHandle.IsNil) { return null; } - MethodDebugInformation methodDebugInformation = PdbReader.GetMethodDebugInformation(methodDef.Handle.ToDebugInformationHandle()); + var methodDef = GetMethodDef(methodDefHandle); + MethodDebugInformation methodDebugInformation = PdbReader.GetMethodDebugInformation(methodDefHandle.ToDebugInformationHandle()); if (methodDebugInformation.SequencePointsBlob.IsNil && searchMoveNext) { - var moveNext = GetMoveNextMethod(methodDef); + var moveNext = GetMoveNextMethod(methodDefHandle, methodDef); if (moveNext.IsNil) { return null; @@ -315,16 +326,17 @@ private int GetLocalVariablesCount(MethodDefinition method) if (PdbReader != null) { - var methodDef = GetMethodDef(methodToken); - if (methodDef.Handle.IsNil) + var methodDefHandle = GetMethodDefHandle(methodToken); + var methodDef = GetMethodDef(methodDefHandle); + if (methodDefHandle.IsNil) { return null; } - MethodDebugInformation methodDebugInformation = PdbReader.GetMethodDebugInformation(methodDef.Handle.ToDebugInformationHandle()); + MethodDebugInformation methodDebugInformation = PdbReader.GetMethodDebugInformation(methodDefHandle.ToDebugInformationHandle()); if (methodDebugInformation.SequencePointsBlob.IsNil && searchMoveNext) { - var moveNext = GetMoveNextMethod(methodDef); + var moveNext = GetMoveNextMethod(methodDefHandle, methodDef); if (moveNext.IsNil) { return null; @@ -355,7 +367,7 @@ private int GetLocalVariablesCount(MethodDefinition method) return null; } - private MethodDefinitionHandle GetMoveNextMethod(MethodDefinition methodDef) + private MethodDefinitionHandle GetMoveNextMethod(MethodDefinitionHandle methodDefHandle, MethodDefinition methodDef) { if (methodDef.GetDeclaringType().IsNil) { @@ -365,7 +377,7 @@ private MethodDefinitionHandle GetMoveNextMethod(MethodDefinition methodDef) TypeDefinitionHandle nestedTypeHandle = default; var enclosingType = MetadataReader.GetTypeDefinition(methodDef.GetDeclaringType()); var provider = new AsyncStateMachineAttributeTypeProvider(); - foreach (var attributeHandle in MetadataReader.GetCustomAttributes(methodDef.Handle)) + foreach (var attributeHandle in MetadataReader.GetCustomAttributes(methodDefHandle)) { if (attributeHandle.IsNil) { @@ -449,7 +461,6 @@ private MethodDefinitionHandle GetMoveNextMethod(MethodDefinition methodDef) return null; } - const int methodDefTablePrefix = 0x06000000; foreach (MethodDefinitionHandle methodDefinitionHandle in MetadataReader.MethodDefinitions) { MethodDebugInformation methodDebugInformation = PdbReader.GetMethodDebugInformation(methodDefinitionHandle); @@ -472,7 +483,7 @@ private MethodDefinitionHandle GetMoveNextMethod(MethodDefinition methodDef) (column.HasValue == false || (sequencePoint.StartColumn <= column && sequencePoint.EndColumn >= column))) { byteCodeOffset = sequencePoint.Offset; - return methodDefTablePrefix | methodDefinitionHandle.RowId; + return MetadataTokens.GetToken(methodDefinitionHandle); } } } @@ -555,7 +566,8 @@ private MethodDefinitionHandle GetMoveNextMethod(MethodDefinition methodDef) return null; } - var method = GetMethodDef(methodToken); + var methodDefHandle = GetMethodDefHandle(methodToken); + var method = GetMethodDef(methodDefHandle); int localsCount = 0; var methodLocalsCount = GetLocalVariablesCount(method); if (methodLocalsCount == 0) @@ -572,7 +584,7 @@ private MethodDefinitionHandle GetMoveNextMethod(MethodDefinition methodDef) return null; } - foreach (var scopeHandle in PdbReader.GetLocalScopes(method.Handle.ToDebugInformationHandle())) + foreach (var scopeHandle in PdbReader.GetLocalScopes(methodDefHandle.ToDebugInformationHandle())) { var localScope = PdbReader.GetLocalScope(scopeHandle); foreach (var localVarHandle in localScope.GetLocalVariables()) @@ -596,7 +608,7 @@ private MethodDefinitionHandle GetMoveNextMethod(MethodDefinition methodDef) if (localsCount == 0 && searchMoveNext) { - var moveNext = GetMoveNextMethod(method); + var moveNext = GetMoveNextMethod(methodDefHandle, method); if (!moveNext.IsNil) { return GetLocalVariableNames(MetadataTokens.GetToken(moveNext), false); @@ -616,7 +628,7 @@ internal CustomDebugInfoAsyncAndClosure GetAsyncAndClosureCustomDebugInfo(int me CustomDebugInfoAsyncAndClosure cdiAsyncAndClosure = default; if (PdbReader != null) { - var methodHandle = MethodDefinitionHandle.FromRowId(RidOf(methodToken)); + var methodHandle = MetadataTokens.MethodDefinitionHandle(RidOf(methodToken)); if (methodHandle.IsNil) { return default; @@ -656,14 +668,15 @@ internal CustomDebugInfoAsyncAndClosure GetAsyncAndClosureCustomDebugInfo(int me internal bool IsCompilerGeneratedAttributeDefinedOnMethod(int methodToken) { - var method = GetMethodDef(methodToken); + var methodDefHandle = GetMethodDefHandle(methodToken); + var method = GetMethodDef(methodDefHandle); var attributes = method.GetCustomAttributes(); return IsCompilerGeneratedAttributeDefine(attributes); } internal bool IsCompilerGeneratedAttributeDefinedOnType(int typeToken) { - var nestedType = MetadataReader.GetTypeDefinition(TypeDefinitionHandle.FromRowId(RidOf(typeToken))); + var nestedType = MetadataReader.GetTypeDefinition(MetadataTokens.TypeDefinitionHandle(RidOf(typeToken))); var attributes = nestedType.GetCustomAttributes(); return IsCompilerGeneratedAttributeDefine(attributes); } @@ -745,9 +758,9 @@ internal bool IsAsyncMethod(CustomAttributeHandleCollection attributes) } } - internal MethodDefinition GetMethodDef(int methodToken) + internal MethodDefinition GetMethodDef(MethodDefinitionHandle handle) { - return MetadataReader.GetMethodDefinition(MethodDefinitionHandle.FromRowId(RidOf(methodToken))); + return MetadataReader.GetMethodDefinition(handle); } internal ImmutableArray? GetLocalSymbols(int methodToken, ReadOnlySpan sequencePoints, bool searchMoveNext) @@ -760,7 +773,8 @@ internal MethodDefinition GetMethodDef(int methodToken) ImmutableArray.Builder? localScopes = default; if (PdbReader != null) { - MethodDefinition method = GetMethodDef(methodToken); + var methodDefHandle = GetMethodDefHandle(methodToken); + var method = GetMethodDef(methodDefHandle); var methodLocalsCount = GetLocalVariablesCount(method); if (methodLocalsCount == 0) { @@ -776,7 +790,7 @@ internal MethodDefinition GetMethodDef(int methodToken) var localTypes = signature.Value.DecodeLocalSignature(new TypeProvider(false), 0); localScopes = ImmutableArray.CreateBuilder(); - foreach (var scopeHandle in PdbReader.GetLocalScopes(method.Handle.ToDebugInformationHandle())) + foreach (var scopeHandle in PdbReader.GetLocalScopes(methodDefHandle.ToDebugInformationHandle())) { var localScope = PdbReader.GetLocalScope(scopeHandle); var locals = localScope.GetLocalVariables(); @@ -884,7 +898,7 @@ internal MethodDefinition GetMethodDef(int methodToken) if (localScopes.Count == 0 && searchMoveNext) { - var moveNext = GetMoveNextMethod(method); + var moveNext = GetMoveNextMethod(methodDefHandle, method); return GetLocalSymbols(MetadataTokens.GetToken(moveNext), sequencePoints, false); } } @@ -894,7 +908,8 @@ internal MethodDefinition GetMethodDef(int methodToken) internal bool HasMethodBody(int methodToken) { - var method = GetMethodDef(methodToken); + var methodDefHandle = GetMethodDefHandle(methodToken); + var method = GetMethodDef(methodDefHandle); if (method.RelativeVirtualAddress == 0) { // Method has no RVA (typically abstract or extern method) diff --git a/tracer/test/Datadog.Trace.Tests/Snapshots/PublicApiTests.Datadog.Trace.AssemblyReferencesHaveNotChanged.net6.0.verified.txt b/tracer/test/Datadog.Trace.Tests/Snapshots/PublicApiTests.Datadog.Trace.AssemblyReferencesHaveNotChanged.net6.0.verified.txt index 9de80e25c8dc..a5dc8e25804e 100644 --- a/tracer/test/Datadog.Trace.Tests/Snapshots/PublicApiTests.Datadog.Trace.AssemblyReferencesHaveNotChanged.net6.0.verified.txt +++ b/tracer/test/Datadog.Trace.Tests/Snapshots/PublicApiTests.Datadog.Trace.AssemblyReferencesHaveNotChanged.net6.0.verified.txt @@ -38,6 +38,7 @@ System.ObjectModel, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d System.Reflection.Emit, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Reflection.Emit.ILGeneration, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Reflection.Emit.Lightweight, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a +System.Reflection.Metadata, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Reflection.Primitives, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a diff --git a/tracer/test/Datadog.Trace.Tests/Snapshots/PublicApiTests.Datadog.Trace.AssemblyReferencesHaveNotChanged.netcoreapp3.1.verified.txt b/tracer/test/Datadog.Trace.Tests/Snapshots/PublicApiTests.Datadog.Trace.AssemblyReferencesHaveNotChanged.netcoreapp3.1.verified.txt index eb7c40fbc577..537dceef03f5 100644 --- a/tracer/test/Datadog.Trace.Tests/Snapshots/PublicApiTests.Datadog.Trace.AssemblyReferencesHaveNotChanged.netcoreapp3.1.verified.txt +++ b/tracer/test/Datadog.Trace.Tests/Snapshots/PublicApiTests.Datadog.Trace.AssemblyReferencesHaveNotChanged.netcoreapp3.1.verified.txt @@ -42,6 +42,7 @@ System.ObjectModel, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d System.Reflection.Emit, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Reflection.Emit.ILGeneration, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Reflection.Emit.Lightweight, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a +System.Reflection.Metadata, Version=1.4.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Reflection.Primitives, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Resources.ResourceManager, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a