From 685d55d2e749ee56bdb69984a151799d8c7e960e Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 16 Jan 2024 10:17:58 +0900 Subject: [PATCH] nuttx: Use larger alignment for os_mmap and comment why (#3017) Other platforms with malloc-based os_mmap might need similar changes too, depending on their target cpu arch. --- core/shared/platform/nuttx/nuttx_platform.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/core/shared/platform/nuttx/nuttx_platform.c b/core/shared/platform/nuttx/nuttx_platform.c index 38e70076..d105924a 100644 --- a/core/shared/platform/nuttx/nuttx_platform.c +++ b/core/shared/platform/nuttx/nuttx_platform.c @@ -87,6 +87,7 @@ os_dumps_proc_mem_info(char *out, unsigned int size) void * os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file) { + void *p; #if (WASM_MEM_DUAL_BUS_MIRROR != 0) void *i_addr, *d_addr; #endif @@ -110,7 +111,21 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file) return in_ibus_ext(i_addr) ? i_addr : d_addr; } #endif - return malloc((uint32)size); + /* Note: aot_loader.c assumes that os_mmap provides large enough + * alignment for any data sections. Some sections like rodata.cst32 + * actually require alignment larger than the natural alignment + * provided by malloc. + * + * Probably it's cleaner to add an explicit alignment argument to + * os_mmap. However, it only makes sense if we change our aot format + * to keep the necessary alignment. + * + * For now, let's assume 32 byte alignment is enough. + */ + if (posix_memalign(&p, 32, size)) { + return NULL; + } + return p; } void