wasm_memory.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. bh_assert(*memory_data_size <= GET_MAX_LINEAR_MEMORY_SIZE(is_memory64));
  868. *memory_data_size = align_as_and_cast(*memory_data_size, page_size);
  869. if (map_size > 0) {
  870. #if WASM_MEM_ALLOC_WITH_USAGE != 0
  871. (void)wasm_mmap_linear_memory;
  872. if (!(*data = malloc_func(Alloc_For_LinearMemory,
  873. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  874. NULL,
  875. #endif
  876. *memory_data_size))) {
  877. return BHT_ERROR;
  878. }
  879. #else
  880. if (!(*data = wasm_mmap_linear_memory(map_size, *memory_data_size))) {
  881. return BHT_ERROR;
  882. }
  883. #endif
  884. }
  885. return BHT_OK;
  886. }