Skip to content

Commit

Permalink
Merge pull request #931 from bytecodealliance/main
Browse files Browse the repository at this point in the history
Merge bytecodealliance:main into wenyongh:main
  • Loading branch information
wenyongh authored Jun 23, 2024
2 parents 549edba + 4c2af25 commit d25cecc
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 44 deletions.
12 changes: 12 additions & 0 deletions core/iwasm/common/wasm_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,12 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
}
#endif /* end of WASM_MEM_ALLOC_WITH_USAGE */

/*
* AOT compiler assumes at least 8 byte alignment.
* see aot_check_memory_overflow.
*/
bh_assert(((uintptr_t)memory->memory_data & 0x7) == 0);

memory->num_bytes_per_page = num_bytes_per_page;
memory->cur_page_count = total_page_count;
memory->max_page_count = max_page_count;
Expand Down Expand Up @@ -1032,5 +1038,11 @@ wasm_allocate_linear_memory(uint8 **data, bool is_shared_memory,
#endif
}

/*
* AOT compiler assumes at least 8 byte alignment.
* see aot_check_memory_overflow.
*/
bh_assert(((uintptr_t)*data & 0x7) == 0);

return BHT_OK;
}
4 changes: 2 additions & 2 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -4010,7 +4010,7 @@ wasm_runtime_get_import_type(WASMModuleCommon *const module, int32 import_index,
import_type->module_name = aot_import_table->module_name;
import_type->name = aot_import_table->table_name;
import_type->kind = WASM_IMPORT_EXPORT_KIND_TABLE;
import_type->linked = false;
import_type->linked = false; /* not supported */
import_type->u.table_type =
(WASMTableType *)&aot_import_table->table_type;
return;
Expand All @@ -4023,7 +4023,7 @@ wasm_runtime_get_import_type(WASMModuleCommon *const module, int32 import_index,
import_type->module_name = aot_import_memory->module_name;
import_type->name = aot_import_memory->memory_name;
import_type->kind = WASM_IMPORT_EXPORT_KIND_MEMORY;
import_type->linked = false;
import_type->linked = false; /* not supported */
import_type->u.memory_type =
(WASMMemoryType *)&aot_import_memory->mem_type;
return;
Expand Down
62 changes: 47 additions & 15 deletions core/iwasm/compilation/aot_emit_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ get_memory_curr_page_count(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx);

LLVMValueRef
aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
mem_offset_t offset, uint32 bytes, bool enable_segue)
mem_offset_t offset, uint32 bytes, bool enable_segue,
unsigned int *alignp)
{
LLVMValueRef offset_const =
MEMORY64_COND_VALUE(I64_CONST(offset), I32_CONST(offset));
Expand Down Expand Up @@ -180,6 +181,26 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
comp_ctx->comp_data->memories[0].init_page_count;
uint64 mem_data_size = (uint64)num_bytes_per_page * init_page_count;

if (alignp != NULL) {
/*
* A note about max_align below:
* the assumption here is the base address of a linear memory
* has the natural alignment. for platforms using mmap, it can
* be even larger. for now, use a conservative value.
*/
const int max_align = 8;
int shift = ffs((int)(unsigned int)mem_offset);
if (shift == 0) {
*alignp = max_align;
}
else {
unsigned int align = 1 << (shift - 1);
if (align > max_align) {
align = max_align;
}
*alignp = align;
}
}
if (mem_offset + bytes <= mem_data_size) {
/* inside memory space */
if (comp_ctx->pointer_size == sizeof(uint64))
Expand All @@ -205,6 +226,9 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
return maddr;
}
}
else if (alignp != NULL) {
*alignp = 1;
}

if (is_target_64bit) {
if (!(offset_const = LLVMBuildZExt(comp_ctx->builder, offset_const,
Expand Down Expand Up @@ -324,7 +348,7 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
aot_set_last_error("llvm build load failed."); \
goto fail; \
} \
LLVMSetAlignment(value, 1); \
LLVMSetAlignment(value, known_align); \
} while (0)

#define BUILD_TRUNC(value, data_type) \
Expand All @@ -343,7 +367,7 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
aot_set_last_error("llvm build store failed."); \
goto fail; \
} \
LLVMSetAlignment(res, 1); \
LLVMSetAlignment(res, known_align); \
} while (0)

#define BUILD_SIGN_EXT(dst_type) \
Expand Down Expand Up @@ -445,8 +469,9 @@ aot_compile_op_i32_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMTypeRef data_type;
bool enable_segue = comp_ctx->enable_segue_i32_load;

unsigned int known_align;
if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
enable_segue)))
enable_segue, &known_align)))
return false;

switch (bytes) {
Expand Down Expand Up @@ -515,8 +540,9 @@ aot_compile_op_i64_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMTypeRef data_type;
bool enable_segue = comp_ctx->enable_segue_i64_load;

unsigned int known_align;
if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
enable_segue)))
enable_segue, &known_align)))
return false;

