From patchwork Wed Mar 30 21:49:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Schmitt X-Patchwork-Id: 12796412 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 778A6C433FE for ; Wed, 30 Mar 2022 21:49:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351374AbiC3VvZ (ORCPT ); Wed, 30 Mar 2022 17:51:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351367AbiC3VvZ (ORCPT ); Wed, 30 Mar 2022 17:51:25 -0400 Received: from mail-oo1-xc2c.google.com (mail-oo1-xc2c.google.com [IPv6:2607:f8b0:4864:20::c2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01F254ECE9; Wed, 30 Mar 2022 14:49:39 -0700 (PDT) Received: by mail-oo1-xc2c.google.com with SMTP id v19-20020a056820101300b0032488bb70f5so3838919oor.5; Wed, 30 Mar 2022 14:49:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=rgtOCaQYJg3zKcFPdllEeiUjgiabRKTrH5ORYrE4T/w=; b=hcRkM3UUJebrww/R5w9mUzbe4kbD7nt8Zr27Ne3DO2YKmNtyuBhz7MdfCl7QN9pr1D p63Ln8FOd31sVxrxoAo1uiMPRE8nRqiSuFQf6SFMvEOSFOu+ouzalkWbW0gNGOI0yQUY 9sVh6K6DMQ6mgi2LzfhO3DslOGMXYCnUM8GL4OvwzHclSR/J4xXUXaA2l6YBYM3lvmg9 YM/st1cfT2fSt/TDAIJQZvX8ugc7IgzXs3SPFH/amVDI4+euD8VgKR3xncVgSbTNF+8m lwW9a6fkepdrD8e5r1rDJkNqoLeBMRRP497JdMNpDh5pP8recQEFKuAr5+TUbzkI6Yc4 YcWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=rgtOCaQYJg3zKcFPdllEeiUjgiabRKTrH5ORYrE4T/w=; b=KQeeEE5fW9+tZXqV+QfbQk/B7WAqPdRGrL29oYSkPoyQHiFUjQkRpWYtNaQIBob+eA OSzScax7Q26sV/QT1vZ6DZ7Wq9XA2wxoB4BvBiUTl27fMj5oKhjC72nXHSIuk7/LZpq7 eXDIJHFEHy8wOHXnSBYKDK3KQCXfDTYolA6eTNTwen7XqGZjv+jzxnCMy4Zon6Kd2gmi Wx9ExsM9HXH5YlS4/lXJJR0lP6aEDLECdzzmfNj0w0TS3Mw6Z9JD6HSt1pKucoa4h5JD vVpxP3xIMqEvVGsyhza782LTWwmEQWSA8Bq4a5ZNiD5FTUTr5Z8YEn7rm8Sl0vyyu0oD JRLA== X-Gm-Message-State: AOAM530qBSJw2fZKQ7bGlDK2mIhNXD6Z8wutAxp+C6bhYwePbntUJdrp WqFG3AkWNnYsUNMksPKjqNI= X-Google-Smtp-Source: ABdhPJyslGYd+OW+6Ie3G5pBE4WtxdK370h79rqT9IOF4vx5o9fKxTiYz+BQffxhQlVGrM1qsRu15A== X-Received: by 2002:a4a:3c47:0:b0:320:f0d5:25a7 with SMTP id p7-20020a4a3c47000000b00320f0d525a7mr3914783oof.58.1648676978240; Wed, 30 Mar 2022 14:49:38 -0700 (PDT) Received: from marsc.168.1.7 ([2804:30c:b6b:3900:e3fc:1545:cb91:17fb]) by smtp.gmail.com with ESMTPSA id q40-20020a056830442800b005c9274907a0sm11613815otv.10.2022.03.30.14.49.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Mar 2022 14:49:37 -0700 (PDT) Date: Wed, 30 Mar 2022 18:49:33 -0300 From: Marcelo Schmitt To: corbet@lwn.net, mchehab+huawei@kernel.org, dlatypov@google.com, davidgow@google.com Cc: linux-doc@vger.kernel.org, linux-sparse@vger.kernel.org, cocci@inria.fr, smatch@vger.kernel.org, linux-kernel@vger.kernel.org, skhan@linuxfoundation.org, dan.carpenter@oracle.com, julia.lawall@inria.fr Subject: [PATCH v3 1/2] Documentation: dev-tools: Add a section for static analysis tools Message-ID: <7d793c1b9f87d9cb8ac0e858e561e108c2bf0176.1648674305.git.marcelo.schmitt1@gmail.com> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Complement the Kernel Testing Guide documentation page by adding a section about static analysis tools. Signed-off-by: Marcelo Schmitt Acked-by: Daniel Latypov Acked-by: Dan Carpenter Acked-by: Julia Lawall Reviewed-by: David Gow Reviewed-by: Shuah Khan --- Change log v2 -> v3: - Added Julia's acknowledgment tag Change log v1 -> v2: - Brought generic tool characteristics to the intro paragraph - Made explicit that these tools run at compile time - Added a note of caution about false positives - Updated Coccinelle info to make it sound better and be more skimmable Documentation/dev-tools/testing-overview.rst | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Documentation/dev-tools/testing-overview.rst b/Documentation/dev-tools/testing-overview.rst index 65feb81edb14..b5e02dd3fd94 100644 --- a/Documentation/dev-tools/testing-overview.rst +++ b/Documentation/dev-tools/testing-overview.rst @@ -115,3 +115,34 @@ that none of these errors are occurring during the test. Some of these tools integrate with KUnit or kselftest and will automatically fail tests if an issue is detected. +Static Analysis Tools +===================== + +In addition to testing a running kernel, one can also analyze kernel source code +directly (**at compile time**) using **static analysis** tools. The tools +commonly used in the kernel allow one to inspect the whole source tree or just +specific files within it. They make it easier to detect and fix problems during +the development process. + +Sparse can help test the kernel by performing type-checking, lock checking, +value range checking, in addition to reporting various errors and warnings while +examining the code. See the Documentation/dev-tools/sparse.rst documentation +page for details on how to use it. + +Smatch extends Sparse and provides additional checks for programming logic +mistakes such as missing breaks in switch statements, unused return values on +error checking, forgetting to set an error code in the return of an error path, +etc. Smatch also has tests against more serious issues such as integer +overflows, null pointer dereferences, and memory leaks. See the project page at +http://smatch.sourceforge.net/. + +Coccinelle is another static analyzer at our disposal. Coccinelle is often used +to aid refactoring and collateral evolution of source code, but it can also help +to avoid certain bugs that occur in common code patterns. The types of tests +available include API tests, tests for correct usage of kernel iterators, checks +for the soundness of free operations, analysis of locking behavior, and further +tests known to help keep consistent kernel usage. See the +Documentation/dev-tools/coccinelle.rst documentation page for details. + +Beware, though, that static analysis tools suffer from **false positives**. +Errors and warns need to be evaluated carefully before attempting to fix them. From patchwork Wed Mar 30 21:49:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Schmitt X-Patchwork-Id: 12796413 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 613D6C433F5 for ; Wed, 30 Mar 2022 21:50:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351389AbiC3Vvw (ORCPT ); Wed, 30 Mar 2022 17:51:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351386AbiC3Vvv (ORCPT ); Wed, 30 Mar 2022 17:51:51 -0400 Received: from mail-ot1-x32a.google.com (mail-ot1-x32a.google.com [IPv6:2607:f8b0:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2029452E4A; Wed, 30 Mar 2022 14:50:05 -0700 (PDT) Received: by mail-ot1-x32a.google.com with SMTP id a17-20020a9d3e11000000b005cb483c500dso15841487otd.6; Wed, 30 Mar 2022 14:50:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=/Bthg0jq8/Diwhpq69bZsSJ8jzbwEA8PqoPE66yQfog=; b=Nfga8Hiai6jnPWHrcn37JnCAYeEsSoYIS9lAUi9RUj2zZYm6zwUuleYAr7hSaiQ+g1 ezHH1BNKWxE5sAnlDb/uVvp0fX1dtnGpx8Y+6IFrxHZxQaJY2tyB3TivVEKNXAe6UIsU Hx99eHfTEChpbiQP+3UPY/45nATjnpWrkSQXHOos+OBwIqEa6Z/H1Cf+fmpQCy7PF/y6 r8aMcAl4DO8EOa36fBm9jnaV19bmtTp6vEv6abQBDv+tXv1DrOf/J+I3eH28D9Ru3QoD ucw4pTnSDYCTLLdjqV7iWlyghBr7hm1mpL+ZqHkcsQVfU6W9/AbMFC+hyAwt5tVsgDp1 UVcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=/Bthg0jq8/Diwhpq69bZsSJ8jzbwEA8PqoPE66yQfog=; b=gwuwp4i137raIWUEryehgHk+kJpAvsL9tSWhDb8clvF9riz0tgmwLyG2sxrGORdrgF yZB7UDhwcASsux+9i5YWvf0Xk1JlRtDjnEAR7Y1rVxIO8/OaUY7+DaZvIpcrQbXOudl6 JuuonlfIndE2HW2nNfIM0I3SbjYo0Etgr0IQ4b/og1oLMPIYITAtkYJt4Ljwlfjcm6ls l9DONRrbe+qT1zya6ug4qqJFG3lxQVrxuX72VY623Culv6TqKc0cXt5KrHY7WX9b6Wnf fPjr1DlSEOsSOwne/QJjTkWnMxkZfg6LT6/4Dyw4N2deve/TtX9FwpuvuqK0xHlbrV2T 4iKQ== X-Gm-Message-State: AOAM530un3pnM8oawvKnsnWl5W1osTawdHRJwul/fOQr2NpVihO8bzWb hyBHXvrdZu33u6nyA+SZqew= X-Google-Smtp-Source: ABdhPJzYJPepEvTuy6SmKbubOCp4UcpWJ95SWPN6CgROFQTdx2QTEQRmi4KeYdD9Ta8dyzx18zTjfQ== X-Received: by 2002:a05:6830:2478:b0:5c9:289c:597f with SMTP id x56-20020a056830247800b005c9289c597fmr4244500otr.130.1648677004423; Wed, 30 Mar 2022 14:50:04 -0700 (PDT) Received: from marsc.168.1.7 ([2804:30c:b6b:3900:e3fc:1545:cb91:17fb]) by smtp.gmail.com with ESMTPSA id w4-20020a4adec4000000b0032109de628esm10535874oou.6.2022.03.30.14.50.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Mar 2022 14:50:03 -0700 (PDT) Date: Wed, 30 Mar 2022 18:49:59 -0300 From: Marcelo Schmitt To: corbet@lwn.net, mchehab+huawei@kernel.org, dlatypov@google.com, davidgow@google.com Cc: linux-doc@vger.kernel.org, linux-sparse@vger.kernel.org, cocci@inria.fr, smatch@vger.kernel.org, linux-kernel@vger.kernel.org, skhan@linuxfoundation.org, dan.carpenter@oracle.com, julia.lawall@inria.fr Subject: [PATCH v3 2/2] Documentation: dev-tools: Enhance static analysis section with discussion Message-ID: <62f461a20600b95e694016c4e5348ef2e260fa87.1648674305.git.marcelo.schmitt1@gmail.com> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Enhance the static analysis tools section with a discussion on when to use each of them. This was mainly taken from Dan Carpenter and Julia Lawall's comments on a previous documentation patch for static analysis tools. Lore: https://lore.kernel.org/linux-doc/20220329090911.GX3293@kadam/T/#mb97770c8e938095aadc3ee08f4ac7fe32ae386e6 Signed-off-by: Marcelo Schmitt Acked-by: David Gow Cc: Dan Carpenter Cc: Julia Lawall --- Change log v2 -> v3: - Changed the paragraph about Sparse to make it sound better (hopefully) - Minor adjusts to make the considerations about Coccinelle sound better and be precise Documentation/dev-tools/testing-overview.rst | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Documentation/dev-tools/testing-overview.rst b/Documentation/dev-tools/testing-overview.rst index b5e02dd3fd94..0aaf6ea53608 100644 --- a/Documentation/dev-tools/testing-overview.rst +++ b/Documentation/dev-tools/testing-overview.rst @@ -146,3 +146,35 @@ Documentation/dev-tools/coccinelle.rst documentation page for details. Beware, though, that static analysis tools suffer from **false positives**. Errors and warns need to be evaluated carefully before attempting to fix them. + +When to use Sparse and Smatch +----------------------------- + +Sparse does type checking, such as verifying that annotated variables do not +cause endianness bugs, detecting places that use ``__user`` pointers improperly, +and analyzing the compatibility of symbol initializers. + +Smatch does flow analysis and, if allowed to build the function database, it +also does cross function analysis. Smatch tries to answer questions like where +is this buffer allocated? How big is it? Can this index be controlled by the +user? Is this variable larger than that variable? + +It's generally easier to write checks in Smatch than it is to write checks in +Sparse. Nevertheless, there are some overlaps between Sparse and Smatch checks. + +Strong points of Smatch and Coccinelle +-------------------------------------- + +Coccinelle is probably the easiest for writing checks. It works before the +pre-processor so it's easier to check for bugs in macros using Coccinelle. +Coccinelle also creates patches for you, which no other tool does. + +For example, with Coccinelle you can do a mass conversion from +``kmalloc(x * size, GFP_KERNEL)`` to ``kmalloc_array(x, size, GFP_KERNEL)``, and +that's really useful. If you just created a Smatch warning and try to push the +work of converting on to the maintainers they would be annoyed. You'd have to +argue about each warning if can really overflow or not. + +Coccinelle does no analysis of variable values, which is the strong point of +Smatch. On the other hand, Coccinelle allows you to do simple things in a simple +way.