Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp1877476pxb; Mon, 12 Apr 2021 08:42:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy+WF2RC16Z7nIRrO769pYfzmoiRFTjnsrRIoa2v6e/UdibFjPkIlQ3vVgX5tl8eUAHAwOE X-Received: by 2002:a17:906:c301:: with SMTP id s1mr27209798ejz.382.1618242177941; Mon, 12 Apr 2021 08:42:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618242177; cv=none; d=google.com; s=arc-20160816; b=NzmVsnyUrWN32XTM0XhSKRZ/YUpJDWZVTv20qhY3NVK1qy9isbmdBfC/awEqHaTcRd FgD+U/JR+vZuqH6suRrC7VDb3FEl/n8Ax/JOyj/T5uaqjB1qxc9WtKqBbnsBi+3XTBnj bmnQRP7PtEdvD1CxeW1J0PYqv45Aqndky0ILfTZ4LnPhQvAYAe9mVhmWZWFLrLx2y/Xh UpBMLStaek1LdzKGq5x4MMRvPOrd6rA1XNk12ZhV+cx7Nq3QYaB2cRHBryTEuM34tXaK LMgL+x88BsMQNCsWhXUd1jNw2HeGKFInQDlcfU1MvFZejpUSO9bJiP81q5S5gwhyHDUP GhNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=LDBRTVwC0javmeAAY2NKan0r3dVftRGxpYXcgw4nO4U=; b=mPJ0jlqYN2TbI1ZqTKNS391Jferm4mAfcqcW4xg0ZfMia0FA29u5z9xjVi/axjskVZ vk5Z1qvTABcJubf7wH9AQL5/vVk5E6pas3CVNigd4QDsB3FuGU1D/HhxOYqPGsDQHrjZ 2YjDGg4fiYB9vT9As+d6a3q5U3a8EpT6wQ4KAtWNY7RhuAJvWT9UB60kjV2mr0Kf56ss FMOFqjrdOLPYQXZVa1GIPPhCbuZohKvIap3O28P44gsoBirqWQCMVe0jQqRFSjHqJRL8 eTB7lF1X6tqix04+fxGTbjnrp6mtOm/DRJYdwQ9IZiGCU5S0v+5929rvNfxt/QQ4Rtcz nAsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=UaYbxJnG; 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 jz13si2127777ejc.634.2021.04.12.08.42.34; Mon, 12 Apr 2021 08:42:57 -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=@chromium.org header.s=google header.b=UaYbxJnG; 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 S243183AbhDLPid (ORCPT + 99 others); Mon, 12 Apr 2021 11:38:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243108AbhDLPiZ (ORCPT ); Mon, 12 Apr 2021 11:38:25 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21E40C061348 for ; Mon, 12 Apr 2021 08:38:06 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id h4so4380328wrt.12 for ; Mon, 12 Apr 2021 08:38:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LDBRTVwC0javmeAAY2NKan0r3dVftRGxpYXcgw4nO4U=; b=UaYbxJnGfvh6WT53HHOwbp6ArYa8y+T1R6zqrN31jdyLCTt9x1Sv56kFFZrxzbxPTv UmVLQTLOCi4DxQUyyxz1D0cmOIL/wH26GSB4yfv+2dFS8cf1dmax0s0s1TdxrUQVDF+F j6hp/vGCEv5hf7CVYmGiPZgNrwlUlMV+d097c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LDBRTVwC0javmeAAY2NKan0r3dVftRGxpYXcgw4nO4U=; b=KlboJLZf4NmLKyGdTwX9lWkPrRLqpy0DCHlAfodqYAif2eFmyKpjNTzC5S1tagxEkW J+AwA9W/ypYMjkx/9UHyPMXW7WA29NKWuUkv8UM5KNPo8saZP1FOyzaM1bWUz/hNexqL NLNYq23MCVzDSOlzDjDfpBwpPvn0Ect3p7CDw7pJZyEI8RNTI8nTs4W/gVc8kXt3MM4c SwXFv4lPnjWx3DiDa2xl4mjbVA/F9cPR5h4xEY4GQ4IShm/B+pyMvXT51e+YsK8IMVtF x036fg6N+8yNnzlvX+RwY7JCOy+nNInAoDu5//dzkB0IkfCfN60qN07DTpuRnhNfCDSJ RSsg== X-Gm-Message-State: AOAM530ZnOwxzJ4KcUAwjQQZKiAiC2lErxIFQoIDmkAM96N8eeEyTL1D EuJzEsna+dwhWldUZpy37VNpqg== X-Received: by 2002:adf:eec1:: with SMTP id a1mr26872515wrp.81.1618241884929; Mon, 12 Apr 2021 08:38:04 -0700 (PDT) Received: from revest.zrh.corp.google.com ([2a00:79e0:42:204:a372:3c3b:eeb:ad14]) by smtp.gmail.com with ESMTPSA id i4sm2501449wrx.56.2021.04.12.08.38.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 08:38:04 -0700 (PDT) From: Florent Revest To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, yhs@fb.com, kpsingh@kernel.org, jackmanb@chromium.org, linux-kernel@vger.kernel.org, Florent Revest Subject: [PATCH bpf-next v3 6/6] selftests/bpf: Add a series of tests for bpf_snprintf Date: Mon, 12 Apr 2021 17:37:54 +0200 Message-Id: <20210412153754.235500-7-revest@chromium.org> X-Mailer: git-send-email 2.31.1.295.g9ea45b61b8-goog In-Reply-To: <20210412153754.235500-1-revest@chromium.org> References: <20210412153754.235500-1-revest@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This exercises most of the format specifiers. Signed-off-by: Florent Revest Acked-by: Andrii Nakryiko --- .../selftests/bpf/prog_tests/snprintf.c | 81 +++++++++++++++++++ .../selftests/bpf/progs/test_snprintf.c | 74 +++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/snprintf.c create mode 100644 tools/testing/selftests/bpf/progs/test_snprintf.c diff --git a/tools/testing/selftests/bpf/prog_tests/snprintf.c b/tools/testing/selftests/bpf/prog_tests/snprintf.c new file mode 100644 index 000000000000..3ad1ee885273 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/snprintf.c @@ -0,0 +1,81 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Google LLC. */ + +#include +#include "test_snprintf.skel.h" + +#define EXP_NUM_OUT "-8 9 96 -424242 1337 DABBAD00" +#define EXP_NUM_RET sizeof(EXP_NUM_OUT) + +#define EXP_IP_OUT "127.000.000.001 0000:0000:0000:0000:0000:0000:0000:0001" +#define EXP_IP_RET sizeof(EXP_IP_OUT) + +/* The third specifier, %pB, depends on compiler inlining so don't check it */ +#define EXP_SYM_OUT "schedule schedule+0x0/" +#define MIN_SYM_RET sizeof(EXP_SYM_OUT) + +/* The third specifier, %p, is a hashed pointer which changes on every reboot */ +#define EXP_ADDR_OUT "0000000000000000 ffff00000add4e55 " +#define EXP_ADDR_RET sizeof(EXP_ADDR_OUT "unknownhashedptr") + +#define EXP_STR_OUT "str1 longstr" +#define EXP_STR_RET sizeof(EXP_STR_OUT) + +#define EXP_OVER_OUT "%over" +#define EXP_OVER_RET 10 + +#define EXP_PAD_OUT " 4 000" +#define EXP_PAD_RET 900007 + +#define EXP_NO_ARG_OUT "simple case" +#define EXP_NO_ARG_RET 12 + +#define EXP_NO_BUF_RET 29 + +void test_snprintf(void) +{ + char exp_addr_out[] = EXP_ADDR_OUT; + char exp_sym_out[] = EXP_SYM_OUT; + struct test_snprintf *skel; + + skel = test_snprintf__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + + if (!ASSERT_OK(test_snprintf__attach(skel), "skel_attach")) + goto cleanup; + + /* trigger tracepoint */ + usleep(1); + + ASSERT_STREQ(skel->bss->num_out, EXP_NUM_OUT, "num_out"); + ASSERT_EQ(skel->bss->num_ret, EXP_NUM_RET, "num_ret"); + + ASSERT_STREQ(skel->bss->ip_out, EXP_IP_OUT, "ip_out"); + ASSERT_EQ(skel->bss->ip_ret, EXP_IP_RET, "ip_ret"); + + ASSERT_OK(memcmp(skel->bss->sym_out, exp_sym_out, + sizeof(exp_sym_out) - 1), "sym_out"); + ASSERT_LT(MIN_SYM_RET, skel->bss->sym_ret, "sym_ret"); + + ASSERT_OK(memcmp(skel->bss->addr_out, exp_addr_out, + sizeof(exp_addr_out) - 1), "addr_out"); + ASSERT_EQ(skel->bss->addr_ret, EXP_ADDR_RET, "addr_ret"); + + ASSERT_STREQ(skel->bss->str_out, EXP_STR_OUT, "str_out"); + ASSERT_EQ(skel->bss->str_ret, EXP_STR_RET, "str_ret"); + + ASSERT_STREQ(skel->bss->over_out, EXP_OVER_OUT, "over_out"); + ASSERT_EQ(skel->bss->over_ret, EXP_OVER_RET, "over_ret"); + + ASSERT_STREQ(skel->bss->pad_out, EXP_PAD_OUT, "pad_out"); + ASSERT_EQ(skel->bss->pad_ret, EXP_PAD_RET, "pad_ret"); + + ASSERT_STREQ(skel->bss->noarg_out, EXP_NO_ARG_OUT, "no_arg_out"); + ASSERT_EQ(skel->bss->noarg_ret, EXP_NO_ARG_RET, "no_arg_ret"); + + ASSERT_EQ(skel->bss->nobuf_ret, EXP_NO_BUF_RET, "no_buf_ret"); + +cleanup: + test_snprintf__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_snprintf.c b/tools/testing/selftests/bpf/progs/test_snprintf.c new file mode 100644 index 000000000000..4c36f355dfca --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_snprintf.c @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Google LLC. */ + +#include +#include +#include +#include + +char num_out[64] = {}; +long num_ret = 0; + +char ip_out[64] = {}; +long ip_ret = 0; + +char sym_out[64] = {}; +long sym_ret = 0; + +char addr_out[64] = {}; +long addr_ret = 0; + +char str_out[64] = {}; +long str_ret = 0; + +char over_out[6] = {}; +long over_ret = 0; + +char pad_out[10] = {}; +long pad_ret = 0; + +char noarg_out[64] = {}; +long noarg_ret = 0; + +long nobuf_ret = 0; + +extern const void schedule __ksym; + +SEC("raw_tp/sys_enter") +int handler(const void *ctx) +{ + /* Convenient values to pretty-print */ + const __u8 ex_ipv4[] = {127, 0, 0, 1}; + const __u8 ex_ipv6[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; + const char str1[] = "str1"; + const char longstr[] = "longstr"; + + /* Integer types */ + num_ret = BPF_SNPRINTF(num_out, sizeof(num_out), + "%d %u %x %li %llu %lX", + -8, 9, 150, -424242, 1337, 0xDABBAD00); + /* IP addresses */ + ip_ret = BPF_SNPRINTF(ip_out, sizeof(ip_out), "%pi4 %pI6", + &ex_ipv4, &ex_ipv6); + /* Symbol lookup formatting */ + sym_ret = BPF_SNPRINTF(sym_out, sizeof(sym_out), "%ps %pS %pB", + &schedule, &schedule, &schedule); + /* Kernel pointers */ + addr_ret = BPF_SNPRINTF(addr_out, sizeof(addr_out), "%pK %px %p", + 0, 0xFFFF00000ADD4E55, 0xFFFF00000ADD4E55); + /* Strings embedding */ + str_ret = BPF_SNPRINTF(str_out, sizeof(str_out), "%s %+05s", + str1, longstr); + /* Overflow */ + over_ret = BPF_SNPRINTF(over_out, sizeof(over_out), "%%overflow"); + /* Padding of fixed width numbers */ + pad_ret = BPF_SNPRINTF(pad_out, sizeof(pad_out), "%5d %0900000X", 4, 4); + /* No args */ + noarg_ret = BPF_SNPRINTF(noarg_out, sizeof(noarg_out), "simple case"); + /* No buffer */ + nobuf_ret = BPF_SNPRINTF(NULL, 0, "only interested in length %d", 60); + + return 0; +} + +char _license[] SEC("license") = "GPL"; -- 2.31.1.295.g9ea45b61b8-goog