From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 13C18A0547 for ; Fri, 11 Nov 2022 19:31:05 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 308B74014F; Fri, 11 Nov 2022 19:31:04 +0100 (CET) Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com [209.85.167.42]) by mails.dpdk.org (Postfix) with ESMTP id 41C0A40141 for ; Fri, 11 Nov 2022 19:31:03 +0100 (CET) Received: by mail-lf1-f42.google.com with SMTP id bp15so9451060lfb.13 for ; Fri, 11 Nov 2022 10:31:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ZuR9rlM4ox5JGUz3P9+gg7wud1zXSUZz3TN4fY7hlp4=; b=J+QASrUdpNfw0fcBudzq+b8Q3N5G9WhwmlSIykNNOLHEmCUdS35hVFrd5F5rQCPDIH z2HO8Z5lePeYWVEo3xtUKyM+Mk9784cvdVjlaqsQPdvEJzh+4Pq6iU/Dsb7v8sGMXxUt zW+SbWtW//POOWaV8RaHU0WzC7jiklnHNB98UC1mpDGbhbkjBjcGjKNH9TyXFrHxt2t4 atQNryWD9GjHNM+hnZzaMoSrDK2HmMwbJjrRCY0jPKbx601AjjCk7i+quOM+W7Y+gcz1 q1ZrTDLGnJD6y3xCofJ6RcRYijT2GR405swdCl6waZVnbE9XHVDzNnhzF6TVAy2tIxbb Nfwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZuR9rlM4ox5JGUz3P9+gg7wud1zXSUZz3TN4fY7hlp4=; b=K7Whmn7etRY8CMstU1r3x2on42f2i7dqGVJHd63wtq/b5kv39vhEmg15cGOrTqTlBq 88HyP3Qed4fioT4mz5AX+RhkfE+cyedkEo5N51fa4cSisXlsoJ4HyxSzkHYOq748B4Ct kRNX+tyHHbwioqguGkDaz/LZS9YQ9Z5lr++XYXxxkNIsXpeT6+pNWpqjDM0m66gS+qH1 V9vpo/gxqAq4+9OB7gaECu9/O+s4NMEwa+DKysR55+37Xiey5Y9UkU0AcZEw7AMmlPco YF8ZBVl/jbaWPJkqUW/qBontPJ8FkkNC8uY7ymUpZ89u2tfH4guX/3RcFgFsDnxT3KQ8 ePYw== X-Gm-Message-State: ANoB5plZ7/HrZQPzjaN3ujzp3vV4xW4t5GfsOZ8omXXtaBp9N+/wXusZ Uds3VX6FpULOVIO2YQhS9P84WACE/lA= X-Google-Smtp-Source: AA0mqf5XYqqg6vZp36v0l9gge7iP75JT9HIX8UnzZyfg8dIl4S6Pr6oDieL/CVJSmoJA9QB4bJObZQ== X-Received: by 2002:a05:6512:3e14:b0:4aa:7eed:f70c with SMTP id i20-20020a0565123e1400b004aa7eedf70cmr1283180lfv.630.1668191462333; Fri, 11 Nov 2022 10:31:02 -0800 (PST) Received: from sovereign.. (broadband-37-110-65-23.ip.moscow.rt.ru. [37.110.65.23]) by smtp.gmail.com with ESMTPSA id k9-20020ac257c9000000b004b1793520a0sm454670lfo.36.2022.11.11.10.31.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Nov 2022 10:31:01 -0800 (PST) From: Dmitry Kozlyuk To: stable@dpdk.org, christian.ehrhardt@canonical.com Cc: Dmitry Kozlyuk , =?UTF-8?q?Morten=20Br=C3=B8rup?= , Bruce Richardson , Chengwen Feng Subject: [PATCH 19.11] eal: fix side effect in some pointer arithmetic macros Date: Fri, 11 Nov 2022 21:30:42 +0300 Message-Id: <20221111183042.2589236-1-dmitry.kozliuk@gmail.com> X-Mailer: git-send-email 2.33.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: stable@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: patches for DPDK stable branches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: stable-bounces@dpdk.org [ upstream commit 1a7374c95678183aef6f40b64074752831a33d26 ] 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 Reviewed-by: Morten Brørup Acked-by: Bruce Richardson Acked-by: Chengwen Feng --- 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