fix(engine): make GetMergeStackResult thunk cdecl to stop plugin stack corruption (API v69)

The delegate lacked [UnmanagedFunctionPointer(Cdecl)], so x86 marshalled a stdcall thunk; cdecl callers popped args twice and AutoStack corrupted RynthAi's stack until the client died. SDK now requires v69 before calling it; release build rejects any non-cdecl API delegate. Adds one-time RenderOverlay diagnostics.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
BlankenshipG 2026-10-04 19:18:16 -04:00
commit 19095e17c0
6 changed files with 71 additions and 3 deletions

View file

@ -40,6 +40,25 @@ if (-not (Test-Path $sln)) {
throw "RynthCore.sln not found at: $sln"
}
# Plugin API delegates must be cdecl. On x86 a delegate without
# [UnmanagedFunctionPointer(CallingConvention.Cdecl)] marshals as stdcall, and every cdecl call
# from a plugin then pops its arguments twice and corrupts the plugin's stack.
$contract = Join-Path $RepoRoot "src\RynthCore.Engine\Plugins\PluginContract.cs"
$contractLines = [IO.File]::ReadAllLines($contract)
$badDelegates = @()
for ($i = 0; $i -lt $contractLines.Length; $i++) {
if ($contractLines[$i] -notmatch '^\s*(internal|public)\s+(unsafe\s+)?delegate\s') { continue }
# Nearest previous line that is not blank and not a // comment must carry the attribute.
$j = $i - 1
while ($j -ge 0 -and ($contractLines[$j].Trim() -eq '' -or $contractLines[$j].Trim().StartsWith('//'))) { $j-- }
if ($j -lt 0 -or $contractLines[$j] -notmatch 'UnmanagedFunctionPointer\(CallingConvention\.Cdecl\)') {
$badDelegates += " line $($i + 1): $($contractLines[$i].Trim())"
}
}
if ($badDelegates.Count -gt 0) {
throw "PluginContract.cs delegate(s) missing [UnmanagedFunctionPointer(CallingConvention.Cdecl)]:`n$($badDelegates -join "`n")"
}
Write-Host "Building solution: $sln" -ForegroundColor Cyan
dotnet build $sln -c Release
if ($LASTEXITCODE -ne 0) { throw "Solution build failed (exit $LASTEXITCODE)" }

View file

@ -0,0 +1,35 @@
# 2026-10-04 — Fix: GetMergeStackResult stack corruption crash (API v69)
## Problem
Builds 2026.10.4.12 and .13 crashed a couple of seconds after AutoStack started
logging merges with garbage item names and IDs (e.g. `src=0x40C16E04 ->
tgt=0xFFFF7C95`, then `Event_StackableMerge from=0x40C16E04 to=0xBD8B1053`, a
target that didn't even match the request). The process died with no exception,
so the crash logger caught nothing and the launcher recorded a "normal close".
## Cause
`GetMergeStackResultCallbackDelegate` (PluginContract.cs) was the only one of the
API delegates without `[UnmanagedFunctionPointer(CallingConvention.Cdecl)]`. On
x86 that makes `Marshal.GetFunctionPointerForDelegate` produce a **stdcall**
thunk, while plugins call it as **cdecl**. Both sides popped the 16 bytes of
arguments, so RynthAi's stack shifted on every call. AutoStack (RynthAi 0.6.17+)
calls it on every pending-merge check, so locals turned to garbage until the
process died.
## Fix
* Added the missing `[UnmanagedFunctionPointer(CallingConvention.Cdecl)]`.
* API version bumped to **v69**. The SDK's `HasGetMergeStackResult` now requires
v69, so a rebuilt plugin never calls the broken v68 thunk on a 4.12/4.13 engine
(it falls back to the 10 s grace check).
* `Build-Release-All.ps1` now fails the release if any delegate in
`PluginContract.cs` is missing the Cdecl attribute.
## ILT Hub draw diagnostics
* Engine logs `PluginManager: first RenderOverlay call for <plugin>` once per
plugin per session, proving the overlay export is being driven.
* See RynthSuite `Changelog/RynthAi-0.6.19-hub-draw-diagnostics.md` for the
plugin-side lines.

View file

@ -57,6 +57,9 @@ internal sealed class LoadedPlugin
/// <summary>Optional RynthPluginRenderOverlay: ImGui windows drawn while the ImGui shell is off.</summary>
public PluginRenderDelegate? RenderOverlay { get; set; }
/// <summary>Set after the first RenderOverlay call has been logged (render thread only).</summary>
public bool RenderOverlayLogged { get; set; }
// ─── Runtime state ───────────────────────────────────────────────
public bool Initialized { get; set; }
public bool LoginCompleteDispatched { get; set; }

View file

@ -747,7 +747,7 @@ internal unsafe struct VendorTradeStatusNative
/// <summary>Current API version. Bump when adding fields to RynthCoreAPI.</summary>
internal static class PluginContractVersion
{
public const uint Current = 68; // v68: GetMergeStackResult
public const uint Current = 69; // v69: GetMergeStackResult thunk is cdecl (v68 engines pass a stdcall thunk - unusable)
}
internal static class ClientActionHookFlags
@ -1214,4 +1214,7 @@ internal unsafe delegate uint VendorSellCallbackDelegate(uint vendorId, uint* it
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal unsafe delegate int GetVendorTradeStatusCallbackDelegate(VendorTradeStatusNative* status);
// Cdecl is mandatory on every API delegate: without it x86 marshals a stdcall thunk, the callee and
// the cdecl caller both pop the args, and the plugin's stack drifts on every call.
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal unsafe delegate int GetMergeStackResultCallbackDelegate(uint sourceObjectId, uint targetObjectId, int* amount, int* ageMs);

View file

@ -1064,6 +1064,13 @@ internal static class PluginManager
if (!plugin.Initialized || plugin.Failed || overlay == null)
continue;
if (!plugin.RenderOverlayLogged)
{
// One line per plugin per session: proves the engine is actually driving the overlay export.
plugin.RenderOverlayLogged = true;
RynthLog.Plugin($"PluginManager: first RenderOverlay call for {plugin.DisplayName}.");
}
try
{
overlay();

View file

@ -6,7 +6,7 @@ namespace RynthCore.PluginSdk;
public readonly unsafe struct RynthCoreHost
{
public const uint CurrentApiVersion = 68; // v68: GetMergeStackResult
public const uint CurrentApiVersion = 69; // v69: GetMergeStackResult usable (cdecl thunk)
/// <summary>
/// The oldest engine API a plugin built on this SDK loads on by default
@ -130,7 +130,8 @@ public readonly unsafe struct RynthCoreHost
public bool HasGetObjectDataIdProperty => _api.Version >= 65 && _api.GetObjectDataIdPropertyFn != IntPtr.Zero;
public bool HasGetPluginExportJson => _api.Version >= 66 && _api.GetPluginExportJsonFn != IntPtr.Zero;
// Version-checked first: on an older engine the field lies past the end of its API table.
public bool HasGetMergeStackResult => _api.Version >= 68 && _api.GetMergeStackResultFn != IntPtr.Zero;
// v68 engines export a stdcall thunk that corrupts the caller's stack, so require v69.
public bool HasGetMergeStackResult => _api.Version >= 69 && _api.GetMergeStackResultFn != IntPtr.Zero;
// ─── Methods ────────────────────────────────────────────────────────────