wasm_runtime.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493
  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. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_SHARED_MEMORY != 0
  14. #include "../common/wasm_shared_memory.h"
  15. #endif
  16. #if WASM_ENABLE_THREAD_MGR != 0
  17. #include "../libraries/thread-mgr/thread_manager.h"
  18. #endif
  19. #if WASM_ENABLE_DEBUG_INTERP != 0
  20. #include "../libraries/debug-engine/debug_engine.h"
  21. #endif
  22. #if WASM_ENABLE_FAST_JIT != 0
  23. #include "../fast-jit/jit_compiler.h"
  24. #endif
  25. #if WASM_ENABLE_JIT != 0
  26. #include "../aot/aot_runtime.h"
  27. #endif
  28. static void
  29. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  30. {
  31. if (error_buf != NULL) {
  32. snprintf(error_buf, error_buf_size,
  33. "WASM module instantiate failed: %s", string);
  34. }
  35. }
  36. static void
  37. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  38. {
  39. va_list args;
  40. char buf[128];
  41. if (error_buf != NULL) {
  42. va_start(args, format);
  43. vsnprintf(buf, sizeof(buf), format, args);
  44. va_end(args);
  45. snprintf(error_buf, error_buf_size,
  46. "WASM module instantiate failed: %s", buf);
  47. }
  48. }
  49. WASMModule *
  50. wasm_load(uint8 *buf, uint32 size,
  51. #if WASM_ENABLE_MULTI_MODULE != 0
  52. bool main_module,
  53. #endif
  54. char *error_buf, uint32 error_buf_size)
  55. {
  56. return wasm_loader_load(buf, size,
  57. #if WASM_ENABLE_MULTI_MODULE != 0
  58. main_module,
  59. #endif
  60. error_buf, error_buf_size);
  61. }
  62. WASMModule *
  63. wasm_load_from_sections(WASMSection *section_list, char *error_buf,
  64. uint32 error_buf_size)
  65. {
  66. return wasm_loader_load_from_sections(section_list, error_buf,
  67. error_buf_size);
  68. }
  69. void
  70. wasm_unload(WASMModule *module)
  71. {
  72. wasm_loader_unload(module);
  73. }
  74. static void *
  75. runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  76. {
  77. void *mem;
  78. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  79. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  80. return NULL;
  81. }
  82. memset(mem, 0, (uint32)size);
  83. return mem;
  84. }
  85. #if WASM_ENABLE_MULTI_MODULE != 0
  86. static WASMModuleInstance *
  87. get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
  88. const WASMModule *sub_module)
  89. {
  90. bh_list *sub_module_inst_list = parent_module_inst->e->sub_module_inst_list;
  91. WASMSubModInstNode *node = bh_list_first_elem(sub_module_inst_list);
  92. while (node && sub_module != node->module_inst->module) {
  93. node = bh_list_elem_next(node);
  94. }
  95. return node ? node->module_inst : NULL;
  96. }
  97. #endif
  98. /**
  99. * Destroy memory instances.
  100. */
  101. static void
  102. memories_deinstantiate(WASMModuleInstance *module_inst,
  103. WASMMemoryInstance **memories, uint32 count)
  104. {
  105. #ifdef WASM_LINEAR_MEMORY_MMAP
  106. uint64 map_size;
  107. #endif
  108. uint32 i;
  109. if (memories) {
  110. for (i = 0; i < count; i++) {
  111. if (memories[i]) {
  112. #if WASM_ENABLE_MULTI_MODULE != 0
  113. WASMModule *module = module_inst->module;
  114. if (i < module->import_memory_count
  115. && module->import_memories[i].u.memory.import_module) {
  116. continue;
  117. }
  118. #endif
  119. #if WASM_ENABLE_SHARED_MEMORY != 0
  120. if (shared_memory_is_shared(memories[i])) {
  121. uint32 ref_count = shared_memory_dec_reference(memories[i]);
  122. /* if the reference count is not zero,
  123. don't free the memory */
  124. if (ref_count > 0)
  125. continue;
  126. }
  127. #endif
  128. if (memories[i]->heap_handle) {
  129. mem_allocator_destroy(memories[i]->heap_handle);
  130. wasm_runtime_free(memories[i]->heap_handle);
  131. memories[i]->heap_handle = NULL;
  132. }
  133. if (memories[i]->memory_data) {
  134. #ifndef OS_ENABLE_HW_BOUND_CHECK
  135. #ifdef WASM_LINEAR_MEMORY_MMAP
  136. if (shared_memory_is_shared(memories[i])) {
  137. map_size = (uint64)memories[i]->num_bytes_per_page
  138. * memories[i]->max_page_count;
  139. wasm_munmap_linear_memory(memories[i]->memory_data,
  140. map_size, map_size);
  141. }
  142. else
  143. #endif
  144. wasm_runtime_free(memories[i]->memory_data);
  145. #else
  146. map_size = (uint64)memories[i]->num_bytes_per_page
  147. * memories[i]->cur_page_count;
  148. wasm_munmap_linear_memory(memories[i]->memory_data,
  149. map_size, 8 * (uint64)BH_GB);
  150. #endif
  151. }
  152. }
  153. }
  154. wasm_runtime_free(memories);
  155. }
  156. (void)module_inst;
  157. }
  158. static WASMMemoryInstance *
  159. memory_instantiate(WASMModuleInstance *module_inst, WASMModuleInstance *parent,
  160. WASMMemoryInstance *memory, uint32 memory_idx,
  161. uint32 num_bytes_per_page, uint32 init_page_count,
  162. uint32 max_page_count, uint32 heap_size, uint32 flags,
  163. char *error_buf, uint32 error_buf_size)
  164. {
  165. WASMModule *module = module_inst->module;
  166. uint64 memory_data_size, max_memory_data_size;
  167. uint32 heap_offset = num_bytes_per_page * init_page_count;
  168. uint32 inc_page_count, aux_heap_base, global_idx;
  169. uint32 bytes_of_last_page, bytes_to_page_end;
  170. uint8 *global_addr;
  171. #ifdef WASM_LINEAR_MEMORY_MMAP
  172. uint8 *mapped_mem = NULL;
  173. uint64 map_size;
  174. #endif
  175. #if WASM_ENABLE_SHARED_MEMORY != 0
  176. bool is_shared_memory = flags & 0x02 ? true : false;
  177. /* shared memory */
  178. if (is_shared_memory && parent != NULL) {
  179. bh_assert(parent->memory_count > memory_idx);
  180. memory = parent->memories[memory_idx];
  181. shared_memory_inc_reference(memory);
  182. return memory;
  183. }
  184. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  185. if (heap_size > 0 && module_inst->module->malloc_function != (uint32)-1
  186. && module_inst->module->free_function != (uint32)-1) {
  187. /* Disable app heap, use malloc/free function exported
  188. by wasm app to allocate/free memory instead */
  189. heap_size = 0;
  190. }
  191. if (init_page_count == max_page_count && init_page_count == 1) {
  192. /* If only one page and at most one page, we just append
  193. the app heap to the end of linear memory, enlarge the
  194. num_bytes_per_page, and don't change the page count */
  195. heap_offset = num_bytes_per_page;
  196. num_bytes_per_page += heap_size;
  197. if (num_bytes_per_page < heap_size) {
  198. set_error_buf(error_buf, error_buf_size,
  199. "failed to insert app heap into linear memory, "
  200. "try using `--heap-size=0` option");
  201. return NULL;
  202. }
  203. }
  204. else if (heap_size > 0) {
  205. if (init_page_count == max_page_count && init_page_count == 0) {
  206. /* If the memory data size is always 0, we resize it to
  207. one page for app heap */
  208. num_bytes_per_page = heap_size;
  209. heap_offset = 0;
  210. inc_page_count = 1;
  211. }
  212. else if (module->aux_heap_base_global_index != (uint32)-1
  213. && module->aux_heap_base
  214. < num_bytes_per_page * init_page_count) {
  215. /* Insert app heap before __heap_base */
  216. aux_heap_base = module->aux_heap_base;
  217. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  218. if (bytes_of_last_page == 0)
  219. bytes_of_last_page = num_bytes_per_page;
  220. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  221. inc_page_count =
  222. (heap_size - bytes_to_page_end + num_bytes_per_page - 1)
  223. / num_bytes_per_page;
  224. heap_offset = aux_heap_base;
  225. aux_heap_base += heap_size;
  226. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  227. if (bytes_of_last_page == 0)
  228. bytes_of_last_page = num_bytes_per_page;
  229. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  230. if (bytes_to_page_end < 1 * BH_KB) {
  231. aux_heap_base += 1 * BH_KB;
  232. inc_page_count++;
  233. }
  234. /* Adjust __heap_base global value */
  235. global_idx = module->aux_heap_base_global_index;
  236. bh_assert(module_inst->e->globals
  237. && global_idx < module_inst->e->global_count);
  238. global_addr = module_inst->global_data
  239. + module_inst->e->globals[global_idx].data_offset;
  240. *(uint32 *)global_addr = aux_heap_base;
  241. LOG_VERBOSE("Reset __heap_base global to %u", aux_heap_base);
  242. }
  243. else {
  244. /* Insert app heap before new page */
  245. inc_page_count =
  246. (heap_size + num_bytes_per_page - 1) / num_bytes_per_page;
  247. heap_offset = num_bytes_per_page * init_page_count;
  248. heap_size = num_bytes_per_page * inc_page_count;
  249. if (heap_size > 0)
  250. heap_size -= 1 * BH_KB;
  251. }
  252. init_page_count += inc_page_count;
  253. max_page_count += inc_page_count;
  254. if (init_page_count > DEFAULT_MAX_PAGES) {
  255. set_error_buf(error_buf, error_buf_size,
  256. "failed to insert app heap into linear memory, "
  257. "try using `--heap-size=0` option");
  258. return NULL;
  259. }
  260. else if (init_page_count == DEFAULT_MAX_PAGES) {
  261. num_bytes_per_page = UINT32_MAX;
  262. init_page_count = max_page_count = 1;
  263. }
  264. if (max_page_count > DEFAULT_MAX_PAGES)
  265. max_page_count = DEFAULT_MAX_PAGES;
  266. }
  267. else { /* heap_size == 0 */
  268. if (init_page_count == DEFAULT_MAX_PAGES) {
  269. num_bytes_per_page = UINT32_MAX;
  270. init_page_count = max_page_count = 1;
  271. }
  272. }
  273. LOG_VERBOSE("Memory instantiate:");
  274. LOG_VERBOSE(" page bytes: %u, init pages: %u, max pages: %u",
  275. num_bytes_per_page, init_page_count, max_page_count);
  276. LOG_VERBOSE(" heap offset: %u, heap size: %d\n", heap_offset, heap_size);
  277. memory_data_size = (uint64)num_bytes_per_page * init_page_count;
  278. max_memory_data_size = (uint64)num_bytes_per_page * max_page_count;
  279. bh_assert(memory_data_size <= UINT32_MAX);
  280. bh_assert(max_memory_data_size <= 4 * (uint64)BH_GB);
  281. (void)max_memory_data_size;
  282. bh_assert(memory != NULL);
  283. #ifndef OS_ENABLE_HW_BOUND_CHECK
  284. #if WASM_ENABLE_SHARED_MEMORY != 0
  285. if (is_shared_memory) {
  286. /* Allocate maximum memory size when memory is shared */
  287. #if WASM_ENABLE_SHARED_MEMORY_MMAP != 0
  288. map_size = max_memory_data_size;
  289. if (max_memory_data_size > 0
  290. && !(memory->memory_data = mapped_mem =
  291. wasm_mmap_linear_memory(map_size, &max_memory_data_size,
  292. error_buf, error_buf_size))) {
  293. goto fail1;
  294. }
  295. #else
  296. if (max_memory_data_size > 0
  297. && !(memory->memory_data = runtime_malloc(
  298. max_memory_data_size, error_buf, error_buf_size))) {
  299. goto fail1;
  300. }
  301. #endif
  302. }
  303. else
  304. #endif /* end of WASM_ENABLE_SHARED_MEMORY != 0 */
  305. {
  306. /* Allocate initial memory size when memory is not shared */
  307. if (memory_data_size > 0
  308. && !(memory->memory_data = runtime_malloc(
  309. memory_data_size, error_buf, error_buf_size))) {
  310. goto fail1;
  311. }
  312. }
  313. #else /* else of OS_ENABLE_HW_BOUND_CHECK */
  314. /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
  315. * ea = i + memarg.offset
  316. * both i and memarg.offset are u32 in range 0 to 4G
  317. * so the range of ea is 0 to 8G
  318. */
  319. map_size = 8 * (uint64)BH_GB;
  320. if (!(memory->memory_data = mapped_mem = wasm_mmap_linear_memory(
  321. map_size, &memory_data_size, error_buf, error_buf_size))) {
  322. set_error_buf(error_buf, error_buf_size, "mmap memory failed");
  323. goto fail1;
  324. }
  325. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  326. memory->module_type = Wasm_Module_Bytecode;
  327. memory->num_bytes_per_page = num_bytes_per_page;
  328. memory->cur_page_count = init_page_count;
  329. memory->max_page_count = max_page_count;
  330. memory->memory_data_size = (uint32)memory_data_size;
  331. memory->heap_data = memory->memory_data + heap_offset;
  332. memory->heap_data_end = memory->heap_data + heap_size;
  333. memory->memory_data_end = memory->memory_data + (uint32)memory_data_size;
  334. /* Initialize heap */
  335. if (heap_size > 0) {
  336. uint32 heap_struct_size = mem_allocator_get_heap_struct_size();
  337. if (!(memory->heap_handle = runtime_malloc(
  338. (uint64)heap_struct_size, error_buf, error_buf_size))) {
  339. goto fail2;
  340. }
  341. if (!mem_allocator_create_with_struct_and_pool(
  342. memory->heap_handle, heap_struct_size, memory->heap_data,
  343. heap_size)) {
  344. set_error_buf(error_buf, error_buf_size, "init app heap failed");
  345. goto fail3;
  346. }
  347. }
  348. if (memory_data_size > 0) {
  349. wasm_runtime_set_mem_bound_check_bytes(memory, memory_data_size);
  350. }
  351. #if WASM_ENABLE_SHARED_MEMORY != 0
  352. if (is_shared_memory) {
  353. memory->is_shared_memory = 1;
  354. memory->ref_count = 1;
  355. }
  356. #endif
  357. LOG_VERBOSE("Memory instantiate success.");
  358. return memory;
  359. fail3:
  360. if (heap_size > 0)
  361. wasm_runtime_free(memory->heap_handle);
  362. fail2:
  363. #ifdef WASM_LINEAR_MEMORY_MMAP
  364. if (mapped_mem)
  365. wasm_munmap_linear_memory(mapped_mem, memory_data_size, map_size);
  366. else
  367. #endif
  368. {
  369. if (memory->memory_data)
  370. wasm_runtime_free(memory->memory_data);
  371. }
  372. fail1:
  373. return NULL;
  374. }
  375. /**
  376. * Instantiate memories in a module.
  377. */
  378. static WASMMemoryInstance **
  379. memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  380. WASMModuleInstance *parent, uint32 heap_size,
  381. char *error_buf, uint32 error_buf_size)
  382. {
  383. WASMImport *import;
  384. uint32 mem_index = 0, i,
  385. memory_count = module->import_memory_count + module->memory_count;
  386. uint64 total_size;
  387. WASMMemoryInstance **memories, *memory;
  388. total_size = sizeof(WASMMemoryInstance *) * (uint64)memory_count;
  389. if (!(memories = runtime_malloc(total_size, error_buf, error_buf_size))) {
  390. return NULL;
  391. }
  392. memory = module_inst->global_table_data.memory_instances;
  393. /* instantiate memories from import section */
  394. import = module->import_memories;
  395. for (i = 0; i < module->import_memory_count; i++, import++, memory++) {
  396. uint32 num_bytes_per_page = import->u.memory.num_bytes_per_page;
  397. uint32 init_page_count = import->u.memory.init_page_count;
  398. uint32 max_page_count = import->u.memory.max_page_count;
  399. uint32 flags = import->u.memory.flags;
  400. uint32 actual_heap_size = heap_size;
  401. #if WASM_ENABLE_MULTI_MODULE != 0
  402. if (import->u.memory.import_module != NULL) {
  403. WASMModuleInstance *module_inst_linked;
  404. if (!(module_inst_linked = get_sub_module_inst(
  405. module_inst, import->u.memory.import_module))) {
  406. set_error_buf(error_buf, error_buf_size, "unknown memory");
  407. memories_deinstantiate(module_inst, memories, memory_count);
  408. return NULL;
  409. }
  410. if (!(memories[mem_index++] = wasm_lookup_memory(
  411. module_inst_linked, import->u.memory.field_name))) {
  412. set_error_buf(error_buf, error_buf_size, "unknown memory");
  413. memories_deinstantiate(module_inst, memories, memory_count);
  414. return NULL;
  415. }
  416. }
  417. else
  418. #endif
  419. {
  420. if (!(memories[mem_index] = memory_instantiate(
  421. module_inst, parent, memory, mem_index,
  422. num_bytes_per_page, init_page_count, max_page_count,
  423. actual_heap_size, flags, error_buf, error_buf_size))) {
  424. memories_deinstantiate(module_inst, memories, memory_count);
  425. return NULL;
  426. }
  427. mem_index++;
  428. }
  429. }
  430. /* instantiate memories from memory section */
  431. for (i = 0; i < module->memory_count; i++, memory++) {
  432. if (!(memories[mem_index] = memory_instantiate(
  433. module_inst, parent, memory, mem_index,
  434. module->memories[i].num_bytes_per_page,
  435. module->memories[i].init_page_count,
  436. module->memories[i].max_page_count, heap_size,
  437. module->memories[i].flags, error_buf, error_buf_size))) {
  438. memories_deinstantiate(module_inst, memories, memory_count);
  439. return NULL;
  440. }
  441. mem_index++;
  442. }
  443. bh_assert(mem_index == memory_count);
  444. (void)module_inst;
  445. return memories;
  446. }
  447. /**
  448. * Destroy table instances.
  449. */
  450. static void
  451. tables_deinstantiate(WASMModuleInstance *module_inst)
  452. {
  453. if (module_inst->tables) {
  454. wasm_runtime_free(module_inst->tables);
  455. }
  456. #if WASM_ENABLE_MULTI_MODULE != 0
  457. if (module_inst->e->table_insts_linked) {
  458. wasm_runtime_free(module_inst->e->table_insts_linked);
  459. }
  460. #endif
  461. }
  462. /**
  463. * Instantiate tables in a module.
  464. */
  465. static WASMTableInstance **
  466. tables_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  467. WASMTableInstance *first_table, char *error_buf,
  468. uint32 error_buf_size)
  469. {
  470. WASMImport *import;
  471. uint32 table_index = 0, i;
  472. uint32 table_count = module->import_table_count + module->table_count;
  473. uint64 total_size = (uint64)sizeof(WASMTableInstance *) * table_count;
  474. WASMTableInstance **tables, *table = first_table;
  475. #if WASM_ENABLE_MULTI_MODULE != 0
  476. uint64 total_size_of_tables_linked =
  477. (uint64)sizeof(WASMTableInstance *) * module->import_table_count;
  478. WASMTableInstance **table_linked = NULL;
  479. #endif
  480. if (!(tables = runtime_malloc(total_size, error_buf, error_buf_size))) {
  481. return NULL;
  482. }
  483. #if WASM_ENABLE_MULTI_MODULE != 0
  484. if (module->import_table_count > 0
  485. && !(module_inst->e->table_insts_linked = table_linked = runtime_malloc(
  486. total_size_of_tables_linked, error_buf, error_buf_size))) {
  487. goto fail;
  488. }
  489. #endif
  490. /* instantiate tables from import section */
  491. import = module->import_tables;
  492. for (i = 0; i < module->import_table_count; i++, import++) {
  493. uint32 max_size_fixed = 0;
  494. #if WASM_ENABLE_MULTI_MODULE != 0
  495. WASMTableInstance *table_inst_linked = NULL;
  496. WASMModuleInstance *module_inst_linked = NULL;
  497. if (import->u.table.import_module) {
  498. if (!(module_inst_linked = get_sub_module_inst(
  499. module_inst, import->u.table.import_module))) {
  500. set_error_buf(error_buf, error_buf_size, "unknown table");
  501. goto fail;
  502. }
  503. if (!(table_inst_linked = wasm_lookup_table(
  504. module_inst_linked, import->u.table.field_name))) {
  505. set_error_buf(error_buf, error_buf_size, "unknown table");
  506. goto fail;
  507. }
  508. total_size = offsetof(WASMTableInstance, elems);
  509. }
  510. else
  511. #endif
  512. {
  513. /* in order to save memory, alloc resource as few as possible */
  514. max_size_fixed = import->u.table.possible_grow
  515. ? import->u.table.max_size
  516. : import->u.table.init_size;
  517. /* it is a built-in table, every module has its own */
  518. total_size = offsetof(WASMTableInstance, elems);
  519. total_size += (uint64)max_size_fixed * sizeof(uint32);
  520. }
  521. tables[table_index++] = table;
  522. /* Set all elements to -1 to mark them as uninitialized elements */
  523. memset(table, -1, (uint32)total_size);
  524. #if WASM_ENABLE_MULTI_MODULE != 0
  525. *table_linked = table_inst_linked;
  526. if (table_inst_linked != NULL) {
  527. table->cur_size = table_inst_linked->cur_size;
  528. table->max_size = table_inst_linked->max_size;
  529. }
  530. else
  531. #endif
  532. {
  533. table->cur_size = import->u.table.init_size;
  534. table->max_size = max_size_fixed;
  535. }
  536. table = (WASMTableInstance *)((uint8 *)table + (uint32)total_size);
  537. #if WASM_ENABLE_MULTI_MODULE != 0
  538. table_linked++;
  539. #endif
  540. }
  541. /* instantiate tables from table section */
  542. for (i = 0; i < module->table_count; i++) {
  543. uint32 max_size_fixed = 0;
  544. total_size = offsetof(WASMTableInstance, elems);
  545. #if WASM_ENABLE_MULTI_MODULE != 0
  546. /* in case, a module which imports this table will grow it */
  547. max_size_fixed = module->tables[i].max_size;
  548. #else
  549. max_size_fixed = module->tables[i].possible_grow
  550. ? module->tables[i].max_size
  551. : module->tables[i].init_size;
  552. #endif
  553. total_size += sizeof(uint32) * (uint64)max_size_fixed;
  554. tables[table_index++] = table;
  555. /* Set all elements to -1 to mark them as uninitialized elements */
  556. memset(table, -1, (uint32)total_size);
  557. table->cur_size = module->tables[i].init_size;
  558. table->max_size = max_size_fixed;
  559. table = (WASMTableInstance *)((uint8 *)table + (uint32)total_size);
  560. }
  561. bh_assert(table_index == table_count);
  562. (void)module_inst;
  563. return tables;
  564. #if WASM_ENABLE_MULTI_MODULE != 0
  565. fail:
  566. wasm_runtime_free(tables);
  567. return NULL;
  568. #endif
  569. }
  570. /**
  571. * Destroy function instances.
  572. */
  573. static void
  574. functions_deinstantiate(WASMFunctionInstance *functions, uint32 count)
  575. {
  576. if (functions) {
  577. wasm_runtime_free(functions);
  578. }
  579. }
  580. /**
  581. * Instantiate functions in a module.
  582. */
  583. static WASMFunctionInstance *
  584. functions_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  585. char *error_buf, uint32 error_buf_size)
  586. {
  587. WASMImport *import;
  588. uint32 i,
  589. function_count = module->import_function_count + module->function_count;
  590. uint64 total_size = sizeof(WASMFunctionInstance) * (uint64)function_count;
  591. WASMFunctionInstance *functions, *function;
  592. if (!(functions = runtime_malloc(total_size, error_buf, error_buf_size))) {
  593. return NULL;
  594. }
  595. total_size = sizeof(void *) * (uint64)module->import_function_count;
  596. if (total_size > 0
  597. && !(module_inst->import_func_ptrs =
  598. runtime_malloc(total_size, error_buf, error_buf_size))) {
  599. wasm_runtime_free(functions);
  600. return NULL;
  601. }
  602. /* instantiate functions from import section */
  603. function = functions;
  604. import = module->import_functions;
  605. for (i = 0; i < module->import_function_count; i++, import++) {
  606. function->is_import_func = true;
  607. #if WASM_ENABLE_MULTI_MODULE != 0
  608. if (import->u.function.import_module) {
  609. function->import_module_inst = get_sub_module_inst(
  610. module_inst, import->u.function.import_module);
  611. if (function->import_module_inst) {
  612. function->import_func_inst =
  613. wasm_lookup_function(function->import_module_inst,
  614. import->u.function.field_name, NULL);
  615. }
  616. }
  617. #endif /* WASM_ENABLE_MULTI_MODULE */
  618. function->u.func_import = &import->u.function;
  619. function->param_cell_num = import->u.function.func_type->param_cell_num;
  620. function->ret_cell_num = import->u.function.func_type->ret_cell_num;
  621. function->param_count =
  622. (uint16)function->u.func_import->func_type->param_count;
  623. function->param_types = function->u.func_import->func_type->types;
  624. function->local_cell_num = 0;
  625. function->local_count = 0;
  626. function->local_types = NULL;
  627. /* Copy the function pointer to current instance */
  628. module_inst->import_func_ptrs[i] =
  629. function->u.func_import->func_ptr_linked;
  630. function++;
  631. }
  632. /* instantiate functions from function section */
  633. for (i = 0; i < module->function_count; i++) {
  634. function->is_import_func = false;
  635. function->u.func = module->functions[i];
  636. function->param_cell_num = function->u.func->param_cell_num;
  637. function->ret_cell_num = function->u.func->ret_cell_num;
  638. function->local_cell_num = function->u.func->local_cell_num;
  639. function->param_count =
  640. (uint16)function->u.func->func_type->param_count;
  641. function->local_count = (uint16)function->u.func->local_count;
  642. function->param_types = function->u.func->func_type->types;
  643. function->local_types = function->u.func->local_types;
  644. function->local_offsets = function->u.func->local_offsets;
  645. #if WASM_ENABLE_FAST_INTERP != 0
  646. function->const_cell_num = function->u.func->const_cell_num;
  647. #endif
  648. function++;
  649. }
  650. bh_assert((uint32)(function - functions) == function_count);
  651. #if WASM_ENABLE_FAST_JIT != 0
  652. module_inst->fast_jit_func_ptrs = module->fast_jit_func_ptrs;
  653. #endif
  654. return functions;
  655. }
  656. /**
  657. * Destroy global instances.
  658. */
  659. static void
  660. globals_deinstantiate(WASMGlobalInstance *globals)
  661. {
  662. if (globals)
  663. wasm_runtime_free(globals);
  664. }
  665. static bool
  666. check_global_init_expr(const WASMModule *module, uint32 global_index,
  667. char *error_buf, uint32 error_buf_size)
  668. {
  669. if (global_index >= module->import_global_count + module->global_count) {
  670. set_error_buf_v(error_buf, error_buf_size, "unknown global %d",
  671. global_index);
  672. return false;
  673. }
  674. /**
  675. * Currently, constant expressions occurring as initializers of
  676. * globals are further constrained in that contained global.get
  677. * instructions are only allowed to refer to imported globals.
  678. *
  679. * And initializer expression cannot reference a mutable global.
  680. */
  681. if (global_index >= module->import_global_count
  682. || (module->import_globals + global_index)->u.global.is_mutable) {
  683. set_error_buf(error_buf, error_buf_size,
  684. "constant expression required");
  685. return false;
  686. }
  687. return true;
  688. }
  689. /**
  690. * Instantiate globals in a module.
  691. */
  692. static WASMGlobalInstance *
  693. globals_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  694. char *error_buf, uint32 error_buf_size)
  695. {
  696. WASMImport *import;
  697. uint32 global_data_offset = 0;
  698. uint32 i, global_count = module->import_global_count + module->global_count;
  699. uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count;
  700. WASMGlobalInstance *globals, *global;
  701. if (!(globals = runtime_malloc(total_size, error_buf, error_buf_size))) {
  702. return NULL;
  703. }
  704. /* instantiate globals from import section */
  705. global = globals;
  706. import = module->import_globals;
  707. for (i = 0; i < module->import_global_count; i++, import++) {
  708. WASMGlobalImport *global_import = &import->u.global;
  709. global->type = global_import->type;
  710. global->is_mutable = global_import->is_mutable;
  711. #if WASM_ENABLE_MULTI_MODULE != 0
  712. if (global_import->import_module) {
  713. if (!(global->import_module_inst = get_sub_module_inst(
  714. module_inst, global_import->import_module))) {
  715. set_error_buf(error_buf, error_buf_size, "unknown global");
  716. goto fail;
  717. }
  718. if (!(global->import_global_inst = wasm_lookup_global(
  719. global->import_module_inst, global_import->field_name))) {
  720. set_error_buf(error_buf, error_buf_size, "unknown global");
  721. goto fail;
  722. }
  723. /* The linked global instance has been initialized, we
  724. just need to copy the value. */
  725. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  726. &(global_import->import_global_linked->init_expr),
  727. sizeof(WASMValue));
  728. }
  729. else
  730. #endif
  731. {
  732. /* native globals share their initial_values in one module */
  733. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  734. &(global_import->global_data_linked),
  735. sizeof(WASMValue));
  736. }
  737. #if WASM_ENABLE_FAST_JIT != 0
  738. bh_assert(global_data_offset == global_import->data_offset);
  739. #endif
  740. global->data_offset = global_data_offset;
  741. global_data_offset += wasm_value_type_size(global->type);
  742. global++;
  743. }
  744. /* instantiate globals from global section */
  745. for (i = 0; i < module->global_count; i++) {
  746. InitializerExpression *init_expr = &(module->globals[i].init_expr);
  747. global->type = module->globals[i].type;
  748. global->is_mutable = module->globals[i].is_mutable;
  749. #if WASM_ENABLE_FAST_JIT != 0
  750. bh_assert(global_data_offset == module->globals[i].data_offset);
  751. #endif
  752. global->data_offset = global_data_offset;
  753. global_data_offset += wasm_value_type_size(global->type);
  754. if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  755. if (!check_global_init_expr(module, init_expr->u.global_index,
  756. error_buf, error_buf_size)) {
  757. goto fail;
  758. }
  759. bh_memcpy_s(
  760. &(global->initial_value), sizeof(WASMValue),
  761. &(globals[init_expr->u.global_index].initial_value),
  762. sizeof(globals[init_expr->u.global_index].initial_value));
  763. }
  764. #if WASM_ENABLE_REF_TYPES != 0
  765. else if (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST) {
  766. global->initial_value.u32 = (uint32)NULL_REF;
  767. }
  768. #endif
  769. else {
  770. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  771. &(init_expr->u), sizeof(init_expr->u));
  772. }
  773. global++;
  774. }
  775. bh_assert((uint32)(global - globals) == global_count);
  776. bh_assert(global_data_offset == module->global_data_size);
  777. (void)module_inst;
  778. return globals;
  779. fail:
  780. wasm_runtime_free(globals);
  781. return NULL;
  782. }
  783. /**
  784. * Return export function count in module export section.
  785. */
  786. static uint32
  787. get_export_count(const WASMModule *module, uint8 kind)
  788. {
  789. WASMExport *export = module->exports;
  790. uint32 count = 0, i;
  791. for (i = 0; i < module->export_count; i++, export ++)
  792. if (export->kind == kind)
  793. count++;
  794. return count;
  795. }
  796. /**
  797. * Destroy export function instances.
  798. */
  799. static void
  800. export_functions_deinstantiate(WASMExportFuncInstance *functions)
  801. {
  802. if (functions)
  803. wasm_runtime_free(functions);
  804. }
  805. /**
  806. * Instantiate export functions in a module.
  807. */
  808. static WASMExportFuncInstance *
  809. export_functions_instantiate(const WASMModule *module,
  810. WASMModuleInstance *module_inst,
  811. uint32 export_func_count, char *error_buf,
  812. uint32 error_buf_size)
  813. {
  814. WASMExportFuncInstance *export_funcs, *export_func;
  815. WASMExport *export = module->exports;
  816. uint32 i;
  817. uint64 total_size =
  818. sizeof(WASMExportFuncInstance) * (uint64)export_func_count;
  819. if (!(export_func = export_funcs =
  820. runtime_malloc(total_size, error_buf, error_buf_size))) {
  821. return NULL;
  822. }
  823. for (i = 0; i < module->export_count; i++, export ++)
  824. if (export->kind == EXPORT_KIND_FUNC) {
  825. export_func->name = export->name;
  826. export_func->function = &module_inst->e->functions[export->index];
  827. export_func++;
  828. }
  829. bh_assert((uint32)(export_func - export_funcs) == export_func_count);
  830. return export_funcs;
  831. }
  832. #if WASM_ENABLE_MULTI_MODULE != 0
  833. static void
  834. export_globals_deinstantiate(WASMExportGlobInstance *globals)
  835. {
  836. if (globals)
  837. wasm_runtime_free(globals);
  838. }
  839. static WASMExportGlobInstance *
  840. export_globals_instantiate(const WASMModule *module,
  841. WASMModuleInstance *module_inst,
  842. uint32 export_glob_count, char *error_buf,
  843. uint32 error_buf_size)
  844. {
  845. WASMExportGlobInstance *export_globals, *export_global;
  846. WASMExport *export = module->exports;
  847. uint32 i;
  848. uint64 total_size =
  849. sizeof(WASMExportGlobInstance) * (uint64)export_glob_count;
  850. if (!(export_global = export_globals =
  851. runtime_malloc(total_size, error_buf, error_buf_size))) {
  852. return NULL;
  853. }
  854. for (i = 0; i < module->export_count; i++, export ++)
  855. if (export->kind == EXPORT_KIND_GLOBAL) {
  856. export_global->name = export->name;
  857. export_global->global = &module_inst->e->globals[export->index];
  858. export_global++;
  859. }
  860. bh_assert((uint32)(export_global - export_globals) == export_glob_count);
  861. return export_globals;
  862. }
  863. #endif
  864. static WASMFunctionInstance *
  865. lookup_post_instantiate_func(WASMModuleInstance *module_inst,
  866. const char *func_name)
  867. {
  868. WASMFunctionInstance *func;
  869. WASMType *func_type;
  870. if (!(func = wasm_lookup_function(module_inst, func_name, NULL)))
  871. /* Not found */
  872. return NULL;
  873. func_type = func->u.func->func_type;
  874. if (!(func_type->param_count == 0 && func_type->result_count == 0))
  875. /* Not a valid function type, ignore it */
  876. return NULL;
  877. return func;
  878. }
  879. static bool
  880. execute_post_instantiate_functions(WASMModuleInstance *module_inst,
  881. bool is_sub_inst, WASMExecEnv *exec_env_main)
  882. {
  883. WASMFunctionInstance *start_func = module_inst->e->start_function;
  884. WASMFunctionInstance *initialize_func = NULL;
  885. WASMFunctionInstance *post_inst_func = NULL;
  886. WASMFunctionInstance *call_ctors_func = NULL;
  887. #if WASM_ENABLE_LIBC_WASI != 0
  888. WASMModule *module = module_inst->module;
  889. #endif
  890. WASMModuleInstanceCommon *module_inst_main = NULL;
  891. #ifdef OS_ENABLE_HW_BOUND_CHECK
  892. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  893. #endif
  894. WASMExecEnv *exec_env = NULL, *exec_env_created = NULL;
  895. bool ret = false;
  896. #if WASM_ENABLE_LIBC_WASI != 0
  897. /*
  898. * WASI reactor instances may assume that _initialize will be called by
  899. * the environment at most once, and that none of their other exports
  900. * are accessed before that call.
  901. */
  902. if (!is_sub_inst && module->import_wasi_api) {
  903. initialize_func =
  904. lookup_post_instantiate_func(module_inst, "_initialize");
  905. }
  906. #endif
  907. /* Execute possible "__post_instantiate" function if wasm app is
  908. compiled by emsdk's early version */
  909. if (!is_sub_inst) {
  910. post_inst_func =
  911. lookup_post_instantiate_func(module_inst, "__post_instantiate");
  912. }
  913. #if WASM_ENABLE_BULK_MEMORY != 0
  914. /* Only execute the memory init function for main instance since
  915. the data segments will be dropped once initialized */
  916. if (!is_sub_inst
  917. #if WASM_ENABLE_LIBC_WASI != 0
  918. && !module->import_wasi_api
  919. #endif
  920. ) {
  921. call_ctors_func =
  922. lookup_post_instantiate_func(module_inst, "__wasm_call_ctors");
  923. }
  924. #endif
  925. if (!start_func && !initialize_func && !post_inst_func
  926. && !call_ctors_func) {
  927. /* No post instantiation functions to call */
  928. return true;
  929. }
  930. if (is_sub_inst) {
  931. bh_assert(exec_env_main);
  932. #ifdef OS_ENABLE_HW_BOUND_CHECK
  933. bh_assert(exec_env_tls == exec_env_main);
  934. (void)exec_env_tls;
  935. #endif
  936. exec_env = exec_env_main;
  937. /* Temporarily replace parent exec_env's module inst to current
  938. module inst to avoid checking failure when calling the
  939. wasm functions, and ensure that the exec_env's module inst
  940. is the correct one. */
  941. module_inst_main = exec_env_main->module_inst;
  942. wasm_exec_env_set_module_inst(exec_env,
  943. (WASMModuleInstanceCommon *)module_inst);
  944. }
  945. else {
  946. /* Try using the existing exec_env */
  947. #ifdef OS_ENABLE_HW_BOUND_CHECK
  948. exec_env = exec_env_tls;
  949. #endif
  950. #if WASM_ENABLE_THREAD_MGR != 0
  951. if (!exec_env)
  952. exec_env = wasm_clusters_search_exec_env(
  953. (WASMModuleInstanceCommon *)module_inst);
  954. #endif
  955. if (!exec_env) {
  956. if (!(exec_env = exec_env_created = wasm_exec_env_create(
  957. (WASMModuleInstanceCommon *)module_inst,
  958. module_inst->default_wasm_stack_size))) {
  959. wasm_set_exception(module_inst, "allocate memory failed");
  960. return false;
  961. }
  962. }
  963. else {
  964. /* Temporarily replace exec_env's module inst with current
  965. module inst to ensure that the exec_env's module inst
  966. is the correct one. */
  967. module_inst_main = exec_env->module_inst;
  968. wasm_exec_env_set_module_inst(
  969. exec_env, (WASMModuleInstanceCommon *)module_inst);
  970. }
  971. }
  972. /* Execute start function for both main insance and sub instance */
  973. if (start_func && !wasm_call_function(exec_env, start_func, 0, NULL)) {
  974. goto fail;
  975. }
  976. #if WASM_ENABLE_LIBC_WASI != 0
  977. if (initialize_func
  978. && !wasm_call_function(exec_env, initialize_func, 0, NULL)) {
  979. goto fail;
  980. }
  981. #else
  982. (void)initialize_func;
  983. #endif
  984. if (post_inst_func
  985. && !wasm_call_function(exec_env, post_inst_func, 0, NULL)) {
  986. goto fail;
  987. }
  988. if (call_ctors_func
  989. && !wasm_call_function(exec_env, call_ctors_func, 0, NULL)) {
  990. goto fail;
  991. }
  992. ret = true;
  993. fail:
  994. if (is_sub_inst) {
  995. /* Restore the parent exec_env's module inst */
  996. wasm_exec_env_restore_module_inst(exec_env_main, module_inst_main);
  997. }
  998. else {
  999. if (module_inst_main)
  1000. /* Restore the existing exec_env's module inst */
  1001. wasm_exec_env_restore_module_inst(exec_env, module_inst_main);
  1002. if (exec_env_created)
  1003. wasm_exec_env_destroy(exec_env_created);
  1004. }
  1005. return ret;
  1006. }
  1007. static bool
  1008. execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  1009. WASMFunctionInstance *malloc_func,
  1010. WASMFunctionInstance *retain_func, uint32 size,
  1011. uint32 *p_result)
  1012. {
  1013. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1014. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  1015. #endif
  1016. WASMExecEnv *exec_env_created = NULL;
  1017. WASMModuleInstanceCommon *module_inst_old = NULL;
  1018. uint32 argv[2], argc;
  1019. bool ret;
  1020. argv[0] = size;
  1021. argc = 1;
  1022. /* if __retain is exported, then this module is compiled by
  1023. assemblyscript, the memory should be managed by as's runtime,
  1024. in this case we need to call the retain function after malloc
  1025. the memory */
  1026. if (retain_func) {
  1027. /* the malloc functino from assemblyscript is:
  1028. function __new(size: usize, id: u32)
  1029. id = 0 means this is an ArrayBuffer object */
  1030. argv[1] = 0;
  1031. argc = 2;
  1032. }
  1033. if (exec_env) {
  1034. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1035. if (exec_env_tls) {
  1036. bh_assert(exec_env_tls == exec_env);
  1037. }
  1038. #endif
  1039. bh_assert(exec_env->module_inst
  1040. == (WASMModuleInstanceCommon *)module_inst);
  1041. }
  1042. else {
  1043. /* Try using the existing exec_env */
  1044. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1045. exec_env = exec_env_tls;
  1046. #endif
  1047. #if WASM_ENABLE_THREAD_MGR != 0
  1048. if (!exec_env)
  1049. exec_env = wasm_clusters_search_exec_env(
  1050. (WASMModuleInstanceCommon *)module_inst);
  1051. #endif
  1052. if (!exec_env) {
  1053. if (!(exec_env = exec_env_created = wasm_exec_env_create(
  1054. (WASMModuleInstanceCommon *)module_inst,
  1055. module_inst->default_wasm_stack_size))) {
  1056. wasm_set_exception(module_inst, "allocate memory failed");
  1057. return false;
  1058. }
  1059. }
  1060. else {
  1061. /* Temporarily replace exec_env's module inst with current
  1062. module inst to ensure that the exec_env's module inst
  1063. is the correct one. */
  1064. module_inst_old = exec_env->module_inst;
  1065. wasm_exec_env_set_module_inst(
  1066. exec_env, (WASMModuleInstanceCommon *)module_inst);
  1067. }
  1068. }
  1069. ret = wasm_call_function(exec_env, malloc_func, argc, argv);
  1070. if (retain_func && ret)
  1071. ret = wasm_call_function(exec_env, retain_func, 1, argv);
  1072. if (module_inst_old)
  1073. /* Restore the existing exec_env's module inst */
  1074. wasm_exec_env_restore_module_inst(exec_env, module_inst_old);
  1075. if (exec_env_created)
  1076. wasm_exec_env_destroy(exec_env_created);
  1077. if (ret)
  1078. *p_result = argv[0];
  1079. return ret;
  1080. }
  1081. static bool
  1082. execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  1083. WASMFunctionInstance *free_func, uint32 offset)
  1084. {
  1085. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1086. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  1087. #endif
  1088. WASMExecEnv *exec_env_created = NULL;
  1089. WASMModuleInstanceCommon *module_inst_old = NULL;
  1090. uint32 argv[2];
  1091. bool ret;
  1092. argv[0] = offset;
  1093. if (exec_env) {
  1094. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1095. if (exec_env_tls) {
  1096. bh_assert(exec_env_tls == exec_env);
  1097. }
  1098. #endif
  1099. bh_assert(exec_env->module_inst
  1100. == (WASMModuleInstanceCommon *)module_inst);
  1101. }
  1102. else {
  1103. /* Try using the existing exec_env */
  1104. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1105. exec_env = exec_env_tls;
  1106. #endif
  1107. #if WASM_ENABLE_THREAD_MGR != 0
  1108. if (!exec_env)
  1109. exec_env = wasm_clusters_search_exec_env(
  1110. (WASMModuleInstanceCommon *)module_inst);
  1111. #endif
  1112. if (!exec_env) {
  1113. if (!(exec_env = exec_env_created = wasm_exec_env_create(
  1114. (WASMModuleInstanceCommon *)module_inst,
  1115. module_inst->default_wasm_stack_size))) {
  1116. wasm_set_exception(module_inst, "allocate memory failed");
  1117. return false;
  1118. }
  1119. }
  1120. else {
  1121. /* Temporarily replace exec_env's module inst with current
  1122. module inst to ensure that the exec_env's module inst
  1123. is the correct one. */
  1124. module_inst_old = exec_env->module_inst;
  1125. wasm_exec_env_set_module_inst(
  1126. exec_env, (WASMModuleInstanceCommon *)module_inst);
  1127. }
  1128. }
  1129. ret = wasm_call_function(exec_env, free_func, 1, argv);
  1130. if (module_inst_old)
  1131. /* Restore the existing exec_env's module inst */
  1132. wasm_exec_env_restore_module_inst(exec_env, module_inst_old);
  1133. if (exec_env_created)
  1134. wasm_exec_env_destroy(exec_env_created);
  1135. return ret;
  1136. }
  1137. static bool
  1138. check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
  1139. uint32 error_buf_size)
  1140. {
  1141. WASMModule *module = module_inst->module;
  1142. uint32 i;
  1143. for (i = 0; i < module->import_function_count; i++) {
  1144. WASMFunctionImport *func =
  1145. &((module->import_functions + i)->u.function);
  1146. if (!func->func_ptr_linked
  1147. #if WASM_ENABLE_MULTI_MODULE != 0
  1148. && !func->import_func_linked
  1149. #endif
  1150. ) {
  1151. #if WASM_ENABLE_WAMR_COMPILER == 0
  1152. LOG_WARNING("warning: failed to link import function (%s, %s)",
  1153. func->module_name, func->field_name);
  1154. /* will throw exception only if calling */
  1155. #else
  1156. /* do nothing to avoid confused message */
  1157. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  1158. }
  1159. }
  1160. for (i = 0; i < module->import_global_count; i++) {
  1161. WASMGlobalImport *global = &((module->import_globals + i)->u.global);
  1162. if (!global->is_linked) {
  1163. #if WASM_ENABLE_SPEC_TEST != 0
  1164. set_error_buf(error_buf, error_buf_size,
  1165. "unknown import or incompatible import type");
  1166. return false;
  1167. #else
  1168. #if WASM_ENABLE_WAMR_COMPILER == 0
  1169. set_error_buf_v(error_buf, error_buf_size,
  1170. "failed to link import global (%s, %s)",
  1171. global->module_name, global->field_name);
  1172. return false;
  1173. #else
  1174. /* do nothing to avoid confused message */
  1175. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  1176. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  1177. }
  1178. }
  1179. return true;
  1180. }
  1181. #if WASM_ENABLE_JIT != 0
  1182. static bool
  1183. init_func_ptrs(WASMModuleInstance *module_inst, WASMModule *module,
  1184. char *error_buf, uint32 error_buf_size)
  1185. {
  1186. uint32 i;
  1187. void **func_ptrs;
  1188. uint64 total_size = (uint64)sizeof(void *) * module_inst->e->function_count;
  1189. /* Allocate memory */
  1190. if (!(func_ptrs = module_inst->func_ptrs =
  1191. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1192. return false;
  1193. }
  1194. /* Set import function pointers */
  1195. for (i = 0; i < module->import_function_count; i++, func_ptrs++) {
  1196. WASMFunctionImport *import_func =
  1197. &module->import_functions[i].u.function;
  1198. /* TODO: handle multi module */
  1199. *func_ptrs = import_func->func_ptr_linked;
  1200. }
  1201. /* The defined function pointers will be set in
  1202. wasm_runtime_set_running_mode, no need to set them here */
  1203. return true;
  1204. }
  1205. #endif /* end of WASM_ENABLE_JIT != 0 */
  1206. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1207. static uint32
  1208. get_smallest_type_idx(WASMModule *module, WASMType *func_type)
  1209. {
  1210. uint32 i;
  1211. for (i = 0; i < module->type_count; i++) {
  1212. if (func_type == module->types[i])
  1213. return i;
  1214. }
  1215. bh_assert(0);
  1216. return -1;
  1217. }
  1218. static bool
  1219. init_func_type_indexes(WASMModuleInstance *module_inst, char *error_buf,
  1220. uint32 error_buf_size)
  1221. {
  1222. uint32 i;
  1223. uint64 total_size = (uint64)sizeof(uint32) * module_inst->e->function_count;
  1224. /* Allocate memory */
  1225. if (!(module_inst->func_type_indexes =
  1226. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1227. return false;
  1228. }
  1229. for (i = 0; i < module_inst->e->function_count; i++) {
  1230. WASMFunctionInstance *func_inst = module_inst->e->functions + i;
  1231. WASMType *func_type = func_inst->is_import_func
  1232. ? func_inst->u.func_import->func_type
  1233. : func_inst->u.func->func_type;
  1234. module_inst->func_type_indexes[i] =
  1235. get_smallest_type_idx(module_inst->module, func_type);
  1236. }
  1237. return true;
  1238. }
  1239. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  1240. static bool
  1241. set_running_mode(WASMModuleInstance *module_inst, RunningMode running_mode,
  1242. bool first_time_set)
  1243. {
  1244. WASMModule *module = module_inst->module;
  1245. if (running_mode == Mode_Default) {
  1246. #if WASM_ENABLE_FAST_JIT == 0 && WASM_ENABLE_JIT == 0
  1247. running_mode = Mode_Interp;
  1248. #elif WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT == 0
  1249. running_mode = Mode_Fast_JIT;
  1250. #elif WASM_ENABLE_FAST_JIT == 0 && WASM_ENABLE_JIT != 0
  1251. running_mode = Mode_LLVM_JIT;
  1252. #else /* WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 */
  1253. #if WASM_ENABLE_LAZY_JIT == 0
  1254. running_mode = Mode_LLVM_JIT;
  1255. #else
  1256. running_mode = Mode_Multi_Tier_JIT;
  1257. #endif
  1258. #endif
  1259. }
  1260. if (!wasm_runtime_is_running_mode_supported(running_mode))
  1261. return false;
  1262. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1263. && WASM_ENABLE_LAZY_JIT != 0) /* No possible multi-tier JIT */
  1264. module_inst->e->running_mode = running_mode;
  1265. if (running_mode == Mode_Interp) {
  1266. /* Do nothing for Mode_Interp */
  1267. }
  1268. else if (running_mode == Mode_Fast_JIT) {
  1269. /* Do nothing for Mode_Fast_JIT since
  1270. module_inst->fast_jit_func_ptrs is same as
  1271. module->fast_jit_func_ptrs */
  1272. }
  1273. #if WASM_ENABLE_JIT != 0
  1274. else if (running_mode == Mode_LLVM_JIT) {
  1275. /* Set defined function pointers */
  1276. bh_memcpy_s(module_inst->func_ptrs + module->import_function_count,
  1277. sizeof(void *) * module->function_count, module->func_ptrs,
  1278. sizeof(void *) * module->function_count);
  1279. }
  1280. #endif
  1281. else {
  1282. bh_assert(0);
  1283. }
  1284. #else /* Possible multi-tier JIT */
  1285. os_mutex_lock(&module->instance_list_lock);
  1286. module_inst->e->running_mode = running_mode;
  1287. if (running_mode == Mode_Interp) {
  1288. /* Do nothing for Mode_Interp */
  1289. }
  1290. #if WASM_ENABLE_FAST_JIT != 0
  1291. else if (running_mode == Mode_Fast_JIT) {
  1292. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  1293. uint32 i;
  1294. /* Allocate memory for fast_jit_func_ptrs if needed */
  1295. if (!module_inst->fast_jit_func_ptrs
  1296. || module_inst->fast_jit_func_ptrs == module->fast_jit_func_ptrs) {
  1297. uint64 total_size = (uint64)sizeof(void *) * module->function_count;
  1298. if (!(module_inst->fast_jit_func_ptrs =
  1299. runtime_malloc(total_size, NULL, 0))) {
  1300. os_mutex_unlock(&module->instance_list_lock);
  1301. return false;
  1302. }
  1303. }
  1304. for (i = 0; i < module->function_count; i++) {
  1305. if (module->functions[i]->fast_jit_jitted_code) {
  1306. /* current fast jit function has been compiled */
  1307. module_inst->fast_jit_func_ptrs[i] =
  1308. module->functions[i]->fast_jit_jitted_code;
  1309. }
  1310. else {
  1311. module_inst->fast_jit_func_ptrs[i] =
  1312. jit_globals->compile_fast_jit_and_then_call;
  1313. }
  1314. }
  1315. }
  1316. #endif
  1317. #if WASM_ENABLE_JIT != 0
  1318. else if (running_mode == Mode_LLVM_JIT) {
  1319. void **llvm_jit_func_ptrs;
  1320. uint32 i;
  1321. /* Notify backend threads to start llvm jit compilation */
  1322. module->enable_llvm_jit_compilation = true;
  1323. /* Wait until llvm jit finishes initialization */
  1324. os_mutex_lock(&module->tierup_wait_lock);
  1325. while (!module->llvm_jit_inited) {
  1326. os_cond_reltimedwait(&module->tierup_wait_cond,
  1327. &module->tierup_wait_lock, 10000);
  1328. if (module->orcjit_stop_compiling) {
  1329. /* init_llvm_jit_functions_stage2 failed */
  1330. os_mutex_unlock(&module->tierup_wait_lock);
  1331. os_mutex_unlock(&module->instance_list_lock);
  1332. return false;
  1333. }
  1334. }
  1335. os_mutex_unlock(&module->tierup_wait_lock);
  1336. llvm_jit_func_ptrs =
  1337. module_inst->func_ptrs + module->import_function_count;
  1338. for (i = 0; i < module->function_count; i++) {
  1339. llvm_jit_func_ptrs[i] = module->functions[i]->llvm_jit_func_ptr;
  1340. }
  1341. }
  1342. #endif
  1343. else if (running_mode == Mode_Multi_Tier_JIT) {
  1344. /* Notify backend threads to start llvm jit compilation */
  1345. module->enable_llvm_jit_compilation = true;
  1346. /* Free fast_jit_func_ptrs if it is allocated before */
  1347. if (module_inst->fast_jit_func_ptrs
  1348. && module_inst->fast_jit_func_ptrs != module->fast_jit_func_ptrs) {
  1349. wasm_runtime_free(module_inst->fast_jit_func_ptrs);
  1350. }
  1351. module_inst->fast_jit_func_ptrs = module->fast_jit_func_ptrs;
  1352. /* Copy all llvm jit func ptrs from the module */
  1353. bh_memcpy_s(module_inst->func_ptrs + module->import_function_count,
  1354. sizeof(void *) * module->function_count, module->func_ptrs,
  1355. sizeof(void *) * module->function_count);
  1356. }
  1357. else {
  1358. bh_assert(0);
  1359. }
  1360. /* Add module instance into module's instance list if not added */
  1361. if (first_time_set) {
  1362. bool found = false;
  1363. WASMModuleInstance *node = module->instance_list;
  1364. while (node) {
  1365. if (node == module_inst) {
  1366. found = true;
  1367. break;
  1368. }
  1369. node = node->e->next;
  1370. }
  1371. if (!found) {
  1372. module_inst->e->next = module->instance_list;
  1373. module->instance_list = module_inst;
  1374. }
  1375. }
  1376. os_mutex_unlock(&module->instance_list_lock);
  1377. #endif /* end of !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1378. && WASM_ENABLE_LAZY_JIT != 0) */
  1379. (void)module;
  1380. return true;
  1381. }
  1382. bool
  1383. wasm_set_running_mode(WASMModuleInstance *module_inst, RunningMode running_mode)
  1384. {
  1385. return set_running_mode(module_inst, running_mode, false);
  1386. }
  1387. /**
  1388. * Instantiate module
  1389. */
  1390. WASMModuleInstance *
  1391. wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
  1392. WASMExecEnv *exec_env_main, uint32 stack_size,
  1393. uint32 heap_size, char *error_buf, uint32 error_buf_size)
  1394. {
  1395. WASMModuleInstance *module_inst;
  1396. WASMGlobalInstance *globals = NULL, *global;
  1397. WASMTableInstance *first_table;
  1398. uint32 global_count, i;
  1399. uint32 base_offset, length, extra_info_offset;
  1400. uint32 module_inst_struct_size =
  1401. offsetof(WASMModuleInstance, global_table_data.bytes);
  1402. uint64 module_inst_mem_inst_size;
  1403. uint64 total_size, table_size = 0;
  1404. uint8 *global_data, *global_data_end;
  1405. #if WASM_ENABLE_MULTI_MODULE != 0
  1406. bool ret = false;
  1407. #endif
  1408. const bool is_sub_inst = parent != NULL;
  1409. if (!module)
  1410. return NULL;
  1411. /* Check the heap size */
  1412. heap_size = align_uint(heap_size, 8);
  1413. if (heap_size > APP_HEAP_SIZE_MAX)
  1414. heap_size = APP_HEAP_SIZE_MAX;
  1415. module_inst_mem_inst_size =
  1416. (uint64)sizeof(WASMMemoryInstance)
  1417. * (module->import_memory_count + module->memory_count);
  1418. #if WASM_ENABLE_JIT != 0
  1419. /* If the module dosen't have memory, reserve one mem_info space
  1420. with empty content to align with llvm jit compiler */
  1421. if (module_inst_mem_inst_size == 0)
  1422. module_inst_mem_inst_size = (uint64)sizeof(WASMMemoryInstance);
  1423. #endif
  1424. /* Size of module inst, memory instances and global data */
  1425. total_size = (uint64)module_inst_struct_size + module_inst_mem_inst_size
  1426. + module->global_data_size;
  1427. /* Calculate the size of table data */
  1428. for (i = 0; i < module->import_table_count; i++) {
  1429. WASMTableImport *import_table = &module->import_tables[i].u.table;
  1430. table_size += offsetof(WASMTableInstance, elems);
  1431. #if WASM_ENABLE_MULTI_MODULE != 0
  1432. table_size += (uint64)sizeof(uint32) * import_table->max_size;
  1433. #else
  1434. table_size += (uint64)sizeof(uint32)
  1435. * (import_table->possible_grow ? import_table->max_size
  1436. : import_table->init_size);
  1437. #endif
  1438. }
  1439. for (i = 0; i < module->table_count; i++) {
  1440. WASMTable *table = module->tables + i;
  1441. table_size += offsetof(WASMTableInstance, elems);
  1442. #if WASM_ENABLE_MULTI_MODULE != 0
  1443. table_size += (uint64)sizeof(uint32) * table->max_size;
  1444. #else
  1445. table_size +=
  1446. (uint64)sizeof(uint32)
  1447. * (table->possible_grow ? table->max_size : table->init_size);
  1448. #endif
  1449. }
  1450. total_size += table_size;
  1451. /* The offset of WASMModuleInstanceExtra, make it 8-byte aligned */
  1452. total_size = (total_size + 7LL) & ~7LL;
  1453. extra_info_offset = (uint32)total_size;
  1454. total_size += sizeof(WASMModuleInstanceExtra);
  1455. /* Allocate the memory for module instance with memory instances,
  1456. global data, table data appended at the end */
  1457. if (!(module_inst =
  1458. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1459. return NULL;
  1460. }
  1461. module_inst->module_type = Wasm_Module_Bytecode;
  1462. module_inst->module = module;
  1463. module_inst->e =
  1464. (WASMModuleInstanceExtra *)((uint8 *)module_inst + extra_info_offset);
  1465. #if WASM_ENABLE_MULTI_MODULE != 0
  1466. module_inst->e->sub_module_inst_list =
  1467. &module_inst->e->sub_module_inst_list_head;
  1468. ret = wasm_runtime_sub_module_instantiate(
  1469. (WASMModuleCommon *)module, (WASMModuleInstanceCommon *)module_inst,
  1470. stack_size, heap_size, error_buf, error_buf_size);
  1471. if (!ret) {
  1472. LOG_DEBUG("build a sub module list failed");
  1473. goto fail;
  1474. }
  1475. #endif
  1476. #if WASM_ENABLE_BULK_MEMORY != 0
  1477. if (module->data_seg_count > 0) {
  1478. module_inst->e->common.data_dropped =
  1479. bh_bitmap_new(0, module->data_seg_count);
  1480. if (module_inst->e->common.data_dropped == NULL) {
  1481. LOG_DEBUG("failed to allocate bitmaps");
  1482. set_error_buf(error_buf, error_buf_size,
  1483. "failed to allocate bitmaps");
  1484. goto fail;
  1485. }
  1486. }
  1487. #endif
  1488. #if WASM_ENABLE_REF_TYPES != 0
  1489. if (module->table_seg_count > 0) {
  1490. module_inst->e->common.elem_dropped =
  1491. bh_bitmap_new(0, module->table_seg_count);
  1492. if (module_inst->e->common.elem_dropped == NULL) {
  1493. LOG_DEBUG("failed to allocate bitmaps");
  1494. set_error_buf(error_buf, error_buf_size,
  1495. "failed to allocate bitmaps");
  1496. goto fail;
  1497. }
  1498. }
  1499. #endif
  1500. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1501. if (!(module_inst->frames = runtime_malloc((uint64)sizeof(Vector),
  1502. error_buf, error_buf_size))) {
  1503. goto fail;
  1504. }
  1505. #endif
  1506. /* Instantiate global firstly to get the mutable data size */
  1507. global_count = module->import_global_count + module->global_count;
  1508. if (global_count
  1509. && !(globals = globals_instantiate(module, module_inst, error_buf,
  1510. error_buf_size))) {
  1511. goto fail;
  1512. }
  1513. module_inst->e->global_count = global_count;
  1514. module_inst->e->globals = globals;
  1515. module_inst->global_data = (uint8 *)module_inst + module_inst_struct_size
  1516. + module_inst_mem_inst_size;
  1517. module_inst->global_data_size = module->global_data_size;
  1518. first_table = (WASMTableInstance *)(module_inst->global_data
  1519. + module->global_data_size);
  1520. module_inst->memory_count =
  1521. module->import_memory_count + module->memory_count;
  1522. module_inst->table_count = module->import_table_count + module->table_count;
  1523. module_inst->e->function_count =
  1524. module->import_function_count + module->function_count;
  1525. /* export */
  1526. module_inst->export_func_count = get_export_count(module, EXPORT_KIND_FUNC);
  1527. #if WASM_ENABLE_MULTI_MODULE != 0
  1528. module_inst->export_table_count =
  1529. get_export_count(module, EXPORT_KIND_TABLE);
  1530. module_inst->export_memory_count =
  1531. get_export_count(module, EXPORT_KIND_MEMORY);
  1532. module_inst->export_global_count =
  1533. get_export_count(module, EXPORT_KIND_GLOBAL);
  1534. #endif
  1535. /* Instantiate memories/tables/functions */
  1536. if ((module_inst->memory_count > 0
  1537. && !(module_inst->memories =
  1538. memories_instantiate(module, module_inst, parent, heap_size,
  1539. error_buf, error_buf_size)))
  1540. || (module_inst->table_count > 0
  1541. && !(module_inst->tables =
  1542. tables_instantiate(module, module_inst, first_table,
  1543. error_buf, error_buf_size)))
  1544. || (module_inst->e->function_count > 0
  1545. && !(module_inst->e->functions = functions_instantiate(
  1546. module, module_inst, error_buf, error_buf_size)))
  1547. || (module_inst->export_func_count > 0
  1548. && !(module_inst->export_functions = export_functions_instantiate(
  1549. module, module_inst, module_inst->export_func_count,
  1550. error_buf, error_buf_size)))
  1551. #if WASM_ENABLE_MULTI_MODULE != 0
  1552. || (module_inst->export_global_count > 0
  1553. && !(module_inst->export_globals = export_globals_instantiate(
  1554. module, module_inst, module_inst->export_global_count,
  1555. error_buf, error_buf_size)))
  1556. #endif
  1557. #if WASM_ENABLE_JIT != 0
  1558. || (module_inst->e->function_count > 0
  1559. && !init_func_ptrs(module_inst, module, error_buf, error_buf_size))
  1560. #endif
  1561. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1562. || (module_inst->e->function_count > 0
  1563. && !init_func_type_indexes(module_inst, error_buf, error_buf_size))
  1564. #endif
  1565. ) {
  1566. goto fail;
  1567. }
  1568. if (global_count > 0) {
  1569. /* Initialize the global data */
  1570. global_data = module_inst->global_data;
  1571. global_data_end = global_data + module->global_data_size;
  1572. global = globals;
  1573. for (i = 0; i < global_count; i++, global++) {
  1574. switch (global->type) {
  1575. case VALUE_TYPE_I32:
  1576. case VALUE_TYPE_F32:
  1577. #if WASM_ENABLE_REF_TYPES != 0
  1578. case VALUE_TYPE_FUNCREF:
  1579. case VALUE_TYPE_EXTERNREF:
  1580. #endif
  1581. *(int32 *)global_data = global->initial_value.i32;
  1582. global_data += sizeof(int32);
  1583. break;
  1584. case VALUE_TYPE_I64:
  1585. case VALUE_TYPE_F64:
  1586. bh_memcpy_s(global_data,
  1587. (uint32)(global_data_end - global_data),
  1588. &global->initial_value.i64, sizeof(int64));
  1589. global_data += sizeof(int64);
  1590. break;
  1591. #if WASM_ENABLE_SIMD != 0
  1592. case VALUE_TYPE_V128:
  1593. bh_memcpy_s(global_data, (uint32)sizeof(V128),
  1594. &global->initial_value.v128, sizeof(V128));
  1595. global_data += sizeof(V128);
  1596. break;
  1597. #endif
  1598. default:
  1599. bh_assert(0);
  1600. }
  1601. }
  1602. bh_assert(global_data == global_data_end);
  1603. }
  1604. if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
  1605. goto fail;
  1606. }
  1607. /* Initialize the memory data with data segment section */
  1608. for (i = 0; i < module->data_seg_count; i++) {
  1609. WASMMemoryInstance *memory = NULL;
  1610. uint8 *memory_data = NULL;
  1611. uint64 memory_size = 0;
  1612. WASMDataSeg *data_seg = module->data_segments[i];
  1613. #if WASM_ENABLE_BULK_MEMORY != 0
  1614. if (data_seg->is_passive)
  1615. continue;
  1616. #endif
  1617. if (is_sub_inst)
  1618. /* Ignore setting memory init data if the memory has been
  1619. initialized */
  1620. continue;
  1621. /* has check it in loader */
  1622. memory = module_inst->memories[data_seg->memory_index];
  1623. bh_assert(memory);
  1624. memory_data = memory->memory_data;
  1625. memory_size =
  1626. (uint64)memory->num_bytes_per_page * memory->cur_page_count;
  1627. bh_assert(memory_data || memory_size == 0);
  1628. bh_assert(data_seg->base_offset.init_expr_type
  1629. == INIT_EXPR_TYPE_I32_CONST
  1630. || data_seg->base_offset.init_expr_type
  1631. == INIT_EXPR_TYPE_GET_GLOBAL);
  1632. if (data_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  1633. if (!check_global_init_expr(module,
  1634. data_seg->base_offset.u.global_index,
  1635. error_buf, error_buf_size)) {
  1636. goto fail;
  1637. }
  1638. if (!globals
  1639. || globals[data_seg->base_offset.u.global_index].type
  1640. != VALUE_TYPE_I32) {
  1641. set_error_buf(error_buf, error_buf_size,
  1642. "data segment does not fit");
  1643. goto fail;
  1644. }
  1645. base_offset =
  1646. globals[data_seg->base_offset.u.global_index].initial_value.i32;
  1647. }
  1648. else {
  1649. base_offset = (uint32)data_seg->base_offset.u.i32;
  1650. }
  1651. /* check offset */
  1652. if (base_offset > memory_size) {
  1653. LOG_DEBUG("base_offset(%d) > memory_size(%d)", base_offset,
  1654. memory_size);
  1655. #if WASM_ENABLE_REF_TYPES != 0
  1656. set_error_buf(error_buf, error_buf_size,
  1657. "out of bounds memory access");
  1658. #else
  1659. set_error_buf(error_buf, error_buf_size,
  1660. "data segment does not fit");
  1661. #endif
  1662. goto fail;
  1663. }
  1664. /* check offset + length(could be zero) */
  1665. length = data_seg->data_length;
  1666. if ((uint64)base_offset + length > memory_size) {
  1667. LOG_DEBUG("base_offset(%d) + length(%d) > memory_size(%d)",
  1668. base_offset, length, memory_size);
  1669. #if WASM_ENABLE_REF_TYPES != 0
  1670. set_error_buf(error_buf, error_buf_size,
  1671. "out of bounds memory access");
  1672. #else
  1673. set_error_buf(error_buf, error_buf_size,
  1674. "data segment does not fit");
  1675. #endif
  1676. goto fail;
  1677. }
  1678. if (memory_data) {
  1679. bh_memcpy_s(memory_data + base_offset,
  1680. (uint32)memory_size - base_offset, data_seg->data,
  1681. length);
  1682. }
  1683. }
  1684. /* Initialize the table data with table segment section */
  1685. for (i = 0; module_inst->table_count > 0 && i < module->table_seg_count;
  1686. i++) {
  1687. WASMTableSeg *table_seg = module->table_segments + i;
  1688. /* has check it in loader */
  1689. WASMTableInstance *table = module_inst->tables[table_seg->table_index];
  1690. uint32 *table_data;
  1691. #if WASM_ENABLE_REF_TYPES != 0
  1692. uint8 tbl_elem_type;
  1693. uint32 tbl_init_size, tbl_max_size;
  1694. #endif
  1695. bh_assert(table);
  1696. #if WASM_ENABLE_REF_TYPES != 0
  1697. (void)wasm_runtime_get_table_inst_elem_type(
  1698. (WASMModuleInstanceCommon *)module_inst, table_seg->table_index,
  1699. &tbl_elem_type, &tbl_init_size, &tbl_max_size);
  1700. if (tbl_elem_type != VALUE_TYPE_FUNCREF
  1701. && tbl_elem_type != VALUE_TYPE_EXTERNREF) {
  1702. set_error_buf(error_buf, error_buf_size,
  1703. "elements segment does not fit");
  1704. goto fail;
  1705. }
  1706. (void)tbl_init_size;
  1707. (void)tbl_max_size;
  1708. #endif
  1709. table_data = table->elems;
  1710. #if WASM_ENABLE_MULTI_MODULE != 0
  1711. if (table_seg->table_index < module->import_table_count
  1712. && module_inst->e->table_insts_linked[table_seg->table_index]) {
  1713. table_data =
  1714. module_inst->e->table_insts_linked[table_seg->table_index]
  1715. ->elems;
  1716. }
  1717. #endif
  1718. bh_assert(table_data);
  1719. #if WASM_ENABLE_REF_TYPES != 0
  1720. if (!wasm_elem_is_active(table_seg->mode))
  1721. continue;
  1722. #endif
  1723. #if WASM_ENABLE_REF_TYPES != 0
  1724. bh_assert(table_seg->base_offset.init_expr_type
  1725. == INIT_EXPR_TYPE_I32_CONST
  1726. || table_seg->base_offset.init_expr_type
  1727. == INIT_EXPR_TYPE_GET_GLOBAL
  1728. || table_seg->base_offset.init_expr_type
  1729. == INIT_EXPR_TYPE_FUNCREF_CONST
  1730. || table_seg->base_offset.init_expr_type
  1731. == INIT_EXPR_TYPE_REFNULL_CONST);
  1732. #else
  1733. bh_assert(table_seg->base_offset.init_expr_type
  1734. == INIT_EXPR_TYPE_I32_CONST
  1735. || table_seg->base_offset.init_expr_type
  1736. == INIT_EXPR_TYPE_GET_GLOBAL);
  1737. #endif
  1738. /* init vec(funcidx) or vec(expr) */
  1739. if (table_seg->base_offset.init_expr_type
  1740. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1741. if (!check_global_init_expr(module,
  1742. table_seg->base_offset.u.global_index,
  1743. error_buf, error_buf_size)) {
  1744. goto fail;
  1745. }
  1746. if (!globals
  1747. || globals[table_seg->base_offset.u.global_index].type
  1748. != VALUE_TYPE_I32) {
  1749. set_error_buf(error_buf, error_buf_size,
  1750. "elements segment does not fit");
  1751. goto fail;
  1752. }
  1753. table_seg->base_offset.u.i32 =
  1754. globals[table_seg->base_offset.u.global_index]
  1755. .initial_value.i32;
  1756. }
  1757. /* check offset since length might negative */
  1758. if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
  1759. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  1760. table_seg->base_offset.u.i32, table->cur_size);
  1761. #if WASM_ENABLE_REF_TYPES != 0
  1762. set_error_buf(error_buf, error_buf_size,
  1763. "out of bounds table access");
  1764. #else
  1765. set_error_buf(error_buf, error_buf_size,
  1766. "elements segment does not fit");
  1767. #endif
  1768. goto fail;
  1769. }
  1770. /* check offset + length(could be zero) */
  1771. length = table_seg->function_count;
  1772. if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
  1773. LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
  1774. table_seg->base_offset.u.i32, length, table->cur_size);
  1775. #if WASM_ENABLE_REF_TYPES != 0
  1776. set_error_buf(error_buf, error_buf_size,
  1777. "out of bounds table access");
  1778. #else
  1779. set_error_buf(error_buf, error_buf_size,
  1780. "elements segment does not fit");
  1781. #endif
  1782. goto fail;
  1783. }
  1784. /**
  1785. * Check function index in the current module inst for now.
  1786. * will check the linked table inst owner in future.
  1787. * so loader check is enough
  1788. */
  1789. bh_memcpy_s(
  1790. table_data + table_seg->base_offset.u.i32,
  1791. (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32)
  1792. * sizeof(uint32)),
  1793. table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
  1794. }
  1795. /* Initialize the thread related data */
  1796. if (stack_size == 0)
  1797. stack_size = DEFAULT_WASM_STACK_SIZE;
  1798. #if WASM_ENABLE_SPEC_TEST != 0
  1799. if (stack_size < 128 * 1024)
  1800. stack_size = 128 * 1024;
  1801. #endif
  1802. module_inst->default_wasm_stack_size = stack_size;
  1803. if (module->malloc_function != (uint32)-1) {
  1804. module_inst->e->malloc_function =
  1805. &module_inst->e->functions[module->malloc_function];
  1806. }
  1807. if (module->free_function != (uint32)-1) {
  1808. module_inst->e->free_function =
  1809. &module_inst->e->functions[module->free_function];
  1810. }
  1811. if (module->retain_function != (uint32)-1) {
  1812. module_inst->e->retain_function =
  1813. &module_inst->e->functions[module->retain_function];
  1814. }
  1815. #if WASM_ENABLE_LIBC_WASI != 0
  1816. /* The sub-instance will get the wasi_ctx from main-instance */
  1817. if (!is_sub_inst) {
  1818. if (!wasm_runtime_init_wasi(
  1819. (WASMModuleInstanceCommon *)module_inst,
  1820. module->wasi_args.dir_list, module->wasi_args.dir_count,
  1821. module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
  1822. module->wasi_args.env, module->wasi_args.env_count,
  1823. module->wasi_args.addr_pool, module->wasi_args.addr_count,
  1824. module->wasi_args.ns_lookup_pool,
  1825. module->wasi_args.ns_lookup_count, module->wasi_args.argv,
  1826. module->wasi_args.argc, module->wasi_args.stdio[0],
  1827. module->wasi_args.stdio[1], module->wasi_args.stdio[2],
  1828. error_buf, error_buf_size)) {
  1829. goto fail;
  1830. }
  1831. }
  1832. #endif
  1833. #if WASM_ENABLE_DEBUG_INTERP != 0
  1834. if (!is_sub_inst) {
  1835. /* Add module instance into module's instance list */
  1836. os_mutex_lock(&module->instance_list_lock);
  1837. if (module->instance_list) {
  1838. LOG_WARNING(
  1839. "warning: multiple instances referencing to the same module "
  1840. "may cause unexpected behaviour during debugging");
  1841. }
  1842. module_inst->e->next = module->instance_list;
  1843. module->instance_list = module_inst;
  1844. os_mutex_unlock(&module->instance_list_lock);
  1845. }
  1846. #endif
  1847. /* Set running mode before executing wasm functions */
  1848. if (!set_running_mode(module_inst, wasm_runtime_get_default_running_mode(),
  1849. true)) {
  1850. set_error_buf(error_buf, error_buf_size,
  1851. "set instance running mode failed");
  1852. goto fail;
  1853. }
  1854. if (module->start_function != (uint32)-1) {
  1855. /* TODO: fix start function can be import function issue */
  1856. if (module->start_function >= module->import_function_count)
  1857. module_inst->e->start_function =
  1858. &module_inst->e->functions[module->start_function];
  1859. }
  1860. if (!execute_post_instantiate_functions(module_inst, is_sub_inst,
  1861. exec_env_main)) {
  1862. set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
  1863. goto fail;
  1864. }
  1865. #if WASM_ENABLE_MEMORY_TRACING != 0
  1866. wasm_runtime_dump_module_inst_mem_consumption(
  1867. (WASMModuleInstanceCommon *)module_inst);
  1868. #endif
  1869. (void)global_data_end;
  1870. return module_inst;
  1871. fail:
  1872. wasm_deinstantiate(module_inst, false);
  1873. return NULL;
  1874. }
  1875. void
  1876. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
  1877. {
  1878. if (!module_inst)
  1879. return;
  1880. if (module_inst->exec_env_singleton) {
  1881. /* wasm_exec_env_destroy will call
  1882. wasm_cluster_wait_for_all_except_self to wait for other
  1883. threads, so as to destroy their exec_envs and module
  1884. instances first, and avoid accessing the shared resources
  1885. of current module instance after it is deinstantiated. */
  1886. wasm_exec_env_destroy(module_inst->exec_env_singleton);
  1887. }
  1888. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  1889. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1890. && WASM_ENABLE_LAZY_JIT != 0)
  1891. /* Remove instance from module's instance list before freeing
  1892. func_ptrs and fast_jit_func_ptrs of the instance, to avoid
  1893. accessing the freed memory in the jit backend compilation
  1894. threads */
  1895. {
  1896. WASMModule *module = module_inst->module;
  1897. WASMModuleInstance *instance_prev = NULL, *instance;
  1898. os_mutex_lock(&module->instance_list_lock);
  1899. instance = module->instance_list;
  1900. while (instance) {
  1901. if (instance == module_inst) {
  1902. if (!instance_prev)
  1903. module->instance_list = instance->e->next;
  1904. else
  1905. instance_prev->e->next = instance->e->next;
  1906. break;
  1907. }
  1908. instance_prev = instance;
  1909. instance = instance->e->next;
  1910. }
  1911. os_mutex_unlock(&module->instance_list_lock);
  1912. }
  1913. #endif
  1914. #if WASM_ENABLE_JIT != 0
  1915. if (module_inst->func_ptrs)
  1916. wasm_runtime_free(module_inst->func_ptrs);
  1917. #endif
  1918. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1919. && WASM_ENABLE_LAZY_JIT != 0
  1920. if (module_inst->fast_jit_func_ptrs
  1921. && module_inst->fast_jit_func_ptrs
  1922. != module_inst->module->fast_jit_func_ptrs)
  1923. wasm_runtime_free(module_inst->fast_jit_func_ptrs);
  1924. #endif
  1925. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1926. if (module_inst->func_type_indexes)
  1927. wasm_runtime_free(module_inst->func_type_indexes);
  1928. #endif
  1929. #if WASM_ENABLE_MULTI_MODULE != 0
  1930. wasm_runtime_sub_module_deinstantiate(
  1931. (WASMModuleInstanceCommon *)module_inst);
  1932. #endif
  1933. if (module_inst->memory_count > 0)
  1934. memories_deinstantiate(module_inst, module_inst->memories,
  1935. module_inst->memory_count);
  1936. if (module_inst->import_func_ptrs) {
  1937. wasm_runtime_free(module_inst->import_func_ptrs);
  1938. }
  1939. tables_deinstantiate(module_inst);
  1940. functions_deinstantiate(module_inst->e->functions,
  1941. module_inst->e->function_count);
  1942. globals_deinstantiate(module_inst->e->globals);
  1943. export_functions_deinstantiate(module_inst->export_functions);
  1944. #if WASM_ENABLE_MULTI_MODULE != 0
  1945. export_globals_deinstantiate(module_inst->export_globals);
  1946. #endif
  1947. #if WASM_ENABLE_REF_TYPES != 0
  1948. wasm_externref_cleanup((WASMModuleInstanceCommon *)module_inst);
  1949. #endif
  1950. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1951. if (module_inst->frames) {
  1952. bh_vector_destroy(module_inst->frames);
  1953. wasm_runtime_free(module_inst->frames);
  1954. module_inst->frames = NULL;
  1955. }
  1956. #endif
  1957. if (module_inst->e->common.c_api_func_imports)
  1958. wasm_runtime_free(module_inst->e->common.c_api_func_imports);
  1959. if (!is_sub_inst) {
  1960. #if WASM_ENABLE_WASI_NN != 0
  1961. wasi_nn_destroy(module_inst);
  1962. #endif
  1963. wasm_native_call_context_dtors((WASMModuleInstanceCommon *)module_inst);
  1964. }
  1965. #if WASM_ENABLE_BULK_MEMORY != 0
  1966. bh_bitmap_delete(module_inst->e->common.data_dropped);
  1967. #endif
  1968. #if WASM_ENABLE_REF_TYPES != 0
  1969. bh_bitmap_delete(module_inst->e->common.elem_dropped);
  1970. #endif
  1971. wasm_runtime_free(module_inst);
  1972. }
  1973. WASMFunctionInstance *
  1974. wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name,
  1975. const char *signature)
  1976. {
  1977. uint32 i;
  1978. for (i = 0; i < module_inst->export_func_count; i++)
  1979. if (!strcmp(module_inst->export_functions[i].name, name))
  1980. return module_inst->export_functions[i].function;
  1981. (void)signature;
  1982. return NULL;
  1983. }
  1984. #if WASM_ENABLE_MULTI_MODULE != 0
  1985. WASMGlobalInstance *
  1986. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name)
  1987. {
  1988. uint32 i;
  1989. for (i = 0; i < module_inst->export_global_count; i++)
  1990. if (!strcmp(module_inst->export_globals[i].name, name))
  1991. return module_inst->export_globals[i].global;
  1992. return NULL;
  1993. }
  1994. WASMMemoryInstance *
  1995. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name)
  1996. {
  1997. /**
  1998. * using a strong assumption that one module instance only has
  1999. * one memory instance
  2000. */
  2001. (void)module_inst->export_memories;
  2002. return module_inst->memories[0];
  2003. }
  2004. WASMTableInstance *
  2005. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
  2006. {
  2007. /**
  2008. * using a strong assumption that one module instance only has
  2009. * one table instance
  2010. */
  2011. (void)module_inst->export_tables;
  2012. return module_inst->tables[0];
  2013. }
  2014. #endif
  2015. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2016. static void
  2017. call_wasm_with_hw_bound_check(WASMModuleInstance *module_inst,
  2018. WASMExecEnv *exec_env,
  2019. WASMFunctionInstance *function, unsigned argc,
  2020. uint32 argv[])
  2021. {
  2022. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  2023. WASMJmpBuf jmpbuf_node = { 0 }, *jmpbuf_node_pop;
  2024. uint32 page_size = os_getpagesize();
  2025. uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
  2026. WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  2027. uint8 *prev_top = exec_env->wasm_stack.s.top;
  2028. #ifdef BH_PLATFORM_WINDOWS
  2029. int result;
  2030. bool has_exception;
  2031. char exception[EXCEPTION_BUF_LEN];
  2032. #endif
  2033. bool ret = true;
  2034. /* Check native stack overflow firstly to ensure we have enough
  2035. native stack to run the following codes before actually calling
  2036. the aot function in invokeNative function. */
  2037. RECORD_STACK_USAGE(exec_env, (uint8 *)&exec_env_tls);
  2038. if ((uint8 *)&exec_env_tls < exec_env->native_stack_boundary
  2039. + page_size * (guard_page_count + 1)) {
  2040. wasm_set_exception(module_inst, "native stack overflow");
  2041. return;
  2042. }
  2043. if (!exec_env_tls) {
  2044. if (!os_thread_signal_inited()) {
  2045. wasm_set_exception(module_inst, "thread signal env not inited");
  2046. return;
  2047. }
  2048. /* Set thread handle and stack boundary if they haven't been set */
  2049. wasm_exec_env_set_thread_info(exec_env);
  2050. wasm_runtime_set_exec_env_tls(exec_env);
  2051. }
  2052. else {
  2053. if (exec_env_tls != exec_env) {
  2054. wasm_set_exception(module_inst, "invalid exec env");
  2055. return;
  2056. }
  2057. }
  2058. wasm_exec_env_push_jmpbuf(exec_env, &jmpbuf_node);
  2059. if (os_setjmp(jmpbuf_node.jmpbuf) == 0) {
  2060. #ifndef BH_PLATFORM_WINDOWS
  2061. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  2062. #else
  2063. __try {
  2064. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  2065. } __except (wasm_copy_exception(module_inst, NULL)
  2066. ? EXCEPTION_EXECUTE_HANDLER
  2067. : EXCEPTION_CONTINUE_SEARCH) {
  2068. /* Exception was thrown in wasm_exception_handler */
  2069. ret = false;
  2070. }
  2071. has_exception = wasm_copy_exception(module_inst, exception);
  2072. if (has_exception && strstr(exception, "native stack overflow")) {
  2073. /* After a stack overflow, the stack was left
  2074. in a damaged state, let the CRT repair it */
  2075. result = _resetstkoflw();
  2076. bh_assert(result != 0);
  2077. }
  2078. #endif
  2079. }
  2080. else {
  2081. /* Exception has been set in signal handler before calling longjmp */
  2082. ret = false;
  2083. }
  2084. /* Note: can't check wasm_get_exception(module_inst) here, there may be
  2085. * exception which is not caught by hardware (e.g. uninitialized elements),
  2086. * then the stack-frame is already freed inside wasm_interp_call_wasm */
  2087. if (!ret) {
  2088. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2089. if (wasm_interp_create_call_stack(exec_env)) {
  2090. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  2091. }
  2092. #endif
  2093. /* Restore operand frames */
  2094. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  2095. exec_env->wasm_stack.s.top = prev_top;
  2096. }
  2097. jmpbuf_node_pop = wasm_exec_env_pop_jmpbuf(exec_env);
  2098. bh_assert(&jmpbuf_node == jmpbuf_node_pop);
  2099. if (!exec_env->jmpbuf_stack_top) {
  2100. wasm_runtime_set_exec_env_tls(NULL);
  2101. }
  2102. if (!ret) {
  2103. os_sigreturn();
  2104. os_signal_unmask();
  2105. }
  2106. (void)jmpbuf_node_pop;
  2107. }
  2108. #define interp_call_wasm call_wasm_with_hw_bound_check
  2109. #else
  2110. #define interp_call_wasm wasm_interp_call_wasm
  2111. #endif
  2112. bool
  2113. wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
  2114. unsigned argc, uint32 argv[])
  2115. {
  2116. WASMModuleInstance *module_inst =
  2117. (WASMModuleInstance *)exec_env->module_inst;
  2118. #ifndef OS_ENABLE_HW_BOUND_CHECK
  2119. /* Set thread handle and stack boundary */
  2120. wasm_exec_env_set_thread_info(exec_env);
  2121. #else
  2122. /* Set thread info in call_wasm_with_hw_bound_check when
  2123. hw bound check is enabled */
  2124. #endif
  2125. /* Set exec env so it can be later retrieved from instance */
  2126. module_inst->e->common.cur_exec_env = exec_env;
  2127. interp_call_wasm(module_inst, exec_env, function, argc, argv);
  2128. return !wasm_copy_exception(module_inst, NULL);
  2129. }
  2130. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0
  2131. /* look for the function name */
  2132. static char *
  2133. get_func_name_from_index(const WASMModuleInstance *inst, uint32 func_index)
  2134. {
  2135. char *func_name = NULL;
  2136. WASMFunctionInstance *func_inst = inst->e->functions + func_index;
  2137. if (func_inst->is_import_func) {
  2138. func_name = func_inst->u.func_import->field_name;
  2139. }
  2140. else {
  2141. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2142. func_name = func_inst->u.func->field_name;
  2143. #endif
  2144. /* if custom name section is not generated,
  2145. search symbols from export table */
  2146. if (!func_name) {
  2147. unsigned j;
  2148. for (j = 0; j < inst->export_func_count; j++) {
  2149. WASMExportFuncInstance *export_func =
  2150. inst->export_functions + j;
  2151. if (export_func->function == func_inst) {
  2152. func_name = export_func->name;
  2153. break;
  2154. }
  2155. }
  2156. }
  2157. }
  2158. return func_name;
  2159. }
  2160. #endif /*WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0*/
  2161. #if WASM_ENABLE_PERF_PROFILING != 0
  2162. void
  2163. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
  2164. {
  2165. WASMFunctionInstance *func_inst;
  2166. char *func_name;
  2167. uint32 i;
  2168. os_printf("Performance profiler data:\n");
  2169. for (i = 0; i < module_inst->e->function_count; i++) {
  2170. func_inst = module_inst->e->functions + i;
  2171. if (func_inst->total_exec_cnt == 0)
  2172. continue;
  2173. func_name = get_func_name_from_index(module_inst, i);
  2174. if (func_name)
  2175. os_printf(
  2176. " func %s, execution time: %.3f ms, execution count: %" PRIu32
  2177. " times, children execution time: %.3f ms\n",
  2178. func_name, func_inst->total_exec_time / 1000.0f,
  2179. func_inst->total_exec_cnt,
  2180. func_inst->children_exec_time / 1000.0f);
  2181. else
  2182. os_printf(" func %" PRIu32
  2183. ", execution time: %.3f ms, execution count: %" PRIu32
  2184. " times, children execution time: %.3f ms\n",
  2185. i, func_inst->total_exec_time / 1000.0f,
  2186. func_inst->total_exec_cnt,
  2187. func_inst->children_exec_time / 1000.0f);
  2188. }
  2189. }
  2190. double
  2191. wasm_summarize_wasm_execute_time(const WASMModuleInstance *inst)
  2192. {
  2193. double ret = 0;
  2194. unsigned i;
  2195. for (i = 0; i < inst->e->function_count; i++) {
  2196. WASMFunctionInstance *func = inst->e->functions + i;
  2197. ret += (func->total_exec_time - func->children_exec_time) / 1000.0f;
  2198. }
  2199. return ret;
  2200. }
  2201. double
  2202. wasm_get_wasm_func_exec_time(const WASMModuleInstance *inst,
  2203. const char *func_name)
  2204. {
  2205. unsigned i;
  2206. for (i = 0; i < inst->e->function_count; i++) {
  2207. char *name_in_wasm = get_func_name_from_index(inst, i);
  2208. if (name_in_wasm && strcmp(name_in_wasm, func_name) == 0) {
  2209. WASMFunctionInstance *func = inst->e->functions + i;
  2210. return (func->total_exec_time - func->children_exec_time) / 1000.0f;
  2211. }
  2212. }
  2213. return -1.0;
  2214. }
  2215. #endif /*WASM_ENABLE_PERF_PROFILING != 0*/
  2216. uint32
  2217. wasm_module_malloc_internal(WASMModuleInstance *module_inst,
  2218. WASMExecEnv *exec_env, uint32 size,
  2219. void **p_native_addr)
  2220. {
  2221. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  2222. uint8 *addr = NULL;
  2223. uint32 offset = 0;
  2224. if (!memory) {
  2225. wasm_set_exception(module_inst, "uninitialized memory");
  2226. return 0;
  2227. }
  2228. if (memory->heap_handle) {
  2229. addr = mem_allocator_malloc(memory->heap_handle, size);
  2230. }
  2231. else if (module_inst->e->malloc_function && module_inst->e->free_function) {
  2232. if (!execute_malloc_function(
  2233. module_inst, exec_env, module_inst->e->malloc_function,
  2234. module_inst->e->retain_function, size, &offset)) {
  2235. return 0;
  2236. }
  2237. /* If we use app's malloc function,
  2238. the default memory may be changed while memory growing */
  2239. memory = wasm_get_default_memory(module_inst);
  2240. addr = offset ? memory->memory_data + offset : NULL;
  2241. }
  2242. if (!addr) {
  2243. if (memory->heap_handle
  2244. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  2245. wasm_runtime_show_app_heap_corrupted_prompt();
  2246. wasm_set_exception(module_inst, "app heap corrupted");
  2247. }
  2248. else {
  2249. LOG_WARNING("warning: allocate %u bytes memory failed", size);
  2250. }
  2251. return 0;
  2252. }
  2253. if (p_native_addr)
  2254. *p_native_addr = addr;
  2255. return (uint32)(addr - memory->memory_data);
  2256. }
  2257. uint32
  2258. wasm_module_realloc_internal(WASMModuleInstance *module_inst,
  2259. WASMExecEnv *exec_env, uint32 ptr, uint32 size,
  2260. void **p_native_addr)
  2261. {
  2262. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  2263. uint8 *addr = NULL;
  2264. if (!memory) {
  2265. wasm_set_exception(module_inst, "uninitialized memory");
  2266. return 0;
  2267. }
  2268. if (memory->heap_handle) {
  2269. addr = mem_allocator_realloc(
  2270. memory->heap_handle, ptr ? memory->memory_data + ptr : NULL, size);
  2271. }
  2272. /* Only support realloc in WAMR's app heap */
  2273. (void)exec_env;
  2274. if (!addr) {
  2275. if (memory->heap_handle
  2276. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  2277. wasm_set_exception(module_inst, "app heap corrupted");
  2278. }
  2279. else {
  2280. wasm_set_exception(module_inst, "out of memory");
  2281. }
  2282. return 0;
  2283. }
  2284. if (p_native_addr)
  2285. *p_native_addr = addr;
  2286. return (uint32)(addr - memory->memory_data);
  2287. }
  2288. void
  2289. wasm_module_free_internal(WASMModuleInstance *module_inst,
  2290. WASMExecEnv *exec_env, uint32 ptr)
  2291. {
  2292. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  2293. if (!memory) {
  2294. return;
  2295. }
  2296. if (ptr) {
  2297. uint8 *addr = memory->memory_data + ptr;
  2298. uint8 *memory_data_end;
  2299. /* memory->memory_data_end may be changed in memory grow */
  2300. SHARED_MEMORY_LOCK(memory);
  2301. memory_data_end = memory->memory_data_end;
  2302. SHARED_MEMORY_UNLOCK(memory);
  2303. if (memory->heap_handle && memory->heap_data <= addr
  2304. && addr < memory->heap_data_end) {
  2305. mem_allocator_free(memory->heap_handle, addr);
  2306. }
  2307. else if (module_inst->e->malloc_function
  2308. && module_inst->e->free_function && memory->memory_data <= addr
  2309. && addr < memory_data_end) {
  2310. execute_free_function(module_inst, exec_env,
  2311. module_inst->e->free_function, ptr);
  2312. }
  2313. }
  2314. }
  2315. uint32
  2316. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  2317. void **p_native_addr)
  2318. {
  2319. return wasm_module_malloc_internal(module_inst, NULL, size, p_native_addr);
  2320. }
  2321. uint32
  2322. wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
  2323. void **p_native_addr)
  2324. {
  2325. return wasm_module_realloc_internal(module_inst, NULL, ptr, size,
  2326. p_native_addr);
  2327. }
  2328. void
  2329. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
  2330. {
  2331. wasm_module_free_internal(module_inst, NULL, ptr);
  2332. }
  2333. uint32
  2334. wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src,
  2335. uint32 size)
  2336. {
  2337. char *buffer;
  2338. uint32 buffer_offset =
  2339. wasm_module_malloc(module_inst, size, (void **)&buffer);
  2340. if (buffer_offset != 0) {
  2341. buffer = wasm_runtime_addr_app_to_native(
  2342. (WASMModuleInstanceCommon *)module_inst, buffer_offset);
  2343. bh_memcpy_s(buffer, size, src, size);
  2344. }
  2345. return buffer_offset;
  2346. }
  2347. #if WASM_ENABLE_REF_TYPES != 0
  2348. bool
  2349. wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx,
  2350. uint32 inc_size, uint32 init_val)
  2351. {
  2352. uint32 total_size, *new_table_data_start, i;
  2353. WASMTableInstance *table_inst;
  2354. if (!inc_size) {
  2355. return true;
  2356. }
  2357. bh_assert(table_idx < module_inst->table_count);
  2358. table_inst = wasm_get_table_inst(module_inst, table_idx);
  2359. if (!table_inst) {
  2360. return false;
  2361. }
  2362. if (inc_size > UINT32_MAX - table_inst->cur_size) {
  2363. return false;
  2364. }
  2365. total_size = table_inst->cur_size + inc_size;
  2366. if (total_size > table_inst->max_size) {
  2367. return false;
  2368. }
  2369. /* fill in */
  2370. new_table_data_start = table_inst->elems + table_inst->cur_size;
  2371. for (i = 0; i < inc_size; ++i) {
  2372. new_table_data_start[i] = init_val;
  2373. }
  2374. table_inst->cur_size = total_size;
  2375. return true;
  2376. }
  2377. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  2378. static bool
  2379. call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2380. uint32 argc, uint32 argv[], bool check_type_idx, uint32 type_idx)
  2381. {
  2382. WASMModuleInstance *module_inst = NULL;
  2383. WASMTableInstance *table_inst = NULL;
  2384. uint32 func_idx = 0;
  2385. WASMFunctionInstance *func_inst = NULL;
  2386. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  2387. bh_assert(module_inst);
  2388. table_inst = module_inst->tables[tbl_idx];
  2389. if (!table_inst) {
  2390. wasm_set_exception(module_inst, "unknown table");
  2391. goto got_exception;
  2392. }
  2393. if (elem_idx >= table_inst->cur_size) {
  2394. wasm_set_exception(module_inst, "undefined element");
  2395. goto got_exception;
  2396. }
  2397. func_idx = table_inst->elems[elem_idx];
  2398. if (func_idx == NULL_REF) {
  2399. wasm_set_exception(module_inst, "uninitialized element");
  2400. goto got_exception;
  2401. }
  2402. /**
  2403. * we insist to call functions owned by the module itself
  2404. **/
  2405. if (func_idx >= module_inst->e->function_count) {
  2406. wasm_set_exception(module_inst, "unknown function");
  2407. goto got_exception;
  2408. }
  2409. func_inst = module_inst->e->functions + func_idx;
  2410. if (check_type_idx) {
  2411. WASMType *cur_type = module_inst->module->types[type_idx];
  2412. WASMType *cur_func_type;
  2413. if (func_inst->is_import_func)
  2414. cur_func_type = func_inst->u.func_import->func_type;
  2415. else
  2416. cur_func_type = func_inst->u.func->func_type;
  2417. if (cur_type != cur_func_type) {
  2418. wasm_set_exception(module_inst, "indirect call type mismatch");
  2419. goto got_exception;
  2420. }
  2421. }
  2422. interp_call_wasm(module_inst, exec_env, func_inst, argc, argv);
  2423. return !wasm_copy_exception(module_inst, NULL);
  2424. got_exception:
  2425. return false;
  2426. }
  2427. bool
  2428. wasm_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2429. uint32 argc, uint32 argv[])
  2430. {
  2431. return call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, false, 0);
  2432. }
  2433. #if WASM_ENABLE_THREAD_MGR != 0
  2434. bool
  2435. wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size)
  2436. {
  2437. WASMModuleInstance *module_inst =
  2438. (WASMModuleInstance *)exec_env->module_inst;
  2439. uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
  2440. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
  2441. /* Check the aux stack space */
  2442. uint32 data_end = module_inst->module->aux_data_end;
  2443. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  2444. bool is_stack_before_data = stack_bottom < data_end ? true : false;
  2445. if ((is_stack_before_data && (size > start_offset))
  2446. || ((!is_stack_before_data) && (start_offset - data_end < size)))
  2447. return false;
  2448. #endif
  2449. if (stack_top_idx != (uint32)-1) {
  2450. /* The aux stack top is a wasm global,
  2451. set the initial value for the global */
  2452. uint8 *global_addr =
  2453. module_inst->global_data
  2454. + module_inst->e->globals[stack_top_idx].data_offset;
  2455. *(int32 *)global_addr = start_offset;
  2456. /* The aux stack boundary is a constant value,
  2457. set the value to exec_env */
  2458. exec_env->aux_stack_boundary.boundary = start_offset - size;
  2459. exec_env->aux_stack_bottom.bottom = start_offset;
  2460. return true;
  2461. }
  2462. return false;
  2463. }
  2464. bool
  2465. wasm_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size)
  2466. {
  2467. WASMModuleInstance *module_inst =
  2468. (WASMModuleInstance *)exec_env->module_inst;
  2469. /* The aux stack information is resolved in loader
  2470. and store in module */
  2471. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  2472. uint32 total_aux_stack_size = module_inst->module->aux_stack_size;
  2473. if (stack_bottom != 0 && total_aux_stack_size != 0) {
  2474. if (start_offset)
  2475. *start_offset = stack_bottom;
  2476. if (size)
  2477. *size = total_aux_stack_size;
  2478. return true;
  2479. }
  2480. return false;
  2481. }
  2482. #endif
  2483. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  2484. void
  2485. wasm_get_module_mem_consumption(const WASMModule *module,
  2486. WASMModuleMemConsumption *mem_conspn)
  2487. {
  2488. uint32 i, size;
  2489. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2490. mem_conspn->module_struct_size = sizeof(WASMModule);
  2491. mem_conspn->types_size = sizeof(WASMType *) * module->type_count;
  2492. for (i = 0; i < module->type_count; i++) {
  2493. WASMType *type = module->types[i];
  2494. size = offsetof(WASMType, types)
  2495. + sizeof(uint8) * (type->param_count + type->result_count);
  2496. mem_conspn->types_size += size;
  2497. }
  2498. mem_conspn->imports_size = sizeof(WASMImport) * module->import_count;
  2499. mem_conspn->functions_size =
  2500. sizeof(WASMFunction *) * module->function_count;
  2501. for (i = 0; i < module->function_count; i++) {
  2502. WASMFunction *func = module->functions[i];
  2503. WASMType *type = func->func_type;
  2504. size = sizeof(WASMFunction) + func->local_count
  2505. + sizeof(uint16) * (type->param_count + func->local_count);
  2506. #if WASM_ENABLE_FAST_INTERP != 0
  2507. size +=
  2508. func->code_compiled_size + sizeof(uint32) * func->const_cell_num;
  2509. #endif
  2510. mem_conspn->functions_size += size;
  2511. }
  2512. mem_conspn->tables_size = sizeof(WASMTable) * module->table_count;
  2513. mem_conspn->memories_size = sizeof(WASMMemory) * module->memory_count;
  2514. mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count;
  2515. mem_conspn->exports_size = sizeof(WASMExport) * module->export_count;
  2516. mem_conspn->table_segs_size =
  2517. sizeof(WASMTableSeg) * module->table_seg_count;
  2518. for (i = 0; i < module->table_seg_count; i++) {
  2519. WASMTableSeg *table_seg = &module->table_segments[i];
  2520. mem_conspn->tables_size += sizeof(uint32) * table_seg->function_count;
  2521. }
  2522. mem_conspn->data_segs_size = sizeof(WASMDataSeg *) * module->data_seg_count;
  2523. for (i = 0; i < module->data_seg_count; i++) {
  2524. mem_conspn->data_segs_size += sizeof(WASMDataSeg);
  2525. }
  2526. if (module->const_str_list) {
  2527. StringNode *node = module->const_str_list, *node_next;
  2528. while (node) {
  2529. node_next = node->next;
  2530. mem_conspn->const_strs_size +=
  2531. sizeof(StringNode) + strlen(node->str) + 1;
  2532. node = node_next;
  2533. }
  2534. }
  2535. mem_conspn->total_size += mem_conspn->module_struct_size;
  2536. mem_conspn->total_size += mem_conspn->types_size;
  2537. mem_conspn->total_size += mem_conspn->imports_size;
  2538. mem_conspn->total_size += mem_conspn->functions_size;
  2539. mem_conspn->total_size += mem_conspn->tables_size;
  2540. mem_conspn->total_size += mem_conspn->memories_size;
  2541. mem_conspn->total_size += mem_conspn->globals_size;
  2542. mem_conspn->total_size += mem_conspn->exports_size;
  2543. mem_conspn->total_size += mem_conspn->table_segs_size;
  2544. mem_conspn->total_size += mem_conspn->data_segs_size;
  2545. mem_conspn->total_size += mem_conspn->const_strs_size;
  2546. }
  2547. void
  2548. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
  2549. WASMModuleInstMemConsumption *mem_conspn)
  2550. {
  2551. uint32 i, size;
  2552. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2553. mem_conspn->module_inst_struct_size = (uint8 *)module_inst->e
  2554. - (uint8 *)module_inst
  2555. + sizeof(WASMModuleInstanceExtra);
  2556. mem_conspn->memories_size =
  2557. sizeof(WASMMemoryInstance *) * module_inst->memory_count;
  2558. for (i = 0; i < module_inst->memory_count; i++) {
  2559. WASMMemoryInstance *memory = module_inst->memories[i];
  2560. size = memory->num_bytes_per_page * memory->cur_page_count;
  2561. mem_conspn->memories_size += size;
  2562. mem_conspn->app_heap_size += memory->heap_data_end - memory->heap_data;
  2563. /* size of app heap structure */
  2564. mem_conspn->memories_size += mem_allocator_get_heap_struct_size();
  2565. /* Module instance structures have been appened into the end of
  2566. module instance */
  2567. }
  2568. mem_conspn->tables_size =
  2569. sizeof(WASMTableInstance *) * module_inst->table_count;
  2570. /* Table instance structures and table elements have been appened into
  2571. the end of module instance */
  2572. mem_conspn->functions_size =
  2573. sizeof(WASMFunctionInstance) * module_inst->e->function_count;
  2574. mem_conspn->globals_size =
  2575. sizeof(WASMGlobalInstance) * module_inst->e->global_count;
  2576. /* Global data has been appened into the end of module instance */
  2577. mem_conspn->exports_size =
  2578. sizeof(WASMExportFuncInstance) * module_inst->export_func_count;
  2579. mem_conspn->total_size += mem_conspn->module_inst_struct_size;
  2580. mem_conspn->total_size += mem_conspn->memories_size;
  2581. mem_conspn->total_size += mem_conspn->functions_size;
  2582. mem_conspn->total_size += mem_conspn->tables_size;
  2583. mem_conspn->total_size += mem_conspn->globals_size;
  2584. mem_conspn->total_size += mem_conspn->exports_size;
  2585. }
  2586. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
  2587. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  2588. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2589. bool
  2590. wasm_interp_create_call_stack(struct WASMExecEnv *exec_env)
  2591. {
  2592. WASMModuleInstance *module_inst =
  2593. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  2594. WASMInterpFrame *first_frame,
  2595. *cur_frame = wasm_exec_env_get_cur_frame(exec_env);
  2596. uint32 n = 0;
  2597. /* count frames includes a function */
  2598. first_frame = cur_frame;
  2599. while (cur_frame) {
  2600. if (cur_frame->function) {
  2601. n++;
  2602. }
  2603. cur_frame = cur_frame->prev_frame;
  2604. }
  2605. /* release previous stack frames and create new ones */
  2606. if (!bh_vector_destroy(module_inst->frames)
  2607. || !bh_vector_init(module_inst->frames, n, sizeof(WASMCApiFrame),
  2608. false)) {
  2609. return false;
  2610. }
  2611. cur_frame = first_frame;
  2612. n = 0;
  2613. while (cur_frame) {
  2614. WASMCApiFrame frame = { 0 };
  2615. WASMFunctionInstance *func_inst = cur_frame->function;
  2616. const char *func_name = NULL;
  2617. const uint8 *func_code_base = NULL;
  2618. if (!func_inst) {
  2619. cur_frame = cur_frame->prev_frame;
  2620. continue;
  2621. }
  2622. /* place holder, will overwrite it in wasm_c_api */
  2623. frame.instance = module_inst;
  2624. frame.module_offset = 0;
  2625. frame.func_index = (uint32)(func_inst - module_inst->e->functions);
  2626. func_code_base = wasm_get_func_code(func_inst);
  2627. if (!cur_frame->ip || !func_code_base) {
  2628. frame.func_offset = 0;
  2629. }
  2630. else {
  2631. frame.func_offset = (uint32)(cur_frame->ip - func_code_base);
  2632. }
  2633. func_name = get_func_name_from_index(module_inst, frame.func_index);
  2634. frame.func_name_wp = func_name;
  2635. if (!bh_vector_append(module_inst->frames, &frame)) {
  2636. bh_vector_destroy(module_inst->frames);
  2637. return false;
  2638. }
  2639. cur_frame = cur_frame->prev_frame;
  2640. n++;
  2641. }
  2642. return true;
  2643. }
  2644. #define PRINT_OR_DUMP() \
  2645. do { \
  2646. total_len += \
  2647. wasm_runtime_dump_line_buf_impl(line_buf, print, &buf, &len); \
  2648. if ((!print) && buf && (len == 0)) { \
  2649. exception_unlock(module_inst); \
  2650. return total_len; \
  2651. } \
  2652. } while (0)
  2653. uint32
  2654. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf,
  2655. uint32 len)
  2656. {
  2657. WASMModuleInstance *module_inst =
  2658. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  2659. uint32 n = 0, total_len = 0, total_frames;
  2660. /* reserve 256 bytes for line buffer, any line longer than 256 bytes
  2661. * will be truncated */
  2662. char line_buf[256];
  2663. if (!module_inst->frames) {
  2664. return 0;
  2665. }
  2666. total_frames = (uint32)bh_vector_size(module_inst->frames);
  2667. if (total_frames == 0) {
  2668. return 0;
  2669. }
  2670. exception_lock(module_inst);
  2671. snprintf(line_buf, sizeof(line_buf), "\n");
  2672. PRINT_OR_DUMP();
  2673. while (n < total_frames) {
  2674. WASMCApiFrame frame = { 0 };
  2675. uint32 line_length, i;
  2676. if (!bh_vector_get(module_inst->frames, n, &frame)) {
  2677. exception_unlock(module_inst);
  2678. return 0;
  2679. }
  2680. /* function name not exported, print number instead */
  2681. if (frame.func_name_wp == NULL) {
  2682. line_length =
  2683. snprintf(line_buf, sizeof(line_buf),
  2684. "#%02" PRIu32 " $f%" PRIu32 "\n", n, frame.func_index);
  2685. }
  2686. else {
  2687. line_length =
  2688. snprintf(line_buf, sizeof(line_buf), "#%02" PRIu32 " %s\n", n,
  2689. frame.func_name_wp);
  2690. }
  2691. if (line_length >= sizeof(line_buf)) {
  2692. uint32 line_buffer_len = sizeof(line_buf);
  2693. /* If line too long, ensure the last character is '\n' */
  2694. for (i = line_buffer_len - 5; i < line_buffer_len - 2; i++) {
  2695. line_buf[i] = '.';
  2696. }
  2697. line_buf[line_buffer_len - 2] = '\n';
  2698. }
  2699. PRINT_OR_DUMP();
  2700. n++;
  2701. }
  2702. snprintf(line_buf, sizeof(line_buf), "\n");
  2703. PRINT_OR_DUMP();
  2704. exception_unlock(module_inst);
  2705. return total_len + 1;
  2706. }
  2707. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
  2708. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  2709. || WASM_ENABLE_WAMR_COMPILER != 0
  2710. void
  2711. jit_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id)
  2712. {
  2713. if (id != EXCE_ALREADY_THROWN)
  2714. wasm_set_exception_with_id(module_inst, id);
  2715. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2716. wasm_runtime_access_exce_check_guard_page();
  2717. #endif
  2718. }
  2719. bool
  2720. jit_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  2721. uint32 app_buf_addr, uint32 app_buf_size,
  2722. void **p_native_addr)
  2723. {
  2724. bool ret = wasm_check_app_addr_and_convert(
  2725. module_inst, is_str, app_buf_addr, app_buf_size, p_native_addr);
  2726. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2727. if (!ret)
  2728. wasm_runtime_access_exce_check_guard_page();
  2729. #endif
  2730. return ret;
  2731. }
  2732. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  2733. || WASM_ENABLE_WAMR_COMPILER != 0 */
  2734. #if WASM_ENABLE_FAST_JIT != 0
  2735. bool
  2736. fast_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2737. uint32 type_idx, uint32 argc, uint32 *argv)
  2738. {
  2739. return call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, true,
  2740. type_idx);
  2741. }
  2742. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  2743. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  2744. bool
  2745. llvm_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2746. uint32 argc, uint32 *argv)
  2747. {
  2748. bool ret;
  2749. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  2750. ret = call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, false, 0);
  2751. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2752. if (!ret)
  2753. wasm_runtime_access_exce_check_guard_page();
  2754. #endif
  2755. return ret;
  2756. }
  2757. bool
  2758. llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
  2759. uint32 *argv)
  2760. {
  2761. WASMModuleInstance *module_inst;
  2762. WASMModule *module;
  2763. uint32 *func_type_indexes;
  2764. uint32 func_type_idx;
  2765. WASMType *func_type;
  2766. void *func_ptr;
  2767. WASMFunctionImport *import_func;
  2768. CApiFuncImport *c_api_func_import = NULL;
  2769. const char *signature;
  2770. void *attachment;
  2771. char buf[96];
  2772. bool ret = false;
  2773. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  2774. module_inst = (WASMModuleInstance *)wasm_runtime_get_module_inst(exec_env);
  2775. module = module_inst->module;
  2776. func_type_indexes = module_inst->func_type_indexes;
  2777. func_type_idx = func_type_indexes[func_idx];
  2778. func_type = module->types[func_type_idx];
  2779. func_ptr = module_inst->func_ptrs[func_idx];
  2780. bh_assert(func_idx < module->import_function_count);
  2781. import_func = &module->import_functions[func_idx].u.function;
  2782. if (import_func->call_conv_wasm_c_api) {
  2783. if (module_inst->e->common.c_api_func_imports) {
  2784. c_api_func_import =
  2785. module_inst->e->common.c_api_func_imports + func_idx;
  2786. func_ptr = c_api_func_import->func_ptr_linked;
  2787. }
  2788. else {
  2789. c_api_func_import = NULL;
  2790. func_ptr = NULL;
  2791. }
  2792. }
  2793. if (!func_ptr) {
  2794. snprintf(buf, sizeof(buf),
  2795. "failed to call unlinked import function (%s, %s)",
  2796. import_func->module_name, import_func->field_name);
  2797. wasm_set_exception(module_inst, buf);
  2798. goto fail;
  2799. }
  2800. attachment = import_func->attachment;
  2801. if (import_func->call_conv_wasm_c_api) {
  2802. ret = wasm_runtime_invoke_c_api_native(
  2803. (WASMModuleInstanceCommon *)module_inst, func_ptr, func_type, argc,
  2804. argv, c_api_func_import->with_env_arg, c_api_func_import->env_arg);
  2805. }
  2806. else if (!import_func->call_conv_raw) {
  2807. signature = import_func->signature;
  2808. ret =
  2809. wasm_runtime_invoke_native(exec_env, func_ptr, func_type, signature,
  2810. attachment, argv, argc, argv);
  2811. }
  2812. else {
  2813. signature = import_func->signature;
  2814. ret = wasm_runtime_invoke_native_raw(exec_env, func_ptr, func_type,
  2815. signature, attachment, argv, argc,
  2816. argv);
  2817. }
  2818. fail:
  2819. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2820. if (!ret)
  2821. wasm_runtime_access_exce_check_guard_page();
  2822. #endif
  2823. return ret;
  2824. }
  2825. #if WASM_ENABLE_BULK_MEMORY != 0
  2826. bool
  2827. llvm_jit_memory_init(WASMModuleInstance *module_inst, uint32 seg_index,
  2828. uint32 offset, uint32 len, uint32 dst)
  2829. {
  2830. WASMMemoryInstance *memory_inst;
  2831. WASMModule *module;
  2832. uint8 *data;
  2833. uint8 *maddr;
  2834. uint64 seg_len;
  2835. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  2836. memory_inst = wasm_get_default_memory(module_inst);
  2837. if (bh_bitmap_get_bit(module_inst->e->common.data_dropped, seg_index)) {
  2838. seg_len = 0;
  2839. data = NULL;
  2840. }
  2841. else {
  2842. module = module_inst->module;
  2843. seg_len = module->data_segments[seg_index]->data_length;
  2844. data = module->data_segments[seg_index]->data;
  2845. }
  2846. if (!wasm_runtime_validate_app_addr((WASMModuleInstanceCommon *)module_inst,
  2847. dst, len))
  2848. return false;
  2849. if ((uint64)offset + (uint64)len > seg_len) {
  2850. wasm_set_exception(module_inst, "out of bounds memory access");
  2851. return false;
  2852. }
  2853. maddr = wasm_runtime_addr_app_to_native(
  2854. (WASMModuleInstanceCommon *)module_inst, dst);
  2855. SHARED_MEMORY_LOCK(memory_inst);
  2856. bh_memcpy_s(maddr, memory_inst->memory_data_size - dst, data + offset, len);
  2857. SHARED_MEMORY_UNLOCK(memory_inst);
  2858. return true;
  2859. }
  2860. bool
  2861. llvm_jit_data_drop(WASMModuleInstance *module_inst, uint32 seg_index)
  2862. {
  2863. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  2864. bh_bitmap_set_bit(module_inst->e->common.data_dropped, seg_index);
  2865. /* Currently we can't free the dropped data segment
  2866. as they are stored in wasm bytecode */
  2867. return true;
  2868. }
  2869. #endif /* end of WASM_ENABLE_BULK_MEMORY != 0 */
  2870. #if WASM_ENABLE_REF_TYPES != 0
  2871. void
  2872. llvm_jit_drop_table_seg(WASMModuleInstance *module_inst, uint32 tbl_seg_idx)
  2873. {
  2874. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  2875. bh_bitmap_set_bit(module_inst->e->common.elem_dropped, tbl_seg_idx);
  2876. }
  2877. void
  2878. llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx,
  2879. uint32 tbl_seg_idx, uint32 length, uint32 src_offset,
  2880. uint32 dst_offset)
  2881. {
  2882. WASMTableInstance *tbl_inst;
  2883. WASMTableSeg *tbl_seg;
  2884. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  2885. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  2886. tbl_seg = module_inst->module->table_segments + tbl_seg_idx;
  2887. bh_assert(tbl_inst);
  2888. bh_assert(tbl_seg);
  2889. if (offset_len_out_of_bounds(src_offset, length, tbl_seg->function_count)
  2890. || offset_len_out_of_bounds(dst_offset, length, tbl_inst->cur_size)) {
  2891. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2892. return;
  2893. }
  2894. if (!length) {
  2895. return;
  2896. }
  2897. if (bh_bitmap_get_bit(module_inst->e->common.elem_dropped, tbl_seg_idx)) {
  2898. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2899. return;
  2900. }
  2901. if (!wasm_elem_is_passive(tbl_seg->mode)) {
  2902. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2903. return;
  2904. }
  2905. bh_memcpy_s((uint8 *)tbl_inst + offsetof(WASMTableInstance, elems)
  2906. + dst_offset * sizeof(uint32),
  2907. (uint32)sizeof(uint32) * (tbl_inst->cur_size - dst_offset),
  2908. tbl_seg->func_indexes + src_offset,
  2909. (uint32)(length * sizeof(uint32)));
  2910. }
  2911. void
  2912. llvm_jit_table_copy(WASMModuleInstance *module_inst, uint32 src_tbl_idx,
  2913. uint32 dst_tbl_idx, uint32 length, uint32 src_offset,
  2914. uint32 dst_offset)
  2915. {
  2916. WASMTableInstance *src_tbl_inst;
  2917. WASMTableInstance *dst_tbl_inst;
  2918. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  2919. src_tbl_inst = wasm_get_table_inst(module_inst, src_tbl_idx);
  2920. dst_tbl_inst = wasm_get_table_inst(module_inst, dst_tbl_idx);
  2921. bh_assert(src_tbl_inst);
  2922. bh_assert(dst_tbl_inst);
  2923. if (offset_len_out_of_bounds(dst_offset, length, dst_tbl_inst->cur_size)
  2924. || offset_len_out_of_bounds(src_offset, length,
  2925. src_tbl_inst->cur_size)) {
  2926. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2927. return;
  2928. }
  2929. /* if src_offset >= dst_offset, copy from front to back */
  2930. /* if src_offset < dst_offset, copy from back to front */
  2931. /* merge all together */
  2932. bh_memmove_s((uint8 *)dst_tbl_inst + offsetof(WASMTableInstance, elems)
  2933. + sizeof(uint32) * dst_offset,
  2934. (uint32)sizeof(uint32) * (dst_tbl_inst->cur_size - dst_offset),
  2935. (uint8 *)src_tbl_inst + offsetof(WASMTableInstance, elems)
  2936. + sizeof(uint32) * src_offset,
  2937. (uint32)sizeof(uint32) * length);
  2938. }
  2939. void
  2940. llvm_jit_table_fill(WASMModuleInstance *module_inst, uint32 tbl_idx,
  2941. uint32 length, uint32 val, uint32 data_offset)
  2942. {
  2943. WASMTableInstance *tbl_inst;
  2944. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  2945. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  2946. bh_assert(tbl_inst);
  2947. if (offset_len_out_of_bounds(data_offset, length, tbl_inst->cur_size)) {
  2948. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2949. return;
  2950. }
  2951. for (; length != 0; data_offset++, length--) {
  2952. tbl_inst->elems[data_offset] = val;
  2953. }
  2954. }
  2955. uint32
  2956. llvm_jit_table_grow(WASMModuleInstance *module_inst, uint32 tbl_idx,
  2957. uint32 inc_size, uint32 init_val)
  2958. {
  2959. WASMTableInstance *tbl_inst;
  2960. uint32 i, orig_size, total_size;
  2961. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  2962. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  2963. if (!tbl_inst) {
  2964. return (uint32)-1;
  2965. }
  2966. orig_size = tbl_inst->cur_size;
  2967. if (!inc_size) {
  2968. return orig_size;
  2969. }
  2970. if (tbl_inst->cur_size > UINT32_MAX - inc_size) { /* integer overflow */
  2971. return (uint32)-1;
  2972. }
  2973. total_size = tbl_inst->cur_size + inc_size;
  2974. if (total_size > tbl_inst->max_size) {
  2975. return (uint32)-1;
  2976. }
  2977. /* fill in */
  2978. for (i = 0; i < inc_size; ++i) {
  2979. tbl_inst->elems[tbl_inst->cur_size + i] = init_val;
  2980. }
  2981. tbl_inst->cur_size = total_size;
  2982. return orig_size;
  2983. }
  2984. #endif /* end of WASM_ENABLE_REF_TYPES != 0 */
  2985. #if WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_PERF_PROFILING != 0
  2986. bool
  2987. llvm_jit_alloc_frame(WASMExecEnv *exec_env, uint32 func_index)
  2988. {
  2989. WASMModuleInstance *module_inst;
  2990. WASMInterpFrame *frame;
  2991. uint32 size;
  2992. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  2993. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  2994. size = wasm_interp_interp_frame_size(0);
  2995. frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
  2996. if (!frame) {
  2997. wasm_set_exception(module_inst, "wasm operand stack overflow");
  2998. return false;
  2999. }
  3000. frame->function = module_inst->e->functions + func_index;
  3001. frame->ip = NULL;
  3002. frame->sp = frame->lp;
  3003. #if WASM_ENABLE_PERF_PROFILING != 0
  3004. frame->time_started = os_time_thread_cputime_us();
  3005. #endif
  3006. frame->prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  3007. wasm_exec_env_set_cur_frame(exec_env, frame);
  3008. return true;
  3009. }
  3010. void
  3011. llvm_jit_free_frame(WASMExecEnv *exec_env)
  3012. {
  3013. WASMInterpFrame *frame;
  3014. WASMInterpFrame *prev_frame;
  3015. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  3016. frame = wasm_exec_env_get_cur_frame(exec_env);
  3017. prev_frame = frame->prev_frame;
  3018. #if WASM_ENABLE_PERF_PROFILING != 0
  3019. if (frame->function) {
  3020. frame->function->total_exec_time +=
  3021. os_time_thread_cputime_us() - frame->time_started;
  3022. frame->function->total_exec_cnt++;
  3023. /* parent function */
  3024. if (prev_frame)
  3025. prev_frame->function->children_exec_time =
  3026. frame->function->total_exec_time;
  3027. }
  3028. #endif
  3029. wasm_exec_env_free_wasm_frame(exec_env, frame);
  3030. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  3031. }
  3032. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 \
  3033. || WASM_ENABLE_PERF_PROFILING != 0 */
  3034. #endif /* end of WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 */
  3035. #if WASM_ENABLE_LIBC_WASI != 0 && WASM_ENABLE_MULTI_MODULE != 0
  3036. void
  3037. wasm_propagate_wasi_args(WASMModule *module)
  3038. {
  3039. if (!module->import_count)
  3040. return;
  3041. bh_assert(&module->import_module_list_head);
  3042. WASMRegisteredModule *node =
  3043. bh_list_first_elem(&module->import_module_list_head);
  3044. while (node) {
  3045. WASIArguments *wasi_args_impt_mod =
  3046. &((WASMModule *)(node->module))->wasi_args;
  3047. bh_assert(wasi_args_impt_mod);
  3048. bh_memcpy_s(wasi_args_impt_mod, sizeof(WASIArguments),
  3049. &module->wasi_args, sizeof(WASIArguments));
  3050. node = bh_list_elem_next(node);
  3051. }
  3052. }
  3053. #endif