wasm_memory.c 26 KB

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