switch (bytes) {
Expand Down Expand Up @@ -591,8 +617,9 @@ aot_compile_op_f32_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMValueRef maddr, value;
bool enable_segue = comp_ctx->enable_segue_f32_load;

unsigned int known_align;
if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 4,
enable_segue)))
enable_segue, &known_align)))
return false;

if (!enable_segue)
Expand All @@ -614,8 +641,9 @@ aot_compile_op_f64_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMValueRef maddr, value;
bool enable_segue = comp_ctx->enable_segue_f64_load;

unsigned int known_align;
if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 8,
enable_segue)))
enable_segue, &known_align)))
return false;

if (!enable_segue)
Expand All @@ -640,8 +668,9 @@ aot_compile_op_i32_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,

POP_I32(value);

unsigned int known_align;
if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
enable_segue)))
enable_segue, &known_align)))
return false;

switch (bytes) {
Expand Down Expand Up @@ -691,8 +720,9 @@ aot_compile_op_i64_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,

POP_I64(value);

unsigned int known_align;
if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
enable_segue)))
enable_segue, &known_align)))
return false;

switch (bytes) {
Expand Down Expand Up @@ -748,8 +778,9 @@ aot_compile_op_f32_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,

POP_F32(value);

unsigned int known_align;
if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 4,
enable_segue)))
enable_segue, &known_align)))
return false;

if (!enable_segue)
Expand All @@ -771,8 +802,9 @@ aot_compile_op_f64_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,

POP_F64(value);

unsigned int known_align;
if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 8,
enable_segue)))
enable_segue, &known_align)))
return false;

if (!enable_segue)
Expand Down Expand Up @@ -1302,7 +1334,7 @@ aot_compile_op_atomic_rmw(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
POP_I64(value);

if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
enable_segue)))
enable_segue, NULL)))
return false;

if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
Expand Down Expand Up @@ -1392,7 +1424,7 @@ aot_compile_op_atomic_cmpxchg(AOTCompContext *comp_ctx,
}

if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
enable_segue)))
enable_segue, NULL)))
return false;

if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
Expand Down Expand Up @@ -1505,7 +1537,7 @@ aot_compile_op_atomic_wait(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
CHECK_LLVM_CONST(is_wait64);

if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
false)))
false, NULL)))
return false;

if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
Expand Down Expand Up @@ -1579,7 +1611,7 @@ aot_compiler_op_atomic_notify(AOTCompContext *comp_ctx,
POP_I32(count);

if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes,
false)))
false, NULL)))
return false;

if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
Expand Down
3 changes: 2 additions & 1 deletion core/iwasm/compilation/aot_emit_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ aot_compile_op_f64_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,

LLVMValueRef
aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
mem_offset_t offset, uint32 bytes, bool enable_segue);
mem_offset_t offset, uint32 bytes, bool enable_segue,
unsigned int *alignp);

bool
aot_compile_op_memory_size(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx);
Expand Down
4 changes: 2 additions & 2 deletions core/iwasm/compilation/simd/simd_load_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ simd_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, uint32 align,
LLVMValueRef maddr, data;

if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset,
data_length, enable_segue))) {
data_length, enable_segue, NULL))) {
HANDLE_FAILURE("aot_check_memory_overflow");
return NULL;
}
Expand Down Expand Up @@ -287,7 +287,7 @@ simd_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, uint32 align,
LLVMValueRef maddr, result;

if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset,
data_length, enable_segue)))
data_length, enable_segue, NULL)))
return false;

