wasm_runtime.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518
  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. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1028. if (!(module_inst->frames = runtime_malloc((uint64)sizeof(Vector),
  1029. error_buf, error_buf_size))) {
  1030. goto fail;
  1031. }
  1032. #endif
  1033. /* Instantiate global firstly to get the mutable data size */
  1034. global_count = module->import_global_count + module->global_count;
  1035. if (global_count
  1036. && !(globals = globals_instantiate(module, module_inst,
  1037. &global_data_size,
  1038. error_buf, error_buf_size))) {
  1039. goto fail;
  1040. }
  1041. module_inst->global_count = global_count;
  1042. module_inst->globals = globals;
  1043. module_inst->memory_count =
  1044. module->import_memory_count + module->memory_count;
  1045. module_inst->table_count =
  1046. module->import_table_count + module->table_count;
  1047. module_inst->function_count =
  1048. module->import_function_count + module->function_count;
  1049. /* export */
  1050. module_inst->export_func_count = get_export_count(module, EXPORT_KIND_FUNC);
  1051. #if WASM_ENABLE_MULTI_MODULE != 0
  1052. module_inst->export_tab_count = get_export_count(module, EXPORT_KIND_TABLE);
  1053. module_inst->export_mem_count = get_export_count(module, EXPORT_KIND_MEMORY);
  1054. module_inst->export_glob_count = get_export_count(module, EXPORT_KIND_GLOBAL);
  1055. #endif
  1056. if (global_count > 0) {
  1057. if (!(module_inst->global_data = runtime_malloc
  1058. (global_data_size, error_buf, error_buf_size))) {
  1059. goto fail;
  1060. }
  1061. }
  1062. /* Instantiate memories/tables/functions */
  1063. if ((module_inst->memory_count > 0
  1064. && !(module_inst->memories =
  1065. memories_instantiate(module,
  1066. module_inst,
  1067. heap_size, error_buf, error_buf_size)))
  1068. || (module_inst->table_count > 0
  1069. && !(module_inst->tables =
  1070. tables_instantiate(module,
  1071. module_inst,
  1072. error_buf, error_buf_size)))
  1073. || (module_inst->function_count > 0
  1074. && !(module_inst->functions =
  1075. functions_instantiate(module,
  1076. module_inst,
  1077. error_buf, error_buf_size)))
  1078. || (module_inst->export_func_count > 0
  1079. && !(module_inst->export_functions = export_functions_instantiate(
  1080. module, module_inst, module_inst->export_func_count,
  1081. error_buf, error_buf_size)))
  1082. #if WASM_ENABLE_MULTI_MODULE != 0
  1083. || (module_inst->export_glob_count > 0
  1084. && !(module_inst->export_globals = export_globals_instantiate(
  1085. module, module_inst, module_inst->export_glob_count,
  1086. error_buf, error_buf_size)))
  1087. #endif
  1088. ) {
  1089. goto fail;
  1090. }
  1091. if (global_count > 0) {
  1092. /* Initialize the global data */
  1093. global_data = module_inst->global_data;
  1094. global_data_end = global_data + global_data_size;
  1095. global = globals;
  1096. for (i = 0; i < global_count; i++, global++) {
  1097. switch (global->type) {
  1098. case VALUE_TYPE_I32:
  1099. case VALUE_TYPE_F32:
  1100. #if WASM_ENABLE_REF_TYPES != 0
  1101. case VALUE_TYPE_FUNCREF:
  1102. case VALUE_TYPE_EXTERNREF:
  1103. #endif
  1104. *(int32*)global_data = global->initial_value.i32;
  1105. global_data += sizeof(int32);
  1106. break;
  1107. case VALUE_TYPE_I64:
  1108. case VALUE_TYPE_F64:
  1109. bh_memcpy_s(global_data, (uint32)(global_data_end - global_data),
  1110. &global->initial_value.i64, sizeof(int64));
  1111. global_data += sizeof(int64);
  1112. break;
  1113. default:
  1114. bh_assert(0);
  1115. }
  1116. }
  1117. bh_assert(global_data == global_data_end);
  1118. }
  1119. if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
  1120. goto fail;
  1121. }
  1122. /* Initialize the memory data with data segment section */
  1123. module_inst->default_memory =
  1124. module_inst->memory_count ? module_inst->memories[0] : NULL;
  1125. for (i = 0; i < module->data_seg_count; i++) {
  1126. WASMMemoryInstance *memory = NULL;
  1127. uint8 *memory_data = NULL;
  1128. uint32 memory_size = 0;
  1129. WASMDataSeg *data_seg = module->data_segments[i];
  1130. #if WASM_ENABLE_BULK_MEMORY != 0
  1131. if (data_seg->is_passive)
  1132. continue;
  1133. #endif
  1134. /* has check it in loader */
  1135. memory = module_inst->memories[data_seg->memory_index];
  1136. bh_assert(memory);
  1137. memory_data = memory->memory_data;
  1138. memory_size = memory->num_bytes_per_page * memory->cur_page_count;
  1139. bh_assert(memory_data || memory_size == 0);
  1140. bh_assert(data_seg->base_offset.init_expr_type
  1141. == INIT_EXPR_TYPE_I32_CONST
  1142. || data_seg->base_offset.init_expr_type
  1143. == INIT_EXPR_TYPE_GET_GLOBAL);
  1144. if (data_seg->base_offset.init_expr_type
  1145. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1146. if (!check_global_init_expr(module,
  1147. data_seg->base_offset.u.global_index,
  1148. error_buf, error_buf_size)) {
  1149. goto fail;
  1150. }
  1151. if (!globals
  1152. || globals[data_seg->base_offset.u.global_index].type
  1153. != VALUE_TYPE_I32) {
  1154. set_error_buf(error_buf, error_buf_size,
  1155. "data segment does not fit");
  1156. goto fail;
  1157. }
  1158. data_seg->base_offset.u.i32 =
  1159. globals[data_seg->base_offset.u.global_index]
  1160. .initial_value.i32;
  1161. }
  1162. /* check offset */
  1163. base_offset = (uint32)data_seg->base_offset.u.i32;
  1164. if (base_offset > memory_size) {
  1165. LOG_DEBUG("base_offset(%d) > memory_size(%d)", base_offset,
  1166. memory_size);
  1167. #if WASM_ENABLE_REF_TYPES != 0
  1168. set_error_buf(error_buf, error_buf_size,
  1169. "out of bounds memory access");
  1170. #else
  1171. set_error_buf(error_buf, error_buf_size,
  1172. "data segment does not fit");
  1173. #endif
  1174. goto fail;
  1175. }
  1176. /* check offset + length(could be zero) */
  1177. length = data_seg->data_length;
  1178. if (base_offset + length > memory_size) {
  1179. LOG_DEBUG("base_offset(%d) + length(%d) > memory_size(%d)",
  1180. base_offset, length, memory_size);
  1181. #if WASM_ENABLE_REF_TYPES != 0
  1182. set_error_buf(error_buf, error_buf_size,
  1183. "out of bounds memory access");
  1184. #else
  1185. set_error_buf(error_buf, error_buf_size,
  1186. "data segment does not fit");
  1187. #endif
  1188. goto fail;
  1189. }
  1190. if (memory_data) {
  1191. bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
  1192. data_seg->data, length);
  1193. }
  1194. }
  1195. /* Initialize the table data with table segment section */
  1196. module_inst->default_table =
  1197. module_inst->table_count ? module_inst->tables[0] : NULL;
  1198. /* in case there is no table */
  1199. for (i = 0; module_inst->table_count > 0 && i < module->table_seg_count;
  1200. i++) {
  1201. WASMTableSeg *table_seg = module->table_segments + i;
  1202. /* has check it in loader */
  1203. WASMTableInstance *table = module_inst->tables[table_seg->table_index];
  1204. bh_assert(table);
  1205. #if WASM_ENABLE_REF_TYPES != 0
  1206. if (table->elem_type != VALUE_TYPE_FUNCREF
  1207. && table->elem_type != VALUE_TYPE_EXTERNREF) {
  1208. set_error_buf(error_buf, error_buf_size,
  1209. "elements segment does not fit");
  1210. goto fail;
  1211. }
  1212. #endif
  1213. uint32 *table_data = (uint32 *)table->base_addr;
  1214. #if WASM_ENABLE_MULTI_MODULE != 0
  1215. table_data = table->table_inst_linked
  1216. ? (uint32 *)table->table_inst_linked->base_addr
  1217. : table_data;
  1218. #endif
  1219. bh_assert(table_data);
  1220. #if WASM_ENABLE_REF_TYPES != 0
  1221. if (!wasm_elem_is_active(table_seg->mode))
  1222. continue;
  1223. #endif
  1224. /* init vec(funcidx) or vec(expr) */
  1225. bh_assert(
  1226. table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_I32_CONST
  1227. || table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL
  1228. #if WASM_ENABLE_REF_TYPES != 0
  1229. || table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_FUNCREF_CONST
  1230. || table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST
  1231. #endif
  1232. );
  1233. if (table_seg->base_offset.init_expr_type
  1234. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1235. if (!check_global_init_expr(module,
  1236. table_seg->base_offset.u.global_index,
  1237. error_buf, error_buf_size)) {
  1238. goto fail;
  1239. }
  1240. if (!globals
  1241. || globals[table_seg->base_offset.u.global_index].type
  1242. != VALUE_TYPE_I32) {
  1243. set_error_buf(error_buf, error_buf_size,
  1244. "elements segment does not fit");
  1245. goto fail;
  1246. }
  1247. table_seg->base_offset.u.i32 =
  1248. globals[table_seg->base_offset.u.global_index].initial_value.i32;
  1249. }
  1250. /* check offset since length might negative */
  1251. if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
  1252. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  1253. table_seg->base_offset.u.i32, table->cur_size);
  1254. #if WASM_ENABLE_REF_TYPES != 0
  1255. set_error_buf(error_buf, error_buf_size,
  1256. "out of bounds table access");
  1257. #else
  1258. set_error_buf(error_buf, error_buf_size,
  1259. "elements segment does not fit");
  1260. #endif
  1261. goto fail;
  1262. }
  1263. /* check offset + length(could be zero) */
  1264. length = table_seg->function_count;
  1265. if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
  1266. LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
  1267. table_seg->base_offset.u.i32, length, table->cur_size);
  1268. #if WASM_ENABLE_REF_TYPES != 0
  1269. set_error_buf(error_buf, error_buf_size,
  1270. "out of bounds table access");
  1271. #else
  1272. set_error_buf(error_buf, error_buf_size,
  1273. "elements segment does not fit");
  1274. #endif
  1275. goto fail;
  1276. }
  1277. /**
  1278. * Check function index in the current module inst for now.
  1279. * will check the linked table inst owner in future.
  1280. * so loader check is enough
  1281. */
  1282. bh_memcpy_s(
  1283. table_data + table_seg->base_offset.u.i32,
  1284. (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32)
  1285. * sizeof(uint32)),
  1286. table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
  1287. }
  1288. /* module instance type */
  1289. module_inst->module_type = Wasm_Module_Bytecode;
  1290. /* Initialize the thread related data */
  1291. if (stack_size == 0)
  1292. stack_size = DEFAULT_WASM_STACK_SIZE;
  1293. #if WASM_ENABLE_SPEC_TEST != 0
  1294. if (stack_size < 48 *1024)
  1295. stack_size = 48 * 1024;
  1296. #endif
  1297. module_inst->default_wasm_stack_size = stack_size;
  1298. if (module->malloc_function != (uint32)-1) {
  1299. module_inst->malloc_function =
  1300. &module_inst->functions[module->malloc_function];
  1301. }
  1302. if (module->free_function != (uint32)-1) {
  1303. module_inst->free_function =
  1304. &module_inst->functions[module->free_function];
  1305. }
  1306. if (module->retain_function != (uint32)-1) {
  1307. module_inst->retain_function =
  1308. &module_inst->functions[module->retain_function];
  1309. }
  1310. #if WASM_ENABLE_LIBC_WASI != 0
  1311. /* The sub-instance will get the wasi_ctx from main-instance */
  1312. if (!is_sub_inst) {
  1313. if (!wasm_runtime_init_wasi((WASMModuleInstanceCommon*)module_inst,
  1314. module->wasi_args.dir_list,
  1315. module->wasi_args.dir_count,
  1316. module->wasi_args.map_dir_list,
  1317. module->wasi_args.map_dir_count,
  1318. module->wasi_args.env,
  1319. module->wasi_args.env_count,
  1320. module->wasi_args.argv,
  1321. module->wasi_args.argc,
  1322. module->wasi_args.stdio[0],
  1323. module->wasi_args.stdio[1],
  1324. module->wasi_args.stdio[2],
  1325. error_buf, error_buf_size)) {
  1326. goto fail;
  1327. }
  1328. }
  1329. #endif
  1330. if (module->start_function != (uint32)-1) {
  1331. /* TODO: fix start function can be import function issue */
  1332. if (module->start_function >= module->import_function_count)
  1333. module_inst->start_function =
  1334. &module_inst->functions[module->start_function];
  1335. }
  1336. /* Execute __post_instantiate function */
  1337. if (!execute_post_inst_function(module_inst)
  1338. || !execute_start_function(module_inst)) {
  1339. set_error_buf(error_buf, error_buf_size,
  1340. module_inst->cur_exception);
  1341. goto fail;
  1342. }
  1343. #if WASM_ENABLE_BULK_MEMORY != 0
  1344. #if WASM_ENABLE_LIBC_WASI != 0
  1345. if (!module->is_wasi_module) {
  1346. #endif
  1347. /* Only execute the memory init function for main instance because
  1348. the data segments will be dropped once initialized.
  1349. */
  1350. if (!is_sub_inst) {
  1351. if (!execute_memory_init_function(module_inst)) {
  1352. set_error_buf(error_buf, error_buf_size,
  1353. module_inst->cur_exception);
  1354. goto fail;
  1355. }
  1356. }
  1357. #if WASM_ENABLE_LIBC_WASI != 0
  1358. }
  1359. #endif
  1360. #endif
  1361. #if WASM_ENABLE_MEMORY_TRACING != 0
  1362. wasm_runtime_dump_module_inst_mem_consumption
  1363. ((WASMModuleInstanceCommon *)module_inst);
  1364. #endif
  1365. (void)global_data_end;
  1366. return module_inst;
  1367. fail:
  1368. wasm_deinstantiate(module_inst, false);
  1369. return NULL;
  1370. }
  1371. void
  1372. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
  1373. {
  1374. if (!module_inst)
  1375. return;
  1376. #if WASM_ENABLE_MULTI_MODULE != 0
  1377. sub_module_deinstantiate(module_inst);
  1378. #endif
  1379. #if WASM_ENABLE_LIBC_WASI != 0
  1380. /* Destroy wasi resource before freeing app heap, since some fields of
  1381. wasi contex are allocated from app heap, and if app heap is freed,
  1382. these fields will be set to NULL, we cannot free their internal data
  1383. which may allocated from global heap. */
  1384. /* Only destroy wasi ctx in the main module instance */
  1385. if (!is_sub_inst)
  1386. wasm_runtime_destroy_wasi((WASMModuleInstanceCommon*)module_inst);
  1387. #endif
  1388. if (module_inst->memory_count > 0)
  1389. memories_deinstantiate(
  1390. module_inst,
  1391. module_inst->memories, module_inst->memory_count);
  1392. tables_deinstantiate(module_inst->tables, module_inst->table_count);
  1393. functions_deinstantiate(module_inst->functions, module_inst->function_count);
  1394. globals_deinstantiate(module_inst->globals);
  1395. export_functions_deinstantiate(module_inst->export_functions);
  1396. #if WASM_ENABLE_MULTI_MODULE != 0
  1397. export_globals_deinstantiate(module_inst->export_globals);
  1398. #endif
  1399. if (module_inst->global_data)
  1400. wasm_runtime_free(module_inst->global_data);
  1401. #if WASM_ENABLE_REF_TYPES != 0
  1402. wasm_externref_cleanup((WASMModuleInstanceCommon*)module_inst);
  1403. #endif
  1404. if (module_inst->exec_env_singleton)
  1405. wasm_exec_env_destroy(module_inst->exec_env_singleton);
  1406. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1407. if (module_inst->frames) {
  1408. bh_vector_destroy(module_inst->frames);
  1409. wasm_runtime_free(module_inst->frames);
  1410. module_inst->frames = NULL;
  1411. }
  1412. #endif
  1413. wasm_runtime_free(module_inst);
  1414. }
  1415. WASMFunctionInstance*
  1416. wasm_lookup_function(const WASMModuleInstance *module_inst,
  1417. const char *name, const char *signature)
  1418. {
  1419. uint32 i;
  1420. for (i = 0; i < module_inst->export_func_count; i++)
  1421. if (!strcmp(module_inst->export_functions[i].name, name))
  1422. return module_inst->export_functions[i].function;
  1423. (void)signature;
  1424. return NULL;
  1425. }
  1426. #if WASM_ENABLE_MULTI_MODULE != 0
  1427. WASMGlobalInstance *
  1428. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name)
  1429. {
  1430. uint32 i;
  1431. for (i = 0; i < module_inst->export_glob_count; i++)
  1432. if (!strcmp(module_inst->export_globals[i].name, name))
  1433. return module_inst->export_globals[i].global;
  1434. return NULL;
  1435. }
  1436. WASMMemoryInstance *
  1437. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name)
  1438. {
  1439. /**
  1440. * using a strong assumption that one module instance only has
  1441. * one memory instance
  1442. */
  1443. (void)module_inst->export_memories;
  1444. return module_inst->memories[0];
  1445. }
  1446. WASMTableInstance *
  1447. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
  1448. {
  1449. /**
  1450. * using a strong assumption that one module instance only has
  1451. * one table instance
  1452. */
  1453. (void)module_inst->export_tables;
  1454. return module_inst->tables[0];
  1455. }
  1456. #endif
  1457. static bool
  1458. clear_wasi_proc_exit_exception(WASMModuleInstance *module_inst)
  1459. {
  1460. #if WASM_ENABLE_LIBC_WASI != 0
  1461. const char *exception = wasm_get_exception(module_inst);
  1462. if (exception && !strcmp(exception, "Exception: wasi proc exit")) {
  1463. /* The "wasi proc exit" exception is thrown by native lib to
  1464. let wasm app exit, which is a normal behavior, we clear
  1465. the exception here. */
  1466. wasm_set_exception(module_inst, NULL);
  1467. return true;
  1468. }
  1469. return false;
  1470. #else
  1471. return false;
  1472. #endif
  1473. }
  1474. bool
  1475. wasm_call_function(WASMExecEnv *exec_env,
  1476. WASMFunctionInstance *function,
  1477. unsigned argc, uint32 argv[])
  1478. {
  1479. WASMModuleInstance *module_inst = (WASMModuleInstance*)exec_env->module_inst;
  1480. /* set thread handle and stack boundary */
  1481. wasm_exec_env_set_thread_info(exec_env);
  1482. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  1483. (void)clear_wasi_proc_exit_exception(module_inst);
  1484. return !wasm_get_exception(module_inst) ? true : false;
  1485. }
  1486. bool
  1487. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  1488. WASMFunctionInstance *func,
  1489. unsigned argc, uint32 argv[])
  1490. {
  1491. WASMExecEnv *exec_env;
  1492. bool ret;
  1493. #if WASM_ENABLE_THREAD_MGR != 0
  1494. WASMExecEnv *existing_exec_env = NULL;
  1495. if (!(existing_exec_env = exec_env =
  1496. wasm_clusters_search_exec_env(
  1497. (WASMModuleInstanceCommon*)module_inst))) {
  1498. #endif
  1499. if (!(exec_env = wasm_exec_env_create(
  1500. (WASMModuleInstanceCommon*)module_inst,
  1501. module_inst->default_wasm_stack_size))) {
  1502. wasm_set_exception(module_inst, "allocate memory failed");
  1503. return false;
  1504. }
  1505. #if WASM_ENABLE_THREAD_MGR != 0
  1506. }
  1507. #endif
  1508. #if WASM_ENABLE_REF_TYPES != 0
  1509. wasm_runtime_prepare_call_function(exec_env, func);
  1510. #endif
  1511. ret = wasm_call_function(exec_env, func, argc, argv);
  1512. #if WASM_ENABLE_REF_TYPES != 0
  1513. wasm_runtime_finalize_call_function(exec_env, func, ret, argv);
  1514. #endif
  1515. #if WASM_ENABLE_THREAD_MGR != 0
  1516. /* don't destroy the exec_env if it's searched from the cluster */
  1517. if (!existing_exec_env)
  1518. #endif
  1519. wasm_exec_env_destroy(exec_env);
  1520. return ret;
  1521. }
  1522. bool
  1523. wasm_create_exec_env_singleton(WASMModuleInstance *module_inst)
  1524. {
  1525. WASMExecEnv *exec_env =
  1526. wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
  1527. module_inst->default_wasm_stack_size);
  1528. if (exec_env)
  1529. module_inst->exec_env_singleton = exec_env;
  1530. return exec_env ? true : false;
  1531. }
  1532. void
  1533. wasm_set_exception(WASMModuleInstance *module_inst,
  1534. const char *exception)
  1535. {
  1536. if (exception)
  1537. snprintf(module_inst->cur_exception,
  1538. sizeof(module_inst->cur_exception),
  1539. "Exception: %s", exception);
  1540. else
  1541. module_inst->cur_exception[0] = '\0';
  1542. }
  1543. const char*
  1544. wasm_get_exception(WASMModuleInstance *module_inst)
  1545. {
  1546. if (module_inst->cur_exception[0] == '\0')
  1547. return NULL;
  1548. else
  1549. return module_inst->cur_exception;
  1550. }
  1551. #if WASM_ENABLE_PERF_PROFILING != 0
  1552. void
  1553. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
  1554. {
  1555. WASMExportFuncInstance *export_func;
  1556. WASMFunctionInstance *func_inst;
  1557. char *func_name;
  1558. uint32 i, j;
  1559. os_printf("Performance profiler data:\n");
  1560. for (i = 0; i < module_inst->function_count; i++) {
  1561. func_inst = module_inst->functions + i;
  1562. if (func_inst->is_import_func) {
  1563. func_name = func_inst->u.func_import->field_name;
  1564. }
  1565. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  1566. else if (func_inst->u.func->field_name) {
  1567. func_name = func_inst->u.func->field_name;
  1568. }
  1569. #endif
  1570. else {
  1571. func_name = NULL;
  1572. for (j = 0; j < module_inst->export_func_count; j++) {
  1573. export_func = module_inst->export_functions + j;
  1574. if (export_func->function == func_inst) {
  1575. func_name = export_func->name;
  1576. break;
  1577. }
  1578. }
  1579. }
  1580. if (func_name)
  1581. os_printf(" func %s, execution time: %.3f ms, execution count: %d times\n",
  1582. func_name, module_inst->functions[i].total_exec_time / 1000.0f,
  1583. module_inst->functions[i].total_exec_cnt);
  1584. else
  1585. os_printf(" func %d, execution time: %.3f ms, execution count: %d times\n",
  1586. i, module_inst->functions[i].total_exec_time / 1000.0f,
  1587. module_inst->functions[i].total_exec_cnt);
  1588. }
  1589. }
  1590. #endif
  1591. uint32
  1592. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  1593. void **p_native_addr)
  1594. {
  1595. WASMMemoryInstance *memory = module_inst->default_memory;
  1596. uint8 *addr = NULL;
  1597. uint32 offset = 0;
  1598. if (!memory) {
  1599. wasm_set_exception(module_inst, "uninitialized memory");
  1600. return 0;
  1601. }
  1602. if (memory->heap_handle) {
  1603. addr = mem_allocator_malloc(memory->heap_handle, size);
  1604. }
  1605. else if (module_inst->malloc_function
  1606. && module_inst->free_function) {
  1607. if (!execute_malloc_function(module_inst,
  1608. module_inst->malloc_function,
  1609. module_inst->retain_function,
  1610. size, &offset)) {
  1611. return 0;
  1612. }
  1613. /* If we use app's malloc function,
  1614. the default memory may be changed while memory growing */
  1615. memory = module_inst->default_memory;
  1616. addr = offset ? memory->memory_data + offset : NULL;
  1617. }
  1618. if (!addr) {
  1619. if (memory->heap_handle
  1620. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  1621. LOG_ERROR("Error: app heap is corrupted, if the wasm file "
  1622. "is compiled by wasi-sdk-12.0 or larger version, "
  1623. "please add -Wl,--export=malloc -Wl,--export=free "
  1624. " to export malloc and free functions.");
  1625. wasm_set_exception(module_inst, "app heap corrupted");
  1626. }
  1627. else {
  1628. wasm_set_exception(module_inst, "out of memory");
  1629. }
  1630. return 0;
  1631. }
  1632. if (p_native_addr)
  1633. *p_native_addr = addr;
  1634. return (uint32)(addr - memory->memory_data);
  1635. }
  1636. uint32
  1637. wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
  1638. void **p_native_addr)
  1639. {
  1640. WASMMemoryInstance *memory = module_inst->default_memory;
  1641. uint8 *addr = NULL;
  1642. if (!memory) {
  1643. wasm_set_exception(module_inst, "uninitialized memory");
  1644. return 0;
  1645. }
  1646. if (memory->heap_handle) {
  1647. addr = mem_allocator_realloc(memory->heap_handle,
  1648. memory->memory_data + ptr, size);
  1649. }
  1650. /* Only support realloc in WAMR's app heap */
  1651. if (!addr) {
  1652. if (memory->heap_handle
  1653. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  1654. wasm_set_exception(module_inst, "app heap corrupted");
  1655. }
  1656. else {
  1657. wasm_set_exception(module_inst, "out of memory");
  1658. }
  1659. return 0;
  1660. }
  1661. if (p_native_addr)
  1662. *p_native_addr = addr;
  1663. return (uint32)(addr - memory->memory_data);
  1664. }
  1665. void
  1666. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
  1667. {
  1668. if (ptr) {
  1669. WASMMemoryInstance *memory = module_inst->default_memory;
  1670. uint8* addr;
  1671. if (!memory) {
  1672. return;
  1673. }
  1674. addr = memory->memory_data + ptr;
  1675. if (memory->heap_handle
  1676. && memory->heap_data <= addr
  1677. && addr < memory->heap_data_end) {
  1678. mem_allocator_free(memory->heap_handle, addr);
  1679. }
  1680. else if (module_inst->malloc_function
  1681. && module_inst->free_function
  1682. && memory->memory_data <= addr
  1683. && addr < memory->memory_data_end) {
  1684. execute_free_function(module_inst,
  1685. module_inst->free_function,
  1686. ptr);
  1687. }
  1688. }
  1689. }
  1690. uint32
  1691. wasm_module_dup_data(WASMModuleInstance *module_inst,
  1692. const char *src, uint32 size)
  1693. {
  1694. char *buffer;
  1695. uint32 buffer_offset = wasm_module_malloc(module_inst, size,
  1696. (void**)&buffer);
  1697. if (buffer_offset != 0) {
  1698. buffer = wasm_addr_app_to_native(module_inst, buffer_offset);
  1699. bh_memcpy_s(buffer, size, src, size);
  1700. }
  1701. return buffer_offset;
  1702. }
  1703. bool
  1704. wasm_validate_app_addr(WASMModuleInstance *module_inst,
  1705. uint32 app_offset, uint32 size)
  1706. {
  1707. WASMMemoryInstance *memory = module_inst->default_memory;
  1708. uint32 memory_data_size;
  1709. if (!memory) {
  1710. goto fail;
  1711. }
  1712. memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
  1713. /* integer overflow check */
  1714. if (app_offset > UINT32_MAX - size) {
  1715. goto fail;
  1716. }
  1717. if (app_offset + size <= memory_data_size) {
  1718. return true;
  1719. }
  1720. fail:
  1721. wasm_set_exception(module_inst, "out of bounds memory access");
  1722. return false;
  1723. }
  1724. bool
  1725. wasm_validate_native_addr(WASMModuleInstance *module_inst,
  1726. void *native_ptr, uint32 size)
  1727. {
  1728. WASMMemoryInstance *memory = module_inst->default_memory;
  1729. uint8 *addr = (uint8 *)native_ptr;
  1730. if (!memory) {
  1731. goto fail;
  1732. }
  1733. /* integer overflow check */
  1734. if ((uintptr_t)addr > UINTPTR_MAX - size) {
  1735. goto fail;
  1736. }
  1737. if (memory->memory_data <= addr
  1738. && addr + size <= memory->memory_data_end) {
  1739. return true;
  1740. }
  1741. fail:
  1742. wasm_set_exception(module_inst, "out of bounds memory access");
  1743. return false;
  1744. }
  1745. void *
  1746. wasm_addr_app_to_native(WASMModuleInstance *module_inst,
  1747. uint32 app_offset)
  1748. {
  1749. WASMMemoryInstance *memory = module_inst->default_memory;
  1750. uint8 *addr;
  1751. if (!memory)
  1752. return NULL;
  1753. addr = memory->memory_data + app_offset;
  1754. if (memory->memory_data <= addr
  1755. && addr < memory->memory_data_end)
  1756. return addr;
  1757. return NULL;
  1758. }
  1759. uint32
  1760. wasm_addr_native_to_app(WASMModuleInstance *module_inst,
  1761. void *native_ptr)
  1762. {
  1763. WASMMemoryInstance *memory = module_inst->default_memory;
  1764. uint8 *addr = (uint8 *)native_ptr;
  1765. if (!memory)
  1766. return 0;
  1767. if (memory->memory_data <= addr
  1768. && addr < memory->memory_data_end)
  1769. return (uint32)(addr - memory->memory_data);
  1770. return 0;
  1771. }
  1772. bool
  1773. wasm_get_app_addr_range(WASMModuleInstance *module_inst,
  1774. uint32 app_offset,
  1775. uint32 *p_app_start_offset,
  1776. uint32 *p_app_end_offset)
  1777. {
  1778. WASMMemoryInstance *memory = module_inst->default_memory;
  1779. uint32 memory_data_size;
  1780. if (!memory)
  1781. return false;
  1782. memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
  1783. if (app_offset < memory_data_size) {
  1784. if (p_app_start_offset)
  1785. *p_app_start_offset = 0;
  1786. if (p_app_end_offset)
  1787. *p_app_end_offset = memory_data_size;
  1788. return true;
  1789. }
  1790. return false;
  1791. }
  1792. bool
  1793. wasm_get_native_addr_range(WASMModuleInstance *module_inst,
  1794. uint8 *native_ptr,
  1795. uint8 **p_native_start_addr,
  1796. uint8 **p_native_end_addr)
  1797. {
  1798. WASMMemoryInstance *memory = module_inst->default_memory;
  1799. uint8 *addr = (uint8 *)native_ptr;
  1800. if (!memory)
  1801. return false;
  1802. if (memory->memory_data <= addr
  1803. && addr < memory->memory_data_end) {
  1804. if (p_native_start_addr)
  1805. *p_native_start_addr = memory->memory_data;
  1806. if (p_native_end_addr)
  1807. *p_native_end_addr = memory->memory_data_end;
  1808. return true;
  1809. }
  1810. return false;
  1811. }
  1812. bool
  1813. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  1814. {
  1815. WASMMemoryInstance *memory = module->default_memory;
  1816. uint8 *new_memory_data, *memory_data, *heap_data_old;
  1817. uint32 heap_size, total_size_old, total_page_count;
  1818. uint64 total_size;
  1819. if (!memory)
  1820. return false;
  1821. memory_data = memory->memory_data;
  1822. heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
  1823. total_size_old = (uint32)(memory->memory_data_end - memory_data);
  1824. total_page_count = inc_page_count + memory->cur_page_count;
  1825. total_size = memory->num_bytes_per_page * (uint64)total_page_count;
  1826. heap_data_old = memory->heap_data;
  1827. if (inc_page_count <= 0)
  1828. /* No need to enlarge memory */
  1829. return true;
  1830. if (total_page_count < memory->cur_page_count /* integer overflow */
  1831. || total_page_count > memory->max_page_count) {
  1832. return false;
  1833. }
  1834. if (total_size >= UINT32_MAX) {
  1835. return false;
  1836. }
  1837. #if WASM_ENABLE_SHARED_MEMORY != 0
  1838. if (memory->is_shared) {
  1839. /* For shared memory, we have reserved the maximum spaces during
  1840. instantiate, only change the cur_page_count here */
  1841. memory->cur_page_count = total_page_count;
  1842. return true;
  1843. }
  1844. #endif
  1845. if (!(new_memory_data = wasm_runtime_realloc(memory_data, (uint32)total_size))) {
  1846. if (!(new_memory_data = wasm_runtime_malloc((uint32)total_size))) {
  1847. return false;
  1848. }
  1849. if (memory_data) {
  1850. bh_memcpy_s(new_memory_data, (uint32)total_size,
  1851. memory_data, total_size_old);
  1852. wasm_runtime_free(memory_data);
  1853. }
  1854. }
  1855. memset(new_memory_data + total_size_old,
  1856. 0, (uint32)total_size - total_size_old);
  1857. if (heap_size > 0) {
  1858. if (mem_allocator_migrate(memory->heap_handle,
  1859. (char *)heap_data_old
  1860. + (new_memory_data - memory_data),
  1861. heap_size) != 0) {
  1862. return false;
  1863. }
  1864. }
  1865. memory->memory_data = new_memory_data;
  1866. memory->cur_page_count = total_page_count;
  1867. memory->heap_data = heap_data_old + (new_memory_data - memory_data);
  1868. memory->heap_data_end = memory->heap_data + heap_size;
  1869. memory->memory_data_end = memory->memory_data
  1870. + memory->num_bytes_per_page
  1871. * total_page_count;
  1872. return true;
  1873. }
  1874. #if WASM_ENABLE_REF_TYPES != 0
  1875. bool
  1876. wasm_enlarge_table(WASMModuleInstance *module_inst,
  1877. uint32 table_idx, uint32 inc_entries, uint32 init_val)
  1878. {
  1879. uint32 entry_count, *new_table_data_start, i;
  1880. WASMTableInstance *table_inst;
  1881. if (!inc_entries) {
  1882. return true;
  1883. }
  1884. bh_assert(table_idx < module_inst->table_count);
  1885. table_inst = wasm_get_table_inst(module_inst, table_idx);
  1886. if (!table_inst) {
  1887. return false;
  1888. }
  1889. entry_count = table_inst->cur_size + inc_entries;
  1890. /* prevent from integer overflow */
  1891. if (entry_count < table_inst->cur_size
  1892. || entry_count > table_inst->max_size) {
  1893. return false;
  1894. }
  1895. /* fill in */
  1896. new_table_data_start =
  1897. (uint32 *)((uint8 *)table_inst + offsetof(WASMTableInstance, base_addr))
  1898. + table_inst->cur_size;
  1899. for (i = 0; i < inc_entries; ++i) {
  1900. new_table_data_start[i] = init_val;
  1901. }
  1902. table_inst->cur_size = entry_count;
  1903. return true;
  1904. }
  1905. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  1906. bool
  1907. wasm_call_indirect(WASMExecEnv *exec_env,
  1908. uint32_t tbl_idx,
  1909. uint32_t element_indices,
  1910. uint32_t argc, uint32_t argv[])
  1911. {
  1912. WASMModuleInstance *module_inst = NULL;
  1913. WASMTableInstance *table_inst = NULL;
  1914. uint32_t function_indices = 0;
  1915. WASMFunctionInstance *function_inst = NULL;
  1916. module_inst =
  1917. (WASMModuleInstance*)exec_env->module_inst;
  1918. bh_assert(module_inst);
  1919. table_inst = module_inst->tables[tbl_idx];
  1920. if (!table_inst) {
  1921. wasm_set_exception(module_inst, "unknown table");
  1922. goto got_exception;
  1923. }
  1924. if (element_indices >= table_inst->cur_size) {
  1925. wasm_set_exception(module_inst, "undefined element");
  1926. goto got_exception;
  1927. }
  1928. /**
  1929. * please be aware that table_inst->base_addr may point
  1930. * to another module's table
  1931. **/
  1932. function_indices = ((uint32_t*)table_inst->base_addr)[element_indices];
  1933. if (function_indices == NULL_REF) {
  1934. wasm_set_exception(module_inst, "uninitialized element");
  1935. goto got_exception;
  1936. }
  1937. /**
  1938. * we insist to call functions owned by the module itself
  1939. **/
  1940. if (function_indices >= module_inst->function_count) {
  1941. wasm_set_exception(module_inst, "unknown function");
  1942. goto got_exception;
  1943. }
  1944. function_inst = module_inst->functions + function_indices;
  1945. wasm_interp_call_wasm(module_inst, exec_env, function_inst, argc, argv);
  1946. (void)clear_wasi_proc_exit_exception(module_inst);
  1947. return !wasm_get_exception(module_inst) ? true : false;
  1948. got_exception:
  1949. return false;
  1950. }
  1951. #if WASM_ENABLE_THREAD_MGR != 0
  1952. bool
  1953. wasm_set_aux_stack(WASMExecEnv *exec_env,
  1954. uint32 start_offset, uint32 size)
  1955. {
  1956. WASMModuleInstance *module_inst =
  1957. (WASMModuleInstance*)exec_env->module_inst;
  1958. uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
  1959. uint32 data_end = module_inst->module->aux_data_end;
  1960. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  1961. bool is_stack_before_data =
  1962. stack_bottom < data_end ? true : false;
  1963. /* Check the aux stack space, currently we don't allocate space in heap */
  1964. if ((is_stack_before_data && (size > start_offset))
  1965. || ((!is_stack_before_data) && (start_offset - data_end < size)))
  1966. return false;
  1967. if (stack_top_idx != (uint32)-1) {
  1968. /* The aux stack top is a wasm global,
  1969. set the initial value for the global */
  1970. uint8 *global_addr =
  1971. module_inst->global_data +
  1972. module_inst->globals[stack_top_idx].data_offset;
  1973. *(int32*)global_addr = start_offset;
  1974. /* The aux stack boundary is a constant value,
  1975. set the value to exec_env */
  1976. exec_env->aux_stack_boundary.boundary = start_offset - size;
  1977. exec_env->aux_stack_bottom.bottom = start_offset;
  1978. return true;
  1979. }
  1980. return false;
  1981. }
  1982. bool
  1983. wasm_get_aux_stack(WASMExecEnv *exec_env,
  1984. uint32 *start_offset, uint32 *size)
  1985. {
  1986. WASMModuleInstance *module_inst =
  1987. (WASMModuleInstance*)exec_env->module_inst;
  1988. /* The aux stack information is resolved in loader
  1989. and store in module */
  1990. uint32 stack_bottom =
  1991. module_inst->module->aux_stack_bottom;
  1992. uint32 total_aux_stack_size =
  1993. module_inst->module->aux_stack_size;
  1994. if (stack_bottom != 0 && total_aux_stack_size != 0) {
  1995. if (start_offset)
  1996. *start_offset = stack_bottom;
  1997. if (size)
  1998. *size = total_aux_stack_size;
  1999. return true;
  2000. }
  2001. return false;
  2002. }
  2003. #endif
  2004. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  2005. void
  2006. wasm_get_module_mem_consumption(const WASMModule *module,
  2007. WASMModuleMemConsumption *mem_conspn)
  2008. {
  2009. uint32 i, size;
  2010. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2011. mem_conspn->module_struct_size = sizeof(WASMModule);
  2012. mem_conspn->types_size = sizeof(WASMType *) * module->type_count;
  2013. for (i = 0; i < module->type_count; i++) {
  2014. WASMType *type = module->types[i];
  2015. size = offsetof(WASMType, types) +
  2016. sizeof(uint8) * (type->param_count + type->result_count);
  2017. mem_conspn->types_size += size;
  2018. }
  2019. mem_conspn->imports_size = sizeof(WASMImport) * module->import_count;
  2020. mem_conspn->functions_size = sizeof(WASMFunction *)
  2021. * module->function_count;
  2022. for (i = 0; i < module->function_count; i++) {
  2023. WASMFunction *func = module->functions[i];
  2024. WASMType *type = func->func_type;
  2025. size = sizeof(WASMFunction) + func->local_count
  2026. + sizeof(uint16) * (type->param_count + func->local_count);
  2027. #if WASM_ENABLE_FAST_INTERP != 0
  2028. size += func->code_compiled_size
  2029. + sizeof(uint32) * func->const_cell_num;
  2030. #endif
  2031. mem_conspn->functions_size += size;
  2032. }
  2033. mem_conspn->tables_size = sizeof(WASMTable) * module->table_count;
  2034. mem_conspn->memories_size = sizeof(WASMMemory) * module->memory_count;
  2035. mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count;
  2036. mem_conspn->exports_size = sizeof(WASMExport) * module->export_count;
  2037. mem_conspn->table_segs_size = sizeof(WASMTableSeg)
  2038. * module->table_seg_count;
  2039. for (i = 0; i < module->table_seg_count; i++) {
  2040. WASMTableSeg *table_seg = &module->table_segments[i];
  2041. mem_conspn->tables_size += sizeof(uint32)
  2042. * table_seg->function_count;
  2043. }
  2044. mem_conspn->data_segs_size = sizeof(WASMDataSeg*)
  2045. * module->data_seg_count;
  2046. for (i = 0; i < module->data_seg_count; i++) {
  2047. mem_conspn->data_segs_size += sizeof(WASMDataSeg);
  2048. }
  2049. if (module->const_str_list) {
  2050. StringNode *node = module->const_str_list, *node_next;
  2051. while (node) {
  2052. node_next = node->next;
  2053. mem_conspn->const_strs_size += sizeof(StringNode)
  2054. + strlen(node->str) + 1;
  2055. node = node_next;
  2056. }
  2057. }
  2058. mem_conspn->total_size += mem_conspn->module_struct_size;
  2059. mem_conspn->total_size += mem_conspn->types_size;
  2060. mem_conspn->total_size += mem_conspn->imports_size;
  2061. mem_conspn->total_size += mem_conspn->functions_size;
  2062. mem_conspn->total_size += mem_conspn->tables_size;
  2063. mem_conspn->total_size += mem_conspn->memories_size;
  2064. mem_conspn->total_size += mem_conspn->globals_size;
  2065. mem_conspn->total_size += mem_conspn->exports_size;
  2066. mem_conspn->total_size += mem_conspn->table_segs_size;
  2067. mem_conspn->total_size += mem_conspn->data_segs_size;
  2068. mem_conspn->total_size += mem_conspn->const_strs_size;
  2069. #if WASM_ENABLE_AOT != 0
  2070. mem_conspn->total_size += mem_conspn->aot_code_size;
  2071. #endif
  2072. }
  2073. void
  2074. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
  2075. WASMModuleInstMemConsumption *mem_conspn)
  2076. {
  2077. uint32 i, size;
  2078. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2079. mem_conspn->module_inst_struct_size = sizeof(WASMModuleInstance);
  2080. mem_conspn->memories_size = sizeof(WASMMemoryInstance *)
  2081. * module_inst->memory_count;
  2082. for (i = 0; i < module_inst->memory_count; i++) {
  2083. WASMMemoryInstance *memory = module_inst->memories[i];
  2084. size = sizeof(WASMMemoryInstance)
  2085. + memory->num_bytes_per_page * memory->cur_page_count;
  2086. mem_conspn->memories_size += size;
  2087. mem_conspn->app_heap_size += memory->heap_data_end
  2088. - memory->heap_data;
  2089. /* size of app heap structure */
  2090. mem_conspn->memories_size +=
  2091. mem_allocator_get_heap_struct_size();
  2092. }
  2093. mem_conspn->tables_size = sizeof(WASMTableInstance *)
  2094. * module_inst->table_count;
  2095. for (i = 0; i < module_inst->table_count; i++) {
  2096. WASMTableInstance *table = module_inst->tables[i];
  2097. #if WASM_ENABLE_MULTI_MODULE != 0
  2098. if (table->table_inst_linked) {
  2099. size = offsetof(WASMTableInstance, base_addr);
  2100. }
  2101. else
  2102. #endif
  2103. {
  2104. size = offsetof(WASMTableInstance, base_addr)
  2105. + sizeof(uint32) * table->cur_size;
  2106. }
  2107. mem_conspn->tables_size += size;
  2108. }
  2109. mem_conspn->functions_size = sizeof(WASMFunctionInstance)
  2110. * module_inst->function_count;
  2111. mem_conspn->globals_size = sizeof(WASMGlobalInstance)
  2112. * module_inst->global_count;
  2113. if (module_inst->global_count > 0) {
  2114. WASMGlobalInstance *global =
  2115. &module_inst->globals[module_inst->global_count - 1];
  2116. mem_conspn->globals_size += global->data_offset
  2117. + wasm_value_type_size(global->type);
  2118. }
  2119. mem_conspn->exports_size = sizeof(WASMExportFuncInstance)
  2120. * module_inst->export_func_count;
  2121. mem_conspn->total_size += mem_conspn->module_inst_struct_size;
  2122. mem_conspn->total_size += mem_conspn->memories_size;
  2123. mem_conspn->total_size += mem_conspn->functions_size;
  2124. mem_conspn->total_size += mem_conspn->tables_size;
  2125. mem_conspn->total_size += mem_conspn->globals_size;
  2126. mem_conspn->total_size += mem_conspn->exports_size;
  2127. }
  2128. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0)
  2129. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  2130. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2131. void
  2132. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
  2133. {
  2134. WASMModuleInstance *module_inst =
  2135. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  2136. WASMInterpFrame *first_frame,
  2137. *cur_frame = wasm_exec_env_get_cur_frame(exec_env);
  2138. uint32 n = 0;
  2139. /* count frames includes a function */
  2140. first_frame = cur_frame;
  2141. while (cur_frame) {
  2142. if (cur_frame->function) {
  2143. n++;
  2144. }
  2145. cur_frame = cur_frame->prev_frame;
  2146. }
  2147. /* release previous stack frames and create new ones */
  2148. if (!bh_vector_destroy(module_inst->frames)
  2149. || !bh_vector_init(module_inst->frames, n, sizeof(WASMCApiFrame))) {
  2150. return;
  2151. }
  2152. cur_frame = first_frame;
  2153. n = 0;
  2154. os_printf("\n");
  2155. while (cur_frame) {
  2156. WASMCApiFrame frame = { 0 };
  2157. WASMFunctionInstance *func_inst = cur_frame->function;
  2158. const char *func_name = NULL;
  2159. if (!func_inst) {
  2160. cur_frame = cur_frame->prev_frame;
  2161. continue;
  2162. }
  2163. /* place holder, will overwrite it in wasm_c_api */
  2164. frame.instance = module_inst;
  2165. frame.module_offset = 0;
  2166. frame.func_index = func_inst - module_inst->functions;
  2167. frame.func_offset =
  2168. cur_frame->ip ? cur_frame->ip - func_inst->u.func->code : 0;
  2169. /* look for the function name */
  2170. if (func_inst->is_import_func) {
  2171. func_name = func_inst->u.func_import->field_name;
  2172. }
  2173. else {
  2174. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2175. func_name = func_inst->u.func->field_name;
  2176. #endif
  2177. /* if custom name section is not generated,
  2178. search symbols from export table */
  2179. if (!func_name) {
  2180. uint32 i;
  2181. for (i = 0; i < module_inst->export_func_count; i++) {
  2182. WASMExportFuncInstance *export_func =
  2183. module_inst->export_functions + i;
  2184. if (export_func->function == func_inst) {
  2185. func_name = export_func->name;
  2186. break;
  2187. }
  2188. }
  2189. }
  2190. }
  2191. /* function name not exported, print number instead */
  2192. if (func_name == NULL) {
  2193. os_printf("#%02d $f%d \n", n, func_inst - module_inst->functions);
  2194. }
  2195. else {
  2196. os_printf("#%02d %s \n", n, func_name);
  2197. }
  2198. /* keep print */
  2199. bh_vector_append(module_inst->frames, &frame);
  2200. cur_frame = cur_frame->prev_frame;
  2201. n++;
  2202. }
  2203. os_printf("\n");
  2204. }
  2205. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */