Skip to content

Commit 9121db5

Browse files
authored
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.
1 parent 03a2af5 commit 9121db5

File tree

4 files changed

+56
-32
lines changed

4 files changed

+56
-32
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,9 @@ load_custom_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
822822
case AOT_CUSTOM_SECTION_NAME:
823823
if (!load_name_section(buf, buf_end, module, is_load_from_file_buf,
824824
error_buf, error_buf_size))
825-
goto fail;
825+
LOG_VERBOSE("Load name section failed.");
826+
else
827+
LOG_VERBOSE("Load name section success.");
826828
break;
827829
#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
828830
case AOT_CUSTOM_SECTION_RAW:

core/iwasm/compilation/aot_emit_aot_file.c

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -912,9 +912,6 @@ get_native_symbol_list_size(AOTCompContext *comp_ctx)
912912
return len;
913913
}
914914

915-
static uint32
916-
get_name_section_size(AOTCompData *comp_data);
917-
918915
static uint32
919916
get_custom_sections_size(AOTCompContext *comp_ctx, AOTCompData *comp_data);
920917

@@ -972,15 +969,6 @@ get_aot_file_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
972969
size += get_native_symbol_list_size(comp_ctx);
973970
}
974971

975-
if (comp_ctx->enable_aux_stack_frame) {
976-
/* custom name section */
977-
size = align_uint(size, 4);
978-
/* section id + section size + sub section id */
979-
size += (uint32)sizeof(uint32) * 3;
980-
size += (comp_data->aot_name_section_size =
981-
get_name_section_size(comp_data));
982-
}
983-
984972
size_custom_section = get_custom_sections_size(comp_ctx, comp_data);
985973
if (size_custom_section > 0) {
986974
size = align_uint(size, 4);
@@ -1333,6 +1321,21 @@ get_custom_sections_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
13331321
const uint8 *content = NULL;
13341322
uint32 length = 0;
13351323

1324+
if (strcmp(section_name, "name") == 0) {
1325+
/* custom name section */
1326+
comp_data->aot_name_section_size = get_name_section_size(comp_data);
1327+
if (comp_data->aot_name_section_size == 0) {
1328+
LOG_WARNING("Can't find custom section [name], ignore it");
1329+
continue;
1330+
}
1331+
1332+
size = align_uint(size, 4);
1333+
/* section id + section size + sub section id */
1334+
size += (uint32)sizeof(uint32) * 3;
1335+
size += comp_data->aot_name_section_size;
1336+
continue;
1337+
}
1338+
13361339
content = wasm_loader_get_custom_section(comp_data->wasm_module,
13371340
section_name, &length);
13381341
if (!content) {
@@ -2066,23 +2069,25 @@ static bool
20662069
aot_emit_name_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
20672070
AOTCompData *comp_data, AOTCompContext *comp_ctx)
20682071
{
2069-
if (comp_ctx->enable_aux_stack_frame) {
2070-
uint32 offset = *p_offset;
2072+
uint32 offset = *p_offset;
20712073

2072-
*p_offset = offset = align_uint(offset, 4);
2074+
if (comp_data->aot_name_section_size == 0)
2075+
return true;
20732076

2074-
EMIT_U32(AOT_SECTION_TYPE_CUSTOM);
2075-
/* sub section id + name section size */
2076-
EMIT_U32(sizeof(uint32) * 1 + comp_data->aot_name_section_size);
2077-
EMIT_U32(AOT_CUSTOM_SECTION_NAME);
2078-
bh_memcpy_s((uint8 *)(buf + offset), (uint32)(buf_end - buf),
2079-
comp_data->aot_name_section_buf,
2080-
(uint32)comp_data->aot_name_section_size);
2081-
offset += comp_data->aot_name_section_size;
2077+
offset = align_uint(offset, 4);
20822078

2083-
*p_offset = offset;
2084-
}
2079+
EMIT_U32(AOT_SECTION_TYPE_CUSTOM);
2080+
/* sub section id + name section size */
2081+
EMIT_U32(sizeof(uint32) * 1 + comp_data->aot_name_section_size);
2082+
EMIT_U32(AOT_CUSTOM_SECTION_NAME);
2083+
bh_memcpy_s((uint8 *)(buf + offset), (uint32)(buf_end - buf),
2084+
comp_data->aot_name_section_buf,
2085+
(uint32)comp_data->aot_name_section_size);
2086+
offset += comp_data->aot_name_section_size;
20852087

2088+
*p_offset = offset;
2089+
2090+
LOG_DEBUG("emit name section");
20862091
return true;
20872092
}
20882093

@@ -2098,6 +2103,16 @@ aot_emit_custom_sections(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
20982103
const uint8 *content = NULL;
20992104
uint32 length = 0;
21002105

2106+
if (strcmp(section_name, "name") == 0) {
2107+
*p_offset = offset;
2108+
if (!aot_emit_name_section(buf, buf_end, p_offset, comp_data,
2109+
comp_ctx))
2110+
return false;
2111+
2112+
offset = *p_offset;
2113+
continue;
2114+
}
2115+
21012116
content = wasm_loader_get_custom_section(comp_data->wasm_module,
21022117
section_name, &length);
21032118
if (!content) {
@@ -3589,6 +3604,10 @@ aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
35893604
return NULL;
35903605

35913606
aot_file_size = get_aot_file_size(comp_ctx, comp_data, obj_data);
3607+
if (aot_file_size == 0) {
3608+
aot_set_last_error("get aot file size failed");
3609+
goto fail1;
3610+
}
35923611

35933612
if (!(buf = aot_file_buf = wasm_runtime_malloc(aot_file_size))) {
35943613
aot_set_last_error("allocate memory failed.");
@@ -3610,7 +3629,6 @@ aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
36103629
|| !aot_emit_relocation_section(buf, buf_end, &offset, comp_ctx,
36113630
comp_data, obj_data)
36123631
|| !aot_emit_native_symbol(buf, buf_end, &offset, comp_ctx)
3613-
|| !aot_emit_name_section(buf, buf_end, &offset, comp_data, comp_ctx)
36143632
|| !aot_emit_custom_sections(buf, buf_end, &offset, comp_data,
36153633
comp_ctx))
36163634
goto fail2;

core/iwasm/interpreter/wasm_loader.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2679,8 +2679,12 @@ handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
26792679
if (!(module->functions[func_index]->field_name =
26802680
const_str_list_insert(
26812681
p, func_name_len, module,
2682-
is_load_from_file_buf, error_buf,
2683-
error_buf_size))) {
2682+
#if WASM_ENABLE_WAMR_COMPILER != 0
2683+
false,
2684+
#else
2685+
is_load_from_file_buf,
2686+
#endif
2687+
error_buf, error_buf_size))) {
26842688
return false;
26852689
}
26862690
}

doc/build_wamr.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# Build WAMR vmcore
33

4-
WAMR vmcore is a set of runtime libraries for loading and running Wasm modules. This document introduces how to build the WAMR vmcore.
4+
WAMR vmcore is a set of runtime libraries for loading and running Wasm modules. This document introduces how to build the WAMR vmcore.
55

66
References:
77
- [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
130130
> Note: if it is enabled, the call stack will be dumped when exception occurs.
131131
132132
> - 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
133-
> - 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.
133+
> - 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.
134134
135135
#### **Enable memory profiling (Experiment)**
136136
- **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
201201
202202
> 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.
203203
204-
> 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.
204+
> 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.
205205
206206
### **Stack guard size**
207207
- **WAMR_BUILD_STACK_GUARD_SIZE**=n, default to N/A if not set.

0 commit comments

Comments
 (0)