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 40390C433F5 for ; Thu, 29 Sep 2022 12:42:22 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 4205884963; Thu, 29 Sep 2022 14:42:19 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=konsulko.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=konsulko.com header.i=@konsulko.com header.b="lveKTxCF"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1A7CF84BB9; Thu, 29 Sep 2022 14:42:18 +0200 (CEST) Received: from mail-qt1-x833.google.com (mail-qt1-x833.google.com [IPv6:2607:f8b0:4864:20::833]) (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 2CF2884942 for ; Thu, 29 Sep 2022 14:42:15 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=konsulko.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=trini@konsulko.com Received: by mail-qt1-x833.google.com with SMTP id a20so644983qtw.10 for ; Thu, 29 Sep 2022 05:42:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=konsulko.com; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=99Csv62Je8jgsKd1mcU4lb0Z4sJYZRoxaG10WOSXhzc=; b=lveKTxCFx9xT57vR38Alnn3k+BJ3Prbd6Gsz186GAENyLwJdvIOIKfk6aTgSbdpC6R KpQVqPi4gC2AtfUFbcW697Dzd28ZqnCNsCIishDe07xfSyRD7uPo8GqZL7vMvd6c+eUv 1oTwJcn/lzVW+jHgiQGV+BwZY8h3vYiV/GM3o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=99Csv62Je8jgsKd1mcU4lb0Z4sJYZRoxaG10WOSXhzc=; b=3MzjoLCThQ1uVi0g9opEe96jxvS19c42TMLxjKkusl4aiziRV5gqElTtsvSUa6qV+J qAxsqvyVIX7cTxG0oYvYSmiRBE4+XsFoGZYI4lXRTcaYRze+UQMMBCofk7pcuqohI1qe QmDandwD31LVXWkMo3eEPEXIBp5viwS50gabWfzzn8R4IlQ9014r2uU3FLhUi8OAUPSy Y5jao2IbphHPMgKe9hoqDyLF34fRGJ2iqwewEbmRiXbuFhqpwXa9Sa8KeMvThOb7FbhZ ezWMhDqySBaL4q3K3gDggeKA5eaDbn3BupyEjb4PQT/x+lDpEH1Tek2vX4KO/crLlQSb IszA== X-Gm-Message-State: ACrzQf11e3CavoqFNaUkB26QTX2++u2fU4Y2q4+vGbKSSDVozud+ZtGy l3mqU90xsIL3RC2ObetoOYy6/Q== X-Google-Smtp-Source: AMsMyM6B8ZDTzeyPY6YoaPSg09WJ15kgjMzA9A/88QRdbFFK0q80r3ahKXWl660LkiPYHJKvvla5kQ== X-Received: by 2002:a05:622a:4ca:b0:35d:5888:3ef3 with SMTP id q10-20020a05622a04ca00b0035d58883ef3mr2126621qtx.337.1664455333889; Thu, 29 Sep 2022 05:42:13 -0700 (PDT) Received: from bill-the-cat (2603-6081-7b00-6400-0186-ea3a-a82b-3964.res6.spectrum.com. [2603:6081:7b00:6400:186:ea3a:a82b:3964]) by smtp.gmail.com with ESMTPSA id bj25-20020a05620a191900b006ce2c3c48ebsm6076135qkb.77.2022.09.29.05.42.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Sep 2022 05:42:13 -0700 (PDT) Date: Thu, 29 Sep 2022 08:42:11 -0400 From: Tom Rini To: Sean Anderson Cc: Simon Glass , U-Boot Mailing List , Kishon Vijay Abraham I , Michal Simek , Marek =?iso-8859-1?Q?Beh=FAn?= , Heinrich Schuchardt Subject: Re: [PATCH v2 12/45] test: Support testing malloc() failures Message-ID: <20220929124211.GX3044094@bill-the-cat> References: <20220907022733.66423-1-sjg@chromium.org> <20220907022733.66423-13-sjg@chromium.org> <90ea7fd1-e162-098d-37e7-90505fd7a408@gmail.com> <5aa4fe71-6f2c-0c97-37cc-40076859c1f5@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="KCGU/3tJP9FZjbY4" Content-Disposition: inline In-Reply-To: <5aa4fe71-6f2c-0c97-37cc-40076859c1f5@gmail.com> X-Clacks-Overhead: GNU Terry Pratchett 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.6 at phobos.denx.de X-Virus-Status: Clean --KCGU/3tJP9FZjbY4 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Sep 29, 2022 at 12:02:43AM -0400, Sean Anderson wrote: > On 9/28/22 22:36, Simon Glass wrote: > > Hi Sean, > >=20 > > On Wed, 28 Sept 2022 at 15:50, Sean Anderson wrote: > > >=20 > > > On 9/28/22 17:06, Simon Glass wrote: > > > > Hi Sean, > > > >=20 > > > > On Wed, 28 Sept 2022 at 11:18, Sean Anderson wr= ote: > > > > >=20 > > > > > On 9/6/22 22:27, Simon Glass wrote: > > > > > > It is helpful to test that out-of-memory checks work correctly = in code > > > > > > that calls malloc(). > > > > > >=20 > > > > > > Add a simple way to force failure after a given number of mallo= c() calls. > > > > > >=20 > > > > > > Fix a header guard to avoid a build error on sandbox_vpl. > > > > > >=20 > > > > > > Signed-off-by: Simon Glass > > > > > > --- > > > > > >=20 > > > > > > (no changes since v1) > > > > > >=20 > > > > > > arch/sandbox/include/asm/malloc.h | 1 + > > > > > > common/dlmalloc.c | 19 +++++++++++++++++++ > > > > > > include/malloc.h | 12 ++++++++++++ > > > > > > test/test-main.c | 1 + > > > > > > 4 files changed, 33 insertions(+) > > > > > >=20 > > > > > > diff --git a/arch/sandbox/include/asm/malloc.h b/arch/sandbox/i= nclude/asm/malloc.h > > > > > > index a1467b5eadd..8aaaa9cb87b 100644 > > > > > > --- a/arch/sandbox/include/asm/malloc.h > > > > > > +++ b/arch/sandbox/include/asm/malloc.h > > > > > > @@ -6,6 +6,7 @@ > > > > > > */ > > > > > >=20 > > > > > > #ifndef __ASM_MALLOC_H > > > > > > +#define __ASM_MALLOC_H > > > > > >=20 > > > > > > void *malloc(size_t size); > > > > > > void free(void *ptr); > > > > > > diff --git a/common/dlmalloc.c b/common/dlmalloc.c > > > > > > index f48cd2a333d..41c7230424c 100644 > > > > > > --- a/common/dlmalloc.c > > > > > > +++ b/common/dlmalloc.c > > > > > > @@ -596,6 +596,9 @@ ulong mem_malloc_start =3D 0; > > > > > > ulong mem_malloc_end =3D 0; > > > > > > ulong mem_malloc_brk =3D 0; > > > > > >=20 > > > > > > +static bool malloc_testing; /* enable test mode */ > > > > > > +static int malloc_max_allocs; /* return NULL after this= many calls to malloc() */ > > > > > > + > > > > > > void *sbrk(ptrdiff_t increment) > > > > > > { > > > > > > ulong old =3D mem_malloc_brk; > > > > > > @@ -1307,6 +1310,11 @@ Void_t* mALLOc(bytes) size_t bytes; > > > > > > return malloc_simple(bytes); > > > > > > #endif > > > > > >=20 > > > > > > + if (CONFIG_IS_ENABLED(UNIT_TEST) && malloc_testing) { > > > > > > + if (--malloc_max_allocs < 0) > > > > > > + return NULL; > > > > > > + } > > > > > > + > > > > > > /* check if mem_malloc_init() was run */ > > > > > > if ((mem_malloc_start =3D=3D 0) && (mem_malloc_end =3D=3D= 0)) { > > > > > > /* not initialized yet */ > > > > > > @@ -2470,6 +2478,17 @@ int initf_malloc(void) > > > > > > return 0; > > > > > > } > > > > > >=20 > > > > > > +void malloc_enable_testing(int max_allocs) > > > > > > +{ > > > > > > + malloc_testing =3D true; > > > > > > + malloc_max_allocs =3D max_allocs; > > > > > > +} > > > > > > + > > > > > > +void malloc_disable_testing(void) > > > > > > +{ > > > > > > + malloc_testing =3D false; > > > > > > +} > > > > > > + > > > > > > /* > > > > > >=20 > > > > > > History: > > > > > > diff --git a/include/malloc.h b/include/malloc.h > > > > > > index e8c8b254c0d..161ccbd1298 100644 > > > > > > --- a/include/malloc.h > > > > > > +++ b/include/malloc.h > > > > > > @@ -883,6 +883,18 @@ extern Void_t* sbrk(); > > > > > >=20 > > > > > > void malloc_simple_info(void); > > > > > >=20 > > > > > > +/** > > > > > > + * malloc_enable_testing() - Put malloc() into test mode > > > > > > + * > > > > > > + * This only works if UNIT_TESTING is enabled > > > > > > + * > > > > > > + * @max_allocs: return -ENOMEM after max_allocs calls to mallo= c() > > > > > > + */ > > > > > > +void malloc_enable_testing(int max_allocs); > > > > > > + > > > > > > +/** malloc_disable_testing() - Put malloc() into normal mode */ > > > > > > +void malloc_disable_testing(void); > > > > > > + > > > > > > #if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE) > > > > > > #define malloc malloc_simple > > > > > > #define realloc realloc_simple > > > > > > diff --git a/test/test-main.c b/test/test-main.c > > > > > > index c65cbd7c351..5b6b5ba5bbe 100644 > > > > > > --- a/test/test-main.c > > > > > > +++ b/test/test-main.c > > > > > > @@ -46,6 +46,7 @@ static int dm_test_pre_run(struct unit_test_s= tate *uts) > > > > > > uts->force_fail_alloc =3D false; > > > > > > uts->skip_post_probe =3D false; > > > > > > gd->dm_root =3D NULL; > > > > > > + malloc_disable_testing(); > > > > > > if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_P= LATDATA)) > > > > > > memset(dm_testdrv_op_count, '\0', sizeof(dm_tes= tdrv_op_count)); > > > > > > arch_reset_for_test(); > > > > >=20 > > > > > Reviewed-by: Sean Anderson > > > > >=20 > > > > > but do we need to instrument rEALLOc too? > > > >=20 > > > > We could, but I'd prefer to have a test which needs it first. Actua= lly > > > > realloc() calls malloc() in some cases, so we are already messing w= ith > > > > things here. > > >=20 > > > Maybe it would be better to do this at the "top-level" instead. E.g. > > > redefine the malloc() et al macro. That way we remove any double-coun= ting. > >=20 > > Yes, that makes sense. But again, I'd rather do this when we have a > > use case for making realloc() fail. >=20 > Fine by me. >=20 > > BTW has dlmalloc been updated in recent years? >=20 > Apparently there's a 2.8.6, last updated in 2012 [1]. And I _think_ when I looked a year or two back there wasn't anything standing out as an important to try and update change in there. But I could be wrong and have missed something. --=20 Tom --KCGU/3tJP9FZjbY4 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQGzBAABCgAdFiEEGjx/cOCPqxcHgJu/FHw5/5Y0tywFAmM1kp8ACgkQFHw5/5Y0 tywqiQv8Ce0yqZs4l7ue/OE9pRITwgLRaTQmalt9x2vF0CU69tn2znKnPpqjoX34 TnC85SCrFzX+/iYOCfjK/ga3xBqaUC5DGAzMT8TR1i81jo+FLVL3zg81Gmczb7VD FuWi0DVHKbEeY6WIdsBK1p7LjDcYGuvmmuZM/Ip9v46cWMTNs7ljD/imX+ZCbbJB gglfZ66WhVacaYRlsUXTT/uJkbuyPh1nGK6QUxZP5ybq9MFmxnFTbEpIY5v8ow9g CO4DROnGcoNWRg7wOc4pEPeYwuwiCjdCnktseY6EwFYprzzmMY7nIw4MMvEJxw/u luEhWmKpGXMq0XE7cSKoCKdoq8aBoBoYck24RlADyG+sE1zJ0MGP+/MtlJmTmyS8 Y5D1IojEJ/FRosIq8C8ZM3iDxMDcurywi8VPZj9tdMVf7TbjELFpJE3xxZ1TDNkP zSoTxITCXAiMrIAft+NGf60McTIRSER9dsriKraWyWR0zukg85VDQEhIXTuM5kij WcZknQHg =3GWY -----END PGP SIGNATURE----- --KCGU/3tJP9FZjbY4--