| 1 |
From 9e0fe492c29c1f8ce762af959a281d431bfe32fc Mon Sep 17 00:00:00 2001 |
| 2 |
From: Sergio Ammirata <[email protected]> |
| 3 |
Date: Mon, 29 Jun 2026 12:39:54 -0400 |
| 4 |
Subject: [PATCH] rist: make recovery-depth maximum platform-aware (fix 32-bit |
| 5 |
test) |
| 6 |
Forwarded: https://code.videolan.org/rist/librist/-/work_items/222 |
| 7 |
|
| 8 |
recovery-depth N sizes the Advanced retransmission ring to UINT16_SIZE << N |
| 9 |
packets. RIST_RECOVERY_DEPTH_MAX is 16, i.e. 2^32 packets - the full 32-bit |
| 10 |
sequence space. The ring is two parallel arrays (one rist_buffer* and one |
| 11 |
uint32_t per slot), so 2^32 slots are only addressable when size_t is 64 bits. |
| 12 |
On a 32-bit size_t the slot count overflows, rist_recovery_depth_apply() |
| 13 |
correctly refused it (-2), and rist_recovery_depth_set() returned failure for |
| 14 |
the documented maximum. The recovery_depth unit test, which expects set(200) |
| 15 |
to clamp to the maximum and succeed, therefore failed on 32-bit targets; its |
| 16 |
own DEPTH_PKTS(MAX) expectation also overflowed to 0. |
| 17 |
|
| 18 |
Add rist_recovery_depth_platform_max() in rist-private.h: the largest exponent |
| 19 |
whose ring (UINT16_SIZE << depth) fits SIZE_MAX / (sizeof(ptr) + sizeof(u32)). |
| 20 |
On LP64 this is RIST_RECOVERY_DEPTH_MAX; on a 32-bit size_t it resolves lower |
| 21 |
(depth 12). rist_recovery_depth_to_packets() now clamps to it - covering both |
| 22 |
the public setter and the ?recovery-depth= / config apply path - so the result |
| 23 |
is always representable. The setter logs when a request is capped. The unit |
| 24 |
test uses the same shared helper for its expectation, so it tracks whatever |
| 25 |
the platform supports. |
| 26 |
|
| 27 |
apply()'s representability/OOM guard is kept as a safety net. No change on |
| 28 |
64-bit, where the maximum is unchanged. |
| 29 |
|
| 30 |
Fixes #222. |
| 31 |
--- |
| 32 |
src/rist-private.h | 16 ++++++++++++++++ |
| 33 |
src/rist.c | 19 +++++++++++++++---- |
| 34 |
test/rist/unit/test_recovery_depth.c | 10 ++++++---- |
| 35 |
3 files changed, 37 insertions(+), 8 deletions(-) |
| 36 |
|
| 37 |
Index: git/src/rist-private.h |
| 38 |
=================================================================== |
| 39 |
--- git.orig/src/rist-private.h |
| 40 |
+++ git/src/rist-private.h |
| 41 |
@@ -138,6 +138,22 @@ struct rist_buffer { |
| 42 |
bool retry_queued; |
| 43 |
}; |
| 44 |
|
| 45 |
+/* Largest recovery depth whose ring (UINT16_SIZE << depth packets) is |
| 46 |
+ * addressable as size_t: the two parallel index arrays cost |
| 47 |
+ * sizeof(ptr) + sizeof(uint32_t) per slot. Depth 16 (2^32) only fits a 64-bit |
| 48 |
+ * size_t; on a 32-bit size_t the exponent is reduced until it fits. Shared with |
| 49 |
+ * the recovery-depth unit test. */ |
| 50 |
+static inline int rist_recovery_depth_platform_max(void) |
| 51 |
+{ |
| 52 |
+ const uint64_t per = (uint64_t)sizeof(struct rist_buffer *) + sizeof(uint32_t); |
| 53 |
+ const uint64_t limit = (uint64_t)SIZE_MAX / per; |
| 54 |
+ int depth = RIST_RECOVERY_DEPTH_MAX; |
| 55 |
+ while (depth > RIST_RECOVERY_DEPTH_MIN && |
| 56 |
+ ((uint64_t)UINT16_SIZE << depth) > limit) |
| 57 |
+ depth--; |
| 58 |
+ return depth; |
| 59 |
+} |
| 60 |
+ |
| 61 |
struct rist_missing_buffer { |
| 62 |
uint32_t seq; |
| 63 |
uint64_t next_nack; |
| 64 |
Index: git/src/rist.c |
| 65 |
=================================================================== |
| 66 |
--- git.orig/src/rist.c |
| 67 |
+++ git/src/rist.c |
| 68 |
@@ -800,16 +800,19 @@ int rist_recovery_rtt_multiplier_set(str |
| 69 |
return rist_recovery_rtt_multiplier_set_internal(cctx, multiplier); |
| 70 |
} |
| 71 |
|
| 72 |
-/* Translate a recovery-depth exponent into the ring capacity in packets: |
| 73 |
- * (65536 << depth). depth is clamped to [MIN, MAX]. Returned as uint64_t |
| 74 |
- * because the top of the range (depth 16) is 2^32, which overflows a 32-bit |
| 75 |
- * size_t; the apply path rejects any size this platform cannot index. */ |
| 76 |
+/* Translate a recovery-depth exponent into ring capacity in packets |
| 77 |
+ * (65536 << depth), clamped to [MIN, MAX] and to the platform maximum |
| 78 |
+ * (rist_recovery_depth_platform_max) so the result always fits size_t. |
| 79 |
+ * Returned as uint64_t so the arithmetic is exact regardless of size_t width. */ |
| 80 |
static uint64_t rist_recovery_depth_to_packets(int depth) |
| 81 |
{ |
| 82 |
if (depth < RIST_RECOVERY_DEPTH_MIN) |
| 83 |
depth = RIST_RECOVERY_DEPTH_MIN; |
| 84 |
if (depth > RIST_RECOVERY_DEPTH_MAX) |
| 85 |
depth = RIST_RECOVERY_DEPTH_MAX; |
| 86 |
+ int platform_max = rist_recovery_depth_platform_max(); |
| 87 |
+ if (depth > platform_max) |
| 88 |
+ depth = platform_max; |
| 89 |
return (uint64_t)UINT16_SIZE << depth; |
| 90 |
} |
| 91 |
|
| 92 |
@@ -892,6 +895,14 @@ int rist_recovery_depth_set(struct rist_ |
| 93 |
depth = RIST_RECOVERY_DEPTH_MAX; |
| 94 |
} |
| 95 |
|
| 96 |
+ int platform_max = rist_recovery_depth_platform_max(); |
| 97 |
+ if (depth > platform_max) { |
| 98 |
+ rist_log_priv(cctx, RIST_LOG_WARN, |
| 99 |
+ "recovery depth %u exceeds what this platform can address, " |
| 100 |
+ "capping to %d\n", (unsigned)depth, platform_max); |
| 101 |
+ depth = platform_max; |
| 102 |
+ } |
| 103 |
+ |
| 104 |
return rist_recovery_depth_apply(ctx, cctx, rist_recovery_depth_to_packets(depth)); |
| 105 |
} |
| 106 |
|
| 107 |
Index: git/test/rist/unit/test_recovery_depth.c |
| 108 |
=================================================================== |
| 109 |
--- git.orig/test/rist/unit/test_recovery_depth.c |
| 110 |
+++ git/test/rist/unit/test_recovery_depth.c |
| 111 |
@@ -130,18 +130,20 @@ static void test_main_profile_sender(voi |
| 112 |
static void test_out_of_range_clamps(void) |
| 113 |
{ |
| 114 |
/* Use a receiver: it records recovery_queue_max without allocating the |
| 115 |
- * (potentially huge) ring up front, so we can check the clamp 200 -> MAX |
| 116 |
- * without depending on the host's overcommit behavior. */ |
| 117 |
+ * (potentially huge) ring up front, so we can check the clamp of an |
| 118 |
+ * over-range request without depending on the host's overcommit behavior. */ |
| 119 |
struct rist_ctx *ctx = NULL; |
| 120 |
if (rist_receiver_create(&ctx, RIST_PROFILE_ADVANCED, NULL) != 0 || !ctx) { |
| 121 |
fprintf(stderr, "FAIL: could not create advanced receiver (clamp)\n"); |
| 122 |
failures++; |
| 123 |
return; |
| 124 |
} |
| 125 |
+ /* expect the platform cap, computed with the same helper the library uses */ |
| 126 |
+ int pmax = rist_recovery_depth_platform_max(); |
| 127 |
CHECK(rist_recovery_depth_set(ctx, 200) == 0, "set(200) (clamped) failed"); |
| 128 |
- CHECK(common_max(ctx) == DEPTH_PKTS(RIST_RECOVERY_DEPTH_MAX), |
| 129 |
+ CHECK(common_max(ctx) == DEPTH_PKTS(pmax), |
| 130 |
"clamp: recovery_queue_max=%zu want %zu (depth %d)", |
| 131 |
- common_max(ctx), DEPTH_PKTS(RIST_RECOVERY_DEPTH_MAX), RIST_RECOVERY_DEPTH_MAX); |
| 132 |
+ common_max(ctx), DEPTH_PKTS(pmax), pmax); |
| 133 |
rist_destroy(ctx); |
| 134 |
} |
| 135 |
|