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 182F8C433F5 for ; Fri, 27 May 2022 13:26:07 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 190E583E8A; Fri, 27 May 2022 15:26:05 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="ByygGqZP"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 26D8383E31; Fri, 27 May 2022 15:26:02 +0200 (CEST) Received: from mail-qt1-x836.google.com (mail-qt1-x836.google.com [IPv6:2607:f8b0:4864:20::836]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 3540083E31 for ; Fri, 27 May 2022 15:25:58 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=seanga2@gmail.com Received: by mail-qt1-x836.google.com with SMTP id hh4so5263513qtb.10 for ; Fri, 27 May 2022 06:25:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=qwFlcd1LELz/IzdYTBORttJO70AXz8yKnnoJejTjvPA=; b=ByygGqZPEmPSxuqLgeRzf0UJPY+rmCiRn5G3mSp4tw1lTwJ2ytYxSUSnc92wLrIZRN nB9Z0kO70Ki/9BkCqswMF5cAJ7Rt9saOQlbzrS+he/Ge+i/+putGA4QHekvYZPbgMKLP 07B4f8v2+34Q7QI23PZUKJTYm5ul+tvtIpPO/aFMQEy2sMT+9ZzlH+gYin0UmD4Vbp48 8/UWCGI8dRc2gEVZ/v6y/5WA/kBxhv7sG/BF05Uaz3dFvW6IQZD8f6HCgKe4IfijDL+t 923Dk9wyUfG6ZLtBGIzvg/xmQ6D/2cY3EtvcY5is7QMU1IP3qn85jexLwU0E0/Fv5Ru0 xN5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=qwFlcd1LELz/IzdYTBORttJO70AXz8yKnnoJejTjvPA=; b=onnMXmatsiXdV+p3puvx+mRFVVhJaftgUSCPjO/2+hADgsWA7ubOWOXgKrnJ6A5n50 1NEZXsxr6PfFRUOCk3QFBM1+2vr0+AKOsPhOnAyKQCkPzY3jQBYWE+KFEulzr6nf+fnw SbZv7zID6Qctc7SYXhTammjs4BxB+r04k8uDfOyVtpMv+iCZ/fJEzCL8pKGY7XP79n0B 9VkzpxHuOcEq13IiZGy1fEoi8ofWcFEJGtRZqbTV7yHmcnAQYqW8axP54jam8zLzjPoV KBauQO/Uf/VJkbQd6M9KpyX37xA3aV6VuQmFIYPipPlKGic5fGJX+K84FMu4CcrSA06x 0VYA== X-Gm-Message-State: AOAM530SkGrN1IiOKomgi11XmCSlo19/ouk3krTIrjIQwesJPqizMEcX l7ihdTw4qEqYUABiHYkgT4W2iG8ffU0= X-Google-Smtp-Source: ABdhPJwtqe/An/jdJ5zQn5P1tUuPuVDNzKx3u8eXzD4r7+gJUzhQI0IlXhlHnqaNlEKCQUj8Mbrcwg== X-Received: by 2002:ac8:7d51:0:b0:2f9:4390:6bfd with SMTP id h17-20020ac87d51000000b002f943906bfdmr14386063qtb.325.1653657956683; Fri, 27 May 2022 06:25:56 -0700 (PDT) Received: from [192.168.1.201] (pool-173-73-95-180.washdc.fios.verizon.net. [173.73.95.180]) by smtp.googlemail.com with ESMTPSA id g18-20020ac84812000000b002f93aeaf770sm2348788qtq.92.2022.05.27.06.25.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 27 May 2022 06:25:56 -0700 (PDT) Subject: Re: [PATCH] doc: sandbox: Add additional valgrind documentation To: Heinrich Schuchardt Cc: Simon Glass , Andrew Scull , Tom Rini , u-boot@lists.denx.de References: <20220527033646.670579-1-seanga2@gmail.com> From: Sean Anderson Message-ID: <18afb7c6-60cd-8cff-b885-6f8ee2eed79b@gmail.com> Date: Fri, 27 May 2022 09:25:55 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: quoted-printable 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 3:14 AM, Heinrich Schuchardt wrote: > On 5/27/22 05:36, Sean Anderson wrote: >> This document some additional options which can be used with valgrind,= as >=20 > Thanks for enhancing this document >=20 > nits > %s/document/documents/ >=20 >> well as directions for future work. It also fixes up inline literals t= o >> 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-faa53e8033b= d@gmail.com/ >> >> Signed-off-by: Sean Anderson >> --- >> >> =C2=A0 doc/arch/sandbox.rst | 65 +++++++++++++++++++++++++++++++++++++= ++++--- >> =C2=A0 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 c= heck memory allocations:: >> >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 valgrind ./u-boot >> >> -For more detailed results, enable `CONFIG_VALGRIND`. There are many f= alse >> -positives due to `malloc` itself. Suppress these with:: >> +For more detailed results, enable ``CONFIG_VALGRIND``. There are many= false >> +positives due to ``malloc`` itself. Suppress these with:: >=20 > What do you mean by 'malloc itself'? Is it the internal implementation > of malloc? Or is it the act of calling malloc()? >=20 > This paragraph should explain what CONFIG_VALGRIND does: >=20 > 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 valgr= ind. >=20 > 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 believe this is correct. I will adapt your above description for v2. > I miss a statement indicating that the sandbox on RISC-V has no valgrin= d > support, yet. I was not aware of this. The Kconfig should probably be updated. > 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 fac= ing? >=20 >> >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 valgrind --suppressions=3Dscripts/u-boo= t.supp ./u-boot >> >> =C2=A0 If you are running sandbox SPL or TPL, then valgrind will not b= y default >> =C2=A0 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 trou= blesome >> -memory, use `--track-origins=3Dyes`. To uncover possible errors, try = running all >> +fix this, use ``--trace-children=3Dyes``. To show who alloc'd some tr= oublesome >> +memory, use ``--track-origins=3Dyes``. To uncover possible errors, tr= y running all >> =C2=A0 unit tests with:: >> >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 valgrind --track-origins=3Dyes --suppre= ssions=3Dscripts/u-boot.supp ./u-boot -Tc 'ut all' >=20 > I would prefer a list like: >=20 > --suppressions=3Dscripts/u-boot.supp > =C2=A0=C2=A0=C2=A0 Suppress false positives due to the internal implem= entation > =C2=A0=C2=A0=C2=A0 of malloc >=20 > --trace-children=3Dyes > =C2=A0=C2=A0=C2=A0 Let valgrind consider the progression from TPL to S= PL to main U-Boot >=20 I will merge this with the below options. >=20 >=20 >> >> +Additional options >> +^^^^^^^^^^^^^^^^^^ >> + >> +The following options are useful in addition to the above examples: >> + >> +* ``--error-limit=3Dno`` will enable printing more than 1000 errors i= n a >> +=C2=A0 single session. >> +* ``--vgdb=3Dyes --vgdb-error=3D0`` will let you use gdb to attach li= ke:: >> + >> +=C2=A0=C2=A0=C2=A0 gdb -ex "target remote | vgdb" u-boot >> + >> +=C2=A0 This is very helpful for inspecting the program state when the= re is >> +=C2=A0 an error. >> +* Passing ``-t cooked`` to U-Boot will keep the console in a sane sta= te if you >> +=C2=A0 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 >=20 > This documentation does not have a single named author. The pronoun "I"= > has no reference point in this context. >=20 > "its" does not refer to anything. Sorry, this is a bit unclear, the wording should be something like Currently, dlmalloc's bookkeeping information is marked as a "red zone" >=20 >> +zone." This means that all reads to it are marked as illegal by >=20 > "it" has no clear reference point. s/it/that area/ >=20 >> +valgrind. This is fine for regular code, but dlmalloc really does nee= d >> +to access this area, so we suppress its violations. However, if dlmal= loc >> +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:: >> + >> +=C2=A0=C2=A0=C2=A0 foo =3D malloc(sizeof(*foo)); >> +=C2=A0=C2=A0=C2=A0 if (!foo) >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 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: >=20 > %s/(as I see it)// >=20 Will update. Thanks for the feedback. --Sean >=20 >> + >> +* Don't mark dlmalloc bookkeeping information as a red zone. This is = the >> +=C2=A0 simplest solution, but reduces the power of valgrind immensely= , since >> +=C2=A0 we can no longer determine that (e.g.) access past the end of = an array >> +=C2=A0 is undefined. >> +* Implement red zones properly. This would involve growing every >> +=C2=A0 allocation by a fixed amount (16 bytes or so) and then using t= hat >> +=C2=A0 extra space for a real red zone that neither regular code nor = dlmalloc >> +=C2=A0 needs to access. Unfortunately, this would probably some fairl= y >> +=C2=A0 intensive surgery to dlmalloc to add/remove the offset appropr= iately. >> +* Mark bookkeeping information as valid before we use it in dlmalloc,= >> +=C2=A0 and then mark it invalid before returning. This would be the m= ost >> +=C2=A0 correct, but it would be very tricky to implement since there = are so >> +=C2=A0 many code paths to mark. I think it would be the most effort o= ut of >> +=C2=A0 the three options here. >> + >> +Until one of the above options are implemented, it will remain diffic= ult >> +to sift through the massive amount of spurious warnings. >> + >> =C2=A0 Testing >> =C2=A0 ------- >> >=20