From 4c1c1875fc44008e4a8816c6b6aac1e214aba017 Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Sun, 8 Mar 2026 14:30:43 +0100 Subject: [PATCH] baremetal: provide os_mmap/os_mremap for wamr aot_load_from_aot_file --- .../shared/platform/baremetal/platform_init.c | 55 ++++++++++++++++++- .../platform/baremetal/platform_internal.h | 5 +- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/core/shared/platform/baremetal/platform_init.c b/core/shared/platform/baremetal/platform_init.c index 996f6dcb..c14cd93e 100644 --- a/core/shared/platform/baremetal/platform_init.c +++ b/core/shared/platform/baremetal/platform_init.c @@ -92,16 +92,67 @@ bh_log_proc_mem(const char *function, uint32 line) * mman ****************************************************/ +// NOTE: We need os_mmap for aot_load_from_aot_file +// +// Stacktrace: +// #5 0x0805e5c1 in os_mmap +// #6 0x080524d3 in loader_mmap +// #7 0x08059294 in create_sections +// #8 0x08059553 in load +// #9 0x0805966b in aot_load_from_aot_file +// #10 0x08048c34 in wasm_runtime_load_ex +// #11 0x08048cff in wasm_runtime_load +// #12 0x08048112 in main + +#define MMAP_SPACE_SIZE (2 * 1024 * 1024) +static uint8_t mmap_space[MMAP_SPACE_SIZE]; +static size_t mmap_offset = 0; // Free space begins here + +static size_t +align_up(size_t x, size_t a) +{ + // a is a power of two, e.g., 8: + // x+a-1: Shift value upwards to the next alignment "bucket" + // a-1: 00000111, ~(a-1): 11111000 - Clears the lower bits/alignment + // remainder + return (x + a - 1) & ~(a - 1); +} + void * os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file) { - return NULL; + // Free space begins here (aligned) + size_t start = align_up(mmap_offset, PAGE_SIZE); + + // How much we're mapping + size_T end = start + align_up(size, PAGE_SIZE); + + if (end > MMAP_SPACE_SIZE) { + return NULL; + } + + void *ptr = &mmap_space[start]; + mmap_offset = end; + + return ptr; } void * os_mremap(void *old_addr, size_t old_size, size_t new_size) { - return NULL; + // Map the new size + void *new_addr = os_mmap(NULL, new_size, 0, 0, -1); + if (!new_addr) { + return NULL; + } + + // Copy the old memory to the new mapped address + if (old_addr) { + size_t copy_size = old_size < new_size ? old_size : new_size; + memcpy(new_addr, old_addr, copy_size); + } + + return new_addr; } void diff --git a/core/shared/platform/baremetal/platform_internal.h b/core/shared/platform/baremetal/platform_internal.h index b0ac7f6e..615d79f7 100644 --- a/core/shared/platform/baremetal/platform_internal.h +++ b/core/shared/platform/baremetal/platform_internal.h @@ -74,11 +74,12 @@ os_get_invalid_handle(void) return -1; } +#define PAGE_SIZE 4096 + static inline int os_getpagesize() { - // TODO: What pagesize? 65536? - return 4096; + return PAGE_SIZE; } // void *