Fix dispatcher crash/deadlock, instruction analyzer, cache equality, task leak, and redirection protection
- MainThreadDispatcher: guard DispatchHook with try/catch so exceptions never escape to native caller; drain and fault pending work on Dispose; synchronize Execute/ExecuteAsync/Dispose against race/dispose. - InstructionAnalyzer: require ModRM 0xEC for 0x83/0x81 sub-esp/rsp forms, rejecting unsafe RIP-relative or memory forms. - PatternScannerCache: implement value equality on CacheKey so repeated scans actually hit cache. - BackgroundTaskExecutor: add remote allocations to the free list immediately after VirtualAllocEx, before any write that could fail and leak. - redirect: capture and restore original page protection in Apply/Remove instead of leaving target RWX. - Regression tests for all six fixes. Tests: 198 passing, 4 integration/interactive skipped.
This commit is contained in:
@@ -21,8 +21,10 @@ public sealed class MainThreadPump : IDisposable
|
||||
private readonly IntPtr _frameAddress;
|
||||
private readonly ConcurrentQueue<WorkItem> _queue = new();
|
||||
|
||||
private readonly object _gate = new();
|
||||
private Detour? _detour;
|
||||
private bool _installed;
|
||||
private bool _disposed;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a pump that will hook the frame function at <paramref name="frameAddress"/>.
|
||||
@@ -52,14 +54,16 @@ public sealed class MainThreadPump : IDisposable
|
||||
/// </summary>
|
||||
public TResult Execute<TResult>(Func<TResult> work)
|
||||
{
|
||||
if (!_installed)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"The main-thread pump is not installed. Call Install() first.");
|
||||
}
|
||||
|
||||
var tcs = new TaskCompletionSource<object?>();
|
||||
_queue.Enqueue(new WorkItem(() => work()!, tcs));
|
||||
lock (_gate)
|
||||
{
|
||||
if (_disposed)
|
||||
ThrowDisposed();
|
||||
if (!_installed)
|
||||
throw new InvalidOperationException("The main-thread pump is not installed. Call Install() first.");
|
||||
|
||||
_queue.Enqueue(new WorkItem(() => work()!, tcs));
|
||||
}
|
||||
|
||||
object? result = tcs.Task.GetAwaiter().GetResult();
|
||||
return (TResult)result!;
|
||||
@@ -70,45 +74,70 @@ public sealed class MainThreadPump : IDisposable
|
||||
/// </summary>
|
||||
public Task<TResult> ExecuteAsync<TResult>(Func<TResult> work)
|
||||
{
|
||||
if (!_installed)
|
||||
var tcs = new TaskCompletionSource<TResult>();
|
||||
lock (_gate)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"The main-thread pump is not installed. Call Install() first.");
|
||||
if (_disposed)
|
||||
ThrowDisposed();
|
||||
if (!_installed)
|
||||
throw new InvalidOperationException("The main-thread pump is not installed. Call Install() first.");
|
||||
|
||||
object? Box() => work()!;
|
||||
_queue.Enqueue(new WorkItem(Box, r => tcs.SetResult((TResult)r!), ex => tcs.SetException(ex)));
|
||||
}
|
||||
|
||||
var tcs = new TaskCompletionSource<TResult>();
|
||||
object? Box() => work()!;
|
||||
_queue.Enqueue(new WorkItem(Box, r => tcs.SetResult((TResult)r!), ex => tcs.SetException(ex)));
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>Removes the frame-function detour if it is installed.</summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (_installed && _detour is not null)
|
||||
lock (_gate)
|
||||
{
|
||||
_detour.Remove();
|
||||
if (_disposed)
|
||||
return;
|
||||
_disposed = true;
|
||||
|
||||
if (_installed && _detour is not null)
|
||||
_detour.Remove();
|
||||
_installed = false;
|
||||
}
|
||||
|
||||
// Fault any caller still blocked on queued work so Execute cannot hang forever.
|
||||
while (_queue.TryDequeue(out WorkItem? item))
|
||||
{
|
||||
item.SetException(new ObjectDisposedException(nameof(MainThreadPump)));
|
||||
}
|
||||
}
|
||||
|
||||
private static void ThrowDisposed()
|
||||
=> throw new ObjectDisposedException(nameof(MainThreadPump));
|
||||
|
||||
private int PumpHook()
|
||||
{
|
||||
while (_queue.TryDequeue(out WorkItem? item))
|
||||
try
|
||||
{
|
||||
try
|
||||
while (_queue.TryDequeue(out WorkItem? item))
|
||||
{
|
||||
object? result = item.Work();
|
||||
item.SetResult(result);
|
||||
try
|
||||
{
|
||||
object? result = item.Work();
|
||||
item.SetResult(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
item.SetException(ex);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
item.SetException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Call the original frame function so rendering/game logic continues.
|
||||
return _detour is null ? 0 : (int?)_detour.CallOriginal() ?? 0;
|
||||
// Call the original frame function so rendering/game logic continues.
|
||||
return _detour is null ? 0 : (int?)_detour.CallOriginal() ?? 0;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Never let an exception escape back into the native frame caller.
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
|
||||
Reference in New Issue
Block a user