Enclave.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "Enclave_t.h"
  8. #include "test_wasm.h"
  9. #include "bh_memory.h"
  10. #include "wasm_export.h"
  11. static char global_heap_buf[2* 1024 * 1024] = { 0 };
  12. static int app_argc;
  13. static char **app_argv;
  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, app_argc, app_argv);
  19. if ((exception = wasm_runtime_get_exception(module_inst))) {
  20. ocall_print(exception);
  21. ocall_print("\n");
  22. }
  23. return NULL;
  24. }
  25. extern "C" {
  26. int bh_printf(const char *message, ...);
  27. typedef void (*bh_print_function_t)(const char* message);
  28. extern void bh_set_print_function(bh_print_function_t pf);
  29. void enclave_print(const char *message)
  30. {
  31. ocall_print(message);
  32. }
  33. }
  34. void ecall_iwasm_main()
  35. {
  36. bh_set_print_function(enclave_print);
  37. uint8_t *wasm_file_buf = NULL;
  38. int wasm_file_size;
  39. wasm_module_t wasm_module = NULL;
  40. wasm_module_inst_t wasm_module_inst = NULL;
  41. char error_buf[128];
  42. if (bh_memory_init_with_pool(global_heap_buf,
  43. sizeof(global_heap_buf)) != 0) {
  44. ocall_print("Init global heap failed.\n");
  45. return;
  46. }
  47. /* initialize runtime environment */
  48. if (!wasm_runtime_init())
  49. goto fail1;
  50. /* load WASM byte buffer from byte buffer of include file */
  51. wasm_file_buf = (uint8_t*) wasm_test_file;
  52. wasm_file_size = sizeof(wasm_test_file);
  53. /* load WASM module */
  54. if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
  55. error_buf, sizeof(error_buf)))) {
  56. ocall_print(error_buf);
  57. ocall_print("\n");
  58. goto fail2;
  59. }
  60. /* instantiate the module */
  61. if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module,
  62. 16 * 1024,
  63. 16 * 1024,
  64. error_buf,
  65. sizeof(error_buf)))) {
  66. ocall_print(error_buf);
  67. ocall_print("\n");
  68. goto fail3;
  69. }
  70. /* execute the main function of wasm app */
  71. app_instance_main(wasm_module_inst);
  72. /* destroy the module instance */
  73. wasm_runtime_deinstantiate(wasm_module_inst);
  74. fail3:
  75. /* unload the module */
  76. wasm_runtime_unload(wasm_module);
  77. fail2:
  78. /* destroy runtime environment */
  79. wasm_runtime_destroy();
  80. fail1:
  81. bh_memory_destroy();
  82. }