wasm_memory.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  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_t 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. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  131. {
  132. if (error_buf != NULL) {
  133. snprintf(error_buf, error_buf_size,
  134. "Operation of shared heap failed: %s", string);
  135. }
  136. }
  137. static void *
  138. runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  139. {
  140. void *mem;
  141. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  142. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  143. return NULL;
  144. }
  145. memset(mem, 0, (uint32)size);
  146. return mem;
  147. }
  148. WASMSharedHeap *
  149. wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args, char *error_buf,
  150. uint32 error_buf_size)
  151. {
  152. uint64 heap_struct_size = sizeof(WASMSharedHeap);
  153. uint32 size = init_args->size;
  154. WASMSharedHeap *heap;
  155. if (!(heap = runtime_malloc(heap_struct_size, error_buf, error_buf_size))) {
  156. goto fail1;
  157. }
  158. if (!(heap->heap_handle =
  159. runtime_malloc(mem_allocator_get_heap_struct_size(), error_buf,
  160. error_buf_size))) {
  161. goto fail2;
  162. }
  163. heap->start_off_mem64 = UINT64_MAX - heap->size + 1;
  164. heap->start_off_mem32 = UINT32_MAX - heap->size + 1;
  165. size = align_uint(size, os_getpagesize());
  166. if (size > APP_HEAP_SIZE_MAX || size < APP_HEAP_SIZE_MIN) {
  167. set_error_buf(error_buf, error_buf_size, "invalid size of shared heap");
  168. goto fail3;
  169. }
  170. if (!(heap->base_addr = wasm_mmap_linear_memory(size, size))) {
  171. goto fail3;
  172. }
  173. if (!mem_allocator_create_with_struct_and_pool(
  174. heap->heap_handle, heap_struct_size, heap->base_addr, size)) {
  175. set_error_buf(error_buf, error_buf_size, "init share heap failed");
  176. goto fail4;
  177. }
  178. os_mutex_lock(&shared_heap_list_lock);
  179. if (shared_heap_list == NULL) {
  180. shared_heap_list = heap;
  181. }
  182. else {
  183. heap->next = shared_heap_list;
  184. shared_heap_list = heap;
  185. }
  186. os_mutex_unlock(&shared_heap_list_lock);
  187. return heap;
  188. fail4:
  189. wasm_munmap_linear_memory(heap->base_addr, size, size);
  190. fail3:
  191. wasm_runtime_free(heap->heap_handle);
  192. fail2:
  193. wasm_runtime_free(heap);
  194. fail1:
  195. return NULL;
  196. }
  197. bool
  198. wasm_runtime_attach_shared_heap_internal(WASMModuleInstanceCommon *module_inst,
  199. WASMSharedHeap *shared_heap)
  200. {
  201. WASMMemoryInstance *memory =
  202. wasm_get_default_memory((WASMModuleInstance *)module_inst);
  203. uint64 linear_mem_size;
  204. if (!memory)
  205. return false;
  206. linear_mem_size = memory->memory_data_size;
  207. /* check if linear memory and shared heap are overlapped */
  208. if ((memory->is_memory64 && linear_mem_size > shared_heap->start_off_mem64)
  209. || (!memory->is_memory64
  210. && linear_mem_size > shared_heap->start_off_mem32)) {
  211. LOG_WARNING("Linear memory address is overlapped with shared heap");
  212. return false;
  213. }
  214. #if WASM_ENABLE_INTERP != 0
  215. if (module_inst->module_type == Wasm_Module_Bytecode) {
  216. if (((WASMModuleInstance *)module_inst)->e->shared_heap) {
  217. LOG_WARNING("A shared heap is already attached");
  218. return false;
  219. }
  220. ((WASMModuleInstance *)module_inst)->e->shared_heap = shared_heap;
  221. }
  222. #endif
  223. #if WASM_ENABLE_AOT != 0
  224. if (module_inst->module_type == Wasm_Module_AoT) {
  225. // TODO
  226. }
  227. #endif
  228. return true;
  229. }
  230. bool
  231. wasm_runtime_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
  232. WASMSharedHeap *shared_heap)
  233. {
  234. #if WASM_ENABLE_THREAD_MGR != 0
  235. return wasm_cluster_attach_shared_heap(module_inst, shared_heap);
  236. #else
  237. return wasm_runtime_attach_shared_heap_internal(module_inst, shared_heap);
  238. #endif
  239. }
  240. void
  241. wasm_runtime_detach_shared_heap_internal(WASMModuleInstanceCommon *module_inst)
  242. {
  243. #if WASM_ENABLE_INTERP != 0
  244. if (module_inst->module_type == Wasm_Module_Bytecode) {
  245. ((WASMModuleInstance *)module_inst)->e->shared_heap = NULL;
  246. }
  247. #endif
  248. #if WASM_ENABLE_AOT != 0
  249. if (module_inst->module_type == Wasm_Module_AoT) {
  250. // TODO
  251. }
  252. #endif
  253. }
  254. void
  255. wasm_runtime_detach_shared_heap(WASMModuleInstanceCommon *module_inst)
  256. {
  257. #if WASM_ENABLE_THREAD_MGR != 0
  258. wasm_cluster_detach_shared_heap(module_inst);
  259. #else
  260. wasm_runtime_detach_shared_heap_internal(module_inst);
  261. #endif
  262. }
  263. static WASMSharedHeap *
  264. get_shared_heap(WASMModuleInstanceCommon *module_inst_comm)
  265. {
  266. #if WASM_ENABLE_INTERP != 0
  267. if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
  268. return ((WASMModuleInstance *)module_inst_comm)->e->shared_heap;
  269. }
  270. #endif
  271. #if WASM_ENABLE_AOT != 0
  272. if (module_inst_comm->module_type == Wasm_Module_AoT) {
  273. // TODO
  274. return NULL;
  275. }
  276. #endif
  277. return NULL;
  278. }
  279. static bool
  280. is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
  281. bool is_memory64, uint64 app_offset, uint32 bytes)
  282. {
  283. WASMSharedHeap *heap = get_shared_heap(module_inst);
  284. if (!heap) {
  285. return false;
  286. }
  287. if (!is_memory64) {
  288. if (app_offset >= heap->start_off_mem32
  289. && app_offset <= UINT32_MAX - bytes + 1) {
  290. return true;
  291. }
  292. }
  293. else {
  294. if (app_offset >= heap->start_off_mem64
  295. && app_offset <= UINT64_MAX - bytes + 1) {
  296. return true;
  297. }
  298. }
  299. return false;
  300. }
  301. static bool
  302. is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
  303. uint8 *addr, uint32 bytes)
  304. {
  305. WASMSharedHeap *heap = get_shared_heap(module_inst);
  306. if (heap && addr >= heap->base_addr
  307. && addr + bytes <= heap->base_addr + heap->size
  308. && addr + bytes > addr) {
  309. return true;
  310. }
  311. return false;
  312. }
  313. uint64
  314. wasm_runtime_shared_heap_malloc(WASMModuleInstanceCommon *module_inst,
  315. uint64_t size, void **p_native_addr)
  316. {
  317. WASMMemoryInstance *memory =
  318. wasm_get_default_memory((WASMModuleInstance *)module_inst);
  319. WASMSharedHeap *shared_heap = get_shared_heap(module_inst);
  320. if (!memory || !shared_heap)
  321. return 0;
  322. *p_native_addr = mem_allocator_malloc(shared_heap->heap_handle, size);
  323. if (!*p_native_addr)
  324. return 0;
  325. if (memory->is_memory64)
  326. return shared_heap->start_off_mem64
  327. + ((uint8 *)*p_native_addr - shared_heap->base_addr);
  328. else
  329. return shared_heap->start_off_mem32
  330. + ((uint8 *)*p_native_addr - shared_heap->base_addr);
  331. }
  332. void
  333. wasm_runtime_shared_heap_free(WASMModuleInstanceCommon *module_inst, uint64 ptr)
  334. {
  335. WASMMemoryInstance *memory =
  336. wasm_get_default_memory((WASMModuleInstance *)module_inst);
  337. WASMSharedHeap *shared_heap = get_shared_heap(module_inst);
  338. uint8 *addr = NULL;
  339. if (!memory || !shared_heap) {
  340. return;
  341. }
  342. if (memory->is_memory64) {
  343. if (ptr < shared_heap->start_off_mem64) { /* ptr can not > UINT64_MAX */
  344. LOG_WARNING("The address to free isn't in shared heap");
  345. return;
  346. }
  347. addr = shared_heap->base_addr + (ptr - shared_heap->start_off_mem64);
  348. }
  349. else {
  350. if (ptr < shared_heap->start_off_mem32 || ptr > UINT32_MAX) {
  351. LOG_WARNING("The address to free isn't in shared heap");
  352. return;
  353. }
  354. addr = shared_heap->base_addr + (ptr - shared_heap->start_off_mem32);
  355. }
  356. mem_allocator_free(shared_heap->heap_handle, addr);
  357. }
  358. #endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */
  359. bool
  360. wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
  361. const MemAllocOption *alloc_option)
  362. {
  363. bool ret = false;
  364. #if WASM_ENABLE_SHARED_HEAP != 0
  365. if (os_mutex_init(&shared_heap_list_lock)) {
  366. return false;
  367. }
  368. #endif
  369. if (mem_alloc_type == Alloc_With_Pool) {
  370. ret = wasm_memory_init_with_pool(alloc_option->pool.heap_buf,
  371. alloc_option->pool.heap_size);
  372. }
  373. else if (mem_alloc_type == Alloc_With_Allocator) {
  374. ret = wasm_memory_init_with_allocator(
  375. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  376. alloc_option->allocator.user_data,
  377. #endif
  378. alloc_option->allocator.malloc_func,
  379. alloc_option->allocator.realloc_func,
  380. alloc_option->allocator.free_func);
  381. }
  382. else if (mem_alloc_type == Alloc_With_System_Allocator) {
  383. memory_mode = MEMORY_MODE_SYSTEM_ALLOCATOR;
  384. ret = true;
  385. }
  386. else {
  387. ret = false;
  388. }
  389. #if WASM_ENABLE_SHARED_HEAP != 0
  390. if (!ret) {
  391. os_mutex_destroy(&shared_heap_list_lock);
  392. }
  393. #endif
  394. return ret;
  395. }
  396. #if WASM_ENABLE_SHARED_HEAP != 0
  397. static void
  398. wasm_runtime_destroy_shared_heaps()
  399. {
  400. WASMSharedHeap *heap = shared_heap_list;
  401. WASMSharedHeap *cur;
  402. while (heap) {
  403. cur = heap;
  404. heap = heap->next;
  405. mem_allocator_destroy(cur->heap_handle);
  406. wasm_runtime_free(cur->heap_handle);
  407. wasm_munmap_linear_memory(cur->base_addr, cur->size, cur->size);
  408. wasm_runtime_free(cur);
  409. }
  410. }
  411. #endif
  412. void
  413. wasm_runtime_memory_destroy(void)
  414. {
  415. #if WASM_ENABLE_SHARED_HEAP != 0
  416. wasm_runtime_destroy_shared_heaps();
  417. #endif
  418. if (memory_mode == MEMORY_MODE_POOL) {
  419. #if BH_ENABLE_GC_VERIFY == 0
  420. (void)mem_allocator_destroy(pool_allocator);
  421. #else
  422. int ret = mem_allocator_destroy(pool_allocator);
  423. if (ret != 0) {
  424. /* Memory leak detected */
  425. exit(-1);
  426. }
  427. #endif
  428. }
  429. memory_mode = MEMORY_MODE_UNKNOWN;
  430. }
  431. unsigned
  432. wasm_runtime_memory_pool_size(void)
  433. {
  434. if (memory_mode == MEMORY_MODE_POOL)
  435. return global_pool_size;
  436. else
  437. return UINT32_MAX;
  438. }
  439. static inline void *
  440. wasm_runtime_malloc_internal(unsigned int size)
  441. {
  442. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  443. LOG_WARNING(
  444. "wasm_runtime_malloc failed: memory hasn't been initialized.\n");
  445. return NULL;
  446. }
  447. else if (memory_mode == MEMORY_MODE_POOL) {
  448. return mem_allocator_malloc(pool_allocator, size);
  449. }
  450. else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
  451. return malloc_func(
  452. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  453. Alloc_For_Runtime,
  454. #endif
  455. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  456. allocator_user_data,
  457. #endif
  458. size);
  459. }
  460. else {
  461. return os_malloc(size);
  462. }
  463. }
  464. static inline void *
  465. wasm_runtime_realloc_internal(void *ptr, unsigned int size)
  466. {
  467. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  468. LOG_WARNING(
  469. "wasm_runtime_realloc failed: memory hasn't been initialized.\n");
  470. return NULL;
  471. }
  472. else if (memory_mode == MEMORY_MODE_POOL) {
  473. return mem_allocator_realloc(pool_allocator, ptr, size);
  474. }
  475. else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
  476. if (realloc_func)
  477. return realloc_func(
  478. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  479. Alloc_For_Runtime, false,
  480. #endif
  481. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  482. allocator_user_data,
  483. #endif
  484. ptr, size);
  485. else
  486. return NULL;
  487. }
  488. else {
  489. return os_realloc(ptr, size);
  490. }
  491. }
  492. static inline void
  493. wasm_runtime_free_internal(void *ptr)
  494. {
  495. if (!ptr) {
  496. LOG_WARNING("warning: wasm_runtime_free with NULL pointer\n");
  497. #if BH_ENABLE_GC_VERIFY != 0
  498. exit(-1);
  499. #endif
  500. return;
  501. }
  502. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  503. LOG_WARNING("warning: wasm_runtime_free failed: "
  504. "memory hasn't been initialize.\n");
  505. }
  506. else if (memory_mode == MEMORY_MODE_POOL) {
  507. mem_allocator_free(pool_allocator, ptr);
  508. }
  509. else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
  510. free_func(
  511. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  512. Alloc_For_Runtime,
  513. #endif
  514. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  515. allocator_user_data,
  516. #endif
  517. ptr);
  518. }
  519. else {
  520. os_free(ptr);
  521. }
  522. }
  523. void *
  524. wasm_runtime_malloc(unsigned int size)
  525. {
  526. if (size == 0) {
  527. LOG_WARNING("warning: wasm_runtime_malloc with size zero\n");
  528. /* At lease alloc 1 byte to avoid malloc failed */
  529. size = 1;
  530. #if BH_ENABLE_GC_VERIFY != 0
  531. exit(-1);
  532. #endif
  533. }
  534. #if WASM_ENABLE_FUZZ_TEST != 0
  535. if (size >= WASM_MEM_ALLOC_MAX_SIZE) {
  536. LOG_WARNING("warning: wasm_runtime_malloc with too large size\n");
  537. return NULL;
  538. }
  539. #endif
  540. return wasm_runtime_malloc_internal(size);
  541. }
  542. void *
  543. wasm_runtime_realloc(void *ptr, unsigned int size)
  544. {
  545. return wasm_runtime_realloc_internal(ptr, size);
  546. }
  547. void
  548. wasm_runtime_free(void *ptr)
  549. {
  550. wasm_runtime_free_internal(ptr);
  551. }
  552. bool
  553. wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info)
  554. {
  555. if (memory_mode == MEMORY_MODE_POOL) {
  556. return mem_allocator_get_alloc_info(pool_allocator, mem_alloc_info);
  557. }
  558. return false;
  559. }
  560. bool
  561. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst_comm,
  562. uint64 app_offset, uint64 size)
  563. {
  564. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  565. WASMMemoryInstance *memory_inst;
  566. uint64 max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
  567. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  568. || module_inst_comm->module_type == Wasm_Module_AoT);
  569. if (!is_bounds_checks_enabled(module_inst_comm)) {
  570. return true;
  571. }
  572. memory_inst = wasm_get_default_memory(module_inst);
  573. if (!memory_inst) {
  574. goto fail;
  575. }
  576. #if WASM_ENABLE_SHARED_HEAP != 0
  577. if (is_app_addr_in_shared_heap(module_inst_comm, memory_inst->is_memory64,
  578. app_offset, size)) {
  579. return true;
  580. }
  581. #endif
  582. #if WASM_ENABLE_MEMORY64 != 0
  583. if (memory_inst->is_memory64)
  584. max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
  585. #endif
  586. /* boundary overflow check */
  587. if (size > max_linear_memory_size
  588. || app_offset > max_linear_memory_size - size) {
  589. goto fail;
  590. }
  591. SHARED_MEMORY_LOCK(memory_inst);
  592. if (app_offset + size <= memory_inst->memory_data_size) {
  593. SHARED_MEMORY_UNLOCK(memory_inst);
  594. return true;
  595. }
  596. SHARED_MEMORY_UNLOCK(memory_inst);
  597. fail:
  598. wasm_set_exception(module_inst, "out of bounds memory access");
  599. return false;
  600. }
  601. bool
  602. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst_comm,
  603. uint64 app_str_offset)
  604. {
  605. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  606. WASMMemoryInstance *memory_inst;
  607. uint64 app_end_offset, max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
  608. char *str, *str_end;
  609. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  610. || module_inst_comm->module_type == Wasm_Module_AoT);
  611. if (!is_bounds_checks_enabled(module_inst_comm)) {
  612. return true;
  613. }
  614. memory_inst = wasm_get_default_memory(module_inst);
  615. if (!memory_inst) {
  616. goto fail;
  617. }
  618. #if WASM_ENABLE_SHARED_HEAP != 0
  619. if (is_app_addr_in_shared_heap(module_inst_comm, memory_inst->is_memory64,
  620. app_str_offset, 1)) {
  621. WASMSharedHeap *shared_heap = get_shared_heap(module_inst_comm);
  622. str = (char *)shared_heap->base_addr
  623. + (memory_inst->is_memory64
  624. ? (app_str_offset - shared_heap->start_off_mem64)
  625. : (app_str_offset - shared_heap->start_off_mem32));
  626. str_end = (char *)shared_heap->base_addr + shared_heap->size;
  627. }
  628. else
  629. #endif
  630. {
  631. if (!wasm_runtime_get_app_addr_range(module_inst_comm, app_str_offset,
  632. NULL, &app_end_offset))
  633. goto fail;
  634. #if WASM_ENABLE_MEMORY64 != 0
  635. if (memory_inst->is_memory64)
  636. max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
  637. #endif
  638. /* boundary overflow check, max start offset can be size - 1, while end
  639. offset can be size */
  640. if (app_str_offset >= max_linear_memory_size
  641. || app_end_offset > max_linear_memory_size)
  642. goto fail;
  643. str = wasm_runtime_addr_app_to_native(module_inst_comm, app_str_offset);
  644. str_end = str + (app_end_offset - app_str_offset);
  645. }
  646. while (str < str_end && *str != '\0')
  647. str++;
  648. if (str == str_end)
  649. goto fail;
  650. return true;
  651. fail:
  652. wasm_set_exception(module_inst, "out of bounds memory access");
  653. return false;
  654. }
  655. bool
  656. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst_comm,
  657. void *native_ptr, uint64 size)
  658. {
  659. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  660. WASMMemoryInstance *memory_inst;
  661. uint8 *addr = (uint8 *)native_ptr;
  662. uint64 max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
  663. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  664. || module_inst_comm->module_type == Wasm_Module_AoT);
  665. if (!is_bounds_checks_enabled(module_inst_comm)) {
  666. return true;
  667. }
  668. memory_inst = wasm_get_default_memory(module_inst);
  669. if (!memory_inst) {
  670. goto fail;
  671. }
  672. #if WASM_ENABLE_MEMORY64 != 0
  673. if (memory_inst->is_memory64)
  674. max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
  675. #endif
  676. /* boundary overflow check */
  677. if (size > max_linear_memory_size || (uintptr_t)addr > UINTPTR_MAX - size) {
  678. goto fail;
  679. }
  680. #if WASM_ENABLE_SHARED_HEAP != 0
  681. if (is_native_addr_in_shared_heap(module_inst_comm, native_ptr, size)) {
  682. return true;
  683. }
  684. #endif
  685. SHARED_MEMORY_LOCK(memory_inst);
  686. if (memory_inst->memory_data <= addr
  687. && addr + size <= memory_inst->memory_data_end) {
  688. SHARED_MEMORY_UNLOCK(memory_inst);
  689. return true;
  690. }
  691. SHARED_MEMORY_UNLOCK(memory_inst);
  692. fail:
  693. wasm_set_exception(module_inst, "out of bounds memory access");
  694. return false;
  695. }
  696. void *
  697. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst_comm,
  698. uint64 app_offset)
  699. {
  700. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  701. WASMMemoryInstance *memory_inst;
  702. uint8 *addr;
  703. bool bounds_checks;
  704. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  705. || module_inst_comm->module_type == Wasm_Module_AoT);
  706. bounds_checks = is_bounds_checks_enabled(module_inst_comm);
  707. memory_inst = wasm_get_default_memory(module_inst);
  708. if (!memory_inst) {
  709. return NULL;
  710. }
  711. #if WASM_ENABLE_SHARED_HEAP != 0
  712. if (is_app_addr_in_shared_heap(module_inst_comm, memory_inst->is_memory64,
  713. app_offset, 1)) {
  714. WASMSharedHeap *shared_heap = get_shared_heap(module_inst_comm);
  715. uint64 shared_heap_start = 0;
  716. if (memory_inst && !memory_inst->is_memory64) {
  717. shared_heap_start = shared_heap->start_off_mem32;
  718. }
  719. else if (memory_inst && memory_inst->is_memory64) {
  720. shared_heap_start = shared_heap->start_off_mem64;
  721. }
  722. return shared_heap->base_addr + app_offset - shared_heap_start;
  723. }
  724. #endif
  725. SHARED_MEMORY_LOCK(memory_inst);
  726. addr = memory_inst->memory_data + (uintptr_t)app_offset;
  727. if (bounds_checks) {
  728. if (memory_inst->memory_data <= addr
  729. && addr < memory_inst->memory_data_end) {
  730. SHARED_MEMORY_UNLOCK(memory_inst);
  731. return addr;
  732. }
  733. SHARED_MEMORY_UNLOCK(memory_inst);
  734. return NULL;
  735. }
  736. /* If bounds checks is disabled, return the address directly */
  737. SHARED_MEMORY_UNLOCK(memory_inst);
  738. return addr;
  739. }
  740. uint64
  741. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst_comm,
  742. void *native_ptr)
  743. {
  744. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  745. WASMMemoryInstance *memory_inst;
  746. uint8 *addr = (uint8 *)native_ptr;
  747. bool bounds_checks;
  748. uint64 ret;
  749. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  750. || module_inst_comm->module_type == Wasm_Module_AoT);
  751. bounds_checks = is_bounds_checks_enabled(module_inst_comm);
  752. #if WASM_ENABLE_SHARED_HEAP != 0
  753. /* If shared heap is enabled, bounds check is always needed */
  754. bounds_checks = true;
  755. #endif
  756. memory_inst = wasm_get_default_memory(module_inst);
  757. if (!memory_inst) {
  758. return 0;
  759. }
  760. #if WASM_ENABLE_SHARED_HEAP != 0
  761. if (is_native_addr_in_shared_heap(module_inst_comm, addr, 1)) {
  762. WASMSharedHeap *shared_heap = get_shared_heap(module_inst_comm);
  763. uint64 shared_heap_start = 0;
  764. if (memory_inst && !memory_inst->is_memory64) {
  765. shared_heap_start = shared_heap->start_off_mem32;
  766. }
  767. else if (memory_inst && memory_inst->is_memory64) {
  768. shared_heap_start = shared_heap->start_off_mem64;
  769. }
  770. return shared_heap_start + (addr - shared_heap->base_addr);
  771. }
  772. #endif
  773. SHARED_MEMORY_LOCK(memory_inst);
  774. if (bounds_checks) {
  775. if (memory_inst->memory_data <= addr
  776. && addr < memory_inst->memory_data_end) {
  777. ret = (uint64)(addr - memory_inst->memory_data);
  778. SHARED_MEMORY_UNLOCK(memory_inst);
  779. return ret;
  780. }
  781. }
  782. /* If bounds checks is disabled, return the offset directly */
  783. else if (addr != NULL) {
  784. ret = (uint64)(addr - memory_inst->memory_data);
  785. SHARED_MEMORY_UNLOCK(memory_inst);
  786. return ret;
  787. }
  788. SHARED_MEMORY_UNLOCK(memory_inst);
  789. return 0;
  790. }
  791. bool
  792. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst_comm,
  793. uint64 app_offset, uint64 *p_app_start_offset,
  794. uint64 *p_app_end_offset)
  795. {
  796. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  797. WASMMemoryInstance *memory_inst;
  798. uint64 memory_data_size;
  799. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  800. || module_inst_comm->module_type == Wasm_Module_AoT);
  801. memory_inst = wasm_get_default_memory(module_inst);
  802. if (!memory_inst) {
  803. return false;
  804. }
  805. SHARED_MEMORY_LOCK(memory_inst);
  806. memory_data_size = memory_inst->memory_data_size;
  807. if (app_offset < memory_data_size) {
  808. if (p_app_start_offset)
  809. *p_app_start_offset = 0;
  810. if (p_app_end_offset)
  811. *p_app_end_offset = memory_data_size;
  812. SHARED_MEMORY_UNLOCK(memory_inst);
  813. return true;
  814. }
  815. SHARED_MEMORY_UNLOCK(memory_inst);
  816. return false;
  817. }
  818. bool
  819. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst_comm,
  820. uint8 *native_ptr,
  821. uint8 **p_native_start_addr,
  822. uint8 **p_native_end_addr)
  823. {
  824. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  825. WASMMemoryInstance *memory_inst;
  826. uint8 *addr = (uint8 *)native_ptr;
  827. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  828. || module_inst_comm->module_type == Wasm_Module_AoT);
  829. memory_inst = wasm_get_default_memory(module_inst);
  830. if (!memory_inst) {
  831. return false;
  832. }
  833. SHARED_MEMORY_LOCK(memory_inst);
  834. if (memory_inst->memory_data <= addr
  835. && addr < memory_inst->memory_data_end) {
  836. if (p_native_start_addr)
  837. *p_native_start_addr = memory_inst->memory_data;
  838. if (p_native_end_addr)
  839. *p_native_end_addr = memory_inst->memory_data_end;
  840. SHARED_MEMORY_UNLOCK(memory_inst);
  841. return true;
  842. }
  843. SHARED_MEMORY_UNLOCK(memory_inst);
  844. return false;
  845. }
  846. bool
  847. wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  848. uint64 app_buf_addr, uint64 app_buf_size,
  849. void **p_native_addr)
  850. {
  851. WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
  852. uint8 *native_addr;
  853. bool bounds_checks;
  854. #if WASM_ENABLE_SHARED_HEAP != 0
  855. WASMSharedHeap *shared_heap;
  856. bool is_in_shared_heap = false;
  857. #endif
  858. bh_assert(app_buf_addr <= UINTPTR_MAX && app_buf_size <= UINTPTR_MAX);
  859. if (!memory_inst) {
  860. wasm_set_exception(module_inst, "out of bounds memory access");
  861. return false;
  862. }
  863. #if WASM_ENABLE_SHARED_HEAP != 0
  864. if (is_app_addr_in_shared_heap((WASMModuleInstanceCommon *)module_inst,
  865. memory_inst->is_memory64, app_buf_addr,
  866. app_buf_size)) {
  867. shared_heap = get_shared_heap((WASMModuleInstanceCommon *)module_inst);
  868. native_addr = shared_heap->base_addr
  869. + (memory_inst->is_memory64
  870. ? (app_buf_addr - shared_heap->start_off_mem64)
  871. : (app_buf_addr - shared_heap->start_off_mem32));
  872. is_in_shared_heap = true;
  873. }
  874. else
  875. #endif
  876. {
  877. native_addr = memory_inst->memory_data + (uintptr_t)app_buf_addr;
  878. }
  879. bounds_checks =
  880. is_bounds_checks_enabled((WASMModuleInstanceCommon *)module_inst);
  881. if (!bounds_checks) {
  882. if (app_buf_addr == 0) {
  883. native_addr = NULL;
  884. }
  885. goto success;
  886. }
  887. #if WASM_ENABLE_SHARED_HEAP != 0
  888. if (is_in_shared_heap) {
  889. const char *str, *str_end;
  890. /* The whole string must be in the linear memory */
  891. str = (const char *)native_addr;
  892. str_end = (const char *)shared_heap->base_addr + shared_heap->size;
  893. while (str < str_end && *str != '\0')
  894. str++;
  895. if (str == str_end) {
  896. wasm_set_exception(module_inst, "out of bounds memory access");
  897. return false;
  898. }
  899. else
  900. goto success;
  901. }
  902. #endif
  903. /* No need to check the app_offset and buf_size if memory access
  904. boundary check with hardware trap is enabled */
  905. #ifndef OS_ENABLE_HW_BOUND_CHECK
  906. SHARED_MEMORY_LOCK(memory_inst);
  907. if (app_buf_addr >= memory_inst->memory_data_size) {
  908. goto fail;
  909. }
  910. if (!is_str) {
  911. if (app_buf_size > memory_inst->memory_data_size - app_buf_addr) {
  912. goto fail;
  913. }
  914. }
  915. else {
  916. const char *str, *str_end;
  917. /* The whole string must be in the linear memory */
  918. str = (const char *)native_addr;
  919. str_end = (const char *)memory_inst->memory_data_end;
  920. while (str < str_end && *str != '\0')
  921. str++;
  922. if (str == str_end)
  923. goto fail;
  924. }
  925. SHARED_MEMORY_UNLOCK(memory_inst);
  926. #endif
  927. success:
  928. *p_native_addr = (void *)native_addr;
  929. return true;
  930. #ifndef OS_ENABLE_HW_BOUND_CHECK
  931. fail:
  932. SHARED_MEMORY_UNLOCK(memory_inst);
  933. wasm_set_exception(module_inst, "out of bounds memory access");
  934. return false;
  935. #endif
  936. }
  937. WASMMemoryInstance *
  938. wasm_get_default_memory(WASMModuleInstance *module_inst)
  939. {
  940. if (module_inst->memories)
  941. return module_inst->memories[0];
  942. else
  943. return NULL;
  944. }
  945. WASMMemoryInstance *
  946. wasm_get_memory_with_idx(WASMModuleInstance *module_inst, uint32 index)
  947. {
  948. bh_assert(index < module_inst->memory_count);
  949. if (module_inst->memories)
  950. return module_inst->memories[index];
  951. else
  952. return NULL;
  953. }
  954. void
  955. wasm_runtime_set_mem_bound_check_bytes(WASMMemoryInstance *memory,
  956. uint64 memory_data_size)
  957. {
  958. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 || WASM_ENABLE_AOT != 0
  959. #if UINTPTR_MAX == UINT64_MAX
  960. memory->mem_bound_check_1byte.u64 = memory_data_size - 1;
  961. memory->mem_bound_check_2bytes.u64 = memory_data_size - 2;
  962. memory->mem_bound_check_4bytes.u64 = memory_data_size - 4;
  963. memory->mem_bound_check_8bytes.u64 = memory_data_size - 8;
  964. memory->mem_bound_check_16bytes.u64 = memory_data_size - 16;
  965. #else
  966. memory->mem_bound_check_1byte.u32[0] = (uint32)memory_data_size - 1;
  967. memory->mem_bound_check_2bytes.u32[0] = (uint32)memory_data_size - 2;
  968. memory->mem_bound_check_4bytes.u32[0] = (uint32)memory_data_size - 4;
  969. memory->mem_bound_check_8bytes.u32[0] = (uint32)memory_data_size - 8;
  970. memory->mem_bound_check_16bytes.u32[0] = (uint32)memory_data_size - 16;
  971. #endif
  972. #endif
  973. }
  974. static void
  975. wasm_munmap_linear_memory(void *mapped_mem, uint64 commit_size, uint64 map_size)
  976. {
  977. #ifdef BH_PLATFORM_WINDOWS
  978. os_mem_decommit(mapped_mem, commit_size);
  979. #else
  980. (void)commit_size;
  981. #endif
  982. os_munmap(mapped_mem, map_size);
  983. }
  984. static void *
  985. wasm_mremap_linear_memory(void *mapped_mem, uint64 old_size, uint64 new_size,
  986. uint64 commit_size)
  987. {
  988. void *new_mem;
  989. bh_assert(new_size > 0);
  990. bh_assert(new_size > old_size);
  991. if (mapped_mem) {
  992. new_mem = os_mremap(mapped_mem, old_size, new_size);
  993. }
  994. else {
  995. new_mem = os_mmap(NULL, new_size, MMAP_PROT_NONE, MMAP_MAP_NONE,
  996. os_get_invalid_handle());
  997. }
  998. if (!new_mem) {
  999. return NULL;
  1000. }
  1001. #ifdef BH_PLATFORM_WINDOWS
  1002. if (commit_size > 0
  1003. && !os_mem_commit(new_mem, commit_size,
  1004. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  1005. os_munmap(new_mem, new_size);
  1006. return NULL;
  1007. }
  1008. #endif
  1009. if (os_mprotect(new_mem, commit_size, MMAP_PROT_READ | MMAP_PROT_WRITE)
  1010. != 0) {
  1011. wasm_munmap_linear_memory(new_mem, new_size, new_size);
  1012. return NULL;
  1013. }
  1014. return new_mem;
  1015. }
  1016. static void *
  1017. wasm_mmap_linear_memory(uint64_t map_size, uint64 commit_size)
  1018. {
  1019. return wasm_mremap_linear_memory(NULL, 0, map_size, commit_size);
  1020. }
  1021. bool
  1022. wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count,
  1023. uint32 memidx)
  1024. {
  1025. #if WASM_ENABLE_MULTI_MEMORY != 0
  1026. WASMMemoryInstance *memory = wasm_get_memory_with_idx(module, memidx);
  1027. #else
  1028. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  1029. #endif
  1030. #if WASM_ENABLE_SHARED_HEAP != 0
  1031. WASMSharedHeap *shared_heap;
  1032. #endif
  1033. uint8 *memory_data_old, *memory_data_new, *heap_data_old;
  1034. uint32 num_bytes_per_page, heap_size;
  1035. uint32 cur_page_count, max_page_count, total_page_count;
  1036. uint64 total_size_old = 0, total_size_new;
  1037. bool ret = true, full_size_mmaped;
  1038. enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
  1039. if (!memory) {
  1040. ret = false;
  1041. goto return_func;
  1042. }
  1043. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1044. full_size_mmaped = true;
  1045. #elif WASM_ENABLE_SHARED_MEMORY != 0
  1046. full_size_mmaped = shared_memory_is_shared(memory);
  1047. #else
  1048. full_size_mmaped = false;
  1049. #endif
  1050. memory_data_old = memory->memory_data;
  1051. total_size_old = memory->memory_data_size;
  1052. heap_data_old = memory->heap_data;
  1053. heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
  1054. num_bytes_per_page = memory->num_bytes_per_page;
  1055. cur_page_count = memory->cur_page_count;
  1056. max_page_count = memory->max_page_count;
  1057. total_page_count = inc_page_count + cur_page_count;
  1058. total_size_new = num_bytes_per_page * (uint64)total_page_count;
  1059. #if WASM_ENABLE_SHARED_HEAP != 0
  1060. shared_heap = get_shared_heap((WASMModuleInstanceCommon *)module);
  1061. if (shared_heap) {
  1062. if (memory->is_memory64
  1063. && total_size_new > shared_heap->start_off_mem64) {
  1064. LOG_WARNING("Linear memory address is overlapped with shared heap");
  1065. ret = false;
  1066. goto return_func;
  1067. }
  1068. else if (!memory->is_memory64
  1069. && total_size_new > shared_heap->start_off_mem32) {
  1070. LOG_WARNING("Linear memory address is overlapped with shared heap");
  1071. ret = false;
  1072. goto return_func;
  1073. }
  1074. }
  1075. #endif
  1076. if (inc_page_count <= 0)
  1077. /* No need to enlarge memory */
  1078. return true;
  1079. if (total_page_count < cur_page_count) { /* integer overflow */
  1080. ret = false;
  1081. goto return_func;
  1082. }
  1083. if (total_page_count > max_page_count) {
  1084. failure_reason = MAX_SIZE_REACHED;
  1085. ret = false;
  1086. goto return_func;
  1087. }
  1088. bh_assert(total_size_new
  1089. <= GET_MAX_LINEAR_MEMORY_SIZE(memory->is_memory64));
  1090. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  1091. if (!(memory_data_new =
  1092. realloc_func(Alloc_For_LinearMemory, full_size_mmaped,
  1093. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  1094. NULL,
  1095. #endif
  1096. memory_data_old, total_size_new))) {
  1097. ret = false;
  1098. goto return_func;
  1099. }
  1100. if (heap_size > 0) {
  1101. if (mem_allocator_migrate(memory->heap_handle,
  1102. (char *)heap_data_old
  1103. + (memory_data_new - memory_data_old),
  1104. heap_size)
  1105. != 0) {
  1106. ret = false;
  1107. }
  1108. }
  1109. memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
  1110. memory->heap_data_end = memory->heap_data + heap_size;
  1111. memory->memory_data = memory_data_new;
  1112. #else
  1113. if (full_size_mmaped) {
  1114. #ifdef BH_PLATFORM_WINDOWS
  1115. if (!os_mem_commit(memory->memory_data_end,
  1116. (mem_offset_t)(total_size_new - total_size_old),
  1117. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  1118. ret = false;
  1119. goto return_func;
  1120. }
  1121. #endif
  1122. if (os_mprotect(memory->memory_data_end,
  1123. (mem_offset_t)(total_size_new - total_size_old),
  1124. MMAP_PROT_READ | MMAP_PROT_WRITE)
  1125. != 0) {
  1126. #ifdef BH_PLATFORM_WINDOWS
  1127. os_mem_decommit(memory->memory_data_end,
  1128. (mem_offset_t)(total_size_new - total_size_old));
  1129. #endif
  1130. ret = false;
  1131. goto return_func;
  1132. }
  1133. }
  1134. else {
  1135. if (heap_size > 0) {
  1136. if (mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  1137. wasm_runtime_show_app_heap_corrupted_prompt();
  1138. ret = false;
  1139. goto return_func;
  1140. }
  1141. }
  1142. if (!(memory_data_new =
  1143. wasm_mremap_linear_memory(memory_data_old, total_size_old,
  1144. total_size_new, total_size_new))) {
  1145. ret = false;
  1146. goto return_func;
  1147. }
  1148. if (heap_size > 0) {
  1149. if (mem_allocator_migrate(memory->heap_handle,
  1150. (char *)heap_data_old
  1151. + (memory_data_new - memory_data_old),
  1152. heap_size)
  1153. != 0) {
  1154. /* Don't return here as memory->memory_data is obsolete and
  1155. must be updated to be correctly used later. */
  1156. ret = false;
  1157. }
  1158. }
  1159. memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
  1160. memory->heap_data_end = memory->heap_data + heap_size;
  1161. memory->memory_data = memory_data_new;
  1162. #if defined(os_writegsbase)
  1163. /* write base addr of linear memory to GS segment register */
  1164. os_writegsbase(memory_data_new);
  1165. #endif
  1166. }
  1167. #endif /* end of WASM_MEM_ALLOC_WITH_USAGE */
  1168. /*
  1169. * AOT compiler assumes at least 8 byte alignment.
  1170. * see aot_check_memory_overflow.
  1171. */
  1172. bh_assert(((uintptr_t)memory->memory_data & 0x7) == 0);
  1173. memory->num_bytes_per_page = num_bytes_per_page;
  1174. memory->cur_page_count = total_page_count;
  1175. memory->max_page_count = max_page_count;
  1176. SET_LINEAR_MEMORY_SIZE(memory, total_size_new);
  1177. memory->memory_data_end = memory->memory_data + total_size_new;
  1178. wasm_runtime_set_mem_bound_check_bytes(memory, total_size_new);
  1179. return_func:
  1180. if (!ret && enlarge_memory_error_cb) {
  1181. WASMExecEnv *exec_env = NULL;
  1182. #if WASM_ENABLE_INTERP != 0
  1183. if (module->module_type == Wasm_Module_Bytecode)
  1184. exec_env = ((WASMModuleInstance *)module)->cur_exec_env;
  1185. #endif
  1186. #if WASM_ENABLE_AOT != 0
  1187. if (module->module_type == Wasm_Module_AoT)
  1188. exec_env = ((AOTModuleInstance *)module)->cur_exec_env;
  1189. #endif
  1190. enlarge_memory_error_cb(inc_page_count, total_size_old, 0,
  1191. failure_reason,
  1192. (WASMModuleInstanceCommon *)module, exec_env,
  1193. enlarge_memory_error_user_data);
  1194. }
  1195. return ret;
  1196. }
  1197. bool
  1198. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module_inst,
  1199. uint64 inc_page_count)
  1200. {
  1201. if (inc_page_count > UINT32_MAX) {
  1202. return false;
  1203. }
  1204. #if WASM_ENABLE_AOT != 0
  1205. if (module_inst->module_type == Wasm_Module_AoT) {
  1206. return aot_enlarge_memory((AOTModuleInstance *)module_inst,
  1207. (uint32)inc_page_count);
  1208. }
  1209. #endif
  1210. #if WASM_ENABLE_INTERP != 0
  1211. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1212. return wasm_enlarge_memory((WASMModuleInstance *)module_inst,
  1213. (uint32)inc_page_count);
  1214. }
  1215. #endif
  1216. return false;
  1217. }
  1218. void
  1219. wasm_runtime_set_enlarge_mem_error_callback(
  1220. const enlarge_memory_error_callback_t callback, void *user_data)
  1221. {
  1222. enlarge_memory_error_cb = callback;
  1223. enlarge_memory_error_user_data = user_data;
  1224. }
  1225. bool
  1226. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  1227. {
  1228. bool ret = false;
  1229. #if WASM_ENABLE_SHARED_MEMORY != 0
  1230. if (module->memory_count > 0)
  1231. shared_memory_lock(module->memories[0]);
  1232. #endif
  1233. ret = wasm_enlarge_memory_internal(module, inc_page_count, 0);
  1234. #if WASM_ENABLE_SHARED_MEMORY != 0
  1235. if (module->memory_count > 0)
  1236. shared_memory_unlock(module->memories[0]);
  1237. #endif
  1238. return ret;
  1239. }
  1240. bool
  1241. wasm_enlarge_memory_with_idx(WASMModuleInstance *module, uint32 inc_page_count,
  1242. uint32 memidx)
  1243. {
  1244. bool ret = false;
  1245. #if WASM_ENABLE_SHARED_MEMORY != 0
  1246. if (memidx < module->memory_count)
  1247. shared_memory_lock(module->memories[memidx]);
  1248. #endif
  1249. ret = wasm_enlarge_memory_internal(module, inc_page_count, memidx);
  1250. #if WASM_ENABLE_SHARED_MEMORY != 0
  1251. if (memidx < module->memory_count)
  1252. shared_memory_unlock(module->memories[memidx]);
  1253. #endif
  1254. return ret;
  1255. }
  1256. void
  1257. wasm_deallocate_linear_memory(WASMMemoryInstance *memory_inst)
  1258. {
  1259. uint64 map_size;
  1260. bh_assert(memory_inst);
  1261. bh_assert(memory_inst->memory_data);
  1262. #ifndef OS_ENABLE_HW_BOUND_CHECK
  1263. #if WASM_ENABLE_SHARED_MEMORY != 0
  1264. if (shared_memory_is_shared(memory_inst)) {
  1265. map_size = (uint64)memory_inst->num_bytes_per_page
  1266. * memory_inst->max_page_count;
  1267. }
  1268. else
  1269. #endif
  1270. {
  1271. map_size = (uint64)memory_inst->num_bytes_per_page
  1272. * memory_inst->cur_page_count;
  1273. }
  1274. #else
  1275. map_size = 8 * (uint64)BH_GB;
  1276. #endif
  1277. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  1278. (void)map_size;
  1279. free_func(Alloc_For_LinearMemory,
  1280. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  1281. NULL,
  1282. #endif
  1283. memory_inst->memory_data);
  1284. #else
  1285. wasm_munmap_linear_memory(memory_inst->memory_data,
  1286. memory_inst->memory_data_size, map_size);
  1287. #endif
  1288. memory_inst->memory_data = NULL;
  1289. }
  1290. int
  1291. wasm_allocate_linear_memory(uint8 **data, bool is_shared_memory,
  1292. bool is_memory64, uint64 num_bytes_per_page,
  1293. uint64 init_page_count, uint64 max_page_count,
  1294. uint64 *memory_data_size)
  1295. {
  1296. uint64 map_size, page_size;
  1297. bh_assert(data);
  1298. bh_assert(memory_data_size);
  1299. #ifndef OS_ENABLE_HW_BOUND_CHECK
  1300. #if WASM_ENABLE_SHARED_MEMORY != 0
  1301. if (is_shared_memory) {
  1302. /* Allocate maximum memory size when memory is shared */
  1303. map_size = max_page_count * num_bytes_per_page;
  1304. }
  1305. else
  1306. #endif
  1307. {
  1308. map_size = init_page_count * num_bytes_per_page;
  1309. }
  1310. #else /* else of OS_ENABLE_HW_BOUND_CHECK */
  1311. /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
  1312. * ea = i + memarg.offset
  1313. * both i and memarg.offset are u32 in range 0 to 4G
  1314. * so the range of ea is 0 to 8G
  1315. */
  1316. map_size = 8 * (uint64)BH_GB;
  1317. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  1318. page_size = os_getpagesize();
  1319. *memory_data_size = init_page_count * num_bytes_per_page;
  1320. bh_assert(*memory_data_size <= GET_MAX_LINEAR_MEMORY_SIZE(is_memory64));
  1321. *memory_data_size = align_as_and_cast(*memory_data_size, page_size);
  1322. if (map_size > 0) {
  1323. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  1324. (void)wasm_mmap_linear_memory;
  1325. if (!(*data = malloc_func(Alloc_For_LinearMemory,
  1326. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  1327. NULL,
  1328. #endif
  1329. *memory_data_size))) {
  1330. return BHT_ERROR;
  1331. }
  1332. #else
  1333. if (!(*data = wasm_mmap_linear_memory(map_size, *memory_data_size))) {
  1334. return BHT_ERROR;
  1335. }
  1336. #endif
  1337. }
  1338. /*
  1339. * AOT compiler assumes at least 8 byte alignment.
  1340. * see aot_check_memory_overflow.
  1341. */
  1342. bh_assert(((uintptr_t)*data & 0x7) == 0);
  1343. return BHT_OK;
  1344. }