wasm_memory.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _WASM_MEMORY_H
  6. #define _WASM_MEMORY_H
  7. #include "bh_common.h"
  8. #include "../include/wasm_export.h"
  9. #include "../interpreter/wasm_runtime.h"
  10. #include "../common/wasm_shared_memory.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #if WASM_ENABLE_SHARED_MEMORY != 0 && BH_ATOMIC_64_IS_ATOMIC != 0
  15. #define GET_LINEAR_MEMORY_SIZE(memory) \
  16. BH_ATOMIC_64_LOAD(memory->memory_data_size)
  17. #define SET_LINEAR_MEMORY_SIZE(memory, size) \
  18. BH_ATOMIC_64_STORE(memory->memory_data_size, size)
  19. #elif WASM_ENABLE_SHARED_MEMORY != 0
  20. static inline uint64
  21. GET_LINEAR_MEMORY_SIZE(const WASMMemoryInstance *memory)
  22. {
  23. SHARED_MEMORY_LOCK(memory);
  24. uint64 memory_data_size = BH_ATOMIC_64_LOAD(memory->memory_data_size);
  25. SHARED_MEMORY_UNLOCK(memory);
  26. return memory_data_size;
  27. }
  28. static inline void
  29. SET_LINEAR_MEMORY_SIZE(WASMMemoryInstance *memory, uint64 size)
  30. {
  31. SHARED_MEMORY_LOCK(memory);
  32. BH_ATOMIC_64_STORE(memory->memory_data_size, size);
  33. SHARED_MEMORY_UNLOCK(memory);
  34. }
  35. #else
  36. #define GET_LINEAR_MEMORY_SIZE(memory) memory->memory_data_size
  37. #define SET_LINEAR_MEMORY_SIZE(memory, size) memory->memory_data_size = size
  38. #endif
  39. #if WASM_ENABLE_INTERP != 0
  40. #if WASM_ENABLE_SHARED_HEAP != 0
  41. #if WASM_ENABLE_MULTI_MEMORY != 0
  42. /* Only enable shared heap for the default memory */
  43. #define is_default_memory (memidx == 0)
  44. #else
  45. #define is_default_memory true
  46. #endif
  47. #if UINTPTR_MAX == UINT64_MAX
  48. #define get_shared_heap_end_off() module->e->shared_heap_end_off.u64
  49. #else
  50. #define get_shared_heap_end_off() \
  51. (uint64)(module->e->shared_heap_end_off.u32[0])
  52. #endif
  53. #if WASM_ENABLE_MEMORY64 != 0
  54. #define shared_heap_is_memory64 is_memory64
  55. #else
  56. #define shared_heap_is_memory64 false
  57. #endif
  58. #define app_addr_in_shared_heap(app_addr, bytes) \
  59. (is_default_memory \
  60. && is_app_addr_in_shared_heap((WASMModuleInstanceCommon *)module, \
  61. shared_heap_is_memory64, (uint64)app_addr, \
  62. bytes))
  63. #define shared_heap_addr_app_to_native(app_addr, native_addr) \
  64. native_addr = module->e->shared_heap_base_addr_adj + app_addr
  65. #define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr) \
  66. if (app_addr_in_shared_heap(app_addr, bytes)) \
  67. shared_heap_addr_app_to_native(app_addr, native_addr); \
  68. else
  69. #else /* else of WASM_ENABLE_SHARED_HEAP != 0 */
  70. #define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr)
  71. #endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */
  72. #endif /* end of WASM_ENABLE_INTERP != 0 */
  73. #if WASM_ENABLE_SHARED_HEAP != 0
  74. bool
  75. is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
  76. bool is_memory64, uint64 app_offset, uint32 bytes);
  77. WASMSharedHeap *
  78. wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args);
  79. WASMSharedHeap *
  80. wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body);
  81. WASMSharedHeap *
  82. wasm_runtime_unchain_shared_heaps(WASMSharedHeap *head, bool entire_chain);
  83. bool
  84. wasm_runtime_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
  85. WASMSharedHeap *shared_heap);
  86. bool
  87. wasm_runtime_attach_shared_heap_internal(WASMModuleInstanceCommon *module_inst,
  88. WASMSharedHeap *shared_heap);
  89. void
  90. wasm_runtime_detach_shared_heap(WASMModuleInstanceCommon *module_inst);
  91. void
  92. wasm_runtime_detach_shared_heap_internal(WASMModuleInstanceCommon *module_inst);
  93. WASMSharedHeap *
  94. wasm_runtime_get_shared_heap(WASMModuleInstanceCommon *module_inst_comm);
  95. uint64
  96. wasm_runtime_shared_heap_malloc(WASMModuleInstanceCommon *module_inst,
  97. uint64 size, void **p_native_addr);
  98. void
  99. wasm_runtime_shared_heap_free(WASMModuleInstanceCommon *module_inst,
  100. uint64 ptr);
  101. #endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */
  102. bool
  103. wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
  104. const MemAllocOption *alloc_option);
  105. void
  106. wasm_runtime_memory_destroy(void);
  107. unsigned
  108. wasm_runtime_memory_pool_size(void);
  109. void
  110. wasm_runtime_set_mem_bound_check_bytes(WASMMemoryInstance *memory,
  111. uint64 memory_data_size);
  112. void
  113. wasm_runtime_set_enlarge_mem_error_callback(
  114. const enlarge_memory_error_callback_t callback, void *user_data);
  115. void
  116. wasm_deallocate_linear_memory(WASMMemoryInstance *memory_inst);
  117. int
  118. wasm_allocate_linear_memory(uint8 **data, bool is_shared_memory,
  119. bool is_memory64, uint64 num_bytes_per_page,
  120. uint64 init_page_count, uint64 max_page_count,
  121. uint64 *memory_data_size);
  122. #ifdef __cplusplus
  123. }
  124. #endif
  125. #endif /* end of _WASM_MEMORY_H */