wasm_runtime_common.c 248 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881
  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 dependencies 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(void);
  82. #endif /* WASM_ENABLE_MULTI_MODULE */
  83. #define E_TYPE_XIP 4
  84. static uint8
  85. val_type_to_val_kind(uint8 value_type);
  86. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  87. /* Initialize externref hashmap */
  88. static bool
  89. wasm_externref_map_init(void);
  90. /* Destroy externref hashmap */
  91. static void
  92. wasm_externref_map_destroy(void);
  93. #endif /* end of WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 */
  94. static void
  95. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  96. {
  97. if (error_buf != NULL)
  98. snprintf(error_buf, error_buf_size, "%s", string);
  99. }
  100. static void *
  101. runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst,
  102. char *error_buf, uint32 error_buf_size)
  103. {
  104. void *mem;
  105. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  106. if (module_inst != NULL) {
  107. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  108. }
  109. else if (error_buf != NULL) {
  110. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  111. }
  112. return NULL;
  113. }
  114. memset(mem, 0, (uint32)size);
  115. return mem;
  116. }
  117. #if WASM_ENABLE_MULTI_MODULE != 0
  118. /* TODO: Let loader_malloc be a general API both for AOT and WASM. */
  119. #define loader_malloc(size, error_buf, error_buf_size) \
  120. runtime_malloc(size, NULL, error_buf, error_buf_size)
  121. static void
  122. set_error_buf_v(const WASMModuleCommon *module, char *error_buf,
  123. uint32 error_buf_size, const char *format, ...)
  124. {
  125. va_list args;
  126. char buf[128];
  127. if (error_buf != NULL) {
  128. va_start(args, format);
  129. vsnprintf(buf, sizeof(buf), format, args);
  130. va_end(args);
  131. if (module->module_type == Wasm_Module_AoT) {
  132. snprintf(error_buf, error_buf_size, "AOT module load failed: %s",
  133. buf);
  134. }
  135. else if (module->module_type == Wasm_Module_Bytecode) {
  136. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  137. buf);
  138. }
  139. }
  140. }
  141. #endif
  142. #if WASM_ENABLE_FAST_JIT != 0
  143. static JitCompOptions jit_options = { 0 };
  144. #endif
  145. #if WASM_ENABLE_JIT != 0
  146. /* opt_level: 3, size_level: 3, segue-flags: 0,
  147. quick_invoke_c_api_import: false */
  148. static LLVMJITOptions llvm_jit_options = { 3, 3, 0, false };
  149. #endif
  150. #if WASM_ENABLE_GC != 0
  151. static uint32 gc_heap_size_default = GC_HEAP_SIZE_DEFAULT;
  152. #endif
  153. static RunningMode runtime_running_mode = Mode_Default;
  154. #ifdef OS_ENABLE_HW_BOUND_CHECK
  155. /* The exec_env of thread local storage, set before calling function
  156. and used in signal handler, as we cannot get it from the argument
  157. of signal handler */
  158. static os_thread_local_attribute WASMExecEnv *exec_env_tls = NULL;
  159. static bool
  160. is_sig_addr_in_guard_pages(void *sig_addr, WASMModuleInstance *module_inst)
  161. {
  162. WASMMemoryInstance *memory_inst;
  163. #if WASM_ENABLE_SHARED_HEAP != 0
  164. WASMSharedHeap *shared_heap;
  165. #endif
  166. uint8 *mapped_mem_start_addr = NULL;
  167. uint8 *mapped_mem_end_addr = NULL;
  168. uint32 i;
  169. for (i = 0; i < module_inst->memory_count; ++i) {
  170. /* To be compatible with multi memory, get the ith memory instance */
  171. memory_inst = wasm_get_memory_with_idx(module_inst, i);
  172. mapped_mem_start_addr = memory_inst->memory_data;
  173. mapped_mem_end_addr = memory_inst->memory_data + 8 * (uint64)BH_GB;
  174. if (mapped_mem_start_addr <= (uint8 *)sig_addr
  175. && (uint8 *)sig_addr < mapped_mem_end_addr) {
  176. /* The address which causes segmentation fault is inside
  177. the memory instance's guard regions */
  178. return true;
  179. }
  180. }
  181. #if WASM_ENABLE_SHARED_HEAP != 0
  182. shared_heap =
  183. wasm_runtime_get_shared_heap((WASMModuleInstanceCommon *)module_inst);
  184. if (shared_heap) {
  185. mapped_mem_start_addr = shared_heap->base_addr;
  186. mapped_mem_end_addr = shared_heap->base_addr + 8 * (uint64)BH_GB;
  187. if (mapped_mem_start_addr <= (uint8 *)sig_addr
  188. && (uint8 *)sig_addr < mapped_mem_end_addr) {
  189. /* The address which causes segmentation fault is inside
  190. the shared heap's guard regions */
  191. return true;
  192. }
  193. }
  194. #endif
  195. return false;
  196. }
  197. #ifndef BH_PLATFORM_WINDOWS
  198. static void
  199. runtime_signal_handler(void *sig_addr)
  200. {
  201. WASMModuleInstance *module_inst;
  202. WASMJmpBuf *jmpbuf_node;
  203. uint32 page_size = os_getpagesize();
  204. #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  205. uint8 *stack_min_addr;
  206. uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
  207. #endif
  208. /* Check whether current thread is running wasm function */
  209. if (exec_env_tls && exec_env_tls->handle == os_self_thread()
  210. && (jmpbuf_node = exec_env_tls->jmpbuf_stack_top)) {
  211. /* Get mapped mem info of current instance */
  212. module_inst = (WASMModuleInstance *)exec_env_tls->module_inst;
  213. #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  214. /* Get stack info of current thread */
  215. stack_min_addr = os_thread_get_stack_boundary();
  216. #endif
  217. if (is_sig_addr_in_guard_pages(sig_addr, module_inst)) {
  218. wasm_set_exception(module_inst, "out of bounds memory access");
  219. os_longjmp(jmpbuf_node->jmpbuf, 1);
  220. }
  221. #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  222. else if (stack_min_addr <= (uint8 *)sig_addr
  223. && (uint8 *)sig_addr
  224. < stack_min_addr + page_size * guard_page_count) {
  225. /* The address which causes segmentation fault is inside
  226. native thread's guard page */
  227. wasm_set_exception(module_inst, "native stack overflow");
  228. os_longjmp(jmpbuf_node->jmpbuf, 1);
  229. }
  230. #endif
  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. os_longjmp(jmpbuf_node->jmpbuf, 1);
  236. }
  237. }
  238. }
  239. #else /* else of BH_PLATFORM_WINDOWS */
  240. #if WASM_ENABLE_AOT != 0
  241. #include <Zydis/Zydis.h>
  242. static uint32
  243. decode_insn(uint8 *insn)
  244. {
  245. uint8 *data = (uint8 *)insn;
  246. uint32 length = 32; /* reserve enough size */
  247. /* Initialize decoder context */
  248. ZydisDecoder decoder;
  249. ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64,
  250. ZYDIS_STACK_WIDTH_64);
  251. /* Initialize formatter */
  252. ZydisFormatter formatter;
  253. ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL);
  254. /* Loop over the instructions in our buffer */
  255. ZyanU64 runtime_address = (ZyanU64)(uintptr_t)data;
  256. ZyanUSize offset = 0;
  257. ZydisDecodedInstruction instruction;
  258. ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT_VISIBLE];
  259. char buffer[256];
  260. if (ZYAN_SUCCESS(ZydisDecoderDecodeFull(
  261. &decoder, data + offset, length - offset, &instruction, operands,
  262. ZYDIS_MAX_OPERAND_COUNT_VISIBLE,
  263. ZYDIS_DFLAG_VISIBLE_OPERANDS_ONLY))) {
  264. /* Format & print the binary instruction structure to
  265. human readable format */
  266. ZydisFormatterFormatInstruction(&formatter, &instruction, operands,
  267. instruction.operand_count_visible,
  268. buffer, sizeof(buffer),
  269. runtime_address);
  270. #if 0
  271. /* Print current instruction */
  272. os_printf("%012" PRIX64 " ", runtime_address);
  273. puts(buffer);
  274. #endif
  275. return instruction.length;
  276. }
  277. /* Decode failed */
  278. return 0;
  279. }
  280. #endif /* end of WASM_ENABLE_AOT != 0 */
  281. static LONG
  282. next_action(WASMModuleInstance *module_inst, EXCEPTION_POINTERS *exce_info)
  283. {
  284. #if WASM_ENABLE_AOT != 0
  285. uint32 insn_size;
  286. #endif
  287. if (module_inst->module_type == Wasm_Module_Bytecode
  288. && module_inst->e->running_mode == Mode_Interp) {
  289. /* Continue to search next exception handler for
  290. interpreter mode as it can be caught by
  291. `__try { .. } __except { .. }` sentences in
  292. wasm_runtime.c */
  293. return EXCEPTION_CONTINUE_SEARCH;
  294. }
  295. #if WASM_ENABLE_AOT != 0
  296. /* Skip current instruction and continue to run for AOT/JIT mode.
  297. TODO: implement unwind support for AOT/JIT code in Windows platform */
  298. insn_size = decode_insn((uint8 *)exce_info->ContextRecord->Rip);
  299. if (insn_size > 0) {
  300. exce_info->ContextRecord->Rip += insn_size;
  301. return EXCEPTION_CONTINUE_EXECUTION;
  302. }
  303. #endif
  304. /* return different value from EXCEPTION_CONTINUE_SEARCH (= 0)
  305. and EXCEPTION_CONTINUE_EXECUTION (= -1) */
  306. return -2;
  307. }
  308. static LONG
  309. runtime_exception_handler(EXCEPTION_POINTERS *exce_info)
  310. {
  311. PEXCEPTION_RECORD ExceptionRecord = exce_info->ExceptionRecord;
  312. uint8 *sig_addr = (uint8 *)ExceptionRecord->ExceptionInformation[1];
  313. WASMModuleInstance *module_inst;
  314. WASMJmpBuf *jmpbuf_node;
  315. uint8 *mapped_mem_start_addr = NULL;
  316. uint8 *mapped_mem_end_addr = NULL;
  317. uint32 page_size = os_getpagesize();
  318. LONG ret;
  319. if (exec_env_tls && exec_env_tls->handle == os_self_thread()
  320. && (jmpbuf_node = exec_env_tls->jmpbuf_stack_top)) {
  321. module_inst = (WASMModuleInstance *)exec_env_tls->module_inst;
  322. if (ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) {
  323. if (is_sig_addr_in_guard_pages(sig_addr, module_inst)) {
  324. /* The address which causes segmentation fault is inside
  325. the memory instance's guard regions.
  326. Set exception and let the wasm func continue to run, when
  327. the wasm func returns, the caller will check whether the
  328. exception is thrown and return to runtime. */
  329. wasm_set_exception(module_inst, "out of bounds memory access");
  330. ret = next_action(module_inst, exce_info);
  331. if (ret == EXCEPTION_CONTINUE_SEARCH
  332. || ret == EXCEPTION_CONTINUE_EXECUTION)
  333. return ret;
  334. }
  335. else if (exec_env_tls->exce_check_guard_page <= (uint8 *)sig_addr
  336. && (uint8 *)sig_addr
  337. < exec_env_tls->exce_check_guard_page + page_size) {
  338. bh_assert(wasm_copy_exception(module_inst, NULL));
  339. ret = next_action(module_inst, exce_info);
  340. if (ret == EXCEPTION_CONTINUE_SEARCH
  341. || ret == EXCEPTION_CONTINUE_EXECUTION)
  342. return ret;
  343. }
  344. }
  345. #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  346. else if (ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) {
  347. /* Set stack overflow exception and let the wasm func continue
  348. to run, when the wasm func returns, the caller will check
  349. whether the exception is thrown and return to runtime, and
  350. the damaged stack will be recovered by _resetstkoflw(). */
  351. wasm_set_exception(module_inst, "native stack overflow");
  352. ret = next_action(module_inst, exce_info);
  353. if (ret == EXCEPTION_CONTINUE_SEARCH
  354. || ret == EXCEPTION_CONTINUE_EXECUTION)
  355. return ret;
  356. }
  357. #endif
  358. else {
  359. LOG_WARNING("Unhandled exception thrown: exception code: 0x%lx, "
  360. "exception address: %p, exception information: %p\n",
  361. ExceptionRecord->ExceptionCode,
  362. ExceptionRecord->ExceptionAddress, sig_addr);
  363. }
  364. }
  365. return EXCEPTION_CONTINUE_SEARCH;
  366. }
  367. #endif /* end of BH_PLATFORM_WINDOWS */
  368. static bool
  369. runtime_signal_init()
  370. {
  371. #ifndef BH_PLATFORM_WINDOWS
  372. return os_thread_signal_init(runtime_signal_handler) == 0 ? true : false;
  373. #else
  374. if (os_thread_signal_init() != 0)
  375. return false;
  376. if (!AddVectoredExceptionHandler(1, runtime_exception_handler)) {
  377. os_thread_signal_destroy();
  378. return false;
  379. }
  380. #endif
  381. return true;
  382. }
  383. static void
  384. runtime_signal_destroy()
  385. {
  386. #ifdef BH_PLATFORM_WINDOWS
  387. RemoveVectoredExceptionHandler(runtime_exception_handler);
  388. #endif
  389. os_thread_signal_destroy();
  390. }
  391. void
  392. wasm_runtime_set_exec_env_tls(WASMExecEnv *exec_env)
  393. {
  394. exec_env_tls = exec_env;
  395. }
  396. WASMExecEnv *
  397. wasm_runtime_get_exec_env_tls()
  398. {
  399. return exec_env_tls;
  400. }
  401. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  402. static bool
  403. wasm_runtime_env_init(void)
  404. {
  405. if (bh_platform_init() != 0)
  406. return false;
  407. if (wasm_native_init() == false) {
  408. goto fail1;
  409. }
  410. #if WASM_ENABLE_MULTI_MODULE
  411. if (BHT_OK != os_mutex_init(&registered_module_list_lock)) {
  412. goto fail2;
  413. }
  414. if (BHT_OK != os_mutex_init(&loading_module_list_lock)) {
  415. goto fail3;
  416. }
  417. #endif
  418. #if WASM_ENABLE_SHARED_MEMORY
  419. if (!wasm_shared_memory_init()) {
  420. goto fail4;
  421. }
  422. #endif
  423. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  424. if (!thread_manager_init()) {
  425. goto fail5;
  426. }
  427. #endif
  428. #ifdef OS_ENABLE_HW_BOUND_CHECK
  429. if (!runtime_signal_init()) {
  430. goto fail6;
  431. }
  432. #endif
  433. #if WASM_ENABLE_AOT != 0
  434. #if WASM_ENABLE_DEBUG_AOT != 0
  435. if (!jit_debug_engine_init()) {
  436. goto fail7;
  437. }
  438. #endif
  439. #endif
  440. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  441. if (!wasm_externref_map_init()) {
  442. goto fail8;
  443. }
  444. #endif
  445. #if WASM_ENABLE_FAST_JIT != 0
  446. if (!jit_compiler_init(&jit_options)) {
  447. goto fail9;
  448. }
  449. #endif
  450. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  451. if (!aot_compiler_init()) {
  452. goto fail10;
  453. }
  454. #endif
  455. #if WASM_ENABLE_THREAD_MGR != 0 && defined(OS_ENABLE_WAKEUP_BLOCKING_OP)
  456. if (os_blocking_op_init() != BHT_OK) {
  457. goto fail11;
  458. }
  459. os_end_blocking_op();
  460. #endif
  461. return true;
  462. #if WASM_ENABLE_THREAD_MGR != 0 && defined(OS_ENABLE_WAKEUP_BLOCKING_OP)
  463. fail11:
  464. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  465. aot_compiler_destroy();
  466. #endif
  467. #endif
  468. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  469. fail10:
  470. #if WASM_ENABLE_FAST_JIT != 0
  471. jit_compiler_destroy();
  472. #endif
  473. #endif
  474. #if WASM_ENABLE_FAST_JIT != 0
  475. fail9:
  476. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  477. wasm_externref_map_destroy();
  478. #endif
  479. #endif
  480. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  481. fail8:
  482. #endif
  483. #if WASM_ENABLE_AOT != 0
  484. #if WASM_ENABLE_DEBUG_AOT != 0
  485. jit_debug_engine_destroy();
  486. fail7:
  487. #endif
  488. #endif
  489. #ifdef OS_ENABLE_HW_BOUND_CHECK
  490. runtime_signal_destroy();
  491. fail6:
  492. #endif
  493. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  494. thread_manager_destroy();
  495. fail5:
  496. #endif
  497. #if WASM_ENABLE_SHARED_MEMORY
  498. wasm_shared_memory_destroy();
  499. fail4:
  500. #endif
  501. #if WASM_ENABLE_MULTI_MODULE
  502. os_mutex_destroy(&loading_module_list_lock);
  503. fail3:
  504. os_mutex_destroy(&registered_module_list_lock);
  505. fail2:
  506. #endif
  507. wasm_native_destroy();
  508. fail1:
  509. bh_platform_destroy();
  510. return false;
  511. }
  512. static bool
  513. wasm_runtime_exec_env_check(WASMExecEnv *exec_env)
  514. {
  515. return exec_env && exec_env->module_inst && exec_env->wasm_stack_size > 0
  516. && exec_env->wasm_stack.top_boundary
  517. == exec_env->wasm_stack.bottom + exec_env->wasm_stack_size
  518. && exec_env->wasm_stack.top <= exec_env->wasm_stack.top_boundary;
  519. }
  520. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  521. /**
  522. * lock for wasm_runtime_init/wasm_runtime_full_init and runtime_ref_count
  523. * Note: if the platform has mutex initializer, we use a global lock to
  524. * lock the operations of runtime init/full_init, otherwise when there are
  525. * operations happening simultaneously in multiple threads, developer
  526. * must create the lock by himself, and use it to lock the operations
  527. */
  528. static korp_mutex runtime_lock = OS_THREAD_MUTEX_INITIALIZER;
  529. #endif
  530. static int32 runtime_ref_count = 0;
  531. static bool
  532. wasm_runtime_init_internal(void)
  533. {
  534. if (!wasm_runtime_memory_init(Alloc_With_System_Allocator, NULL))
  535. return false;
  536. if (!wasm_runtime_env_init()) {
  537. wasm_runtime_memory_destroy();
  538. return false;
  539. }
  540. return true;
  541. }
  542. bool
  543. wasm_runtime_init()
  544. {
  545. bool ret = true;
  546. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  547. os_mutex_lock(&runtime_lock);
  548. #endif
  549. bh_assert(runtime_ref_count >= 0);
  550. if (runtime_ref_count == 0) {
  551. ret = wasm_runtime_init_internal();
  552. }
  553. if (ret) {
  554. runtime_ref_count++;
  555. }
  556. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  557. os_mutex_unlock(&runtime_lock);
  558. #endif
  559. return ret;
  560. }
  561. static void
  562. wasm_runtime_destroy_internal(void)
  563. {
  564. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  565. wasm_externref_map_destroy();
  566. #endif
  567. #if WASM_ENABLE_AOT != 0
  568. #if WASM_ENABLE_DEBUG_AOT != 0
  569. jit_debug_engine_destroy();
  570. #endif
  571. #endif
  572. #ifdef OS_ENABLE_HW_BOUND_CHECK
  573. runtime_signal_destroy();
  574. #endif
  575. /* runtime env destroy */
  576. #if WASM_ENABLE_MULTI_MODULE
  577. wasm_runtime_destroy_loading_module_list();
  578. os_mutex_destroy(&loading_module_list_lock);
  579. wasm_runtime_destroy_registered_module_list();
  580. os_mutex_destroy(&registered_module_list_lock);
  581. #endif
  582. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  583. /* Destroy LLVM-JIT compiler after destroying the modules
  584. * loaded by multi-module feature, since these modules may
  585. * create backend threads to compile the wasm functions,
  586. * which may access the LLVM resources. We wait until they
  587. * finish the compilation to avoid accessing the destroyed
  588. * resources in the compilation threads.
  589. */
  590. aot_compiler_destroy();
  591. #endif
  592. #if WASM_ENABLE_FAST_JIT != 0
  593. /* Destroy Fast-JIT compiler after destroying the modules
  594. * loaded by multi-module feature, since the Fast JIT's
  595. * code cache allocator may be used by these modules.
  596. */
  597. jit_compiler_destroy();
  598. #endif
  599. #if WASM_ENABLE_SHARED_MEMORY
  600. wasm_shared_memory_destroy();
  601. #endif
  602. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  603. #if WASM_ENABLE_DEBUG_INTERP != 0
  604. wasm_debug_engine_destroy();
  605. #endif
  606. thread_manager_destroy();
  607. #endif
  608. wasm_native_destroy();
  609. bh_platform_destroy();
  610. wasm_runtime_memory_destroy();
  611. }
  612. void
  613. wasm_runtime_destroy()
  614. {
  615. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  616. os_mutex_lock(&runtime_lock);
  617. #endif
  618. bh_assert(runtime_ref_count > 0);
  619. runtime_ref_count--;
  620. if (runtime_ref_count == 0) {
  621. wasm_runtime_destroy_internal();
  622. }
  623. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  624. os_mutex_unlock(&runtime_lock);
  625. #endif
  626. }
  627. RunningMode
  628. wasm_runtime_get_default_running_mode(void)
  629. {
  630. return runtime_running_mode;
  631. }
  632. #if WASM_ENABLE_JIT != 0
  633. LLVMJITOptions *
  634. wasm_runtime_get_llvm_jit_options(void)
  635. {
  636. return &llvm_jit_options;
  637. }
  638. #endif
  639. #if WASM_ENABLE_GC != 0
  640. uint32
  641. wasm_runtime_get_gc_heap_size_default(void)
  642. {
  643. return gc_heap_size_default;
  644. }
  645. #endif
  646. static bool
  647. wasm_runtime_full_init_internal(RuntimeInitArgs *init_args)
  648. {
  649. if (!wasm_runtime_memory_init(init_args->mem_alloc_type,
  650. &init_args->mem_alloc_option))
  651. return false;
  652. if (!wasm_runtime_set_default_running_mode(init_args->running_mode)) {
  653. wasm_runtime_memory_destroy();
  654. return false;
  655. }
  656. #if WASM_ENABLE_FAST_JIT != 0
  657. jit_options.code_cache_size = init_args->fast_jit_code_cache_size;
  658. #endif
  659. #if WASM_ENABLE_GC != 0
  660. uint32 gc_heap_size = init_args->gc_heap_size;
  661. if (gc_heap_size > 0) {
  662. gc_heap_size_default = gc_heap_size;
  663. }
  664. #endif
  665. #if WASM_ENABLE_JIT != 0
  666. llvm_jit_options.size_level = init_args->llvm_jit_size_level;
  667. llvm_jit_options.opt_level = init_args->llvm_jit_opt_level;
  668. llvm_jit_options.segue_flags = init_args->segue_flags;
  669. #endif
  670. #if WASM_ENABLE_LINUX_PERF != 0
  671. wasm_runtime_set_linux_perf(init_args->enable_linux_perf);
  672. #else
  673. if (init_args->enable_linux_perf)
  674. LOG_WARNING("warning: to enable linux perf support, please recompile "
  675. "with -DWAMR_BUILD_LINUX_PERF=1");
  676. #endif
  677. if (!wasm_runtime_env_init()) {
  678. wasm_runtime_memory_destroy();
  679. return false;
  680. }
  681. #if WASM_ENABLE_DEBUG_INTERP != 0
  682. if (strlen(init_args->ip_addr))
  683. if (!wasm_debug_engine_init(init_args->ip_addr,
  684. init_args->instance_port)) {
  685. wasm_runtime_destroy();
  686. return false;
  687. }
  688. #endif
  689. if (init_args->n_native_symbols > 0
  690. && !wasm_runtime_register_natives(init_args->native_module_name,
  691. init_args->native_symbols,
  692. init_args->n_native_symbols)) {
  693. wasm_runtime_destroy();
  694. return false;
  695. }
  696. #if WASM_ENABLE_THREAD_MGR != 0
  697. wasm_cluster_set_max_thread_num(init_args->max_thread_num);
  698. #endif
  699. return true;
  700. }
  701. bool
  702. wasm_runtime_full_init(RuntimeInitArgs *init_args)
  703. {
  704. bool ret = true;
  705. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  706. os_mutex_lock(&runtime_lock);
  707. #endif
  708. bh_assert(runtime_ref_count >= 0);
  709. if (runtime_ref_count == 0) {
  710. ret = wasm_runtime_full_init_internal(init_args);
  711. }
  712. if (ret) {
  713. runtime_ref_count++;
  714. }
  715. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  716. os_mutex_unlock(&runtime_lock);
  717. #endif
  718. return ret;
  719. }
  720. void
  721. wasm_runtime_set_log_level(log_level_t level)
  722. {
  723. bh_log_set_verbose_level(level);
  724. }
  725. bool
  726. wasm_runtime_is_running_mode_supported(RunningMode running_mode)
  727. {
  728. if (running_mode == Mode_Default) {
  729. return true;
  730. }
  731. else if (running_mode == Mode_Interp) {
  732. #if WASM_ENABLE_INTERP != 0
  733. return true;
  734. #endif
  735. }
  736. else if (running_mode == Mode_Fast_JIT) {
  737. #if WASM_ENABLE_FAST_JIT != 0
  738. return true;
  739. #endif
  740. }
  741. else if (running_mode == Mode_LLVM_JIT) {
  742. #if WASM_ENABLE_JIT != 0
  743. return true;
  744. #endif
  745. }
  746. else if (running_mode == Mode_Multi_Tier_JIT) {
  747. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  748. && WASM_ENABLE_LAZY_JIT != 0
  749. return true;
  750. #endif
  751. }
  752. return false;
  753. }
  754. bool
  755. wasm_runtime_set_default_running_mode(RunningMode running_mode)
  756. {
  757. if (wasm_runtime_is_running_mode_supported(running_mode)) {
  758. runtime_running_mode = running_mode;
  759. return true;
  760. }
  761. return false;
  762. }
  763. PackageType
  764. get_package_type(const uint8 *buf, uint32 size)
  765. {
  766. if (buf && size >= 4) {
  767. #if (WASM_ENABLE_WORD_ALIGN_READ != 0)
  768. uint32 buf32 = *(uint32 *)buf;
  769. buf = (const uint8 *)&buf32;
  770. #endif
  771. if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 's' && buf[3] == 'm')
  772. return Wasm_Module_Bytecode;
  773. if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 'o' && buf[3] == 't')
  774. return Wasm_Module_AoT;
  775. }
  776. return Package_Type_Unknown;
  777. }
  778. PackageType
  779. wasm_runtime_get_file_package_type(const uint8 *buf, uint32 size)
  780. {
  781. return get_package_type(buf, size);
  782. }
  783. PackageType
  784. wasm_runtime_get_module_package_type(WASMModuleCommon *const module)
  785. {
  786. if (!module) {
  787. return Package_Type_Unknown;
  788. }
  789. return module->module_type;
  790. }
  791. uint32
  792. wasm_runtime_get_file_package_version(const uint8 *buf, uint32 size)
  793. {
  794. if (buf && size >= 8) {
  795. uint32 version;
  796. #if (WASM_ENABLE_WORD_ALIGN_READ != 0)
  797. uint32 buf32 = *(uint32 *)(buf + sizeof(uint32));
  798. buf = (const uint8 *)&buf32;
  799. version = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
  800. #else
  801. version = buf[4] | buf[5] << 8 | buf[6] << 16 | buf[7] << 24;
  802. #endif
  803. return version;
  804. }
  805. return 0;
  806. }
  807. uint32
  808. wasm_runtime_get_module_package_version(WASMModuleCommon *const module)
  809. {
  810. if (!module) {
  811. return 0;
  812. }
  813. #if WASM_ENABLE_INTERP != 0
  814. if (module->module_type == Wasm_Module_Bytecode) {
  815. WASMModule *wasm_module = (WASMModule *)module;
  816. return wasm_module->package_version;
  817. }
  818. #endif
  819. #if WASM_ENABLE_AOT != 0
  820. if (module->module_type == Wasm_Module_AoT) {
  821. AOTModule *aot_module = (AOTModule *)module;
  822. return aot_module->package_version;
  823. }
  824. #endif
  825. return 0;
  826. }
  827. uint32
  828. wasm_runtime_get_current_package_version(package_type_t package_type)
  829. {
  830. switch (package_type) {
  831. case Wasm_Module_Bytecode:
  832. return WASM_CURRENT_VERSION;
  833. case Wasm_Module_AoT:
  834. return AOT_CURRENT_VERSION;
  835. case Package_Type_Unknown:
  836. default:
  837. return 0;
  838. }
  839. }
  840. #if WASM_ENABLE_AOT != 0
  841. static uint8 *
  842. align_ptr(const uint8 *p, uint32 b)
  843. {
  844. uintptr_t v = (uintptr_t)p;
  845. uintptr_t m = b - 1;
  846. return (uint8 *)((v + m) & ~m);
  847. }
  848. #define CHECK_BUF(buf, buf_end, length) \
  849. do { \
  850. if ((uintptr_t)buf + length < (uintptr_t)buf \
  851. || (uintptr_t)buf + length > (uintptr_t)buf_end) \
  852. return false; \
  853. } while (0)
  854. /* NOLINTNEXTLINE */
  855. #define read_uint16(p, p_end, res) \
  856. do { \
  857. p = (uint8 *)align_ptr(p, sizeof(uint16)); \
  858. CHECK_BUF(p, p_end, sizeof(uint16)); \
  859. res = *(uint16 *)p; \
  860. p += sizeof(uint16); \
  861. } while (0)
  862. /* NOLINTNEXTLINE */
  863. #define read_uint32(p, p_end, res) \
  864. do { \
  865. p = (uint8 *)align_ptr(p, sizeof(uint32)); \
  866. CHECK_BUF(p, p_end, sizeof(uint32)); \
  867. res = *(uint32 *)p; \
  868. p += sizeof(uint32); \
  869. } while (0)
  870. bool
  871. wasm_runtime_is_xip_file(const uint8 *buf, uint32 size)
  872. {
  873. const uint8 *p = buf, *p_end = buf + size;
  874. uint32 section_type, section_size;
  875. uint16 e_type;
  876. if (get_package_type(buf, size) != Wasm_Module_AoT)
  877. return false;
  878. CHECK_BUF(p, p_end, 8);
  879. p += 8;
  880. while (p < p_end) {
  881. read_uint32(p, p_end, section_type);
  882. read_uint32(p, p_end, section_size);
  883. CHECK_BUF(p, p_end, section_size);
  884. if (section_type == AOT_SECTION_TYPE_TARGET_INFO) {
  885. p += 4;
  886. read_uint16(p, p_end, e_type);
  887. return (e_type == E_TYPE_XIP) ? true : false;
  888. }
  889. else if (section_type >= AOT_SECTION_TYPE_SIGNATURE) {
  890. return false;
  891. }
  892. p += section_size;
  893. }
  894. return false;
  895. }
  896. #endif /* end of WASM_ENABLE_AOT */
  897. #if (WASM_ENABLE_THREAD_MGR != 0) && (WASM_ENABLE_DEBUG_INTERP != 0)
  898. uint32
  899. wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env, int32_t port)
  900. {
  901. WASMModuleInstanceCommon *module_inst =
  902. wasm_runtime_get_module_inst(exec_env);
  903. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  904. bh_assert(module_inst);
  905. bh_assert(cluster);
  906. if (module_inst->module_type != Wasm_Module_Bytecode) {
  907. LOG_WARNING("Attempt to create a debug instance for an AOT module");
  908. return 0;
  909. }
  910. if (cluster->debug_inst) {
  911. LOG_WARNING("Cluster already bind to a debug instance");
  912. return cluster->debug_inst->control_thread->port;
  913. }
  914. if (wasm_debug_instance_create(cluster, port)) {
  915. return cluster->debug_inst->control_thread->port;
  916. }
  917. return 0;
  918. }
  919. uint32
  920. wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
  921. {
  922. return wasm_runtime_start_debug_instance_with_port(exec_env, -1);
  923. }
  924. #endif
  925. #if WASM_ENABLE_MULTI_MODULE != 0
  926. static module_reader reader;
  927. static module_destroyer destroyer;
  928. void
  929. wasm_runtime_set_module_reader(const module_reader reader_cb,
  930. const module_destroyer destroyer_cb)
  931. {
  932. reader = reader_cb;
  933. destroyer = destroyer_cb;
  934. }
  935. module_reader
  936. wasm_runtime_get_module_reader()
  937. {
  938. return reader;
  939. }
  940. module_destroyer
  941. wasm_runtime_get_module_destroyer()
  942. {
  943. return destroyer;
  944. }
  945. static WASMRegisteredModule *
  946. wasm_runtime_find_module_registered_by_reference(WASMModuleCommon *module)
  947. {
  948. WASMRegisteredModule *reg_module = NULL;
  949. os_mutex_lock(&registered_module_list_lock);
  950. reg_module = bh_list_first_elem(registered_module_list);
  951. while (reg_module && module != reg_module->module) {
  952. reg_module = bh_list_elem_next(reg_module);
  953. }
  954. os_mutex_unlock(&registered_module_list_lock);
  955. return reg_module;
  956. }
  957. bool
  958. wasm_runtime_register_module_internal(const char *module_name,
  959. WASMModuleCommon *module,
  960. uint8 *orig_file_buf,
  961. uint32 orig_file_buf_size,
  962. char *error_buf, uint32 error_buf_size)
  963. {
  964. WASMRegisteredModule *node = NULL;
  965. node = wasm_runtime_find_module_registered_by_reference(module);
  966. if (node) { /* module has been registered */
  967. if (node->module_name) { /* module has name */
  968. if (!module_name || strcmp(node->module_name, module_name)) {
  969. /* module has different name */
  970. LOG_DEBUG("module(%p) has been registered with name %s", module,
  971. node->module_name);
  972. set_error_buf(error_buf, error_buf_size,
  973. "Register module failed: "
  974. "failed to rename the module");
  975. return false;
  976. }
  977. else {
  978. /* module has the same name */
  979. LOG_DEBUG(
  980. "module(%p) has been registered with the same name %s",
  981. module, node->module_name);
  982. return true;
  983. }
  984. }
  985. else {
  986. /* module has empty name, reset it */
  987. node->module_name = module_name;
  988. return true;
  989. }
  990. }
  991. /* module hasn't been registered */
  992. node = runtime_malloc(sizeof(WASMRegisteredModule), NULL, NULL, 0);
  993. if (!node) {
  994. LOG_DEBUG("malloc WASMRegisteredModule failed. SZ=%zu",
  995. sizeof(WASMRegisteredModule));
  996. return false;
  997. }
  998. /* share the string and the module */
  999. node->module_name = module_name;
  1000. node->module = module;
  1001. node->orig_file_buf = orig_file_buf;
  1002. node->orig_file_buf_size = orig_file_buf_size;
  1003. os_mutex_lock(&registered_module_list_lock);
  1004. bh_list_status ret = bh_list_insert(registered_module_list, node);
  1005. bh_assert(BH_LIST_SUCCESS == ret);
  1006. (void)ret;
  1007. os_mutex_unlock(&registered_module_list_lock);
  1008. return true;
  1009. }
  1010. bool
  1011. wasm_runtime_register_module(const char *module_name, WASMModuleCommon *module,
  1012. char *error_buf, uint32 error_buf_size)
  1013. {
  1014. if (!error_buf || !error_buf_size) {
  1015. LOG_ERROR("error buffer is required");
  1016. return false;
  1017. }
  1018. if (!module_name || !module) {
  1019. LOG_DEBUG("module_name and module are required");
  1020. set_error_buf(error_buf, error_buf_size,
  1021. "Register module failed: "
  1022. "module_name and module are required");
  1023. return false;
  1024. }
  1025. if (wasm_runtime_is_built_in_module(module_name)) {
  1026. LOG_DEBUG("%s is a built-in module name", module_name);
  1027. set_error_buf(error_buf, error_buf_size,
  1028. "Register module failed: "
  1029. "can not register as a built-in module");
  1030. return false;
  1031. }
  1032. return wasm_runtime_register_module_internal(module_name, module, NULL, 0,
  1033. error_buf, error_buf_size);
  1034. }
  1035. void
  1036. wasm_runtime_unregister_module(const WASMModuleCommon *module)
  1037. {
  1038. WASMRegisteredModule *registered_module = NULL;
  1039. os_mutex_lock(&registered_module_list_lock);
  1040. registered_module = bh_list_first_elem(registered_module_list);
  1041. while (registered_module && module != registered_module->module) {
  1042. registered_module = bh_list_elem_next(registered_module);
  1043. }
  1044. /* it does not matter if it is not exist. after all, it is gone */
  1045. if (registered_module) {
  1046. bh_list_remove(registered_module_list, registered_module);
  1047. wasm_runtime_free(registered_module);
  1048. }
  1049. os_mutex_unlock(&registered_module_list_lock);
  1050. }
  1051. WASMModuleCommon *
  1052. wasm_runtime_find_module_registered(const char *module_name)
  1053. {
  1054. WASMRegisteredModule *module = NULL, *module_next;
  1055. os_mutex_lock(&registered_module_list_lock);
  1056. module = bh_list_first_elem(registered_module_list);
  1057. while (module) {
  1058. module_next = bh_list_elem_next(module);
  1059. if (module->module_name && !strcmp(module_name, module->module_name)) {
  1060. break;
  1061. }
  1062. module = module_next;
  1063. }
  1064. os_mutex_unlock(&registered_module_list_lock);
  1065. return module ? module->module : NULL;
  1066. }
  1067. /*
  1068. * simply destroy all
  1069. */
  1070. static void
  1071. wasm_runtime_destroy_registered_module_list()
  1072. {
  1073. WASMRegisteredModule *reg_module = NULL;
  1074. os_mutex_lock(&registered_module_list_lock);
  1075. reg_module = bh_list_first_elem(registered_module_list);
  1076. while (reg_module) {
  1077. WASMRegisteredModule *next_reg_module = bh_list_elem_next(reg_module);
  1078. bh_list_remove(registered_module_list, reg_module);
  1079. /* now, it is time to release every module in the runtime */
  1080. if (reg_module->module->module_type == Wasm_Module_Bytecode) {
  1081. #if WASM_ENABLE_INTERP != 0
  1082. wasm_unload((WASMModule *)reg_module->module);
  1083. #endif
  1084. }
  1085. else {
  1086. #if WASM_ENABLE_AOT != 0
  1087. aot_unload((AOTModule *)reg_module->module);
  1088. #endif
  1089. }
  1090. /* destroy the file buffer */
  1091. if (destroyer && reg_module->orig_file_buf) {
  1092. destroyer(reg_module->orig_file_buf,
  1093. reg_module->orig_file_buf_size);
  1094. reg_module->orig_file_buf = NULL;
  1095. reg_module->orig_file_buf_size = 0;
  1096. }
  1097. wasm_runtime_free(reg_module);
  1098. reg_module = next_reg_module;
  1099. }
  1100. os_mutex_unlock(&registered_module_list_lock);
  1101. }
  1102. bool
  1103. wasm_runtime_add_loading_module(const char *module_name, char *error_buf,
  1104. uint32 error_buf_size)
  1105. {
  1106. LOG_DEBUG("add %s into a loading list", module_name);
  1107. LoadingModule *loadingModule =
  1108. runtime_malloc(sizeof(LoadingModule), NULL, error_buf, error_buf_size);
  1109. if (!loadingModule) {
  1110. return false;
  1111. }
  1112. /* share the incoming string */
  1113. loadingModule->module_name = module_name;
  1114. os_mutex_lock(&loading_module_list_lock);
  1115. bh_list_status ret = bh_list_insert(loading_module_list, loadingModule);
  1116. bh_assert(BH_LIST_SUCCESS == ret);
  1117. (void)ret;
  1118. os_mutex_unlock(&loading_module_list_lock);
  1119. return true;
  1120. }
  1121. void
  1122. wasm_runtime_delete_loading_module(const char *module_name)
  1123. {
  1124. LOG_DEBUG("delete %s from a loading list", module_name);
  1125. LoadingModule *module = NULL;
  1126. os_mutex_lock(&loading_module_list_lock);
  1127. module = bh_list_first_elem(loading_module_list);
  1128. while (module && strcmp(module->module_name, module_name)) {
  1129. module = bh_list_elem_next(module);
  1130. }
  1131. /* it does not matter if it is not exist. after all, it is gone */
  1132. if (module) {
  1133. bh_list_remove(loading_module_list, module);
  1134. wasm_runtime_free(module);
  1135. }
  1136. os_mutex_unlock(&loading_module_list_lock);
  1137. }
  1138. bool
  1139. wasm_runtime_is_loading_module(const char *module_name)
  1140. {
  1141. LOG_DEBUG("find %s in a loading list", module_name);
  1142. LoadingModule *module = NULL;
  1143. os_mutex_lock(&loading_module_list_lock);
  1144. module = bh_list_first_elem(loading_module_list);
  1145. while (module && strcmp(module_name, module->module_name)) {
  1146. module = bh_list_elem_next(module);
  1147. }
  1148. os_mutex_unlock(&loading_module_list_lock);
  1149. return module != NULL;
  1150. }
  1151. void
  1152. wasm_runtime_destroy_loading_module_list()
  1153. {
  1154. LoadingModule *module = NULL;
  1155. os_mutex_lock(&loading_module_list_lock);
  1156. module = bh_list_first_elem(loading_module_list);
  1157. while (module) {
  1158. LoadingModule *next_module = bh_list_elem_next(module);
  1159. bh_list_remove(loading_module_list, module);
  1160. /*
  1161. * will not free the module_name since it is
  1162. * shared one of the const string pool
  1163. */
  1164. wasm_runtime_free(module);
  1165. module = next_module;
  1166. }
  1167. os_mutex_unlock(&loading_module_list_lock);
  1168. }
  1169. #endif /* WASM_ENABLE_MULTI_MODULE */
  1170. bool
  1171. wasm_runtime_is_built_in_module(const char *module_name)
  1172. {
  1173. return (!strcmp("env", module_name) || !strcmp("wasi_unstable", module_name)
  1174. || !strcmp("wasi_snapshot_preview1", module_name)
  1175. #if WASM_ENABLE_SPEC_TEST != 0
  1176. || !strcmp("spectest", module_name)
  1177. #endif
  1178. #if WASM_ENABLE_WASI_TEST != 0
  1179. || !strcmp("foo", module_name)
  1180. #endif
  1181. || !strcmp("", module_name));
  1182. }
  1183. #if WASM_ENABLE_THREAD_MGR != 0
  1184. bool
  1185. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint64 start_offset,
  1186. uint32 size)
  1187. {
  1188. WASMModuleInstanceCommon *module_inst =
  1189. wasm_exec_env_get_module_inst(exec_env);
  1190. #if WASM_ENABLE_INTERP != 0
  1191. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1192. return wasm_set_aux_stack(exec_env, start_offset, size);
  1193. }
  1194. #endif
  1195. #if WASM_ENABLE_AOT != 0
  1196. if (module_inst->module_type == Wasm_Module_AoT) {
  1197. return aot_set_aux_stack(exec_env, start_offset, size);
  1198. }
  1199. #endif
  1200. return false;
  1201. }
  1202. bool
  1203. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint64 *start_offset,
  1204. uint32 *size)
  1205. {
  1206. WASMModuleInstanceCommon *module_inst =
  1207. wasm_exec_env_get_module_inst(exec_env);
  1208. #if WASM_ENABLE_INTERP != 0
  1209. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1210. return wasm_get_aux_stack(exec_env, start_offset, size);
  1211. }
  1212. #endif
  1213. #if WASM_ENABLE_AOT != 0
  1214. if (module_inst->module_type == Wasm_Module_AoT) {
  1215. return aot_get_aux_stack(exec_env, start_offset, size);
  1216. }
  1217. #endif
  1218. return false;
  1219. }
  1220. void
  1221. wasm_runtime_set_max_thread_num(uint32 num)
  1222. {
  1223. wasm_cluster_set_max_thread_num(num);
  1224. }
  1225. #endif /* end of WASM_ENABLE_THREAD_MGR */
  1226. static WASMModuleCommon *
  1227. register_module_with_null_name(WASMModuleCommon *module_common, char *error_buf,
  1228. uint32 error_buf_size)
  1229. {
  1230. #if WASM_ENABLE_MULTI_MODULE != 0
  1231. if (module_common) {
  1232. if (!wasm_runtime_register_module_internal(NULL, module_common, NULL, 0,
  1233. error_buf, error_buf_size)) {
  1234. wasm_runtime_unload(module_common);
  1235. return NULL;
  1236. }
  1237. return module_common;
  1238. }
  1239. else
  1240. return NULL;
  1241. #else
  1242. return module_common;
  1243. #endif
  1244. }
  1245. WASMModuleCommon *
  1246. wasm_runtime_load_ex(uint8 *buf, uint32 size, const LoadArgs *args,
  1247. char *error_buf, uint32 error_buf_size)
  1248. {
  1249. WASMModuleCommon *module_common = NULL;
  1250. uint32 package_type;
  1251. bool magic_header_detected = false;
  1252. if (!args) {
  1253. set_error_buf(error_buf, error_buf_size,
  1254. "WASM module load failed: null load arguments");
  1255. return NULL;
  1256. }
  1257. if (size < 4) {
  1258. set_error_buf(error_buf, error_buf_size,
  1259. "WASM module load failed: unexpected end");
  1260. return NULL;
  1261. }
  1262. package_type = get_package_type(buf, size);
  1263. if (package_type == Wasm_Module_Bytecode) {
  1264. #if WASM_ENABLE_INTERP != 0
  1265. magic_header_detected = true;
  1266. #endif
  1267. }
  1268. else if (package_type == Wasm_Module_AoT) {
  1269. #if WASM_ENABLE_AOT != 0
  1270. magic_header_detected = true;
  1271. #endif
  1272. }
  1273. if (!magic_header_detected) {
  1274. set_error_buf(error_buf, error_buf_size,
  1275. "WASM module load failed: magic header not detected");
  1276. return NULL;
  1277. }
  1278. if (package_type == Wasm_Module_Bytecode) {
  1279. #if WASM_ENABLE_INTERP != 0
  1280. module_common =
  1281. (WASMModuleCommon *)wasm_load(buf, size,
  1282. #if WASM_ENABLE_MULTI_MODULE != 0
  1283. true,
  1284. #endif
  1285. args, error_buf, error_buf_size);
  1286. if (module_common)
  1287. ((WASMModule *)module_common)->is_binary_freeable =
  1288. args->wasm_binary_freeable;
  1289. #endif
  1290. }
  1291. else if (package_type == Wasm_Module_AoT) {
  1292. #if WASM_ENABLE_AOT != 0
  1293. module_common = (WASMModuleCommon *)aot_load_from_aot_file(
  1294. buf, size, args, error_buf, error_buf_size);
  1295. if (module_common)
  1296. ((AOTModule *)module_common)->is_binary_freeable =
  1297. args->wasm_binary_freeable;
  1298. #endif
  1299. }
  1300. if (!module_common) {
  1301. LOG_DEBUG("WASM module load failed");
  1302. return NULL;
  1303. }
  1304. /*TODO: use file name as name and register with name? */
  1305. return register_module_with_null_name(module_common, error_buf,
  1306. error_buf_size);
  1307. }
  1308. bool
  1309. wasm_runtime_resolve_symbols(WASMModuleCommon *module)
  1310. {
  1311. #if WASM_ENABLE_INTERP != 0
  1312. if (module->module_type == Wasm_Module_Bytecode) {
  1313. return wasm_resolve_symbols((WASMModule *)module);
  1314. }
  1315. #endif
  1316. #if WASM_ENABLE_AOT != 0
  1317. if (module->module_type == Wasm_Module_AoT) {
  1318. return aot_resolve_symbols((AOTModule *)module);
  1319. }
  1320. #endif
  1321. return false;
  1322. }
  1323. WASMModuleCommon *
  1324. wasm_runtime_load(uint8 *buf, uint32 size, char *error_buf,
  1325. uint32 error_buf_size)
  1326. {
  1327. LoadArgs args = { 0 };
  1328. args.name = "";
  1329. args.wasm_binary_freeable = false;
  1330. return wasm_runtime_load_ex(buf, size, &args, error_buf, error_buf_size);
  1331. }
  1332. WASMModuleCommon *
  1333. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  1334. char *error_buf, uint32 error_buf_size)
  1335. {
  1336. WASMModuleCommon *module_common;
  1337. if (!is_aot) {
  1338. #if WASM_ENABLE_INTERP != 0
  1339. module_common = (WASMModuleCommon *)wasm_load_from_sections(
  1340. section_list, error_buf, error_buf_size);
  1341. if (!module_common) {
  1342. LOG_DEBUG("WASM module load failed from sections");
  1343. return NULL;
  1344. }
  1345. ((WASMModule *)module_common)->is_binary_freeable = true;
  1346. return register_module_with_null_name(module_common, error_buf,
  1347. error_buf_size);
  1348. #endif
  1349. }
  1350. else {
  1351. #if WASM_ENABLE_AOT != 0
  1352. module_common = (WASMModuleCommon *)aot_load_from_sections(
  1353. section_list, error_buf, error_buf_size);
  1354. if (!module_common) {
  1355. LOG_DEBUG("WASM module load failed from sections");
  1356. return NULL;
  1357. }
  1358. ((AOTModule *)module_common)->is_binary_freeable = true;
  1359. return register_module_with_null_name(module_common, error_buf,
  1360. error_buf_size);
  1361. #endif
  1362. }
  1363. #if WASM_ENABLE_INTERP == 0 || WASM_ENABLE_AOT == 0
  1364. set_error_buf(error_buf, error_buf_size,
  1365. "WASM module load failed: invalid section list type");
  1366. return NULL;
  1367. #endif
  1368. }
  1369. void
  1370. wasm_runtime_unload(WASMModuleCommon *module)
  1371. {
  1372. #if WASM_ENABLE_MULTI_MODULE != 0
  1373. /**
  1374. * since we will unload and free all module when runtime_destroy()
  1375. * we don't want users to unwillingly disrupt it
  1376. */
  1377. return;
  1378. #endif
  1379. #if WASM_ENABLE_INTERP != 0
  1380. if (module->module_type == Wasm_Module_Bytecode) {
  1381. wasm_unload((WASMModule *)module);
  1382. return;
  1383. }
  1384. #endif
  1385. #if WASM_ENABLE_AOT != 0
  1386. if (module->module_type == Wasm_Module_AoT) {
  1387. aot_unload((AOTModule *)module);
  1388. return;
  1389. }
  1390. #endif
  1391. }
  1392. uint32
  1393. wasm_runtime_get_max_mem(uint32 max_memory_pages, uint32 module_init_page_count,
  1394. uint32 module_max_page_count)
  1395. {
  1396. if (max_memory_pages == 0) {
  1397. /* Max memory not overwritten by runtime, use value from wasm module */
  1398. return module_max_page_count;
  1399. }
  1400. if (max_memory_pages < module_init_page_count) {
  1401. LOG_WARNING("Cannot override max memory with value lower than module "
  1402. "initial memory");
  1403. return module_init_page_count;
  1404. }
  1405. if (max_memory_pages > module_max_page_count) {
  1406. LOG_WARNING("Cannot override max memory with value greater than module "
  1407. "max memory");
  1408. return module_max_page_count;
  1409. }
  1410. return max_memory_pages;
  1411. }
  1412. WASMModuleInstanceCommon *
  1413. wasm_runtime_instantiate_internal(WASMModuleCommon *module,
  1414. WASMModuleInstanceCommon *parent,
  1415. WASMExecEnv *exec_env_main, uint32 stack_size,
  1416. uint32 heap_size, uint32 max_memory_pages,
  1417. char *error_buf, uint32 error_buf_size)
  1418. {
  1419. #if WASM_ENABLE_INTERP != 0
  1420. if (module->module_type == Wasm_Module_Bytecode)
  1421. return (WASMModuleInstanceCommon *)wasm_instantiate(
  1422. (WASMModule *)module, (WASMModuleInstance *)parent, exec_env_main,
  1423. stack_size, heap_size, max_memory_pages, error_buf, error_buf_size);
  1424. #endif
  1425. #if WASM_ENABLE_AOT != 0
  1426. if (module->module_type == Wasm_Module_AoT)
  1427. return (WASMModuleInstanceCommon *)aot_instantiate(
  1428. (AOTModule *)module, (AOTModuleInstance *)parent, exec_env_main,
  1429. stack_size, heap_size, max_memory_pages, error_buf, error_buf_size);
  1430. #endif
  1431. set_error_buf(error_buf, error_buf_size,
  1432. "Instantiate module failed, invalid module type");
  1433. return NULL;
  1434. }
  1435. WASMModuleInstanceCommon *
  1436. wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size,
  1437. uint32 heap_size, char *error_buf,
  1438. uint32 error_buf_size)
  1439. {
  1440. return wasm_runtime_instantiate_internal(module, NULL, NULL, stack_size,
  1441. heap_size, 0, error_buf,
  1442. error_buf_size);
  1443. }
  1444. WASMModuleInstanceCommon *
  1445. wasm_runtime_instantiate_ex(WASMModuleCommon *module,
  1446. const InstantiationArgs *args, char *error_buf,
  1447. uint32 error_buf_size)
  1448. {
  1449. return wasm_runtime_instantiate_internal(
  1450. module, NULL, NULL, args->default_stack_size,
  1451. args->host_managed_heap_size, args->max_memory_pages, error_buf,
  1452. error_buf_size);
  1453. }
  1454. void
  1455. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  1456. bool is_sub_inst)
  1457. {
  1458. #if WASM_ENABLE_INTERP != 0
  1459. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1460. wasm_deinstantiate((WASMModuleInstance *)module_inst, is_sub_inst);
  1461. return;
  1462. }
  1463. #endif
  1464. #if WASM_ENABLE_AOT != 0
  1465. if (module_inst->module_type == Wasm_Module_AoT) {
  1466. aot_deinstantiate((AOTModuleInstance *)module_inst, is_sub_inst);
  1467. return;
  1468. }
  1469. #endif
  1470. }
  1471. bool
  1472. wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
  1473. RunningMode running_mode)
  1474. {
  1475. #if WASM_ENABLE_AOT != 0
  1476. if (module_inst->module_type == Wasm_Module_AoT)
  1477. return true;
  1478. #endif
  1479. #if WASM_ENABLE_INTERP != 0
  1480. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1481. WASMModuleInstance *module_inst_interp =
  1482. (WASMModuleInstance *)module_inst;
  1483. return wasm_set_running_mode(module_inst_interp, running_mode);
  1484. }
  1485. #endif
  1486. return false;
  1487. }
  1488. RunningMode
  1489. wasm_runtime_get_running_mode(wasm_module_inst_t module_inst)
  1490. {
  1491. #if WASM_ENABLE_INTERP != 0
  1492. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1493. WASMModuleInstance *module_inst_interp =
  1494. (WASMModuleInstance *)module_inst;
  1495. return module_inst_interp->e->running_mode;
  1496. }
  1497. #endif
  1498. return Mode_Default;
  1499. }
  1500. void
  1501. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst)
  1502. {
  1503. wasm_runtime_deinstantiate_internal(module_inst, false);
  1504. }
  1505. WASMModuleCommon *
  1506. wasm_runtime_get_module(WASMModuleInstanceCommon *module_inst)
  1507. {
  1508. return (WASMModuleCommon *)((WASMModuleInstance *)module_inst)->module;
  1509. }
  1510. WASMExecEnv *
  1511. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  1512. uint32 stack_size)
  1513. {
  1514. return wasm_exec_env_create(module_inst, stack_size);
  1515. }
  1516. void
  1517. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env)
  1518. {
  1519. wasm_exec_env_destroy(exec_env);
  1520. }
  1521. #if WAMR_ENABLE_COPY_CALLSTACK != 0
  1522. uint32
  1523. wasm_copy_callstack(const wasm_exec_env_t exec_env, wasm_frame_t *buffer,
  1524. const uint32 length, const uint32 skip_n, char *error_buf,
  1525. uint32_t error_buf_size)
  1526. {
  1527. /*
  1528. * Note for devs: please refrain from such modifications inside of
  1529. * wasm_copy_callstack to preserve async-signal-safety
  1530. * - any allocations/freeing memory
  1531. * - dereferencing any pointers other than: exec_env, exec_env->module_inst,
  1532. * exec_env->module_inst->module, pointers between stack's bottom and
  1533. * top_boundary For more details check wasm_copy_callstack in
  1534. * wasm_export.h
  1535. */
  1536. #if WASM_ENABLE_DUMP_CALL_STACK
  1537. WASMModuleInstance *module_inst =
  1538. (WASMModuleInstance *)get_module_inst(exec_env);
  1539. #if WASM_ENABLE_INTERP != 0
  1540. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1541. return wasm_interp_copy_callstack(exec_env, buffer, length, skip_n,
  1542. error_buf, error_buf_size);
  1543. }
  1544. #endif
  1545. #if WASM_ENABLE_AOT != 0
  1546. if (module_inst->module_type == Wasm_Module_AoT) {
  1547. return aot_copy_callstack(exec_env, buffer, length, skip_n, error_buf,
  1548. error_buf_size);
  1549. }
  1550. #endif
  1551. #endif
  1552. char *err_msg = "No copy_callstack API was actually executed";
  1553. strncpy(error_buf, err_msg, error_buf_size);
  1554. return 0;
  1555. }
  1556. #endif // WAMR_ENABLE_COPY_CALLSTACK
  1557. bool
  1558. wasm_runtime_init_thread_env(void)
  1559. {
  1560. #ifdef BH_PLATFORM_WINDOWS
  1561. if (os_thread_env_init() != 0)
  1562. return false;
  1563. #endif
  1564. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1565. if (!runtime_signal_init()) {
  1566. #ifdef BH_PLATFORM_WINDOWS
  1567. os_thread_env_destroy();
  1568. #endif
  1569. return false;
  1570. }
  1571. #endif
  1572. #if WASM_ENABLE_THREAD_MGR != 0 && defined(OS_ENABLE_WAKEUP_BLOCKING_OP)
  1573. os_end_blocking_op();
  1574. #endif
  1575. return true;
  1576. }
  1577. void
  1578. wasm_runtime_destroy_thread_env(void)
  1579. {
  1580. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1581. runtime_signal_destroy();
  1582. #endif
  1583. #ifdef BH_PLATFORM_WINDOWS
  1584. os_thread_env_destroy();
  1585. #endif
  1586. }
  1587. bool
  1588. wasm_runtime_thread_env_inited(void)
  1589. {
  1590. #ifdef BH_PLATFORM_WINDOWS
  1591. if (!os_thread_env_inited())
  1592. return false;
  1593. #endif
  1594. #if WASM_ENABLE_AOT != 0
  1595. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1596. if (!os_thread_signal_inited())
  1597. return false;
  1598. #endif
  1599. #endif
  1600. return true;
  1601. }
  1602. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  1603. void
  1604. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module)
  1605. {
  1606. WASMModuleMemConsumption mem_conspn = { 0 };
  1607. #if WASM_ENABLE_INTERP != 0
  1608. if (module->module_type == Wasm_Module_Bytecode) {
  1609. wasm_get_module_mem_consumption((WASMModule *)module, &mem_conspn);
  1610. }
  1611. #endif
  1612. #if WASM_ENABLE_AOT != 0
  1613. if (module->module_type == Wasm_Module_AoT) {
  1614. aot_get_module_mem_consumption((AOTModule *)module, &mem_conspn);
  1615. }
  1616. #endif
  1617. os_printf("WASM module memory consumption, total size: %u\n",
  1618. mem_conspn.total_size);
  1619. os_printf(" module struct size: %u\n", mem_conspn.module_struct_size);
  1620. os_printf(" types size: %u\n", mem_conspn.types_size);
  1621. os_printf(" imports size: %u\n", mem_conspn.imports_size);
  1622. os_printf(" funcs size: %u\n", mem_conspn.functions_size);
  1623. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  1624. os_printf(" memories size: %u\n", mem_conspn.memories_size);
  1625. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  1626. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  1627. os_printf(" table segs size: %u\n", mem_conspn.table_segs_size);
  1628. os_printf(" data segs size: %u\n", mem_conspn.data_segs_size);
  1629. os_printf(" const strings size: %u\n", mem_conspn.const_strs_size);
  1630. #if WASM_ENABLE_AOT != 0
  1631. os_printf(" aot code size: %u\n", mem_conspn.aot_code_size);
  1632. #endif
  1633. }
  1634. void
  1635. wasm_runtime_dump_module_inst_mem_consumption(
  1636. const WASMModuleInstanceCommon *module_inst)
  1637. {
  1638. WASMModuleInstMemConsumption mem_conspn = { 0 };
  1639. #if WASM_ENABLE_INTERP != 0
  1640. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1641. wasm_get_module_inst_mem_consumption((WASMModuleInstance *)module_inst,
  1642. &mem_conspn);
  1643. }
  1644. #endif
  1645. #if WASM_ENABLE_AOT != 0
  1646. if (module_inst->module_type == Wasm_Module_AoT) {
  1647. aot_get_module_inst_mem_consumption((AOTModuleInstance *)module_inst,
  1648. &mem_conspn);
  1649. }
  1650. #endif
  1651. os_printf("WASM module inst memory consumption, total size: %lu\n",
  1652. mem_conspn.total_size);
  1653. os_printf(" module inst struct size: %u\n",
  1654. mem_conspn.module_inst_struct_size);
  1655. os_printf(" memories size: %lu\n", mem_conspn.memories_size);
  1656. os_printf(" app heap size: %u\n", mem_conspn.app_heap_size);
  1657. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  1658. os_printf(" functions size: %u\n", mem_conspn.functions_size);
  1659. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  1660. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  1661. }
  1662. void
  1663. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env)
  1664. {
  1665. uint32 total_size =
  1666. offsetof(WASMExecEnv, wasm_stack_u.bottom) + exec_env->wasm_stack_size;
  1667. os_printf("Exec env memory consumption, total size: %u\n", total_size);
  1668. os_printf(" exec env struct size: %u\n",
  1669. offsetof(WASMExecEnv, wasm_stack_u.bottom));
  1670. #if WASM_ENABLE_INTERP != 0 && WASM_ENABLE_FAST_INTERP == 0
  1671. os_printf(" block addr cache size: %u\n",
  1672. sizeof(exec_env->block_addr_cache));
  1673. #endif
  1674. os_printf(" stack size: %u\n", exec_env->wasm_stack_size);
  1675. }
  1676. uint32
  1677. gc_get_heap_highmark_size(void *heap);
  1678. void
  1679. wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env)
  1680. {
  1681. WASMModuleInstMemConsumption module_inst_mem_consps;
  1682. WASMModuleMemConsumption module_mem_consps;
  1683. WASMModuleInstanceCommon *module_inst_common;
  1684. WASMModuleCommon *module_common = NULL;
  1685. void *heap_handle = NULL;
  1686. uint32 app_heap_peak_size = 0;
  1687. uint32 max_aux_stack_used = -1;
  1688. uint64 total_size = 0;
  1689. module_inst_common = exec_env->module_inst;
  1690. #if WASM_ENABLE_INTERP != 0
  1691. if (module_inst_common->module_type == Wasm_Module_Bytecode) {
  1692. WASMModuleInstance *wasm_module_inst =
  1693. (WASMModuleInstance *)module_inst_common;
  1694. WASMModule *wasm_module = wasm_module_inst->module;
  1695. module_common = (WASMModuleCommon *)wasm_module;
  1696. if (wasm_module_inst->memories) {
  1697. heap_handle = wasm_module_inst->memories[0]->heap_handle;
  1698. }
  1699. wasm_get_module_inst_mem_consumption(wasm_module_inst,
  1700. &module_inst_mem_consps);
  1701. wasm_get_module_mem_consumption(wasm_module, &module_mem_consps);
  1702. if (wasm_module_inst->module->aux_stack_top_global_index != (uint32)-1)
  1703. max_aux_stack_used = wasm_module_inst->e->max_aux_stack_used;
  1704. }
  1705. #endif
  1706. #if WASM_ENABLE_AOT != 0
  1707. if (module_inst_common->module_type == Wasm_Module_AoT) {
  1708. AOTModuleInstance *aot_module_inst =
  1709. (AOTModuleInstance *)module_inst_common;
  1710. AOTModule *aot_module = (AOTModule *)aot_module_inst->module;
  1711. module_common = (WASMModuleCommon *)aot_module;
  1712. if (aot_module_inst->memories) {
  1713. AOTMemoryInstance **memories = aot_module_inst->memories;
  1714. heap_handle = memories[0]->heap_handle;
  1715. }
  1716. aot_get_module_inst_mem_consumption(aot_module_inst,
  1717. &module_inst_mem_consps);
  1718. aot_get_module_mem_consumption(aot_module, &module_mem_consps);
  1719. }
  1720. #endif
  1721. bh_assert(module_common != NULL);
  1722. if (heap_handle) {
  1723. app_heap_peak_size = gc_get_heap_highmark_size(heap_handle);
  1724. }
  1725. total_size = offsetof(WASMExecEnv, wasm_stack_u.bottom)
  1726. + exec_env->wasm_stack_size + module_mem_consps.total_size
  1727. + module_inst_mem_consps.total_size;
  1728. os_printf("\nMemory consumption summary (bytes):\n");
  1729. wasm_runtime_dump_module_mem_consumption(module_common);
  1730. wasm_runtime_dump_module_inst_mem_consumption(module_inst_common);
  1731. wasm_runtime_dump_exec_env_mem_consumption(exec_env);
  1732. os_printf("\nTotal memory consumption of module, module inst and "
  1733. "exec env: %" PRIu64 "\n",
  1734. total_size);
  1735. os_printf("Total interpreter stack used: %u\n",
  1736. exec_env->max_wasm_stack_used);
  1737. if (max_aux_stack_used != (uint32)-1)
  1738. os_printf("Total auxiliary stack used: %u\n", max_aux_stack_used);
  1739. else
  1740. os_printf("Total aux stack used: no enough info to profile\n");
  1741. /*
  1742. * Report the native stack usage estimation.
  1743. *
  1744. * Unlike the aux stack above, we report the amount unused
  1745. * because we don't know the stack "bottom".
  1746. *
  1747. * Note that this is just about what the runtime itself observed.
  1748. * It doesn't cover host func implementations, signal handlers, etc.
  1749. */
  1750. if (exec_env->native_stack_top_min != (void *)UINTPTR_MAX)
  1751. os_printf("Native stack left: %zd\n",
  1752. exec_env->native_stack_top_min
  1753. - exec_env->native_stack_boundary);
  1754. else
  1755. os_printf("Native stack left: no enough info to profile\n");
  1756. os_printf("Total app heap used: %u\n", app_heap_peak_size);
  1757. }
  1758. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
  1759. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  1760. #if WASM_ENABLE_PERF_PROFILING != 0
  1761. void
  1762. wasm_runtime_dump_perf_profiling(WASMModuleInstanceCommon *module_inst)
  1763. {
  1764. #if WASM_ENABLE_INTERP != 0
  1765. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1766. wasm_dump_perf_profiling((WASMModuleInstance *)module_inst);
  1767. }
  1768. #endif
  1769. #if WASM_ENABLE_AOT != 0
  1770. if (module_inst->module_type == Wasm_Module_AoT) {
  1771. aot_dump_perf_profiling((AOTModuleInstance *)module_inst);
  1772. }
  1773. #endif
  1774. }
  1775. double
  1776. wasm_runtime_sum_wasm_exec_time(WASMModuleInstanceCommon *inst)
  1777. {
  1778. #if WASM_ENABLE_INTERP != 0
  1779. if (inst->module_type == Wasm_Module_Bytecode)
  1780. return wasm_summarize_wasm_execute_time((WASMModuleInstance *)inst);
  1781. #endif
  1782. #if WASM_ENABLE_AOT != 0
  1783. if (inst->module_type == Wasm_Module_AoT)
  1784. return aot_summarize_wasm_execute_time((AOTModuleInstance *)inst);
  1785. #endif
  1786. return 0.0;
  1787. }
  1788. double
  1789. wasm_runtime_get_wasm_func_exec_time(WASMModuleInstanceCommon *inst,
  1790. const char *func_name)
  1791. {
  1792. #if WASM_ENABLE_INTERP != 0
  1793. if (inst->module_type == Wasm_Module_Bytecode)
  1794. return wasm_get_wasm_func_exec_time((WASMModuleInstance *)inst,
  1795. func_name);
  1796. #endif
  1797. #if WASM_ENABLE_AOT != 0
  1798. if (inst->module_type == Wasm_Module_AoT)
  1799. return aot_get_wasm_func_exec_time((AOTModuleInstance *)inst,
  1800. func_name);
  1801. #endif
  1802. return 0.0;
  1803. }
  1804. #endif /* WASM_ENABLE_PERF_PROFILING != 0 */
  1805. WASMModuleInstanceCommon *
  1806. wasm_runtime_get_module_inst(WASMExecEnv *exec_env)
  1807. {
  1808. return wasm_exec_env_get_module_inst(exec_env);
  1809. }
  1810. void
  1811. wasm_runtime_set_module_inst(WASMExecEnv *exec_env,
  1812. WASMModuleInstanceCommon *const module_inst)
  1813. {
  1814. wasm_exec_env_set_module_inst(exec_env, module_inst);
  1815. }
  1816. bool
  1817. wasm_runtime_get_export_global_inst(WASMModuleInstanceCommon *const module_inst,
  1818. char const *name,
  1819. wasm_global_inst_t *global_inst)
  1820. {
  1821. #if WASM_ENABLE_INTERP != 0
  1822. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1823. const WASMModuleInstance *wasm_module_inst =
  1824. (const WASMModuleInstance *)module_inst;
  1825. const WASMModule *wasm_module = wasm_module_inst->module;
  1826. uint32 i;
  1827. for (i = 0; i < wasm_module->export_count; i++) {
  1828. const WASMExport *wasm_export = &wasm_module->exports[i];
  1829. if ((wasm_export->kind == WASM_IMPORT_EXPORT_KIND_GLOBAL)
  1830. && !strcmp(wasm_export->name, name)) {
  1831. const WASMModuleInstanceExtra *e =
  1832. (WASMModuleInstanceExtra *)wasm_module_inst->e;
  1833. const WASMGlobalInstance *global =
  1834. &e->globals[wasm_export->index];
  1835. global_inst->kind = val_type_to_val_kind(global->type);
  1836. global_inst->is_mutable = global->is_mutable;
  1837. #if WASM_ENABLE_MULTI_MODULE == 0
  1838. global_inst->global_data =
  1839. wasm_module_inst->global_data + global->data_offset;
  1840. #else
  1841. global_inst->global_data =
  1842. global->import_global_inst
  1843. ? global->import_module_inst->global_data
  1844. + global->import_global_inst->data_offset
  1845. : wasm_module_inst->global_data + global->data_offset;
  1846. #endif
  1847. return true;
  1848. }
  1849. }
  1850. }
  1851. #endif
  1852. #if WASM_ENABLE_AOT != 0
  1853. if (module_inst->module_type == Wasm_Module_AoT) {
  1854. const AOTModuleInstance *aot_module_inst =
  1855. (AOTModuleInstance *)module_inst;
  1856. const AOTModule *aot_module = (AOTModule *)aot_module_inst->module;
  1857. uint32 i;
  1858. for (i = 0; i < aot_module->export_count; i++) {
  1859. const AOTExport *aot_export = &aot_module->exports[i];
  1860. if ((aot_export->kind == WASM_IMPORT_EXPORT_KIND_GLOBAL)
  1861. && !strcmp(aot_export->name, name)) {
  1862. const AOTGlobal *global =
  1863. &aot_module->globals[aot_export->index];
  1864. global_inst->kind = val_type_to_val_kind(global->type.val_type);
  1865. global_inst->is_mutable = global->type.is_mutable;
  1866. global_inst->global_data =
  1867. aot_module_inst->global_data + global->data_offset;
  1868. return true;
  1869. }
  1870. }
  1871. }
  1872. #endif
  1873. return false;
  1874. }
  1875. bool
  1876. wasm_runtime_get_export_table_inst(WASMModuleInstanceCommon *const module_inst,
  1877. char const *name,
  1878. wasm_table_inst_t *table_inst)
  1879. {
  1880. #if WASM_ENABLE_INTERP != 0
  1881. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1882. const WASMModuleInstance *wasm_module_inst =
  1883. (const WASMModuleInstance *)module_inst;
  1884. const WASMModule *wasm_module = wasm_module_inst->module;
  1885. uint32 i;
  1886. for (i = 0; i < wasm_module->export_count; i++) {
  1887. const WASMExport *wasm_export = &wasm_module->exports[i];
  1888. if ((wasm_export->kind == WASM_IMPORT_EXPORT_KIND_TABLE)
  1889. && !strcmp(wasm_export->name, name)) {
  1890. const WASMTableInstance *wasm_table_inst =
  1891. wasm_module_inst->tables[wasm_export->index];
  1892. table_inst->elem_kind =
  1893. val_type_to_val_kind(wasm_table_inst->elem_type);
  1894. table_inst->cur_size = wasm_table_inst->cur_size;
  1895. table_inst->max_size = wasm_table_inst->max_size;
  1896. table_inst->elems = (void *)wasm_table_inst->elems;
  1897. return true;
  1898. }
  1899. }
  1900. }
  1901. #endif
  1902. #if WASM_ENABLE_AOT != 0
  1903. if (module_inst->module_type == Wasm_Module_AoT) {
  1904. const AOTModuleInstance *aot_module_inst =
  1905. (AOTModuleInstance *)module_inst;
  1906. const AOTModule *aot_module = (AOTModule *)aot_module_inst->module;
  1907. uint32 i;
  1908. for (i = 0; i < aot_module->export_count; i++) {
  1909. const AOTExport *aot_export = &aot_module->exports[i];
  1910. if ((aot_export->kind == WASM_IMPORT_EXPORT_KIND_TABLE)
  1911. && !strcmp(aot_export->name, name)) {
  1912. const AOTTableInstance *aot_table_inst =
  1913. aot_module_inst->tables[aot_export->index];
  1914. table_inst->elem_kind =
  1915. val_type_to_val_kind(aot_table_inst->elem_type);
  1916. table_inst->cur_size = aot_table_inst->cur_size;
  1917. table_inst->max_size = aot_table_inst->max_size;
  1918. table_inst->elems = (void *)aot_table_inst->elems;
  1919. return true;
  1920. }
  1921. }
  1922. }
  1923. #endif
  1924. return false;
  1925. }
  1926. WASMFunctionInstanceCommon *
  1927. wasm_table_get_func_inst(struct WASMModuleInstanceCommon *const module_inst,
  1928. const wasm_table_inst_t *table_inst, uint32_t idx)
  1929. {
  1930. if (!table_inst) {
  1931. bh_assert(0);
  1932. return NULL;
  1933. }
  1934. if (idx >= table_inst->cur_size) {
  1935. bh_assert(0);
  1936. return NULL;
  1937. }
  1938. #if WASM_ENABLE_INTERP != 0
  1939. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1940. const WASMModuleInstance *wasm_module_inst =
  1941. (const WASMModuleInstance *)module_inst;
  1942. table_elem_type_t tbl_elem_val =
  1943. ((table_elem_type_t *)table_inst->elems)[idx];
  1944. if (tbl_elem_val == NULL_REF) {
  1945. return NULL;
  1946. }
  1947. #if WASM_ENABLE_GC == 0
  1948. uint32 func_idx = (uint32)tbl_elem_val;
  1949. #else
  1950. uint32 func_idx =
  1951. wasm_func_obj_get_func_idx_bound((WASMFuncObjectRef)tbl_elem_val);
  1952. #endif
  1953. bh_assert(func_idx < wasm_module_inst->e->function_count);
  1954. return wasm_module_inst->e->functions + func_idx;
  1955. }
  1956. #endif
  1957. #if WASM_ENABLE_AOT != 0
  1958. if (module_inst->module_type == Wasm_Module_AoT) {
  1959. AOTModuleInstance *aot_module_inst = (AOTModuleInstance *)module_inst;
  1960. uint32 func_idx;
  1961. table_elem_type_t tbl_elem_val =
  1962. ((table_elem_type_t *)table_inst->elems)[idx];
  1963. if (tbl_elem_val == NULL_REF) {
  1964. return NULL;
  1965. }
  1966. #if WASM_ENABLE_GC == 0
  1967. func_idx = (uint32)tbl_elem_val;
  1968. #else
  1969. func_idx =
  1970. wasm_func_obj_get_func_idx_bound((WASMFuncObjectRef)tbl_elem_val);
  1971. #endif
  1972. return aot_get_function_instance(aot_module_inst, func_idx);
  1973. }
  1974. #endif
  1975. return NULL;
  1976. }
  1977. void *
  1978. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env)
  1979. {
  1980. return exec_env->attachment;
  1981. }
  1982. void
  1983. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data)
  1984. {
  1985. exec_env->user_data = user_data;
  1986. }
  1987. void *
  1988. wasm_runtime_get_user_data(WASMExecEnv *exec_env)
  1989. {
  1990. return exec_env->user_data;
  1991. }
  1992. void
  1993. wasm_runtime_set_native_stack_boundary(WASMExecEnv *exec_env,
  1994. uint8 *native_stack_boundary)
  1995. {
  1996. exec_env->user_native_stack_boundary = native_stack_boundary;
  1997. }
  1998. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1999. void
  2000. wasm_runtime_access_exce_check_guard_page()
  2001. {
  2002. if (exec_env_tls && exec_env_tls->handle == os_self_thread()) {
  2003. uint32 page_size = os_getpagesize();
  2004. memset(exec_env_tls->exce_check_guard_page, 0, page_size);
  2005. }
  2006. }
  2007. #endif
  2008. #if WASM_ENABLE_INSTRUCTION_METERING != 0
  2009. void
  2010. wasm_runtime_set_instruction_count_limit(WASMExecEnv *exec_env,
  2011. int instructions_to_execute)
  2012. {
  2013. exec_env->instructions_to_execute = instructions_to_execute;
  2014. }
  2015. #endif
  2016. WASMFuncType *
  2017. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  2018. uint32 module_type)
  2019. {
  2020. WASMFuncType *type = NULL;
  2021. #if WASM_ENABLE_INTERP != 0
  2022. if (module_type == Wasm_Module_Bytecode) {
  2023. WASMFunctionInstance *wasm_func = (WASMFunctionInstance *)function;
  2024. type = wasm_func->is_import_func ? wasm_func->u.func_import->func_type
  2025. : wasm_func->u.func->func_type;
  2026. }
  2027. #endif
  2028. #if WASM_ENABLE_AOT != 0
  2029. if (module_type == Wasm_Module_AoT) {
  2030. AOTFunctionInstance *aot_func = (AOTFunctionInstance *)function;
  2031. type = aot_func->is_import_func ? aot_func->u.func_import->func_type
  2032. : aot_func->u.func.func_type;
  2033. }
  2034. #endif
  2035. return type;
  2036. }
  2037. WASMFunctionInstanceCommon *
  2038. wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
  2039. const char *name)
  2040. {
  2041. #if WASM_ENABLE_INTERP != 0
  2042. if (module_inst->module_type == Wasm_Module_Bytecode)
  2043. return (WASMFunctionInstanceCommon *)wasm_lookup_function(
  2044. (const WASMModuleInstance *)module_inst, name);
  2045. #endif
  2046. #if WASM_ENABLE_AOT != 0
  2047. if (module_inst->module_type == Wasm_Module_AoT)
  2048. return (WASMFunctionInstanceCommon *)aot_lookup_function(
  2049. (const AOTModuleInstance *)module_inst, name);
  2050. #endif
  2051. return NULL;
  2052. }
  2053. uint32
  2054. wasm_func_get_param_count(WASMFunctionInstanceCommon *const func_inst,
  2055. WASMModuleInstanceCommon *const module_inst)
  2056. {
  2057. WASMFuncType *type =
  2058. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  2059. bh_assert(type);
  2060. return type->param_count;
  2061. }
  2062. uint32
  2063. wasm_func_get_result_count(WASMFunctionInstanceCommon *const func_inst,
  2064. WASMModuleInstanceCommon *const module_inst)
  2065. {
  2066. WASMFuncType *type =
  2067. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  2068. bh_assert(type);
  2069. return type->result_count;
  2070. }
  2071. static uint8
  2072. val_type_to_val_kind(uint8 value_type)
  2073. {
  2074. switch (value_type) {
  2075. case VALUE_TYPE_I32:
  2076. return WASM_I32;
  2077. case VALUE_TYPE_I64:
  2078. return WASM_I64;
  2079. case VALUE_TYPE_F32:
  2080. return WASM_F32;
  2081. case VALUE_TYPE_F64:
  2082. return WASM_F64;
  2083. case VALUE_TYPE_V128:
  2084. return WASM_V128;
  2085. case VALUE_TYPE_FUNCREF:
  2086. return WASM_FUNCREF;
  2087. case VALUE_TYPE_EXTERNREF:
  2088. return WASM_EXTERNREF;
  2089. default:
  2090. bh_assert(0);
  2091. return 0;
  2092. }
  2093. }
  2094. void
  2095. wasm_func_get_param_types(WASMFunctionInstanceCommon *const func_inst,
  2096. WASMModuleInstanceCommon *const module_inst,
  2097. wasm_valkind_t *param_types)
  2098. {
  2099. WASMFuncType *type =
  2100. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  2101. uint32 i;
  2102. bh_assert(type);
  2103. for (i = 0; i < type->param_count; i++) {
  2104. param_types[i] = val_type_to_val_kind(type->types[i]);
  2105. }
  2106. }
  2107. void
  2108. wasm_func_get_result_types(WASMFunctionInstanceCommon *const func_inst,
  2109. WASMModuleInstanceCommon *const module_inst,
  2110. wasm_valkind_t *result_types)
  2111. {
  2112. WASMFuncType *type =
  2113. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  2114. uint32 i;
  2115. bh_assert(type);
  2116. for (i = 0; i < type->result_count; i++) {
  2117. result_types[i] =
  2118. val_type_to_val_kind(type->types[type->param_count + i]);
  2119. }
  2120. }
  2121. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2122. /* (uintptr_t)externref -> (uint32)index */
  2123. /* argv -> *ret_argv */
  2124. static bool
  2125. wasm_runtime_prepare_call_function(WASMExecEnv *exec_env,
  2126. WASMFunctionInstanceCommon *function,
  2127. uint32 *argv, uint32 argc, uint32 **ret_argv,
  2128. uint32 *ret_argc_param,
  2129. uint32 *ret_argc_result)
  2130. {
  2131. uint32 *new_argv = NULL, argv_i = 0, new_argv_i = 0, param_i = 0,
  2132. result_i = 0;
  2133. bool need_param_transform = false, need_result_transform = false;
  2134. uint64 size = 0;
  2135. WASMFuncType *func_type = wasm_runtime_get_function_type(
  2136. function, exec_env->module_inst->module_type);
  2137. bh_assert(func_type);
  2138. *ret_argc_param = func_type->param_cell_num;
  2139. *ret_argc_result = func_type->ret_cell_num;
  2140. for (param_i = 0; param_i < func_type->param_count; param_i++) {
  2141. if (VALUE_TYPE_EXTERNREF == func_type->types[param_i]) {
  2142. need_param_transform = true;
  2143. }
  2144. }
  2145. for (result_i = 0; result_i < func_type->result_count; result_i++) {
  2146. if (VALUE_TYPE_EXTERNREF
  2147. == func_type->types[func_type->param_count + result_i]) {
  2148. need_result_transform = true;
  2149. }
  2150. }
  2151. if (!need_param_transform && !need_result_transform) {
  2152. *ret_argv = argv;
  2153. return true;
  2154. }
  2155. if (func_type->param_cell_num >= func_type->ret_cell_num) {
  2156. size = sizeof(uint32) * func_type->param_cell_num;
  2157. }
  2158. else {
  2159. size = sizeof(uint32) * func_type->ret_cell_num;
  2160. }
  2161. if (!(new_argv = runtime_malloc(size, exec_env->module_inst, NULL, 0))) {
  2162. return false;
  2163. }
  2164. if (!need_param_transform) {
  2165. bh_memcpy_s(new_argv, (uint32)size, argv, (uint32)size);
  2166. }
  2167. else {
  2168. for (param_i = 0; param_i < func_type->param_count && argv_i < argc
  2169. && new_argv_i < func_type->param_cell_num;
  2170. param_i++) {
  2171. uint8 param_type = func_type->types[param_i];
  2172. if (VALUE_TYPE_EXTERNREF == param_type) {
  2173. void *externref_obj;
  2174. uint32 externref_index;
  2175. #if UINTPTR_MAX == UINT32_MAX
  2176. externref_obj = (void *)argv[argv_i];
  2177. #else
  2178. union {
  2179. uintptr_t val;
  2180. uint32 parts[2];
  2181. } u;
  2182. u.parts[0] = argv[argv_i];
  2183. u.parts[1] = argv[argv_i + 1];
  2184. externref_obj = (void *)u.val;
  2185. #endif
  2186. if (!wasm_externref_obj2ref(exec_env->module_inst,
  2187. externref_obj, &externref_index)) {
  2188. wasm_runtime_free(new_argv);
  2189. return false;
  2190. }
  2191. new_argv[new_argv_i] = externref_index;
  2192. argv_i += sizeof(uintptr_t) / sizeof(uint32);
  2193. new_argv_i++;
  2194. }
  2195. else {
  2196. uint16 param_cell_num = wasm_value_type_cell_num(param_type);
  2197. uint32 param_size = sizeof(uint32) * param_cell_num;
  2198. bh_memcpy_s(new_argv + new_argv_i, param_size, argv + argv_i,
  2199. param_size);
  2200. argv_i += param_cell_num;
  2201. new_argv_i += param_cell_num;
  2202. }
  2203. }
  2204. }
  2205. *ret_argv = new_argv;
  2206. return true;
  2207. }
  2208. /* (uintptr_t)externref <- (uint32)index */
  2209. /* argv <- new_argv */
  2210. static bool
  2211. wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
  2212. WASMFunctionInstanceCommon *function,
  2213. uint32 *argv, uint32 argc, uint32 *ret_argv)
  2214. {
  2215. uint32 argv_i = 0, result_i = 0, ret_argv_i = 0;
  2216. WASMFuncType *func_type;
  2217. bh_assert((argv && ret_argv) || (argc == 0));
  2218. if (argv == ret_argv) {
  2219. /* no need to transform externref results */
  2220. return true;
  2221. }
  2222. func_type = wasm_runtime_get_function_type(
  2223. function, exec_env->module_inst->module_type);
  2224. bh_assert(func_type);
  2225. for (result_i = 0; result_i < func_type->result_count && argv_i < argc;
  2226. result_i++) {
  2227. uint8 result_type = func_type->types[func_type->param_count + result_i];
  2228. if (result_type == VALUE_TYPE_EXTERNREF) {
  2229. void *externref_obj;
  2230. #if UINTPTR_MAX != UINT32_MAX
  2231. union {
  2232. uintptr_t val;
  2233. uint32 parts[2];
  2234. } u;
  2235. #endif
  2236. if (!wasm_externref_ref2obj(argv[argv_i], &externref_obj)) {
  2237. wasm_runtime_free(argv);
  2238. return false;
  2239. }
  2240. #if UINTPTR_MAX == UINT32_MAX
  2241. ret_argv[ret_argv_i] = (uintptr_t)externref_obj;
  2242. #else
  2243. u.val = (uintptr_t)externref_obj;
  2244. ret_argv[ret_argv_i] = u.parts[0];
  2245. ret_argv[ret_argv_i + 1] = u.parts[1];
  2246. #endif
  2247. argv_i += 1;
  2248. ret_argv_i += sizeof(uintptr_t) / sizeof(uint32);
  2249. }
  2250. else {
  2251. uint16 result_cell_num = wasm_value_type_cell_num(result_type);
  2252. uint32 result_size = sizeof(uint32) * result_cell_num;
  2253. bh_memcpy_s(ret_argv + ret_argv_i, result_size, argv + argv_i,
  2254. result_size);
  2255. argv_i += result_cell_num;
  2256. ret_argv_i += result_cell_num;
  2257. }
  2258. }
  2259. wasm_runtime_free(argv);
  2260. return true;
  2261. }
  2262. #endif
  2263. bool
  2264. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  2265. WASMFunctionInstanceCommon *function, uint32 argc,
  2266. uint32 argv[])
  2267. {
  2268. bool ret = false;
  2269. uint32 *new_argv = NULL, param_argc;
  2270. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2271. uint32 result_argc = 0;
  2272. #endif
  2273. if (!wasm_runtime_exec_env_check(exec_env)) {
  2274. LOG_ERROR("Invalid exec env stack info.");
  2275. return false;
  2276. }
  2277. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2278. if (!wasm_runtime_prepare_call_function(exec_env, function, argv, argc,
  2279. &new_argv, &param_argc,
  2280. &result_argc)) {
  2281. wasm_runtime_set_exception(exec_env->module_inst,
  2282. "the arguments conversion is failed");
  2283. return false;
  2284. }
  2285. #else
  2286. new_argv = argv;
  2287. param_argc = argc;
  2288. #endif
  2289. #if WASM_ENABLE_INTERP != 0
  2290. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  2291. ret = wasm_call_function(exec_env, (WASMFunctionInstance *)function,
  2292. param_argc, new_argv);
  2293. #endif
  2294. #if WASM_ENABLE_AOT != 0
  2295. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  2296. ret = aot_call_function(exec_env, (AOTFunctionInstance *)function,
  2297. param_argc, new_argv);
  2298. #endif
  2299. if (!ret) {
  2300. if (new_argv != argv) {
  2301. wasm_runtime_free(new_argv);
  2302. }
  2303. return false;
  2304. }
  2305. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2306. if (!wasm_runtime_finalize_call_function(exec_env, function, new_argv,
  2307. result_argc, argv)) {
  2308. wasm_runtime_set_exception(exec_env->module_inst,
  2309. "the result conversion is failed");
  2310. return false;
  2311. }
  2312. #endif
  2313. return ret;
  2314. }
  2315. static void
  2316. parse_args_to_uint32_array(WASMFuncType *type, wasm_val_t *args,
  2317. uint32 *out_argv)
  2318. {
  2319. uint32 i, p;
  2320. for (i = 0, p = 0; i < type->param_count; i++) {
  2321. switch (args[i].kind) {
  2322. case WASM_I32:
  2323. out_argv[p++] = args[i].of.i32;
  2324. break;
  2325. case WASM_I64:
  2326. {
  2327. union {
  2328. uint64 val;
  2329. uint32 parts[2];
  2330. } u;
  2331. u.val = args[i].of.i64;
  2332. out_argv[p++] = u.parts[0];
  2333. out_argv[p++] = u.parts[1];
  2334. break;
  2335. }
  2336. case WASM_F32:
  2337. {
  2338. union {
  2339. float32 val;
  2340. uint32 part;
  2341. } u;
  2342. u.val = args[i].of.f32;
  2343. out_argv[p++] = u.part;
  2344. break;
  2345. }
  2346. case WASM_F64:
  2347. {
  2348. union {
  2349. float64 val;
  2350. uint32 parts[2];
  2351. } u;
  2352. u.val = args[i].of.f64;
  2353. out_argv[p++] = u.parts[0];
  2354. out_argv[p++] = u.parts[1];
  2355. break;
  2356. }
  2357. case WASM_V128:
  2358. {
  2359. bh_assert(0);
  2360. break;
  2361. }
  2362. #if WASM_ENABLE_REF_TYPES != 0
  2363. #if WASM_ENABLE_GC == 0
  2364. case WASM_FUNCREF:
  2365. {
  2366. out_argv[p++] = args[i].of.i32;
  2367. break;
  2368. }
  2369. #else
  2370. case WASM_FUNCREF:
  2371. #endif
  2372. case WASM_EXTERNREF:
  2373. {
  2374. #if UINTPTR_MAX == UINT32_MAX
  2375. out_argv[p++] = args[i].of.foreign;
  2376. #else
  2377. union {
  2378. uintptr_t val;
  2379. uint32 parts[2];
  2380. } u;
  2381. u.val = (uintptr_t)args[i].of.foreign;
  2382. out_argv[p++] = u.parts[0];
  2383. out_argv[p++] = u.parts[1];
  2384. #endif
  2385. break;
  2386. }
  2387. #endif
  2388. default:
  2389. bh_assert(0);
  2390. break;
  2391. }
  2392. }
  2393. }
  2394. static void
  2395. parse_uint32_array_to_results(WASMFuncType *type, uint32 *argv,
  2396. wasm_val_t *out_results)
  2397. {
  2398. uint32 i, p;
  2399. for (i = 0, p = 0; i < type->result_count; i++) {
  2400. switch (type->types[type->param_count + i]) {
  2401. case VALUE_TYPE_I32:
  2402. out_results[i].kind = WASM_I32;
  2403. out_results[i].of.i32 = (int32)argv[p++];
  2404. break;
  2405. case VALUE_TYPE_I64:
  2406. {
  2407. union {
  2408. uint64 val;
  2409. uint32 parts[2];
  2410. } u;
  2411. u.parts[0] = argv[p++];
  2412. u.parts[1] = argv[p++];
  2413. out_results[i].kind = WASM_I64;
  2414. out_results[i].of.i64 = u.val;
  2415. break;
  2416. }
  2417. case VALUE_TYPE_F32:
  2418. {
  2419. union {
  2420. float32 val;
  2421. uint32 part;
  2422. } u;
  2423. u.part = argv[p++];
  2424. out_results[i].kind = WASM_F32;
  2425. out_results[i].of.f32 = u.val;
  2426. break;
  2427. }
  2428. case VALUE_TYPE_F64:
  2429. {
  2430. union {
  2431. float64 val;
  2432. uint32 parts[2];
  2433. } u;
  2434. u.parts[0] = argv[p++];
  2435. u.parts[1] = argv[p++];
  2436. out_results[i].kind = WASM_F64;
  2437. out_results[i].of.f64 = u.val;
  2438. break;
  2439. }
  2440. case VALUE_TYPE_V128:
  2441. {
  2442. bh_assert(0);
  2443. break;
  2444. }
  2445. #if WASM_ENABLE_REF_TYPES != 0
  2446. #if WASM_ENABLE_GC == 0
  2447. case VALUE_TYPE_FUNCREF:
  2448. {
  2449. out_results[i].kind = WASM_I32;
  2450. out_results[i].of.i32 = (int32)argv[p++];
  2451. break;
  2452. }
  2453. case VALUE_TYPE_EXTERNREF:
  2454. #else
  2455. case REF_TYPE_FUNCREF:
  2456. case REF_TYPE_EXTERNREF:
  2457. case REF_TYPE_ANYREF:
  2458. case REF_TYPE_EQREF:
  2459. case REF_TYPE_HT_NULLABLE:
  2460. case REF_TYPE_HT_NON_NULLABLE:
  2461. case REF_TYPE_I31REF:
  2462. case REF_TYPE_NULLFUNCREF:
  2463. case REF_TYPE_NULLEXTERNREF:
  2464. case REF_TYPE_STRUCTREF:
  2465. case REF_TYPE_ARRAYREF:
  2466. case REF_TYPE_NULLREF:
  2467. #endif /* end of WASM_ENABLE_GC == 0 */
  2468. {
  2469. #if UINTPTR_MAX == UINT32_MAX
  2470. out_results[i].kind = WASM_EXTERNREF;
  2471. out_results[i].of.foreign = (uintptr_t)argv[p++];
  2472. #else
  2473. union {
  2474. uintptr_t val;
  2475. uint32 parts[2];
  2476. } u;
  2477. u.parts[0] = argv[p++];
  2478. u.parts[1] = argv[p++];
  2479. out_results[i].kind = WASM_EXTERNREF;
  2480. out_results[i].of.foreign = u.val;
  2481. #endif
  2482. break;
  2483. }
  2484. #endif /* end of WASM_ENABLE_REF_TYPES != 0 */
  2485. default:
  2486. bh_assert(0);
  2487. break;
  2488. }
  2489. }
  2490. }
  2491. bool
  2492. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  2493. WASMFunctionInstanceCommon *function,
  2494. uint32 num_results, wasm_val_t results[],
  2495. uint32 num_args, wasm_val_t args[])
  2496. {
  2497. uint32 argc, argv_buf[16] = { 0 }, *argv = argv_buf, cell_num, module_type;
  2498. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2499. uint32 i, param_size_in_double_world = 0, result_size_in_double_world = 0;
  2500. #endif
  2501. uint64 total_size;
  2502. WASMFuncType *type;
  2503. bool ret = false;
  2504. module_type = exec_env->module_inst->module_type;
  2505. type = wasm_runtime_get_function_type(function, module_type);
  2506. if (!type) {
  2507. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT must be "
  2508. "enabled at least one.");
  2509. goto fail1;
  2510. }
  2511. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2512. for (i = 0; i < type->param_count; i++) {
  2513. param_size_in_double_world +=
  2514. wasm_value_type_cell_num_outside(type->types[i]);
  2515. }
  2516. for (i = 0; i < type->result_count; i++) {
  2517. result_size_in_double_world += wasm_value_type_cell_num_outside(
  2518. type->types[type->param_count + i]);
  2519. }
  2520. argc = param_size_in_double_world;
  2521. cell_num = (argc >= result_size_in_double_world)
  2522. ? argc
  2523. : result_size_in_double_world;
  2524. #else
  2525. argc = type->param_cell_num;
  2526. cell_num = (argc > type->ret_cell_num) ? argc : type->ret_cell_num;
  2527. #endif
  2528. if (num_results != type->result_count) {
  2529. LOG_ERROR(
  2530. "The result value number does not match the function declaration.");
  2531. goto fail1;
  2532. }
  2533. if (num_args != type->param_count) {
  2534. LOG_ERROR("The argument value number does not match the function "
  2535. "declaration.");
  2536. goto fail1;
  2537. }
  2538. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  2539. if (total_size > sizeof(argv_buf)) {
  2540. if (!(argv =
  2541. runtime_malloc(total_size, exec_env->module_inst, NULL, 0))) {
  2542. goto fail1;
  2543. }
  2544. }
  2545. parse_args_to_uint32_array(type, args, argv);
  2546. if (!(ret = wasm_runtime_call_wasm(exec_env, function, argc, argv)))
  2547. goto fail2;
  2548. parse_uint32_array_to_results(type, argv, results);
  2549. fail2:
  2550. if (argv != argv_buf)
  2551. wasm_runtime_free(argv);
  2552. fail1:
  2553. return ret;
  2554. }
  2555. bool
  2556. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  2557. WASMFunctionInstanceCommon *function,
  2558. uint32 num_results, wasm_val_t results[],
  2559. uint32 num_args, ...)
  2560. {
  2561. wasm_val_t args_buf[8] = { 0 }, *args = args_buf;
  2562. WASMFuncType *type = NULL;
  2563. bool ret = false;
  2564. uint64 total_size;
  2565. uint32 i = 0, module_type;
  2566. va_list vargs;
  2567. module_type = exec_env->module_inst->module_type;
  2568. type = wasm_runtime_get_function_type(function, module_type);
  2569. if (!type) {
  2570. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT "
  2571. "must be enabled at least one.");
  2572. goto fail1;
  2573. }
  2574. if (num_args != type->param_count) {
  2575. LOG_ERROR("The argument value number does not match the "
  2576. "function declaration.");
  2577. goto fail1;
  2578. }
  2579. total_size = sizeof(wasm_val_t) * (uint64)num_args;
  2580. if (total_size > sizeof(args_buf)) {
  2581. if (!(args =
  2582. runtime_malloc(total_size, exec_env->module_inst, NULL, 0))) {
  2583. goto fail1;
  2584. }
  2585. }
  2586. va_start(vargs, num_args);
  2587. for (i = 0; i < num_args; i++) {
  2588. switch (type->types[i]) {
  2589. case VALUE_TYPE_I32:
  2590. args[i].kind = WASM_I32;
  2591. args[i].of.i32 = va_arg(vargs, uint32);
  2592. break;
  2593. case VALUE_TYPE_I64:
  2594. args[i].kind = WASM_I64;
  2595. args[i].of.i64 = va_arg(vargs, uint64);
  2596. break;
  2597. case VALUE_TYPE_F32:
  2598. args[i].kind = WASM_F32;
  2599. args[i].of.f32 = (float32)va_arg(vargs, float64);
  2600. break;
  2601. case VALUE_TYPE_F64:
  2602. args[i].kind = WASM_F64;
  2603. args[i].of.f64 = va_arg(vargs, float64);
  2604. break;
  2605. case VALUE_TYPE_V128:
  2606. bh_assert(0);
  2607. break;
  2608. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2609. case VALUE_TYPE_FUNCREF:
  2610. {
  2611. args[i].kind = WASM_FUNCREF;
  2612. args[i].of.i32 = va_arg(vargs, uint32);
  2613. break;
  2614. }
  2615. case VALUE_TYPE_EXTERNREF:
  2616. {
  2617. args[i].kind = WASM_EXTERNREF;
  2618. args[i].of.foreign = va_arg(vargs, uintptr_t);
  2619. break;
  2620. }
  2621. #endif
  2622. default:
  2623. bh_assert(0);
  2624. break;
  2625. }
  2626. }
  2627. va_end(vargs);
  2628. ret = wasm_runtime_call_wasm_a(exec_env, function, num_results, results,
  2629. num_args, args);
  2630. if (args != args_buf)
  2631. wasm_runtime_free(args);
  2632. fail1:
  2633. return ret;
  2634. }
  2635. bool
  2636. wasm_runtime_create_exec_env_singleton(
  2637. WASMModuleInstanceCommon *module_inst_comm)
  2638. {
  2639. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2640. WASMExecEnv *exec_env = NULL;
  2641. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2642. || module_inst_comm->module_type == Wasm_Module_AoT);
  2643. if (module_inst->exec_env_singleton) {
  2644. return true;
  2645. }
  2646. exec_env = wasm_exec_env_create(module_inst_comm,
  2647. module_inst->default_wasm_stack_size);
  2648. if (exec_env)
  2649. module_inst->exec_env_singleton = exec_env;
  2650. return exec_env ? true : false;
  2651. }
  2652. WASMExecEnv *
  2653. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst_comm)
  2654. {
  2655. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2656. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2657. || module_inst_comm->module_type == Wasm_Module_AoT);
  2658. if (!module_inst->exec_env_singleton) {
  2659. wasm_runtime_create_exec_env_singleton(module_inst_comm);
  2660. }
  2661. return module_inst->exec_env_singleton;
  2662. }
  2663. static void
  2664. wasm_set_exception_local(WASMModuleInstance *module_inst, const char *exception)
  2665. {
  2666. exception_lock(module_inst);
  2667. if (exception) {
  2668. snprintf(module_inst->cur_exception, sizeof(module_inst->cur_exception),
  2669. "Exception: %s", exception);
  2670. }
  2671. else {
  2672. module_inst->cur_exception[0] = '\0';
  2673. }
  2674. exception_unlock(module_inst);
  2675. }
  2676. void
  2677. wasm_set_exception(WASMModuleInstance *module_inst, const char *exception)
  2678. {
  2679. #if WASM_ENABLE_THREAD_MGR != 0
  2680. WASMExecEnv *exec_env =
  2681. wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst);
  2682. if (exec_env) {
  2683. wasm_cluster_set_exception(exec_env, exception);
  2684. }
  2685. else {
  2686. wasm_set_exception_local(module_inst, exception);
  2687. }
  2688. #else
  2689. wasm_set_exception_local(module_inst, exception);
  2690. #endif
  2691. }
  2692. /* clang-format off */
  2693. static const char *exception_msgs[] = {
  2694. "unreachable", /* EXCE_UNREACHABLE */
  2695. "allocate memory failed", /* EXCE_OUT_OF_MEMORY */
  2696. "out of bounds memory access", /* EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS */
  2697. "integer overflow", /* EXCE_INTEGER_OVERFLOW */
  2698. "integer divide by zero", /* EXCE_INTEGER_DIVIDE_BY_ZERO */
  2699. "invalid conversion to integer", /* EXCE_INVALID_CONVERSION_TO_INTEGER */
  2700. "indirect call type mismatch", /* EXCE_INVALID_FUNCTION_TYPE_INDEX */
  2701. "invalid function index", /* EXCE_INVALID_FUNCTION_INDEX */
  2702. "undefined element", /* EXCE_UNDEFINED_ELEMENT */
  2703. "uninitialized element", /* EXCE_UNINITIALIZED_ELEMENT */
  2704. "failed to call unlinked import function", /* EXCE_CALL_UNLINKED_IMPORT_FUNC */
  2705. "native stack overflow", /* EXCE_NATIVE_STACK_OVERFLOW */
  2706. "unaligned atomic", /* EXCE_UNALIGNED_ATOMIC */
  2707. "wasm auxiliary stack overflow", /* EXCE_AUX_STACK_OVERFLOW */
  2708. "wasm auxiliary stack underflow", /* EXCE_AUX_STACK_UNDERFLOW */
  2709. "out of bounds table access", /* EXCE_OUT_OF_BOUNDS_TABLE_ACCESS */
  2710. "wasm operand stack overflow", /* EXCE_OPERAND_STACK_OVERFLOW */
  2711. "failed to compile fast jit function", /* EXCE_FAILED_TO_COMPILE_FAST_JIT_FUNC */
  2712. /* GC related exceptions */
  2713. "null function reference", /* EXCE_NULL_FUNC_OBJ */
  2714. "null structure reference", /* EXCE_NULL_STRUCT_OBJ */
  2715. "null array reference", /* EXCE_NULL_ARRAY_OBJ */
  2716. "null i31 reference", /* EXCE_NULL_I31_OBJ */
  2717. "null reference", /* EXCE_NULL_REFERENCE */
  2718. "create rtt type failed", /* EXCE_FAILED_TO_CREATE_RTT_TYPE */
  2719. "create struct object failed", /* EXCE_FAILED_TO_CREATE_STRUCT_OBJ */
  2720. "create array object failed", /* EXCE_FAILED_TO_CREATE_ARRAY_OBJ */
  2721. "create externref object failed", /* EXCE_FAILED_TO_CREATE_EXTERNREF_OBJ */
  2722. "cast failure", /* EXCE_CAST_FAILURE */
  2723. "out of bounds array access", /* EXCE_ARRAY_IDX_OOB */
  2724. /* stringref related exceptions */
  2725. "create string object failed", /* EXCE_FAILED_TO_CREATE_STRING */
  2726. "create stringref failed", /* EXCE_FAILED_TO_CREATE_STRINGREF */
  2727. "create stringview failed", /* EXCE_FAILED_TO_CREATE_STRINGVIEW */
  2728. "encode failed", /* EXCE_FAILED_TO_ENCODE_STRING */
  2729. "", /* EXCE_ALREADY_THROWN */
  2730. };
  2731. /* clang-format on */
  2732. void
  2733. wasm_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id)
  2734. {
  2735. if (id < EXCE_NUM)
  2736. wasm_set_exception(module_inst, exception_msgs[id]);
  2737. else
  2738. wasm_set_exception(module_inst, "unknown exception");
  2739. }
  2740. const char *
  2741. wasm_get_exception(WASMModuleInstance *module_inst)
  2742. {
  2743. if (module_inst->cur_exception[0] == '\0')
  2744. return NULL;
  2745. else
  2746. return module_inst->cur_exception;
  2747. }
  2748. bool
  2749. wasm_copy_exception(WASMModuleInstance *module_inst, char *exception_buf)
  2750. {
  2751. bool has_exception = false;
  2752. exception_lock(module_inst);
  2753. if (module_inst->cur_exception[0] != '\0') {
  2754. /* NULL is passed if the caller is not interested in getting the
  2755. * exception content, but only in knowing if an exception has been
  2756. * raised
  2757. */
  2758. if (exception_buf != NULL)
  2759. bh_memcpy_s(exception_buf, sizeof(module_inst->cur_exception),
  2760. module_inst->cur_exception,
  2761. sizeof(module_inst->cur_exception));
  2762. has_exception = true;
  2763. }
  2764. exception_unlock(module_inst);
  2765. return has_exception;
  2766. }
  2767. void
  2768. wasm_runtime_set_exception(WASMModuleInstanceCommon *module_inst_comm,
  2769. const char *exception)
  2770. {
  2771. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2772. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2773. || module_inst_comm->module_type == Wasm_Module_AoT);
  2774. wasm_set_exception(module_inst, exception);
  2775. }
  2776. const char *
  2777. wasm_runtime_get_exception(WASMModuleInstanceCommon *module_inst_comm)
  2778. {
  2779. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2780. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2781. || module_inst_comm->module_type == Wasm_Module_AoT);
  2782. return wasm_get_exception(module_inst);
  2783. }
  2784. bool
  2785. wasm_runtime_copy_exception(WASMModuleInstanceCommon *module_inst_comm,
  2786. char *exception_buf)
  2787. {
  2788. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2789. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2790. || module_inst_comm->module_type == Wasm_Module_AoT);
  2791. return wasm_copy_exception(module_inst, exception_buf);
  2792. }
  2793. void
  2794. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst_comm)
  2795. {
  2796. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2797. || module_inst_comm->module_type == Wasm_Module_AoT);
  2798. wasm_runtime_set_exception(module_inst_comm, NULL);
  2799. }
  2800. void
  2801. wasm_runtime_terminate(WASMModuleInstanceCommon *module_inst_comm)
  2802. {
  2803. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2804. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2805. || module_inst_comm->module_type == Wasm_Module_AoT);
  2806. wasm_set_exception(module_inst, "terminated by user");
  2807. }
  2808. void
  2809. wasm_runtime_set_custom_data_internal(
  2810. WASMModuleInstanceCommon *module_inst_comm, void *custom_data)
  2811. {
  2812. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2813. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2814. || module_inst_comm->module_type == Wasm_Module_AoT);
  2815. module_inst->custom_data = custom_data;
  2816. }
  2817. void
  2818. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  2819. void *custom_data)
  2820. {
  2821. #if WASM_ENABLE_THREAD_MGR != 0
  2822. wasm_cluster_spread_custom_data(module_inst, custom_data);
  2823. #else
  2824. wasm_runtime_set_custom_data_internal(module_inst, custom_data);
  2825. #endif
  2826. }
  2827. void *
  2828. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst_comm)
  2829. {
  2830. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2831. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2832. || module_inst_comm->module_type == Wasm_Module_AoT);
  2833. return module_inst->custom_data;
  2834. }
  2835. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  2836. void
  2837. wasm_runtime_set_bounds_checks(WASMModuleInstanceCommon *module_inst,
  2838. bool enable)
  2839. {
  2840. /* Always disable bounds checks if hw bounds checks is enabled */
  2841. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2842. enable = false;
  2843. #endif
  2844. #if WASM_ENABLE_INTERP != 0
  2845. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2846. ((WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e)
  2847. ->common.disable_bounds_checks = enable ? false : true;
  2848. }
  2849. #endif
  2850. #if WASM_ENABLE_AOT != 0
  2851. if (module_inst->module_type == Wasm_Module_AoT) {
  2852. ((AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e)
  2853. ->common.disable_bounds_checks = enable ? false : true;
  2854. }
  2855. #endif
  2856. }
  2857. bool
  2858. wasm_runtime_is_bounds_checks_enabled(WASMModuleInstanceCommon *module_inst)
  2859. {
  2860. #if WASM_ENABLE_INTERP != 0
  2861. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2862. return !((WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)
  2863. ->e)
  2864. ->common.disable_bounds_checks;
  2865. }
  2866. #endif
  2867. #if WASM_ENABLE_AOT != 0
  2868. if (module_inst->module_type == Wasm_Module_AoT) {
  2869. return !((AOTModuleInstanceExtra *)((WASMModuleInstance *)module_inst)
  2870. ->e)
  2871. ->common.disable_bounds_checks;
  2872. }
  2873. #endif
  2874. return true;
  2875. }
  2876. #endif
  2877. uint64
  2878. wasm_runtime_module_malloc_internal(WASMModuleInstanceCommon *module_inst,
  2879. WASMExecEnv *exec_env, uint64 size,
  2880. void **p_native_addr)
  2881. {
  2882. #if WASM_ENABLE_INTERP != 0
  2883. if (module_inst->module_type == Wasm_Module_Bytecode)
  2884. return wasm_module_malloc_internal((WASMModuleInstance *)module_inst,
  2885. exec_env, size, p_native_addr);
  2886. #endif
  2887. #if WASM_ENABLE_AOT != 0
  2888. if (module_inst->module_type == Wasm_Module_AoT)
  2889. return aot_module_malloc_internal((AOTModuleInstance *)module_inst,
  2890. exec_env, size, p_native_addr);
  2891. #endif
  2892. return 0;
  2893. }
  2894. uint64
  2895. wasm_runtime_module_realloc_internal(WASMModuleInstanceCommon *module_inst,
  2896. WASMExecEnv *exec_env, uint64 ptr,
  2897. uint64 size, void **p_native_addr)
  2898. {
  2899. #if WASM_ENABLE_INTERP != 0
  2900. if (module_inst->module_type == Wasm_Module_Bytecode)
  2901. return wasm_module_realloc_internal((WASMModuleInstance *)module_inst,
  2902. exec_env, ptr, size, p_native_addr);
  2903. #endif
  2904. #if WASM_ENABLE_AOT != 0
  2905. if (module_inst->module_type == Wasm_Module_AoT)
  2906. return aot_module_realloc_internal((AOTModuleInstance *)module_inst,
  2907. exec_env, ptr, size, p_native_addr);
  2908. #endif
  2909. return 0;
  2910. }
  2911. void
  2912. wasm_runtime_module_free_internal(WASMModuleInstanceCommon *module_inst,
  2913. WASMExecEnv *exec_env, uint64 ptr)
  2914. {
  2915. #if WASM_ENABLE_INTERP != 0
  2916. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2917. wasm_module_free_internal((WASMModuleInstance *)module_inst, exec_env,
  2918. ptr);
  2919. return;
  2920. }
  2921. #endif
  2922. #if WASM_ENABLE_AOT != 0
  2923. if (module_inst->module_type == Wasm_Module_AoT) {
  2924. aot_module_free_internal((AOTModuleInstance *)module_inst, exec_env,
  2925. ptr);
  2926. return;
  2927. }
  2928. #endif
  2929. }
  2930. uint64
  2931. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint64 size,
  2932. void **p_native_addr)
  2933. {
  2934. #if WASM_ENABLE_INTERP != 0
  2935. if (module_inst->module_type == Wasm_Module_Bytecode)
  2936. return wasm_module_malloc((WASMModuleInstance *)module_inst, size,
  2937. p_native_addr);
  2938. #endif
  2939. #if WASM_ENABLE_AOT != 0
  2940. if (module_inst->module_type == Wasm_Module_AoT)
  2941. return aot_module_malloc((AOTModuleInstance *)module_inst, size,
  2942. p_native_addr);
  2943. #endif
  2944. return 0;
  2945. }
  2946. uint64
  2947. wasm_runtime_module_realloc(WASMModuleInstanceCommon *module_inst, uint64 ptr,
  2948. uint64 size, void **p_native_addr)
  2949. {
  2950. #if WASM_ENABLE_INTERP != 0
  2951. if (module_inst->module_type == Wasm_Module_Bytecode)
  2952. return wasm_module_realloc((WASMModuleInstance *)module_inst, ptr, size,
  2953. p_native_addr);
  2954. #endif
  2955. #if WASM_ENABLE_AOT != 0
  2956. if (module_inst->module_type == Wasm_Module_AoT)
  2957. return aot_module_realloc((AOTModuleInstance *)module_inst, ptr, size,
  2958. p_native_addr);
  2959. #endif
  2960. return 0;
  2961. }
  2962. void
  2963. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint64 ptr)
  2964. {
  2965. #if WASM_ENABLE_INTERP != 0
  2966. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2967. wasm_module_free((WASMModuleInstance *)module_inst, ptr);
  2968. return;
  2969. }
  2970. #endif
  2971. #if WASM_ENABLE_AOT != 0
  2972. if (module_inst->module_type == Wasm_Module_AoT) {
  2973. aot_module_free((AOTModuleInstance *)module_inst, ptr);
  2974. return;
  2975. }
  2976. #endif
  2977. }
  2978. uint64
  2979. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  2980. const char *src, uint64 size)
  2981. {
  2982. #if WASM_ENABLE_INTERP != 0
  2983. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2984. return wasm_module_dup_data((WASMModuleInstance *)module_inst, src,
  2985. size);
  2986. }
  2987. #endif
  2988. #if WASM_ENABLE_AOT != 0
  2989. if (module_inst->module_type == Wasm_Module_AoT) {
  2990. return aot_module_dup_data((AOTModuleInstance *)module_inst, src, size);
  2991. }
  2992. #endif
  2993. return 0;
  2994. }
  2995. #if WASM_ENABLE_LIBC_WASI != 0
  2996. static WASIArguments *
  2997. get_wasi_args_from_module(wasm_module_t module)
  2998. {
  2999. WASIArguments *wasi_args = NULL;
  3000. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  3001. if (module->module_type == Wasm_Module_Bytecode)
  3002. wasi_args = &((WASMModule *)module)->wasi_args;
  3003. #endif
  3004. #if WASM_ENABLE_AOT != 0
  3005. if (module->module_type == Wasm_Module_AoT)
  3006. wasi_args = &((AOTModule *)module)->wasi_args;
  3007. #endif
  3008. return wasi_args;
  3009. }
  3010. void
  3011. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
  3012. uint32 dir_count, const char *map_dir_list[],
  3013. uint32 map_dir_count, const char *env_list[],
  3014. uint32 env_count, char *argv[], int argc,
  3015. int64 stdinfd, int64 stdoutfd, int64 stderrfd)
  3016. {
  3017. WASIArguments *wasi_args = get_wasi_args_from_module(module);
  3018. bh_assert(wasi_args);
  3019. wasi_args->dir_list = dir_list;
  3020. wasi_args->dir_count = dir_count;
  3021. wasi_args->map_dir_list = map_dir_list;
  3022. wasi_args->map_dir_count = map_dir_count;
  3023. wasi_args->env = env_list;
  3024. wasi_args->env_count = env_count;
  3025. wasi_args->argv = argv;
  3026. wasi_args->argc = (uint32)argc;
  3027. wasi_args->stdio[0] = (os_raw_file_handle)stdinfd;
  3028. wasi_args->stdio[1] = (os_raw_file_handle)stdoutfd;
  3029. wasi_args->stdio[2] = (os_raw_file_handle)stderrfd;
  3030. #if WASM_ENABLE_MULTI_MODULE != 0
  3031. #if WASM_ENABLE_INTERP != 0
  3032. if (module->module_type == Wasm_Module_Bytecode) {
  3033. wasm_propagate_wasi_args((WASMModule *)module);
  3034. }
  3035. #endif
  3036. #endif
  3037. }
  3038. void
  3039. wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
  3040. uint32 dir_count, const char *map_dir_list[],
  3041. uint32 map_dir_count, const char *env_list[],
  3042. uint32 env_count, char *argv[], int argc)
  3043. {
  3044. wasm_runtime_set_wasi_args_ex(module, dir_list, dir_count, map_dir_list,
  3045. map_dir_count, env_list, env_count, argv,
  3046. argc, -1, -1, -1);
  3047. }
  3048. void
  3049. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  3050. uint32 addr_pool_size)
  3051. {
  3052. WASIArguments *wasi_args = get_wasi_args_from_module(module);
  3053. if (wasi_args) {
  3054. wasi_args->addr_pool = addr_pool;
  3055. wasi_args->addr_count = addr_pool_size;
  3056. }
  3057. }
  3058. void
  3059. wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
  3060. const char *ns_lookup_pool[],
  3061. uint32 ns_lookup_pool_size)
  3062. {
  3063. WASIArguments *wasi_args = get_wasi_args_from_module(module);
  3064. if (wasi_args) {
  3065. wasi_args->ns_lookup_pool = ns_lookup_pool;
  3066. wasi_args->ns_lookup_count = ns_lookup_pool_size;
  3067. }
  3068. }
  3069. #if WASM_ENABLE_UVWASI == 0
  3070. static bool
  3071. copy_string_array(const char *array[], uint32 array_size, char **buf_ptr,
  3072. char ***list_ptr, uint64 *out_buf_size)
  3073. {
  3074. uint64 buf_size = 0, total_size;
  3075. uint32 buf_offset = 0, i;
  3076. char *buf = NULL, **list = NULL;
  3077. for (i = 0; i < array_size; i++)
  3078. buf_size += strlen(array[i]) + 1;
  3079. /* We add +1 to generate null-terminated array of strings */
  3080. total_size = sizeof(char *) * ((uint64)array_size + 1);
  3081. if (total_size >= UINT32_MAX
  3082. /* total_size must be larger than 0, don' check it again */
  3083. || !(list = wasm_runtime_malloc((uint32)total_size))
  3084. || buf_size >= UINT32_MAX
  3085. || (buf_size > 0 && !(buf = wasm_runtime_malloc((uint32)buf_size)))) {
  3086. if (buf)
  3087. wasm_runtime_free(buf);
  3088. if (list)
  3089. wasm_runtime_free(list);
  3090. return false;
  3091. }
  3092. for (i = 0; i < array_size; i++) {
  3093. list[i] = buf + buf_offset;
  3094. bh_strcpy_s(buf + buf_offset, (uint32)buf_size - buf_offset, array[i]);
  3095. buf_offset += (uint32)(strlen(array[i]) + 1);
  3096. }
  3097. list[array_size] = NULL;
  3098. *list_ptr = list;
  3099. *buf_ptr = buf;
  3100. if (out_buf_size)
  3101. *out_buf_size = buf_size;
  3102. return true;
  3103. }
  3104. bool
  3105. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  3106. const char *dir_list[], uint32 dir_count,
  3107. const char *map_dir_list[], uint32 map_dir_count,
  3108. const char *env[], uint32 env_count,
  3109. const char *addr_pool[], uint32 addr_pool_size,
  3110. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  3111. char *argv[], uint32 argc, os_raw_file_handle stdinfd,
  3112. os_raw_file_handle stdoutfd, os_raw_file_handle stderrfd,
  3113. char *error_buf, uint32 error_buf_size)
  3114. {
  3115. WASIContext *wasi_ctx;
  3116. char *argv_buf = NULL;
  3117. char **argv_list = NULL;
  3118. char *env_buf = NULL;
  3119. char **env_list = NULL;
  3120. char *ns_lookup_buf = NULL;
  3121. char **ns_lookup_list = NULL;
  3122. uint64 argv_buf_size = 0, env_buf_size = 0;
  3123. struct fd_table *curfds = NULL;
  3124. struct fd_prestats *prestats = NULL;
  3125. struct argv_environ_values *argv_environ = NULL;
  3126. struct addr_pool *apool = NULL;
  3127. bool fd_table_inited = false, fd_prestats_inited = false;
  3128. bool argv_environ_inited = false;
  3129. bool addr_pool_inited = false;
  3130. __wasi_fd_t wasm_fd = 3;
  3131. os_file_handle file_handle;
  3132. char *path, resolved_path[PATH_MAX];
  3133. uint32 i;
  3134. if (!(wasi_ctx = runtime_malloc(sizeof(WASIContext), NULL, error_buf,
  3135. error_buf_size))) {
  3136. return false;
  3137. }
  3138. wasm_runtime_set_wasi_ctx(module_inst, wasi_ctx);
  3139. /* process argv[0], trip the path and suffix, only keep the program name
  3140. */
  3141. if (!copy_string_array((const char **)argv, argc, &argv_buf, &argv_list,
  3142. &argv_buf_size)) {
  3143. set_error_buf(error_buf, error_buf_size,
  3144. "Init wasi environment failed: allocate memory failed");
  3145. goto fail;
  3146. }
  3147. if (!copy_string_array(env, env_count, &env_buf, &env_list,
  3148. &env_buf_size)) {
  3149. set_error_buf(error_buf, error_buf_size,
  3150. "Init wasi environment failed: allocate memory failed");
  3151. goto fail;
  3152. }
  3153. if (!(curfds = wasm_runtime_malloc(sizeof(struct fd_table)))
  3154. || !(prestats = wasm_runtime_malloc(sizeof(struct fd_prestats)))
  3155. || !(argv_environ =
  3156. wasm_runtime_malloc(sizeof(struct argv_environ_values)))
  3157. || !(apool = wasm_runtime_malloc(sizeof(struct addr_pool)))) {
  3158. set_error_buf(error_buf, error_buf_size,
  3159. "Init wasi environment failed: allocate memory failed");
  3160. goto fail;
  3161. }
  3162. if (!fd_table_init(curfds)) {
  3163. set_error_buf(error_buf, error_buf_size,
  3164. "Init wasi environment failed: "
  3165. "init fd table failed");
  3166. goto fail;
  3167. }
  3168. fd_table_inited = true;
  3169. if (!fd_prestats_init(prestats)) {
  3170. set_error_buf(error_buf, error_buf_size,
  3171. "Init wasi environment failed: "
  3172. "init fd prestats failed");
  3173. goto fail;
  3174. }
  3175. fd_prestats_inited = true;
  3176. if (!argv_environ_init(argv_environ, argv_buf, argv_buf_size, argv_list,
  3177. argc, env_buf, env_buf_size, env_list, env_count)) {
  3178. set_error_buf(error_buf, error_buf_size,
  3179. "Init wasi environment failed: "
  3180. "init argument environment failed");
  3181. goto fail;
  3182. }
  3183. argv_environ_inited = true;
  3184. if (!addr_pool_init(apool)) {
  3185. set_error_buf(error_buf, error_buf_size,
  3186. "Init wasi environment failed: "
  3187. "init the address pool failed");
  3188. goto fail;
  3189. }
  3190. addr_pool_inited = true;
  3191. os_file_handle stdin_file_handle = os_convert_stdin_handle(stdinfd);
  3192. os_file_handle stdout_file_handle = os_convert_stdout_handle(stdoutfd);
  3193. os_file_handle stderr_file_handle = os_convert_stderr_handle(stderrfd);
  3194. if (!os_is_handle_valid(&stdin_file_handle)
  3195. || !os_is_handle_valid(&stdout_file_handle)
  3196. || !os_is_handle_valid(&stderr_file_handle))
  3197. goto fail;
  3198. /* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
  3199. if (!fd_table_insert_existing(curfds, 0, stdin_file_handle, true)
  3200. || !fd_table_insert_existing(curfds, 1, stdout_file_handle, true)
  3201. || !fd_table_insert_existing(curfds, 2, stderr_file_handle, true)) {
  3202. set_error_buf(error_buf, error_buf_size,
  3203. "Init wasi environment failed: init fd table failed");
  3204. goto fail;
  3205. }
  3206. wasm_fd = 3;
  3207. for (i = 0; i < dir_count; i++, wasm_fd++) {
  3208. path = os_realpath(dir_list[i], resolved_path);
  3209. if (!path) {
  3210. if (error_buf)
  3211. snprintf(error_buf, error_buf_size,
  3212. "error while pre-opening directory %s: %d\n",
  3213. dir_list[i], errno);
  3214. goto fail;
  3215. }
  3216. __wasi_errno_t error = os_open_preopendir(path, &file_handle);
  3217. if (error != __WASI_ESUCCESS) {
  3218. if (error_buf)
  3219. snprintf(error_buf, error_buf_size,
  3220. "error while pre-opening directory %s: %d\n",
  3221. dir_list[i], error);
  3222. goto fail;
  3223. }
  3224. if (!fd_table_insert_existing(curfds, wasm_fd, file_handle, false)) {
  3225. if (error_buf)
  3226. snprintf(error_buf, error_buf_size,
  3227. "error inserting preopen fd %u (directory %s) into fd "
  3228. "table",
  3229. (unsigned int)wasm_fd, dir_list[i]);
  3230. goto fail;
  3231. }
  3232. if (!fd_prestats_insert(prestats, dir_list[i], wasm_fd)) {
  3233. if (error_buf)
  3234. snprintf(error_buf, error_buf_size,
  3235. "error inserting preopen fd %u (directory %s) into "
  3236. "prestats table",
  3237. (unsigned int)wasm_fd, dir_list[i]);
  3238. goto fail;
  3239. }
  3240. }
  3241. for (i = 0; i < map_dir_count; i++, wasm_fd++) {
  3242. char mapping_copy_buf[256];
  3243. char *mapping_copy = mapping_copy_buf;
  3244. char *map_mapped = NULL, *map_host = NULL;
  3245. const unsigned long max_len =
  3246. (unsigned long)strlen(map_dir_list[i]) * 2 + 3;
  3247. /* Allocation limit for runtime environments with reduced stack size */
  3248. if (max_len > 256) {
  3249. if (!(mapping_copy = wasm_runtime_malloc(max_len))) {
  3250. snprintf(error_buf, error_buf_size,
  3251. "error while allocating for directory mapping\n");
  3252. goto fail;
  3253. }
  3254. }
  3255. bh_memcpy_s(mapping_copy, max_len, map_dir_list[i],
  3256. (uint32)(strlen(map_dir_list[i]) + 1));
  3257. const char *delim = "::";
  3258. char *delim_pos = strstr(mapping_copy, delim);
  3259. if (delim_pos) {
  3260. *delim_pos = '\0';
  3261. map_mapped = mapping_copy;
  3262. map_host = delim_pos + strlen(delim);
  3263. }
  3264. if (!map_mapped || !map_host) {
  3265. if (error_buf)
  3266. snprintf(error_buf, error_buf_size,
  3267. "error while pre-opening mapped directory: "
  3268. "invalid map\n");
  3269. if (mapping_copy != mapping_copy_buf)
  3270. wasm_runtime_free(mapping_copy);
  3271. goto fail;
  3272. }
  3273. path = os_realpath(map_host, resolved_path);
  3274. if (!path) {
  3275. if (error_buf)
  3276. snprintf(error_buf, error_buf_size,
  3277. "error while pre-opening mapped directory %s: %d\n",
  3278. map_host, errno);
  3279. if (mapping_copy != mapping_copy_buf)
  3280. wasm_runtime_free(mapping_copy);
  3281. goto fail;
  3282. }
  3283. __wasi_errno_t error = os_open_preopendir(path, &file_handle);
  3284. if (error != __WASI_ESUCCESS) {
  3285. if (error_buf)
  3286. snprintf(error_buf, error_buf_size,
  3287. "error while pre-opening mapped directory %s: %d\n",
  3288. map_host, errno);
  3289. if (mapping_copy != mapping_copy_buf)
  3290. wasm_runtime_free(mapping_copy);
  3291. goto fail;
  3292. }
  3293. if (!fd_table_insert_existing(curfds, wasm_fd, file_handle, false)
  3294. || !fd_prestats_insert(prestats, map_mapped, wasm_fd)) {
  3295. if (error_buf)
  3296. snprintf(error_buf, error_buf_size,
  3297. "error while pre-opening mapped directory %s: "
  3298. "insertion failed\n",
  3299. dir_list[i]);
  3300. if (mapping_copy != mapping_copy_buf)
  3301. wasm_runtime_free(mapping_copy);
  3302. goto fail;
  3303. }
  3304. if (mapping_copy != mapping_copy_buf)
  3305. wasm_runtime_free(mapping_copy);
  3306. }
  3307. /* addr_pool(textual) -> apool */
  3308. for (i = 0; i < addr_pool_size; i++) {
  3309. char *cp, *address, *mask;
  3310. bool ret = false;
  3311. cp = bh_strdup(addr_pool[i]);
  3312. if (!cp) {
  3313. set_error_buf(error_buf, error_buf_size,
  3314. "Init wasi environment failed: copy address failed");
  3315. goto fail;
  3316. }
  3317. address = strtok(cp, "/");
  3318. mask = strtok(NULL, "/");
  3319. ret = addr_pool_insert(apool, address, (uint8)(mask ? atoi(mask) : 0));
  3320. wasm_runtime_free(cp);
  3321. if (!ret) {
  3322. set_error_buf(error_buf, error_buf_size,
  3323. "Init wasi environment failed: store address failed");
  3324. goto fail;
  3325. }
  3326. }
  3327. if (!copy_string_array(ns_lookup_pool, ns_lookup_pool_size, &ns_lookup_buf,
  3328. &ns_lookup_list, NULL)) {
  3329. set_error_buf(error_buf, error_buf_size,
  3330. "Init wasi environment failed: allocate memory failed");
  3331. goto fail;
  3332. }
  3333. wasi_ctx->curfds = curfds;
  3334. wasi_ctx->prestats = prestats;
  3335. wasi_ctx->argv_environ = argv_environ;
  3336. wasi_ctx->addr_pool = apool;
  3337. wasi_ctx->argv_buf = argv_buf;
  3338. wasi_ctx->argv_list = argv_list;
  3339. wasi_ctx->env_buf = env_buf;
  3340. wasi_ctx->env_list = env_list;
  3341. wasi_ctx->ns_lookup_buf = ns_lookup_buf;
  3342. wasi_ctx->ns_lookup_list = ns_lookup_list;
  3343. return true;
  3344. fail:
  3345. if (argv_environ_inited)
  3346. argv_environ_destroy(argv_environ);
  3347. if (fd_prestats_inited)
  3348. fd_prestats_destroy(prestats);
  3349. if (fd_table_inited)
  3350. fd_table_destroy(curfds);
  3351. if (addr_pool_inited)
  3352. addr_pool_destroy(apool);
  3353. if (curfds)
  3354. wasm_runtime_free(curfds);
  3355. if (prestats)
  3356. wasm_runtime_free(prestats);
  3357. if (argv_environ)
  3358. wasm_runtime_free(argv_environ);
  3359. if (apool)
  3360. wasm_runtime_free(apool);
  3361. if (argv_buf)
  3362. wasm_runtime_free(argv_buf);
  3363. if (argv_list)
  3364. wasm_runtime_free(argv_list);
  3365. if (env_buf)
  3366. wasm_runtime_free(env_buf);
  3367. if (env_list)
  3368. wasm_runtime_free(env_list);
  3369. if (ns_lookup_buf)
  3370. wasm_runtime_free(ns_lookup_buf);
  3371. if (ns_lookup_list)
  3372. wasm_runtime_free(ns_lookup_list);
  3373. return false;
  3374. }
  3375. #else /* else of WASM_ENABLE_UVWASI == 0 */
  3376. static void *
  3377. wasm_uvwasi_malloc(size_t size, void *mem_user_data)
  3378. {
  3379. return runtime_malloc(size, NULL, NULL, 0);
  3380. (void)mem_user_data;
  3381. }
  3382. static void
  3383. wasm_uvwasi_free(void *ptr, void *mem_user_data)
  3384. {
  3385. if (ptr)
  3386. wasm_runtime_free(ptr);
  3387. (void)mem_user_data;
  3388. }
  3389. static void *
  3390. wasm_uvwasi_calloc(size_t nmemb, size_t size, void *mem_user_data)
  3391. {
  3392. uint64 total_size = (uint64)nmemb * size;
  3393. return runtime_malloc(total_size, NULL, NULL, 0);
  3394. (void)mem_user_data;
  3395. }
  3396. static void *
  3397. wasm_uvwasi_realloc(void *ptr, size_t size, void *mem_user_data)
  3398. {
  3399. if (size >= UINT32_MAX) {
  3400. return NULL;
  3401. }
  3402. return wasm_runtime_realloc(ptr, (uint32)size);
  3403. }
  3404. /* clang-format off */
  3405. static uvwasi_mem_t uvwasi_allocator = {
  3406. .mem_user_data = 0,
  3407. .malloc = wasm_uvwasi_malloc,
  3408. .free = wasm_uvwasi_free,
  3409. .calloc = wasm_uvwasi_calloc,
  3410. .realloc = wasm_uvwasi_realloc
  3411. };
  3412. /* clang-format on */
  3413. bool
  3414. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  3415. const char *dir_list[], uint32 dir_count,
  3416. const char *map_dir_list[], uint32 map_dir_count,
  3417. const char *env[], uint32 env_count,
  3418. const char *addr_pool[], uint32 addr_pool_size,
  3419. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  3420. char *argv[], uint32 argc, os_raw_file_handle stdinfd,
  3421. os_raw_file_handle stdoutfd, os_raw_file_handle stderrfd,
  3422. char *error_buf, uint32 error_buf_size)
  3423. {
  3424. WASIContext *ctx;
  3425. uvwasi_t *uvwasi;
  3426. uvwasi_options_t init_options;
  3427. const char **envp = NULL;
  3428. uint64 total_size;
  3429. uint32 i;
  3430. bool ret = false;
  3431. ctx = runtime_malloc(sizeof(*ctx), module_inst, error_buf, error_buf_size);
  3432. if (!ctx)
  3433. return false;
  3434. uvwasi = &ctx->uvwasi;
  3435. /* Setup the initialization options */
  3436. uvwasi_options_init(&init_options);
  3437. init_options.allocator = &uvwasi_allocator;
  3438. init_options.argc = argc;
  3439. init_options.argv = (const char **)argv;
  3440. init_options.in = (stdinfd != -1) ? (uvwasi_fd_t)stdinfd : init_options.in;
  3441. init_options.out =
  3442. (stdoutfd != -1) ? (uvwasi_fd_t)stdoutfd : init_options.out;
  3443. init_options.err =
  3444. (stderrfd != -1) ? (uvwasi_fd_t)stderrfd : init_options.err;
  3445. if (dir_count > 0) {
  3446. init_options.preopenc = dir_count;
  3447. total_size = sizeof(uvwasi_preopen_t) * (uint64)init_options.preopenc;
  3448. init_options.preopens = (uvwasi_preopen_t *)runtime_malloc(
  3449. total_size, module_inst, error_buf, error_buf_size);
  3450. if (init_options.preopens == NULL)
  3451. goto fail;
  3452. for (i = 0; i < init_options.preopenc; i++) {
  3453. init_options.preopens[i].real_path = dir_list[i];
  3454. init_options.preopens[i].mapped_path =
  3455. (i < map_dir_count) ? map_dir_list[i] : dir_list[i];
  3456. }
  3457. }
  3458. if (env_count > 0) {
  3459. total_size = sizeof(char *) * (uint64)(env_count + 1);
  3460. envp =
  3461. runtime_malloc(total_size, module_inst, error_buf, error_buf_size);
  3462. if (envp == NULL)
  3463. goto fail;
  3464. for (i = 0; i < env_count; i++) {
  3465. envp[i] = env[i];
  3466. }
  3467. envp[env_count] = NULL;
  3468. init_options.envp = envp;
  3469. }
  3470. if (UVWASI_ESUCCESS != uvwasi_init(uvwasi, &init_options)) {
  3471. set_error_buf(error_buf, error_buf_size, "uvwasi init failed");
  3472. goto fail;
  3473. }
  3474. wasm_runtime_set_wasi_ctx(module_inst, ctx);
  3475. ret = true;
  3476. fail:
  3477. if (envp)
  3478. wasm_runtime_free((void *)envp);
  3479. if (init_options.preopens)
  3480. wasm_runtime_free(init_options.preopens);
  3481. if (!ret && uvwasi)
  3482. wasm_runtime_free(uvwasi);
  3483. return ret;
  3484. }
  3485. #endif /* end of WASM_ENABLE_UVWASI */
  3486. bool
  3487. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst)
  3488. {
  3489. #if WASM_ENABLE_INTERP != 0
  3490. if (module_inst->module_type == Wasm_Module_Bytecode
  3491. && ((WASMModuleInstance *)module_inst)->module->import_wasi_api)
  3492. return true;
  3493. #endif
  3494. #if WASM_ENABLE_AOT != 0
  3495. if (module_inst->module_type == Wasm_Module_AoT
  3496. && ((AOTModule *)((AOTModuleInstance *)module_inst)->module)
  3497. ->import_wasi_api)
  3498. return true;
  3499. #endif
  3500. return false;
  3501. }
  3502. WASMFunctionInstanceCommon *
  3503. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst)
  3504. {
  3505. #if WASM_ENABLE_INTERP != 0
  3506. if (module_inst->module_type == Wasm_Module_Bytecode) {
  3507. WASMModuleInstance *wasm_inst = (WASMModuleInstance *)module_inst;
  3508. WASMFunctionInstance *func = wasm_lookup_function(wasm_inst, "_start");
  3509. if (func) {
  3510. if (func->u.func->func_type->param_count != 0
  3511. || func->u.func->func_type->result_count != 0) {
  3512. LOG_ERROR("Lookup wasi _start function failed: "
  3513. "invalid function type.\n");
  3514. return NULL;
  3515. }
  3516. return (WASMFunctionInstanceCommon *)func;
  3517. }
  3518. return NULL;
  3519. }
  3520. #endif
  3521. #if WASM_ENABLE_AOT != 0
  3522. if (module_inst->module_type == Wasm_Module_AoT) {
  3523. AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
  3524. AOTFunctionInstance *func = aot_lookup_function(aot_inst, "_start");
  3525. if (func) {
  3526. AOTFuncType *func_type = func->u.func.func_type;
  3527. if (func_type->param_count != 0 || func_type->result_count != 0) {
  3528. LOG_ERROR("Lookup wasi _start function failed: "
  3529. "invalid function type.\n");
  3530. return NULL;
  3531. }
  3532. return func;
  3533. }
  3534. return NULL;
  3535. }
  3536. #endif /* end of WASM_ENABLE_AOT */
  3537. return NULL;
  3538. }
  3539. #if WASM_ENABLE_UVWASI == 0
  3540. void
  3541. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  3542. {
  3543. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  3544. if (wasi_ctx) {
  3545. if (wasi_ctx->argv_environ) {
  3546. argv_environ_destroy(wasi_ctx->argv_environ);
  3547. wasm_runtime_free(wasi_ctx->argv_environ);
  3548. }
  3549. if (wasi_ctx->curfds) {
  3550. fd_table_destroy(wasi_ctx->curfds);
  3551. wasm_runtime_free(wasi_ctx->curfds);
  3552. }
  3553. if (wasi_ctx->prestats) {
  3554. fd_prestats_destroy(wasi_ctx->prestats);
  3555. wasm_runtime_free(wasi_ctx->prestats);
  3556. }
  3557. if (wasi_ctx->addr_pool) {
  3558. addr_pool_destroy(wasi_ctx->addr_pool);
  3559. wasm_runtime_free(wasi_ctx->addr_pool);
  3560. }
  3561. if (wasi_ctx->argv_buf)
  3562. wasm_runtime_free(wasi_ctx->argv_buf);
  3563. if (wasi_ctx->argv_list)
  3564. wasm_runtime_free(wasi_ctx->argv_list);
  3565. if (wasi_ctx->env_buf)
  3566. wasm_runtime_free(wasi_ctx->env_buf);
  3567. if (wasi_ctx->env_list)
  3568. wasm_runtime_free(wasi_ctx->env_list);
  3569. if (wasi_ctx->ns_lookup_buf)
  3570. wasm_runtime_free(wasi_ctx->ns_lookup_buf);
  3571. if (wasi_ctx->ns_lookup_list)
  3572. wasm_runtime_free(wasi_ctx->ns_lookup_list);
  3573. wasm_runtime_free(wasi_ctx);
  3574. }
  3575. }
  3576. #else
  3577. void
  3578. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  3579. {
  3580. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  3581. if (wasi_ctx) {
  3582. uvwasi_destroy(&wasi_ctx->uvwasi);
  3583. wasm_runtime_free(wasi_ctx);
  3584. }
  3585. }
  3586. #endif
  3587. uint32_t
  3588. wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst)
  3589. {
  3590. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  3591. #if WASM_ENABLE_THREAD_MGR != 0
  3592. WASMCluster *cluster;
  3593. WASMExecEnv *exec_env;
  3594. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  3595. if (exec_env && (cluster = wasm_exec_env_get_cluster(exec_env))) {
  3596. /**
  3597. * The main thread may exit earlier than other threads, and
  3598. * the exit_code of wasi_ctx may be changed by other thread
  3599. * when it runs into wasi_proc_exit, here we wait until all
  3600. * other threads exit to avoid getting invalid exit_code.
  3601. */
  3602. wasm_cluster_wait_for_all_except_self(cluster, exec_env);
  3603. }
  3604. #endif
  3605. return wasi_ctx->exit_code;
  3606. }
  3607. #endif /* end of WASM_ENABLE_LIBC_WASI */
  3608. WASMModuleCommon *
  3609. wasm_exec_env_get_module(WASMExecEnv *exec_env)
  3610. {
  3611. WASMModuleInstanceCommon *module_inst_comm =
  3612. wasm_runtime_get_module_inst(exec_env);
  3613. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  3614. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  3615. || module_inst_comm->module_type == Wasm_Module_AoT);
  3616. return (WASMModuleCommon *)module_inst->module;
  3617. }
  3618. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  3619. const uint8 *
  3620. wasm_runtime_get_custom_section(WASMModuleCommon *const module_comm,
  3621. const char *name, uint32 *len)
  3622. {
  3623. #if WASM_ENABLE_INTERP != 0
  3624. if (module_comm->module_type == Wasm_Module_Bytecode)
  3625. return wasm_loader_get_custom_section((WASMModule *)module_comm, name,
  3626. len);
  3627. #endif
  3628. #if WASM_ENABLE_AOT != 0
  3629. if (module_comm->module_type == Wasm_Module_AoT)
  3630. return aot_get_custom_section((AOTModule *)module_comm, name, len);
  3631. #endif
  3632. return NULL;
  3633. }
  3634. #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 */
  3635. static union {
  3636. int a;
  3637. char b;
  3638. } __ue = { .a = 1 };
  3639. #define is_little_endian() (__ue.b == 1) /* NOLINT */
  3640. int32
  3641. wasm_runtime_get_import_count(WASMModuleCommon *const module)
  3642. {
  3643. if (!module) {
  3644. bh_assert(0);
  3645. return -1;
  3646. }
  3647. #if WASM_ENABLE_AOT != 0
  3648. if (module->module_type == Wasm_Module_AoT) {
  3649. const AOTModule *aot_module = (const AOTModule *)module;
  3650. return (int32)(aot_module->import_func_count
  3651. + aot_module->import_global_count
  3652. + aot_module->import_table_count
  3653. + aot_module->import_memory_count);
  3654. }
  3655. #endif
  3656. #if WASM_ENABLE_INTERP != 0
  3657. if (module->module_type == Wasm_Module_Bytecode) {
  3658. const WASMModule *wasm_module = (const WASMModule *)module;
  3659. return (int32)wasm_module->import_count;
  3660. }
  3661. #endif
  3662. return -1;
  3663. }
  3664. void
  3665. wasm_runtime_get_import_type(WASMModuleCommon *const module, int32 import_index,
  3666. wasm_import_t *import_type)
  3667. {
  3668. if (!import_type) {
  3669. bh_assert(0);
  3670. return;
  3671. }
  3672. memset(import_type, 0, sizeof(wasm_import_t));
  3673. if (!module) {
  3674. bh_assert(0);
  3675. return;
  3676. }
  3677. #if WASM_ENABLE_AOT != 0
  3678. if (module->module_type == Wasm_Module_AoT) {
  3679. const AOTModule *aot_module = (const AOTModule *)module;
  3680. uint32 func_index = (uint32)import_index;
  3681. if (func_index < aot_module->import_func_count) {
  3682. const AOTImportFunc *aot_import_func =
  3683. &aot_module->import_funcs[func_index];
  3684. import_type->module_name = aot_import_func->module_name;
  3685. import_type->name = aot_import_func->func_name;
  3686. import_type->kind = WASM_IMPORT_EXPORT_KIND_FUNC;
  3687. import_type->linked =
  3688. aot_import_func->func_ptr_linked ? true : false;
  3689. import_type->u.func_type =
  3690. (WASMFuncType *)aot_import_func->func_type;
  3691. return;
  3692. }
  3693. uint32 global_index = func_index - aot_module->import_func_count;
  3694. if (global_index < aot_module->import_global_count) {
  3695. const AOTImportGlobal *aot_import_global =
  3696. &aot_module->import_globals[global_index];
  3697. import_type->module_name = aot_import_global->module_name;
  3698. import_type->name = aot_import_global->global_name;
  3699. import_type->kind = WASM_IMPORT_EXPORT_KIND_GLOBAL;
  3700. import_type->linked = aot_import_global->is_linked;
  3701. import_type->u.global_type =
  3702. (WASMGlobalType *)&aot_import_global->type;
  3703. return;
  3704. }
  3705. uint32 table_index = global_index - aot_module->import_global_count;
  3706. if (table_index < aot_module->import_table_count) {
  3707. const AOTImportTable *aot_import_table =
  3708. &aot_module->import_tables[table_index];
  3709. import_type->module_name = aot_import_table->module_name;
  3710. import_type->name = aot_import_table->table_name;
  3711. import_type->kind = WASM_IMPORT_EXPORT_KIND_TABLE;
  3712. import_type->linked = false; /* not supported */
  3713. import_type->u.table_type =
  3714. (WASMTableType *)&aot_import_table->table_type;
  3715. return;
  3716. }
  3717. uint32 memory_index = table_index - aot_module->import_table_count;
  3718. if (memory_index < aot_module->import_memory_count) {
  3719. const AOTImportMemory *aot_import_memory =
  3720. &aot_module->import_memories[memory_index];
  3721. import_type->module_name = aot_import_memory->module_name;
  3722. import_type->name = aot_import_memory->memory_name;
  3723. import_type->kind = WASM_IMPORT_EXPORT_KIND_MEMORY;
  3724. import_type->linked = false; /* not supported */
  3725. import_type->u.memory_type =
  3726. (WASMMemoryType *)&aot_import_memory->mem_type;
  3727. return;
  3728. }
  3729. bh_assert(0);
  3730. return;
  3731. }
  3732. #endif
  3733. #if WASM_ENABLE_INTERP != 0
  3734. if (module->module_type == Wasm_Module_Bytecode) {
  3735. const WASMModule *wasm_module = (const WASMModule *)module;
  3736. if ((uint32)import_index >= wasm_module->import_count) {
  3737. bh_assert(0);
  3738. return;
  3739. }
  3740. const WASMImport *wasm_import = &wasm_module->imports[import_index];
  3741. import_type->module_name = wasm_import->u.names.module_name;
  3742. import_type->name = wasm_import->u.names.field_name;
  3743. import_type->kind = wasm_import->kind;
  3744. switch (import_type->kind) {
  3745. case WASM_IMPORT_EXPORT_KIND_FUNC:
  3746. import_type->linked = wasm_import->u.function.func_ptr_linked;
  3747. import_type->u.func_type =
  3748. (WASMFuncType *)wasm_import->u.function.func_type;
  3749. break;
  3750. case WASM_IMPORT_EXPORT_KIND_GLOBAL:
  3751. import_type->linked = wasm_import->u.global.is_linked;
  3752. import_type->u.global_type =
  3753. (WASMGlobalType *)&wasm_import->u.global.type;
  3754. break;
  3755. case WASM_IMPORT_EXPORT_KIND_TABLE:
  3756. import_type->linked = false; /* not supported */
  3757. import_type->u.table_type =
  3758. (WASMTableType *)&wasm_import->u.table.table_type;
  3759. break;
  3760. case WASM_IMPORT_EXPORT_KIND_MEMORY:
  3761. import_type->linked = false; /* not supported */
  3762. import_type->u.memory_type =
  3763. (WASMMemoryType *)&wasm_import->u.memory.mem_type;
  3764. break;
  3765. default:
  3766. bh_assert(0);
  3767. break;
  3768. }
  3769. return;
  3770. }
  3771. #endif
  3772. }
  3773. int32
  3774. wasm_runtime_get_export_count(WASMModuleCommon *const module)
  3775. {
  3776. if (!module) {
  3777. bh_assert(0);
  3778. return -1;
  3779. }
  3780. #if WASM_ENABLE_AOT != 0
  3781. if (module->module_type == Wasm_Module_AoT) {
  3782. const AOTModule *aot_module = (const AOTModule *)module;
  3783. return (int32)aot_module->export_count;
  3784. }
  3785. #endif
  3786. #if WASM_ENABLE_INTERP != 0
  3787. if (module->module_type == Wasm_Module_Bytecode) {
  3788. const WASMModule *wasm_module = (const WASMModule *)module;
  3789. return (int32)wasm_module->export_count;
  3790. }
  3791. #endif
  3792. return -1;
  3793. }
  3794. void
  3795. wasm_runtime_get_export_type(WASMModuleCommon *const module, int32 export_index,
  3796. wasm_export_t *export_type)
  3797. {
  3798. if (!export_type) {
  3799. bh_assert(0);
  3800. return;
  3801. }
  3802. memset(export_type, 0, sizeof(wasm_export_t));
  3803. if (!module) {
  3804. bh_assert(0);
  3805. return;
  3806. }
  3807. #if WASM_ENABLE_AOT != 0
  3808. if (module->module_type == Wasm_Module_AoT) {
  3809. const AOTModule *aot_module = (const AOTModule *)module;
  3810. if ((uint32)export_index >= aot_module->export_count) {
  3811. bh_assert(0);
  3812. return;
  3813. }
  3814. const AOTExport *aot_export = &aot_module->exports[export_index];
  3815. export_type->name = aot_export->name;
  3816. export_type->kind = aot_export->kind;
  3817. switch (export_type->kind) {
  3818. case WASM_IMPORT_EXPORT_KIND_FUNC:
  3819. {
  3820. if (aot_export->index < aot_module->import_func_count) {
  3821. export_type->u.func_type =
  3822. (AOTFuncType *)aot_module
  3823. ->import_funcs[aot_export->index]
  3824. .func_type;
  3825. }
  3826. else {
  3827. export_type->u.func_type =
  3828. (AOTFuncType *)aot_module
  3829. ->types[aot_module->func_type_indexes
  3830. [aot_export->index
  3831. - aot_module->import_func_count]];
  3832. }
  3833. break;
  3834. }
  3835. case WASM_IMPORT_EXPORT_KIND_GLOBAL:
  3836. {
  3837. if (aot_export->index < aot_module->import_global_count) {
  3838. export_type->u.global_type =
  3839. &aot_module->import_globals[aot_export->index].type;
  3840. }
  3841. else {
  3842. export_type->u.global_type =
  3843. &aot_module
  3844. ->globals[aot_export->index
  3845. - aot_module->import_global_count]
  3846. .type;
  3847. }
  3848. break;
  3849. }
  3850. case WASM_IMPORT_EXPORT_KIND_TABLE:
  3851. {
  3852. if (aot_export->index < aot_module->import_table_count) {
  3853. export_type->u.table_type =
  3854. &aot_module->import_tables[aot_export->index]
  3855. .table_type;
  3856. }
  3857. else {
  3858. export_type->u.table_type =
  3859. &aot_module
  3860. ->tables[aot_export->index
  3861. - aot_module->import_table_count]
  3862. .table_type;
  3863. }
  3864. break;
  3865. }
  3866. case WASM_IMPORT_EXPORT_KIND_MEMORY:
  3867. {
  3868. if (aot_export->index < aot_module->import_memory_count) {
  3869. export_type->u.memory_type =
  3870. &aot_module->import_memories[aot_export->index]
  3871. .mem_type;
  3872. }
  3873. else {
  3874. export_type->u.memory_type =
  3875. &aot_module
  3876. ->memories[aot_export->index
  3877. - aot_module->import_memory_count];
  3878. }
  3879. break;
  3880. }
  3881. default:
  3882. bh_assert(0);
  3883. break;
  3884. }
  3885. return;
  3886. }
  3887. #endif
  3888. #if WASM_ENABLE_INTERP != 0
  3889. if (module->module_type == Wasm_Module_Bytecode) {
  3890. const WASMModule *wasm_module = (const WASMModule *)module;
  3891. if ((uint32)export_index >= wasm_module->export_count) {
  3892. bh_assert(0);
  3893. return;
  3894. }
  3895. const WASMExport *wasm_export = &wasm_module->exports[export_index];
  3896. export_type->name = wasm_export->name;
  3897. export_type->kind = wasm_export->kind;
  3898. switch (export_type->kind) {
  3899. case WASM_IMPORT_EXPORT_KIND_FUNC:
  3900. {
  3901. if (wasm_export->index < wasm_module->import_function_count) {
  3902. export_type->u.func_type =
  3903. (WASMFuncType *)wasm_module
  3904. ->import_functions[wasm_export->index]
  3905. .u.function.func_type;
  3906. }
  3907. else {
  3908. export_type->u.func_type =
  3909. wasm_module
  3910. ->functions[wasm_export->index
  3911. - wasm_module->import_function_count]
  3912. ->func_type;
  3913. }
  3914. break;
  3915. }
  3916. case WASM_IMPORT_EXPORT_KIND_GLOBAL:
  3917. {
  3918. if (wasm_export->index < wasm_module->import_global_count) {
  3919. export_type->u.global_type =
  3920. (WASMGlobalType *)&wasm_module
  3921. ->import_globals[wasm_export->index]
  3922. .u.global.type;
  3923. }
  3924. else {
  3925. export_type->u.global_type =
  3926. &wasm_module
  3927. ->globals[wasm_export->index
  3928. - wasm_module->import_global_count]
  3929. .type;
  3930. }
  3931. break;
  3932. }
  3933. case WASM_IMPORT_EXPORT_KIND_TABLE:
  3934. {
  3935. if (wasm_export->index < wasm_module->import_table_count) {
  3936. export_type->u.table_type =
  3937. (WASMTableType *)&wasm_module
  3938. ->import_tables[wasm_export->index]
  3939. .u.table.table_type;
  3940. }
  3941. else {
  3942. export_type->u.table_type =
  3943. &wasm_module
  3944. ->tables[wasm_export->index
  3945. - wasm_module->import_table_count]
  3946. .table_type;
  3947. }
  3948. break;
  3949. }
  3950. case WASM_IMPORT_EXPORT_KIND_MEMORY:
  3951. {
  3952. if (wasm_export->index < wasm_module->import_memory_count) {
  3953. export_type->u.memory_type =
  3954. (WASMMemoryType *)&wasm_module
  3955. ->import_memories[wasm_export->index]
  3956. .u.memory.mem_type;
  3957. }
  3958. else {
  3959. export_type->u.memory_type =
  3960. &wasm_module
  3961. ->memories[wasm_export->index
  3962. - wasm_module->import_memory_count];
  3963. }
  3964. break;
  3965. }
  3966. default:
  3967. bh_assert(0);
  3968. break;
  3969. }
  3970. return;
  3971. }
  3972. #endif
  3973. }
  3974. uint32
  3975. wasm_func_type_get_param_count(WASMFuncType *const func_type)
  3976. {
  3977. bh_assert(func_type);
  3978. return func_type->param_count;
  3979. }
  3980. wasm_valkind_t
  3981. wasm_func_type_get_param_valkind(WASMFuncType *const func_type,
  3982. uint32 param_index)
  3983. {
  3984. if (!func_type || (param_index >= func_type->param_count)) {
  3985. bh_assert(0);
  3986. return (wasm_valkind_t)-1;
  3987. }
  3988. switch (func_type->types[param_index]) {
  3989. case VALUE_TYPE_I32:
  3990. return WASM_I32;
  3991. case VALUE_TYPE_I64:
  3992. return WASM_I64;
  3993. case VALUE_TYPE_F32:
  3994. return WASM_F32;
  3995. case VALUE_TYPE_F64:
  3996. return WASM_F64;
  3997. case VALUE_TYPE_V128:
  3998. return WASM_V128;
  3999. case VALUE_TYPE_FUNCREF:
  4000. return WASM_FUNCREF;
  4001. case VALUE_TYPE_EXTERNREF:
  4002. return WASM_EXTERNREF;
  4003. case VALUE_TYPE_VOID:
  4004. default:
  4005. {
  4006. bh_assert(0);
  4007. return (wasm_valkind_t)-1;
  4008. }
  4009. }
  4010. }
  4011. uint32
  4012. wasm_func_type_get_result_count(WASMFuncType *const func_type)
  4013. {
  4014. bh_assert(func_type);
  4015. return func_type->result_count;
  4016. }
  4017. wasm_valkind_t
  4018. wasm_func_type_get_result_valkind(WASMFuncType *const func_type,
  4019. uint32 result_index)
  4020. {
  4021. if (!func_type || (result_index >= func_type->result_count)) {
  4022. bh_assert(0);
  4023. return (wasm_valkind_t)-1;
  4024. }
  4025. switch (func_type->types[func_type->param_count + result_index]) {
  4026. case VALUE_TYPE_I32:
  4027. return WASM_I32;
  4028. case VALUE_TYPE_I64:
  4029. return WASM_I64;
  4030. case VALUE_TYPE_F32:
  4031. return WASM_F32;
  4032. case VALUE_TYPE_F64:
  4033. return WASM_F64;
  4034. case VALUE_TYPE_FUNCREF:
  4035. return WASM_FUNCREF;
  4036. #if WASM_ENABLE_SIMD != 0
  4037. case VALUE_TYPE_V128:
  4038. return WASM_V128;
  4039. #endif
  4040. #if WASM_ENABLE_REF_TYPES != 0
  4041. case VALUE_TYPE_EXTERNREF:
  4042. #endif
  4043. case VALUE_TYPE_VOID:
  4044. default:
  4045. {
  4046. bh_assert(0);
  4047. return (wasm_valkind_t)-1;
  4048. }
  4049. }
  4050. }
  4051. wasm_valkind_t
  4052. wasm_global_type_get_valkind(WASMGlobalType *const global_type)
  4053. {
  4054. bh_assert(global_type);
  4055. return val_type_to_val_kind(global_type->val_type);
  4056. }
  4057. bool
  4058. wasm_global_type_get_mutable(WASMGlobalType *const global_type)
  4059. {
  4060. bh_assert(global_type);
  4061. return global_type->is_mutable;
  4062. }
  4063. bool
  4064. wasm_memory_type_get_shared(WASMMemoryType *const memory_type)
  4065. {
  4066. bh_assert(memory_type);
  4067. return (memory_type->flags & SHARED_MEMORY_FLAG) ? true : false;
  4068. }
  4069. uint32
  4070. wasm_memory_type_get_init_page_count(WASMMemoryType *const memory_type)
  4071. {
  4072. bh_assert(memory_type);
  4073. return memory_type->init_page_count;
  4074. }
  4075. uint32
  4076. wasm_memory_type_get_max_page_count(WASMMemoryType *const memory_type)
  4077. {
  4078. bh_assert(memory_type);
  4079. return memory_type->max_page_count;
  4080. }
  4081. wasm_valkind_t
  4082. wasm_table_type_get_elem_kind(WASMTableType *const table_type)
  4083. {
  4084. bh_assert(table_type);
  4085. return val_type_to_val_kind(table_type->elem_type);
  4086. }
  4087. bool
  4088. wasm_table_type_get_shared(WASMTableType *const table_type)
  4089. {
  4090. bh_assert(table_type);
  4091. return (table_type->flags & 2) ? true : false;
  4092. }
  4093. uint32
  4094. wasm_table_type_get_init_size(WASMTableType *const table_type)
  4095. {
  4096. bh_assert(table_type);
  4097. return table_type->init_size;
  4098. }
  4099. uint32
  4100. wasm_table_type_get_max_size(WASMTableType *const table_type)
  4101. {
  4102. bh_assert(table_type);
  4103. return table_type->max_size;
  4104. }
  4105. bool
  4106. wasm_runtime_register_natives(const char *module_name,
  4107. NativeSymbol *native_symbols,
  4108. uint32 n_native_symbols)
  4109. {
  4110. return wasm_native_register_natives(module_name, native_symbols,
  4111. n_native_symbols);
  4112. }
  4113. bool
  4114. wasm_runtime_register_natives_raw(const char *module_name,
  4115. NativeSymbol *native_symbols,
  4116. uint32 n_native_symbols)
  4117. {
  4118. return wasm_native_register_natives_raw(module_name, native_symbols,
  4119. n_native_symbols);
  4120. }
  4121. bool
  4122. wasm_runtime_unregister_natives(const char *module_name,
  4123. NativeSymbol *native_symbols)
  4124. {
  4125. return wasm_native_unregister_natives(module_name, native_symbols);
  4126. }
  4127. bool
  4128. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  4129. const WASMFuncType *func_type,
  4130. const char *signature, void *attachment,
  4131. uint32 *argv, uint32 argc, uint32 *argv_ret)
  4132. {
  4133. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  4134. #if WASM_ENABLE_MEMORY64 != 0
  4135. WASMMemoryInstance *memory =
  4136. wasm_get_default_memory((WASMModuleInstance *)module);
  4137. bool is_memory64 = memory ? memory->is_memory64 : false;
  4138. #endif
  4139. typedef void (*NativeRawFuncPtr)(WASMExecEnv *, uint64 *);
  4140. NativeRawFuncPtr invoke_native_raw = (NativeRawFuncPtr)func_ptr;
  4141. uint64 argv_buf[16] = { 0 }, *argv1 = argv_buf, *argv_dst, size;
  4142. uint32 *argv_src = argv, i, argc1, ptr_len;
  4143. uint32 arg_i32;
  4144. bool ret = false;
  4145. argc1 = func_type->param_count;
  4146. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  4147. size = sizeof(uint64) * (uint64)argc1;
  4148. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  4149. 0))) {
  4150. return false;
  4151. }
  4152. }
  4153. argv_dst = argv1;
  4154. /* Traverse secondly to fill in each argument */
  4155. for (i = 0; i < func_type->param_count; i++, argv_dst++) {
  4156. switch (func_type->types[i]) {
  4157. case VALUE_TYPE_I32:
  4158. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4159. case VALUE_TYPE_FUNCREF:
  4160. #endif
  4161. {
  4162. *(uint32 *)argv_dst = arg_i32 = *argv_src++;
  4163. if (signature
  4164. #if WASM_ENABLE_MEMORY64 != 0
  4165. && !is_memory64
  4166. #endif
  4167. ) {
  4168. if (signature[i + 1] == '*') {
  4169. /* param is a pointer */
  4170. if (signature[i + 2] == '~')
  4171. /* pointer with length followed */
  4172. ptr_len = *argv_src;
  4173. else
  4174. /* pointer without length followed */
  4175. ptr_len = 1;
  4176. if (!wasm_runtime_validate_app_addr(
  4177. module, (uint64)arg_i32, (uint64)ptr_len))
  4178. goto fail;
  4179. *(uintptr_t *)argv_dst =
  4180. (uintptr_t)wasm_runtime_addr_app_to_native(
  4181. module, (uint64)arg_i32);
  4182. }
  4183. else if (signature[i + 1] == '$') {
  4184. /* param is a string */
  4185. if (!wasm_runtime_validate_app_str_addr(
  4186. module, (uint64)arg_i32))
  4187. goto fail;
  4188. *(uintptr_t *)argv_dst =
  4189. (uintptr_t)wasm_runtime_addr_app_to_native(
  4190. module, (uint64)arg_i32);
  4191. }
  4192. }
  4193. break;
  4194. }
  4195. case VALUE_TYPE_I64:
  4196. #if WASM_ENABLE_MEMORY64 != 0
  4197. {
  4198. uint64 arg_i64;
  4199. PUT_I64_TO_ADDR((uint32 *)argv_dst,
  4200. GET_I64_FROM_ADDR(argv_src));
  4201. argv_src += 2;
  4202. arg_i64 = *argv_dst;
  4203. if (signature && is_memory64) {
  4204. /* TODO: memory64 pointer with length need a new symbol
  4205. * to represent type i64, with '~' still represent i32
  4206. * length */
  4207. if (signature[i + 1] == '*') {
  4208. /* param is a pointer */
  4209. if (signature[i + 2] == '~')
  4210. /* pointer with length followed */
  4211. ptr_len = *argv_src;
  4212. else
  4213. /* pointer without length followed */
  4214. ptr_len = 1;
  4215. if (!wasm_runtime_validate_app_addr(module, arg_i64,
  4216. (uint64)ptr_len))
  4217. goto fail;
  4218. *argv_dst = (uint64)wasm_runtime_addr_app_to_native(
  4219. module, arg_i64);
  4220. }
  4221. else if (signature[i + 1] == '$') {
  4222. /* param is a string */
  4223. if (!wasm_runtime_validate_app_str_addr(module,
  4224. arg_i64))
  4225. goto fail;
  4226. *argv_dst = (uint64)wasm_runtime_addr_app_to_native(
  4227. module, arg_i64);
  4228. }
  4229. }
  4230. break;
  4231. }
  4232. #endif
  4233. case VALUE_TYPE_F64:
  4234. bh_memcpy_s(argv_dst, sizeof(uint64), argv_src,
  4235. sizeof(uint32) * 2);
  4236. argv_src += 2;
  4237. break;
  4238. case VALUE_TYPE_F32:
  4239. *(float32 *)argv_dst = *(float32 *)argv_src++;
  4240. break;
  4241. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4242. case VALUE_TYPE_EXTERNREF:
  4243. {
  4244. uint32 externref_idx = *argv_src++;
  4245. void *externref_obj;
  4246. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  4247. goto fail;
  4248. bh_memcpy_s(argv_dst, sizeof(uintptr_t), argv_src,
  4249. sizeof(uintptr_t));
  4250. break;
  4251. }
  4252. #endif
  4253. #if WASM_ENABLE_GC != 0
  4254. case REF_TYPE_FUNCREF:
  4255. case REF_TYPE_EXTERNREF:
  4256. case REF_TYPE_ANYREF:
  4257. case REF_TYPE_EQREF:
  4258. case REF_TYPE_HT_NULLABLE:
  4259. case REF_TYPE_HT_NON_NULLABLE:
  4260. case REF_TYPE_I31REF:
  4261. case REF_TYPE_NULLFUNCREF:
  4262. case REF_TYPE_NULLEXTERNREF:
  4263. case REF_TYPE_STRUCTREF:
  4264. case REF_TYPE_ARRAYREF:
  4265. case REF_TYPE_NULLREF:
  4266. #if WASM_ENABLE_STRINGREF != 0
  4267. case REF_TYPE_STRINGREF:
  4268. case REF_TYPE_STRINGVIEWWTF8:
  4269. case REF_TYPE_STRINGVIEWWTF16:
  4270. case REF_TYPE_STRINGVIEWITER:
  4271. #endif
  4272. {
  4273. bh_memcpy_s(argv_dst, sizeof(uintptr_t), argv_src,
  4274. sizeof(uintptr_t));
  4275. argv_src += sizeof(uintptr_t) / sizeof(uint32);
  4276. break;
  4277. }
  4278. #endif
  4279. default:
  4280. bh_assert(0);
  4281. break;
  4282. }
  4283. }
  4284. exec_env->attachment = attachment;
  4285. invoke_native_raw(exec_env, argv1);
  4286. exec_env->attachment = NULL;
  4287. if (func_type->result_count > 0) {
  4288. switch (func_type->types[func_type->param_count]) {
  4289. case VALUE_TYPE_I32:
  4290. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4291. case VALUE_TYPE_FUNCREF:
  4292. #endif
  4293. argv_ret[0] = *(uint32 *)argv1;
  4294. break;
  4295. case VALUE_TYPE_F32:
  4296. *(float32 *)argv_ret = *(float32 *)argv1;
  4297. break;
  4298. case VALUE_TYPE_I64:
  4299. case VALUE_TYPE_F64:
  4300. bh_memcpy_s(argv_ret, sizeof(uint32) * 2, argv1,
  4301. sizeof(uint64));
  4302. break;
  4303. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4304. case VALUE_TYPE_EXTERNREF:
  4305. {
  4306. uint32 externref_idx;
  4307. uint64 externref_obj;
  4308. bh_memcpy_s(&externref_obj, sizeof(uint64), argv1,
  4309. sizeof(uint64));
  4310. if (!wasm_externref_obj2ref(exec_env->module_inst,
  4311. (void *)(uintptr_t)externref_obj,
  4312. &externref_idx))
  4313. goto fail;
  4314. argv_ret[0] = externref_idx;
  4315. break;
  4316. }
  4317. #endif
  4318. #if WASM_ENABLE_GC != 0
  4319. case REF_TYPE_FUNCREF:
  4320. case REF_TYPE_EXTERNREF:
  4321. case REF_TYPE_ANYREF:
  4322. case REF_TYPE_EQREF:
  4323. case REF_TYPE_HT_NULLABLE:
  4324. case REF_TYPE_HT_NON_NULLABLE:
  4325. case REF_TYPE_I31REF:
  4326. case REF_TYPE_NULLFUNCREF:
  4327. case REF_TYPE_NULLEXTERNREF:
  4328. case REF_TYPE_STRUCTREF:
  4329. case REF_TYPE_ARRAYREF:
  4330. case REF_TYPE_NULLREF:
  4331. #if WASM_ENABLE_STRINGREF != 0
  4332. case REF_TYPE_STRINGREF:
  4333. case REF_TYPE_STRINGVIEWWTF8:
  4334. case REF_TYPE_STRINGVIEWWTF16:
  4335. case REF_TYPE_STRINGVIEWITER:
  4336. #endif
  4337. {
  4338. bh_memcpy_s(argv_ret, sizeof(uintptr_t), argv1,
  4339. sizeof(uintptr_t));
  4340. break;
  4341. }
  4342. #endif
  4343. default:
  4344. bh_assert(0);
  4345. break;
  4346. }
  4347. }
  4348. ret = !wasm_runtime_copy_exception(module, NULL);
  4349. fail:
  4350. if (argv1 != argv_buf)
  4351. wasm_runtime_free(argv1);
  4352. return ret;
  4353. }
  4354. /**
  4355. * Implementation of wasm_runtime_invoke_native()
  4356. */
  4357. /**
  4358. * The invoke native implementation on ARM platform with VFP co-processor,
  4359. * RISCV32 platform with/without FPU/DPFPU and ARC platform.
  4360. */
  4361. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) \
  4362. || defined(BUILD_TARGET_RISCV32_ILP32D) \
  4363. || defined(BUILD_TARGET_RISCV32_ILP32F) \
  4364. || defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  4365. typedef void (*GenericFunctionPointer)(void);
  4366. void
  4367. invokeNative(GenericFunctionPointer f, uint32 *args, uint32 n_stacks);
  4368. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  4369. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  4370. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  4371. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  4372. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  4373. static volatile Float64FuncPtr invokeNative_Float64 =
  4374. (Float64FuncPtr)(uintptr_t)invokeNative;
  4375. static volatile Float32FuncPtr invokeNative_Float32 =
  4376. (Float32FuncPtr)(uintptr_t)invokeNative;
  4377. static volatile Int64FuncPtr invokeNative_Int64 =
  4378. (Int64FuncPtr)(uintptr_t)invokeNative;
  4379. static volatile Int32FuncPtr invokeNative_Int32 =
  4380. (Int32FuncPtr)(uintptr_t)invokeNative;
  4381. static volatile VoidFuncPtr invokeNative_Void =
  4382. (VoidFuncPtr)(uintptr_t)invokeNative;
  4383. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  4384. #define MAX_REG_INTS 4
  4385. #define MAX_REG_FLOATS 16
  4386. #else
  4387. #define MAX_REG_INTS 8
  4388. #define MAX_REG_FLOATS 8
  4389. #endif
  4390. bool
  4391. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  4392. const WASMFuncType *func_type, const char *signature,
  4393. void *attachment, uint32 *argv, uint32 argc,
  4394. uint32 *argv_ret)
  4395. {
  4396. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  4397. /* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args
  4398. */
  4399. uint32 argv_buf[32], *argv1 = argv_buf, *ints, *stacks, size;
  4400. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  4401. uint32 arg_i32, ptr_len;
  4402. uint32 result_count = func_type->result_count;
  4403. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  4404. bool ret = false;
  4405. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4406. bool is_aot_func = (NULL == signature);
  4407. #endif
  4408. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  4409. uint32 *fps;
  4410. int n_fps = 0;
  4411. #else
  4412. #define fps ints
  4413. #define n_fps n_ints
  4414. #endif
  4415. n_ints++; /* exec env */
  4416. /* Traverse firstly to calculate stack args count */
  4417. for (i = 0; i < func_type->param_count; i++) {
  4418. switch (func_type->types[i]) {
  4419. case VALUE_TYPE_I32:
  4420. #if WASM_ENABLE_GC != 0
  4421. case REF_TYPE_FUNCREF:
  4422. case REF_TYPE_EXTERNREF:
  4423. case REF_TYPE_ANYREF:
  4424. case REF_TYPE_EQREF:
  4425. case REF_TYPE_HT_NULLABLE:
  4426. case REF_TYPE_HT_NON_NULLABLE:
  4427. case REF_TYPE_I31REF:
  4428. case REF_TYPE_NULLFUNCREF:
  4429. case REF_TYPE_NULLEXTERNREF:
  4430. case REF_TYPE_STRUCTREF:
  4431. case REF_TYPE_ARRAYREF:
  4432. case REF_TYPE_NULLREF:
  4433. #if WASM_ENABLE_STRINGREF != 0
  4434. case REF_TYPE_STRINGREF:
  4435. case REF_TYPE_STRINGVIEWWTF8:
  4436. case REF_TYPE_STRINGVIEWWTF16:
  4437. case REF_TYPE_STRINGVIEWITER:
  4438. #endif
  4439. #endif
  4440. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4441. case VALUE_TYPE_FUNCREF:
  4442. case VALUE_TYPE_EXTERNREF:
  4443. #endif
  4444. if (n_ints < MAX_REG_INTS)
  4445. n_ints++;
  4446. else
  4447. n_stacks++;
  4448. break;
  4449. case VALUE_TYPE_I64:
  4450. if (n_ints < MAX_REG_INTS - 1) {
  4451. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  4452. /* 64-bit data must be 8 bytes aligned in arm */
  4453. if (n_ints & 1)
  4454. n_ints++;
  4455. #endif
  4456. n_ints += 2;
  4457. }
  4458. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  4459. || defined(BUILD_TARGET_RISCV32_ILP32F) \
  4460. || defined(BUILD_TARGET_RISCV32_ILP32D) || defined(BUILD_TARGET_ARC)
  4461. /* part in register, part in stack */
  4462. else if (n_ints == MAX_REG_INTS - 1) {
  4463. n_ints++;
  4464. n_stacks++;
  4465. }
  4466. #endif
  4467. else {
  4468. /* 64-bit data in stack must be 8 bytes aligned
  4469. in arm and riscv32 */
  4470. #if !defined(BUILD_TARGET_ARC)
  4471. if (n_stacks & 1)
  4472. n_stacks++;
  4473. #endif
  4474. n_stacks += 2;
  4475. }
  4476. break;
  4477. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  4478. case VALUE_TYPE_F32:
  4479. if (n_fps < MAX_REG_FLOATS)
  4480. n_fps++;
  4481. #if defined(BUILD_TARGET_RISCV32_ILP32F)
  4482. else if (n_ints < MAX_REG_INTS) {
  4483. n_ints++;
  4484. }
  4485. #endif
  4486. else
  4487. n_stacks++;
  4488. break;
  4489. case VALUE_TYPE_F64:
  4490. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  4491. || defined(BUILD_TARGET_RISCV32_ILP32F) || defined(BUILD_TARGET_ARC)
  4492. if (n_ints < MAX_REG_INTS - 1) {
  4493. n_ints += 2;
  4494. }
  4495. else if (n_ints == MAX_REG_INTS - 1) {
  4496. n_ints++;
  4497. n_stacks++;
  4498. }
  4499. #endif
  4500. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  4501. if (n_fps < MAX_REG_FLOATS - 1) {
  4502. /* 64-bit data must be 8 bytes aligned in arm */
  4503. if (n_fps & 1)
  4504. n_fps++;
  4505. n_fps += 2;
  4506. }
  4507. else if (n_fps == MAX_REG_FLOATS - 1) {
  4508. n_fps++;
  4509. n_stacks++;
  4510. }
  4511. #endif
  4512. else {
  4513. /* 64-bit data in stack must be 8 bytes aligned
  4514. in arm and riscv32 */
  4515. #if !defined(BUILD_TARGET_ARC)
  4516. if (n_stacks & 1)
  4517. n_stacks++;
  4518. #endif
  4519. n_stacks += 2;
  4520. }
  4521. break;
  4522. #else /* BUILD_TARGET_RISCV32_ILP32D */
  4523. case VALUE_TYPE_F32:
  4524. case VALUE_TYPE_F64:
  4525. if (n_fps < MAX_REG_FLOATS) {
  4526. n_fps++;
  4527. }
  4528. else if (func_type->types[i] == VALUE_TYPE_F32
  4529. && n_ints < MAX_REG_INTS) {
  4530. /* use int reg firstly if available */
  4531. n_ints++;
  4532. }
  4533. else if (func_type->types[i] == VALUE_TYPE_F64
  4534. && n_ints < MAX_REG_INTS - 1) {
  4535. /* use int regs firstly if available */
  4536. if (n_ints & 1)
  4537. n_ints++;
  4538. n_ints += 2;
  4539. }
  4540. else {
  4541. /* 64-bit data in stack must be 8 bytes aligned in riscv32
  4542. */
  4543. if (n_stacks & 1)
  4544. n_stacks++;
  4545. n_stacks += 2;
  4546. }
  4547. break;
  4548. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  4549. default:
  4550. bh_assert(0);
  4551. break;
  4552. }
  4553. }
  4554. for (i = 0; i < ext_ret_count; i++) {
  4555. if (n_ints < MAX_REG_INTS)
  4556. n_ints++;
  4557. else
  4558. n_stacks++;
  4559. }
  4560. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) \
  4561. || defined(BUILD_TARGET_RISCV32_ILP32F)
  4562. argc1 = MAX_REG_INTS + MAX_REG_FLOATS + n_stacks;
  4563. #elif defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  4564. argc1 = MAX_REG_INTS + n_stacks;
  4565. #else /* for BUILD_TARGET_RISCV32_ILP32D */
  4566. argc1 = MAX_REG_INTS + MAX_REG_FLOATS * 2 + n_stacks;
  4567. #endif
  4568. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  4569. size = sizeof(uint32) * (uint32)argc1;
  4570. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  4571. 0))) {
  4572. return false;
  4573. }
  4574. }
  4575. ints = argv1;
  4576. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) \
  4577. || defined(BUILD_TARGET_RISCV32_ILP32F)
  4578. fps = ints + MAX_REG_INTS;
  4579. stacks = fps + MAX_REG_FLOATS;
  4580. #elif defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  4581. stacks = ints + MAX_REG_INTS;
  4582. #else /* for BUILD_TARGET_RISCV32_ILP32D */
  4583. fps = ints + MAX_REG_INTS;
  4584. stacks = fps + MAX_REG_FLOATS * 2;
  4585. #endif
  4586. n_ints = 0;
  4587. n_fps = 0;
  4588. n_stacks = 0;
  4589. ints[n_ints++] = (uint32)(uintptr_t)exec_env;
  4590. /* Traverse secondly to fill in each argument */
  4591. for (i = 0; i < func_type->param_count; i++) {
  4592. switch (func_type->types[i]) {
  4593. case VALUE_TYPE_I32:
  4594. #if WASM_ENABLE_GC != 0
  4595. case REF_TYPE_FUNCREF:
  4596. case REF_TYPE_EXTERNREF:
  4597. case REF_TYPE_ANYREF:
  4598. case REF_TYPE_EQREF:
  4599. case REF_TYPE_HT_NULLABLE:
  4600. case REF_TYPE_HT_NON_NULLABLE:
  4601. case REF_TYPE_I31REF:
  4602. case REF_TYPE_NULLFUNCREF:
  4603. case REF_TYPE_NULLEXTERNREF:
  4604. case REF_TYPE_STRUCTREF:
  4605. case REF_TYPE_ARRAYREF:
  4606. case REF_TYPE_NULLREF:
  4607. #if WASM_ENABLE_STRINGREF != 0
  4608. case REF_TYPE_STRINGREF:
  4609. case REF_TYPE_STRINGVIEWWTF8:
  4610. case REF_TYPE_STRINGVIEWWTF16:
  4611. case REF_TYPE_STRINGVIEWITER:
  4612. #endif
  4613. #endif
  4614. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4615. case VALUE_TYPE_FUNCREF:
  4616. #endif
  4617. {
  4618. arg_i32 = *argv_src++;
  4619. if (signature) {
  4620. if (signature[i + 1] == '*') {
  4621. /* param is a pointer */
  4622. if (signature[i + 2] == '~')
  4623. /* pointer with length followed */
  4624. ptr_len = *argv_src;
  4625. else
  4626. /* pointer without length followed */
  4627. ptr_len = 1;
  4628. if (!wasm_runtime_validate_app_addr(
  4629. module, (uint64)arg_i32, (uint64)ptr_len))
  4630. goto fail;
  4631. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  4632. module, (uint64)arg_i32);
  4633. }
  4634. else if (signature[i + 1] == '$') {
  4635. /* param is a string */
  4636. if (!wasm_runtime_validate_app_str_addr(
  4637. module, (uint64)arg_i32))
  4638. goto fail;
  4639. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  4640. module, (uint64)arg_i32);
  4641. }
  4642. }
  4643. if (n_ints < MAX_REG_INTS)
  4644. ints[n_ints++] = arg_i32;
  4645. else
  4646. stacks[n_stacks++] = arg_i32;
  4647. break;
  4648. }
  4649. case VALUE_TYPE_I64:
  4650. {
  4651. if (n_ints < MAX_REG_INTS - 1) {
  4652. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  4653. /* 64-bit data must be 8 bytes aligned in arm */
  4654. if (n_ints & 1)
  4655. n_ints++;
  4656. #endif
  4657. ints[n_ints++] = *argv_src++;
  4658. ints[n_ints++] = *argv_src++;
  4659. }
  4660. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  4661. || defined(BUILD_TARGET_RISCV32_ILP32F) \
  4662. || defined(BUILD_TARGET_RISCV32_ILP32D) || defined(BUILD_TARGET_ARC)
  4663. else if (n_ints == MAX_REG_INTS - 1) {
  4664. ints[n_ints++] = *argv_src++;
  4665. stacks[n_stacks++] = *argv_src++;
  4666. }
  4667. #endif
  4668. else {
  4669. /* 64-bit data in stack must be 8 bytes aligned
  4670. in arm and riscv32 */
  4671. #if !defined(BUILD_TARGET_ARC)
  4672. if (n_stacks & 1)
  4673. n_stacks++;
  4674. #endif
  4675. stacks[n_stacks++] = *argv_src++;
  4676. stacks[n_stacks++] = *argv_src++;
  4677. }
  4678. break;
  4679. }
  4680. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  4681. case VALUE_TYPE_F32:
  4682. {
  4683. if (n_fps < MAX_REG_FLOATS)
  4684. *(float32 *)&fps[n_fps++] = *(float32 *)argv_src++;
  4685. #if defined(BUILD_TARGET_RISCV32_ILP32F)
  4686. else if (n_ints < MAX_REG_INTS) {
  4687. ints[n_ints++] = *argv_src++;
  4688. }
  4689. #endif
  4690. else
  4691. *(float32 *)&stacks[n_stacks++] = *(float32 *)argv_src++;
  4692. break;
  4693. }
  4694. case VALUE_TYPE_F64:
  4695. {
  4696. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  4697. || defined(BUILD_TARGET_RISCV32_ILP32F) || defined(BUILD_TARGET_ARC)
  4698. if (n_ints < MAX_REG_INTS - 1) {
  4699. ints[n_ints++] = *argv_src++;
  4700. ints[n_ints++] = *argv_src++;
  4701. }
  4702. else if (n_ints == MAX_REG_INTS - 1) {
  4703. ints[n_ints++] = *argv_src++;
  4704. stacks[n_stacks++] = *argv_src++;
  4705. }
  4706. #endif
  4707. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  4708. if (n_fps < MAX_REG_FLOATS - 1) {
  4709. /* 64-bit data must be 8 bytes aligned in arm */
  4710. if (n_fps & 1)
  4711. n_fps++;
  4712. fps[n_fps++] = *argv_src++;
  4713. fps[n_fps++] = *argv_src++;
  4714. }
  4715. else if (n_fps == MAX_REG_FLOATS - 1) {
  4716. fps[n_fps++] = *argv_src++;
  4717. stacks[n_stacks++] = *argv_src++;
  4718. }
  4719. #endif
  4720. else {
  4721. /* 64-bit data in stack must be 8 bytes aligned
  4722. in arm and riscv32 */
  4723. #if !defined(BUILD_TARGET_ARC)
  4724. if (n_stacks & 1)
  4725. n_stacks++;
  4726. #endif
  4727. stacks[n_stacks++] = *argv_src++;
  4728. stacks[n_stacks++] = *argv_src++;
  4729. }
  4730. break;
  4731. }
  4732. #else /* BUILD_TARGET_RISCV32_ILP32D */
  4733. case VALUE_TYPE_F32:
  4734. case VALUE_TYPE_F64:
  4735. {
  4736. if (n_fps < MAX_REG_FLOATS) {
  4737. if (func_type->types[i] == VALUE_TYPE_F32) {
  4738. *(float32 *)&fps[n_fps * 2] = *(float32 *)argv_src++;
  4739. /* NaN boxing, the upper bits of a valid NaN-boxed
  4740. value must be all 1s. */
  4741. fps[n_fps * 2 + 1] = 0xFFFFFFFF;
  4742. }
  4743. else {
  4744. *(float64 *)&fps[n_fps * 2] = *(float64 *)argv_src;
  4745. argv_src += 2;
  4746. }
  4747. n_fps++;
  4748. }
  4749. else if (func_type->types[i] == VALUE_TYPE_F32
  4750. && n_ints < MAX_REG_INTS) {
  4751. /* use int reg firstly if available */
  4752. *(float32 *)&ints[n_ints++] = *(float32 *)argv_src++;
  4753. }
  4754. else if (func_type->types[i] == VALUE_TYPE_F64
  4755. && n_ints < MAX_REG_INTS - 1) {
  4756. /* use int regs firstly if available */
  4757. if (n_ints & 1)
  4758. n_ints++;
  4759. *(float64 *)&ints[n_ints] = *(float64 *)argv_src;
  4760. n_ints += 2;
  4761. argv_src += 2;
  4762. }
  4763. else {
  4764. /* 64-bit data in stack must be 8 bytes aligned in riscv32
  4765. */
  4766. if (n_stacks & 1)
  4767. n_stacks++;
  4768. if (func_type->types[i] == VALUE_TYPE_F32) {
  4769. *(float32 *)&stacks[n_stacks++] =
  4770. *(float32 *)argv_src++;
  4771. }
  4772. else {
  4773. *(float64 *)&stacks[n_stacks] = *(float64 *)argv_src;
  4774. argv_src += 2;
  4775. n_stacks += 2;
  4776. }
  4777. }
  4778. break;
  4779. }
  4780. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  4781. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4782. case VALUE_TYPE_EXTERNREF:
  4783. {
  4784. uint32 externref_idx = *argv_src++;
  4785. if (is_aot_func) {
  4786. if (n_ints < MAX_REG_INTS)
  4787. ints[n_ints++] = externref_idx;
  4788. else
  4789. stacks[n_stacks++] = externref_idx;
  4790. }
  4791. else {
  4792. void *externref_obj;
  4793. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  4794. goto fail;
  4795. if (n_ints < MAX_REG_INTS)
  4796. ints[n_ints++] = (uintptr_t)externref_obj;
  4797. else
  4798. stacks[n_stacks++] = (uintptr_t)externref_obj;
  4799. }
  4800. break;
  4801. }
  4802. #endif
  4803. default:
  4804. bh_assert(0);
  4805. break;
  4806. }
  4807. }
  4808. /* Save extra result values' address to argv1 */
  4809. for (i = 0; i < ext_ret_count; i++) {
  4810. if (n_ints < MAX_REG_INTS)
  4811. ints[n_ints++] = *(uint32 *)argv_src++;
  4812. else
  4813. stacks[n_stacks++] = *(uint32 *)argv_src++;
  4814. }
  4815. exec_env->attachment = attachment;
  4816. if (func_type->result_count == 0) {
  4817. invokeNative_Void(func_ptr, argv1, n_stacks);
  4818. }
  4819. else {
  4820. switch (func_type->types[func_type->param_count]) {
  4821. case VALUE_TYPE_I32:
  4822. #if WASM_ENABLE_GC != 0
  4823. case REF_TYPE_FUNCREF:
  4824. case REF_TYPE_EXTERNREF:
  4825. case REF_TYPE_ANYREF:
  4826. case REF_TYPE_EQREF:
  4827. case REF_TYPE_HT_NULLABLE:
  4828. case REF_TYPE_HT_NON_NULLABLE:
  4829. case REF_TYPE_I31REF:
  4830. case REF_TYPE_NULLFUNCREF:
  4831. case REF_TYPE_NULLEXTERNREF:
  4832. case REF_TYPE_STRUCTREF:
  4833. case REF_TYPE_ARRAYREF:
  4834. case REF_TYPE_NULLREF:
  4835. #if WASM_ENABLE_STRINGREF != 0
  4836. case REF_TYPE_STRINGREF:
  4837. case REF_TYPE_STRINGVIEWWTF8:
  4838. case REF_TYPE_STRINGVIEWWTF16:
  4839. case REF_TYPE_STRINGVIEWITER:
  4840. #endif
  4841. #endif
  4842. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4843. case VALUE_TYPE_FUNCREF:
  4844. #endif
  4845. argv_ret[0] =
  4846. (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  4847. break;
  4848. case VALUE_TYPE_I64:
  4849. PUT_I64_TO_ADDR(argv_ret,
  4850. invokeNative_Int64(func_ptr, argv1, n_stacks));
  4851. break;
  4852. case VALUE_TYPE_F32:
  4853. *(float32 *)argv_ret =
  4854. invokeNative_Float32(func_ptr, argv1, n_stacks);
  4855. break;
  4856. case VALUE_TYPE_F64:
  4857. PUT_F64_TO_ADDR(
  4858. argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  4859. break;
  4860. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4861. case VALUE_TYPE_EXTERNREF:
  4862. {
  4863. if (is_aot_func) {
  4864. uint32 externref_idx =
  4865. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  4866. argv_ret[0] = externref_idx;
  4867. }
  4868. else {
  4869. uint32 externref_idx;
  4870. void *externref_obj;
  4871. externref_obj = (void *)(uintptr_t)invokeNative_Int32(
  4872. func_ptr, argv1, argc1);
  4873. if (!wasm_externref_obj2ref(exec_env->module_inst,
  4874. externref_obj, &externref_idx))
  4875. goto fail;
  4876. argv_ret[0] = externref_idx;
  4877. }
  4878. break;
  4879. }
  4880. #endif
  4881. default:
  4882. bh_assert(0);
  4883. break;
  4884. }
  4885. }
  4886. exec_env->attachment = NULL;
  4887. ret = !wasm_runtime_copy_exception(module, NULL);
  4888. fail:
  4889. if (argv1 != argv_buf)
  4890. wasm_runtime_free(argv1);
  4891. return ret;
  4892. }
  4893. #endif /* end of defined(BUILD_TARGET_ARM_VFP) \
  4894. || defined(BUILD_TARGET_THUMB_VFP) \
  4895. || defined(BUILD_TARGET_RISCV32_ILP32D) \
  4896. || defined(BUILD_TARGET_RISCV32_ILP32F) \
  4897. || defined(BUILD_TARGET_RISCV32_ILP32) \
  4898. || defined(BUILD_TARGET_ARC) */
  4899. #if defined(BUILD_TARGET_X86_32) || defined(BUILD_TARGET_ARM) \
  4900. || defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_MIPS) \
  4901. || defined(BUILD_TARGET_XTENSA)
  4902. typedef void (*GenericFunctionPointer)(void);
  4903. void
  4904. invokeNative(GenericFunctionPointer f, uint32 *args, uint32 sz);
  4905. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  4906. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  4907. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  4908. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  4909. typedef void (*VoidFuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  4910. static volatile Int64FuncPtr invokeNative_Int64 =
  4911. (Int64FuncPtr)(uintptr_t)invokeNative;
  4912. static volatile Int32FuncPtr invokeNative_Int32 =
  4913. (Int32FuncPtr)(uintptr_t)invokeNative;
  4914. static volatile Float64FuncPtr invokeNative_Float64 =
  4915. (Float64FuncPtr)(uintptr_t)invokeNative;
  4916. static volatile Float32FuncPtr invokeNative_Float32 =
  4917. (Float32FuncPtr)(uintptr_t)invokeNative;
  4918. static volatile VoidFuncPtr invokeNative_Void =
  4919. (VoidFuncPtr)(uintptr_t)invokeNative;
  4920. static inline void
  4921. word_copy(uint32 *dest, uint32 *src, unsigned num)
  4922. {
  4923. for (; num > 0; num--)
  4924. *dest++ = *src++;
  4925. }
  4926. bool
  4927. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  4928. const WASMFuncType *func_type, const char *signature,
  4929. void *attachment, uint32 *argv, uint32 argc,
  4930. uint32 *argv_ret)
  4931. {
  4932. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  4933. uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0;
  4934. uint32 arg_i32, ptr_len;
  4935. uint32 result_count = func_type->result_count;
  4936. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  4937. uint64 size;
  4938. bool ret = false;
  4939. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4940. bool is_aot_func = (NULL == signature);
  4941. #endif
  4942. #if defined(BUILD_TARGET_X86_32)
  4943. argc1 = argc + ext_ret_count + 2;
  4944. #else
  4945. /* arm/thumb/mips/xtensa, 64-bit data must be 8 bytes aligned,
  4946. so we need to allocate more memory. */
  4947. argc1 = func_type->param_count * 2 + ext_ret_count + 2;
  4948. #endif
  4949. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  4950. size = sizeof(uint32) * (uint64)argc1;
  4951. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  4952. 0))) {
  4953. return false;
  4954. }
  4955. }
  4956. for (i = 0; i < sizeof(WASMExecEnv *) / sizeof(uint32); i++)
  4957. argv1[j++] = ((uint32 *)&exec_env)[i];
  4958. for (i = 0; i < func_type->param_count; i++) {
  4959. switch (func_type->types[i]) {
  4960. case VALUE_TYPE_I32:
  4961. #if WASM_ENABLE_GC != 0
  4962. case REF_TYPE_FUNCREF:
  4963. case REF_TYPE_EXTERNREF:
  4964. case REF_TYPE_ANYREF:
  4965. case REF_TYPE_EQREF:
  4966. case REF_TYPE_HT_NULLABLE:
  4967. case REF_TYPE_HT_NON_NULLABLE:
  4968. case REF_TYPE_I31REF:
  4969. case REF_TYPE_NULLFUNCREF:
  4970. case REF_TYPE_NULLEXTERNREF:
  4971. case REF_TYPE_STRUCTREF:
  4972. case REF_TYPE_ARRAYREF:
  4973. case REF_TYPE_NULLREF:
  4974. #if WASM_ENABLE_STRINGREF != 0
  4975. case REF_TYPE_STRINGREF:
  4976. case REF_TYPE_STRINGVIEWWTF8:
  4977. case REF_TYPE_STRINGVIEWWTF16:
  4978. case REF_TYPE_STRINGVIEWITER:
  4979. #endif
  4980. #endif
  4981. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4982. case VALUE_TYPE_FUNCREF:
  4983. #endif
  4984. {
  4985. arg_i32 = *argv++;
  4986. if (signature) {
  4987. if (signature[i + 1] == '*') {
  4988. /* param is a pointer */
  4989. if (signature[i + 2] == '~')
  4990. /* pointer with length followed */
  4991. ptr_len = *argv;
  4992. else
  4993. /* pointer without length followed */
  4994. ptr_len = 1;
  4995. if (!wasm_runtime_validate_app_addr(
  4996. module, (uint64)arg_i32, (uint64)ptr_len))
  4997. goto fail;
  4998. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  4999. module, (uint64)arg_i32);
  5000. }
  5001. else if (signature[i + 1] == '$') {
  5002. /* param is a string */
  5003. if (!wasm_runtime_validate_app_str_addr(
  5004. module, (uint64)arg_i32))
  5005. goto fail;
  5006. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  5007. module, (uint64)arg_i32);
  5008. }
  5009. }
  5010. argv1[j++] = arg_i32;
  5011. break;
  5012. }
  5013. case VALUE_TYPE_I64:
  5014. case VALUE_TYPE_F64:
  5015. #if !defined(BUILD_TARGET_X86_32)
  5016. /* 64-bit data must be 8 bytes aligned in arm, thumb, mips
  5017. and xtensa */
  5018. if (j & 1)
  5019. j++;
  5020. #endif
  5021. argv1[j++] = *argv++;
  5022. argv1[j++] = *argv++;
  5023. break;
  5024. case VALUE_TYPE_F32:
  5025. argv1[j++] = *argv++;
  5026. break;
  5027. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5028. case VALUE_TYPE_EXTERNREF:
  5029. {
  5030. uint32 externref_idx = *argv++;
  5031. if (is_aot_func) {
  5032. argv1[j++] = externref_idx;
  5033. }
  5034. else {
  5035. void *externref_obj;
  5036. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  5037. goto fail;
  5038. argv1[j++] = (uintptr_t)externref_obj;
  5039. }
  5040. break;
  5041. }
  5042. #endif
  5043. default:
  5044. bh_assert(0);
  5045. break;
  5046. }
  5047. }
  5048. /* Save extra result values' address to argv1 */
  5049. word_copy(argv1 + j, argv, ext_ret_count);
  5050. argc1 = j + ext_ret_count;
  5051. exec_env->attachment = attachment;
  5052. if (func_type->result_count == 0) {
  5053. invokeNative_Void(func_ptr, argv1, argc1);
  5054. }
  5055. else {
  5056. switch (func_type->types[func_type->param_count]) {
  5057. case VALUE_TYPE_I32:
  5058. #if WASM_ENABLE_GC != 0
  5059. case REF_TYPE_FUNCREF:
  5060. case REF_TYPE_EXTERNREF:
  5061. case REF_TYPE_ANYREF:
  5062. case REF_TYPE_EQREF:
  5063. case REF_TYPE_HT_NULLABLE:
  5064. case REF_TYPE_HT_NON_NULLABLE:
  5065. case REF_TYPE_I31REF:
  5066. case REF_TYPE_NULLFUNCREF:
  5067. case REF_TYPE_NULLEXTERNREF:
  5068. case REF_TYPE_STRUCTREF:
  5069. case REF_TYPE_ARRAYREF:
  5070. case REF_TYPE_NULLREF:
  5071. #if WASM_ENABLE_STRINGREF != 0
  5072. case REF_TYPE_STRINGREF:
  5073. case REF_TYPE_STRINGVIEWWTF8:
  5074. case REF_TYPE_STRINGVIEWWTF16:
  5075. case REF_TYPE_STRINGVIEWITER:
  5076. #endif
  5077. #endif
  5078. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5079. case VALUE_TYPE_FUNCREF:
  5080. #endif
  5081. argv_ret[0] =
  5082. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  5083. break;
  5084. case VALUE_TYPE_I64:
  5085. PUT_I64_TO_ADDR(argv_ret,
  5086. invokeNative_Int64(func_ptr, argv1, argc1));
  5087. break;
  5088. case VALUE_TYPE_F32:
  5089. *(float32 *)argv_ret =
  5090. invokeNative_Float32(func_ptr, argv1, argc1);
  5091. break;
  5092. case VALUE_TYPE_F64:
  5093. PUT_F64_TO_ADDR(argv_ret,
  5094. invokeNative_Float64(func_ptr, argv1, argc1));
  5095. break;
  5096. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5097. case VALUE_TYPE_EXTERNREF:
  5098. {
  5099. if (is_aot_func) {
  5100. uint32 externref_idx =
  5101. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  5102. argv_ret[0] = externref_idx;
  5103. }
  5104. else {
  5105. void *externref_obj = (void *)(uintptr_t)invokeNative_Int32(
  5106. func_ptr, argv1, argc1);
  5107. uint32 externref_idx;
  5108. if (!wasm_externref_obj2ref(exec_env->module_inst,
  5109. externref_obj, &externref_idx))
  5110. goto fail;
  5111. argv_ret[0] = externref_idx;
  5112. }
  5113. break;
  5114. }
  5115. #endif
  5116. default:
  5117. bh_assert(0);
  5118. break;
  5119. }
  5120. }
  5121. exec_env->attachment = NULL;
  5122. ret = !wasm_runtime_copy_exception(module, NULL);
  5123. fail:
  5124. if (argv1 != argv_buf)
  5125. wasm_runtime_free(argv1);
  5126. return ret;
  5127. }
  5128. #endif /* end of defined(BUILD_TARGET_X86_32) \
  5129. || defined(BUILD_TARGET_ARM) \
  5130. || defined(BUILD_TARGET_THUMB) \
  5131. || defined(BUILD_TARGET_MIPS) \
  5132. || defined(BUILD_TARGET_XTENSA) */
  5133. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  5134. || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
  5135. || defined(BUILD_TARGET_RISCV64_LP64)
  5136. #if WASM_ENABLE_SIMD != 0
  5137. #ifdef v128
  5138. #undef v128
  5139. #endif
  5140. #if defined(_WIN32) || defined(_WIN32_)
  5141. typedef union __declspec(intrin_type) __declspec(align(8)) v128 {
  5142. __int8 m128i_i8[16];
  5143. __int16 m128i_i16[8];
  5144. __int32 m128i_i32[4];
  5145. __int64 m128i_i64[2];
  5146. unsigned __int8 m128i_u8[16];
  5147. unsigned __int16 m128i_u16[8];
  5148. unsigned __int32 m128i_u32[4];
  5149. unsigned __int64 m128i_u64[2];
  5150. } v128;
  5151. #elif defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  5152. || defined(BUILD_TARGET_RISCV64_LP64D) \
  5153. || defined(BUILD_TARGET_RISCV64_LP64)
  5154. typedef long long v128
  5155. __attribute__((__vector_size__(16), __may_alias__, __aligned__(1)));
  5156. #elif defined(BUILD_TARGET_AARCH64)
  5157. #include <arm_neon.h>
  5158. typedef uint32x4_t __m128i;
  5159. #define v128 __m128i
  5160. #endif
  5161. #endif /* end of WASM_ENABLE_SIMD != 0 */
  5162. typedef void (*GenericFunctionPointer)(void);
  5163. void
  5164. invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks);
  5165. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  5166. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  5167. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  5168. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  5169. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  5170. /* NOLINTBEGIN */
  5171. static volatile Float64FuncPtr invokeNative_Float64 =
  5172. (Float64FuncPtr)(uintptr_t)invokeNative;
  5173. static volatile Float32FuncPtr invokeNative_Float32 =
  5174. (Float32FuncPtr)(uintptr_t)invokeNative;
  5175. static volatile Int64FuncPtr invokeNative_Int64 =
  5176. (Int64FuncPtr)(uintptr_t)invokeNative;
  5177. static volatile Int32FuncPtr invokeNative_Int32 =
  5178. (Int32FuncPtr)(uintptr_t)invokeNative;
  5179. static volatile VoidFuncPtr invokeNative_Void =
  5180. (VoidFuncPtr)(uintptr_t)invokeNative;
  5181. #if WASM_ENABLE_SIMD != 0
  5182. typedef v128 (*V128FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  5183. static V128FuncPtr invokeNative_V128 = (V128FuncPtr)(uintptr_t)invokeNative;
  5184. #endif
  5185. /* NOLINTEND */
  5186. #if defined(_WIN32) || defined(_WIN32_)
  5187. #define MAX_REG_FLOATS 4
  5188. #define MAX_REG_INTS 4
  5189. #else /* else of defined(_WIN32) || defined(_WIN32_) */
  5190. #define MAX_REG_FLOATS 8
  5191. #if defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
  5192. || defined(BUILD_TARGET_RISCV64_LP64)
  5193. #define MAX_REG_INTS 8
  5194. #else
  5195. #define MAX_REG_INTS 6
  5196. #endif /* end of defined(BUILD_TARGET_AARCH64) \
  5197. || defined(BUILD_TARGET_RISCV64_LP64D) \
  5198. || defined(BUILD_TARGET_RISCV64_LP64) */
  5199. #endif /* end of defined(_WIN32) || defined(_WIN32_) */
  5200. /*
  5201. * ASAN is not designed to work with custom stack unwind or other low-level
  5202. * things. Ignore a function that does some low-level magic. (e.g. walking
  5203. * through the thread's stack bypassing the frame boundaries)
  5204. */
  5205. #if defined(__GNUC__) || defined(__clang__)
  5206. __attribute__((no_sanitize_address))
  5207. #endif
  5208. bool
  5209. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  5210. const WASMFuncType *func_type, const char *signature,
  5211. void *attachment, uint32 *argv, uint32 argc,
  5212. uint32 *argv_ret)
  5213. {
  5214. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  5215. #if WASM_ENABLE_MEMORY64 != 0
  5216. WASMMemoryInstance *memory =
  5217. wasm_get_default_memory((WASMModuleInstance *)module);
  5218. bool is_memory64 = memory ? memory->is_memory64 : false;
  5219. #endif
  5220. uint64 argv_buf[32] = { 0 }, *argv1 = argv_buf, *ints, *stacks, size,
  5221. arg_i64;
  5222. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  5223. uint32 arg_i32, ptr_len;
  5224. uint32 result_count = func_type->result_count;
  5225. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  5226. bool ret = false;
  5227. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5228. bool is_aot_func = (NULL == signature);
  5229. #endif
  5230. #ifndef BUILD_TARGET_RISCV64_LP64
  5231. #if WASM_ENABLE_SIMD == 0
  5232. uint64 *fps;
  5233. #else
  5234. v128 *fps;
  5235. #endif
  5236. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  5237. #define fps ints
  5238. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  5239. #if defined(_WIN32) || defined(_WIN32_) || defined(BUILD_TARGET_RISCV64_LP64)
  5240. /* important difference in calling conventions */
  5241. #define n_fps n_ints
  5242. #else
  5243. int n_fps = 0;
  5244. #endif
  5245. #if WASM_ENABLE_SIMD == 0
  5246. argc1 = 1 + MAX_REG_FLOATS + (uint32)func_type->param_count + ext_ret_count;
  5247. #else
  5248. argc1 = 1 + MAX_REG_FLOATS * 2 + (uint32)func_type->param_count * 2
  5249. + ext_ret_count;
  5250. #endif
  5251. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  5252. size = sizeof(uint64) * (uint64)argc1;
  5253. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  5254. 0))) {
  5255. return false;
  5256. }
  5257. }
  5258. #ifndef BUILD_TARGET_RISCV64_LP64
  5259. #if WASM_ENABLE_SIMD == 0
  5260. fps = argv1;
  5261. ints = fps + MAX_REG_FLOATS;
  5262. #else
  5263. fps = (v128 *)argv1;
  5264. ints = (uint64 *)(fps + MAX_REG_FLOATS);
  5265. #endif
  5266. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  5267. ints = argv1;
  5268. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  5269. stacks = ints + MAX_REG_INTS;
  5270. ints[n_ints++] = (uint64)(uintptr_t)exec_env;
  5271. for (i = 0; i < func_type->param_count; i++) {
  5272. switch (func_type->types[i]) {
  5273. case VALUE_TYPE_I32:
  5274. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5275. case VALUE_TYPE_FUNCREF:
  5276. #endif
  5277. {
  5278. arg_i32 = *argv_src++;
  5279. arg_i64 = arg_i32;
  5280. if (signature
  5281. #if WASM_ENABLE_MEMORY64 != 0
  5282. && !is_memory64
  5283. #endif
  5284. ) {
  5285. if (signature[i + 1] == '*') {
  5286. /* param is a pointer */
  5287. if (signature[i + 2] == '~')
  5288. /* pointer with length followed */
  5289. ptr_len = *argv_src;
  5290. else
  5291. /* pointer without length followed */
  5292. ptr_len = 1;
  5293. if (!wasm_runtime_validate_app_addr(
  5294. module, (uint64)arg_i32, (uint64)ptr_len))
  5295. goto fail;
  5296. arg_i64 = (uintptr_t)wasm_runtime_addr_app_to_native(
  5297. module, (uint64)arg_i32);
  5298. }
  5299. else if (signature[i + 1] == '$') {
  5300. /* param is a string */
  5301. if (!wasm_runtime_validate_app_str_addr(
  5302. module, (uint64)arg_i32))
  5303. goto fail;
  5304. arg_i64 = (uintptr_t)wasm_runtime_addr_app_to_native(
  5305. module, (uint64)arg_i32);
  5306. }
  5307. }
  5308. if (n_ints < MAX_REG_INTS)
  5309. ints[n_ints++] = arg_i64;
  5310. else
  5311. stacks[n_stacks++] = arg_i64;
  5312. break;
  5313. }
  5314. case VALUE_TYPE_I64:
  5315. #if WASM_ENABLE_MEMORY64 != 0
  5316. {
  5317. arg_i64 = GET_I64_FROM_ADDR(argv_src);
  5318. argv_src += 2;
  5319. if (signature && is_memory64) {
  5320. /* TODO: memory64 pointer with length need a new symbol
  5321. * to represent type i64, with '~' still represent i32
  5322. * length */
  5323. if (signature[i + 1] == '*') {
  5324. /* param is a pointer */
  5325. if (signature[i + 2] == '~')
  5326. /* pointer with length followed */
  5327. ptr_len = *argv_src;
  5328. else
  5329. /* pointer without length followed */
  5330. ptr_len = 1;
  5331. if (!wasm_runtime_validate_app_addr(module, arg_i64,
  5332. (uint64)ptr_len))
  5333. goto fail;
  5334. arg_i64 = (uint64)wasm_runtime_addr_app_to_native(
  5335. module, arg_i64);
  5336. }
  5337. else if (signature[i + 1] == '$') {
  5338. /* param is a string */
  5339. if (!wasm_runtime_validate_app_str_addr(module,
  5340. arg_i64))
  5341. goto fail;
  5342. arg_i64 = (uint64)wasm_runtime_addr_app_to_native(
  5343. module, arg_i64);
  5344. }
  5345. }
  5346. if (n_ints < MAX_REG_INTS)
  5347. ints[n_ints++] = arg_i64;
  5348. else
  5349. stacks[n_stacks++] = arg_i64;
  5350. break;
  5351. }
  5352. #endif
  5353. #if WASM_ENABLE_GC != 0
  5354. case REF_TYPE_FUNCREF:
  5355. case REF_TYPE_EXTERNREF:
  5356. case REF_TYPE_ANYREF:
  5357. case REF_TYPE_EQREF:
  5358. case REF_TYPE_HT_NULLABLE:
  5359. case REF_TYPE_HT_NON_NULLABLE:
  5360. case REF_TYPE_I31REF:
  5361. case REF_TYPE_NULLFUNCREF:
  5362. case REF_TYPE_NULLEXTERNREF:
  5363. case REF_TYPE_STRUCTREF:
  5364. case REF_TYPE_ARRAYREF:
  5365. case REF_TYPE_NULLREF:
  5366. #if WASM_ENABLE_STRINGREF != 0
  5367. case REF_TYPE_STRINGREF:
  5368. case REF_TYPE_STRINGVIEWWTF8:
  5369. case REF_TYPE_STRINGVIEWWTF16:
  5370. case REF_TYPE_STRINGVIEWITER:
  5371. #endif
  5372. #endif
  5373. if (n_ints < MAX_REG_INTS)
  5374. ints[n_ints++] = *(uint64 *)argv_src;
  5375. else
  5376. stacks[n_stacks++] = *(uint64 *)argv_src;
  5377. argv_src += 2;
  5378. break;
  5379. case VALUE_TYPE_F32:
  5380. if (n_fps < MAX_REG_FLOATS) {
  5381. *(float32 *)&fps[n_fps++] = *(float32 *)argv_src++;
  5382. }
  5383. else {
  5384. *(float32 *)&stacks[n_stacks++] = *(float32 *)argv_src++;
  5385. }
  5386. break;
  5387. case VALUE_TYPE_F64:
  5388. if (n_fps < MAX_REG_FLOATS) {
  5389. *(float64 *)&fps[n_fps++] = *(float64 *)argv_src;
  5390. }
  5391. else {
  5392. *(float64 *)&stacks[n_stacks++] = *(float64 *)argv_src;
  5393. }
  5394. argv_src += 2;
  5395. break;
  5396. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5397. case VALUE_TYPE_EXTERNREF:
  5398. {
  5399. uint32 externref_idx = *argv_src++;
  5400. if (is_aot_func) {
  5401. if (n_ints < MAX_REG_INTS)
  5402. ints[n_ints++] = externref_idx;
  5403. else
  5404. stacks[n_stacks++] = externref_idx;
  5405. }
  5406. else {
  5407. void *externref_obj;
  5408. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  5409. goto fail;
  5410. if (n_ints < MAX_REG_INTS)
  5411. ints[n_ints++] = (uintptr_t)externref_obj;
  5412. else
  5413. stacks[n_stacks++] = (uintptr_t)externref_obj;
  5414. }
  5415. break;
  5416. }
  5417. #endif
  5418. #if WASM_ENABLE_SIMD != 0
  5419. case VALUE_TYPE_V128:
  5420. if (n_fps < MAX_REG_FLOATS) {
  5421. *(v128 *)&fps[n_fps++] = *(v128 *)argv_src;
  5422. }
  5423. else {
  5424. *(v128 *)&stacks[n_stacks++] = *(v128 *)argv_src;
  5425. n_stacks++;
  5426. }
  5427. argv_src += 4;
  5428. break;
  5429. #endif
  5430. default:
  5431. bh_assert(0);
  5432. break;
  5433. }
  5434. }
  5435. /* Save extra result values' address to argv1 */
  5436. for (i = 0; i < ext_ret_count; i++) {
  5437. if (n_ints < MAX_REG_INTS)
  5438. ints[n_ints++] = *(uint64 *)argv_src;
  5439. else
  5440. stacks[n_stacks++] = *(uint64 *)argv_src;
  5441. argv_src += 2;
  5442. }
  5443. exec_env->attachment = attachment;
  5444. if (result_count == 0) {
  5445. invokeNative_Void(func_ptr, argv1, n_stacks);
  5446. }
  5447. else {
  5448. /* Invoke the native function and get the first result value */
  5449. switch (func_type->types[func_type->param_count]) {
  5450. case VALUE_TYPE_I32:
  5451. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5452. case VALUE_TYPE_FUNCREF:
  5453. #endif
  5454. argv_ret[0] =
  5455. (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  5456. break;
  5457. case VALUE_TYPE_I64:
  5458. #if WASM_ENABLE_GC != 0
  5459. case REF_TYPE_FUNCREF:
  5460. case REF_TYPE_EXTERNREF:
  5461. case REF_TYPE_ANYREF:
  5462. case REF_TYPE_EQREF:
  5463. case REF_TYPE_HT_NULLABLE:
  5464. case REF_TYPE_HT_NON_NULLABLE:
  5465. case REF_TYPE_I31REF:
  5466. case REF_TYPE_NULLFUNCREF:
  5467. case REF_TYPE_NULLEXTERNREF:
  5468. case REF_TYPE_STRUCTREF:
  5469. case REF_TYPE_ARRAYREF:
  5470. case REF_TYPE_NULLREF:
  5471. #if WASM_ENABLE_STRINGREF != 0
  5472. case REF_TYPE_STRINGREF:
  5473. case REF_TYPE_STRINGVIEWWTF8:
  5474. case REF_TYPE_STRINGVIEWWTF16:
  5475. case REF_TYPE_STRINGVIEWITER:
  5476. #endif
  5477. #endif
  5478. PUT_I64_TO_ADDR(argv_ret,
  5479. invokeNative_Int64(func_ptr, argv1, n_stacks));
  5480. break;
  5481. case VALUE_TYPE_F32:
  5482. *(float32 *)argv_ret =
  5483. invokeNative_Float32(func_ptr, argv1, n_stacks);
  5484. break;
  5485. case VALUE_TYPE_F64:
  5486. PUT_F64_TO_ADDR(
  5487. argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  5488. break;
  5489. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5490. case VALUE_TYPE_EXTERNREF:
  5491. {
  5492. if (is_aot_func) {
  5493. argv_ret[0] = invokeNative_Int32(func_ptr, argv1, n_stacks);
  5494. }
  5495. else {
  5496. uint32 externref_idx;
  5497. void *externref_obj = (void *)(uintptr_t)invokeNative_Int64(
  5498. func_ptr, argv1, n_stacks);
  5499. if (!wasm_externref_obj2ref(exec_env->module_inst,
  5500. externref_obj, &externref_idx))
  5501. goto fail;
  5502. argv_ret[0] = externref_idx;
  5503. }
  5504. break;
  5505. }
  5506. #endif
  5507. #if WASM_ENABLE_SIMD != 0
  5508. case VALUE_TYPE_V128:
  5509. *(v128 *)argv_ret =
  5510. invokeNative_V128(func_ptr, argv1, n_stacks);
  5511. break;
  5512. #endif
  5513. default:
  5514. bh_assert(0);
  5515. break;
  5516. }
  5517. }
  5518. exec_env->attachment = NULL;
  5519. ret = !wasm_runtime_copy_exception(module, NULL);
  5520. fail:
  5521. if (argv1 != argv_buf)
  5522. wasm_runtime_free(argv1);
  5523. return ret;
  5524. }
  5525. #endif /* end of defined(BUILD_TARGET_X86_64) \
  5526. || defined(BUILD_TARGET_AMD_64) \
  5527. || defined(BUILD_TARGET_AARCH64) \
  5528. || defined(BUILD_TARGET_RISCV64_LP64D) \
  5529. || defined(BUILD_TARGET_RISCV64_LP64) */
  5530. bool
  5531. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index,
  5532. uint32 argc, uint32 argv[])
  5533. {
  5534. bool ret = false;
  5535. if (!wasm_runtime_exec_env_check(exec_env)) {
  5536. LOG_ERROR("Invalid exec env stack info.");
  5537. return false;
  5538. }
  5539. /* this function is called from native code, so exec_env->handle and
  5540. exec_env->native_stack_boundary must have been set, we don't set
  5541. it again */
  5542. #if WASM_ENABLE_INTERP != 0
  5543. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  5544. ret = wasm_call_indirect(exec_env, 0, element_index, argc, argv);
  5545. #endif
  5546. #if WASM_ENABLE_AOT != 0
  5547. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  5548. ret = aot_call_indirect(exec_env, 0, element_index, argc, argv);
  5549. #endif
  5550. return ret;
  5551. }
  5552. static void
  5553. exchange_uint32(uint8 *p_data)
  5554. {
  5555. uint8 value = *p_data;
  5556. *p_data = *(p_data + 3);
  5557. *(p_data + 3) = value;
  5558. value = *(p_data + 1);
  5559. *(p_data + 1) = *(p_data + 2);
  5560. *(p_data + 2) = value;
  5561. }
  5562. static void
  5563. exchange_uint64(uint8 *p_data)
  5564. {
  5565. uint32 value;
  5566. value = *(uint32 *)p_data;
  5567. *(uint32 *)p_data = *(uint32 *)(p_data + 4);
  5568. *(uint32 *)(p_data + 4) = value;
  5569. exchange_uint32(p_data);
  5570. exchange_uint32(p_data + 4);
  5571. }
  5572. void
  5573. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2)
  5574. {
  5575. uint64 u1, u2;
  5576. bh_memcpy_s(&u1, 8, bytes, 8);
  5577. bh_memcpy_s(&u2, 8, bytes + 8, 8);
  5578. if (!is_little_endian()) {
  5579. exchange_uint64((uint8 *)&u1);
  5580. exchange_uint64((uint8 *)&u2);
  5581. *ret1 = u2;
  5582. *ret2 = u1;
  5583. }
  5584. else {
  5585. *ret1 = u1;
  5586. *ret2 = u2;
  5587. }
  5588. }
  5589. #if WASM_ENABLE_THREAD_MGR != 0
  5590. typedef struct WASMThreadArg {
  5591. WASMExecEnv *new_exec_env;
  5592. wasm_thread_callback_t callback;
  5593. void *arg;
  5594. } WASMThreadArg;
  5595. WASMExecEnv *
  5596. wasm_runtime_spawn_exec_env(WASMExecEnv *exec_env)
  5597. {
  5598. return wasm_cluster_spawn_exec_env(exec_env);
  5599. }
  5600. void
  5601. wasm_runtime_destroy_spawned_exec_env(WASMExecEnv *exec_env)
  5602. {
  5603. wasm_cluster_destroy_spawned_exec_env(exec_env);
  5604. }
  5605. static void *
  5606. wasm_runtime_thread_routine(void *arg)
  5607. {
  5608. WASMThreadArg *thread_arg = (WASMThreadArg *)arg;
  5609. void *ret;
  5610. bh_assert(thread_arg->new_exec_env);
  5611. ret = thread_arg->callback(thread_arg->new_exec_env, thread_arg->arg);
  5612. wasm_runtime_destroy_spawned_exec_env(thread_arg->new_exec_env);
  5613. wasm_runtime_free(thread_arg);
  5614. os_thread_exit(ret);
  5615. return ret;
  5616. }
  5617. int32
  5618. wasm_runtime_spawn_thread(WASMExecEnv *exec_env, wasm_thread_t *tid,
  5619. wasm_thread_callback_t callback, void *arg)
  5620. {
  5621. WASMExecEnv *new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
  5622. WASMThreadArg *thread_arg;
  5623. int32 ret;
  5624. if (!new_exec_env)
  5625. return -1;
  5626. if (!(thread_arg = wasm_runtime_malloc(sizeof(WASMThreadArg)))) {
  5627. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  5628. return -1;
  5629. }
  5630. thread_arg->new_exec_env = new_exec_env;
  5631. thread_arg->callback = callback;
  5632. thread_arg->arg = arg;
  5633. ret = os_thread_create((korp_tid *)tid, wasm_runtime_thread_routine,
  5634. thread_arg, APP_THREAD_STACK_SIZE_DEFAULT);
  5635. if (ret != 0) {
  5636. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  5637. wasm_runtime_free(thread_arg);
  5638. }
  5639. return ret;
  5640. }
  5641. int32
  5642. wasm_runtime_join_thread(wasm_thread_t tid, void **retval)
  5643. {
  5644. return os_thread_join((korp_tid)tid, retval);
  5645. }
  5646. #endif /* end of WASM_ENABLE_THREAD_MGR */
  5647. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5648. static korp_mutex externref_lock;
  5649. static uint32 externref_global_id = 1;
  5650. static HashMap *externref_map;
  5651. typedef struct ExternRefMapNode {
  5652. /* The extern object from runtime embedder */
  5653. void *extern_obj;
  5654. /* The module instance it belongs to */
  5655. WASMModuleInstanceCommon *module_inst;
  5656. /* Whether it is retained */
  5657. bool retained;
  5658. /* Whether it is marked by runtime */
  5659. bool marked;
  5660. /* cleanup function called when the externref is freed */
  5661. void (*cleanup)(void *);
  5662. } ExternRefMapNode;
  5663. static uint32
  5664. wasm_externref_hash(const void *key)
  5665. {
  5666. uint32 externref_idx = (uint32)(uintptr_t)key;
  5667. return externref_idx;
  5668. }
  5669. static bool
  5670. wasm_externref_equal(void *key1, void *key2)
  5671. {
  5672. uint32 externref_idx1 = (uint32)(uintptr_t)key1;
  5673. uint32 externref_idx2 = (uint32)(uintptr_t)key2;
  5674. return externref_idx1 == externref_idx2 ? true : false;
  5675. }
  5676. static bool
  5677. wasm_externref_map_init()
  5678. {
  5679. if (os_mutex_init(&externref_lock) != 0)
  5680. return false;
  5681. if (!(externref_map = bh_hash_map_create(32, false, wasm_externref_hash,
  5682. wasm_externref_equal, NULL,
  5683. wasm_runtime_free))) {
  5684. os_mutex_destroy(&externref_lock);
  5685. return false;
  5686. }
  5687. externref_global_id = 1;
  5688. return true;
  5689. }
  5690. static void
  5691. wasm_externref_map_destroy()
  5692. {
  5693. bh_hash_map_destroy(externref_map);
  5694. os_mutex_destroy(&externref_lock);
  5695. }
  5696. typedef struct LookupExtObj_UserData {
  5697. ExternRefMapNode node;
  5698. bool found;
  5699. uint32 externref_idx;
  5700. } LookupExtObj_UserData;
  5701. static void
  5702. lookup_extobj_callback(void *key, void *value, void *user_data)
  5703. {
  5704. uint32 externref_idx = (uint32)(uintptr_t)key;
  5705. ExternRefMapNode *node = (ExternRefMapNode *)value;
  5706. LookupExtObj_UserData *user_data_lookup =
  5707. (LookupExtObj_UserData *)user_data;
  5708. if (node->extern_obj == user_data_lookup->node.extern_obj
  5709. && node->module_inst == user_data_lookup->node.module_inst) {
  5710. user_data_lookup->found = true;
  5711. user_data_lookup->externref_idx = externref_idx;
  5712. }
  5713. }
  5714. static void
  5715. delete_externref(void *key, ExternRefMapNode *node)
  5716. {
  5717. bh_hash_map_remove(externref_map, key, NULL, NULL);
  5718. if (node->cleanup) {
  5719. (*node->cleanup)(node->extern_obj);
  5720. }
  5721. wasm_runtime_free(node);
  5722. }
  5723. static void
  5724. delete_extobj_callback(void *key, void *value, void *user_data)
  5725. {
  5726. ExternRefMapNode *node = (ExternRefMapNode *)value;
  5727. LookupExtObj_UserData *lookup_user_data =
  5728. (LookupExtObj_UserData *)user_data;
  5729. if (node->extern_obj == lookup_user_data->node.extern_obj
  5730. && node->module_inst == lookup_user_data->node.module_inst) {
  5731. lookup_user_data->found = true;
  5732. delete_externref(key, node);
  5733. }
  5734. }
  5735. bool
  5736. wasm_externref_objdel(WASMModuleInstanceCommon *module_inst, void *extern_obj)
  5737. {
  5738. LookupExtObj_UserData lookup_user_data = { 0 };
  5739. bool ok = false;
  5740. /* in a wrapper, extern_obj could be any value */
  5741. lookup_user_data.node.extern_obj = extern_obj;
  5742. lookup_user_data.node.module_inst = module_inst;
  5743. lookup_user_data.found = false;
  5744. os_mutex_lock(&externref_lock);
  5745. /* Lookup hashmap firstly */
  5746. bh_hash_map_traverse(externref_map, delete_extobj_callback,
  5747. (void *)&lookup_user_data);
  5748. if (lookup_user_data.found) {
  5749. ok = true;
  5750. }
  5751. os_mutex_unlock(&externref_lock);
  5752. return ok;
  5753. }
  5754. bool
  5755. wasm_externref_set_cleanup(WASMModuleInstanceCommon *module_inst,
  5756. void *extern_obj, void (*extern_obj_cleanup)(void *))
  5757. {
  5758. LookupExtObj_UserData lookup_user_data = { 0 };
  5759. bool ok = false;
  5760. /* in a wrapper, extern_obj could be any value */
  5761. lookup_user_data.node.extern_obj = extern_obj;
  5762. lookup_user_data.node.module_inst = module_inst;
  5763. lookup_user_data.found = false;
  5764. os_mutex_lock(&externref_lock);
  5765. /* Lookup hashmap firstly */
  5766. bh_hash_map_traverse(externref_map, lookup_extobj_callback,
  5767. (void *)&lookup_user_data);
  5768. if (lookup_user_data.found) {
  5769. void *key = (void *)(uintptr_t)lookup_user_data.externref_idx;
  5770. ExternRefMapNode *node = bh_hash_map_find(externref_map, key);
  5771. bh_assert(node);
  5772. node->cleanup = extern_obj_cleanup;
  5773. ok = true;
  5774. }
  5775. os_mutex_unlock(&externref_lock);
  5776. return ok;
  5777. }
  5778. bool
  5779. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
  5780. uint32 *p_externref_idx)
  5781. {
  5782. LookupExtObj_UserData lookup_user_data = { 0 };
  5783. ExternRefMapNode *node;
  5784. uint32 externref_idx;
  5785. /*
  5786. * to catch a parameter from `wasm_application_execute_func`,
  5787. * which represents a string 'null'
  5788. */
  5789. #if UINTPTR_MAX == UINT32_MAX
  5790. if ((uint32)-1 == (uintptr_t)extern_obj) {
  5791. #else
  5792. if ((uint64)-1LL == (uintptr_t)extern_obj) {
  5793. #endif
  5794. *p_externref_idx = NULL_REF;
  5795. return true;
  5796. }
  5797. /* in a wrapper, extern_obj could be any value */
  5798. lookup_user_data.node.extern_obj = extern_obj;
  5799. lookup_user_data.node.module_inst = module_inst;
  5800. lookup_user_data.found = false;
  5801. os_mutex_lock(&externref_lock);
  5802. /* Lookup hashmap firstly */
  5803. bh_hash_map_traverse(externref_map, lookup_extobj_callback,
  5804. (void *)&lookup_user_data);
  5805. if (lookup_user_data.found) {
  5806. *p_externref_idx = lookup_user_data.externref_idx;
  5807. os_mutex_unlock(&externref_lock);
  5808. return true;
  5809. }
  5810. /* Not found in hashmap */
  5811. if (externref_global_id == NULL_REF || externref_global_id == 0) {
  5812. goto fail1;
  5813. }
  5814. if (!(node = wasm_runtime_malloc(sizeof(ExternRefMapNode)))) {
  5815. goto fail1;
  5816. }
  5817. memset(node, 0, sizeof(ExternRefMapNode));
  5818. node->extern_obj = extern_obj;
  5819. node->module_inst = module_inst;
  5820. node->cleanup = NULL;
  5821. externref_idx = externref_global_id;
  5822. if (!bh_hash_map_insert(externref_map, (void *)(uintptr_t)externref_idx,
  5823. (void *)node)) {
  5824. goto fail2;
  5825. }
  5826. externref_global_id++;
  5827. *p_externref_idx = externref_idx;
  5828. os_mutex_unlock(&externref_lock);
  5829. return true;
  5830. fail2:
  5831. wasm_runtime_free(node);
  5832. fail1:
  5833. os_mutex_unlock(&externref_lock);
  5834. return false;
  5835. }
  5836. bool
  5837. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj)
  5838. {
  5839. ExternRefMapNode *node;
  5840. /* catch a `ref.null` variable */
  5841. if (externref_idx == NULL_REF) {
  5842. *p_extern_obj = NULL;
  5843. return true;
  5844. }
  5845. os_mutex_lock(&externref_lock);
  5846. node = bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  5847. os_mutex_unlock(&externref_lock);
  5848. if (!node)
  5849. return false;
  5850. *p_extern_obj = node->extern_obj;
  5851. return true;
  5852. }
  5853. static void
  5854. reclaim_extobj_callback(void *key, void *value, void *user_data)
  5855. {
  5856. ExternRefMapNode *node = (ExternRefMapNode *)value;
  5857. WASMModuleInstanceCommon *module_inst =
  5858. (WASMModuleInstanceCommon *)user_data;
  5859. if (node->module_inst == module_inst) {
  5860. if (!node->marked && !node->retained) {
  5861. delete_externref(key, node);
  5862. }
  5863. else {
  5864. node->marked = false;
  5865. }
  5866. }
  5867. }
  5868. static void
  5869. mark_externref(uint32 externref_idx)
  5870. {
  5871. ExternRefMapNode *node;
  5872. if (externref_idx != NULL_REF) {
  5873. node =
  5874. bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  5875. if (node) {
  5876. node->marked = true;
  5877. }
  5878. }
  5879. }
  5880. #if WASM_ENABLE_INTERP != 0
  5881. static void
  5882. interp_mark_all_externrefs(WASMModuleInstance *module_inst)
  5883. {
  5884. uint32 i, j, externref_idx;
  5885. table_elem_type_t *table_data;
  5886. uint8 *global_data = module_inst->global_data;
  5887. WASMGlobalInstance *global;
  5888. WASMTableInstance *table;
  5889. global = module_inst->e->globals;
  5890. for (i = 0; i < module_inst->e->global_count; i++, global++) {
  5891. if (global->type == VALUE_TYPE_EXTERNREF) {
  5892. externref_idx = *(uint32 *)(global_data + global->data_offset);
  5893. mark_externref(externref_idx);
  5894. }
  5895. }
  5896. for (i = 0; i < module_inst->table_count; i++) {
  5897. uint8 elem_type = 0;
  5898. uint32 init_size, max_size;
  5899. table = wasm_get_table_inst(module_inst, i);
  5900. (void)wasm_runtime_get_table_inst_elem_type(
  5901. (WASMModuleInstanceCommon *)module_inst, i, &elem_type, &init_size,
  5902. &max_size);
  5903. if (elem_type == VALUE_TYPE_EXTERNREF) {
  5904. table_data = table->elems;
  5905. for (j = 0; j < table->cur_size; j++) {
  5906. externref_idx = table_data[j];
  5907. mark_externref(externref_idx);
  5908. }
  5909. }
  5910. (void)init_size;
  5911. (void)max_size;
  5912. }
  5913. }
  5914. #endif
  5915. #if WASM_ENABLE_AOT != 0
  5916. static void
  5917. aot_mark_all_externrefs(AOTModuleInstance *module_inst)
  5918. {
  5919. uint32 i = 0, j = 0;
  5920. const AOTModule *module = (AOTModule *)module_inst->module;
  5921. const AOTTable *table = module->tables;
  5922. const AOTGlobal *global = module->globals;
  5923. const AOTTableInstance *table_inst;
  5924. for (i = 0; i < module->global_count; i++, global++) {
  5925. if (global->type.val_type == VALUE_TYPE_EXTERNREF) {
  5926. mark_externref(
  5927. *(uint32 *)(module_inst->global_data + global->data_offset));
  5928. }
  5929. }
  5930. for (i = 0; i < module->table_count; i++) {
  5931. table_inst = module_inst->tables[i];
  5932. if ((table + i)->table_type.elem_type == VALUE_TYPE_EXTERNREF) {
  5933. while (j < table_inst->cur_size) {
  5934. mark_externref(table_inst->elems[j++]);
  5935. }
  5936. }
  5937. }
  5938. }
  5939. #endif
  5940. void
  5941. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst)
  5942. {
  5943. os_mutex_lock(&externref_lock);
  5944. #if WASM_ENABLE_INTERP != 0
  5945. if (module_inst->module_type == Wasm_Module_Bytecode)
  5946. interp_mark_all_externrefs((WASMModuleInstance *)module_inst);
  5947. #endif
  5948. #if WASM_ENABLE_AOT != 0
  5949. if (module_inst->module_type == Wasm_Module_AoT)
  5950. aot_mark_all_externrefs((AOTModuleInstance *)module_inst);
  5951. #endif
  5952. bh_hash_map_traverse(externref_map, reclaim_extobj_callback,
  5953. (void *)module_inst);
  5954. os_mutex_unlock(&externref_lock);
  5955. }
  5956. static void
  5957. cleanup_extobj_callback(void *key, void *value, void *user_data)
  5958. {
  5959. ExternRefMapNode *node = (ExternRefMapNode *)value;
  5960. WASMModuleInstanceCommon *module_inst =
  5961. (WASMModuleInstanceCommon *)user_data;
  5962. if (node->module_inst == module_inst) {
  5963. delete_externref(key, node);
  5964. }
  5965. }
  5966. void
  5967. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst)
  5968. {
  5969. os_mutex_lock(&externref_lock);
  5970. bh_hash_map_traverse(externref_map, cleanup_extobj_callback,
  5971. (void *)module_inst);
  5972. os_mutex_unlock(&externref_lock);
  5973. }
  5974. bool
  5975. wasm_externref_retain(uint32 externref_idx)
  5976. {
  5977. ExternRefMapNode *node;
  5978. os_mutex_lock(&externref_lock);
  5979. if (externref_idx != NULL_REF) {
  5980. node =
  5981. bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  5982. if (node) {
  5983. node->retained = true;
  5984. os_mutex_unlock(&externref_lock);
  5985. return true;
  5986. }
  5987. }
  5988. os_mutex_unlock(&externref_lock);
  5989. return false;
  5990. }
  5991. #endif /* end of WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 */
  5992. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  5993. uint32
  5994. wasm_runtime_dump_line_buf_impl(const char *line_buf, bool dump_or_print,
  5995. char **buf, uint32 *len)
  5996. {
  5997. if (dump_or_print) {
  5998. return (uint32)os_printf("%s", line_buf);
  5999. }
  6000. else if (*buf) {
  6001. uint32 dump_len;
  6002. dump_len = snprintf(*buf, *len, "%s", line_buf);
  6003. if (dump_len >= *len) {
  6004. dump_len = *len;
  6005. }
  6006. *len = *len - dump_len;
  6007. *buf = *buf + dump_len;
  6008. return dump_len;
  6009. }
  6010. else {
  6011. return (uint32)strlen(line_buf);
  6012. }
  6013. }
  6014. void
  6015. wasm_runtime_dump_call_stack(WASMExecEnv *exec_env)
  6016. {
  6017. WASMModuleInstanceCommon *module_inst =
  6018. wasm_exec_env_get_module_inst(exec_env);
  6019. #if WASM_ENABLE_INTERP != 0
  6020. if (module_inst->module_type == Wasm_Module_Bytecode) {
  6021. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  6022. }
  6023. #endif
  6024. #if WASM_ENABLE_AOT != 0
  6025. if (module_inst->module_type == Wasm_Module_AoT) {
  6026. aot_dump_call_stack(exec_env, true, NULL, 0);
  6027. }
  6028. #endif
  6029. }
  6030. uint32
  6031. wasm_runtime_get_call_stack_buf_size(wasm_exec_env_t exec_env)
  6032. {
  6033. WASMModuleInstanceCommon *module_inst =
  6034. wasm_exec_env_get_module_inst(exec_env);
  6035. #if WASM_ENABLE_INTERP != 0
  6036. if (module_inst->module_type == Wasm_Module_Bytecode) {
  6037. return wasm_interp_dump_call_stack(exec_env, false, NULL, 0);
  6038. }
  6039. #endif
  6040. #if WASM_ENABLE_AOT != 0
  6041. if (module_inst->module_type == Wasm_Module_AoT) {
  6042. return aot_dump_call_stack(exec_env, false, NULL, 0);
  6043. }
  6044. #endif
  6045. return 0;
  6046. }
  6047. uint32
  6048. wasm_runtime_dump_call_stack_to_buf(wasm_exec_env_t exec_env, char *buf,
  6049. uint32 len)
  6050. {
  6051. WASMModuleInstanceCommon *module_inst =
  6052. wasm_exec_env_get_module_inst(exec_env);
  6053. #if WASM_ENABLE_INTERP != 0
  6054. if (module_inst->module_type == Wasm_Module_Bytecode) {
  6055. return wasm_interp_dump_call_stack(exec_env, false, buf, len);
  6056. }
  6057. #endif
  6058. #if WASM_ENABLE_AOT != 0
  6059. if (module_inst->module_type == Wasm_Module_AoT) {
  6060. return aot_dump_call_stack(exec_env, false, buf, len);
  6061. }
  6062. #endif
  6063. return 0;
  6064. }
  6065. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
  6066. #if WASM_ENABLE_STATIC_PGO != 0
  6067. uint32
  6068. wasm_runtime_get_pgo_prof_data_size(WASMModuleInstanceCommon *module_inst)
  6069. {
  6070. #if WASM_ENABLE_AOT != 0
  6071. if (module_inst->module_type == Wasm_Module_AoT) {
  6072. AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
  6073. return aot_get_pgo_prof_data_size(aot_inst);
  6074. }
  6075. #endif
  6076. return 0;
  6077. }
  6078. uint32
  6079. wasm_runtime_dump_pgo_prof_data_to_buf(WASMModuleInstanceCommon *module_inst,
  6080. char *buf, uint32 len)
  6081. {
  6082. #if WASM_ENABLE_AOT != 0
  6083. if (module_inst->module_type == Wasm_Module_AoT) {
  6084. AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
  6085. return aot_dump_pgo_prof_data_to_buf(aot_inst, buf, len);
  6086. }
  6087. #endif
  6088. return 0;
  6089. }
  6090. #endif /* end of WASM_ENABLE_STATIC_PGO != 0 */
  6091. bool
  6092. wasm_runtime_get_table_elem_type(const WASMModuleCommon *module_comm,
  6093. uint32 table_idx, uint8 *out_elem_type,
  6094. #if WASM_ENABLE_GC != 0
  6095. WASMRefType **out_ref_type,
  6096. #endif
  6097. uint32 *out_min_size, uint32 *out_max_size)
  6098. {
  6099. #if WASM_ENABLE_INTERP != 0
  6100. if (module_comm->module_type == Wasm_Module_Bytecode) {
  6101. WASMModule *module = (WASMModule *)module_comm;
  6102. if (table_idx < module->import_table_count) {
  6103. WASMTableImport *import_table =
  6104. &((module->import_tables + table_idx)->u.table);
  6105. *out_elem_type = import_table->table_type.elem_type;
  6106. #if WASM_ENABLE_GC != 0
  6107. *out_ref_type = import_table->table_type.elem_ref_type;
  6108. #endif
  6109. *out_min_size = import_table->table_type.init_size;
  6110. *out_max_size = import_table->table_type.max_size;
  6111. }
  6112. else {
  6113. WASMTable *table =
  6114. module->tables + (table_idx - module->import_table_count);
  6115. *out_elem_type = table->table_type.elem_type;
  6116. #if WASM_ENABLE_GC != 0
  6117. *out_ref_type = table->table_type.elem_ref_type;
  6118. #endif
  6119. *out_min_size = table->table_type.init_size;
  6120. *out_max_size = table->table_type.max_size;
  6121. }
  6122. return true;
  6123. }
  6124. #endif
  6125. #if WASM_ENABLE_AOT != 0
  6126. if (module_comm->module_type == Wasm_Module_AoT) {
  6127. AOTModule *module = (AOTModule *)module_comm;
  6128. if (table_idx < module->import_table_count) {
  6129. AOTImportTable *import_table = module->import_tables + table_idx;
  6130. *out_elem_type = import_table->table_type.elem_type;
  6131. #if WASM_ENABLE_GC != 0
  6132. *out_ref_type = NULL; /* TODO */
  6133. #endif
  6134. *out_min_size = import_table->table_type.init_size;
  6135. *out_max_size = import_table->table_type.max_size;
  6136. }
  6137. else {
  6138. AOTTable *table =
  6139. module->tables + (table_idx - module->import_table_count);
  6140. *out_elem_type = table->table_type.elem_type;
  6141. #if WASM_ENABLE_GC != 0
  6142. *out_ref_type = NULL; /* TODO */
  6143. #endif
  6144. *out_min_size = table->table_type.init_size;
  6145. *out_max_size = table->table_type.max_size;
  6146. }
  6147. return true;
  6148. }
  6149. #endif
  6150. return false;
  6151. }
  6152. bool
  6153. wasm_runtime_get_table_inst_elem_type(
  6154. const WASMModuleInstanceCommon *module_inst_comm, uint32 table_idx,
  6155. uint8 *out_elem_type,
  6156. #if WASM_ENABLE_GC != 0
  6157. WASMRefType **out_ref_type,
  6158. #endif
  6159. uint32 *out_min_size, uint32 *out_max_size)
  6160. {
  6161. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  6162. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  6163. || module_inst_comm->module_type == Wasm_Module_AoT);
  6164. return wasm_runtime_get_table_elem_type(
  6165. (WASMModuleCommon *)module_inst->module, table_idx, out_elem_type,
  6166. #if WASM_ENABLE_GC != 0
  6167. out_ref_type,
  6168. #endif
  6169. out_min_size, out_max_size);
  6170. }
  6171. bool
  6172. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  6173. const WASMExport *export, WASMFuncType **out)
  6174. {
  6175. #if WASM_ENABLE_INTERP != 0
  6176. if (module_comm->module_type == Wasm_Module_Bytecode) {
  6177. WASMModule *module = (WASMModule *)module_comm;
  6178. if (export->index < module->import_function_count) {
  6179. *out = module->import_functions[export->index].u.function.func_type;
  6180. }
  6181. else {
  6182. *out =
  6183. module->functions[export->index - module->import_function_count]
  6184. ->func_type;
  6185. }
  6186. return true;
  6187. }
  6188. #endif
  6189. #if WASM_ENABLE_AOT != 0
  6190. if (module_comm->module_type == Wasm_Module_AoT) {
  6191. AOTModule *module = (AOTModule *)module_comm;
  6192. if (export->index < module->import_func_count) {
  6193. *out = (WASMFuncType *)
  6194. module->types[module->import_funcs[export->index]
  6195. .func_type_index];
  6196. }
  6197. else {
  6198. *out = (WASMFuncType *)module
  6199. ->types[module->func_type_indexes
  6200. [export->index - module->import_func_count]];
  6201. }
  6202. return true;
  6203. }
  6204. #endif
  6205. return false;
  6206. }
  6207. bool
  6208. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  6209. const WASMExport *export,
  6210. uint8 *out_val_type, bool *out_mutability)
  6211. {
  6212. #if WASM_ENABLE_INTERP != 0
  6213. if (module_comm->module_type == Wasm_Module_Bytecode) {
  6214. WASMModule *module = (WASMModule *)module_comm;
  6215. if (export->index < module->import_global_count) {
  6216. WASMGlobalImport *import_global =
  6217. &((module->import_globals + export->index)->u.global);
  6218. *out_val_type = import_global->type.val_type;
  6219. *out_mutability = import_global->type.is_mutable;
  6220. }
  6221. else {
  6222. WASMGlobal *global =
  6223. module->globals + (export->index - module->import_global_count);
  6224. *out_val_type = global->type.val_type;
  6225. *out_mutability = global->type.is_mutable;
  6226. }
  6227. return true;
  6228. }
  6229. #endif
  6230. #if WASM_ENABLE_AOT != 0
  6231. if (module_comm->module_type == Wasm_Module_AoT) {
  6232. AOTModule *module = (AOTModule *)module_comm;
  6233. if (export->index < module->import_global_count) {
  6234. AOTImportGlobal *import_global =
  6235. module->import_globals + export->index;
  6236. *out_val_type = import_global->type.val_type;
  6237. *out_mutability = import_global->type.is_mutable;
  6238. }
  6239. else {
  6240. AOTGlobal *global =
  6241. module->globals + (export->index - module->import_global_count);
  6242. *out_val_type = global->type.val_type;
  6243. *out_mutability = global->type.is_mutable;
  6244. }
  6245. return true;
  6246. }
  6247. #endif
  6248. return false;
  6249. }
  6250. bool
  6251. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  6252. const WASMExport *export,
  6253. uint32 *out_min_page, uint32 *out_max_page)
  6254. {
  6255. #if WASM_ENABLE_INTERP != 0
  6256. if (module_comm->module_type == Wasm_Module_Bytecode) {
  6257. WASMModule *module = (WASMModule *)module_comm;
  6258. if (export->index < module->import_memory_count) {
  6259. WASMMemoryImport *import_memory =
  6260. &((module->import_memories + export->index)->u.memory);
  6261. *out_min_page = import_memory->mem_type.init_page_count;
  6262. *out_max_page = import_memory->mem_type.max_page_count;
  6263. }
  6264. else {
  6265. WASMMemory *memory =
  6266. module->memories
  6267. + (export->index - module->import_memory_count);
  6268. *out_min_page = memory->init_page_count;
  6269. *out_max_page = memory->max_page_count;
  6270. }
  6271. return true;
  6272. }
  6273. #endif
  6274. #if WASM_ENABLE_AOT != 0
  6275. if (module_comm->module_type == Wasm_Module_AoT) {
  6276. AOTModule *module = (AOTModule *)module_comm;
  6277. if (export->index < module->import_memory_count) {
  6278. AOTImportMemory *import_memory =
  6279. module->import_memories + export->index;
  6280. *out_min_page = import_memory->mem_type.init_page_count;
  6281. *out_max_page = import_memory->mem_type.max_page_count;
  6282. }
  6283. else {
  6284. AOTMemory *memory = module->memories
  6285. + (export->index - module->import_memory_count);
  6286. *out_min_page = memory->init_page_count;
  6287. *out_max_page = memory->max_page_count;
  6288. }
  6289. return true;
  6290. }
  6291. #endif
  6292. return false;
  6293. }
  6294. bool
  6295. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  6296. const WASMExport *export,
  6297. uint8 *out_elem_type,
  6298. #if WASM_ENABLE_GC != 0
  6299. WASMRefType **out_ref_type,
  6300. #endif
  6301. uint32 *out_min_size, uint32 *out_max_size)
  6302. {
  6303. return wasm_runtime_get_table_elem_type(module_comm, export->index,
  6304. out_elem_type,
  6305. #if WASM_ENABLE_GC != 0
  6306. out_ref_type,
  6307. #endif
  6308. out_min_size, out_max_size);
  6309. }
  6310. static inline bool
  6311. argv_to_params(wasm_val_t *out_params, const uint32 *argv,
  6312. WASMFuncType *func_type)
  6313. {
  6314. wasm_val_t *param = out_params;
  6315. uint32 i = 0, *u32;
  6316. for (i = 0; i < func_type->param_count; i++, param++) {
  6317. switch (func_type->types[i]) {
  6318. case VALUE_TYPE_I32:
  6319. param->kind = WASM_I32;
  6320. param->of.i32 = *argv++;
  6321. break;
  6322. case VALUE_TYPE_I64:
  6323. param->kind = WASM_I64;
  6324. u32 = (uint32 *)&param->of.i64;
  6325. u32[0] = *argv++;
  6326. u32[1] = *argv++;
  6327. break;
  6328. case VALUE_TYPE_F32:
  6329. param->kind = WASM_F32;
  6330. param->of.f32 = *(float32 *)argv++;
  6331. break;
  6332. case VALUE_TYPE_F64:
  6333. param->kind = WASM_F64;
  6334. u32 = (uint32 *)&param->of.i64;
  6335. u32[0] = *argv++;
  6336. u32[1] = *argv++;
  6337. break;
  6338. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  6339. case VALUE_TYPE_EXTERNREF:
  6340. param->kind = WASM_EXTERNREF;
  6341. if (!wasm_externref_ref2obj(*argv,
  6342. (void **)&param->of.foreign)) {
  6343. return false;
  6344. }
  6345. argv++;
  6346. break;
  6347. #endif
  6348. default:
  6349. return false;
  6350. }
  6351. }
  6352. return true;
  6353. }
  6354. static inline bool
  6355. results_to_argv(WASMModuleInstanceCommon *module_inst, uint32 *out_argv,
  6356. const wasm_val_t *results, WASMFuncType *func_type)
  6357. {
  6358. const wasm_val_t *result = results;
  6359. uint32 *argv = out_argv, *u32, i;
  6360. uint8 *result_types = func_type->types + func_type->param_count;
  6361. for (i = 0; i < func_type->result_count; i++, result++) {
  6362. switch (result_types[i]) {
  6363. case VALUE_TYPE_I32:
  6364. case VALUE_TYPE_F32:
  6365. *(int32 *)argv++ = result->of.i32;
  6366. break;
  6367. case VALUE_TYPE_I64:
  6368. case VALUE_TYPE_F64:
  6369. u32 = (uint32 *)&result->of.i64;
  6370. *argv++ = u32[0];
  6371. *argv++ = u32[1];
  6372. break;
  6373. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  6374. case VALUE_TYPE_EXTERNREF:
  6375. if (!wasm_externref_obj2ref(module_inst,
  6376. (void *)result->of.foreign,
  6377. (uint32 *)argv)) {
  6378. return false;
  6379. }
  6380. argv++;
  6381. break;
  6382. #endif
  6383. default:
  6384. return false;
  6385. }
  6386. }
  6387. return true;
  6388. }
  6389. bool
  6390. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  6391. void *func_ptr, WASMFuncType *func_type,
  6392. uint32 argc, uint32 *argv, bool with_env,
  6393. void *wasm_c_api_env)
  6394. {
  6395. wasm_val_t params_buf[16] = { 0 }, results_buf[4] = { 0 };
  6396. wasm_val_t *params = params_buf, *results = results_buf;
  6397. wasm_trap_t *trap = NULL;
  6398. bool ret = false;
  6399. wasm_val_vec_t params_vec = { 0 }, results_vec = { 0 };
  6400. if (func_type->param_count > 16) {
  6401. if (!(params =
  6402. runtime_malloc(sizeof(wasm_val_t) * func_type->param_count,
  6403. module_inst, NULL, 0))) {
  6404. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  6405. return false;
  6406. }
  6407. }
  6408. if (!argv_to_params(params, argv, func_type)) {
  6409. wasm_runtime_set_exception(module_inst, "unsupported param type");
  6410. goto fail;
  6411. }
  6412. if (func_type->result_count > 4) {
  6413. if (!(results =
  6414. runtime_malloc(sizeof(wasm_val_t) * func_type->result_count,
  6415. module_inst, NULL, 0))) {
  6416. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  6417. goto fail;
  6418. }
  6419. }
  6420. params_vec.data = params;
  6421. params_vec.num_elems = func_type->param_count;
  6422. params_vec.size = func_type->param_count;
  6423. results_vec.data = results;
  6424. results_vec.num_elems = 0;
  6425. results_vec.size = func_type->result_count;
  6426. if (!with_env) {
  6427. wasm_func_callback_t callback = (wasm_func_callback_t)func_ptr;
  6428. trap = callback(&params_vec, &results_vec);
  6429. }
  6430. else {
  6431. wasm_func_callback_with_env_t callback =
  6432. (wasm_func_callback_with_env_t)func_ptr;
  6433. trap = callback(wasm_c_api_env, &params_vec, &results_vec);
  6434. }
  6435. if (trap) {
  6436. if (trap->message->data) {
  6437. /* since trap->message->data does not end with '\0' */
  6438. char trap_message[108] = { 0 };
  6439. uint32 max_size_to_copy = (uint32)sizeof(trap_message) - 1;
  6440. uint32 size_to_copy = (trap->message->size < max_size_to_copy)
  6441. ? (uint32)trap->message->size
  6442. : max_size_to_copy;
  6443. bh_memcpy_s(trap_message, (uint32)sizeof(trap_message),
  6444. trap->message->data, size_to_copy);
  6445. wasm_runtime_set_exception(module_inst, trap_message);
  6446. }
  6447. else {
  6448. wasm_runtime_set_exception(
  6449. module_inst, "native function throw unknown exception");
  6450. }
  6451. wasm_trap_delete(trap);
  6452. goto fail;
  6453. }
  6454. if (!results_to_argv(module_inst, argv, results, func_type)) {
  6455. wasm_runtime_set_exception(module_inst, "unsupported result type");
  6456. goto fail;
  6457. }
  6458. ret = true;
  6459. fail:
  6460. if (params != params_buf)
  6461. wasm_runtime_free(params);
  6462. if (results != results_buf)
  6463. wasm_runtime_free(results);
  6464. return ret;
  6465. }
  6466. bool
  6467. wasm_runtime_quick_invoke_c_api_native(WASMModuleInstanceCommon *inst_comm,
  6468. CApiFuncImport *c_api_import,
  6469. wasm_val_t *params, uint32 param_count,
  6470. wasm_val_t *results, uint32 result_count)
  6471. {
  6472. WASMModuleInstance *module_inst = (WASMModuleInstance *)inst_comm;
  6473. void *func_ptr = c_api_import->func_ptr_linked;
  6474. bool with_env_arg = c_api_import->with_env_arg, ret = true;
  6475. wasm_val_vec_t params_vec = { 0 }, results_vec = { 0 };
  6476. wasm_trap_t *trap = NULL;
  6477. params_vec.data = params;
  6478. params_vec.num_elems = param_count;
  6479. params_vec.size = param_count;
  6480. results_vec.data = results;
  6481. results_vec.num_elems = 0;
  6482. results_vec.size = result_count;
  6483. if (!func_ptr) {
  6484. wasm_set_exception_with_id(module_inst, EXCE_CALL_UNLINKED_IMPORT_FUNC);
  6485. ret = false;
  6486. goto fail;
  6487. }
  6488. if (!with_env_arg) {
  6489. wasm_func_callback_t callback = (wasm_func_callback_t)func_ptr;
  6490. trap = callback(&params_vec, &results_vec);
  6491. }
  6492. else {
  6493. void *wasm_c_api_env = c_api_import->env_arg;
  6494. wasm_func_callback_with_env_t callback =
  6495. (wasm_func_callback_with_env_t)func_ptr;
  6496. trap = callback(wasm_c_api_env, &params_vec, &results_vec);
  6497. }
  6498. if (trap) {
  6499. if (trap->message->data) {
  6500. /* since trap->message->data does not end with '\0' */
  6501. char trap_message[108] = { 0 };
  6502. uint32 max_size_to_copy = (uint32)sizeof(trap_message) - 1;
  6503. uint32 size_to_copy = (trap->message->size < max_size_to_copy)
  6504. ? (uint32)trap->message->size
  6505. : max_size_to_copy;
  6506. bh_memcpy_s(trap_message, (uint32)sizeof(trap_message),
  6507. trap->message->data, size_to_copy);
  6508. wasm_set_exception(module_inst, trap_message);
  6509. }
  6510. else {
  6511. wasm_set_exception(module_inst,
  6512. "native function throw unknown exception");
  6513. }
  6514. wasm_trap_delete(trap);
  6515. ret = false;
  6516. }
  6517. fail:
  6518. #ifdef OS_ENABLE_HW_BOUND_CHECK
  6519. if (!ret)
  6520. wasm_runtime_access_exce_check_guard_page();
  6521. #endif
  6522. return ret;
  6523. }
  6524. void
  6525. wasm_runtime_show_app_heap_corrupted_prompt()
  6526. {
  6527. LOG_ERROR("Error: app heap is corrupted, if the wasm file "
  6528. "is compiled by wasi-sdk-12.0 or higher version, "
  6529. "please add -Wl,--export=malloc -Wl,--export=free "
  6530. "to export malloc and free functions. If it is "
  6531. "compiled by asc, please add --exportRuntime to "
  6532. "export the runtime helpers.");
  6533. }
  6534. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6535. void
  6536. wasm_runtime_destroy_custom_sections(WASMCustomSection *section_list)
  6537. {
  6538. WASMCustomSection *section = section_list, *next;
  6539. while (section) {
  6540. next = section->next;
  6541. wasm_runtime_free(section);
  6542. section = next;
  6543. }
  6544. }
  6545. #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION */
  6546. void
  6547. wasm_runtime_get_version(uint32_t *major, uint32_t *minor, uint32_t *patch)
  6548. {
  6549. *major = WAMR_VERSION_MAJOR;
  6550. *minor = WAMR_VERSION_MINOR;
  6551. *patch = WAMR_VERSION_PATCH;
  6552. }
  6553. bool
  6554. wasm_runtime_is_import_func_linked(const char *module_name,
  6555. const char *func_name)
  6556. {
  6557. return wasm_native_resolve_symbol(module_name, func_name, NULL, NULL, NULL,
  6558. NULL);
  6559. }
  6560. bool
  6561. wasm_runtime_is_import_global_linked(const char *module_name,
  6562. const char *global_name)
  6563. {
  6564. #if WASM_ENABLE_LIBC_BUILTIN != 0
  6565. WASMGlobalImport global = { 0 };
  6566. return wasm_native_lookup_libc_builtin_global(module_name, global_name,
  6567. &global);
  6568. #else
  6569. return false;
  6570. #endif
  6571. }
  6572. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  6573. WASMExport *
  6574. loader_find_export(const WASMModuleCommon *module, const char *module_name,
  6575. const char *field_name, uint8 export_kind, char *error_buf,
  6576. uint32 error_buf_size)
  6577. {
  6578. WASMExport *exports = NULL, *result = NULL, *export;
  6579. uint32 export_count = 0, i;
  6580. #if WASM_ENABLE_AOT != 0
  6581. if (module->module_type == Wasm_Module_AoT) {
  6582. AOTModule *aot_module = (AOTModule *)module;
  6583. exports = (WASMExport *)aot_module->exports;
  6584. export_count = aot_module->export_count;
  6585. }
  6586. #endif
  6587. #if WASM_ENABLE_INTERP != 0
  6588. if (module->module_type == Wasm_Module_Bytecode) {
  6589. WASMModule *wasm_module = (WASMModule *)module;
  6590. exports = wasm_module->exports;
  6591. export_count = wasm_module->export_count;
  6592. }
  6593. #endif
  6594. for (i = 0, export = exports; i < export_count; ++i, ++export) {
  6595. if (export->kind == export_kind && !strcmp(field_name, export->name)) {
  6596. result = export;
  6597. goto exit;
  6598. }
  6599. }
  6600. if (i == export_count) {
  6601. LOG_DEBUG("can not find an export %d named %s in the module %s",
  6602. export_kind, field_name, module_name);
  6603. set_error_buf(error_buf, error_buf_size,
  6604. "unknown import or incompatible import type");
  6605. }
  6606. exit:
  6607. return result;
  6608. }
  6609. #endif
  6610. #if WASM_ENABLE_MULTI_MODULE != 0
  6611. WASMModuleCommon *
  6612. wasm_runtime_search_sub_module(const WASMModuleCommon *parent_module,
  6613. const char *sub_module_name)
  6614. {
  6615. WASMRegisteredModule *node = NULL;
  6616. #if WASM_ENABLE_AOT != 0
  6617. if (parent_module->module_type == Wasm_Module_AoT) {
  6618. node = bh_list_first_elem(
  6619. ((AOTModule *)parent_module)->import_module_list);
  6620. }
  6621. #endif
  6622. #if WASM_ENABLE_INTERP != 0
  6623. if (parent_module->module_type == Wasm_Module_Bytecode) {
  6624. node = bh_list_first_elem(
  6625. ((WASMModule *)parent_module)->import_module_list);
  6626. }
  6627. #endif
  6628. while (node && strcmp(sub_module_name, node->module_name)) {
  6629. node = bh_list_elem_next(node);
  6630. }
  6631. return node ? node->module : NULL;
  6632. }
  6633. bool
  6634. wasm_runtime_register_sub_module(const WASMModuleCommon *parent_module,
  6635. const char *sub_module_name,
  6636. WASMModuleCommon *sub_module)
  6637. {
  6638. /* register sub_module into its parent sub module list */
  6639. WASMRegisteredModule *node = NULL;
  6640. bh_list_status ret = BH_LIST_ERROR;
  6641. if (wasm_runtime_search_sub_module(parent_module, sub_module_name)) {
  6642. LOG_DEBUG("%s has been registered in its parent", sub_module_name);
  6643. return true;
  6644. }
  6645. node = loader_malloc(sizeof(WASMRegisteredModule), NULL, 0);
  6646. if (!node) {
  6647. return false;
  6648. }
  6649. node->module_name = sub_module_name;
  6650. node->module = sub_module;
  6651. #if WASM_ENABLE_AOT != 0
  6652. if (parent_module->module_type == Wasm_Module_AoT) {
  6653. ret = bh_list_insert(((AOTModule *)parent_module)->import_module_list,
  6654. node);
  6655. }
  6656. #endif
  6657. #if WASM_ENABLE_INTERP != 0
  6658. if (parent_module->module_type == Wasm_Module_Bytecode) {
  6659. ret = bh_list_insert(((WASMModule *)parent_module)->import_module_list,
  6660. node);
  6661. }
  6662. #endif
  6663. bh_assert(BH_LIST_SUCCESS == ret);
  6664. (void)ret;
  6665. return true;
  6666. }
  6667. WASMModuleCommon *
  6668. wasm_runtime_load_depended_module(const WASMModuleCommon *parent_module,
  6669. const char *sub_module_name, char *error_buf,
  6670. uint32 error_buf_size)
  6671. {
  6672. WASMModuleCommon *sub_module = NULL;
  6673. bool ret = false;
  6674. uint8 *buffer = NULL;
  6675. uint32 buffer_size = 0;
  6676. LoadArgs args = { 0 };
  6677. /* check the registered module list of the parent */
  6678. sub_module = wasm_runtime_search_sub_module(parent_module, sub_module_name);
  6679. if (sub_module) {
  6680. LOG_DEBUG("%s has been loaded before", sub_module_name);
  6681. return sub_module;
  6682. }
  6683. /* check the global registered module list */
  6684. sub_module = wasm_runtime_find_module_registered(sub_module_name);
  6685. if (sub_module) {
  6686. LOG_DEBUG("%s has been loaded", sub_module_name);
  6687. goto wasm_runtime_register_sub_module;
  6688. }
  6689. LOG_VERBOSE("loading %s", sub_module_name);
  6690. if (!reader) {
  6691. set_error_buf_v(parent_module, error_buf, error_buf_size,
  6692. "no sub module reader to load %s", sub_module_name);
  6693. return NULL;
  6694. }
  6695. /* start to maintain a loading module list */
  6696. ret = wasm_runtime_is_loading_module(sub_module_name);
  6697. if (ret) {
  6698. set_error_buf_v(parent_module, error_buf, error_buf_size,
  6699. "found circular dependency on %s", sub_module_name);
  6700. return NULL;
  6701. }
  6702. ret = wasm_runtime_add_loading_module(sub_module_name, error_buf,
  6703. error_buf_size);
  6704. if (!ret) {
  6705. LOG_DEBUG("can not add %s into loading module list\n", sub_module_name);
  6706. return NULL;
  6707. }
  6708. ret = reader(parent_module->module_type, sub_module_name, &buffer,
  6709. &buffer_size);
  6710. if (!ret) {
  6711. LOG_DEBUG("read the file of %s failed", sub_module_name);
  6712. set_error_buf_v(parent_module, error_buf, error_buf_size,
  6713. "unknown import %s", sub_module_name);
  6714. goto delete_loading_module;
  6715. }
  6716. if (get_package_type(buffer, buffer_size) != parent_module->module_type) {
  6717. LOG_DEBUG("module %s type error", sub_module_name);
  6718. goto destroy_file_buffer;
  6719. }
  6720. args.name = (char *)sub_module_name;
  6721. if (get_package_type(buffer, buffer_size) == Wasm_Module_Bytecode) {
  6722. #if WASM_ENABLE_INTERP != 0
  6723. sub_module = (WASMModuleCommon *)wasm_load(
  6724. buffer, buffer_size, false, &args, error_buf, error_buf_size);
  6725. #endif
  6726. }
  6727. else if (get_package_type(buffer, buffer_size) == Wasm_Module_AoT) {
  6728. #if WASM_ENABLE_AOT != 0
  6729. sub_module = (WASMModuleCommon *)aot_load_from_aot_file(
  6730. buffer, buffer_size, &args, error_buf, error_buf_size);
  6731. #endif
  6732. }
  6733. if (!sub_module) {
  6734. LOG_DEBUG("error: can not load the sub_module %s", sub_module_name);
  6735. /* others will be destroyed in runtime_destroy() */
  6736. goto destroy_file_buffer;
  6737. }
  6738. wasm_runtime_delete_loading_module(sub_module_name);
  6739. /* register on a global list */
  6740. ret = wasm_runtime_register_module_internal(
  6741. sub_module_name, (WASMModuleCommon *)sub_module, buffer, buffer_size,
  6742. error_buf, error_buf_size);
  6743. if (!ret) {
  6744. LOG_DEBUG("error: can not register module %s globally\n",
  6745. sub_module_name);
  6746. /* others will be unloaded in runtime_destroy() */
  6747. goto unload_module;
  6748. }
  6749. /* register into its parent list */
  6750. wasm_runtime_register_sub_module:
  6751. ret = wasm_runtime_register_sub_module(parent_module, sub_module_name,
  6752. sub_module);
  6753. if (!ret) {
  6754. set_error_buf_v(parent_module, error_buf, error_buf_size,
  6755. "failed to register sub module %s", sub_module_name);
  6756. /* since it is in the global module list, no need to
  6757. * unload the module. the runtime_destroy() will do it
  6758. */
  6759. return NULL;
  6760. }
  6761. return sub_module;
  6762. unload_module:
  6763. wasm_runtime_unload(sub_module);
  6764. destroy_file_buffer:
  6765. if (destroyer) {
  6766. destroyer(buffer, buffer_size);
  6767. }
  6768. else {
  6769. LOG_WARNING("need to release the reading buffer of %s manually",
  6770. sub_module_name);
  6771. }
  6772. delete_loading_module:
  6773. wasm_runtime_delete_loading_module(sub_module_name);
  6774. return NULL;
  6775. }
  6776. bool
  6777. wasm_runtime_sub_module_instantiate(WASMModuleCommon *module,
  6778. WASMModuleInstanceCommon *module_inst,
  6779. uint32 stack_size, uint32 heap_size,
  6780. uint32 max_memory_pages, char *error_buf,
  6781. uint32 error_buf_size)
  6782. {
  6783. bh_list *sub_module_inst_list = NULL;
  6784. WASMRegisteredModule *sub_module_list_node = NULL;
  6785. #if WASM_ENABLE_AOT != 0
  6786. if (module->module_type == Wasm_Module_AoT) {
  6787. sub_module_inst_list =
  6788. ((AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e)
  6789. ->sub_module_inst_list;
  6790. sub_module_list_node =
  6791. bh_list_first_elem(((AOTModule *)module)->import_module_list);
  6792. }
  6793. #endif
  6794. #if WASM_ENABLE_INTERP != 0
  6795. if (module->module_type == Wasm_Module_Bytecode) {
  6796. sub_module_inst_list =
  6797. ((WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e)
  6798. ->sub_module_inst_list;
  6799. sub_module_list_node =
  6800. bh_list_first_elem(((WASMModule *)module)->import_module_list);
  6801. }
  6802. #endif
  6803. while (sub_module_list_node) {
  6804. WASMSubModInstNode *sub_module_inst_list_node = NULL;
  6805. WASMModuleCommon *sub_module = sub_module_list_node->module;
  6806. WASMModuleInstanceCommon *sub_module_inst = NULL;
  6807. sub_module_inst = wasm_runtime_instantiate_internal(
  6808. sub_module, NULL, NULL, stack_size, heap_size, max_memory_pages,
  6809. error_buf, error_buf_size);
  6810. if (!sub_module_inst) {
  6811. LOG_DEBUG("instantiate %s failed",
  6812. sub_module_list_node->module_name);
  6813. return false;
  6814. }
  6815. sub_module_inst_list_node = loader_malloc(sizeof(WASMSubModInstNode),
  6816. error_buf, error_buf_size);
  6817. if (!sub_module_inst_list_node) {
  6818. LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ: %zu",
  6819. sizeof(WASMSubModInstNode));
  6820. if (sub_module_inst)
  6821. wasm_runtime_deinstantiate_internal(sub_module_inst, false);
  6822. return false;
  6823. }
  6824. sub_module_inst_list_node->module_inst =
  6825. (WASMModuleInstance *)sub_module_inst;
  6826. sub_module_inst_list_node->module_name =
  6827. sub_module_list_node->module_name;
  6828. #if WASM_ENABLE_AOT != 0
  6829. if (module_inst->module_type == Wasm_Module_AoT) {
  6830. AOTModuleInstance *aot_module_inst =
  6831. (AOTModuleInstance *)module_inst;
  6832. AOTModule *aot_module = (AOTModule *)module;
  6833. AOTModuleInstanceExtra *aot_extra =
  6834. (AOTModuleInstanceExtra *)aot_module_inst->e;
  6835. uint32 i;
  6836. AOTImportFunc *import_func;
  6837. for (i = 0; i < aot_module->import_func_count; i++) {
  6838. if (aot_extra->import_func_module_insts[i])
  6839. continue;
  6840. import_func = &aot_module->import_funcs[i];
  6841. if (strcmp(sub_module_inst_list_node->module_name,
  6842. import_func->module_name)
  6843. == 0) {
  6844. aot_extra->import_func_module_insts[i] =
  6845. (WASMModuleInstanceCommon *)
  6846. sub_module_inst_list_node->module_inst;
  6847. }
  6848. }
  6849. }
  6850. #endif
  6851. bh_list_status ret =
  6852. bh_list_insert(sub_module_inst_list, sub_module_inst_list_node);
  6853. bh_assert(BH_LIST_SUCCESS == ret);
  6854. (void)ret;
  6855. sub_module_list_node = bh_list_elem_next(sub_module_list_node);
  6856. }
  6857. return true;
  6858. }
  6859. void
  6860. wasm_runtime_sub_module_deinstantiate(WASMModuleInstanceCommon *module_inst)
  6861. {
  6862. bh_list *list = NULL;
  6863. #if WASM_ENABLE_AOT != 0
  6864. if (module_inst->module_type == Wasm_Module_AoT) {
  6865. list = ((AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e)
  6866. ->sub_module_inst_list;
  6867. }
  6868. #endif
  6869. #if WASM_ENABLE_INTERP != 0
  6870. if (module_inst->module_type == Wasm_Module_Bytecode) {
  6871. list =
  6872. ((WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e)
  6873. ->sub_module_inst_list;
  6874. }
  6875. #endif
  6876. WASMSubModInstNode *node = bh_list_first_elem(list);
  6877. while (node) {
  6878. WASMSubModInstNode *next_node = bh_list_elem_next(node);
  6879. bh_list_remove(list, node);
  6880. wasm_runtime_deinstantiate_internal(
  6881. (WASMModuleInstanceCommon *)node->module_inst, false);
  6882. wasm_runtime_free(node);
  6883. node = next_node;
  6884. }
  6885. }
  6886. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  6887. #if WASM_ENABLE_MODULE_INST_CONTEXT != 0
  6888. void *
  6889. wasm_runtime_create_context_key(void (*dtor)(WASMModuleInstanceCommon *inst,
  6890. void *ctx))
  6891. {
  6892. return wasm_native_create_context_key(dtor);
  6893. }
  6894. void
  6895. wasm_runtime_destroy_context_key(void *key)
  6896. {
  6897. wasm_native_destroy_context_key(key);
  6898. }
  6899. void
  6900. wasm_runtime_set_context(WASMModuleInstanceCommon *inst, void *key, void *ctx)
  6901. {
  6902. wasm_native_set_context(inst, key, ctx);
  6903. }
  6904. void
  6905. wasm_runtime_set_context_spread(WASMModuleInstanceCommon *inst, void *key,
  6906. void *ctx)
  6907. {
  6908. wasm_native_set_context_spread(inst, key, ctx);
  6909. }
  6910. void *
  6911. wasm_runtime_get_context(WASMModuleInstanceCommon *inst, void *key)
  6912. {
  6913. return wasm_native_get_context(inst, key);
  6914. }
  6915. #endif /* WASM_ENABLE_MODULE_INST_CONTEXT != 0 */
  6916. #if WASM_ENABLE_LINUX_PERF != 0
  6917. static bool enable_linux_perf = false;
  6918. bool
  6919. wasm_runtime_get_linux_perf(void)
  6920. {
  6921. return enable_linux_perf;
  6922. }
  6923. void
  6924. wasm_runtime_set_linux_perf(bool flag)
  6925. {
  6926. enable_linux_perf = flag;
  6927. }
  6928. #endif
  6929. bool
  6930. wasm_runtime_set_module_name(wasm_module_t module, const char *name,
  6931. char *error_buf, uint32_t error_buf_size)
  6932. {
  6933. if (!module)
  6934. return false;
  6935. #if WASM_ENABLE_INTERP != 0
  6936. if (module->module_type == Wasm_Module_Bytecode)
  6937. return wasm_set_module_name((WASMModule *)module, name, error_buf,
  6938. error_buf_size);
  6939. #endif
  6940. #if WASM_ENABLE_AOT != 0
  6941. if (module->module_type == Wasm_Module_AoT)
  6942. return aot_set_module_name((AOTModule *)module, name, error_buf,
  6943. error_buf_size);
  6944. #endif
  6945. return false;
  6946. }
  6947. const char *
  6948. wasm_runtime_get_module_name(wasm_module_t module)
  6949. {
  6950. if (!module)
  6951. return "";
  6952. #if WASM_ENABLE_INTERP != 0
  6953. if (module->module_type == Wasm_Module_Bytecode)
  6954. return wasm_get_module_name((WASMModule *)module);
  6955. #endif
  6956. #if WASM_ENABLE_AOT != 0
  6957. if (module->module_type == Wasm_Module_AoT)
  6958. return aot_get_module_name((AOTModule *)module);
  6959. #endif
  6960. return "";
  6961. }
  6962. /*
  6963. * wasm_runtime_detect_native_stack_overflow
  6964. *
  6965. * - raise "native stack overflow" exception if available native stack
  6966. * at this point is less than WASM_STACK_GUARD_SIZE. in that case,
  6967. * return false.
  6968. *
  6969. * - update native_stack_top_min.
  6970. */
  6971. bool
  6972. wasm_runtime_detect_native_stack_overflow(WASMExecEnv *exec_env)
  6973. {
  6974. uint8 *boundary = exec_env->native_stack_boundary;
  6975. RECORD_STACK_USAGE(exec_env, (uint8 *)&boundary);
  6976. if (boundary == NULL) {
  6977. /* the platform doesn't support os_thread_get_stack_boundary */
  6978. return true;
  6979. }
  6980. #if defined(OS_ENABLE_HW_BOUND_CHECK) && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  6981. uint32 page_size = os_getpagesize();
  6982. uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
  6983. boundary = boundary + page_size * guard_page_count;
  6984. #endif
  6985. if ((uint8 *)&boundary < boundary) {
  6986. wasm_runtime_set_exception(wasm_runtime_get_module_inst(exec_env),
  6987. "native stack overflow");
  6988. return false;
  6989. }
  6990. return true;
  6991. }
  6992. bool
  6993. wasm_runtime_detect_native_stack_overflow_size(WASMExecEnv *exec_env,
  6994. uint32 requested_size)
  6995. {
  6996. uint8 *boundary = exec_env->native_stack_boundary;
  6997. RECORD_STACK_USAGE(exec_env, (uint8 *)&boundary);
  6998. if (boundary == NULL) {
  6999. /* the platform doesn't support os_thread_get_stack_boundary */
  7000. return true;
  7001. }
  7002. #if defined(OS_ENABLE_HW_BOUND_CHECK) && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  7003. uint32 page_size = os_getpagesize();
  7004. uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
  7005. boundary = boundary + page_size * guard_page_count;
  7006. #endif
  7007. /* adjust the boundary for the requested size */
  7008. boundary = boundary - WASM_STACK_GUARD_SIZE + requested_size;
  7009. if ((uint8 *)&boundary < boundary) {
  7010. wasm_runtime_set_exception(wasm_runtime_get_module_inst(exec_env),
  7011. "native stack overflow");
  7012. return false;
  7013. }
  7014. return true;
  7015. }
  7016. bool
  7017. wasm_runtime_is_underlying_binary_freeable(WASMModuleCommon *const module)
  7018. {
  7019. #if WASM_ENABLE_INTERP != 0
  7020. if (module->module_type == Wasm_Module_Bytecode) {
  7021. #if (WASM_ENABLE_JIT != 0 || WASM_ENABLE_FAST_JIT != 0) \
  7022. && (WASM_ENABLE_LAZY_JIT != 0)
  7023. return false;
  7024. #elif WASM_ENABLE_FAST_INTERP == 0
  7025. return false;
  7026. #else
  7027. /* Fast interpreter mode */
  7028. if (!((WASMModule *)module)->is_binary_freeable)
  7029. return false;
  7030. #if WASM_ENABLE_GC != 0 && WASM_ENABLE_STRINGREF != 0
  7031. if (((WASMModule *)module)->string_literal_ptrs)
  7032. return false;
  7033. #endif
  7034. #endif
  7035. }
  7036. #endif /* WASM_ENABLE_INTERP != 0 */
  7037. #if WASM_ENABLE_AOT != 0
  7038. if (module->module_type == Wasm_Module_AoT) {
  7039. if (!((AOTModule *)module)->is_binary_freeable)
  7040. return false;
  7041. #if WASM_ENABLE_GC != 0 && WASM_ENABLE_STRINGREF != 0
  7042. if (((AOTModule *)module)->string_literal_ptrs)
  7043. return false;
  7044. #endif
  7045. }
  7046. #endif /* WASM_ENABLE_AOT != 0 */
  7047. return true;
  7048. }