Skip to content

Commit dcf56e4

Browse files
nchamznwenyongh
authored andcommitted
Create trap for error message when wasm_instance_new fails (#1751)
Create trap for error message when wasm_instance_new fails: - Similar to [this PR](#1526), but create a wasm_trap_t to output the error msg instead of adding error_buf to the API. - Trap will need to be deleted by the caller but is not a breaking change as it is only created if trap is not NULL. - Add error messages for all failure cases here, try to make them accurate but welcome feedback for improvements. Signed-off-by: Andrew Chambers <[email protected]>
1 parent dff74c4 commit dcf56e4

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

core/iwasm/common/wasm_c_api.c

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4444,25 +4444,25 @@ aot_process_export(wasm_store_t *store, const AOTModuleInstance *inst_aot,
44444444

44454445
wasm_instance_t *
44464446
wasm_instance_new(wasm_store_t *store, const wasm_module_t *module,
4447-
const wasm_extern_vec_t *imports, own wasm_trap_t **traps)
4447+
const wasm_extern_vec_t *imports, own wasm_trap_t **trap)
44484448
{
4449-
return wasm_instance_new_with_args(store, module, imports, traps,
4449+
return wasm_instance_new_with_args(store, module, imports, trap,
44504450
KILOBYTE(32), KILOBYTE(32));
44514451
}
44524452

44534453
wasm_instance_t *
44544454
wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
44554455
const wasm_extern_vec_t *imports,
4456-
own wasm_trap_t **traps, const uint32 stack_size,
4456+
own wasm_trap_t **trap, const uint32 stack_size,
44574457
const uint32 heap_size)
44584458
{
4459-
char error_buf[128] = { 0 };
4459+
char sub_error_buf[128] = { 0 };
4460+
char error_buf[256] = { 0 };
44604461
uint32 import_count = 0;
44614462
bool import_count_verified = false;
44624463
wasm_instance_t *instance = NULL;
44634464
uint32 i = 0;
44644465
bool processed = false;
4465-
(void)traps;
44664466

44674467
bh_assert(singleton_engine);
44684468

@@ -4474,6 +4474,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
44744474

44754475
instance = malloc_internal(sizeof(wasm_instance_t));
44764476
if (!instance) {
4477+
snprintf(sub_error_buf, sizeof(sub_error_buf),
4478+
"Failed to malloc instance");
44774479
goto failed;
44784480
}
44794481

@@ -4490,6 +4492,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
44904492
/* make sure a complete import list */
44914493
if ((int32)import_count < 0
44924494
|| import_count != actual_link_import_count) {
4495+
snprintf(sub_error_buf, sizeof(sub_error_buf),
4496+
"Failed to validate imports");
44934497
goto failed;
44944498
}
44954499
}
@@ -4508,6 +4512,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
45084512
import_count = aot_link(instance, MODULE_AOT(module),
45094513
(wasm_extern_t **)imports->data);
45104514
if ((int32)import_count < 0) {
4515+
snprintf(sub_error_buf, sizeof(sub_error_buf),
4516+
"Failed to validate imports");
45114517
goto failed;
45124518
}
45134519
}
@@ -4520,18 +4526,21 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
45204526
* also leads to below branch
45214527
*/
45224528
if (!import_count_verified) {
4529+
snprintf(sub_error_buf, sizeof(sub_error_buf),
4530+
"Failed to verify import count");
45234531
goto failed;
45244532
}
45254533
}
45264534

45274535
instance->inst_comm_rt = wasm_runtime_instantiate(
4528-
*module, stack_size, heap_size, error_buf, sizeof(error_buf));
4536+
*module, stack_size, heap_size, sub_error_buf, sizeof(sub_error_buf));
45294537
if (!instance->inst_comm_rt) {
4530-
LOG_ERROR(error_buf);
45314538
goto failed;
45324539
}
45334540

45344541
if (!wasm_runtime_create_exec_env_singleton(instance->inst_comm_rt)) {
4542+
snprintf(sub_error_buf, sizeof(sub_error_buf),
4543+
"Failed to create exec env singleton");
45354544
goto failed;
45364545
}
45374546

@@ -4556,6 +4565,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
45564565
instance->inst_comm_rt;
45574566
break;
45584567
default:
4568+
snprintf(sub_error_buf, sizeof(sub_error_buf),
4569+
"Unknown import kind");
45594570
goto failed;
45604571
}
45614572
}
@@ -4572,6 +4583,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
45724583
if (!interp_process_export(store,
45734584
(WASMModuleInstance *)instance->inst_comm_rt,
45744585
instance->exports)) {
4586+
snprintf(sub_error_buf, sizeof(sub_error_buf),
4587+
"Interpreter failed to process exports");
45754588
goto failed;
45764589
}
45774590

@@ -4594,6 +4607,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
45944607
if (!aot_process_export(store,
45954608
(AOTModuleInstance *)instance->inst_comm_rt,
45964609
instance->exports)) {
4610+
snprintf(sub_error_buf, sizeof(sub_error_buf),
4611+
"AOT failed to process exports");
45974612
goto failed;
45984613
}
45994614

@@ -4606,11 +4621,15 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
46064621
* leads to below branch
46074622
*/
46084623
if (!processed) {
4624+
snprintf(sub_error_buf, sizeof(sub_error_buf),
4625+
"Incorrect filetype and compilation flags");
46094626
goto failed;
46104627
}
46114628

46124629
/* add it to a watching list in store */
46134630
if (!bh_vector_append((Vector *)store->instances, &instance)) {
4631+
snprintf(sub_error_buf, sizeof(sub_error_buf),
4632+
"Failed to add to store instances");
46144633
goto failed;
46154634
}
46164635

@@ -4619,7 +4638,15 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
46194638
return instance;
46204639

46214640
failed:
4622-
LOG_DEBUG("%s failed", __FUNCTION__);
4641+
snprintf(error_buf, sizeof(error_buf), "%s failed: %s", __FUNCTION__,
4642+
sub_error_buf);
4643+
if (trap != NULL) {
4644+
wasm_message_t message = { 0 };
4645+
wasm_name_new_from_string(&message, error_buf);
4646+
*trap = wasm_trap_new(store, &message);
4647+
wasm_byte_vec_delete(&message);
4648+
}
4649+
LOG_DEBUG(error_buf);
46234650
wasm_instance_delete_internal(instance);
46244651
return NULL;
46254652
}

core/iwasm/include/wasm_c_api.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,13 +592,13 @@ WASM_DECLARE_REF(instance)
592592

593593
WASM_API_EXTERN own wasm_instance_t* wasm_instance_new(
594594
wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t *imports,
595-
own wasm_trap_t**
595+
own wasm_trap_t** trap
596596
);
597597

598598
// please refer to wasm_runtime_instantiate(...) in core/iwasm/include/wasm_export.h
599599
WASM_API_EXTERN own wasm_instance_t* wasm_instance_new_with_args(
600600
wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t *imports,
601-
own wasm_trap_t**, const uint32_t stack_size, const uint32_t heap_size
601+
own wasm_trap_t** trap, const uint32_t stack_size, const uint32_t heap_size
602602
);
603603

604604
WASM_API_EXTERN void wasm_instance_exports(const wasm_instance_t*, own wasm_extern_vec_t* out);

0 commit comments

Comments
 (0)