wasm_runtime_common.c 246 KB

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