From patchwork Tue Apr 10 22:46:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joey Pabalinas X-Patchwork-Id: 10334355 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 04EF1601A0 for ; Tue, 10 Apr 2018 22:46:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E70A02853A for ; Tue, 10 Apr 2018 22:46:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DAD532853E; Tue, 10 Apr 2018 22:46:42 +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=unavailable 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 5A11B2853A for ; Tue, 10 Apr 2018 22:46:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932255AbeDJWq3 (ORCPT ); Tue, 10 Apr 2018 18:46:29 -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-Google-Smtp-Source: AIpwx4+GYzMSjsKaTulWml7VHgljr5kSl4RmLpAlPC4Ey6OP64EcxagaOIGWAHxQCyHQoT/BkZip/Q== 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-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 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(-) Acked-by: Luc Van Oostenryck 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 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; } @@ -2180,7 +2181,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 645132a892107512a1..0caee38a996cd0fa4c 100644 --- a/lib.c +++ b/lib.c @@ -241,6 +241,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; @@ -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 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-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) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) + +static 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(ptr++); + s += is_constexpr((i++, 1)); + s += is_constexpr(sizeof *ptr); + s += is_constexpr(ptr + 1); + s += is_constexpr(&ptr + 1); + s += is_constexpr(*(((char *)&ptr) + 1)); + + return s; +} + +/* + * check-name: sizeof(void) is valid + * check-description: sizeof(void) was rejected because void is an incomplete + * 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 + */