wasm_memory.c 60 KB

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