Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom name section to aot file #794

Merged
merged 13 commits into from
Oct 26, 2021
Merged

Add custom name section to aot file #794

merged 13 commits into from
Oct 26, 2021

Conversation

JavanZhu
Copy link
Contributor

Wamrc will append custom name section to aot file if we set WAMR_BUILD_DUMP_CALL_STACK to 1(which is disable by default),
this makes aot_dump_call_stack() could print function name but no need to use -Wl, --export-all to export all methods(I haven't found some way to export all methods of AssemblyScript module).

I am not sure whether we should extract following duplicate methods, please help me to review it, thanks.

read_leb()
check_utf8_str()
const_str_list_insert()

core/iwasm/aot/aot_runtime.h Outdated Show resolved Hide resolved
@@ -223,6 +224,8 @@ typedef struct AOTModule {
/* constant string set */
HashMap *const_str_set;

StringList const_str_list;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to define a new string list, use const_str_set should be enough, and use macro read_string to read and insert the const string to the hash set.

core/iwasm/compilation/aot_emit_aot_file.c Outdated Show resolved Hide resolved
size = align_uint(size, 4);
/* section id + section size + sub section id */
size += (uint32)sizeof(uint32) * 3;
size += comp_data->name_section_buf_end - comp_data->name_section_buf;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we decode the function names previously here, and emit these strings with format 2-byte string len + string so that aot loader can just use read_string macro to read string? Ref to emitting of other strings.

core/iwasm/compilation/aot_emit_aot_file.c Outdated Show resolved Hide resolved
core/iwasm/compilation/aot_emit_aot_file.c Show resolved Hide resolved
}

static char *
const_str_list_insert(const uint8 *str, uint32 len, AOTModule *module,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use read_string macro instead

p += name_len;

while (p < p_end) {
read_leb_uint32(p, p_end, name_type);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just emit decoded uint32 directly, so here we can read uint32 directly, don't add decoding leb codes so as to reduce footprint.


for (name_index = 0; name_index < num_func_name;
name_index++) {
read_leb_uint32(p, p_end, func_index);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that here the func_index is bound to the following function_name, should add an array in AOTModule to store these func indexes? And when looking up, we should traverse the array one by one to lookup the func name of related func index?

@@ -2822,6 +2822,12 @@ get_func_name_from_index(const AOTModuleInstance *module_inst,
const char *func_name = NULL;
AOTModule *module = module_inst->aot_module.ptr;

#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
if (module->aux_func_name && func_index < module->aux_func_name_count) {
return *(module->aux_func_name + func_index);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems invalid, seems that we should also add a func index array in AOTModule, and here lookup it (Just my understanding, to be confirmed):

AOTModule {
    uint32 aux_func_name_count;
    uint32 *aux_func_indexes;
    const char *aux_func_names;
};
Here:
for (i = 0; i < aux_func_name_count; i++) {
    if (aux_func_indexes[i] == func_index)
        return aux_func_names[i];
}
Or sort the arrays previously, and here use quick search to lookup the func name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

.gitignore Show resolved Hide resolved
Comment on lines 528 to 542
if (name_len == 0 || p + name_len > p_end) {
set_error_buf(error_buf, error_buf_size, "unexpected end");
return false;
}

if (!check_utf8_str(p, name_len)) {
set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
return false;
}

if (memcmp(p, "name", 4) != 0) {
set_error_buf(error_buf, error_buf_size, "invalid custom name section");
return false;
}
p += name_len;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must the name here be "name" and the name_len be 4? If yes, how about changing the codes to be:

    if (name_len != 4 || p + name_len > p_end) {
        set_error_buf(error_buf, error_buf_size, "unexpected end");
        return false;
    }

    if (memcmp(p, "name", 4) != 0) {
        set_error_buf(error_buf, error_buf_size, "invalid custom name section");
        return false;
    }
    p += name_len;

So that we don't need to call check_utf8_str

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

for (name_index = 0; name_index < num_func_name;
name_index++) {
read_uint32(p, p_end, func_index);
if (func_index == previous_func_index) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be name_index != 0 && func_index == previous_func_index? The first func_index can be 0.

return false;
}
if (func_index < previous_func_index
&& previous_func_index != ~0U) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about change to if (name_index != 0 && func_index < previous_func_index)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And had better check whether func_index < module->import_func_count + module->func_count

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

core/iwasm/aot/aot_loader.c Show resolved Hide resolved
core/iwasm/compilation/aot_emit_aot_file.c Show resolved Hide resolved
Comment on lines 2827 to 2829
for (i = 0; i < module->aux_func_name_count; i++) {
if (module->aux_func_indexes[i] == func_index)
return module->aux_func_names[i];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Traversing array elements one by one is a little slow, as the elements in aux_func_indexes are sorted, can we use binary search to search the element, so as to improve the performance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@wenyongh wenyongh merged commit 788e14e into bytecodealliance:main Oct 26, 2021
victoryang00 pushed a commit to victoryang00/wamr-aot-gc-checkpoint-restore that referenced this pull request May 27, 2024
Enable emitting custom name section to aot file when adding
`--enable-dump-call-stack` or `--enable-dump-call-stack` to
wamrc and there is custom name section in wasm file, which
can be generated by wasi-sdk/emcc "-g" option. So aot runtime
can also get the function name from the custom name section
instead of export section,  to which developer should use
`--export-all` for wasi-sdk/emcc to generate export function
names.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants