From patchwork Thu Feb 10 00:36:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12741192 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD358C433F5 for ; Thu, 10 Feb 2022 02:22:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229488AbiBJCWs (ORCPT ); Wed, 9 Feb 2022 21:22:48 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:40880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229522AbiBJCWr (ORCPT ); Wed, 9 Feb 2022 21:22:47 -0500 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4628822BC7 for ; Wed, 9 Feb 2022 18:22:49 -0800 (PST) Received: from pps.filterd (m0109334.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 219NYHi5029395 for ; Wed, 9 Feb 2022 16:36:57 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : subject : date : message-id : references : in-reply-to : content-type : content-transfer-encoding : mime-version; s=facebook; bh=hTwRg7irlzBwnuu6oczWfaLxt3YkTybNqeR7ha9yFXQ=; b=C+VHB9yxRccOxTvt+QcQGjJFB1LuH88B2wcA+gLPEi276YJLFtHlwuBCZCwmOFqfM+/U 9iwLRHry8MfmEjQUy79INuCBOMER05wbaJc2mLNIpClsTo1vJO3AvnEVFt9XZDeOkeeW 6H0FJ4jCSijGtTuZeg0IeR7ZAtHxAwlF0Ew= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3e4e8nvnfh-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 09 Feb 2022 16:36:57 -0800 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (100.104.98.9) by o365-in.thefacebook.com (100.104.94.199) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Wed, 9 Feb 2022 16:36:56 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CUHsT+dossyHYYAMquWQNRVFuGLPVytKzelFQfUIl78ABpC0hPwhcumpC+7nor1fBKlI6p3oIPBuZQuP8yUC5SaxL4Jn+VRXyXsqZVuhwNOh/yWFGaFxNvn6pUgRg18WmZ0KrzzEQsTKCJrnTwOsnBvOLYDszYc4NTKEqtCxT6LMxGuBblVVtG2KVBeY7NcdcOVjSB/OSy215rfAPdT/qKwvod79QkMGZwlHzsnN1O5hdJPOuJXZ79KM510nKyJ8nuMkHZAWhzUQBmc74l2INsI6Sum/jBSbMJOTHnN2clsJW2cut+h7N0X+JmyUnS31r6n3rYwgKvdW+AtojWT+SQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=hTwRg7irlzBwnuu6oczWfaLxt3YkTybNqeR7ha9yFXQ=; b=XXzK1c1z+9ski4RGMpZNoZSyFRg+1/H/Xzk9cAzIGhFOLEFREQVdMafqgmbI9X4vZ0gBsEZCS2Nmd/dWBLcpFbG59FgtFhydbNkl7Zm35HBz7Vui1SB+VKg6vvrAOSX2t6LNtseHv2RPEPbfYYEZPqAidMZ3E/5aNu32jJfBNkWmNRDNJwF3YSAG8hjQNvaQ9zHkKq78/o0TMqPRcWdLTFe7zK976uZhDdAL8/2LwI0uJffPW+FB+LFYirB3mV2AwUem17J1BhD56dn3x9j27pLpZzJi9Bbid7c/osJkcgJAFAcrju4j+usmQ/d/4pmpZdsULWbv3RKLLoXyR1JomA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none Received: from SJ0PR15MB5154.namprd15.prod.outlook.com (2603:10b6:a03:423::6) by CY4PR15MB1590.namprd15.prod.outlook.com (2603:10b6:903:f5::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4975.11; Thu, 10 Feb 2022 00:36:55 +0000 Received: from SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::f1fc:6c73:10d4:1098]) by SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::f1fc:6c73:10d4:1098%6]) with mapi id 15.20.4951.019; Thu, 10 Feb 2022 00:36:55 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next v1 1/3] libbpf: btf_dump can produce explicitly sized ints Thread-Topic: [PATCH bpf-next v1 1/3] libbpf: btf_dump can produce explicitly sized ints Thread-Index: AQHYHhZNW2xoTni6OUuR9sTJr9fafg== Date: Thu, 10 Feb 2022 00:36:55 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 006ddca7-fa3c-42b7-4ac6-08d9ec2d7063 x-ms-traffictypediagnostic: CY4PR15MB1590:EE_ x-microsoft-antispam-prvs: x-fb-source: Internal x-ms-oob-tlc-oobclassifiers: OLM:849; x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 2qjwxMhxowDK4avkDVKmiPyw9V76q2n4WimiM6JfgEixSj/RTt2q25y7eQdLZOrXZN5MiTlFDpOuQH3JpOnKPmsqJX3OyUykzijooqAp3MW+rnpEIES9RameNEpTI6P7ROmzAD+mBcl5Nv2l2ckd0q7jR61uJYVQcYs7GAL4OvsK/QFmp5//xZCmtgcmvSyqesSQ5m/ob+bn/zQuAXDVQCQ9rp6baAFhKMKDMaWSkKbmFL8p0T8xFj4qpUHjtLwQCtC0ljXecXELnapUwD4RO7c0RYsYgzzyCN2OMAtZeL6SwOxzwAp7rQdUOTZ+SNSzJmV/RM3nKo/+edAVWQyQPW3YlizNMZARHocGyU7STrdlqvrHaBs+TT/FlFO6TccEzOPYHhQnBoXTPhXLrh37DiGgtwDqLBZhC2Co5ZQG2nR50XqKZrcGwuGxSeg39y7vUoQFfeKyFSEDNqaK0OxTNKfKwsD9puC8VVC3Xkf/jKrHmYj1uXS2HRC6hx141ewwRyA6t+QfdN8yjUXuZqJN9n8CooGeFweZYhEQNM/5bUDy1WW0MxHyqryA+Dp8f6myhEfrgkFMPPpzm2fmvhGJvckiplS/tO5+MZde0h19uW1rzmeiEjYt1qlk1ZSW5OVeWTjv7BCOzoGqvAW/aQ6hRSJClxB03ZtrpjrZ0TkEMIFNgswPePCKh1hZ2wNNWgm3n/P0MQQuO/gOZyV3Fsw3OA== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR15MB5154.namprd15.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(38070700005)(316002)(8936002)(5660300002)(110136005)(2906002)(6512007)(6506007)(122000001)(508600001)(26005)(2616005)(186003)(6486002)(86362001)(76116006)(64756008)(66556008)(66476007)(66446008)(66946007)(71200400001)(8676002)(36756003)(38100700002)(83380400001);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?acnSkyNBW0H7uwbdc4G3Qae?= =?iso-8859-1?q?qCoQFRdlifzmi77XwbZ8AH1PQX4lZ0v7iA/t1rLABiG73B581sAdnhv7zvg5?= =?iso-8859-1?q?TrgajdWM/92TFIzACBkF2hILPlhhSXlgpYo+dj7HhNzkNoorlzIpOKBHLgNB?= =?iso-8859-1?q?nbOf2HvrYm3trDHt7XhB9Wo8SUIz5m2jvtVR0RDCwpEillrXmx6SCL80/fKq?= =?iso-8859-1?q?kHN+Ht/rPw6AM50yOZXcRvS2ZPYxu/WPA3Be/LxAXpU7oTrLRYkBXgEuE4hf?= =?iso-8859-1?q?wtFcTIhxkoXIRsmvWR6NnCGUNRB2PGbOzLE9eeQ3WwA8Evq1XHvjK+8pJbjB?= =?iso-8859-1?q?5I0/J/Ys65BkAb7CPTB7TwqHTo+aaM24zOH6nBI2SjbcOH5pi91yfSK+EmZx?= =?iso-8859-1?q?4MLRRRz3g0kQLDmD897MrhUntaY0VhCvpukoJd79lgwjycOQ/SwXTq0TMTjb?= =?iso-8859-1?q?BzR5hLo4tqltqVNyLh1/HPuR0FLdhJMXOUOeq76zkPnl965lop/vYEkAYXSM?= =?iso-8859-1?q?D8ardX5Fgdp+0q8hNpB7ncykTWqn1+9/2Dl9aBJjxw3TJvsezXnuV8p+M/au?= =?iso-8859-1?q?pwXFHtt+9Vk2eH7jjrsu4T3D5ZiFxS/73E8U2YCRqqXZzSg/W1TbZfB9lDe4?= =?iso-8859-1?q?Z2GvrChBY2Z0Vc0qJBDccXdXZYsfe6wmv+IckDfx+20JwjqZByAmNl8RVnW8?= =?iso-8859-1?q?Ay1l65Qq2J8mowSAwp0N3gpEojjB4Af/lod95Qesxec4PJlTtC76h9d6EMIc?= =?iso-8859-1?q?6ApsNpZyxgUMcE4cf0nVBbQqvH7XbL8zALnSUAiKUWO32aEuL6/fv1ce/4sZ?= =?iso-8859-1?q?LnQMkFEjGA6E4DDUxMMv6Wv/59BhZlXu34se1lxQoai0TvlkDNAl0Npjtmj7?= =?iso-8859-1?q?EOo/VhpThcmcROAUE/jWAwYaUns3oNF3AmUDT0Jv+dQ2AOw7Y94i260X8zI4?= =?iso-8859-1?q?MyEnDtC6a4q5521voHoDcHjKUcIsdVGQv+Qj+b3mhGDmqS3Qre+xRL/jCHkn?= =?iso-8859-1?q?9q75GF5Xftp6CcGesNjBvhQurBLf9DHfPbJd2qhzypkEiw4jVS4ZCDOZXPbd?= =?iso-8859-1?q?HSw/X9x3wL3yy8KmOs/lbuLNldesQYd5FoCImD6ZycHOhO2BdBqR/VkSrATQ?= =?iso-8859-1?q?9qcvre9Z6tuFvhqHK0PGW0AC5xxRc4V8HB71Ad8lrv28BNoYuL+n3N0UcM8v?= =?iso-8859-1?q?UofTLxqCOmqYauumspyY0wrf+nxxu2drBNIBEdDJ5XytXb30bG0YkS6EzElL?= =?iso-8859-1?q?3OMRJWGrtZxOgmNPOl/Z1XiN1snAG815sd0MFQ+1lr9TLf4XXR4cbjjU6ai1?= =?iso-8859-1?q?eA8qJH51QOH1EBesSBr7YkDWAuewokmfK/+b1iuAlBDvvLmSEvZRqGiU5q4C?= =?iso-8859-1?q?OYEBhAn/MN0WPgLQjknaW9WXZ1F+w3Bb/mKtBpcZCLQ9Q+zL3O1qbD7ELmmv?= =?iso-8859-1?q?HDeDEUuaGwYMI8zotGQzeEZJXyoHKSb3LX1zoG9EukNrnosdfRdb00h2mX/M?= =?iso-8859-1?q?Zl1ScbhSZcJNcXF4B/6Gjz81cKZvn9a7rVQvm7sSJEnnqkEFd0ilbAPseYMz?= =?iso-8859-1?q?nKrMsT3E5cV6NGk3A4kgSiKLNQCJZUPcrRbCNo8diJkxt2rDJEr5CaRE=3D?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SJ0PR15MB5154.namprd15.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 006ddca7-fa3c-42b7-4ac6-08d9ec2d7063 X-MS-Exchange-CrossTenant-originalarrivaltime: 10 Feb 2022 00:36:55.1232 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: 5OLnpjXTiSEU576px7bmGGYcPDWtLFgBxMDp+YGmkrp9Fvp+7FniouRGp0T/EdLj X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR15MB1590 X-OriginatorOrg: fb.com X-Proofpoint-GUID: hKnmtiOXKbS-_uBgSFGPEYMTFtjGQpN8 X-Proofpoint-ORIG-GUID: hKnmtiOXKbS-_uBgSFGPEYMTFtjGQpN8 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.816,Hydra:6.0.425,FMLib:17.11.62.513 definitions=2022-02-09_12,2022-02-09_01,2021-12-02_01 X-Proofpoint-Spam-Details: rule=fb_outbound_notspam policy=fb_outbound score=0 suspectscore=0 mlxlogscore=999 lowpriorityscore=0 malwarescore=0 phishscore=0 impostorscore=0 mlxscore=0 adultscore=0 bulkscore=0 priorityscore=1501 clxscore=1015 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202100001 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net When emitting type declations, btf_dump can now optionally rename int types (including typedefs) to standard types with explicit sizes. Types like pid_t get renamed but types like __u32, char, and _Bool are left alone to preserve cast semantics in as many pre-existing programs as possible. This option is useful when generating data structures on a system where types may differ due to arch differences or just userspace and bpf program disagreeing on the definition of a typedef. Signed-off-by: Delyan Kratunov --- tools/lib/bpf/btf.h | 4 +- tools/lib/bpf/btf_dump.c | 80 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 2 deletions(-) -- 2.34.1 diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index 951ac7475794..dbd41bf93b13 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -347,9 +347,11 @@ struct btf_dump_emit_type_decl_opts { int indent_level; /* strip all the const/volatile/restrict mods */ bool strip_mods; + /* normalize int fields to (u)?int(16|32|64)_t types */ + bool sizedints; size_t :0; }; -#define btf_dump_emit_type_decl_opts__last_field strip_mods +#define btf_dump_emit_type_decl_opts__last_field sizedints LIBBPF_API int btf_dump__emit_type_decl(struct btf_dump *d, __u32 id, diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c index 07ebe70d3a30..56bafacf1cbd 100644 --- a/tools/lib/bpf/btf_dump.c +++ b/tools/lib/bpf/btf_dump.c @@ -81,6 +81,7 @@ struct btf_dump { void *cb_ctx; int ptr_sz; bool strip_mods; + bool sizedints; bool skip_anon_defs; int last_id; @@ -1130,7 +1131,9 @@ int btf_dump__emit_type_decl(struct btf_dump *d, __u32 id, fname = OPTS_GET(opts, field_name, ""); lvl = OPTS_GET(opts, indent_level, 0); d->strip_mods = OPTS_GET(opts, strip_mods, false); + d->sizedints = OPTS_GET(opts, sizedints, false); btf_dump_emit_type_decl(d, id, fname, lvl); + d->sizedints = false; d->strip_mods = false; return 0; } @@ -1263,6 +1266,34 @@ static void btf_dump_emit_name(const struct btf_dump *d, btf_dump_printf(d, "%s%s", separate ? " " : "", name); } +/* Encode custom heurstics to find char types since BTF_INT_CHAR is never set. */ +static bool btf_is_char(const struct btf_dump *d, const struct btf_type *t) +{ + return btf_is_int(t) && + t->size == 1 && + strcmp(btf_name_of(d, t->name_off), "char") == 0; +} + +static bool btf_is_bool(const struct btf_type *t) +{ + return btf_is_int(t) && (btf_int_encoding(t) & BTF_INT_BOOL); +} + +/* returns true if type is of the '__[su](8|16|32|64)' type */ +static bool btf_is_kernel_sizedint(const struct btf_dump *d, const struct btf_type *t) +{ + const char *name = btf_name_of(d, t->name_off); + + return strcmp(name, "__s8") == 0 || + strcmp(name, "__u8") == 0 || + strcmp(name, "__s16") == 0 || + strcmp(name, "__u16") == 0 || + strcmp(name, "__s32") == 0 || + strcmp(name, "__u32") == 0 || + strcmp(name, "__s64") == 0 || + strcmp(name, "__u64") == 0; +} + static void btf_dump_emit_type_chain(struct btf_dump *d, struct id_stack *decls, const char *fname, int lvl) @@ -1277,10 +1308,12 @@ static void btf_dump_emit_type_chain(struct btf_dump *d, * don't want to prepend space for that last pointer. */ bool last_was_ptr = true; - const struct btf_type *t; + const struct btf_type *t, *rest; const char *name; __u16 kind; __u32 id; + __u8 intenc; + int restypeid; while (decls->cnt) { id = decls->ids[--decls->cnt]; @@ -1295,8 +1328,51 @@ static void btf_dump_emit_type_chain(struct btf_dump *d, t = btf__type_by_id(d->btf, id); kind = btf_kind(t); + /* If we're asked to produce stdint declarations, we need + * to only do that in the following cases: + * - int types other than char and _Bool + * - typedefs to int types (including char and _Bool) except + * kernel types like __s16/__u32/etc. + * + * If a typedef resolves to char or _Bool, we do want to use + * the resolved type instead of the stdint types (i.e. char + * instead of int8_t) because the stdint types are explicitly + * signed/unsigned, which affects pointer casts. + * + * If the typedef is of the __s32 variety, we leave it as-is + * due to incompatibilities in e.g. s64 vs int64_t definitions + * (one is `long long` on x86_64 and the other is not). + * + * Unfortunately, the BTF type info never includes BTF_INT_CHAR, + * so we use a size comparison to avoid chars and + * BTF_INT_BOOL to avoid bools. + */ + if (d->sizedints && kind == BTF_KIND_TYPEDEF && + !btf_is_kernel_sizedint(d, t)) { + restypeid = btf__resolve_type(d->btf, id); + if (restypeid >= 0) { + rest = btf__type_by_id(d->btf, restypeid); + if (rest && btf_is_int(rest)) { + t = rest; + kind = btf_kind(rest); + } + } + } + switch (kind) { case BTF_KIND_INT: + btf_dump_emit_mods(d, decls); + if (d->sizedints && !btf_is_bool(t) && !btf_is_char(d, t)) { + intenc = btf_int_encoding(t); + btf_dump_printf(d, + intenc & BTF_INT_SIGNED ? + "int%d_t" : "uint%d_t", + t->size * 8); + } else { + name = btf_name_of(d, t->name_off); + btf_dump_printf(d, "%s", name); + } + break; case BTF_KIND_FLOAT: btf_dump_emit_mods(d, decls); name = btf_name_of(d, t->name_off); @@ -1469,7 +1545,9 @@ static void btf_dump_emit_type_cast(struct btf_dump *d, __u32 id, d->skip_anon_defs = true; d->strip_mods = true; + d->sizedints = true; btf_dump_emit_type_decl(d, id, "", 0); + d->sizedints = false; d->strip_mods = false; d->skip_anon_defs = false;