if (!(maddr = LLVMBuildBitCast(comp_ctx->builder, maddr, value_ptr_type,
Expand Down
28 changes: 14 additions & 14 deletions core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ wasm_runtime_get_wasi_exit_code(wasm_module_inst_t module_inst);
* @return the function instance found, NULL if not found
*/
WASM_RUNTIME_API_EXTERN wasm_function_inst_t
wasm_runtime_lookup_function(wasm_module_inst_t const module_inst,
wasm_runtime_lookup_function(const wasm_module_inst_t module_inst,
const char *name);

/**
Expand All @@ -724,8 +724,8 @@ wasm_runtime_lookup_function(wasm_module_inst_t const module_inst,
* @return the parameter count of the function instance
*/
WASM_RUNTIME_API_EXTERN uint32_t
wasm_func_get_param_count(wasm_function_inst_t const func_inst,
wasm_module_inst_t const module_inst);
wasm_func_get_param_count(const wasm_function_inst_t func_inst,
const wasm_module_inst_t module_inst);

/**
* Get result count of the function instance
Expand All @@ -736,8 +736,8 @@ wasm_func_get_param_count(wasm_function_inst_t const func_inst,
* @return the result count of the function instance
*/
WASM_RUNTIME_API_EXTERN uint32_t
wasm_func_get_result_count(wasm_function_inst_t const func_inst,
wasm_module_inst_t const module_inst);
wasm_func_get_result_count(const wasm_function_inst_t func_inst,
const wasm_module_inst_t module_inst);

/**
* Get parameter types of the function instance
Expand All @@ -747,8 +747,8 @@ wasm_func_get_result_count(wasm_function_inst_t const func_inst,
* @param param_types the parameter types returned
*/
WASM_RUNTIME_API_EXTERN void
wasm_func_get_param_types(wasm_function_inst_t const func_inst,
wasm_module_inst_t const module_inst,
wasm_func_get_param_types(const wasm_function_inst_t func_inst,
const wasm_module_inst_t module_inst,
wasm_valkind_t *param_types);

/**
Expand All @@ -759,8 +759,8 @@ wasm_func_get_param_types(wasm_function_inst_t const func_inst,
* @param result_types the result types returned
*/
WASM_RUNTIME_API_EXTERN void
wasm_func_get_result_types(wasm_function_inst_t const func_inst,
wasm_module_inst_t const module_inst,
wasm_func_get_result_types(const wasm_function_inst_t func_inst,
const wasm_module_inst_t module_inst,
wasm_valkind_t *result_types);

/**
Expand Down Expand Up @@ -1314,7 +1314,7 @@ wasm_runtime_get_export_type(const wasm_module_t module, int32_t export_index,
* @return the number of parameters for the function type
*/
WASM_RUNTIME_API_EXTERN uint32_t
wasm_func_type_get_param_count(wasm_func_type_t const func_type);
wasm_func_type_get_param_count(const wasm_func_type_t func_type);

/**
* Get the kind of a parameter for a function type
Expand All @@ -1325,7 +1325,7 @@ wasm_func_type_get_param_count(wasm_func_type_t const func_type);
* @return the kind of the parameter if successful, -1 otherwise
*/
WASM_RUNTIME_API_EXTERN wasm_valkind_t
wasm_func_type_get_param_valkind(wasm_func_type_t const func_type,
wasm_func_type_get_param_valkind(const wasm_func_type_t func_type,
uint32_t param_index);

/**
Expand All @@ -1336,7 +1336,7 @@ wasm_func_type_get_param_valkind(wasm_func_type_t const func_type,
* @return the number of results for the function type
*/
WASM_RUNTIME_API_EXTERN uint32_t
wasm_func_type_get_result_count(wasm_func_type_t const func_type);
wasm_func_type_get_result_count(const wasm_func_type_t func_type);

/**
* Get the kind of a result for a function type
Expand All @@ -1347,7 +1347,7 @@ wasm_func_type_get_result_count(wasm_func_type_t const func_type);
* @return the kind of the result if successful, -1 otherwise
*/
WASM_RUNTIME_API_EXTERN wasm_valkind_t
wasm_func_type_get_result_valkind(wasm_func_type_t const func_type,
wasm_func_type_get_result_valkind(const wasm_func_type_t func_type,
uint32_t result_index);

/**
Expand Down Expand Up @@ -1817,7 +1817,7 @@ wasm_runtime_dump_pgo_prof_data_to_buf(wasm_module_inst_t module_inst,
* and name string) if found, NULL otherwise
*/
WASM_RUNTIME_API_EXTERN const uint8_t *
wasm_runtime_get_custom_section(wasm_module_t const module_comm,
wasm_runtime_get_custom_section(const wasm_module_t module_comm,
const char *name, uint32_t *len);

/**
Expand Down
10 changes: 8 additions & 2 deletions core/shared/platform/alios/alios_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ 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)
{
if ((uint64)size >= UINT32_MAX)
void *addr;

if (size >= UINT32_MAX)
return NULL;
return BH_MALLOC((uint32)size);

if ((addr = BH_MALLOC((uint32)size)))
memset(addr, 0, (uint32)size);

return addr;
}

void
Expand Down
3 changes: 3 additions & 0 deletions core/shared/platform/esp-idf/espidf_memmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
uintptr_t *addr_field = buf_fixed - sizeof(uintptr_t);
*addr_field = (uintptr_t)buf_origin;
#if (WASM_MEM_DUAL_BUS_MIRROR != 0)
memset(buf_fixed + MEM_DUAL_BUS_OFFSET, 0, size);
return buf_fixed + MEM_DUAL_BUS_OFFSET;
#else
memset(buf_fixed, 0, size);
return buf_fixed;
#endif
}
Expand All @@ -71,6 +73,7 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
uintptr_t *addr_field = buf_fixed - sizeof(uintptr_t);
*addr_field = (uintptr_t)buf_origin;

memset(buf_fixed, 0, size);
return buf_fixed;
}
}
Expand Down
Loading

0 comments on commit d25cecc

Please sign in to comment.