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 phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CDF26C433F5 for ; Fri, 27 May 2022 07:14:19 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 486C984099; Fri, 27 May 2022 09:14:17 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; secure) header.d=gmx.net header.i=@gmx.net header.b="Oaz2wgVr"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 80D83842C4; Fri, 27 May 2022 09:14:14 +0200 (CEST) Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 2C38F83AD1 for ; Fri, 27 May 2022 09:14:11 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=xypron.glpk@gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1653635647; bh=P20ExJNnebkkQ+CHBkBYPbJjn/LbnS5AzJK0K3YZT3A=; h=X-UI-Sender-Class:Date:Subject:To:Cc:References:From:In-Reply-To; b=Oaz2wgVr/e0dOdk3E/dKwSgjJBzeLbpjR4Jp+EgiA9LtJwcrpp2mfszfEBCGtujdb 8IZ0lBzhcXykNFy5Dn4qwiu0y4hKlgjnAVjuahqZSczO5CZc0BNEPLVQPCc4ygsCQL kKE55RygGWCfw+ifHcHYNoCwdl7wO3FGkKGJGkdg= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.123.94] ([62.143.94.109]) by mail.gmx.net (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MSc1B-1oNFSA1pqD-00T0Ew; Fri, 27 May 2022 09:14:07 +0200 Message-ID: Date: Fri, 27 May 2022 09:14:02 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: [PATCH] doc: sandbox: Add additional valgrind documentation Content-Language: en-US To: Sean Anderson Cc: Simon Glass , Andrew Scull , Tom Rini , u-boot@lists.denx.de References: <20220527033646.670579-1-seanga2@gmail.com> From: Heinrich Schuchardt In-Reply-To: <20220527033646.670579-1-seanga2@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:E9X32s3i4pOtpAZqLkn24SI0xr/PpHNnHXtizb5sh4Nf1ScrxO0 DsH9qj3p996C/g+FOLw65koounIjj2UWEWQBz6dCfM3cjvAE+4d/REYdyUPRfDMytU3RzuJ kwKTJOlGjPOnY1zQaMiyfS8epPQ2bZEZwxgEkH/Xp2eMRmJ6Rj94Xysn3ANfKX6d9hQqdqb mOa5RMoKuXhjTvnwfJaJA== X-UI-Out-Filterresults: notjunk:1;V03:K0:6KCeA8Sqezc=:/p+fYoF8JfvQ92uq2oEzRy MsBu2N47OB2Pzh7qeILWiELH+cQeCVfakeFQg3GtUl5qhq8raSWyeWdEe78NmX536vbP9VgHj ktvEfZ1oS4xZXJWYxZBL2yrRpn+4V/0B8Z0YWOBss0HPvPj2iarRSC9+GD/lyA7loiSfQp+q+ NyNS1u+ttgZryH0EsqAGLqPjq6Q8KdYmMEoZV4b7VK9/4ITmxVCU8ni/Ln5JwmMd/QHRtEXn5 6tivZQ7BE6uNcmkQ9uc+SK0kkL4q5AICJi/w25QjLrpJRuHhO7exZqOy2sCRCZfNerO9Yxuhk 0qcA65FJqUjpDpkJqstbheIxwSf0SarEg2uSCtGiO77hqBxB6fOp74kc7jhEqiwpXMEbz1Rkk B1rS7dEBwInEBwjoyi+ocQBrW7LonuRLB6icbpvQMsrRMRJEl+jDrTFGmVUEuQkjgyQoBh/oa 0wMyOq3jrcu6eH1uUjK3xgVIFRUf/lH84URs4uQuKkdT1ajDuXN1KLBc5/EjGww7PZHhivp4Y QNQnjHRmjQ0C2rgTmNfrbywCyqtfAearlIOK9MkeFExuLD/qFYEXDNrjJbRGYyt/2ZJDlKMWa WOAS2vxioDLNyK2zLB09yyTvq2Y+Ze+Yb92NvwjAM7PQVN3O8vucf2BhHCvUWh5zWvpPfAzTW QtLVSOAeWzd3re1TzhWkODPejJuEJCJbVE9Fni/yq7uL4vmsUimx3mrXBF+EK+rib2k3Upene zbTerSKKw7fEVOrpCImNnTWcy9CUXd1+Q0Lf7Vlb8lzCIuBhZyXHjd3g78qoBsUKMbe+gxAcr LTsVFLpNuBU57ZLyW52FXVdrqQ8G+ev3cnPEo3DfsguIcwsvx+8QhAc7OxvgQ0X5hCMnPJFew 5b343gUUMoL5fBaTtbeTI7avMHwkQx6M9GR2sf5tbRpDEgsz4O/t98AOTK3OxNpqwQUzo/jTK n1vahxkSOKFF0dQGp6yuPfZMDRicLCul9YVstGoiEOyGmF5duPTYDrNqyNG96bMETMdBcBkX0 ox1YT6DoOTLLDtZ8gUjnbmVWt7kOjUXG7yDDszv2hm5WMgqw3sOUgNLyBUn4YHFx16GKp0pz9 vQBkFfDNfTvMfsMv6I6QV6Bamr8J/GA6SPG0Dn1jLAYTjrFjH4dOupg4Q== X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean On 5/27/22 05:36, Sean Anderson wrote: > This document some additional options which can be used with valgrind, a= s Thanks for enhancing this document nits %s/document/documents/ > well as directions for future work. It also fixes up inline literals to > actually be inline literals (and not italics). The content of this > documentation is primarily adapted from [1]. > > [1] https://lore.kernel.org/u-boot/57cb4b49-fa30-1194-9ac3-faa53e8033bd@= gmail.com/ > > Signed-off-by: Sean Anderson > --- > > doc/arch/sandbox.rst | 65 +++++++++++++++++++++++++++++++++++++++++--- > 1 file changed, 61 insertions(+), 4 deletions(-) > > diff --git a/doc/arch/sandbox.rst b/doc/arch/sandbox.rst > index e1119492b4..cd5090be71 100644 > --- a/doc/arch/sandbox.rst > +++ b/doc/arch/sandbox.rst > @@ -479,19 +479,76 @@ It is possible to run U-Boot under valgrind to che= ck memory allocations:: > > valgrind ./u-boot > > -For more detailed results, enable `CONFIG_VALGRIND`. There are many fal= se > -positives due to `malloc` itself. Suppress these with:: > +For more detailed results, enable ``CONFIG_VALGRIND``. There are many f= alse > +positives due to ``malloc`` itself. Suppress these with:: What do you mean by 'malloc itself'? Is it the internal implementation of malloc? Or is it the act of calling malloc()? This paragraph should explain what CONFIG_VALGRIND does: The sandbox allocates a memory pool via mmap(). U-Boot's internal malloc() and free() work on this memory pool. Custom allocators and deallocators are by default invisible to valgrind. It is CONFIG_VARLGRIND=3Dy that exposes U-Boot's malloc() and free() to valgrind= . If I understand include/valgrind/valgrind.h correctly, it injects placeholder assembler code that valgrind can replace by library calls into valgrind itself when loading the U-Boot binary. I miss a statement indicating that the sandbox on RISC-V has no valgrind support, yet. The U-Boot code does not use VALGRIND_MEMPOOL* macros to indicate which memory pools it manages. Is this the reason for the problems we are facing= ? > > valgrind --suppressions=3Dscripts/u-boot.supp ./u-boot > > If you are running sandbox SPL or TPL, then valgrind will not by defau= lt > notice when U-Boot jumps from TPL to SPL, or from SPL to U-Boot proper= . To > -fix this, use `--trace-children=3Dyes`. To show who alloc'd some troubl= esome > -memory, use `--track-origins=3Dyes`. To uncover possible errors, try ru= nning all > +fix this, use ``--trace-children=3Dyes``. To show who alloc'd some trou= blesome > +memory, use ``--track-origins=3Dyes``. To uncover possible errors, try = running all > unit tests with:: > > valgrind --track-origins=3Dyes --suppressions=3Dscripts/u-boot.sup= p ./u-boot -Tc 'ut all' I would prefer a list like: =2D-suppressions=3Dscripts/u-boot.supp Suppress false positives due to the internal implementation of malloc =2D-trace-children=3Dyes Let valgrind consider the progression from TPL to SPL to main U-Boot .... > > +Additional options > +^^^^^^^^^^^^^^^^^^ > + > +The following options are useful in addition to the above examples: > + > +* ``--error-limit=3Dno`` will enable printing more than 1000 errors in = a > + single session. > +* ``--vgdb=3Dyes --vgdb-error=3D0`` will let you use gdb to attach like= :: > + > + gdb -ex "target remote | vgdb" u-boot > + > + This is very helpful for inspecting the program state when there is > + an error. > +* Passing ``-t cooked`` to U-Boot will keep the console in a sane state= if you > + terminate it early (instead of having to run tset). > + > +Future work > +^^^^^^^^^^^ > + > +The biggest limitation to the current approach is that > +supressions don't "un-taint" uninitialized memory accesses. Currently, = I > +have dlmalloc's reads its bookkeeping information marked as a "red This documentation does not have a single named author. The pronoun "I" has no reference point in this context. "its" does not refer to anything. > +zone." This means that all reads to it are marked as illegal by "it" has no clear reference point. > +valgrind. This is fine for regular code, but dlmalloc really does need > +to access this area, so we suppress its violations. However, if dlmallo= c > +then passes a result calculated from a "tainted" access, that result is > +still tainted. So the first accessor will raise a warning. This means > +that every construct like > + > +.. code-block:: > + > + foo =3D malloc(sizeof(*foo)); > + if (!foo) > + return -ENOMEM; > + > +will raise a warning when we check the result of malloc. Whoops. > + > +There are three ways (as I see it) to address this: %s/(as I see it)// Best regards Heinrich > + > +* Don't mark dlmalloc bookkeeping information as a red zone. This is th= e > + simplest solution, but reduces the power of valgrind immensely, since > + we can no longer determine that (e.g.) access past the end of an arra= y > + is undefined. > +* Implement red zones properly. This would involve growing every > + allocation by a fixed amount (16 bytes or so) and then using that > + extra space for a real red zone that neither regular code nor dlmallo= c > + needs to access. Unfortunately, this would probably some fairly > + intensive surgery to dlmalloc to add/remove the offset appropriately. > +* Mark bookkeeping information as valid before we use it in dlmalloc, > + and then mark it invalid before returning. This would be the most > + correct, but it would be very tricky to implement since there are so > + many code paths to mark. I think it would be the most effort out of > + the three options here. > + > +Until one of the above options are implemented, it will remain difficul= t > +to sift through the massive amount of spurious warnings. > + > Testing > ------- >