[Concept,04/37] test: Use TOTAL_MALLOC_LEN for abuf and alist tests

Message ID 20251201170529.3237986-5-sjg@u-boot.org
State New
Headers
Series malloc: Import dlmalloc 2.8.6 |

Commit Message

Simon Glass Dec. 1, 2025, 5:04 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Several tests use CONFIG_SYS_MALLOC_LEN to test allocations that should
fail due to exceeding pool size. However, the actual malloc pool size is
TOTAL_MALLOC_LEN, which includes CONFIG_ENV_SIZE for boards that need to
store the environment in RAM. The extra space accommodates:

- the hash table allocated via calloc()
- strdup() calls for each environment variable key
- strdup() calls for each environment variable value

This is an estimate and typically consumes less than CONFIG_ENV_SIZE,
leaving more free space in the malloc pool than was reserved.

On qemu-x86_64, CONFIG_ENV_SIZE is 0x40000, making the actual pool
0x240000 bytes. Tests expecting malloc(CONFIG_SYS_MALLOC_LEN) to fail
might unexpectedly succeed since there's more space available.

Update all tests to use TOTAL_MALLOC_LEN to correctly reflect the actual
malloc pool size.

Co-developed-by: Claude <noreply@anthropic.com>
---

 test/lib/abuf.c  | 5 +++--
 test/lib/alist.c | 3 ++-
 2 files changed, 5 insertions(+), 3 deletions(-)
  

Patch

diff --git a/test/lib/abuf.c b/test/lib/abuf.c
index 9cbb627d0b6..e97bb8b66bc 100644
--- a/test/lib/abuf.c
+++ b/test/lib/abuf.c
@@ -5,6 +5,7 @@ 
  */
 
 #include <abuf.h>
+#include <env_internal.h>
 #include <mapmem.h>
 #include <test/lib.h>
 #include <test/test.h>
@@ -244,7 +245,7 @@  static int lib_test_abuf_large(struct unit_test_state *uts)
 
 	/* Try an impossible size */
 	abuf_init(&buf);
-	ut_asserteq(false, abuf_realloc(&buf, CONFIG_SYS_MALLOC_LEN));
+	ut_asserteq(false, abuf_realloc(&buf, TOTAL_MALLOC_LEN));
 	ut_assertnull(buf.data);
 	ut_asserteq(0, buf.size);
 	ut_asserteq(false, buf.alloced);
@@ -264,7 +265,7 @@  static int lib_test_abuf_large(struct unit_test_state *uts)
 	ut_assert(delta > 0);
 
 	/* try to increase it */
-	ut_asserteq(false, abuf_realloc(&buf, CONFIG_SYS_MALLOC_LEN));
+	ut_asserteq(false, abuf_realloc(&buf, TOTAL_MALLOC_LEN));
 	ut_asserteq_ptr(ptr, buf.data);
 	ut_asserteq(TEST_DATA_LEN, buf.size);
 	ut_asserteq(true, buf.alloced);
diff --git a/test/lib/alist.c b/test/lib/alist.c
index 0bf24578d2e..108eaed8d92 100644
--- a/test/lib/alist.c
+++ b/test/lib/alist.c
@@ -5,6 +5,7 @@ 
  */
 
 #include <alist.h>
+#include <env_internal.h>
 #include <string.h>
 #include <test/lib.h>
 #include <test/test.h>
@@ -41,7 +42,7 @@  static int lib_test_alist_init(struct unit_test_state *uts)
 
 	/* use an impossible size */
 	ut_asserteq(false, alist_init(&lst, obj_size,
-				      CONFIG_SYS_MALLOC_LEN));
+				      TOTAL_MALLOC_LEN));
 	ut_assertnull(lst.data);
 	ut_asserteq(0, lst.count);
 	ut_asserteq(0, lst.alloc);