wasm_memory.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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 "bh_platform.h"
  9. #include "mem_alloc.h"
  10. #include "wasm_memory.h"
  11. #if WASM_ENABLE_SHARED_MEMORY != 0
  12. #include "../common/wasm_shared_memory.h"
  13. #endif
  14. typedef enum Memory_Mode {
  15. MEMORY_MODE_UNKNOWN = 0,
  16. MEMORY_MODE_POOL,
  17. MEMORY_MODE_ALLOCATOR,
  18. MEMORY_MODE_SYSTEM_ALLOCATOR
  19. } Memory_Mode;
  20. static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
  21. static mem_allocator_t pool_allocator = NULL;
  22. static enlarge_memory_error_callback_t enlarge_memory_error_cb;
  23. static void *enlarge_memory_error_user_data;
  24. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  25. static void *allocator_user_data = NULL;
  26. static void *(*malloc_func)(void *user_data, unsigned int size) = NULL;
  27. static void *(*realloc_func)(void *user_data, void *ptr,
  28. unsigned int size) = NULL;
  29. static void (*free_func)(void *user_data, void *ptr) = NULL;
  30. #else
  31. static void *(*malloc_func)(unsigned int size) = NULL;
  32. static void *(*realloc_func)(void *ptr, unsigned int size) = NULL;
  33. static void (*free_func)(void *ptr) = NULL;
  34. #endif
  35. static unsigned int global_pool_size;
  36. static bool
  37. wasm_memory_init_with_pool(void *mem, unsigned int bytes)
  38. {
  39. mem_allocator_t _allocator = mem_allocator_create(mem, bytes);
  40. if (_allocator) {
  41. memory_mode = MEMORY_MODE_POOL;
  42. pool_allocator = _allocator;
  43. global_pool_size = bytes;
  44. return true;
  45. }
  46. LOG_ERROR("Init memory with pool (%p, %u) failed.\n", mem, bytes);
  47. return false;
  48. }
  49. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  50. static bool
  51. wasm_memory_init_with_allocator(void *_user_data, void *_malloc_func,
  52. void *_realloc_func, void *_free_func)
  53. {
  54. if (_malloc_func && _free_func && _malloc_func != _free_func) {
  55. memory_mode = MEMORY_MODE_ALLOCATOR;
  56. allocator_user_data = _user_data;
  57. malloc_func = _malloc_func;
  58. realloc_func = _realloc_func;
  59. free_func = _free_func;
  60. return true;
  61. }
  62. LOG_ERROR("Init memory with allocator (%p, %p, %p, %p) failed.\n",
  63. _user_data, _malloc_func, _realloc_func, _free_func);
  64. return false;
  65. }
  66. #else
  67. static bool
  68. wasm_memory_init_with_allocator(void *_malloc_func, void *_realloc_func,
  69. void *_free_func)
  70. {
  71. if (_malloc_func && _free_func && _malloc_func != _free_func) {
  72. memory_mode = MEMORY_MODE_ALLOCATOR;
  73. malloc_func = _malloc_func;
  74. realloc_func = _realloc_func;
  75. free_func = _free_func;
  76. return true;
  77. }
  78. LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n", _malloc_func,
  79. _realloc_func, _free_func);
  80. return false;
  81. }
  82. #endif
  83. static inline bool
  84. is_bounds_checks_enabled(WASMModuleInstanceCommon *module_inst)
  85. {
  86. #if WASM_CONFIGUABLE_BOUNDS_CHECKS != 0
  87. return wasm_runtime_is_bounds_checks_enabled(module_inst);
  88. #else
  89. return true;
  90. #endif
  91. }
  92. bool
  93. wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
  94. const MemAllocOption *alloc_option)
  95. {
  96. if (mem_alloc_type == Alloc_With_Pool) {
  97. return wasm_memory_init_with_pool(alloc_option->pool.heap_buf,
  98. alloc_option->pool.heap_size);
  99. }
  100. else if (mem_alloc_type == Alloc_With_Allocator) {
  101. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  102. return wasm_memory_init_with_allocator(
  103. alloc_option->allocator.user_data,
  104. alloc_option->allocator.malloc_func,
  105. alloc_option->allocator.realloc_func,
  106. alloc_option->allocator.free_func);
  107. #else
  108. return wasm_memory_init_with_allocator(
  109. alloc_option->allocator.malloc_func,
  110. alloc_option->allocator.realloc_func,
  111. alloc_option->allocator.free_func);
  112. #endif
  113. }
  114. else if (mem_alloc_type == Alloc_With_System_Allocator) {
  115. memory_mode = MEMORY_MODE_SYSTEM_ALLOCATOR;
  116. return true;
  117. }
  118. else {
  119. return false;
  120. }
  121. }
  122. void
  123. wasm_runtime_memory_destroy()
  124. {
  125. if (memory_mode == MEMORY_MODE_POOL) {
  126. #if BH_ENABLE_GC_VERIFY == 0
  127. (void)mem_allocator_destroy(pool_allocator);
  128. #else
  129. int ret = mem_allocator_destroy(pool_allocator);
  130. if (ret != 0) {
  131. /* Memory leak detected */
  132. exit(-1);
  133. }
  134. #endif
  135. }
  136. memory_mode = MEMORY_MODE_UNKNOWN;
  137. }
  138. unsigned
  139. wasm_runtime_memory_pool_size()
  140. {
  141. if (memory_mode == MEMORY_MODE_POOL)
  142. return global_pool_size;
  143. else
  144. return UINT32_MAX;
  145. }
  146. static inline void *
  147. wasm_runtime_malloc_internal(unsigned int size)
  148. {
  149. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  150. LOG_WARNING(
  151. "wasm_runtime_malloc failed: memory hasn't been initialize.\n");
  152. return NULL;
  153. }
  154. else if (memory_mode == MEMORY_MODE_POOL) {
  155. return mem_allocator_malloc(pool_allocator, size);
  156. }
  157. else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
  158. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  159. return malloc_func(allocator_user_data, size);
  160. #else
  161. return malloc_func(size);
  162. #endif
  163. }
  164. else {
  165. return os_malloc(size);
  166. }
  167. }
  168. static inline void *
  169. wasm_runtime_realloc_internal(void *ptr, unsigned int size)
  170. {
  171. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  172. LOG_WARNING(
  173. "wasm_runtime_realloc failed: memory hasn't been initialize.\n");
  174. return NULL;
  175. }
  176. else if (memory_mode == MEMORY_MODE_POOL) {
  177. return mem_allocator_realloc(pool_allocator, ptr, size);
  178. }
  179. else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
  180. if (realloc_func)
  181. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  182. return realloc_func(allocator_user_data, ptr, size);
  183. #else
  184. return realloc_func(ptr, size);
  185. #endif
  186. else
  187. return NULL;
  188. }
  189. else {
  190. return os_realloc(ptr, size);
  191. }
  192. }
  193. static inline void
  194. wasm_runtime_free_internal(void *ptr)
  195. {
  196. if (!ptr) {
  197. LOG_WARNING("warning: wasm_runtime_free with NULL pointer\n");
  198. #if BH_ENABLE_GC_VERIFY != 0
  199. exit(-1);
  200. #endif
  201. return;
  202. }
  203. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  204. LOG_WARNING("warning: wasm_runtime_free failed: "
  205. "memory hasn't been initialize.\n");
  206. }
  207. else if (memory_mode == MEMORY_MODE_POOL) {
  208. mem_allocator_free(pool_allocator, ptr);
  209. }
  210. else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
  211. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  212. free_func(allocator_user_data, ptr);
  213. #else
  214. free_func(ptr);
  215. #endif
  216. }
  217. else {
  218. os_free(ptr);
  219. }
  220. }
  221. void *
  222. wasm_runtime_malloc(unsigned int size)
  223. {
  224. if (size == 0) {
  225. LOG_WARNING("warning: wasm_runtime_malloc with size zero\n");
  226. /* At lease alloc 1 byte to avoid malloc failed */
  227. size = 1;
  228. #if BH_ENABLE_GC_VERIFY != 0
  229. exit(-1);
  230. #endif
  231. }
  232. return wasm_runtime_malloc_internal(size);
  233. }
  234. void *
  235. wasm_runtime_realloc(void *ptr, unsigned int size)
  236. {
  237. return wasm_runtime_realloc_internal(ptr, size);
  238. }
  239. void
  240. wasm_runtime_free(void *ptr)
  241. {
  242. wasm_runtime_free_internal(ptr);
  243. }
  244. bool
  245. wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info)
  246. {
  247. if (memory_mode == MEMORY_MODE_POOL) {
  248. return mem_allocator_get_alloc_info(pool_allocator, mem_alloc_info);
  249. }
  250. return false;
  251. }
  252. bool
  253. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst_comm,
  254. uint32 app_offset, uint32 size)
  255. {
  256. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  257. WASMMemoryInstance *memory_inst;
  258. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  259. || module_inst_comm->module_type == Wasm_Module_AoT);
  260. if (!is_bounds_checks_enabled(module_inst_comm)) {
  261. return true;
  262. }
  263. memory_inst = wasm_get_default_memory(module_inst);
  264. if (!memory_inst) {
  265. goto fail;
  266. }
  267. /* integer overflow check */
  268. if (app_offset > UINT32_MAX - size) {
  269. goto fail;
  270. }
  271. if (app_offset + size <= memory_inst->memory_data_size) {
  272. return true;
  273. }
  274. fail:
  275. wasm_set_exception(module_inst, "out of bounds memory access");
  276. return false;
  277. }
  278. bool
  279. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst_comm,
  280. uint32 app_str_offset)
  281. {
  282. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  283. uint32 app_end_offset;
  284. char *str, *str_end;
  285. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  286. || module_inst_comm->module_type == Wasm_Module_AoT);
  287. if (!is_bounds_checks_enabled(module_inst_comm)) {
  288. return true;
  289. }
  290. if (!wasm_runtime_get_app_addr_range(module_inst_comm, app_str_offset, NULL,
  291. &app_end_offset))
  292. goto fail;
  293. str = wasm_runtime_addr_app_to_native(module_inst_comm, app_str_offset);
  294. str_end = str + (app_end_offset - app_str_offset);
  295. while (str < str_end && *str != '\0')
  296. str++;
  297. if (str == str_end)
  298. goto fail;
  299. return true;
  300. fail:
  301. wasm_set_exception(module_inst, "out of bounds memory access");
  302. return false;
  303. }
  304. bool
  305. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst_comm,
  306. void *native_ptr, uint32 size)
  307. {
  308. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  309. WASMMemoryInstance *memory_inst;
  310. uint8 *addr = (uint8 *)native_ptr;
  311. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  312. || module_inst_comm->module_type == Wasm_Module_AoT);
  313. if (!is_bounds_checks_enabled(module_inst_comm)) {
  314. return true;
  315. }
  316. memory_inst = wasm_get_default_memory(module_inst);
  317. if (!memory_inst) {
  318. goto fail;
  319. }
  320. /* integer overflow check */
  321. if ((uintptr_t)addr > UINTPTR_MAX - size) {
  322. goto fail;
  323. }
  324. if (memory_inst->memory_data <= addr
  325. && addr + size <= memory_inst->memory_data_end) {
  326. return true;
  327. }
  328. fail:
  329. wasm_set_exception(module_inst, "out of bounds memory access");
  330. return false;
  331. }
  332. void *
  333. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst_comm,
  334. uint32 app_offset)
  335. {
  336. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  337. WASMMemoryInstance *memory_inst;
  338. uint8 *addr;
  339. bool bounds_checks;
  340. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  341. || module_inst_comm->module_type == Wasm_Module_AoT);
  342. bounds_checks = is_bounds_checks_enabled(module_inst_comm);
  343. memory_inst = wasm_get_default_memory(module_inst);
  344. if (!memory_inst) {
  345. return NULL;
  346. }
  347. addr = memory_inst->memory_data + app_offset;
  348. if (bounds_checks) {
  349. if (memory_inst->memory_data <= addr
  350. && addr < memory_inst->memory_data_end) {
  351. return addr;
  352. }
  353. }
  354. /* If bounds checks is disabled, return the address directly */
  355. else if (app_offset != 0) {
  356. return addr;
  357. }
  358. return NULL;
  359. }
  360. uint32
  361. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst_comm,
  362. void *native_ptr)
  363. {
  364. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  365. WASMMemoryInstance *memory_inst;
  366. uint8 *addr = (uint8 *)native_ptr;
  367. bool bounds_checks;
  368. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  369. || module_inst_comm->module_type == Wasm_Module_AoT);
  370. bounds_checks = is_bounds_checks_enabled(module_inst_comm);
  371. memory_inst = wasm_get_default_memory(module_inst);
  372. if (!memory_inst) {
  373. return 0;
  374. }
  375. if (bounds_checks) {
  376. if (memory_inst->memory_data <= addr
  377. && addr < memory_inst->memory_data_end)
  378. return (uint32)(addr - memory_inst->memory_data);
  379. }
  380. /* If bounds checks is disabled, return the offset directly */
  381. else if (addr != NULL) {
  382. return (uint32)(addr - memory_inst->memory_data);
  383. }
  384. return 0;
  385. }
  386. bool
  387. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst_comm,
  388. uint32 app_offset, uint32 *p_app_start_offset,
  389. uint32 *p_app_end_offset)
  390. {
  391. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  392. WASMMemoryInstance *memory_inst;
  393. uint32 memory_data_size;
  394. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  395. || module_inst_comm->module_type == Wasm_Module_AoT);
  396. memory_inst = wasm_get_default_memory(module_inst);
  397. if (!memory_inst) {
  398. return false;
  399. }
  400. memory_data_size = memory_inst->memory_data_size;
  401. if (app_offset < memory_data_size) {
  402. if (p_app_start_offset)
  403. *p_app_start_offset = 0;
  404. if (p_app_end_offset)
  405. *p_app_end_offset = memory_data_size;
  406. return true;
  407. }
  408. return false;
  409. }
  410. bool
  411. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst_comm,
  412. uint8 *native_ptr,
  413. uint8 **p_native_start_addr,
  414. uint8 **p_native_end_addr)
  415. {
  416. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  417. WASMMemoryInstance *memory_inst;
  418. uint8 *addr = (uint8 *)native_ptr;
  419. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  420. || module_inst_comm->module_type == Wasm_Module_AoT);
  421. memory_inst = wasm_get_default_memory(module_inst);
  422. if (!memory_inst) {
  423. return false;
  424. }
  425. if (memory_inst->memory_data <= addr
  426. && addr < memory_inst->memory_data_end) {
  427. if (p_native_start_addr)
  428. *p_native_start_addr = memory_inst->memory_data;
  429. if (p_native_end_addr)
  430. *p_native_end_addr = memory_inst->memory_data_end;
  431. return true;
  432. }
  433. return false;
  434. }
  435. bool
  436. wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  437. uint32 app_buf_addr, uint32 app_buf_size,
  438. void **p_native_addr)
  439. {
  440. WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
  441. uint8 *native_addr;
  442. bool bounds_checks;
  443. if (!memory_inst) {
  444. goto fail;
  445. }
  446. native_addr = memory_inst->memory_data + app_buf_addr;
  447. bounds_checks = is_bounds_checks_enabled((wasm_module_inst_t)module_inst);
  448. if (!bounds_checks) {
  449. if (app_buf_addr == 0) {
  450. native_addr = NULL;
  451. }
  452. goto success;
  453. }
  454. /* No need to check the app_offset and buf_size if memory access
  455. boundary check with hardware trap is enabled */
  456. #ifndef OS_ENABLE_HW_BOUND_CHECK
  457. if (app_buf_addr >= memory_inst->memory_data_size) {
  458. goto fail;
  459. }
  460. if (!is_str) {
  461. if (app_buf_size > memory_inst->memory_data_size - app_buf_addr) {
  462. goto fail;
  463. }
  464. }
  465. else {
  466. const char *str, *str_end;
  467. /* The whole string must be in the linear memory */
  468. str = (const char *)native_addr;
  469. str_end = (const char *)memory_inst->memory_data_end;
  470. while (str < str_end && *str != '\0')
  471. str++;
  472. if (str == str_end)
  473. goto fail;
  474. }
  475. #endif
  476. success:
  477. *p_native_addr = (void *)native_addr;
  478. return true;
  479. fail:
  480. wasm_set_exception(module_inst, "out of bounds memory access");
  481. return false;
  482. }
  483. WASMMemoryInstance *
  484. wasm_get_default_memory(WASMModuleInstance *module_inst)
  485. {
  486. if (module_inst->memories)
  487. return module_inst->memories[0];
  488. else
  489. return NULL;
  490. }
  491. #ifndef OS_ENABLE_HW_BOUND_CHECK
  492. bool
  493. wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
  494. {
  495. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  496. uint8 *memory_data_old, *memory_data_new, *heap_data_old;
  497. uint32 num_bytes_per_page, heap_size, total_size_old = 0;
  498. uint32 cur_page_count, max_page_count, total_page_count;
  499. uint64 total_size_new;
  500. bool ret = true;
  501. enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
  502. if (!memory) {
  503. ret = false;
  504. goto return_func;
  505. }
  506. heap_data_old = memory->heap_data;
  507. heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
  508. memory_data_old = memory->memory_data;
  509. total_size_old = memory->memory_data_size;
  510. num_bytes_per_page = memory->num_bytes_per_page;
  511. cur_page_count = memory->cur_page_count;
  512. max_page_count = memory->max_page_count;
  513. total_page_count = inc_page_count + cur_page_count;
  514. total_size_new = num_bytes_per_page * (uint64)total_page_count;
  515. if (inc_page_count <= 0)
  516. /* No need to enlarge memory */
  517. return true;
  518. if (total_page_count < cur_page_count) { /* integer overflow */
  519. ret = false;
  520. goto return_func;
  521. }
  522. if (total_page_count > max_page_count) {
  523. failure_reason = MAX_SIZE_REACHED;
  524. ret = false;
  525. goto return_func;
  526. }
  527. bh_assert(total_size_new <= 4 * (uint64)BH_GB);
  528. if (total_size_new > UINT32_MAX) {
  529. /* Resize to 1 page with size 4G-1 */
  530. num_bytes_per_page = UINT32_MAX;
  531. total_page_count = max_page_count = 1;
  532. total_size_new = UINT32_MAX;
  533. }
  534. #if WASM_ENABLE_SHARED_MEMORY != 0
  535. if (shared_memory_is_shared(memory)) {
  536. memory->num_bytes_per_page = num_bytes_per_page;
  537. memory->cur_page_count = total_page_count;
  538. memory->max_page_count = max_page_count;
  539. /* No need to update memory->memory_data_size as it is
  540. initialized with the maximum memory data size for
  541. shared memory */
  542. return true;
  543. }
  544. #endif
  545. if (heap_size > 0) {
  546. if (mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  547. wasm_runtime_show_app_heap_corrupted_prompt();
  548. ret = false;
  549. goto return_func;
  550. }
  551. }
  552. if (!(memory_data_new =
  553. wasm_runtime_realloc(memory_data_old, (uint32)total_size_new))) {
  554. if (!(memory_data_new = wasm_runtime_malloc((uint32)total_size_new))) {
  555. ret = false;
  556. goto return_func;
  557. }
  558. if (memory_data_old) {
  559. bh_memcpy_s(memory_data_new, (uint32)total_size_new,
  560. memory_data_old, total_size_old);
  561. wasm_runtime_free(memory_data_old);
  562. }
  563. }
  564. memset(memory_data_new + total_size_old, 0,
  565. (uint32)total_size_new - total_size_old);
  566. if (heap_size > 0) {
  567. if (mem_allocator_migrate(memory->heap_handle,
  568. (char *)heap_data_old
  569. + (memory_data_new - memory_data_old),
  570. heap_size)
  571. != 0) {
  572. /* Don't return here as memory->memory_data is obsolete and
  573. must be updated to be correctly used later. */
  574. ret = false;
  575. }
  576. }
  577. memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
  578. memory->heap_data_end = memory->heap_data + heap_size;
  579. memory->num_bytes_per_page = num_bytes_per_page;
  580. memory->cur_page_count = total_page_count;
  581. memory->max_page_count = max_page_count;
  582. memory->memory_data_size = (uint32)total_size_new;
  583. memory->memory_data = memory_data_new;
  584. memory->memory_data_end = memory_data_new + (uint32)total_size_new;
  585. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 || WASM_ENABLE_AOT != 0
  586. #if UINTPTR_MAX == UINT64_MAX
  587. memory->mem_bound_check_1byte.u64 = total_size_new - 1;
  588. memory->mem_bound_check_2bytes.u64 = total_size_new - 2;
  589. memory->mem_bound_check_4bytes.u64 = total_size_new - 4;
  590. memory->mem_bound_check_8bytes.u64 = total_size_new - 8;
  591. memory->mem_bound_check_16bytes.u64 = total_size_new - 16;
  592. #else
  593. memory->mem_bound_check_1byte.u32[0] = (uint32)total_size_new - 1;
  594. memory->mem_bound_check_2bytes.u32[0] = (uint32)total_size_new - 2;
  595. memory->mem_bound_check_4bytes.u32[0] = (uint32)total_size_new - 4;
  596. memory->mem_bound_check_8bytes.u32[0] = (uint32)total_size_new - 8;
  597. memory->mem_bound_check_16bytes.u32[0] = (uint32)total_size_new - 16;
  598. #endif
  599. #endif
  600. #if defined(os_writegsbase)
  601. /* write base addr of linear memory to GS segment register */
  602. os_writegsbase(memory_data_new);
  603. #endif
  604. return_func:
  605. if (!ret && enlarge_memory_error_cb) {
  606. WASMExecEnv *exec_env = NULL;
  607. #if WASM_ENABLE_INTERP != 0
  608. if (module->module_type == Wasm_Module_Bytecode)
  609. exec_env =
  610. ((WASMModuleInstanceExtra *)module->e)->common.cur_exec_env;
  611. #endif
  612. #if WASM_ENABLE_AOT != 0
  613. if (module->module_type == Wasm_Module_AoT)
  614. exec_env =
  615. ((AOTModuleInstanceExtra *)module->e)->common.cur_exec_env;
  616. #endif
  617. enlarge_memory_error_cb(inc_page_count, total_size_old, 0,
  618. failure_reason,
  619. (WASMModuleInstanceCommon *)module, exec_env,
  620. enlarge_memory_error_user_data);
  621. }
  622. return ret;
  623. }
  624. #else
  625. bool
  626. wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
  627. {
  628. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  629. uint32 num_bytes_per_page, total_size_old = 0;
  630. uint32 cur_page_count, max_page_count, total_page_count;
  631. uint64 total_size_new;
  632. bool ret = true;
  633. enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
  634. if (!memory) {
  635. ret = false;
  636. goto return_func;
  637. }
  638. num_bytes_per_page = memory->num_bytes_per_page;
  639. cur_page_count = memory->cur_page_count;
  640. max_page_count = memory->max_page_count;
  641. total_size_old = num_bytes_per_page * cur_page_count;
  642. total_page_count = inc_page_count + cur_page_count;
  643. total_size_new = num_bytes_per_page * (uint64)total_page_count;
  644. if (inc_page_count <= 0)
  645. /* No need to enlarge memory */
  646. return true;
  647. if (total_page_count < cur_page_count) { /* integer overflow */
  648. ret = false;
  649. goto return_func;
  650. }
  651. if (total_page_count > max_page_count) {
  652. failure_reason = MAX_SIZE_REACHED;
  653. ret = false;
  654. goto return_func;
  655. }
  656. bh_assert(total_size_new <= 4 * (uint64)BH_GB);
  657. if (total_size_new > UINT32_MAX) {
  658. /* Resize to 1 page with size 4G-1 */
  659. num_bytes_per_page = UINT32_MAX;
  660. total_page_count = max_page_count = 1;
  661. total_size_new = UINT32_MAX;
  662. }
  663. #ifdef BH_PLATFORM_WINDOWS
  664. if (!os_mem_commit(memory->memory_data_end,
  665. (uint32)total_size_new - total_size_old,
  666. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  667. ret = false;
  668. goto return_func;
  669. }
  670. #endif
  671. if (os_mprotect(memory->memory_data_end,
  672. (uint32)total_size_new - total_size_old,
  673. MMAP_PROT_READ | MMAP_PROT_WRITE)
  674. != 0) {
  675. #ifdef BH_PLATFORM_WINDOWS
  676. os_mem_decommit(memory->memory_data_end,
  677. (uint32)total_size_new - total_size_old);
  678. #endif
  679. ret = false;
  680. goto return_func;
  681. }
  682. /* The increased pages are filled with zero by the OS when os_mmap,
  683. no need to memset it again here */
  684. memory->num_bytes_per_page = num_bytes_per_page;
  685. memory->cur_page_count = total_page_count;
  686. memory->max_page_count = max_page_count;
  687. memory->memory_data_size = (uint32)total_size_new;
  688. memory->memory_data_end = memory->memory_data + (uint32)total_size_new;
  689. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 || WASM_ENABLE_AOT != 0
  690. memory->mem_bound_check_1byte.u64 = total_size_new - 1;
  691. memory->mem_bound_check_2bytes.u64 = total_size_new - 2;
  692. memory->mem_bound_check_4bytes.u64 = total_size_new - 4;
  693. memory->mem_bound_check_8bytes.u64 = total_size_new - 8;
  694. memory->mem_bound_check_16bytes.u64 = total_size_new - 16;
  695. #endif
  696. return_func:
  697. if (!ret && enlarge_memory_error_cb) {
  698. WASMExecEnv *exec_env = NULL;
  699. #if WASM_ENABLE_INTERP != 0
  700. if (module->module_type == Wasm_Module_Bytecode)
  701. exec_env =
  702. ((WASMModuleInstanceExtra *)module->e)->common.cur_exec_env;
  703. #endif
  704. #if WASM_ENABLE_AOT != 0
  705. if (module->module_type == Wasm_Module_AoT)
  706. exec_env =
  707. ((AOTModuleInstanceExtra *)module->e)->common.cur_exec_env;
  708. #endif
  709. enlarge_memory_error_cb(inc_page_count, total_size_old, 0,
  710. failure_reason,
  711. (WASMModuleInstanceCommon *)module, exec_env,
  712. enlarge_memory_error_user_data);
  713. }
  714. return ret;
  715. }
  716. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  717. void
  718. wasm_runtime_set_enlarge_mem_error_callback(
  719. const enlarge_memory_error_callback_t callback, void *user_data)
  720. {
  721. enlarge_memory_error_cb = callback;
  722. enlarge_memory_error_user_data = user_data;
  723. }
  724. bool
  725. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  726. {
  727. bool ret = false;
  728. #if WASM_ENABLE_SHARED_MEMORY != 0
  729. if (module->memory_count > 0)
  730. shared_memory_lock(module->memories[0]);
  731. #endif
  732. ret = wasm_enlarge_memory_internal(module, inc_page_count);
  733. #if WASM_ENABLE_SHARED_MEMORY != 0
  734. if (module->memory_count > 0)
  735. shared_memory_unlock(module->memories[0]);
  736. #endif
  737. return ret;
  738. }