iwasmt.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * Copyright (C) 2020 TU Bergakademie Freiberg Karl Fessel
  4. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5. */
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include "wasm_export.h"
  11. #include <thread.h>
  12. /* provide some test program */
  13. #include "test_wasm.h"
  14. #define DEFAULT_THREAD_STACKSIZE (6 * 1024)
  15. #define DEFAULT_THREAD_PRIORITY 50
  16. static int app_argc;
  17. static char **app_argv;
  18. static void *
  19. app_instance_main(wasm_module_inst_t module_inst)
  20. {
  21. const char *exception;
  22. wasm_application_execute_main(module_inst, app_argc, app_argv);
  23. if ((exception = wasm_runtime_get_exception(module_inst))) {
  24. puts(exception);
  25. }
  26. return NULL;
  27. }
  28. void *
  29. iwasm_t(void *arg1)
  30. {
  31. wasm_module_t wasm_module = (wasm_module_t)arg1;
  32. wasm_module_inst_t wasm_module_inst = NULL;
  33. char error_buf[128];
  34. /* instantiate the module */
  35. if (!(wasm_module_inst = wasm_runtime_instantiate(
  36. wasm_module, 8 * 1024, 8 * 1024, error_buf, sizeof(error_buf)))) {
  37. puts(error_buf);
  38. }
  39. else {
  40. app_instance_main(wasm_module_inst);
  41. /* destroy the module instance */
  42. wasm_runtime_deinstantiate(wasm_module_inst);
  43. }
  44. return NULL;
  45. }
  46. /* enable FUNC_ALLOC to use custom memory allocation functions */
  47. #define FUNC_ALLOC
  48. void *
  49. iwasm_main(void *arg1)
  50. {
  51. (void)arg1; /* unused */
  52. uint8_t *wasm_file_buf = NULL;
  53. unsigned wasm_file_buf_size = 0;
  54. wasm_module_t wasm_module = NULL;
  55. char error_buf[128];
  56. RuntimeInitArgs init_args;
  57. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  58. #if defined(FUNC_ALLOC) && WASM_ENABLE_GLOBAL_HEAP_POOL == 0
  59. init_args.mem_alloc_type = Alloc_With_Allocator;
  60. init_args.mem_alloc_option.allocator.malloc_func = malloc;
  61. init_args.mem_alloc_option.allocator.realloc_func = realloc;
  62. init_args.mem_alloc_option.allocator.free_func = free;
  63. #elif WASM_ENABLE_GLOBAL_HEAP_POOL != 0
  64. static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
  65. init_args.mem_alloc_type = Alloc_With_Pool;
  66. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  67. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  68. #else
  69. init_args.mem_alloc_type = Alloc_With_System_Allocator;
  70. #endif
  71. /* initialize runtime environment */
  72. if (!wasm_runtime_full_init(&init_args)) {
  73. puts("Init runtime environment failed.");
  74. return NULL;
  75. }
  76. /* load WASM byte buffer from byte buffer of include file */
  77. wasm_file_buf = (uint8_t *)wasm_test_file;
  78. wasm_file_buf_size = sizeof(wasm_test_file);
  79. /* load WASM module */
  80. if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_buf_size,
  81. error_buf, sizeof(error_buf)))) {
  82. puts(error_buf);
  83. }
  84. else {
  85. iwasm_t(wasm_module);
  86. wasm_runtime_unload(wasm_module);
  87. }
  88. wasm_runtime_destroy();
  89. return NULL;
  90. }
  91. bool
  92. iwasm_init(void)
  93. {
  94. /* clang-format off */
  95. struct {
  96. char *stack;
  97. int stacksize;
  98. uint8_t priority;
  99. int flags;
  100. thread_task_func_t task_func;
  101. void *arg;
  102. const char *name;
  103. } b = {
  104. .stacksize = DEFAULT_THREAD_STACKSIZE,
  105. .priority = 8,
  106. .flags = 0,
  107. .task_func = iwasm_main,
  108. .arg = NULL,
  109. .name = "simple_wamr"
  110. };
  111. /* clang-format on */
  112. b.stack = malloc(b.stacksize);
  113. kernel_pid_t tpid = thread_create(b.stack, b.stacksize, b.priority, b.flags,
  114. b.task_func, b.arg, b.name);
  115. return tpid != 0 ? true : false;
  116. ;
  117. }
  118. #define telltruth(X) ((X) ? "true" : "false")
  119. int
  120. main(void)
  121. {
  122. printf("iwasm_initilised: %s\n", telltruth(iwasm_init()));
  123. }