From 9e0fe492c29c1f8ce762af959a281d431bfe32fc Mon Sep 17 00:00:00 2001 From: Sergio Ammirata Date: Mon, 29 Jun 2026 12:39:54 -0400 Subject: [PATCH] rist: make recovery-depth maximum platform-aware (fix 32-bit test) Forwarded: https://code.videolan.org/rist/librist/-/work_items/222 recovery-depth N sizes the Advanced retransmission ring to UINT16_SIZE << N packets. RIST_RECOVERY_DEPTH_MAX is 16, i.e. 2^32 packets - the full 32-bit sequence space. The ring is two parallel arrays (one rist_buffer* and one uint32_t per slot), so 2^32 slots are only addressable when size_t is 64 bits. On a 32-bit size_t the slot count overflows, rist_recovery_depth_apply() correctly refused it (-2), and rist_recovery_depth_set() returned failure for the documented maximum. The recovery_depth unit test, which expects set(200) to clamp to the maximum and succeed, therefore failed on 32-bit targets; its own DEPTH_PKTS(MAX) expectation also overflowed to 0. Add rist_recovery_depth_platform_max() in rist-private.h: the largest exponent whose ring (UINT16_SIZE << depth) fits SIZE_MAX / (sizeof(ptr) + sizeof(u32)). On LP64 this is RIST_RECOVERY_DEPTH_MAX; on a 32-bit size_t it resolves lower (depth 12). rist_recovery_depth_to_packets() now clamps to it - covering both the public setter and the ?recovery-depth= / config apply path - so the result is always representable. The setter logs when a request is capped. The unit test uses the same shared helper for its expectation, so it tracks whatever the platform supports. apply()'s representability/OOM guard is kept as a safety net. No change on 64-bit, where the maximum is unchanged. Fixes #222. --- src/rist-private.h | 16 ++++++++++++++++ src/rist.c | 19 +++++++++++++++---- test/rist/unit/test_recovery_depth.c | 10 ++++++---- 3 files changed, 37 insertions(+), 8 deletions(-) Index: git/src/rist-private.h =================================================================== --- git.orig/src/rist-private.h +++ git/src/rist-private.h @@ -138,6 +138,22 @@ struct rist_buffer { bool retry_queued; }; +/* Largest recovery depth whose ring (UINT16_SIZE << depth packets) is + * addressable as size_t: the two parallel index arrays cost + * sizeof(ptr) + sizeof(uint32_t) per slot. Depth 16 (2^32) only fits a 64-bit + * size_t; on a 32-bit size_t the exponent is reduced until it fits. Shared with + * the recovery-depth unit test. */ +static inline int rist_recovery_depth_platform_max(void) +{ + const uint64_t per = (uint64_t)sizeof(struct rist_buffer *) + sizeof(uint32_t); + const uint64_t limit = (uint64_t)SIZE_MAX / per; + int depth = RIST_RECOVERY_DEPTH_MAX; + while (depth > RIST_RECOVERY_DEPTH_MIN && + ((uint64_t)UINT16_SIZE << depth) > limit) + depth--; + return depth; +} + struct rist_missing_buffer { uint32_t seq; uint64_t next_nack; Index: git/src/rist.c =================================================================== --- git.orig/src/rist.c +++ git/src/rist.c @@ -800,16 +800,19 @@ int rist_recovery_rtt_multiplier_set(str return rist_recovery_rtt_multiplier_set_internal(cctx, multiplier); } -/* Translate a recovery-depth exponent into the ring capacity in packets: - * (65536 << depth). depth is clamped to [MIN, MAX]. Returned as uint64_t - * because the top of the range (depth 16) is 2^32, which overflows a 32-bit - * size_t; the apply path rejects any size this platform cannot index. */ +/* Translate a recovery-depth exponent into ring capacity in packets + * (65536 << depth), clamped to [MIN, MAX] and to the platform maximum + * (rist_recovery_depth_platform_max) so the result always fits size_t. + * Returned as uint64_t so the arithmetic is exact regardless of size_t width. */ static uint64_t rist_recovery_depth_to_packets(int depth) { if (depth < RIST_RECOVERY_DEPTH_MIN) depth = RIST_RECOVERY_DEPTH_MIN; if (depth > RIST_RECOVERY_DEPTH_MAX) depth = RIST_RECOVERY_DEPTH_MAX; + int platform_max = rist_recovery_depth_platform_max(); + if (depth > platform_max) + depth = platform_max; return (uint64_t)UINT16_SIZE << depth; } @@ -892,6 +895,14 @@ int rist_recovery_depth_set(struct rist_ depth = RIST_RECOVERY_DEPTH_MAX; } + int platform_max = rist_recovery_depth_platform_max(); + if (depth > platform_max) { + rist_log_priv(cctx, RIST_LOG_WARN, + "recovery depth %u exceeds what this platform can address, " + "capping to %d\n", (unsigned)depth, platform_max); + depth = platform_max; + } + return rist_recovery_depth_apply(ctx, cctx, rist_recovery_depth_to_packets(depth)); } Index: git/test/rist/unit/test_recovery_depth.c =================================================================== --- git.orig/test/rist/unit/test_recovery_depth.c +++ git/test/rist/unit/test_recovery_depth.c @@ -130,18 +130,20 @@ static void test_main_profile_sender(voi static void test_out_of_range_clamps(void) { /* Use a receiver: it records recovery_queue_max without allocating the - * (potentially huge) ring up front, so we can check the clamp 200 -> MAX - * without depending on the host's overcommit behavior. */ + * (potentially huge) ring up front, so we can check the clamp of an + * over-range request without depending on the host's overcommit behavior. */ struct rist_ctx *ctx = NULL; if (rist_receiver_create(&ctx, RIST_PROFILE_ADVANCED, NULL) != 0 || !ctx) { fprintf(stderr, "FAIL: could not create advanced receiver (clamp)\n"); failures++; return; } + /* expect the platform cap, computed with the same helper the library uses */ + int pmax = rist_recovery_depth_platform_max(); CHECK(rist_recovery_depth_set(ctx, 200) == 0, "set(200) (clamped) failed"); - CHECK(common_max(ctx) == DEPTH_PKTS(RIST_RECOVERY_DEPTH_MAX), + CHECK(common_max(ctx) == DEPTH_PKTS(pmax), "clamp: recovery_queue_max=%zu want %zu (depth %d)", - common_max(ctx), DEPTH_PKTS(RIST_RECOVERY_DEPTH_MAX), RIST_RECOVERY_DEPTH_MAX); + common_max(ctx), DEPTH_PKTS(pmax), pmax); rist_destroy(ctx); }