wasm_runtime_common.c 150 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803
  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_t 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_t 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_t 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. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  854. void
  855. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module)
  856. {
  857. WASMModuleMemConsumption mem_conspn = { 0 };
  858. #if WASM_ENABLE_INTERP != 0
  859. if (module->module_type == Wasm_Module_Bytecode) {
  860. wasm_get_module_mem_consumption((WASMModule *)module, &mem_conspn);
  861. }
  862. #endif
  863. #if WASM_ENABLE_AOT != 0
  864. if (module->module_type == Wasm_Module_AoT) {
  865. aot_get_module_mem_consumption((AOTModule *)module, &mem_conspn);
  866. }
  867. #endif
  868. os_printf("WASM module memory consumption, total size: %u\n",
  869. mem_conspn.total_size);
  870. os_printf(" module struct size: %u\n", mem_conspn.module_struct_size);
  871. os_printf(" types size: %u\n", mem_conspn.types_size);
  872. os_printf(" imports size: %u\n", mem_conspn.imports_size);
  873. os_printf(" funcs size: %u\n", mem_conspn.functions_size);
  874. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  875. os_printf(" memories size: %u\n", mem_conspn.memories_size);
  876. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  877. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  878. os_printf(" table segs size: %u\n", mem_conspn.table_segs_size);
  879. os_printf(" data segs size: %u\n", mem_conspn.data_segs_size);
  880. os_printf(" const strings size: %u\n", mem_conspn.const_strs_size);
  881. #if WASM_ENABLE_AOT != 0
  882. os_printf(" aot code size: %u\n", mem_conspn.aot_code_size);
  883. #endif
  884. }
  885. void
  886. wasm_runtime_dump_module_inst_mem_consumption(
  887. const WASMModuleInstanceCommon *module_inst)
  888. {
  889. WASMModuleInstMemConsumption mem_conspn = { 0 };
  890. #if WASM_ENABLE_INTERP != 0
  891. if (module_inst->module_type == Wasm_Module_Bytecode) {
  892. wasm_get_module_inst_mem_consumption((WASMModuleInstance *)module_inst,
  893. &mem_conspn);
  894. }
  895. #endif
  896. #if WASM_ENABLE_AOT != 0
  897. if (module_inst->module_type == Wasm_Module_AoT) {
  898. aot_get_module_inst_mem_consumption((AOTModuleInstance *)module_inst,
  899. &mem_conspn);
  900. }
  901. #endif
  902. os_printf("WASM module inst memory consumption, total size: %u\n",
  903. mem_conspn.total_size);
  904. os_printf(" module inst struct size: %u\n",
  905. mem_conspn.module_inst_struct_size);
  906. os_printf(" memories size: %u\n", mem_conspn.memories_size);
  907. os_printf(" app heap size: %u\n", mem_conspn.app_heap_size);
  908. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  909. os_printf(" functions size: %u\n", mem_conspn.functions_size);
  910. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  911. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  912. }
  913. void
  914. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env)
  915. {
  916. uint32 total_size =
  917. offsetof(WASMExecEnv, wasm_stack.s.bottom) + exec_env->wasm_stack_size;
  918. os_printf("Exec env memory consumption, total size: %u\n", total_size);
  919. os_printf(" exec env struct size: %u\n",
  920. offsetof(WASMExecEnv, wasm_stack.s.bottom));
  921. #if WASM_ENABLE_INTERP != 0 && WASM_ENABLE_FAST_INTERP == 0
  922. os_printf(" block addr cache size: %u\n",
  923. sizeof(exec_env->block_addr_cache));
  924. #endif
  925. os_printf(" stack size: %u\n", exec_env->wasm_stack_size);
  926. }
  927. uint32
  928. gc_get_heap_highmark_size(void *heap);
  929. void
  930. wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env)
  931. {
  932. WASMModuleInstMemConsumption module_inst_mem_consps;
  933. WASMModuleMemConsumption module_mem_consps;
  934. WASMModuleInstanceCommon *module_inst_common;
  935. WASMModuleCommon *module_common = NULL;
  936. void *heap_handle = NULL;
  937. uint32 total_size = 0, app_heap_peak_size = 0;
  938. uint32 max_aux_stack_used = -1;
  939. module_inst_common = exec_env->module_inst;
  940. #if WASM_ENABLE_INTERP != 0
  941. if (module_inst_common->module_type == Wasm_Module_Bytecode) {
  942. WASMModuleInstance *wasm_module_inst =
  943. (WASMModuleInstance *)module_inst_common;
  944. WASMModule *wasm_module = wasm_module_inst->module;
  945. module_common = (WASMModuleCommon *)wasm_module;
  946. if (wasm_module_inst->memories) {
  947. heap_handle = wasm_module_inst->memories[0]->heap_handle;
  948. }
  949. wasm_get_module_inst_mem_consumption(wasm_module_inst,
  950. &module_inst_mem_consps);
  951. wasm_get_module_mem_consumption(wasm_module, &module_mem_consps);
  952. if (wasm_module_inst->module->aux_stack_top_global_index != (uint32)-1)
  953. max_aux_stack_used = wasm_module_inst->max_aux_stack_used;
  954. }
  955. #endif
  956. #if WASM_ENABLE_AOT != 0
  957. if (module_inst_common->module_type == Wasm_Module_AoT) {
  958. AOTModuleInstance *aot_module_inst =
  959. (AOTModuleInstance *)module_inst_common;
  960. AOTModule *aot_module = (AOTModule *)aot_module_inst->aot_module.ptr;
  961. module_common = (WASMModuleCommon *)aot_module;
  962. if (aot_module_inst->memories.ptr) {
  963. AOTMemoryInstance **memories =
  964. (AOTMemoryInstance **)aot_module_inst->memories.ptr;
  965. heap_handle = memories[0]->heap_handle.ptr;
  966. }
  967. aot_get_module_inst_mem_consumption(aot_module_inst,
  968. &module_inst_mem_consps);
  969. aot_get_module_mem_consumption(aot_module, &module_mem_consps);
  970. }
  971. #endif
  972. bh_assert(module_common != NULL);
  973. if (heap_handle) {
  974. app_heap_peak_size = gc_get_heap_highmark_size(heap_handle);
  975. }
  976. total_size = offsetof(WASMExecEnv, wasm_stack.s.bottom)
  977. + exec_env->wasm_stack_size + module_mem_consps.total_size
  978. + module_inst_mem_consps.total_size;
  979. os_printf("\nMemory consumption summary (bytes):\n");
  980. wasm_runtime_dump_module_mem_consumption(module_common);
  981. wasm_runtime_dump_module_inst_mem_consumption(module_inst_common);
  982. wasm_runtime_dump_exec_env_mem_consumption(exec_env);
  983. os_printf("\nTotal memory consumption of module, module inst and "
  984. "exec env: %u\n",
  985. total_size);
  986. os_printf("Total interpreter stack used: %u\n",
  987. exec_env->max_wasm_stack_used);
  988. if (max_aux_stack_used != (uint32)-1)
  989. os_printf("Total auxiliary stack used: %u\n", max_aux_stack_used);
  990. else
  991. os_printf("Total aux stack used: no enough info to profile\n");
  992. os_printf("Total app heap used: %u\n", app_heap_peak_size);
  993. }
  994. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
  995. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  996. #if WASM_ENABLE_PERF_PROFILING != 0
  997. void
  998. wasm_runtime_dump_perf_profiling(WASMModuleInstanceCommon *module_inst)
  999. {
  1000. #if WASM_ENABLE_INTERP != 0
  1001. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1002. wasm_dump_perf_profiling((WASMModuleInstance *)module_inst);
  1003. }
  1004. #endif
  1005. #if WASM_ENABLE_AOT != 0
  1006. if (module_inst->module_type == Wasm_Module_AoT) {
  1007. aot_dump_perf_profiling((AOTModuleInstance *)module_inst);
  1008. }
  1009. #endif
  1010. }
  1011. #endif
  1012. WASMModuleInstanceCommon *
  1013. wasm_runtime_get_module_inst(WASMExecEnv *exec_env)
  1014. {
  1015. return wasm_exec_env_get_module_inst(exec_env);
  1016. }
  1017. void *
  1018. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env)
  1019. {
  1020. return exec_env->attachment;
  1021. }
  1022. void
  1023. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data)
  1024. {
  1025. exec_env->user_data = user_data;
  1026. }
  1027. void *
  1028. wasm_runtime_get_user_data(WASMExecEnv *exec_env)
  1029. {
  1030. return exec_env->user_data;
  1031. }
  1032. WASMType *
  1033. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  1034. uint32 module_type)
  1035. {
  1036. WASMType *type = NULL;
  1037. #if WASM_ENABLE_INTERP != 0
  1038. if (module_type == Wasm_Module_Bytecode) {
  1039. WASMFunctionInstance *wasm_func = (WASMFunctionInstance *)function;
  1040. type = wasm_func->is_import_func ? wasm_func->u.func_import->func_type
  1041. : wasm_func->u.func->func_type;
  1042. }
  1043. #endif
  1044. #if WASM_ENABLE_AOT != 0
  1045. if (module_type == Wasm_Module_AoT) {
  1046. AOTFunctionInstance *aot_func = (AOTFunctionInstance *)function;
  1047. type = aot_func->is_import_func ? aot_func->u.func_import->func_type
  1048. : aot_func->u.func.func_type;
  1049. }
  1050. #endif
  1051. return type;
  1052. }
  1053. WASMFunctionInstanceCommon *
  1054. wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
  1055. const char *name, const char *signature)
  1056. {
  1057. #if WASM_ENABLE_INTERP != 0
  1058. if (module_inst->module_type == Wasm_Module_Bytecode)
  1059. return (WASMFunctionInstanceCommon *)wasm_lookup_function(
  1060. (const WASMModuleInstance *)module_inst, name, signature);
  1061. #endif
  1062. #if WASM_ENABLE_AOT != 0
  1063. if (module_inst->module_type == Wasm_Module_AoT)
  1064. return (WASMFunctionInstanceCommon *)aot_lookup_function(
  1065. (const AOTModuleInstance *)module_inst, name, signature);
  1066. #endif
  1067. return NULL;
  1068. }
  1069. #if WASM_ENABLE_REF_TYPES != 0
  1070. /* (uintptr_t)externref -> (uint32_t)index */
  1071. /* argv -> *ret_argv */
  1072. static bool
  1073. wasm_runtime_prepare_call_function(WASMExecEnv *exec_env,
  1074. WASMFunctionInstanceCommon *function,
  1075. uint32 *argv, uint32 argc, uint32 **ret_argv,
  1076. uint32 *ret_argc_param,
  1077. uint32 *ret_argc_result)
  1078. {
  1079. uint32 *new_argv = NULL, argv_i = 0, new_argv_i = 0, param_i = 0,
  1080. result_i = 0;
  1081. bool need_param_transform = false, need_result_transform = false;
  1082. uint64 size = 0;
  1083. WASMType *func_type = wasm_runtime_get_function_type(
  1084. function, exec_env->module_inst->module_type);
  1085. bh_assert(func_type);
  1086. *ret_argc_param = func_type->param_cell_num;
  1087. *ret_argc_result = func_type->ret_cell_num;
  1088. for (param_i = 0; param_i < func_type->param_count; param_i++) {
  1089. if (VALUE_TYPE_EXTERNREF == func_type->types[param_i]) {
  1090. need_param_transform = true;
  1091. }
  1092. }
  1093. for (result_i = 0; result_i < func_type->result_count; result_i++) {
  1094. if (VALUE_TYPE_EXTERNREF
  1095. == func_type->types[func_type->param_count + result_i]) {
  1096. need_result_transform = true;
  1097. }
  1098. }
  1099. if (!need_param_transform && !need_result_transform) {
  1100. *ret_argv = argv;
  1101. return true;
  1102. }
  1103. if (func_type->param_cell_num >= func_type->ret_cell_num) {
  1104. size = sizeof(uint32) * func_type->param_cell_num;
  1105. }
  1106. else {
  1107. size = sizeof(uint32) * func_type->ret_cell_num;
  1108. }
  1109. if (!(new_argv = runtime_malloc(size, exec_env->module_inst, NULL, 0))) {
  1110. return false;
  1111. }
  1112. if (!need_param_transform) {
  1113. bh_memcpy_s(new_argv, size, argv, size);
  1114. }
  1115. else {
  1116. for (param_i = 0; param_i < func_type->param_count && argv_i < argc
  1117. && new_argv_i < func_type->param_cell_num;
  1118. param_i++) {
  1119. uint8 param_type = func_type->types[param_i];
  1120. if (VALUE_TYPE_EXTERNREF == param_type) {
  1121. void *externref_obj;
  1122. uint32 externref_index;
  1123. #if UINTPTR_MAX == UINT32_MAX
  1124. externref_obj = (void *)argv[argv_i];
  1125. #else
  1126. union {
  1127. uintptr_t val;
  1128. uint32 parts[2];
  1129. } u;
  1130. u.parts[0] = argv[argv_i];
  1131. u.parts[1] = argv[argv_i + 1];
  1132. externref_obj = (void *)u.val;
  1133. #endif
  1134. if (!wasm_externref_obj2ref(exec_env->module_inst,
  1135. externref_obj, &externref_index)) {
  1136. wasm_runtime_free(new_argv);
  1137. return false;
  1138. }
  1139. new_argv[new_argv_i] = externref_index;
  1140. argv_i += sizeof(uintptr_t) / sizeof(uint32);
  1141. new_argv_i++;
  1142. }
  1143. else {
  1144. uint16 param_cell_num = wasm_value_type_cell_num(param_type);
  1145. uint32 param_size = sizeof(uint32) * param_cell_num;
  1146. bh_memcpy_s(new_argv + new_argv_i, param_size, argv + argv_i,
  1147. param_size);
  1148. argv_i += param_cell_num;
  1149. new_argv_i += param_cell_num;
  1150. }
  1151. }
  1152. }
  1153. *ret_argv = new_argv;
  1154. return true;
  1155. }
  1156. /* (uintptr_t)externref <- (uint32_t)index */
  1157. /* argv <- new_argv */
  1158. static bool
  1159. wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
  1160. WASMFunctionInstanceCommon *function,
  1161. uint32 *argv, uint32 argc, uint32 *ret_argv)
  1162. {
  1163. uint32 argv_i = 0, result_i = 0, ret_argv_i = 0;
  1164. WASMType *func_type;
  1165. bh_assert((argv && ret_argv) || (argc == 0));
  1166. if (argv == ret_argv) {
  1167. /* no need to transfrom externref results */
  1168. return true;
  1169. }
  1170. func_type = wasm_runtime_get_function_type(
  1171. function, exec_env->module_inst->module_type);
  1172. bh_assert(func_type);
  1173. for (result_i = 0; result_i < func_type->result_count && argv_i < argc;
  1174. result_i++) {
  1175. uint8 result_type = func_type->types[func_type->param_count + result_i];
  1176. if (result_type == VALUE_TYPE_EXTERNREF) {
  1177. void *externref_obj;
  1178. #if UINTPTR_MAX != UINT32_MAX
  1179. union {
  1180. uintptr_t val;
  1181. uint32 parts[2];
  1182. } u;
  1183. #endif
  1184. if (!wasm_externref_ref2obj(argv[argv_i], &externref_obj)) {
  1185. wasm_runtime_free(argv);
  1186. return false;
  1187. }
  1188. #if UINTPTR_MAX == UINT32_MAX
  1189. ret_argv[ret_argv_i] = (uintptr_t)externref_obj;
  1190. #else
  1191. u.val = (uintptr_t)externref_obj;
  1192. ret_argv[ret_argv_i] = u.parts[0];
  1193. ret_argv[ret_argv_i + 1] = u.parts[1];
  1194. #endif
  1195. argv_i += 1;
  1196. ret_argv_i += sizeof(uintptr_t) / sizeof(uint32);
  1197. }
  1198. else {
  1199. uint16 result_cell_num = wasm_value_type_cell_num(result_type);
  1200. uint32 result_size = sizeof(uint32) * result_cell_num;
  1201. bh_memcpy_s(ret_argv + ret_argv_i, result_size, argv + argv_i,
  1202. result_size);
  1203. argv_i += result_cell_num;
  1204. ret_argv_i += result_cell_num;
  1205. }
  1206. }
  1207. wasm_runtime_free(argv);
  1208. return true;
  1209. }
  1210. #endif
  1211. bool
  1212. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  1213. WASMFunctionInstanceCommon *function, uint32 argc,
  1214. uint32 argv[])
  1215. {
  1216. bool ret = false;
  1217. uint32 *new_argv = NULL, param_argc;
  1218. #if WASM_ENABLE_REF_TYPES != 0
  1219. uint32 result_argc = 0;
  1220. #endif
  1221. if (!wasm_runtime_exec_env_check(exec_env)) {
  1222. LOG_ERROR("Invalid exec env stack info.");
  1223. return false;
  1224. }
  1225. #if WASM_ENABLE_REF_TYPES != 0
  1226. if (!wasm_runtime_prepare_call_function(exec_env, function, argv, argc,
  1227. &new_argv, &param_argc,
  1228. &result_argc)) {
  1229. wasm_runtime_set_exception(exec_env->module_inst,
  1230. "the arguments conversion is failed");
  1231. return false;
  1232. }
  1233. #else
  1234. new_argv = argv;
  1235. param_argc = argc;
  1236. #endif
  1237. #if WASM_ENABLE_INTERP != 0
  1238. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  1239. ret = wasm_call_function(exec_env, (WASMFunctionInstance *)function,
  1240. param_argc, new_argv);
  1241. #endif
  1242. #if WASM_ENABLE_AOT != 0
  1243. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  1244. ret = aot_call_function(exec_env, (AOTFunctionInstance *)function,
  1245. param_argc, new_argv);
  1246. #endif
  1247. if (!ret) {
  1248. if (new_argv != argv) {
  1249. wasm_runtime_free(new_argv);
  1250. }
  1251. return false;
  1252. }
  1253. #if WASM_ENABLE_REF_TYPES != 0
  1254. if (!wasm_runtime_finalize_call_function(exec_env, function, new_argv,
  1255. result_argc, argv)) {
  1256. wasm_runtime_set_exception(exec_env->module_inst,
  1257. "the result conversion is failed");
  1258. return false;
  1259. }
  1260. #endif
  1261. return ret;
  1262. }
  1263. static void
  1264. parse_args_to_uint32_array(WASMType *type, wasm_val_t *args, uint32 *out_argv)
  1265. {
  1266. uint32 i, p;
  1267. for (i = 0, p = 0; i < type->param_count; i++) {
  1268. switch (args[i].kind) {
  1269. case WASM_I32:
  1270. out_argv[p++] = args[i].of.i32;
  1271. break;
  1272. case WASM_I64:
  1273. {
  1274. union {
  1275. uint64 val;
  1276. uint32 parts[2];
  1277. } u;
  1278. u.val = args[i].of.i64;
  1279. out_argv[p++] = u.parts[0];
  1280. out_argv[p++] = u.parts[1];
  1281. break;
  1282. }
  1283. case WASM_F32:
  1284. {
  1285. union {
  1286. float32 val;
  1287. uint32 part;
  1288. } u;
  1289. u.val = args[i].of.f32;
  1290. out_argv[p++] = u.part;
  1291. break;
  1292. }
  1293. case WASM_F64:
  1294. {
  1295. union {
  1296. float64 val;
  1297. uint32 parts[2];
  1298. } u;
  1299. u.val = args[i].of.f64;
  1300. out_argv[p++] = u.parts[0];
  1301. out_argv[p++] = u.parts[1];
  1302. break;
  1303. }
  1304. #if WASM_ENABLE_REF_TYPES != 0
  1305. case WASM_FUNCREF:
  1306. {
  1307. out_argv[p++] = args[i].of.i32;
  1308. break;
  1309. }
  1310. case WASM_ANYREF:
  1311. {
  1312. #if UINTPTR_MAX == UINT32_MAX
  1313. out_argv[p++] = args[i].of.foreign;
  1314. #else
  1315. union {
  1316. uintptr_t val;
  1317. uint32 parts[2];
  1318. } u;
  1319. u.val = (uintptr_t)args[i].of.foreign;
  1320. out_argv[p++] = u.parts[0];
  1321. out_argv[p++] = u.parts[1];
  1322. #endif
  1323. break;
  1324. }
  1325. #endif
  1326. default:
  1327. bh_assert(0);
  1328. break;
  1329. }
  1330. }
  1331. }
  1332. static void
  1333. parse_uint32_array_to_results(WASMType *type, uint32 *argv,
  1334. wasm_val_t *out_results)
  1335. {
  1336. uint32 i, p;
  1337. for (i = 0, p = 0; i < type->result_count; i++) {
  1338. switch (type->types[type->param_count + i]) {
  1339. case VALUE_TYPE_I32:
  1340. out_results[i].kind = WASM_I32;
  1341. out_results[i].of.i32 = (int32)argv[p++];
  1342. break;
  1343. case VALUE_TYPE_I64:
  1344. {
  1345. union {
  1346. uint64 val;
  1347. uint32 parts[2];
  1348. } u;
  1349. u.parts[0] = argv[p++];
  1350. u.parts[1] = argv[p++];
  1351. out_results[i].kind = WASM_I64;
  1352. out_results[i].of.i64 = u.val;
  1353. break;
  1354. }
  1355. case VALUE_TYPE_F32:
  1356. {
  1357. union {
  1358. float32 val;
  1359. uint32 part;
  1360. } u;
  1361. u.part = argv[p++];
  1362. out_results[i].kind = WASM_F32;
  1363. out_results[i].of.f32 = u.val;
  1364. break;
  1365. }
  1366. case VALUE_TYPE_F64:
  1367. {
  1368. union {
  1369. float64 val;
  1370. uint32 parts[2];
  1371. } u;
  1372. u.parts[0] = argv[p++];
  1373. u.parts[1] = argv[p++];
  1374. out_results[i].kind = WASM_F64;
  1375. out_results[i].of.f64 = u.val;
  1376. break;
  1377. }
  1378. #if WASM_ENABLE_REF_TYPES != 0
  1379. case VALUE_TYPE_FUNCREF:
  1380. {
  1381. out_results[i].kind = WASM_I32;
  1382. out_results[i].of.i32 = (int32)argv[p++];
  1383. break;
  1384. }
  1385. case VALUE_TYPE_EXTERNREF:
  1386. {
  1387. #if UINTPTR_MAX == UINT32_MAX
  1388. out_results[i].kind = WASM_ANYREF;
  1389. out_results[i].of.foreign = (uintptr_t)argv[p++];
  1390. #else
  1391. union {
  1392. uintptr_t val;
  1393. uint32 parts[2];
  1394. } u;
  1395. u.parts[0] = argv[p++];
  1396. u.parts[1] = argv[p++];
  1397. out_results[i].kind = WASM_ANYREF;
  1398. out_results[i].of.foreign = u.val;
  1399. #endif
  1400. break;
  1401. }
  1402. #endif
  1403. default:
  1404. bh_assert(0);
  1405. break;
  1406. }
  1407. }
  1408. }
  1409. bool
  1410. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  1411. WASMFunctionInstanceCommon *function,
  1412. uint32 num_results, wasm_val_t results[],
  1413. uint32 num_args, wasm_val_t args[])
  1414. {
  1415. uint32 argc, *argv, cell_num, total_size, module_type;
  1416. #if WASM_ENABLE_REF_TYPES != 0
  1417. uint32 i, param_size_in_double_world = 0, result_size_in_double_world = 0;
  1418. #endif
  1419. WASMType *type;
  1420. bool ret = false;
  1421. module_type = exec_env->module_inst->module_type;
  1422. type = wasm_runtime_get_function_type(function, module_type);
  1423. if (!type) {
  1424. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT must be "
  1425. "enabled at least one.");
  1426. goto fail1;
  1427. }
  1428. #if WASM_ENABLE_REF_TYPES != 0
  1429. for (i = 0; i < type->param_count; i++) {
  1430. param_size_in_double_world +=
  1431. wasm_value_type_cell_num_outside(type->types[i]);
  1432. }
  1433. for (i = 0; i < type->result_count; i++) {
  1434. result_size_in_double_world += wasm_value_type_cell_num_outside(
  1435. type->types[type->param_count + i]);
  1436. }
  1437. argc = param_size_in_double_world;
  1438. cell_num = (argc >= result_size_in_double_world)
  1439. ? argc
  1440. : result_size_in_double_world;
  1441. #else
  1442. argc = type->param_cell_num;
  1443. cell_num = (argc > type->ret_cell_num) ? argc : type->ret_cell_num;
  1444. #endif
  1445. if (num_results != type->result_count) {
  1446. LOG_ERROR(
  1447. "The result value number does not match the function declaration.");
  1448. goto fail1;
  1449. }
  1450. if (num_args != type->param_count) {
  1451. LOG_ERROR("The argument value number does not match the function "
  1452. "declaration.");
  1453. goto fail1;
  1454. }
  1455. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  1456. if (!(argv = runtime_malloc((uint32)total_size, exec_env->module_inst, NULL,
  1457. 0))) {
  1458. wasm_runtime_set_exception(exec_env->module_inst,
  1459. "allocate memory failed");
  1460. goto fail1;
  1461. }
  1462. parse_args_to_uint32_array(type, args, argv);
  1463. if (!(ret = wasm_runtime_call_wasm(exec_env, function, argc, argv)))
  1464. goto fail2;
  1465. parse_uint32_array_to_results(type, argv, results);
  1466. fail2:
  1467. wasm_runtime_free(argv);
  1468. fail1:
  1469. return ret;
  1470. }
  1471. bool
  1472. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  1473. WASMFunctionInstanceCommon *function,
  1474. uint32 num_results, wasm_val_t results[],
  1475. uint32 num_args, ...)
  1476. {
  1477. wasm_val_t *args = NULL;
  1478. WASMType *type = NULL;
  1479. bool ret = false;
  1480. uint32 i = 0, module_type;
  1481. va_list vargs;
  1482. module_type = exec_env->module_inst->module_type;
  1483. type = wasm_runtime_get_function_type(function, module_type);
  1484. if (!type) {
  1485. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT "
  1486. "must be enabled at least one.");
  1487. goto fail1;
  1488. }
  1489. if (num_args != type->param_count) {
  1490. LOG_ERROR("The argument value number does not match the "
  1491. "function declaration.");
  1492. goto fail1;
  1493. }
  1494. if (!(args =
  1495. runtime_malloc(sizeof(wasm_val_t) * num_args, NULL, NULL, 0))) {
  1496. wasm_runtime_set_exception(exec_env->module_inst,
  1497. "allocate memory failed");
  1498. goto fail1;
  1499. }
  1500. va_start(vargs, num_args);
  1501. for (i = 0; i < num_args; i++) {
  1502. switch (type->types[i]) {
  1503. case VALUE_TYPE_I32:
  1504. args[i].kind = WASM_I32;
  1505. args[i].of.i32 = va_arg(vargs, uint32);
  1506. break;
  1507. case VALUE_TYPE_I64:
  1508. args[i].kind = WASM_I64;
  1509. args[i].of.i64 = va_arg(vargs, uint64);
  1510. break;
  1511. case VALUE_TYPE_F32:
  1512. args[i].kind = WASM_F32;
  1513. args[i].of.f32 = (float32)va_arg(vargs, float64);
  1514. break;
  1515. case VALUE_TYPE_F64:
  1516. args[i].kind = WASM_F64;
  1517. args[i].of.f64 = va_arg(vargs, float64);
  1518. break;
  1519. #if WASM_ENABLE_REF_TYPES != 0
  1520. case VALUE_TYPE_FUNCREF:
  1521. {
  1522. args[i].kind = WASM_FUNCREF;
  1523. args[i].of.i32 = va_arg(vargs, uint32);
  1524. break;
  1525. }
  1526. case VALUE_TYPE_EXTERNREF:
  1527. {
  1528. args[i].kind = WASM_ANYREF;
  1529. args[i].of.foreign = va_arg(vargs, uintptr_t);
  1530. break;
  1531. }
  1532. #endif
  1533. default:
  1534. bh_assert(0);
  1535. break;
  1536. }
  1537. }
  1538. va_end(vargs);
  1539. ret = wasm_runtime_call_wasm_a(exec_env, function, num_results, results,
  1540. num_args, args);
  1541. wasm_runtime_free(args);
  1542. fail1:
  1543. return ret;
  1544. }
  1545. bool
  1546. wasm_runtime_create_exec_env_and_call_wasm(
  1547. WASMModuleInstanceCommon *module_inst, WASMFunctionInstanceCommon *function,
  1548. uint32 argc, uint32 argv[])
  1549. {
  1550. bool ret = false;
  1551. #if WASM_ENABLE_INTERP != 0
  1552. if (module_inst->module_type == Wasm_Module_Bytecode)
  1553. ret = wasm_create_exec_env_and_call_function(
  1554. (WASMModuleInstance *)module_inst, (WASMFunctionInstance *)function,
  1555. argc, argv, true);
  1556. #endif
  1557. #if WASM_ENABLE_AOT != 0
  1558. if (module_inst->module_type == Wasm_Module_AoT)
  1559. ret = aot_create_exec_env_and_call_function(
  1560. (AOTModuleInstance *)module_inst, (AOTFunctionInstance *)function,
  1561. argc, argv);
  1562. #endif
  1563. return ret;
  1564. }
  1565. bool
  1566. wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst)
  1567. {
  1568. #if WASM_ENABLE_INTERP != 0
  1569. if (module_inst->module_type == Wasm_Module_Bytecode)
  1570. return wasm_create_exec_env_singleton(
  1571. (WASMModuleInstance *)module_inst);
  1572. #endif
  1573. #if WASM_ENABLE_AOT != 0
  1574. if (module_inst->module_type == Wasm_Module_AoT)
  1575. return aot_create_exec_env_singleton((AOTModuleInstance *)module_inst);
  1576. #endif
  1577. return false;
  1578. }
  1579. WASMExecEnv *
  1580. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst)
  1581. {
  1582. #if WASM_ENABLE_INTERP != 0
  1583. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1584. if (!((WASMModuleInstance *)module_inst)->exec_env_singleton) {
  1585. wasm_create_exec_env_singleton((WASMModuleInstance *)module_inst);
  1586. }
  1587. return ((WASMModuleInstance *)module_inst)->exec_env_singleton;
  1588. }
  1589. #endif
  1590. #if WASM_ENABLE_AOT != 0
  1591. if (module_inst->module_type == Wasm_Module_AoT) {
  1592. if (!((AOTModuleInstance *)module_inst)->exec_env_singleton.ptr) {
  1593. aot_create_exec_env_singleton((AOTModuleInstance *)module_inst);
  1594. }
  1595. return (WASMExecEnv *)((AOTModuleInstance *)module_inst)
  1596. ->exec_env_singleton.ptr;
  1597. }
  1598. #endif
  1599. return NULL;
  1600. }
  1601. void
  1602. wasm_runtime_set_exception(WASMModuleInstanceCommon *module_inst,
  1603. const char *exception)
  1604. {
  1605. #if WASM_ENABLE_INTERP != 0
  1606. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1607. wasm_set_exception((WASMModuleInstance *)module_inst, exception);
  1608. return;
  1609. }
  1610. #endif
  1611. #if WASM_ENABLE_AOT != 0
  1612. if (module_inst->module_type == Wasm_Module_AoT) {
  1613. aot_set_exception((AOTModuleInstance *)module_inst, exception);
  1614. return;
  1615. }
  1616. #endif
  1617. }
  1618. const char *
  1619. wasm_runtime_get_exception(WASMModuleInstanceCommon *module_inst)
  1620. {
  1621. #if WASM_ENABLE_INTERP != 0
  1622. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1623. return wasm_get_exception((WASMModuleInstance *)module_inst);
  1624. }
  1625. #endif
  1626. #if WASM_ENABLE_AOT != 0
  1627. if (module_inst->module_type == Wasm_Module_AoT) {
  1628. return aot_get_exception((AOTModuleInstance *)module_inst);
  1629. }
  1630. #endif
  1631. return NULL;
  1632. }
  1633. void
  1634. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst)
  1635. {
  1636. wasm_runtime_set_exception(module_inst, NULL);
  1637. }
  1638. void
  1639. wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
  1640. void *custom_data)
  1641. {
  1642. #if WASM_ENABLE_INTERP != 0
  1643. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1644. ((WASMModuleInstance *)module_inst)->custom_data = custom_data;
  1645. return;
  1646. }
  1647. #endif
  1648. #if WASM_ENABLE_AOT != 0
  1649. if (module_inst->module_type == Wasm_Module_AoT) {
  1650. ((AOTModuleInstance *)module_inst)->custom_data.ptr = custom_data;
  1651. return;
  1652. }
  1653. #endif
  1654. }
  1655. void
  1656. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  1657. void *custom_data)
  1658. {
  1659. #if WASM_ENABLE_THREAD_MGR != 0
  1660. wasm_cluster_spread_custom_data(module_inst, custom_data);
  1661. #else
  1662. wasm_runtime_set_custom_data_internal(module_inst, custom_data);
  1663. #endif
  1664. }
  1665. void *
  1666. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst)
  1667. {
  1668. #if WASM_ENABLE_INTERP != 0
  1669. if (module_inst->module_type == Wasm_Module_Bytecode)
  1670. return ((WASMModuleInstance *)module_inst)->custom_data;
  1671. #endif
  1672. #if WASM_ENABLE_AOT != 0
  1673. if (module_inst->module_type == Wasm_Module_AoT)
  1674. return ((AOTModuleInstance *)module_inst)->custom_data.ptr;
  1675. #endif
  1676. return NULL;
  1677. }
  1678. uint32
  1679. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  1680. void **p_native_addr)
  1681. {
  1682. #if WASM_ENABLE_INTERP != 0
  1683. if (module_inst->module_type == Wasm_Module_Bytecode)
  1684. return wasm_module_malloc((WASMModuleInstance *)module_inst, size,
  1685. p_native_addr);
  1686. #endif
  1687. #if WASM_ENABLE_AOT != 0
  1688. if (module_inst->module_type == Wasm_Module_AoT)
  1689. return aot_module_malloc((AOTModuleInstance *)module_inst, size,
  1690. p_native_addr);
  1691. #endif
  1692. return 0;
  1693. }
  1694. uint32
  1695. wasm_runtime_module_realloc(WASMModuleInstanceCommon *module_inst, uint32 ptr,
  1696. uint32 size, void **p_native_addr)
  1697. {
  1698. #if WASM_ENABLE_INTERP != 0
  1699. if (module_inst->module_type == Wasm_Module_Bytecode)
  1700. return wasm_module_realloc((WASMModuleInstance *)module_inst, ptr, size,
  1701. p_native_addr);
  1702. #endif
  1703. #if WASM_ENABLE_AOT != 0
  1704. if (module_inst->module_type == Wasm_Module_AoT)
  1705. return aot_module_realloc((AOTModuleInstance *)module_inst, ptr, size,
  1706. p_native_addr);
  1707. #endif
  1708. return 0;
  1709. }
  1710. void
  1711. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr)
  1712. {
  1713. #if WASM_ENABLE_INTERP != 0
  1714. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1715. wasm_module_free((WASMModuleInstance *)module_inst, ptr);
  1716. return;
  1717. }
  1718. #endif
  1719. #if WASM_ENABLE_AOT != 0
  1720. if (module_inst->module_type == Wasm_Module_AoT) {
  1721. aot_module_free((AOTModuleInstance *)module_inst, ptr);
  1722. return;
  1723. }
  1724. #endif
  1725. }
  1726. uint32
  1727. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  1728. const char *src, uint32 size)
  1729. {
  1730. #if WASM_ENABLE_INTERP != 0
  1731. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1732. return wasm_module_dup_data((WASMModuleInstance *)module_inst, src,
  1733. size);
  1734. }
  1735. #endif
  1736. #if WASM_ENABLE_AOT != 0
  1737. if (module_inst->module_type == Wasm_Module_AoT) {
  1738. return aot_module_dup_data((AOTModuleInstance *)module_inst, src, size);
  1739. }
  1740. #endif
  1741. return 0;
  1742. }
  1743. bool
  1744. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  1745. uint32 app_offset, uint32 size)
  1746. {
  1747. #if WASM_ENABLE_INTERP != 0
  1748. if (module_inst->module_type == Wasm_Module_Bytecode)
  1749. return wasm_validate_app_addr((WASMModuleInstance *)module_inst,
  1750. app_offset, size);
  1751. #endif
  1752. #if WASM_ENABLE_AOT != 0
  1753. if (module_inst->module_type == Wasm_Module_AoT)
  1754. return aot_validate_app_addr((AOTModuleInstance *)module_inst,
  1755. app_offset, size);
  1756. #endif
  1757. return false;
  1758. }
  1759. bool
  1760. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  1761. uint32 app_str_offset)
  1762. {
  1763. uint32 app_end_offset;
  1764. char *str, *str_end;
  1765. if (!wasm_runtime_get_app_addr_range(module_inst, app_str_offset, NULL,
  1766. &app_end_offset))
  1767. goto fail;
  1768. str = wasm_runtime_addr_app_to_native(module_inst, app_str_offset);
  1769. str_end = str + (app_end_offset - app_str_offset);
  1770. while (str < str_end && *str != '\0')
  1771. str++;
  1772. if (str == str_end)
  1773. goto fail;
  1774. return true;
  1775. fail:
  1776. wasm_runtime_set_exception(module_inst, "out of bounds memory access");
  1777. return false;
  1778. }
  1779. bool
  1780. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  1781. void *native_ptr, uint32 size)
  1782. {
  1783. #if WASM_ENABLE_INTERP != 0
  1784. if (module_inst->module_type == Wasm_Module_Bytecode)
  1785. return wasm_validate_native_addr((WASMModuleInstance *)module_inst,
  1786. native_ptr, size);
  1787. #endif
  1788. #if WASM_ENABLE_AOT != 0
  1789. if (module_inst->module_type == Wasm_Module_AoT)
  1790. return aot_validate_native_addr((AOTModuleInstance *)module_inst,
  1791. native_ptr, size);
  1792. #endif
  1793. return false;
  1794. }
  1795. void *
  1796. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  1797. uint32 app_offset)
  1798. {
  1799. #if WASM_ENABLE_INTERP != 0
  1800. if (module_inst->module_type == Wasm_Module_Bytecode)
  1801. return wasm_addr_app_to_native((WASMModuleInstance *)module_inst,
  1802. app_offset);
  1803. #endif
  1804. #if WASM_ENABLE_AOT != 0
  1805. if (module_inst->module_type == Wasm_Module_AoT)
  1806. return aot_addr_app_to_native((AOTModuleInstance *)module_inst,
  1807. app_offset);
  1808. #endif
  1809. return NULL;
  1810. }
  1811. uint32
  1812. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  1813. void *native_ptr)
  1814. {
  1815. #if WASM_ENABLE_INTERP != 0
  1816. if (module_inst->module_type == Wasm_Module_Bytecode)
  1817. return wasm_addr_native_to_app((WASMModuleInstance *)module_inst,
  1818. native_ptr);
  1819. #endif
  1820. #if WASM_ENABLE_AOT != 0
  1821. if (module_inst->module_type == Wasm_Module_AoT)
  1822. return aot_addr_native_to_app((AOTModuleInstance *)module_inst,
  1823. native_ptr);
  1824. #endif
  1825. return 0;
  1826. }
  1827. bool
  1828. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  1829. uint32 app_offset, uint32 *p_app_start_offset,
  1830. uint32 *p_app_end_offset)
  1831. {
  1832. #if WASM_ENABLE_INTERP != 0
  1833. if (module_inst->module_type == Wasm_Module_Bytecode)
  1834. return wasm_get_app_addr_range((WASMModuleInstance *)module_inst,
  1835. app_offset, p_app_start_offset,
  1836. p_app_end_offset);
  1837. #endif
  1838. #if WASM_ENABLE_AOT != 0
  1839. if (module_inst->module_type == Wasm_Module_AoT)
  1840. return aot_get_app_addr_range((AOTModuleInstance *)module_inst,
  1841. app_offset, p_app_start_offset,
  1842. p_app_end_offset);
  1843. #endif
  1844. return false;
  1845. }
  1846. bool
  1847. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  1848. uint8 *native_ptr,
  1849. uint8 **p_native_start_addr,
  1850. uint8 **p_native_end_addr)
  1851. {
  1852. #if WASM_ENABLE_INTERP != 0
  1853. if (module_inst->module_type == Wasm_Module_Bytecode)
  1854. return wasm_get_native_addr_range((WASMModuleInstance *)module_inst,
  1855. native_ptr, p_native_start_addr,
  1856. p_native_end_addr);
  1857. #endif
  1858. #if WASM_ENABLE_AOT != 0
  1859. if (module_inst->module_type == Wasm_Module_AoT)
  1860. return aot_get_native_addr_range((AOTModuleInstance *)module_inst,
  1861. native_ptr, p_native_start_addr,
  1862. p_native_end_addr);
  1863. #endif
  1864. return false;
  1865. }
  1866. uint32
  1867. wasm_runtime_get_temp_ret(WASMModuleInstanceCommon *module_inst)
  1868. {
  1869. #if WASM_ENABLE_INTERP != 0
  1870. if (module_inst->module_type == Wasm_Module_Bytecode)
  1871. return ((WASMModuleInstance *)module_inst)->temp_ret;
  1872. #endif
  1873. #if WASM_ENABLE_AOT != 0
  1874. if (module_inst->module_type == Wasm_Module_AoT)
  1875. return ((AOTModuleInstance *)module_inst)->temp_ret;
  1876. #endif
  1877. return 0;
  1878. }
  1879. void
  1880. wasm_runtime_set_temp_ret(WASMModuleInstanceCommon *module_inst,
  1881. uint32 temp_ret)
  1882. {
  1883. #if WASM_ENABLE_INTERP != 0
  1884. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1885. ((WASMModuleInstance *)module_inst)->temp_ret = temp_ret;
  1886. return;
  1887. }
  1888. #endif
  1889. #if WASM_ENABLE_AOT != 0
  1890. if (module_inst->module_type == Wasm_Module_AoT) {
  1891. ((AOTModuleInstance *)module_inst)->temp_ret = temp_ret;
  1892. return;
  1893. }
  1894. #endif
  1895. }
  1896. uint32
  1897. wasm_runtime_get_llvm_stack(WASMModuleInstanceCommon *module_inst)
  1898. {
  1899. #if WASM_ENABLE_INTERP != 0
  1900. if (module_inst->module_type == Wasm_Module_Bytecode)
  1901. return ((WASMModuleInstance *)module_inst)->llvm_stack;
  1902. #endif
  1903. #if WASM_ENABLE_AOT != 0
  1904. if (module_inst->module_type == Wasm_Module_AoT)
  1905. return ((AOTModuleInstance *)module_inst)->llvm_stack;
  1906. #endif
  1907. return 0;
  1908. }
  1909. void
  1910. wasm_runtime_set_llvm_stack(WASMModuleInstanceCommon *module_inst,
  1911. uint32 llvm_stack)
  1912. {
  1913. #if WASM_ENABLE_INTERP != 0
  1914. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1915. ((WASMModuleInstance *)module_inst)->llvm_stack = llvm_stack;
  1916. return;
  1917. }
  1918. #endif
  1919. #if WASM_ENABLE_AOT != 0
  1920. if (module_inst->module_type == Wasm_Module_AoT) {
  1921. ((AOTModuleInstance *)module_inst)->llvm_stack = llvm_stack;
  1922. return;
  1923. }
  1924. #endif
  1925. }
  1926. bool
  1927. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module,
  1928. uint32 inc_page_count)
  1929. {
  1930. #if WASM_ENABLE_INTERP != 0
  1931. if (module->module_type == Wasm_Module_Bytecode)
  1932. return wasm_enlarge_memory((WASMModuleInstance *)module,
  1933. inc_page_count);
  1934. #endif
  1935. #if WASM_ENABLE_AOT != 0
  1936. if (module->module_type == Wasm_Module_AoT)
  1937. return aot_enlarge_memory((AOTModuleInstance *)module, inc_page_count);
  1938. #endif
  1939. return false;
  1940. }
  1941. #if WASM_ENABLE_LIBC_WASI != 0
  1942. void
  1943. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
  1944. uint32 dir_count, const char *map_dir_list[],
  1945. uint32 map_dir_count, const char *env_list[],
  1946. uint32 env_count, char *argv[], int argc,
  1947. int stdinfd, int stdoutfd, int stderrfd)
  1948. {
  1949. WASIArguments *wasi_args = NULL;
  1950. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  1951. if (module->module_type == Wasm_Module_Bytecode)
  1952. wasi_args = &((WASMModule *)module)->wasi_args;
  1953. #endif
  1954. #if WASM_ENABLE_AOT != 0
  1955. if (module->module_type == Wasm_Module_AoT)
  1956. wasi_args = &((AOTModule *)module)->wasi_args;
  1957. #endif
  1958. if (wasi_args) {
  1959. wasi_args->dir_list = dir_list;
  1960. wasi_args->dir_count = dir_count;
  1961. wasi_args->map_dir_list = map_dir_list;
  1962. wasi_args->map_dir_count = map_dir_count;
  1963. wasi_args->env = env_list;
  1964. wasi_args->env_count = env_count;
  1965. wasi_args->argv = argv;
  1966. wasi_args->argc = (uint32)argc;
  1967. wasi_args->stdio[0] = stdinfd;
  1968. wasi_args->stdio[1] = stdoutfd;
  1969. wasi_args->stdio[2] = stderrfd;
  1970. }
  1971. }
  1972. void
  1973. wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
  1974. uint32 dir_count, const char *map_dir_list[],
  1975. uint32 map_dir_count, const char *env_list[],
  1976. uint32 env_count, char *argv[], int argc)
  1977. {
  1978. wasm_runtime_set_wasi_args_ex(module, dir_list, dir_count, map_dir_list,
  1979. map_dir_count, env_list, env_count, argv,
  1980. argc, -1, -1, -1);
  1981. }
  1982. void
  1983. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  1984. uint32 addr_pool_size)
  1985. {
  1986. WASIArguments *wasi_args = NULL;
  1987. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  1988. if (module->module_type == Wasm_Module_Bytecode)
  1989. wasi_args = &((WASMModule *)module)->wasi_args;
  1990. #endif
  1991. #if WASM_ENABLE_AOT != 0
  1992. if (module->module_type == Wasm_Module_AoT)
  1993. wasi_args = &((AOTModule *)module)->wasi_args;
  1994. #endif
  1995. if (wasi_args) {
  1996. wasi_args->addr_pool = addr_pool;
  1997. wasi_args->addr_count = addr_pool_size;
  1998. }
  1999. }
  2000. #if WASM_ENABLE_UVWASI == 0
  2001. bool
  2002. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  2003. const char *dir_list[], uint32 dir_count,
  2004. const char *map_dir_list[], uint32 map_dir_count,
  2005. const char *env[], uint32 env_count,
  2006. const char *addr_pool[], uint32 addr_pool_size,
  2007. char *argv[], uint32 argc, int stdinfd, int stdoutfd,
  2008. int stderrfd, char *error_buf, uint32 error_buf_size)
  2009. {
  2010. WASIContext *wasi_ctx;
  2011. char *argv_buf = NULL;
  2012. char **argv_list = NULL;
  2013. char *env_buf = NULL;
  2014. char **env_list = NULL;
  2015. uint64 argv_buf_size = 0, env_buf_size = 0, total_size;
  2016. uint32 argv_buf_offset = 0, env_buf_offset = 0;
  2017. struct fd_table *curfds = NULL;
  2018. struct fd_prestats *prestats = NULL;
  2019. struct argv_environ_values *argv_environ = NULL;
  2020. struct addr_pool *apool = NULL;
  2021. bool fd_table_inited = false, fd_prestats_inited = false;
  2022. bool argv_environ_inited = false;
  2023. bool addr_pool_inited = false;
  2024. __wasi_fd_t wasm_fd = 3;
  2025. int32 raw_fd;
  2026. char *path, resolved_path[PATH_MAX];
  2027. uint32 i;
  2028. if (!(wasi_ctx = runtime_malloc(sizeof(WASIContext), NULL, error_buf,
  2029. error_buf_size))) {
  2030. return false;
  2031. }
  2032. wasm_runtime_set_wasi_ctx(module_inst, wasi_ctx);
  2033. #if WASM_ENABLE_INTERP != 0
  2034. if (module_inst->module_type == Wasm_Module_Bytecode
  2035. && !((WASMModuleInstance *)module_inst)->default_memory)
  2036. return true;
  2037. #endif
  2038. #if WASM_ENABLE_AOT != 0
  2039. if (module_inst->module_type == Wasm_Module_AoT
  2040. && !((AOTModuleInstance *)module_inst)
  2041. ->global_table_data.memory_instances[0]
  2042. .memory_data.ptr)
  2043. return true;
  2044. #endif
  2045. /* process argv[0], trip the path and suffix, only keep the program name */
  2046. for (i = 0; i < argc; i++)
  2047. argv_buf_size += strlen(argv[i]) + 1;
  2048. total_size = sizeof(char *) * (uint64)argc;
  2049. if (total_size >= UINT32_MAX
  2050. || (total_size > 0
  2051. && !(argv_list = wasm_runtime_malloc((uint32)total_size)))
  2052. || argv_buf_size >= UINT32_MAX
  2053. || (argv_buf_size > 0
  2054. && !(argv_buf = wasm_runtime_malloc((uint32)argv_buf_size)))) {
  2055. set_error_buf(error_buf, error_buf_size,
  2056. "Init wasi environment failed: allocate memory failed");
  2057. goto fail;
  2058. }
  2059. for (i = 0; i < argc; i++) {
  2060. argv_list[i] = argv_buf + argv_buf_offset;
  2061. bh_strcpy_s(argv_buf + argv_buf_offset,
  2062. (uint32)argv_buf_size - argv_buf_offset, argv[i]);
  2063. argv_buf_offset += (uint32)(strlen(argv[i]) + 1);
  2064. }
  2065. for (i = 0; i < env_count; i++)
  2066. env_buf_size += strlen(env[i]) + 1;
  2067. total_size = sizeof(char *) * (uint64)env_count;
  2068. if (total_size >= UINT32_MAX
  2069. || (total_size > 0
  2070. && !(env_list = wasm_runtime_malloc((uint32)total_size)))
  2071. || env_buf_size >= UINT32_MAX
  2072. || (env_buf_size > 0
  2073. && !(env_buf = wasm_runtime_malloc((uint32)env_buf_size)))) {
  2074. set_error_buf(error_buf, error_buf_size,
  2075. "Init wasi environment failed: allocate memory failed");
  2076. goto fail;
  2077. }
  2078. for (i = 0; i < env_count; i++) {
  2079. env_list[i] = env_buf + env_buf_offset;
  2080. bh_strcpy_s(env_buf + env_buf_offset,
  2081. (uint32)env_buf_size - env_buf_offset, env[i]);
  2082. env_buf_offset += (uint32)(strlen(env[i]) + 1);
  2083. }
  2084. if (!(curfds = wasm_runtime_malloc(sizeof(struct fd_table)))
  2085. || !(prestats = wasm_runtime_malloc(sizeof(struct fd_prestats)))
  2086. || !(argv_environ =
  2087. wasm_runtime_malloc(sizeof(struct argv_environ_values)))
  2088. || !(apool = wasm_runtime_malloc(sizeof(struct addr_pool)))) {
  2089. set_error_buf(error_buf, error_buf_size,
  2090. "Init wasi environment failed: allocate memory failed");
  2091. goto fail;
  2092. }
  2093. if (!fd_table_init(curfds)) {
  2094. set_error_buf(error_buf, error_buf_size,
  2095. "Init wasi environment failed: "
  2096. "init fd table failed");
  2097. goto fail;
  2098. }
  2099. fd_table_inited = true;
  2100. if (!fd_prestats_init(prestats)) {
  2101. set_error_buf(error_buf, error_buf_size,
  2102. "Init wasi environment failed: "
  2103. "init fd prestats failed");
  2104. goto fail;
  2105. }
  2106. fd_prestats_inited = true;
  2107. if (!argv_environ_init(argv_environ, argv_buf, argv_buf_size, argv_list,
  2108. argc, env_buf, env_buf_size, env_list, env_count)) {
  2109. set_error_buf(error_buf, error_buf_size,
  2110. "Init wasi environment failed: "
  2111. "init argument environment failed");
  2112. goto fail;
  2113. }
  2114. argv_environ_inited = true;
  2115. if (!addr_pool_init(apool)) {
  2116. set_error_buf(error_buf, error_buf_size,
  2117. "Init wasi environment failed: "
  2118. "init the address pool failed");
  2119. goto fail;
  2120. }
  2121. addr_pool_inited = true;
  2122. /* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
  2123. if (!fd_table_insert_existing(curfds, 0, (stdinfd != -1) ? stdinfd : 0)
  2124. || !fd_table_insert_existing(curfds, 1, (stdoutfd != -1) ? stdoutfd : 1)
  2125. || !fd_table_insert_existing(curfds, 2,
  2126. (stderrfd != -1) ? stderrfd : 2)) {
  2127. set_error_buf(error_buf, error_buf_size,
  2128. "Init wasi environment failed: init fd table failed");
  2129. goto fail;
  2130. }
  2131. wasm_fd = 3;
  2132. for (i = 0; i < dir_count; i++, wasm_fd++) {
  2133. path = realpath(dir_list[i], resolved_path);
  2134. if (!path) {
  2135. if (error_buf)
  2136. snprintf(error_buf, error_buf_size,
  2137. "error while pre-opening directory %s: %d\n",
  2138. dir_list[i], errno);
  2139. goto fail;
  2140. }
  2141. raw_fd = open(path, O_RDONLY | O_DIRECTORY, 0);
  2142. if (raw_fd == -1) {
  2143. if (error_buf)
  2144. snprintf(error_buf, error_buf_size,
  2145. "error while pre-opening directory %s: %d\n",
  2146. dir_list[i], errno);
  2147. goto fail;
  2148. }
  2149. fd_table_insert_existing(curfds, wasm_fd, raw_fd);
  2150. fd_prestats_insert(prestats, dir_list[i], wasm_fd);
  2151. }
  2152. /* addr_pool(textual) -> apool */
  2153. for (i = 0; i < addr_pool_size; i++) {
  2154. char *cp, *address, *mask;
  2155. bool ret = false;
  2156. cp = bh_strdup(addr_pool[i]);
  2157. if (!cp) {
  2158. set_error_buf(error_buf, error_buf_size,
  2159. "Init wasi environment failed: copy address failed");
  2160. goto fail;
  2161. }
  2162. address = strtok(cp, "/");
  2163. mask = strtok(NULL, "/");
  2164. ret = addr_pool_insert(apool, address, (uint8)(mask ? atoi(mask) : 0));
  2165. wasm_runtime_free(cp);
  2166. if (!ret) {
  2167. set_error_buf(error_buf, error_buf_size,
  2168. "Init wasi environment failed: store address failed");
  2169. goto fail;
  2170. }
  2171. }
  2172. wasi_ctx->curfds = curfds;
  2173. wasi_ctx->prestats = prestats;
  2174. wasi_ctx->argv_environ = argv_environ;
  2175. wasi_ctx->addr_pool = apool;
  2176. wasi_ctx->argv_buf = argv_buf;
  2177. wasi_ctx->argv_list = argv_list;
  2178. wasi_ctx->env_buf = env_buf;
  2179. wasi_ctx->env_list = env_list;
  2180. return true;
  2181. fail:
  2182. if (argv_environ_inited)
  2183. argv_environ_destroy(argv_environ);
  2184. if (fd_prestats_inited)
  2185. fd_prestats_destroy(prestats);
  2186. if (fd_table_inited)
  2187. fd_table_destroy(curfds);
  2188. if (addr_pool_inited)
  2189. addr_pool_destroy(apool);
  2190. if (curfds)
  2191. wasm_runtime_free(curfds);
  2192. if (prestats)
  2193. wasm_runtime_free(prestats);
  2194. if (argv_environ)
  2195. wasm_runtime_free(argv_environ);
  2196. if (apool)
  2197. wasm_runtime_free(apool);
  2198. if (argv_buf)
  2199. wasm_runtime_free(argv_buf);
  2200. if (argv_list)
  2201. wasm_runtime_free(argv_list);
  2202. if (env_buf)
  2203. wasm_runtime_free(env_buf);
  2204. if (env_list)
  2205. wasm_runtime_free(env_list);
  2206. return false;
  2207. }
  2208. #else /* else of WASM_ENABLE_UVWASI == 0 */
  2209. static void *
  2210. wasm_uvwasi_malloc(size_t size, void *mem_user_data)
  2211. {
  2212. return runtime_malloc(size, NULL, NULL, 0);
  2213. (void)mem_user_data;
  2214. }
  2215. static void
  2216. wasm_uvwasi_free(void *ptr, void *mem_user_data)
  2217. {
  2218. if (ptr)
  2219. wasm_runtime_free(ptr);
  2220. (void)mem_user_data;
  2221. }
  2222. static void *
  2223. wasm_uvwasi_calloc(size_t nmemb, size_t size, void *mem_user_data)
  2224. {
  2225. uint64 total_size = (uint64)nmemb * size;
  2226. return runtime_malloc(total_size, NULL, NULL, 0);
  2227. (void)mem_user_data;
  2228. }
  2229. static void *
  2230. wasm_uvwasi_realloc(void *ptr, size_t size, void *mem_user_data)
  2231. {
  2232. if (size >= UINT32_MAX) {
  2233. return NULL;
  2234. }
  2235. return wasm_runtime_realloc(ptr, (uint32)size);
  2236. }
  2237. /* clang-format off */
  2238. static uvwasi_mem_t uvwasi_allocator = {
  2239. .mem_user_data = 0,
  2240. .malloc = wasm_uvwasi_malloc,
  2241. .free = wasm_uvwasi_free,
  2242. .calloc = wasm_uvwasi_calloc,
  2243. .realloc = wasm_uvwasi_realloc
  2244. };
  2245. /* clang-format on */
  2246. bool
  2247. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  2248. const char *dir_list[], uint32 dir_count,
  2249. const char *map_dir_list[], uint32 map_dir_count,
  2250. const char *env[], uint32 env_count,
  2251. const char *addr_pool[], uint32 addr_pool_size,
  2252. char *argv[], uint32 argc, int stdinfd, int stdoutfd,
  2253. int stderrfd, char *error_buf, uint32 error_buf_size)
  2254. {
  2255. uvwasi_t *uvwasi = NULL;
  2256. uvwasi_options_t init_options;
  2257. const char **envp = NULL;
  2258. uint64 total_size;
  2259. uint32 i;
  2260. bool ret = false;
  2261. uvwasi = runtime_malloc(sizeof(uvwasi_t), module_inst, error_buf,
  2262. error_buf_size);
  2263. if (!uvwasi)
  2264. return false;
  2265. /* Setup the initialization options */
  2266. uvwasi_options_init(&init_options);
  2267. init_options.allocator = &uvwasi_allocator;
  2268. init_options.argc = argc;
  2269. init_options.argv = (const char **)argv;
  2270. init_options.in = (stdinfd != -1) ? (uvwasi_fd_t)stdinfd : init_options.in;
  2271. init_options.out =
  2272. (stdoutfd != -1) ? (uvwasi_fd_t)stdoutfd : init_options.out;
  2273. init_options.err =
  2274. (stderrfd != -1) ? (uvwasi_fd_t)stderrfd : init_options.err;
  2275. if (dir_count > 0) {
  2276. init_options.preopenc = dir_count;
  2277. total_size = sizeof(uvwasi_preopen_t) * (uint64)init_options.preopenc;
  2278. init_options.preopens = (uvwasi_preopen_t *)runtime_malloc(
  2279. total_size, module_inst, error_buf, error_buf_size);
  2280. if (init_options.preopens == NULL)
  2281. goto fail;
  2282. for (i = 0; i < init_options.preopenc; i++) {
  2283. init_options.preopens[i].real_path = dir_list[i];
  2284. init_options.preopens[i].mapped_path =
  2285. (i < map_dir_count) ? map_dir_list[i] : dir_list[i];
  2286. }
  2287. }
  2288. if (env_count > 0) {
  2289. total_size = sizeof(char *) * (uint64)(env_count + 1);
  2290. envp =
  2291. runtime_malloc(total_size, module_inst, error_buf, error_buf_size);
  2292. if (envp == NULL)
  2293. goto fail;
  2294. for (i = 0; i < env_count; i++) {
  2295. envp[i] = env[i];
  2296. }
  2297. envp[env_count] = NULL;
  2298. init_options.envp = envp;
  2299. }
  2300. if (UVWASI_ESUCCESS != uvwasi_init(uvwasi, &init_options)) {
  2301. set_error_buf(error_buf, error_buf_size, "uvwasi init failed");
  2302. goto fail;
  2303. }
  2304. wasm_runtime_set_wasi_ctx(module_inst, uvwasi);
  2305. ret = true;
  2306. fail:
  2307. if (envp)
  2308. wasm_runtime_free((void *)envp);
  2309. if (init_options.preopens)
  2310. wasm_runtime_free(init_options.preopens);
  2311. if (!ret && uvwasi)
  2312. wasm_runtime_free(uvwasi);
  2313. return ret;
  2314. }
  2315. #endif /* end of WASM_ENABLE_UVWASI */
  2316. bool
  2317. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst)
  2318. {
  2319. #if WASM_ENABLE_INTERP != 0
  2320. if (module_inst->module_type == Wasm_Module_Bytecode
  2321. && ((WASMModuleInstance *)module_inst)->module->import_wasi_api)
  2322. return true;
  2323. #endif
  2324. #if WASM_ENABLE_AOT != 0
  2325. if (module_inst->module_type == Wasm_Module_AoT
  2326. && ((AOTModule *)((AOTModuleInstance *)module_inst)->aot_module.ptr)
  2327. ->import_wasi_api)
  2328. return true;
  2329. #endif
  2330. return false;
  2331. }
  2332. WASMFunctionInstanceCommon *
  2333. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst)
  2334. {
  2335. uint32 i;
  2336. #if WASM_ENABLE_INTERP != 0
  2337. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2338. WASMModuleInstance *wasm_inst = (WASMModuleInstance *)module_inst;
  2339. WASMFunctionInstance *func;
  2340. for (i = 0; i < wasm_inst->export_func_count; i++) {
  2341. if (!strcmp(wasm_inst->export_functions[i].name, "_start")) {
  2342. func = wasm_inst->export_functions[i].function;
  2343. if (func->u.func->func_type->param_count != 0
  2344. || func->u.func->func_type->result_count != 0) {
  2345. LOG_ERROR("Lookup wasi _start function failed: "
  2346. "invalid function type.\n");
  2347. return NULL;
  2348. }
  2349. return (WASMFunctionInstanceCommon *)func;
  2350. }
  2351. }
  2352. return NULL;
  2353. }
  2354. #endif
  2355. #if WASM_ENABLE_AOT != 0
  2356. if (module_inst->module_type == Wasm_Module_AoT) {
  2357. AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
  2358. AOTFunctionInstance *export_funcs =
  2359. (AOTFunctionInstance *)aot_inst->export_funcs.ptr;
  2360. for (i = 0; i < aot_inst->export_func_count; i++) {
  2361. if (!strcmp(export_funcs[i].func_name, "_start")) {
  2362. AOTFuncType *func_type = export_funcs[i].u.func.func_type;
  2363. if (func_type->param_count != 0
  2364. || func_type->result_count != 0) {
  2365. LOG_ERROR("Lookup wasi _start function failed: "
  2366. "invalid function type.\n");
  2367. return NULL;
  2368. }
  2369. return (WASMFunctionInstanceCommon *)&export_funcs[i];
  2370. }
  2371. }
  2372. return NULL;
  2373. }
  2374. #endif /* end of WASM_ENABLE_AOT */
  2375. return NULL;
  2376. }
  2377. #if WASM_ENABLE_UVWASI == 0
  2378. void
  2379. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  2380. {
  2381. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  2382. if (wasi_ctx) {
  2383. if (wasi_ctx->argv_environ) {
  2384. argv_environ_destroy(wasi_ctx->argv_environ);
  2385. wasm_runtime_free(wasi_ctx->argv_environ);
  2386. }
  2387. if (wasi_ctx->curfds) {
  2388. fd_table_destroy(wasi_ctx->curfds);
  2389. wasm_runtime_free(wasi_ctx->curfds);
  2390. }
  2391. if (wasi_ctx->prestats) {
  2392. fd_prestats_destroy(wasi_ctx->prestats);
  2393. wasm_runtime_free(wasi_ctx->prestats);
  2394. }
  2395. if (wasi_ctx->addr_pool) {
  2396. addr_pool_destroy(wasi_ctx->addr_pool);
  2397. wasm_runtime_free(wasi_ctx->addr_pool);
  2398. }
  2399. if (wasi_ctx->argv_buf)
  2400. wasm_runtime_free(wasi_ctx->argv_buf);
  2401. if (wasi_ctx->argv_list)
  2402. wasm_runtime_free(wasi_ctx->argv_list);
  2403. if (wasi_ctx->env_buf)
  2404. wasm_runtime_free(wasi_ctx->env_buf);
  2405. if (wasi_ctx->env_list)
  2406. wasm_runtime_free(wasi_ctx->env_list);
  2407. wasm_runtime_free(wasi_ctx);
  2408. }
  2409. }
  2410. #else
  2411. void
  2412. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  2413. {
  2414. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  2415. if (wasi_ctx) {
  2416. uvwasi_destroy(wasi_ctx);
  2417. wasm_runtime_free(wasi_ctx);
  2418. }
  2419. }
  2420. #endif
  2421. WASIContext *
  2422. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst)
  2423. {
  2424. #if WASM_ENABLE_INTERP != 0
  2425. if (module_inst->module_type == Wasm_Module_Bytecode)
  2426. return ((WASMModuleInstance *)module_inst)->wasi_ctx;
  2427. #endif
  2428. #if WASM_ENABLE_AOT != 0
  2429. if (module_inst->module_type == Wasm_Module_AoT)
  2430. return ((AOTModuleInstance *)module_inst)->wasi_ctx.ptr;
  2431. #endif
  2432. return NULL;
  2433. }
  2434. void
  2435. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  2436. WASIContext *wasi_ctx)
  2437. {
  2438. #if WASM_ENABLE_INTERP != 0
  2439. if (module_inst->module_type == Wasm_Module_Bytecode)
  2440. ((WASMModuleInstance *)module_inst)->wasi_ctx = wasi_ctx;
  2441. #endif
  2442. #if WASM_ENABLE_AOT != 0
  2443. if (module_inst->module_type == Wasm_Module_AoT)
  2444. ((AOTModuleInstance *)module_inst)->wasi_ctx.ptr = wasi_ctx;
  2445. #endif
  2446. }
  2447. #endif /* end of WASM_ENABLE_LIBC_WASI */
  2448. WASMModuleCommon *
  2449. wasm_exec_env_get_module(WASMExecEnv *exec_env)
  2450. {
  2451. WASMModuleInstanceCommon *module_inst =
  2452. wasm_runtime_get_module_inst(exec_env);
  2453. #if WASM_ENABLE_INTERP != 0
  2454. if (module_inst->module_type == Wasm_Module_Bytecode)
  2455. return (WASMModuleCommon *)((WASMModuleInstance *)module_inst)->module;
  2456. #endif
  2457. #if WASM_ENABLE_AOT != 0
  2458. if (module_inst->module_type == Wasm_Module_AoT)
  2459. return (WASMModuleCommon *)((AOTModuleInstance *)module_inst)
  2460. ->aot_module.ptr;
  2461. #endif
  2462. return NULL;
  2463. }
  2464. static union {
  2465. int a;
  2466. char b;
  2467. } __ue = { .a = 1 };
  2468. #define is_little_endian() (__ue.b == 1)
  2469. bool
  2470. wasm_runtime_register_natives(const char *module_name,
  2471. NativeSymbol *native_symbols,
  2472. uint32 n_native_symbols)
  2473. {
  2474. return wasm_native_register_natives(module_name, native_symbols,
  2475. n_native_symbols);
  2476. }
  2477. bool
  2478. wasm_runtime_register_natives_raw(const char *module_name,
  2479. NativeSymbol *native_symbols,
  2480. uint32 n_native_symbols)
  2481. {
  2482. return wasm_native_register_natives_raw(module_name, native_symbols,
  2483. n_native_symbols);
  2484. }
  2485. bool
  2486. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  2487. const WASMType *func_type, const char *signature,
  2488. void *attachment, uint32 *argv, uint32 argc,
  2489. uint32 *argv_ret)
  2490. {
  2491. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2492. typedef void (*NativeRawFuncPtr)(WASMExecEnv *, uint64 *);
  2493. NativeRawFuncPtr invokeNativeRaw = (NativeRawFuncPtr)func_ptr;
  2494. uint64 argv_buf[16] = { 0 }, *argv1 = argv_buf, *argv_dst, size;
  2495. uint32 *argv_src = argv, i, argc1, ptr_len;
  2496. uint32 arg_i32;
  2497. bool ret = false;
  2498. argc1 = func_type->param_count;
  2499. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  2500. size = sizeof(uint64) * (uint64)argc1;
  2501. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  2502. 0))) {
  2503. return false;
  2504. }
  2505. }
  2506. argv_dst = argv1;
  2507. /* Traverse secondly to fill in each argument */
  2508. for (i = 0; i < func_type->param_count; i++, argv_dst++) {
  2509. switch (func_type->types[i]) {
  2510. case VALUE_TYPE_I32:
  2511. #if WASM_ENABLE_REF_TYPES != 0
  2512. case VALUE_TYPE_FUNCREF:
  2513. #endif
  2514. {
  2515. *(uint32 *)argv_dst = arg_i32 = *argv_src++;
  2516. if (signature) {
  2517. if (signature[i + 1] == '*') {
  2518. /* param is a pointer */
  2519. if (signature[i + 2] == '~')
  2520. /* pointer with length followed */
  2521. ptr_len = *argv_src;
  2522. else
  2523. /* pointer without length followed */
  2524. ptr_len = 1;
  2525. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  2526. ptr_len))
  2527. goto fail;
  2528. *(uintptr_t *)argv_dst =
  2529. (uintptr_t)wasm_runtime_addr_app_to_native(module,
  2530. arg_i32);
  2531. }
  2532. else if (signature[i + 1] == '$') {
  2533. /* param is a string */
  2534. if (!wasm_runtime_validate_app_str_addr(module,
  2535. arg_i32))
  2536. goto fail;
  2537. *(uintptr_t *)argv_dst =
  2538. (uintptr_t)wasm_runtime_addr_app_to_native(module,
  2539. arg_i32);
  2540. }
  2541. }
  2542. break;
  2543. }
  2544. case VALUE_TYPE_I64:
  2545. case VALUE_TYPE_F64:
  2546. bh_memcpy_s(argv_dst, sizeof(uint64), argv_src,
  2547. sizeof(uint32) * 2);
  2548. argv_src += 2;
  2549. break;
  2550. case VALUE_TYPE_F32:
  2551. *(float32 *)argv_dst = *(float32 *)argv_src++;
  2552. break;
  2553. #if WASM_ENABLE_REF_TYPES != 0
  2554. case VALUE_TYPE_EXTERNREF:
  2555. {
  2556. uint32 externref_idx = *argv_src++;
  2557. void *externref_obj;
  2558. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  2559. goto fail;
  2560. bh_memcpy_s(argv_dst, sizeof(uintptr_t), argv_src,
  2561. sizeof(uintptr_t));
  2562. break;
  2563. }
  2564. #endif
  2565. default:
  2566. bh_assert(0);
  2567. break;
  2568. }
  2569. }
  2570. exec_env->attachment = attachment;
  2571. invokeNativeRaw(exec_env, argv1);
  2572. exec_env->attachment = NULL;
  2573. if (func_type->result_count > 0) {
  2574. switch (func_type->types[func_type->param_count]) {
  2575. case VALUE_TYPE_I32:
  2576. #if WASM_ENABLE_REF_TYPES != 0
  2577. case VALUE_TYPE_FUNCREF:
  2578. #endif
  2579. argv_ret[0] = *(uint32 *)argv1;
  2580. break;
  2581. case VALUE_TYPE_F32:
  2582. *(float32 *)argv_ret = *(float32 *)argv1;
  2583. break;
  2584. case VALUE_TYPE_I64:
  2585. case VALUE_TYPE_F64:
  2586. bh_memcpy_s(argv_ret, sizeof(uint32) * 2, argv1,
  2587. sizeof(uint64));
  2588. break;
  2589. #if WASM_ENABLE_REF_TYPES != 0
  2590. case VALUE_TYPE_EXTERNREF:
  2591. {
  2592. uint32 externref_idx;
  2593. uint64 externref_obj;
  2594. bh_memcpy_s(&externref_obj, sizeof(uint64), argv1,
  2595. sizeof(uint64));
  2596. if (!wasm_externref_obj2ref(exec_env->module_inst,
  2597. (void *)(uintptr_t)externref_obj,
  2598. &externref_idx))
  2599. goto fail;
  2600. argv_ret[0] = externref_idx;
  2601. break;
  2602. }
  2603. #endif
  2604. default:
  2605. bh_assert(0);
  2606. break;
  2607. }
  2608. }
  2609. ret = !wasm_runtime_get_exception(module) ? true : false;
  2610. fail:
  2611. if (argv1 != argv_buf)
  2612. wasm_runtime_free(argv1);
  2613. return ret;
  2614. }
  2615. /**
  2616. * Implementation of wasm_runtime_invoke_native()
  2617. */
  2618. /* The invoke native implementation on ARM platform with VFP co-processor */
  2619. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) \
  2620. || defined(BUILD_TARGET_RISCV32_ILP32D) \
  2621. || defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  2622. typedef void (*GenericFunctionPointer)();
  2623. int64
  2624. invokeNative(GenericFunctionPointer f, uint32 *args, uint32 n_stacks);
  2625. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  2626. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  2627. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  2628. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  2629. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  2630. static Float64FuncPtr invokeNative_Float64 =
  2631. (Float64FuncPtr)(uintptr_t)invokeNative;
  2632. static Float32FuncPtr invokeNative_Float32 =
  2633. (Float32FuncPtr)(uintptr_t)invokeNative;
  2634. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)(uintptr_t)invokeNative;
  2635. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)(uintptr_t)invokeNative;
  2636. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)(uintptr_t)invokeNative;
  2637. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  2638. #define MAX_REG_INTS 4
  2639. #define MAX_REG_FLOATS 16
  2640. #else
  2641. #define MAX_REG_INTS 8
  2642. #define MAX_REG_FLOATS 8
  2643. #endif
  2644. bool
  2645. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  2646. const WASMType *func_type, const char *signature,
  2647. void *attachment, uint32 *argv, uint32 argc,
  2648. uint32 *argv_ret)
  2649. {
  2650. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2651. /* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args */
  2652. uint32 argv_buf[32], *argv1 = argv_buf, *ints, *stacks, size;
  2653. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  2654. uint32 arg_i32, ptr_len;
  2655. uint32 result_count = func_type->result_count;
  2656. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  2657. bool ret = false;
  2658. #if WASM_ENABLE_REF_TYPES != 0
  2659. bool is_aot_func = (NULL == signature);
  2660. #endif
  2661. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  2662. uint32 *fps;
  2663. int n_fps = 0;
  2664. #else
  2665. #define fps ints
  2666. #define n_fps n_ints
  2667. #endif
  2668. n_ints++; /* exec env */
  2669. /* Traverse firstly to calculate stack args count */
  2670. for (i = 0; i < func_type->param_count; i++) {
  2671. switch (func_type->types[i]) {
  2672. case VALUE_TYPE_I32:
  2673. #if WASM_ENABLE_REF_TYPES != 0
  2674. case VALUE_TYPE_FUNCREF:
  2675. case VALUE_TYPE_EXTERNREF:
  2676. #endif
  2677. if (n_ints < MAX_REG_INTS)
  2678. n_ints++;
  2679. else
  2680. n_stacks++;
  2681. break;
  2682. case VALUE_TYPE_I64:
  2683. if (n_ints < MAX_REG_INTS - 1) {
  2684. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  2685. /* 64-bit data must be 8 bytes aligned in arm */
  2686. if (n_ints & 1)
  2687. n_ints++;
  2688. #endif
  2689. n_ints += 2;
  2690. }
  2691. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  2692. || defined(BUILD_TARGET_RISCV32_ILP32D) || defined(BUILD_TARGET_ARC)
  2693. /* part in register, part in stack */
  2694. else if (n_ints == MAX_REG_INTS - 1) {
  2695. n_ints++;
  2696. n_stacks++;
  2697. }
  2698. #endif
  2699. else {
  2700. /* 64-bit data in stack must be 8 bytes aligned
  2701. in arm and riscv32 */
  2702. #if !defined(BUILD_TARGET_ARC)
  2703. if (n_stacks & 1)
  2704. n_stacks++;
  2705. #endif
  2706. n_stacks += 2;
  2707. }
  2708. break;
  2709. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  2710. case VALUE_TYPE_F32:
  2711. if (n_fps < MAX_REG_FLOATS)
  2712. n_fps++;
  2713. else
  2714. n_stacks++;
  2715. break;
  2716. case VALUE_TYPE_F64:
  2717. if (n_fps < MAX_REG_FLOATS - 1) {
  2718. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  2719. /* 64-bit data must be 8 bytes aligned in arm */
  2720. if (n_fps & 1)
  2721. n_fps++;
  2722. #endif
  2723. n_fps += 2;
  2724. }
  2725. #if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  2726. else if (n_fps == MAX_REG_FLOATS - 1) {
  2727. n_fps++;
  2728. n_stacks++;
  2729. }
  2730. #endif
  2731. else {
  2732. /* 64-bit data in stack must be 8 bytes aligned
  2733. in arm and riscv32 */
  2734. #if !defined(BUILD_TARGET_ARC)
  2735. if (n_stacks & 1)
  2736. n_stacks++;
  2737. #endif
  2738. n_stacks += 2;
  2739. }
  2740. break;
  2741. #else /* BUILD_TARGET_RISCV32_ILP32D */
  2742. case VALUE_TYPE_F32:
  2743. case VALUE_TYPE_F64:
  2744. if (n_fps < MAX_REG_FLOATS) {
  2745. n_fps++;
  2746. }
  2747. else if (func_type->types[i] == VALUE_TYPE_F32
  2748. && n_ints < MAX_REG_INTS) {
  2749. /* use int reg firstly if available */
  2750. n_ints++;
  2751. }
  2752. else if (func_type->types[i] == VALUE_TYPE_F64
  2753. && n_ints < MAX_REG_INTS - 1) {
  2754. /* use int regs firstly if available */
  2755. if (n_ints & 1)
  2756. n_ints++;
  2757. ints += 2;
  2758. }
  2759. else {
  2760. /* 64-bit data in stack must be 8 bytes aligned in riscv32
  2761. */
  2762. if (n_stacks & 1)
  2763. n_stacks++;
  2764. n_stacks += 2;
  2765. }
  2766. break;
  2767. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  2768. default:
  2769. bh_assert(0);
  2770. break;
  2771. }
  2772. }
  2773. for (i = 0; i < ext_ret_count; i++) {
  2774. if (n_ints < MAX_REG_INTS)
  2775. n_ints++;
  2776. else
  2777. n_stacks++;
  2778. }
  2779. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  2780. argc1 = MAX_REG_INTS + MAX_REG_FLOATS + n_stacks;
  2781. #elif defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  2782. argc1 = MAX_REG_INTS + n_stacks;
  2783. #else /* for BUILD_TARGET_RISCV32_ILP32D */
  2784. argc1 = MAX_REG_INTS + MAX_REG_FLOATS * 2 + n_stacks;
  2785. #endif
  2786. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  2787. size = sizeof(uint32) * (uint32)argc1;
  2788. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  2789. 0))) {
  2790. return false;
  2791. }
  2792. }
  2793. ints = argv1;
  2794. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  2795. fps = ints + MAX_REG_INTS;
  2796. stacks = fps + MAX_REG_FLOATS;
  2797. #elif defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  2798. stacks = ints + MAX_REG_INTS;
  2799. #else /* for BUILD_TARGET_RISCV32_ILP32D */
  2800. fps = ints + MAX_REG_INTS;
  2801. stacks = fps + MAX_REG_FLOATS * 2;
  2802. #endif
  2803. n_ints = 0;
  2804. n_fps = 0;
  2805. n_stacks = 0;
  2806. ints[n_ints++] = (uint32)(uintptr_t)exec_env;
  2807. /* Traverse secondly to fill in each argument */
  2808. for (i = 0; i < func_type->param_count; i++) {
  2809. switch (func_type->types[i]) {
  2810. case VALUE_TYPE_I32:
  2811. #if WASM_ENABLE_REF_TYPES != 0
  2812. case VALUE_TYPE_FUNCREF:
  2813. #endif
  2814. {
  2815. arg_i32 = *argv_src++;
  2816. if (signature) {
  2817. if (signature[i + 1] == '*') {
  2818. /* param is a pointer */
  2819. if (signature[i + 2] == '~')
  2820. /* pointer with length followed */
  2821. ptr_len = *argv_src;
  2822. else
  2823. /* pointer without length followed */
  2824. ptr_len = 1;
  2825. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  2826. ptr_len))
  2827. goto fail;
  2828. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  2829. module, arg_i32);
  2830. }
  2831. else if (signature[i + 1] == '$') {
  2832. /* param is a string */
  2833. if (!wasm_runtime_validate_app_str_addr(module,
  2834. arg_i32))
  2835. goto fail;
  2836. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  2837. module, arg_i32);
  2838. }
  2839. }
  2840. if (n_ints < MAX_REG_INTS)
  2841. ints[n_ints++] = arg_i32;
  2842. else
  2843. stacks[n_stacks++] = arg_i32;
  2844. break;
  2845. }
  2846. case VALUE_TYPE_I64:
  2847. {
  2848. if (n_ints < MAX_REG_INTS - 1) {
  2849. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  2850. /* 64-bit data must be 8 bytes aligned in arm */
  2851. if (n_ints & 1)
  2852. n_ints++;
  2853. #endif
  2854. ints[n_ints++] = *argv_src++;
  2855. ints[n_ints++] = *argv_src++;
  2856. }
  2857. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  2858. || defined(BUILD_TARGET_RISCV32_ILP32D) || defined(BUILD_TARGET_ARC)
  2859. else if (n_ints == MAX_REG_INTS - 1) {
  2860. ints[n_ints++] = *argv_src++;
  2861. stacks[n_stacks++] = *argv_src++;
  2862. }
  2863. #endif
  2864. else {
  2865. /* 64-bit data in stack must be 8 bytes aligned
  2866. in arm and riscv32 */
  2867. #if !defined(BUILD_TARGET_ARC)
  2868. if (n_stacks & 1)
  2869. n_stacks++;
  2870. #endif
  2871. stacks[n_stacks++] = *argv_src++;
  2872. stacks[n_stacks++] = *argv_src++;
  2873. }
  2874. break;
  2875. }
  2876. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  2877. case VALUE_TYPE_F32:
  2878. {
  2879. if (n_fps < MAX_REG_FLOATS)
  2880. *(float32 *)&fps[n_fps++] = *(float32 *)argv_src++;
  2881. else
  2882. *(float32 *)&stacks[n_stacks++] = *(float32 *)argv_src++;
  2883. break;
  2884. }
  2885. case VALUE_TYPE_F64:
  2886. {
  2887. if (n_fps < MAX_REG_FLOATS - 1) {
  2888. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  2889. /* 64-bit data must be 8 bytes aligned in arm */
  2890. if (n_fps & 1)
  2891. n_fps++;
  2892. #endif
  2893. fps[n_fps++] = *argv_src++;
  2894. fps[n_fps++] = *argv_src++;
  2895. }
  2896. #if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  2897. else if (n_fps == MAX_REG_FLOATS - 1) {
  2898. fps[n_fps++] = *argv_src++;
  2899. stacks[n_stacks++] = *argv_src++;
  2900. }
  2901. #endif
  2902. else {
  2903. /* 64-bit data in stack must be 8 bytes aligned
  2904. in arm and riscv32 */
  2905. #if !defined(BUILD_TARGET_ARC)
  2906. if (n_stacks & 1)
  2907. n_stacks++;
  2908. #endif
  2909. stacks[n_stacks++] = *argv_src++;
  2910. stacks[n_stacks++] = *argv_src++;
  2911. }
  2912. break;
  2913. }
  2914. #else /* BUILD_TARGET_RISCV32_ILP32D */
  2915. case VALUE_TYPE_F32:
  2916. case VALUE_TYPE_F64:
  2917. {
  2918. if (n_fps < MAX_REG_FLOATS) {
  2919. if (func_type->types[i] == VALUE_TYPE_F32) {
  2920. *(float32 *)&fps[n_fps * 2] = *(float32 *)argv_src++;
  2921. /* NaN boxing, the upper bits of a valid NaN-boxed
  2922. value must be all 1s. */
  2923. fps[n_fps * 2 + 1] = 0xFFFFFFFF;
  2924. }
  2925. else {
  2926. *(float64 *)&fps[n_fps * 2] = *(float64 *)argv_src;
  2927. argv_src += 2;
  2928. }
  2929. n_fps++;
  2930. }
  2931. else if (func_type->types[i] == VALUE_TYPE_F32
  2932. && n_ints < MAX_REG_INTS) {
  2933. /* use int reg firstly if available */
  2934. *(float32 *)&ints[n_ints++] = *(float32 *)argv_src++;
  2935. }
  2936. else if (func_type->types[i] == VALUE_TYPE_F64
  2937. && n_ints < MAX_REG_INTS - 1) {
  2938. /* use int regs firstly if available */
  2939. if (n_ints & 1)
  2940. n_ints++;
  2941. *(float64 *)&ints[n_ints] = *(float64 *)argv_src;
  2942. n_ints += 2;
  2943. argv_src += 2;
  2944. }
  2945. else {
  2946. /* 64-bit data in stack must be 8 bytes aligned in riscv32
  2947. */
  2948. if (n_stacks & 1)
  2949. n_stacks++;
  2950. if (func_type->types[i] == VALUE_TYPE_F32) {
  2951. *(float32 *)&stacks[n_stacks] = *(float32 *)argv_src++;
  2952. /* NaN boxing, the upper bits of a valid NaN-boxed
  2953. value must be all 1s. */
  2954. stacks[n_stacks + 1] = 0xFFFFFFFF;
  2955. }
  2956. else {
  2957. *(float64 *)&stacks[n_stacks] = *(float64 *)argv_src;
  2958. argv_src += 2;
  2959. }
  2960. n_stacks += 2;
  2961. }
  2962. break;
  2963. }
  2964. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  2965. #if WASM_ENABLE_REF_TYPES != 0
  2966. case VALUE_TYPE_EXTERNREF:
  2967. {
  2968. uint32 externref_idx = *argv_src++;
  2969. if (is_aot_func) {
  2970. if (n_ints < MAX_REG_INTS)
  2971. ints[n_ints++] = externref_idx;
  2972. else
  2973. stacks[n_stacks++] = externref_idx;
  2974. }
  2975. else {
  2976. void *externref_obj;
  2977. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  2978. goto fail;
  2979. if (n_ints < MAX_REG_INTS)
  2980. ints[n_ints++] = (uintptr_t)externref_obj;
  2981. else
  2982. stacks[n_stacks++] = (uintptr_t)externref_obj;
  2983. }
  2984. break;
  2985. }
  2986. #endif
  2987. default:
  2988. bh_assert(0);
  2989. break;
  2990. }
  2991. }
  2992. /* Save extra result values' address to argv1 */
  2993. for (i = 0; i < ext_ret_count; i++) {
  2994. if (n_ints < MAX_REG_INTS)
  2995. ints[n_ints++] = *(uint32 *)argv_src++;
  2996. else
  2997. stacks[n_stacks++] = *(uint32 *)argv_src++;
  2998. }
  2999. exec_env->attachment = attachment;
  3000. if (func_type->result_count == 0) {
  3001. invokeNative_Void(func_ptr, argv1, n_stacks);
  3002. }
  3003. else {
  3004. switch (func_type->types[func_type->param_count]) {
  3005. case VALUE_TYPE_I32:
  3006. #if WASM_ENABLE_REF_TYPES != 0
  3007. case VALUE_TYPE_FUNCREF:
  3008. #endif
  3009. argv_ret[0] =
  3010. (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  3011. break;
  3012. case VALUE_TYPE_I64:
  3013. PUT_I64_TO_ADDR(argv_ret,
  3014. invokeNative_Int64(func_ptr, argv1, n_stacks));
  3015. break;
  3016. case VALUE_TYPE_F32:
  3017. *(float32 *)argv_ret =
  3018. invokeNative_Float32(func_ptr, argv1, n_stacks);
  3019. break;
  3020. case VALUE_TYPE_F64:
  3021. PUT_F64_TO_ADDR(
  3022. argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  3023. break;
  3024. #if WASM_ENABLE_REF_TYPES != 0
  3025. case VALUE_TYPE_EXTERNREF:
  3026. {
  3027. if (is_aot_func) {
  3028. uint32 externref_idx =
  3029. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  3030. argv_ret[0] = externref_idx;
  3031. }
  3032. else {
  3033. uint32 externref_idx;
  3034. void *externref_obj;
  3035. externref_obj = (void *)(uintptr_t)invokeNative_Int32(
  3036. func_ptr, argv1, argc1);
  3037. if (!wasm_externref_obj2ref(exec_env->module_inst,
  3038. externref_obj, &externref_idx))
  3039. goto fail;
  3040. argv_ret[0] = externref_idx;
  3041. }
  3042. break;
  3043. }
  3044. #endif
  3045. default:
  3046. bh_assert(0);
  3047. break;
  3048. }
  3049. }
  3050. exec_env->attachment = NULL;
  3051. ret = !wasm_runtime_get_exception(module) ? true : false;
  3052. fail:
  3053. if (argv1 != argv_buf)
  3054. wasm_runtime_free(argv1);
  3055. return ret;
  3056. }
  3057. #endif /* end of defined(BUILD_TARGET_ARM_VFP) \
  3058. || defined(BUILD_TARGET_THUMB_VFP) \
  3059. || defined(BUILD_TARGET_RISCV32_ILP32D) \
  3060. || defined(BUILD_TARGET_RISCV32_ILP32) \
  3061. || defined(BUILD_TARGET_ARC) */
  3062. #if defined(BUILD_TARGET_X86_32) || defined(BUILD_TARGET_ARM) \
  3063. || defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_MIPS) \
  3064. || defined(BUILD_TARGET_XTENSA)
  3065. typedef void (*GenericFunctionPointer)();
  3066. int64
  3067. invokeNative(GenericFunctionPointer f, uint32 *args, uint32 sz);
  3068. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3069. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3070. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3071. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3072. typedef void (*VoidFuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  3073. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)invokeNative;
  3074. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)(uintptr_t)invokeNative;
  3075. static Float64FuncPtr invokeNative_Float64 =
  3076. (Float64FuncPtr)(uintptr_t)invokeNative;
  3077. static Float32FuncPtr invokeNative_Float32 =
  3078. (Float32FuncPtr)(uintptr_t)invokeNative;
  3079. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)(uintptr_t)invokeNative;
  3080. static inline void
  3081. word_copy(uint32 *dest, uint32 *src, unsigned num)
  3082. {
  3083. for (; num > 0; num--)
  3084. *dest++ = *src++;
  3085. }
  3086. bool
  3087. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  3088. const WASMType *func_type, const char *signature,
  3089. void *attachment, uint32 *argv, uint32 argc,
  3090. uint32 *argv_ret)
  3091. {
  3092. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  3093. uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0;
  3094. uint32 arg_i32, ptr_len;
  3095. uint32 result_count = func_type->result_count;
  3096. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  3097. uint64 size;
  3098. bool ret = false;
  3099. #if WASM_ENABLE_REF_TYPES != 0
  3100. bool is_aot_func = (NULL == signature);
  3101. #endif
  3102. #if defined(BUILD_TARGET_X86_32)
  3103. argc1 = argc + ext_ret_count + 2;
  3104. #else
  3105. /* arm/thumb/mips/xtensa, 64-bit data must be 8 bytes aligned,
  3106. so we need to allocate more memory. */
  3107. argc1 = func_type->param_count * 2 + ext_ret_count + 2;
  3108. #endif
  3109. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  3110. size = sizeof(uint32) * (uint64)argc1;
  3111. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  3112. 0))) {
  3113. return false;
  3114. }
  3115. }
  3116. for (i = 0; i < sizeof(WASMExecEnv *) / sizeof(uint32); i++)
  3117. argv1[j++] = ((uint32 *)&exec_env)[i];
  3118. for (i = 0; i < func_type->param_count; i++) {
  3119. switch (func_type->types[i]) {
  3120. case VALUE_TYPE_I32:
  3121. #if WASM_ENABLE_REF_TYPES != 0
  3122. case VALUE_TYPE_FUNCREF:
  3123. #endif
  3124. {
  3125. arg_i32 = *argv++;
  3126. if (signature) {
  3127. if (signature[i + 1] == '*') {
  3128. /* param is a pointer */
  3129. if (signature[i + 2] == '~')
  3130. /* pointer with length followed */
  3131. ptr_len = *argv;
  3132. else
  3133. /* pointer without length followed */
  3134. ptr_len = 1;
  3135. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  3136. ptr_len))
  3137. goto fail;
  3138. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3139. module, arg_i32);
  3140. }
  3141. else if (signature[i + 1] == '$') {
  3142. /* param is a string */
  3143. if (!wasm_runtime_validate_app_str_addr(module,
  3144. arg_i32))
  3145. goto fail;
  3146. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3147. module, arg_i32);
  3148. }
  3149. }
  3150. argv1[j++] = arg_i32;
  3151. break;
  3152. }
  3153. case VALUE_TYPE_I64:
  3154. case VALUE_TYPE_F64:
  3155. #if !defined(BUILD_TARGET_X86_32)
  3156. /* 64-bit data must be 8 bytes aligned in arm, thumb, mips
  3157. and xtensa */
  3158. if (j & 1)
  3159. j++;
  3160. #endif
  3161. argv1[j++] = *argv++;
  3162. argv1[j++] = *argv++;
  3163. break;
  3164. case VALUE_TYPE_F32:
  3165. argv1[j++] = *argv++;
  3166. break;
  3167. #if WASM_ENABLE_REF_TYPES != 0
  3168. case VALUE_TYPE_EXTERNREF:
  3169. {
  3170. uint32 externref_idx = *argv++;
  3171. if (is_aot_func) {
  3172. argv1[j++] = externref_idx;
  3173. }
  3174. else {
  3175. void *externref_obj;
  3176. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  3177. goto fail;
  3178. argv1[j++] = (uintptr_t)externref_obj;
  3179. }
  3180. break;
  3181. }
  3182. #endif
  3183. default:
  3184. bh_assert(0);
  3185. break;
  3186. }
  3187. }
  3188. /* Save extra result values' address to argv1 */
  3189. word_copy(argv1 + j, argv, ext_ret_count);
  3190. argc1 = j + ext_ret_count;
  3191. exec_env->attachment = attachment;
  3192. if (func_type->result_count == 0) {
  3193. invokeNative_Void(func_ptr, argv1, argc1);
  3194. }
  3195. else {
  3196. switch (func_type->types[func_type->param_count]) {
  3197. case VALUE_TYPE_I32:
  3198. #if WASM_ENABLE_REF_TYPES != 0
  3199. case VALUE_TYPE_FUNCREF:
  3200. #endif
  3201. argv_ret[0] =
  3202. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  3203. break;
  3204. case VALUE_TYPE_I64:
  3205. PUT_I64_TO_ADDR(argv_ret,
  3206. invokeNative_Int64(func_ptr, argv1, argc1));
  3207. break;
  3208. case VALUE_TYPE_F32:
  3209. *(float32 *)argv_ret =
  3210. invokeNative_Float32(func_ptr, argv1, argc1);
  3211. break;
  3212. case VALUE_TYPE_F64:
  3213. PUT_F64_TO_ADDR(argv_ret,
  3214. invokeNative_Float64(func_ptr, argv1, argc1));
  3215. break;
  3216. #if WASM_ENABLE_REF_TYPES != 0
  3217. case VALUE_TYPE_EXTERNREF:
  3218. {
  3219. if (is_aot_func) {
  3220. uint32 externref_idx =
  3221. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  3222. argv_ret[0] = externref_idx;
  3223. }
  3224. else {
  3225. void *externref_obj = (void *)(uintptr_t)invokeNative_Int32(
  3226. func_ptr, argv1, argc1);
  3227. uint32 externref_idx;
  3228. if (!wasm_externref_obj2ref(exec_env->module_inst,
  3229. externref_obj, &externref_idx))
  3230. goto fail;
  3231. argv_ret[0] = externref_idx;
  3232. }
  3233. break;
  3234. }
  3235. #endif
  3236. default:
  3237. bh_assert(0);
  3238. break;
  3239. }
  3240. }
  3241. exec_env->attachment = NULL;
  3242. ret = !wasm_runtime_get_exception(module) ? true : false;
  3243. fail:
  3244. if (argv1 != argv_buf)
  3245. wasm_runtime_free(argv1);
  3246. return ret;
  3247. }
  3248. #endif /* end of defined(BUILD_TARGET_X86_32) \
  3249. || defined(BUILD_TARGET_ARM) \
  3250. || defined(BUILD_TARGET_THUMB) \
  3251. || defined(BUILD_TARGET_MIPS) \
  3252. || defined(BUILD_TARGET_XTENSA) */
  3253. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  3254. || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
  3255. || defined(BUILD_TARGET_RISCV64_LP64)
  3256. #if WASM_ENABLE_SIMD != 0
  3257. #ifdef v128
  3258. #undef v128
  3259. #endif
  3260. #if defined(_WIN32) || defined(_WIN32_)
  3261. typedef union __declspec(intrin_type) __declspec(align(8)) v128 {
  3262. __int8 m128i_i8[16];
  3263. __int16 m128i_i16[8];
  3264. __int32 m128i_i32[4];
  3265. __int64 m128i_i64[2];
  3266. unsigned __int8 m128i_u8[16];
  3267. unsigned __int16 m128i_u16[8];
  3268. unsigned __int32 m128i_u32[4];
  3269. unsigned __int64 m128i_u64[2];
  3270. } v128;
  3271. #elif defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  3272. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3273. || defined(BUILD_TARGET_RISCV64_LP64)
  3274. typedef long long v128
  3275. __attribute__((__vector_size__(16), __may_alias__, __aligned__(1)));
  3276. #elif defined(BUILD_TARGET_AARCH64)
  3277. #include <arm_neon.h>
  3278. typedef uint32x4_t __m128i;
  3279. #define v128 __m128i
  3280. #endif
  3281. #endif /* end of WASM_ENABLE_SIMD != 0 */
  3282. typedef void (*GenericFunctionPointer)();
  3283. #if defined(__APPLE__) || defined(__MACH__)
  3284. /**
  3285. * Define the return type as 'void' in MacOS, since after converting
  3286. * 'int64 invokeNative' into 'float64 invokeNative_Float64', the
  3287. * return value passing might be invalid, the caller reads the return
  3288. * value from register rax but not xmm0.
  3289. */
  3290. void
  3291. invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks);
  3292. #else
  3293. int64
  3294. invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks);
  3295. #endif
  3296. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3297. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3298. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3299. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3300. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3301. static Float64FuncPtr invokeNative_Float64 =
  3302. (Float64FuncPtr)(uintptr_t)invokeNative;
  3303. static Float32FuncPtr invokeNative_Float32 =
  3304. (Float32FuncPtr)(uintptr_t)invokeNative;
  3305. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)(uintptr_t)invokeNative;
  3306. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)(uintptr_t)invokeNative;
  3307. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)(uintptr_t)invokeNative;
  3308. #if WASM_ENABLE_SIMD != 0
  3309. typedef v128 (*V128FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  3310. static V128FuncPtr invokeNative_V128 = (V128FuncPtr)(uintptr_t)invokeNative;
  3311. #endif
  3312. #if defined(_WIN32) || defined(_WIN32_)
  3313. #define MAX_REG_FLOATS 4
  3314. #define MAX_REG_INTS 4
  3315. #else /* else of defined(_WIN32) || defined(_WIN32_) */
  3316. #define MAX_REG_FLOATS 8
  3317. #if defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
  3318. || defined(BUILD_TARGET_RISCV64_LP64)
  3319. #define MAX_REG_INTS 8
  3320. #else
  3321. #define MAX_REG_INTS 6
  3322. #endif /* end of defined(BUILD_TARGET_AARCH64) \
  3323. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3324. || defined(BUILD_TARGET_RISCV64_LP64) */
  3325. #endif /* end of defined(_WIN32) || defined(_WIN32_) */
  3326. bool
  3327. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  3328. const WASMType *func_type, const char *signature,
  3329. void *attachment, uint32 *argv, uint32 argc,
  3330. uint32 *argv_ret)
  3331. {
  3332. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  3333. uint64 argv_buf[32] = { 0 }, *argv1 = argv_buf, *ints, *stacks, size,
  3334. arg_i64;
  3335. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  3336. uint32 arg_i32, ptr_len;
  3337. uint32 result_count = func_type->result_count;
  3338. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  3339. bool ret = false;
  3340. #if WASM_ENABLE_REF_TYPES != 0
  3341. bool is_aot_func = (NULL == signature);
  3342. #endif
  3343. #ifndef BUILD_TARGET_RISCV64_LP64
  3344. #if WASM_ENABLE_SIMD == 0
  3345. uint64 *fps;
  3346. #else
  3347. v128 *fps;
  3348. #endif
  3349. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  3350. #define fps ints
  3351. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  3352. #if defined(_WIN32) || defined(_WIN32_) || defined(BUILD_TARGET_RISCV64_LP64)
  3353. /* important difference in calling conventions */
  3354. #define n_fps n_ints
  3355. #else
  3356. int n_fps = 0;
  3357. #endif
  3358. #if WASM_ENABLE_SIMD == 0
  3359. argc1 = 1 + MAX_REG_FLOATS + (uint32)func_type->param_count + ext_ret_count;
  3360. #else
  3361. argc1 = 1 + MAX_REG_FLOATS * 2 + (uint32)func_type->param_count * 2
  3362. + ext_ret_count;
  3363. #endif
  3364. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  3365. size = sizeof(uint64) * (uint64)argc1;
  3366. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  3367. 0))) {
  3368. return false;
  3369. }
  3370. }
  3371. #ifndef BUILD_TARGET_RISCV64_LP64
  3372. #if WASM_ENABLE_SIMD == 0
  3373. fps = argv1;
  3374. ints = fps + MAX_REG_FLOATS;
  3375. #else
  3376. fps = (v128 *)argv1;
  3377. ints = (uint64 *)(fps + MAX_REG_FLOATS);
  3378. #endif
  3379. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  3380. ints = argv1;
  3381. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  3382. stacks = ints + MAX_REG_INTS;
  3383. ints[n_ints++] = (uint64)(uintptr_t)exec_env;
  3384. for (i = 0; i < func_type->param_count; i++) {
  3385. switch (func_type->types[i]) {
  3386. case VALUE_TYPE_I32:
  3387. #if WASM_ENABLE_REF_TYPES != 0
  3388. case VALUE_TYPE_FUNCREF:
  3389. #endif
  3390. {
  3391. arg_i32 = *argv_src++;
  3392. arg_i64 = arg_i32;
  3393. if (signature) {
  3394. if (signature[i + 1] == '*') {
  3395. /* param is a pointer */
  3396. if (signature[i + 2] == '~')
  3397. /* pointer with length followed */
  3398. ptr_len = *argv_src;
  3399. else
  3400. /* pointer without length followed */
  3401. ptr_len = 1;
  3402. if (!wasm_runtime_validate_app_addr(module, arg_i32,
  3403. ptr_len))
  3404. goto fail;
  3405. arg_i64 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3406. module, arg_i32);
  3407. }
  3408. else if (signature[i + 1] == '$') {
  3409. /* param is a string */
  3410. if (!wasm_runtime_validate_app_str_addr(module,
  3411. arg_i32))
  3412. goto fail;
  3413. arg_i64 = (uintptr_t)wasm_runtime_addr_app_to_native(
  3414. module, arg_i32);
  3415. }
  3416. }
  3417. if (n_ints < MAX_REG_INTS)
  3418. ints[n_ints++] = arg_i64;
  3419. else
  3420. stacks[n_stacks++] = arg_i64;
  3421. break;
  3422. }
  3423. case VALUE_TYPE_I64:
  3424. if (n_ints < MAX_REG_INTS)
  3425. ints[n_ints++] = *(uint64 *)argv_src;
  3426. else
  3427. stacks[n_stacks++] = *(uint64 *)argv_src;
  3428. argv_src += 2;
  3429. break;
  3430. case VALUE_TYPE_F32:
  3431. if (n_fps < MAX_REG_FLOATS) {
  3432. *(float32 *)&fps[n_fps++] = *(float32 *)argv_src++;
  3433. }
  3434. else {
  3435. *(float32 *)&stacks[n_stacks++] = *(float32 *)argv_src++;
  3436. }
  3437. break;
  3438. case VALUE_TYPE_F64:
  3439. if (n_fps < MAX_REG_FLOATS) {
  3440. *(float64 *)&fps[n_fps++] = *(float64 *)argv_src;
  3441. }
  3442. else {
  3443. *(float64 *)&stacks[n_stacks++] = *(float64 *)argv_src;
  3444. }
  3445. argv_src += 2;
  3446. break;
  3447. #if WASM_ENABLE_REF_TYPES != 0
  3448. case VALUE_TYPE_EXTERNREF:
  3449. {
  3450. uint32 externref_idx = *argv_src++;
  3451. if (is_aot_func) {
  3452. if (n_ints < MAX_REG_INTS)
  3453. ints[n_ints++] = externref_idx;
  3454. else
  3455. stacks[n_stacks++] = externref_idx;
  3456. }
  3457. else {
  3458. void *externref_obj;
  3459. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  3460. goto fail;
  3461. if (n_ints < MAX_REG_INTS)
  3462. ints[n_ints++] = (uintptr_t)externref_obj;
  3463. else
  3464. stacks[n_stacks++] = (uintptr_t)externref_obj;
  3465. }
  3466. break;
  3467. }
  3468. #endif
  3469. #if WASM_ENABLE_SIMD != 0
  3470. case VALUE_TYPE_V128:
  3471. if (n_fps < MAX_REG_FLOATS) {
  3472. *(v128 *)&fps[n_fps++] = *(v128 *)argv_src;
  3473. }
  3474. else {
  3475. *(v128 *)&stacks[n_stacks++] = *(v128 *)argv_src;
  3476. n_stacks++;
  3477. }
  3478. argv_src += 4;
  3479. break;
  3480. #endif
  3481. default:
  3482. bh_assert(0);
  3483. break;
  3484. }
  3485. }
  3486. /* Save extra result values' address to argv1 */
  3487. for (i = 0; i < ext_ret_count; i++) {
  3488. if (n_ints < MAX_REG_INTS)
  3489. ints[n_ints++] = *(uint64 *)argv_src;
  3490. else
  3491. stacks[n_stacks++] = *(uint64 *)argv_src;
  3492. argv_src += 2;
  3493. }
  3494. exec_env->attachment = attachment;
  3495. if (result_count == 0) {
  3496. invokeNative_Void(func_ptr, argv1, n_stacks);
  3497. }
  3498. else {
  3499. /* Invoke the native function and get the first result value */
  3500. switch (func_type->types[func_type->param_count]) {
  3501. case VALUE_TYPE_I32:
  3502. #if WASM_ENABLE_REF_TYPES != 0
  3503. case VALUE_TYPE_FUNCREF:
  3504. #endif
  3505. argv_ret[0] =
  3506. (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  3507. break;
  3508. case VALUE_TYPE_I64:
  3509. PUT_I64_TO_ADDR(argv_ret,
  3510. invokeNative_Int64(func_ptr, argv1, n_stacks));
  3511. break;
  3512. case VALUE_TYPE_F32:
  3513. *(float32 *)argv_ret =
  3514. invokeNative_Float32(func_ptr, argv1, n_stacks);
  3515. break;
  3516. case VALUE_TYPE_F64:
  3517. PUT_F64_TO_ADDR(
  3518. argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  3519. break;
  3520. #if WASM_ENABLE_REF_TYPES != 0
  3521. case VALUE_TYPE_EXTERNREF:
  3522. {
  3523. if (is_aot_func) {
  3524. argv_ret[0] = invokeNative_Int32(func_ptr, argv1, n_stacks);
  3525. }
  3526. else {
  3527. uint32 externref_idx;
  3528. void *externref_obj = (void *)(uintptr_t)invokeNative_Int64(
  3529. func_ptr, argv1, n_stacks);
  3530. if (!wasm_externref_obj2ref(exec_env->module_inst,
  3531. externref_obj, &externref_idx))
  3532. goto fail;
  3533. argv_ret[0] = externref_idx;
  3534. }
  3535. break;
  3536. }
  3537. #endif
  3538. #if WASM_ENABLE_SIMD != 0
  3539. case VALUE_TYPE_V128:
  3540. *(v128 *)argv_ret =
  3541. invokeNative_V128(func_ptr, argv1, n_stacks);
  3542. break;
  3543. #endif
  3544. default:
  3545. bh_assert(0);
  3546. break;
  3547. }
  3548. }
  3549. exec_env->attachment = NULL;
  3550. ret = !wasm_runtime_get_exception(module) ? true : false;
  3551. fail:
  3552. if (argv1 != argv_buf)
  3553. wasm_runtime_free(argv1);
  3554. return ret;
  3555. }
  3556. #endif /* end of defined(BUILD_TARGET_X86_64) \
  3557. || defined(BUILD_TARGET_AMD_64) \
  3558. || defined(BUILD_TARGET_AARCH64) \
  3559. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3560. || defined(BUILD_TARGET_RISCV64_LP64) */
  3561. bool
  3562. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32_t element_indices,
  3563. uint32_t argc, uint32_t argv[])
  3564. {
  3565. if (!wasm_runtime_exec_env_check(exec_env)) {
  3566. LOG_ERROR("Invalid exec env stack info.");
  3567. return false;
  3568. }
  3569. /* this function is called from native code, so exec_env->handle and
  3570. exec_env->native_stack_boundary must have been set, we don't set
  3571. it again */
  3572. #if WASM_ENABLE_INTERP != 0
  3573. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  3574. return wasm_call_indirect(exec_env, 0, element_indices, argc, argv);
  3575. #endif
  3576. #if WASM_ENABLE_AOT != 0
  3577. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  3578. return aot_call_indirect(exec_env, 0, element_indices, argc, argv);
  3579. #endif
  3580. return false;
  3581. }
  3582. static void
  3583. exchange_uint32(uint8 *p_data)
  3584. {
  3585. uint8 value = *p_data;
  3586. *p_data = *(p_data + 3);
  3587. *(p_data + 3) = value;
  3588. value = *(p_data + 1);
  3589. *(p_data + 1) = *(p_data + 2);
  3590. *(p_data + 2) = value;
  3591. }
  3592. static void
  3593. exchange_uint64(uint8 *p_data)
  3594. {
  3595. uint32 value;
  3596. value = *(uint32 *)p_data;
  3597. *(uint32 *)p_data = *(uint32 *)(p_data + 4);
  3598. *(uint32 *)(p_data + 4) = value;
  3599. exchange_uint32(p_data);
  3600. exchange_uint32(p_data + 4);
  3601. }
  3602. void
  3603. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2)
  3604. {
  3605. uint64 u1, u2;
  3606. bh_memcpy_s(&u1, 8, bytes, 8);
  3607. bh_memcpy_s(&u2, 8, bytes + 8, 8);
  3608. if (!is_little_endian()) {
  3609. exchange_uint64((uint8 *)&u1);
  3610. exchange_uint64((uint8 *)&u2);
  3611. *ret1 = u2;
  3612. *ret2 = u1;
  3613. }
  3614. else {
  3615. *ret1 = u1;
  3616. *ret2 = u2;
  3617. }
  3618. }
  3619. #if WASM_ENABLE_THREAD_MGR != 0
  3620. typedef struct WASMThreadArg {
  3621. WASMExecEnv *new_exec_env;
  3622. wasm_thread_callback_t callback;
  3623. void *arg;
  3624. } WASMThreadArg;
  3625. WASMExecEnv *
  3626. wasm_runtime_spawn_exec_env(WASMExecEnv *exec_env)
  3627. {
  3628. return wasm_cluster_spawn_exec_env(exec_env);
  3629. }
  3630. void
  3631. wasm_runtime_destroy_spawned_exec_env(WASMExecEnv *exec_env)
  3632. {
  3633. wasm_cluster_destroy_spawned_exec_env(exec_env);
  3634. }
  3635. static void *
  3636. wasm_runtime_thread_routine(void *arg)
  3637. {
  3638. WASMThreadArg *thread_arg = (WASMThreadArg *)arg;
  3639. void *ret;
  3640. bh_assert(thread_arg->new_exec_env);
  3641. ret = thread_arg->callback(thread_arg->new_exec_env, thread_arg->arg);
  3642. wasm_runtime_destroy_spawned_exec_env(thread_arg->new_exec_env);
  3643. wasm_runtime_free(thread_arg);
  3644. os_thread_exit(ret);
  3645. return ret;
  3646. }
  3647. int32
  3648. wasm_runtime_spawn_thread(WASMExecEnv *exec_env, wasm_thread_t *tid,
  3649. wasm_thread_callback_t callback, void *arg)
  3650. {
  3651. WASMExecEnv *new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
  3652. WASMThreadArg *thread_arg;
  3653. int32 ret;
  3654. if (!new_exec_env)
  3655. return -1;
  3656. if (!(thread_arg = wasm_runtime_malloc(sizeof(WASMThreadArg)))) {
  3657. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  3658. return -1;
  3659. }
  3660. thread_arg->new_exec_env = new_exec_env;
  3661. thread_arg->callback = callback;
  3662. thread_arg->arg = arg;
  3663. ret = os_thread_create((korp_tid *)tid, wasm_runtime_thread_routine,
  3664. thread_arg, APP_THREAD_STACK_SIZE_DEFAULT);
  3665. if (ret != 0) {
  3666. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  3667. wasm_runtime_free(thread_arg);
  3668. }
  3669. return ret;
  3670. }
  3671. int32
  3672. wasm_runtime_join_thread(wasm_thread_t tid, void **retval)
  3673. {
  3674. return os_thread_join((korp_tid)tid, retval);
  3675. }
  3676. #endif /* end of WASM_ENABLE_THREAD_MGR */
  3677. #if WASM_ENABLE_REF_TYPES != 0
  3678. static korp_mutex externref_lock;
  3679. static uint32 externref_global_id = 1;
  3680. static HashMap *externref_map;
  3681. typedef struct ExternRefMapNode {
  3682. /* The extern object from runtime embedder */
  3683. void *extern_obj;
  3684. /* The module instance it belongs to */
  3685. WASMModuleInstanceCommon *module_inst;
  3686. /* Whether it is retained */
  3687. bool retained;
  3688. /* Whether it is marked by runtime */
  3689. bool marked;
  3690. } ExternRefMapNode;
  3691. static uint32
  3692. wasm_externref_hash(const void *key)
  3693. {
  3694. uint32 externref_idx = (uint32)(uintptr_t)key;
  3695. return externref_idx;
  3696. }
  3697. static bool
  3698. wasm_externref_equal(void *key1, void *key2)
  3699. {
  3700. uint32 externref_idx1 = (uint32)(uintptr_t)key1;
  3701. uint32 externref_idx2 = (uint32)(uintptr_t)key2;
  3702. return externref_idx1 == externref_idx2 ? true : false;
  3703. }
  3704. static bool
  3705. wasm_externref_map_init()
  3706. {
  3707. if (os_mutex_init(&externref_lock) != 0)
  3708. return false;
  3709. if (!(externref_map = bh_hash_map_create(32, false, wasm_externref_hash,
  3710. wasm_externref_equal, NULL,
  3711. wasm_runtime_free))) {
  3712. os_mutex_destroy(&externref_lock);
  3713. return false;
  3714. }
  3715. externref_global_id = 1;
  3716. return true;
  3717. }
  3718. static void
  3719. wasm_externref_map_destroy()
  3720. {
  3721. bh_hash_map_destroy(externref_map);
  3722. os_mutex_destroy(&externref_lock);
  3723. }
  3724. typedef struct LookupExtObj_UserData {
  3725. ExternRefMapNode node;
  3726. bool found;
  3727. uint32 externref_idx;
  3728. } LookupExtObj_UserData;
  3729. static void
  3730. lookup_extobj_callback(void *key, void *value, void *user_data)
  3731. {
  3732. uint32 externref_idx = (uint32)(uintptr_t)key;
  3733. ExternRefMapNode *node = (ExternRefMapNode *)value;
  3734. LookupExtObj_UserData *user_data_lookup =
  3735. (LookupExtObj_UserData *)user_data;
  3736. if (node->extern_obj == user_data_lookup->node.extern_obj
  3737. && node->module_inst == user_data_lookup->node.module_inst) {
  3738. user_data_lookup->found = true;
  3739. user_data_lookup->externref_idx = externref_idx;
  3740. }
  3741. }
  3742. bool
  3743. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
  3744. uint32 *p_externref_idx)
  3745. {
  3746. LookupExtObj_UserData lookup_user_data = { 0 };
  3747. ExternRefMapNode *node;
  3748. uint32 externref_idx;
  3749. /*
  3750. * to catch a parameter from `wasm_application_execute_func`,
  3751. * which represents a string 'null'
  3752. */
  3753. #if UINTPTR_MAX == UINT32_MAX
  3754. if ((uint32)-1 == (uintptr_t)extern_obj) {
  3755. #else
  3756. if ((uint64)-1LL == (uintptr_t)extern_obj) {
  3757. #endif
  3758. *p_externref_idx = NULL_REF;
  3759. return true;
  3760. }
  3761. /* in a wrapper, extern_obj could be any value */
  3762. lookup_user_data.node.extern_obj = extern_obj;
  3763. lookup_user_data.node.module_inst = module_inst;
  3764. lookup_user_data.found = false;
  3765. os_mutex_lock(&externref_lock);
  3766. /* Lookup hashmap firstly */
  3767. bh_hash_map_traverse(externref_map, lookup_extobj_callback,
  3768. (void *)&lookup_user_data);
  3769. if (lookup_user_data.found) {
  3770. *p_externref_idx = lookup_user_data.externref_idx;
  3771. os_mutex_unlock(&externref_lock);
  3772. return true;
  3773. }
  3774. /* Not found in hashmap */
  3775. if (externref_global_id == NULL_REF || externref_global_id == 0) {
  3776. goto fail1;
  3777. }
  3778. if (!(node = wasm_runtime_malloc(sizeof(ExternRefMapNode)))) {
  3779. goto fail1;
  3780. }
  3781. memset(node, 0, sizeof(ExternRefMapNode));
  3782. node->extern_obj = extern_obj;
  3783. node->module_inst = module_inst;
  3784. externref_idx = externref_global_id;
  3785. if (!bh_hash_map_insert(externref_map, (void *)(uintptr_t)externref_idx,
  3786. (void *)node)) {
  3787. goto fail2;
  3788. }
  3789. externref_global_id++;
  3790. *p_externref_idx = externref_idx;
  3791. os_mutex_unlock(&externref_lock);
  3792. return true;
  3793. fail2:
  3794. wasm_runtime_free(node);
  3795. fail1:
  3796. os_mutex_unlock(&externref_lock);
  3797. return false;
  3798. }
  3799. bool
  3800. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj)
  3801. {
  3802. ExternRefMapNode *node;
  3803. /* catch a `ref.null` vairable */
  3804. if (externref_idx == NULL_REF) {
  3805. *p_extern_obj = NULL;
  3806. return true;
  3807. }
  3808. os_mutex_lock(&externref_lock);
  3809. node = bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  3810. os_mutex_unlock(&externref_lock);
  3811. if (!node)
  3812. return false;
  3813. *p_extern_obj = node->extern_obj;
  3814. return true;
  3815. }
  3816. static void
  3817. reclaim_extobj_callback(void *key, void *value, void *user_data)
  3818. {
  3819. ExternRefMapNode *node = (ExternRefMapNode *)value;
  3820. WASMModuleInstanceCommon *module_inst =
  3821. (WASMModuleInstanceCommon *)user_data;
  3822. if (node->module_inst == module_inst) {
  3823. if (!node->marked && !node->retained) {
  3824. bh_hash_map_remove(externref_map, key, NULL, NULL);
  3825. wasm_runtime_free(value);
  3826. }
  3827. else {
  3828. node->marked = false;
  3829. }
  3830. }
  3831. }
  3832. static void
  3833. mark_externref(uint32 externref_idx)
  3834. {
  3835. ExternRefMapNode *node;
  3836. if (externref_idx != NULL_REF) {
  3837. node =
  3838. bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  3839. if (node) {
  3840. node->marked = true;
  3841. }
  3842. }
  3843. }
  3844. #if WASM_ENABLE_INTERP != 0
  3845. static void
  3846. interp_mark_all_externrefs(WASMModuleInstance *module_inst)
  3847. {
  3848. uint32 i, j, externref_idx, *table_data;
  3849. uint8 *global_data = module_inst->global_data;
  3850. WASMGlobalInstance *global;
  3851. WASMTableInstance *table;
  3852. global = module_inst->globals;
  3853. for (i = 0; i < module_inst->global_count; i++, global++) {
  3854. if (global->type == VALUE_TYPE_EXTERNREF) {
  3855. externref_idx = *(uint32 *)(global_data + global->data_offset);
  3856. mark_externref(externref_idx);
  3857. }
  3858. }
  3859. for (i = 0; i < module_inst->table_count; i++) {
  3860. table = wasm_get_table_inst(module_inst, i);
  3861. if (table->elem_type == VALUE_TYPE_EXTERNREF) {
  3862. table_data = (uint32 *)table->base_addr;
  3863. for (j = 0; j < table->cur_size; j++) {
  3864. externref_idx = table_data[j];
  3865. mark_externref(externref_idx);
  3866. }
  3867. }
  3868. }
  3869. }
  3870. #endif
  3871. #if WASM_ENABLE_AOT != 0
  3872. static void
  3873. aot_mark_all_externrefs(AOTModuleInstance *module_inst)
  3874. {
  3875. uint32 i = 0, j = 0;
  3876. const AOTModule *module = (AOTModule *)(module_inst->aot_module.ptr);
  3877. const AOTTable *table = module->tables;
  3878. const AOTGlobal *global = module->globals;
  3879. const AOTTableInstance *table_inst =
  3880. (AOTTableInstance *)module_inst->tables.ptr;
  3881. for (i = 0; i < module->global_count; i++, global++) {
  3882. if (global->type == VALUE_TYPE_EXTERNREF) {
  3883. mark_externref(*(uint32 *)((uint8 *)module_inst->global_data.ptr
  3884. + global->data_offset));
  3885. }
  3886. }
  3887. for (i = 0; i < module->table_count;
  3888. i++, table_inst = aot_next_tbl_inst(table_inst)) {
  3889. if ((table + i)->elem_type == VALUE_TYPE_EXTERNREF) {
  3890. while (j < table_inst->cur_size) {
  3891. mark_externref(table_inst->data[j++]);
  3892. }
  3893. }
  3894. }
  3895. }
  3896. #endif
  3897. void
  3898. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst)
  3899. {
  3900. os_mutex_lock(&externref_lock);
  3901. #if WASM_ENABLE_INTERP != 0
  3902. if (module_inst->module_type == Wasm_Module_Bytecode)
  3903. interp_mark_all_externrefs((WASMModuleInstance *)module_inst);
  3904. #endif
  3905. #if WASM_ENABLE_AOT != 0
  3906. if (module_inst->module_type == Wasm_Module_AoT)
  3907. aot_mark_all_externrefs((AOTModuleInstance *)module_inst);
  3908. #endif
  3909. bh_hash_map_traverse(externref_map, reclaim_extobj_callback,
  3910. (void *)module_inst);
  3911. os_mutex_unlock(&externref_lock);
  3912. }
  3913. static void
  3914. cleanup_extobj_callback(void *key, void *value, void *user_data)
  3915. {
  3916. ExternRefMapNode *node = (ExternRefMapNode *)value;
  3917. WASMModuleInstanceCommon *module_inst =
  3918. (WASMModuleInstanceCommon *)user_data;
  3919. if (node->module_inst == module_inst) {
  3920. bh_hash_map_remove(externref_map, key, NULL, NULL);
  3921. wasm_runtime_free(value);
  3922. }
  3923. }
  3924. void
  3925. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst)
  3926. {
  3927. os_mutex_lock(&externref_lock);
  3928. bh_hash_map_traverse(externref_map, cleanup_extobj_callback,
  3929. (void *)module_inst);
  3930. os_mutex_unlock(&externref_lock);
  3931. }
  3932. bool
  3933. wasm_externref_retain(uint32 externref_idx)
  3934. {
  3935. ExternRefMapNode *node;
  3936. os_mutex_lock(&externref_lock);
  3937. if (externref_idx != NULL_REF) {
  3938. node =
  3939. bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  3940. if (node) {
  3941. node->retained = true;
  3942. os_mutex_unlock(&externref_lock);
  3943. return true;
  3944. }
  3945. }
  3946. os_mutex_unlock(&externref_lock);
  3947. return false;
  3948. }
  3949. #endif /* end of WASM_ENABLE_REF_TYPES */
  3950. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  3951. void
  3952. wasm_runtime_dump_call_stack(WASMExecEnv *exec_env)
  3953. {
  3954. WASMModuleInstanceCommon *module_inst =
  3955. wasm_exec_env_get_module_inst(exec_env);
  3956. #if WASM_ENABLE_INTERP != 0
  3957. if (module_inst->module_type == Wasm_Module_Bytecode) {
  3958. wasm_interp_dump_call_stack(exec_env);
  3959. }
  3960. #endif
  3961. #if WASM_ENABLE_AOT != 0
  3962. if (module_inst->module_type == Wasm_Module_AoT) {
  3963. aot_dump_call_stack(exec_env);
  3964. }
  3965. #endif
  3966. }
  3967. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
  3968. bool
  3969. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  3970. const WASMExport *export, WASMType **out)
  3971. {
  3972. #if WASM_ENABLE_INTERP != 0
  3973. if (module_comm->module_type == Wasm_Module_Bytecode) {
  3974. WASMModule *module = (WASMModule *)module_comm;
  3975. if (export->index < module->import_function_count) {
  3976. *out = module->import_functions[export->index].u.function.func_type;
  3977. }
  3978. else {
  3979. *out =
  3980. module->functions[export->index - module->import_function_count]
  3981. ->func_type;
  3982. }
  3983. return true;
  3984. }
  3985. #endif
  3986. #if WASM_ENABLE_AOT != 0
  3987. if (module_comm->module_type == Wasm_Module_AoT) {
  3988. AOTModule *module = (AOTModule *)module_comm;
  3989. if (export->index < module->import_func_count) {
  3990. *out = module->func_types[module->import_funcs[export->index]
  3991. .func_type_index];
  3992. }
  3993. else {
  3994. *out = module->func_types
  3995. [module->func_type_indexes[export->index
  3996. - module->import_func_count]];
  3997. }
  3998. return true;
  3999. }
  4000. #endif
  4001. return false;
  4002. }
  4003. bool
  4004. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  4005. const WASMExport *export,
  4006. uint8 *out_val_type, bool *out_mutability)
  4007. {
  4008. #if WASM_ENABLE_INTERP != 0
  4009. if (module_comm->module_type == Wasm_Module_Bytecode) {
  4010. WASMModule *module = (WASMModule *)module_comm;
  4011. if (export->index < module->import_global_count) {
  4012. WASMGlobalImport *import_global =
  4013. &((module->import_globals + export->index)->u.global);
  4014. *out_val_type = import_global->type;
  4015. *out_mutability = import_global->is_mutable;
  4016. }
  4017. else {
  4018. WASMGlobal *global =
  4019. module->globals + (export->index - module->import_global_count);
  4020. *out_val_type = global->type;
  4021. *out_mutability = global->is_mutable;
  4022. }
  4023. return true;
  4024. }
  4025. #endif
  4026. #if WASM_ENABLE_AOT != 0
  4027. if (module_comm->module_type == Wasm_Module_AoT) {
  4028. AOTModule *module = (AOTModule *)module_comm;
  4029. if (export->index < module->import_global_count) {
  4030. AOTImportGlobal *import_global =
  4031. module->import_globals + export->index;
  4032. *out_val_type = import_global->type;
  4033. *out_mutability = import_global->is_mutable;
  4034. }
  4035. else {
  4036. AOTGlobal *global =
  4037. module->globals + (export->index - module->import_global_count);
  4038. *out_val_type = global->type;
  4039. *out_mutability = global->is_mutable;
  4040. }
  4041. return true;
  4042. }
  4043. #endif
  4044. return false;
  4045. }
  4046. bool
  4047. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  4048. const WASMExport *export,
  4049. uint32 *out_min_page, uint32 *out_max_page)
  4050. {
  4051. #if WASM_ENABLE_INTERP != 0
  4052. if (module_comm->module_type == Wasm_Module_Bytecode) {
  4053. WASMModule *module = (WASMModule *)module_comm;
  4054. if (export->index < module->import_memory_count) {
  4055. WASMMemoryImport *import_memory =
  4056. &((module->import_memories + export->index)->u.memory);
  4057. *out_min_page = import_memory->init_page_count;
  4058. *out_max_page = import_memory->max_page_count;
  4059. }
  4060. else {
  4061. WASMMemory *memory =
  4062. module->memories
  4063. + (export->index - module->import_memory_count);
  4064. *out_min_page = memory->init_page_count;
  4065. *out_max_page = memory->max_page_count;
  4066. }
  4067. return true;
  4068. }
  4069. #endif
  4070. #if WASM_ENABLE_AOT != 0
  4071. if (module_comm->module_type == Wasm_Module_AoT) {
  4072. AOTModule *module = (AOTModule *)module_comm;
  4073. if (export->index < module->import_memory_count) {
  4074. AOTImportMemory *import_memory =
  4075. module->import_memories + export->index;
  4076. *out_min_page = import_memory->mem_init_page_count;
  4077. *out_max_page = import_memory->mem_max_page_count;
  4078. }
  4079. else {
  4080. AOTMemory *memory = module->memories
  4081. + (export->index - module->import_memory_count);
  4082. *out_min_page = memory->mem_init_page_count;
  4083. *out_max_page = memory->mem_max_page_count;
  4084. }
  4085. return true;
  4086. }
  4087. #endif
  4088. return false;
  4089. }
  4090. bool
  4091. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  4092. const WASMExport *export,
  4093. uint8 *out_elem_type, uint32 *out_min_size,
  4094. uint32 *out_max_size)
  4095. {
  4096. #if WASM_ENABLE_INTERP != 0
  4097. if (module_comm->module_type == Wasm_Module_Bytecode) {
  4098. WASMModule *module = (WASMModule *)module_comm;
  4099. if (export->index < module->import_table_count) {
  4100. WASMTableImport *import_table =
  4101. &((module->import_tables + export->index)->u.table);
  4102. *out_elem_type = import_table->elem_type;
  4103. *out_min_size = import_table->init_size;
  4104. *out_max_size = import_table->max_size;
  4105. }
  4106. else {
  4107. WASMTable *table =
  4108. module->tables + (export->index - module->import_table_count);
  4109. *out_elem_type = table->elem_type;
  4110. *out_min_size = table->init_size;
  4111. *out_max_size = table->max_size;
  4112. }
  4113. return true;
  4114. }
  4115. #endif
  4116. #if WASM_ENABLE_AOT != 0
  4117. if (module_comm->module_type == Wasm_Module_AoT) {
  4118. AOTModule *module = (AOTModule *)module_comm;
  4119. if (export->index < module->import_table_count) {
  4120. AOTImportTable *import_table =
  4121. module->import_tables + export->index;
  4122. *out_elem_type = VALUE_TYPE_FUNCREF;
  4123. *out_min_size = import_table->table_init_size;
  4124. *out_max_size = import_table->table_max_size;
  4125. }
  4126. else {
  4127. AOTTable *table =
  4128. module->tables + (export->index - module->import_table_count);
  4129. *out_elem_type = table->elem_type;
  4130. *out_min_size = table->table_init_size;
  4131. *out_max_size = table->table_max_size;
  4132. }
  4133. return true;
  4134. }
  4135. #endif
  4136. return false;
  4137. }
  4138. static inline bool
  4139. argv_to_params(wasm_val_t *out_params, const uint32 *argv, WASMType *func_type)
  4140. {
  4141. wasm_val_t *param = out_params;
  4142. uint32 i = 0, *u32;
  4143. for (i = 0; i < func_type->param_count; i++, param++) {
  4144. switch (func_type->types[i]) {
  4145. case VALUE_TYPE_I32:
  4146. param->kind = WASM_I32;
  4147. param->of.i32 = *argv++;
  4148. break;
  4149. case VALUE_TYPE_I64:
  4150. param->kind = WASM_I64;
  4151. u32 = (uint32 *)&param->of.i64;
  4152. u32[0] = *argv++;
  4153. u32[1] = *argv++;
  4154. break;
  4155. case VALUE_TYPE_F32:
  4156. param->kind = WASM_F32;
  4157. param->of.f32 = *(float32 *)argv++;
  4158. break;
  4159. case VALUE_TYPE_F64:
  4160. param->kind = WASM_F64;
  4161. u32 = (uint32 *)&param->of.i64;
  4162. u32[0] = *argv++;
  4163. u32[1] = *argv++;
  4164. break;
  4165. #if WASM_ENABLE_REF_TYPES != 0
  4166. case VALUE_TYPE_EXTERNREF:
  4167. param->kind = WASM_ANYREF;
  4168. if (!wasm_externref_ref2obj(*argv,
  4169. (void **)&param->of.foreign)) {
  4170. return false;
  4171. }
  4172. argv++;
  4173. break;
  4174. #endif
  4175. default:
  4176. return false;
  4177. }
  4178. }
  4179. return true;
  4180. }
  4181. static inline bool
  4182. results_to_argv(WASMModuleInstanceCommon *module_inst, uint32 *out_argv,
  4183. const wasm_val_t *results, WASMType *func_type)
  4184. {
  4185. const wasm_val_t *result = results;
  4186. uint32 *argv = out_argv, *u32, i;
  4187. uint8 *result_types = func_type->types + func_type->param_count;
  4188. for (i = 0; i < func_type->result_count; i++, result++) {
  4189. switch (result_types[i]) {
  4190. case VALUE_TYPE_I32:
  4191. case VALUE_TYPE_F32:
  4192. *(int32 *)argv++ = result->of.i32;
  4193. break;
  4194. case VALUE_TYPE_I64:
  4195. case VALUE_TYPE_F64:
  4196. u32 = (uint32 *)&result->of.i64;
  4197. *argv++ = u32[0];
  4198. *argv++ = u32[1];
  4199. break;
  4200. #if WASM_ENABLE_REF_TYPES != 0
  4201. case VALUE_TYPE_EXTERNREF:
  4202. if (!wasm_externref_obj2ref(module_inst,
  4203. (void *)result->of.foreign, argv)) {
  4204. return false;
  4205. }
  4206. argv++;
  4207. break;
  4208. #endif
  4209. default:
  4210. return false;
  4211. }
  4212. }
  4213. return true;
  4214. }
  4215. bool
  4216. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  4217. void *func_ptr, WASMType *func_type,
  4218. uint32 argc, uint32 *argv, bool with_env,
  4219. void *wasm_c_api_env)
  4220. {
  4221. wasm_val_t params_buf[16] = { 0 }, results_buf[4] = { 0 };
  4222. wasm_val_t *params = params_buf, *results = results_buf;
  4223. wasm_trap_t *trap = NULL;
  4224. bool ret = false;
  4225. wasm_val_vec_t params_vec, results_vec;
  4226. if (func_type->param_count > 16) {
  4227. if (!(params =
  4228. runtime_malloc(sizeof(wasm_val_t) * func_type->param_count,
  4229. module_inst, NULL, 0))) {
  4230. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  4231. return false;
  4232. }
  4233. }
  4234. if (!argv_to_params(params, argv, func_type)) {
  4235. wasm_runtime_set_exception(module_inst, "unsupported param type");
  4236. goto fail;
  4237. }
  4238. if (func_type->result_count > 4) {
  4239. if (!(results =
  4240. runtime_malloc(sizeof(wasm_val_t) * func_type->result_count,
  4241. module_inst, NULL, 0))) {
  4242. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  4243. goto fail;
  4244. }
  4245. }
  4246. params_vec.data = params;
  4247. params_vec.num_elems = func_type->param_count;
  4248. params_vec.size = func_type->param_count;
  4249. params_vec.size_of_elem = sizeof(wasm_val_t);
  4250. results_vec.data = results;
  4251. results_vec.num_elems = 0;
  4252. results_vec.size = func_type->result_count;
  4253. results_vec.size_of_elem = sizeof(wasm_val_t);
  4254. if (!with_env) {
  4255. wasm_func_callback_t callback = (wasm_func_callback_t)func_ptr;
  4256. trap = callback(&params_vec, &results_vec);
  4257. }
  4258. else {
  4259. wasm_func_callback_with_env_t callback =
  4260. (wasm_func_callback_with_env_t)func_ptr;
  4261. trap = callback(wasm_c_api_env, &params_vec, &results_vec);
  4262. }
  4263. if (trap) {
  4264. if (trap->message->data) {
  4265. /* since trap->message->data does not end with '\0' */
  4266. char trap_message[128] = { 0 };
  4267. bh_memcpy_s(trap_message, 127, trap->message->data,
  4268. (trap->message->size < 127 ? (uint32)trap->message->size
  4269. : 127));
  4270. wasm_runtime_set_exception(module_inst, trap_message);
  4271. }
  4272. else {
  4273. wasm_runtime_set_exception(
  4274. module_inst, "native function throw unknown exception");
  4275. }
  4276. wasm_trap_delete(trap);
  4277. goto fail;
  4278. }
  4279. if (!results_to_argv(module_inst, argv, results, func_type)) {
  4280. wasm_runtime_set_exception(module_inst, "unsupported result type");
  4281. goto fail;
  4282. }
  4283. results_vec.num_elems = func_type->result_count;
  4284. ret = true;
  4285. fail:
  4286. if (params != params_buf)
  4287. wasm_runtime_free(params);
  4288. if (results != results_buf)
  4289. wasm_runtime_free(results);
  4290. return ret;
  4291. }
  4292. void
  4293. wasm_runtime_show_app_heap_corrupted_prompt()
  4294. {
  4295. LOG_ERROR("Error: app heap is corrupted, if the wasm file "
  4296. "is compiled by wasi-sdk-12.0 or higher version, "
  4297. "please add -Wl,--export=malloc -Wl,--export=free "
  4298. "to export malloc and free functions. If it is "
  4299. "compiled by asc, please add --exportRuntime to "
  4300. "export the runtime helpers.");
  4301. }