wasm_runtime_common.c 242 KB

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