main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_export.h"
  6. #include "bh_read_file.h"
  7. int
  8. main(int argc, char *argv_main[])
  9. {
  10. int exit_code = EXIT_FAILURE;
  11. /* runtime */
  12. if (!wasm_runtime_init()) {
  13. printf("Init runtime failed.\n");
  14. return EXIT_FAILURE;
  15. }
  16. wasm_runtime_set_log_level(WASM_LOG_LEVEL_VERBOSE);
  17. /* wasm module file */
  18. char *buffer;
  19. uint32 buf_size;
  20. #if WASM_ENABLE_AOT != 0
  21. printf("Loading AOT file...\n");
  22. buffer = bh_read_file_to_buffer("import_memory.aot", &buf_size);
  23. #else
  24. printf("Loading WASM file...\n");
  25. buffer = bh_read_file_to_buffer("import_memory.wasm", &buf_size);
  26. #endif
  27. if (!buffer) {
  28. printf("Open wasm file failed.\n");
  29. goto destroy_runtime;
  30. }
  31. /* wasm module */
  32. char error_buf[128];
  33. wasm_module_t module = wasm_runtime_load((uint8 *)buffer, buf_size,
  34. error_buf, sizeof(error_buf));
  35. if (!module) {
  36. printf("Load wasm file failed: %s\n", error_buf);
  37. goto release_file_buffer;
  38. }
  39. /* import type */
  40. int32_t import_count = wasm_runtime_get_import_count(module);
  41. if (import_count < 0)
  42. goto unload_module;
  43. wasm_import_t import_type = { 0 };
  44. int32_t import_memory_index = -1;
  45. for (int32_t i = 0; i < import_count; i++) {
  46. wasm_runtime_get_import_type(module, (uint32_t)i, &import_type);
  47. if (import_type.kind == WASM_IMPORT_EXPORT_KIND_MEMORY) {
  48. import_memory_index = i;
  49. break;
  50. }
  51. }
  52. if (import_memory_index == -1) {
  53. printf("No memory import found.\n");
  54. goto unload_module;
  55. }
  56. /* host memory */
  57. wasm_memory_type_t memory_type = import_type.u.memory_type;
  58. wasm_memory_inst_t memory = wasm_runtime_create_memory(module, memory_type);
  59. if (!memory) {
  60. printf("Create memory failed.\n");
  61. goto unload_module;
  62. }
  63. /* import list */
  64. WASMExternInstance import_list[10] = { 0 };
  65. import_list[import_memory_index].module_name = "env";
  66. import_list[import_memory_index].field_name = "memory";
  67. import_list[import_memory_index].kind = WASM_IMPORT_EXPORT_KIND_MEMORY;
  68. import_list[import_memory_index].u.memory = memory;
  69. /* wasm instance */
  70. InstantiationArgs inst_args = {
  71. .default_stack_size = 65536,
  72. .imports = import_list,
  73. .import_count = 10,
  74. };
  75. wasm_module_inst_t inst = wasm_runtime_instantiate_ex(
  76. module, &inst_args, error_buf, sizeof(error_buf));
  77. if (!inst) {
  78. printf("Instantiate wasm file failed: %s\n", error_buf);
  79. wasm_runtime_destroy_memory(module, memory);
  80. memory = NULL;
  81. goto unload_module;
  82. }
  83. /* export function */
  84. wasm_function_inst_t func =
  85. wasm_runtime_lookup_function(inst, "goodhart_law");
  86. if (!func) {
  87. printf("The function goodhart_law is not found.\n");
  88. goto destroy_inst;
  89. }
  90. wasm_exec_env_t exec_env = wasm_runtime_create_exec_env(inst, 65536);
  91. if (!exec_env) {
  92. printf("Create wasm execution environment failed.\n");
  93. goto destroy_inst;
  94. }
  95. if (!wasm_runtime_call_wasm(exec_env, func, 0, NULL)) {
  96. printf("call wasm function goodhart_law failed. %s\n",
  97. wasm_runtime_get_exception(inst));
  98. goto destroy_exec_env;
  99. }
  100. exit_code = EXIT_SUCCESS;
  101. destroy_exec_env:
  102. wasm_runtime_destroy_exec_env(exec_env);
  103. destroy_inst:
  104. wasm_runtime_deinstantiate(inst);
  105. unload_module:
  106. wasm_runtime_unload(module);
  107. release_file_buffer:
  108. wasm_runtime_free(buffer);
  109. destroy_runtime:
  110. wasm_runtime_destroy();
  111. return exit_code;
  112. }