public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Sean Anderson <seanga2@gmail.com>
To: Simon Glass <sjg@chromium.org>,
	U-Boot Mailing List <u-boot@lists.denx.de>
Cc: "Kishon Vijay Abraham I" <kishon@ti.com>,
	"Michal Simek" <michal.simek@amd.com>,
	"Marek Behún" <kabel@kernel.org>, "Tom Rini" <trini@konsulko.com>,
	"Heinrich Schuchardt" <xypron.glpk@gmx.de>
Subject: Re: [PATCH v2 12/45] test: Support testing malloc() failures
Date: Wed, 28 Sep 2022 13:17:57 -0400	[thread overview]
Message-ID: <c8e574b2-df6f-d0fc-b7e9-9b244ae3c076@gmail.com> (raw)
In-Reply-To: <20220907022733.66423-13-sjg@chromium.org>

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().
> 
> Add a simple way to force failure after a given number of malloc() calls.
> 
> Fix a header guard to avoid a build error on sandbox_vpl.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
> 
> (no changes since v1)
> 
>   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(+)
> 
> diff --git a/arch/sandbox/include/asm/malloc.h b/arch/sandbox/include/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 @@
>    */
>   
>   #ifndef __ASM_MALLOC_H
> +#define __ASM_MALLOC_H
>   
>   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 = 0;
>   ulong mem_malloc_end = 0;
>   ulong mem_malloc_brk = 0;
>   
> +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 = mem_malloc_brk;
> @@ -1307,6 +1310,11 @@ Void_t* mALLOc(bytes) size_t bytes;
>   		return malloc_simple(bytes);
>   #endif
>   
> +  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 == 0) && (mem_malloc_end == 0)) {
>       /* not initialized yet */
> @@ -2470,6 +2478,17 @@ int initf_malloc(void)
>   	return 0;
>   }
>   
> +void malloc_enable_testing(int max_allocs)
> +{
> +	malloc_testing = true;
> +	malloc_max_allocs = max_allocs;
> +}
> +
> +void malloc_disable_testing(void)
> +{
> +	malloc_testing = false;
> +}
> +
>   /*
>   
>   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();
>   
>   void malloc_simple_info(void);
>   
> +/**
> + * 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 malloc()
> + */
> +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_state *uts)
>   	uts->force_fail_alloc = false;
>   	uts->skip_post_probe = false;
>   	gd->dm_root = NULL;
> +	malloc_disable_testing();
>   	if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
>   		memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
>   	arch_reset_for_test();

Reviewed-by: Sean Anderson <seanga2@gmail.com>

