diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index b5fa33c0c..6e2fc3248 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -3232,10 +3232,13 @@ aot_load_from_aot_file(const uint8 *buf, uint32 size, char *error_buf, if (!module) return NULL; + os_thread_jit_write_protect_np(false); /* Make memory writable */ if (!load(buf, size, module, error_buf, error_buf_size)) { aot_unload(module); return NULL; } + os_thread_jit_write_protect_np(true); /* Make memory executable */ + os_icache_flush(module->code, module->code_size); LOG_VERBOSE("Load module success.\n"); return module; diff --git a/core/iwasm/aot/arch/aot_reloc_aarch64.c b/core/iwasm/aot/arch/aot_reloc_aarch64.c index 4c46eaa00..b4bb6024a 100644 --- a/core/iwasm/aot/arch/aot_reloc_aarch64.c +++ b/core/iwasm/aot/arch/aot_reloc_aarch64.c @@ -53,7 +53,12 @@ get_target_symbol_map(uint32 *sym_num) return target_sym_map; } +#if (defined(__APPLE__) || defined(__MACH__)) && defined(__arm64__) +#define BUILD_TARGET_AARCH64_DEFAULT "arm64" +#else #define BUILD_TARGET_AARCH64_DEFAULT "aarch64v8" +#endif + void get_current_target(char *target_buf, uint32 target_buf_size) { diff --git a/core/shared/platform/alios/alios_platform.c b/core/shared/platform/alios/alios_platform.c index c9f5f17e6..60393ae52 100644 --- a/core/shared/platform/alios/alios_platform.c +++ b/core/shared/platform/alios/alios_platform.c @@ -69,3 +69,7 @@ os_mprotect(void *addr, size_t size, int prot) void os_dcache_flush() {} + +void +os_icache_flush(void *start, size_t len) +{} diff --git a/core/shared/platform/alios/alios_thread.c b/core/shared/platform/alios/alios_thread.c index 0efd2f394..9fe927db0 100644 --- a/core/shared/platform/alios/alios_thread.c +++ b/core/shared/platform/alios/alios_thread.c @@ -359,3 +359,7 @@ os_thread_get_stack_boundary() /* TODO: get alios stack boundary */ return NULL; } + +void +os_thread_jit_write_protect_np(bool enabled) +{} \ No newline at end of file diff --git a/core/shared/platform/common/posix/posix_memmap.c b/core/shared/platform/common/posix/posix_memmap.c index 2dfbee453..5d33dcc81 100644 --- a/core/shared/platform/common/posix/posix_memmap.c +++ b/core/shared/platform/common/posix/posix_memmap.c @@ -5,6 +5,10 @@ #include "platform_api_vmcore.h" +#if (defined(__APPLE__) || defined(__MACH__)) && defined(__arm64__) +#include +#endif + #ifndef BH_ENABLE_TRACE_MMAP #define BH_ENABLE_TRACE_MMAP 0 #endif @@ -36,7 +40,11 @@ void * os_mmap(void *hint, size_t size, int prot, int flags) { int map_prot = PROT_NONE; +#if (defined(__APPLE__) || defined(__MACH__)) && defined(__arm64__) + int map_flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_JIT; +#else int map_flags = MAP_ANONYMOUS | MAP_PRIVATE; +#endif uint64 request_size, page_size; uint8 *addr = MAP_FAILED; uint32 i; @@ -251,3 +259,11 @@ os_mprotect(void *addr, size_t size, int prot) void os_dcache_flush(void) {} + +void +os_icache_flush(void *start, size_t len) +{ +#if (defined(__APPLE__) || defined(__MACH__)) && defined(__arm64__) + sys_icache_invalidate(start, len); +#endif +} \ No newline at end of file diff --git a/core/shared/platform/common/posix/posix_thread.c b/core/shared/platform/common/posix/posix_thread.c index a7bdc6d10..498f5f5a2 100644 --- a/core/shared/platform/common/posix/posix_thread.c +++ b/core/shared/platform/common/posix/posix_thread.c @@ -418,6 +418,14 @@ os_thread_get_stack_boundary() return addr; } +void +os_thread_jit_write_protect_np(bool enabled) +{ +#if (defined(__APPLE__) || defined(__MACH__)) && defined(__arm64__) + pthread_jit_write_protect_np(enabled); +#endif +} + #ifdef OS_ENABLE_HW_BOUND_CHECK #define SIG_ALT_STACK_SIZE (32 * 1024) diff --git a/core/shared/platform/esp-idf/espidf_memmap.c b/core/shared/platform/esp-idf/espidf_memmap.c index 0a1fd4fe4..ce94a9549 100644 --- a/core/shared/platform/esp-idf/espidf_memmap.c +++ b/core/shared/platform/esp-idf/espidf_memmap.c @@ -100,6 +100,10 @@ void #endif } +void +os_icache_flush(void *start, size_t len) +{} + #if (WASM_MEM_DUAL_BUS_MIRROR != 0) void * os_get_dbus_mirror(void *ibus) diff --git a/core/shared/platform/esp-idf/espidf_platform.c b/core/shared/platform/esp-idf/espidf_platform.c index 35b893d81..bbc18f69b 100644 --- a/core/shared/platform/esp-idf/espidf_platform.c +++ b/core/shared/platform/esp-idf/espidf_platform.c @@ -53,6 +53,10 @@ os_thread_get_stack_boundary(void) #endif } +void +os_thread_jit_write_protect_np(bool enabled) +{} + int os_usleep(uint32 usec) { diff --git a/core/shared/platform/include/platform_api_vmcore.h b/core/shared/platform/include/platform_api_vmcore.h index 15fc1d387..c803c12e4 100644 --- a/core/shared/platform/include/platform_api_vmcore.h +++ b/core/shared/platform/include/platform_api_vmcore.h @@ -81,6 +81,13 @@ os_self_thread(void); uint8 * os_thread_get_stack_boundary(void); +/** + * Set whether the MAP_JIT region write protection is enabled for this thread. + * Pass true to make the region executable, false to make it writable. + */ +void +os_thread_jit_write_protect_np(bool enabled); + /** ************** mutext APIs *********** * vmcore: Not required until pthread is supported by runtime @@ -143,6 +150,12 @@ os_get_dbus_mirror(void *ibus); void os_dcache_flush(void); +/** + * Flush instruction cache. + */ +void +os_icache_flush(void *start, size_t len); + #ifdef __cplusplus } #endif diff --git a/core/shared/platform/linux-sgx/sgx_platform.c b/core/shared/platform/linux-sgx/sgx_platform.c index b40eaf79c..1a90af4c6 100644 --- a/core/shared/platform/linux-sgx/sgx_platform.c +++ b/core/shared/platform/linux-sgx/sgx_platform.c @@ -195,3 +195,7 @@ os_mprotect(void *addr, size_t size, int prot) void os_dcache_flush(void) {} + +void +os_icache_flush(void *start, size_t len) +{} \ No newline at end of file diff --git a/core/shared/platform/linux-sgx/sgx_thread.c b/core/shared/platform/linux-sgx/sgx_thread.c index 1cb2f5d09..d9412927b 100644 --- a/core/shared/platform/linux-sgx/sgx_thread.c +++ b/core/shared/platform/linux-sgx/sgx_thread.c @@ -210,3 +210,7 @@ os_thread_get_stack_boundary() /* TODO: get sgx stack boundary */ return NULL; } + +void +os_thread_jit_write_protect_np(bool enabled) +{} \ No newline at end of file diff --git a/core/shared/platform/nuttx/nuttx_platform.c b/core/shared/platform/nuttx/nuttx_platform.c index 0bd1f6808..92c17ed48 100644 --- a/core/shared/platform/nuttx/nuttx_platform.c +++ b/core/shared/platform/nuttx/nuttx_platform.c @@ -144,6 +144,10 @@ os_dcache_flush() bus_sync(); } +void +os_icache_flush(void *start, size_t len) +{} + #if (WASM_MEM_DUAL_BUS_MIRROR != 0) void * os_get_dbus_mirror(void *ibus) diff --git a/core/shared/platform/riot/riot_platform.c b/core/shared/platform/riot/riot_platform.c index a0c38e8c9..6fd6617b4 100644 --- a/core/shared/platform/riot/riot_platform.c +++ b/core/shared/platform/riot/riot_platform.c @@ -79,3 +79,7 @@ os_dcache_flush(void) irq_unlock(key); #endif } + +void +os_icache_flush(void *start, size_t len) +{} \ No newline at end of file diff --git a/core/shared/platform/riot/riot_thread.c b/core/shared/platform/riot/riot_thread.c index 0ebcf30e0..a9062bfec 100644 --- a/core/shared/platform/riot/riot_thread.c +++ b/core/shared/platform/riot/riot_thread.c @@ -430,3 +430,7 @@ os_thread_get_stack_boundary() return NULL; #endif } + +void +os_thread_jit_write_protect_np(bool enabled) +{} \ No newline at end of file diff --git a/core/shared/platform/rt-thread/rtt_platform.c b/core/shared/platform/rt-thread/rtt_platform.c index 4685e1ea3..6dc719f01 100644 --- a/core/shared/platform/rt-thread/rtt_platform.c +++ b/core/shared/platform/rt-thread/rtt_platform.c @@ -140,6 +140,10 @@ os_thread_get_stack_boundary(void) return tid->stack_addr; } +void +os_thread_jit_write_protect_np(bool enabled) +{} + int os_mutex_init(korp_mutex *mutex) { @@ -207,3 +211,7 @@ os_mprotect(void *addr, size_t size, int prot) void os_dcache_flush(void) {} + +void +os_icache_flush(void *start, size_t len) +{} \ No newline at end of file diff --git a/core/shared/platform/windows/platform_init.c b/core/shared/platform/windows/platform_init.c index db5885387..96bcf9ab1 100644 --- a/core/shared/platform/windows/platform_init.c +++ b/core/shared/platform/windows/platform_init.c @@ -73,3 +73,7 @@ os_getpagesize() void os_dcache_flush(void) {} + +void +os_icache_flush(void *start, size_t len) +{} \ No newline at end of file diff --git a/core/shared/platform/windows/win_thread.c b/core/shared/platform/windows/win_thread.c index 09cf0c63f..abc36f2fc 100644 --- a/core/shared/platform/windows/win_thread.c +++ b/core/shared/platform/windows/win_thread.c @@ -712,6 +712,10 @@ os_thread_get_stack_boundary() return thread_stack_boundary; } +void +os_thread_jit_write_protect_np(bool enabled) +{} + #ifdef OS_ENABLE_HW_BOUND_CHECK static os_thread_local_attribute bool thread_signal_inited = false; diff --git a/core/shared/platform/zephyr/zephyr_platform.c b/core/shared/platform/zephyr/zephyr_platform.c index b4f2e5ec7..715be8b94 100644 --- a/core/shared/platform/zephyr/zephyr_platform.c +++ b/core/shared/platform/zephyr/zephyr_platform.c @@ -214,6 +214,10 @@ os_dcache_flush() #endif } +void +os_icache_flush(void *start, size_t len) +{} + void set_exec_mem_alloc_func(exec_mem_alloc_func_t alloc_func, exec_mem_free_func_t free_func) diff --git a/core/shared/platform/zephyr/zephyr_thread.c b/core/shared/platform/zephyr/zephyr_thread.c index 1ee2c5cef..105d53993 100644 --- a/core/shared/platform/zephyr/zephyr_thread.c +++ b/core/shared/platform/zephyr/zephyr_thread.c @@ -574,3 +574,7 @@ os_thread_get_stack_boundary() return NULL; #endif } + +void +os_thread_jit_write_protect_np(bool enabled) +{} \ No newline at end of file