Skip to content

Commit b6dea22

Browse files
Fix wasm loader check data segment count (#4039)
correctly report error when datacount section has non-zero data segment count while the data section is not present
1 parent e3ddbd5 commit b6dea22

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

core/iwasm/interpreter/wasm_loader.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4713,6 +4713,21 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
47134713
return false;
47144714
}
47154715

4716+
#if WASM_ENABLE_BULK_MEMORY != 0
4717+
static bool
4718+
check_data_count_consistency(bool has_datacount_section, int datacount_len,
4719+
int data_seg_len, char *error_buf,
4720+
uint32 error_buf_size)
4721+
{
4722+
if (has_datacount_section && datacount_len != data_seg_len) {
4723+
set_error_buf(error_buf, error_buf_size,
4724+
"data count and data section have inconsistent lengths");
4725+
return false;
4726+
}
4727+
return true;
4728+
}
4729+
#endif
4730+
47164731
static bool
47174732
load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
47184733
WASMModule *module,
@@ -4736,9 +4751,9 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
47364751
read_leb_uint32(p, p_end, data_seg_count);
47374752

47384753
#if WASM_ENABLE_BULK_MEMORY != 0
4739-
if (has_datacount_section && data_seg_count != module->data_seg_count1) {
4740-
set_error_buf(error_buf, error_buf_size,
4741-
"data count and data section have inconsistent lengths");
4754+
if (!check_data_count_consistency(has_datacount_section,
4755+
module->data_seg_count1, data_seg_count,
4756+
error_buf, error_buf_size)) {
47424757
return false;
47434758
}
47444759
#endif
@@ -5926,6 +5941,14 @@ load_from_sections(WASMModule *module, WASMSection *sections,
59265941
section = section->next;
59275942
}
59285943

5944+
#if WASM_ENABLE_BULK_MEMORY != 0
5945+
if (!check_data_count_consistency(
5946+
has_datacount_section, module->data_seg_count1,
5947+
module->data_seg_count, error_buf, error_buf_size)) {
5948+
return false;
5949+
}
5950+
#endif
5951+
59295952
module->aux_data_end_global_index = (uint32)-1;
59305953
module->aux_heap_base_global_index = (uint32)-1;
59315954
module->aux_stack_top_global_index = (uint32)-1;

core/iwasm/interpreter/wasm_mini_loader.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,6 +2734,11 @@ load_from_sections(WASMModule *module, WASMSection *sections,
27342734
section = section->next;
27352735
}
27362736

2737+
#if WASM_ENABLE_BULK_MEMORY != 0
2738+
bh_assert(!has_datacount_section
2739+
|| module->data_seg_count == module->data_seg_count1);
2740+
#endif
2741+
27372742
module->aux_data_end_global_index = (uint32)-1;
27382743
module->aux_heap_base_global_index = (uint32)-1;
27392744
module->aux_stack_top_global_index = (uint32)-1;

0 commit comments

Comments
 (0)