From 0755ca15f78437496771cc9e81e26cda51e8c59e Mon Sep 17 00:00:00 2001 From: Tell-Shanks Date: Fri, 17 Jul 2026 02:58:31 +0300 Subject: [PATCH] core: report what occupies a fixed-address allocation on failure (#278) * Implement DescribeAddressForDiagnostics method Added a method to describe the state of a memory address for diagnostics. * core: enrich allocation failure exception with host region diagnostic --- src/SharpEmu.Core/Loader/SelfLoader.cs | 3 ++- .../Memory/PhysicalVirtualMemory.cs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/SharpEmu.Core/Loader/SelfLoader.cs b/src/SharpEmu.Core/Loader/SelfLoader.cs index e804dc91..814d1ef0 100644 --- a/src/SharpEmu.Core/Loader/SelfLoader.cs +++ b/src/SharpEmu.Core/Loader/SelfLoader.cs @@ -197,8 +197,9 @@ public sealed class SelfLoader : ISelfLoader { if (!physicalVm.TryAllocateAtExact(imageBase, totalImageSize, executable: true, out var allocatedBase)) { + var reason = physicalVm.DescribeAddressForDiagnostics(imageBase); throw new InvalidOperationException( - $"Could not allocate main image at required base 0x{imageBase:X16} (size=0x{totalImageSize:X})."); + $"Could not allocate main image at required base 0x{imageBase:X16} (size=0x{totalImageSize:X}): {reason}."); } imageBase = allocatedBase; diff --git a/src/SharpEmu.Core/Memory/PhysicalVirtualMemory.cs b/src/SharpEmu.Core/Memory/PhysicalVirtualMemory.cs index 4e88262e..06535ea5 100644 --- a/src/SharpEmu.Core/Memory/PhysicalVirtualMemory.cs +++ b/src/SharpEmu.Core/Memory/PhysicalVirtualMemory.cs @@ -198,6 +198,24 @@ public sealed unsafe class PhysicalVirtualMemory : IVirtualMemory, IGuestMemoryA return true; } + public string DescribeAddressForDiagnostics(ulong address) + { + if (!_hostMemory.Query(address, out var info)) + { + return "unable to query host memory at this address"; + } + + return info.State switch + { + HostRegionState.Free => "address reports free, but the exact-address reservation still failed", + HostRegionState.Reserved => + $"already reserved by another host allocation (base=0x{info.AllocationBase:X16}, size=0x{info.RegionSize:X})", + HostRegionState.Committed => + $"already committed by another host allocation (base=0x{info.AllocationBase:X16}, size=0x{info.RegionSize:X}, protect=0x{info.RawProtection:X})", + _ => $"in an unexpected host state (raw=0x{info.RawState:X})", + }; + } + public ulong AllocateAt(ulong desiredAddress, ulong size, bool executable = true, bool allowAlternative = true) { if (size == 0)