wasm_memory.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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 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. void
  575. wasm_runtime_set_mem_bound_check_bytes(WASMMemoryInstance *memory,
  576. uint64 memory_data_size)
  577. {
  578. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 || WASM_ENABLE_AOT != 0
  579. #if UINTPTR_MAX == UINT64_MAX
  580. memory->mem_bound_check_1byte.u64 = memory_data_size - 1;
  581. memory->mem_bound_check_2bytes.u64 = memory_data_size - 2;
  582. memory->mem_bound_check_4bytes.u64 = memory_data_size - 4;
  583. memory->mem_bound_check_8bytes.u64 = memory_data_size - 8;
  584. memory->mem_bound_check_16bytes.u64 = memory_data_size - 16;
  585. #else
  586. memory->mem_bound_check_1byte.u32[0] = (uint32)memory_data_size - 1;
  587. memory->mem_bound_check_2bytes.u32[0] = (uint32)memory_data_size - 2;
  588. memory->mem_bound_check_4bytes.u32[0] = (uint32)memory_data_size - 4;
  589. memory->mem_bound_check_8bytes.u32[0] = (uint32)memory_data_size - 8;
  590. memory->mem_bound_check_16bytes.u32[0] = (uint32)memory_data_size - 16;
  591. #endif
  592. #endif
  593. }
  594. static void
  595. wasm_munmap_linear_memory(void *mapped_mem, uint64 commit_size, uint64 map_size)
  596. {
  597. #ifdef BH_PLATFORM_WINDOWS
  598. os_mem_decommit(mapped_mem, commit_size);
  599. #else
  600. (void)commit_size;
  601. #endif
  602. os_munmap(mapped_mem, map_size);
  603. }
  604. static void *
  605. wasm_mremap_linear_memory(void *mapped_mem, uint64 old_size, uint64 new_size,
  606. uint64 commit_size)
  607. {
  608. void *new_mem;
  609. bh_assert(new_size > 0);
  610. bh_assert(new_size > old_size);
  611. if (mapped_mem) {
  612. new_mem = os_mremap(mapped_mem, old_size, new_size);
  613. }
  614. else {
  615. new_mem = os_mmap(NULL, new_size, MMAP_PROT_NONE, MMAP_MAP_NONE,
  616. os_get_invalid_handle());
  617. }
  618. if (!new_mem) {
  619. return NULL;
  620. }
  621. #ifdef BH_PLATFORM_WINDOWS
  622. if (commit_size > 0
  623. && !os_mem_commit(new_mem, commit_size,
  624. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  625. os_munmap(new_mem, new_size);
  626. return NULL;
  627. }
  628. #endif
  629. if (os_mprotect(new_mem, commit_size, MMAP_PROT_READ | MMAP_PROT_WRITE)
  630. != 0) {
  631. wasm_munmap_linear_memory(new_mem, new_size, new_size);
  632. return NULL;
  633. }
  634. return new_mem;
  635. }
  636. static void *
  637. wasm_mmap_linear_memory(uint64_t map_size, uint64 commit_size)
  638. {
  639. return wasm_mremap_linear_memory(NULL, 0, map_size, commit_size);
  640. }
  641. bool
  642. wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
  643. {
  644. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  645. uint8 *memory_data_old, *memory_data_new, *heap_data_old;
  646. uint32 num_bytes_per_page, heap_size;
  647. uint32 cur_page_count, max_page_count, total_page_count;
  648. uint64 total_size_old = 0, total_size_new;
  649. bool ret = true, full_size_mmaped;
  650. enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
  651. if (!memory) {
  652. ret = false;
  653. goto return_func;
  654. }
  655. #ifdef OS_ENABLE_HW_BOUND_CHECK
  656. full_size_mmaped = true;
  657. #elif WASM_ENABLE_SHARED_MEMORY != 0
  658. full_size_mmaped = shared_memory_is_shared(memory);
  659. #else
  660. full_size_mmaped = false;
  661. #endif
  662. memory_data_old = memory->memory_data;
  663. total_size_old = memory->memory_data_size;
  664. heap_data_old = memory->heap_data;
  665. heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
  666. num_bytes_per_page = memory->num_bytes_per_page;
  667. cur_page_count = memory->cur_page_count;
  668. max_page_count = memory->max_page_count;
  669. total_page_count = inc_page_count + cur_page_count;
  670. total_size_new = num_bytes_per_page * (uint64)total_page_count;
  671. if (inc_page_count <= 0)
  672. /* No need to enlarge memory */
  673. return true;
  674. if (total_page_count < cur_page_count) { /* integer overflow */
  675. ret = false;
  676. goto return_func;
  677. }
  678. if (total_page_count > max_page_count) {
  679. failure_reason = MAX_SIZE_REACHED;
  680. ret = false;
  681. goto return_func;
  682. }
  683. bh_assert(total_size_new
  684. <= GET_MAX_LINEAR_MEMORY_SIZE(memory->is_memory64));
  685. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  686. if (!(memory_data_new =
  687. realloc_func(Alloc_For_LinearMemory, full_size_mmaped,
  688. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  689. NULL,
  690. #endif
  691. memory_data_old, total_size_new))) {
  692. ret = false;
  693. goto return_func;
  694. }
  695. if (heap_size > 0) {
  696. if (mem_allocator_migrate(memory->heap_handle,
  697. (char *)heap_data_old
  698. + (memory_data_new - memory_data_old),
  699. heap_size)
  700. != 0) {
  701. ret = false;
  702. }
  703. }
  704. memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
  705. memory->heap_data_end = memory->heap_data + heap_size;
  706. memory->memory_data = memory_data_new;
  707. #else
  708. if (full_size_mmaped) {
  709. #ifdef BH_PLATFORM_WINDOWS
  710. if (!os_mem_commit(memory->memory_data_end,
  711. (mem_offset_t)(total_size_new - total_size_old),
  712. MMAP_PROT_READ | MMAP_PROT_WRITE)) {
  713. ret = false;
  714. goto return_func;
  715. }
  716. #endif
  717. if (os_mprotect(memory->memory_data_end,
  718. (mem_offset_t)(total_size_new - total_size_old),
  719. MMAP_PROT_READ | MMAP_PROT_WRITE)
  720. != 0) {
  721. #ifdef BH_PLATFORM_WINDOWS
  722. os_mem_decommit(memory->memory_data_end,
  723. (mem_offset_t)(total_size_new - total_size_old));
  724. #endif
  725. ret = false;
  726. goto return_func;
  727. }
  728. }
  729. else {
  730. if (heap_size > 0) {
  731. if (mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  732. wasm_runtime_show_app_heap_corrupted_prompt();
  733. ret = false;
  734. goto return_func;
  735. }
  736. }
  737. if (!(memory_data_new =
  738. wasm_mremap_linear_memory(memory_data_old, total_size_old,
  739. total_size_new, total_size_new))) {
  740. ret = false;
  741. goto return_func;
  742. }
  743. if (heap_size > 0) {
  744. if (mem_allocator_migrate(memory->heap_handle,
  745. (char *)heap_data_old
  746. + (memory_data_new - memory_data_old),
  747. heap_size)
  748. != 0) {
  749. /* Don't return here as memory->memory_data is obsolete and
  750. must be updated to be correctly used later. */
  751. ret = false;
  752. }
  753. }
  754. memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
  755. memory->heap_data_end = memory->heap_data + heap_size;
  756. memory->memory_data = memory_data_new;
  757. #if defined(os_writegsbase)
  758. /* write base addr of linear memory to GS segment register */
  759. os_writegsbase(memory_data_new);
  760. #endif
  761. }
  762. #endif /* end of WASM_MEM_ALLOC_WITH_USAGE */
  763. /*
  764. * AOT compiler assumes at least 8 byte alignment.
  765. * see aot_check_memory_overflow.
  766. */
  767. bh_assert(((uintptr_t)memory->memory_data & 0x7) == 0);
  768. memory->num_bytes_per_page = num_bytes_per_page;
  769. memory->cur_page_count = total_page_count;
  770. memory->max_page_count = max_page_count;
  771. SET_LINEAR_MEMORY_SIZE(memory, total_size_new);
  772. memory->memory_data_end = memory->memory_data + total_size_new;
  773. wasm_runtime_set_mem_bound_check_bytes(memory, total_size_new);
  774. return_func:
  775. if (!ret && enlarge_memory_error_cb) {
  776. WASMExecEnv *exec_env = NULL;
  777. #if WASM_ENABLE_INTERP != 0
  778. if (module->module_type == Wasm_Module_Bytecode)
  779. exec_env = ((WASMModuleInstance *)module)->cur_exec_env;
  780. #endif
  781. #if WASM_ENABLE_AOT != 0
  782. if (module->module_type == Wasm_Module_AoT)
  783. exec_env = ((AOTModuleInstance *)module)->cur_exec_env;
  784. #endif
  785. enlarge_memory_error_cb(inc_page_count, total_size_old, 0,
  786. failure_reason,
  787. (WASMModuleInstanceCommon *)module, exec_env,
  788. enlarge_memory_error_user_data);
  789. }
  790. return ret;
  791. }
  792. bool
  793. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module_inst,
  794. uint64 inc_page_count)
  795. {
  796. if (inc_page_count > UINT32_MAX) {
  797. return false;
  798. }
  799. #if WASM_ENABLE_AOT != 0
  800. if (module_inst->module_type == Wasm_Module_AoT) {
  801. return aot_enlarge_memory((AOTModuleInstance *)module_inst,
  802. (uint32)inc_page_count);
  803. }
  804. #endif
  805. #if WASM_ENABLE_INTERP != 0
  806. if (module_inst->module_type == Wasm_Module_Bytecode) {
  807. return wasm_enlarge_memory((WASMModuleInstance *)module_inst,
  808. (uint32)inc_page_count);
  809. }
  810. #endif
  811. return false;
  812. }
  813. void
  814. wasm_runtime_set_enlarge_mem_error_callback(
  815. const enlarge_memory_error_callback_t callback, void *user_data)
  816. {
  817. enlarge_memory_error_cb = callback;
  818. enlarge_memory_error_user_data = user_data;
  819. }
  820. bool
  821. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  822. {
  823. bool ret = false;
  824. #if WASM_ENABLE_SHARED_MEMORY != 0
  825. if (module->memory_count > 0)
  826. shared_memory_lock(module->memories[0]);
  827. #endif
  828. ret = wasm_enlarge_memory_internal(module, inc_page_count);
  829. #if WASM_ENABLE_SHARED_MEMORY != 0
  830. if (module->memory_count > 0)
  831. shared_memory_unlock(module->memories[0]);
  832. #endif
  833. return ret;
  834. }
  835. void
  836. wasm_deallocate_linear_memory(WASMMemoryInstance *memory_inst)
  837. {
  838. uint64 map_size;
  839. bh_assert(memory_inst);
  840. bh_assert(memory_inst->memory_data);
  841. #ifndef OS_ENABLE_HW_BOUND_CHECK
  842. #if WASM_ENABLE_SHARED_MEMORY != 0
  843. if (shared_memory_is_shared(memory_inst)) {
  844. map_size = (uint64)memory_inst->num_bytes_per_page
  845. * memory_inst->max_page_count;
  846. }
  847. else
  848. #endif
  849. {
  850. map_size = (uint64)memory_inst->num_bytes_per_page
  851. * memory_inst->cur_page_count;
  852. }
  853. #else
  854. map_size = 8 * (uint64)BH_GB;
  855. #endif
  856. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  857. (void)map_size;
  858. free_func(Alloc_For_LinearMemory,
  859. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  860. NULL,
  861. #endif
  862. memory_inst->memory_data);
  863. #else
  864. wasm_munmap_linear_memory(memory_inst->memory_data,
  865. memory_inst->memory_data_size, map_size);
  866. #endif
  867. memory_inst->memory_data = NULL;
  868. }
  869. int
  870. wasm_allocate_linear_memory(uint8 **data, bool is_shared_memory,
  871. bool is_memory64, uint64 num_bytes_per_page,
  872. uint64 init_page_count, uint64 max_page_count,
  873. uint64 *memory_data_size)
  874. {
  875. uint64 map_size, page_size;
  876. bh_assert(data);
  877. bh_assert(memory_data_size);
  878. #ifndef OS_ENABLE_HW_BOUND_CHECK
  879. #if WASM_ENABLE_SHARED_MEMORY != 0
  880. if (is_shared_memory) {
  881. /* Allocate maximum memory size when memory is shared */
  882. map_size = max_page_count * num_bytes_per_page;
  883. }
  884. else
  885. #endif
  886. {
  887. map_size = init_page_count * num_bytes_per_page;
  888. }
  889. #else /* else of OS_ENABLE_HW_BOUND_CHECK */
  890. /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
  891. * ea = i + memarg.offset
  892. * both i and memarg.offset are u32 in range 0 to 4G
  893. * so the range of ea is 0 to 8G
  894. */
  895. map_size = 8 * (uint64)BH_GB;
  896. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  897. page_size = os_getpagesize();
  898. *memory_data_size = init_page_count * num_bytes_per_page;
  899. bh_assert(*memory_data_size <= GET_MAX_LINEAR_MEMORY_SIZE(is_memory64));
  900. *memory_data_size = align_as_and_cast(*memory_data_size, page_size);
  901. if (map_size > 0) {
  902. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  903. (void)wasm_mmap_linear_memory;
  904. if (!(*data = malloc_func(Alloc_For_LinearMemory,
  905. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  906. NULL,
  907. #endif
  908. *memory_data_size))) {
  909. return BHT_ERROR;
  910. }
  911. #else
  912. if (!(*data = wasm_mmap_linear_memory(map_size, *memory_data_size))) {
  913. return BHT_ERROR;
  914. }
  915. #endif
  916. }
  917. /*
  918. * AOT compiler assumes at least 8 byte alignment.
  919. * see aot_check_memory_overflow.
  920. */
  921. bh_assert(((uintptr_t)*data & 0x7) == 0);
  922. return BHT_OK;
  923. }