Skip to content

Commit

Permalink
Add build-id checks in test
Browse files Browse the repository at this point in the history
  • Loading branch information
gleocadie committed Nov 27, 2024
1 parent 5ce79d8 commit 157cbca
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string>();

foreach (var frame in frames)
{
var validatedModules = new HashSet<string>();
var moduleName = frame["names"][0]["name"].Value<string>().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<string>().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<byte>()).ToArray();
var age = pdbNode["age"].Value<uint>();

// Open the PE file
using var file = File.OpenRead(moduleName);
using var peReader = new PEReader(file);

Expand All @@ -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<byte>()).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}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ELFSharp" Version="2.17.3" />
<PackageReference Include="Verify.Xunit" Version="14.13.1" />
</ItemGroup>

Expand Down

0 comments on commit 157cbca

Please sign in to comment.