Skip to content
Closed
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
24 changes: 19 additions & 5 deletions src/protocols/ioxide.http2/Http2ResponseWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,14 @@ public Memory<byte> 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;
}

/// <summary>
Expand Down Expand Up @@ -434,11 +437,22 @@ private async ValueTask<FlushResult> 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<byte>.Shared.Rent(Math.Max(needed, DefaultChunk));
if (_staged > 0)
Expand Down
24 changes: 19 additions & 5 deletions src/protocols/ioxide.http3/Http3ResponseWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,14 @@ public Memory<byte> 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;
}

/// <summary>
Expand Down Expand Up @@ -277,11 +280,22 @@ private async ValueTask<FlushResult> 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<byte>.Shared.Rent(Math.Max(needed, DefaultChunk));
if (_staged > 0)
Expand Down
19 changes: 16 additions & 3 deletions src/protocols/ioxide.nghttp2/Http/Nghttp2ResponseWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte>.Shared.Rent(size);
Expand Down
25 changes: 20 additions & 5 deletions src/protocols/ioxide.nghttp3/Http/Nghttp3ResponseWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ private void SubmitUnsent()
/// <inheritdoc />
public unsafe Span<byte> 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<byte>(_staging + _staged, _stagingCapacity - _staged);
}
Expand Down Expand Up @@ -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;
}

/// <summary>
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tests/Ioxide.Tests.E2E/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
84 changes: 84 additions & 0 deletions tests/Ioxide.Tests.E2E/Protocols/H3StreamedWriterBoundsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Buffers;
using System.Text;
using ioxide;
using ioxide.http3;
using ioxide.nghttp3;
using ioxide.ngtcp2;

namespace Ioxide.Tests;

/// <summary>
/// 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.
/// </summary>
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<IBufferWriter<byte>, string> probe)
{
var told = new TaskCompletionSource<string>(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<IBufferWriter<byte>, string> probe)
{
var told = new TaskCompletionSource<string>(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<string> 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<QuicConnection, Task> 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;
}
}
15 changes: 15 additions & 0 deletions tests/Ioxide.Tests.E2E/Protocols/Http3BodyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
}

/// <param name="chunk">
Expand Down
54 changes: 54 additions & 0 deletions tests/Ioxide.Tests.Harness/BufferWriterProbes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Buffers;

namespace Ioxide.Tests;

/// <summary>
/// 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 <see cref="Staged"/>
/// first and says what the writer did, so a test can assert that, and that the staged bytes still go out after it.
/// </summary>
public static class BufferWriterProbes
{
/// <summary>What each probe stages before it reaches past the block.</summary>
public static ReadOnlySpan<byte> Staged => "0123456789abcdef"u8;

/// <summary>
/// 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.
/// </summary>
public static string Overreach(IBufferWriter<byte> 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}";
}

/// <summary>An Advance of int.MaxValue past a span handed out beside the staged bytes: "refused", or "advanced".</summary>
public static string Overrun(IBufferWriter<byte> writer)
{
writer.Write(Staged);
writer.GetSpan(1);
try
{
writer.Advance(int.MaxValue);
return "advanced";
}
catch (InvalidOperationException)
{
return "refused";
}
}

private static string Asked(Func<int> ask)
{
try
{
return $"handed out {ask()} bytes";
}
catch (OutOfMemoryException)
{
return "refused";
}
}
}
1 change: 1 addition & 0 deletions tests/Ioxide.Tests.Unit/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading
Loading