wasm_memory.c 49 KB

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