wasm_runtime.c 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824
  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.h"
  7. #include "wasm_loader.h"
  8. #include "wasm_interp.h"
  9. #include "bh_common.h"
  10. #include "bh_log.h"
  11. #include "mem_alloc.h"
  12. #include "../common/wasm_runtime_common.h"
  13. #include "../common/wasm_memory.h"
  14. #if WASM_ENABLE_GC != 0
  15. #include "../common/gc/gc_object.h"
  16. #endif
  17. #if WASM_ENABLE_SHARED_MEMORY != 0
  18. #include "../common/wasm_shared_memory.h"
  19. #endif
  20. #if WASM_ENABLE_THREAD_MGR != 0
  21. #include "../libraries/thread-mgr/thread_manager.h"
  22. #endif
  23. #if WASM_ENABLE_DEBUG_INTERP != 0
  24. #include "../libraries/debug-engine/debug_engine.h"
  25. #endif
  26. #if WASM_ENABLE_FAST_JIT != 0
  27. #include "../fast-jit/jit_compiler.h"
  28. #endif
  29. #if WASM_ENABLE_JIT != 0
  30. #include "../aot/aot_runtime.h"
  31. #endif
  32. static void
  33. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  34. {
  35. if (error_buf != NULL) {
  36. snprintf(error_buf, error_buf_size,
  37. "WASM module instantiate failed: %s", string);
  38. }
  39. }
  40. static void
  41. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  42. {
  43. va_list args;
  44. char buf[128];
  45. if (error_buf != NULL) {
  46. va_start(args, format);
  47. vsnprintf(buf, sizeof(buf), format, args);
  48. va_end(args);
  49. snprintf(error_buf, error_buf_size,
  50. "WASM module instantiate failed: %s", buf);
  51. }
  52. }
  53. WASMModule *
  54. wasm_load(uint8 *buf, uint32 size,
  55. #if WASM_ENABLE_MULTI_MODULE != 0
  56. bool main_module,
  57. #endif
  58. const LoadArgs *name, char *error_buf, uint32 error_buf_size)
  59. {
  60. return wasm_loader_load(buf, size,
  61. #if WASM_ENABLE_MULTI_MODULE != 0
  62. main_module,
  63. #endif
  64. name, error_buf, error_buf_size);
  65. }
  66. WASMModule *
  67. wasm_load_from_sections(WASMSection *section_list, char *error_buf,
  68. uint32 error_buf_size)
  69. {
  70. return wasm_loader_load_from_sections(section_list, error_buf,
  71. error_buf_size);
  72. }
  73. void
  74. wasm_unload(WASMModule *module)
  75. {
  76. wasm_loader_unload(module);
  77. }
  78. static void *
  79. runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  80. {
  81. void *mem;
  82. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  83. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  84. return NULL;
  85. }
  86. memset(mem, 0, (uint32)size);
  87. return mem;
  88. }
  89. #if WASM_ENABLE_MULTI_MODULE != 0
  90. static WASMModuleInstance *
  91. get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
  92. const WASMModule *sub_module)
  93. {
  94. bh_list *sub_module_inst_list = parent_module_inst->e->sub_module_inst_list;
  95. WASMSubModInstNode *node = bh_list_first_elem(sub_module_inst_list);
  96. while (node && sub_module != node->module_inst->module) {
  97. node = bh_list_elem_next(node);
  98. }
  99. return node ? node->module_inst : NULL;
  100. }
  101. #endif
  102. /**
  103. * Destroy memory instances.
  104. */
  105. static void
  106. memories_deinstantiate(WASMModuleInstance *module_inst,
  107. WASMMemoryInstance **memories, uint32 count)
  108. {
  109. uint32 i;
  110. if (memories) {
  111. for (i = 0; i < count; i++) {
  112. if (memories[i]) {
  113. #if WASM_ENABLE_MULTI_MODULE != 0
  114. WASMModule *module = module_inst->module;
  115. if (i < module->import_memory_count
  116. && module->import_memories[i].u.memory.import_module) {
  117. continue;
  118. }
  119. #endif
  120. #if WASM_ENABLE_SHARED_MEMORY != 0
  121. if (shared_memory_is_shared(memories[i])) {
  122. uint32 ref_count = shared_memory_dec_reference(memories[i]);
  123. /* if the reference count is not zero,
  124. don't free the memory */
  125. if (ref_count > 0)
  126. continue;
  127. }
  128. #endif
  129. if (memories[i]->heap_handle) {
  130. mem_allocator_destroy(memories[i]->heap_handle);
  131. wasm_runtime_free(memories[i]->heap_handle);
  132. memories[i]->heap_handle = NULL;
  133. }
  134. if (memories[i]->memory_data) {
  135. wasm_deallocate_linear_memory(memories[i]);
  136. }
  137. }
  138. }
  139. wasm_runtime_free(memories);
  140. }
  141. (void)module_inst;
  142. }
  143. static WASMMemoryInstance *
  144. memory_instantiate(WASMModuleInstance *module_inst, WASMModuleInstance *parent,
  145. WASMMemoryInstance *memory, uint32 memory_idx,
  146. uint32 num_bytes_per_page, uint32 init_page_count,
  147. uint32 max_page_count, uint32 heap_size, uint32 flags,
  148. char *error_buf, uint32 error_buf_size)
  149. {
  150. WASMModule *module = module_inst->module;
  151. uint32 inc_page_count, global_idx, default_max_page;
  152. uint32 bytes_of_last_page, bytes_to_page_end;
  153. uint64 aux_heap_base,
  154. heap_offset = (uint64)num_bytes_per_page * init_page_count;
  155. uint64 memory_data_size, max_memory_data_size;
  156. uint8 *global_addr;
  157. bool is_shared_memory = false;
  158. #if WASM_ENABLE_SHARED_MEMORY != 0
  159. is_shared_memory = flags & SHARED_MEMORY_FLAG ? true : false;
  160. /* shared memory */
  161. if (is_shared_memory && parent != NULL) {
  162. bh_assert(parent->memory_count > memory_idx);
  163. memory = parent->memories[memory_idx];
  164. shared_memory_inc_reference(memory);
  165. return memory;
  166. }
  167. #else
  168. (void)parent;
  169. (void)memory_idx;
  170. (void)flags;
  171. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  172. #if WASM_ENABLE_MEMORY64 != 0
  173. if (flags & MEMORY64_FLAG) {
  174. memory->is_memory64 = 1;
  175. }
  176. #endif
  177. default_max_page =
  178. memory->is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  179. if (heap_size > 0 && module_inst->module->malloc_function != (uint32)-1
  180. && module_inst->module->free_function != (uint32)-1) {
  181. /* Disable app heap, use malloc/free function exported
  182. by wasm app to allocate/free memory instead */
  183. heap_size = 0;
  184. }
  185. /* If initial memory is the largest size allowed, disallowing insert host
  186. * managed heap */
  187. if (heap_size > 0
  188. && heap_offset == GET_MAX_LINEAR_MEMORY_SIZE(memory->is_memory64)) {
  189. set_error_buf(error_buf, error_buf_size,
  190. "failed to insert app heap into linear memory, "
  191. "try using `--heap-size=0` option");
  192. return NULL;
  193. }
  194. if (init_page_count == max_page_count && init_page_count == 1) {
  195. /* If only one page and at most one page, we just append
  196. the app heap to the end of linear memory, enlarge the
  197. num_bytes_per_page, and don't change the page count */
  198. heap_offset = num_bytes_per_page;
  199. num_bytes_per_page += heap_size;
  200. if (num_bytes_per_page < heap_size) {
  201. set_error_buf(error_buf, error_buf_size,
  202. "failed to insert app heap into linear memory, "
  203. "try using `--heap-size=0` option");
  204. return NULL;
  205. }
  206. }
  207. else if (heap_size > 0) {
  208. if (init_page_count == max_page_count && init_page_count == 0) {
  209. /* If the memory data size is always 0, we resize it to
  210. one page for app heap */
  211. num_bytes_per_page = heap_size;
  212. heap_offset = 0;
  213. inc_page_count = 1;
  214. }
  215. else if (module->aux_heap_base_global_index != (uint32)-1
  216. && module->aux_heap_base
  217. < (uint64)num_bytes_per_page * init_page_count) {
  218. /* Insert app heap before __heap_base */
  219. aux_heap_base = module->aux_heap_base;
  220. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  221. if (bytes_of_last_page == 0)
  222. bytes_of_last_page = num_bytes_per_page;
  223. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  224. inc_page_count =
  225. (heap_size - bytes_to_page_end + num_bytes_per_page - 1)
  226. / num_bytes_per_page;
  227. heap_offset = aux_heap_base;
  228. aux_heap_base += heap_size;
  229. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  230. if (bytes_of_last_page == 0)
  231. bytes_of_last_page = num_bytes_per_page;
  232. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  233. if (bytes_to_page_end < 1 * BH_KB) {
  234. aux_heap_base += 1 * BH_KB;
  235. inc_page_count++;
  236. }
  237. /* Adjust __heap_base global value */
  238. global_idx = module->aux_heap_base_global_index;
  239. bh_assert(module_inst->e->globals
  240. && global_idx < module_inst->e->global_count);
  241. global_addr = module_inst->global_data
  242. + module_inst->e->globals[global_idx].data_offset;
  243. #if WASM_ENABLE_MEMORY64 != 0
  244. if (memory->is_memory64) {
  245. /* For memory64, the global value should be i64 */
  246. *(uint64 *)global_addr = aux_heap_base;
  247. }
  248. else
  249. #endif
  250. {
  251. /* For memory32, the global value should be i32 */
  252. *(uint32 *)global_addr = (uint32)aux_heap_base;
  253. }
  254. LOG_VERBOSE("Reset __heap_base global to %" PRIu64, aux_heap_base);
  255. }
  256. else {
  257. /* Insert app heap before new page */
  258. inc_page_count =
  259. (heap_size + num_bytes_per_page - 1) / num_bytes_per_page;
  260. heap_offset = (uint64)num_bytes_per_page * init_page_count;
  261. heap_size = (uint64)num_bytes_per_page * inc_page_count;
  262. if (heap_size > 0)
  263. heap_size -= 1 * BH_KB;
  264. }
  265. init_page_count += inc_page_count;
  266. max_page_count += inc_page_count;
  267. if (init_page_count > default_max_page) {
  268. set_error_buf(error_buf, error_buf_size,
  269. "failed to insert app heap into linear memory, "
  270. "try using `--heap-size=0` option");
  271. return NULL;
  272. }
  273. if (max_page_count > default_max_page)
  274. max_page_count = default_max_page;
  275. }
  276. LOG_VERBOSE("Memory instantiate:");
  277. LOG_VERBOSE(" page bytes: %u, init pages: %u, max pages: %u",
  278. num_bytes_per_page, init_page_count, max_page_count);
  279. LOG_VERBOSE(" heap offset: %" PRIu64 ", heap size: %u\n", heap_offset,
  280. heap_size);
  281. max_memory_data_size = (uint64)num_bytes_per_page * max_page_count;
  282. bh_assert(max_memory_data_size
  283. <= GET_MAX_LINEAR_MEMORY_SIZE(memory->is_memory64));
  284. (void)max_memory_data_size;
  285. bh_assert(memory != NULL);
  286. if (wasm_allocate_linear_memory(&memory->memory_data, is_shared_memory,
  287. memory->is_memory64, num_bytes_per_page,
  288. init_page_count, max_page_count,
  289. &memory_data_size)
  290. != BHT_OK) {
  291. set_error_buf(error_buf, error_buf_size,
  292. "allocate linear memory failed");
  293. return NULL;
  294. }
  295. memory->module_type = Wasm_Module_Bytecode;
  296. memory->num_bytes_per_page = num_bytes_per_page;
  297. memory->cur_page_count = init_page_count;
  298. memory->max_page_count = max_page_count;
  299. memory->memory_data_size = memory_data_size;
  300. memory->heap_data = memory->memory_data + heap_offset;
  301. memory->heap_data_end = memory->heap_data + heap_size;
  302. memory->memory_data_end = memory->memory_data + memory_data_size;
  303. /* Initialize heap */
  304. if (heap_size > 0) {
  305. uint32 heap_struct_size = mem_allocator_get_heap_struct_size();
  306. if (!(memory->heap_handle = runtime_malloc(
  307. (uint64)heap_struct_size, error_buf, error_buf_size))) {
  308. goto fail1;
  309. }
  310. if (!mem_allocator_create_with_struct_and_pool(
  311. memory->heap_handle, heap_struct_size, memory->heap_data,
  312. heap_size)) {
  313. set_error_buf(error_buf, error_buf_size, "init app heap failed");
  314. goto fail2;
  315. }
  316. }
  317. if (memory_data_size > 0) {
  318. wasm_runtime_set_mem_bound_check_bytes(memory, memory_data_size);
  319. }
  320. #if WASM_ENABLE_SHARED_MEMORY != 0
  321. if (is_shared_memory) {
  322. memory->is_shared_memory = 1;
  323. memory->ref_count = 1;
  324. }
  325. #endif
  326. LOG_VERBOSE("Memory instantiate success.");
  327. return memory;
  328. fail2:
  329. if (heap_size > 0)
  330. wasm_runtime_free(memory->heap_handle);
  331. fail1:
  332. if (memory->memory_data)
  333. wasm_deallocate_linear_memory(memory);
  334. return NULL;
  335. }
  336. /**
  337. * Instantiate memories in a module.
  338. */
  339. static WASMMemoryInstance **
  340. memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  341. WASMModuleInstance *parent, uint32 heap_size,
  342. uint32 max_memory_pages, char *error_buf,
  343. uint32 error_buf_size)
  344. {
  345. WASMImport *import;
  346. uint32 mem_index = 0, i,
  347. memory_count = module->import_memory_count + module->memory_count;
  348. uint64 total_size;
  349. WASMMemoryInstance **memories, *memory;
  350. total_size = sizeof(WASMMemoryInstance *) * (uint64)memory_count;
  351. if (!(memories = runtime_malloc(total_size, error_buf, error_buf_size))) {
  352. return NULL;
  353. }
  354. memory = module_inst->global_table_data.memory_instances;
  355. /* instantiate memories from import section */
  356. import = module->import_memories;
  357. for (i = 0; i < module->import_memory_count; i++, import++, memory++) {
  358. uint32 num_bytes_per_page =
  359. import->u.memory.mem_type.num_bytes_per_page;
  360. uint32 init_page_count = import->u.memory.mem_type.init_page_count;
  361. uint32 max_page_count = wasm_runtime_get_max_mem(
  362. max_memory_pages, import->u.memory.mem_type.init_page_count,
  363. import->u.memory.mem_type.max_page_count);
  364. uint32 flags = import->u.memory.mem_type.flags;
  365. uint32 actual_heap_size = heap_size;
  366. #if WASM_ENABLE_MULTI_MODULE != 0
  367. if (import->u.memory.import_module != NULL) {
  368. WASMModuleInstance *module_inst_linked;
  369. if (!(module_inst_linked = get_sub_module_inst(
  370. module_inst, import->u.memory.import_module))) {
  371. set_error_buf(error_buf, error_buf_size, "unknown memory");
  372. memories_deinstantiate(module_inst, memories, memory_count);
  373. return NULL;
  374. }
  375. if (!(memories[mem_index++] = wasm_lookup_memory(
  376. module_inst_linked, import->u.memory.field_name))) {
  377. set_error_buf(error_buf, error_buf_size, "unknown memory");
  378. memories_deinstantiate(module_inst, memories, memory_count);
  379. return NULL;
  380. }
  381. }
  382. else
  383. #endif
  384. {
  385. if (!(memories[mem_index] = memory_instantiate(
  386. module_inst, parent, memory, mem_index,
  387. num_bytes_per_page, init_page_count, max_page_count,
  388. actual_heap_size, flags, error_buf, error_buf_size))) {
  389. memories_deinstantiate(module_inst, memories, memory_count);
  390. return NULL;
  391. }
  392. mem_index++;
  393. }
  394. }
  395. /* instantiate memories from memory section */
  396. for (i = 0; i < module->memory_count; i++, memory++) {
  397. uint32 max_page_count = wasm_runtime_get_max_mem(
  398. max_memory_pages, module->memories[i].init_page_count,
  399. module->memories[i].max_page_count);
  400. if (!(memories[mem_index] = memory_instantiate(
  401. module_inst, parent, memory, mem_index,
  402. module->memories[i].num_bytes_per_page,
  403. module->memories[i].init_page_count, max_page_count,
  404. heap_size, module->memories[i].flags, error_buf,
  405. error_buf_size))) {
  406. memories_deinstantiate(module_inst, memories, memory_count);
  407. return NULL;
  408. }
  409. mem_index++;
  410. }
  411. bh_assert(mem_index == memory_count);
  412. (void)module_inst;
  413. return memories;
  414. }
  415. /**
  416. * Destroy table instances.
  417. */
  418. static void
  419. tables_deinstantiate(WASMModuleInstance *module_inst)
  420. {
  421. if (module_inst->tables) {
  422. wasm_runtime_free(module_inst->tables);
  423. }
  424. #if WASM_ENABLE_MULTI_MODULE != 0
  425. if (module_inst->e->table_insts_linked) {
  426. wasm_runtime_free(module_inst->e->table_insts_linked);
  427. }
  428. #endif
  429. }
  430. /**
  431. * Instantiate tables in a module.
  432. */
  433. static WASMTableInstance **
  434. tables_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  435. WASMTableInstance *first_table, char *error_buf,
  436. uint32 error_buf_size)
  437. {
  438. WASMImport *import;
  439. uint32 table_index = 0, i;
  440. uint32 table_count = module->import_table_count + module->table_count;
  441. uint64 total_size = (uint64)sizeof(WASMTableInstance *) * table_count;
  442. WASMTableInstance **tables, *table = first_table;
  443. #if WASM_ENABLE_MULTI_MODULE != 0
  444. uint64 total_size_of_tables_linked =
  445. (uint64)sizeof(WASMTableInstance *) * module->import_table_count;
  446. WASMTableInstance **table_linked = NULL;
  447. #endif
  448. if (!(tables = runtime_malloc(total_size, error_buf, error_buf_size))) {
  449. return NULL;
  450. }
  451. #if WASM_ENABLE_MULTI_MODULE != 0
  452. if (module->import_table_count > 0
  453. && !(module_inst->e->table_insts_linked = table_linked = runtime_malloc(
  454. total_size_of_tables_linked, error_buf, error_buf_size))) {
  455. goto fail;
  456. }
  457. #endif
  458. /* instantiate tables from import section */
  459. import = module->import_tables;
  460. for (i = 0; i < module->import_table_count; i++, import++) {
  461. uint32 max_size_fixed = 0;
  462. #if WASM_ENABLE_MULTI_MODULE != 0
  463. WASMTableInstance *table_inst_linked = NULL;
  464. WASMModuleInstance *module_inst_linked = NULL;
  465. if (import->u.table.import_module) {
  466. if (!(module_inst_linked = get_sub_module_inst(
  467. module_inst, import->u.table.import_module))) {
  468. set_error_buf(error_buf, error_buf_size, "unknown table");
  469. goto fail;
  470. }
  471. if (!(table_inst_linked = wasm_lookup_table(
  472. module_inst_linked, import->u.table.field_name))) {
  473. set_error_buf(error_buf, error_buf_size, "unknown table");
  474. goto fail;
  475. }
  476. total_size = offsetof(WASMTableInstance, elems);
  477. }
  478. else
  479. #endif
  480. {
  481. /* in order to save memory, alloc resource as few as possible */
  482. max_size_fixed = import->u.table.table_type.possible_grow
  483. ? import->u.table.table_type.max_size
  484. : import->u.table.table_type.init_size;
  485. /* it is a built-in table, every module has its own */
  486. total_size = offsetof(WASMTableInstance, elems);
  487. /* store function indexes for non-gc, object pointers for gc */
  488. total_size += (uint64)sizeof(table_elem_type_t) * max_size_fixed;
  489. }
  490. tables[table_index++] = table;
  491. #if WASM_ENABLE_GC == 0
  492. /* Set all elements to -1 to mark them as uninitialized elements */
  493. memset(table, -1, (uint32)total_size);
  494. #else
  495. /* For GC, all elements have already been set to NULL_REF (0) as
  496. uninitialized elements */
  497. #endif
  498. #if WASM_ENABLE_MULTI_MODULE != 0
  499. *table_linked = table_inst_linked;
  500. if (table_inst_linked != NULL) {
  501. table->elem_type = table_inst_linked->elem_type;
  502. #if WASM_ENABLE_GC != 0
  503. table->elem_ref_type = table_inst_linked->elem_ref_type;
  504. #endif
  505. table->cur_size = table_inst_linked->cur_size;
  506. table->max_size = table_inst_linked->max_size;
  507. }
  508. else
  509. #endif
  510. {
  511. table->elem_type = import->u.table.table_type.elem_type;
  512. #if WASM_ENABLE_GC != 0
  513. table->elem_ref_type.elem_ref_type =
  514. import->u.table.table_type.elem_ref_type;
  515. #endif
  516. table->cur_size = import->u.table.table_type.init_size;
  517. table->max_size = max_size_fixed;
  518. }
  519. table = (WASMTableInstance *)((uint8 *)table + (uint32)total_size);
  520. #if WASM_ENABLE_MULTI_MODULE != 0
  521. table_linked++;
  522. #endif
  523. }
  524. /* instantiate tables from table section */
  525. for (i = 0; i < module->table_count; i++) {
  526. uint32 max_size_fixed = 0;
  527. total_size = offsetof(WASMTableInstance, elems);
  528. #if WASM_ENABLE_MULTI_MODULE != 0
  529. /* in case, a module which imports this table will grow it */
  530. max_size_fixed = module->tables[i].table_type.max_size;
  531. #else
  532. max_size_fixed = module->tables[i].table_type.possible_grow
  533. ? module->tables[i].table_type.max_size
  534. : module->tables[i].table_type.init_size;
  535. #endif
  536. #if WASM_ENABLE_GC == 0
  537. /* Store function indexes */
  538. total_size += sizeof(uintptr_t) * (uint64)max_size_fixed;
  539. #else
  540. /* Store object pointers */
  541. total_size += sizeof(uintptr_t) * (uint64)max_size_fixed;
  542. #endif
  543. tables[table_index++] = table;
  544. #if WASM_ENABLE_GC == 0
  545. /* Set all elements to -1 to mark them as uninitialized elements */
  546. memset(table, -1, (uint32)total_size);
  547. #else
  548. /* For GC, all elements have already been set to NULL_REF (0) as
  549. uninitialized elements */
  550. #endif
  551. table->elem_type = module->tables[i].table_type.elem_type;
  552. #if WASM_ENABLE_GC != 0
  553. table->elem_ref_type.elem_ref_type =
  554. module->tables[i].table_type.elem_ref_type;
  555. #endif
  556. table->cur_size = module->tables[i].table_type.init_size;
  557. table->max_size = max_size_fixed;
  558. table = (WASMTableInstance *)((uint8 *)table + (uint32)total_size);
  559. }
  560. bh_assert(table_index == table_count);
  561. (void)module_inst;
  562. return tables;
  563. #if WASM_ENABLE_MULTI_MODULE != 0
  564. fail:
  565. wasm_runtime_free(tables);
  566. return NULL;
  567. #endif
  568. }
  569. /**
  570. * Destroy function instances.
  571. */
  572. static void
  573. functions_deinstantiate(WASMFunctionInstance *functions)
  574. {
  575. if (functions) {
  576. wasm_runtime_free(functions);
  577. }
  578. }
  579. /**
  580. * Instantiate functions in a module.
  581. */
  582. static WASMFunctionInstance *
  583. functions_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  584. char *error_buf, uint32 error_buf_size)
  585. {
  586. WASMImport *import;
  587. uint32 i,
  588. function_count = module->import_function_count + module->function_count;
  589. uint64 total_size = sizeof(WASMFunctionInstance) * (uint64)function_count;
  590. WASMFunctionInstance *functions, *function;
  591. if (!(functions = runtime_malloc(total_size, error_buf, error_buf_size))) {
  592. return NULL;
  593. }
  594. total_size = sizeof(void *) * (uint64)module->import_function_count;
  595. if (total_size > 0
  596. && !(module_inst->import_func_ptrs =
  597. runtime_malloc(total_size, error_buf, error_buf_size))) {
  598. wasm_runtime_free(functions);
  599. return NULL;
  600. }
  601. /* instantiate functions from import section */
  602. function = functions;
  603. import = module->import_functions;
  604. for (i = 0; i < module->import_function_count; i++, import++) {
  605. function->is_import_func = true;
  606. #if WASM_ENABLE_MULTI_MODULE != 0
  607. if (import->u.function.import_module) {
  608. function->import_module_inst = get_sub_module_inst(
  609. module_inst, import->u.function.import_module);
  610. if (function->import_module_inst) {
  611. function->import_func_inst =
  612. wasm_lookup_function(function->import_module_inst,
  613. import->u.function.field_name);
  614. }
  615. }
  616. #endif /* WASM_ENABLE_MULTI_MODULE */
  617. function->u.func_import = &import->u.function;
  618. function->param_cell_num = import->u.function.func_type->param_cell_num;
  619. function->ret_cell_num = import->u.function.func_type->ret_cell_num;
  620. function->param_count =
  621. (uint16)function->u.func_import->func_type->param_count;
  622. function->param_types = function->u.func_import->func_type->types;
  623. function->local_cell_num = 0;
  624. function->local_count = 0;
  625. function->local_types = NULL;
  626. /* Copy the function pointer to current instance */
  627. module_inst->import_func_ptrs[i] =
  628. function->u.func_import->func_ptr_linked;
  629. function++;
  630. }
  631. /* instantiate functions from function section */
  632. for (i = 0; i < module->function_count; i++) {
  633. function->is_import_func = false;
  634. function->u.func = module->functions[i];
  635. function->param_cell_num = function->u.func->param_cell_num;
  636. function->ret_cell_num = function->u.func->ret_cell_num;
  637. function->local_cell_num = function->u.func->local_cell_num;
  638. function->param_count =
  639. (uint16)function->u.func->func_type->param_count;
  640. function->local_count = (uint16)function->u.func->local_count;
  641. function->param_types = function->u.func->func_type->types;
  642. function->local_types = function->u.func->local_types;
  643. function->local_offsets = function->u.func->local_offsets;
  644. #if WASM_ENABLE_FAST_INTERP != 0
  645. function->const_cell_num = function->u.func->const_cell_num;
  646. #endif
  647. function++;
  648. }
  649. bh_assert((uint32)(function - functions) == function_count);
  650. #if WASM_ENABLE_FAST_JIT != 0
  651. module_inst->fast_jit_func_ptrs = module->fast_jit_func_ptrs;
  652. #endif
  653. return functions;
  654. }
  655. #if WASM_ENABLE_TAGS != 0
  656. /**
  657. * Destroy tags instances.
  658. */
  659. static void
  660. tags_deinstantiate(WASMTagInstance *tags, void **import_tag_ptrs)
  661. {
  662. if (tags) {
  663. wasm_runtime_free(tags);
  664. }
  665. if (import_tag_ptrs) {
  666. wasm_runtime_free(import_tag_ptrs);
  667. }
  668. }
  669. /**
  670. * Instantiate tags in a module.
  671. */
  672. static WASMTagInstance *
  673. tags_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  674. char *error_buf, uint32 error_buf_size)
  675. {
  676. WASMImport *import;
  677. uint32 i, tag_count = module->import_tag_count + module->tag_count;
  678. uint64 total_size = sizeof(WASMTagInstance) * (uint64)tag_count;
  679. WASMTagInstance *tags, *tag;
  680. if (!(tags = runtime_malloc(total_size, error_buf, error_buf_size))) {
  681. return NULL;
  682. }
  683. total_size = sizeof(void *) * (uint64)module->import_tag_count;
  684. if (total_size > 0
  685. && !(module_inst->e->import_tag_ptrs =
  686. runtime_malloc(total_size, error_buf, error_buf_size))) {
  687. wasm_runtime_free(tags);
  688. return NULL;
  689. }
  690. /* instantiate tags from import section */
  691. tag = tags;
  692. import = module->import_tags;
  693. for (i = 0; i < module->import_tag_count; i++, import++) {
  694. tag->is_import_tag = true;
  695. tag->u.tag_import = &import->u.tag;
  696. tag->type = import->u.tag.type;
  697. tag->attribute = import->u.tag.attribute;
  698. #if WASM_ENABLE_MULTI_MODULE != 0
  699. if (import->u.tag.import_module) {
  700. if (!(tag->import_module_inst = get_sub_module_inst(
  701. module_inst, import->u.tag.import_module))) {
  702. set_error_buf(error_buf, error_buf_size, "unknown tag");
  703. goto fail;
  704. }
  705. if (!(tag->import_tag_inst =
  706. wasm_lookup_tag(tag->import_module_inst,
  707. import->u.tag.field_name, NULL))) {
  708. set_error_buf(error_buf, error_buf_size, "unknown tag");
  709. goto fail;
  710. }
  711. /* Copy the imported tag to current instance */
  712. module_inst->e->import_tag_ptrs[i] =
  713. tag->u.tag_import->import_tag_linked;
  714. }
  715. #endif
  716. tag++;
  717. }
  718. /* instantiate tags from tag section */
  719. for (i = 0; i < module->tag_count; i++) {
  720. tag->is_import_tag = false;
  721. tag->type = module->tags[i]->type;
  722. tag->u.tag = module->tags[i];
  723. #if WASM_ENABLE_FAST_INTERP != 0
  724. /* tag->const_cell_num = function->u.func->const_cell_num; */
  725. #endif
  726. tag++;
  727. }
  728. bh_assert((uint32)(tag - tags) == tag_count);
  729. return tags;
  730. #if WASM_ENABLE_MULTI_MODULE != 0
  731. fail:
  732. tags_deinstantiate(tags, module_inst->e->import_tag_ptrs);
  733. /* clean up */
  734. module_inst->e->import_tag_ptrs = NULL;
  735. return NULL;
  736. #endif
  737. }
  738. #endif /* end of WASM_ENABLE_TAGS != 0 */
  739. /**
  740. * Destroy global instances.
  741. */
  742. static void
  743. globals_deinstantiate(WASMGlobalInstance *globals)
  744. {
  745. if (globals)
  746. wasm_runtime_free(globals);
  747. }
  748. static bool
  749. check_global_init_expr(const WASMModule *module, uint32 global_index,
  750. char *error_buf, uint32 error_buf_size)
  751. {
  752. if (global_index >= module->import_global_count + module->global_count) {
  753. set_error_buf_v(error_buf, error_buf_size, "unknown global %d",
  754. global_index);
  755. return false;
  756. }
  757. #if WASM_ENABLE_GC == 0
  758. /**
  759. * Currently, constant expressions occurring as initializers of
  760. * globals are further constrained in that contained global.get
  761. * instructions are only allowed to refer to imported globals.
  762. *
  763. * And initializer expression cannot reference a mutable global.
  764. */
  765. if (global_index >= module->import_global_count
  766. || (module->import_globals + global_index)->u.global.type.is_mutable) {
  767. set_error_buf(error_buf, error_buf_size,
  768. "constant expression required");
  769. return false;
  770. }
  771. #endif
  772. return true;
  773. }
  774. #if WASM_ENABLE_GC != 0
  775. /* Instantiate struct global variable recursively */
  776. static WASMStructObjectRef
  777. instantiate_struct_global_recursive(WASMModule *module,
  778. WASMModuleInstance *module_inst,
  779. uint32 type_idx, uint8 flag,
  780. WASMStructNewInitValues *init_values,
  781. char *error_buf, uint32 error_buf_size)
  782. {
  783. WASMRttType *rtt_type;
  784. WASMStructObjectRef struct_obj;
  785. WASMStructType *struct_type;
  786. struct_type = (WASMStructType *)module->types[type_idx];
  787. if (!(rtt_type = wasm_rtt_type_new((WASMType *)struct_type, type_idx,
  788. module->rtt_types, module->type_count,
  789. &module->rtt_type_lock))) {
  790. set_error_buf(error_buf, error_buf_size, "create rtt object failed");
  791. return NULL;
  792. }
  793. if (!(struct_obj = wasm_struct_obj_new_internal(
  794. module_inst->e->common.gc_heap_handle, rtt_type))) {
  795. set_error_buf(error_buf, error_buf_size, "create struct object failed");
  796. return NULL;
  797. }
  798. if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
  799. uint32 field_idx;
  800. WASMRefTypeMap *ref_type_map = struct_type->ref_type_maps;
  801. bh_assert(init_values->count == struct_type->field_count);
  802. for (field_idx = 0; field_idx < init_values->count; field_idx++) {
  803. uint8 field_type = struct_type->fields[field_idx].field_type;
  804. WASMRefType *field_ref_type = NULL;
  805. if (wasm_is_type_multi_byte_type(field_type)) {
  806. field_ref_type = ref_type_map->ref_type;
  807. }
  808. if (wasm_reftype_is_subtype_of(field_type, field_ref_type,
  809. REF_TYPE_STRUCTREF, NULL,
  810. module->types, module->type_count)
  811. || wasm_reftype_is_subtype_of(field_type, field_ref_type,
  812. REF_TYPE_ARRAYREF, NULL,
  813. module->types, module->type_count)
  814. || wasm_reftype_is_subtype_of(
  815. field_type, field_ref_type, REF_TYPE_FUNCREF, NULL,
  816. module->types, module->type_count)) {
  817. WASMType *wasm_type;
  818. int32 heap_type =
  819. ref_type_map->ref_type->ref_ht_common.heap_type;
  820. WASMValue *wasm_value = &init_values->fields[field_idx];
  821. WASMValue field_value = { 0 };
  822. bh_assert(heap_type >= 0);
  823. wasm_type = module->types[heap_type];
  824. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  825. || wasm_type->type_flag == WASM_TYPE_ARRAY
  826. || wasm_type->type_flag == WASM_TYPE_FUNC);
  827. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  828. WASMStructNewInitValues *init_values1 =
  829. (WASMStructNewInitValues *)wasm_value->data;
  830. WASMStructObjectRef field =
  831. instantiate_struct_global_recursive(
  832. module, module_inst, heap_type,
  833. init_values1 ? INIT_EXPR_TYPE_STRUCT_NEW
  834. : INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT,
  835. init_values1, error_buf, error_buf_size);
  836. field_value.gc_obj = (WASMObjectRef)field;
  837. wasm_struct_obj_set_field(struct_obj, field_idx,
  838. &field_value);
  839. }
  840. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  841. /* struct object's field is an array obj */
  842. set_error_buf(error_buf, error_buf_size,
  843. "array as a field in struct object is "
  844. "not supported in constant init expr");
  845. return NULL;
  846. }
  847. else if (wasm_type->type_flag == WASM_TYPE_FUNC) {
  848. WASMFuncObjectRef func_obj = NULL;
  849. /* UINT32_MAX indicates that it is a null reference */
  850. if (wasm_value->u32 != UINT32_MAX) {
  851. if (!(func_obj = wasm_create_func_obj(
  852. module_inst, wasm_value->u32, false,
  853. error_buf, error_buf_size))) {
  854. return NULL;
  855. }
  856. }
  857. field_value.gc_obj = (WASMObjectRef)func_obj;
  858. wasm_struct_obj_set_field(struct_obj, field_idx,
  859. &field_value);
  860. }
  861. }
  862. else {
  863. wasm_struct_obj_set_field(struct_obj, field_idx,
  864. &init_values->fields[field_idx]);
  865. }
  866. if (wasm_is_type_multi_byte_type(field_type)) {
  867. ref_type_map++;
  868. }
  869. }
  870. }
  871. return struct_obj;
  872. }
  873. static WASMArrayObjectRef
  874. instantiate_array_global_recursive(WASMModule *module,
  875. WASMModuleInstance *module_inst,
  876. uint32 type_idx, uint8 flag, uint32 len,
  877. WASMValue *array_init_value,
  878. WASMArrayNewInitValues *init_values,
  879. char *error_buf, uint32 error_buf_size)
  880. {
  881. WASMRttType *rtt_type;
  882. WASMArrayObjectRef array_obj;
  883. WASMArrayType *array_type;
  884. array_type = (WASMArrayType *)module->types[type_idx];
  885. if (!(rtt_type = wasm_rtt_type_new((WASMType *)array_type, type_idx,
  886. module->rtt_types, module->type_count,
  887. &module->rtt_type_lock))) {
  888. set_error_buf(error_buf, error_buf_size, "create rtt object failed");
  889. return NULL;
  890. }
  891. if (!(array_obj =
  892. wasm_array_obj_new_internal(module_inst->e->common.gc_heap_handle,
  893. rtt_type, len, array_init_value))) {
  894. set_error_buf(error_buf, error_buf_size, "create array object failed");
  895. return NULL;
  896. }
  897. if (flag == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  898. uint32 elem_idx;
  899. uint8 elem_type = array_type->elem_type;
  900. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  901. bh_assert(init_values);
  902. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  903. REF_TYPE_STRUCTREF, NULL, module->types,
  904. module->type_count)
  905. || wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  906. REF_TYPE_ARRAYREF, NULL,
  907. module->types, module->type_count)
  908. || wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  909. REF_TYPE_FUNCREF, NULL, module->types,
  910. module->type_count)) {
  911. /* TODO */
  912. }
  913. for (elem_idx = 0; elem_idx < len; elem_idx++) {
  914. wasm_array_obj_set_elem(array_obj, elem_idx,
  915. &init_values->elem_data[elem_idx]);
  916. }
  917. }
  918. return array_obj;
  919. }
  920. #endif
  921. /**
  922. * Instantiate globals in a module.
  923. */
  924. static WASMGlobalInstance *
  925. globals_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
  926. char *error_buf, uint32 error_buf_size)
  927. {
  928. WASMImport *import;
  929. uint32 global_data_offset = 0;
  930. uint32 i, global_count = module->import_global_count + module->global_count;
  931. uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count;
  932. WASMGlobalInstance *globals, *global;
  933. if (!(globals = runtime_malloc(total_size, error_buf, error_buf_size))) {
  934. return NULL;
  935. }
  936. /* instantiate globals from import section */
  937. global = globals;
  938. import = module->import_globals;
  939. for (i = 0; i < module->import_global_count; i++, import++) {
  940. WASMGlobalImport *global_import = &import->u.global;
  941. global->type = global_import->type.val_type;
  942. global->is_mutable = global_import->type.is_mutable;
  943. #if WASM_ENABLE_GC != 0
  944. global->ref_type = global_import->ref_type;
  945. #endif
  946. #if WASM_ENABLE_MULTI_MODULE != 0
  947. if (global_import->import_module) {
  948. if (!(global->import_module_inst = get_sub_module_inst(
  949. module_inst, global_import->import_module))) {
  950. set_error_buf(error_buf, error_buf_size, "unknown global");
  951. goto fail;
  952. }
  953. if (!(global->import_global_inst = wasm_lookup_global(
  954. global->import_module_inst, global_import->field_name))) {
  955. set_error_buf(error_buf, error_buf_size, "unknown global");
  956. goto fail;
  957. }
  958. /* The linked global instance has been initialized, we
  959. just need to copy the value. */
  960. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  961. &(global_import->import_global_linked->init_expr),
  962. sizeof(WASMValue));
  963. }
  964. else
  965. #endif
  966. {
  967. /* native globals share their initial_values in one module */
  968. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  969. &(global_import->global_data_linked),
  970. sizeof(WASMValue));
  971. }
  972. #if WASM_ENABLE_FAST_JIT != 0
  973. bh_assert(global_data_offset == global_import->data_offset);
  974. #endif
  975. global->data_offset = global_data_offset;
  976. global_data_offset += wasm_value_type_size(global->type);
  977. global++;
  978. }
  979. /* instantiate globals from global section */
  980. for (i = 0; i < module->global_count; i++) {
  981. InitializerExpression *init_expr = &(module->globals[i].init_expr);
  982. uint8 flag = init_expr->init_expr_type;
  983. global->type = module->globals[i].type.val_type;
  984. global->is_mutable = module->globals[i].type.is_mutable;
  985. #if WASM_ENABLE_FAST_JIT != 0
  986. bh_assert(global_data_offset == module->globals[i].data_offset);
  987. #endif
  988. global->data_offset = global_data_offset;
  989. global_data_offset += wasm_value_type_size(global->type);
  990. #if WASM_ENABLE_GC != 0
  991. global->ref_type = module->globals[i].ref_type;
  992. #endif
  993. switch (flag) {
  994. case INIT_EXPR_TYPE_GET_GLOBAL:
  995. {
  996. if (!check_global_init_expr(module, init_expr->u.global_index,
  997. error_buf, error_buf_size)) {
  998. goto fail;
  999. }
  1000. bh_memcpy_s(
  1001. &(global->initial_value), sizeof(WASMValue),
  1002. &(globals[init_expr->u.global_index].initial_value),
  1003. sizeof(globals[init_expr->u.global_index].initial_value));
  1004. break;
  1005. }
  1006. #if WASM_ENABLE_GC != 0
  1007. case INIT_EXPR_TYPE_STRUCT_NEW:
  1008. case INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT:
  1009. {
  1010. WASMStructObjectRef struct_obj;
  1011. WASMStructNewInitValues *init_values = NULL;
  1012. uint32 type_idx;
  1013. if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
  1014. init_values = (WASMStructNewInitValues *)init_expr->u.data;
  1015. type_idx = init_values->type_idx;
  1016. }
  1017. else {
  1018. type_idx = init_expr->u.type_index;
  1019. }
  1020. struct_obj = instantiate_struct_global_recursive(
  1021. module, module_inst, type_idx, flag, init_values, error_buf,
  1022. error_buf_size);
  1023. if (!struct_obj) {
  1024. goto fail;
  1025. }
  1026. global->initial_value.gc_obj = (void *)struct_obj;
  1027. break;
  1028. }
  1029. case INIT_EXPR_TYPE_ARRAY_NEW:
  1030. case INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT:
  1031. case INIT_EXPR_TYPE_ARRAY_NEW_FIXED:
  1032. {
  1033. WASMArrayObjectRef array_obj;
  1034. WASMArrayNewInitValues *init_values = NULL;
  1035. WASMValue *array_init_value = NULL, empty_value = { 0 };
  1036. uint32 type_idx, len;
  1037. if (flag == INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT) {
  1038. type_idx = init_expr->u.array_new_default.type_index;
  1039. len = init_expr->u.array_new_default.length;
  1040. array_init_value = &empty_value;
  1041. }
  1042. else {
  1043. init_values = (WASMArrayNewInitValues *)init_expr->u.data;
  1044. type_idx = init_values->type_idx;
  1045. len = init_values->length;
  1046. if (flag == INIT_EXPR_TYPE_ARRAY_NEW) {
  1047. array_init_value = init_values->elem_data;
  1048. }
  1049. }
  1050. array_obj = instantiate_array_global_recursive(
  1051. module, module_inst, type_idx, flag, len, array_init_value,
  1052. init_values, error_buf, error_buf_size);
  1053. global->initial_value.gc_obj = (void *)array_obj;
  1054. break;
  1055. }
  1056. case INIT_EXPR_TYPE_I31_NEW:
  1057. {
  1058. global->initial_value.gc_obj =
  1059. (wasm_obj_t)wasm_i31_obj_new(init_expr->u.i32);
  1060. break;
  1061. }
  1062. #endif /* end of WASM_ENABLE_GC != 0 */
  1063. default:
  1064. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  1065. &(init_expr->u), sizeof(init_expr->u));
  1066. break;
  1067. }
  1068. global++;
  1069. }
  1070. bh_assert((uint32)(global - globals) == global_count);
  1071. bh_assert(global_data_offset == module->global_data_size);
  1072. (void)module_inst;
  1073. return globals;
  1074. fail:
  1075. wasm_runtime_free(globals);
  1076. return NULL;
  1077. }
  1078. /**
  1079. * Return export function count in module export section.
  1080. */
  1081. static uint32
  1082. get_export_count(const WASMModule *module, uint8 kind)
  1083. {
  1084. WASMExport *export = module->exports;
  1085. uint32 count = 0, i;
  1086. for (i = 0; i < module->export_count; i++, export ++)
  1087. if (export->kind == kind)
  1088. count++;
  1089. return count;
  1090. }
  1091. /**
  1092. * Destroy export function instances.
  1093. */
  1094. static void
  1095. export_functions_deinstantiate(WASMExportFuncInstance *functions)
  1096. {
  1097. if (functions)
  1098. wasm_runtime_free(functions);
  1099. }
  1100. /**
  1101. * Instantiate export functions in a module.
  1102. */
  1103. static WASMExportFuncInstance *
  1104. export_functions_instantiate(const WASMModule *module,
  1105. WASMModuleInstance *module_inst,
  1106. uint32 export_func_count, char *error_buf,
  1107. uint32 error_buf_size)
  1108. {
  1109. WASMExportFuncInstance *export_funcs, *export_func;
  1110. WASMExport *export = module->exports;
  1111. uint32 i;
  1112. uint64 total_size =
  1113. sizeof(WASMExportFuncInstance) * (uint64)export_func_count;
  1114. if (!(export_func = export_funcs =
  1115. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1116. return NULL;
  1117. }
  1118. for (i = 0; i < module->export_count; i++, export ++)
  1119. if (export->kind == EXPORT_KIND_FUNC) {
  1120. export_func->name = export->name;
  1121. export_func->function = &module_inst->e->functions[export->index];
  1122. export_func++;
  1123. }
  1124. bh_assert((uint32)(export_func - export_funcs) == export_func_count);
  1125. return export_funcs;
  1126. }
  1127. #if WASM_ENABLE_TAGS != 0
  1128. /**
  1129. * Destroy export function instances.
  1130. */
  1131. static void
  1132. export_tags_deinstantiate(WASMExportTagInstance *tags)
  1133. {
  1134. if (tags)
  1135. wasm_runtime_free(tags);
  1136. }
  1137. /**
  1138. * Instantiate export functions in a module.
  1139. */
  1140. static WASMExportTagInstance *
  1141. export_tags_instantiate(const WASMModule *module,
  1142. WASMModuleInstance *module_inst,
  1143. uint32 export_tag_count, char *error_buf,
  1144. uint32 error_buf_size)
  1145. {
  1146. WASMExportTagInstance *export_tags, *export_tag;
  1147. WASMExport *export = module->exports;
  1148. uint32 i;
  1149. uint64 total_size =
  1150. sizeof(WASMExportTagInstance) * (uint64)export_tag_count;
  1151. if (!(export_tag = export_tags =
  1152. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1153. return NULL;
  1154. }
  1155. for (i = 0; i < module->export_count; i++, export ++)
  1156. if (export->kind == EXPORT_KIND_TAG) {
  1157. export_tag->name = export->name;
  1158. bh_assert(module_inst->e->tags);
  1159. export_tag->tag = &module_inst->e->tags[export->index];
  1160. export_tag++;
  1161. }
  1162. bh_assert((uint32)(export_tag - export_tags) == export_tag_count);
  1163. return export_tags;
  1164. }
  1165. #endif /* end of WASM_ENABLE_TAGS != 0 */
  1166. #if WASM_ENABLE_MULTI_MODULE != 0
  1167. static void
  1168. export_globals_deinstantiate(WASMExportGlobInstance *globals)
  1169. {
  1170. if (globals)
  1171. wasm_runtime_free(globals);
  1172. }
  1173. static WASMExportGlobInstance *
  1174. export_globals_instantiate(const WASMModule *module,
  1175. WASMModuleInstance *module_inst,
  1176. uint32 export_glob_count, char *error_buf,
  1177. uint32 error_buf_size)
  1178. {
  1179. WASMExportGlobInstance *export_globals, *export_global;
  1180. WASMExport *export = module->exports;
  1181. uint32 i;
  1182. uint64 total_size =
  1183. sizeof(WASMExportGlobInstance) * (uint64)export_glob_count;
  1184. if (!(export_global = export_globals =
  1185. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1186. return NULL;
  1187. }
  1188. for (i = 0; i < module->export_count; i++, export ++)
  1189. if (export->kind == EXPORT_KIND_GLOBAL) {
  1190. export_global->name = export->name;
  1191. export_global->global = &module_inst->e->globals[export->index];
  1192. export_global++;
  1193. }
  1194. bh_assert((uint32)(export_global - export_globals) == export_glob_count);
  1195. return export_globals;
  1196. }
  1197. #endif
  1198. static WASMFunctionInstance *
  1199. lookup_post_instantiate_func(WASMModuleInstance *module_inst,
  1200. const char *func_name)
  1201. {
  1202. WASMFunctionInstance *func;
  1203. WASMFuncType *func_type;
  1204. if (!(func = wasm_lookup_function(module_inst, func_name)))
  1205. /* Not found */
  1206. return NULL;
  1207. func_type = func->u.func->func_type;
  1208. if (!(func_type->param_count == 0 && func_type->result_count == 0))
  1209. /* Not a valid function type, ignore it */
  1210. return NULL;
  1211. return func;
  1212. }
  1213. static bool
  1214. execute_post_instantiate_functions(WASMModuleInstance *module_inst,
  1215. bool is_sub_inst, WASMExecEnv *exec_env_main)
  1216. {
  1217. WASMFunctionInstance *start_func = module_inst->e->start_function;
  1218. WASMFunctionInstance *initialize_func = NULL;
  1219. WASMFunctionInstance *post_inst_func = NULL;
  1220. WASMFunctionInstance *call_ctors_func = NULL;
  1221. #if WASM_ENABLE_LIBC_WASI != 0
  1222. WASMModule *module = module_inst->module;
  1223. #endif
  1224. WASMModuleInstanceCommon *module_inst_main = NULL;
  1225. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1226. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  1227. #endif
  1228. WASMExecEnv *exec_env = NULL, *exec_env_created = NULL;
  1229. bool ret = false;
  1230. #if WASM_ENABLE_LIBC_WASI != 0
  1231. /*
  1232. * WASI reactor instances may assume that _initialize will be called by
  1233. * the environment at most once, and that none of their other exports
  1234. * are accessed before that call.
  1235. */
  1236. if (!is_sub_inst && module->import_wasi_api) {
  1237. initialize_func =
  1238. lookup_post_instantiate_func(module_inst, "_initialize");
  1239. }
  1240. #endif
  1241. /* Execute possible "__post_instantiate" function if wasm app is
  1242. compiled by emsdk's early version */
  1243. if (!is_sub_inst) {
  1244. post_inst_func =
  1245. lookup_post_instantiate_func(module_inst, "__post_instantiate");
  1246. }
  1247. #if WASM_ENABLE_BULK_MEMORY != 0
  1248. /* Only execute the memory init function for main instance since
  1249. the data segments will be dropped once initialized */
  1250. if (!is_sub_inst
  1251. #if WASM_ENABLE_LIBC_WASI != 0
  1252. && !module->import_wasi_api
  1253. #endif
  1254. ) {
  1255. call_ctors_func =
  1256. lookup_post_instantiate_func(module_inst, "__wasm_call_ctors");
  1257. }
  1258. #endif
  1259. if (!start_func && !initialize_func && !post_inst_func
  1260. && !call_ctors_func) {
  1261. /* No post instantiation functions to call */
  1262. return true;
  1263. }
  1264. if (is_sub_inst) {
  1265. bh_assert(exec_env_main);
  1266. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1267. bh_assert(exec_env_tls == exec_env_main);
  1268. (void)exec_env_tls;
  1269. #endif
  1270. exec_env = exec_env_main;
  1271. /* Temporarily replace parent exec_env's module inst to current
  1272. module inst to avoid checking failure when calling the
  1273. wasm functions, and ensure that the exec_env's module inst
  1274. is the correct one. */
  1275. module_inst_main = exec_env_main->module_inst;
  1276. wasm_exec_env_set_module_inst(exec_env,
  1277. (WASMModuleInstanceCommon *)module_inst);
  1278. }
  1279. else {
  1280. /* Try using the existing exec_env */
  1281. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1282. exec_env = exec_env_tls;
  1283. #endif
  1284. #if WASM_ENABLE_THREAD_MGR != 0
  1285. if (!exec_env)
  1286. exec_env = wasm_clusters_search_exec_env(
  1287. (WASMModuleInstanceCommon *)module_inst);
  1288. #endif
  1289. if (!exec_env) {
  1290. if (!(exec_env = exec_env_created = wasm_exec_env_create(
  1291. (WASMModuleInstanceCommon *)module_inst,
  1292. module_inst->default_wasm_stack_size))) {
  1293. wasm_set_exception(module_inst, "allocate memory failed");
  1294. return false;
  1295. }
  1296. }
  1297. else {
  1298. /* Temporarily replace exec_env's module inst with current
  1299. module inst to ensure that the exec_env's module inst
  1300. is the correct one. */
  1301. module_inst_main = exec_env->module_inst;
  1302. wasm_exec_env_set_module_inst(
  1303. exec_env, (WASMModuleInstanceCommon *)module_inst);
  1304. }
  1305. }
  1306. /* Execute start function for both main instance and sub instance */
  1307. if (start_func && !wasm_call_function(exec_env, start_func, 0, NULL)) {
  1308. goto fail;
  1309. }
  1310. #if WASM_ENABLE_LIBC_WASI != 0
  1311. if (initialize_func
  1312. && !wasm_call_function(exec_env, initialize_func, 0, NULL)) {
  1313. goto fail;
  1314. }
  1315. #else
  1316. (void)initialize_func;
  1317. #endif
  1318. if (post_inst_func
  1319. && !wasm_call_function(exec_env, post_inst_func, 0, NULL)) {
  1320. goto fail;
  1321. }
  1322. if (call_ctors_func
  1323. && !wasm_call_function(exec_env, call_ctors_func, 0, NULL)) {
  1324. goto fail;
  1325. }
  1326. ret = true;
  1327. fail:
  1328. if (is_sub_inst) {
  1329. /* Restore the parent exec_env's module inst */
  1330. wasm_exec_env_restore_module_inst(exec_env_main, module_inst_main);
  1331. }
  1332. else {
  1333. if (module_inst_main)
  1334. /* Restore the existing exec_env's module inst */
  1335. wasm_exec_env_restore_module_inst(exec_env, module_inst_main);
  1336. if (exec_env_created)
  1337. wasm_exec_env_destroy(exec_env_created);
  1338. }
  1339. return ret;
  1340. }
  1341. static bool
  1342. execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  1343. WASMFunctionInstance *malloc_func,
  1344. WASMFunctionInstance *retain_func, uint64 size,
  1345. uint64 *p_result)
  1346. {
  1347. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1348. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  1349. #endif
  1350. WASMExecEnv *exec_env_created = NULL;
  1351. WASMModuleInstanceCommon *module_inst_old = NULL;
  1352. union {
  1353. uint32 u32[3];
  1354. uint64 u64;
  1355. } argv;
  1356. uint32 argc;
  1357. bool ret;
  1358. #if WASM_ENABLE_MEMORY64 != 0
  1359. bool is_memory64 = module_inst->memories[0]->is_memory64;
  1360. if (is_memory64) {
  1361. argc = 2;
  1362. PUT_I64_TO_ADDR(&argv.u64, size);
  1363. }
  1364. else
  1365. #endif
  1366. {
  1367. argc = 1;
  1368. argv.u32[0] = (uint32)size;
  1369. }
  1370. /* if __retain is exported, then this module is compiled by
  1371. assemblyscript, the memory should be managed by as's runtime,
  1372. in this case we need to call the retain function after malloc
  1373. the memory */
  1374. if (retain_func) {
  1375. /* the malloc function from assemblyscript is:
  1376. function __new(size: usize, id: u32)
  1377. id = 0 means this is an ArrayBuffer object */
  1378. argv.u32[argc] = 0;
  1379. argc++;
  1380. }
  1381. if (exec_env) {
  1382. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1383. if (exec_env_tls) {
  1384. bh_assert(exec_env_tls == exec_env);
  1385. }
  1386. #endif
  1387. bh_assert(exec_env->module_inst
  1388. == (WASMModuleInstanceCommon *)module_inst);
  1389. }
  1390. else {
  1391. /* Try using the existing exec_env */
  1392. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1393. exec_env = exec_env_tls;
  1394. #endif
  1395. #if WASM_ENABLE_THREAD_MGR != 0
  1396. if (!exec_env)
  1397. exec_env = wasm_clusters_search_exec_env(
  1398. (WASMModuleInstanceCommon *)module_inst);
  1399. #endif
  1400. if (!exec_env) {
  1401. if (!(exec_env = exec_env_created = wasm_exec_env_create(
  1402. (WASMModuleInstanceCommon *)module_inst,
  1403. module_inst->default_wasm_stack_size))) {
  1404. wasm_set_exception(module_inst, "allocate memory failed");
  1405. return false;
  1406. }
  1407. }
  1408. else {
  1409. /* Temporarily replace exec_env's module inst with current
  1410. module inst to ensure that the exec_env's module inst
  1411. is the correct one. */
  1412. module_inst_old = exec_env->module_inst;
  1413. wasm_exec_env_set_module_inst(
  1414. exec_env, (WASMModuleInstanceCommon *)module_inst);
  1415. }
  1416. }
  1417. ret = wasm_call_function(exec_env, malloc_func, argc, argv.u32);
  1418. if (retain_func && ret)
  1419. ret = wasm_call_function(exec_env, retain_func, 1, argv.u32);
  1420. if (module_inst_old)
  1421. /* Restore the existing exec_env's module inst */
  1422. wasm_exec_env_restore_module_inst(exec_env, module_inst_old);
  1423. if (exec_env_created)
  1424. wasm_exec_env_destroy(exec_env_created);
  1425. if (ret) {
  1426. #if WASM_ENABLE_MEMORY64 != 0
  1427. if (is_memory64)
  1428. *p_result = argv.u64;
  1429. else
  1430. #endif
  1431. {
  1432. *p_result = argv.u32[0];
  1433. }
  1434. }
  1435. return ret;
  1436. }
  1437. static bool
  1438. execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  1439. WASMFunctionInstance *free_func, uint64 offset)
  1440. {
  1441. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1442. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  1443. #endif
  1444. WASMExecEnv *exec_env_created = NULL;
  1445. WASMModuleInstanceCommon *module_inst_old = NULL;
  1446. union {
  1447. uint32 u32[2];
  1448. uint64 u64;
  1449. } argv;
  1450. uint32 argc;
  1451. bool ret;
  1452. #if WASM_ENABLE_MEMORY64 != 0
  1453. if (module_inst->memories[0]->is_memory64) {
  1454. PUT_I64_TO_ADDR(&argv.u64, offset);
  1455. argc = 2;
  1456. }
  1457. else
  1458. #endif
  1459. {
  1460. argv.u32[0] = (uint32)offset;
  1461. argc = 1;
  1462. }
  1463. if (exec_env) {
  1464. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1465. if (exec_env_tls) {
  1466. bh_assert(exec_env_tls == exec_env);
  1467. }
  1468. #endif
  1469. bh_assert(exec_env->module_inst
  1470. == (WASMModuleInstanceCommon *)module_inst);
  1471. }
  1472. else {
  1473. /* Try using the existing exec_env */
  1474. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1475. exec_env = exec_env_tls;
  1476. #endif
  1477. #if WASM_ENABLE_THREAD_MGR != 0
  1478. if (!exec_env)
  1479. exec_env = wasm_clusters_search_exec_env(
  1480. (WASMModuleInstanceCommon *)module_inst);
  1481. #endif
  1482. if (!exec_env) {
  1483. if (!(exec_env = exec_env_created = wasm_exec_env_create(
  1484. (WASMModuleInstanceCommon *)module_inst,
  1485. module_inst->default_wasm_stack_size))) {
  1486. wasm_set_exception(module_inst, "allocate memory failed");
  1487. return false;
  1488. }
  1489. }
  1490. else {
  1491. /* Temporarily replace exec_env's module inst with current
  1492. module inst to ensure that the exec_env's module inst
  1493. is the correct one. */
  1494. module_inst_old = exec_env->module_inst;
  1495. wasm_exec_env_set_module_inst(
  1496. exec_env, (WASMModuleInstanceCommon *)module_inst);
  1497. }
  1498. }
  1499. ret = wasm_call_function(exec_env, free_func, argc, argv.u32);
  1500. if (module_inst_old)
  1501. /* Restore the existing exec_env's module inst */
  1502. wasm_exec_env_restore_module_inst(exec_env, module_inst_old);
  1503. if (exec_env_created)
  1504. wasm_exec_env_destroy(exec_env_created);
  1505. return ret;
  1506. }
  1507. static bool
  1508. check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
  1509. uint32 error_buf_size)
  1510. {
  1511. WASMModule *module = module_inst->module;
  1512. uint32 i;
  1513. for (i = 0; i < module->import_function_count; i++) {
  1514. WASMFunctionImport *func =
  1515. &((module->import_functions + i)->u.function);
  1516. if (!func->func_ptr_linked
  1517. #if WASM_ENABLE_MULTI_MODULE != 0
  1518. && !func->import_func_linked
  1519. #endif
  1520. ) {
  1521. LOG_WARNING("warning: failed to link import function (%s, %s)",
  1522. func->module_name, func->field_name);
  1523. }
  1524. }
  1525. for (i = 0; i < module->import_global_count; i++) {
  1526. WASMGlobalImport *global = &((module->import_globals + i)->u.global);
  1527. if (!global->is_linked) {
  1528. #if WASM_ENABLE_SPEC_TEST != 0
  1529. set_error_buf(error_buf, error_buf_size,
  1530. "unknown import or incompatible import type");
  1531. return false;
  1532. #else
  1533. set_error_buf_v(error_buf, error_buf_size,
  1534. "failed to link import global (%s, %s)",
  1535. global->module_name, global->field_name);
  1536. return false;
  1537. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  1538. }
  1539. }
  1540. for (i = 0; i < module->import_table_count; i++) {
  1541. WASMTableImport *table = &((module->import_tables + i)->u.table);
  1542. if (!wasm_runtime_is_built_in_module(table->module_name)
  1543. #if WASM_ENABLE_MULTI_MODULE != 0
  1544. && !table->import_table_linked
  1545. #endif
  1546. ) {
  1547. set_error_buf_v(error_buf, error_buf_size,
  1548. "failed to link import table (%s, %s)",
  1549. table->module_name, table->field_name);
  1550. return false;
  1551. }
  1552. }
  1553. for (i = 0; i < module->import_memory_count; i++) {
  1554. WASMMemoryImport *memory = &((module->import_memories + i)->u.memory);
  1555. if (!wasm_runtime_is_built_in_module(memory->module_name)
  1556. #if WASM_ENABLE_MULTI_MODULE != 0
  1557. && !memory->import_memory_linked
  1558. #endif
  1559. ) {
  1560. set_error_buf_v(error_buf, error_buf_size,
  1561. "failed to link import memory (%s, %s)",
  1562. memory->module_name, memory->field_name);
  1563. return false;
  1564. }
  1565. }
  1566. #if WASM_ENABLE_MULTI_MODULE != 0
  1567. #if WASM_ENABLE_TAGS != 0
  1568. for (i = 0; i < module->import_tag_count; i++) {
  1569. WASMTagImport *tag = &((module->import_tags + i)->u.tag);
  1570. if (!tag->import_tag_linked) {
  1571. set_error_buf_v(error_buf, error_buf_size,
  1572. "failed to link import tag (%s, %s)",
  1573. tag->module_name, tag->field_name);
  1574. return false;
  1575. }
  1576. }
  1577. #endif /* WASM_ENABLE_TAGS != 0 */
  1578. #endif
  1579. return true;
  1580. }
  1581. #if WASM_ENABLE_JIT != 0
  1582. static bool
  1583. init_func_ptrs(WASMModuleInstance *module_inst, WASMModule *module,
  1584. char *error_buf, uint32 error_buf_size)
  1585. {
  1586. uint32 i;
  1587. void **func_ptrs;
  1588. uint64 total_size = (uint64)sizeof(void *) * module_inst->e->function_count;
  1589. /* Allocate memory */
  1590. if (!(func_ptrs = module_inst->func_ptrs =
  1591. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1592. return false;
  1593. }
  1594. /* Set import function pointers */
  1595. for (i = 0; i < module->import_function_count; i++, func_ptrs++) {
  1596. WASMFunctionImport *import_func =
  1597. &module->import_functions[i].u.function;
  1598. /* TODO: handle multi module */
  1599. *func_ptrs = import_func->func_ptr_linked;
  1600. }
  1601. /* The defined function pointers will be set in
  1602. wasm_runtime_set_running_mode, no need to set them here */
  1603. return true;
  1604. }
  1605. #endif /* end of WASM_ENABLE_JIT != 0 */
  1606. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1607. static uint32
  1608. get_smallest_type_idx(WASMModule *module, WASMFuncType *func_type)
  1609. {
  1610. uint32 i;
  1611. for (i = 0; i < module->type_count; i++) {
  1612. if (func_type == (WASMFuncType *)module->types[i])
  1613. return i;
  1614. }
  1615. bh_assert(0);
  1616. return -1;
  1617. }
  1618. static bool
  1619. init_func_type_indexes(WASMModuleInstance *module_inst, char *error_buf,
  1620. uint32 error_buf_size)
  1621. {
  1622. uint32 i;
  1623. uint64 total_size = (uint64)sizeof(uint32) * module_inst->e->function_count;
  1624. /* Allocate memory */
  1625. if (!(module_inst->func_type_indexes =
  1626. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1627. return false;
  1628. }
  1629. for (i = 0; i < module_inst->e->function_count; i++) {
  1630. WASMFunctionInstance *func_inst = module_inst->e->functions + i;
  1631. WASMFuncType *func_type = func_inst->is_import_func
  1632. ? func_inst->u.func_import->func_type
  1633. : func_inst->u.func->func_type;
  1634. module_inst->func_type_indexes[i] =
  1635. get_smallest_type_idx(module_inst->module, func_type);
  1636. }
  1637. return true;
  1638. }
  1639. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  1640. #if WASM_ENABLE_GC != 0
  1641. void *
  1642. wasm_create_func_obj(WASMModuleInstance *module_inst, uint32 func_idx,
  1643. bool throw_exce, char *error_buf, uint32 error_buf_size)
  1644. {
  1645. WASMModule *module = module_inst->module;
  1646. WASMRttTypeRef rtt_type;
  1647. WASMFuncObjectRef func_obj;
  1648. WASMFuncType *func_type;
  1649. uint32 type_idx;
  1650. if (throw_exce) {
  1651. error_buf = module_inst->cur_exception;
  1652. error_buf_size = sizeof(module_inst->cur_exception);
  1653. }
  1654. if (func_idx >= module->import_function_count + module->function_count) {
  1655. set_error_buf_v(error_buf, error_buf_size, "unknown function %d",
  1656. func_idx);
  1657. return NULL;
  1658. }
  1659. if (func_idx < module->import_function_count) {
  1660. func_type = module->import_functions[func_idx].u.function.func_type;
  1661. type_idx = module->import_functions[func_idx].u.function.type_idx;
  1662. }
  1663. else {
  1664. func_type = module->functions[func_idx - module->import_function_count]
  1665. ->func_type;
  1666. type_idx = module->functions[func_idx - module->import_function_count]
  1667. ->type_idx;
  1668. }
  1669. if (!(rtt_type = wasm_rtt_type_new((WASMType *)func_type, type_idx,
  1670. module->rtt_types, module->type_count,
  1671. &module->rtt_type_lock))) {
  1672. set_error_buf(error_buf, error_buf_size, "create rtt object failed");
  1673. return NULL;
  1674. }
  1675. if (!(func_obj = wasm_func_obj_new_internal(
  1676. module_inst->e->common.gc_heap_handle, rtt_type, func_idx))) {
  1677. set_error_buf(error_buf, error_buf_size, "create func object failed");
  1678. return NULL;
  1679. }
  1680. return func_obj;
  1681. }
  1682. static bool
  1683. wasm_global_traverse_gc_rootset(WASMModuleInstance *module_inst, void *heap)
  1684. {
  1685. WASMGlobalInstance *global = module_inst->e->globals;
  1686. WASMGlobalInstance *global_end = global + module_inst->e->global_count;
  1687. uint8 *global_data = module_inst->global_data;
  1688. WASMObjectRef gc_obj;
  1689. while (global < global_end) {
  1690. if (wasm_is_type_reftype(global->type)) {
  1691. gc_obj = GET_REF_FROM_ADDR(
  1692. (uint32 *)(global_data + global->data_offset));
  1693. if (wasm_obj_is_created_from_heap(gc_obj)) {
  1694. if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))
  1695. return false;
  1696. }
  1697. }
  1698. global++;
  1699. }
  1700. return true;
  1701. }
  1702. static bool
  1703. wasm_table_traverse_gc_rootset(WASMModuleInstance *module_inst, void *heap)
  1704. {
  1705. WASMTableInstance **tables = module_inst->tables, *table;
  1706. uint32 table_count = module_inst->table_count, i, j;
  1707. WASMObjectRef gc_obj, *table_elems;
  1708. for (i = 0; i < table_count; i++) {
  1709. table = tables[i];
  1710. table_elems = (WASMObjectRef *)table->elems;
  1711. for (j = 0; j < table->cur_size; j++) {
  1712. gc_obj = table_elems[j];
  1713. if (wasm_obj_is_created_from_heap(gc_obj)) {
  1714. if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))
  1715. return false;
  1716. }
  1717. }
  1718. }
  1719. return true;
  1720. }
  1721. static bool
  1722. local_object_refs_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
  1723. {
  1724. WASMLocalObjectRef *r;
  1725. WASMObjectRef gc_obj;
  1726. for (r = exec_env->cur_local_object_ref; r; r = r->prev) {
  1727. gc_obj = r->val;
  1728. if (wasm_obj_is_created_from_heap(gc_obj)) {
  1729. if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))
  1730. return false;
  1731. }
  1732. }
  1733. return true;
  1734. }
  1735. bool
  1736. wasm_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
  1737. {
  1738. WASMModuleInstance *module_inst =
  1739. (WASMModuleInstance *)exec_env->module_inst;
  1740. bool ret;
  1741. ret = wasm_global_traverse_gc_rootset(module_inst, heap);
  1742. if (!ret)
  1743. return ret;
  1744. ret = wasm_table_traverse_gc_rootset(module_inst, heap);
  1745. if (!ret)
  1746. return ret;
  1747. ret = local_object_refs_traverse_gc_rootset(exec_env, heap);
  1748. if (!ret)
  1749. return ret;
  1750. return wasm_interp_traverse_gc_rootset(exec_env, heap);
  1751. }
  1752. #endif /* end of WASM_ENABLE_GC != 0 */
  1753. static bool
  1754. set_running_mode(WASMModuleInstance *module_inst, RunningMode running_mode,
  1755. bool first_time_set)
  1756. {
  1757. WASMModule *module = module_inst->module;
  1758. if (running_mode == Mode_Default) {
  1759. #if WASM_ENABLE_FAST_JIT == 0 && WASM_ENABLE_JIT == 0
  1760. running_mode = Mode_Interp;
  1761. #elif WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT == 0
  1762. running_mode = Mode_Fast_JIT;
  1763. #elif WASM_ENABLE_FAST_JIT == 0 && WASM_ENABLE_JIT != 0
  1764. running_mode = Mode_LLVM_JIT;
  1765. #else /* WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 */
  1766. #if WASM_ENABLE_LAZY_JIT == 0
  1767. running_mode = Mode_LLVM_JIT;
  1768. #else
  1769. running_mode = Mode_Multi_Tier_JIT;
  1770. #endif
  1771. #endif
  1772. }
  1773. if (!wasm_runtime_is_running_mode_supported(running_mode))
  1774. return false;
  1775. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1776. && WASM_ENABLE_LAZY_JIT != 0) /* No possible multi-tier JIT */
  1777. (void)first_time_set;
  1778. module_inst->e->running_mode = running_mode;
  1779. if (running_mode == Mode_Interp) {
  1780. /* Do nothing for Mode_Interp */
  1781. }
  1782. else if (running_mode == Mode_Fast_JIT) {
  1783. /* Do nothing for Mode_Fast_JIT since
  1784. module_inst->fast_jit_func_ptrs is same as
  1785. module->fast_jit_func_ptrs */
  1786. }
  1787. #if WASM_ENABLE_JIT != 0
  1788. else if (running_mode == Mode_LLVM_JIT) {
  1789. /* Set defined function pointers */
  1790. bh_memcpy_s(module_inst->func_ptrs + module->import_function_count,
  1791. sizeof(void *) * module->function_count, module->func_ptrs,
  1792. sizeof(void *) * module->function_count);
  1793. }
  1794. #endif
  1795. else {
  1796. bh_assert(0);
  1797. }
  1798. #else /* Possible multi-tier JIT */
  1799. os_mutex_lock(&module->instance_list_lock);
  1800. module_inst->e->running_mode = running_mode;
  1801. if (running_mode == Mode_Interp) {
  1802. /* Do nothing for Mode_Interp */
  1803. }
  1804. #if WASM_ENABLE_FAST_JIT != 0
  1805. else if (running_mode == Mode_Fast_JIT) {
  1806. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  1807. uint32 i;
  1808. /* Allocate memory for fast_jit_func_ptrs if needed */
  1809. if (!module_inst->fast_jit_func_ptrs
  1810. || module_inst->fast_jit_func_ptrs == module->fast_jit_func_ptrs) {
  1811. uint64 total_size = (uint64)sizeof(void *) * module->function_count;
  1812. if (!(module_inst->fast_jit_func_ptrs =
  1813. runtime_malloc(total_size, NULL, 0))) {
  1814. os_mutex_unlock(&module->instance_list_lock);
  1815. return false;
  1816. }
  1817. }
  1818. for (i = 0; i < module->function_count; i++) {
  1819. if (module->functions[i]->fast_jit_jitted_code) {
  1820. /* current fast jit function has been compiled */
  1821. module_inst->fast_jit_func_ptrs[i] =
  1822. module->functions[i]->fast_jit_jitted_code;
  1823. }
  1824. else {
  1825. module_inst->fast_jit_func_ptrs[i] =
  1826. jit_globals->compile_fast_jit_and_then_call;
  1827. }
  1828. }
  1829. }
  1830. #endif
  1831. #if WASM_ENABLE_JIT != 0
  1832. else if (running_mode == Mode_LLVM_JIT) {
  1833. void **llvm_jit_func_ptrs;
  1834. uint32 i;
  1835. /* Notify backend threads to start llvm jit compilation */
  1836. module->enable_llvm_jit_compilation = true;
  1837. /* Wait until llvm jit finishes initialization */
  1838. os_mutex_lock(&module->tierup_wait_lock);
  1839. while (!module->llvm_jit_inited) {
  1840. os_cond_reltimedwait(&module->tierup_wait_cond,
  1841. &module->tierup_wait_lock, 10000);
  1842. if (module->orcjit_stop_compiling) {
  1843. /* init_llvm_jit_functions_stage2 failed */
  1844. os_mutex_unlock(&module->tierup_wait_lock);
  1845. os_mutex_unlock(&module->instance_list_lock);
  1846. return false;
  1847. }
  1848. }
  1849. os_mutex_unlock(&module->tierup_wait_lock);
  1850. llvm_jit_func_ptrs =
  1851. module_inst->func_ptrs + module->import_function_count;
  1852. for (i = 0; i < module->function_count; i++) {
  1853. llvm_jit_func_ptrs[i] = module->functions[i]->llvm_jit_func_ptr;
  1854. }
  1855. }
  1856. #endif
  1857. else if (running_mode == Mode_Multi_Tier_JIT) {
  1858. /* Notify backend threads to start llvm jit compilation */
  1859. module->enable_llvm_jit_compilation = true;
  1860. /* Free fast_jit_func_ptrs if it is allocated before */
  1861. if (module_inst->fast_jit_func_ptrs
  1862. && module_inst->fast_jit_func_ptrs != module->fast_jit_func_ptrs) {
  1863. wasm_runtime_free(module_inst->fast_jit_func_ptrs);
  1864. }
  1865. module_inst->fast_jit_func_ptrs = module->fast_jit_func_ptrs;
  1866. /* Copy all llvm jit func ptrs from the module */
  1867. bh_memcpy_s(module_inst->func_ptrs + module->import_function_count,
  1868. sizeof(void *) * module->function_count, module->func_ptrs,
  1869. sizeof(void *) * module->function_count);
  1870. }
  1871. else {
  1872. bh_assert(0);
  1873. }
  1874. /* Add module instance into module's instance list if not added */
  1875. if (first_time_set) {
  1876. bool found = false;
  1877. WASMModuleInstance *node = module->instance_list;
  1878. while (node) {
  1879. if (node == module_inst) {
  1880. found = true;
  1881. break;
  1882. }
  1883. node = node->e->next;
  1884. }
  1885. if (!found) {
  1886. module_inst->e->next = module->instance_list;
  1887. module->instance_list = module_inst;
  1888. }
  1889. }
  1890. os_mutex_unlock(&module->instance_list_lock);
  1891. #endif /* end of !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1892. && WASM_ENABLE_LAZY_JIT != 0) */
  1893. (void)module;
  1894. return true;
  1895. }
  1896. bool
  1897. wasm_set_running_mode(WASMModuleInstance *module_inst, RunningMode running_mode)
  1898. {
  1899. return set_running_mode(module_inst, running_mode, false);
  1900. }
  1901. /**
  1902. * Instantiate module
  1903. */
  1904. WASMModuleInstance *
  1905. wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
  1906. WASMExecEnv *exec_env_main, uint32 stack_size,
  1907. uint32 heap_size, uint32 max_memory_pages, char *error_buf,
  1908. uint32 error_buf_size)
  1909. {
  1910. WASMModuleInstance *module_inst;
  1911. WASMGlobalInstance *globals = NULL, *global;
  1912. WASMTableInstance *first_table;
  1913. uint32 global_count, i;
  1914. uint32 length, extra_info_offset;
  1915. mem_offset_t base_offset;
  1916. uint32 module_inst_struct_size =
  1917. offsetof(WASMModuleInstance, global_table_data.bytes);
  1918. uint64 module_inst_mem_inst_size;
  1919. uint64 total_size, table_size = 0;
  1920. uint8 *global_data, *global_data_end;
  1921. #if WASM_ENABLE_MULTI_MODULE != 0
  1922. bool ret = false;
  1923. #endif
  1924. const bool is_sub_inst = parent != NULL;
  1925. if (!module)
  1926. return NULL;
  1927. /* Check the heap size */
  1928. heap_size = align_uint(heap_size, 8);
  1929. if (heap_size > APP_HEAP_SIZE_MAX)
  1930. heap_size = APP_HEAP_SIZE_MAX;
  1931. module_inst_mem_inst_size =
  1932. sizeof(WASMMemoryInstance)
  1933. * ((uint64)module->import_memory_count + module->memory_count);
  1934. #if WASM_ENABLE_JIT != 0
  1935. /* If the module doesn't have memory, reserve one mem_info space
  1936. with empty content to align with llvm jit compiler */
  1937. if (module_inst_mem_inst_size == 0)
  1938. module_inst_mem_inst_size = (uint64)sizeof(WASMMemoryInstance);
  1939. #endif
  1940. /* Size of module inst, memory instances and global data */
  1941. total_size = (uint64)module_inst_struct_size + module_inst_mem_inst_size
  1942. + module->global_data_size;
  1943. /* Calculate the size of table data */
  1944. for (i = 0; i < module->import_table_count; i++) {
  1945. WASMTableImport *import_table = &module->import_tables[i].u.table;
  1946. table_size += offsetof(WASMTableInstance, elems);
  1947. #if WASM_ENABLE_MULTI_MODULE != 0
  1948. table_size += (uint64)sizeof(table_elem_type_t)
  1949. * import_table->table_type.max_size;
  1950. #else
  1951. table_size += (uint64)sizeof(table_elem_type_t)
  1952. * (import_table->table_type.possible_grow
  1953. ? import_table->table_type.max_size
  1954. : import_table->table_type.init_size);
  1955. #endif
  1956. }
  1957. for (i = 0; i < module->table_count; i++) {
  1958. WASMTable *table = module->tables + i;
  1959. table_size += offsetof(WASMTableInstance, elems);
  1960. #if WASM_ENABLE_MULTI_MODULE != 0
  1961. table_size +=
  1962. (uint64)sizeof(table_elem_type_t) * table->table_type.max_size;
  1963. #else
  1964. table_size +=
  1965. (uint64)sizeof(table_elem_type_t)
  1966. * (table->table_type.possible_grow ? table->table_type.max_size
  1967. : table->table_type.init_size);
  1968. #endif
  1969. }
  1970. total_size += table_size;
  1971. /* The offset of WASMModuleInstanceExtra, make it 8-byte aligned */
  1972. total_size = (total_size + 7LL) & ~7LL;
  1973. extra_info_offset = (uint32)total_size;
  1974. total_size += sizeof(WASMModuleInstanceExtra);
  1975. /* Allocate the memory for module instance with memory instances,
  1976. global data, table data appended at the end */
  1977. if (!(module_inst =
  1978. runtime_malloc(total_size, error_buf, error_buf_size))) {
  1979. return NULL;
  1980. }
  1981. module_inst->module_type = Wasm_Module_Bytecode;
  1982. module_inst->module = module;
  1983. module_inst->e =
  1984. (WASMModuleInstanceExtra *)((uint8 *)module_inst + extra_info_offset);
  1985. #if WASM_ENABLE_MULTI_MODULE != 0
  1986. module_inst->e->sub_module_inst_list =
  1987. &module_inst->e->sub_module_inst_list_head;
  1988. ret = wasm_runtime_sub_module_instantiate(
  1989. (WASMModuleCommon *)module, (WASMModuleInstanceCommon *)module_inst,
  1990. stack_size, heap_size, max_memory_pages, error_buf, error_buf_size);
  1991. if (!ret) {
  1992. LOG_DEBUG("build a sub module list failed");
  1993. goto fail;
  1994. }
  1995. #endif
  1996. #if WASM_ENABLE_BULK_MEMORY != 0
  1997. if (module->data_seg_count > 0) {
  1998. module_inst->e->common.data_dropped =
  1999. bh_bitmap_new(0, module->data_seg_count);
  2000. if (module_inst->e->common.data_dropped == NULL) {
  2001. LOG_DEBUG("failed to allocate bitmaps");
  2002. set_error_buf(error_buf, error_buf_size,
  2003. "failed to allocate bitmaps");
  2004. goto fail;
  2005. }
  2006. for (i = 0; i < module->data_seg_count; i++) {
  2007. if (!module->data_segments[i]->is_passive)
  2008. bh_bitmap_set_bit(module_inst->e->common.data_dropped, i);
  2009. }
  2010. }
  2011. #endif
  2012. #if WASM_ENABLE_REF_TYPES != 0
  2013. if (module->table_seg_count > 0) {
  2014. module_inst->e->common.elem_dropped =
  2015. bh_bitmap_new(0, module->table_seg_count);
  2016. if (module_inst->e->common.elem_dropped == NULL) {
  2017. LOG_DEBUG("failed to allocate bitmaps");
  2018. set_error_buf(error_buf, error_buf_size,
  2019. "failed to allocate bitmaps");
  2020. goto fail;
  2021. }
  2022. for (i = 0; i < module->table_seg_count; i++) {
  2023. if (wasm_elem_is_active(module->table_segments[i].mode))
  2024. bh_bitmap_set_bit(module_inst->e->common.elem_dropped, i);
  2025. }
  2026. }
  2027. #endif
  2028. #if WASM_ENABLE_GC != 0
  2029. if (!is_sub_inst) {
  2030. uint32 gc_heap_size = wasm_runtime_get_gc_heap_size_default();
  2031. if (gc_heap_size < GC_HEAP_SIZE_MIN)
  2032. gc_heap_size = GC_HEAP_SIZE_MIN;
  2033. if (gc_heap_size > GC_HEAP_SIZE_MAX)
  2034. gc_heap_size = GC_HEAP_SIZE_MAX;
  2035. module_inst->e->common.gc_heap_pool =
  2036. runtime_malloc(gc_heap_size, error_buf, error_buf_size);
  2037. if (!module_inst->e->common.gc_heap_pool)
  2038. goto fail;
  2039. module_inst->e->common.gc_heap_handle = mem_allocator_create(
  2040. module_inst->e->common.gc_heap_pool, gc_heap_size);
  2041. if (!module_inst->e->common.gc_heap_handle)
  2042. goto fail;
  2043. }
  2044. #endif
  2045. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2046. if (!(module_inst->frames = runtime_malloc((uint64)sizeof(Vector),
  2047. error_buf, error_buf_size))) {
  2048. goto fail;
  2049. }
  2050. #endif
  2051. /* Instantiate global firstly to get the mutable data size */
  2052. global_count = module->import_global_count + module->global_count;
  2053. if (global_count
  2054. && !(globals = globals_instantiate(module, module_inst, error_buf,
  2055. error_buf_size))) {
  2056. goto fail;
  2057. }
  2058. module_inst->e->global_count = global_count;
  2059. module_inst->e->globals = globals;
  2060. module_inst->global_data = (uint8 *)module_inst + module_inst_struct_size
  2061. + module_inst_mem_inst_size;
  2062. module_inst->global_data_size = module->global_data_size;
  2063. first_table = (WASMTableInstance *)(module_inst->global_data
  2064. + module->global_data_size);
  2065. module_inst->memory_count =
  2066. module->import_memory_count + module->memory_count;
  2067. module_inst->table_count = module->import_table_count + module->table_count;
  2068. module_inst->e->function_count =
  2069. module->import_function_count + module->function_count;
  2070. #if WASM_ENABLE_TAGS != 0
  2071. module_inst->e->tag_count = module->import_tag_count + module->tag_count;
  2072. #endif
  2073. /* export */
  2074. module_inst->export_func_count = get_export_count(module, EXPORT_KIND_FUNC);
  2075. #if WASM_ENABLE_MULTI_MODULE != 0
  2076. module_inst->export_table_count =
  2077. get_export_count(module, EXPORT_KIND_TABLE);
  2078. module_inst->export_memory_count =
  2079. get_export_count(module, EXPORT_KIND_MEMORY);
  2080. #if WASM_ENABLE_TAGS != 0
  2081. module_inst->e->export_tag_count =
  2082. get_export_count(module, EXPORT_KIND_TAG);
  2083. #endif
  2084. module_inst->export_global_count =
  2085. get_export_count(module, EXPORT_KIND_GLOBAL);
  2086. #endif
  2087. /* Instantiate memories/tables/functions/tags */
  2088. if ((module_inst->memory_count > 0
  2089. && !(module_inst->memories = memories_instantiate(
  2090. module, module_inst, parent, heap_size, max_memory_pages,
  2091. error_buf, error_buf_size)))
  2092. || (module_inst->table_count > 0
  2093. && !(module_inst->tables =
  2094. tables_instantiate(module, module_inst, first_table,
  2095. error_buf, error_buf_size)))
  2096. || (module_inst->e->function_count > 0
  2097. && !(module_inst->e->functions = functions_instantiate(
  2098. module, module_inst, error_buf, error_buf_size)))
  2099. || (module_inst->export_func_count > 0
  2100. && !(module_inst->export_functions = export_functions_instantiate(
  2101. module, module_inst, module_inst->export_func_count,
  2102. error_buf, error_buf_size)))
  2103. #if WASM_ENABLE_TAGS != 0
  2104. || (module_inst->e->tag_count > 0
  2105. && !(module_inst->e->tags = tags_instantiate(
  2106. module, module_inst, error_buf, error_buf_size)))
  2107. || (module_inst->e->export_tag_count > 0
  2108. && !(module_inst->e->export_tags = export_tags_instantiate(
  2109. module, module_inst, module_inst->e->export_tag_count,
  2110. error_buf, error_buf_size)))
  2111. #endif
  2112. #if WASM_ENABLE_MULTI_MODULE != 0
  2113. || (module_inst->export_global_count > 0
  2114. && !(module_inst->export_globals = export_globals_instantiate(
  2115. module, module_inst, module_inst->export_global_count,
  2116. error_buf, error_buf_size)))
  2117. #endif
  2118. #if WASM_ENABLE_JIT != 0
  2119. || (module_inst->e->function_count > 0
  2120. && !init_func_ptrs(module_inst, module, error_buf, error_buf_size))
  2121. #endif
  2122. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  2123. || (module_inst->e->function_count > 0
  2124. && !init_func_type_indexes(module_inst, error_buf, error_buf_size))
  2125. #endif
  2126. ) {
  2127. goto fail;
  2128. }
  2129. if (global_count > 0) {
  2130. /* Initialize the global data */
  2131. global_data = module_inst->global_data;
  2132. global_data_end = global_data + module->global_data_size;
  2133. global = globals;
  2134. for (i = 0; i < global_count; i++, global++) {
  2135. switch (global->type) {
  2136. case VALUE_TYPE_I32:
  2137. case VALUE_TYPE_F32:
  2138. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2139. case VALUE_TYPE_FUNCREF:
  2140. case VALUE_TYPE_EXTERNREF:
  2141. #endif
  2142. *(int32 *)global_data = global->initial_value.i32;
  2143. global_data += sizeof(int32);
  2144. break;
  2145. case VALUE_TYPE_I64:
  2146. case VALUE_TYPE_F64:
  2147. bh_memcpy_s(global_data,
  2148. (uint32)(global_data_end - global_data),
  2149. &global->initial_value.i64, sizeof(int64));
  2150. global_data += sizeof(int64);
  2151. break;
  2152. #if WASM_ENABLE_SIMD != 0
  2153. case VALUE_TYPE_V128:
  2154. bh_memcpy_s(global_data, (uint32)sizeof(V128),
  2155. &global->initial_value.v128, sizeof(V128));
  2156. global_data += sizeof(V128);
  2157. break;
  2158. #endif
  2159. #if WASM_ENABLE_GC != 0
  2160. case VALUE_TYPE_EXTERNREF:
  2161. /* the initial value should be a null reference */
  2162. bh_assert(global->initial_value.gc_obj == NULL_REF);
  2163. STORE_PTR((void **)global_data, NULL_REF);
  2164. global_data += sizeof(void *);
  2165. break;
  2166. #endif
  2167. default:
  2168. {
  2169. #if WASM_ENABLE_GC != 0
  2170. InitializerExpression *global_init = NULL;
  2171. bh_assert(wasm_is_type_reftype(global->type));
  2172. if (i >= module->import_global_count) {
  2173. global_init =
  2174. &module->globals[i - module->import_global_count]
  2175. .init_expr;
  2176. }
  2177. if (global->type == REF_TYPE_NULLFUNCREF
  2178. || global->type == REF_TYPE_NULLEXTERNREF
  2179. || global->type == REF_TYPE_NULLREF) {
  2180. STORE_PTR((void **)global_data, NULL_REF);
  2181. global_data += sizeof(void *);
  2182. break;
  2183. }
  2184. /* We can't create funcref obj during global instantiation
  2185. * since the functions are not instantiated yet, so we need
  2186. * to defer the initialization here */
  2187. if (global_init
  2188. && (global_init->init_expr_type
  2189. == INIT_EXPR_TYPE_FUNCREF_CONST)
  2190. && wasm_reftype_is_subtype_of(
  2191. global->type, global->ref_type, REF_TYPE_FUNCREF,
  2192. NULL, module_inst->module->types,
  2193. module_inst->module->type_count)) {
  2194. WASMFuncObjectRef func_obj = NULL;
  2195. /* UINT32_MAX indicates that it is a null reference */
  2196. if ((uint32)global->initial_value.i32 != UINT32_MAX) {
  2197. if (!(func_obj = wasm_create_func_obj(
  2198. module_inst, global->initial_value.i32,
  2199. false, error_buf, error_buf_size)))
  2200. goto fail;
  2201. }
  2202. STORE_PTR((void **)global_data, func_obj);
  2203. global_data += sizeof(void *);
  2204. /* Also update the inital_value since other globals may
  2205. * refer to this */
  2206. global->initial_value.gc_obj = (wasm_obj_t)func_obj;
  2207. break;
  2208. }
  2209. else {
  2210. STORE_PTR((void **)global_data,
  2211. global->initial_value.gc_obj);
  2212. global_data += sizeof(void *);
  2213. break;
  2214. }
  2215. #endif
  2216. bh_assert(0);
  2217. break;
  2218. }
  2219. }
  2220. }
  2221. bh_assert(global_data == global_data_end);
  2222. }
  2223. if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
  2224. goto fail;
  2225. }
  2226. /* Initialize the memory data with data segment section */
  2227. for (i = 0; i < module->data_seg_count; i++) {
  2228. WASMMemoryInstance *memory = NULL;
  2229. uint8 *memory_data = NULL;
  2230. uint64 memory_size = 0;
  2231. WASMDataSeg *data_seg = module->data_segments[i];
  2232. #if WASM_ENABLE_BULK_MEMORY != 0
  2233. if (data_seg->is_passive)
  2234. continue;
  2235. #endif
  2236. if (is_sub_inst)
  2237. /* Ignore setting memory init data if the memory has been
  2238. initialized */
  2239. continue;
  2240. /* has check it in loader */
  2241. memory = module_inst->memories[data_seg->memory_index];
  2242. bh_assert(memory);
  2243. memory_data = memory->memory_data;
  2244. memory_size =
  2245. (uint64)memory->num_bytes_per_page * memory->cur_page_count;
  2246. bh_assert(memory_data || memory_size == 0);
  2247. bh_assert(data_seg->base_offset.init_expr_type
  2248. == INIT_EXPR_TYPE_GET_GLOBAL
  2249. || data_seg->base_offset.init_expr_type
  2250. == (memory->is_memory64 ? INIT_EXPR_TYPE_I64_CONST
  2251. : INIT_EXPR_TYPE_I32_CONST));
  2252. if (data_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  2253. if (!check_global_init_expr(module,
  2254. data_seg->base_offset.u.global_index,
  2255. error_buf, error_buf_size)) {
  2256. goto fail;
  2257. }
  2258. if (!globals
  2259. || globals[data_seg->base_offset.u.global_index].type
  2260. != (memory->is_memory64 ? VALUE_TYPE_I64
  2261. : VALUE_TYPE_I32)) {
  2262. set_error_buf(error_buf, error_buf_size,
  2263. "data segment does not fit");
  2264. goto fail;
  2265. }
  2266. #if WASM_ENABLE_MEMORY64 != 0
  2267. if (memory->is_memory64) {
  2268. base_offset =
  2269. (uint64)globals[data_seg->base_offset.u.global_index]
  2270. .initial_value.i64;
  2271. }
  2272. else
  2273. #endif
  2274. {
  2275. base_offset =
  2276. (uint32)globals[data_seg->base_offset.u.global_index]
  2277. .initial_value.i32;
  2278. }
  2279. }
  2280. else {
  2281. #if WASM_ENABLE_MEMORY64 != 0
  2282. if (memory->is_memory64) {
  2283. base_offset = (uint64)data_seg->base_offset.u.i64;
  2284. }
  2285. else
  2286. #endif
  2287. {
  2288. base_offset = (uint32)data_seg->base_offset.u.i32;
  2289. }
  2290. }
  2291. /* check offset */
  2292. if (base_offset > memory_size) {
  2293. #if WASM_ENABLE_MEMORY64 != 0
  2294. LOG_DEBUG("base_offset(%" PRIu64 ") > memory_size(%" PRIu64 ")",
  2295. base_offset, memory_size);
  2296. #else
  2297. LOG_DEBUG("base_offset(%u) > memory_size(%" PRIu64 ")", base_offset,
  2298. memory_size);
  2299. #endif
  2300. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2301. set_error_buf(error_buf, error_buf_size,
  2302. "out of bounds memory access");
  2303. #else
  2304. set_error_buf(error_buf, error_buf_size,
  2305. "data segment does not fit");
  2306. #endif
  2307. goto fail;
  2308. }
  2309. /* check offset + length(could be zero) */
  2310. length = data_seg->data_length;
  2311. if ((uint64)base_offset + length > memory_size) {
  2312. #if WASM_ENABLE_MEMORY64 != 0
  2313. LOG_DEBUG("base_offset(%" PRIu64
  2314. ") + length(%d) > memory_size(%" PRIu64 ")",
  2315. base_offset, length, memory_size);
  2316. #else
  2317. LOG_DEBUG("base_offset(%u) + length(%d) > memory_size(%" PRIu64 ")",
  2318. base_offset, length, memory_size);
  2319. #endif
  2320. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2321. set_error_buf(error_buf, error_buf_size,
  2322. "out of bounds memory access");
  2323. #else
  2324. set_error_buf(error_buf, error_buf_size,
  2325. "data segment does not fit");
  2326. #endif
  2327. goto fail;
  2328. }
  2329. if (memory_data) {
  2330. bh_memcpy_s(memory_data + base_offset,
  2331. (uint32)(memory_size - base_offset), data_seg->data,
  2332. length);
  2333. }
  2334. }
  2335. #if WASM_ENABLE_GC != 0
  2336. /* Initialize the table data with init expr */
  2337. for (i = 0; i < module->table_count; i++) {
  2338. WASMTable *table = module->tables + i;
  2339. WASMTableInstance *table_inst = module_inst->tables[i];
  2340. table_elem_type_t *table_data;
  2341. uint32 j;
  2342. if (table->init_expr.init_expr_type == 0) {
  2343. /* No table initializer */
  2344. continue;
  2345. }
  2346. table_data = table_inst->elems;
  2347. bh_assert(
  2348. table->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL
  2349. || table->init_expr.init_expr_type == INIT_EXPR_TYPE_FUNCREF_CONST
  2350. || table->init_expr.init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST);
  2351. if (table->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  2352. if (!check_global_init_expr(module, table->init_expr.u.global_index,
  2353. error_buf, error_buf_size)) {
  2354. goto fail;
  2355. }
  2356. table->init_expr.u.gc_obj =
  2357. globals[table->init_expr.u.global_index].initial_value.gc_obj;
  2358. }
  2359. else if (table->init_expr.init_expr_type
  2360. == INIT_EXPR_TYPE_FUNCREF_CONST) {
  2361. uint32 func_idx = table->init_expr.u.ref_index;
  2362. if (func_idx != UINT32_MAX) {
  2363. if (!(table->init_expr.u.gc_obj =
  2364. wasm_create_func_obj(module_inst, func_idx, false,
  2365. error_buf, error_buf_size)))
  2366. goto fail;
  2367. }
  2368. else {
  2369. table->init_expr.u.gc_obj = NULL_REF;
  2370. }
  2371. }
  2372. else if (table->init_expr.init_expr_type
  2373. == INIT_EXPR_TYPE_REFNULL_CONST) {
  2374. table->init_expr.u.gc_obj = NULL_REF;
  2375. }
  2376. LOG_DEBUG("Init table [%d] elements from [%d] to [%d] as: %p", i, 0,
  2377. table_inst->cur_size, (void *)table->init_expr.u.gc_obj);
  2378. for (j = 0; j < table_inst->cur_size; j++) {
  2379. *(table_data + j) = table->init_expr.u.gc_obj;
  2380. }
  2381. }
  2382. #endif /* end of WASM_ENABLE_GC != 0 */
  2383. /* Initialize the table data with table segment section */
  2384. for (i = 0; module_inst->table_count > 0 && i < module->table_seg_count;
  2385. i++) {
  2386. WASMTableSeg *table_seg = module->table_segments + i;
  2387. /* has check it in loader */
  2388. WASMTableInstance *table = module_inst->tables[table_seg->table_index];
  2389. table_elem_type_t *table_data;
  2390. uint32 j;
  2391. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2392. uint8 tbl_elem_type;
  2393. uint32 tbl_init_size, tbl_max_size;
  2394. #endif
  2395. #if WASM_ENABLE_GC != 0
  2396. WASMRefType *tbl_elem_ref_type;
  2397. #endif
  2398. bh_assert(table);
  2399. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2400. (void)wasm_runtime_get_table_inst_elem_type(
  2401. (WASMModuleInstanceCommon *)module_inst, table_seg->table_index,
  2402. &tbl_elem_type,
  2403. #if WASM_ENABLE_GC != 0
  2404. &tbl_elem_ref_type,
  2405. #endif
  2406. &tbl_init_size, &tbl_max_size);
  2407. #if WASM_ENABLE_GC == 0
  2408. if (tbl_elem_type != VALUE_TYPE_FUNCREF
  2409. && tbl_elem_type != VALUE_TYPE_EXTERNREF) {
  2410. set_error_buf(error_buf, error_buf_size,
  2411. "type mismatch: elements segment does not fit");
  2412. goto fail;
  2413. }
  2414. #elif WASM_ENABLE_GC != 0
  2415. if (!wasm_elem_is_declarative(table_seg->mode)
  2416. && !wasm_reftype_is_subtype_of(
  2417. table_seg->elem_type, table_seg->elem_ref_type,
  2418. table->elem_type, table->elem_ref_type.elem_ref_type,
  2419. module->types, module->type_count)) {
  2420. set_error_buf(error_buf, error_buf_size,
  2421. "type mismatch: elements segment does not fit");
  2422. goto fail;
  2423. }
  2424. #endif
  2425. (void)tbl_init_size;
  2426. (void)tbl_max_size;
  2427. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  2428. table_data = table->elems;
  2429. #if WASM_ENABLE_MULTI_MODULE != 0
  2430. if (table_seg->table_index < module->import_table_count
  2431. && module_inst->e->table_insts_linked[table_seg->table_index]) {
  2432. table_data =
  2433. module_inst->e->table_insts_linked[table_seg->table_index]
  2434. ->elems;
  2435. }
  2436. #endif
  2437. bh_assert(table_data);
  2438. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2439. if (!wasm_elem_is_active(table_seg->mode))
  2440. continue;
  2441. #endif
  2442. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2443. bh_assert(table_seg->base_offset.init_expr_type
  2444. == INIT_EXPR_TYPE_I32_CONST
  2445. || table_seg->base_offset.init_expr_type
  2446. == INIT_EXPR_TYPE_GET_GLOBAL
  2447. || table_seg->base_offset.init_expr_type
  2448. == INIT_EXPR_TYPE_FUNCREF_CONST
  2449. || table_seg->base_offset.init_expr_type
  2450. == INIT_EXPR_TYPE_REFNULL_CONST);
  2451. #else
  2452. bh_assert(table_seg->base_offset.init_expr_type
  2453. == INIT_EXPR_TYPE_I32_CONST
  2454. || table_seg->base_offset.init_expr_type
  2455. == INIT_EXPR_TYPE_GET_GLOBAL);
  2456. #endif
  2457. /* init vec(funcidx) or vec(expr) */
  2458. if (table_seg->base_offset.init_expr_type
  2459. == INIT_EXPR_TYPE_GET_GLOBAL) {
  2460. if (!check_global_init_expr(module,
  2461. table_seg->base_offset.u.global_index,
  2462. error_buf, error_buf_size)) {
  2463. goto fail;
  2464. }
  2465. if (!globals
  2466. || globals[table_seg->base_offset.u.global_index].type
  2467. != VALUE_TYPE_I32) {
  2468. set_error_buf(error_buf, error_buf_size,
  2469. "type mismatch: elements segment does not fit");
  2470. goto fail;
  2471. }
  2472. table_seg->base_offset.u.i32 =
  2473. globals[table_seg->base_offset.u.global_index]
  2474. .initial_value.i32;
  2475. }
  2476. /* check offset since length might negative */
  2477. if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
  2478. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  2479. table_seg->base_offset.u.i32, table->cur_size);
  2480. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2481. set_error_buf(error_buf, error_buf_size,
  2482. "out of bounds table access");
  2483. #else
  2484. set_error_buf(error_buf, error_buf_size,
  2485. "type mismatch: elements segment does not fit");
  2486. #endif
  2487. goto fail;
  2488. }
  2489. /* check offset + length(could be zero) */
  2490. length = table_seg->value_count;
  2491. if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
  2492. LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
  2493. table_seg->base_offset.u.i32, length, table->cur_size);
  2494. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2495. set_error_buf(error_buf, error_buf_size,
  2496. "out of bounds table access");
  2497. #else
  2498. set_error_buf(error_buf, error_buf_size,
  2499. "type mismatch: elements segment does not fit");
  2500. #endif
  2501. goto fail;
  2502. }
  2503. for (j = 0; j < length; j++) {
  2504. InitializerExpression *init_expr = &table_seg->init_values[j];
  2505. uint8 flag = init_expr->init_expr_type;
  2506. void *ref = NULL;
  2507. /* const and get global init values should be resolved during
  2508. * loading */
  2509. bh_assert((flag == INIT_EXPR_TYPE_GET_GLOBAL)
  2510. || (flag == INIT_EXPR_TYPE_REFNULL_CONST)
  2511. || ((flag >= INIT_EXPR_TYPE_FUNCREF_CONST)
  2512. && (flag <= INIT_EXPR_TYPE_EXTERN_CONVERT_ANY)));
  2513. switch (flag) {
  2514. case INIT_EXPR_TYPE_REFNULL_CONST:
  2515. ref = NULL;
  2516. break;
  2517. case INIT_EXPR_TYPE_FUNCREF_CONST:
  2518. {
  2519. #if WASM_ENABLE_GC == 0
  2520. ref = (void *)(uintptr_t)init_expr->u.ref_index;
  2521. #else
  2522. WASMFuncObjectRef func_obj;
  2523. uint32 func_idx = init_expr->u.ref_index;
  2524. /* UINT32_MAX indicates that it is a null reference */
  2525. if (func_idx != UINT32_MAX) {
  2526. if (!(func_obj = wasm_create_func_obj(
  2527. module_inst, func_idx, false, error_buf,
  2528. error_buf_size))) {
  2529. goto fail;
  2530. }
  2531. ref = func_obj;
  2532. }
  2533. else {
  2534. ref = NULL_REF;
  2535. }
  2536. #endif /* end of WASM_ENABLE_GC != 0 */
  2537. break;
  2538. }
  2539. #if WASM_ENABLE_GC != 0
  2540. case INIT_EXPR_TYPE_GET_GLOBAL:
  2541. {
  2542. if (!check_global_init_expr(module,
  2543. init_expr->u.global_index,
  2544. error_buf, error_buf_size)) {
  2545. goto fail;
  2546. }
  2547. ref =
  2548. globals[init_expr->u.global_index].initial_value.gc_obj;
  2549. break;
  2550. }
  2551. case INIT_EXPR_TYPE_STRUCT_NEW:
  2552. case INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT:
  2553. {
  2554. WASMRttType *rtt_type;
  2555. WASMStructObjectRef struct_obj;
  2556. WASMStructType *struct_type;
  2557. WASMStructNewInitValues *init_values = NULL;
  2558. uint32 type_idx;
  2559. if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
  2560. init_values =
  2561. (WASMStructNewInitValues *)init_expr->u.data;
  2562. type_idx = init_values->type_idx;
  2563. }
  2564. else {
  2565. type_idx = init_expr->u.type_index;
  2566. }
  2567. struct_type = (WASMStructType *)module->types[type_idx];
  2568. if (!(rtt_type = wasm_rtt_type_new(
  2569. (WASMType *)struct_type, type_idx,
  2570. module->rtt_types, module->type_count,
  2571. &module->rtt_type_lock))) {
  2572. set_error_buf(error_buf, error_buf_size,
  2573. "create rtt object failed");
  2574. goto fail;
  2575. }
  2576. if (!(struct_obj = wasm_struct_obj_new_internal(
  2577. module_inst->e->common.gc_heap_handle,
  2578. rtt_type))) {
  2579. set_error_buf(error_buf, error_buf_size,
  2580. "create struct object failed");
  2581. goto fail;
  2582. }
  2583. if (flag == INIT_EXPR_TYPE_STRUCT_NEW) {
  2584. uint32 field_idx;
  2585. bh_assert(init_values->count
  2586. == struct_type->field_count);
  2587. for (field_idx = 0; field_idx < init_values->count;
  2588. field_idx++) {
  2589. wasm_struct_obj_set_field(
  2590. struct_obj, field_idx,
  2591. &init_values->fields[field_idx]);
  2592. }
  2593. }
  2594. ref = struct_obj;
  2595. break;
  2596. }
  2597. case INIT_EXPR_TYPE_ARRAY_NEW:
  2598. case INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT:
  2599. case INIT_EXPR_TYPE_ARRAY_NEW_FIXED:
  2600. {
  2601. WASMRttType *rtt_type;
  2602. WASMArrayObjectRef array_obj;
  2603. WASMArrayType *array_type;
  2604. WASMArrayNewInitValues *init_values = NULL;
  2605. WASMValue *arr_init_val = NULL, empty_val = { 0 };
  2606. uint32 type_idx, len;
  2607. if (flag == INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT) {
  2608. type_idx = init_expr->u.array_new_default.type_index;
  2609. len = init_expr->u.array_new_default.length;
  2610. arr_init_val = &empty_val;
  2611. }
  2612. else {
  2613. init_values =
  2614. (WASMArrayNewInitValues *)init_expr->u.data;
  2615. type_idx = init_values->type_idx;
  2616. len = init_values->length;
  2617. if (flag == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  2618. arr_init_val = init_values->elem_data;
  2619. }
  2620. }
  2621. array_type = (WASMArrayType *)module->types[type_idx];
  2622. if (!(rtt_type = wasm_rtt_type_new(
  2623. (WASMType *)array_type, type_idx,
  2624. module->rtt_types, module->type_count,
  2625. &module->rtt_type_lock))) {
  2626. set_error_buf(error_buf, error_buf_size,
  2627. "create rtt object failed");
  2628. goto fail;
  2629. }
  2630. if (!(array_obj = wasm_array_obj_new_internal(
  2631. module_inst->e->common.gc_heap_handle, rtt_type,
  2632. len, arr_init_val))) {
  2633. set_error_buf(error_buf, error_buf_size,
  2634. "create array object failed");
  2635. goto fail;
  2636. }
  2637. if (flag == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  2638. uint32 elem_idx;
  2639. bh_assert(init_values);
  2640. for (elem_idx = 0; elem_idx < len; elem_idx++) {
  2641. wasm_array_obj_set_elem(
  2642. array_obj, elem_idx,
  2643. &init_values->elem_data[elem_idx]);
  2644. }
  2645. }
  2646. ref = array_obj;
  2647. break;
  2648. }
  2649. case INIT_EXPR_TYPE_I31_NEW:
  2650. {
  2651. ref = (wasm_obj_t)wasm_i31_obj_new(init_expr->u.i32);
  2652. break;
  2653. }
  2654. #endif /* end of WASM_ENABLE_GC != 0 */
  2655. }
  2656. *(table_data + table_seg->base_offset.u.i32 + j) =
  2657. (table_elem_type_t)ref;
  2658. }
  2659. }
  2660. /* Initialize the thread related data */
  2661. if (stack_size == 0)
  2662. stack_size = DEFAULT_WASM_STACK_SIZE;
  2663. #if WASM_ENABLE_SPEC_TEST != 0
  2664. #if WASM_ENABLE_TAIL_CALL == 0
  2665. if (stack_size < 128 * 1024)
  2666. stack_size = 128 * 1024;
  2667. #else
  2668. /* Some tail-call cases require large operand stack */
  2669. if (stack_size < 10 * 1024 * 1024)
  2670. stack_size = 10 * 1024 * 1024;
  2671. #endif
  2672. #endif
  2673. module_inst->default_wasm_stack_size = stack_size;
  2674. if (module->malloc_function != (uint32)-1) {
  2675. module_inst->e->malloc_function =
  2676. &module_inst->e->functions[module->malloc_function];
  2677. }
  2678. if (module->free_function != (uint32)-1) {
  2679. module_inst->e->free_function =
  2680. &module_inst->e->functions[module->free_function];
  2681. }
  2682. if (module->retain_function != (uint32)-1) {
  2683. module_inst->e->retain_function =
  2684. &module_inst->e->functions[module->retain_function];
  2685. }
  2686. #if WASM_ENABLE_LIBC_WASI != 0
  2687. /* The sub-instance will get the wasi_ctx from main-instance */
  2688. if (!is_sub_inst) {
  2689. if (!wasm_runtime_init_wasi(
  2690. (WASMModuleInstanceCommon *)module_inst,
  2691. module->wasi_args.dir_list, module->wasi_args.dir_count,
  2692. module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
  2693. module->wasi_args.env, module->wasi_args.env_count,
  2694. module->wasi_args.addr_pool, module->wasi_args.addr_count,
  2695. module->wasi_args.ns_lookup_pool,
  2696. module->wasi_args.ns_lookup_count, module->wasi_args.argv,
  2697. module->wasi_args.argc, module->wasi_args.stdio[0],
  2698. module->wasi_args.stdio[1], module->wasi_args.stdio[2],
  2699. error_buf, error_buf_size)) {
  2700. goto fail;
  2701. }
  2702. }
  2703. #endif
  2704. #if WASM_ENABLE_DEBUG_INTERP != 0
  2705. if (!is_sub_inst) {
  2706. /* Add module instance into module's instance list */
  2707. os_mutex_lock(&module->instance_list_lock);
  2708. if (module->instance_list) {
  2709. LOG_WARNING(
  2710. "warning: multiple instances referencing to the same module "
  2711. "may cause unexpected behaviour during debugging");
  2712. }
  2713. module_inst->e->next = module->instance_list;
  2714. module->instance_list = module_inst;
  2715. os_mutex_unlock(&module->instance_list_lock);
  2716. }
  2717. #endif
  2718. /* Set running mode before executing wasm functions */
  2719. if (!set_running_mode(module_inst, wasm_runtime_get_default_running_mode(),
  2720. true)) {
  2721. set_error_buf(error_buf, error_buf_size,
  2722. "set instance running mode failed");
  2723. goto fail;
  2724. }
  2725. if (module->start_function != (uint32)-1) {
  2726. /* TODO: fix start function can be import function issue */
  2727. if (module->start_function >= module->import_function_count)
  2728. module_inst->e->start_function =
  2729. &module_inst->e->functions[module->start_function];
  2730. }
  2731. if (!execute_post_instantiate_functions(module_inst, is_sub_inst,
  2732. exec_env_main)) {
  2733. set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
  2734. goto fail;
  2735. }
  2736. #if WASM_ENABLE_MEMORY_TRACING != 0
  2737. wasm_runtime_dump_module_inst_mem_consumption(
  2738. (WASMModuleInstanceCommon *)module_inst);
  2739. #endif
  2740. (void)global_data_end;
  2741. return module_inst;
  2742. fail:
  2743. wasm_deinstantiate(module_inst, false);
  2744. return NULL;
  2745. }
  2746. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2747. static void
  2748. destroy_c_api_frames(Vector *frames)
  2749. {
  2750. WASMCApiFrame frame = { 0 };
  2751. uint32 i, total_frames, ret;
  2752. total_frames = (uint32)bh_vector_size(frames);
  2753. for (i = 0; i < total_frames; i++) {
  2754. ret = bh_vector_get(frames, i, &frame);
  2755. bh_assert(ret);
  2756. if (frame.lp)
  2757. wasm_runtime_free(frame.lp);
  2758. }
  2759. ret = bh_vector_destroy(frames);
  2760. bh_assert(ret);
  2761. (void)ret;
  2762. }
  2763. #endif
  2764. void
  2765. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
  2766. {
  2767. if (!module_inst)
  2768. return;
  2769. if (module_inst->exec_env_singleton) {
  2770. /* wasm_exec_env_destroy will call
  2771. wasm_cluster_wait_for_all_except_self to wait for other
  2772. threads, so as to destroy their exec_envs and module
  2773. instances first, and avoid accessing the shared resources
  2774. of current module instance after it is deinstantiated. */
  2775. wasm_exec_env_destroy(module_inst->exec_env_singleton);
  2776. }
  2777. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  2778. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2779. && WASM_ENABLE_LAZY_JIT != 0)
  2780. /* Remove instance from module's instance list before freeing
  2781. func_ptrs and fast_jit_func_ptrs of the instance, to avoid
  2782. accessing the freed memory in the jit backend compilation
  2783. threads */
  2784. {
  2785. WASMModule *module = module_inst->module;
  2786. WASMModuleInstance *instance_prev = NULL, *instance;
  2787. os_mutex_lock(&module->instance_list_lock);
  2788. instance = module->instance_list;
  2789. while (instance) {
  2790. if (instance == module_inst) {
  2791. if (!instance_prev)
  2792. module->instance_list = instance->e->next;
  2793. else
  2794. instance_prev->e->next = instance->e->next;
  2795. break;
  2796. }
  2797. instance_prev = instance;
  2798. instance = instance->e->next;
  2799. }
  2800. os_mutex_unlock(&module->instance_list_lock);
  2801. }
  2802. #endif
  2803. #if WASM_ENABLE_JIT != 0
  2804. if (module_inst->func_ptrs)
  2805. wasm_runtime_free(module_inst->func_ptrs);
  2806. #endif
  2807. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2808. && WASM_ENABLE_LAZY_JIT != 0
  2809. if (module_inst->fast_jit_func_ptrs
  2810. && module_inst->fast_jit_func_ptrs
  2811. != module_inst->module->fast_jit_func_ptrs)
  2812. wasm_runtime_free(module_inst->fast_jit_func_ptrs);
  2813. #endif
  2814. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  2815. if (module_inst->func_type_indexes)
  2816. wasm_runtime_free(module_inst->func_type_indexes);
  2817. #endif
  2818. #if WASM_ENABLE_MULTI_MODULE != 0
  2819. wasm_runtime_sub_module_deinstantiate(
  2820. (WASMModuleInstanceCommon *)module_inst);
  2821. #endif
  2822. if (module_inst->memory_count > 0)
  2823. memories_deinstantiate(module_inst, module_inst->memories,
  2824. module_inst->memory_count);
  2825. if (module_inst->import_func_ptrs) {
  2826. wasm_runtime_free(module_inst->import_func_ptrs);
  2827. }
  2828. tables_deinstantiate(module_inst);
  2829. functions_deinstantiate(module_inst->e->functions);
  2830. #if WASM_ENABLE_TAGS != 0
  2831. tags_deinstantiate(module_inst->e->tags, module_inst->e->import_tag_ptrs);
  2832. #endif
  2833. globals_deinstantiate(module_inst->e->globals);
  2834. export_functions_deinstantiate(module_inst->export_functions);
  2835. #if WASM_ENABLE_TAGS != 0
  2836. export_tags_deinstantiate(module_inst->e->export_tags);
  2837. #endif
  2838. #if WASM_ENABLE_MULTI_MODULE != 0
  2839. export_globals_deinstantiate(module_inst->export_globals);
  2840. #endif
  2841. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2842. wasm_externref_cleanup((WASMModuleInstanceCommon *)module_inst);
  2843. #endif
  2844. #if WASM_ENABLE_GC != 0
  2845. if (!is_sub_inst) {
  2846. if (module_inst->e->common.gc_heap_handle)
  2847. mem_allocator_destroy(module_inst->e->common.gc_heap_handle);
  2848. if (module_inst->e->common.gc_heap_pool)
  2849. wasm_runtime_free(module_inst->e->common.gc_heap_pool);
  2850. }
  2851. #endif
  2852. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2853. if (module_inst->frames) {
  2854. destroy_c_api_frames(module_inst->frames);
  2855. wasm_runtime_free(module_inst->frames);
  2856. module_inst->frames = NULL;
  2857. }
  2858. #endif
  2859. if (module_inst->c_api_func_imports)
  2860. wasm_runtime_free(module_inst->c_api_func_imports);
  2861. if (!is_sub_inst) {
  2862. wasm_native_call_context_dtors((WASMModuleInstanceCommon *)module_inst);
  2863. }
  2864. #if WASM_ENABLE_BULK_MEMORY != 0
  2865. bh_bitmap_delete(module_inst->e->common.data_dropped);
  2866. #endif
  2867. #if WASM_ENABLE_REF_TYPES != 0
  2868. bh_bitmap_delete(module_inst->e->common.elem_dropped);
  2869. #endif
  2870. wasm_runtime_free(module_inst);
  2871. }
  2872. WASMFunctionInstance *
  2873. wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name)
  2874. {
  2875. uint32 i;
  2876. for (i = 0; i < module_inst->export_func_count; i++)
  2877. if (!strcmp(module_inst->export_functions[i].name, name))
  2878. return module_inst->export_functions[i].function;
  2879. return NULL;
  2880. }
  2881. #if WASM_ENABLE_MULTI_MODULE != 0
  2882. WASMGlobalInstance *
  2883. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name)
  2884. {
  2885. uint32 i;
  2886. for (i = 0; i < module_inst->export_global_count; i++)
  2887. if (!strcmp(module_inst->export_globals[i].name, name))
  2888. return module_inst->export_globals[i].global;
  2889. return NULL;
  2890. }
  2891. WASMMemoryInstance *
  2892. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name)
  2893. {
  2894. /**
  2895. * using a strong assumption that one module instance only has
  2896. * one memory instance
  2897. */
  2898. (void)module_inst->export_memories;
  2899. return module_inst->memories[0];
  2900. }
  2901. WASMTableInstance *
  2902. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
  2903. {
  2904. /**
  2905. * using a strong assumption that one module instance only has
  2906. * one table instance
  2907. */
  2908. (void)module_inst->export_tables;
  2909. return module_inst->tables[0];
  2910. }
  2911. #if WASM_ENABLE_TAGS != 0
  2912. WASMTagInstance *
  2913. wasm_lookup_tag(const WASMModuleInstance *module_inst, const char *name,
  2914. const char *signature)
  2915. {
  2916. uint32 i;
  2917. for (i = 0; i < module_inst->e->export_tag_count; i++)
  2918. if (!strcmp(module_inst->e->export_tags[i].name, name))
  2919. return module_inst->e->export_tags[i].tag;
  2920. (void)signature;
  2921. return NULL;
  2922. }
  2923. #endif
  2924. #endif
  2925. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2926. static void
  2927. call_wasm_with_hw_bound_check(WASMModuleInstance *module_inst,
  2928. WASMExecEnv *exec_env,
  2929. WASMFunctionInstance *function, unsigned argc,
  2930. uint32 argv[])
  2931. {
  2932. WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
  2933. WASMJmpBuf jmpbuf_node = { 0 }, *jmpbuf_node_pop;
  2934. WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  2935. uint8 *prev_top = exec_env->wasm_stack.top;
  2936. #ifdef BH_PLATFORM_WINDOWS
  2937. int result;
  2938. bool has_exception;
  2939. char exception[EXCEPTION_BUF_LEN];
  2940. #endif
  2941. bool ret = true;
  2942. /* Check native stack overflow firstly to ensure we have enough
  2943. native stack to run the following codes before actually calling
  2944. the aot function in invokeNative function. */
  2945. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  2946. return;
  2947. }
  2948. if (!exec_env_tls) {
  2949. if (!os_thread_signal_inited()) {
  2950. wasm_set_exception(module_inst, "thread signal env not inited");
  2951. return;
  2952. }
  2953. /* Set thread handle and stack boundary if they haven't been set */
  2954. wasm_exec_env_set_thread_info(exec_env);
  2955. wasm_runtime_set_exec_env_tls(exec_env);
  2956. }
  2957. else {
  2958. if (exec_env_tls != exec_env) {
  2959. wasm_set_exception(module_inst, "invalid exec env");
  2960. return;
  2961. }
  2962. }
  2963. wasm_exec_env_push_jmpbuf(exec_env, &jmpbuf_node);
  2964. if (os_setjmp(jmpbuf_node.jmpbuf) == 0) {
  2965. #ifndef BH_PLATFORM_WINDOWS
  2966. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  2967. #else
  2968. __try {
  2969. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  2970. } __except (wasm_copy_exception(module_inst, NULL)
  2971. ? EXCEPTION_EXECUTE_HANDLER
  2972. : EXCEPTION_CONTINUE_SEARCH) {
  2973. /* Exception was thrown in wasm_exception_handler */
  2974. ret = false;
  2975. }
  2976. has_exception = wasm_copy_exception(module_inst, exception);
  2977. if (has_exception && strstr(exception, "native stack overflow")) {
  2978. /* After a stack overflow, the stack was left
  2979. in a damaged state, let the CRT repair it */
  2980. result = _resetstkoflw();
  2981. bh_assert(result != 0);
  2982. }
  2983. #endif
  2984. }
  2985. else {
  2986. /* Exception has been set in signal handler before calling longjmp */
  2987. ret = false;
  2988. }
  2989. /* Note: can't check wasm_get_exception(module_inst) here, there may be
  2990. * exception which is not caught by hardware (e.g. uninitialized elements),
  2991. * then the stack-frame is already freed inside wasm_interp_call_wasm */
  2992. if (!ret) {
  2993. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2994. if (wasm_interp_create_call_stack(exec_env)) {
  2995. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  2996. }
  2997. #endif
  2998. /* Restore operand frames */
  2999. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  3000. exec_env->wasm_stack.top = prev_top;
  3001. }
  3002. jmpbuf_node_pop = wasm_exec_env_pop_jmpbuf(exec_env);
  3003. bh_assert(&jmpbuf_node == jmpbuf_node_pop);
  3004. if (!exec_env->jmpbuf_stack_top) {
  3005. wasm_runtime_set_exec_env_tls(NULL);
  3006. }
  3007. if (!ret) {
  3008. os_sigreturn();
  3009. os_signal_unmask();
  3010. }
  3011. (void)jmpbuf_node_pop;
  3012. }
  3013. #define interp_call_wasm call_wasm_with_hw_bound_check
  3014. #else
  3015. #define interp_call_wasm wasm_interp_call_wasm
  3016. #endif
  3017. bool
  3018. wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
  3019. unsigned argc, uint32 argv[])
  3020. {
  3021. WASMModuleInstance *module_inst =
  3022. (WASMModuleInstance *)exec_env->module_inst;
  3023. #ifndef OS_ENABLE_HW_BOUND_CHECK
  3024. /* Set thread handle and stack boundary */
  3025. wasm_exec_env_set_thread_info(exec_env);
  3026. #else
  3027. /* Set thread info in call_wasm_with_hw_bound_check when
  3028. hw bound check is enabled */
  3029. #endif
  3030. /* Set exec env, so it can be later retrieved from instance */
  3031. module_inst->cur_exec_env = exec_env;
  3032. interp_call_wasm(module_inst, exec_env, function, argc, argv);
  3033. return !wasm_copy_exception(module_inst, NULL);
  3034. }
  3035. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0
  3036. /* look for the function name */
  3037. static char *
  3038. get_func_name_from_index(const WASMModuleInstance *inst, uint32 func_index)
  3039. {
  3040. char *func_name = NULL;
  3041. WASMFunctionInstance *func_inst = inst->e->functions + func_index;
  3042. if (func_inst->is_import_func) {
  3043. func_name = func_inst->u.func_import->field_name;
  3044. }
  3045. else {
  3046. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  3047. func_name = func_inst->u.func->field_name;
  3048. #endif
  3049. /* if custom name section is not generated,
  3050. search symbols from export table */
  3051. if (!func_name) {
  3052. unsigned j;
  3053. for (j = 0; j < inst->export_func_count; j++) {
  3054. WASMExportFuncInstance *export_func =
  3055. inst->export_functions + j;
  3056. if (export_func->function == func_inst) {
  3057. func_name = export_func->name;
  3058. break;
  3059. }
  3060. }
  3061. }
  3062. }
  3063. return func_name;
  3064. }
  3065. #endif /*WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0*/
  3066. #if WASM_ENABLE_PERF_PROFILING != 0
  3067. void
  3068. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
  3069. {
  3070. WASMFunctionInstance *func_inst;
  3071. char *func_name;
  3072. uint32 i;
  3073. os_printf("Performance profiler data:\n");
  3074. for (i = 0; i < module_inst->e->function_count; i++) {
  3075. func_inst = module_inst->e->functions + i;
  3076. if (func_inst->total_exec_cnt == 0)
  3077. continue;
  3078. func_name = get_func_name_from_index(module_inst, i);
  3079. if (func_name)
  3080. os_printf(
  3081. " func %s, execution time: %.3f ms, execution count: %" PRIu32
  3082. " times, children execution time: %.3f ms\n",
  3083. func_name, func_inst->total_exec_time / 1000.0f,
  3084. func_inst->total_exec_cnt,
  3085. func_inst->children_exec_time / 1000.0f);
  3086. else
  3087. os_printf(" func %" PRIu32
  3088. ", execution time: %.3f ms, execution count: %" PRIu32
  3089. " times, children execution time: %.3f ms\n",
  3090. i, func_inst->total_exec_time / 1000.0f,
  3091. func_inst->total_exec_cnt,
  3092. func_inst->children_exec_time / 1000.0f);
  3093. }
  3094. }
  3095. double
  3096. wasm_summarize_wasm_execute_time(const WASMModuleInstance *inst)
  3097. {
  3098. double ret = 0;
  3099. unsigned i;
  3100. for (i = 0; i < inst->e->function_count; i++) {
  3101. WASMFunctionInstance *func = inst->e->functions + i;
  3102. ret += (func->total_exec_time - func->children_exec_time) / 1000.0f;
  3103. }
  3104. return ret;
  3105. }
  3106. double
  3107. wasm_get_wasm_func_exec_time(const WASMModuleInstance *inst,
  3108. const char *func_name)
  3109. {
  3110. unsigned i;
  3111. for (i = 0; i < inst->e->function_count; i++) {
  3112. char *name_in_wasm = get_func_name_from_index(inst, i);
  3113. if (name_in_wasm && strcmp(name_in_wasm, func_name) == 0) {
  3114. WASMFunctionInstance *func = inst->e->functions + i;
  3115. return (func->total_exec_time - func->children_exec_time) / 1000.0f;
  3116. }
  3117. }
  3118. return -1.0;
  3119. }
  3120. #endif /*WASM_ENABLE_PERF_PROFILING != 0*/
  3121. uint64
  3122. wasm_module_malloc_internal(WASMModuleInstance *module_inst,
  3123. WASMExecEnv *exec_env, uint64 size,
  3124. void **p_native_addr)
  3125. {
  3126. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  3127. uint8 *addr = NULL;
  3128. uint64 offset = 0;
  3129. /* TODO: Memory64 size check based on memory idx type */
  3130. bh_assert(size <= UINT32_MAX);
  3131. if (!memory) {
  3132. wasm_set_exception(module_inst, "uninitialized memory");
  3133. return 0;
  3134. }
  3135. if (memory->heap_handle) {
  3136. addr = mem_allocator_malloc(memory->heap_handle, (uint32)size);
  3137. }
  3138. else if (module_inst->e->malloc_function && module_inst->e->free_function) {
  3139. if (!execute_malloc_function(
  3140. module_inst, exec_env, module_inst->e->malloc_function,
  3141. module_inst->e->retain_function, size, &offset)) {
  3142. return 0;
  3143. }
  3144. /* If we use app's malloc function,
  3145. the default memory may be changed while memory growing */
  3146. memory = wasm_get_default_memory(module_inst);
  3147. addr = offset ? memory->memory_data + offset : NULL;
  3148. }
  3149. if (!addr) {
  3150. if (memory->heap_handle
  3151. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  3152. wasm_runtime_show_app_heap_corrupted_prompt();
  3153. wasm_set_exception(module_inst, "app heap corrupted");
  3154. }
  3155. else {
  3156. LOG_WARNING("warning: allocate %" PRIu64 " bytes memory failed",
  3157. size);
  3158. }
  3159. return 0;
  3160. }
  3161. if (p_native_addr)
  3162. *p_native_addr = addr;
  3163. return (uint64)(addr - memory->memory_data);
  3164. }
  3165. uint64
  3166. wasm_module_realloc_internal(WASMModuleInstance *module_inst,
  3167. WASMExecEnv *exec_env, uint64 ptr, uint64 size,
  3168. void **p_native_addr)
  3169. {
  3170. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  3171. uint8 *addr = NULL;
  3172. /* TODO: Memory64 ptr and size check based on memory idx type */
  3173. bh_assert(ptr <= UINT32_MAX);
  3174. bh_assert(size <= UINT32_MAX);
  3175. if (!memory) {
  3176. wasm_set_exception(module_inst, "uninitialized memory");
  3177. return 0;
  3178. }
  3179. if (memory->heap_handle) {
  3180. addr = mem_allocator_realloc(
  3181. memory->heap_handle,
  3182. (uint32)ptr ? memory->memory_data + (uint32)ptr : NULL,
  3183. (uint32)size);
  3184. }
  3185. /* Only support realloc in WAMR's app heap */
  3186. (void)exec_env;
  3187. if (!addr) {
  3188. if (memory->heap_handle
  3189. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  3190. wasm_set_exception(module_inst, "app heap corrupted");
  3191. }
  3192. else {
  3193. wasm_set_exception(module_inst, "out of memory");
  3194. }
  3195. return 0;
  3196. }
  3197. if (p_native_addr)
  3198. *p_native_addr = addr;
  3199. return (uint64)(addr - memory->memory_data);
  3200. }
  3201. void
  3202. wasm_module_free_internal(WASMModuleInstance *module_inst,
  3203. WASMExecEnv *exec_env, uint64 ptr)
  3204. {
  3205. WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
  3206. /* TODO: Memory64 ptr and size check based on memory idx type */
  3207. bh_assert(ptr <= UINT32_MAX);
  3208. if (!memory) {
  3209. return;
  3210. }
  3211. if (ptr) {
  3212. uint8 *addr = memory->memory_data + (uint32)ptr;
  3213. uint8 *memory_data_end;
  3214. /* memory->memory_data_end may be changed in memory grow */
  3215. SHARED_MEMORY_LOCK(memory);
  3216. memory_data_end = memory->memory_data_end;
  3217. SHARED_MEMORY_UNLOCK(memory);
  3218. if (memory->heap_handle && memory->heap_data <= addr
  3219. && addr < memory->heap_data_end) {
  3220. mem_allocator_free(memory->heap_handle, addr);
  3221. }
  3222. else if (module_inst->e->malloc_function
  3223. && module_inst->e->free_function && memory->memory_data <= addr
  3224. && addr < memory_data_end) {
  3225. execute_free_function(module_inst, exec_env,
  3226. module_inst->e->free_function, ptr);
  3227. }
  3228. }
  3229. }
  3230. uint64
  3231. wasm_module_malloc(WASMModuleInstance *module_inst, uint64 size,
  3232. void **p_native_addr)
  3233. {
  3234. return wasm_module_malloc_internal(module_inst, NULL, size, p_native_addr);
  3235. }
  3236. uint64
  3237. wasm_module_realloc(WASMModuleInstance *module_inst, uint64 ptr, uint64 size,
  3238. void **p_native_addr)
  3239. {
  3240. return wasm_module_realloc_internal(module_inst, NULL, ptr, size,
  3241. p_native_addr);
  3242. }
  3243. void
  3244. wasm_module_free(WASMModuleInstance *module_inst, uint64 ptr)
  3245. {
  3246. wasm_module_free_internal(module_inst, NULL, ptr);
  3247. }
  3248. uint64
  3249. wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src,
  3250. uint64 size)
  3251. {
  3252. char *buffer;
  3253. uint64 buffer_offset;
  3254. /* TODO: Memory64 size check based on memory idx type */
  3255. bh_assert(size <= UINT32_MAX);
  3256. buffer_offset = wasm_module_malloc(module_inst, size, (void **)&buffer);
  3257. if (buffer_offset != 0) {
  3258. buffer = wasm_runtime_addr_app_to_native(
  3259. (WASMModuleInstanceCommon *)module_inst, buffer_offset);
  3260. bh_memcpy_s(buffer, (uint32)size, src, (uint32)size);
  3261. }
  3262. return buffer_offset;
  3263. }
  3264. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3265. bool
  3266. wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx,
  3267. uint32 inc_size, table_elem_type_t init_val)
  3268. {
  3269. uint32 total_size, i;
  3270. table_elem_type_t *new_table_data_start;
  3271. WASMTableInstance *table_inst;
  3272. if (!inc_size) {
  3273. return true;
  3274. }
  3275. bh_assert(table_idx < module_inst->table_count);
  3276. table_inst = wasm_get_table_inst(module_inst, table_idx);
  3277. if (!table_inst) {
  3278. return false;
  3279. }
  3280. if (inc_size > UINT32_MAX - table_inst->cur_size) {
  3281. return false;
  3282. }
  3283. total_size = table_inst->cur_size + inc_size;
  3284. if (total_size > table_inst->max_size) {
  3285. return false;
  3286. }
  3287. /* fill in */
  3288. new_table_data_start = table_inst->elems + table_inst->cur_size;
  3289. for (i = 0; i < inc_size; ++i) {
  3290. new_table_data_start[i] = init_val;
  3291. }
  3292. table_inst->cur_size = total_size;
  3293. return true;
  3294. }
  3295. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3296. static bool
  3297. call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 tbl_elem_idx,
  3298. uint32 argc, uint32 argv[], bool check_type_idx, uint32 type_idx)
  3299. {
  3300. WASMModuleInstance *module_inst = NULL;
  3301. WASMTableInstance *table_inst = NULL;
  3302. table_elem_type_t tbl_elem_val = NULL_REF;
  3303. uint32 func_idx = 0;
  3304. WASMFunctionInstance *func_inst = NULL;
  3305. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  3306. bh_assert(module_inst);
  3307. table_inst = module_inst->tables[tbl_idx];
  3308. if (!table_inst) {
  3309. wasm_set_exception(module_inst, "unknown table");
  3310. goto got_exception;
  3311. }
  3312. if (tbl_elem_idx >= table_inst->cur_size) {
  3313. wasm_set_exception(module_inst, "undefined element");
  3314. goto got_exception;
  3315. }
  3316. tbl_elem_val = ((table_elem_type_t *)table_inst->elems)[tbl_elem_idx];
  3317. if (tbl_elem_val == NULL_REF) {
  3318. wasm_set_exception(module_inst, "uninitialized element");
  3319. goto got_exception;
  3320. }
  3321. #if WASM_ENABLE_GC == 0
  3322. func_idx = (uint32)tbl_elem_val;
  3323. #else
  3324. func_idx =
  3325. wasm_func_obj_get_func_idx_bound((WASMFuncObjectRef)tbl_elem_val);
  3326. #endif
  3327. /**
  3328. * we insist to call functions owned by the module itself
  3329. **/
  3330. if (func_idx >= module_inst->e->function_count) {
  3331. wasm_set_exception(module_inst, "unknown function");
  3332. goto got_exception;
  3333. }
  3334. func_inst = module_inst->e->functions + func_idx;
  3335. if (check_type_idx) {
  3336. WASMType *cur_type = module_inst->module->types[type_idx];
  3337. WASMType *cur_func_type;
  3338. if (func_inst->is_import_func)
  3339. cur_func_type = (WASMType *)func_inst->u.func_import->func_type;
  3340. else
  3341. cur_func_type = (WASMType *)func_inst->u.func->func_type;
  3342. if (cur_type != cur_func_type) {
  3343. wasm_set_exception(module_inst, "indirect call type mismatch");
  3344. goto got_exception;
  3345. }
  3346. }
  3347. interp_call_wasm(module_inst, exec_env, func_inst, argc, argv);
  3348. return !wasm_copy_exception(module_inst, NULL);
  3349. got_exception:
  3350. return false;
  3351. }
  3352. bool
  3353. wasm_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  3354. uint32 argc, uint32 argv[])
  3355. {
  3356. return call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, false, 0);
  3357. }
  3358. #if WASM_ENABLE_THREAD_MGR != 0
  3359. bool
  3360. wasm_set_aux_stack(WASMExecEnv *exec_env, uint64 start_offset, uint32 size)
  3361. {
  3362. WASMModuleInstance *module_inst =
  3363. (WASMModuleInstance *)exec_env->module_inst;
  3364. uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
  3365. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
  3366. /* Check the aux stack space */
  3367. uint64 data_end = module_inst->module->aux_data_end;
  3368. uint64 stack_bottom = module_inst->module->aux_stack_bottom;
  3369. bool is_stack_before_data = stack_bottom < data_end ? true : false;
  3370. if ((is_stack_before_data && (size > start_offset))
  3371. || ((!is_stack_before_data) && (start_offset - data_end < size)))
  3372. return false;
  3373. #endif
  3374. if (stack_top_idx != (uint32)-1) {
  3375. /* The aux stack top is a wasm global,
  3376. set the initial value for the global */
  3377. uint8 *global_addr =
  3378. module_inst->global_data
  3379. + module_inst->e->globals[stack_top_idx].data_offset;
  3380. *(int32 *)global_addr = (uint32)start_offset;
  3381. /* The aux stack boundary is a constant value,
  3382. set the value to exec_env */
  3383. exec_env->aux_stack_boundary = (uintptr_t)start_offset - size;
  3384. exec_env->aux_stack_bottom = (uintptr_t)start_offset;
  3385. return true;
  3386. }
  3387. return false;
  3388. }
  3389. bool
  3390. wasm_get_aux_stack(WASMExecEnv *exec_env, uint64 *start_offset, uint32 *size)
  3391. {
  3392. WASMModuleInstance *module_inst =
  3393. (WASMModuleInstance *)exec_env->module_inst;
  3394. /* The aux stack information is resolved in loader
  3395. and store in module */
  3396. uint64 stack_bottom = module_inst->module->aux_stack_bottom;
  3397. uint32 total_aux_stack_size = module_inst->module->aux_stack_size;
  3398. if (stack_bottom != 0 && total_aux_stack_size != 0) {
  3399. if (start_offset)
  3400. *start_offset = stack_bottom;
  3401. if (size)
  3402. *size = total_aux_stack_size;
  3403. return true;
  3404. }
  3405. return false;
  3406. }
  3407. #endif
  3408. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  3409. void
  3410. wasm_get_module_mem_consumption(const WASMModule *module,
  3411. WASMModuleMemConsumption *mem_conspn)
  3412. {
  3413. uint32 i, size;
  3414. memset(mem_conspn, 0, sizeof(*mem_conspn));
  3415. mem_conspn->module_struct_size = sizeof(WASMModule);
  3416. mem_conspn->types_size = sizeof(WASMFuncType *) * module->type_count;
  3417. for (i = 0; i < module->type_count; i++) {
  3418. WASMFuncType *type = module->types[i];
  3419. size = offsetof(WASMFuncType, types)
  3420. + sizeof(uint8) * (type->param_count + type->result_count);
  3421. mem_conspn->types_size += size;
  3422. }
  3423. mem_conspn->imports_size = sizeof(WASMImport) * module->import_count;
  3424. mem_conspn->functions_size =
  3425. sizeof(WASMFunction *) * module->function_count;
  3426. for (i = 0; i < module->function_count; i++) {
  3427. WASMFunction *func = module->functions[i];
  3428. WASMFuncType *type = func->func_type;
  3429. size = sizeof(WASMFunction) + func->local_count
  3430. + sizeof(uint16) * (type->param_count + func->local_count);
  3431. #if WASM_ENABLE_FAST_INTERP != 0
  3432. size +=
  3433. func->code_compiled_size + sizeof(uint32) * func->const_cell_num;
  3434. #endif
  3435. mem_conspn->functions_size += size;
  3436. }
  3437. mem_conspn->tables_size = sizeof(WASMTable) * module->table_count;
  3438. mem_conspn->memories_size = sizeof(WASMMemory) * module->memory_count;
  3439. mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count;
  3440. mem_conspn->exports_size = sizeof(WASMExport) * module->export_count;
  3441. mem_conspn->table_segs_size =
  3442. sizeof(WASMTableSeg) * module->table_seg_count;
  3443. for (i = 0; i < module->table_seg_count; i++) {
  3444. WASMTableSeg *table_seg = &module->table_segments[i];
  3445. mem_conspn->tables_size +=
  3446. sizeof(InitializerExpression *) * table_seg->value_count;
  3447. }
  3448. mem_conspn->data_segs_size = sizeof(WASMDataSeg *) * module->data_seg_count;
  3449. for (i = 0; i < module->data_seg_count; i++) {
  3450. mem_conspn->data_segs_size += sizeof(WASMDataSeg);
  3451. }
  3452. if (module->const_str_list) {
  3453. StringNode *node = module->const_str_list, *node_next;
  3454. while (node) {
  3455. node_next = node->next;
  3456. mem_conspn->const_strs_size +=
  3457. sizeof(StringNode) + strlen(node->str) + 1;
  3458. node = node_next;
  3459. }
  3460. }
  3461. mem_conspn->total_size += mem_conspn->module_struct_size;
  3462. mem_conspn->total_size += mem_conspn->types_size;
  3463. mem_conspn->total_size += mem_conspn->imports_size;
  3464. mem_conspn->total_size += mem_conspn->functions_size;
  3465. mem_conspn->total_size += mem_conspn->tables_size;
  3466. mem_conspn->total_size += mem_conspn->memories_size;
  3467. mem_conspn->total_size += mem_conspn->globals_size;
  3468. mem_conspn->total_size += mem_conspn->exports_size;
  3469. mem_conspn->total_size += mem_conspn->table_segs_size;
  3470. mem_conspn->total_size += mem_conspn->data_segs_size;
  3471. mem_conspn->total_size += mem_conspn->const_strs_size;
  3472. }
  3473. void
  3474. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
  3475. WASMModuleInstMemConsumption *mem_conspn)
  3476. {
  3477. uint32 i;
  3478. uint64 size;
  3479. memset(mem_conspn, 0, sizeof(*mem_conspn));
  3480. mem_conspn->module_inst_struct_size = (uint8 *)module_inst->e
  3481. - (uint8 *)module_inst
  3482. + sizeof(WASMModuleInstanceExtra);
  3483. mem_conspn->memories_size =
  3484. sizeof(WASMMemoryInstance *) * module_inst->memory_count;
  3485. for (i = 0; i < module_inst->memory_count; i++) {
  3486. WASMMemoryInstance *memory = module_inst->memories[i];
  3487. size = memory->num_bytes_per_page * memory->cur_page_count;
  3488. mem_conspn->memories_size += size;
  3489. mem_conspn->app_heap_size += memory->heap_data_end - memory->heap_data;
  3490. /* size of app heap structure */
  3491. mem_conspn->memories_size += mem_allocator_get_heap_struct_size();
  3492. /* Module instance structures have been appended into the end of
  3493. module instance */
  3494. }
  3495. mem_conspn->tables_size =
  3496. sizeof(WASMTableInstance *) * module_inst->table_count;
  3497. /* Table instance structures and table elements have been appended into
  3498. the end of module instance */
  3499. mem_conspn->functions_size =
  3500. sizeof(WASMFunctionInstance) * module_inst->e->function_count;
  3501. mem_conspn->globals_size =
  3502. sizeof(WASMGlobalInstance) * module_inst->e->global_count;
  3503. /* Global data has been appended into the end of module instance */
  3504. mem_conspn->exports_size =
  3505. sizeof(WASMExportFuncInstance) * module_inst->export_func_count;
  3506. mem_conspn->total_size += mem_conspn->module_inst_struct_size;
  3507. mem_conspn->total_size += mem_conspn->memories_size;
  3508. mem_conspn->total_size += mem_conspn->functions_size;
  3509. mem_conspn->total_size += mem_conspn->tables_size;
  3510. mem_conspn->total_size += mem_conspn->globals_size;
  3511. mem_conspn->total_size += mem_conspn->exports_size;
  3512. }
  3513. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
  3514. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  3515. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  3516. bool
  3517. wasm_interp_create_call_stack(struct WASMExecEnv *exec_env)
  3518. {
  3519. WASMModuleInstance *module_inst =
  3520. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  3521. WASMModule *module = module_inst->module;
  3522. WASMInterpFrame *first_frame,
  3523. *cur_frame = wasm_exec_env_get_cur_frame(exec_env);
  3524. uint32 n = 0;
  3525. /* count frames includes a function */
  3526. first_frame = cur_frame;
  3527. while (cur_frame) {
  3528. if (cur_frame->function) {
  3529. n++;
  3530. }
  3531. cur_frame = cur_frame->prev_frame;
  3532. }
  3533. /* release previous stack frames and create new ones */
  3534. destroy_c_api_frames(module_inst->frames);
  3535. if (!bh_vector_init(module_inst->frames, n, sizeof(WASMCApiFrame), false)) {
  3536. return false;
  3537. }
  3538. cur_frame = first_frame;
  3539. n = 0;
  3540. while (cur_frame) {
  3541. WASMCApiFrame frame = { 0 };
  3542. WASMFunctionInstance *func_inst = cur_frame->function;
  3543. const char *func_name = NULL;
  3544. const uint8 *func_code_base = NULL;
  3545. uint32 max_local_cell_num, max_stack_cell_num;
  3546. uint32 all_cell_num, lp_size;
  3547. if (!func_inst) {
  3548. cur_frame = cur_frame->prev_frame;
  3549. continue;
  3550. }
  3551. /* place holder, will overwrite it in wasm_c_api */
  3552. frame.instance = module_inst;
  3553. frame.module_offset = 0;
  3554. frame.func_index = (uint32)(func_inst - module_inst->e->functions);
  3555. func_code_base = wasm_get_func_code(func_inst);
  3556. if (!cur_frame->ip || !func_code_base) {
  3557. frame.func_offset = 0;
  3558. }
  3559. else {
  3560. #if WASM_ENABLE_FAST_INTERP == 0
  3561. frame.func_offset = (uint32)(cur_frame->ip - module->load_addr);
  3562. #else
  3563. frame.func_offset = (uint32)(cur_frame->ip - func_code_base);
  3564. #endif
  3565. }
  3566. func_name = get_func_name_from_index(module_inst, frame.func_index);
  3567. frame.func_name_wp = func_name;
  3568. if (frame.func_index >= module->import_function_count) {
  3569. uint32 wasm_func_idx =
  3570. frame.func_index - module->import_function_count;
  3571. max_local_cell_num =
  3572. module->functions[wasm_func_idx]->param_cell_num
  3573. + module->functions[wasm_func_idx]->local_cell_num;
  3574. max_stack_cell_num =
  3575. module->functions[wasm_func_idx]->max_stack_cell_num;
  3576. all_cell_num = max_local_cell_num + max_stack_cell_num;
  3577. #if WASM_ENABLE_FAST_INTERP != 0
  3578. all_cell_num += module->functions[wasm_func_idx]->const_cell_num;
  3579. #endif
  3580. }
  3581. else {
  3582. WASMFuncType *func_type =
  3583. module->import_functions[frame.func_index].u.function.func_type;
  3584. max_local_cell_num =
  3585. func_type->param_cell_num > 2 ? func_type->param_cell_num : 2;
  3586. max_stack_cell_num = 0;
  3587. all_cell_num = max_local_cell_num + max_stack_cell_num;
  3588. }
  3589. #if WASM_ENABLE_GC == 0
  3590. lp_size = all_cell_num * 4;
  3591. #else
  3592. lp_size = align_uint(all_cell_num * 5, 4);
  3593. #endif
  3594. if (lp_size > 0) {
  3595. if (!(frame.lp = wasm_runtime_malloc(lp_size))) {
  3596. destroy_c_api_frames(module_inst->frames);
  3597. return false;
  3598. }
  3599. bh_memcpy_s(frame.lp, lp_size, cur_frame->lp, lp_size);
  3600. #if WASM_ENABLE_GC != 0
  3601. #if WASM_ENABLE_FAST_INTERP == 0
  3602. frame.sp = frame.lp + (cur_frame->sp - cur_frame->lp);
  3603. #else
  3604. /* for fast-interp, let frame sp point to the end of the frame */
  3605. frame.sp = frame.lp + all_cell_num;
  3606. #endif
  3607. frame.frame_ref = (uint8 *)frame.lp
  3608. + (wasm_interp_get_frame_ref(cur_frame)
  3609. - (uint8 *)cur_frame->lp);
  3610. #endif
  3611. }
  3612. if (!bh_vector_append(module_inst->frames, &frame)) {
  3613. if (frame.lp)
  3614. wasm_runtime_free(frame.lp);
  3615. destroy_c_api_frames(module_inst->frames);
  3616. return false;
  3617. }
  3618. cur_frame = cur_frame->prev_frame;
  3619. n++;
  3620. }
  3621. return true;
  3622. }
  3623. #define PRINT_OR_DUMP() \
  3624. do { \
  3625. total_len += \
  3626. wasm_runtime_dump_line_buf_impl(line_buf, print, &buf, &len); \
  3627. if ((!print) && buf && (len == 0)) { \
  3628. exception_unlock(module_inst); \
  3629. return total_len; \
  3630. } \
  3631. } while (0)
  3632. uint32
  3633. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf,
  3634. uint32 len)
  3635. {
  3636. WASMModuleInstance *module_inst =
  3637. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  3638. uint32 n = 0, total_len = 0, total_frames;
  3639. /* reserve 256 bytes for line buffer, any line longer than 256 bytes
  3640. * will be truncated */
  3641. char line_buf[256];
  3642. if (!module_inst->frames) {
  3643. return 0;
  3644. }
  3645. total_frames = (uint32)bh_vector_size(module_inst->frames);
  3646. if (total_frames == 0) {
  3647. return 0;
  3648. }
  3649. exception_lock(module_inst);
  3650. snprintf(line_buf, sizeof(line_buf), "\n");
  3651. PRINT_OR_DUMP();
  3652. while (n < total_frames) {
  3653. WASMCApiFrame frame = { 0 };
  3654. uint32 line_length, i;
  3655. if (!bh_vector_get(module_inst->frames, n, &frame)) {
  3656. exception_unlock(module_inst);
  3657. return 0;
  3658. }
  3659. #if WASM_ENABLE_FAST_JIT != 0
  3660. /* Fast JIT doesn't support committing ip (instruction pointer) yet */
  3661. if (module_inst->e->running_mode == Mode_Fast_JIT
  3662. || module_inst->e->running_mode == Mode_Multi_Tier_JIT) {
  3663. /* function name not exported, print number instead */
  3664. if (frame.func_name_wp == NULL) {
  3665. line_length = snprintf(line_buf, sizeof(line_buf),
  3666. "#%02" PRIu32 " $f%" PRIu32 "\n", n,
  3667. frame.func_index);
  3668. }
  3669. else {
  3670. line_length =
  3671. snprintf(line_buf, sizeof(line_buf), "#%02" PRIu32 " %s\n",
  3672. n, frame.func_name_wp);
  3673. }
  3674. }
  3675. else
  3676. #endif
  3677. {
  3678. /* function name not exported, print number instead */
  3679. if (frame.func_name_wp == NULL) {
  3680. line_length =
  3681. snprintf(line_buf, sizeof(line_buf),
  3682. "#%02" PRIu32 ": 0x%04x - $f%" PRIu32 "\n", n,
  3683. frame.func_offset, frame.func_index);
  3684. }
  3685. else {
  3686. line_length = snprintf(line_buf, sizeof(line_buf),
  3687. "#%02" PRIu32 ": 0x%04x - %s\n", n,
  3688. frame.func_offset, frame.func_name_wp);
  3689. }
  3690. }
  3691. if (line_length >= sizeof(line_buf)) {
  3692. uint32 line_buffer_len = sizeof(line_buf);
  3693. /* If line too long, ensure the last character is '\n' */
  3694. for (i = line_buffer_len - 5; i < line_buffer_len - 2; i++) {
  3695. line_buf[i] = '.';
  3696. }
  3697. line_buf[line_buffer_len - 2] = '\n';
  3698. }
  3699. PRINT_OR_DUMP();
  3700. n++;
  3701. }
  3702. snprintf(line_buf, sizeof(line_buf), "\n");
  3703. PRINT_OR_DUMP();
  3704. exception_unlock(module_inst);
  3705. return total_len + 1;
  3706. }
  3707. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
  3708. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  3709. || WASM_ENABLE_WAMR_COMPILER != 0
  3710. void
  3711. jit_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id)
  3712. {
  3713. if (id != EXCE_ALREADY_THROWN)
  3714. wasm_set_exception_with_id(module_inst, id);
  3715. #ifdef OS_ENABLE_HW_BOUND_CHECK
  3716. wasm_runtime_access_exce_check_guard_page();
  3717. #endif
  3718. }
  3719. bool
  3720. jit_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  3721. uint64 app_buf_addr, uint64 app_buf_size,
  3722. void **p_native_addr)
  3723. {
  3724. bool ret = wasm_check_app_addr_and_convert(
  3725. module_inst, is_str, app_buf_addr, app_buf_size, p_native_addr);
  3726. #ifdef OS_ENABLE_HW_BOUND_CHECK
  3727. if (!ret)
  3728. wasm_runtime_access_exce_check_guard_page();
  3729. #endif
  3730. return ret;
  3731. }
  3732. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  3733. || WASM_ENABLE_WAMR_COMPILER != 0 */
  3734. #if WASM_ENABLE_FAST_JIT != 0
  3735. bool
  3736. fast_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  3737. uint32 type_idx, uint32 argc, uint32 *argv)
  3738. {
  3739. return call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, true,
  3740. type_idx);
  3741. }
  3742. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  3743. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  3744. bool
  3745. llvm_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  3746. uint32 argc, uint32 *argv)
  3747. {
  3748. bool ret;
  3749. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  3750. ret = call_indirect(exec_env, tbl_idx, elem_idx, argc, argv, false, 0);
  3751. #ifdef OS_ENABLE_HW_BOUND_CHECK
  3752. if (!ret)
  3753. wasm_runtime_access_exce_check_guard_page();
  3754. #endif
  3755. return ret;
  3756. }
  3757. bool
  3758. llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
  3759. uint32 *argv)
  3760. {
  3761. WASMModuleInstance *module_inst;
  3762. WASMModule *module;
  3763. uint32 *func_type_indexes;
  3764. uint32 func_type_idx;
  3765. WASMFuncType *func_type;
  3766. void *func_ptr;
  3767. WASMFunctionImport *import_func;
  3768. CApiFuncImport *c_api_func_import = NULL;
  3769. const char *signature;
  3770. void *attachment;
  3771. char buf[96];
  3772. bool ret = false;
  3773. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  3774. module_inst = (WASMModuleInstance *)wasm_runtime_get_module_inst(exec_env);
  3775. module = module_inst->module;
  3776. func_type_indexes = module_inst->func_type_indexes;
  3777. func_type_idx = func_type_indexes[func_idx];
  3778. func_type = (WASMFuncType *)module->types[func_type_idx];
  3779. func_ptr = module_inst->func_ptrs[func_idx];
  3780. bh_assert(func_idx < module->import_function_count);
  3781. import_func = &module->import_functions[func_idx].u.function;
  3782. if (import_func->call_conv_wasm_c_api) {
  3783. if (module_inst->c_api_func_imports) {
  3784. c_api_func_import = module_inst->c_api_func_imports + func_idx;
  3785. func_ptr = c_api_func_import->func_ptr_linked;
  3786. }
  3787. else {
  3788. c_api_func_import = NULL;
  3789. func_ptr = NULL;
  3790. }
  3791. }
  3792. if (!func_ptr) {
  3793. snprintf(buf, sizeof(buf),
  3794. "failed to call unlinked import function (%s, %s)",
  3795. import_func->module_name, import_func->field_name);
  3796. wasm_set_exception(module_inst, buf);
  3797. goto fail;
  3798. }
  3799. attachment = import_func->attachment;
  3800. if (import_func->call_conv_wasm_c_api) {
  3801. ret = wasm_runtime_invoke_c_api_native(
  3802. (WASMModuleInstanceCommon *)module_inst, func_ptr, func_type, argc,
  3803. argv, c_api_func_import->with_env_arg, c_api_func_import->env_arg);
  3804. }
  3805. else if (!import_func->call_conv_raw) {
  3806. signature = import_func->signature;
  3807. ret =
  3808. wasm_runtime_invoke_native(exec_env, func_ptr, func_type, signature,
  3809. attachment, argv, argc, argv);
  3810. }
  3811. else {
  3812. signature = import_func->signature;
  3813. ret = wasm_runtime_invoke_native_raw(exec_env, func_ptr, func_type,
  3814. signature, attachment, argv, argc,
  3815. argv);
  3816. }
  3817. fail:
  3818. #ifdef OS_ENABLE_HW_BOUND_CHECK
  3819. if (!ret)
  3820. wasm_runtime_access_exce_check_guard_page();
  3821. #endif
  3822. return ret;
  3823. }
  3824. #if WASM_ENABLE_BULK_MEMORY != 0
  3825. bool
  3826. llvm_jit_memory_init(WASMModuleInstance *module_inst, uint32 seg_index,
  3827. uint32 offset, uint32 len, size_t dst)
  3828. {
  3829. WASMMemoryInstance *memory_inst;
  3830. WASMModule *module;
  3831. uint8 *data;
  3832. uint8 *maddr;
  3833. uint64 seg_len;
  3834. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  3835. memory_inst = wasm_get_default_memory(module_inst);
  3836. if (bh_bitmap_get_bit(module_inst->e->common.data_dropped, seg_index)) {
  3837. seg_len = 0;
  3838. data = NULL;
  3839. }
  3840. else {
  3841. module = module_inst->module;
  3842. seg_len = module->data_segments[seg_index]->data_length;
  3843. data = module->data_segments[seg_index]->data;
  3844. }
  3845. if (!wasm_runtime_validate_app_addr((WASMModuleInstanceCommon *)module_inst,
  3846. (uint64)dst, (uint64)len))
  3847. return false;
  3848. if ((uint64)offset + (uint64)len > seg_len) {
  3849. wasm_set_exception(module_inst, "out of bounds memory access");
  3850. return false;
  3851. }
  3852. maddr = wasm_runtime_addr_app_to_native(
  3853. (WASMModuleInstanceCommon *)module_inst, (uint64)dst);
  3854. SHARED_MEMORY_LOCK(memory_inst);
  3855. bh_memcpy_s(maddr, CLAMP_U64_TO_U32(memory_inst->memory_data_size - dst),
  3856. data + offset, len);
  3857. SHARED_MEMORY_UNLOCK(memory_inst);
  3858. return true;
  3859. }
  3860. bool
  3861. llvm_jit_data_drop(WASMModuleInstance *module_inst, uint32 seg_index)
  3862. {
  3863. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  3864. bh_bitmap_set_bit(module_inst->e->common.data_dropped, seg_index);
  3865. /* Currently we can't free the dropped data segment
  3866. as they are stored in wasm bytecode */
  3867. return true;
  3868. }
  3869. #endif /* end of WASM_ENABLE_BULK_MEMORY != 0 */
  3870. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3871. void
  3872. llvm_jit_drop_table_seg(WASMModuleInstance *module_inst, uint32 tbl_seg_idx)
  3873. {
  3874. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  3875. bh_bitmap_set_bit(module_inst->e->common.elem_dropped, tbl_seg_idx);
  3876. }
  3877. void
  3878. llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx,
  3879. uint32 tbl_seg_idx, uint32 length, uint32 src_offset,
  3880. uint32 dst_offset)
  3881. {
  3882. WASMTableInstance *tbl_inst;
  3883. WASMTableSeg *tbl_seg;
  3884. table_elem_type_t *table_elems;
  3885. InitializerExpression *tbl_seg_init_values = NULL, *init_values;
  3886. uint32 i, tbl_seg_len = 0;
  3887. #if WASM_ENABLE_GC != 0
  3888. void *func_obj;
  3889. #endif
  3890. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  3891. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  3892. tbl_seg = module_inst->module->table_segments + tbl_seg_idx;
  3893. bh_assert(tbl_inst);
  3894. bh_assert(tbl_seg);
  3895. if (!bh_bitmap_get_bit(module_inst->e->common.elem_dropped, tbl_seg_idx)) {
  3896. /* table segment isn't dropped */
  3897. tbl_seg_init_values = tbl_seg->init_values;
  3898. tbl_seg_len = tbl_seg->value_count;
  3899. }
  3900. if (offset_len_out_of_bounds(src_offset, length, tbl_seg_len)
  3901. || offset_len_out_of_bounds(dst_offset, length, tbl_inst->cur_size)) {
  3902. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  3903. return;
  3904. }
  3905. if (!length) {
  3906. return;
  3907. }
  3908. table_elems = tbl_inst->elems + dst_offset;
  3909. init_values = tbl_seg_init_values + src_offset;
  3910. for (i = 0; i < length; i++) {
  3911. #if WASM_ENABLE_GC != 0
  3912. /* UINT32_MAX indicates that it is a null ref */
  3913. if (init_values[i].u.ref_index != UINT32_MAX) {
  3914. if (!(func_obj = wasm_create_func_obj(module_inst,
  3915. init_values[i].u.ref_index,
  3916. true, NULL, 0))) {
  3917. wasm_set_exception(module_inst, "null function object");
  3918. return;
  3919. }
  3920. table_elems[i] = func_obj;
  3921. }
  3922. else {
  3923. table_elems[i] = NULL_REF;
  3924. }
  3925. #else
  3926. table_elems[i] = init_values[i].u.ref_index;
  3927. #endif
  3928. }
  3929. }
  3930. void
  3931. llvm_jit_table_copy(WASMModuleInstance *module_inst, uint32 src_tbl_idx,
  3932. uint32 dst_tbl_idx, uint32 length, uint32 src_offset,
  3933. uint32 dst_offset)
  3934. {
  3935. WASMTableInstance *src_tbl_inst;
  3936. WASMTableInstance *dst_tbl_inst;
  3937. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  3938. src_tbl_inst = wasm_get_table_inst(module_inst, src_tbl_idx);
  3939. dst_tbl_inst = wasm_get_table_inst(module_inst, dst_tbl_idx);
  3940. bh_assert(src_tbl_inst);
  3941. bh_assert(dst_tbl_inst);
  3942. if (offset_len_out_of_bounds(dst_offset, length, dst_tbl_inst->cur_size)
  3943. || offset_len_out_of_bounds(src_offset, length,
  3944. src_tbl_inst->cur_size)) {
  3945. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  3946. return;
  3947. }
  3948. /* if src_offset >= dst_offset, copy from front to back */
  3949. /* if src_offset < dst_offset, copy from back to front */
  3950. /* merge all together */
  3951. bh_memmove_s((uint8 *)dst_tbl_inst + offsetof(WASMTableInstance, elems)
  3952. + sizeof(table_elem_type_t) * dst_offset,
  3953. (uint32)sizeof(table_elem_type_t)
  3954. * (dst_tbl_inst->cur_size - dst_offset),
  3955. (uint8 *)src_tbl_inst + offsetof(WASMTableInstance, elems)
  3956. + sizeof(table_elem_type_t) * src_offset,
  3957. (uint32)sizeof(table_elem_type_t) * length);
  3958. }
  3959. void
  3960. llvm_jit_table_fill(WASMModuleInstance *module_inst, uint32 tbl_idx,
  3961. uint32 length, uintptr_t val, uint32 data_offset)
  3962. {
  3963. WASMTableInstance *tbl_inst;
  3964. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  3965. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  3966. bh_assert(tbl_inst);
  3967. if (offset_len_out_of_bounds(data_offset, length, tbl_inst->cur_size)) {
  3968. jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
  3969. return;
  3970. }
  3971. for (; length != 0; data_offset++, length--) {
  3972. tbl_inst->elems[data_offset] = (table_elem_type_t)val;
  3973. }
  3974. }
  3975. uint32
  3976. llvm_jit_table_grow(WASMModuleInstance *module_inst, uint32 tbl_idx,
  3977. uint32 inc_size, uintptr_t init_val)
  3978. {
  3979. WASMTableInstance *tbl_inst;
  3980. uint32 i, orig_size, total_size;
  3981. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  3982. tbl_inst = wasm_get_table_inst(module_inst, tbl_idx);
  3983. if (!tbl_inst) {
  3984. return (uint32)-1;
  3985. }
  3986. orig_size = tbl_inst->cur_size;
  3987. if (!inc_size) {
  3988. return orig_size;
  3989. }
  3990. if (tbl_inst->cur_size > UINT32_MAX - inc_size) { /* integer overflow */
  3991. #if WASM_ENABLE_SPEC_TEST == 0
  3992. LOG_WARNING("table grow (%" PRIu32 "-> %" PRIu32
  3993. ") failed because of integer overflow",
  3994. tbl_inst->cur_size, inc_size);
  3995. #endif
  3996. return (uint32)-1;
  3997. }
  3998. total_size = tbl_inst->cur_size + inc_size;
  3999. if (total_size > tbl_inst->max_size) {
  4000. #if WASM_ENABLE_SPEC_TEST == 0
  4001. LOG_WARNING("table grow (%" PRIu32 "-> %" PRIu32
  4002. ") failed because of over max size",
  4003. tbl_inst->cur_size, inc_size);
  4004. #endif
  4005. return (uint32)-1;
  4006. }
  4007. /* fill in */
  4008. for (i = 0; i < inc_size; ++i) {
  4009. tbl_inst->elems[tbl_inst->cur_size + i] = (table_elem_type_t)init_val;
  4010. }
  4011. tbl_inst->cur_size = total_size;
  4012. return orig_size;
  4013. }
  4014. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4015. #if WASM_ENABLE_GC != 0
  4016. void *
  4017. llvm_jit_create_func_obj(WASMModuleInstance *module_inst, uint32 func_idx,
  4018. bool throw_exce, char *error_buf,
  4019. uint32 error_buf_size)
  4020. {
  4021. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  4022. return wasm_create_func_obj(module_inst, func_idx, throw_exce, error_buf,
  4023. error_buf_size);
  4024. }
  4025. bool
  4026. llvm_jit_obj_is_instance_of(WASMModuleInstance *module_inst,
  4027. WASMObjectRef gc_obj, uint32 type_index)
  4028. {
  4029. WASMModule *module = module_inst->module;
  4030. WASMType **types = module->types;
  4031. uint32 type_count = module->type_count;
  4032. return wasm_obj_is_instance_of(gc_obj, type_index, types, type_count);
  4033. }
  4034. bool
  4035. llvm_jit_func_type_is_super_of(WASMModuleInstance *module_inst,
  4036. uint32 type_idx1, uint32 type_idx2)
  4037. {
  4038. WASMModule *module = module_inst->module;
  4039. WASMType **types = module->types;
  4040. if (type_idx1 == type_idx2)
  4041. return true;
  4042. bh_assert(types[type_idx1]->type_flag == WASM_TYPE_FUNC);
  4043. bh_assert(types[type_idx2]->type_flag == WASM_TYPE_FUNC);
  4044. return wasm_func_type_is_super_of((WASMFuncType *)types[type_idx1],
  4045. (WASMFuncType *)types[type_idx2]);
  4046. }
  4047. WASMRttTypeRef
  4048. llvm_jit_rtt_type_new(WASMModuleInstance *module_inst, uint32 type_index)
  4049. {
  4050. WASMModule *module = module_inst->module;
  4051. WASMType *defined_type = module->types[type_index];
  4052. WASMRttType **rtt_types = module->rtt_types;
  4053. uint32 rtt_type_count = module->type_count;
  4054. korp_mutex *rtt_type_lock = &module->rtt_type_lock;
  4055. return wasm_rtt_type_new(defined_type, type_index, rtt_types,
  4056. rtt_type_count, rtt_type_lock);
  4057. }
  4058. bool
  4059. llvm_array_init_with_data(WASMModuleInstance *module_inst, uint32 seg_index,
  4060. uint32 data_seg_offset, WASMArrayObjectRef array_obj,
  4061. uint32 elem_size, uint32 array_len)
  4062. {
  4063. WASMModule *wasm_module = module_inst->module;
  4064. WASMDataSeg *data_seg;
  4065. uint8 *array_elem_base;
  4066. uint64 total_size;
  4067. data_seg = wasm_module->data_segments[seg_index];
  4068. total_size = (int64)elem_size * array_len;
  4069. if (data_seg_offset >= data_seg->data_length
  4070. || total_size > data_seg->data_length - data_seg_offset) {
  4071. wasm_set_exception(module_inst, "out of bounds memory access");
  4072. return false;
  4073. }
  4074. array_elem_base = (uint8 *)wasm_array_obj_first_elem_addr(array_obj);
  4075. bh_memcpy_s(array_elem_base, (uint32)total_size,
  4076. data_seg->data + data_seg_offset, (uint32)total_size);
  4077. return true;
  4078. }
  4079. #endif /* end of WASM_ENABLE_GC != 0 */
  4080. #endif /* end of WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 */
  4081. #if WASM_ENABLE_LIBC_WASI != 0 && WASM_ENABLE_MULTI_MODULE != 0
  4082. void
  4083. wasm_propagate_wasi_args(WASMModule *module)
  4084. {
  4085. if (!module->import_count)
  4086. return;
  4087. bh_assert(&module->import_module_list_head);
  4088. WASMRegisteredModule *node =
  4089. bh_list_first_elem(&module->import_module_list_head);
  4090. while (node) {
  4091. WASIArguments *wasi_args_impt_mod =
  4092. &((WASMModule *)(node->module))->wasi_args;
  4093. bh_assert(wasi_args_impt_mod);
  4094. bh_memcpy_s(wasi_args_impt_mod, sizeof(WASIArguments),
  4095. &module->wasi_args, sizeof(WASIArguments));
  4096. node = bh_list_elem_next(node);
  4097. }
  4098. }
  4099. #endif
  4100. bool
  4101. wasm_check_utf8_str(const uint8 *str, uint32 len)
  4102. {
  4103. /* The valid ranges are taken from page 125, below link
  4104. https://www.unicode.org/versions/Unicode9.0.0/ch03.pdf */
  4105. const uint8 *p = str, *p_end = str + len;
  4106. uint8 chr;
  4107. while (p < p_end) {
  4108. chr = *p;
  4109. if (chr == 0) {
  4110. LOG_WARNING(
  4111. "LIMITATION: a string which contains '\\00' is unsupported");
  4112. return false;
  4113. }
  4114. else if (chr < 0x80) {
  4115. p++;
  4116. }
  4117. else if (chr >= 0xC2 && chr <= 0xDF && p + 1 < p_end) {
  4118. if (p[1] < 0x80 || p[1] > 0xBF) {
  4119. return false;
  4120. }
  4121. p += 2;
  4122. }
  4123. else if (chr >= 0xE0 && chr <= 0xEF && p + 2 < p_end) {
  4124. if (chr == 0xE0) {
  4125. if (p[1] < 0xA0 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  4126. return false;
  4127. }
  4128. }
  4129. else if (chr == 0xED) {
  4130. if (p[1] < 0x80 || p[1] > 0x9F || p[2] < 0x80 || p[2] > 0xBF) {
  4131. return false;
  4132. }
  4133. }
  4134. else { /* chr >= 0xE1 && chr <= 0xEF */
  4135. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  4136. return false;
  4137. }
  4138. }
  4139. p += 3;
  4140. }
  4141. else if (chr >= 0xF0 && chr <= 0xF4 && p + 3 < p_end) {
  4142. if (chr == 0xF0) {
  4143. if (p[1] < 0x90 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  4144. || p[3] < 0x80 || p[3] > 0xBF) {
  4145. return false;
  4146. }
  4147. }
  4148. else if (chr <= 0xF3) { /* and also chr >= 0xF1 */
  4149. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  4150. || p[3] < 0x80 || p[3] > 0xBF) {
  4151. return false;
  4152. }
  4153. }
  4154. else { /* chr == 0xF4 */
  4155. if (p[1] < 0x80 || p[1] > 0x8F || p[2] < 0x80 || p[2] > 0xBF
  4156. || p[3] < 0x80 || p[3] > 0xBF) {
  4157. return false;
  4158. }
  4159. }
  4160. p += 4;
  4161. }
  4162. else {
  4163. return false;
  4164. }
  4165. }
  4166. return (p == p_end);
  4167. }
  4168. char *
  4169. wasm_const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
  4170. bool is_load_from_file_buf, char *error_buf,
  4171. uint32 error_buf_size)
  4172. {
  4173. StringNode *node, *node_next;
  4174. if (!wasm_check_utf8_str(str, len)) {
  4175. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4176. return NULL;
  4177. }
  4178. if (len == 0) {
  4179. return "";
  4180. }
  4181. else if (is_load_from_file_buf) {
  4182. /* As the file buffer can be referred to after loading, we use
  4183. the previous byte of leb encoded size to adjust the string:
  4184. move string 1 byte backward and then append '\0' */
  4185. char *c_str = (char *)str - 1;
  4186. bh_memmove_s(c_str, len + 1, c_str + 1, len);
  4187. c_str[len] = '\0';
  4188. return c_str;
  4189. }
  4190. /* Search const str list */
  4191. node = module->const_str_list;
  4192. while (node) {
  4193. node_next = node->next;
  4194. if (strlen(node->str) == len && !memcmp(node->str, str, len))
  4195. break;
  4196. node = node_next;
  4197. }
  4198. if (node) {
  4199. return node->str;
  4200. }
  4201. if (!(node = runtime_malloc(sizeof(StringNode) + len + 1, error_buf,
  4202. error_buf_size))) {
  4203. return NULL;
  4204. }
  4205. node->str = ((char *)node) + sizeof(StringNode);
  4206. bh_memcpy_s(node->str, len + 1, str, len);
  4207. node->str[len] = '\0';
  4208. if (!module->const_str_list) {
  4209. /* set as head */
  4210. module->const_str_list = node;
  4211. node->next = NULL;
  4212. }
  4213. else {
  4214. /* insert it */
  4215. node->next = module->const_str_list;
  4216. module->const_str_list = node;
  4217. }
  4218. return node->str;
  4219. }
  4220. bool
  4221. wasm_set_module_name(WASMModule *module, const char *name, char *error_buf,
  4222. uint32_t error_buf_size)
  4223. {
  4224. if (!name)
  4225. return false;
  4226. module->name =
  4227. wasm_const_str_list_insert((const uint8 *)name, (uint32)strlen(name),
  4228. module, false, error_buf, error_buf_size);
  4229. return module->name != NULL;
  4230. }
  4231. const char *
  4232. wasm_get_module_name(WASMModule *module)
  4233. {
  4234. return module->name;
  4235. }