Skip to content

Commit 65d3ffa

Browse files
authored
add validation for recursive type count in loader (#4440)
1 parent 47c7c85 commit 65d3ffa

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

core/iwasm/interpreter/wasm_loader.c

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
400400
error_buf_size)) {
401401
return false;
402402
}
403-
if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
403+
if (module->types[type_index] == NULL
404+
|| module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
404405
set_error_buf(error_buf, error_buf_size, "unknown array type");
405406
return false;
406407
}
@@ -423,7 +424,8 @@ check_function_type(const WASMModule *module, uint32 type_index,
423424
}
424425

425426
#if WASM_ENABLE_GC != 0
426-
if (module->types[type_index]->type_flag != WASM_TYPE_FUNC) {
427+
if (module->types[type_index] == NULL
428+
|| module->types[type_index]->type_flag != WASM_TYPE_FUNC) {
427429
set_error_buf(error_buf, error_buf_size, "unknown function type");
428430
return false;
429431
}
@@ -1255,8 +1257,9 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
12551257
error_buf_size)) {
12561258
goto fail;
12571259
}
1258-
if (module->types[type_idx]->type_flag
1259-
!= WASM_TYPE_STRUCT) {
1260+
if (module->types[type_idx] == NULL
1261+
|| module->types[type_idx]->type_flag
1262+
!= WASM_TYPE_STRUCT) {
12601263
set_error_buf(error_buf, error_buf_size,
12611264
"unknown struct type");
12621265
goto fail;
@@ -2303,9 +2306,14 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
23032306
total_size = new_total_size;
23042307
}
23052308

2306-
LOG_VERBOSE("Processing rec group [%d-%d]",
2307-
processed_type_count,
2308-
processed_type_count + rec_count - 1);
2309+
if (rec_count < 1) {
2310+
LOG_VERBOSE("Processing 0-entry rec group");
2311+
}
2312+
else {
2313+
LOG_VERBOSE("Processing rec group [%d-%d]",
2314+
processed_type_count,
2315+
processed_type_count + rec_count - 1);
2316+
}
23092317
}
23102318
else {
23112319
p--;
@@ -12677,7 +12685,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1267712685
error_buf, error_buf_size)) {
1267812686
goto fail;
1267912687
}
12680-
if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
12688+
if (module->types[type_idx1] == NULL
12689+
|| module->types[type_idx1]->type_flag
12690+
!= WASM_TYPE_FUNC) {
1268112691
set_error_buf(error_buf, error_buf_size,
1268212692
"unknown function type");
1268312693
goto fail;
@@ -12694,7 +12704,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1269412704
error_buf, error_buf_size)) {
1269512705
goto fail;
1269612706
}
12697-
if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
12707+
if (module->types[type_idx] == NULL
12708+
|| module->types[type_idx]->type_flag
12709+
!= WASM_TYPE_FUNC) {
1269812710
set_error_buf(error_buf, error_buf_size,
1269912711
"unknown function type");
1270012712
goto fail;
@@ -14533,8 +14545,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1453314545
error_buf_size)) {
1453414546
goto fail;
1453514547
}
14536-
if (module->types[type_idx]->type_flag
14537-
!= WASM_TYPE_STRUCT) {
14548+
if (module->types[type_idx] == NULL
14549+
|| module->types[type_idx]->type_flag
14550+
!= WASM_TYPE_STRUCT) {
1453814551
set_error_buf(error_buf, error_buf_size,
1453914552
"unknown struct type");
1454014553
goto fail;
@@ -14620,8 +14633,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1462014633
error_buf_size)) {
1462114634
goto fail;
1462214635
}
14623-
if (module->types[type_idx]->type_flag
14624-
!= WASM_TYPE_STRUCT) {
14636+
if (module->types[type_idx] == NULL
14637+
|| module->types[type_idx]->type_flag
14638+
!= WASM_TYPE_STRUCT) {
1462514639
set_error_buf(error_buf, error_buf_size,
1462614640
"unknown struct type");
1462714641
goto fail;

0 commit comments

Comments
 (0)