Skip to content

Commit 0a80cc4

Browse files
authored
Fix wasm loader check data segment count (#3492)
When datacount section exists, loader will check whether the data count read from data segment section is same with the data count read from datacount section, but the value of latter can be 0, loader should not skip the check when the latter is 0. This fixes #3491. And fix handle_name_section return value not checked issue and early return true issue after handle_name_section. And also add the failed case in #3491 to ba-issues.
1 parent 082cfa1 commit 0a80cc4

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

core/iwasm/interpreter/wasm_loader.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4716,8 +4716,12 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
47164716

47174717
static bool
47184718
load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
4719-
WASMModule *module, bool clone_data_seg,
4720-
char *error_buf, uint32 error_buf_size)
4719+
WASMModule *module,
4720+
#if WASM_ENABLE_BULK_MEMORY != 0
4721+
bool has_datacount_section,
4722+
#endif
4723+
bool clone_data_seg, char *error_buf,
4724+
uint32 error_buf_size)
47214725
{
47224726
const uint8 *p = buf, *p_end = buf_end;
47234727
uint32 data_seg_count, i, mem_index, data_seg_len;
@@ -4733,8 +4737,7 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
47334737
read_leb_uint32(p, p_end, data_seg_count);
47344738

47354739
#if WASM_ENABLE_BULK_MEMORY != 0
4736-
if ((module->data_seg_count1 != 0)
4737-
&& (data_seg_count != module->data_seg_count1)) {
4740+
if (has_datacount_section && data_seg_count != module->data_seg_count1) {
47384741
set_error_buf(error_buf, error_buf_size,
47394742
"data count and data section have inconsistent lengths");
47404743
return false;
@@ -5242,10 +5245,11 @@ load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
52425245
module->name_section_buf = buf;
52435246
module->name_section_buf_end = buf_end;
52445247
p += name_len;
5245-
handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
5246-
error_buf_size);
5248+
if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
5249+
error_buf, error_buf_size)) {
5250+
return false;
5251+
}
52475252
LOG_VERBOSE("Load custom name section success.");
5248-
return true;
52495253
}
52505254
#endif
52515255

@@ -5789,6 +5793,9 @@ load_from_sections(WASMModule *module, WASMSection *sections,
57895793
uint8 malloc_free_io_type = VALUE_TYPE_I32;
57905794
bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
57915795
bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
5796+
#if WASM_ENABLE_BULK_MEMORY != 0
5797+
bool has_datacount_section = false;
5798+
#endif
57925799

57935800
/* Find code and function sections if have */
57945801
while (section) {
@@ -5881,6 +5888,9 @@ load_from_sections(WASMModule *module, WASMSection *sections,
58815888
break;
58825889
case SECTION_TYPE_DATA:
58835890
if (!load_data_segment_section(buf, buf_end, module,
5891+
#if WASM_ENABLE_BULK_MEMORY != 0
5892+
has_datacount_section,
5893+
#endif
58845894
clone_data_seg, error_buf,
58855895
error_buf_size))
58865896
return false;
@@ -5890,6 +5900,7 @@ load_from_sections(WASMModule *module, WASMSection *sections,
58905900
if (!load_datacount_section(buf, buf_end, module, error_buf,
58915901
error_buf_size))
58925902
return false;
5903+
has_datacount_section = true;
58935904
break;
58945905
#endif
58955906
#if WASM_ENABLE_STRINGREF != 0

core/iwasm/interpreter/wasm_mini_loader.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,8 +1740,12 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
17401740

17411741
static bool
17421742
load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
1743-
WASMModule *module, bool clone_data_seg,
1744-
char *error_buf, uint32 error_buf_size)
1743+
WASMModule *module,
1744+
#if WASM_ENABLE_BULK_MEMORY != 0
1745+
bool has_datacount_section,
1746+
#endif
1747+
bool clone_data_seg, char *error_buf,
1748+
uint32 error_buf_size)
17451749
{
17461750
const uint8 *p = buf, *p_end = buf_end;
17471751
uint32 data_seg_count, i, mem_index, data_seg_len;
@@ -1757,7 +1761,7 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
17571761
read_leb_uint32(p, p_end, data_seg_count);
17581762

17591763
#if WASM_ENABLE_BULK_MEMORY != 0
1760-
bh_assert(module->data_seg_count1 == 0
1764+
bh_assert(!has_datacount_section
17611765
|| data_seg_count == module->data_seg_count1);
17621766
#endif
17631767

@@ -2029,8 +2033,10 @@ load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
20292033
#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
20302034
if (name_len == 4 && memcmp(p, "name", 4) == 0) {
20312035
p += name_len;
2032-
handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
2033-
error_buf_size);
2036+
if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
2037+
error_buf, error_buf_size)) {
2038+
return false;
2039+
}
20342040
}
20352041
#endif
20362042
LOG_VERBOSE("Load custom section success.\n");
@@ -2579,6 +2585,9 @@ load_from_sections(WASMModule *module, WASMSection *sections,
25792585
uint8 malloc_free_io_type = VALUE_TYPE_I32;
25802586
bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
25812587
bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
2588+
#if WASM_ENABLE_BULK_MEMORY != 0
2589+
bool has_datacount_section = false;
2590+
#endif
25822591

25832592
/* Find code and function sections if have */
25842593
while (section) {
@@ -2660,6 +2669,9 @@ load_from_sections(WASMModule *module, WASMSection *sections,
26602669
break;
26612670
case SECTION_TYPE_DATA:
26622671
if (!load_data_segment_section(buf, buf_end, module,
2672+
#if WASM_ENABLE_BULK_MEMORY != 0
2673+
has_datacount_section,
2674+
#endif
26632675
clone_data_seg, error_buf,
26642676
error_buf_size))
26652677
return false;
@@ -2669,6 +2681,7 @@ load_from_sections(WASMModule *module, WASMSection *sections,
26692681
if (!load_datacount_section(buf, buf_end, module, error_buf,
26702682
error_buf_size))
26712683
return false;
2684+
has_datacount_section = true;
26722685
break;
26732686
#endif
26742687
default:
Binary file not shown.

tests/regression/ba-issues/running_config.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@
912912
"options": "",
913913
"argument": "",
914914
"expected return": {
915-
"ret code": 57,
915+
"ret code": 8,
916916
"stdout content": "",
917917
"description": "sock_shutdown on a non-socket file descriptor should fail with 57 notsock"
918918
}
@@ -1706,6 +1706,22 @@
17061706
"stdout content": "WASM module load failed: unknown type",
17071707
"description": "no '0x0:i64'"
17081708
}
1709+
},
1710+
{
1711+
"deprecated": false,
1712+
"ids": [
1713+
3491
1714+
],
1715+
"runtime": "iwasm-default-wasi-disabled",
1716+
"file": "nop_0LM_592_17171016522810388.wasm",
1717+
"mode": "fast-interp",
1718+
"options": "",
1719+
"argument": "",
1720+
"expected return": {
1721+
"ret code": 255,
1722+
"stdout content": "WASM module load failed: data count and data section have inconsistent lengths",
1723+
"description": "Check data segment count"
1724+
}
17091725
}
17101726
]
17111727
}

0 commit comments

Comments
 (0)