main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_export.h"
  6. #include "bh_read_file.h"
  7. #include "bh_getopt.h"
  8. #include "my_context.h"
  9. void
  10. set_context(wasm_exec_env_t exec_env, int32_t n);
  11. int32_t
  12. get_context(wasm_exec_env_t exec_env);
  13. void *my_context_key;
  14. struct my_context my_context;
  15. int my_dtor_called;
  16. wasm_module_inst_t module_inst = NULL;
  17. void
  18. print_usage(void)
  19. {
  20. fprintf(stdout, "Options:\r\n");
  21. fprintf(stdout, " -f [path of wasm file] \n");
  22. }
  23. void
  24. my_context_dtor(wasm_module_inst_t inst, void *ctx)
  25. {
  26. printf("%s called\n", __func__);
  27. my_dtor_called++;
  28. bh_assert(ctx == &my_context);
  29. bh_assert(inst == module_inst);
  30. }
  31. int
  32. main(int argc, char *argv_main[])
  33. {
  34. static char global_heap_buf[512 * 1024];
  35. char *buffer;
  36. char error_buf[128];
  37. int opt;
  38. char *wasm_path = NULL;
  39. int exit_code = 1;
  40. wasm_module_t module = NULL;
  41. uint32 buf_size, stack_size = 8092, heap_size = 8092;
  42. RuntimeInitArgs init_args;
  43. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  44. while ((opt = getopt(argc, argv_main, "hf:")) != -1) {
  45. switch (opt) {
  46. case 'f':
  47. wasm_path = optarg;
  48. break;
  49. case 'h':
  50. print_usage();
  51. return 0;
  52. case '?':
  53. print_usage();
  54. return 0;
  55. }
  56. }
  57. if (optind == 1) {
  58. print_usage();
  59. return 0;
  60. }
  61. // Define an array of NativeSymbol for the APIs to be exported.
  62. // Note: the array must be static defined since runtime
  63. // will keep it after registration
  64. // For the function signature specifications, goto the link:
  65. // https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md
  66. static NativeSymbol native_symbols[] = {
  67. { "set_context", set_context, "(i)", NULL },
  68. { "get_context", get_context, "()i", NULL },
  69. };
  70. init_args.mem_alloc_type = Alloc_With_Pool;
  71. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  72. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  73. // Native symbols need below registration phase
  74. init_args.n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
  75. init_args.native_module_name = "env";
  76. init_args.native_symbols = native_symbols;
  77. if (!wasm_runtime_full_init(&init_args)) {
  78. printf("Init runtime environment failed.\n");
  79. return -1;
  80. }
  81. my_context_key = wasm_runtime_create_context_key(my_context_dtor);
  82. if (!my_context_key) {
  83. printf("wasm_runtime_create_context_key failed.\n");
  84. return -1;
  85. }
  86. buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
  87. if (!buffer) {
  88. printf("Open wasm app file [%s] failed.\n", wasm_path);
  89. goto fail;
  90. }
  91. module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf,
  92. sizeof(error_buf));
  93. if (!module) {
  94. printf("Load wasm module failed. error: %s\n", error_buf);
  95. goto fail;
  96. }
  97. module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
  98. error_buf, sizeof(error_buf));
  99. if (!module_inst) {
  100. printf("Instantiate wasm module failed. error: %s\n", error_buf);
  101. goto fail;
  102. }
  103. char *args[] = {
  104. "testapp",
  105. };
  106. wasm_application_execute_main(module_inst, 1, args);
  107. const char *exc = wasm_runtime_get_exception(module_inst);
  108. if (exc != NULL) {
  109. printf("call wasm function calculate failed. error: %s\n", exc);
  110. goto fail;
  111. }
  112. exit_code = 0;
  113. fail:
  114. if (module_inst) {
  115. bh_assert(my_dtor_called == 0);
  116. wasm_runtime_deinstantiate(module_inst);
  117. bh_assert(my_dtor_called == 1);
  118. }
  119. if (module)
  120. wasm_runtime_unload(module);
  121. if (buffer)
  122. BH_FREE(buffer);
  123. if (my_context_key)
  124. wasm_runtime_destroy_context_key(my_context_key);
  125. wasm_runtime_destroy();
  126. return exit_code;
  127. }