[PATCH] tools/nolibc: add alloca()

Thomas Weißschuh posted 1 patch 2 months ago
tools/include/nolibc/Makefile                |  1 +
tools/include/nolibc/alloca.h                | 15 +++++++++++++++
tools/include/nolibc/nolibc.h                |  1 +
tools/testing/selftests/nolibc/nolibc-test.c | 14 ++++++++++++++
4 files changed, 31 insertions(+)
[PATCH] tools/nolibc: add alloca()
Posted by Thomas Weißschuh 2 months ago
Add the wide-used alloca() function. As it is highly machine and
compiler dependent, just defer to the compiler builtin. This has
been available since GCC 4 and clang 3.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 tools/include/nolibc/Makefile                |  1 +
 tools/include/nolibc/alloca.h                | 15 +++++++++++++++
 tools/include/nolibc/nolibc.h                |  1 +
 tools/testing/selftests/nolibc/nolibc-test.c | 14 ++++++++++++++
 4 files changed, 31 insertions(+)

diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile
index 7455097cff69..039ba9d59dfa 100644
--- a/tools/include/nolibc/Makefile
+++ b/tools/include/nolibc/Makefile
@@ -20,6 +20,7 @@ OUTPUT ?= $(CURDIR)/
 architectures := arm arm64 loongarch m68k mips powerpc riscv s390 sh sparc x86
 arch_files := arch.h $(addsuffix .h, $(addprefix arch-, $(architectures)))
 all_files := \
+		alloca.h \
 		byteswap.h \
 		compiler.h \
 		crt.h \
diff --git a/tools/include/nolibc/alloca.h b/tools/include/nolibc/alloca.h
new file mode 100644
index 000000000000..448233a79e6e
--- /dev/null
+++ b/tools/include/nolibc/alloca.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
+/*
+ * alloca() for NOLIBC
+ * Copyright (C) 2026 Thomas Weißschuh <linux@weissschuh.net>
+ */
+
+/* make sure to include all global symbols */
+#include "nolibc.h"
+
+#ifndef _NOLIBC_ALLOCA_H
+#define _NOLIBC_ALLOCA_H
+
+#define alloca(size) __builtin_alloca(size)
+
+#endif /* _NOLIBC_ALLOCA_H */
diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h
index f4120f65fe79..e93b9c18b48c 100644
--- a/tools/include/nolibc/nolibc.h
+++ b/tools/include/nolibc/nolibc.h
@@ -133,6 +133,7 @@
 #include "err.h"
 #include "byteswap.h"
 #include "endian.h"
+#include "alloca.h"
 
 /* Used by programs to avoid std includes */
 #define NOLIBC
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index d3c4facb54c0..cfb797bf416c 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -45,6 +45,7 @@
 #include <stdbool.h>
 #include <byteswap.h>
 #include <endian.h>
+#include <alloca.h>
 
 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
 
@@ -1516,6 +1517,18 @@ int run_syscall(int min, int max)
 	return ret;
 }
 
+int test_alloca(void)
+{
+	uint64_t *x;
+
+	x = alloca(sizeof(*x));
+
+	*x = 0x1234;
+	__asm__ ("" : "+r" (x));
+
+	return *x - 0x1234;
+}
+
 int test_difftime(void)
 {
 	if (difftime(200., 100.) != 100.)
@@ -1731,6 +1744,7 @@ int run_stdlib(int min, int max)
 		CASE_TEST(toupper_noop);            EXPECT_EQ(1, toupper('A'), 'A'); break;
 		CASE_TEST(abs);                     EXPECT_EQ(1, abs(-10), 10); break;
 		CASE_TEST(abs_noop);                EXPECT_EQ(1, abs(10), 10); break;
+		CASE_TEST(alloca);                  EXPECT_ZR(1, test_alloca()); break;
 		CASE_TEST(difftime);                EXPECT_ZR(1, test_difftime()); break;
 		CASE_TEST(memchr_foobar6_o);        EXPECT_STREQ(1, memchr("foobar", 'o', 6), "oobar"); break;
 		CASE_TEST(memchr_foobar3_b);        EXPECT_STRZR(1, memchr("foobar", 'b', 3)); break;

---
base-commit: 598b670af347bc8d998866b1e795e40a3bb168de
change-id: 20260409-nolibc-alloca-00c65280263f

Best regards,
--  
Thomas Weißschuh <linux@weissschuh.net>

Re: [PATCH] tools/nolibc: add alloca()
Posted by Willy Tarreau 2 months ago
Hi Thomas,

On Thu, Apr 09, 2026 at 05:55:28PM +0200, Thomas Weißschuh wrote:
> Add the wide-used alloca() function. As it is highly machine and
> compiler dependent, just defer to the compiler builtin. This has
> been available since GCC 4 and clang 3.

Yes it's indeed very useful, I'm also using it at some places, with
the same define.

Acked-by: Willy Tarreau <w@1wt.eu>

thanks!
Willy