main.c 5.1 KB

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