wasm_memory.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. typedef enum Memory_Mode {
  14. MEMORY_MODE_UNKNOWN = 0,
  15. MEMORY_MODE_POOL,
  16. MEMORY_MODE_ALLOCATOR,
  17. MEMORY_MODE_SYSTEM_ALLOCATOR
  18. } Memory_Mode;
  19. static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
  20. static mem_allocator_t pool_allocator = NULL;
  21. static enlarge_memory_error_callback_t enlarge_memory_error_cb;
  22. static void *enlarge_memory_error_user_data;
  23. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  24. static void *allocator_user_data = NULL;
  25. static void *(*malloc_func)(void *user_data, unsigned int size) = NULL;
  26. static void *(*realloc_func)(void *user_data, void *ptr,
  27. unsigned int size) = NULL;
  28. static void (*free_func)(void *user_data, void *ptr) = NULL;
  29. #else
  30. static void *(*malloc_func)(unsigned int size) = NULL;
  31. static void *(*realloc_func)(void *ptr, unsigned int size) = NULL;
  32. static void (*free_func)(void *ptr) = NULL;
  33. #endif
  34. static unsigned int global_pool_size;
  35. static uint64
  36. align_as_and_cast(uint64 size, uint64 alignment)
  37. {
  38. uint64 aligned_size = (size + alignment - 1) & ~(alignment - 1);
  39. return aligned_size;
  40. }
  41. static bool
  42. wasm_memory_init_with_pool(void *mem, unsigned int bytes)
  43. {
  44. mem_allocator_t allocator = mem_allocator_create(mem, bytes);
  45. if (allocator) {
  46. memory_mode = MEMORY_MODE_POOL;
  47. pool_allocator = allocator;
  48. global_pool_size = bytes;
  49. return true;
  50. }
  51. LOG_ERROR("Init memory with pool (%p, %u) failed.\n", mem, bytes);
  52. return false;
  53. }
  54. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  55. static bool
  56. wasm_memory_init_with_allocator(void *_user_data, void *_malloc_func,
  57. void *_realloc_func, void *_free_func)
  58. {
  59. if (_malloc_func && _free_func && _malloc_func != _free_func) {
  60. memory_mode = MEMORY_MODE_ALLOCATOR;
  61. allocator_user_data = _user_data;
  62. malloc_func = _malloc_func;
  63. realloc_func = _realloc_func;
  64. free_func = _free_func;
  65. return true;
  66. }
  67. LOG_ERROR("Init memory with allocator (%p, %p, %p, %p) failed.\n",
  68. _user_data, _malloc_func, _realloc_func, _free_func);
  69. return false;
  70. }
  71. #else
  72. static bool
  73. wasm_memory_init_with_allocator(void *malloc_func_ptr, void *realloc_func_ptr,
  74. void *free_func_ptr)
  75. {
  76. if (malloc_func_ptr && free_func_ptr && malloc_func_ptr != free_func_ptr) {
  77. memory_mode = MEMORY_MODE_ALLOCATOR;
  78. malloc_func = malloc_func_ptr;
  79. realloc_func = realloc_func_ptr;
  80. free_func = free_func_ptr;
  81. return true;
  82. }
  83. LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n",
  84. malloc_func_ptr, realloc_func_ptr, free_func_ptr);
  85. return false;
  86. }
  87. #endif
  88. static inline bool
  89. is_bounds_checks_enabled(WASMModuleInstanceCommon *module_inst)
  90. {
  91. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  92. if (!module_inst) {
  93. return true;
  94. }
  95. return wasm_runtime_is_bounds_checks_enabled(module_inst);
  96. #else
  97. return true;
  98. #endif
  99. }
  100. bool
  101. wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
  102. const MemAllocOption *alloc_option)
  103. {
  104. if (mem_alloc_type == Alloc_With_Pool) {
  105. return wasm_memory_init_with_pool(alloc_option->pool.heap_buf,
  106. alloc_option->pool.heap_size);
  107. }
  108. else if (mem_alloc_type == Alloc_With_Allocator) {
  109. return wasm_memory_init_with_allocator(
  110. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  111. alloc_option->allocator.user_data,
  112. #endif
  113. alloc_option->allocator.malloc_func,
  114. alloc_option->allocator.realloc_func,
  115. alloc_option->allocator.free_func);
  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. uint64 app_offset, uint64 size)
  258. {
  259. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  260. WASMMemoryInstance *memory_inst;
  261. uint64 max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
  262. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  263. || module_inst_comm->module_type == Wasm_Module_AoT);
  264. if (!is_bounds_checks_enabled(module_inst_comm)) {
  265. return true;
  266. }
  267. memory_inst = wasm_get_default_memory(module_inst);
  268. if (!memory_inst) {
  269. goto fail;
  270. }
  271. #if WASM_ENABLE_MEMORY64 != 0
  272. if (memory_inst->is_memory64)
  273. max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
  274. #endif
  275. /* boundary overflow check */
  276. if (size > max_linear_memory_size
  277. || app_offset > max_linear_memory_size - size) {
  278. goto fail;
  279. }
  280. SHARED_MEMORY_LOCK(memory_inst);
  281. if (app_offset + size <= memory_inst->memory_data_size) {
  282. SHARED_MEMORY_UNLOCK(memory_inst);
  283. return true;
  284. }
  285. SHARED_MEMORY_UNLOCK(memory_inst);
  286. fail:
  287. wasm_set_exception(module_inst, "out of bounds memory access");
  288. return false;
  289. }
  290. bool
  291. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst_comm,
  292. uint64 app_str_offset)
  293. {
  294. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  295. uint64 app_end_offset, max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
  296. char *str, *str_end;
  297. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  298. || module_inst_comm->module_type == Wasm_Module_AoT);
  299. if (!is_bounds_checks_enabled(module_inst_comm)) {
  300. return true;
  301. }
  302. if (!wasm_runtime_get_app_addr_range(module_inst_comm, app_str_offset, NULL,
  303. &app_end_offset))
  304. goto fail;
  305. #if WASM_ENABLE_MEMORY64 != 0
  306. if (module_inst->memories[0]->is_memory64)
  307. max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
  308. #endif
  309. /* boundary overflow check, max start offset can only be size - 1, while end
  310. * offset can be size */
  311. if (app_str_offset >= max_linear_memory_size
  312. || app_end_offset > max_linear_memory_size)
  313. goto fail;
  314. str = wasm_runtime_addr_app_to_native(module_inst_comm, app_str_offset);
  315. str_end = str + (app_end_offset - app_str_offset);
  316. while (str < str_end && *str != '\0')
  317. str++;
  318. if (str == str_end)
  319. goto fail;
  320. return true;
  321. fail:
  322. wasm_set_exception(module_inst, "out of bounds memory access");
  323. return false;
  324. }
  325. bool
  326. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst_comm,
  327. void *native_ptr, uint64 size)
  328. {
  329. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  330. WASMMemoryInstance *memory_inst;
  331. uint8 *addr = (uint8 *)native_ptr;
  332. uint64 max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
  333. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  334. || module_inst_comm->module_type == Wasm_Module_AoT);
  335. if (!is_bounds_checks_enabled(module_inst_comm)) {
  336. return true;
  337. }
  338. memory_inst = wasm_get_default_memory(module_inst);
  339. if (!memory_inst) {
  340. goto fail;
  341. }
  342. #if WASM_ENABLE_MEMORY64 != 0
  343. if (memory_inst->is_memory64)
  344. max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
  345. #endif
  346. /* boundary overflow check */
  347. if (size > max_linear_memory_size || (uintptr_t)addr > UINTPTR_MAX - size) {
  348. goto fail;
  349. }
  350. SHARED_MEMORY_LOCK(memory_inst);
  351. if (memory_inst->memory_data <= addr
  352. && addr + size <= memory_inst->memory_data_end) {
  353. SHARED_MEMORY_UNLOCK(memory_inst);
  354. return true;
  355. }
  356. SHARED_MEMORY_UNLOCK(memory_inst);
  357. fail:
  358. wasm_set_exception(module_inst, "out of bounds memory access");
  359. return false;
  360. }
  361. void *
  362. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst_comm,
  363. uint64 app_offset)
  364. {
  365. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  366. WASMMemoryInstance *memory_inst;
  367. uint8 *addr;
  368. bool bounds_checks;
  369. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  370. || module_inst_comm->module_type == Wasm_Module_AoT);
  371. bounds_checks = is_bounds_checks_enabled(module_inst_comm);
  372. memory_inst = wasm_get_default_memory(module_inst);
  373. if (!memory_inst) {
  374. return NULL;
  375. }
  376. SHARED_MEMORY_LOCK(memory_inst);
  377. addr = memory_inst->memory_data + (uintptr_t)app_offset;
  378. if (bounds_checks) {
  379. if (memory_inst->memory_data <= addr
  380. && addr < memory_inst->memory_data_end) {
  381. SHARED_MEMORY_UNLOCK(memory_inst);
  382. return addr;
  383. }
  384. SHARED_MEMORY_UNLOCK(memory_inst);
  385. return NULL;
  386. }
  387. /* If bounds checks is disabled, return the address directly */
  388. SHARED_MEMORY_UNLOCK(memory_inst);
  389. return addr;
  390. }
  391. uint64
  392. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst_comm,
  393. void *native_ptr)
  394. {
  395. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  396. WASMMemoryInstance *memory_inst;
  397. uint8 *addr = (uint8 *)native_ptr;
  398. bool bounds_checks;
  399. uint64 ret;
  400. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  401. || module_inst_comm->module_type == Wasm_Module_AoT);
  402. bounds_checks = is_bounds_checks_enabled(module_inst_comm);
  403. memory_inst = wasm_get_default_memory(module_inst);
  404. if (!memory_inst) {
  405. return 0;
  406. }
  407. SHARED_MEMORY_LOCK(memory_inst);
  408. if (bounds_checks) {
  409. if (memory_inst->memory_data <= addr
  410. && addr < memory_inst->memory_data_end) {
  411. ret = (uint64)(addr - memory_inst->memory_data);
  412. SHARED_MEMORY_UNLOCK(memory_inst);
  413. return ret;
  414. }
  415. }
  416. /* If bounds checks is disabled, return the offset directly */
  417. else if (addr != NULL) {
  418. ret = (uint64)(addr - memory_inst->memory_data);
  419. SHARED_MEMORY_UNLOCK(memory_inst);
  420. return ret;
  421. }
  422. SHARED_MEMORY_UNLOCK(memory_inst);
  423. return 0;
  424. }
  425. bool
  426. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst_comm,
  427. uint64 app_offset, uint64 *p_app_start_offset,
  428. uint64 *p_app_end_offset)
  429. {
  430. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  431. WASMMemoryInstance *memory_inst;
  432. uint64 memory_data_size;
  433. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  434. || module_inst_comm->module_type == Wasm_Module_AoT);
  435. memory_inst = wasm_get_default_memory(module_inst);
  436. if (!memory_inst) {
  437. return false;
  438. }
  439. SHARED_MEMORY_LOCK(memory_inst);
  440. memory_data_size = memory_inst->memory_data_size;
  441. if (app_offset < memory_data_size) {
  442. if (p_app_start_offset)
  443. *p_app_start_offset = 0;
  444. if (p_app_end_offset)
  445. *p_app_end_offset = memory_data_size;
  446. SHARED_MEMORY_UNLOCK(memory_inst);
  447. return true;
  448. }
  449. SHARED_MEMORY_UNLOCK(memory_inst);
  450. return false;
  451. }
  452. bool
  453. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst_comm,
  454. uint8 *native_ptr,
  455. uint8 **p_native_start_addr,
  456. uint8 **p_native_end_addr)
  457. {
  458. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  459. WASMMemoryInstance *memory_inst;
  460. uint8 *addr = (uint8 *)native_ptr;
  461. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  462. || module_inst_comm->module_type == Wasm_Module_AoT);
  463. memory_inst = wasm_get_default_memory(module_inst);
  464. if (!memory_inst) {
  465. return false;
  466. }
  467. SHARED_MEMORY_LOCK(memory_inst);
  468. if (memory_inst->memory_data <= addr
  469. && addr < memory_inst->memory_data_end) {
  470. if (p_native_start_addr)
  471. *p_native_start_addr = memory_inst->memory_data;
  472. if (p_native_end_addr)
  473. *p_native_end_addr = memory_inst->memory_data_end;
  474. SHARED_MEMORY_UNLOCK(memory_inst);
  475. return true;
  476. }
  477. SHARED_MEMORY_UNLOCK(memory_inst);
  478. return false;
  479. }
  480. bool
  481. wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  482. uint64 app_buf_addr, uint64 app_buf_size,
  483. void **p_native_addr)
  484. {
  485. WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
  486. uint8 *native_addr;
  487. bool bounds_checks;
  488. bh_assert(app_buf_addr <= UINTPTR_MAX && app_buf_size <= UINTPTR_MAX);
  489. if (!memory_inst) {
  490. wasm_set_exception(module_inst, "out of bounds memory access");
  491. return false;
  492. }
  493. native_addr = memory_inst->memory_data + (uintptr_t)app_buf_addr;
  494. bounds_checks = is_bounds_checks_enabled((wasm_module_inst_t)module_inst);
  495. if (!bounds_checks) {
  496. if (app_buf_addr == 0) {
  497. native_addr = NULL;
  498. }
  499. goto success;
  500. }
  501. /* No need to check the app_offset and buf_size if memory access
  502. boundary check with hardware trap is enabled */
  503. #ifndef OS_ENABLE_HW_BOUND_CHECK
  504. SHARED_MEMORY_LOCK(memory_inst);
  505. if (app_buf_addr >= memory_inst->memory_data_size) {
  506. goto fail;
  507. }
  508. if (!is_str) {
  509. if (app_buf_size > memory_inst->memory_data_size - app_buf_addr) {
  510. goto fail;
  511. }
  512. }
  513. else {
  514. const char *str, *str_end;
  515. /* The whole string must be in the linear memory */
  516. str = (const char *)native_addr;
  517. str_end = (const char *)memory_inst->memory_data_end;
  518. while (str < str_end && *str != '\0')
  519. str++;
  520. if (str == str_end)
  521. goto fail;
  522. }
  523. SHARED_MEMORY_UNLOCK(memory_inst);
  524. #endif
  525. success:
  526. *p_native_addr = (void *)native_addr;
  527. return true;
  528. #ifndef OS_ENABLE_HW_BOUND_CHECK
  529. fail:
  530. SHARED_MEMORY_UNLOCK(memory_inst);
  531. wasm_set_exception(module_inst, "out of bounds memory access");
  532. return false;
  533. #endif
  534. }
  535. WASMMemoryInstance *
  536. wasm_get_default_memory(WASMModuleInstance *module_inst)
  537. {
  538. if (module_inst->memories)
  539. return module_inst->memories[0];
  540. else
  541. return NULL;
  542. }
  543. void
  544. wasm_runtime_set_mem_bound_check_bytes(WASMMemoryInstance *memory,
  545. uint64 memory_data_size)
  546. {
  547. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 || WASM_ENABLE_AOT != 0
  548. #if UINTPTR_MAX == UINT64_MAX
  549. memory->mem_bound_check_1byte.u64 = memory_data_size - 1;
  550. memory->mem_bound_check_2bytes.u64 = memory_data_size - 2;
  551. memory->mem_bound_check_4bytes.u64 = memory_data_size - 4;
  552. memory->mem_bound_check_8bytes.u64 = memory_data_size - 8;
  553. memory->mem_bound_check_16bytes.u64 = memory_data_size - 16;
  554. #else
  555. memory->mem_bound_check_1byte.u32[0] = (uint32)memory_data_size - 1;
  556. memory->mem_bound_check_2bytes.u32[0] = (uint32)memory_data_size - 2;
  557. memory->mem_bound_check_4bytes.u32[0] = (uint32)memory_data_size - 4;
  558. memory->mem_bound_check_8bytes.u32[0] = (uint32)memory_data_size - 8;
  559. memory->mem_bound_check_16bytes.u32[0] = (uint32)memory_data_size - 16;
  560. #endif
  561. #endif
  562. }
  563. static void
  564. wasm_munmap_linear_memory(void *mapped_mem, uint64 commit_size, uint64 map_size)
  565. {
  566. #ifdef BH_PLATFORM_WINDOWS
  567. os_mem_decommit(mapped_mem, commit_size);
  568. #else
  569. (void)commit_size;
  570. #endif
  571. os_munmap(mapped_mem, map_size);
  572. }
  573. static void *
  574. wasm_mremap_linear_memory(void *mapped_mem, uint64 old_size, uint64 new_size,
  575. uint64 commit_size)
  576. {
  577. void *new_mem;
  578. bh_assert(new_size > 0);
  579. bh_assert(new_size > old_size);
  580. if (mapped_mem) {
  581. new_mem = os_mremap(mapped_mem, old_size, new_size);
  582. }
  583. else {
  584. new_mem = os_mmap(NULL, new_size, MMAP_PROT_NONE, MMAP_MAP_NONE,
  585. os_get_invalid_handle());
  586. }
  587. if (!new_mem) {
  588. return NULL;
  589. }
  590. #ifdef BH_PLATFORM_WINDOWS
  591. if (commit_size > 0
  592. && !os_mem_commit(new_mem, commit_size,
  593. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  594. os_munmap(new_mem, new_size);
  595. return NULL;
  596. }
  597. #endif
  598. if (os_mprotect(new_mem, commit_size, MMAP_PROT_READ | MMAP_PROT_WRITE)
  599. != 0) {
  600. wasm_munmap_linear_memory(new_mem, new_size, new_size);
  601. return NULL;
  602. }
  603. return new_mem;
  604. }
  605. static void *
  606. wasm_mmap_linear_memory(uint64_t map_size, uint64 commit_size)
  607. {
  608. return wasm_mremap_linear_memory(NULL, 0, map_size, commit_size);
  609. }
  610. bool
  611. wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
  612. {
  613. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  614. uint8 *memory_data_old, *memory_data_new, *heap_data_old;
  615. uint32 num_bytes_per_page, heap_size;
  616. uint32 cur_page_count, max_page_count, total_page_count;
  617. uint64 total_size_old = 0, total_size_new;
  618. bool ret = true, full_size_mmaped;
  619. enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
  620. if (!memory) {
  621. ret = false;
  622. goto return_func;
  623. }
  624. #ifdef OS_ENABLE_HW_BOUND_CHECK
  625. full_size_mmaped = true;
  626. #elif WASM_ENABLE_SHARED_MEMORY != 0
  627. full_size_mmaped = shared_memory_is_shared(memory);
  628. #else
  629. full_size_mmaped = false;
  630. #endif
  631. memory_data_old = memory->memory_data;
  632. total_size_old = memory->memory_data_size;
  633. heap_data_old = memory->heap_data;
  634. heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
  635. num_bytes_per_page = memory->num_bytes_per_page;
  636. cur_page_count = memory->cur_page_count;
  637. max_page_count = memory->max_page_count;
  638. total_page_count = inc_page_count + cur_page_count;
  639. total_size_new = num_bytes_per_page * (uint64)total_page_count;
  640. if (inc_page_count <= 0)
  641. /* No need to enlarge memory */
  642. return true;
  643. if (total_page_count < cur_page_count) { /* integer overflow */
  644. ret = false;
  645. goto return_func;
  646. }
  647. if (total_page_count > max_page_count) {
  648. failure_reason = MAX_SIZE_REACHED;
  649. ret = false;
  650. goto return_func;
  651. }
  652. bh_assert(total_size_new
  653. <= GET_MAX_LINEAR_MEMORY_SIZE(memory->is_memory64));
  654. if (full_size_mmaped) {
  655. #ifdef BH_PLATFORM_WINDOWS
  656. if (!os_mem_commit(memory->memory_data_end,
  657. (mem_offset_t)(total_size_new - total_size_old),
  658. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  659. ret = false;
  660. goto return_func;
  661. }
  662. #endif
  663. if (os_mprotect(memory->memory_data_end,
  664. (mem_offset_t)(total_size_new - total_size_old),
  665. MMAP_PROT_READ | MMAP_PROT_WRITE)
  666. != 0) {
  667. #ifdef BH_PLATFORM_WINDOWS
  668. os_mem_decommit(memory->memory_data_end,
  669. (mem_offset_t)(total_size_new - total_size_old));
  670. #endif
  671. ret = false;
  672. goto return_func;
  673. }
  674. }
  675. else {
  676. if (heap_size > 0) {
  677. if (mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  678. wasm_runtime_show_app_heap_corrupted_prompt();
  679. ret = false;
  680. goto return_func;
  681. }
  682. }
  683. if (!(memory_data_new =
  684. wasm_mremap_linear_memory(memory_data_old, total_size_old,
  685. total_size_new, total_size_new))) {
  686. ret = false;
  687. goto return_func;
  688. }
  689. if (heap_size > 0) {
  690. if (mem_allocator_migrate(memory->heap_handle,
  691. (char *)heap_data_old
  692. + (memory_data_new - memory_data_old),
  693. heap_size)
  694. != 0) {
  695. /* Don't return here as memory->memory_data is obsolete and
  696. must be updated to be correctly used later. */
  697. ret = false;
  698. }
  699. }
  700. memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
  701. memory->heap_data_end = memory->heap_data + heap_size;
  702. memory->memory_data = memory_data_new;
  703. #if defined(os_writegsbase)
  704. /* write base addr of linear memory to GS segment register */
  705. os_writegsbase(memory_data_new);
  706. #endif
  707. }
  708. memory->num_bytes_per_page = num_bytes_per_page;
  709. memory->cur_page_count = total_page_count;
  710. memory->max_page_count = max_page_count;
  711. SET_LINEAR_MEMORY_SIZE(memory, total_size_new);
  712. memory->memory_data_end = memory->memory_data + total_size_new;
  713. wasm_runtime_set_mem_bound_check_bytes(memory, total_size_new);
  714. return_func:
  715. if (!ret && enlarge_memory_error_cb) {
  716. WASMExecEnv *exec_env = NULL;
  717. #if WASM_ENABLE_INTERP != 0
  718. if (module->module_type == Wasm_Module_Bytecode)
  719. exec_env = ((WASMModuleInstance *)module)->cur_exec_env;
  720. #endif
  721. #if WASM_ENABLE_AOT != 0
  722. if (module->module_type == Wasm_Module_AoT)
  723. exec_env = ((AOTModuleInstance *)module)->cur_exec_env;
  724. #endif
  725. enlarge_memory_error_cb(inc_page_count, total_size_old, 0,
  726. failure_reason,
  727. (WASMModuleInstanceCommon *)module, exec_env,
  728. enlarge_memory_error_user_data);
  729. }
  730. return ret;
  731. }
  732. void
  733. wasm_runtime_set_enlarge_mem_error_callback(
  734. const enlarge_memory_error_callback_t callback, void *user_data)
  735. {
  736. enlarge_memory_error_cb = callback;
  737. enlarge_memory_error_user_data = user_data;
  738. }
  739. bool
  740. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  741. {
  742. bool ret = false;
  743. #if WASM_ENABLE_SHARED_MEMORY != 0
  744. if (module->memory_count > 0)
  745. shared_memory_lock(module->memories[0]);
  746. #endif
  747. ret = wasm_enlarge_memory_internal(module, inc_page_count);
  748. #if WASM_ENABLE_SHARED_MEMORY != 0
  749. if (module->memory_count > 0)
  750. shared_memory_unlock(module->memories[0]);
  751. #endif
  752. return ret;
  753. }
  754. void
  755. wasm_deallocate_linear_memory(WASMMemoryInstance *memory_inst)
  756. {
  757. uint64 map_size;
  758. bh_assert(memory_inst);
  759. bh_assert(memory_inst->memory_data);
  760. #ifndef OS_ENABLE_HW_BOUND_CHECK
  761. #if WASM_ENABLE_SHARED_MEMORY != 0
  762. if (shared_memory_is_shared(memory_inst)) {
  763. map_size = (uint64)memory_inst->num_bytes_per_page
  764. * memory_inst->max_page_count;
  765. }
  766. else
  767. #endif
  768. {
  769. map_size = (uint64)memory_inst->num_bytes_per_page
  770. * memory_inst->cur_page_count;
  771. }
  772. #else
  773. map_size = 8 * (uint64)BH_GB;
  774. #endif
  775. wasm_munmap_linear_memory(memory_inst->memory_data,
  776. memory_inst->memory_data_size, map_size);
  777. memory_inst->memory_data = NULL;
  778. }
  779. int
  780. wasm_allocate_linear_memory(uint8 **data, bool is_shared_memory,
  781. bool is_memory64, uint64 num_bytes_per_page,
  782. uint64 init_page_count, uint64 max_page_count,
  783. uint64 *memory_data_size)
  784. {
  785. uint64 map_size, page_size;
  786. bh_assert(data);
  787. bh_assert(memory_data_size);
  788. #ifndef OS_ENABLE_HW_BOUND_CHECK
  789. #if WASM_ENABLE_SHARED_MEMORY != 0
  790. if (is_shared_memory) {
  791. /* Allocate maximum memory size when memory is shared */
  792. map_size = max_page_count * num_bytes_per_page;
  793. }
  794. else
  795. #endif
  796. {
  797. map_size = init_page_count * num_bytes_per_page;
  798. }
  799. #else /* else of OS_ENABLE_HW_BOUND_CHECK */
  800. /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
  801. * ea = i + memarg.offset
  802. * both i and memarg.offset are u32 in range 0 to 4G
  803. * so the range of ea is 0 to 8G
  804. */
  805. map_size = 8 * (uint64)BH_GB;
  806. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  807. page_size = os_getpagesize();
  808. *memory_data_size = init_page_count * num_bytes_per_page;
  809. #if WASM_ENABLE_MEMORY64 != 0
  810. if (is_memory64) {
  811. bh_assert(*memory_data_size <= MAX_LINEAR_MEM64_MEMORY_SIZE);
  812. }
  813. else
  814. #endif
  815. {
  816. bh_assert(*memory_data_size <= MAX_LINEAR_MEMORY_SIZE);
  817. }
  818. *memory_data_size = align_as_and_cast(*memory_data_size, page_size);
  819. if (map_size > 0) {
  820. if (!(*data = wasm_mmap_linear_memory(map_size, *memory_data_size))) {
  821. return BHT_ERROR;
  822. }
  823. }
  824. return BHT_OK;
  825. }