wasm_memory.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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. #endif
  26. static void *(*malloc_func)(
  27. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  28. mem_alloc_usage_t usage,
  29. #endif
  30. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  31. void *user_data,
  32. #endif
  33. unsigned int size) = NULL;
  34. static void *(*realloc_func)(
  35. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  36. mem_alloc_usage_t usage, bool full_size_mmaped,
  37. #endif
  38. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  39. void *user_data,
  40. #endif
  41. void *ptr, unsigned int size) = NULL;
  42. static void (*free_func)(
  43. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  44. mem_alloc_usage_t usage,
  45. #endif
  46. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  47. void *user_data,
  48. #endif
  49. void *ptr) = NULL;
  50. static unsigned int global_pool_size;
  51. static uint64
  52. align_as_and_cast(uint64 size, uint64 alignment)
  53. {
  54. uint64 aligned_size = (size + alignment - 1) & ~(alignment - 1);
  55. return aligned_size;
  56. }
  57. static bool
  58. wasm_memory_init_with_pool(void *mem, unsigned int bytes)
  59. {
  60. mem_allocator_t allocator = mem_allocator_create(mem, bytes);
  61. if (allocator) {
  62. memory_mode = MEMORY_MODE_POOL;
  63. pool_allocator = allocator;
  64. global_pool_size = bytes;
  65. return true;
  66. }
  67. LOG_ERROR("Init memory with pool (%p, %u) failed.\n", mem, bytes);
  68. return false;
  69. }
  70. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  71. static bool
  72. wasm_memory_init_with_allocator(void *_user_data, void *_malloc_func,
  73. void *_realloc_func, void *_free_func)
  74. {
  75. if (_malloc_func && _free_func && _malloc_func != _free_func) {
  76. memory_mode = MEMORY_MODE_ALLOCATOR;
  77. allocator_user_data = _user_data;
  78. malloc_func = _malloc_func;
  79. realloc_func = _realloc_func;
  80. free_func = _free_func;
  81. return true;
  82. }
  83. LOG_ERROR("Init memory with allocator (%p, %p, %p, %p) failed.\n",
  84. _user_data, _malloc_func, _realloc_func, _free_func);
  85. return false;
  86. }
  87. #else
  88. static bool
  89. wasm_memory_init_with_allocator(void *malloc_func_ptr, void *realloc_func_ptr,
  90. void *free_func_ptr)
  91. {
  92. if (malloc_func_ptr && free_func_ptr && malloc_func_ptr != free_func_ptr) {
  93. memory_mode = MEMORY_MODE_ALLOCATOR;
  94. malloc_func = malloc_func_ptr;
  95. realloc_func = realloc_func_ptr;
  96. free_func = free_func_ptr;
  97. return true;
  98. }
  99. LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n",
  100. malloc_func_ptr, realloc_func_ptr, free_func_ptr);
  101. return false;
  102. }
  103. #endif
  104. static inline bool
  105. is_bounds_checks_enabled(WASMModuleInstanceCommon *module_inst)
  106. {
  107. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  108. if (!module_inst) {
  109. return true;
  110. }
  111. return wasm_runtime_is_bounds_checks_enabled(module_inst);
  112. #else
  113. return true;
  114. #endif
  115. }
  116. bool
  117. wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
  118. const MemAllocOption *alloc_option)
  119. {
  120. if (mem_alloc_type == Alloc_With_Pool) {
  121. return wasm_memory_init_with_pool(alloc_option->pool.heap_buf,
  122. alloc_option->pool.heap_size);
  123. }
  124. else if (mem_alloc_type == Alloc_With_Allocator) {
  125. return wasm_memory_init_with_allocator(
  126. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  127. alloc_option->allocator.user_data,
  128. #endif
  129. alloc_option->allocator.malloc_func,
  130. alloc_option->allocator.realloc_func,
  131. alloc_option->allocator.free_func);
  132. }
  133. else if (mem_alloc_type == Alloc_With_System_Allocator) {
  134. memory_mode = MEMORY_MODE_SYSTEM_ALLOCATOR;
  135. return true;
  136. }
  137. else {
  138. return false;
  139. }
  140. }
  141. void
  142. wasm_runtime_memory_destroy(void)
  143. {
  144. if (memory_mode == MEMORY_MODE_POOL) {
  145. #if BH_ENABLE_GC_VERIFY == 0
  146. (void)mem_allocator_destroy(pool_allocator);
  147. #else
  148. int ret = mem_allocator_destroy(pool_allocator);
  149. if (ret != 0) {
  150. /* Memory leak detected */
  151. exit(-1);
  152. }
  153. #endif
  154. }
  155. memory_mode = MEMORY_MODE_UNKNOWN;
  156. }
  157. unsigned
  158. wasm_runtime_memory_pool_size(void)
  159. {
  160. if (memory_mode == MEMORY_MODE_POOL)
  161. return global_pool_size;
  162. else
  163. return UINT32_MAX;
  164. }
  165. static inline void *
  166. wasm_runtime_malloc_internal(unsigned int size)
  167. {
  168. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  169. LOG_WARNING(
  170. "wasm_runtime_malloc failed: memory hasn't been initialized.\n");
  171. return NULL;
  172. }
  173. else if (memory_mode == MEMORY_MODE_POOL) {
  174. return mem_allocator_malloc(pool_allocator, size);
  175. }
  176. else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
  177. return malloc_func(
  178. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  179. Alloc_For_Runtime,
  180. #endif
  181. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  182. allocator_user_data,
  183. #endif
  184. size);
  185. }
  186. else {
  187. return os_malloc(size);
  188. }
  189. }
  190. static inline void *
  191. wasm_runtime_realloc_internal(void *ptr, unsigned int size)
  192. {
  193. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  194. LOG_WARNING(
  195. "wasm_runtime_realloc failed: memory hasn't been initialized.\n");
  196. return NULL;
  197. }
  198. else if (memory_mode == MEMORY_MODE_POOL) {
  199. return mem_allocator_realloc(pool_allocator, ptr, size);
  200. }
  201. else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
  202. if (realloc_func)
  203. return realloc_func(
  204. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  205. Alloc_For_Runtime, false,
  206. #endif
  207. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  208. allocator_user_data,
  209. #endif
  210. ptr, size);
  211. else
  212. return NULL;
  213. }
  214. else {
  215. return os_realloc(ptr, size);
  216. }
  217. }
  218. static inline void
  219. wasm_runtime_free_internal(void *ptr)
  220. {
  221. if (!ptr) {
  222. LOG_WARNING("warning: wasm_runtime_free with NULL pointer\n");
  223. #if BH_ENABLE_GC_VERIFY != 0
  224. exit(-1);
  225. #endif
  226. return;
  227. }
  228. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  229. LOG_WARNING("warning: wasm_runtime_free failed: "
  230. "memory hasn't been initialize.\n");
  231. }
  232. else if (memory_mode == MEMORY_MODE_POOL) {
  233. mem_allocator_free(pool_allocator, ptr);
  234. }
  235. else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
  236. free_func(
  237. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  238. Alloc_For_Runtime,
  239. #endif
  240. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  241. allocator_user_data,
  242. #endif
  243. ptr);
  244. }
  245. else {
  246. os_free(ptr);
  247. }
  248. }
  249. void *
  250. wasm_runtime_malloc(unsigned int size)
  251. {
  252. if (size == 0) {
  253. LOG_WARNING("warning: wasm_runtime_malloc with size zero\n");
  254. /* At lease alloc 1 byte to avoid malloc failed */
  255. size = 1;
  256. #if BH_ENABLE_GC_VERIFY != 0
  257. exit(-1);
  258. #endif
  259. }
  260. #if WASM_ENABLE_FUZZ_TEST != 0
  261. if (size >= WASM_MEM_ALLOC_MAX_SIZE) {
  262. LOG_WARNING("warning: wasm_runtime_malloc with too large size\n");
  263. return NULL;
  264. }
  265. #endif
  266. return wasm_runtime_malloc_internal(size);
  267. }
  268. void *
  269. wasm_runtime_realloc(void *ptr, unsigned int size)
  270. {
  271. return wasm_runtime_realloc_internal(ptr, size);
  272. }
  273. void
  274. wasm_runtime_free(void *ptr)
  275. {
  276. wasm_runtime_free_internal(ptr);
  277. }
  278. bool
  279. wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info)
  280. {
  281. if (memory_mode == MEMORY_MODE_POOL) {
  282. return mem_allocator_get_alloc_info(pool_allocator, mem_alloc_info);
  283. }
  284. return false;
  285. }
  286. bool
  287. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst_comm,
  288. uint64 app_offset, uint64 size)
  289. {
  290. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  291. WASMMemoryInstance *memory_inst;
  292. uint64 max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
  293. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  294. || module_inst_comm->module_type == Wasm_Module_AoT);
  295. if (!is_bounds_checks_enabled(module_inst_comm)) {
  296. return true;
  297. }
  298. memory_inst = wasm_get_default_memory(module_inst);
  299. if (!memory_inst) {
  300. goto fail;
  301. }
  302. #if WASM_ENABLE_MEMORY64 != 0
  303. if (memory_inst->is_memory64)
  304. max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
  305. #endif
  306. /* boundary overflow check */
  307. if (size > max_linear_memory_size
  308. || app_offset > max_linear_memory_size - size) {
  309. goto fail;
  310. }
  311. SHARED_MEMORY_LOCK(memory_inst);
  312. if (app_offset + size <= memory_inst->memory_data_size) {
  313. SHARED_MEMORY_UNLOCK(memory_inst);
  314. return true;
  315. }
  316. SHARED_MEMORY_UNLOCK(memory_inst);
  317. fail:
  318. wasm_set_exception(module_inst, "out of bounds memory access");
  319. return false;
  320. }
  321. bool
  322. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst_comm,
  323. uint64 app_str_offset)
  324. {
  325. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  326. uint64 app_end_offset, max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
  327. char *str, *str_end;
  328. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  329. || module_inst_comm->module_type == Wasm_Module_AoT);
  330. if (!is_bounds_checks_enabled(module_inst_comm)) {
  331. return true;
  332. }
  333. if (!wasm_runtime_get_app_addr_range(module_inst_comm, app_str_offset, NULL,
  334. &app_end_offset))
  335. goto fail;
  336. #if WASM_ENABLE_MEMORY64 != 0
  337. if (module_inst->memories[0]->is_memory64)
  338. max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
  339. #endif
  340. /* boundary overflow check, max start offset can only be size - 1, while end
  341. * offset can be size */
  342. if (app_str_offset >= max_linear_memory_size
  343. || app_end_offset > max_linear_memory_size)
  344. goto fail;
  345. str = wasm_runtime_addr_app_to_native(module_inst_comm, app_str_offset);
  346. str_end = str + (app_end_offset - app_str_offset);
  347. while (str < str_end && *str != '\0')
  348. str++;
  349. if (str == str_end)
  350. goto fail;
  351. return true;
  352. fail:
  353. wasm_set_exception(module_inst, "out of bounds memory access");
  354. return false;
  355. }
  356. bool
  357. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst_comm,
  358. void *native_ptr, uint64 size)
  359. {
  360. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  361. WASMMemoryInstance *memory_inst;
  362. uint8 *addr = (uint8 *)native_ptr;
  363. uint64 max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
  364. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  365. || module_inst_comm->module_type == Wasm_Module_AoT);
  366. if (!is_bounds_checks_enabled(module_inst_comm)) {
  367. return true;
  368. }
  369. memory_inst = wasm_get_default_memory(module_inst);
  370. if (!memory_inst) {
  371. goto fail;
  372. }
  373. #if WASM_ENABLE_MEMORY64 != 0
  374. if (memory_inst->is_memory64)
  375. max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
  376. #endif
  377. /* boundary overflow check */
  378. if (size > max_linear_memory_size || (uintptr_t)addr > UINTPTR_MAX - size) {
  379. goto fail;
  380. }
  381. SHARED_MEMORY_LOCK(memory_inst);
  382. if (memory_inst->memory_data <= addr
  383. && addr + size <= memory_inst->memory_data_end) {
  384. SHARED_MEMORY_UNLOCK(memory_inst);
  385. return true;
  386. }
  387. SHARED_MEMORY_UNLOCK(memory_inst);
  388. fail:
  389. wasm_set_exception(module_inst, "out of bounds memory access");
  390. return false;
  391. }
  392. void *
  393. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst_comm,
  394. uint64 app_offset)
  395. {
  396. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  397. WASMMemoryInstance *memory_inst;
  398. uint8 *addr;
  399. bool bounds_checks;
  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 NULL;
  406. }
  407. SHARED_MEMORY_LOCK(memory_inst);
  408. addr = memory_inst->memory_data + (uintptr_t)app_offset;
  409. if (bounds_checks) {
  410. if (memory_inst->memory_data <= addr
  411. && addr < memory_inst->memory_data_end) {
  412. SHARED_MEMORY_UNLOCK(memory_inst);
  413. return addr;
  414. }
  415. SHARED_MEMORY_UNLOCK(memory_inst);
  416. return NULL;
  417. }
  418. /* If bounds checks is disabled, return the address directly */
  419. SHARED_MEMORY_UNLOCK(memory_inst);
  420. return addr;
  421. }
  422. uint64
  423. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst_comm,
  424. void *native_ptr)
  425. {
  426. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  427. WASMMemoryInstance *memory_inst;
  428. uint8 *addr = (uint8 *)native_ptr;
  429. bool bounds_checks;
  430. uint64 ret;
  431. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  432. || module_inst_comm->module_type == Wasm_Module_AoT);
  433. bounds_checks = is_bounds_checks_enabled(module_inst_comm);
  434. memory_inst = wasm_get_default_memory(module_inst);
  435. if (!memory_inst) {
  436. return 0;
  437. }
  438. SHARED_MEMORY_LOCK(memory_inst);
  439. if (bounds_checks) {
  440. if (memory_inst->memory_data <= addr
  441. && addr < memory_inst->memory_data_end) {
  442. ret = (uint64)(addr - memory_inst->memory_data);
  443. SHARED_MEMORY_UNLOCK(memory_inst);
  444. return ret;
  445. }
  446. }
  447. /* If bounds checks is disabled, return the offset directly */
  448. else if (addr != NULL) {
  449. ret = (uint64)(addr - memory_inst->memory_data);
  450. SHARED_MEMORY_UNLOCK(memory_inst);
  451. return ret;
  452. }
  453. SHARED_MEMORY_UNLOCK(memory_inst);
  454. return 0;
  455. }
  456. bool
  457. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst_comm,
  458. uint64 app_offset, uint64 *p_app_start_offset,
  459. uint64 *p_app_end_offset)
  460. {
  461. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  462. WASMMemoryInstance *memory_inst;
  463. uint64 memory_data_size;
  464. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  465. || module_inst_comm->module_type == Wasm_Module_AoT);
  466. memory_inst = wasm_get_default_memory(module_inst);
  467. if (!memory_inst) {
  468. return false;
  469. }
  470. SHARED_MEMORY_LOCK(memory_inst);
  471. memory_data_size = memory_inst->memory_data_size;
  472. if (app_offset < memory_data_size) {
  473. if (p_app_start_offset)
  474. *p_app_start_offset = 0;
  475. if (p_app_end_offset)
  476. *p_app_end_offset = memory_data_size;
  477. SHARED_MEMORY_UNLOCK(memory_inst);
  478. return true;
  479. }
  480. SHARED_MEMORY_UNLOCK(memory_inst);
  481. return false;
  482. }
  483. bool
  484. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst_comm,
  485. uint8 *native_ptr,
  486. uint8 **p_native_start_addr,
  487. uint8 **p_native_end_addr)
  488. {
  489. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  490. WASMMemoryInstance *memory_inst;
  491. uint8 *addr = (uint8 *)native_ptr;
  492. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  493. || module_inst_comm->module_type == Wasm_Module_AoT);
  494. memory_inst = wasm_get_default_memory(module_inst);
  495. if (!memory_inst) {
  496. return false;
  497. }
  498. SHARED_MEMORY_LOCK(memory_inst);
  499. if (memory_inst->memory_data <= addr
  500. && addr < memory_inst->memory_data_end) {
  501. if (p_native_start_addr)
  502. *p_native_start_addr = memory_inst->memory_data;
  503. if (p_native_end_addr)
  504. *p_native_end_addr = memory_inst->memory_data_end;
  505. SHARED_MEMORY_UNLOCK(memory_inst);
  506. return true;
  507. }
  508. SHARED_MEMORY_UNLOCK(memory_inst);
  509. return false;
  510. }
  511. bool
  512. wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  513. uint64 app_buf_addr, uint64 app_buf_size,
  514. void **p_native_addr)
  515. {
  516. WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
  517. uint8 *native_addr;
  518. bool bounds_checks;
  519. bh_assert(app_buf_addr <= UINTPTR_MAX && app_buf_size <= UINTPTR_MAX);
  520. if (!memory_inst) {
  521. wasm_set_exception(module_inst, "out of bounds memory access");
  522. return false;
  523. }
  524. native_addr = memory_inst->memory_data + (uintptr_t)app_buf_addr;
  525. bounds_checks = is_bounds_checks_enabled((wasm_module_inst_t)module_inst);
  526. if (!bounds_checks) {
  527. if (app_buf_addr == 0) {
  528. native_addr = NULL;
  529. }
  530. goto success;
  531. }
  532. /* No need to check the app_offset and buf_size if memory access
  533. boundary check with hardware trap is enabled */
  534. #ifndef OS_ENABLE_HW_BOUND_CHECK
  535. SHARED_MEMORY_LOCK(memory_inst);
  536. if (app_buf_addr >= memory_inst->memory_data_size) {
  537. goto fail;
  538. }
  539. if (!is_str) {
  540. if (app_buf_size > memory_inst->memory_data_size - app_buf_addr) {
  541. goto fail;
  542. }
  543. }
  544. else {
  545. const char *str, *str_end;
  546. /* The whole string must be in the linear memory */
  547. str = (const char *)native_addr;
  548. str_end = (const char *)memory_inst->memory_data_end;
  549. while (str < str_end && *str != '\0')
  550. str++;
  551. if (str == str_end)
  552. goto fail;
  553. }
  554. SHARED_MEMORY_UNLOCK(memory_inst);
  555. #endif
  556. success:
  557. *p_native_addr = (void *)native_addr;
  558. return true;
  559. #ifndef OS_ENABLE_HW_BOUND_CHECK
  560. fail:
  561. SHARED_MEMORY_UNLOCK(memory_inst);
  562. wasm_set_exception(module_inst, "out of bounds memory access");
  563. return false;
  564. #endif
  565. }
  566. WASMMemoryInstance *
  567. wasm_get_default_memory(WASMModuleInstance *module_inst)
  568. {
  569. if (module_inst->memories)
  570. return module_inst->memories[0];
  571. else
  572. return NULL;
  573. }
  574. WASMMemoryInstance *
  575. wasm_get_memory_with_idx(WASMModuleInstance *module_inst, uint32 index)
  576. {
  577. bh_assert(index < module_inst->memory_count);
  578. if (module_inst->memories)
  579. return module_inst->memories[index];
  580. else
  581. return NULL;
  582. }
  583. void
  584. wasm_runtime_set_mem_bound_check_bytes(WASMMemoryInstance *memory,
  585. uint64 memory_data_size)
  586. {
  587. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 || WASM_ENABLE_AOT != 0
  588. #if UINTPTR_MAX == UINT64_MAX
  589. memory->mem_bound_check_1byte.u64 = memory_data_size - 1;
  590. memory->mem_bound_check_2bytes.u64 = memory_data_size - 2;
  591. memory->mem_bound_check_4bytes.u64 = memory_data_size - 4;
  592. memory->mem_bound_check_8bytes.u64 = memory_data_size - 8;
  593. memory->mem_bound_check_16bytes.u64 = memory_data_size - 16;
  594. #else
  595. memory->mem_bound_check_1byte.u32[0] = (uint32)memory_data_size - 1;
  596. memory->mem_bound_check_2bytes.u32[0] = (uint32)memory_data_size - 2;
  597. memory->mem_bound_check_4bytes.u32[0] = (uint32)memory_data_size - 4;
  598. memory->mem_bound_check_8bytes.u32[0] = (uint32)memory_data_size - 8;
  599. memory->mem_bound_check_16bytes.u32[0] = (uint32)memory_data_size - 16;
  600. #endif
  601. #endif
  602. }
  603. static void
  604. wasm_munmap_linear_memory(void *mapped_mem, uint64 commit_size, uint64 map_size)
  605. {
  606. #ifdef BH_PLATFORM_WINDOWS
  607. os_mem_decommit(mapped_mem, commit_size);
  608. #else
  609. (void)commit_size;
  610. #endif
  611. os_munmap(mapped_mem, map_size);
  612. }
  613. static void *
  614. wasm_mremap_linear_memory(void *mapped_mem, uint64 old_size, uint64 new_size,
  615. uint64 commit_size)
  616. {
  617. void *new_mem;
  618. bh_assert(new_size > 0);
  619. bh_assert(new_size > old_size);
  620. if (mapped_mem) {
  621. new_mem = os_mremap(mapped_mem, old_size, new_size);
  622. }
  623. else {
  624. new_mem = os_mmap(NULL, new_size, MMAP_PROT_NONE, MMAP_MAP_NONE,
  625. os_get_invalid_handle());
  626. }
  627. if (!new_mem) {
  628. return NULL;
  629. }
  630. #ifdef BH_PLATFORM_WINDOWS
  631. if (commit_size > 0
  632. && !os_mem_commit(new_mem, commit_size,
  633. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  634. os_munmap(new_mem, new_size);
  635. return NULL;
  636. }
  637. #endif
  638. if (os_mprotect(new_mem, commit_size, MMAP_PROT_READ | MMAP_PROT_WRITE)
  639. != 0) {
  640. wasm_munmap_linear_memory(new_mem, new_size, new_size);
  641. return NULL;
  642. }
  643. return new_mem;
  644. }
  645. static void *
  646. wasm_mmap_linear_memory(uint64_t map_size, uint64 commit_size)
  647. {
  648. return wasm_mremap_linear_memory(NULL, 0, map_size, commit_size);
  649. }
  650. bool
  651. wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count,
  652. uint32 memidx)
  653. {
  654. #if WASM_ENABLE_MULTI_MEMORY != 0
  655. WASMMemoryInstance *memory = wasm_get_memory_with_idx(module, memidx);
  656. #else
  657. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  658. #endif
  659. uint8 *memory_data_old, *memory_data_new, *heap_data_old;
  660. uint32 num_bytes_per_page, heap_size;
  661. uint32 cur_page_count, max_page_count, total_page_count;
  662. uint64 total_size_old = 0, total_size_new;
  663. bool ret = true, full_size_mmaped;
  664. enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
  665. if (!memory) {
  666. ret = false;
  667. goto return_func;
  668. }
  669. #ifdef OS_ENABLE_HW_BOUND_CHECK
  670. full_size_mmaped = true;
  671. #elif WASM_ENABLE_SHARED_MEMORY != 0
  672. full_size_mmaped = shared_memory_is_shared(memory);
  673. #else
  674. full_size_mmaped = false;
  675. #endif
  676. memory_data_old = memory->memory_data;
  677. total_size_old = memory->memory_data_size;
  678. heap_data_old = memory->heap_data;
  679. heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
  680. num_bytes_per_page = memory->num_bytes_per_page;
  681. cur_page_count = memory->cur_page_count;
  682. max_page_count = memory->max_page_count;
  683. total_page_count = inc_page_count + cur_page_count;
  684. total_size_new = num_bytes_per_page * (uint64)total_page_count;
  685. if (inc_page_count <= 0)
  686. /* No need to enlarge memory */
  687. return true;
  688. if (total_page_count < cur_page_count) { /* integer overflow */
  689. ret = false;
  690. goto return_func;
  691. }
  692. if (total_page_count > max_page_count) {
  693. failure_reason = MAX_SIZE_REACHED;
  694. ret = false;
  695. goto return_func;
  696. }
  697. bh_assert(total_size_new
  698. <= GET_MAX_LINEAR_MEMORY_SIZE(memory->is_memory64));
  699. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  700. if (!(memory_data_new =
  701. realloc_func(Alloc_For_LinearMemory, full_size_mmaped,
  702. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  703. NULL,
  704. #endif
  705. memory_data_old, total_size_new))) {
  706. ret = false;
  707. goto return_func;
  708. }
  709. if (heap_size > 0) {
  710. if (mem_allocator_migrate(memory->heap_handle,
  711. (char *)heap_data_old
  712. + (memory_data_new - memory_data_old),
  713. heap_size)
  714. != 0) {
  715. ret = false;
  716. }
  717. }
  718. memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
  719. memory->heap_data_end = memory->heap_data + heap_size;
  720. memory->memory_data = memory_data_new;
  721. #else
  722. if (full_size_mmaped) {
  723. #ifdef BH_PLATFORM_WINDOWS
  724. if (!os_mem_commit(memory->memory_data_end,
  725. (mem_offset_t)(total_size_new - total_size_old),
  726. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  727. ret = false;
  728. goto return_func;
  729. }
  730. #endif
  731. if (os_mprotect(memory->memory_data_end,
  732. (mem_offset_t)(total_size_new - total_size_old),
  733. MMAP_PROT_READ | MMAP_PROT_WRITE)
  734. != 0) {
  735. #ifdef BH_PLATFORM_WINDOWS
  736. os_mem_decommit(memory->memory_data_end,
  737. (mem_offset_t)(total_size_new - total_size_old));
  738. #endif
  739. ret = false;
  740. goto return_func;
  741. }
  742. }
  743. else {
  744. if (heap_size > 0) {
  745. if (mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  746. wasm_runtime_show_app_heap_corrupted_prompt();
  747. ret = false;
  748. goto return_func;
  749. }
  750. }
  751. if (!(memory_data_new =
  752. wasm_mremap_linear_memory(memory_data_old, total_size_old,
  753. total_size_new, total_size_new))) {
  754. ret = false;
  755. goto return_func;
  756. }
  757. if (heap_size > 0) {
  758. if (mem_allocator_migrate(memory->heap_handle,
  759. (char *)heap_data_old
  760. + (memory_data_new - memory_data_old),
  761. heap_size)
  762. != 0) {
  763. /* Don't return here as memory->memory_data is obsolete and
  764. must be updated to be correctly used later. */
  765. ret = false;
  766. }
  767. }
  768. memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
  769. memory->heap_data_end = memory->heap_data + heap_size;
  770. memory->memory_data = memory_data_new;
  771. #if defined(os_writegsbase)
  772. /* write base addr of linear memory to GS segment register */
  773. os_writegsbase(memory_data_new);
  774. #endif
  775. }
  776. #endif /* end of WASM_MEM_ALLOC_WITH_USAGE */
  777. /*
  778. * AOT compiler assumes at least 8 byte alignment.
  779. * see aot_check_memory_overflow.
  780. */
  781. bh_assert(((uintptr_t)memory->memory_data & 0x7) == 0);
  782. memory->num_bytes_per_page = num_bytes_per_page;
  783. memory->cur_page_count = total_page_count;
  784. memory->max_page_count = max_page_count;
  785. SET_LINEAR_MEMORY_SIZE(memory, total_size_new);
  786. memory->memory_data_end = memory->memory_data + total_size_new;
  787. wasm_runtime_set_mem_bound_check_bytes(memory, total_size_new);
  788. return_func:
  789. if (!ret && enlarge_memory_error_cb) {
  790. WASMExecEnv *exec_env = NULL;
  791. #if WASM_ENABLE_INTERP != 0
  792. if (module->module_type == Wasm_Module_Bytecode)
  793. exec_env = ((WASMModuleInstance *)module)->cur_exec_env;
  794. #endif
  795. #if WASM_ENABLE_AOT != 0
  796. if (module->module_type == Wasm_Module_AoT)
  797. exec_env = ((AOTModuleInstance *)module)->cur_exec_env;
  798. #endif
  799. enlarge_memory_error_cb(inc_page_count, total_size_old, 0,
  800. failure_reason,
  801. (WASMModuleInstanceCommon *)module, exec_env,
  802. enlarge_memory_error_user_data);
  803. }
  804. return ret;
  805. }
  806. bool
  807. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module_inst,
  808. uint64 inc_page_count)
  809. {
  810. if (inc_page_count > UINT32_MAX) {
  811. return false;
  812. }
  813. #if WASM_ENABLE_AOT != 0
  814. if (module_inst->module_type == Wasm_Module_AoT) {
  815. return aot_enlarge_memory((AOTModuleInstance *)module_inst,
  816. (uint32)inc_page_count);
  817. }
  818. #endif
  819. #if WASM_ENABLE_INTERP != 0
  820. if (module_inst->module_type == Wasm_Module_Bytecode) {
  821. return wasm_enlarge_memory((WASMModuleInstance *)module_inst,
  822. (uint32)inc_page_count);
  823. }
  824. #endif
  825. return false;
  826. }
  827. void
  828. wasm_runtime_set_enlarge_mem_error_callback(
  829. const enlarge_memory_error_callback_t callback, void *user_data)
  830. {
  831. enlarge_memory_error_cb = callback;
  832. enlarge_memory_error_user_data = user_data;
  833. }
  834. bool
  835. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  836. {
  837. bool ret = false;
  838. #if WASM_ENABLE_SHARED_MEMORY != 0
  839. if (module->memory_count > 0)
  840. shared_memory_lock(module->memories[0]);
  841. #endif
  842. ret = wasm_enlarge_memory_internal(module, inc_page_count, 0);
  843. #if WASM_ENABLE_SHARED_MEMORY != 0
  844. if (module->memory_count > 0)
  845. shared_memory_unlock(module->memories[0]);
  846. #endif
  847. return ret;
  848. }
  849. bool
  850. wasm_enlarge_memory_with_idx(WASMModuleInstance *module, uint32 inc_page_count,
  851. uint32 memidx)
  852. {
  853. bool ret = false;
  854. #if WASM_ENABLE_SHARED_MEMORY != 0
  855. if (memidx < module->memory_count)
  856. shared_memory_lock(module->memories[memidx]);
  857. #endif
  858. ret = wasm_enlarge_memory_internal(module, inc_page_count, memidx);
  859. #if WASM_ENABLE_SHARED_MEMORY != 0
  860. if (memidx < module->memory_count)
  861. shared_memory_unlock(module->memories[memidx]);
  862. #endif
  863. return ret;
  864. }
  865. void
  866. wasm_deallocate_linear_memory(WASMMemoryInstance *memory_inst)
  867. {
  868. uint64 map_size;
  869. bh_assert(memory_inst);
  870. bh_assert(memory_inst->memory_data);
  871. #ifndef OS_ENABLE_HW_BOUND_CHECK
  872. #if WASM_ENABLE_SHARED_MEMORY != 0
  873. if (shared_memory_is_shared(memory_inst)) {
  874. map_size = (uint64)memory_inst->num_bytes_per_page
  875. * memory_inst->max_page_count;
  876. }
  877. else
  878. #endif
  879. {
  880. map_size = (uint64)memory_inst->num_bytes_per_page
  881. * memory_inst->cur_page_count;
  882. }
  883. #else
  884. map_size = 8 * (uint64)BH_GB;
  885. #endif
  886. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  887. (void)map_size;
  888. free_func(Alloc_For_LinearMemory,
  889. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  890. NULL,
  891. #endif
  892. memory_inst->memory_data);
  893. #else
  894. wasm_munmap_linear_memory(memory_inst->memory_data,
  895. memory_inst->memory_data_size, map_size);
  896. #endif
  897. memory_inst->memory_data = NULL;
  898. }
  899. int
  900. wasm_allocate_linear_memory(uint8 **data, bool is_shared_memory,
  901. bool is_memory64, uint64 num_bytes_per_page,
  902. uint64 init_page_count, uint64 max_page_count,
  903. uint64 *memory_data_size)
  904. {
  905. uint64 map_size, page_size;
  906. bh_assert(data);
  907. bh_assert(memory_data_size);
  908. #ifndef OS_ENABLE_HW_BOUND_CHECK
  909. #if WASM_ENABLE_SHARED_MEMORY != 0
  910. if (is_shared_memory) {
  911. /* Allocate maximum memory size when memory is shared */
  912. map_size = max_page_count * num_bytes_per_page;
  913. }
  914. else
  915. #endif
  916. {
  917. map_size = init_page_count * num_bytes_per_page;
  918. }
  919. #else /* else of OS_ENABLE_HW_BOUND_CHECK */
  920. /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
  921. * ea = i + memarg.offset
  922. * both i and memarg.offset are u32 in range 0 to 4G
  923. * so the range of ea is 0 to 8G
  924. */
  925. map_size = 8 * (uint64)BH_GB;
  926. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  927. page_size = os_getpagesize();
  928. *memory_data_size = init_page_count * num_bytes_per_page;
  929. bh_assert(*memory_data_size <= GET_MAX_LINEAR_MEMORY_SIZE(is_memory64));
  930. *memory_data_size = align_as_and_cast(*memory_data_size, page_size);
  931. if (map_size > 0) {
  932. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  933. (void)wasm_mmap_linear_memory;
  934. if (!(*data = malloc_func(Alloc_For_LinearMemory,
  935. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  936. NULL,
  937. #endif
  938. *memory_data_size))) {
  939. return BHT_ERROR;
  940. }
  941. #else
  942. if (!(*data = wasm_mmap_linear_memory(map_size, *memory_data_size))) {
  943. return BHT_ERROR;
  944. }
  945. #endif
  946. }
  947. /*
  948. * AOT compiler assumes at least 8 byte alignment.
  949. * see aot_check_memory_overflow.
  950. */
  951. bh_assert(((uintptr_t)*data & 0x7) == 0);
  952. return BHT_OK;
  953. }