[PATCH v3] sched: fix infinite loop in rational approximation

Alexander.Chesnokov at kaspersky.com Alexander.Chesnokov at kaspersky.com
Thu Nov 26 11:17:41 CET 2026


From: Alexander Chesnokov <Alexander.Chesnokov at kaspersky.com>

rte_approx() scales alpha and d by 10 until d reaches 1,
multiplying denum by 10 on every step. For d below 1e-9 the loop
makes 10 or more steps, denum wraps in uint32_t and no longer
equals 10^k. The input check still passes, the search compares
against the wrapped denum, never finds a match and spins forever.
A fuzzer input with alpha = 2.65e-23 and d = 1.13e-23 (23 scaling
steps, denum = 4135583744) hangs the function.

The same wrapping breaks rte_approx_64() for d below 1e-19. Besides,
both variants hang even with an intact denum once denum squared does
not fit the type: the products in the search overflow and the
comparisons produce garbage. The 32-bit variant hangs on
alpha = 0.333333333 with d = 1.5e-9, the 64-bit variant on
alpha = 7.38e-19 with d = 6.78e-19.

Reject requests whose precision cannot be represented: stop scaling
before denum wraps and require denum * denum to fit the type, so
that every product in the search stays exact. Requests outside these
bounds could hang forever, or compute against a wrapped denominator
and return a fraction unrelated to the request. Requests within the
bounds behave exactly as before; the sched configuration calls
rte_approx_64() with the constant 1e-7, which keeps working. The
smallest supported precision, 1e-4 for rte_approx() and 1e-9 for
rte_approx_64(), is now stated in the API header.

Add sched_approx_autotest cases for the 32-bit variant: the two
hanging requests are rejected, and a coarse request still returns
the minimal fraction inside the requested interval. rte_approx_64()
gets the same guards but is not called by the test: the symbol is
not exported from the shared library, so an external caller cannot
link against it.

Fixes: de3cfa2c9823 ("sched: initial import")
Cc: stable at dpdk.org
Cc: jasvinder.singh at intel.com

Signed-off-by: Alexander Chesnokov <Alexander.Chesnokov at kaspersky.com>
---
v3:
- Call only rte_approx() in the test: rte_approx_64() is not
  exported from the shared library (no RTE_EXPORT_SYMBOL since
  its introduction), which broke the shared build of app/dpdk-test.
  The 32-bit variant now covers both guards: the denum wrap and
  the denum squared overflow.
- State the smallest supported precision of both variants in the
  API header Doxygen.

 app/test/test_sched.c  | 35 +++++++++++++++++++++++++++++++++++
 lib/sched/rte_approx.c | 10 ++++++++++
 lib/sched/rte_approx.h |  6 ++++--
 3 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/app/test/test_sched.c b/app/test/test_sched.c
index 96b48f1897..62396766f7 100644
--- a/app/test/test_sched.c
+++ b/app/test/test_sched.c
@@ -22,8 +22,16 @@ test_sched(void)
 	printf("sched not supported on Windows, skipping test\n");
 	return TEST_SKIPPED;
 }
+
+static int
+test_sched_approx(void)
+{
+	printf("sched not supported on Windows, skipping test\n");
+	return TEST_SKIPPED;
+}
 #else
 
+#include <rte_approx.h>
 #include <rte_sched.h>
 
 #define SUBPORT         0
@@ -213,6 +221,33 @@ test_sched(void)
 	return 0;
 }
 
+static int
+test_sched_approx(void)
+{
+	uint32_t p32 = 0;
+	uint32_t q32 = 0;
+	int err;
+
+	/* denum wraps in uint32_t during scaling */
+	err = rte_approx(2.647139521348302e-23, 1.126263171287887e-23,
+		&p32, &q32);
+	TEST_ASSERT_FAIL(err, "rte_approx accepted unrepresentable d\n");
+
+	/* denum * denum does not fit in uint32_t */
+	err = rte_approx(0.333333333, 1.5e-9, &p32, &q32);
+	TEST_ASSERT_FAIL(err, "rte_approx accepted d = 1.5e-9\n");
+
+	/* Coarse precision still succeeds */
+	err = rte_approx(0.125, 0.01, &p32, &q32);
+	TEST_ASSERT_SUCCESS(err, "rte_approx rejected d = 0.01\n");
+	TEST_ASSERT_EQUAL(p32, 1, "Wrong approximation p\n");
+	TEST_ASSERT_EQUAL(q32, 8, "Wrong approximation q\n");
+
+	return 0;
+}
+
 #endif /* !RTE_EXEC_ENV_WINDOWS */
 
 REGISTER_FAST_TEST(sched_autotest, NOHUGE_OK, ASAN_OK, test_sched);
+REGISTER_FAST_TEST(sched_approx_autotest, NOHUGE_OK, ASAN_OK,
+	test_sched_approx);
diff --git a/lib/sched/rte_approx.c b/lib/sched/rte_approx.c
index 86c7d1d3fb..7af4191df1 100644
--- a/lib/sched/rte_approx.c
+++ b/lib/sched/rte_approx.c
@@ -157,10 +157,15 @@ int rte_approx(double alpha, double d, uint32_t *p, uint32_t *q)
 	/* Compute alpha_num, d_num and denum */
 	denum = 1;
 	while (d < 1) {
+		if (denum > UINT32_MAX / 10)
+			return -1;
 		alpha *= 10;
 		d *= 10;
 		denum *= 10;
 	}
+	/* the search products must fit in uint32_t */
+	if (denum > UINT32_MAX / denum)
+		return -1;
 	alpha_num = (uint32_t) alpha;
 	d_num = (uint32_t) d;
 
@@ -310,10 +315,15 @@ int rte_approx_64(double alpha, double d, uint64_t *p, uint64_t *q)
 	/* Compute alpha_num, d_num and denum */
 	denum = 1;
 	while (d < 1) {
+		if (denum > UINT64_MAX / 10)
+			return -1;
 		alpha *= 10;
 		d *= 10;
 		denum *= 10;
 	}
+	/* the search products must fit in uint64_t */
+	if (denum > UINT64_MAX / denum)
+		return -1;
 	alpha_num = (uint64_t) alpha;
 	d_num = (uint64_t) d;
 
diff --git a/lib/sched/rte_approx.h b/lib/sched/rte_approx.h
index 738e33a98b..4a4b047406 100644
--- a/lib/sched/rte_approx.h
+++ b/lib/sched/rte_approx.h
@@ -34,7 +34,8 @@ extern "C" {
  *   Pointer to pre-allocated space where the denominator of the rational
  *   approximation will be stored when operation is successful
  * @return
- *   0 upon success, error code otherwise
+ *   0 upon success, error code otherwise. The smallest supported
+ *   precision is 1e-4; a request for a smaller d returns an error
  */
 int rte_approx(double alpha, double d, uint32_t *p, uint32_t *q);
 
@@ -52,7 +53,8 @@ int rte_approx(double alpha, double d, uint32_t *p, uint32_t *q);
  *   Pointer to pre-allocated space where the denominator of the rational
  *   approximation will be stored when operation is successful
  * @return
- *   0 upon success, error code otherwise
+ *   0 upon success, error code otherwise. The smallest supported
+ *   precision is 1e-9; a request for a smaller d returns an error
  */
 int rte_approx_64(double alpha, double d, uint64_t *p, uint64_t *q);
 
-- 
2.53.0



More information about the stable mailing list