From patchwork Fri Nov 15 15:34:06 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harman Kalra X-Patchwork-Id: 63047 Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 08B09A04C6; Fri, 15 Nov 2019 16:34:15 +0100 (CET) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 59BFC2BA8; Fri, 15 Nov 2019 16:34:15 +0100 (CET) Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by dpdk.org (Postfix) with ESMTP id AA4722B86 for ; Fri, 15 Nov 2019 16:34:13 +0100 (CET) Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id xAFFVN5m031876; Fri, 15 Nov 2019 07:34:12 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : references : in-reply-to : content-type : content-transfer-encoding : mime-version; s=pfpt0818; bh=nQGKeXNjRar8b3OYDIs7u3rZ7CxnT2tkGv/DCaBPeo8=; b=ZVqHivy/zJfKIWgcdhtczjQJsW7w8euVwt6PJnXXHjBFhXdGTkCObM08l2smB4z+24PC NLADtdvExjc/LZVxobJOcNZV0Ua75tQGWjMa4h8D60dK5uwKv33whm31KzdjlzV2F1P1 RpRkHM8j/d6tgFfUFO51eIBtnBKYwDcp20l85RyClzCD87Pkxr5s50t454PedgtMDX5t O0qlu2FcJG4wvDDQkXxDa3fZqBpER2t9+YfbKv+szxSnRWJ1D8NgGIxObBJVk+DhOXsW gUP4qVzOEdSPqdbmalO2k7pkN8rTkvH6ZPzf41Z4w1LCHKEOB3nEP8LWBPuATeUA/5Sz LA== Received: from sc-exch04.marvell.com ([199.233.58.184]) by mx0b-0016f401.pphosted.com with ESMTP id 2w8whr0eye-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Fri, 15 Nov 2019 07:34:12 -0800 Received: from SC-EXCH01.marvell.com (10.93.176.81) by SC-EXCH04.marvell.com (10.93.176.84) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Fri, 15 Nov 2019 07:34:11 -0800 Received: from NAM01-SN1-obe.outbound.protection.outlook.com (104.47.32.58) by SC-EXCH01.marvell.com (10.93.176.81) with Microsoft SMTP Server (TLS) id 15.0.1367.3 via Frontend Transport; Fri, 15 Nov 2019 07:34:10 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WtinP4SVvf1AR84KhqhpWworDaqNKQfjXs1pnmo3l+ixhwaXetej8NMkzZU+6kLXcjErDaOo5iRttjrpXH3m96/1NIGtrxtatIS9RBDn/UGxzzQXUlcq4cQmguXnMrU+uf6KUhny/BX1ICtto3daAB/20bJJznZaEaYu49navOYilVke+XTrJAJyZS1uN4N535yrWTPqW7EzP3QVAOklDBf1FrPAy8TDLtDmsPp/8I4ik2nL1EF+zja++PVnETXfMhldqyRdStW/bxKlbL1uJqnlS6V4mhbX6uUaNbN883OR6ai//uGysT14plfHmvKj32TJufQPpCD13Ot/SnqxTA== 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-SenderADCheck; bh=nQGKeXNjRar8b3OYDIs7u3rZ7CxnT2tkGv/DCaBPeo8=; b=AdFPL6ZdLVllIrIP7RhIhgfilbzd6ZRwarMEmy4T3DpEWz+r0vfkHpr0CTQPsZ8r1F7fXwGGUGHfczxMcRMM27K62sAkywadJpcZ4Zsbjoz3SPrmkufXQjaXQncXCT9SGUMciHYRf6abvhshPPXdoPiQsrwPEb6UsXAJmIz/j1Azwmz++T6lwqrCofn5oh55B8ZIBF7QW7JbLlWPNizqcV1VU+bJiC1o0IceyhzVe4GzF/UETmgl4euXc7V9FolGR6Vium8+iKGljvPmOnMhqgMILT3a2enpog83mCqJrIbtkD43uM63i7ZsVX0tvL6qX1bdiyK60x3WVpXsuZn/Mg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=marvell.com; dmarc=pass action=none header.from=marvell.com; dkim=pass header.d=marvell.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.onmicrosoft.com; s=selector1-marvell-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=nQGKeXNjRar8b3OYDIs7u3rZ7CxnT2tkGv/DCaBPeo8=; b=WYIMxwfSwp1u+tzi/WLiCh0cdizV/dVI7DoTgAzTSkQTWLkOOFyKm964pKGZSMks5Gb2QQkXdAlyKZOwe0h7UZbE9S4u44spUposFmdfhyd1oeU/BB1VVQ7a2E8VYihWAH+eRGbBtXUqGGRUrKI4io/Vp9lulMAVCUafKAouoJk= Received: from MN2PR18MB2848.namprd18.prod.outlook.com (20.179.21.149) by MN2PR18MB3376.namprd18.prod.outlook.com (10.255.239.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2451.23; Fri, 15 Nov 2019 15:34:06 +0000 Received: from MN2PR18MB2848.namprd18.prod.outlook.com ([fe80::a8a7:cb5b:a6a6:9693]) by MN2PR18MB2848.namprd18.prod.outlook.com ([fe80::a8a7:cb5b:a6a6:9693%7]) with mapi id 15.20.2451.029; Fri, 15 Nov 2019 15:34:06 +0000 From: Harman Kalra To: Thomas Monjalon , John McNamara , Marko Kovacevic , "Bruce Richardson" CC: "dev@dpdk.org" , Harman Kalra Thread-Topic: [PATCH v3] mk: add support for UBSAN Thread-Index: AQHVm8oeMwfcY0KD3Uq9x+Bcnh2VbQ== Date: Fri, 15 Nov 2019 15:34:06 +0000 Message-ID: <1573832013-18946-1-git-send-email-hkalra@marvell.com> References: <1573829661-16290-1-git-send-email-hkalra@marvell.com> In-Reply-To: <1573829661-16290-1-git-send-email-hkalra@marvell.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: PN1PR01CA0080.INDPRD01.PROD.OUTLOOK.COM (2603:1096:c00:1::20) To MN2PR18MB2848.namprd18.prod.outlook.com (2603:10b6:208:3e::21) x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.7.4 x-originating-ip: [115.113.156.2] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 7563c93d-44ad-455d-4f0a-08d769e1407f x-ms-traffictypediagnostic: MN2PR18MB3376: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:9508; x-forefront-prvs: 02229A4115 x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(4636009)(136003)(346002)(366004)(396003)(39860400002)(376002)(199004)(189003)(54534003)(6512007)(476003)(71200400001)(102836004)(6436002)(8936002)(305945005)(7736002)(55236004)(50226002)(86362001)(81156014)(81166006)(8676002)(66946007)(64756008)(66476007)(66446008)(52116002)(66556008)(6506007)(386003)(2906002)(2616005)(5660300002)(76176011)(26005)(99286004)(71190400001)(107886003)(3846002)(6116002)(486006)(446003)(14454004)(36756003)(256004)(14444005)(6486002)(110136005)(316002)(4326008)(25786009)(66066001)(54906003)(11346002)(186003)(478600001); DIR:OUT; SFP:1101; SCL:1; SRVR:MN2PR18MB3376; H:MN2PR18MB2848.namprd18.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; received-spf: None (protection.outlook.com: marvell.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: L94w597lNvwkJpOq4ZLiu7lVFJ1uNlOhsufwJQQLfsh5cUwDJB8g+PiXhBsu1IH71Bi9E6TUXVmJOyDIzmOyxsRj0ru8U3In2cI1Y9g907DFA7PY9dbpcsJhAwl45aaNWUwpGQfAGPBbcRFOM4xT7N8NyG0INrpSqj+88/aAMrtv5GQxrfgti3G896YbnCtLxY9X2SNJnO9+H1L5oxh/LeNa3TghgpC0lHAhB0a/ee0Aa+g1TXrqCsinE9yKD+PHgeNc0XiML8kSyamv+H/KOYRK5f40xResqOWJuia+bJsfnWJ4WvdsM6YqMDY9AxF+3bzV519K6UhVUnUnD/4iBf17kq/dGZBslmjViUeUr9A6gtgM5wgjXL78/a4oRakwNjMIfAoWAA/d6xGVZ4wPz64dwgaQmgWgpyc4oV/SYQwXFCdKCCzprCg6CJuLmKYY MIME-Version: 1.0 X-MS-Exchange-CrossTenant-Network-Message-Id: 7563c93d-44ad-455d-4f0a-08d769e1407f X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Nov 2019 15:34:06.8446 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 70e1fb47-1155-421d-87fc-2e58f638b6e0 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: Rhg3FuY7KspqlSohahzlXoe2EG8HgnUhDN5gDkbJN8FK+Y1u/kW6J4AIWZB9sLhBcu+SICPXLB6iY5dQlE5mFg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR18MB3376 X-OriginatorOrg: marvell.com X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.95,18.0.572 definitions=2019-11-15_04:2019-11-15,2019-11-15 signatures=0 Subject: [dpdk-dev] [PATCH v3] mk: add support for UBSAN X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" UndefinedBehaviorSanitizer (UBSan) is a fast undefined behavior detector. UBSan modifies the program at compile-time to catch various kinds of undefined behavior during program execution. This patch introduces support for UBSan to the DPDK. See: doc/guides/prog_guide/ubsan.rst for more information. Signed-off-by: Harman Kalra --- V2: * Addressed review comment regarding combining two ifeq into one. V3: * Added version change logs. config/common_base | 6 ++ config/meson.build | 15 ++++ doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/ubsan.rst | 112 +++++++++++++++++++++++++ doc/guides/rel_notes/release_19_11.rst | 7 ++ meson_options.txt | 2 + mk/rte.app.mk | 8 ++ mk/rte.lib.mk | 12 +++ mk/toolchain/clang/rte.vars.mk | 4 + mk/toolchain/gcc/rte.vars.mk | 8 ++ 10 files changed, 175 insertions(+) create mode 100644 doc/guides/prog_guide/ubsan.rst diff --git a/config/common_base b/config/common_base index 914277856..f1bb3e0b2 100644 --- a/config/common_base +++ b/config/common_base @@ -1098,3 +1098,9 @@ CONFIG_RTE_APP_CRYPTO_PERF=y # Compile the eventdev application # CONFIG_RTE_APP_EVENTDEV=y + +# +# Enable undefined behavior sanitizer +# +CONFIG_RTE_UBSAN=n +CONFIG_RTE_UBSAN_SANITIZE_ALL=n diff --git a/config/meson.build b/config/meson.build index 2b1cb92e7..a43c23f50 100644 --- a/config/meson.build +++ b/config/meson.build @@ -238,3 +238,18 @@ if get_option('b_lto') add_project_link_arguments('-Wno-lto-type-mismatch', language: 'c') endif endif + +# enable ubsan +if get_option('enable_ubsan') + if cc.has_argument('-fsanitize=undefined') + ubsan_dep = cc.find_library('libubsan', required: false) + if ubsan_dep.found() + add_project_arguments('-fsanitize=undefined', language: 'c') + add_project_link_arguments('-fsanitize=undefined', language: 'c') + else + message('libubsan not found, UBSAN cannot be enabled') + endif + else + message('gcc version does not support UBSAN') + endif +endif diff --git a/doc/guides/prog_guide/index.rst b/doc/guides/prog_guide/index.rst index dc4851c57..911b82a41 100644 --- a/doc/guides/prog_guide/index.rst +++ b/doc/guides/prog_guide/index.rst @@ -67,4 +67,5 @@ Programmer's Guide writing_efficient_code lto profile_app + ubsan glossary diff --git a/doc/guides/prog_guide/ubsan.rst b/doc/guides/prog_guide/ubsan.rst new file mode 100644 index 000000000..cb19f3bd9 --- /dev/null +++ b/doc/guides/prog_guide/ubsan.rst @@ -0,0 +1,112 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2019 Marvell International Ltd. + +The Undefined Behavior Sanitizer - UBSan +======================================== + +UndefinedBehaviorSanitizer (UBSan) is a runtime undefined behavior detector. +UBSan uses compile-time instrumentation and modifies the program by adding +some stubs which perform certain checks before operations that might cause +undefined behaviour. If some UB detected, respective _UBSan_handle_* handlers +(which are defined in libUBSan library) are called to prints the error message. + +Some examples of undefined behaviour checks: + +* Misaligned memory access +* Signed integer overflow +* Load from/store to an object with insufficient space. +* Integer divide by zero as well as INT_MIN / -1 division +* Out-of-bounds memory accesses. +* Null argument declared with nonnull attribute, returned null from function + which never returns null, null ptr dereference +* Variable size array with non-positive length + +GCC supports this feature since 4.9, however GCC 5.0 onwards has many more +checkers implemented. + +Example UBSan error +-------------------- + +Following error was reported when UBSan was enabled: + +.. code-block:: console + + drivers/net/octeontx2/otx2_stats.c:82:26: runtime error: left shift of + 1 by 31 places cannot be represented in type 'int' + +Code responsible for this error: + +.. code-block:: c + + if (dev->txmap[i] & (1 << 31)) { + +To fix this error: + +.. code-block:: c + + if (dev->txmap[i] & (1U << 31)) { + +Usage +----- + +make build +^^^^^^^^^^ + +To enable UBSan, enable following configuration: + +.. code-block:: console + + CONFIG_RTE_UBSAN=y + +UBSan framework supports three modes: + +1. Enable UBSan on the entire DPDK source code - set following configuration: + +.. code-block:: console + + CONFIG_RTE_UBSAN_SANITIZE_ALL=y + +2. Enable UBSan on a particular library or PMD - add the following line to the + respective Makefile of the library or PMD + (make sure ``CONFIG_RTE_UBSAN_SANITIZE_ALL=n``). This will instrument only + the library or PMD and not the entire repository. + +.. code-block:: console + + UBSAN_SANITIZE := y + +3. Disable UBSan for a particular library or PMD - add the following line to + the respective Makefile of the library or PMD. Make sure + ``CONFIG_RTE_UBSAN_SANITIZE_ALL=y`` config is set. This will instrument + entire DPDK repository but not this specific library or PMD. + +.. code-block:: console + + UBSAN_SANITIZE := n + +.. Note:: + + Standard DPDK applications like test, testpmd, etc. cannot be + chosen explicitly for UBSan check, like libraries or PMD. The reason is, + say UBSan is enabled for library X, and ``UBSAN_SANITIZE=y`` is not added + in Makefile of app Y which uses X APIs. This will lead to undefined + reference to _UBSan_handle_* handlers as Y is not compiled with UBSan flags. + Hence UBSan check is enabled for all standard DPDK applications as soon as + ``CONFIG_RTE_UBSAN=y`` is set. + +meson build +^^^^^^^^^^^ + +To enable UBSan in meson build system, use following meson build command: + +**Example usage:** + +.. code-block:: console + + meson build -Denable_ubsan=true + ninja -C build + +.. Note:: + + Meson build works only in one mode i.e. UBSan can be enabled for + the entire DPDK sources and not individual libraries or PMD, like make build. diff --git a/doc/guides/rel_notes/release_19_11.rst b/doc/guides/rel_notes/release_19_11.rst index c0045a91f..61fd1bcc2 100644 --- a/doc/guides/rel_notes/release_19_11.rst +++ b/doc/guides/rel_notes/release_19_11.rst @@ -294,6 +294,13 @@ New Features See :doc:`../prog_guide/lto` for more information: +* **Added Undefined Behavior Sanitizer framework.** + + UBSan is a fast runtime undefined behavior detector which uses compile-time + instrumentation and modifies the program by adding some stubs that perform + certain checks before operations that might cause undefined behavior. + + See :doc:`../prog_guide/ubsan` for more information: Removed Items diff --git a/meson_options.txt b/meson_options.txt index 89650b0e9..f3b42d2b1 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -10,6 +10,8 @@ option('enable_docs', type: 'boolean', value: false, description: 'build documentation') option('enable_kmods', type: 'boolean', value: true, description: 'build kernel modules') +option('enable_ubsan', type: 'boolean', value: false, + description: 'Enables undefined behavior sanitizer') option('examples', type: 'string', value: '', description: 'Comma-separated list of examples to build by default') option('flexran_sdk', type: 'string', value: '', diff --git a/mk/rte.app.mk b/mk/rte.app.mk index 683e3a4e3..1304227cf 100644 --- a/mk/rte.app.mk +++ b/mk/rte.app.mk @@ -385,6 +385,14 @@ endif MAPFLAGS = -Map=$@.map --cref +# +# If UBSAN is enabled, all application will be compiled with +# '-fsanitize=undefined' flag +# +ifeq ($(CONFIG_RTE_UBSAN)$(UBSAN_ENABLE),yy) +CFLAGS += -fsanitize=undefined +endif + .PHONY: all all: install diff --git a/mk/rte.lib.mk b/mk/rte.lib.mk index 4df8849a0..33f5746c8 100644 --- a/mk/rte.lib.mk +++ b/mk/rte.lib.mk @@ -29,6 +29,18 @@ CPU_LDFLAGS += --version-script=$(SRCDIR)/$(EXPORT_MAP) endif endif +# +# If UBSAN is enabled, lib to undergo check can be chosen +# by setting UBSAN_SANITIZE=y in respective lib Makefile +# else set CONFIG_RTE_UBSAN_SANITIZE_ALL=y to enforce check +# on entire repo. +# +ifeq ($(CONFIG_RTE_UBSAN),y) +ifeq ($(UBSAN_ENABLE),y) +CFLAGS += $(if $(patsubst %n,,$(CONFIG_RTE_UBSAN_SANITIZE_ALL)$(UBSAN_SANITIZE)) \ + , -fsanitize=undefined) +endif +endif _BUILD = $(LIB) PREINSTALL = $(SYMLINK-FILES-y) diff --git a/mk/toolchain/clang/rte.vars.mk b/mk/toolchain/clang/rte.vars.mk index 3c49dc568..623780106 100644 --- a/mk/toolchain/clang/rte.vars.mk +++ b/mk/toolchain/clang/rte.vars.mk @@ -56,5 +56,9 @@ ifeq ($(shell test $(CLANG_MAJOR_VERSION) -ge 4 && echo 1), 1) WERROR_FLAGS += -Wno-address-of-packed-member endif +ifeq ($(CONFIG_RTE_UBSAN),y) +UBSAN_ENABLE := y +endif + export CC AS AR LD OBJCOPY OBJDUMP STRIP READELF export TOOLCHAIN_CFLAGS TOOLCHAIN_LDFLAGS TOOLCHAIN_ASFLAGS diff --git a/mk/toolchain/gcc/rte.vars.mk b/mk/toolchain/gcc/rte.vars.mk index 9fc704193..43e7d139b 100644 --- a/mk/toolchain/gcc/rte.vars.mk +++ b/mk/toolchain/gcc/rte.vars.mk @@ -102,5 +102,13 @@ endif # disable packed member unalign warnings WERROR_FLAGS += -Wno-address-of-packed-member +ifeq ($(CONFIG_RTE_UBSAN),y) +ifeq ($(shell test $(GCC_VERSION) -lt 49 && echo 1), 1) +$(warning UBSAN not supported gcc < 4.9) +else +UBSAN_ENABLE = y +endif +endif + export CC AS AR LD OBJCOPY OBJDUMP STRIP READELF export TOOLCHAIN_CFLAGS TOOLCHAIN_LDFLAGS TOOLCHAIN_ASFLAGS