Implement wasm-c-api frame/trap APIs for interpreter mode (#660)

And enable to cache compiled AOT file buffer for wasm-c-api JIT mode
Avoid checks that rely on undefined C behavior
Fix issues of wasm-c-api sample trap and callback_chain

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
Wenyong Huang
2021-07-06 17:05:59 +08:00
committed by GitHub
parent d91047cc37
commit b554a9d05d
16 changed files with 576 additions and 104 deletions

View File

@ -92,32 +92,6 @@ DEFINE_FUNCTION(log)
return NULL;
}
static inline void
create_import_function_list(wasm_store_t *store,
const wasm_extern_t *import_function_list[])
{
#define IMPORT_FUNCTION_VARIABLE_NAME(name, ...) \
own wasm_func_t *function_##name = NULL;
IMPORT_FUNCTION_LIST(IMPORT_FUNCTION_VARIABLE_NAME)
#undef IMPORT_FUNCTION_VARIABLE_NAME
#define CREATE_WASM_FUNCTION(name, index, CREATE_FUNC_TYPE) \
{ \
own wasm_functype_t *type = CREATE_FUNC_TYPE; \
if (!(function_##name = wasm_func_new(store, type, STUB_##name))) { \
printf("> Error creating new function\n"); \
} \
wasm_functype_delete(type); \
}
IMPORT_FUNCTION_LIST(CREATE_WASM_FUNCTION)
#undef CREATE_WASM_FUNCTION
#define ADD_TO_FUNCTION_LIST(name, index, ...) \
import_function_list[index] = wasm_func_as_extern(function_##name);
IMPORT_FUNCTION_LIST(ADD_TO_FUNCTION_LIST)
#undef CREATE_IMPORT_FUNCTION
}
/**********************************************************************/
// all exportted wasm functions. check with "/opt/wabt/bin/wasm-objdump -x -j Export X.wasm"
// -1: memory
@ -221,11 +195,33 @@ main(int argc, const char *argv[])
// Instantiate.
printf("Instantiating module...\n");
const wasm_extern_t *imports[10] = { 0 };
// Create external functions.
printf("Creating callback...\n");
create_import_function_list(store, imports);
#define IMPORT_FUNCTION_VARIABLE_NAME(name, ...) \
own wasm_func_t *function_##name = NULL;
IMPORT_FUNCTION_LIST(IMPORT_FUNCTION_VARIABLE_NAME)
#undef IMPORT_FUNCTION_VARIABLE_NAME
const wasm_extern_t *imports[10] = { 0 };
#define CREATE_WASM_FUNCTION(name, index, CREATE_FUNC_TYPE) \
{ \
own wasm_functype_t *type = CREATE_FUNC_TYPE; \
if (!(function_##name = wasm_func_new(store, type, STUB_##name))) { \
printf("> Error creating new function\n"); \
return 1; \
} \
wasm_functype_delete(type); \
}
IMPORT_FUNCTION_LIST(CREATE_WASM_FUNCTION)
#undef CREATE_WASM_FUNCTION
#define ADD_TO_FUNCTION_LIST(name, index, ...) \
imports[index] = wasm_func_as_extern(function_##name);
IMPORT_FUNCTION_LIST(ADD_TO_FUNCTION_LIST)
#undef CREATE_IMPORT_FUNCTION
own wasm_instance_t *instance =
wasm_instance_new(store, module, imports, NULL);
@ -234,6 +230,11 @@ main(int argc, const char *argv[])
return 1;
}
#define DESTROY_WASM_FUNCITON(name, index, ...) \
wasm_func_delete(function_##name);
IMPORT_FUNCTION_LIST(DESTROY_WASM_FUNCITON)
#undef DESTROY_WASM_FUNCITON
// Extract export.
printf("Extracting export...\n");
wasm_instance_exports(instance, &exports);

View File

@ -19,6 +19,17 @@ own wasm_trap_t* fail_callback(
return trap;
}
void print_frame(wasm_frame_t* frame) {
printf("> %p @ 0x%zx = %"PRIu32".0x%zx\n",
wasm_frame_instance(frame),
wasm_frame_module_offset(frame),
wasm_frame_func_index(frame),
wasm_frame_func_offset(frame)
);
}
int main(int argc, const char* argv[]) {
// Initialize.
printf("Initializing...\n");
@ -93,7 +104,6 @@ int main(int argc, const char* argv[]) {
// Call.
for (int i = 0; i < 2; ++i) {
char buf[32];
const wasm_func_t* func = wasm_extern_as_func(exports.data[i]);
if (func == NULL) {
printf("> Error accessing export!\n");
@ -111,9 +121,29 @@ int main(int argc, const char* argv[]) {
printf("Printing message...\n");
own wasm_name_t message;
wasm_trap_message(trap, &message);
snprintf(buf, sizeof(buf), "> %%.%us\n", (unsigned)message.size);
printf(buf, message.data);
printf("> %s\n", message.data);
printf("Printing origin...\n");
own wasm_frame_t* frame = wasm_trap_origin(trap);
if (frame) {
print_frame(frame);
wasm_frame_delete(frame);
} else {
printf("> Empty origin.\n");
}
printf("Printing trace...\n");
own wasm_frame_vec_t trace;
wasm_trap_trace(trap, &trace);
if (trace.size > 0) {
for (size_t i = 0; i < trace.size; ++i) {
print_frame(trace.data[i]);
}
} else {
printf("> Empty trace.\n");
}
wasm_frame_vec_delete(&trace);
wasm_trap_delete(trap);
wasm_name_delete(&message);
}