From 41b0a5310ff6e9be955e596385c6a380bd3c9ec7 Mon Sep 17 00:00:00 2001 From: niansa Date: Wed, 3 Apr 2024 11:10:57 +0200 Subject: [PATCH] Implement heap management correctly --- Makefile | 22 +++++++++++----------- crosslibc | 2 +- prep.sh | 2 +- src/clibc-user-impl.c | 25 +++++++++++++++++++++++++ src/main.c | 7 +++++++ 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index aeaece9..dbc5ec1 100644 --- a/Makefile +++ b/Makefile @@ -7,37 +7,37 @@ clean: obj/clibc-user-impl-c.o: src/clibc-user-impl.c - gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c src/clibc-user-impl.c -o obj/clibc-user-impl-c.o + gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -DCLIBC_NO_LL_HEAP -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c src/clibc-user-impl.c -o obj/clibc-user-impl-c.o obj/linux-uefi-c.o: src/linux-uefi.c - gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c src/linux-uefi.c -o obj/linux-uefi-c.o + gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -DCLIBC_NO_LL_HEAP -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c src/linux-uefi.c -o obj/linux-uefi-c.o obj/main-c.o: src/main.c - gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c src/main.c -o obj/main-c.o + gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -DCLIBC_NO_LL_HEAP -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c src/main.c -o obj/main-c.o obj/atof-c.o: crosslibc/atof.c - gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c crosslibc/atof.c -o obj/atof-c.o + gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -DCLIBC_NO_LL_HEAP -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c crosslibc/atof.c -o obj/atof-c.o obj/heap-c.o: crosslibc/heap.c - gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c crosslibc/heap.c -o obj/heap-c.o + gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -DCLIBC_NO_LL_HEAP -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c crosslibc/heap.c -o obj/heap-c.o obj/math-c.o: crosslibc/math.c - gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c crosslibc/math.c -o obj/math-c.o + gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -DCLIBC_NO_LL_HEAP -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c crosslibc/math.c -o obj/math-c.o obj/stdio-c.o: crosslibc/stdio.c - gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c crosslibc/stdio.c -o obj/stdio-c.o + gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -DCLIBC_NO_LL_HEAP -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c crosslibc/stdio.c -o obj/stdio-c.o obj/string-c.o: crosslibc/string.c - gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c crosslibc/string.c -o obj/string-c.o + gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -DCLIBC_NO_LL_HEAP -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c crosslibc/string.c -o obj/string-c.o obj/strtod-c.o: crosslibc/strtod.c - gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c crosslibc/strtod.c -o obj/strtod-c.o + gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -DCLIBC_NO_LL_HEAP -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c crosslibc/strtod.c -o obj/strtod-c.o obj/printf-c.o: crosslibc/printf/printf.c - gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c crosslibc/printf/printf.c -o obj/printf-c.o + gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -DCLIBC_NO_LL_HEAP -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c crosslibc/printf/printf.c -o obj/printf-c.o obj/cppbase-cpp.o: crosslibc/cppbase.cpp - gcc -std=c++17 -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -fno-rtti -nostdinc++ -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c crosslibc/cppbase.cpp -o obj/cppbase-cpp.o + gcc -std=c++17 -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -DCLIBC_NO_LL_HEAP -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -fno-rtti -nostdinc++ -Iinclude -Iinclude/compat -Inolibc -Icrosslibc -Icrosslibc/STL -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c crosslibc/cppbase.cpp -o obj/cppbase-cpp.o efi: bin/efitest.efi diff --git a/crosslibc b/crosslibc index faea33e..9feaa3b 160000 --- a/crosslibc +++ b/crosslibc @@ -1 +1 @@ -Subproject commit faea33e55eca7346604afe6281244ba651ab2300 +Subproject commit 9feaa3b5c1ef2aa056027c465a4f997db9366425 diff --git a/prep.sh b/prep.sh index 8e1dcbb..3c5773a 100755 --- a/prep.sh +++ b/prep.sh @@ -9,7 +9,7 @@ INCLUDE_PATHS="include include/compat nolibc crosslibc crosslibc/STL /usr/incl # Config ARCH="x86_64" # Warning: Also specified in Makefile_cst COMPILER="gcc" -COMPILER_FLAGS="-DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch" +COMPILER_FLAGS="-DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -DCLIBC_NO_MEMCPY -DCLIBC_NO_MEMSET -DCLIBC_NO_LL_HEAP -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch" COMPILER_FLAGS_CXX="-fno-rtti -nostdinc++" ASSEMBLER="nasm" ASSEMBLER_FLAGS="" diff --git a/src/clibc-user-impl.c b/src/clibc-user-impl.c index c6d5923..c2590c5 100644 --- a/src/clibc-user-impl.c +++ b/src/clibc-user-impl.c @@ -16,3 +16,28 @@ void _puts(const char *str) { write(1, str, strlen(str)); _putchar('\n'); } + + +void heap_init() { + // Nothing to do +} + +void *heap_alloc(size_t *size) { + return mmap(NULL, *size, PROT_READ | PROT_WRITE, MAP_PRIVATE, -1, 0); +} + +void heap_dealloc(void *memptr, size_t size) { + munmap(memptr, size); +} + +size_t get_mem_used() { + return 0; +} + +size_t get_mem_total() { + return 0; +} + +size_t get_mem_free() { + return 0; +} diff --git a/src/main.c b/src/main.c index a930b53..73dfa86 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,7 @@ #include #include +#include #include @@ -22,6 +23,12 @@ int main(int argc, char **argv) { } printf("\n"); + puts("Testing malloc()..."); + char *mbuf = malloc(16); + strcpy(mbuf, "Hello"); + puts("Testing free()..."); + free(mbuf); + puts("Testing getline() from stdin..."); char *input_buf = NULL; size_t input_len;