Received: from mail-lj1-f175.google.com (mail-lj1-f175.google.com [209.85.208.175]) by nld3-dev1.alpinelinux.org (Postfix) with ESMTPS id 7799778105A for <~alpine/apk-tools@lists.alpinelinux.org>; Mon, 17 Feb 2020 10:33:38 +0000 (UTC) Received: by mail-lj1-f175.google.com with SMTP id r19so18236561ljg.3 for <~alpine/apk-tools@lists.alpinelinux.org>; Mon, 17 Feb 2020 02:33:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DbX7mgVdO61oig25vXyyq19aCrEfNGkGe9HGi9XSZ0I=; b=AMyuIZr7ovLzpuI7B9AVpSLIBWYHYy7bLXvy9MSgrwZtLKwLD+FDI6inUx7BBwkltW 0OBPtLW2rHWl4nkK6n2q6z1ox8PLOucXlSVNQaS5+rRPbCx84rlmW9P9jnC04BPFmxdy k8k22/SIXT2C39EB9FccVvzyccwsfjwuVgVwqMwZ0lU4siMuxHRCOIwuH6kg03kC4XCf xH4M9vm6DosEj4eD6JC+8fvY9CA5j9AvZliZHDyGaUyhmRvkCpb5UZjOufQkOb/L9NUx wIJb/ncRxTmTaXFx1Ijcl1PFjKRl92ttME0ND02LtTGWbvwXWEb4UXDj59KxD6AWzNcY q0Fg== X-Gm-Message-State: APjAAAVsU432gAcHXsYGowNKCXe1jRFfopY48QPFd4t9x5YdQzd2PfW3 iX2vAgmWhOqyNO8r7MS64zA= X-Google-Smtp-Source: APXvYqzuAvR4FZUqAfXvJpfaLGRNCzfKUCihBFDgc/shZhnM04AvpFoxfKUb2YpbU0VFKt9/nnH1LA== X-Received: by 2002:a05:651c:299:: with SMTP id b25mr630846ljo.1.1581935617752; Mon, 17 Feb 2020 02:33:37 -0800 (PST) Received: from vostro.wlan (dtc5qkyyyyyyyyyyyyx9y-3.rev.dnainternet.fi. [2001:14ba:80b2:d400::4fa]) by smtp.gmail.com with ESMTPSA id b17sm137755ljd.5.2020.02.17.02.33.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Feb 2020 02:33:37 -0800 (PST) Date: Mon, 17 Feb 2020 12:33:34 +0200 From: Timo Teras To: Carl Chave Cc: ~alpine/apk-tools@lists.alpinelinux.org Subject: Re: APK Package Name Issue on armel port Message-ID: <20200217123334.031047d1@vostro.wlan> In-Reply-To: <20200217111200.08405b9c@vostro.wlan> References: <20200214105215.6873e402@vostro.wlan> <62f21c96-ff43-83e4-5b7f-2e65e6d72729@adelielinux.org> <20200217111200.08405b9c@vostro.wlan> X-Mailer: Claws Mail 3.17.4 (GTK+ 2.24.32; x86_64-alpine-linux-musl) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Mon, 17 Feb 2020 11:12:00 +0200 Timo Teras wrote: > Reviewing the code in apk, it seems that the murmur hashing code is > not properly accounting for alignment. So the symptoms do match. The > hash lookup does not work (well, works randomly based on few things), > but when enumerating all packages (by using the wildcard in lookups) > it'll show everything. Seems this is a common complaint about original murmur3 and there's been other projects affected with this too. Can you try the below patch if it fixes things? Cheers, Timo [PATCH] updates to murmur3 hash - do not do unaligned accesses on non-x86 hardware - clean up the code a little bit --- src/blob.c | 68 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/src/blob.c b/src/blob.c index 68a596e..38e370c 100644 --- a/src/blob.c +++ b/src/blob.c @@ -199,49 +199,59 @@ apk_blob_t apk_blob_pushed(apk_blob_t buffer, apk_blob_t left) return APK_BLOB_PTR_LEN(buffer.ptr, left.ptr - buffer.ptr); } -static uint32_t murmur3_32(const char *key, uint32_t len, uint32_t seed) +static inline uint32_t rotl32(uint32_t x, int8_t r) +{ + return (x << r) | (x >> (32 - r)); +} + +static inline uint32_t get_unaligned32(const void *ptr) +{ +#if defined(__x86_64__) || defined(__i386__) + return *(const uint32_t *)ptr; +#else + const uint8_t *p = ptr; + return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24; +#endif +} + +static uint32_t murmur3_32(const void *pkey, uint32_t len, uint32_t seed) { static const uint32_t c1 = 0xcc9e2d51; static const uint32_t c2 = 0x1b873593; - static const uint32_t r1 = 15; - static const uint32_t r2 = 13; - static const uint32_t m = 5; - static const uint32_t n = 0xe6546b64; - uint32_t hash = seed; + const uint8_t *key = pkey; const int nblocks = len / 4; - const uint32_t *blocks = (const uint32_t *) key; + uint32_t k, h = seed; int i; - for (i = 0; i < nblocks; i++) { - uint32_t k = blocks[i]; + + for (i = 0; i < nblocks; i++, key += 4) { + k = get_unaligned32(key); k *= c1; - k = (k << r1) | (k >> (32 - r1)); + k = rotl32(k, 15); k *= c2; - hash ^= k; - hash = ((hash << r2) | (hash >> (32 - r2))) * m + n; + h ^= k; + h = rotl32(h, 13) * 5 + 0xe6546b64; } - const uint8_t *tail = (const uint8_t *) (key + nblocks * 4); - uint32_t k1 = 0; - + k = 0; switch (len & 3) { case 3: - k1 ^= tail[2] << 16; + k ^= key[2] << 16; case 2: - k1 ^= tail[1] << 8; + k ^= key[1] << 8; case 1: - k1 ^= tail[0]; - k1 *= c1; - k1 = (k1 << r1) | (k1 >> (32 - r1)); - k1 *= c2; - hash ^= k1; + k ^= key[0]; + k *= c1; + k = rotl32(k, 15); + k *= c2; + h ^= k; } - hash ^= len; - hash ^= (hash >> 16); - hash *= 0x85ebca6b; - hash ^= (hash >> 13); - hash *= 0xc2b2ae35; - hash ^= (hash >> 16); - return hash; + h ^= len; + h ^= (h >> 16); + h *= 0x85ebca6b; + h ^= (h >> 13); + h *= 0xc2b2ae35; + h ^= (h >> 16); + return h; } unsigned long apk_blob_hash_seed(apk_blob_t blob, unsigned long seed) -- 2.25.0