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:
liang.he 2024-01-11 13:14:54 +08:00 committed by GitHub
parent 03a2af5095
commit 9121db5671
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 32 deletions

View File

@ -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:

View File

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

View File

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

View File

@ -1,7 +1,7 @@
# Build WAMR vmcore
WAMR vmcore is a set of runtime libraries for loading and running Wasm modules. This document introduces how to build the WAMR vmcore.
WAMR vmcore is a set of runtime libraries for loading and running Wasm modules. This document introduces how to build the WAMR vmcore.
References:
- [how to build iwasm](../product-mini/README.md): building different target platforms such as Linux, Windows, Mac etc
@ -130,7 +130,7 @@ cmake -DWAMR_BUILD_PLATFORM=linux -DWAMR_BUILD_TARGET=ARM
> Note: if it is enabled, the call stack will be dumped when exception occurs.
> - For interpreter mode, the function names are firstly extracted from *custom name section*, if this section doesn't exist or the feature is not enabled, then the name will be extracted from the import/export sections
> - For AOT/JIT mode, the function names are extracted from import/export section, please export as many functions as possible (for `wasi-sdk` you can use `-Wl,--export-all`) when compiling wasm module, and add `--enable-dump-call-stack` option to wamrc during compiling AOT module.
> - For AOT/JIT mode, the function names are extracted from import/export section, please export as many functions as possible (for `wasi-sdk` you can use `-Wl,--export-all`) when compiling wasm module, and add `--enable-dump-call-stack --emit-custom-sections=name` option to wamrc during compiling AOT module.
#### **Enable memory profiling (Experiment)**
- **WAMR_BUILD_MEMORY_PROFILING**=1/0, default to disable if not set
@ -201,7 +201,7 @@ Currently we only profile the memory consumption of module, module_instance and
> Note: If `WAMR_BUILD_CUSTOM_NAME_SECTION` is enabled, then the `custom name section` will be treated as a special section and consumed by the runtime, not available to the embedder.
> For AoT file, must use `--emit-custom-sections` to specify which sections need to be emitted into AoT file, otherwise all custom sections (except custom name section) will be ignored.
> For AoT file, must use `--emit-custom-sections` to specify which sections need to be emitted into AoT file, otherwise all custom sections will be ignored.
### **Stack guard size**
- **WAMR_BUILD_STACK_GUARD_SIZE**=n, default to N/A if not set.