wasm_runtime_common.c 157 KB

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