Compare commits

...

3 Commits

Author SHA1 Message Date
YAMAMOTO Takashi
b763451c99 wasm_memory.c: assert 8 byte alignement of linear memories 2024-06-21 19:01:09 +09:00
YAMAMOTO Takashi
1c9d5a523f aot_check_memory_overflow: bump the assumed alignment to 8 2024-06-21 19:00:46 +09:00
YAMAMOTO Takashi
5757905698 aot_check_memory_overflow: provide the alignment for more cases 2024-06-21 15:56:02 +09:00
2 changed files with 33 additions and 21 deletions

View File

@ -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;
@ -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;
}

View File

@ -181,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))
@ -203,30 +223,10 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
goto fail;
}
}
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 = 4;
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;
}
}
return maddr;
}
}
if (alignp != NULL) {
else if (alignp != NULL) {
*alignp = 1;
}