* [PATCH 1/3] eal: fix pointer arithmetic with an expression argument
[not found] <20220821205009.1317044-1-dmitry.kozliuk@gmail.com>
@ 2022-08-21 20:50 ` Dmitry Kozlyuk
2022-08-22 7:14 ` Morten Brørup
[not found] ` <20220825153709.33181-1-dmitry.kozliuk@gmail.com>
1 sibling, 1 reply; 5+ messages in thread
From: Dmitry Kozlyuk @ 2022-08-21 20:50 UTC (permalink / raw)
To: dev; +Cc: Dmitry Kozlyuk, stable
RTE_PTR_SUB(ptr, x) and RTE_PTR_ALIGN_FLOOR() worked incorrectly
if "ptr" was an expression:
uint32_t arr[3];
RTE_PTR_SUB(arr + 1, sizeof(arr[0]));
// expected: (uint32_t *)((uintptr_t)(arr + 1) - 4) == arr
// actual: (uint32_t *)((uintptr_t) arr + 1 - 4) != arr
RTE_PTR_ALIGN_FLOOR(arr + 2, sizeof(arr[0]));
// expected: RTE_ALIGN_FLOOR((uintptr_t)(arr + 2), 4) == &arr[2]
// actual: RTE_ALIGN_FLOOR((uintptr_t) arr + 2, 4) == &arr[0]
Fix the macros and extend the relevant unit test.
Fixes: af75078fece3 ("first public release")
Cc: stable@dpdk.org
Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
---
app/test/test_common.c | 11 +++++++++++
lib/eal/include/rte_common.h | 4 ++--
2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/app/test/test_common.c b/app/test/test_common.c
index ef177cecb1..4194c1208a 100644
--- a/app/test/test_common.c
+++ b/app/test/test_common.c
@@ -31,6 +31,7 @@ test_macros(int __rte_unused unused_parm)
uintptr_t unused = 0;
unsigned int smaller = SMALLER, bigger = BIGGER;
+ uint32_t arr[3];
RTE_SET_USED(unused);
@@ -41,6 +42,16 @@ test_macros(int __rte_unused unused_parm)
FAIL_MACRO(RTE_PTR_ADD);
if ((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF) != SMALLER)
FAIL_MACRO(RTE_PTR_SUB);
+ if (RTE_PTR_ADD(arr + 1, sizeof(arr[0])) != &arr[2])
+ FAIL_MACRO(RTE_PTR_ADD);
+ if (RTE_PTR_SUB(arr + 1, sizeof(arr[0])) != &arr[0])
+ FAIL_MACRO(RTE_PTR_SUB);
+ if (RTE_PTR_ALIGN_FLOOR(arr + 2, 4) != &arr[2])
+ FAIL_MACRO(RTE_PTR_ALIGN_FLOOR);
+ if (RTE_PTR_ALIGN_CEIL(arr + 2, 4) != &arr[2])
+ FAIL_MACRO(RTE_PTR_ALIGN_CEIL);
+ if (RTE_PTR_ALIGN(arr + 2, 4) != &arr[2])
+ FAIL_MACRO(RTE_PTR_ALIGN);
if (RTE_PTR_DIFF(BIGGER, SMALLER) != PTR_DIFF)
FAIL_MACRO(RTE_PTR_DIFF);
if (RTE_MAX(SMALLER, BIGGER) != BIGGER)
diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h
index a96cc2a138..d517e9f75f 100644
--- a/lib/eal/include/rte_common.h
+++ b/lib/eal/include/rte_common.h
@@ -295,7 +295,7 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
/**
* subtract a byte-value offset from a pointer
*/
-#define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
+#define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
/**
* get the difference between two pointer values, i.e. how far apart
@@ -320,7 +320,7 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
* must be a power-of-two value.
*/
#define RTE_PTR_ALIGN_FLOOR(ptr, align) \
- ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
+ ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
/**
* Macro to align a value to a given power-of-two. The resultant value
--
2.33.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: [PATCH 1/3] eal: fix pointer arithmetic with an expression argument
2022-08-21 20:50 ` [PATCH 1/3] eal: fix pointer arithmetic with an expression argument Dmitry Kozlyuk
@ 2022-08-22 7:14 ` Morten Brørup
0 siblings, 0 replies; 5+ messages in thread
From: Morten Brørup @ 2022-08-22 7:14 UTC (permalink / raw)
To: Dmitry Kozlyuk, dev; +Cc: stable
> From: Dmitry Kozlyuk [mailto:dmitry.kozliuk@gmail.com]
> Sent: Sunday, 21 August 2022 22.50
>
> RTE_PTR_SUB(ptr, x) and RTE_PTR_ALIGN_FLOOR() worked incorrectly
> if "ptr" was an expression:
>
> uint32_t arr[3];
>
> RTE_PTR_SUB(arr + 1, sizeof(arr[0]));
> // expected: (uint32_t *)((uintptr_t)(arr + 1) - 4) == arr
> // actual: (uint32_t *)((uintptr_t) arr + 1 - 4) != arr
>
> RTE_PTR_ALIGN_FLOOR(arr + 2, sizeof(arr[0]));
> // expected: RTE_ALIGN_FLOOR((uintptr_t)(arr + 2), 4) == &arr[2]
> // actual: RTE_ALIGN_FLOOR((uintptr_t) arr + 2, 4) == &arr[0]
>
> Fix the macros and extend the relevant unit test.
Good catch. Serious bugs!
>
> Fixes: af75078fece3 ("first public release")
> Cc: stable@dpdk.org
>
> Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
> ---
> app/test/test_common.c | 11 +++++++++++
> lib/eal/include/rte_common.h | 4 ++--
> 2 files changed, 13 insertions(+), 2 deletions(-)
>
> diff --git a/app/test/test_common.c b/app/test/test_common.c
> index ef177cecb1..4194c1208a 100644
> --- a/app/test/test_common.c
> +++ b/app/test/test_common.c
> @@ -31,6 +31,7 @@ test_macros(int __rte_unused unused_parm)
>
> uintptr_t unused = 0;
> unsigned int smaller = SMALLER, bigger = BIGGER;
> + uint32_t arr[3];
>
> RTE_SET_USED(unused);
>
> @@ -41,6 +42,16 @@ test_macros(int __rte_unused unused_parm)
> FAIL_MACRO(RTE_PTR_ADD);
> if ((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF) != SMALLER)
> FAIL_MACRO(RTE_PTR_SUB);
> + if (RTE_PTR_ADD(arr + 1, sizeof(arr[0])) != &arr[2])
> + FAIL_MACRO(RTE_PTR_ADD);
> + if (RTE_PTR_SUB(arr + 1, sizeof(arr[0])) != &arr[0])
> + FAIL_MACRO(RTE_PTR_SUB);
Very elegant test cases. :-)
> + if (RTE_PTR_ALIGN_FLOOR(arr + 2, 4) != &arr[2])
> + FAIL_MACRO(RTE_PTR_ALIGN_FLOOR);
> + if (RTE_PTR_ALIGN_CEIL(arr + 2, 4) != &arr[2])
> + FAIL_MACRO(RTE_PTR_ALIGN_CEIL);
While you are at it, consider adding a few more test cases, e.g.
RTE_PTR_ALIGN_FLOOR/CEIL(RTE_PTR_ADD(&arr[1], 1), 4), and
RTE_PTR_ALIGN_FLOOR/CEIL(RTE_PTR_ADD(&arr[1], sizeof(uint32_t) - 1), 4)
> + if (RTE_PTR_ALIGN(arr + 2, 4) != &arr[2])
> + FAIL_MACRO(RTE_PTR_ALIGN);
> if (RTE_PTR_DIFF(BIGGER, SMALLER) != PTR_DIFF)
> FAIL_MACRO(RTE_PTR_DIFF);
> if (RTE_MAX(SMALLER, BIGGER) != BIGGER)
> diff --git a/lib/eal/include/rte_common.h
> b/lib/eal/include/rte_common.h
> index a96cc2a138..d517e9f75f 100644
> --- a/lib/eal/include/rte_common.h
> +++ b/lib/eal/include/rte_common.h
> @@ -295,7 +295,7 @@ static void
> __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
> /**
> * subtract a byte-value offset from a pointer
> */
> -#define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
> +#define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
>
> /**
> * get the difference between two pointer values, i.e. how far apart
> @@ -320,7 +320,7 @@ static void
> __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
> * must be a power-of-two value.
> */
> #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
> - ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
> + ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
>
> /**
> * Macro to align a value to a given power-of-two. The resultant value
> --
> 2.33.1
>
Reviewed-by: Morten Brørup <mb@smartsharesystems.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 1/4] eal: fix pointer arithmetic with an expression argument
[not found] ` <20220825153709.33181-1-dmitry.kozliuk@gmail.com>
@ 2022-08-25 15:37 ` Dmitry Kozlyuk
[not found] ` <20220825223355.410540-1-dmitry.kozliuk@gmail.com>
1 sibling, 0 replies; 5+ messages in thread
From: Dmitry Kozlyuk @ 2022-08-25 15:37 UTC (permalink / raw)
To: dev
Cc: Dmitry Kozlyuk, stable, Morten Brørup, Bruce Richardson,
Chengwen Feng
RTE_PTR_SUB(ptr, x) and RTE_PTR_ALIGN_FLOOR() worked incorrectly
if "ptr" was an expression:
uint32_t arr[3];
RTE_PTR_SUB(arr + 1, sizeof(arr[0]));
// expected: (uint32_t *)((uintptr_t)(arr + 1) - 4) == arr
// actual: (uint32_t *)((uintptr_t) arr + 1 - 4) != arr
RTE_PTR_ALIGN_FLOOR(arr + 2, sizeof(arr[0]));
// expected: RTE_ALIGN_FLOOR((uintptr_t)(arr + 2), 4) == &arr[2]
// actual: RTE_ALIGN_FLOOR((uintptr_t) arr + 2, 4) == &arr[0]
Fix the macros and extend the relevant unit test.
Convert uses of a custom test failure macro to RTE_TEST_ASSERT*().
Fixes: af75078fece3 ("first public release")
Cc: stable@dpdk.org
Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
Reviewed-by: Morten Brørup <mb@smartsharesystems.com>
Acked-by: Bruce Richardson <bruce.richardson@intel.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
---
app/test/test_common.c | 58 +++++++++++++++++++++++++-----------
lib/eal/include/rte_common.h | 4 +--
2 files changed, 42 insertions(+), 20 deletions(-)
diff --git a/app/test/test_common.c b/app/test/test_common.c
index ef177cecb1..f89e1eb7ee 100644
--- a/app/test/test_common.c
+++ b/app/test/test_common.c
@@ -25,31 +25,53 @@ test_macros(int __rte_unused unused_parm)
#define SMALLER 0x1000U
#define BIGGER 0x2000U
#define PTR_DIFF BIGGER - SMALLER
-#define FAIL_MACRO(x)\
- {printf(#x "() test failed!\n");\
- return -1;}
uintptr_t unused = 0;
unsigned int smaller = SMALLER, bigger = BIGGER;
+ uint32_t arr[3];
RTE_SET_USED(unused);
RTE_SWAP(smaller, bigger);
- if (smaller != BIGGER && bigger != SMALLER)
- FAIL_MACRO(RTE_SWAP);
- if ((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF) != BIGGER)
- FAIL_MACRO(RTE_PTR_ADD);
- if ((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF) != SMALLER)
- FAIL_MACRO(RTE_PTR_SUB);
- if (RTE_PTR_DIFF(BIGGER, SMALLER) != PTR_DIFF)
- FAIL_MACRO(RTE_PTR_DIFF);
- if (RTE_MAX(SMALLER, BIGGER) != BIGGER)
- FAIL_MACRO(RTE_MAX);
- if (RTE_MIN(SMALLER, BIGGER) != SMALLER)
- FAIL_MACRO(RTE_MIN);
-
- if (strncmp(RTE_STR(test), "test", sizeof("test")))
- FAIL_MACRO(RTE_STR);
+ RTE_TEST_ASSERT(smaller == BIGGER && bigger == SMALLER,
+ "RTE_SWAP");
+ RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF), BIGGER,
+ "RTE_PTR_ADD");
+ RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF), SMALLER,
+ "RTE_PTR_SUB");
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_DIFF(BIGGER, SMALLER), PTR_DIFF,
+ "RTE_PTR_DIFF");
+ RTE_TEST_ASSERT_EQUAL(RTE_MAX(SMALLER, BIGGER), BIGGER,
+ "RTE_MAX");
+ RTE_TEST_ASSERT_EQUAL(RTE_MIN(SMALLER, BIGGER), SMALLER,
+ "RTE_MIN");
+
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_ADD(arr + 1, sizeof(arr[0])), &arr[2],
+ "RTE_PTR_ADD(expr, x)");
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_SUB(arr + 1, sizeof(arr[0])), &arr[0],
+ "RTE_PTR_SUB(expr, x)");
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_ALIGN_FLOOR(arr + 2, 4), &arr[2],
+ "RTE_PTR_ALIGN_FLOOR(expr, x)");
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_ALIGN_CEIL(arr + 2, 4), &arr[2],
+ "RTE_PTR_ALIGN_CEIL(expr, x)");
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_ALIGN(arr + 2, 4), &arr[2],
+ "RTE_PTR_ALIGN(expr, x)");
+
+ RTE_TEST_ASSERT_EQUAL(
+ RTE_PTR_ALIGN_FLOOR(RTE_PTR_ADD(&arr[1], 1), 4), &arr[1],
+ "RTE_PTR_ALIGN_FLOOR(x < y/2, y)");
+ RTE_TEST_ASSERT_EQUAL(
+ RTE_PTR_ALIGN_FLOOR(RTE_PTR_ADD(&arr[1], 3), 4), &arr[1],
+ "RTE_PTR_ALIGN_FLOOR(x > y/2, y)");
+ RTE_TEST_ASSERT_EQUAL(
+ RTE_PTR_ALIGN_CEIL(RTE_PTR_ADD(&arr[1], 3), 4), &arr[2],
+ "RTE_PTR_ALIGN_CEIL(x < y/2, y)");
+ RTE_TEST_ASSERT_EQUAL(
+ RTE_PTR_ALIGN_CEIL(RTE_PTR_ADD(&arr[1], 1), 4), &arr[2],
+ "RTE_PTR_ALIGN_CEIL(x > y/2, y)");
+
+ RTE_TEST_ASSERT(strncmp(RTE_STR(test), "test", sizeof("test")) == 0,
+ "RTE_STR");
return 0;
}
diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h
index a96cc2a138..d517e9f75f 100644
--- a/lib/eal/include/rte_common.h
+++ b/lib/eal/include/rte_common.h
@@ -295,7 +295,7 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
/**
* subtract a byte-value offset from a pointer
*/
-#define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
+#define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
/**
* get the difference between two pointer values, i.e. how far apart
@@ -320,7 +320,7 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
* must be a power-of-two value.
*/
#define RTE_PTR_ALIGN_FLOOR(ptr, align) \
- ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
+ ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
/**
* Macro to align a value to a given power-of-two. The resultant value
--
2.33.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v3 1/4] eal: fix pointer arithmetic with an expression argument
[not found] ` <20220825223355.410540-1-dmitry.kozliuk@gmail.com>
@ 2022-08-25 22:33 ` Dmitry Kozlyuk
[not found] ` <20220827113222.3951505-1-dmitry.kozliuk@gmail.com>
1 sibling, 0 replies; 5+ messages in thread
From: Dmitry Kozlyuk @ 2022-08-25 22:33 UTC (permalink / raw)
To: dev
Cc: Dmitry Kozlyuk, stable, Morten Brørup, Bruce Richardson,
Chengwen Feng
RTE_PTR_SUB(ptr, x) and RTE_PTR_ALIGN_FLOOR() worked incorrectly
if "ptr" was an expression:
uint32_t arr[3];
RTE_PTR_SUB(arr + 1, sizeof(arr[0]));
// expected: (uint32_t *)((uintptr_t)(arr + 1) - 4) == arr
// actual: (uint32_t *)((uintptr_t) arr + 1 - 4) != arr
RTE_PTR_ALIGN_FLOOR(arr + 2, sizeof(arr[0]));
// expected: RTE_ALIGN_FLOOR((uintptr_t)(arr + 2), 4) == &arr[2]
// actual: RTE_ALIGN_FLOOR((uintptr_t) arr + 2, 4) == &arr[0]
Fix the macros and extend the relevant unit test.
Convert uses of a custom test failure macro to RTE_TEST_ASSERT*().
Fixes: af75078fece3 ("first public release")
Cc: stable@dpdk.org
Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
Reviewed-by: Morten Brørup <mb@smartsharesystems.com>
Acked-by: Bruce Richardson <bruce.richardson@intel.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
---
app/test/test_common.c | 58 +++++++++++++++++++++++++-----------
lib/eal/include/rte_common.h | 4 +--
2 files changed, 42 insertions(+), 20 deletions(-)
diff --git a/app/test/test_common.c b/app/test/test_common.c
index ef177cecb1..f89e1eb7ee 100644
--- a/app/test/test_common.c
+++ b/app/test/test_common.c
@@ -25,31 +25,53 @@ test_macros(int __rte_unused unused_parm)
#define SMALLER 0x1000U
#define BIGGER 0x2000U
#define PTR_DIFF BIGGER - SMALLER
-#define FAIL_MACRO(x)\
- {printf(#x "() test failed!\n");\
- return -1;}
uintptr_t unused = 0;
unsigned int smaller = SMALLER, bigger = BIGGER;
+ uint32_t arr[3];
RTE_SET_USED(unused);
RTE_SWAP(smaller, bigger);
- if (smaller != BIGGER && bigger != SMALLER)
- FAIL_MACRO(RTE_SWAP);
- if ((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF) != BIGGER)
- FAIL_MACRO(RTE_PTR_ADD);
- if ((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF) != SMALLER)
- FAIL_MACRO(RTE_PTR_SUB);
- if (RTE_PTR_DIFF(BIGGER, SMALLER) != PTR_DIFF)
- FAIL_MACRO(RTE_PTR_DIFF);
- if (RTE_MAX(SMALLER, BIGGER) != BIGGER)
- FAIL_MACRO(RTE_MAX);
- if (RTE_MIN(SMALLER, BIGGER) != SMALLER)
- FAIL_MACRO(RTE_MIN);
-
- if (strncmp(RTE_STR(test), "test", sizeof("test")))
- FAIL_MACRO(RTE_STR);
+ RTE_TEST_ASSERT(smaller == BIGGER && bigger == SMALLER,
+ "RTE_SWAP");
+ RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF), BIGGER,
+ "RTE_PTR_ADD");
+ RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF), SMALLER,
+ "RTE_PTR_SUB");
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_DIFF(BIGGER, SMALLER), PTR_DIFF,
+ "RTE_PTR_DIFF");
+ RTE_TEST_ASSERT_EQUAL(RTE_MAX(SMALLER, BIGGER), BIGGER,
+ "RTE_MAX");
+ RTE_TEST_ASSERT_EQUAL(RTE_MIN(SMALLER, BIGGER), SMALLER,
+ "RTE_MIN");
+
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_ADD(arr + 1, sizeof(arr[0])), &arr[2],
+ "RTE_PTR_ADD(expr, x)");
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_SUB(arr + 1, sizeof(arr[0])), &arr[0],
+ "RTE_PTR_SUB(expr, x)");
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_ALIGN_FLOOR(arr + 2, 4), &arr[2],
+ "RTE_PTR_ALIGN_FLOOR(expr, x)");
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_ALIGN_CEIL(arr + 2, 4), &arr[2],
+ "RTE_PTR_ALIGN_CEIL(expr, x)");
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_ALIGN(arr + 2, 4), &arr[2],
+ "RTE_PTR_ALIGN(expr, x)");
+
+ RTE_TEST_ASSERT_EQUAL(
+ RTE_PTR_ALIGN_FLOOR(RTE_PTR_ADD(&arr[1], 1), 4), &arr[1],
+ "RTE_PTR_ALIGN_FLOOR(x < y/2, y)");
+ RTE_TEST_ASSERT_EQUAL(
+ RTE_PTR_ALIGN_FLOOR(RTE_PTR_ADD(&arr[1], 3), 4), &arr[1],
+ "RTE_PTR_ALIGN_FLOOR(x > y/2, y)");
+ RTE_TEST_ASSERT_EQUAL(
+ RTE_PTR_ALIGN_CEIL(RTE_PTR_ADD(&arr[1], 3), 4), &arr[2],
+ "RTE_PTR_ALIGN_CEIL(x < y/2, y)");
+ RTE_TEST_ASSERT_EQUAL(
+ RTE_PTR_ALIGN_CEIL(RTE_PTR_ADD(&arr[1], 1), 4), &arr[2],
+ "RTE_PTR_ALIGN_CEIL(x > y/2, y)");
+
+ RTE_TEST_ASSERT(strncmp(RTE_STR(test), "test", sizeof("test")) == 0,
+ "RTE_STR");
return 0;
}
diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h
index a96cc2a138..d517e9f75f 100644
--- a/lib/eal/include/rte_common.h
+++ b/lib/eal/include/rte_common.h
@@ -295,7 +295,7 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
/**
* subtract a byte-value offset from a pointer
*/
-#define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
+#define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
/**
* get the difference between two pointer values, i.e. how far apart
@@ -320,7 +320,7 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
* must be a power-of-two value.
*/
#define RTE_PTR_ALIGN_FLOOR(ptr, align) \
- ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
+ ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
/**
* Macro to align a value to a given power-of-two. The resultant value
--
2.33.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v4 1/4] eal: fix pointer arithmetic with an expression argument
[not found] ` <20220827113222.3951505-1-dmitry.kozliuk@gmail.com>
@ 2022-08-27 11:32 ` Dmitry Kozlyuk
0 siblings, 0 replies; 5+ messages in thread
From: Dmitry Kozlyuk @ 2022-08-27 11:32 UTC (permalink / raw)
To: dev
Cc: Dmitry Kozlyuk, stable, Morten Brørup, Bruce Richardson,
Chengwen Feng
RTE_PTR_SUB(ptr, x) and RTE_PTR_ALIGN_FLOOR() worked incorrectly
if "ptr" was an expression:
uint32_t arr[3];
RTE_PTR_SUB(arr + 1, sizeof(arr[0]));
// expected: (uint32_t *)((uintptr_t)(arr + 1) - 4) == arr
// actual: (uint32_t *)((uintptr_t) arr + 1 - 4) != arr
RTE_PTR_ALIGN_FLOOR(arr + 2, sizeof(arr[0]));
// expected: RTE_ALIGN_FLOOR((uintptr_t)(arr + 2), 4) == &arr[2]
// actual: RTE_ALIGN_FLOOR((uintptr_t) arr + 2, 4) == &arr[0]
Fix the macros and extend the relevant unit test.
Convert uses of a custom test failure macro to RTE_TEST_ASSERT*().
Fixes: af75078fece3 ("first public release")
Cc: stable@dpdk.org
Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
Reviewed-by: Morten Brørup <mb@smartsharesystems.com>
Acked-by: Bruce Richardson <bruce.richardson@intel.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
---
app/test/test_common.c | 58 +++++++++++++++++++++++++-----------
lib/eal/include/rte_common.h | 4 +--
2 files changed, 42 insertions(+), 20 deletions(-)
diff --git a/app/test/test_common.c b/app/test/test_common.c
index ef177cecb1..f89e1eb7ee 100644
--- a/app/test/test_common.c
+++ b/app/test/test_common.c
@@ -25,31 +25,53 @@ test_macros(int __rte_unused unused_parm)
#define SMALLER 0x1000U
#define BIGGER 0x2000U
#define PTR_DIFF BIGGER - SMALLER
-#define FAIL_MACRO(x)\
- {printf(#x "() test failed!\n");\
- return -1;}
uintptr_t unused = 0;
unsigned int smaller = SMALLER, bigger = BIGGER;
+ uint32_t arr[3];
RTE_SET_USED(unused);
RTE_SWAP(smaller, bigger);
- if (smaller != BIGGER && bigger != SMALLER)
- FAIL_MACRO(RTE_SWAP);
- if ((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF) != BIGGER)
- FAIL_MACRO(RTE_PTR_ADD);
- if ((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF) != SMALLER)
- FAIL_MACRO(RTE_PTR_SUB);
- if (RTE_PTR_DIFF(BIGGER, SMALLER) != PTR_DIFF)
- FAIL_MACRO(RTE_PTR_DIFF);
- if (RTE_MAX(SMALLER, BIGGER) != BIGGER)
- FAIL_MACRO(RTE_MAX);
- if (RTE_MIN(SMALLER, BIGGER) != SMALLER)
- FAIL_MACRO(RTE_MIN);
-
- if (strncmp(RTE_STR(test), "test", sizeof("test")))
- FAIL_MACRO(RTE_STR);
+ RTE_TEST_ASSERT(smaller == BIGGER && bigger == SMALLER,
+ "RTE_SWAP");
+ RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF), BIGGER,
+ "RTE_PTR_ADD");
+ RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF), SMALLER,
+ "RTE_PTR_SUB");
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_DIFF(BIGGER, SMALLER), PTR_DIFF,
+ "RTE_PTR_DIFF");
+ RTE_TEST_ASSERT_EQUAL(RTE_MAX(SMALLER, BIGGER), BIGGER,
+ "RTE_MAX");
+ RTE_TEST_ASSERT_EQUAL(RTE_MIN(SMALLER, BIGGER), SMALLER,
+ "RTE_MIN");
+
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_ADD(arr + 1, sizeof(arr[0])), &arr[2],
+ "RTE_PTR_ADD(expr, x)");
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_SUB(arr + 1, sizeof(arr[0])), &arr[0],
+ "RTE_PTR_SUB(expr, x)");
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_ALIGN_FLOOR(arr + 2, 4), &arr[2],
+ "RTE_PTR_ALIGN_FLOOR(expr, x)");
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_ALIGN_CEIL(arr + 2, 4), &arr[2],
+ "RTE_PTR_ALIGN_CEIL(expr, x)");
+ RTE_TEST_ASSERT_EQUAL(RTE_PTR_ALIGN(arr + 2, 4), &arr[2],
+ "RTE_PTR_ALIGN(expr, x)");
+
+ RTE_TEST_ASSERT_EQUAL(
+ RTE_PTR_ALIGN_FLOOR(RTE_PTR_ADD(&arr[1], 1), 4), &arr[1],
+ "RTE_PTR_ALIGN_FLOOR(x < y/2, y)");
+ RTE_TEST_ASSERT_EQUAL(
+ RTE_PTR_ALIGN_FLOOR(RTE_PTR_ADD(&arr[1], 3), 4), &arr[1],
+ "RTE_PTR_ALIGN_FLOOR(x > y/2, y)");
+ RTE_TEST_ASSERT_EQUAL(
+ RTE_PTR_ALIGN_CEIL(RTE_PTR_ADD(&arr[1], 3), 4), &arr[2],
+ "RTE_PTR_ALIGN_CEIL(x < y/2, y)");
+ RTE_TEST_ASSERT_EQUAL(
+ RTE_PTR_ALIGN_CEIL(RTE_PTR_ADD(&arr[1], 1), 4), &arr[2],
+ "RTE_PTR_ALIGN_CEIL(x > y/2, y)");
+
+ RTE_TEST_ASSERT(strncmp(RTE_STR(test), "test", sizeof("test")) == 0,
+ "RTE_STR");
return 0;
}
diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h
index a96cc2a138..d517e9f75f 100644
--- a/lib/eal/include/rte_common.h
+++ b/lib/eal/include/rte_common.h
@@ -295,7 +295,7 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
/**
* subtract a byte-value offset from a pointer
*/
-#define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
+#define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
/**
* get the difference between two pointer values, i.e. how far apart
@@ -320,7 +320,7 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
* must be a power-of-two value.
*/
#define RTE_PTR_ALIGN_FLOOR(ptr, align) \
- ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
+ ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
/**
* Macro to align a value to a given power-of-two. The resultant value
--
2.33.1
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2022-08-27 11:32 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <20220821205009.1317044-1-dmitry.kozliuk@gmail.com>
2022-08-21 20:50 ` [PATCH 1/3] eal: fix pointer arithmetic with an expression argument Dmitry Kozlyuk
2022-08-22 7:14 ` Morten Brørup
[not found] ` <20220825153709.33181-1-dmitry.kozliuk@gmail.com>
2022-08-25 15:37 ` [PATCH v2 1/4] " Dmitry Kozlyuk
[not found] ` <20220825223355.410540-1-dmitry.kozliuk@gmail.com>
2022-08-25 22:33 ` [PATCH v3 " Dmitry Kozlyuk
[not found] ` <20220827113222.3951505-1-dmitry.kozliuk@gmail.com>
2022-08-27 11:32 ` [PATCH v4 " Dmitry Kozlyuk
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).