Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/iwasm/aot/arch/aot_reloc_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ static SymbolMap target_sym_map[] = {
REG_SYM(__divdi3),
/* clang-format on */
REG_SYM(__udivdi3),
REG_SYM(__moddi3),
REG_SYM(__umoddi3),
REG_SYM(__divsi3),
REG_SYM(__udivsi3),
Expand Down
17 changes: 17 additions & 0 deletions core/iwasm/compilation/aot_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,23 @@ check_type_compatible(uint8 src_type, uint8 dst_type)
return false; \
} \
} \
else if (comp_ctx->is_indirect_mode) { \
int32 func_index; \
if (!(func_ptr_type = LLVMPointerType(func_type, 0))) { \
aot_set_last_error("create LLVM function type failed."); \
return false; \
} \
\
func_index = aot_get_native_symbol_index(comp_ctx, #name); \
if (func_index < 0) { \
return false; \
} \
if (!(func = aot_get_func_from_table( \
comp_ctx, func_ctx->native_symbol, func_ptr_type, \
func_index))) { \
return false; \
} \
} \
else { \
char *func_name = #name; \
/* AOT mode, delcare the function */ \
Expand Down
131 changes: 119 additions & 12 deletions core/shared/platform/common/posix/posix_memmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,56 @@

#include "platform_api_vmcore.h"

#ifndef BH_ENABLE_TRACE_MMAP
#define BH_ENABLE_TRACE_MMAP 0
#endif

#if BH_ENABLE_TRACE_MMAP != 0
static size_t total_size_mmapped = 0;
static size_t total_size_munmapped = 0;
#endif

#define HUGE_PAGE_SIZE (2 * 1024 * 1024)

static inline uintptr_t
round_up(uintptr_t v, uintptr_t b)
{
uintptr_t m = b - 1;
return (v + m) & ~m;
}

static inline uintptr_t
round_down(uintptr_t v, uintptr_t b)
{
uintptr_t m = b - 1;
return v & ~m;
}

void *
os_mmap(void *hint, size_t size, int prot, int flags)
{
int map_prot = PROT_NONE;
int map_flags = MAP_ANONYMOUS | MAP_PRIVATE;
uint64 request_size, page_size;
uint8 *addr;
uint8 *addr = MAP_FAILED;
uint32 i;

page_size = (uint64)getpagesize();
request_size = (size + page_size - 1) & ~(page_size - 1);

#if !defined(__APPLE__) && !defined(__NuttX__)
/* huge page isn't supported on MacOS and NuttX */
if (request_size >= HUGE_PAGE_SIZE)
/* apply one extra huge page */
request_size += HUGE_PAGE_SIZE;
#endif

if ((size_t)request_size < size)
/* integer overflow */
return NULL;

if (request_size > 16 * (uint64)UINT32_MAX)
/* At most 16 G is allowed */
/* at most 16 G is allowed */
return NULL;

if (prot & MMAP_PROT_READ)
Expand Down Expand Up @@ -80,25 +112,93 @@ os_mmap(void *hint, size_t size, int prot, int flags)
os_munmap(addr, request_size);
}
else {
/* reset next hint address */
/* success, reset next hint address */
hint_addr += request_size;
return addr;
break;
}
}
hint_addr += BH_MB;
}
}
#endif

/* try 5 times */
for (i = 0; i < 5; i++) {
addr = mmap(hint, request_size, map_prot, map_flags, -1, 0);
if (addr != MAP_FAILED)
break;
#endif /* end of BUILD_TARGET_RISCV64_LP64D || BUILD_TARGET_RISCV64_LP64 */

/* memory has't been mapped or was mapped failed previously */
if (addr == MAP_FAILED) {
/* try 5 times */
for (i = 0; i < 5; i++) {
addr = mmap(hint, request_size, map_prot, map_flags, -1, 0);
if (addr != MAP_FAILED)
break;
}
}

if (addr == MAP_FAILED)
if (addr == MAP_FAILED) {
#if BH_ENABLE_TRACE_MMAP != 0
os_printf("mmap failed\n");
#endif
return NULL;
}

#if BH_ENABLE_TRACE_MMAP != 0
total_size_mmapped += request_size;
os_printf("mmap return: %p with size: %zu, total_size_mmapped: %zu, "
"total_size_munmapped: %zu\n",
addr, request_size, total_size_mmapped, total_size_munmapped);
#endif

#if !defined(__APPLE__) && !defined(__NuttX__)
/* huge page isn't supported on MacOS and NuttX */
if (request_size > HUGE_PAGE_SIZE) {
uintptr_t huge_start, huge_end;
size_t prefix_size = 0, suffix_size = HUGE_PAGE_SIZE;

huge_start = round_up((uintptr_t)addr, HUGE_PAGE_SIZE);

if (huge_start > (uintptr_t)addr) {
prefix_size += huge_start - (uintptr_t)addr;
suffix_size -= huge_start - (uintptr_t)addr;
}

/* unmap one extra huge page */

if (prefix_size > 0) {
munmap(addr, prefix_size);
#if BH_ENABLE_TRACE_MMAP != 0
total_size_munmapped += prefix_size;
os_printf("munmap %p with size: %zu, total_size_mmapped: %zu, "
"total_size_munmapped: %zu\n",
addr, prefix_size, total_size_mmapped,
total_size_munmapped);
#endif
}
if (suffix_size > 0) {
munmap(addr + request_size - suffix_size, suffix_size);
#if BH_ENABLE_TRACE_MMAP != 0
total_size_munmapped += suffix_size;
os_printf("munmap %p with size: %zu, total_size_mmapped: %zu, "
"total_size_munmapped: %zu\n",
addr + request_size - suffix_size, suffix_size,
total_size_mmapped, total_size_munmapped);
#endif
}

addr = (uint8 *)huge_start;
request_size -= HUGE_PAGE_SIZE;

huge_end = round_down(huge_start + request_size, HUGE_PAGE_SIZE);
if (huge_end > huge_start) {
int ret = madvise((void *)huge_start, huge_end - huge_start,
MADV_HUGEPAGE);
if (ret) {
#if BH_ENABLE_TRACE_MMAP != 0
os_printf(
"warning: madvise(%p, %lu) huge page failed, return %d\n",
(void *)huge_start, huge_end - huge_start, ret);
#endif
}
}
}
#endif /* end of __APPLE__ || __NuttX__ */

return addr;
}
Expand All @@ -113,7 +213,14 @@ os_munmap(void *addr, size_t size)
if (munmap(addr, request_size)) {
os_printf("os_munmap error addr:%p, size:0x%" PRIx64 ", errno:%d\n",
addr, request_size, errno);
return;
}
#if BH_ENABLE_TRACE_MMAP != 0
total_size_munmapped += request_size;
os_printf("munmap %p with size: %zu, total_size_mmapped: %zu, "
"total_size_munmapped: %zu\n",
addr, request_size, total_size_mmapped, total_size_munmapped);
#endif
}
}

Expand Down