wasm_runtime.c 156 KB

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