wasm_runtime.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449
  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.h"
  6. #include "wasm_loader.h"
  7. #include "wasm_interp.h"
  8. #include "bh_common.h"
  9. #include "bh_log.h"
  10. #include "mem_alloc.h"
  11. #include "../common/wasm_runtime_common.h"
  12. #if WASM_ENABLE_SHARED_MEMORY != 0
  13. #include "../common/wasm_shared_memory.h"
  14. #endif
  15. #if WASM_ENABLE_THREAD_MGR != 0
  16. #include "../libraries/thread-mgr/thread_manager.h"
  17. #endif
  18. static void
  19. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  20. {
  21. if (error_buf != NULL) {
  22. snprintf(error_buf, error_buf_size,
  23. "WASM module instantiate failed: %s", string);
  24. }
  25. }
  26. static void
  27. set_error_buf_v(char *error_buf, uint32 error_buf_size,
  28. const char *format, ...)
  29. {
  30. va_list args;
  31. char buf[128];
  32. if (error_buf != NULL) {
  33. va_start(args, format);
  34. vsnprintf(buf, sizeof(buf), format, args);
  35. va_end(args);
  36. snprintf(error_buf, error_buf_size,
  37. "WASM module instantiate failed: %s", buf);
  38. }
  39. }
  40. WASMModule*
  41. wasm_load(const uint8 *buf, uint32 size,
  42. char *error_buf, uint32 error_buf_size)
  43. {
  44. return wasm_loader_load(buf, size, error_buf, error_buf_size);
  45. }
  46. WASMModule*
  47. wasm_load_from_sections(WASMSection *section_list,
  48. char *error_buf, uint32_t error_buf_size)
  49. {
  50. return wasm_loader_load_from_sections(section_list,
  51. error_buf, error_buf_size);
  52. }
  53. void
  54. wasm_unload(WASMModule *module)
  55. {
  56. wasm_loader_unload(module);
  57. }
  58. static void *
  59. runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  60. {
  61. void *mem;
  62. if (size >= UINT32_MAX
  63. || !(mem = wasm_runtime_malloc((uint32)size))) {
  64. set_error_buf(error_buf, error_buf_size,
  65. "allocate memory failed");
  66. return NULL;
  67. }
  68. memset(mem, 0, (uint32)size);
  69. return mem;
  70. }
  71. #if WASM_ENABLE_MULTI_MODULE != 0
  72. static WASMModuleInstance *
  73. get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
  74. const WASMModule *sub_module)
  75. {
  76. bh_list *sub_module_inst_list = parent_module_inst->sub_module_inst_list;
  77. WASMSubModInstNode *node = bh_list_first_elem(sub_module_inst_list);
  78. while (node && sub_module != node->module_inst->module) {
  79. node = bh_list_elem_next(node);
  80. }
  81. return node ? node->module_inst : NULL;
  82. }
  83. #endif
  84. /**
  85. * Destroy memory instances.
  86. */
  87. static void
  88. memories_deinstantiate(WASMModuleInstance *module_inst,
  89. WASMMemoryInstance **memories,
  90. uint32 count)
  91. {
  92. uint32 i;
  93. if (memories) {
  94. for (i = 0; i < count; i++) {
  95. if (memories[i]) {
  96. #if WASM_ENABLE_MULTI_MODULE != 0
  97. if (memories[i]->owner != module_inst)
  98. continue;
  99. #endif
  100. #if WASM_ENABLE_SHARED_MEMORY != 0
  101. if (memories[i]->is_shared) {
  102. int32 ref_count =
  103. shared_memory_dec_reference(
  104. (WASMModuleCommon *)module_inst->module);
  105. bh_assert(ref_count >= 0);
  106. /* if the reference count is not zero,
  107. don't free the memory */
  108. if (ref_count > 0)
  109. continue;
  110. }
  111. os_mutex_destroy(&memories[i]->mem_lock);
  112. #endif
  113. if (memories[i]->heap_handle) {
  114. mem_allocator_destroy(memories[i]->heap_handle);
  115. wasm_runtime_free(memories[i]->heap_handle);
  116. memories[i]->heap_handle = NULL;
  117. }
  118. if (memories[i]->memory_data)
  119. wasm_runtime_free(memories[i]->memory_data);
  120. wasm_runtime_free(memories[i]);
  121. }
  122. }
  123. wasm_runtime_free(memories);
  124. }
  125. (void)module_inst;
  126. }
  127. static WASMMemoryInstance*
  128. memory_instantiate(WASMModuleInstance *module_inst,
  129. uint32 num_bytes_per_page,
  130. uint32 init_page_count, uint32 max_page_count,
  131. uint32 heap_size, uint32 flags,
  132. char *error_buf, uint32 error_buf_size)
  133. {
  134. WASMModule *module = module_inst->module;
  135. WASMMemoryInstance *memory;
  136. uint64 memory_data_size;
  137. uint32 heap_offset = num_bytes_per_page * init_page_count;
  138. uint32 inc_page_count, aux_heap_base, global_idx;
  139. uint32 bytes_of_last_page, bytes_to_page_end;
  140. uint8 *global_addr;
  141. #if WASM_ENABLE_SHARED_MEMORY != 0
  142. bool is_shared_memory = flags & 0x02 ? true : false;
  143. /* shared memory */
  144. if (is_shared_memory) {
  145. WASMSharedMemNode *node =
  146. wasm_module_get_shared_memory(
  147. (WASMModuleCommon *)module_inst->module);
  148. /* If the memory of this module has been instantiated,
  149. return the memory instance directly */
  150. if (node) {
  151. uint32 ref_count;
  152. ref_count = shared_memory_inc_reference(
  153. (WASMModuleCommon *)module_inst->module);
  154. bh_assert(ref_count > 0);
  155. memory = (WASMMemoryInstance *)shared_memory_get_memory_inst(node);
  156. bh_assert(memory);
  157. (void)ref_count;
  158. return memory;
  159. }
  160. }
  161. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  162. if (heap_size > 0
  163. && module_inst->module->malloc_function != (uint32)-1
  164. && module_inst->module->free_function != (uint32)-1) {
  165. /* Disable app heap, use malloc/free function exported
  166. by wasm app to allocate/free memory instead */
  167. heap_size = 0;
  168. }
  169. if (init_page_count == max_page_count && init_page_count == 1) {
  170. /* If only one page and at most one page, we just append
  171. the app heap to the end of linear memory, enlarge the
  172. num_bytes_per_page, and don't change the page count*/
  173. heap_offset = num_bytes_per_page;
  174. num_bytes_per_page += heap_size;
  175. if (num_bytes_per_page < heap_size) {
  176. set_error_buf(error_buf, error_buf_size,
  177. "memory size must be at most 65536 pages (4GiB)");
  178. return NULL;
  179. }
  180. }
  181. else if (heap_size > 0) {
  182. if (module->aux_heap_base_global_index != (uint32)-1
  183. && module->aux_heap_base < num_bytes_per_page
  184. * init_page_count) {
  185. /* Insert app heap before __heap_base */
  186. aux_heap_base = module->aux_heap_base;
  187. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  188. if (bytes_of_last_page == 0)
  189. bytes_of_last_page = num_bytes_per_page;
  190. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  191. inc_page_count = (heap_size - bytes_to_page_end
  192. + num_bytes_per_page - 1) / num_bytes_per_page;
  193. heap_offset = aux_heap_base;
  194. aux_heap_base += heap_size;
  195. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  196. if (bytes_of_last_page == 0)
  197. bytes_of_last_page = num_bytes_per_page;
  198. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  199. if (bytes_to_page_end < 1 * BH_KB) {
  200. aux_heap_base += 1 * BH_KB;
  201. inc_page_count++;
  202. }
  203. /* Adjust __heap_base global value */
  204. global_idx = module->aux_heap_base_global_index;
  205. global_addr = module_inst->global_data +
  206. module_inst->globals[global_idx].data_offset;
  207. *(uint32 *)global_addr = aux_heap_base;
  208. LOG_VERBOSE("Reset __heap_base global to %u", aux_heap_base);
  209. }
  210. else {
  211. /* Insert app heap before new page */
  212. inc_page_count = (heap_size + num_bytes_per_page - 1)
  213. / num_bytes_per_page;
  214. heap_offset = num_bytes_per_page * init_page_count;
  215. heap_size = num_bytes_per_page * inc_page_count;
  216. if (heap_size > 0)
  217. heap_size -= 1 * BH_KB;
  218. }
  219. init_page_count += inc_page_count;
  220. max_page_count += inc_page_count;
  221. if (init_page_count > 65536) {
  222. set_error_buf(error_buf, error_buf_size,
  223. "memory size must be at most 65536 pages (4GiB)");
  224. return NULL;
  225. }
  226. if (max_page_count > 65536)
  227. max_page_count = 65536;
  228. }
  229. LOG_VERBOSE("Memory instantiate:");
  230. LOG_VERBOSE(" page bytes: %u, init pages: %u, max pages: %u",
  231. num_bytes_per_page, init_page_count, max_page_count);
  232. LOG_VERBOSE(" heap offset: %u, heap size: %d\n", heap_offset, heap_size);
  233. memory_data_size = (uint64)num_bytes_per_page * init_page_count;
  234. #if WASM_ENABLE_SHARED_MEMORY != 0
  235. if (is_shared_memory) {
  236. /* Allocate max page for shared memory */
  237. memory_data_size = (uint64)num_bytes_per_page * max_page_count;
  238. }
  239. #endif
  240. /* Allocate memory space, addr data and global data */
  241. if (!(memory = runtime_malloc((uint64)sizeof(WASMMemoryInstance),
  242. error_buf, error_buf_size))) {
  243. return NULL;
  244. }
  245. if (memory_data_size > 0
  246. && !(memory->memory_data =
  247. runtime_malloc(memory_data_size,
  248. error_buf, error_buf_size))) {
  249. goto fail1;
  250. }
  251. memory->module_type = Wasm_Module_Bytecode;
  252. memory->num_bytes_per_page = num_bytes_per_page;
  253. memory->cur_page_count = init_page_count;
  254. memory->max_page_count = max_page_count;
  255. memory->heap_data = memory->memory_data + heap_offset;
  256. memory->heap_data_end = memory->heap_data + heap_size;
  257. memory->memory_data_end = memory->memory_data + (uint32)memory_data_size;
  258. /* Initialize heap */
  259. if (heap_size > 0) {
  260. uint32 heap_struct_size = mem_allocator_get_heap_struct_size();
  261. if (!(memory->heap_handle = runtime_malloc((uint64)heap_struct_size,
  262. error_buf, error_buf_size))) {
  263. goto fail2;
  264. }
  265. if (!mem_allocator_create_with_struct_and_pool
  266. (memory->heap_handle, heap_struct_size,
  267. memory->heap_data, heap_size)) {
  268. set_error_buf(error_buf, error_buf_size, "init app heap failed");
  269. goto fail3;
  270. }
  271. }
  272. #if WASM_ENABLE_SHARED_MEMORY != 0
  273. if (0 != os_mutex_init(&memory->mem_lock)) {
  274. set_error_buf(error_buf, error_buf_size, "init mutex failed");
  275. goto fail4;
  276. }
  277. if (is_shared_memory) {
  278. memory->is_shared = true;
  279. if (!shared_memory_set_memory_inst(
  280. (WASMModuleCommon *)module_inst->module,
  281. (WASMMemoryInstanceCommon *)memory)) {
  282. set_error_buf(error_buf, error_buf_size,
  283. "allocate memory failed");
  284. goto fail5;
  285. }
  286. }
  287. #endif
  288. LOG_VERBOSE("Memory instantiate success.");
  289. return memory;
  290. #if WASM_ENABLE_SHARED_MEMORY != 0
  291. fail5:
  292. os_mutex_destroy(&memory->mem_lock);
  293. fail4:
  294. if (heap_size > 0)
  295. mem_allocator_destroy(memory->heap_handle);
  296. #endif
  297. fail3:
  298. if (heap_size > 0)
  299. wasm_runtime_free(memory->heap_handle);
  300. fail2:
  301. if (memory->memory_data)
  302. wasm_runtime_free(memory->memory_data);
  303. fail1:
  304. wasm_runtime_free(memory);
  305. return NULL;
  306. }
  307. /**
  308. * Instantiate memories in a module.
  309. */
  310. static WASMMemoryInstance **
  311. memories_instantiate(const WASMModule *module,
  312. WASMModuleInstance *module_inst,
  313. uint32 heap_size, char *error_buf, uint32 error_buf_size)
  314. {
  315. WASMImport *import;
  316. uint32 mem_index = 0, i, memory_count =
  317. module->import_memory_count + module->memory_count;
  318. uint64 total_size;
  319. WASMMemoryInstance **memories, *memory;
  320. total_size = sizeof(WASMMemoryInstance*) * (uint64)memory_count;
  321. if (!(memories = runtime_malloc(total_size,
  322. error_buf, error_buf_size))) {
  323. return NULL;
  324. }
  325. /* instantiate memories from import section */
  326. import = module->import_memories;
  327. for (i = 0; i < module->import_memory_count; i++, import++) {
  328. uint32 num_bytes_per_page = import->u.memory.num_bytes_per_page;
  329. uint32 init_page_count = import->u.memory.init_page_count;
  330. uint32 max_page_count = import->u.memory.max_page_count;
  331. uint32 flags = import->u.memory.flags;
  332. uint32 actual_heap_size = heap_size;
  333. #if WASM_ENABLE_MULTI_MODULE != 0
  334. if (import->u.memory.import_module != NULL) {
  335. WASMModuleInstance *module_inst_linked;
  336. if (!(module_inst_linked = get_sub_module_inst(
  337. module_inst, import->u.memory.import_module))) {
  338. set_error_buf(error_buf, error_buf_size, "unknown memory");
  339. memories_deinstantiate(module_inst, memories, memory_count);
  340. return NULL;
  341. }
  342. if (!(memory = memories[mem_index++] = wasm_lookup_memory(
  343. module_inst_linked, import->u.memory.field_name))) {
  344. set_error_buf(error_buf, error_buf_size, "unknown memory");
  345. memories_deinstantiate(module_inst, memories, memory_count);
  346. return NULL;
  347. }
  348. }
  349. else
  350. #endif
  351. {
  352. if (!(memory = memories[mem_index++] = memory_instantiate(
  353. module_inst, num_bytes_per_page, init_page_count,
  354. max_page_count, actual_heap_size, flags,
  355. error_buf, error_buf_size))) {
  356. memories_deinstantiate(module_inst, memories, memory_count);
  357. return NULL;
  358. }
  359. #if WASM_ENABLE_MULTI_MODULE != 0
  360. /* The module of the import memory is a builtin module, and
  361. the memory is created by current module, set its owner
  362. to current module, so the memory can be destroyed in
  363. memories_deinstantiate. */
  364. memory->owner = module_inst;
  365. #endif
  366. }
  367. }
  368. /* instantiate memories from memory section */
  369. for (i = 0; i < module->memory_count; i++) {
  370. if (!(memory = memories[mem_index++] =
  371. memory_instantiate(module_inst,
  372. module->memories[i].num_bytes_per_page,
  373. module->memories[i].init_page_count,
  374. module->memories[i].max_page_count,
  375. heap_size, module->memories[i].flags,
  376. error_buf, error_buf_size))) {
  377. memories_deinstantiate(module_inst, memories, memory_count);
  378. return NULL;
  379. }
  380. #if WASM_ENABLE_MULTI_MODULE != 0
  381. memory->owner = module_inst;
  382. #endif
  383. }
  384. if (mem_index == 0) {
  385. /**
  386. * no import memory and define memory, but still need heap
  387. * for wasm code
  388. */
  389. if (!(memory = memories[mem_index++] =
  390. memory_instantiate(module_inst, 0, 0, 0, heap_size, 0,
  391. error_buf, error_buf_size))) {
  392. memories_deinstantiate(module_inst, memories, memory_count);
  393. return NULL;
  394. }
  395. }
  396. bh_assert(mem_index == memory_count);
  397. (void)module_inst;
  398. return memories;
  399. }
  400. /**
  401. * Destroy table instances.
  402. */
  403. static void
  404. tables_deinstantiate(WASMTableInstance **tables, uint32 count)
  405. {
  406. uint32 i;
  407. if (tables) {
  408. for (i = 0; i < count; i++)
  409. if (tables[i])
  410. wasm_runtime_free(tables[i]);
  411. wasm_runtime_free(tables);
  412. }
  413. }
  414. /**
  415. * Instantiate tables in a module.
  416. */
  417. static WASMTableInstance **
  418. tables_instantiate(const WASMModule *module,
  419. WASMModuleInstance *module_inst,
  420. char *error_buf, uint32 error_buf_size)
  421. {
  422. WASMImport *import;
  423. uint32 table_index = 0, i, table_count =
  424. module->import_table_count + module->table_count;
  425. uint64 total_size = sizeof(WASMTableInstance*) * (uint64)table_count;
  426. WASMTableInstance **tables, *table;
  427. if (!(tables = runtime_malloc(total_size,
  428. error_buf, error_buf_size))) {
  429. return NULL;
  430. }
  431. /* instantiate tables from import section */
  432. import = module->import_tables;
  433. for (i = 0; i < module->import_table_count; i++, import++) {
  434. #if WASM_ENABLE_MULTI_MODULE != 0
  435. WASMTableInstance *table_inst_linked = NULL;
  436. WASMModuleInstance *module_inst_linked = NULL;
  437. if (import->u.table.import_module) {
  438. if (!(module_inst_linked =
  439. get_sub_module_inst(module_inst, import->u.table.import_module))) {
  440. set_error_buf(error_buf, error_buf_size, "unknown table");
  441. tables_deinstantiate(tables, table_count);
  442. return NULL;
  443. }
  444. if (!(table_inst_linked = wasm_lookup_table(module_inst_linked,
  445. import->u.table.field_name))) {
  446. set_error_buf(error_buf, error_buf_size, "unknown table");
  447. tables_deinstantiate(tables, table_count);
  448. return NULL;
  449. }
  450. total_size = offsetof(WASMTableInstance, base_addr);
  451. }
  452. else
  453. #endif
  454. {
  455. /* it is a built-in table, every module has its own */
  456. total_size = offsetof(WASMTableInstance, base_addr);
  457. total_size +=
  458. import->u.table.possible_grow
  459. ? sizeof(uint32) * (uint64)import->u.table.max_size
  460. : sizeof(uint32) * (uint64)import->u.table.init_size;
  461. }
  462. if (!(table = tables[table_index++] = runtime_malloc
  463. (total_size, error_buf, error_buf_size))) {
  464. tables_deinstantiate(tables, table_count);
  465. return NULL;
  466. }
  467. /* Set all elements to -1 to mark them as uninitialized elements */
  468. memset(table, -1, (uint32)total_size);
  469. #if WASM_ENABLE_MULTI_MODULE != 0
  470. table->table_inst_linked = table_inst_linked;
  471. if (table_inst_linked != NULL) {
  472. table->elem_type = table_inst_linked->elem_type;
  473. table->cur_size = table_inst_linked->cur_size;
  474. table->max_size = table_inst_linked->max_size;
  475. }
  476. else
  477. #endif
  478. {
  479. table->elem_type = import->u.table.elem_type;
  480. table->cur_size = import->u.table.init_size;
  481. table->max_size = import->u.table.max_size;
  482. }
  483. }
  484. /* instantiate tables from table section */
  485. for (i = 0; i < module->table_count; i++) {
  486. total_size = offsetof(WASMTableInstance, base_addr);
  487. #if WASM_ENABLE_MULTI_MODULE != 0
  488. /* in case, a module which imports this table will grow it */
  489. total_size += sizeof(uint32) * (uint64)module->tables[i].max_size;
  490. #else
  491. total_size += module->tables[i].possible_grow
  492. ? sizeof(uint32) * (uint64)module->tables[i].max_size
  493. : sizeof(uint32) * (uint64)module->tables[i].init_size;
  494. #endif
  495. if (!(table = tables[table_index++] = runtime_malloc
  496. (total_size, error_buf, error_buf_size))) {
  497. tables_deinstantiate(tables, table_count);
  498. return NULL;
  499. }
  500. /* Set all elements to -1 to mark them as uninitialized elements */
  501. memset(table, -1, (uint32)total_size);
  502. table->elem_type = module->tables[i].elem_type;
  503. table->cur_size = module->tables[i].init_size;
  504. table->max_size = module->tables[i].max_size;
  505. #if WASM_ENABLE_MULTI_MODULE != 0
  506. table->table_inst_linked = NULL;
  507. #endif
  508. }
  509. bh_assert(table_index == table_count);
  510. (void)module_inst;
  511. return tables;
  512. }
  513. /**
  514. * Destroy function instances.
  515. */
  516. static void
  517. functions_deinstantiate(WASMFunctionInstance *functions, uint32 count)
  518. {
  519. if (functions) {
  520. wasm_runtime_free(functions);
  521. }
  522. }
  523. /**
  524. * Instantiate functions in a module.
  525. */
  526. static WASMFunctionInstance *
  527. functions_instantiate(const WASMModule *module,
  528. WASMModuleInstance *module_inst,
  529. char *error_buf, uint32 error_buf_size)
  530. {
  531. WASMImport *import;
  532. uint32 i, function_count =
  533. module->import_function_count + module->function_count;
  534. uint64 total_size = sizeof(WASMFunctionInstance) * (uint64)function_count;
  535. WASMFunctionInstance *functions, *function;
  536. if (!(functions = runtime_malloc(total_size,
  537. error_buf, error_buf_size))) {
  538. return NULL;
  539. }
  540. /* instantiate functions from import section */
  541. function = functions;
  542. import = module->import_functions;
  543. for (i = 0; i < module->import_function_count; i++, import++) {
  544. function->is_import_func = true;
  545. #if WASM_ENABLE_MULTI_MODULE != 0
  546. if (import->u.function.import_module) {
  547. function->import_module_inst =
  548. get_sub_module_inst(module_inst,
  549. import->u.function.import_module);
  550. if (function->import_module_inst) {
  551. function->import_func_inst =
  552. wasm_lookup_function(function->import_module_inst,
  553. import->u.function.field_name, NULL);
  554. }
  555. }
  556. #endif /* WASM_ENABLE_MULTI_MODULE */
  557. function->u.func_import = &import->u.function;
  558. function->param_cell_num =
  559. import->u.function.func_type->param_cell_num;
  560. function->ret_cell_num = import->u.function.func_type->ret_cell_num;
  561. function->param_count =
  562. (uint16)function->u.func_import->func_type->param_count;
  563. function->param_types = function->u.func_import->func_type->types;
  564. function->local_cell_num = 0;
  565. function->local_count = 0;
  566. function->local_types = NULL;
  567. function++;
  568. }
  569. /* instantiate functions from function section */
  570. for (i = 0; i < module->function_count; i++) {
  571. function->is_import_func = false;
  572. function->u.func = module->functions[i];
  573. function->param_cell_num = function->u.func->param_cell_num;
  574. function->ret_cell_num = function->u.func->ret_cell_num;
  575. function->local_cell_num = function->u.func->local_cell_num;
  576. function->param_count = (uint16)function->u.func->func_type->param_count;
  577. function->local_count = (uint16)function->u.func->local_count;
  578. function->param_types = function->u.func->func_type->types;
  579. function->local_types = function->u.func->local_types;
  580. function->local_offsets = function->u.func->local_offsets;
  581. #if WASM_ENABLE_FAST_INTERP != 0
  582. function->const_cell_num = function->u.func->const_cell_num;
  583. #endif
  584. function++;
  585. }
  586. bh_assert((uint32)(function - functions) == function_count);
  587. (void)module_inst;
  588. return functions;
  589. }
  590. /**
  591. * Destroy global instances.
  592. */
  593. static void
  594. globals_deinstantiate(WASMGlobalInstance *globals)
  595. {
  596. if (globals)
  597. wasm_runtime_free(globals);
  598. }
  599. static bool
  600. check_global_init_expr(const WASMModule *module, uint32 global_index,
  601. char *error_buf, uint32 error_buf_size)
  602. {
  603. if (global_index >= module->import_global_count + module->global_count) {
  604. set_error_buf_v(error_buf, error_buf_size,
  605. "unknown global %d", global_index);
  606. return false;
  607. }
  608. /**
  609. * Currently, constant expressions occurring as initializers of
  610. * globals are further constrained in that contained global.get
  611. * instructions are only allowed to refer to imported globals.
  612. *
  613. * And initializer expression cannot reference a mutable global.
  614. */
  615. if (global_index >= module->import_global_count
  616. || (module->import_globals + global_index)->u.global.is_mutable) {
  617. set_error_buf(error_buf, error_buf_size,
  618. "constant expression required");
  619. return false;
  620. }
  621. return true;
  622. }
  623. /**
  624. * Instantiate globals in a module.
  625. */
  626. static WASMGlobalInstance *
  627. globals_instantiate(const WASMModule *module,
  628. WASMModuleInstance *module_inst,
  629. uint32 *p_global_data_size, char *error_buf,
  630. uint32 error_buf_size)
  631. {
  632. WASMImport *import;
  633. uint32 global_data_offset = 0;
  634. uint32 i, global_count =
  635. module->import_global_count + module->global_count;
  636. uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count;
  637. WASMGlobalInstance *globals, *global;
  638. if (!(globals = runtime_malloc(total_size,
  639. error_buf, error_buf_size))) {
  640. return NULL;
  641. }
  642. /* instantiate globals from import section */
  643. global = globals;
  644. import = module->import_globals;
  645. for (i = 0; i < module->import_global_count; i++, import++) {
  646. WASMGlobalImport *global_import = &import->u.global;
  647. global->type = global_import->type;
  648. global->is_mutable = global_import->is_mutable;
  649. #if WASM_ENABLE_MULTI_MODULE != 0
  650. if (global_import->import_module) {
  651. if (!(global->import_module_inst = get_sub_module_inst(
  652. module_inst, global_import->import_module))) {
  653. set_error_buf(error_buf, error_buf_size, "unknown global");
  654. return NULL;
  655. }
  656. if (!(global->import_global_inst = wasm_lookup_global(
  657. global->import_module_inst, global_import->field_name))) {
  658. set_error_buf(error_buf, error_buf_size, "unknown global");
  659. return NULL;
  660. }
  661. /* The linked global instance has been initialized, we
  662. just need to copy the value. */
  663. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  664. &(global_import->import_global_linked->init_expr),
  665. sizeof(WASMValue));
  666. }
  667. else
  668. #endif
  669. {
  670. /* native globals share their initial_values in one module */
  671. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  672. &(global_import->global_data_linked),
  673. sizeof(WASMValue));
  674. }
  675. global->data_offset = global_data_offset;
  676. global_data_offset += wasm_value_type_size(global->type);
  677. global++;
  678. }
  679. /* instantiate globals from global section */
  680. for (i = 0; i < module->global_count; i++) {
  681. InitializerExpression *init_expr = &(module->globals[i].init_expr);
  682. global->type = module->globals[i].type;
  683. global->is_mutable = module->globals[i].is_mutable;
  684. global->data_offset = global_data_offset;
  685. global_data_offset += wasm_value_type_size(global->type);
  686. if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  687. if (!check_global_init_expr(module, init_expr->u.global_index,
  688. error_buf, error_buf_size)) {
  689. return NULL;
  690. }
  691. bh_memcpy_s(
  692. &(global->initial_value), sizeof(WASMValue),
  693. &(globals[init_expr->u.global_index].initial_value),
  694. sizeof(globals[init_expr->u.global_index].initial_value));
  695. }
  696. #if WASM_ENABLE_REF_TYPES != 0
  697. else if (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST) {
  698. global->initial_value.u32 = (uint32)NULL_REF;
  699. }
  700. #endif
  701. else {
  702. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  703. &(init_expr->u), sizeof(init_expr->u));
  704. }
  705. global++;
  706. }
  707. bh_assert((uint32)(global - globals) == global_count);
  708. *p_global_data_size = global_data_offset;
  709. (void)module_inst;
  710. return globals;
  711. }
  712. /**
  713. * Return export function count in module export section.
  714. */
  715. static uint32
  716. get_export_count(const WASMModule *module, uint8 kind)
  717. {
  718. WASMExport *export = module->exports;
  719. uint32 count = 0, i;
  720. for (i = 0; i < module->export_count; i++, export++)
  721. if (export->kind == kind)
  722. count++;
  723. return count;
  724. }
  725. /**
  726. * Destroy export function instances.
  727. */
  728. static void
  729. export_functions_deinstantiate(WASMExportFuncInstance *functions)
  730. {
  731. if (functions)
  732. wasm_runtime_free(functions);
  733. }
  734. /**
  735. * Instantiate export functions in a module.
  736. */
  737. static WASMExportFuncInstance*
  738. export_functions_instantiate(const WASMModule *module,
  739. WASMModuleInstance *module_inst,
  740. uint32 export_func_count,
  741. char *error_buf, uint32 error_buf_size)
  742. {
  743. WASMExportFuncInstance *export_funcs, *export_func;
  744. WASMExport *export = module->exports;
  745. uint32 i;
  746. uint64 total_size = sizeof(WASMExportFuncInstance) * (uint64)export_func_count;
  747. if (!(export_func = export_funcs = runtime_malloc
  748. (total_size, error_buf, error_buf_size))) {
  749. return NULL;
  750. }
  751. for (i = 0; i < module->export_count; i++, export++)
  752. if (export->kind == EXPORT_KIND_FUNC) {
  753. export_func->name = export->name;
  754. export_func->function = &module_inst->functions[export->index];
  755. export_func++;
  756. }
  757. bh_assert((uint32)(export_func - export_funcs) == export_func_count);
  758. return export_funcs;
  759. }
  760. #if WASM_ENABLE_MULTI_MODULE != 0
  761. static void
  762. export_globals_deinstantiate(WASMExportGlobInstance *globals)
  763. {
  764. if (globals)
  765. wasm_runtime_free(globals);
  766. }
  767. static WASMExportGlobInstance *
  768. export_globals_instantiate(const WASMModule *module,
  769. WASMModuleInstance *module_inst,
  770. uint32 export_glob_count, char *error_buf,
  771. uint32 error_buf_size)
  772. {
  773. WASMExportGlobInstance *export_globals, *export_global;
  774. WASMExport *export = module->exports;
  775. uint32 i;
  776. uint64 total_size = sizeof(WASMExportGlobInstance) * (uint64)export_glob_count;
  777. if (!(export_global = export_globals = runtime_malloc
  778. (total_size, error_buf, error_buf_size))) {
  779. return NULL;
  780. }
  781. for (i = 0; i < module->export_count; i++, export++)
  782. if (export->kind == EXPORT_KIND_GLOBAL) {
  783. export_global->name = export->name;
  784. export_global->global = &module_inst->globals[export->index];
  785. export_global++;
  786. }
  787. bh_assert((uint32)(export_global - export_globals) == export_glob_count);
  788. return export_globals;
  789. }
  790. #endif
  791. static bool
  792. execute_post_inst_function(WASMModuleInstance *module_inst)
  793. {
  794. WASMFunctionInstance *post_inst_func = NULL;
  795. WASMType *post_inst_func_type;
  796. uint32 i;
  797. for (i = 0; i < module_inst->export_func_count; i++)
  798. if (!strcmp(module_inst->export_functions[i].name, "__post_instantiate")) {
  799. post_inst_func = module_inst->export_functions[i].function;
  800. break;
  801. }
  802. if (!post_inst_func)
  803. /* Not found */
  804. return true;
  805. post_inst_func_type = post_inst_func->u.func->func_type;
  806. if (post_inst_func_type->param_count != 0
  807. || post_inst_func_type->result_count != 0)
  808. /* Not a valid function type, ignore it */
  809. return true;
  810. return wasm_create_exec_env_and_call_function(module_inst, post_inst_func,
  811. 0, NULL);
  812. }
  813. #if WASM_ENABLE_BULK_MEMORY != 0
  814. static bool
  815. execute_memory_init_function(WASMModuleInstance *module_inst)
  816. {
  817. WASMFunctionInstance *memory_init_func = NULL;
  818. WASMType *memory_init_func_type;
  819. uint32 i;
  820. for (i = 0; i < module_inst->export_func_count; i++)
  821. if (!strcmp(module_inst->export_functions[i].name, "__wasm_call_ctors")) {
  822. memory_init_func = module_inst->export_functions[i].function;
  823. break;
  824. }
  825. if (!memory_init_func)
  826. /* Not found */
  827. return true;
  828. memory_init_func_type = memory_init_func->u.func->func_type;
  829. if (memory_init_func_type->param_count != 0
  830. || memory_init_func_type->result_count != 0)
  831. /* Not a valid function type, ignore it */
  832. return true;
  833. return wasm_create_exec_env_and_call_function(module_inst,
  834. memory_init_func,
  835. 0, NULL);
  836. }
  837. #endif
  838. static bool
  839. execute_start_function(WASMModuleInstance *module_inst)
  840. {
  841. WASMFunctionInstance *func = module_inst->start_function;
  842. if (!func)
  843. return true;
  844. bh_assert(!func->is_import_func && func->param_cell_num == 0
  845. && func->ret_cell_num == 0);
  846. return wasm_create_exec_env_and_call_function(module_inst, func, 0, NULL);
  847. }
  848. static bool
  849. execute_malloc_function(WASMModuleInstance *module_inst,
  850. WASMFunctionInstance *malloc_func,
  851. WASMFunctionInstance *retain_func,
  852. uint32 size, uint32 *p_result)
  853. {
  854. uint32 argv[2], argc;
  855. bool ret;
  856. argv[0] = size;
  857. argc = 1;
  858. /* if __retain is exported, then this module is compiled by
  859. assemblyscript, the memory should be managed by as's runtime,
  860. in this case we need to call the retain function after malloc
  861. the memory */
  862. if (retain_func) {
  863. /* the malloc functino from assemblyscript is:
  864. function __new(size: usize, id: u32)
  865. id = 0 means this is an ArrayBuffer object */
  866. argv[1] = 0;
  867. argc = 2;
  868. }
  869. ret = wasm_create_exec_env_and_call_function
  870. (module_inst, malloc_func, argc, argv);
  871. if (retain_func && ret) {
  872. ret = wasm_create_exec_env_and_call_function
  873. (module_inst, retain_func, 1, argv);
  874. }
  875. if (ret)
  876. *p_result = argv[0];
  877. return ret;
  878. }
  879. static bool
  880. execute_free_function(WASMModuleInstance *module_inst,
  881. WASMFunctionInstance *free_func,
  882. uint32 offset)
  883. {
  884. uint32 argv[2];
  885. argv[0] = offset;
  886. return wasm_create_exec_env_and_call_function
  887. (module_inst, free_func, 1, argv);
  888. }
  889. #if WASM_ENABLE_MULTI_MODULE != 0
  890. static bool
  891. sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
  892. uint32 stack_size, uint32 heap_size, char *error_buf,
  893. uint32 error_buf_size)
  894. {
  895. bh_list *sub_module_inst_list = module_inst->sub_module_inst_list;
  896. WASMRegisteredModule *sub_module_list_node =
  897. bh_list_first_elem(module->import_module_list);
  898. while (sub_module_list_node) {
  899. WASMSubModInstNode *sub_module_inst_list_node;
  900. WASMModule *sub_module = (WASMModule*)sub_module_list_node->module;
  901. WASMModuleInstance *sub_module_inst =
  902. wasm_instantiate(sub_module, false, stack_size, heap_size,
  903. error_buf, error_buf_size);
  904. if (!sub_module_inst) {
  905. LOG_DEBUG("instantiate %s failed",
  906. sub_module_list_node->module_name);
  907. return false;
  908. }
  909. sub_module_inst_list_node = runtime_malloc
  910. (sizeof(WASMSubModInstNode), error_buf, error_buf_size);
  911. if (!sub_module_inst_list_node) {
  912. LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ:%d",
  913. sizeof(WASMSubModInstNode));
  914. wasm_deinstantiate(sub_module_inst, false);
  915. return false;
  916. }
  917. sub_module_inst_list_node->module_inst = sub_module_inst;
  918. sub_module_inst_list_node->module_name =
  919. sub_module_list_node->module_name;
  920. bh_list_status ret =
  921. bh_list_insert(sub_module_inst_list, sub_module_inst_list_node);
  922. bh_assert(BH_LIST_SUCCESS == ret);
  923. (void)ret;
  924. sub_module_list_node = bh_list_elem_next(sub_module_list_node);
  925. }
  926. return true;
  927. }
  928. static void
  929. sub_module_deinstantiate(WASMModuleInstance *module_inst)
  930. {
  931. bh_list *list = module_inst->sub_module_inst_list;
  932. WASMSubModInstNode *node = bh_list_first_elem(list);
  933. while (node) {
  934. WASMSubModInstNode *next_node = bh_list_elem_next(node);
  935. bh_list_remove(list, node);
  936. wasm_deinstantiate(node->module_inst, false);
  937. wasm_runtime_free(node);
  938. node = next_node;
  939. }
  940. }
  941. #endif
  942. static bool
  943. check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
  944. uint32 error_buf_size)
  945. {
  946. WASMModule *module = module_inst->module;
  947. uint32 i;
  948. for (i = 0; i < module->import_function_count; i++) {
  949. WASMFunctionImport *func =
  950. &((module->import_functions + i)->u.function);
  951. if (!func->func_ptr_linked
  952. #if WASM_ENABLE_MULTI_MODULE != 0
  953. && !func->import_func_linked
  954. #endif
  955. ) {
  956. #if WASM_ENABLE_SPEC_TEST != 0
  957. set_error_buf(error_buf, error_buf_size,
  958. "unknown import or incompatible import type");
  959. return false;
  960. #else
  961. #if WASM_ENABLE_WAMR_COMPILER == 0
  962. LOG_WARNING("warning: failed to link import function (%s, %s)",
  963. func->module_name, func->field_name);
  964. #else
  965. /* do nothing to avoid confused message */
  966. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  967. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  968. }
  969. }
  970. for (i = 0; i < module->import_global_count; i++) {
  971. WASMGlobalImport *global = &((module->import_globals + i)->u.global);
  972. if (!global->is_linked) {
  973. #if WASM_ENABLE_SPEC_TEST != 0
  974. set_error_buf(error_buf, error_buf_size,
  975. "unknown import or incompatible import type");
  976. return false;
  977. #else
  978. #if WASM_ENABLE_WAMR_COMPILER == 0
  979. LOG_DEBUG("warning: failed to link import global (%s, %s)",
  980. global->module_name, global->field_name);
  981. #else
  982. /* do nothing to avoid confused message */
  983. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  984. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  985. }
  986. }
  987. return true;
  988. }
  989. /**
  990. * Instantiate module
  991. */
  992. WASMModuleInstance*
  993. wasm_instantiate(WASMModule *module, bool is_sub_inst,
  994. uint32 stack_size, uint32 heap_size,
  995. char *error_buf, uint32 error_buf_size)
  996. {
  997. WASMModuleInstance *module_inst;
  998. WASMGlobalInstance *globals = NULL, *global;
  999. uint32 global_count, global_data_size = 0, i;
  1000. uint32 base_offset, length;
  1001. uint8 *global_data, *global_data_end;
  1002. #if WASM_ENABLE_MULTI_MODULE != 0
  1003. bool ret = false;
  1004. #endif
  1005. if (!module)
  1006. return NULL;
  1007. /* Check heap size */
  1008. heap_size = align_uint(heap_size, 8);
  1009. if (heap_size > APP_HEAP_SIZE_MAX)
  1010. heap_size = APP_HEAP_SIZE_MAX;
  1011. /* Allocate the memory */
  1012. if (!(module_inst = runtime_malloc(sizeof(WASMModuleInstance),
  1013. error_buf, error_buf_size))) {
  1014. return NULL;
  1015. }
  1016. module_inst->module = module;
  1017. #if WASM_ENABLE_MULTI_MODULE != 0
  1018. module_inst->sub_module_inst_list =
  1019. &module_inst->sub_module_inst_list_head;
  1020. ret = sub_module_instantiate(module, module_inst, stack_size, heap_size,
  1021. error_buf, error_buf_size);
  1022. if (!ret) {
  1023. LOG_DEBUG("build a sub module list failed");
  1024. goto fail;
  1025. }
  1026. #endif
  1027. /* Instantiate global firstly to get the mutable data size */
  1028. global_count = module->import_global_count + module->global_count;
  1029. if (global_count
  1030. && !(globals = globals_instantiate(module, module_inst,
  1031. &global_data_size,
  1032. error_buf, error_buf_size))) {
  1033. goto fail;
  1034. }
  1035. module_inst->global_count = global_count;
  1036. module_inst->globals = globals;
  1037. module_inst->memory_count =
  1038. module->import_memory_count + module->memory_count;
  1039. module_inst->table_count =
  1040. module->import_table_count + module->table_count;
  1041. module_inst->function_count =
  1042. module->import_function_count + module->function_count;
  1043. /* export */
  1044. module_inst->export_func_count = get_export_count(module, EXPORT_KIND_FUNC);
  1045. #if WASM_ENABLE_MULTI_MODULE != 0
  1046. module_inst->export_tab_count = get_export_count(module, EXPORT_KIND_TABLE);
  1047. module_inst->export_mem_count = get_export_count(module, EXPORT_KIND_MEMORY);
  1048. module_inst->export_glob_count = get_export_count(module, EXPORT_KIND_GLOBAL);
  1049. #endif
  1050. if (global_count > 0) {
  1051. if (!(module_inst->global_data = runtime_malloc
  1052. (global_data_size, error_buf, error_buf_size))) {
  1053. goto fail;
  1054. }
  1055. }
  1056. /* Instantiate memories/tables/functions */
  1057. if ((module_inst->memory_count > 0
  1058. && !(module_inst->memories =
  1059. memories_instantiate(module,
  1060. module_inst,
  1061. heap_size, error_buf, error_buf_size)))
  1062. || (module_inst->table_count > 0
  1063. && !(module_inst->tables =
  1064. tables_instantiate(module,
  1065. module_inst,
  1066. error_buf, error_buf_size)))
  1067. || (module_inst->function_count > 0
  1068. && !(module_inst->functions =
  1069. functions_instantiate(module,
  1070. module_inst,
  1071. error_buf, error_buf_size)))
  1072. || (module_inst->export_func_count > 0
  1073. && !(module_inst->export_functions = export_functions_instantiate(
  1074. module, module_inst, module_inst->export_func_count,
  1075. error_buf, error_buf_size)))
  1076. #if WASM_ENABLE_MULTI_MODULE != 0
  1077. || (module_inst->export_glob_count > 0
  1078. && !(module_inst->export_globals = export_globals_instantiate(
  1079. module, module_inst, module_inst->export_glob_count,
  1080. error_buf, error_buf_size)))
  1081. #endif
  1082. ) {
  1083. goto fail;
  1084. }
  1085. if (global_count > 0) {
  1086. /* Initialize the global data */
  1087. global_data = module_inst->global_data;
  1088. global_data_end = global_data + global_data_size;
  1089. global = globals;
  1090. for (i = 0; i < global_count; i++, global++) {
  1091. switch (global->type) {
  1092. case VALUE_TYPE_I32:
  1093. case VALUE_TYPE_F32:
  1094. #if WASM_ENABLE_REF_TYPES != 0
  1095. case VALUE_TYPE_FUNCREF:
  1096. case VALUE_TYPE_EXTERNREF:
  1097. #endif
  1098. *(int32*)global_data = global->initial_value.i32;
  1099. global_data += sizeof(int32);
  1100. break;
  1101. case VALUE_TYPE_I64:
  1102. case VALUE_TYPE_F64:
  1103. bh_memcpy_s(global_data, (uint32)(global_data_end - global_data),
  1104. &global->initial_value.i64, sizeof(int64));
  1105. global_data += sizeof(int64);
  1106. break;
  1107. default:
  1108. bh_assert(0);
  1109. }
  1110. }
  1111. bh_assert(global_data == global_data_end);
  1112. }
  1113. if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
  1114. goto fail;
  1115. }
  1116. /* Initialize the memory data with data segment section */
  1117. module_inst->default_memory =
  1118. module_inst->memory_count ? module_inst->memories[0] : NULL;
  1119. for (i = 0; i < module->data_seg_count; i++) {
  1120. WASMMemoryInstance *memory = NULL;
  1121. uint8 *memory_data = NULL;
  1122. uint32 memory_size = 0;
  1123. WASMDataSeg *data_seg = module->data_segments[i];
  1124. #if WASM_ENABLE_BULK_MEMORY != 0
  1125. if (data_seg->is_passive)
  1126. continue;
  1127. #endif
  1128. /* has check it in loader */
  1129. memory = module_inst->memories[data_seg->memory_index];
  1130. bh_assert(memory);
  1131. memory_data = memory->memory_data;
  1132. memory_size = memory->num_bytes_per_page * memory->cur_page_count;
  1133. bh_assert(memory_data || memory_size == 0);
  1134. bh_assert(data_seg->base_offset.init_expr_type
  1135. == INIT_EXPR_TYPE_I32_CONST
  1136. || data_seg->base_offset.init_expr_type
  1137. == INIT_EXPR_TYPE_GET_GLOBAL);
  1138. if (data_seg->base_offset.init_expr_type
  1139. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1140. if (!check_global_init_expr(module,
  1141. data_seg->base_offset.u.global_index,
  1142. error_buf, error_buf_size)) {
  1143. goto fail;
  1144. }
  1145. if (!globals
  1146. || globals[data_seg->base_offset.u.global_index].type
  1147. != VALUE_TYPE_I32) {
  1148. set_error_buf(error_buf, error_buf_size,
  1149. "data segment does not fit");
  1150. goto fail;
  1151. }
  1152. data_seg->base_offset.u.i32 =
  1153. globals[data_seg->base_offset.u.global_index]
  1154. .initial_value.i32;
  1155. }
  1156. /* check offset */
  1157. base_offset = (uint32)data_seg->base_offset.u.i32;
  1158. if (base_offset > memory_size) {
  1159. LOG_DEBUG("base_offset(%d) > memory_size(%d)", base_offset,
  1160. memory_size);
  1161. #if WASM_ENABLE_REF_TYPES != 0
  1162. set_error_buf(error_buf, error_buf_size,
  1163. "out of bounds memory access");
  1164. #else
  1165. set_error_buf(error_buf, error_buf_size,
  1166. "data segment does not fit");
  1167. #endif
  1168. goto fail;
  1169. }
  1170. /* check offset + length(could be zero) */
  1171. length = data_seg->data_length;
  1172. if (base_offset + length > memory_size) {
  1173. LOG_DEBUG("base_offset(%d) + length(%d) > memory_size(%d)",
  1174. base_offset, length, memory_size);
  1175. #if WASM_ENABLE_REF_TYPES != 0
  1176. set_error_buf(error_buf, error_buf_size,
  1177. "out of bounds memory access");
  1178. #else
  1179. set_error_buf(error_buf, error_buf_size,
  1180. "data segment does not fit");
  1181. #endif
  1182. goto fail;
  1183. }
  1184. if (memory_data) {
  1185. bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
  1186. data_seg->data, length);
  1187. }
  1188. }
  1189. /* Initialize the table data with table segment section */
  1190. module_inst->default_table =
  1191. module_inst->table_count ? module_inst->tables[0] : NULL;
  1192. /* in case there is no table */
  1193. for (i = 0; module_inst->table_count > 0 && i < module->table_seg_count;
  1194. i++) {
  1195. WASMTableSeg *table_seg = module->table_segments + i;
  1196. /* has check it in loader */
  1197. WASMTableInstance *table = module_inst->tables[table_seg->table_index];
  1198. bh_assert(table);
  1199. #if WASM_ENABLE_REF_TYPES != 0
  1200. if (table->elem_type != VALUE_TYPE_FUNCREF
  1201. && table->elem_type != VALUE_TYPE_EXTERNREF) {
  1202. set_error_buf(error_buf, error_buf_size,
  1203. "elements segment does not fit");
  1204. goto fail;
  1205. }
  1206. #endif
  1207. uint32 *table_data = (uint32 *)table->base_addr;
  1208. #if WASM_ENABLE_MULTI_MODULE != 0
  1209. table_data = table->table_inst_linked
  1210. ? (uint32 *)table->table_inst_linked->base_addr
  1211. : table_data;
  1212. #endif
  1213. bh_assert(table_data);
  1214. #if WASM_ENABLE_REF_TYPES != 0
  1215. if (!wasm_elem_is_active(table_seg->mode))
  1216. continue;
  1217. #endif
  1218. /* init vec(funcidx) or vec(expr) */
  1219. bh_assert(
  1220. table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_I32_CONST
  1221. || table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL
  1222. #if WASM_ENABLE_REF_TYPES != 0
  1223. || table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_FUNCREF_CONST
  1224. || table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST
  1225. #endif
  1226. );
  1227. if (table_seg->base_offset.init_expr_type
  1228. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1229. if (!check_global_init_expr(module,
  1230. table_seg->base_offset.u.global_index,
  1231. error_buf, error_buf_size)) {
  1232. goto fail;
  1233. }
  1234. if (!globals
  1235. || globals[table_seg->base_offset.u.global_index].type
  1236. != VALUE_TYPE_I32) {
  1237. set_error_buf(error_buf, error_buf_size,
  1238. "elements segment does not fit");
  1239. goto fail;
  1240. }
  1241. table_seg->base_offset.u.i32 =
  1242. globals[table_seg->base_offset.u.global_index].initial_value.i32;
  1243. }
  1244. /* check offset since length might negative */
  1245. if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
  1246. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  1247. table_seg->base_offset.u.i32, table->cur_size);
  1248. #if WASM_ENABLE_REF_TYPES != 0
  1249. set_error_buf(error_buf, error_buf_size,
  1250. "out of bounds table access");
  1251. #else
  1252. set_error_buf(error_buf, error_buf_size,
  1253. "elements segment does not fit");
  1254. #endif
  1255. goto fail;
  1256. }
  1257. /* check offset + length(could be zero) */
  1258. length = table_seg->function_count;
  1259. if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
  1260. LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
  1261. table_seg->base_offset.u.i32, length, table->cur_size);
  1262. #if WASM_ENABLE_REF_TYPES != 0
  1263. set_error_buf(error_buf, error_buf_size,
  1264. "out of bounds table access");
  1265. #else
  1266. set_error_buf(error_buf, error_buf_size,
  1267. "elements segment does not fit");
  1268. #endif
  1269. goto fail;
  1270. }
  1271. /**
  1272. * Check function index in the current module inst for now.
  1273. * will check the linked table inst owner in future.
  1274. * so loader check is enough
  1275. */
  1276. bh_memcpy_s(
  1277. table_data + table_seg->base_offset.u.i32,
  1278. (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32)
  1279. * sizeof(uint32)),
  1280. table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
  1281. }
  1282. /* module instance type */
  1283. module_inst->module_type = Wasm_Module_Bytecode;
  1284. /* Initialize the thread related data */
  1285. if (stack_size == 0)
  1286. stack_size = DEFAULT_WASM_STACK_SIZE;
  1287. #if WASM_ENABLE_SPEC_TEST != 0
  1288. if (stack_size < 48 *1024)
  1289. stack_size = 48 * 1024;
  1290. #endif
  1291. module_inst->default_wasm_stack_size = stack_size;
  1292. if (module->malloc_function != (uint32)-1) {
  1293. module_inst->malloc_function =
  1294. &module_inst->functions[module->malloc_function];
  1295. }
  1296. if (module->free_function != (uint32)-1) {
  1297. module_inst->free_function =
  1298. &module_inst->functions[module->free_function];
  1299. }
  1300. if (module->retain_function != (uint32)-1) {
  1301. module_inst->retain_function =
  1302. &module_inst->functions[module->retain_function];
  1303. }
  1304. #if WASM_ENABLE_LIBC_WASI != 0
  1305. /* The sub-instance will get the wasi_ctx from main-instance */
  1306. if (!is_sub_inst) {
  1307. if (!wasm_runtime_init_wasi((WASMModuleInstanceCommon*)module_inst,
  1308. module->wasi_args.dir_list,
  1309. module->wasi_args.dir_count,
  1310. module->wasi_args.map_dir_list,
  1311. module->wasi_args.map_dir_count,
  1312. module->wasi_args.env,
  1313. module->wasi_args.env_count,
  1314. module->wasi_args.argv,
  1315. module->wasi_args.argc,
  1316. error_buf, error_buf_size)) {
  1317. goto fail;
  1318. }
  1319. }
  1320. #endif
  1321. if (module->start_function != (uint32)-1) {
  1322. /* TODO: fix start function can be import function issue */
  1323. if (module->start_function >= module->import_function_count)
  1324. module_inst->start_function =
  1325. &module_inst->functions[module->start_function];
  1326. }
  1327. /* Execute __post_instantiate function */
  1328. if (!execute_post_inst_function(module_inst)
  1329. || !execute_start_function(module_inst)) {
  1330. set_error_buf(error_buf, error_buf_size,
  1331. module_inst->cur_exception);
  1332. goto fail;
  1333. }
  1334. #if WASM_ENABLE_BULK_MEMORY != 0
  1335. #if WASM_ENABLE_LIBC_WASI != 0
  1336. if (!module->is_wasi_module) {
  1337. #endif
  1338. /* Only execute the memory init function for main instance because
  1339. the data segments will be dropped once initialized.
  1340. */
  1341. if (!is_sub_inst) {
  1342. if (!execute_memory_init_function(module_inst)) {
  1343. set_error_buf(error_buf, error_buf_size,
  1344. module_inst->cur_exception);
  1345. goto fail;
  1346. }
  1347. }
  1348. #if WASM_ENABLE_LIBC_WASI != 0
  1349. }
  1350. #endif
  1351. #endif
  1352. #if WASM_ENABLE_MEMORY_TRACING != 0
  1353. wasm_runtime_dump_module_inst_mem_consumption
  1354. ((WASMModuleInstanceCommon *)module_inst);
  1355. #endif
  1356. (void)global_data_end;
  1357. return module_inst;
  1358. fail:
  1359. wasm_deinstantiate(module_inst, false);
  1360. return NULL;
  1361. }
  1362. void
  1363. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
  1364. {
  1365. if (!module_inst)
  1366. return;
  1367. #if WASM_ENABLE_MULTI_MODULE != 0
  1368. sub_module_deinstantiate(module_inst);
  1369. #endif
  1370. #if WASM_ENABLE_LIBC_WASI != 0
  1371. /* Destroy wasi resource before freeing app heap, since some fields of
  1372. wasi contex are allocated from app heap, and if app heap is freed,
  1373. these fields will be set to NULL, we cannot free their internal data
  1374. which may allocated from global heap. */
  1375. /* Only destroy wasi ctx in the main module instance */
  1376. if (!is_sub_inst)
  1377. wasm_runtime_destroy_wasi((WASMModuleInstanceCommon*)module_inst);
  1378. #endif
  1379. if (module_inst->memory_count > 0)
  1380. memories_deinstantiate(
  1381. module_inst,
  1382. module_inst->memories, module_inst->memory_count);
  1383. tables_deinstantiate(module_inst->tables, module_inst->table_count);
  1384. functions_deinstantiate(module_inst->functions, module_inst->function_count);
  1385. globals_deinstantiate(module_inst->globals);
  1386. export_functions_deinstantiate(module_inst->export_functions);
  1387. #if WASM_ENABLE_MULTI_MODULE != 0
  1388. export_globals_deinstantiate(module_inst->export_globals);
  1389. #endif
  1390. if (module_inst->global_data)
  1391. wasm_runtime_free(module_inst->global_data);
  1392. #if WASM_ENABLE_REF_TYPES != 0
  1393. wasm_externref_cleanup((WASMModuleInstanceCommon*)module_inst);
  1394. #endif
  1395. wasm_runtime_free(module_inst);
  1396. }
  1397. WASMFunctionInstance*
  1398. wasm_lookup_function(const WASMModuleInstance *module_inst,
  1399. const char *name, const char *signature)
  1400. {
  1401. uint32 i;
  1402. for (i = 0; i < module_inst->export_func_count; i++)
  1403. if (!strcmp(module_inst->export_functions[i].name, name))
  1404. return module_inst->export_functions[i].function;
  1405. (void)signature;
  1406. return NULL;
  1407. }
  1408. #if WASM_ENABLE_MULTI_MODULE != 0
  1409. WASMGlobalInstance *
  1410. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name)
  1411. {
  1412. uint32 i;
  1413. for (i = 0; i < module_inst->export_glob_count; i++)
  1414. if (!strcmp(module_inst->export_globals[i].name, name))
  1415. return module_inst->export_globals[i].global;
  1416. return NULL;
  1417. }
  1418. WASMMemoryInstance *
  1419. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name)
  1420. {
  1421. /**
  1422. * using a strong assumption that one module instance only has
  1423. * one memory instance
  1424. */
  1425. (void)module_inst->export_memories;
  1426. return module_inst->memories[0];
  1427. }
  1428. WASMTableInstance *
  1429. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
  1430. {
  1431. /**
  1432. * using a strong assumption that one module instance only has
  1433. * one table instance
  1434. */
  1435. (void)module_inst->export_tables;
  1436. return module_inst->tables[0];
  1437. }
  1438. #endif
  1439. static bool
  1440. clear_wasi_proc_exit_exception(WASMModuleInstance *module_inst)
  1441. {
  1442. #if WASM_ENABLE_LIBC_WASI != 0
  1443. const char *exception = wasm_get_exception(module_inst);
  1444. if (exception && !strcmp(exception, "Exception: wasi proc exit")) {
  1445. /* The "wasi proc exit" exception is thrown by native lib to
  1446. let wasm app exit, which is a normal behavior, we clear
  1447. the exception here. */
  1448. wasm_set_exception(module_inst, NULL);
  1449. return true;
  1450. }
  1451. return false;
  1452. #else
  1453. return false;
  1454. #endif
  1455. }
  1456. bool
  1457. wasm_call_function(WASMExecEnv *exec_env,
  1458. WASMFunctionInstance *function,
  1459. unsigned argc, uint32 argv[])
  1460. {
  1461. WASMModuleInstance *module_inst = (WASMModuleInstance*)exec_env->module_inst;
  1462. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  1463. (void)clear_wasi_proc_exit_exception(module_inst);
  1464. return !wasm_get_exception(module_inst) ? true : false;
  1465. }
  1466. bool
  1467. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  1468. WASMFunctionInstance *func,
  1469. unsigned argc, uint32 argv[])
  1470. {
  1471. WASMExecEnv *exec_env;
  1472. bool ret;
  1473. #if WASM_ENABLE_THREAD_MGR != 0
  1474. WASMExecEnv *existing_exec_env = NULL;
  1475. if (!(existing_exec_env = exec_env =
  1476. wasm_clusters_search_exec_env(
  1477. (WASMModuleInstanceCommon*)module_inst))) {
  1478. #endif
  1479. if (!(exec_env = wasm_exec_env_create(
  1480. (WASMModuleInstanceCommon*)module_inst,
  1481. module_inst->default_wasm_stack_size))) {
  1482. wasm_set_exception(module_inst, "allocate memory failed");
  1483. return false;
  1484. }
  1485. /* set thread handle and stack boundary */
  1486. wasm_exec_env_set_thread_info(exec_env);
  1487. #if WASM_ENABLE_THREAD_MGR != 0
  1488. }
  1489. #endif
  1490. #if WASM_ENABLE_REF_TYPES != 0
  1491. wasm_runtime_prepare_call_function(exec_env, func);
  1492. #endif
  1493. ret = wasm_call_function(exec_env, func, argc, argv);
  1494. #if WASM_ENABLE_REF_TYPES != 0
  1495. wasm_runtime_finalize_call_function(exec_env, func, ret, argv);
  1496. #endif
  1497. #if WASM_ENABLE_THREAD_MGR != 0
  1498. /* don't destroy the exec_env if it's searched from the cluster */
  1499. if (!existing_exec_env)
  1500. #endif
  1501. wasm_exec_env_destroy(exec_env);
  1502. return ret;
  1503. }
  1504. void
  1505. wasm_set_exception(WASMModuleInstance *module_inst,
  1506. const char *exception)
  1507. {
  1508. if (exception)
  1509. snprintf(module_inst->cur_exception,
  1510. sizeof(module_inst->cur_exception),
  1511. "Exception: %s", exception);
  1512. else
  1513. module_inst->cur_exception[0] = '\0';
  1514. }
  1515. const char*
  1516. wasm_get_exception(WASMModuleInstance *module_inst)
  1517. {
  1518. if (module_inst->cur_exception[0] == '\0')
  1519. return NULL;
  1520. else
  1521. return module_inst->cur_exception;
  1522. }
  1523. #if WASM_ENABLE_PERF_PROFILING != 0
  1524. void
  1525. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
  1526. {
  1527. WASMExportFuncInstance *export_func;
  1528. WASMFunctionInstance *func_inst;
  1529. char *func_name;
  1530. uint32 i, j;
  1531. os_printf("Performance profiler data:\n");
  1532. for (i = 0; i < module_inst->function_count; i++) {
  1533. func_inst = module_inst->functions + i;
  1534. if (func_inst->is_import_func) {
  1535. func_name = func_inst->u.func_import->field_name;
  1536. }
  1537. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  1538. else if (func_inst->u.func->field_name) {
  1539. func_name = func_inst->u.func->field_name;
  1540. }
  1541. #endif
  1542. else {
  1543. func_name = NULL;
  1544. for (j = 0; j < module_inst->export_func_count; j++) {
  1545. export_func = module_inst->export_functions + j;
  1546. if (export_func->function == func_inst) {
  1547. func_name = export_func->name;
  1548. break;
  1549. }
  1550. }
  1551. }
  1552. if (func_name)
  1553. os_printf(" func %s, execution time: %.3f ms, execution count: %d times\n",
  1554. func_name, module_inst->functions[i].total_exec_time / 1000.0f,
  1555. module_inst->functions[i].total_exec_cnt);
  1556. else
  1557. os_printf(" func %d, execution time: %.3f ms, execution count: %d times\n",
  1558. i, module_inst->functions[i].total_exec_time / 1000.0f,
  1559. module_inst->functions[i].total_exec_cnt);
  1560. }
  1561. }
  1562. #endif
  1563. uint32
  1564. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  1565. void **p_native_addr)
  1566. {
  1567. WASMMemoryInstance *memory = module_inst->default_memory;
  1568. uint8 *addr = NULL;
  1569. uint32 offset = 0;
  1570. if (!memory) {
  1571. wasm_set_exception(module_inst, "uninitialized memory");
  1572. return 0;
  1573. }
  1574. if (memory->heap_handle) {
  1575. addr = mem_allocator_malloc(memory->heap_handle, size);
  1576. }
  1577. else if (module_inst->malloc_function
  1578. && module_inst->free_function) {
  1579. if (!execute_malloc_function(module_inst,
  1580. module_inst->malloc_function,
  1581. module_inst->retain_function,
  1582. size, &offset)) {
  1583. return 0;
  1584. }
  1585. /* If we use app's malloc function,
  1586. the default memory may be changed while memory growing */
  1587. memory = module_inst->default_memory;
  1588. addr = offset ? memory->memory_data + offset : NULL;
  1589. }
  1590. if (!addr) {
  1591. if (memory->heap_handle
  1592. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  1593. LOG_ERROR("Error: app heap is corrupted, if the wasm file "
  1594. "is compiled by wasi-sdk-12.0 or larger version, "
  1595. "please add -Wl,--export=malloc -Wl,--export=free "
  1596. " to export malloc and free functions.");
  1597. wasm_set_exception(module_inst, "app heap corrupted");
  1598. }
  1599. else {
  1600. wasm_set_exception(module_inst, "out of memory");
  1601. }
  1602. return 0;
  1603. }
  1604. if (p_native_addr)
  1605. *p_native_addr = addr;
  1606. return (uint32)(addr - memory->memory_data);
  1607. }
  1608. uint32
  1609. wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
  1610. void **p_native_addr)
  1611. {
  1612. WASMMemoryInstance *memory = module_inst->default_memory;
  1613. uint8 *addr = NULL;
  1614. if (!memory) {
  1615. wasm_set_exception(module_inst, "uninitialized memory");
  1616. return 0;
  1617. }
  1618. if (memory->heap_handle) {
  1619. addr = mem_allocator_realloc(memory->heap_handle,
  1620. memory->memory_data + ptr, size);
  1621. }
  1622. /* Only support realloc in WAMR's app heap */
  1623. if (!addr) {
  1624. if (memory->heap_handle
  1625. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  1626. wasm_set_exception(module_inst, "app heap corrupted");
  1627. }
  1628. else {
  1629. wasm_set_exception(module_inst, "out of memory");
  1630. }
  1631. return 0;
  1632. }
  1633. if (p_native_addr)
  1634. *p_native_addr = addr;
  1635. return (uint32)(addr - memory->memory_data);
  1636. }
  1637. void
  1638. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
  1639. {
  1640. if (ptr) {
  1641. WASMMemoryInstance *memory = module_inst->default_memory;
  1642. uint8* addr;
  1643. if (!memory) {
  1644. return;
  1645. }
  1646. addr = memory->memory_data + ptr;
  1647. if (memory->heap_handle
  1648. && memory->heap_data <= addr
  1649. && addr < memory->heap_data_end) {
  1650. mem_allocator_free(memory->heap_handle, addr);
  1651. }
  1652. else if (module_inst->malloc_function
  1653. && module_inst->free_function
  1654. && memory->memory_data <= addr
  1655. && addr < memory->memory_data_end) {
  1656. execute_free_function(module_inst,
  1657. module_inst->free_function,
  1658. ptr);
  1659. }
  1660. }
  1661. }
  1662. uint32
  1663. wasm_module_dup_data(WASMModuleInstance *module_inst,
  1664. const char *src, uint32 size)
  1665. {
  1666. char *buffer;
  1667. uint32 buffer_offset = wasm_module_malloc(module_inst, size,
  1668. (void**)&buffer);
  1669. if (buffer_offset != 0) {
  1670. buffer = wasm_addr_app_to_native(module_inst, buffer_offset);
  1671. bh_memcpy_s(buffer, size, src, size);
  1672. }
  1673. return buffer_offset;
  1674. }
  1675. bool
  1676. wasm_validate_app_addr(WASMModuleInstance *module_inst,
  1677. uint32 app_offset, uint32 size)
  1678. {
  1679. WASMMemoryInstance *memory = module_inst->default_memory;
  1680. uint32 memory_data_size;
  1681. if (!memory) {
  1682. goto fail;
  1683. }
  1684. memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
  1685. /* integer overflow check */
  1686. if (app_offset + size < app_offset) {
  1687. goto fail;
  1688. }
  1689. if (app_offset + size <= memory_data_size) {
  1690. return true;
  1691. }
  1692. fail:
  1693. wasm_set_exception(module_inst, "out of bounds memory access");
  1694. return false;
  1695. }
  1696. bool
  1697. wasm_validate_native_addr(WASMModuleInstance *module_inst,
  1698. void *native_ptr, uint32 size)
  1699. {
  1700. WASMMemoryInstance *memory = module_inst->default_memory;
  1701. uint8 *addr = (uint8 *)native_ptr;
  1702. if (!memory) {
  1703. goto fail;
  1704. }
  1705. /* integer overflow check */
  1706. if (addr + size < addr) {
  1707. goto fail;
  1708. }
  1709. if (memory->memory_data <= addr
  1710. && addr + size <= memory->memory_data_end) {
  1711. return true;
  1712. }
  1713. fail:
  1714. wasm_set_exception(module_inst, "out of bounds memory access");
  1715. return false;
  1716. }
  1717. void *
  1718. wasm_addr_app_to_native(WASMModuleInstance *module_inst,
  1719. uint32 app_offset)
  1720. {
  1721. WASMMemoryInstance *memory = module_inst->default_memory;
  1722. uint8 *addr;
  1723. if (!memory)
  1724. return NULL;
  1725. addr = memory->memory_data + app_offset;
  1726. if (memory->memory_data <= addr
  1727. && addr < memory->memory_data_end)
  1728. return addr;
  1729. return NULL;
  1730. }
  1731. uint32
  1732. wasm_addr_native_to_app(WASMModuleInstance *module_inst,
  1733. void *native_ptr)
  1734. {
  1735. WASMMemoryInstance *memory = module_inst->default_memory;
  1736. uint8 *addr = (uint8 *)native_ptr;
  1737. if (!memory)
  1738. return 0;
  1739. if (memory->memory_data <= addr
  1740. && addr < memory->memory_data_end)
  1741. return (uint32)(addr - memory->memory_data);
  1742. return 0;
  1743. }
  1744. bool
  1745. wasm_get_app_addr_range(WASMModuleInstance *module_inst,
  1746. uint32 app_offset,
  1747. uint32 *p_app_start_offset,
  1748. uint32 *p_app_end_offset)
  1749. {
  1750. WASMMemoryInstance *memory = module_inst->default_memory;
  1751. uint32 memory_data_size;
  1752. if (!memory)
  1753. return false;
  1754. memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
  1755. if (app_offset < memory_data_size) {
  1756. if (p_app_start_offset)
  1757. *p_app_start_offset = 0;
  1758. if (p_app_end_offset)
  1759. *p_app_end_offset = memory_data_size;
  1760. return true;
  1761. }
  1762. return false;
  1763. }
  1764. bool
  1765. wasm_get_native_addr_range(WASMModuleInstance *module_inst,
  1766. uint8 *native_ptr,
  1767. uint8 **p_native_start_addr,
  1768. uint8 **p_native_end_addr)
  1769. {
  1770. WASMMemoryInstance *memory = module_inst->default_memory;
  1771. uint8 *addr = (uint8 *)native_ptr;
  1772. if (!memory)
  1773. return false;
  1774. if (memory->memory_data <= addr
  1775. && addr < memory->memory_data_end) {
  1776. if (p_native_start_addr)
  1777. *p_native_start_addr = memory->memory_data;
  1778. if (p_native_end_addr)
  1779. *p_native_end_addr = memory->memory_data_end;
  1780. return true;
  1781. }
  1782. return false;
  1783. }
  1784. bool
  1785. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  1786. {
  1787. WASMMemoryInstance *memory = module->default_memory;
  1788. uint8 *new_memory_data, *memory_data, *heap_data_old;
  1789. uint32 heap_size, total_size_old, total_page_count;
  1790. uint64 total_size;
  1791. if (!memory)
  1792. return false;
  1793. memory_data = memory->memory_data;
  1794. heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
  1795. total_size_old = (uint32)(memory->memory_data_end - memory_data);
  1796. total_page_count = inc_page_count + memory->cur_page_count;
  1797. total_size = memory->num_bytes_per_page * (uint64)total_page_count;
  1798. heap_data_old = memory->heap_data;
  1799. if (inc_page_count <= 0)
  1800. /* No need to enlarge memory */
  1801. return true;
  1802. if (total_page_count < memory->cur_page_count /* integer overflow */
  1803. || total_page_count > memory->max_page_count) {
  1804. return false;
  1805. }
  1806. if (total_size >= UINT32_MAX) {
  1807. return false;
  1808. }
  1809. #if WASM_ENABLE_SHARED_MEMORY != 0
  1810. if (memory->is_shared) {
  1811. /* For shared memory, we have reserved the maximum spaces during
  1812. instantiate, only change the cur_page_count here */
  1813. memory->cur_page_count = total_page_count;
  1814. return true;
  1815. }
  1816. #endif
  1817. if (!(new_memory_data = wasm_runtime_realloc(memory_data, (uint32)total_size))) {
  1818. if (!(new_memory_data = wasm_runtime_malloc((uint32)total_size))) {
  1819. return false;
  1820. }
  1821. if (memory_data) {
  1822. bh_memcpy_s(new_memory_data, (uint32)total_size,
  1823. memory_data, total_size_old);
  1824. wasm_runtime_free(memory_data);
  1825. }
  1826. }
  1827. memset(new_memory_data + total_size_old,
  1828. 0, (uint32)total_size - total_size_old);
  1829. if (heap_size > 0) {
  1830. if (mem_allocator_migrate(memory->heap_handle,
  1831. (char *)heap_data_old
  1832. + (new_memory_data - memory_data),
  1833. heap_size) != 0) {
  1834. return false;
  1835. }
  1836. }
  1837. memory->memory_data = new_memory_data;
  1838. memory->cur_page_count = total_page_count;
  1839. memory->heap_data = heap_data_old + (new_memory_data - memory_data);
  1840. memory->heap_data_end = memory->heap_data + heap_size;
  1841. memory->memory_data_end = memory->memory_data
  1842. + memory->num_bytes_per_page
  1843. * total_page_count;
  1844. return true;
  1845. }
  1846. #if WASM_ENABLE_REF_TYPES != 0
  1847. bool
  1848. wasm_enlarge_table(WASMModuleInstance *module_inst,
  1849. uint32 table_idx, uint32 inc_entries, uint32 init_val)
  1850. {
  1851. uint32 entry_count, *new_table_data_start, i;
  1852. WASMTableInstance *table_inst;
  1853. if (!inc_entries) {
  1854. return true;
  1855. }
  1856. bh_assert(table_idx < module_inst->table_count);
  1857. table_inst = wasm_get_table_inst(module_inst, table_idx);
  1858. if (!table_inst) {
  1859. return false;
  1860. }
  1861. entry_count = table_inst->cur_size + inc_entries;
  1862. /* prevent from integer overflow */
  1863. if (entry_count < table_inst->cur_size
  1864. || entry_count > table_inst->max_size) {
  1865. return false;
  1866. }
  1867. /* fill in */
  1868. new_table_data_start =
  1869. (uint32 *)((uint8 *)table_inst + offsetof(WASMTableInstance, base_addr))
  1870. + table_inst->cur_size;
  1871. for (i = 0; i < inc_entries; ++i) {
  1872. new_table_data_start[i] = init_val;
  1873. }
  1874. table_inst->cur_size = entry_count;
  1875. return true;
  1876. }
  1877. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  1878. bool
  1879. wasm_call_indirect(WASMExecEnv *exec_env,
  1880. uint32_t tbl_idx,
  1881. uint32_t element_indices,
  1882. uint32_t argc, uint32_t argv[])
  1883. {
  1884. WASMModuleInstance *module_inst = NULL;
  1885. WASMTableInstance *table_inst = NULL;
  1886. uint32_t function_indices = 0;
  1887. WASMFunctionInstance *function_inst = NULL;
  1888. module_inst =
  1889. (WASMModuleInstance*)exec_env->module_inst;
  1890. bh_assert(module_inst);
  1891. table_inst = module_inst->tables[tbl_idx];
  1892. if (!table_inst) {
  1893. wasm_set_exception(module_inst, "unknown table");
  1894. goto got_exception;
  1895. }
  1896. if (element_indices >= table_inst->cur_size) {
  1897. wasm_set_exception(module_inst, "undefined element");
  1898. goto got_exception;
  1899. }
  1900. /**
  1901. * please be aware that table_inst->base_addr may point
  1902. * to another module's table
  1903. **/
  1904. function_indices = ((uint32_t*)table_inst->base_addr)[element_indices];
  1905. if (function_indices == NULL_REF) {
  1906. wasm_set_exception(module_inst, "uninitialized element");
  1907. goto got_exception;
  1908. }
  1909. /**
  1910. * we insist to call functions owned by the module itself
  1911. **/
  1912. if (function_indices >= module_inst->function_count) {
  1913. wasm_set_exception(module_inst, "unknown function");
  1914. goto got_exception;
  1915. }
  1916. function_inst = module_inst->functions + function_indices;
  1917. wasm_interp_call_wasm(module_inst, exec_env, function_inst, argc, argv);
  1918. (void)clear_wasi_proc_exit_exception(module_inst);
  1919. return !wasm_get_exception(module_inst) ? true : false;
  1920. got_exception:
  1921. return false;
  1922. }
  1923. #if WASM_ENABLE_THREAD_MGR != 0
  1924. bool
  1925. wasm_set_aux_stack(WASMExecEnv *exec_env,
  1926. uint32 start_offset, uint32 size)
  1927. {
  1928. WASMModuleInstance *module_inst =
  1929. (WASMModuleInstance*)exec_env->module_inst;
  1930. uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
  1931. uint32 data_end = module_inst->module->aux_data_end;
  1932. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  1933. bool is_stack_before_data =
  1934. stack_bottom < data_end ? true : false;
  1935. /* Check the aux stack space, currently we don't allocate space in heap */
  1936. if ((is_stack_before_data && (size > start_offset))
  1937. || ((!is_stack_before_data) && (start_offset - data_end < size)))
  1938. return false;
  1939. if (stack_top_idx != (uint32)-1) {
  1940. /* The aux stack top is a wasm global,
  1941. set the initial value for the global */
  1942. uint8 *global_addr =
  1943. module_inst->global_data +
  1944. module_inst->globals[stack_top_idx].data_offset;
  1945. *(int32*)global_addr = start_offset;
  1946. /* The aux stack boundary is a constant value,
  1947. set the value to exec_env */
  1948. exec_env->aux_stack_boundary.boundary = start_offset - size;
  1949. exec_env->aux_stack_bottom.bottom = start_offset;
  1950. return true;
  1951. }
  1952. return false;
  1953. }
  1954. bool
  1955. wasm_get_aux_stack(WASMExecEnv *exec_env,
  1956. uint32 *start_offset, uint32 *size)
  1957. {
  1958. WASMModuleInstance *module_inst =
  1959. (WASMModuleInstance*)exec_env->module_inst;
  1960. /* The aux stack information is resolved in loader
  1961. and store in module */
  1962. uint32 stack_bottom =
  1963. module_inst->module->aux_stack_bottom;
  1964. uint32 total_aux_stack_size =
  1965. module_inst->module->aux_stack_size;
  1966. if (stack_bottom != 0 && total_aux_stack_size != 0) {
  1967. if (start_offset)
  1968. *start_offset = stack_bottom;
  1969. if (size)
  1970. *size = total_aux_stack_size;
  1971. return true;
  1972. }
  1973. return false;
  1974. }
  1975. #endif
  1976. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  1977. void
  1978. wasm_get_module_mem_consumption(const WASMModule *module,
  1979. WASMModuleMemConsumption *mem_conspn)
  1980. {
  1981. uint32 i, size;
  1982. memset(mem_conspn, 0, sizeof(*mem_conspn));
  1983. mem_conspn->module_struct_size = sizeof(WASMModule);
  1984. mem_conspn->types_size = sizeof(WASMType *) * module->type_count;
  1985. for (i = 0; i < module->type_count; i++) {
  1986. WASMType *type = module->types[i];
  1987. size = offsetof(WASMType, types) +
  1988. sizeof(uint8) * (type->param_count + type->result_count);
  1989. mem_conspn->types_size += size;
  1990. }
  1991. mem_conspn->imports_size = sizeof(WASMImport) * module->import_count;
  1992. mem_conspn->functions_size = sizeof(WASMFunction *)
  1993. * module->function_count;
  1994. for (i = 0; i < module->function_count; i++) {
  1995. WASMFunction *func = module->functions[i];
  1996. WASMType *type = func->func_type;
  1997. size = sizeof(WASMFunction) + func->local_count
  1998. + sizeof(uint16) * (type->param_count + func->local_count);
  1999. #if WASM_ENABLE_FAST_INTERP != 0
  2000. size += func->code_compiled_size
  2001. + sizeof(uint32) * func->const_cell_num;
  2002. #endif
  2003. mem_conspn->functions_size += size;
  2004. }
  2005. mem_conspn->tables_size = sizeof(WASMTable) * module->table_count;
  2006. mem_conspn->memories_size = sizeof(WASMMemory) * module->memory_count;
  2007. mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count;
  2008. mem_conspn->exports_size = sizeof(WASMExport) * module->export_count;
  2009. mem_conspn->table_segs_size = sizeof(WASMTableSeg)
  2010. * module->table_seg_count;
  2011. for (i = 0; i < module->table_seg_count; i++) {
  2012. WASMTableSeg *table_seg = &module->table_segments[i];
  2013. mem_conspn->tables_size += sizeof(uint32)
  2014. * table_seg->function_count;
  2015. }
  2016. mem_conspn->data_segs_size = sizeof(WASMDataSeg*)
  2017. * module->data_seg_count;
  2018. for (i = 0; i < module->data_seg_count; i++) {
  2019. mem_conspn->data_segs_size += sizeof(WASMDataSeg);
  2020. }
  2021. if (module->const_str_list) {
  2022. StringNode *node = module->const_str_list, *node_next;
  2023. while (node) {
  2024. node_next = node->next;
  2025. mem_conspn->const_strs_size += sizeof(StringNode)
  2026. + strlen(node->str) + 1;
  2027. node = node_next;
  2028. }
  2029. }
  2030. mem_conspn->total_size += mem_conspn->module_struct_size;
  2031. mem_conspn->total_size += mem_conspn->types_size;
  2032. mem_conspn->total_size += mem_conspn->imports_size;
  2033. mem_conspn->total_size += mem_conspn->functions_size;
  2034. mem_conspn->total_size += mem_conspn->tables_size;
  2035. mem_conspn->total_size += mem_conspn->memories_size;
  2036. mem_conspn->total_size += mem_conspn->globals_size;
  2037. mem_conspn->total_size += mem_conspn->exports_size;
  2038. mem_conspn->total_size += mem_conspn->table_segs_size;
  2039. mem_conspn->total_size += mem_conspn->data_segs_size;
  2040. mem_conspn->total_size += mem_conspn->const_strs_size;
  2041. #if WASM_ENABLE_AOT != 0
  2042. mem_conspn->total_size += mem_conspn->aot_code_size;
  2043. #endif
  2044. }
  2045. void
  2046. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
  2047. WASMModuleInstMemConsumption *mem_conspn)
  2048. {
  2049. uint32 i, size;
  2050. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2051. mem_conspn->module_inst_struct_size = sizeof(WASMModuleInstance);
  2052. mem_conspn->memories_size = sizeof(WASMMemoryInstance *)
  2053. * module_inst->memory_count;
  2054. for (i = 0; i < module_inst->memory_count; i++) {
  2055. WASMMemoryInstance *memory = module_inst->memories[i];
  2056. size = sizeof(WASMMemoryInstance)
  2057. + memory->num_bytes_per_page * memory->cur_page_count;
  2058. mem_conspn->memories_size += size;
  2059. mem_conspn->app_heap_size += memory->heap_data_end
  2060. - memory->heap_data;
  2061. /* size of app heap structure */
  2062. mem_conspn->memories_size +=
  2063. mem_allocator_get_heap_struct_size();
  2064. }
  2065. mem_conspn->tables_size = sizeof(WASMTableInstance *)
  2066. * module_inst->table_count;
  2067. for (i = 0; i < module_inst->table_count; i++) {
  2068. WASMTableInstance *table = module_inst->tables[i];
  2069. #if WASM_ENABLE_MULTI_MODULE != 0
  2070. if (table->table_inst_linked) {
  2071. size = offsetof(WASMTableInstance, base_addr);
  2072. }
  2073. else
  2074. #endif
  2075. {
  2076. size = offsetof(WASMTableInstance, base_addr)
  2077. + sizeof(uint32) * table->cur_size;
  2078. }
  2079. mem_conspn->tables_size += size;
  2080. }
  2081. mem_conspn->functions_size = sizeof(WASMFunctionInstance)
  2082. * module_inst->function_count;
  2083. mem_conspn->globals_size = sizeof(WASMGlobalInstance)
  2084. * module_inst->global_count;
  2085. if (module_inst->global_count > 0) {
  2086. WASMGlobalInstance *global =
  2087. &module_inst->globals[module_inst->global_count - 1];
  2088. mem_conspn->globals_size += global->data_offset
  2089. + wasm_value_type_size(global->type);
  2090. }
  2091. mem_conspn->exports_size = sizeof(WASMExportFuncInstance)
  2092. * module_inst->export_func_count;
  2093. mem_conspn->total_size += mem_conspn->module_inst_struct_size;
  2094. mem_conspn->total_size += mem_conspn->memories_size;
  2095. mem_conspn->total_size += mem_conspn->functions_size;
  2096. mem_conspn->total_size += mem_conspn->tables_size;
  2097. mem_conspn->total_size += mem_conspn->globals_size;
  2098. mem_conspn->total_size += mem_conspn->exports_size;
  2099. }
  2100. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0)
  2101. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  2102. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2103. void
  2104. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
  2105. {
  2106. WASMModuleInstance *module_inst =
  2107. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  2108. WASMInterpFrame *cur_frame =
  2109. wasm_exec_env_get_cur_frame(exec_env);
  2110. WASMFunctionInstance *func_inst;
  2111. WASMExportFuncInstance *export_func;
  2112. const char *func_name = NULL;
  2113. uint32 n, i;
  2114. os_printf("\n");
  2115. for (n = 0; cur_frame && cur_frame->function; n++) {
  2116. func_name = NULL;
  2117. func_inst = cur_frame->function;
  2118. if (func_inst->is_import_func) {
  2119. func_name = func_inst->u.func_import->field_name;
  2120. }
  2121. else {
  2122. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2123. func_name = func_inst->u.func->field_name;
  2124. #endif
  2125. /* if custom name section is not generated,
  2126. search symbols from export table */
  2127. if (!func_name) {
  2128. for (i = 0; i < module_inst->export_func_count; i++) {
  2129. export_func = module_inst->export_functions + i;
  2130. if (export_func->function == func_inst) {
  2131. func_name = export_func->name;
  2132. break;
  2133. }
  2134. }
  2135. }
  2136. }
  2137. /* function name not exported, print number instead */
  2138. if (func_name == NULL) {
  2139. os_printf("#%02d $f%d \n", n, func_inst - module_inst->functions);
  2140. }
  2141. else {
  2142. os_printf("#%02d %s \n", n, func_name);
  2143. }
  2144. cur_frame = cur_frame->prev_frame;
  2145. }
  2146. os_printf("\n");
  2147. }
  2148. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */