From 3d4c842e5de029cdfb30fac67fccbc3dfd6184da Mon Sep 17 00:00:00 2001 From: Refil <53364591+Refilewski@users.noreply.github.com> Date: Tue, 20 Oct 2026 06:21:09 +0200 Subject: [PATCH 1/5] fix(standalone): retry invalid vehicle IDs with fresh snapshots --- .../Game/GameSnapshotReader.cs | 105 +++++++++++++++--- 1 file changed, 90 insertions(+), 15 deletions(-) diff --git a/src/AzimuthRPC.Core/Game/GameSnapshotReader.cs b/src/AzimuthRPC.Core/Game/GameSnapshotReader.cs index addfdb0..1b7042d 100644 --- a/src/AzimuthRPC.Core/Game/GameSnapshotReader.cs +++ b/src/AzimuthRPC.Core/Game/GameSnapshotReader.cs @@ -17,6 +17,31 @@ public GameSnapshotReader(IMemoryReader memory, ILocationResolver? locationResol } public GameSnapshot Capture(ulong gameBase, bool supportedBuild) + { + GameSnapshot initial = CaptureOnce(gameBase, supportedBuild); + if (!initial.Attached || initial.Car.RawId is not null || !ShouldRetryCarProbe(initial.CarProbeStatus)) + return initial; + + // Re-evaluate roots, AudioEngine and the car field from scratch once. + // Never reuse a stale car when a garage/race transition interrupts a read. + GameSnapshot retried = CaptureOnce(gameBase, supportedBuild); + if (!retried.Attached) + return retried; + + return retried with + { + CarRetryStatus = retried.Car.RawId is null + ? $"failed:{initial.CarProbeStatus}->{retried.CarProbeStatus}" + : $"recovered:{initial.CarProbeStatus}->{retried.CarProbeStatus}" + }; + } + + private static bool ShouldRetryCarProbe(string probeStatus) => probeStatus is + "invalid-character" or "unterminated" or "too-short" or "missing-underscore" + or "heap-invalid-pointer" or "heap-unreadable" or "heap-changed" + or "heap-unterminated" or "heap-invalid-id" or "inline-changed"; + + private GameSnapshot CaptureOnce(ulong gameBase, bool supportedBuild) { if (!supportedBuild) return GameSnapshot.Unavailable("Unsupported FH2 PE identity"); if (!MemoryRead.Pointer(gameBase)) return GameSnapshot.Unavailable("Invalid game base"); @@ -29,7 +54,7 @@ public GameSnapshot Capture(ulong gameBase, bool supportedBuild) var raw = frame.Read(audio, 0x2C0); if (raw is null) return GameSnapshot.Unavailable("Audio/vehicle snapshot read failed"); var ui = UiModeReader.ReadSnapshot(gameBase, true, frame.Read); - string? car = ReadCar(raw.AsSpan(0x278, 64), out string carProbeStatus); + string? car = ReadCar(raw.AsSpan(0x278, 64), checked(audio + 0x278), frame, out string carProbeStatus); if (car != _lastCarId) { _lastCarId = car; _displayName = FriendlyCarName(car); } float mph = F32(raw, 0x90); float? speed = float.IsFinite(mph) && mph >= 0 && mph <= 400 ? mph : null; @@ -67,8 +92,51 @@ private static WorldPresence VehiclePresence(UiMode mode, string? car, VehicleSt float x = F32(data, offset), y = F32(data, offset + 4), z = F32(data, offset + 8); return float.IsFinite(x) && float.IsFinite(y) && float.IsFinite(z) && MathF.Abs(x) <= bound && MathF.Abs(y) <= bound && MathF.Abs(z) <= bound ? new Vector3(x, y, z) : null; } - private static string? ReadCar(ReadOnlySpan bytes, out string status) + private static string? ReadCar(ReadOnlySpan bytes, ulong fieldAddress, PointerSnapshot frame, out string status) { + // Candidate MSVC std::string (32-byte object): 16-byte buffer/pointer, + // followed by 64-bit size and capacity. Short IDs remain inline. + // This is a guarded fallback, not yet a live-validated FH2 layout. + ulong length = BinaryPrimitives.ReadUInt64LittleEndian(bytes[16..]); + ulong capacity = BinaryPrimitives.ReadUInt64LittleEndian(bytes[24..]); + if (length is >= 16 and <= 127 && capacity >= length && capacity <= 4096) + { + ulong pointer = BinaryPrimitives.ReadUInt64LittleEndian(bytes); + if (!MemoryRead.Pointer(pointer)) + { + status = "heap-invalid-pointer"; + return null; + } + + byte[]? first = frame.Read(pointer, (int)length + 1); + byte[]? second = frame.Read(pointer, (int)length + 1); + byte[]? currentField = frame.Read(fieldAddress, 32); + if (first is null || second is null) + { + status = "heap-unreadable"; + return null; + } + if (!first.AsSpan().SequenceEqual(second) || currentField is null || + !bytes[..32].SequenceEqual(currentField)) + { + status = "heap-changed"; + return null; + } + if (first[(int)length] != 0) + { + status = "heap-unterminated"; + return null; + } + if (!ValidCarId(first.AsSpan(0, (int)length))) + { + status = "heap-invalid-id"; + return null; + } + + status = "ok-heap"; + return Encoding.ASCII.GetString(first, 0, (int)length); + } + int end = bytes.IndexOf((byte)0); if (end < 0 || end > 63) { @@ -85,26 +153,33 @@ private static WorldPresence VehiclePresence(UiMode mode, string? car, VehicleSt status = "too-short"; return null; } - for (int i = 0; i < end; i++) + if (!ValidCarId(bytes[..end])) { - byte b = bytes[i]; - if (!((b >= 'A' && b <= 'Z') || - (b >= 'a' && b <= 'z') || - (b >= '0' && b <= '9') || b == '_')) - { - status = "invalid-character"; - return null; - } + status = "invalid-character"; + return null; } - string value = Encoding.ASCII.GetString(bytes[..end]); - if (!value.Contains('_')) + // The inline identifier can change after the bulk AudioEngine read. + // Reject torn snapshots instead of attributing an old ID to a new car. + byte[]? currentInline = frame.Read(fieldAddress, 32); + if (currentInline is null || !bytes[..32].SequenceEqual(currentInline)) { - status = "missing-underscore"; + status = "inline-changed"; return null; } status = "ok"; - return value; + return Encoding.ASCII.GetString(bytes[..end]); + } + + private static bool ValidCarId(ReadOnlySpan bytes) + { + if (bytes.Length < 5 || !bytes.Contains((byte)'_')) return false; + foreach (byte b in bytes) + if (!((b >= 'A' && b <= 'Z') || + (b >= 'a' && b <= 'z') || + (b >= '0' && b <= '9') || b == '_')) + return false; + return true; } private static string? FriendlyCarName(string? raw) { From 174542e2873c9e5f9dd2fa4b130951916a274bc3 Mon Sep 17 00:00:00 2001 From: Refil <53364591+Refilewski@users.noreply.github.com> Date: Tue, 20 Oct 2026 06:21:11 +0200 Subject: [PATCH 2/5] chore(standalone): expose bounded car retry diagnostics --- src/AzimuthRPC.Core/Models/GameSnapshot.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/AzimuthRPC.Core/Models/GameSnapshot.cs b/src/AzimuthRPC.Core/Models/GameSnapshot.cs index c9317fd..63b19bc 100644 --- a/src/AzimuthRPC.Core/Models/GameSnapshot.cs +++ b/src/AzimuthRPC.Core/Models/GameSnapshot.cs @@ -23,6 +23,8 @@ public sealed record GameSnapshot public CarState Car { get; init; } = new(); // Diagnostic only: classification of the car ID field, no raw heap bytes. public string CarProbeStatus { get; init; } = "Not sampled"; + // Standalone: shows whether a fresh one-shot probe recovered a transient ID. + public string CarRetryStatus { get; init; } = "not-needed"; public VehicleState Vehicle { get; init; } = new(); public UiMode UiMode { get; init; } public GameActivity Activity { get; init; } From 7c6474c102fd373ec43963f697b9db36c2a7e7c1 Mon Sep 17 00:00:00 2001 From: Refil <53364591+Refilewski@users.noreply.github.com> Date: Tue, 20 Oct 2026 06:21:13 +0200 Subject: [PATCH 3/5] test: cover heap vehicle IDs and one-shot retry recovery --- tests/AzimuthRPC.Core.Tests/Program.cs | 125 +++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/tests/AzimuthRPC.Core.Tests/Program.cs b/tests/AzimuthRPC.Core.Tests/Program.cs index 06dc892..f75e510 100644 --- a/tests/AzimuthRPC.Core.Tests/Program.cs +++ b/tests/AzimuthRPC.Core.Tests/Program.cs @@ -2,6 +2,7 @@ using System.Globalization; using System.Numerics; using System.Text.Json; +using System.Text; using AzimuthRPC; using AzimuthRPC.Tests; @@ -63,6 +64,130 @@ var s when s.StartsWith("worldmap_") => UiMode.WorldMap, Check(!reader.Capture(gameBase, true).Attached, "Changing root published a mixed snapshot"); }); } +Test("Car IDs survive heap string / inline changes and never reuse stale IDs", () => +{ + var memory = new FixtureMemory(); + var fixture = captures.First(c => c.Label == "free-roam-stopped"); + foreach (var block in fixture.Memory()) memory.Blocks.Add(block.Key, block.Value); + var audio = memory.Blocks[SyntheticFixture.Audio]; + var reader = new GameSnapshotReader(memory); + const ulong heapA = 0x7500000000, heapB = 0x7600000000; + + void PutHeap(string id, ulong address) + { + Check(id.Length >= 16 && id.Length <= 127); + audio.AsSpan(0x278, 64).Clear(); + BinaryPrimitives.WriteUInt64LittleEndian(audio.AsSpan(0x278), address); + BinaryPrimitives.WriteUInt64LittleEndian(audio.AsSpan(0x288), (ulong)id.Length); + BinaryPrimitives.WriteUInt64LittleEndian(audio.AsSpan(0x290), (ulong)Math.Max(31, id.Length)); + memory.Blocks[address] = Encoding.ASCII.GetBytes(id + "\0"); + } + void PutShort(string id) + { + Check(id.Length >= 5 && id.Length <= 15); + audio.AsSpan(0x278, 64).Clear(); + Encoding.ASCII.GetBytes(id + "\0").CopyTo(audio, 0x278); + BinaryPrimitives.WriteUInt64LittleEndian(audio.AsSpan(0x288), (ulong)id.Length); + BinaryPrimitives.WriteUInt64LittleEndian(audio.AsSpan(0x290), 15); + } + + PutHeap("HON_CivicTypeR_2015", heapA); + var first = reader.Capture(SyntheticFixture.GameBase, true); + Check(first.Car.RawId == "HON_CivicTypeR_2015" && first.CarProbeStatus == "ok-heap", + "Long MSVC string should resolve via guarded heap pointer"); + + PutHeap("BMW_M3Coupe_2010", heapB); + var switched = reader.Capture(SyntheticFixture.GameBase, true); + Check(switched.Car.RawId == "BMW_M3Coupe_2010" && switched.CarProbeStatus == "ok-heap", + "Second long car should replace first"); + + PutShort("TOY_SupraRZ_98"); + var shortCar = reader.Capture(SyntheticFixture.GameBase, true); + Check(shortCar.Car.RawId == "TOY_SupraRZ_98" && shortCar.CarProbeStatus == "ok", + "Short car should switch back to inline encoding"); + + PutHeap("HON_CivicTypeR_2015", heapA); + memory.Blocks.Remove(heapA); + var unreadable = reader.Capture(SyntheticFixture.GameBase, true); + Check(unreadable.Attached && unreadable.Car.RawId is null && unreadable.CarProbeStatus == "heap-unreadable", + "Unreadable heap must not reuse a previous valid car"); + + PutHeap("HON_CivicTypeR_2015", heapA); + memory.Blocks[heapA][0] = (byte)'!'; + var malformed = reader.Capture(SyntheticFixture.GameBase, true); + Check(malformed.Car.RawId is null && malformed.CarProbeStatus == "heap-invalid-id", + "Malformed pointer payload must be rejected"); + + PutHeap("HON_CivicTypeR_2015", heapA); + int heapReads = 0; + memory.BeforeRead = (address, size) => + { + if (address == heapA && ++heapReads == 2) + memory.Blocks[heapA][0] = (byte)'A'; + }; + var racing = reader.Capture(SyntheticFixture.GameBase, true); + Check(racing.Car.RawId is null && racing.CarProbeStatus == "heap-changed", + "Heap changing during a snapshot must not publish a mixed ID"); +}); + +Test("Invalid car ID is retried exactly once using a new full snapshot", () => +{ + var memory = new FixtureMemory(); + var fixture = captures.First(c => c.Label == "free-roam-stopped"); + foreach (var block in fixture.Memory()) memory.Blocks.Add(block.Key, block.Value); + var audio = memory.Blocks[SyntheticFixture.Audio]; + var reader = new GameSnapshotReader(memory); + void WriteCar(string id) + { + audio.AsSpan(0x278, 64).Clear(); + Encoding.ASCII.GetBytes(id + "\0").CopyTo(audio, 0x278); + } + + WriteCar("BAD!_model_2026"); + int rawReads = 0; + memory.BeforeRead = (address, size) => + { + if (address == SyntheticFixture.Audio && size == 0x2C0 && ++rawReads == 2) + WriteCar("TOY_SupraRZ_98"); + }; + var recovered = reader.Capture(SyntheticFixture.GameBase, true); + Check(rawReads == 2, "Expected exactly one fresh retry for invalid-character"); + Check(recovered.Car.RawId == "TOY_SupraRZ_98" && recovered.CarProbeStatus == "ok"); + Check(recovered.CarRetryStatus == "recovered:invalid-character->ok"); + + WriteCar("BAD!_model_2026"); + rawReads = 0; + memory.BeforeRead = (address, size) => + { + if (address == SyntheticFixture.Audio && size == 0x2C0) rawReads++; + }; + var rejected = reader.Capture(SyntheticFixture.GameBase, true); + Check(rawReads == 2, "Persistent invalid ID must not cause unbounded rescanning"); + Check(rejected.Attached && rejected.Car.RawId is null && rejected.Car.DisplayName is null); + Check(rejected.CarProbeStatus == "invalid-character"); + Check(rejected.CarRetryStatus == "failed:invalid-character->invalid-character"); +}); + +Test("Changing an inline car during sampling recovers without a stale result", () => +{ + var memory = new FixtureMemory(); + var fixture = captures.First(c => c.Label == "free-roam-stopped"); + foreach (var block in fixture.Memory()) memory.Blocks.Add(block.Key, block.Value); + var audio = memory.Blocks[SyntheticFixture.Audio]; + var reader = new GameSnapshotReader(memory); + int fieldReads = 0; + memory.BeforeRead = (address, size) => + { + if (address != SyntheticFixture.Audio + 0x278 || size != 32 || ++fieldReads != 1) return; + audio.AsSpan(0x278, 64).Clear(); + Encoding.ASCII.GetBytes("LAM_Huracan_15\0").CopyTo(audio, 0x278); + }; + var recovered = reader.Capture(SyntheticFixture.GameBase, true); + Check(fieldReads == 2, "Second car read should use a fresh memory snapshot"); + Check(recovered.Car.RawId == "LAM_Huracan_15"); + Check(recovered.CarRetryStatus == "recovered:inline-changed->ok"); +}); + Test("Unsupported and overflowing bases fail without reads", () => { using var memory = new FixtureMemory(); var reader = new GameSnapshotReader(memory); From 77ce3a7887d78ecbd0bc29a889b323c4a4b7c3b7 Mon Sep 17 00:00:00 2001 From: Refil <53364591+Refilewski@users.noreply.github.com> Date: Tue, 20 Oct 2026 06:21:15 +0200 Subject: [PATCH 4/5] chore(standalone): log car recovery outcomes --- src/AzimuthRPC.App/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AzimuthRPC.App/Program.cs b/src/AzimuthRPC.App/Program.cs index 5708832..7f31f48 100644 --- a/src/AzimuthRPC.App/Program.cs +++ b/src/AzimuthRPC.App/Program.cs @@ -152,7 +152,7 @@ private void OnRuntimeChanged(DebugStatus status) ? $"PID={status.ProcessId}; UiMode={status.Game.UiMode}; Activity={status.Game.Activity}; " + $"HooksInstalled={status.Game.Hooks.Installed}; TelemetryActive={status.Game.Hooks.Active}; " + $"EventContext={status.Game.Hooks.EventContext?.ToString() ?? "none"}; " + - $"CarProbe={status.Game.CarProbeStatus}; CarId={status.Game.Car.RawId ?? "none"}" + $"CarProbe={status.Game.CarProbeStatus}; CarRetry={status.Game.CarRetryStatus}; CarId={status.Game.Car.RawId ?? "none"}" : $"PID={status.ProcessId}; Game not attached; Reason={status.Game.Error ?? "unknown"}"; if (diagnostics != _lastDiagnosticContext) From 96d40f6cebcf85c451006dff790e7fae7ed3275d Mon Sep 17 00:00:00 2001 From: Refil <53364591+Refilewski@users.noreply.github.com> Date: Tue, 20 Oct 2026 06:24:14 +0200 Subject: [PATCH 5/5] test: model transient heap corruption and distinct inline vehicle swap --- tests/AzimuthRPC.Core.Tests/Program.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/AzimuthRPC.Core.Tests/Program.cs b/tests/AzimuthRPC.Core.Tests/Program.cs index f75e510..681a49c 100644 --- a/tests/AzimuthRPC.Core.Tests/Program.cs +++ b/tests/AzimuthRPC.Core.Tests/Program.cs @@ -123,11 +123,12 @@ void PutShort(string id) memory.BeforeRead = (address, size) => { if (address == heapA && ++heapReads == 2) - memory.Blocks[heapA][0] = (byte)'A'; + memory.Blocks[heapA][0] = (byte)'!'; }; var racing = reader.Capture(SyntheticFixture.GameBase, true); - Check(racing.Car.RawId is null && racing.CarProbeStatus == "heap-changed", - "Heap changing during a snapshot must not publish a mixed ID"); + Check(racing.Car.RawId is null && racing.CarProbeStatus == "heap-invalid-id" && + racing.CarRetryStatus == "failed:heap-changed->heap-invalid-id", + "A changed heap that becomes invalid must fail closed after retry"); }); Test("Invalid car ID is retried exactly once using a new full snapshot", () => @@ -175,6 +176,8 @@ void WriteCar(string id) foreach (var block in fixture.Memory()) memory.Blocks.Add(block.Key, block.Value); var audio = memory.Blocks[SyntheticFixture.Audio]; var reader = new GameSnapshotReader(memory); + audio.AsSpan(0x278, 64).Clear(); + Encoding.ASCII.GetBytes("TOY_SupraRZ_98\0").CopyTo(audio, 0x278); int fieldReads = 0; memory.BeforeRead = (address, size) => {