Append \0 to every name string in aot name section (#3249)

Since strings in .name section in .wasm is not c-style, need to append
a `\0` to each string in .name section in AOT file when emitting.
This commit is contained in:
liang.he 2024-03-26 14:27:20 +08:00 committed by GitHub
parent d8d8f8ce04
commit 498eb5d54a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 9 deletions

View File

@ -1485,9 +1485,16 @@ fail_integer_too_large:
res = (uint32)res64; \
} while (0)
/*
* - transfer .name section in .wasm (comp_data->name_section_buf) to
* aot buf (comp_data->aot_name_section_buf)
* - leb128 to u32
* - add `\0` at the end of every name, and adjust length(+1)
*/
static uint32
get_name_section_size(AOTCompData *comp_data)
{
/* original name section content in .wasm */
const uint8 *p = comp_data->name_section_buf,
*p_end = comp_data->name_section_buf_end;
uint8 *buf, *buf_end;
@ -1514,22 +1521,20 @@ get_name_section_size(AOTCompData *comp_data)
aot_set_last_error("allocate memory for custom name section failed.");
return 0;
}
memset(buf, 0, (uint32)max_aot_buf_size);
buf_end = buf + max_aot_buf_size;
/* the size of "name". it should be 4 */
read_leb_uint32(p, p_end, name_len);
offset = align_uint(offset, 4);
EMIT_U32(name_len);
if (name_len == 0 || p + name_len > p_end) {
if (name_len != 4 || p + name_len > p_end) {
aot_set_last_error("unexpected end");
return 0;
}
if (!wasm_check_utf8_str(p, name_len)) {
aot_set_last_error("invalid UTF-8 encoding");
return 0;
}
/* "name" */
if (memcmp(p, "name", 4) != 0) {
aot_set_last_error("invalid custom name section");
return 0;
@ -1578,9 +1583,18 @@ get_name_section_size(AOTCompData *comp_data)
previous_func_index = func_index;
read_leb_uint32(p, p_end, func_name_len);
offset = align_uint(offset, 2);
EMIT_U16(func_name_len);
/* emit a string ends with `\0` */
if (func_name_len + 1 > UINT16_MAX) {
aot_set_last_error(
"emit string failed: string too long");
goto fail;
}
/* extra 1 byte for \0 */
EMIT_U16(func_name_len + 1);
EMIT_BUF(p, func_name_len);
p += func_name_len;
EMIT_U8(0);
}
}
break;

View File

@ -4923,7 +4923,7 @@ load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
}
#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
if (memcmp(p, "name", 4) == 0) {
if (name_len == 4 && memcmp(p, "name", 4) == 0) {
module->name_section_buf = buf;
module->name_section_buf_end = buf_end;
p += name_len;

View File

@ -1942,7 +1942,7 @@ 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) {
if (name_len == 4 && memcmp(p, "name", 4) == 0) {
p += name_len;
handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
error_buf_size);