wasm_runtime_common.c 248 KB

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