wasm_memory.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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()
  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()
  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 initialize.\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 initialize.\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. return wasm_runtime_malloc_internal(size);
  261. }
  262. void *
  263. wasm_runtime_realloc(void *ptr, unsigned int size)
  264. {
  265. return wasm_runtime_realloc_internal(ptr, size);
  266. }
  267. void
  268. wasm_runtime_free(void *ptr)
  269. {
  270. wasm_runtime_free_internal(ptr);
  271. }
  272. bool
  273. wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info)
  274. {
  275. if (memory_mode == MEMORY_MODE_POOL) {
  276. return mem_allocator_get_alloc_info(pool_allocator, mem_alloc_info);
  277. }
  278. return false;
  279. }
  280. bool
  281. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst_comm,
  282. uint64 app_offset, uint64 size)
  283. {
  284. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  285. WASMMemoryInstance *memory_inst;
  286. uint64 max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
  287. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  288. || module_inst_comm->module_type == Wasm_Module_AoT);
  289. if (!is_bounds_checks_enabled(module_inst_comm)) {
  290. return true;
  291. }
  292. memory_inst = wasm_get_default_memory(module_inst);
  293. if (!memory_inst) {
  294. goto fail;
  295. }
  296. #if WASM_ENABLE_MEMORY64 != 0
  297. if (memory_inst->is_memory64)
  298. max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
  299. #endif
  300. /* boundary overflow check */
  301. if (size > max_linear_memory_size
  302. || app_offset > max_linear_memory_size - size) {
  303. goto fail;
  304. }
  305. SHARED_MEMORY_LOCK(memory_inst);
  306. if (app_offset + size <= memory_inst->memory_data_size) {
  307. SHARED_MEMORY_UNLOCK(memory_inst);
  308. return true;
  309. }
  310. SHARED_MEMORY_UNLOCK(memory_inst);
  311. fail:
  312. wasm_set_exception(module_inst, "out of bounds memory access");
  313. return false;
  314. }
  315. bool
  316. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst_comm,
  317. uint64 app_str_offset)
  318. {
  319. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  320. uint64 app_end_offset, max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
  321. char *str, *str_end;
  322. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  323. || module_inst_comm->module_type == Wasm_Module_AoT);
  324. if (!is_bounds_checks_enabled(module_inst_comm)) {
  325. return true;
  326. }
  327. if (!wasm_runtime_get_app_addr_range(module_inst_comm, app_str_offset, NULL,
  328. &app_end_offset))
  329. goto fail;
  330. #if WASM_ENABLE_MEMORY64 != 0
  331. if (module_inst->memories[0]->is_memory64)
  332. max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
  333. #endif
  334. /* boundary overflow check, max start offset can only be size - 1, while end
  335. * offset can be size */
  336. if (app_str_offset >= max_linear_memory_size
  337. || app_end_offset > max_linear_memory_size)
  338. goto fail;
  339. str = wasm_runtime_addr_app_to_native(module_inst_comm, app_str_offset);
  340. str_end = str + (app_end_offset - app_str_offset);
  341. while (str < str_end && *str != '\0')
  342. str++;
  343. if (str == str_end)
  344. goto fail;
  345. return true;
  346. fail:
  347. wasm_set_exception(module_inst, "out of bounds memory access");
  348. return false;
  349. }
  350. bool
  351. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst_comm,
  352. void *native_ptr, uint64 size)
  353. {
  354. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  355. WASMMemoryInstance *memory_inst;
  356. uint8 *addr = (uint8 *)native_ptr;
  357. uint64 max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
  358. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  359. || module_inst_comm->module_type == Wasm_Module_AoT);
  360. if (!is_bounds_checks_enabled(module_inst_comm)) {
  361. return true;
  362. }
  363. memory_inst = wasm_get_default_memory(module_inst);
  364. if (!memory_inst) {
  365. goto fail;
  366. }
  367. #if WASM_ENABLE_MEMORY64 != 0
  368. if (memory_inst->is_memory64)
  369. max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
  370. #endif
  371. /* boundary overflow check */
  372. if (size > max_linear_memory_size || (uintptr_t)addr > UINTPTR_MAX - size) {
  373. goto fail;
  374. }
  375. SHARED_MEMORY_LOCK(memory_inst);
  376. if (memory_inst->memory_data <= addr
  377. && addr + size <= memory_inst->memory_data_end) {
  378. SHARED_MEMORY_UNLOCK(memory_inst);
  379. return true;
  380. }
  381. SHARED_MEMORY_UNLOCK(memory_inst);
  382. fail:
  383. wasm_set_exception(module_inst, "out of bounds memory access");
  384. return false;
  385. }
  386. void *
  387. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst_comm,
  388. uint64 app_offset)
  389. {
  390. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  391. WASMMemoryInstance *memory_inst;
  392. uint8 *addr;
  393. bool bounds_checks;
  394. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  395. || module_inst_comm->module_type == Wasm_Module_AoT);
  396. bounds_checks = is_bounds_checks_enabled(module_inst_comm);
  397. memory_inst = wasm_get_default_memory(module_inst);
  398. if (!memory_inst) {
  399. return NULL;
  400. }
  401. SHARED_MEMORY_LOCK(memory_inst);
  402. addr = memory_inst->memory_data + (uintptr_t)app_offset;
  403. if (bounds_checks) {
  404. if (memory_inst->memory_data <= addr
  405. && addr < memory_inst->memory_data_end) {
  406. SHARED_MEMORY_UNLOCK(memory_inst);
  407. return addr;
  408. }
  409. SHARED_MEMORY_UNLOCK(memory_inst);
  410. return NULL;
  411. }
  412. /* If bounds checks is disabled, return the address directly */
  413. SHARED_MEMORY_UNLOCK(memory_inst);
  414. return addr;
  415. }
  416. uint64
  417. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst_comm,
  418. void *native_ptr)
  419. {
  420. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  421. WASMMemoryInstance *memory_inst;
  422. uint8 *addr = (uint8 *)native_ptr;
  423. bool bounds_checks;
  424. uint64 ret;
  425. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  426. || module_inst_comm->module_type == Wasm_Module_AoT);
  427. bounds_checks = is_bounds_checks_enabled(module_inst_comm);
  428. memory_inst = wasm_get_default_memory(module_inst);
  429. if (!memory_inst) {
  430. return 0;
  431. }
  432. SHARED_MEMORY_LOCK(memory_inst);
  433. if (bounds_checks) {
  434. if (memory_inst->memory_data <= addr
  435. && addr < memory_inst->memory_data_end) {
  436. ret = (uint64)(addr - memory_inst->memory_data);
  437. SHARED_MEMORY_UNLOCK(memory_inst);
  438. return ret;
  439. }
  440. }
  441. /* If bounds checks is disabled, return the offset directly */
  442. else if (addr != NULL) {
  443. ret = (uint64)(addr - memory_inst->memory_data);
  444. SHARED_MEMORY_UNLOCK(memory_inst);
  445. return ret;
  446. }
  447. SHARED_MEMORY_UNLOCK(memory_inst);
  448. return 0;
  449. }
  450. bool
  451. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst_comm,
  452. uint64 app_offset, uint64 *p_app_start_offset,
  453. uint64 *p_app_end_offset)
  454. {
  455. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  456. WASMMemoryInstance *memory_inst;
  457. uint64 memory_data_size;
  458. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  459. || module_inst_comm->module_type == Wasm_Module_AoT);
  460. memory_inst = wasm_get_default_memory(module_inst);
  461. if (!memory_inst) {
  462. return false;
  463. }
  464. SHARED_MEMORY_LOCK(memory_inst);
  465. memory_data_size = memory_inst->memory_data_size;
  466. if (app_offset < memory_data_size) {
  467. if (p_app_start_offset)
  468. *p_app_start_offset = 0;
  469. if (p_app_end_offset)
  470. *p_app_end_offset = memory_data_size;
  471. SHARED_MEMORY_UNLOCK(memory_inst);
  472. return true;
  473. }
  474. SHARED_MEMORY_UNLOCK(memory_inst);
  475. return false;
  476. }
  477. bool
  478. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst_comm,
  479. uint8 *native_ptr,
  480. uint8 **p_native_start_addr,
  481. uint8 **p_native_end_addr)
  482. {
  483. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  484. WASMMemoryInstance *memory_inst;
  485. uint8 *addr = (uint8 *)native_ptr;
  486. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  487. || module_inst_comm->module_type == Wasm_Module_AoT);
  488. memory_inst = wasm_get_default_memory(module_inst);
  489. if (!memory_inst) {
  490. return false;
  491. }
  492. SHARED_MEMORY_LOCK(memory_inst);
  493. if (memory_inst->memory_data <= addr
  494. && addr < memory_inst->memory_data_end) {
  495. if (p_native_start_addr)
  496. *p_native_start_addr = memory_inst->memory_data;
  497. if (p_native_end_addr)
  498. *p_native_end_addr = memory_inst->memory_data_end;
  499. SHARED_MEMORY_UNLOCK(memory_inst);
  500. return true;
  501. }
  502. SHARED_MEMORY_UNLOCK(memory_inst);
  503. return false;
  504. }
  505. bool
  506. wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  507. uint64 app_buf_addr, uint64 app_buf_size,
  508. void **p_native_addr)
  509. {
  510. WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
  511. uint8 *native_addr;
  512. bool bounds_checks;
  513. bh_assert(app_buf_addr <= UINTPTR_MAX && app_buf_size <= UINTPTR_MAX);
  514. if (!memory_inst) {
  515. wasm_set_exception(module_inst, "out of bounds memory access");
  516. return false;
  517. }
  518. native_addr = memory_inst->memory_data + (uintptr_t)app_buf_addr;
  519. bounds_checks = is_bounds_checks_enabled((wasm_module_inst_t)module_inst);
  520. if (!bounds_checks) {
  521. if (app_buf_addr == 0) {
  522. native_addr = NULL;
  523. }
  524. goto success;
  525. }
  526. /* No need to check the app_offset and buf_size if memory access
  527. boundary check with hardware trap is enabled */
  528. #ifndef OS_ENABLE_HW_BOUND_CHECK
  529. SHARED_MEMORY_LOCK(memory_inst);
  530. if (app_buf_addr >= memory_inst->memory_data_size) {
  531. goto fail;
  532. }
  533. if (!is_str) {
  534. if (app_buf_size > memory_inst->memory_data_size - app_buf_addr) {
  535. goto fail;
  536. }
  537. }
  538. else {
  539. const char *str, *str_end;
  540. /* The whole string must be in the linear memory */
  541. str = (const char *)native_addr;
  542. str_end = (const char *)memory_inst->memory_data_end;
  543. while (str < str_end && *str != '\0')
  544. str++;
  545. if (str == str_end)
  546. goto fail;
  547. }
  548. SHARED_MEMORY_UNLOCK(memory_inst);
  549. #endif
  550. success:
  551. *p_native_addr = (void *)native_addr;
  552. return true;
  553. #ifndef OS_ENABLE_HW_BOUND_CHECK
  554. fail:
  555. SHARED_MEMORY_UNLOCK(memory_inst);
  556. wasm_set_exception(module_inst, "out of bounds memory access");
  557. return false;
  558. #endif
  559. }
  560. WASMMemoryInstance *
  561. wasm_get_default_memory(WASMModuleInstance *module_inst)
  562. {
  563. if (module_inst->memories)
  564. return module_inst->memories[0];
  565. else
  566. return NULL;
  567. }
  568. void
  569. wasm_runtime_set_mem_bound_check_bytes(WASMMemoryInstance *memory,
  570. uint64 memory_data_size)
  571. {
  572. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 || WASM_ENABLE_AOT != 0
  573. #if UINTPTR_MAX == UINT64_MAX
  574. memory->mem_bound_check_1byte.u64 = memory_data_size - 1;
  575. memory->mem_bound_check_2bytes.u64 = memory_data_size - 2;
  576. memory->mem_bound_check_4bytes.u64 = memory_data_size - 4;
  577. memory->mem_bound_check_8bytes.u64 = memory_data_size - 8;
  578. memory->mem_bound_check_16bytes.u64 = memory_data_size - 16;
  579. #else
  580. memory->mem_bound_check_1byte.u32[0] = (uint32)memory_data_size - 1;
  581. memory->mem_bound_check_2bytes.u32[0] = (uint32)memory_data_size - 2;
  582. memory->mem_bound_check_4bytes.u32[0] = (uint32)memory_data_size - 4;
  583. memory->mem_bound_check_8bytes.u32[0] = (uint32)memory_data_size - 8;
  584. memory->mem_bound_check_16bytes.u32[0] = (uint32)memory_data_size - 16;
  585. #endif
  586. #endif
  587. }
  588. static void
  589. wasm_munmap_linear_memory(void *mapped_mem, uint64 commit_size, uint64 map_size)
  590. {
  591. #ifdef BH_PLATFORM_WINDOWS
  592. os_mem_decommit(mapped_mem, commit_size);
  593. #else
  594. (void)commit_size;
  595. #endif
  596. os_munmap(mapped_mem, map_size);
  597. }
  598. static void *
  599. wasm_mremap_linear_memory(void *mapped_mem, uint64 old_size, uint64 new_size,
  600. uint64 commit_size)
  601. {
  602. void *new_mem;
  603. bh_assert(new_size > 0);
  604. bh_assert(new_size > old_size);
  605. if (mapped_mem) {
  606. new_mem = os_mremap(mapped_mem, old_size, new_size);
  607. }
  608. else {
  609. new_mem = os_mmap(NULL, new_size, MMAP_PROT_NONE, MMAP_MAP_NONE,
  610. os_get_invalid_handle());
  611. }
  612. if (!new_mem) {
  613. return NULL;
  614. }
  615. #ifdef BH_PLATFORM_WINDOWS
  616. if (commit_size > 0
  617. && !os_mem_commit(new_mem, commit_size,
  618. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  619. os_munmap(new_mem, new_size);
  620. return NULL;
  621. }
  622. #endif
  623. if (os_mprotect(new_mem, commit_size, MMAP_PROT_READ | MMAP_PROT_WRITE)
  624. != 0) {
  625. wasm_munmap_linear_memory(new_mem, new_size, new_size);
  626. return NULL;
  627. }
  628. return new_mem;
  629. }
  630. static void *
  631. wasm_mmap_linear_memory(uint64_t map_size, uint64 commit_size)
  632. {
  633. return wasm_mremap_linear_memory(NULL, 0, map_size, commit_size);
  634. }
  635. bool
  636. wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
  637. {
  638. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  639. uint8 *memory_data_old, *memory_data_new, *heap_data_old;
  640. uint32 num_bytes_per_page, heap_size;
  641. uint32 cur_page_count, max_page_count, total_page_count;
  642. uint64 total_size_old = 0, total_size_new;
  643. bool ret = true, full_size_mmaped;
  644. enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
  645. if (!memory) {
  646. ret = false;
  647. goto return_func;
  648. }
  649. #ifdef OS_ENABLE_HW_BOUND_CHECK
  650. full_size_mmaped = true;
  651. #elif WASM_ENABLE_SHARED_MEMORY != 0
  652. full_size_mmaped = shared_memory_is_shared(memory);
  653. #else
  654. full_size_mmaped = false;
  655. #endif
  656. memory_data_old = memory->memory_data;
  657. total_size_old = memory->memory_data_size;
  658. heap_data_old = memory->heap_data;
  659. heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
  660. num_bytes_per_page = memory->num_bytes_per_page;
  661. cur_page_count = memory->cur_page_count;
  662. max_page_count = memory->max_page_count;
  663. total_page_count = inc_page_count + cur_page_count;
  664. total_size_new = num_bytes_per_page * (uint64)total_page_count;
  665. if (inc_page_count <= 0)
  666. /* No need to enlarge memory */
  667. return true;
  668. if (total_page_count < cur_page_count) { /* integer overflow */
  669. ret = false;
  670. goto return_func;
  671. }
  672. if (total_page_count > max_page_count) {
  673. failure_reason = MAX_SIZE_REACHED;
  674. ret = false;
  675. goto return_func;
  676. }
  677. bh_assert(total_size_new
  678. <= GET_MAX_LINEAR_MEMORY_SIZE(memory->is_memory64));
  679. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  680. if (!(memory_data_new =
  681. realloc_func(Alloc_For_LinearMemory, full_size_mmaped,
  682. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  683. NULL,
  684. #endif
  685. memory_data_old, 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. ret = false;
  696. }
  697. }
  698. memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
  699. memory->heap_data_end = memory->heap_data + heap_size;
  700. memory->memory_data = memory_data_new;
  701. #else
  702. if (full_size_mmaped) {
  703. #ifdef BH_PLATFORM_WINDOWS
  704. if (!os_mem_commit(memory->memory_data_end,
  705. (mem_offset_t)(total_size_new - total_size_old),
  706. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  707. ret = false;
  708. goto return_func;
  709. }
  710. #endif
  711. if (os_mprotect(memory->memory_data_end,
  712. (mem_offset_t)(total_size_new - total_size_old),
  713. MMAP_PROT_READ | MMAP_PROT_WRITE)
  714. != 0) {
  715. #ifdef BH_PLATFORM_WINDOWS
  716. os_mem_decommit(memory->memory_data_end,
  717. (mem_offset_t)(total_size_new - total_size_old));
  718. #endif
  719. ret = false;
  720. goto return_func;
  721. }
  722. }
  723. else {
  724. if (heap_size > 0) {
  725. if (mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  726. wasm_runtime_show_app_heap_corrupted_prompt();
  727. ret = false;
  728. goto return_func;
  729. }
  730. }
  731. if (!(memory_data_new =
  732. wasm_mremap_linear_memory(memory_data_old, total_size_old,
  733. total_size_new, total_size_new))) {
  734. ret = false;
  735. goto return_func;
  736. }
  737. if (heap_size > 0) {
  738. if (mem_allocator_migrate(memory->heap_handle,
  739. (char *)heap_data_old
  740. + (memory_data_new - memory_data_old),
  741. heap_size)
  742. != 0) {
  743. /* Don't return here as memory->memory_data is obsolete and
  744. must be updated to be correctly used later. */
  745. ret = false;
  746. }
  747. }
  748. memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
  749. memory->heap_data_end = memory->heap_data + heap_size;
  750. memory->memory_data = memory_data_new;
  751. #if defined(os_writegsbase)
  752. /* write base addr of linear memory to GS segment register */
  753. os_writegsbase(memory_data_new);
  754. #endif
  755. }
  756. #endif /* end of WASM_MEM_ALLOC_WITH_USAGE */
  757. memory->num_bytes_per_page = num_bytes_per_page;
  758. memory->cur_page_count = total_page_count;
  759. memory->max_page_count = max_page_count;
  760. SET_LINEAR_MEMORY_SIZE(memory, total_size_new);
  761. memory->memory_data_end = memory->memory_data + total_size_new;
  762. wasm_runtime_set_mem_bound_check_bytes(memory, total_size_new);
  763. return_func:
  764. if (!ret && enlarge_memory_error_cb) {
  765. WASMExecEnv *exec_env = NULL;
  766. #if WASM_ENABLE_INTERP != 0
  767. if (module->module_type == Wasm_Module_Bytecode)
  768. exec_env = ((WASMModuleInstance *)module)->cur_exec_env;
  769. #endif
  770. #if WASM_ENABLE_AOT != 0
  771. if (module->module_type == Wasm_Module_AoT)
  772. exec_env = ((AOTModuleInstance *)module)->cur_exec_env;
  773. #endif
  774. enlarge_memory_error_cb(inc_page_count, total_size_old, 0,
  775. failure_reason,
  776. (WASMModuleInstanceCommon *)module, exec_env,
  777. enlarge_memory_error_user_data);
  778. }
  779. return ret;
  780. }
  781. void
  782. wasm_runtime_set_enlarge_mem_error_callback(
  783. const enlarge_memory_error_callback_t callback, void *user_data)
  784. {
  785. enlarge_memory_error_cb = callback;
  786. enlarge_memory_error_user_data = user_data;
  787. }
  788. bool
  789. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  790. {
  791. bool ret = false;
  792. #if WASM_ENABLE_SHARED_MEMORY != 0
  793. if (module->memory_count > 0)
  794. shared_memory_lock(module->memories[0]);
  795. #endif
  796. ret = wasm_enlarge_memory_internal(module, inc_page_count);
  797. #if WASM_ENABLE_SHARED_MEMORY != 0
  798. if (module->memory_count > 0)
  799. shared_memory_unlock(module->memories[0]);
  800. #endif
  801. return ret;
  802. }
  803. void
  804. wasm_deallocate_linear_memory(WASMMemoryInstance *memory_inst)
  805. {
  806. uint64 map_size;
  807. bh_assert(memory_inst);
  808. bh_assert(memory_inst->memory_data);
  809. #ifndef OS_ENABLE_HW_BOUND_CHECK
  810. #if WASM_ENABLE_SHARED_MEMORY != 0
  811. if (shared_memory_is_shared(memory_inst)) {
  812. map_size = (uint64)memory_inst->num_bytes_per_page
  813. * memory_inst->max_page_count;
  814. }
  815. else
  816. #endif
  817. {
  818. map_size = (uint64)memory_inst->num_bytes_per_page
  819. * memory_inst->cur_page_count;
  820. }
  821. #else
  822. map_size = 8 * (uint64)BH_GB;
  823. #endif
  824. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  825. (void)map_size;
  826. free_func(Alloc_For_LinearMemory,
  827. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  828. NULL,
  829. #endif
  830. memory_inst->memory_data);
  831. #else
  832. wasm_munmap_linear_memory(memory_inst->memory_data,
  833. memory_inst->memory_data_size, map_size);
  834. #endif
  835. memory_inst->memory_data = NULL;
  836. }
  837. int
  838. wasm_allocate_linear_memory(uint8 **data, bool is_shared_memory,
  839. bool is_memory64, uint64 num_bytes_per_page,
  840. uint64 init_page_count, uint64 max_page_count,
  841. uint64 *memory_data_size)
  842. {
  843. uint64 map_size, page_size;
  844. bh_assert(data);
  845. bh_assert(memory_data_size);
  846. #ifndef OS_ENABLE_HW_BOUND_CHECK
  847. #if WASM_ENABLE_SHARED_MEMORY != 0
  848. if (is_shared_memory) {
  849. /* Allocate maximum memory size when memory is shared */
  850. map_size = max_page_count * num_bytes_per_page;
  851. }
  852. else
  853. #endif
  854. {
  855. map_size = init_page_count * num_bytes_per_page;
  856. }
  857. #else /* else of OS_ENABLE_HW_BOUND_CHECK */
  858. /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
  859. * ea = i + memarg.offset
  860. * both i and memarg.offset are u32 in range 0 to 4G
  861. * so the range of ea is 0 to 8G
  862. */
  863. map_size = 8 * (uint64)BH_GB;
  864. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  865. page_size = os_getpagesize();
  866. *memory_data_size = init_page_count * num_bytes_per_page;
  867. #if WASM_ENABLE_MEMORY64 != 0
  868. if (is_memory64) {
  869. bh_assert(*memory_data_size <= MAX_LINEAR_MEM64_MEMORY_SIZE);
  870. }
  871. else
  872. #endif
  873. {
  874. bh_assert(*memory_data_size <= MAX_LINEAR_MEMORY_SIZE);
  875. }
  876. *memory_data_size = align_as_and_cast(*memory_data_size, page_size);
  877. if (map_size > 0) {
  878. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  879. (void)wasm_mmap_linear_memory;
  880. if (!(*data = malloc_func(Alloc_For_LinearMemory,
  881. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  882. NULL,
  883. #endif
  884. *memory_data_size))) {
  885. return BHT_ERROR;
  886. }
  887. #else
  888. if (!(*data = wasm_mmap_linear_memory(map_size, *memory_data_size))) {
  889. return BHT_ERROR;
  890. }
  891. #endif
  892. }
  893. return BHT_OK;
  894. }