Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/linux-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
26 changes: 20 additions & 6 deletions src/BBTools.LinuxCore/LinuxImageLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,31 @@ public static ulong Find(IMemory memory, IEnumerable<LinuxMap> maps, TimeSpan bu
var anchor = PlayerSession.Fingerprints[0];
var candidates = new HashSet<ulong>();
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;)
{
Expand All @@ -35,10 +46,13 @@ public static ulong Find(IMemory memory, IEnumerable<LinuxMap> 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);
}
}
16 changes: 15 additions & 1 deletion tests/BBTools.LinuxChecks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ public static async Task<int> Main(string[] args)
Reject<IOException>(()=>LinuxImageLocator.Find(fixture,maps.Append(second),TimeSpan.FromSeconds(5)),"ambiguous supported images are rejected");
Reject<IOException>(()=>LinuxImageLocator.Find(fixture,maps,TimeSpan.FromTicks(-1)),"image scan respects its deadline");
using(var cancelled=new CancellationTokenSource()){cancelled.Cancel();Reject<OperationCanceledException>(()=>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;
Expand Down Expand Up @@ -190,7 +204,7 @@ private sealed class SparseMemory : IMemory
{
private readonly Dictionary<ulong,byte> bytes=new();
public void Store(ulong at,byte[] value){for(var i=0;i<value.Length;i++)bytes[at+(ulong)i]=value[i];}
public byte[] Read(ulong at,int size){var result=new byte[size];for(var i=0;i<size;i++)result[i]=bytes.GetValueOrDefault(at+(ulong)i);return result;}
public byte[] Read(ulong at,int size){var result=new byte[size];foreach(var pair in bytes)if(pair.Key>=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.");
}
}