wasm_runtime.c 115 KB

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