wasm_memory.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. SHARED_MEMORY_LOCK(memory_inst);
  272. if (app_offset + size <= memory_inst->memory_data_size) {
  273. SHARED_MEMORY_UNLOCK(memory_inst);
  274. return true;
  275. }
  276. SHARED_MEMORY_UNLOCK(memory_inst);
  277. fail:
  278. wasm_set_exception(module_inst, "out of bounds memory access");
  279. return false;
  280. }
  281. bool
  282. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst_comm,
  283. uint32 app_str_offset)
  284. {
  285. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  286. uint32 app_end_offset;
  287. char *str, *str_end;
  288. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  289. || module_inst_comm->module_type == Wasm_Module_AoT);
  290. if (!is_bounds_checks_enabled(module_inst_comm)) {
  291. return true;
  292. }
  293. if (!wasm_runtime_get_app_addr_range(module_inst_comm, app_str_offset, NULL,
  294. &app_end_offset))
  295. goto fail;
  296. str = wasm_runtime_addr_app_to_native(module_inst_comm, app_str_offset);
  297. str_end = str + (app_end_offset - app_str_offset);
  298. while (str < str_end && *str != '\0')
  299. str++;
  300. if (str == str_end)
  301. goto fail;
  302. return true;
  303. fail:
  304. wasm_set_exception(module_inst, "out of bounds memory access");
  305. return false;
  306. }
  307. bool
  308. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst_comm,
  309. void *native_ptr, uint32 size)
  310. {
  311. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  312. WASMMemoryInstance *memory_inst;
  313. uint8 *addr = (uint8 *)native_ptr;
  314. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  315. || module_inst_comm->module_type == Wasm_Module_AoT);
  316. if (!is_bounds_checks_enabled(module_inst_comm)) {
  317. return true;
  318. }
  319. memory_inst = wasm_get_default_memory(module_inst);
  320. if (!memory_inst) {
  321. goto fail;
  322. }
  323. /* integer overflow check */
  324. if ((uintptr_t)addr > UINTPTR_MAX - size) {
  325. goto fail;
  326. }
  327. SHARED_MEMORY_LOCK(memory_inst);
  328. if (memory_inst->memory_data <= addr
  329. && addr + size <= memory_inst->memory_data_end) {
  330. SHARED_MEMORY_UNLOCK(memory_inst);
  331. return true;
  332. }
  333. SHARED_MEMORY_UNLOCK(memory_inst);
  334. fail:
  335. wasm_set_exception(module_inst, "out of bounds memory access");
  336. return false;
  337. }
  338. void *
  339. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst_comm,
  340. uint32 app_offset)
  341. {
  342. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  343. WASMMemoryInstance *memory_inst;
  344. uint8 *addr;
  345. bool bounds_checks;
  346. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  347. || module_inst_comm->module_type == Wasm_Module_AoT);
  348. bounds_checks = is_bounds_checks_enabled(module_inst_comm);
  349. memory_inst = wasm_get_default_memory(module_inst);
  350. if (!memory_inst) {
  351. return NULL;
  352. }
  353. SHARED_MEMORY_LOCK(memory_inst);
  354. addr = memory_inst->memory_data + app_offset;
  355. if (bounds_checks) {
  356. if (memory_inst->memory_data <= addr
  357. && addr < memory_inst->memory_data_end) {
  358. SHARED_MEMORY_UNLOCK(memory_inst);
  359. return addr;
  360. }
  361. }
  362. /* If bounds checks is disabled, return the address directly */
  363. else if (app_offset != 0) {
  364. SHARED_MEMORY_UNLOCK(memory_inst);
  365. return addr;
  366. }
  367. SHARED_MEMORY_UNLOCK(memory_inst);
  368. return NULL;
  369. }
  370. uint32
  371. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst_comm,
  372. void *native_ptr)
  373. {
  374. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  375. WASMMemoryInstance *memory_inst;
  376. uint8 *addr = (uint8 *)native_ptr;
  377. bool bounds_checks;
  378. uint32 ret;
  379. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  380. || module_inst_comm->module_type == Wasm_Module_AoT);
  381. bounds_checks = is_bounds_checks_enabled(module_inst_comm);
  382. memory_inst = wasm_get_default_memory(module_inst);
  383. if (!memory_inst) {
  384. return 0;
  385. }
  386. SHARED_MEMORY_LOCK(memory_inst);
  387. if (bounds_checks) {
  388. if (memory_inst->memory_data <= addr
  389. && addr < memory_inst->memory_data_end) {
  390. ret = (uint32)(addr - memory_inst->memory_data);
  391. SHARED_MEMORY_UNLOCK(memory_inst);
  392. return ret;
  393. }
  394. }
  395. /* If bounds checks is disabled, return the offset directly */
  396. else if (addr != NULL) {
  397. ret = (uint32)(addr - memory_inst->memory_data);
  398. SHARED_MEMORY_UNLOCK(memory_inst);
  399. return ret;
  400. }
  401. SHARED_MEMORY_UNLOCK(memory_inst);
  402. return 0;
  403. }
  404. bool
  405. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst_comm,
  406. uint32 app_offset, uint32 *p_app_start_offset,
  407. uint32 *p_app_end_offset)
  408. {
  409. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  410. WASMMemoryInstance *memory_inst;
  411. uint32 memory_data_size;
  412. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  413. || module_inst_comm->module_type == Wasm_Module_AoT);
  414. memory_inst = wasm_get_default_memory(module_inst);
  415. if (!memory_inst) {
  416. return false;
  417. }
  418. SHARED_MEMORY_LOCK(memory_inst);
  419. memory_data_size = memory_inst->memory_data_size;
  420. if (app_offset < memory_data_size) {
  421. if (p_app_start_offset)
  422. *p_app_start_offset = 0;
  423. if (p_app_end_offset)
  424. *p_app_end_offset = memory_data_size;
  425. SHARED_MEMORY_UNLOCK(memory_inst);
  426. return true;
  427. }
  428. SHARED_MEMORY_UNLOCK(memory_inst);
  429. return false;
  430. }
  431. bool
  432. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst_comm,
  433. uint8 *native_ptr,
  434. uint8 **p_native_start_addr,
  435. uint8 **p_native_end_addr)
  436. {
  437. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  438. WASMMemoryInstance *memory_inst;
  439. uint8 *addr = (uint8 *)native_ptr;
  440. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  441. || module_inst_comm->module_type == Wasm_Module_AoT);
  442. memory_inst = wasm_get_default_memory(module_inst);
  443. if (!memory_inst) {
  444. return false;
  445. }
  446. SHARED_MEMORY_LOCK(memory_inst);
  447. if (memory_inst->memory_data <= addr
  448. && addr < memory_inst->memory_data_end) {
  449. if (p_native_start_addr)
  450. *p_native_start_addr = memory_inst->memory_data;
  451. if (p_native_end_addr)
  452. *p_native_end_addr = memory_inst->memory_data_end;
  453. SHARED_MEMORY_UNLOCK(memory_inst);
  454. return true;
  455. }
  456. SHARED_MEMORY_UNLOCK(memory_inst);
  457. return false;
  458. }
  459. bool
  460. wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  461. uint32 app_buf_addr, uint32 app_buf_size,
  462. void **p_native_addr)
  463. {
  464. WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
  465. uint8 *native_addr;
  466. bool bounds_checks;
  467. if (!memory_inst) {
  468. wasm_set_exception(module_inst, "out of bounds memory access");
  469. return false;
  470. }
  471. SHARED_MEMORY_LOCK(memory_inst);
  472. native_addr = memory_inst->memory_data + app_buf_addr;
  473. bounds_checks = is_bounds_checks_enabled((wasm_module_inst_t)module_inst);
  474. if (!bounds_checks) {
  475. if (app_buf_addr == 0) {
  476. native_addr = NULL;
  477. }
  478. goto success;
  479. }
  480. /* No need to check the app_offset and buf_size if memory access
  481. boundary check with hardware trap is enabled */
  482. #ifndef OS_ENABLE_HW_BOUND_CHECK
  483. if (app_buf_addr >= memory_inst->memory_data_size) {
  484. goto fail;
  485. }
  486. if (!is_str) {
  487. if (app_buf_size > memory_inst->memory_data_size - app_buf_addr) {
  488. goto fail;
  489. }
  490. }
  491. else {
  492. const char *str, *str_end;
  493. /* The whole string must be in the linear memory */
  494. str = (const char *)native_addr;
  495. str_end = (const char *)memory_inst->memory_data_end;
  496. while (str < str_end && *str != '\0')
  497. str++;
  498. if (str == str_end)
  499. goto fail;
  500. }
  501. #endif
  502. SHARED_MEMORY_UNLOCK(memory_inst);
  503. success:
  504. *p_native_addr = (void *)native_addr;
  505. return true;
  506. #ifndef OS_ENABLE_HW_BOUND_CHECK
  507. fail:
  508. SHARED_MEMORY_UNLOCK(memory_inst);
  509. wasm_set_exception(module_inst, "out of bounds memory access");
  510. return false;
  511. #endif
  512. }
  513. WASMMemoryInstance *
  514. wasm_get_default_memory(WASMModuleInstance *module_inst)
  515. {
  516. if (module_inst->memories)
  517. return module_inst->memories[0];
  518. else
  519. return NULL;
  520. }
  521. void
  522. wasm_runtime_set_mem_bound_check_bytes(WASMMemoryInstance *memory,
  523. uint64 memory_data_size)
  524. {
  525. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 || WASM_ENABLE_AOT != 0
  526. #if UINTPTR_MAX == UINT64_MAX
  527. memory->mem_bound_check_1byte.u64 = memory_data_size - 1;
  528. memory->mem_bound_check_2bytes.u64 = memory_data_size - 2;
  529. memory->mem_bound_check_4bytes.u64 = memory_data_size - 4;
  530. memory->mem_bound_check_8bytes.u64 = memory_data_size - 8;
  531. memory->mem_bound_check_16bytes.u64 = memory_data_size - 16;
  532. #else
  533. memory->mem_bound_check_1byte.u32[0] = (uint32)memory_data_size - 1;
  534. memory->mem_bound_check_2bytes.u32[0] = (uint32)memory_data_size - 2;
  535. memory->mem_bound_check_4bytes.u32[0] = (uint32)memory_data_size - 4;
  536. memory->mem_bound_check_8bytes.u32[0] = (uint32)memory_data_size - 8;
  537. memory->mem_bound_check_16bytes.u32[0] = (uint32)memory_data_size - 16;
  538. #endif
  539. #endif
  540. }
  541. #ifndef OS_ENABLE_HW_BOUND_CHECK
  542. bool
  543. wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
  544. {
  545. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  546. uint8 *memory_data_old, *memory_data_new, *heap_data_old;
  547. uint32 num_bytes_per_page, heap_size, total_size_old = 0;
  548. uint32 cur_page_count, max_page_count, total_page_count;
  549. uint64 total_size_new;
  550. bool ret = true;
  551. enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
  552. if (!memory) {
  553. ret = false;
  554. goto return_func;
  555. }
  556. heap_data_old = memory->heap_data;
  557. heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
  558. memory_data_old = memory->memory_data;
  559. total_size_old = memory->memory_data_size;
  560. num_bytes_per_page = memory->num_bytes_per_page;
  561. cur_page_count = memory->cur_page_count;
  562. max_page_count = memory->max_page_count;
  563. total_page_count = inc_page_count + cur_page_count;
  564. total_size_new = num_bytes_per_page * (uint64)total_page_count;
  565. if (inc_page_count <= 0)
  566. /* No need to enlarge memory */
  567. return true;
  568. if (total_page_count < cur_page_count) { /* integer overflow */
  569. ret = false;
  570. goto return_func;
  571. }
  572. if (total_page_count > max_page_count) {
  573. failure_reason = MAX_SIZE_REACHED;
  574. ret = false;
  575. goto return_func;
  576. }
  577. bh_assert(total_size_new <= 4 * (uint64)BH_GB);
  578. if (total_size_new > UINT32_MAX) {
  579. /* Resize to 1 page with size 4G-1 */
  580. num_bytes_per_page = UINT32_MAX;
  581. total_page_count = max_page_count = 1;
  582. total_size_new = UINT32_MAX;
  583. }
  584. #if WASM_ENABLE_SHARED_MEMORY != 0
  585. if (shared_memory_is_shared(memory)) {
  586. memory->num_bytes_per_page = num_bytes_per_page;
  587. memory->cur_page_count = total_page_count;
  588. memory->max_page_count = max_page_count;
  589. memory->memory_data_size = (uint32)total_size_new;
  590. memory->memory_data_end = memory->memory_data + (uint32)total_size_new;
  591. wasm_runtime_set_mem_bound_check_bytes(memory, total_size_new);
  592. return true;
  593. }
  594. #endif
  595. if (heap_size > 0) {
  596. if (mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  597. wasm_runtime_show_app_heap_corrupted_prompt();
  598. ret = false;
  599. goto return_func;
  600. }
  601. }
  602. if (!(memory_data_new =
  603. wasm_runtime_realloc(memory_data_old, (uint32)total_size_new))) {
  604. if (!(memory_data_new = wasm_runtime_malloc((uint32)total_size_new))) {
  605. ret = false;
  606. goto return_func;
  607. }
  608. if (memory_data_old) {
  609. bh_memcpy_s(memory_data_new, (uint32)total_size_new,
  610. memory_data_old, total_size_old);
  611. wasm_runtime_free(memory_data_old);
  612. }
  613. }
  614. memset(memory_data_new + total_size_old, 0,
  615. (uint32)total_size_new - total_size_old);
  616. if (heap_size > 0) {
  617. if (mem_allocator_migrate(memory->heap_handle,
  618. (char *)heap_data_old
  619. + (memory_data_new - memory_data_old),
  620. heap_size)
  621. != 0) {
  622. /* Don't return here as memory->memory_data is obsolete and
  623. must be updated to be correctly used later. */
  624. ret = false;
  625. }
  626. }
  627. memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
  628. memory->heap_data_end = memory->heap_data + heap_size;
  629. memory->num_bytes_per_page = num_bytes_per_page;
  630. memory->cur_page_count = total_page_count;
  631. memory->max_page_count = max_page_count;
  632. memory->memory_data_size = (uint32)total_size_new;
  633. memory->memory_data = memory_data_new;
  634. memory->memory_data_end = memory_data_new + (uint32)total_size_new;
  635. wasm_runtime_set_mem_bound_check_bytes(memory, total_size_new);
  636. #if defined(os_writegsbase)
  637. /* write base addr of linear memory to GS segment register */
  638. os_writegsbase(memory_data_new);
  639. #endif
  640. return_func:
  641. if (!ret && enlarge_memory_error_cb) {
  642. WASMExecEnv *exec_env = NULL;
  643. #if WASM_ENABLE_INTERP != 0
  644. if (module->module_type == Wasm_Module_Bytecode)
  645. exec_env =
  646. ((WASMModuleInstanceExtra *)module->e)->common.cur_exec_env;
  647. #endif
  648. #if WASM_ENABLE_AOT != 0
  649. if (module->module_type == Wasm_Module_AoT)
  650. exec_env =
  651. ((AOTModuleInstanceExtra *)module->e)->common.cur_exec_env;
  652. #endif
  653. enlarge_memory_error_cb(inc_page_count, total_size_old, 0,
  654. failure_reason,
  655. (WASMModuleInstanceCommon *)module, exec_env,
  656. enlarge_memory_error_user_data);
  657. }
  658. return ret;
  659. }
  660. #else
  661. bool
  662. wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
  663. {
  664. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  665. uint32 num_bytes_per_page, total_size_old = 0;
  666. uint32 cur_page_count, max_page_count, total_page_count;
  667. uint64 total_size_new;
  668. bool ret = true;
  669. enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
  670. if (!memory) {
  671. ret = false;
  672. goto return_func;
  673. }
  674. num_bytes_per_page = memory->num_bytes_per_page;
  675. cur_page_count = memory->cur_page_count;
  676. max_page_count = memory->max_page_count;
  677. total_size_old = num_bytes_per_page * cur_page_count;
  678. total_page_count = inc_page_count + cur_page_count;
  679. total_size_new = num_bytes_per_page * (uint64)total_page_count;
  680. if (inc_page_count <= 0)
  681. /* No need to enlarge memory */
  682. return true;
  683. if (total_page_count < cur_page_count) { /* integer overflow */
  684. ret = false;
  685. goto return_func;
  686. }
  687. if (total_page_count > max_page_count) {
  688. failure_reason = MAX_SIZE_REACHED;
  689. ret = false;
  690. goto return_func;
  691. }
  692. bh_assert(total_size_new <= 4 * (uint64)BH_GB);
  693. if (total_size_new > UINT32_MAX) {
  694. /* Resize to 1 page with size 4G-1 */
  695. num_bytes_per_page = UINT32_MAX;
  696. total_page_count = max_page_count = 1;
  697. total_size_new = UINT32_MAX;
  698. }
  699. #ifdef BH_PLATFORM_WINDOWS
  700. if (!os_mem_commit(memory->memory_data_end,
  701. (uint32)total_size_new - total_size_old,
  702. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  703. ret = false;
  704. goto return_func;
  705. }
  706. #endif
  707. if (os_mprotect(memory->memory_data_end,
  708. (uint32)total_size_new - total_size_old,
  709. MMAP_PROT_READ | MMAP_PROT_WRITE)
  710. != 0) {
  711. #ifdef BH_PLATFORM_WINDOWS
  712. os_mem_decommit(memory->memory_data_end,
  713. (uint32)total_size_new - total_size_old);
  714. #endif
  715. ret = false;
  716. goto return_func;
  717. }
  718. /* The increased pages are filled with zero by the OS when os_mmap,
  719. no need to memset it again here */
  720. memory->num_bytes_per_page = num_bytes_per_page;
  721. memory->cur_page_count = total_page_count;
  722. memory->max_page_count = max_page_count;
  723. memory->memory_data_size = (uint32)total_size_new;
  724. memory->memory_data_end = memory->memory_data + (uint32)total_size_new;
  725. wasm_runtime_set_mem_bound_check_bytes(memory, total_size_new);
  726. return_func:
  727. if (!ret && enlarge_memory_error_cb) {
  728. WASMExecEnv *exec_env = NULL;
  729. #if WASM_ENABLE_INTERP != 0
  730. if (module->module_type == Wasm_Module_Bytecode)
  731. exec_env =
  732. ((WASMModuleInstanceExtra *)module->e)->common.cur_exec_env;
  733. #endif
  734. #if WASM_ENABLE_AOT != 0
  735. if (module->module_type == Wasm_Module_AoT)
  736. exec_env =
  737. ((AOTModuleInstanceExtra *)module->e)->common.cur_exec_env;
  738. #endif
  739. enlarge_memory_error_cb(inc_page_count, total_size_old, 0,
  740. failure_reason,
  741. (WASMModuleInstanceCommon *)module, exec_env,
  742. enlarge_memory_error_user_data);
  743. }
  744. return ret;
  745. }
  746. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  747. void
  748. wasm_runtime_set_enlarge_mem_error_callback(
  749. const enlarge_memory_error_callback_t callback, void *user_data)
  750. {
  751. enlarge_memory_error_cb = callback;
  752. enlarge_memory_error_user_data = user_data;
  753. }
  754. bool
  755. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  756. {
  757. bool ret = false;
  758. #if WASM_ENABLE_SHARED_MEMORY != 0
  759. if (module->memory_count > 0)
  760. shared_memory_lock(module->memories[0]);
  761. #endif
  762. ret = wasm_enlarge_memory_internal(module, inc_page_count);
  763. #if WASM_ENABLE_SHARED_MEMORY != 0
  764. if (module->memory_count > 0)
  765. shared_memory_unlock(module->memories[0]);
  766. #endif
  767. return ret;
  768. }