wasm_interp_classic.c 284 KB

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