[PATCH v2] hash: fix pointer alignment
Radu Nicolau
radu.nicolau at intel.com
Fri Feb 27 14:00:50 CET 2026
On 27-Feb-26 12:37 PM, Marat Khalili wrote:
>> -----Original Message-----
>> From: Radu Nicolau <radu.nicolau at intel.com>
>> Sent: Friday 27 February 2026 09:48
>> To: dev at dpdk.org
>> Cc: Marat Khalili <marat.khalili at huawei.com>; Radu Nicolau <radu.nicolau at intel.com>; stable at dpdk.org;
>> stephen at networkplumber.org; Yipeng Wang <yipeng1.wang at intel.com>; Sameh Gobriel
>> <sameh.gobriel at intel.com>; Bruce Richardson <bruce.richardson at intel.com>; Vladimir Medvedkin
>> <vladimir.medvedkin at intel.com>; Pablo de Lara <pablo.de.lara.guarch at intel.com>; Yerden Zhumabekov
>> <e_zhumabekov at sts.kz>
>> Subject: [PATCH v2] hash: fix pointer alignment
>>
>> rte_hash_crc assumes input pointer address is 8 byte aligned
>> which may not be always the case.
>> This fix aligns the input pointer before proceeding to process it
>> in 8 byte chunks.
>>
>> Fixes: 504a29af13a7 ("hash: fix strict-aliasing for CRC")
>> Cc: stable at dpdk.org
>> Cc: stephen at networkplumber.org
>>
>> Signed-off-by: Radu Nicolau <radu.nicolau at intel.com>
>> ---
>> v2: reverse the order of alignment adjustment calls
>>
>> lib/hash/rte_hash_crc.h | 18 ++++++++++++++++++
>> 1 file changed, 18 insertions(+)
>>
>> diff --git a/lib/hash/rte_hash_crc.h b/lib/hash/rte_hash_crc.h
>> index fa07c97685..d61420868a 100644
>> --- a/lib/hash/rte_hash_crc.h
>> +++ b/lib/hash/rte_hash_crc.h
>> @@ -127,6 +127,24 @@ rte_hash_crc(const void *data, uint32_t data_len, uint32_t init_val)
>> unsigned i;
>> uintptr_t pd = (uintptr_t) data;
>>
>> + /* align input to 8 byte boundary if needed */
>> + if ((pd & 0x7) && data_len >= 8) {
>> + uintptr_t unaligned_bytes = 8 - (pd & 0x7);
>> + data_len -= unaligned_bytes;
>> + if (unaligned_bytes & 0x1) {
>> + init_val = rte_hash_crc_1byte(*(const uint8_t *)pd, init_val);
>> + pd += 1;
>> + }
>> + if (unaligned_bytes & 0x2) {
>> + init_val = rte_hash_crc_2byte(*(const uint16_t *)pd, init_val);
>> + pd += 2;
>> + }
>> + if (unaligned_bytes & 0x4) {
>> + init_val = rte_hash_crc_4byte(*(const uint32_t *)pd, init_val);
>> + pd += 4;
>> + }
>> + }
>> +
>> for (i = 0; i < data_len / 8; i++) {
>> init_val = rte_hash_crc_8byte(*(const uint64_t *)pd, init_val);
>> pd += 8;
>> --
>> 2.52.0
>>
> Surprisingly, we do not seem to have any tests calling rte_hash_crc with misaligned data. I tried to tweak existing ones in test_hash.c and test_hash_functions.c, and found out that rte_hash_crc still fails (with ubsan) for sizes less than 8 in one of the last 3 if's, and also that jhash has the same problems. Still this commit is a step in the right direction IMO, so:
>
> Acked-by: Marat Khalili <marat.khalili at huawei.com>
Thanks for the ack!
The issue still persist with less that 8 bytes unaligned input because
the 3 ifs after the loop assume the input is aligned, which is the case
only if the loop was taken. I will fix the patch to address this case in
the added section - you were right with the first comment.
More information about the stable
mailing list