example1.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include "wasm_c_api.h"
  8. #include "wasm_export.h"
  9. static wasm_trap_t *
  10. host_logs(const wasm_val_vec_t *args, wasm_val_vec_t *results)
  11. {
  12. return NULL;
  13. }
  14. static bool
  15. build_imports(wasm_store_t *store, const wasm_module_t *module,
  16. wasm_extern_vec_t *out)
  17. {
  18. wasm_importtype_vec_t importtypes = { 0 };
  19. wasm_module_imports(module, &importtypes);
  20. wasm_extern_t *externs[32] = { 0 };
  21. for (unsigned i = 0; i < importtypes.num_elems; i++) {
  22. wasm_importtype_t *importtype = importtypes.data[i];
  23. /* use wasm_extern_new_empty() to create a placeholder */
  24. if (wasm_importtype_is_linked(importtype)) {
  25. externs[i] = wasm_extern_new_empty(
  26. store, wasm_externtype_kind(wasm_importtype_type(importtype)));
  27. continue;
  28. }
  29. const wasm_name_t *module_name =
  30. wasm_importtype_module(importtypes.data[i]);
  31. const wasm_name_t *field_name =
  32. wasm_importtype_name(importtypes.data[i]);
  33. if (strncmp(module_name->data, "env", strlen("env")) == 0
  34. && strncmp(field_name->data, "log", strlen("log")) == 0) {
  35. wasm_functype_t *log_type = wasm_functype_new_2_0(
  36. wasm_valtype_new_i64(), wasm_valtype_new_i32());
  37. wasm_func_t *log_func = wasm_func_new(store, log_type, host_logs);
  38. wasm_functype_delete(log_type);
  39. externs[i] = wasm_func_as_extern(log_func);
  40. }
  41. }
  42. wasm_extern_vec_new(out, importtypes.num_elems, externs);
  43. wasm_importtype_vec_delete(&importtypes);
  44. return true;
  45. }
  46. int
  47. main()
  48. {
  49. int main_ret = EXIT_FAILURE;
  50. // Initialize.
  51. printf("Initializing...\n");
  52. wasm_engine_t *engine = wasm_engine_new();
  53. if (!engine)
  54. goto quit;
  55. wasm_store_t *store = wasm_store_new(engine);
  56. if (!store)
  57. goto delete_engine;
  58. // Load binary.
  59. printf("Loading binary...\n");
  60. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
  61. FILE *file = fopen("send_recv.aot", "rb");
  62. printf("> Load .aot\n");
  63. #else
  64. FILE *file = fopen("send_recv.wasm", "rb");
  65. printf("> Load .wasm\n");
  66. #endif
  67. if (!file) {
  68. printf("> Error loading module!\n");
  69. goto delete_store;
  70. }
  71. int ret = fseek(file, 0L, SEEK_END);
  72. if (ret == -1) {
  73. printf("> Error loading module!\n");
  74. goto close_file;
  75. }
  76. long file_size = ftell(file);
  77. if (file_size == -1) {
  78. printf("> Error loading module!\n");
  79. goto close_file;
  80. }
  81. ret = fseek(file, 0L, SEEK_SET);
  82. if (ret == -1) {
  83. printf("> Error loading module!\n");
  84. goto close_file;
  85. }
  86. wasm_byte_vec_t binary;
  87. wasm_byte_vec_new_uninitialized(&binary, file_size);
  88. if (fread(binary.data, file_size, 1, file) != 1) {
  89. printf("> Error loading module!\n");
  90. goto delete_binary;
  91. }
  92. // Compile.
  93. printf("Compiling module...\n");
  94. wasm_module_t *module = wasm_module_new(store, &binary);
  95. if (!module) {
  96. printf("> Error compiling module!\n");
  97. goto delete_binary;
  98. }
  99. // Set Wasi Context
  100. const char *addr_pool[1] = { "127.0.0.1" };
  101. wasm_runtime_set_wasi_addr_pool(*module, addr_pool, 1);
  102. // Instantiate.
  103. printf("Instantiating module...\n");
  104. wasm_extern_vec_t imports = { 0 };
  105. ret = build_imports(store, module, &imports);
  106. if (!ret) {
  107. printf("> Error building imports!\n");
  108. goto delete_module;
  109. }
  110. wasm_instance_t *instance =
  111. wasm_instance_new(store, module, &imports, NULL);
  112. if (!instance) {
  113. printf("> Error instantiating module!\n");
  114. goto delete_imports;
  115. }
  116. // Extract export.
  117. printf("Extracting export...\n");
  118. wasm_extern_vec_t exports;
  119. wasm_instance_exports(instance, &exports);
  120. if (exports.size == 0) {
  121. printf("> Error accessing exports!\n");
  122. goto delete_instance;
  123. }
  124. /**
  125. * should use information from wasm_module_exports to avoid hard coding "1"
  126. */
  127. const wasm_func_t *start_func = wasm_extern_as_func(exports.data[1]);
  128. if (start_func == NULL) {
  129. printf("> Error accessing export!\n");
  130. goto delete_exports;
  131. }
  132. // Call. "_start(nil) -> i32"
  133. printf("Calling _start ...\n");
  134. wasm_val_t rs[1] = { WASM_I32_VAL(0) };
  135. wasm_val_vec_t args = WASM_EMPTY_VEC;
  136. wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
  137. wasm_trap_t *trap = wasm_func_call(start_func, &args, &results);
  138. if (trap) {
  139. wasm_name_t message = { 0 };
  140. wasm_trap_message(trap, &message);
  141. printf("> Error calling function! %s\n", message.data);
  142. wasm_name_delete(&message);
  143. wasm_trap_delete(trap);
  144. goto delete_exports;
  145. }
  146. // Print result.
  147. printf("Printing result...\n");
  148. printf("> %u\n", rs[0].of.i32);
  149. // Shut down.
  150. printf("Shutting down...\n");
  151. // All done.
  152. printf("Done.\n");
  153. main_ret = EXIT_SUCCESS;
  154. delete_exports:
  155. wasm_extern_vec_delete(&exports);
  156. delete_instance:
  157. wasm_instance_delete(instance);
  158. delete_imports:
  159. wasm_extern_vec_delete(&imports);
  160. delete_module:
  161. wasm_module_delete(module);
  162. delete_binary:
  163. wasm_byte_vec_delete(&binary);
  164. close_file:
  165. fclose(file);
  166. delete_store:
  167. wasm_store_delete(store);
  168. delete_engine:
  169. wasm_engine_delete(engine);
  170. quit:
  171. return main_ret;
  172. }