wasm_memory.c 33 KB

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