Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp747515pxb; Wed, 3 Nov 2021 11:41:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzWaY37gmc1xpf5PDamvDTHOJCOmCGjhkAfDMdn+1mJc8uIVhnMbXKKwA8QIyFnvEVZE+4l X-Received: by 2002:a05:6e02:1d1a:: with SMTP id i26mr8151515ila.303.1635964871646; Wed, 03 Nov 2021 11:41:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635964871; cv=none; d=google.com; s=arc-20160816; b=XZ4Yxq7rdc+87iz5eTQLIAX/CKfIaV1UQuUNsFLptkEJ0aHLJ10/6IZu1xA3WFulo/ ySB4r+tz0R2ZwQq1BVn9Ehsn/hJEAz64CDkceGkSZeP1uJYAlZniuns+WyFmz6HitLG3 2aqIfR9DfZyzidbdYt/JBdWIPaemVcUvZwAP5QbCa6yZ1sbSG+HyeOMIKA4ff3YnXp87 uT7XDAlp0P9cebgyHo3gcHx0TkQid0Kjs/GOe4d0jNTqR1N61eVsVCJlL39HcfTS+Vzn tr6vaesN1UXqVoF3XI681Tppzm/lBkkkIVFFFLZ1/r5q9GYfmALSar5G7FM4Xb66o3Sk Gabg== 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=ZtBQxj50FdO8v/Q0TtDRDnyywac0YLAeKVXvSpTYK3c=; b=Wl7DlzNQW6Qi3bA5uFPVpVnDlP9AsJd9dllRk2Ma2R0vaEZz3v8z5GqNjn+0hma+bA uEX/p0eaBQBWt77S/VsI9z47m2876c2SQF3Tza7U+RWEHNUeiuy6sb6cecwNqLxYCDhr gu8cp52xjsspTp+vJcQ6dDYuq4AExnqZWKMhgW2oercCTnYCH2jStVRRaxQr8F5T/zvN Ros6PfWBhaO8qV24vKfUGQzR+qtG7u02M2PBGnqT9AkdwATlLh7pv3THtQDmJUR6yXIv NIuy5AeLPC7JLq1dpZR4oVxW0UcR4BVTgDpMCghi7uw6lS4zgGZGIedipLw+flEuvZcv 55Rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=LL605NzB; 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 x6si6308910iow.112.2021.11.03.11.40.59; Wed, 03 Nov 2021 11:41:11 -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=20210112 header.b=LL605NzB; 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 S231489AbhKCSmJ (ORCPT + 99 others); Wed, 3 Nov 2021 14:42:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231402AbhKCSmH (ORCPT ); Wed, 3 Nov 2021 14:42:07 -0400 Received: from mail-yb1-xb2b.google.com (mail-yb1-xb2b.google.com [IPv6:2607:f8b0:4864:20::b2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DA50C061205; Wed, 3 Nov 2021 11:39:31 -0700 (PDT) Received: by mail-yb1-xb2b.google.com with SMTP id e136so5391564ybc.4; Wed, 03 Nov 2021 11:39:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ZtBQxj50FdO8v/Q0TtDRDnyywac0YLAeKVXvSpTYK3c=; b=LL605NzBTkON5uknJc51rqMFlx4Y8vz3C4BE2xzvtJDfpw3vyd548KQy+HbDxdDKz2 vXp9y5DpfBhrR0lh5LGKdPGhYvZj6Af4RI7FmDfKBWzBKWFo6k+/ND0xy2cQFRmgLjli HDn2tsH2FdKgClk/GPq/Pv4uW6kq6cuUPHMaM/TfdadweJzb9wZAmTL9nb77tUJVUicd 3PXSYhLFe868XIUB1z90HvmN8UKKPST0F4zbIsHcilbNWlnYcrzKb7jT/V+5ZmvUFLFv Pj8xKzWxzh5jDXh+iW5MJ6al+iRPDZF0zbkVwmTXArF841Ack042zhtafnzgxp4WxOYj ckzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ZtBQxj50FdO8v/Q0TtDRDnyywac0YLAeKVXvSpTYK3c=; b=4Onkmiw9SghMo/rdKonaD4Mryy9QRi/nQXO4xyCPgpMa35ZAcXrlaStiyxTdi/FbYH sAV5524iW5TyAFO61QxbygjYN6iYHap7NBUnU2IZE5927v41ERzLKrFrl3rfJn0ZyAg8 hoVRyGANyLL4YKRkyv9kZOrAa8lL7l1iWmKt1p03cvCzm3NMHoEd85IhRFe4KWGPW/bU 0lV8ISnsYv7JxJaXaQoUuHfnkvMID+cicOiyIGGPfxPi+XXbcSCENvlQtMCCs++IYQaI G3PXHynh2z2QoaYyarJDuqmCCz6p0Kwfd1aLen4MWT551PjY/Kjva34bgnkRltWYKf7/ +eYQ== X-Gm-Message-State: AOAM531MHHwdPp+p+WTOREHrwCnv2KGtYgmuW/XdZoR9vwwg+K37RZrM gOMuzzANSewxHsAb9snbNAOxzw+H9u1wiH09TRU= X-Received: by 2002:a25:afcf:: with SMTP id d15mr46347135ybj.433.1635964770161; Wed, 03 Nov 2021 11:39:30 -0700 (PDT) MIME-Version: 1.0 References: <1635932969-13149-1-git-send-email-alan.maguire@oracle.com> <1635932969-13149-3-git-send-email-alan.maguire@oracle.com> In-Reply-To: <1635932969-13149-3-git-send-email-alan.maguire@oracle.com> From: Andrii Nakryiko Date: Wed, 3 Nov 2021 11:39:18 -0700 Message-ID: Subject: Re: [PATCH bpf-next 2/2] selftests/bpf: add exception handling selftests for tp_bpf program To: Alan Maguire Cc: ardb@kernel.org, Catalin Marinas , Will Deacon , Daniel Borkmann , Alexei Starovoitov , Zi Shen Lim , Andrii Nakryiko , Martin Lau , Song Liu , Yonghong Song , john fastabend , KP Singh , andreyknvl@gmail.com, vincenzo.frascino@arm.com, Mark Rutland , samitolvanen@google.com, joey.gouly@arm.com, maz@kernel.org, daizhiyuan@phytium.com.cn, jthierry@redhat.com, Tian Tao , pcc@google.com, Andrew Morton , rppt@kernel.org, Jisheng.Zhang@synaptics.com, liu.hailong6@zte.com.cn, linux-arm-kernel , open list , Networking , bpf Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 3, 2021 at 2:50 AM Alan Maguire wrote: > > Exception handling is triggered in BPF tracing programs when > a NULL pointer is dereferenced; the exception handler zeroes the > target register and execution of the BPF program progresses. > > To test exception handling then, we need to trigger a NULL pointer > dereference for a field which should never be zero; if it is, the > only explanation is the exception handler ran. The skb->sk is > the NULL pointer chosen (for a ping received for 127.0.0.1 there > is no associated socket), and the sk_sndbuf size is chosen as the > "should never be 0" field. Test verifies sk is NULL and sk_sndbuf > is zero. > > Signed-off-by: Alan Maguire > --- > tools/testing/selftests/bpf/prog_tests/exhandler.c | 45 ++++++++++++++++++++++ > tools/testing/selftests/bpf/progs/exhandler_kern.c | 35 +++++++++++++++++ > 2 files changed, 80 insertions(+) > create mode 100644 tools/testing/selftests/bpf/prog_tests/exhandler.c > create mode 100644 tools/testing/selftests/bpf/progs/exhandler_kern.c > > diff --git a/tools/testing/selftests/bpf/prog_tests/exhandler.c b/tools/testing/selftests/bpf/prog_tests/exhandler.c > new file mode 100644 > index 0000000..5999498 > --- /dev/null > +++ b/tools/testing/selftests/bpf/prog_tests/exhandler.c > @@ -0,0 +1,45 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* Copyright (c) 2021, Oracle and/or its affiliates. */ > + > +#include > + > +/* Test that verifies exception handling is working; ping to localhost > + * will result in a receive with a NULL skb->sk; our BPF program > + * then dereferences the an sk field which shouldn't be 0, and if we > + * see 0 we can conclude the exception handler ran when we attempted to > + * dereference the NULL sk and zeroed the destination register. > + */ > +#include "exhandler_kern.skel.h" > + > +#define SYSTEM(...) \ > + (env.verbosity >= VERBOSE_VERY ? \ > + system(__VA_ARGS__) : system(__VA_ARGS__ " >/dev/null 2>&1")) > + > +void test_exhandler(void) > +{ > + struct exhandler_kern *skel; > + struct exhandler_kern__bss *bss; > + int err = 0, duration = 0; > + > + skel = exhandler_kern__open_and_load(); > + if (CHECK(!skel, "skel_load", "skeleton failed: %d\n", err)) > + goto cleanup; > + > + bss = skel->bss; nit: you don't need to have a separate variable for that, skel->bss->exception_triggered in below check would be just as readable > + > + err = exhandler_kern__attach(skel); > + if (CHECK(err, "attach", "attach failed: %d\n", err)) > + goto cleanup; > + > + if (CHECK(SYSTEM("ping -c 1 127.0.0.1"), Is there some other tracepoint or kernel function that could be used for testing and triggered without shelling out to ping binary? This hurts test isolation and will make it or some other ping-using selftests spuriously fail when running in parallel test mode (i.e., sudo ./test_progs -j). > + "ping localhost", > + "ping localhost failed\n")) > + goto cleanup; > + > + if (CHECK(bss->exception_triggered == 0, please use ASSERT_EQ() instead, CHECK()s are kind of deprecated for new tests > + "verify exceptions were triggered", > + "no exceptions were triggered\n")) > + goto cleanup; > +cleanup: > + exhandler_kern__destroy(skel); > +} > diff --git a/tools/testing/selftests/bpf/progs/exhandler_kern.c b/tools/testing/selftests/bpf/progs/exhandler_kern.c > new file mode 100644 > index 0000000..4049450 > --- /dev/null > +++ b/tools/testing/selftests/bpf/progs/exhandler_kern.c > @@ -0,0 +1,35 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* Copyright (c) 2021, Oracle and/or its affiliates. */ > + > +#include "vmlinux.h" > + > +#include > +#include > +#include > + > +char _license[] SEC("license") = "GPL"; > + > +unsigned int exception_triggered; > + > +/* TRACE_EVENT(netif_rx, > + * TP_PROTO(struct sk_buff *skb), > + */ > +SEC("tp_btf/netif_rx") > +int BPF_PROG(trace_netif_rx, struct sk_buff *skb) > +{ > + struct sock *sk; > + int sndbuf; > + > + /* To verify we hit an exception we dereference skb->sk->sk_sndbuf; > + * sndbuf size should never be zero, so if it is we know the exception > + * handler triggered and zeroed the destination register. > + */ > + __builtin_preserve_access_index(({ > + sk = skb->sk; > + sndbuf = sk->sk_sndbuf; > + })); you don't need __builtin_preserve_access_index(({ }) region, because vmlinux.h already annotates all the types with preserve_access_index attribute > + > + if (!sk && !sndbuf) > + exception_triggered++; > + return 0; > +} > -- > 1.8.3.1 >