Received: by 10.192.165.156 with SMTP id m28csp37514imm; Tue, 10 Apr 2018 15:54:47 -0700 (PDT) X-Google-Smtp-Source: AIpwx484Fy1tsDq6rlA7CRix4bmVQU5JMNxfnDuwMBqXNC8kLzXeTR6V5zevq/maPfh3idJte+2U X-Received: by 10.98.70.199 with SMTP id o68mr1853813pfi.169.1523400887101; Tue, 10 Apr 2018 15:54:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523400887; cv=none; d=google.com; s=arc-20160816; b=cr2SjSxOO290BDVkX4PEc9aWS8Wo+mufTJVTt0CnYmwYkoGJWYtWtDosUF9eFCdZxd A1CqzLjgRW/INsI44wwqjXnxDY5sSeRk4XIIBHGMcScnDGbZkB52SSU1DuGrboir4oV8 gbq1VV3Y86dJMjtK5xiCVxgmXfqeEhbRLV1SZQp5PE2mYMjheJai6WMOxzCtFrr78o34 65c2zZufT1q8jkOvi1pPRbJ8nnHJGjOrIhzThVGPZBaVfsjeTWPiYIHD7TIiGqXOdF68 kFkhNlL5l2LhhsHEwOmzamtUphnsjmgHvxcGHlmL3vjgvW8jh5t8I+/Bb3bTGGjOhzPc ilug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-authentication-results; bh=vusQd/Hl0VNJvjFrfbUFCig8nrzKzlKuah2t2gGCTTU=; b=GurF3G4nTp2MShgo5pCupke4gh7h52Aj3MwLnAcDK6MyGgQxqPR+buMvDxmsj4UbCM DRWegMlcYE7x0SlKlsDj2/Q3JSdWCHRPB0lIK7S8Dq5jEHkdEpT5ELicD2AeOggjaFYS Xk5IoZWGbCQcJFQTnWhog5zF2xC51EkuBm6pEYd1Cl/C+M2HXPHq91LaSosFboqziRfy vweao4vwuR4YXHklZycRfH+zeRfMhLsXyvEAeItA7C9X8ALuqURdsD0KBhbpmzrE7FzF lzuj9VBPX5N7l6e1uQrLR1q9VIcPOJshN2MgCBAgE9Fv/rNq3cQnYMR2Kr8mEmtQvIpX l1JQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=cQ/zy6Z7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 37-v6si3526957plq.288.2018.04.10.15.54.10; Tue, 10 Apr 2018 15:54:47 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=cQ/zy6Z7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932488AbeDJWqa (ORCPT + 99 others); Tue, 10 Apr 2018 18:46:30 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:43080 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755965AbeDJWq1 (ORCPT ); Tue, 10 Apr 2018 18:46:27 -0400 Received: by mail-pf0-f193.google.com with SMTP id j2so9447822pff.10; Tue, 10 Apr 2018 15:46:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=vusQd/Hl0VNJvjFrfbUFCig8nrzKzlKuah2t2gGCTTU=; b=cQ/zy6Z73lgt6E1QV9dJb7w37vBd3eZtQ0lCEhmaFNsK+e8WnN6j4uG9D68HpS4Sx+ 5LtXfYQc56QtdltMyozjbCH9lClYNEWRcLS5sH+1+LIriiR09YwVHSfi8i4OjBRUGQF9 3vdS6jzFIK+SBjXmjMDgxTq/jrNKPChYBpeZMc17bquCiTao90Q6Ogn0j7PrYSd2ByMc X5dFasVfnLa8nqxML4PiqwCu0Otljcyx5x5mBBGuzSfEqFz9gCNwcRwLz/pLPlEs+k0J DfUMlmlkNqRdF45LfrjoL/jKAlTuBva1EtECOsekblfW1G1jicAWpZKzvEGrRs3QGOE5 Twlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=vusQd/Hl0VNJvjFrfbUFCig8nrzKzlKuah2t2gGCTTU=; b=aFVlK8Tn+/sC5LbH1rL6O4bhPbWYi2ktf397KipMFPg7uqhrJ2yhN2jA3dvsp8qc5U Lt2l0Bhbr3Ag5fer8+ool+OIIajZWX7waMejh3h2RMNzAlXrNQ2Uknox5e3e8ZoW6Rjc XxuaTA+b9dtxpUqhWjh387mERmsjlYjPqUecXJwqziPcKlHLagD0YnQYKBRK/ZAvkiiX FCE5xEcEUPOg6ZWtVwmzRRA7FNPXpFz6QBXKA/Ku9Mu5PCSn6dG6z7wluaxNXSi9NgF6 4MG6cmQ5T2x98Xk+v13jrP5rfEOXyWWkT2dxiHt1csgRqwj1lsxoe0vTa+86+bB3XHJA dsCA== X-Gm-Message-State: ALQs6tDoDLxmj5n1T40JoYP2VmtmVuOB3W0u9lHqdJo7RRtvK/HiVxRp jxlf21Ahi4kxUEItTMhzYbQPww== X-Received: by 10.99.100.132 with SMTP id y126mr1553604pgb.77.1523400387029; Tue, 10 Apr 2018 15:46:27 -0700 (PDT) Received: from gmail.com ([2605:e000:1117:c0e3:2e0:1bff:fe70:ea]) by smtp.gmail.com with ESMTPSA id v7sm6697725pfm.147.2018.04.10.15.46.25 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 10 Apr 2018 15:46:26 -0700 (PDT) Date: Tue, 10 Apr 2018 12:46:24 -1000 From: Joey Pabalinas To: linux-sparse@vger.kernel.org Cc: Kees Cook , Linus Torvalds , Martin Uecker , Al Viro , Christopher Li , Joey Pabalinas , Luc Van Oostenryck , Linux Kernel Mailing List Subject: [PATCH v4] sparse: add -Wpointer-arith flag to toggle sizeof(void) warnings Message-ID: <20180410224624.psyo7vsy4qjplh3j@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="4bytdfjebyecb6md" Content-Disposition: inline User-Agent: NeoMutt/20180323-62-378db9 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --4bytdfjebyecb6md Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Recent changes to the min()/max() macros in include/linux/kernel.h have added a lot of noise when compiling the kernel with Sparse checking enabled. This mostly is due to the *huge* increase in the number of sizeof(void) warnings, a larger number of which can safely be ignored. Add the -Wpointer-arith flag to enable/disable these warnings (along with the warning when applying sizeof to function types as well as warning about pointer arithmetic on these types exactly like the GCC -Wpointer-arith flag) on demand; the warning itself has been disabled by default to reduce the large influx of noise which was inadvertently added by commit 3c8ba0d61d04ced9f8 (kernel.h: Retain constant expression output for max()/min()). Update the manpage to document the new flag and add a validation case for sizeof(void). CC: Kees Cook CC: Linus Torvalds CC: Martin Uecker CC: Al Viro CC: Christopher Li CC: Joey Pabalinas CC: Luc Van Oostenryck Signed-off-by: Joey Pabalinas Signed-off-by: Luc Van Oostenryck 5 files changed, 64 insertions(+), 2 deletions(-) diff --git a/evaluate.c b/evaluate.c index b96696d3a51396800a..8f07d08cf5b494f8f0 100644 --- a/evaluate.c +++ b/evaluate.c @@ -2169,7 +2169,8 @@ static struct symbol *evaluate_sizeof(struct expressi= on *expr) size =3D type->bit_size; =20 if (size < 0 && is_void_type(type)) { - warning(expr->pos, "expression using sizeof(void)"); + if (Wpointer_arith) + warning(expr->pos, "expression using sizeof(void)"); size =3D bits_in_char; } =20 @@ -2180,7 +2181,8 @@ static struct symbol *evaluate_sizeof(struct expressi= on *expr) } =20 if (is_function(type->ctype.base_type)) { - warning(expr->pos, "expression using sizeof on a function"); + if (Wpointer_arith) + warning(expr->pos, "expression using sizeof on a function"); size =3D bits_in_char; } =20 diff --git a/lib.c b/lib.c index 645132a892107512a1..0caee38a996cd0fa4c 100644 --- a/lib.c +++ b/lib.c @@ -241,6 +241,7 @@ int Woverride_init =3D 1; int Woverride_init_all =3D 0; int Woverride_init_whole_range =3D 0; int Wparen_string =3D 0; +int Wpointer_arith =3D 0; int Wptr_subtraction_blows =3D 0; int Wreturn_void =3D 0; int Wshadow =3D 0; @@ -536,6 +537,7 @@ static const struct warning { { "return-void", &Wreturn_void }, { "shadow", &Wshadow }, { "sizeof-bool", &Wsizeof_bool }, + { "pointer-arith", &Wpointer_arith }, { "sparse-error", &Wsparse_error }, { "tautological-compare", &Wtautological_compare }, { "transparent-union", &Wtransparent_union }, diff --git a/lib.h b/lib.h index a9b70b07686801305c..98e20d3830d9059acb 100644 --- a/lib.h +++ b/lib.h @@ -134,6 +134,7 @@ extern int Woverride_init; extern int Woverride_init_all; extern int Woverride_init_whole_range; extern int Wparen_string; +extern int Wpointer_arith; extern int Wptr_subtraction_blows; extern int Wreturn_void; extern int Wshadow; diff --git a/sparse.1 b/sparse.1 index e183204de623efd022..3bd5f1cb11309e65b8 100644 --- a/sparse.1 +++ b/sparse.1 @@ -282,6 +282,19 @@ Standard C syntax does not permit a parenthesized stri= ng as an array initializer. GCC allows this syntax as an extension. With \fB\-Wparen\-string\fR, Sparse will warn about this syntax. =20 +Sparse does not issue these warnings by default. +. +.TP +.B \-Wpointer\-arith +Warn about anything that depends on the \fBsizeof\fR a void or function ty= pe. + +C99 does not allow the \fBsizeof\fR operator to be applied to function typ= es +or to incomplete types such as void. GCC allows \fBsizeof\fR to be applied= to +these types as an extension and assigns these types a size of \fI1\fR. With +\fB\-pointer\-arith\fR, Sparse will warn about pointer arithmetic on void +or function pointers, as well as expressions which directly apply the +\fBsizeof\fR operator to void or function types. + Sparse does not issue these warnings by default. . .TP diff --git a/validation/sizeof-void.c b/validation/sizeof-void.c new file mode 100644 index 000000000000000000..0fd917a21fb296a95d --- /dev/null +++ b/validation/sizeof-void.c @@ -0,0 +1,44 @@ +#define is_constexpr(x) \ + (sizeof(int) =3D=3D sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) + +static int test(void) +{ + unsigned int s =3D 0, i =3D 0; + void *ptr =3D &i; + + // OK + s +=3D sizeof i; + s +=3D sizeof &i; + s +=3D sizeof ptr; + s +=3D sizeof &ptr; + + // KO + s +=3D sizeof(void); + s +=3D sizeof *ptr; + s +=3D is_constexpr(ptr++); + s +=3D is_constexpr((i++, 1)); + s +=3D is_constexpr(sizeof *ptr); + s +=3D is_constexpr(ptr + 1); + s +=3D is_constexpr(&ptr + 1); + s +=3D is_constexpr(*(((char *)&ptr) + 1)); + + return s; +} + +/* + * check-name: sizeof(void) is valid + * check-description: sizeof(void) was rejected because void is an incompl= ete + * type + * check-command: sparse -Wpointer-arith $file + * + * check-error-start +sizeof-void.c:16:14: warning: expression using sizeof(void) +sizeof-void.c:17:14: warning: expression using sizeof(void) +sizeof-void.c:18:14: warning: expression using sizeof(void) +sizeof-void.c:19:14: warning: expression using sizeof(void) +sizeof-void.c:20:14: warning: expression using sizeof(void) +sizeof-void.c:21:14: warning: expression using sizeof(void) +sizeof-void.c:22:14: warning: expression using sizeof(void) +sizeof-void.c:23:14: warning: expression using sizeof(void) + * check-error-end + */ --=20 2.17.0.rc1.35.g90bbd502d54fe92035.dirty --4bytdfjebyecb6md Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEKlZXrihdNOcUPZTNruvLfWhyVBkFAlrNPsAACgkQruvLfWhy VBnyvg//WQiT6Tw9pBZ8DuJVTgH8OJT2M3M9fwgJPr4i3Md0Z0pLaBUpvkAaVWmL QdWeB+BKcQKg6k+RvSUPy6L1L69qRhJqzxmAK5YNGxR44040Y92O4lDcoAczBLcv F8cJF93/RBSxoyI73FQbIzxfss8rnzYcy1LlvCxeisL3/giHqVhL7ANqa92tS84C 4xJYzY/ulSMBNz250Fl49Xje/GC3uHiroaECjLFG9DlXYfn/tM7vPGzVshwjiH9L JAxPoBofA3q1PR2yIJUYEx7fpAxK8x/9eNvgKIvs5GyIDUpoegLA6o/+AzMI/PAM 7V1P4uYfpHwdAFK2GiTWYrD8yU55gAIx63rlLrCmFTAMOKZZaKthaqGOkNpJvNRd McfiNKRc1xRjwpsZdlOx972zQGlee6c58PTfluOIO6H65srhlOG7f4Hs9GzkmfUh MJ20ozr98YMujxXR4PeI/0KdBuR6jh/3VzGyZ/JOysfF1E3Ks5utFOXPpJaC9N0G syV54nujy7ek+a7bF8Csv84aoexoY5b04jLTuTX0yzowrvNr77kdLV2om9tNsmBj o3Z0vLn4N9pSPa99Djv4NaLgKWZq9ugB9hsZ0DmAmYgo13GGg2Z8a1GPncy1Tci+ w8EIV+YzMcmDMh7argeiUE52ezgLMb5LQ7r0MJ7C8vgOZYSozV0= =O1XX -----END PGP SIGNATURE----- --4bytdfjebyecb6md--