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 92B12A034F; Sat, 15 Jan 2022 20:39:14 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id CDE61410FC; Sat, 15 Jan 2022 20:39:13 +0100 (CET) Received: from mail-qt1-f180.google.com (mail-qt1-f180.google.com [209.85.160.180]) by mails.dpdk.org (Postfix) with ESMTP id 6466E4013F; Sat, 15 Jan 2022 20:39:12 +0100 (CET) Received: by mail-qt1-f180.google.com with SMTP id y10so14438165qtw.1; Sat, 15 Jan 2022 11:39:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=7GKL1J0k79Xnap3c8KeJl070XvbaMglfpbX+JLJwEkQ=; b=HMa/FRHS9ga/42laa/pch+Rpka4VoeCtj2xTC/aKn+3eX8ZGVnt1/oLFqfr7+gnNNU +GS5kHkwMfqH1iRha9+bqfNRPg29H2bPq4pMPlfufCEGCwpdSS3KEBnKojSB1QHSG9yc nEo5IUH+2ozy5wwsnh1Olq8GcYklobxK9Eo2/otZm4QIPfF0zRtDcOVz0RjtO8J6GUkf Bpzy7t8JU6PO1o2eYTmKdZmZypK1fa+86qtBhvKSvKtu27z33p5xc6aPobNzmUdhg8oC m9cYlWY5MwDXSTqwiRJsfWDavtj2V6oyWg6vK2OZ9kZ4xxXs96fsu6jZbX89+7zNqdxf RW/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=7GKL1J0k79Xnap3c8KeJl070XvbaMglfpbX+JLJwEkQ=; b=J6hlTAS9gYe0UVuQ2FgtXd9/jNH+yPBZH3PPFtKRxE1+9urRUcc5zbYVQU9bRxDUXV LVa+LAAxbAXu05bB3t8eOZJn5g40UWdk58hFxfw8FR0D66QCe+COr9z4VbU7impzPBky Et/NYNfL42vEXpC8kg1ehRbCLvznY/toMxcvruArJflCo83j4OtE/9wBnashBZ/jsaoC NWNknhBMOmYfiR3iGGAZOKYQz1EsaOuOLhKaCGpvi5RrTmgJBrv5GKYyyySqWHUHOA/E tAJlko4ebcYlU2Dv5JA7hxoOcwX1kFxXZq4FlRsfzQy3vlUkkkHU/cd3xPJIyCy3d5Ln 8nTQ== X-Gm-Message-State: AOAM533Nyr6DEs8Aa8ox2nQCYzaBL6oaHnlnS0PPb8R3FMqHwDQ3CUjy xppFJdWYSdX9hksbMjaetck= X-Google-Smtp-Source: ABdhPJzhzgqdVv4cYys4gE5Ry+xElGJxQMiylFmzEY23yogSWnHLcvOXAvxT8wOlRNCnUowlAtHDgw== X-Received: by 2002:a05:622a:164b:: with SMTP id y11mr12273715qtj.621.1642275551747; Sat, 15 Jan 2022 11:39:11 -0800 (PST) Received: from localhost.localdomain (bras-base-hullpq2034w-grc-18-74-15-213-135.dsl.bell.ca. [74.15.213.135]) by smtp.gmail.com with ESMTPSA id o10sm6445885qtx.33.2022.01.15.11.39.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Jan 2022 11:39:11 -0800 (PST) From: Luc Pelletier To: bruce.richardson@intel.com, konstantin.ananyev@intel.com Cc: dev@dpdk.org, Luc Pelletier , Xiaoyun Li , stable@dpdk.org Subject: [PATCH] eal: fix unaligned loads/stores in rte_memcpy_aligned Date: Sat, 15 Jan 2022 14:38:36 -0500 Message-Id: <20220115193836.442209-1-lucp.at.work@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Calls to rte_memcpy_aligned could result in unaligned loads/stores for 1 < n < 16. This is undefined behavior according to the C standard, and it gets flagged by the clang undefined behavior sanitizer. rte_memcpy_aligned is called with aligned src and dst addresses. When n is odd, the code would copy a single byte first, increment src/dst, then, depending on the value of n, would cast src/dst to a qword, dword or word pointer. This results in an unaligned load/store. Reversing the order of the casts & copies (ie. copying a qword first, dword second, etc.) fixes the issue. Fixes: d35cc1fe6a7a ("eal/x86: revert select optimized memcpy at run-time") Cc: Xiaoyun Li Cc: stable@dpdk.org Signed-off-by: Luc Pelletier --- lib/eal/x86/include/rte_memcpy.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/eal/x86/include/rte_memcpy.h b/lib/eal/x86/include/rte_memcpy.h index 1b6c6e585f..a4eb1316b6 100644 --- a/lib/eal/x86/include/rte_memcpy.h +++ b/lib/eal/x86/include/rte_memcpy.h @@ -818,25 +818,25 @@ rte_memcpy_aligned(void *dst, const void *src, size_t n) { void *ret = dst; - /* Copy size <= 16 bytes */ + /* Copy size < 16 bytes */ if (n < 16) { - if (n & 0x01) { - *(uint8_t *)dst = *(const uint8_t *)src; - src = (const uint8_t *)src + 1; - dst = (uint8_t *)dst + 1; - } - if (n & 0x02) { - *(uint16_t *)dst = *(const uint16_t *)src; - src = (const uint16_t *)src + 1; - dst = (uint16_t *)dst + 1; + if (n & 0x08) { + *(uint64_t *)dst = *(const uint64_t *)src; + src = (const uint64_t *)src + 1; + dst = (uint64_t *)dst + 1; } if (n & 0x04) { *(uint32_t *)dst = *(const uint32_t *)src; src = (const uint32_t *)src + 1; dst = (uint32_t *)dst + 1; } - if (n & 0x08) - *(uint64_t *)dst = *(const uint64_t *)src; + if (n & 0x02) { + *(uint16_t *)dst = *(const uint16_t *)src; + src = (const uint16_t *)src + 1; + dst = (uint16_t *)dst + 1; + } + if (n & 0x01) + *(uint8_t *)dst = *(const uint8_t *)src; return ret; } -- 2.25.1