diff --git a/Build-Release-All.ps1 b/Build-Release-All.ps1 index 37ec1ad..2c680ff 100644 --- a/Build-Release-All.ps1 +++ b/Build-Release-All.ps1 @@ -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)" } diff --git a/Changelog/2026-10-04-merge-result-cdecl-crash-fix.md b/Changelog/2026-10-04-merge-result-cdecl-crash-fix.md new file mode 100644 index 0000000..d76cff2 --- /dev/null +++ b/Changelog/2026-10-04-merge-result-cdecl-crash-fix.md @@ -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 ` 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. diff --git a/src/RynthCore.Engine/Plugins/LoadedPlugin.cs b/src/RynthCore.Engine/Plugins/LoadedPlugin.cs index 9778be8..35a96a7 100644 --- a/src/RynthCore.Engine/Plugins/LoadedPlugin.cs +++ b/src/RynthCore.Engine/Plugins/LoadedPlugin.cs @@ -57,6 +57,9 @@ internal sealed class LoadedPlugin /// Optional RynthPluginRenderOverlay: ImGui windows drawn while the ImGui shell is off. public PluginRenderDelegate? RenderOverlay { get; set; } + /// Set after the first RenderOverlay call has been logged (render thread only). + public bool RenderOverlayLogged { get; set; } + // ─── Runtime state ─────────────────────────────────────────────── public bool Initialized { get; set; } public bool LoginCompleteDispatched { get; set; } diff --git a/src/RynthCore.Engine/Plugins/PluginContract.cs b/src/RynthCore.Engine/Plugins/PluginContract.cs index 8afc354..bf8c966 100644 --- a/src/RynthCore.Engine/Plugins/PluginContract.cs +++ b/src/RynthCore.Engine/Plugins/PluginContract.cs @@ -747,7 +747,7 @@ internal unsafe struct VendorTradeStatusNative /// Current API version. Bump when adding fields to RynthCoreAPI. 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); diff --git a/src/RynthCore.Engine/Plugins/PluginManager.cs b/src/RynthCore.Engine/Plugins/PluginManager.cs index bc3efcf..e094271 100644 --- a/src/RynthCore.Engine/Plugins/PluginManager.cs +++ b/src/RynthCore.Engine/Plugins/PluginManager.cs @@ -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(); diff --git a/src/RynthCore.PluginSdk/RynthCoreHost.cs b/src/RynthCore.PluginSdk/RynthCoreHost.cs index 80a98ad..6da015a 100644 --- a/src/RynthCore.PluginSdk/RynthCoreHost.cs +++ b/src/RynthCore.PluginSdk/RynthCoreHost.cs @@ -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) /// /// 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 ────────────────────────────────────────────────────────────