2022-03-21 22:27:43

by Ammar Faizi

[permalink] [raw]
Subject: [RFC PATCH v1 0/6] Add dynamic memory allocator support for nolibc

Hi,

This is the v1 of RFC to add dynamic memory allocator support for
nolibc.


## Background

The need to allocate memory dynamically has become a requirement for
the C programming language. Mainly it happens when the allocation size
is determined at runtime. Many other use cases also do it when the
object's lifetime is long-lived and needs to be recycled at runtime.

Currently, the nolibc header doesn't support such a type of allocation.
This series adds it.


## Implementation

Add basic functions to manage dynamic memory allocation:
- malloc()
- calloc()
- realloc()
- free()

The allocator uses mmap() syscall to allocate the memory and uses
munmap() syscall to free the allocated memory.

The metadata to keep track the length for munmap-ing is simply
defined as a struct below:
```
struct nolibc_heap {
size_t len;
char user_p[] __attribute__((__aligned__));
};
```
malloc(), realloc() and calloc() return a pointer to `user_p`.


## Add my_syscall6() support for x86 32-bit.

mmap() needs 6 arguments to work with. Not all architectures that
nolibc supports have the my_syscall6() wrapper. This series also
adds my_syscall6() wrapper support for i386.

Notes:

Both Clang and GCC cannot use %ebp in the clobber list and in the "r"
constraint without using -fomit-frame-pointer. To make it always
available for any kind of compilation, the below workaround is
implemented.

For clang (the Assembly statement can't clobber %ebp):
1) Save the %ebp value to the redzone area -4(%esp).
2) Load the 6-th argument from memory to %ebp.
3) Subtract the %esp by 4.
4) Do the syscall (int $0x80).
5) Pop %ebp.

For GCC, fortunately it has a #pragma that can force a specific function
to be compiled with -fomit-frame-pointer, so it can use "r"(var) where
var is a variable bound to %ebp.


## Limitation

Currently, for mips and arm arch cannot use these dynamic memory allocator
functions because they're missing the my_syscall6() macro.

[ammarfaizi2: I would love to add the support for them too, but I don't
have the hardware to play with MIPS and ARM. ]


## Test

The following simple program can be used to test this series:

https://gist.github.com/ammarfaizi2/db0af6aa0b95a0c7478bce64e349f021


## Patchset Summary

1) Patch 1 is a fix for the System V ABI document link.

2) Patch 2 is a fix to support compile with clang.

3) Patch 3 adds my_syscall6() implementation for i386.

4) Patch 4 adds mmap() and munmap() functions.

5) Patch 5 adds malloc(), calloc(), realloc() and free().

6) Patch 6 adds strdup() and strndup().


Signed-off-by: Ammar Faizi <[email protected]>
---
Ammar Faizi (6):
tools/nolibc: x86-64: Update System V ABI document link
tools/nolibc: Make the entry point not weak for clang
tools/nolibc: i386: Implement syscall with 6 arguments
tools/nolibc/sys: Implement `mmap()` and `munmap()`
tools/nolibc/stdlib: Implement `malloc()`, `calloc()`, `realloc()` and `free()`
tools/include/string: Implement `strdup()` and `strndup()`

tools/include/nolibc/arch-aarch64.h | 2 +
tools/include/nolibc/arch-arm.h | 2 +
tools/include/nolibc/arch-i386.h | 66 ++++++++++++++++++++++++
tools/include/nolibc/arch-mips.h | 2 +
tools/include/nolibc/arch-riscv.h | 2 +
tools/include/nolibc/arch-x86_64.h | 4 +-
tools/include/nolibc/stdlib.h | 79 +++++++++++++++++++++++++++++
tools/include/nolibc/string.h | 68 +++++++++++++++++++++++++
tools/include/nolibc/sys.h | 62 ++++++++++++++++++++++
9 files changed, 286 insertions(+), 1 deletion(-)


base-commit: fda0d5d1b79d8b7032be3d7720a481a9fde91baf
--
Ammar Faizi


2022-03-21 23:06:35

by Ammar Faizi

[permalink] [raw]
Subject: [RFC PATCH v1 5/6] tools/nolibc/stdlib: Implement `malloc()`, `calloc()`, `realloc()` and `free()`

Implement basic dynamic allocator functions. These functions are
currently only available on architectures that have nolibc mmap()
syscall implemented. These are not a super-fast memory allocator,
but at least they can satisfy basic needs for having heap without
libc.

Signed-off-by: Ammar Faizi <[email protected]>
---
tools/include/nolibc/stdlib.h | 79 +++++++++++++++++++++++++++++++++++
1 file changed, 79 insertions(+)

diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h
index 733105c574ee..13600e73404d 100644
--- a/tools/include/nolibc/stdlib.h
+++ b/tools/include/nolibc/stdlib.h
@@ -10,8 +10,24 @@
#include "std.h"
#include "arch.h"
#include "types.h"
+#include "string.h"
#include "sys.h"

+struct nolibc_heap {
+ size_t len;
+ char user_p[] __attribute__((__aligned__));
+};
+
+#ifndef offsetof
+#define offsetof(TYPE, FIELD) ((size_t) &((TYPE *)0)->FIELD)
+#endif
+
+#ifndef container_of
+#define container_of(PTR, TYPE, FIELD) ({ \
+ __typeof__(((TYPE *)0)->FIELD) *__FIELD_PTR = (PTR); \
+ (TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD)); \
+})
+#endif

/* Buffer used to store int-to-ASCII conversions. Will only be implemented if
* any of the related functions is implemented. The area is large enough to
@@ -60,6 +76,18 @@ int atoi(const char *s)
return atol(s);
}

+static __attribute__((unused))
+void free(void *ptr)
+{
+ struct nolibc_heap *heap;
+
+ if (!ptr)
+ return;
+
+ heap = container_of(ptr, struct nolibc_heap, user_p);
+ munmap(heap, heap->len);
+}
+
/* Converts the unsigned long integer <in> to its hex representation into
* buffer <buffer>, which must be long enough to store the number and the
* trailing zero (17 bytes for "ffffffffffffffff" or 9 for "ffffffff"). The
@@ -182,6 +210,57 @@ char *ltoa(long in)
return itoa_buffer;
}

+static inline __attribute__((unused))
+void *malloc(size_t len)
+{
+ struct nolibc_heap *heap;
+
+ heap = mmap(NULL, sizeof(*heap) + len, PROT_READ|PROT_WRITE,
+ MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
+ if (__builtin_expect(heap == MAP_FAILED, 0))
+ return NULL;
+
+ heap->len = sizeof(*heap) + len;
+ return heap->user_p;
+}
+
+static inline __attribute__((unused))
+void *calloc(size_t size, size_t nmemb)
+{
+ void *orig;
+ size_t res = 0;
+
+ if (__builtin_expect(__builtin_mul_overflow(nmemb, size, &res), 0)) {
+ SET_ERRNO(ENOMEM);
+ return NULL;
+ }
+
+ /*
+ * No need to zero the heap, the MAP_ANONYMOUS in malloc()
+ * already does it.
+ */
+ return malloc(res);
+}
+
+static inline __attribute__((unused))
+void *realloc(void *old_ptr, size_t new_size)
+{
+ struct nolibc_heap *heap;
+ void *ret;
+
+ if (!old_ptr)
+ return malloc(new_size);
+
+ ret = malloc(new_size);
+ if (__builtin_expect(!ret, 0))
+ return NULL;
+
+ heap = container_of(old_ptr, struct nolibc_heap, user_p);
+ memcpy(ret, heap->user_p, heap->len);
+ munmap(heap, heap->len);
+ return ret;
+}
+
/* converts unsigned long integer <in> to a string using the static itoa_buffer
* and returns the pointer to that string.
*/
--
Ammar Faizi

2022-03-21 23:28:17

by Alviro Iskandar Setiawan

[permalink] [raw]
Subject: Re: [RFC PATCH v1 5/6] tools/nolibc/stdlib: Implement `malloc()`, `calloc()`, `realloc()` and `free()`

On Sun, Mar 20, 2022 at 4:37 PM Ammar Faizi wrote:
> +void *realloc(void *old_ptr, size_t new_size)
> +{
> + struct nolibc_heap *heap;
> + void *ret;
> +
> + if (!old_ptr)
> + return malloc(new_size);
> +
> + ret = malloc(new_size);
> + if (__builtin_expect(!ret, 0))
> + return NULL;
> +
> + heap = container_of(old_ptr, struct nolibc_heap, user_p);
> + memcpy(ret, heap->user_p, heap->len);
> + munmap(heap, heap->len);
> + return ret;
> +}

This better be simplified like this, so only have 1 malloc() call that
applies to both branches.

void *realloc(void *old_ptr, size_t new_size)
{
struct nolibc_heap *heap;
void *ret;

ret = malloc(new_size);
if (__builtin_expect(!ret, 0))
return NULL;

if (!old_ptr)
return ret;

heap = container_of(old_ptr, struct nolibc_heap, user_p);
memcpy(ret, heap->user_p, heap->len);
munmap(heap, heap->len);
return ret;
}

-- Viro