Merge branch 'fix/w8-charge' into fix/w8-integration

This commit is contained in:
2026-09-13 11:13:40 +02:00
5 changed files with 114 additions and 3 deletions
+83
View File
@@ -412,6 +412,87 @@ static void test_protocol_accounting_release_does_not_underflow() {
protocol_session_unbind();
}
/* A Data acquired on session A must return its connection-memory charge to A
regardless of what (if anything) is bound at destroy time. The original bug
had two halves: destroying A's Data while a different session is bound leaks
A and drains the bound session, and destroying it with nothing bound leaks A
and drains the legacy fallback session. */
static void test_receive_data_charge_follows_owning_session() {
int pipe_a[2];
int pipe_b[2];
EXPECT_EQ_INT(pipe(pipe_a), 0);
EXPECT_EQ_INT(pipe(pipe_b), 0);
ProtocolSession session_a;
ProtocolSession session_b;
protocol_session_init(&session_a, pipe_a[0], pipe_a[1]);
protocol_session_init(&session_b, pipe_b[0], pipe_b[1]);
protocol_session_set_max_alloc(&session_a, 64);
protocol_session_set_max_alloc(&session_b, 64);
unsigned long long size = 8;
EXPECT_EQ_INT((int)write(pipe_a[1], &size, sizeof(size)), (int)sizeof(size));
EXPECT_EQ_INT((int)write(pipe_a[1], "12345678", 8), 8);
EXPECT_EQ_INT((int)write(pipe_a[1], &size, sizeof(size)), (int)sizeof(size));
EXPECT_EQ_INT((int)write(pipe_a[1], "ABCDEFGH", 8), 8);
EXPECT_EQ_INT((int)write(pipe_b[1], &size, sizeof(size)), (int)sizeof(size));
EXPECT_EQ_INT((int)write(pipe_b[1], "abcdefgh", 8), 8);
Data* data_a1 = protocol_receive_data_limited(&session_a, 8);
Data* data_a2 = protocol_receive_data_limited(&session_a, 8);
Data* data_b = protocol_receive_data_limited(&session_b, 8);
EXPECT_NOT_NULL(data_a1);
EXPECT_NOT_NULL(data_a2);
EXPECT_NOT_NULL(data_b);
EXPECT_TRUE(data_a1->owner == &session_a);
EXPECT_TRUE(data_a2->owner == &session_a);
EXPECT_TRUE(data_b->owner == &session_b);
EXPECT_EQ_INT((int)atomic_load(&session_a.total_allocated_bytes), 16);
EXPECT_EQ_INT((int)atomic_load(&session_b.total_allocated_bytes), 8);
/* Half 1: destroy A's Data while the unrelated session B is bound. The
charge must go to A, not to the bound B. */
protocol_session_bind(&session_b);
data_destroy(data_a1);
protocol_session_unbind();
EXPECT_EQ_INT((int)atomic_load(&session_a.total_allocated_bytes), 8);
EXPECT_EQ_INT((int)atomic_load(&session_b.total_allocated_bytes), 8);
/* Half 2: destroy A's remaining Data with NO session bound. The charge must
still go to A, not to the legacy fallback session. */
protocol_session_unbind();
data_destroy(data_a2);
EXPECT_EQ_INT((int)atomic_load(&session_a.total_allocated_bytes), 0);
EXPECT_EQ_INT((int)atomic_load(&session_b.total_allocated_bytes), 8);
data_destroy(data_b);
EXPECT_EQ_INT((int)atomic_load(&session_b.total_allocated_bytes), 0);
close(pipe_a[0]);
close(pipe_a[1]);
close(pipe_b[0]);
close(pipe_b[1]);
}
/* Freshest Data holds no connection charge; only a bounded receive binds an
owner and a charge, so creation helpers must start uncharged and unowned. */
static void test_data_create_starts_uncharged_and_unowned() {
void* buf = malloc(8);
EXPECT_NOT_NULL(buf);
Data* created = data_create(buf, 8);
EXPECT_NOT_NULL(created);
EXPECT_TRUE(created->owner == NULL);
EXPECT_EQ_INT((int)created->protocol_charge, 0);
data_destroy(created);
Data* reserved = data_create_reserve(64);
EXPECT_NOT_NULL(reserved);
EXPECT_TRUE(reserved->owner == NULL);
EXPECT_EQ_INT((int)reserved->protocol_charge, 0);
data_destroy(reserved);
}
static void test_protocol_session_io_timeout() {
/* Default is the built-in 60 s window; the setter stores exactly what it is
* given (<= 0 means "fall back to the default") so callers can propagate
@@ -574,4 +655,6 @@ void test_protocol() {
test_protocol_accounting_reservation_is_atomic();
test_protocol_string_accounting_is_transient();
test_protocol_accounting_release_does_not_underflow();
test_receive_data_charge_follows_owning_session();
test_data_create_starts_uncharged_and_unowned();
}