wasm_interp_classic.c 275 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_interp.h"
  6. #include "bh_log.h"
  7. #include "wasm_runtime.h"
  8. #include "wasm_opcode.h"
  9. #include "wasm_loader.h"
  10. #include "wasm_memory.h"
  11. #include "../common/wasm_exec_env.h"
  12. #if WASM_ENABLE_GC != 0
  13. #include "../common/gc/gc_object.h"
  14. #include "mem_alloc.h"
  15. #if WASM_ENABLE_STRINGREF != 0
  16. #include "string_object.h"
  17. #endif
  18. #endif
  19. #if WASM_ENABLE_SHARED_MEMORY != 0
  20. #include "../common/wasm_shared_memory.h"
  21. #endif
  22. #if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0
  23. #include "../libraries/thread-mgr/thread_manager.h"
  24. #include "../libraries/debug-engine/debug_engine.h"
  25. #endif
  26. #if WASM_ENABLE_FAST_JIT != 0
  27. #include "../fast-jit/jit_compiler.h"
  28. #endif
  29. typedef int32 CellType_I32;
  30. typedef int64 CellType_I64;
  31. typedef float32 CellType_F32;
  32. typedef float64 CellType_F64;
  33. #define BR_TABLE_TMP_BUF_LEN 32
  34. #if WASM_ENABLE_THREAD_MGR == 0
  35. #define get_linear_mem_size() linear_mem_size
  36. #else
  37. /**
  38. * Load memory data size in each time boundary check in
  39. * multi-threading mode since it may be changed by other
  40. * threads in memory.grow
  41. */
  42. #define get_linear_mem_size() GET_LINEAR_MEMORY_SIZE(memory)
  43. #endif
  44. #if WASM_ENABLE_MEMORY64 == 0
  45. #if (!defined(OS_ENABLE_HW_BOUND_CHECK) \
  46. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0)
  47. #define CHECK_MEMORY_OVERFLOW(bytes) \
  48. do { \
  49. uint64 offset1 = (uint64)offset + (uint64)addr; \
  50. if (disable_bounds_checks || offset1 + bytes <= get_linear_mem_size()) \
  51. /* If offset1 is in valid range, maddr must also \
  52. be in valid range, no need to check it again. */ \
  53. maddr = memory->memory_data + offset1; \
  54. else \
  55. goto out_of_bounds; \
  56. } while (0)
  57. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  58. do { \
  59. uint64 offset1 = (uint32)(start); \
  60. if (disable_bounds_checks || offset1 + bytes <= get_linear_mem_size()) \
  61. /* App heap space is not valid space for \
  62. bulk memory operation */ \
  63. maddr = memory->memory_data + offset1; \
  64. else \
  65. goto out_of_bounds; \
  66. } while (0)
  67. #else /* else of !defined(OS_ENABLE_HW_BOUND_CHECK) || \
  68. WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 */
  69. #define CHECK_MEMORY_OVERFLOW(bytes) \
  70. do { \
  71. uint64 offset1 = (uint64)offset + (uint64)addr; \
  72. maddr = memory->memory_data + offset1; \
  73. } while (0)
  74. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  75. do { \
  76. maddr = memory->memory_data + (uint32)(start); \
  77. } while (0)
  78. #endif /* end of !defined(OS_ENABLE_HW_BOUND_CHECK) || \
  79. WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 */
  80. #else /* else of WASM_ENABLE_MEMORY64 == 0 */
  81. #define CHECK_MEMORY_OVERFLOW(bytes) \
  82. do { \
  83. uint64 offset1 = (uint64)offset + (uint64)addr; \
  84. /* If memory64 is enabled, offset1, offset1 + bytes can overflow */ \
  85. if (disable_bounds_checks \
  86. || (offset1 >= offset && offset1 + bytes >= offset1 \
  87. && offset1 + bytes <= get_linear_mem_size())) \
  88. maddr = memory->memory_data + offset1; \
  89. else \
  90. goto out_of_bounds; \
  91. } while (0)
  92. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  93. do { \
  94. uint64 offset1 = (uint64)(start); \
  95. /* If memory64 is enabled, offset1 + bytes can overflow */ \
  96. if (disable_bounds_checks \
  97. || (offset1 + bytes >= offset1 \
  98. && offset1 + bytes <= get_linear_mem_size())) \
  99. /* App heap space is not valid space for \
  100. bulk memory operation */ \
  101. maddr = memory->memory_data + offset1; \
  102. else \
  103. goto out_of_bounds; \
  104. } while (0)
  105. #endif /* end of WASM_ENABLE_MEMORY64 == 0 */
  106. #define CHECK_ATOMIC_MEMORY_ACCESS() \
  107. do { \
  108. if (((uintptr_t)maddr & (((uintptr_t)1 << align) - 1)) != 0) \
  109. goto unaligned_atomic; \
  110. } while (0)
  111. #if WASM_ENABLE_DEBUG_INTERP != 0
  112. #define TRIGGER_WATCHPOINT_SIGTRAP() \
  113. do { \
  114. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TRAP); \
  115. CHECK_SUSPEND_FLAGS(); \
  116. } while (0)
  117. #define CHECK_WATCHPOINT(list, current_addr) \
  118. do { \
  119. WASMDebugWatchPoint *watchpoint = bh_list_first_elem(list); \
  120. while (watchpoint) { \
  121. WASMDebugWatchPoint *next = bh_list_elem_next(watchpoint); \
  122. if (watchpoint->addr <= current_addr \
  123. && watchpoint->addr + watchpoint->length > current_addr) { \
  124. TRIGGER_WATCHPOINT_SIGTRAP(); \
  125. } \
  126. watchpoint = next; \
  127. } \
  128. } while (0)
  129. #define CHECK_READ_WATCHPOINT(addr, offset) \
  130. CHECK_WATCHPOINT(watch_point_list_read, WASM_ADDR_OFFSET(addr + offset))
  131. #define CHECK_WRITE_WATCHPOINT(addr, offset) \
  132. CHECK_WATCHPOINT(watch_point_list_write, WASM_ADDR_OFFSET(addr + offset))
  133. #else
  134. #define CHECK_READ_WATCHPOINT(addr, offset) (void)0
  135. #define CHECK_WRITE_WATCHPOINT(addr, offset) (void)0
  136. #endif
  137. static inline uint32
  138. rotl32(uint32 n, uint32 c)
  139. {
  140. const uint32 mask = (31);
  141. c = c % 32;
  142. c &= mask;
  143. return (n << c) | (n >> ((0 - c) & mask));
  144. }
  145. static inline uint32
  146. rotr32(uint32 n, uint32 c)
  147. {
  148. const uint32 mask = (31);
  149. c = c % 32;
  150. c &= mask;
  151. return (n >> c) | (n << ((0 - c) & mask));
  152. }
  153. static inline uint64
  154. rotl64(uint64 n, uint64 c)
  155. {
  156. const uint64 mask = (63);
  157. c = c % 64;
  158. c &= mask;
  159. return (n << c) | (n >> ((0 - c) & mask));
  160. }
  161. static inline uint64
  162. rotr64(uint64 n, uint64 c)
  163. {
  164. const uint64 mask = (63);
  165. c = c % 64;
  166. c &= mask;
  167. return (n >> c) | (n << ((0 - c) & mask));
  168. }
  169. static inline float32
  170. f32_min(float32 a, float32 b)
  171. {
  172. if (isnan(a) || isnan(b))
  173. return NAN;
  174. else if (a == 0 && a == b)
  175. return signbit(a) ? a : b;
  176. else
  177. return a > b ? b : a;
  178. }
  179. static inline float32
  180. f32_max(float32 a, float32 b)
  181. {
  182. if (isnan(a) || isnan(b))
  183. return NAN;
  184. else if (a == 0 && a == b)
  185. return signbit(a) ? b : a;
  186. else
  187. return a > b ? a : b;
  188. }
  189. static inline float64
  190. f64_min(float64 a, float64 b)
  191. {
  192. if (isnan(a) || isnan(b))
  193. return NAN;
  194. else if (a == 0 && a == b)
  195. return signbit(a) ? a : b;
  196. else
  197. return a > b ? b : a;
  198. }
  199. static inline float64
  200. f64_max(float64 a, float64 b)
  201. {
  202. if (isnan(a) || isnan(b))
  203. return NAN;
  204. else if (a == 0 && a == b)
  205. return signbit(a) ? b : a;
  206. else
  207. return a > b ? a : b;
  208. }
  209. static inline uint32
  210. clz32(uint32 type)
  211. {
  212. uint32 num = 0;
  213. if (type == 0)
  214. return 32;
  215. while (!(type & 0x80000000)) {
  216. num++;
  217. type <<= 1;
  218. }
  219. return num;
  220. }
  221. static inline uint32
  222. clz64(uint64 type)
  223. {
  224. uint32 num = 0;
  225. if (type == 0)
  226. return 64;
  227. while (!(type & 0x8000000000000000LL)) {
  228. num++;
  229. type <<= 1;
  230. }
  231. return num;
  232. }
  233. static inline uint32
  234. ctz32(uint32 type)
  235. {
  236. uint32 num = 0;
  237. if (type == 0)
  238. return 32;
  239. while (!(type & 1)) {
  240. num++;
  241. type >>= 1;
  242. }
  243. return num;
  244. }
  245. static inline uint32
  246. ctz64(uint64 type)
  247. {
  248. uint32 num = 0;
  249. if (type == 0)
  250. return 64;
  251. while (!(type & 1)) {
  252. num++;
  253. type >>= 1;
  254. }
  255. return num;
  256. }
  257. static inline uint32
  258. popcount32(uint32 u)
  259. {
  260. uint32 ret = 0;
  261. while (u) {
  262. u = (u & (u - 1));
  263. ret++;
  264. }
  265. return ret;
  266. }
  267. static inline uint32
  268. popcount64(uint64 u)
  269. {
  270. uint32 ret = 0;
  271. while (u) {
  272. u = (u & (u - 1));
  273. ret++;
  274. }
  275. return ret;
  276. }
  277. static float
  278. local_copysignf(float x, float y)
  279. {
  280. union {
  281. float f;
  282. uint32 i;
  283. } ux = { x }, uy = { y };
  284. ux.i &= 0x7fffffff;
  285. ux.i |= uy.i & 0x80000000;
  286. return ux.f;
  287. }
  288. static double
  289. local_copysign(double x, double y)
  290. {
  291. union {
  292. double f;
  293. uint64 i;
  294. } ux = { x }, uy = { y };
  295. ux.i &= UINT64_MAX / 2;
  296. ux.i |= uy.i & 1ULL << 63;
  297. return ux.f;
  298. }
  299. static uint64
  300. read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
  301. {
  302. uint64 result = 0, byte;
  303. uint32 offset = *p_offset;
  304. uint32 shift = 0;
  305. while (true) {
  306. byte = buf[offset++];
  307. result |= ((byte & 0x7f) << shift);
  308. shift += 7;
  309. if ((byte & 0x80) == 0) {
  310. break;
  311. }
  312. }
  313. if (sign && (shift < maxbits) && (byte & 0x40)) {
  314. /* Sign extend */
  315. result |= (~((uint64)0)) << shift;
  316. }
  317. *p_offset = offset;
  318. return result;
  319. }
  320. #if WASM_ENABLE_GC != 0
  321. static uint8 *
  322. get_frame_ref(WASMInterpFrame *frame)
  323. {
  324. WASMFunctionInstance *cur_func = frame->function;
  325. uint32 all_cell_num;
  326. if (!cur_func) {
  327. /* it's a glue frame created in wasm_interp_call_wasm,
  328. no GC object will be traversed */
  329. return (uint8 *)frame->lp;
  330. }
  331. else if (!frame->ip) {
  332. /* it's a native method frame created in
  333. wasm_interp_call_func_native */
  334. all_cell_num =
  335. cur_func->param_cell_num > 2 ? cur_func->param_cell_num : 2;
  336. return (uint8 *)(frame->lp + all_cell_num);
  337. }
  338. else {
  339. #if WASM_ENABLE_JIT == 0
  340. /* it's a wasm bytecode function frame */
  341. return (uint8 *)frame->csp_boundary;
  342. #else
  343. return (uint8 *)(frame->lp + cur_func->param_cell_num
  344. + cur_func->local_cell_num
  345. + cur_func->u.func->max_stack_cell_num);
  346. #endif
  347. }
  348. }
  349. static void
  350. init_frame_refs(uint8 *frame_ref, uint32 cell_num, WASMFunctionInstance *func)
  351. {
  352. uint32 i, j;
  353. memset(frame_ref, 0, cell_num);
  354. for (i = 0, j = 0; i < func->param_count; i++) {
  355. if (wasm_is_type_reftype(func->param_types[i])
  356. && !wasm_is_reftype_i31ref(func->param_types[i])) {
  357. frame_ref[j++] = 1;
  358. #if UINTPTR_MAX == UINT64_MAX
  359. frame_ref[j++] = 1;
  360. #endif
  361. }
  362. else {
  363. j += wasm_value_type_cell_num(func->param_types[i]);
  364. }
  365. }
  366. for (i = 0; i < func->local_count; i++) {
  367. if (wasm_is_type_reftype(func->local_types[i])
  368. && !wasm_is_reftype_i31ref(func->local_types[i])) {
  369. frame_ref[j++] = 1;
  370. #if UINTPTR_MAX == UINT64_MAX
  371. frame_ref[j++] = 1;
  372. #endif
  373. }
  374. else {
  375. j += wasm_value_type_cell_num(func->local_types[i]);
  376. }
  377. }
  378. }
  379. uint8 *
  380. wasm_interp_get_frame_ref(WASMInterpFrame *frame)
  381. {
  382. return get_frame_ref(frame);
  383. }
  384. /* Return the corresponding ref slot of the given address of local
  385. variable or stack pointer. */
  386. #define COMPUTE_FRAME_REF(ref, lp, p) (ref + (unsigned)((uint32 *)p - lp))
  387. #define FRAME_REF(p) COMPUTE_FRAME_REF(frame_ref, frame_lp, p)
  388. #define FRAME_REF_FOR(frame, p) \
  389. COMPUTE_FRAME_REF(get_frame_ref(frame), frame->lp, p)
  390. #define CLEAR_FRAME_REF(p, n) \
  391. do { \
  392. int32 ref_i, ref_n = (int32)(n); \
  393. uint8 *ref = FRAME_REF(p); \
  394. for (ref_i = 0; ref_i < ref_n; ref_i++) \
  395. ref[ref_i] = 0; \
  396. } while (0)
  397. #else
  398. #define CLEAR_FRAME_REF(p, n) (void)0
  399. #endif /* end of WASM_ENABLE_GC != 0 */
  400. #define skip_leb(p) while (*p++ & 0x80)
  401. #define PUSH_I32(value) \
  402. do { \
  403. *(int32 *)frame_sp++ = (int32)(value); \
  404. } while (0)
  405. #define PUSH_F32(value) \
  406. do { \
  407. *(float32 *)frame_sp++ = (float32)(value); \
  408. } while (0)
  409. #define PUSH_I64(value) \
  410. do { \
  411. PUT_I64_TO_ADDR(frame_sp, value); \
  412. frame_sp += 2; \
  413. } while (0)
  414. #define PUSH_F64(value) \
  415. do { \
  416. PUT_F64_TO_ADDR(frame_sp, value); \
  417. frame_sp += 2; \
  418. } while (0)
  419. #if UINTPTR_MAX == UINT64_MAX
  420. #define PUSH_REF(value) \
  421. do { \
  422. PUT_REF_TO_ADDR(frame_sp, value); \
  423. frame_ref_tmp = FRAME_REF(frame_sp); \
  424. *frame_ref_tmp = *(frame_ref_tmp + 1) = 1; \
  425. frame_sp += 2; \
  426. } while (0)
  427. #define PUSH_I31REF(value) \
  428. do { \
  429. PUT_REF_TO_ADDR(frame_sp, value); \
  430. frame_sp += 2; \
  431. } while (0)
  432. #else
  433. #define PUSH_REF(value) \
  434. do { \
  435. PUT_REF_TO_ADDR(frame_sp, value); \
  436. frame_ref_tmp = FRAME_REF(frame_sp); \
  437. *frame_ref_tmp = 1; \
  438. frame_sp++; \
  439. } while (0)
  440. #define PUSH_I31REF(value) \
  441. do { \
  442. PUT_REF_TO_ADDR(frame_sp, value); \
  443. frame_sp++; \
  444. } while (0)
  445. #endif
  446. #if UINTPTR_MAX == UINT64_MAX
  447. #define PUSH_PTR(value) PUSH_I64(value)
  448. #else
  449. #define PUSH_PTR(value) PUSH_I32(value)
  450. #endif
  451. /* in exception handling, label_type needs to be stored to lookup exception
  452. * handlers */
  453. #if WASM_ENABLE_EXCE_HANDLING != 0
  454. #define SET_LABEL_TYPE(_label_type) frame_csp->label_type = _label_type
  455. #else
  456. #define SET_LABEL_TYPE(_label_type) (void)0
  457. #endif
  458. #if WASM_ENABLE_MEMORY64 != 0
  459. #define PUSH_MEM_OFFSET(value) \
  460. do { \
  461. if (is_memory64) { \
  462. PUT_I64_TO_ADDR(frame_sp, value); \
  463. frame_sp += 2; \
  464. } \
  465. else { \
  466. *(int32 *)frame_sp++ = (int32)(value); \
  467. } \
  468. } while (0)
  469. #else
  470. #define PUSH_MEM_OFFSET(value) PUSH_I32(value)
  471. #endif
  472. #define PUSH_PAGE_COUNT(value) PUSH_MEM_OFFSET(value)
  473. #define PUSH_CSP(_label_type, param_cell_num, cell_num, _target_addr) \
  474. do { \
  475. bh_assert(frame_csp < frame->csp_boundary); \
  476. SET_LABEL_TYPE(_label_type); \
  477. frame_csp->cell_num = cell_num; \
  478. frame_csp->begin_addr = frame_ip; \
  479. frame_csp->target_addr = _target_addr; \
  480. frame_csp->frame_sp = frame_sp - param_cell_num; \
  481. frame_csp++; \
  482. } while (0)
  483. #define POP_I32() (--frame_sp, *(int32 *)frame_sp)
  484. #define POP_F32() (--frame_sp, *(float32 *)frame_sp)
  485. #define POP_I64() (frame_sp -= 2, GET_I64_FROM_ADDR(frame_sp))
  486. #define POP_F64() (frame_sp -= 2, GET_F64_FROM_ADDR(frame_sp))
  487. #if UINTPTR_MAX == UINT64_MAX
  488. #define POP_REF() \
  489. (frame_sp -= 2, frame_ref_tmp = FRAME_REF(frame_sp), \
  490. *frame_ref_tmp = *(frame_ref_tmp + 1) = 0, GET_REF_FROM_ADDR(frame_sp))
  491. #else
  492. #define POP_REF() \
  493. (frame_sp--, frame_ref_tmp = FRAME_REF(frame_sp), *frame_ref_tmp = 0, \
  494. GET_REF_FROM_ADDR(frame_sp))
  495. #endif
  496. #if WASM_ENABLE_MEMORY64 != 0
  497. #define POP_MEM_OFFSET() (is_memory64 ? POP_I64() : POP_I32())
  498. #else
  499. #define POP_MEM_OFFSET() POP_I32()
  500. #endif
  501. #define POP_PAGE_COUNT() POP_MEM_OFFSET()
  502. #define POP_CSP_CHECK_OVERFLOW(n) \
  503. do { \
  504. bh_assert(frame_csp - n >= frame->csp_bottom); \
  505. } while (0)
  506. #define POP_CSP() \
  507. do { \
  508. POP_CSP_CHECK_OVERFLOW(1); \
  509. --frame_csp; \
  510. } while (0)
  511. #define POP_CSP_N(n) \
  512. do { \
  513. uint32 *frame_sp_old = frame_sp; \
  514. uint32 cell_num_to_copy; \
  515. POP_CSP_CHECK_OVERFLOW(n + 1); \
  516. frame_csp -= n; \
  517. frame_ip = (frame_csp - 1)->target_addr; \
  518. /* copy arity values of block */ \
  519. frame_sp = (frame_csp - 1)->frame_sp; \
  520. cell_num_to_copy = (frame_csp - 1)->cell_num; \
  521. if (cell_num_to_copy > 0) { \
  522. word_copy(frame_sp, frame_sp_old - cell_num_to_copy, \
  523. cell_num_to_copy); \
  524. frame_ref_copy(FRAME_REF(frame_sp), \
  525. FRAME_REF(frame_sp_old - cell_num_to_copy), \
  526. cell_num_to_copy); \
  527. } \
  528. frame_sp += cell_num_to_copy; \
  529. CLEAR_FRAME_REF(frame_sp, frame_sp_old - frame_sp); \
  530. } while (0)
  531. /* Pop the given number of elements from the given frame's stack. */
  532. #define POP(N) \
  533. do { \
  534. int n = (N); \
  535. frame_sp -= n; \
  536. CLEAR_FRAME_REF(frame_sp, n); \
  537. } while (0)
  538. #if WASM_ENABLE_EXCE_HANDLING != 0
  539. /* unwind the CSP to a given label and optionally modify the labeltype */
  540. #define UNWIND_CSP(N, T) \
  541. do { \
  542. /* unwind to function frame */ \
  543. frame_csp -= N; \
  544. /* drop handlers and values pushd in try block */ \
  545. frame_sp = (frame_csp - 1)->frame_sp; \
  546. (frame_csp - 1)->label_type = T ? T : (frame_csp - 1)->label_type; \
  547. } while (0)
  548. #endif
  549. #define SYNC_ALL_TO_FRAME() \
  550. do { \
  551. frame->sp = frame_sp; \
  552. frame->ip = frame_ip; \
  553. frame->csp = frame_csp; \
  554. } while (0)
  555. #define UPDATE_ALL_FROM_FRAME() \
  556. do { \
  557. frame_sp = frame->sp; \
  558. frame_ip = frame->ip; \
  559. frame_csp = frame->csp; \
  560. } while (0)
  561. #define read_leb_int64(p, p_end, res) \
  562. do { \
  563. uint8 _val = *p; \
  564. if (!(_val & 0x80)) { \
  565. res = (int64)_val; \
  566. if (_val & 0x40) \
  567. /* sign extend */ \
  568. res |= 0xFFFFFFFFFFFFFF80LL; \
  569. p++; \
  570. } \
  571. else { \
  572. uint32 _off = 0; \
  573. res = (int64)read_leb(p, &_off, 64, true); \
  574. p += _off; \
  575. } \
  576. } while (0)
  577. #define read_leb_uint32(p, p_end, res) \
  578. do { \
  579. uint8 _val = *p; \
  580. if (!(_val & 0x80)) { \
  581. res = _val; \
  582. p++; \
  583. } \
  584. else { \
  585. uint32 _off = 0; \
  586. res = (uint32)read_leb(p, &_off, 32, false); \
  587. p += _off; \
  588. } \
  589. } while (0)
  590. #define read_leb_int32(p, p_end, res) \
  591. do { \
  592. uint8 _val = *p; \
  593. if (!(_val & 0x80)) { \
  594. res = (int32)_val; \
  595. if (_val & 0x40) \
  596. /* sign extend */ \
  597. res |= 0xFFFFFF80; \
  598. p++; \
  599. } \
  600. else { \
  601. uint32 _off = 0; \
  602. res = (int32)read_leb(p, &_off, 32, true); \
  603. p += _off; \
  604. } \
  605. } while (0)
  606. #if WASM_ENABLE_MEMORY64 != 0
  607. #define read_leb_mem_offset(p, p_end, res) \
  608. do { \
  609. uint8 _val = *p; \
  610. if (!(_val & 0x80)) { \
  611. res = (mem_offset_t)_val; \
  612. p++; \
  613. } \
  614. else { \
  615. uint32 _off = 0; \
  616. res = (mem_offset_t)read_leb(p, &_off, is_memory64 ? 64 : 32, \
  617. false); \
  618. p += _off; \
  619. } \
  620. } while (0)
  621. #else
  622. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  623. #endif
  624. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  625. #define RECOVER_FRAME_IP_END() frame_ip_end = wasm_get_func_code_end(cur_func)
  626. #else
  627. #define RECOVER_FRAME_IP_END() (void)0
  628. #endif
  629. #if WASM_ENABLE_GC != 0
  630. #define RECOVER_FRAME_REF() frame_ref = (uint8 *)frame->csp_boundary
  631. #else
  632. #define RECOVER_FRAME_REF() (void)0
  633. #endif
  634. #define RECOVER_CONTEXT(new_frame) \
  635. do { \
  636. frame = (new_frame); \
  637. cur_func = frame->function; \
  638. prev_frame = frame->prev_frame; \
  639. frame_ip = frame->ip; \
  640. RECOVER_FRAME_IP_END(); \
  641. frame_lp = frame->lp; \
  642. frame_sp = frame->sp; \
  643. frame_csp = frame->csp; \
  644. RECOVER_FRAME_REF(); \
  645. } while (0)
  646. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  647. #define GET_OPCODE() opcode = *(frame_ip - 1);
  648. #else
  649. #define GET_OPCODE() (void)0
  650. #endif
  651. #define DEF_OP_I_CONST(ctype, src_op_type) \
  652. do { \
  653. ctype cval; \
  654. read_leb_##ctype(frame_ip, frame_ip_end, cval); \
  655. PUSH_##src_op_type(cval); \
  656. } while (0)
  657. #define DEF_OP_EQZ(src_op_type) \
  658. do { \
  659. int32 pop_val; \
  660. pop_val = POP_##src_op_type() == 0; \
  661. PUSH_I32(pop_val); \
  662. } while (0)
  663. #define DEF_OP_CMP(src_type, src_op_type, cond) \
  664. do { \
  665. uint32 res; \
  666. src_type val1, val2; \
  667. val2 = (src_type)POP_##src_op_type(); \
  668. val1 = (src_type)POP_##src_op_type(); \
  669. res = val1 cond val2; \
  670. PUSH_I32(res); \
  671. } while (0)
  672. #define DEF_OP_BIT_COUNT(src_type, src_op_type, operation) \
  673. do { \
  674. src_type val1, val2; \
  675. val1 = (src_type)POP_##src_op_type(); \
  676. val2 = (src_type)operation(val1); \
  677. PUSH_##src_op_type(val2); \
  678. } while (0)
  679. #define DEF_OP_NUMERIC(src_type1, src_type2, src_op_type, operation) \
  680. do { \
  681. frame_sp -= sizeof(src_type2) / sizeof(uint32); \
  682. *(src_type1 *)(frame_sp - sizeof(src_type1) / sizeof(uint32)) \
  683. operation## = *(src_type2 *)(frame_sp); \
  684. } while (0)
  685. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  686. #define DEF_OP_NUMERIC_64 DEF_OP_NUMERIC
  687. #else
  688. #define DEF_OP_NUMERIC_64(src_type1, src_type2, src_op_type, operation) \
  689. do { \
  690. src_type1 val1; \
  691. src_type2 val2; \
  692. frame_sp -= 2; \
  693. val1 = (src_type1)GET_##src_op_type##_FROM_ADDR(frame_sp - 2); \
  694. val2 = (src_type2)GET_##src_op_type##_FROM_ADDR(frame_sp); \
  695. val1 operation## = val2; \
  696. PUT_##src_op_type##_TO_ADDR(frame_sp - 2, val1); \
  697. } while (0)
  698. #endif
  699. #define DEF_OP_NUMERIC2(src_type1, src_type2, src_op_type, operation) \
  700. do { \
  701. frame_sp -= sizeof(src_type2) / sizeof(uint32); \
  702. *(src_type1 *)(frame_sp - sizeof(src_type1) / sizeof(uint32)) \
  703. operation## = (*(src_type2 *)(frame_sp) % 32); \
  704. } while (0)
  705. #define DEF_OP_NUMERIC2_64(src_type1, src_type2, src_op_type, operation) \
  706. do { \
  707. src_type1 val1; \
  708. src_type2 val2; \
  709. frame_sp -= 2; \
  710. val1 = (src_type1)GET_##src_op_type##_FROM_ADDR(frame_sp - 2); \
  711. val2 = (src_type2)GET_##src_op_type##_FROM_ADDR(frame_sp); \
  712. val1 operation## = (val2 % 64); \
  713. PUT_##src_op_type##_TO_ADDR(frame_sp - 2, val1); \
  714. } while (0)
  715. #define DEF_OP_MATH(src_type, src_op_type, method) \
  716. do { \
  717. src_type src_val; \
  718. src_val = POP_##src_op_type(); \
  719. PUSH_##src_op_type(method(src_val)); \
  720. } while (0)
  721. #define TRUNC_FUNCTION(func_name, src_type, dst_type, signed_type) \
  722. static dst_type func_name(src_type src_value, src_type src_min, \
  723. src_type src_max, dst_type dst_min, \
  724. dst_type dst_max, bool is_sign) \
  725. { \
  726. dst_type dst_value = 0; \
  727. if (!isnan(src_value)) { \
  728. if (src_value <= src_min) \
  729. dst_value = dst_min; \
  730. else if (src_value >= src_max) \
  731. dst_value = dst_max; \
  732. else { \
  733. if (is_sign) \
  734. dst_value = (dst_type)(signed_type)src_value; \
  735. else \
  736. dst_value = (dst_type)src_value; \
  737. } \
  738. } \
  739. return dst_value; \
  740. }
  741. TRUNC_FUNCTION(trunc_f32_to_i32, float32, uint32, int32)
  742. TRUNC_FUNCTION(trunc_f32_to_i64, float32, uint64, int64)
  743. TRUNC_FUNCTION(trunc_f64_to_i32, float64, uint32, int32)
  744. TRUNC_FUNCTION(trunc_f64_to_i64, float64, uint64, int64)
  745. static bool
  746. trunc_f32_to_int(WASMModuleInstance *module, uint32 *frame_sp, float32 src_min,
  747. float32 src_max, bool saturating, bool is_i32, bool is_sign)
  748. {
  749. float32 src_value = POP_F32();
  750. uint64 dst_value_i64;
  751. uint32 dst_value_i32;
  752. if (!saturating) {
  753. if (isnan(src_value)) {
  754. wasm_set_exception(module, "invalid conversion to integer");
  755. return false;
  756. }
  757. else if (src_value <= src_min || src_value >= src_max) {
  758. wasm_set_exception(module, "integer overflow");
  759. return false;
  760. }
  761. }
  762. if (is_i32) {
  763. uint32 dst_min = is_sign ? INT32_MIN : 0;
  764. uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
  765. dst_value_i32 = trunc_f32_to_i32(src_value, src_min, src_max, dst_min,
  766. dst_max, is_sign);
  767. PUSH_I32(dst_value_i32);
  768. }
  769. else {
  770. uint64 dst_min = is_sign ? INT64_MIN : 0;
  771. uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
  772. dst_value_i64 = trunc_f32_to_i64(src_value, src_min, src_max, dst_min,
  773. dst_max, is_sign);
  774. PUSH_I64(dst_value_i64);
  775. }
  776. return true;
  777. }
  778. static bool
  779. trunc_f64_to_int(WASMModuleInstance *module, uint32 *frame_sp, float64 src_min,
  780. float64 src_max, bool saturating, bool is_i32, bool is_sign)
  781. {
  782. float64 src_value = POP_F64();
  783. uint64 dst_value_i64;
  784. uint32 dst_value_i32;
  785. if (!saturating) {
  786. if (isnan(src_value)) {
  787. wasm_set_exception(module, "invalid conversion to integer");
  788. return false;
  789. }
  790. else if (src_value <= src_min || src_value >= src_max) {
  791. wasm_set_exception(module, "integer overflow");
  792. return false;
  793. }
  794. }
  795. if (is_i32) {
  796. uint32 dst_min = is_sign ? INT32_MIN : 0;
  797. uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
  798. dst_value_i32 = trunc_f64_to_i32(src_value, src_min, src_max, dst_min,
  799. dst_max, is_sign);
  800. PUSH_I32(dst_value_i32);
  801. }
  802. else {
  803. uint64 dst_min = is_sign ? INT64_MIN : 0;
  804. uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
  805. dst_value_i64 = trunc_f64_to_i64(src_value, src_min, src_max, dst_min,
  806. dst_max, is_sign);
  807. PUSH_I64(dst_value_i64);
  808. }
  809. return true;
  810. }
  811. #define DEF_OP_TRUNC_F32(min, max, is_i32, is_sign) \
  812. do { \
  813. if (!trunc_f32_to_int(module, frame_sp, min, max, false, is_i32, \
  814. is_sign)) \
  815. goto got_exception; \
  816. } while (0)
  817. #define DEF_OP_TRUNC_F64(min, max, is_i32, is_sign) \
  818. do { \
  819. if (!trunc_f64_to_int(module, frame_sp, min, max, false, is_i32, \
  820. is_sign)) \
  821. goto got_exception; \
  822. } while (0)
  823. #define DEF_OP_TRUNC_SAT_F32(min, max, is_i32, is_sign) \
  824. do { \
  825. (void)trunc_f32_to_int(module, frame_sp, min, max, true, is_i32, \
  826. is_sign); \
  827. } while (0)
  828. #define DEF_OP_TRUNC_SAT_F64(min, max, is_i32, is_sign) \
  829. do { \
  830. (void)trunc_f64_to_int(module, frame_sp, min, max, true, is_i32, \
  831. is_sign); \
  832. } while (0)
  833. #define DEF_OP_CONVERT(dst_type, dst_op_type, src_type, src_op_type) \
  834. do { \
  835. dst_type value = (dst_type)(src_type)POP_##src_op_type(); \
  836. PUSH_##dst_op_type(value); \
  837. } while (0)
  838. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  839. do { \
  840. uint32 param_count = cur_func->param_count; \
  841. read_leb_uint32(frame_ip, frame_ip_end, local_idx); \
  842. bh_assert(local_idx < param_count + cur_func->local_count); \
  843. local_offset = cur_func->local_offsets[local_idx]; \
  844. if (local_idx < param_count) \
  845. local_type = cur_func->param_types[local_idx]; \
  846. else \
  847. local_type = cur_func->local_types[local_idx - param_count]; \
  848. } while (0)
  849. #define DEF_ATOMIC_RMW_OPCODE(OP_NAME, op) \
  850. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME: \
  851. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U: \
  852. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U: \
  853. { \
  854. uint32 readv, sval; \
  855. \
  856. sval = POP_I32(); \
  857. addr = POP_MEM_OFFSET(); \
  858. \
  859. if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U) { \
  860. CHECK_MEMORY_OVERFLOW(1); \
  861. CHECK_ATOMIC_MEMORY_ACCESS(); \
  862. \
  863. shared_memory_lock(memory); \
  864. readv = (uint32)(*(uint8 *)maddr); \
  865. *(uint8 *)maddr = (uint8)(readv op sval); \
  866. shared_memory_unlock(memory); \
  867. } \
  868. else if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U) { \
  869. CHECK_MEMORY_OVERFLOW(2); \
  870. CHECK_ATOMIC_MEMORY_ACCESS(); \
  871. \
  872. shared_memory_lock(memory); \
  873. readv = (uint32)LOAD_U16(maddr); \
  874. STORE_U16(maddr, (uint16)(readv op sval)); \
  875. shared_memory_unlock(memory); \
  876. } \
  877. else { \
  878. CHECK_MEMORY_OVERFLOW(4); \
  879. CHECK_ATOMIC_MEMORY_ACCESS(); \
  880. \
  881. shared_memory_lock(memory); \
  882. readv = LOAD_I32(maddr); \
  883. STORE_U32(maddr, readv op sval); \
  884. shared_memory_unlock(memory); \
  885. } \
  886. PUSH_I32(readv); \
  887. break; \
  888. } \
  889. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME: \
  890. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U: \
  891. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U: \
  892. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U: \
  893. { \
  894. uint64 readv, sval; \
  895. \
  896. sval = (uint64)POP_I64(); \
  897. addr = POP_MEM_OFFSET(); \
  898. \
  899. if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U) { \
  900. CHECK_MEMORY_OVERFLOW(1); \
  901. CHECK_ATOMIC_MEMORY_ACCESS(); \
  902. \
  903. shared_memory_lock(memory); \
  904. readv = (uint64)(*(uint8 *)maddr); \
  905. *(uint8 *)maddr = (uint8)(readv op sval); \
  906. shared_memory_unlock(memory); \
  907. } \
  908. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U) { \
  909. CHECK_MEMORY_OVERFLOW(2); \
  910. CHECK_ATOMIC_MEMORY_ACCESS(); \
  911. \
  912. shared_memory_lock(memory); \
  913. readv = (uint64)LOAD_U16(maddr); \
  914. STORE_U16(maddr, (uint16)(readv op sval)); \
  915. shared_memory_unlock(memory); \
  916. } \
  917. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U) { \
  918. CHECK_MEMORY_OVERFLOW(4); \
  919. CHECK_ATOMIC_MEMORY_ACCESS(); \
  920. \
  921. shared_memory_lock(memory); \
  922. readv = (uint64)LOAD_U32(maddr); \
  923. STORE_U32(maddr, (uint32)(readv op sval)); \
  924. shared_memory_unlock(memory); \
  925. } \
  926. else { \
  927. uint64 op_result; \
  928. CHECK_MEMORY_OVERFLOW(8); \
  929. CHECK_ATOMIC_MEMORY_ACCESS(); \
  930. \
  931. shared_memory_lock(memory); \
  932. readv = (uint64)LOAD_I64(maddr); \
  933. op_result = readv op sval; \
  934. STORE_I64(maddr, op_result); \
  935. shared_memory_unlock(memory); \
  936. } \
  937. PUSH_I64(readv); \
  938. break; \
  939. }
  940. static inline int32
  941. sign_ext_8_32(int8 val)
  942. {
  943. if (val & 0x80)
  944. return (int32)val | (int32)0xffffff00;
  945. return val;
  946. }
  947. static inline int32
  948. sign_ext_16_32(int16 val)
  949. {
  950. if (val & 0x8000)
  951. return (int32)val | (int32)0xffff0000;
  952. return val;
  953. }
  954. static inline int64
  955. sign_ext_8_64(int8 val)
  956. {
  957. if (val & 0x80)
  958. return (int64)val | (int64)0xffffffffffffff00LL;
  959. return val;
  960. }
  961. static inline int64
  962. sign_ext_16_64(int16 val)
  963. {
  964. if (val & 0x8000)
  965. return (int64)val | (int64)0xffffffffffff0000LL;
  966. return val;
  967. }
  968. static inline int64
  969. sign_ext_32_64(int32 val)
  970. {
  971. if (val & (int32)0x80000000)
  972. return (int64)val | (int64)0xffffffff00000000LL;
  973. return val;
  974. }
  975. static inline void
  976. word_copy(uint32 *dest, uint32 *src, unsigned num)
  977. {
  978. bh_assert(dest != NULL);
  979. bh_assert(src != NULL);
  980. bh_assert(num > 0);
  981. if (dest != src) {
  982. /* No overlap buffer */
  983. bh_assert(!((src < dest) && (dest < src + num)));
  984. for (; num > 0; num--)
  985. *dest++ = *src++;
  986. }
  987. }
  988. #if WASM_ENABLE_GC != 0
  989. static inline void
  990. frame_ref_copy(uint8 *frame_ref_dest, uint8 *frame_ref_src, unsigned num)
  991. {
  992. if (frame_ref_dest != frame_ref_src)
  993. for (; num > 0; num--)
  994. *frame_ref_dest++ = *frame_ref_src++;
  995. }
  996. #else
  997. #define frame_ref_copy(frame_ref_dst, frame_ref_src, num) (void)0
  998. #endif
  999. static inline WASMInterpFrame *
  1000. ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame)
  1001. {
  1002. WASMInterpFrame *frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
  1003. if (frame) {
  1004. frame->prev_frame = prev_frame;
  1005. #if WASM_ENABLE_PERF_PROFILING != 0
  1006. frame->time_started = os_time_thread_cputime_us();
  1007. #endif
  1008. }
  1009. else {
  1010. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  1011. "wasm operand stack overflow");
  1012. }
  1013. return frame;
  1014. }
  1015. static inline void
  1016. FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame)
  1017. {
  1018. #if WASM_ENABLE_PERF_PROFILING != 0
  1019. if (frame->function) {
  1020. WASMInterpFrame *prev_frame = frame->prev_frame;
  1021. uint64 time_elapsed = os_time_thread_cputime_us() - frame->time_started;
  1022. frame->function->total_exec_time += time_elapsed;
  1023. frame->function->total_exec_cnt++;
  1024. if (prev_frame && prev_frame->function)
  1025. prev_frame->function->children_exec_time += time_elapsed;
  1026. }
  1027. #endif
  1028. wasm_exec_env_free_wasm_frame(exec_env, frame);
  1029. }
  1030. static void
  1031. wasm_interp_call_func_native(WASMModuleInstance *module_inst,
  1032. WASMExecEnv *exec_env,
  1033. WASMFunctionInstance *cur_func,
  1034. WASMInterpFrame *prev_frame)
  1035. {
  1036. WASMFunctionImport *func_import = cur_func->u.func_import;
  1037. CApiFuncImport *c_api_func_import = NULL;
  1038. unsigned local_cell_num =
  1039. cur_func->param_cell_num > 2 ? cur_func->param_cell_num : 2;
  1040. unsigned all_cell_num;
  1041. WASMInterpFrame *frame;
  1042. uint32 argv_ret[2], cur_func_index;
  1043. void *native_func_pointer = NULL;
  1044. char buf[128];
  1045. bool ret;
  1046. #if WASM_ENABLE_GC != 0
  1047. WASMFuncType *func_type;
  1048. uint8 *frame_ref;
  1049. #endif
  1050. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  1051. return;
  1052. }
  1053. all_cell_num = local_cell_num;
  1054. #if WASM_ENABLE_GC != 0
  1055. all_cell_num += (local_cell_num + 3) / 4;
  1056. #endif
  1057. if (!(frame =
  1058. ALLOC_FRAME(exec_env, wasm_interp_interp_frame_size(all_cell_num),
  1059. prev_frame)))
  1060. return;
  1061. frame->function = cur_func;
  1062. frame->ip = NULL;
  1063. frame->sp = frame->lp + local_cell_num;
  1064. #if WASM_ENABLE_GC != 0
  1065. /* native function doesn't have operand stack and label stack */
  1066. frame_ref = (uint8 *)frame->sp;
  1067. init_frame_refs(frame_ref, local_cell_num, cur_func);
  1068. #endif
  1069. wasm_exec_env_set_cur_frame(exec_env, frame);
  1070. cur_func_index = (uint32)(cur_func - module_inst->e->functions);
  1071. bh_assert(cur_func_index < module_inst->module->import_function_count);
  1072. if (!func_import->call_conv_wasm_c_api) {
  1073. native_func_pointer = module_inst->import_func_ptrs[cur_func_index];
  1074. }
  1075. else if (module_inst->c_api_func_imports) {
  1076. c_api_func_import = module_inst->c_api_func_imports + cur_func_index;
  1077. native_func_pointer = c_api_func_import->func_ptr_linked;
  1078. }
  1079. if (!native_func_pointer) {
  1080. snprintf(buf, sizeof(buf),
  1081. "failed to call unlinked import function (%s, %s)",
  1082. func_import->module_name, func_import->field_name);
  1083. wasm_set_exception(module_inst, buf);
  1084. return;
  1085. }
  1086. if (func_import->call_conv_wasm_c_api) {
  1087. ret = wasm_runtime_invoke_c_api_native(
  1088. (WASMModuleInstanceCommon *)module_inst, native_func_pointer,
  1089. func_import->func_type, cur_func->param_cell_num, frame->lp,
  1090. c_api_func_import->with_env_arg, c_api_func_import->env_arg);
  1091. if (ret) {
  1092. argv_ret[0] = frame->lp[0];
  1093. argv_ret[1] = frame->lp[1];
  1094. }
  1095. }
  1096. else if (!func_import->call_conv_raw) {
  1097. ret = wasm_runtime_invoke_native(
  1098. exec_env, native_func_pointer, func_import->func_type,
  1099. func_import->signature, func_import->attachment, frame->lp,
  1100. cur_func->param_cell_num, argv_ret);
  1101. }
  1102. else {
  1103. ret = wasm_runtime_invoke_native_raw(
  1104. exec_env, native_func_pointer, func_import->func_type,
  1105. func_import->signature, func_import->attachment, frame->lp,
  1106. cur_func->param_cell_num, argv_ret);
  1107. }
  1108. if (!ret)
  1109. return;
  1110. #if WASM_ENABLE_GC != 0
  1111. func_type = cur_func->u.func_import->func_type;
  1112. if (func_type->result_count
  1113. && wasm_is_type_reftype(func_type->types[cur_func->param_count])) {
  1114. frame_ref = (uint8 *)prev_frame->csp_boundary
  1115. + (unsigned)(uintptr_t)(prev_frame->sp - prev_frame->lp);
  1116. if (!wasm_is_reftype_i31ref(func_type->types[cur_func->param_count])) {
  1117. #if UINTPTR_MAX == UINT64_MAX
  1118. *frame_ref = *(frame_ref + 1) = 1;
  1119. #else
  1120. *frame_ref = 1;
  1121. #endif
  1122. }
  1123. }
  1124. #endif
  1125. if (cur_func->ret_cell_num == 1) {
  1126. prev_frame->sp[0] = argv_ret[0];
  1127. prev_frame->sp++;
  1128. }
  1129. else if (cur_func->ret_cell_num == 2) {
  1130. prev_frame->sp[0] = argv_ret[0];
  1131. prev_frame->sp[1] = argv_ret[1];
  1132. prev_frame->sp += 2;
  1133. }
  1134. FREE_FRAME(exec_env, frame);
  1135. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  1136. }
  1137. #if WASM_ENABLE_FAST_JIT != 0
  1138. bool
  1139. fast_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx,
  1140. WASMInterpFrame *prev_frame)
  1141. {
  1142. WASMModuleInstance *module_inst =
  1143. (WASMModuleInstance *)exec_env->module_inst;
  1144. WASMFunctionInstance *cur_func = module_inst->e->functions + func_idx;
  1145. wasm_interp_call_func_native(module_inst, exec_env, cur_func, prev_frame);
  1146. return wasm_copy_exception(module_inst, NULL) ? false : true;
  1147. }
  1148. #endif
  1149. #if WASM_ENABLE_MULTI_MODULE != 0
  1150. static void
  1151. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  1152. WASMExecEnv *exec_env,
  1153. WASMFunctionInstance *cur_func,
  1154. WASMInterpFrame *prev_frame);
  1155. static void
  1156. wasm_interp_call_func_import(WASMModuleInstance *module_inst,
  1157. WASMExecEnv *exec_env,
  1158. WASMFunctionInstance *cur_func,
  1159. WASMInterpFrame *prev_frame)
  1160. {
  1161. WASMModuleInstance *sub_module_inst = cur_func->import_module_inst;
  1162. WASMFunctionInstance *sub_func_inst = cur_func->import_func_inst;
  1163. WASMFunctionImport *func_import = cur_func->u.func_import;
  1164. uint8 *ip = prev_frame->ip;
  1165. char buf[128];
  1166. WASMExecEnv *sub_module_exec_env = NULL;
  1167. uintptr_t aux_stack_origin_boundary = 0;
  1168. uintptr_t aux_stack_origin_bottom = 0;
  1169. /*
  1170. * perform stack overflow check before calling
  1171. * wasm_interp_call_func_bytecode recursively.
  1172. */
  1173. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  1174. return;
  1175. }
  1176. if (!sub_func_inst) {
  1177. snprintf(buf, sizeof(buf),
  1178. "failed to call unlinked import function (%s, %s)",
  1179. func_import->module_name, func_import->field_name);
  1180. wasm_set_exception(module_inst, buf);
  1181. return;
  1182. }
  1183. /* Switch exec_env but keep using the same one by replacing necessary
  1184. * variables */
  1185. sub_module_exec_env = wasm_runtime_get_exec_env_singleton(
  1186. (WASMModuleInstanceCommon *)sub_module_inst);
  1187. if (!sub_module_exec_env) {
  1188. wasm_set_exception(module_inst, "create singleton exec_env failed");
  1189. return;
  1190. }
  1191. /* - module_inst */
  1192. wasm_exec_env_set_module_inst(exec_env,
  1193. (WASMModuleInstanceCommon *)sub_module_inst);
  1194. /* - aux_stack_boundary */
  1195. aux_stack_origin_boundary = exec_env->aux_stack_boundary;
  1196. exec_env->aux_stack_boundary = sub_module_exec_env->aux_stack_boundary;
  1197. /* - aux_stack_bottom */
  1198. aux_stack_origin_bottom = exec_env->aux_stack_bottom;
  1199. exec_env->aux_stack_bottom = sub_module_exec_env->aux_stack_bottom;
  1200. /* set ip NULL to make call_func_bytecode return after executing
  1201. this function */
  1202. prev_frame->ip = NULL;
  1203. /* call function of sub-module*/
  1204. wasm_interp_call_func_bytecode(sub_module_inst, exec_env, sub_func_inst,
  1205. prev_frame);
  1206. /* restore ip and other replaced */
  1207. prev_frame->ip = ip;
  1208. exec_env->aux_stack_boundary = aux_stack_origin_boundary;
  1209. exec_env->aux_stack_bottom = aux_stack_origin_bottom;
  1210. wasm_exec_env_restore_module_inst(exec_env,
  1211. (WASMModuleInstanceCommon *)module_inst);
  1212. }
  1213. #endif
  1214. #if WASM_ENABLE_THREAD_MGR != 0
  1215. #if WASM_ENABLE_DEBUG_INTERP != 0
  1216. #define CHECK_SUSPEND_FLAGS() \
  1217. do { \
  1218. os_mutex_lock(&exec_env->wait_lock); \
  1219. if (IS_WAMR_TERM_SIG(exec_env->current_status->signal_flag)) { \
  1220. os_mutex_unlock(&exec_env->wait_lock); \
  1221. return; \
  1222. } \
  1223. if (IS_WAMR_STOP_SIG(exec_env->current_status->signal_flag)) { \
  1224. SYNC_ALL_TO_FRAME(); \
  1225. wasm_cluster_thread_waiting_run(exec_env); \
  1226. } \
  1227. os_mutex_unlock(&exec_env->wait_lock); \
  1228. } while (0)
  1229. #else
  1230. #if WASM_SUSPEND_FLAGS_IS_ATOMIC != 0
  1231. /* The lock is only needed when the suspend_flags is atomic; otherwise
  1232. the lock is already taken at the time when SUSPENSION_LOCK() is called. */
  1233. #define SUSPENSION_LOCK() os_mutex_lock(&exec_env->wait_lock);
  1234. #define SUSPENSION_UNLOCK() os_mutex_unlock(&exec_env->wait_lock);
  1235. #else
  1236. #define SUSPENSION_LOCK()
  1237. #define SUSPENSION_UNLOCK()
  1238. #endif
  1239. #define CHECK_SUSPEND_FLAGS() \
  1240. do { \
  1241. WASM_SUSPEND_FLAGS_LOCK(exec_env->wait_lock); \
  1242. if (WASM_SUSPEND_FLAGS_GET(exec_env->suspend_flags) \
  1243. & WASM_SUSPEND_FLAG_TERMINATE) { \
  1244. /* terminate current thread */ \
  1245. WASM_SUSPEND_FLAGS_UNLOCK(exec_env->wait_lock); \
  1246. return; \
  1247. } \
  1248. while (WASM_SUSPEND_FLAGS_GET(exec_env->suspend_flags) \
  1249. & WASM_SUSPEND_FLAG_SUSPEND) { \
  1250. /* suspend current thread */ \
  1251. SUSPENSION_LOCK() \
  1252. os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock); \
  1253. SUSPENSION_UNLOCK() \
  1254. } \
  1255. WASM_SUSPEND_FLAGS_UNLOCK(exec_env->wait_lock); \
  1256. } while (0)
  1257. #endif /* WASM_ENABLE_DEBUG_INTERP */
  1258. #endif /* WASM_ENABLE_THREAD_MGR */
  1259. #if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0
  1260. #if BH_ATOMIC_32_IS_ATOMIC != 0
  1261. #define GET_SIGNAL_FLAG() \
  1262. do { \
  1263. signal_flag = \
  1264. BH_ATOMIC_32_LOAD(exec_env->current_status->signal_flag); \
  1265. } while (0)
  1266. #else
  1267. #define GET_SIGNAL_FLAG() \
  1268. do { \
  1269. os_mutex_lock(&exec_env->wait_lock); \
  1270. signal_flag = exec_env->current_status->signal_flag; \
  1271. os_mutex_unlock(&exec_env->wait_lock); \
  1272. } while (0)
  1273. #endif
  1274. #endif
  1275. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1276. #define HANDLE_OP(opcode) HANDLE_##opcode:
  1277. #define FETCH_OPCODE_AND_DISPATCH() goto *handle_table[*frame_ip++]
  1278. #if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0
  1279. #define HANDLE_OP_END() \
  1280. do { \
  1281. /* Record the current frame_ip, so when exception occurs, \
  1282. debugger can know the exact opcode who caused the exception */ \
  1283. frame_ip_orig = frame_ip; \
  1284. /* Atomic load the exec_env's signal_flag first, and then handle \
  1285. more with lock if it is WAMR_SIG_SINGSTEP */ \
  1286. GET_SIGNAL_FLAG(); \
  1287. if (signal_flag == WAMR_SIG_SINGSTEP) { \
  1288. os_mutex_lock(&exec_env->wait_lock); \
  1289. while (exec_env->current_status->signal_flag == WAMR_SIG_SINGSTEP \
  1290. && exec_env->current_status->step_count++ == 1) { \
  1291. exec_env->current_status->step_count = 0; \
  1292. SYNC_ALL_TO_FRAME(); \
  1293. wasm_cluster_thread_waiting_run(exec_env); \
  1294. } \
  1295. os_mutex_unlock(&exec_env->wait_lock); \
  1296. } \
  1297. goto *handle_table[*frame_ip++]; \
  1298. } while (0)
  1299. #else
  1300. #define HANDLE_OP_END() FETCH_OPCODE_AND_DISPATCH()
  1301. #endif
  1302. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  1303. #define HANDLE_OP(opcode) case opcode:
  1304. #if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0
  1305. #define HANDLE_OP_END() \
  1306. /* Record the current frame_ip, so when exception occurs, \
  1307. debugger can know the exact opcode who caused the exception */ \
  1308. frame_ip_orig = frame_ip; \
  1309. /* Atomic load the exec_env's signal_flag first, and then handle \
  1310. more with lock if it is WAMR_SIG_SINGSTEP */ \
  1311. GET_SIGNAL_FLAG(); \
  1312. if (signal_flag == WAMR_SIG_SINGSTEP) { \
  1313. os_mutex_lock(&exec_env->wait_lock); \
  1314. while (exec_env->current_status->signal_flag == WAMR_SIG_SINGSTEP \
  1315. && exec_env->current_status->step_count++ == 1) { \
  1316. exec_env->current_status->step_count = 0; \
  1317. SYNC_ALL_TO_FRAME(); \
  1318. wasm_cluster_thread_waiting_run(exec_env); \
  1319. } \
  1320. os_mutex_unlock(&exec_env->wait_lock); \
  1321. } \
  1322. continue;
  1323. #else
  1324. #define HANDLE_OP_END() continue
  1325. #endif
  1326. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  1327. static inline uint8 *
  1328. get_global_addr(uint8 *global_data, WASMGlobalInstance *global)
  1329. {
  1330. #if WASM_ENABLE_MULTI_MODULE == 0
  1331. return global_data + global->data_offset;
  1332. #else
  1333. return global->import_global_inst
  1334. ? global->import_module_inst->global_data
  1335. + global->import_global_inst->data_offset
  1336. : global_data + global->data_offset;
  1337. #endif
  1338. }
  1339. static void
  1340. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  1341. WASMExecEnv *exec_env,
  1342. WASMFunctionInstance *cur_func,
  1343. WASMInterpFrame *prev_frame)
  1344. {
  1345. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  1346. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  1347. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  1348. || WASM_ENABLE_BULK_MEMORY != 0
  1349. uint64 linear_mem_size = 0;
  1350. if (memory)
  1351. #if WASM_ENABLE_THREAD_MGR == 0
  1352. linear_mem_size = memory->memory_data_size;
  1353. #else
  1354. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
  1355. #endif
  1356. #endif
  1357. WASMFuncType **wasm_types = (WASMFuncType **)module->module->types;
  1358. WASMGlobalInstance *globals = module->e->globals, *global;
  1359. uint8 *global_data = module->global_data;
  1360. uint8 opcode_IMPDEP = WASM_OP_IMPDEP;
  1361. WASMInterpFrame *frame = NULL;
  1362. /* Points to this special opcode so as to jump to the
  1363. * call_method_from_entry. */
  1364. register uint8 *frame_ip = &opcode_IMPDEP; /* cache of frame->ip */
  1365. register uint32 *frame_lp = NULL; /* cache of frame->lp */
  1366. register uint32 *frame_sp = NULL; /* cache of frame->sp */
  1367. #if WASM_ENABLE_GC != 0
  1368. register uint8 *frame_ref = NULL; /* cache of frame->ref */
  1369. uint8 *frame_ref_tmp;
  1370. #endif
  1371. WASMBranchBlock *frame_csp = NULL;
  1372. BlockAddr *cache_items;
  1373. uint8 *frame_ip_end = frame_ip + 1;
  1374. uint8 opcode;
  1375. uint32 i, depth, cond, count, fidx, tidx, lidx, frame_size = 0;
  1376. uint32 all_cell_num = 0;
  1377. int32 val;
  1378. uint8 *else_addr, *end_addr, *maddr = NULL;
  1379. uint32 local_idx, local_offset, global_idx;
  1380. uint8 local_type, *global_addr;
  1381. uint32 cache_index, type_index, param_cell_num, cell_num;
  1382. #if WASM_ENABLE_EXCE_HANDLING != 0
  1383. int32_t exception_tag_index;
  1384. #endif
  1385. uint8 value_type;
  1386. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  1387. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  1388. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  1389. bool disable_bounds_checks = !wasm_runtime_is_bounds_checks_enabled(
  1390. (WASMModuleInstanceCommon *)module);
  1391. #else
  1392. bool disable_bounds_checks = false;
  1393. #endif
  1394. #endif
  1395. #if WASM_ENABLE_GC != 0
  1396. WASMObjectRef gc_obj;
  1397. WASMStructObjectRef struct_obj;
  1398. WASMArrayObjectRef array_obj;
  1399. WASMFuncObjectRef func_obj;
  1400. WASMI31ObjectRef i31_obj;
  1401. WASMExternrefObjectRef externref_obj;
  1402. #if WASM_ENABLE_STRINGREF != 0
  1403. WASMString str_obj = NULL;
  1404. WASMStringrefObjectRef stringref_obj;
  1405. WASMStringviewWTF8ObjectRef stringview_wtf8_obj;
  1406. WASMStringviewWTF16ObjectRef stringview_wtf16_obj;
  1407. WASMStringviewIterObjectRef stringview_iter_obj;
  1408. #endif
  1409. #endif
  1410. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  1411. bool is_return_call = false;
  1412. #endif
  1413. #if WASM_ENABLE_MEMORY64 != 0
  1414. /* TODO: multi-memories for now assuming the memory idx type is consistent
  1415. * across multi-memories */
  1416. bool is_memory64 = false;
  1417. if (memory)
  1418. is_memory64 = memory->is_memory64;
  1419. #endif
  1420. #if WASM_ENABLE_DEBUG_INTERP != 0
  1421. uint8 *frame_ip_orig = NULL;
  1422. WASMDebugInstance *debug_instance = wasm_exec_env_get_instance(exec_env);
  1423. bh_list *watch_point_list_read =
  1424. debug_instance ? &debug_instance->watch_point_list_read : NULL;
  1425. bh_list *watch_point_list_write =
  1426. debug_instance ? &debug_instance->watch_point_list_write : NULL;
  1427. #if WASM_ENABLE_THREAD_MGR != 0
  1428. uint32 signal_flag;
  1429. #endif
  1430. #endif
  1431. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1432. #define HANDLE_OPCODE(op) &&HANDLE_##op
  1433. DEFINE_GOTO_TABLE(const void *, handle_table);
  1434. #undef HANDLE_OPCODE
  1435. #endif
  1436. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  1437. while (frame_ip < frame_ip_end) {
  1438. opcode = *frame_ip++;
  1439. switch (opcode) {
  1440. #else
  1441. FETCH_OPCODE_AND_DISPATCH();
  1442. #endif
  1443. /* control instructions */
  1444. HANDLE_OP(WASM_OP_UNREACHABLE)
  1445. {
  1446. wasm_set_exception(module, "unreachable");
  1447. goto got_exception;
  1448. }
  1449. HANDLE_OP(WASM_OP_NOP) { HANDLE_OP_END(); }
  1450. #if WASM_ENABLE_EXCE_HANDLING != 0
  1451. HANDLE_OP(WASM_OP_RETHROW)
  1452. {
  1453. int32_t relative_depth;
  1454. read_leb_int32(frame_ip, frame_ip_end, relative_depth);
  1455. /* No frame found with exception handler; validation should
  1456. * catch it */
  1457. bh_assert(frame_csp >= frame->csp_bottom + relative_depth);
  1458. /* go up the frame stack */
  1459. WASMBranchBlock *tgtframe = (frame_csp - 1) - relative_depth;
  1460. bh_assert(tgtframe->label_type == LABEL_TYPE_CATCH
  1461. || tgtframe->label_type == LABEL_TYPE_CATCH_ALL);
  1462. /* tgtframe points to the frame containing a thrown
  1463. * exception */
  1464. uint32 *tgtframe_sp = tgtframe->frame_sp;
  1465. /* frame sp of tgtframe points to caught exception */
  1466. exception_tag_index = *((uint32 *)tgtframe_sp);
  1467. tgtframe_sp++;
  1468. /* get tag type */
  1469. uint8 tag_type_index =
  1470. module->module->tags[exception_tag_index]->type;
  1471. uint32 cell_num_to_copy =
  1472. wasm_types[tag_type_index]->param_cell_num;
  1473. /* move exception parameters (if there are any) onto top
  1474. * of stack */
  1475. if (cell_num_to_copy > 0) {
  1476. word_copy(frame_sp, tgtframe_sp - cell_num_to_copy,
  1477. cell_num_to_copy);
  1478. }
  1479. frame_sp += cell_num_to_copy;
  1480. goto find_a_catch_handler;
  1481. }
  1482. HANDLE_OP(WASM_OP_THROW)
  1483. {
  1484. read_leb_int32(frame_ip, frame_ip_end, exception_tag_index);
  1485. /* landing pad for the rethrow ? */
  1486. find_a_catch_handler:
  1487. {
  1488. WASMFuncType *tag_type = NULL;
  1489. uint32 cell_num_to_copy = 0;
  1490. if (IS_INVALID_TAGINDEX(exception_tag_index)) {
  1491. /*
  1492. * invalid exception index,
  1493. * generated if a submodule throws an exception
  1494. * that has not been imported here
  1495. *
  1496. * This should result in a branch to the CATCH_ALL block,
  1497. * if there is one
  1498. */
  1499. tag_type = NULL;
  1500. cell_num_to_copy = 0;
  1501. }
  1502. else {
  1503. if (module->e->tags[exception_tag_index].is_import_tag) {
  1504. tag_type = module->e->tags[exception_tag_index]
  1505. .u.tag_import->tag_type;
  1506. }
  1507. else {
  1508. tag_type = module->e->tags[exception_tag_index]
  1509. .u.tag->tag_type;
  1510. }
  1511. cell_num_to_copy = tag_type->param_cell_num;
  1512. }
  1513. /* browse through frame stack */
  1514. uint32 relative_depth = 0;
  1515. do {
  1516. POP_CSP_CHECK_OVERFLOW(relative_depth - 1);
  1517. WASMBranchBlock *tgtframe = frame_csp - relative_depth - 1;
  1518. switch (tgtframe->label_type) {
  1519. case LABEL_TYPE_BLOCK:
  1520. case LABEL_TYPE_IF:
  1521. case LABEL_TYPE_LOOP:
  1522. case LABEL_TYPE_CATCH:
  1523. case LABEL_TYPE_CATCH_ALL:
  1524. /*
  1525. * skip that blocks in search
  1526. * BLOCK, IF and LOOP do not contain handlers and
  1527. * cannot catch exceptions.
  1528. * blocks marked as CATCH or
  1529. * CATCH_ALL did already caught an exception and can
  1530. * only be a target for RETHROW, but cannot catch an
  1531. * exception again
  1532. */
  1533. break;
  1534. case LABEL_TYPE_TRY:
  1535. {
  1536. uint32 handler_number = 0;
  1537. uint8 **handlers = (uint8 **)tgtframe->frame_sp;
  1538. uint8 *handler = NULL;
  1539. while ((handler = handlers[handler_number]) != 0) {
  1540. uint8 handler_opcode = *handler;
  1541. uint8 *target_addr =
  1542. handler
  1543. + 1; /* first instruction or leb-immediate
  1544. behind the handler opcode */
  1545. switch (handler_opcode) {
  1546. case WASM_OP_CATCH:
  1547. {
  1548. int32 lookup_index = 0;
  1549. /* read the tag_index and advance
  1550. * target_addr to the first instruction
  1551. * in the block */
  1552. read_leb_int32(target_addr, 0,
  1553. lookup_index);
  1554. if (exception_tag_index
  1555. == lookup_index) {
  1556. /* set ip */
  1557. frame_ip = target_addr;
  1558. /* save frame_sp (points to
  1559. * exception values) */
  1560. uint32 *frame_sp_old = frame_sp;
  1561. UNWIND_CSP(relative_depth,
  1562. LABEL_TYPE_CATCH);
  1563. /* push exception_tag_index and
  1564. * exception values for rethrow */
  1565. PUSH_I32(exception_tag_index);
  1566. if (cell_num_to_copy > 0) {
  1567. word_copy(
  1568. frame_sp,
  1569. frame_sp_old
  1570. - cell_num_to_copy,
  1571. cell_num_to_copy);
  1572. frame_sp += cell_num_to_copy;
  1573. /* push exception values for
  1574. * catch
  1575. */
  1576. word_copy(
  1577. frame_sp,
  1578. frame_sp_old
  1579. - cell_num_to_copy,
  1580. cell_num_to_copy);
  1581. frame_sp += cell_num_to_copy;
  1582. }
  1583. /* advance to handler */
  1584. HANDLE_OP_END();
  1585. }
  1586. break;
  1587. }
  1588. case WASM_OP_DELEGATE:
  1589. {
  1590. int32 lookup_depth = 0;
  1591. /* read the depth */
  1592. read_leb_int32(target_addr, 0,
  1593. lookup_depth);
  1594. /* save frame_sp (points to exception
  1595. * values) */
  1596. uint32 *frame_sp_old = frame_sp;
  1597. UNWIND_CSP(relative_depth,
  1598. LABEL_TYPE_CATCH);
  1599. /* leave the block (the delegate is
  1600. * technically not inside the frame) */
  1601. frame_csp--;
  1602. /* unwind to delegated frame */
  1603. frame_csp -= lookup_depth;
  1604. /* push exception values for catch */
  1605. if (cell_num_to_copy > 0) {
  1606. word_copy(frame_sp,
  1607. frame_sp_old
  1608. - cell_num_to_copy,
  1609. cell_num_to_copy);
  1610. frame_sp += cell_num_to_copy;
  1611. }
  1612. /* tag_index is already stored in
  1613. * exception_tag_index */
  1614. goto find_a_catch_handler;
  1615. }
  1616. case WASM_OP_CATCH_ALL:
  1617. {
  1618. /* no immediate */
  1619. /* save frame_sp (points to exception
  1620. * values) */
  1621. uint32 *frame_sp_old = frame_sp;
  1622. /* set ip */
  1623. frame_ip = target_addr;
  1624. UNWIND_CSP(relative_depth,
  1625. LABEL_TYPE_CATCH_ALL);
  1626. /* push exception_tag_index and
  1627. * exception values for rethrow */
  1628. PUSH_I32(exception_tag_index);
  1629. if (cell_num_to_copy > 0) {
  1630. word_copy(frame_sp,
  1631. frame_sp_old
  1632. - cell_num_to_copy,
  1633. cell_num_to_copy);
  1634. frame_sp += cell_num_to_copy;
  1635. }
  1636. /* catch_all has no exception values */
  1637. /* advance to handler */
  1638. HANDLE_OP_END();
  1639. }
  1640. default:
  1641. wasm_set_exception(
  1642. module, "WASM_OP_THROW found "
  1643. "unexpected handler type");
  1644. goto got_exception;
  1645. }
  1646. handler_number++;
  1647. }
  1648. /* exception not caught in this frame */
  1649. break;
  1650. }
  1651. case LABEL_TYPE_FUNCTION:
  1652. {
  1653. /* save frame_sp (points to exception values) */
  1654. uint32 *frame_sp_old = frame_sp;
  1655. UNWIND_CSP(relative_depth, LABEL_TYPE_FUNCTION);
  1656. /* push exception values for catch
  1657. * The values are copied to the CALLER FRAME
  1658. * (prev_frame->sp) same behvior ad WASM_OP_RETURN
  1659. */
  1660. if (cell_num_to_copy > 0) {
  1661. word_copy(prev_frame->sp,
  1662. frame_sp_old - cell_num_to_copy,
  1663. cell_num_to_copy);
  1664. prev_frame->sp += cell_num_to_copy;
  1665. }
  1666. *((int32 *)(prev_frame->sp)) = exception_tag_index;
  1667. prev_frame->sp++;
  1668. /* mark frame as raised exception */
  1669. wasm_set_exception(module,
  1670. "uncaught wasm exception");
  1671. /* end of function, treat as WASM_OP_RETURN */
  1672. goto return_func;
  1673. }
  1674. default:
  1675. wasm_set_exception(
  1676. module,
  1677. "unexpected or invalid label in THROW or "
  1678. "RETHROW when searching a catch handler");
  1679. goto got_exception;
  1680. }
  1681. relative_depth++;
  1682. } while (1);
  1683. }
  1684. /* something went wrong. normally, we should always find the
  1685. * func label. if not, stop the interpreter */
  1686. wasm_set_exception(
  1687. module, "WASM_OP_THROW hit the bottom of the frame stack");
  1688. goto got_exception;
  1689. }
  1690. HANDLE_OP(EXT_OP_TRY)
  1691. {
  1692. /* read the blocktype */
  1693. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1694. param_cell_num = wasm_types[type_index]->param_cell_num;
  1695. cell_num = wasm_types[type_index]->ret_cell_num;
  1696. goto handle_op_try;
  1697. }
  1698. HANDLE_OP(WASM_OP_TRY)
  1699. {
  1700. value_type = *frame_ip++;
  1701. param_cell_num = 0;
  1702. cell_num = wasm_value_type_cell_num(value_type);
  1703. handle_op_try:
  1704. cache_index = ((uintptr_t)frame_ip)
  1705. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  1706. cache_items = exec_env->block_addr_cache[cache_index];
  1707. if (cache_items[0].start_addr == frame_ip) {
  1708. cache_items[0].start_addr = 0;
  1709. }
  1710. if (cache_items[1].start_addr == frame_ip) {
  1711. cache_items[1].start_addr = 0;
  1712. }
  1713. /* start at the first opcode following the try and its blocktype
  1714. */
  1715. uint8 *lookup_cursor = frame_ip;
  1716. uint8 handler_opcode = WASM_OP_UNREACHABLE;
  1717. /* target_addr filled in when END or DELEGATE is found */
  1718. PUSH_CSP(LABEL_TYPE_TRY, param_cell_num, cell_num, 0);
  1719. /* reset to begin of block */
  1720. lookup_cursor = frame_ip;
  1721. do {
  1722. /* lookup the next CATCH, CATCH_ALL or END for this TRY */
  1723. if (!wasm_loader_find_block_addr(
  1724. exec_env, (BlockAddr *)exec_env->block_addr_cache,
  1725. lookup_cursor, (uint8 *)-1, LABEL_TYPE_TRY,
  1726. &else_addr, &end_addr)) {
  1727. /* something went wrong */
  1728. wasm_set_exception(module, "find block address failed");
  1729. goto got_exception;
  1730. }
  1731. /* place cursor for continuation past opcode */
  1732. lookup_cursor = end_addr + 1;
  1733. /* end_addr points to CATCH, CATCH_ALL, DELEGATE or END */
  1734. handler_opcode = *end_addr;
  1735. switch (handler_opcode) {
  1736. case WASM_OP_CATCH:
  1737. skip_leb(lookup_cursor); /* skip tag_index */
  1738. PUSH_PTR(end_addr);
  1739. break;
  1740. case WASM_OP_CATCH_ALL:
  1741. PUSH_PTR(end_addr);
  1742. break;
  1743. case WASM_OP_DELEGATE:
  1744. skip_leb(lookup_cursor); /* skip depth */
  1745. PUSH_PTR(end_addr);
  1746. /* patch target_addr */
  1747. (frame_csp - 1)->target_addr = lookup_cursor;
  1748. break;
  1749. case WASM_OP_END:
  1750. PUSH_PTR(0);
  1751. /* patch target_addr */
  1752. (frame_csp - 1)->target_addr = end_addr;
  1753. break;
  1754. default:
  1755. /* something went wrong */
  1756. wasm_set_exception(module,
  1757. "find block address returned an "
  1758. "unexpected opcode");
  1759. goto got_exception;
  1760. }
  1761. /* ... search until the returned address is the END of the
  1762. * TRY block */
  1763. } while (handler_opcode != WASM_OP_END
  1764. && handler_opcode != WASM_OP_DELEGATE);
  1765. /* handler setup on stack complete */
  1766. HANDLE_OP_END();
  1767. }
  1768. HANDLE_OP(WASM_OP_CATCH)
  1769. {
  1770. /* skip the tag_index */
  1771. skip_leb(frame_ip);
  1772. /* leave the frame */
  1773. POP_CSP_N(0);
  1774. HANDLE_OP_END();
  1775. }
  1776. HANDLE_OP(WASM_OP_CATCH_ALL)
  1777. {
  1778. /* leave the frame */
  1779. POP_CSP_N(0);
  1780. HANDLE_OP_END();
  1781. }
  1782. HANDLE_OP(WASM_OP_DELEGATE)
  1783. {
  1784. /* skip the delegate depth */
  1785. skip_leb(frame_ip);
  1786. /* leave the frame like WASM_OP_END */
  1787. POP_CSP();
  1788. HANDLE_OP_END();
  1789. }
  1790. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  1791. HANDLE_OP(EXT_OP_BLOCK)
  1792. {
  1793. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1794. param_cell_num =
  1795. ((WASMFuncType *)wasm_types[type_index])->param_cell_num;
  1796. cell_num =
  1797. ((WASMFuncType *)wasm_types[type_index])->ret_cell_num;
  1798. goto handle_op_block;
  1799. }
  1800. HANDLE_OP(WASM_OP_BLOCK)
  1801. {
  1802. value_type = *frame_ip++;
  1803. param_cell_num = 0;
  1804. cell_num = wasm_value_type_cell_num(value_type);
  1805. handle_op_block:
  1806. cache_index = ((uintptr_t)frame_ip)
  1807. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  1808. cache_items = exec_env->block_addr_cache[cache_index];
  1809. if (cache_items[0].start_addr == frame_ip) {
  1810. end_addr = cache_items[0].end_addr;
  1811. }
  1812. else if (cache_items[1].start_addr == frame_ip) {
  1813. end_addr = cache_items[1].end_addr;
  1814. }
  1815. #if WASM_ENABLE_DEBUG_INTERP != 0
  1816. else if (!wasm_loader_find_block_addr(
  1817. exec_env, (BlockAddr *)exec_env->block_addr_cache,
  1818. frame_ip, (uint8 *)-1, LABEL_TYPE_BLOCK,
  1819. &else_addr, &end_addr)) {
  1820. wasm_set_exception(module, "find block address failed");
  1821. goto got_exception;
  1822. }
  1823. #endif
  1824. else {
  1825. end_addr = NULL;
  1826. }
  1827. PUSH_CSP(LABEL_TYPE_BLOCK, param_cell_num, cell_num, end_addr);
  1828. HANDLE_OP_END();
  1829. }
  1830. HANDLE_OP(EXT_OP_LOOP)
  1831. {
  1832. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1833. param_cell_num =
  1834. ((WASMFuncType *)wasm_types[type_index])->param_cell_num;
  1835. cell_num =
  1836. ((WASMFuncType *)wasm_types[type_index])->param_cell_num;
  1837. goto handle_op_loop;
  1838. }
  1839. HANDLE_OP(WASM_OP_LOOP)
  1840. {
  1841. value_type = *frame_ip++;
  1842. param_cell_num = 0;
  1843. cell_num = 0;
  1844. handle_op_loop:
  1845. PUSH_CSP(LABEL_TYPE_LOOP, param_cell_num, cell_num, frame_ip);
  1846. HANDLE_OP_END();
  1847. }
  1848. HANDLE_OP(EXT_OP_IF)
  1849. {
  1850. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1851. param_cell_num =
  1852. ((WASMFuncType *)wasm_types[type_index])->param_cell_num;
  1853. cell_num =
  1854. ((WASMFuncType *)wasm_types[type_index])->ret_cell_num;
  1855. goto handle_op_if;
  1856. }
  1857. HANDLE_OP(WASM_OP_IF)
  1858. {
  1859. value_type = *frame_ip++;
  1860. param_cell_num = 0;
  1861. cell_num = wasm_value_type_cell_num(value_type);
  1862. handle_op_if:
  1863. cache_index = ((uintptr_t)frame_ip)
  1864. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  1865. cache_items = exec_env->block_addr_cache[cache_index];
  1866. if (cache_items[0].start_addr == frame_ip) {
  1867. else_addr = cache_items[0].else_addr;
  1868. end_addr = cache_items[0].end_addr;
  1869. }
  1870. else if (cache_items[1].start_addr == frame_ip) {
  1871. else_addr = cache_items[1].else_addr;
  1872. end_addr = cache_items[1].end_addr;
  1873. }
  1874. else if (!wasm_loader_find_block_addr(
  1875. exec_env, (BlockAddr *)exec_env->block_addr_cache,
  1876. frame_ip, (uint8 *)-1, LABEL_TYPE_IF, &else_addr,
  1877. &end_addr)) {
  1878. wasm_set_exception(module, "find block address failed");
  1879. goto got_exception;
  1880. }
  1881. cond = (uint32)POP_I32();
  1882. if (cond) { /* if branch is met */
  1883. PUSH_CSP(LABEL_TYPE_IF, param_cell_num, cell_num, end_addr);
  1884. }
  1885. else { /* if branch is not met */
  1886. /* if there is no else branch, go to the end addr */
  1887. if (else_addr == NULL) {
  1888. frame_ip = end_addr + 1;
  1889. }
  1890. /* if there is an else branch, go to the else addr */
  1891. else {
  1892. PUSH_CSP(LABEL_TYPE_IF, param_cell_num, cell_num,
  1893. end_addr);
  1894. frame_ip = else_addr + 1;
  1895. }
  1896. }
  1897. HANDLE_OP_END();
  1898. }
  1899. HANDLE_OP(WASM_OP_ELSE)
  1900. {
  1901. /* comes from the if branch in WASM_OP_IF */
  1902. frame_ip = (frame_csp - 1)->target_addr;
  1903. HANDLE_OP_END();
  1904. }
  1905. HANDLE_OP(WASM_OP_END)
  1906. {
  1907. if (frame_csp > frame->csp_bottom + 1) {
  1908. POP_CSP();
  1909. }
  1910. else { /* end of function, treat as WASM_OP_RETURN */
  1911. frame_sp -= cur_func->ret_cell_num;
  1912. for (i = 0; i < cur_func->ret_cell_num; i++) {
  1913. #if WASM_ENABLE_GC != 0
  1914. if (prev_frame->ip) {
  1915. /* prev frame is not a glue frame and has
  1916. the frame ref area */
  1917. *FRAME_REF_FOR(prev_frame, prev_frame->sp) =
  1918. *FRAME_REF(frame_sp + i);
  1919. }
  1920. #endif
  1921. *prev_frame->sp++ = frame_sp[i];
  1922. }
  1923. goto return_func;
  1924. }
  1925. HANDLE_OP_END();
  1926. }
  1927. HANDLE_OP(WASM_OP_BR)
  1928. {
  1929. #if WASM_ENABLE_THREAD_MGR != 0
  1930. CHECK_SUSPEND_FLAGS();
  1931. #endif
  1932. read_leb_uint32(frame_ip, frame_ip_end, depth);
  1933. label_pop_csp_n:
  1934. POP_CSP_N(depth);
  1935. if (!frame_ip) { /* must be label pushed by WASM_OP_BLOCK */
  1936. if (!wasm_loader_find_block_addr(
  1937. exec_env, (BlockAddr *)exec_env->block_addr_cache,
  1938. (frame_csp - 1)->begin_addr, (uint8 *)-1,
  1939. LABEL_TYPE_BLOCK, &else_addr, &end_addr)) {
  1940. wasm_set_exception(module, "find block address failed");
  1941. goto got_exception;
  1942. }
  1943. frame_ip = end_addr;
  1944. }
  1945. HANDLE_OP_END();
  1946. }
  1947. HANDLE_OP(WASM_OP_BR_IF)
  1948. {
  1949. #if WASM_ENABLE_THREAD_MGR != 0
  1950. CHECK_SUSPEND_FLAGS();
  1951. #endif
  1952. read_leb_uint32(frame_ip, frame_ip_end, depth);
  1953. cond = (uint32)POP_I32();
  1954. if (cond)
  1955. goto label_pop_csp_n;
  1956. HANDLE_OP_END();
  1957. }
  1958. HANDLE_OP(WASM_OP_BR_TABLE)
  1959. {
  1960. #if WASM_ENABLE_THREAD_MGR != 0
  1961. CHECK_SUSPEND_FLAGS();
  1962. #endif
  1963. read_leb_uint32(frame_ip, frame_ip_end, count);
  1964. lidx = POP_I32();
  1965. if (lidx > count)
  1966. lidx = count;
  1967. depth = frame_ip[lidx];
  1968. goto label_pop_csp_n;
  1969. }
  1970. HANDLE_OP(EXT_OP_BR_TABLE_CACHE)
  1971. {
  1972. BrTableCache *node_cache =
  1973. bh_list_first_elem(module->module->br_table_cache_list);
  1974. BrTableCache *node_next;
  1975. #if WASM_ENABLE_THREAD_MGR != 0
  1976. CHECK_SUSPEND_FLAGS();
  1977. #endif
  1978. lidx = POP_I32();
  1979. while (node_cache) {
  1980. node_next = bh_list_elem_next(node_cache);
  1981. if (node_cache->br_table_op_addr == frame_ip - 1) {
  1982. if (lidx > node_cache->br_count)
  1983. lidx = node_cache->br_count;
  1984. depth = node_cache->br_depths[lidx];
  1985. goto label_pop_csp_n;
  1986. }
  1987. node_cache = node_next;
  1988. }
  1989. bh_assert(0);
  1990. HANDLE_OP_END();
  1991. }
  1992. HANDLE_OP(WASM_OP_RETURN)
  1993. {
  1994. frame_sp -= cur_func->ret_cell_num;
  1995. for (i = 0; i < cur_func->ret_cell_num; i++) {
  1996. #if WASM_ENABLE_GC != 0
  1997. if (prev_frame->ip) {
  1998. /* prev frame is not a glue frame and has
  1999. the frame ref area */
  2000. *FRAME_REF_FOR(prev_frame, prev_frame->sp) =
  2001. *FRAME_REF(frame_sp + i);
  2002. }
  2003. #endif
  2004. *prev_frame->sp++ = frame_sp[i];
  2005. }
  2006. goto return_func;
  2007. }
  2008. HANDLE_OP(WASM_OP_CALL)
  2009. {
  2010. #if WASM_ENABLE_THREAD_MGR != 0
  2011. CHECK_SUSPEND_FLAGS();
  2012. #endif
  2013. read_leb_uint32(frame_ip, frame_ip_end, fidx);
  2014. #if WASM_ENABLE_MULTI_MODULE != 0
  2015. if (fidx >= module->e->function_count) {
  2016. wasm_set_exception(module, "unknown function");
  2017. goto got_exception;
  2018. }
  2019. #endif
  2020. cur_func = module->e->functions + fidx;
  2021. goto call_func_from_interp;
  2022. }
  2023. #if WASM_ENABLE_TAIL_CALL != 0
  2024. HANDLE_OP(WASM_OP_RETURN_CALL)
  2025. {
  2026. #if WASM_ENABLE_THREAD_MGR != 0
  2027. CHECK_SUSPEND_FLAGS();
  2028. #endif
  2029. read_leb_uint32(frame_ip, frame_ip_end, fidx);
  2030. #if WASM_ENABLE_MULTI_MODULE != 0
  2031. if (fidx >= module->e->function_count) {
  2032. wasm_set_exception(module, "unknown function");
  2033. goto got_exception;
  2034. }
  2035. #endif
  2036. cur_func = module->e->functions + fidx;
  2037. goto call_func_from_return_call;
  2038. }
  2039. #endif /* WASM_ENABLE_TAIL_CALL */
  2040. HANDLE_OP(WASM_OP_CALL_INDIRECT)
  2041. #if WASM_ENABLE_TAIL_CALL != 0
  2042. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  2043. #endif
  2044. {
  2045. WASMFuncType *cur_type, *cur_func_type;
  2046. WASMTableInstance *tbl_inst;
  2047. uint32 tbl_idx;
  2048. #if WASM_ENABLE_TAIL_CALL != 0
  2049. opcode = *(frame_ip - 1);
  2050. #endif
  2051. #if WASM_ENABLE_THREAD_MGR != 0
  2052. CHECK_SUSPEND_FLAGS();
  2053. #endif
  2054. /**
  2055. * type check. compiler will make sure all like
  2056. * (call_indirect (type $x) (i32.const 1))
  2057. * the function type has to be defined in the module also
  2058. * no matter it is used or not
  2059. */
  2060. read_leb_uint32(frame_ip, frame_ip_end, tidx);
  2061. bh_assert(tidx < module->module->type_count);
  2062. cur_type = wasm_types[tidx];
  2063. /* clang-format off */
  2064. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2065. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2066. #else
  2067. frame_ip++;
  2068. tbl_idx = 0;
  2069. #endif
  2070. bh_assert(tbl_idx < module->table_count);
  2071. /* clang-format on */
  2072. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2073. val = POP_I32();
  2074. if ((uint32)val >= tbl_inst->cur_size) {
  2075. wasm_set_exception(module, "undefined element");
  2076. goto got_exception;
  2077. }
  2078. /* clang-format off */
  2079. #if WASM_ENABLE_GC == 0
  2080. fidx = tbl_inst->elems[val];
  2081. if (fidx == (uint32)-1) {
  2082. wasm_set_exception(module, "uninitialized element");
  2083. goto got_exception;
  2084. }
  2085. #else
  2086. func_obj = (WASMFuncObjectRef)tbl_inst->elems[val];
  2087. if (!func_obj) {
  2088. wasm_set_exception(module, "uninitialized element");
  2089. goto got_exception;
  2090. }
  2091. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  2092. #endif
  2093. /* clang-format on */
  2094. /*
  2095. * we might be using a table injected by host or
  2096. * another module. In that case, we don't validate
  2097. * the elem value while loading
  2098. */
  2099. if (fidx >= module->e->function_count) {
  2100. wasm_set_exception(module, "unknown function");
  2101. goto got_exception;
  2102. }
  2103. /* always call module own functions */
  2104. cur_func = module->e->functions + fidx;
  2105. if (cur_func->is_import_func)
  2106. cur_func_type = cur_func->u.func_import->func_type;
  2107. else
  2108. cur_func_type = cur_func->u.func->func_type;
  2109. /* clang-format off */
  2110. #if WASM_ENABLE_GC == 0
  2111. if (cur_type != cur_func_type) {
  2112. wasm_set_exception(module, "indirect call type mismatch");
  2113. goto got_exception;
  2114. }
  2115. #else
  2116. if (!wasm_func_type_is_super_of(cur_type, cur_func_type)) {
  2117. wasm_set_exception(module, "indirect call type mismatch");
  2118. goto got_exception;
  2119. }
  2120. #endif
  2121. /* clang-format on */
  2122. #if WASM_ENABLE_TAIL_CALL != 0
  2123. if (opcode == WASM_OP_RETURN_CALL_INDIRECT)
  2124. goto call_func_from_return_call;
  2125. #endif
  2126. goto call_func_from_interp;
  2127. }
  2128. /* parametric instructions */
  2129. HANDLE_OP(WASM_OP_DROP)
  2130. {
  2131. frame_sp--;
  2132. #if WASM_ENABLE_GC != 0
  2133. frame_ref_tmp = FRAME_REF(frame_sp);
  2134. *frame_ref_tmp = 0;
  2135. #endif
  2136. HANDLE_OP_END();
  2137. }
  2138. HANDLE_OP(WASM_OP_DROP_64)
  2139. {
  2140. frame_sp -= 2;
  2141. #if WASM_ENABLE_GC != 0
  2142. frame_ref_tmp = FRAME_REF(frame_sp);
  2143. *frame_ref_tmp = 0;
  2144. *(frame_ref_tmp + 1) = 0;
  2145. #endif
  2146. HANDLE_OP_END();
  2147. }
  2148. HANDLE_OP(WASM_OP_SELECT)
  2149. {
  2150. cond = (uint32)POP_I32();
  2151. frame_sp--;
  2152. if (!cond)
  2153. *(frame_sp - 1) = *frame_sp;
  2154. HANDLE_OP_END();
  2155. }
  2156. HANDLE_OP(WASM_OP_SELECT_64)
  2157. {
  2158. cond = (uint32)POP_I32();
  2159. frame_sp -= 2;
  2160. if (!cond) {
  2161. *(frame_sp - 2) = *frame_sp;
  2162. *(frame_sp - 1) = *(frame_sp + 1);
  2163. }
  2164. HANDLE_OP_END();
  2165. }
  2166. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2167. HANDLE_OP(WASM_OP_SELECT_T)
  2168. {
  2169. uint32 vec_len;
  2170. uint8 type;
  2171. read_leb_uint32(frame_ip, frame_ip_end, vec_len);
  2172. type = *frame_ip++;
  2173. cond = (uint32)POP_I32();
  2174. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  2175. #if WASM_ENABLE_GC != 0 && UINTPTR_MAX == UINT64_MAX
  2176. || wasm_is_type_reftype(type)
  2177. #endif
  2178. ) {
  2179. frame_sp -= 2;
  2180. if (!cond) {
  2181. *(frame_sp - 2) = *frame_sp;
  2182. *(frame_sp - 1) = *(frame_sp + 1);
  2183. }
  2184. }
  2185. else {
  2186. frame_sp--;
  2187. if (!cond)
  2188. *(frame_sp - 1) = *frame_sp;
  2189. }
  2190. #if WASM_ENABLE_GC != 0
  2191. frame_ref_tmp = FRAME_REF(frame_sp);
  2192. *frame_ref_tmp = 0;
  2193. #if UINTPTR_MAX == UINT64_MAX
  2194. *(frame_ref_tmp + 1) = 0;
  2195. #endif
  2196. #endif
  2197. (void)vec_len;
  2198. HANDLE_OP_END();
  2199. }
  2200. HANDLE_OP(WASM_OP_TABLE_GET)
  2201. {
  2202. uint32 tbl_idx, elem_idx;
  2203. WASMTableInstance *tbl_inst;
  2204. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2205. bh_assert(tbl_idx < module->table_count);
  2206. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2207. elem_idx = POP_I32();
  2208. if (elem_idx >= tbl_inst->cur_size) {
  2209. wasm_set_exception(module, "out of bounds table access");
  2210. goto got_exception;
  2211. }
  2212. #if WASM_ENABLE_GC == 0
  2213. PUSH_I32(tbl_inst->elems[elem_idx]);
  2214. #else
  2215. PUSH_REF(tbl_inst->elems[elem_idx]);
  2216. #endif
  2217. HANDLE_OP_END();
  2218. }
  2219. HANDLE_OP(WASM_OP_TABLE_SET)
  2220. {
  2221. WASMTableInstance *tbl_inst;
  2222. uint32 tbl_idx, elem_idx;
  2223. table_elem_type_t elem_val;
  2224. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2225. bh_assert(tbl_idx < module->table_count);
  2226. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2227. #if WASM_ENABLE_GC == 0
  2228. elem_val = POP_I32();
  2229. #else
  2230. elem_val = POP_REF();
  2231. #endif
  2232. elem_idx = POP_I32();
  2233. if (elem_idx >= tbl_inst->cur_size) {
  2234. wasm_set_exception(module, "out of bounds table access");
  2235. goto got_exception;
  2236. }
  2237. tbl_inst->elems[elem_idx] = elem_val;
  2238. HANDLE_OP_END();
  2239. }
  2240. HANDLE_OP(WASM_OP_REF_NULL)
  2241. {
  2242. uint32 ref_type;
  2243. read_leb_uint32(frame_ip, frame_ip_end, ref_type);
  2244. #if WASM_ENABLE_GC == 0
  2245. PUSH_I32(NULL_REF);
  2246. #else
  2247. PUSH_REF(NULL_REF);
  2248. #endif
  2249. (void)ref_type;
  2250. HANDLE_OP_END();
  2251. }
  2252. HANDLE_OP(WASM_OP_REF_IS_NULL)
  2253. {
  2254. #if WASM_ENABLE_GC == 0
  2255. uint32 ref_val;
  2256. ref_val = POP_I32();
  2257. #else
  2258. void *ref_val;
  2259. ref_val = POP_REF();
  2260. #endif
  2261. PUSH_I32(ref_val == NULL_REF ? 1 : 0);
  2262. HANDLE_OP_END();
  2263. }
  2264. HANDLE_OP(WASM_OP_REF_FUNC)
  2265. {
  2266. uint32 func_idx;
  2267. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  2268. #if WASM_ENABLE_GC == 0
  2269. PUSH_I32(func_idx);
  2270. #else
  2271. SYNC_ALL_TO_FRAME();
  2272. if (!(gc_obj = wasm_create_func_obj(module, func_idx, true,
  2273. NULL, 0))) {
  2274. goto got_exception;
  2275. }
  2276. PUSH_REF(gc_obj);
  2277. #endif
  2278. HANDLE_OP_END();
  2279. }
  2280. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  2281. #if WASM_ENABLE_GC != 0
  2282. HANDLE_OP(WASM_OP_CALL_REF)
  2283. {
  2284. #if WASM_ENABLE_THREAD_MGR != 0
  2285. CHECK_SUSPEND_FLAGS();
  2286. #endif
  2287. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2288. func_obj = POP_REF();
  2289. if (!func_obj) {
  2290. wasm_set_exception(module, "null function object");
  2291. goto got_exception;
  2292. }
  2293. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  2294. cur_func = module->e->functions + fidx;
  2295. goto call_func_from_interp;
  2296. }
  2297. HANDLE_OP(WASM_OP_RETURN_CALL_REF)
  2298. {
  2299. #if WASM_ENABLE_THREAD_MGR != 0
  2300. CHECK_SUSPEND_FLAGS();
  2301. #endif
  2302. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2303. func_obj = POP_REF();
  2304. if (!func_obj) {
  2305. wasm_set_exception(module, "null function object");
  2306. goto got_exception;
  2307. }
  2308. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  2309. cur_func = module->e->functions + fidx;
  2310. goto call_func_from_return_call;
  2311. }
  2312. HANDLE_OP(WASM_OP_REF_EQ)
  2313. {
  2314. WASMObjectRef gc_obj1, gc_obj2;
  2315. gc_obj2 = POP_REF();
  2316. gc_obj1 = POP_REF();
  2317. val = wasm_obj_equal(gc_obj1, gc_obj2);
  2318. PUSH_I32(val);
  2319. HANDLE_OP_END();
  2320. }
  2321. HANDLE_OP(WASM_OP_REF_AS_NON_NULL)
  2322. {
  2323. gc_obj = POP_REF();
  2324. if (gc_obj == NULL_REF) {
  2325. wasm_set_exception(module, "null reference");
  2326. goto got_exception;
  2327. }
  2328. PUSH_REF(gc_obj);
  2329. HANDLE_OP_END();
  2330. }
  2331. HANDLE_OP(WASM_OP_BR_ON_NULL)
  2332. {
  2333. #if WASM_ENABLE_THREAD_MGR != 0
  2334. CHECK_SUSPEND_FLAGS();
  2335. #endif
  2336. read_leb_uint32(frame_ip, frame_ip_end, depth);
  2337. gc_obj = GET_REF_FROM_ADDR(frame_sp - REF_CELL_NUM);
  2338. if (gc_obj == NULL_REF) {
  2339. frame_sp -= REF_CELL_NUM;
  2340. CLEAR_FRAME_REF(frame_sp, REF_CELL_NUM);
  2341. goto label_pop_csp_n;
  2342. }
  2343. HANDLE_OP_END();
  2344. }
  2345. HANDLE_OP(WASM_OP_BR_ON_NON_NULL)
  2346. {
  2347. #if WASM_ENABLE_THREAD_MGR != 0
  2348. CHECK_SUSPEND_FLAGS();
  2349. #endif
  2350. read_leb_uint32(frame_ip, frame_ip_end, depth);
  2351. gc_obj = GET_REF_FROM_ADDR(frame_sp - REF_CELL_NUM);
  2352. if (gc_obj != NULL_REF) {
  2353. goto label_pop_csp_n;
  2354. }
  2355. else {
  2356. frame_sp -= REF_CELL_NUM;
  2357. CLEAR_FRAME_REF(frame_sp, REF_CELL_NUM);
  2358. }
  2359. HANDLE_OP_END();
  2360. }
  2361. HANDLE_OP(WASM_OP_GC_PREFIX)
  2362. {
  2363. uint32 opcode1;
  2364. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  2365. /* opcode1 was checked in loader and is no larger than
  2366. UINT8_MAX */
  2367. opcode = (uint8)opcode1;
  2368. switch (opcode) {
  2369. case WASM_OP_STRUCT_NEW:
  2370. case WASM_OP_STRUCT_NEW_DEFAULT:
  2371. {
  2372. WASMModule *wasm_module = module->module;
  2373. WASMStructType *struct_type;
  2374. WASMRttType *rtt_type;
  2375. WASMValue field_value = { 0 };
  2376. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2377. struct_type =
  2378. (WASMStructType *)module->module->types[type_index];
  2379. if (!(rtt_type = wasm_rtt_type_new(
  2380. (WASMType *)struct_type, type_index,
  2381. wasm_module->rtt_types,
  2382. wasm_module->type_count,
  2383. &wasm_module->rtt_type_lock))) {
  2384. wasm_set_exception(module,
  2385. "create rtt type failed");
  2386. goto got_exception;
  2387. }
  2388. SYNC_ALL_TO_FRAME();
  2389. struct_obj = wasm_struct_obj_new(exec_env, rtt_type);
  2390. if (!struct_obj) {
  2391. wasm_set_exception(module,
  2392. "create struct object failed");
  2393. goto got_exception;
  2394. }
  2395. if (opcode == WASM_OP_STRUCT_NEW) {
  2396. WASMStructFieldType *fields = struct_type->fields;
  2397. int32 field_count = (int32)struct_type->field_count;
  2398. int32 field_idx;
  2399. uint8 field_type;
  2400. for (field_idx = field_count - 1; field_idx >= 0;
  2401. field_idx--) {
  2402. field_type = fields[field_idx].field_type;
  2403. if (wasm_is_type_reftype(field_type)) {
  2404. field_value.gc_obj = POP_REF();
  2405. }
  2406. else if (field_type == VALUE_TYPE_I32
  2407. || field_type == VALUE_TYPE_F32
  2408. || field_type == PACKED_TYPE_I8
  2409. || field_type == PACKED_TYPE_I16) {
  2410. field_value.i32 = POP_I32();
  2411. }
  2412. else {
  2413. field_value.i64 = POP_I64();
  2414. }
  2415. wasm_struct_obj_set_field(struct_obj, field_idx,
  2416. &field_value);
  2417. }
  2418. }
  2419. PUSH_REF(struct_obj);
  2420. HANDLE_OP_END();
  2421. }
  2422. case WASM_OP_STRUCT_GET:
  2423. case WASM_OP_STRUCT_GET_S:
  2424. case WASM_OP_STRUCT_GET_U:
  2425. {
  2426. WASMStructType *struct_type;
  2427. WASMValue field_value = { 0 };
  2428. uint32 field_idx;
  2429. uint8 field_type;
  2430. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2431. read_leb_uint32(frame_ip, frame_ip_end, field_idx);
  2432. struct_type =
  2433. (WASMStructType *)module->module->types[type_index];
  2434. struct_obj = POP_REF();
  2435. if (!struct_obj) {
  2436. wasm_set_exception(module, "null structure object");
  2437. goto got_exception;
  2438. }
  2439. wasm_struct_obj_get_field(
  2440. struct_obj, field_idx,
  2441. opcode == WASM_OP_STRUCT_GET_S ? true : false,
  2442. &field_value);
  2443. field_type = struct_type->fields[field_idx].field_type;
  2444. if (wasm_is_reftype_i31ref(field_type)) {
  2445. PUSH_I31REF(field_value.gc_obj);
  2446. }
  2447. else if (wasm_is_type_reftype(field_type)) {
  2448. PUSH_REF(field_value.gc_obj);
  2449. }
  2450. else if (field_type == VALUE_TYPE_I32
  2451. || field_type == VALUE_TYPE_F32
  2452. || field_type == PACKED_TYPE_I8
  2453. || field_type == PACKED_TYPE_I16) {
  2454. PUSH_I32(field_value.i32);
  2455. }
  2456. else {
  2457. PUSH_I64(field_value.i64);
  2458. }
  2459. HANDLE_OP_END();
  2460. }
  2461. case WASM_OP_STRUCT_SET:
  2462. {
  2463. WASMStructType *struct_type;
  2464. WASMValue field_value = { 0 };
  2465. uint32 field_idx;
  2466. uint8 field_type;
  2467. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2468. read_leb_uint32(frame_ip, frame_ip_end, field_idx);
  2469. struct_type =
  2470. (WASMStructType *)module->module->types[type_index];
  2471. field_type = struct_type->fields[field_idx].field_type;
  2472. if (wasm_is_type_reftype(field_type)) {
  2473. field_value.gc_obj = POP_REF();
  2474. }
  2475. else if (field_type == VALUE_TYPE_I32
  2476. || field_type == VALUE_TYPE_F32
  2477. || field_type == PACKED_TYPE_I8
  2478. || field_type == PACKED_TYPE_I16) {
  2479. field_value.i32 = POP_I32();
  2480. }
  2481. else {
  2482. field_value.i64 = POP_I64();
  2483. }
  2484. struct_obj = POP_REF();
  2485. if (!struct_obj) {
  2486. wasm_set_exception(module, "null structure object");
  2487. goto got_exception;
  2488. }
  2489. wasm_struct_obj_set_field(struct_obj, field_idx,
  2490. &field_value);
  2491. HANDLE_OP_END();
  2492. }
  2493. case WASM_OP_ARRAY_NEW:
  2494. case WASM_OP_ARRAY_NEW_DEFAULT:
  2495. case WASM_OP_ARRAY_NEW_FIXED:
  2496. {
  2497. WASMModule *wasm_module = module->module;
  2498. WASMArrayType *array_type;
  2499. WASMRttType *rtt_type;
  2500. WASMValue array_elem = { 0 };
  2501. uint32 array_len;
  2502. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2503. array_type =
  2504. (WASMArrayType *)wasm_module->types[type_index];
  2505. if (!(rtt_type = wasm_rtt_type_new(
  2506. (WASMType *)array_type, type_index,
  2507. wasm_module->rtt_types,
  2508. wasm_module->type_count,
  2509. &wasm_module->rtt_type_lock))) {
  2510. wasm_set_exception(module,
  2511. "create rtt type failed");
  2512. goto got_exception;
  2513. }
  2514. if (opcode != WASM_OP_ARRAY_NEW_FIXED)
  2515. array_len = POP_I32();
  2516. else
  2517. read_leb_uint32(frame_ip, frame_ip_end, array_len);
  2518. if (opcode == WASM_OP_ARRAY_NEW) {
  2519. if (wasm_is_type_reftype(array_type->elem_type)) {
  2520. array_elem.gc_obj = POP_REF();
  2521. }
  2522. else if (array_type->elem_type == VALUE_TYPE_I32
  2523. || array_type->elem_type == VALUE_TYPE_F32
  2524. || array_type->elem_type == PACKED_TYPE_I8
  2525. || array_type->elem_type
  2526. == PACKED_TYPE_I16) {
  2527. array_elem.i32 = POP_I32();
  2528. }
  2529. else {
  2530. array_elem.i64 = POP_I64();
  2531. }
  2532. }
  2533. SYNC_ALL_TO_FRAME();
  2534. array_obj = wasm_array_obj_new(exec_env, rtt_type,
  2535. array_len, &array_elem);
  2536. if (!array_obj) {
  2537. wasm_set_exception(module,
  2538. "create array object failed");
  2539. goto got_exception;
  2540. }
  2541. if (opcode == WASM_OP_ARRAY_NEW_FIXED) {
  2542. for (i = 0; i < array_len; i++) {
  2543. if (wasm_is_type_reftype(
  2544. array_type->elem_type)) {
  2545. array_elem.gc_obj = POP_REF();
  2546. }
  2547. else if (array_type->elem_type == VALUE_TYPE_I32
  2548. || array_type->elem_type
  2549. == VALUE_TYPE_F32
  2550. || array_type->elem_type
  2551. == PACKED_TYPE_I8
  2552. || array_type->elem_type
  2553. == PACKED_TYPE_I16) {
  2554. array_elem.i32 = POP_I32();
  2555. }
  2556. else {
  2557. array_elem.i64 = POP_I64();
  2558. }
  2559. wasm_array_obj_set_elem(
  2560. array_obj, array_len - 1 - i, &array_elem);
  2561. }
  2562. }
  2563. PUSH_REF(array_obj);
  2564. HANDLE_OP_END();
  2565. }
  2566. case WASM_OP_ARRAY_NEW_DATA:
  2567. {
  2568. WASMModule *wasm_module = module->module;
  2569. WASMArrayType *array_type;
  2570. WASMRttType *rtt_type;
  2571. WASMValue array_elem = { 0 };
  2572. WASMDataSeg *data_seg;
  2573. uint8 *array_elem_base;
  2574. uint32 array_len, data_seg_idx, data_seg_offset;
  2575. uint32 elem_size = 0;
  2576. uint64 total_size;
  2577. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2578. read_leb_uint32(frame_ip, frame_ip_end, data_seg_idx);
  2579. data_seg = wasm_module->data_segments[data_seg_idx];
  2580. array_type =
  2581. (WASMArrayType *)wasm_module->types[type_index];
  2582. if (!(rtt_type = wasm_rtt_type_new(
  2583. (WASMType *)array_type, type_index,
  2584. wasm_module->rtt_types,
  2585. wasm_module->type_count,
  2586. &wasm_module->rtt_type_lock))) {
  2587. wasm_set_exception(module,
  2588. "create rtt type failed");
  2589. goto got_exception;
  2590. }
  2591. array_len = POP_I32();
  2592. data_seg_offset = POP_I32();
  2593. switch (array_type->elem_type) {
  2594. case PACKED_TYPE_I8:
  2595. elem_size = 1;
  2596. break;
  2597. case PACKED_TYPE_I16:
  2598. elem_size = 2;
  2599. break;
  2600. case VALUE_TYPE_I32:
  2601. case VALUE_TYPE_F32:
  2602. elem_size = 4;
  2603. break;
  2604. case VALUE_TYPE_I64:
  2605. case VALUE_TYPE_F64:
  2606. elem_size = 8;
  2607. break;
  2608. default:
  2609. bh_assert(0);
  2610. }
  2611. total_size = (uint64)elem_size * array_len;
  2612. if (data_seg_offset >= data_seg->data_length
  2613. || total_size
  2614. > data_seg->data_length - data_seg_offset) {
  2615. wasm_set_exception(module,
  2616. "data segment out of bounds");
  2617. goto got_exception;
  2618. }
  2619. SYNC_ALL_TO_FRAME();
  2620. array_obj = wasm_array_obj_new(exec_env, rtt_type,
  2621. array_len, &array_elem);
  2622. if (!array_obj) {
  2623. wasm_set_exception(module,
  2624. "create array object failed");
  2625. goto got_exception;
  2626. }
  2627. array_elem_base =
  2628. (uint8 *)wasm_array_obj_first_elem_addr(array_obj);
  2629. bh_memcpy_s(array_elem_base, (uint32)total_size,
  2630. data_seg->data + data_seg_offset,
  2631. (uint32)total_size);
  2632. PUSH_REF(array_obj);
  2633. HANDLE_OP_END();
  2634. }
  2635. case WASM_OP_ARRAY_NEW_ELEM:
  2636. {
  2637. /* TODO */
  2638. wasm_set_exception(module, "unsupported opcode");
  2639. goto got_exception;
  2640. }
  2641. case WASM_OP_ARRAY_GET:
  2642. case WASM_OP_ARRAY_GET_S:
  2643. case WASM_OP_ARRAY_GET_U:
  2644. {
  2645. WASMArrayType *array_type;
  2646. WASMValue array_elem = { 0 };
  2647. uint32 elem_idx, elem_size_log;
  2648. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2649. array_type =
  2650. (WASMArrayType *)module->module->types[type_index];
  2651. elem_idx = POP_I32();
  2652. array_obj = POP_REF();
  2653. if (!array_obj) {
  2654. wasm_set_exception(module, "null array reference");
  2655. goto got_exception;
  2656. }
  2657. if (elem_idx >= wasm_array_obj_length(array_obj)) {
  2658. wasm_set_exception(module,
  2659. "out of bounds array access");
  2660. goto got_exception;
  2661. }
  2662. wasm_array_obj_get_elem(
  2663. array_obj, elem_idx,
  2664. opcode == WASM_OP_ARRAY_GET_S ? true : false,
  2665. &array_elem);
  2666. elem_size_log = wasm_array_obj_elem_size_log(array_obj);
  2667. if (wasm_is_reftype_i31ref(array_type->elem_type)) {
  2668. PUSH_I31REF(array_elem.gc_obj);
  2669. }
  2670. else if (wasm_is_type_reftype(array_type->elem_type)) {
  2671. PUSH_REF(array_elem.gc_obj);
  2672. }
  2673. else if (elem_size_log < 3) {
  2674. PUSH_I32(array_elem.i32);
  2675. }
  2676. else {
  2677. PUSH_I64(array_elem.i64);
  2678. }
  2679. HANDLE_OP_END();
  2680. }
  2681. case WASM_OP_ARRAY_SET:
  2682. {
  2683. WASMArrayType *array_type;
  2684. WASMValue array_elem = { 0 };
  2685. uint32 elem_idx;
  2686. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2687. array_type =
  2688. (WASMArrayType *)module->module->types[type_index];
  2689. if (wasm_is_type_reftype(array_type->elem_type)) {
  2690. array_elem.gc_obj = POP_REF();
  2691. }
  2692. else if (array_type->elem_type == VALUE_TYPE_I32
  2693. || array_type->elem_type == VALUE_TYPE_F32
  2694. || array_type->elem_type == PACKED_TYPE_I8
  2695. || array_type->elem_type == PACKED_TYPE_I16) {
  2696. array_elem.i32 = POP_I32();
  2697. }
  2698. else {
  2699. array_elem.i64 = POP_I64();
  2700. }
  2701. elem_idx = POP_I32();
  2702. array_obj = POP_REF();
  2703. if (!array_obj) {
  2704. wasm_set_exception(module, "null array reference");
  2705. goto got_exception;
  2706. }
  2707. if (elem_idx >= wasm_array_obj_length(array_obj)) {
  2708. wasm_set_exception(module,
  2709. "out of bounds array access");
  2710. goto got_exception;
  2711. }
  2712. wasm_array_obj_set_elem(array_obj, elem_idx,
  2713. &array_elem);
  2714. HANDLE_OP_END();
  2715. }
  2716. case WASM_OP_ARRAY_LEN:
  2717. {
  2718. uint32 array_len;
  2719. array_obj = POP_REF();
  2720. if (!array_obj) {
  2721. wasm_set_exception(module, "null array reference");
  2722. goto got_exception;
  2723. }
  2724. array_len = wasm_array_obj_length(array_obj);
  2725. PUSH_I32(array_len);
  2726. HANDLE_OP_END();
  2727. }
  2728. case WASM_OP_ARRAY_FILL:
  2729. {
  2730. WASMArrayType *array_type;
  2731. WASMValue fill_value = { 0 };
  2732. uint32 start_offset, len;
  2733. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2734. array_type =
  2735. (WASMArrayType *)module->module->types[type_index];
  2736. len = POP_I32();
  2737. if (wasm_is_type_reftype(array_type->elem_type)) {
  2738. fill_value.gc_obj = POP_REF();
  2739. }
  2740. else if (array_type->elem_type == VALUE_TYPE_I32
  2741. || array_type->elem_type == VALUE_TYPE_F32
  2742. || array_type->elem_type == PACKED_TYPE_I8
  2743. || array_type->elem_type == PACKED_TYPE_I16) {
  2744. fill_value.i32 = POP_I32();
  2745. }
  2746. else {
  2747. fill_value.i64 = POP_I64();
  2748. }
  2749. start_offset = POP_I32();
  2750. array_obj = POP_REF();
  2751. if (!array_obj) {
  2752. wasm_set_exception(module, "null array reference");
  2753. goto got_exception;
  2754. }
  2755. if (len > 0) {
  2756. if ((uint64)start_offset + len
  2757. >= wasm_array_obj_length(array_obj)) {
  2758. wasm_set_exception(
  2759. module, "out of bounds array access");
  2760. goto got_exception;
  2761. }
  2762. wasm_array_obj_fill(array_obj, start_offset, len,
  2763. &fill_value);
  2764. }
  2765. HANDLE_OP_END();
  2766. }
  2767. case WASM_OP_ARRAY_COPY:
  2768. {
  2769. uint32 dst_offset, src_offset, len, src_type_index;
  2770. WASMArrayObjectRef src_obj, dst_obj;
  2771. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2772. read_leb_uint32(frame_ip, frame_ip_end, src_type_index);
  2773. len = POP_I32();
  2774. src_offset = POP_I32();
  2775. src_obj = POP_REF();
  2776. dst_offset = POP_I32();
  2777. dst_obj = POP_REF();
  2778. if (!src_obj || !dst_obj) {
  2779. wasm_set_exception(module, "null array reference");
  2780. goto got_exception;
  2781. }
  2782. if (len > 0) {
  2783. if ((dst_offset > UINT32_MAX - len)
  2784. || (dst_offset + len
  2785. > wasm_array_obj_length(dst_obj))
  2786. || (src_offset > UINT32_MAX - len)
  2787. || (src_offset + len
  2788. > wasm_array_obj_length(src_obj))) {
  2789. wasm_set_exception(
  2790. module, "out of bounds array access");
  2791. goto got_exception;
  2792. }
  2793. wasm_array_obj_copy(dst_obj, dst_offset, src_obj,
  2794. src_offset, len);
  2795. }
  2796. (void)src_type_index;
  2797. HANDLE_OP_END();
  2798. }
  2799. case WASM_OP_REF_I31:
  2800. {
  2801. uint32 i31_val;
  2802. i31_val = POP_I32();
  2803. i31_obj = wasm_i31_obj_new(i31_val);
  2804. PUSH_I31REF(i31_obj);
  2805. HANDLE_OP_END();
  2806. }
  2807. case WASM_OP_I31_GET_S:
  2808. case WASM_OP_I31_GET_U:
  2809. {
  2810. uint32 i31_val;
  2811. i31_obj = (WASMI31ObjectRef)POP_REF();
  2812. if (!i31_obj) {
  2813. wasm_set_exception(module, "null i31 reference");
  2814. goto got_exception;
  2815. }
  2816. i31_val = (uint32)(((uintptr_t)i31_obj) >> 1);
  2817. if (opcode == WASM_OP_I31_GET_S
  2818. && (i31_val & 0x40000000) /* bit 30 is 1 */)
  2819. /* set bit 31 to 1 */
  2820. i31_val |= 0x80000000;
  2821. PUSH_I32(i31_val);
  2822. HANDLE_OP_END();
  2823. }
  2824. case WASM_OP_REF_TEST:
  2825. case WASM_OP_REF_CAST:
  2826. case WASM_OP_REF_TEST_NULLABLE:
  2827. case WASM_OP_REF_CAST_NULLABLE:
  2828. {
  2829. int32 heap_type;
  2830. read_leb_int32(frame_ip, frame_ip_end, heap_type);
  2831. gc_obj = GET_REF_FROM_ADDR(frame_sp - REF_CELL_NUM);
  2832. if (!gc_obj) {
  2833. if (opcode == WASM_OP_REF_TEST
  2834. || opcode == WASM_OP_REF_TEST_NULLABLE) {
  2835. (void)POP_REF();
  2836. if (opcode == WASM_OP_REF_TEST)
  2837. PUSH_I32(0);
  2838. else
  2839. PUSH_I32(1);
  2840. }
  2841. else if (opcode == WASM_OP_REF_CAST) {
  2842. wasm_set_exception(module, "cast failure");
  2843. goto got_exception;
  2844. }
  2845. else {
  2846. /* Do nothing for WASM_OP_REF_CAST_NULLABLE */
  2847. }
  2848. }
  2849. else {
  2850. bool castable = false;
  2851. if (heap_type >= 0) {
  2852. WASMModule *wasm_module = module->module;
  2853. castable = wasm_obj_is_instance_of(
  2854. gc_obj, (uint32)heap_type,
  2855. wasm_module->types,
  2856. wasm_module->type_count);
  2857. }
  2858. else {
  2859. castable =
  2860. wasm_obj_is_type_of(gc_obj, heap_type);
  2861. }
  2862. if (opcode == WASM_OP_REF_TEST
  2863. || opcode == WASM_OP_REF_TEST_NULLABLE) {
  2864. (void)POP_REF();
  2865. if (castable)
  2866. PUSH_I32(1);
  2867. else
  2868. PUSH_I32(0);
  2869. }
  2870. else if (!castable) {
  2871. wasm_set_exception(module, "cast failure");
  2872. goto got_exception;
  2873. }
  2874. }
  2875. HANDLE_OP_END();
  2876. }
  2877. case WASM_OP_BR_ON_CAST:
  2878. case WASM_OP_BR_ON_CAST_FAIL:
  2879. {
  2880. int32 heap_type, heap_type_dst;
  2881. uint8 castflags;
  2882. #if WASM_ENABLE_THREAD_MGR != 0
  2883. CHECK_SUSPEND_FLAGS();
  2884. #endif
  2885. castflags = *frame_ip++;
  2886. read_leb_uint32(frame_ip, frame_ip_end, depth);
  2887. read_leb_int32(frame_ip, frame_ip_end, heap_type);
  2888. read_leb_int32(frame_ip, frame_ip_end, heap_type_dst);
  2889. gc_obj = GET_REF_FROM_ADDR(frame_sp - REF_CELL_NUM);
  2890. if (!gc_obj) {
  2891. /*
  2892. * castflags should be 0~3:
  2893. * 0: (non-null, non-null)
  2894. * 1: (null, non-null)
  2895. * 2: (non-null, null)
  2896. * 3: (null, null)
  2897. */
  2898. if (
  2899. /* op is BR_ON_CAST and dst reftype is nullable
  2900. */
  2901. ((opcode1 == WASM_OP_BR_ON_CAST)
  2902. && ((castflags == 2) || (castflags == 3)))
  2903. /* op is BR_ON_CAST_FAIL and dst reftype is
  2904. non-nullable */
  2905. || ((opcode1 == WASM_OP_BR_ON_CAST_FAIL)
  2906. && ((castflags == 0) || (castflags == 1))))
  2907. goto label_pop_csp_n;
  2908. }
  2909. else {
  2910. bool castable = false;
  2911. if (heap_type_dst >= 0) {
  2912. WASMModule *wasm_module = module->module;
  2913. castable = wasm_obj_is_instance_of(
  2914. gc_obj, (uint32)heap_type_dst,
  2915. wasm_module->types,
  2916. wasm_module->type_count);
  2917. }
  2918. else {
  2919. castable =
  2920. wasm_obj_is_type_of(gc_obj, heap_type_dst);
  2921. }
  2922. if ((castable && (opcode == WASM_OP_BR_ON_CAST))
  2923. || (!castable
  2924. && (opcode == WASM_OP_BR_ON_CAST_FAIL))) {
  2925. goto label_pop_csp_n;
  2926. }
  2927. }
  2928. (void)heap_type;
  2929. HANDLE_OP_END();
  2930. }
  2931. case WASM_OP_ANY_CONVERT_EXTERN:
  2932. {
  2933. externref_obj = POP_REF();
  2934. if (externref_obj == NULL_REF)
  2935. PUSH_REF(NULL_REF);
  2936. else {
  2937. gc_obj = wasm_externref_obj_to_internal_obj(
  2938. externref_obj);
  2939. PUSH_REF(gc_obj);
  2940. }
  2941. HANDLE_OP_END();
  2942. }
  2943. case WASM_OP_EXTERN_CONVERT_ANY:
  2944. {
  2945. gc_obj = POP_REF();
  2946. if (gc_obj == NULL_REF)
  2947. PUSH_REF(NULL_REF);
  2948. else {
  2949. if (!(externref_obj =
  2950. wasm_internal_obj_to_externref_obj(
  2951. exec_env, gc_obj))) {
  2952. wasm_set_exception(
  2953. module, "create externref object failed");
  2954. goto got_exception;
  2955. }
  2956. PUSH_REF(externref_obj);
  2957. }
  2958. HANDLE_OP_END();
  2959. }
  2960. #if WASM_ENABLE_STRINGREF != 0
  2961. case WASM_OP_STRING_NEW_UTF8:
  2962. case WASM_OP_STRING_NEW_WTF16:
  2963. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  2964. case WASM_OP_STRING_NEW_WTF8:
  2965. {
  2966. uint32 mem_idx, addr, bytes_length, offset = 0;
  2967. EncodingFlag flag = WTF8;
  2968. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  2969. bytes_length = POP_I32();
  2970. addr = POP_I32();
  2971. CHECK_MEMORY_OVERFLOW(bytes_length);
  2972. if (opcode == WASM_OP_STRING_NEW_WTF16) {
  2973. flag = WTF16;
  2974. }
  2975. else if (opcode == WASM_OP_STRING_NEW_UTF8) {
  2976. flag = UTF8;
  2977. }
  2978. else if (opcode == WASM_OP_STRING_NEW_LOSSY_UTF8) {
  2979. flag = LOSSY_UTF8;
  2980. }
  2981. else if (opcode == WASM_OP_STRING_NEW_WTF8) {
  2982. flag = WTF8;
  2983. }
  2984. str_obj = wasm_string_new_with_encoding(
  2985. maddr, bytes_length, flag);
  2986. if (!str_obj) {
  2987. wasm_set_exception(module,
  2988. "create string object failed");
  2989. goto got_exception;
  2990. }
  2991. SYNC_ALL_TO_FRAME();
  2992. stringref_obj =
  2993. wasm_stringref_obj_new(exec_env, str_obj);
  2994. if (!stringref_obj) {
  2995. wasm_set_exception(module,
  2996. "create stringref failed");
  2997. goto got_exception;
  2998. }
  2999. PUSH_REF(stringref_obj);
  3000. (void)mem_idx;
  3001. HANDLE_OP_END();
  3002. }
  3003. case WASM_OP_STRING_CONST:
  3004. {
  3005. WASMModule *wasm_module = module->module;
  3006. uint32 contents;
  3007. read_leb_uint32(frame_ip, frame_ip_end, contents);
  3008. str_obj = wasm_string_new_const(
  3009. (const char *)
  3010. wasm_module->string_literal_ptrs[contents],
  3011. wasm_module->string_literal_lengths[contents]);
  3012. if (!str_obj) {
  3013. wasm_set_exception(module,
  3014. "create string object failed");
  3015. goto got_exception;
  3016. }
  3017. SYNC_ALL_TO_FRAME();
  3018. stringref_obj =
  3019. wasm_stringref_obj_new(exec_env, str_obj);
  3020. if (!stringref_obj) {
  3021. wasm_set_exception(module,
  3022. "create stringref failed");
  3023. goto got_exception;
  3024. }
  3025. PUSH_REF(stringref_obj);
  3026. HANDLE_OP_END();
  3027. }
  3028. case WASM_OP_STRING_MEASURE_UTF8:
  3029. case WASM_OP_STRING_MEASURE_WTF8:
  3030. case WASM_OP_STRING_MEASURE_WTF16:
  3031. {
  3032. int32 target_bytes_length;
  3033. EncodingFlag flag = WTF8;
  3034. stringref_obj = POP_REF();
  3035. if (opcode == WASM_OP_STRING_MEASURE_WTF16) {
  3036. flag = WTF16;
  3037. }
  3038. else if (opcode == WASM_OP_STRING_MEASURE_UTF8) {
  3039. flag = UTF8;
  3040. }
  3041. else if (opcode == WASM_OP_STRING_MEASURE_WTF8) {
  3042. flag = LOSSY_UTF8;
  3043. }
  3044. target_bytes_length = wasm_string_measure(
  3045. (WASMString)wasm_stringref_obj_get_value(
  3046. stringref_obj),
  3047. flag);
  3048. PUSH_I32(target_bytes_length);
  3049. HANDLE_OP_END();
  3050. }
  3051. case WASM_OP_STRING_ENCODE_UTF8:
  3052. case WASM_OP_STRING_ENCODE_WTF16:
  3053. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  3054. case WASM_OP_STRING_ENCODE_WTF8:
  3055. {
  3056. uint32 mem_idx, addr;
  3057. int32 target_bytes_length;
  3058. WASMMemoryInstance *memory_inst;
  3059. EncodingFlag flag = WTF8;
  3060. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  3061. addr = POP_I32();
  3062. stringref_obj = POP_REF();
  3063. str_obj = (WASMString)wasm_stringref_obj_get_value(
  3064. stringref_obj);
  3065. memory_inst = module->memories[mem_idx];
  3066. maddr = memory_inst->memory_data + addr;
  3067. if (opcode == WASM_OP_STRING_ENCODE_WTF16) {
  3068. flag = WTF16;
  3069. count = wasm_string_measure(str_obj, flag);
  3070. target_bytes_length = wasm_string_encode(
  3071. str_obj, 0, count, maddr, NULL, flag);
  3072. }
  3073. else {
  3074. if (opcode == WASM_OP_STRING_ENCODE_UTF8) {
  3075. flag = UTF8;
  3076. }
  3077. else if (opcode
  3078. == WASM_OP_STRING_ENCODE_LOSSY_UTF8) {
  3079. flag = LOSSY_UTF8;
  3080. }
  3081. else if (opcode == WASM_OP_STRING_ENCODE_WTF8) {
  3082. flag = WTF8;
  3083. }
  3084. count = wasm_string_measure(str_obj, flag);
  3085. target_bytes_length = wasm_string_encode(
  3086. str_obj, 0, count, maddr, NULL, flag);
  3087. if (target_bytes_length == -1) {
  3088. wasm_set_exception(
  3089. module, "isolated surrogate is seen");
  3090. goto got_exception;
  3091. }
  3092. }
  3093. if (target_bytes_length < 0) {
  3094. wasm_set_exception(module,
  3095. "stringref encode failed");
  3096. goto got_exception;
  3097. }
  3098. PUSH_I32(target_bytes_length);
  3099. HANDLE_OP_END();
  3100. }
  3101. case WASM_OP_STRING_CONCAT:
  3102. {
  3103. WASMStringrefObjectRef stringref_obj1, stringref_obj2;
  3104. stringref_obj2 = POP_REF();
  3105. stringref_obj1 = POP_REF();
  3106. str_obj = wasm_string_concat(
  3107. (WASMString)wasm_stringref_obj_get_value(
  3108. stringref_obj1),
  3109. (WASMString)wasm_stringref_obj_get_value(
  3110. stringref_obj2));
  3111. if (!str_obj) {
  3112. wasm_set_exception(module,
  3113. "create string object failed");
  3114. goto got_exception;
  3115. }
  3116. SYNC_ALL_TO_FRAME();
  3117. stringref_obj =
  3118. wasm_stringref_obj_new(exec_env, str_obj);
  3119. if (!stringref_obj) {
  3120. wasm_set_exception(module,
  3121. "create stringref failed");
  3122. goto got_exception;
  3123. }
  3124. PUSH_REF(stringref_obj);
  3125. HANDLE_OP_END();
  3126. }
  3127. case WASM_OP_STRING_EQ:
  3128. {
  3129. WASMStringrefObjectRef stringref_obj1, stringref_obj2;
  3130. int32 is_eq;
  3131. stringref_obj2 = POP_REF();
  3132. stringref_obj1 = POP_REF();
  3133. is_eq = wasm_string_eq(
  3134. (WASMString)wasm_stringref_obj_get_value(
  3135. stringref_obj1),
  3136. (WASMString)wasm_stringref_obj_get_value(
  3137. stringref_obj2));
  3138. PUSH_I32(is_eq);
  3139. HANDLE_OP_END();
  3140. }
  3141. case WASM_OP_STRING_IS_USV_SEQUENCE:
  3142. {
  3143. int32 is_usv_sequence;
  3144. stringref_obj = POP_REF();
  3145. is_usv_sequence = wasm_string_is_usv_sequence(
  3146. (WASMString)wasm_stringref_obj_get_value(
  3147. stringref_obj));
  3148. PUSH_I32(is_usv_sequence);
  3149. HANDLE_OP_END();
  3150. }
  3151. case WASM_OP_STRING_AS_WTF8:
  3152. {
  3153. stringref_obj = POP_REF();
  3154. str_obj = wasm_string_create_view(
  3155. (WASMString)wasm_stringref_obj_get_value(
  3156. stringref_obj),
  3157. STRING_VIEW_WTF8);
  3158. if (!str_obj) {
  3159. wasm_set_exception(module,
  3160. "create string object failed");
  3161. goto got_exception;
  3162. }
  3163. SYNC_ALL_TO_FRAME();
  3164. stringview_wtf8_obj =
  3165. wasm_stringview_wtf8_obj_new(exec_env, str_obj);
  3166. if (!stringview_wtf8_obj) {
  3167. wasm_set_exception(module,
  3168. "create stringview wtf8 failed");
  3169. goto got_exception;
  3170. }
  3171. PUSH_REF(stringview_wtf8_obj);
  3172. HANDLE_OP_END();
  3173. }
  3174. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  3175. {
  3176. uint32 next_pos, bytes, pos;
  3177. bytes = POP_I32();
  3178. pos = POP_I32();
  3179. stringview_wtf8_obj = POP_REF();
  3180. next_pos = wasm_string_advance(
  3181. (WASMString)wasm_stringview_wtf8_obj_get_value(
  3182. stringview_wtf8_obj),
  3183. pos, bytes, NULL);
  3184. PUSH_I32(next_pos);
  3185. HANDLE_OP_END();
  3186. }
  3187. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  3188. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  3189. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  3190. {
  3191. uint32 mem_idx, addr, pos, bytes, next_pos;
  3192. int32 bytes_written;
  3193. WASMMemoryInstance *memory_inst;
  3194. EncodingFlag flag = WTF8;
  3195. if (opcode == WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8) {
  3196. flag = UTF8;
  3197. }
  3198. else if (opcode
  3199. == WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8) {
  3200. flag = LOSSY_UTF8;
  3201. }
  3202. else if (opcode
  3203. == WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8) {
  3204. flag = WTF8;
  3205. }
  3206. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  3207. bytes = POP_I32();
  3208. pos = POP_I32();
  3209. addr = POP_I32();
  3210. stringview_wtf8_obj = POP_REF();
  3211. memory_inst = module->memories[mem_idx];
  3212. maddr = memory_inst->memory_data + addr;
  3213. bytes_written = wasm_string_encode(
  3214. (WASMString)wasm_stringview_wtf8_obj_get_value(
  3215. stringview_wtf8_obj),
  3216. pos, bytes, maddr, &next_pos, flag);
  3217. if (bytes_written < 0) {
  3218. if (bytes_written == Isolated_Surrogate) {
  3219. wasm_set_exception(
  3220. module, "isolated surrogate is seen");
  3221. }
  3222. else {
  3223. wasm_set_exception(module, "encode failed");
  3224. }
  3225. goto got_exception;
  3226. }
  3227. PUSH_I32(next_pos);
  3228. PUSH_I32(bytes_written);
  3229. HANDLE_OP_END();
  3230. }
  3231. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  3232. {
  3233. uint32 start, end;
  3234. end = POP_I32();
  3235. start = POP_I32();
  3236. stringview_wtf8_obj = POP_REF();
  3237. str_obj = wasm_string_slice(
  3238. (WASMString)wasm_stringview_wtf8_obj_get_value(
  3239. stringview_wtf8_obj),
  3240. start, end, STRING_VIEW_WTF8);
  3241. if (!str_obj) {
  3242. wasm_set_exception(module,
  3243. "create string object failed");
  3244. goto got_exception;
  3245. }
  3246. SYNC_ALL_TO_FRAME();
  3247. stringref_obj =
  3248. wasm_stringref_obj_new(exec_env, str_obj);
  3249. if (!stringref_obj) {
  3250. wasm_set_exception(module,
  3251. "create stringref failed");
  3252. goto got_exception;
  3253. }
  3254. PUSH_REF(stringref_obj);
  3255. HANDLE_OP_END();
  3256. }
  3257. case WASM_OP_STRING_AS_WTF16:
  3258. {
  3259. stringref_obj = POP_REF();
  3260. str_obj = wasm_string_create_view(
  3261. (WASMString)wasm_stringref_obj_get_value(
  3262. stringref_obj),
  3263. STRING_VIEW_WTF16);
  3264. if (!str_obj) {
  3265. wasm_set_exception(module,
  3266. "create string object failed");
  3267. goto got_exception;
  3268. }
  3269. SYNC_ALL_TO_FRAME();
  3270. stringview_wtf16_obj =
  3271. wasm_stringview_wtf16_obj_new(exec_env, str_obj);
  3272. if (!stringview_wtf16_obj) {
  3273. wasm_set_exception(
  3274. module, "create stringview wtf16 failed");
  3275. goto got_exception;
  3276. }
  3277. PUSH_REF(stringview_wtf16_obj);
  3278. HANDLE_OP_END();
  3279. }
  3280. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  3281. {
  3282. int32 code_units_length;
  3283. stringview_wtf16_obj = POP_REF();
  3284. code_units_length = wasm_string_wtf16_get_length(
  3285. (WASMString)wasm_stringview_wtf16_obj_get_value(
  3286. stringview_wtf16_obj));
  3287. PUSH_I32(code_units_length);
  3288. HANDLE_OP_END();
  3289. }
  3290. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  3291. {
  3292. int32 pos;
  3293. uint32 code_unit;
  3294. pos = POP_I32();
  3295. stringview_wtf16_obj = POP_REF();
  3296. code_unit = (uint32)wasm_string_get_wtf16_codeunit(
  3297. (WASMString)wasm_stringview_wtf16_obj_get_value(
  3298. stringview_wtf16_obj),
  3299. pos);
  3300. PUSH_I32(code_unit);
  3301. HANDLE_OP_END();
  3302. }
  3303. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  3304. {
  3305. uint32 mem_idx, addr, pos, len, offset = 0;
  3306. int32 written_code_units = 0;
  3307. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  3308. len = POP_I32();
  3309. pos = POP_I32();
  3310. addr = POP_I32();
  3311. stringview_wtf16_obj = POP_REF();
  3312. CHECK_MEMORY_OVERFLOW(len * sizeof(uint16));
  3313. /* check 2-byte alignment */
  3314. if (((uintptr_t)maddr & (((uintptr_t)1 << 2) - 1))
  3315. != 0) {
  3316. wasm_set_exception(module,
  3317. "unaligned memory access");
  3318. goto got_exception;
  3319. }
  3320. written_code_units = wasm_string_encode(
  3321. (WASMString)wasm_stringview_wtf16_obj_get_value(
  3322. stringview_wtf16_obj),
  3323. pos, len, maddr, NULL, WTF16);
  3324. if (written_code_units < 0) {
  3325. wasm_set_exception(module, "encode failed");
  3326. goto got_exception;
  3327. }
  3328. PUSH_I32(written_code_units);
  3329. (void)mem_idx;
  3330. HANDLE_OP_END();
  3331. }
  3332. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  3333. {
  3334. uint32 start, end;
  3335. end = POP_I32();
  3336. start = POP_I32();
  3337. stringview_wtf16_obj = POP_REF();
  3338. str_obj = wasm_string_slice(
  3339. (WASMString)wasm_stringview_wtf16_obj_get_value(
  3340. stringview_wtf16_obj),
  3341. start, end, STRING_VIEW_WTF16);
  3342. if (!str_obj) {
  3343. wasm_set_exception(module,
  3344. "create string object failed");
  3345. goto got_exception;
  3346. }
  3347. SYNC_ALL_TO_FRAME();
  3348. stringref_obj =
  3349. wasm_stringref_obj_new(exec_env, str_obj);
  3350. if (!stringref_obj) {
  3351. wasm_set_exception(module,
  3352. "create stringref failed");
  3353. goto got_exception;
  3354. }
  3355. PUSH_REF(stringref_obj);
  3356. HANDLE_OP_END();
  3357. }
  3358. case WASM_OP_STRING_AS_ITER:
  3359. {
  3360. stringref_obj = POP_REF();
  3361. str_obj = wasm_string_create_view(
  3362. (WASMString)wasm_stringref_obj_get_value(
  3363. stringref_obj),
  3364. STRING_VIEW_ITER);
  3365. if (!str_obj) {
  3366. wasm_set_exception(module,
  3367. "create string object failed");
  3368. goto got_exception;
  3369. }
  3370. SYNC_ALL_TO_FRAME();
  3371. stringview_iter_obj =
  3372. wasm_stringview_iter_obj_new(exec_env, str_obj, 0);
  3373. if (!stringview_iter_obj) {
  3374. wasm_set_exception(module,
  3375. "create stringview iter failed");
  3376. goto got_exception;
  3377. }
  3378. PUSH_REF(stringview_iter_obj);
  3379. HANDLE_OP_END();
  3380. }
  3381. case WASM_OP_STRINGVIEW_ITER_NEXT:
  3382. {
  3383. uint32 code_point;
  3384. stringview_iter_obj = POP_REF();
  3385. code_point = wasm_string_next_codepoint(
  3386. (WASMString)wasm_stringview_iter_obj_get_value(
  3387. stringview_iter_obj),
  3388. wasm_stringview_iter_obj_get_pos(
  3389. stringview_iter_obj));
  3390. PUSH_I32(code_point);
  3391. HANDLE_OP_END();
  3392. }
  3393. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  3394. case WASM_OP_STRINGVIEW_ITER_REWIND:
  3395. {
  3396. uint32 code_points_count, code_points_consumed = 0,
  3397. cur_pos, next_pos = 0;
  3398. code_points_count = POP_I32();
  3399. stringview_iter_obj = POP_REF();
  3400. str_obj =
  3401. (WASMString)wasm_stringview_iter_obj_get_value(
  3402. stringview_iter_obj);
  3403. cur_pos = wasm_stringview_iter_obj_get_pos(
  3404. stringview_iter_obj);
  3405. if (opcode == WASM_OP_STRINGVIEW_ITER_ADVANCE) {
  3406. next_pos = wasm_string_advance(
  3407. str_obj, cur_pos, code_points_count,
  3408. &code_points_consumed);
  3409. }
  3410. else if (opcode == WASM_OP_STRINGVIEW_ITER_REWIND) {
  3411. next_pos = wasm_string_rewind(
  3412. str_obj, cur_pos, code_points_count,
  3413. &code_points_consumed);
  3414. }
  3415. wasm_stringview_iter_obj_update_pos(stringview_iter_obj,
  3416. next_pos);
  3417. PUSH_I32(code_points_consumed);
  3418. HANDLE_OP_END();
  3419. }
  3420. case WASM_OP_STRINGVIEW_ITER_SLICE:
  3421. {
  3422. uint32 code_points_count, cur_pos;
  3423. code_points_count = POP_I32();
  3424. stringview_iter_obj = POP_REF();
  3425. cur_pos = wasm_stringview_iter_obj_get_pos(
  3426. stringview_iter_obj);
  3427. str_obj = wasm_string_slice(
  3428. (WASMString)wasm_stringview_iter_obj_get_value(
  3429. stringview_iter_obj),
  3430. cur_pos, cur_pos + code_points_count,
  3431. STRING_VIEW_ITER);
  3432. if (!str_obj) {
  3433. wasm_set_exception(module,
  3434. "create string object failed");
  3435. goto got_exception;
  3436. }
  3437. SYNC_ALL_TO_FRAME();
  3438. stringref_obj =
  3439. wasm_stringref_obj_new(exec_env, str_obj);
  3440. if (!stringref_obj) {
  3441. wasm_set_exception(module,
  3442. "create stringref failed");
  3443. goto got_exception;
  3444. }
  3445. PUSH_REF(stringref_obj);
  3446. HANDLE_OP_END();
  3447. }
  3448. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  3449. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  3450. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  3451. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  3452. {
  3453. uint32 start, end, array_len;
  3454. EncodingFlag flag = WTF8;
  3455. WASMArrayType *array_type;
  3456. void *arr_start_addr;
  3457. end = POP_I32();
  3458. start = POP_I32();
  3459. array_obj = POP_REF();
  3460. array_type = (WASMArrayType *)wasm_obj_get_defined_type(
  3461. (WASMObjectRef)array_obj);
  3462. arr_start_addr =
  3463. wasm_array_obj_elem_addr(array_obj, start);
  3464. array_len = wasm_array_obj_length(array_obj);
  3465. if (start > end || end > array_len) {
  3466. wasm_set_exception(module,
  3467. "out of bounds array access");
  3468. goto got_exception;
  3469. }
  3470. if (opcode == WASM_OP_STRING_NEW_WTF16_ARRAY) {
  3471. if (array_type->elem_type != VALUE_TYPE_I16) {
  3472. wasm_set_exception(module,
  3473. "array type mismatch");
  3474. goto got_exception;
  3475. }
  3476. flag = WTF16;
  3477. }
  3478. else {
  3479. if (array_type->elem_type != VALUE_TYPE_I8) {
  3480. wasm_set_exception(module,
  3481. "array type mismatch");
  3482. goto got_exception;
  3483. }
  3484. if (opcode == WASM_OP_STRING_NEW_UTF8_ARRAY) {
  3485. flag = UTF8;
  3486. }
  3487. else if (opcode == WASM_OP_STRING_NEW_WTF8_ARRAY) {
  3488. flag = WTF8;
  3489. }
  3490. else if (opcode
  3491. == WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY) {
  3492. flag = LOSSY_UTF8;
  3493. }
  3494. }
  3495. str_obj = wasm_string_new_with_encoding(
  3496. arr_start_addr, (end - start), flag);
  3497. if (!str_obj) {
  3498. wasm_set_exception(module,
  3499. "create string object failed");
  3500. goto got_exception;
  3501. }
  3502. SYNC_ALL_TO_FRAME();
  3503. stringref_obj =
  3504. wasm_stringref_obj_new(exec_env, str_obj);
  3505. if (!stringref_obj) {
  3506. wasm_set_exception(module,
  3507. "create stringref failed");
  3508. goto got_exception;
  3509. }
  3510. PUSH_REF(stringref_obj);
  3511. HANDLE_OP_END();
  3512. }
  3513. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  3514. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  3515. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  3516. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  3517. {
  3518. uint32 start, array_len, count;
  3519. int32 bytes_written;
  3520. EncodingFlag flag = WTF8;
  3521. WASMArrayType *array_type;
  3522. void *arr_start_addr;
  3523. start = POP_I32();
  3524. array_obj = POP_REF();
  3525. stringref_obj = POP_REF();
  3526. str_obj = (WASMString)wasm_stringref_obj_get_value(
  3527. stringref_obj);
  3528. array_type = (WASMArrayType *)wasm_obj_get_defined_type(
  3529. (WASMObjectRef)array_obj);
  3530. arr_start_addr =
  3531. wasm_array_obj_elem_addr(array_obj, start);
  3532. array_len = wasm_array_obj_length(array_obj);
  3533. if (start > array_len) {
  3534. wasm_set_exception(module,
  3535. "out of bounds array access");
  3536. goto got_exception;
  3537. }
  3538. if (opcode == WASM_OP_STRING_ENCODE_WTF16_ARRAY) {
  3539. if (array_type->elem_type != VALUE_TYPE_I16) {
  3540. wasm_set_exception(module,
  3541. "array type mismatch");
  3542. goto got_exception;
  3543. }
  3544. flag = WTF16;
  3545. }
  3546. else {
  3547. if (array_type->elem_type != VALUE_TYPE_I8) {
  3548. wasm_set_exception(module,
  3549. "array type mismatch");
  3550. goto got_exception;
  3551. }
  3552. if (opcode == WASM_OP_STRING_ENCODE_UTF8_ARRAY) {
  3553. flag = UTF8;
  3554. }
  3555. else if (opcode
  3556. == WASM_OP_STRING_ENCODE_WTF8_ARRAY) {
  3557. flag = WTF8;
  3558. }
  3559. else if (
  3560. opcode
  3561. == WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY) {
  3562. flag = LOSSY_UTF8;
  3563. }
  3564. }
  3565. count = wasm_string_measure(str_obj, flag);
  3566. bytes_written = wasm_string_encode(
  3567. str_obj, 0, count, arr_start_addr, NULL, flag);
  3568. if (bytes_written < 0) {
  3569. if (bytes_written == Isolated_Surrogate) {
  3570. wasm_set_exception(
  3571. module, "isolated surrogate is seen");
  3572. }
  3573. else if (bytes_written == Insufficient_Space) {
  3574. wasm_set_exception(
  3575. module, "array space is insufficient");
  3576. }
  3577. else {
  3578. wasm_set_exception(module, "encode failed");
  3579. }
  3580. goto got_exception;
  3581. }
  3582. PUSH_I32(bytes_written);
  3583. HANDLE_OP_END();
  3584. }
  3585. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  3586. default:
  3587. {
  3588. wasm_set_exception(module, "unsupported opcode");
  3589. goto got_exception;
  3590. }
  3591. }
  3592. }
  3593. #endif /* end of WASM_ENABLE_GC != 0 */
  3594. /* variable instructions */
  3595. HANDLE_OP(WASM_OP_GET_LOCAL)
  3596. {
  3597. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  3598. switch (local_type) {
  3599. case VALUE_TYPE_I32:
  3600. case VALUE_TYPE_F32:
  3601. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  3602. case VALUE_TYPE_FUNCREF:
  3603. case VALUE_TYPE_EXTERNREF:
  3604. #endif
  3605. PUSH_I32(*(int32 *)(frame_lp + local_offset));
  3606. break;
  3607. case VALUE_TYPE_I64:
  3608. case VALUE_TYPE_F64:
  3609. PUSH_I64(GET_I64_FROM_ADDR(frame_lp + local_offset));
  3610. break;
  3611. default:
  3612. #if WASM_ENABLE_GC != 0
  3613. if (wasm_is_type_reftype(local_type)) {
  3614. if (wasm_is_reftype_i31ref(local_type)) {
  3615. PUSH_I31REF(
  3616. GET_REF_FROM_ADDR(frame_lp + local_offset));
  3617. }
  3618. else {
  3619. PUSH_REF(
  3620. GET_REF_FROM_ADDR(frame_lp + local_offset));
  3621. }
  3622. }
  3623. else
  3624. #endif
  3625. {
  3626. wasm_set_exception(module, "invalid local type");
  3627. goto got_exception;
  3628. }
  3629. }
  3630. HANDLE_OP_END();
  3631. }
  3632. HANDLE_OP(EXT_OP_GET_LOCAL_FAST)
  3633. {
  3634. local_offset = *frame_ip++;
  3635. if (local_offset & 0x80)
  3636. PUSH_I64(
  3637. GET_I64_FROM_ADDR(frame_lp + (local_offset & 0x7F)));
  3638. else
  3639. PUSH_I32(*(int32 *)(frame_lp + local_offset));
  3640. HANDLE_OP_END();
  3641. }
  3642. HANDLE_OP(WASM_OP_SET_LOCAL)
  3643. {
  3644. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  3645. switch (local_type) {
  3646. case VALUE_TYPE_I32:
  3647. case VALUE_TYPE_F32:
  3648. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  3649. case VALUE_TYPE_FUNCREF:
  3650. case VALUE_TYPE_EXTERNREF:
  3651. #endif
  3652. *(int32 *)(frame_lp + local_offset) = POP_I32();
  3653. break;
  3654. case VALUE_TYPE_I64:
  3655. case VALUE_TYPE_F64:
  3656. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  3657. POP_I64());
  3658. break;
  3659. default:
  3660. #if WASM_ENABLE_GC != 0
  3661. if (wasm_is_type_reftype(local_type)) {
  3662. PUT_REF_TO_ADDR(frame_lp + local_offset, POP_REF());
  3663. }
  3664. else
  3665. #endif
  3666. {
  3667. wasm_set_exception(module, "invalid local type");
  3668. goto got_exception;
  3669. }
  3670. }
  3671. HANDLE_OP_END();
  3672. }
  3673. HANDLE_OP(EXT_OP_SET_LOCAL_FAST)
  3674. {
  3675. local_offset = *frame_ip++;
  3676. if (local_offset & 0x80)
  3677. PUT_I64_TO_ADDR(
  3678. (uint32 *)(frame_lp + (local_offset & 0x7F)),
  3679. POP_I64());
  3680. else
  3681. *(int32 *)(frame_lp + local_offset) = POP_I32();
  3682. HANDLE_OP_END();
  3683. }
  3684. HANDLE_OP(WASM_OP_TEE_LOCAL)
  3685. {
  3686. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  3687. switch (local_type) {
  3688. case VALUE_TYPE_I32:
  3689. case VALUE_TYPE_F32:
  3690. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  3691. case VALUE_TYPE_FUNCREF:
  3692. case VALUE_TYPE_EXTERNREF:
  3693. #endif
  3694. *(int32 *)(frame_lp + local_offset) =
  3695. *(int32 *)(frame_sp - 1);
  3696. break;
  3697. case VALUE_TYPE_I64:
  3698. case VALUE_TYPE_F64:
  3699. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  3700. GET_I64_FROM_ADDR(frame_sp - 2));
  3701. break;
  3702. default:
  3703. #if WASM_ENABLE_GC != 0
  3704. if (wasm_is_type_reftype(local_type)) {
  3705. PUT_REF_TO_ADDR(
  3706. frame_lp + local_offset,
  3707. GET_REF_FROM_ADDR(frame_sp - REF_CELL_NUM));
  3708. }
  3709. else
  3710. #endif
  3711. {
  3712. wasm_set_exception(module, "invalid local type");
  3713. goto got_exception;
  3714. }
  3715. }
  3716. HANDLE_OP_END();
  3717. }
  3718. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST)
  3719. {
  3720. local_offset = *frame_ip++;
  3721. if (local_offset & 0x80)
  3722. PUT_I64_TO_ADDR(
  3723. (uint32 *)(frame_lp + (local_offset & 0x7F)),
  3724. GET_I64_FROM_ADDR(frame_sp - 2));
  3725. else
  3726. *(int32 *)(frame_lp + local_offset) =
  3727. *(int32 *)(frame_sp - 1);
  3728. HANDLE_OP_END();
  3729. }
  3730. HANDLE_OP(WASM_OP_GET_GLOBAL)
  3731. {
  3732. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  3733. bh_assert(global_idx < module->e->global_count);
  3734. global = globals + global_idx;
  3735. global_addr = get_global_addr(global_data, global);
  3736. /* clang-format off */
  3737. #if WASM_ENABLE_GC == 0
  3738. PUSH_I32(*(uint32 *)global_addr);
  3739. #else
  3740. if (!wasm_is_type_reftype(global->type)) {
  3741. PUSH_I32(*(uint32 *)global_addr);
  3742. }
  3743. else if (wasm_is_reftype_i31ref(global->type)) {
  3744. PUSH_I31REF(GET_REF_FROM_ADDR((uint32 *)global_addr));
  3745. }
  3746. else {
  3747. PUSH_REF(GET_REF_FROM_ADDR((uint32 *)global_addr));
  3748. }
  3749. #endif
  3750. /* clang-format on */
  3751. HANDLE_OP_END();
  3752. }
  3753. HANDLE_OP(WASM_OP_GET_GLOBAL_64)
  3754. {
  3755. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  3756. bh_assert(global_idx < module->e->global_count);
  3757. global = globals + global_idx;
  3758. global_addr = get_global_addr(global_data, global);
  3759. PUSH_I64(GET_I64_FROM_ADDR((uint32 *)global_addr));
  3760. HANDLE_OP_END();
  3761. }
  3762. HANDLE_OP(WASM_OP_SET_GLOBAL)
  3763. {
  3764. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  3765. bh_assert(global_idx < module->e->global_count);
  3766. global = globals + global_idx;
  3767. global_addr = get_global_addr(global_data, global);
  3768. /* clang-format off */
  3769. #if WASM_ENABLE_GC == 0
  3770. *(int32 *)global_addr = POP_I32();
  3771. #else
  3772. if (!wasm_is_type_reftype(global->type))
  3773. *(int32 *)global_addr = POP_I32();
  3774. else
  3775. PUT_REF_TO_ADDR((uint32 *)global_addr, POP_REF());
  3776. #endif
  3777. /* clang-format on */
  3778. HANDLE_OP_END();
  3779. }
  3780. HANDLE_OP(WASM_OP_SET_GLOBAL_AUX_STACK)
  3781. {
  3782. uint64 aux_stack_top;
  3783. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  3784. bh_assert(global_idx < module->e->global_count);
  3785. global = globals + global_idx;
  3786. global_addr = get_global_addr(global_data, global);
  3787. #if WASM_ENABLE_MEMORY64 != 0
  3788. if (is_memory64) {
  3789. aux_stack_top = *(uint64 *)(frame_sp - 2);
  3790. }
  3791. else
  3792. #endif
  3793. {
  3794. aux_stack_top = (uint64)(*(uint32 *)(frame_sp - 1));
  3795. }
  3796. if (aux_stack_top <= (uint64)exec_env->aux_stack_boundary) {
  3797. wasm_set_exception(module, "wasm auxiliary stack overflow");
  3798. goto got_exception;
  3799. }
  3800. if (aux_stack_top > (uint64)exec_env->aux_stack_bottom) {
  3801. wasm_set_exception(module,
  3802. "wasm auxiliary stack underflow");
  3803. goto got_exception;
  3804. }
  3805. #if WASM_ENABLE_MEMORY64 != 0
  3806. if (is_memory64) {
  3807. *(uint64 *)global_addr = aux_stack_top;
  3808. frame_sp -= 2;
  3809. }
  3810. else
  3811. #endif
  3812. {
  3813. *(uint32 *)global_addr = (uint32)aux_stack_top;
  3814. frame_sp--;
  3815. }
  3816. #if WASM_ENABLE_MEMORY_PROFILING != 0
  3817. if (module->module->aux_stack_top_global_index != (uint32)-1) {
  3818. uint32 aux_stack_used =
  3819. (uint32)(module->module->aux_stack_bottom
  3820. - *(uint32 *)global_addr);
  3821. if (aux_stack_used > module->e->max_aux_stack_used)
  3822. module->e->max_aux_stack_used = aux_stack_used;
  3823. }
  3824. #endif
  3825. HANDLE_OP_END();
  3826. }
  3827. HANDLE_OP(WASM_OP_SET_GLOBAL_64)
  3828. {
  3829. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  3830. bh_assert(global_idx < module->e->global_count);
  3831. global = globals + global_idx;
  3832. global_addr = get_global_addr(global_data, global);
  3833. PUT_I64_TO_ADDR((uint32 *)global_addr, POP_I64());
  3834. HANDLE_OP_END();
  3835. }
  3836. /* memory load instructions */
  3837. HANDLE_OP(WASM_OP_I32_LOAD)
  3838. HANDLE_OP(WASM_OP_F32_LOAD)
  3839. {
  3840. uint32 flags;
  3841. mem_offset_t offset, addr;
  3842. read_leb_uint32(frame_ip, frame_ip_end, flags);
  3843. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3844. addr = POP_MEM_OFFSET();
  3845. CHECK_MEMORY_OVERFLOW(4);
  3846. PUSH_I32(LOAD_I32(maddr));
  3847. CHECK_READ_WATCHPOINT(addr, offset);
  3848. (void)flags;
  3849. HANDLE_OP_END();
  3850. }
  3851. HANDLE_OP(WASM_OP_I64_LOAD)
  3852. HANDLE_OP(WASM_OP_F64_LOAD)
  3853. {
  3854. uint32 flags;
  3855. mem_offset_t offset, addr;
  3856. read_leb_uint32(frame_ip, frame_ip_end, flags);
  3857. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3858. addr = POP_MEM_OFFSET();
  3859. CHECK_MEMORY_OVERFLOW(8);
  3860. PUSH_I64(LOAD_I64(maddr));
  3861. CHECK_READ_WATCHPOINT(addr, offset);
  3862. (void)flags;
  3863. HANDLE_OP_END();
  3864. }
  3865. HANDLE_OP(WASM_OP_I32_LOAD8_S)
  3866. {
  3867. uint32 flags;
  3868. mem_offset_t offset, addr;
  3869. read_leb_uint32(frame_ip, frame_ip_end, flags);
  3870. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3871. addr = POP_MEM_OFFSET();
  3872. CHECK_MEMORY_OVERFLOW(1);
  3873. PUSH_I32(sign_ext_8_32(*(int8 *)maddr));
  3874. CHECK_READ_WATCHPOINT(addr, offset);
  3875. (void)flags;
  3876. HANDLE_OP_END();
  3877. }
  3878. HANDLE_OP(WASM_OP_I32_LOAD8_U)
  3879. {
  3880. uint32 flags;
  3881. mem_offset_t offset, addr;
  3882. read_leb_uint32(frame_ip, frame_ip_end, flags);
  3883. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3884. addr = POP_MEM_OFFSET();
  3885. CHECK_MEMORY_OVERFLOW(1);
  3886. PUSH_I32((uint32)(*(uint8 *)maddr));
  3887. CHECK_READ_WATCHPOINT(addr, offset);
  3888. (void)flags;
  3889. HANDLE_OP_END();
  3890. }
  3891. HANDLE_OP(WASM_OP_I32_LOAD16_S)
  3892. {
  3893. uint32 flags;
  3894. mem_offset_t offset, addr;
  3895. read_leb_uint32(frame_ip, frame_ip_end, flags);
  3896. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3897. addr = POP_MEM_OFFSET();
  3898. CHECK_MEMORY_OVERFLOW(2);
  3899. PUSH_I32(sign_ext_16_32(LOAD_I16(maddr)));
  3900. CHECK_READ_WATCHPOINT(addr, offset);
  3901. (void)flags;
  3902. HANDLE_OP_END();
  3903. }
  3904. HANDLE_OP(WASM_OP_I32_LOAD16_U)
  3905. {
  3906. uint32 flags;
  3907. mem_offset_t offset, addr;
  3908. read_leb_uint32(frame_ip, frame_ip_end, flags);
  3909. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3910. addr = POP_MEM_OFFSET();
  3911. CHECK_MEMORY_OVERFLOW(2);
  3912. PUSH_I32((uint32)(LOAD_U16(maddr)));
  3913. CHECK_READ_WATCHPOINT(addr, offset);
  3914. (void)flags;
  3915. HANDLE_OP_END();
  3916. }
  3917. HANDLE_OP(WASM_OP_I64_LOAD8_S)
  3918. {
  3919. uint32 flags;
  3920. mem_offset_t offset, addr;
  3921. read_leb_uint32(frame_ip, frame_ip_end, flags);
  3922. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3923. addr = POP_MEM_OFFSET();
  3924. CHECK_MEMORY_OVERFLOW(1);
  3925. PUSH_I64(sign_ext_8_64(*(int8 *)maddr));
  3926. CHECK_READ_WATCHPOINT(addr, offset);
  3927. (void)flags;
  3928. HANDLE_OP_END();
  3929. }
  3930. HANDLE_OP(WASM_OP_I64_LOAD8_U)
  3931. {
  3932. uint32 flags;
  3933. mem_offset_t offset, addr;
  3934. read_leb_uint32(frame_ip, frame_ip_end, flags);
  3935. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3936. addr = POP_MEM_OFFSET();
  3937. CHECK_MEMORY_OVERFLOW(1);
  3938. PUSH_I64((uint64)(*(uint8 *)maddr));
  3939. CHECK_READ_WATCHPOINT(addr, offset);
  3940. (void)flags;
  3941. HANDLE_OP_END();
  3942. }
  3943. HANDLE_OP(WASM_OP_I64_LOAD16_S)
  3944. {
  3945. uint32 flags;
  3946. mem_offset_t offset, addr;
  3947. read_leb_uint32(frame_ip, frame_ip_end, flags);
  3948. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3949. addr = POP_MEM_OFFSET();
  3950. CHECK_MEMORY_OVERFLOW(2);
  3951. PUSH_I64(sign_ext_16_64(LOAD_I16(maddr)));
  3952. CHECK_READ_WATCHPOINT(addr, offset);
  3953. (void)flags;
  3954. HANDLE_OP_END();
  3955. }
  3956. HANDLE_OP(WASM_OP_I64_LOAD16_U)
  3957. {
  3958. uint32 flags;
  3959. mem_offset_t offset, addr;
  3960. read_leb_uint32(frame_ip, frame_ip_end, flags);
  3961. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3962. addr = POP_MEM_OFFSET();
  3963. CHECK_MEMORY_OVERFLOW(2);
  3964. PUSH_I64((uint64)(LOAD_U16(maddr)));
  3965. CHECK_READ_WATCHPOINT(addr, offset);
  3966. (void)flags;
  3967. HANDLE_OP_END();
  3968. }
  3969. HANDLE_OP(WASM_OP_I64_LOAD32_S)
  3970. {
  3971. uint32 flags;
  3972. mem_offset_t offset, addr;
  3973. opcode = *(frame_ip - 1);
  3974. read_leb_uint32(frame_ip, frame_ip_end, flags);
  3975. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3976. addr = POP_MEM_OFFSET();
  3977. CHECK_MEMORY_OVERFLOW(4);
  3978. PUSH_I64(sign_ext_32_64(LOAD_I32(maddr)));
  3979. CHECK_READ_WATCHPOINT(addr, offset);
  3980. (void)flags;
  3981. HANDLE_OP_END();
  3982. }
  3983. HANDLE_OP(WASM_OP_I64_LOAD32_U)
  3984. {
  3985. uint32 flags;
  3986. mem_offset_t offset, addr;
  3987. read_leb_uint32(frame_ip, frame_ip_end, flags);
  3988. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3989. addr = POP_MEM_OFFSET();
  3990. CHECK_MEMORY_OVERFLOW(4);
  3991. PUSH_I64((uint64)(LOAD_U32(maddr)));
  3992. CHECK_READ_WATCHPOINT(addr, offset);
  3993. (void)flags;
  3994. HANDLE_OP_END();
  3995. }
  3996. /* memory store instructions */
  3997. HANDLE_OP(WASM_OP_I32_STORE)
  3998. HANDLE_OP(WASM_OP_F32_STORE)
  3999. {
  4000. uint32 flags;
  4001. mem_offset_t offset, addr;
  4002. read_leb_uint32(frame_ip, frame_ip_end, flags);
  4003. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4004. frame_sp--;
  4005. addr = POP_MEM_OFFSET();
  4006. CHECK_MEMORY_OVERFLOW(4);
  4007. #if WASM_ENABLE_MEMORY64 != 0
  4008. if (is_memory64) {
  4009. STORE_U32(maddr, frame_sp[2]);
  4010. }
  4011. else
  4012. #endif
  4013. {
  4014. STORE_U32(maddr, frame_sp[1]);
  4015. }
  4016. CHECK_WRITE_WATCHPOINT(addr, offset);
  4017. (void)flags;
  4018. HANDLE_OP_END();
  4019. }
  4020. HANDLE_OP(WASM_OP_I64_STORE)
  4021. HANDLE_OP(WASM_OP_F64_STORE)
  4022. {
  4023. uint32 flags;
  4024. mem_offset_t offset, addr;
  4025. read_leb_uint32(frame_ip, frame_ip_end, flags);
  4026. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4027. frame_sp -= 2;
  4028. addr = POP_MEM_OFFSET();
  4029. CHECK_MEMORY_OVERFLOW(8);
  4030. #if WASM_ENABLE_MEMORY64 != 0
  4031. if (is_memory64) {
  4032. PUT_I64_TO_ADDR((mem_offset_t *)maddr,
  4033. GET_I64_FROM_ADDR(frame_sp + 2));
  4034. }
  4035. else
  4036. #endif
  4037. {
  4038. PUT_I64_TO_ADDR((uint32 *)maddr,
  4039. GET_I64_FROM_ADDR(frame_sp + 1));
  4040. }
  4041. CHECK_WRITE_WATCHPOINT(addr, offset);
  4042. (void)flags;
  4043. HANDLE_OP_END();
  4044. }
  4045. HANDLE_OP(WASM_OP_I32_STORE8)
  4046. HANDLE_OP(WASM_OP_I32_STORE16)
  4047. {
  4048. uint32 flags;
  4049. mem_offset_t offset, addr;
  4050. uint32 sval;
  4051. opcode = *(frame_ip - 1);
  4052. read_leb_uint32(frame_ip, frame_ip_end, flags);
  4053. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4054. sval = (uint32)POP_I32();
  4055. addr = POP_MEM_OFFSET();
  4056. if (opcode == WASM_OP_I32_STORE8) {
  4057. CHECK_MEMORY_OVERFLOW(1);
  4058. *(uint8 *)maddr = (uint8)sval;
  4059. }
  4060. else {
  4061. CHECK_MEMORY_OVERFLOW(2);
  4062. STORE_U16(maddr, (uint16)sval);
  4063. }
  4064. CHECK_WRITE_WATCHPOINT(addr, offset);
  4065. (void)flags;
  4066. HANDLE_OP_END();
  4067. }
  4068. HANDLE_OP(WASM_OP_I64_STORE8)
  4069. HANDLE_OP(WASM_OP_I64_STORE16)
  4070. HANDLE_OP(WASM_OP_I64_STORE32)
  4071. {
  4072. uint32 flags;
  4073. mem_offset_t offset, addr;
  4074. uint64 sval;
  4075. opcode = *(frame_ip - 1);
  4076. read_leb_uint32(frame_ip, frame_ip_end, flags);
  4077. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4078. sval = (uint64)POP_I64();
  4079. addr = POP_MEM_OFFSET();
  4080. if (opcode == WASM_OP_I64_STORE8) {
  4081. CHECK_MEMORY_OVERFLOW(1);
  4082. *(uint8 *)maddr = (uint8)sval;
  4083. }
  4084. else if (opcode == WASM_OP_I64_STORE16) {
  4085. CHECK_MEMORY_OVERFLOW(2);
  4086. STORE_U16(maddr, (uint16)sval);
  4087. }
  4088. else {
  4089. CHECK_MEMORY_OVERFLOW(4);
  4090. STORE_U32(maddr, (uint32)sval);
  4091. }
  4092. CHECK_WRITE_WATCHPOINT(addr, offset);
  4093. (void)flags;
  4094. HANDLE_OP_END();
  4095. }
  4096. /* memory size and memory grow instructions */
  4097. HANDLE_OP(WASM_OP_MEMORY_SIZE)
  4098. {
  4099. uint32 reserved;
  4100. read_leb_uint32(frame_ip, frame_ip_end, reserved);
  4101. PUSH_PAGE_COUNT(memory->cur_page_count);
  4102. (void)reserved;
  4103. HANDLE_OP_END();
  4104. }
  4105. HANDLE_OP(WASM_OP_MEMORY_GROW)
  4106. {
  4107. uint32 reserved, delta,
  4108. prev_page_count = memory->cur_page_count;
  4109. read_leb_uint32(frame_ip, frame_ip_end, reserved);
  4110. delta = (uint32)POP_PAGE_COUNT();
  4111. if (!wasm_enlarge_memory(module, delta)) {
  4112. /* failed to memory.grow, return -1 */
  4113. PUSH_PAGE_COUNT(-1);
  4114. }
  4115. else {
  4116. /* success, return previous page count */
  4117. PUSH_PAGE_COUNT(prev_page_count);
  4118. /* update memory size, no need to update memory ptr as
  4119. it isn't changed in wasm_enlarge_memory */
  4120. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  4121. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  4122. || WASM_ENABLE_BULK_MEMORY != 0
  4123. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
  4124. #endif
  4125. }
  4126. (void)reserved;
  4127. HANDLE_OP_END();
  4128. }
  4129. /* constant instructions */
  4130. HANDLE_OP(WASM_OP_I32_CONST)
  4131. DEF_OP_I_CONST(int32, I32);
  4132. HANDLE_OP_END();
  4133. HANDLE_OP(WASM_OP_I64_CONST)
  4134. DEF_OP_I_CONST(int64, I64);
  4135. HANDLE_OP_END();
  4136. HANDLE_OP(WASM_OP_F32_CONST)
  4137. {
  4138. uint8 *p_float = (uint8 *)frame_sp++;
  4139. for (i = 0; i < sizeof(float32); i++)
  4140. *p_float++ = *frame_ip++;
  4141. HANDLE_OP_END();
  4142. }
  4143. HANDLE_OP(WASM_OP_F64_CONST)
  4144. {
  4145. uint8 *p_float = (uint8 *)frame_sp++;
  4146. frame_sp++;
  4147. for (i = 0; i < sizeof(float64); i++)
  4148. *p_float++ = *frame_ip++;
  4149. HANDLE_OP_END();
  4150. }
  4151. /* comparison instructions of i32 */
  4152. HANDLE_OP(WASM_OP_I32_EQZ)
  4153. {
  4154. DEF_OP_EQZ(I32);
  4155. HANDLE_OP_END();
  4156. }
  4157. HANDLE_OP(WASM_OP_I32_EQ)
  4158. {
  4159. DEF_OP_CMP(uint32, I32, ==);
  4160. HANDLE_OP_END();
  4161. }
  4162. HANDLE_OP(WASM_OP_I32_NE)
  4163. {
  4164. DEF_OP_CMP(uint32, I32, !=);
  4165. HANDLE_OP_END();
  4166. }
  4167. HANDLE_OP(WASM_OP_I32_LT_S)
  4168. {
  4169. DEF_OP_CMP(int32, I32, <);
  4170. HANDLE_OP_END();
  4171. }
  4172. HANDLE_OP(WASM_OP_I32_LT_U)
  4173. {
  4174. DEF_OP_CMP(uint32, I32, <);
  4175. HANDLE_OP_END();
  4176. }
  4177. HANDLE_OP(WASM_OP_I32_GT_S)
  4178. {
  4179. DEF_OP_CMP(int32, I32, >);
  4180. HANDLE_OP_END();
  4181. }
  4182. HANDLE_OP(WASM_OP_I32_GT_U)
  4183. {
  4184. DEF_OP_CMP(uint32, I32, >);
  4185. HANDLE_OP_END();
  4186. }
  4187. HANDLE_OP(WASM_OP_I32_LE_S)
  4188. {
  4189. DEF_OP_CMP(int32, I32, <=);
  4190. HANDLE_OP_END();
  4191. }
  4192. HANDLE_OP(WASM_OP_I32_LE_U)
  4193. {
  4194. DEF_OP_CMP(uint32, I32, <=);
  4195. HANDLE_OP_END();
  4196. }
  4197. HANDLE_OP(WASM_OP_I32_GE_S)
  4198. {
  4199. DEF_OP_CMP(int32, I32, >=);
  4200. HANDLE_OP_END();
  4201. }
  4202. HANDLE_OP(WASM_OP_I32_GE_U)
  4203. {
  4204. DEF_OP_CMP(uint32, I32, >=);
  4205. HANDLE_OP_END();
  4206. }
  4207. /* comparison instructions of i64 */
  4208. HANDLE_OP(WASM_OP_I64_EQZ)
  4209. {
  4210. DEF_OP_EQZ(I64);
  4211. HANDLE_OP_END();
  4212. }
  4213. HANDLE_OP(WASM_OP_I64_EQ)
  4214. {
  4215. DEF_OP_CMP(uint64, I64, ==);
  4216. HANDLE_OP_END();
  4217. }
  4218. HANDLE_OP(WASM_OP_I64_NE)
  4219. {
  4220. DEF_OP_CMP(uint64, I64, !=);
  4221. HANDLE_OP_END();
  4222. }
  4223. HANDLE_OP(WASM_OP_I64_LT_S)
  4224. {
  4225. DEF_OP_CMP(int64, I64, <);
  4226. HANDLE_OP_END();
  4227. }
  4228. HANDLE_OP(WASM_OP_I64_LT_U)
  4229. {
  4230. DEF_OP_CMP(uint64, I64, <);
  4231. HANDLE_OP_END();
  4232. }
  4233. HANDLE_OP(WASM_OP_I64_GT_S)
  4234. {
  4235. DEF_OP_CMP(int64, I64, >);
  4236. HANDLE_OP_END();
  4237. }
  4238. HANDLE_OP(WASM_OP_I64_GT_U)
  4239. {
  4240. DEF_OP_CMP(uint64, I64, >);
  4241. HANDLE_OP_END();
  4242. }
  4243. HANDLE_OP(WASM_OP_I64_LE_S)
  4244. {
  4245. DEF_OP_CMP(int64, I64, <=);
  4246. HANDLE_OP_END();
  4247. }
  4248. HANDLE_OP(WASM_OP_I64_LE_U)
  4249. {
  4250. DEF_OP_CMP(uint64, I64, <=);
  4251. HANDLE_OP_END();
  4252. }
  4253. HANDLE_OP(WASM_OP_I64_GE_S)
  4254. {
  4255. DEF_OP_CMP(int64, I64, >=);
  4256. HANDLE_OP_END();
  4257. }
  4258. HANDLE_OP(WASM_OP_I64_GE_U)
  4259. {
  4260. DEF_OP_CMP(uint64, I64, >=);
  4261. HANDLE_OP_END();
  4262. }
  4263. /* comparison instructions of f32 */
  4264. HANDLE_OP(WASM_OP_F32_EQ)
  4265. {
  4266. DEF_OP_CMP(float32, F32, ==);
  4267. HANDLE_OP_END();
  4268. }
  4269. HANDLE_OP(WASM_OP_F32_NE)
  4270. {
  4271. DEF_OP_CMP(float32, F32, !=);
  4272. HANDLE_OP_END();
  4273. }
  4274. HANDLE_OP(WASM_OP_F32_LT)
  4275. {
  4276. DEF_OP_CMP(float32, F32, <);
  4277. HANDLE_OP_END();
  4278. }
  4279. HANDLE_OP(WASM_OP_F32_GT)
  4280. {
  4281. DEF_OP_CMP(float32, F32, >);
  4282. HANDLE_OP_END();
  4283. }
  4284. HANDLE_OP(WASM_OP_F32_LE)
  4285. {
  4286. DEF_OP_CMP(float32, F32, <=);
  4287. HANDLE_OP_END();
  4288. }
  4289. HANDLE_OP(WASM_OP_F32_GE)
  4290. {
  4291. DEF_OP_CMP(float32, F32, >=);
  4292. HANDLE_OP_END();
  4293. }
  4294. /* comparison instructions of f64 */
  4295. HANDLE_OP(WASM_OP_F64_EQ)
  4296. {
  4297. DEF_OP_CMP(float64, F64, ==);
  4298. HANDLE_OP_END();
  4299. }
  4300. HANDLE_OP(WASM_OP_F64_NE)
  4301. {
  4302. DEF_OP_CMP(float64, F64, !=);
  4303. HANDLE_OP_END();
  4304. }
  4305. HANDLE_OP(WASM_OP_F64_LT)
  4306. {
  4307. DEF_OP_CMP(float64, F64, <);
  4308. HANDLE_OP_END();
  4309. }
  4310. HANDLE_OP(WASM_OP_F64_GT)
  4311. {
  4312. DEF_OP_CMP(float64, F64, >);
  4313. HANDLE_OP_END();
  4314. }
  4315. HANDLE_OP(WASM_OP_F64_LE)
  4316. {
  4317. DEF_OP_CMP(float64, F64, <=);
  4318. HANDLE_OP_END();
  4319. }
  4320. HANDLE_OP(WASM_OP_F64_GE)
  4321. {
  4322. DEF_OP_CMP(float64, F64, >=);
  4323. HANDLE_OP_END();
  4324. }
  4325. /* numberic instructions of i32 */
  4326. HANDLE_OP(WASM_OP_I32_CLZ)
  4327. {
  4328. DEF_OP_BIT_COUNT(uint32, I32, clz32);
  4329. HANDLE_OP_END();
  4330. }
  4331. HANDLE_OP(WASM_OP_I32_CTZ)
  4332. {
  4333. DEF_OP_BIT_COUNT(uint32, I32, ctz32);
  4334. HANDLE_OP_END();
  4335. }
  4336. HANDLE_OP(WASM_OP_I32_POPCNT)
  4337. {
  4338. DEF_OP_BIT_COUNT(uint32, I32, popcount32);
  4339. HANDLE_OP_END();
  4340. }
  4341. HANDLE_OP(WASM_OP_I32_ADD)
  4342. {
  4343. DEF_OP_NUMERIC(uint32, uint32, I32, +);
  4344. HANDLE_OP_END();
  4345. }
  4346. HANDLE_OP(WASM_OP_I32_SUB)
  4347. {
  4348. DEF_OP_NUMERIC(uint32, uint32, I32, -);
  4349. HANDLE_OP_END();
  4350. }
  4351. HANDLE_OP(WASM_OP_I32_MUL)
  4352. {
  4353. DEF_OP_NUMERIC(uint32, uint32, I32, *);
  4354. HANDLE_OP_END();
  4355. }
  4356. HANDLE_OP(WASM_OP_I32_DIV_S)
  4357. {
  4358. int32 a, b;
  4359. b = POP_I32();
  4360. a = POP_I32();
  4361. if (a == (int32)0x80000000 && b == -1) {
  4362. wasm_set_exception(module, "integer overflow");
  4363. goto got_exception;
  4364. }
  4365. if (b == 0) {
  4366. wasm_set_exception(module, "integer divide by zero");
  4367. goto got_exception;
  4368. }
  4369. PUSH_I32(a / b);
  4370. HANDLE_OP_END();
  4371. }
  4372. HANDLE_OP(WASM_OP_I32_DIV_U)
  4373. {
  4374. uint32 a, b;
  4375. b = (uint32)POP_I32();
  4376. a = (uint32)POP_I32();
  4377. if (b == 0) {
  4378. wasm_set_exception(module, "integer divide by zero");
  4379. goto got_exception;
  4380. }
  4381. PUSH_I32(a / b);
  4382. HANDLE_OP_END();
  4383. }
  4384. HANDLE_OP(WASM_OP_I32_REM_S)
  4385. {
  4386. int32 a, b;
  4387. b = POP_I32();
  4388. a = POP_I32();
  4389. if (a == (int32)0x80000000 && b == -1) {
  4390. PUSH_I32(0);
  4391. HANDLE_OP_END();
  4392. }
  4393. if (b == 0) {
  4394. wasm_set_exception(module, "integer divide by zero");
  4395. goto got_exception;
  4396. }
  4397. PUSH_I32(a % b);
  4398. HANDLE_OP_END();
  4399. }
  4400. HANDLE_OP(WASM_OP_I32_REM_U)
  4401. {
  4402. uint32 a, b;
  4403. b = (uint32)POP_I32();
  4404. a = (uint32)POP_I32();
  4405. if (b == 0) {
  4406. wasm_set_exception(module, "integer divide by zero");
  4407. goto got_exception;
  4408. }
  4409. PUSH_I32(a % b);
  4410. HANDLE_OP_END();
  4411. }
  4412. HANDLE_OP(WASM_OP_I32_AND)
  4413. {
  4414. DEF_OP_NUMERIC(uint32, uint32, I32, &);
  4415. HANDLE_OP_END();
  4416. }
  4417. HANDLE_OP(WASM_OP_I32_OR)
  4418. {
  4419. DEF_OP_NUMERIC(uint32, uint32, I32, |);
  4420. HANDLE_OP_END();
  4421. }
  4422. HANDLE_OP(WASM_OP_I32_XOR)
  4423. {
  4424. DEF_OP_NUMERIC(uint32, uint32, I32, ^);
  4425. HANDLE_OP_END();
  4426. }
  4427. HANDLE_OP(WASM_OP_I32_SHL)
  4428. {
  4429. DEF_OP_NUMERIC2(uint32, uint32, I32, <<);
  4430. HANDLE_OP_END();
  4431. }
  4432. HANDLE_OP(WASM_OP_I32_SHR_S)
  4433. {
  4434. DEF_OP_NUMERIC2(int32, uint32, I32, >>);
  4435. HANDLE_OP_END();
  4436. }
  4437. HANDLE_OP(WASM_OP_I32_SHR_U)
  4438. {
  4439. DEF_OP_NUMERIC2(uint32, uint32, I32, >>);
  4440. HANDLE_OP_END();
  4441. }
  4442. HANDLE_OP(WASM_OP_I32_ROTL)
  4443. {
  4444. uint32 a, b;
  4445. b = (uint32)POP_I32();
  4446. a = (uint32)POP_I32();
  4447. PUSH_I32(rotl32(a, b));
  4448. HANDLE_OP_END();
  4449. }
  4450. HANDLE_OP(WASM_OP_I32_ROTR)
  4451. {
  4452. uint32 a, b;
  4453. b = (uint32)POP_I32();
  4454. a = (uint32)POP_I32();
  4455. PUSH_I32(rotr32(a, b));
  4456. HANDLE_OP_END();
  4457. }
  4458. /* numberic instructions of i64 */
  4459. HANDLE_OP(WASM_OP_I64_CLZ)
  4460. {
  4461. DEF_OP_BIT_COUNT(uint64, I64, clz64);
  4462. HANDLE_OP_END();
  4463. }
  4464. HANDLE_OP(WASM_OP_I64_CTZ)
  4465. {
  4466. DEF_OP_BIT_COUNT(uint64, I64, ctz64);
  4467. HANDLE_OP_END();
  4468. }
  4469. HANDLE_OP(WASM_OP_I64_POPCNT)
  4470. {
  4471. DEF_OP_BIT_COUNT(uint64, I64, popcount64);
  4472. HANDLE_OP_END();
  4473. }
  4474. HANDLE_OP(WASM_OP_I64_ADD)
  4475. {
  4476. DEF_OP_NUMERIC_64(uint64, uint64, I64, +);
  4477. HANDLE_OP_END();
  4478. }
  4479. HANDLE_OP(WASM_OP_I64_SUB)
  4480. {
  4481. DEF_OP_NUMERIC_64(uint64, uint64, I64, -);
  4482. HANDLE_OP_END();
  4483. }
  4484. HANDLE_OP(WASM_OP_I64_MUL)
  4485. {
  4486. DEF_OP_NUMERIC_64(uint64, uint64, I64, *);
  4487. HANDLE_OP_END();
  4488. }
  4489. HANDLE_OP(WASM_OP_I64_DIV_S)
  4490. {
  4491. int64 a, b;
  4492. b = POP_I64();
  4493. a = POP_I64();
  4494. if (a == (int64)0x8000000000000000LL && b == -1) {
  4495. wasm_set_exception(module, "integer overflow");
  4496. goto got_exception;
  4497. }
  4498. if (b == 0) {
  4499. wasm_set_exception(module, "integer divide by zero");
  4500. goto got_exception;
  4501. }
  4502. PUSH_I64(a / b);
  4503. HANDLE_OP_END();
  4504. }
  4505. HANDLE_OP(WASM_OP_I64_DIV_U)
  4506. {
  4507. uint64 a, b;
  4508. b = (uint64)POP_I64();
  4509. a = (uint64)POP_I64();
  4510. if (b == 0) {
  4511. wasm_set_exception(module, "integer divide by zero");
  4512. goto got_exception;
  4513. }
  4514. PUSH_I64(a / b);
  4515. HANDLE_OP_END();
  4516. }
  4517. HANDLE_OP(WASM_OP_I64_REM_S)
  4518. {
  4519. int64 a, b;
  4520. b = POP_I64();
  4521. a = POP_I64();
  4522. if (a == (int64)0x8000000000000000LL && b == -1) {
  4523. PUSH_I64(0);
  4524. HANDLE_OP_END();
  4525. }
  4526. if (b == 0) {
  4527. wasm_set_exception(module, "integer divide by zero");
  4528. goto got_exception;
  4529. }
  4530. PUSH_I64(a % b);
  4531. HANDLE_OP_END();
  4532. }
  4533. HANDLE_OP(WASM_OP_I64_REM_U)
  4534. {
  4535. uint64 a, b;
  4536. b = (uint64)POP_I64();
  4537. a = (uint64)POP_I64();
  4538. if (b == 0) {
  4539. wasm_set_exception(module, "integer divide by zero");
  4540. goto got_exception;
  4541. }
  4542. PUSH_I64(a % b);
  4543. HANDLE_OP_END();
  4544. }
  4545. HANDLE_OP(WASM_OP_I64_AND)
  4546. {
  4547. DEF_OP_NUMERIC_64(uint64, uint64, I64, &);
  4548. HANDLE_OP_END();
  4549. }
  4550. HANDLE_OP(WASM_OP_I64_OR)
  4551. {
  4552. DEF_OP_NUMERIC_64(uint64, uint64, I64, |);
  4553. HANDLE_OP_END();
  4554. }
  4555. HANDLE_OP(WASM_OP_I64_XOR)
  4556. {
  4557. DEF_OP_NUMERIC_64(uint64, uint64, I64, ^);
  4558. HANDLE_OP_END();
  4559. }
  4560. HANDLE_OP(WASM_OP_I64_SHL)
  4561. {
  4562. DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<);
  4563. HANDLE_OP_END();
  4564. }
  4565. HANDLE_OP(WASM_OP_I64_SHR_S)
  4566. {
  4567. DEF_OP_NUMERIC2_64(int64, uint64, I64, >>);
  4568. HANDLE_OP_END();
  4569. }
  4570. HANDLE_OP(WASM_OP_I64_SHR_U)
  4571. {
  4572. DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>);
  4573. HANDLE_OP_END();
  4574. }
  4575. HANDLE_OP(WASM_OP_I64_ROTL)
  4576. {
  4577. uint64 a, b;
  4578. b = (uint64)POP_I64();
  4579. a = (uint64)POP_I64();
  4580. PUSH_I64(rotl64(a, b));
  4581. HANDLE_OP_END();
  4582. }
  4583. HANDLE_OP(WASM_OP_I64_ROTR)
  4584. {
  4585. uint64 a, b;
  4586. b = (uint64)POP_I64();
  4587. a = (uint64)POP_I64();
  4588. PUSH_I64(rotr64(a, b));
  4589. HANDLE_OP_END();
  4590. }
  4591. /* numberic instructions of f32 */
  4592. HANDLE_OP(WASM_OP_F32_ABS)
  4593. {
  4594. DEF_OP_MATH(float32, F32, fabsf);
  4595. HANDLE_OP_END();
  4596. }
  4597. HANDLE_OP(WASM_OP_F32_NEG)
  4598. {
  4599. uint32 u32 = frame_sp[-1];
  4600. uint32 sign_bit = u32 & ((uint32)1 << 31);
  4601. if (sign_bit)
  4602. frame_sp[-1] = u32 & ~((uint32)1 << 31);
  4603. else
  4604. frame_sp[-1] = u32 | ((uint32)1 << 31);
  4605. HANDLE_OP_END();
  4606. }
  4607. HANDLE_OP(WASM_OP_F32_CEIL)
  4608. {
  4609. DEF_OP_MATH(float32, F32, ceilf);
  4610. HANDLE_OP_END();
  4611. }
  4612. HANDLE_OP(WASM_OP_F32_FLOOR)
  4613. {
  4614. DEF_OP_MATH(float32, F32, floorf);
  4615. HANDLE_OP_END();
  4616. }
  4617. HANDLE_OP(WASM_OP_F32_TRUNC)
  4618. {
  4619. DEF_OP_MATH(float32, F32, truncf);
  4620. HANDLE_OP_END();
  4621. }
  4622. HANDLE_OP(WASM_OP_F32_NEAREST)
  4623. {
  4624. DEF_OP_MATH(float32, F32, rintf);
  4625. HANDLE_OP_END();
  4626. }
  4627. HANDLE_OP(WASM_OP_F32_SQRT)
  4628. {
  4629. DEF_OP_MATH(float32, F32, sqrtf);
  4630. HANDLE_OP_END();
  4631. }
  4632. HANDLE_OP(WASM_OP_F32_ADD)
  4633. {
  4634. DEF_OP_NUMERIC(float32, float32, F32, +);
  4635. HANDLE_OP_END();
  4636. }
  4637. HANDLE_OP(WASM_OP_F32_SUB)
  4638. {
  4639. DEF_OP_NUMERIC(float32, float32, F32, -);
  4640. HANDLE_OP_END();
  4641. }
  4642. HANDLE_OP(WASM_OP_F32_MUL)
  4643. {
  4644. DEF_OP_NUMERIC(float32, float32, F32, *);
  4645. HANDLE_OP_END();
  4646. }
  4647. HANDLE_OP(WASM_OP_F32_DIV)
  4648. {
  4649. DEF_OP_NUMERIC(float32, float32, F32, /);
  4650. HANDLE_OP_END();
  4651. }
  4652. HANDLE_OP(WASM_OP_F32_MIN)
  4653. {
  4654. float32 a, b;
  4655. b = POP_F32();
  4656. a = POP_F32();
  4657. PUSH_F32(f32_min(a, b));
  4658. HANDLE_OP_END();
  4659. }
  4660. HANDLE_OP(WASM_OP_F32_MAX)
  4661. {
  4662. float32 a, b;
  4663. b = POP_F32();
  4664. a = POP_F32();
  4665. PUSH_F32(f32_max(a, b));
  4666. HANDLE_OP_END();
  4667. }
  4668. HANDLE_OP(WASM_OP_F32_COPYSIGN)
  4669. {
  4670. float32 a, b;
  4671. b = POP_F32();
  4672. a = POP_F32();
  4673. PUSH_F32(local_copysignf(a, b));
  4674. HANDLE_OP_END();
  4675. }
  4676. /* numberic instructions of f64 */
  4677. HANDLE_OP(WASM_OP_F64_ABS)
  4678. {
  4679. DEF_OP_MATH(float64, F64, fabs);
  4680. HANDLE_OP_END();
  4681. }
  4682. HANDLE_OP(WASM_OP_F64_NEG)
  4683. {
  4684. uint64 u64 = GET_I64_FROM_ADDR(frame_sp - 2);
  4685. uint64 sign_bit = u64 & (((uint64)1) << 63);
  4686. if (sign_bit)
  4687. PUT_I64_TO_ADDR(frame_sp - 2, (u64 & ~(((uint64)1) << 63)));
  4688. else
  4689. PUT_I64_TO_ADDR(frame_sp - 2, (u64 | (((uint64)1) << 63)));
  4690. HANDLE_OP_END();
  4691. }
  4692. HANDLE_OP(WASM_OP_F64_CEIL)
  4693. {
  4694. DEF_OP_MATH(float64, F64, ceil);
  4695. HANDLE_OP_END();
  4696. }
  4697. HANDLE_OP(WASM_OP_F64_FLOOR)
  4698. {
  4699. DEF_OP_MATH(float64, F64, floor);
  4700. HANDLE_OP_END();
  4701. }
  4702. HANDLE_OP(WASM_OP_F64_TRUNC)
  4703. {
  4704. DEF_OP_MATH(float64, F64, trunc);
  4705. HANDLE_OP_END();
  4706. }
  4707. HANDLE_OP(WASM_OP_F64_NEAREST)
  4708. {
  4709. DEF_OP_MATH(float64, F64, rint);
  4710. HANDLE_OP_END();
  4711. }
  4712. HANDLE_OP(WASM_OP_F64_SQRT)
  4713. {
  4714. DEF_OP_MATH(float64, F64, sqrt);
  4715. HANDLE_OP_END();
  4716. }
  4717. HANDLE_OP(WASM_OP_F64_ADD)
  4718. {
  4719. DEF_OP_NUMERIC_64(float64, float64, F64, +);
  4720. HANDLE_OP_END();
  4721. }
  4722. HANDLE_OP(WASM_OP_F64_SUB)
  4723. {
  4724. DEF_OP_NUMERIC_64(float64, float64, F64, -);
  4725. HANDLE_OP_END();
  4726. }
  4727. HANDLE_OP(WASM_OP_F64_MUL)
  4728. {
  4729. DEF_OP_NUMERIC_64(float64, float64, F64, *);
  4730. HANDLE_OP_END();
  4731. }
  4732. HANDLE_OP(WASM_OP_F64_DIV)
  4733. {
  4734. DEF_OP_NUMERIC_64(float64, float64, F64, /);
  4735. HANDLE_OP_END();
  4736. }
  4737. HANDLE_OP(WASM_OP_F64_MIN)
  4738. {
  4739. float64 a, b;
  4740. b = POP_F64();
  4741. a = POP_F64();
  4742. PUSH_F64(f64_min(a, b));
  4743. HANDLE_OP_END();
  4744. }
  4745. HANDLE_OP(WASM_OP_F64_MAX)
  4746. {
  4747. float64 a, b;
  4748. b = POP_F64();
  4749. a = POP_F64();
  4750. PUSH_F64(f64_max(a, b));
  4751. HANDLE_OP_END();
  4752. }
  4753. HANDLE_OP(WASM_OP_F64_COPYSIGN)
  4754. {
  4755. float64 a, b;
  4756. b = POP_F64();
  4757. a = POP_F64();
  4758. PUSH_F64(local_copysign(a, b));
  4759. HANDLE_OP_END();
  4760. }
  4761. /* conversions of i32 */
  4762. HANDLE_OP(WASM_OP_I32_WRAP_I64)
  4763. {
  4764. int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL);
  4765. PUSH_I32(value);
  4766. HANDLE_OP_END();
  4767. }
  4768. HANDLE_OP(WASM_OP_I32_TRUNC_S_F32)
  4769. {
  4770. /* We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX,
  4771. since float/double values of ieee754 cannot precisely
  4772. represent all int32/uint32/int64/uint64 values, e.g.
  4773. UINT32_MAX is 4294967295, but (float32)4294967295 is
  4774. 4294967296.0f, but not 4294967295.0f. */
  4775. DEF_OP_TRUNC_F32(-2147483904.0f, 2147483648.0f, true, true);
  4776. HANDLE_OP_END();
  4777. }
  4778. HANDLE_OP(WASM_OP_I32_TRUNC_U_F32)
  4779. {
  4780. DEF_OP_TRUNC_F32(-1.0f, 4294967296.0f, true, false);
  4781. HANDLE_OP_END();
  4782. }
  4783. HANDLE_OP(WASM_OP_I32_TRUNC_S_F64)
  4784. {
  4785. DEF_OP_TRUNC_F64(-2147483649.0, 2147483648.0, true, true);
  4786. /* frame_sp can't be moved in trunc function, we need to
  4787. manually adjust it if src and dst op's cell num is
  4788. different */
  4789. frame_sp--;
  4790. HANDLE_OP_END();
  4791. }
  4792. HANDLE_OP(WASM_OP_I32_TRUNC_U_F64)
  4793. {
  4794. DEF_OP_TRUNC_F64(-1.0, 4294967296.0, true, false);
  4795. frame_sp--;
  4796. HANDLE_OP_END();
  4797. }
  4798. /* conversions of i64 */
  4799. HANDLE_OP(WASM_OP_I64_EXTEND_S_I32)
  4800. {
  4801. DEF_OP_CONVERT(int64, I64, int32, I32);
  4802. HANDLE_OP_END();
  4803. }
  4804. HANDLE_OP(WASM_OP_I64_EXTEND_U_I32)
  4805. {
  4806. DEF_OP_CONVERT(int64, I64, uint32, I32);
  4807. HANDLE_OP_END();
  4808. }
  4809. HANDLE_OP(WASM_OP_I64_TRUNC_S_F32)
  4810. {
  4811. DEF_OP_TRUNC_F32(-9223373136366403584.0f,
  4812. 9223372036854775808.0f, false, true);
  4813. frame_sp++;
  4814. HANDLE_OP_END();
  4815. }
  4816. HANDLE_OP(WASM_OP_I64_TRUNC_U_F32)
  4817. {
  4818. DEF_OP_TRUNC_F32(-1.0f, 18446744073709551616.0f, false, false);
  4819. frame_sp++;
  4820. HANDLE_OP_END();
  4821. }
  4822. HANDLE_OP(WASM_OP_I64_TRUNC_S_F64)
  4823. {
  4824. DEF_OP_TRUNC_F64(-9223372036854777856.0, 9223372036854775808.0,
  4825. false, true);
  4826. HANDLE_OP_END();
  4827. }
  4828. HANDLE_OP(WASM_OP_I64_TRUNC_U_F64)
  4829. {
  4830. DEF_OP_TRUNC_F64(-1.0, 18446744073709551616.0, false, false);
  4831. HANDLE_OP_END();
  4832. }
  4833. /* conversions of f32 */
  4834. HANDLE_OP(WASM_OP_F32_CONVERT_S_I32)
  4835. {
  4836. DEF_OP_CONVERT(float32, F32, int32, I32);
  4837. HANDLE_OP_END();
  4838. }
  4839. HANDLE_OP(WASM_OP_F32_CONVERT_U_I32)
  4840. {
  4841. DEF_OP_CONVERT(float32, F32, uint32, I32);
  4842. HANDLE_OP_END();
  4843. }
  4844. HANDLE_OP(WASM_OP_F32_CONVERT_S_I64)
  4845. {
  4846. DEF_OP_CONVERT(float32, F32, int64, I64);
  4847. HANDLE_OP_END();
  4848. }
  4849. HANDLE_OP(WASM_OP_F32_CONVERT_U_I64)
  4850. {
  4851. DEF_OP_CONVERT(float32, F32, uint64, I64);
  4852. HANDLE_OP_END();
  4853. }
  4854. HANDLE_OP(WASM_OP_F32_DEMOTE_F64)
  4855. {
  4856. DEF_OP_CONVERT(float32, F32, float64, F64);
  4857. HANDLE_OP_END();
  4858. }
  4859. /* conversions of f64 */
  4860. HANDLE_OP(WASM_OP_F64_CONVERT_S_I32)
  4861. {
  4862. DEF_OP_CONVERT(float64, F64, int32, I32);
  4863. HANDLE_OP_END();
  4864. }
  4865. HANDLE_OP(WASM_OP_F64_CONVERT_U_I32)
  4866. {
  4867. DEF_OP_CONVERT(float64, F64, uint32, I32);
  4868. HANDLE_OP_END();
  4869. }
  4870. HANDLE_OP(WASM_OP_F64_CONVERT_S_I64)
  4871. {
  4872. DEF_OP_CONVERT(float64, F64, int64, I64);
  4873. HANDLE_OP_END();
  4874. }
  4875. HANDLE_OP(WASM_OP_F64_CONVERT_U_I64)
  4876. {
  4877. DEF_OP_CONVERT(float64, F64, uint64, I64);
  4878. HANDLE_OP_END();
  4879. }
  4880. HANDLE_OP(WASM_OP_F64_PROMOTE_F32)
  4881. {
  4882. DEF_OP_CONVERT(float64, F64, float32, F32);
  4883. HANDLE_OP_END();
  4884. }
  4885. /* reinterpretations */
  4886. HANDLE_OP(WASM_OP_I32_REINTERPRET_F32)
  4887. HANDLE_OP(WASM_OP_I64_REINTERPRET_F64)
  4888. HANDLE_OP(WASM_OP_F32_REINTERPRET_I32)
  4889. HANDLE_OP(WASM_OP_F64_REINTERPRET_I64) { HANDLE_OP_END(); }
  4890. HANDLE_OP(WASM_OP_I32_EXTEND8_S)
  4891. {
  4892. DEF_OP_CONVERT(int32, I32, int8, I32);
  4893. HANDLE_OP_END();
  4894. }
  4895. HANDLE_OP(WASM_OP_I32_EXTEND16_S)
  4896. {
  4897. DEF_OP_CONVERT(int32, I32, int16, I32);
  4898. HANDLE_OP_END();
  4899. }
  4900. HANDLE_OP(WASM_OP_I64_EXTEND8_S)
  4901. {
  4902. DEF_OP_CONVERT(int64, I64, int8, I64);
  4903. HANDLE_OP_END();
  4904. }
  4905. HANDLE_OP(WASM_OP_I64_EXTEND16_S)
  4906. {
  4907. DEF_OP_CONVERT(int64, I64, int16, I64);
  4908. HANDLE_OP_END();
  4909. }
  4910. HANDLE_OP(WASM_OP_I64_EXTEND32_S)
  4911. {
  4912. DEF_OP_CONVERT(int64, I64, int32, I64);
  4913. HANDLE_OP_END();
  4914. }
  4915. HANDLE_OP(WASM_OP_MISC_PREFIX)
  4916. {
  4917. uint32 opcode1;
  4918. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  4919. /* opcode1 was checked in loader and is no larger than
  4920. UINT8_MAX */
  4921. opcode = (uint8)opcode1;
  4922. switch (opcode) {
  4923. case WASM_OP_I32_TRUNC_SAT_S_F32:
  4924. DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f,
  4925. true, true);
  4926. break;
  4927. case WASM_OP_I32_TRUNC_SAT_U_F32:
  4928. DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, true, false);
  4929. break;
  4930. case WASM_OP_I32_TRUNC_SAT_S_F64:
  4931. DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, true,
  4932. true);
  4933. frame_sp--;
  4934. break;
  4935. case WASM_OP_I32_TRUNC_SAT_U_F64:
  4936. DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, true, false);
  4937. frame_sp--;
  4938. break;
  4939. case WASM_OP_I64_TRUNC_SAT_S_F32:
  4940. DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f,
  4941. 9223372036854775808.0f, false,
  4942. true);
  4943. frame_sp++;
  4944. break;
  4945. case WASM_OP_I64_TRUNC_SAT_U_F32:
  4946. DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f,
  4947. false, false);
  4948. frame_sp++;
  4949. break;
  4950. case WASM_OP_I64_TRUNC_SAT_S_F64:
  4951. DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0,
  4952. 9223372036854775808.0, false,
  4953. true);
  4954. break;
  4955. case WASM_OP_I64_TRUNC_SAT_U_F64:
  4956. DEF_OP_TRUNC_SAT_F64(-1.0f, 18446744073709551616.0,
  4957. false, false);
  4958. break;
  4959. #if WASM_ENABLE_BULK_MEMORY != 0
  4960. case WASM_OP_MEMORY_INIT:
  4961. {
  4962. uint32 segment;
  4963. mem_offset_t addr;
  4964. uint64 bytes, offset, seg_len;
  4965. uint8 *data;
  4966. read_leb_uint32(frame_ip, frame_ip_end, segment);
  4967. /* skip memory index */
  4968. frame_ip++;
  4969. bytes = (uint64)(uint32)POP_I32();
  4970. offset = (uint64)(uint32)POP_I32();
  4971. addr = (mem_offset_t)POP_MEM_OFFSET();
  4972. #if WASM_ENABLE_THREAD_MGR != 0
  4973. linear_mem_size = get_linear_mem_size();
  4974. #endif
  4975. #ifndef OS_ENABLE_HW_BOUND_CHECK
  4976. CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr);
  4977. #else
  4978. if ((uint64)(uint32)addr + bytes > linear_mem_size)
  4979. goto out_of_bounds;
  4980. maddr = memory->memory_data + (uint32)addr;
  4981. #endif
  4982. if (bh_bitmap_get_bit(module->e->common.data_dropped,
  4983. segment)) {
  4984. seg_len = 0;
  4985. data = NULL;
  4986. }
  4987. else {
  4988. seg_len =
  4989. (uint64)module->module->data_segments[segment]
  4990. ->data_length;
  4991. data = module->module->data_segments[segment]->data;
  4992. }
  4993. if (offset + bytes > seg_len)
  4994. goto out_of_bounds;
  4995. bh_memcpy_s(maddr, (uint32)(linear_mem_size - addr),
  4996. data + offset, (uint32)bytes);
  4997. break;
  4998. }
  4999. case WASM_OP_DATA_DROP:
  5000. {
  5001. uint32 segment;
  5002. read_leb_uint32(frame_ip, frame_ip_end, segment);
  5003. bh_bitmap_set_bit(module->e->common.data_dropped,
  5004. segment);
  5005. break;
  5006. }
  5007. case WASM_OP_MEMORY_COPY:
  5008. {
  5009. mem_offset_t dst, src, len;
  5010. uint8 *mdst, *msrc;
  5011. frame_ip += 2;
  5012. len = POP_MEM_OFFSET();
  5013. src = POP_MEM_OFFSET();
  5014. dst = POP_MEM_OFFSET();
  5015. #if WASM_ENABLE_THREAD_MGR != 0
  5016. linear_mem_size = get_linear_mem_size();
  5017. #endif
  5018. #ifndef OS_ENABLE_HW_BOUND_CHECK
  5019. CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc);
  5020. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  5021. #else
  5022. if ((uint64)(uint32)src + len > linear_mem_size)
  5023. goto out_of_bounds;
  5024. msrc = memory->memory_data + (uint32)src;
  5025. if ((uint64)(uint32)dst + len > linear_mem_size)
  5026. goto out_of_bounds;
  5027. mdst = memory->memory_data + (uint32)dst;
  5028. #endif
  5029. /* allowing the destination and source to overlap */
  5030. #if WASM_ENABLE_MEMORY64 == 0
  5031. bh_memmove_s(mdst, (uint32)(linear_mem_size - dst),
  5032. msrc, (uint32)len);
  5033. #else
  5034. /* use memmove when memory64 is enabled since len
  5035. may be larger than UINT32_MAX */
  5036. memmove(mdst, msrc, len);
  5037. #endif
  5038. break;
  5039. }
  5040. case WASM_OP_MEMORY_FILL:
  5041. {
  5042. mem_offset_t dst, len;
  5043. uint8 fill_val, *mdst;
  5044. frame_ip++;
  5045. len = POP_MEM_OFFSET();
  5046. fill_val = POP_I32();
  5047. dst = POP_MEM_OFFSET();
  5048. #if WASM_ENABLE_THREAD_MGR != 0
  5049. linear_mem_size = get_linear_mem_size();
  5050. #endif
  5051. #ifndef OS_ENABLE_HW_BOUND_CHECK
  5052. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  5053. #else
  5054. if ((uint64)(uint32)dst + len > linear_mem_size)
  5055. goto out_of_bounds;
  5056. mdst = memory->memory_data + (uint32)dst;
  5057. #endif
  5058. memset(mdst, fill_val, len);
  5059. break;
  5060. }
  5061. #endif /* WASM_ENABLE_BULK_MEMORY */
  5062. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  5063. case WASM_OP_TABLE_INIT:
  5064. {
  5065. uint32 tbl_idx, elem_idx;
  5066. uint32 n, s, d;
  5067. WASMTableInstance *tbl_inst;
  5068. table_elem_type_t *table_elems;
  5069. InitializerExpression *tbl_seg_init_values = NULL,
  5070. *init_values;
  5071. uint32 tbl_seg_len = 0;
  5072. read_leb_uint32(frame_ip, frame_ip_end, elem_idx);
  5073. bh_assert(elem_idx < module->module->table_seg_count);
  5074. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  5075. bh_assert(tbl_idx < module->module->table_count);
  5076. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  5077. n = (uint32)POP_I32();
  5078. s = (uint32)POP_I32();
  5079. d = (uint32)POP_I32();
  5080. if (!bh_bitmap_get_bit(module->e->common.elem_dropped,
  5081. elem_idx)) {
  5082. /* table segment isn't dropped */
  5083. tbl_seg_init_values =
  5084. module->module->table_segments[elem_idx]
  5085. .init_values;
  5086. tbl_seg_len =
  5087. module->module->table_segments[elem_idx]
  5088. .value_count;
  5089. }
  5090. if (offset_len_out_of_bounds(s, n, tbl_seg_len)
  5091. || offset_len_out_of_bounds(d, n,
  5092. tbl_inst->cur_size)) {
  5093. wasm_set_exception(module,
  5094. "out of bounds table access");
  5095. goto got_exception;
  5096. }
  5097. if (!n) {
  5098. break;
  5099. }
  5100. table_elems = tbl_inst->elems + d;
  5101. init_values = tbl_seg_init_values + s;
  5102. #if WASM_ENABLE_GC != 0
  5103. SYNC_ALL_TO_FRAME();
  5104. #endif
  5105. for (i = 0; i < n; i++) {
  5106. /* UINT32_MAX indicates that it is a null ref */
  5107. bh_assert(init_values[i].init_expr_type
  5108. == INIT_EXPR_TYPE_REFNULL_CONST
  5109. || init_values[i].init_expr_type
  5110. == INIT_EXPR_TYPE_FUNCREF_CONST);
  5111. #if WASM_ENABLE_GC == 0
  5112. table_elems[i] =
  5113. (table_elem_type_t)init_values[i].u.ref_index;
  5114. #else
  5115. if (init_values[i].u.ref_index != UINT32_MAX) {
  5116. if (!(func_obj = wasm_create_func_obj(
  5117. module, init_values[i].u.ref_index,
  5118. true, NULL, 0))) {
  5119. goto got_exception;
  5120. }
  5121. table_elems[i] = func_obj;
  5122. }
  5123. else {
  5124. table_elems[i] = NULL_REF;
  5125. }
  5126. #endif
  5127. }
  5128. break;
  5129. }
  5130. case WASM_OP_ELEM_DROP:
  5131. {
  5132. uint32 elem_idx;
  5133. read_leb_uint32(frame_ip, frame_ip_end, elem_idx);
  5134. bh_assert(elem_idx < module->module->table_seg_count);
  5135. bh_bitmap_set_bit(module->e->common.elem_dropped,
  5136. elem_idx);
  5137. break;
  5138. }
  5139. case WASM_OP_TABLE_COPY:
  5140. {
  5141. uint32 src_tbl_idx, dst_tbl_idx;
  5142. uint32 n, s, d;
  5143. WASMTableInstance *src_tbl_inst, *dst_tbl_inst;
  5144. read_leb_uint32(frame_ip, frame_ip_end, dst_tbl_idx);
  5145. bh_assert(dst_tbl_idx < module->table_count);
  5146. dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx);
  5147. read_leb_uint32(frame_ip, frame_ip_end, src_tbl_idx);
  5148. bh_assert(src_tbl_idx < module->table_count);
  5149. src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx);
  5150. n = (uint32)POP_I32();
  5151. s = (uint32)POP_I32();
  5152. d = (uint32)POP_I32();
  5153. if (offset_len_out_of_bounds(d, n,
  5154. dst_tbl_inst->cur_size)
  5155. || offset_len_out_of_bounds(
  5156. s, n, src_tbl_inst->cur_size)) {
  5157. wasm_set_exception(module,
  5158. "out of bounds table access");
  5159. goto got_exception;
  5160. }
  5161. /* if s >= d, copy from front to back */
  5162. /* if s < d, copy from back to front */
  5163. /* merge all together */
  5164. bh_memmove_s((uint8 *)dst_tbl_inst
  5165. + offsetof(WASMTableInstance, elems)
  5166. + d * sizeof(table_elem_type_t),
  5167. (uint32)((dst_tbl_inst->cur_size - d)
  5168. * sizeof(table_elem_type_t)),
  5169. (uint8 *)src_tbl_inst
  5170. + offsetof(WASMTableInstance, elems)
  5171. + s * sizeof(table_elem_type_t),
  5172. (uint32)(n * sizeof(table_elem_type_t)));
  5173. break;
  5174. }
  5175. case WASM_OP_TABLE_GROW:
  5176. {
  5177. WASMTableInstance *tbl_inst;
  5178. uint32 tbl_idx, n, orig_tbl_sz;
  5179. table_elem_type_t init_val;
  5180. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  5181. bh_assert(tbl_idx < module->table_count);
  5182. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  5183. orig_tbl_sz = tbl_inst->cur_size;
  5184. n = POP_I32();
  5185. #if WASM_ENABLE_GC == 0
  5186. init_val = POP_I32();
  5187. #else
  5188. init_val = POP_REF();
  5189. #endif
  5190. if (!wasm_enlarge_table(module, tbl_idx, n, init_val)) {
  5191. PUSH_I32(-1);
  5192. }
  5193. else {
  5194. PUSH_I32(orig_tbl_sz);
  5195. }
  5196. break;
  5197. }
  5198. case WASM_OP_TABLE_SIZE:
  5199. {
  5200. uint32 tbl_idx;
  5201. WASMTableInstance *tbl_inst;
  5202. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  5203. bh_assert(tbl_idx < module->table_count);
  5204. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  5205. PUSH_I32(tbl_inst->cur_size);
  5206. break;
  5207. }
  5208. case WASM_OP_TABLE_FILL:
  5209. {
  5210. uint32 tbl_idx, n;
  5211. WASMTableInstance *tbl_inst;
  5212. table_elem_type_t fill_val;
  5213. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  5214. bh_assert(tbl_idx < module->table_count);
  5215. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  5216. n = POP_I32();
  5217. #if WASM_ENABLE_GC == 0
  5218. fill_val = POP_I32();
  5219. #else
  5220. fill_val = POP_REF();
  5221. #endif
  5222. i = POP_I32();
  5223. if (offset_len_out_of_bounds(i, n,
  5224. tbl_inst->cur_size)) {
  5225. wasm_set_exception(module,
  5226. "out of bounds table access");
  5227. goto got_exception;
  5228. }
  5229. for (; n != 0; i++, n--) {
  5230. tbl_inst->elems[i] = fill_val;
  5231. }
  5232. break;
  5233. }
  5234. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  5235. default:
  5236. wasm_set_exception(module, "unsupported opcode");
  5237. goto got_exception;
  5238. }
  5239. HANDLE_OP_END();
  5240. }
  5241. #if WASM_ENABLE_SHARED_MEMORY != 0
  5242. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  5243. {
  5244. mem_offset_t offset = 0, addr;
  5245. uint32 align = 0;
  5246. uint32 opcode1;
  5247. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  5248. /* opcode1 was checked in loader and is no larger than
  5249. UINT8_MAX */
  5250. opcode = (uint8)opcode1;
  5251. if (opcode != WASM_OP_ATOMIC_FENCE) {
  5252. read_leb_uint32(frame_ip, frame_ip_end, align);
  5253. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  5254. }
  5255. switch (opcode) {
  5256. case WASM_OP_ATOMIC_NOTIFY:
  5257. {
  5258. uint32 notify_count, ret;
  5259. notify_count = POP_I32();
  5260. addr = POP_MEM_OFFSET();
  5261. CHECK_MEMORY_OVERFLOW(4);
  5262. CHECK_ATOMIC_MEMORY_ACCESS();
  5263. ret = wasm_runtime_atomic_notify(
  5264. (WASMModuleInstanceCommon *)module, maddr,
  5265. notify_count);
  5266. if (ret == (uint32)-1)
  5267. goto got_exception;
  5268. PUSH_I32(ret);
  5269. break;
  5270. }
  5271. case WASM_OP_ATOMIC_WAIT32:
  5272. {
  5273. uint64 timeout;
  5274. uint32 expect, ret;
  5275. timeout = POP_I64();
  5276. expect = POP_I32();
  5277. addr = POP_MEM_OFFSET();
  5278. CHECK_MEMORY_OVERFLOW(4);
  5279. CHECK_ATOMIC_MEMORY_ACCESS();
  5280. ret = wasm_runtime_atomic_wait(
  5281. (WASMModuleInstanceCommon *)module, maddr,
  5282. (uint64)expect, timeout, false);
  5283. if (ret == (uint32)-1)
  5284. goto got_exception;
  5285. #if WASM_ENABLE_THREAD_MGR != 0
  5286. CHECK_SUSPEND_FLAGS();
  5287. #endif
  5288. PUSH_I32(ret);
  5289. break;
  5290. }
  5291. case WASM_OP_ATOMIC_WAIT64:
  5292. {
  5293. uint64 timeout, expect;
  5294. uint32 ret;
  5295. timeout = POP_I64();
  5296. expect = POP_I64();
  5297. addr = POP_MEM_OFFSET();
  5298. CHECK_MEMORY_OVERFLOW(8);
  5299. CHECK_ATOMIC_MEMORY_ACCESS();
  5300. ret = wasm_runtime_atomic_wait(
  5301. (WASMModuleInstanceCommon *)module, maddr, expect,
  5302. timeout, true);
  5303. if (ret == (uint32)-1)
  5304. goto got_exception;
  5305. #if WASM_ENABLE_THREAD_MGR != 0
  5306. CHECK_SUSPEND_FLAGS();
  5307. #endif
  5308. PUSH_I32(ret);
  5309. break;
  5310. }
  5311. case WASM_OP_ATOMIC_FENCE:
  5312. {
  5313. /* Skip the memory index */
  5314. frame_ip++;
  5315. os_atomic_thread_fence(os_memory_order_seq_cst);
  5316. break;
  5317. }
  5318. case WASM_OP_ATOMIC_I32_LOAD:
  5319. case WASM_OP_ATOMIC_I32_LOAD8_U:
  5320. case WASM_OP_ATOMIC_I32_LOAD16_U:
  5321. {
  5322. uint32 readv;
  5323. addr = POP_MEM_OFFSET();
  5324. if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) {
  5325. CHECK_MEMORY_OVERFLOW(1);
  5326. CHECK_ATOMIC_MEMORY_ACCESS();
  5327. shared_memory_lock(memory);
  5328. readv = (uint32)(*(uint8 *)maddr);
  5329. shared_memory_unlock(memory);
  5330. }
  5331. else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) {
  5332. CHECK_MEMORY_OVERFLOW(2);
  5333. CHECK_ATOMIC_MEMORY_ACCESS();
  5334. shared_memory_lock(memory);
  5335. readv = (uint32)LOAD_U16(maddr);
  5336. shared_memory_unlock(memory);
  5337. }
  5338. else {
  5339. CHECK_MEMORY_OVERFLOW(4);
  5340. CHECK_ATOMIC_MEMORY_ACCESS();
  5341. shared_memory_lock(memory);
  5342. readv = LOAD_I32(maddr);
  5343. shared_memory_unlock(memory);
  5344. }
  5345. PUSH_I32(readv);
  5346. break;
  5347. }
  5348. case WASM_OP_ATOMIC_I64_LOAD:
  5349. case WASM_OP_ATOMIC_I64_LOAD8_U:
  5350. case WASM_OP_ATOMIC_I64_LOAD16_U:
  5351. case WASM_OP_ATOMIC_I64_LOAD32_U:
  5352. {
  5353. uint64 readv;
  5354. addr = POP_MEM_OFFSET();
  5355. if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) {
  5356. CHECK_MEMORY_OVERFLOW(1);
  5357. CHECK_ATOMIC_MEMORY_ACCESS();
  5358. shared_memory_lock(memory);
  5359. readv = (uint64)(*(uint8 *)maddr);
  5360. shared_memory_unlock(memory);
  5361. }
  5362. else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) {
  5363. CHECK_MEMORY_OVERFLOW(2);
  5364. CHECK_ATOMIC_MEMORY_ACCESS();
  5365. shared_memory_lock(memory);
  5366. readv = (uint64)LOAD_U16(maddr);
  5367. shared_memory_unlock(memory);
  5368. }
  5369. else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) {
  5370. CHECK_MEMORY_OVERFLOW(4);
  5371. CHECK_ATOMIC_MEMORY_ACCESS();
  5372. shared_memory_lock(memory);
  5373. readv = (uint64)LOAD_U32(maddr);
  5374. shared_memory_unlock(memory);
  5375. }
  5376. else {
  5377. CHECK_MEMORY_OVERFLOW(8);
  5378. CHECK_ATOMIC_MEMORY_ACCESS();
  5379. shared_memory_lock(memory);
  5380. readv = LOAD_I64(maddr);
  5381. shared_memory_unlock(memory);
  5382. }
  5383. PUSH_I64(readv);
  5384. break;
  5385. }
  5386. case WASM_OP_ATOMIC_I32_STORE:
  5387. case WASM_OP_ATOMIC_I32_STORE8:
  5388. case WASM_OP_ATOMIC_I32_STORE16:
  5389. {
  5390. uint32 sval;
  5391. sval = (uint32)POP_I32();
  5392. addr = POP_MEM_OFFSET();
  5393. if (opcode == WASM_OP_ATOMIC_I32_STORE8) {
  5394. CHECK_MEMORY_OVERFLOW(1);
  5395. CHECK_ATOMIC_MEMORY_ACCESS();
  5396. shared_memory_lock(memory);
  5397. *(uint8 *)maddr = (uint8)sval;
  5398. shared_memory_unlock(memory);
  5399. }
  5400. else if (opcode == WASM_OP_ATOMIC_I32_STORE16) {
  5401. CHECK_MEMORY_OVERFLOW(2);
  5402. CHECK_ATOMIC_MEMORY_ACCESS();
  5403. shared_memory_lock(memory);
  5404. STORE_U16(maddr, (uint16)sval);
  5405. shared_memory_unlock(memory);
  5406. }
  5407. else {
  5408. CHECK_MEMORY_OVERFLOW(4);
  5409. CHECK_ATOMIC_MEMORY_ACCESS();
  5410. shared_memory_lock(memory);
  5411. STORE_U32(maddr, sval);
  5412. shared_memory_unlock(memory);
  5413. }
  5414. break;
  5415. }
  5416. case WASM_OP_ATOMIC_I64_STORE:
  5417. case WASM_OP_ATOMIC_I64_STORE8:
  5418. case WASM_OP_ATOMIC_I64_STORE16:
  5419. case WASM_OP_ATOMIC_I64_STORE32:
  5420. {
  5421. uint64 sval;
  5422. sval = (uint64)POP_I64();
  5423. addr = POP_MEM_OFFSET();
  5424. if (opcode == WASM_OP_ATOMIC_I64_STORE8) {
  5425. CHECK_MEMORY_OVERFLOW(1);
  5426. CHECK_ATOMIC_MEMORY_ACCESS();
  5427. shared_memory_lock(memory);
  5428. *(uint8 *)maddr = (uint8)sval;
  5429. shared_memory_unlock(memory);
  5430. }
  5431. else if (opcode == WASM_OP_ATOMIC_I64_STORE16) {
  5432. CHECK_MEMORY_OVERFLOW(2);
  5433. CHECK_ATOMIC_MEMORY_ACCESS();
  5434. shared_memory_lock(memory);
  5435. STORE_U16(maddr, (uint16)sval);
  5436. shared_memory_unlock(memory);
  5437. }
  5438. else if (opcode == WASM_OP_ATOMIC_I64_STORE32) {
  5439. CHECK_MEMORY_OVERFLOW(4);
  5440. CHECK_ATOMIC_MEMORY_ACCESS();
  5441. shared_memory_lock(memory);
  5442. STORE_U32(maddr, (uint32)sval);
  5443. shared_memory_unlock(memory);
  5444. }
  5445. else {
  5446. CHECK_MEMORY_OVERFLOW(8);
  5447. CHECK_ATOMIC_MEMORY_ACCESS();
  5448. shared_memory_lock(memory);
  5449. STORE_I64(maddr, sval);
  5450. shared_memory_unlock(memory);
  5451. }
  5452. break;
  5453. }
  5454. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  5455. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  5456. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  5457. {
  5458. uint32 readv, sval, expect;
  5459. sval = POP_I32();
  5460. expect = POP_I32();
  5461. addr = POP_MEM_OFFSET();
  5462. if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) {
  5463. CHECK_MEMORY_OVERFLOW(1);
  5464. CHECK_ATOMIC_MEMORY_ACCESS();
  5465. expect = (uint8)expect;
  5466. shared_memory_lock(memory);
  5467. readv = (uint32)(*(uint8 *)maddr);
  5468. if (readv == expect)
  5469. *(uint8 *)maddr = (uint8)(sval);
  5470. shared_memory_unlock(memory);
  5471. }
  5472. else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) {
  5473. CHECK_MEMORY_OVERFLOW(2);
  5474. CHECK_ATOMIC_MEMORY_ACCESS();
  5475. expect = (uint16)expect;
  5476. shared_memory_lock(memory);
  5477. readv = (uint32)LOAD_U16(maddr);
  5478. if (readv == expect)
  5479. STORE_U16(maddr, (uint16)(sval));
  5480. shared_memory_unlock(memory);
  5481. }
  5482. else {
  5483. CHECK_MEMORY_OVERFLOW(4);
  5484. CHECK_ATOMIC_MEMORY_ACCESS();
  5485. shared_memory_lock(memory);
  5486. readv = LOAD_I32(maddr);
  5487. if (readv == expect)
  5488. STORE_U32(maddr, sval);
  5489. shared_memory_unlock(memory);
  5490. }
  5491. PUSH_I32(readv);
  5492. break;
  5493. }
  5494. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  5495. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  5496. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  5497. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  5498. {
  5499. uint64 readv, sval, expect;
  5500. sval = (uint64)POP_I64();
  5501. expect = (uint64)POP_I64();
  5502. addr = POP_MEM_OFFSET();
  5503. if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) {
  5504. CHECK_MEMORY_OVERFLOW(1);
  5505. CHECK_ATOMIC_MEMORY_ACCESS();
  5506. expect = (uint8)expect;
  5507. shared_memory_lock(memory);
  5508. readv = (uint64)(*(uint8 *)maddr);
  5509. if (readv == expect)
  5510. *(uint8 *)maddr = (uint8)(sval);
  5511. shared_memory_unlock(memory);
  5512. }
  5513. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) {
  5514. CHECK_MEMORY_OVERFLOW(2);
  5515. CHECK_ATOMIC_MEMORY_ACCESS();
  5516. expect = (uint16)expect;
  5517. shared_memory_lock(memory);
  5518. readv = (uint64)LOAD_U16(maddr);
  5519. if (readv == expect)
  5520. STORE_U16(maddr, (uint16)(sval));
  5521. shared_memory_unlock(memory);
  5522. }
  5523. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) {
  5524. CHECK_MEMORY_OVERFLOW(4);
  5525. CHECK_ATOMIC_MEMORY_ACCESS();
  5526. expect = (uint32)expect;
  5527. shared_memory_lock(memory);
  5528. readv = (uint64)LOAD_U32(maddr);
  5529. if (readv == expect)
  5530. STORE_U32(maddr, (uint32)(sval));
  5531. shared_memory_unlock(memory);
  5532. }
  5533. else {
  5534. CHECK_MEMORY_OVERFLOW(8);
  5535. CHECK_ATOMIC_MEMORY_ACCESS();
  5536. shared_memory_lock(memory);
  5537. readv = (uint64)LOAD_I64(maddr);
  5538. if (readv == expect)
  5539. STORE_I64(maddr, sval);
  5540. shared_memory_unlock(memory);
  5541. }
  5542. PUSH_I64(readv);
  5543. break;
  5544. }
  5545. DEF_ATOMIC_RMW_OPCODE(ADD, +);
  5546. DEF_ATOMIC_RMW_OPCODE(SUB, -);
  5547. DEF_ATOMIC_RMW_OPCODE(AND, &);
  5548. DEF_ATOMIC_RMW_OPCODE(OR, |);
  5549. DEF_ATOMIC_RMW_OPCODE(XOR, ^);
  5550. /* xchg, ignore the read value, and store the given
  5551. value: readv * 0 + sval */
  5552. DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +);
  5553. }
  5554. HANDLE_OP_END();
  5555. }
  5556. #endif
  5557. HANDLE_OP(WASM_OP_IMPDEP)
  5558. {
  5559. frame = prev_frame;
  5560. frame_ip = frame->ip;
  5561. frame_sp = frame->sp;
  5562. frame_csp = frame->csp;
  5563. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5564. is_return_call = false;
  5565. #endif
  5566. goto call_func_from_entry;
  5567. }
  5568. #if WASM_ENABLE_DEBUG_INTERP != 0
  5569. HANDLE_OP(DEBUG_OP_BREAK)
  5570. {
  5571. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TRAP);
  5572. WASM_SUSPEND_FLAGS_FETCH_OR(exec_env->suspend_flags,
  5573. WASM_SUSPEND_FLAG_SUSPEND);
  5574. frame_ip--;
  5575. SYNC_ALL_TO_FRAME();
  5576. CHECK_SUSPEND_FLAGS();
  5577. HANDLE_OP_END();
  5578. }
  5579. #endif
  5580. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  5581. default:
  5582. wasm_set_exception(module, "unsupported opcode");
  5583. goto got_exception;
  5584. }
  5585. #endif
  5586. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  5587. HANDLE_OP(WASM_OP_UNUSED_0x0a)
  5588. #if WASM_ENABLE_TAIL_CALL == 0
  5589. HANDLE_OP(WASM_OP_RETURN_CALL)
  5590. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  5591. #endif
  5592. #if WASM_ENABLE_SHARED_MEMORY == 0
  5593. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  5594. #endif
  5595. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  5596. HANDLE_OP(WASM_OP_SELECT_T)
  5597. HANDLE_OP(WASM_OP_TABLE_GET)
  5598. HANDLE_OP(WASM_OP_TABLE_SET)
  5599. HANDLE_OP(WASM_OP_REF_NULL)
  5600. HANDLE_OP(WASM_OP_REF_IS_NULL)
  5601. HANDLE_OP(WASM_OP_REF_FUNC)
  5602. #endif
  5603. #if WASM_ENABLE_GC == 0
  5604. HANDLE_OP(WASM_OP_CALL_REF)
  5605. HANDLE_OP(WASM_OP_RETURN_CALL_REF)
  5606. HANDLE_OP(WASM_OP_REF_EQ)
  5607. HANDLE_OP(WASM_OP_REF_AS_NON_NULL)
  5608. HANDLE_OP(WASM_OP_BR_ON_NULL)
  5609. HANDLE_OP(WASM_OP_BR_ON_NON_NULL)
  5610. HANDLE_OP(WASM_OP_GC_PREFIX)
  5611. #endif
  5612. #if WASM_ENABLE_EXCE_HANDLING == 0
  5613. HANDLE_OP(WASM_OP_TRY)
  5614. HANDLE_OP(WASM_OP_CATCH)
  5615. HANDLE_OP(WASM_OP_THROW)
  5616. HANDLE_OP(WASM_OP_RETHROW)
  5617. HANDLE_OP(WASM_OP_DELEGATE)
  5618. HANDLE_OP(WASM_OP_CATCH_ALL)
  5619. HANDLE_OP(EXT_OP_TRY)
  5620. #endif
  5621. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_SIMD != 0
  5622. /* SIMD isn't supported by interpreter, but when JIT is
  5623. enabled, `iwasm --interp <wasm_file>` may be run to
  5624. trigger the SIMD opcode in interpreter */
  5625. HANDLE_OP(WASM_OP_SIMD_PREFIX)
  5626. #endif
  5627. HANDLE_OP(WASM_OP_UNUSED_0x16)
  5628. HANDLE_OP(WASM_OP_UNUSED_0x17)
  5629. HANDLE_OP(WASM_OP_UNUSED_0x27)
  5630. /* Used by fast interpreter */
  5631. HANDLE_OP(EXT_OP_SET_LOCAL_FAST_I64)
  5632. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST_I64)
  5633. HANDLE_OP(EXT_OP_COPY_STACK_TOP)
  5634. HANDLE_OP(EXT_OP_COPY_STACK_TOP_I64)
  5635. HANDLE_OP(EXT_OP_COPY_STACK_VALUES)
  5636. {
  5637. wasm_set_exception(module, "unsupported opcode");
  5638. goto got_exception;
  5639. }
  5640. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES != 0 */
  5641. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  5642. continue;
  5643. #else
  5644. FETCH_OPCODE_AND_DISPATCH();
  5645. #endif
  5646. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5647. call_func_from_return_call:
  5648. {
  5649. POP(cur_func->param_cell_num);
  5650. if (cur_func->param_cell_num > 0) {
  5651. word_copy(frame->lp, frame_sp, cur_func->param_cell_num);
  5652. }
  5653. FREE_FRAME(exec_env, frame);
  5654. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  5655. is_return_call = true;
  5656. goto call_func_from_entry;
  5657. }
  5658. #endif
  5659. call_func_from_interp:
  5660. {
  5661. /* Only do the copy when it's called from interpreter. */
  5662. WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  5663. if (cur_func->param_cell_num > 0) {
  5664. POP(cur_func->param_cell_num);
  5665. word_copy(outs_area->lp, frame_sp, cur_func->param_cell_num);
  5666. }
  5667. SYNC_ALL_TO_FRAME();
  5668. prev_frame = frame;
  5669. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5670. is_return_call = false;
  5671. #endif
  5672. }
  5673. call_func_from_entry:
  5674. {
  5675. if (cur_func->is_import_func) {
  5676. #if WASM_ENABLE_MULTI_MODULE != 0
  5677. if (cur_func->import_func_inst) {
  5678. wasm_interp_call_func_import(module, exec_env, cur_func,
  5679. prev_frame);
  5680. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5681. if (is_return_call) {
  5682. /* the frame was freed before tail calling and
  5683. the prev_frame was set as exec_env's cur_frame,
  5684. so here we recover context from prev_frame */
  5685. RECOVER_CONTEXT(prev_frame);
  5686. }
  5687. else
  5688. #endif
  5689. {
  5690. prev_frame = frame->prev_frame;
  5691. cur_func = frame->function;
  5692. UPDATE_ALL_FROM_FRAME();
  5693. }
  5694. #if WASM_ENABLE_EXCE_HANDLING != 0
  5695. char uncaught_exception[128] = { 0 };
  5696. bool has_exception =
  5697. wasm_copy_exception(module, uncaught_exception);
  5698. if (has_exception
  5699. && strstr(uncaught_exception, "uncaught wasm exception")) {
  5700. uint32 import_exception;
  5701. /* initialize imported exception index to be invalid */
  5702. SET_INVALID_TAGINDEX(import_exception);
  5703. /* pull external exception */
  5704. uint32 ext_exception = POP_I32();
  5705. /* external function came back with an exception or trap */
  5706. /* lookup exception in import tags */
  5707. WASMTagInstance *tag = module->e->tags;
  5708. for (uint32 t = 0; t < module->module->import_tag_count;
  5709. tag++, t++) {
  5710. /* compare the module and the external index with the
  5711. * imort tag data */
  5712. if ((cur_func->u.func_import->import_module
  5713. == tag->u.tag_import->import_module)
  5714. && (ext_exception
  5715. == tag->u.tag_import
  5716. ->import_tag_index_linked)) {
  5717. /* set the import_exception to the import tag */
  5718. import_exception = t;
  5719. break;
  5720. }
  5721. }
  5722. /*
  5723. * exchange the thrown exception (index valid in submodule)
  5724. * with the imported exception index (valid in this module)
  5725. * if the module did not import the exception,
  5726. * that results in a "INVALID_TAGINDEX", that triggers
  5727. * an CATCH_ALL block, if there is one.
  5728. */
  5729. PUSH_I32(import_exception);
  5730. }
  5731. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  5732. }
  5733. else
  5734. #endif /* end of WASM_ENABLE_MULTI_MODULE != 0 */
  5735. {
  5736. wasm_interp_call_func_native(module, exec_env, cur_func,
  5737. prev_frame);
  5738. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5739. if (is_return_call) {
  5740. /* the frame was freed before tail calling and
  5741. the prev_frame was set as exec_env's cur_frame,
  5742. so here we recover context from prev_frame */
  5743. RECOVER_CONTEXT(prev_frame);
  5744. }
  5745. else
  5746. #endif
  5747. {
  5748. prev_frame = frame->prev_frame;
  5749. cur_func = frame->function;
  5750. UPDATE_ALL_FROM_FRAME();
  5751. }
  5752. }
  5753. /* update memory size, no need to update memory ptr as
  5754. it isn't changed in wasm_enlarge_memory */
  5755. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  5756. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  5757. || WASM_ENABLE_BULK_MEMORY != 0
  5758. if (memory)
  5759. linear_mem_size = get_linear_mem_size();
  5760. #endif
  5761. if (wasm_copy_exception(module, NULL)) {
  5762. #if WASM_ENABLE_EXCE_HANDLING != 0
  5763. /* the caller raised an exception */
  5764. char uncaught_exception[128] = { 0 };
  5765. bool has_exception =
  5766. wasm_copy_exception(module, uncaught_exception);
  5767. /* libc_builtin signaled a "exception thrown by stdc++" trap */
  5768. if (has_exception
  5769. && strstr(uncaught_exception,
  5770. "exception thrown by stdc++")) {
  5771. wasm_set_exception(module, NULL);
  5772. /* setup internal c++ rethrow */
  5773. exception_tag_index = 0;
  5774. goto find_a_catch_handler;
  5775. }
  5776. /* when throw hits the end of a function it signals with a
  5777. * "uncaught wasm exception" trap */
  5778. if (has_exception
  5779. && strstr(uncaught_exception, "uncaught wasm exception")) {
  5780. wasm_set_exception(module, NULL);
  5781. exception_tag_index = POP_I32();
  5782. /* rethrow the exception into that frame */
  5783. goto find_a_catch_handler;
  5784. }
  5785. #endif /* WASM_ENABLE_EXCE_HANDLING != 0 */
  5786. goto got_exception;
  5787. }
  5788. }
  5789. else {
  5790. WASMFunction *cur_wasm_func = cur_func->u.func;
  5791. WASMFuncType *func_type = cur_wasm_func->func_type;
  5792. uint32 max_stack_cell_num = cur_wasm_func->max_stack_cell_num;
  5793. uint32 cell_num_of_local_stack;
  5794. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  5795. uint32 local_cell_idx;
  5796. #endif
  5797. #if WASM_ENABLE_EXCE_HANDLING != 0
  5798. /* account for exception handlers, bundle them here */
  5799. uint32 eh_size =
  5800. cur_wasm_func->exception_handler_count * sizeof(uint8 *);
  5801. max_stack_cell_num += eh_size;
  5802. #endif
  5803. cell_num_of_local_stack = cur_func->param_cell_num
  5804. + cur_func->local_cell_num
  5805. + max_stack_cell_num;
  5806. all_cell_num = cell_num_of_local_stack
  5807. + cur_wasm_func->max_block_num
  5808. * (uint32)sizeof(WASMBranchBlock) / 4;
  5809. #if WASM_ENABLE_GC != 0
  5810. /* area of frame_ref */
  5811. all_cell_num += (cell_num_of_local_stack + 3) / 4;
  5812. #endif
  5813. /* param_cell_num, local_cell_num, max_stack_cell_num and
  5814. max_block_num are all no larger than UINT16_MAX (checked
  5815. in loader), all_cell_num must be smaller than 1MB */
  5816. bh_assert(all_cell_num < 1 * BH_MB);
  5817. frame_size = wasm_interp_interp_frame_size(all_cell_num);
  5818. if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) {
  5819. frame = prev_frame;
  5820. goto got_exception;
  5821. }
  5822. /* Initialize the interpreter context. */
  5823. frame->function = cur_func;
  5824. frame_ip = wasm_get_func_code(cur_func);
  5825. frame_ip_end = wasm_get_func_code_end(cur_func);
  5826. frame_lp = frame->lp;
  5827. frame_sp = frame->sp_bottom =
  5828. frame_lp + cur_func->param_cell_num + cur_func->local_cell_num;
  5829. frame->sp_boundary = frame->sp_bottom + max_stack_cell_num;
  5830. frame_csp = frame->csp_bottom =
  5831. (WASMBranchBlock *)frame->sp_boundary;
  5832. frame->csp_boundary =
  5833. frame->csp_bottom + cur_wasm_func->max_block_num;
  5834. #if WASM_ENABLE_GC != 0
  5835. /* frame->sp and frame->ip are used during GC root set enumeration,
  5836. * so we must initialized these fields here */
  5837. frame->sp = frame_sp;
  5838. frame->ip = frame_ip;
  5839. frame_ref = (uint8 *)frame->csp_boundary;
  5840. init_frame_refs(frame_ref, (uint32)cell_num_of_local_stack,
  5841. cur_func);
  5842. #endif
  5843. /* Initialize the local variables */
  5844. memset(frame_lp + cur_func->param_cell_num, 0,
  5845. (uint32)(cur_func->local_cell_num * 4));
  5846. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  5847. /* externref/funcref should be NULL_REF rather than 0 */
  5848. local_cell_idx = cur_func->param_cell_num;
  5849. for (i = 0; i < cur_wasm_func->local_count; i++) {
  5850. if (cur_wasm_func->local_types[i] == VALUE_TYPE_EXTERNREF
  5851. || cur_wasm_func->local_types[i] == VALUE_TYPE_FUNCREF) {
  5852. *(frame_lp + local_cell_idx) = NULL_REF;
  5853. }
  5854. local_cell_idx +=
  5855. wasm_value_type_cell_num(cur_wasm_func->local_types[i]);
  5856. }
  5857. #endif
  5858. /* Push function block as first block */
  5859. cell_num = func_type->ret_cell_num;
  5860. PUSH_CSP(LABEL_TYPE_FUNCTION, 0, cell_num, frame_ip_end - 1);
  5861. wasm_exec_env_set_cur_frame(exec_env, frame);
  5862. }
  5863. #if WASM_ENABLE_THREAD_MGR != 0
  5864. CHECK_SUSPEND_FLAGS();
  5865. #endif
  5866. HANDLE_OP_END();
  5867. }
  5868. return_func:
  5869. {
  5870. FREE_FRAME(exec_env, frame);
  5871. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  5872. if (!prev_frame->ip) {
  5873. /* Called from native. */
  5874. return;
  5875. }
  5876. RECOVER_CONTEXT(prev_frame);
  5877. #if WASM_ENABLE_EXCE_HANDLING != 0
  5878. if (wasm_get_exception(module)) {
  5879. wasm_set_exception(module, NULL);
  5880. exception_tag_index = POP_I32();
  5881. goto find_a_catch_handler;
  5882. }
  5883. #endif
  5884. HANDLE_OP_END();
  5885. }
  5886. #if WASM_ENABLE_SHARED_MEMORY != 0
  5887. unaligned_atomic:
  5888. wasm_set_exception(module, "unaligned atomic");
  5889. goto got_exception;
  5890. #endif
  5891. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  5892. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  5893. || WASM_ENABLE_BULK_MEMORY != 0
  5894. out_of_bounds:
  5895. wasm_set_exception(module, "out of bounds memory access");
  5896. #endif
  5897. got_exception:
  5898. #if WASM_ENABLE_DEBUG_INTERP != 0
  5899. if (wasm_exec_env_get_instance(exec_env) != NULL) {
  5900. uint8 *frame_ip_temp = frame_ip;
  5901. frame_ip = frame_ip_orig;
  5902. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TRAP);
  5903. CHECK_SUSPEND_FLAGS();
  5904. frame_ip = frame_ip_temp;
  5905. }
  5906. #endif
  5907. SYNC_ALL_TO_FRAME();
  5908. return;
  5909. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  5910. }
  5911. #else
  5912. FETCH_OPCODE_AND_DISPATCH();
  5913. #endif
  5914. }
  5915. #if WASM_ENABLE_GC != 0
  5916. bool
  5917. wasm_interp_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
  5918. {
  5919. WASMInterpFrame *frame;
  5920. WASMObjectRef gc_obj;
  5921. int i;
  5922. frame = wasm_exec_env_get_cur_frame(exec_env);
  5923. for (; frame; frame = frame->prev_frame) {
  5924. uint8 *frame_ref = get_frame_ref(frame);
  5925. for (i = 0; i < frame->sp - frame->lp; i++) {
  5926. if (frame_ref[i]) {
  5927. gc_obj = GET_REF_FROM_ADDR(frame->lp + i);
  5928. if (wasm_obj_is_created_from_heap(gc_obj)) {
  5929. if (mem_allocator_add_root((mem_allocator_t)heap, gc_obj)) {
  5930. return false;
  5931. }
  5932. }
  5933. #if UINTPTR_MAX == UINT64_MAX
  5934. bh_assert(frame_ref[i + 1]);
  5935. i++;
  5936. #endif
  5937. }
  5938. }
  5939. }
  5940. return true;
  5941. }
  5942. #endif
  5943. #if WASM_ENABLE_FAST_JIT != 0
  5944. /*
  5945. * ASAN is not designed to work with custom stack unwind or other low-level
  5946. * things. Ignore a function that does some low-level magic. (e.g. walking
  5947. * through the thread's stack bypassing the frame boundaries)
  5948. */
  5949. #if defined(__GNUC__) || defined(__clang__)
  5950. __attribute__((no_sanitize_address))
  5951. #endif
  5952. static void
  5953. fast_jit_call_func_bytecode(WASMModuleInstance *module_inst,
  5954. WASMExecEnv *exec_env,
  5955. WASMFunctionInstance *function,
  5956. WASMInterpFrame *frame)
  5957. {
  5958. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  5959. JitInterpSwitchInfo info;
  5960. WASMModule *module = module_inst->module;
  5961. WASMFuncType *func_type = function->u.func->func_type;
  5962. uint8 type = func_type->result_count
  5963. ? func_type->types[func_type->param_count]
  5964. : VALUE_TYPE_VOID;
  5965. uint32 func_idx = (uint32)(function - module_inst->e->functions);
  5966. uint32 func_idx_non_import = func_idx - module->import_function_count;
  5967. int32 action;
  5968. #if WASM_ENABLE_REF_TYPES != 0
  5969. if (type == VALUE_TYPE_EXTERNREF || type == VALUE_TYPE_FUNCREF)
  5970. type = VALUE_TYPE_I32;
  5971. #endif
  5972. #if WASM_ENABLE_LAZY_JIT != 0
  5973. if (!jit_compiler_compile(module, func_idx)) {
  5974. wasm_set_exception(module_inst, "failed to compile fast jit function");
  5975. return;
  5976. }
  5977. #endif
  5978. bh_assert(jit_compiler_is_compiled(module, func_idx));
  5979. /* Switch to jitted code to call the jit function */
  5980. info.out.ret.last_return_type = type;
  5981. info.frame = frame;
  5982. frame->jitted_return_addr =
  5983. (uint8 *)jit_globals->return_to_interp_from_jitted;
  5984. action = jit_interp_switch_to_jitted(
  5985. exec_env, &info, func_idx,
  5986. module_inst->fast_jit_func_ptrs[func_idx_non_import]);
  5987. bh_assert(action == JIT_INTERP_ACTION_NORMAL
  5988. || (action == JIT_INTERP_ACTION_THROWN
  5989. && wasm_copy_exception(
  5990. (WASMModuleInstance *)exec_env->module_inst, NULL)));
  5991. /* Get the return values form info.out.ret */
  5992. if (func_type->result_count) {
  5993. switch (type) {
  5994. case VALUE_TYPE_I32:
  5995. *(frame->sp - function->ret_cell_num) = info.out.ret.ival[0];
  5996. break;
  5997. case VALUE_TYPE_I64:
  5998. *(frame->sp - function->ret_cell_num) = info.out.ret.ival[0];
  5999. *(frame->sp - function->ret_cell_num + 1) =
  6000. info.out.ret.ival[1];
  6001. break;
  6002. case VALUE_TYPE_F32:
  6003. *(frame->sp - function->ret_cell_num) = info.out.ret.fval[0];
  6004. break;
  6005. case VALUE_TYPE_F64:
  6006. *(frame->sp - function->ret_cell_num) = info.out.ret.fval[0];
  6007. *(frame->sp - function->ret_cell_num + 1) =
  6008. info.out.ret.fval[1];
  6009. break;
  6010. default:
  6011. bh_assert(0);
  6012. break;
  6013. }
  6014. }
  6015. (void)action;
  6016. (void)func_idx;
  6017. }
  6018. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  6019. #if WASM_ENABLE_JIT != 0
  6020. #if WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_PERF_PROFILING != 0 \
  6021. || WASM_ENABLE_AOT_STACK_FRAME != 0
  6022. #if WASM_ENABLE_GC == 0
  6023. bool
  6024. llvm_jit_alloc_frame(WASMExecEnv *exec_env, uint32 func_index)
  6025. {
  6026. WASMModuleInstance *module_inst =
  6027. (WASMModuleInstance *)exec_env->module_inst;
  6028. WASMInterpFrame *cur_frame, *frame;
  6029. uint32 size = (uint32)offsetof(WASMInterpFrame, lp);
  6030. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  6031. cur_frame = exec_env->cur_frame;
  6032. if (!cur_frame)
  6033. frame = (WASMInterpFrame *)exec_env->wasm_stack.bottom;
  6034. else
  6035. frame = (WASMInterpFrame *)((uint8 *)cur_frame + size);
  6036. if ((uint8 *)frame + size > exec_env->wasm_stack.top_boundary) {
  6037. wasm_set_exception(module_inst, "wasm operand stack overflow");
  6038. return false;
  6039. }
  6040. frame->function = module_inst->e->functions + func_index;
  6041. /* No need to initialize ip, it will be committed in jitted code
  6042. when needed */
  6043. /* frame->ip = NULL; */
  6044. frame->prev_frame = cur_frame;
  6045. #if WASM_ENABLE_PERF_PROFILING != 0
  6046. frame->time_started = os_time_thread_cputime_us();
  6047. #endif
  6048. #if WASM_ENABLE_MEMORY_PROFILING != 0
  6049. {
  6050. uint32 wasm_stack_used =
  6051. (uint8 *)frame + size - exec_env->wasm_stack.bottom;
  6052. if (wasm_stack_used > exec_env->max_wasm_stack_used)
  6053. exec_env->max_wasm_stack_used = wasm_stack_used;
  6054. }
  6055. #endif
  6056. exec_env->cur_frame = frame;
  6057. return true;
  6058. }
  6059. static inline void
  6060. llvm_jit_free_frame_internal(WASMExecEnv *exec_env)
  6061. {
  6062. WASMInterpFrame *frame = exec_env->cur_frame;
  6063. WASMInterpFrame *prev_frame = frame->prev_frame;
  6064. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  6065. #if WASM_ENABLE_PERF_PROFILING != 0
  6066. if (frame->function) {
  6067. uint64 time_elapsed = os_time_thread_cputime_us() - frame->time_started;
  6068. frame->function->total_exec_time += time_elapsed;
  6069. frame->function->total_exec_cnt++;
  6070. /* parent function */
  6071. if (prev_frame)
  6072. prev_frame->function->children_exec_time += time_elapsed;
  6073. }
  6074. #endif
  6075. exec_env->cur_frame = prev_frame;
  6076. }
  6077. void
  6078. llvm_jit_free_frame(WASMExecEnv *exec_env)
  6079. {
  6080. llvm_jit_free_frame_internal(exec_env);
  6081. }
  6082. #else /* else of WASM_ENABLE_GC == 0 */
  6083. bool
  6084. llvm_jit_alloc_frame(WASMExecEnv *exec_env, uint32 func_index)
  6085. {
  6086. WASMModuleInstance *module_inst;
  6087. WASMModule *module;
  6088. WASMInterpFrame *frame;
  6089. uint32 size, max_local_cell_num, max_stack_cell_num;
  6090. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  6091. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  6092. module = module_inst->module;
  6093. if (func_index >= func_index - module->import_function_count) {
  6094. WASMFunction *func =
  6095. module->functions[func_index - module->import_function_count];
  6096. max_local_cell_num = func->param_cell_num + func->local_cell_num;
  6097. max_stack_cell_num = func->max_stack_cell_num;
  6098. }
  6099. else {
  6100. WASMFunctionImport *func =
  6101. &((module->import_functions + func_index)->u.function);
  6102. max_local_cell_num = func->func_type->param_cell_num > 2
  6103. ? func->func_type->param_cell_num
  6104. : 2;
  6105. max_stack_cell_num = 0;
  6106. }
  6107. size =
  6108. wasm_interp_interp_frame_size(max_local_cell_num + max_stack_cell_num);
  6109. frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
  6110. if (!frame) {
  6111. wasm_set_exception(module_inst, "wasm operand stack overflow");
  6112. return false;
  6113. }
  6114. frame->function = module_inst->e->functions + func_index;
  6115. #if WASM_ENABLE_PERF_PROFILING != 0
  6116. frame->time_started = os_time_thread_cputime_us();
  6117. #endif
  6118. frame->prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  6119. /* No need to initialize ip, it will be committed in jitted code
  6120. when needed */
  6121. /* frame->ip = NULL; */
  6122. #if WASM_ENABLE_GC != 0
  6123. frame->sp = frame->lp + max_local_cell_num;
  6124. /* Initialize frame ref flags for import function */
  6125. if (func_index < module->import_function_count) {
  6126. WASMFunctionImport *func =
  6127. &((module->import_functions + func_index)->u.function);
  6128. WASMFuncType *func_type = func->func_type;
  6129. /* native function doesn't have operand stack and label stack */
  6130. uint8 *frame_ref = (uint8 *)frame->sp;
  6131. uint32 i, j, k, value_type_cell_num;
  6132. for (i = 0, j = 0; i < func_type->param_count; i++) {
  6133. if (wasm_is_type_reftype(func_type->types[i])
  6134. && !wasm_is_reftype_i31ref(func_type->types[i])) {
  6135. frame_ref[j++] = 1;
  6136. #if UINTPTR_MAX == UINT64_MAX
  6137. frame_ref[j++] = 1;
  6138. #endif
  6139. }
  6140. else {
  6141. value_type_cell_num =
  6142. wasm_value_type_cell_num(func_type->types[i]);
  6143. for (k = 0; k < value_type_cell_num; k++)
  6144. frame_ref[j++] = 0;
  6145. }
  6146. }
  6147. }
  6148. #endif
  6149. wasm_exec_env_set_cur_frame(exec_env, frame);
  6150. return true;
  6151. }
  6152. static inline void
  6153. llvm_jit_free_frame_internal(WASMExecEnv *exec_env)
  6154. {
  6155. WASMInterpFrame *frame;
  6156. WASMInterpFrame *prev_frame;
  6157. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  6158. frame = wasm_exec_env_get_cur_frame(exec_env);
  6159. prev_frame = frame->prev_frame;
  6160. #if WASM_ENABLE_PERF_PROFILING != 0
  6161. if (frame->function) {
  6162. uint64 time_elapsed = os_time_thread_cputime_us() - frame->time_started;
  6163. frame->function->total_exec_time += time_elapsed;
  6164. frame->function->total_exec_cnt++;
  6165. /* parent function */
  6166. if (prev_frame)
  6167. prev_frame->function->children_exec_time += time_elapsed;
  6168. }
  6169. #endif
  6170. wasm_exec_env_free_wasm_frame(exec_env, frame);
  6171. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  6172. }
  6173. void
  6174. llvm_jit_free_frame(WASMExecEnv *exec_env)
  6175. {
  6176. llvm_jit_free_frame_internal(exec_env);
  6177. }
  6178. #endif /* end of WASM_ENABLE_GC == 0 */
  6179. void
  6180. llvm_jit_frame_update_profile_info(WASMExecEnv *exec_env, bool alloc_frame)
  6181. {
  6182. #if WASM_ENABLE_PERF_PROFILING != 0
  6183. WASMInterpFrame *cur_frame = exec_env->cur_frame;
  6184. if (alloc_frame) {
  6185. cur_frame->time_started = os_time_thread_cputime_us();
  6186. }
  6187. else {
  6188. if (cur_frame->function) {
  6189. WASMInterpFrame *prev_frame = cur_frame->prev_frame;
  6190. uint64 time_elapsed =
  6191. os_time_thread_cputime_us() - cur_frame->time_started;
  6192. cur_frame->function->total_exec_time += time_elapsed;
  6193. cur_frame->function->total_exec_cnt++;
  6194. /* parent function */
  6195. if (prev_frame)
  6196. prev_frame->function->children_exec_time += time_elapsed;
  6197. }
  6198. }
  6199. #endif
  6200. #if WASM_ENABLE_MEMORY_PROFILING != 0
  6201. if (alloc_frame) {
  6202. #if WASM_ENABLE_GC == 0
  6203. uint32 wasm_stack_used = (uint8 *)exec_env->cur_frame
  6204. + (uint32)offsetof(WASMInterpFrame, lp)
  6205. - exec_env->wasm_stack.bottom;
  6206. #else
  6207. uint32 wasm_stack_used =
  6208. exec_env->wasm_stack.top - exec_env->wasm_stack.bottom;
  6209. #endif
  6210. if (wasm_stack_used > exec_env->max_wasm_stack_used)
  6211. exec_env->max_wasm_stack_used = wasm_stack_used;
  6212. }
  6213. #endif
  6214. }
  6215. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 \
  6216. || WASM_ENABLE_PERF_PROFILING != 0 \
  6217. || WASM_ENABLE_AOT_STACK_FRAME != 0 */
  6218. static bool
  6219. llvm_jit_call_func_bytecode(WASMModuleInstance *module_inst,
  6220. WASMExecEnv *exec_env,
  6221. WASMFunctionInstance *function, uint32 argc,
  6222. uint32 argv[])
  6223. {
  6224. WASMFuncType *func_type = function->u.func->func_type;
  6225. uint32 result_count = func_type->result_count;
  6226. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  6227. uint32 func_idx = (uint32)(function - module_inst->e->functions);
  6228. bool ret = false;
  6229. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0) \
  6230. || (WASM_ENABLE_AOT_STACK_FRAME != 0)
  6231. if (!llvm_jit_alloc_frame(exec_env, function - module_inst->e->functions)) {
  6232. /* wasm operand stack overflow has been thrown,
  6233. no need to throw again */
  6234. return false;
  6235. }
  6236. #endif
  6237. if (ext_ret_count > 0) {
  6238. uint32 cell_num = 0, i;
  6239. uint8 *ext_ret_types = func_type->types + func_type->param_count + 1;
  6240. uint32 argv1_buf[32], *argv1 = argv1_buf, *ext_rets = NULL;
  6241. uint32 *argv_ret = argv;
  6242. uint32 ext_ret_cell = wasm_get_cell_num(ext_ret_types, ext_ret_count);
  6243. uint64 size;
  6244. /* Allocate memory all arguments */
  6245. size =
  6246. sizeof(uint32) * (uint64)argc /* original arguments */
  6247. + sizeof(void *)
  6248. * (uint64)ext_ret_count /* extra result values' addr */
  6249. + sizeof(uint32) * (uint64)ext_ret_cell; /* extra result values */
  6250. if (size > sizeof(argv1_buf)) {
  6251. if (size > UINT32_MAX
  6252. || !(argv1 = wasm_runtime_malloc((uint32)size))) {
  6253. wasm_set_exception(module_inst, "allocate memory failed");
  6254. ret = false;
  6255. goto fail;
  6256. }
  6257. }
  6258. /* Copy original arguments */
  6259. bh_memcpy_s(argv1, (uint32)size, argv, sizeof(uint32) * argc);
  6260. /* Get the extra result value's address */
  6261. ext_rets =
  6262. argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count;
  6263. /* Append each extra result value's address to original arguments */
  6264. for (i = 0; i < ext_ret_count; i++) {
  6265. *(uintptr_t *)(argv1 + argc + sizeof(void *) / sizeof(uint32) * i) =
  6266. (uintptr_t)(ext_rets + cell_num);
  6267. cell_num += wasm_value_type_cell_num(ext_ret_types[i]);
  6268. }
  6269. ret = wasm_runtime_invoke_native(
  6270. exec_env, module_inst->func_ptrs[func_idx], func_type, NULL, NULL,
  6271. argv1, argc, argv);
  6272. if (!ret) {
  6273. if (argv1 != argv1_buf)
  6274. wasm_runtime_free(argv1);
  6275. goto fail;
  6276. }
  6277. /* Get extra result values */
  6278. switch (func_type->types[func_type->param_count]) {
  6279. case VALUE_TYPE_I32:
  6280. case VALUE_TYPE_F32:
  6281. #if WASM_ENABLE_REF_TYPES != 0
  6282. case VALUE_TYPE_FUNCREF:
  6283. case VALUE_TYPE_EXTERNREF:
  6284. #endif
  6285. argv_ret++;
  6286. break;
  6287. case VALUE_TYPE_I64:
  6288. case VALUE_TYPE_F64:
  6289. argv_ret += 2;
  6290. break;
  6291. #if WASM_ENABLE_SIMD != 0
  6292. case VALUE_TYPE_V128:
  6293. argv_ret += 4;
  6294. break;
  6295. #endif
  6296. default:
  6297. bh_assert(0);
  6298. break;
  6299. }
  6300. ext_rets =
  6301. argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count;
  6302. bh_memcpy_s(argv_ret, sizeof(uint32) * cell_num, ext_rets,
  6303. sizeof(uint32) * cell_num);
  6304. if (argv1 != argv1_buf)
  6305. wasm_runtime_free(argv1);
  6306. ret = true;
  6307. }
  6308. else {
  6309. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  6310. /* Quick call if the quick jit entry is registered */
  6311. if (func_type->quick_aot_entry) {
  6312. void (*invoke_native)(void *func_ptr, void *exec_env, uint32 *argv,
  6313. uint32 *argv_ret) =
  6314. func_type->quick_aot_entry;
  6315. invoke_native(module_inst->func_ptrs[func_idx], exec_env, argv,
  6316. argv);
  6317. ret = !wasm_copy_exception(module_inst, NULL);
  6318. }
  6319. else
  6320. #endif
  6321. {
  6322. ret = wasm_runtime_invoke_native(
  6323. exec_env, module_inst->func_ptrs[func_idx], func_type, NULL,
  6324. NULL, argv, argc, argv);
  6325. if (ret)
  6326. ret = !wasm_copy_exception(module_inst, NULL);
  6327. }
  6328. }
  6329. fail:
  6330. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0) \
  6331. || (WASM_ENABLE_AOT_STACK_FRAME != 0)
  6332. llvm_jit_free_frame_internal(exec_env);
  6333. #endif
  6334. return ret;
  6335. }
  6336. #endif /* end of WASM_ENABLE_JIT != 0 */
  6337. void
  6338. wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  6339. WASMFunctionInstance *function, uint32 argc,
  6340. uint32 argv[])
  6341. {
  6342. WASMRuntimeFrame *frame = NULL, *prev_frame, *outs_area;
  6343. RunningMode running_mode =
  6344. wasm_runtime_get_running_mode((WASMModuleInstanceCommon *)module_inst);
  6345. /* Allocate sufficient cells for all kinds of return values. */
  6346. bool alloc_frame = true;
  6347. if (argc < function->param_cell_num) {
  6348. char buf[128];
  6349. snprintf(buf, sizeof(buf),
  6350. "invalid argument count %" PRIu32
  6351. ", must be no smaller than %u",
  6352. argc, function->param_cell_num);
  6353. wasm_set_exception(module_inst, buf);
  6354. return;
  6355. }
  6356. argc = function->param_cell_num;
  6357. #if defined(OS_ENABLE_HW_BOUND_CHECK) && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  6358. /*
  6359. * wasm_runtime_detect_native_stack_overflow is done by
  6360. * call_wasm_with_hw_bound_check.
  6361. */
  6362. #else
  6363. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  6364. return;
  6365. }
  6366. #endif
  6367. if (!function->is_import_func) {
  6368. /* No need to alloc frame when calling LLVM JIT function */
  6369. #if WASM_ENABLE_JIT != 0
  6370. if (running_mode == Mode_LLVM_JIT) {
  6371. alloc_frame = false;
  6372. }
  6373. #if WASM_ENABLE_LAZY_JIT != 0 && WASM_ENABLE_FAST_JIT != 0
  6374. else if (running_mode == Mode_Multi_Tier_JIT) {
  6375. /* Tier-up from Fast JIT to LLVM JIT, call llvm jit function
  6376. if it is compiled, else call fast jit function */
  6377. uint32 func_idx = (uint32)(function - module_inst->e->functions);
  6378. if (module_inst->module->func_ptrs_compiled
  6379. [func_idx - module_inst->module->import_function_count]) {
  6380. alloc_frame = false;
  6381. }
  6382. }
  6383. #endif
  6384. #endif
  6385. }
  6386. if (alloc_frame) {
  6387. unsigned all_cell_num =
  6388. function->ret_cell_num > 2 ? function->ret_cell_num : 2;
  6389. unsigned frame_size;
  6390. prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  6391. /* This frame won't be used by JITed code, so only allocate interp
  6392. frame here. */
  6393. frame_size = wasm_interp_interp_frame_size(all_cell_num);
  6394. if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame)))
  6395. return;
  6396. outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  6397. frame->function = NULL;
  6398. frame->ip = NULL;
  6399. /* There is no local variable. */
  6400. frame->sp = frame->lp + 0;
  6401. if ((uint8 *)(outs_area->lp + function->param_cell_num)
  6402. > exec_env->wasm_stack.top_boundary) {
  6403. wasm_set_exception(module_inst, "wasm operand stack overflow");
  6404. return;
  6405. }
  6406. if (argc > 0)
  6407. word_copy(outs_area->lp, argv, argc);
  6408. wasm_exec_env_set_cur_frame(exec_env, frame);
  6409. }
  6410. #if defined(os_writegsbase)
  6411. {
  6412. WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
  6413. if (memory_inst)
  6414. /* write base addr of linear memory to GS segment register */
  6415. os_writegsbase(memory_inst->memory_data);
  6416. }
  6417. #endif
  6418. if (function->is_import_func) {
  6419. #if WASM_ENABLE_MULTI_MODULE != 0
  6420. if (function->import_module_inst) {
  6421. wasm_interp_call_func_import(module_inst, exec_env, function,
  6422. frame);
  6423. }
  6424. else
  6425. #endif
  6426. {
  6427. /* it is a native function */
  6428. wasm_interp_call_func_native(module_inst, exec_env, function,
  6429. frame);
  6430. }
  6431. }
  6432. else {
  6433. if (running_mode == Mode_Interp) {
  6434. wasm_interp_call_func_bytecode(module_inst, exec_env, function,
  6435. frame);
  6436. }
  6437. #if WASM_ENABLE_FAST_JIT != 0
  6438. else if (running_mode == Mode_Fast_JIT) {
  6439. fast_jit_call_func_bytecode(module_inst, exec_env, function, frame);
  6440. }
  6441. #endif
  6442. #if WASM_ENABLE_JIT != 0
  6443. else if (running_mode == Mode_LLVM_JIT) {
  6444. llvm_jit_call_func_bytecode(module_inst, exec_env, function, argc,
  6445. argv);
  6446. }
  6447. #endif
  6448. #if WASM_ENABLE_LAZY_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 \
  6449. && WASM_ENABLE_JIT != 0
  6450. else if (running_mode == Mode_Multi_Tier_JIT) {
  6451. /* Tier-up from Fast JIT to LLVM JIT, call llvm jit function
  6452. if it is compiled, else call fast jit function */
  6453. uint32 func_idx = (uint32)(function - module_inst->e->functions);
  6454. if (module_inst->module->func_ptrs_compiled
  6455. [func_idx - module_inst->module->import_function_count]) {
  6456. llvm_jit_call_func_bytecode(module_inst, exec_env, function,
  6457. argc, argv);
  6458. }
  6459. else {
  6460. fast_jit_call_func_bytecode(module_inst, exec_env, function,
  6461. frame);
  6462. }
  6463. }
  6464. #endif
  6465. else {
  6466. /* There should always be a supported running mode selected */
  6467. bh_assert(0);
  6468. }
  6469. (void)wasm_interp_call_func_bytecode;
  6470. #if WASM_ENABLE_FAST_JIT != 0
  6471. (void)fast_jit_call_func_bytecode;
  6472. #endif
  6473. }
  6474. /* Output the return value to the caller */
  6475. if (!wasm_copy_exception(module_inst, NULL)) {
  6476. if (alloc_frame) {
  6477. uint32 i;
  6478. for (i = 0; i < function->ret_cell_num; i++) {
  6479. argv[i] = *(frame->sp + i - function->ret_cell_num);
  6480. }
  6481. }
  6482. }
  6483. else {
  6484. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  6485. if (wasm_interp_create_call_stack(exec_env)) {
  6486. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  6487. }
  6488. #endif
  6489. }
  6490. if (alloc_frame) {
  6491. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  6492. FREE_FRAME(exec_env, frame);
  6493. }
  6494. }