wasm_interp_fast.c 233 KB

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