From 39d06bcbb8498b472e946cc7bc690eb5775d3872 Mon Sep 17 00:00:00 2001 From: Matthew Lorimor Date: Fri, 6 Nov 2026 08:36:01 -0600 Subject: [PATCH 1/2] Bound an edited claim's read by a cut its check refuses, read beside it (#345) TestAClaimNoCutCanSeeIsRefusedBeforeItIsPricedOrAllocatedWhenEdited, from #345, failed for its SublimeCountMinSketch.CheckShape row in one of four full runs on master, 56 bytes past its bound, and on another branch 8 past it. It passed alone. The cause is the baseline. The row held the edited claim's read to the read of a cut just before the field, its envelope's difference and 1,024 bytes. What a read allocates depends on the code the JIT has made of it so far: optimised code inlines and devirtualises away hundreds of bytes, and with dynamic PGO on, the code in between came out higher than either. A cut before the field is refused on another path than the claim, for the bytes it lacks, so the two are optimised apart, and the difference moves with the run. - The failing figures were 82,904 against a bound of 82,896, and 82,976 against 82,920. - Probed alone, from cold through rounds of reads to hot, under the default runtime, with tiering, dynamic PGO or stack allocation off, and under the segments collector, the claim's read ran from -672 to 1,080 bytes past the old cut's. With tiering off it settled at 432 after one round. - A probe that reads every other structure's cuts and makes the Sublime cut's path hot, then runs the real row, failed it byte for byte (82,976 against 82,920) in 7 of 8 runs on master dde2413, 4 regions and 3 of 4 segments, and in 2 of 3 on 2a4dfbc. - Not the cause: the shared ArrayPool's trim, which after a gen2 GC drops an array a thread has held for 30 seconds of trims, sooner under load (SharedArrayPool.Trim, .NET 10.0.14, read 2026-11-06). The forced failure came in a run of about a second, too short for that. Nor an event listener or first-chance handler: the suite has none. The baseline is now the payload cut just after the field, which the same check refuses at its writer's claim, so it runs the claim's code and differs only in the numbers its messages name. Premises hold that: its price stops at the check, for what it lacks, having priced what the cut before the field does, and each of its reads is refused by the check for what it lacks. It is read under the same budget as the edited read, just before it and just after it, in one no-GC region, and the larger is taken, so neither the JIT nor a pool trim can move one between them unseen. A budget changes a read only by refusing it (its limit is only ever compared with what is charged), and the cut is charged no more than its own price, within every budget the edited read is given. Refusing stays 1,024 bytes. - The forcing probe's row passed 8 of 8 runs, 4 under each collector, on the fix as first written and again on this commit. - In all 30 probed states the claim's read came out 48 bytes past the new cut's, where the old cut's ran from -120 to 1,080 in the same states. - Eight instrumented full runs on 2a4dfbc, alternating the collectors, all passed, and the difference was the same in every read of a claim: HeavyKeeper 32 and 96 (one past, the largest), TopK 32 and 80, Sublime 48. A read of the most the bytes hold came out past the cut's, with the envelope's difference, by 75,928 (HeavyKeeper), 154,560 to 155,024 (TopK) and 4,728 to 5,816 (Sublime); the least in any probed state was 4,632. - Those two were measured on the fix as first written, which read the cut under its own price where this reads it under the edited read's; by the above the two allocate alike. They were not measured again. - On this commit, the whole suite six times, alternating the collectors, passed every time, 2,668 tests each, and a clean -warnaserror build exited 0 with the suite passing after it. Mutation through scripts/mutate.sh, on this commit. The fix's own conditions, against this test alone unless marked: T1 price premise: the cut prices more killed (3) T2 price premise: the cut stops elsewhere killed (3) T3 price premise: refused for another reason killed (3) P1 read premise: dropped survived (a) P2 read premise: refused for another reason killed (3) P3 read premise: null guard dropped survived (b) P4 read premise: the read before only survived (b) F0 baseline: the cut before the field killed (3) T4 baseline: a byte before the field ends killed (3) T5 baseline: a byte after the cut survived (c) W baseline: the payload whole killed (3) T6 beside: the smaller of the two survived (d) T7 beside: the read before only survived (d) T8 Refusing: nothing killed (3) T9 Refusing: 1 << 13, whole suite killed (1) T9b Refusing: 1 << 14 killed (1) T10 bound: the envelope difference dropped killed (3) T11 bound: the envelope difference reversed killed (3) T13 control: dropped survived (a) (a) Guards, which no correct library can fail. Without the read premise T4 survived alone (measured on the commit before it), and with it F0, T4 and W are killed alone. A larger Refusing loosens only the edited reads' bound, so only the control can kill T9 and T9b. (b) Equivalent. A cut that reads fails the test anyway, on its Message; and the second read of the same bytes under the same budget is refused as the first is, since the JIT changes code, not what it does. (c) Any cut the check refuses for what it lacks is as good a baseline, and the read premise holds that one is. The cut at the writer's claim is the coarsest. (d) Not killed: no test can place a change of the JIT between two reads. Kept because the edited read ran from 82,008 to 82,976 bytes across the measured states, 968 bytes against 976 of room, so one change landing between the read before and the edited read could take nearly all of it. The read after sees the edited read's code. Under the forcing probe, on the commit before the read premise, T6 and T7 passed and F0 and T4 failed. The #344 library mutants, against the three #344 tests alone: E1-E3, E5-E10 and E12-E16, R1-R4 killed. E4 and E11, Slice's RequireAhead dropped and a byte laxer, survived, and survive the whole suite here and on master dde2413 (2,668 tests each). Before #349 the nested-envelope test killed them; #349 held it to RequireHeld, which since #347 refuses those claims first. This commit leaves that test alone. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KK3MspqAcczxT45xCfLgyx --- .../TestReadsAllocateOnlyWhatTheyHold.cs | 91 ++++++++++++++----- 1 file changed, 66 insertions(+), 25 deletions(-) diff --git a/TestProbabilisticDataStructures/TestReadsAllocateOnlyWhatTheyHold.cs b/TestProbabilisticDataStructures/TestReadsAllocateOnlyWhatTheyHold.cs index e0e94b1..cc48ae6 100644 --- a/TestProbabilisticDataStructures/TestReadsAllocateOnlyWhatTheyHold.cs +++ b/TestProbabilisticDataStructures/TestReadsAllocateOnlyWhatTheyHold.cs @@ -938,10 +938,11 @@ internal sealed record EditedClaim( Func FieldAt, Func CheckedAt, Func Most, uint Largest); /// - /// What the read of an edited claim may allocate past the read of a cut just before its - /// field, the envelope's difference aside: both are refused at once, and differ in their - /// messages. Measured, every one allocated less than that cut's read, by 56 to 696 bytes, - /// and a read of the most the bytes hold allocated at least 15,472 bytes more than it. + /// What the read of an edited claim may allocate past the read of the payload cut just after + /// its field, the envelope's difference aside: the same check refuses both, so they run the + /// same code, and differ only in the numbers their messages name. Measured, 32 to 96 bytes, + /// by claim, the same in every full run, and for Sublime in every state of the JIT probed; + /// and a read of the most the bytes hold allocated at least 4,632 bytes more than that cut's. /// private const long Refusing = 1 << 10; @@ -1021,10 +1022,28 @@ private static byte[] RoomFor(StructureId id) => /// edited here, as #332's was, in the sample the sweep cuts, its checksum resealed. The price /// of each edited claim stops where the check refuses it, having priced exactly what a cut /// just before the field does, and its read is refused for what it lacks under no budget, - /// under the default one and under exactly its own price, having allocated no more than the - /// read of that cut, the envelope's difference and . The most the bytes - /// hold is the control: its price walks past the check and prices more, and its read - /// allocates past that bound, so the bound can see what the check stops. + /// under the default one and under exactly its own price. It allocates no more than the read, + /// under the same budget, of the payload cut just after the field, which the check refuses + /// at its writer's claim, with the envelope's difference and . + /// The most the bytes hold is the control: its price walks past the check and prices more, + /// and its read allocates past that bound, so the bound can see what the check stops. + /// + /// The cut is the one after the field, not the one before it, and it is read just before and + /// just after each read it bounds, in one region, because what a read allocates depends on + /// the code the JIT has made of it so far. Optimised code allocates hundreds of bytes less + /// than the first code a method runs, and with dynamic PGO on, the code in between came out + /// higher than either; and that changes as the run goes on and as other tests make the + /// code they share hot. A cut just before the field is refused on another path than the + /// claim's, for the bytes it lacks, so the two are optimised apart. Read after every other + /// structure's cuts, with the Sublime cut's path made hot, a Sublime claim's read came out + /// 1,080 bytes past that cut's where 1,024 were allowed, the figures of a full run that + /// failed (#345). Measured from cold to hot, with tiering, dynamic PGO or stack allocation + /// off, and under the segments collector, that difference ran from -672 to 1,080 bytes. A + /// cut the same check refuses runs the same code as the claim, and differs from it only in + /// the numbers its messages name: measured the same ways, it came out at 48 bytes for + /// Sublime in every state, and in full runs at 48 for Sublime and 32 to 96, by claim, for + /// the element counts, every time. + /// /// [TestMethod] [DynamicData(nameof(EditedClaimCases))] @@ -1042,32 +1061,52 @@ public void TestAClaimNoCutCanSeeIsRefusedBeforeItIsPricedOrAllocatedWhenEdited( var envelope = PersistenceFormat.EnvelopeCost((ulong)length); Assert.IsGreaterThan(most, claim.Largest, $"premise: {checks}: the largest claim, {claim.Largest:N0}, is past the most the bytes hold, {most:N0}"); - InvalidDataException Refusal(byte[] payload, ReadOptions options, out long allocated) + var after = Cut(bytes, checkedAt); + + // What payload allocates read under options, and the larger of what the payload cut + // just after the field allocates read under the same options just before it and just + // after it, all three in one region. The cut is refused by the same check, at its + // writer's claim, so it runs the code the edited read does, and the JIT can change that + // code between any two reads, so the cut is read beside it rather than once beforehand. + // A budget changes a read only by refusing it, and the cut is charged no more than its + // own price, within every budget the edited read is given; each read of the cut is + // held to that, refused by the check for what it lacks, which no budget's refusal says. + (InvalidDataException Refused, long Allocated, long Beside) Measure(byte[] payload, ReadOptions options) { - InvalidDataException refused = null; - allocated = NoGCRegion.Allocated(() => + var refusals = new InvalidDataException[3]; + Func Reading(int i, byte[] what) => () => { - refused = null; try { - read(payload, options); + read(what, options); } catch (InvalidDataException e) { - refused = e; + refusals[i] = e; } - return refused; - }); - return refused; + return null; + }; + var counts = NoGCRegion.AllocatedEach([Reading(0, after), Reading(1, payload), Reading(2, after)]); + foreach (var cut in new[] { refusals[0], refusals[2] }) + { + Assert.IsTrue( + cut is not null && cut.Message.Contains(lacks, StringComparison.Ordinal), + $"premise: {checks}: the cut just after the field, read beside the read it bounds, is refused by the check " + + $"for what it lacks: {cut?.Message ?? "it was read"}"); + } + + return (refusals[1], counts[1], Math.Max(counts[0], counts[2])); } var before = WalkPrice(claim.Id, bytes, at); Assert.AreEqual(at, before.StoppedAt, $"premise: {checks}: a cut just before the field at {at:N0} is past every earlier check, and its price stops there: {before.Why}"); - var cut = Refusal(Cut(bytes, at), ReadOptions.Unlimited, out var cutAllocated); - Assert.IsNotNull(cut, $"premise: {checks}: a cut just before the field was read"); - var bound = cutAllocated - PersistenceFormat.EnvelopeCost((ulong)at) + envelope + Refusing; + var same = WalkPrice(claim.Id, bytes, checkedAt); + Assert.IsTrue(same.Priced == before.Priced && same.StoppedAt == checkedAt && (same.Why?.Contains(lacks, StringComparison.Ordinal) ?? false), + $"premise: {checks}: a cut just after the field is refused by the check, at its writer's claim, having priced what a cut " + + $"before it does: it priced {same.Priced - before.Priced:N0} more and stopped at {same.StoppedAt:N0}: {same.Why}"); + var shift = envelope - PersistenceFormat.EnvelopeCost((ulong)checkedAt); var fits = Edited(bytes, at, most); var taken = WalkPrice(claim.Id, fits, length); @@ -1075,10 +1114,10 @@ InvalidDataException Refusal(byte[] payload, ReadOptions options, out long alloc $"{checks}: with its {claim.Field} at {most:N0}, the most the bytes hold, it was refused where the check refuses: {taken.Why}"); Assert.IsGreaterThan(before.Priced, taken.Priced, $"{checks}: with its {claim.Field} at {most:N0}, the most the bytes hold, it priced no more than a cut before the field: {taken.Why}"); - Refusal(fits, ReadOptions.Unlimited, out var readInto); - Assert.IsGreaterThan(bound, readInto, + var (_, readInto, beside) = Measure(fits, ReadOptions.Unlimited); + Assert.IsGreaterThan(beside + shift + Refusing, readInto, $"{checks}: a read with its {claim.Field} at {most:N0}, the most the bytes hold, allocated {readInto:N0} bytes, within the " + - $"{bound:N0} a refused claim is held to, so that bound does not show the check stopped anything"); + $"{beside + shift + Refusing:N0} a refused claim is held to, so that bound does not show the check stopped anything"); var problems = new List(); foreach (var value in new[] { most + 1, claim.Largest }) @@ -1109,7 +1148,8 @@ InvalidDataException Refusal(byte[] payload, ReadOptions options, out long alloc ]; foreach (var (budget, options) in budgets) { - var refused = Refusal(edited, options, out var allocated); + var (refused, allocated, around) = Measure(edited, options); + var bound = around + shift + Refusing; var problem = refused is null ? $"with its {claim.Field} at {value:N0}, it was read under {budget}" : refused.InnerException is ReadBudgetExceededException @@ -1118,7 +1158,8 @@ InvalidDataException Refusal(byte[] payload, ReadOptions options, out long alloc ? $"with its {claim.Field} at {value:N0}, it was refused under {budget} for something else: {refused.Message}" : allocated > bound ? $"with its {claim.Field} at {value:N0}, it allocated {allocated:N0} bytes under {budget} before it " + - $"was refused, past the {bound:N0} a cut before the field, its envelope and {Refusing:N0} allow" + $"was refused, past the {bound:N0} that a cut just after the field, read beside it, its envelope and " + + $"{Refusing:N0} allow" : null; if (problem is not null) { From 3def4181570cbb92656ab8816dd268b63b4a5b5f Mon Sep 17 00:00:00 2001 From: Matthew Lorimor Date: Fri, 6 Nov 2026 09:20:23 -0600 Subject: [PATCH 2/2] Say in TESTING.md what refuses a nested length and what bounds an edited claim (#351) Two passages in the #344 bullet went stale. A nested envelope's length was said to be refused by the outer price's slice of it, with that check's loss showing as an ArgumentOutOfRangeException. Since #347 the outer price, from a held payload, holds the length to what the run holds after the header, with the checksum after it, before it prices it (RequireHeld, PersistenceFormat.PriceNested), and that is what refuses one longer: dropping the slice's check now passes the whole suite, on master dde2413 and on this branch. An edited claim's read was said to be bounded by a cut before the field. It is now bounded by the cut just after the field, which the same check refuses, read beside the edited read under the same budget, as the commit before this one made it. The passage says why: a cut before the field is refused on another path, which the JIT optimises apart from the claim's, and across the states of the JIT probed the difference ran from -672 to 1,080 bytes, past the 1,024 the bound allowed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KK3MspqAcczxT45xCfLgyx --- TESTING.md | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/TESTING.md b/TESTING.md index a078171..495fc3e 100644 --- a/TESTING.md +++ b/TESTING.md @@ -977,14 +977,19 @@ rest of a message survives, as it should, and one that deletes the guard does no shape check holds the current rows to the bytes after the shape, and every block is held again where it begins, so every cut it refuses is refused at the current rows at the latest, having read only blocks it holds. And a nested envelope's length is the one - claim no cut can make disagree with its run: the outer price's slice of that length is - what refuses one longer, and with that check gone the refusal was an - `ArgumentOutOfRangeException`. Each now has a test that edits the claim, its checksum - resealed, to one past what the bytes hold and to the largest the field can make. Each - edited claim is priced exactly what a cut just before the field is, and refused for what - it lacks under every budget, having allocated no more than that cut's read. The most the + claim no cut can make disagree with its run: since #347 the outer price, from a held + payload, holds it to what the run holds after the header, with the checksum after it, + before it prices it (`RequireHeld`), and that is what refuses one longer. Each now has a + test that edits the claim, its checksum resealed, to one past what the bytes hold and to + the largest the field can make. Each edited claim is priced exactly what a cut just + before the field is, and refused for what it lacks under every budget, having allocated + no more than the read, beside it and under the same budget, of the cut just after the + field, which the same check refuses. The bound was a cut before the field at first, but + that cut is refused on another path, which the JIT optimises apart from the claim's, and + across states of the JIT the difference ran from -672 to 1,080 bytes, past the 1,024 the + bound allowed (#351). A cut the same check refuses runs the claim's code. The most the bytes hold is the control, and its read allocates past that, so the bound can see what - the check stops. That bound is the cut's own read, not the sweep's slack, which is what + the check stops. That bound is a cut's own read, not the sweep's slack, which is what let #332's check pass unseen. `TestEveryHoldsCheckIsHeldByAnEditedClaimOrNamesTheTestThatHoldsIt` takes its roster from the library's IL, every call of `RequireHolds` with what it counts. So a check