From ef17b13b896efacca3d08f21e134c202a36ee948 Mon Sep 17 00:00:00 2001 From: Asanilo Date: Mon, 9 Nov 2026 22:43:58 +0800 Subject: [PATCH] Fix Linux image discovery exceeding the 512 MiB scan limit --- docs/linux-support.md | 2 +- src/BBTools.LinuxCore/LinuxImageLocator.cs | 26 +++++++++++++++++----- tests/BBTools.LinuxChecks/Program.cs | 16 ++++++++++++- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/docs/linux-support.md b/docs/linux-support.md index 5f25b43..7faaa48 100644 --- a/docs/linux-support.md +++ b/docs/linux-support.md @@ -83,7 +83,7 @@ chmod +x BBTools.LinuxProbe This executable never enables writes. It reports platform/access-policy metadata or health, stamina, level, Echoes and attributes as JSON, without character names, saves, environment values or credentials. It rejects ambiguous images and caps -scanning at 20 seconds/512 MiB. Image discovery uses a guest instruction anchor and +scanning at 20 seconds/1024 MiB. Image discovery uses a guest instruction anchor and all seven core fingerprints; it does not assume the launcher ELF is the game image. Different builds/hooks remain subject to strict validation. diff --git a/src/BBTools.LinuxCore/LinuxImageLocator.cs b/src/BBTools.LinuxCore/LinuxImageLocator.cs index 2b4193a..0adbc16 100644 --- a/src/BBTools.LinuxCore/LinuxImageLocator.cs +++ b/src/BBTools.LinuxCore/LinuxImageLocator.cs @@ -11,20 +11,31 @@ public static ulong Find(IMemory memory, IEnumerable maps, TimeSpan bu var anchor = PlayerSession.Fingerprints[0]; var candidates = new HashSet(); var scanned = 0UL; + var readFailures = 0; const int chunkSize = 1024 * 1024; + // NVIDIA/LLVM/DLSS code alone can push a loaded bbport process past + // 512 MiB. Keep a bounded full scan so later valid images still make + // the result ambiguous instead of accepting the first anchor found. + const ulong maxScanBytes = 1024UL * 1024 * 1024; + var codeMaps = maps.Where(m => m.ScanCode && m.End <= 0x800000000000).ToArray(); + string Progress() => $"scanned={scanned / (1024.0 * 1024):F2} MiB, elapsed={clock.Elapsed.TotalSeconds:F2}s, candidates={candidates.Count}, failed reads={readFailures}"; + GameDiagnostics.Log?.Invoke("SCAN", $"Starting Linux image scan: {codeMaps.Length} code mappings, {codeMaps.Sum(m => (double)(m.End - m.Start)) / (1024 * 1024):F2} MiB; limit=1024 MiB, budget={budget.TotalSeconds:F2}s."); // Locate an instruction anchor, not the host ELF base: bbport maps the // guest image separately and may split/coalesce its anonymous mappings. - foreach (var map in maps.Where(m => m.ScanCode && m.End <= 0x800000000000)) + foreach (var map in codeMaps) { for (var at = map.Start; at < map.End;) { cancellationToken.ThrowIfCancellationRequested(); - if (clock.Elapsed > budget || scanned >= 512UL * 1024 * 1024) - throw new IOException("Linux image scan reached its time/size limit. No game writes were performed."); - var length = (int)Math.Min((ulong)chunkSize, map.End - at); + if (clock.Elapsed > budget || scanned >= maxScanBytes) + { + var reason = clock.Elapsed > budget ? "time" : "size"; + throw new IOException($"Linux image scan reached its {reason} limit ({Progress()}). No game writes were performed."); + } + var length = (int)Math.Min((ulong)chunkSize, Math.Min(map.End - at, maxScanBytes - scanned)); byte[] block; try { block = memory.Read(at, length); } - catch (IOException) { at += (ulong)length; continue; } + catch (IOException) { readFailures++; at += (ulong)length; continue; } scanned += (ulong)length; for (var offset = 0; offset <= block.Length - anchor.Bytes.Length;) { @@ -35,10 +46,13 @@ public static ulong Find(IMemory memory, IEnumerable maps, TimeSpan bu if (address >= anchor.Offset) candidates.Add(address - anchor.Offset); offset++; } - if (length < chunkSize) break; + if ((ulong)length == map.End - at) break; + if (length < chunkSize) + throw new IOException($"Linux image scan reached its size limit ({Progress()}). No game writes were performed."); at += (ulong)(length - anchor.Bytes.Length + 1); } } + GameDiagnostics.Log?.Invoke("SCAN", $"Linux image scan complete: {Progress()}."); return PlayerSession.FindImage(memory, candidates); } } diff --git a/tests/BBTools.LinuxChecks/Program.cs b/tests/BBTools.LinuxChecks/Program.cs index 42c4036..bad6b2c 100644 --- a/tests/BBTools.LinuxChecks/Program.cs +++ b/tests/BBTools.LinuxChecks/Program.cs @@ -68,6 +68,20 @@ public static async Task Main(string[] args) Reject(()=>LinuxImageLocator.Find(fixture,maps.Append(second),TimeSpan.FromSeconds(5)),"ambiguous supported images are rejected"); Reject(()=>LinuxImageLocator.Find(fixture,maps,TimeSpan.FromTicks(-1)),"image scan respects its deadline"); using(var cancelled=new CancellationTokenSource()){cancelled.Cancel();Reject(()=>LinuxImageLocator.Find(fixture,maps,TimeSpan.FromSeconds(5),cancelled.Token),"guest-image discovery is cancellable before a window closes");} + // A loaded bbport process with NVIDIA/LLVM/DLSS libraries exceeds + // 512 MiB of executable mappings even when its guest validates. + var large=new SparseMemory(); + foreach(var f in PlayerSession.Fingerprints)large.Store(image+f.Offset,f.Bytes); + var largeMaps=new[]{new LinuxMap(image,image+600UL*1024*1024,"rwxp","")}; + Check(LinuxImageLocator.Find(large,largeMaps,TimeSpan.FromSeconds(20))==image,"validate a supported image when executable mappings exceed 512 MiB"); + const ulong lateImage=image+550UL*1024*1024; + foreach(var f in PlayerSession.Fingerprints)large.Store(lateImage+f.Offset,f.Bytes); + try{LinuxImageLocator.Find(large,largeMaps,TimeSpan.FromSeconds(20));throw new Exception("ambiguous large scan accepted");} + catch(IOException ex){Check(ex.Message.Contains("No unique supported"),"reject a second valid image beyond the former 512 MiB limit");} + var bounded=new SparseMemory(); + foreach(var f in PlayerSession.Fingerprints)bounded.Store(image+f.Offset,f.Bytes); + try{LinuxImageLocator.Find(bounded,[new LinuxMap(image,image+1100UL*1024*1024,"rwxp","")],TimeSpan.FromSeconds(20));throw new Exception("incomplete size-limited scan accepted");} + catch(IOException ex){Check(ex.Message.Contains("size limit"),"a size limit inside the last mapping cannot accept an incompletely scanned image");} if(OperatingSystem.IsLinux()&&RuntimeInformation.ProcessArchitecture==Architecture.X64){await NativeChecks();await DiscordChecks();} else Console.WriteLine("SKIP Linux syscall checks: this host is not Linux x64."); Console.WriteLine($"PASS: {count} Linux-port foundation checks.");return 0; @@ -190,7 +204,7 @@ private sealed class SparseMemory : IMemory { private readonly Dictionary bytes=new(); public void Store(ulong at,byte[] value){for(var i=0;i=at&&pair.Key-at<(ulong)size)result[(int)(pair.Key-at)]=pair.Value;return result;} public void Write(ulong at,byte[] value)=>throw new Exception("Read-only inspection attempted a write."); } }