Commit 8d62623510 for openssl.org
commit 8d62623510676f8598bf9a15d06eec7a9e747cd7
Author: 007bsd <007bsd@proton.me>
Date: Fri Nov 13 17:35:02 2026 +0200
Test that an active send part keeps a deleted stream alive
Add test_stream_gc_send_active: a bidirectional stream is read to
the end by the application, so its receive part is terminal, and is
then written to, concluded and freed before that data has been sent.
The stream must not be garbage collected while its send part still
has data the peer has not acknowledged, the peer must receive all of
it, and the stream must be collected once it has been acknowledged.
Assisted-by: Claude:claude-opus-5-5
Reviewed-by: Tomas Mraz <tomas@openssl.foundation>
Reviewed-by: Saša NedvÄ›dický <sashan@openssl.org>
Merge-date: Mon Nov 16 13:10:34 2026
Merged-from: https://github.com/openssl/openssl/pull/33718
diff --git a/test/quicapitest.c b/test/quicapitest.c
index eb7ddf5a5c..7804374a3a 100644
--- a/test/quicapitest.c
+++ b/test/quicapitest.c
@@ -4822,6 +4822,143 @@ err:
return testresult;
}
+/*
+ * A deleted bidirectional stream whose receive part is terminal must not be
+ * garbage collected while its send part still has data which the peer has not
+ * acknowledged. Here the application reads the stream to the end, then writes
+ * to it, concludes it and frees it before that data has been sent.
+ */
+static int test_stream_gc_send_active(void)
+{
+ static const unsigned char msg[] = "sent after the receive part is terminal";
+ SSL_CTX *cctx = NULL, *sctx = NULL;
+ SSL *clientssl = NULL, *serverssl = NULL, *qlistener = NULL;
+ SSL *sstream = NULL, *cstream = NULL;
+ QUIC_CHANNEL *ch;
+ QUIC_STREAM_MAP *qsm;
+ QUIC_STREAM *qs;
+ unsigned char buf[sizeof(msg) + 16];
+ size_t written = 0, readbytes = 0, total = 0;
+ uint64_t sid;
+ int testresult = 0, ret, i, eof = 0;
+
+ if (!TEST_ptr(sctx = create_server_ctx())
+ || !TEST_ptr(cctx = create_client_ctx())
+ || !create_quic_ssl_objects(sctx, cctx, &qlistener, &clientssl))
+ goto err;
+
+ for (i = 0; i < 2; i++) {
+ ret = SSL_connect(clientssl);
+ if (!TEST_int_le(ret, 0)
+ || !TEST_int_eq(SSL_get_error(clientssl, ret),
+ SSL_ERROR_WANT_READ))
+ goto err;
+ SSL_handle_events(qlistener);
+ }
+
+ if (!TEST_ptr(serverssl = SSL_accept_connection(qlistener, 0))
+ || !TEST_true(create_bare_ssl_connection(serverssl, clientssl,
+ SSL_ERROR_NONE, 0, 0))
+ || !TEST_true(SSL_set_incoming_stream_policy(clientssl,
+ SSL_INCOMING_STREAM_POLICY_ACCEPT, 0))
+ || !TEST_ptr(ch = ossl_quic_conn_get_channel(clientssl)))
+ goto err;
+
+ qsm = ossl_quic_channel_get_qsm(ch);
+
+ /* The server opens a bidirectional stream, writes to it and concludes. */
+ if (!TEST_ptr(sstream = SSL_new_stream(serverssl, 0))
+ || !TEST_true(SSL_write_ex(sstream, "x", 1, &written))
+ || !TEST_true(SSL_stream_conclude(sstream, 0))
+ || !TEST_int_eq(SSL_handle_events(serverssl), 1)
+ || !TEST_int_eq(SSL_handle_events(clientssl), 1)
+ || !TEST_ptr(cstream = SSL_accept_stream(clientssl,
+ SSL_ACCEPT_STREAM_NO_BLOCK)))
+ goto err;
+
+ /* The client reads it to the end, so its receive part is terminal. */
+ for (i = 0;; i++) {
+ if (!TEST_int_lt(i, 10))
+ goto err;
+ if (SSL_read_ex(cstream, buf, sizeof(buf), &readbytes))
+ continue;
+ if (SSL_get_error(cstream, 0) == SSL_ERROR_ZERO_RETURN)
+ break;
+ if (!TEST_int_eq(SSL_get_error(cstream, 0), SSL_ERROR_WANT_READ)
+ || !TEST_int_eq(SSL_handle_events(serverssl), 1)
+ || !TEST_int_eq(SSL_handle_events(clientssl), 1))
+ goto err;
+ }
+
+ /* Write, conclude and free the stream while the data is still pending. */
+ sid = SSL_get_stream_id(cstream);
+ if (!TEST_true(SSL_write_ex(cstream, msg, sizeof(msg), &written))
+ || !TEST_size_t_eq(written, sizeof(msg))
+ || !TEST_true(SSL_stream_conclude(cstream, 0)))
+ goto err;
+ SSL_free(cstream);
+ cstream = NULL;
+
+ /*
+ * The stream is deleted and its receive part is terminal, but the peer has
+ * not acknowledged the data yet, so it must not be collected.
+ */
+ if (!TEST_ptr(qs = ossl_quic_stream_map_get_by_id(qsm, sid))
+ || !TEST_true(qs->deleted)
+ || !TEST_int_eq(qs->recv_state, QUIC_RSTREAM_STATE_DATA_READ)
+ || !TEST_true(qs->send_state == QUIC_SSTREAM_STATE_SEND
+ || qs->send_state == QUIC_SSTREAM_STATE_DATA_SENT)
+ || !TEST_false(qs->ready_for_gc))
+ goto err;
+
+ /* Sent, but still not acknowledged: the stream must remain. */
+ if (!TEST_int_eq(SSL_handle_events(clientssl), 1)
+ || !TEST_ptr(qs = ossl_quic_stream_map_get_by_id(qsm, sid))
+ || !TEST_int_ne(qs->send_state, QUIC_SSTREAM_STATE_DATA_RECVD)
+ || !TEST_false(qs->ready_for_gc))
+ goto err;
+
+ /*
+ * The server receives all of the data and the FIN. Once the client has
+ * seen that acknowledged, the stream is collected.
+ */
+ for (i = 0; i < 1000; i++) {
+ if (!eof) {
+ if (SSL_read_ex(sstream, buf + total, sizeof(buf) - total,
+ &readbytes))
+ total += readbytes;
+ else if (SSL_get_error(sstream, 0) == SSL_ERROR_ZERO_RETURN)
+ eof = 1;
+ else if (!TEST_int_eq(SSL_get_error(sstream, 0),
+ SSL_ERROR_WANT_READ))
+ goto err;
+ }
+ if (eof && ossl_quic_stream_map_get_by_id(qsm, sid) == NULL)
+ break;
+ if (!TEST_int_eq(SSL_handle_events(serverssl), 1)
+ || !TEST_int_eq(SSL_handle_events(clientssl), 1))
+ goto err;
+ OSSL_sleep(1);
+ }
+
+ if (!TEST_true(eof)
+ || !TEST_mem_eq(buf, total, msg, sizeof(msg))
+ || !TEST_ptr_null(ossl_quic_stream_map_get_by_id(qsm, sid)))
+ goto err;
+
+ testresult = 1;
+err:
+ SSL_free(cstream);
+ SSL_free(sstream);
+ SSL_free(serverssl);
+ SSL_free(clientssl);
+ SSL_free(qlistener);
+ SSL_CTX_free(sctx);
+ SSL_CTX_free(cctx);
+
+ return testresult;
+}
+
/*
* When the server has a different primary group than the client, the server
* should not fail on the client hello retry.
@@ -5802,6 +5939,7 @@ int setup_tests(void)
ADD_TEST(test_ssl_set_verify);
ADD_TEST(test_accept_stream);
ADD_ALL_TESTS(test_stream_gc, 3);
+ ADD_TEST(test_stream_gc_send_active);
ADD_TEST(test_client_hello_retry);
#if OPENSSL_USE_IPV6
ADD_TEST(test_quic_peer_addr_v6);