wasm_runtime_common.c 251 KB

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