wasm-jni.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <cstring>
  2. #include <stdlib.h>
  3. #include <jni.h>
  4. #include <cinttypes>
  5. #include <android/log.h>
  6. #include <bh_log.h>
  7. #include <string>
  8. #include <bh_platform.h>
  9. #include <wasm_export.h>
  10. #define LOGI(...) \
  11. ((void)__android_log_print(ANDROID_LOG_INFO, "wasm_jni::", __VA_ARGS__))
  12. static void *
  13. app_instance_main(wasm_module_inst_t module_inst) {
  14. const char *exception;
  15. wasm_application_execute_main(module_inst, 0, NULL);
  16. if ((exception = wasm_runtime_get_exception(module_inst)))
  17. LOGI("%s\n", exception);
  18. return NULL;
  19. }
  20. // WARNING! CAN NOT BE READ ONLY!!!
  21. static unsigned char wasm_test_file[] = { 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00,
  22. 0x00, 0x00, 0x0D, 0x06, 0x64, 0x79, 0x6C, 0x69, 0x6E, 0x6B, 0xC0, 0x80,
  23. 0x04, 0x04, 0x00, 0x00, 0x01, 0x13, 0x04, 0x60, 0x01, 0x7F, 0x00, 0x60,
  24. 0x01, 0x7F, 0x01, 0x7F, 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F, 0x60, 0x00,
  25. 0x00, 0x02, 0x58, 0x06, 0x03, 0x65, 0x6E, 0x76, 0x05, 0x5F, 0x66, 0x72,
  26. 0x65, 0x65, 0x00, 0x00, 0x03, 0x65, 0x6E, 0x76, 0x07, 0x5F, 0x6D, 0x61,
  27. 0x6C, 0x6C, 0x6F, 0x63, 0x00, 0x01, 0x03, 0x65, 0x6E, 0x76, 0x07, 0x5F,
  28. 0x70, 0x72, 0x69, 0x6E, 0x74, 0x66, 0x00, 0x02, 0x03, 0x65, 0x6E, 0x76,
  29. 0x05, 0x5F, 0x70, 0x75, 0x74, 0x73, 0x00, 0x01, 0x03, 0x65, 0x6E, 0x76,
  30. 0x0D, 0x5F, 0x5F, 0x6D, 0x65, 0x6D, 0x6F, 0x72, 0x79, 0x5F, 0x62, 0x61,
  31. 0x73, 0x65, 0x03, 0x7F, 0x00, 0x03, 0x65, 0x6E, 0x76, 0x06, 0x6D, 0x65,
  32. 0x6D, 0x6F, 0x72, 0x79, 0x02, 0x00, 0x01, 0x03, 0x04, 0x03, 0x02, 0x03,
  33. 0x03, 0x06, 0x10, 0x03, 0x7F, 0x01, 0x41, 0x00, 0x0B, 0x7F, 0x01, 0x41,
  34. 0x00, 0x0B, 0x7F, 0x00, 0x41, 0x1B, 0x0B, 0x07, 0x33, 0x04, 0x12, 0x5F,
  35. 0x5F, 0x70, 0x6F, 0x73, 0x74, 0x5F, 0x69, 0x6E, 0x73, 0x74, 0x61, 0x6E,
  36. 0x74, 0x69, 0x61, 0x74, 0x65, 0x00, 0x06, 0x05, 0x5F, 0x6D, 0x61, 0x69,
  37. 0x6E, 0x00, 0x04, 0x0B, 0x72, 0x75, 0x6E, 0x50, 0x6F, 0x73, 0x74, 0x53,
  38. 0x65, 0x74, 0x73, 0x00, 0x05, 0x04, 0x5F, 0x73, 0x74, 0x72, 0x03, 0x03,
  39. 0x0A, 0xBA, 0x01, 0x03, 0x9E, 0x01, 0x01, 0x01, 0x7F, 0x23, 0x01, 0x21,
  40. 0x00, 0x23, 0x01, 0x41, 0x10, 0x6A, 0x24, 0x01, 0x20, 0x00, 0x41, 0x08,
  41. 0x6A, 0x21, 0x02, 0x23, 0x00, 0x41, 0x1B, 0x6A, 0x10, 0x03, 0x1A, 0x41,
  42. 0x80, 0x08, 0x10, 0x01, 0x21, 0x01, 0x20, 0x01, 0x04, 0x7F, 0x20, 0x00,
  43. 0x20, 0x01, 0x36, 0x02, 0x00, 0x23, 0x00, 0x20, 0x00, 0x10, 0x02, 0x1A,
  44. 0x20, 0x01, 0x23, 0x00, 0x2C, 0x00, 0x0D, 0x3A, 0x00, 0x00, 0x20, 0x01,
  45. 0x23, 0x00, 0x2C, 0x00, 0x0E, 0x3A, 0x00, 0x01, 0x20, 0x01, 0x23, 0x00,
  46. 0x2C, 0x00, 0x0F, 0x3A, 0x00, 0x02, 0x20, 0x01, 0x23, 0x00, 0x2C, 0x00,
  47. 0x10, 0x3A, 0x00, 0x03, 0x20, 0x01, 0x23, 0x00, 0x2C, 0x00, 0x11, 0x3A,
  48. 0x00, 0x04, 0x20, 0x01, 0x23, 0x00, 0x2C, 0x00, 0x12, 0x3A, 0x00, 0x05,
  49. 0x20, 0x02, 0x20, 0x01, 0x36, 0x02, 0x00, 0x23, 0x00, 0x41, 0x13, 0x6A,
  50. 0x20, 0x02, 0x10, 0x02, 0x1A, 0x20, 0x01, 0x10, 0x00, 0x20, 0x00, 0x24,
  51. 0x01, 0x41, 0x00, 0x05, 0x23, 0x00, 0x41, 0x28, 0x6A, 0x10, 0x03, 0x1A,
  52. 0x20, 0x00, 0x24, 0x01, 0x41, 0x7F, 0x0B, 0x0B, 0x03, 0x00, 0x01, 0x0B,
  53. 0x14, 0x00, 0x23, 0x00, 0x41, 0x40, 0x6B, 0x24, 0x01, 0x23, 0x01, 0x41,
  54. 0x80, 0x80, 0x04, 0x6A, 0x24, 0x02, 0x10, 0x05, 0x0B, 0x0B, 0x3F, 0x01,
  55. 0x00, 0x23, 0x00, 0x0B, 0x39, 0x62, 0x75, 0x66, 0x20, 0x70, 0x74, 0x72,
  56. 0x3A, 0x20, 0x25, 0x70, 0x0A, 0x00, 0x31, 0x32, 0x33, 0x34, 0x0A, 0x00,
  57. 0x62, 0x75, 0x66, 0x3A, 0x20, 0x25, 0x73, 0x00, 0x48, 0x65, 0x6C, 0x6C,
  58. 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21, 0x00, 0x6D, 0x61, 0x6C,
  59. 0x6C, 0x6F, 0x63, 0x20, 0x62, 0x75, 0x66, 0x20, 0x66, 0x61, 0x69, 0x6C,
  60. 0x65, 0x64, 0x00, 0x50, 0x04, 0x6E, 0x61, 0x6D, 0x65, 0x01, 0x49, 0x07,
  61. 0x00, 0x05, 0x5F, 0x66, 0x72, 0x65, 0x65, 0x01, 0x07, 0x5F, 0x6D, 0x61,
  62. 0x6C, 0x6C, 0x6F, 0x63, 0x02, 0x07, 0x5F, 0x70, 0x72, 0x69, 0x6E, 0x74,
  63. 0x66, 0x03, 0x05, 0x5F, 0x70, 0x75, 0x74, 0x73, 0x04, 0x05, 0x5F, 0x6D,
  64. 0x61, 0x69, 0x6E, 0x05, 0x0B, 0x72, 0x75, 0x6E, 0x50, 0x6F, 0x73, 0x74,
  65. 0x53, 0x65, 0x74, 0x73, 0x06, 0x12, 0x5F, 0x5F, 0x70, 0x6F, 0x73, 0x74,
  66. 0x5F, 0x69, 0x6E, 0x73, 0x74, 0x61, 0x6E, 0x74, 0x69, 0x61, 0x74, 0x65,
  67. 0x00, 0x20, 0x10, 0x73, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x4D, 0x61, 0x70,
  68. 0x70, 0x69, 0x6E, 0x67, 0x55, 0x52, 0x4C, 0x0E, 0x61, 0x2E, 0x6F, 0x75,
  69. 0x74, 0x2E, 0x77, 0x61, 0x73, 0x6D, 0x2E, 0x6D, 0x61, 0x70 };
  70. extern "C" JNIEXPORT void JNICALL
  71. Java_com_intel_wasm_api_Runtime_run(JNIEnv *env, jclass thiz) {
  72. wasm_module_t wasm_module = NULL;
  73. wasm_module_inst_t wasm_module_inst = NULL;
  74. RuntimeInitArgs init_args;
  75. uint wasm_file_size = 0;
  76. uint8_t *wasm_file_buf = NULL;
  77. char error_buf[128] = {0};
  78. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  79. init_args.mem_alloc_type = Alloc_With_Allocator;
  80. init_args.mem_alloc_option.allocator.malloc_func = (void*)malloc;
  81. init_args.mem_alloc_option.allocator.realloc_func = (void*)realloc;
  82. init_args.mem_alloc_option.allocator.free_func = (void*)free;
  83. LOGI("wasm_runtime_full_init");
  84. /* initialize runtime environment */
  85. if (!wasm_runtime_full_init(&init_args)) {
  86. LOGI("Init runtime failed.\n");
  87. return;
  88. }
  89. // set log level to INFO
  90. LOGI("set log level to INFO");
  91. bh_log_set_verbose_level(3);
  92. /* load WASM byte buffer from a preinstall WASM bin file */
  93. LOGI("use an internal test file, gona to output Hello World in logcat\n");
  94. wasm_file_buf = (uint8_t*) wasm_test_file;
  95. wasm_file_size = sizeof(wasm_test_file);
  96. /* load WASM module */
  97. LOGI("wasm_runtime_load");
  98. if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
  99. error_buf, sizeof(error_buf)))) {
  100. LOGI("in wasm_runtime_load %s\n", error_buf);
  101. LOGI("goto fail1\n");
  102. goto fail1;
  103. }
  104. /* instantiate the module */
  105. LOGI("wasm_runtime_instantiate");
  106. if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module,
  107. 64 * 1024, /* stack size */
  108. 64 * 1024, /* heap size */
  109. error_buf,
  110. sizeof(error_buf)))) {
  111. LOGI("%s\n", error_buf);
  112. LOGI("goto fail2\n");
  113. goto fail2;
  114. }
  115. LOGI("run main() of the application");
  116. app_instance_main(wasm_module_inst);
  117. /* destroy the module instance */
  118. LOGI("wasm_runtime_deinstantiate");
  119. wasm_runtime_deinstantiate(wasm_module_inst);
  120. fail2:
  121. /* unload the module */
  122. LOGI("wasm_runtime_unload");
  123. wasm_runtime_unload(wasm_module);
  124. fail1:
  125. // in our case, we don't need a free, but it is not a typical one
  126. /* free the file buffer */
  127. //bh_free((void *) wasm_file_buf);
  128. /* destroy runtime environment */
  129. LOGI("wasm_runtime_destroy");
  130. wasm_runtime_destroy();
  131. return;
  132. }