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 CAA58C433FE for ; Thu, 7 Apr 2022 09:42:44 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id CC40483D61; Thu, 7 Apr 2022 11:42:24 +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="SxxE4Y12"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 990BA83C8F; Thu, 7 Apr 2022 11:41:50 +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 520B883C08 for ; Thu, 7 Apr 2022 11:41:41 +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=31LFOYgYKBosp7r900v33v0t.r319-q3380x787.st2C.st@flex--ascull.bounces.google.com Received: by mail-wm1-x34a.google.com with SMTP id n19-20020a05600c305300b0038e72cfc391so2710916wmh.9 for ; Thu, 07 Apr 2022 02:41:41 -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=gSVFziPHC5MJSv9YOltLymFMpNf0caA1I+97b7VBBw0=; b=SxxE4Y128ufWRZg3f4TVrZq3WCIKcEoiaxtZ9HrOWoN6bC6NS5ucJtx6turUS4eTJl DreVMv2UeHc1s0PQKrVBy99AY+gtAgP6psZCzSB3TSF0SWu+0vBRpyKXoW0ND45d1Njz lHpnX0uzEKQe8i7JP30YbImvuCEvU7U8U9IU3i/jgjjJHZ8C4zKZJf6oUZ5EKUJv/Uxd e0AMaJIRME495WanK9PWxSg0PCWdlcRcOQTHR32hfX9awvnxI3uOWjZtF4CYwYj+YFYw BpznmYQ5GjPE5pfwExQNpt3zBd8nN2j8TEcRHZQkUSNM5uwHwaQzn/9sCTpMbh8LNeYv q4mg== 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=gSVFziPHC5MJSv9YOltLymFMpNf0caA1I+97b7VBBw0=; b=avLjrkvNuCwkwcdDEVIBddxxlEFqUEel+ITJ6JN68FVNxm0KrSE9AGZKscmXdGgzy6 4ddSaPswh4I18BSgihzBrxLniTYE2wZi9HsLMhLZ5tiyZiRKh2JBtTFCoBoqF9qLteh9 KhUwBSWL5KvVEtNvG7UvOdX8LX07dj9Fl2dTY6VV5bTI0JKlHxXWv+wGLFAlbfB/qaoa KArbGOcdW2ih8IqR8lI35P6ju1/vpecs9ehLOWW8B96TNUdjDL+OImIajgDo394ZvA+Q aJCieDL2+rvGedLo/xt3pHWH0Txe9oZX8AsolGiLQUf/EFsV8Wsx2ZZvOVAIMFTJp/TK XXYw== X-Gm-Message-State: AOAM5327EZOBpYac3s9YdWoMHgHXkFhy8vafl/gLlB58/KhqIH15zcl4 mY1E2mE2ONDXJiv/21PQfBC2kL+8N5WhSMCD8+7DfTcSPelwW/UJvRjWfOEmikzKl6+s66QOHcP c+G3FmQVIjunvOQa0HahcpG5TRYRPS0RVqKrJa3lN/gkLNYOY8II+jHYo+MA= X-Google-Smtp-Source: ABdhPJzDin7bcNwLDgD+rTRNSXog9K9FPEl8PH46De48/iGPEA5R+VLM64R2zhRs9D14sdWGk/IUfLFiSuw= X-Received: from ascull.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1510]) (user=ascull job=sendgmr) by 2002:a1c:35c7:0:b0:38e:4c59:6788 with SMTP id c190-20020a1c35c7000000b0038e4c596788mr402228wma.1.1649324500424; Thu, 07 Apr 2022 02:41:40 -0700 (PDT) Date: Thu, 7 Apr 2022 09:41:16 +0000 In-Reply-To: <20220407094123.1752236-1-ascull@google.com> Message-Id: <20220407094123.1752236-5-ascull@google.com> Mime-Version: 1.0 References: <20220407094123.1752236-1-ascull@google.com> X-Mailer: git-send-email 2.35.1.1094.g7c7d902a7c-goog Subject: [PATCH 04/11] sandbox: Add support for Address Sanitizer From: Andrew Scull To: u-boot@lists.denx.de Cc: sjg@chromium.org, 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. Signed-off-by: Andrew Scull --- Kconfig | 7 +++++++ arch/sandbox/config.mk | 8 ++++++++ configs/sandbox_defconfig | 1 + 3 files changed, 16 insertions(+) diff --git a/Kconfig b/Kconfig index 9dd9ec7f6d..ae7e92611d 100644 --- a/Kconfig +++ b/Kconfig @@ -137,6 +137,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 7ebeb89264..4862af07cd 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.1094.g7c7d902a7c-goog