wasm_runtime_common.c 251 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983
  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. static void
  1445. instantiation_args_set_defaults(struct InstantiationArgs2 *args)
  1446. {
  1447. memset(args, 0, sizeof(*args));
  1448. }
  1449. WASMModuleInstanceCommon *
  1450. wasm_runtime_instantiate_ex(WASMModuleCommon *module,
  1451. const InstantiationArgs *args, char *error_buf,
  1452. uint32 error_buf_size)
  1453. {
  1454. struct InstantiationArgs2 v2;
  1455. instantiation_args_set_defaults(&v2);
  1456. v2.v1 = *args;
  1457. return wasm_runtime_instantiate_ex2(module, &v2, error_buf, error_buf_size);
  1458. }
  1459. bool
  1460. wasm_runtime_instantiation_args_create(struct InstantiationArgs2 **p)
  1461. {
  1462. struct InstantiationArgs2 *args = wasm_runtime_malloc(sizeof(*args));
  1463. if (args == NULL) {
  1464. return false;
  1465. }
  1466. instantiation_args_set_defaults(args);
  1467. *p = args;
  1468. return true;
  1469. }
  1470. void
  1471. wasm_runtime_instantiation_args_destroy(struct InstantiationArgs2 *p)
  1472. {
  1473. wasm_runtime_free(p);
  1474. }
  1475. void
  1476. wasm_runtime_instantiation_args_set_default_stack_size(
  1477. struct InstantiationArgs2 *p, uint32 v)
  1478. {
  1479. p->v1.default_stack_size = v;
  1480. }
  1481. void
  1482. wasm_runtime_instantiation_args_set_host_managed_heap_size(
  1483. struct InstantiationArgs2 *p, uint32 v)
  1484. {
  1485. p->v1.host_managed_heap_size = v;
  1486. }
  1487. void
  1488. wasm_runtime_instantiation_args_set_max_memory_pages(
  1489. struct InstantiationArgs2 *p, uint32 v)
  1490. {
  1491. p->v1.max_memory_pages = v;
  1492. }
  1493. WASMModuleInstanceCommon *
  1494. wasm_runtime_instantiate_ex2(WASMModuleCommon *module,
  1495. const struct InstantiationArgs2 *args,
  1496. char *error_buf, uint32 error_buf_size)
  1497. {
  1498. return wasm_runtime_instantiate_internal(
  1499. module, NULL, NULL, args->v1.default_stack_size,
  1500. args->v1.host_managed_heap_size, args->v1.max_memory_pages, error_buf,
  1501. error_buf_size);
  1502. }
  1503. void
  1504. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  1505. bool is_sub_inst)
  1506. {
  1507. #if WASM_ENABLE_INTERP != 0
  1508. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1509. wasm_deinstantiate((WASMModuleInstance *)module_inst, is_sub_inst);
  1510. return;
  1511. }
  1512. #endif
  1513. #if WASM_ENABLE_AOT != 0
  1514. if (module_inst->module_type == Wasm_Module_AoT) {
  1515. aot_deinstantiate((AOTModuleInstance *)module_inst, is_sub_inst);
  1516. return;
  1517. }
  1518. #endif
  1519. }
  1520. bool
  1521. wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
  1522. RunningMode running_mode)
  1523. {
  1524. #if WASM_ENABLE_AOT != 0
  1525. if (module_inst->module_type == Wasm_Module_AoT)
  1526. return true;
  1527. #endif
  1528. #if WASM_ENABLE_INTERP != 0
  1529. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1530. WASMModuleInstance *module_inst_interp =
  1531. (WASMModuleInstance *)module_inst;
  1532. return wasm_set_running_mode(module_inst_interp, running_mode);
  1533. }
  1534. #endif
  1535. return false;
  1536. }
  1537. RunningMode
  1538. wasm_runtime_get_running_mode(wasm_module_inst_t module_inst)
  1539. {
  1540. #if WASM_ENABLE_INTERP != 0
  1541. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1542. WASMModuleInstance *module_inst_interp =
  1543. (WASMModuleInstance *)module_inst;
  1544. return module_inst_interp->e->running_mode;
  1545. }
  1546. #endif
  1547. return Mode_Default;
  1548. }
  1549. void
  1550. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst)
  1551. {
  1552. wasm_runtime_deinstantiate_internal(module_inst, false);
  1553. }
  1554. WASMModuleCommon *
  1555. wasm_runtime_get_module(WASMModuleInstanceCommon *module_inst)
  1556. {
  1557. return (WASMModuleCommon *)((WASMModuleInstance *)module_inst)->module;
  1558. }
  1559. WASMExecEnv *
  1560. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  1561. uint32 stack_size)
  1562. {
  1563. return wasm_exec_env_create(module_inst, stack_size);
  1564. }
  1565. void
  1566. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env)
  1567. {
  1568. wasm_exec_env_destroy(exec_env);
  1569. }
  1570. #if WASM_ENABLE_COPY_CALL_STACK != 0
  1571. uint32
  1572. wasm_copy_callstack(const wasm_exec_env_t exec_env, WASMCApiFrame *buffer,
  1573. const uint32 length, const uint32 skip_n, char *error_buf,
  1574. uint32_t error_buf_size)
  1575. {
  1576. /*
  1577. * Note for devs: please refrain from such modifications inside of
  1578. * wasm_copy_callstack to preserve async-signal-safety
  1579. * - any allocations/freeing memory
  1580. * - dereferencing any pointers other than: exec_env, exec_env->module_inst,
  1581. * exec_env->module_inst->module, pointers between stack's bottom and
  1582. * top_boundary For more details check wasm_copy_callstack in
  1583. * wasm_export.h
  1584. */
  1585. #if WASM_ENABLE_DUMP_CALL_STACK
  1586. WASMModuleInstance *module_inst =
  1587. (WASMModuleInstance *)get_module_inst(exec_env);
  1588. #if WASM_ENABLE_INTERP != 0
  1589. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1590. return wasm_interp_copy_callstack(exec_env, buffer, length, skip_n,
  1591. error_buf, error_buf_size);
  1592. }
  1593. #endif
  1594. #if WASM_ENABLE_AOT != 0
  1595. if (module_inst->module_type == Wasm_Module_AoT) {
  1596. return aot_copy_callstack(exec_env, buffer, length, skip_n, error_buf,
  1597. error_buf_size);
  1598. }
  1599. #endif
  1600. #endif
  1601. char *err_msg = "No copy_callstack API was actually executed";
  1602. strncpy(error_buf, err_msg, error_buf_size);
  1603. return 0;
  1604. }
  1605. #endif // WASM_ENABLE_COPY_CALL_STACK
  1606. bool
  1607. wasm_runtime_init_thread_env(void)
  1608. {
  1609. #ifdef BH_PLATFORM_WINDOWS
  1610. if (os_thread_env_init() != 0)
  1611. return false;
  1612. #endif
  1613. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1614. if (!runtime_signal_init()) {
  1615. #ifdef BH_PLATFORM_WINDOWS
  1616. os_thread_env_destroy();
  1617. #endif
  1618. return false;
  1619. }
  1620. #endif
  1621. #if WASM_ENABLE_THREAD_MGR != 0 && defined(OS_ENABLE_WAKEUP_BLOCKING_OP)
  1622. os_end_blocking_op();
  1623. #endif
  1624. return true;
  1625. }
  1626. void
  1627. wasm_runtime_destroy_thread_env(void)
  1628. {
  1629. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1630. runtime_signal_destroy();
  1631. #endif
  1632. #ifdef BH_PLATFORM_WINDOWS
  1633. os_thread_env_destroy();
  1634. #endif
  1635. }
  1636. bool
  1637. wasm_runtime_thread_env_inited(void)
  1638. {
  1639. #ifdef BH_PLATFORM_WINDOWS
  1640. if (!os_thread_env_inited())
  1641. return false;
  1642. #endif
  1643. #if WASM_ENABLE_AOT != 0
  1644. #ifdef OS_ENABLE_HW_BOUND_CHECK
  1645. if (!os_thread_signal_inited())
  1646. return false;
  1647. #endif
  1648. #endif
  1649. return true;
  1650. }
  1651. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  1652. void
  1653. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module)
  1654. {
  1655. WASMModuleMemConsumption mem_conspn = { 0 };
  1656. #if WASM_ENABLE_INTERP != 0
  1657. if (module->module_type == Wasm_Module_Bytecode) {
  1658. wasm_get_module_mem_consumption((WASMModule *)module, &mem_conspn);
  1659. }
  1660. #endif
  1661. #if WASM_ENABLE_AOT != 0
  1662. if (module->module_type == Wasm_Module_AoT) {
  1663. aot_get_module_mem_consumption((AOTModule *)module, &mem_conspn);
  1664. }
  1665. #endif
  1666. os_printf("WASM module memory consumption, total size: %u\n",
  1667. mem_conspn.total_size);
  1668. os_printf(" module struct size: %u\n", mem_conspn.module_struct_size);
  1669. os_printf(" types size: %u\n", mem_conspn.types_size);
  1670. os_printf(" imports size: %u\n", mem_conspn.imports_size);
  1671. os_printf(" funcs size: %u\n", mem_conspn.functions_size);
  1672. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  1673. os_printf(" memories size: %u\n", mem_conspn.memories_size);
  1674. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  1675. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  1676. os_printf(" table segs size: %u\n", mem_conspn.table_segs_size);
  1677. os_printf(" data segs size: %u\n", mem_conspn.data_segs_size);
  1678. os_printf(" const strings size: %u\n", mem_conspn.const_strs_size);
  1679. #if WASM_ENABLE_AOT != 0
  1680. os_printf(" aot code size: %u\n", mem_conspn.aot_code_size);
  1681. #endif
  1682. }
  1683. void
  1684. wasm_runtime_dump_module_inst_mem_consumption(
  1685. const WASMModuleInstanceCommon *module_inst)
  1686. {
  1687. WASMModuleInstMemConsumption mem_conspn = { 0 };
  1688. #if WASM_ENABLE_INTERP != 0
  1689. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1690. wasm_get_module_inst_mem_consumption((WASMModuleInstance *)module_inst,
  1691. &mem_conspn);
  1692. }
  1693. #endif
  1694. #if WASM_ENABLE_AOT != 0
  1695. if (module_inst->module_type == Wasm_Module_AoT) {
  1696. aot_get_module_inst_mem_consumption((AOTModuleInstance *)module_inst,
  1697. &mem_conspn);
  1698. }
  1699. #endif
  1700. os_printf("WASM module inst memory consumption, total size: %lu\n",
  1701. mem_conspn.total_size);
  1702. os_printf(" module inst struct size: %u\n",
  1703. mem_conspn.module_inst_struct_size);
  1704. os_printf(" memories size: %lu\n", mem_conspn.memories_size);
  1705. os_printf(" app heap size: %u\n", mem_conspn.app_heap_size);
  1706. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  1707. os_printf(" functions size: %u\n", mem_conspn.functions_size);
  1708. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  1709. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  1710. }
  1711. void
  1712. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env)
  1713. {
  1714. uint32 total_size =
  1715. offsetof(WASMExecEnv, wasm_stack_u.bottom) + exec_env->wasm_stack_size;
  1716. os_printf("Exec env memory consumption, total size: %u\n", total_size);
  1717. os_printf(" exec env struct size: %u\n",
  1718. offsetof(WASMExecEnv, wasm_stack_u.bottom));
  1719. #if WASM_ENABLE_INTERP != 0 && WASM_ENABLE_FAST_INTERP == 0
  1720. os_printf(" block addr cache size: %u\n",
  1721. sizeof(exec_env->block_addr_cache));
  1722. #endif
  1723. os_printf(" stack size: %u\n", exec_env->wasm_stack_size);
  1724. }
  1725. uint32
  1726. gc_get_heap_highmark_size(void *heap);
  1727. void
  1728. wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env)
  1729. {
  1730. WASMModuleInstMemConsumption module_inst_mem_consps;
  1731. WASMModuleMemConsumption module_mem_consps;
  1732. WASMModuleInstanceCommon *module_inst_common;
  1733. WASMModuleCommon *module_common = NULL;
  1734. void *heap_handle = NULL;
  1735. uint32 app_heap_peak_size = 0;
  1736. uint32 max_aux_stack_used = -1;
  1737. uint64 total_size = 0;
  1738. module_inst_common = exec_env->module_inst;
  1739. #if WASM_ENABLE_INTERP != 0
  1740. if (module_inst_common->module_type == Wasm_Module_Bytecode) {
  1741. WASMModuleInstance *wasm_module_inst =
  1742. (WASMModuleInstance *)module_inst_common;
  1743. WASMModule *wasm_module = wasm_module_inst->module;
  1744. module_common = (WASMModuleCommon *)wasm_module;
  1745. if (wasm_module_inst->memories) {
  1746. heap_handle = wasm_module_inst->memories[0]->heap_handle;
  1747. }
  1748. wasm_get_module_inst_mem_consumption(wasm_module_inst,
  1749. &module_inst_mem_consps);
  1750. wasm_get_module_mem_consumption(wasm_module, &module_mem_consps);
  1751. if (wasm_module_inst->module->aux_stack_top_global_index != (uint32)-1)
  1752. max_aux_stack_used = wasm_module_inst->e->max_aux_stack_used;
  1753. }
  1754. #endif
  1755. #if WASM_ENABLE_AOT != 0
  1756. if (module_inst_common->module_type == Wasm_Module_AoT) {
  1757. AOTModuleInstance *aot_module_inst =
  1758. (AOTModuleInstance *)module_inst_common;
  1759. AOTModule *aot_module = (AOTModule *)aot_module_inst->module;
  1760. module_common = (WASMModuleCommon *)aot_module;
  1761. if (aot_module_inst->memories) {
  1762. AOTMemoryInstance **memories = aot_module_inst->memories;
  1763. heap_handle = memories[0]->heap_handle;
  1764. }
  1765. aot_get_module_inst_mem_consumption(aot_module_inst,
  1766. &module_inst_mem_consps);
  1767. aot_get_module_mem_consumption(aot_module, &module_mem_consps);
  1768. }
  1769. #endif
  1770. bh_assert(module_common != NULL);
  1771. if (heap_handle) {
  1772. app_heap_peak_size = gc_get_heap_highmark_size(heap_handle);
  1773. }
  1774. total_size = offsetof(WASMExecEnv, wasm_stack_u.bottom)
  1775. + exec_env->wasm_stack_size + module_mem_consps.total_size
  1776. + module_inst_mem_consps.total_size;
  1777. os_printf("\nMemory consumption summary (bytes):\n");
  1778. wasm_runtime_dump_module_mem_consumption(module_common);
  1779. wasm_runtime_dump_module_inst_mem_consumption(module_inst_common);
  1780. wasm_runtime_dump_exec_env_mem_consumption(exec_env);
  1781. os_printf("\nTotal memory consumption of module, module inst and "
  1782. "exec env: %" PRIu64 "\n",
  1783. total_size);
  1784. os_printf("Total interpreter stack used: %u\n",
  1785. exec_env->max_wasm_stack_used);
  1786. if (max_aux_stack_used != (uint32)-1)
  1787. os_printf("Total auxiliary stack used: %u\n", max_aux_stack_used);
  1788. else
  1789. os_printf("Total aux stack used: no enough info to profile\n");
  1790. /*
  1791. * Report the native stack usage estimation.
  1792. *
  1793. * Unlike the aux stack above, we report the amount unused
  1794. * because we don't know the stack "bottom".
  1795. *
  1796. * Note that this is just about what the runtime itself observed.
  1797. * It doesn't cover host func implementations, signal handlers, etc.
  1798. */
  1799. if (exec_env->native_stack_top_min != (void *)UINTPTR_MAX)
  1800. os_printf("Native stack left: %zd\n",
  1801. exec_env->native_stack_top_min
  1802. - exec_env->native_stack_boundary);
  1803. else
  1804. os_printf("Native stack left: no enough info to profile\n");
  1805. os_printf("Total app heap used: %u\n", app_heap_peak_size);
  1806. }
  1807. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
  1808. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  1809. #if WASM_ENABLE_PERF_PROFILING != 0
  1810. void
  1811. wasm_runtime_dump_perf_profiling(WASMModuleInstanceCommon *module_inst)
  1812. {
  1813. #if WASM_ENABLE_INTERP != 0
  1814. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1815. wasm_dump_perf_profiling((WASMModuleInstance *)module_inst);
  1816. }
  1817. #endif
  1818. #if WASM_ENABLE_AOT != 0
  1819. if (module_inst->module_type == Wasm_Module_AoT) {
  1820. aot_dump_perf_profiling((AOTModuleInstance *)module_inst);
  1821. }
  1822. #endif
  1823. }
  1824. double
  1825. wasm_runtime_sum_wasm_exec_time(WASMModuleInstanceCommon *inst)
  1826. {
  1827. #if WASM_ENABLE_INTERP != 0
  1828. if (inst->module_type == Wasm_Module_Bytecode)
  1829. return wasm_summarize_wasm_execute_time((WASMModuleInstance *)inst);
  1830. #endif
  1831. #if WASM_ENABLE_AOT != 0
  1832. if (inst->module_type == Wasm_Module_AoT)
  1833. return aot_summarize_wasm_execute_time((AOTModuleInstance *)inst);
  1834. #endif
  1835. return 0.0;
  1836. }
  1837. double
  1838. wasm_runtime_get_wasm_func_exec_time(WASMModuleInstanceCommon *inst,
  1839. const char *func_name)
  1840. {
  1841. #if WASM_ENABLE_INTERP != 0
  1842. if (inst->module_type == Wasm_Module_Bytecode)
  1843. return wasm_get_wasm_func_exec_time((WASMModuleInstance *)inst,
  1844. func_name);
  1845. #endif
  1846. #if WASM_ENABLE_AOT != 0
  1847. if (inst->module_type == Wasm_Module_AoT)
  1848. return aot_get_wasm_func_exec_time((AOTModuleInstance *)inst,
  1849. func_name);
  1850. #endif
  1851. return 0.0;
  1852. }
  1853. #endif /* WASM_ENABLE_PERF_PROFILING != 0 */
  1854. WASMModuleInstanceCommon *
  1855. wasm_runtime_get_module_inst(WASMExecEnv *exec_env)
  1856. {
  1857. return wasm_exec_env_get_module_inst(exec_env);
  1858. }
  1859. void
  1860. wasm_runtime_set_module_inst(WASMExecEnv *exec_env,
  1861. WASMModuleInstanceCommon *const module_inst)
  1862. {
  1863. wasm_exec_env_set_module_inst(exec_env, module_inst);
  1864. }
  1865. bool
  1866. wasm_runtime_get_export_global_inst(WASMModuleInstanceCommon *const module_inst,
  1867. char const *name,
  1868. wasm_global_inst_t *global_inst)
  1869. {
  1870. #if WASM_ENABLE_INTERP != 0
  1871. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1872. const WASMModuleInstance *wasm_module_inst =
  1873. (const WASMModuleInstance *)module_inst;
  1874. const WASMModule *wasm_module = wasm_module_inst->module;
  1875. uint32 i;
  1876. for (i = 0; i < wasm_module->export_count; i++) {
  1877. const WASMExport *wasm_export = &wasm_module->exports[i];
  1878. if ((wasm_export->kind == WASM_IMPORT_EXPORT_KIND_GLOBAL)
  1879. && !strcmp(wasm_export->name, name)) {
  1880. const WASMModuleInstanceExtra *e =
  1881. (WASMModuleInstanceExtra *)wasm_module_inst->e;
  1882. const WASMGlobalInstance *global =
  1883. &e->globals[wasm_export->index];
  1884. global_inst->kind = val_type_to_val_kind(global->type);
  1885. global_inst->is_mutable = global->is_mutable;
  1886. #if WASM_ENABLE_MULTI_MODULE == 0
  1887. global_inst->global_data =
  1888. wasm_module_inst->global_data + global->data_offset;
  1889. #else
  1890. global_inst->global_data =
  1891. global->import_global_inst
  1892. ? global->import_module_inst->global_data
  1893. + global->import_global_inst->data_offset
  1894. : wasm_module_inst->global_data + global->data_offset;
  1895. #endif
  1896. return true;
  1897. }
  1898. }
  1899. }
  1900. #endif
  1901. #if WASM_ENABLE_AOT != 0
  1902. if (module_inst->module_type == Wasm_Module_AoT) {
  1903. const AOTModuleInstance *aot_module_inst =
  1904. (AOTModuleInstance *)module_inst;
  1905. const AOTModule *aot_module = (AOTModule *)aot_module_inst->module;
  1906. uint32 i;
  1907. for (i = 0; i < aot_module->export_count; i++) {
  1908. const AOTExport *aot_export = &aot_module->exports[i];
  1909. if ((aot_export->kind == WASM_IMPORT_EXPORT_KIND_GLOBAL)
  1910. && !strcmp(aot_export->name, name)) {
  1911. const AOTGlobal *global =
  1912. &aot_module->globals[aot_export->index];
  1913. global_inst->kind = val_type_to_val_kind(global->type.val_type);
  1914. global_inst->is_mutable = global->type.is_mutable;
  1915. global_inst->global_data =
  1916. aot_module_inst->global_data + global->data_offset;
  1917. return true;
  1918. }
  1919. }
  1920. }
  1921. #endif
  1922. return false;
  1923. }
  1924. bool
  1925. wasm_runtime_get_export_table_inst(WASMModuleInstanceCommon *const module_inst,
  1926. char const *name,
  1927. wasm_table_inst_t *table_inst)
  1928. {
  1929. #if WASM_ENABLE_INTERP != 0
  1930. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1931. const WASMModuleInstance *wasm_module_inst =
  1932. (const WASMModuleInstance *)module_inst;
  1933. const WASMModule *wasm_module = wasm_module_inst->module;
  1934. uint32 i;
  1935. for (i = 0; i < wasm_module->export_count; i++) {
  1936. const WASMExport *wasm_export = &wasm_module->exports[i];
  1937. if ((wasm_export->kind == WASM_IMPORT_EXPORT_KIND_TABLE)
  1938. && !strcmp(wasm_export->name, name)) {
  1939. const WASMTableInstance *wasm_table_inst =
  1940. wasm_module_inst->tables[wasm_export->index];
  1941. table_inst->elem_kind =
  1942. val_type_to_val_kind(wasm_table_inst->elem_type);
  1943. table_inst->cur_size = wasm_table_inst->cur_size;
  1944. table_inst->max_size = wasm_table_inst->max_size;
  1945. table_inst->elems = (void *)wasm_table_inst->elems;
  1946. return true;
  1947. }
  1948. }
  1949. }
  1950. #endif
  1951. #if WASM_ENABLE_AOT != 0
  1952. if (module_inst->module_type == Wasm_Module_AoT) {
  1953. const AOTModuleInstance *aot_module_inst =
  1954. (AOTModuleInstance *)module_inst;
  1955. const AOTModule *aot_module = (AOTModule *)aot_module_inst->module;
  1956. uint32 i;
  1957. for (i = 0; i < aot_module->export_count; i++) {
  1958. const AOTExport *aot_export = &aot_module->exports[i];
  1959. if ((aot_export->kind == WASM_IMPORT_EXPORT_KIND_TABLE)
  1960. && !strcmp(aot_export->name, name)) {
  1961. const AOTTableInstance *aot_table_inst =
  1962. aot_module_inst->tables[aot_export->index];
  1963. table_inst->elem_kind =
  1964. val_type_to_val_kind(aot_table_inst->elem_type);
  1965. table_inst->cur_size = aot_table_inst->cur_size;
  1966. table_inst->max_size = aot_table_inst->max_size;
  1967. table_inst->elems = (void *)aot_table_inst->elems;
  1968. return true;
  1969. }
  1970. }
  1971. }
  1972. #endif
  1973. return false;
  1974. }
  1975. WASMFunctionInstanceCommon *
  1976. wasm_table_get_func_inst(struct WASMModuleInstanceCommon *const module_inst,
  1977. const wasm_table_inst_t *table_inst, uint32_t idx)
  1978. {
  1979. if (!table_inst) {
  1980. bh_assert(0);
  1981. return NULL;
  1982. }
  1983. if (idx >= table_inst->cur_size) {
  1984. bh_assert(0);
  1985. return NULL;
  1986. }
  1987. #if WASM_ENABLE_INTERP != 0
  1988. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1989. const WASMModuleInstance *wasm_module_inst =
  1990. (const WASMModuleInstance *)module_inst;
  1991. table_elem_type_t tbl_elem_val =
  1992. ((table_elem_type_t *)table_inst->elems)[idx];
  1993. if (tbl_elem_val == NULL_REF) {
  1994. return NULL;
  1995. }
  1996. #if WASM_ENABLE_GC == 0
  1997. uint32 func_idx = (uint32)tbl_elem_val;
  1998. #else
  1999. uint32 func_idx =
  2000. wasm_func_obj_get_func_idx_bound((WASMFuncObjectRef)tbl_elem_val);
  2001. #endif
  2002. bh_assert(func_idx < wasm_module_inst->e->function_count);
  2003. return wasm_module_inst->e->functions + func_idx;
  2004. }
  2005. #endif
  2006. #if WASM_ENABLE_AOT != 0
  2007. if (module_inst->module_type == Wasm_Module_AoT) {
  2008. AOTModuleInstance *aot_module_inst = (AOTModuleInstance *)module_inst;
  2009. uint32 func_idx;
  2010. table_elem_type_t tbl_elem_val =
  2011. ((table_elem_type_t *)table_inst->elems)[idx];
  2012. if (tbl_elem_val == NULL_REF) {
  2013. return NULL;
  2014. }
  2015. #if WASM_ENABLE_GC == 0
  2016. func_idx = (uint32)tbl_elem_val;
  2017. #else
  2018. func_idx =
  2019. wasm_func_obj_get_func_idx_bound((WASMFuncObjectRef)tbl_elem_val);
  2020. #endif
  2021. return aot_get_function_instance(aot_module_inst, func_idx);
  2022. }
  2023. #endif
  2024. return NULL;
  2025. }
  2026. void *
  2027. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env)
  2028. {
  2029. return exec_env->attachment;
  2030. }
  2031. void
  2032. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data)
  2033. {
  2034. exec_env->user_data = user_data;
  2035. }
  2036. void *
  2037. wasm_runtime_get_user_data(WASMExecEnv *exec_env)
  2038. {
  2039. return exec_env->user_data;
  2040. }
  2041. void
  2042. wasm_runtime_set_native_stack_boundary(WASMExecEnv *exec_env,
  2043. uint8 *native_stack_boundary)
  2044. {
  2045. exec_env->user_native_stack_boundary = native_stack_boundary;
  2046. }
  2047. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2048. void
  2049. wasm_runtime_access_exce_check_guard_page()
  2050. {
  2051. if (exec_env_tls && exec_env_tls->handle == os_self_thread()) {
  2052. uint32 page_size = os_getpagesize();
  2053. memset(exec_env_tls->exce_check_guard_page, 0, page_size);
  2054. }
  2055. }
  2056. #endif
  2057. #if WASM_ENABLE_INSTRUCTION_METERING != 0
  2058. void
  2059. wasm_runtime_set_instruction_count_limit(WASMExecEnv *exec_env,
  2060. int instructions_to_execute)
  2061. {
  2062. exec_env->instructions_to_execute = instructions_to_execute;
  2063. }
  2064. #endif
  2065. WASMFuncType *
  2066. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  2067. uint32 module_type)
  2068. {
  2069. WASMFuncType *type = NULL;
  2070. #if WASM_ENABLE_INTERP != 0
  2071. if (module_type == Wasm_Module_Bytecode) {
  2072. WASMFunctionInstance *wasm_func = (WASMFunctionInstance *)function;
  2073. type = wasm_func->is_import_func ? wasm_func->u.func_import->func_type
  2074. : wasm_func->u.func->func_type;
  2075. }
  2076. #endif
  2077. #if WASM_ENABLE_AOT != 0
  2078. if (module_type == Wasm_Module_AoT) {
  2079. AOTFunctionInstance *aot_func = (AOTFunctionInstance *)function;
  2080. type = aot_func->is_import_func ? aot_func->u.func_import->func_type
  2081. : aot_func->u.func.func_type;
  2082. }
  2083. #endif
  2084. return type;
  2085. }
  2086. WASMFunctionInstanceCommon *
  2087. wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
  2088. const char *name)
  2089. {
  2090. #if WASM_ENABLE_INTERP != 0
  2091. if (module_inst->module_type == Wasm_Module_Bytecode)
  2092. return (WASMFunctionInstanceCommon *)wasm_lookup_function(
  2093. (const WASMModuleInstance *)module_inst, name);
  2094. #endif
  2095. #if WASM_ENABLE_AOT != 0
  2096. if (module_inst->module_type == Wasm_Module_AoT)
  2097. return (WASMFunctionInstanceCommon *)aot_lookup_function(
  2098. (const AOTModuleInstance *)module_inst, name);
  2099. #endif
  2100. return NULL;
  2101. }
  2102. uint32
  2103. wasm_func_get_param_count(WASMFunctionInstanceCommon *const func_inst,
  2104. WASMModuleInstanceCommon *const module_inst)
  2105. {
  2106. WASMFuncType *type =
  2107. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  2108. bh_assert(type);
  2109. return type->param_count;
  2110. }
  2111. uint32
  2112. wasm_func_get_result_count(WASMFunctionInstanceCommon *const func_inst,
  2113. WASMModuleInstanceCommon *const module_inst)
  2114. {
  2115. WASMFuncType *type =
  2116. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  2117. bh_assert(type);
  2118. return type->result_count;
  2119. }
  2120. static uint8
  2121. val_type_to_val_kind(uint8 value_type)
  2122. {
  2123. switch (value_type) {
  2124. case VALUE_TYPE_I32:
  2125. return WASM_I32;
  2126. case VALUE_TYPE_I64:
  2127. return WASM_I64;
  2128. case VALUE_TYPE_F32:
  2129. return WASM_F32;
  2130. case VALUE_TYPE_F64:
  2131. return WASM_F64;
  2132. case VALUE_TYPE_V128:
  2133. return WASM_V128;
  2134. case VALUE_TYPE_FUNCREF:
  2135. return WASM_FUNCREF;
  2136. case VALUE_TYPE_EXTERNREF:
  2137. return WASM_EXTERNREF;
  2138. default:
  2139. bh_assert(0);
  2140. return 0;
  2141. }
  2142. }
  2143. void
  2144. wasm_func_get_param_types(WASMFunctionInstanceCommon *const func_inst,
  2145. WASMModuleInstanceCommon *const module_inst,
  2146. wasm_valkind_t *param_types)
  2147. {
  2148. WASMFuncType *type =
  2149. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  2150. uint32 i;
  2151. bh_assert(type);
  2152. for (i = 0; i < type->param_count; i++) {
  2153. param_types[i] = val_type_to_val_kind(type->types[i]);
  2154. }
  2155. }
  2156. void
  2157. wasm_func_get_result_types(WASMFunctionInstanceCommon *const func_inst,
  2158. WASMModuleInstanceCommon *const module_inst,
  2159. wasm_valkind_t *result_types)
  2160. {
  2161. WASMFuncType *type =
  2162. wasm_runtime_get_function_type(func_inst, module_inst->module_type);
  2163. uint32 i;
  2164. bh_assert(type);
  2165. for (i = 0; i < type->result_count; i++) {
  2166. result_types[i] =
  2167. val_type_to_val_kind(type->types[type->param_count + i]);
  2168. }
  2169. }
  2170. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2171. /* (uintptr_t)externref -> (uint32)index */
  2172. /* argv -> *ret_argv */
  2173. static bool
  2174. wasm_runtime_prepare_call_function(WASMExecEnv *exec_env,
  2175. WASMFunctionInstanceCommon *function,
  2176. uint32 *argv, uint32 argc, uint32 **ret_argv,
  2177. uint32 *ret_argc_param,
  2178. uint32 *ret_argc_result)
  2179. {
  2180. uint32 *new_argv = NULL, argv_i = 0, new_argv_i = 0, param_i = 0,
  2181. result_i = 0;
  2182. bool need_param_transform = false, need_result_transform = false;
  2183. uint64 size = 0;
  2184. WASMFuncType *func_type = wasm_runtime_get_function_type(
  2185. function, exec_env->module_inst->module_type);
  2186. bh_assert(func_type);
  2187. *ret_argc_param = func_type->param_cell_num;
  2188. *ret_argc_result = func_type->ret_cell_num;
  2189. for (param_i = 0; param_i < func_type->param_count; param_i++) {
  2190. if (VALUE_TYPE_EXTERNREF == func_type->types[param_i]) {
  2191. need_param_transform = true;
  2192. }
  2193. }
  2194. for (result_i = 0; result_i < func_type->result_count; result_i++) {
  2195. if (VALUE_TYPE_EXTERNREF
  2196. == func_type->types[func_type->param_count + result_i]) {
  2197. need_result_transform = true;
  2198. }
  2199. }
  2200. if (!need_param_transform && !need_result_transform) {
  2201. *ret_argv = argv;
  2202. return true;
  2203. }
  2204. if (func_type->param_cell_num >= func_type->ret_cell_num) {
  2205. size = sizeof(uint32) * func_type->param_cell_num;
  2206. }
  2207. else {
  2208. size = sizeof(uint32) * func_type->ret_cell_num;
  2209. }
  2210. if (!(new_argv = runtime_malloc(size, exec_env->module_inst, NULL, 0))) {
  2211. return false;
  2212. }
  2213. if (!need_param_transform) {
  2214. bh_memcpy_s(new_argv, (uint32)size, argv, (uint32)size);
  2215. }
  2216. else {
  2217. for (param_i = 0; param_i < func_type->param_count && argv_i < argc
  2218. && new_argv_i < func_type->param_cell_num;
  2219. param_i++) {
  2220. uint8 param_type = func_type->types[param_i];
  2221. if (VALUE_TYPE_EXTERNREF == param_type) {
  2222. void *externref_obj;
  2223. uint32 externref_index;
  2224. #if UINTPTR_MAX == UINT32_MAX
  2225. externref_obj = (void *)argv[argv_i];
  2226. #else
  2227. union {
  2228. uintptr_t val;
  2229. uint32 parts[2];
  2230. } u;
  2231. u.parts[0] = argv[argv_i];
  2232. u.parts[1] = argv[argv_i + 1];
  2233. externref_obj = (void *)u.val;
  2234. #endif
  2235. if (!wasm_externref_obj2ref(exec_env->module_inst,
  2236. externref_obj, &externref_index)) {
  2237. wasm_runtime_free(new_argv);
  2238. return false;
  2239. }
  2240. new_argv[new_argv_i] = externref_index;
  2241. argv_i += sizeof(uintptr_t) / sizeof(uint32);
  2242. new_argv_i++;
  2243. }
  2244. else {
  2245. uint16 param_cell_num = wasm_value_type_cell_num(param_type);
  2246. uint32 param_size = sizeof(uint32) * param_cell_num;
  2247. bh_memcpy_s(new_argv + new_argv_i, param_size, argv + argv_i,
  2248. param_size);
  2249. argv_i += param_cell_num;
  2250. new_argv_i += param_cell_num;
  2251. }
  2252. }
  2253. }
  2254. *ret_argv = new_argv;
  2255. return true;
  2256. }
  2257. /* (uintptr_t)externref <- (uint32)index */
  2258. /* argv <- new_argv */
  2259. static bool
  2260. wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
  2261. WASMFunctionInstanceCommon *function,
  2262. uint32 *argv, uint32 argc, uint32 *ret_argv)
  2263. {
  2264. uint32 argv_i = 0, result_i = 0, ret_argv_i = 0;
  2265. WASMFuncType *func_type;
  2266. bh_assert((argv && ret_argv) || (argc == 0));
  2267. if (argv == ret_argv) {
  2268. /* no need to transform externref results */
  2269. return true;
  2270. }
  2271. func_type = wasm_runtime_get_function_type(
  2272. function, exec_env->module_inst->module_type);
  2273. bh_assert(func_type);
  2274. for (result_i = 0; result_i < func_type->result_count && argv_i < argc;
  2275. result_i++) {
  2276. uint8 result_type = func_type->types[func_type->param_count + result_i];
  2277. if (result_type == VALUE_TYPE_EXTERNREF) {
  2278. void *externref_obj;
  2279. #if UINTPTR_MAX != UINT32_MAX
  2280. union {
  2281. uintptr_t val;
  2282. uint32 parts[2];
  2283. } u;
  2284. #endif
  2285. if (!wasm_externref_ref2obj(argv[argv_i], &externref_obj)) {
  2286. wasm_runtime_free(argv);
  2287. return false;
  2288. }
  2289. #if UINTPTR_MAX == UINT32_MAX
  2290. ret_argv[ret_argv_i] = (uintptr_t)externref_obj;
  2291. #else
  2292. u.val = (uintptr_t)externref_obj;
  2293. ret_argv[ret_argv_i] = u.parts[0];
  2294. ret_argv[ret_argv_i + 1] = u.parts[1];
  2295. #endif
  2296. argv_i += 1;
  2297. ret_argv_i += sizeof(uintptr_t) / sizeof(uint32);
  2298. }
  2299. else {
  2300. uint16 result_cell_num = wasm_value_type_cell_num(result_type);
  2301. uint32 result_size = sizeof(uint32) * result_cell_num;
  2302. bh_memcpy_s(ret_argv + ret_argv_i, result_size, argv + argv_i,
  2303. result_size);
  2304. argv_i += result_cell_num;
  2305. ret_argv_i += result_cell_num;
  2306. }
  2307. }
  2308. wasm_runtime_free(argv);
  2309. return true;
  2310. }
  2311. #endif
  2312. bool
  2313. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  2314. WASMFunctionInstanceCommon *function, uint32 argc,
  2315. uint32 argv[])
  2316. {
  2317. bool ret = false;
  2318. uint32 *new_argv = NULL, param_argc;
  2319. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2320. uint32 result_argc = 0;
  2321. #endif
  2322. if (!wasm_runtime_exec_env_check(exec_env)) {
  2323. LOG_ERROR("Invalid exec env stack info.");
  2324. return false;
  2325. }
  2326. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2327. if (!wasm_runtime_prepare_call_function(exec_env, function, argv, argc,
  2328. &new_argv, &param_argc,
  2329. &result_argc)) {
  2330. wasm_runtime_set_exception(exec_env->module_inst,
  2331. "the arguments conversion is failed");
  2332. return false;
  2333. }
  2334. #else
  2335. new_argv = argv;
  2336. param_argc = argc;
  2337. #endif
  2338. #if WASM_ENABLE_INTERP != 0
  2339. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  2340. ret = wasm_call_function(exec_env, (WASMFunctionInstance *)function,
  2341. param_argc, new_argv);
  2342. #endif
  2343. #if WASM_ENABLE_AOT != 0
  2344. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  2345. ret = aot_call_function(exec_env, (AOTFunctionInstance *)function,
  2346. param_argc, new_argv);
  2347. #endif
  2348. if (!ret) {
  2349. if (new_argv != argv) {
  2350. wasm_runtime_free(new_argv);
  2351. }
  2352. return false;
  2353. }
  2354. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2355. if (!wasm_runtime_finalize_call_function(exec_env, function, new_argv,
  2356. result_argc, argv)) {
  2357. wasm_runtime_set_exception(exec_env->module_inst,
  2358. "the result conversion is failed");
  2359. return false;
  2360. }
  2361. #endif
  2362. return ret;
  2363. }
  2364. static void
  2365. parse_args_to_uint32_array(WASMFuncType *type, wasm_val_t *args,
  2366. uint32 *out_argv)
  2367. {
  2368. uint32 i, p;
  2369. for (i = 0, p = 0; i < type->param_count; i++) {
  2370. switch (args[i].kind) {
  2371. case WASM_I32:
  2372. out_argv[p++] = args[i].of.i32;
  2373. break;
  2374. case WASM_I64:
  2375. {
  2376. union {
  2377. uint64 val;
  2378. uint32 parts[2];
  2379. } u;
  2380. u.val = args[i].of.i64;
  2381. out_argv[p++] = u.parts[0];
  2382. out_argv[p++] = u.parts[1];
  2383. break;
  2384. }
  2385. case WASM_F32:
  2386. {
  2387. union {
  2388. float32 val;
  2389. uint32 part;
  2390. } u;
  2391. u.val = args[i].of.f32;
  2392. out_argv[p++] = u.part;
  2393. break;
  2394. }
  2395. case WASM_F64:
  2396. {
  2397. union {
  2398. float64 val;
  2399. uint32 parts[2];
  2400. } u;
  2401. u.val = args[i].of.f64;
  2402. out_argv[p++] = u.parts[0];
  2403. out_argv[p++] = u.parts[1];
  2404. break;
  2405. }
  2406. case WASM_V128:
  2407. {
  2408. bh_assert(0);
  2409. break;
  2410. }
  2411. #if WASM_ENABLE_REF_TYPES != 0
  2412. #if WASM_ENABLE_GC == 0
  2413. case WASM_FUNCREF:
  2414. {
  2415. out_argv[p++] = args[i].of.i32;
  2416. break;
  2417. }
  2418. #else
  2419. case WASM_FUNCREF:
  2420. #endif
  2421. case WASM_EXTERNREF:
  2422. {
  2423. #if UINTPTR_MAX == UINT32_MAX
  2424. out_argv[p++] = args[i].of.foreign;
  2425. #else
  2426. union {
  2427. uintptr_t val;
  2428. uint32 parts[2];
  2429. } u;
  2430. u.val = (uintptr_t)args[i].of.foreign;
  2431. out_argv[p++] = u.parts[0];
  2432. out_argv[p++] = u.parts[1];
  2433. #endif
  2434. break;
  2435. }
  2436. #endif
  2437. default:
  2438. bh_assert(0);
  2439. break;
  2440. }
  2441. }
  2442. }
  2443. static void
  2444. parse_uint32_array_to_results(WASMFuncType *type, uint32 *argv,
  2445. wasm_val_t *out_results)
  2446. {
  2447. uint32 i, p;
  2448. for (i = 0, p = 0; i < type->result_count; i++) {
  2449. switch (type->types[type->param_count + i]) {
  2450. case VALUE_TYPE_I32:
  2451. out_results[i].kind = WASM_I32;
  2452. out_results[i].of.i32 = (int32)argv[p++];
  2453. break;
  2454. case VALUE_TYPE_I64:
  2455. {
  2456. union {
  2457. uint64 val;
  2458. uint32 parts[2];
  2459. } u;
  2460. u.parts[0] = argv[p++];
  2461. u.parts[1] = argv[p++];
  2462. out_results[i].kind = WASM_I64;
  2463. out_results[i].of.i64 = u.val;
  2464. break;
  2465. }
  2466. case VALUE_TYPE_F32:
  2467. {
  2468. union {
  2469. float32 val;
  2470. uint32 part;
  2471. } u;
  2472. u.part = argv[p++];
  2473. out_results[i].kind = WASM_F32;
  2474. out_results[i].of.f32 = u.val;
  2475. break;
  2476. }
  2477. case VALUE_TYPE_F64:
  2478. {
  2479. union {
  2480. float64 val;
  2481. uint32 parts[2];
  2482. } u;
  2483. u.parts[0] = argv[p++];
  2484. u.parts[1] = argv[p++];
  2485. out_results[i].kind = WASM_F64;
  2486. out_results[i].of.f64 = u.val;
  2487. break;
  2488. }
  2489. case VALUE_TYPE_V128:
  2490. {
  2491. bh_assert(0);
  2492. break;
  2493. }
  2494. #if WASM_ENABLE_REF_TYPES != 0
  2495. #if WASM_ENABLE_GC == 0
  2496. case VALUE_TYPE_FUNCREF:
  2497. {
  2498. out_results[i].kind = WASM_I32;
  2499. out_results[i].of.i32 = (int32)argv[p++];
  2500. break;
  2501. }
  2502. case VALUE_TYPE_EXTERNREF:
  2503. #else
  2504. case REF_TYPE_FUNCREF:
  2505. case REF_TYPE_EXTERNREF:
  2506. case REF_TYPE_ANYREF:
  2507. case REF_TYPE_EQREF:
  2508. case REF_TYPE_HT_NULLABLE:
  2509. case REF_TYPE_HT_NON_NULLABLE:
  2510. case REF_TYPE_I31REF:
  2511. case REF_TYPE_NULLFUNCREF:
  2512. case REF_TYPE_NULLEXTERNREF:
  2513. case REF_TYPE_STRUCTREF:
  2514. case REF_TYPE_ARRAYREF:
  2515. case REF_TYPE_NULLREF:
  2516. #endif /* end of WASM_ENABLE_GC == 0 */
  2517. {
  2518. #if UINTPTR_MAX == UINT32_MAX
  2519. out_results[i].kind = WASM_EXTERNREF;
  2520. out_results[i].of.foreign = (uintptr_t)argv[p++];
  2521. #else
  2522. union {
  2523. uintptr_t val;
  2524. uint32 parts[2];
  2525. } u;
  2526. u.parts[0] = argv[p++];
  2527. u.parts[1] = argv[p++];
  2528. out_results[i].kind = WASM_EXTERNREF;
  2529. out_results[i].of.foreign = u.val;
  2530. #endif
  2531. break;
  2532. }
  2533. #endif /* end of WASM_ENABLE_REF_TYPES != 0 */
  2534. default:
  2535. bh_assert(0);
  2536. break;
  2537. }
  2538. }
  2539. }
  2540. bool
  2541. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  2542. WASMFunctionInstanceCommon *function,
  2543. uint32 num_results, wasm_val_t results[],
  2544. uint32 num_args, wasm_val_t args[])
  2545. {
  2546. uint32 argc, argv_buf[16] = { 0 }, *argv = argv_buf, cell_num, module_type;
  2547. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2548. uint32 i, param_size_in_double_world = 0, result_size_in_double_world = 0;
  2549. #endif
  2550. uint64 total_size;
  2551. WASMFuncType *type;
  2552. bool ret = false;
  2553. module_type = exec_env->module_inst->module_type;
  2554. type = wasm_runtime_get_function_type(function, module_type);
  2555. if (!type) {
  2556. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT must be "
  2557. "enabled at least one.");
  2558. goto fail1;
  2559. }
  2560. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2561. for (i = 0; i < type->param_count; i++) {
  2562. param_size_in_double_world +=
  2563. wasm_value_type_cell_num_outside(type->types[i]);
  2564. }
  2565. for (i = 0; i < type->result_count; i++) {
  2566. result_size_in_double_world += wasm_value_type_cell_num_outside(
  2567. type->types[type->param_count + i]);
  2568. }
  2569. argc = param_size_in_double_world;
  2570. cell_num = (argc >= result_size_in_double_world)
  2571. ? argc
  2572. : result_size_in_double_world;
  2573. #else
  2574. argc = type->param_cell_num;
  2575. cell_num = (argc > type->ret_cell_num) ? argc : type->ret_cell_num;
  2576. #endif
  2577. if (num_results != type->result_count) {
  2578. LOG_ERROR(
  2579. "The result value number does not match the function declaration.");
  2580. goto fail1;
  2581. }
  2582. if (num_args != type->param_count) {
  2583. LOG_ERROR("The argument value number does not match the function "
  2584. "declaration.");
  2585. goto fail1;
  2586. }
  2587. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  2588. if (total_size > sizeof(argv_buf)) {
  2589. if (!(argv =
  2590. runtime_malloc(total_size, exec_env->module_inst, NULL, 0))) {
  2591. goto fail1;
  2592. }
  2593. }
  2594. parse_args_to_uint32_array(type, args, argv);
  2595. if (!(ret = wasm_runtime_call_wasm(exec_env, function, argc, argv)))
  2596. goto fail2;
  2597. parse_uint32_array_to_results(type, argv, results);
  2598. fail2:
  2599. if (argv != argv_buf)
  2600. wasm_runtime_free(argv);
  2601. fail1:
  2602. return ret;
  2603. }
  2604. bool
  2605. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  2606. WASMFunctionInstanceCommon *function,
  2607. uint32 num_results, wasm_val_t results[],
  2608. uint32 num_args, ...)
  2609. {
  2610. wasm_val_t args_buf[8] = { 0 }, *args = args_buf;
  2611. WASMFuncType *type = NULL;
  2612. bool ret = false;
  2613. uint64 total_size;
  2614. uint32 i = 0, module_type;
  2615. va_list vargs;
  2616. module_type = exec_env->module_inst->module_type;
  2617. type = wasm_runtime_get_function_type(function, module_type);
  2618. if (!type) {
  2619. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT "
  2620. "must be enabled at least one.");
  2621. goto fail1;
  2622. }
  2623. if (num_args != type->param_count) {
  2624. LOG_ERROR("The argument value number does not match the "
  2625. "function declaration.");
  2626. goto fail1;
  2627. }
  2628. total_size = sizeof(wasm_val_t) * (uint64)num_args;
  2629. if (total_size > sizeof(args_buf)) {
  2630. if (!(args =
  2631. runtime_malloc(total_size, exec_env->module_inst, NULL, 0))) {
  2632. goto fail1;
  2633. }
  2634. }
  2635. va_start(vargs, num_args);
  2636. for (i = 0; i < num_args; i++) {
  2637. switch (type->types[i]) {
  2638. case VALUE_TYPE_I32:
  2639. args[i].kind = WASM_I32;
  2640. args[i].of.i32 = va_arg(vargs, uint32);
  2641. break;
  2642. case VALUE_TYPE_I64:
  2643. args[i].kind = WASM_I64;
  2644. args[i].of.i64 = va_arg(vargs, uint64);
  2645. break;
  2646. case VALUE_TYPE_F32:
  2647. args[i].kind = WASM_F32;
  2648. args[i].of.f32 = (float32)va_arg(vargs, float64);
  2649. break;
  2650. case VALUE_TYPE_F64:
  2651. args[i].kind = WASM_F64;
  2652. args[i].of.f64 = va_arg(vargs, float64);
  2653. break;
  2654. case VALUE_TYPE_V128:
  2655. bh_assert(0);
  2656. break;
  2657. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2658. case VALUE_TYPE_FUNCREF:
  2659. {
  2660. args[i].kind = WASM_FUNCREF;
  2661. args[i].of.i32 = va_arg(vargs, uint32);
  2662. break;
  2663. }
  2664. case VALUE_TYPE_EXTERNREF:
  2665. {
  2666. args[i].kind = WASM_EXTERNREF;
  2667. args[i].of.foreign = va_arg(vargs, uintptr_t);
  2668. break;
  2669. }
  2670. #endif
  2671. default:
  2672. bh_assert(0);
  2673. break;
  2674. }
  2675. }
  2676. va_end(vargs);
  2677. ret = wasm_runtime_call_wasm_a(exec_env, function, num_results, results,
  2678. num_args, args);
  2679. if (args != args_buf)
  2680. wasm_runtime_free(args);
  2681. fail1:
  2682. return ret;
  2683. }
  2684. bool
  2685. wasm_runtime_create_exec_env_singleton(
  2686. WASMModuleInstanceCommon *module_inst_comm)
  2687. {
  2688. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2689. WASMExecEnv *exec_env = NULL;
  2690. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2691. || module_inst_comm->module_type == Wasm_Module_AoT);
  2692. if (module_inst->exec_env_singleton) {
  2693. return true;
  2694. }
  2695. exec_env = wasm_exec_env_create(module_inst_comm,
  2696. module_inst->default_wasm_stack_size);
  2697. if (exec_env)
  2698. module_inst->exec_env_singleton = exec_env;
  2699. return exec_env ? true : false;
  2700. }
  2701. WASMExecEnv *
  2702. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst_comm)
  2703. {
  2704. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2705. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2706. || module_inst_comm->module_type == Wasm_Module_AoT);
  2707. if (!module_inst->exec_env_singleton) {
  2708. wasm_runtime_create_exec_env_singleton(module_inst_comm);
  2709. }
  2710. return module_inst->exec_env_singleton;
  2711. }
  2712. static void
  2713. wasm_set_exception_local(WASMModuleInstance *module_inst, const char *exception)
  2714. {
  2715. exception_lock(module_inst);
  2716. if (exception) {
  2717. snprintf(module_inst->cur_exception, sizeof(module_inst->cur_exception),
  2718. "Exception: %s", exception);
  2719. }
  2720. else {
  2721. module_inst->cur_exception[0] = '\0';
  2722. }
  2723. exception_unlock(module_inst);
  2724. }
  2725. void
  2726. wasm_set_exception(WASMModuleInstance *module_inst, const char *exception)
  2727. {
  2728. #if WASM_ENABLE_THREAD_MGR != 0
  2729. WASMExecEnv *exec_env =
  2730. wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst);
  2731. if (exec_env) {
  2732. wasm_cluster_set_exception(exec_env, exception);
  2733. }
  2734. else {
  2735. wasm_set_exception_local(module_inst, exception);
  2736. }
  2737. #else
  2738. wasm_set_exception_local(module_inst, exception);
  2739. #endif
  2740. }
  2741. /* clang-format off */
  2742. static const char *exception_msgs[] = {
  2743. "unreachable", /* EXCE_UNREACHABLE */
  2744. "allocate memory failed", /* EXCE_OUT_OF_MEMORY */
  2745. "out of bounds memory access", /* EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS */
  2746. "integer overflow", /* EXCE_INTEGER_OVERFLOW */
  2747. "integer divide by zero", /* EXCE_INTEGER_DIVIDE_BY_ZERO */
  2748. "invalid conversion to integer", /* EXCE_INVALID_CONVERSION_TO_INTEGER */
  2749. "indirect call type mismatch", /* EXCE_INVALID_FUNCTION_TYPE_INDEX */
  2750. "invalid function index", /* EXCE_INVALID_FUNCTION_INDEX */
  2751. "undefined element", /* EXCE_UNDEFINED_ELEMENT */
  2752. "uninitialized element", /* EXCE_UNINITIALIZED_ELEMENT */
  2753. "failed to call unlinked import function", /* EXCE_CALL_UNLINKED_IMPORT_FUNC */
  2754. "native stack overflow", /* EXCE_NATIVE_STACK_OVERFLOW */
  2755. "unaligned atomic", /* EXCE_UNALIGNED_ATOMIC */
  2756. "wasm auxiliary stack overflow", /* EXCE_AUX_STACK_OVERFLOW */
  2757. "wasm auxiliary stack underflow", /* EXCE_AUX_STACK_UNDERFLOW */
  2758. "out of bounds table access", /* EXCE_OUT_OF_BOUNDS_TABLE_ACCESS */
  2759. "wasm operand stack overflow", /* EXCE_OPERAND_STACK_OVERFLOW */
  2760. "failed to compile fast jit function", /* EXCE_FAILED_TO_COMPILE_FAST_JIT_FUNC */
  2761. /* GC related exceptions */
  2762. "null function reference", /* EXCE_NULL_FUNC_OBJ */
  2763. "null structure reference", /* EXCE_NULL_STRUCT_OBJ */
  2764. "null array reference", /* EXCE_NULL_ARRAY_OBJ */
  2765. "null i31 reference", /* EXCE_NULL_I31_OBJ */
  2766. "null reference", /* EXCE_NULL_REFERENCE */
  2767. "create rtt type failed", /* EXCE_FAILED_TO_CREATE_RTT_TYPE */
  2768. "create struct object failed", /* EXCE_FAILED_TO_CREATE_STRUCT_OBJ */
  2769. "create array object failed", /* EXCE_FAILED_TO_CREATE_ARRAY_OBJ */
  2770. "create externref object failed", /* EXCE_FAILED_TO_CREATE_EXTERNREF_OBJ */
  2771. "cast failure", /* EXCE_CAST_FAILURE */
  2772. "out of bounds array access", /* EXCE_ARRAY_IDX_OOB */
  2773. /* stringref related exceptions */
  2774. "create string object failed", /* EXCE_FAILED_TO_CREATE_STRING */
  2775. "create stringref failed", /* EXCE_FAILED_TO_CREATE_STRINGREF */
  2776. "create stringview failed", /* EXCE_FAILED_TO_CREATE_STRINGVIEW */
  2777. "encode failed", /* EXCE_FAILED_TO_ENCODE_STRING */
  2778. "", /* EXCE_ALREADY_THROWN */
  2779. };
  2780. /* clang-format on */
  2781. void
  2782. wasm_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id)
  2783. {
  2784. if (id < EXCE_NUM)
  2785. wasm_set_exception(module_inst, exception_msgs[id]);
  2786. else
  2787. wasm_set_exception(module_inst, "unknown exception");
  2788. }
  2789. const char *
  2790. wasm_get_exception(WASMModuleInstance *module_inst)
  2791. {
  2792. if (module_inst->cur_exception[0] == '\0')
  2793. return NULL;
  2794. else
  2795. return module_inst->cur_exception;
  2796. }
  2797. bool
  2798. wasm_copy_exception(WASMModuleInstance *module_inst, char *exception_buf)
  2799. {
  2800. bool has_exception = false;
  2801. exception_lock(module_inst);
  2802. if (module_inst->cur_exception[0] != '\0') {
  2803. /* NULL is passed if the caller is not interested in getting the
  2804. * exception content, but only in knowing if an exception has been
  2805. * raised
  2806. */
  2807. if (exception_buf != NULL)
  2808. bh_memcpy_s(exception_buf, sizeof(module_inst->cur_exception),
  2809. module_inst->cur_exception,
  2810. sizeof(module_inst->cur_exception));
  2811. has_exception = true;
  2812. }
  2813. exception_unlock(module_inst);
  2814. return has_exception;
  2815. }
  2816. void
  2817. wasm_runtime_set_exception(WASMModuleInstanceCommon *module_inst_comm,
  2818. const char *exception)
  2819. {
  2820. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2821. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2822. || module_inst_comm->module_type == Wasm_Module_AoT);
  2823. wasm_set_exception(module_inst, exception);
  2824. }
  2825. const char *
  2826. wasm_runtime_get_exception(WASMModuleInstanceCommon *module_inst_comm)
  2827. {
  2828. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2829. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2830. || module_inst_comm->module_type == Wasm_Module_AoT);
  2831. return wasm_get_exception(module_inst);
  2832. }
  2833. bool
  2834. wasm_runtime_copy_exception(WASMModuleInstanceCommon *module_inst_comm,
  2835. char *exception_buf)
  2836. {
  2837. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2838. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2839. || module_inst_comm->module_type == Wasm_Module_AoT);
  2840. return wasm_copy_exception(module_inst, exception_buf);
  2841. }
  2842. void
  2843. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst_comm)
  2844. {
  2845. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2846. || module_inst_comm->module_type == Wasm_Module_AoT);
  2847. wasm_runtime_set_exception(module_inst_comm, NULL);
  2848. }
  2849. void
  2850. wasm_runtime_terminate(WASMModuleInstanceCommon *module_inst_comm)
  2851. {
  2852. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2853. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2854. || module_inst_comm->module_type == Wasm_Module_AoT);
  2855. wasm_set_exception(module_inst, "terminated by user");
  2856. }
  2857. void
  2858. wasm_runtime_set_custom_data_internal(
  2859. WASMModuleInstanceCommon *module_inst_comm, void *custom_data)
  2860. {
  2861. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2862. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2863. || module_inst_comm->module_type == Wasm_Module_AoT);
  2864. module_inst->custom_data = custom_data;
  2865. }
  2866. void
  2867. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  2868. void *custom_data)
  2869. {
  2870. #if WASM_ENABLE_THREAD_MGR != 0
  2871. wasm_cluster_spread_custom_data(module_inst, custom_data);
  2872. #else
  2873. wasm_runtime_set_custom_data_internal(module_inst, custom_data);
  2874. #endif
  2875. }
  2876. void *
  2877. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst_comm)
  2878. {
  2879. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  2880. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  2881. || module_inst_comm->module_type == Wasm_Module_AoT);
  2882. return module_inst->custom_data;
  2883. }
  2884. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  2885. void
  2886. wasm_runtime_set_bounds_checks(WASMModuleInstanceCommon *module_inst,
  2887. bool enable)
  2888. {
  2889. /* Always disable bounds checks if hw bounds checks is enabled */
  2890. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2891. enable = false;
  2892. #endif
  2893. #if WASM_ENABLE_INTERP != 0
  2894. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2895. ((WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e)
  2896. ->common.disable_bounds_checks = enable ? false : true;
  2897. }
  2898. #endif
  2899. #if WASM_ENABLE_AOT != 0
  2900. if (module_inst->module_type == Wasm_Module_AoT) {
  2901. ((AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e)
  2902. ->common.disable_bounds_checks = enable ? false : true;
  2903. }
  2904. #endif
  2905. }
  2906. bool
  2907. wasm_runtime_is_bounds_checks_enabled(WASMModuleInstanceCommon *module_inst)
  2908. {
  2909. #if WASM_ENABLE_INTERP != 0
  2910. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2911. return !((WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)
  2912. ->e)
  2913. ->common.disable_bounds_checks;
  2914. }
  2915. #endif
  2916. #if WASM_ENABLE_AOT != 0
  2917. if (module_inst->module_type == Wasm_Module_AoT) {
  2918. return !((AOTModuleInstanceExtra *)((WASMModuleInstance *)module_inst)
  2919. ->e)
  2920. ->common.disable_bounds_checks;
  2921. }
  2922. #endif
  2923. return true;
  2924. }
  2925. #endif
  2926. uint64
  2927. wasm_runtime_module_malloc_internal(WASMModuleInstanceCommon *module_inst,
  2928. WASMExecEnv *exec_env, uint64 size,
  2929. void **p_native_addr)
  2930. {
  2931. #if WASM_ENABLE_INTERP != 0
  2932. if (module_inst->module_type == Wasm_Module_Bytecode)
  2933. return wasm_module_malloc_internal((WASMModuleInstance *)module_inst,
  2934. exec_env, size, p_native_addr);
  2935. #endif
  2936. #if WASM_ENABLE_AOT != 0
  2937. if (module_inst->module_type == Wasm_Module_AoT)
  2938. return aot_module_malloc_internal((AOTModuleInstance *)module_inst,
  2939. exec_env, size, p_native_addr);
  2940. #endif
  2941. return 0;
  2942. }
  2943. uint64
  2944. wasm_runtime_module_realloc_internal(WASMModuleInstanceCommon *module_inst,
  2945. WASMExecEnv *exec_env, uint64 ptr,
  2946. uint64 size, void **p_native_addr)
  2947. {
  2948. #if WASM_ENABLE_INTERP != 0
  2949. if (module_inst->module_type == Wasm_Module_Bytecode)
  2950. return wasm_module_realloc_internal((WASMModuleInstance *)module_inst,
  2951. exec_env, ptr, size, p_native_addr);
  2952. #endif
  2953. #if WASM_ENABLE_AOT != 0
  2954. if (module_inst->module_type == Wasm_Module_AoT)
  2955. return aot_module_realloc_internal((AOTModuleInstance *)module_inst,
  2956. exec_env, ptr, size, p_native_addr);
  2957. #endif
  2958. return 0;
  2959. }
  2960. void
  2961. wasm_runtime_module_free_internal(WASMModuleInstanceCommon *module_inst,
  2962. WASMExecEnv *exec_env, uint64 ptr)
  2963. {
  2964. #if WASM_ENABLE_INTERP != 0
  2965. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2966. wasm_module_free_internal((WASMModuleInstance *)module_inst, exec_env,
  2967. 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_internal((AOTModuleInstance *)module_inst, exec_env,
  2974. ptr);
  2975. return;
  2976. }
  2977. #endif
  2978. }
  2979. uint64
  2980. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint64 size,
  2981. void **p_native_addr)
  2982. {
  2983. #if WASM_ENABLE_INTERP != 0
  2984. if (module_inst->module_type == Wasm_Module_Bytecode)
  2985. return wasm_module_malloc((WASMModuleInstance *)module_inst, size,
  2986. p_native_addr);
  2987. #endif
  2988. #if WASM_ENABLE_AOT != 0
  2989. if (module_inst->module_type == Wasm_Module_AoT)
  2990. return aot_module_malloc((AOTModuleInstance *)module_inst, size,
  2991. p_native_addr);
  2992. #endif
  2993. return 0;
  2994. }
  2995. uint64
  2996. wasm_runtime_module_realloc(WASMModuleInstanceCommon *module_inst, uint64 ptr,
  2997. uint64 size, void **p_native_addr)
  2998. {
  2999. #if WASM_ENABLE_INTERP != 0
  3000. if (module_inst->module_type == Wasm_Module_Bytecode)
  3001. return wasm_module_realloc((WASMModuleInstance *)module_inst, ptr, size,
  3002. p_native_addr);
  3003. #endif
  3004. #if WASM_ENABLE_AOT != 0
  3005. if (module_inst->module_type == Wasm_Module_AoT)
  3006. return aot_module_realloc((AOTModuleInstance *)module_inst, ptr, size,
  3007. p_native_addr);
  3008. #endif
  3009. return 0;
  3010. }
  3011. void
  3012. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint64 ptr)
  3013. {
  3014. #if WASM_ENABLE_INTERP != 0
  3015. if (module_inst->module_type == Wasm_Module_Bytecode) {
  3016. wasm_module_free((WASMModuleInstance *)module_inst, ptr);
  3017. return;
  3018. }
  3019. #endif
  3020. #if WASM_ENABLE_AOT != 0
  3021. if (module_inst->module_type == Wasm_Module_AoT) {
  3022. aot_module_free((AOTModuleInstance *)module_inst, ptr);
  3023. return;
  3024. }
  3025. #endif
  3026. }
  3027. uint64
  3028. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  3029. const char *src, uint64 size)
  3030. {
  3031. #if WASM_ENABLE_INTERP != 0
  3032. if (module_inst->module_type == Wasm_Module_Bytecode) {
  3033. return wasm_module_dup_data((WASMModuleInstance *)module_inst, src,
  3034. size);
  3035. }
  3036. #endif
  3037. #if WASM_ENABLE_AOT != 0
  3038. if (module_inst->module_type == Wasm_Module_AoT) {
  3039. return aot_module_dup_data((AOTModuleInstance *)module_inst, src, size);
  3040. }
  3041. #endif
  3042. return 0;
  3043. }
  3044. #if WASM_ENABLE_LIBC_WASI != 0
  3045. static WASIArguments *
  3046. get_wasi_args_from_module(wasm_module_t module)
  3047. {
  3048. WASIArguments *wasi_args = NULL;
  3049. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  3050. if (module->module_type == Wasm_Module_Bytecode)
  3051. wasi_args = &((WASMModule *)module)->wasi_args;
  3052. #endif
  3053. #if WASM_ENABLE_AOT != 0
  3054. if (module->module_type == Wasm_Module_AoT)
  3055. wasi_args = &((AOTModule *)module)->wasi_args;
  3056. #endif
  3057. return wasi_args;
  3058. }
  3059. void
  3060. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
  3061. uint32 dir_count, const char *map_dir_list[],
  3062. uint32 map_dir_count, const char *env_list[],
  3063. uint32 env_count, char *argv[], int argc,
  3064. int64 stdinfd, int64 stdoutfd, int64 stderrfd)
  3065. {
  3066. WASIArguments *wasi_args = get_wasi_args_from_module(module);
  3067. bh_assert(wasi_args);
  3068. wasi_args->dir_list = dir_list;
  3069. wasi_args->dir_count = dir_count;
  3070. wasi_args->map_dir_list = map_dir_list;
  3071. wasi_args->map_dir_count = map_dir_count;
  3072. wasi_args->env = env_list;
  3073. wasi_args->env_count = env_count;
  3074. wasi_args->argv = argv;
  3075. wasi_args->argc = (uint32)argc;
  3076. wasi_args->stdio[0] = (os_raw_file_handle)stdinfd;
  3077. wasi_args->stdio[1] = (os_raw_file_handle)stdoutfd;
  3078. wasi_args->stdio[2] = (os_raw_file_handle)stderrfd;
  3079. #if WASM_ENABLE_MULTI_MODULE != 0
  3080. #if WASM_ENABLE_INTERP != 0
  3081. if (module->module_type == Wasm_Module_Bytecode) {
  3082. wasm_propagate_wasi_args((WASMModule *)module);
  3083. }
  3084. #endif
  3085. #endif
  3086. }
  3087. void
  3088. wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
  3089. uint32 dir_count, const char *map_dir_list[],
  3090. uint32 map_dir_count, const char *env_list[],
  3091. uint32 env_count, char *argv[], int argc)
  3092. {
  3093. wasm_runtime_set_wasi_args_ex(module, dir_list, dir_count, map_dir_list,
  3094. map_dir_count, env_list, env_count, argv,
  3095. argc, -1, -1, -1);
  3096. }
  3097. void
  3098. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  3099. uint32 addr_pool_size)
  3100. {
  3101. WASIArguments *wasi_args = get_wasi_args_from_module(module);
  3102. if (wasi_args) {
  3103. wasi_args->addr_pool = addr_pool;
  3104. wasi_args->addr_count = addr_pool_size;
  3105. }
  3106. }
  3107. void
  3108. wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
  3109. const char *ns_lookup_pool[],
  3110. uint32 ns_lookup_pool_size)
  3111. {
  3112. WASIArguments *wasi_args = get_wasi_args_from_module(module);
  3113. if (wasi_args) {
  3114. wasi_args->ns_lookup_pool = ns_lookup_pool;
  3115. wasi_args->ns_lookup_count = ns_lookup_pool_size;
  3116. }
  3117. }
  3118. #if WASM_ENABLE_UVWASI == 0
  3119. static bool
  3120. copy_string_array(const char *array[], uint32 array_size, char **buf_ptr,
  3121. char ***list_ptr, uint64 *out_buf_size)
  3122. {
  3123. uint64 buf_size = 0, total_size;
  3124. uint32 buf_offset = 0, i;
  3125. char *buf = NULL, **list = NULL;
  3126. for (i = 0; i < array_size; i++)
  3127. buf_size += strlen(array[i]) + 1;
  3128. /* We add +1 to generate null-terminated array of strings */
  3129. total_size = sizeof(char *) * ((uint64)array_size + 1);
  3130. if (total_size >= UINT32_MAX
  3131. /* total_size must be larger than 0, don' check it again */
  3132. || !(list = wasm_runtime_malloc((uint32)total_size))
  3133. || buf_size >= UINT32_MAX
  3134. || (buf_size > 0 && !(buf = wasm_runtime_malloc((uint32)buf_size)))) {
  3135. if (buf)
  3136. wasm_runtime_free(buf);
  3137. if (list)
  3138. wasm_runtime_free(list);
  3139. return false;
  3140. }
  3141. for (i = 0; i < array_size; i++) {
  3142. list[i] = buf + buf_offset;
  3143. bh_strcpy_s(buf + buf_offset, (uint32)buf_size - buf_offset, array[i]);
  3144. buf_offset += (uint32)(strlen(array[i]) + 1);
  3145. }
  3146. list[array_size] = NULL;
  3147. *list_ptr = list;
  3148. *buf_ptr = buf;
  3149. if (out_buf_size)
  3150. *out_buf_size = buf_size;
  3151. return true;
  3152. }
  3153. bool
  3154. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  3155. const char *dir_list[], uint32 dir_count,
  3156. const char *map_dir_list[], uint32 map_dir_count,
  3157. const char *env[], uint32 env_count,
  3158. const char *addr_pool[], uint32 addr_pool_size,
  3159. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  3160. char *argv[], uint32 argc, os_raw_file_handle stdinfd,
  3161. os_raw_file_handle stdoutfd, os_raw_file_handle stderrfd,
  3162. char *error_buf, uint32 error_buf_size)
  3163. {
  3164. WASIContext *wasi_ctx;
  3165. char *argv_buf = NULL;
  3166. char **argv_list = NULL;
  3167. char *env_buf = NULL;
  3168. char **env_list = NULL;
  3169. char *ns_lookup_buf = NULL;
  3170. char **ns_lookup_list = NULL;
  3171. uint64 argv_buf_size = 0, env_buf_size = 0;
  3172. struct fd_table *curfds = NULL;
  3173. struct fd_prestats *prestats = NULL;
  3174. struct argv_environ_values *argv_environ = NULL;
  3175. struct addr_pool *apool = NULL;
  3176. bool fd_table_inited = false, fd_prestats_inited = false;
  3177. bool argv_environ_inited = false;
  3178. bool addr_pool_inited = false;
  3179. __wasi_fd_t wasm_fd = 3;
  3180. os_file_handle file_handle;
  3181. char *path, resolved_path[PATH_MAX];
  3182. uint32 i;
  3183. if (!(wasi_ctx = runtime_malloc(sizeof(WASIContext), NULL, error_buf,
  3184. error_buf_size))) {
  3185. return false;
  3186. }
  3187. wasm_runtime_set_wasi_ctx(module_inst, wasi_ctx);
  3188. /* process argv[0], trip the path and suffix, only keep the program name
  3189. */
  3190. if (!copy_string_array((const char **)argv, argc, &argv_buf, &argv_list,
  3191. &argv_buf_size)) {
  3192. set_error_buf(error_buf, error_buf_size,
  3193. "Init wasi environment failed: allocate memory failed");
  3194. goto fail;
  3195. }
  3196. if (!copy_string_array(env, env_count, &env_buf, &env_list,
  3197. &env_buf_size)) {
  3198. set_error_buf(error_buf, error_buf_size,
  3199. "Init wasi environment failed: allocate memory failed");
  3200. goto fail;
  3201. }
  3202. if (!(curfds = wasm_runtime_malloc(sizeof(struct fd_table)))
  3203. || !(prestats = wasm_runtime_malloc(sizeof(struct fd_prestats)))
  3204. || !(argv_environ =
  3205. wasm_runtime_malloc(sizeof(struct argv_environ_values)))
  3206. || !(apool = wasm_runtime_malloc(sizeof(struct addr_pool)))) {
  3207. set_error_buf(error_buf, error_buf_size,
  3208. "Init wasi environment failed: allocate memory failed");
  3209. goto fail;
  3210. }
  3211. if (!fd_table_init(curfds)) {
  3212. set_error_buf(error_buf, error_buf_size,
  3213. "Init wasi environment failed: "
  3214. "init fd table failed");
  3215. goto fail;
  3216. }
  3217. fd_table_inited = true;
  3218. if (!fd_prestats_init(prestats)) {
  3219. set_error_buf(error_buf, error_buf_size,
  3220. "Init wasi environment failed: "
  3221. "init fd prestats failed");
  3222. goto fail;
  3223. }
  3224. fd_prestats_inited = true;
  3225. if (!argv_environ_init(argv_environ, argv_buf, argv_buf_size, argv_list,
  3226. argc, env_buf, env_buf_size, env_list, env_count)) {
  3227. set_error_buf(error_buf, error_buf_size,
  3228. "Init wasi environment failed: "
  3229. "init argument environment failed");
  3230. goto fail;
  3231. }
  3232. argv_environ_inited = true;
  3233. if (!addr_pool_init(apool)) {
  3234. set_error_buf(error_buf, error_buf_size,
  3235. "Init wasi environment failed: "
  3236. "init the address pool failed");
  3237. goto fail;
  3238. }
  3239. addr_pool_inited = true;
  3240. os_file_handle stdin_file_handle = os_convert_stdin_handle(stdinfd);
  3241. os_file_handle stdout_file_handle = os_convert_stdout_handle(stdoutfd);
  3242. os_file_handle stderr_file_handle = os_convert_stderr_handle(stderrfd);
  3243. if (!os_is_handle_valid(&stdin_file_handle)
  3244. || !os_is_handle_valid(&stdout_file_handle)
  3245. || !os_is_handle_valid(&stderr_file_handle))
  3246. goto fail;
  3247. /* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
  3248. if (!fd_table_insert_existing(curfds, 0, stdin_file_handle, true)
  3249. || !fd_table_insert_existing(curfds, 1, stdout_file_handle, true)
  3250. || !fd_table_insert_existing(curfds, 2, stderr_file_handle, true)) {
  3251. set_error_buf(error_buf, error_buf_size,
  3252. "Init wasi environment failed: init fd table failed");
  3253. goto fail;
  3254. }
  3255. wasm_fd = 3;
  3256. for (i = 0; i < dir_count; i++, wasm_fd++) {
  3257. path = os_realpath(dir_list[i], resolved_path);
  3258. if (!path) {
  3259. if (error_buf)
  3260. snprintf(error_buf, error_buf_size,
  3261. "error while pre-opening directory %s: %d\n",
  3262. dir_list[i], errno);
  3263. goto fail;
  3264. }
  3265. __wasi_errno_t error = os_open_preopendir(path, &file_handle);
  3266. if (error != __WASI_ESUCCESS) {
  3267. if (error_buf)
  3268. snprintf(error_buf, error_buf_size,
  3269. "error while pre-opening directory %s: %d\n",
  3270. dir_list[i], error);
  3271. goto fail;
  3272. }
  3273. if (!fd_table_insert_existing(curfds, wasm_fd, file_handle, false)) {
  3274. if (error_buf)
  3275. snprintf(error_buf, error_buf_size,
  3276. "error inserting preopen fd %u (directory %s) into fd "
  3277. "table",
  3278. (unsigned int)wasm_fd, dir_list[i]);
  3279. goto fail;
  3280. }
  3281. if (!fd_prestats_insert(prestats, dir_list[i], wasm_fd)) {
  3282. if (error_buf)
  3283. snprintf(error_buf, error_buf_size,
  3284. "error inserting preopen fd %u (directory %s) into "
  3285. "prestats table",
  3286. (unsigned int)wasm_fd, dir_list[i]);
  3287. goto fail;
  3288. }
  3289. }
  3290. for (i = 0; i < map_dir_count; i++, wasm_fd++) {
  3291. char mapping_copy_buf[256];
  3292. char *mapping_copy = mapping_copy_buf;
  3293. char *map_mapped = NULL, *map_host = NULL;
  3294. const unsigned long max_len =
  3295. (unsigned long)strlen(map_dir_list[i]) * 2 + 3;
  3296. /* Allocation limit for runtime environments with reduced stack size */
  3297. if (max_len > 256) {
  3298. if (!(mapping_copy = wasm_runtime_malloc(max_len))) {
  3299. snprintf(error_buf, error_buf_size,
  3300. "error while allocating for directory mapping\n");
  3301. goto fail;
  3302. }
  3303. }
  3304. bh_memcpy_s(mapping_copy, max_len, map_dir_list[i],
  3305. (uint32)(strlen(map_dir_list[i]) + 1));
  3306. const char *delim = "::";
  3307. char *delim_pos = strstr(mapping_copy, delim);
  3308. if (delim_pos) {
  3309. *delim_pos = '\0';
  3310. map_mapped = mapping_copy;
  3311. map_host = delim_pos + strlen(delim);
  3312. }
  3313. if (!map_mapped || !map_host) {
  3314. if (error_buf)
  3315. snprintf(error_buf, error_buf_size,
  3316. "error while pre-opening mapped directory: "
  3317. "invalid map\n");
  3318. if (mapping_copy != mapping_copy_buf)
  3319. wasm_runtime_free(mapping_copy);
  3320. goto fail;
  3321. }
  3322. path = os_realpath(map_host, resolved_path);
  3323. if (!path) {
  3324. if (error_buf)
  3325. snprintf(error_buf, error_buf_size,
  3326. "error while pre-opening mapped directory %s: %d\n",
  3327. map_host, errno);
  3328. if (mapping_copy != mapping_copy_buf)
  3329. wasm_runtime_free(mapping_copy);
  3330. goto fail;
  3331. }
  3332. __wasi_errno_t error = os_open_preopendir(path, &file_handle);
  3333. if (error != __WASI_ESUCCESS) {
  3334. if (error_buf)
  3335. snprintf(error_buf, error_buf_size,
  3336. "error while pre-opening mapped directory %s: %d\n",
  3337. map_host, errno);
  3338. if (mapping_copy != mapping_copy_buf)
  3339. wasm_runtime_free(mapping_copy);
  3340. goto fail;
  3341. }
  3342. if (!fd_table_insert_existing(curfds, wasm_fd, file_handle, false)
  3343. || !fd_prestats_insert(prestats, map_mapped, wasm_fd)) {
  3344. if (error_buf)
  3345. snprintf(error_buf, error_buf_size,
  3346. "error while pre-opening mapped directory %s: "
  3347. "insertion failed\n",
  3348. dir_list[i]);
  3349. if (mapping_copy != mapping_copy_buf)
  3350. wasm_runtime_free(mapping_copy);
  3351. goto fail;
  3352. }
  3353. if (mapping_copy != mapping_copy_buf)
  3354. wasm_runtime_free(mapping_copy);
  3355. }
  3356. /* addr_pool(textual) -> apool */
  3357. for (i = 0; i < addr_pool_size; i++) {
  3358. char *cp, *address, *mask;
  3359. bool ret = false;
  3360. cp = bh_strdup(addr_pool[i]);
  3361. if (!cp) {
  3362. set_error_buf(error_buf, error_buf_size,
  3363. "Init wasi environment failed: copy address failed");
  3364. goto fail;
  3365. }
  3366. address = strtok(cp, "/");
  3367. mask = strtok(NULL, "/");
  3368. if (!mask) {
  3369. snprintf(error_buf, error_buf_size,
  3370. "Invalid address pool entry: %s, must be in the format of "
  3371. "ADDRESS/MASK",
  3372. addr_pool[i]);
  3373. goto fail;
  3374. }
  3375. ret = addr_pool_insert(apool, address, (uint8)atoi(mask));
  3376. wasm_runtime_free(cp);
  3377. if (!ret) {
  3378. set_error_buf(error_buf, error_buf_size,
  3379. "Init wasi environment failed: store address failed");
  3380. goto fail;
  3381. }
  3382. }
  3383. if (!copy_string_array(ns_lookup_pool, ns_lookup_pool_size, &ns_lookup_buf,
  3384. &ns_lookup_list, NULL)) {
  3385. set_error_buf(error_buf, error_buf_size,
  3386. "Init wasi environment failed: allocate memory failed");
  3387. goto fail;
  3388. }
  3389. wasi_ctx->curfds = curfds;
  3390. wasi_ctx->prestats = prestats;
  3391. wasi_ctx->argv_environ = argv_environ;
  3392. wasi_ctx->addr_pool = apool;
  3393. wasi_ctx->argv_buf = argv_buf;
  3394. wasi_ctx->argv_list = argv_list;
  3395. wasi_ctx->env_buf = env_buf;
  3396. wasi_ctx->env_list = env_list;
  3397. wasi_ctx->ns_lookup_buf = ns_lookup_buf;
  3398. wasi_ctx->ns_lookup_list = ns_lookup_list;
  3399. return true;
  3400. fail:
  3401. if (argv_environ_inited)
  3402. argv_environ_destroy(argv_environ);
  3403. if (fd_prestats_inited)
  3404. fd_prestats_destroy(prestats);
  3405. if (fd_table_inited)
  3406. fd_table_destroy(curfds);
  3407. if (addr_pool_inited)
  3408. addr_pool_destroy(apool);
  3409. if (curfds)
  3410. wasm_runtime_free(curfds);
  3411. if (prestats)
  3412. wasm_runtime_free(prestats);
  3413. if (argv_environ)
  3414. wasm_runtime_free(argv_environ);
  3415. if (apool)
  3416. wasm_runtime_free(apool);
  3417. if (argv_buf)
  3418. wasm_runtime_free(argv_buf);
  3419. if (argv_list)
  3420. wasm_runtime_free(argv_list);
  3421. if (env_buf)
  3422. wasm_runtime_free(env_buf);
  3423. if (env_list)
  3424. wasm_runtime_free(env_list);
  3425. if (ns_lookup_buf)
  3426. wasm_runtime_free(ns_lookup_buf);
  3427. if (ns_lookup_list)
  3428. wasm_runtime_free(ns_lookup_list);
  3429. return false;
  3430. }
  3431. #else /* else of WASM_ENABLE_UVWASI == 0 */
  3432. static void *
  3433. wasm_uvwasi_malloc(size_t size, void *mem_user_data)
  3434. {
  3435. return runtime_malloc(size, NULL, NULL, 0);
  3436. (void)mem_user_data;
  3437. }
  3438. static void
  3439. wasm_uvwasi_free(void *ptr, void *mem_user_data)
  3440. {
  3441. if (ptr)
  3442. wasm_runtime_free(ptr);
  3443. (void)mem_user_data;
  3444. }
  3445. static void *
  3446. wasm_uvwasi_calloc(size_t nmemb, size_t size, void *mem_user_data)
  3447. {
  3448. uint64 total_size = (uint64)nmemb * size;
  3449. return runtime_malloc(total_size, NULL, NULL, 0);
  3450. (void)mem_user_data;
  3451. }
  3452. static void *
  3453. wasm_uvwasi_realloc(void *ptr, size_t size, void *mem_user_data)
  3454. {
  3455. if (size >= UINT32_MAX) {
  3456. return NULL;
  3457. }
  3458. return wasm_runtime_realloc(ptr, (uint32)size);
  3459. }
  3460. /* clang-format off */
  3461. static uvwasi_mem_t uvwasi_allocator = {
  3462. .mem_user_data = 0,
  3463. .malloc = wasm_uvwasi_malloc,
  3464. .free = wasm_uvwasi_free,
  3465. .calloc = wasm_uvwasi_calloc,
  3466. .realloc = wasm_uvwasi_realloc
  3467. };
  3468. /* clang-format on */
  3469. bool
  3470. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  3471. const char *dir_list[], uint32 dir_count,
  3472. const char *map_dir_list[], uint32 map_dir_count,
  3473. const char *env[], uint32 env_count,
  3474. const char *addr_pool[], uint32 addr_pool_size,
  3475. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  3476. char *argv[], uint32 argc, os_raw_file_handle stdinfd,
  3477. os_raw_file_handle stdoutfd, os_raw_file_handle stderrfd,
  3478. char *error_buf, uint32 error_buf_size)
  3479. {
  3480. WASIContext *ctx;
  3481. uvwasi_t *uvwasi;
  3482. uvwasi_options_t init_options;
  3483. const char **envp = NULL;
  3484. uint64 total_size;
  3485. uint32 i;
  3486. bool ret = false;
  3487. ctx = runtime_malloc(sizeof(*ctx), module_inst, error_buf, error_buf_size);
  3488. if (!ctx)
  3489. return false;
  3490. uvwasi = &ctx->uvwasi;
  3491. /* Setup the initialization options */
  3492. uvwasi_options_init(&init_options);
  3493. init_options.allocator = &uvwasi_allocator;
  3494. init_options.argc = argc;
  3495. init_options.argv = (const char **)argv;
  3496. init_options.in = (stdinfd != os_get_invalid_handle())
  3497. ? (uvwasi_fd_t)stdinfd
  3498. : init_options.in;
  3499. init_options.out = (stdoutfd != os_get_invalid_handle())
  3500. ? (uvwasi_fd_t)stdoutfd
  3501. : init_options.out;
  3502. init_options.err = (stderrfd != os_get_invalid_handle())
  3503. ? (uvwasi_fd_t)stderrfd
  3504. : init_options.err;
  3505. if (dir_count > 0) {
  3506. init_options.preopenc = dir_count;
  3507. total_size = sizeof(uvwasi_preopen_t) * (uint64)init_options.preopenc;
  3508. init_options.preopens = (uvwasi_preopen_t *)runtime_malloc(
  3509. total_size, module_inst, error_buf, error_buf_size);
  3510. if (init_options.preopens == NULL)
  3511. goto fail;
  3512. for (i = 0; i < init_options.preopenc; i++) {
  3513. init_options.preopens[i].real_path = dir_list[i];
  3514. init_options.preopens[i].mapped_path =
  3515. (i < map_dir_count) ? map_dir_list[i] : dir_list[i];
  3516. }
  3517. }
  3518. if (env_count > 0) {
  3519. total_size = sizeof(char *) * (uint64)(env_count + 1);
  3520. envp =
  3521. runtime_malloc(total_size, module_inst, error_buf, error_buf_size);
  3522. if (envp == NULL)
  3523. goto fail;
  3524. for (i = 0; i < env_count; i++) {
  3525. envp[i] = env[i];
  3526. }
  3527. envp[env_count] = NULL;
  3528. init_options.envp = envp;
  3529. }
  3530. if (UVWASI_ESUCCESS != uvwasi_init(uvwasi, &init_options)) {
  3531. set_error_buf(error_buf, error_buf_size, "uvwasi init failed");
  3532. goto fail;
  3533. }
  3534. wasm_runtime_set_wasi_ctx(module_inst, ctx);
  3535. ret = true;
  3536. fail:
  3537. if (envp)
  3538. wasm_runtime_free((void *)envp);
  3539. if (init_options.preopens)
  3540. wasm_runtime_free(init_options.preopens);
  3541. if (!ret && uvwasi)
  3542. wasm_runtime_free(uvwasi);
  3543. return ret;
  3544. }
  3545. #endif /* end of WASM_ENABLE_UVWASI */
  3546. bool
  3547. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst)
  3548. {
  3549. #if WASM_ENABLE_INTERP != 0
  3550. if (module_inst->module_type == Wasm_Module_Bytecode
  3551. && ((WASMModuleInstance *)module_inst)->module->import_wasi_api)
  3552. return true;
  3553. #endif
  3554. #if WASM_ENABLE_AOT != 0
  3555. if (module_inst->module_type == Wasm_Module_AoT
  3556. && ((AOTModule *)((AOTModuleInstance *)module_inst)->module)
  3557. ->import_wasi_api)
  3558. return true;
  3559. #endif
  3560. return false;
  3561. }
  3562. WASMFunctionInstanceCommon *
  3563. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst)
  3564. {
  3565. #if WASM_ENABLE_INTERP != 0
  3566. if (module_inst->module_type == Wasm_Module_Bytecode) {
  3567. WASMModuleInstance *wasm_inst = (WASMModuleInstance *)module_inst;
  3568. WASMFunctionInstance *func = wasm_lookup_function(wasm_inst, "_start");
  3569. if (func) {
  3570. if (func->u.func->func_type->param_count != 0
  3571. || func->u.func->func_type->result_count != 0) {
  3572. LOG_ERROR("Lookup wasi _start function failed: "
  3573. "invalid function type.\n");
  3574. return NULL;
  3575. }
  3576. return (WASMFunctionInstanceCommon *)func;
  3577. }
  3578. return NULL;
  3579. }
  3580. #endif
  3581. #if WASM_ENABLE_AOT != 0
  3582. if (module_inst->module_type == Wasm_Module_AoT) {
  3583. AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
  3584. AOTFunctionInstance *func = aot_lookup_function(aot_inst, "_start");
  3585. if (func) {
  3586. AOTFuncType *func_type = func->u.func.func_type;
  3587. if (func_type->param_count != 0 || func_type->result_count != 0) {
  3588. LOG_ERROR("Lookup wasi _start function failed: "
  3589. "invalid function type.\n");
  3590. return NULL;
  3591. }
  3592. return func;
  3593. }
  3594. return NULL;
  3595. }
  3596. #endif /* end of WASM_ENABLE_AOT */
  3597. return NULL;
  3598. }
  3599. #if WASM_ENABLE_UVWASI == 0
  3600. void
  3601. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  3602. {
  3603. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  3604. if (wasi_ctx) {
  3605. if (wasi_ctx->argv_environ) {
  3606. argv_environ_destroy(wasi_ctx->argv_environ);
  3607. wasm_runtime_free(wasi_ctx->argv_environ);
  3608. }
  3609. if (wasi_ctx->curfds) {
  3610. fd_table_destroy(wasi_ctx->curfds);
  3611. wasm_runtime_free(wasi_ctx->curfds);
  3612. }
  3613. if (wasi_ctx->prestats) {
  3614. fd_prestats_destroy(wasi_ctx->prestats);
  3615. wasm_runtime_free(wasi_ctx->prestats);
  3616. }
  3617. if (wasi_ctx->addr_pool) {
  3618. addr_pool_destroy(wasi_ctx->addr_pool);
  3619. wasm_runtime_free(wasi_ctx->addr_pool);
  3620. }
  3621. if (wasi_ctx->argv_buf)
  3622. wasm_runtime_free(wasi_ctx->argv_buf);
  3623. if (wasi_ctx->argv_list)
  3624. wasm_runtime_free(wasi_ctx->argv_list);
  3625. if (wasi_ctx->env_buf)
  3626. wasm_runtime_free(wasi_ctx->env_buf);
  3627. if (wasi_ctx->env_list)
  3628. wasm_runtime_free(wasi_ctx->env_list);
  3629. if (wasi_ctx->ns_lookup_buf)
  3630. wasm_runtime_free(wasi_ctx->ns_lookup_buf);
  3631. if (wasi_ctx->ns_lookup_list)
  3632. wasm_runtime_free(wasi_ctx->ns_lookup_list);
  3633. wasm_runtime_free(wasi_ctx);
  3634. }
  3635. }
  3636. #else
  3637. void
  3638. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  3639. {
  3640. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  3641. if (wasi_ctx) {
  3642. uvwasi_destroy(&wasi_ctx->uvwasi);
  3643. wasm_runtime_free(wasi_ctx);
  3644. }
  3645. }
  3646. #endif
  3647. uint32_t
  3648. wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst)
  3649. {
  3650. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  3651. #if WASM_ENABLE_THREAD_MGR != 0
  3652. WASMCluster *cluster;
  3653. WASMExecEnv *exec_env;
  3654. exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
  3655. if (exec_env && (cluster = wasm_exec_env_get_cluster(exec_env))) {
  3656. /**
  3657. * The main thread may exit earlier than other threads, and
  3658. * the exit_code of wasi_ctx may be changed by other thread
  3659. * when it runs into wasi_proc_exit, here we wait until all
  3660. * other threads exit to avoid getting invalid exit_code.
  3661. */
  3662. wasm_cluster_wait_for_all_except_self(cluster, exec_env);
  3663. }
  3664. #endif
  3665. return wasi_ctx->exit_code;
  3666. }
  3667. #endif /* end of WASM_ENABLE_LIBC_WASI */
  3668. WASMModuleCommon *
  3669. wasm_exec_env_get_module(WASMExecEnv *exec_env)
  3670. {
  3671. WASMModuleInstanceCommon *module_inst_comm =
  3672. wasm_runtime_get_module_inst(exec_env);
  3673. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  3674. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  3675. || module_inst_comm->module_type == Wasm_Module_AoT);
  3676. return (WASMModuleCommon *)module_inst->module;
  3677. }
  3678. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  3679. const uint8 *
  3680. wasm_runtime_get_custom_section(WASMModuleCommon *const module_comm,
  3681. const char *name, uint32 *len)
  3682. {
  3683. #if WASM_ENABLE_INTERP != 0
  3684. if (module_comm->module_type == Wasm_Module_Bytecode)
  3685. return wasm_loader_get_custom_section((WASMModule *)module_comm, name,
  3686. len);
  3687. #endif
  3688. #if WASM_ENABLE_AOT != 0
  3689. if (module_comm->module_type == Wasm_Module_AoT)
  3690. return aot_get_custom_section((AOTModule *)module_comm, name, len);
  3691. #endif
  3692. return NULL;
  3693. }
  3694. #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 */
  3695. static union {
  3696. int a;
  3697. char b;
  3698. } __ue = { .a = 1 };
  3699. #define is_little_endian() (__ue.b == 1) /* NOLINT */
  3700. int32
  3701. wasm_runtime_get_import_count(WASMModuleCommon *const module)
  3702. {
  3703. if (!module) {
  3704. bh_assert(0);
  3705. return -1;
  3706. }
  3707. #if WASM_ENABLE_AOT != 0
  3708. if (module->module_type == Wasm_Module_AoT) {
  3709. const AOTModule *aot_module = (const AOTModule *)module;
  3710. return (int32)(aot_module->import_func_count
  3711. + aot_module->import_global_count
  3712. + aot_module->import_table_count
  3713. + aot_module->import_memory_count);
  3714. }
  3715. #endif
  3716. #if WASM_ENABLE_INTERP != 0
  3717. if (module->module_type == Wasm_Module_Bytecode) {
  3718. const WASMModule *wasm_module = (const WASMModule *)module;
  3719. return (int32)wasm_module->import_count;
  3720. }
  3721. #endif
  3722. return -1;
  3723. }
  3724. void
  3725. wasm_runtime_get_import_type(WASMModuleCommon *const module, int32 import_index,
  3726. wasm_import_t *import_type)
  3727. {
  3728. if (!import_type) {
  3729. bh_assert(0);
  3730. return;
  3731. }
  3732. memset(import_type, 0, sizeof(wasm_import_t));
  3733. if (!module) {
  3734. bh_assert(0);
  3735. return;
  3736. }
  3737. #if WASM_ENABLE_AOT != 0
  3738. if (module->module_type == Wasm_Module_AoT) {
  3739. const AOTModule *aot_module = (const AOTModule *)module;
  3740. uint32 func_index = (uint32)import_index;
  3741. if (func_index < aot_module->import_func_count) {
  3742. const AOTImportFunc *aot_import_func =
  3743. &aot_module->import_funcs[func_index];
  3744. import_type->module_name = aot_import_func->module_name;
  3745. import_type->name = aot_import_func->func_name;
  3746. import_type->kind = WASM_IMPORT_EXPORT_KIND_FUNC;
  3747. import_type->linked =
  3748. aot_import_func->func_ptr_linked ? true : false;
  3749. import_type->u.func_type =
  3750. (WASMFuncType *)aot_import_func->func_type;
  3751. return;
  3752. }
  3753. uint32 global_index = func_index - aot_module->import_func_count;
  3754. if (global_index < aot_module->import_global_count) {
  3755. const AOTImportGlobal *aot_import_global =
  3756. &aot_module->import_globals[global_index];
  3757. import_type->module_name = aot_import_global->module_name;
  3758. import_type->name = aot_import_global->global_name;
  3759. import_type->kind = WASM_IMPORT_EXPORT_KIND_GLOBAL;
  3760. import_type->linked = aot_import_global->is_linked;
  3761. import_type->u.global_type =
  3762. (WASMGlobalType *)&aot_import_global->type;
  3763. return;
  3764. }
  3765. uint32 table_index = global_index - aot_module->import_global_count;
  3766. if (table_index < aot_module->import_table_count) {
  3767. const AOTImportTable *aot_import_table =
  3768. &aot_module->import_tables[table_index];
  3769. import_type->module_name = aot_import_table->module_name;
  3770. import_type->name = aot_import_table->table_name;
  3771. import_type->kind = WASM_IMPORT_EXPORT_KIND_TABLE;
  3772. import_type->linked = false; /* not supported */
  3773. import_type->u.table_type =
  3774. (WASMTableType *)&aot_import_table->table_type;
  3775. return;
  3776. }
  3777. uint32 memory_index = table_index - aot_module->import_table_count;
  3778. if (memory_index < aot_module->import_memory_count) {
  3779. const AOTImportMemory *aot_import_memory =
  3780. &aot_module->import_memories[memory_index];
  3781. import_type->module_name = aot_import_memory->module_name;
  3782. import_type->name = aot_import_memory->memory_name;
  3783. import_type->kind = WASM_IMPORT_EXPORT_KIND_MEMORY;
  3784. import_type->linked = false; /* not supported */
  3785. import_type->u.memory_type =
  3786. (WASMMemoryType *)&aot_import_memory->mem_type;
  3787. return;
  3788. }
  3789. bh_assert(0);
  3790. return;
  3791. }
  3792. #endif
  3793. #if WASM_ENABLE_INTERP != 0
  3794. if (module->module_type == Wasm_Module_Bytecode) {
  3795. const WASMModule *wasm_module = (const WASMModule *)module;
  3796. if ((uint32)import_index >= wasm_module->import_count) {
  3797. bh_assert(0);
  3798. return;
  3799. }
  3800. const WASMImport *wasm_import = &wasm_module->imports[import_index];
  3801. import_type->module_name = wasm_import->u.names.module_name;
  3802. import_type->name = wasm_import->u.names.field_name;
  3803. import_type->kind = wasm_import->kind;
  3804. switch (import_type->kind) {
  3805. case WASM_IMPORT_EXPORT_KIND_FUNC:
  3806. import_type->linked = wasm_import->u.function.func_ptr_linked;
  3807. import_type->u.func_type =
  3808. (WASMFuncType *)wasm_import->u.function.func_type;
  3809. break;
  3810. case WASM_IMPORT_EXPORT_KIND_GLOBAL:
  3811. import_type->linked = wasm_import->u.global.is_linked;
  3812. import_type->u.global_type =
  3813. (WASMGlobalType *)&wasm_import->u.global.type;
  3814. break;
  3815. case WASM_IMPORT_EXPORT_KIND_TABLE:
  3816. import_type->linked = false; /* not supported */
  3817. import_type->u.table_type =
  3818. (WASMTableType *)&wasm_import->u.table.table_type;
  3819. break;
  3820. case WASM_IMPORT_EXPORT_KIND_MEMORY:
  3821. import_type->linked = false; /* not supported */
  3822. import_type->u.memory_type =
  3823. (WASMMemoryType *)&wasm_import->u.memory.mem_type;
  3824. break;
  3825. default:
  3826. bh_assert(0);
  3827. break;
  3828. }
  3829. return;
  3830. }
  3831. #endif
  3832. }
  3833. int32
  3834. wasm_runtime_get_export_count(WASMModuleCommon *const module)
  3835. {
  3836. if (!module) {
  3837. bh_assert(0);
  3838. return -1;
  3839. }
  3840. #if WASM_ENABLE_AOT != 0
  3841. if (module->module_type == Wasm_Module_AoT) {
  3842. const AOTModule *aot_module = (const AOTModule *)module;
  3843. return (int32)aot_module->export_count;
  3844. }
  3845. #endif
  3846. #if WASM_ENABLE_INTERP != 0
  3847. if (module->module_type == Wasm_Module_Bytecode) {
  3848. const WASMModule *wasm_module = (const WASMModule *)module;
  3849. return (int32)wasm_module->export_count;
  3850. }
  3851. #endif
  3852. return -1;
  3853. }
  3854. void
  3855. wasm_runtime_get_export_type(WASMModuleCommon *const module, int32 export_index,
  3856. wasm_export_t *export_type)
  3857. {
  3858. if (!export_type) {
  3859. bh_assert(0);
  3860. return;
  3861. }
  3862. memset(export_type, 0, sizeof(wasm_export_t));
  3863. if (!module) {
  3864. bh_assert(0);
  3865. return;
  3866. }
  3867. #if WASM_ENABLE_AOT != 0
  3868. if (module->module_type == Wasm_Module_AoT) {
  3869. const AOTModule *aot_module = (const AOTModule *)module;
  3870. if ((uint32)export_index >= aot_module->export_count) {
  3871. bh_assert(0);
  3872. return;
  3873. }
  3874. const AOTExport *aot_export = &aot_module->exports[export_index];
  3875. export_type->name = aot_export->name;
  3876. export_type->kind = aot_export->kind;
  3877. switch (export_type->kind) {
  3878. case WASM_IMPORT_EXPORT_KIND_FUNC:
  3879. {
  3880. if (aot_export->index < aot_module->import_func_count) {
  3881. export_type->u.func_type =
  3882. (AOTFuncType *)aot_module
  3883. ->import_funcs[aot_export->index]
  3884. .func_type;
  3885. }
  3886. else {
  3887. export_type->u.func_type =
  3888. (AOTFuncType *)aot_module
  3889. ->types[aot_module->func_type_indexes
  3890. [aot_export->index
  3891. - aot_module->import_func_count]];
  3892. }
  3893. break;
  3894. }
  3895. case WASM_IMPORT_EXPORT_KIND_GLOBAL:
  3896. {
  3897. if (aot_export->index < aot_module->import_global_count) {
  3898. export_type->u.global_type =
  3899. &aot_module->import_globals[aot_export->index].type;
  3900. }
  3901. else {
  3902. export_type->u.global_type =
  3903. &aot_module
  3904. ->globals[aot_export->index
  3905. - aot_module->import_global_count]
  3906. .type;
  3907. }
  3908. break;
  3909. }
  3910. case WASM_IMPORT_EXPORT_KIND_TABLE:
  3911. {
  3912. if (aot_export->index < aot_module->import_table_count) {
  3913. export_type->u.table_type =
  3914. &aot_module->import_tables[aot_export->index]
  3915. .table_type;
  3916. }
  3917. else {
  3918. export_type->u.table_type =
  3919. &aot_module
  3920. ->tables[aot_export->index
  3921. - aot_module->import_table_count]
  3922. .table_type;
  3923. }
  3924. break;
  3925. }
  3926. case WASM_IMPORT_EXPORT_KIND_MEMORY:
  3927. {
  3928. if (aot_export->index < aot_module->import_memory_count) {
  3929. export_type->u.memory_type =
  3930. &aot_module->import_memories[aot_export->index]
  3931. .mem_type;
  3932. }
  3933. else {
  3934. export_type->u.memory_type =
  3935. &aot_module
  3936. ->memories[aot_export->index
  3937. - aot_module->import_memory_count];
  3938. }
  3939. break;
  3940. }
  3941. default:
  3942. bh_assert(0);
  3943. break;
  3944. }
  3945. return;
  3946. }
  3947. #endif
  3948. #if WASM_ENABLE_INTERP != 0
  3949. if (module->module_type == Wasm_Module_Bytecode) {
  3950. const WASMModule *wasm_module = (const WASMModule *)module;
  3951. if ((uint32)export_index >= wasm_module->export_count) {
  3952. bh_assert(0);
  3953. return;
  3954. }
  3955. const WASMExport *wasm_export = &wasm_module->exports[export_index];
  3956. export_type->name = wasm_export->name;
  3957. export_type->kind = wasm_export->kind;
  3958. switch (export_type->kind) {
  3959. case WASM_IMPORT_EXPORT_KIND_FUNC:
  3960. {
  3961. if (wasm_export->index < wasm_module->import_function_count) {
  3962. export_type->u.func_type =
  3963. (WASMFuncType *)wasm_module
  3964. ->import_functions[wasm_export->index]
  3965. .u.function.func_type;
  3966. }
  3967. else {
  3968. export_type->u.func_type =
  3969. wasm_module
  3970. ->functions[wasm_export->index
  3971. - wasm_module->import_function_count]
  3972. ->func_type;
  3973. }
  3974. break;
  3975. }
  3976. case WASM_IMPORT_EXPORT_KIND_GLOBAL:
  3977. {
  3978. if (wasm_export->index < wasm_module->import_global_count) {
  3979. export_type->u.global_type =
  3980. (WASMGlobalType *)&wasm_module
  3981. ->import_globals[wasm_export->index]
  3982. .u.global.type;
  3983. }
  3984. else {
  3985. export_type->u.global_type =
  3986. &wasm_module
  3987. ->globals[wasm_export->index
  3988. - wasm_module->import_global_count]
  3989. .type;
  3990. }
  3991. break;
  3992. }
  3993. case WASM_IMPORT_EXPORT_KIND_TABLE:
  3994. {
  3995. if (wasm_export->index < wasm_module->import_table_count) {
  3996. export_type->u.table_type =
  3997. (WASMTableType *)&wasm_module
  3998. ->import_tables[wasm_export->index]
  3999. .u.table.table_type;
  4000. }
  4001. else {
  4002. export_type->u.table_type =
  4003. &wasm_module
  4004. ->tables[wasm_export->index
  4005. - wasm_module->import_table_count]
  4006. .table_type;
  4007. }
  4008. break;
  4009. }
  4010. case WASM_IMPORT_EXPORT_KIND_MEMORY:
  4011. {
  4012. if (wasm_export->index < wasm_module->import_memory_count) {
  4013. export_type->u.memory_type =
  4014. (WASMMemoryType *)&wasm_module
  4015. ->import_memories[wasm_export->index]
  4016. .u.memory.mem_type;
  4017. }
  4018. else {
  4019. export_type->u.memory_type =
  4020. &wasm_module
  4021. ->memories[wasm_export->index
  4022. - wasm_module->import_memory_count];
  4023. }
  4024. break;
  4025. }
  4026. default:
  4027. bh_assert(0);
  4028. break;
  4029. }
  4030. return;
  4031. }
  4032. #endif
  4033. }
  4034. uint32
  4035. wasm_func_type_get_param_count(WASMFuncType *const func_type)
  4036. {
  4037. bh_assert(func_type);
  4038. return func_type->param_count;
  4039. }
  4040. wasm_valkind_t
  4041. wasm_func_type_get_param_valkind(WASMFuncType *const func_type,
  4042. uint32 param_index)
  4043. {
  4044. if (!func_type || (param_index >= func_type->param_count)) {
  4045. bh_assert(0);
  4046. return (wasm_valkind_t)-1;
  4047. }
  4048. switch (func_type->types[param_index]) {
  4049. case VALUE_TYPE_I32:
  4050. return WASM_I32;
  4051. case VALUE_TYPE_I64:
  4052. return WASM_I64;
  4053. case VALUE_TYPE_F32:
  4054. return WASM_F32;
  4055. case VALUE_TYPE_F64:
  4056. return WASM_F64;
  4057. case VALUE_TYPE_V128:
  4058. return WASM_V128;
  4059. case VALUE_TYPE_FUNCREF:
  4060. return WASM_FUNCREF;
  4061. case VALUE_TYPE_EXTERNREF:
  4062. return WASM_EXTERNREF;
  4063. case VALUE_TYPE_VOID:
  4064. default:
  4065. {
  4066. bh_assert(0);
  4067. return (wasm_valkind_t)-1;
  4068. }
  4069. }
  4070. }
  4071. uint32
  4072. wasm_func_type_get_result_count(WASMFuncType *const func_type)
  4073. {
  4074. bh_assert(func_type);
  4075. return func_type->result_count;
  4076. }
  4077. wasm_valkind_t
  4078. wasm_func_type_get_result_valkind(WASMFuncType *const func_type,
  4079. uint32 result_index)
  4080. {
  4081. if (!func_type || (result_index >= func_type->result_count)) {
  4082. bh_assert(0);
  4083. return (wasm_valkind_t)-1;
  4084. }
  4085. switch (func_type->types[func_type->param_count + result_index]) {
  4086. case VALUE_TYPE_I32:
  4087. return WASM_I32;
  4088. case VALUE_TYPE_I64:
  4089. return WASM_I64;
  4090. case VALUE_TYPE_F32:
  4091. return WASM_F32;
  4092. case VALUE_TYPE_F64:
  4093. return WASM_F64;
  4094. case VALUE_TYPE_FUNCREF:
  4095. return WASM_FUNCREF;
  4096. #if WASM_ENABLE_SIMD != 0
  4097. case VALUE_TYPE_V128:
  4098. return WASM_V128;
  4099. #endif
  4100. #if WASM_ENABLE_REF_TYPES != 0
  4101. case VALUE_TYPE_EXTERNREF:
  4102. #endif
  4103. case VALUE_TYPE_VOID:
  4104. default:
  4105. {
  4106. bh_assert(0);
  4107. return (wasm_valkind_t)-1;
  4108. }
  4109. }
  4110. }
  4111. wasm_valkind_t
  4112. wasm_global_type_get_valkind(WASMGlobalType *const global_type)
  4113. {
  4114. bh_assert(global_type);
  4115. return val_type_to_val_kind(global_type->val_type);
  4116. }
  4117. bool
  4118. wasm_global_type_get_mutable(WASMGlobalType *const global_type)
  4119. {
  4120. bh_assert(global_type);
  4121. return global_type->is_mutable;
  4122. }
  4123. bool
  4124. wasm_memory_type_get_shared(WASMMemoryType *const memory_type)
  4125. {
  4126. bh_assert(memory_type);
  4127. return (memory_type->flags & SHARED_MEMORY_FLAG) ? true : false;
  4128. }
  4129. uint32
  4130. wasm_memory_type_get_init_page_count(WASMMemoryType *const memory_type)
  4131. {
  4132. bh_assert(memory_type);
  4133. return memory_type->init_page_count;
  4134. }
  4135. uint32
  4136. wasm_memory_type_get_max_page_count(WASMMemoryType *const memory_type)
  4137. {
  4138. bh_assert(memory_type);
  4139. return memory_type->max_page_count;
  4140. }
  4141. wasm_valkind_t
  4142. wasm_table_type_get_elem_kind(WASMTableType *const table_type)
  4143. {
  4144. bh_assert(table_type);
  4145. return val_type_to_val_kind(table_type->elem_type);
  4146. }
  4147. bool
  4148. wasm_table_type_get_shared(WASMTableType *const table_type)
  4149. {
  4150. bh_assert(table_type);
  4151. return (table_type->flags & 2) ? true : false;
  4152. }
  4153. uint32
  4154. wasm_table_type_get_init_size(WASMTableType *const table_type)
  4155. {
  4156. bh_assert(table_type);
  4157. return table_type->init_size;
  4158. }
  4159. uint32
  4160. wasm_table_type_get_max_size(WASMTableType *const table_type)
  4161. {
  4162. bh_assert(table_type);
  4163. return table_type->max_size;
  4164. }
  4165. bool
  4166. wasm_runtime_register_natives(const char *module_name,
  4167. NativeSymbol *native_symbols,
  4168. uint32 n_native_symbols)
  4169. {
  4170. return wasm_native_register_natives(module_name, native_symbols,
  4171. n_native_symbols);
  4172. }
  4173. bool
  4174. wasm_runtime_register_natives_raw(const char *module_name,
  4175. NativeSymbol *native_symbols,
  4176. uint32 n_native_symbols)
  4177. {
  4178. return wasm_native_register_natives_raw(module_name, native_symbols,
  4179. n_native_symbols);
  4180. }
  4181. bool
  4182. wasm_runtime_unregister_natives(const char *module_name,
  4183. NativeSymbol *native_symbols)
  4184. {
  4185. return wasm_native_unregister_natives(module_name, native_symbols);
  4186. }
  4187. bool
  4188. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  4189. const WASMFuncType *func_type,
  4190. const char *signature, void *attachment,
  4191. uint32 *argv, uint32 argc, uint32 *argv_ret)
  4192. {
  4193. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  4194. #if WASM_ENABLE_MEMORY64 != 0
  4195. WASMMemoryInstance *memory =
  4196. wasm_get_default_memory((WASMModuleInstance *)module);
  4197. bool is_memory64 = memory ? memory->is_memory64 : false;
  4198. #endif
  4199. typedef void (*NativeRawFuncPtr)(WASMExecEnv *, uint64 *);
  4200. NativeRawFuncPtr invoke_native_raw = (NativeRawFuncPtr)func_ptr;
  4201. uint64 argv_buf[16] = { 0 }, *argv1 = argv_buf, *argv_dst, size;
  4202. uint32 *argv_src = argv, i, argc1, ptr_len;
  4203. uint32 arg_i32;
  4204. bool ret = false;
  4205. argc1 = func_type->param_count;
  4206. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  4207. size = sizeof(uint64) * (uint64)argc1;
  4208. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  4209. 0))) {
  4210. return false;
  4211. }
  4212. }
  4213. argv_dst = argv1;
  4214. /* Traverse secondly to fill in each argument */
  4215. for (i = 0; i < func_type->param_count; i++, argv_dst++) {
  4216. switch (func_type->types[i]) {
  4217. case VALUE_TYPE_I32:
  4218. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4219. case VALUE_TYPE_FUNCREF:
  4220. #endif
  4221. {
  4222. *(uint32 *)argv_dst = arg_i32 = *argv_src++;
  4223. if (signature
  4224. #if WASM_ENABLE_MEMORY64 != 0
  4225. && !is_memory64
  4226. #endif
  4227. ) {
  4228. if (signature[i + 1] == '*') {
  4229. /* param is a pointer */
  4230. if (signature[i + 2] == '~')
  4231. /* pointer with length followed */
  4232. ptr_len = *argv_src;
  4233. else
  4234. /* pointer without length followed */
  4235. ptr_len = 1;
  4236. if (!wasm_runtime_validate_app_addr(
  4237. module, (uint64)arg_i32, (uint64)ptr_len))
  4238. goto fail;
  4239. *(uintptr_t *)argv_dst =
  4240. (uintptr_t)wasm_runtime_addr_app_to_native(
  4241. module, (uint64)arg_i32);
  4242. }
  4243. else if (signature[i + 1] == '$') {
  4244. /* param is a string */
  4245. if (!wasm_runtime_validate_app_str_addr(
  4246. module, (uint64)arg_i32))
  4247. goto fail;
  4248. *(uintptr_t *)argv_dst =
  4249. (uintptr_t)wasm_runtime_addr_app_to_native(
  4250. module, (uint64)arg_i32);
  4251. }
  4252. }
  4253. break;
  4254. }
  4255. case VALUE_TYPE_I64:
  4256. #if WASM_ENABLE_MEMORY64 != 0
  4257. {
  4258. uint64 arg_i64;
  4259. PUT_I64_TO_ADDR((uint32 *)argv_dst,
  4260. GET_I64_FROM_ADDR(argv_src));
  4261. argv_src += 2;
  4262. arg_i64 = *argv_dst;
  4263. if (signature && is_memory64) {
  4264. /* TODO: memory64 pointer with length need a new symbol
  4265. * to represent type i64, with '~' still represent i32
  4266. * length */
  4267. if (signature[i + 1] == '*') {
  4268. /* param is a pointer */
  4269. if (signature[i + 2] == '~')
  4270. /* pointer with length followed */
  4271. ptr_len = *argv_src;
  4272. else
  4273. /* pointer without length followed */
  4274. ptr_len = 1;
  4275. if (!wasm_runtime_validate_app_addr(module, arg_i64,
  4276. (uint64)ptr_len))
  4277. goto fail;
  4278. *argv_dst = (uint64)wasm_runtime_addr_app_to_native(
  4279. module, arg_i64);
  4280. }
  4281. else if (signature[i + 1] == '$') {
  4282. /* param is a string */
  4283. if (!wasm_runtime_validate_app_str_addr(module,
  4284. arg_i64))
  4285. goto fail;
  4286. *argv_dst = (uint64)wasm_runtime_addr_app_to_native(
  4287. module, arg_i64);
  4288. }
  4289. }
  4290. break;
  4291. }
  4292. #endif
  4293. case VALUE_TYPE_F64:
  4294. bh_memcpy_s(argv_dst, sizeof(uint64), argv_src,
  4295. sizeof(uint32) * 2);
  4296. argv_src += 2;
  4297. break;
  4298. case VALUE_TYPE_F32:
  4299. *(float32 *)argv_dst = *(float32 *)argv_src++;
  4300. break;
  4301. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4302. case VALUE_TYPE_EXTERNREF:
  4303. {
  4304. uint32 externref_idx = *argv_src++;
  4305. void *externref_obj;
  4306. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  4307. goto fail;
  4308. bh_memcpy_s(argv_dst, sizeof(uintptr_t), argv_src,
  4309. sizeof(uintptr_t));
  4310. break;
  4311. }
  4312. #endif
  4313. #if WASM_ENABLE_GC != 0
  4314. case REF_TYPE_FUNCREF:
  4315. case REF_TYPE_EXTERNREF:
  4316. case REF_TYPE_ANYREF:
  4317. case REF_TYPE_EQREF:
  4318. case REF_TYPE_HT_NULLABLE:
  4319. case REF_TYPE_HT_NON_NULLABLE:
  4320. case REF_TYPE_I31REF:
  4321. case REF_TYPE_NULLFUNCREF:
  4322. case REF_TYPE_NULLEXTERNREF:
  4323. case REF_TYPE_STRUCTREF:
  4324. case REF_TYPE_ARRAYREF:
  4325. case REF_TYPE_NULLREF:
  4326. #if WASM_ENABLE_STRINGREF != 0
  4327. case REF_TYPE_STRINGREF:
  4328. case REF_TYPE_STRINGVIEWWTF8:
  4329. case REF_TYPE_STRINGVIEWWTF16:
  4330. case REF_TYPE_STRINGVIEWITER:
  4331. #endif
  4332. {
  4333. bh_memcpy_s(argv_dst, sizeof(uintptr_t), argv_src,
  4334. sizeof(uintptr_t));
  4335. argv_src += sizeof(uintptr_t) / sizeof(uint32);
  4336. break;
  4337. }
  4338. #endif
  4339. default:
  4340. bh_assert(0);
  4341. break;
  4342. }
  4343. }
  4344. exec_env->attachment = attachment;
  4345. invoke_native_raw(exec_env, argv1);
  4346. exec_env->attachment = NULL;
  4347. if (func_type->result_count > 0) {
  4348. switch (func_type->types[func_type->param_count]) {
  4349. case VALUE_TYPE_I32:
  4350. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4351. case VALUE_TYPE_FUNCREF:
  4352. #endif
  4353. argv_ret[0] = *(uint32 *)argv1;
  4354. break;
  4355. case VALUE_TYPE_F32:
  4356. *(float32 *)argv_ret = *(float32 *)argv1;
  4357. break;
  4358. case VALUE_TYPE_I64:
  4359. case VALUE_TYPE_F64:
  4360. bh_memcpy_s(argv_ret, sizeof(uint32) * 2, argv1,
  4361. sizeof(uint64));
  4362. break;
  4363. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4364. case VALUE_TYPE_EXTERNREF:
  4365. {
  4366. uint32 externref_idx;
  4367. uint64 externref_obj;
  4368. bh_memcpy_s(&externref_obj, sizeof(uint64), argv1,
  4369. sizeof(uint64));
  4370. if (!wasm_externref_obj2ref(exec_env->module_inst,
  4371. (void *)(uintptr_t)externref_obj,
  4372. &externref_idx))
  4373. goto fail;
  4374. argv_ret[0] = externref_idx;
  4375. break;
  4376. }
  4377. #endif
  4378. #if WASM_ENABLE_GC != 0
  4379. case REF_TYPE_FUNCREF:
  4380. case REF_TYPE_EXTERNREF:
  4381. case REF_TYPE_ANYREF:
  4382. case REF_TYPE_EQREF:
  4383. case REF_TYPE_HT_NULLABLE:
  4384. case REF_TYPE_HT_NON_NULLABLE:
  4385. case REF_TYPE_I31REF:
  4386. case REF_TYPE_NULLFUNCREF:
  4387. case REF_TYPE_NULLEXTERNREF:
  4388. case REF_TYPE_STRUCTREF:
  4389. case REF_TYPE_ARRAYREF:
  4390. case REF_TYPE_NULLREF:
  4391. #if WASM_ENABLE_STRINGREF != 0
  4392. case REF_TYPE_STRINGREF:
  4393. case REF_TYPE_STRINGVIEWWTF8:
  4394. case REF_TYPE_STRINGVIEWWTF16:
  4395. case REF_TYPE_STRINGVIEWITER:
  4396. #endif
  4397. {
  4398. bh_memcpy_s(argv_ret, sizeof(uintptr_t), argv1,
  4399. sizeof(uintptr_t));
  4400. break;
  4401. }
  4402. #endif
  4403. default:
  4404. bh_assert(0);
  4405. break;
  4406. }
  4407. }
  4408. ret = !wasm_runtime_copy_exception(module, NULL);
  4409. fail:
  4410. if (argv1 != argv_buf)
  4411. wasm_runtime_free(argv1);
  4412. return ret;
  4413. }
  4414. /**
  4415. * Implementation of wasm_runtime_invoke_native()
  4416. */
  4417. /**
  4418. * The invoke native implementation on ARM platform with VFP co-processor,
  4419. * RISCV32 platform with/without FPU/DPFPU and ARC platform.
  4420. */
  4421. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) \
  4422. || defined(BUILD_TARGET_RISCV32_ILP32D) \
  4423. || defined(BUILD_TARGET_RISCV32_ILP32F) \
  4424. || defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  4425. typedef void (*GenericFunctionPointer)(void);
  4426. void
  4427. invokeNative(GenericFunctionPointer f, uint32 *args, uint32 n_stacks);
  4428. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  4429. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  4430. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  4431. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  4432. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint32 *, uint32);
  4433. static volatile Float64FuncPtr invokeNative_Float64 =
  4434. (Float64FuncPtr)(uintptr_t)invokeNative;
  4435. static volatile Float32FuncPtr invokeNative_Float32 =
  4436. (Float32FuncPtr)(uintptr_t)invokeNative;
  4437. static volatile Int64FuncPtr invokeNative_Int64 =
  4438. (Int64FuncPtr)(uintptr_t)invokeNative;
  4439. static volatile Int32FuncPtr invokeNative_Int32 =
  4440. (Int32FuncPtr)(uintptr_t)invokeNative;
  4441. static volatile VoidFuncPtr invokeNative_Void =
  4442. (VoidFuncPtr)(uintptr_t)invokeNative;
  4443. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  4444. #define MAX_REG_INTS 4
  4445. #define MAX_REG_FLOATS 16
  4446. #else
  4447. #define MAX_REG_INTS 8
  4448. #define MAX_REG_FLOATS 8
  4449. #endif
  4450. bool
  4451. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  4452. const WASMFuncType *func_type, const char *signature,
  4453. void *attachment, uint32 *argv, uint32 argc,
  4454. uint32 *argv_ret)
  4455. {
  4456. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  4457. /* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args
  4458. */
  4459. uint32 argv_buf[32], *argv1 = argv_buf, *ints, *stacks, size;
  4460. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  4461. uint32 arg_i32, ptr_len;
  4462. uint32 result_count = func_type->result_count;
  4463. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  4464. bool ret = false;
  4465. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4466. bool is_aot_func = (NULL == signature);
  4467. #endif
  4468. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_ARC)
  4469. uint32 *fps;
  4470. int n_fps = 0;
  4471. #else
  4472. #define fps ints
  4473. #define n_fps n_ints
  4474. #endif
  4475. n_ints++; /* exec env */
  4476. /* Traverse firstly to calculate stack args count */
  4477. for (i = 0; i < func_type->param_count; i++) {
  4478. switch (func_type->types[i]) {
  4479. case VALUE_TYPE_I32:
  4480. #if WASM_ENABLE_GC != 0
  4481. case REF_TYPE_FUNCREF:
  4482. case REF_TYPE_EXTERNREF:
  4483. case REF_TYPE_ANYREF:
  4484. case REF_TYPE_EQREF:
  4485. case REF_TYPE_HT_NULLABLE:
  4486. case REF_TYPE_HT_NON_NULLABLE:
  4487. case REF_TYPE_I31REF:
  4488. case REF_TYPE_NULLFUNCREF:
  4489. case REF_TYPE_NULLEXTERNREF:
  4490. case REF_TYPE_STRUCTREF:
  4491. case REF_TYPE_ARRAYREF:
  4492. case REF_TYPE_NULLREF:
  4493. #if WASM_ENABLE_STRINGREF != 0
  4494. case REF_TYPE_STRINGREF:
  4495. case REF_TYPE_STRINGVIEWWTF8:
  4496. case REF_TYPE_STRINGVIEWWTF16:
  4497. case REF_TYPE_STRINGVIEWITER:
  4498. #endif
  4499. #endif
  4500. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4501. case VALUE_TYPE_FUNCREF:
  4502. case VALUE_TYPE_EXTERNREF:
  4503. #endif
  4504. if (n_ints < MAX_REG_INTS)
  4505. n_ints++;
  4506. else
  4507. n_stacks++;
  4508. break;
  4509. case VALUE_TYPE_I64:
  4510. if (n_ints < MAX_REG_INTS - 1) {
  4511. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  4512. /* 64-bit data must be 8 bytes aligned in arm */
  4513. if (n_ints & 1)
  4514. n_ints++;
  4515. #endif
  4516. n_ints += 2;
  4517. }
  4518. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  4519. || defined(BUILD_TARGET_RISCV32_ILP32F) \
  4520. || defined(BUILD_TARGET_RISCV32_ILP32D) || defined(BUILD_TARGET_ARC)
  4521. /* part in register, part in stack */
  4522. else if (n_ints == MAX_REG_INTS - 1) {
  4523. n_ints++;
  4524. n_stacks++;
  4525. }
  4526. #endif
  4527. else {
  4528. /* 64-bit data in stack must be 8 bytes aligned
  4529. in arm and riscv32 */
  4530. #if !defined(BUILD_TARGET_ARC)
  4531. if (n_stacks & 1)
  4532. n_stacks++;
  4533. #endif
  4534. n_stacks += 2;
  4535. }
  4536. break;
  4537. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  4538. case VALUE_TYPE_F32:
  4539. if (n_fps < MAX_REG_FLOATS)
  4540. n_fps++;
  4541. #if defined(BUILD_TARGET_RISCV32_ILP32F)
  4542. else if (n_ints < MAX_REG_INTS) {
  4543. n_ints++;
  4544. }
  4545. #endif
  4546. else
  4547. n_stacks++;
  4548. break;
  4549. case VALUE_TYPE_F64:
  4550. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  4551. || defined(BUILD_TARGET_RISCV32_ILP32F) || defined(BUILD_TARGET_ARC)
  4552. if (n_ints < MAX_REG_INTS - 1) {
  4553. n_ints += 2;
  4554. }
  4555. else if (n_ints == MAX_REG_INTS - 1) {
  4556. n_ints++;
  4557. n_stacks++;
  4558. }
  4559. #endif
  4560. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  4561. if (n_fps < MAX_REG_FLOATS - 1) {
  4562. /* 64-bit data must be 8 bytes aligned in arm */
  4563. if (n_fps & 1)
  4564. n_fps++;
  4565. n_fps += 2;
  4566. }
  4567. else if (n_fps == MAX_REG_FLOATS - 1) {
  4568. n_fps++;
  4569. n_stacks++;
  4570. }
  4571. #endif
  4572. else {
  4573. /* 64-bit data in stack must be 8 bytes aligned
  4574. in arm and riscv32 */
  4575. #if !defined(BUILD_TARGET_ARC)
  4576. if (n_stacks & 1)
  4577. n_stacks++;
  4578. #endif
  4579. n_stacks += 2;
  4580. }
  4581. break;
  4582. #else /* BUILD_TARGET_RISCV32_ILP32D */
  4583. case VALUE_TYPE_F32:
  4584. case VALUE_TYPE_F64:
  4585. if (n_fps < MAX_REG_FLOATS) {
  4586. n_fps++;
  4587. }
  4588. else if (func_type->types[i] == VALUE_TYPE_F32
  4589. && n_ints < MAX_REG_INTS) {
  4590. /* use int reg firstly if available */
  4591. n_ints++;
  4592. }
  4593. else if (func_type->types[i] == VALUE_TYPE_F64
  4594. && n_ints < MAX_REG_INTS - 1) {
  4595. /* use int regs firstly if available */
  4596. if (n_ints & 1)
  4597. n_ints++;
  4598. n_ints += 2;
  4599. }
  4600. else {
  4601. /* 64-bit data in stack must be 8 bytes aligned in riscv32
  4602. */
  4603. if (n_stacks & 1)
  4604. n_stacks++;
  4605. n_stacks += 2;
  4606. }
  4607. break;
  4608. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  4609. default:
  4610. bh_assert(0);
  4611. break;
  4612. }
  4613. }
  4614. for (i = 0; i < ext_ret_count; i++) {
  4615. if (n_ints < MAX_REG_INTS)
  4616. n_ints++;
  4617. else
  4618. n_stacks++;
  4619. }
  4620. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) \
  4621. || defined(BUILD_TARGET_RISCV32_ILP32F)
  4622. argc1 = MAX_REG_INTS + MAX_REG_FLOATS + n_stacks;
  4623. #elif defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  4624. argc1 = MAX_REG_INTS + n_stacks;
  4625. #else /* for BUILD_TARGET_RISCV32_ILP32D */
  4626. argc1 = MAX_REG_INTS + MAX_REG_FLOATS * 2 + n_stacks;
  4627. #endif
  4628. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  4629. size = sizeof(uint32) * (uint32)argc1;
  4630. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  4631. 0))) {
  4632. return false;
  4633. }
  4634. }
  4635. ints = argv1;
  4636. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) \
  4637. || defined(BUILD_TARGET_RISCV32_ILP32F)
  4638. fps = ints + MAX_REG_INTS;
  4639. stacks = fps + MAX_REG_FLOATS;
  4640. #elif defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC)
  4641. stacks = ints + MAX_REG_INTS;
  4642. #else /* for BUILD_TARGET_RISCV32_ILP32D */
  4643. fps = ints + MAX_REG_INTS;
  4644. stacks = fps + MAX_REG_FLOATS * 2;
  4645. #endif
  4646. n_ints = 0;
  4647. n_fps = 0;
  4648. n_stacks = 0;
  4649. ints[n_ints++] = (uint32)(uintptr_t)exec_env;
  4650. /* Traverse secondly to fill in each argument */
  4651. for (i = 0; i < func_type->param_count; i++) {
  4652. switch (func_type->types[i]) {
  4653. case VALUE_TYPE_I32:
  4654. #if WASM_ENABLE_GC != 0
  4655. case REF_TYPE_FUNCREF:
  4656. case REF_TYPE_EXTERNREF:
  4657. case REF_TYPE_ANYREF:
  4658. case REF_TYPE_EQREF:
  4659. case REF_TYPE_HT_NULLABLE:
  4660. case REF_TYPE_HT_NON_NULLABLE:
  4661. case REF_TYPE_I31REF:
  4662. case REF_TYPE_NULLFUNCREF:
  4663. case REF_TYPE_NULLEXTERNREF:
  4664. case REF_TYPE_STRUCTREF:
  4665. case REF_TYPE_ARRAYREF:
  4666. case REF_TYPE_NULLREF:
  4667. #if WASM_ENABLE_STRINGREF != 0
  4668. case REF_TYPE_STRINGREF:
  4669. case REF_TYPE_STRINGVIEWWTF8:
  4670. case REF_TYPE_STRINGVIEWWTF16:
  4671. case REF_TYPE_STRINGVIEWITER:
  4672. #endif
  4673. #endif
  4674. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4675. case VALUE_TYPE_FUNCREF:
  4676. #endif
  4677. {
  4678. arg_i32 = *argv_src++;
  4679. if (signature) {
  4680. if (signature[i + 1] == '*') {
  4681. /* param is a pointer */
  4682. if (signature[i + 2] == '~')
  4683. /* pointer with length followed */
  4684. ptr_len = *argv_src;
  4685. else
  4686. /* pointer without length followed */
  4687. ptr_len = 1;
  4688. if (!wasm_runtime_validate_app_addr(
  4689. module, (uint64)arg_i32, (uint64)ptr_len))
  4690. goto fail;
  4691. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  4692. module, (uint64)arg_i32);
  4693. }
  4694. else if (signature[i + 1] == '$') {
  4695. /* param is a string */
  4696. if (!wasm_runtime_validate_app_str_addr(
  4697. module, (uint64)arg_i32))
  4698. goto fail;
  4699. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  4700. module, (uint64)arg_i32);
  4701. }
  4702. }
  4703. if (n_ints < MAX_REG_INTS)
  4704. ints[n_ints++] = arg_i32;
  4705. else
  4706. stacks[n_stacks++] = arg_i32;
  4707. break;
  4708. }
  4709. case VALUE_TYPE_I64:
  4710. {
  4711. if (n_ints < MAX_REG_INTS - 1) {
  4712. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  4713. /* 64-bit data must be 8 bytes aligned in arm */
  4714. if (n_ints & 1)
  4715. n_ints++;
  4716. #endif
  4717. ints[n_ints++] = *argv_src++;
  4718. ints[n_ints++] = *argv_src++;
  4719. }
  4720. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  4721. || defined(BUILD_TARGET_RISCV32_ILP32F) \
  4722. || defined(BUILD_TARGET_RISCV32_ILP32D) || defined(BUILD_TARGET_ARC)
  4723. else if (n_ints == MAX_REG_INTS - 1) {
  4724. ints[n_ints++] = *argv_src++;
  4725. stacks[n_stacks++] = *argv_src++;
  4726. }
  4727. #endif
  4728. else {
  4729. /* 64-bit data in stack must be 8 bytes aligned
  4730. in arm and riscv32 */
  4731. #if !defined(BUILD_TARGET_ARC)
  4732. if (n_stacks & 1)
  4733. n_stacks++;
  4734. #endif
  4735. stacks[n_stacks++] = *argv_src++;
  4736. stacks[n_stacks++] = *argv_src++;
  4737. }
  4738. break;
  4739. }
  4740. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  4741. case VALUE_TYPE_F32:
  4742. {
  4743. if (n_fps < MAX_REG_FLOATS)
  4744. *(float32 *)&fps[n_fps++] = *(float32 *)argv_src++;
  4745. #if defined(BUILD_TARGET_RISCV32_ILP32F)
  4746. else if (n_ints < MAX_REG_INTS) {
  4747. ints[n_ints++] = *argv_src++;
  4748. }
  4749. #endif
  4750. else
  4751. *(float32 *)&stacks[n_stacks++] = *(float32 *)argv_src++;
  4752. break;
  4753. }
  4754. case VALUE_TYPE_F64:
  4755. {
  4756. #if defined(BUILD_TARGET_RISCV32_ILP32) \
  4757. || defined(BUILD_TARGET_RISCV32_ILP32F) || defined(BUILD_TARGET_ARC)
  4758. if (n_ints < MAX_REG_INTS - 1) {
  4759. ints[n_ints++] = *argv_src++;
  4760. ints[n_ints++] = *argv_src++;
  4761. }
  4762. else if (n_ints == MAX_REG_INTS - 1) {
  4763. ints[n_ints++] = *argv_src++;
  4764. stacks[n_stacks++] = *argv_src++;
  4765. }
  4766. #endif
  4767. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  4768. if (n_fps < MAX_REG_FLOATS - 1) {
  4769. /* 64-bit data must be 8 bytes aligned in arm */
  4770. if (n_fps & 1)
  4771. n_fps++;
  4772. fps[n_fps++] = *argv_src++;
  4773. fps[n_fps++] = *argv_src++;
  4774. }
  4775. else if (n_fps == MAX_REG_FLOATS - 1) {
  4776. fps[n_fps++] = *argv_src++;
  4777. stacks[n_stacks++] = *argv_src++;
  4778. }
  4779. #endif
  4780. else {
  4781. /* 64-bit data in stack must be 8 bytes aligned
  4782. in arm and riscv32 */
  4783. #if !defined(BUILD_TARGET_ARC)
  4784. if (n_stacks & 1)
  4785. n_stacks++;
  4786. #endif
  4787. stacks[n_stacks++] = *argv_src++;
  4788. stacks[n_stacks++] = *argv_src++;
  4789. }
  4790. break;
  4791. }
  4792. #else /* BUILD_TARGET_RISCV32_ILP32D */
  4793. case VALUE_TYPE_F32:
  4794. case VALUE_TYPE_F64:
  4795. {
  4796. if (n_fps < MAX_REG_FLOATS) {
  4797. if (func_type->types[i] == VALUE_TYPE_F32) {
  4798. *(float32 *)&fps[n_fps * 2] = *(float32 *)argv_src++;
  4799. /* NaN boxing, the upper bits of a valid NaN-boxed
  4800. value must be all 1s. */
  4801. fps[n_fps * 2 + 1] = 0xFFFFFFFF;
  4802. }
  4803. else {
  4804. *(float64 *)&fps[n_fps * 2] = *(float64 *)argv_src;
  4805. argv_src += 2;
  4806. }
  4807. n_fps++;
  4808. }
  4809. else if (func_type->types[i] == VALUE_TYPE_F32
  4810. && n_ints < MAX_REG_INTS) {
  4811. /* use int reg firstly if available */
  4812. *(float32 *)&ints[n_ints++] = *(float32 *)argv_src++;
  4813. }
  4814. else if (func_type->types[i] == VALUE_TYPE_F64
  4815. && n_ints < MAX_REG_INTS - 1) {
  4816. /* use int regs firstly if available */
  4817. if (n_ints & 1)
  4818. n_ints++;
  4819. *(float64 *)&ints[n_ints] = *(float64 *)argv_src;
  4820. n_ints += 2;
  4821. argv_src += 2;
  4822. }
  4823. else {
  4824. /* 64-bit data in stack must be 8 bytes aligned in riscv32
  4825. */
  4826. if (n_stacks & 1)
  4827. n_stacks++;
  4828. if (func_type->types[i] == VALUE_TYPE_F32) {
  4829. *(float32 *)&stacks[n_stacks++] =
  4830. *(float32 *)argv_src++;
  4831. }
  4832. else {
  4833. *(float64 *)&stacks[n_stacks] = *(float64 *)argv_src;
  4834. argv_src += 2;
  4835. n_stacks += 2;
  4836. }
  4837. }
  4838. break;
  4839. }
  4840. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  4841. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4842. case VALUE_TYPE_EXTERNREF:
  4843. {
  4844. uint32 externref_idx = *argv_src++;
  4845. if (is_aot_func) {
  4846. if (n_ints < MAX_REG_INTS)
  4847. ints[n_ints++] = externref_idx;
  4848. else
  4849. stacks[n_stacks++] = externref_idx;
  4850. }
  4851. else {
  4852. void *externref_obj;
  4853. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  4854. goto fail;
  4855. if (n_ints < MAX_REG_INTS)
  4856. ints[n_ints++] = (uintptr_t)externref_obj;
  4857. else
  4858. stacks[n_stacks++] = (uintptr_t)externref_obj;
  4859. }
  4860. break;
  4861. }
  4862. #endif
  4863. default:
  4864. bh_assert(0);
  4865. break;
  4866. }
  4867. }
  4868. /* Save extra result values' address to argv1 */
  4869. for (i = 0; i < ext_ret_count; i++) {
  4870. if (n_ints < MAX_REG_INTS)
  4871. ints[n_ints++] = *(uint32 *)argv_src++;
  4872. else
  4873. stacks[n_stacks++] = *(uint32 *)argv_src++;
  4874. }
  4875. exec_env->attachment = attachment;
  4876. if (func_type->result_count == 0) {
  4877. invokeNative_Void(func_ptr, argv1, n_stacks);
  4878. }
  4879. else {
  4880. switch (func_type->types[func_type->param_count]) {
  4881. case VALUE_TYPE_I32:
  4882. #if WASM_ENABLE_GC != 0
  4883. case REF_TYPE_FUNCREF:
  4884. case REF_TYPE_EXTERNREF:
  4885. case REF_TYPE_ANYREF:
  4886. case REF_TYPE_EQREF:
  4887. case REF_TYPE_HT_NULLABLE:
  4888. case REF_TYPE_HT_NON_NULLABLE:
  4889. case REF_TYPE_I31REF:
  4890. case REF_TYPE_NULLFUNCREF:
  4891. case REF_TYPE_NULLEXTERNREF:
  4892. case REF_TYPE_STRUCTREF:
  4893. case REF_TYPE_ARRAYREF:
  4894. case REF_TYPE_NULLREF:
  4895. #if WASM_ENABLE_STRINGREF != 0
  4896. case REF_TYPE_STRINGREF:
  4897. case REF_TYPE_STRINGVIEWWTF8:
  4898. case REF_TYPE_STRINGVIEWWTF16:
  4899. case REF_TYPE_STRINGVIEWITER:
  4900. #endif
  4901. #endif
  4902. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4903. case VALUE_TYPE_FUNCREF:
  4904. #endif
  4905. argv_ret[0] =
  4906. (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  4907. break;
  4908. case VALUE_TYPE_I64:
  4909. PUT_I64_TO_ADDR(argv_ret,
  4910. invokeNative_Int64(func_ptr, argv1, n_stacks));
  4911. break;
  4912. case VALUE_TYPE_F32:
  4913. *(float32 *)argv_ret =
  4914. invokeNative_Float32(func_ptr, argv1, n_stacks);
  4915. break;
  4916. case VALUE_TYPE_F64:
  4917. PUT_F64_TO_ADDR(
  4918. argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  4919. break;
  4920. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4921. case VALUE_TYPE_EXTERNREF:
  4922. {
  4923. if (is_aot_func) {
  4924. uint32 externref_idx =
  4925. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  4926. argv_ret[0] = externref_idx;
  4927. }
  4928. else {
  4929. uint32 externref_idx;
  4930. void *externref_obj;
  4931. externref_obj = (void *)(uintptr_t)invokeNative_Int32(
  4932. func_ptr, argv1, argc1);
  4933. if (!wasm_externref_obj2ref(exec_env->module_inst,
  4934. externref_obj, &externref_idx))
  4935. goto fail;
  4936. argv_ret[0] = externref_idx;
  4937. }
  4938. break;
  4939. }
  4940. #endif
  4941. default:
  4942. bh_assert(0);
  4943. break;
  4944. }
  4945. }
  4946. exec_env->attachment = NULL;
  4947. ret = !wasm_runtime_copy_exception(module, NULL);
  4948. fail:
  4949. if (argv1 != argv_buf)
  4950. wasm_runtime_free(argv1);
  4951. return ret;
  4952. }
  4953. #endif /* end of defined(BUILD_TARGET_ARM_VFP) \
  4954. || defined(BUILD_TARGET_THUMB_VFP) \
  4955. || defined(BUILD_TARGET_RISCV32_ILP32D) \
  4956. || defined(BUILD_TARGET_RISCV32_ILP32F) \
  4957. || defined(BUILD_TARGET_RISCV32_ILP32) \
  4958. || defined(BUILD_TARGET_ARC) */
  4959. #if defined(BUILD_TARGET_X86_32) || defined(BUILD_TARGET_ARM) \
  4960. || defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_MIPS) \
  4961. || defined(BUILD_TARGET_XTENSA)
  4962. typedef void (*GenericFunctionPointer)(void);
  4963. void
  4964. invokeNative(GenericFunctionPointer f, uint32 *args, uint32 sz);
  4965. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  4966. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  4967. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  4968. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  4969. typedef void (*VoidFuncPtr)(GenericFunctionPointer f, uint32 *, uint32);
  4970. static volatile Int64FuncPtr invokeNative_Int64 =
  4971. (Int64FuncPtr)(uintptr_t)invokeNative;
  4972. static volatile Int32FuncPtr invokeNative_Int32 =
  4973. (Int32FuncPtr)(uintptr_t)invokeNative;
  4974. static volatile Float64FuncPtr invokeNative_Float64 =
  4975. (Float64FuncPtr)(uintptr_t)invokeNative;
  4976. static volatile Float32FuncPtr invokeNative_Float32 =
  4977. (Float32FuncPtr)(uintptr_t)invokeNative;
  4978. static volatile VoidFuncPtr invokeNative_Void =
  4979. (VoidFuncPtr)(uintptr_t)invokeNative;
  4980. static inline void
  4981. word_copy(uint32 *dest, uint32 *src, unsigned num)
  4982. {
  4983. for (; num > 0; num--)
  4984. *dest++ = *src++;
  4985. }
  4986. bool
  4987. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  4988. const WASMFuncType *func_type, const char *signature,
  4989. void *attachment, uint32 *argv, uint32 argc,
  4990. uint32 *argv_ret)
  4991. {
  4992. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  4993. uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0;
  4994. uint32 arg_i32, ptr_len;
  4995. uint32 result_count = func_type->result_count;
  4996. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  4997. uint64 size;
  4998. bool ret = false;
  4999. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5000. bool is_aot_func = (NULL == signature);
  5001. #endif
  5002. #if defined(BUILD_TARGET_X86_32)
  5003. argc1 = argc + ext_ret_count + 2;
  5004. #else
  5005. /* arm/thumb/mips/xtensa, 64-bit data must be 8 bytes aligned,
  5006. so we need to allocate more memory. */
  5007. argc1 = func_type->param_count * 2 + ext_ret_count + 2;
  5008. #endif
  5009. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  5010. size = sizeof(uint32) * (uint64)argc1;
  5011. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  5012. 0))) {
  5013. return false;
  5014. }
  5015. }
  5016. for (i = 0; i < sizeof(WASMExecEnv *) / sizeof(uint32); i++)
  5017. argv1[j++] = ((uint32 *)&exec_env)[i];
  5018. for (i = 0; i < func_type->param_count; i++) {
  5019. switch (func_type->types[i]) {
  5020. case VALUE_TYPE_I32:
  5021. #if WASM_ENABLE_GC != 0
  5022. case REF_TYPE_FUNCREF:
  5023. case REF_TYPE_EXTERNREF:
  5024. case REF_TYPE_ANYREF:
  5025. case REF_TYPE_EQREF:
  5026. case REF_TYPE_HT_NULLABLE:
  5027. case REF_TYPE_HT_NON_NULLABLE:
  5028. case REF_TYPE_I31REF:
  5029. case REF_TYPE_NULLFUNCREF:
  5030. case REF_TYPE_NULLEXTERNREF:
  5031. case REF_TYPE_STRUCTREF:
  5032. case REF_TYPE_ARRAYREF:
  5033. case REF_TYPE_NULLREF:
  5034. #if WASM_ENABLE_STRINGREF != 0
  5035. case REF_TYPE_STRINGREF:
  5036. case REF_TYPE_STRINGVIEWWTF8:
  5037. case REF_TYPE_STRINGVIEWWTF16:
  5038. case REF_TYPE_STRINGVIEWITER:
  5039. #endif
  5040. #endif
  5041. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5042. case VALUE_TYPE_FUNCREF:
  5043. #endif
  5044. {
  5045. arg_i32 = *argv++;
  5046. if (signature) {
  5047. if (signature[i + 1] == '*') {
  5048. /* param is a pointer */
  5049. if (signature[i + 2] == '~')
  5050. /* pointer with length followed */
  5051. ptr_len = *argv;
  5052. else
  5053. /* pointer without length followed */
  5054. ptr_len = 1;
  5055. if (!wasm_runtime_validate_app_addr(
  5056. module, (uint64)arg_i32, (uint64)ptr_len))
  5057. goto fail;
  5058. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  5059. module, (uint64)arg_i32);
  5060. }
  5061. else if (signature[i + 1] == '$') {
  5062. /* param is a string */
  5063. if (!wasm_runtime_validate_app_str_addr(
  5064. module, (uint64)arg_i32))
  5065. goto fail;
  5066. arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native(
  5067. module, (uint64)arg_i32);
  5068. }
  5069. }
  5070. argv1[j++] = arg_i32;
  5071. break;
  5072. }
  5073. case VALUE_TYPE_I64:
  5074. case VALUE_TYPE_F64:
  5075. #if !defined(BUILD_TARGET_X86_32)
  5076. /* 64-bit data must be 8 bytes aligned in arm, thumb, mips
  5077. and xtensa */
  5078. if (j & 1)
  5079. j++;
  5080. #endif
  5081. argv1[j++] = *argv++;
  5082. argv1[j++] = *argv++;
  5083. break;
  5084. case VALUE_TYPE_F32:
  5085. argv1[j++] = *argv++;
  5086. break;
  5087. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5088. case VALUE_TYPE_EXTERNREF:
  5089. {
  5090. uint32 externref_idx = *argv++;
  5091. if (is_aot_func) {
  5092. argv1[j++] = externref_idx;
  5093. }
  5094. else {
  5095. void *externref_obj;
  5096. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  5097. goto fail;
  5098. argv1[j++] = (uintptr_t)externref_obj;
  5099. }
  5100. break;
  5101. }
  5102. #endif
  5103. default:
  5104. bh_assert(0);
  5105. break;
  5106. }
  5107. }
  5108. /* Save extra result values' address to argv1 */
  5109. word_copy(argv1 + j, argv, ext_ret_count);
  5110. argc1 = j + ext_ret_count;
  5111. exec_env->attachment = attachment;
  5112. if (func_type->result_count == 0) {
  5113. invokeNative_Void(func_ptr, argv1, argc1);
  5114. }
  5115. else {
  5116. switch (func_type->types[func_type->param_count]) {
  5117. case VALUE_TYPE_I32:
  5118. #if WASM_ENABLE_GC != 0
  5119. case REF_TYPE_FUNCREF:
  5120. case REF_TYPE_EXTERNREF:
  5121. case REF_TYPE_ANYREF:
  5122. case REF_TYPE_EQREF:
  5123. case REF_TYPE_HT_NULLABLE:
  5124. case REF_TYPE_HT_NON_NULLABLE:
  5125. case REF_TYPE_I31REF:
  5126. case REF_TYPE_NULLFUNCREF:
  5127. case REF_TYPE_NULLEXTERNREF:
  5128. case REF_TYPE_STRUCTREF:
  5129. case REF_TYPE_ARRAYREF:
  5130. case REF_TYPE_NULLREF:
  5131. #if WASM_ENABLE_STRINGREF != 0
  5132. case REF_TYPE_STRINGREF:
  5133. case REF_TYPE_STRINGVIEWWTF8:
  5134. case REF_TYPE_STRINGVIEWWTF16:
  5135. case REF_TYPE_STRINGVIEWITER:
  5136. #endif
  5137. #endif
  5138. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5139. case VALUE_TYPE_FUNCREF:
  5140. #endif
  5141. argv_ret[0] =
  5142. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  5143. break;
  5144. case VALUE_TYPE_I64:
  5145. PUT_I64_TO_ADDR(argv_ret,
  5146. invokeNative_Int64(func_ptr, argv1, argc1));
  5147. break;
  5148. case VALUE_TYPE_F32:
  5149. *(float32 *)argv_ret =
  5150. invokeNative_Float32(func_ptr, argv1, argc1);
  5151. break;
  5152. case VALUE_TYPE_F64:
  5153. PUT_F64_TO_ADDR(argv_ret,
  5154. invokeNative_Float64(func_ptr, argv1, argc1));
  5155. break;
  5156. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5157. case VALUE_TYPE_EXTERNREF:
  5158. {
  5159. if (is_aot_func) {
  5160. uint32 externref_idx =
  5161. (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  5162. argv_ret[0] = externref_idx;
  5163. }
  5164. else {
  5165. void *externref_obj = (void *)(uintptr_t)invokeNative_Int32(
  5166. func_ptr, argv1, argc1);
  5167. uint32 externref_idx;
  5168. if (!wasm_externref_obj2ref(exec_env->module_inst,
  5169. externref_obj, &externref_idx))
  5170. goto fail;
  5171. argv_ret[0] = externref_idx;
  5172. }
  5173. break;
  5174. }
  5175. #endif
  5176. default:
  5177. bh_assert(0);
  5178. break;
  5179. }
  5180. }
  5181. exec_env->attachment = NULL;
  5182. ret = !wasm_runtime_copy_exception(module, NULL);
  5183. fail:
  5184. if (argv1 != argv_buf)
  5185. wasm_runtime_free(argv1);
  5186. return ret;
  5187. }
  5188. #endif /* end of defined(BUILD_TARGET_X86_32) \
  5189. || defined(BUILD_TARGET_ARM) \
  5190. || defined(BUILD_TARGET_THUMB) \
  5191. || defined(BUILD_TARGET_MIPS) \
  5192. || defined(BUILD_TARGET_XTENSA) */
  5193. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  5194. || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
  5195. || defined(BUILD_TARGET_RISCV64_LP64)
  5196. #if WASM_ENABLE_SIMD != 0
  5197. #ifdef v128
  5198. #undef v128
  5199. #endif
  5200. #if defined(_WIN32) || defined(_WIN32_)
  5201. typedef union __declspec(intrin_type) __declspec(align(8)) v128 {
  5202. __int8 m128i_i8[16];
  5203. __int16 m128i_i16[8];
  5204. __int32 m128i_i32[4];
  5205. __int64 m128i_i64[2];
  5206. unsigned __int8 m128i_u8[16];
  5207. unsigned __int16 m128i_u16[8];
  5208. unsigned __int32 m128i_u32[4];
  5209. unsigned __int64 m128i_u64[2];
  5210. } v128;
  5211. #elif defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  5212. || defined(BUILD_TARGET_RISCV64_LP64D) \
  5213. || defined(BUILD_TARGET_RISCV64_LP64)
  5214. typedef long long v128
  5215. __attribute__((__vector_size__(16), __may_alias__, __aligned__(1)));
  5216. #elif defined(BUILD_TARGET_AARCH64)
  5217. #include <arm_neon.h>
  5218. typedef uint32x4_t __m128i;
  5219. #define v128 __m128i
  5220. #endif
  5221. #endif /* end of WASM_ENABLE_SIMD != 0 */
  5222. typedef void (*GenericFunctionPointer)(void);
  5223. void
  5224. invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks);
  5225. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  5226. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  5227. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  5228. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  5229. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  5230. /* NOLINTBEGIN */
  5231. static volatile Float64FuncPtr invokeNative_Float64 =
  5232. (Float64FuncPtr)(uintptr_t)invokeNative;
  5233. static volatile Float32FuncPtr invokeNative_Float32 =
  5234. (Float32FuncPtr)(uintptr_t)invokeNative;
  5235. static volatile Int64FuncPtr invokeNative_Int64 =
  5236. (Int64FuncPtr)(uintptr_t)invokeNative;
  5237. static volatile Int32FuncPtr invokeNative_Int32 =
  5238. (Int32FuncPtr)(uintptr_t)invokeNative;
  5239. static volatile VoidFuncPtr invokeNative_Void =
  5240. (VoidFuncPtr)(uintptr_t)invokeNative;
  5241. #if WASM_ENABLE_SIMD != 0
  5242. typedef v128 (*V128FuncPtr)(GenericFunctionPointer, uint64 *, uint64);
  5243. static V128FuncPtr invokeNative_V128 = (V128FuncPtr)(uintptr_t)invokeNative;
  5244. #endif
  5245. /* NOLINTEND */
  5246. #if defined(_WIN32) || defined(_WIN32_)
  5247. #define MAX_REG_FLOATS 4
  5248. #define MAX_REG_INTS 4
  5249. #else /* else of defined(_WIN32) || defined(_WIN32_) */
  5250. #define MAX_REG_FLOATS 8
  5251. #if defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \
  5252. || defined(BUILD_TARGET_RISCV64_LP64)
  5253. #define MAX_REG_INTS 8
  5254. #else
  5255. #define MAX_REG_INTS 6
  5256. #endif /* end of defined(BUILD_TARGET_AARCH64) \
  5257. || defined(BUILD_TARGET_RISCV64_LP64D) \
  5258. || defined(BUILD_TARGET_RISCV64_LP64) */
  5259. #endif /* end of defined(_WIN32) || defined(_WIN32_) */
  5260. /*
  5261. * ASAN is not designed to work with custom stack unwind or other low-level
  5262. * things. Ignore a function that does some low-level magic. (e.g. walking
  5263. * through the thread's stack bypassing the frame boundaries)
  5264. */
  5265. #if defined(__GNUC__) || defined(__clang__)
  5266. __attribute__((no_sanitize_address))
  5267. #endif
  5268. bool
  5269. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  5270. const WASMFuncType *func_type, const char *signature,
  5271. void *attachment, uint32 *argv, uint32 argc,
  5272. uint32 *argv_ret)
  5273. {
  5274. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  5275. #if WASM_ENABLE_MEMORY64 != 0
  5276. WASMMemoryInstance *memory =
  5277. wasm_get_default_memory((WASMModuleInstance *)module);
  5278. bool is_memory64 = memory ? memory->is_memory64 : false;
  5279. #endif
  5280. uint64 argv_buf[32] = { 0 }, *argv1 = argv_buf, *ints, *stacks, size,
  5281. arg_i64;
  5282. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  5283. uint32 arg_i32, ptr_len;
  5284. uint32 result_count = func_type->result_count;
  5285. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  5286. bool ret = false;
  5287. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5288. bool is_aot_func = (NULL == signature);
  5289. #endif
  5290. #ifndef BUILD_TARGET_RISCV64_LP64
  5291. #if WASM_ENABLE_SIMD == 0
  5292. uint64 *fps;
  5293. #else
  5294. v128 *fps;
  5295. #endif
  5296. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  5297. #define fps ints
  5298. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  5299. #if defined(_WIN32) || defined(_WIN32_) || defined(BUILD_TARGET_RISCV64_LP64)
  5300. /* important difference in calling conventions */
  5301. #define n_fps n_ints
  5302. #else
  5303. int n_fps = 0;
  5304. #endif
  5305. #if WASM_ENABLE_SIMD == 0
  5306. argc1 = 1 + MAX_REG_FLOATS + (uint32)func_type->param_count + ext_ret_count;
  5307. #else
  5308. argc1 = 1 + MAX_REG_FLOATS * 2 + (uint32)func_type->param_count * 2
  5309. + ext_ret_count;
  5310. #endif
  5311. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  5312. size = sizeof(uint64) * (uint64)argc1;
  5313. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL,
  5314. 0))) {
  5315. return false;
  5316. }
  5317. }
  5318. #ifndef BUILD_TARGET_RISCV64_LP64
  5319. #if WASM_ENABLE_SIMD == 0
  5320. fps = argv1;
  5321. ints = fps + MAX_REG_FLOATS;
  5322. #else
  5323. fps = (v128 *)argv1;
  5324. ints = (uint64 *)(fps + MAX_REG_FLOATS);
  5325. #endif
  5326. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  5327. ints = argv1;
  5328. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  5329. stacks = ints + MAX_REG_INTS;
  5330. ints[n_ints++] = (uint64)(uintptr_t)exec_env;
  5331. for (i = 0; i < func_type->param_count; i++) {
  5332. switch (func_type->types[i]) {
  5333. case VALUE_TYPE_I32:
  5334. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5335. case VALUE_TYPE_FUNCREF:
  5336. #endif
  5337. {
  5338. arg_i32 = *argv_src++;
  5339. arg_i64 = arg_i32;
  5340. if (signature
  5341. #if WASM_ENABLE_MEMORY64 != 0
  5342. && !is_memory64
  5343. #endif
  5344. ) {
  5345. if (signature[i + 1] == '*') {
  5346. /* param is a pointer */
  5347. if (signature[i + 2] == '~')
  5348. /* pointer with length followed */
  5349. ptr_len = *argv_src;
  5350. else
  5351. /* pointer without length followed */
  5352. ptr_len = 1;
  5353. if (!wasm_runtime_validate_app_addr(
  5354. module, (uint64)arg_i32, (uint64)ptr_len))
  5355. goto fail;
  5356. arg_i64 = (uintptr_t)wasm_runtime_addr_app_to_native(
  5357. module, (uint64)arg_i32);
  5358. }
  5359. else if (signature[i + 1] == '$') {
  5360. /* param is a string */
  5361. if (!wasm_runtime_validate_app_str_addr(
  5362. module, (uint64)arg_i32))
  5363. goto fail;
  5364. arg_i64 = (uintptr_t)wasm_runtime_addr_app_to_native(
  5365. module, (uint64)arg_i32);
  5366. }
  5367. }
  5368. if (n_ints < MAX_REG_INTS)
  5369. ints[n_ints++] = arg_i64;
  5370. else
  5371. stacks[n_stacks++] = arg_i64;
  5372. break;
  5373. }
  5374. case VALUE_TYPE_I64:
  5375. #if WASM_ENABLE_MEMORY64 != 0
  5376. {
  5377. arg_i64 = GET_I64_FROM_ADDR(argv_src);
  5378. argv_src += 2;
  5379. if (signature && is_memory64) {
  5380. /* TODO: memory64 pointer with length need a new symbol
  5381. * to represent type i64, with '~' still represent i32
  5382. * length */
  5383. if (signature[i + 1] == '*') {
  5384. /* param is a pointer */
  5385. if (signature[i + 2] == '~')
  5386. /* pointer with length followed */
  5387. ptr_len = *argv_src;
  5388. else
  5389. /* pointer without length followed */
  5390. ptr_len = 1;
  5391. if (!wasm_runtime_validate_app_addr(module, arg_i64,
  5392. (uint64)ptr_len))
  5393. goto fail;
  5394. arg_i64 = (uint64)wasm_runtime_addr_app_to_native(
  5395. module, arg_i64);
  5396. }
  5397. else if (signature[i + 1] == '$') {
  5398. /* param is a string */
  5399. if (!wasm_runtime_validate_app_str_addr(module,
  5400. arg_i64))
  5401. goto fail;
  5402. arg_i64 = (uint64)wasm_runtime_addr_app_to_native(
  5403. module, arg_i64);
  5404. }
  5405. }
  5406. if (n_ints < MAX_REG_INTS)
  5407. ints[n_ints++] = arg_i64;
  5408. else
  5409. stacks[n_stacks++] = arg_i64;
  5410. break;
  5411. }
  5412. #endif
  5413. #if WASM_ENABLE_GC != 0
  5414. case REF_TYPE_FUNCREF:
  5415. case REF_TYPE_EXTERNREF:
  5416. case REF_TYPE_ANYREF:
  5417. case REF_TYPE_EQREF:
  5418. case REF_TYPE_HT_NULLABLE:
  5419. case REF_TYPE_HT_NON_NULLABLE:
  5420. case REF_TYPE_I31REF:
  5421. case REF_TYPE_NULLFUNCREF:
  5422. case REF_TYPE_NULLEXTERNREF:
  5423. case REF_TYPE_STRUCTREF:
  5424. case REF_TYPE_ARRAYREF:
  5425. case REF_TYPE_NULLREF:
  5426. #if WASM_ENABLE_STRINGREF != 0
  5427. case REF_TYPE_STRINGREF:
  5428. case REF_TYPE_STRINGVIEWWTF8:
  5429. case REF_TYPE_STRINGVIEWWTF16:
  5430. case REF_TYPE_STRINGVIEWITER:
  5431. #endif
  5432. #endif
  5433. if (n_ints < MAX_REG_INTS)
  5434. ints[n_ints++] = *(uint64 *)argv_src;
  5435. else
  5436. stacks[n_stacks++] = *(uint64 *)argv_src;
  5437. argv_src += 2;
  5438. break;
  5439. case VALUE_TYPE_F32:
  5440. if (n_fps < MAX_REG_FLOATS) {
  5441. *(float32 *)&fps[n_fps++] = *(float32 *)argv_src++;
  5442. }
  5443. else {
  5444. *(float32 *)&stacks[n_stacks++] = *(float32 *)argv_src++;
  5445. }
  5446. break;
  5447. case VALUE_TYPE_F64:
  5448. if (n_fps < MAX_REG_FLOATS) {
  5449. *(float64 *)&fps[n_fps++] = *(float64 *)argv_src;
  5450. }
  5451. else {
  5452. *(float64 *)&stacks[n_stacks++] = *(float64 *)argv_src;
  5453. }
  5454. argv_src += 2;
  5455. break;
  5456. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5457. case VALUE_TYPE_EXTERNREF:
  5458. {
  5459. uint32 externref_idx = *argv_src++;
  5460. if (is_aot_func) {
  5461. if (n_ints < MAX_REG_INTS)
  5462. ints[n_ints++] = externref_idx;
  5463. else
  5464. stacks[n_stacks++] = externref_idx;
  5465. }
  5466. else {
  5467. void *externref_obj;
  5468. if (!wasm_externref_ref2obj(externref_idx, &externref_obj))
  5469. goto fail;
  5470. if (n_ints < MAX_REG_INTS)
  5471. ints[n_ints++] = (uintptr_t)externref_obj;
  5472. else
  5473. stacks[n_stacks++] = (uintptr_t)externref_obj;
  5474. }
  5475. break;
  5476. }
  5477. #endif
  5478. #if WASM_ENABLE_SIMD != 0
  5479. case VALUE_TYPE_V128:
  5480. if (n_fps < MAX_REG_FLOATS) {
  5481. *(v128 *)&fps[n_fps++] = *(v128 *)argv_src;
  5482. }
  5483. else {
  5484. *(v128 *)&stacks[n_stacks++] = *(v128 *)argv_src;
  5485. n_stacks++;
  5486. }
  5487. argv_src += 4;
  5488. break;
  5489. #endif
  5490. default:
  5491. bh_assert(0);
  5492. break;
  5493. }
  5494. }
  5495. /* Save extra result values' address to argv1 */
  5496. for (i = 0; i < ext_ret_count; i++) {
  5497. if (n_ints < MAX_REG_INTS)
  5498. ints[n_ints++] = *(uint64 *)argv_src;
  5499. else
  5500. stacks[n_stacks++] = *(uint64 *)argv_src;
  5501. argv_src += 2;
  5502. }
  5503. exec_env->attachment = attachment;
  5504. if (result_count == 0) {
  5505. invokeNative_Void(func_ptr, argv1, n_stacks);
  5506. }
  5507. else {
  5508. /* Invoke the native function and get the first result value */
  5509. switch (func_type->types[func_type->param_count]) {
  5510. case VALUE_TYPE_I32:
  5511. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5512. case VALUE_TYPE_FUNCREF:
  5513. #endif
  5514. argv_ret[0] =
  5515. (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  5516. break;
  5517. case VALUE_TYPE_I64:
  5518. #if WASM_ENABLE_GC != 0
  5519. case REF_TYPE_FUNCREF:
  5520. case REF_TYPE_EXTERNREF:
  5521. case REF_TYPE_ANYREF:
  5522. case REF_TYPE_EQREF:
  5523. case REF_TYPE_HT_NULLABLE:
  5524. case REF_TYPE_HT_NON_NULLABLE:
  5525. case REF_TYPE_I31REF:
  5526. case REF_TYPE_NULLFUNCREF:
  5527. case REF_TYPE_NULLEXTERNREF:
  5528. case REF_TYPE_STRUCTREF:
  5529. case REF_TYPE_ARRAYREF:
  5530. case REF_TYPE_NULLREF:
  5531. #if WASM_ENABLE_STRINGREF != 0
  5532. case REF_TYPE_STRINGREF:
  5533. case REF_TYPE_STRINGVIEWWTF8:
  5534. case REF_TYPE_STRINGVIEWWTF16:
  5535. case REF_TYPE_STRINGVIEWITER:
  5536. #endif
  5537. #endif
  5538. PUT_I64_TO_ADDR(argv_ret,
  5539. invokeNative_Int64(func_ptr, argv1, n_stacks));
  5540. break;
  5541. case VALUE_TYPE_F32:
  5542. *(float32 *)argv_ret =
  5543. invokeNative_Float32(func_ptr, argv1, n_stacks);
  5544. break;
  5545. case VALUE_TYPE_F64:
  5546. PUT_F64_TO_ADDR(
  5547. argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  5548. break;
  5549. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5550. case VALUE_TYPE_EXTERNREF:
  5551. {
  5552. if (is_aot_func) {
  5553. argv_ret[0] = invokeNative_Int32(func_ptr, argv1, n_stacks);
  5554. }
  5555. else {
  5556. uint32 externref_idx;
  5557. void *externref_obj = (void *)(uintptr_t)invokeNative_Int64(
  5558. func_ptr, argv1, n_stacks);
  5559. if (!wasm_externref_obj2ref(exec_env->module_inst,
  5560. externref_obj, &externref_idx))
  5561. goto fail;
  5562. argv_ret[0] = externref_idx;
  5563. }
  5564. break;
  5565. }
  5566. #endif
  5567. #if WASM_ENABLE_SIMD != 0
  5568. case VALUE_TYPE_V128:
  5569. *(v128 *)argv_ret =
  5570. invokeNative_V128(func_ptr, argv1, n_stacks);
  5571. break;
  5572. #endif
  5573. default:
  5574. bh_assert(0);
  5575. break;
  5576. }
  5577. }
  5578. exec_env->attachment = NULL;
  5579. ret = !wasm_runtime_copy_exception(module, NULL);
  5580. fail:
  5581. if (argv1 != argv_buf)
  5582. wasm_runtime_free(argv1);
  5583. return ret;
  5584. }
  5585. #endif /* end of defined(BUILD_TARGET_X86_64) \
  5586. || defined(BUILD_TARGET_AMD_64) \
  5587. || defined(BUILD_TARGET_AARCH64) \
  5588. || defined(BUILD_TARGET_RISCV64_LP64D) \
  5589. || defined(BUILD_TARGET_RISCV64_LP64) */
  5590. bool
  5591. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index,
  5592. uint32 argc, uint32 argv[])
  5593. {
  5594. bool ret = false;
  5595. if (!wasm_runtime_exec_env_check(exec_env)) {
  5596. LOG_ERROR("Invalid exec env stack info.");
  5597. return false;
  5598. }
  5599. /* this function is called from native code, so exec_env->handle and
  5600. exec_env->native_stack_boundary must have been set, we don't set
  5601. it again */
  5602. #if WASM_ENABLE_INTERP != 0
  5603. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  5604. ret = wasm_call_indirect(exec_env, 0, element_index, argc, argv);
  5605. #endif
  5606. #if WASM_ENABLE_AOT != 0
  5607. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  5608. ret = aot_call_indirect(exec_env, 0, element_index, argc, argv);
  5609. #endif
  5610. return ret;
  5611. }
  5612. static void
  5613. exchange_uint32(uint8 *p_data)
  5614. {
  5615. uint8 value = *p_data;
  5616. *p_data = *(p_data + 3);
  5617. *(p_data + 3) = value;
  5618. value = *(p_data + 1);
  5619. *(p_data + 1) = *(p_data + 2);
  5620. *(p_data + 2) = value;
  5621. }
  5622. static void
  5623. exchange_uint64(uint8 *p_data)
  5624. {
  5625. uint32 value;
  5626. value = *(uint32 *)p_data;
  5627. *(uint32 *)p_data = *(uint32 *)(p_data + 4);
  5628. *(uint32 *)(p_data + 4) = value;
  5629. exchange_uint32(p_data);
  5630. exchange_uint32(p_data + 4);
  5631. }
  5632. void
  5633. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2)
  5634. {
  5635. uint64 u1, u2;
  5636. bh_memcpy_s(&u1, 8, bytes, 8);
  5637. bh_memcpy_s(&u2, 8, bytes + 8, 8);
  5638. if (!is_little_endian()) {
  5639. exchange_uint64((uint8 *)&u1);
  5640. exchange_uint64((uint8 *)&u2);
  5641. *ret1 = u2;
  5642. *ret2 = u1;
  5643. }
  5644. else {
  5645. *ret1 = u1;
  5646. *ret2 = u2;
  5647. }
  5648. }
  5649. #if WASM_ENABLE_THREAD_MGR != 0
  5650. typedef struct WASMThreadArg {
  5651. WASMExecEnv *new_exec_env;
  5652. wasm_thread_callback_t callback;
  5653. void *arg;
  5654. } WASMThreadArg;
  5655. WASMExecEnv *
  5656. wasm_runtime_spawn_exec_env(WASMExecEnv *exec_env)
  5657. {
  5658. return wasm_cluster_spawn_exec_env(exec_env);
  5659. }
  5660. void
  5661. wasm_runtime_destroy_spawned_exec_env(WASMExecEnv *exec_env)
  5662. {
  5663. wasm_cluster_destroy_spawned_exec_env(exec_env);
  5664. }
  5665. static void *
  5666. wasm_runtime_thread_routine(void *arg)
  5667. {
  5668. WASMThreadArg *thread_arg = (WASMThreadArg *)arg;
  5669. void *ret;
  5670. bh_assert(thread_arg->new_exec_env);
  5671. ret = thread_arg->callback(thread_arg->new_exec_env, thread_arg->arg);
  5672. wasm_runtime_destroy_spawned_exec_env(thread_arg->new_exec_env);
  5673. wasm_runtime_free(thread_arg);
  5674. os_thread_exit(ret);
  5675. return ret;
  5676. }
  5677. int32
  5678. wasm_runtime_spawn_thread(WASMExecEnv *exec_env, wasm_thread_t *tid,
  5679. wasm_thread_callback_t callback, void *arg)
  5680. {
  5681. WASMExecEnv *new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
  5682. WASMThreadArg *thread_arg;
  5683. int32 ret;
  5684. if (!new_exec_env)
  5685. return -1;
  5686. if (!(thread_arg = wasm_runtime_malloc(sizeof(WASMThreadArg)))) {
  5687. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  5688. return -1;
  5689. }
  5690. thread_arg->new_exec_env = new_exec_env;
  5691. thread_arg->callback = callback;
  5692. thread_arg->arg = arg;
  5693. ret = os_thread_create((korp_tid *)tid, wasm_runtime_thread_routine,
  5694. thread_arg, APP_THREAD_STACK_SIZE_DEFAULT);
  5695. if (ret != 0) {
  5696. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  5697. wasm_runtime_free(thread_arg);
  5698. }
  5699. return ret;
  5700. }
  5701. int32
  5702. wasm_runtime_join_thread(wasm_thread_t tid, void **retval)
  5703. {
  5704. return os_thread_join((korp_tid)tid, retval);
  5705. }
  5706. #endif /* end of WASM_ENABLE_THREAD_MGR */
  5707. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5708. static korp_mutex externref_lock;
  5709. static uint32 externref_global_id = 1;
  5710. static HashMap *externref_map;
  5711. typedef struct ExternRefMapNode {
  5712. /* The extern object from runtime embedder */
  5713. void *extern_obj;
  5714. /* The module instance it belongs to */
  5715. WASMModuleInstanceCommon *module_inst;
  5716. /* Whether it is retained */
  5717. bool retained;
  5718. /* Whether it is marked by runtime */
  5719. bool marked;
  5720. /* cleanup function called when the externref is freed */
  5721. void (*cleanup)(void *);
  5722. } ExternRefMapNode;
  5723. static uint32
  5724. wasm_externref_hash(const void *key)
  5725. {
  5726. uint32 externref_idx = (uint32)(uintptr_t)key;
  5727. return externref_idx;
  5728. }
  5729. static bool
  5730. wasm_externref_equal(void *key1, void *key2)
  5731. {
  5732. uint32 externref_idx1 = (uint32)(uintptr_t)key1;
  5733. uint32 externref_idx2 = (uint32)(uintptr_t)key2;
  5734. return externref_idx1 == externref_idx2 ? true : false;
  5735. }
  5736. static bool
  5737. wasm_externref_map_init()
  5738. {
  5739. if (os_mutex_init(&externref_lock) != 0)
  5740. return false;
  5741. if (!(externref_map = bh_hash_map_create(32, false, wasm_externref_hash,
  5742. wasm_externref_equal, NULL,
  5743. wasm_runtime_free))) {
  5744. os_mutex_destroy(&externref_lock);
  5745. return false;
  5746. }
  5747. externref_global_id = 1;
  5748. return true;
  5749. }
  5750. static void
  5751. wasm_externref_map_destroy()
  5752. {
  5753. bh_hash_map_destroy(externref_map);
  5754. os_mutex_destroy(&externref_lock);
  5755. }
  5756. typedef struct LookupExtObj_UserData {
  5757. ExternRefMapNode node;
  5758. bool found;
  5759. uint32 externref_idx;
  5760. } LookupExtObj_UserData;
  5761. static void
  5762. lookup_extobj_callback(void *key, void *value, void *user_data)
  5763. {
  5764. uint32 externref_idx = (uint32)(uintptr_t)key;
  5765. ExternRefMapNode *node = (ExternRefMapNode *)value;
  5766. LookupExtObj_UserData *user_data_lookup =
  5767. (LookupExtObj_UserData *)user_data;
  5768. if (node->extern_obj == user_data_lookup->node.extern_obj
  5769. && node->module_inst == user_data_lookup->node.module_inst) {
  5770. user_data_lookup->found = true;
  5771. user_data_lookup->externref_idx = externref_idx;
  5772. }
  5773. }
  5774. static void
  5775. delete_externref(void *key, ExternRefMapNode *node)
  5776. {
  5777. bh_hash_map_remove(externref_map, key, NULL, NULL);
  5778. if (node->cleanup) {
  5779. (*node->cleanup)(node->extern_obj);
  5780. }
  5781. wasm_runtime_free(node);
  5782. }
  5783. static void
  5784. delete_extobj_callback(void *key, void *value, void *user_data)
  5785. {
  5786. ExternRefMapNode *node = (ExternRefMapNode *)value;
  5787. LookupExtObj_UserData *lookup_user_data =
  5788. (LookupExtObj_UserData *)user_data;
  5789. if (node->extern_obj == lookup_user_data->node.extern_obj
  5790. && node->module_inst == lookup_user_data->node.module_inst) {
  5791. lookup_user_data->found = true;
  5792. delete_externref(key, node);
  5793. }
  5794. }
  5795. bool
  5796. wasm_externref_objdel(WASMModuleInstanceCommon *module_inst, void *extern_obj)
  5797. {
  5798. LookupExtObj_UserData lookup_user_data = { 0 };
  5799. bool ok = false;
  5800. /* in a wrapper, extern_obj could be any value */
  5801. lookup_user_data.node.extern_obj = extern_obj;
  5802. lookup_user_data.node.module_inst = module_inst;
  5803. lookup_user_data.found = false;
  5804. os_mutex_lock(&externref_lock);
  5805. /* Lookup hashmap firstly */
  5806. bh_hash_map_traverse(externref_map, delete_extobj_callback,
  5807. (void *)&lookup_user_data);
  5808. if (lookup_user_data.found) {
  5809. ok = true;
  5810. }
  5811. os_mutex_unlock(&externref_lock);
  5812. return ok;
  5813. }
  5814. bool
  5815. wasm_externref_set_cleanup(WASMModuleInstanceCommon *module_inst,
  5816. void *extern_obj, void (*extern_obj_cleanup)(void *))
  5817. {
  5818. LookupExtObj_UserData lookup_user_data = { 0 };
  5819. bool ok = false;
  5820. /* in a wrapper, extern_obj could be any value */
  5821. lookup_user_data.node.extern_obj = extern_obj;
  5822. lookup_user_data.node.module_inst = module_inst;
  5823. lookup_user_data.found = false;
  5824. os_mutex_lock(&externref_lock);
  5825. /* Lookup hashmap firstly */
  5826. bh_hash_map_traverse(externref_map, lookup_extobj_callback,
  5827. (void *)&lookup_user_data);
  5828. if (lookup_user_data.found) {
  5829. void *key = (void *)(uintptr_t)lookup_user_data.externref_idx;
  5830. ExternRefMapNode *node = bh_hash_map_find(externref_map, key);
  5831. bh_assert(node);
  5832. node->cleanup = extern_obj_cleanup;
  5833. ok = true;
  5834. }
  5835. os_mutex_unlock(&externref_lock);
  5836. return ok;
  5837. }
  5838. bool
  5839. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
  5840. uint32 *p_externref_idx)
  5841. {
  5842. LookupExtObj_UserData lookup_user_data = { 0 };
  5843. ExternRefMapNode *node;
  5844. uint32 externref_idx;
  5845. /*
  5846. * to catch a parameter from `wasm_application_execute_func`,
  5847. * which represents a string 'null'
  5848. */
  5849. #if UINTPTR_MAX == UINT32_MAX
  5850. if ((uint32)-1 == (uintptr_t)extern_obj) {
  5851. #else
  5852. if ((uint64)-1LL == (uintptr_t)extern_obj) {
  5853. #endif
  5854. *p_externref_idx = NULL_REF;
  5855. return true;
  5856. }
  5857. /* in a wrapper, extern_obj could be any value */
  5858. lookup_user_data.node.extern_obj = extern_obj;
  5859. lookup_user_data.node.module_inst = module_inst;
  5860. lookup_user_data.found = false;
  5861. os_mutex_lock(&externref_lock);
  5862. /* Lookup hashmap firstly */
  5863. bh_hash_map_traverse(externref_map, lookup_extobj_callback,
  5864. (void *)&lookup_user_data);
  5865. if (lookup_user_data.found) {
  5866. *p_externref_idx = lookup_user_data.externref_idx;
  5867. os_mutex_unlock(&externref_lock);
  5868. return true;
  5869. }
  5870. /* Not found in hashmap */
  5871. if (externref_global_id == NULL_REF || externref_global_id == 0) {
  5872. goto fail1;
  5873. }
  5874. if (!(node = wasm_runtime_malloc(sizeof(ExternRefMapNode)))) {
  5875. goto fail1;
  5876. }
  5877. memset(node, 0, sizeof(ExternRefMapNode));
  5878. node->extern_obj = extern_obj;
  5879. node->module_inst = module_inst;
  5880. node->cleanup = NULL;
  5881. externref_idx = externref_global_id;
  5882. if (!bh_hash_map_insert(externref_map, (void *)(uintptr_t)externref_idx,
  5883. (void *)node)) {
  5884. goto fail2;
  5885. }
  5886. externref_global_id++;
  5887. *p_externref_idx = externref_idx;
  5888. os_mutex_unlock(&externref_lock);
  5889. return true;
  5890. fail2:
  5891. wasm_runtime_free(node);
  5892. fail1:
  5893. os_mutex_unlock(&externref_lock);
  5894. return false;
  5895. }
  5896. bool
  5897. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj)
  5898. {
  5899. ExternRefMapNode *node;
  5900. /* catch a `ref.null` variable */
  5901. if (externref_idx == NULL_REF) {
  5902. *p_extern_obj = NULL;
  5903. return true;
  5904. }
  5905. os_mutex_lock(&externref_lock);
  5906. node = bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  5907. os_mutex_unlock(&externref_lock);
  5908. if (!node)
  5909. return false;
  5910. *p_extern_obj = node->extern_obj;
  5911. return true;
  5912. }
  5913. static void
  5914. reclaim_extobj_callback(void *key, void *value, void *user_data)
  5915. {
  5916. ExternRefMapNode *node = (ExternRefMapNode *)value;
  5917. WASMModuleInstanceCommon *module_inst =
  5918. (WASMModuleInstanceCommon *)user_data;
  5919. if (node->module_inst == module_inst) {
  5920. if (!node->marked && !node->retained) {
  5921. delete_externref(key, node);
  5922. }
  5923. else {
  5924. node->marked = false;
  5925. }
  5926. }
  5927. }
  5928. static void
  5929. mark_externref(uint32 externref_idx)
  5930. {
  5931. ExternRefMapNode *node;
  5932. if (externref_idx != NULL_REF) {
  5933. node =
  5934. bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  5935. if (node) {
  5936. node->marked = true;
  5937. }
  5938. }
  5939. }
  5940. #if WASM_ENABLE_INTERP != 0
  5941. static void
  5942. interp_mark_all_externrefs(WASMModuleInstance *module_inst)
  5943. {
  5944. uint32 i, j, externref_idx;
  5945. table_elem_type_t *table_data;
  5946. uint8 *global_data = module_inst->global_data;
  5947. WASMGlobalInstance *global;
  5948. WASMTableInstance *table;
  5949. global = module_inst->e->globals;
  5950. for (i = 0; i < module_inst->e->global_count; i++, global++) {
  5951. if (global->type == VALUE_TYPE_EXTERNREF) {
  5952. externref_idx = *(uint32 *)(global_data + global->data_offset);
  5953. mark_externref(externref_idx);
  5954. }
  5955. }
  5956. for (i = 0; i < module_inst->table_count; i++) {
  5957. uint8 elem_type = 0;
  5958. uint32 init_size, max_size;
  5959. table = wasm_get_table_inst(module_inst, i);
  5960. (void)wasm_runtime_get_table_inst_elem_type(
  5961. (WASMModuleInstanceCommon *)module_inst, i, &elem_type, &init_size,
  5962. &max_size);
  5963. if (elem_type == VALUE_TYPE_EXTERNREF) {
  5964. table_data = table->elems;
  5965. for (j = 0; j < table->cur_size; j++) {
  5966. externref_idx = table_data[j];
  5967. mark_externref(externref_idx);
  5968. }
  5969. }
  5970. (void)init_size;
  5971. (void)max_size;
  5972. }
  5973. }
  5974. #endif
  5975. #if WASM_ENABLE_AOT != 0
  5976. static void
  5977. aot_mark_all_externrefs(AOTModuleInstance *module_inst)
  5978. {
  5979. uint32 i = 0, j = 0;
  5980. const AOTModule *module = (AOTModule *)module_inst->module;
  5981. const AOTTable *table = module->tables;
  5982. const AOTGlobal *global = module->globals;
  5983. const AOTTableInstance *table_inst;
  5984. for (i = 0; i < module->global_count; i++, global++) {
  5985. if (global->type.val_type == VALUE_TYPE_EXTERNREF) {
  5986. mark_externref(
  5987. *(uint32 *)(module_inst->global_data + global->data_offset));
  5988. }
  5989. }
  5990. for (i = 0; i < module->table_count; i++) {
  5991. table_inst = module_inst->tables[i];
  5992. if ((table + i)->table_type.elem_type == VALUE_TYPE_EXTERNREF) {
  5993. while (j < table_inst->cur_size) {
  5994. mark_externref(table_inst->elems[j++]);
  5995. }
  5996. }
  5997. }
  5998. }
  5999. #endif
  6000. void
  6001. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst)
  6002. {
  6003. os_mutex_lock(&externref_lock);
  6004. #if WASM_ENABLE_INTERP != 0
  6005. if (module_inst->module_type == Wasm_Module_Bytecode)
  6006. interp_mark_all_externrefs((WASMModuleInstance *)module_inst);
  6007. #endif
  6008. #if WASM_ENABLE_AOT != 0
  6009. if (module_inst->module_type == Wasm_Module_AoT)
  6010. aot_mark_all_externrefs((AOTModuleInstance *)module_inst);
  6011. #endif
  6012. bh_hash_map_traverse(externref_map, reclaim_extobj_callback,
  6013. (void *)module_inst);
  6014. os_mutex_unlock(&externref_lock);
  6015. }
  6016. static void
  6017. cleanup_extobj_callback(void *key, void *value, void *user_data)
  6018. {
  6019. ExternRefMapNode *node = (ExternRefMapNode *)value;
  6020. WASMModuleInstanceCommon *module_inst =
  6021. (WASMModuleInstanceCommon *)user_data;
  6022. if (node->module_inst == module_inst) {
  6023. delete_externref(key, node);
  6024. }
  6025. }
  6026. void
  6027. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst)
  6028. {
  6029. os_mutex_lock(&externref_lock);
  6030. bh_hash_map_traverse(externref_map, cleanup_extobj_callback,
  6031. (void *)module_inst);
  6032. os_mutex_unlock(&externref_lock);
  6033. }
  6034. bool
  6035. wasm_externref_retain(uint32 externref_idx)
  6036. {
  6037. ExternRefMapNode *node;
  6038. os_mutex_lock(&externref_lock);
  6039. if (externref_idx != NULL_REF) {
  6040. node =
  6041. bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx);
  6042. if (node) {
  6043. node->retained = true;
  6044. os_mutex_unlock(&externref_lock);
  6045. return true;
  6046. }
  6047. }
  6048. os_mutex_unlock(&externref_lock);
  6049. return false;
  6050. }
  6051. #endif /* end of WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 */
  6052. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  6053. uint32
  6054. wasm_runtime_dump_line_buf_impl(const char *line_buf, bool dump_or_print,
  6055. char **buf, uint32 *len)
  6056. {
  6057. if (dump_or_print) {
  6058. return (uint32)os_printf("%s", line_buf);
  6059. }
  6060. else if (*buf) {
  6061. uint32 dump_len;
  6062. dump_len = snprintf(*buf, *len, "%s", line_buf);
  6063. if (dump_len >= *len) {
  6064. dump_len = *len;
  6065. }
  6066. *len = *len - dump_len;
  6067. *buf = *buf + dump_len;
  6068. return dump_len;
  6069. }
  6070. else {
  6071. return (uint32)strlen(line_buf);
  6072. }
  6073. }
  6074. void
  6075. wasm_runtime_dump_call_stack(WASMExecEnv *exec_env)
  6076. {
  6077. WASMModuleInstanceCommon *module_inst =
  6078. wasm_exec_env_get_module_inst(exec_env);
  6079. #if WASM_ENABLE_INTERP != 0
  6080. if (module_inst->module_type == Wasm_Module_Bytecode) {
  6081. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  6082. }
  6083. #endif
  6084. #if WASM_ENABLE_AOT != 0
  6085. if (module_inst->module_type == Wasm_Module_AoT) {
  6086. aot_dump_call_stack(exec_env, true, NULL, 0);
  6087. }
  6088. #endif
  6089. }
  6090. uint32
  6091. wasm_runtime_get_call_stack_buf_size(wasm_exec_env_t exec_env)
  6092. {
  6093. WASMModuleInstanceCommon *module_inst =
  6094. wasm_exec_env_get_module_inst(exec_env);
  6095. #if WASM_ENABLE_INTERP != 0
  6096. if (module_inst->module_type == Wasm_Module_Bytecode) {
  6097. return wasm_interp_dump_call_stack(exec_env, false, NULL, 0);
  6098. }
  6099. #endif
  6100. #if WASM_ENABLE_AOT != 0
  6101. if (module_inst->module_type == Wasm_Module_AoT) {
  6102. return aot_dump_call_stack(exec_env, false, NULL, 0);
  6103. }
  6104. #endif
  6105. return 0;
  6106. }
  6107. uint32
  6108. wasm_runtime_dump_call_stack_to_buf(wasm_exec_env_t exec_env, char *buf,
  6109. uint32 len)
  6110. {
  6111. WASMModuleInstanceCommon *module_inst =
  6112. wasm_exec_env_get_module_inst(exec_env);
  6113. #if WASM_ENABLE_INTERP != 0
  6114. if (module_inst->module_type == Wasm_Module_Bytecode) {
  6115. return wasm_interp_dump_call_stack(exec_env, false, buf, len);
  6116. }
  6117. #endif
  6118. #if WASM_ENABLE_AOT != 0
  6119. if (module_inst->module_type == Wasm_Module_AoT) {
  6120. return aot_dump_call_stack(exec_env, false, buf, len);
  6121. }
  6122. #endif
  6123. return 0;
  6124. }
  6125. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
  6126. #if WASM_ENABLE_STATIC_PGO != 0
  6127. uint32
  6128. wasm_runtime_get_pgo_prof_data_size(WASMModuleInstanceCommon *module_inst)
  6129. {
  6130. #if WASM_ENABLE_AOT != 0
  6131. if (module_inst->module_type == Wasm_Module_AoT) {
  6132. AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
  6133. return aot_get_pgo_prof_data_size(aot_inst);
  6134. }
  6135. #endif
  6136. return 0;
  6137. }
  6138. uint32
  6139. wasm_runtime_dump_pgo_prof_data_to_buf(WASMModuleInstanceCommon *module_inst,
  6140. char *buf, uint32 len)
  6141. {
  6142. #if WASM_ENABLE_AOT != 0
  6143. if (module_inst->module_type == Wasm_Module_AoT) {
  6144. AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst;
  6145. return aot_dump_pgo_prof_data_to_buf(aot_inst, buf, len);
  6146. }
  6147. #endif
  6148. return 0;
  6149. }
  6150. #endif /* end of WASM_ENABLE_STATIC_PGO != 0 */
  6151. bool
  6152. wasm_runtime_get_table_elem_type(const WASMModuleCommon *module_comm,
  6153. uint32 table_idx, uint8 *out_elem_type,
  6154. #if WASM_ENABLE_GC != 0
  6155. WASMRefType **out_ref_type,
  6156. #endif
  6157. uint32 *out_min_size, uint32 *out_max_size)
  6158. {
  6159. #if WASM_ENABLE_INTERP != 0
  6160. if (module_comm->module_type == Wasm_Module_Bytecode) {
  6161. WASMModule *module = (WASMModule *)module_comm;
  6162. if (table_idx < module->import_table_count) {
  6163. WASMTableImport *import_table =
  6164. &((module->import_tables + table_idx)->u.table);
  6165. *out_elem_type = import_table->table_type.elem_type;
  6166. #if WASM_ENABLE_GC != 0
  6167. *out_ref_type = import_table->table_type.elem_ref_type;
  6168. #endif
  6169. *out_min_size = import_table->table_type.init_size;
  6170. *out_max_size = import_table->table_type.max_size;
  6171. }
  6172. else {
  6173. WASMTable *table =
  6174. module->tables + (table_idx - module->import_table_count);
  6175. *out_elem_type = table->table_type.elem_type;
  6176. #if WASM_ENABLE_GC != 0
  6177. *out_ref_type = table->table_type.elem_ref_type;
  6178. #endif
  6179. *out_min_size = table->table_type.init_size;
  6180. *out_max_size = table->table_type.max_size;
  6181. }
  6182. return true;
  6183. }
  6184. #endif
  6185. #if WASM_ENABLE_AOT != 0
  6186. if (module_comm->module_type == Wasm_Module_AoT) {
  6187. AOTModule *module = (AOTModule *)module_comm;
  6188. if (table_idx < module->import_table_count) {
  6189. AOTImportTable *import_table = module->import_tables + table_idx;
  6190. *out_elem_type = import_table->table_type.elem_type;
  6191. #if WASM_ENABLE_GC != 0
  6192. *out_ref_type = NULL; /* TODO */
  6193. #endif
  6194. *out_min_size = import_table->table_type.init_size;
  6195. *out_max_size = import_table->table_type.max_size;
  6196. }
  6197. else {
  6198. AOTTable *table =
  6199. module->tables + (table_idx - module->import_table_count);
  6200. *out_elem_type = table->table_type.elem_type;
  6201. #if WASM_ENABLE_GC != 0
  6202. *out_ref_type = NULL; /* TODO */
  6203. #endif
  6204. *out_min_size = table->table_type.init_size;
  6205. *out_max_size = table->table_type.max_size;
  6206. }
  6207. return true;
  6208. }
  6209. #endif
  6210. return false;
  6211. }
  6212. bool
  6213. wasm_runtime_get_table_inst_elem_type(
  6214. const WASMModuleInstanceCommon *module_inst_comm, uint32 table_idx,
  6215. uint8 *out_elem_type,
  6216. #if WASM_ENABLE_GC != 0
  6217. WASMRefType **out_ref_type,
  6218. #endif
  6219. uint32 *out_min_size, uint32 *out_max_size)
  6220. {
  6221. WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
  6222. bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
  6223. || module_inst_comm->module_type == Wasm_Module_AoT);
  6224. return wasm_runtime_get_table_elem_type(
  6225. (WASMModuleCommon *)module_inst->module, table_idx, out_elem_type,
  6226. #if WASM_ENABLE_GC != 0
  6227. out_ref_type,
  6228. #endif
  6229. out_min_size, out_max_size);
  6230. }
  6231. bool
  6232. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  6233. const WASMExport *export, WASMFuncType **out)
  6234. {
  6235. #if WASM_ENABLE_INTERP != 0
  6236. if (module_comm->module_type == Wasm_Module_Bytecode) {
  6237. WASMModule *module = (WASMModule *)module_comm;
  6238. if (export->index < module->import_function_count) {
  6239. *out = module->import_functions[export->index].u.function.func_type;
  6240. }
  6241. else {
  6242. *out =
  6243. module->functions[export->index - module->import_function_count]
  6244. ->func_type;
  6245. }
  6246. return true;
  6247. }
  6248. #endif
  6249. #if WASM_ENABLE_AOT != 0
  6250. if (module_comm->module_type == Wasm_Module_AoT) {
  6251. AOTModule *module = (AOTModule *)module_comm;
  6252. if (export->index < module->import_func_count) {
  6253. *out = (WASMFuncType *)
  6254. module->types[module->import_funcs[export->index]
  6255. .func_type_index];
  6256. }
  6257. else {
  6258. *out = (WASMFuncType *)module
  6259. ->types[module->func_type_indexes
  6260. [export->index - module->import_func_count]];
  6261. }
  6262. return true;
  6263. }
  6264. #endif
  6265. return false;
  6266. }
  6267. bool
  6268. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  6269. const WASMExport *export,
  6270. uint8 *out_val_type, bool *out_mutability)
  6271. {
  6272. #if WASM_ENABLE_INTERP != 0
  6273. if (module_comm->module_type == Wasm_Module_Bytecode) {
  6274. WASMModule *module = (WASMModule *)module_comm;
  6275. if (export->index < module->import_global_count) {
  6276. WASMGlobalImport *import_global =
  6277. &((module->import_globals + export->index)->u.global);
  6278. *out_val_type = import_global->type.val_type;
  6279. *out_mutability = import_global->type.is_mutable;
  6280. }
  6281. else {
  6282. WASMGlobal *global =
  6283. module->globals + (export->index - module->import_global_count);
  6284. *out_val_type = global->type.val_type;
  6285. *out_mutability = global->type.is_mutable;
  6286. }
  6287. return true;
  6288. }
  6289. #endif
  6290. #if WASM_ENABLE_AOT != 0
  6291. if (module_comm->module_type == Wasm_Module_AoT) {
  6292. AOTModule *module = (AOTModule *)module_comm;
  6293. if (export->index < module->import_global_count) {
  6294. AOTImportGlobal *import_global =
  6295. module->import_globals + export->index;
  6296. *out_val_type = import_global->type.val_type;
  6297. *out_mutability = import_global->type.is_mutable;
  6298. }
  6299. else {
  6300. AOTGlobal *global =
  6301. module->globals + (export->index - module->import_global_count);
  6302. *out_val_type = global->type.val_type;
  6303. *out_mutability = global->type.is_mutable;
  6304. }
  6305. return true;
  6306. }
  6307. #endif
  6308. return false;
  6309. }
  6310. bool
  6311. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  6312. const WASMExport *export,
  6313. uint32 *out_min_page, uint32 *out_max_page)
  6314. {
  6315. #if WASM_ENABLE_INTERP != 0
  6316. if (module_comm->module_type == Wasm_Module_Bytecode) {
  6317. WASMModule *module = (WASMModule *)module_comm;
  6318. if (export->index < module->import_memory_count) {
  6319. WASMMemoryImport *import_memory =
  6320. &((module->import_memories + export->index)->u.memory);
  6321. *out_min_page = import_memory->mem_type.init_page_count;
  6322. *out_max_page = import_memory->mem_type.max_page_count;
  6323. }
  6324. else {
  6325. WASMMemory *memory =
  6326. module->memories
  6327. + (export->index - module->import_memory_count);
  6328. *out_min_page = memory->init_page_count;
  6329. *out_max_page = memory->max_page_count;
  6330. }
  6331. return true;
  6332. }
  6333. #endif
  6334. #if WASM_ENABLE_AOT != 0
  6335. if (module_comm->module_type == Wasm_Module_AoT) {
  6336. AOTModule *module = (AOTModule *)module_comm;
  6337. if (export->index < module->import_memory_count) {
  6338. AOTImportMemory *import_memory =
  6339. module->import_memories + export->index;
  6340. *out_min_page = import_memory->mem_type.init_page_count;
  6341. *out_max_page = import_memory->mem_type.max_page_count;
  6342. }
  6343. else {
  6344. AOTMemory *memory = module->memories
  6345. + (export->index - module->import_memory_count);
  6346. *out_min_page = memory->init_page_count;
  6347. *out_max_page = memory->max_page_count;
  6348. }
  6349. return true;
  6350. }
  6351. #endif
  6352. return false;
  6353. }
  6354. bool
  6355. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  6356. const WASMExport *export,
  6357. uint8 *out_elem_type,
  6358. #if WASM_ENABLE_GC != 0
  6359. WASMRefType **out_ref_type,
  6360. #endif
  6361. uint32 *out_min_size, uint32 *out_max_size)
  6362. {
  6363. return wasm_runtime_get_table_elem_type(module_comm, export->index,
  6364. out_elem_type,
  6365. #if WASM_ENABLE_GC != 0
  6366. out_ref_type,
  6367. #endif
  6368. out_min_size, out_max_size);
  6369. }
  6370. static inline bool
  6371. argv_to_params(wasm_val_t *out_params, const uint32 *argv,
  6372. WASMFuncType *func_type)
  6373. {
  6374. wasm_val_t *param = out_params;
  6375. uint32 i = 0, *u32;
  6376. for (i = 0; i < func_type->param_count; i++, param++) {
  6377. switch (func_type->types[i]) {
  6378. case VALUE_TYPE_I32:
  6379. param->kind = WASM_I32;
  6380. param->of.i32 = *argv++;
  6381. break;
  6382. case VALUE_TYPE_I64:
  6383. param->kind = WASM_I64;
  6384. u32 = (uint32 *)&param->of.i64;
  6385. u32[0] = *argv++;
  6386. u32[1] = *argv++;
  6387. break;
  6388. case VALUE_TYPE_F32:
  6389. param->kind = WASM_F32;
  6390. param->of.f32 = *(float32 *)argv++;
  6391. break;
  6392. case VALUE_TYPE_F64:
  6393. param->kind = WASM_F64;
  6394. u32 = (uint32 *)&param->of.i64;
  6395. u32[0] = *argv++;
  6396. u32[1] = *argv++;
  6397. break;
  6398. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  6399. case VALUE_TYPE_EXTERNREF:
  6400. param->kind = WASM_EXTERNREF;
  6401. if (!wasm_externref_ref2obj(*argv,
  6402. (void **)&param->of.foreign)) {
  6403. return false;
  6404. }
  6405. argv++;
  6406. break;
  6407. #endif
  6408. default:
  6409. return false;
  6410. }
  6411. }
  6412. return true;
  6413. }
  6414. static inline bool
  6415. results_to_argv(WASMModuleInstanceCommon *module_inst, uint32 *out_argv,
  6416. const wasm_val_t *results, WASMFuncType *func_type)
  6417. {
  6418. const wasm_val_t *result = results;
  6419. uint32 *argv = out_argv, *u32, i;
  6420. uint8 *result_types = func_type->types + func_type->param_count;
  6421. for (i = 0; i < func_type->result_count; i++, result++) {
  6422. switch (result_types[i]) {
  6423. case VALUE_TYPE_I32:
  6424. case VALUE_TYPE_F32:
  6425. *(int32 *)argv++ = result->of.i32;
  6426. break;
  6427. case VALUE_TYPE_I64:
  6428. case VALUE_TYPE_F64:
  6429. u32 = (uint32 *)&result->of.i64;
  6430. *argv++ = u32[0];
  6431. *argv++ = u32[1];
  6432. break;
  6433. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  6434. case VALUE_TYPE_EXTERNREF:
  6435. if (!wasm_externref_obj2ref(module_inst,
  6436. (void *)result->of.foreign,
  6437. (uint32 *)argv)) {
  6438. return false;
  6439. }
  6440. argv++;
  6441. break;
  6442. #endif
  6443. default:
  6444. return false;
  6445. }
  6446. }
  6447. return true;
  6448. }
  6449. bool
  6450. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  6451. void *func_ptr, WASMFuncType *func_type,
  6452. uint32 argc, uint32 *argv, bool with_env,
  6453. void *wasm_c_api_env)
  6454. {
  6455. wasm_val_t params_buf[16] = { 0 }, results_buf[4] = { 0 };
  6456. wasm_val_t *params = params_buf, *results = results_buf;
  6457. wasm_trap_t *trap = NULL;
  6458. bool ret = false;
  6459. wasm_val_vec_t params_vec = { 0 }, results_vec = { 0 };
  6460. if (func_type->param_count > 16) {
  6461. if (!(params =
  6462. runtime_malloc(sizeof(wasm_val_t) * func_type->param_count,
  6463. module_inst, NULL, 0))) {
  6464. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  6465. return false;
  6466. }
  6467. }
  6468. if (!argv_to_params(params, argv, func_type)) {
  6469. wasm_runtime_set_exception(module_inst, "unsupported param type");
  6470. goto fail;
  6471. }
  6472. if (func_type->result_count > 4) {
  6473. if (!(results =
  6474. runtime_malloc(sizeof(wasm_val_t) * func_type->result_count,
  6475. module_inst, NULL, 0))) {
  6476. wasm_runtime_set_exception(module_inst, "allocate memory failed");
  6477. goto fail;
  6478. }
  6479. }
  6480. params_vec.data = params;
  6481. params_vec.num_elems = func_type->param_count;
  6482. params_vec.size = func_type->param_count;
  6483. results_vec.data = results;
  6484. results_vec.num_elems = 0;
  6485. results_vec.size = func_type->result_count;
  6486. if (!with_env) {
  6487. wasm_func_callback_t callback = (wasm_func_callback_t)func_ptr;
  6488. trap = callback(&params_vec, &results_vec);
  6489. }
  6490. else {
  6491. wasm_func_callback_with_env_t callback =
  6492. (wasm_func_callback_with_env_t)func_ptr;
  6493. trap = callback(wasm_c_api_env, &params_vec, &results_vec);
  6494. }
  6495. if (trap) {
  6496. if (trap->message->data) {
  6497. /* since trap->message->data does not end with '\0' */
  6498. char trap_message[108] = { 0 };
  6499. uint32 max_size_to_copy = (uint32)sizeof(trap_message) - 1;
  6500. uint32 size_to_copy = (trap->message->size < max_size_to_copy)
  6501. ? (uint32)trap->message->size
  6502. : max_size_to_copy;
  6503. bh_memcpy_s(trap_message, (uint32)sizeof(trap_message),
  6504. trap->message->data, size_to_copy);
  6505. wasm_runtime_set_exception(module_inst, trap_message);
  6506. }
  6507. else {
  6508. wasm_runtime_set_exception(
  6509. module_inst, "native function throw unknown exception");
  6510. }
  6511. wasm_trap_delete(trap);
  6512. goto fail;
  6513. }
  6514. if (!results_to_argv(module_inst, argv, results, func_type)) {
  6515. wasm_runtime_set_exception(module_inst, "unsupported result type");
  6516. goto fail;
  6517. }
  6518. ret = true;
  6519. fail:
  6520. if (params != params_buf)
  6521. wasm_runtime_free(params);
  6522. if (results != results_buf)
  6523. wasm_runtime_free(results);
  6524. return ret;
  6525. }
  6526. bool
  6527. wasm_runtime_quick_invoke_c_api_native(WASMModuleInstanceCommon *inst_comm,
  6528. CApiFuncImport *c_api_import,
  6529. wasm_val_t *params, uint32 param_count,
  6530. wasm_val_t *results, uint32 result_count)
  6531. {
  6532. WASMModuleInstance *module_inst = (WASMModuleInstance *)inst_comm;
  6533. void *func_ptr = c_api_import->func_ptr_linked;
  6534. bool with_env_arg = c_api_import->with_env_arg, ret = true;
  6535. wasm_val_vec_t params_vec = { 0 }, results_vec = { 0 };
  6536. wasm_trap_t *trap = NULL;
  6537. params_vec.data = params;
  6538. params_vec.num_elems = param_count;
  6539. params_vec.size = param_count;
  6540. results_vec.data = results;
  6541. results_vec.num_elems = 0;
  6542. results_vec.size = result_count;
  6543. if (!func_ptr) {
  6544. wasm_set_exception_with_id(module_inst, EXCE_CALL_UNLINKED_IMPORT_FUNC);
  6545. ret = false;
  6546. goto fail;
  6547. }
  6548. if (!with_env_arg) {
  6549. wasm_func_callback_t callback = (wasm_func_callback_t)func_ptr;
  6550. trap = callback(&params_vec, &results_vec);
  6551. }
  6552. else {
  6553. void *wasm_c_api_env = c_api_import->env_arg;
  6554. wasm_func_callback_with_env_t callback =
  6555. (wasm_func_callback_with_env_t)func_ptr;
  6556. trap = callback(wasm_c_api_env, &params_vec, &results_vec);
  6557. }
  6558. if (trap) {
  6559. if (trap->message->data) {
  6560. /* since trap->message->data does not end with '\0' */
  6561. char trap_message[108] = { 0 };
  6562. uint32 max_size_to_copy = (uint32)sizeof(trap_message) - 1;
  6563. uint32 size_to_copy = (trap->message->size < max_size_to_copy)
  6564. ? (uint32)trap->message->size
  6565. : max_size_to_copy;
  6566. bh_memcpy_s(trap_message, (uint32)sizeof(trap_message),
  6567. trap->message->data, size_to_copy);
  6568. wasm_set_exception(module_inst, trap_message);
  6569. }
  6570. else {
  6571. wasm_set_exception(module_inst,
  6572. "native function throw unknown exception");
  6573. }
  6574. wasm_trap_delete(trap);
  6575. ret = false;
  6576. }
  6577. fail:
  6578. #ifdef OS_ENABLE_HW_BOUND_CHECK
  6579. if (!ret)
  6580. wasm_runtime_access_exce_check_guard_page();
  6581. #endif
  6582. return ret;
  6583. }
  6584. void
  6585. wasm_runtime_show_app_heap_corrupted_prompt()
  6586. {
  6587. LOG_ERROR("Error: app heap is corrupted, if the wasm file "
  6588. "is compiled by wasi-sdk-12.0 or higher version, "
  6589. "please add -Wl,--export=malloc -Wl,--export=free "
  6590. "to export malloc and free functions. If it is "
  6591. "compiled by asc, please add --exportRuntime to "
  6592. "export the runtime helpers.");
  6593. }
  6594. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6595. void
  6596. wasm_runtime_destroy_custom_sections(WASMCustomSection *section_list)
  6597. {
  6598. WASMCustomSection *section = section_list, *next;
  6599. while (section) {
  6600. next = section->next;
  6601. wasm_runtime_free(section);
  6602. section = next;
  6603. }
  6604. }
  6605. #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION */
  6606. void
  6607. wasm_runtime_get_version(uint32_t *major, uint32_t *minor, uint32_t *patch)
  6608. {
  6609. *major = WAMR_VERSION_MAJOR;
  6610. *minor = WAMR_VERSION_MINOR;
  6611. *patch = WAMR_VERSION_PATCH;
  6612. }
  6613. bool
  6614. wasm_runtime_is_import_func_linked(const char *module_name,
  6615. const char *func_name)
  6616. {
  6617. return wasm_native_resolve_symbol(module_name, func_name, NULL, NULL, NULL,
  6618. NULL);
  6619. }
  6620. bool
  6621. wasm_runtime_is_import_global_linked(const char *module_name,
  6622. const char *global_name)
  6623. {
  6624. #if WASM_ENABLE_LIBC_BUILTIN != 0
  6625. WASMGlobalImport global = { 0 };
  6626. return wasm_native_lookup_libc_builtin_global(module_name, global_name,
  6627. &global);
  6628. #else
  6629. return false;
  6630. #endif
  6631. }
  6632. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  6633. WASMExport *
  6634. loader_find_export(const WASMModuleCommon *module, const char *module_name,
  6635. const char *field_name, uint8 export_kind, char *error_buf,
  6636. uint32 error_buf_size)
  6637. {
  6638. WASMExport *exports = NULL, *result = NULL, *export;
  6639. uint32 export_count = 0, i;
  6640. #if WASM_ENABLE_AOT != 0
  6641. if (module->module_type == Wasm_Module_AoT) {
  6642. AOTModule *aot_module = (AOTModule *)module;
  6643. exports = (WASMExport *)aot_module->exports;
  6644. export_count = aot_module->export_count;
  6645. }
  6646. #endif
  6647. #if WASM_ENABLE_INTERP != 0
  6648. if (module->module_type == Wasm_Module_Bytecode) {
  6649. WASMModule *wasm_module = (WASMModule *)module;
  6650. exports = wasm_module->exports;
  6651. export_count = wasm_module->export_count;
  6652. }
  6653. #endif
  6654. for (i = 0, export = exports; i < export_count; ++i, ++export) {
  6655. if (export->kind == export_kind && !strcmp(field_name, export->name)) {
  6656. result = export;
  6657. goto exit;
  6658. }
  6659. }
  6660. if (i == export_count) {
  6661. LOG_DEBUG("can not find an export %d named %s in the module %s",
  6662. export_kind, field_name, module_name);
  6663. set_error_buf(error_buf, error_buf_size,
  6664. "unknown import or incompatible import type");
  6665. }
  6666. exit:
  6667. return result;
  6668. }
  6669. #endif
  6670. #if WASM_ENABLE_MULTI_MODULE != 0
  6671. WASMModuleCommon *
  6672. wasm_runtime_search_sub_module(const WASMModuleCommon *parent_module,
  6673. const char *sub_module_name)
  6674. {
  6675. WASMRegisteredModule *node = NULL;
  6676. #if WASM_ENABLE_AOT != 0
  6677. if (parent_module->module_type == Wasm_Module_AoT) {
  6678. node = bh_list_first_elem(
  6679. ((AOTModule *)parent_module)->import_module_list);
  6680. }
  6681. #endif
  6682. #if WASM_ENABLE_INTERP != 0
  6683. if (parent_module->module_type == Wasm_Module_Bytecode) {
  6684. node = bh_list_first_elem(
  6685. ((WASMModule *)parent_module)->import_module_list);
  6686. }
  6687. #endif
  6688. while (node && strcmp(sub_module_name, node->module_name)) {
  6689. node = bh_list_elem_next(node);
  6690. }
  6691. return node ? node->module : NULL;
  6692. }
  6693. bool
  6694. wasm_runtime_register_sub_module(const WASMModuleCommon *parent_module,
  6695. const char *sub_module_name,
  6696. WASMModuleCommon *sub_module)
  6697. {
  6698. /* register sub_module into its parent sub module list */
  6699. WASMRegisteredModule *node = NULL;
  6700. bh_list_status ret = BH_LIST_ERROR;
  6701. if (wasm_runtime_search_sub_module(parent_module, sub_module_name)) {
  6702. LOG_DEBUG("%s has been registered in its parent", sub_module_name);
  6703. return true;
  6704. }
  6705. node = loader_malloc(sizeof(WASMRegisteredModule), NULL, 0);
  6706. if (!node) {
  6707. return false;
  6708. }
  6709. node->module_name = sub_module_name;
  6710. node->module = sub_module;
  6711. #if WASM_ENABLE_AOT != 0
  6712. if (parent_module->module_type == Wasm_Module_AoT) {
  6713. ret = bh_list_insert(((AOTModule *)parent_module)->import_module_list,
  6714. node);
  6715. }
  6716. #endif
  6717. #if WASM_ENABLE_INTERP != 0
  6718. if (parent_module->module_type == Wasm_Module_Bytecode) {
  6719. ret = bh_list_insert(((WASMModule *)parent_module)->import_module_list,
  6720. node);
  6721. }
  6722. #endif
  6723. bh_assert(BH_LIST_SUCCESS == ret);
  6724. (void)ret;
  6725. return true;
  6726. }
  6727. WASMModuleCommon *
  6728. wasm_runtime_load_depended_module(const WASMModuleCommon *parent_module,
  6729. const char *sub_module_name, char *error_buf,
  6730. uint32 error_buf_size)
  6731. {
  6732. WASMModuleCommon *sub_module = NULL;
  6733. bool ret = false;
  6734. uint8 *buffer = NULL;
  6735. uint32 buffer_size = 0;
  6736. LoadArgs args = { 0 };
  6737. /* check the registered module list of the parent */
  6738. sub_module = wasm_runtime_search_sub_module(parent_module, sub_module_name);
  6739. if (sub_module) {
  6740. LOG_DEBUG("%s has been loaded before", sub_module_name);
  6741. return sub_module;
  6742. }
  6743. /* check the global registered module list */
  6744. sub_module = wasm_runtime_find_module_registered(sub_module_name);
  6745. if (sub_module) {
  6746. LOG_DEBUG("%s has been loaded", sub_module_name);
  6747. goto wasm_runtime_register_sub_module;
  6748. }
  6749. LOG_VERBOSE("loading %s", sub_module_name);
  6750. if (!reader) {
  6751. set_error_buf_v(parent_module, error_buf, error_buf_size,
  6752. "no sub module reader to load %s", sub_module_name);
  6753. return NULL;
  6754. }
  6755. /* start to maintain a loading module list */
  6756. ret = wasm_runtime_is_loading_module(sub_module_name);
  6757. if (ret) {
  6758. set_error_buf_v(parent_module, error_buf, error_buf_size,
  6759. "found circular dependency on %s", sub_module_name);
  6760. return NULL;
  6761. }
  6762. ret = wasm_runtime_add_loading_module(sub_module_name, error_buf,
  6763. error_buf_size);
  6764. if (!ret) {
  6765. LOG_DEBUG("can not add %s into loading module list\n", sub_module_name);
  6766. return NULL;
  6767. }
  6768. ret = reader(parent_module->module_type, sub_module_name, &buffer,
  6769. &buffer_size);
  6770. if (!ret) {
  6771. LOG_DEBUG("read the file of %s failed", sub_module_name);
  6772. set_error_buf_v(parent_module, error_buf, error_buf_size,
  6773. "unknown import %s", sub_module_name);
  6774. goto delete_loading_module;
  6775. }
  6776. if (get_package_type(buffer, buffer_size) != parent_module->module_type) {
  6777. LOG_DEBUG("module %s type error", sub_module_name);
  6778. goto destroy_file_buffer;
  6779. }
  6780. args.name = (char *)sub_module_name;
  6781. if (get_package_type(buffer, buffer_size) == Wasm_Module_Bytecode) {
  6782. #if WASM_ENABLE_INTERP != 0
  6783. sub_module = (WASMModuleCommon *)wasm_load(
  6784. buffer, buffer_size, false, &args, error_buf, error_buf_size);
  6785. #endif
  6786. }
  6787. else if (get_package_type(buffer, buffer_size) == Wasm_Module_AoT) {
  6788. #if WASM_ENABLE_AOT != 0
  6789. sub_module = (WASMModuleCommon *)aot_load_from_aot_file(
  6790. buffer, buffer_size, &args, error_buf, error_buf_size);
  6791. #endif
  6792. }
  6793. if (!sub_module) {
  6794. LOG_DEBUG("error: can not load the sub_module %s", sub_module_name);
  6795. /* others will be destroyed in runtime_destroy() */
  6796. goto destroy_file_buffer;
  6797. }
  6798. wasm_runtime_delete_loading_module(sub_module_name);
  6799. /* register on a global list */
  6800. ret = wasm_runtime_register_module_internal(
  6801. sub_module_name, (WASMModuleCommon *)sub_module, buffer, buffer_size,
  6802. error_buf, error_buf_size);
  6803. if (!ret) {
  6804. LOG_DEBUG("error: can not register module %s globally\n",
  6805. sub_module_name);
  6806. /* others will be unloaded in runtime_destroy() */
  6807. goto unload_module;
  6808. }
  6809. /* register into its parent list */
  6810. wasm_runtime_register_sub_module:
  6811. ret = wasm_runtime_register_sub_module(parent_module, sub_module_name,
  6812. sub_module);
  6813. if (!ret) {
  6814. set_error_buf_v(parent_module, error_buf, error_buf_size,
  6815. "failed to register sub module %s", sub_module_name);
  6816. /* since it is in the global module list, no need to
  6817. * unload the module. the runtime_destroy() will do it
  6818. */
  6819. return NULL;
  6820. }
  6821. return sub_module;
  6822. unload_module:
  6823. wasm_runtime_unload(sub_module);
  6824. destroy_file_buffer:
  6825. if (destroyer) {
  6826. destroyer(buffer, buffer_size);
  6827. }
  6828. else {
  6829. LOG_WARNING("need to release the reading buffer of %s manually",
  6830. sub_module_name);
  6831. }
  6832. delete_loading_module:
  6833. wasm_runtime_delete_loading_module(sub_module_name);
  6834. return NULL;
  6835. }
  6836. bool
  6837. wasm_runtime_sub_module_instantiate(WASMModuleCommon *module,
  6838. WASMModuleInstanceCommon *module_inst,
  6839. uint32 stack_size, uint32 heap_size,
  6840. uint32 max_memory_pages, char *error_buf,
  6841. uint32 error_buf_size)
  6842. {
  6843. bh_list *sub_module_inst_list = NULL;
  6844. WASMRegisteredModule *sub_module_list_node = NULL;
  6845. #if WASM_ENABLE_AOT != 0
  6846. if (module->module_type == Wasm_Module_AoT) {
  6847. sub_module_inst_list =
  6848. ((AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e)
  6849. ->sub_module_inst_list;
  6850. sub_module_list_node =
  6851. bh_list_first_elem(((AOTModule *)module)->import_module_list);
  6852. }
  6853. #endif
  6854. #if WASM_ENABLE_INTERP != 0
  6855. if (module->module_type == Wasm_Module_Bytecode) {
  6856. sub_module_inst_list =
  6857. ((WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e)
  6858. ->sub_module_inst_list;
  6859. sub_module_list_node =
  6860. bh_list_first_elem(((WASMModule *)module)->import_module_list);
  6861. }
  6862. #endif
  6863. while (sub_module_list_node) {
  6864. WASMSubModInstNode *sub_module_inst_list_node = NULL;
  6865. WASMModuleCommon *sub_module = sub_module_list_node->module;
  6866. WASMModuleInstanceCommon *sub_module_inst = NULL;
  6867. sub_module_inst = wasm_runtime_instantiate_internal(
  6868. sub_module, NULL, NULL, stack_size, heap_size, max_memory_pages,
  6869. error_buf, error_buf_size);
  6870. if (!sub_module_inst) {
  6871. LOG_DEBUG("instantiate %s failed",
  6872. sub_module_list_node->module_name);
  6873. return false;
  6874. }
  6875. sub_module_inst_list_node = loader_malloc(sizeof(WASMSubModInstNode),
  6876. error_buf, error_buf_size);
  6877. if (!sub_module_inst_list_node) {
  6878. LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ: %zu",
  6879. sizeof(WASMSubModInstNode));
  6880. if (sub_module_inst)
  6881. wasm_runtime_deinstantiate_internal(sub_module_inst, false);
  6882. return false;
  6883. }
  6884. sub_module_inst_list_node->module_inst =
  6885. (WASMModuleInstance *)sub_module_inst;
  6886. sub_module_inst_list_node->module_name =
  6887. sub_module_list_node->module_name;
  6888. #if WASM_ENABLE_AOT != 0
  6889. if (module_inst->module_type == Wasm_Module_AoT) {
  6890. AOTModuleInstance *aot_module_inst =
  6891. (AOTModuleInstance *)module_inst;
  6892. AOTModule *aot_module = (AOTModule *)module;
  6893. AOTModuleInstanceExtra *aot_extra =
  6894. (AOTModuleInstanceExtra *)aot_module_inst->e;
  6895. uint32 i;
  6896. AOTImportFunc *import_func;
  6897. for (i = 0; i < aot_module->import_func_count; i++) {
  6898. if (aot_extra->import_func_module_insts[i])
  6899. continue;
  6900. import_func = &aot_module->import_funcs[i];
  6901. if (strcmp(sub_module_inst_list_node->module_name,
  6902. import_func->module_name)
  6903. == 0) {
  6904. aot_extra->import_func_module_insts[i] =
  6905. (WASMModuleInstanceCommon *)
  6906. sub_module_inst_list_node->module_inst;
  6907. }
  6908. }
  6909. }
  6910. #endif
  6911. bh_list_status ret =
  6912. bh_list_insert(sub_module_inst_list, sub_module_inst_list_node);
  6913. bh_assert(BH_LIST_SUCCESS == ret);
  6914. (void)ret;
  6915. sub_module_list_node = bh_list_elem_next(sub_module_list_node);
  6916. }
  6917. return true;
  6918. }
  6919. void
  6920. wasm_runtime_sub_module_deinstantiate(WASMModuleInstanceCommon *module_inst)
  6921. {
  6922. bh_list *list = NULL;
  6923. #if WASM_ENABLE_AOT != 0
  6924. if (module_inst->module_type == Wasm_Module_AoT) {
  6925. list = ((AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e)
  6926. ->sub_module_inst_list;
  6927. }
  6928. #endif
  6929. #if WASM_ENABLE_INTERP != 0
  6930. if (module_inst->module_type == Wasm_Module_Bytecode) {
  6931. list =
  6932. ((WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e)
  6933. ->sub_module_inst_list;
  6934. }
  6935. #endif
  6936. WASMSubModInstNode *node = bh_list_first_elem(list);
  6937. while (node) {
  6938. WASMSubModInstNode *next_node = bh_list_elem_next(node);
  6939. bh_list_remove(list, node);
  6940. wasm_runtime_deinstantiate_internal(
  6941. (WASMModuleInstanceCommon *)node->module_inst, false);
  6942. wasm_runtime_free(node);
  6943. node = next_node;
  6944. }
  6945. }
  6946. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  6947. #if WASM_ENABLE_MODULE_INST_CONTEXT != 0
  6948. void *
  6949. wasm_runtime_create_context_key(void (*dtor)(WASMModuleInstanceCommon *inst,
  6950. void *ctx))
  6951. {
  6952. return wasm_native_create_context_key(dtor);
  6953. }
  6954. void
  6955. wasm_runtime_destroy_context_key(void *key)
  6956. {
  6957. wasm_native_destroy_context_key(key);
  6958. }
  6959. void
  6960. wasm_runtime_set_context(WASMModuleInstanceCommon *inst, void *key, void *ctx)
  6961. {
  6962. wasm_native_set_context(inst, key, ctx);
  6963. }
  6964. void
  6965. wasm_runtime_set_context_spread(WASMModuleInstanceCommon *inst, void *key,
  6966. void *ctx)
  6967. {
  6968. wasm_native_set_context_spread(inst, key, ctx);
  6969. }
  6970. void *
  6971. wasm_runtime_get_context(WASMModuleInstanceCommon *inst, void *key)
  6972. {
  6973. return wasm_native_get_context(inst, key);
  6974. }
  6975. #endif /* WASM_ENABLE_MODULE_INST_CONTEXT != 0 */
  6976. #if WASM_ENABLE_LINUX_PERF != 0
  6977. static bool enable_linux_perf = false;
  6978. bool
  6979. wasm_runtime_get_linux_perf(void)
  6980. {
  6981. return enable_linux_perf;
  6982. }
  6983. void
  6984. wasm_runtime_set_linux_perf(bool flag)
  6985. {
  6986. enable_linux_perf = flag;
  6987. }
  6988. #endif
  6989. bool
  6990. wasm_runtime_set_module_name(wasm_module_t module, const char *name,
  6991. char *error_buf, uint32_t error_buf_size)
  6992. {
  6993. if (!module)
  6994. return false;
  6995. #if WASM_ENABLE_INTERP != 0
  6996. if (module->module_type == Wasm_Module_Bytecode)
  6997. return wasm_set_module_name((WASMModule *)module, name, error_buf,
  6998. error_buf_size);
  6999. #endif
  7000. #if WASM_ENABLE_AOT != 0
  7001. if (module->module_type == Wasm_Module_AoT)
  7002. return aot_set_module_name((AOTModule *)module, name, error_buf,
  7003. error_buf_size);
  7004. #endif
  7005. return false;
  7006. }
  7007. const char *
  7008. wasm_runtime_get_module_name(wasm_module_t module)
  7009. {
  7010. if (!module)
  7011. return "";
  7012. #if WASM_ENABLE_INTERP != 0
  7013. if (module->module_type == Wasm_Module_Bytecode)
  7014. return wasm_get_module_name((WASMModule *)module);
  7015. #endif
  7016. #if WASM_ENABLE_AOT != 0
  7017. if (module->module_type == Wasm_Module_AoT)
  7018. return aot_get_module_name((AOTModule *)module);
  7019. #endif
  7020. return "";
  7021. }
  7022. /*
  7023. * wasm_runtime_detect_native_stack_overflow
  7024. *
  7025. * - raise "native stack overflow" exception if available native stack
  7026. * at this point is less than WASM_STACK_GUARD_SIZE. in that case,
  7027. * return false.
  7028. *
  7029. * - update native_stack_top_min.
  7030. */
  7031. bool
  7032. wasm_runtime_detect_native_stack_overflow(WASMExecEnv *exec_env)
  7033. {
  7034. uint8 *boundary = exec_env->native_stack_boundary;
  7035. RECORD_STACK_USAGE(exec_env, (uint8 *)&boundary);
  7036. if (boundary == NULL) {
  7037. /* the platform doesn't support os_thread_get_stack_boundary */
  7038. return true;
  7039. }
  7040. #if defined(OS_ENABLE_HW_BOUND_CHECK) && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  7041. uint32 page_size = os_getpagesize();
  7042. uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
  7043. boundary = boundary + page_size * guard_page_count;
  7044. #endif
  7045. if ((uint8 *)&boundary < boundary) {
  7046. wasm_runtime_set_exception(wasm_runtime_get_module_inst(exec_env),
  7047. "native stack overflow");
  7048. return false;
  7049. }
  7050. return true;
  7051. }
  7052. bool
  7053. wasm_runtime_detect_native_stack_overflow_size(WASMExecEnv *exec_env,
  7054. uint32 requested_size)
  7055. {
  7056. uint8 *boundary = exec_env->native_stack_boundary;
  7057. RECORD_STACK_USAGE(exec_env, (uint8 *)&boundary);
  7058. if (boundary == NULL) {
  7059. /* the platform doesn't support os_thread_get_stack_boundary */
  7060. return true;
  7061. }
  7062. #if defined(OS_ENABLE_HW_BOUND_CHECK) && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  7063. uint32 page_size = os_getpagesize();
  7064. uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
  7065. boundary = boundary + page_size * guard_page_count;
  7066. #endif
  7067. /* adjust the boundary for the requested size */
  7068. boundary = boundary - WASM_STACK_GUARD_SIZE + requested_size;
  7069. if ((uint8 *)&boundary < boundary) {
  7070. wasm_runtime_set_exception(wasm_runtime_get_module_inst(exec_env),
  7071. "native stack overflow");
  7072. return false;
  7073. }
  7074. return true;
  7075. }
  7076. bool
  7077. wasm_runtime_is_underlying_binary_freeable(WASMModuleCommon *const module)
  7078. {
  7079. #if WASM_ENABLE_INTERP != 0
  7080. if (module->module_type == Wasm_Module_Bytecode) {
  7081. #if (WASM_ENABLE_JIT != 0 || WASM_ENABLE_FAST_JIT != 0) \
  7082. && (WASM_ENABLE_LAZY_JIT != 0)
  7083. return false;
  7084. #elif WASM_ENABLE_FAST_INTERP == 0
  7085. return false;
  7086. #else
  7087. /* Fast interpreter mode */
  7088. if (!((WASMModule *)module)->is_binary_freeable)
  7089. return false;
  7090. #if WASM_ENABLE_GC != 0 && WASM_ENABLE_STRINGREF != 0
  7091. if (((WASMModule *)module)->string_literal_ptrs)
  7092. return false;
  7093. #endif
  7094. #endif
  7095. }
  7096. #endif /* WASM_ENABLE_INTERP != 0 */
  7097. #if WASM_ENABLE_AOT != 0
  7098. if (module->module_type == Wasm_Module_AoT) {
  7099. if (!((AOTModule *)module)->is_binary_freeable)
  7100. return false;
  7101. #if WASM_ENABLE_GC != 0 && WASM_ENABLE_STRINGREF != 0
  7102. if (((AOTModule *)module)->string_literal_ptrs)
  7103. return false;
  7104. #endif
  7105. }
  7106. #endif /* WASM_ENABLE_AOT != 0 */
  7107. return true;
  7108. }
  7109. #if WASM_ENABLE_SHARED_HEAP != 0
  7110. bool
  7111. wasm_runtime_check_and_update_last_used_shared_heap(
  7112. WASMModuleInstanceCommon *module_inst, uintptr_t app_offset, size_t bytes,
  7113. uintptr_t *shared_heap_start_off_p, uintptr_t *shared_heap_end_off_p,
  7114. uint8 **shared_heap_base_addr_adj_p, bool is_memory64)
  7115. {
  7116. WASMSharedHeap *heap = wasm_runtime_get_shared_heap(module_inst), *cur;
  7117. uint64 shared_heap_start, shared_heap_end;
  7118. if (bytes == 0) {
  7119. bytes = 1;
  7120. }
  7121. /* Find the exact shared heap that app addr is in, and update last used
  7122. * shared heap info in func context */
  7123. for (cur = heap; cur; cur = cur->chain_next) {
  7124. shared_heap_start =
  7125. is_memory64 ? cur->start_off_mem64 : cur->start_off_mem32;
  7126. shared_heap_end = shared_heap_start - 1 + cur->size;
  7127. if (bytes - 1 <= shared_heap_end && app_offset >= shared_heap_start
  7128. && app_offset <= shared_heap_end - bytes + 1) {
  7129. *shared_heap_start_off_p = (uintptr_t)shared_heap_start;
  7130. *shared_heap_end_off_p = (uintptr_t)shared_heap_end;
  7131. *shared_heap_base_addr_adj_p =
  7132. cur->base_addr - (uintptr_t)shared_heap_start;
  7133. return true;
  7134. }
  7135. }
  7136. return false;
  7137. }
  7138. #endif