From 549815e5b201c640cc3f42cc161e321acff88e91 Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Sat, 14 Nov 2026 20:57:15 +0000 Subject: [PATCH] nghttp3, http2, http3, nghttp2: the streamed writers stage what was written to the buffer handed out last, and refuse a hint or an Advance past what a block holds rather than wrap the sum (#617) IBufferWriter has Advance apply to the buffer handed out last, and GetSpan and GetMemory return at least the hint or throw. nghttp3: GetMemory marked its scratch as outstanding and GetSpan left the mark, so GetMemory, GetSpan, Advance copied the scratch over the bytes written into the span. GetSpan now ends an outstanding Memory. All four writers checked room as _staged + hint in int. Beside staged bytes a hint near int.MaxValue wrapped that sum negative, the check passed, and GetSpan or GetMemory handed out the rest of the block instead of throwing. The room check is now done in uint, where two non-negative ints cannot wrap, and growth refuses a block past Array.MaxLength (int.MaxValue for nghttp3's native block) with OutOfMemoryException. Advance in http2, http3 and nghttp3 had the same int sum in its bounds check, so a count near int.MaxValue was accepted and left the staged count negative; it is checked in uint too. Growth moves out of line, so the room check still inlines into GetSpan. --- .../ioxide.http2/Http2ResponseWriter.cs | 24 ++++-- .../ioxide.http3/Http3ResponseWriter.cs | 24 ++++-- .../Http/Nghttp2ResponseWriter.cs | 19 ++++- .../Http/Nghttp3ResponseWriter.cs | 25 ++++-- tests/Ioxide.Tests.E2E/Program.cs | 1 + .../Protocols/H3StreamedWriterBoundsTests.cs | 84 +++++++++++++++++++ .../Protocols/Http3BodyTests.cs | 15 ++++ .../BufferWriterProbes.cs | 54 ++++++++++++ tests/Ioxide.Tests.Unit/Program.cs | 1 + .../StreamedWriterBoundsTests.cs | 80 ++++++++++++++++++ 10 files changed, 309 insertions(+), 18 deletions(-) create mode 100644 tests/Ioxide.Tests.E2E/Protocols/H3StreamedWriterBoundsTests.cs create mode 100644 tests/Ioxide.Tests.Harness/BufferWriterProbes.cs create mode 100644 tests/Ioxide.Tests.Unit/StreamedWriterBoundsTests.cs diff --git a/src/protocols/ioxide.http2/Http2ResponseWriter.cs b/src/protocols/ioxide.http2/Http2ResponseWriter.cs index 3ebd5c49..63641fdc 100644 --- a/src/protocols/ioxide.http2/Http2ResponseWriter.cs +++ b/src/protocols/ioxide.http2/Http2ResponseWriter.cs @@ -147,11 +147,14 @@ public Memory GetMemory(int sizeHint = 0) public void Advance(int count) { ArgumentOutOfRangeException.ThrowIfNegative(count); - if (_staged + count > _staging.Length) + + // In uint, where two non-negative ints cannot wrap: an int sum wraps negative for a count near int.MaxValue, and passes. + uint staged = (uint)_staged + (uint)count; + if (staged > (uint)_staging.Length) { throw new InvalidOperationException("Advanced past the end of the span handed out by GetSpan."); } - _staged += count; + _staged = (int)staged; } /// @@ -434,11 +437,22 @@ private async ValueTask SendAfterCreditAsync(bool endStream, int se private void EnsureStaging(int sizeHint) { - int needed = _staged + sizeHint; - if (_staging.Length >= needed) + // In uint, where two non-negative ints cannot wrap: an int sum wraps negative for a hint near int.MaxValue, and passes for room. + if ((uint)_staged + (uint)sizeHint > (uint)_staging.Length) { - return; + GrowStaging(sizeHint); + } + } + + // Out of line, so the check above stays small enough to inline into GetSpan. + [MethodImpl(MethodImplOptions.NoInlining)] + private void GrowStaging(int sizeHint) + { + if (sizeHint > Array.MaxLength - _staged) + { + throw new OutOfMemoryException($"The staging array cannot hold {(long)_staged + sizeHint} bytes; flush before asking for more."); } + int needed = _staged + sizeHint; byte[] grown = ArrayPool.Shared.Rent(Math.Max(needed, DefaultChunk)); if (_staged > 0) diff --git a/src/protocols/ioxide.http3/Http3ResponseWriter.cs b/src/protocols/ioxide.http3/Http3ResponseWriter.cs index 31e9a1c7..ef0de38a 100644 --- a/src/protocols/ioxide.http3/Http3ResponseWriter.cs +++ b/src/protocols/ioxide.http3/Http3ResponseWriter.cs @@ -143,11 +143,14 @@ public Memory GetMemory(int sizeHint = 0) public void Advance(int count) { ArgumentOutOfRangeException.ThrowIfNegative(count); - if (_staged + count > _staging.Length) + + // In uint, where two non-negative ints cannot wrap: an int sum wraps negative for a count near int.MaxValue, and passes. + uint staged = (uint)_staged + (uint)count; + if (staged > (uint)_staging.Length) { throw new InvalidOperationException("Advanced past the end of the span handed out by GetSpan."); } - _staged += count; + _staged = (int)staged; } /// @@ -277,11 +280,22 @@ private async ValueTask FlushCore(bool fin) private void EnsureStaging(int sizeHint) { - int needed = _staged + sizeHint; - if (_staging.Length >= needed) + // In uint, where two non-negative ints cannot wrap: an int sum wraps negative for a hint near int.MaxValue, and passes for room. + if ((uint)_staged + (uint)sizeHint > (uint)_staging.Length) { - return; + GrowStaging(sizeHint); + } + } + + // Out of line, so the check above stays small enough to inline into GetSpan. + [MethodImpl(MethodImplOptions.NoInlining)] + private void GrowStaging(int sizeHint) + { + if (sizeHint > Array.MaxLength - _staged) + { + throw new OutOfMemoryException($"The staging array cannot hold {(long)_staged + sizeHint} bytes; flush before asking for more."); } + int needed = _staged + sizeHint; byte[] grown = ArrayPool.Shared.Rent(Math.Max(needed, DefaultChunk)); if (_staged > 0) diff --git a/src/protocols/ioxide.nghttp2/Http/Nghttp2ResponseWriter.cs b/src/protocols/ioxide.nghttp2/Http/Nghttp2ResponseWriter.cs index 939f843e..e91ed6c5 100644 --- a/src/protocols/ioxide.nghttp2/Http/Nghttp2ResponseWriter.cs +++ b/src/protocols/ioxide.nghttp2/Http/Nghttp2ResponseWriter.cs @@ -265,11 +265,24 @@ internal async ValueTask FailAsync() private void EnsureStaging(int sizeHint) { - int needed = _staged + Math.Max(sizeHint, 1); - if (_staging.Length >= needed) + sizeHint = Math.Max(sizeHint, 1); + + // In uint, where two non-negative ints cannot wrap: an int sum wraps negative for a hint near int.MaxValue, and passes for room. + if ((uint)_staged + (uint)sizeHint > (uint)_staging.Length) { - return; + GrowStaging(sizeHint); + } + } + + // Out of line, so the check above stays small enough to inline into GetSpan. + [MethodImpl(MethodImplOptions.NoInlining)] + private void GrowStaging(int sizeHint) + { + if (sizeHint > Array.MaxLength - _staged) + { + throw new OutOfMemoryException($"The staging array cannot hold {(long)_staged + sizeHint} bytes; flush before asking for more."); } + int needed = _staged + sizeHint; int size = Math.Max(needed, Math.Max(4096, _staging.Length * 2)); byte[] grown = ArrayPool.Shared.Rent(size); diff --git a/src/protocols/ioxide.nghttp3/Http/Nghttp3ResponseWriter.cs b/src/protocols/ioxide.nghttp3/Http/Nghttp3ResponseWriter.cs index 227dc763..110b2846 100644 --- a/src/protocols/ioxide.nghttp3/Http/Nghttp3ResponseWriter.cs +++ b/src/protocols/ioxide.nghttp3/Http/Nghttp3ResponseWriter.cs @@ -186,6 +186,7 @@ private void SubmitUnsent() /// public unsafe Span GetSpan(int sizeHint = 0) { + _memoryOutstanding = false; // Advance applies to the buffer handed out last, and from here that is this span EnsureStaging(sizeHint <= 0 ? 1 : sizeHint); return new Span(_staging + _staged, _stagingCapacity - _staged); } @@ -236,11 +237,13 @@ public unsafe void Advance(int count) return; } - if (_staged + count > _stagingCapacity) + // In uint, where two non-negative ints cannot wrap: an int sum wraps negative for a count near int.MaxValue, and passes. + uint staged = (uint)_staged + (uint)count; + if (staged > (uint)_stagingCapacity) { throw new InvalidOperationException("Advanced past the end of the span handed out by GetSpan."); } - _staged += count; + _staged = (int)staged; } /// @@ -478,11 +481,23 @@ private void ReleaseScratch() private unsafe void EnsureStaging(int sizeHint) { - int needed = _staged + sizeHint; - if (_staging != null && _stagingCapacity >= needed) + // In uint, where two non-negative ints cannot wrap: an int sum wraps negative for a hint near int.MaxValue, and passes for room. + if (_staging == null || (uint)_staged + (uint)sizeHint > (uint)_stagingCapacity) { - return; + GrowStaging(sizeHint); } + } + + // Out of line, so the check above stays small enough to inline into GetSpan. + [MethodImpl(MethodImplOptions.NoInlining)] + private unsafe void GrowStaging(int sizeHint) + { + // A span is at most int.MaxValue long, so that is all a block can hold. + if (sizeHint > int.MaxValue - _staged) + { + throw new OutOfMemoryException($"The staging block cannot hold {(long)_staged + sizeHint} bytes; flush before asking for more."); + } + int needed = _staged + sizeHint; int capacity = Math.Max(needed, DefaultChunk); byte* grown = (byte*)NativeMemory.Alloc((nuint)capacity); diff --git a/tests/Ioxide.Tests.E2E/Program.cs b/tests/Ioxide.Tests.E2E/Program.cs index a471a316..17ea3cca 100644 --- a/tests/Ioxide.Tests.E2E/Program.cs +++ b/tests/Ioxide.Tests.E2E/Program.cs @@ -76,6 +76,7 @@ private static int Main() EarlyAnswerTests.Register(runner); QuicStreamingCreditTests.Register(runner); Http3BodyTests.Register(runner); + H3StreamedWriterBoundsTests.Register(runner); Http3RequestReuseTests.Register(runner); H3BodyModeTests.Register(runner); H3BufferedFaultTests.Register(runner); diff --git a/tests/Ioxide.Tests.E2E/Protocols/H3StreamedWriterBoundsTests.cs b/tests/Ioxide.Tests.E2E/Protocols/H3StreamedWriterBoundsTests.cs new file mode 100644 index 00000000..96fba61e --- /dev/null +++ b/tests/Ioxide.Tests.E2E/Protocols/H3StreamedWriterBoundsTests.cs @@ -0,0 +1,84 @@ +using System.Buffers; +using System.Text; +using ioxide; +using ioxide.http3; +using ioxide.nghttp3; +using ioxide.ngtcp2; + +namespace Ioxide.Tests; + +/// +/// The IBufferWriter edges of the streamed writers on both HTTP/3 stacks: a hint no staging block holds beside what +/// is staged is refused rather than handed less, and an Advance past the span is refused however large its count. +/// Each handler runs a probe, and the response it then completes has to carry what the probe staged. +/// +internal static class H3StreamedWriterBoundsTests +{ + public static void Register(Runner runner) + { + runner.Test("h3 writer: GetSpan and GetMemory refuse a hint the staging block cannot hold beside what it has staged, rather than hand out less", () => + AssertNghttp3("GetSpan refused, GetMemory refused", BufferWriterProbes.Overreach)); + + runner.Test("h3 writer: an Advance past the span it was handed is refused, however large its count", () => + AssertNghttp3("refused", BufferWriterProbes.Overrun)); + + runner.Test("http3 writer: GetSpan and GetMemory refuse a hint the staging array cannot hold beside what it has staged, rather than hand out less", () => + AssertHttp3("GetSpan refused, GetMemory refused", BufferWriterProbes.Overreach)); + + runner.Test("http3 writer: an Advance past the span it was handed is refused, however large its count", () => + AssertHttp3("refused", BufferWriterProbes.Overrun)); + } + + private static void AssertNghttp3(string expected, Func, string> probe) + { + var told = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + string body = Get(conn => new Nghttp3Connection(conn).RunAsync(async (_, _, writer) => + { + writer.WriteHeaders(new Nghttp3Response { Status = 200 }); + told.SetResult(probe(writer)); + await writer.CompleteAsync(); + })); + + AssertProbed(expected, told.Task, body); + } + + private static void AssertHttp3(string expected, Func, string> probe) + { + var told = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + string body = Get(conn => new Http3Connection(conn).RunAsync(async (_, _, writer) => + { + writer.WriteHeaders(new Http3Response { Status = 200 }); + told.SetResult(probe(writer)); + await writer.CompleteAsync(); + })); + + AssertProbed(expected, told.Task, body); + } + + // What the probe reported first: a writer it got past may never end its response at all. + private static void AssertProbed(string expected, Task told, string body) + { + Assert.True(told.Wait(5_000), "the handler never ran its probe"); + Assert.Equal(expected, told.Result); + Assert.True(body == Encoding.ASCII.GetString(BufferWriterProbes.Staged), + $"the response carried '{body}', not the bytes staged before the probe"); + } + + private static string Get(Func serve) + { + (string certPath, string keyPath) = TestCert.Ensure(); + var engine = TestServer.Own(new QuicEngine(certPath, keyPath, cidLength: 8, alpn: ["h3"])); + + (_, int udpPort) = TestServer.StartDatagram( + onDatagram: null, + quicFactory: engine.CreateFactory(), + quicHandle: (_, conn) => serve(conn)); + + using var client = new H3TestClient("127.0.0.1", udpPort); + client.Connect(); + Assert.True(client.CompleteHandshake(timeoutMs: 5000), "handshake did not complete"); + + (_, string body) = client.Get("/", timeoutMs: 15_000); + return body; + } +} diff --git a/tests/Ioxide.Tests.E2E/Protocols/Http3BodyTests.cs b/tests/Ioxide.Tests.E2E/Protocols/Http3BodyTests.cs index 8c8e8063..10b76b5d 100644 --- a/tests/Ioxide.Tests.E2E/Protocols/Http3BodyTests.cs +++ b/tests/Ioxide.Tests.E2E/Protocols/Http3BodyTests.cs @@ -113,6 +113,21 @@ public static void Register(Runner runner) await writer.CompleteAsync(); }); }); + + runner.Test("h3 body: a span asked for after a Memory is what Advance sends", () => + { + // Advance applies to the buffer handed out last. The Memory before the span is filled, so a writer that + // stages it instead shows that, whatever the pool's array held before. + byte[] file = Pattern(26); + + AssertServedBy(file, (writer, body) => + { + writer.GetMemory(body.Length).Span.Fill((byte)'#'); + body.CopyTo(writer.GetSpan(body.Length)); + writer.Advance(body.Length); + return writer.CompleteAsync(); + }); + }); } /// diff --git a/tests/Ioxide.Tests.Harness/BufferWriterProbes.cs b/tests/Ioxide.Tests.Harness/BufferWriterProbes.cs new file mode 100644 index 00000000..4b6c44e0 --- /dev/null +++ b/tests/Ioxide.Tests.Harness/BufferWriterProbes.cs @@ -0,0 +1,54 @@ +using System.Buffers; + +namespace Ioxide.Tests; + +/// +/// The edges of the IBufferWriter contract a streamed response writer has to hold: GetSpan and GetMemory hand out at +/// least the hint or throw, and Advance never runs past what was handed out. Each probe stages +/// first and says what the writer did, so a test can assert that, and that the staged bytes still go out after it. +/// +public static class BufferWriterProbes +{ + /// What each probe stages before it reaches past the block. + public static ReadOnlySpan Staged => "0123456789abcdef"u8; + + /// + /// GetSpan, then GetMemory, with a hint of int.MaxValue beside the staged bytes: no block holds that much, so + /// "refused" for each, or how much was handed out instead. + /// + public static string Overreach(IBufferWriter writer) + { + writer.Write(Staged); + string span = Asked(() => writer.GetSpan(int.MaxValue).Length); + string memory = Asked(() => writer.GetMemory(int.MaxValue).Length); + return $"GetSpan {span}, GetMemory {memory}"; + } + + /// An Advance of int.MaxValue past a span handed out beside the staged bytes: "refused", or "advanced". + public static string Overrun(IBufferWriter writer) + { + writer.Write(Staged); + writer.GetSpan(1); + try + { + writer.Advance(int.MaxValue); + return "advanced"; + } + catch (InvalidOperationException) + { + return "refused"; + } + } + + private static string Asked(Func ask) + { + try + { + return $"handed out {ask()} bytes"; + } + catch (OutOfMemoryException) + { + return "refused"; + } + } +} diff --git a/tests/Ioxide.Tests.Unit/Program.cs b/tests/Ioxide.Tests.Unit/Program.cs index bba8de8b..a9905472 100644 --- a/tests/Ioxide.Tests.Unit/Program.cs +++ b/tests/Ioxide.Tests.Unit/Program.cs @@ -38,6 +38,7 @@ private static int Main() Http2RequestReuseTests.Register(runner); Nghttp2RequestReuseTests.Register(runner); Http2StreamedBodyTests.Register(runner); + StreamedWriterBoundsTests.Register(runner); Http2FlowControlTests.Register(runner); Http2ReceiveWindowTests.Register(runner); Http2BufferedCreditTests.Register(runner); diff --git a/tests/Ioxide.Tests.Unit/StreamedWriterBoundsTests.cs b/tests/Ioxide.Tests.Unit/StreamedWriterBoundsTests.cs new file mode 100644 index 00000000..6974d752 --- /dev/null +++ b/tests/Ioxide.Tests.Unit/StreamedWriterBoundsTests.cs @@ -0,0 +1,80 @@ +using System.Buffers; +using System.Text; +using ioxide.http2; +using ioxide.nghttp2; + +namespace Ioxide.Tests; + +/// +/// The IBufferWriter edges of the streamed writers on both HTTP/2 stacks: a hint no staging array holds beside what +/// is staged is refused rather than handed less, and an Advance past the span is refused however large its count. +/// Each handler runs a probe, and the response it then completes has to carry what the probe staged. +/// +internal static class StreamedWriterBoundsTests +{ + public static void Register(Runner runner) + { + runner.Test("h2 writer: GetSpan and GetMemory refuse a hint the staging array cannot hold beside what it has staged, rather than hand out less", () => + AssertHttp2("GetSpan refused, GetMemory refused", BufferWriterProbes.Overreach)); + + runner.Test("h2 writer: an Advance past the span it was handed is refused, however large its count", () => + AssertHttp2("refused", BufferWriterProbes.Overrun)); + + runner.Test("nghttp2 writer: GetSpan and GetMemory refuse a hint the staging array cannot hold beside what it has staged, rather than hand out less", () => + AssertNghttp2("GetSpan refused, GetMemory refused", BufferWriterProbes.Overreach)); + } + + private static void AssertHttp2(string expected, Func, string> probe) + { + string? told = null; + using var client = new StrictClient(); + Task run = client.Connection.RunAsync(async (_, writer) => + { + writer.WriteHeaders(new Http2Response { Status = 200 }); + told = probe(writer); + await writer.CompleteAsync(); + }); + + client.ReleaseFlush(); // server SETTINGS + client.SendRequests(1); + List frames = client.Drain(); + + Assert.Equal(expected, told); + AssertStaged(Frame.Body(frames, streamId: 1)); + client.Close(run); + } + + private static void AssertNghttp2(string expected, Func, string> probe) + { + string? told = null; + using var client = new StrictClient(); + var connection = new Nghttp2Connection(client); + Task run = connection.RunAsync((_, writer) => + { + writer.WriteHeaders(new Nghttp2Response { Status = 200 }); + told = probe(writer); + return ValueTask.CompletedTask; + }); + + client.SendRequests(); // preface and SETTINGS + client.Drain(); + client.Feed(Get(1)); + List frames = client.Drain(); + + Assert.Equal(expected, told); + AssertStaged(Frame.Body(frames, streamId: 1)); + client.Close(run); + connection.Dispose(); + } + + private static void AssertStaged(byte[] body) + => Assert.True(body.AsSpan().SequenceEqual(BufferWriterProbes.Staged), + $"the stream carried '{Encoding.ASCII.GetString(body)}', not the bytes staged before the probe"); + + // A GET with END_STREAM: :scheme http, :path /, and an :authority, which nghttp2 requires. + private static byte[] Get(int streamId) + { + byte[] block = [0x82, 0x86, 0x84, 0x01, 9, .. "a.example"u8.ToArray()]; + return Http2FlowControlTests.FrameBytes(0x1, 0x5, streamId, block); + } +}