Skip to content

Commit

Permalink
Correct Exception Handling tag type when GC is enabled (#3413)
Browse files Browse the repository at this point in the history
Use `WASMFuncType` to represent tag_type in `WASMTagImport` and
`WASMTag` so that the type definition is consistent no matter to GC is
enabled or not.

This PR fixes #3409.
  • Loading branch information
TianlongLiang committed May 13, 2024
1 parent 22eef3d commit c6d42db
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
4 changes: 2 additions & 2 deletions core/iwasm/interpreter/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ typedef struct WASMTagImport {
char *field_name;
uint8 attribute; /* the type of the tag (numerical) */
uint32 type; /* the type of the catch function (numerical)*/
WASMType *tag_type;
WASMFuncType *tag_type;
void *tag_ptr_linked;

#if WASM_ENABLE_MULTI_MODULE != 0
Expand Down Expand Up @@ -706,7 +706,7 @@ struct WASMFunction {
struct WASMTag {
uint8 attribute; /* the attribute property of the tag (expected to be 0) */
uint32 type; /* the type of the tag (expected valid inden in type table) */
WASMType *tag_type;
WASMFuncType *tag_type;
};
#endif

Expand Down
2 changes: 1 addition & 1 deletion core/iwasm/interpreter/wasm_interp_classic.c
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
/* landing pad for the rethrow ? */
find_a_catch_handler:
{
WASMType *tag_type = NULL;
WASMFuncType *tag_type = NULL;
uint32 cell_num_to_copy = 0;
if (IS_INVALID_TAGINDEX(exception_tag_index)) {
/*
Expand Down
36 changes: 31 additions & 5 deletions core/iwasm/interpreter/wasm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2919,7 +2919,8 @@ load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
goto fail;
}

WASMType *declare_tag_type = parent_module->types[declare_type_index];
WASMFuncType *declare_tag_type =
(WASMFuncType *)parent_module->types[declare_type_index];

/* check, that the type of the declared tag returns void */
if (declare_tag_type->result_count != 0) {
Expand Down Expand Up @@ -4806,7 +4807,7 @@ load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,

/* get return type (must be 0) */
/* check, that the type of the referred tag returns void */
WASMType *func_type = (WASMType *)module->types[tag_type];
WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
if (func_type->result_count != 0) {
set_error_buf(error_buf, error_buf_size,
"non-empty tag result type");
Expand Down Expand Up @@ -11122,7 +11123,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,

/* the tag_type is stored in either the WASMTag (section tags)
* or WASMTagImport (import tag) */
WASMType *tag_type = NULL;
WASMFuncType *tag_type = NULL;
if (tag_index < module->import_tag_count) {
tag_type = module->import_tags[tag_index].u.tag.tag_type;
}
Expand All @@ -11145,11 +11146,36 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
/* Check stack values match return types by comparing tag param
* types with stack cells */
uint8 *frame_ref = loader_ctx->frame_ref;
#if WASM_ENABLE_GC != 0
WASMRefTypeMap *frame_reftype_map =
loader_ctx->frame_reftype_map;
uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
param_reftype_maps = tag_type->ref_type_maps;
/* For tag_type function, it shouldn't have result_count = 0 */
param_reftype_map_count = tag_type->ref_type_map_count;
param_count = (int32)tag_type->param_count;
#endif

for (int tti = (int32)tag_type->param_count - 1; tti >= 0;
tti--) {
#if WASM_ENABLE_GC != 0
local_type = tag_type->types[tti];
local_idx = tti;
/* Get the wasm_ref_type if the local_type is multibyte
* type */
GET_LOCAL_REFTYPE();
#endif

if (!check_stack_top_values(
loader_ctx, frame_ref, available_stack_cell,
tag_type->types[tti], error_buf, error_buf_size)) {
#if WASM_ENABLE_GC != 0
frame_reftype_map, frame_reftype_map_num,
#endif
tag_type->types[tti],
#if WASM_ENABLE_GC != 0
&wasm_ref_type,
#endif
error_buf, error_buf_size)) {
snprintf(error_buf, error_buf_size,
"type mismatch: instruction requires [%s] but "
"stack has [%s]",
Expand Down Expand Up @@ -11232,7 +11258,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,

/* the tag_type is stored in either the WASMTag (section tags)
* or WASMTagImport (import tag) */
WASMType *func_type = NULL;
WASMFuncType *func_type = NULL;
if (tag_index < module->import_tag_count) {
func_type = module->import_tags[tag_index].u.tag.tag_type;
}
Expand Down

0 comments on commit c6d42db

Please sign in to comment.