Skip to content

Commit c85bada

Browse files
authored
Add wasm module global type information APIs (#3406)
Support getting global type from `wasm_runtime_get_import_type` and `wasm_runtime_get_export_type`, and add two APIs: ```C wasm_valkind_t wasm_global_type_get_valkind(const wasm_global_type_t global_type); bool wasm_global_type_get_mutable(const wasm_global_type_t global_type); ```
1 parent 46f83cf commit c85bada

File tree

15 files changed

+255
-172
lines changed

15 files changed

+255
-172
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,17 +2042,18 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
20422042
/* Create each import global */
20432043
for (i = 0; i < module->import_global_count; i++) {
20442044
buf = (uint8 *)align_ptr(buf, 2);
2045-
read_uint8(buf, buf_end, import_globals[i].type);
2046-
read_uint8(buf, buf_end, import_globals[i].is_mutable);
2045+
read_uint8(buf, buf_end, import_globals[i].type.val_type);
2046+
read_uint8(buf, buf_end, import_globals[i].type.is_mutable);
20472047
read_string(buf, buf_end, import_globals[i].module_name);
20482048
read_string(buf, buf_end, import_globals[i].global_name);
20492049

20502050
#if WASM_ENABLE_LIBC_BUILTIN != 0
20512051
if (wasm_native_lookup_libc_builtin_global(
20522052
import_globals[i].module_name, import_globals[i].global_name,
20532053
&tmp_global)) {
2054-
if (tmp_global.type != import_globals[i].type
2055-
|| tmp_global.is_mutable != import_globals[i].is_mutable) {
2054+
if (tmp_global.type.val_type != import_globals[i].type.val_type
2055+
|| tmp_global.type.is_mutable
2056+
!= import_globals[i].type.is_mutable) {
20562057
set_error_buf(error_buf, error_buf_size,
20572058
"incompatible import type");
20582059
return false;
@@ -2065,7 +2066,8 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
20652066
import_globals[i].is_linked = false;
20662067
#endif
20672068

2068-
import_globals[i].size = wasm_value_type_size(import_globals[i].type);
2069+
import_globals[i].size =
2070+
wasm_value_type_size(import_globals[i].type.val_type);
20692071
import_globals[i].data_offset = data_offset;
20702072
data_offset += import_globals[i].size;
20712073
module->global_data_size += import_globals[i].size;
@@ -2130,16 +2132,16 @@ load_globals(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
21302132

21312133
/* Create each global */
21322134
for (i = 0; i < module->global_count; i++) {
2133-
read_uint8(buf, buf_end, globals[i].type);
2134-
read_uint8(buf, buf_end, globals[i].is_mutable);
2135+
read_uint8(buf, buf_end, globals[i].type.val_type);
2136+
read_uint8(buf, buf_end, globals[i].type.is_mutable);
21352137

21362138
buf = align_ptr(buf, 4);
21372139

21382140
if (!load_init_expr(&buf, buf_end, module, &globals[i].init_expr,
21392141
error_buf, error_buf_size))
21402142
return false;
21412143

2142-
globals[i].size = wasm_value_type_size(globals[i].type);
2144+
globals[i].size = wasm_value_type_size(globals[i].type.val_type);
21432145
globals[i].data_offset = data_offset;
21442146
data_offset += globals[i].size;
21452147
module->global_data_size += globals[i].size;

core/iwasm/aot/aot_runtime.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ check_global_init_expr(const AOTModule *module, uint32 global_index,
129129
* And initializer expression cannot reference a mutable global.
130130
*/
131131
if (global_index >= module->import_global_count
132-
|| module->import_globals->is_mutable) {
132+
|| module->import_globals->type.is_mutable) {
133133
set_error_buf(error_buf, error_buf_size,
134134
"constant expression required");
135135
return false;
@@ -141,7 +141,7 @@ check_global_init_expr(const AOTModule *module, uint32 global_index,
141141
return false;
142142
}
143143
if (global_index < module->import_global_count
144-
&& module->import_globals[global_index].is_mutable) {
144+
&& module->import_globals[global_index].type.is_mutable) {
145145
set_error_buf(error_buf, error_buf_size,
146146
"constant expression required");
147147
return false;
@@ -389,7 +389,7 @@ global_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
389389
for (i = 0; i < module->import_global_count; i++, import_global++) {
390390
bh_assert(import_global->data_offset
391391
== (uint32)(p - module_inst->global_data));
392-
init_global_data(p, import_global->type,
392+
init_global_data(p, import_global->type.val_type,
393393
&import_global->global_data_linked);
394394
p += import_global->size;
395395
}
@@ -410,20 +410,20 @@ global_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
410410
}
411411
#if WASM_ENABLE_GC == 0
412412
init_global_data(
413-
p, global->type,
413+
p, global->type.val_type,
414414
&module->import_globals[init_expr->u.global_index]
415415
.global_data_linked);
416416
#else
417417
if (init_expr->u.global_index < module->import_global_count) {
418418
init_global_data(
419-
p, global->type,
419+
p, global->type.val_type,
420420
&module->import_globals[init_expr->u.global_index]
421421
.global_data_linked);
422422
}
423423
else {
424424
uint32 global_idx =
425425
init_expr->u.global_index - module->import_global_count;
426-
init_global_data(p, global->type,
426+
init_global_data(p, global->type.val_type,
427427
&module->globals[global_idx].init_expr.u);
428428
}
429429
#endif
@@ -581,7 +581,7 @@ global_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
581581
#endif /* end of WASM_ENABLE_GC != 0 */
582582
default:
583583
{
584-
init_global_data(p, global->type, &init_expr->u);
584+
init_global_data(p, global->type.val_type, &init_expr->u);
585585
break;
586586
}
587587
}
@@ -4549,7 +4549,7 @@ aot_global_traverse_gc_rootset(AOTModuleInstance *module_inst, void *heap)
45494549
uint32 i;
45504550

45514551
for (i = 0; i < module->import_global_count; i++, import_global++) {
4552-
if (wasm_is_type_reftype(import_global->type)) {
4552+
if (wasm_is_type_reftype(import_global->type.val_type)) {
45534553
gc_obj = GET_REF_FROM_ADDR((uint32 *)global_data);
45544554
if (wasm_obj_is_created_from_heap(gc_obj)) {
45554555
if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))
@@ -4560,7 +4560,7 @@ aot_global_traverse_gc_rootset(AOTModuleInstance *module_inst, void *heap)
45604560
}
45614561

45624562
for (i = 0; i < module->global_count; i++, global++) {
4563-
if (wasm_is_type_reftype(global->type)) {
4563+
if (wasm_is_type_reftype(global->type.val_type)) {
45644564
gc_obj = GET_REF_FROM_ADDR((uint32 *)global_data);
45654565
if (wasm_obj_is_created_from_heap(gc_obj)) {
45664566
if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))

core/iwasm/common/wasm_c_api.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2521,8 +2521,8 @@ wasm_module_imports(const wasm_module_t *module, own wasm_importtype_vec_t *out)
25212521
+ (i - import_func_count);
25222522
module_name_rt = import->u.names.module_name;
25232523
field_name_rt = import->u.names.field_name;
2524-
val_type_rt = import->u.global.type;
2525-
mutability_rt = import->u.global.is_mutable;
2524+
val_type_rt = import->u.global.type.val_type;
2525+
mutability_rt = import->u.global.type.is_mutable;
25262526
}
25272527
#endif
25282528

@@ -2532,8 +2532,8 @@ wasm_module_imports(const wasm_module_t *module, own wasm_importtype_vec_t *out)
25322532
+ (i - import_func_count);
25332533
module_name_rt = import->module_name;
25342534
field_name_rt = import->global_name;
2535-
val_type_rt = import->type;
2536-
mutability_rt = import->is_mutable;
2535+
val_type_rt = import->type.val_type;
2536+
mutability_rt = import->type.is_mutable;
25372537
}
25382538
#endif
25392539

@@ -3634,15 +3634,15 @@ aot_global_set(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
36343634

36353635
if (global_idx_rt < module_aot->import_global_count) {
36363636
data_offset = module_aot->import_globals[global_idx_rt].data_offset;
3637-
val_type_rt = module_aot->import_globals[global_idx_rt].type;
3637+
val_type_rt = module_aot->import_globals[global_idx_rt].type.val_type;
36383638
}
36393639
else {
36403640
data_offset =
36413641
module_aot->globals[global_idx_rt - module_aot->import_global_count]
36423642
.data_offset;
36433643
val_type_rt =
36443644
module_aot->globals[global_idx_rt - module_aot->import_global_count]
3645-
.type;
3645+
.type.val_type;
36463646
}
36473647

36483648
data = (void *)(inst_aot->global_data + data_offset);
@@ -3661,15 +3661,15 @@ aot_global_get(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
36613661

36623662
if (global_idx_rt < module_aot->import_global_count) {
36633663
data_offset = module_aot->import_globals[global_idx_rt].data_offset;
3664-
val_type_rt = module_aot->import_globals[global_idx_rt].type;
3664+
val_type_rt = module_aot->import_globals[global_idx_rt].type.val_type;
36653665
}
36663666
else {
36673667
data_offset =
36683668
module_aot->globals[global_idx_rt - module_aot->import_global_count]
36693669
.data_offset;
36703670
val_type_rt =
36713671
module_aot->globals[global_idx_rt - module_aot->import_global_count]
3672-
.type;
3672+
.type.val_type;
36733673
}
36743674

36753675
data = inst_aot->global_data + data_offset;
@@ -3786,15 +3786,15 @@ wasm_global_new_internal(wasm_store_t *store, uint16 global_idx_rt,
37863786
if (global_idx_rt < module_aot->import_global_count) {
37873787
AOTImportGlobal *global_import_aot =
37883788
module_aot->import_globals + global_idx_rt;
3789-
val_type_rt = global_import_aot->type;
3790-
is_mutable = global_import_aot->is_mutable;
3789+
val_type_rt = global_import_aot->type.val_type;
3790+
is_mutable = global_import_aot->type.is_mutable;
37913791
}
37923792
else {
37933793
AOTGlobal *global_aot =
37943794
module_aot->globals
37953795
+ (global_idx_rt - module_aot->import_global_count);
3796-
val_type_rt = global_aot->type;
3797-
is_mutable = global_aot->is_mutable;
3796+
val_type_rt = global_aot->type.val_type;
3797+
is_mutable = global_aot->type.is_mutable;
37983798
}
37993799
}
38003800
#endif
@@ -4511,8 +4511,9 @@ interp_link_global(const WASMModule *module_interp, uint16 global_idx_rt,
45114511
return true;
45124512

45134513
/* type comparison */
4514-
if (!cmp_val_kind_with_val_type(wasm_valtype_kind(import->type->val_type),
4515-
imported_global_interp->u.global.type))
4514+
if (!cmp_val_kind_with_val_type(
4515+
wasm_valtype_kind(import->type->val_type),
4516+
imported_global_interp->u.global.type.val_type))
45164517
return false;
45174518

45184519
/* set init value */
@@ -4685,7 +4686,7 @@ aot_link_global(const AOTModule *module_aot, uint16 global_idx_rt,
46854686
bh_assert(val_type);
46864687

46874688
if (!cmp_val_kind_with_val_type(wasm_valtype_kind(val_type),
4688-
import_aot_global->type))
4689+
import_aot_global->type.val_type))
46894690
return false;
46904691

46914692
bh_assert(import->init);

core/iwasm/common/wasm_runtime_common.c

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3795,6 +3795,8 @@ wasm_runtime_get_import_type(WASMModuleCommon *const module, int32 import_index,
37953795
import_type->name = aot_import_global->global_name;
37963796
import_type->kind = WASM_IMPORT_EXPORT_KIND_GLOBAL;
37973797
import_type->linked = aot_import_global->is_linked;
3798+
import_type->u.global_type =
3799+
(WASMGlobalType *)&aot_import_global->type;
37983800
return;
37993801
}
38003802

@@ -3845,6 +3847,8 @@ wasm_runtime_get_import_type(WASMModuleCommon *const module, int32 import_index,
38453847
break;
38463848
case WASM_IMPORT_EXPORT_KIND_GLOBAL:
38473849
import_type->linked = wasm_import->u.global.is_linked;
3850+
import_type->u.global_type =
3851+
(WASMGlobalType *)&wasm_import->u.global.type;
38483852
break;
38493853
case WASM_IMPORT_EXPORT_KIND_TABLE:
38503854
/* not supported */
@@ -3916,11 +3920,32 @@ wasm_runtime_get_export_type(WASMModuleCommon *const module, int32 export_index,
39163920
const AOTExport *aot_export = &aot_module->exports[export_index];
39173921
export_type->name = aot_export->name;
39183922
export_type->kind = aot_export->kind;
3919-
if (export_type->kind == EXPORT_KIND_FUNC) {
3920-
export_type->u.func_type =
3921-
(AOTFuncType *)aot_module->types
3922-
[aot_module->func_type_indexes
3923-
[aot_export->index - aot_module->import_func_count]];
3923+
switch (export_type->kind) {
3924+
case WASM_IMPORT_EXPORT_KIND_FUNC:
3925+
export_type->u.func_type =
3926+
(AOTFuncType *)aot_module
3927+
->types[aot_module->func_type_indexes
3928+
[aot_export->index
3929+
- aot_module->import_func_count]];
3930+
break;
3931+
case WASM_IMPORT_EXPORT_KIND_GLOBAL:
3932+
export_type->u.global_type =
3933+
&aot_module
3934+
->globals[aot_export->index
3935+
- aot_module->import_global_count]
3936+
.type;
3937+
break;
3938+
case WASM_IMPORT_EXPORT_KIND_TABLE:
3939+
/* not supported */
3940+
// export_type->linked = false;
3941+
break;
3942+
case WASM_IMPORT_EXPORT_KIND_MEMORY:
3943+
/* not supported */
3944+
// export_type->linked = false;
3945+
break;
3946+
default:
3947+
bh_assert(0);
3948+
break;
39243949
}
39253950
return;
39263951
}
@@ -3937,12 +3962,31 @@ wasm_runtime_get_export_type(WASMModuleCommon *const module, int32 export_index,
39373962
const WASMExport *wasm_export = &wasm_module->exports[export_index];
39383963
export_type->name = wasm_export->name;
39393964
export_type->kind = wasm_export->kind;
3940-
if (wasm_export->kind == EXPORT_KIND_FUNC) {
3941-
export_type->u.func_type =
3942-
wasm_module
3943-
->functions[wasm_export->index
3944-
- wasm_module->import_function_count]
3945-
->func_type;
3965+
switch (export_type->kind) {
3966+
case WASM_IMPORT_EXPORT_KIND_FUNC:
3967+
export_type->u.func_type =
3968+
wasm_module
3969+
->functions[wasm_export->index
3970+
- wasm_module->import_function_count]
3971+
->func_type;
3972+
break;
3973+
case WASM_IMPORT_EXPORT_KIND_GLOBAL:
3974+
export_type->u.global_type =
3975+
&wasm_module
3976+
->globals[wasm_export->index
3977+
- wasm_module->import_global_count]
3978+
.type;
3979+
break;
3980+
case WASM_IMPORT_EXPORT_KIND_TABLE:
3981+
/* not supported */
3982+
// export_type->linked = false;
3983+
break;
3984+
case WASM_IMPORT_EXPORT_KIND_MEMORY:
3985+
/* not supported */
3986+
// export_type->linked = false;
3987+
break;
3988+
bh_assert(0);
3989+
break;
39463990
}
39473991
return;
39483992
}
@@ -4033,6 +4077,22 @@ wasm_func_type_get_result_valkind(WASMFuncType *const func_type,
40334077
}
40344078
}
40354079

4080+
wasm_valkind_t
4081+
wasm_global_type_get_valkind(const wasm_global_type_t global_type)
4082+
{
4083+
bh_assert(global_type);
4084+
4085+
return val_type_to_val_kind(global_type->val_type);
4086+
}
4087+
4088+
bool
4089+
wasm_global_type_get_mutable(const wasm_global_type_t global_type)
4090+
{
4091+
bh_assert(global_type);
4092+
4093+
return global_type->is_mutable;
4094+
}
4095+
40364096
bool
40374097
wasm_runtime_register_natives(const char *module_name,
40384098
NativeSymbol *native_symbols,
@@ -5991,7 +6051,7 @@ aot_mark_all_externrefs(AOTModuleInstance *module_inst)
59916051
const AOTTableInstance *table_inst;
59926052

59936053
for (i = 0; i < module->global_count; i++, global++) {
5994-
if (global->type == VALUE_TYPE_EXTERNREF) {
6054+
if (global->type.val_type == VALUE_TYPE_EXTERNREF) {
59956055
mark_externref(
59966056
*(uint32 *)(module_inst->global_data + global->data_offset));
59976057
}
@@ -6320,14 +6380,14 @@ wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
63206380
if (export->index < module->import_global_count) {
63216381
WASMGlobalImport *import_global =
63226382
&((module->import_globals + export->index)->u.global);
6323-
*out_val_type = import_global->type;
6324-
*out_mutability = import_global->is_mutable;
6383+
*out_val_type = import_global->type.val_type;
6384+
*out_mutability = import_global->type.is_mutable;
63256385
}
63266386
else {
63276387
WASMGlobal *global =
63286388
module->globals + (export->index - module->import_global_count);
6329-
*out_val_type = global->type;
6330-
*out_mutability = global->is_mutable;
6389+
*out_val_type = global->type.val_type;
6390+
*out_mutability = global->type.is_mutable;
63316391
}
63326392
return true;
63336393
}
@@ -6340,14 +6400,14 @@ wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
63406400
if (export->index < module->import_global_count) {
63416401
AOTImportGlobal *import_global =
63426402
module->import_globals + export->index;
6343-
*out_val_type = import_global->type;
6344-
*out_mutability = import_global->is_mutable;
6403+
*out_val_type = import_global->type.val_type;
6404+
*out_mutability = import_global->type.is_mutable;
63456405
}
63466406
else {
63476407
AOTGlobal *global =
63486408
module->globals + (export->index - module->import_global_count);
6349-
*out_val_type = global->type;
6350-
*out_mutability = global->is_mutable;
6409+
*out_val_type = global->type.val_type;
6410+
*out_mutability = global->type.is_mutable;
63516411
}
63526412
return true;
63536413
}

0 commit comments

Comments
 (0)