From patchwork Tue Apr 10 01:02:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joey Pabalinas X-Patchwork-Id: 10332169 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B01936037F for ; Tue, 10 Apr 2018 01:02:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9CE3628C60 for ; Tue, 10 Apr 2018 01:02:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9179328C7E; Tue, 10 Apr 2018 01:02:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.4 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, FSL_HELO_FAKE, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID,T_TVD_MIME_EPI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 75C6428C60 for ; Tue, 10 Apr 2018 01:02:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751998AbeDJBCT (ORCPT ); Mon, 9 Apr 2018 21:02:19 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:35640 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751635AbeDJBCS (ORCPT ); Mon, 9 Apr 2018 21:02:18 -0400 Received: by mail-pf0-f193.google.com with SMTP id u86so6832012pfd.2; Mon, 09 Apr 2018 18:02:18 -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=AR8VNz3zJk0xmDKI+vjkSUTrYOmg3tXlCe16pFzA5wE=; b=iSRcOeggkhWEzhMHPS/IMaoMRp2OIOVYza6VVaynRn9H9f0wb7lg+mn1M7neAX0Uk2 z/RxA7Gg0goxGWN4JoaFnbMlxkyYWVFzEJf/CVUYZ4JUGVfWsWUt662EEjff5eWrP2tX ZbObCVdfEsqyXDSGBiT2PqOGfbaXyU5TXaSkCLD08FopqmotQUth8r8KJ9APppHT5NNb PokbCffyKvL3kMnsgzu5ao5Lxl0g1z7mO585CEY92EFx++EJh7cu2j3uk5iPaSJ+faZL fNE9SARspAmcvXrfhx+DMSHxoFSUllMBoPdeZ9l7psOMBv6amdyP0K9lQXAAJY4YIyp0 X9gQ== 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=AR8VNz3zJk0xmDKI+vjkSUTrYOmg3tXlCe16pFzA5wE=; b=acn3zcbvVZSKJ/G41sbefEJT34kPaxo2mwhpvyFjKCyZ+MKR1QlwSo2sZSre7zciYI 0o3W6mNBd+1uFEuMHE6v78GFMFN4sd5ykMF7DAp76EEtPpC7z8ORi9G9XUGB5PqAL1r/ kk8eB9rORxZpdk2/QHyx5Ldb4VhzgOpRVQvAPReWt25INa6oXbP60F9KPyrXbEMJfUVB tQPdbwO+d9ykCJM8R6EcfcjHvhdj4zq2pTCTV/6kSZSjuxZZkrCc6hzhXgCG+9FQWZWw 8ObikATNxZiTMNSFShTiu+trIPoa8yAFMMNjbdgYAQeSiEvjVo6Dt7Uj1HSE9DCCcyGt 3XAA== X-Gm-Message-State: ALQs6tB7vwkARGZHF+vRa6urafldX57Zl0Sv8Ovp/t442fLb/bWP31ZN ooDgpK9tYxoZBMRNkfOKEXQpbw== X-Google-Smtp-Source: AIpwx484O6dC/dGTGqzNQNwdrfPYbzxLUg4ETkioXs6S3OFdJc53rxbrJDF/jlGd8UrmNa+7Kba4Ow== X-Received: by 10.98.137.218 with SMTP id n87mr957368pfk.48.1523322138118; Mon, 09 Apr 2018 18:02:18 -0700 (PDT) Received: from gmail.com (cpe-98-150-136-16.hawaii.res.rr.com. [98.150.136.16]) by smtp.gmail.com with ESMTPSA id c186sm2359458pfb.40.2018.04.09.18.02.16 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 09 Apr 2018 18:02:17 -0700 (PDT) Date: Mon, 9 Apr 2018 15:02:04 -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 v3] sparse: add -Wpointer-arith flag to toggle sizeof(void) warnings Message-ID: <20180410010204.qeudkkru4jilun3v@gmail.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: NeoMutt/20180323-62-378db9 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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/update validation cases regarding the application of sizeof to void and function types. 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 evaluate.c | 6 ++++-- lib.c | 2 ++ lib.h | 1 + sparse.1 | 13 ++++++++++++ validation/sizeof-function.c | 2 +- validation/sizeof-void.c | 39 ++++++++++++++++++++++++++++++++++++ 6 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 validation/sizeof-void.c 6 files changed, 60 insertions(+), 3 deletions(-) diff --git a/evaluate.c b/evaluate.c index 4e1dffe9c5416380df..f828da37df8e686623 100644 --- a/evaluate.c +++ b/evaluate.c @@ -2193,7 +2193,8 @@ static struct symbol *evaluate_sizeof(struct expression *expr) size = type->bit_size; 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 = bits_in_char; } @@ -2204,7 +2205,8 @@ static struct symbol *evaluate_sizeof(struct expression *expr) } 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 = bits_in_char; } diff --git a/lib.c b/lib.c index 73d372c36626538bab..f7fdac96674aec4c24 100644 --- a/lib.c +++ b/lib.c @@ -242,6 +242,7 @@ int Woverride_init = 1; int Woverride_init_all = 0; int Woverride_init_whole_range = 0; int Wparen_string = 0; +int Wpointer_arith = 0; int Wptr_subtraction_blows = 0; int Wreturn_void = 0; int Wshadow = 0; @@ -654,6 +655,7 @@ static const struct flag warnings[] = { { "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 3050b5577ba4d42e97..e34bb9a02ebac03f52 100644 --- a/lib.h +++ b/lib.h @@ -151,6 +151,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 88343f3170f195297a..4e1f2385fea7ec9296 100644 --- a/sparse.1 +++ b/sparse.1 @@ -288,6 +288,19 @@ Standard C syntax does not permit a parenthesized string as an array initializer. GCC allows this syntax as an extension. With \fB\-Wparen\-string\fR, Sparse will warn about this syntax. +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 type. + +C99 does not allow the \fBsizeof\fR operator to be applied to function types +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-function.c b/validation/sizeof-function.c index 27d535d4ebda79ef0c..8ff67f2149981de7a9 100644 --- a/validation/sizeof-function.c +++ b/validation/sizeof-function.c @@ -35,7 +35,7 @@ int test(void) /* * check-name: sizeof-function - * check-command: sparse -Wno-decl $file + * check-command: sparse -Wpointer-arith -Wno-decl $file * * check-error-start sizeof-function.c:22:14: warning: expression using sizeof on a function diff --git a/validation/sizeof-void.c b/validation/sizeof-void.c new file mode 100644 index 000000000000000000..bc58c98d18d5fa3cda --- /dev/null +++ b/validation/sizeof-void.c @@ -0,0 +1,39 @@ +#define is_constexpr(x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) + +int test(void) +{ + unsigned int s = 0, i = 0; + void *ptr = &i; + + // OK + s += sizeof i; + s += sizeof &i; + s += sizeof ptr; + s += sizeof &ptr; + + // KO + s += sizeof(void); + s += sizeof *ptr; + s += is_constexpr(1 + 1); + s += is_constexpr((1, 1)); + s += is_constexpr(ptr + 1); + s += is_constexpr(&ptr + 1); + s += is_constexpr(*(((char *)&ptr) + 1)); + + return s; +} + +/* + * check-name: sizeof-void + * check-command: sparse -Wpointer-arith -Wno-decl -Wno-unused-value $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: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) + * check-error-end + */