main.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (C) 2019-21 Intel Corporation and others. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdio.h>
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "wasm_export.h"
  9. #include "bh_platform.h"
  10. #include "test_wasm.h"
  11. #include "esp_log.h"
  12. #define IWASM_MAIN_STACK_SIZE 5120
  13. #define LOG_TAG "wamr"
  14. static void *
  15. app_instance_main(wasm_module_inst_t module_inst)
  16. {
  17. const char *exception;
  18. wasm_application_execute_main(module_inst, 0, NULL);
  19. if ((exception = wasm_runtime_get_exception(module_inst)))
  20. printf("%s\n", exception);
  21. return NULL;
  22. }
  23. void *
  24. iwasm_main(void *arg)
  25. {
  26. (void)arg; /* unused */
  27. /* setup variables for instantiating and running the wasm module */
  28. uint8_t *wasm_file_buf = NULL;
  29. unsigned wasm_file_buf_size = 0;
  30. wasm_module_t wasm_module = NULL;
  31. wasm_module_inst_t wasm_module_inst = NULL;
  32. char error_buf[128];
  33. void *ret;
  34. RuntimeInitArgs init_args;
  35. /* configure memory allocation */
  36. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  37. #if WASM_ENABLE_GLOBAL_HEAP_POOL == 0
  38. init_args.mem_alloc_type = Alloc_With_Allocator;
  39. init_args.mem_alloc_option.allocator.malloc_func = (void *)os_malloc;
  40. init_args.mem_alloc_option.allocator.realloc_func = (void *)os_realloc;
  41. init_args.mem_alloc_option.allocator.free_func = (void *)os_free;
  42. #else
  43. #error The usage of a global heap pool is not implemented yet for esp-idf.
  44. #endif
  45. ESP_LOGI(LOG_TAG, "Initialize WASM runtime");
  46. /* initialize runtime environment */
  47. if (!wasm_runtime_full_init(&init_args)) {
  48. ESP_LOGE(LOG_TAG, "Init runtime failed.");
  49. return NULL;
  50. }
  51. #if WASM_ENABLE_INTERP != 0
  52. ESP_LOGI(LOG_TAG, "Run wamr with interpreter");
  53. wasm_file_buf = (uint8_t *)wasm_test_file_interp;
  54. wasm_file_buf_size = sizeof(wasm_test_file_interp);
  55. /* load WASM module */
  56. if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_buf_size,
  57. error_buf, sizeof(error_buf)))) {
  58. ESP_LOGE(LOG_TAG, "Error in wasm_runtime_load: %s", error_buf);
  59. goto fail1interp;
  60. }
  61. ESP_LOGI(LOG_TAG, "Instantiate WASM runtime");
  62. if (!(wasm_module_inst =
  63. wasm_runtime_instantiate(wasm_module, 32 * 1024, // stack size
  64. 32 * 1024, // heap size
  65. error_buf, sizeof(error_buf)))) {
  66. ESP_LOGE(LOG_TAG, "Error while instantiating: %s", error_buf);
  67. goto fail2interp;
  68. }
  69. ESP_LOGI(LOG_TAG, "run main() of the application");
  70. ret = app_instance_main(wasm_module_inst);
  71. assert(!ret);
  72. /* destroy the module instance */
  73. ESP_LOGI(LOG_TAG, "Deinstantiate WASM runtime");
  74. wasm_runtime_deinstantiate(wasm_module_inst);
  75. fail2interp:
  76. /* unload the module */
  77. ESP_LOGI(LOG_TAG, "Unload WASM module");
  78. wasm_runtime_unload(wasm_module);
  79. fail1interp:
  80. #endif
  81. #if WASM_ENABLE_AOT != 0
  82. ESP_LOGI(LOG_TAG, "Run wamr with AoT");
  83. wasm_file_buf = (uint8_t *)wasm_test_file_aot;
  84. wasm_file_buf_size = sizeof(wasm_test_file_aot);
  85. /* load WASM module */
  86. if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_buf_size,
  87. error_buf, sizeof(error_buf)))) {
  88. ESP_LOGE(LOG_TAG, "Error in wasm_runtime_load: %s", error_buf);
  89. goto fail1aot;
  90. }
  91. ESP_LOGI(LOG_TAG, "Instantiate WASM runtime");
  92. if (!(wasm_module_inst =
  93. wasm_runtime_instantiate(wasm_module, 32 * 1024, // stack size
  94. 32 * 1024, // heap size
  95. error_buf, sizeof(error_buf)))) {
  96. ESP_LOGE(LOG_TAG, "Error while instantiating: %s", error_buf);
  97. goto fail2aot;
  98. }
  99. ESP_LOGI(LOG_TAG, "run main() of the application");
  100. ret = app_instance_main(wasm_module_inst);
  101. assert(!ret);
  102. /* destroy the module instance */
  103. ESP_LOGI(LOG_TAG, "Deinstantiate WASM runtime");
  104. wasm_runtime_deinstantiate(wasm_module_inst);
  105. fail2aot:
  106. /* unload the module */
  107. ESP_LOGI(LOG_TAG, "Unload WASM module");
  108. wasm_runtime_unload(wasm_module);
  109. fail1aot:
  110. #endif
  111. /* destroy runtime environment */
  112. ESP_LOGI(LOG_TAG, "Destroy WASM runtime");
  113. wasm_runtime_destroy();
  114. return NULL;
  115. }
  116. void
  117. app_main(void)
  118. {
  119. pthread_t t;
  120. int res;
  121. pthread_attr_t tattr;
  122. pthread_attr_init(&tattr);
  123. pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE);
  124. pthread_attr_setstacksize(&tattr, IWASM_MAIN_STACK_SIZE);
  125. res = pthread_create(&t, &tattr, iwasm_main, (void *)NULL);
  126. assert(res == 0);
  127. res = pthread_join(t, NULL);
  128. assert(res == 0);
  129. ESP_LOGI(LOG_TAG, "Exiting...");
  130. }