Skip to content

Commit c67eabd

Browse files
committed
Check for minimum address.
1 parent 5fd7ed8 commit c67eabd

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

src/BenchmarkDotNet/Disassemblers/ClrMdV2Disassembler.cs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,36 @@
88
using System.Linq;
99
using System.Text.RegularExpressions;
1010
using BenchmarkDotNet.Portability;
11+
using BenchmarkDotNet.Helpers;
1112

1213
namespace BenchmarkDotNet.Disassemblers
1314
{
1415
// This Disassembler uses ClrMd v2x. Please keep it in sync with ClrMdV1Disassembler (if possible).
1516
internal abstract class ClrMdV2Disassembler
1617
{
18+
private static readonly ulong MinValidAddress = GetMinValidAddress();
19+
20+
private static ulong GetMinValidAddress()
21+
{
22+
// https://github.com/dotnet/BenchmarkDotNet/pull/2413#issuecomment-1688100117
23+
if (RuntimeInformation.IsWindows())
24+
return 65536;
25+
if (RuntimeInformation.IsMacOS())
26+
return RuntimeInformation.GetCurrentPlatform() switch
27+
{
28+
Environments.Platform.X86 or Environments.Platform.X64 => 4096,
29+
Environments.Platform.Arm64 => 0x100000000,
30+
_ => throw new NotSupportedException($"{RuntimeInformation.GetCurrentPlatform()} is not supported")
31+
};
32+
if (RuntimeInformation.IsLinux())
33+
{
34+
string? minAddrResult = ProcessHelper.RunAndReadOutput("/bin/bash", "sudo cat /proc/sys/vm/mmap_min_addr");
35+
ulong.TryParse(minAddrResult, out ulong minAddress);
36+
return minAddress;
37+
}
38+
throw new NotSupportedException($"{System.Runtime.InteropServices.RuntimeInformation.OSDescription} is not supported");
39+
}
40+
1741
internal DisassemblyResult AttachAndDisassemble(Settings settings)
1842
{
1943
using (var dataTarget = DataTarget.AttachToProcess(
@@ -242,8 +266,10 @@ protected static bool TryReadNativeCodeAddresses(ClrRuntime runtime, ClrMethod m
242266

243267
protected void TryTranslateAddressToName(ulong address, bool isAddressPrecodeMD, State state, bool isIndirectCallOrJump, int depth, ClrMethod currentMethod)
244268
{
245-
// 0 or -1 (ulong.MaxValue) addresses are invalid, and will crash the runtime in older runtimes. https://github.com/dotnet/runtime/pull/90794
246-
if (address == 0 || address == ulong.MaxValue)
269+
// -1 (ulong.MaxValue) address is invalid, and will crash the runtime in older runtimes. https://github.com/dotnet/runtime/pull/90794
270+
// 0 is NULL and therefore never valid.
271+
// Addresses less than the minimum virtual address are also invalid.
272+
if (address == ulong.MaxValue || address == 0 || address < MinValidAddress)
247273
return;
248274

249275
if (state.AddressToNameMapping.ContainsKey(address))
@@ -259,7 +285,7 @@ protected void TryTranslateAddressToName(ulong address, bool isAddressPrecodeMD,
259285
}
260286

261287
var method = runtime.GetMethodByInstructionPointer(address);
262-
if (method is null && (address & ((uint)runtime.DataTarget.DataReader.PointerSize - 1)) == 0)
288+
if (method is null && (address & ((uint) runtime.DataTarget.DataReader.PointerSize - 1)) == 0)
263289
{
264290
if (runtime.DataTarget.DataReader.ReadPointer(address, out ulong newAddress) && newAddress > ushort.MaxValue)
265291
{
@@ -275,28 +301,24 @@ protected void TryTranslateAddressToName(ulong address, bool isAddressPrecodeMD,
275301

276302
if (method is null)
277303
{
278-
if (isAddressPrecodeMD)
304+
var methodDescriptor = runtime.GetMethodByHandle(address);
305+
if (methodDescriptor is not null)
279306
{
280-
var methodDescriptor = runtime.GetMethodByHandle(address);
281-
if (!(methodDescriptor is null))
307+
if (isAddressPrecodeMD)
282308
{
283-
if (isAddressPrecodeMD)
284-
{
285-
state.AddressToNameMapping.Add(address, $"Precode of {methodDescriptor.Signature}");
286-
}
287-
else
288-
{
289-
state.AddressToNameMapping.Add(address, $"MD_{methodDescriptor.Signature}");
290-
}
291-
return;
309+
state.AddressToNameMapping.Add(address, $"Precode of {methodDescriptor.Signature}");
292310
}
311+
else
312+
{
313+
state.AddressToNameMapping.Add(address, $"MD_{methodDescriptor.Signature}");
314+
}
315+
return;
293316
}
294317

295318
var methodTableName = runtime.DacLibrary.SOSDacInterface.GetMethodTableName(address);
296319
if (!string.IsNullOrEmpty(methodTableName))
297320
{
298321
state.AddressToNameMapping.Add(address, $"MT_{methodTableName}");
299-
return;
300322
}
301323
return;
302324
}

src/BenchmarkDotNet/Disassemblers/IntelDisassembler.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,7 @@ protected override IEnumerable<Asm> Decode(byte[] code, ulong startAddress, Stat
8181
}
8282
}
8383
}
84-
85-
if (address > ushort.MaxValue)
86-
{
87-
TryTranslateAddressToName(address, isPrestubMD, state, isIndirect, depth, currentMethod);
88-
}
84+
TryTranslateAddressToName(address, isPrestubMD, state, isIndirect, depth, currentMethod);
8985
}
9086

9187
yield return new IntelAsm

0 commit comments

Comments
 (0)