main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "bh_read_file.h"
  5. #include "platform_common.h"
  6. #include "wasm_export.h"
  7. #if WASM_ENABLE_MULTI_MODULE != 0
  8. static char *module_search_path = ".";
  9. static bool
  10. module_reader_callback(package_type_t module_type, const char *module_name,
  11. uint8 **p_buffer, uint32 *p_size)
  12. {
  13. char *file_format = NULL;
  14. #if WASM_ENABLE_INTERP != 0
  15. if (module_type == Wasm_Module_Bytecode)
  16. file_format = ".wasm";
  17. #endif
  18. #if WASM_ENABLE_AOT != 0
  19. if (module_type == Wasm_Module_AoT)
  20. file_format = ".aot";
  21. #endif
  22. bh_assert(file_format != NULL);
  23. const char *format = "%s/%s%s";
  24. int sz = strlen(module_search_path) + strlen("/") + strlen(module_name)
  25. + strlen(file_format) + 1;
  26. char *wasm_file_name = wasm_runtime_malloc(sz);
  27. if (!wasm_file_name) {
  28. return false;
  29. }
  30. snprintf(wasm_file_name, sz, format, module_search_path, module_name,
  31. file_format);
  32. *p_buffer = (uint8_t *)bh_read_file_to_buffer(wasm_file_name, p_size);
  33. wasm_runtime_free(wasm_file_name);
  34. return *p_buffer != NULL;
  35. }
  36. static void
  37. module_destroyer_callback(uint8 *buffer, uint32 size)
  38. {
  39. if (!buffer) {
  40. return;
  41. }
  42. wasm_runtime_free(buffer);
  43. buffer = NULL;
  44. }
  45. #endif /* WASM_ENABLE_MULTI_MODULE */
  46. /* 10M */
  47. static char sandbox_memory_space[10 * 1024 * 1024] = { 0 };
  48. int
  49. main(int argc, char *argv[])
  50. {
  51. bool ret = false;
  52. if (argc != 2) {
  53. return -1;
  54. }
  55. char *wasm_file = argv[1];
  56. /* 16K */
  57. const uint32 stack_size = 16 * 1024;
  58. const uint32 heap_size = 16 * 1024;
  59. RuntimeInitArgs init_args = { 0 };
  60. char error_buf[128] = { 0 };
  61. /* parameters and return values */
  62. char *args[1] = { 0 };
  63. uint8 *file_buf = NULL;
  64. uint32 file_buf_size = 0;
  65. wasm_module_t module = NULL;
  66. wasm_module_t module1;
  67. wasm_module_inst_t module_inst = NULL;
  68. /* all malloc() only from the given buffer */
  69. init_args.mem_alloc_type = Alloc_With_Pool;
  70. init_args.mem_alloc_option.pool.heap_buf = sandbox_memory_space;
  71. init_args.mem_alloc_option.pool.heap_size = sizeof(sandbox_memory_space);
  72. printf("- wasm_runtime_full_init\n");
  73. /* initialize runtime environment */
  74. if (!wasm_runtime_full_init(&init_args)) {
  75. printf("Init runtime environment failed.\n");
  76. goto EXIT;
  77. }
  78. #if WASM_ENABLE_MULTI_MODULE != 0
  79. printf("- wasm_runtime_set_module_reader\n");
  80. /* set module reader and destroyer */
  81. wasm_runtime_set_module_reader(module_reader_callback,
  82. module_destroyer_callback);
  83. #endif
  84. /* load WASM byte buffer from WASM bin file */
  85. if (!(file_buf =
  86. (uint8 *)bh_read_file_to_buffer(wasm_file, &file_buf_size)))
  87. goto RELEASE_RUNTIME;
  88. /* load mC and let WAMR load mA and mB */
  89. printf("- wasm_runtime_load\n");
  90. if (!(module = wasm_runtime_load(file_buf, file_buf_size, error_buf,
  91. sizeof(error_buf)))) {
  92. printf("%s\n", error_buf);
  93. goto RELEASE_BINARY;
  94. }
  95. /* instantiate the module */
  96. printf("- wasm_runtime_instantiate\n");
  97. if (!(module_inst = wasm_runtime_instantiate(
  98. module, stack_size, heap_size, error_buf, sizeof(error_buf)))) {
  99. printf("%s\n", error_buf);
  100. goto UNLOAD_MODULE;
  101. }
  102. /* call functions of mC */
  103. printf("\n----------------------------------------\n");
  104. printf("call \"C1\", it will return 0x1f:i32, ===> ");
  105. wasm_application_execute_func(module_inst, "C1", 0, args);
  106. printf("call \"C2\", it will call B1() of mB and return 0x15:i32, ===> ");
  107. wasm_application_execute_func(module_inst, "C2", 0, args);
  108. printf("call \"C3\", it will call A1() of mA and return 0xb:i32, ===> ");
  109. wasm_application_execute_func(module_inst, "C3", 0, args);
  110. printf("call \"C4\", it will call B2() of mB and call A1() of mA and "
  111. "return 0xb:i32, ===> ");
  112. wasm_application_execute_func(module_inst, "C4", 0, args);
  113. printf(
  114. "call \"C5\", it will be failed since it is a export function, ===> ");
  115. wasm_application_execute_func(module_inst, "C5", 0, args);
  116. /* examine module registration a bit */
  117. module1 = wasm_runtime_find_module_registered("mC");
  118. if (module1 != NULL) {
  119. printf("unexpected module mC %p != NULL\n", module1);
  120. goto UNLOAD_MODULE;
  121. }
  122. module1 = wasm_runtime_find_module_registered("mA");
  123. if (module1 == NULL) {
  124. printf("unexpected module mA\n");
  125. goto UNLOAD_MODULE;
  126. }
  127. module1 = wasm_runtime_find_module_registered("mB");
  128. if (module1 == NULL) {
  129. printf("unexpected module mB\n");
  130. goto UNLOAD_MODULE;
  131. }
  132. if (!wasm_runtime_register_module("mC", module, error_buf,
  133. sizeof(error_buf))) {
  134. printf("%s\n", error_buf);
  135. goto UNLOAD_MODULE;
  136. }
  137. module1 = wasm_runtime_find_module_registered("mC");
  138. if (module1 != module) {
  139. printf("unexpected module mC %p != %p\n", module1, module);
  140. goto UNLOAD_MODULE;
  141. }
  142. ret = true;
  143. printf("- wasm_runtime_deinstantiate\n");
  144. wasm_runtime_deinstantiate(module_inst);
  145. UNLOAD_MODULE:
  146. printf("- wasm_runtime_unload\n");
  147. wasm_runtime_unload(module);
  148. RELEASE_BINARY:
  149. module_destroyer_callback(file_buf, file_buf_size);
  150. RELEASE_RUNTIME:
  151. printf("- wasm_runtime_destroy\n");
  152. wasm_runtime_destroy();
  153. EXIT:
  154. return ret ? 0 : 1;
  155. }