wasm_runtime.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496
  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. for (i = 0; i < module->data_seg_count; i++) {
  1487. if (!module->data_segments[i]->is_passive)
  1488. bh_bitmap_set_bit(module_inst->e->common.data_dropped, i);
  1489. }
  1490. }
  1491. #endif
  1492. #if WASM_ENABLE_REF_TYPES != 0
  1493. if (module->table_seg_count > 0) {
  1494. module_inst->e->common.elem_dropped =
  1495. bh_bitmap_new(0, module->table_seg_count);
  1496. if (module_inst->e->common.elem_dropped == NULL) {
  1497. LOG_DEBUG("failed to allocate bitmaps");
  1498. set_error_buf(error_buf, error_buf_size,
  1499. "failed to allocate bitmaps");
  1500. goto fail;
  1501. }
  1502. for (i = 0; i < module->table_seg_count; i++) {
  1503. if (wasm_elem_is_active(module->table_segments[i].mode))
  1504. bh_bitmap_set_bit(module_inst->e->common.elem_dropped, i);
  1505. }
  1506. }
  1507. #endif
  1508. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1509. if (!(module_inst->frames = runtime_malloc((uint64)sizeof(Vector),
  1510. error_buf, error_buf_size))) {
  1511. goto fail;
  1512. }
  1513. #endif
  1514. /* Instantiate global firstly to get the mutable data size */
  1515. global_count = module->import_global_count + module->global_count;
  1516. if (global_count
  1517. && !(globals = globals_instantiate(module, module_inst, error_buf,
  1518. error_buf_size))) {
  1519. goto fail;
  1520. }
  1521. module_inst->e->global_count = global_count;
  1522. module_inst->e->globals = globals;
  1523. module_inst->global_data = (uint8 *)module_inst + module_inst_struct_size
  1524. + module_inst_mem_inst_size;
  1525. module_inst->global_data_size = module->global_data_size;
  1526. first_table = (WASMTableInstance *)(module_inst->global_data
  1527. + module->global_data_size);
  1528. module_inst->memory_count =
  1529. module->import_memory_count + module->memory_count;
  1530. module_inst->table_count = module->import_table_count + module->table_count;
  1531. module_inst->e->function_count =
  1532. module->import_function_count + module->function_count;
  1533. /* export */
  1534. module_inst->export_func_count = get_export_count(module, EXPORT_KIND_FUNC);
  1535. #if WASM_ENABLE_MULTI_MODULE != 0
  1536. module_inst->export_table_count =
  1537. get_export_count(module, EXPORT_KIND_TABLE);
  1538. module_inst->export_memory_count =
  1539. get_export_count(module, EXPORT_KIND_MEMORY);
  1540. module_inst->export_global_count =
  1541. get_export_count(module, EXPORT_KIND_GLOBAL);
  1542. #endif
  1543. /* Instantiate memories/tables/functions */
  1544. if ((module_inst->memory_count > 0
  1545. && !(module_inst->memories =
  1546. memories_instantiate(module, module_inst, parent, heap_size,
  1547. error_buf, error_buf_size)))
  1548. || (module_inst->table_count > 0
  1549. && !(module_inst->tables =
  1550. tables_instantiate(module, module_inst, first_table,
  1551. error_buf, error_buf_size)))
  1552. || (module_inst->e->function_count > 0
  1553. && !(module_inst->e->functions = functions_instantiate(
  1554. module, module_inst, error_buf, error_buf_size)))
  1555. || (module_inst->export_func_count > 0
  1556. && !(module_inst->export_functions = export_functions_instantiate(
  1557. module, module_inst, module_inst->export_func_count,
  1558. error_buf, error_buf_size)))
  1559. #if WASM_ENABLE_MULTI_MODULE != 0
  1560. || (module_inst->export_global_count > 0
  1561. && !(module_inst->export_globals = export_globals_instantiate(
  1562. module, module_inst, module_inst->export_global_count,
  1563. error_buf, error_buf_size)))
  1564. #endif
  1565. #if WASM_ENABLE_JIT != 0
  1566. || (module_inst->e->function_count > 0
  1567. && !init_func_ptrs(module_inst, module, error_buf, error_buf_size))
  1568. #endif
  1569. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1570. || (module_inst->e->function_count > 0
  1571. && !init_func_type_indexes(module_inst, error_buf, error_buf_size))
  1572. #endif
  1573. ) {
  1574. goto fail;
  1575. }
  1576. if (global_count > 0) {
  1577. /* Initialize the global data */
  1578. global_data = module_inst->global_data;
  1579. global_data_end = global_data + module->global_data_size;
  1580. global = globals;
  1581. for (i = 0; i < global_count; i++, global++) {
  1582. switch (global->type) {
  1583. case VALUE_TYPE_I32:
  1584. case VALUE_TYPE_F32:
  1585. #if WASM_ENABLE_REF_TYPES != 0
  1586. case VALUE_TYPE_FUNCREF:
  1587. case VALUE_TYPE_EXTERNREF:
  1588. #endif
  1589. *(int32 *)global_data = global->initial_value.i32;
  1590. global_data += sizeof(int32);
  1591. break;
  1592. case VALUE_TYPE_I64:
  1593. case VALUE_TYPE_F64:
  1594. bh_memcpy_s(global_data,
  1595. (uint32)(global_data_end - global_data),
  1596. &global->initial_value.i64, sizeof(int64));
  1597. global_data += sizeof(int64);
  1598. break;
  1599. #if WASM_ENABLE_SIMD != 0
  1600. case VALUE_TYPE_V128:
  1601. bh_memcpy_s(global_data, (uint32)sizeof(V128),
  1602. &global->initial_value.v128, sizeof(V128));
  1603. global_data += sizeof(V128);
  1604. break;
  1605. #endif
  1606. default:
  1607. bh_assert(0);
  1608. }
  1609. }
  1610. bh_assert(global_data == global_data_end);
  1611. }
  1612. if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
  1613. goto fail;
  1614. }
  1615. /* Initialize the memory data with data segment section */
  1616. for (i = 0; i < module->data_seg_count; i++) {
  1617. WASMMemoryInstance *memory = NULL;
  1618. uint8 *memory_data = NULL;
  1619. uint64 memory_size = 0;
  1620. WASMDataSeg *data_seg = module->data_segments[i];
  1621. #if WASM_ENABLE_BULK_MEMORY != 0
  1622. if (data_seg->is_passive)
  1623. continue;
  1624. #endif
  1625. if (is_sub_inst)
  1626. /* Ignore setting memory init data if the memory has been
  1627. initialized */
  1628. continue;
  1629. /* has check it in loader */
  1630. memory = module_inst->memories[data_seg->memory_index];
  1631. bh_assert(memory);
  1632. memory_data = memory->memory_data;
  1633. memory_size =
  1634. (uint64)memory->num_bytes_per_page * memory->cur_page_count;
  1635. bh_assert(memory_data || memory_size == 0);
  1636. bh_assert(data_seg->base_offset.init_expr_type
  1637. == INIT_EXPR_TYPE_I32_CONST
  1638. || data_seg->base_offset.init_expr_type
  1639. == INIT_EXPR_TYPE_GET_GLOBAL);
  1640. if (data_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  1641. if (!check_global_init_expr(module,
  1642. data_seg->base_offset.u.global_index,
  1643. error_buf, error_buf_size)) {
  1644. goto fail;
  1645. }
  1646. if (!globals
  1647. || globals[data_seg->base_offset.u.global_index].type
  1648. != VALUE_TYPE_I32) {
  1649. set_error_buf(error_buf, error_buf_size,
  1650. "data segment does not fit");
  1651. goto fail;
  1652. }
  1653. base_offset =
  1654. globals[data_seg->base_offset.u.global_index].initial_value.i32;
  1655. }
  1656. else {
  1657. base_offset = (uint32)data_seg->base_offset.u.i32;
  1658. }
  1659. /* check offset */
  1660. if (base_offset > memory_size) {
  1661. LOG_DEBUG("base_offset(%d) > memory_size(%d)", base_offset,
  1662. memory_size);
  1663. #if WASM_ENABLE_REF_TYPES != 0
  1664. set_error_buf(error_buf, error_buf_size,
  1665. "out of bounds memory access");
  1666. #else
  1667. set_error_buf(error_buf, error_buf_size,
  1668. "data segment does not fit");
  1669. #endif
  1670. goto fail;
  1671. }
  1672. /* check offset + length(could be zero) */
  1673. length = data_seg->data_length;
  1674. if ((uint64)base_offset + length > memory_size) {
  1675. LOG_DEBUG("base_offset(%d) + length(%d) > memory_size(%d)",
  1676. base_offset, length, memory_size);
  1677. #if WASM_ENABLE_REF_TYPES != 0
  1678. set_error_buf(error_buf, error_buf_size,
  1679. "out of bounds memory access");
  1680. #else
  1681. set_error_buf(error_buf, error_buf_size,
  1682. "data segment does not fit");
  1683. #endif
  1684. goto fail;
  1685. }
  1686. if (memory_data) {
  1687. bh_memcpy_s(memory_data + base_offset,
  1688. (uint32)memory_size - base_offset, data_seg->data,
  1689. length);
  1690. }
  1691. }
  1692. /* Initialize the table data with table segment section */
  1693. for (i = 0; module_inst->table_count > 0 && i < module->table_seg_count;
  1694. i++) {
  1695. WASMTableSeg *table_seg = module->table_segments + i;
  1696. /* has check it in loader */
  1697. WASMTableInstance *table = module_inst->tables[table_seg->table_index];
  1698. uint32 *table_data;
  1699. #if WASM_ENABLE_REF_TYPES != 0
  1700. uint8 tbl_elem_type;
  1701. uint32 tbl_init_size, tbl_max_size;
  1702. #endif
  1703. bh_assert(table);
  1704. #if WASM_ENABLE_REF_TYPES != 0
  1705. (void)wasm_runtime_get_table_inst_elem_type(
  1706. (WASMModuleInstanceCommon *)module_inst, table_seg->table_index,
  1707. &tbl_elem_type, &tbl_init_size, &tbl_max_size);
  1708. if (tbl_elem_type != VALUE_TYPE_FUNCREF
  1709. && tbl_elem_type != VALUE_TYPE_EXTERNREF) {
  1710. set_error_buf(error_buf, error_buf_size,
  1711. "elements segment does not fit");
  1712. goto fail;
  1713. }
  1714. (void)tbl_init_size;
  1715. (void)tbl_max_size;
  1716. #endif
  1717. table_data = table->elems;
  1718. #if WASM_ENABLE_MULTI_MODULE != 0
  1719. if (table_seg->table_index < module->import_table_count
  1720. && module_inst->e->table_insts_linked[table_seg->table_index]) {
  1721. table_data =
  1722. module_inst->e->table_insts_linked[table_seg->table_index]
  1723. ->elems;
  1724. }
  1725. #endif
  1726. bh_assert(table_data);
  1727. #if WASM_ENABLE_REF_TYPES != 0
  1728. if (!wasm_elem_is_active(table_seg->mode))
  1729. continue;
  1730. #endif
  1731. #if WASM_ENABLE_REF_TYPES != 0
  1732. bh_assert(table_seg->base_offset.init_expr_type
  1733. == INIT_EXPR_TYPE_I32_CONST
  1734. || table_seg->base_offset.init_expr_type
  1735. == INIT_EXPR_TYPE_GET_GLOBAL
  1736. || table_seg->base_offset.init_expr_type
  1737. == INIT_EXPR_TYPE_FUNCREF_CONST
  1738. || table_seg->base_offset.init_expr_type
  1739. == INIT_EXPR_TYPE_REFNULL_CONST);
  1740. #else
  1741. bh_assert(table_seg->base_offset.init_expr_type
  1742. == INIT_EXPR_TYPE_I32_CONST
  1743. || table_seg->base_offset.init_expr_type
  1744. == INIT_EXPR_TYPE_GET_GLOBAL);
  1745. #endif
  1746. /* init vec(funcidx) or vec(expr) */
  1747. if (table_seg->base_offset.init_expr_type
  1748. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1749. if (!check_global_init_expr(module,
  1750. table_seg->base_offset.u.global_index,
  1751. error_buf, error_buf_size)) {
  1752. goto fail;
  1753. }
  1754. if (!globals
  1755. || globals[table_seg->base_offset.u.global_index].type
  1756. != VALUE_TYPE_I32) {
  1757. set_error_buf(error_buf, error_buf_size,
  1758. "elements segment does not fit");
  1759. goto fail;
  1760. }
  1761. table_seg->base_offset.u.i32 =
  1762. globals[table_seg->base_offset.u.global_index]
  1763. .initial_value.i32;
  1764. }
  1765. /* check offset since length might negative */
  1766. if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
  1767. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  1768. table_seg->base_offset.u.i32, table->cur_size);
  1769. #if WASM_ENABLE_REF_TYPES != 0
  1770. set_error_buf(error_buf, error_buf_size,
  1771. "out of bounds table access");
  1772. #else
  1773. set_error_buf(error_buf, error_buf_size,
  1774. "elements segment does not fit");
  1775. #endif
  1776. goto fail;
  1777. }
  1778. /* check offset + length(could be zero) */
  1779. length = table_seg->function_count;
  1780. if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
  1781. LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
  1782. table_seg->base_offset.u.i32, length, table->cur_size);
  1783. #if WASM_ENABLE_REF_TYPES != 0
  1784. set_error_buf(error_buf, error_buf_size,
  1785. "out of bounds table access");
  1786. #else
  1787. set_error_buf(error_buf, error_buf_size,
  1788. "elements segment does not fit");
  1789. #endif
  1790. goto fail;
  1791. }
  1792. /**
  1793. * Check function index in the current module inst for now.
  1794. * will check the linked table inst owner in future.
  1795. * so loader check is enough
  1796. */
  1797. bh_memcpy_s(
  1798. table_data + table_seg->base_offset.u.i32,
  1799. (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32)
  1800. * sizeof(uint32)),
  1801. table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
  1802. }
  1803. /* Initialize the thread related data */
  1804. if (stack_size == 0)
  1805. stack_size = DEFAULT_WASM_STACK_SIZE;
  1806. #if WASM_ENABLE_SPEC_TEST != 0
  1807. if (stack_size < 128 * 1024)
  1808. stack_size = 128 * 1024;
  1809. #endif
  1810. module_inst->default_wasm_stack_size = stack_size;
  1811. if (module->malloc_function != (uint32)-1) {
  1812. module_inst->e->malloc_function =
  1813. &module_inst->e->functions[module->malloc_function];
  1814. }
  1815. if (module->free_function != (uint32)-1) {
  1816. module_inst->e->free_function =
  1817. &module_inst->e->functions[module->free_function];
  1818. }
  1819. if (module->retain_function != (uint32)-1) {
  1820. module_inst->e->retain_function =
  1821. &module_inst->e->functions[module->retain_function];
  1822. }
  1823. #if WASM_ENABLE_LIBC_WASI != 0
  1824. /* The sub-instance will get the wasi_ctx from main-instance */
  1825. if (!is_sub_inst) {
  1826. if (!wasm_runtime_init_wasi(
  1827. (WASMModuleInstanceCommon *)module_inst,
  1828. module->wasi_args.dir_list, module->wasi_args.dir_count,
  1829. module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
  1830. module->wasi_args.env, module->wasi_args.env_count,
  1831. module->wasi_args.addr_pool, module->wasi_args.addr_count,
  1832. module->wasi_args.ns_lookup_pool,
  1833. module->wasi_args.ns_lookup_count, module->wasi_args.argv,
  1834. module->wasi_args.argc, module->wasi_args.stdio[0],
  1835. module->wasi_args.stdio[1], module->wasi_args.stdio[2],
  1836. error_buf, error_buf_size)) {
  1837. goto fail;
  1838. }
  1839. }
  1840. #endif
  1841. #if WASM_ENABLE_DEBUG_INTERP != 0
  1842. if (!is_sub_inst) {
  1843. /* Add module instance into module's instance list */
  1844. os_mutex_lock(&module->instance_list_lock);
  1845. if (module->instance_list) {
  1846. LOG_WARNING(
  1847. "warning: multiple instances referencing to the same module "
  1848. "may cause unexpected behaviour during debugging");
  1849. }
  1850. module_inst->e->next = module->instance_list;
  1851. module->instance_list = module_inst;
  1852. os_mutex_unlock(&module->instance_list_lock);
  1853. }
  1854. #endif
  1855. /* Set running mode before executing wasm functions */
  1856. if (!set_running_mode(module_inst, wasm_runtime_get_default_running_mode(),
  1857. true)) {
  1858. set_error_buf(error_buf, error_buf_size,
  1859. "set instance running mode failed");
  1860. goto fail;
  1861. }
  1862. if (module->start_function != (uint32)-1) {
  1863. /* TODO: fix start function can be import function issue */
  1864. if (module->start_function >= module->import_function_count)
  1865. module_inst->e->start_function =
  1866. &module_inst->e->functions[module->start_function];
  1867. }
  1868. if (!execute_post_instantiate_functions(module_inst, is_sub_inst,
  1869. exec_env_main)) {
  1870. set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
  1871. goto fail;
  1872. }
  1873. #if WASM_ENABLE_MEMORY_TRACING != 0
  1874. wasm_runtime_dump_module_inst_mem_consumption(
  1875. (WASMModuleInstanceCommon *)module_inst);
  1876. #endif
  1877. (void)global_data_end;
  1878. return module_inst;
  1879. fail:
  1880. wasm_deinstantiate(module_inst, false);
  1881. return NULL;
  1882. }
  1883. void
  1884. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
  1885. {
  1886. if (!module_inst)
  1887. return;
  1888. if (module_inst->exec_env_singleton) {
  1889. /* wasm_exec_env_destroy will call
  1890. wasm_cluster_wait_for_all_except_self to wait for other
  1891. threads, so as to destroy their exec_envs and module
  1892. instances first, and avoid accessing the shared resources
  1893. of current module instance after it is deinstantiated. */
  1894. wasm_exec_env_destroy(module_inst->exec_env_singleton);
  1895. }
  1896. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  1897. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1898. && WASM_ENABLE_LAZY_JIT != 0)
  1899. /* Remove instance from module's instance list before freeing
  1900. func_ptrs and fast_jit_func_ptrs of the instance, to avoid
  1901. accessing the freed memory in the jit backend compilation
  1902. threads */
  1903. {
  1904. WASMModule *module = module_inst->module;
  1905. WASMModuleInstance *instance_prev = NULL, *instance;
  1906. os_mutex_lock(&module->instance_list_lock);
  1907. instance = module->instance_list;
  1908. while (instance) {
  1909. if (instance == module_inst) {
  1910. if (!instance_prev)
  1911. module->instance_list = instance->e->next;
  1912. else
  1913. instance_prev->e->next = instance->e->next;
  1914. break;
  1915. }
  1916. instance_prev = instance;
  1917. instance = instance->e->next;
  1918. }
  1919. os_mutex_unlock(&module->instance_list_lock);
  1920. }
  1921. #endif
  1922. #if WASM_ENABLE_JIT != 0
  1923. if (module_inst->func_ptrs)
  1924. wasm_runtime_free(module_inst->func_ptrs);
  1925. #endif
  1926. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1927. && WASM_ENABLE_LAZY_JIT != 0
  1928. if (module_inst->fast_jit_func_ptrs
  1929. && module_inst->fast_jit_func_ptrs
  1930. != module_inst->module->fast_jit_func_ptrs)
  1931. wasm_runtime_free(module_inst->fast_jit_func_ptrs);
  1932. #endif
  1933. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1934. if (module_inst->func_type_indexes)
  1935. wasm_runtime_free(module_inst->func_type_indexes);
  1936. #endif
  1937. #if WASM_ENABLE_MULTI_MODULE != 0
  1938. wasm_runtime_sub_module_deinstantiate(
  1939. (WASMModuleInstanceCommon *)module_inst);
  1940. #endif
  1941. if (module_inst->memory_count > 0)
  1942. memories_deinstantiate(module_inst, module_inst->memories,
  1943. module_inst->memory_count);
  1944. if (module_inst->import_func_ptrs) {
  1945. wasm_runtime_free(module_inst->import_func_ptrs);
  1946. }
  1947. tables_deinstantiate(module_inst);
  1948. functions_deinstantiate(module_inst->e->functions,
  1949. module_inst->e->function_count);
  1950. globals_deinstantiate(module_inst->e->globals);
  1951. export_functions_deinstantiate(module_inst->export_functions);
  1952. #if WASM_ENABLE_MULTI_MODULE != 0
  1953. export_globals_deinstantiate(module_inst->export_globals);
  1954. #endif
  1955. #if WASM_ENABLE_REF_TYPES != 0
  1956. wasm_externref_cleanup((WASMModuleInstanceCommon *)module_inst);
  1957. #endif
  1958. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1959. if (module_inst->frames) {
  1960. bh_vector_destroy(module_inst->frames);
  1961. wasm_runtime_free(module_inst->frames);
  1962. module_inst->frames = NULL;
  1963. }
  1964. #endif
  1965. if (module_inst->e->common.c_api_func_imports)
  1966. wasm_runtime_free(module_inst->e->common.c_api_func_imports);
  1967. if (!is_sub_inst) {
  1968. #if WASM_ENABLE_WASI_NN != 0
  1969. wasi_nn_destroy(module_inst);
  1970. #endif
  1971. wasm_native_call_context_dtors((WASMModuleInstanceCommon *)module_inst);
  1972. }
  1973. #if WASM_ENABLE_BULK_MEMORY != 0
  1974. bh_bitmap_delete(module_inst->e->common.data_dropped);
  1975. #endif
  1976. #if WASM_ENABLE_REF_TYPES != 0
  1977. bh_bitmap_delete(module_inst->e->common.elem_dropped);
  1978. #endif
  1979. wasm_runtime_free(module_inst);
  1980. }
  1981. WASMFunctionInstance *
  1982. wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name,
  1983. const char *signature)
  1984. {
  1985. uint32 i;
  1986. for (i = 0; i < module_inst->export_func_count; i++)
  1987. if (!strcmp(module_inst->export_functions[i].name, name))
  1988. return module_inst->export_functions[i].function;
  1989. (void)signature;
  1990. return NULL;
  1991. }
  1992. #if WASM_ENABLE_MULTI_MODULE != 0
  1993. WASMGlobalInstance *
  1994. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name)
  1995. {
  1996. uint32 i;
  1997. for (i = 0; i < module_inst->export_global_count; i++)
  1998. if (!strcmp(module_inst->export_globals[i].name, name))
  1999. return module_inst->export_globals[i].global;
  2000. return NULL;
  2001. }
  2002. WASMMemoryInstance *
  2003. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name)
  2004. {
  2005. /**
  2006. * using a strong assumption that one module instance only has
  2007. * one memory instance
  2008. */
  2009. (void)module_inst->export_memories;
  2010. return module_inst->memories[0];
  2011. }
  2012. WASMTableInstance *
  2013. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
  2014. {
  2015. /**
  2016. * using a strong assumption that one module instance only has
  2017. * one table instance
  2018. */
  2019. (void)module_inst->export_tables;
  2020. return module_inst->tables[0];
  2021. }
  2022. #endif
  2023. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2024. static void
  2025. call_wasm_with_hw_bound_check(WASMModuleInstance *module_inst,
  2026. WASMExecEnv *exec_env,
  2027. WASMFunctionInstance *function, unsigned argc,
  2028. uint32 argv[])
  2029. {
  2030. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  2031. WASMJmpBuf jmpbuf_node = { 0 }, *jmpbuf_node_pop;
  2032. uint32 page_size = os_getpagesize();
  2033. uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
  2034. WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  2035. uint8 *prev_top = exec_env->wasm_stack.s.top;
  2036. #ifdef BH_PLATFORM_WINDOWS
  2037. int result;
  2038. bool has_exception;
  2039. char exception[EXCEPTION_BUF_LEN];
  2040. #endif
  2041. bool ret = true;
  2042. /* Check native stack overflow firstly to ensure we have enough
  2043. native stack to run the following codes before actually calling
  2044. the aot function in invokeNative function. */
  2045. RECORD_STACK_USAGE(exec_env, (uint8 *)&exec_env_tls);
  2046. if ((uint8 *)&exec_env_tls < exec_env->native_stack_boundary
  2047. + page_size * (guard_page_count + 1)) {
  2048. wasm_set_exception(module_inst, "native stack overflow");
  2049. return;
  2050. }
  2051. if (!exec_env_tls) {
  2052. if (!os_thread_signal_inited()) {
  2053. wasm_set_exception(module_inst, "thread signal env not inited");
  2054. return;
  2055. }
  2056. /* Set thread handle and stack boundary if they haven't been set */
  2057. wasm_exec_env_set_thread_info(exec_env);
  2058. wasm_runtime_set_exec_env_tls(exec_env);
  2059. }
  2060. else {
  2061. if (exec_env_tls != exec_env) {
  2062. wasm_set_exception(module_inst, "invalid exec env");
  2063. return;
  2064. }
  2065. }
  2066. wasm_exec_env_push_jmpbuf(exec_env, &jmpbuf_node);
  2067. if (os_setjmp(jmpbuf_node.jmpbuf) == 0) {
  2068. #ifndef BH_PLATFORM_WINDOWS
  2069. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  2070. #else
  2071. __try {
  2072. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  2073. } __except (wasm_copy_exception(module_inst, NULL)
  2074. ? EXCEPTION_EXECUTE_HANDLER
  2075. : EXCEPTION_CONTINUE_SEARCH) {
  2076. /* Exception was thrown in wasm_exception_handler */
  2077. ret = false;
  2078. }
  2079. has_exception = wasm_copy_exception(module_inst, exception);
  2080. if (has_exception && strstr(exception, "native stack overflow")) {
  2081. /* After a stack overflow, the stack was left
  2082. in a damaged state, let the CRT repair it */
  2083. result = _resetstkoflw();
  2084. bh_assert(result != 0);
  2085. }
  2086. #endif
  2087. }
  2088. else {
  2089. /* Exception has been set in signal handler before calling longjmp */
  2090. ret = false;
  2091. }
  2092. /* Note: can't check wasm_get_exception(module_inst) here, there may be
  2093. * exception which is not caught by hardware (e.g. uninitialized elements),
  2094. * then the stack-frame is already freed inside wasm_interp_call_wasm */
  2095. if (!ret) {
  2096. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2097. if (wasm_interp_create_call_stack(exec_env)) {
  2098. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  2099. }
  2100. #endif
  2101. /* Restore operand frames */
  2102. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  2103. exec_env->wasm_stack.s.top = prev_top;
  2104. }
  2105. jmpbuf_node_pop = wasm_exec_env_pop_jmpbuf(exec_env);
  2106. bh_assert(&jmpbuf_node == jmpbuf_node_pop);
  2107. if (!exec_env->jmpbuf_stack_top) {
  2108. wasm_runtime_set_exec_env_tls(NULL);
  2109. }
  2110. if (!ret) {
  2111. os_sigreturn();
  2112. os_signal_unmask();
  2113. }
  2114. (void)jmpbuf_node_pop;
  2115. }
  2116. #define interp_call_wasm call_wasm_with_hw_bound_check
  2117. #else
  2118. #define interp_call_wasm wasm_interp_call_wasm
  2119. #endif
  2120. bool
  2121. wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
  2122. unsigned argc, uint32 argv[])
  2123. {
  2124. WASMModuleInstance *module_inst =
  2125. (WASMModuleInstance *)exec_env->module_inst;
  2126. #ifndef OS_ENABLE_HW_BOUND_CHECK
  2127. /* Set thread handle and stack boundary */
  2128. wasm_exec_env_set_thread_info(exec_env);
  2129. #else
  2130. /* Set thread info in call_wasm_with_hw_bound_check when
  2131. hw bound check is enabled */
  2132. #endif
  2133. /* Set exec env so it can be later retrieved from instance */
  2134. module_inst->e->common.cur_exec_env = exec_env;
  2135. interp_call_wasm(module_inst, exec_env, function, argc, argv);
  2136. return !wasm_copy_exception(module_inst, NULL);
  2137. }
  2138. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0
  2139. /* look for the function name */
  2140. static char *
  2141. get_func_name_from_index(const WASMModuleInstance *inst, uint32 func_index)
  2142. {
  2143. char *func_name = NULL;
  2144. WASMFunctionInstance *func_inst = inst->e->functions + func_index;
  2145. if (func_inst->is_import_func) {
  2146. func_name = func_inst->u.func_import->field_name;
  2147. }
  2148. else {
  2149. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2150. func_name = func_inst->u.func->field_name;
  2151. #endif
  2152. /* if custom name section is not generated,
  2153. search symbols from export table */
  2154. if (!func_name) {
  2155. unsigned j;
  2156. for (j = 0; j < inst->export_func_count; j++) {
  2157. WASMExportFuncInstance *export_func =
  2158. inst->export_functions + j;
  2159. if (export_func->function == func_inst) {
  2160. func_name = export_func->name;
  2161. break;
  2162. }
  2163. }
  2164. }
  2165. }
  2166. return func_name;
  2167. }
  2168. #endif /*WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0*/
  2169. #if WASM_ENABLE_PERF_PROFILING != 0
  2170. void
  2171. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
  2172. {
  2173. WASMFunctionInstance *func_inst;
  2174. char *func_name;
  2175. uint32 i;
  2176. os_printf("Performance profiler data:\n");
  2177. for (i = 0; i < module_inst->e->function_count; i++) {
  2178. func_inst = module_inst->e->functions + i;
  2179. if (func_inst->total_exec_cnt == 0)
  2180. continue;
  2181. func_name = get_func_name_from_index(module_inst, i);
  2182. if (func_name)
  2183. os_printf(
  2184. " func %s, execution time: %.3f ms, execution count: %" PRIu32
  2185. " times, children execution time: %.3f ms\n",
  2186. func_name, func_inst->total_exec_time / 1000.0f,
  2187. func_inst->total_exec_cnt,
  2188. func_inst->children_exec_time / 1000.0f);
  2189. else
  2190. os_printf(" func %" PRIu32
  2191. ", execution time: %.3f ms, execution count: %" PRIu32
  2192. " times, children execution time: %.3f ms\n",
  2193. i, func_inst->total_exec_time / 1000.0f,
  2194. func_inst->total_exec_cnt,
  2195. func_inst->children_exec_time / 1000.0f);
  2196. }
  2197. }
  2198. double
  2199. wasm_summarize_wasm_execute_time(const WASMModuleInstance *inst)
  2200. {
  2201. double ret = 0;
  2202. unsigned i;
  2203. for (i = 0; i < inst->e->function_count; i++) {
  2204. WASMFunctionInstance *func = inst->e->functions + i;
  2205. ret += (func->total_exec_time - func->children_exec_time) / 1000.0f;
  2206. }
  2207. return ret;
  2208. }
  2209. double
  2210. wasm_get_wasm_func_exec_time(const WASMModuleInstance *inst,
  2211. const char *func_name)
  2212. {
  2213. unsigned i;
  2214. for (i = 0; i < inst->e->function_count; i++) {
  2215. char *name_in_wasm = get_func_name_from_index(inst, i);
  2216. if (name_in_wasm && strcmp(name_in_wasm, func_name) == 0) {
  2217. WASMFunctionInstance *func = inst->e->functions + i;
  2218. return (func->total_exec_time - func->children_exec_time) / 1000.0f;
  2219. }
  2220. }
  2221. return -1.0;
  2222. }
  2223. #endif /*WASM_ENABLE_PERF_PROFILING != 0*/
  2224. uint32
  2225. wasm_module_malloc_internal(WASMModuleInstance *module_inst,
  2226. WASMExecEnv *exec_env, uint32 size,
  2227. void **p_native_addr)
  2228. {
  2229. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  2230. uint8 *addr = NULL;
  2231. uint32 offset = 0;
  2232. if (!memory) {
  2233. wasm_set_exception(module_inst, "uninitialized memory");
  2234. return 0;
  2235. }
  2236. if (memory->heap_handle) {
  2237. addr = mem_allocator_malloc(memory->heap_handle, size);
  2238. }
  2239. else if (module_inst->e->malloc_function && module_inst->e->free_function) {
  2240. if (!execute_malloc_function(
  2241. module_inst, exec_env, module_inst->e->malloc_function,
  2242. module_inst->e->retain_function, size, &offset)) {
  2243. return 0;
  2244. }
  2245. /* If we use app's malloc function,
  2246. the default memory may be changed while memory growing */
  2247. memory = wasm_get_default_memory(module_inst);
  2248. addr = offset ? memory->memory_data + offset : NULL;
  2249. }
  2250. if (!addr) {
  2251. if (memory->heap_handle
  2252. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  2253. wasm_runtime_show_app_heap_corrupted_prompt();
  2254. wasm_set_exception(module_inst, "app heap corrupted");
  2255. }
  2256. else {
  2257. LOG_WARNING("warning: allocate %u bytes memory failed", size);
  2258. }
  2259. return 0;
  2260. }
  2261. if (p_native_addr)
  2262. *p_native_addr = addr;
  2263. return (uint32)(addr - memory->memory_data);
  2264. }
  2265. uint32
  2266. wasm_module_realloc_internal(WASMModuleInstance *module_inst,
  2267. WASMExecEnv *exec_env, uint32 ptr, uint32 size,
  2268. void **p_native_addr)
  2269. {
  2270. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  2271. uint8 *addr = NULL;
  2272. if (!memory) {
  2273. wasm_set_exception(module_inst, "uninitialized memory");
  2274. return 0;
  2275. }
  2276. if (memory->heap_handle) {
  2277. addr = mem_allocator_realloc(
  2278. memory->heap_handle, ptr ? memory->memory_data + ptr : NULL, size);
  2279. }
  2280. /* Only support realloc in WAMR's app heap */
  2281. (void)exec_env;
  2282. if (!addr) {
  2283. if (memory->heap_handle
  2284. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  2285. wasm_set_exception(module_inst, "app heap corrupted");
  2286. }
  2287. else {
  2288. wasm_set_exception(module_inst, "out of memory");
  2289. }
  2290. return 0;
  2291. }
  2292. if (p_native_addr)
  2293. *p_native_addr = addr;
  2294. return (uint32)(addr - memory->memory_data);
  2295. }
  2296. void
  2297. wasm_module_free_internal(WASMModuleInstance *module_inst,
  2298. WASMExecEnv *exec_env, uint32 ptr)
  2299. {
  2300. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  2301. if (!memory) {
  2302. return;
  2303. }
  2304. if (ptr) {
  2305. uint8 *addr = memory->memory_data + ptr;
  2306. uint8 *memory_data_end;
  2307. /* memory->memory_data_end may be changed in memory grow */
  2308. SHARED_MEMORY_LOCK(memory);
  2309. memory_data_end = memory->memory_data_end;
  2310. SHARED_MEMORY_UNLOCK(memory);
  2311. if (memory->heap_handle && memory->heap_data <= addr
  2312. && addr < memory->heap_data_end) {
  2313. mem_allocator_free(memory->heap_handle, addr);
  2314. }
  2315. else if (module_inst->e->malloc_function
  2316. && module_inst->e->free_function && memory->memory_data <= addr
  2317. && addr < memory_data_end) {
  2318. execute_free_function(module_inst, exec_env,
  2319. module_inst->e->free_function, ptr);
  2320. }
  2321. }
  2322. }
  2323. uint32
  2324. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  2325. void **p_native_addr)
  2326. {
  2327. return wasm_module_malloc_internal(module_inst, NULL, size, p_native_addr);
  2328. }
  2329. uint32
  2330. wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
  2331. void **p_native_addr)
  2332. {
  2333. return wasm_module_realloc_internal(module_inst, NULL, ptr, size,
  2334. p_native_addr);
  2335. }
  2336. void
  2337. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
  2338. {
  2339. wasm_module_free_internal(module_inst, NULL, ptr);
  2340. }
  2341. uint32
  2342. wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src,
  2343. uint32 size)
  2344. {
  2345. char *buffer;
  2346. uint32 buffer_offset =
  2347. wasm_module_malloc(module_inst, size, (void **)&buffer);
  2348. if (buffer_offset != 0) {
  2349. buffer = wasm_runtime_addr_app_to_native(
  2350. (WASMModuleInstanceCommon *)module_inst, buffer_offset);
  2351. bh_memcpy_s(buffer, size, src, size);
  2352. }
  2353. return buffer_offset;
  2354. }
  2355. #if WASM_ENABLE_REF_TYPES != 0
  2356. bool
  2357. wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx,
  2358. uint32 inc_size, uint32 init_val)
  2359. {
  2360. uint32 total_size, *new_table_data_start, i;
  2361. WASMTableInstance *table_inst;
  2362. if (!inc_size) {
  2363. return true;
  2364. }
  2365. bh_assert(table_idx < module_inst->table_count);
  2366. table_inst = wasm_get_table_inst(module_inst, table_idx);
  2367. if (!table_inst) {
  2368. return false;
  2369. }
  2370. if (inc_size > UINT32_MAX - table_inst->cur_size) {
  2371. return false;
  2372. }
  2373. total_size = table_inst->cur_size + inc_size;
  2374. if (total_size > table_inst->max_size) {
  2375. return false;
  2376. }
  2377. /* fill in */
  2378. new_table_data_start = table_inst->elems + table_inst->cur_size;
  2379. for (i = 0; i < inc_size; ++i) {
  2380. new_table_data_start[i] = init_val;
  2381. }
  2382. table_inst->cur_size = total_size;
  2383. return true;
  2384. }
  2385. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  2386. static bool
  2387. call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2388. uint32 argc, uint32 argv[], bool check_type_idx, uint32 type_idx)
  2389. {
  2390. WASMModuleInstance *module_inst = NULL;
  2391. WASMTableInstance *table_inst = NULL;
  2392. uint32 func_idx = 0;
  2393. WASMFunctionInstance *func_inst = NULL;
  2394. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  2395. bh_assert(module_inst);
  2396. table_inst = module_inst->tables[tbl_idx];
  2397. if (!table_inst) {
  2398. wasm_set_exception(module_inst, "unknown table");
  2399. goto got_exception;
  2400. }
  2401. if (elem_idx >= table_inst->cur_size) {
  2402. wasm_set_exception(module_inst, "undefined element");
  2403. goto got_exception;
  2404. }
  2405. func_idx = table_inst->elems[elem_idx];
  2406. if (func_idx == NULL_REF) {
  2407. wasm_set_exception(module_inst, "uninitialized element");
  2408. goto got_exception;
  2409. }
  2410. /**
  2411. * we insist to call functions owned by the module itself
  2412. **/
  2413. if (func_idx >= module_inst->e->function_count) {
  2414. wasm_set_exception(module_inst, "unknown function");
  2415. goto got_exception;
  2416. }
  2417. func_inst = module_inst->e->functions + func_idx;
  2418. if (check_type_idx) {
  2419. WASMType *cur_type = module_inst->module->types[type_idx];
  2420. WASMType *cur_func_type;
  2421. if (func_inst->is_import_func)
  2422. cur_func_type = func_inst->u.func_import->func_type;
  2423. else
  2424. cur_func_type = func_inst->u.func->func_type;
  2425. if (cur_type != cur_func_type) {
  2426. wasm_set_exception(module_inst, "indirect call type mismatch");
  2427. goto got_exception;
  2428. }
  2429. }
  2430. interp_call_wasm(module_inst, exec_env, func_inst, argc, argv);
  2431. return !wasm_copy_exception(module_inst, NULL);
  2432. got_exception:
  2433. return false;
  2434. }
  2435. bool
  2436. wasm_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2437. uint32 argc, uint32 argv[])
  2438. {
  2439. return call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, false, 0);
  2440. }
  2441. #if WASM_ENABLE_THREAD_MGR != 0
  2442. bool
  2443. wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size)
  2444. {
  2445. WASMModuleInstance *module_inst =
  2446. (WASMModuleInstance *)exec_env->module_inst;
  2447. uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
  2448. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
  2449. /* Check the aux stack space */
  2450. uint32 data_end = module_inst->module->aux_data_end;
  2451. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  2452. bool is_stack_before_data = stack_bottom < data_end ? true : false;
  2453. if ((is_stack_before_data && (size > start_offset))
  2454. || ((!is_stack_before_data) && (start_offset - data_end < size)))
  2455. return false;
  2456. #endif
  2457. if (stack_top_idx != (uint32)-1) {
  2458. /* The aux stack top is a wasm global,
  2459. set the initial value for the global */
  2460. uint8 *global_addr =
  2461. module_inst->global_data
  2462. + module_inst->e->globals[stack_top_idx].data_offset;
  2463. *(int32 *)global_addr = start_offset;
  2464. /* The aux stack boundary is a constant value,
  2465. set the value to exec_env */
  2466. exec_env->aux_stack_boundary.boundary = start_offset - size;
  2467. exec_env->aux_stack_bottom.bottom = start_offset;
  2468. return true;
  2469. }
  2470. return false;
  2471. }
  2472. bool
  2473. wasm_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size)
  2474. {
  2475. WASMModuleInstance *module_inst =
  2476. (WASMModuleInstance *)exec_env->module_inst;
  2477. /* The aux stack information is resolved in loader
  2478. and store in module */
  2479. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  2480. uint32 total_aux_stack_size = module_inst->module->aux_stack_size;
  2481. if (stack_bottom != 0 && total_aux_stack_size != 0) {
  2482. if (start_offset)
  2483. *start_offset = stack_bottom;
  2484. if (size)
  2485. *size = total_aux_stack_size;
  2486. return true;
  2487. }
  2488. return false;
  2489. }
  2490. #endif
  2491. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  2492. void
  2493. wasm_get_module_mem_consumption(const WASMModule *module,
  2494. WASMModuleMemConsumption *mem_conspn)
  2495. {
  2496. uint32 i, size;
  2497. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2498. mem_conspn->module_struct_size = sizeof(WASMModule);
  2499. mem_conspn->types_size = sizeof(WASMType *) * module->type_count;
  2500. for (i = 0; i < module->type_count; i++) {
  2501. WASMType *type = module->types[i];
  2502. size = offsetof(WASMType, types)
  2503. + sizeof(uint8) * (type->param_count + type->result_count);
  2504. mem_conspn->types_size += size;
  2505. }
  2506. mem_conspn->imports_size = sizeof(WASMImport) * module->import_count;
  2507. mem_conspn->functions_size =
  2508. sizeof(WASMFunction *) * module->function_count;
  2509. for (i = 0; i < module->function_count; i++) {
  2510. WASMFunction *func = module->functions[i];
  2511. WASMType *type = func->func_type;
  2512. size = sizeof(WASMFunction) + func->local_count
  2513. + sizeof(uint16) * (type->param_count + func->local_count);
  2514. #if WASM_ENABLE_FAST_INTERP != 0
  2515. size +=
  2516. func->code_compiled_size + sizeof(uint32) * func->const_cell_num;
  2517. #endif
  2518. mem_conspn->functions_size += size;
  2519. }
  2520. mem_conspn->tables_size = sizeof(WASMTable) * module->table_count;
  2521. mem_conspn->memories_size = sizeof(WASMMemory) * module->memory_count;
  2522. mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count;
  2523. mem_conspn->exports_size = sizeof(WASMExport) * module->export_count;
  2524. mem_conspn->table_segs_size =
  2525. sizeof(WASMTableSeg) * module->table_seg_count;
  2526. for (i = 0; i < module->table_seg_count; i++) {
  2527. WASMTableSeg *table_seg = &module->table_segments[i];
  2528. mem_conspn->tables_size += sizeof(uint32) * table_seg->function_count;
  2529. }
  2530. mem_conspn->data_segs_size = sizeof(WASMDataSeg *) * module->data_seg_count;
  2531. for (i = 0; i < module->data_seg_count; i++) {
  2532. mem_conspn->data_segs_size += sizeof(WASMDataSeg);
  2533. }
  2534. if (module->const_str_list) {
  2535. StringNode *node = module->const_str_list, *node_next;
  2536. while (node) {
  2537. node_next = node->next;
  2538. mem_conspn->const_strs_size +=
  2539. sizeof(StringNode) + strlen(node->str) + 1;
  2540. node = node_next;
  2541. }
  2542. }
  2543. mem_conspn->total_size += mem_conspn->module_struct_size;
  2544. mem_conspn->total_size += mem_conspn->types_size;
  2545. mem_conspn->total_size += mem_conspn->imports_size;
  2546. mem_conspn->total_size += mem_conspn->functions_size;
  2547. mem_conspn->total_size += mem_conspn->tables_size;
  2548. mem_conspn->total_size += mem_conspn->memories_size;
  2549. mem_conspn->total_size += mem_conspn->globals_size;
  2550. mem_conspn->total_size += mem_conspn->exports_size;
  2551. mem_conspn->total_size += mem_conspn->table_segs_size;
  2552. mem_conspn->total_size += mem_conspn->data_segs_size;
  2553. mem_conspn->total_size += mem_conspn->const_strs_size;
  2554. }
  2555. void
  2556. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
  2557. WASMModuleInstMemConsumption *mem_conspn)
  2558. {
  2559. uint32 i, size;
  2560. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2561. mem_conspn->module_inst_struct_size = (uint8 *)module_inst->e
  2562. - (uint8 *)module_inst
  2563. + sizeof(WASMModuleInstanceExtra);
  2564. mem_conspn->memories_size =
  2565. sizeof(WASMMemoryInstance *) * module_inst->memory_count;
  2566. for (i = 0; i < module_inst->memory_count; i++) {
  2567. WASMMemoryInstance *memory = module_inst->memories[i];
  2568. size = memory->num_bytes_per_page * memory->cur_page_count;
  2569. mem_conspn->memories_size += size;
  2570. mem_conspn->app_heap_size += memory->heap_data_end - memory->heap_data;
  2571. /* size of app heap structure */
  2572. mem_conspn->memories_size += mem_allocator_get_heap_struct_size();
  2573. /* Module instance structures have been appened into the end of
  2574. module instance */
  2575. }
  2576. mem_conspn->tables_size =
  2577. sizeof(WASMTableInstance *) * module_inst->table_count;
  2578. /* Table instance structures and table elements have been appened into
  2579. the end of module instance */
  2580. mem_conspn->functions_size =
  2581. sizeof(WASMFunctionInstance) * module_inst->e->function_count;
  2582. mem_conspn->globals_size =
  2583. sizeof(WASMGlobalInstance) * module_inst->e->global_count;
  2584. /* Global data has been appened into the end of module instance */
  2585. mem_conspn->exports_size =
  2586. sizeof(WASMExportFuncInstance) * module_inst->export_func_count;
  2587. mem_conspn->total_size += mem_conspn->module_inst_struct_size;
  2588. mem_conspn->total_size += mem_conspn->memories_size;
  2589. mem_conspn->total_size += mem_conspn->functions_size;
  2590. mem_conspn->total_size += mem_conspn->tables_size;
  2591. mem_conspn->total_size += mem_conspn->globals_size;
  2592. mem_conspn->total_size += mem_conspn->exports_size;
  2593. }
  2594. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
  2595. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  2596. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2597. bool
  2598. wasm_interp_create_call_stack(struct WASMExecEnv *exec_env)
  2599. {
  2600. WASMModuleInstance *module_inst =
  2601. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  2602. WASMInterpFrame *first_frame,
  2603. *cur_frame = wasm_exec_env_get_cur_frame(exec_env);
  2604. uint32 n = 0;
  2605. /* count frames includes a function */
  2606. first_frame = cur_frame;
  2607. while (cur_frame) {
  2608. if (cur_frame->function) {
  2609. n++;
  2610. }
  2611. cur_frame = cur_frame->prev_frame;
  2612. }
  2613. /* release previous stack frames and create new ones */
  2614. if (!bh_vector_destroy(module_inst->frames)
  2615. || !bh_vector_init(module_inst->frames, n, sizeof(WASMCApiFrame),
  2616. false)) {
  2617. return false;
  2618. }
  2619. cur_frame = first_frame;
  2620. n = 0;
  2621. while (cur_frame) {
  2622. WASMCApiFrame frame = { 0 };
  2623. WASMFunctionInstance *func_inst = cur_frame->function;
  2624. const char *func_name = NULL;
  2625. const uint8 *func_code_base = NULL;
  2626. if (!func_inst) {
  2627. cur_frame = cur_frame->prev_frame;
  2628. continue;
  2629. }
  2630. /* place holder, will overwrite it in wasm_c_api */
  2631. frame.instance = module_inst;
  2632. frame.module_offset = 0;
  2633. frame.func_index = (uint32)(func_inst - module_inst->e->functions);
  2634. func_code_base = wasm_get_func_code(func_inst);
  2635. if (!cur_frame->ip || !func_code_base) {
  2636. frame.func_offset = 0;
  2637. }
  2638. else {
  2639. frame.func_offset = (uint32)(cur_frame->ip - func_code_base);
  2640. }
  2641. func_name = get_func_name_from_index(module_inst, frame.func_index);
  2642. frame.func_name_wp = func_name;
  2643. if (!bh_vector_append(module_inst->frames, &frame)) {
  2644. bh_vector_destroy(module_inst->frames);
  2645. return false;
  2646. }
  2647. cur_frame = cur_frame->prev_frame;
  2648. n++;
  2649. }
  2650. return true;
  2651. }
  2652. #define PRINT_OR_DUMP() \
  2653. do { \
  2654. total_len += \
  2655. wasm_runtime_dump_line_buf_impl(line_buf, print, &buf, &len); \
  2656. if ((!print) && buf && (len == 0)) { \
  2657. exception_unlock(module_inst); \
  2658. return total_len; \
  2659. } \
  2660. } while (0)
  2661. uint32
  2662. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf,
  2663. uint32 len)
  2664. {
  2665. WASMModuleInstance *module_inst =
  2666. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  2667. uint32 n = 0, total_len = 0, total_frames;
  2668. /* reserve 256 bytes for line buffer, any line longer than 256 bytes
  2669. * will be truncated */
  2670. char line_buf[256];
  2671. if (!module_inst->frames) {
  2672. return 0;
  2673. }
  2674. total_frames = (uint32)bh_vector_size(module_inst->frames);
  2675. if (total_frames == 0) {
  2676. return 0;
  2677. }
  2678. exception_lock(module_inst);
  2679. snprintf(line_buf, sizeof(line_buf), "\n");
  2680. PRINT_OR_DUMP();
  2681. while (n < total_frames) {
  2682. WASMCApiFrame frame = { 0 };
  2683. uint32 line_length, i;
  2684. if (!bh_vector_get(module_inst->frames, n, &frame)) {
  2685. exception_unlock(module_inst);
  2686. return 0;
  2687. }
  2688. /* function name not exported, print number instead */
  2689. if (frame.func_name_wp == NULL) {
  2690. line_length =
  2691. snprintf(line_buf, sizeof(line_buf),
  2692. "#%02" PRIu32 " $f%" PRIu32 "\n", n, frame.func_index);
  2693. }
  2694. else {
  2695. line_length =
  2696. snprintf(line_buf, sizeof(line_buf), "#%02" PRIu32 " %s\n", n,
  2697. frame.func_name_wp);
  2698. }
  2699. if (line_length >= sizeof(line_buf)) {
  2700. uint32 line_buffer_len = sizeof(line_buf);
  2701. /* If line too long, ensure the last character is '\n' */
  2702. for (i = line_buffer_len - 5; i < line_buffer_len - 2; i++) {
  2703. line_buf[i] = '.';
  2704. }
  2705. line_buf[line_buffer_len - 2] = '\n';
  2706. }
  2707. PRINT_OR_DUMP();
  2708. n++;
  2709. }
  2710. snprintf(line_buf, sizeof(line_buf), "\n");
  2711. PRINT_OR_DUMP();
  2712. exception_unlock(module_inst);
  2713. return total_len + 1;
  2714. }
  2715. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
  2716. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  2717. || WASM_ENABLE_WAMR_COMPILER != 0
  2718. void
  2719. jit_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id)
  2720. {
  2721. if (id != EXCE_ALREADY_THROWN)
  2722. wasm_set_exception_with_id(module_inst, id);
  2723. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2724. wasm_runtime_access_exce_check_guard_page();
  2725. #endif
  2726. }
  2727. bool
  2728. jit_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  2729. uint32 app_buf_addr, uint32 app_buf_size,
  2730. void **p_native_addr)
  2731. {
  2732. bool ret = wasm_check_app_addr_and_convert(
  2733. module_inst, is_str, app_buf_addr, app_buf_size, p_native_addr);
  2734. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2735. if (!ret)
  2736. wasm_runtime_access_exce_check_guard_page();
  2737. #endif
  2738. return ret;
  2739. }
  2740. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  2741. || WASM_ENABLE_WAMR_COMPILER != 0 */
  2742. #if WASM_ENABLE_FAST_JIT != 0
  2743. bool
  2744. fast_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2745. uint32 type_idx, uint32 argc, uint32 *argv)
  2746. {
  2747. return call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, true,
  2748. type_idx);
  2749. }
  2750. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  2751. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  2752. bool
  2753. llvm_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  2754. uint32 argc, uint32 *argv)
  2755. {
  2756. bool ret;
  2757. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  2758. ret = call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, false, 0);
  2759. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2760. if (!ret)
  2761. wasm_runtime_access_exce_check_guard_page();
  2762. #endif
  2763. return ret;
  2764. }
  2765. bool
  2766. llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
  2767. uint32 *argv)
  2768. {
  2769. WASMModuleInstance *module_inst;
  2770. WASMModule *module;
  2771. uint32 *func_type_indexes;
  2772. uint32 func_type_idx;
  2773. WASMType *func_type;
  2774. void *func_ptr;
  2775. WASMFunctionImport *import_func;
  2776. CApiFuncImport *c_api_func_import = NULL;
  2777. const char *signature;
  2778. void *attachment;
  2779. char buf[96];
  2780. bool ret = false;
  2781. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  2782. module_inst = (WASMModuleInstance *)wasm_runtime_get_module_inst(exec_env);
  2783. module = module_inst->module;
  2784. func_type_indexes = module_inst->func_type_indexes;
  2785. func_type_idx = func_type_indexes[func_idx];
  2786. func_type = module->types[func_type_idx];
  2787. func_ptr = module_inst->func_ptrs[func_idx];
  2788. bh_assert(func_idx < module->import_function_count);
  2789. import_func = &module->import_functions[func_idx].u.function;
  2790. if (import_func->call_conv_wasm_c_api) {
  2791. if (module_inst->e->common.c_api_func_imports) {
  2792. c_api_func_import =
  2793. module_inst->e->common.c_api_func_imports + func_idx;
  2794. func_ptr = c_api_func_import->func_ptr_linked;
  2795. }
  2796. else {
  2797. c_api_func_import = NULL;
  2798. func_ptr = NULL;
  2799. }
  2800. }
  2801. if (!func_ptr) {
  2802. snprintf(buf, sizeof(buf),
  2803. "failed to call unlinked import function (%s, %s)",
  2804. import_func->module_name, import_func->field_name);
  2805. wasm_set_exception(module_inst, buf);
  2806. goto fail;
  2807. }
  2808. attachment = import_func->attachment;
  2809. if (import_func->call_conv_wasm_c_api) {
  2810. ret = wasm_runtime_invoke_c_api_native(
  2811. (WASMModuleInstanceCommon *)module_inst, func_ptr, func_type, argc,
  2812. argv, c_api_func_import->with_env_arg, c_api_func_import->env_arg);
  2813. }
  2814. else if (!import_func->call_conv_raw) {
  2815. signature = import_func->signature;
  2816. ret =
  2817. wasm_runtime_invoke_native(exec_env, func_ptr, func_type, signature,
  2818. attachment, argv, argc, argv);
  2819. }
  2820. else {
  2821. signature = import_func->signature;
  2822. ret = wasm_runtime_invoke_native_raw(exec_env, func_ptr, func_type,
  2823. signature, attachment, argv, argc,
  2824. argv);
  2825. }
  2826. fail:
  2827. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2828. if (!ret)
  2829. wasm_runtime_access_exce_check_guard_page();
  2830. #endif
  2831. return ret;
  2832. }
  2833. #if WASM_ENABLE_BULK_MEMORY != 0
  2834. bool
  2835. llvm_jit_memory_init(WASMModuleInstance *module_inst, uint32 seg_index,
  2836. uint32 offset, uint32 len, uint32 dst)
  2837. {
  2838. WASMMemoryInstance *memory_inst;
  2839. WASMModule *module;
  2840. uint8 *data;
  2841. uint8 *maddr;
  2842. uint64 seg_len;
  2843. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  2844. memory_inst = wasm_get_default_memory(module_inst);
  2845. if (bh_bitmap_get_bit(module_inst->e->common.data_dropped, seg_index)) {
  2846. seg_len = 0;
  2847. data = NULL;
  2848. }
  2849. else {
  2850. module = module_inst->module;
  2851. seg_len = module->data_segments[seg_index]->data_length;
  2852. data = module->data_segments[seg_index]->data;
  2853. }
  2854. if (!wasm_runtime_validate_app_addr((WASMModuleInstanceCommon *)module_inst,
  2855. dst, len))
  2856. return false;
  2857. if ((uint64)offset + (uint64)len > seg_len) {
  2858. wasm_set_exception(module_inst, "out of bounds memory access");
  2859. return false;
  2860. }
  2861. maddr = wasm_runtime_addr_app_to_native(
  2862. (WASMModuleInstanceCommon *)module_inst, dst);
  2863. SHARED_MEMORY_LOCK(memory_inst);
  2864. bh_memcpy_s(maddr, memory_inst->memory_data_size - dst, data + offset, len);
  2865. SHARED_MEMORY_UNLOCK(memory_inst);
  2866. return true;
  2867. }
  2868. bool
  2869. llvm_jit_data_drop(WASMModuleInstance *module_inst, uint32 seg_index)
  2870. {
  2871. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  2872. bh_bitmap_set_bit(module_inst->e->common.data_dropped, seg_index);
  2873. /* Currently we can't free the dropped data segment
  2874. as they are stored in wasm bytecode */
  2875. return true;
  2876. }
  2877. #endif /* end of WASM_ENABLE_BULK_MEMORY != 0 */
  2878. #if WASM_ENABLE_REF_TYPES != 0
  2879. void
  2880. llvm_jit_drop_table_seg(WASMModuleInstance *module_inst, uint32 tbl_seg_idx)
  2881. {
  2882. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  2883. bh_bitmap_set_bit(module_inst->e->common.elem_dropped, tbl_seg_idx);
  2884. }
  2885. void
  2886. llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx,
  2887. uint32 tbl_seg_idx, uint32 length, uint32 src_offset,
  2888. uint32 dst_offset)
  2889. {
  2890. WASMTableInstance *tbl_inst;
  2891. WASMTableSeg *tbl_seg;
  2892. uint32 *tbl_seg_elems = NULL, tbl_seg_len = 0;
  2893. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  2894. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  2895. tbl_seg = module_inst->module->table_segments + tbl_seg_idx;
  2896. bh_assert(tbl_inst);
  2897. bh_assert(tbl_seg);
  2898. if (!bh_bitmap_get_bit(module_inst->e->common.elem_dropped, tbl_seg_idx)) {
  2899. /* table segment isn't dropped */
  2900. tbl_seg_elems = tbl_seg->func_indexes;
  2901. tbl_seg_len = tbl_seg->function_count;
  2902. }
  2903. if (offset_len_out_of_bounds(src_offset, length, tbl_seg_len)
  2904. || offset_len_out_of_bounds(dst_offset, length, tbl_inst->cur_size)) {
  2905. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2906. return;
  2907. }
  2908. if (!length) {
  2909. return;
  2910. }
  2911. bh_memcpy_s((uint8 *)tbl_inst + offsetof(WASMTableInstance, elems)
  2912. + dst_offset * sizeof(uint32),
  2913. (uint32)sizeof(uint32) * (tbl_inst->cur_size - dst_offset),
  2914. tbl_seg_elems + src_offset, (uint32)(length * sizeof(uint32)));
  2915. }
  2916. void
  2917. llvm_jit_table_copy(WASMModuleInstance *module_inst, uint32 src_tbl_idx,
  2918. uint32 dst_tbl_idx, uint32 length, uint32 src_offset,
  2919. uint32 dst_offset)
  2920. {
  2921. WASMTableInstance *src_tbl_inst;
  2922. WASMTableInstance *dst_tbl_inst;
  2923. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  2924. src_tbl_inst = wasm_get_table_inst(module_inst, src_tbl_idx);
  2925. dst_tbl_inst = wasm_get_table_inst(module_inst, dst_tbl_idx);
  2926. bh_assert(src_tbl_inst);
  2927. bh_assert(dst_tbl_inst);
  2928. if (offset_len_out_of_bounds(dst_offset, length, dst_tbl_inst->cur_size)
  2929. || offset_len_out_of_bounds(src_offset, length,
  2930. src_tbl_inst->cur_size)) {
  2931. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2932. return;
  2933. }
  2934. /* if src_offset >= dst_offset, copy from front to back */
  2935. /* if src_offset < dst_offset, copy from back to front */
  2936. /* merge all together */
  2937. bh_memmove_s((uint8 *)dst_tbl_inst + offsetof(WASMTableInstance, elems)
  2938. + sizeof(uint32) * dst_offset,
  2939. (uint32)sizeof(uint32) * (dst_tbl_inst->cur_size - dst_offset),
  2940. (uint8 *)src_tbl_inst + offsetof(WASMTableInstance, elems)
  2941. + sizeof(uint32) * src_offset,
  2942. (uint32)sizeof(uint32) * length);
  2943. }
  2944. void
  2945. llvm_jit_table_fill(WASMModuleInstance *module_inst, uint32 tbl_idx,
  2946. uint32 length, uint32 val, uint32 data_offset)
  2947. {
  2948. WASMTableInstance *tbl_inst;
  2949. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  2950. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  2951. bh_assert(tbl_inst);
  2952. if (offset_len_out_of_bounds(data_offset, length, tbl_inst->cur_size)) {
  2953. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  2954. return;
  2955. }
  2956. for (; length != 0; data_offset++, length--) {
  2957. tbl_inst->elems[data_offset] = val;
  2958. }
  2959. }
  2960. uint32
  2961. llvm_jit_table_grow(WASMModuleInstance *module_inst, uint32 tbl_idx,
  2962. uint32 inc_size, uint32 init_val)
  2963. {
  2964. WASMTableInstance *tbl_inst;
  2965. uint32 i, orig_size, total_size;
  2966. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  2967. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  2968. if (!tbl_inst) {
  2969. return (uint32)-1;
  2970. }
  2971. orig_size = tbl_inst->cur_size;
  2972. if (!inc_size) {
  2973. return orig_size;
  2974. }
  2975. if (tbl_inst->cur_size > UINT32_MAX - inc_size) { /* integer overflow */
  2976. return (uint32)-1;
  2977. }
  2978. total_size = tbl_inst->cur_size + inc_size;
  2979. if (total_size > tbl_inst->max_size) {
  2980. return (uint32)-1;
  2981. }
  2982. /* fill in */
  2983. for (i = 0; i < inc_size; ++i) {
  2984. tbl_inst->elems[tbl_inst->cur_size + i] = init_val;
  2985. }
  2986. tbl_inst->cur_size = total_size;
  2987. return orig_size;
  2988. }
  2989. #endif /* end of WASM_ENABLE_REF_TYPES != 0 */
  2990. #if WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_PERF_PROFILING != 0
  2991. bool
  2992. llvm_jit_alloc_frame(WASMExecEnv *exec_env, uint32 func_index)
  2993. {
  2994. WASMModuleInstance *module_inst;
  2995. WASMInterpFrame *frame;
  2996. uint32 size;
  2997. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  2998. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  2999. size = wasm_interp_interp_frame_size(0);
  3000. frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
  3001. if (!frame) {
  3002. wasm_set_exception(module_inst, "wasm operand stack overflow");
  3003. return false;
  3004. }
  3005. frame->function = module_inst->e->functions + func_index;
  3006. frame->ip = NULL;
  3007. frame->sp = frame->lp;
  3008. #if WASM_ENABLE_PERF_PROFILING != 0
  3009. frame->time_started = os_time_thread_cputime_us();
  3010. #endif
  3011. frame->prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  3012. wasm_exec_env_set_cur_frame(exec_env, frame);
  3013. return true;
  3014. }
  3015. void
  3016. llvm_jit_free_frame(WASMExecEnv *exec_env)
  3017. {
  3018. WASMInterpFrame *frame;
  3019. WASMInterpFrame *prev_frame;
  3020. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  3021. frame = wasm_exec_env_get_cur_frame(exec_env);
  3022. prev_frame = frame->prev_frame;
  3023. #if WASM_ENABLE_PERF_PROFILING != 0
  3024. if (frame->function) {
  3025. uint64 elapsed = os_time_thread_cputime_us() - frame->time_started;
  3026. frame->function->total_exec_time += elapsed;
  3027. frame->function->total_exec_cnt++;
  3028. /* parent function */
  3029. if (prev_frame)
  3030. prev_frame->function->children_exec_time += elapsed;
  3031. }
  3032. #endif
  3033. wasm_exec_env_free_wasm_frame(exec_env, frame);
  3034. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  3035. }
  3036. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 \
  3037. || WASM_ENABLE_PERF_PROFILING != 0 */
  3038. #endif /* end of WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 */
  3039. #if WASM_ENABLE_LIBC_WASI != 0 && WASM_ENABLE_MULTI_MODULE != 0
  3040. void
  3041. wasm_propagate_wasi_args(WASMModule *module)
  3042. {
  3043. if (!module->import_count)
  3044. return;
  3045. bh_assert(&module->import_module_list_head);
  3046. WASMRegisteredModule *node =
  3047. bh_list_first_elem(&module->import_module_list_head);
  3048. while (node) {
  3049. WASIArguments *wasi_args_impt_mod =
  3050. &((WASMModule *)(node->module))->wasi_args;
  3051. bh_assert(wasi_args_impt_mod);
  3052. bh_memcpy_s(wasi_args_impt_mod, sizeof(WASIArguments),
  3053. &module->wasi_args, sizeof(WASIArguments));
  3054. node = bh_list_elem_next(node);
  3055. }
  3056. }
  3057. #endif