diff --git a/docs/debugging.md b/docs/debugging.md index 47965883..45426ae9 100644 --- a/docs/debugging.md +++ b/docs/debugging.md @@ -144,7 +144,7 @@ every thread blocked a second or more in one of those waits: the handle, the tim kind or semaphore counts, creating thread and time, the game frames of the creating call) and whether it was signaled since, how often and by which thread last (`WaitObjects`), its last 12 operations (signaled, reset, wait, woke, timed out, abandoned, with the thread, the time before the dump and the game's calling address, the innermost frame only: a signal another wait took or a reset dropped shows there; a wait on several objects records its timeout on each), and the game frames of the waiting call (a wait on several names them all, "one of N" or "all of N"). A handle the shims did not see made is named when it is a thread's (a join). Frames come from a scan of the thread's stack for return addresses -into the game (`ReturnAddresses`; only values whose call bytes sit in committed readable code, so never in an execute-only section): a walk stops in the shims' generated code, and #13's first report named no creator. A stall whose waiter sits on an object nobody signals any +into the game (`ReturnAddresses`; only values whose call bytes lie in an executable section of the image, by its headers, and then in committed readable code, so never in an execute-only section; the section test comes first because stacks hold many pointers into an image's data, and the VirtualQuery for each one took two thirds of the shims' time on the game's threads during a world load, enough to stutter an animation): a walk stops in the shims' generated code, and #13's first report named no creator. A stall whose waiter sits on an object nobody signals any more (a lost wake-up) then names that object and its creator. For each thread in the game's fence wait (0x144fb0ad0) it also names what the wait depends on (`StallCulprit`): the queue that was asked to signal that fence to the awaited value and the last six executions it was given before that Signal, newest first, each with its last pipeline and whether it finished, and any Wait on another diff --git a/src/FiveStar.Loader/Diagnostics/ReturnAddresses.cs b/src/FiveStar.Loader/Diagnostics/ReturnAddresses.cs index ee5b07ff..dbe44b9d 100644 --- a/src/FiveStar.Loader/Diagnostics/ReturnAddresses.cs +++ b/src/FiveStar.Loader/Diagnostics/ReturnAddresses.cs @@ -55,7 +55,9 @@ public static nint[] Scan(ImageTable images, int count) nint value = *slot; var image = images.FindByAddress(value); - if (image is not null && value - IndirectCall >= image.Base && CallBytesReadable(value) && AfterCall((byte*)value)) + // The section test first: stacks hold many pointers into an image's data, and each VirtualQuery of + // CallBytesReadable is a system call (two thirds of the shims' time in a boot's world load, on every wait). + if (image is not null && InCode(image, value) && CallBytesReadable(value) && AfterCall((byte*)value)) { found.Add(value); } @@ -64,6 +66,12 @@ public static nint[] Scan(ImageTable images, int count) return [.. found]; } + /// Whether the 6 bytes before lie in executable sections of . + internal static bool InCode(LoadedImage image, nint value) + { + return image.InCode(value - IndirectCall) && image.InCode(value - 1); + } + /// /// Whether the 6 bytes before are committed readable code (): an /// image's range has pages that are not (a section window unmapped, a discarded section, an execute-only section), and diff --git a/src/FiveStar.Loader/Loading/LoadedImage.cs b/src/FiveStar.Loader/Loading/LoadedImage.cs index 04232520..8bc53b05 100644 --- a/src/FiveStar.Loader/Loading/LoadedImage.cs +++ b/src/FiveStar.Loader/Loading/LoadedImage.cs @@ -8,6 +8,9 @@ namespace FiveStar.Loader.Loading; /// An image this loader mapped, with the state of its load. internal sealed class LoadedImage { + /// The executable sections' RVA ranges, read from the headers on first use (). + private (uint Start, uint End)[]? _code; + public LoadedImage(string name, string path, PeImage pe, nint imageBase) { Name = name; @@ -67,11 +70,46 @@ public bool Contains(nint address) return (ulong)address >= (ulong)Base && (ulong)address < (ulong)Base + Size; } + /// + /// Whether lies in a section the image's headers declare executable. That says nothing of + /// whether the page is mapped now; it rules out the data sections without asking the system. + /// + public bool InCode(nint address) + { + if (!Contains(address)) + { + return false; + } + + uint rva = (uint)(address - Base); + + foreach (var (start, end) in _code ??= CodeRanges()) + { + if (rva >= start && rva < end) + { + return true; + } + } + + return false; + } + public nint Va(uint rva) { return Base + (nint)rva; } + /// The executable sections' RVA ranges (start, end), from the headers. + private (uint Start, uint End)[] CodeRanges() + { + return + [ + .. Pe.Sections + .Where(section => (section.Characteristics & SectionCharacteristics.MemoryExecute) != 0) + .Select(section => (section.VirtualAddress, section.VirtualAddress + Math.Max(section.VirtualSize, section.RawSize))), + ]; + } + public override string ToString() { string relocated = string.Empty; diff --git a/tests/FiveStar.Loader.Tests/WaitObjectsTests.cs b/tests/FiveStar.Loader.Tests/WaitObjectsTests.cs index f503ce9c..86a8d4b3 100644 --- a/tests/FiveStar.Loader.Tests/WaitObjectsTests.cs +++ b/tests/FiveStar.Loader.Tests/WaitObjectsTests.cs @@ -4,6 +4,7 @@ using FiveStar.Loader.Diagnostics; using FiveStar.Loader.Loading; using FiveStar.Loader.Native; +using FiveStar.Loader.Pe; using Xunit; namespace FiveStar.Loader.Tests; @@ -203,4 +204,24 @@ public void A_stack_value_counts_as_a_return_address_only_after_a_call(byte[] be Assert.Equal(call, ReturnAddresses.AfterCall(bytes + before.Length)); } } + + /// A stack value is checked against the image's executable sections before any system call. + [Fact] + public void Only_a_value_after_six_bytes_of_an_executable_section_is_checked_as_code() + { + nint imageBase = unchecked((nint)0x7FF6_0000_0000); + var sections = new List + { + new(".text", 0x1000, 0x1000, 0x400, 0x1000, SectionCharacteristics.ContainsCode | SectionCharacteristics.MemoryExecute | SectionCharacteristics.MemoryRead), + new(".data", 0x2000, 0x1000, 0x1400, 0x1000, SectionCharacteristics.ContainsInitializedData | SectionCharacteristics.MemoryRead | SectionCharacteristics.MemoryWrite), + }; + var pe = new PeImage([], 0, (ulong)imageBase, 0x3000, 0x400, 0x1000, 0x1000, [], sections); + var image = new LoadedImage("game.exe", "game.exe", pe, imageBase); + + Assert.True(ReturnAddresses.InCode(image, imageBase + 0x1010)); + Assert.True(ReturnAddresses.InCode(image, imageBase + 0x2000)); + Assert.False(ReturnAddresses.InCode(image, imageBase + 0x1003)); + Assert.False(ReturnAddresses.InCode(image, imageBase + 0x2010)); + Assert.False(ReturnAddresses.InCode(image, imageBase + 0x4000)); + } }