Fix a bug when emit the custom name section to aot file (#2987)
The content in custom name section is changed after loaded since the strings are adjusted with '\0' appended, the emitted AOT file then cannot be loaded. The PR disables changing the content for AOT compiler to resolve it. And disable emitting custom name section for `wamrc --enable-dump-call-stack`, instead, use `wamrc --emit-custom-sections=name` to emit it.
This commit is contained in:
@ -822,7 +822,9 @@ load_custom_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
|
||||
case AOT_CUSTOM_SECTION_NAME:
|
||||
if (!load_name_section(buf, buf_end, module, is_load_from_file_buf,
|
||||
error_buf, error_buf_size))
|
||||
goto fail;
|
||||
LOG_VERBOSE("Load name section failed.");
|
||||
else
|
||||
LOG_VERBOSE("Load name section success.");
|
||||
break;
|
||||
#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
|
||||
case AOT_CUSTOM_SECTION_RAW:
|
||||
|
||||
@ -912,9 +912,6 @@ get_native_symbol_list_size(AOTCompContext *comp_ctx)
|
||||
return len;
|
||||
}
|
||||
|
||||
static uint32
|
||||
get_name_section_size(AOTCompData *comp_data);
|
||||
|
||||
static uint32
|
||||
get_custom_sections_size(AOTCompContext *comp_ctx, AOTCompData *comp_data);
|
||||
|
||||
@ -972,15 +969,6 @@ get_aot_file_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
|
||||
size += get_native_symbol_list_size(comp_ctx);
|
||||
}
|
||||
|
||||
if (comp_ctx->enable_aux_stack_frame) {
|
||||
/* custom name section */
|
||||
size = align_uint(size, 4);
|
||||
/* section id + section size + sub section id */
|
||||
size += (uint32)sizeof(uint32) * 3;
|
||||
size += (comp_data->aot_name_section_size =
|
||||
get_name_section_size(comp_data));
|
||||
}
|
||||
|
||||
size_custom_section = get_custom_sections_size(comp_ctx, comp_data);
|
||||
if (size_custom_section > 0) {
|
||||
size = align_uint(size, 4);
|
||||
@ -1333,6 +1321,21 @@ get_custom_sections_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
|
||||
const uint8 *content = NULL;
|
||||
uint32 length = 0;
|
||||
|
||||
if (strcmp(section_name, "name") == 0) {
|
||||
/* custom name section */
|
||||
comp_data->aot_name_section_size = get_name_section_size(comp_data);
|
||||
if (comp_data->aot_name_section_size == 0) {
|
||||
LOG_WARNING("Can't find custom section [name], ignore it");
|
||||
continue;
|
||||
}
|
||||
|
||||
size = align_uint(size, 4);
|
||||
/* section id + section size + sub section id */
|
||||
size += (uint32)sizeof(uint32) * 3;
|
||||
size += comp_data->aot_name_section_size;
|
||||
continue;
|
||||
}
|
||||
|
||||
content = wasm_loader_get_custom_section(comp_data->wasm_module,
|
||||
section_name, &length);
|
||||
if (!content) {
|
||||
@ -2066,23 +2069,25 @@ static bool
|
||||
aot_emit_name_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
|
||||
AOTCompData *comp_data, AOTCompContext *comp_ctx)
|
||||
{
|
||||
if (comp_ctx->enable_aux_stack_frame) {
|
||||
uint32 offset = *p_offset;
|
||||
uint32 offset = *p_offset;
|
||||
|
||||
*p_offset = offset = align_uint(offset, 4);
|
||||
if (comp_data->aot_name_section_size == 0)
|
||||
return true;
|
||||
|
||||
EMIT_U32(AOT_SECTION_TYPE_CUSTOM);
|
||||
/* sub section id + name section size */
|
||||
EMIT_U32(sizeof(uint32) * 1 + comp_data->aot_name_section_size);
|
||||
EMIT_U32(AOT_CUSTOM_SECTION_NAME);
|
||||
bh_memcpy_s((uint8 *)(buf + offset), (uint32)(buf_end - buf),
|
||||
comp_data->aot_name_section_buf,
|
||||
(uint32)comp_data->aot_name_section_size);
|
||||
offset += comp_data->aot_name_section_size;
|
||||
offset = align_uint(offset, 4);
|
||||
|
||||
*p_offset = offset;
|
||||
}
|
||||
EMIT_U32(AOT_SECTION_TYPE_CUSTOM);
|
||||
/* sub section id + name section size */
|
||||
EMIT_U32(sizeof(uint32) * 1 + comp_data->aot_name_section_size);
|
||||
EMIT_U32(AOT_CUSTOM_SECTION_NAME);
|
||||
bh_memcpy_s((uint8 *)(buf + offset), (uint32)(buf_end - buf),
|
||||
comp_data->aot_name_section_buf,
|
||||
(uint32)comp_data->aot_name_section_size);
|
||||
offset += comp_data->aot_name_section_size;
|
||||
|
||||
*p_offset = offset;
|
||||
|
||||
LOG_DEBUG("emit name section");
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -2098,6 +2103,16 @@ aot_emit_custom_sections(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
|
||||
const uint8 *content = NULL;
|
||||
uint32 length = 0;
|
||||
|
||||
if (strcmp(section_name, "name") == 0) {
|
||||
*p_offset = offset;
|
||||
if (!aot_emit_name_section(buf, buf_end, p_offset, comp_data,
|
||||
comp_ctx))
|
||||
return false;
|
||||
|
||||
offset = *p_offset;
|
||||
continue;
|
||||
}
|
||||
|
||||
content = wasm_loader_get_custom_section(comp_data->wasm_module,
|
||||
section_name, &length);
|
||||
if (!content) {
|
||||
@ -3589,6 +3604,10 @@ aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
|
||||
return NULL;
|
||||
|
||||
aot_file_size = get_aot_file_size(comp_ctx, comp_data, obj_data);
|
||||
if (aot_file_size == 0) {
|
||||
aot_set_last_error("get aot file size failed");
|
||||
goto fail1;
|
||||
}
|
||||
|
||||
if (!(buf = aot_file_buf = wasm_runtime_malloc(aot_file_size))) {
|
||||
aot_set_last_error("allocate memory failed.");
|
||||
@ -3610,7 +3629,6 @@ aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
|
||||
|| !aot_emit_relocation_section(buf, buf_end, &offset, comp_ctx,
|
||||
comp_data, obj_data)
|
||||
|| !aot_emit_native_symbol(buf, buf_end, &offset, comp_ctx)
|
||||
|| !aot_emit_name_section(buf, buf_end, &offset, comp_data, comp_ctx)
|
||||
|| !aot_emit_custom_sections(buf, buf_end, &offset, comp_data,
|
||||
comp_ctx))
|
||||
goto fail2;
|
||||
|
||||
@ -2679,8 +2679,12 @@ handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
||||
if (!(module->functions[func_index]->field_name =
|
||||
const_str_list_insert(
|
||||
p, func_name_len, module,
|
||||
is_load_from_file_buf, error_buf,
|
||||
error_buf_size))) {
|
||||
#if WASM_ENABLE_WAMR_COMPILER != 0
|
||||
false,
|
||||
#else
|
||||
is_load_from_file_buf,
|
||||
#endif
|
||||
error_buf, error_buf_size))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user