wasm_memory.c 60 KB

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