wasm_runtime.c 125 KB

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