Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/FiveStar.Loader/Diagnostics/ReturnAddresses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -64,6 +66,12 @@ public static nint[] Scan(ImageTable images, int count)
return [.. found];
}

/// <summary>Whether the 6 bytes before <paramref name="value"/> lie in executable sections of <paramref name="image"/>.</summary>
internal static bool InCode(LoadedImage image, nint value)
{
return image.InCode(value - IndirectCall) && image.InCode(value - 1);
}

/// <summary>
/// Whether the 6 bytes before <paramref name="value"/> are committed readable code (<see cref="ReadableCode"/>): an
/// image's range has pages that are not (a section window unmapped, a discarded section, an execute-only section), and
Expand Down
38 changes: 38 additions & 0 deletions src/FiveStar.Loader/Loading/LoadedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace FiveStar.Loader.Loading;
/// <summary>An image this loader mapped, with the state of its load.</summary>
internal sealed class LoadedImage
{
/// <summary>The executable sections' RVA ranges, read from the headers on first use (<see cref="InCode"/>).</summary>
private (uint Start, uint End)[]? _code;

public LoadedImage(string name, string path, PeImage pe, nint imageBase)
{
Name = name;
Expand Down Expand Up @@ -67,11 +70,46 @@ public bool Contains(nint address)
return (ulong)address >= (ulong)Base && (ulong)address < (ulong)Base + Size;
}

/// <summary>
/// Whether <paramref name="address"/> 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.
/// </summary>
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;
}

/// <summary>The executable sections' RVA ranges (start, end), from the headers.</summary>
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;
Expand Down
21 changes: 21 additions & 0 deletions tests/FiveStar.Loader.Tests/WaitObjectsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

/// <summary>A stack value is checked against the image's executable sections before any system call.</summary>
[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<PeSection>
{
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));
}
}
Loading