Add the support for custom name section (#398)

Add the function name field for internal function struct

Signed-off-by: Zhongmin Wu <vwzm@live.com>
Signed-off-by: Xiaokang Qin <xiaokang.qxk@antgroup.com>

Co-authored-by: Zhongmin Wu <vwzm@live.com>
This commit is contained in:
Xiaokang Qin
2020-09-23 16:12:09 +08:00
committed by GitHub
parent a290aaf93e
commit a7e7711f63
5 changed files with 188 additions and 0 deletions

View File

@ -2392,6 +2392,99 @@ fail:
return false;
}
#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
static bool
handle_name_section(const uint8 *buf, const uint8 *buf_end,Ø
WASMModule *module,
char *error_buf, uint32 error_buf_size)
{
const uint8 *p = buf, *p_end = buf_end;
uint32 name_type, subsection_size;
uint32 previous_name_type = 0;
uint32 num_func_name;
uint32 func_index;
uint32 previous_func_index = ~0U;
uint32 func_name_len;
uint32 name_index;
int i = 0;
if (p >= p_end) {
set_error_buf(error_buf, error_buf_size, "unexpected end");
return false;
}
while (p < p_end) {
read_leb_uint32(p, p_end, name_type);
if (i != 0) {
if (name_type == previous_name_type) {
set_error_buf(error_buf, error_buf_size,
"duplicate sub-section");
return false;
}
if (name_type < previous_name_type) {
set_error_buf(error_buf, error_buf_size,
"out-of-order sub-section");
return false;
}
}
previous_name_type = name_type;
read_leb_uint32(p, p_end, subsection_size);
CHECK_BUF(p, p_end, subsection_size);
switch (name_type) {
case SUB_SECTION_TYPE_FUNC:
if (subsection_size) {
read_leb_uint32(p, p_end, num_func_name);
for (name_index = 0; name_index < num_func_name;
name_index++) {
read_leb_uint32(p, p_end, func_index);
if (func_index == previous_func_index) {
set_error_buf(error_buf, error_buf_size,
"duplicate function name");
return false;
}
if (func_index < previous_func_index
&& previous_func_index != ~0U) {
set_error_buf(error_buf, error_buf_size,
"out-of-order function index ");
return false;
}
previous_func_index = func_index;
read_leb_uint32(p, p_end, func_name_len);
CHECK_BUF(p, p_end, func_name_len);
// Skip the import functions
if (func_index >= module->import_count) {
func_index -= module->import_count;
if (func_index >= module->function_count) {
set_error_buf(error_buf, error_buf_size,
"out-of-range function index");
return false;
}
if (!(module->functions[func_index]->field_name =
const_str_list_insert(p, func_name_len,
module, error_buf,
error_buf_size))) {
return false;
}
}
p += func_name_len;
}
}
break;
case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection */
case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
default:
p = p + subsection_size;
break;
}
i++;
}
return true;
fail:
return false;
}
#endif
static bool
load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
char *error_buf, uint32 error_buf_size)
@ -2418,6 +2511,12 @@ load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
return false;
}
#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
if (memcmp(p, "name", 4) == 0) {
p += name_len;
handle_name_section(p, p_end, module, error_buf, error_buf_size);
}
#endif
LOG_VERBOSE("Load custom section success.\n");
return true;
fail: