Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp1409072ybj; Fri, 8 May 2020 00:14:06 -0700 (PDT) X-Google-Smtp-Source: APiQypK0JzL1JG65qcsj6G5o9ByD5oXp1Aq+RLhi4CDoF7XJ94IAUVj+xmBsm7q8tTak+uLqbnpq X-Received: by 2002:a17:906:3004:: with SMTP id 4mr726059ejz.381.1588922046364; Fri, 08 May 2020 00:14:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588922046; cv=none; d=google.com; s=arc-20160816; b=DJ/Xpy/CvO1NStU6A+9opyRl+mnpa0z3COgRchpU50PueI+XK1/2bCbJikT8jkqgGM s2lXO3kwd0cW8cxaVQ4u5Kn3/irYw2qs0gdzNy7VqgvCD/WP/lmOetZPGVPjeKKlhI5A avTvqxfgSBWcxpjWoR4DeUX7wgJqfAzFxydMTxy3rG3xF6WZy5htdLHAGCUgJ1RwRx/f kmPlaYQNl1nNjCmfquZhiXBnHfQ4H9nS68rUKCLjLZMr50G1QwMueT+OdcOK0UNx1E/o NFdSMK5QOfu6+VXe/YuCQddmeo2CdN/JQneN32paUq1GjNPpDE24HbIhXgJnJl54l25J L1Wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=WT8dz7lOh/SMir2tdDotI3JRi9SKgaJFh8CCLOK2p14=; b=xJYspJt+yKqRw2hSnTozA7FYGLGa2F5Gd3+4jRBDnuzUreiMamOMBWpL+/FG0lDanz EpR8k3Z1O2h5B7a5yJAJRC+6UAp7r52xoQ/6xJjTjHjUmTo0WPgzgz6QAYsBLHxwh7Ui qcvmXnZYvkFfOR3SPTWvCdDtkiuJQH6VLNYcehD/yH5R8k/c0jcdSPBrazCbWr98+Wxz 3Rf/OPazr/g/ZgyYppbdSLEjCX/FbXdxCuLSXIBcJ34joiP0Cv1TXaiwmEJcoLsADzwx 6rFVWYngQEN+9uJDXQIdUYr8sqNLbT8lb/2rJ28fe8YjJq7dW4gBV/tbm3ZBNun5W3mO PgkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=WcNavMyx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d9si505511ejb.387.2020.05.08.00.13.41; Fri, 08 May 2020 00:14:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=WcNavMyx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726877AbgEHHMK (ORCPT + 99 others); Fri, 8 May 2020 03:12:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726009AbgEHHMK (ORCPT ); Fri, 8 May 2020 03:12:10 -0400 Received: from mail-qt1-x842.google.com (mail-qt1-x842.google.com [IPv6:2607:f8b0:4864:20::842]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8483C05BD43; Fri, 8 May 2020 00:12:09 -0700 (PDT) Received: by mail-qt1-x842.google.com with SMTP id g16so489188qtp.11; Fri, 08 May 2020 00:12:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=WT8dz7lOh/SMir2tdDotI3JRi9SKgaJFh8CCLOK2p14=; b=WcNavMyxO3Mk9BoeN2PBmT3sie1iK9pvecFYWrBWDGJsOnmAIIVREK8+HLk/y+6/Mz 9QyqwRruohhAlsmXSwILe7LpKvI6tZaeU8KRfEutWhqw1l1AA3WSCAmRj2aWAMZIc7BO eyVwB2FkCpRp3m85Oh0JeKVX89XeR4u8CxKcBkHAQghk5D7SjMpAVxiDFM0h03snhSfb P6KLRsbO+ludyu3zxYS0t0LAqfO4urxX9MswrxMJyBe887fQik40irwbBzYNgcCD7EPN i1nB06F5oN/2ULXBKAGTZq2bY8kP/tI3lZT5wGVO5Ci3QYYL71rmWLquK1GDJEelL/9i xEMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=WT8dz7lOh/SMir2tdDotI3JRi9SKgaJFh8CCLOK2p14=; b=UxZy9DA6kp0HWIFrvZWTV30ICEQ0UWyNYbbp0UBvL7s1S5K6U0xzChhE20bn3Wt6sg pDRfET9gzVVjel5NfRH7PQkmXgwjt/uyPpMlrTkEkHGOS10lrTOj4vtAnCcYW1P2SLK3 5Y7YtrBut9Gs7lQrNOajfR1d2MEdCep14bcmaM+VBJ7CZBBwktwjTZcSNgOsm8u8oVMf bqmhfmisswhB3hXhiY6RGEcS+Fqq2uWTlMty6OSh17FN1nuWjHkyGcxp1mfCy1lqmbnr cMLjzhCH/vbQLbqMuKC8PEZbKe7KsxKruG9UuWJ8d+jNQsspbBZoqhveeOcjWxYtFTHL 2T7Q== X-Gm-Message-State: AGi0PuZhYxQQhVg7F5sn31dh7HR85h9dLu/KR+sFFuA8i4T0PVLBsB3u 5t/eqtn+1FPEHpMP3unGRtmdQzsTGSt1R4EE1YU= X-Received: by 2002:aed:2e24:: with SMTP id j33mr1456847qtd.117.1588921929065; Fri, 08 May 2020 00:12:09 -0700 (PDT) MIME-Version: 1.0 References: <20200508063954.256593-1-irogers@google.com> In-Reply-To: <20200508063954.256593-1-irogers@google.com> From: Andrii Nakryiko Date: Fri, 8 May 2020 00:11:58 -0700 Message-ID: Subject: Re: [PATCH] libbpf hashmap: fix undefined behavior in hash_bits To: Ian Rogers Cc: Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , John Fastabend , KP Singh , Networking , bpf , open list Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 7, 2020 at 11:40 PM Ian Rogers wrote: > > If bits is 0, the case when the map is empty, then the >> is the size of > the register which is undefined behavior - on x86 it is the same as a > shift by 0. Fix by handling the 0 case explicitly. > > Signed-off-by: Ian Rogers > --- No need. The only case when bits can be 0 is when hashmap is completely empty (no elements have ever been added yet). In that case, it doesn't matter what value hash_bits() returns, hashmap__for_each_key_entry/hashmap__for_each_key_entry_safe will behave correctly, because map->buckets will be NULL. > tools/lib/bpf/hashmap.h | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/tools/lib/bpf/hashmap.h b/tools/lib/bpf/hashmap.h > index d5ef212a55ba..781db653d16c 100644 > --- a/tools/lib/bpf/hashmap.h > +++ b/tools/lib/bpf/hashmap.h > @@ -19,6 +19,8 @@ > static inline size_t hash_bits(size_t h, int bits) > { > /* shuffle bits and return requested number of upper bits */ > + if (bits == 0) > + return 0; > return (h * 11400714819323198485llu) >> (__WORDSIZE - bits); > } > > -- > 2.26.2.645.ge9eca65c58-goog >