wasm_runtime_common.c 152 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_platform.h"
  6. #include "bh_common.h"
  7. #include "bh_assert.h"
  8. #include "bh_log.h"
  9. #include "wasm_runtime_common.h"
  10. #include "wasm_memory.h"
  11. #if WASM_ENABLE_INTERP != 0
  12. #include "../interpreter/wasm_runtime.h"
  13. #endif
  14. #if WASM_ENABLE_AOT != 0
  15. #include "../aot/aot_runtime.h"
  16. #if WASM_ENABLE_DEBUG_AOT != 0
  17. #include "../aot/debug/jit_debug.h"
  18. #endif
  19. #endif
  20. #if WASM_ENABLE_THREAD_MGR != 0
  21. #include "../libraries/thread-mgr/thread_manager.h"
  22. #if WASM_ENABLE_DEBUG_INTERP != 0
  23. #include "../libraries/debug-engine/debug_engine.h"
  24. #endif
  25. #endif
  26. #if WASM_ENABLE_SHARED_MEMORY != 0
  27. #include "wasm_shared_memory.h"
  28. #endif
  29. #include "../common/wasm_c_api_internal.h"
  30. /**
  31. * For runtime build, BH_MALLOC/BH_FREE should be defined as
  32. * wasm_runtime_malloc/wasm_runtime_free.
  33. */
  34. #define CHECK(a) CHECK1(a)
  35. #define CHECK1(a) SHOULD_BE_##a
  36. #define SHOULD_BE_wasm_runtime_malloc 1
  37. #if !CHECK(BH_MALLOC)
  38. #error unexpected BH_MALLOC
  39. #endif
  40. #undef SHOULD_BE_wasm_runtime_malloc
  41. #define SHOULD_BE_wasm_runtime_free 1
  42. #if !CHECK(BH_FREE)
  43. #error unexpected BH_FREE
  44. #endif
  45. #undef SHOULD_BE_wasm_runtime_free
  46. #undef CHECK
  47. #undef CHECK1
  48. #if WASM_ENABLE_MULTI_MODULE != 0
  49. /**
  50. * A safety insurance to prevent
  51. * circular depencies which leads stack overflow
  52. * try to break early
  53. */
  54. typedef struct LoadingModule {
  55. bh_list_link l;
  56. /* point to a string pool */
  57. const char *module_name;
  58. } LoadingModule;
  59. static bh_list loading_module_list_head;
  60. static bh_list *const loading_module_list = &loading_module_list_head;
  61. static korp_mutex loading_module_list_lock;
  62. /**
  63. * A list to store all exported functions/globals/memories/tables
  64. * of every fully loaded module
  65. */
  66. static bh_list registered_module_list_head;
  67. static bh_list *const registered_module_list = &registered_module_list_head;
  68. static korp_mutex registered_module_list_lock;
  69. static void
  70. wasm_runtime_destroy_registered_module_list();
  71. #endif /* WASM_ENABLE_MULTI_MODULE */
  72. #define E_TYPE_XIP 4
  73. #if WASM_ENABLE_REF_TYPES != 0
  74. /* Initialize externref hashmap */
  75. static bool
  76. wasm_externref_map_init();
  77. /* Destroy externref hashmap */
  78. static void
  79. wasm_externref_map_destroy();
  80. #endif /* WASM_ENABLE_REF_TYPES */
  81. static void
  82. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  83. {
  84. if (error_buf != NULL)
  85. snprintf(error_buf, error_buf_size, "%s", string);
  86. }
  87. static void *
  88. runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst,
  89. char *error_buf, uint32 error_buf_size)
  90. {
  91. void *mem;
  92. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  93. if (module_inst != NULL) {
  94. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  95. }
  96. else if (error_buf != NULL) {
  97. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  98. }
  99. return NULL;
  100. }
  101. memset(mem, 0, (uint32)size);
  102. return mem;
  103. }
  104. static bool
  105. wasm_runtime_env_init()
  106. {
  107. if (bh_platform_init() != 0)
  108. return false;
  109. if (wasm_native_init() == false) {
  110. goto fail1;
  111. }
  112. #if WASM_ENABLE_MULTI_MODULE
  113. if (BHT_OK != os_mutex_init(&registered_module_list_lock)) {
  114. goto fail2;
  115. }
  116. if (BHT_OK != os_mutex_init(&loading_module_list_lock)) {
  117. goto fail3;
  118. }
  119. #endif
  120. #if WASM_ENABLE_SHARED_MEMORY
  121. if (!wasm_shared_memory_init()) {
  122. goto fail4;
  123. }
  124. #endif
  125. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  126. if (!thread_manager_init()) {
  127. goto fail5;
  128. }
  129. #endif
  130. #if WASM_ENABLE_AOT != 0
  131. #ifdef OS_ENABLE_HW_BOUND_CHECK
  132. if (!aot_signal_init()) {
  133. goto fail6;
  134. }
  135. #endif
  136. #if WASM_ENABLE_DEBUG_AOT != 0
  137. if (!jit_debug_engine_init()) {
  138. goto fail7;
  139. }
  140. #endif
  141. #endif
  142. #if WASM_ENABLE_REF_TYPES != 0
  143. if (!wasm_externref_map_init()) {
  144. goto fail8;
  145. }
  146. #endif
  147. return true;
  148. #if WASM_ENABLE_REF_TYPES != 0
  149. fail8:
  150. #endif
  151. #if WASM_ENABLE_AOT != 0
  152. #if WASM_ENABLE_DEBUG_AOT != 0
  153. jit_debug_engine_destroy();
  154. fail7:
  155. #endif
  156. #ifdef OS_ENABLE_HW_BOUND_CHECK
  157. aot_signal_destroy();
  158. fail6:
  159. #endif
  160. #endif
  161. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  162. thread_manager_destroy();
  163. fail5:
  164. #endif
  165. #if WASM_ENABLE_SHARED_MEMORY
  166. wasm_shared_memory_destroy();
  167. fail4:
  168. #endif
  169. #if WASM_ENABLE_MULTI_MODULE
  170. os_mutex_destroy(&loading_module_list_lock);
  171. fail3:
  172. os_mutex_destroy(&registered_module_list_lock);
  173. fail2:
  174. #endif
  175. wasm_native_destroy();
  176. fail1:
  177. bh_platform_destroy();
  178. return false;
  179. }
  180. static bool
  181. wasm_runtime_exec_env_check(WASMExecEnv *exec_env)
  182. {
  183. return exec_env && exec_env->module_inst && exec_env->wasm_stack_size > 0
  184. && exec_env->wasm_stack.s.top_boundary
  185. == exec_env->wasm_stack.s.bottom + exec_env->wasm_stack_size
  186. && exec_env->wasm_stack.s.top <= exec_env->wasm_stack.s.top_boundary;
  187. }
  188. bool
  189. wasm_runtime_init()
  190. {
  191. if (!wasm_runtime_memory_init(Alloc_With_System_Allocator, NULL))
  192. return false;
  193. if (!wasm_runtime_env_init()) {
  194. wasm_runtime_memory_destroy();
  195. return false;
  196. }
  197. return true;
  198. }
  199. void
  200. wasm_runtime_destroy()
  201. {
  202. #if WASM_ENABLE_REF_TYPES != 0
  203. wasm_externref_map_destroy();
  204. #endif
  205. #if WASM_ENABLE_AOT != 0
  206. #if WASM_ENABLE_DEBUG_AOT != 0
  207. jit_debug_engine_destroy();
  208. #endif
  209. #ifdef OS_ENABLE_HW_BOUND_CHECK
  210. aot_signal_destroy();
  211. #endif
  212. #endif
  213. /* runtime env destroy */
  214. #if WASM_ENABLE_MULTI_MODULE
  215. wasm_runtime_destroy_loading_module_list();
  216. os_mutex_destroy(&loading_module_list_lock);
  217. wasm_runtime_destroy_registered_module_list();
  218. os_mutex_destroy(&registered_module_list_lock);
  219. #endif
  220. #if WASM_ENABLE_SHARED_MEMORY
  221. wasm_shared_memory_destroy();
  222. #endif
  223. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  224. #if WASM_ENABLE_DEBUG_INTERP != 0
  225. wasm_debug_engine_destroy();
  226. #endif
  227. thread_manager_destroy();
  228. #endif
  229. wasm_native_destroy();
  230. bh_platform_destroy();
  231. wasm_runtime_memory_destroy();
  232. }
  233. bool
  234. wasm_runtime_full_init(RuntimeInitArgs *init_args)
  235. {
  236. if (!wasm_runtime_memory_init(init_args->mem_alloc_type,
  237. &init_args->mem_alloc_option))
  238. return false;
  239. if (!wasm_runtime_env_init()) {
  240. wasm_runtime_memory_destroy();
  241. return false;
  242. }
  243. #if WASM_ENABLE_DEBUG_INTERP != 0
  244. if (strlen(init_args->ip_addr))
  245. if (!wasm_debug_engine_init(init_args->ip_addr,
  246. init_args->platform_port,
  247. init_args->instance_port)) {
  248. wasm_runtime_destroy();
  249. return false;
  250. }
  251. #endif
  252. if (init_args->n_native_symbols > 0
  253. && !wasm_runtime_register_natives(init_args->native_module_name,
  254. init_args->native_symbols,
  255. init_args->n_native_symbols)) {
  256. wasm_runtime_destroy();
  257. return false;
  258. }
  259. #if WASM_ENABLE_THREAD_MGR != 0
  260. wasm_cluster_set_max_thread_num(init_args->max_thread_num);
  261. #endif
  262. return true;
  263. }
  264. PackageType
  265. get_package_type(const uint8 *buf, uint32 size)
  266. {
  267. if (buf && size >= 4) {
  268. if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 's' && buf[3] == 'm')
  269. return Wasm_Module_Bytecode;
  270. if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 'o' && buf[3] == 't')
  271. return Wasm_Module_AoT;
  272. }
  273. return Package_Type_Unknown;
  274. }
  275. #if WASM_ENABLE_AOT != 0
  276. static uint8 *
  277. align_ptr(const uint8 *p, uint32 b)
  278. {
  279. uintptr_t v = (uintptr_t)p;
  280. uintptr_t m = b - 1;
  281. return (uint8 *)((v + m) & ~m);
  282. }
  283. #define CHECK_BUF(buf, buf_end, length) \
  284. do { \
  285. if ((uintptr_t)buf + length < (uintptr_t)buf \
  286. || (uintptr_t)buf + length > (uintptr_t)buf_end) \
  287. return false; \
  288. } while (0)
  289. #define read_uint16(p, p_end, res) \
  290. do { \
  291. p = (uint8 *)align_ptr(p, sizeof(uint16)); \
  292. CHECK_BUF(p, p_end, sizeof(uint16)); \
  293. res = *(uint16 *)p; \
  294. p += sizeof(uint16); \
  295. } while (0)
  296. #define read_uint32(p, p_end, res) \
  297. do { \
  298. p = (uint8 *)align_ptr(p, sizeof(uint32)); \
  299. CHECK_BUF(p, p_end, sizeof(uint32)); \
  300. res = *(uint32 *)p; \
  301. p += sizeof(uint32); \
  302. } while (0)
  303. bool
  304. wasm_runtime_is_xip_file(const uint8 *buf, uint32 size)
  305. {
  306. const uint8 *p = buf, *p_end = buf + size;
  307. uint32 section_type, section_size;
  308. uint16 e_type;
  309. if (get_package_type(buf, size) != Wasm_Module_AoT)
  310. return false;
  311. CHECK_BUF(p, p_end, 8);
  312. p += 8;
  313. while (p < p_end) {
  314. read_uint32(p, p_end, section_type);
  315. read_uint32(p, p_end, section_size);
  316. CHECK_BUF(p, p_end, section_size);
  317. if (section_type == AOT_SECTION_TYPE_TARGET_INFO) {
  318. p += 4;
  319. read_uint16(p, p_end, e_type);
  320. return (e_type == E_TYPE_XIP) ? true : false;
  321. }
  322. else if (section_type >= AOT_SECTION_TYPE_SIGANATURE) {
  323. return false;
  324. }
  325. p += section_size;
  326. }
  327. return false;
  328. }
  329. #endif /* end of WASM_ENABLE_AOT */
  330. #if (WASM_ENABLE_THREAD_MGR != 0) && (WASM_ENABLE_DEBUG_INTERP != 0)
  331. uint32
  332. wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
  333. {
  334. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  335. bh_assert(cluster);
  336. if (cluster->debug_inst) {
  337. LOG_WARNING("Cluster already bind to a debug instance");
  338. return cluster->debug_inst->control_thread->port;
  339. }
  340. if (wasm_debug_instance_create(cluster)) {
  341. return cluster->debug_inst->control_thread->port;
  342. }
  343. return 0;
  344. }
  345. #endif
  346. #if WASM_ENABLE_MULTI_MODULE != 0
  347. static module_reader reader;
  348. static module_destroyer destroyer;
  349. void
  350. wasm_runtime_set_module_reader(const module_reader reader_cb,
  351. const module_destroyer destroyer_cb)
  352. {
  353. reader = reader_cb;
  354. destroyer = destroyer_cb;
  355. }
  356. module_reader
  357. wasm_runtime_get_module_reader()
  358. {
  359. return reader;
  360. }
  361. module_destroyer
  362. wasm_runtime_get_module_destroyer()
  363. {
  364. return destroyer;
  365. }
  366. static WASMRegisteredModule *
  367. wasm_runtime_find_module_registered_by_reference(WASMModuleCommon *module)
  368. {
  369. WASMRegisteredModule *reg_module = NULL;
  370. os_mutex_lock(&registered_module_list_lock);
  371. reg_module = bh_list_first_elem(registered_module_list);
  372. while (reg_module && module != reg_module->module) {
  373. reg_module = bh_list_elem_next(reg_module);
  374. }
  375. os_mutex_unlock(&registered_module_list_lock);
  376. return reg_module;
  377. }
  378. bool
  379. wasm_runtime_register_module_internal(const char *module_name,
  380. WASMModuleCommon *module,
  381. uint8 *orig_file_buf,
  382. uint32 orig_file_buf_size,
  383. char *error_buf, uint32 error_buf_size)
  384. {
  385. WASMRegisteredModule *node = NULL;
  386. node = wasm_runtime_find_module_registered_by_reference(module);
  387. if (node) { /* module has been registered */
  388. if (node->module_name) { /* module has name */
  389. if (!module_name || strcmp(node->module_name, module_name)) {
  390. /* module has different name */
  391. LOG_DEBUG("module(%p) has been registered with name %s", module,
  392. node->module_name);
  393. set_error_buf(error_buf, error_buf_size,
  394. "Register module failed: "
  395. "failed to rename the module");
  396. return false;
  397. }
  398. else {
  399. /* module has the same name */
  400. LOG_DEBUG(
  401. "module(%p) has been registered with the same name %s",
  402. module, node->module_name);
  403. return true;
  404. }
  405. }
  406. else {
  407. /* module has empyt name, reset it */
  408. node->module_name = module_name;
  409. return true;
  410. }
  411. }
  412. /* module hasn't been registered */
  413. node = runtime_malloc(sizeof(WASMRegisteredModule), NULL, NULL, 0);
  414. if (!node) {
  415. LOG_DEBUG("malloc WASMRegisteredModule failed. SZ=%d",
  416. sizeof(WASMRegisteredModule));
  417. return false;
  418. }
  419. /* share the string and the module */
  420. node->module_name = module_name;
  421. node->module = module;
  422. node->orig_file_buf = orig_file_buf;
  423. node->orig_file_buf_size = orig_file_buf_size;
  424. os_mutex_lock(&registered_module_list_lock);
  425. bh_list_status ret = bh_list_insert(registered_module_list, node);
  426. bh_assert(BH_LIST_SUCCESS == ret);
  427. (void)ret;
  428. os_mutex_unlock(&registered_module_list_lock);
  429. return true;
  430. }
  431. bool
  432. wasm_runtime_register_module(const char *module_name, WASMModuleCommon *module,
  433. char *error_buf, uint32 error_buf_size)
  434. {
  435. if (!error_buf || !error_buf_size) {
  436. LOG_ERROR("error buffer is required");
  437. return false;
  438. }
  439. if (!module_name || !module) {
  440. LOG_DEBUG("module_name and module are required");
  441. set_error_buf(error_buf, error_buf_size,
  442. "Register module failed: "
  443. "module_name and module are required");
  444. return false;
  445. }
  446. if (wasm_runtime_is_built_in_module(module_name)) {
  447. LOG_DEBUG("%s is a built-in module name", module_name);
  448. set_error_buf(error_buf, error_buf_size,
  449. "Register module failed: "
  450. "can not register as a built-in module");
  451. return false;
  452. }
  453. return wasm_runtime_register_module_internal(module_name, module, NULL, 0,
  454. error_buf, error_buf_size);
  455. }
  456. void
  457. wasm_runtime_unregister_module(const WASMModuleCommon *module)
  458. {
  459. WASMRegisteredModule *registered_module = NULL;
  460. os_mutex_lock(&registered_module_list_lock);
  461. registered_module = bh_list_first_elem(registered_module_list);
  462. while (registered_module && module != registered_module->module) {
  463. registered_module = bh_list_elem_next(registered_module);
  464. }
  465. /* it does not matter if it is not exist. after all, it is gone */
  466. if (registered_module) {
  467. bh_list_remove(registered_module_list, registered_module);
  468. wasm_runtime_free(registered_module);
  469. }
  470. os_mutex_unlock(&registered_module_list_lock);
  471. }
  472. WASMModuleCommon *
  473. wasm_runtime_find_module_registered(const char *module_name)
  474. {
  475. WASMRegisteredModule *module = NULL, *module_next;
  476. os_mutex_lock(&registered_module_list_lock);
  477. module = bh_list_first_elem(registered_module_list);
  478. while (module) {
  479. module_next = bh_list_elem_next(module);
  480. if (module->module_name && !strcmp(module_name, module->module_name)) {
  481. break;
  482. }
  483. module = module_next;
  484. }
  485. os_mutex_unlock(&registered_module_list_lock);
  486. return module ? module->module : NULL;
  487. }
  488. bool
  489. wasm_runtime_is_module_registered(const char *module_name)
  490. {
  491. return NULL != wasm_runtime_find_module_registered(module_name);
  492. }
  493. /*
  494. * simply destroy all
  495. */
  496. static void
  497. wasm_runtime_destroy_registered_module_list()
  498. {
  499. WASMRegisteredModule *reg_module = NULL;
  500. os_mutex_lock(&registered_module_list_lock);
  501. reg_module = bh_list_first_elem(registered_module_list);
  502. while (reg_module) {
  503. WASMRegisteredModule *next_reg_module = bh_list_elem_next(reg_module);
  504. bh_list_remove(registered_module_list, reg_module);
  505. /* now, it is time to release every module in the runtime */
  506. if (reg_module->module->module_type == Wasm_Module_Bytecode) {
  507. #if WASM_ENABLE_INTERP != 0
  508. wasm_unload((WASMModule *)reg_module->module);
  509. #endif
  510. }
  511. else {
  512. #if WASM_ENABLE_AOT != 0
  513. aot_unload((AOTModule *)reg_module->module);
  514. #endif
  515. }
  516. /* destroy the file buffer */
  517. if (destroyer && reg_module->orig_file_buf) {
  518. destroyer(reg_module->orig_file_buf,
  519. reg_module->orig_file_buf_size);
  520. reg_module->orig_file_buf = NULL;
  521. reg_module->orig_file_buf_size = 0;
  522. }
  523. wasm_runtime_free(reg_module);
  524. reg_module = next_reg_module;
  525. }
  526. os_mutex_unlock(&registered_module_list_lock);
  527. }
  528. bool
  529. wasm_runtime_add_loading_module(const char *module_name, char *error_buf,
  530. uint32 error_buf_size)
  531. {
  532. LOG_DEBUG("add %s into a loading list", module_name);
  533. LoadingModule *loadingModule =
  534. runtime_malloc(sizeof(LoadingModule), NULL, error_buf, error_buf_size);
  535. if (!loadingModule) {
  536. return false;
  537. }
  538. /* share the incoming string */
  539. loadingModule->module_name = module_name;
  540. os_mutex_lock(&loading_module_list_lock);
  541. bh_list_status ret = bh_list_insert(loading_module_list, loadingModule);
  542. bh_assert(BH_LIST_SUCCESS == ret);
  543. (void)ret;
  544. os_mutex_unlock(&loading_module_list_lock);
  545. return true;
  546. }
  547. void
  548. wasm_runtime_delete_loading_module(const char *module_name)
  549. {
  550. LOG_DEBUG("delete %s from a loading list", module_name);
  551. LoadingModule *module = NULL;
  552. os_mutex_lock(&loading_module_list_lock);
  553. module = bh_list_first_elem(loading_module_list);
  554. while (module && strcmp(module->module_name, module_name)) {
  555. module = bh_list_elem_next(module);
  556. }
  557. /* it does not matter if it is not exist. after all, it is gone */
  558. if (module) {
  559. bh_list_remove(loading_module_list, module);
  560. wasm_runtime_free(module);
  561. }
  562. os_mutex_unlock(&loading_module_list_lock);
  563. }
  564. bool
  565. wasm_runtime_is_loading_module(const char *module_name)
  566. {
  567. LOG_DEBUG("find %s in a loading list", module_name);
  568. LoadingModule *module = NULL;
  569. os_mutex_lock(&loading_module_list_lock);
  570. module = bh_list_first_elem(loading_module_list);
  571. while (module && strcmp(module_name, module->module_name)) {
  572. module = bh_list_elem_next(module);
  573. }
  574. os_mutex_unlock(&loading_module_list_lock);
  575. return module != NULL;
  576. }
  577. void
  578. wasm_runtime_destroy_loading_module_list()
  579. {
  580. LoadingModule *module = NULL;
  581. os_mutex_lock(&loading_module_list_lock);
  582. module = bh_list_first_elem(loading_module_list);
  583. while (module) {
  584. LoadingModule *next_module = bh_list_elem_next(module);
  585. bh_list_remove(loading_module_list, module);
  586. /*
  587. * will not free the module_name since it is
  588. * shared one of the const string pool
  589. */
  590. wasm_runtime_free(module);
  591. module = next_module;
  592. }
  593. os_mutex_unlock(&loading_module_list_lock);
  594. }
  595. #endif /* WASM_ENABLE_MULTI_MODULE */
  596. bool
  597. wasm_runtime_is_built_in_module(const char *module_name)
  598. {
  599. return (!strcmp("env", module_name) || !strcmp("wasi_unstable", module_name)
  600. || !strcmp("wasi_snapshot_preview1", module_name)
  601. #if WASM_ENABLE_SPEC_TEST != 0
  602. || !strcmp("spectest", module_name)
  603. #endif
  604. || !strcmp("", module_name));
  605. }
  606. #if WASM_ENABLE_THREAD_MGR != 0
  607. bool
  608. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset,
  609. uint32 size)
  610. {
  611. WASMModuleInstanceCommon *module_inst =
  612. wasm_exec_env_get_module_inst(exec_env);
  613. #if WASM_ENABLE_INTERP != 0
  614. if (module_inst->module_type == Wasm_Module_Bytecode) {
  615. return wasm_set_aux_stack(exec_env, start_offset, size);
  616. }
  617. #endif
  618. #if WASM_ENABLE_AOT != 0
  619. if (module_inst->module_type == Wasm_Module_AoT) {
  620. return aot_set_aux_stack(exec_env, start_offset, size);
  621. }
  622. #endif
  623. return false;
  624. }
  625. bool
  626. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset,
  627. uint32 *size)
  628. {
  629. WASMModuleInstanceCommon *module_inst =
  630. wasm_exec_env_get_module_inst(exec_env);
  631. #if WASM_ENABLE_INTERP != 0
  632. if (module_inst->module_type == Wasm_Module_Bytecode) {
  633. return wasm_get_aux_stack(exec_env, start_offset, size);
  634. }
  635. #endif
  636. #if WASM_ENABLE_AOT != 0
  637. if (module_inst->module_type == Wasm_Module_AoT) {
  638. return aot_get_aux_stack(exec_env, start_offset, size);
  639. }
  640. #endif
  641. return false;
  642. }
  643. void
  644. wasm_runtime_set_max_thread_num(uint32 num)
  645. {
  646. wasm_cluster_set_max_thread_num(num);
  647. }
  648. #endif /* end of WASM_ENABLE_THREAD_MGR */
  649. static WASMModuleCommon *
  650. register_module_with_null_name(WASMModuleCommon *module_common, char *error_buf,
  651. uint32 error_buf_size)
  652. {
  653. #if WASM_ENABLE_MULTI_MODULE != 0
  654. if (module_common) {
  655. if (!wasm_runtime_register_module_internal(NULL, module_common, NULL, 0,
  656. error_buf, error_buf_size)) {
  657. wasm_runtime_unload(module_common);
  658. return NULL;
  659. }
  660. return module_common;
  661. }
  662. else
  663. return NULL;
  664. #else
  665. return module_common;
  666. #endif
  667. }
  668. WASMModuleCommon *
  669. wasm_runtime_load(uint8 *buf, uint32 size, char *error_buf,
  670. uint32 error_buf_size)
  671. {
  672. WASMModuleCommon *module_common = NULL;
  673. if (get_package_type(buf, size) == Wasm_Module_Bytecode) {
  674. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0
  675. AOTModule *aot_module;
  676. WASMModule *module = wasm_load(buf, size, error_buf, error_buf_size);
  677. if (!module)
  678. return NULL;
  679. if (!(aot_module =
  680. aot_convert_wasm_module(module, error_buf, error_buf_size))) {
  681. wasm_unload(module);
  682. return NULL;
  683. }
  684. module_common = (WASMModuleCommon *)aot_module;
  685. return register_module_with_null_name(module_common, error_buf,
  686. error_buf_size);
  687. #elif WASM_ENABLE_INTERP != 0
  688. module_common =
  689. (WASMModuleCommon *)wasm_load(buf, size, error_buf, error_buf_size);
  690. return register_module_with_null_name(module_common, error_buf,
  691. error_buf_size);
  692. #endif
  693. }
  694. else if (get_package_type(buf, size) == Wasm_Module_AoT) {
  695. #if WASM_ENABLE_AOT != 0
  696. module_common = (WASMModuleCommon *)aot_load_from_aot_file(
  697. buf, size, error_buf, error_buf_size);
  698. return register_module_with_null_name(module_common, error_buf,
  699. error_buf_size);
  700. #endif
  701. }
  702. if (size < 4)
  703. set_error_buf(error_buf, error_buf_size,
  704. "WASM module load failed: unexpected end");
  705. else
  706. set_error_buf(error_buf, error_buf_size,
  707. "WASM module load failed: magic header not detected");
  708. return NULL;
  709. }
  710. WASMModuleCommon *
  711. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  712. char *error_buf, uint32 error_buf_size)
  713. {
  714. WASMModuleCommon *module_common;
  715. if (!is_aot) {
  716. #if WASM_ENABLE_INTERP != 0
  717. module_common = (WASMModuleCommon *)wasm_load_from_sections(
  718. section_list, error_buf, error_buf_size);
  719. return register_module_with_null_name(module_common, error_buf,
  720. error_buf_size);
  721. #endif
  722. }
  723. else {
  724. #if WASM_ENABLE_AOT != 0
  725. module_common = (WASMModuleCommon *)aot_load_from_sections(
  726. section_list, error_buf, error_buf_size);
  727. return register_module_with_null_name(module_common, error_buf,
  728. error_buf_size);
  729. #endif
  730. }
  731. #if WASM_ENABLE_INTERP == 0 || WASM_ENABLE_AOT == 0
  732. set_error_buf(error_buf, error_buf_size,
  733. "WASM module load failed: invalid section list type");
  734. return NULL;
  735. #endif
  736. }
  737. void
  738. wasm_runtime_unload(WASMModuleCommon *module)
  739. {
  740. #if WASM_ENABLE_MULTI_MODULE != 0
  741. /**
  742. * since we will unload and free all module when runtime_destroy()
  743. * we don't want users to unwillingly disrupt it
  744. */
  745. return;
  746. #endif
  747. #if WASM_ENABLE_INTERP != 0
  748. if (module->module_type == Wasm_Module_Bytecode) {
  749. wasm_unload((WASMModule *)module);
  750. return;
  751. }
  752. #endif
  753. #if WASM_ENABLE_AOT != 0
  754. if (module->module_type == Wasm_Module_AoT) {
  755. aot_unload((AOTModule *)module);
  756. return;
  757. }
  758. #endif
  759. }
  760. WASMModuleInstanceCommon *
  761. wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst,
  762. uint32 stack_size, uint32 heap_size,
  763. char *error_buf, uint32 error_buf_size)
  764. {
  765. #if WASM_ENABLE_INTERP != 0
  766. if (module->module_type == Wasm_Module_Bytecode)
  767. return (WASMModuleInstanceCommon *)wasm_instantiate(
  768. (WASMModule *)module, is_sub_inst, stack_size, heap_size, error_buf,
  769. error_buf_size);
  770. #endif
  771. #if WASM_ENABLE_AOT != 0
  772. if (module->module_type == Wasm_Module_AoT)
  773. return (WASMModuleInstanceCommon *)aot_instantiate(
  774. (AOTModule *)module, is_sub_inst, stack_size, heap_size, error_buf,
  775. error_buf_size);
  776. #endif
  777. set_error_buf(error_buf, error_buf_size,
  778. "Instantiate module failed, invalid module type");
  779. return NULL;
  780. }
  781. WASMModuleInstanceCommon *
  782. wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size,
  783. uint32 heap_size, char *error_buf,
  784. uint32 error_buf_size)
  785. {
  786. return wasm_runtime_instantiate_internal(
  787. module, false, stack_size, heap_size, error_buf, error_buf_size);
  788. }
  789. void
  790. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  791. bool is_sub_inst)
  792. {
  793. #if WASM_ENABLE_INTERP != 0
  794. if (module_inst->module_type == Wasm_Module_Bytecode) {
  795. wasm_deinstantiate((WASMModuleInstance *)module_inst, is_sub_inst);
  796. return;
  797. }
  798. #endif
  799. #if WASM_ENABLE_AOT != 0
  800. if (module_inst->module_type == Wasm_Module_AoT) {
  801. aot_deinstantiate((AOTModuleInstance *)module_inst, is_sub_inst);
  802. return;
  803. }
  804. #endif
  805. }
  806. void
  807. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst)
  808. {
  809. wasm_runtime_deinstantiate_internal(module_inst, false);
  810. }
  811. WASMExecEnv *
  812. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  813. uint32 stack_size)
  814. {
  815. return wasm_exec_env_create(module_inst, stack_size);
  816. }
  817. void
  818. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env)
  819. {
  820. wasm_exec_env_destroy(exec_env);
  821. }
  822. bool
  823. wasm_runtime_init_thread_env(void)
  824. {
  825. #ifdef BH_PLATFORM_WINDOWS
  826. if (os_thread_env_init() != 0)
  827. return false;
  828. #endif
  829. #if WASM_ENABLE_AOT != 0
  830. #ifdef OS_ENABLE_HW_BOUND_CHECK
  831. if (!aot_signal_init()) {
  832. #ifdef BH_PLATFORM_WINDOWS
  833. os_thread_env_destroy();
  834. #endif
  835. return false;
  836. }
  837. #endif
  838. #endif
  839. return true;
  840. }
  841. void
  842. wasm_runtime_destroy_thread_env(void)
  843. {
  844. #if WASM_ENABLE_AOT != 0
  845. #ifdef OS_ENABLE_HW_BOUND_CHECK
  846. aot_signal_destroy();
  847. #endif
  848. #endif
  849. #ifdef BH_PLATFORM_WINDOWS
  850. os_thread_env_destroy();
  851. #endif
  852. }
  853. bool
  854. wasm_runtime_thread_env_inited(void)
  855. {
  856. #ifdef BH_PLATFORM_WINDOWS
  857. if (!os_thread_env_inited())
  858. return false;
  859. #endif
  860. #if WASM_ENABLE_AOT != 0
  861. #ifdef OS_ENABLE_HW_BOUND_CHECK
  862. if (!os_thread_signal_inited())
  863. return false;
  864. #endif
  865. #endif
  866. return true;
  867. }
  868. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  869. void
  870. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module)
  871. {
  872. WASMModuleMemConsumption mem_conspn = { 0 };
  873. #if WASM_ENABLE_INTERP != 0
  874. if (module->module_type == Wasm_Module_Bytecode) {
  875. wasm_get_module_mem_consumption((WASMModule *)module, &mem_conspn);
  876. }
  877. #endif
  878. #if WASM_ENABLE_AOT != 0
  879. if (module->module_type == Wasm_Module_AoT) {
  880. aot_get_module_mem_consumption((AOTModule *)module, &mem_conspn);
  881. }
  882. #endif
  883. os_printf("WASM module memory consumption, total size: %u\n",
  884. mem_conspn.total_size);
  885. os_printf(" module struct size: %u\n", mem_conspn.module_struct_size);
  886. os_printf(" types size: %u\n", mem_conspn.types_size);
  887. os_printf(" imports size: %u\n", mem_conspn.imports_size);
  888. os_printf(" funcs size: %u\n", mem_conspn.functions_size);
  889. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  890. os_printf(" memories size: %u\n", mem_conspn.memories_size);
  891. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  892. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  893. os_printf(" table segs size: %u\n", mem_conspn.table_segs_size);
  894. os_printf(" data segs size: %u\n", mem_conspn.data_segs_size);
  895. os_printf(" const strings size: %u\n", mem_conspn.const_strs_size);
  896. #if WASM_ENABLE_AOT != 0
  897. os_printf(" aot code size: %u\n", mem_conspn.aot_code_size);
  898. #endif
  899. }
  900. void
  901. wasm_runtime_dump_module_inst_mem_consumption(
  902. const WASMModuleInstanceCommon *module_inst)
  903. {
  904. WASMModuleInstMemConsumption mem_conspn = { 0 };
  905. #if WASM_ENABLE_INTERP != 0
  906. if (module_inst->module_type == Wasm_Module_Bytecode) {
  907. wasm_get_module_inst_mem_consumption((WASMModuleInstance *)module_inst,
  908. &mem_conspn);
  909. }
  910. #endif
  911. #if WASM_ENABLE_AOT != 0
  912. if (module_inst->module_type == Wasm_Module_AoT) {
  913. aot_get_module_inst_mem_consumption((AOTModuleInstance *)module_inst,
  914. &mem_conspn);
  915. }
  916. #endif
  917. os_printf("WASM module inst memory consumption, total size: %u\n",
  918. mem_conspn.total_size);
  919. os_printf(" module inst struct size: %u\n",
  920. mem_conspn.module_inst_struct_size);
  921. os_printf(" memories size: %u\n", mem_conspn.memories_size);
  922. os_printf(" app heap size: %u\n", mem_conspn.app_heap_size);
  923. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  924. os_printf(" functions size: %u\n", mem_conspn.functions_size);
  925. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  926. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  927. }
  928. void
  929. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env)
  930. {
  931. uint32 total_size =
  932. offsetof(WASMExecEnv, wasm_stack.s.bottom) + exec_env->wasm_stack_size;
  933. os_printf("Exec env memory consumption, total size: %u\n", total_size);
  934. os_printf(" exec env struct size: %u\n",
  935. offsetof(WASMExecEnv, wasm_stack.s.bottom));
  936. #if WASM_ENABLE_INTERP != 0 && WASM_ENABLE_FAST_INTERP == 0
  937. os_printf(" block addr cache size: %u\n",
  938. sizeof(exec_env->block_addr_cache));
  939. #endif
  940. os_printf(" stack size: %u\n", exec_env->wasm_stack_size);
  941. }
  942. uint32
  943. gc_get_heap_highmark_size(void *heap);
  944. void
  945. wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env)
  946. {
  947. WASMModuleInstMemConsumption module_inst_mem_consps;
  948. WASMModuleMemConsumption module_mem_consps;
  949. WASMModuleInstanceCommon *module_inst_common;
  950. WASMModuleCommon *module_common = NULL;
  951. void *heap_handle = NULL;
  952. uint32 total_size = 0, app_heap_peak_size = 0;
  953. uint32 max_aux_stack_used = -1;
  954. module_inst_common = exec_env->module_inst;
  955. #if WASM_ENABLE_INTERP != 0
  956. if (module_inst_common->module_type == Wasm_Module_Bytecode) {
  957. WASMModuleInstance *wasm_module_inst =
  958. (WASMModuleInstance *)module_inst_common;
  959. WASMModule *wasm_module = wasm_module_inst->module;
  960. module_common = (WASMModuleCommon *)wasm_module;
  961. if (wasm_module_inst->memories) {
  962. heap_handle = wasm_module_inst->memories[0]->heap_handle;
  963. }
  964. wasm_get_module_inst_mem_consumption(wasm_module_inst,
  965. &module_inst_mem_consps);
  966. wasm_get_module_mem_consumption(wasm_module, &module_mem_consps);
  967. if (wasm_module_inst->module->aux_stack_top_global_index != (uint32)-1)
  968. max_aux_stack_used = wasm_module_inst->max_aux_stack_used;
  969. }
  970. #endif
  971. #if WASM_ENABLE_AOT != 0
  972. if (module_inst_common->module_type == Wasm_Module_AoT) {
  973. AOTModuleInstance *aot_module_inst =
  974. (AOTModuleInstance *)module_inst_common;
  975. AOTModule *aot_module = (AOTModule *)aot_module_inst->aot_module.ptr;
  976. module_common = (WASMModuleCommon *)aot_module;
  977. if (aot_module_inst->memories.ptr) {
  978. AOTMemoryInstance **memories =
  979. (AOTMemoryInstance **)aot_module_inst->memories.ptr;
  980. heap_handle = memories[0]->heap_handle.ptr;
  981. }
  982. aot_get_module_inst_mem_consumption(aot_module_inst,
  983. &module_inst_mem_consps);
  984. aot_get_module_mem_consumption(aot_module, &module_mem_consps);
  985. }
  986. #endif
  987. bh_assert(module_common != NULL);
  988. if (heap_handle) {
  989. app_heap_peak_size = gc_get_heap_highmark_size(heap_handle);
  990. }
  991. total_size = offsetof(WASMExecEnv, wasm_stack.s.bottom)
  992. + exec_env->wasm_stack_size + module_mem_consps.total_size
  993. + module_inst_mem_consps.total_size;
  994. os_printf("\nMemory consumption summary (bytes):\n");
  995. wasm_runtime_dump_module_mem_consumption(module_common);
  996. wasm_runtime_dump_module_inst_mem_consumption(module_inst_common);
  997. wasm_runtime_dump_exec_env_mem_consumption(exec_env);
  998. os_printf("\nTotal memory consumption of module, module inst and "
  999. "exec env: %u\n",
  1000. total_size);
  1001. os_printf("Total interpreter stack used: %u\n",
  1002. exec_env->max_wasm_stack_used);
  1003. if (max_aux_stack_used != (uint32)-1)
  1004. os_printf("Total auxiliary stack used: %u\n", max_aux_stack_used);
  1005. else
  1006. os_printf("Total aux stack used: no enough info to profile\n");
  1007. os_printf("Total app heap used: %u\n", app_heap_peak_size);
  1008. }
  1009. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
  1010. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  1011. #if WASM_ENABLE_PERF_PROFILING != 0
  1012. void
  1013. wasm_runtime_dump_perf_profiling(WASMModuleInstanceCommon *module_inst)
  1014. {
  1015. #if WASM_ENABLE_INTERP != 0
  1016. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1017. wasm_dump_perf_profiling((WASMModuleInstance *)module_inst);
  1018. }
  1019. #endif
  1020. #if WASM_ENABLE_AOT != 0
  1021. if (module_inst->module_type == Wasm_Module_AoT) {
  1022. aot_dump_perf_profiling((AOTModuleInstance *)module_inst);
  1023. }
  1024. #endif
  1025. }
  1026. #endif
  1027. WASMModuleInstanceCommon *
  1028. wasm_runtime_get_module_inst(WASMExecEnv *exec_env)
  1029. {
  1030. return wasm_exec_env_get_module_inst(exec_env);
  1031. }
  1032. void *
  1033. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env)
  1034. {
  1035. return exec_env->attachment;
  1036. }
  1037. void
  1038. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data)
  1039. {
  1040. exec_env->user_data = user_data;
  1041. }
  1042. void *
  1043. wasm_runtime_get_user_data(WASMExecEnv *exec_env)
  1044. {
  1045. return exec_env->user_data;
  1046. }
  1047. WASMType *
  1048. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  1049. uint32 module_type)
  1050. {
  1051. WASMType *type = NULL;
  1052. #if WASM_ENABLE_INTERP != 0
  1053. if (module_type == Wasm_Module_Bytecode) {
  1054. WASMFunctionInstance *wasm_func = (WASMFunctionInstance *)function;
  1055. type = wasm_func->is_import_func ? wasm_func->u.func_import->func_type
  1056. : wasm_func->u.func->func_type;
  1057. }
  1058. #endif
  1059. #if WASM_ENABLE_AOT != 0
  1060. if (module_type == Wasm_Module_AoT) {
  1061. AOTFunctionInstance *aot_func = (AOTFunctionInstance *)function;
  1062. type = aot_func->is_import_func ? aot_func->u.func_import->func_type
  1063. : aot_func->u.func.func_type;
  1064. }
  1065. #endif
  1066. return type;
  1067. }
  1068. WASMFunctionInstanceCommon *
  1069. wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
  1070. const char *name, const char *signature)
  1071. {
  1072. #if WASM_ENABLE_INTERP != 0
  1073. if (module_inst->module_type == Wasm_Module_Bytecode)
  1074. return (WASMFunctionInstanceCommon *)wasm_lookup_function(
  1075. (const WASMModuleInstance *)module_inst, name, signature);
  1076. #endif
  1077. #if WASM_ENABLE_AOT != 0
  1078. if (module_inst->module_type == Wasm_Module_AoT)
  1079. return (WASMFunctionInstanceCommon *)aot_lookup_function(
  1080. (const AOTModuleInstance *)module_inst, name, signature);
  1081. #endif
  1082. return NULL;
  1083. }
  1084. uint32
  1085. wasm_func_get_param_count(WASMFunctionInstanceCommon *const func_inst,
  1086. WASMModuleInstanceCommon *const module_inst)
  1087. {
  1088. WASMType *type =
  1089. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  1090. return type->param_count;
  1091. }
  1092. uint32
  1093. wasm_func_get_result_count(WASMFunctionInstanceCommon *const func_inst,
  1094. WASMModuleInstanceCommon *const module_inst)
  1095. {
  1096. WASMType *type =
  1097. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  1098. return type->result_count;
  1099. }
  1100. static uint8
  1101. val_type_to_val_kind(uint8 value_type)
  1102. {
  1103. switch (value_type) {
  1104. case VALUE_TYPE_I32:
  1105. return WASM_I32;
  1106. case VALUE_TYPE_I64:
  1107. return WASM_I64;
  1108. case VALUE_TYPE_F32:
  1109. return WASM_F32;
  1110. case VALUE_TYPE_F64:
  1111. return WASM_F64;
  1112. case VALUE_TYPE_FUNCREF:
  1113. return WASM_FUNCREF;
  1114. case VALUE_TYPE_EXTERNREF:
  1115. return WASM_ANYREF;
  1116. default:
  1117. #if WASM_ENABLE_GC != 0
  1118. if (wasm_is_type_reftype(value_type))
  1119. return WASM_ANYREF;
  1120. #endif
  1121. bh_assert(0);
  1122. return 0;
  1123. }
  1124. }
  1125. void
  1126. wasm_func_get_param_types(WASMFunctionInstanceCommon *const func_inst,
  1127. WASMModuleInstanceCommon *const module_inst,
  1128. wasm_valkind_t *param_types)
  1129. {
  1130. WASMType *type =
  1131. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  1132. uint32 i;
  1133. for (i = 0; i < type->param_count; i++) {
  1134. param_types[i] = val_type_to_val_kind(type->types[i]);
  1135. }
  1136. }
  1137. void
  1138. wasm_func_get_result_types(WASMFunctionInstanceCommon *const func_inst,
  1139. WASMModuleInstanceCommon *const module_inst,
  1140. wasm_valkind_t *result_types)
  1141. {
  1142. WASMType *type =
  1143. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  1144. uint32 i;
  1145. for (i = 0; i < type->result_count; i++) {
  1146. result_types[i] =
  1147. val_type_to_val_kind(type->types[type->param_count + i]);
  1148. }
  1149. }
  1150. #if WASM_ENABLE_REF_TYPES != 0
  1151. /* (uintptr_t)externref -> (uint32)index */
  1152. /* argv -> *ret_argv */
  1153. static bool
  1154. wasm_runtime_prepare_call_function(WASMExecEnv *exec_env,
  1155. WASMFunctionInstanceCommon *function,
  1156. uint32 *argv, uint32 argc, uint32 **ret_argv,
  1157. uint32 *ret_argc_param,
  1158. uint32 *ret_argc_result)
  1159. {
  1160. uint32 *new_argv = NULL, argv_i = 0, new_argv_i = 0, param_i = 0,
  1161. result_i = 0;
  1162. bool need_param_transform = false, need_result_transform = false;
  1163. uint64 size = 0;
  1164. WASMType *func_type = wasm_runtime_get_function_type(
  1165. function, exec_env->module_inst->module_type);
  1166. bh_assert(func_type);
  1167. *ret_argc_param = func_type->param_cell_num;
  1168. *ret_argc_result = func_type->ret_cell_num;
  1169. for (param_i = 0; param_i < func_type->param_count; param_i++) {
  1170. if (VALUE_TYPE_EXTERNREF == func_type->types[param_i]) {
  1171. need_param_transform = true;
  1172. }
  1173. }
  1174. for (result_i = 0; result_i < func_type->result_count; result_i++) {
  1175. if (VALUE_TYPE_EXTERNREF
  1176. == func_type->types[func_type->param_count + result_i]) {
  1177. need_result_transform = true;
  1178. }
  1179. }
  1180. if (!need_param_transform && !need_result_transform) {
  1181. *ret_argv = argv;
  1182. return true;
  1183. }
  1184. if (func_type->param_cell_num >= func_type->ret_cell_num) {
  1185. size = sizeof(uint32) * func_type->param_cell_num;
  1186. }
  1187. else {
  1188. size = sizeof(uint32) * func_type->ret_cell_num;
  1189. }
  1190. if (!(new_argv = runtime_malloc(size, exec_env->module_inst, NULL, 0))) {
  1191. return false;
  1192. }
  1193. if (!need_param_transform) {
  1194. bh_memcpy_s(new_argv, (uint32)size, argv, (uint32)size);
  1195. }
  1196. else {
  1197. for (param_i = 0; param_i < func_type->param_count && argv_i < argc
  1198. && new_argv_i < func_type->param_cell_num;
  1199. param_i++) {
  1200. uint8 param_type = func_type->types[param_i];
  1201. if (VALUE_TYPE_EXTERNREF == param_type) {
  1202. void *externref_obj;
  1203. uint32 externref_index;
  1204. #if UINTPTR_MAX == UINT32_MAX
  1205. externref_obj = (void *)argv[argv_i];
  1206. #else
  1207. union {
  1208. uintptr_t val;
  1209. uint32 parts[2];
  1210. } u;
  1211. u.parts[0] = argv[argv_i];
  1212. u.parts[1] = argv[argv_i + 1];
  1213. externref_obj = (void *)u.val;
  1214. #endif
  1215. if (!wasm_externref_obj2ref(exec_env->module_inst,
  1216. externref_obj, &externref_index)) {
  1217. wasm_runtime_free(new_argv);
  1218. return false;
  1219. }
  1220. new_argv[new_argv_i] = externref_index;
  1221. argv_i += sizeof(uintptr_t) / sizeof(uint32);
  1222. new_argv_i++;
  1223. }
  1224. else {
  1225. uint16 param_cell_num = wasm_value_type_cell_num(param_type);
  1226. uint32 param_size = sizeof(uint32) * param_cell_num;
  1227. bh_memcpy_s(new_argv + new_argv_i, param_size, argv + argv_i,
  1228. param_size);
  1229. argv_i += param_cell_num;
  1230. new_argv_i += param_cell_num;
  1231. }
  1232. }
  1233. }
  1234. *ret_argv = new_argv;
  1235. return true;
  1236. }
  1237. /* (uintptr_t)externref <- (uint32)index */
  1238. /* argv <- new_argv */
  1239. static bool
  1240. wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
  1241. WASMFunctionInstanceCommon *function,
  1242. uint32 *argv, uint32 argc, uint32 *ret_argv)
  1243. {
  1244. uint32 argv_i = 0, result_i = 0, ret_argv_i = 0;
  1245. WASMType *func_type;
  1246. bh_assert((argv && ret_argv) || (argc == 0));
  1247. if (argv == ret_argv) {
  1248. /* no need to transfrom externref results */
  1249. return true;
  1250. }
  1251. func_type = wasm_runtime_get_function_type(
  1252. function, exec_env->module_inst->module_type);
  1253. bh_assert(func_type);
  1254. for (result_i = 0; result_i < func_type->result_count && argv_i < argc;
  1255. result_i++) {
  1256. uint8 result_type = func_type->types[func_type->param_count + result_i];
  1257. if (result_type == VALUE_TYPE_EXTERNREF) {
  1258. void *externref_obj;
  1259. #if UINTPTR_MAX != UINT32_MAX
  1260. union {
  1261. uintptr_t val;
  1262. uint32 parts[2];
  1263. } u;
  1264. #endif
  1265. if (!wasm_externref_ref2obj(argv[argv_i], &externref_obj)) {
  1266. wasm_runtime_free(argv);
  1267. return false;
  1268. }
  1269. #if UINTPTR_MAX == UINT32_MAX
  1270. ret_argv[ret_argv_i] = (uintptr_t)externref_obj;
  1271. #else
  1272. u.val = (uintptr_t)externref_obj;
  1273. ret_argv[ret_argv_i] = u.parts[0];
  1274. ret_argv[ret_argv_i + 1] = u.parts[1];
  1275. #endif
  1276. argv_i += 1;
  1277. ret_argv_i += sizeof(uintptr_t) / sizeof(uint32);
  1278. }
  1279. else {
  1280. uint16 result_cell_num = wasm_value_type_cell_num(result_type);
  1281. uint32 result_size = sizeof(uint32) * result_cell_num;
  1282. bh_memcpy_s(ret_argv + ret_argv_i, result_size, argv + argv_i,
  1283. result_size);
  1284. argv_i += result_cell_num;
  1285. ret_argv_i += result_cell_num;
  1286. }
  1287. }
  1288. wasm_runtime_free(argv);
  1289. return true;
  1290. }
  1291. #endif
  1292. bool
  1293. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  1294. WASMFunctionInstanceCommon *function, uint32 argc,
  1295. uint32 argv[])
  1296. {
  1297. bool ret = false;
  1298. uint32 *new_argv = NULL, param_argc;
  1299. #if WASM_ENABLE_REF_TYPES != 0
  1300. uint32 result_argc = 0;
  1301. #endif
  1302. if (!wasm_runtime_exec_env_check(exec_env)) {
  1303. LOG_ERROR("Invalid exec env stack info.");
  1304. return false;
  1305. }
  1306. #if WASM_ENABLE_REF_TYPES != 0
  1307. if (!wasm_runtime_prepare_call_function(exec_env, function, argv, argc,
  1308. &new_argv, &param_argc,
  1309. &result_argc)) {
  1310. wasm_runtime_set_exception(exec_env->module_inst,
  1311. "the arguments conversion is failed");
  1312. return false;
  1313. }
  1314. #else
  1315. new_argv = argv;
  1316. param_argc = argc;
  1317. #endif
  1318. #if WASM_ENABLE_INTERP != 0
  1319. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  1320. ret = wasm_call_function(exec_env, (WASMFunctionInstance *)function,
  1321. param_argc, new_argv);
  1322. #endif
  1323. #if WASM_ENABLE_AOT != 0
  1324. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  1325. ret = aot_call_function(exec_env, (AOTFunctionInstance *)function,
  1326. param_argc, new_argv);
  1327. #endif
  1328. if (!ret) {
  1329. if (new_argv != argv) {
  1330. wasm_runtime_free(new_argv);
  1331. }
  1332. return false;
  1333. }
  1334. #if WASM_ENABLE_REF_TYPES != 0
  1335. if (!wasm_runtime_finalize_call_function(exec_env, function, new_argv,
  1336. result_argc, argv)) {
  1337. wasm_runtime_set_exception(exec_env->module_inst,
  1338. "the result conversion is failed");
  1339. return false;
  1340. }
  1341. #endif
  1342. return ret;
  1343. }
  1344. static void
  1345. parse_args_to_uint32_array(WASMType *type, wasm_val_t *args, uint32 *out_argv)
  1346. {
  1347. uint32 i, p;
  1348. for (i = 0, p = 0; i < type->param_count; i++) {
  1349. switch (args[i].kind) {
  1350. case WASM_I32:
  1351. out_argv[p++] = args[i].of.i32;
  1352. break;
  1353. case WASM_I64:
  1354. {
  1355. union {
  1356. uint64 val;
  1357. uint32 parts[2];
  1358. } u;
  1359. u.val = args[i].of.i64;
  1360. out_argv[p++] = u.parts[0];
  1361. out_argv[p++] = u.parts[1];
  1362. break;
  1363. }
  1364. case WASM_F32:
  1365. {
  1366. union {
  1367. float32 val;
  1368. uint32 part;
  1369. } u;
  1370. u.val = args[i].of.f32;
  1371. out_argv[p++] = u.part;
  1372. break;
  1373. }
  1374. case WASM_F64:
  1375. {
  1376. union {
  1377. float64 val;
  1378. uint32 parts[2];
  1379. } u;
  1380. u.val = args[i].of.f64;
  1381. out_argv[p++] = u.parts[0];
  1382. out_argv[p++] = u.parts[1];
  1383. break;
  1384. }
  1385. #if WASM_ENABLE_REF_TYPES != 0
  1386. case WASM_FUNCREF:
  1387. {
  1388. out_argv[p++] = args[i].of.i32;
  1389. break;
  1390. }
  1391. case WASM_ANYREF:
  1392. {
  1393. #if UINTPTR_MAX == UINT32_MAX
  1394. out_argv[p++] = args[i].of.foreign;
  1395. #else
  1396. union {
  1397. uintptr_t val;
  1398. uint32 parts[2];
  1399. } u;
  1400. u.val = (uintptr_t)args[i].of.foreign;
  1401. out_argv[p++] = u.parts[0];
  1402. out_argv[p++] = u.parts[1];
  1403. #endif
  1404. break;
  1405. }
  1406. #endif
  1407. default:
  1408. bh_assert(0);
  1409. break;
  1410. }
  1411. }
  1412. }
  1413. static void
  1414. parse_uint32_array_to_results(WASMType *type, uint32 *argv,
  1415. wasm_val_t *out_results)
  1416. {
  1417. uint32 i, p;
  1418. for (i = 0, p = 0; i < type->result_count; i++) {
  1419. switch (type->types[type->param_count + i]) {
  1420. case VALUE_TYPE_I32:
  1421. out_results[i].kind = WASM_I32;
  1422. out_results[i].of.i32 = (int32)argv[p++];
  1423. break;
  1424. case VALUE_TYPE_I64:
  1425. {
  1426. union {
  1427. uint64 val;
  1428. uint32 parts[2];
  1429. } u;
  1430. u.parts[0] = argv[p++];
  1431. u.parts[1] = argv[p++];
  1432. out_results[i].kind = WASM_I64;
  1433. out_results[i].of.i64 = u.val;
  1434. break;
  1435. }
  1436. case VALUE_TYPE_F32:
  1437. {
  1438. union {
  1439. float32 val;
  1440. uint32 part;
  1441. } u;
  1442. u.part = argv[p++];
  1443. out_results[i].kind = WASM_F32;
  1444. out_results[i].of.f32 = u.val;
  1445. break;
  1446. }
  1447. case VALUE_TYPE_F64:
  1448. {
  1449. union {
  1450. float64 val;
  1451. uint32 parts[2];
  1452. } u;
  1453. u.parts[0] = argv[p++];
  1454. u.parts[1] = argv[p++];
  1455. out_results[i].kind = WASM_F64;
  1456. out_results[i].of.f64 = u.val;
  1457. break;
  1458. }
  1459. #if WASM_ENABLE_REF_TYPES != 0
  1460. case VALUE_TYPE_FUNCREF:
  1461. {
  1462. out_results[i].kind = WASM_I32;
  1463. out_results[i].of.i32 = (int32)argv[p++];
  1464. break;
  1465. }
  1466. case VALUE_TYPE_EXTERNREF:
  1467. {
  1468. #if UINTPTR_MAX == UINT32_MAX
  1469. out_results[i].kind = WASM_ANYREF;
  1470. out_results[i].of.foreign = (uintptr_t)argv[p++];
  1471. #else
  1472. union {
  1473. uintptr_t val;
  1474. uint32 parts[2];
  1475. } u;
  1476. u.parts[0] = argv[p++];
  1477. u.parts[1] = argv[p++];
  1478. out_results[i].kind = WASM_ANYREF;
  1479. out_results[i].of.foreign = u.val;
  1480. #endif
  1481. break;
  1482. }
  1483. #endif
  1484. default:
  1485. bh_assert(0);
  1486. break;
  1487. }
  1488. }
  1489. }
  1490. bool
  1491. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  1492. WASMFunctionInstanceCommon *function,
  1493. uint32 num_results, wasm_val_t results[],
  1494. uint32 num_args, wasm_val_t args[])
  1495. {
  1496. uint32 argc, *argv, cell_num, total_size, module_type;
  1497. #if WASM_ENABLE_REF_TYPES != 0
  1498. uint32 i, param_size_in_double_world = 0, result_size_in_double_world = 0;
  1499. #endif
  1500. WASMType *type;
  1501. bool ret = false;
  1502. module_type = exec_env->module_inst->module_type;
  1503. type = wasm_runtime_get_function_type(function, module_type);
  1504. if (!type) {
  1505. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT must be "
  1506. "enabled at least one.");
  1507. goto fail1;
  1508. }
  1509. #if WASM_ENABLE_REF_TYPES != 0
  1510. for (i = 0; i < type->param_count; i++) {
  1511. param_size_in_double_world +=
  1512. wasm_value_type_cell_num_outside(type->types[i]);
  1513. }
  1514. for (i = 0; i < type->result_count; i++) {
  1515. result_size_in_double_world += wasm_value_type_cell_num_outside(
  1516. type->types[type->param_count + i]);
  1517. }
  1518. argc = param_size_in_double_world;
  1519. cell_num = (argc >= result_size_in_double_world)
  1520. ? argc
  1521. : result_size_in_double_world;
  1522. #else
  1523. argc = type->param_cell_num;
  1524. cell_num = (argc > type->ret_cell_num) ? argc : type->ret_cell_num;
  1525. #endif
  1526. if (num_results != type->result_count) {
  1527. LOG_ERROR(
  1528. "The result value number does not match the function declaration.");
  1529. goto fail1;
  1530. }
  1531. if (num_args != type->param_count) {
  1532. LOG_ERROR("The argument value number does not match the function "
  1533. "declaration.");
  1534. goto fail1;
  1535. }
  1536. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  1537. if (!(argv = runtime_malloc((uint32)total_size, exec_env->module_inst, NULL,
  1538. 0))) {
  1539. wasm_runtime_set_exception(exec_env->module_inst,
  1540. "allocate memory failed");
  1541. goto fail1;
  1542. }
  1543. parse_args_to_uint32_array(type, args, argv);
  1544. if (!(ret = wasm_runtime_call_wasm(exec_env, function, argc, argv)))
  1545. goto fail2;
  1546. parse_uint32_array_to_results(type, argv, results);
  1547. fail2:
  1548. wasm_runtime_free(argv);
  1549. fail1:
  1550. return ret;
  1551. }
  1552. bool
  1553. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  1554. WASMFunctionInstanceCommon *function,
  1555. uint32 num_results, wasm_val_t results[],
  1556. uint32 num_args, ...)
  1557. {
  1558. wasm_val_t *args = NULL;
  1559. WASMType *type = NULL;
  1560. bool ret = false;
  1561. uint32 i = 0, module_type;
  1562. va_list vargs;
  1563. module_type = exec_env->module_inst->module_type;
  1564. type = wasm_runtime_get_function_type(function, module_type);
  1565. if (!type) {
  1566. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT "
  1567. "must be enabled at least one.");
  1568. goto fail1;
  1569. }
  1570. if (num_args != type->param_count) {
  1571. LOG_ERROR("The argument value number does not match the "
  1572. "function declaration.");
  1573. goto fail1;
  1574. }
  1575. if (!(args =
  1576. runtime_malloc(sizeof(wasm_val_t) * num_args, NULL, NULL, 0))) {
  1577. wasm_runtime_set_exception(exec_env->module_inst,
  1578. "allocate memory failed");
  1579. goto fail1;
  1580. }
  1581. va_start(vargs, num_args);
  1582. for (i = 0; i < num_args; i++) {
  1583. switch (type->types[i]) {
  1584. case VALUE_TYPE_I32:
  1585. args[i].kind = WASM_I32;
  1586. args[i].of.i32 = va_arg(vargs, uint32);
  1587. break;
  1588. case VALUE_TYPE_I64:
  1589. args[i].kind = WASM_I64;
  1590. args[i].of.i64 = va_arg(vargs, uint64);
  1591. break;
  1592. case VALUE_TYPE_F32:
  1593. args[i].kind = WASM_F32;
  1594. args[i].of.f32 = (float32)va_arg(vargs, float64);
  1595. break;
  1596. case VALUE_TYPE_F64:
  1597. args[i].kind = WASM_F64;
  1598. args[i].of.f64 = va_arg(vargs, float64);
  1599. break;
  1600. #if WASM_ENABLE_REF_TYPES != 0
  1601. case VALUE_TYPE_FUNCREF:
  1602. {
  1603. args[i].kind = WASM_FUNCREF;
  1604. args[i].of.i32 = va_arg(vargs, uint32);
  1605. break;
  1606. }
  1607. case VALUE_TYPE_EXTERNREF:
  1608. {
  1609. args[i].kind = WASM_ANYREF;
  1610. args[i].of.foreign = va_arg(vargs, uintptr_t);
  1611. break;
  1612. }
  1613. #endif
  1614. default:
  1615. bh_assert(0);
  1616. break;
  1617. }
  1618. }
  1619. va_end(vargs);
  1620. ret = wasm_runtime_call_wasm_a(exec_env, function, num_results, results,
  1621. num_args, args);
  1622. wasm_runtime_free(args);
  1623. fail1:
  1624. return ret;
  1625. }
  1626. bool
  1627. wasm_runtime_create_exec_env_and_call_wasm(
  1628. WASMModuleInstanceCommon *module_inst, WASMFunctionInstanceCommon *function,
  1629. uint32 argc, uint32 argv[])
  1630. {
  1631. bool ret = false;
  1632. #if WASM_ENABLE_INTERP != 0
  1633. if (module_inst->module_type == Wasm_Module_Bytecode)
  1634. ret = wasm_create_exec_env_and_call_function(
  1635. (WASMModuleInstance *)module_inst, (WASMFunctionInstance *)function,
  1636. argc, argv, true);
  1637. #endif
  1638. #if WASM_ENABLE_AOT != 0
  1639. if (module_inst->module_type == Wasm_Module_AoT)
  1640. ret = aot_create_exec_env_and_call_function(
  1641. (AOTModuleInstance *)module_inst, (AOTFunctionInstance *)function,
  1642. argc, argv);
  1643. #endif
  1644. return ret;
  1645. }
  1646. bool
  1647. wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst)
  1648. {
  1649. #if WASM_ENABLE_INTERP != 0
  1650. if (module_inst->module_type == Wasm_Module_Bytecode)
  1651. return wasm_create_exec_env_singleton(
  1652. (WASMModuleInstance *)module_inst);
  1653. #endif
  1654. #if WASM_ENABLE_AOT != 0
  1655. if (module_inst->module_type == Wasm_Module_AoT)
  1656. return aot_create_exec_env_singleton((AOTModuleInstance *)module_inst);
  1657. #endif
  1658. return false;
  1659. }
  1660. WASMExecEnv *
  1661. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst)
  1662. {
  1663. #if WASM_ENABLE_INTERP != 0
  1664. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1665. if (!((WASMModuleInstance *)module_inst)->exec_env_singleton) {
  1666. wasm_create_exec_env_singleton((WASMModuleInstance *)module_inst);
  1667. }
  1668. return ((WASMModuleInstance *)module_inst)->exec_env_singleton;
  1669. }
  1670. #endif
  1671. #if WASM_ENABLE_AOT != 0
  1672. if (module_inst->module_type == Wasm_Module_AoT) {
  1673. if (!((AOTModuleInstance *)module_inst)->exec_env_singleton.ptr) {
  1674. aot_create_exec_env_singleton((AOTModuleInstance *)module_inst);
  1675. }
  1676. return (WASMExecEnv *)((AOTModuleInstance *)module_inst)
  1677. ->exec_env_singleton.ptr;
  1678. }
  1679. #endif
  1680. return NULL;
  1681. }
  1682. void
  1683. wasm_runtime_set_exception(WASMModuleInstanceCommon *module_inst,
  1684. const char *exception)
  1685. {
  1686. #if WASM_ENABLE_INTERP != 0
  1687. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1688. wasm_set_exception((WASMModuleInstance *)module_inst, exception);
  1689. return;
  1690. }
  1691. #endif
  1692. #if WASM_ENABLE_AOT != 0
  1693. if (module_inst->module_type == Wasm_Module_AoT) {
  1694. aot_set_exception((AOTModuleInstance *)module_inst, exception);
  1695. return;
  1696. }
  1697. #endif
  1698. }
  1699. const char *
  1700. wasm_runtime_get_exception(WASMModuleInstanceCommon *module_inst)
  1701. {
  1702. #if WASM_ENABLE_INTERP != 0
  1703. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1704. return wasm_get_exception((WASMModuleInstance *)module_inst);
  1705. }
  1706. #endif
  1707. #if WASM_ENABLE_AOT != 0
  1708. if (module_inst->module_type == Wasm_Module_AoT) {
  1709. return aot_get_exception((AOTModuleInstance *)module_inst);
  1710. }
  1711. #endif
  1712. return NULL;
  1713. }
  1714. void
  1715. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst)
  1716. {
  1717. wasm_runtime_set_exception(module_inst, NULL);
  1718. }
  1719. void
  1720. wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
  1721. void *custom_data)
  1722. {
  1723. #if WASM_ENABLE_INTERP != 0
  1724. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1725. ((WASMModuleInstance *)module_inst)->custom_data = custom_data;
  1726. return;
  1727. }
  1728. #endif
  1729. #if WASM_ENABLE_AOT != 0
  1730. if (module_inst->module_type == Wasm_Module_AoT) {
  1731. ((AOTModuleInstance *)module_inst)->custom_data.ptr = custom_data;
  1732. return;
  1733. }
  1734. #endif
  1735. }
  1736. void
  1737. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  1738. void *custom_data)
  1739. {
  1740. #if WASM_ENABLE_THREAD_MGR != 0
  1741. wasm_cluster_spread_custom_data(module_inst, custom_data);
  1742. #else
  1743. wasm_runtime_set_custom_data_internal(module_inst, custom_data);
  1744. #endif
  1745. }
  1746. void *
  1747. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst)
  1748. {
  1749. #if WASM_ENABLE_INTERP != 0
  1750. if (module_inst->module_type == Wasm_Module_Bytecode)
  1751. return ((WASMModuleInstance *)module_inst)->custom_data;
  1752. #endif
  1753. #if WASM_ENABLE_AOT != 0
  1754. if (module_inst->module_type == Wasm_Module_AoT)
  1755. return ((AOTModuleInstance *)module_inst)->custom_data.ptr;
  1756. #endif
  1757. return NULL;
  1758. }
  1759. uint32
  1760. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  1761. void **p_native_addr)
  1762. {
  1763. #if WASM_ENABLE_INTERP != 0
  1764. if (module_inst->module_type == Wasm_Module_Bytecode)
  1765. return wasm_module_malloc((WASMModuleInstance *)module_inst, size,
  1766. p_native_addr);
  1767. #endif
  1768. #if WASM_ENABLE_AOT != 0
  1769. if (module_inst->module_type == Wasm_Module_AoT)
  1770. return aot_module_malloc((AOTModuleInstance *)module_inst, size,
  1771. p_native_addr);
  1772. #endif
  1773. return 0;
  1774. }
  1775. uint32
  1776. wasm_runtime_module_realloc(WASMModuleInstanceCommon *module_inst, uint32 ptr,
  1777. uint32 size, void **p_native_addr)
  1778. {
  1779. #if WASM_ENABLE_INTERP != 0
  1780. if (module_inst->module_type == Wasm_Module_Bytecode)
  1781. return wasm_module_realloc((WASMModuleInstance *)module_inst, ptr, size,
  1782. p_native_addr);
  1783. #endif
  1784. #if WASM_ENABLE_AOT != 0
  1785. if (module_inst->module_type == Wasm_Module_AoT)
  1786. return aot_module_realloc((AOTModuleInstance *)module_inst, ptr, size,
  1787. p_native_addr);
  1788. #endif
  1789. return 0;
  1790. }
  1791. void
  1792. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr)
  1793. {
  1794. #if WASM_ENABLE_INTERP != 0
  1795. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1796. wasm_module_free((WASMModuleInstance *)module_inst, ptr);
  1797. return;
  1798. }
  1799. #endif
  1800. #if WASM_ENABLE_AOT != 0
  1801. if (module_inst->module_type == Wasm_Module_AoT) {
  1802. aot_module_free((AOTModuleInstance *)module_inst, ptr);
  1803. return;
  1804. }
  1805. #endif
  1806. }
  1807. uint32
  1808. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  1809. const char *src, uint32 size)
  1810. {
  1811. #if WASM_ENABLE_INTERP != 0
  1812. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1813. return wasm_module_dup_data((WASMModuleInstance *)module_inst, src,
  1814. size);
  1815. }
  1816. #endif
  1817. #if WASM_ENABLE_AOT != 0
  1818. if (module_inst->module_type == Wasm_Module_AoT) {
  1819. return aot_module_dup_data((AOTModuleInstance *)module_inst, src, size);
  1820. }
  1821. #endif
  1822. return 0;
  1823. }
  1824. bool
  1825. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  1826. uint32 app_offset, uint32 size)
  1827. {
  1828. #if WASM_ENABLE_INTERP != 0
  1829. if (module_inst->module_type == Wasm_Module_Bytecode)
  1830. return wasm_validate_app_addr((WASMModuleInstance *)module_inst,
  1831. app_offset, size);
  1832. #endif
  1833. #if WASM_ENABLE_AOT != 0
  1834. if (module_inst->module_type == Wasm_Module_AoT)
  1835. return aot_validate_app_addr((AOTModuleInstance *)module_inst,
  1836. app_offset, size);
  1837. #endif
  1838. return false;
  1839. }
  1840. bool
  1841. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  1842. uint32 app_str_offset)
  1843. {
  1844. uint32 app_end_offset;
  1845. char *str, *str_end;
  1846. if (!wasm_runtime_get_app_addr_range(module_inst, app_str_offset, NULL,
  1847. &app_end_offset))
  1848. goto fail;
  1849. str = wasm_runtime_addr_app_to_native(module_inst, app_str_offset);
  1850. str_end = str + (app_end_offset - app_str_offset);
  1851. while (str < str_end && *str != '\0')
  1852. str++;
  1853. if (str == str_end)
  1854. goto fail;
  1855. return true;
  1856. fail:
  1857. wasm_runtime_set_exception(module_inst, "out of bounds memory access");
  1858. return false;
  1859. }
  1860. bool
  1861. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  1862. void *native_ptr, uint32 size)
  1863. {
  1864. #if WASM_ENABLE_INTERP != 0
  1865. if (module_inst->module_type == Wasm_Module_Bytecode)
  1866. return wasm_validate_native_addr((WASMModuleInstance *)module_inst,
  1867. native_ptr, size);
  1868. #endif
  1869. #if WASM_ENABLE_AOT != 0
  1870. if (module_inst->module_type == Wasm_Module_AoT)
  1871. return aot_validate_native_addr((AOTModuleInstance *)module_inst,
  1872. native_ptr, size);
  1873. #endif
  1874. return false;
  1875. }
  1876. void *
  1877. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  1878. uint32 app_offset)
  1879. {
  1880. #if WASM_ENABLE_INTERP != 0
  1881. if (module_inst->module_type == Wasm_Module_Bytecode)
  1882. return wasm_addr_app_to_native((WASMModuleInstance *)module_inst,
  1883. app_offset);
  1884. #endif
  1885. #if WASM_ENABLE_AOT != 0
  1886. if (module_inst->module_type == Wasm_Module_AoT)
  1887. return aot_addr_app_to_native((AOTModuleInstance *)module_inst,
  1888. app_offset);
  1889. #endif
  1890. return NULL;
  1891. }
  1892. uint32
  1893. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  1894. void *native_ptr)
  1895. {
  1896. #if WASM_ENABLE_INTERP != 0
  1897. if (module_inst->module_type == Wasm_Module_Bytecode)
  1898. return wasm_addr_native_to_app((WASMModuleInstance *)module_inst,
  1899. native_ptr);
  1900. #endif
  1901. #if WASM_ENABLE_AOT != 0
  1902. if (module_inst->module_type == Wasm_Module_AoT)
  1903. return aot_addr_native_to_app((AOTModuleInstance *)module_inst,
  1904. native_ptr);
  1905. #endif
  1906. return 0;
  1907. }
  1908. bool
  1909. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  1910. uint32 app_offset, uint32 *p_app_start_offset,
  1911. uint32 *p_app_end_offset)
  1912. {
  1913. #if WASM_ENABLE_INTERP != 0
  1914. if (module_inst->module_type == Wasm_Module_Bytecode)
  1915. return wasm_get_app_addr_range((WASMModuleInstance *)module_inst,
  1916. app_offset, p_app_start_offset,
  1917. p_app_end_offset);
  1918. #endif
  1919. #if WASM_ENABLE_AOT != 0
  1920. if (module_inst->module_type == Wasm_Module_AoT)
  1921. return aot_get_app_addr_range((AOTModuleInstance *)module_inst,
  1922. app_offset, p_app_start_offset,
  1923. p_app_end_offset);
  1924. #endif
  1925. return false;
  1926. }
  1927. bool
  1928. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  1929. uint8 *native_ptr,
  1930. uint8 **p_native_start_addr,
  1931. uint8 **p_native_end_addr)
  1932. {
  1933. #if WASM_ENABLE_INTERP != 0
  1934. if (module_inst->module_type == Wasm_Module_Bytecode)
  1935. return wasm_get_native_addr_range((WASMModuleInstance *)module_inst,
  1936. native_ptr, p_native_start_addr,
  1937. p_native_end_addr);
  1938. #endif
  1939. #if WASM_ENABLE_AOT != 0
  1940. if (module_inst->module_type == Wasm_Module_AoT)
  1941. return aot_get_native_addr_range((AOTModuleInstance *)module_inst,
  1942. native_ptr, p_native_start_addr,
  1943. p_native_end_addr);
  1944. #endif
  1945. return false;
  1946. }
  1947. uint32
  1948. wasm_runtime_get_temp_ret(WASMModuleInstanceCommon *module_inst)
  1949. {
  1950. #if WASM_ENABLE_INTERP != 0
  1951. if (module_inst->module_type == Wasm_Module_Bytecode)
  1952. return ((WASMModuleInstance *)module_inst)->temp_ret;
  1953. #endif
  1954. #if WASM_ENABLE_AOT != 0
  1955. if (module_inst->module_type == Wasm_Module_AoT)
  1956. return ((AOTModuleInstance *)module_inst)->temp_ret;
  1957. #endif
  1958. return 0;
  1959. }
  1960. void
  1961. wasm_runtime_set_temp_ret(WASMModuleInstanceCommon *module_inst,
  1962. uint32 temp_ret)
  1963. {
  1964. #if WASM_ENABLE_INTERP != 0
  1965. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1966. ((WASMModuleInstance *)module_inst)->temp_ret = temp_ret;
  1967. return;
  1968. }
  1969. #endif
  1970. #if WASM_ENABLE_AOT != 0
  1971. if (module_inst->module_type == Wasm_Module_AoT) {
  1972. ((AOTModuleInstance *)module_inst)->temp_ret = temp_ret;
  1973. return;
  1974. }
  1975. #endif
  1976. }
  1977. uint32
  1978. wasm_runtime_get_llvm_stack(WASMModuleInstanceCommon *module_inst)
  1979. {
  1980. #if WASM_ENABLE_INTERP != 0
  1981. if (module_inst->module_type == Wasm_Module_Bytecode)
  1982. return ((WASMModuleInstance *)module_inst)->llvm_stack;
  1983. #endif
  1984. #if WASM_ENABLE_AOT != 0
  1985. if (module_inst->module_type == Wasm_Module_AoT)
  1986. return ((AOTModuleInstance *)module_inst)->llvm_stack;
  1987. #endif
  1988. return 0;
  1989. }
  1990. void
  1991. wasm_runtime_set_llvm_stack(WASMModuleInstanceCommon *module_inst,
  1992. uint32 llvm_stack)
  1993. {
  1994. #if WASM_ENABLE_INTERP != 0
  1995. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1996. ((WASMModuleInstance *)module_inst)->llvm_stack = llvm_stack;
  1997. return;
  1998. }
  1999. #endif
  2000. #if WASM_ENABLE_AOT != 0
  2001. if (module_inst->module_type == Wasm_Module_AoT) {
  2002. ((AOTModuleInstance *)module_inst)->llvm_stack = llvm_stack;
  2003. return;
  2004. }
  2005. #endif
  2006. }
  2007. bool
  2008. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module,
  2009. uint32 inc_page_count)
  2010. {
  2011. #if WASM_ENABLE_INTERP != 0
  2012. if (module->module_type == Wasm_Module_Bytecode)
  2013. return wasm_enlarge_memory((WASMModuleInstance *)module,
  2014. inc_page_count);
  2015. #endif
  2016. #if WASM_ENABLE_AOT != 0
  2017. if (module->module_type == Wasm_Module_AoT)
  2018. return aot_enlarge_memory((AOTModuleInstance *)module, inc_page_count);
  2019. #endif
  2020. return false;
  2021. }
  2022. #if WASM_ENABLE_LIBC_WASI != 0
  2023. void
  2024. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
  2025. uint32 dir_count, const char *map_dir_list[],
  2026. uint32 map_dir_count, const char *env_list[],
  2027. uint32 env_count, char *argv[], int argc,
  2028. int stdinfd, int stdoutfd, int stderrfd)
  2029. {
  2030. WASIArguments *wasi_args = NULL;
  2031. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  2032. if (module->module_type == Wasm_Module_Bytecode)
  2033. wasi_args = &((WASMModule *)module)->wasi_args;
  2034. #endif
  2035. #if WASM_ENABLE_AOT != 0
  2036. if (module->module_type == Wasm_Module_AoT)
  2037. wasi_args = &((AOTModule *)module)->wasi_args;
  2038. #endif
  2039. if (wasi_args) {
  2040. wasi_args->dir_list = dir_list;
  2041. wasi_args->dir_count = dir_count;
  2042. wasi_args->map_dir_list = map_dir_list;
  2043. wasi_args->map_dir_count = map_dir_count;
  2044. wasi_args->env = env_list;
  2045. wasi_args->env_count = env_count;
  2046. wasi_args->argv = argv;
  2047. wasi_args->argc = (uint32)argc;
  2048. wasi_args->stdio[0] = stdinfd;
  2049. wasi_args->stdio[1] = stdoutfd;
  2050. wasi_args->stdio[2] = stderrfd;
  2051. }
  2052. }
  2053. void
  2054. wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
  2055. uint32 dir_count, const char *map_dir_list[],
  2056. uint32 map_dir_count, const char *env_list[],
  2057. uint32 env_count, char *argv[], int argc)
  2058. {
  2059. wasm_runtime_set_wasi_args_ex(module, dir_list, dir_count, map_dir_list,
  2060. map_dir_count, env_list, env_count, argv,
  2061. argc, -1, -1, -1);
  2062. }
  2063. void
  2064. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  2065. uint32 addr_pool_size)
  2066. {
  2067. WASIArguments *wasi_args = NULL;
  2068. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  2069. if (module->module_type == Wasm_Module_Bytecode)
  2070. wasi_args = &((WASMModule *)module)->wasi_args;
  2071. #endif
  2072. #if WASM_ENABLE_AOT != 0
  2073. if (module->module_type == Wasm_Module_AoT)
  2074. wasi_args = &((AOTModule *)module)->wasi_args;
  2075. #endif
  2076. if (wasi_args) {
  2077. wasi_args->addr_pool = addr_pool;
  2078. wasi_args->addr_count = addr_pool_size;
  2079. }
  2080. }
  2081. #if WASM_ENABLE_UVWASI == 0
  2082. bool
  2083. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  2084. const char *dir_list[], uint32 dir_count,
  2085. const char *map_dir_list[], uint32 map_dir_count,
  2086. const char *env[], uint32 env_count,
  2087. const char *addr_pool[], uint32 addr_pool_size,
  2088. char *argv[], uint32 argc, int stdinfd, int stdoutfd,
  2089. int stderrfd, char *error_buf, uint32 error_buf_size)
  2090. {
  2091. WASIContext *wasi_ctx;
  2092. char *argv_buf = NULL;
  2093. char **argv_list = NULL;
  2094. char *env_buf = NULL;
  2095. char **env_list = NULL;
  2096. uint64 argv_buf_size = 0, env_buf_size = 0, total_size;
  2097. uint32 argv_buf_offset = 0, env_buf_offset = 0;
  2098. struct fd_table *curfds = NULL;
  2099. struct fd_prestats *prestats = NULL;
  2100. struct argv_environ_values *argv_environ = NULL;
  2101. struct addr_pool *apool = NULL;
  2102. bool fd_table_inited = false, fd_prestats_inited = false;
  2103. bool argv_environ_inited = false;
  2104. bool addr_pool_inited = false;
  2105. __wasi_fd_t wasm_fd = 3;
  2106. int32 raw_fd;
  2107. char *path, resolved_path[PATH_MAX];
  2108. uint32 i;
  2109. if (!(wasi_ctx = runtime_malloc(sizeof(WASIContext), NULL, error_buf,
  2110. error_buf_size))) {
  2111. return false;
  2112. }
  2113. wasm_runtime_set_wasi_ctx(module_inst, wasi_ctx);
  2114. #if WASM_ENABLE_INTERP != 0
  2115. if (module_inst->module_type == Wasm_Module_Bytecode
  2116. && !((WASMModuleInstance *)module_inst)->default_memory)
  2117. return true;
  2118. #endif
  2119. #if WASM_ENABLE_AOT != 0
  2120. if (module_inst->module_type == Wasm_Module_AoT
  2121. && !((AOTModuleInstance *)module_inst)
  2122. ->global_table_data.memory_instances[0]
  2123. .memory_data.ptr)
  2124. return true;
  2125. #endif
  2126. /* process argv[0], trip the path and suffix, only keep the program name */
  2127. for (i = 0; i < argc; i++)
  2128. argv_buf_size += strlen(argv[i]) + 1;
  2129. total_size = sizeof(char *) * (uint64)argc;
  2130. if (total_size >= UINT32_MAX
  2131. || (total_size > 0
  2132. && !(argv_list = wasm_runtime_malloc((uint32)total_size)))
  2133. || argv_buf_size >= UINT32_MAX
  2134. || (argv_buf_size > 0
  2135. && !(argv_buf = wasm_runtime_malloc((uint32)argv_buf_size)))) {
  2136. set_error_buf(error_buf, error_buf_size,
  2137. "Init wasi environment failed: allocate memory failed");
  2138. goto fail;
  2139. }
  2140. for (i = 0; i < argc; i++) {
  2141. argv_list[i] = argv_buf + argv_buf_offset;
  2142. bh_strcpy_s(argv_buf + argv_buf_offset,
  2143. (uint32)argv_buf_size - argv_buf_offset, argv[i]);
  2144. argv_buf_offset += (uint32)(strlen(argv[i]) + 1);
  2145. }
  2146. for (i = 0; i < env_count; i++)
  2147. env_buf_size += strlen(env[i]) + 1;
  2148. total_size = sizeof(char *) * (uint64)env_count;
  2149. if (total_size >= UINT32_MAX
  2150. || (total_size > 0
  2151. && !(env_list = wasm_runtime_malloc((uint32)total_size)))
  2152. || env_buf_size >= UINT32_MAX
  2153. || (env_buf_size > 0
  2154. && !(env_buf = wasm_runtime_malloc((uint32)env_buf_size)))) {
  2155. set_error_buf(error_buf, error_buf_size,
  2156. "Init wasi environment failed: allocate memory failed");
  2157. goto fail;
  2158. }
  2159. for (i = 0; i < env_count; i++) {
  2160. env_list[i] = env_buf + env_buf_offset;
  2161. bh_strcpy_s(env_buf + env_buf_offset,
  2162. (uint32)env_buf_size - env_buf_offset, env[i]);
  2163. env_buf_offset += (uint32)(strlen(env[i]) + 1);
  2164. }
  2165. if (!(curfds = wasm_runtime_malloc(sizeof(struct fd_table)))
  2166. || !(prestats = wasm_runtime_malloc(sizeof(struct fd_prestats)))
  2167. || !(argv_environ =
  2168. wasm_runtime_malloc(sizeof(struct argv_environ_values)))
  2169. || !(apool = wasm_runtime_malloc(sizeof(struct addr_pool)))) {
  2170. set_error_buf(error_buf, error_buf_size,
  2171. "Init wasi environment failed: allocate memory failed");
  2172. goto fail;
  2173. }
  2174. if (!fd_table_init(curfds)) {
  2175. set_error_buf(error_buf, error_buf_size,
  2176. "Init wasi environment failed: "
  2177. "init fd table failed");
  2178. goto fail;
  2179. }
  2180. fd_table_inited = true;
  2181. if (!fd_prestats_init(prestats)) {
  2182. set_error_buf(error_buf, error_buf_size,
  2183. "Init wasi environment failed: "
  2184. "init fd prestats failed");
  2185. goto fail;
  2186. }
  2187. fd_prestats_inited = true;
  2188. if (!argv_environ_init(argv_environ, argv_buf, argv_buf_size, argv_list,
  2189. argc, env_buf, env_buf_size, env_list, env_count)) {
  2190. set_error_buf(error_buf, error_buf_size,
  2191. "Init wasi environment failed: "
  2192. "init argument environment failed");
  2193. goto fail;
  2194. }
  2195. argv_environ_inited = true;
  2196. if (!addr_pool_init(apool)) {
  2197. set_error_buf(error_buf, error_buf_size,
  2198. "Init wasi environment failed: "
  2199. "init the address pool failed");
  2200. goto fail;
  2201. }
  2202. addr_pool_inited = true;
  2203. /* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
  2204. if (!fd_table_insert_existing(curfds, 0, (stdinfd != -1) ? stdinfd : 0)
  2205. || !fd_table_insert_existing(curfds, 1, (stdoutfd != -1) ? stdoutfd : 1)
  2206. || !fd_table_insert_existing(curfds, 2,
  2207. (stderrfd != -1) ? stderrfd : 2)) {
  2208. set_error_buf(error_buf, error_buf_size,
  2209. "Init wasi environment failed: init fd table failed");
  2210. goto fail;
  2211. }
  2212. wasm_fd = 3;
  2213. for (i = 0; i < dir_count; i++, wasm_fd++) {
  2214. path = realpath(dir_list[i], resolved_path);
  2215. if (!path) {
  2216. if (error_buf)
  2217. snprintf(error_buf, error_buf_size,
  2218. "error while pre-opening directory %s: %d\n",
  2219. dir_list[i], errno);
  2220. goto fail;
  2221. }
  2222. raw_fd = open(path, O_RDONLY | O_DIRECTORY, 0);
  2223. if (raw_fd == -1) {
  2224. if (error_buf)
  2225. snprintf(error_buf, error_buf_size,
  2226. "error while pre-opening directory %s: %d\n",
  2227. dir_list[i], errno);
  2228. goto fail;
  2229. }
  2230. fd_table_insert_existing(curfds, wasm_fd, raw_fd);
  2231. fd_prestats_insert(prestats, dir_list[i], wasm_fd);
  2232. }
  2233. /* addr_pool(textual) -> apool */
  2234. for (i = 0; i < addr_pool_size; i++) {
  2235. char *cp, *address, *mask;
  2236. bool ret = false;
  2237. cp = bh_strdup(addr_pool[i]);
  2238. if (!cp) {
  2239. set_error_buf(error_buf, error_buf_size,
  2240. "Init wasi environment failed: copy address failed");
  2241. goto fail;
  2242. }
  2243. address = strtok(cp, "/");
  2244. mask = strtok(NULL, "/");
  2245. ret = addr_pool_insert(apool, address, (uint8)(mask ? atoi(mask) : 0));
  2246. wasm_runtime_free(cp);
  2247. if (!ret) {
  2248. set_error_buf(error_buf, error_buf_size,
  2249. "Init wasi environment failed: store address failed");
  2250. goto fail;
  2251. }
  2252. }
  2253. wasi_ctx->curfds = curfds;
  2254. wasi_ctx->prestats = prestats;
  2255. wasi_ctx->argv_environ = argv_environ;
  2256. wasi_ctx->addr_pool = apool;
  2257. wasi_ctx->argv_buf = argv_buf;
  2258. wasi_ctx->argv_list = argv_list;
  2259. wasi_ctx->env_buf = env_buf;
  2260. wasi_ctx->env_list = env_list;
  2261. return true;
  2262. fail:
  2263. if (argv_environ_inited)
  2264. argv_environ_destroy(argv_environ);
  2265. if (fd_prestats_inited)
  2266. fd_prestats_destroy(prestats);
  2267. if (fd_table_inited)
  2268. fd_table_destroy(curfds);
  2269. if (addr_pool_inited)
  2270. addr_pool_destroy(apool);
  2271. if (curfds)
  2272. wasm_runtime_free(curfds);
  2273. if (prestats)
  2274. wasm_runtime_free(prestats);
  2275. if (argv_environ)
  2276. wasm_runtime_free(argv_environ);
  2277. if (apool)
  2278. wasm_runtime_free(apool);
  2279. if (argv_buf)
  2280. wasm_runtime_free(argv_buf);
  2281. if (argv_list)
  2282. wasm_runtime_free(argv_list);
  2283. if (env_buf)
  2284. wasm_runtime_free(env_buf);
  2285. if (env_list)
  2286. wasm_runtime_free(env_list);
  2287. return false;
  2288. }
  2289. #else /* else of WASM_ENABLE_UVWASI == 0 */
  2290. static void *
  2291. wasm_uvwasi_malloc(size_t size, void *mem_user_data)
  2292. {
  2293. return runtime_malloc(size, NULL, NULL, 0);
  2294. (void)mem_user_data;
  2295. }
  2296. static void
  2297. wasm_uvwasi_free(void *ptr, void *mem_user_data)
  2298. {
  2299. if (ptr)
  2300. wasm_runtime_free(ptr);
  2301. (void)mem_user_data;
  2302. }
  2303. static void *
  2304. wasm_uvwasi_calloc(size_t nmemb, size_t size, void *mem_user_data)
  2305. {
  2306. uint64 total_size = (uint64)nmemb * size;
  2307. return runtime_malloc(total_size, NULL, NULL, 0);
  2308. (void)mem_user_data;
  2309. }
  2310. static void *
  2311. wasm_uvwasi_realloc(void *ptr, size_t size, void *mem_user_data)
  2312. {
  2313. if (size >= UINT32_MAX) {
  2314. return NULL;
  2315. }
  2316. return wasm_runtime_realloc(ptr, (uint32)size);
  2317. }
  2318. /* clang-format off */
  2319. static uvwasi_mem_t uvwasi_allocator = {
  2320. .mem_user_data = 0,
  2321. .malloc = wasm_uvwasi_malloc,
  2322. .free = wasm_uvwasi_free,
  2323. .calloc = wasm_uvwasi_calloc,
  2324. .realloc = wasm_uvwasi_realloc
  2325. };
  2326. /* clang-format on */
  2327. bool
  2328. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  2329. const char *dir_list[], uint32 dir_count,
  2330. const char *map_dir_list[], uint32 map_dir_count,
  2331. const char *env[], uint32 env_count,
  2332. const char *addr_pool[], uint32 addr_pool_size,
  2333. char *argv[], uint32 argc, int stdinfd, int stdoutfd,
  2334. int stderrfd, char *error_buf, uint32 error_buf_size)
  2335. {
  2336. uvwasi_t *uvwasi = NULL;
  2337. uvwasi_options_t init_options;
  2338. const char **envp = NULL;
  2339. uint64 total_size;
  2340. uint32 i;
  2341. bool ret = false;
  2342. uvwasi = runtime_malloc(sizeof(uvwasi_t), module_inst, error_buf,
  2343. error_buf_size);
  2344. if (!uvwasi)
  2345. return false;
  2346. /* Setup the initialization options */
  2347. uvwasi_options_init(&init_options);
  2348. init_options.allocator = &uvwasi_allocator;
  2349. init_options.argc = argc;
  2350. init_options.argv = (const char **)argv;
  2351. init_options.in = (stdinfd != -1) ? (uvwasi_fd_t)stdinfd : init_options.in;
  2352. init_options.out =
  2353. (stdoutfd != -1) ? (uvwasi_fd_t)stdoutfd : init_options.out;
  2354. init_options.err =
  2355. (stderrfd != -1) ? (uvwasi_fd_t)stderrfd : init_options.err;
  2356. if (dir_count > 0) {
  2357. init_options.preopenc = dir_count;
  2358. total_size = sizeof(uvwasi_preopen_t) * (uint64)init_options.preopenc;
  2359. init_options.preopens = (uvwasi_preopen_t *)runtime_malloc(
  2360. total_size, module_inst, error_buf, error_buf_size);
  2361. if (init_options.preopens == NULL)
  2362. goto fail;
  2363. for (i = 0; i < init_options.preopenc; i++) {
  2364. init_options.preopens[i].real_path = dir_list[i];
  2365. init_options.preopens[i].mapped_path =
  2366. (i < map_dir_count) ? map_dir_list[i] : dir_list[i];
  2367. }
  2368. }
  2369. if (env_count > 0) {
  2370. total_size = sizeof(char *) * (uint64)(env_count + 1);
  2371. envp =
  2372. runtime_malloc(total_size, module_inst, error_buf, error_buf_size);
  2373. if (envp == NULL)
  2374. goto fail;
  2375. for (i = 0; i < env_count; i++) {
  2376. envp[i] = env[i];
  2377. }
  2378. envp[env_count] = NULL;
  2379. init_options.envp = envp;
  2380. }
  2381. if (UVWASI_ESUCCESS != uvwasi_init(uvwasi, &init_options)) {
  2382. set_error_buf(error_buf, error_buf_size, "uvwasi init failed");
  2383. goto fail;
  2384. }
  2385. wasm_runtime_set_wasi_ctx(module_inst, uvwasi);
  2386. ret = true;
  2387. fail:
  2388. if (envp)
  2389. wasm_runtime_free((void *)envp);
  2390. if (init_options.preopens)
  2391. wasm_runtime_free(init_options.preopens);
  2392. if (!ret && uvwasi)
  2393. wasm_runtime_free(uvwasi);
  2394. return ret;
  2395. }
  2396. #endif /* end of WASM_ENABLE_UVWASI */
  2397. bool
  2398. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst)
  2399. {
  2400. #if WASM_ENABLE_INTERP != 0
  2401. if (module_inst->module_type == Wasm_Module_Bytecode
  2402. && ((WASMModuleInstance *)module_inst)->module->import_wasi_api)
  2403. return true;
  2404. #endif
  2405. #if WASM_ENABLE_AOT != 0
  2406. if (module_inst->module_type == Wasm_Module_AoT
  2407. && ((AOTModule *)((AOTModuleInstance *)module_inst)->aot_module.ptr)
  2408. ->import_wasi_api)
  2409. return true;
  2410. #endif
  2411. return false;
  2412. }
  2413. WASMFunctionInstanceCommon *
  2414. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst)
  2415. {
  2416. uint32 i;
  2417. #if WASM_ENABLE_INTERP != 0
  2418. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2419. WASMModuleInstance *wasm_inst = (WASMModuleInstance *)module_inst;
  2420. WASMFunctionInstance *func;
  2421. for (i = 0; i < wasm_inst->export_func_count; i++) {
  2422. if (!strcmp(wasm_inst->export_functions[i].name, "_start")) {
  2423. func = wasm_inst->export_functions[i].function;
  2424. if (func->u.func->func_type->param_count != 0
  2425. || func->u.func->func_type->result_count != 0) {
  2426. LOG_ERROR("Lookup wasi _start function failed: "
  2427. "invalid function type.\n");
  2428. return NULL;
  2429. }
  2430. return (WASMFunctionInstanceCommon *)func;
  2431. }
  2432. }
  2433. return NULL;
  2434. }
  2435. #endif
  2436. #if WASM_ENABLE_AOT != 0
  2437. if (module_inst->module_type == Wasm_Module_AoT) {
  2438. AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
  2439. AOTFunctionInstance *export_funcs =
  2440. (AOTFunctionInstance *)aot_inst->export_funcs.ptr;
  2441. for (i = 0; i < aot_inst->export_func_count; i++) {
  2442. if (!strcmp(export_funcs[i].func_name, "_start")) {
  2443. AOTFuncType *func_type = export_funcs[i].u.func.func_type;
  2444. if (func_type->param_count != 0
  2445. || func_type->result_count != 0) {
  2446. LOG_ERROR("Lookup wasi _start function failed: "
  2447. "invalid function type.\n");
  2448. return NULL;
  2449. }
  2450. return (WASMFunctionInstanceCommon *)&export_funcs[i];
  2451. }
  2452. }
  2453. return NULL;
  2454. }
  2455. #endif /* end of WASM_ENABLE_AOT */
  2456. return NULL;
  2457. }
  2458. #if WASM_ENABLE_UVWASI == 0
  2459. void
  2460. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  2461. {
  2462. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  2463. if (wasi_ctx) {
  2464. if (wasi_ctx->argv_environ) {
  2465. argv_environ_destroy(wasi_ctx->argv_environ);
  2466. wasm_runtime_free(wasi_ctx->argv_environ);
  2467. }
  2468. if (wasi_ctx->curfds) {
  2469. fd_table_destroy(wasi_ctx->curfds);
  2470. wasm_runtime_free(wasi_ctx->curfds);
  2471. }
  2472. if (wasi_ctx->prestats) {
  2473. fd_prestats_destroy(wasi_ctx->prestats);
  2474. wasm_runtime_free(wasi_ctx->prestats);
  2475. }
  2476. if (wasi_ctx->addr_pool) {
  2477. addr_pool_destroy(wasi_ctx->addr_pool);
  2478. wasm_runtime_free(wasi_ctx->addr_pool);
  2479. }
  2480. if (wasi_ctx->argv_buf)
  2481. wasm_runtime_free(wasi_ctx->argv_buf);
  2482. if (wasi_ctx->argv_list)
  2483. wasm_runtime_free(wasi_ctx->argv_list);
  2484. if (wasi_ctx->env_buf)
  2485. wasm_runtime_free(wasi_ctx->env_buf);
  2486. if (wasi_ctx->env_list)
  2487. wasm_runtime_free(wasi_ctx->env_list);
  2488. wasm_runtime_free(wasi_ctx);
  2489. }
  2490. }
  2491. #else
  2492. void
  2493. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  2494. {
  2495. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  2496. if (wasi_ctx) {
  2497. uvwasi_destroy(wasi_ctx);
  2498. wasm_runtime_free(wasi_ctx);
  2499. }
  2500. }
  2501. #endif
  2502. WASIContext *
  2503. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst)
  2504. {
  2505. #if WASM_ENABLE_INTERP != 0
  2506. if (module_inst->module_type == Wasm_Module_Bytecode)
  2507. return ((WASMModuleInstance *)module_inst)->wasi_ctx;
  2508. #endif
  2509. #if WASM_ENABLE_AOT != 0
  2510. if (module_inst->module_type == Wasm_Module_AoT)
  2511. return ((AOTModuleInstance *)module_inst)->wasi_ctx.ptr;
  2512. #endif
  2513. return NULL;
  2514. }
  2515. void
  2516. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  2517. WASIContext *wasi_ctx)
  2518. {
  2519. #if WASM_ENABLE_INTERP != 0
  2520. if (module_inst->module_type == Wasm_Module_Bytecode)
  2521. ((WASMModuleInstance *)module_inst)->wasi_ctx = wasi_ctx;
  2522. #endif
  2523. #if WASM_ENABLE_AOT != 0
  2524. if (module_inst->module_type == Wasm_Module_AoT)
  2525. ((AOTModuleInstance *)module_inst)->wasi_ctx.ptr = wasi_ctx;
  2526. #endif
  2527. }
  2528. #endif /* end of WASM_ENABLE_LIBC_WASI */
  2529. WASMModuleCommon *
  2530. wasm_exec_env_get_module(WASMExecEnv *exec_env)
  2531. {
  2532. WASMModuleInstanceCommon *module_inst =
  2533. wasm_runtime_get_module_inst(exec_env);
  2534. #if WASM_ENABLE_INTERP != 0
  2535. if (module_inst->module_type == Wasm_Module_Bytecode)
  2536. return (WASMModuleCommon *)((WASMModuleInstance *)module_inst)->module;
  2537. #endif
  2538. #if WASM_ENABLE_AOT != 0
  2539. if (module_inst->module_type == Wasm_Module_AoT)
  2540. return (WASMModuleCommon *)((AOTModuleInstance *)module_inst)
  2541. ->aot_module.ptr;
  2542. #endif
  2543. return NULL;
  2544. }
  2545. static union {
  2546. int a;
  2547. char b;
  2548. } __ue = { .a = 1 };
  2549. #define is_little_endian() (__ue.b == 1)
  2550. bool
  2551. wasm_runtime_register_natives(const char *module_name,
  2552. NativeSymbol *native_symbols,
  2553. uint32 n_native_symbols)
  2554. {
  2555. return wasm_native_register_natives(module_name, native_symbols,
  2556. n_native_symbols);
  2557. }
  2558. bool
  2559. wasm_runtime_register_natives_raw(const char *module_name,
  2560. NativeSymbol *native_symbols,
  2561. uint32 n_native_symbols)
  2562. {
  2563. return wasm_native_register_natives_raw(module_name, native_symbols,
  2564. n_native_symbols);
  2565. }
  2566. bool
  2567. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  2568. const WASMType *func_type, const char *signature,
  2569. void *attachment, uint32 *argv, uint32 argc,
  2570. uint32 *argv_ret)
  2571. {
  2572. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2573. typedef void (*NativeRawFuncPtr)(WASMExecEnv *, uint64 *);
  2574. NativeRawFuncPtr invokeNativeRaw = (NativeRawFuncPtr)func_ptr;
  2575. uint64 argv_buf[16] = { 0 }, *argv1 = argv_buf, *argv_dst, size;
  2576. uint32 *argv_src = argv, i, argc1, ptr_len;
  2577. uint32 arg_i32;
  2578. bool ret = false;
  2579. argc1 = func_type->param_count;
  2580. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  2581. size = sizeof(uint64) * (uint64)argc1;
  2582. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  2583. 0))) {
  2584. return false;
  2585. }
  2586. }
  2587. argv_dst = argv1;
  2588. /* Traverse secondly to fill in each argument */
  2589. for (i = 0; i < func_type->param_count; i++, argv_dst++) {
  2590. switch (func_type->types[i]) {
  2591. case VALUE_TYPE_I32:
  2592. #if WASM_ENABLE_REF_TYPES != 0
  2593. case VALUE_TYPE_FUNCREF:
  2594. #endif
  2595. {
  2596. *(uint32 *)argv_dst = arg_i32 = *argv_src++;
  2597. if (signature) {
  2598. if (signature[i + 1] == '*') {
  2599. /* param is a pointer */
  2600. if (signature[i + 2] == '~')
  2601. /* pointer with length followed */
  2602. ptr_len = *argv_src;
  2603. else
  2604. /* pointer without length followed */
  2605. ptr_len = 1;
  2606. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  2607. ptr_len))
  2608. goto fail;
  2609. *(uintptr_t *)argv_dst =
  2610. (uintptr_t)wasm_runtime_addr_app_to_native(module,
  2611. arg_i32);
  2612. }
  2613. else if (signature[i + 1] == '$') {
  2614. /* param is a string */
  2615. if (!wasm_runtime_validate_app_str_addr(module,
  2616. arg_i32))
  2617. goto fail;
  2618. *(uintptr_t *)argv_dst =
  2619. (uintptr_t)wasm_runtime_addr_app_to_native(module,
  2620. arg_i32);
  2621. }
  2622. }
  2623. break;
  2624. }
  2625. case VALUE_TYPE_I64:
  2626. case VALUE_TYPE_F64:
  2627. bh_memcpy_s(argv_dst, sizeof(uint64), argv_src,
  2628. sizeof(uint32) * 2);
  2629. argv_src += 2;
  2630. break;
  2631. case VALUE_TYPE_F32:
  2632. *(float32 *)argv_dst = *(float32 *)argv_src++;
  2633. break;
  2634. #if WASM_ENABLE_REF_TYPES != 0
  2635. case VALUE_TYPE_EXTERNREF:
  2636. {
  2637. uint32 externref_idx = *argv_src++;
  2638. void *externref_obj;
  2639. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  2640. goto fail;
  2641. bh_memcpy_s(argv_dst, sizeof(uintptr_t), argv_src,
  2642. sizeof(uintptr_t));
  2643. break;
  2644. }
  2645. #endif
  2646. default:
  2647. bh_assert(0);
  2648. break;
  2649. }
  2650. }
  2651. exec_env->attachment = attachment;
  2652. invokeNativeRaw(exec_env, argv1);
  2653. exec_env->attachment = NULL;
  2654. if (func_type->result_count > 0) {
  2655. switch (func_type->types[func_type->param_count]) {
  2656. case VALUE_TYPE_I32:
  2657. #if WASM_ENABLE_REF_TYPES != 0
  2658. case VALUE_TYPE_FUNCREF:
  2659. #endif
  2660. argv_ret[0] = *(uint32 *)argv1;
  2661. break;
  2662. case VALUE_TYPE_F32:
  2663. *(float32 *)argv_ret = *(float32 *)argv1;
  2664. break;
  2665. case VALUE_TYPE_I64:
  2666. case VALUE_TYPE_F64:
  2667. bh_memcpy_s(argv_ret, sizeof(uint32) * 2, argv1,
  2668. sizeof(uint64));
  2669. break;
  2670. #if WASM_ENABLE_REF_TYPES != 0
  2671. case VALUE_TYPE_EXTERNREF:
  2672. {
  2673. uint32 externref_idx;
  2674. uint64 externref_obj;
  2675. bh_memcpy_s(&externref_obj, sizeof(uint64), argv1,
  2676. sizeof(uint64));
  2677. if (!wasm_externref_obj2ref(exec_env->module_inst,
  2678. (void *)(uintptr_t)externref_obj,
  2679. &externref_idx))
  2680. goto fail;
  2681. argv_ret[0] = externref_idx;
  2682. break;
  2683. }
  2684. #endif
  2685. default:
  2686. bh_assert(0);
  2687. break;
  2688. }
  2689. }
  2690. ret = !wasm_runtime_get_exception(module) ? true : false;
  2691. fail:
  2692. if (argv1 != argv_buf)
  2693. wasm_runtime_free(argv1);
  2694. return ret;
  2695. }
  2696. /**
  2697. * Implementation of wasm_runtime_invoke_native()
  2698. */
  2699. /* The invoke native implementation on ARM platform with VFP co-processor */
  2700. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) \
  2701. || defined(BUILD_TARGET_RISCV32_ILP32D) \
  2702. || defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  2703. typedef void (*GenericFunctionPointer)();
  2704. int64
  2705. invokeNative(GenericFunctionPointer f, uint32 *args, uint32 n_stacks);
  2706. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  2707. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  2708. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  2709. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  2710. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  2711. static Float64FuncPtr invokeNative_Float64 =
  2712. (Float64FuncPtr)(uintptr_t)invokeNative;
  2713. static Float32FuncPtr invokeNative_Float32 =
  2714. (Float32FuncPtr)(uintptr_t)invokeNative;
  2715. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)(uintptr_t)invokeNative;
  2716. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)(uintptr_t)invokeNative;
  2717. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)(uintptr_t)invokeNative;
  2718. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  2719. #define MAX_REG_INTS 4
  2720. #define MAX_REG_FLOATS 16
  2721. #else
  2722. #define MAX_REG_INTS 8
  2723. #define MAX_REG_FLOATS 8
  2724. #endif
  2725. bool
  2726. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  2727. const WASMType *func_type, const char *signature,
  2728. void *attachment, uint32 *argv, uint32 argc,
  2729. uint32 *argv_ret)
  2730. {
  2731. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2732. /* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args */
  2733. uint32 argv_buf[32], *argv1 = argv_buf, *ints, *stacks, size;
  2734. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  2735. uint32 arg_i32, ptr_len;
  2736. uint32 result_count = func_type->result_count;
  2737. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  2738. bool ret = false;
  2739. #if WASM_ENABLE_REF_TYPES != 0
  2740. bool is_aot_func = (NULL == signature);
  2741. #endif
  2742. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  2743. uint32 *fps;
  2744. int n_fps = 0;
  2745. #else
  2746. #define fps ints
  2747. #define n_fps n_ints
  2748. #endif
  2749. n_ints++; /* exec env */
  2750. /* Traverse firstly to calculate stack args count */
  2751. for (i = 0; i < func_type->param_count; i++) {
  2752. switch (func_type->types[i]) {
  2753. case VALUE_TYPE_I32:
  2754. #if WASM_ENABLE_REF_TYPES != 0
  2755. case VALUE_TYPE_FUNCREF:
  2756. case VALUE_TYPE_EXTERNREF:
  2757. #endif
  2758. if (n_ints < MAX_REG_INTS)
  2759. n_ints++;
  2760. else
  2761. n_stacks++;
  2762. break;
  2763. case VALUE_TYPE_I64:
  2764. if (n_ints < MAX_REG_INTS - 1) {
  2765. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  2766. /* 64-bit data must be 8 bytes aligned in arm */
  2767. if (n_ints & 1)
  2768. n_ints++;
  2769. #endif
  2770. n_ints += 2;
  2771. }
  2772. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  2773. || defined(BUILD_TARGET_RISCV32_ILP32D) || defined(BUILD_TARGET_ARC)
  2774. /* part in register, part in stack */
  2775. else if (n_ints == MAX_REG_INTS - 1) {
  2776. n_ints++;
  2777. n_stacks++;
  2778. }
  2779. #endif
  2780. else {
  2781. /* 64-bit data in stack must be 8 bytes aligned
  2782. in arm and riscv32 */
  2783. #if !defined(BUILD_TARGET_ARC)
  2784. if (n_stacks & 1)
  2785. n_stacks++;
  2786. #endif
  2787. n_stacks += 2;
  2788. }
  2789. break;
  2790. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  2791. case VALUE_TYPE_F32:
  2792. if (n_fps < MAX_REG_FLOATS)
  2793. n_fps++;
  2794. else
  2795. n_stacks++;
  2796. break;
  2797. case VALUE_TYPE_F64:
  2798. if (n_fps < MAX_REG_FLOATS - 1) {
  2799. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  2800. /* 64-bit data must be 8 bytes aligned in arm */
  2801. if (n_fps & 1)
  2802. n_fps++;
  2803. #endif
  2804. n_fps += 2;
  2805. }
  2806. #if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  2807. else if (n_fps == MAX_REG_FLOATS - 1) {
  2808. n_fps++;
  2809. n_stacks++;
  2810. }
  2811. #endif
  2812. else {
  2813. /* 64-bit data in stack must be 8 bytes aligned
  2814. in arm and riscv32 */
  2815. #if !defined(BUILD_TARGET_ARC)
  2816. if (n_stacks & 1)
  2817. n_stacks++;
  2818. #endif
  2819. n_stacks += 2;
  2820. }
  2821. break;
  2822. #else /* BUILD_TARGET_RISCV32_ILP32D */
  2823. case VALUE_TYPE_F32:
  2824. case VALUE_TYPE_F64:
  2825. if (n_fps < MAX_REG_FLOATS) {
  2826. n_fps++;
  2827. }
  2828. else if (func_type->types[i] == VALUE_TYPE_F32
  2829. && n_ints < MAX_REG_INTS) {
  2830. /* use int reg firstly if available */
  2831. n_ints++;
  2832. }
  2833. else if (func_type->types[i] == VALUE_TYPE_F64
  2834. && n_ints < MAX_REG_INTS - 1) {
  2835. /* use int regs firstly if available */
  2836. if (n_ints & 1)
  2837. n_ints++;
  2838. ints += 2;
  2839. }
  2840. else {
  2841. /* 64-bit data in stack must be 8 bytes aligned in riscv32
  2842. */
  2843. if (n_stacks & 1)
  2844. n_stacks++;
  2845. n_stacks += 2;
  2846. }
  2847. break;
  2848. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  2849. default:
  2850. bh_assert(0);
  2851. break;
  2852. }
  2853. }
  2854. for (i = 0; i < ext_ret_count; i++) {
  2855. if (n_ints < MAX_REG_INTS)
  2856. n_ints++;
  2857. else
  2858. n_stacks++;
  2859. }
  2860. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  2861. argc1 = MAX_REG_INTS + MAX_REG_FLOATS + n_stacks;
  2862. #elif defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  2863. argc1 = MAX_REG_INTS + n_stacks;
  2864. #else /* for BUILD_TARGET_RISCV32_ILP32D */
  2865. argc1 = MAX_REG_INTS + MAX_REG_FLOATS * 2 + n_stacks;
  2866. #endif
  2867. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  2868. size = sizeof(uint32) * (uint32)argc1;
  2869. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  2870. 0))) {
  2871. return false;
  2872. }
  2873. }
  2874. ints = argv1;
  2875. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  2876. fps = ints + MAX_REG_INTS;
  2877. stacks = fps + MAX_REG_FLOATS;
  2878. #elif defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  2879. stacks = ints + MAX_REG_INTS;
  2880. #else /* for BUILD_TARGET_RISCV32_ILP32D */
  2881. fps = ints + MAX_REG_INTS;
  2882. stacks = fps + MAX_REG_FLOATS * 2;
  2883. #endif
  2884. n_ints = 0;
  2885. n_fps = 0;
  2886. n_stacks = 0;
  2887. ints[n_ints++] = (uint32)(uintptr_t)exec_env;
  2888. /* Traverse secondly to fill in each argument */
  2889. for (i = 0; i < func_type->param_count; i++) {
  2890. switch (func_type->types[i]) {
  2891. case VALUE_TYPE_I32:
  2892. #if WASM_ENABLE_REF_TYPES != 0
  2893. case VALUE_TYPE_FUNCREF:
  2894. #endif
  2895. {
  2896. arg_i32 = *argv_src++;
  2897. if (signature) {
  2898. if (signature[i + 1] == '*') {
  2899. /* param is a pointer */
  2900. if (signature[i + 2] == '~')
  2901. /* pointer with length followed */
  2902. ptr_len = *argv_src;
  2903. else
  2904. /* pointer without length followed */
  2905. ptr_len = 1;
  2906. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  2907. ptr_len))
  2908. goto fail;
  2909. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  2910. module, arg_i32);
  2911. }
  2912. else if (signature[i + 1] == '$') {
  2913. /* param is a string */
  2914. if (!wasm_runtime_validate_app_str_addr(module,
  2915. arg_i32))
  2916. goto fail;
  2917. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  2918. module, arg_i32);
  2919. }
  2920. }
  2921. if (n_ints < MAX_REG_INTS)
  2922. ints[n_ints++] = arg_i32;
  2923. else
  2924. stacks[n_stacks++] = arg_i32;
  2925. break;
  2926. }
  2927. case VALUE_TYPE_I64:
  2928. {
  2929. if (n_ints < MAX_REG_INTS - 1) {
  2930. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  2931. /* 64-bit data must be 8 bytes aligned in arm */
  2932. if (n_ints & 1)
  2933. n_ints++;
  2934. #endif
  2935. ints[n_ints++] = *argv_src++;
  2936. ints[n_ints++] = *argv_src++;
  2937. }
  2938. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  2939. || defined(BUILD_TARGET_RISCV32_ILP32D) || defined(BUILD_TARGET_ARC)
  2940. else if (n_ints == MAX_REG_INTS - 1) {
  2941. ints[n_ints++] = *argv_src++;
  2942. stacks[n_stacks++] = *argv_src++;
  2943. }
  2944. #endif
  2945. else {
  2946. /* 64-bit data in stack must be 8 bytes aligned
  2947. in arm and riscv32 */
  2948. #if !defined(BUILD_TARGET_ARC)
  2949. if (n_stacks & 1)
  2950. n_stacks++;
  2951. #endif
  2952. stacks[n_stacks++] = *argv_src++;
  2953. stacks[n_stacks++] = *argv_src++;
  2954. }
  2955. break;
  2956. }
  2957. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  2958. case VALUE_TYPE_F32:
  2959. {
  2960. if (n_fps < MAX_REG_FLOATS)
  2961. *(float32 *)&fps[n_fps++] = *(float32 *)argv_src++;
  2962. else
  2963. *(float32 *)&stacks[n_stacks++] = *(float32 *)argv_src++;
  2964. break;
  2965. }
  2966. case VALUE_TYPE_F64:
  2967. {
  2968. if (n_fps < MAX_REG_FLOATS - 1) {
  2969. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  2970. /* 64-bit data must be 8 bytes aligned in arm */
  2971. if (n_fps & 1)
  2972. n_fps++;
  2973. #endif
  2974. fps[n_fps++] = *argv_src++;
  2975. fps[n_fps++] = *argv_src++;
  2976. }
  2977. #if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  2978. else if (n_fps == MAX_REG_FLOATS - 1) {
  2979. fps[n_fps++] = *argv_src++;
  2980. stacks[n_stacks++] = *argv_src++;
  2981. }
  2982. #endif
  2983. else {
  2984. /* 64-bit data in stack must be 8 bytes aligned
  2985. in arm and riscv32 */
  2986. #if !defined(BUILD_TARGET_ARC)
  2987. if (n_stacks & 1)
  2988. n_stacks++;
  2989. #endif
  2990. stacks[n_stacks++] = *argv_src++;
  2991. stacks[n_stacks++] = *argv_src++;
  2992. }
  2993. break;
  2994. }
  2995. #else /* BUILD_TARGET_RISCV32_ILP32D */
  2996. case VALUE_TYPE_F32:
  2997. case VALUE_TYPE_F64:
  2998. {
  2999. if (n_fps < MAX_REG_FLOATS) {
  3000. if (func_type->types[i] == VALUE_TYPE_F32) {
  3001. *(float32 *)&fps[n_fps * 2] = *(float32 *)argv_src++;
  3002. /* NaN boxing, the upper bits of a valid NaN-boxed
  3003. value must be all 1s. */
  3004. fps[n_fps * 2 + 1] = 0xFFFFFFFF;
  3005. }
  3006. else {
  3007. *(float64 *)&fps[n_fps * 2] = *(float64 *)argv_src;
  3008. argv_src += 2;
  3009. }
  3010. n_fps++;
  3011. }
  3012. else if (func_type->types[i] == VALUE_TYPE_F32
  3013. && n_ints < MAX_REG_INTS) {
  3014. /* use int reg firstly if available */
  3015. *(float32 *)&ints[n_ints++] = *(float32 *)argv_src++;
  3016. }
  3017. else if (func_type->types[i] == VALUE_TYPE_F64
  3018. && n_ints < MAX_REG_INTS - 1) {
  3019. /* use int regs firstly if available */
  3020. if (n_ints & 1)
  3021. n_ints++;
  3022. *(float64 *)&ints[n_ints] = *(float64 *)argv_src;
  3023. n_ints += 2;
  3024. argv_src += 2;
  3025. }
  3026. else {
  3027. /* 64-bit data in stack must be 8 bytes aligned in riscv32
  3028. */
  3029. if (n_stacks & 1)
  3030. n_stacks++;
  3031. if (func_type->types[i] == VALUE_TYPE_F32) {
  3032. *(float32 *)&stacks[n_stacks] = *(float32 *)argv_src++;
  3033. /* NaN boxing, the upper bits of a valid NaN-boxed
  3034. value must be all 1s. */
  3035. stacks[n_stacks + 1] = 0xFFFFFFFF;
  3036. }
  3037. else {
  3038. *(float64 *)&stacks[n_stacks] = *(float64 *)argv_src;
  3039. argv_src += 2;
  3040. }
  3041. n_stacks += 2;
  3042. }
  3043. break;
  3044. }
  3045. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  3046. #if WASM_ENABLE_REF_TYPES != 0
  3047. case VALUE_TYPE_EXTERNREF:
  3048. {
  3049. uint32 externref_idx = *argv_src++;
  3050. if (is_aot_func) {
  3051. if (n_ints < MAX_REG_INTS)
  3052. ints[n_ints++] = externref_idx;
  3053. else
  3054. stacks[n_stacks++] = externref_idx;
  3055. }
  3056. else {
  3057. void *externref_obj;
  3058. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  3059. goto fail;
  3060. if (n_ints < MAX_REG_INTS)
  3061. ints[n_ints++] = (uintptr_t)externref_obj;
  3062. else
  3063. stacks[n_stacks++] = (uintptr_t)externref_obj;
  3064. }
  3065. break;
  3066. }
  3067. #endif
  3068. default:
  3069. bh_assert(0);
  3070. break;
  3071. }
  3072. }
  3073. /* Save extra result values' address to argv1 */
  3074. for (i = 0; i < ext_ret_count; i++) {
  3075. if (n_ints < MAX_REG_INTS)
  3076. ints[n_ints++] = *(uint32 *)argv_src++;
  3077. else
  3078. stacks[n_stacks++] = *(uint32 *)argv_src++;
  3079. }
  3080. exec_env->attachment = attachment;
  3081. if (func_type->result_count == 0) {
  3082. invokeNative_Void(func_ptr, argv1, n_stacks);
  3083. }
  3084. else {
  3085. switch (func_type->types[func_type->param_count]) {
  3086. case VALUE_TYPE_I32:
  3087. #if WASM_ENABLE_REF_TYPES != 0
  3088. case VALUE_TYPE_FUNCREF:
  3089. #endif
  3090. argv_ret[0] =
  3091. (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  3092. break;
  3093. case VALUE_TYPE_I64:
  3094. PUT_I64_TO_ADDR(argv_ret,
  3095. invokeNative_Int64(func_ptr, argv1, n_stacks));
  3096. break;
  3097. case VALUE_TYPE_F32:
  3098. *(float32 *)argv_ret =
  3099. invokeNative_Float32(func_ptr, argv1, n_stacks);
  3100. break;
  3101. case VALUE_TYPE_F64:
  3102. PUT_F64_TO_ADDR(
  3103. argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  3104. break;
  3105. #if WASM_ENABLE_REF_TYPES != 0
  3106. case VALUE_TYPE_EXTERNREF:
  3107. {
  3108. if (is_aot_func) {
  3109. uint32 externref_idx =
  3110. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  3111. argv_ret[0] = externref_idx;
  3112. }
  3113. else {
  3114. uint32 externref_idx;
  3115. void *externref_obj;
  3116. externref_obj = (void *)(uintptr_t)invokeNative_Int32(
  3117. func_ptr, argv1, argc1);
  3118. if (!wasm_externref_obj2ref(exec_env->module_inst,
  3119. externref_obj, &externref_idx))
  3120. goto fail;
  3121. argv_ret[0] = externref_idx;
  3122. }
  3123. break;
  3124. }
  3125. #endif
  3126. default:
  3127. bh_assert(0);
  3128. break;
  3129. }
  3130. }
  3131. exec_env->attachment = NULL;
  3132. ret = !wasm_runtime_get_exception(module) ? true : false;
  3133. fail:
  3134. if (argv1 != argv_buf)
  3135. wasm_runtime_free(argv1);
  3136. return ret;
  3137. }
  3138. #endif /* end of defined(BUILD_TARGET_ARM_VFP) \
  3139. || defined(BUILD_TARGET_THUMB_VFP) \
  3140. || defined(BUILD_TARGET_RISCV32_ILP32D) \
  3141. || defined(BUILD_TARGET_RISCV32_ILP32) \
  3142. || defined(BUILD_TARGET_ARC) */
  3143. #if defined(BUILD_TARGET_X86_32) || defined(BUILD_TARGET_ARM) \
  3144. || defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_MIPS) \
  3145. || defined(BUILD_TARGET_XTENSA)
  3146. typedef void (*GenericFunctionPointer)();
  3147. int64
  3148. invokeNative(GenericFunctionPointer f, uint32 *args, uint32 sz);
  3149. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3150. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3151. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3152. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3153. typedef void (*VoidFuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3154. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)invokeNative;
  3155. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)(uintptr_t)invokeNative;
  3156. static Float64FuncPtr invokeNative_Float64 =
  3157. (Float64FuncPtr)(uintptr_t)invokeNative;
  3158. static Float32FuncPtr invokeNative_Float32 =
  3159. (Float32FuncPtr)(uintptr_t)invokeNative;
  3160. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)(uintptr_t)invokeNative;
  3161. static inline void
  3162. word_copy(uint32 *dest, uint32 *src, unsigned num)
  3163. {
  3164. for (; num > 0; num--)
  3165. *dest++ = *src++;
  3166. }
  3167. bool
  3168. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  3169. const WASMType *func_type, const char *signature,
  3170. void *attachment, uint32 *argv, uint32 argc,
  3171. uint32 *argv_ret)
  3172. {
  3173. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  3174. uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0;
  3175. uint32 arg_i32, ptr_len;
  3176. uint32 result_count = func_type->result_count;
  3177. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  3178. uint64 size;
  3179. bool ret = false;
  3180. #if WASM_ENABLE_REF_TYPES != 0
  3181. bool is_aot_func = (NULL == signature);
  3182. #endif
  3183. #if defined(BUILD_TARGET_X86_32)
  3184. argc1 = argc + ext_ret_count + 2;
  3185. #else
  3186. /* arm/thumb/mips/xtensa, 64-bit data must be 8 bytes aligned,
  3187. so we need to allocate more memory. */
  3188. argc1 = func_type->param_count * 2 + ext_ret_count + 2;
  3189. #endif
  3190. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  3191. size = sizeof(uint32) * (uint64)argc1;
  3192. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  3193. 0))) {
  3194. return false;
  3195. }
  3196. }
  3197. for (i = 0; i < sizeof(WASMExecEnv *) / sizeof(uint32); i++)
  3198. argv1[j++] = ((uint32 *)&exec_env)[i];
  3199. for (i = 0; i < func_type->param_count; i++) {
  3200. switch (func_type->types[i]) {
  3201. case VALUE_TYPE_I32:
  3202. #if WASM_ENABLE_REF_TYPES != 0
  3203. case VALUE_TYPE_FUNCREF:
  3204. #endif
  3205. {
  3206. arg_i32 = *argv++;
  3207. if (signature) {
  3208. if (signature[i + 1] == '*') {
  3209. /* param is a pointer */
  3210. if (signature[i + 2] == '~')
  3211. /* pointer with length followed */
  3212. ptr_len = *argv;
  3213. else
  3214. /* pointer without length followed */
  3215. ptr_len = 1;
  3216. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  3217. ptr_len))
  3218. goto fail;
  3219. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3220. module, arg_i32);
  3221. }
  3222. else if (signature[i + 1] == '$') {
  3223. /* param is a string */
  3224. if (!wasm_runtime_validate_app_str_addr(module,
  3225. arg_i32))
  3226. goto fail;
  3227. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3228. module, arg_i32);
  3229. }
  3230. }
  3231. argv1[j++] = arg_i32;
  3232. break;
  3233. }
  3234. case VALUE_TYPE_I64:
  3235. case VALUE_TYPE_F64:
  3236. #if !defined(BUILD_TARGET_X86_32)
  3237. /* 64-bit data must be 8 bytes aligned in arm, thumb, mips
  3238. and xtensa */
  3239. if (j & 1)
  3240. j++;
  3241. #endif
  3242. argv1[j++] = *argv++;
  3243. argv1[j++] = *argv++;
  3244. break;
  3245. case VALUE_TYPE_F32:
  3246. argv1[j++] = *argv++;
  3247. break;
  3248. #if WASM_ENABLE_REF_TYPES != 0
  3249. case VALUE_TYPE_EXTERNREF:
  3250. {
  3251. uint32 externref_idx = *argv++;
  3252. if (is_aot_func) {
  3253. argv1[j++] = externref_idx;
  3254. }
  3255. else {
  3256. void *externref_obj;
  3257. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  3258. goto fail;
  3259. argv1[j++] = (uintptr_t)externref_obj;
  3260. }
  3261. break;
  3262. }
  3263. #endif
  3264. default:
  3265. bh_assert(0);
  3266. break;
  3267. }
  3268. }
  3269. /* Save extra result values' address to argv1 */
  3270. word_copy(argv1 + j, argv, ext_ret_count);
  3271. argc1 = j + ext_ret_count;
  3272. exec_env->attachment = attachment;
  3273. if (func_type->result_count == 0) {
  3274. invokeNative_Void(func_ptr, argv1, argc1);
  3275. }
  3276. else {
  3277. switch (func_type->types[func_type->param_count]) {
  3278. case VALUE_TYPE_I32:
  3279. #if WASM_ENABLE_REF_TYPES != 0
  3280. case VALUE_TYPE_FUNCREF:
  3281. #endif
  3282. argv_ret[0] =
  3283. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  3284. break;
  3285. case VALUE_TYPE_I64:
  3286. PUT_I64_TO_ADDR(argv_ret,
  3287. invokeNative_Int64(func_ptr, argv1, argc1));
  3288. break;
  3289. case VALUE_TYPE_F32:
  3290. *(float32 *)argv_ret =
  3291. invokeNative_Float32(func_ptr, argv1, argc1);
  3292. break;
  3293. case VALUE_TYPE_F64:
  3294. PUT_F64_TO_ADDR(argv_ret,
  3295. invokeNative_Float64(func_ptr, argv1, argc1));
  3296. break;
  3297. #if WASM_ENABLE_REF_TYPES != 0
  3298. case VALUE_TYPE_EXTERNREF:
  3299. {
  3300. if (is_aot_func) {
  3301. uint32 externref_idx =
  3302. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  3303. argv_ret[0] = externref_idx;
  3304. }
  3305. else {
  3306. void *externref_obj = (void *)(uintptr_t)invokeNative_Int32(
  3307. func_ptr, argv1, argc1);
  3308. uint32 externref_idx;
  3309. if (!wasm_externref_obj2ref(exec_env->module_inst,
  3310. externref_obj, &externref_idx))
  3311. goto fail;
  3312. argv_ret[0] = externref_idx;
  3313. }
  3314. break;
  3315. }
  3316. #endif
  3317. default:
  3318. bh_assert(0);
  3319. break;
  3320. }
  3321. }
  3322. exec_env->attachment = NULL;
  3323. ret = !wasm_runtime_get_exception(module) ? true : false;
  3324. fail:
  3325. if (argv1 != argv_buf)
  3326. wasm_runtime_free(argv1);
  3327. return ret;
  3328. }
  3329. #endif /* end of defined(BUILD_TARGET_X86_32) \
  3330. || defined(BUILD_TARGET_ARM) \
  3331. || defined(BUILD_TARGET_THUMB) \
  3332. || defined(BUILD_TARGET_MIPS) \
  3333. || defined(BUILD_TARGET_XTENSA) */
  3334. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  3335. || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
  3336. || defined(BUILD_TARGET_RISCV64_LP64)
  3337. #if WASM_ENABLE_SIMD != 0
  3338. #ifdef v128
  3339. #undef v128
  3340. #endif
  3341. #if defined(_WIN32) || defined(_WIN32_)
  3342. typedef union __declspec(intrin_type) __declspec(align(8)) v128 {
  3343. __int8 m128i_i8[16];
  3344. __int16 m128i_i16[8];
  3345. __int32 m128i_i32[4];
  3346. __int64 m128i_i64[2];
  3347. unsigned __int8 m128i_u8[16];
  3348. unsigned __int16 m128i_u16[8];
  3349. unsigned __int32 m128i_u32[4];
  3350. unsigned __int64 m128i_u64[2];
  3351. } v128;
  3352. #elif defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  3353. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3354. || defined(BUILD_TARGET_RISCV64_LP64)
  3355. typedef long long v128
  3356. __attribute__((__vector_size__(16), __may_alias__, __aligned__(1)));
  3357. #elif defined(BUILD_TARGET_AARCH64)
  3358. #include <arm_neon.h>
  3359. typedef uint32x4_t __m128i;
  3360. #define v128 __m128i
  3361. #endif
  3362. #endif /* end of WASM_ENABLE_SIMD != 0 */
  3363. typedef void (*GenericFunctionPointer)();
  3364. #if defined(__APPLE__) || defined(__MACH__)
  3365. /**
  3366. * Define the return type as 'void' in MacOS, since after converting
  3367. * 'int64 invokeNative' into 'float64 invokeNative_Float64', the
  3368. * return value passing might be invalid, the caller reads the return
  3369. * value from register rax but not xmm0.
  3370. */
  3371. void
  3372. invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks);
  3373. #else
  3374. int64
  3375. invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks);
  3376. #endif
  3377. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3378. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3379. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3380. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3381. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3382. static Float64FuncPtr invokeNative_Float64 =
  3383. (Float64FuncPtr)(uintptr_t)invokeNative;
  3384. static Float32FuncPtr invokeNative_Float32 =
  3385. (Float32FuncPtr)(uintptr_t)invokeNative;
  3386. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)(uintptr_t)invokeNative;
  3387. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)(uintptr_t)invokeNative;
  3388. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)(uintptr_t)invokeNative;
  3389. #if WASM_ENABLE_SIMD != 0
  3390. typedef v128 (*V128FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3391. static V128FuncPtr invokeNative_V128 = (V128FuncPtr)(uintptr_t)invokeNative;
  3392. #endif
  3393. #if defined(_WIN32) || defined(_WIN32_)
  3394. #define MAX_REG_FLOATS 4
  3395. #define MAX_REG_INTS 4
  3396. #else /* else of defined(_WIN32) || defined(_WIN32_) */
  3397. #define MAX_REG_FLOATS 8
  3398. #if defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
  3399. || defined(BUILD_TARGET_RISCV64_LP64)
  3400. #define MAX_REG_INTS 8
  3401. #else
  3402. #define MAX_REG_INTS 6
  3403. #endif /* end of defined(BUILD_TARGET_AARCH64) \
  3404. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3405. || defined(BUILD_TARGET_RISCV64_LP64) */
  3406. #endif /* end of defined(_WIN32) || defined(_WIN32_) */
  3407. bool
  3408. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  3409. const WASMType *func_type, const char *signature,
  3410. void *attachment, uint32 *argv, uint32 argc,
  3411. uint32 *argv_ret)
  3412. {
  3413. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  3414. uint64 argv_buf[32] = { 0 }, *argv1 = argv_buf, *ints, *stacks, size,
  3415. arg_i64;
  3416. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  3417. uint32 arg_i32, ptr_len;
  3418. uint32 result_count = func_type->result_count;
  3419. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  3420. bool ret = false;
  3421. #if WASM_ENABLE_REF_TYPES != 0
  3422. bool is_aot_func = (NULL == signature);
  3423. #endif
  3424. #ifndef BUILD_TARGET_RISCV64_LP64
  3425. #if WASM_ENABLE_SIMD == 0
  3426. uint64 *fps;
  3427. #else
  3428. v128 *fps;
  3429. #endif
  3430. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  3431. #define fps ints
  3432. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  3433. #if defined(_WIN32) || defined(_WIN32_) || defined(BUILD_TARGET_RISCV64_LP64)
  3434. /* important difference in calling conventions */
  3435. #define n_fps n_ints
  3436. #else
  3437. int n_fps = 0;
  3438. #endif
  3439. #if WASM_ENABLE_SIMD == 0
  3440. argc1 = 1 + MAX_REG_FLOATS + (uint32)func_type->param_count + ext_ret_count;
  3441. #else
  3442. argc1 = 1 + MAX_REG_FLOATS * 2 + (uint32)func_type->param_count * 2
  3443. + ext_ret_count;
  3444. #endif
  3445. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  3446. size = sizeof(uint64) * (uint64)argc1;
  3447. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  3448. 0))) {
  3449. return false;
  3450. }
  3451. }
  3452. #ifndef BUILD_TARGET_RISCV64_LP64
  3453. #if WASM_ENABLE_SIMD == 0
  3454. fps = argv1;
  3455. ints = fps + MAX_REG_FLOATS;
  3456. #else
  3457. fps = (v128 *)argv1;
  3458. ints = (uint64 *)(fps + MAX_REG_FLOATS);
  3459. #endif
  3460. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  3461. ints = argv1;
  3462. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  3463. stacks = ints + MAX_REG_INTS;
  3464. ints[n_ints++] = (uint64)(uintptr_t)exec_env;
  3465. for (i = 0; i < func_type->param_count; i++) {
  3466. switch (func_type->types[i]) {
  3467. case VALUE_TYPE_I32:
  3468. #if WASM_ENABLE_REF_TYPES != 0
  3469. case VALUE_TYPE_FUNCREF:
  3470. #endif
  3471. {
  3472. arg_i32 = *argv_src++;
  3473. arg_i64 = arg_i32;
  3474. if (signature) {
  3475. if (signature[i + 1] == '*') {
  3476. /* param is a pointer */
  3477. if (signature[i + 2] == '~')
  3478. /* pointer with length followed */
  3479. ptr_len = *argv_src;
  3480. else
  3481. /* pointer without length followed */
  3482. ptr_len = 1;
  3483. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  3484. ptr_len))
  3485. goto fail;
  3486. arg_i64 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3487. module, arg_i32);
  3488. }
  3489. else if (signature[i + 1] == '$') {
  3490. /* param is a string */
  3491. if (!wasm_runtime_validate_app_str_addr(module,
  3492. arg_i32))
  3493. goto fail;
  3494. arg_i64 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3495. module, arg_i32);
  3496. }
  3497. }
  3498. if (n_ints < MAX_REG_INTS)
  3499. ints[n_ints++] = arg_i64;
  3500. else
  3501. stacks[n_stacks++] = arg_i64;
  3502. break;
  3503. }
  3504. case VALUE_TYPE_I64:
  3505. if (n_ints < MAX_REG_INTS)
  3506. ints[n_ints++] = *(uint64 *)argv_src;
  3507. else
  3508. stacks[n_stacks++] = *(uint64 *)argv_src;
  3509. argv_src += 2;
  3510. break;
  3511. case VALUE_TYPE_F32:
  3512. if (n_fps < MAX_REG_FLOATS) {
  3513. *(float32 *)&fps[n_fps++] = *(float32 *)argv_src++;
  3514. }
  3515. else {
  3516. *(float32 *)&stacks[n_stacks++] = *(float32 *)argv_src++;
  3517. }
  3518. break;
  3519. case VALUE_TYPE_F64:
  3520. if (n_fps < MAX_REG_FLOATS) {
  3521. *(float64 *)&fps[n_fps++] = *(float64 *)argv_src;
  3522. }
  3523. else {
  3524. *(float64 *)&stacks[n_stacks++] = *(float64 *)argv_src;
  3525. }
  3526. argv_src += 2;
  3527. break;
  3528. #if WASM_ENABLE_REF_TYPES != 0
  3529. case VALUE_TYPE_EXTERNREF:
  3530. {
  3531. uint32 externref_idx = *argv_src++;
  3532. if (is_aot_func) {
  3533. if (n_ints < MAX_REG_INTS)
  3534. ints[n_ints++] = externref_idx;
  3535. else
  3536. stacks[n_stacks++] = externref_idx;
  3537. }
  3538. else {
  3539. void *externref_obj;
  3540. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  3541. goto fail;
  3542. if (n_ints < MAX_REG_INTS)
  3543. ints[n_ints++] = (uintptr_t)externref_obj;
  3544. else
  3545. stacks[n_stacks++] = (uintptr_t)externref_obj;
  3546. }
  3547. break;
  3548. }
  3549. #endif
  3550. #if WASM_ENABLE_SIMD != 0
  3551. case VALUE_TYPE_V128:
  3552. if (n_fps < MAX_REG_FLOATS) {
  3553. *(v128 *)&fps[n_fps++] = *(v128 *)argv_src;
  3554. }
  3555. else {
  3556. *(v128 *)&stacks[n_stacks++] = *(v128 *)argv_src;
  3557. n_stacks++;
  3558. }
  3559. argv_src += 4;
  3560. break;
  3561. #endif
  3562. default:
  3563. bh_assert(0);
  3564. break;
  3565. }
  3566. }
  3567. /* Save extra result values' address to argv1 */
  3568. for (i = 0; i < ext_ret_count; i++) {
  3569. if (n_ints < MAX_REG_INTS)
  3570. ints[n_ints++] = *(uint64 *)argv_src;
  3571. else
  3572. stacks[n_stacks++] = *(uint64 *)argv_src;
  3573. argv_src += 2;
  3574. }
  3575. exec_env->attachment = attachment;
  3576. if (result_count == 0) {
  3577. invokeNative_Void(func_ptr, argv1, n_stacks);
  3578. }
  3579. else {
  3580. /* Invoke the native function and get the first result value */
  3581. switch (func_type->types[func_type->param_count]) {
  3582. case VALUE_TYPE_I32:
  3583. #if WASM_ENABLE_REF_TYPES != 0
  3584. case VALUE_TYPE_FUNCREF:
  3585. #endif
  3586. argv_ret[0] =
  3587. (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  3588. break;
  3589. case VALUE_TYPE_I64:
  3590. PUT_I64_TO_ADDR(argv_ret,
  3591. invokeNative_Int64(func_ptr, argv1, n_stacks));
  3592. break;
  3593. case VALUE_TYPE_F32:
  3594. *(float32 *)argv_ret =
  3595. invokeNative_Float32(func_ptr, argv1, n_stacks);
  3596. break;
  3597. case VALUE_TYPE_F64:
  3598. PUT_F64_TO_ADDR(
  3599. argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  3600. break;
  3601. #if WASM_ENABLE_REF_TYPES != 0
  3602. case VALUE_TYPE_EXTERNREF:
  3603. {
  3604. if (is_aot_func) {
  3605. argv_ret[0] = invokeNative_Int32(func_ptr, argv1, n_stacks);
  3606. }
  3607. else {
  3608. uint32 externref_idx;
  3609. void *externref_obj = (void *)(uintptr_t)invokeNative_Int64(
  3610. func_ptr, argv1, n_stacks);
  3611. if (!wasm_externref_obj2ref(exec_env->module_inst,
  3612. externref_obj, &externref_idx))
  3613. goto fail;
  3614. argv_ret[0] = externref_idx;
  3615. }
  3616. break;
  3617. }
  3618. #endif
  3619. #if WASM_ENABLE_SIMD != 0
  3620. case VALUE_TYPE_V128:
  3621. *(v128 *)argv_ret =
  3622. invokeNative_V128(func_ptr, argv1, n_stacks);
  3623. break;
  3624. #endif
  3625. default:
  3626. bh_assert(0);
  3627. break;
  3628. }
  3629. }
  3630. exec_env->attachment = NULL;
  3631. ret = !wasm_runtime_get_exception(module) ? true : false;
  3632. fail:
  3633. if (argv1 != argv_buf)
  3634. wasm_runtime_free(argv1);
  3635. return ret;
  3636. }
  3637. #endif /* end of defined(BUILD_TARGET_X86_64) \
  3638. || defined(BUILD_TARGET_AMD_64) \
  3639. || defined(BUILD_TARGET_AARCH64) \
  3640. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3641. || defined(BUILD_TARGET_RISCV64_LP64) */
  3642. bool
  3643. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_indices,
  3644. uint32 argc, uint32 argv[])
  3645. {
  3646. if (!wasm_runtime_exec_env_check(exec_env)) {
  3647. LOG_ERROR("Invalid exec env stack info.");
  3648. return false;
  3649. }
  3650. /* this function is called from native code, so exec_env->handle and
  3651. exec_env->native_stack_boundary must have been set, we don't set
  3652. it again */
  3653. #if WASM_ENABLE_INTERP != 0
  3654. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  3655. return wasm_call_indirect(exec_env, 0, element_indices, argc, argv);
  3656. #endif
  3657. #if WASM_ENABLE_AOT != 0
  3658. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  3659. return aot_call_indirect(exec_env, 0, element_indices, argc, argv);
  3660. #endif
  3661. return false;
  3662. }
  3663. static void
  3664. exchange_uint32(uint8 *p_data)
  3665. {
  3666. uint8 value = *p_data;
  3667. *p_data = *(p_data + 3);
  3668. *(p_data + 3) = value;
  3669. value = *(p_data + 1);
  3670. *(p_data + 1) = *(p_data + 2);
  3671. *(p_data + 2) = value;
  3672. }
  3673. static void
  3674. exchange_uint64(uint8 *p_data)
  3675. {
  3676. uint32 value;
  3677. value = *(uint32 *)p_data;
  3678. *(uint32 *)p_data = *(uint32 *)(p_data + 4);
  3679. *(uint32 *)(p_data + 4) = value;
  3680. exchange_uint32(p_data);
  3681. exchange_uint32(p_data + 4);
  3682. }
  3683. void
  3684. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2)
  3685. {
  3686. uint64 u1, u2;
  3687. bh_memcpy_s(&u1, 8, bytes, 8);
  3688. bh_memcpy_s(&u2, 8, bytes + 8, 8);
  3689. if (!is_little_endian()) {
  3690. exchange_uint64((uint8 *)&u1);
  3691. exchange_uint64((uint8 *)&u2);
  3692. *ret1 = u2;
  3693. *ret2 = u1;
  3694. }
  3695. else {
  3696. *ret1 = u1;
  3697. *ret2 = u2;
  3698. }
  3699. }
  3700. #if WASM_ENABLE_THREAD_MGR != 0
  3701. typedef struct WASMThreadArg {
  3702. WASMExecEnv *new_exec_env;
  3703. wasm_thread_callback_t callback;
  3704. void *arg;
  3705. } WASMThreadArg;
  3706. WASMExecEnv *
  3707. wasm_runtime_spawn_exec_env(WASMExecEnv *exec_env)
  3708. {
  3709. return wasm_cluster_spawn_exec_env(exec_env);
  3710. }
  3711. void
  3712. wasm_runtime_destroy_spawned_exec_env(WASMExecEnv *exec_env)
  3713. {
  3714. wasm_cluster_destroy_spawned_exec_env(exec_env);
  3715. }
  3716. static void *
  3717. wasm_runtime_thread_routine(void *arg)
  3718. {
  3719. WASMThreadArg *thread_arg = (WASMThreadArg *)arg;
  3720. void *ret;
  3721. bh_assert(thread_arg->new_exec_env);
  3722. ret = thread_arg->callback(thread_arg->new_exec_env, thread_arg->arg);
  3723. wasm_runtime_destroy_spawned_exec_env(thread_arg->new_exec_env);
  3724. wasm_runtime_free(thread_arg);
  3725. os_thread_exit(ret);
  3726. return ret;
  3727. }
  3728. int32
  3729. wasm_runtime_spawn_thread(WASMExecEnv *exec_env, wasm_thread_t *tid,
  3730. wasm_thread_callback_t callback, void *arg)
  3731. {
  3732. WASMExecEnv *new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
  3733. WASMThreadArg *thread_arg;
  3734. int32 ret;
  3735. if (!new_exec_env)
  3736. return -1;
  3737. if (!(thread_arg = wasm_runtime_malloc(sizeof(WASMThreadArg)))) {
  3738. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  3739. return -1;
  3740. }
  3741. thread_arg->new_exec_env = new_exec_env;
  3742. thread_arg->callback = callback;
  3743. thread_arg->arg = arg;
  3744. ret = os_thread_create((korp_tid *)tid, wasm_runtime_thread_routine,
  3745. thread_arg, APP_THREAD_STACK_SIZE_DEFAULT);
  3746. if (ret != 0) {
  3747. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  3748. wasm_runtime_free(thread_arg);
  3749. }
  3750. return ret;
  3751. }
  3752. int32
  3753. wasm_runtime_join_thread(wasm_thread_t tid, void **retval)
  3754. {
  3755. return os_thread_join((korp_tid)tid, retval);
  3756. }
  3757. #endif /* end of WASM_ENABLE_THREAD_MGR */
  3758. #if WASM_ENABLE_REF_TYPES != 0
  3759. static korp_mutex externref_lock;
  3760. static uint32 externref_global_id = 1;
  3761. static HashMap *externref_map;
  3762. typedef struct ExternRefMapNode {
  3763. /* The extern object from runtime embedder */
  3764. void *extern_obj;
  3765. /* The module instance it belongs to */
  3766. WASMModuleInstanceCommon *module_inst;
  3767. /* Whether it is retained */
  3768. bool retained;
  3769. /* Whether it is marked by runtime */
  3770. bool marked;
  3771. } ExternRefMapNode;
  3772. static uint32
  3773. wasm_externref_hash(const void *key)
  3774. {
  3775. uint32 externref_idx = (uint32)(uintptr_t)key;
  3776. return externref_idx;
  3777. }
  3778. static bool
  3779. wasm_externref_equal(void *key1, void *key2)
  3780. {
  3781. uint32 externref_idx1 = (uint32)(uintptr_t)key1;
  3782. uint32 externref_idx2 = (uint32)(uintptr_t)key2;
  3783. return externref_idx1 == externref_idx2 ? true : false;
  3784. }
  3785. static bool
  3786. wasm_externref_map_init()
  3787. {
  3788. if (os_mutex_init(&externref_lock) != 0)
  3789. return false;
  3790. if (!(externref_map = bh_hash_map_create(32, false, wasm_externref_hash,
  3791. wasm_externref_equal, NULL,
  3792. wasm_runtime_free))) {
  3793. os_mutex_destroy(&externref_lock);
  3794. return false;
  3795. }
  3796. externref_global_id = 1;
  3797. return true;
  3798. }
  3799. static void
  3800. wasm_externref_map_destroy()
  3801. {
  3802. bh_hash_map_destroy(externref_map);
  3803. os_mutex_destroy(&externref_lock);
  3804. }
  3805. typedef struct LookupExtObj_UserData {
  3806. ExternRefMapNode node;
  3807. bool found;
  3808. uint32 externref_idx;
  3809. } LookupExtObj_UserData;
  3810. static void
  3811. lookup_extobj_callback(void *key, void *value, void *user_data)
  3812. {
  3813. uint32 externref_idx = (uint32)(uintptr_t)key;
  3814. ExternRefMapNode *node = (ExternRefMapNode *)value;
  3815. LookupExtObj_UserData *user_data_lookup =
  3816. (LookupExtObj_UserData *)user_data;
  3817. if (node->extern_obj == user_data_lookup->node.extern_obj
  3818. && node->module_inst == user_data_lookup->node.module_inst) {
  3819. user_data_lookup->found = true;
  3820. user_data_lookup->externref_idx = externref_idx;
  3821. }
  3822. }
  3823. bool
  3824. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
  3825. uint32 *p_externref_idx)
  3826. {
  3827. LookupExtObj_UserData lookup_user_data = { 0 };
  3828. ExternRefMapNode *node;
  3829. uint32 externref_idx;
  3830. /*
  3831. * to catch a parameter from `wasm_application_execute_func`,
  3832. * which represents a string 'null'
  3833. */
  3834. #if UINTPTR_MAX == UINT32_MAX
  3835. if ((uint32)-1 == (uintptr_t)extern_obj) {
  3836. #else
  3837. if ((uint64)-1LL == (uintptr_t)extern_obj) {
  3838. #endif
  3839. *p_externref_idx = NULL_REF;
  3840. return true;
  3841. }
  3842. /* in a wrapper, extern_obj could be any value */
  3843. lookup_user_data.node.extern_obj = extern_obj;
  3844. lookup_user_data.node.module_inst = module_inst;
  3845. lookup_user_data.found = false;
  3846. os_mutex_lock(&externref_lock);
  3847. /* Lookup hashmap firstly */
  3848. bh_hash_map_traverse(externref_map, lookup_extobj_callback,
  3849. (void *)&lookup_user_data);
  3850. if (lookup_user_data.found) {
  3851. *p_externref_idx = lookup_user_data.externref_idx;
  3852. os_mutex_unlock(&externref_lock);
  3853. return true;
  3854. }
  3855. /* Not found in hashmap */
  3856. if (externref_global_id == NULL_REF || externref_global_id == 0) {
  3857. goto fail1;
  3858. }
  3859. if (!(node = wasm_runtime_malloc(sizeof(ExternRefMapNode)))) {
  3860. goto fail1;
  3861. }
  3862. memset(node, 0, sizeof(ExternRefMapNode));
  3863. node->extern_obj = extern_obj;
  3864. node->module_inst = module_inst;
  3865. externref_idx = externref_global_id;
  3866. if (!bh_hash_map_insert(externref_map, (void *)(uintptr_t)externref_idx,
  3867. (void *)node)) {
  3868. goto fail2;
  3869. }
  3870. externref_global_id++;
  3871. *p_externref_idx = externref_idx;
  3872. os_mutex_unlock(&externref_lock);
  3873. return true;
  3874. fail2:
  3875. wasm_runtime_free(node);
  3876. fail1:
  3877. os_mutex_unlock(&externref_lock);
  3878. return false;
  3879. }
  3880. bool
  3881. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj)
  3882. {
  3883. ExternRefMapNode *node;
  3884. /* catch a `ref.null` vairable */
  3885. if (externref_idx == NULL_REF) {
  3886. *p_extern_obj = NULL;
  3887. return true;
  3888. }
  3889. os_mutex_lock(&externref_lock);
  3890. node = bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  3891. os_mutex_unlock(&externref_lock);
  3892. if (!node)
  3893. return false;
  3894. *p_extern_obj = node->extern_obj;
  3895. return true;
  3896. }
  3897. static void
  3898. reclaim_extobj_callback(void *key, void *value, void *user_data)
  3899. {
  3900. ExternRefMapNode *node = (ExternRefMapNode *)value;
  3901. WASMModuleInstanceCommon *module_inst =
  3902. (WASMModuleInstanceCommon *)user_data;
  3903. if (node->module_inst == module_inst) {
  3904. if (!node->marked && !node->retained) {
  3905. bh_hash_map_remove(externref_map, key, NULL, NULL);
  3906. wasm_runtime_free(value);
  3907. }
  3908. else {
  3909. node->marked = false;
  3910. }
  3911. }
  3912. }
  3913. static void
  3914. mark_externref(uint32 externref_idx)
  3915. {
  3916. ExternRefMapNode *node;
  3917. if (externref_idx != NULL_REF) {
  3918. node =
  3919. bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  3920. if (node) {
  3921. node->marked = true;
  3922. }
  3923. }
  3924. }
  3925. #if WASM_ENABLE_INTERP != 0
  3926. static void
  3927. interp_mark_all_externrefs(WASMModuleInstance *module_inst)
  3928. {
  3929. uint32 i, j, externref_idx, *table_data;
  3930. uint8 *global_data = module_inst->global_data;
  3931. WASMGlobalInstance *global;
  3932. WASMTableInstance *table;
  3933. global = module_inst->globals;
  3934. for (i = 0; i < module_inst->global_count; i++, global++) {
  3935. if (global->type == VALUE_TYPE_EXTERNREF) {
  3936. externref_idx = *(uint32 *)(global_data + global->data_offset);
  3937. mark_externref(externref_idx);
  3938. }
  3939. }
  3940. for (i = 0; i < module_inst->table_count; i++) {
  3941. table = wasm_get_table_inst(module_inst, i);
  3942. if (table->elem_type == VALUE_TYPE_EXTERNREF) {
  3943. table_data = (uint32 *)table->base_addr;
  3944. for (j = 0; j < table->cur_size; j++) {
  3945. externref_idx = table_data[j];
  3946. mark_externref(externref_idx);
  3947. }
  3948. }
  3949. }
  3950. }
  3951. #endif
  3952. #if WASM_ENABLE_AOT != 0
  3953. static void
  3954. aot_mark_all_externrefs(AOTModuleInstance *module_inst)
  3955. {
  3956. uint32 i = 0, j = 0;
  3957. const AOTModule *module = (AOTModule *)(module_inst->aot_module.ptr);
  3958. const AOTTable *table = module->tables;
  3959. const AOTGlobal *global = module->globals;
  3960. const AOTTableInstance *table_inst =
  3961. (AOTTableInstance *)module_inst->tables.ptr;
  3962. for (i = 0; i < module->global_count; i++, global++) {
  3963. if (global->type == VALUE_TYPE_EXTERNREF) {
  3964. mark_externref(*(uint32 *)((uint8 *)module_inst->global_data.ptr
  3965. + global->data_offset));
  3966. }
  3967. }
  3968. for (i = 0; i < module->table_count;
  3969. i++, table_inst = aot_next_tbl_inst(table_inst)) {
  3970. if ((table + i)->elem_type == VALUE_TYPE_EXTERNREF) {
  3971. while (j < table_inst->cur_size) {
  3972. mark_externref(table_inst->data[j++]);
  3973. }
  3974. }
  3975. }
  3976. }
  3977. #endif
  3978. void
  3979. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst)
  3980. {
  3981. os_mutex_lock(&externref_lock);
  3982. #if WASM_ENABLE_INTERP != 0
  3983. if (module_inst->module_type == Wasm_Module_Bytecode)
  3984. interp_mark_all_externrefs((WASMModuleInstance *)module_inst);
  3985. #endif
  3986. #if WASM_ENABLE_AOT != 0
  3987. if (module_inst->module_type == Wasm_Module_AoT)
  3988. aot_mark_all_externrefs((AOTModuleInstance *)module_inst);
  3989. #endif
  3990. bh_hash_map_traverse(externref_map, reclaim_extobj_callback,
  3991. (void *)module_inst);
  3992. os_mutex_unlock(&externref_lock);
  3993. }
  3994. static void
  3995. cleanup_extobj_callback(void *key, void *value, void *user_data)
  3996. {
  3997. ExternRefMapNode *node = (ExternRefMapNode *)value;
  3998. WASMModuleInstanceCommon *module_inst =
  3999. (WASMModuleInstanceCommon *)user_data;
  4000. if (node->module_inst == module_inst) {
  4001. bh_hash_map_remove(externref_map, key, NULL, NULL);
  4002. wasm_runtime_free(value);
  4003. }
  4004. }
  4005. void
  4006. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst)
  4007. {
  4008. os_mutex_lock(&externref_lock);
  4009. bh_hash_map_traverse(externref_map, cleanup_extobj_callback,
  4010. (void *)module_inst);
  4011. os_mutex_unlock(&externref_lock);
  4012. }
  4013. bool
  4014. wasm_externref_retain(uint32 externref_idx)
  4015. {
  4016. ExternRefMapNode *node;
  4017. os_mutex_lock(&externref_lock);
  4018. if (externref_idx != NULL_REF) {
  4019. node =
  4020. bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  4021. if (node) {
  4022. node->retained = true;
  4023. os_mutex_unlock(&externref_lock);
  4024. return true;
  4025. }
  4026. }
  4027. os_mutex_unlock(&externref_lock);
  4028. return false;
  4029. }
  4030. #endif /* end of WASM_ENABLE_REF_TYPES */
  4031. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  4032. void
  4033. wasm_runtime_dump_call_stack(WASMExecEnv *exec_env)
  4034. {
  4035. WASMModuleInstanceCommon *module_inst =
  4036. wasm_exec_env_get_module_inst(exec_env);
  4037. #if WASM_ENABLE_INTERP != 0
  4038. if (module_inst->module_type == Wasm_Module_Bytecode) {
  4039. wasm_interp_dump_call_stack(exec_env);
  4040. }
  4041. #endif
  4042. #if WASM_ENABLE_AOT != 0
  4043. if (module_inst->module_type == Wasm_Module_AoT) {
  4044. aot_dump_call_stack(exec_env);
  4045. }
  4046. #endif
  4047. }
  4048. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
  4049. bool
  4050. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  4051. const WASMExport *export, WASMType **out)
  4052. {
  4053. #if WASM_ENABLE_INTERP != 0
  4054. if (module_comm->module_type == Wasm_Module_Bytecode) {
  4055. WASMModule *module = (WASMModule *)module_comm;
  4056. if (export->index < module->import_function_count) {
  4057. *out = module->import_functions[export->index].u.function.func_type;
  4058. }
  4059. else {
  4060. *out =
  4061. module->functions[export->index - module->import_function_count]
  4062. ->func_type;
  4063. }
  4064. return true;
  4065. }
  4066. #endif
  4067. #if WASM_ENABLE_AOT != 0
  4068. if (module_comm->module_type == Wasm_Module_AoT) {
  4069. AOTModule *module = (AOTModule *)module_comm;
  4070. if (export->index < module->import_func_count) {
  4071. *out = module->func_types[module->import_funcs[export->index]
  4072. .func_type_index];
  4073. }
  4074. else {
  4075. *out = module->func_types
  4076. [module->func_type_indexes[export->index
  4077. - module->import_func_count]];
  4078. }
  4079. return true;
  4080. }
  4081. #endif
  4082. return false;
  4083. }
  4084. bool
  4085. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  4086. const WASMExport *export,
  4087. uint8 *out_val_type, bool *out_mutability)
  4088. {
  4089. #if WASM_ENABLE_INTERP != 0
  4090. if (module_comm->module_type == Wasm_Module_Bytecode) {
  4091. WASMModule *module = (WASMModule *)module_comm;
  4092. if (export->index < module->import_global_count) {
  4093. WASMGlobalImport *import_global =
  4094. &((module->import_globals + export->index)->u.global);
  4095. *out_val_type = import_global->type;
  4096. *out_mutability = import_global->is_mutable;
  4097. }
  4098. else {
  4099. WASMGlobal *global =
  4100. module->globals + (export->index - module->import_global_count);
  4101. *out_val_type = global->type;
  4102. *out_mutability = global->is_mutable;
  4103. }
  4104. return true;
  4105. }
  4106. #endif
  4107. #if WASM_ENABLE_AOT != 0
  4108. if (module_comm->module_type == Wasm_Module_AoT) {
  4109. AOTModule *module = (AOTModule *)module_comm;
  4110. if (export->index < module->import_global_count) {
  4111. AOTImportGlobal *import_global =
  4112. module->import_globals + export->index;
  4113. *out_val_type = import_global->type;
  4114. *out_mutability = import_global->is_mutable;
  4115. }
  4116. else {
  4117. AOTGlobal *global =
  4118. module->globals + (export->index - module->import_global_count);
  4119. *out_val_type = global->type;
  4120. *out_mutability = global->is_mutable;
  4121. }
  4122. return true;
  4123. }
  4124. #endif
  4125. return false;
  4126. }
  4127. bool
  4128. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  4129. const WASMExport *export,
  4130. uint32 *out_min_page, uint32 *out_max_page)
  4131. {
  4132. #if WASM_ENABLE_INTERP != 0
  4133. if (module_comm->module_type == Wasm_Module_Bytecode) {
  4134. WASMModule *module = (WASMModule *)module_comm;
  4135. if (export->index < module->import_memory_count) {
  4136. WASMMemoryImport *import_memory =
  4137. &((module->import_memories + export->index)->u.memory);
  4138. *out_min_page = import_memory->init_page_count;
  4139. *out_max_page = import_memory->max_page_count;
  4140. }
  4141. else {
  4142. WASMMemory *memory =
  4143. module->memories
  4144. + (export->index - module->import_memory_count);
  4145. *out_min_page = memory->init_page_count;
  4146. *out_max_page = memory->max_page_count;
  4147. }
  4148. return true;
  4149. }
  4150. #endif
  4151. #if WASM_ENABLE_AOT != 0
  4152. if (module_comm->module_type == Wasm_Module_AoT) {
  4153. AOTModule *module = (AOTModule *)module_comm;
  4154. if (export->index < module->import_memory_count) {
  4155. AOTImportMemory *import_memory =
  4156. module->import_memories + export->index;
  4157. *out_min_page = import_memory->mem_init_page_count;
  4158. *out_max_page = import_memory->mem_max_page_count;
  4159. }
  4160. else {
  4161. AOTMemory *memory = module->memories
  4162. + (export->index - module->import_memory_count);
  4163. *out_min_page = memory->mem_init_page_count;
  4164. *out_max_page = memory->mem_max_page_count;
  4165. }
  4166. return true;
  4167. }
  4168. #endif
  4169. return false;
  4170. }
  4171. bool
  4172. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  4173. const WASMExport *export,
  4174. uint8 *out_elem_type, uint32 *out_min_size,
  4175. uint32 *out_max_size)
  4176. {
  4177. #if WASM_ENABLE_INTERP != 0
  4178. if (module_comm->module_type == Wasm_Module_Bytecode) {
  4179. WASMModule *module = (WASMModule *)module_comm;
  4180. if (export->index < module->import_table_count) {
  4181. WASMTableImport *import_table =
  4182. &((module->import_tables + export->index)->u.table);
  4183. *out_elem_type = import_table->elem_type;
  4184. *out_min_size = import_table->init_size;
  4185. *out_max_size = import_table->max_size;
  4186. }
  4187. else {
  4188. WASMTable *table =
  4189. module->tables + (export->index - module->import_table_count);
  4190. *out_elem_type = table->elem_type;
  4191. *out_min_size = table->init_size;
  4192. *out_max_size = table->max_size;
  4193. }
  4194. return true;
  4195. }
  4196. #endif
  4197. #if WASM_ENABLE_AOT != 0
  4198. if (module_comm->module_type == Wasm_Module_AoT) {
  4199. AOTModule *module = (AOTModule *)module_comm;
  4200. if (export->index < module->import_table_count) {
  4201. AOTImportTable *import_table =
  4202. module->import_tables + export->index;
  4203. *out_elem_type = VALUE_TYPE_FUNCREF;
  4204. *out_min_size = import_table->table_init_size;
  4205. *out_max_size = import_table->table_max_size;
  4206. }
  4207. else {
  4208. AOTTable *table =
  4209. module->tables + (export->index - module->import_table_count);
  4210. *out_elem_type = table->elem_type;
  4211. *out_min_size = table->table_init_size;
  4212. *out_max_size = table->table_max_size;
  4213. }
  4214. return true;
  4215. }
  4216. #endif
  4217. return false;
  4218. }
  4219. static inline bool
  4220. argv_to_params(wasm_val_t *out_params, const uint32 *argv, WASMType *func_type)
  4221. {
  4222. wasm_val_t *param = out_params;
  4223. uint32 i = 0, *u32;
  4224. for (i = 0; i < func_type->param_count; i++, param++) {
  4225. switch (func_type->types[i]) {
  4226. case VALUE_TYPE_I32:
  4227. param->kind = WASM_I32;
  4228. param->of.i32 = *argv++;
  4229. break;
  4230. case VALUE_TYPE_I64:
  4231. param->kind = WASM_I64;
  4232. u32 = (uint32 *)&param->of.i64;
  4233. u32[0] = *argv++;
  4234. u32[1] = *argv++;
  4235. break;
  4236. case VALUE_TYPE_F32:
  4237. param->kind = WASM_F32;
  4238. param->of.f32 = *(float32 *)argv++;
  4239. break;
  4240. case VALUE_TYPE_F64:
  4241. param->kind = WASM_F64;
  4242. u32 = (uint32 *)&param->of.i64;
  4243. u32[0] = *argv++;
  4244. u32[1] = *argv++;
  4245. break;
  4246. #if WASM_ENABLE_REF_TYPES != 0
  4247. case VALUE_TYPE_EXTERNREF:
  4248. param->kind = WASM_ANYREF;
  4249. if (!wasm_externref_ref2obj(*argv,
  4250. (void **)&param->of.foreign)) {
  4251. return false;
  4252. }
  4253. argv++;
  4254. break;
  4255. #endif
  4256. default:
  4257. return false;
  4258. }
  4259. }
  4260. return true;
  4261. }
  4262. static inline bool
  4263. results_to_argv(WASMModuleInstanceCommon *module_inst, uint32 *out_argv,
  4264. const wasm_val_t *results, WASMType *func_type)
  4265. {
  4266. const wasm_val_t *result = results;
  4267. uint32 *argv = out_argv, *u32, i;
  4268. uint8 *result_types = func_type->types + func_type->param_count;
  4269. for (i = 0; i < func_type->result_count; i++, result++) {
  4270. switch (result_types[i]) {
  4271. case VALUE_TYPE_I32:
  4272. case VALUE_TYPE_F32:
  4273. *(int32 *)argv++ = result->of.i32;
  4274. break;
  4275. case VALUE_TYPE_I64:
  4276. case VALUE_TYPE_F64:
  4277. u32 = (uint32 *)&result->of.i64;
  4278. *argv++ = u32[0];
  4279. *argv++ = u32[1];
  4280. break;
  4281. #if WASM_ENABLE_REF_TYPES != 0
  4282. case VALUE_TYPE_EXTERNREF:
  4283. if (!wasm_externref_obj2ref(module_inst,
  4284. (void *)result->of.foreign, argv)) {
  4285. return false;
  4286. }
  4287. argv++;
  4288. break;
  4289. #endif
  4290. default:
  4291. return false;
  4292. }
  4293. }
  4294. return true;
  4295. }
  4296. bool
  4297. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  4298. void *func_ptr, WASMType *func_type,
  4299. uint32 argc, uint32 *argv, bool with_env,
  4300. void *wasm_c_api_env)
  4301. {
  4302. wasm_val_t params_buf[16] = { 0 }, results_buf[4] = { 0 };
  4303. wasm_val_t *params = params_buf, *results = results_buf;
  4304. wasm_trap_t *trap = NULL;
  4305. bool ret = false;
  4306. wasm_val_vec_t params_vec, results_vec;
  4307. if (func_type->param_count > 16) {
  4308. if (!(params =
  4309. runtime_malloc(sizeof(wasm_val_t) * func_type->param_count,
  4310. module_inst, NULL, 0))) {
  4311. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  4312. return false;
  4313. }
  4314. }
  4315. if (!argv_to_params(params, argv, func_type)) {
  4316. wasm_runtime_set_exception(module_inst, "unsupported param type");
  4317. goto fail;
  4318. }
  4319. if (func_type->result_count > 4) {
  4320. if (!(results =
  4321. runtime_malloc(sizeof(wasm_val_t) * func_type->result_count,
  4322. module_inst, NULL, 0))) {
  4323. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  4324. goto fail;
  4325. }
  4326. }
  4327. params_vec.data = params;
  4328. params_vec.num_elems = func_type->param_count;
  4329. params_vec.size = func_type->param_count;
  4330. params_vec.size_of_elem = sizeof(wasm_val_t);
  4331. results_vec.data = results;
  4332. results_vec.num_elems = 0;
  4333. results_vec.size = func_type->result_count;
  4334. results_vec.size_of_elem = sizeof(wasm_val_t);
  4335. if (!with_env) {
  4336. wasm_func_callback_t callback = (wasm_func_callback_t)func_ptr;
  4337. trap = callback(&params_vec, &results_vec);
  4338. }
  4339. else {
  4340. wasm_func_callback_with_env_t callback =
  4341. (wasm_func_callback_with_env_t)func_ptr;
  4342. trap = callback(wasm_c_api_env, &params_vec, &results_vec);
  4343. }
  4344. if (trap) {
  4345. if (trap->message->data) {
  4346. /* since trap->message->data does not end with '\0' */
  4347. char trap_message[128] = { 0 };
  4348. bh_memcpy_s(trap_message, 127, trap->message->data,
  4349. (trap->message->size < 127 ? (uint32)trap->message->size
  4350. : 127));
  4351. wasm_runtime_set_exception(module_inst, trap_message);
  4352. }
  4353. else {
  4354. wasm_runtime_set_exception(
  4355. module_inst, "native function throw unknown exception");
  4356. }
  4357. wasm_trap_delete(trap);
  4358. goto fail;
  4359. }
  4360. if (!results_to_argv(module_inst, argv, results, func_type)) {
  4361. wasm_runtime_set_exception(module_inst, "unsupported result type");
  4362. goto fail;
  4363. }
  4364. results_vec.num_elems = func_type->result_count;
  4365. ret = true;
  4366. fail:
  4367. if (params != params_buf)
  4368. wasm_runtime_free(params);
  4369. if (results != results_buf)
  4370. wasm_runtime_free(results);
  4371. return ret;
  4372. }
  4373. void
  4374. wasm_runtime_show_app_heap_corrupted_prompt()
  4375. {
  4376. LOG_ERROR("Error: app heap is corrupted, if the wasm file "
  4377. "is compiled by wasi-sdk-12.0 or higher version, "
  4378. "please add -Wl,--export=malloc -Wl,--export=free "
  4379. "to export malloc and free functions. If it is "
  4380. "compiled by asc, please add --exportRuntime to "
  4381. "export the runtime helpers.");
  4382. }