Skip to contentSkip to conversation

nolibc: Don't use `malloc()` and `free()` as the function name

PATCH Applied Ammar Faizi 2 participants 2 messages Jan 23, 2022 io_uring

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nolibc: Don't use `malloc()` and `free()` as the function name

Miyasaki reports that liburing with CONFIG_NOLIBC breaks apps that use libc. The first spotted issue was a realloc() call that results in an invalid pointer, and then the program exits with SIGABRT.

The cause is liburing nolibc overrides malloc() and free() from the libc (especially when we statically link the "liburing.a" to the apps that use libc).

The malloc() and free() from liburing nolibc use hand-coded Assembly to invoke mmap() and munmap() syscall directly. That means any allocation from malloc() is not a valid object with respect to the libc, and free() will also break the app if we use it to free a pointer from a libc function (e.g., strdup()).

liburing nolibc should not break any libc app.

This renames the malloc() and free() to __uring_malloc() and __uring_free(). Also, add new inline functions to wrap the malloc() and free(), they are uring_malloc() and uring_free(). These wrappers will call the appropriate functions depending on CONFIG_NOLIBC.

Fixes f48ee3168cdc325233825603269f304d348d323c ("Add nolibc build support")
Cc Tea Inside Mailing List <[email protected]>
---
 src/lib.h    | 20 ++++++++++++++++++++
 src/nolibc.c |  4 ++--
 src/setup.c  |  6 +++---
 3 files changed, 25 insertions(+), 5 deletions(-)
src/lib.h +20 -0
index 58d91be..bd02805 100644
@@ -25,6 +25,26 @@
 })
 #endif
 
+void *__uring_malloc(size_t len);
+void __uring_free(void *p);
+
+static inline void *uring_malloc(size_t len)
+{
+#ifdef CONFIG_NOLIBC
+	return __uring_malloc(len);
+#else
+	return malloc(len);
+#endif
+}
+
+static inline void uring_free(void *ptr)
+{
+#ifdef CONFIG_NOLIBC
+	__uring_free(ptr);
+#else
+	free(ptr);
+#endif
+}
 
 static inline long get_page_size(void)
 {
src/nolibc.c +2 -2
index 251780b..f7848d3 100644
@@ -23,7 +23,7 @@ struct uring_heap {
 	char		user_p[] __attribute__((__aligned__));
 };
 
-void *malloc(size_t len)
+void *__uring_malloc(size_t len)
 {
 	struct uring_heap *heap;
 
@@ -36,7 +36,7 @@ void *malloc(size_t len)
 	return heap->user_p;
 }
 
-void free(void *p)
+void __uring_free(void *p)
 {
 	struct uring_heap *heap;
 
src/setup.c +3 -3
index 891fc43..1e4dbf4 100644
@@ -178,7 +178,7 @@ struct io_uring_probe *io_uring_get_probe_ring(struct io_uring *ring)
 	int r;
 
 	len = sizeof(*probe) + 256 * sizeof(struct io_uring_probe_op);
-	probe = malloc(len);
+	probe = uring_malloc(len);
 	if (!probe)
 		return NULL;
 	memset(probe, 0, len);
@@ -187,7 +187,7 @@ struct io_uring_probe *io_uring_get_probe_ring(struct io_uring *ring)
 	if (r >= 0)
 		return probe;
 
-	free(probe);
+	uring_free(probe);
 	return NULL;
 }
 
@@ -208,7 +208,7 @@ struct io_uring_probe *io_uring_get_probe(void)
 
 void io_uring_free_probe(struct io_uring_probe *probe)
 {
-	free(probe);
+	uring_free(probe);
 }
 
 static inline int __fls(int x)
Signature · 1 line
2.32.0
maintainer Applied
#1 lore raw
Quoted 9 lines · Ammar Faizi

Miyasaki reports that liburing with CONFIG_NOLIBC breaks apps that use libc. The first spotted issue was a realloc() call that results in an invalid pointer, and then the program exits with SIGABRT.

The cause is liburing nolibc overrides malloc() and free() from the libc (especially when we statically link the "liburing.a" to the apps that use libc).

[...]

Applied, thanks!

[1/1] nolibc: Don't use malloc() and free() as the function name

      commit: 29ff69397fa13478b5619201347c51159874279e

Best regards,

Signature · 1 line
Jens Axboe