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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 188 additions and 0 deletions

View File

@ -169,4 +169,8 @@ endif ()
if (DEFINED WAMR_APP_THREAD_STACK_SIZE_MAX)
add_definitions (-DAPP_THREAD_STACK_SIZE_MAX=${WAMR_APP_THREAD_STACK_SIZE_MAX})
endif ()
if (WAMR_BUILD_CUSTOM_NAME_SECTION EQUAL 1)
add_definitions (-DWASM_ENABLE_CUSTOM_NAME_SECTION=1)
message (" Custom name section enabled")
endif ()

View File

@ -56,6 +56,10 @@ extern "C" {
#define SECTION_TYPE_DATACOUNT 12
#endif
#define SUB_SECTION_TYPE_MODULE 0
#define SUB_SECTION_TYPE_FUNC 1
#define SUB_SECTION_TYPE_LOCAL 2
#define IMPORT_KIND_FUNC 0
#define IMPORT_KIND_TABLE 1
#define IMPORT_KIND_MEMORY 2
@ -196,6 +200,9 @@ typedef struct WASMImport {
} WASMImport;
struct WASMFunction {
#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
char *field_name;
#endif
/* the type of function */
WASMType *func_type;
uint32 local_count;

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:

View File

@ -1408,6 +1408,73 @@ load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
return true;
}
#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;
bh_assert(p < p_end);
while (p < p_end) {
read_leb_uint32(p, p_end, name_type);
if (i != 0) {
bh_assert(name_type > previous_name_type);
}
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);
bh_assert(func_index > previous_func_index);
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;
bh_assert(func_index < module->function_count);
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)
@ -1421,6 +1488,13 @@ load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
bh_assert(name_len > 0
&& p + name_len <= p_end);
#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");
(void)name_len;
return true;

View File

@ -44,6 +44,10 @@ cmake -DWAMR_BUILD_PLATFORM=linux -DWAMR_BUILD_TARGET=ARM
- **WAMR_BUILD_LIBC_WASI**=1/0, default to enable if not set
#### **Configure Debug**
- **WAMR_BUILD_CUSTOM_NAME_SECTION**=1/0, load the function name from custom name section, default to disable if not set
#### **Enable Multi-Module feature**
- **WAMR_BUILD_MULTI_MODULE**=1/0, default to disable if not set