hello.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_platform.h"
  6. #include "bh_read_file.h"
  7. #include "wasm_export.h"
  8. #define USE_GLOBAL_HEAP_BUF 0
  9. #if USE_GLOBAL_HEAP_BUF != 0
  10. static char global_heap_buf[10 * 1024 * 1024] = { 0 };
  11. #endif
  12. static int
  13. test_write_wrapper(wasm_exec_env_t exec_env,
  14. uint32 externref_idx_of_file,
  15. const char *str, int len)
  16. {
  17. FILE *file;
  18. char buf[16];
  19. printf("## retrieve file handle from externref index\n");
  20. if (!wasm_externref_ref2obj(externref_idx_of_file, (void **)&file)) {
  21. printf("failed to get host object from externref index!\n");
  22. return -1;
  23. }
  24. snprintf(buf, sizeof(buf), "%%%ds", len);
  25. printf("## write string to file: ");
  26. printf(buf, str);
  27. return fprintf(file, buf, str);
  28. }
  29. static NativeSymbol native_symbols[] = {
  30. { "test_write", test_write_wrapper, "(i*~)i", NULL }
  31. };
  32. int
  33. main(int argc, char *argv[])
  34. {
  35. char *wasm_file = "hello.wasm";
  36. uint8 *wasm_file_buf = NULL;
  37. uint32 wasm_file_size, externref_idx;
  38. uint32 stack_size = 16 * 1024, heap_size = 16 * 1024;
  39. wasm_module_t wasm_module = NULL;
  40. wasm_module_inst_t wasm_module_inst = NULL;
  41. wasm_function_inst_t func_inst = NULL;
  42. wasm_exec_env_t exec_env = NULL;
  43. RuntimeInitArgs init_args;
  44. char error_buf[128] = { 0 };
  45. const char *exce;
  46. unsigned argv1[8];
  47. #if WASM_ENABLE_LOG != 0
  48. int log_verbose_level = 2;
  49. #endif
  50. FILE *file;
  51. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  52. #if USE_GLOBAL_HEAP_BUF != 0
  53. init_args.mem_alloc_type = Alloc_With_Pool;
  54. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  55. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  56. #else
  57. init_args.mem_alloc_type = Alloc_With_Allocator;
  58. init_args.mem_alloc_option.allocator.malloc_func = malloc;
  59. init_args.mem_alloc_option.allocator.realloc_func = realloc;
  60. init_args.mem_alloc_option.allocator.free_func = free;
  61. #endif
  62. init_args.n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
  63. init_args.native_module_name = "env";
  64. init_args.native_symbols = native_symbols;
  65. /* initialize runtime environment */
  66. if (!wasm_runtime_full_init(&init_args)) {
  67. printf("Init runtime environment failed.\n");
  68. return -1;
  69. }
  70. #if WASM_ENABLE_LOG != 0
  71. bh_log_set_verbose_level(log_verbose_level);
  72. #endif
  73. /* load WASM byte buffer from WASM bin file */
  74. if (!(wasm_file_buf =
  75. (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size)))
  76. goto fail1;
  77. /* load WASM module */
  78. if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
  79. error_buf, sizeof(error_buf)))) {
  80. printf("%s\n", error_buf);
  81. goto fail2;
  82. }
  83. /* instantiate the module */
  84. if (!(wasm_module_inst =
  85. wasm_runtime_instantiate(wasm_module, stack_size, heap_size,
  86. error_buf, sizeof(error_buf)))) {
  87. printf("%s\n", error_buf);
  88. goto fail3;
  89. }
  90. /* lookup function instance */
  91. if (!(func_inst = wasm_runtime_lookup_function(wasm_module_inst,
  92. "test", NULL))) {
  93. printf("%s\n", "lookup function test failed");
  94. goto fail4;
  95. }
  96. if (!(exec_env =
  97. wasm_runtime_create_exec_env(wasm_module_inst, stack_size))) {
  98. printf("%s\n", "create exec env failed");
  99. goto fail4;
  100. }
  101. printf("## open file test.txt\n");
  102. if (!(file = fopen("test.txt", "wb+"))) {
  103. printf("%s\n", "open file text.txt failed");
  104. goto fail5;
  105. }
  106. printf("## map file handle to externref index\n");
  107. if (!wasm_externref_obj2ref(wasm_module_inst, file, &externref_idx)) {
  108. printf("%s\n", "map host object to externref index failed");
  109. goto fail6;
  110. }
  111. printf("## call wasm function with externref index\n");
  112. argv1[0] = externref_idx;
  113. wasm_runtime_call_wasm(exec_env, func_inst, 1, argv1);
  114. if ((exce = wasm_runtime_get_exception(wasm_module_inst))) {
  115. printf("Exception: %s\n", exce);
  116. }
  117. fail6:
  118. fclose(file);
  119. fail5:
  120. /* destroy exec env */
  121. wasm_runtime_destroy_exec_env(exec_env);
  122. fail4:
  123. /* destroy the module instance */
  124. wasm_runtime_deinstantiate(wasm_module_inst);
  125. fail3:
  126. /* unload the module */
  127. wasm_runtime_unload(wasm_module);
  128. fail2:
  129. /* free the file buffer */
  130. wasm_runtime_free(wasm_file_buf);
  131. fail1:
  132. /* destroy runtime environment */
  133. wasm_runtime_destroy();
  134. return 0;
  135. }