wasm_memory.c 44 KB

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