wasm_memory.c 49 KB

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