From patchwork Sun Mar 31 20:03:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eugene Uriev X-Patchwork-Id: 1918360 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=AixyhEa8; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V76493ZSPz1yWv for ; Mon, 1 Apr 2024 08:00:41 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id CC5DF87FCD; Sun, 31 Mar 2024 23:00:18 +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="AixyhEa8"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1CED187C82; Sun, 31 Mar 2024 22:03:35 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) (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 0C89287F77 for ; Sun, 31 Mar 2024 22:03:32 +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=eugeneuriev@gmail.com Received: by mail-lf1-x130.google.com with SMTP id 2adb3069b0e04-513e134f73aso4461653e87.2 for ; Sun, 31 Mar 2024 13:03:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711915411; x=1712520211; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9sB9B0D4hQ6ACojBU0L8lgwfz1dkWz1YxNlq2ZZ++7M=; b=AixyhEa8lACpwOjQY9uca7PbAoc2t45DzsgvNQjYgeqnuk3Nj/VpGgbm82cTVIYm3E 6LxuYSUI3rFc0KFhqohRZdy4XLng2ILEzVcil90Gb1dAkH9H3Y3rS+3o7LtfaE0nJmd0 OxTHA79xSrkZj5ru+i1jXWQuqzSD2BGeM1nEQNGfUj2meCcSMuCUAsj17eplA+CAwSKZ aswXsCdIMFHVCnEDZSJv2Sf6kAyiuTZICga16DrYTCXLFfrUmUoOwKYEzGi+p+o3rCYv w8nK4JGzIQH/8EISlkFragQyfuEe7IejS2+776oIrLg2/Pr5IR/0pujmfiAGAbfmzA3f kJNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711915411; x=1712520211; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9sB9B0D4hQ6ACojBU0L8lgwfz1dkWz1YxNlq2ZZ++7M=; b=Us+hc68iGWKHT1mRp5GaOREmgmBfbaCuwnuwUMlKVPwtdHPXTTLy73Km6bptu6K7mX Js4ZkHUeKN+Yh/7JM8XQpPyfsjVYYu5ZhjqtM5GFL3IvIX9XvP7IDEUh4HEHvIqN5LDT KgW45vv64V7l02jZv+FHiITCdrPfddIluJKI9zCp9uyArqG8xXRs9t7WzwfpbvGbNhI8 cRjz5l9ykvVw1zjEdZje3JvG4uW+5dkyHbR+tfIB2IoynzCnJgEY+PA7IzTLbcEmTn7z ZYemux9ELfj8obh4LwjKj/zA5aC+yOtJTmp1cVGZ7I4Q3LtPvf0dDpIlcEtpSOKyfvdE G0Cg== X-Gm-Message-State: AOJu0YwXv5uWuCGq6tl7YIVrmFtvIUK49vFfrIC1SD8ur5ueAD+jbe8x EUR7Nn6jiJF/7Y1QT+BRUeyVfRv9FOsSO+YqnSF4yJQWEhVXVxDhFM9B9iRk X-Google-Smtp-Source: AGHT+IGthQpSO4hmW+aquAiQmGibWgpS0o/Fn33UvPwsQfra71+bw8OKFmA/EaGA+4yduA/k5/izQA== X-Received: by 2002:a05:6512:2145:b0:515:9dcd:22ae with SMTP id s5-20020a056512214500b005159dcd22aemr5386688lfr.66.1711915411298; Sun, 31 Mar 2024 13:03:31 -0700 (PDT) Received: from BlackProtonRN.clients.gtk.su ([185.16.100.35]) by smtp.gmail.com with ESMTPSA id l5-20020ac25545000000b00516a08ce8f8sm425000lfk.269.2024.03.31.13.03.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Mar 2024 13:03:30 -0700 (PDT) From: Eugene Uriev To: Tom Rini Cc: u-boot@lists.denx.de, Eugene Uriev Subject: [PATCH 1/9] mcheck: prepare +1 tier for mcheck-wrappers, in dl-*alloc commands Date: Sun, 31 Mar 2024 23:03:19 +0300 Message-Id: <20240331200327.29141-2-eugeneuriev@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240331200327.29141-1-eugeneuriev@gmail.com> References: <20240331200327.29141-1-eugeneuriev@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Sun, 31 Mar 2024 23:00:15 +0200 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.8 at phobos.denx.de X-Virus-Status: Clean Signed-off-by: Eugene Uriev --- The file contradicts U-Boot code style a lot; so I preserve original style, according to recommendations. common/dlmalloc.c | 66 +++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index de3f04225f..40acd3dfa5 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -32,6 +32,17 @@ void malloc_stats(); DECLARE_GLOBAL_DATA_PTR; +#ifdef MCHECK_HEAP_PROTECTION + #define STATIC_IF_MCHECK static +#else + #define STATIC_IF_MCHECK + #define mALLOc_impl mALLOc + #define fREe_impl fREe + #define rEALLOc_impl rEALLOc + #define mEMALIGn_impl mEMALIGn + #define cALLOc_impl cALLOc +#endif + /* Emulation of sbrk for WIN32 All code within the ifdef WIN32 is untested by me. @@ -1270,10 +1281,11 @@ static void malloc_extend_top(nb) INTERNAL_SIZE_T nb; */ +STATIC_IF_MCHECK #if __STD_C -Void_t* mALLOc(size_t bytes) +Void_t* mALLOc_impl(size_t bytes) #else -Void_t* mALLOc(bytes) size_t bytes; +Void_t* mALLOc_impl(bytes) size_t bytes; #endif { mchunkptr victim; /* inspected/selected chunk */ @@ -1555,10 +1567,11 @@ Void_t* mALLOc(bytes) size_t bytes; */ +STATIC_IF_MCHECK #if __STD_C -void fREe(Void_t* mem) +void fREe_impl(Void_t* mem) #else -void fREe(mem) Void_t* mem; +void fREe_impl(mem) Void_t* mem; #endif { mchunkptr p; /* chunk corresponding to mem */ @@ -1696,10 +1709,11 @@ void fREe(mem) Void_t* mem; */ +STATIC_IF_MCHECK #if __STD_C -Void_t* rEALLOc(Void_t* oldmem, size_t bytes) +Void_t* rEALLOc_impl(Void_t* oldmem, size_t bytes) #else -Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes; +Void_t* rEALLOc_impl(oldmem, bytes) Void_t* oldmem; size_t bytes; #endif { INTERNAL_SIZE_T nb; /* padded request size */ @@ -1725,7 +1739,7 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes; #ifdef REALLOC_ZERO_BYTES_FREES if (!bytes) { - fREe(oldmem); + fREe_impl(oldmem); return NULL; } #endif @@ -1733,7 +1747,7 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes; if ((long)bytes < 0) return NULL; /* realloc of null is supposed to be same as malloc */ - if (oldmem == NULL) return mALLOc(bytes); + if (oldmem == NULL) return mALLOc_impl(bytes); #if CONFIG_IS_ENABLED(SYS_MALLOC_F) if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) { @@ -1758,7 +1772,7 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes; /* Note the extra SIZE_SZ overhead. */ if(oldsize - SIZE_SZ >= nb) return oldmem; /* do nothing */ /* Must alloc, copy, free. */ - newmem = mALLOc(bytes); + newmem = mALLOc_impl(bytes); if (!newmem) return NULL; /* propagate failure */ MALLOC_COPY(newmem, oldmem, oldsize - 2*SIZE_SZ); @@ -1869,7 +1883,7 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes; /* Must allocate */ - newmem = mALLOc (bytes); + newmem = mALLOc_impl (bytes); if (newmem == NULL) /* propagate failure */ return NULL; @@ -1886,7 +1900,7 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes; /* Otherwise copy, free, and exit */ MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ); - fREe(oldmem); + fREe_impl(oldmem); return newmem; } else { VALGRIND_RESIZEINPLACE_BLOCK(oldmem, 0, bytes, SIZE_SZ); @@ -1905,7 +1919,7 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes; set_inuse_bit_at_offset(remainder, remainder_size); VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(remainder), remainder_size, SIZE_SZ, false); - fREe(chunk2mem(remainder)); /* let free() deal with it */ + fREe_impl(chunk2mem(remainder)); /* let free() deal with it */ } else { @@ -1939,10 +1953,11 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes; */ +STATIC_IF_MCHECK #if __STD_C -Void_t* mEMALIGn(size_t alignment, size_t bytes) +Void_t* mEMALIGn_impl(size_t alignment, size_t bytes) #else -Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes; +Void_t* mEMALIGn_impl(alignment, bytes) size_t alignment; size_t bytes; #endif { INTERNAL_SIZE_T nb; /* padded request size */ @@ -1965,7 +1980,7 @@ Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes; /* If need less alignment than we give anyway, just relay to malloc */ - if (alignment <= MALLOC_ALIGNMENT) return mALLOc(bytes); + if (alignment <= MALLOC_ALIGNMENT) return mALLOc_impl(bytes); /* Otherwise, ensure that it is at least a minimum chunk size */ @@ -1974,7 +1989,7 @@ Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes; /* Call malloc with worst case padding to hit alignment. */ nb = request2size(bytes); - m = (char*)(mALLOc(nb + alignment + MINSIZE)); + m = (char*)(mALLOc_impl(nb + alignment + MINSIZE)); /* * The attempt to over-allocate (with a size large enough to guarantee the @@ -1990,7 +2005,7 @@ Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes; * Use bytes not nb, since mALLOc internally calls request2size too, and * each call increases the size to allocate, to account for the header. */ - m = (char*)(mALLOc(bytes)); + m = (char*)(mALLOc_impl(bytes)); /* Aligned -> return it */ if ((((unsigned long)(m)) % alignment) == 0) return m; @@ -1998,10 +2013,10 @@ Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes; * Otherwise, try again, requesting enough extra space to be able to * acquire alignment. */ - fREe(m); + fREe_impl(m); /* Add in extra bytes to match misalignment of unexpanded allocation */ extra = alignment - (((unsigned long)(m)) % alignment); - m = (char*)(mALLOc(bytes + extra)); + m = (char*)(mALLOc_impl(bytes + extra)); /* * m might not be the same as before. Validate that the previous value of * extra still works for the current value of m. @@ -2010,7 +2025,7 @@ Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes; if (m) { extra2 = alignment - (((unsigned long)(m)) % alignment); if (extra2 > extra) { - fREe(m); + fREe_impl(m); m = NULL; } } @@ -2060,7 +2075,7 @@ Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes; set_head(newp, newsize | PREV_INUSE); set_inuse_bit_at_offset(newp, newsize); set_head_size(p, leadsize); - fREe(chunk2mem(p)); + fREe_impl(chunk2mem(p)); p = newp; VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(p), bytes, SIZE_SZ, false); @@ -2078,7 +2093,7 @@ Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes; set_head_size(p, nb); VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(remainder), remainder_size, SIZE_SZ, false); - fREe(chunk2mem(remainder)); + fREe_impl(chunk2mem(remainder)); } check_inuse_chunk(p); @@ -2126,10 +2141,11 @@ Void_t* pvALLOc(bytes) size_t bytes; */ +STATIC_IF_MCHECK #if __STD_C -Void_t* cALLOc(size_t n, size_t elem_size) +Void_t* cALLOc_impl(size_t n, size_t elem_size) #else -Void_t* cALLOc(n, elem_size) size_t n; size_t elem_size; +Void_t* cALLOc_impl(n, elem_size) size_t n; size_t elem_size; #endif { mchunkptr p; @@ -2145,7 +2161,7 @@ Void_t* cALLOc(n, elem_size) size_t n; size_t elem_size; INTERNAL_SIZE_T oldtopsize = chunksize(top); #endif #endif - Void_t* mem = mALLOc (sz); + Void_t* mem = mALLOc_impl (sz); if ((long)n < 0) return NULL; From patchwork Sun Mar 31 20:03:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eugene Uriev X-Patchwork-Id: 1918359 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=ZFstuf9v; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V76410ZdCz1yWv for ; Mon, 1 Apr 2024 08:00:33 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2E55987FA7; Sun, 31 Mar 2024 23:00:18 +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="ZFstuf9v"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 14FC787FC6; Sun, 31 Mar 2024 22:03:35 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) (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 B6BE387F8B for ; Sun, 31 Mar 2024 22:03:32 +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=eugeneuriev@gmail.com Received: by mail-lf1-x12d.google.com with SMTP id 2adb3069b0e04-515b43b39fdso2949914e87.1 for ; Sun, 31 Mar 2024 13:03:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711915412; x=1712520212; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TKcNN9BhN8UKpZzIBetRxa/3ScpLoQgKpgUqXqnwsWw=; b=ZFstuf9v3DkLK7AYjFzA3hoL7zbBAp8QZ/x8gY8DrFv3YbFGxU1LhTdM5g8JJQwFYN P83syuAesfFV9FkbsqsNwJTrx54khvgFrOeEa1Xp76tKtu1XtvQFVy0ivw7tdP9LW75U sZJ+emrJARDli+XSyN1XmuM9ZEhshWe1wD2MKuvgpuDZZRIWF1cGGWDIxPOLxhAJaJuY 3TqXa+WVIbBuWm0WEFxsnrqw6FNJXQ031rX71ttvgUUWvH2ieqejm1uRFby2sLvlcYfy qjwtdy7MXoaWZshS0NEGYKjb7utYnX2VNS+ONeSKXmnj7aTWJqjUJWi7qiqismVDftax EiKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711915412; x=1712520212; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TKcNN9BhN8UKpZzIBetRxa/3ScpLoQgKpgUqXqnwsWw=; b=ezUitkaVwxKB58kw5ZOWiGqatmevAJuO8RxsMAlo8Os7ETo9C+YKx0zJFpRzrK0BVZ JHAGkR/aUgAc52x1riwamL0+VShtSq3IWdMKRPxBRy+wt3OEGBVVvkYZhcNtrF3jy+Vp bPSDmg4L9Rr/6/jUvBKKQctkh6OjyvRzhjhTwh+r5GU5rOBPj8921BFwFgo0MGkI1z0V IM3GdUkvYtKZsvFtqh17ZAz0yQYpbqPt5AwCBtSMzjuzn7qOrzI9D+vzEem25qP2VaL/ +TMCgLV+zWXM7UsPB3XN8Wb6u4soz5TXW0gtMvb7XhBfCwgPbXh9qqGrf8uycI9wfnnd Jnsw== X-Gm-Message-State: AOJu0Yx5PmQvwDh82qejgFPZ9kBAnUEU9ur3wsAzgvIVfQ+A0GYfIgnC Lc6CiCFg7Ce/7mOfU9errFY6G8k/KnrMZ+WTSX28B7shhwNxyNuE6MSbqeaD X-Google-Smtp-Source: AGHT+IGop+vW5M6ZJKy9U/q2LelBKIDEpYMDoBIdCBEfi0Vws/D9GVzrSRY8mGJd9X0JqXHhHADGGQ== X-Received: by 2002:a05:6512:15a6:b0:516:a1ad:cedd with SMTP id bp38-20020a05651215a600b00516a1adceddmr1526053lfb.14.1711915412066; Sun, 31 Mar 2024 13:03:32 -0700 (PDT) Received: from BlackProtonRN.clients.gtk.su ([185.16.100.35]) by smtp.gmail.com with ESMTPSA id l5-20020ac25545000000b00516a08ce8f8sm425000lfk.269.2024.03.31.13.03.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Mar 2024 13:03:31 -0700 (PDT) From: Eugene Uriev To: Tom Rini Cc: u-boot@lists.denx.de, Eugene Uriev Subject: [PATCH 2/9] mcheck: Use memset/memcpy instead of MALLOC_ZERO/MALLOC_COPY for mcheck. Date: Sun, 31 Mar 2024 23:03:20 +0300 Message-Id: <20240331200327.29141-3-eugeneuriev@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240331200327.29141-1-eugeneuriev@gmail.com> References: <20240331200327.29141-1-eugeneuriev@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Sun, 31 Mar 2024 23:00:15 +0200 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.8 at phobos.denx.de X-Virus-Status: Clean These fast helpers sometimes breach mem-chunk boundaries. Thus they trigger mcheck alarm. Standard ones are accurate though. Signed-off-by: Eugene Uriev --- common/dlmalloc.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 40acd3dfa5..0813e7e8b1 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -34,6 +34,10 @@ DECLARE_GLOBAL_DATA_PTR; #ifdef MCHECK_HEAP_PROTECTION #define STATIC_IF_MCHECK static + #undef MALLOC_COPY + #undef MALLOC_ZERO +static inline void MALLOC_ZERO(void *p, size_t sz) { memset(p, 0, sz); } +static inline void MALLOC_COPY(void *dest, const void *src, size_t sz) { memcpy(dest, src, sz); } #else #define STATIC_IF_MCHECK #define mALLOc_impl mALLOc From patchwork Sun Mar 31 20:03:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eugene Uriev X-Patchwork-Id: 1918362 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=lAJ0xYNz; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V764X5CGLz1yWv for ; Mon, 1 Apr 2024 08:01:00 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 01E3A88273; Sun, 31 Mar 2024 23:00:20 +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="lAJ0xYNz"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5B7EE87E50; Sun, 31 Mar 2024 22:03:38 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) (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 55B5887FA0 for ; Sun, 31 Mar 2024 22:03: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=eugeneuriev@gmail.com Received: by mail-lf1-x12e.google.com with SMTP id 2adb3069b0e04-515c3eeea5dso3895534e87.1 for ; Sun, 31 Mar 2024 13:03:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711915413; x=1712520213; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gYw3M+Lwsqzgyt039QrsQyAsviAsIJHKsL9TdIF+/RM=; b=lAJ0xYNzaGg9jSoH7l9s1MirCval56oM8ky1szy3OswPjPBZ7ZIMP+6Yz2fx3RvzOE zUv3J2Dpv38li+zF1cBba7klFp9aHiTn61DPkyzLKmxEi8UtYlzwMBDjMp7qLIG1u17o Ar9aVFhYWOto0NSeGI5/q01m2A74TJxv42cGUu3uEnqrvcjvzl/2s/eJEyCsbmxYJIjj jVfRie8CH3YqoloPPLbj06GtXjIpcIF9gwz4XopqEtq2xPBRFM/GOHPMB+ypLniNzCQJ e1gykHNxD+V9etOlWoYlC8xGUpngeRZdTx1DDaGgFqPZ1qDlh/CIq3esiuLXc6vNBUle ZBXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711915413; x=1712520213; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gYw3M+Lwsqzgyt039QrsQyAsviAsIJHKsL9TdIF+/RM=; b=bZ9PrRzXLQf4NWSV++OJqqHlAq1a2A/VKp+520d4NCj7T1YlLROYU1Oe9uqxL4J36O olrQmxX5ZXlM4TVdb+C0d7SoSP+mqPZWD/ZKEMM5rHH9bPBwnJYd3qHqW4xNynYdEHjf 5BjccBZu1+ZeHI70NjMejRhh0d7WZQpOxMrsgM+mlO75l9fGUaA7dwglMzwKhskQM8aO ptv2Dr+ivLJFMAPJI0UqDK67TLuMHcl75yBGUQGupcMWoSqZa+htQD8jb0FC129zqfUH 87MWaUx4K37zLSL1uDXlLyYwDaaU3F89G+idAt9rZ4/LZ+mRXI8EPE8R0hZUmodTU2P7 CZ6Q== X-Gm-Message-State: AOJu0YxB6fj6SmsTgH5NK4CuY7E3dXI3XSYyK1rn3HdZRf6JjxDUzdCv Uz75RAV4l4J9pIo7bPo/jDI+Nkr210q6Wd3UJxhSCEZ+k56d8W4ioVZmqn1Y X-Google-Smtp-Source: AGHT+IF0btegrtEz3m8c5e0RQje1JVM++aloJB3PU9ZzPgGs7VJXEbvWH6TxESPicR9t5pgF//KbnA== X-Received: by 2002:a19:7608:0:b0:516:9fbe:3b93 with SMTP id c8-20020a197608000000b005169fbe3b93mr2563942lff.15.1711915413392; Sun, 31 Mar 2024 13:03:33 -0700 (PDT) Received: from BlackProtonRN.clients.gtk.su ([185.16.100.35]) by smtp.gmail.com with ESMTPSA id l5-20020ac25545000000b00516a08ce8f8sm425000lfk.269.2024.03.31.13.03.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Mar 2024 13:03:32 -0700 (PDT) From: Eugene Uriev To: Tom Rini Cc: u-boot@lists.denx.de, Eugene Uriev Subject: [PATCH 3/9] mcheck: introduce essentials of mcheck Date: Sun, 31 Mar 2024 23:03:21 +0300 Message-Id: <20240331200327.29141-4-eugeneuriev@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240331200327.29141-1-eugeneuriev@gmail.com> References: <20240331200327.29141-1-eugeneuriev@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Sun, 31 Mar 2024 23:00:15 +0200 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.8 at phobos.denx.de X-Virus-Status: Clean The core part of mcheck, but without memalign. memalign - to be added in ensuing commits. Signed-off-by: Eugene Uriev --- common/mcheck_core.inc.h | 220 +++++++++++++++++++++++++++++++++++++++ include/mcheck.h | 42 ++++++++ 2 files changed, 262 insertions(+) create mode 100644 common/mcheck_core.inc.h create mode 100644 include/mcheck.h diff --git a/common/mcheck_core.inc.h b/common/mcheck_core.inc.h new file mode 100644 index 0000000000..6f26ef00d9 --- /dev/null +++ b/common/mcheck_core.inc.h @@ -0,0 +1,220 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright (C) 2024 Free Software Foundation, Inc. + * Written by Eugene Uriev, based on glibc 2.0 prototype of Mike Haertel. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + */ + +/* + * TL;DR: this is a porting of glibc mcheck into U-Boot + * + * This file contains no entities for external linkage. + * So mcheck protection may be used in parallel, e.g. for "malloc_simple(..)" and "malloc(..)". + * To do so, the file should be shared/include twice, - without linkage conflicts. + * I.e. "core"-part is shared as a source, but not as a binary. + * Maybe some optimization here make sense, to engage more binary sharing too. + * But, currently I strive to keep it as simple, as possible. + * And this, programmers'-only, mode don't pretend to be main. + * + * This library is aware of U-Boot specific. It's also aware of ARM alignment concerns. + * Unlike glibc-clients, U-Boot has limited malloc-usage, and only one thread. + * So it's better to make the protection heavier. + * Thus overflow canary here is greater, than glibc's one. Underflow canary is bigger too. + * Heavy canary allows to catch not only memset(..)-errors, + * but overflow/underflow of struct-array access: + * { + * struct mystruct* p = malloc(sizeof(struct mystruct) * N); + * p[-1].field1 = 0; + * p[N].field2 = 13; + * } + * TODO: In order to guarantee full coverage of that kind of errors, a user can add variable-size + * canaries here. So pre- and post-canary with size >= reqested_size, could be provided + * (with the price of 3x heap-usage). Therefore, it would catch 100% of changes beyond + * an array, for index(+1/-1) errors. + * + * U-Boot is a BL, not an OS with a lib. Activity of the library is set not in runtime, + * rather in compile-time, by MCHECK_HEAP_PROTECTION macro. That guarantees that + * we haven't missed first malloc. + */ +#ifndef _MCHECKCORE_INC_H +#define _MCHECKCORE_INC_H 1 +#include "mcheck.h" + +#if defined(MCHECK_HEAP_PROTECTION) +#define mcheck_flood memset + +// these are from /dev/random: +#define MAGICWORD 0x99ccf430fa562a05ULL +#define MAGICFREE 0x4875e63c0c6fc08eULL +#define MAGICTAIL 0x918dbcd7df78dcd6ULL +#define MALLOCFLOOD ((char)0xb6) +#define FREEFLOOD ((char)0xf5) +#define PADDINGFLOOD ((char)0x58) + +#define CANARY_DEPTH 2 + +typedef unsigned long long mcheck_elem; +typedef struct { + mcheck_elem elems[CANARY_DEPTH]; +} mcheck_canary; +struct mcheck_hdr { + size_t size; /* Exact size requested by user. */ + mcheck_canary canary; /* Magic number to check header integrity. */ +}; + +static void mcheck_default_abort(enum mcheck_status status) +{ + const char *msg; + + switch (status) { + case MCHECK_OK: + msg = "memory is consistent, library is buggy\n"; + break; + case MCHECK_HEAD: + msg = "memory clobbered before allocated block\n"; + break; + case MCHECK_TAIL: + msg = "memory clobbered past end of allocated block\n"; + break; + case MCHECK_FREE: + msg = "block freed twice\n"; + break; + default: + msg = "bogus mcheck_status, library is buggy\n"; + break; + } + printf("\n\nmcheck: %s!!!\n\n", msg); +} + +static mcheck_abortfunc_t mcheck_abortfunc = &mcheck_default_abort; + +static inline size_t allign_size_up(size_t sz, size_t grain) +{ + return (sz + grain - 1) & ~(grain - 1); +} + +#define mcheck_allign_customer_size(SZ) allign_size_up(SZ, sizeof(mcheck_elem)) + +static enum mcheck_status mcheck_OnNok(enum mcheck_status status) +{ + (*mcheck_abortfunc)(status); + return status; +} + +static enum mcheck_status mcheck_checkhdr(const struct mcheck_hdr *hdr) +{ + int i; + + for (i = 0; i < CANARY_DEPTH; ++i) + if (hdr->canary.elems[i] == MAGICFREE) + return mcheck_OnNok(MCHECK_FREE); + + for (i = 0; i < CANARY_DEPTH; ++i) + if (hdr->canary.elems[i] != MAGICWORD) + return mcheck_OnNok(MCHECK_HEAD); + + const size_t payload_size = hdr->size; + const size_t payload_size_aligned = mcheck_allign_customer_size(payload_size); + const size_t padd_size = payload_size_aligned - hdr->size; + + const char *payload = (const char *)&hdr[1]; + + for (i = 0; i < padd_size; ++i) + if (payload[payload_size + i] != PADDINGFLOOD) + return mcheck_OnNok(MCHECK_TAIL); + + const mcheck_canary *tail = (const mcheck_canary *)&payload[payload_size_aligned]; + + for (i = 0; i < CANARY_DEPTH; ++i) + if (tail->elems[i] != MAGICTAIL) + return mcheck_OnNok(MCHECK_TAIL); + return MCHECK_OK; +} + +enum { KEEP_CONTENT = 0, CLEAN_CONTENT, ANY_ALIGNMENT = 1 }; +static void *mcheck_free_helper(void *ptr, int clean_content) +{ + if (!ptr) + return ptr; + + struct mcheck_hdr *hdr = &((struct mcheck_hdr *)ptr)[-1]; + int i; + + mcheck_checkhdr(hdr); + for (i = 0; i < CANARY_DEPTH; ++i) + hdr->canary.elems[i] = MAGICFREE; + + if (clean_content) + mcheck_flood(ptr, FREEFLOOD, mcheck_allign_customer_size(hdr->size)); + return hdr; +} + +static void *mcheck_free_prehook(void *ptr) { return mcheck_free_helper(ptr, CLEAN_CONTENT); } +static void *mcheck_reallocfree_prehook(void *ptr) { return mcheck_free_helper(ptr, KEEP_CONTENT); } + +static size_t mcheck_alloc_prehook(size_t sz) +{ + sz = mcheck_allign_customer_size(sz); + return sizeof(struct mcheck_hdr) + sz + sizeof(mcheck_canary); +} + +static void *mcheck_allocated_helper(void *altoghether_ptr, size_t customer_sz, + size_t alignment, int clean_content) +{ + struct mcheck_hdr *hdr = (struct mcheck_hdr *)altoghether_ptr; + int i; + + hdr->size = customer_sz; + for (i = 0; i < CANARY_DEPTH; ++i) + hdr->canary.elems[i] = MAGICWORD; + + char *payload = (char *)&hdr[1]; + + if (clean_content) + mcheck_flood(payload, MALLOCFLOOD, customer_sz); + + const size_t customer_size_aligned = mcheck_allign_customer_size(customer_sz); + + mcheck_flood(payload + customer_sz, PADDINGFLOOD, customer_size_aligned - customer_sz); + + mcheck_canary *tail = (mcheck_canary *)&payload[customer_size_aligned]; + + for (i = 0; i < CANARY_DEPTH; ++i) + tail->elems[i] = MAGICTAIL; + return payload; +} + +static void *mcheck_alloc_posthook(void *altoghether_ptr, size_t customer_sz) +{ + return mcheck_allocated_helper(altoghether_ptr, customer_sz, ANY_ALIGNMENT, CLEAN_CONTENT); +} + +static void *mcheck_alloc_noclean_posthook(void *altoghether_ptr, size_t customer_sz) +{ + return mcheck_allocated_helper(altoghether_ptr, customer_sz, ANY_ALIGNMENT, KEEP_CONTENT); +} + +static enum mcheck_status mcheck_mprobe(void *ptr) +{ + struct mcheck_hdr *hdr = &((struct mcheck_hdr *)ptr)[-1]; + + return mcheck_checkhdr(hdr); +} + +static void mcheck_initialize(mcheck_abortfunc_t new_func, char pedantic_flag) +{ + mcheck_abortfunc = (new_func) ? new_func : &mcheck_default_abort; +} + +#endif +#endif diff --git a/include/mcheck.h b/include/mcheck.h new file mode 100644 index 0000000000..a049745e4e --- /dev/null +++ b/include/mcheck.h @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: GPL-2.1+ */ +/* + * Copyright (C) 1996-2024 Free Software Foundation, Inc. + * This file is part of the GNU C Library. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * . + */ +#ifndef _MCHECK_H +#define _MCHECK_H 1 + +/* + * Return values for `mprobe': these are the kinds of inconsistencies that + * `mcheck' enables detection of. + */ +enum mcheck_status { + MCHECK_DISABLED = -1, /* Consistency checking is not turned on. */ + MCHECK_OK, /* Block is fine. */ + MCHECK_FREE, /* Block freed twice. */ + MCHECK_HEAD, /* Memory before the block was clobbered. */ + MCHECK_TAIL /* Memory after the block was clobbered. */ +}; + +typedef void (*mcheck_abortfunc_t)(enum mcheck_status); + +int mcheck(mcheck_abortfunc_t func); + +/* + * Check for aberrations in a particular malloc'd block. These are the + * same checks that `mcheck' does, when you free or reallocate a block. + */ +enum mcheck_status mprobe(void *__ptr); + +#endif From patchwork Sun Mar 31 20:03:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eugene Uriev X-Patchwork-Id: 1918361 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=QTl2QEjr; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V764L54z3z1yWv for ; Mon, 1 Apr 2024 08:00:50 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 67F9C88003; Sun, 31 Mar 2024 23:00:19 +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="QTl2QEjr"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0ACC287DF1; Sun, 31 Mar 2024 22:03:38 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) (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 C39E587F77 for ; Sun, 31 Mar 2024 22:03:35 +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=eugeneuriev@gmail.com Received: by mail-lf1-x129.google.com with SMTP id 2adb3069b0e04-515d515e28dso1110540e87.0 for ; Sun, 31 Mar 2024 13:03:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711915415; x=1712520215; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=y+mY6dXHcULqxXXWwnFpN8OlJTa3xJV+EwqFwZYcPIk=; b=QTl2QEjrob4eS9MIKydc0NWIklQKDvUzKRWqxRGMhPShI3gMJAb10jde/ygnO3X53p BNw4Y6e7Rm2blfoOLKIJuxzbuaucoNEAFGorbkVwrbKqwIGJ1y7rPSzodzIjjaOR5vnU Quf0sMrEBCrbO9Rt3Pl5IZ1nsWfRvRfPYFHxN1/m8eqdmPqqLha3aABZ43xQSjYlpxXb Gf+7NV8v+eRO+iE61SCFP4iKmYz2K7QuGOh3wjZCbgCu8sWxQmMuWqeaifOnn6iOvhzS KHz0rUGYYfiTMSwHdN8Z/yFI7XER5Hy6yWwRVzA8KBBk+Thsmd4HhXHH0PP8O6p22TE0 BsqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711915415; x=1712520215; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=y+mY6dXHcULqxXXWwnFpN8OlJTa3xJV+EwqFwZYcPIk=; b=janB3d2w81nU1djKv0G/rImitg04oHE2WmTFdRmO8dXH7yaOVCyIaqeVnLfQaCVHFb ex4IeEhCaoos5ndobitTKXuzB5FTqwJluOD29r+5MPpbvf3ZC2anj2Sqdpy9jWDbRDrN mA92eMz0I/uBjY+ks+oKFrVUVqe7ALvD+b/uTHQLjoz5nFjmsXCVr74n5J+8a5UJW91U OpRxjVkNkTqcl3Vbd0J+n+rjp6rKu4wSM7z+JmzO75hA+H8tRKRTMPlbKgiEwFu4KAxH C4j2Ma8GDV2ackUeJMxichClKQDKCnQePfyIlphsnU7mSrbmXroU0sETW2xvJwG8cmsU 8Hsg== X-Gm-Message-State: AOJu0YxSr+WrPF+RufzictDt1Xxeh1XdFJhtrGd5q30jnBxWOTtLs2P0 SYDNcwoY8TE2afl2REj+llI2IJdv7Zhev5GkQr434LF+TFXnos8D X-Google-Smtp-Source: AGHT+IGkNfxmeaA53D4ssOErcfQ5408QuXxseRQ+E2KYKsLvHYrExfc3F1F1xtWvWGBnqpBDlozojw== X-Received: by 2002:a05:6512:3d18:b0:516:2b59:15b7 with SMTP id d24-20020a0565123d1800b005162b5915b7mr4059549lfv.22.1711915415009; Sun, 31 Mar 2024 13:03:35 -0700 (PDT) Received: from BlackProtonRN.clients.gtk.su ([185.16.100.35]) by smtp.gmail.com with ESMTPSA id l5-20020ac25545000000b00516a08ce8f8sm425000lfk.269.2024.03.31.13.03.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Mar 2024 13:03:33 -0700 (PDT) From: Eugene Uriev To: Tom Rini Cc: u-boot@lists.denx.de, Eugene Uriev Subject: [PATCH 4/9] mcheck: integrate mcheck into dlmalloc.c Date: Sun, 31 Mar 2024 23:03:22 +0300 Message-Id: <20240331200327.29141-5-eugeneuriev@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240331200327.29141-1-eugeneuriev@gmail.com> References: <20240331200327.29141-1-eugeneuriev@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Sun, 31 Mar 2024 23:00:15 +0200 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.8 at phobos.denx.de X-Virus-Status: Clean This changes are probable worth to be generalized in a separate .h-file so, making it able to cover libc-mallocs and others, without too much copy-paste. But the malloc<=>mALLOc substitutions interfere with an elegant way to do this. Signed-off-by: Eugene Uriev --- common/dlmalloc.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 0813e7e8b1..8de15d7193 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -2225,6 +2225,73 @@ void cfree(mem) Void_t *mem; #endif +#ifdef MCHECK_HEAP_PROTECTION + #include "mcheck_core.inc.h" + #if !__STD_C + #error "must have __STD_C" + #endif + +Void_t *mALLOc(size_t bytes) +{ + size_t fullsz = mcheck_alloc_prehook(bytes); + void *p = mALLOc_impl(fullsz); + + if (!p) + return p; + return mcheck_alloc_posthook(p, bytes); +} + +void fREe(Void_t *mem) { fREe_impl(mcheck_free_prehook(mem)); } + +Void_t *rEALLOc(Void_t *oldmem, size_t bytes) +{ + if (bytes == 0) { + if (oldmem) + fREe(oldmem); + return NULL; + } + + if (oldmem == NULL) + return mALLOc(bytes); + + void *p = mcheck_reallocfree_prehook(oldmem); + size_t newsz = mcheck_alloc_prehook(bytes); + + p = rEALLOc_impl(p, newsz); + if (!p) + return p; + return mcheck_alloc_noclean_posthook(p, bytes); +} + +Void_t *mEMALIGn(size_t alignment, size_t bytes) +{ + return NULL; +} + +// pvALLOc, vALLOc - redirect to mEMALIGn, defined here, so they need no wrapping. + +Void_t *cALLOc(size_t n, size_t elem_size) +{ + // NB: here is no overflow check. + size_t fullsz = mcheck_alloc_prehook(n * elem_size); + void *p = cALLOc_impl(1, fullsz); + + if (!p) + return p; + return mcheck_alloc_noclean_posthook(p, n * elem_size); +} + +// mcheck API { +int mcheck(mcheck_abortfunc_t f) +{ + mcheck_initialize(f, 0); + return 0; +} + +enum mcheck_status mprobe(void *__ptr) { return mcheck_mprobe(__ptr); } +// mcheck API } +#endif + /* From patchwork Sun Mar 31 20:03:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eugene Uriev X-Patchwork-Id: 1918363 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=H8GKdBHc; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V764l0f2Fz1yWv for ; Mon, 1 Apr 2024 08:01:11 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 83036882B0; Sun, 31 Mar 2024 23:00:20 +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="H8GKdBHc"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5568587C82; Sun, 31 Mar 2024 22:03:39 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lj1-x22c.google.com (mail-lj1-x22c.google.com [IPv6:2a00:1450:4864:20::22c]) (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 6639D87F74 for ; Sun, 31 Mar 2024 22:03:37 +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=eugeneuriev@gmail.com Received: by mail-lj1-x22c.google.com with SMTP id 38308e7fff4ca-2d6ff0422a2so49190961fa.2 for ; Sun, 31 Mar 2024 13:03:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711915417; x=1712520217; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=iEbE/13KyoJaJ/y0cMWxB2voFqYX1/3jewfTlMW2E5Y=; b=H8GKdBHcRYmy/N2GYThFcIZuOt4B52ze5tpypZFFm5Cyt8YHiernEP1KUllWhmE/jn XGdc4SyW+9mwmMLpiJHUG9+twNNIF3POYrPkuxWWwfEY3HOOJdHCGhaDQd2m7QsscxW4 3RwzOO8qDYGq6YYvZd1Rao6v15dMJqzy6+SDiNx8BT+G8rFx/ejNAZlXBlw0HvExkulp ZXUmYk3QLqavNBp5p8X8yHUrGpId7K3VFT1qjjXdXIMfN8bUPloEKZd1s9+dHMaK6RH6 qX9tXFTSTGSy0mx53WtlzaDEHFekR6HjQJBXZaLE+Xwqd2qO8rLkjVQDYb+/Imfpz7Oc iJCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711915417; x=1712520217; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iEbE/13KyoJaJ/y0cMWxB2voFqYX1/3jewfTlMW2E5Y=; b=FHKLGgWzO33W2qKE1d2AUQX7yIP6FE2LtyowEuXPP82Phxc+koIGEebteHT8l0HoQ8 +c8MGQSE/DwDXEkrTcxq+MDILYevZ0DvX/SAOeCXtyRuSoX93J2jBfKOLXB+niNA92yJ vRLQg34t2KnABCFNwm6pSQFl7s3ghraIkUbtntyIcyKvc7JB8Qfblubo+iWSq0QIfesW 4srM4PwnN8Sf9j+3KCaHQNwKY9hQCyekK1yaIIp3eGUXdJ+rn5ywV+MIs77UtvgElRQP KpbtQM7zzHKKuUPd6itX54MRszan6RcW7WipvcOWx7itAnCkJ3Mn/PsgaS/Six0Kvsu2 CbBg== X-Gm-Message-State: AOJu0Yyttwrzr3ciG/h5bdR6w5fL0pxYBSKpAadJF+JW6jvJuMG8ggwE s3nG2WK1YIPsuI42eqa7uCnR9Anci66Ul/SHOMi2epLwlgc7o1AG X-Google-Smtp-Source: AGHT+IFfq/Waisn/BjCT64FeoOnAF0nhcKKvbQHAslPduFlhgWCoJOLh+YfY0Iy42z7nQNI9Yx1lgA== X-Received: by 2002:ac2:5f1a:0:b0:513:a05d:7e9a with SMTP id 26-20020ac25f1a000000b00513a05d7e9amr4577840lfq.45.1711915416482; Sun, 31 Mar 2024 13:03:36 -0700 (PDT) Received: from BlackProtonRN.clients.gtk.su ([185.16.100.35]) by smtp.gmail.com with ESMTPSA id l5-20020ac25545000000b00516a08ce8f8sm425000lfk.269.2024.03.31.13.03.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Mar 2024 13:03:35 -0700 (PDT) From: Eugene Uriev To: Tom Rini Cc: u-boot@lists.denx.de, Eugene Uriev Subject: [PATCH 5/9] mcheck: support memalign Date: Sun, 31 Mar 2024 23:03:23 +0300 Message-Id: <20240331200327.29141-6-eugeneuriev@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240331200327.29141-1-eugeneuriev@gmail.com> References: <20240331200327.29141-1-eugeneuriev@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Sun, 31 Mar 2024 23:00:15 +0200 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.8 at phobos.denx.de X-Virus-Status: Clean Signed-off-by: Eugene Uriev --- common/dlmalloc.c | 7 ++++++- common/mcheck_core.inc.h | 20 ++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 8de15d7193..73c04af2a3 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -2265,7 +2265,12 @@ Void_t *rEALLOc(Void_t *oldmem, size_t bytes) Void_t *mEMALIGn(size_t alignment, size_t bytes) { - return NULL; + size_t fullsz = mcheck_memalign_prehook(alignment, bytes); + void *p = mEMALIGn_impl(alignment, fullsz); + + if (!p) + return p; + return mcheck_memalign_posthook(alignment, p, bytes); } // pvALLOc, vALLOc - redirect to mEMALIGn, defined here, so they need no wrapping. diff --git a/common/mcheck_core.inc.h b/common/mcheck_core.inc.h index 6f26ef00d9..b038bb0539 100644 --- a/common/mcheck_core.inc.h +++ b/common/mcheck_core.inc.h @@ -69,6 +69,7 @@ typedef struct { } mcheck_canary; struct mcheck_hdr { size_t size; /* Exact size requested by user. */ + size_t aln_skip; /* Ignored bytes, before the mcheck_hdr, to fulfill alignment */ mcheck_canary canary; /* Magic number to check header integrity. */ }; @@ -104,6 +105,7 @@ static inline size_t allign_size_up(size_t sz, size_t grain) } #define mcheck_allign_customer_size(SZ) allign_size_up(SZ, sizeof(mcheck_elem)) +#define mcheck_evaluate_memalign_prefix_size(ALIGN) allign_size_up(sizeof(struct mcheck_hdr), ALIGN) static enum mcheck_status mcheck_OnNok(enum mcheck_status status) { @@ -156,7 +158,8 @@ static void *mcheck_free_helper(void *ptr, int clean_content) if (clean_content) mcheck_flood(ptr, FREEFLOOD, mcheck_allign_customer_size(hdr->size)); - return hdr; + + return (char *)hdr - hdr->aln_skip; } static void *mcheck_free_prehook(void *ptr) { return mcheck_free_helper(ptr, CLEAN_CONTENT); } @@ -171,10 +174,13 @@ static size_t mcheck_alloc_prehook(size_t sz) static void *mcheck_allocated_helper(void *altoghether_ptr, size_t customer_sz, size_t alignment, int clean_content) { - struct mcheck_hdr *hdr = (struct mcheck_hdr *)altoghether_ptr; + const size_t slop = alignment ? + mcheck_evaluate_memalign_prefix_size(alignment) - sizeof(struct mcheck_hdr) : 0; + struct mcheck_hdr *hdr = (struct mcheck_hdr *)((char *)altoghether_ptr + slop); int i; hdr->size = customer_sz; + hdr->aln_skip = slop; for (i = 0; i < CANARY_DEPTH; ++i) hdr->canary.elems[i] = MAGICWORD; @@ -204,6 +210,16 @@ static void *mcheck_alloc_noclean_posthook(void *altoghether_ptr, size_t custome return mcheck_allocated_helper(altoghether_ptr, customer_sz, ANY_ALIGNMENT, KEEP_CONTENT); } +static size_t mcheck_memalign_prehook(size_t alig, size_t sz) +{ + return mcheck_evaluate_memalign_prefix_size(alig) + sz + sizeof(mcheck_canary); +} + +static void *mcheck_memalign_posthook(size_t alignment, void *altoghether_ptr, size_t customer_sz) +{ + return mcheck_allocated_helper(altoghether_ptr, customer_sz, alignment, CLEAN_CONTENT); +} + static enum mcheck_status mcheck_mprobe(void *ptr) { struct mcheck_hdr *hdr = &((struct mcheck_hdr *)ptr)[-1]; From patchwork Sun Mar 31 20:03:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eugene Uriev X-Patchwork-Id: 1918364 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=gin49LeB; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V764x0QgKz1yWv for ; Mon, 1 Apr 2024 08:01:21 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 144E6882C4; Sun, 31 Mar 2024 23:00:21 +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="gin49LeB"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CF94087F8B; Sun, 31 Mar 2024 22:03:40 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lf1-x132.google.com (mail-lf1-x132.google.com [IPv6:2a00:1450:4864:20::132]) (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 D40B987DF1 for ; Sun, 31 Mar 2024 22:03:38 +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=eugeneuriev@gmail.com Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-516a01c8490so797932e87.1 for ; Sun, 31 Mar 2024 13:03:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711915418; x=1712520218; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=CykDrKhEY3XMdGlZOyT5MTTTgwvqdMBuZvQx6VAYeN8=; b=gin49LeBdBiUyD8DqrpdKnJfi3rfKl12ID1gqLNXxLMw/IVuLm1Zdr4FWX+CQk0j9P Ch8Zp8T+ql07f8datXvfz3mrFwwGETlHuvhaPasUUVQLzVGI4Dr3dEYgJqoVgZhgOcp4 fOQzKWRAM1VHjL07TMJ4D3k2bmPTQ/aUiWobsKl1Mi9H2/9sujsfkcNxVT9HVEZZ8o6V npZUqhmbemamKE0LA8C1WcQfkNW97B7X1hxJSlq7J7GTvdU+bOeMT4qcC+L6zuWJX5oy DTkkHq5WR5mlsXAAFEbBbVhhJwgATo8yb9PSM2zBhuxJGmoecOKAS/opEplGs0FQ+iwq wqrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711915418; x=1712520218; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CykDrKhEY3XMdGlZOyT5MTTTgwvqdMBuZvQx6VAYeN8=; b=nQGce14V+yBdC/hwSVVPc/EnIyVPU8Xt7aiK9RheVDQi3cu9ywRCcvAoDQGaU55lMK lPpGYC4mCNJnHRX/EcajEp7c5CQpLePlxnVZct+LEBOt+EiIqYP0Lwm8Zbke/ThRpFhQ xcLDNwX7ik21C4jX/AVlcSnIDBV8SALSrQX7TB/LNUEOCPiKB9M+nKd5Pr89cwj94ulP oPOzGaqkRWS79SmLFs3irkK5phNDYAf4YBUc8G3T0VWxcI8ZobdO524mZ6GMJZyyJyOA Cpo+umUtUxzWjT+AzBNdE1Q8QrYlDgF+6KKTi4BISP52hnJcQjKuToc9S3zPsjtOCLDp NAag== X-Gm-Message-State: AOJu0Yx7T5HNVfyNCDoNekLko7xneHolDJc8CFXt2T5JYiSDMHbJKvJW ThbtE2kYtkuZC16yIlIOLnq3imU0dGEyvIImMj3jyuCuYGTite4H X-Google-Smtp-Source: AGHT+IFyIqqDQLVnWNfKi5FrSGBwVhyef1EHpgitd1keSlF8nEl/saU7tBmiWGLgphIDhCM7IAg7wg== X-Received: by 2002:ac2:532d:0:b0:515:89cc:26ab with SMTP id f13-20020ac2532d000000b0051589cc26abmr2564694lfh.9.1711915418051; Sun, 31 Mar 2024 13:03:38 -0700 (PDT) Received: from BlackProtonRN.clients.gtk.su ([185.16.100.35]) by smtp.gmail.com with ESMTPSA id l5-20020ac25545000000b00516a08ce8f8sm425000lfk.269.2024.03.31.13.03.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Mar 2024 13:03:37 -0700 (PDT) From: Eugene Uriev To: Tom Rini Cc: u-boot@lists.denx.de, Eugene Uriev Subject: [PATCH 6/9] mcheck: add pedantic mode support Date: Sun, 31 Mar 2024 23:03:24 +0300 Message-Id: <20240331200327.29141-7-eugeneuriev@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240331200327.29141-1-eugeneuriev@gmail.com> References: <20240331200327.29141-1-eugeneuriev@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Sun, 31 Mar 2024 23:00:15 +0200 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.8 at phobos.denx.de X-Virus-Status: Clean The pedantic mode is run-time contolled, so appropriate registry take place everytime. Maybe it's worth to use compile-time control only. So, the registry could be optimized out by an #ifdef. Signed-off-by: Eugene Uriev --- common/dlmalloc.c | 12 ++++++++++++ common/mcheck_core.inc.h | 41 ++++++++++++++++++++++++++++++++++++++++ include/mcheck.h | 9 +++++++++ 3 files changed, 62 insertions(+) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 73c04af2a3..a0616217d4 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -2233,6 +2233,7 @@ void cfree(mem) Void_t *mem; Void_t *mALLOc(size_t bytes) { + mcheck_pedantic_prehook(); size_t fullsz = mcheck_alloc_prehook(bytes); void *p = mALLOc_impl(fullsz); @@ -2245,6 +2246,7 @@ void fREe(Void_t *mem) { fREe_impl(mcheck_free_prehook(mem)); } Void_t *rEALLOc(Void_t *oldmem, size_t bytes) { + mcheck_pedantic_prehook(); if (bytes == 0) { if (oldmem) fREe(oldmem); @@ -2265,6 +2267,7 @@ Void_t *rEALLOc(Void_t *oldmem, size_t bytes) Void_t *mEMALIGn(size_t alignment, size_t bytes) { + mcheck_pedantic_prehook(); size_t fullsz = mcheck_memalign_prehook(alignment, bytes); void *p = mEMALIGn_impl(alignment, fullsz); @@ -2277,6 +2280,7 @@ Void_t *mEMALIGn(size_t alignment, size_t bytes) Void_t *cALLOc(size_t n, size_t elem_size) { + mcheck_pedantic_prehook(); // NB: here is no overflow check. size_t fullsz = mcheck_alloc_prehook(n * elem_size); void *p = cALLOc_impl(1, fullsz); @@ -2287,12 +2291,20 @@ Void_t *cALLOc(size_t n, size_t elem_size) } // mcheck API { +int mcheck_pedantic(mcheck_abortfunc_t f) +{ + mcheck_initialize(f, 1); + return 0; +} + int mcheck(mcheck_abortfunc_t f) { mcheck_initialize(f, 0); return 0; } +void mcheck_check_all(void) { mcheck_pedantic_check(); } + enum mcheck_status mprobe(void *__ptr) { return mcheck_mprobe(__ptr); } // mcheck API } #endif diff --git a/common/mcheck_core.inc.h b/common/mcheck_core.inc.h index b038bb0539..85a34de295 100644 --- a/common/mcheck_core.inc.h +++ b/common/mcheck_core.inc.h @@ -30,6 +30,8 @@ * Unlike glibc-clients, U-Boot has limited malloc-usage, and only one thread. * So it's better to make the protection heavier. * Thus overflow canary here is greater, than glibc's one. Underflow canary is bigger too. + * U-Boot also allows to use fixed-size heap-registry, instead of double-linked list in glibc. + * * Heavy canary allows to catch not only memset(..)-errors, * but overflow/underflow of struct-array access: * { @@ -61,8 +63,14 @@ #define FREEFLOOD ((char)0xf5) #define PADDINGFLOOD ((char)0x58) +// my normal run demands 4427-6449 chunks: +#define REGISTRY_SZ 6608 #define CANARY_DEPTH 2 +// avoid problems with BSS at early stage: +static char mcheck_pedantic_flag __section(".data") = 0; +static void *mcheck_registry[REGISTRY_SZ] __section(".data") = {0}; + typedef unsigned long long mcheck_elem; typedef struct { mcheck_elem elems[CANARY_DEPTH]; @@ -159,6 +167,12 @@ static void *mcheck_free_helper(void *ptr, int clean_content) if (clean_content) mcheck_flood(ptr, FREEFLOOD, mcheck_allign_customer_size(hdr->size)); + for (i = 0; i < REGISTRY_SZ; ++i) + if (mcheck_registry[i] == hdr) { + mcheck_registry[i] = 0; + break; + } + return (char *)hdr - hdr->aln_skip; } @@ -197,6 +211,17 @@ static void *mcheck_allocated_helper(void *altoghether_ptr, size_t customer_sz, for (i = 0; i < CANARY_DEPTH; ++i) tail->elems[i] = MAGICTAIL; + + for (i = 0; i < REGISTRY_SZ; ++i) + if (!mcheck_registry[i]) { + mcheck_registry[i] = hdr; + return payload; // normal end + } + + static char *overflow_msg = "\n\n\nERROR: mcheck registry overflow, pedantic check would be incomplete!!\n\n\n\n"; + + printf("%s", overflow_msg); + overflow_msg = "(mcheck registry full)"; return payload; } @@ -227,9 +252,25 @@ static enum mcheck_status mcheck_mprobe(void *ptr) return mcheck_checkhdr(hdr); } +static void mcheck_pedantic_check(void) +{ + int i; + + for (i = 0; i < REGISTRY_SZ; ++i) + if (mcheck_registry[i]) + mcheck_checkhdr(mcheck_registry[i]); +} + +static void mcheck_pedantic_prehook(void) +{ + if (mcheck_pedantic_flag) + mcheck_pedantic_check(); +} + static void mcheck_initialize(mcheck_abortfunc_t new_func, char pedantic_flag) { mcheck_abortfunc = (new_func) ? new_func : &mcheck_default_abort; + mcheck_pedantic_flag = pedantic_flag; } #endif diff --git a/include/mcheck.h b/include/mcheck.h index a049745e4e..f4c9b7e61c 100644 --- a/include/mcheck.h +++ b/include/mcheck.h @@ -33,6 +33,15 @@ typedef void (*mcheck_abortfunc_t)(enum mcheck_status); int mcheck(mcheck_abortfunc_t func); +/* + * Similar to `mcheck' but performs checks for all block whenever one of + * the memory handling functions is called. This can be very slow. + */ +int mcheck_pedantic(mcheck_abortfunc_t f); + +/* Force check of all blocks now. */ +void mcheck_check_all(void); + /* * Check for aberrations in a particular malloc'd block. These are the * same checks that `mcheck' does, when you free or reallocate a block. From patchwork Sun Mar 31 20:03:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eugene Uriev X-Patchwork-Id: 1918365 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=g3fDSdnf; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V76560cS7z1yWv for ; Mon, 1 Apr 2024 08:01:30 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id AEA0387FF2; Sun, 31 Mar 2024 23:00:21 +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="g3fDSdnf"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0989087F74; Sun, 31 Mar 2024 22:03:42 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) (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 307DC87C82 for ; Sun, 31 Mar 2024 22:03:40 +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=eugeneuriev@gmail.com Received: by mail-lf1-x12d.google.com with SMTP id 2adb3069b0e04-515c198e835so3845826e87.3 for ; Sun, 31 Mar 2024 13:03:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711915419; x=1712520219; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gi/NBORItLLjd1uawMUB02FO6QSi/DEwP+jrSTxEsqI=; b=g3fDSdnfK5gsquAdOJGLaI4R4JvN5NCqcuegB+mqSCOEIH/WBjXgjcNSaFeZxyKyhi 5fQUZBhVzTwp+vSj64zMUCtSOmgfrNlMbgqX24W/voxzbeXHnO4D1w5876ZUENpwiWui fFjq3VMFF15atMvFPqQDlYaLDySPu9ww4NAkPOdOtdDn8mlNanvWpPATHaZ8hWjDS9oi W1qYHaiPdiCGXyzhDm9Vp24k0K9/cR+isg3m/JSXW+eaGjOvprKgw9yUdDpnsUlDE8Fl 2x901lrUd0W64wwH7KM8rwsRuugBZg1dbJ4/rxwKC2qJq70VVuvq6XnBoQiGPgMc3Moq UWAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711915419; x=1712520219; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gi/NBORItLLjd1uawMUB02FO6QSi/DEwP+jrSTxEsqI=; b=QA1HbDpiRgZHrIF+hLKoC7NqgrCLluBQmBLMSSyCkZ8NZwO28Vxn5h5Gl40nbibhXY Me4F5jPekJON8PHVIQ0r+EwmbuwmPGci0b11PKG1r0R+b7siLfn3z/InkigwM5V+IBen iXCo368AcdvB5ucP7WedZLrFDdYZmPZVrFnDjpYXNu11gggOMnssgOTJNkGOb6DYzNqX N3XgtZbjSvddhBD+We+aKlpspu8jpTgOX7NWXIh6Ylr6AHJ4iA0sCjMJYNANsCQRmyDi RtvJdijeG5cvh2Ja5IjDM9Jgx+xbmQO5E+gSQNGnt8Fa0ersI8FmhXEIohcVDOJ4CGZy ncdQ== X-Gm-Message-State: AOJu0Yyoy3y756jB7+ERXJvdR/kedOE9iKOmXtjo69JK53r65qWGlLPf i8aRzFKcGJlb8H5MLjIFPtD8zVMxIEz3pKRBVgXFGSkGLVQIJilI X-Google-Smtp-Source: AGHT+IEiNARPz25puFTFbMkiTFyohVyZ32SvJwNZPj0WnwILsEu4g6/JfaCgENv19px0QyAt2dVaUA== X-Received: by 2002:a05:6512:475:b0:515:cf44:904e with SMTP id x21-20020a056512047500b00515cf44904emr5123338lfd.46.1711915419498; Sun, 31 Mar 2024 13:03:39 -0700 (PDT) Received: from BlackProtonRN.clients.gtk.su ([185.16.100.35]) by smtp.gmail.com with ESMTPSA id l5-20020ac25545000000b00516a08ce8f8sm425000lfk.269.2024.03.31.13.03.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Mar 2024 13:03:38 -0700 (PDT) From: Eugene Uriev To: Tom Rini Cc: u-boot@lists.denx.de, Eugene Uriev Subject: [PATCH 7/9] mcheck: introduce mcheck_on_ramrelocation(.) Date: Sun, 31 Mar 2024 23:03:25 +0300 Message-Id: <20240331200327.29141-8-eugeneuriev@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240331200327.29141-1-eugeneuriev@gmail.com> References: <20240331200327.29141-1-eugeneuriev@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Sun, 31 Mar 2024 23:00:15 +0200 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.8 at phobos.denx.de X-Virus-Status: Clean The using of pre-reloc/malloc_simple heap is too hard to follow after the relocation. So lets drop it from the pedantic registry and switch to dlmalloc, when moved. The offset is ignored, but kept in the API for the probable case, when that early heap is relocated too. Signed-off-by: Eugene Uriev --- common/board_f.c | 4 ++++ common/mcheck_core.inc.h | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/common/board_f.c b/common/board_f.c index 442b8349d0..ca4d5291a9 100644 --- a/common/board_f.c +++ b/common/board_f.c @@ -719,6 +719,7 @@ static int reloc_bloblist(void) return 0; } +void mcheck_on_ramrelocation(size_t offset); static int setup_reloc(void) { if (!(gd->flags & GD_FLG_SKIP_RELOC)) { @@ -744,6 +745,9 @@ static int setup_reloc(void) if (gd->flags & GD_FLG_SKIP_RELOC) { debug("Skipping relocation due to flag\n"); } else { +#ifdef MCHECK_HEAP_PROTECTION + mcheck_on_ramrelocation(gd->reloc_off); +#endif debug("Relocation Offset is: %08lx\n", gd->reloc_off); debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n", gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd), diff --git a/common/mcheck_core.inc.h b/common/mcheck_core.inc.h index 85a34de295..bade03598f 100644 --- a/common/mcheck_core.inc.h +++ b/common/mcheck_core.inc.h @@ -273,5 +273,16 @@ static void mcheck_initialize(mcheck_abortfunc_t new_func, char pedantic_flag) mcheck_pedantic_flag = pedantic_flag; } +void mcheck_on_ramrelocation(size_t offset) +{ + char *p; + int i; + // Simple, but inaccurate strategy: drop the pre-reloc heap + for (i = 0; i < REGISTRY_SZ; ++i) + if ((p = mcheck_registry[i]) != NULL ) { + printf("mcheck, WRN: forgetting %p chunk\n", p); + mcheck_registry[i] = 0; + } +} #endif #endif From patchwork Sun Mar 31 20:03:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eugene Uriev X-Patchwork-Id: 1918366 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=L39LKsnU; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V765H0F4Rz1yWv for ; Mon, 1 Apr 2024 08:01:39 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 56408882D9; Sun, 31 Mar 2024 23:00:22 +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="L39LKsnU"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 75D7187F77; Sun, 31 Mar 2024 22:03:43 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) (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 876D687D45 for ; Sun, 31 Mar 2024 22:03:41 +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=eugeneuriev@gmail.com Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-515ac73c516so3548229e87.0 for ; Sun, 31 Mar 2024 13:03:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711915421; x=1712520221; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wVkt/rIBjeu9Yqfi63A9RbaAeLYqqut84gRhnOzbIZM=; b=L39LKsnU76p3f7c0JQxZGwNK9sswMDzU0uKh4iy199DLbhaXdqrNIETeYfl56gXPge noXiDzhgNqhlkpDPSVXLYG+zKGqY89GRpNpCkcngNmoZBjIUaD17SoV4W6/jRdNwe5Pi Cfm9ooeHLPdPhzByU0lcN/42RSU7CghB/XQBFDOKz6io3TTq/A7krVOmfJBxDFH58Meu TV29bCt6sxP91YQ7j2VDI/exfUQ24qSOyH16USkxqtWoHtsN3UTd2krplboNX7ONn/sV 4Yyufe8mEyzB8iA7Dc87YzyYjtBMqutEsssngcox6PXxiwl7CAtiICTFJwHEdjJJu4YU sGOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711915421; x=1712520221; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wVkt/rIBjeu9Yqfi63A9RbaAeLYqqut84gRhnOzbIZM=; b=kHaPg45jJr32Th65y3m2TADonjh77vUiJt2c+OplKgrX/VCVyWVoBYfzfoTAaCzgxY /p8Vk85Iupf7QMZENaGJTXmR0bIdmdFiF0Wag7Ow6O6eSvs0eWrTS3fcPrfYUv/2Gg16 ky8+BSAMXlgpWil24x5Gv2LRZ9BOEuLPuc+lvC1/7GJkd7D8SeKPoSFBrXOLX9qzPByq soWmSvLRf5uYnZNoR22ftOawQy3p/l/q3//zsfO74ySfsY9E8m9OVigC1ZjJ1hOsc6tN ojFCSjmI3o/egPInaxqYYlOl1WyJVZA24RJdO2FWYqIHOFIAXosFs0YYp2AsKvkSfNzO MHUA== X-Gm-Message-State: AOJu0Yx6eIetWqMGQ2j/wuY4/Exs8IGVDlfV4s0VV+fZv2XmuMKV1cGj 8b9IieO3t0ykp7LJI6v3Pkq5TYd8FA6lmwW0P50a1yA7kEgCtpAz X-Google-Smtp-Source: AGHT+IGDYBdhxsYcGBueqog56ZpQk8Xq91TQ6hZMISkWw6KjLcXctzfMmB8yhZ+0KbQNNzMFcmugzw== X-Received: by 2002:a05:6512:3102:b0:513:dd84:a088 with SMTP id n2-20020a056512310200b00513dd84a088mr3975833lfb.21.1711915420761; Sun, 31 Mar 2024 13:03:40 -0700 (PDT) Received: from BlackProtonRN.clients.gtk.su ([185.16.100.35]) by smtp.gmail.com with ESMTPSA id l5-20020ac25545000000b00516a08ce8f8sm425000lfk.269.2024.03.31.13.03.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Mar 2024 13:03:39 -0700 (PDT) From: Eugene Uriev To: Tom Rini Cc: u-boot@lists.denx.de, Eugene Uriev Subject: [PATCH 8/9] mcheck: add stats, add a comment with test results Date: Sun, 31 Mar 2024 23:03:26 +0300 Message-Id: <20240331200327.29141-9-eugeneuriev@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240331200327.29141-1-eugeneuriev@gmail.com> References: <20240331200327.29141-1-eugeneuriev@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Sun, 31 Mar 2024 23:00:15 +0200 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.8 at phobos.denx.de X-Virus-Status: Clean My tests have been run on an U-Boot (of older version) for ARM (64bits). Signed-off-by: Eugene Uriev --- common/mcheck_core.inc.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/common/mcheck_core.inc.h b/common/mcheck_core.inc.h index bade03598f..2f11ac567f 100644 --- a/common/mcheck_core.inc.h +++ b/common/mcheck_core.inc.h @@ -48,6 +48,13 @@ * rather in compile-time, by MCHECK_HEAP_PROTECTION macro. That guarantees that * we haven't missed first malloc. */ + +/* + * Testing + * This library had been successfully tested for U-Boot @ ARM SoC chip / 64bits. + * Proven for both default and pedantic mode: confirms U-Boot to be clean, and catches + * intentional/testing corruptions. Working with malloc_trim is not tested. + */ #ifndef _MCHECKCORE_INC_H #define _MCHECKCORE_INC_H 1 #include "mcheck.h" @@ -70,6 +77,8 @@ // avoid problems with BSS at early stage: static char mcheck_pedantic_flag __section(".data") = 0; static void *mcheck_registry[REGISTRY_SZ] __section(".data") = {0}; +static size_t mcheck_chunk_count __section(".data") = 0; +static size_t mcheck_chunk_count_max __section(".data") = 0; typedef unsigned long long mcheck_elem; typedef struct { @@ -102,7 +111,7 @@ static void mcheck_default_abort(enum mcheck_status status) msg = "bogus mcheck_status, library is buggy\n"; break; } - printf("\n\nmcheck: %s!!!\n\n", msg); + printf("\n\nmcheck: %s!!! [%zu]\n\n", msg, mcheck_chunk_count_max); } static mcheck_abortfunc_t mcheck_abortfunc = &mcheck_default_abort; @@ -173,6 +182,7 @@ static void *mcheck_free_helper(void *ptr, int clean_content) break; } + --mcheck_chunk_count; return (char *)hdr - hdr->aln_skip; } @@ -212,6 +222,10 @@ static void *mcheck_allocated_helper(void *altoghether_ptr, size_t customer_sz, for (i = 0; i < CANARY_DEPTH; ++i) tail->elems[i] = MAGICTAIL; + ++mcheck_chunk_count; + if (mcheck_chunk_count > mcheck_chunk_count_max) + mcheck_chunk_count_max = mcheck_chunk_count; + for (i = 0; i < REGISTRY_SZ; ++i) if (!mcheck_registry[i]) { mcheck_registry[i] = hdr; @@ -283,6 +297,8 @@ void mcheck_on_ramrelocation(size_t offset) printf("mcheck, WRN: forgetting %p chunk\n", p); mcheck_registry[i] = 0; } + + mcheck_chunk_count = 0; } #endif #endif From patchwork Sun Mar 31 20:03:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eugene Uriev X-Patchwork-Id: 1918367 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=j/vsk1kE; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V765S0z4Xz1yWv for ; Mon, 1 Apr 2024 08:01:48 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 035728825C; Sun, 31 Mar 2024 23:00:23 +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="j/vsk1kE"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CC66887E50; Sun, 31 Mar 2024 22:03:44 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) (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 DB0FB87F74 for ; Sun, 31 Mar 2024 22:03:42 +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=eugeneuriev@gmail.com Received: by mail-lf1-x12b.google.com with SMTP id 2adb3069b0e04-515d515e28dso1110583e87.0 for ; Sun, 31 Mar 2024 13:03:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711915422; x=1712520222; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2SN9H1KWdNsEF/z0kmR2dcyCmCCNFhLM7FXzlMpjzoA=; b=j/vsk1kEYAqW2xqzNGhWPoL1Vu1lgQNTxaiYqrxmtyEgRSYx0xlFfZv7nkDvWlBFwX smNo6k79teDvB/9lC7poJPXUrxnGBMA8HYSZozjsbmXghCCtm0U4S7ArOSkklbF8xPxx csaYA4Jv5GDxIDb8MOGj//mHfSQKvSjvHRAbFmFMU2LFUuNguzEwUYrPDHIiuF1aWkxB uJgzU2wxaVp4JGaN4WcEzIakm4C2N1uDQjdsS8rtbnARROtv6V3xv5FTf9e6Hc9X/u37 WrB1bAp5SuQFHX22hwpFz2bx0Ls+2DBCmUDAqh7+EMioc6FmBuP6Zou0HFAlw0102RlM X1YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711915422; x=1712520222; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2SN9H1KWdNsEF/z0kmR2dcyCmCCNFhLM7FXzlMpjzoA=; b=vHj2tewAgEsj6kAWIY74pWdLEqPQhMBbytcv0nX48QPD9NYb+yH5rNbb/L1yLUSfio aw/xp6uoi/PBJUMkAbcR9EB+y9fwgFtHlsrrhhjVMFWnof+m0uv2MNnftKrkAOjfIW1L omLRBucb4Kg0m50SD3ZAjmXJruTCjfd5op8W7lkZAO7FqtDkg4wFAIk+TFUJWHHOQMnv zycSndWtxqfLj72VvX0EaCOKujug+vzT2pXvXEd+ZQWxf/x2o6wjU3WpBjka7IvH5KLI NQMAqyh1rThI4sMsfqtOPzeGxNePoEzE7Shdd9AH2B5uW6AUCkNIK1ih0+08RrmLJeEs gmzw== X-Gm-Message-State: AOJu0YwGGQWGWyQTkARmZ/xmnBUwLYJebAW20SJ355PHBWh1woqbRH9p XBxTkC3xMn4PKEJXF9A1VDvINepLvWuBnNIOGgmddnPsY0g37iWr/96HukAC X-Google-Smtp-Source: AGHT+IHoTxLTErqZFsUAv/+VfqZ4+llAQGLAhuICVCqD7H+7aizDMuhncBXvkmYgklWd83cVZIxP8g== X-Received: by 2002:a05:6512:48c6:b0:515:cedb:a518 with SMTP id er6-20020a05651248c600b00515cedba518mr4517112lfb.16.1711915422161; Sun, 31 Mar 2024 13:03:42 -0700 (PDT) Received: from BlackProtonRN.clients.gtk.su ([185.16.100.35]) by smtp.gmail.com with ESMTPSA id l5-20020ac25545000000b00516a08ce8f8sm425000lfk.269.2024.03.31.13.03.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Mar 2024 13:03:41 -0700 (PDT) From: Eugene Uriev To: Tom Rini Cc: u-boot@lists.denx.de, Eugene Uriev Subject: [PATCH 9/9] mcheck: let mcheck_abortfunc_t print the pointer Date: Sun, 31 Mar 2024 23:03:27 +0300 Message-Id: <20240331200327.29141-10-eugeneuriev@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240331200327.29141-1-eugeneuriev@gmail.com> References: <20240331200327.29141-1-eugeneuriev@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Sun, 31 Mar 2024 23:00:15 +0200 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.8 at phobos.denx.de X-Virus-Status: Clean Signed-off-by: Eugene Uriev --- common/mcheck_core.inc.h | 16 ++++++++-------- include/mcheck.h | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/common/mcheck_core.inc.h b/common/mcheck_core.inc.h index 2f11ac567f..6902140992 100644 --- a/common/mcheck_core.inc.h +++ b/common/mcheck_core.inc.h @@ -90,7 +90,7 @@ struct mcheck_hdr { mcheck_canary canary; /* Magic number to check header integrity. */ }; -static void mcheck_default_abort(enum mcheck_status status) +static void mcheck_default_abort(enum mcheck_status status, const void *p) { const char *msg; @@ -111,7 +111,7 @@ static void mcheck_default_abort(enum mcheck_status status) msg = "bogus mcheck_status, library is buggy\n"; break; } - printf("\n\nmcheck: %s!!! [%zu]\n\n", msg, mcheck_chunk_count_max); + printf("\n\nmcheck: %p:%s!!! [%zu]\n\n", p, msg, mcheck_chunk_count_max); } static mcheck_abortfunc_t mcheck_abortfunc = &mcheck_default_abort; @@ -124,9 +124,9 @@ static inline size_t allign_size_up(size_t sz, size_t grain) #define mcheck_allign_customer_size(SZ) allign_size_up(SZ, sizeof(mcheck_elem)) #define mcheck_evaluate_memalign_prefix_size(ALIGN) allign_size_up(sizeof(struct mcheck_hdr), ALIGN) -static enum mcheck_status mcheck_OnNok(enum mcheck_status status) +static enum mcheck_status mcheck_OnNok(enum mcheck_status status, const void *p) { - (*mcheck_abortfunc)(status); + (*mcheck_abortfunc)(status, p); return status; } @@ -136,11 +136,11 @@ static enum mcheck_status mcheck_checkhdr(const struct mcheck_hdr *hdr) for (i = 0; i < CANARY_DEPTH; ++i) if (hdr->canary.elems[i] == MAGICFREE) - return mcheck_OnNok(MCHECK_FREE); + return mcheck_OnNok(MCHECK_FREE, hdr + 1); for (i = 0; i < CANARY_DEPTH; ++i) if (hdr->canary.elems[i] != MAGICWORD) - return mcheck_OnNok(MCHECK_HEAD); + return mcheck_OnNok(MCHECK_HEAD, hdr + 1); const size_t payload_size = hdr->size; const size_t payload_size_aligned = mcheck_allign_customer_size(payload_size); @@ -150,13 +150,13 @@ static enum mcheck_status mcheck_checkhdr(const struct mcheck_hdr *hdr) for (i = 0; i < padd_size; ++i) if (payload[payload_size + i] != PADDINGFLOOD) - return mcheck_OnNok(MCHECK_TAIL); + return mcheck_OnNok(MCHECK_TAIL, hdr + 1); const mcheck_canary *tail = (const mcheck_canary *)&payload[payload_size_aligned]; for (i = 0; i < CANARY_DEPTH; ++i) if (tail->elems[i] != MAGICTAIL) - return mcheck_OnNok(MCHECK_TAIL); + return mcheck_OnNok(MCHECK_TAIL, hdr + 1); return MCHECK_OK; } diff --git a/include/mcheck.h b/include/mcheck.h index f4c9b7e61c..bd506ae629 100644 --- a/include/mcheck.h +++ b/include/mcheck.h @@ -29,7 +29,7 @@ enum mcheck_status { MCHECK_TAIL /* Memory after the block was clobbered. */ }; -typedef void (*mcheck_abortfunc_t)(enum mcheck_status); +typedef void (*mcheck_abortfunc_t)(enum mcheck_status, const void *p); int mcheck(mcheck_abortfunc_t func);