Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp258927ybj; Fri, 8 May 2020 11:06:46 -0700 (PDT) X-Google-Smtp-Source: APiQypJGeQABu7KM2ipAdmlQz+3pZvxOEyNMk28fUpRCuEes1lUDosr5yYmt4T0SQH7RCL9nlhyR X-Received: by 2002:a50:a985:: with SMTP id n5mr3225911edc.338.1588961206426; Fri, 08 May 2020 11:06:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588961206; cv=none; d=google.com; s=arc-20160816; b=KuSbJNSgCbP0fTAc5BiFfOPVMiwF2TFzXrU/ZjsNVqSFj1IRPs8XOEoGYmC9j6jyo3 15udpllISqoxHvUsgLMPGpLXH40YDswRbBupQ9cnqYFSXzykKVXkNZPuCU6e6wYgDn/u Aoo9l4mRI89O5TLUTAAdLB5PGyLwRM7EjqW4s0r3bOfgRrId5pIra118Gf9lUhqCFyNr 9dp6/ngW1+dV0GuQP5Qfo+/Qa3ZO/XzQSeBhToKFaoVoYIHNlb50JXX3+NNM6Xv7hXW+ Q3gB2dKD29hfCXUv51FmLTQn5fFfui+mn9R0vDB5mSstHTwJI5jvLDyf09lLlzKh/ogJ 5rxA== 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=6n7N9OP5xGlbdCY3wbnFvbe92wVnKtN4kGkgM+TDOk0=; b=JA/N441Sznvo87Acwihtl8d2k9u8yoBwxRAt0N7vvLLdLqR3MBMuB5zJKzZUH/HOnM BUAgBsqmjFmLv7DSNauT5WtPjOofIvbTeUnvwa5W03ITmefy5TA5Xw78bFyehnxwW6j6 9ZfBk/6aIaRp8FHhWjVdZaV/Gg/Gc7eZAQqFuoHkASiOVyc0PU2WjK/12Bu1cFyt2SgV 5JfMJlIjR7p3IFcG2F4hBuWtkj84JNOrkQw09YB+tzSrqGCyWuGwS2Kf9dG3CUwScT+z xUaN4YUwp2I9jhia6sPPwJwEtA9J/yeU+d/9tRWIT1SaFZ6v5xNmOpZ6QW3Zp1Oussx5 DZlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=V7+iqsCA; 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 c13si1448099edq.97.2020.05.08.11.06.18; Fri, 08 May 2020 11:06:46 -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=V7+iqsCA; 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 S1727086AbgEHSEl (ORCPT + 99 others); Fri, 8 May 2020 14:04:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726756AbgEHSEl (ORCPT ); Fri, 8 May 2020 14:04:41 -0400 Received: from mail-io1-xd44.google.com (mail-io1-xd44.google.com [IPv6:2607:f8b0:4864:20::d44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39321C061A0C; Fri, 8 May 2020 11:04:41 -0700 (PDT) Received: by mail-io1-xd44.google.com with SMTP id z2so2655181iol.11; Fri, 08 May 2020 11:04:41 -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=6n7N9OP5xGlbdCY3wbnFvbe92wVnKtN4kGkgM+TDOk0=; b=V7+iqsCA8GIu6utgH2UMhByKaA2/FCf7MLe6adjxHlKp1CGufE5CkcjrPULjDhUhw7 C1SLnkOu88d+kq7GTGEhYnndx2U218EuNKPCi15Iiw9YgiVEqobpg1xfBRNb565+4Dp3 Iiv0OGfmbGMgvKl8dp8hg3QClDCHjoK/AFnzaxFngSlMIQlWZm8eFljIrCJRE/7Iq8K0 HMUTpORzJaindByoGANqSJKIIRJvXnTjroQPEYkH81WLuEqy43XDfVcdpMmIGcTdpzrq +rtv6Ksf7hvlvzZ3bFK0I66nt123P7WSrgDRR7mDmyqiaVEKZwGriVhn0/jackUrHwl1 IXFg== 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=6n7N9OP5xGlbdCY3wbnFvbe92wVnKtN4kGkgM+TDOk0=; b=LhrwEZqrpCH8wKGSqb9GllcoaQ/qCDlAe6RlGIJLjOWQG/5ewZdTaWuVk4kHvdwZ50 CB3gc9t/Jogikce2+KY4H2WnJzHlOA58CW3hZeriJaKxJt7SZa/seCXSNRU4TH7NXtbj 6gs9Z7ATzB6ls07S81LKySdnQJuWTt2tqs26PWqwJmZq1ri+Dw6abf4cEFNC7a+mcH4O qbpoyGztkL6Es7OO0hka5IufZWYbR76Og0gfH4018Fz93bzjaoP1qngqSIawdzQuU1tc gwZfvkgykaR3Z/HSWqTeD+4En2kmMhIaI69rmgC60uLWGYt/12jDI9nV8/dp5Gbp/xv5 onyA== X-Gm-Message-State: AGi0PubhBT9Ag+8O5xt51tmoWq1nHdhjWic+uW8FwRP2Xyabeff/AP6z dJYtn6Eh6abKkMg8QF6mLqFPl8xHjYEAK4Orwsg= X-Received: by 2002:a5d:970e:: with SMTP id h14mr3769395iol.117.1588961080652; Fri, 08 May 2020 11:04:40 -0700 (PDT) MIME-Version: 1.0 References: <20200508063954.256593-1-irogers@google.com> In-Reply-To: From: Andrii Nakryiko Date: Fri, 8 May 2020 11:04:29 -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 Fri, May 8, 2020 at 12:21 AM Ian Rogers wrote: > > On Fri, May 8, 2020 at 12:12 AM Andrii Nakryiko > wrote: > > > > 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. > > Agreed. Unfortunately the LLVM undefined behavior sanitizer (I've not > tested with GCC to the same extent) will cause an exit when it sees >> > 64 regardless of whether the value is used or not. It'd be possible to > #ifdef this code on whether a sanitizer was present. Yeah, let's do that rather than slowing down hashing function. > > Thanks, > Ian > > > > 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 > > >