From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 4/6] sandbox: libc_malloc: fail allocations exceeding MALLOC_MAX_SIZE
Date: Thu, 20 Feb 2025 21:30:30 +0100 [thread overview]
Message-ID: <20250220203032.3350247-4-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250220203032.3350247-1-a.fatoum@pengutronix.de>
The libc allocator is a bit unpredictable, because overcommit can
mean that big allocations succeed initially, only to OOM later.
Let's thus enforce a maximum allocation limit on the barebox side in
alignment with the bare metal allocators.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
arch/sandbox/os/libc_malloc.c | 24 ++++++++++++++++++++----
1 file changed, 20 insertions(+), 4 deletions(-)
diff --git a/arch/sandbox/os/libc_malloc.c b/arch/sandbox/os/libc_malloc.c
index e34156cd49e7..ac97fc37eee5 100644
--- a/arch/sandbox/os/libc_malloc.c
+++ b/arch/sandbox/os/libc_malloc.c
@@ -7,6 +7,8 @@
#include <malloc.h>
#define BAREBOX_ENOMEM 12
+#define BAREBOX_MALLOC_MAX_SIZE 0x40000000
+
extern int barebox_errno;
void barebox_malloc_stats(void)
@@ -15,7 +17,10 @@ void barebox_malloc_stats(void)
void *barebox_memalign(size_t alignment, size_t bytes)
{
- void *mem = memalign(alignment, bytes);
+ void *mem = NULL;
+
+ if (alignment <= BAREBOX_MALLOC_MAX_SIZE && bytes <= BAREBOX_MALLOC_MAX_SIZE)
+ mem = memalign(alignment, bytes);
if (!mem)
barebox_errno = BAREBOX_ENOMEM;
@@ -25,7 +30,10 @@ void *barebox_memalign(size_t alignment, size_t bytes)
void *barebox_malloc(size_t size)
{
- void *mem = malloc(size);
+ void *mem = NULL;
+
+ if (size <= BAREBOX_MALLOC_MAX_SIZE)
+ mem = malloc(size);
if (!mem)
barebox_errno = BAREBOX_ENOMEM;
@@ -44,7 +52,10 @@ void barebox_free(void *ptr)
void *barebox_realloc(void *ptr, size_t size)
{
- void *mem = realloc(ptr, size);
+ void *mem = NULL;
+
+ if (size <= BAREBOX_MALLOC_MAX_SIZE)
+ mem = realloc(ptr, size);
if (!mem)
barebox_errno = BAREBOX_ENOMEM;
@@ -53,7 +64,12 @@ void *barebox_realloc(void *ptr, size_t size)
void *barebox_calloc(size_t n, size_t elem_size)
{
- void *mem = calloc(n, elem_size);
+ size_t product;
+ void *mem = NULL;
+
+ if (!__builtin_add_overflow(n, elem_size, &product) &&
+ product <= BAREBOX_MALLOC_MAX_SIZE)
+ mem = calloc(n, elem_size);
if (!mem)
barebox_errno = BAREBOX_ENOMEM;
--
2.39.5
next prev parent reply other threads:[~2025-02-20 20:31 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-20 20:30 [PATCH 1/6] malloc: define a maximum malloc size Ahmad Fatoum
2025-02-20 20:30 ` [PATCH 2/6] tlsf: fail allocations exceeding MALLOC_MAX_SIZE Ahmad Fatoum
2025-02-20 20:30 ` [PATCH 3/6] dlmalloc: " Ahmad Fatoum
2025-02-20 20:30 ` Ahmad Fatoum [this message]
2025-02-20 20:30 ` [PATCH 5/6] dummy_malloc: " Ahmad Fatoum
2025-02-20 20:30 ` [PATCH 6/6] test: self: malloc: adapt to addition of MALLOC_MAX_SIZE Ahmad Fatoum
2025-02-21 12:20 ` [PATCH 1/6] malloc: define a maximum malloc size Sascha Hauer
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=20250220203032.3350247-4-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/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