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; From patchwork Thu Feb 10 00:36:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12741092 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 61FCDC433EF for ; Thu, 10 Feb 2022 01:22:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231512AbiBJBWr (ORCPT ); Wed, 9 Feb 2022 20:22:47 -0500 Received: from gmail-smtp-in.l.google.com ([23.128.96.19]:42126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231540AbiBJBWl (ORCPT ); Wed, 9 Feb 2022 20:22:41 -0500 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F58E1EC47 for ; Wed, 9 Feb 2022 17:22:43 -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 219NYHi3029395 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=R4upJ/IquiANQLCltuWGGy6R+6bn4TGeOOM0s2ESvlI=; b=S3bEXS0YIC0gtKu8HGdQvnaMH2BcbotPSZSEnvTBNTKWiTBVxhOfSNb+pRE89OtGonfZ EnyprmrHv45cIN3J+SyTMXscRVb5w2zxkrHSjxzfjOjJub9sih3hhcrvEQdNQiTS5eJG 0Oq/A45YslDhHliA/IJTZVCsEKRkKKe4dcQ= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3e4e8nvnfh-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 09 Feb 2022 16:36:56 -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=kvbck3W3OrA1JbEzws4ujK9LBmRfsv+/FkQoyKclDGIN5lOdgnV405grqHBmXwc1XjcBvInv37hg24VlRJBLc5u9N91Ju5K4TWxUPaWQhUbXQdKmFA3BxCtqbsYqd6ijoSuk3yxZezvdcL2WYRFzul3F5cOKD2By4vzV5EjejMsoTCCZuICSaVlC+Yj1vp0bkpCf44i7CmEKUqIhjgLtf22r1id1ZjlyT1XI4OrMYALlUqSRPhrvOv+D/281QR8OO86Hn3/Ou6OgdgLeD6Ce2T0q6pgSxLJxwsH7ncxn2fXhkwEZuNHuJur3g5q/b7dLJ6c4xsxYOcXI1FJll5RZEg== 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=7khCYI7ljTUwy2g6JN04oaknrH1FRqkmEHyG8PhDaks=; b=eULv4WZ3AxmwLv8nGEJus2jj4dVc30BwQiaF8STmhoVQPt2L8hAajRVgGCbMloQGrv0al/m7h1RYho5heSWYnpyg9gojI82elcgWco1dGFthjyJOniArBwps1o4n7W7s5+/qNJ0KIwB/Kqduq7xw8R3iDJuldmEsgNGtNaf/91WQrG2abYNR4oJRZqkU4P8Q+mfcyCjLph/fzB0Zu/LCsjjfDV9UvCGC7x61XZM+0IumdFGVBBav2pZgZWFKKnSxniNveRlWcDKOSceVQCMhEt+khvkzGyCAnnGWY2ZvynwDi6oRrQ9LagRHKJixO4CObByWG9+Jd7wynFL3wBoiYw== 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:54 +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:54 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next v1 2/3] bpftool: skeleton uses explicitly sized ints Thread-Topic: [PATCH bpf-next v1 2/3] bpftool: skeleton uses explicitly sized ints Thread-Index: AQHYHhZN/e7JuZr7akC7vD+AXIiWjA== Date: Thu, 10 Feb 2022 00:36:54 +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: 65ccafdc-641e-4f2a-9f16-08d9ec2d6fd6 x-ms-traffictypediagnostic: CY4PR15MB1590:EE_ x-microsoft-antispam-prvs: x-fb-source: Internal x-ms-oob-tlc-oobclassifiers: OLM:101; x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: Bz2Kao0FB4pO6u5aNaF+PDuHRoyCIO5ohkwjd8fXopDm2wNntrqMhLZn2IbAFXmLKsklxtHaVuc0qdpB7Ys2/549ZaxBRc2FRG/Um6l/EyG/cGVqCg+eum/eJO60z4ssSHU8MwQFWrYvn+YQnj9kp43wBUoLUleB92nVV7n32Hn2SQIef7ewV4tRHf+RtiHJFIpgxSQ5Ib/jz1HQuAE1Y0RyoRoGY9hOUOxtU/ud+8T743YNWsJo8N1P4XIhWRA9l8olsDM4fTrPRVJ1rySBBD3MFyL5ODbiMwqCC/DNUv07dZnfGHmkKYKSRqV13+AfNf/rOhZeYV+3stMfAtmj30jLr+NqMp6FVQASYyTj+cfIGqSIQ3w2Cu8Fcv6FphXayIL5mbVMGVAfNc+BjcPYWvJyvyvBYS/BMcpjulkuZjQAth1iXgzsQlKIkmSIWm36mf4ZuAvbL2/LJiPjVUkL1djddJDaKJ2ItptZv/yk+TQtTwRWLScYJXL4AmXTmuiru7YUHKUOr3BbJQg9gu2lfZN7pzJROcJLkWftx2xIVy/H6RYpdMCYyTHLszzmbRApHntrtCwSti/jerBIH+I/HVbLxM7YH2V0a6Tt0bhPQ/WKT2w0ffe5DMoMi/yq72LnMMvLhgvxXxvlp3BcSSutZRejyN6FNuE7K0IA8tmSq/ZWMn2AJjTEFlomb62yccpdVLJ4XVtvQSkGU0zqiz1wOqY5viekuvvPzRBSl9rupxEv1oLx/O7TfZdxInyrD2mExtENDDV4ZGzpWxYaMN6LrLjo23ZADHJQw7U+/OFTvrY/eY/FWdki6D4UipGMLxdoPKXGbn6GzcLi2KQkbIcXQA== 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)(966005)(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?eWCnabsD9Odk5SDd/HutxxI?= =?iso-8859-1?q?WxYTKMWcurTjMBIxGn8DE8Z5556ODDemp78e08rg/r+QwcxMCFOXPyzHaYns?= =?iso-8859-1?q?uML2Yf9n1S/xARy5UoOfncJfmGZN742Am+I2cYXufdFVDuOr2lg3muXd8EeW?= =?iso-8859-1?q?7aL/+cAt0jsJxe91BRT32yinjYyoTdejqqpVpRpaBrroOo96OjRVedrsL/cK?= =?iso-8859-1?q?oeFo73+e6WJCCfM2IbxL1+w/hhrx8TkOTX+Hgh7XR+80OmKRhaf2Kk5Gq8Eu?= =?iso-8859-1?q?R88VpFzMC7A3DkFad/QT53SLfWHdfZxeX4uNEK2jcZ+Ce8cFe7Y0zG30g/Uo?= =?iso-8859-1?q?hcFPoQXVJMVjXoaI9qvyjUf6KuTFOAmmPPF5Ngi7u8GlLRziNj74HiA5vaDW?= =?iso-8859-1?q?4XsADGORI1Pk24h/IKpVLDfeOq3lRL3Kpjk/h6GsgsLQ0TyIK7TvLHAEF1WG?= =?iso-8859-1?q?icg3zUU+Y5s/CAvwq8NKfep4LyIZhh+byHWnBE0+vVRfe4KhbwPCSaGsHWup?= =?iso-8859-1?q?SNt0WX1+gWBnnqE8n2AGMQ9nhalzDE8UyA5tgskarzwsCkZW4YyUW0lo9Lmn?= =?iso-8859-1?q?JaBVOJooge25qFP2P+gtyQFJWqaUNGsoSqaRVFe+9aJvsAbpNVuIC5pi8aLx?= =?iso-8859-1?q?cTUNPJj/3u96wb5/p/gjbtfV61ShUIvIqoNN2B1w2gyuM7+3aAWhXS0D6W7/?= =?iso-8859-1?q?eRaRDxJSM121eAFUHWvJ5yzIyzkbRzyD5jVLBr1acSAWSOuIUm+9EM3moOCc?= =?iso-8859-1?q?FONbYaPRtl7xrNNz3oOPN6Fi7G79NZgX7eC+2l4fEnXV+v1GShKfQDTUxRGW?= =?iso-8859-1?q?lzoA2+8HCyan9J9RppcTgHTExMqIsU+AUhlNai3jMzD1WIwT+o40FfA0Dn4i?= =?iso-8859-1?q?Q5xmHQJPyEiltDFZy8k3qy5iK89XMHiMi6RFoTMJqEBuzgTjbCm+MbEHwTYs?= =?iso-8859-1?q?yuuCNEvzO11PNEjMpUDH9OjE3H3Cwnt8+B8nBvwmzZA5ePvqvOk4jlETaupt?= =?iso-8859-1?q?68Zn5shpuJJNvUx5N6ntjhQHSJ7QvA2HF7NCbIN3Ux63WyfNLGOTWbpHy1B3?= =?iso-8859-1?q?htlFRZSO2jt2xFkNAyPyzAMpGpGDegBuZmmQoKXFAciHlm8yiJECx6IVCM5A?= =?iso-8859-1?q?JIYcTHbunXnPvvM2RPy5JQ1y5u821i7BOltikvWvdrlrI2tB6YSkZWsyryZl?= =?iso-8859-1?q?qVcIHAQuFIU8OaRl6wmHFY4hIAKzDaZT3cDwC7KxYLwHnBC0529q8zz6V++t?= =?iso-8859-1?q?khujJtb2S/cDtZcv+6wucjWBpq+7Ql4+RlmJpcg4s84bmCKU1lWlDS26Mlu4?= =?iso-8859-1?q?nNEYBDoHlbWf67zIOCh+kZXxpN7G/To1j2RzcA2dU6hYI5BvXloRILkrJTmf?= =?iso-8859-1?q?pVu/nlaoxas9hv9f+CYFeAw6J6pBsigKlGqMshnnCnpVQjXpCT4dXYheGTOS?= =?iso-8859-1?q?LVzrYT1pAuGnzLURVkmUptjkAi5D+bHJ4TmkTl9nxxUHVuX55FSl87RkFdfU?= =?iso-8859-1?q?ZktK+xbu/l8wRkkIwWl+mpMK6vntaGbcisqw/q/J5t/LuRFLgIpUrXYorSHj?= =?iso-8859-1?q?i3cJpZ4C1MScqgHRYDXkiAukLwuEu35sHYH6fqD8gVrksDD4rgkxZiSs=3D?= X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SJ0PR15MB5154.namprd15.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 65ccafdc-641e-4f2a-9f16-08d9ec2d6fd6 X-MS-Exchange-CrossTenant-originalarrivaltime: 10 Feb 2022 00:36:54.2562 (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: Rz4yeClBEvfPm8uVYSU1meZWG/r4ldwHXiP7JIKrAyiyxE9Hiip29v4TgqRF5MYv X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR15MB1590 X-OriginatorOrg: fb.com X-Proofpoint-GUID: 4Wm11nyt7NoezmuKiSwv7qdx7puD6h7L X-Proofpoint-ORIG-GUID: 4Wm11nyt7NoezmuKiSwv7qdx7puD6h7L X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 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 As reported in [0] and [1], kernel and userspace can sometimes disagree on the definition of a typedef (in particular, the size). This leads to trouble when userspace maps the memory of a bpf program and reads/writes to it assuming a different memory layout. This commit now uses the libbpf sized ints logic when emitting the skeleton. This resolves int types to int32_t-like equivalents and ensures that typedefs are not just emitted verbatim. The drive-by selftest changes fix format specifier issues due to the definitions of [us]64 and (u)int64_t differing in how many longs they use (long long int vs long int on x86_64). [0]: https://github.com/iovisor/bcc/pull/3777 [1]: Closes: https://github.com/libbpf/libbpf/issues/433 Signed-off-by: Delyan Kratunov --- tools/bpf/bpftool/gen.c | 3 +++ .../testing/selftests/bpf/prog_tests/skeleton.c | 16 ++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) -- 2.34.1 diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index eacfc6a2060d..18c3f755ad88 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -146,6 +146,7 @@ static int codegen_datasec_def(struct bpf_object *obj, .field_name = var_ident, .indent_level = 2, .strip_mods = strip_mods, + .sizedints = true, ); int need_off = sec_var->offset, align_off, align; __u32 var_type_id = var->type; @@ -751,6 +752,7 @@ static int do_skeleton(int argc, char **argv) #ifndef %2$s \n\ #define %2$s \n\ \n\ + #include \n\ #include \n\ #include \n\ #include \n\ @@ -770,6 +772,7 @@ static int do_skeleton(int argc, char **argv) #define %2$s \n\ \n\ #include \n\ + #include \n\ #include \n\ #include \n\ \n\ diff --git a/tools/testing/selftests/bpf/prog_tests/skeleton.c b/tools/testing/selftests/bpf/prog_tests/skeleton.c index 180afd632f4c..9894e1b39211 100644 --- a/tools/testing/selftests/bpf/prog_tests/skeleton.c +++ b/tools/testing/selftests/bpf/prog_tests/skeleton.c @@ -43,13 +43,13 @@ void test_skeleton(void) /* validate values are pre-initialized correctly */ CHECK(data->in1 != -1, "in1", "got %d != exp %d\n", data->in1, -1); CHECK(data->out1 != -1, "out1", "got %d != exp %d\n", data->out1, -1); - CHECK(data->in2 != -1, "in2", "got %lld != exp %lld\n", data->in2, -1LL); - CHECK(data->out2 != -1, "out2", "got %lld != exp %lld\n", data->out2, -1LL); + CHECK(data->in2 != -1, "in2", "got %"PRId64" != exp %lld\n", data->in2, -1LL); + CHECK(data->out2 != -1, "out2", "got %"PRId64" != exp %lld\n", data->out2, -1LL); CHECK(bss->in3 != 0, "in3", "got %d != exp %d\n", bss->in3, 0); CHECK(bss->out3 != 0, "out3", "got %d != exp %d\n", bss->out3, 0); - CHECK(bss->in4 != 0, "in4", "got %lld != exp %lld\n", bss->in4, 0LL); - CHECK(bss->out4 != 0, "out4", "got %lld != exp %lld\n", bss->out4, 0LL); + CHECK(bss->in4 != 0, "in4", "got %"PRId64" != exp %lld\n", bss->in4, 0LL); + CHECK(bss->out4 != 0, "out4", "got %"PRId64" != exp %lld\n", bss->out4, 0LL); CHECK(rodata->in.in6 != 0, "in6", "got %d != exp %d\n", rodata->in.in6, 0); CHECK(bss->out6 != 0, "out6", "got %d != exp %d\n", bss->out6, 0); @@ -77,9 +77,9 @@ void test_skeleton(void) /* validate pre-setup values are still there */ CHECK(data->in1 != 10, "in1", "got %d != exp %d\n", data->in1, 10); - CHECK(data->in2 != 11, "in2", "got %lld != exp %lld\n", data->in2, 11LL); + CHECK(data->in2 != 11, "in2", "got %"PRId64" != exp %lld\n", data->in2, 11LL); CHECK(bss->in3 != 12, "in3", "got %d != exp %d\n", bss->in3, 12); - CHECK(bss->in4 != 13, "in4", "got %lld != exp %lld\n", bss->in4, 13LL); + CHECK(bss->in4 != 13, "in4", "got %"PRId64" != exp %lld\n", bss->in4, 13LL); CHECK(rodata->in.in6 != 14, "in6", "got %d != exp %d\n", rodata->in.in6, 14); ASSERT_EQ(rodata_dyn->in_dynarr_sz, 4, "in_dynarr_sz"); @@ -105,9 +105,9 @@ void test_skeleton(void) usleep(1); CHECK(data->out1 != 1, "res1", "got %d != exp %d\n", data->out1, 1); - CHECK(data->out2 != 2, "res2", "got %lld != exp %d\n", data->out2, 2); + CHECK(data->out2 != 2, "res2", "got %"PRId64" != exp %d\n", data->out2, 2); CHECK(bss->out3 != 3, "res3", "got %d != exp %d\n", (int)bss->out3, 3); - CHECK(bss->out4 != 4, "res4", "got %lld != exp %d\n", bss->out4, 4); + CHECK(bss->out4 != 4, "res4", "got %"PRId64" != exp %d\n", bss->out4, 4); CHECK(bss->out5.a != 5, "res5", "got %d != exp %d\n", bss->out5.a, 5); CHECK(bss->out5.b != 6, "res6", "got %lld != exp %d\n", bss->out5.b, 6); CHECK(bss->out6 != 14, "res7", "got %d != exp %d\n", bss->out6, 14); From patchwork Thu Feb 10 00:36:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12741164 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 EDBD0C433FE for ; Thu, 10 Feb 2022 02:02:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232986AbiBJCCq (ORCPT ); Wed, 9 Feb 2022 21:02:46 -0500 Received: from gmail-smtp-in.l.google.com ([23.128.96.19]:60092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234465AbiBJCAR (ORCPT ); Wed, 9 Feb 2022 21:00:17 -0500 X-Greylist: delayed 1854 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Wed, 09 Feb 2022 17:57:53 PST Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 705B61121 for ; Wed, 9 Feb 2022 17:57:53 -0800 (PST) Received: from pps.filterd (m0148460.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 219NYMNT000469 for ; Wed, 9 Feb 2022 16:36:56 -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=mQaiR1N/vY9DqHIWA9TgCZG6ThMzPYpvSPSbS1Zx1BM=; b=SDRtnIlrCyN4b09TCuvSDrmoPelp7SEG+B3c9AJQcvL9C/HyeRkXiEivYMrAZ4dSgNoV 1wlZRWwUvh4CBOULcP1dRrt+9sQpsvtgXWH3z9MsIKSdWvk7KeVuvsIH6+0cJ7eotYWi 8RpVHW+srUUo6CT0pnGAoAw2xRpeu4fw4Ec= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3e3y3s9ke3-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 09 Feb 2022 16:36:56 -0800 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (100.104.31.183) by o365-in.thefacebook.com (100.104.35.174) 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:54 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=X8zf3gek4KRwX5QJrHFGJGlNE5gI4e7bVLmSWt5/3Ntm3eukcvscV0b+3mSq9yql3HwmfVYA5usfdua98tCxdTjawPbcXgK1Lw3PHXlt5wTmRm12zifFG55g32MlV2Ee1ODzJl5q3vGwJxkMgt10G+uu1Z7oYXyjCwzNImdPDp02V9dUiGLXOru3tsJ1tmyZj9CcbRTT19ANKfQSqZSb96H8Y+tDiNO1XeS1g+wgYBehxJnc+7PpdReXf3O40NyRozXGMheKZrPDiWd7iBRAWn8Mxda4qo0luwLyQCEEC8e6rD6/Qw67snz3OIXnALligx017DcmoY1b6vVcAhuHzw== 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=mQaiR1N/vY9DqHIWA9TgCZG6ThMzPYpvSPSbS1Zx1BM=; b=JtpBxuTPlXXNwc3OuoVrUfB543/Z7nUU3n5bUCOJV1BIY4WTey5qDNBaZUTeyBzUgbkJRG22MwKmNZqxVLRCkAaAynqEr/E/A78Ed9/Wu7yXFon9Xs6bTQZa9nNVWCssp3Htkje4O2YzMCJzrTjdS2VI7ZC9DqC03lmn9nhO26CXkSb5LJP3z9jbmE7glLXa1NBhnCjW0MjR7Kei9BLsa1OE6bcXZoHOscnLgLSmbhC6q0WEzhjomsAzSN/LjBbotUs+uvmXMC7iU0Nnc+YmvMrXb1z7v6UYnzGhGZZD6fxltiAfT86XV/OBBU5B+Mk+T8is8cbXRq4hp175jQG56Q== 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:53 +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:53 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next v1 3/3] selftests/bpf: add test case for userspace and bpf type size mismatch Thread-Topic: [PATCH bpf-next v1 3/3] selftests/bpf: add test case for userspace and bpf type size mismatch Thread-Index: AQHYHhZMs1nTeF9OrEOrzXLcBPCJjQ== Date: Thu, 10 Feb 2022 00:36:53 +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: 874bc4d2-1754-42b2-e377-08d9ec2d6f3b x-ms-traffictypediagnostic: CY4PR15MB1590:EE_ x-microsoft-antispam-prvs: x-fb-source: Internal x-ms-oob-tlc-oobclassifiers: OLM:6790; x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: wDKqjDxlHlKAKwLLvp9HtjtchOnqK3jRWba+NJWA7iwIxj4a2KZ/+Z1SgGv4Lwtxbm3hR2/tlcQHSwh1JDETQ++6NJXqA+4PDOOYf+sBxFCRZHqy9JSAisFHm0fJO3zzQAkGqpfcVt4yXbNGGWrFMLBg0LpVwFsftAAwIGySeVV+63QoocJzRkIDZKdSjUSDnUSkCjPcFn8DS7dpHtyASA1a432WWA6xj3T8g9KJ29UDlsIi4hz2DdtMaLoMmY8pl8zHJS9vwclR1ZbGZ1BPXnvAXTTj9LcMO57+GOujGRimTCtLx7jiB93GseXHWwblsKreNn3Z2ZUD0gE+HDJhU4+I25TPH49AgOrsyIKTeD7mYf2F+LRl8lAQWrBD7mdT/C/aoCNbu0rPbXIiWFn7u1r5dD/h+EbJR3XCkKFYA402oJtrZIwerL76NzSZsrgW6XyiQBHan02yOiTJ7uXPNMj4D3YNvu7WWGW+IdN2S9R0xahFfsDzKFZdqsUGLHqDzP62zigcR0FfksVEbjGi2qA6Ip8TWycNlXiDe+FEz7C2L9tPmHvXX6V/PPC5GpSy2NP7HWDqQbGQwMxtoeTZhD2pEOAfmzAjEm3fbd5rjG9oTYv83hIZ8U11/FwzO+cHptXO0VvAjLFwZu3NP+18P3T1uROcCsgBHPz7axW/XoNGkaDUDWV04yMY+K3FYJxnyXcXZRw2sDATsZ4Mma0EpA== 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?imZMw/GJgDCC8XeM7zshQJw?= =?iso-8859-1?q?XzVQGrdKr/np9kzvM7BZNZBRwAKvZzkF7Y44iypweOskL1MjbzlHqxbbr7GM?= =?iso-8859-1?q?3tAJdAdD3ti1q8/jnQEj3wfcgERdJeKwdkhWzFG7zWPXhIknKk4WhNJvWmlu?= =?iso-8859-1?q?MJG8gXcKFpP2MleJ1KVm3igvarK8UjROv4xwiMYHgu34DPWmvTp9rbNZYbdm?= =?iso-8859-1?q?oO4AsjcsQCcdjP2YuarP5S1sqbfUej7Uvjx98nBL3kfFt9cqOyo6pNDsrLc8?= =?iso-8859-1?q?blHuUtfRuzjNHgKeQHkBaElr/NeOwgvv+PusADdj4SPJemKj1daVcFqqeVJT?= =?iso-8859-1?q?mx2KYkjI/rZYkMdJmToAfZbp1uH0BRkCiFCWVaIykYoUbKfsDMhkMhjyNxop?= =?iso-8859-1?q?YBcKfEuB+s/zcjSjjo7solUIG344cQDZ3DCqerqgEiatwqWaDFeXUaDkbHS8?= =?iso-8859-1?q?OI8XG7hfnOVRZi49nwRKKxyjuyj+ATEFZgAF56u/XyotSy+b0brHB0C3Csx+?= =?iso-8859-1?q?oUSK4dmoXliqZeMG/nmFOtvu2Lv08G0Lo+aytXCQvayrSXup6/pIkmdQhfub?= =?iso-8859-1?q?umyr1u8NjDl2KYoqNPNbKkivvhnZ9XJMzzfL6D+4tcOeia6ZpICX+E0R3IDD?= =?iso-8859-1?q?qq3nwt0JMGgdUUZzUKEs4YSQGD96hRdQQMWOLQdtsBGDwTlUyMmiaZ4e5a+J?= =?iso-8859-1?q?weh88rD5YG0UmJmPt6OBdi2xpCztcwgMjqWrnxP0Zl+0ENpFFXgMsVdE6Tgp?= =?iso-8859-1?q?rIUTeRaLqbGPUNwwPYC6CqZrW2r6Ayk5K36mjqtP/9APISyuRKsyULdKiBqw?= =?iso-8859-1?q?e9lDWZYgThruIksIksFxaR36kCrF7ZgqM9xjxAEEv364cMJTCeOY1LnjM82S?= =?iso-8859-1?q?4UGIXNdE433Fip57BMBcXVU2ufKeD8cbdTUNUqJ8YBEIZNxY59kq+V4CSyKq?= =?iso-8859-1?q?FVmI8a+6XODKJh/LumsQMXO7hLwbWOf1cLTEcgUsWSgDuppgP01diPBo9byh?= =?iso-8859-1?q?ikotX45IRwljF9sJl3V002TDClvZ4qLnAqKna7A2TIBWiEnH8H6ZkyByerms?= =?iso-8859-1?q?SIYB2xnG1SrmM91j39m4/pmkPpThvGQPlCKcnSSo7IrbQzBgCwhECuvLmguY?= =?iso-8859-1?q?tRHvNAxaPtfNjlBlZP7AkN0/8OpYt8VWYKAoNWi8F1QppLxAxr2GTdNKBQ0G?= =?iso-8859-1?q?2Gmy2oZqV9xwWDszIiOAlyMAlv7C2r5tbj8+Wut2pdMWo8FX8GdNMwjVLtp4?= =?iso-8859-1?q?VvhmgMWWXTnDImGTgcsWdYDURG9d3c6op3TFF9HvGYuoGNzYRV/ADWqA2xzx?= =?iso-8859-1?q?8FDHUglPmZU/Rd370q6okhATAnimy7H/9jbpiVqhPrheQLODdPmgDnM6H/0k?= =?iso-8859-1?q?RDIVouYi1wiRVfMhhSdR2RnPqJpbCj6mh6ycxERg1fhcISgIyn+8QJkKyQfl?= =?iso-8859-1?q?kWx9EawFubhclKA2T/qwq1LMrOSv+L9gJdCNQ9KoXMforeHkZQbObgd21uXm?= =?iso-8859-1?q?Y2OwYlclyqz4Zo6Nzk7rnMfoRbY1xDlktcHXga8sSWJ+Px1ituoGj5MZ/pCj?= =?iso-8859-1?q?SpMAEeOwuC9D4vIN7/OrF9e6AoK985p6Yo8XX+RI3XlRzH06FmeyZG8A=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: 874bc4d2-1754-42b2-e377-08d9ec2d6f3b X-MS-Exchange-CrossTenant-originalarrivaltime: 10 Feb 2022 00:36:53.2734 (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: cTu8en1yD+0peicL2CzqjieAXydXVgsZ12K3EhOZlYPF+sWhO07B3kyAPLMTuucs X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR15MB1590 X-OriginatorOrg: fb.com X-Proofpoint-GUID: 3U-7BTlxSYufFHuSjKXlhFWoMmL3rBVp X-Proofpoint-ORIG-GUID: 3U-7BTlxSYufFHuSjKXlhFWoMmL3rBVp 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 priorityscore=1501 mlxlogscore=999 mlxscore=0 phishscore=0 impostorscore=0 lowpriorityscore=0 suspectscore=0 malwarescore=0 clxscore=1015 bulkscore=0 spamscore=0 adultscore=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 Multiple test cases already fail if you add a type whose size is different between userspace and bpf. That said, let's also add an explicit test that ensures mis-sized reads/writes do not actually happen. This test case fails before this patch series and passes after: test_skeleton:FAIL:writes and reads match size unexpected writes and reads match size: actual 3735928559 != expected 8030895855 test_skeleton:FAIL:skeleton uses underlying type unexpected skeleton uses underlying type: actual 8 != expected 4 Signed-off-by: Delyan Kratunov --- tools/testing/selftests/bpf/prog_tests/skeleton.c | 6 ++++++ tools/testing/selftests/bpf/progs/test_skeleton.c | 8 ++++++++ 2 files changed, 14 insertions(+) -- 2.34.1 diff --git a/tools/testing/selftests/bpf/prog_tests/skeleton.c b/tools/testing/selftests/bpf/prog_tests/skeleton.c index 9894e1b39211..bc07da929566 100644 --- a/tools/testing/selftests/bpf/prog_tests/skeleton.c +++ b/tools/testing/selftests/bpf/prog_tests/skeleton.c @@ -97,6 +97,9 @@ void test_skeleton(void) skel->data_read_mostly->read_mostly_var = 123; + /* validate apparent 64-bit value is actually 32-bit */ + skel->data->intest64 = (typeof(skel->data->intest64)) 0xdeadbeefdeadbeefULL; + err = test_skeleton__attach(skel); if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err)) goto cleanup; @@ -126,6 +129,9 @@ void test_skeleton(void) ASSERT_OK_PTR(elf_bytes, "elf_bytes"); ASSERT_GE(elf_bytes_sz, 0, "elf_bytes_sz"); + ASSERT_EQ(skel->data->outtest64, skel->data->intest64, "writes and reads match size"); + ASSERT_EQ(sizeof(skel->data->intest64), sizeof(u32), "skeleton uses underlying type"); + cleanup: test_skeleton__destroy(skel); } diff --git a/tools/testing/selftests/bpf/progs/test_skeleton.c b/tools/testing/selftests/bpf/progs/test_skeleton.c index 1b1187d2967b..fd1f4910cf42 100644 --- a/tools/testing/selftests/bpf/progs/test_skeleton.c +++ b/tools/testing/selftests/bpf/progs/test_skeleton.c @@ -16,6 +16,13 @@ struct s { int in1 = -1; long long in2 = -1; +/* declare the int64_t type to actually be 32-bit to ensure the skeleton + * uses actual sizes and doesn't just copy the type name + */ +typedef __s32 int64_t; +int64_t intest64 = -1; +int64_t outtest64 = -1; + /* .bss section */ char in3 = '\0'; long long in4 __attribute__((aligned(64))) = 0; @@ -62,6 +69,7 @@ int handler(const void *ctx) out4 = in4; out5 = in5; out6 = in.in6; + outtest64 = intest64; bpf_syscall = CONFIG_BPF_SYSCALL; kern_ver = LINUX_KERNEL_VERSION;