From patchwork Sat Apr 13 21:12:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Deslandes X-Patchwork-Id: 13628794 X-Patchwork-Delegate: bpf@iogearbox.net Received: from wfout3-smtp.messagingengine.com (wfout3-smtp.messagingengine.com [64.147.123.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F23F55E4F; Sat, 13 Apr 2024 21:13:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.146 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713042795; cv=none; b=djBOzK2oiAuIeI50yqHqXyGWznsWLiVXNfwu6J9VOqatWTCfKhfbNZMH+tNzkhy5Tp6eKpSyqNjK+wrHFB1G4YEQnKVsCN4/3XVTgB41ysmNc4ua8FT1zoUWusw4JvJZwL28VZJ31tRD0/dGwjqGsgb1aKvhqx9aQt+apbPSFT4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713042795; c=relaxed/simple; bh=TnwOVpWbbZtVgN6LqfHmT/tXLz+PEvXIlov2jxXkZNs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=F/3scqmaZ6jdDQ1sHSeCWKQ6fD4/cHfJ4y5iJSRllKhKsFiOy0A4KeN2MLTmmsb1+cqW260HkWqqt/vZj3yeg0UeZwnPS6oZFU6UjtGUA6Kj4dS1gXNir1XBnycJAbEkEdZk3sQcf3+/43ij0SrhrQp7Nt8qNkrR4BdK2ReQnOU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=naccy.de; spf=pass smtp.mailfrom=naccy.de; dkim=pass (2048-bit key) header.d=naccy.de header.i=@naccy.de header.b=guA0T/K0; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=TN9PY7j4; arc=none smtp.client-ip=64.147.123.146 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=naccy.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=naccy.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=naccy.de header.i=@naccy.de header.b="guA0T/K0"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="TN9PY7j4" Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfout.west.internal (Postfix) with ESMTP id 97C6A1C000FC; Sat, 13 Apr 2024 17:13:10 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Sat, 13 Apr 2024 17:13:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=naccy.de; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm1; t=1713042790; x= 1713129190; bh=Vb15qo2kI4aLnF/tBRyhuoRFKr8FftX5ce1CP89OW5U=; b=g uA0T/K0knLMUDBpeID1XaHLwE8KWxFh3IA1WuTTYxaU8BwBoU7ojFg5GNgLkFmKr GZkM0GPL5bR6TwDNuEv2+hIuxIp+Esl9QUYLttl/cVDSi933aInI098BCHMAYZAh fBw90obEnQAxq6Kxrkz1kgLwoKmjvukKlHj5FIs3qZZyPYJCfyFGeESpS2ij4pLZ jeTj03eZfUG/Oqr6CHZn1AwcXwNcu+49iUgW8eYrXU0reumTUtqtTAWvtEz3t5Xj 8ROAm3i/efb9VsS3oBd/vWVqQMpipooKO373OoJQ3ioWpoTW+fMKiZQPzXBxDFAc A6NXvMAAvbmoyzPfwiXQA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1713042790; x= 1713129190; bh=Vb15qo2kI4aLnF/tBRyhuoRFKr8FftX5ce1CP89OW5U=; b=T N9PY7j4Ukliqeit4AbbaRTLSFafwV8onDZt0Zdr3TbHJ5gkut289ZW7owF5hx9TA ESfPTPoGmejr3gZEjFM3c/FTgOn0aVmrEy//CcRqoCv+hhmu5nv3spRIggwl3d5+ bQ3LVH/WLdxR1aZBT664afNLuM8bkZcMlwNXaGu5KHnNIiwZA9KmuEbsugw9YKki w7Y3SWDmFpJCi7VTycdOsWKj7mRO1eZ88b9yqPZWxlFqQBv1a9Re4fxKlRSL2452 5Iv9l9gxx12cT6EerssCOLc3kUB8Xv71CxfDfvtwgV0xr0Gavp0Q+EeIxQi3LZGe gu6dm/Dp68cXreZRHZm+A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeiiedgudeiudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpefsuhgv nhhtihhnucffvghslhgrnhguvghsuceoqhguvgesnhgrtggthidruggvqeenucggtffrrg htthgvrhhnpeevieehjedtveevueeujedtveehtddugfeukeeffeettddttddtleehudeh feetleenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe hquggvsehnrggttgihrdguvg X-ME-Proxy: Feedback-ID: i14194934:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 13 Apr 2024 17:13:07 -0400 (EDT) From: Quentin Deslandes To: bpf@vger.kernel.org Cc: Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , linux-kernel@vger.kernel.org, kernel-team@meta.com, Quentin Deslandes Subject: [PATCH 1/2] libbpf: fix misaligned array closing bracket Date: Sat, 13 Apr 2024 23:12:57 +0200 Message-ID: <20240413211258.134421-2-qde@naccy.de> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240413211258.134421-1-qde@naccy.de> References: <20240413211258.134421-1-qde@naccy.de> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net In btf_dump_array_data(), libbpf will call btf_dump_dump_type_data() for each element. For an array of characters, each element will be processed the following way: - btf_dump_dump_type_data() is called to print the character - btf_dump_data_pfx() prefixes the current line with the proper number of indentations - btf_dump_int_data() is called to print the character - After the last character is printed, btf_dump_dump_type_data() calls btf_dump_data_pfx() before writing the closing bracket However, for an array containing characters, btf_dump_int_data() won't print any '\0' and subsequent characters. This leads to situations where the line prefix is written, no character is added, then the prefix is written again before adding the closing bracket: (struct sk_metadata){ .str_array = (__u8[14])[ 'H', 'e', 'l', 'l', 'o', ], This change solves this issue by printing the '\0' character, which has two benefits: - The bracket closing the array is properly aligned - It's clear from a user point of view that libbpf uses '\0' as a terminator for arrays of characters. Signed-off-by: Quentin Deslandes --- tools/lib/bpf/btf_dump.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c index 4d9f30bf7f01..6a37e8517435 100644 --- a/tools/lib/bpf/btf_dump.c +++ b/tools/lib/bpf/btf_dump.c @@ -1929,6 +1929,7 @@ static int btf_dump_int_data(struct btf_dump *d, if (d->typed_dump->is_array_terminated) break; if (*(char *)data == '\0') { + btf_dump_type_values(d, "'\\0'"); d->typed_dump->is_array_terminated = true; break; } From patchwork Sat Apr 13 21:12:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Deslandes X-Patchwork-Id: 13628795 X-Patchwork-Delegate: bpf@iogearbox.net Received: from wfout3-smtp.messagingengine.com (wfout3-smtp.messagingengine.com [64.147.123.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CE34756B7A; Sat, 13 Apr 2024 21:13:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.146 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713042799; cv=none; b=D4VK8kg57m9AKqR7qMwIiXxGQ/8sbfdImhGyJKfH4TdqKisbG+W5FwzJJIlcON/SutF5ehXHfzPIohAmtcLyalcOIWw7z+KMwz6G+C2roKC/yXBLpfyVspT5Hzh4vz2NjidfNurFMduyX3SHpQuMok/tZGUYam6tSA+iC5k+BDQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713042799; c=relaxed/simple; bh=XLox6UtpxNNViuhE/NDra0lioJOlzF3C0YrXvofTdgE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DmgYoZ5bpEmFf6ExrxvrqWMCtUsgNiSIsEC33Zan1b6SIaQ/lSOzA94LK5NkkstDo8YXWjaxYUDqqP/CoAmHQ7fjF2WEDkrw+akdpRehAbUwK6VRuos4uFc5rO7i1rLZTSQ83+Fqmuf7sJg54hIQf7gSirhKRdTMovl0qYBNjlU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=naccy.de; spf=pass smtp.mailfrom=naccy.de; dkim=pass (2048-bit key) header.d=naccy.de header.i=@naccy.de header.b=E9arffBQ; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=R92ttszp; arc=none smtp.client-ip=64.147.123.146 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=naccy.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=naccy.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=naccy.de header.i=@naccy.de header.b="E9arffBQ"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="R92ttszp" Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfout.west.internal (Postfix) with ESMTP id 6919E1C000F3; Sat, 13 Apr 2024 17:13:15 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Sat, 13 Apr 2024 17:13:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=naccy.de; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm1; t=1713042794; x= 1713129194; bh=x9bMflE1VBqipkYwFyM5kAoaLsmNkwYcPLOiqTA4+lc=; b=E 9arffBQkErqwyEZdWFovtua/5raeNnSZ1xWOsGubC98lFyJcqm3hwtjG1Q6n8K5L TtS+5Abds7t6VNGxUBn2jvZ30mJtSvSMZtFFh5bsrAeuUio5NZ50O0qvZrboq4+v hGJ7BlO4+TwO+f8v1x2hPPGl43NSXkU/kHlFq9jpa5WL0o8oU3ypMCzeQS6418b0 JTy8ZlFL3jqGUtfBKs9jzj9St3R81hxnPsJjnrV0MBWwvWePVYfKLhUJdJ9iZ6qt OkYjuhdVbEEYvCjmNJrshpZ6SVy4NctFGLTHGFPtWfNHB8ezeliwaembksnIfSNw gCbHazQ5PdibDH4Nzo81w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1713042794; x= 1713129194; bh=x9bMflE1VBqipkYwFyM5kAoaLsmNkwYcPLOiqTA4+lc=; b=R 92ttszpXK6uq685UbSmsEqJpJquDBFoz4llvPrc5NB6x1iuycHgz8UQxcsXnO3EQ NEAoWY0eDGqZZtVL8jM8HNRlaseWRNKisUAbvHYr6xkI+JPnTVXlRica/0WmHkq9 a61ZjqZC0qZNcbUXxYT74u81/leAoAB3DNjgoq8czeII8npuuCNmnl216t2fUaPQ f8RNx0b/dmyCWZkTUJ/dP7f2Ypz62s3emCmslofnrtLNtJYwGF1rqmbiTQ5h5RL8 uQAUvBD+u0s+PelncJhGLtuwPCpPRVGQa2dEJ+AGz0uuB+tMYiDo+Wm0x82MSiM9 OH3ir0EvZYvIp4TXqVekg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeiiedgudeiudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpefsuhgv nhhtihhnucffvghslhgrnhguvghsuceoqhguvgesnhgrtggthidruggvqeenucggtffrrg htthgvrhhnpeevieehjedtveevueeujedtveehtddugfeukeeffeettddttddtleehudeh feetleenucevlhhushhtvghrufhiiigvpedunecurfgrrhgrmhepmhgrihhlfhhrohhmpe hquggvsehnrggttgihrdguvg X-ME-Proxy: Feedback-ID: i14194934:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 13 Apr 2024 17:13:12 -0400 (EDT) From: Quentin Deslandes To: bpf@vger.kernel.org Cc: Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , linux-kernel@vger.kernel.org, kernel-team@meta.com, Quentin Deslandes Subject: [PATCH 2/2] libbpf: fix dump of subsequent char arrays Date: Sat, 13 Apr 2024 23:12:58 +0200 Message-ID: <20240413211258.134421-3-qde@naccy.de> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240413211258.134421-1-qde@naccy.de> References: <20240413211258.134421-1-qde@naccy.de> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net When dumping a character array, libbpf will watch for a '\0' and set is_array_terminated=true if found. This prevents libbpf from printing the remaining characters of the array, treating it as a nul-terminated string. However, once this flag is set, it's never reset, leading to subsequent characters array not being printed properly: .str_multi = (__u8[2][16])[ [ 'H', 'e', 'l', ], ], This patch saves the is_array_terminated flag and restores its default (false) value before looping over the elements of an array, then restores it afterward. This way, libbpf's behavior is unchanged when dumping the characters of an array, but subsequent arrays are printed properly: .str_multi = (__u8[2][16])[ [ 'H', 'e', 'l', ], [ 'l', 'o', ], ], Signed-off-by: Quentin Deslandes --- tools/lib/bpf/btf_dump.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c index 6a37e8517435..5dbca76b953f 100644 --- a/tools/lib/bpf/btf_dump.c +++ b/tools/lib/bpf/btf_dump.c @@ -2032,6 +2032,7 @@ static int btf_dump_array_data(struct btf_dump *d, __u32 i, elem_type_id; __s64 elem_size; bool is_array_member; + bool is_array_terminated; elem_type_id = array->type; elem_type = skip_mods_and_typedefs(d->btf, elem_type_id, NULL); @@ -2067,12 +2068,15 @@ static int btf_dump_array_data(struct btf_dump *d, */ is_array_member = d->typed_dump->is_array_member; d->typed_dump->is_array_member = true; + is_array_terminated = d->typed_dump->is_array_terminated; + d->typed_dump->is_array_terminated = false; for (i = 0; i < array->nelems; i++, data += elem_size) { if (d->typed_dump->is_array_terminated) break; btf_dump_dump_type_data(d, NULL, elem_type, elem_type_id, data, 0, 0); } d->typed_dump->is_array_member = is_array_member; + d->typed_dump->is_array_terminated = is_array_terminated; d->typed_dump->depth--; btf_dump_data_pfx(d); btf_dump_type_values(d, "]");