wasm_runtime_common.c 155 KB

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