-
Notifications
You must be signed in to change notification settings - Fork 0
Start runtime layer: leak-harness ingest bridge + C# harness skeleton (Plan.md §4) #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Diagnostics.Runtime; | ||
|
|
||
| namespace OwnNet.Audit.Runtime | ||
| { | ||
| /// <summary> | ||
| /// Snapshots the TARGET process heap and counts live instances of suspect types. | ||
| /// The harness drives the app out-of-process (FlaUI), so the only way to read the | ||
| /// target's managed heap is a full dump (procdump) + ClrMD — dotnet-gcdump / | ||
| /// dotnet-counters do not attach to .NET Framework, only CoreCLR (Plan.md §4). | ||
| /// </summary> | ||
| internal sealed class HeapCounter | ||
| { | ||
| private readonly string _procdump; | ||
| private readonly string _scratch; | ||
|
|
||
| public HeapCounter(string procdumpPath, string scratchDir) | ||
| { | ||
| _procdump = procdumpPath; | ||
| _scratch = scratchDir; | ||
| Directory.CreateDirectory(_scratch); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Full-dump the process and return { type -> live instance count } for the | ||
| /// requested types. A full dump captures the heap as the GC last left it, so | ||
| /// request a GC in the target (SematixTrace) before calling this. | ||
| /// </summary> | ||
| public Dictionary<string, int> CountLiveInstances(int pid, IEnumerable<string> types) | ||
| { | ||
| var wanted = new HashSet<string>(types); | ||
| var counts = new Dictionary<string, int>(); | ||
| foreach (var t in wanted) | ||
| { | ||
| counts[t] = 0; | ||
| } | ||
|
|
||
| var dump = Path.Combine(_scratch, $"target-{pid}-{Stopwatch.GetTimestamp()}.dmp"); | ||
| RunProcdump(pid, dump); | ||
| try | ||
| { | ||
| using var dataTarget = DataTarget.LoadDump(dump); | ||
| var clr = dataTarget.ClrVersions.FirstOrDefault() | ||
| ?? throw new InvalidOperationException( | ||
| $"dump for pid {pid} contains no CLR — is the target a managed process?"); | ||
| using var runtime = clr.CreateRuntime(); | ||
| foreach (var obj in runtime.Heap.EnumerateObjects()) | ||
| { | ||
| var name = obj.Type?.Name; | ||
| if (name != null && wanted.Contains(name)) | ||
| { | ||
| counts[name]++; | ||
| } | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| File.Delete(dump); // dumps are large; the counts are the artifact, not the dump | ||
| } | ||
| return counts; | ||
| } | ||
|
|
||
| private void RunProcdump(int pid, string dumpPath) | ||
| { | ||
| // -ma = full dump (managed heap included), -accepteula for unattended runs. | ||
| var psi = new ProcessStartInfo(_procdump, $"-accepteula -ma {pid} \"{dumpPath}\"") | ||
| { | ||
| UseShellExecute = false, | ||
| RedirectStandardOutput = true, | ||
| RedirectStandardError = true, | ||
| CreateNoWindow = true, | ||
| }; | ||
| using var p = Process.Start(psi)!; | ||
| // Drain both pipes asynchronously BEFORE waiting: if procdump fills a pipe | ||
| // buffer while the harness blocks in WaitForExit(), both deadlock. Bound the | ||
| // wait so a stuck procdump can't hang the harness forever, and check the | ||
| // exit code, not just whether a file appeared. | ||
| var stdout = p.StandardOutput.ReadToEndAsync(); | ||
| var stderr = p.StandardError.ReadToEndAsync(); | ||
| if (!p.WaitForExit(120_000)) | ||
| { | ||
| try { p.Kill(); } catch { /* best effort */ } | ||
| throw new IOException($"procdump timed out (>120s) for pid {pid}"); | ||
| } | ||
| Task.WaitAll(stdout, stderr); | ||
| if (p.ExitCode != 0 || !File.Exists(dumpPath)) | ||
| { | ||
| throw new IOException( | ||
| $"procdump failed for pid {pid} (exit {p.ExitCode}): {stderr.Result}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <!-- | ||
| Own.NET Audit — leak-harness (Plan.md §4.1). Build-required, WINDOWS-ONLY: | ||
| needs a running target, procdump (Sysinternals) and ClrMD. This project is NOT | ||
| part of the Linux CI gate — the Python ingest bridge (audit/runtime/ingest.py) | ||
| is what CI tests. net472 to match the legacy target's runtime so ClrMD reads its | ||
| heap and FlaUI drives its WPF/WinForms UI. | ||
| --> | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net472</TargetFramework> | ||
| <LangVersion>latest</LangVersion> | ||
| <Nullable>enable</Nullable> | ||
| <AssemblyName>LeakHarness</AssemblyName> | ||
| <RootNamespace>OwnNet.Audit.Runtime</RootNamespace> | ||
| <Platforms>x64</Platforms> | ||
| <!-- <Platforms> only declares availability; pin the build/run bitness so ClrMD | ||
| reads the full dump and FlaUI inspects the target at a predictable arch. --> | ||
| <PlatformTarget>x64</PlatformTarget> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="FlaUI.UIA3" Version="4.0.0" /> | ||
| <PackageReference Include="Microsoft.Diagnostics.Runtime" Version="3.1.512801" /> | ||
| <PackageReference Include="YamlDotNet" Version="15.1.2" /> | ||
| <PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> | ||
| </ItemGroup> | ||
| </Project> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.