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 9DFC6C433F5 for ; Fri, 27 May 2022 13:58:47 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 7BE3A84341; Fri, 27 May 2022 15:58:34 +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="jIGHBTym"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 45547842C4; Fri, 27 May 2022 15:58:30 +0200 (CEST) Received: from mail-qt1-x82b.google.com (mail-qt1-x82b.google.com [IPv6:2607:f8b0:4864:20::82b]) (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 54DAE83AD1 for ; Fri, 27 May 2022 15:58:26 +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-x82b.google.com with SMTP id f35so5312849qtb.11 for ; Fri, 27 May 2022 06:58:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=dYJNcYeZZlyB4mfB4+S4BQn0B8+PgNT16fEF2zntaG4=; b=jIGHBTymj/W1JFtBjr4/JBSdubhOB9ty7clAjAsDL3LKsL+95SdGbQgZCi4sc51CDV seLggoQ5HCR/r1QWJzmxq/JWVOTu6U4QwgzbEHrLojV/dToXq+m4VQfIxkB/ewsbKFXD QPlakWDUzvUbln1dkFyM1P7ebMv33qCNO6GwR/y1+yyb3u8Sx6Ob4Sgqg4uzQlhqva6G UQiCG1vxN1kp50UyLyUO/8lFulaav64CNeIwcDCP04Xi4jrQ58oBzFrCmeKqcWYUtyVG HMXGPgAlGXwcxAv5ykWqOE4uLRMn+nVfHScO+5HsxmrfoirVBYuDdvnH+X6rnwGsjUBd C2zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=dYJNcYeZZlyB4mfB4+S4BQn0B8+PgNT16fEF2zntaG4=; b=mhQ9DH1zqQDX2HTO2GVccEgHQXldzwgWTKRPEu2nIgECj0QQdY1WPgMtVa3CRn898D uSq/Z7W8CDVR13+x+sx1BIRdLcrD/G/jQTG4ftSfaihOIyCBj7kAgOdsYV2SCLzhKk9P jnDixFIJ6qiS+9bnFP+tUWAQDVTZMA3RFqkP78ZRUQdcrnKQ/dmQmyOzUDIJ7Ji+fmuT X6xEwyT7cNTfFe/rcv7p7iygZRR/mzMbMZLBpxRL0gDjPT2e9RhK1v3TUxiWhSurH1TF 9ivA93P6fjXjZAdqpzBY0BrxTXcbC+qd1J7fIJnfrHfBJsyqzEe6LG3SrSOKZ3bOK9pn ba4A== X-Gm-Message-State: AOAM533TX9XWQrBevOlzjhggQVgB4/hIwkQl6Rqsg3XXCEhC2GHa63Ck RVLJzsh0HcJWqLdbO4T1LlsXfE+KkOA= X-Google-Smtp-Source: ABdhPJyB+n++z7Ru3+036Wp+9IzoFFsqijL1hTW8cZ+9cPrFCb72mOU2Bt9pHQbZsBDfHP2APw5bjQ== X-Received: by 2002:a05:622a:13cd:b0:2fa:eb66:b0 with SMTP id p13-20020a05622a13cd00b002faeb6600b0mr11111398qtk.348.1653659904908; Fri, 27 May 2022 06:58:24 -0700 (PDT) Received: from godwin.fios-router.home (pool-173-73-95-180.washdc.fios.verizon.net. [173.73.95.180]) by smtp.gmail.com with ESMTPSA id u128-20020a376086000000b006a366b4b197sm2857166qkb.75.2022.05.27.06.58.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 May 2022 06:58:24 -0700 (PDT) From: Sean Anderson To: u-boot@lists.denx.de Cc: Tom Rini , Heinrich Schuchardt , Simon Glass , Andrew Scull , Sean Anderson Subject: [PATCH v2 1/2] doc: sandbox: Add additional valgrind documentation Date: Fri, 27 May 2022 09:58:13 -0400 Message-Id: <20220527135814.682048-1-seanga2@gmail.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 This documents some additional options which can be used with valgrind, as 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 --- Changes in v2: - Reworked most documentation to address Heinrich's feedback doc/arch/sandbox.rst | 91 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 83 insertions(+), 8 deletions(-) diff --git a/doc/arch/sandbox.rst b/doc/arch/sandbox.rst index e1119492b4..ede760a965 100644 --- a/doc/arch/sandbox.rst +++ b/doc/arch/sandbox.rst @@ -479,18 +479,93 @@ It is possible to run U-Boot under valgrind to check memory allocations:: valgrind ./u-boot -For more detailed results, enable `CONFIG_VALGRIND`. There are many false -positives due to `malloc` itself. Suppress these with:: +However, this does not give very useful results. 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 invisible to valgrind by default. To +expose U-Boot's malloc() and free() to valgrind, enable ``CONFIG_VALGRIND``. +Enabling this option will inject placeholder assembler code which valgrind +interprets. This is used to annotate sections of memory as safe or unsafe, and +to inform valgrind about malloc()s and free()s. There are currently no standard +placeholder addembly sequences for RISC-V, so this option cannot be enabled on +that architecture. + +Malloc's bookeeping information is marked as unsafe by default. However, this +will generate many false positives when malloc itself accesses this information. +These warnings can be suppressed with:: valgrind --suppressions=scripts/u-boot.supp ./u-boot -If you are running sandbox SPL or TPL, then valgrind will not by default -notice when U-Boot jumps from TPL to SPL, or from SPL to U-Boot proper. To -fix this, use `--trace-children=yes`. To show who alloc'd some troublesome -memory, use `--track-origins=yes`. To uncover possible errors, try running all -unit tests with:: +Additionally, you may experience false positives if U-Boot is using a smaller +pointer size than your host architecture. This is because the pointers used by +U-Boot will only contain 32 bits of addressing information. When interpreted as +64-bit pointers, valgrind will think that they are not initialized properly. To +fix this, enable ``CONFIG_SANDBOX64`` (such as via ``sandbox64_defconfig``) +when running on a 64-bit host. - valgrind --track-origins=yes --suppressions=scripts/u-boot.supp ./u-boot -Tc 'ut all' +Additional options +^^^^^^^^^^^^^^^^^^ + +The following options are useful in addition to the above examples: + +* ``--trace-childen=yes`` will tell valgrind to keep tracking subprocesses, such + as when U-Boot jumps from TPL to SPL, or from SPL to U-Boot proper. +* ``--track-origins=yes`` will (for a small overhead) tell valgrind to keep + track of who allocated some troublesome memory. +* ``--error-limit=no`` will enable printing more than 1000 errors in a + single session. +* ``--vgdb=yes --vgdb-error=0`` 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 ``-Tc 'ut all'`` will cause U-Boot to run all unit tests + automatically. Note that not all unit tests will succeed in the default + configuration. +* 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, dlmalloc's bookkeeping +information is marked as a "red zone." This means that all reads to that zone +are marked as illegal by valgrind. This is fine for regular code, but dlmalloc +really does need to access this area, so we suppress its violations. However, if +dlmalloc 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 = malloc(sizeof(*foo)); + if (!foo) + return -ENOMEM; + +will raise a warning when we check the result of malloc. Whoops. + +There are at least four possible ways to address this: + +* Don't mark dlmalloc bookkeeping information as a red zone. This is the + simplest solution, but reduces the power of valgrind immensely, since we can + no longer determine that (e.g.) access past the end of an array 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 dlmalloc 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. +* Use the host malloc and free instead of U-Boot's custom allocator. This will + eliminate the need to annotate dlmalloc. However, using a different allocator + for sandbox will mean that bugs in dlmalloc will only be tested when running + on read (or emulated) hardware. + +Until one of the above options are implemented, it will remain difficult +to sift through the massive amount of spurious warnings. Testing ------- -- 2.35.1