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);