wasm_memory.c 58 KB

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