@@ -379,7 +379,6 @@ memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
379379 mem = mem_new; \
380380 } while (0)
381381
382- #if WASM_ENABLE_GC != 0
383382static bool
384383check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
385384 char *error_buf, uint32 error_buf_size)
@@ -392,6 +391,7 @@ check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
392391 return true;
393392}
394393
394+ #if WASM_ENABLE_GC != 0
395395static bool
396396check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
397397 uint32 error_buf_size)
@@ -409,6 +409,29 @@ check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
409409}
410410#endif
411411
412+ /*
413+ * if no GC is enabled, an valid type is always a function type.
414+ * but if GC is enabled, we need to check the type flag
415+ */
416+ static bool
417+ check_function_type(const WASMModule *module, uint32 type_index,
418+ char *error_buf, uint32 error_buf_size)
419+ {
420+ if (!check_type_index(module, module->type_count, type_index, error_buf,
421+ error_buf_size)) {
422+ return false;
423+ }
424+
425+ #if WASM_ENABLE_GC != 0
426+ if (module->types[type_index]->type_flag != WASM_TYPE_FUNC) {
427+ set_error_buf(error_buf, error_buf_size, "unknown function type");
428+ return false;
429+ }
430+ #endif
431+
432+ return true;
433+ }
434+
412435static bool
413436check_function_index(const WASMModule *module, uint32 function_index,
414437 char *error_buf, uint32 error_buf_size)
@@ -2479,8 +2502,8 @@ load_function_import(const uint8 **p_buf, const uint8 *buf_end,
24792502 read_leb_uint32(p, p_end, declare_type_index);
24802503 *p_buf = p;
24812504
2482- if (declare_type_index >= parent_module->type_count) {
2483- set_error_buf(error_buf, error_buf_size, "unknown type");
2505+ if (!check_function_type( parent_module, declare_type_index, error_buf,
2506+ error_buf_size)) {
24842507 return false;
24852508 }
24862509
@@ -2893,8 +2916,8 @@ load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
28932916 /* get type */
28942917 read_leb_uint32(p, p_end, declare_type_index);
28952918 /* compare against module->types */
2896- if (declare_type_index >= parent_module->type_count) {
2897- set_error_buf(error_buf, error_buf_size, "unknown tag type");
2919+ if (!check_function_type( parent_module, declare_type_index, error_buf,
2920+ error_buf_size)) {
28982921 goto fail;
28992922 }
29002923
@@ -3563,8 +3586,9 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
35633586 for (i = 0; i < func_count; i++) {
35643587 /* Resolve function type */
35653588 read_leb_uint32(p, p_end, type_index);
3566- if (type_index >= module->type_count) {
3567- set_error_buf(error_buf, error_buf_size, "unknown type");
3589+
3590+ if (!check_function_type(module, type_index, error_buf,
3591+ error_buf_size)) {
35683592 return false;
35693593 }
35703594
@@ -4970,8 +4994,8 @@ load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
49704994 /* get type */
49714995 read_leb_uint32(p, p_end, tag_type);
49724996 /* compare against module->types */
4973- if (tag_type >= module->type_count) {
4974- set_error_buf(error_buf, error_buf_size, "unknown type");
4997+ if (!check_function_type( module, tag_type, error_buf,
4998+ error_buf_size)) {
49754999 return false;
49765000 }
49775001
@@ -10477,7 +10501,7 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
1047710501 * match block type. */
1047810502 if (cur_block->is_stack_polymorphic) {
1047910503#if WASM_ENABLE_GC != 0
10480- int32 j = reftype_map_count - 1;
10504+ int32 j = (int32) reftype_map_count - 1;
1048110505#endif
1048210506 for (i = (int32)arity - 1; i >= 0; i--) {
1048310507#if WASM_ENABLE_GC != 0
@@ -10780,7 +10804,7 @@ check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
1078010804 * match block type. */
1078110805 if (block->is_stack_polymorphic) {
1078210806#if WASM_ENABLE_GC != 0
10783- int32 j = return_reftype_map_count - 1;
10807+ int32 j = (int32) return_reftype_map_count - 1;
1078410808#endif
1078510809 for (i = (int32)return_count - 1; i >= 0; i--) {
1078610810#if WASM_ENABLE_GC != 0
@@ -11549,15 +11573,17 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1154911573 }
1155011574 else {
1155111575 int32 type_index;
11576+
1155211577 /* Resolve the leb128 encoded type index as block type */
1155311578 p--;
1155411579 p_org = p - 1;
1155511580 pb_read_leb_int32(p, p_end, type_index);
11556- if ((uint32)type_index >= module->type_count) {
11557- set_error_buf(error_buf, error_buf_size ,
11558- "unknown type");
11581+
11582+ if (!check_function_type(module, type_index, error_buf ,
11583+ error_buf_size)) {
1155911584 goto fail;
1156011585 }
11586+
1156111587 block_type.is_value_type = false;
1156211588 block_type.u.type =
1156311589 (WASMFuncType *)module->types[type_index];
@@ -12607,8 +12633,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1260712633 /* skip elem idx */
1260812634 POP_TBL_ELEM_IDX();
1260912635
12610- if (type_idx >= module->type_count) {
12611- set_error_buf(error_buf, error_buf_size, "unknown type");
12636+ if (!check_function_type( module, type_idx, error_buf,
12637+ error_buf_size)) {
1261212638 goto fail;
1261312639 }
1261412640
0 commit comments