wasm_exec_env.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _WASM_EXEC_ENV_H
  6. #define _WASM_EXEC_ENV_H
  7. #include "bh_thread.h"
  8. #include "bh_assert.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. struct WASMModuleInstanceCommon;
  13. struct WASMInterpFrame;
  14. /* Execution environment */
  15. typedef struct WASMExecEnv {
  16. /* Next thread's exec env of a WASM module instance. */
  17. struct WASMExecEnv *next;
  18. /* Previous thread's exec env of a WASM module instance. */
  19. struct WASMExecEnv *prev;
  20. /* The WASM module instance of current thread */
  21. struct WASMModuleInstanceCommon *module_inst;
  22. /* Current interpreter frame of current thread */
  23. struct WASMInterpFrame *cur_frame;
  24. /* The native thread handle of current thread */
  25. korp_tid handle;
  26. /* The boundary of native stack. When interpreter detects that native
  27. frame may overrun this boundary, it throws a stack overflow
  28. exception. */
  29. void *native_stack_boundary;
  30. /* The WASM stack size */
  31. uint32 wasm_stack_size;
  32. /* The WASM stack of current thread */
  33. union {
  34. uint64 __make_it_8_byte_aligned_;
  35. struct {
  36. /* The top boundary of the stack. */
  37. uint8 *top_boundary;
  38. /* Top cell index which is free. */
  39. uint8 *top;
  40. /* The Java stack. */
  41. uint8 bottom[1];
  42. } s;
  43. } wasm_stack;
  44. } WASMExecEnv;
  45. WASMExecEnv *
  46. wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
  47. uint32 stack_size);
  48. void
  49. wasm_exec_env_destroy(WASMExecEnv *exec_env);
  50. /**
  51. * Allocate a WASM frame from the WASM stack.
  52. *
  53. * @param exec_env the current execution environment
  54. * @param size size of the WASM frame, it must be a multiple of 4
  55. *
  56. * @return the WASM frame if there is enough space in the stack area
  57. * with a protection area, NULL otherwise
  58. */
  59. static inline void *
  60. wasm_exec_env_alloc_wasm_frame(WASMExecEnv *exec_env, unsigned size)
  61. {
  62. uint8 *addr = exec_env->wasm_stack.s.top;
  63. bh_assert(!(size & 3));
  64. /* The outs area size cannot be larger than the frame size, so
  65. multiplying by 2 is enough. */
  66. if (addr + size * 2 > exec_env->wasm_stack.s.top_boundary) {
  67. /* WASM stack overflow. */
  68. /* When throwing SOE, the preserved space must be enough. */
  69. /* bh_assert(!exec_env->throwing_soe);*/
  70. return NULL;
  71. }
  72. exec_env->wasm_stack.s.top += size;
  73. return addr;
  74. }
  75. static inline void
  76. wasm_exec_env_free_wasm_frame(WASMExecEnv *exec_env, void *prev_top)
  77. {
  78. bh_assert((uint8 *)prev_top >= exec_env->wasm_stack.s.bottom);
  79. exec_env->wasm_stack.s.top = (uint8 *)prev_top;
  80. }
  81. /**
  82. * Get the current WASM stack top pointer.
  83. *
  84. * @param exec_env the current execution environment
  85. *
  86. * @return the current WASM stack top pointer
  87. */
  88. static inline void*
  89. wasm_exec_env_wasm_stack_top(WASMExecEnv *exec_env)
  90. {
  91. return exec_env->wasm_stack.s.top;
  92. }
  93. /**
  94. * Set the current frame pointer.
  95. *
  96. * @param exec_env the current execution environment
  97. * @param frame the WASM frame to be set for the current exec env
  98. */
  99. static inline void
  100. wasm_exec_env_set_cur_frame(WASMExecEnv *exec_env,
  101. struct WASMInterpFrame *frame)
  102. {
  103. exec_env->cur_frame = frame;
  104. }
  105. /**
  106. * Get the current frame pointer.
  107. *
  108. * @param exec_env the current execution environment
  109. *
  110. * @return the current frame pointer
  111. */
  112. static inline struct WASMInterpFrame*
  113. wasm_exec_env_get_cur_frame(WASMExecEnv *exec_env)
  114. {
  115. return exec_env->cur_frame;
  116. }
  117. struct WASMModuleInstanceCommon *
  118. wasm_exec_env_get_module_inst(WASMExecEnv *exec_env);
  119. #ifdef __cplusplus
  120. }
  121. #endif
  122. #endif /* end of _WASM_EXEC_ENV_H */