From patchwork Mon Dec 1 17:04:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 795 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764608945; bh=q0sGYI3sZ6sobHqMa5+JTu+oKHFPrrvgBBUK3lUrGmU=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=euMA9hFmSCdk11rL9sUBfDaDdl/4nbFbbWy9bbD4ImVk+JPPviVK89ykruDcpNUIR 34obQiKuBxY5QI0olAXzVrZcW6BrXYl0j0t6McvvdELy4XjcxY9kEEcMnDjmBZLMlq n+Gc83I5Rfe38BOqGZNqIU7ALe3Ju2V1y3C3orQzuXJpz47tJJf37wG+Fs3GHMM+GB r68hp81uzKiIILqLrp/anq1IBMjL4mgb/Db7k2V2ZpVqt0wIS0qLkQ4CYHw8YZfKPY 7c66StS8nvdOgCq1YLFXEyqk1XK499X3YnpqVhUTay24ozOzRRLXNK0D3QL3Ar3Oi8 vWtF6cuPM6v3Q== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id EA64B688A4 for ; Mon, 1 Dec 2025 10:09:05 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id M6GDgmNuvDdR for ; Mon, 1 Dec 2025 10:09:05 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764608945; bh=q0sGYI3sZ6sobHqMa5+JTu+oKHFPrrvgBBUK3lUrGmU=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=euMA9hFmSCdk11rL9sUBfDaDdl/4nbFbbWy9bbD4ImVk+JPPviVK89ykruDcpNUIR 34obQiKuBxY5QI0olAXzVrZcW6BrXYl0j0t6McvvdELy4XjcxY9kEEcMnDjmBZLMlq n+Gc83I5Rfe38BOqGZNqIU7ALe3Ju2V1y3C3orQzuXJpz47tJJf37wG+Fs3GHMM+GB r68hp81uzKiIILqLrp/anq1IBMjL4mgb/Db7k2V2ZpVqt0wIS0qLkQ4CYHw8YZfKPY 7c66StS8nvdOgCq1YLFXEyqk1XK499X3YnpqVhUTay24ozOzRRLXNK0D3QL3Ar3Oi8 vWtF6cuPM6v3Q== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id D63616881A for ; Mon, 1 Dec 2025 10:09:05 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764608943; bh=fZLGrjwc1OB0lH8DPXmp6A3Iv7m0rMDDd2wx87i00jU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NOvRsWEW6UhzHKr/bSvdxWh96ktZ1+z0Oszs3tdCM0oCopEiQDpWPjwotIRA4U1lI BuBIFE8cJDfeguZUjLCKYovSL8MLnSirCAF7OKTN2SAZc7zplPnGXa49mkCMgYlbaG b0b6S+6okNIkBBYz2h4YfkTIzZWFcbQo5SzSha4QBHy5StZlEKSrwjZdNr3XmzZfjl ovQdXcPbea+Y8uYEvnsb5abK2d0aRZYLUvHYOoqgbzDWWLaHAvWVO7lwd9VgHsxj+U HnTgxayJJ8gO0oYEH8yTiuiJPahxP1fv+ZVR5Xc0bZYV1KE6W1c+mtmM13GeAKXCCR 3TxVMWOBvXolQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id B3330688AE; Mon, 1 Dec 2025 10:09:03 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id OUGZmXnwJhyM; Mon, 1 Dec 2025 10:09:03 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764608937; bh=67AW5ysHvAULAOBuDT7o8sXsI3iQLbvvKQBclX3Gi1s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=guzXUR9O8tNFPsWtnI+3JLG/L7vCJbS4p5giRr1Cfi3ljCPVIX8/qPdk2kNwg+R/P Ir2zDJdD+0paOP3QHBgVtCMt0pUt1hWWAnGEMg2RvYcwKHZziQOyzEJItpjfyNdyzN IGK3FwLR7uWyV/5LK9ndXPle+19B3znhxVhwbelYuF7qgA1eEhxNdlkhGO9IJShnJs fTCsDYIPApjf6KLOyYXHikNDFzszkyvKa3MS5EeCFAh/ioErZlz1uhwqFXQZ3f7nm7 TdO5at7Q4kp9lJNc/T6OnWjoGIzpIio1mwzVrDFFAkPxMGRg9Ub0Yfpp13oyVv97AT 2a4z3nUhHcsGA== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 753885E439; Mon, 1 Dec 2025 10:08:57 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Mon, 1 Dec 2025 10:04:59 -0700 Message-ID: <20251201170529.3237986-21-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251201170529.3237986-1-sjg@u-boot.org> References: <20251201170529.3237986-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: KASLEDUDQPMGPU62XJXCXLVIERT6NWWH X-Message-ID-Hash: KASLEDUDQPMGPU62XJXCXLVIERT6NWWH X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Heinrich Schuchardt , Simon Glass , Simon Glass , Sean Anderson , Claude X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 20/37] test: Support testing malloc() failures List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass It is helpful to test that out-of-memory checks work correctly in code that calls malloc(). Add a simple way to force failure after a given number of malloc() calls. Also add support for realloc() testing (from commit 04894f5ad53). Changes from original commits: - Variable declarations moved to top of U-Boot section (before dlmalloc()) - Adapted to new dlmalloc function names (dlmalloc/dlrealloc vs mALLOc/rEALLOc) Signed-off-by: Simon Glass Reviewed-by: Sean Anderson Co-developed-by: Claude Signed-off-by: Simon Glass (cherry picked from commit 62d638386c17d17b929ad10956c7f60825335a4e) (cherry picked from commit 04894f5ad53cab0ee03eb3bc1cc1682e22f5dd1b) --- common/dlmalloc.c | 24 ++++++++++++++++++++++++ include/malloc.h | 12 ++++++++++++ 2 files changed, 36 insertions(+) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 9298fc445e4..aacc9b5db3b 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -592,6 +592,9 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP DECLARE_GLOBAL_DATA_PTR; +static bool malloc_testing; /* enable test mode */ +static int malloc_max_allocs; /* return NULL after this many calls to malloc() */ + ulong mem_malloc_start; ulong mem_malloc_end; ulong mem_malloc_brk; @@ -4614,6 +4617,11 @@ void* dlmalloc(size_t bytes) { /* Return NULL if not initialized yet */ if (!mem_malloc_start && !mem_malloc_end) return NULL; + + if (CONFIG_IS_ENABLED(UNIT_TEST) && malloc_testing) { + if (--malloc_max_allocs < 0) + return NULL; + } #endif /* Basic algorithm: @@ -5328,6 +5336,10 @@ void* dlrealloc(void* oldmem, size_t bytes) { panic("pre-reloc realloc() is not supported"); } #endif + if (CONFIG_IS_ENABLED(UNIT_TEST) && malloc_testing) { + if (--malloc_max_allocs < 0) + return NULL; + } #endif void* mem = 0; if (oldmem == 0) { @@ -6491,6 +6503,17 @@ void mem_malloc_init(ulong start, ulong size) #endif } +void malloc_enable_testing(int max_allocs) +{ + malloc_testing = true; + malloc_max_allocs = max_allocs; +} + +void malloc_disable_testing(void) +{ + malloc_testing = false; +} + int initf_malloc(void) { #if CONFIG_IS_ENABLED(SYS_MALLOC_F) @@ -6501,4 +6524,5 @@ int initf_malloc(void) return 0; } + #endif /* __UBOOT__ */ diff --git a/include/malloc.h b/include/malloc.h index 76068032da7..72db7fdb507 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -658,6 +658,18 @@ extern ulong mem_malloc_start; extern ulong mem_malloc_end; extern ulong mem_malloc_brk; +/** + * malloc_enable_testing() - Enable malloc failure testing + * + * @max_allocs: Number of allocations to allow before returning NULL + */ +void malloc_enable_testing(int max_allocs); + +/** + * malloc_disable_testing() - Disable malloc failure testing + */ +void malloc_disable_testing(void); + /** * mem_malloc_init() - Initialize the malloc() heap *