but do we need to instrument rEALLOc too?

  reply	other threads:[~2022-09-28 17:18 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-07  2:26 [PATCH v2 00/45] dm: core: Support multiple device trees in ofnode Simon Glass
2022-09-07  2:26 ` [PATCH v2 01/45] image: Fix BOOTM_STATE values Simon Glass
2022-09-30 19:52   ` Tom Rini
2022-09-07  2:26 ` [PATCH v2 02/45] treewide: Drop bootm_headers_t typedef Simon Glass
2022-09-07  2:26 ` [PATCH v2 03/45] treewide: Drop image_info_t typedef Simon Glass
2022-09-07  2:26 ` [PATCH v2 04/45] treewide: Drop image_header_t typedef Simon Glass
2022-09-07  2:26 ` [PATCH v2 05/45] log: update the comment for log_msg_ret() Simon Glass
2022-09-07  2:26 ` [PATCH v2 06/45] sandbox: power: Update PMIC driver to use log Simon Glass
2022-09-08 11:36   ` Jaehoon Chung
2022-09-07  2:26 ` [PATCH v2 07/45] event: Fix a typo in the EVENT help Simon Glass
2022-09-07  2:26 ` [PATCH v2 08/45] event: Allow multiple spy declarations for each event Simon Glass
2022-09-07  2:26 ` [PATCH v2 09/45] dm: core: Pass a root node to of_find_node_by_phandle() Simon Glass
2022-09-07  2:26 ` [PATCH v2 10/45] event: Pass the images to EVT_FT_FIXUP Simon Glass
2022-09-07  2:26 ` [PATCH v2 11/45] test: Fix missing livetree test runs Simon Glass
2022-09-07  2:27 ` [PATCH v2 12/45] test: Support testing malloc() failures Simon Glass
2022-09-28 17:17   ` Sean Anderson [this message]
2022-09-28 21:06     ` Simon Glass
2022-09-28 21:50       ` Sean Anderson
2022-09-29  2:36         ` Simon Glass
2022-09-29  4:02           ` Sean Anderson
2022-09-29 12:42             ` Tom Rini
2022-09-07  2:27 ` [PATCH v2 13/45] dm: core: Document the livetree structures properly Simon Glass
2022-09-07  2:27 ` [PATCH v2 14/45] dm: core: Allow adding ofnode subnodes Simon Glass
2022-09-07  2:27 ` [PATCH v2 15/45] dm: core: Support writing a property to an empty node Simon Glass
2022-09-07  2:27 ` [PATCH v2 16/45] dm: core: Drop the const from ofnode Simon Glass
2022-09-07  2:27 ` [PATCH v2 17/45] test: Make a copy of the device tree before running a test Simon Glass
2022-09-07  2:27 ` [PATCH v2 18/45] test: Detect a change in the device tree Simon Glass
2022-09-07  2:27 ` [PATCH v2 19/45] test: Drop the UT_TESTF_LIVE_OR_FLAT flag Simon Glass
2022-09-07  2:27 ` [PATCH v2 20/45] sandbox: Add a function to load a relative file path Simon Glass
2022-09-07  2:27 ` [PATCH v2 21/45] sandbox: Support loading the other FDT Simon Glass
2022-09-07  2:27 ` [PATCH v2 22/45] sandbox: Support setting up the other FDT for testing Simon Glass
2022-09-07  2:27 ` [PATCH v2 23/45] sandbox: test: Provide an easy way to use the other FDT Simon Glass
2022-09-07  2:27 ` [PATCH v2 24/45] dm: core: Reduce code size with dev_of_offset() Simon Glass
2022-09-07  2:27 ` [PATCH v2 25/45] dm: core: Rename ofnode_get_first/next_property() Simon Glass
2022-09-07  2:27 ` [PATCH v2 26/45] dm: core: Rename ofnode_get_property_by_prop() Simon Glass
2022-09-07  2:27 ` [PATCH v2 27/45] dm: core: Avoid creating a name property when unflattening Simon Glass
2022-09-07  2:27 ` [PATCH v2 28/45] dm: core: Add a macro to iterate through properties Simon Glass
2022-09-07  2:27 ` [PATCH v2 29/45] dm: core: Drop ofnode_is_available() Simon Glass
2022-09-07  2:27 ` [PATCH v2 30/45] dm: core: Expand integer-reading tests Simon Glass
2022-09-29 23:13   ` Tom Rini
2022-09-29 23:55     ` Simon Glass
2022-09-07  2:27 ` [PATCH v2 31/45] dm: core: Provide a way to reset the device tree Simon Glass
2022-09-07  2:27 ` [PATCH v2 32/45] dm: core: Add an ofnode function to obtain the flat tree Simon Glass
2022-09-07  2:27 ` [PATCH v2 33/45] dm: core: Add ofnode functions to obtain an oftree Simon Glass
2022-09-07  2:27 ` [PATCH v2 34/45] dm: core: Add a way to look up a phandle in " Simon Glass
2022-09-07  2:27 ` [PATCH v2 35/45] dm: core: Allow obtaining a node offset in the same tree Simon Glass
2022-09-07  2:27 ` [PATCH v2 36/45] dm: core: Split ofnode_path_root() into two functions Simon Glass
2022-09-07  2:27 ` [PATCH v2 37/45] dm: core: Add definitions for multiple ofnode trees Simon Glass
2022-09-07  2:27 ` [PATCH v2 38/45] dm: core: Add the ofnode multi-tree implementation Simon Glass
2022-09-07  2:27 ` [PATCH v2 39/45] dm: core: Complete phandle implementation using the other FDT Simon Glass
2022-09-07  2:27 ` [PATCH v2 40/45] dm: core: Update comments for default-FDT ofnode functions Simon Glass
2022-09-07  2:27 ` [PATCH v2 41/45] dm: core: Create a function to get a live tree in a test Simon Glass
2022-09-07  2:27 ` [PATCH v2 42/45] dm: core: Expand ofnode tests Simon Glass
2022-09-07  2:27 ` [PATCH v2 43/45] vbe: Allow test to run with live/flat tree Simon Glass
2022-09-07  2:27 ` [PATCH v2 44/45] dm: core: Allow copying ofnode property data when writing Simon Glass
2022-09-07  2:27 ` [PATCH v2 45/45] dm: core: Support copying properties with ofnode Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c8e574b2-df6f-d0fc-b7e9-9b244ae3c076@gmail.com \
    --to=seanga2@gmail.com \
    --cc=kabel@kernel.org \
    --cc=kishon@ti.com \
    --cc=michal.simek@amd.com \
    --cc=sjg@chromium.org \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox