Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp646564pxf; Wed, 10 Mar 2021 14:11:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJywSFfYXY49pFKcov/mWUXX3/f0BXmNCUvFOFDIi3uNxyJjJGm0piIxQy/0A36QrD2vlb2E X-Received: by 2002:a17:906:b2c3:: with SMTP id cf3mr56676ejb.133.1615414260328; Wed, 10 Mar 2021 14:11:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615414260; cv=none; d=google.com; s=arc-20160816; b=GuUvWCD3l/ZLg3cp/C34cB/bBDYbh4/E7cTfuPq02UbEomXxQ8n3iMZXhmly5q9evb DXgL+Osl+dxVbbH8u5Y7dfaV3hrA53o0MaWXvFr9ds9jglIA0Bm/PpS55SVhSQbGtkkU f5SNUXMi2l/DgVa60/tP+toMK36sDgVRr6mvqH04PG0Ec3rP1OjCoiHTa33ZTUKsfMy2 C4CvYL5ee1i7o/fhU9WlJMe36G2cxJGqXK+I66DaUla/fhPjDdAfOHkH7rF8Ak3gXfER IpdF+Kfl6yYg5qHxdC1hDpajBCOdp52wqOVdXlDxfDj1r87ziI5CIGHdskk0dHWU7snD X3zg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=3yg0o3VoZc2Igrv2WKaYIdci4mKdYRYHXxP6y4sgjMk=; b=S5ZgEA8VFE11SveAbrKCzTsLsk1rJ1gWzbelXQCiwSntHcK9BYxe8Jcpbj0D21yBrW r73eZSE0VdluROEsppcvsX/mPh/xBpxeBrt63MpyQN+TsIIwvhEgHk6KQOZlm0ZtA2dy IaTykssnNWe+2itg0563EOjbFTfFMFFZ46VSuA1bTtObkqDlJEXz21dFb/dSedfyLYm6 OBDeweU+n23+5l6rg6Du6+YobtukKAWRT2minDv1di3/5u0GZBaAUzqP9lFAm8jT6dlV ynWAtTj8L/uOpDnkwBd9o84V7NOsl0ftZsytve3kqYKK+PGXe/JYgd7p0bcf165LZIon pvlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=GDwL5+aE; 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=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m19si440789eds.451.2021.03.10.14.10.38; Wed, 10 Mar 2021 14:11:00 -0800 (PST) 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=@chromium.org header.s=google header.b=GDwL5+aE; 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=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234013AbhCJWH3 (ORCPT + 99 others); Wed, 10 Mar 2021 17:07:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233725AbhCJWHT (ORCPT ); Wed, 10 Mar 2021 17:07:19 -0500 Received: from mail-io1-xd30.google.com (mail-io1-xd30.google.com [IPv6:2607:f8b0:4864:20::d30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BD1CC061756 for ; Wed, 10 Mar 2021 14:07:19 -0800 (PST) Received: by mail-io1-xd30.google.com with SMTP id k2so19698954ioh.5 for ; Wed, 10 Mar 2021 14:07:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=3yg0o3VoZc2Igrv2WKaYIdci4mKdYRYHXxP6y4sgjMk=; b=GDwL5+aEG8LzO+VLWexjdVnLbmtF53yrvXdPdWxdCFmMqml9Bh/Ab2NmG+tFjIdIxm n1606YRVDxLCTirQ/87COiOR/TK22acOukM8TYX+dxxfQsdEboOZd4E2ocQ1cvQRtQP5 +UevCGAtwW5uEoEfAszU0YXaY8v/9vv0gcwBg= 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=3yg0o3VoZc2Igrv2WKaYIdci4mKdYRYHXxP6y4sgjMk=; b=jRwZ4mQSVBk4NjtfUxOM/5EF0aSYi2ltcsfD6Sz1Ij0GRE1lJ9Y06wb7yd7xg+uoL2 Yna5uLOabqmj9oqY9GVgbqGHXel5oj0LJNc8fZ+GuSqlg6xJIsTPWwQsWeKHz6hTBL8P hgI0jZH6ThNeFvVfR45m7YMwtnhRUglo+PkmoxkTjMgLm6x12A9CzsobkX4kUMAFp0vb hAwGMYyRgWYM3PqzDpcf/G4OwQ/tSiAKCKzh7hx18hAirfNCf96RNpS26kPaM0RNGqoy PwMuqQw1dHrRWkkD9mWHxQiL/mbMKYqt7XwhcX6JuvcRweKBMHSGzwGnKUcezQisL406 MZ2w== X-Gm-Message-State: AOAM533XJehES3FjN4IMQKjPhjctltX2QrOUIRhEu2DwIciIUJDV2qtk SMms9G2ko1WNHgek0kCkO7rcfVZ5pywJRcS8UsEYkQ== X-Received: by 2002:a02:a303:: with SMTP id q3mr652085jai.32.1615414038764; Wed, 10 Mar 2021 14:07:18 -0800 (PST) MIME-Version: 1.0 References: <20210310015455.1095207-1-revest@chromium.org> <454d2e4b-f842-624c-a89e-441830c98e99@fb.com> In-Reply-To: From: Florent Revest Date: Wed, 10 Mar 2021 23:07:08 +0100 Message-ID: Subject: Re: [BUG] One-liner array initialization with two pointers in BPF results in NULLs To: Andrii Nakryiko Cc: Yonghong Song , bpf , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , KP Singh , Brendan Jackman , open list Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 10, 2021 at 10:51 PM Andrii Nakryiko wrote: > On Wed, Mar 10, 2021 at 12:12 PM Andrii Nakryiko > wrote: > > On Wed, Mar 10, 2021 at 8:59 AM Yonghong Song wrote: > > > On 3/10/21 3:48 AM, Florent Revest wrote: > > > > On Wed, Mar 10, 2021 at 6:16 AM Yonghong Song wrote: > > > >> On 3/9/21 7:43 PM, Yonghong Song wrote: > > > >>> On 3/9/21 5:54 PM, Florent Revest wrote: > > > >>>> I noticed that initializing an array of pointers using this syntax: > > > >>>> __u64 array[] = { (__u64)&var1, (__u64)&var2 }; > > > >>>> (which is a fairly common operation with macros such as BPF_SEQ_PRINTF) > > > >>>> always results in array[0] and array[1] being NULL. > > > >>>> > > > >>>> Interestingly, if the array is only initialized with one pointer, ex: > > > >>>> __u64 array[] = { (__u64)&var1 }; > > > >>>> Then array[0] will not be NULL. > > > >>>> > > > >>>> Or if the array is initialized field by field, ex: > > > >>>> __u64 array[2]; > > > >>>> array[0] = (__u64)&var1; > > > >>>> array[1] = (__u64)&var2; > > > >>>> Then array[0] and array[1] will not be NULL either. > > > >>>> > > > >>>> I'm assuming that this should have something to do with relocations > > > >>>> and might be a bug in clang or in libbpf but because I don't know much > > > >>>> about these, I thought that reporting could be a good first step. :) > > > >>> > > > >>> Thanks for reporting. What you guess is correct, this is due to > > > >>> relocations :-( > > > >>> > > > >>> The compiler notoriously tend to put complex initial values into > > > >>> rodata section. For example, for > > > >>> __u64 array[] = { (__u64)&var1, (__u64)&var2 }; > > > >>> the compiler will put > > > >>> { (__u64)&var1, (__u64)&var2 } > > > >>> into rodata section. > > > >>> > > > >>> But &var1 and &var2 themselves need relocation since they are > > > >>> address of static variables which will sit inside .data section. > > > >>> > > > >>> So in the elf file, you will see the following relocations: > > > >>> > > > >>> RELOCATION RECORDS FOR [.rodata]: > > > >>> OFFSET TYPE VALUE > > > >>> 0000000000000018 R_BPF_64_64 .data > > > >>> 0000000000000020 R_BPF_64_64 .data > > > > > > > > Right :) Thank you for the explanations Yonghong! > > > > > > > >>> Currently, libbpf does not handle relocation inside .rodata > > > >>> section, so they content remains 0. > > > > > > > > Just for my own edification, why is .rodata relocation not yet handled > > > > in libbpf ? Is it because of a read-only mapping that makes it more > > > > difficult ? > > > > > > We don't have this use case before. In general, people do not put > > > string pointers in init code in the declaration. I think > > > bpf_seq_printf() is special about this and hence triggering > > > the issue. Fair enough, the only reasonable usecase that I can think of is a selftest like the one I wrote for bpf_snprintf and the macro in bpf_tracing.h will be a good enough workaround for that. > > > To support relocation of rodata section, kernel needs to be > > > involved and this is actually more complicated as > > > > Exactly. It would be trivial for libbpf to support it, but it needs to > > resolve to the actual in-kernel address of a map (plus offset), which > > libbpf has no way of knowing. Ah right, I see now, thanks! Indeed this would be quite complex and probably not very useful. > Having said that, libbpf should probably error out when such > relocation is present, because there is no way the application with > such relocations is going to be correct. Good point, it would have helped me notice the problem earlier. :)