From: Christian Ehrhardt <christian.ehrhardt@canonical.com>
To: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
Cc: stable@dpdk.org, "Morten Brørup" <mb@smartsharesystems.com>,
"Bruce Richardson" <bruce.richardson@intel.com>,
"Chengwen Feng" <fengchengwen@huawei.com>
Subject: Re: [PATCH 19.11] eal: fix side effect in some pointer arithmetic macros
Date: Tue, 15 Nov 2022 09:18:52 +0100 [thread overview]
Message-ID: <CAATJJ0LGiPdfMeaMcb2ZFcxSR9n3R-HTREe6pOtRUvCwQAFubg@mail.gmail.com> (raw)
In-Reply-To: <20221111183042.2589236-1-dmitry.kozliuk@gmail.com>
On Fri, Nov 11, 2022 at 7:31 PM Dmitry Kozlyuk <dmitry.kozliuk@gmail.com> wrote:
>
> [ upstream commit 1a7374c95678183aef6f40b64074752831a33d26 ]
Thanks, applied to the WIP branch - expect it to be part of 19.11.14
unless some builds stumble over it.
> 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 | 54 +++++++++++++++-------
> lib/librte_eal/common/include/rte_common.h | 4 +-
> 2 files changed, 40 insertions(+), 18 deletions(-)
>
> diff --git a/app/test/test_common.c b/app/test/test_common.c
> index 12bd1cad90..181c8be3be 100644
> --- a/app/test/test_common.c
> +++ b/app/test/test_common.c
> @@ -25,27 +25,49 @@ 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;
> + uint32_t arr[3];
>
> RTE_SET_USED(unused);
>
> - 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_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/librte_eal/common/include/rte_common.h b/lib/librte_eal/common/include/rte_common.h
> index fc938eadcd..abe6bc9d15 100644
> --- a/lib/librte_eal/common/include/rte_common.h
> +++ b/lib/librte_eal/common/include/rte_common.h
> @@ -172,7 +172,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
> @@ -197,7 +197,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
>
--
Christian Ehrhardt
Senior Staff Engineer, Ubuntu Server
Canonical Ltd
prev parent reply other threads:[~2022-11-15 8:19 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-11 18:30 Dmitry Kozlyuk
2022-11-15 8:18 ` Christian Ehrhardt [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAATJJ0LGiPdfMeaMcb2ZFcxSR9n3R-HTREe6pOtRUvCwQAFubg@mail.gmail.com \
--to=christian.ehrhardt@canonical.com \
--cc=bruce.richardson@intel.com \
--cc=dmitry.kozliuk@gmail.com \
--cc=fengchengwen@huawei.com \
--cc=mb@smartsharesystems.com \
--cc=stable@dpdk.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).