wasm_memory.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_runtime_common.h"
  6. #include "../interpreter/wasm_runtime.h"
  7. #include "../aot/aot_runtime.h"
  8. #include "mem_alloc.h"
  9. #include "wasm_memory.h"
  10. #if WASM_ENABLE_SHARED_MEMORY != 0
  11. #include "../common/wasm_shared_memory.h"
  12. #endif
  13. #if WASM_ENABLE_THREAD_MGR != 0
  14. #include "../libraries/thread-mgr/thread_manager.h"
  15. #endif
  16. typedef enum Memory_Mode {
  17. MEMORY_MODE_UNKNOWN = 0,
  18. MEMORY_MODE_POOL,
  19. MEMORY_MODE_ALLOCATOR,
  20. MEMORY_MODE_SYSTEM_ALLOCATOR
  21. } Memory_Mode;
  22. static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
  23. static mem_allocator_t pool_allocator = NULL;
  24. #if WASM_ENABLE_SHARED_HEAP != 0
  25. static WASMSharedHeap *shared_heap_list = NULL;
  26. static korp_mutex shared_heap_list_lock;
  27. #endif
  28. static enlarge_memory_error_callback_t enlarge_memory_error_cb;
  29. static void *enlarge_memory_error_user_data;
  30. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  31. static void *allocator_user_data = NULL;
  32. #endif
  33. static void *(*malloc_func)(
  34. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  35. mem_alloc_usage_t usage,
  36. #endif
  37. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  38. void *user_data,
  39. #endif
  40. unsigned int size) = NULL;
  41. static void *(*realloc_func)(
  42. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  43. mem_alloc_usage_t usage, bool full_size_mmaped,
  44. #endif
  45. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  46. void *user_data,
  47. #endif
  48. void *ptr, unsigned int size) = NULL;
  49. static void (*free_func)(
  50. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  51. mem_alloc_usage_t usage,
  52. #endif
  53. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  54. void *user_data,
  55. #endif
  56. void *ptr) = NULL;
  57. static unsigned int global_pool_size;
  58. static uint64
  59. align_as_and_cast(uint64 size, uint64 alignment)
  60. {
  61. uint64 aligned_size = (size + alignment - 1) & ~(alignment - 1);
  62. return aligned_size;
  63. }
  64. static bool
  65. wasm_memory_init_with_pool(void *mem, unsigned int bytes)
  66. {
  67. mem_allocator_t allocator = mem_allocator_create(mem, bytes);
  68. if (allocator) {
  69. memory_mode = MEMORY_MODE_POOL;
  70. pool_allocator = allocator;
  71. global_pool_size = bytes;
  72. return true;
  73. }
  74. LOG_ERROR("Init memory with pool (%p, %u) failed.\n", mem, bytes);
  75. return false;
  76. }
  77. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  78. static bool
  79. wasm_memory_init_with_allocator(void *_user_data, void *_malloc_func,
  80. void *_realloc_func, void *_free_func)
  81. {
  82. if (_malloc_func && _free_func && _malloc_func != _free_func) {
  83. memory_mode = MEMORY_MODE_ALLOCATOR;
  84. allocator_user_data = _user_data;
  85. malloc_func = _malloc_func;
  86. realloc_func = _realloc_func;
  87. free_func = _free_func;
  88. return true;
  89. }
  90. LOG_ERROR("Init memory with allocator (%p, %p, %p, %p) failed.\n",
  91. _user_data, _malloc_func, _realloc_func, _free_func);
  92. return false;
  93. }
  94. #else
  95. static bool
  96. wasm_memory_init_with_allocator(void *malloc_func_ptr, void *realloc_func_ptr,
  97. void *free_func_ptr)
  98. {
  99. if (malloc_func_ptr && free_func_ptr && malloc_func_ptr != free_func_ptr) {
  100. memory_mode = MEMORY_MODE_ALLOCATOR;
  101. malloc_func = malloc_func_ptr;
  102. realloc_func = realloc_func_ptr;
  103. free_func = free_func_ptr;
  104. return true;
  105. }
  106. LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n",
  107. malloc_func_ptr, realloc_func_ptr, free_func_ptr);
  108. return false;
  109. }
  110. #endif
  111. static inline bool
  112. is_bounds_checks_enabled(WASMModuleInstanceCommon *module_inst)
  113. {
  114. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  115. if (!module_inst) {
  116. return true;
  117. }
  118. return wasm_runtime_is_bounds_checks_enabled(module_inst);
  119. #else
  120. return true;
  121. #endif
  122. }
  123. #if WASM_ENABLE_SHARED_HEAP != 0
  124. static void *
  125. wasm_mmap_linear_memory(uint64 map_size, uint64 commit_size);
  126. static void
  127. wasm_munmap_linear_memory(void *mapped_mem, uint64 commit_size,
  128. uint64 map_size);
  129. static void *
  130. runtime_malloc(uint64 size)
  131. {
  132. void *mem;
  133. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  134. LOG_WARNING("Allocate memory failed");
  135. return NULL;
  136. }
  137. memset(mem, 0, (uint32)size);
  138. return mem;
  139. }
  140. WASMSharedHeap *
  141. wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
  142. {
  143. uint64 heap_struct_size = sizeof(WASMSharedHeap), map_size;
  144. uint32 size = init_args->size;
  145. WASMSharedHeap *heap;
  146. if (size == 0) {
  147. goto fail1;
  148. }
  149. if (!(heap = runtime_malloc(heap_struct_size))) {
  150. goto fail1;
  151. }
  152. size = align_uint(size, os_getpagesize());
  153. if (size > APP_HEAP_SIZE_MAX || size < APP_HEAP_SIZE_MIN) {
  154. LOG_WARNING("Invalid size of shared heap");
  155. goto fail2;
  156. }
  157. heap->size = size;
  158. heap->start_off_mem64 = UINT64_MAX - heap->size + 1;
  159. heap->start_off_mem32 = UINT32_MAX - heap->size + 1;
  160. heap->attached_count = 0;
  161. if (init_args->pre_allocated_addr != NULL) {
  162. /* Create shared heap from a pre allocated buffer, its size need to
  163. * align with system page */
  164. if (size != init_args->size) {
  165. LOG_WARNING("Pre allocated size need to be aligned with system "
  166. "page size to create shared heap");
  167. goto fail2;
  168. }
  169. heap->heap_handle = NULL;
  170. heap->base_addr = init_args->pre_allocated_addr;
  171. LOG_VERBOSE("Create preallocated shared heap %p with size %u",
  172. heap->base_addr, size);
  173. }
  174. else {
  175. if (!(heap->heap_handle =
  176. runtime_malloc(mem_allocator_get_heap_struct_size()))) {
  177. goto fail2;
  178. }
  179. #ifndef OS_ENABLE_HW_BOUND_CHECK
  180. map_size = size;
  181. #else
  182. /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
  183. * ea = i + memarg.offset
  184. * both i and memarg.offset are u32 in range 0 to 4G
  185. * so the range of ea is 0 to 8G
  186. */
  187. map_size = 8 * (uint64)BH_GB;
  188. #endif
  189. if (!(heap->base_addr = wasm_mmap_linear_memory(map_size, size))) {
  190. goto fail3;
  191. }
  192. if (!mem_allocator_create_with_struct_and_pool(
  193. heap->heap_handle, heap_struct_size, heap->base_addr, size)) {
  194. LOG_WARNING("init share heap failed");
  195. goto fail4;
  196. }
  197. LOG_VERBOSE("Create pool shared heap %p with size %u", heap->base_addr,
  198. size);
  199. }
  200. os_mutex_lock(&shared_heap_list_lock);
  201. if (shared_heap_list == NULL) {
  202. shared_heap_list = heap;
  203. }
  204. else {
  205. heap->next = shared_heap_list;
  206. shared_heap_list = heap;
  207. }
  208. os_mutex_unlock(&shared_heap_list_lock);
  209. return heap;
  210. fail4:
  211. wasm_munmap_linear_memory(heap->base_addr, size, map_size);
  212. fail3:
  213. wasm_runtime_free(heap->heap_handle);
  214. fail2:
  215. wasm_runtime_free(heap);
  216. fail1:
  217. return NULL;
  218. }
  219. WASMSharedHeap *
  220. wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body)
  221. {
  222. WASMSharedHeap *cur;
  223. bool heap_handle_exist = false;
  224. if (!head || !body) {
  225. LOG_WARNING("Invalid shared heap to chain.");
  226. return NULL;
  227. }
  228. heap_handle_exist = head->heap_handle != NULL;
  229. os_mutex_lock(&shared_heap_list_lock);
  230. if (head->attached_count != 0 || body->attached_count != 0) {
  231. LOG_WARNING("To create shared heap chain, all shared heap need to be "
  232. "detached first.");
  233. os_mutex_unlock(&shared_heap_list_lock);
  234. return NULL;
  235. }
  236. for (cur = shared_heap_list; cur; cur = cur->next) {
  237. if (cur->chain_next == body || cur->chain_next == head) {
  238. LOG_WARNING(
  239. "To create shared heap chain, both the 'head' and 'body' "
  240. "shared heap can't already be the 'body' in another a chain");
  241. os_mutex_unlock(&shared_heap_list_lock);
  242. return NULL;
  243. }
  244. if (cur == head && cur->chain_next) {
  245. LOG_WARNING(
  246. "To create shared heap chain, the 'head' shared heap can't "
  247. "already be the 'head' in another a chain");
  248. os_mutex_unlock(&shared_heap_list_lock);
  249. return NULL;
  250. }
  251. }
  252. for (cur = body; cur; cur = cur->chain_next) {
  253. if (cur->heap_handle && heap_handle_exist) {
  254. LOG_WARNING(
  255. "To create shared heap chain, only one of shared heap can "
  256. "dynamically shared_heap_malloc and shared_heap_free, the rest "
  257. "can only be pre-allocated shared heap");
  258. os_mutex_unlock(&shared_heap_list_lock);
  259. return NULL;
  260. }
  261. if (cur->heap_handle)
  262. heap_handle_exist = true;
  263. }
  264. head->start_off_mem64 = body->start_off_mem64 - head->size;
  265. head->start_off_mem32 = body->start_off_mem32 - head->size;
  266. head->chain_next = body;
  267. os_mutex_unlock(&shared_heap_list_lock);
  268. return head;
  269. }
  270. WASMSharedHeap *
  271. wasm_runtime_unchain_shared_heaps(WASMSharedHeap *head, bool entire_chain)
  272. {
  273. WASMSharedHeap *cur, *tmp;
  274. if (!head || !head->chain_next) {
  275. LOG_WARNING("Invalid shared heap chain to disconnect the head from.");
  276. return NULL;
  277. }
  278. os_mutex_lock(&shared_heap_list_lock);
  279. if (head->attached_count != 0) {
  280. LOG_WARNING("To disconnect the shared heap head from the shared heap "
  281. "chain, the shared heap chain needs to be detached first.");
  282. os_mutex_unlock(&shared_heap_list_lock);
  283. return NULL;
  284. }
  285. cur = head;
  286. while (cur && cur->chain_next) {
  287. cur->start_off_mem64 = UINT64_MAX - cur->size + 1;
  288. cur->start_off_mem32 = UINT32_MAX - cur->size + 1;
  289. tmp = cur;
  290. cur = cur->chain_next;
  291. tmp->chain_next = NULL;
  292. if (!entire_chain)
  293. break;
  294. }
  295. os_mutex_unlock(&shared_heap_list_lock);
  296. return cur;
  297. }
  298. static uint8 *
  299. get_last_used_shared_heap_base_addr_adj(WASMModuleInstanceCommon *module_inst)
  300. {
  301. #if WASM_ENABLE_INTERP != 0
  302. if (module_inst->module_type == Wasm_Module_Bytecode) {
  303. WASMModuleInstanceExtra *e =
  304. (WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e;
  305. return e->shared_heap_base_addr_adj;
  306. }
  307. #endif /* end of WASM_ENABLE_INTERP != 0 */
  308. #if WASM_ENABLE_AOT != 0
  309. if (module_inst->module_type == Wasm_Module_AoT) {
  310. AOTModuleInstanceExtra *e =
  311. (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
  312. return e->shared_heap_base_addr_adj;
  313. }
  314. #endif /* end of WASM_ENABLE_AOT != 0 */
  315. return 0;
  316. }
  317. static uintptr_t
  318. get_last_used_shared_heap_start_offset(WASMModuleInstanceCommon *module_inst)
  319. {
  320. #if WASM_ENABLE_INTERP != 0
  321. if (module_inst->module_type == Wasm_Module_Bytecode) {
  322. WASMModuleInstanceExtra *e =
  323. (WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e;
  324. #if UINTPTR_MAX == UINT64_MAX
  325. return e->shared_heap_start_off.u64;
  326. #else
  327. return e->shared_heap_start_off.u32[0];
  328. #endif
  329. }
  330. #endif /* end of WASM_ENABLE_INTERP != 0 */
  331. #if WASM_ENABLE_AOT != 0
  332. if (module_inst->module_type == Wasm_Module_AoT) {
  333. AOTModuleInstanceExtra *e =
  334. (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
  335. #if UINTPTR_MAX == UINT64_MAX
  336. return e->shared_heap_start_off.u64;
  337. #else
  338. return e->shared_heap_start_off.u32[0];
  339. #endif
  340. }
  341. #endif /* end of WASM_ENABLE_AOT != 0 */
  342. return 0;
  343. }
  344. static uintptr_t
  345. get_last_used_shared_heap_end_offset(WASMModuleInstanceCommon *module_inst)
  346. {
  347. #if WASM_ENABLE_INTERP != 0
  348. if (module_inst->module_type == Wasm_Module_Bytecode) {
  349. WASMModuleInstanceExtra *e =
  350. (WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e;
  351. #if UINTPTR_MAX == UINT64_MAX
  352. return e->shared_heap_end_off.u64;
  353. #else
  354. return e->shared_heap_end_off.u32[0];
  355. #endif
  356. }
  357. #endif /* end of WASM_ENABLE_INTERP != 0 */
  358. #if WASM_ENABLE_AOT != 0
  359. if (module_inst->module_type == Wasm_Module_AoT) {
  360. AOTModuleInstanceExtra *e =
  361. (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
  362. #if UINTPTR_MAX == UINT64_MAX
  363. return e->shared_heap_end_off.u64;
  364. #else
  365. return e->shared_heap_end_off.u32[0];
  366. #endif
  367. }
  368. #endif /* end of WASM_ENABLE_AOT != 0 */
  369. return 0;
  370. }
  371. static void
  372. update_last_used_shared_heap(WASMModuleInstanceCommon *module_inst,
  373. WASMSharedHeap *shared_heap, bool is_memory64)
  374. {
  375. #if WASM_ENABLE_INTERP != 0
  376. if (module_inst->module_type == Wasm_Module_Bytecode) {
  377. WASMModuleInstanceExtra *e =
  378. (WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e;
  379. #if UINTPTR_MAX == UINT64_MAX
  380. if (is_memory64)
  381. e->shared_heap_start_off.u64 = shared_heap->start_off_mem64;
  382. else
  383. e->shared_heap_start_off.u64 = shared_heap->start_off_mem32;
  384. e->shared_heap_end_off.u64 =
  385. e->shared_heap_start_off.u64 - 1 + shared_heap->size;
  386. e->shared_heap_base_addr_adj =
  387. shared_heap->base_addr - e->shared_heap_start_off.u64;
  388. #else
  389. e->shared_heap_start_off.u32[0] = (uint32)shared_heap->start_off_mem32;
  390. e->shared_heap_end_off.u32[0] =
  391. e->shared_heap_start_off.u32[0] - 1 + shared_heap->size;
  392. e->shared_heap_base_addr_adj =
  393. shared_heap->base_addr - e->shared_heap_start_off.u32[0];
  394. #endif
  395. }
  396. #endif /* end of WASM_ENABLE_INTERP != 0 */
  397. #if WASM_ENABLE_AOT != 0
  398. if (module_inst->module_type == Wasm_Module_AoT) {
  399. AOTModuleInstanceExtra *e =
  400. (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
  401. #if UINTPTR_MAX == UINT64_MAX
  402. if (is_memory64)
  403. e->shared_heap_start_off.u64 = shared_heap->start_off_mem64;
  404. else
  405. e->shared_heap_start_off.u64 = shared_heap->start_off_mem32;
  406. e->shared_heap_end_off.u64 =
  407. e->shared_heap_start_off.u64 - 1 + shared_heap->size;
  408. e->shared_heap_base_addr_adj =
  409. shared_heap->base_addr - e->shared_heap_start_off.u64;
  410. #else
  411. e->shared_heap_start_off.u32[0] = (uint32)shared_heap->start_off_mem32;
  412. e->shared_heap_end_off.u32[0] =
  413. e->shared_heap_start_off.u32[0] - 1 + shared_heap->size;
  414. e->shared_heap_base_addr_adj =
  415. shared_heap->base_addr - e->shared_heap_start_off.u32[0];
  416. #endif
  417. }
  418. #endif /* end of WASM_ENABLE_AOT != 0 */
  419. }
  420. bool
  421. wasm_runtime_attach_shared_heap_internal(WASMModuleInstanceCommon *module_inst,
  422. WASMSharedHeap *shared_heap)
  423. {
  424. WASMMemoryInstance *memory =
  425. wasm_get_default_memory((WASMModuleInstance *)module_inst);
  426. uint64 linear_mem_size;
  427. if (!memory)
  428. return false;
  429. linear_mem_size = memory->memory_data_size;
  430. /* check if linear memory and shared heap are overlapped */
  431. if ((memory->is_memory64 && linear_mem_size > shared_heap->start_off_mem64)
  432. || (!memory->is_memory64
  433. && linear_mem_size > shared_heap->start_off_mem32)) {
  434. LOG_WARNING("Linear memory address is overlapped with shared heap");
  435. return false;
  436. }
  437. #if WASM_ENABLE_INTERP != 0
  438. if (module_inst->module_type == Wasm_Module_Bytecode) {
  439. WASMModuleInstanceExtra *e =
  440. (WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e;
  441. if (e->shared_heap) {
  442. LOG_WARNING("A shared heap is already attached");
  443. return false;
  444. }
  445. e->shared_heap = shared_heap;
  446. }
  447. #endif /* end of WASM_ENABLE_INTERP != 0 */
  448. #if WASM_ENABLE_AOT != 0
  449. if (module_inst->module_type == Wasm_Module_AoT) {
  450. AOTModuleInstanceExtra *e =
  451. (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
  452. if (e->shared_heap) {
  453. LOG_WARNING("A shared heap is already attached");
  454. return false;
  455. }
  456. e->shared_heap = shared_heap;
  457. }
  458. #endif /* end of WASM_ENABLE_AOT != 0 */
  459. update_last_used_shared_heap(module_inst, shared_heap, memory->is_memory64);
  460. os_mutex_lock(&shared_heap_list_lock);
  461. shared_heap->attached_count++;
  462. os_mutex_unlock(&shared_heap_list_lock);
  463. LOG_VERBOSE("Shared heap %p is attached to module instance %p", shared_heap,
  464. module_inst);
  465. return true;
  466. }
  467. bool
  468. wasm_runtime_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
  469. WASMSharedHeap *shared_heap)
  470. {
  471. #if WASM_ENABLE_THREAD_MGR != 0
  472. return wasm_cluster_attach_shared_heap(module_inst, shared_heap);
  473. #else
  474. return wasm_runtime_attach_shared_heap_internal(module_inst, shared_heap);
  475. #endif
  476. }
  477. void
  478. wasm_runtime_detach_shared_heap_internal(WASMModuleInstanceCommon *module_inst)
  479. {
  480. /* Reset shared_heap_end_off = UINT64/32_MAX - 1 to handling a corner case,
  481. app_offset >= shared_heap_start && app_offset <= shared_heap_end-bytes+1
  482. when bytes=1 and both e->shared_heap_start_off and e->shared_heap_end_off
  483. is 0xffffffff */
  484. #if WASM_ENABLE_INTERP != 0
  485. if (module_inst->module_type == Wasm_Module_Bytecode) {
  486. WASMModuleInstanceExtra *e =
  487. (WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e;
  488. if (e->shared_heap != NULL) {
  489. os_mutex_lock(&shared_heap_list_lock);
  490. e->shared_heap->attached_count--;
  491. os_mutex_unlock(&shared_heap_list_lock);
  492. }
  493. e->shared_heap = NULL;
  494. #if UINTPTR_MAX == UINT64_MAX
  495. e->shared_heap_start_off.u64 = UINT64_MAX;
  496. e->shared_heap_end_off.u64 = UINT64_MAX - 1;
  497. #else
  498. e->shared_heap_start_off.u32[0] = UINT32_MAX;
  499. e->shared_heap_end_off.u32[0] = UINT32_MAX - 1;
  500. #endif
  501. e->shared_heap_base_addr_adj = NULL;
  502. }
  503. #endif /* end of WASM_ENABLE_INTERP != 0 */
  504. #if WASM_ENABLE_AOT != 0
  505. if (module_inst->module_type == Wasm_Module_AoT) {
  506. AOTModuleInstanceExtra *e =
  507. (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
  508. if (e->shared_heap != NULL) {
  509. os_mutex_lock(&shared_heap_list_lock);
  510. e->shared_heap->attached_count--;
  511. os_mutex_unlock(&shared_heap_list_lock);
  512. }
  513. e->shared_heap = NULL;
  514. #if UINTPTR_MAX == UINT64_MAX
  515. e->shared_heap_start_off.u64 = UINT64_MAX;
  516. e->shared_heap_end_off.u64 = UINT64_MAX - 1;
  517. #else
  518. e->shared_heap_start_off.u32[0] = UINT32_MAX;
  519. e->shared_heap_end_off.u32[0] = UINT32_MAX - 1;
  520. #endif
  521. e->shared_heap_base_addr_adj = NULL;
  522. }
  523. #endif /* end of WASM_ENABLE_AOT != 0 */
  524. LOG_VERBOSE("Shared heap is detached from module instance %p", module_inst);
  525. }
  526. void
  527. wasm_runtime_detach_shared_heap(WASMModuleInstanceCommon *module_inst)
  528. {
  529. #if WASM_ENABLE_THREAD_MGR != 0
  530. wasm_cluster_detach_shared_heap(module_inst);
  531. #else
  532. wasm_runtime_detach_shared_heap_internal(module_inst);
  533. #endif
  534. }
  535. static WASMSharedHeap *
  536. get_shared_heap(WASMModuleInstanceCommon *module_inst_comm)
  537. {
  538. #if WASM_ENABLE_INTERP != 0
  539. if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
  540. return ((WASMModuleInstance *)module_inst_comm)->e->shared_heap;
  541. }
  542. #endif
  543. #if WASM_ENABLE_AOT != 0
  544. if (module_inst_comm->module_type == Wasm_Module_AoT) {
  545. AOTModuleInstanceExtra *e =
  546. (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst_comm)
  547. ->e;
  548. return e->shared_heap;
  549. }
  550. #endif
  551. return NULL;
  552. }
  553. WASMSharedHeap *
  554. wasm_runtime_get_shared_heap(WASMModuleInstanceCommon *module_inst_comm)
  555. {
  556. return get_shared_heap(module_inst_comm);
  557. }
  558. bool
  559. is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
  560. bool is_memory64, uint64 app_offset, uint64 bytes)
  561. {
  562. WASMSharedHeap *heap = get_shared_heap(module_inst), *cur;
  563. uint64 shared_heap_start, shared_heap_end;
  564. if (!heap || bytes > APP_HEAP_SIZE_MAX) {
  565. goto fail;
  566. }
  567. if (bytes == 0) {
  568. bytes = 1;
  569. }
  570. shared_heap_start =
  571. (uint64)get_last_used_shared_heap_start_offset(module_inst);
  572. shared_heap_end = (uint64)get_last_used_shared_heap_end_offset(module_inst);
  573. if (bytes - 1 <= shared_heap_end && app_offset >= shared_heap_start
  574. && app_offset <= shared_heap_end - bytes + 1) {
  575. return true;
  576. }
  577. /* Early stop for app start address not in the shared heap(chain) at all */
  578. shared_heap_start =
  579. is_memory64 ? heap->start_off_mem64 : heap->start_off_mem32;
  580. shared_heap_end = is_memory64 ? UINT64_MAX : UINT32_MAX;
  581. if (bytes - 1 > shared_heap_end || app_offset < shared_heap_start
  582. || app_offset > shared_heap_end - bytes + 1) {
  583. goto fail;
  584. }
  585. /* Find the exact shared heap that app addr is in, and update last used
  586. * shared heap info in module inst extra */
  587. for (cur = heap; cur; cur = cur->chain_next) {
  588. shared_heap_start =
  589. is_memory64 ? cur->start_off_mem64 : cur->start_off_mem32;
  590. shared_heap_end = shared_heap_start - 1 + cur->size;
  591. if (bytes - 1 <= shared_heap_end && app_offset >= shared_heap_start
  592. && app_offset <= shared_heap_end - bytes + 1) {
  593. update_last_used_shared_heap(module_inst, cur, is_memory64);
  594. return true;
  595. }
  596. }
  597. fail:
  598. return false;
  599. }
  600. static bool
  601. is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
  602. bool is_memory64, uint8 *addr, uint64 bytes)
  603. {
  604. WASMSharedHeap *cur, *heap = get_shared_heap(module_inst);
  605. uintptr_t base_addr, addr_int, end_addr;
  606. if (!heap || bytes > APP_HEAP_SIZE_MAX) {
  607. goto fail;
  608. }
  609. /* Iterate through shared heap chain to find whether native addr in one of
  610. * shared heap */
  611. for (cur = heap; cur != NULL; cur = cur->chain_next) {
  612. base_addr = (uintptr_t)cur->base_addr;
  613. addr_int = (uintptr_t)addr;
  614. if (addr_int < base_addr)
  615. continue;
  616. end_addr = addr_int + bytes;
  617. /* Check for overflow */
  618. if (end_addr <= addr_int)
  619. continue;
  620. if (end_addr > base_addr + cur->size)
  621. continue;
  622. update_last_used_shared_heap(module_inst, cur, is_memory64);
  623. return true;
  624. }
  625. fail:
  626. return false;
  627. }
  628. uint64
  629. wasm_runtime_shared_heap_malloc(WASMModuleInstanceCommon *module_inst,
  630. uint64 size, void **p_native_addr)
  631. {
  632. WASMMemoryInstance *memory =
  633. wasm_get_default_memory((WASMModuleInstance *)module_inst);
  634. WASMSharedHeap *shared_heap = get_shared_heap(module_inst);
  635. void *native_addr = NULL;
  636. if (!memory || !shared_heap)
  637. return 0;
  638. while (shared_heap && !shared_heap->heap_handle) {
  639. shared_heap = shared_heap->chain_next;
  640. }
  641. if (!shared_heap) {
  642. LOG_WARNING("Can't allocate from pre allocated shared heap");
  643. return 0;
  644. }
  645. native_addr = mem_allocator_malloc(shared_heap->heap_handle, size);
  646. if (!native_addr)
  647. return 0;
  648. if (p_native_addr) {
  649. *p_native_addr = native_addr;
  650. }
  651. return memory->is_memory64
  652. ? shared_heap->start_off_mem64
  653. : shared_heap->start_off_mem32
  654. + ((uint8 *)native_addr - shared_heap->base_addr);
  655. }
  656. void
  657. wasm_runtime_shared_heap_free(WASMModuleInstanceCommon *module_inst, uint64 ptr)
  658. {
  659. WASMMemoryInstance *memory =
  660. wasm_get_default_memory((WASMModuleInstance *)module_inst);
  661. WASMSharedHeap *shared_heap = get_shared_heap(module_inst);
  662. uint8 *addr = NULL;
  663. if (!memory || !shared_heap) {
  664. return;
  665. }
  666. while (shared_heap && !shared_heap->heap_handle) {
  667. shared_heap = shared_heap->chain_next;
  668. }
  669. if (!shared_heap) {
  670. LOG_WARNING("The address to free is from pre allocated shared heap");
  671. return;
  672. }
  673. if (memory->is_memory64) {
  674. if (ptr < shared_heap->start_off_mem64) { /* ptr can not > UINT64_MAX */
  675. LOG_WARNING("The address to free isn't in shared heap");
  676. return;
  677. }
  678. addr = shared_heap->base_addr + (ptr - shared_heap->start_off_mem64);
  679. }
  680. else {
  681. if (ptr < shared_heap->start_off_mem32 || ptr > UINT32_MAX) {
  682. LOG_WARNING("The address to free isn't in shared heap");
  683. return;
  684. }
  685. addr = shared_heap->base_addr + (ptr - shared_heap->start_off_mem32);
  686. }
  687. mem_allocator_free(shared_heap->heap_handle, addr);
  688. }
  689. #endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */
  690. bool
  691. wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
  692. const MemAllocOption *alloc_option)
  693. {
  694. bool ret = false;
  695. #if WASM_ENABLE_SHARED_HEAP != 0
  696. if (os_mutex_init(&shared_heap_list_lock)) {
  697. return false;
  698. }
  699. #endif
  700. if (mem_alloc_type == Alloc_With_Pool) {
  701. ret = wasm_memory_init_with_pool(alloc_option->pool.heap_buf,
  702. alloc_option->pool.heap_size);
  703. }
  704. else if (mem_alloc_type == Alloc_With_Allocator) {
  705. ret = wasm_memory_init_with_allocator(
  706. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  707. alloc_option->allocator.user_data,
  708. #endif
  709. alloc_option->allocator.malloc_func,
  710. alloc_option->allocator.realloc_func,
  711. alloc_option->allocator.free_func);
  712. }
  713. else if (mem_alloc_type == Alloc_With_System_Allocator) {
  714. memory_mode = MEMORY_MODE_SYSTEM_ALLOCATOR;
  715. ret = true;
  716. }
  717. else {
  718. ret = false;
  719. }
  720. #if WASM_ENABLE_SHARED_HEAP != 0
  721. if (!ret) {
  722. os_mutex_destroy(&shared_heap_list_lock);
  723. }
  724. #endif
  725. return ret;
  726. }
  727. #if WASM_ENABLE_SHARED_HEAP != 0
  728. static void
  729. destroy_shared_heaps()
  730. {
  731. WASMSharedHeap *heap;
  732. WASMSharedHeap *cur;
  733. uint64 map_size;
  734. os_mutex_lock(&shared_heap_list_lock);
  735. heap = shared_heap_list;
  736. shared_heap_list = NULL;
  737. os_mutex_unlock(&shared_heap_list_lock);
  738. while (heap) {
  739. cur = heap;
  740. heap = heap->next;
  741. if (cur->heap_handle) {
  742. mem_allocator_destroy(cur->heap_handle);
  743. wasm_runtime_free(cur->heap_handle);
  744. #ifndef OS_ENABLE_HW_BOUND_CHECK
  745. map_size = cur->size;
  746. #else
  747. map_size = 8 * (uint64)BH_GB;
  748. #endif
  749. wasm_munmap_linear_memory(cur->base_addr, cur->size, map_size);
  750. }
  751. wasm_runtime_free(cur);
  752. }
  753. os_mutex_destroy(&shared_heap_list_lock);
  754. }
  755. #endif
  756. void
  757. wasm_runtime_memory_destroy(void)
  758. {
  759. #if WASM_ENABLE_SHARED_HEAP != 0
  760. destroy_shared_heaps();
  761. #endif
  762. if (memory_mode == MEMORY_MODE_POOL) {
  763. #if BH_ENABLE_GC_VERIFY == 0
  764. (void)mem_allocator_destroy(pool_allocator);
  765. #else
  766. int ret = mem_allocator_destroy(pool_allocator);
  767. if (ret != 0) {
  768. /* Memory leak detected */
  769. exit(-1);
  770. }
  771. #endif
  772. }
  773. memory_mode = MEMORY_MODE_UNKNOWN;
  774. }
  775. unsigned
  776. wasm_runtime_memory_pool_size(void)
  777. {
  778. if (memory_mode == MEMORY_MODE_POOL)
  779. return global_pool_size;
  780. else
  781. return UINT32_MAX;
  782. }
  783. static inline void *
  784. wasm_runtime_malloc_internal(unsigned int size)
  785. {
  786. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  787. LOG_WARNING(
  788. "wasm_runtime_malloc failed: memory hasn't been initialized.\n");
  789. return NULL;
  790. }
  791. else if (memory_mode == MEMORY_MODE_POOL) {
  792. return mem_allocator_malloc(pool_allocator, size);
  793. }
  794. else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
  795. return malloc_func(
  796. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  797. Alloc_For_Runtime,
  798. #endif
  799. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  800. allocator_user_data,
  801. #endif
  802. size);
  803. }
  804. else {
  805. return os_malloc(size);
  806. }
  807. }
  808. static inline void *
  809. wasm_runtime_realloc_internal(void *ptr, unsigned int size)
  810. {
  811. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  812. LOG_WARNING(
  813. "wasm_runtime_realloc failed: memory hasn't been initialized.\n");
  814. return NULL;
  815. }
  816. else if (memory_mode == MEMORY_MODE_POOL) {
  817. return mem_allocator_realloc(pool_allocator, ptr, size);
  818. }
  819. else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
  820. if (realloc_func)
  821. return realloc_func(
  822. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  823. Alloc_For_Runtime, false,
  824. #endif
  825. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  826. allocator_user_data,
  827. #endif
  828. ptr, size);
  829. else
  830. return NULL;
  831. }
  832. else {
  833. return os_realloc(ptr, size);
  834. }
  835. }
  836. static inline void
  837. wasm_runtime_free_internal(void *ptr)
  838. {
  839. if (!ptr) {
  840. LOG_WARNING("warning: wasm_runtime_free with NULL pointer\n");
  841. #if BH_ENABLE_GC_VERIFY != 0
  842. exit(-1);
  843. #endif
  844. return;
  845. }
  846. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  847. LOG_WARNING("warning: wasm_runtime_free failed: "
  848. "memory hasn't been initialize.\n");
  849. }
  850. else if (memory_mode == MEMORY_MODE_POOL) {
  851. mem_allocator_free(pool_allocator, ptr);
  852. }
  853. else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
  854. free_func(
  855. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  856. Alloc_For_Runtime,
  857. #endif
  858. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  859. allocator_user_data,
  860. #endif
  861. ptr);
  862. }
  863. else {
  864. os_free(ptr);
  865. }
  866. }
  867. void *
  868. wasm_runtime_malloc(unsigned int size)
  869. {
  870. if (size == 0) {
  871. LOG_WARNING("warning: wasm_runtime_malloc with size zero\n");
  872. /* At lease alloc 1 byte to avoid malloc failed */
  873. size = 1;
  874. #if BH_ENABLE_GC_VERIFY != 0
  875. exit(-1);
  876. #endif
  877. }
  878. #if WASM_ENABLE_FUZZ_TEST != 0
  879. if (size >= WASM_MEM_ALLOC_MAX_SIZE) {
  880. LOG_WARNING("warning: wasm_runtime_malloc with too large size\n");
  881. return NULL;
  882. }
  883. #endif
  884. return wasm_runtime_malloc_internal(size);
  885. }
  886. void *
  887. wasm_runtime_realloc(void *ptr, unsigned int size)
  888. {
  889. return wasm_runtime_realloc_internal(ptr, size);
  890. }
  891. void
  892. wasm_runtime_free(void *ptr)
  893. {
  894. wasm_runtime_free_internal(ptr);
  895. }
  896. bool
  897. wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info)
  898. {
  899. if (memory_mode == MEMORY_MODE_POOL) {
  900. return mem_allocator_get_alloc_info(pool_allocator, mem_alloc_info);
  901. }
  902. return false;
  903. }
  904. bool
  905. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst_comm,
  906. uint64 app_offset, uint64 size)
  907. {
  908. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  909. WASMMemoryInstance *memory_inst;
  910. uint64 max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
  911. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  912. || module_inst_comm->module_type == Wasm_Module_AoT);
  913. if (!is_bounds_checks_enabled(module_inst_comm)) {
  914. return true;
  915. }
  916. memory_inst = wasm_get_default_memory(module_inst);
  917. if (!memory_inst) {
  918. goto fail;
  919. }
  920. #if WASM_ENABLE_SHARED_HEAP != 0
  921. if (is_app_addr_in_shared_heap(module_inst_comm, memory_inst->is_memory64,
  922. app_offset, size)) {
  923. return true;
  924. }
  925. #endif
  926. #if WASM_ENABLE_MEMORY64 != 0
  927. if (memory_inst->is_memory64)
  928. max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
  929. #endif
  930. /* boundary overflow check */
  931. if (size > max_linear_memory_size
  932. || app_offset > max_linear_memory_size - size) {
  933. goto fail;
  934. }
  935. SHARED_MEMORY_LOCK(memory_inst);
  936. if (app_offset + size <= memory_inst->memory_data_size) {
  937. SHARED_MEMORY_UNLOCK(memory_inst);
  938. return true;
  939. }
  940. SHARED_MEMORY_UNLOCK(memory_inst);
  941. fail:
  942. wasm_set_exception(module_inst, "out of bounds memory access");
  943. return false;
  944. }
  945. bool
  946. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst_comm,
  947. uint64 app_str_offset)
  948. {
  949. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  950. WASMMemoryInstance *memory_inst;
  951. uint64 app_end_offset, max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
  952. char *str, *str_end;
  953. #if WASM_ENABLE_SHARED_HEAP != 0
  954. uintptr_t shared_heap_end_off;
  955. char *shared_heap_base_addr_adj;
  956. #endif
  957. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  958. || module_inst_comm->module_type == Wasm_Module_AoT);
  959. if (!is_bounds_checks_enabled(module_inst_comm)) {
  960. return true;
  961. }
  962. memory_inst = wasm_get_default_memory(module_inst);
  963. if (!memory_inst) {
  964. goto fail;
  965. }
  966. #if WASM_ENABLE_SHARED_HEAP != 0
  967. if (is_app_addr_in_shared_heap(module_inst_comm, memory_inst->is_memory64,
  968. app_str_offset, 1)) {
  969. shared_heap_end_off =
  970. get_last_used_shared_heap_end_offset(module_inst_comm);
  971. shared_heap_base_addr_adj =
  972. (char *)get_last_used_shared_heap_base_addr_adj(module_inst_comm);
  973. str = shared_heap_base_addr_adj + app_str_offset;
  974. str_end = shared_heap_base_addr_adj + shared_heap_end_off + 1;
  975. }
  976. else
  977. #endif
  978. {
  979. if (!wasm_runtime_get_app_addr_range(module_inst_comm, app_str_offset,
  980. NULL, &app_end_offset))
  981. goto fail;
  982. #if WASM_ENABLE_MEMORY64 != 0
  983. if (memory_inst->is_memory64)
  984. max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
  985. #endif
  986. /* boundary overflow check, max start offset can be size - 1, while end
  987. offset can be size */
  988. if (app_str_offset >= max_linear_memory_size
  989. || app_end_offset > max_linear_memory_size)
  990. goto fail;
  991. str = wasm_runtime_addr_app_to_native(module_inst_comm, app_str_offset);
  992. str_end = str + (app_end_offset - app_str_offset);
  993. }
  994. while (str < str_end && *str != '\0')
  995. str++;
  996. if (str == str_end)
  997. goto fail;
  998. return true;
  999. fail:
  1000. wasm_set_exception(module_inst, "out of bounds memory access");
  1001. return false;
  1002. }
  1003. bool
  1004. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst_comm,
  1005. void *native_ptr, uint64 size)
  1006. {
  1007. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  1008. WASMMemoryInstance *memory_inst;
  1009. uint8 *addr = (uint8 *)native_ptr;
  1010. uint64 max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
  1011. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  1012. || module_inst_comm->module_type == Wasm_Module_AoT);
  1013. if (!is_bounds_checks_enabled(module_inst_comm)) {
  1014. return true;
  1015. }
  1016. memory_inst = wasm_get_default_memory(module_inst);
  1017. if (!memory_inst) {
  1018. goto fail;
  1019. }
  1020. #if WASM_ENABLE_MEMORY64 != 0
  1021. if (memory_inst->is_memory64)
  1022. max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
  1023. #endif
  1024. /* boundary overflow check */
  1025. if (size > max_linear_memory_size || (uintptr_t)addr > UINTPTR_MAX - size) {
  1026. goto fail;
  1027. }
  1028. #if WASM_ENABLE_SHARED_HEAP != 0
  1029. if (is_native_addr_in_shared_heap(
  1030. module_inst_comm, memory_inst->is_memory64, native_ptr, size)) {
  1031. return true;
  1032. }
  1033. #endif
  1034. SHARED_MEMORY_LOCK(memory_inst);
  1035. if (memory_inst->memory_data <= addr
  1036. && addr + size <= memory_inst->memory_data_end) {
  1037. SHARED_MEMORY_UNLOCK(memory_inst);
  1038. return true;
  1039. }
  1040. SHARED_MEMORY_UNLOCK(memory_inst);
  1041. fail:
  1042. wasm_set_exception(module_inst, "out of bounds memory access");
  1043. return false;
  1044. }
  1045. void *
  1046. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst_comm,
  1047. uint64 app_offset)
  1048. {
  1049. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  1050. WASMMemoryInstance *memory_inst;
  1051. uint8 *addr;
  1052. bool bounds_checks;
  1053. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  1054. || module_inst_comm->module_type == Wasm_Module_AoT);
  1055. bounds_checks = is_bounds_checks_enabled(module_inst_comm);
  1056. memory_inst = wasm_get_default_memory(module_inst);
  1057. if (!memory_inst) {
  1058. return NULL;
  1059. }
  1060. #if WASM_ENABLE_SHARED_HEAP != 0
  1061. if (is_app_addr_in_shared_heap(module_inst_comm, memory_inst->is_memory64,
  1062. app_offset, 1)) {
  1063. return get_last_used_shared_heap_base_addr_adj(module_inst_comm)
  1064. + app_offset;
  1065. }
  1066. #endif
  1067. SHARED_MEMORY_LOCK(memory_inst);
  1068. addr = memory_inst->memory_data + (uintptr_t)app_offset;
  1069. if (bounds_checks) {
  1070. if (memory_inst->memory_data <= addr
  1071. && addr < memory_inst->memory_data_end) {
  1072. SHARED_MEMORY_UNLOCK(memory_inst);
  1073. return addr;
  1074. }
  1075. SHARED_MEMORY_UNLOCK(memory_inst);
  1076. return NULL;
  1077. }
  1078. /* If bounds checks is disabled, return the address directly */
  1079. SHARED_MEMORY_UNLOCK(memory_inst);
  1080. return addr;
  1081. }
  1082. uint64
  1083. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst_comm,
  1084. void *native_ptr)
  1085. {
  1086. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  1087. WASMMemoryInstance *memory_inst;
  1088. uint8 *addr = (uint8 *)native_ptr;
  1089. bool bounds_checks;
  1090. uint64 ret;
  1091. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  1092. || module_inst_comm->module_type == Wasm_Module_AoT);
  1093. bounds_checks = is_bounds_checks_enabled(module_inst_comm);
  1094. memory_inst = wasm_get_default_memory(module_inst);
  1095. if (!memory_inst) {
  1096. return 0;
  1097. }
  1098. #if WASM_ENABLE_SHARED_HEAP != 0
  1099. if (is_native_addr_in_shared_heap(module_inst_comm,
  1100. memory_inst->is_memory64, addr, 1)) {
  1101. return (uint64)(uintptr_t)(addr
  1102. - get_last_used_shared_heap_base_addr_adj(
  1103. module_inst_comm));
  1104. }
  1105. #endif
  1106. SHARED_MEMORY_LOCK(memory_inst);
  1107. if (bounds_checks) {
  1108. if (memory_inst->memory_data <= addr
  1109. && addr < memory_inst->memory_data_end) {
  1110. ret = (uint64)(addr - memory_inst->memory_data);
  1111. SHARED_MEMORY_UNLOCK(memory_inst);
  1112. return ret;
  1113. }
  1114. }
  1115. /* If bounds checks is disabled, return the offset directly */
  1116. else if (addr != NULL) {
  1117. ret = (uint64)(addr - memory_inst->memory_data);
  1118. SHARED_MEMORY_UNLOCK(memory_inst);
  1119. return ret;
  1120. }
  1121. SHARED_MEMORY_UNLOCK(memory_inst);
  1122. return 0;
  1123. }
  1124. bool
  1125. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst_comm,
  1126. uint64 app_offset, uint64 *p_app_start_offset,
  1127. uint64 *p_app_end_offset)
  1128. {
  1129. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  1130. WASMMemoryInstance *memory_inst;
  1131. uint64 memory_data_size;
  1132. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  1133. || module_inst_comm->module_type == Wasm_Module_AoT);
  1134. memory_inst = wasm_get_default_memory(module_inst);
  1135. if (!memory_inst) {
  1136. return false;
  1137. }
  1138. SHARED_MEMORY_LOCK(memory_inst);
  1139. memory_data_size = memory_inst->memory_data_size;
  1140. if (app_offset < memory_data_size) {
  1141. if (p_app_start_offset)
  1142. *p_app_start_offset = 0;
  1143. if (p_app_end_offset)
  1144. *p_app_end_offset = memory_data_size;
  1145. SHARED_MEMORY_UNLOCK(memory_inst);
  1146. return true;
  1147. }
  1148. SHARED_MEMORY_UNLOCK(memory_inst);
  1149. return false;
  1150. }
  1151. bool
  1152. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst_comm,
  1153. uint8 *native_ptr,
  1154. uint8 **p_native_start_addr,
  1155. uint8 **p_native_end_addr)
  1156. {
  1157. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  1158. WASMMemoryInstance *memory_inst;
  1159. uint8 *addr = (uint8 *)native_ptr;
  1160. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  1161. || module_inst_comm->module_type == Wasm_Module_AoT);
  1162. memory_inst = wasm_get_default_memory(module_inst);
  1163. if (!memory_inst) {
  1164. return false;
  1165. }
  1166. SHARED_MEMORY_LOCK(memory_inst);
  1167. if (memory_inst->memory_data <= addr
  1168. && addr < memory_inst->memory_data_end) {
  1169. if (p_native_start_addr)
  1170. *p_native_start_addr = memory_inst->memory_data;
  1171. if (p_native_end_addr)
  1172. *p_native_end_addr = memory_inst->memory_data_end;
  1173. SHARED_MEMORY_UNLOCK(memory_inst);
  1174. return true;
  1175. }
  1176. SHARED_MEMORY_UNLOCK(memory_inst);
  1177. return false;
  1178. }
  1179. bool
  1180. wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  1181. uint64 app_buf_addr, uint64 app_buf_size,
  1182. void **p_native_addr)
  1183. {
  1184. WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
  1185. uint8 *native_addr;
  1186. bool bounds_checks;
  1187. #if WASM_ENABLE_SHARED_HEAP != 0
  1188. uint8 *shared_heap_base_addr_adj = NULL;
  1189. uintptr_t shared_heap_end_off = 0;
  1190. #endif
  1191. bh_assert(app_buf_addr <= UINTPTR_MAX && app_buf_size <= UINTPTR_MAX);
  1192. if (!memory_inst) {
  1193. wasm_set_exception(module_inst, "out of bounds memory access");
  1194. return false;
  1195. }
  1196. #if WASM_ENABLE_SHARED_HEAP != 0
  1197. if (is_app_addr_in_shared_heap((WASMModuleInstanceCommon *)module_inst,
  1198. memory_inst->is_memory64, app_buf_addr,
  1199. app_buf_size)) {
  1200. const char *str, *str_end;
  1201. shared_heap_base_addr_adj = get_last_used_shared_heap_base_addr_adj(
  1202. (WASMModuleInstanceCommon *)module_inst);
  1203. shared_heap_end_off = get_last_used_shared_heap_end_offset(
  1204. (WASMModuleInstanceCommon *)module_inst);
  1205. native_addr = shared_heap_base_addr_adj + (uintptr_t)app_buf_addr;
  1206. /* The whole string must be in the shared heap */
  1207. str = (const char *)native_addr;
  1208. str_end =
  1209. (const char *)shared_heap_base_addr_adj + shared_heap_end_off + 1;
  1210. while (str < str_end && *str != '\0')
  1211. str++;
  1212. if (str == str_end) {
  1213. wasm_set_exception(module_inst, "out of bounds memory access");
  1214. return false;
  1215. }
  1216. else
  1217. goto success;
  1218. }
  1219. #endif
  1220. native_addr = memory_inst->memory_data + (uintptr_t)app_buf_addr;
  1221. bounds_checks =
  1222. is_bounds_checks_enabled((WASMModuleInstanceCommon *)module_inst);
  1223. if (!bounds_checks) {
  1224. if (app_buf_addr == 0) {
  1225. native_addr = NULL;
  1226. }
  1227. goto success;
  1228. }
  1229. /* No need to check the app_offset and buf_size if memory access
  1230. boundary check with hardware trap is enabled */
  1231. #ifndef OS_ENABLE_HW_BOUND_CHECK
  1232. SHARED_MEMORY_LOCK(memory_inst);
  1233. if (app_buf_addr >= memory_inst->memory_data_size) {
  1234. goto fail;
  1235. }
  1236. if (!is_str) {
  1237. if (app_buf_size > memory_inst->memory_data_size - app_buf_addr) {
  1238. goto fail;
  1239. }
  1240. }
  1241. else {
  1242. const char *str, *str_end;
  1243. /* The whole string must be in the linear memory */
  1244. str = (const char *)native_addr;
  1245. str_end = (const char *)memory_inst->memory_data_end;
  1246. while (str < str_end && *str != '\0')
  1247. str++;
  1248. if (str == str_end)
  1249. goto fail;
  1250. }
  1251. SHARED_MEMORY_UNLOCK(memory_inst);
  1252. #endif
  1253. success:
  1254. *p_native_addr = (void *)native_addr;
  1255. return true;
  1256. #ifndef OS_ENABLE_HW_BOUND_CHECK
  1257. fail:
  1258. SHARED_MEMORY_UNLOCK(memory_inst);
  1259. wasm_set_exception(module_inst, "out of bounds memory access");
  1260. return false;
  1261. #endif
  1262. }
  1263. WASMMemoryInstance *
  1264. wasm_get_default_memory(WASMModuleInstance *module_inst)
  1265. {
  1266. if (module_inst->memories)
  1267. return module_inst->memories[0];
  1268. else
  1269. return NULL;
  1270. }
  1271. WASMMemoryInstance *
  1272. wasm_get_memory_with_idx(WASMModuleInstance *module_inst, uint32 index)
  1273. {
  1274. if ((index >= module_inst->memory_count) || !module_inst->memories)
  1275. return NULL;
  1276. return module_inst->memories[index];
  1277. }
  1278. void
  1279. wasm_runtime_set_mem_bound_check_bytes(WASMMemoryInstance *memory,
  1280. uint64 memory_data_size)
  1281. {
  1282. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 || WASM_ENABLE_AOT != 0
  1283. #if UINTPTR_MAX == UINT64_MAX
  1284. memory->mem_bound_check_1byte.u64 = memory_data_size - 1;
  1285. memory->mem_bound_check_2bytes.u64 = memory_data_size - 2;
  1286. memory->mem_bound_check_4bytes.u64 = memory_data_size - 4;
  1287. memory->mem_bound_check_8bytes.u64 = memory_data_size - 8;
  1288. memory->mem_bound_check_16bytes.u64 = memory_data_size - 16;
  1289. #else
  1290. memory->mem_bound_check_1byte.u32[0] = (uint32)memory_data_size - 1;
  1291. memory->mem_bound_check_2bytes.u32[0] = (uint32)memory_data_size - 2;
  1292. memory->mem_bound_check_4bytes.u32[0] = (uint32)memory_data_size - 4;
  1293. memory->mem_bound_check_8bytes.u32[0] = (uint32)memory_data_size - 8;
  1294. memory->mem_bound_check_16bytes.u32[0] = (uint32)memory_data_size - 16;
  1295. #endif
  1296. #endif
  1297. }
  1298. static void
  1299. wasm_munmap_linear_memory(void *mapped_mem, uint64 commit_size, uint64 map_size)
  1300. {
  1301. #ifdef BH_PLATFORM_WINDOWS
  1302. os_mem_decommit(mapped_mem, commit_size);
  1303. #else
  1304. (void)commit_size;
  1305. #endif
  1306. os_munmap(mapped_mem, map_size);
  1307. }
  1308. static void *
  1309. wasm_mremap_linear_memory(void *mapped_mem, uint64 old_size, uint64 new_size,
  1310. uint64 commit_size)
  1311. {
  1312. void *new_mem;
  1313. bh_assert(new_size > 0);
  1314. bh_assert(new_size > old_size);
  1315. #if UINTPTR_MAX == UINT32_MAX
  1316. if (new_size == 4 * (uint64)BH_GB) {
  1317. LOG_WARNING("On 32 bit platform, linear memory can't reach maximum "
  1318. "size of 4GB\n");
  1319. return NULL;
  1320. }
  1321. #endif
  1322. if (mapped_mem) {
  1323. new_mem = os_mremap(mapped_mem, old_size, new_size);
  1324. }
  1325. else {
  1326. new_mem = os_mmap(NULL, new_size, MMAP_PROT_NONE, MMAP_MAP_NONE,
  1327. os_get_invalid_handle());
  1328. }
  1329. if (!new_mem) {
  1330. return NULL;
  1331. }
  1332. #ifdef BH_PLATFORM_WINDOWS
  1333. if (commit_size > 0
  1334. && !os_mem_commit(new_mem, commit_size,
  1335. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  1336. os_munmap(new_mem, new_size);
  1337. return NULL;
  1338. }
  1339. #endif
  1340. if (os_mprotect(new_mem, commit_size, MMAP_PROT_READ | MMAP_PROT_WRITE)
  1341. != 0) {
  1342. wasm_munmap_linear_memory(new_mem, new_size, new_size);
  1343. return NULL;
  1344. }
  1345. return new_mem;
  1346. }
  1347. static void *
  1348. wasm_mmap_linear_memory(uint64 map_size, uint64 commit_size)
  1349. {
  1350. return wasm_mremap_linear_memory(NULL, 0, map_size, commit_size);
  1351. }
  1352. static bool
  1353. wasm_enlarge_memory_internal(WASMModuleInstanceCommon *module,
  1354. WASMMemoryInstance *memory, uint32 inc_page_count)
  1355. {
  1356. #if WASM_ENABLE_SHARED_HEAP != 0
  1357. WASMSharedHeap *shared_heap;
  1358. #endif
  1359. uint8 *memory_data_old, *memory_data_new, *heap_data_old;
  1360. uint32 num_bytes_per_page, heap_size;
  1361. uint32 cur_page_count, max_page_count, total_page_count;
  1362. uint64 total_size_old = 0, total_size_new;
  1363. bool ret = true, full_size_mmaped;
  1364. enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
  1365. if (!memory) {
  1366. ret = false;
  1367. goto return_func;
  1368. }
  1369. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1370. full_size_mmaped = true;
  1371. #elif WASM_ENABLE_SHARED_MEMORY != 0
  1372. full_size_mmaped = shared_memory_is_shared(memory);
  1373. #else
  1374. full_size_mmaped = false;
  1375. #endif
  1376. memory_data_old = memory->memory_data;
  1377. total_size_old = memory->memory_data_size;
  1378. heap_data_old = memory->heap_data;
  1379. heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
  1380. num_bytes_per_page = memory->num_bytes_per_page;
  1381. cur_page_count = memory->cur_page_count;
  1382. max_page_count = memory->max_page_count;
  1383. total_page_count = inc_page_count + cur_page_count;
  1384. total_size_new = num_bytes_per_page * (uint64)total_page_count;
  1385. if (inc_page_count <= 0)
  1386. /* No need to enlarge memory */
  1387. return true;
  1388. if (total_page_count < cur_page_count) { /* integer overflow */
  1389. ret = false;
  1390. goto return_func;
  1391. }
  1392. if (total_page_count > max_page_count) {
  1393. failure_reason = MAX_SIZE_REACHED;
  1394. ret = false;
  1395. goto return_func;
  1396. }
  1397. #if WASM_ENABLE_SHARED_HEAP != 0
  1398. shared_heap = get_shared_heap(module);
  1399. if (shared_heap) {
  1400. if (memory->is_memory64
  1401. && total_size_new > shared_heap->start_off_mem64) {
  1402. LOG_WARNING("Linear memory address is overlapped with shared heap");
  1403. ret = false;
  1404. goto return_func;
  1405. }
  1406. else if (!memory->is_memory64
  1407. && total_size_new > shared_heap->start_off_mem32) {
  1408. LOG_WARNING("Linear memory address is overlapped with shared heap");
  1409. ret = false;
  1410. goto return_func;
  1411. }
  1412. }
  1413. #endif
  1414. bh_assert(total_size_new
  1415. <= GET_MAX_LINEAR_MEMORY_SIZE(memory->is_memory64));
  1416. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  1417. if (!(memory_data_new =
  1418. realloc_func(Alloc_For_LinearMemory, full_size_mmaped,
  1419. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  1420. allocator_user_data,
  1421. #endif
  1422. memory_data_old, total_size_new))) {
  1423. ret = false;
  1424. goto return_func;
  1425. }
  1426. if (heap_size > 0) {
  1427. if (mem_allocator_migrate(memory->heap_handle,
  1428. (char *)heap_data_old
  1429. + (memory_data_new - memory_data_old),
  1430. heap_size)
  1431. != 0) {
  1432. ret = false;
  1433. }
  1434. }
  1435. memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
  1436. memory->heap_data_end = memory->heap_data + heap_size;
  1437. memory->memory_data = memory_data_new;
  1438. #else
  1439. if (full_size_mmaped) {
  1440. #ifdef BH_PLATFORM_WINDOWS
  1441. if (!os_mem_commit(memory->memory_data_end,
  1442. total_size_new - total_size_old,
  1443. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  1444. ret = false;
  1445. goto return_func;
  1446. }
  1447. #endif
  1448. if (os_mprotect(memory->memory_data_end,
  1449. total_size_new - total_size_old,
  1450. MMAP_PROT_READ | MMAP_PROT_WRITE)
  1451. != 0) {
  1452. #ifdef BH_PLATFORM_WINDOWS
  1453. os_mem_decommit(memory->memory_data_end,
  1454. total_size_new - total_size_old);
  1455. #endif
  1456. ret = false;
  1457. goto return_func;
  1458. }
  1459. }
  1460. else {
  1461. if (heap_size > 0) {
  1462. if (mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  1463. wasm_runtime_show_app_heap_corrupted_prompt();
  1464. ret = false;
  1465. goto return_func;
  1466. }
  1467. }
  1468. if (!(memory_data_new =
  1469. wasm_mremap_linear_memory(memory_data_old, total_size_old,
  1470. total_size_new, total_size_new))) {
  1471. ret = false;
  1472. goto return_func;
  1473. }
  1474. if (heap_size > 0) {
  1475. if (mem_allocator_migrate(memory->heap_handle,
  1476. (char *)heap_data_old
  1477. + (memory_data_new - memory_data_old),
  1478. heap_size)
  1479. != 0) {
  1480. /* Don't return here as memory->memory_data is obsolete and
  1481. must be updated to be correctly used later. */
  1482. ret = false;
  1483. }
  1484. }
  1485. memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
  1486. memory->heap_data_end = memory->heap_data + heap_size;
  1487. memory->memory_data = memory_data_new;
  1488. #if defined(os_writegsbase)
  1489. /* write base addr of linear memory to GS segment register */
  1490. os_writegsbase(memory_data_new);
  1491. #endif
  1492. }
  1493. #endif /* end of WASM_MEM_ALLOC_WITH_USAGE */
  1494. /*
  1495. * AOT compiler assumes at least 8 byte alignment.
  1496. * see aot_check_memory_overflow.
  1497. */
  1498. bh_assert(((uintptr_t)memory->memory_data & 0x7) == 0);
  1499. memory->num_bytes_per_page = num_bytes_per_page;
  1500. memory->cur_page_count = total_page_count;
  1501. memory->max_page_count = max_page_count;
  1502. SET_LINEAR_MEMORY_SIZE(memory, total_size_new);
  1503. memory->memory_data_end = memory->memory_data + total_size_new;
  1504. wasm_runtime_set_mem_bound_check_bytes(memory, total_size_new);
  1505. return_func:
  1506. if (!ret && module && enlarge_memory_error_cb) {
  1507. WASMExecEnv *exec_env = NULL;
  1508. #if WASM_ENABLE_INTERP != 0
  1509. if (module->module_type == Wasm_Module_Bytecode)
  1510. exec_env = ((WASMModuleInstance *)module)->cur_exec_env;
  1511. #endif
  1512. #if WASM_ENABLE_AOT != 0
  1513. if (module->module_type == Wasm_Module_AoT)
  1514. exec_env = ((AOTModuleInstance *)module)->cur_exec_env;
  1515. #endif
  1516. enlarge_memory_error_cb(inc_page_count, total_size_old, 0,
  1517. failure_reason, module, exec_env,
  1518. enlarge_memory_error_user_data);
  1519. }
  1520. return ret;
  1521. }
  1522. bool
  1523. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module_inst,
  1524. uint64 inc_page_count)
  1525. {
  1526. if (inc_page_count > UINT32_MAX) {
  1527. return false;
  1528. }
  1529. #if WASM_ENABLE_AOT != 0
  1530. if (module_inst->module_type == Wasm_Module_AoT) {
  1531. return aot_enlarge_memory((AOTModuleInstance *)module_inst,
  1532. (uint32)inc_page_count);
  1533. }
  1534. #endif
  1535. #if WASM_ENABLE_INTERP != 0
  1536. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1537. return wasm_enlarge_memory((WASMModuleInstance *)module_inst,
  1538. (uint32)inc_page_count);
  1539. }
  1540. #endif
  1541. return false;
  1542. }
  1543. void
  1544. wasm_runtime_set_enlarge_mem_error_callback(
  1545. const enlarge_memory_error_callback_t callback, void *user_data)
  1546. {
  1547. enlarge_memory_error_cb = callback;
  1548. enlarge_memory_error_user_data = user_data;
  1549. }
  1550. bool
  1551. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  1552. {
  1553. bool ret = false;
  1554. if (module->memory_count > 0) {
  1555. #if WASM_ENABLE_SHARED_MEMORY != 0
  1556. shared_memory_lock(module->memories[0]);
  1557. #endif
  1558. ret = wasm_enlarge_memory_internal((WASMModuleInstanceCommon *)module,
  1559. module->memories[0], inc_page_count);
  1560. #if WASM_ENABLE_SHARED_MEMORY != 0
  1561. shared_memory_unlock(module->memories[0]);
  1562. #endif
  1563. }
  1564. return ret;
  1565. }
  1566. bool
  1567. wasm_enlarge_memory_with_idx(WASMModuleInstance *module, uint32 inc_page_count,
  1568. uint32 memidx)
  1569. {
  1570. bool ret = false;
  1571. if (memidx < module->memory_count) {
  1572. #if WASM_ENABLE_SHARED_MEMORY != 0
  1573. shared_memory_lock(module->memories[memidx]);
  1574. #endif
  1575. ret = wasm_enlarge_memory_internal((WASMModuleInstanceCommon *)module,
  1576. module->memories[memidx],
  1577. inc_page_count);
  1578. #if WASM_ENABLE_SHARED_MEMORY != 0
  1579. shared_memory_unlock(module->memories[memidx]);
  1580. #endif
  1581. }
  1582. return ret;
  1583. }
  1584. WASMMemoryInstance *
  1585. wasm_runtime_lookup_memory(WASMModuleInstanceCommon *module_inst,
  1586. const char *name)
  1587. {
  1588. #if WASM_ENABLE_INTERP != 0
  1589. if (module_inst->module_type == Wasm_Module_Bytecode)
  1590. return wasm_lookup_memory((WASMModuleInstance *)module_inst, name);
  1591. #endif
  1592. #if WASM_ENABLE_AOT != 0
  1593. if (module_inst->module_type == Wasm_Module_AoT)
  1594. return aot_lookup_memory((WASMModuleInstance *)module_inst, name);
  1595. #endif
  1596. return NULL;
  1597. }
  1598. WASMMemoryInstance *
  1599. wasm_runtime_get_default_memory(WASMModuleInstanceCommon *module_inst)
  1600. {
  1601. #if WASM_ENABLE_INTERP != 0
  1602. if (module_inst->module_type == Wasm_Module_Bytecode)
  1603. return wasm_get_default_memory((WASMModuleInstance *)module_inst);
  1604. #endif
  1605. #if WASM_ENABLE_AOT != 0
  1606. if (module_inst->module_type == Wasm_Module_AoT)
  1607. return aot_get_default_memory((AOTModuleInstance *)module_inst);
  1608. #endif
  1609. return NULL;
  1610. }
  1611. WASMMemoryInstance *
  1612. wasm_runtime_get_memory(WASMModuleInstanceCommon *module_inst, uint32 index)
  1613. {
  1614. #if WASM_ENABLE_INTERP != 0
  1615. if (module_inst->module_type == Wasm_Module_Bytecode)
  1616. return wasm_get_memory_with_idx((WASMModuleInstance *)module_inst,
  1617. index);
  1618. #endif
  1619. #if WASM_ENABLE_AOT != 0
  1620. if (module_inst->module_type == Wasm_Module_AoT)
  1621. return aot_get_memory_with_idx((AOTModuleInstance *)module_inst, index);
  1622. #endif
  1623. return NULL;
  1624. }
  1625. uint64
  1626. wasm_memory_get_cur_page_count(WASMMemoryInstance *memory)
  1627. {
  1628. return memory->cur_page_count;
  1629. }
  1630. uint64
  1631. wasm_memory_get_max_page_count(WASMMemoryInstance *memory)
  1632. {
  1633. return memory->max_page_count;
  1634. }
  1635. uint64
  1636. wasm_memory_get_bytes_per_page(WASMMemoryInstance *memory)
  1637. {
  1638. return memory->num_bytes_per_page;
  1639. }
  1640. bool
  1641. wasm_memory_get_shared(WASMMemoryInstance *memory)
  1642. {
  1643. return memory->is_shared_memory;
  1644. }
  1645. void *
  1646. wasm_memory_get_base_address(WASMMemoryInstance *memory)
  1647. {
  1648. return memory->memory_data;
  1649. }
  1650. bool
  1651. wasm_memory_enlarge(WASMMemoryInstance *memory, uint64 inc_page_count)
  1652. {
  1653. bool ret = false;
  1654. if (memory) {
  1655. #if WASM_ENABLE_SHARED_MEMORY != 0
  1656. shared_memory_lock(memory);
  1657. #endif
  1658. ret =
  1659. wasm_enlarge_memory_internal(NULL, memory, (uint32)inc_page_count);
  1660. #if WASM_ENABLE_SHARED_MEMORY != 0
  1661. shared_memory_unlock(memory);
  1662. #endif
  1663. }
  1664. return ret;
  1665. }
  1666. void
  1667. wasm_deallocate_linear_memory(WASMMemoryInstance *memory_inst)
  1668. {
  1669. uint64 map_size;
  1670. bh_assert(memory_inst);
  1671. bh_assert(memory_inst->memory_data);
  1672. #ifndef OS_ENABLE_HW_BOUND_CHECK
  1673. #if WASM_ENABLE_SHARED_MEMORY != 0
  1674. if (shared_memory_is_shared(memory_inst)) {
  1675. map_size = (uint64)memory_inst->num_bytes_per_page
  1676. * memory_inst->max_page_count;
  1677. }
  1678. else
  1679. #endif
  1680. {
  1681. map_size = (uint64)memory_inst->num_bytes_per_page
  1682. * memory_inst->cur_page_count;
  1683. }
  1684. #else
  1685. map_size = 8 * (uint64)BH_GB;
  1686. #endif
  1687. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  1688. (void)map_size;
  1689. free_func(Alloc_For_LinearMemory,
  1690. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  1691. allocator_user_data,
  1692. #endif
  1693. memory_inst->memory_data);
  1694. #else
  1695. wasm_munmap_linear_memory(memory_inst->memory_data,
  1696. memory_inst->memory_data_size, map_size);
  1697. #endif
  1698. memory_inst->memory_data = NULL;
  1699. }
  1700. int
  1701. wasm_allocate_linear_memory(uint8 **data, bool is_shared_memory,
  1702. bool is_memory64, uint64 num_bytes_per_page,
  1703. uint64 init_page_count, uint64 max_page_count,
  1704. uint64 *memory_data_size)
  1705. {
  1706. uint64 map_size, page_size;
  1707. bh_assert(data);
  1708. bh_assert(memory_data_size);
  1709. #ifndef OS_ENABLE_HW_BOUND_CHECK
  1710. #if WASM_ENABLE_SHARED_MEMORY != 0
  1711. if (is_shared_memory) {
  1712. /* Allocate maximum memory size when memory is shared */
  1713. map_size = max_page_count * num_bytes_per_page;
  1714. }
  1715. else
  1716. #endif
  1717. {
  1718. map_size = init_page_count * num_bytes_per_page;
  1719. }
  1720. #else /* else of OS_ENABLE_HW_BOUND_CHECK */
  1721. /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
  1722. * ea = i + memarg.offset
  1723. * both i and memarg.offset are u32 in range 0 to 4G
  1724. * so the range of ea is 0 to 8G
  1725. */
  1726. map_size = 8 * (uint64)BH_GB;
  1727. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  1728. page_size = os_getpagesize();
  1729. *memory_data_size = init_page_count * num_bytes_per_page;
  1730. bh_assert(*memory_data_size <= GET_MAX_LINEAR_MEMORY_SIZE(is_memory64));
  1731. *memory_data_size = align_as_and_cast(*memory_data_size, page_size);
  1732. if (map_size > 0) {
  1733. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  1734. (void)wasm_mmap_linear_memory;
  1735. if (!(*data = malloc_func(Alloc_For_LinearMemory,
  1736. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  1737. allocator_user_data,
  1738. #endif
  1739. *memory_data_size))) {
  1740. return BHT_ERROR;
  1741. }
  1742. #else
  1743. if (!(*data = wasm_mmap_linear_memory(map_size, *memory_data_size))) {
  1744. return BHT_ERROR;
  1745. }
  1746. #endif
  1747. }
  1748. /*
  1749. * AOT compiler assumes at least 8 byte alignment.
  1750. * see aot_check_memory_overflow.
  1751. */
  1752. bh_assert(((uintptr_t)*data & 0x7) == 0);
  1753. return BHT_OK;
  1754. }