From 157cbcac80b5171481e999c1fd41667069737714 Mon Sep 17 00:00:00 2001 From: Gregory LEOCADIE Date: Wed, 27 Nov 2024 23:46:29 +0100 Subject: [PATCH] Add build-id checks in test --- .../CreatedumpTests.cs | 47 ++++++++++++------- ...Trace.Tools.dd_dotnet.ArtifactTests.csproj | 1 + 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/tracer/test/Datadog.Trace.Tools.dd_dotnet.ArtifactTests/CreatedumpTests.cs b/tracer/test/Datadog.Trace.Tools.dd_dotnet.ArtifactTests/CreatedumpTests.cs index 8979f2ce96f6..40446d9d34e6 100644 --- a/tracer/test/Datadog.Trace.Tools.dd_dotnet.ArtifactTests/CreatedumpTests.cs +++ b/tracer/test/Datadog.Trace.Tools.dd_dotnet.ArtifactTests/CreatedumpTests.cs @@ -14,6 +14,8 @@ using Datadog.Trace.Telemetry; using Datadog.Trace.Telemetry.DTOs; using Datadog.Trace.TestHelpers; +using ELFSharp.ELF; +using ELFSharp.ELF.Sections; using FluentAssertions; using FluentAssertions.Execution; using Newtonsoft.Json.Linq; @@ -549,28 +551,27 @@ void ValidateStacktrace(JToken callstack) frame.Should().NotBeNull($"couldn't find expected frame {expectedFrame}"); } - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + var validatedModules = new HashSet(); + + foreach (var frame in frames) { - var validatedModules = new HashSet(); + var moduleName = frame["names"][0]["name"].Value().Split('!').First(); - // Validate PDBs - foreach (var frame in frames) + if (moduleName.Length > 0 && !moduleName.StartsWith("<") && Path.IsPathRooted(moduleName)) { - // Open the PE file - var moduleName = frame["names"][0]["name"].Value().Split('!').First(); - - if (moduleName.Length > 0 && !moduleName.StartsWith("<") && Path.IsPathRooted(moduleName)) + if (!validatedModules.Add(moduleName)) { - if (!validatedModules.Add(moduleName)) - { - continue; - } + continue; + } + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + // Validate PDBs var pdbNode = frame["normalized_ip"]["meta"]["Pdb"]; - var hash = ((JArray)pdbNode["guid"]).Select(g => g.Value()).ToArray(); var age = pdbNode["age"].Value(); + // Open the PE file using var file = File.OpenRead(moduleName); using var peReader = new PEReader(file); @@ -581,18 +582,28 @@ void ValidateStacktrace(JToken callstack) age.Should().Be(unchecked((uint)pdbInfo.Age)); hash.Should().Equal(pdbInfo.Guid.ToByteArray()); } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + // Validate sofile + var elfNode = frame["normalized_ip"]["meta"]["Elf"]; + var buildId = ((JArray)elfNode["build_id"]).Select(g => g.Value()).ToArray(); + + using var elf = ELFReader.Load(moduleName); + var buildIdNote = elf.GetSection(".note.gnu.build-id") as INoteSection; + buildId.Should().Equal(buildIdNote.Description); + } } + } - validatedModules.Should().NotBeEmpty(); + validatedModules.Should().NotBeEmpty(); #if NETFRAMEWORK - var clrModuleName = "clr.dll"; + var clrModuleName = "clr.dll"; #else - var clrModuleName = "coreclr.dll"; + var clrModuleName = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "libcoreclr.so" : "coreclr.dll"; #endif - validatedModules.Should().ContainMatch($@"*\{clrModuleName}"); - } + validatedModules.Should().ContainMatch($@"*\{clrModuleName}"); } } diff --git a/tracer/test/Datadog.Trace.Tools.dd_dotnet.ArtifactTests/Datadog.Trace.Tools.dd_dotnet.ArtifactTests.csproj b/tracer/test/Datadog.Trace.Tools.dd_dotnet.ArtifactTests/Datadog.Trace.Tools.dd_dotnet.ArtifactTests.csproj index 9253b25c11f7..3f17fb174ecb 100644 --- a/tracer/test/Datadog.Trace.Tools.dd_dotnet.ArtifactTests/Datadog.Trace.Tools.dd_dotnet.ArtifactTests.csproj +++ b/tracer/test/Datadog.Trace.Tools.dd_dotnet.ArtifactTests/Datadog.Trace.Tools.dd_dotnet.ArtifactTests.csproj @@ -30,6 +30,7 @@ +