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`), and the game frames of the waiting call. 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`): 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 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
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
11 changes: 8 additions & 3 deletions src/FiveStar.Loader/Diagnostics/GameHangWatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,11 @@ private static string Describe()
return $"main state {(short)state} (next {(short)next}), loading screen {loading}, loading render callback {loadingRender}, session 0x{session:x} ready {ready}, watchdog paused {paused}, handler 0x{handler:x}";
}

/// <summary>Whether <paramref name="address"/> is committed and accessible in this process.</summary>
internal static bool IsReadable(ulong address)
/// <summary>
/// Whether <paramref name="address"/> is committed and accessible in this process; with <paramref name="protections"/>,
/// also whether its page has one of them.
/// </summary>
internal static bool IsReadable(ulong address, uint protections = 0)
{
MemoryBasicInformation info;

Expand All @@ -210,6 +213,8 @@ internal static bool IsReadable(ulong address)

bool blocked = (info.Protect & (Win32Constants.PageNoAccess | Win32Constants.PageGuard)) != 0;

return info.State == Win32Constants.MemCommit && !blocked;
bool allowed = protections == 0 || (info.Protect & protections) != 0;

return info.State == Win32Constants.MemCommit && !blocked && allowed;
}
}
13 changes: 10 additions & 3 deletions src/FiveStar.Loader/Diagnostics/ReturnAddresses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ internal static unsafe class ReturnAddresses
private const byte Displacement8 = 0x50;
private const byte Displacement32 = 0x90;

/// <summary>
/// Pages whose bytes can be read as code. PAGE_EXECUTE alone is not one: the loader maps execute-only sections, and
/// a read there faults like an uncommitted page.
/// </summary>
private const uint ReadableCode = Win32Constants.PageExecuteRead | Win32Constants.PageExecuteReadWrite | Win32Constants.PageExecuteWriteCopy;

/// <summary>Up to <paramref name="count"/> return addresses into mapped images above the caller, innermost first.</summary>
public static nint[] Scan(ImageTable images, int count)
{
Expand Down Expand Up @@ -59,8 +65,9 @@ public static nint[] Scan(ImageTable images, int count)
}

/// <summary>
/// Whether the 6 bytes before <paramref name="value"/> are committed: an image's range has pages that are not (a section
/// window unmapped, a discarded section), and a read there killed a boot inside a wait.
/// 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
/// a read there killed a boot inside a wait.
/// </summary>
internal static bool CallBytesReadable(nint value)
{
Expand All @@ -69,7 +76,7 @@ internal static bool CallBytesReadable(nint value)
ulong page = (ulong)Environment.SystemPageSize;
bool samePage = first / page == last / page;

return GameHangWatch.IsReadable(first) && (samePage || GameHangWatch.IsReadable(last));
return GameHangWatch.IsReadable(first, ReadableCode) && (samePage || GameHangWatch.IsReadable(last, ReadableCode));
}

/// <summary>Whether the bytes before <paramref name="address"/> end a call instruction (6 readable bytes before it).</summary>
Expand Down
1 change: 1 addition & 0 deletions src/FiveStar.Loader/Native/Win32Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal static class Win32Constants
public const uint PageExecute = 0x10;
public const uint PageExecuteRead = 0x20;
public const uint PageExecuteReadWrite = 0x40;
public const uint PageExecuteWriteCopy = 0x80;
public const uint PageGuard = 0x100;

/// <summary>The x64 page size (winnt.h PAGE_SIZE on AMD64).</summary>
Expand Down
14 changes: 9 additions & 5 deletions tests/FiveStar.Loader.Tests/WaitObjectsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,25 @@ public void A_wait_on_a_thread_handle_names_the_thread_it_joins()
}
}

/// <summary>A stack value inside an image's range is only read when the call bytes before it are committed.</summary>
[Fact]
public void A_value_after_an_uncommitted_page_is_not_read()
/// <summary>A stack value inside an image's range is only read when the call bytes before it are committed readable code.</summary>
[Theory]
[InlineData(Win32Constants.PageExecuteRead, true)]
[InlineData(Win32Constants.PageExecuteReadWrite, true)]
[InlineData(Win32Constants.PageExecute, false)]
[InlineData(Win32Constants.PageReadWrite, false)]
public void A_value_after_a_page_that_is_not_readable_code_is_not_read(uint protection, bool read)
{
int page = Environment.SystemPageSize;
nint region = Kernel32.VirtualAlloc(0, (nuint)(page * 2), Win32Constants.MemReserve, Win32Constants.PageReadWrite);
Assert.NotEqual(0, region);

try
{
Assert.NotEqual(0, Kernel32.VirtualAlloc(region + page, (nuint)page, Win32Constants.MemCommit, Win32Constants.PageReadWrite));
Assert.NotEqual(0, Kernel32.VirtualAlloc(region + page, (nuint)page, Win32Constants.MemCommit, protection));

Assert.False(ReturnAddresses.CallBytesReadable(region + 16));
Assert.False(ReturnAddresses.CallBytesReadable(region + page + 2));
Assert.True(ReturnAddresses.CallBytesReadable(region + page + 16));
Assert.Equal(read, ReturnAddresses.CallBytesReadable(region + page + 16));
}
finally
{
Expand Down
Loading