From b6adec373ea6d9b43231c27cd13bb2e99415de36 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Sat, 17 Feb 2024 13:44:33 +0800 Subject: [PATCH] shared-platform: Remove dependency on shared-utils' bh_memory_remap_slow (#3153) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As an original design rule, the code in `core/shared/platform` should not rely on the code in `core/share/utils`. In the current implementation, platform layer calls function `bh_memory_remap_slow` in utils layer. This PR adds inline function `os_mremap_slow` in platform_api_vmcore.h, and lets os_remap call it if mremap fails. And remove bh_memutils.h/c as as they are unused. And resolve the compilation warning in wamrc: ```bash core/shared/platform/common/posix/posix_memmap.c:255:16: warning: implicit declaration of function ‘bh_memory_remap_slow’ 255 | return bh_memory_remap_slow(old_addr, old_size, new_size); ``` --- core/shared/platform/common/memory/mremap.c | 4 +-- .../common/posix/platform_api_posix.cmake | 1 + .../platform/common/posix/posix_memmap.c | 8 +---- .../platform/include/platform_api_vmcore.h | 18 ++++++++++ core/shared/utils/bh_memutils.c | 24 ------------- core/shared/utils/bh_memutils.h | 35 ------------------- product-mini/platforms/nuttx/wamr.mk | 1 - 7 files changed, 22 insertions(+), 69 deletions(-) delete mode 100644 core/shared/utils/bh_memutils.c delete mode 100644 core/shared/utils/bh_memutils.h diff --git a/core/shared/platform/common/memory/mremap.c b/core/shared/platform/common/memory/mremap.c index bbd287e77..dca10a342 100644 --- a/core/shared/platform/common/memory/mremap.c +++ b/core/shared/platform/common/memory/mremap.c @@ -3,10 +3,10 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ -#include "bh_memutils.h" +#include "platform_api_vmcore.h" void * os_mremap(void *old_addr, size_t old_size, size_t new_size) { - return bh_memory_remap_slow(old_addr, old_size, new_size); + return os_mremap_slow(old_addr, old_size, new_size); } diff --git a/core/shared/platform/common/posix/platform_api_posix.cmake b/core/shared/platform/common/posix/platform_api_posix.cmake index 17ee04f82..15d6daf3f 100644 --- a/core/shared/platform/common/posix/platform_api_posix.cmake +++ b/core/shared/platform/common/posix/platform_api_posix.cmake @@ -25,6 +25,7 @@ list (REMOVE_AT CMAKE_REQUIRED_DEFINITIONS 0) if(MREMAP_EXISTS) add_definitions (-DWASM_HAVE_MREMAP=1) + add_definitions (-D_GNU_SOURCE) else() add_definitions (-DWASM_HAVE_MREMAP=0) include (${CMAKE_CURRENT_LIST_DIR}/../memory/platform_api_memory.cmake) diff --git a/core/shared/platform/common/posix/posix_memmap.c b/core/shared/platform/common/posix/posix_memmap.c index afc549cdd..72c8d70e6 100644 --- a/core/shared/platform/common/posix/posix_memmap.c +++ b/core/shared/platform/common/posix/posix_memmap.c @@ -3,12 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ -#if !defined(_GNU_SOURCE) && WASM_HAVE_MREMAP != 0 -/* Enable mremap */ -#define _GNU_SOURCE -#include "bh_memutils.h" -#endif - #include "platform_api_vmcore.h" #if defined(__APPLE__) || defined(__MACH__) @@ -252,7 +246,7 @@ os_mremap(void *old_addr, size_t old_size, size_t new_size) #if BH_ENABLE_TRACE_MMAP != 0 os_printf("mremap failed: %d\n", errno); #endif - return bh_memory_remap_slow(old_addr, old_size, new_size); + return os_mremap_slow(old_addr, old_size, new_size); } return ptr; diff --git a/core/shared/platform/include/platform_api_vmcore.h b/core/shared/platform/include/platform_api_vmcore.h index 1c948f58c..1fa524f9e 100644 --- a/core/shared/platform/include/platform_api_vmcore.h +++ b/core/shared/platform/include/platform_api_vmcore.h @@ -142,6 +142,24 @@ os_munmap(void *addr, size_t size); int os_mprotect(void *addr, size_t size, int prot); +static inline void * +os_mremap_slow(void *old_addr, size_t old_size, size_t new_size) +{ + void *new_memory = os_mmap(NULL, new_size, MMAP_PROT_WRITE | MMAP_PROT_READ, + 0, os_get_invalid_handle()); + if (!new_memory) { + return NULL; + } + /* + * bh_memcpy_s can't be used as it doesn't support values bigger than + * UINT32_MAX + */ + memcpy(new_memory, old_addr, new_size < old_size ? new_size : old_size); + os_munmap(old_addr, old_size); + + return new_memory; +} + /* Doesn't guarantee that protection flags will be preserved. os_mprotect() must be called after remapping. */ void * diff --git a/core/shared/utils/bh_memutils.c b/core/shared/utils/bh_memutils.c deleted file mode 100644 index a655d8ac2..000000000 --- a/core/shared/utils/bh_memutils.c +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) 2024 Amazon Inc. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "bh_memutils.h" - -void * -bh_memory_remap_slow(void *old_addr, size_t old_size, size_t new_size) -{ - void *new_memory = - os_mmap(NULL, new_size, MMAP_PROT_WRITE | MMAP_PROT_READ, 0, -1); - if (!new_memory) { - return NULL; - } - /* - * bh_memcpy_s can't be used as it doesn't support values bigger than - * UINT32_MAX - */ - memcpy(new_memory, old_addr, new_size < old_size ? new_size : old_size); - os_munmap(old_addr, old_size); - - return new_memory; -} diff --git a/core/shared/utils/bh_memutils.h b/core/shared/utils/bh_memutils.h deleted file mode 100644 index 7581860bd..000000000 --- a/core/shared/utils/bh_memutils.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2024 Amazon Inc. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#ifndef _BH_MEMUTILS_H -#define _BH_MEMUTILS_H - -#include "bh_platform.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * Remaps memory by mapping a new region, copying data from the old - * region and umapping the old region. - * - * Unless the behavior is desired, in most cases os_mremap should be used - * as it's at worst equally slow as this function, and on some platforms - * (e.g. posix with mremap) os_mremap will perform better. - * - * @param old_addr an old address. - * @param old_size a size of the old address. - * @param new_size a size of the new memory region. - * @return a pointer to the new memory region. - */ -void * -bh_memory_remap_slow(void *old_addr, size_t old_size, size_t new_size); - -#ifdef __cplusplus -} -#endif - -#endif /* end of _BH_MEMUTILS_H */ diff --git a/product-mini/platforms/nuttx/wamr.mk b/product-mini/platforms/nuttx/wamr.mk index e329601a2..e414a7cda 100644 --- a/product-mini/platforms/nuttx/wamr.mk +++ b/product-mini/platforms/nuttx/wamr.mk @@ -437,7 +437,6 @@ CSRCS += nuttx_platform.c \ bh_hashmap.c \ bh_list.c \ bh_log.c \ - bh_memutils.c \ bh_queue.c \ bh_vector.c \ bh_read_file.c \