From mboxrd@z Thu Jan 1 00:00:00 1970 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 52626C433EF for ; Mon, 30 May 2022 10:01:29 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3CE9E843B4; Mon, 30 May 2022 12:01:06 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.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=google.com header.i=@google.com header.b="VlnaO56R"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 209DD843B3; Mon, 30 May 2022 12:00:44 +0200 (CEST) Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) (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 A375E843A4 for ; Mon, 30 May 2022 12:00:38 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=3xZWUYgYKBl46O8QHHCKKCHA.8KIQ-7KKPHEOPO.9AJT.9A@flex--ascull.bounces.google.com Received: by mail-wm1-x34a.google.com with SMTP id k7-20020a05600c1c8700b003974d762928so4326991wms.7 for ; Mon, 30 May 2022 03:00:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=WKuKb3vUEawW8TkuuAQtK4QmQtWJCtyqRINDRUaRdC0=; b=VlnaO56R+qnToIWFzRv7VFkIHSJrNfqyI7DuDLvZTVnwjuS3lauY4XtILkfCQYIbie yenbUS8YAivHpeYS6kjTgeJw1JKe8txPthOn886L276JPYcxLESkGsPw7kopCgQZMOFz naiuKeI+mN03GhA5bF9MnnCcqUZ2E+KrMf3v5eUPqBs+fW5F4nTPTzgl3HRDHKLdQ6Q2 cpVDQu8xsuI26jezXhUhoces79e0/wd3nZPy8sMim30oqRYs4owwRy3cpLaziyOFnP36 aZYXAiTS+LHBICJsiCKFsTr+CMPEx2TBdpBVzP+nKR4zpRmzO2gqVMwELARpBDKdk2nd NRKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=WKuKb3vUEawW8TkuuAQtK4QmQtWJCtyqRINDRUaRdC0=; b=s6BxsLPCn2dAEQZkBKlnYSVF+bsMNh7Zvd2+fUIZAfSLJaTcoZS87uoTkKYV/YxUUZ ecc5H0VqLs+jHC7zbgEghjpyPW7MaGySRcWStyiv8U7DKIRqaC2eutfspasA50+xaFee bNLhvXxbbu0jx29sOGaoSq/FW9x6f0p6Qc29t/zCBh1tZojfZ6P3Apxy7KojWxY2uW2e sXB2QidlOETaJTO6EmNEsgUCEKi8mCTwcMO6LPAWiZhvN/7NJUMSbi7+x+254RxO2Ydq Luzm4jS+rn9VSplRMSq/0drZ6BiyWkg7yFUZUYUbV0dfXz/KbxtyV8+JHWDJygEzEIfV oMrw== X-Gm-Message-State: AOAM533bYl51WbqzBE5pg7dPbftFy/sbukK2UsgPxwgaOzSVwSPPSsrR ACJjSOudZmP7wFxbnCuw0mzg+XO3zEBewfbHXQD8l156Kkp4bkPeDRmwXj5LJ7Drjtt9QneceHO nsRP38Ae/X8X4K63CpPCEd6JgwsBhetM4FdDW3/oGXkZg09113GdKTMNW2bg= X-Google-Smtp-Source: ABdhPJxVjYdW3Ex/gAfAZZOghN+Hbc8/vST0NEA2PB4xo5ogiYbkNAm7EJjUuhkJcpRFB6B8oKVzHhfYe2w= X-Received: from ascull.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1510]) (user=ascull job=sendgmr) by 2002:a1c:ac89:0:b0:393:fbb4:f52c with SMTP id v131-20020a1cac89000000b00393fbb4f52cmr687744wme.0.1653904837631; Mon, 30 May 2022 03:00:37 -0700 (PDT) Date: Mon, 30 May 2022 10:00:05 +0000 In-Reply-To: <20220530100013.3753780-1-ascull@google.com> Message-Id: <20220530100013.3753780-6-ascull@google.com> Mime-Version: 1.0 References: <20220530100013.3753780-1-ascull@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH v3 05/13] sandbox: Add support for Address Sanitizer From: Andrew Scull To: u-boot@lists.denx.de Cc: sjg@chromium.org, trini@konsulko.com, xypron.glpk@gmx.de, jonbottarini@google.com, seanga2@gmail.com, Andrew Scull Content-Type: text/plain; charset="UTF-8" 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 Add CONFIG_ASAN to build with the Address Sanitizer. This only works with the sandbox so the config is likewise dependent. The resulting executable will have ASAN instrumentation, including the leak detector that can be disabled with the ASAN_OPTIONS environment variable: ASAN_OPTIONS=detect_leaks=0 ./u-boot Since u-boot uses its own dlmalloc, dynamic allocations aren't automatically instrumented, but stack variables and globals are. Instrumentation could be added to dlmalloc to poison and unpoison memory as it is allocated and deallocated, and to introduce redzones between allocations. Alternatively, the sandbox may be able to play games with the system allocator and somehow still keep the required memory abstraction. No effort to address dynamic allocation is made by this patch. The config is not yet enabled for any targets by default. Signed-off-by: Andrew Scull Reviewed-by: Simon Glass --- Kconfig | 7 +++++++ arch/sandbox/config.mk | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Kconfig b/Kconfig index 797038b037..2257aca97f 100644 --- a/Kconfig +++ b/Kconfig @@ -154,6 +154,13 @@ config CC_COVERAGE Enabling this option will pass "--coverage" to gcc to compile and link code instrumented for coverage analysis. +config ASAN + bool "Enable AddressSanitizer" + depends on SANDBOX + help + Enables AddressSanitizer to discover out-of-bounds accesses, + use-after-free, double-free and memory leaks. + config CC_HAS_ASM_INLINE def_bool $(success,echo 'void foo(void) { asm inline (""); }' | $(CC) -x c - -c -o /dev/null) diff --git a/arch/sandbox/config.mk b/arch/sandbox/config.mk index f3d3af6611..410603252e 100644 --- a/arch/sandbox/config.mk +++ b/arch/sandbox/config.mk @@ -15,8 +15,16 @@ PLATFORM_LIBS += $(shell $(SDL_CONFIG) --libs) PLATFORM_CPPFLAGS += $(shell $(SDL_CONFIG) --cflags) endif +SANITIZERS := +ifdef CONFIG_ASAN +SANITIZERS += -fsanitize=address +endif +KBUILD_CFLAGS += $(SANITIZERS) + cmd_u-boot__ = $(CC) -o $@ -Wl,-T u-boot.lds $(u-boot-init) \ - $(KBUILD_LDFLAGS:%=-Wl,%)$(LTO_FINAL_LDFLAGS) \ + $(KBUILD_LDFLAGS:%=-Wl,%) \ + $(SANITIZERS) \ + $(LTO_FINAL_LDFLAGS) \ -Wl,--whole-archive \ $(u-boot-main) \ $(u-boot-keep-syms-lto) \ @@ -24,7 +32,9 @@ cmd_u-boot__ = $(CC) -o $@ -Wl,-T u-boot.lds $(u-boot-init) \ $(PLATFORM_LIBS) -Wl,-Map -Wl,u-boot.map cmd_u-boot-spl = (cd $(obj) && $(CC) -o $(SPL_BIN) -Wl,-T u-boot-spl.lds \ - $(KBUILD_LDFLAGS:%=-Wl,%) $(LTO_FINAL_LDFLAGS) \ + $(KBUILD_LDFLAGS:%=-Wl,%) \ + $(SANITIZERS) \ + $(LTO_FINAL_LDFLAGS) \ $(patsubst $(obj)/%,%,$(u-boot-spl-init)) \ -Wl,--whole-archive \ $(patsubst $(obj)/%,%,$(u-boot-spl-main)) \ -- 2.36.1.124.g0e6072fb45-goog