wasm_runtime_common.c 175 KB

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