Skip to content

Commit

Permalink
Fix wasm loader check data segment count (#4039)
Browse files Browse the repository at this point in the history
correctly report error when datacount section has non-zero data segment count while the data section is not present
  • Loading branch information
TianlongLiang authored Jan 21, 2025
1 parent e3ddbd5 commit b6dea22
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
29 changes: 26 additions & 3 deletions core/iwasm/interpreter/wasm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -4713,6 +4713,21 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
return false;
}

#if WASM_ENABLE_BULK_MEMORY != 0
static bool
check_data_count_consistency(bool has_datacount_section, int datacount_len,

Check notice

Code scanning / CodeQL

Unused static function Note

Static function check_data_count_consistency is unreachable (
load
must be removed at the same time)
Static function check_data_count_consistency is unreachable (
load_from_sections
must be removed at the same time)
Static function check_data_count_consistency is unreachable (
load_data_segment_section
must be removed at the same time)
int data_seg_len, char *error_buf,
uint32 error_buf_size)
{
if (has_datacount_section && datacount_len != data_seg_len) {
set_error_buf(error_buf, error_buf_size,
"data count and data section have inconsistent lengths");
return false;
}
return true;
}
#endif

static bool
load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
WASMModule *module,
Expand All @@ -4736,9 +4751,9 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
read_leb_uint32(p, p_end, data_seg_count);

#if WASM_ENABLE_BULK_MEMORY != 0
if (has_datacount_section && data_seg_count != module->data_seg_count1) {
set_error_buf(error_buf, error_buf_size,
"data count and data section have inconsistent lengths");
if (!check_data_count_consistency(has_datacount_section,
module->data_seg_count1, data_seg_count,
error_buf, error_buf_size)) {
return false;
}
#endif
Expand Down Expand Up @@ -5926,6 +5941,14 @@ load_from_sections(WASMModule *module, WASMSection *sections,
section = section->next;
}

#if WASM_ENABLE_BULK_MEMORY != 0
if (!check_data_count_consistency(
has_datacount_section, module->data_seg_count1,
module->data_seg_count, error_buf, error_buf_size)) {
return false;
}
#endif

module->aux_data_end_global_index = (uint32)-1;
module->aux_heap_base_global_index = (uint32)-1;
module->aux_stack_top_global_index = (uint32)-1;
Expand Down
5 changes: 5 additions & 0 deletions core/iwasm/interpreter/wasm_mini_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2734,6 +2734,11 @@ load_from_sections(WASMModule *module, WASMSection *sections,
section = section->next;
}

#if WASM_ENABLE_BULK_MEMORY != 0
bh_assert(!has_datacount_section
|| module->data_seg_count == module->data_seg_count1);
#endif

module->aux_data_end_global_index = (uint32)-1;
module->aux_heap_base_global_index = (uint32)-1;
module->aux_stack_top_global_index = (uint32)-1;
Expand Down

0 comments on commit b6dea22

Please sign in to comment.