wasm_runtime_common.c 158 KB

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