wasm_memory.c 49 KB

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