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 830CAC433EF for ; Thu, 14 Apr 2022 14:00:53 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 1C95383ED7; Thu, 14 Apr 2022 16:00:34 +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="h2w4JpJn"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 85B5C83EAE; Thu, 14 Apr 2022 15:59:59 +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 A3B5283EB9 for ; Thu, 14 Apr 2022 15:59:55 +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=32yhYYgYKBqYGYIaRRMUUMRK.IUSa-HUUZROYZY.JKTd.JK@flex--ascull.bounces.google.com Received: by mail-wm1-x34a.google.com with SMTP id q6-20020a1cf306000000b0038c5726365aso2282090wmq.3 for ; Thu, 14 Apr 2022 06:59:55 -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=6iuv6/fTjfrrjTxBxaEC/gaCDs79fmJDKt84i2OImf8=; b=h2w4JpJn77y2/H7HsL1r3aGj+D08KvOPNnQ91XKzWVYBIaO44HYlKnxdgx1pr7eTIY 5Hu+aykNBlvs1hMSXXDsy9SCthFnymMHbBDQS1r1J64Nws/tvOUfsO1wV40LTGHJI0Nw zFvTSP4e9eZOGRKGjUlj62PlkBnuWm5omTc9jMH8uKAwRreFjMCEZADHOkqxiis1Zszs 0f2eIaT0hOJ6H7xmPCYQ1T9Z+HP4OVQ5P592pindNAP1oyZipkLAym3wpCXTP8QLVLX2 aNcVGdWxJs+/oh7aSaJypey0WH3Q+dqQhec8LPEfGyV185BHjl/g2ewK0JMz2L/oV4ef AQeg== 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=6iuv6/fTjfrrjTxBxaEC/gaCDs79fmJDKt84i2OImf8=; b=zJ6FQ7bgDlfnnsZ+eP19e2XuQJ+ZO9a3aemn01V5tJ6hEW9Hk9bWUasU/T4MEUNBVR ssFeyFKaZVtoWpdhTinuMR5ozIvSQdyMdEDmgUwOTzoWfpenTFSNanZkSYRbZd4Tvv07 AUQzUktz5SxtY4M/vJij1EwxJq5+v1EMjZd4DLmi/C+xtz0qwNn0wKaeJ/T1UUSYavOP 5a5OeHR3ZB3di+3sdhchWAVajAfdRLcuXRttXpgRWS6R2Fb1tY6zmXEL95Wq1DgH+IKt 9v8socKuqQ+PX/XEhbNoGDJs368r54f5qBghYpduTJ/OSE4ciDRTu4viTGsHm+DLPd/+ UTeA== X-Gm-Message-State: AOAM531d9bDiFusSMq1itVrGwi9clqFj7T3aljB7udwvxGX+ORLT2yMv I7CXnIglSOHuQc300X++10WsgxTWC0sEBUFZz+hpFNi+Y1pDyiloMDgvdOAsjkx8XOHSPQMqURJ LiAdn2GPCE+jaRwl9Azp3HdyN682Tg3dwUdzzYUy2IN3hJWmB3V76OzDDEZ8= X-Google-Smtp-Source: ABdhPJwthbmVYArGcynLSEl+XxZX2aH1DJEbWFKr8Opuf+TgFJZx7HUwNZCko41/ze9V3q2oRIF4CYlH1hc= X-Received: from ascull.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1510]) (user=ascull job=sendgmr) by 2002:a05:600c:3ac7:b0:38b:f9c6:27b8 with SMTP id d7-20020a05600c3ac700b0038bf9c627b8mr3271193wms.75.1649944795284; Thu, 14 Apr 2022 06:59:55 -0700 (PDT) Date: Thu, 14 Apr 2022 13:59:34 +0000 In-Reply-To: <20220414135941.1732585-1-ascull@google.com> Message-Id: <20220414135941.1732585-6-ascull@google.com> Mime-Version: 1.0 References: <20220414135941.1732585-1-ascull@google.com> X-Mailer: git-send-email 2.35.1.1178.g4f1659d476-goog Subject: [PATCH v2 05/12] sandbox: Add support for Address Sanitizer From: Andrew Scull To: u-boot@lists.denx.de Cc: sjg@chromium.org, xypron.glpk@gmx.de, 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. Signed-off-by: Andrew Scull Reviewed-by: Simon Glass --- Kconfig | 7 +++++++ arch/sandbox/config.mk | 8 ++++++++ configs/sandbox_defconfig | 1 + 3 files changed, 16 insertions(+) diff --git a/Kconfig b/Kconfig index 0ee3068b9a..a2577bcce6 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 c42de2ff27..d7ce66fb6c 100644 --- a/arch/sandbox/config.mk +++ b/arch/sandbox/config.mk @@ -15,7 +15,14 @@ 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) \ + $(SANITIZERS) \ $(LTO_FINAL_LDFLAGS) \ -Wl,--whole-archive \ $(u-boot-main) \ @@ -24,6 +31,7 @@ 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 \ + $(SANITIZERS) \ $(LTO_FINAL_LDFLAGS) \ $(patsubst $(obj)/%,%,$(u-boot-spl-init)) \ -Wl,--whole-archive \ diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig index cb8d590eb6..462a0afb2e 100644 --- a/configs/sandbox_defconfig +++ b/configs/sandbox_defconfig @@ -1,3 +1,4 @@ +CONFIG_ASAN=y CONFIG_SYS_TEXT_BASE=0 CONFIG_SYS_MALLOC_LEN=0x2000000 CONFIG_NR_DRAM_BANKS=1 -- 2.35.1.1178.g4f1659d476-goog