Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp19348863ybl; Fri, 3 Jan 2020 22:48:02 -0800 (PST) X-Google-Smtp-Source: APXvYqyM84bnLlrbaF2TVotyEzbamx+jKLuEphDY2U3apERkm/mlCuOGZe0+bO+py2B/bHhEVcrk X-Received: by 2002:a05:6830:1515:: with SMTP id k21mr85815255otp.177.1578120482195; Fri, 03 Jan 2020 22:48:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578120482; cv=none; d=google.com; s=arc-20160816; b=CUSwUNq0DQLKcIQ8cHibYTucKZkbJeuud2tRCjoRc7W4wpiO7HsQuJyM3EB+X1zMAH 2HI9L3t0XSf/zObgeqe0LtGCsS1nGrxeYczGxba7my/iB3xYt5sjM0IjG+a6lDT3wC+h a5aAkew18kAb1g5LLFyN3niN0VH1Mdioc462Ls3wRNw04vy0fsRNQ5pVvvTsHFFEk/6O KJ+2X9tjF8MLzZoKX7bba/OrWCPoPtUtMJ1Mvt6FwoIVv0MbC9hC3PM8S2Rc06QWFWR7 uHD+UpFkPGXiyQwTMno2Aw/JSH5BxjOsDOJ6CnuJqXnHHykX4R5SWNZk+6tFN4qYy3LM h4Bw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=ViDgrf0859ftI4OrvNHYx4LFtPQ/9B1S/z81kX+gfhM=; b=NiJpEc1pVWv6fjy6MODzcEZ3kcCfEWn5xAqbi7sI3rLnEZQKRwxZD5JYDlockT0x2P tJfFkHuKbD0eT6MLqI4lEPs6d5KSpbbK/yTrBXuF6YrPeKTdfiXcdNo8teaVZOLyVtJi MdmohUQaeniRpl46tlhMMkHCYxmI5vFqQh7fOnLWLbz6ry61ITj8FIRqthoaBBNXRvfO gt1aZwF8IK31CuE7tuLzpfCKAD+iDmWB7cbfZt6C05uTW7tlSPNljzM4VP5xzFGhKDPr miGvwYFZPL5fD25tKRwUwGpHZX6TZjXCgGnQRTAwvcHjGtcO3LhbDnukSCkyAAZnTCTu 81pg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a20si32227221otf.271.2020.01.03.22.47.19; Fri, 03 Jan 2020 22:48:02 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725940AbgADGpF (ORCPT + 99 others); Sat, 4 Jan 2020 01:45:05 -0500 Received: from out30-56.freemail.mail.aliyun.com ([115.124.30.56]:39729 "EHLO out30-56.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725790AbgADGpF (ORCPT ); Sat, 4 Jan 2020 01:45:05 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R191e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01f04427;MF=wenyang@linux.alibaba.com;NM=1;PH=DS;RN=11;SR=0;TI=SMTPD_---0Tmn3PsD_1578120290; Received: from localhost(mailfrom:wenyang@linux.alibaba.com fp:SMTPD_---0Tmn3PsD_1578120290) by smtp.aliyun-inc.com(127.0.0.1); Sat, 04 Jan 2020 14:44:56 +0800 From: Wen Yang To: Julia Lawall Cc: Wen Yang , Gilles Muller , Nicolas Palix , Michal Marek , Matthias Maennich , Greg Kroah-Hartman , Masahiro Yamada , Thomas Gleixner , cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org Subject: [PATCH] coccinelle: semantic patch to check for inappropriate do_div() calls Date: Sat, 4 Jan 2020 14:44:48 +0800 Message-Id: <20200104064448.24314-1-wenyang@linux.alibaba.com> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org do_div() does a 64-by-32 division. When the divisor is unsigned long, u64, or s64, do_div() truncates it to 32 bits, this means it can test non-zero and be truncated to zero for division. This semantic patch is inspired by Mateusz Guzik's patch: commit b0ab99e7736a ("sched: Fix possible divide by zero in avg_atom() calculation") Signed-off-by: Wen Yang Cc: Julia Lawall Cc: Gilles Muller Cc: Nicolas Palix Cc: Michal Marek Cc: Matthias Maennich Cc: Greg Kroah-Hartman Cc: Masahiro Yamada Cc: Thomas Gleixner Cc: cocci@systeme.lip6.fr Cc: linux-kernel@vger.kernel.org --- scripts/coccinelle/misc/do_div.cocci | 66 ++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 scripts/coccinelle/misc/do_div.cocci diff --git a/scripts/coccinelle/misc/do_div.cocci b/scripts/coccinelle/misc/do_div.cocci new file mode 100644 index 0000000..f1b72d1 --- /dev/null +++ b/scripts/coccinelle/misc/do_div.cocci @@ -0,0 +1,66 @@ +// SPDX-License-Identifier: GPL-2.0-only +/// do_div() does a 64-by-32 division. +/// When the divisor is unsigned long, u64, or s64, +/// do_div() truncates it to 32 bits, this means it +/// can test non-zero and be truncated to zero for division. +/// +//# This makes an effort to find those inappropriate do_div () calls. +// +// Confidence: Moderate +// Copyright: (C) 2020 Wen Yang, Alibaba. +// Comments: +// Options: --no-includes --include-headers + +virtual context +virtual org +virtual report + +@depends on context@ +expression f; +long l; +unsigned long ul; +u64 ul64; +s64 sl64; + +@@ +( +* do_div(f, l); +| +* do_div(f, ul); +| +* do_div(f, ul64); +| +* do_div(f, sl64); +) + +@r depends on (org || report)@ +expression f; +long l; +unsigned long ul; +position p; +u64 ul64; +s64 sl64; +@@ +( +do_div@p(f, l); +| +do_div@p(f, ul); +| +do_div@p(f, ul64); +| +do_div@p(f, sl64); +) + +@script:python depends on org@ +p << r.p; +@@ + +msg="WARNING: WARNING: do_div() does a 64-by-32 division, which may truncation the divisor to 32-bit" +coccilib.org.print_todo(p[0], msg) + +@script:python depends on report@ +p << r.p; +@@ + +msg="WARNING: WARNING: do_div() does a 64-by-32 division, which may truncation the divisor to 32-bit" +coccilib.report.print_report(p[0], msg) -- 1.8.3.1