wasm_interp_fast.c 233 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294
  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. uint32 off, ret_offset;
  1511. uint8 *ret_types;
  1512. if (cur_func->is_import_func)
  1513. func_type = cur_func->u.func_import->func_type;
  1514. else
  1515. func_type = cur_func->u.func->func_type;
  1516. /* types of each return value */
  1517. ret_types = func_type->types + func_type->param_count;
  1518. ret_offset = prev_frame->ret_offset;
  1519. for (ret_idx = 0,
  1520. off = sizeof(int16) * (func_type->result_count - 1);
  1521. ret_idx < func_type->result_count;
  1522. ret_idx++, off -= sizeof(int16)) {
  1523. if (ret_types[ret_idx] == VALUE_TYPE_I64
  1524. || ret_types[ret_idx] == VALUE_TYPE_F64) {
  1525. PUT_I64_TO_ADDR(prev_frame->lp + ret_offset,
  1526. GET_OPERAND(uint64, I64, off));
  1527. ret_offset += 2;
  1528. }
  1529. #if WASM_ENABLE_GC != 0
  1530. else if (wasm_is_type_reftype(ret_types[ret_idx])) {
  1531. PUT_REF_TO_ADDR(prev_frame->lp + ret_offset,
  1532. GET_OPERAND(void *, REF, off));
  1533. if (!wasm_is_reftype_i31ref(ret_types[ret_idx])) {
  1534. *(prev_frame->frame_ref + ret_offset) = 1;
  1535. #if UINTPTR_MAX == UINT64_MAX
  1536. *(prev_frame->frame_ref + ret_offset + 1) = 1;
  1537. #endif
  1538. }
  1539. ret_offset += REF_CELL_NUM;
  1540. }
  1541. #endif
  1542. else {
  1543. prev_frame->lp[ret_offset] =
  1544. GET_OPERAND(uint32, I32, off);
  1545. ret_offset++;
  1546. }
  1547. }
  1548. goto return_func;
  1549. }
  1550. HANDLE_OP(WASM_OP_CALL_INDIRECT)
  1551. #if WASM_ENABLE_TAIL_CALL != 0
  1552. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  1553. #endif
  1554. {
  1555. WASMFuncType *cur_type, *cur_func_type;
  1556. WASMTableInstance *tbl_inst;
  1557. uint32 tbl_idx;
  1558. #if WASM_ENABLE_TAIL_CALL != 0
  1559. GET_OPCODE();
  1560. #endif
  1561. #if WASM_ENABLE_THREAD_MGR != 0
  1562. CHECK_SUSPEND_FLAGS();
  1563. #endif
  1564. tidx = read_uint32(frame_ip);
  1565. cur_type = (WASMFuncType *)module->module->types[tidx];
  1566. tbl_idx = read_uint32(frame_ip);
  1567. bh_assert(tbl_idx < module->table_count);
  1568. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1569. val = GET_OPERAND(uint32, I32, 0);
  1570. frame_ip += 2;
  1571. if ((uint32)val >= tbl_inst->cur_size) {
  1572. wasm_set_exception(module, "undefined element");
  1573. goto got_exception;
  1574. }
  1575. /* clang-format off */
  1576. #if WASM_ENABLE_GC == 0
  1577. fidx = (uint32)tbl_inst->elems[val];
  1578. if (fidx == (uint32)-1) {
  1579. wasm_set_exception(module, "uninitialized element");
  1580. goto got_exception;
  1581. }
  1582. #else
  1583. func_obj = (WASMFuncObjectRef)tbl_inst->elems[val];
  1584. if (!func_obj) {
  1585. wasm_set_exception(module, "uninitialized element");
  1586. goto got_exception;
  1587. }
  1588. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  1589. #endif
  1590. /* clang-format on */
  1591. /*
  1592. * we might be using a table injected by host or
  1593. * another module. in that case, we don't validate
  1594. * the elem value while loading
  1595. */
  1596. if (fidx >= module->e->function_count) {
  1597. wasm_set_exception(module, "unknown function");
  1598. goto got_exception;
  1599. }
  1600. /* always call module own functions */
  1601. cur_func = module->e->functions + fidx;
  1602. if (cur_func->is_import_func)
  1603. cur_func_type = cur_func->u.func_import->func_type;
  1604. else
  1605. cur_func_type = cur_func->u.func->func_type;
  1606. /* clang-format off */
  1607. #if WASM_ENABLE_GC == 0
  1608. if (cur_type != cur_func_type) {
  1609. wasm_set_exception(module, "indirect call type mismatch");
  1610. goto got_exception;
  1611. }
  1612. #else
  1613. if (!wasm_func_type_is_super_of(cur_type, cur_func_type)) {
  1614. wasm_set_exception(module, "indirect call type mismatch");
  1615. goto got_exception;
  1616. }
  1617. #endif
  1618. /* clang-format on */
  1619. #if WASM_ENABLE_TAIL_CALL != 0
  1620. if (opcode == WASM_OP_RETURN_CALL_INDIRECT)
  1621. goto call_func_from_return_call;
  1622. #endif
  1623. goto call_func_from_interp;
  1624. }
  1625. #if WASM_ENABLE_EXCE_HANDLING != 0
  1626. HANDLE_OP(WASM_OP_TRY)
  1627. HANDLE_OP(WASM_OP_CATCH)
  1628. HANDLE_OP(WASM_OP_THROW)
  1629. HANDLE_OP(WASM_OP_RETHROW)
  1630. HANDLE_OP(WASM_OP_DELEGATE)
  1631. HANDLE_OP(WASM_OP_CATCH_ALL)
  1632. HANDLE_OP(EXT_OP_TRY)
  1633. {
  1634. wasm_set_exception(module, "unsupported opcode");
  1635. goto got_exception;
  1636. }
  1637. #endif
  1638. /* parametric instructions */
  1639. HANDLE_OP(WASM_OP_SELECT)
  1640. {
  1641. cond = frame_lp[GET_OFFSET()];
  1642. addr1 = GET_OFFSET();
  1643. addr2 = GET_OFFSET();
  1644. addr_ret = GET_OFFSET();
  1645. if (!cond) {
  1646. if (addr_ret != addr1)
  1647. frame_lp[addr_ret] = frame_lp[addr1];
  1648. }
  1649. else {
  1650. if (addr_ret != addr2)
  1651. frame_lp[addr_ret] = frame_lp[addr2];
  1652. }
  1653. HANDLE_OP_END();
  1654. }
  1655. HANDLE_OP(WASM_OP_SELECT_64)
  1656. {
  1657. cond = frame_lp[GET_OFFSET()];
  1658. addr1 = GET_OFFSET();
  1659. addr2 = GET_OFFSET();
  1660. addr_ret = GET_OFFSET();
  1661. if (!cond) {
  1662. if (addr_ret != addr1)
  1663. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1664. GET_I64_FROM_ADDR(frame_lp + addr1));
  1665. }
  1666. else {
  1667. if (addr_ret != addr2)
  1668. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1669. GET_I64_FROM_ADDR(frame_lp + addr2));
  1670. }
  1671. HANDLE_OP_END();
  1672. }
  1673. #if WASM_ENABLE_GC != 0
  1674. HANDLE_OP(WASM_OP_SELECT_T)
  1675. {
  1676. cond = frame_lp[GET_OFFSET()];
  1677. addr1 = GET_OFFSET();
  1678. addr2 = GET_OFFSET();
  1679. addr_ret = GET_OFFSET();
  1680. if (!cond) {
  1681. if (addr_ret != addr1)
  1682. PUT_REF_TO_ADDR(frame_lp + addr_ret,
  1683. GET_REF_FROM_ADDR(frame_lp + addr1));
  1684. }
  1685. else {
  1686. if (addr_ret != addr2)
  1687. PUT_REF_TO_ADDR(frame_lp + addr_ret,
  1688. GET_REF_FROM_ADDR(frame_lp + addr2));
  1689. }
  1690. {
  1691. uint8 orig_ref = 0;
  1692. /* Ignore constants because they are not reference */
  1693. if (addr1 >= 0) {
  1694. orig_ref = *FRAME_REF(addr1);
  1695. CLEAR_FRAME_REF(addr1);
  1696. }
  1697. if (addr2 >= 0) {
  1698. CLEAR_FRAME_REF(addr2);
  1699. }
  1700. if (orig_ref) {
  1701. SET_FRAME_REF(addr_ret);
  1702. }
  1703. }
  1704. HANDLE_OP_END();
  1705. }
  1706. #endif
  1707. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  1708. HANDLE_OP(WASM_OP_TABLE_GET)
  1709. {
  1710. uint32 tbl_idx, elem_idx;
  1711. WASMTableInstance *tbl_inst;
  1712. tbl_idx = read_uint32(frame_ip);
  1713. bh_assert(tbl_idx < module->table_count);
  1714. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1715. elem_idx = POP_I32();
  1716. if (elem_idx >= tbl_inst->cur_size) {
  1717. wasm_set_exception(module, "out of bounds table access");
  1718. goto got_exception;
  1719. }
  1720. #if WASM_ENABLE_GC == 0
  1721. PUSH_I32(tbl_inst->elems[elem_idx]);
  1722. #else
  1723. PUSH_REF(tbl_inst->elems[elem_idx]);
  1724. #endif
  1725. HANDLE_OP_END();
  1726. }
  1727. HANDLE_OP(WASM_OP_TABLE_SET)
  1728. {
  1729. uint32 tbl_idx, elem_idx;
  1730. WASMTableInstance *tbl_inst;
  1731. table_elem_type_t elem_val;
  1732. tbl_idx = read_uint32(frame_ip);
  1733. bh_assert(tbl_idx < module->table_count);
  1734. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1735. #if WASM_ENABLE_GC == 0
  1736. elem_val = POP_I32();
  1737. #else
  1738. elem_val = POP_REF();
  1739. #endif
  1740. elem_idx = POP_I32();
  1741. if (elem_idx >= tbl_inst->cur_size) {
  1742. wasm_set_exception(module, "out of bounds table access");
  1743. goto got_exception;
  1744. }
  1745. tbl_inst->elems[elem_idx] = elem_val;
  1746. HANDLE_OP_END();
  1747. }
  1748. HANDLE_OP(WASM_OP_REF_NULL)
  1749. {
  1750. #if WASM_ENABLE_GC == 0
  1751. PUSH_I32(NULL_REF);
  1752. #else
  1753. PUSH_REF(NULL_REF);
  1754. #endif
  1755. HANDLE_OP_END();
  1756. }
  1757. HANDLE_OP(WASM_OP_REF_IS_NULL)
  1758. {
  1759. #if WASM_ENABLE_GC == 0
  1760. uint32 ref_val;
  1761. ref_val = POP_I32();
  1762. #else
  1763. void *ref_val;
  1764. ref_val = POP_REF();
  1765. #endif
  1766. PUSH_I32(ref_val == NULL_REF ? 1 : 0);
  1767. HANDLE_OP_END();
  1768. }
  1769. HANDLE_OP(WASM_OP_REF_FUNC)
  1770. {
  1771. uint32 func_idx = read_uint32(frame_ip);
  1772. #if WASM_ENABLE_GC == 0
  1773. PUSH_I32(func_idx);
  1774. #else
  1775. SYNC_ALL_TO_FRAME();
  1776. if (!(gc_obj = wasm_create_func_obj(module, func_idx, true,
  1777. NULL, 0))) {
  1778. goto got_exception;
  1779. }
  1780. PUSH_REF(gc_obj);
  1781. #endif
  1782. HANDLE_OP_END();
  1783. }
  1784. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  1785. #if WASM_ENABLE_GC != 0
  1786. HANDLE_OP(WASM_OP_CALL_REF)
  1787. {
  1788. #if WASM_ENABLE_THREAD_MGR != 0
  1789. CHECK_SUSPEND_FLAGS();
  1790. #endif
  1791. func_obj = POP_REF();
  1792. if (!func_obj) {
  1793. wasm_set_exception(module, "null function object");
  1794. goto got_exception;
  1795. }
  1796. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  1797. cur_func = module->e->functions + fidx;
  1798. goto call_func_from_interp;
  1799. }
  1800. HANDLE_OP(WASM_OP_RETURN_CALL_REF)
  1801. {
  1802. #if WASM_ENABLE_THREAD_MGR != 0
  1803. CHECK_SUSPEND_FLAGS();
  1804. #endif
  1805. func_obj = POP_REF();
  1806. if (!func_obj) {
  1807. wasm_set_exception(module, "null function object");
  1808. goto got_exception;
  1809. }
  1810. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  1811. cur_func = module->e->functions + fidx;
  1812. goto call_func_from_return_call;
  1813. }
  1814. HANDLE_OP(WASM_OP_REF_AS_NON_NULL)
  1815. {
  1816. gc_obj = POP_REF();
  1817. if (gc_obj == NULL_REF) {
  1818. wasm_set_exception(module, "null reference");
  1819. goto got_exception;
  1820. }
  1821. PUSH_REF(gc_obj);
  1822. HANDLE_OP_END();
  1823. }
  1824. HANDLE_OP(WASM_OP_REF_EQ)
  1825. {
  1826. WASMObjectRef gc_obj1, gc_obj2;
  1827. gc_obj2 = POP_REF();
  1828. gc_obj1 = POP_REF();
  1829. val = wasm_obj_equal(gc_obj1, gc_obj2);
  1830. PUSH_I32(val);
  1831. HANDLE_OP_END();
  1832. }
  1833. HANDLE_OP(WASM_OP_BR_ON_NULL)
  1834. {
  1835. #if WASM_ENABLE_THREAD_MGR != 0
  1836. CHECK_SUSPEND_FLAGS();
  1837. #endif
  1838. opnd_off = GET_OFFSET();
  1839. gc_obj = GET_REF_FROM_ADDR(frame_lp + opnd_off);
  1840. if (gc_obj == NULL_REF) {
  1841. CLEAR_FRAME_REF(opnd_off);
  1842. goto recover_br_info;
  1843. }
  1844. else {
  1845. SKIP_BR_INFO();
  1846. }
  1847. HANDLE_OP_END();
  1848. }
  1849. HANDLE_OP(WASM_OP_BR_ON_NON_NULL)
  1850. {
  1851. #if WASM_ENABLE_THREAD_MGR != 0
  1852. CHECK_SUSPEND_FLAGS();
  1853. #endif
  1854. opnd_off = GET_OFFSET();
  1855. gc_obj = GET_REF_FROM_ADDR(frame_lp + opnd_off);
  1856. if (gc_obj != NULL_REF) {
  1857. goto recover_br_info;
  1858. }
  1859. else {
  1860. CLEAR_FRAME_REF(opnd_off);
  1861. SKIP_BR_INFO();
  1862. }
  1863. HANDLE_OP_END();
  1864. }
  1865. HANDLE_OP(WASM_OP_GC_PREFIX)
  1866. {
  1867. GET_OPCODE();
  1868. switch (opcode) {
  1869. case WASM_OP_STRUCT_NEW:
  1870. case WASM_OP_STRUCT_NEW_DEFAULT:
  1871. {
  1872. WASMModule *wasm_module = module->module;
  1873. WASMStructType *struct_type;
  1874. WASMRttType *rtt_type;
  1875. WASMValue field_value = { 0 };
  1876. type_idx = read_uint32(frame_ip);
  1877. struct_type =
  1878. (WASMStructType *)module->module->types[type_idx];
  1879. if (!(rtt_type = wasm_rtt_type_new(
  1880. (WASMType *)struct_type, type_idx,
  1881. wasm_module->rtt_types,
  1882. wasm_module->type_count,
  1883. &wasm_module->rtt_type_lock))) {
  1884. wasm_set_exception(module,
  1885. "create rtt type failed");
  1886. goto got_exception;
  1887. }
  1888. SYNC_ALL_TO_FRAME();
  1889. struct_obj = wasm_struct_obj_new(exec_env, rtt_type);
  1890. if (!struct_obj) {
  1891. wasm_set_exception(module,
  1892. "create struct object failed");
  1893. goto got_exception;
  1894. }
  1895. if (opcode == WASM_OP_STRUCT_NEW) {
  1896. WASMStructFieldType *fields = struct_type->fields;
  1897. int32 field_count = (int32)struct_type->field_count;
  1898. int32 field_idx;
  1899. uint8 field_type;
  1900. for (field_idx = field_count - 1; field_idx >= 0;
  1901. field_idx--) {
  1902. field_type = fields[field_idx].field_type;
  1903. if (wasm_is_type_reftype(field_type)) {
  1904. field_value.gc_obj = POP_REF();
  1905. }
  1906. else if (field_type == VALUE_TYPE_I32
  1907. || field_type == VALUE_TYPE_F32
  1908. || field_type == PACKED_TYPE_I8
  1909. || field_type == PACKED_TYPE_I16) {
  1910. field_value.i32 = POP_I32();
  1911. }
  1912. else {
  1913. field_value.i64 = POP_I64();
  1914. }
  1915. wasm_struct_obj_set_field(struct_obj, field_idx,
  1916. &field_value);
  1917. }
  1918. }
  1919. PUSH_REF(struct_obj);
  1920. HANDLE_OP_END();
  1921. }
  1922. case WASM_OP_STRUCT_GET:
  1923. case WASM_OP_STRUCT_GET_S:
  1924. case WASM_OP_STRUCT_GET_U:
  1925. {
  1926. WASMStructType *struct_type;
  1927. WASMValue field_value = { 0 };
  1928. uint32 field_idx;
  1929. uint8 field_type;
  1930. type_idx = read_uint32(frame_ip);
  1931. field_idx = read_uint32(frame_ip);
  1932. struct_type =
  1933. (WASMStructType *)module->module->types[type_idx];
  1934. struct_obj = POP_REF();
  1935. if (!struct_obj) {
  1936. wasm_set_exception(module, "null structure object");
  1937. goto got_exception;
  1938. }
  1939. wasm_struct_obj_get_field(
  1940. struct_obj, field_idx,
  1941. opcode == WASM_OP_STRUCT_GET_S ? true : false,
  1942. &field_value);
  1943. field_type = struct_type->fields[field_idx].field_type;
  1944. if (wasm_is_reftype_i31ref(field_type)) {
  1945. PUSH_I31REF(field_value.gc_obj);
  1946. }
  1947. else if (wasm_is_type_reftype(field_type)) {
  1948. PUSH_REF(field_value.gc_obj);
  1949. }
  1950. else if (field_type == VALUE_TYPE_I32
  1951. || field_type == VALUE_TYPE_F32
  1952. || field_type == PACKED_TYPE_I8
  1953. || field_type == PACKED_TYPE_I16) {
  1954. PUSH_I32(field_value.i32);
  1955. }
  1956. else {
  1957. PUSH_I64(field_value.i64);
  1958. }
  1959. HANDLE_OP_END();
  1960. }
  1961. case WASM_OP_STRUCT_SET:
  1962. {
  1963. WASMStructType *struct_type;
  1964. WASMValue field_value = { 0 };
  1965. uint32 field_idx;
  1966. uint8 field_type;
  1967. type_idx = read_uint32(frame_ip);
  1968. field_idx = read_uint32(frame_ip);
  1969. struct_type =
  1970. (WASMStructType *)module->module->types[type_idx];
  1971. field_type = struct_type->fields[field_idx].field_type;
  1972. if (wasm_is_type_reftype(field_type)) {
  1973. field_value.gc_obj = POP_REF();
  1974. }
  1975. else if (field_type == VALUE_TYPE_I32
  1976. || field_type == VALUE_TYPE_F32
  1977. || field_type == PACKED_TYPE_I8
  1978. || field_type == PACKED_TYPE_I16) {
  1979. field_value.i32 = POP_I32();
  1980. }
  1981. else {
  1982. field_value.i64 = POP_I64();
  1983. }
  1984. struct_obj = POP_REF();
  1985. if (!struct_obj) {
  1986. wasm_set_exception(module, "null structure object");
  1987. goto got_exception;
  1988. }
  1989. wasm_struct_obj_set_field(struct_obj, field_idx,
  1990. &field_value);
  1991. HANDLE_OP_END();
  1992. }
  1993. case WASM_OP_ARRAY_NEW:
  1994. case WASM_OP_ARRAY_NEW_DEFAULT:
  1995. case WASM_OP_ARRAY_NEW_FIXED:
  1996. {
  1997. WASMModule *wasm_module = module->module;
  1998. WASMArrayType *array_type;
  1999. WASMRttType *rtt_type;
  2000. WASMValue array_elem = { 0 };
  2001. uint32 array_len, i;
  2002. type_idx = read_uint32(frame_ip);
  2003. array_type =
  2004. (WASMArrayType *)wasm_module->types[type_idx];
  2005. if (!(rtt_type = wasm_rtt_type_new(
  2006. (WASMType *)array_type, type_idx,
  2007. wasm_module->rtt_types,
  2008. wasm_module->type_count,
  2009. &wasm_module->rtt_type_lock))) {
  2010. wasm_set_exception(module,
  2011. "create rtt type failed");
  2012. goto got_exception;
  2013. }
  2014. if (opcode != WASM_OP_ARRAY_NEW_FIXED)
  2015. array_len = POP_I32();
  2016. else
  2017. array_len = read_uint32(frame_ip);
  2018. if (opcode == WASM_OP_ARRAY_NEW) {
  2019. if (wasm_is_type_reftype(array_type->elem_type)) {
  2020. array_elem.gc_obj = POP_REF();
  2021. }
  2022. else if (array_type->elem_type == VALUE_TYPE_I32
  2023. || array_type->elem_type == VALUE_TYPE_F32
  2024. || array_type->elem_type == PACKED_TYPE_I8
  2025. || array_type->elem_type
  2026. == PACKED_TYPE_I16) {
  2027. array_elem.i32 = POP_I32();
  2028. }
  2029. else {
  2030. array_elem.i64 = POP_I64();
  2031. }
  2032. }
  2033. SYNC_ALL_TO_FRAME();
  2034. array_obj = wasm_array_obj_new(exec_env, rtt_type,
  2035. array_len, &array_elem);
  2036. if (!array_obj) {
  2037. wasm_set_exception(module,
  2038. "create array object failed");
  2039. goto got_exception;
  2040. }
  2041. if (opcode == WASM_OP_ARRAY_NEW_FIXED) {
  2042. for (i = 0; i < array_len; i++) {
  2043. if (wasm_is_type_reftype(
  2044. array_type->elem_type)) {
  2045. array_elem.gc_obj = POP_REF();
  2046. }
  2047. else if (array_type->elem_type == VALUE_TYPE_I32
  2048. || array_type->elem_type
  2049. == VALUE_TYPE_F32
  2050. || array_type->elem_type
  2051. == PACKED_TYPE_I8
  2052. || array_type->elem_type
  2053. == PACKED_TYPE_I16) {
  2054. array_elem.i32 = POP_I32();
  2055. }
  2056. else {
  2057. array_elem.i64 = POP_I64();
  2058. }
  2059. wasm_array_obj_set_elem(
  2060. array_obj, array_len - 1 - i, &array_elem);
  2061. }
  2062. }
  2063. PUSH_REF(array_obj);
  2064. HANDLE_OP_END();
  2065. }
  2066. case WASM_OP_ARRAY_NEW_DATA:
  2067. {
  2068. WASMModule *wasm_module = module->module;
  2069. WASMArrayType *array_type;
  2070. WASMRttType *rtt_type;
  2071. WASMValue array_elem = { 0 };
  2072. WASMDataSeg *data_seg;
  2073. uint8 *array_elem_base;
  2074. uint32 array_len, data_seg_idx, data_seg_offset;
  2075. uint32 elem_size = 0;
  2076. uint64 total_size;
  2077. type_idx = read_uint32(frame_ip);
  2078. data_seg_idx = read_uint32(frame_ip);
  2079. data_seg = wasm_module->data_segments[data_seg_idx];
  2080. array_type =
  2081. (WASMArrayType *)wasm_module->types[type_idx];
  2082. if (!(rtt_type = wasm_rtt_type_new(
  2083. (WASMType *)array_type, type_idx,
  2084. wasm_module->rtt_types,
  2085. wasm_module->type_count,
  2086. &wasm_module->rtt_type_lock))) {
  2087. wasm_set_exception(module,
  2088. "create rtt type failed");
  2089. goto got_exception;
  2090. }
  2091. array_len = POP_I32();
  2092. data_seg_offset = POP_I32();
  2093. switch (array_type->elem_type) {
  2094. case PACKED_TYPE_I8:
  2095. elem_size = 1;
  2096. break;
  2097. case PACKED_TYPE_I16:
  2098. elem_size = 2;
  2099. break;
  2100. case VALUE_TYPE_I32:
  2101. case VALUE_TYPE_F32:
  2102. elem_size = 4;
  2103. break;
  2104. case VALUE_TYPE_I64:
  2105. case VALUE_TYPE_F64:
  2106. elem_size = 8;
  2107. break;
  2108. default:
  2109. bh_assert(0);
  2110. }
  2111. total_size = (uint64)elem_size * array_len;
  2112. if (data_seg_offset >= data_seg->data_length
  2113. || total_size
  2114. > data_seg->data_length - data_seg_offset) {
  2115. wasm_set_exception(module,
  2116. "data segment out of bounds");
  2117. goto got_exception;
  2118. }
  2119. SYNC_ALL_TO_FRAME();
  2120. array_obj = wasm_array_obj_new(exec_env, rtt_type,
  2121. array_len, &array_elem);
  2122. if (!array_obj) {
  2123. wasm_set_exception(module,
  2124. "create array object failed");
  2125. goto got_exception;
  2126. }
  2127. array_elem_base =
  2128. (uint8 *)wasm_array_obj_first_elem_addr(array_obj);
  2129. bh_memcpy_s(array_elem_base, (uint32)total_size,
  2130. data_seg->data + data_seg_offset,
  2131. (uint32)total_size);
  2132. PUSH_REF(array_obj);
  2133. HANDLE_OP_END();
  2134. }
  2135. case WASM_OP_ARRAY_NEW_ELEM:
  2136. {
  2137. /* TODO */
  2138. wasm_set_exception(module, "unsupported opcode");
  2139. goto got_exception;
  2140. }
  2141. case WASM_OP_ARRAY_GET:
  2142. case WASM_OP_ARRAY_GET_S:
  2143. case WASM_OP_ARRAY_GET_U:
  2144. {
  2145. WASMArrayType *array_type;
  2146. WASMValue array_elem = { 0 };
  2147. uint32 elem_idx, elem_size_log;
  2148. type_idx = read_uint32(frame_ip);
  2149. array_type =
  2150. (WASMArrayType *)module->module->types[type_idx];
  2151. elem_idx = POP_I32();
  2152. array_obj = POP_REF();
  2153. if (!array_obj) {
  2154. wasm_set_exception(module, "null array reference");
  2155. goto got_exception;
  2156. }
  2157. if (elem_idx >= wasm_array_obj_length(array_obj)) {
  2158. wasm_set_exception(module,
  2159. "out of bounds array access");
  2160. goto got_exception;
  2161. }
  2162. wasm_array_obj_get_elem(
  2163. array_obj, elem_idx,
  2164. opcode == WASM_OP_ARRAY_GET_S ? true : false,
  2165. &array_elem);
  2166. elem_size_log = wasm_array_obj_elem_size_log(array_obj);
  2167. if (wasm_is_reftype_i31ref(array_type->elem_type)) {
  2168. PUSH_I31REF(array_elem.gc_obj);
  2169. }
  2170. else if (wasm_is_type_reftype(array_type->elem_type)) {
  2171. PUSH_REF(array_elem.gc_obj);
  2172. }
  2173. else if (elem_size_log < 3) {
  2174. PUSH_I32(array_elem.i32);
  2175. }
  2176. else {
  2177. PUSH_I64(array_elem.i64);
  2178. }
  2179. HANDLE_OP_END();
  2180. }
  2181. case WASM_OP_ARRAY_SET:
  2182. {
  2183. WASMArrayType *array_type;
  2184. WASMValue array_elem = { 0 };
  2185. uint32 elem_idx;
  2186. type_idx = read_uint32(frame_ip);
  2187. array_type =
  2188. (WASMArrayType *)module->module->types[type_idx];
  2189. if (wasm_is_type_reftype(array_type->elem_type)) {
  2190. array_elem.gc_obj = POP_REF();
  2191. }
  2192. else if (array_type->elem_type == VALUE_TYPE_I32
  2193. || array_type->elem_type == VALUE_TYPE_F32
  2194. || array_type->elem_type == PACKED_TYPE_I8
  2195. || array_type->elem_type == PACKED_TYPE_I16) {
  2196. array_elem.i32 = POP_I32();
  2197. }
  2198. else {
  2199. array_elem.i64 = POP_I64();
  2200. }
  2201. elem_idx = POP_I32();
  2202. array_obj = POP_REF();
  2203. if (!array_obj) {
  2204. wasm_set_exception(module, "null array reference");
  2205. goto got_exception;
  2206. }
  2207. if (elem_idx >= wasm_array_obj_length(array_obj)) {
  2208. wasm_set_exception(module,
  2209. "out of bounds array access");
  2210. goto got_exception;
  2211. }
  2212. wasm_array_obj_set_elem(array_obj, elem_idx,
  2213. &array_elem);
  2214. HANDLE_OP_END();
  2215. }
  2216. case WASM_OP_ARRAY_LEN:
  2217. {
  2218. uint32 array_len;
  2219. array_obj = POP_REF();
  2220. if (!array_obj) {
  2221. wasm_set_exception(module, "null array reference");
  2222. goto got_exception;
  2223. }
  2224. array_len = wasm_array_obj_length(array_obj);
  2225. PUSH_I32(array_len);
  2226. HANDLE_OP_END();
  2227. }
  2228. case WASM_OP_ARRAY_FILL:
  2229. {
  2230. WASMArrayType *array_type;
  2231. WASMValue fill_value = { 0 };
  2232. uint32 start_offset, len;
  2233. type_idx = read_uint32(frame_ip);
  2234. array_type =
  2235. (WASMArrayType *)module->module->types[type_idx];
  2236. len = POP_I32();
  2237. if (wasm_is_type_reftype(array_type->elem_type)) {
  2238. fill_value.gc_obj = POP_REF();
  2239. }
  2240. else if (array_type->elem_type == VALUE_TYPE_I32
  2241. || array_type->elem_type == VALUE_TYPE_F32
  2242. || array_type->elem_type == PACKED_TYPE_I8
  2243. || array_type->elem_type == PACKED_TYPE_I16) {
  2244. fill_value.i32 = POP_I32();
  2245. }
  2246. else {
  2247. fill_value.i64 = POP_I64();
  2248. }
  2249. start_offset = POP_I32();
  2250. array_obj = POP_REF();
  2251. if (!array_obj) {
  2252. wasm_set_exception(module, "null array reference");
  2253. goto got_exception;
  2254. }
  2255. if (len > 0) {
  2256. if ((uint64)start_offset + len
  2257. >= wasm_array_obj_length(array_obj)) {
  2258. wasm_set_exception(
  2259. module, "out of bounds array access");
  2260. goto got_exception;
  2261. }
  2262. wasm_array_obj_fill(array_obj, start_offset, len,
  2263. &fill_value);
  2264. }
  2265. HANDLE_OP_END();
  2266. }
  2267. case WASM_OP_ARRAY_COPY:
  2268. {
  2269. uint32 dst_offset, src_offset, len, src_type_index;
  2270. WASMArrayObjectRef src_obj, dst_obj;
  2271. type_idx = read_uint32(frame_ip);
  2272. src_type_index = read_uint32(frame_ip);
  2273. len = POP_I32();
  2274. src_offset = POP_I32();
  2275. src_obj = POP_REF();
  2276. dst_offset = POP_I32();
  2277. dst_obj = POP_REF();
  2278. if (!src_obj || !dst_obj) {
  2279. wasm_set_exception(module, "null array reference");
  2280. goto got_exception;
  2281. }
  2282. if (len > 0) {
  2283. if ((dst_offset > UINT32_MAX - len)
  2284. || (dst_offset + len
  2285. > wasm_array_obj_length(dst_obj))
  2286. || (src_offset > UINT32_MAX - len)
  2287. || (src_offset + len
  2288. > wasm_array_obj_length(src_obj))) {
  2289. wasm_set_exception(
  2290. module, "out of bounds array access");
  2291. goto got_exception;
  2292. }
  2293. wasm_array_obj_copy(dst_obj, dst_offset, src_obj,
  2294. src_offset, len);
  2295. }
  2296. (void)src_type_index;
  2297. HANDLE_OP_END();
  2298. }
  2299. case WASM_OP_REF_I31:
  2300. {
  2301. uint32 i31_val;
  2302. i31_val = POP_I32();
  2303. i31_obj = wasm_i31_obj_new(i31_val);
  2304. PUSH_I31REF(i31_obj);
  2305. HANDLE_OP_END();
  2306. }
  2307. case WASM_OP_I31_GET_S:
  2308. case WASM_OP_I31_GET_U:
  2309. {
  2310. uint32 i31_val;
  2311. i31_obj = (WASMI31ObjectRef)POP_REF();
  2312. if (!i31_obj) {
  2313. wasm_set_exception(module, "null i31 reference");
  2314. goto got_exception;
  2315. }
  2316. i31_val = (uint32)(((uintptr_t)i31_obj) >> 1);
  2317. if (opcode == WASM_OP_I31_GET_S
  2318. && (i31_val & 0x40000000) /* bit 30 is 1 */)
  2319. /* set bit 31 to 1 */
  2320. i31_val |= 0x80000000;
  2321. PUSH_I32(i31_val);
  2322. HANDLE_OP_END();
  2323. }
  2324. case WASM_OP_REF_TEST:
  2325. case WASM_OP_REF_CAST:
  2326. case WASM_OP_REF_TEST_NULLABLE:
  2327. case WASM_OP_REF_CAST_NULLABLE:
  2328. {
  2329. int32 heap_type;
  2330. heap_type = (int32)read_uint32(frame_ip);
  2331. gc_obj = POP_REF();
  2332. if (!gc_obj) {
  2333. if (opcode == WASM_OP_REF_TEST
  2334. || opcode == WASM_OP_REF_TEST_NULLABLE) {
  2335. if (opcode == WASM_OP_REF_TEST)
  2336. PUSH_I32(0);
  2337. else
  2338. PUSH_I32(1);
  2339. }
  2340. else if (opcode == WASM_OP_REF_CAST) {
  2341. wasm_set_exception(module, "cast failure");
  2342. goto got_exception;
  2343. }
  2344. else {
  2345. PUSH_REF(gc_obj);
  2346. }
  2347. }
  2348. else {
  2349. bool castable = false;
  2350. if (heap_type >= 0) {
  2351. WASMModule *wasm_module = module->module;
  2352. castable = wasm_obj_is_instance_of(
  2353. gc_obj, (uint32)heap_type,
  2354. wasm_module->types,
  2355. wasm_module->type_count);
  2356. }
  2357. else {
  2358. castable =
  2359. wasm_obj_is_type_of(gc_obj, heap_type);
  2360. }
  2361. if (opcode == WASM_OP_REF_TEST
  2362. || opcode == WASM_OP_REF_TEST_NULLABLE) {
  2363. if (castable)
  2364. PUSH_I32(1);
  2365. else
  2366. PUSH_I32(0);
  2367. }
  2368. else if (!castable) {
  2369. wasm_set_exception(module, "cast failure");
  2370. goto got_exception;
  2371. }
  2372. else {
  2373. PUSH_REF(gc_obj);
  2374. }
  2375. }
  2376. HANDLE_OP_END();
  2377. }
  2378. case WASM_OP_BR_ON_CAST:
  2379. case WASM_OP_BR_ON_CAST_FAIL:
  2380. {
  2381. int32 heap_type, heap_type_dst;
  2382. uint8 castflags;
  2383. uint16 opnd_off_br;
  2384. #if WASM_ENABLE_THREAD_MGR != 0
  2385. CHECK_SUSPEND_FLAGS();
  2386. #endif
  2387. castflags = *frame_ip++;
  2388. heap_type = (int32)read_uint32(frame_ip);
  2389. heap_type_dst = (int32)read_uint32(frame_ip);
  2390. opnd_off = GET_OFFSET();
  2391. opnd_off_br = GET_OFFSET();
  2392. gc_obj = GET_REF_FROM_ADDR(frame_lp + opnd_off);
  2393. PUT_REF_TO_ADDR(frame_lp + opnd_off_br, gc_obj);
  2394. if (!gc_obj) {
  2395. /*
  2396. * castflags should be 0~3:
  2397. * 0: (non-null, non-null)
  2398. * 1: (null, non-null)
  2399. * 2: (non-null, null)
  2400. * 3: (null, null)
  2401. */
  2402. if (
  2403. /* op is BR_ON_CAST and dst reftype is nullable
  2404. */
  2405. ((opcode == WASM_OP_BR_ON_CAST)
  2406. && ((castflags == 2) || (castflags == 3)))
  2407. /* op is BR_ON_CAST_FAIL and dst reftype is
  2408. non-nullable */
  2409. || ((opcode == WASM_OP_BR_ON_CAST_FAIL)
  2410. && ((castflags == 0)
  2411. || (castflags == 1)))) {
  2412. CLEAR_FRAME_REF(opnd_off);
  2413. if (!wasm_is_reftype_i31ref(heap_type)) {
  2414. SET_FRAME_REF(opnd_off_br);
  2415. }
  2416. goto recover_br_info;
  2417. }
  2418. }
  2419. else {
  2420. bool castable = false;
  2421. if (heap_type_dst >= 0) {
  2422. WASMModule *wasm_module = module->module;
  2423. castable = wasm_obj_is_instance_of(
  2424. gc_obj, (uint32)heap_type_dst,
  2425. wasm_module->types,
  2426. wasm_module->type_count);
  2427. }
  2428. else {
  2429. castable =
  2430. wasm_obj_is_type_of(gc_obj, heap_type_dst);
  2431. }
  2432. if ((castable && (opcode == WASM_OP_BR_ON_CAST))
  2433. || (!castable
  2434. && (opcode == WASM_OP_BR_ON_CAST_FAIL))) {
  2435. CLEAR_FRAME_REF(opnd_off);
  2436. if (!wasm_is_reftype_i31ref(heap_type)) {
  2437. SET_FRAME_REF(opnd_off_br);
  2438. }
  2439. goto recover_br_info;
  2440. }
  2441. }
  2442. SKIP_BR_INFO();
  2443. (void)heap_type_dst;
  2444. HANDLE_OP_END();
  2445. }
  2446. case WASM_OP_ANY_CONVERT_EXTERN:
  2447. {
  2448. externref_obj = POP_REF();
  2449. if (externref_obj == NULL_REF)
  2450. PUSH_REF(NULL_REF);
  2451. else {
  2452. gc_obj = wasm_externref_obj_to_internal_obj(
  2453. externref_obj);
  2454. PUSH_REF(gc_obj);
  2455. }
  2456. HANDLE_OP_END();
  2457. }
  2458. case WASM_OP_EXTERN_CONVERT_ANY:
  2459. {
  2460. gc_obj = POP_REF();
  2461. if (gc_obj == NULL_REF)
  2462. PUSH_REF(NULL_REF);
  2463. else {
  2464. if (!(externref_obj =
  2465. wasm_internal_obj_to_externref_obj(
  2466. exec_env, gc_obj))) {
  2467. wasm_set_exception(
  2468. module, "create externref object failed");
  2469. goto got_exception;
  2470. }
  2471. PUSH_REF(externref_obj);
  2472. }
  2473. HANDLE_OP_END();
  2474. }
  2475. #if WASM_ENABLE_STRINGREF != 0
  2476. case WASM_OP_STRING_NEW_UTF8:
  2477. case WASM_OP_STRING_NEW_WTF16:
  2478. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  2479. case WASM_OP_STRING_NEW_WTF8:
  2480. {
  2481. uint32 mem_idx, addr, bytes_length, offset = 0;
  2482. EncodingFlag flag = WTF8;
  2483. mem_idx = (uint32)read_uint32(frame_ip);
  2484. bytes_length = POP_I32();
  2485. addr = POP_I32();
  2486. CHECK_MEMORY_OVERFLOW(bytes_length);
  2487. if (opcode == WASM_OP_STRING_NEW_WTF16) {
  2488. flag = WTF16;
  2489. }
  2490. else if (opcode == WASM_OP_STRING_NEW_UTF8) {
  2491. flag = UTF8;
  2492. }
  2493. else if (opcode == WASM_OP_STRING_NEW_LOSSY_UTF8) {
  2494. flag = LOSSY_UTF8;
  2495. }
  2496. else if (opcode == WASM_OP_STRING_NEW_WTF8) {
  2497. flag = WTF8;
  2498. }
  2499. str_obj = wasm_string_new_with_encoding(
  2500. maddr, bytes_length, flag);
  2501. if (!str_obj) {
  2502. wasm_set_exception(module,
  2503. "create string object failed");
  2504. goto got_exception;
  2505. }
  2506. SYNC_ALL_TO_FRAME();
  2507. stringref_obj =
  2508. wasm_stringref_obj_new(exec_env, str_obj);
  2509. if (!stringref_obj) {
  2510. wasm_set_exception(module,
  2511. "create stringref failed");
  2512. goto got_exception;
  2513. }
  2514. PUSH_REF(stringref_obj);
  2515. (void)mem_idx;
  2516. HANDLE_OP_END();
  2517. }
  2518. case WASM_OP_STRING_CONST:
  2519. {
  2520. WASMModule *wasm_module = module->module;
  2521. uint32 contents;
  2522. contents = (uint32)read_uint32(frame_ip);
  2523. str_obj = wasm_string_new_const(
  2524. (const char *)
  2525. wasm_module->string_literal_ptrs[contents],
  2526. wasm_module->string_literal_lengths[contents]);
  2527. if (!str_obj) {
  2528. wasm_set_exception(module,
  2529. "create string object failed");
  2530. goto got_exception;
  2531. }
  2532. SYNC_ALL_TO_FRAME();
  2533. stringref_obj =
  2534. wasm_stringref_obj_new(exec_env, str_obj);
  2535. if (!str_obj) {
  2536. wasm_set_exception(module,
  2537. "create stringref failed");
  2538. goto got_exception;
  2539. }
  2540. PUSH_REF(stringref_obj);
  2541. HANDLE_OP_END();
  2542. }
  2543. case WASM_OP_STRING_MEASURE_UTF8:
  2544. case WASM_OP_STRING_MEASURE_WTF8:
  2545. case WASM_OP_STRING_MEASURE_WTF16:
  2546. {
  2547. int32 target_bytes_length;
  2548. EncodingFlag flag = WTF8;
  2549. stringref_obj = POP_REF();
  2550. if (opcode == WASM_OP_STRING_MEASURE_WTF16) {
  2551. flag = WTF16;
  2552. }
  2553. else if (opcode == WASM_OP_STRING_MEASURE_UTF8) {
  2554. flag = UTF8;
  2555. }
  2556. else if (opcode == WASM_OP_STRING_MEASURE_WTF8) {
  2557. flag = LOSSY_UTF8;
  2558. }
  2559. target_bytes_length = wasm_string_measure(
  2560. (WASMString)wasm_stringref_obj_get_value(
  2561. stringref_obj),
  2562. flag);
  2563. PUSH_I32(target_bytes_length);
  2564. HANDLE_OP_END();
  2565. }
  2566. case WASM_OP_STRING_ENCODE_UTF8:
  2567. case WASM_OP_STRING_ENCODE_WTF16:
  2568. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  2569. case WASM_OP_STRING_ENCODE_WTF8:
  2570. {
  2571. uint32 mem_idx, addr;
  2572. int32 target_bytes_length;
  2573. WASMMemoryInstance *memory_inst;
  2574. EncodingFlag flag = WTF8;
  2575. mem_idx = (uint32)read_uint32(frame_ip);
  2576. addr = POP_I32();
  2577. stringref_obj = POP_REF();
  2578. str_obj = (WASMString)wasm_stringref_obj_get_value(
  2579. stringref_obj);
  2580. #if WASM_ENABLE_SHARED_HEAP != 0
  2581. if (app_addr_in_shared_heap((uint64)addr, 1))
  2582. shared_heap_addr_app_to_native((uint64)addr, maddr);
  2583. else
  2584. #endif
  2585. {
  2586. memory_inst = module->memories[mem_idx];
  2587. maddr = memory_inst->memory_data + addr;
  2588. }
  2589. if (opcode == WASM_OP_STRING_ENCODE_WTF16) {
  2590. flag = WTF16;
  2591. count = wasm_string_measure(str_obj, flag);
  2592. target_bytes_length = wasm_string_encode(
  2593. str_obj, 0, count, maddr, NULL, flag);
  2594. }
  2595. else {
  2596. if (opcode == WASM_OP_STRING_ENCODE_UTF8) {
  2597. flag = UTF8;
  2598. }
  2599. else if (opcode
  2600. == WASM_OP_STRING_ENCODE_LOSSY_UTF8) {
  2601. flag = LOSSY_UTF8;
  2602. }
  2603. else if (opcode == WASM_OP_STRING_ENCODE_WTF8) {
  2604. flag = WTF8;
  2605. }
  2606. count = wasm_string_measure(str_obj, flag);
  2607. target_bytes_length = wasm_string_encode(
  2608. str_obj, 0, count, maddr, NULL, flag);
  2609. if (target_bytes_length == -1) {
  2610. wasm_set_exception(
  2611. module, "isolated surrogate is seen");
  2612. goto got_exception;
  2613. }
  2614. }
  2615. if (target_bytes_length < 0) {
  2616. wasm_set_exception(module,
  2617. "stringref encode failed");
  2618. goto got_exception;
  2619. }
  2620. PUSH_I32(target_bytes_length);
  2621. HANDLE_OP_END();
  2622. }
  2623. case WASM_OP_STRING_CONCAT:
  2624. {
  2625. WASMStringrefObjectRef stringref_obj1, stringref_obj2;
  2626. stringref_obj2 = POP_REF();
  2627. stringref_obj1 = POP_REF();
  2628. str_obj = wasm_string_concat(
  2629. (WASMString)wasm_stringref_obj_get_value(
  2630. stringref_obj1),
  2631. (WASMString)wasm_stringref_obj_get_value(
  2632. stringref_obj2));
  2633. if (!str_obj) {
  2634. wasm_set_exception(module,
  2635. "create string object failed");
  2636. goto got_exception;
  2637. }
  2638. SYNC_ALL_TO_FRAME();
  2639. stringref_obj =
  2640. wasm_stringref_obj_new(exec_env, str_obj);
  2641. if (!stringref_obj) {
  2642. wasm_set_exception(module,
  2643. "create stringref failed");
  2644. goto got_exception;
  2645. }
  2646. PUSH_REF(stringref_obj);
  2647. HANDLE_OP_END();
  2648. }
  2649. case WASM_OP_STRING_EQ:
  2650. {
  2651. WASMStringrefObjectRef stringref_obj1, stringref_obj2;
  2652. int32 is_eq;
  2653. stringref_obj2 = POP_REF();
  2654. stringref_obj1 = POP_REF();
  2655. is_eq = wasm_string_eq(
  2656. (WASMString)wasm_stringref_obj_get_value(
  2657. stringref_obj1),
  2658. (WASMString)wasm_stringref_obj_get_value(
  2659. stringref_obj2));
  2660. PUSH_I32(is_eq);
  2661. HANDLE_OP_END();
  2662. }
  2663. case WASM_OP_STRING_IS_USV_SEQUENCE:
  2664. {
  2665. int32 is_usv_sequence;
  2666. stringref_obj = POP_REF();
  2667. is_usv_sequence = wasm_string_is_usv_sequence(
  2668. (WASMString)wasm_stringref_obj_get_value(
  2669. stringref_obj));
  2670. PUSH_I32(is_usv_sequence);
  2671. HANDLE_OP_END();
  2672. }
  2673. case WASM_OP_STRING_AS_WTF8:
  2674. {
  2675. stringref_obj = POP_REF();
  2676. str_obj = wasm_string_create_view(
  2677. (WASMString)wasm_stringref_obj_get_value(
  2678. stringref_obj),
  2679. STRING_VIEW_WTF8);
  2680. if (!str_obj) {
  2681. wasm_set_exception(module,
  2682. "create string object failed");
  2683. goto got_exception;
  2684. }
  2685. SYNC_ALL_TO_FRAME();
  2686. stringview_wtf8_obj =
  2687. wasm_stringview_wtf8_obj_new(exec_env, str_obj);
  2688. if (!stringview_wtf8_obj) {
  2689. wasm_set_exception(module,
  2690. "create stringview wtf8 failed");
  2691. goto got_exception;
  2692. }
  2693. PUSH_REF(stringview_wtf8_obj);
  2694. HANDLE_OP_END();
  2695. }
  2696. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  2697. {
  2698. uint32 next_pos, bytes, pos;
  2699. bytes = POP_I32();
  2700. pos = POP_I32();
  2701. stringview_wtf8_obj = POP_REF();
  2702. next_pos = wasm_string_advance(
  2703. (WASMString)wasm_stringview_wtf8_obj_get_value(
  2704. stringview_wtf8_obj),
  2705. pos, bytes, NULL);
  2706. PUSH_I32(next_pos);
  2707. HANDLE_OP_END();
  2708. }
  2709. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  2710. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  2711. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  2712. {
  2713. uint32 mem_idx, addr, pos, bytes, next_pos;
  2714. int32 bytes_written;
  2715. WASMMemoryInstance *memory_inst;
  2716. EncodingFlag flag = WTF8;
  2717. if (opcode == WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8) {
  2718. flag = UTF8;
  2719. }
  2720. else if (opcode
  2721. == WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8) {
  2722. flag = LOSSY_UTF8;
  2723. }
  2724. else if (opcode
  2725. == WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8) {
  2726. flag = WTF8;
  2727. }
  2728. mem_idx = (uint32)read_uint32(frame_ip);
  2729. bytes = POP_I32();
  2730. pos = POP_I32();
  2731. addr = POP_I32();
  2732. stringview_wtf8_obj = POP_REF();
  2733. #if WASM_ENABLE_SHARED_HEAP != 0
  2734. if (app_addr_in_shared_heap((uint64)addr, 1))
  2735. shared_heap_addr_app_to_native((uint64)addr, maddr);
  2736. else
  2737. #endif
  2738. {
  2739. memory_inst = module->memories[mem_idx];
  2740. maddr = memory_inst->memory_data + addr;
  2741. }
  2742. bytes_written = wasm_string_encode(
  2743. (WASMString)wasm_stringview_wtf8_obj_get_value(
  2744. stringview_wtf8_obj),
  2745. pos, bytes, maddr, &next_pos, flag);
  2746. if (bytes_written < 0) {
  2747. if (bytes_written == Isolated_Surrogate) {
  2748. wasm_set_exception(
  2749. module, "isolated surrogate is seen");
  2750. }
  2751. else {
  2752. wasm_set_exception(module, "encode failed");
  2753. }
  2754. goto got_exception;
  2755. }
  2756. PUSH_I32(next_pos);
  2757. PUSH_I32(bytes_written);
  2758. HANDLE_OP_END();
  2759. }
  2760. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  2761. {
  2762. uint32 start, end;
  2763. end = POP_I32();
  2764. start = POP_I32();
  2765. stringview_wtf8_obj = POP_REF();
  2766. str_obj = wasm_string_slice(
  2767. (WASMString)wasm_stringview_wtf8_obj_get_value(
  2768. stringview_wtf8_obj),
  2769. start, end, STRING_VIEW_WTF8);
  2770. if (!str_obj) {
  2771. wasm_set_exception(module,
  2772. "create string object failed");
  2773. goto got_exception;
  2774. }
  2775. SYNC_ALL_TO_FRAME();
  2776. stringref_obj =
  2777. wasm_stringref_obj_new(exec_env, str_obj);
  2778. if (!stringref_obj) {
  2779. wasm_set_exception(module,
  2780. "create stringref failed");
  2781. goto got_exception;
  2782. }
  2783. PUSH_REF(stringref_obj);
  2784. HANDLE_OP_END();
  2785. }
  2786. case WASM_OP_STRING_AS_WTF16:
  2787. {
  2788. stringref_obj = POP_REF();
  2789. str_obj = wasm_string_create_view(
  2790. (WASMString)wasm_stringref_obj_get_value(
  2791. stringref_obj),
  2792. STRING_VIEW_WTF16);
  2793. if (!str_obj) {
  2794. wasm_set_exception(module,
  2795. "create string object failed");
  2796. goto got_exception;
  2797. }
  2798. SYNC_ALL_TO_FRAME();
  2799. stringview_wtf16_obj =
  2800. wasm_stringview_wtf16_obj_new(exec_env, str_obj);
  2801. if (!stringview_wtf16_obj) {
  2802. wasm_set_exception(
  2803. module, "create stringview wtf16 failed");
  2804. goto got_exception;
  2805. }
  2806. PUSH_REF(stringview_wtf16_obj);
  2807. HANDLE_OP_END();
  2808. }
  2809. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  2810. {
  2811. int32 code_units_length;
  2812. stringview_wtf16_obj = POP_REF();
  2813. code_units_length = wasm_string_wtf16_get_length(
  2814. (WASMString)wasm_stringview_wtf16_obj_get_value(
  2815. stringview_wtf16_obj));
  2816. PUSH_I32(code_units_length);
  2817. HANDLE_OP_END();
  2818. }
  2819. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  2820. {
  2821. int32 pos;
  2822. uint32 code_unit;
  2823. pos = POP_I32();
  2824. stringview_wtf16_obj = POP_REF();
  2825. code_unit = (uint32)wasm_string_get_wtf16_codeunit(
  2826. (WASMString)wasm_stringview_wtf16_obj_get_value(
  2827. stringview_wtf16_obj),
  2828. pos);
  2829. PUSH_I32(code_unit);
  2830. HANDLE_OP_END();
  2831. }
  2832. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  2833. {
  2834. uint32 mem_idx, addr, pos, len, offset = 0;
  2835. int32 written_code_units = 0;
  2836. mem_idx = (uint32)read_uint32(frame_ip);
  2837. len = POP_I32();
  2838. pos = POP_I32();
  2839. addr = POP_I32();
  2840. stringview_wtf16_obj = POP_REF();
  2841. CHECK_MEMORY_OVERFLOW(len * sizeof(uint16));
  2842. /* check 2-byte alignment */
  2843. if (((uintptr_t)maddr & (((uintptr_t)1 << 2) - 1))
  2844. != 0) {
  2845. wasm_set_exception(module,
  2846. "unaligned memory access");
  2847. goto got_exception;
  2848. }
  2849. written_code_units = wasm_string_encode(
  2850. (WASMString)wasm_stringview_wtf16_obj_get_value(
  2851. stringview_wtf16_obj),
  2852. pos, len, maddr, NULL, WTF16);
  2853. PUSH_I32(written_code_units);
  2854. (void)mem_idx;
  2855. HANDLE_OP_END();
  2856. }
  2857. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  2858. {
  2859. uint32 start, end;
  2860. end = POP_I32();
  2861. start = POP_I32();
  2862. stringview_wtf16_obj = POP_REF();
  2863. str_obj = wasm_string_slice(
  2864. (WASMString)wasm_stringview_wtf16_obj_get_value(
  2865. stringview_wtf16_obj),
  2866. start, end, STRING_VIEW_WTF16);
  2867. if (!str_obj) {
  2868. wasm_set_exception(module,
  2869. "create string object failed");
  2870. goto got_exception;
  2871. }
  2872. SYNC_ALL_TO_FRAME();
  2873. stringref_obj =
  2874. wasm_stringref_obj_new(exec_env, str_obj);
  2875. if (!stringref_obj) {
  2876. wasm_set_exception(module,
  2877. "create stringref failed");
  2878. goto got_exception;
  2879. }
  2880. PUSH_REF(stringref_obj);
  2881. HANDLE_OP_END();
  2882. }
  2883. case WASM_OP_STRING_AS_ITER:
  2884. {
  2885. stringref_obj = POP_REF();
  2886. str_obj = wasm_string_create_view(
  2887. (WASMString)wasm_stringref_obj_get_value(
  2888. stringref_obj),
  2889. STRING_VIEW_ITER);
  2890. if (!str_obj) {
  2891. wasm_set_exception(module,
  2892. "create string object failed");
  2893. goto got_exception;
  2894. }
  2895. SYNC_ALL_TO_FRAME();
  2896. stringview_iter_obj =
  2897. wasm_stringview_iter_obj_new(exec_env, str_obj, 0);
  2898. if (!stringview_iter_obj) {
  2899. wasm_set_exception(module,
  2900. "create stringview iter failed");
  2901. goto got_exception;
  2902. }
  2903. PUSH_REF(stringview_iter_obj);
  2904. HANDLE_OP_END();
  2905. }
  2906. case WASM_OP_STRINGVIEW_ITER_NEXT:
  2907. {
  2908. uint32 code_point;
  2909. stringview_iter_obj = POP_REF();
  2910. code_point = wasm_string_next_codepoint(
  2911. (WASMString)wasm_stringview_iter_obj_get_value(
  2912. stringview_iter_obj),
  2913. wasm_stringview_iter_obj_get_pos(
  2914. stringview_iter_obj));
  2915. PUSH_I32(code_point);
  2916. HANDLE_OP_END();
  2917. }
  2918. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  2919. case WASM_OP_STRINGVIEW_ITER_REWIND:
  2920. {
  2921. uint32 code_points_count, code_points_consumed = 0,
  2922. cur_pos, next_pos = 0;
  2923. code_points_count = POP_I32();
  2924. stringview_iter_obj = POP_REF();
  2925. str_obj =
  2926. (WASMString)wasm_stringview_iter_obj_get_value(
  2927. stringview_iter_obj);
  2928. cur_pos = wasm_stringview_iter_obj_get_pos(
  2929. stringview_iter_obj);
  2930. if (opcode == WASM_OP_STRINGVIEW_ITER_ADVANCE) {
  2931. next_pos = wasm_string_advance(
  2932. str_obj, cur_pos, code_points_count,
  2933. &code_points_consumed);
  2934. }
  2935. else if (opcode == WASM_OP_STRINGVIEW_ITER_REWIND) {
  2936. next_pos = wasm_string_rewind(
  2937. str_obj, cur_pos, code_points_count,
  2938. &code_points_consumed);
  2939. }
  2940. wasm_stringview_iter_obj_update_pos(stringview_iter_obj,
  2941. next_pos);
  2942. PUSH_I32(code_points_consumed);
  2943. HANDLE_OP_END();
  2944. }
  2945. case WASM_OP_STRINGVIEW_ITER_SLICE:
  2946. {
  2947. uint32 code_points_count, cur_pos;
  2948. code_points_count = POP_I32();
  2949. stringview_iter_obj = POP_REF();
  2950. cur_pos = wasm_stringview_iter_obj_get_pos(
  2951. stringview_iter_obj);
  2952. str_obj = wasm_string_slice(
  2953. (WASMString)wasm_stringview_iter_obj_get_value(
  2954. stringview_iter_obj),
  2955. cur_pos, cur_pos + code_points_count,
  2956. STRING_VIEW_ITER);
  2957. if (!str_obj) {
  2958. wasm_set_exception(module,
  2959. "create string object failed");
  2960. goto got_exception;
  2961. }
  2962. SYNC_ALL_TO_FRAME();
  2963. stringref_obj =
  2964. wasm_stringref_obj_new(exec_env, str_obj);
  2965. if (!stringref_obj) {
  2966. wasm_set_exception(module,
  2967. "create stringref failed");
  2968. goto got_exception;
  2969. }
  2970. PUSH_REF(stringref_obj);
  2971. HANDLE_OP_END();
  2972. }
  2973. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  2974. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  2975. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  2976. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  2977. {
  2978. uint32 start, end, array_len;
  2979. EncodingFlag flag = WTF8;
  2980. WASMArrayType *array_type;
  2981. void *arr_start_addr;
  2982. end = POP_I32();
  2983. start = POP_I32();
  2984. array_obj = POP_REF();
  2985. array_type = (WASMArrayType *)wasm_obj_get_defined_type(
  2986. (WASMObjectRef)array_obj);
  2987. arr_start_addr =
  2988. wasm_array_obj_elem_addr(array_obj, start);
  2989. array_len = wasm_array_obj_length(array_obj);
  2990. if (start > end || end > array_len) {
  2991. wasm_set_exception(module,
  2992. "out of bounds array access");
  2993. goto got_exception;
  2994. }
  2995. if (opcode == WASM_OP_STRING_NEW_WTF16_ARRAY) {
  2996. if (array_type->elem_type != VALUE_TYPE_I16) {
  2997. wasm_set_exception(module,
  2998. "array type mismatch");
  2999. goto got_exception;
  3000. }
  3001. flag = WTF16;
  3002. }
  3003. else {
  3004. if (array_type->elem_type != VALUE_TYPE_I8) {
  3005. wasm_set_exception(module,
  3006. "array type mismatch");
  3007. goto got_exception;
  3008. }
  3009. if (opcode == WASM_OP_STRING_NEW_UTF8_ARRAY) {
  3010. flag = UTF8;
  3011. }
  3012. else if (opcode == WASM_OP_STRING_NEW_WTF8_ARRAY) {
  3013. flag = WTF8;
  3014. }
  3015. else if (opcode
  3016. == WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY) {
  3017. flag = LOSSY_UTF8;
  3018. }
  3019. }
  3020. str_obj = wasm_string_new_with_encoding(
  3021. arr_start_addr, (end - start), flag);
  3022. if (!str_obj) {
  3023. wasm_set_exception(module,
  3024. "create string object failed");
  3025. goto got_exception;
  3026. }
  3027. SYNC_ALL_TO_FRAME();
  3028. stringref_obj =
  3029. wasm_stringref_obj_new(exec_env, str_obj);
  3030. if (!stringref_obj) {
  3031. wasm_set_exception(module,
  3032. "create stringref failed");
  3033. goto got_exception;
  3034. }
  3035. PUSH_REF(stringref_obj);
  3036. HANDLE_OP_END();
  3037. }
  3038. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  3039. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  3040. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  3041. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  3042. {
  3043. uint32 start, array_len, count;
  3044. int32 bytes_written;
  3045. EncodingFlag flag = WTF8;
  3046. WASMArrayType *array_type;
  3047. void *arr_start_addr;
  3048. start = POP_I32();
  3049. array_obj = POP_REF();
  3050. stringref_obj = POP_REF();
  3051. str_obj = (WASMString)wasm_stringref_obj_get_value(
  3052. stringref_obj);
  3053. array_type = (WASMArrayType *)wasm_obj_get_defined_type(
  3054. (WASMObjectRef)array_obj);
  3055. arr_start_addr =
  3056. wasm_array_obj_elem_addr(array_obj, start);
  3057. array_len = wasm_array_obj_length(array_obj);
  3058. if (start > array_len) {
  3059. wasm_set_exception(module,
  3060. "out of bounds array access");
  3061. goto got_exception;
  3062. }
  3063. if (opcode == WASM_OP_STRING_ENCODE_WTF16_ARRAY) {
  3064. if (array_type->elem_type != VALUE_TYPE_I16) {
  3065. wasm_set_exception(module,
  3066. "array type mismatch");
  3067. goto got_exception;
  3068. }
  3069. flag = WTF16;
  3070. }
  3071. else {
  3072. if (array_type->elem_type != VALUE_TYPE_I8) {
  3073. wasm_set_exception(module,
  3074. "array type mismatch");
  3075. goto got_exception;
  3076. }
  3077. if (opcode == WASM_OP_STRING_ENCODE_UTF8_ARRAY) {
  3078. flag = UTF8;
  3079. }
  3080. else if (opcode
  3081. == WASM_OP_STRING_ENCODE_WTF8_ARRAY) {
  3082. flag = WTF8;
  3083. }
  3084. else if (
  3085. opcode
  3086. == WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY) {
  3087. flag = LOSSY_UTF8;
  3088. }
  3089. }
  3090. count = wasm_string_measure(str_obj, flag);
  3091. bytes_written = wasm_string_encode(
  3092. str_obj, 0, count, arr_start_addr, NULL, flag);
  3093. if (bytes_written < 0) {
  3094. if (bytes_written == Isolated_Surrogate) {
  3095. wasm_set_exception(
  3096. module, "isolated surrogate is seen");
  3097. }
  3098. else if (bytes_written == Insufficient_Space) {
  3099. wasm_set_exception(
  3100. module, "array space is insufficient");
  3101. }
  3102. else {
  3103. wasm_set_exception(module, "encode failed");
  3104. }
  3105. goto got_exception;
  3106. }
  3107. PUSH_I32(bytes_written);
  3108. HANDLE_OP_END();
  3109. }
  3110. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  3111. default:
  3112. {
  3113. wasm_set_exception(module, "unsupported opcode");
  3114. goto got_exception;
  3115. }
  3116. }
  3117. }
  3118. #endif /* end of WASM_ENABLE_GC != 0 */
  3119. /* variable instructions */
  3120. HANDLE_OP(EXT_OP_SET_LOCAL_FAST)
  3121. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST)
  3122. {
  3123. /* clang-format off */
  3124. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  3125. local_offset = *frame_ip++;
  3126. #else
  3127. local_offset = *frame_ip;
  3128. frame_ip += 2;
  3129. #endif
  3130. /* clang-format on */
  3131. *(uint32 *)(frame_lp + local_offset) =
  3132. GET_OPERAND(uint32, I32, 0);
  3133. frame_ip += 2;
  3134. HANDLE_OP_END();
  3135. }
  3136. HANDLE_OP(EXT_OP_SET_LOCAL_FAST_I64)
  3137. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST_I64)
  3138. {
  3139. /* clang-format off */
  3140. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  3141. local_offset = *frame_ip++;
  3142. #else
  3143. local_offset = *frame_ip;
  3144. frame_ip += 2;
  3145. #endif
  3146. /* clang-format on */
  3147. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  3148. GET_OPERAND(uint64, I64, 0));
  3149. frame_ip += 2;
  3150. HANDLE_OP_END();
  3151. }
  3152. HANDLE_OP(WASM_OP_GET_GLOBAL)
  3153. {
  3154. global_idx = read_uint32(frame_ip);
  3155. bh_assert(global_idx < module->e->global_count);
  3156. global = globals + global_idx;
  3157. global_addr = get_global_addr(global_data, global);
  3158. addr_ret = GET_OFFSET();
  3159. /* clang-format off */
  3160. #if WASM_ENABLE_GC == 0
  3161. frame_lp[addr_ret] = *(uint32 *)global_addr;
  3162. #else
  3163. if (!wasm_is_type_reftype(global->type))
  3164. frame_lp[addr_ret] = *(uint32 *)global_addr;
  3165. else {
  3166. PUT_REF_TO_ADDR(frame_lp + addr_ret,
  3167. GET_REF_FROM_ADDR((uint32 *)global_addr));
  3168. if (!wasm_is_reftype_i31ref(global->type)) {
  3169. SET_FRAME_REF(addr_ret);
  3170. }
  3171. }
  3172. #endif
  3173. /* clang-format on */
  3174. HANDLE_OP_END();
  3175. }
  3176. HANDLE_OP(WASM_OP_GET_GLOBAL_64)
  3177. {
  3178. global_idx = read_uint32(frame_ip);
  3179. bh_assert(global_idx < module->e->global_count);
  3180. global = globals + global_idx;
  3181. global_addr = get_global_addr(global_data, global);
  3182. addr_ret = GET_OFFSET();
  3183. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  3184. GET_I64_FROM_ADDR((uint32 *)global_addr));
  3185. HANDLE_OP_END();
  3186. }
  3187. HANDLE_OP(WASM_OP_SET_GLOBAL)
  3188. {
  3189. global_idx = read_uint32(frame_ip);
  3190. bh_assert(global_idx < module->e->global_count);
  3191. global = globals + global_idx;
  3192. global_addr = get_global_addr(global_data, global);
  3193. addr1 = GET_OFFSET();
  3194. /* clang-format off */
  3195. #if WASM_ENABLE_GC == 0
  3196. *(int32 *)global_addr = frame_lp[addr1];
  3197. #else
  3198. if (!wasm_is_type_reftype(global->type))
  3199. *(int32 *)global_addr = frame_lp[addr1];
  3200. else {
  3201. PUT_REF_TO_ADDR((uint32 *)global_addr,
  3202. GET_REF_FROM_ADDR(frame_lp + addr1));
  3203. CLEAR_FRAME_REF(addr1);
  3204. }
  3205. #endif
  3206. /* clang-format on */
  3207. HANDLE_OP_END();
  3208. }
  3209. HANDLE_OP(WASM_OP_SET_GLOBAL_AUX_STACK)
  3210. {
  3211. uint64 aux_stack_top;
  3212. global_idx = read_uint32(frame_ip);
  3213. bh_assert(global_idx < module->e->global_count);
  3214. global = globals + global_idx;
  3215. global_addr = get_global_addr(global_data, global);
  3216. /* TODO: Memory64 the data type depends on mem idx type */
  3217. aux_stack_top = (uint64)frame_lp[GET_OFFSET()];
  3218. if (aux_stack_top <= (uint64)exec_env->aux_stack_boundary) {
  3219. wasm_set_exception(module, "wasm auxiliary stack overflow");
  3220. goto got_exception;
  3221. }
  3222. if (aux_stack_top > (uint64)exec_env->aux_stack_bottom) {
  3223. wasm_set_exception(module,
  3224. "wasm auxiliary stack underflow");
  3225. goto got_exception;
  3226. }
  3227. *(int32 *)global_addr = (uint32)aux_stack_top;
  3228. #if WASM_ENABLE_MEMORY_PROFILING != 0
  3229. if (module->module->aux_stack_top_global_index != (uint32)-1) {
  3230. uint32 aux_stack_used =
  3231. (uint32)(module->module->aux_stack_bottom
  3232. - *(uint32 *)global_addr);
  3233. if (aux_stack_used > module->e->max_aux_stack_used)
  3234. module->e->max_aux_stack_used = aux_stack_used;
  3235. }
  3236. #endif
  3237. HANDLE_OP_END();
  3238. }
  3239. HANDLE_OP(WASM_OP_SET_GLOBAL_64)
  3240. {
  3241. global_idx = read_uint32(frame_ip);
  3242. bh_assert(global_idx < module->e->global_count);
  3243. global = globals + global_idx;
  3244. global_addr = get_global_addr(global_data, global);
  3245. addr1 = GET_OFFSET();
  3246. PUT_I64_TO_ADDR((uint32 *)global_addr,
  3247. GET_I64_FROM_ADDR(frame_lp + addr1));
  3248. HANDLE_OP_END();
  3249. }
  3250. /* memory load instructions */
  3251. HANDLE_OP(WASM_OP_I32_LOAD)
  3252. {
  3253. uint32 offset, addr;
  3254. offset = read_uint32(frame_ip);
  3255. addr = GET_OPERAND(uint32, I32, 0);
  3256. frame_ip += 2;
  3257. addr_ret = GET_OFFSET();
  3258. CHECK_MEMORY_OVERFLOW(4);
  3259. frame_lp[addr_ret] = LOAD_I32(maddr);
  3260. HANDLE_OP_END();
  3261. }
  3262. HANDLE_OP(WASM_OP_I64_LOAD)
  3263. {
  3264. uint32 offset, addr;
  3265. offset = read_uint32(frame_ip);
  3266. addr = GET_OPERAND(uint32, I32, 0);
  3267. frame_ip += 2;
  3268. addr_ret = GET_OFFSET();
  3269. CHECK_MEMORY_OVERFLOW(8);
  3270. PUT_I64_TO_ADDR(frame_lp + addr_ret, LOAD_I64(maddr));
  3271. HANDLE_OP_END();
  3272. }
  3273. HANDLE_OP(WASM_OP_I32_LOAD8_S)
  3274. {
  3275. uint32 offset, addr;
  3276. offset = read_uint32(frame_ip);
  3277. addr = GET_OPERAND(uint32, I32, 0);
  3278. frame_ip += 2;
  3279. addr_ret = GET_OFFSET();
  3280. CHECK_MEMORY_OVERFLOW(1);
  3281. frame_lp[addr_ret] = sign_ext_8_32(*(int8 *)maddr);
  3282. HANDLE_OP_END();
  3283. }
  3284. HANDLE_OP(WASM_OP_I32_LOAD8_U)
  3285. {
  3286. uint32 offset, addr;
  3287. offset = read_uint32(frame_ip);
  3288. addr = GET_OPERAND(uint32, I32, 0);
  3289. frame_ip += 2;
  3290. addr_ret = GET_OFFSET();
  3291. CHECK_MEMORY_OVERFLOW(1);
  3292. frame_lp[addr_ret] = (uint32)(*(uint8 *)(maddr));
  3293. HANDLE_OP_END();
  3294. }
  3295. HANDLE_OP(WASM_OP_I32_LOAD16_S)
  3296. {
  3297. uint32 offset, addr;
  3298. offset = read_uint32(frame_ip);
  3299. addr = GET_OPERAND(uint32, I32, 0);
  3300. frame_ip += 2;
  3301. addr_ret = GET_OFFSET();
  3302. CHECK_MEMORY_OVERFLOW(2);
  3303. frame_lp[addr_ret] = sign_ext_16_32(LOAD_I16(maddr));
  3304. HANDLE_OP_END();
  3305. }
  3306. HANDLE_OP(WASM_OP_I32_LOAD16_U)
  3307. {
  3308. uint32 offset, addr;
  3309. offset = read_uint32(frame_ip);
  3310. addr = GET_OPERAND(uint32, I32, 0);
  3311. frame_ip += 2;
  3312. addr_ret = GET_OFFSET();
  3313. CHECK_MEMORY_OVERFLOW(2);
  3314. frame_lp[addr_ret] = (uint32)(LOAD_U16(maddr));
  3315. HANDLE_OP_END();
  3316. }
  3317. HANDLE_OP(WASM_OP_I64_LOAD8_S)
  3318. {
  3319. uint32 offset, addr;
  3320. offset = read_uint32(frame_ip);
  3321. addr = GET_OPERAND(uint32, I32, 0);
  3322. frame_ip += 2;
  3323. addr_ret = GET_OFFSET();
  3324. CHECK_MEMORY_OVERFLOW(1);
  3325. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  3326. sign_ext_8_64(*(int8 *)maddr));
  3327. HANDLE_OP_END();
  3328. }
  3329. HANDLE_OP(WASM_OP_I64_LOAD8_U)
  3330. {
  3331. uint32 offset, addr;
  3332. offset = read_uint32(frame_ip);
  3333. addr = GET_OPERAND(uint32, I32, 0);
  3334. frame_ip += 2;
  3335. addr_ret = GET_OFFSET();
  3336. CHECK_MEMORY_OVERFLOW(1);
  3337. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(*(uint8 *)maddr));
  3338. HANDLE_OP_END();
  3339. }
  3340. HANDLE_OP(WASM_OP_I64_LOAD16_S)
  3341. {
  3342. uint32 offset, addr;
  3343. offset = read_uint32(frame_ip);
  3344. addr = GET_OPERAND(uint32, I32, 0);
  3345. frame_ip += 2;
  3346. addr_ret = GET_OFFSET();
  3347. CHECK_MEMORY_OVERFLOW(2);
  3348. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  3349. sign_ext_16_64(LOAD_I16(maddr)));
  3350. HANDLE_OP_END();
  3351. }
  3352. HANDLE_OP(WASM_OP_I64_LOAD16_U)
  3353. {
  3354. uint32 offset, addr;
  3355. offset = read_uint32(frame_ip);
  3356. addr = GET_OPERAND(uint32, I32, 0);
  3357. frame_ip += 2;
  3358. addr_ret = GET_OFFSET();
  3359. CHECK_MEMORY_OVERFLOW(2);
  3360. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U16(maddr)));
  3361. HANDLE_OP_END();
  3362. }
  3363. HANDLE_OP(WASM_OP_I64_LOAD32_S)
  3364. {
  3365. uint32 offset, addr;
  3366. offset = read_uint32(frame_ip);
  3367. addr = GET_OPERAND(uint32, I32, 0);
  3368. frame_ip += 2;
  3369. addr_ret = GET_OFFSET();
  3370. CHECK_MEMORY_OVERFLOW(4);
  3371. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  3372. sign_ext_32_64(LOAD_I32(maddr)));
  3373. HANDLE_OP_END();
  3374. }
  3375. HANDLE_OP(WASM_OP_I64_LOAD32_U)
  3376. {
  3377. uint32 offset, addr;
  3378. offset = read_uint32(frame_ip);
  3379. addr = GET_OPERAND(uint32, I32, 0);
  3380. frame_ip += 2;
  3381. addr_ret = GET_OFFSET();
  3382. CHECK_MEMORY_OVERFLOW(4);
  3383. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U32(maddr)));
  3384. HANDLE_OP_END();
  3385. }
  3386. HANDLE_OP(WASM_OP_I32_STORE)
  3387. {
  3388. uint32 offset, addr;
  3389. uint32 sval;
  3390. offset = read_uint32(frame_ip);
  3391. sval = GET_OPERAND(uint32, I32, 0);
  3392. addr = GET_OPERAND(uint32, I32, 2);
  3393. frame_ip += 4;
  3394. CHECK_MEMORY_OVERFLOW(4);
  3395. STORE_U32(maddr, sval);
  3396. HANDLE_OP_END();
  3397. }
  3398. HANDLE_OP(WASM_OP_I32_STORE8)
  3399. {
  3400. uint32 offset, addr;
  3401. uint32 sval;
  3402. offset = read_uint32(frame_ip);
  3403. sval = GET_OPERAND(uint32, I32, 0);
  3404. addr = GET_OPERAND(uint32, I32, 2);
  3405. frame_ip += 4;
  3406. CHECK_MEMORY_OVERFLOW(1);
  3407. STORE_U8(maddr, (uint8_t)sval);
  3408. HANDLE_OP_END();
  3409. }
  3410. HANDLE_OP(WASM_OP_I32_STORE16)
  3411. {
  3412. uint32 offset, addr;
  3413. uint32 sval;
  3414. offset = read_uint32(frame_ip);
  3415. sval = GET_OPERAND(uint32, I32, 0);
  3416. addr = GET_OPERAND(uint32, I32, 2);
  3417. frame_ip += 4;
  3418. CHECK_MEMORY_OVERFLOW(2);
  3419. STORE_U16(maddr, (uint16)sval);
  3420. HANDLE_OP_END();
  3421. }
  3422. HANDLE_OP(WASM_OP_I64_STORE)
  3423. {
  3424. uint32 offset, addr;
  3425. uint64 sval;
  3426. offset = read_uint32(frame_ip);
  3427. sval = GET_OPERAND(uint64, I64, 0);
  3428. addr = GET_OPERAND(uint32, I32, 2);
  3429. frame_ip += 4;
  3430. CHECK_MEMORY_OVERFLOW(8);
  3431. STORE_I64(maddr, sval);
  3432. HANDLE_OP_END();
  3433. }
  3434. HANDLE_OP(WASM_OP_I64_STORE8)
  3435. {
  3436. uint32 offset, addr;
  3437. uint64 sval;
  3438. offset = read_uint32(frame_ip);
  3439. sval = GET_OPERAND(uint64, I64, 0);
  3440. addr = GET_OPERAND(uint32, I32, 2);
  3441. frame_ip += 4;
  3442. CHECK_MEMORY_OVERFLOW(1);
  3443. *(uint8 *)maddr = (uint8)sval;
  3444. HANDLE_OP_END();
  3445. }
  3446. HANDLE_OP(WASM_OP_I64_STORE16)
  3447. {
  3448. uint32 offset, addr;
  3449. uint64 sval;
  3450. offset = read_uint32(frame_ip);
  3451. sval = GET_OPERAND(uint64, I64, 0);
  3452. addr = GET_OPERAND(uint32, I32, 2);
  3453. frame_ip += 4;
  3454. CHECK_MEMORY_OVERFLOW(2);
  3455. STORE_U16(maddr, (uint16)sval);
  3456. HANDLE_OP_END();
  3457. }
  3458. HANDLE_OP(WASM_OP_I64_STORE32)
  3459. {
  3460. uint32 offset, addr;
  3461. uint64 sval;
  3462. offset = read_uint32(frame_ip);
  3463. sval = GET_OPERAND(uint64, I64, 0);
  3464. addr = GET_OPERAND(uint32, I32, 2);
  3465. frame_ip += 4;
  3466. CHECK_MEMORY_OVERFLOW(4);
  3467. STORE_U32(maddr, (uint32)sval);
  3468. HANDLE_OP_END();
  3469. }
  3470. /* memory size and memory grow instructions */
  3471. HANDLE_OP(WASM_OP_MEMORY_SIZE)
  3472. {
  3473. uint32 reserved;
  3474. addr_ret = GET_OFFSET();
  3475. frame_lp[addr_ret] = memory->cur_page_count;
  3476. (void)reserved;
  3477. HANDLE_OP_END();
  3478. }
  3479. HANDLE_OP(WASM_OP_MEMORY_GROW)
  3480. {
  3481. uint32 reserved, delta,
  3482. prev_page_count = memory->cur_page_count;
  3483. addr1 = GET_OFFSET();
  3484. addr_ret = GET_OFFSET();
  3485. delta = (uint32)frame_lp[addr1];
  3486. /* TODO: multi-memory wasm_enlarge_memory_with_idx() */
  3487. if (!wasm_enlarge_memory(module, delta)) {
  3488. /* failed to memory.grow, return -1 */
  3489. frame_lp[addr_ret] = -1;
  3490. }
  3491. else {
  3492. /* success, return previous page count */
  3493. frame_lp[addr_ret] = prev_page_count;
  3494. /* update memory size, no need to update memory ptr as
  3495. it isn't changed in wasm_enlarge_memory */
  3496. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  3497. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  3498. || WASM_ENABLE_BULK_MEMORY != 0
  3499. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
  3500. #endif
  3501. }
  3502. (void)reserved;
  3503. HANDLE_OP_END();
  3504. }
  3505. /* constant instructions */
  3506. HANDLE_OP(WASM_OP_F64_CONST)
  3507. HANDLE_OP(WASM_OP_I64_CONST)
  3508. {
  3509. uint8 *orig_ip = frame_ip;
  3510. frame_ip += sizeof(uint64);
  3511. addr_ret = GET_OFFSET();
  3512. bh_memcpy_s(frame_lp + addr_ret, sizeof(uint64), orig_ip,
  3513. sizeof(uint64));
  3514. HANDLE_OP_END();
  3515. }
  3516. HANDLE_OP(WASM_OP_F32_CONST)
  3517. HANDLE_OP(WASM_OP_I32_CONST)
  3518. {
  3519. uint8 *orig_ip = frame_ip;
  3520. frame_ip += sizeof(uint32);
  3521. addr_ret = GET_OFFSET();
  3522. bh_memcpy_s(frame_lp + addr_ret, sizeof(uint32), orig_ip,
  3523. sizeof(uint32));
  3524. HANDLE_OP_END();
  3525. }
  3526. /* comparison instructions of i32 */
  3527. HANDLE_OP(WASM_OP_I32_EQZ)
  3528. {
  3529. DEF_OP_EQZ(int32, I32);
  3530. HANDLE_OP_END();
  3531. }
  3532. HANDLE_OP(WASM_OP_I32_EQ)
  3533. {
  3534. DEF_OP_CMP(uint32, I32, ==);
  3535. HANDLE_OP_END();
  3536. }
  3537. HANDLE_OP(WASM_OP_I32_NE)
  3538. {
  3539. DEF_OP_CMP(uint32, I32, !=);
  3540. HANDLE_OP_END();
  3541. }
  3542. HANDLE_OP(WASM_OP_I32_LT_S)
  3543. {
  3544. DEF_OP_CMP(int32, I32, <);
  3545. HANDLE_OP_END();
  3546. }
  3547. HANDLE_OP(WASM_OP_I32_LT_U)
  3548. {
  3549. DEF_OP_CMP(uint32, I32, <);
  3550. HANDLE_OP_END();
  3551. }
  3552. HANDLE_OP(WASM_OP_I32_GT_S)
  3553. {
  3554. DEF_OP_CMP(int32, I32, >);
  3555. HANDLE_OP_END();
  3556. }
  3557. HANDLE_OP(WASM_OP_I32_GT_U)
  3558. {
  3559. DEF_OP_CMP(uint32, I32, >);
  3560. HANDLE_OP_END();
  3561. }
  3562. HANDLE_OP(WASM_OP_I32_LE_S)
  3563. {
  3564. DEF_OP_CMP(int32, I32, <=);
  3565. HANDLE_OP_END();
  3566. }
  3567. HANDLE_OP(WASM_OP_I32_LE_U)
  3568. {
  3569. DEF_OP_CMP(uint32, I32, <=);
  3570. HANDLE_OP_END();
  3571. }
  3572. HANDLE_OP(WASM_OP_I32_GE_S)
  3573. {
  3574. DEF_OP_CMP(int32, I32, >=);
  3575. HANDLE_OP_END();
  3576. }
  3577. HANDLE_OP(WASM_OP_I32_GE_U)
  3578. {
  3579. DEF_OP_CMP(uint32, I32, >=);
  3580. HANDLE_OP_END();
  3581. }
  3582. /* comparison instructions of i64 */
  3583. HANDLE_OP(WASM_OP_I64_EQZ)
  3584. {
  3585. DEF_OP_EQZ(int64, I64);
  3586. HANDLE_OP_END();
  3587. }
  3588. HANDLE_OP(WASM_OP_I64_EQ)
  3589. {
  3590. DEF_OP_CMP(uint64, I64, ==);
  3591. HANDLE_OP_END();
  3592. }
  3593. HANDLE_OP(WASM_OP_I64_NE)
  3594. {
  3595. DEF_OP_CMP(uint64, I64, !=);
  3596. HANDLE_OP_END();
  3597. }
  3598. HANDLE_OP(WASM_OP_I64_LT_S)
  3599. {
  3600. DEF_OP_CMP(int64, I64, <);
  3601. HANDLE_OP_END();
  3602. }
  3603. HANDLE_OP(WASM_OP_I64_LT_U)
  3604. {
  3605. DEF_OP_CMP(uint64, I64, <);
  3606. HANDLE_OP_END();
  3607. }
  3608. HANDLE_OP(WASM_OP_I64_GT_S)
  3609. {
  3610. DEF_OP_CMP(int64, I64, >);
  3611. HANDLE_OP_END();
  3612. }
  3613. HANDLE_OP(WASM_OP_I64_GT_U)
  3614. {
  3615. DEF_OP_CMP(uint64, I64, >);
  3616. HANDLE_OP_END();
  3617. }
  3618. HANDLE_OP(WASM_OP_I64_LE_S)
  3619. {
  3620. DEF_OP_CMP(int64, I64, <=);
  3621. HANDLE_OP_END();
  3622. }
  3623. HANDLE_OP(WASM_OP_I64_LE_U)
  3624. {
  3625. DEF_OP_CMP(uint64, I64, <=);
  3626. HANDLE_OP_END();
  3627. }
  3628. HANDLE_OP(WASM_OP_I64_GE_S)
  3629. {
  3630. DEF_OP_CMP(int64, I64, >=);
  3631. HANDLE_OP_END();
  3632. }
  3633. HANDLE_OP(WASM_OP_I64_GE_U)
  3634. {
  3635. DEF_OP_CMP(uint64, I64, >=);
  3636. HANDLE_OP_END();
  3637. }
  3638. /* comparison instructions of f32 */
  3639. HANDLE_OP(WASM_OP_F32_EQ)
  3640. {
  3641. DEF_OP_CMP(float32, F32, ==);
  3642. HANDLE_OP_END();
  3643. }
  3644. HANDLE_OP(WASM_OP_F32_NE)
  3645. {
  3646. DEF_OP_CMP(float32, F32, !=);
  3647. HANDLE_OP_END();
  3648. }
  3649. HANDLE_OP(WASM_OP_F32_LT)
  3650. {
  3651. DEF_OP_CMP(float32, F32, <);
  3652. HANDLE_OP_END();
  3653. }
  3654. HANDLE_OP(WASM_OP_F32_GT)
  3655. {
  3656. DEF_OP_CMP(float32, F32, >);
  3657. HANDLE_OP_END();
  3658. }
  3659. HANDLE_OP(WASM_OP_F32_LE)
  3660. {
  3661. DEF_OP_CMP(float32, F32, <=);
  3662. HANDLE_OP_END();
  3663. }
  3664. HANDLE_OP(WASM_OP_F32_GE)
  3665. {
  3666. DEF_OP_CMP(float32, F32, >=);
  3667. HANDLE_OP_END();
  3668. }
  3669. /* comparison instructions of f64 */
  3670. HANDLE_OP(WASM_OP_F64_EQ)
  3671. {
  3672. DEF_OP_CMP(float64, F64, ==);
  3673. HANDLE_OP_END();
  3674. }
  3675. HANDLE_OP(WASM_OP_F64_NE)
  3676. {
  3677. DEF_OP_CMP(float64, F64, !=);
  3678. HANDLE_OP_END();
  3679. }
  3680. HANDLE_OP(WASM_OP_F64_LT)
  3681. {
  3682. DEF_OP_CMP(float64, F64, <);
  3683. HANDLE_OP_END();
  3684. }
  3685. HANDLE_OP(WASM_OP_F64_GT)
  3686. {
  3687. DEF_OP_CMP(float64, F64, >);
  3688. HANDLE_OP_END();
  3689. }
  3690. HANDLE_OP(WASM_OP_F64_LE)
  3691. {
  3692. DEF_OP_CMP(float64, F64, <=);
  3693. HANDLE_OP_END();
  3694. }
  3695. HANDLE_OP(WASM_OP_F64_GE)
  3696. {
  3697. DEF_OP_CMP(float64, F64, >=);
  3698. HANDLE_OP_END();
  3699. }
  3700. /* numberic instructions of i32 */
  3701. HANDLE_OP(WASM_OP_I32_CLZ)
  3702. {
  3703. DEF_OP_BIT_COUNT(uint32, I32, clz32);
  3704. HANDLE_OP_END();
  3705. }
  3706. HANDLE_OP(WASM_OP_I32_CTZ)
  3707. {
  3708. DEF_OP_BIT_COUNT(uint32, I32, ctz32);
  3709. HANDLE_OP_END();
  3710. }
  3711. HANDLE_OP(WASM_OP_I32_POPCNT)
  3712. {
  3713. DEF_OP_BIT_COUNT(uint32, I32, popcount32);
  3714. HANDLE_OP_END();
  3715. }
  3716. HANDLE_OP(WASM_OP_I32_ADD)
  3717. {
  3718. DEF_OP_NUMERIC(uint32, uint32, I32, +);
  3719. HANDLE_OP_END();
  3720. }
  3721. HANDLE_OP(WASM_OP_I32_SUB)
  3722. {
  3723. DEF_OP_NUMERIC(uint32, uint32, I32, -);
  3724. HANDLE_OP_END();
  3725. }
  3726. HANDLE_OP(WASM_OP_I32_MUL)
  3727. {
  3728. DEF_OP_NUMERIC(uint32, uint32, I32, *);
  3729. HANDLE_OP_END();
  3730. }
  3731. HANDLE_OP(WASM_OP_I32_DIV_S)
  3732. {
  3733. int32 a, b;
  3734. b = frame_lp[GET_OFFSET()];
  3735. a = frame_lp[GET_OFFSET()];
  3736. addr_ret = GET_OFFSET();
  3737. if (a == (int32)0x80000000 && b == -1) {
  3738. wasm_set_exception(module, "integer overflow");
  3739. goto got_exception;
  3740. }
  3741. if (b == 0) {
  3742. wasm_set_exception(module, "integer divide by zero");
  3743. goto got_exception;
  3744. }
  3745. frame_lp[addr_ret] = (a / b);
  3746. HANDLE_OP_END();
  3747. }
  3748. HANDLE_OP(WASM_OP_I32_DIV_U)
  3749. {
  3750. uint32 a, b;
  3751. addr1 = GET_OFFSET();
  3752. addr2 = GET_OFFSET();
  3753. addr_ret = GET_OFFSET();
  3754. b = (uint32)frame_lp[addr1];
  3755. a = (uint32)frame_lp[addr2];
  3756. if (b == 0) {
  3757. wasm_set_exception(module, "integer divide by zero");
  3758. goto got_exception;
  3759. }
  3760. frame_lp[addr_ret] = (a / b);
  3761. HANDLE_OP_END();
  3762. }
  3763. HANDLE_OP(WASM_OP_I32_REM_S)
  3764. {
  3765. int32 a, b;
  3766. addr1 = GET_OFFSET();
  3767. addr2 = GET_OFFSET();
  3768. addr_ret = GET_OFFSET();
  3769. b = frame_lp[addr1];
  3770. a = frame_lp[addr2];
  3771. if (a == (int32)0x80000000 && b == -1) {
  3772. frame_lp[addr_ret] = 0;
  3773. HANDLE_OP_END();
  3774. }
  3775. if (b == 0) {
  3776. wasm_set_exception(module, "integer divide by zero");
  3777. goto got_exception;
  3778. }
  3779. frame_lp[addr_ret] = (a % b);
  3780. HANDLE_OP_END();
  3781. }
  3782. HANDLE_OP(WASM_OP_I32_REM_U)
  3783. {
  3784. uint32 a, b;
  3785. addr1 = GET_OFFSET();
  3786. addr2 = GET_OFFSET();
  3787. addr_ret = GET_OFFSET();
  3788. b = (uint32)frame_lp[addr1];
  3789. a = (uint32)frame_lp[addr2];
  3790. if (b == 0) {
  3791. wasm_set_exception(module, "integer divide by zero");
  3792. goto got_exception;
  3793. }
  3794. frame_lp[addr_ret] = (a % b);
  3795. HANDLE_OP_END();
  3796. }
  3797. HANDLE_OP(WASM_OP_I32_AND)
  3798. {
  3799. DEF_OP_NUMERIC(uint32, uint32, I32, &);
  3800. HANDLE_OP_END();
  3801. }
  3802. HANDLE_OP(WASM_OP_I32_OR)
  3803. {
  3804. DEF_OP_NUMERIC(uint32, uint32, I32, |);
  3805. HANDLE_OP_END();
  3806. }
  3807. HANDLE_OP(WASM_OP_I32_XOR)
  3808. {
  3809. DEF_OP_NUMERIC(uint32, uint32, I32, ^);
  3810. HANDLE_OP_END();
  3811. }
  3812. HANDLE_OP(WASM_OP_I32_SHL)
  3813. {
  3814. DEF_OP_NUMERIC2(uint32, uint32, I32, <<);
  3815. HANDLE_OP_END();
  3816. }
  3817. HANDLE_OP(WASM_OP_I32_SHR_S)
  3818. {
  3819. DEF_OP_NUMERIC2(int32, uint32, I32, >>);
  3820. HANDLE_OP_END();
  3821. }
  3822. HANDLE_OP(WASM_OP_I32_SHR_U)
  3823. {
  3824. DEF_OP_NUMERIC2(uint32, uint32, I32, >>);
  3825. HANDLE_OP_END();
  3826. }
  3827. HANDLE_OP(WASM_OP_I32_ROTL)
  3828. {
  3829. uint32 a, b;
  3830. b = (uint32)frame_lp[GET_OFFSET()];
  3831. a = (uint32)frame_lp[GET_OFFSET()];
  3832. frame_lp[GET_OFFSET()] = rotl32(a, b);
  3833. HANDLE_OP_END();
  3834. }
  3835. HANDLE_OP(WASM_OP_I32_ROTR)
  3836. {
  3837. uint32 a, b;
  3838. b = (uint32)frame_lp[GET_OFFSET()];
  3839. a = (uint32)frame_lp[GET_OFFSET()];
  3840. frame_lp[GET_OFFSET()] = rotr32(a, b);
  3841. HANDLE_OP_END();
  3842. }
  3843. /* numberic instructions of i64 */
  3844. HANDLE_OP(WASM_OP_I64_CLZ)
  3845. {
  3846. DEF_OP_BIT_COUNT(uint64, I64, clz64);
  3847. HANDLE_OP_END();
  3848. }
  3849. HANDLE_OP(WASM_OP_I64_CTZ)
  3850. {
  3851. DEF_OP_BIT_COUNT(uint64, I64, ctz64);
  3852. HANDLE_OP_END();
  3853. }
  3854. HANDLE_OP(WASM_OP_I64_POPCNT)
  3855. {
  3856. DEF_OP_BIT_COUNT(uint64, I64, popcount64);
  3857. HANDLE_OP_END();
  3858. }
  3859. HANDLE_OP(WASM_OP_I64_ADD)
  3860. {
  3861. DEF_OP_NUMERIC_64(uint64, uint64, I64, +);
  3862. HANDLE_OP_END();
  3863. }
  3864. HANDLE_OP(WASM_OP_I64_SUB)
  3865. {
  3866. DEF_OP_NUMERIC_64(uint64, uint64, I64, -);
  3867. HANDLE_OP_END();
  3868. }
  3869. HANDLE_OP(WASM_OP_I64_MUL)
  3870. {
  3871. DEF_OP_NUMERIC_64(uint64, uint64, I64, *);
  3872. HANDLE_OP_END();
  3873. }
  3874. HANDLE_OP(WASM_OP_I64_DIV_S)
  3875. {
  3876. int64 a, b;
  3877. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3878. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3879. if (a == (int64)0x8000000000000000LL && b == -1) {
  3880. wasm_set_exception(module, "integer overflow");
  3881. goto got_exception;
  3882. }
  3883. if (b == 0) {
  3884. wasm_set_exception(module, "integer divide by zero");
  3885. goto got_exception;
  3886. }
  3887. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
  3888. HANDLE_OP_END();
  3889. }
  3890. HANDLE_OP(WASM_OP_I64_DIV_U)
  3891. {
  3892. uint64 a, b;
  3893. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3894. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3895. if (b == 0) {
  3896. wasm_set_exception(module, "integer divide by zero");
  3897. goto got_exception;
  3898. }
  3899. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
  3900. HANDLE_OP_END();
  3901. }
  3902. HANDLE_OP(WASM_OP_I64_REM_S)
  3903. {
  3904. int64 a, b;
  3905. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3906. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3907. if (a == (int64)0x8000000000000000LL && b == -1) {
  3908. *(int64 *)(frame_lp + GET_OFFSET()) = 0;
  3909. HANDLE_OP_END();
  3910. }
  3911. if (b == 0) {
  3912. wasm_set_exception(module, "integer divide by zero");
  3913. goto got_exception;
  3914. }
  3915. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
  3916. HANDLE_OP_END();
  3917. }
  3918. HANDLE_OP(WASM_OP_I64_REM_U)
  3919. {
  3920. uint64 a, b;
  3921. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3922. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3923. if (b == 0) {
  3924. wasm_set_exception(module, "integer divide by zero");
  3925. goto got_exception;
  3926. }
  3927. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
  3928. HANDLE_OP_END();
  3929. }
  3930. HANDLE_OP(WASM_OP_I64_AND)
  3931. {
  3932. DEF_OP_NUMERIC_64(uint64, uint64, I64, &);
  3933. HANDLE_OP_END();
  3934. }
  3935. HANDLE_OP(WASM_OP_I64_OR)
  3936. {
  3937. DEF_OP_NUMERIC_64(uint64, uint64, I64, |);
  3938. HANDLE_OP_END();
  3939. }
  3940. HANDLE_OP(WASM_OP_I64_XOR)
  3941. {
  3942. DEF_OP_NUMERIC_64(uint64, uint64, I64, ^);
  3943. HANDLE_OP_END();
  3944. }
  3945. HANDLE_OP(WASM_OP_I64_SHL)
  3946. {
  3947. DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<);
  3948. HANDLE_OP_END();
  3949. }
  3950. HANDLE_OP(WASM_OP_I64_SHR_S)
  3951. {
  3952. DEF_OP_NUMERIC2_64(int64, uint64, I64, >>);
  3953. HANDLE_OP_END();
  3954. }
  3955. HANDLE_OP(WASM_OP_I64_SHR_U)
  3956. {
  3957. DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>);
  3958. HANDLE_OP_END();
  3959. }
  3960. HANDLE_OP(WASM_OP_I64_ROTL)
  3961. {
  3962. uint64 a, b;
  3963. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3964. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3965. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotl64(a, b));
  3966. HANDLE_OP_END();
  3967. }
  3968. HANDLE_OP(WASM_OP_I64_ROTR)
  3969. {
  3970. uint64 a, b;
  3971. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3972. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3973. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotr64(a, b));
  3974. HANDLE_OP_END();
  3975. }
  3976. /* numberic instructions of f32 */
  3977. HANDLE_OP(WASM_OP_F32_ABS)
  3978. {
  3979. DEF_OP_MATH(float32, F32, fabsf);
  3980. HANDLE_OP_END();
  3981. }
  3982. HANDLE_OP(WASM_OP_F32_NEG)
  3983. {
  3984. uint32 u32 = frame_lp[GET_OFFSET()];
  3985. uint32 sign_bit = u32 & ((uint32)1 << 31);
  3986. addr_ret = GET_OFFSET();
  3987. if (sign_bit)
  3988. frame_lp[addr_ret] = u32 & ~((uint32)1 << 31);
  3989. else
  3990. frame_lp[addr_ret] = u32 | ((uint32)1 << 31);
  3991. HANDLE_OP_END();
  3992. }
  3993. HANDLE_OP(WASM_OP_F32_CEIL)
  3994. {
  3995. DEF_OP_MATH(float32, F32, ceilf);
  3996. HANDLE_OP_END();
  3997. }
  3998. HANDLE_OP(WASM_OP_F32_FLOOR)
  3999. {
  4000. DEF_OP_MATH(float32, F32, floorf);
  4001. HANDLE_OP_END();
  4002. }
  4003. HANDLE_OP(WASM_OP_F32_TRUNC)
  4004. {
  4005. DEF_OP_MATH(float32, F32, truncf);
  4006. HANDLE_OP_END();
  4007. }
  4008. HANDLE_OP(WASM_OP_F32_NEAREST)
  4009. {
  4010. DEF_OP_MATH(float32, F32, rintf);
  4011. HANDLE_OP_END();
  4012. }
  4013. HANDLE_OP(WASM_OP_F32_SQRT)
  4014. {
  4015. DEF_OP_MATH(float32, F32, sqrtf);
  4016. HANDLE_OP_END();
  4017. }
  4018. HANDLE_OP(WASM_OP_F32_ADD)
  4019. {
  4020. DEF_OP_NUMERIC(float32, float32, F32, +);
  4021. HANDLE_OP_END();
  4022. }
  4023. HANDLE_OP(WASM_OP_F32_SUB)
  4024. {
  4025. DEF_OP_NUMERIC(float32, float32, F32, -);
  4026. HANDLE_OP_END();
  4027. }
  4028. HANDLE_OP(WASM_OP_F32_MUL)
  4029. {
  4030. DEF_OP_NUMERIC(float32, float32, F32, *);
  4031. HANDLE_OP_END();
  4032. }
  4033. HANDLE_OP(WASM_OP_F32_DIV)
  4034. {
  4035. DEF_OP_NUMERIC(float32, float32, F32, /);
  4036. HANDLE_OP_END();
  4037. }
  4038. HANDLE_OP(WASM_OP_F32_MIN)
  4039. {
  4040. float32 a, b;
  4041. b = *(float32 *)(frame_lp + GET_OFFSET());
  4042. a = *(float32 *)(frame_lp + GET_OFFSET());
  4043. *(float32 *)(frame_lp + GET_OFFSET()) = f32_min(a, b);
  4044. HANDLE_OP_END();
  4045. }
  4046. HANDLE_OP(WASM_OP_F32_MAX)
  4047. {
  4048. float32 a, b;
  4049. b = *(float32 *)(frame_lp + GET_OFFSET());
  4050. a = *(float32 *)(frame_lp + GET_OFFSET());
  4051. *(float32 *)(frame_lp + GET_OFFSET()) = f32_max(a, b);
  4052. HANDLE_OP_END();
  4053. }
  4054. HANDLE_OP(WASM_OP_F32_COPYSIGN)
  4055. {
  4056. float32 a, b;
  4057. b = *(float32 *)(frame_lp + GET_OFFSET());
  4058. a = *(float32 *)(frame_lp + GET_OFFSET());
  4059. *(float32 *)(frame_lp + GET_OFFSET()) = local_copysignf(a, b);
  4060. HANDLE_OP_END();
  4061. }
  4062. /* numberic instructions of f64 */
  4063. HANDLE_OP(WASM_OP_F64_ABS)
  4064. {
  4065. DEF_OP_MATH(float64, F64, fabs);
  4066. HANDLE_OP_END();
  4067. }
  4068. HANDLE_OP(WASM_OP_F64_NEG)
  4069. {
  4070. uint64 u64 = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  4071. uint64 sign_bit = u64 & (((uint64)1) << 63);
  4072. if (sign_bit)
  4073. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
  4074. (u64 & ~(((uint64)1) << 63)));
  4075. else
  4076. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
  4077. (u64 | (((uint64)1) << 63)));
  4078. HANDLE_OP_END();
  4079. }
  4080. HANDLE_OP(WASM_OP_F64_CEIL)
  4081. {
  4082. DEF_OP_MATH(float64, F64, ceil);
  4083. HANDLE_OP_END();
  4084. }
  4085. HANDLE_OP(WASM_OP_F64_FLOOR)
  4086. {
  4087. DEF_OP_MATH(float64, F64, floor);
  4088. HANDLE_OP_END();
  4089. }
  4090. HANDLE_OP(WASM_OP_F64_TRUNC)
  4091. {
  4092. DEF_OP_MATH(float64, F64, trunc);
  4093. HANDLE_OP_END();
  4094. }
  4095. HANDLE_OP(WASM_OP_F64_NEAREST)
  4096. {
  4097. DEF_OP_MATH(float64, F64, rint);
  4098. HANDLE_OP_END();
  4099. }
  4100. HANDLE_OP(WASM_OP_F64_SQRT)
  4101. {
  4102. DEF_OP_MATH(float64, F64, sqrt);
  4103. HANDLE_OP_END();
  4104. }
  4105. HANDLE_OP(WASM_OP_F64_ADD)
  4106. {
  4107. DEF_OP_NUMERIC_64(float64, float64, F64, +);
  4108. HANDLE_OP_END();
  4109. }
  4110. HANDLE_OP(WASM_OP_F64_SUB)
  4111. {
  4112. DEF_OP_NUMERIC_64(float64, float64, F64, -);
  4113. HANDLE_OP_END();
  4114. }
  4115. HANDLE_OP(WASM_OP_F64_MUL)
  4116. {
  4117. DEF_OP_NUMERIC_64(float64, float64, F64, *);
  4118. HANDLE_OP_END();
  4119. }
  4120. HANDLE_OP(WASM_OP_F64_DIV)
  4121. {
  4122. DEF_OP_NUMERIC_64(float64, float64, F64, /);
  4123. HANDLE_OP_END();
  4124. }
  4125. HANDLE_OP(WASM_OP_F64_MIN)
  4126. {
  4127. float64 a, b;
  4128. b = POP_F64();
  4129. a = POP_F64();
  4130. PUSH_F64(f64_min(a, b));
  4131. HANDLE_OP_END();
  4132. }
  4133. HANDLE_OP(WASM_OP_F64_MAX)
  4134. {
  4135. float64 a, b;
  4136. b = POP_F64();
  4137. a = POP_F64();
  4138. PUSH_F64(f64_max(a, b));
  4139. HANDLE_OP_END();
  4140. }
  4141. HANDLE_OP(WASM_OP_F64_COPYSIGN)
  4142. {
  4143. float64 a, b;
  4144. b = POP_F64();
  4145. a = POP_F64();
  4146. PUSH_F64(local_copysign(a, b));
  4147. HANDLE_OP_END();
  4148. }
  4149. /* conversions of i32 */
  4150. HANDLE_OP(WASM_OP_I32_WRAP_I64)
  4151. {
  4152. int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL);
  4153. PUSH_I32(value);
  4154. HANDLE_OP_END();
  4155. }
  4156. HANDLE_OP(WASM_OP_I32_TRUNC_S_F32)
  4157. {
  4158. /* We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX,
  4159. since float/double values of ieee754 cannot precisely
  4160. represent all int32/uint32/int64/uint64 values, e.g.:
  4161. UINT32_MAX is 4294967295, but (float32)4294967295 is
  4162. 4294967296.0f, but not 4294967295.0f. */
  4163. DEF_OP_TRUNC_F32(-2147483904.0f, 2147483648.0f, true, true);
  4164. HANDLE_OP_END();
  4165. }
  4166. HANDLE_OP(WASM_OP_I32_TRUNC_U_F32)
  4167. {
  4168. DEF_OP_TRUNC_F32(-1.0f, 4294967296.0f, true, false);
  4169. HANDLE_OP_END();
  4170. }
  4171. HANDLE_OP(WASM_OP_I32_TRUNC_S_F64)
  4172. {
  4173. DEF_OP_TRUNC_F64(-2147483649.0, 2147483648.0, true, true);
  4174. HANDLE_OP_END();
  4175. }
  4176. HANDLE_OP(WASM_OP_I32_TRUNC_U_F64)
  4177. {
  4178. DEF_OP_TRUNC_F64(-1.0, 4294967296.0, true, false);
  4179. HANDLE_OP_END();
  4180. }
  4181. /* conversions of i64 */
  4182. HANDLE_OP(WASM_OP_I64_EXTEND_S_I32)
  4183. {
  4184. DEF_OP_CONVERT(int64, I64, int32, I32);
  4185. HANDLE_OP_END();
  4186. }
  4187. HANDLE_OP(WASM_OP_I64_EXTEND_U_I32)
  4188. {
  4189. DEF_OP_CONVERT(int64, I64, uint32, I32);
  4190. HANDLE_OP_END();
  4191. }
  4192. HANDLE_OP(WASM_OP_I64_TRUNC_S_F32)
  4193. {
  4194. DEF_OP_TRUNC_F32(-9223373136366403584.0f,
  4195. 9223372036854775808.0f, false, true);
  4196. HANDLE_OP_END();
  4197. }
  4198. HANDLE_OP(WASM_OP_I64_TRUNC_U_F32)
  4199. {
  4200. DEF_OP_TRUNC_F32(-1.0f, 18446744073709551616.0f, false, false);
  4201. HANDLE_OP_END();
  4202. }
  4203. HANDLE_OP(WASM_OP_I64_TRUNC_S_F64)
  4204. {
  4205. DEF_OP_TRUNC_F64(-9223372036854777856.0, 9223372036854775808.0,
  4206. false, true);
  4207. HANDLE_OP_END();
  4208. }
  4209. HANDLE_OP(WASM_OP_I64_TRUNC_U_F64)
  4210. {
  4211. DEF_OP_TRUNC_F64(-1.0, 18446744073709551616.0, false, false);
  4212. HANDLE_OP_END();
  4213. }
  4214. /* conversions of f32 */
  4215. HANDLE_OP(WASM_OP_F32_CONVERT_S_I32)
  4216. {
  4217. DEF_OP_CONVERT(float32, F32, int32, I32);
  4218. HANDLE_OP_END();
  4219. }
  4220. HANDLE_OP(WASM_OP_F32_CONVERT_U_I32)
  4221. {
  4222. DEF_OP_CONVERT(float32, F32, uint32, I32);
  4223. HANDLE_OP_END();
  4224. }
  4225. HANDLE_OP(WASM_OP_F32_CONVERT_S_I64)
  4226. {
  4227. DEF_OP_CONVERT(float32, F32, int64, I64);
  4228. HANDLE_OP_END();
  4229. }
  4230. HANDLE_OP(WASM_OP_F32_CONVERT_U_I64)
  4231. {
  4232. DEF_OP_CONVERT(float32, F32, uint64, I64);
  4233. HANDLE_OP_END();
  4234. }
  4235. HANDLE_OP(WASM_OP_F32_DEMOTE_F64)
  4236. {
  4237. DEF_OP_CONVERT(float32, F32, float64, F64);
  4238. HANDLE_OP_END();
  4239. }
  4240. /* conversions of f64 */
  4241. HANDLE_OP(WASM_OP_F64_CONVERT_S_I32)
  4242. {
  4243. DEF_OP_CONVERT(float64, F64, int32, I32);
  4244. HANDLE_OP_END();
  4245. }
  4246. HANDLE_OP(WASM_OP_F64_CONVERT_U_I32)
  4247. {
  4248. DEF_OP_CONVERT(float64, F64, uint32, I32);
  4249. HANDLE_OP_END();
  4250. }
  4251. HANDLE_OP(WASM_OP_F64_CONVERT_S_I64)
  4252. {
  4253. DEF_OP_CONVERT(float64, F64, int64, I64);
  4254. HANDLE_OP_END();
  4255. }
  4256. HANDLE_OP(WASM_OP_F64_CONVERT_U_I64)
  4257. {
  4258. DEF_OP_CONVERT(float64, F64, uint64, I64);
  4259. HANDLE_OP_END();
  4260. }
  4261. HANDLE_OP(WASM_OP_F64_PROMOTE_F32)
  4262. {
  4263. DEF_OP_CONVERT(float64, F64, float32, F32);
  4264. HANDLE_OP_END();
  4265. }
  4266. /* reinterpretations */
  4267. HANDLE_OP(WASM_OP_I32_REINTERPRET_F32)
  4268. HANDLE_OP(WASM_OP_F32_REINTERPRET_I32)
  4269. {
  4270. DEF_OP_REINTERPRET(uint32, I32);
  4271. HANDLE_OP_END();
  4272. }
  4273. HANDLE_OP(WASM_OP_I64_REINTERPRET_F64)
  4274. HANDLE_OP(WASM_OP_F64_REINTERPRET_I64)
  4275. {
  4276. DEF_OP_REINTERPRET(int64, I64);
  4277. HANDLE_OP_END();
  4278. }
  4279. HANDLE_OP(EXT_OP_COPY_STACK_TOP)
  4280. {
  4281. addr1 = GET_OFFSET();
  4282. addr2 = GET_OFFSET();
  4283. frame_lp[addr2] = frame_lp[addr1];
  4284. #if WASM_ENABLE_GC != 0
  4285. /* Ignore constants because they are not reference */
  4286. if (addr1 >= 0) {
  4287. if (*FRAME_REF(addr1)) {
  4288. CLEAR_FRAME_REF(addr1);
  4289. SET_FRAME_REF(addr2);
  4290. }
  4291. }
  4292. #endif
  4293. HANDLE_OP_END();
  4294. }
  4295. HANDLE_OP(EXT_OP_COPY_STACK_TOP_I64)
  4296. {
  4297. addr1 = GET_OFFSET();
  4298. addr2 = GET_OFFSET();
  4299. PUT_I64_TO_ADDR(frame_lp + addr2,
  4300. GET_I64_FROM_ADDR(frame_lp + addr1));
  4301. #if WASM_ENABLE_GC != 0
  4302. /* Ignore constants because they are not reference */
  4303. if (addr1 >= 0) {
  4304. if (*FRAME_REF(addr1)) {
  4305. CLEAR_FRAME_REF(addr1);
  4306. SET_FRAME_REF(addr2);
  4307. }
  4308. }
  4309. #endif
  4310. HANDLE_OP_END();
  4311. }
  4312. HANDLE_OP(EXT_OP_COPY_STACK_VALUES)
  4313. {
  4314. uint32 values_count, total_cell;
  4315. uint8 *cells;
  4316. int16 *src_offsets = NULL;
  4317. uint16 *dst_offsets = NULL;
  4318. /* read values_count */
  4319. values_count = read_uint32(frame_ip);
  4320. /* read total cell num */
  4321. total_cell = read_uint32(frame_ip);
  4322. /* cells */
  4323. cells = (uint8 *)frame_ip;
  4324. frame_ip += values_count * CELL_SIZE;
  4325. /* src offsets */
  4326. src_offsets = (int16 *)frame_ip;
  4327. frame_ip += values_count * sizeof(int16);
  4328. /* dst offsets */
  4329. dst_offsets = (uint16 *)frame_ip;
  4330. frame_ip += values_count * sizeof(uint16);
  4331. if (!copy_stack_values(module, frame_lp, values_count,
  4332. #if WASM_ENABLE_GC != 0
  4333. frame_ref,
  4334. #endif
  4335. total_cell, cells, src_offsets,
  4336. dst_offsets))
  4337. goto got_exception;
  4338. HANDLE_OP_END();
  4339. }
  4340. HANDLE_OP(WASM_OP_SET_LOCAL)
  4341. {
  4342. opcode = WASM_OP_SET_LOCAL;
  4343. goto handle_op_set_tee_local;
  4344. }
  4345. HANDLE_OP(WASM_OP_TEE_LOCAL)
  4346. {
  4347. opcode = WASM_OP_TEE_LOCAL;
  4348. handle_op_set_tee_local:
  4349. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  4350. addr1 = GET_OFFSET();
  4351. if (local_type == VALUE_TYPE_I32 || local_type == VALUE_TYPE_F32
  4352. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  4353. || local_type == VALUE_TYPE_FUNCREF
  4354. || local_type == VALUE_TYPE_EXTERNREF
  4355. #endif
  4356. ) {
  4357. *(int32 *)(frame_lp + local_offset) = frame_lp[addr1];
  4358. }
  4359. else if (local_type == VALUE_TYPE_I64
  4360. || local_type == VALUE_TYPE_F64) {
  4361. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  4362. GET_I64_FROM_ADDR(frame_lp + addr1));
  4363. }
  4364. #if WASM_ENABLE_GC != 0
  4365. else if (wasm_is_type_reftype(local_type)) {
  4366. PUT_REF_TO_ADDR((uint32 *)(frame_lp + local_offset),
  4367. GET_REF_FROM_ADDR(frame_lp + addr1));
  4368. if (opcode == WASM_OP_SET_LOCAL) {
  4369. CLEAR_FRAME_REF(addr1);
  4370. }
  4371. }
  4372. #endif
  4373. else {
  4374. wasm_set_exception(module, "invalid local type");
  4375. goto got_exception;
  4376. }
  4377. HANDLE_OP_END();
  4378. }
  4379. HANDLE_OP(WASM_OP_I32_EXTEND8_S)
  4380. {
  4381. DEF_OP_CONVERT(int32, I32, int8, I32);
  4382. HANDLE_OP_END();
  4383. }
  4384. HANDLE_OP(WASM_OP_I32_EXTEND16_S)
  4385. {
  4386. DEF_OP_CONVERT(int32, I32, int16, I32);
  4387. HANDLE_OP_END();
  4388. }
  4389. HANDLE_OP(WASM_OP_I64_EXTEND8_S)
  4390. {
  4391. DEF_OP_CONVERT(int64, I64, int8, I64);
  4392. HANDLE_OP_END();
  4393. }
  4394. HANDLE_OP(WASM_OP_I64_EXTEND16_S)
  4395. {
  4396. DEF_OP_CONVERT(int64, I64, int16, I64);
  4397. HANDLE_OP_END();
  4398. }
  4399. HANDLE_OP(WASM_OP_I64_EXTEND32_S)
  4400. {
  4401. DEF_OP_CONVERT(int64, I64, int32, I64);
  4402. HANDLE_OP_END();
  4403. }
  4404. HANDLE_OP(WASM_OP_MISC_PREFIX)
  4405. {
  4406. GET_OPCODE();
  4407. switch (opcode) {
  4408. case WASM_OP_I32_TRUNC_SAT_S_F32:
  4409. DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f,
  4410. true, true);
  4411. break;
  4412. case WASM_OP_I32_TRUNC_SAT_U_F32:
  4413. DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, true, false);
  4414. break;
  4415. case WASM_OP_I32_TRUNC_SAT_S_F64:
  4416. DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, true,
  4417. true);
  4418. break;
  4419. case WASM_OP_I32_TRUNC_SAT_U_F64:
  4420. DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, true, false);
  4421. break;
  4422. case WASM_OP_I64_TRUNC_SAT_S_F32:
  4423. DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f,
  4424. 9223372036854775808.0f, false,
  4425. true);
  4426. break;
  4427. case WASM_OP_I64_TRUNC_SAT_U_F32:
  4428. DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f,
  4429. false, false);
  4430. break;
  4431. case WASM_OP_I64_TRUNC_SAT_S_F64:
  4432. DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0,
  4433. 9223372036854775808.0, false,
  4434. true);
  4435. break;
  4436. case WASM_OP_I64_TRUNC_SAT_U_F64:
  4437. DEF_OP_TRUNC_SAT_F64(-1.0, 18446744073709551616.0,
  4438. false, false);
  4439. break;
  4440. #if WASM_ENABLE_BULK_MEMORY != 0
  4441. case WASM_OP_MEMORY_INIT:
  4442. {
  4443. uint32 addr, segment;
  4444. uint64 bytes, offset, seg_len;
  4445. uint8 *data;
  4446. segment = read_uint32(frame_ip);
  4447. bytes = (uint64)(uint32)POP_I32();
  4448. offset = (uint64)(uint32)POP_I32();
  4449. addr = POP_I32();
  4450. #if WASM_ENABLE_THREAD_MGR != 0
  4451. linear_mem_size = get_linear_mem_size();
  4452. #endif
  4453. #ifndef OS_ENABLE_HW_BOUND_CHECK
  4454. CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr);
  4455. #else
  4456. #if WASM_ENABLE_SHARED_HEAP != 0
  4457. if (app_addr_in_shared_heap((uint64)(uint32)addr,
  4458. bytes))
  4459. shared_heap_addr_app_to_native((uint64)(uint32)addr,
  4460. maddr);
  4461. else
  4462. #endif
  4463. {
  4464. if ((uint64)(uint32)addr + bytes > linear_mem_size)
  4465. goto out_of_bounds;
  4466. maddr = memory->memory_data + (uint32)addr;
  4467. }
  4468. #endif
  4469. if (bh_bitmap_get_bit(module->e->common.data_dropped,
  4470. segment)) {
  4471. seg_len = 0;
  4472. data = NULL;
  4473. }
  4474. else {
  4475. seg_len =
  4476. (uint64)module->module->data_segments[segment]
  4477. ->data_length;
  4478. data = module->module->data_segments[segment]->data;
  4479. }
  4480. if (offset + bytes > seg_len)
  4481. goto out_of_bounds;
  4482. bh_memcpy_s(maddr, (uint32)(linear_mem_size - addr),
  4483. data + offset, (uint32)bytes);
  4484. break;
  4485. }
  4486. case WASM_OP_DATA_DROP:
  4487. {
  4488. uint32 segment;
  4489. segment = read_uint32(frame_ip);
  4490. bh_bitmap_set_bit(module->e->common.data_dropped,
  4491. segment);
  4492. break;
  4493. }
  4494. case WASM_OP_MEMORY_COPY:
  4495. {
  4496. uint32 dst, src, len;
  4497. uint8 *mdst, *msrc;
  4498. uint64 dlen;
  4499. len = POP_I32();
  4500. src = POP_I32();
  4501. dst = POP_I32();
  4502. #if WASM_ENABLE_THREAD_MGR != 0
  4503. linear_mem_size = get_linear_mem_size();
  4504. #endif
  4505. dlen = linear_mem_size - dst;
  4506. #ifndef OS_ENABLE_HW_BOUND_CHECK
  4507. CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc);
  4508. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  4509. #if WASM_ENABLE_SHARED_HEAP != 0
  4510. if (app_addr_in_shared_heap((uint64)dst, len))
  4511. dlen = shared_heap_end_off - dst + 1;
  4512. #endif
  4513. #else /* else of OS_ENABLE_HW_BOUND_CHECK */
  4514. #if WASM_ENABLE_SHARED_HEAP != 0
  4515. if (app_addr_in_shared_heap((uint64)src, len))
  4516. shared_heap_addr_app_to_native((uint64)src, msrc);
  4517. else
  4518. #endif
  4519. {
  4520. if ((uint64)(uint32)src + len > linear_mem_size)
  4521. goto out_of_bounds;
  4522. msrc = memory->memory_data + (uint32)src;
  4523. }
  4524. #if WASM_ENABLE_SHARED_HEAP != 0
  4525. if (app_addr_in_shared_heap((uint64)dst, len)) {
  4526. shared_heap_addr_app_to_native((uint64)dst, mdst);
  4527. dlen = shared_heap_end_off - dst + 1;
  4528. }
  4529. else
  4530. #endif
  4531. {
  4532. if ((uint64)(uint32)dst + len > linear_mem_size)
  4533. goto out_of_bounds;
  4534. mdst = memory->memory_data + (uint32)dst;
  4535. }
  4536. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  4537. /* allowing the destination and source to overlap */
  4538. bh_memmove_s(mdst, (uint32)dlen, msrc, len);
  4539. break;
  4540. }
  4541. case WASM_OP_MEMORY_FILL:
  4542. {
  4543. uint32 dst, len;
  4544. uint8 fill_val, *mdst;
  4545. len = POP_I32();
  4546. fill_val = POP_I32();
  4547. dst = POP_I32();
  4548. #if WASM_ENABLE_THREAD_MGR != 0
  4549. linear_mem_size = get_linear_mem_size();
  4550. #endif
  4551. #ifndef OS_ENABLE_HW_BOUND_CHECK
  4552. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  4553. #else
  4554. #if WASM_ENABLE_SHARED_HEAP != 0
  4555. if (app_addr_in_shared_heap((uint64)(uint32)dst, len))
  4556. shared_heap_addr_app_to_native((uint64)(uint32)dst,
  4557. mdst);
  4558. else
  4559. #endif
  4560. {
  4561. if ((uint64)(uint32)dst + len > linear_mem_size)
  4562. goto out_of_bounds;
  4563. mdst = memory->memory_data + (uint32)dst;
  4564. }
  4565. #endif
  4566. memset(mdst, fill_val, len);
  4567. break;
  4568. }
  4569. #endif /* WASM_ENABLE_BULK_MEMORY */
  4570. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4571. case WASM_OP_TABLE_INIT:
  4572. {
  4573. uint32 tbl_idx, elem_idx;
  4574. uint32 n, s, d;
  4575. WASMTableInstance *tbl_inst;
  4576. table_elem_type_t *table_elems;
  4577. InitializerExpression *tbl_seg_init_values = NULL,
  4578. *init_values;
  4579. uint64 i;
  4580. uint32 tbl_seg_len = 0;
  4581. elem_idx = read_uint32(frame_ip);
  4582. bh_assert(elem_idx < module->module->table_seg_count);
  4583. tbl_idx = read_uint32(frame_ip);
  4584. bh_assert(tbl_idx < module->module->table_count);
  4585. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  4586. n = (uint32)POP_I32();
  4587. s = (uint32)POP_I32();
  4588. d = (uint32)POP_I32();
  4589. if (!bh_bitmap_get_bit(module->e->common.elem_dropped,
  4590. elem_idx)) {
  4591. /* table segment isn't dropped */
  4592. tbl_seg_init_values =
  4593. module->module->table_segments[elem_idx]
  4594. .init_values;
  4595. tbl_seg_len =
  4596. module->module->table_segments[elem_idx]
  4597. .value_count;
  4598. }
  4599. if (offset_len_out_of_bounds(s, n, tbl_seg_len)
  4600. || offset_len_out_of_bounds(d, n,
  4601. tbl_inst->cur_size)) {
  4602. wasm_set_exception(module,
  4603. "out of bounds table access");
  4604. goto got_exception;
  4605. }
  4606. if (!n) {
  4607. break;
  4608. }
  4609. table_elems = tbl_inst->elems + d;
  4610. init_values = tbl_seg_init_values + s;
  4611. #if WASM_ENABLE_GC != 0
  4612. SYNC_ALL_TO_FRAME();
  4613. #endif
  4614. for (i = 0; i < n; i++) {
  4615. /* UINT32_MAX indicates that it is a null ref */
  4616. bh_assert(init_values[i].init_expr_type
  4617. == INIT_EXPR_TYPE_REFNULL_CONST
  4618. || init_values[i].init_expr_type
  4619. == INIT_EXPR_TYPE_FUNCREF_CONST);
  4620. #if WASM_ENABLE_GC == 0
  4621. table_elems[i] =
  4622. (table_elem_type_t)init_values[i].u.ref_index;
  4623. #else
  4624. if (init_values[i].u.ref_index != UINT32_MAX) {
  4625. if (!(func_obj = wasm_create_func_obj(
  4626. module, init_values[i].u.ref_index,
  4627. true, NULL, 0))) {
  4628. goto got_exception;
  4629. }
  4630. table_elems[i] = func_obj;
  4631. }
  4632. else {
  4633. table_elems[i] = NULL_REF;
  4634. }
  4635. #endif
  4636. }
  4637. break;
  4638. }
  4639. case WASM_OP_ELEM_DROP:
  4640. {
  4641. uint32 elem_idx = read_uint32(frame_ip);
  4642. bh_assert(elem_idx < module->module->table_seg_count);
  4643. bh_bitmap_set_bit(module->e->common.elem_dropped,
  4644. elem_idx);
  4645. break;
  4646. }
  4647. case WASM_OP_TABLE_COPY:
  4648. {
  4649. uint32 src_tbl_idx, dst_tbl_idx;
  4650. uint32 n, s, d;
  4651. WASMTableInstance *src_tbl_inst, *dst_tbl_inst;
  4652. dst_tbl_idx = read_uint32(frame_ip);
  4653. bh_assert(dst_tbl_idx < module->table_count);
  4654. dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx);
  4655. src_tbl_idx = read_uint32(frame_ip);
  4656. bh_assert(src_tbl_idx < module->table_count);
  4657. src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx);
  4658. n = (uint32)POP_I32();
  4659. s = (uint32)POP_I32();
  4660. d = (uint32)POP_I32();
  4661. if (offset_len_out_of_bounds(d, n,
  4662. dst_tbl_inst->cur_size)
  4663. || offset_len_out_of_bounds(
  4664. s, n, src_tbl_inst->cur_size)) {
  4665. wasm_set_exception(module,
  4666. "out of bounds table access");
  4667. goto got_exception;
  4668. }
  4669. /* if s >= d, copy from front to back */
  4670. /* if s < d, copy from back to front */
  4671. /* merge all together */
  4672. bh_memmove_s((uint8 *)dst_tbl_inst
  4673. + offsetof(WASMTableInstance, elems)
  4674. + d * sizeof(table_elem_type_t),
  4675. (uint32)((dst_tbl_inst->cur_size - d)
  4676. * sizeof(table_elem_type_t)),
  4677. (uint8 *)src_tbl_inst
  4678. + offsetof(WASMTableInstance, elems)
  4679. + s * sizeof(table_elem_type_t),
  4680. (uint32)(n * sizeof(table_elem_type_t)));
  4681. break;
  4682. }
  4683. case WASM_OP_TABLE_GROW:
  4684. {
  4685. uint32 tbl_idx, n, orig_tbl_sz;
  4686. WASMTableInstance *tbl_inst;
  4687. table_elem_type_t init_val;
  4688. tbl_idx = read_uint32(frame_ip);
  4689. bh_assert(tbl_idx < module->table_count);
  4690. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  4691. orig_tbl_sz = tbl_inst->cur_size;
  4692. n = POP_I32();
  4693. #if WASM_ENABLE_GC == 0
  4694. init_val = POP_I32();
  4695. #else
  4696. init_val = POP_REF();
  4697. #endif
  4698. if (!wasm_enlarge_table(module, tbl_idx, n, init_val)) {
  4699. PUSH_I32(-1);
  4700. }
  4701. else {
  4702. PUSH_I32(orig_tbl_sz);
  4703. }
  4704. break;
  4705. }
  4706. case WASM_OP_TABLE_SIZE:
  4707. {
  4708. uint32 tbl_idx;
  4709. WASMTableInstance *tbl_inst;
  4710. tbl_idx = read_uint32(frame_ip);
  4711. bh_assert(tbl_idx < module->table_count);
  4712. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  4713. PUSH_I32(tbl_inst->cur_size);
  4714. break;
  4715. }
  4716. case WASM_OP_TABLE_FILL:
  4717. {
  4718. uint32 tbl_idx, n, i;
  4719. WASMTableInstance *tbl_inst;
  4720. table_elem_type_t fill_val;
  4721. tbl_idx = read_uint32(frame_ip);
  4722. bh_assert(tbl_idx < module->table_count);
  4723. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  4724. n = POP_I32();
  4725. #if WASM_ENABLE_GC == 0
  4726. fill_val = POP_I32();
  4727. #else
  4728. fill_val = POP_REF();
  4729. #endif
  4730. i = POP_I32();
  4731. if (offset_len_out_of_bounds(i, n,
  4732. tbl_inst->cur_size)) {
  4733. wasm_set_exception(module,
  4734. "out of bounds table access");
  4735. goto got_exception;
  4736. }
  4737. for (; n != 0; i++, n--) {
  4738. tbl_inst->elems[i] = fill_val;
  4739. }
  4740. break;
  4741. }
  4742. #endif /* WASM_ENABLE_REF_TYPES */
  4743. default:
  4744. wasm_set_exception(module, "unsupported opcode");
  4745. goto got_exception;
  4746. }
  4747. HANDLE_OP_END();
  4748. }
  4749. #if WASM_ENABLE_SHARED_MEMORY != 0
  4750. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  4751. {
  4752. uint32 offset = 0, addr;
  4753. GET_OPCODE();
  4754. if (opcode != WASM_OP_ATOMIC_FENCE) {
  4755. offset = read_uint32(frame_ip);
  4756. }
  4757. switch (opcode) {
  4758. case WASM_OP_ATOMIC_NOTIFY:
  4759. {
  4760. uint32 notify_count, ret;
  4761. notify_count = POP_I32();
  4762. addr = POP_I32();
  4763. CHECK_MEMORY_OVERFLOW(4);
  4764. CHECK_ATOMIC_MEMORY_ACCESS(4);
  4765. ret = wasm_runtime_atomic_notify(
  4766. (WASMModuleInstanceCommon *)module, maddr,
  4767. notify_count);
  4768. if (ret == (uint32)-1)
  4769. goto got_exception;
  4770. PUSH_I32(ret);
  4771. break;
  4772. }
  4773. case WASM_OP_ATOMIC_WAIT32:
  4774. {
  4775. uint64 timeout;
  4776. uint32 expect, ret;
  4777. timeout = POP_I64();
  4778. expect = POP_I32();
  4779. addr = POP_I32();
  4780. CHECK_MEMORY_OVERFLOW(4);
  4781. CHECK_ATOMIC_MEMORY_ACCESS(4);
  4782. ret = wasm_runtime_atomic_wait(
  4783. (WASMModuleInstanceCommon *)module, maddr,
  4784. (uint64)expect, timeout, false);
  4785. if (ret == (uint32)-1)
  4786. goto got_exception;
  4787. #if WASM_ENABLE_THREAD_MGR != 0
  4788. CHECK_SUSPEND_FLAGS();
  4789. #endif
  4790. PUSH_I32(ret);
  4791. break;
  4792. }
  4793. case WASM_OP_ATOMIC_WAIT64:
  4794. {
  4795. uint64 timeout, expect;
  4796. uint32 ret;
  4797. timeout = POP_I64();
  4798. expect = POP_I64();
  4799. addr = POP_I32();
  4800. CHECK_MEMORY_OVERFLOW(8);
  4801. CHECK_ATOMIC_MEMORY_ACCESS(8);
  4802. ret = wasm_runtime_atomic_wait(
  4803. (WASMModuleInstanceCommon *)module, maddr, expect,
  4804. timeout, true);
  4805. if (ret == (uint32)-1)
  4806. goto got_exception;
  4807. #if WASM_ENABLE_THREAD_MGR != 0
  4808. CHECK_SUSPEND_FLAGS();
  4809. #endif
  4810. PUSH_I32(ret);
  4811. break;
  4812. }
  4813. case WASM_OP_ATOMIC_FENCE:
  4814. {
  4815. os_atomic_thread_fence(os_memory_order_seq_cst);
  4816. break;
  4817. }
  4818. case WASM_OP_ATOMIC_I32_LOAD:
  4819. case WASM_OP_ATOMIC_I32_LOAD8_U:
  4820. case WASM_OP_ATOMIC_I32_LOAD16_U:
  4821. {
  4822. uint32 readv;
  4823. addr = POP_I32();
  4824. if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) {
  4825. CHECK_MEMORY_OVERFLOW(1);
  4826. CHECK_ATOMIC_MEMORY_ACCESS(1);
  4827. shared_memory_lock(memory);
  4828. readv = (uint32)(*(uint8 *)maddr);
  4829. shared_memory_unlock(memory);
  4830. }
  4831. else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) {
  4832. CHECK_MEMORY_OVERFLOW(2);
  4833. CHECK_ATOMIC_MEMORY_ACCESS(2);
  4834. shared_memory_lock(memory);
  4835. readv = (uint32)LOAD_U16(maddr);
  4836. shared_memory_unlock(memory);
  4837. }
  4838. else {
  4839. CHECK_MEMORY_OVERFLOW(4);
  4840. CHECK_ATOMIC_MEMORY_ACCESS(4);
  4841. shared_memory_lock(memory);
  4842. readv = LOAD_I32(maddr);
  4843. shared_memory_unlock(memory);
  4844. }
  4845. PUSH_I32(readv);
  4846. break;
  4847. }
  4848. case WASM_OP_ATOMIC_I64_LOAD:
  4849. case WASM_OP_ATOMIC_I64_LOAD8_U:
  4850. case WASM_OP_ATOMIC_I64_LOAD16_U:
  4851. case WASM_OP_ATOMIC_I64_LOAD32_U:
  4852. {
  4853. uint64 readv;
  4854. addr = POP_I32();
  4855. if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) {
  4856. CHECK_MEMORY_OVERFLOW(1);
  4857. CHECK_ATOMIC_MEMORY_ACCESS(1);
  4858. shared_memory_lock(memory);
  4859. readv = (uint64)(*(uint8 *)maddr);
  4860. shared_memory_unlock(memory);
  4861. }
  4862. else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) {
  4863. CHECK_MEMORY_OVERFLOW(2);
  4864. CHECK_ATOMIC_MEMORY_ACCESS(2);
  4865. shared_memory_lock(memory);
  4866. readv = (uint64)LOAD_U16(maddr);
  4867. shared_memory_unlock(memory);
  4868. }
  4869. else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) {
  4870. CHECK_MEMORY_OVERFLOW(4);
  4871. CHECK_ATOMIC_MEMORY_ACCESS(4);
  4872. shared_memory_lock(memory);
  4873. readv = (uint64)LOAD_U32(maddr);
  4874. shared_memory_unlock(memory);
  4875. }
  4876. else {
  4877. CHECK_MEMORY_OVERFLOW(8);
  4878. CHECK_ATOMIC_MEMORY_ACCESS(8);
  4879. shared_memory_lock(memory);
  4880. readv = LOAD_I64(maddr);
  4881. shared_memory_unlock(memory);
  4882. }
  4883. PUSH_I64(readv);
  4884. break;
  4885. }
  4886. case WASM_OP_ATOMIC_I32_STORE:
  4887. case WASM_OP_ATOMIC_I32_STORE8:
  4888. case WASM_OP_ATOMIC_I32_STORE16:
  4889. {
  4890. uint32 sval;
  4891. sval = (uint32)POP_I32();
  4892. addr = POP_I32();
  4893. if (opcode == WASM_OP_ATOMIC_I32_STORE8) {
  4894. CHECK_MEMORY_OVERFLOW(1);
  4895. CHECK_ATOMIC_MEMORY_ACCESS(1);
  4896. shared_memory_lock(memory);
  4897. *(uint8 *)maddr = (uint8)sval;
  4898. shared_memory_unlock(memory);
  4899. }
  4900. else if (opcode == WASM_OP_ATOMIC_I32_STORE16) {
  4901. CHECK_MEMORY_OVERFLOW(2);
  4902. CHECK_ATOMIC_MEMORY_ACCESS(2);
  4903. shared_memory_lock(memory);
  4904. STORE_U16(maddr, (uint16)sval);
  4905. shared_memory_unlock(memory);
  4906. }
  4907. else {
  4908. CHECK_MEMORY_OVERFLOW(4);
  4909. CHECK_ATOMIC_MEMORY_ACCESS(4);
  4910. shared_memory_lock(memory);
  4911. STORE_U32(maddr, sval);
  4912. shared_memory_unlock(memory);
  4913. }
  4914. break;
  4915. }
  4916. case WASM_OP_ATOMIC_I64_STORE:
  4917. case WASM_OP_ATOMIC_I64_STORE8:
  4918. case WASM_OP_ATOMIC_I64_STORE16:
  4919. case WASM_OP_ATOMIC_I64_STORE32:
  4920. {
  4921. uint64 sval;
  4922. sval = (uint64)POP_I64();
  4923. addr = POP_I32();
  4924. if (opcode == WASM_OP_ATOMIC_I64_STORE8) {
  4925. CHECK_MEMORY_OVERFLOW(1);
  4926. CHECK_ATOMIC_MEMORY_ACCESS(1);
  4927. shared_memory_lock(memory);
  4928. *(uint8 *)maddr = (uint8)sval;
  4929. shared_memory_unlock(memory);
  4930. }
  4931. else if (opcode == WASM_OP_ATOMIC_I64_STORE16) {
  4932. CHECK_MEMORY_OVERFLOW(2);
  4933. CHECK_ATOMIC_MEMORY_ACCESS(2);
  4934. shared_memory_lock(memory);
  4935. STORE_U16(maddr, (uint16)sval);
  4936. shared_memory_unlock(memory);
  4937. }
  4938. else if (opcode == WASM_OP_ATOMIC_I64_STORE32) {
  4939. CHECK_MEMORY_OVERFLOW(4);
  4940. CHECK_ATOMIC_MEMORY_ACCESS(4);
  4941. shared_memory_lock(memory);
  4942. STORE_U32(maddr, (uint32)sval);
  4943. shared_memory_unlock(memory);
  4944. }
  4945. else {
  4946. CHECK_MEMORY_OVERFLOW(8);
  4947. CHECK_ATOMIC_MEMORY_ACCESS(8);
  4948. shared_memory_lock(memory);
  4949. STORE_I64(maddr, sval);
  4950. shared_memory_unlock(memory);
  4951. }
  4952. break;
  4953. }
  4954. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  4955. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  4956. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  4957. {
  4958. uint32 readv, sval, expect;
  4959. sval = POP_I32();
  4960. expect = POP_I32();
  4961. addr = POP_I32();
  4962. if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) {
  4963. CHECK_MEMORY_OVERFLOW(1);
  4964. CHECK_ATOMIC_MEMORY_ACCESS(1);
  4965. expect = (uint8)expect;
  4966. shared_memory_lock(memory);
  4967. readv = (uint32)(*(uint8 *)maddr);
  4968. if (readv == expect)
  4969. *(uint8 *)maddr = (uint8)(sval);
  4970. shared_memory_unlock(memory);
  4971. }
  4972. else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) {
  4973. CHECK_MEMORY_OVERFLOW(2);
  4974. CHECK_ATOMIC_MEMORY_ACCESS(2);
  4975. expect = (uint16)expect;
  4976. shared_memory_lock(memory);
  4977. readv = (uint32)LOAD_U16(maddr);
  4978. if (readv == expect)
  4979. STORE_U16(maddr, (uint16)(sval));
  4980. shared_memory_unlock(memory);
  4981. }
  4982. else {
  4983. CHECK_MEMORY_OVERFLOW(4);
  4984. CHECK_ATOMIC_MEMORY_ACCESS(4);
  4985. shared_memory_lock(memory);
  4986. readv = LOAD_I32(maddr);
  4987. if (readv == expect)
  4988. STORE_U32(maddr, sval);
  4989. shared_memory_unlock(memory);
  4990. }
  4991. PUSH_I32(readv);
  4992. break;
  4993. }
  4994. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  4995. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  4996. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  4997. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  4998. {
  4999. uint64 readv, sval, expect;
  5000. sval = (uint64)POP_I64();
  5001. expect = (uint64)POP_I64();
  5002. addr = POP_I32();
  5003. if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) {
  5004. CHECK_MEMORY_OVERFLOW(1);
  5005. CHECK_ATOMIC_MEMORY_ACCESS(1);
  5006. expect = (uint8)expect;
  5007. shared_memory_lock(memory);
  5008. readv = (uint64)(*(uint8 *)maddr);
  5009. if (readv == expect)
  5010. *(uint8 *)maddr = (uint8)(sval);
  5011. shared_memory_unlock(memory);
  5012. }
  5013. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) {
  5014. CHECK_MEMORY_OVERFLOW(2);
  5015. CHECK_ATOMIC_MEMORY_ACCESS(2);
  5016. expect = (uint16)expect;
  5017. shared_memory_lock(memory);
  5018. readv = (uint64)LOAD_U16(maddr);
  5019. if (readv == expect)
  5020. STORE_U16(maddr, (uint16)(sval));
  5021. shared_memory_unlock(memory);
  5022. }
  5023. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) {
  5024. CHECK_MEMORY_OVERFLOW(4);
  5025. CHECK_ATOMIC_MEMORY_ACCESS(4);
  5026. expect = (uint32)expect;
  5027. shared_memory_lock(memory);
  5028. readv = (uint64)LOAD_U32(maddr);
  5029. if (readv == expect)
  5030. STORE_U32(maddr, (uint32)(sval));
  5031. shared_memory_unlock(memory);
  5032. }
  5033. else {
  5034. CHECK_MEMORY_OVERFLOW(8);
  5035. CHECK_ATOMIC_MEMORY_ACCESS(8);
  5036. shared_memory_lock(memory);
  5037. readv = (uint64)LOAD_I64(maddr);
  5038. if (readv == expect)
  5039. STORE_I64(maddr, sval);
  5040. shared_memory_unlock(memory);
  5041. }
  5042. PUSH_I64(readv);
  5043. break;
  5044. }
  5045. DEF_ATOMIC_RMW_OPCODE(ADD, +);
  5046. DEF_ATOMIC_RMW_OPCODE(SUB, -);
  5047. DEF_ATOMIC_RMW_OPCODE(AND, &);
  5048. DEF_ATOMIC_RMW_OPCODE(OR, |);
  5049. DEF_ATOMIC_RMW_OPCODE(XOR, ^);
  5050. /* xchg, ignore the read value, and store the given
  5051. value: readv * 0 + sval */
  5052. DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +);
  5053. }
  5054. HANDLE_OP_END();
  5055. }
  5056. #endif
  5057. HANDLE_OP(WASM_OP_IMPDEP)
  5058. {
  5059. frame = prev_frame;
  5060. frame_ip = frame->ip;
  5061. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5062. is_return_call = false;
  5063. #endif
  5064. goto call_func_from_entry;
  5065. }
  5066. HANDLE_OP(WASM_OP_CALL)
  5067. {
  5068. #if WASM_ENABLE_THREAD_MGR != 0
  5069. CHECK_SUSPEND_FLAGS();
  5070. #endif
  5071. fidx = read_uint32(frame_ip);
  5072. #if WASM_ENABLE_MULTI_MODULE != 0
  5073. if (fidx >= module->e->function_count) {
  5074. wasm_set_exception(module, "unknown function");
  5075. goto got_exception;
  5076. }
  5077. #endif
  5078. cur_func = module->e->functions + fidx;
  5079. goto call_func_from_interp;
  5080. }
  5081. #if WASM_ENABLE_TAIL_CALL != 0
  5082. HANDLE_OP(WASM_OP_RETURN_CALL)
  5083. {
  5084. #if WASM_ENABLE_THREAD_MGR != 0
  5085. CHECK_SUSPEND_FLAGS();
  5086. #endif
  5087. fidx = read_uint32(frame_ip);
  5088. #if WASM_ENABLE_MULTI_MODULE != 0
  5089. if (fidx >= module->e->function_count) {
  5090. wasm_set_exception(module, "unknown function");
  5091. goto got_exception;
  5092. }
  5093. #endif
  5094. cur_func = module->e->functions + fidx;
  5095. goto call_func_from_return_call;
  5096. }
  5097. #endif /* WASM_ENABLE_TAIL_CALL */
  5098. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  5099. default:
  5100. wasm_set_exception(module, "unsupported opcode");
  5101. goto got_exception;
  5102. }
  5103. #endif
  5104. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  5105. HANDLE_OP(WASM_OP_UNUSED_0x0a)
  5106. #if WASM_ENABLE_TAIL_CALL == 0
  5107. HANDLE_OP(WASM_OP_RETURN_CALL)
  5108. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  5109. #endif
  5110. #if WASM_ENABLE_SHARED_MEMORY == 0
  5111. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  5112. #endif
  5113. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  5114. HANDLE_OP(WASM_OP_TABLE_GET)
  5115. HANDLE_OP(WASM_OP_TABLE_SET)
  5116. HANDLE_OP(WASM_OP_REF_NULL)
  5117. HANDLE_OP(WASM_OP_REF_IS_NULL)
  5118. HANDLE_OP(WASM_OP_REF_FUNC)
  5119. #endif
  5120. #if WASM_ENABLE_GC == 0
  5121. /* SELECT_T is converted to SELECT or SELECT_64 */
  5122. HANDLE_OP(WASM_OP_SELECT_T)
  5123. HANDLE_OP(WASM_OP_CALL_REF)
  5124. HANDLE_OP(WASM_OP_RETURN_CALL_REF)
  5125. HANDLE_OP(WASM_OP_REF_EQ)
  5126. HANDLE_OP(WASM_OP_REF_AS_NON_NULL)
  5127. HANDLE_OP(WASM_OP_BR_ON_NULL)
  5128. HANDLE_OP(WASM_OP_BR_ON_NON_NULL)
  5129. HANDLE_OP(WASM_OP_GC_PREFIX)
  5130. #endif
  5131. #if WASM_ENABLE_EXCE_HANDLING == 0
  5132. /* if exception handling is disabled, these opcodes issue a trap */
  5133. HANDLE_OP(WASM_OP_TRY)
  5134. HANDLE_OP(WASM_OP_CATCH)
  5135. HANDLE_OP(WASM_OP_THROW)
  5136. HANDLE_OP(WASM_OP_RETHROW)
  5137. HANDLE_OP(WASM_OP_DELEGATE)
  5138. HANDLE_OP(WASM_OP_CATCH_ALL)
  5139. HANDLE_OP(EXT_OP_TRY)
  5140. #endif
  5141. HANDLE_OP(WASM_OP_UNUSED_0x16)
  5142. HANDLE_OP(WASM_OP_UNUSED_0x17)
  5143. HANDLE_OP(WASM_OP_UNUSED_0x27)
  5144. /* optimized op code */
  5145. HANDLE_OP(WASM_OP_F32_STORE)
  5146. HANDLE_OP(WASM_OP_F64_STORE)
  5147. HANDLE_OP(WASM_OP_F32_LOAD)
  5148. HANDLE_OP(WASM_OP_F64_LOAD)
  5149. HANDLE_OP(EXT_OP_GET_LOCAL_FAST)
  5150. HANDLE_OP(WASM_OP_GET_LOCAL)
  5151. HANDLE_OP(WASM_OP_DROP)
  5152. HANDLE_OP(WASM_OP_DROP_64)
  5153. HANDLE_OP(WASM_OP_BLOCK)
  5154. HANDLE_OP(WASM_OP_LOOP)
  5155. HANDLE_OP(WASM_OP_END)
  5156. HANDLE_OP(WASM_OP_NOP)
  5157. HANDLE_OP(EXT_OP_BLOCK)
  5158. HANDLE_OP(EXT_OP_LOOP)
  5159. HANDLE_OP(EXT_OP_IF)
  5160. HANDLE_OP(EXT_OP_BR_TABLE_CACHE)
  5161. {
  5162. wasm_set_exception(module, "unsupported opcode");
  5163. goto got_exception;
  5164. }
  5165. #endif
  5166. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  5167. continue;
  5168. #else
  5169. FETCH_OPCODE_AND_DISPATCH();
  5170. #endif
  5171. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5172. call_func_from_return_call:
  5173. {
  5174. uint32 *lp_base = NULL, *lp = NULL;
  5175. int i;
  5176. if (cur_func->param_cell_num > 0
  5177. && !(lp_base = lp = wasm_runtime_malloc(cur_func->param_cell_num
  5178. * sizeof(uint32)))) {
  5179. wasm_set_exception(module, "allocate memory failed");
  5180. goto got_exception;
  5181. }
  5182. for (i = 0; i < cur_func->param_count; i++) {
  5183. if (cur_func->param_types[i] == VALUE_TYPE_I64
  5184. || cur_func->param_types[i] == VALUE_TYPE_F64) {
  5185. PUT_I64_TO_ADDR(
  5186. lp, GET_OPERAND(uint64, I64,
  5187. 2 * (cur_func->param_count - i - 1)));
  5188. lp += 2;
  5189. }
  5190. else {
  5191. *lp = GET_OPERAND(uint32, I32,
  5192. (2 * (cur_func->param_count - i - 1)));
  5193. lp++;
  5194. }
  5195. }
  5196. frame->lp = frame->operand + cur_func->const_cell_num;
  5197. if (lp - lp_base > 0) {
  5198. word_copy(frame->lp, lp_base, lp - lp_base);
  5199. }
  5200. if (lp_base)
  5201. wasm_runtime_free(lp_base);
  5202. FREE_FRAME(exec_env, frame);
  5203. frame_ip += cur_func->param_count * sizeof(int16);
  5204. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
  5205. is_return_call = true;
  5206. goto call_func_from_entry;
  5207. }
  5208. #endif /* WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0 */
  5209. call_func_from_interp:
  5210. {
  5211. /* Only do the copy when it's called from interpreter. */
  5212. WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  5213. int i;
  5214. #if WASM_ENABLE_MULTI_MODULE != 0
  5215. if (cur_func->is_import_func) {
  5216. outs_area->lp = outs_area->operand
  5217. + (cur_func->import_func_inst
  5218. ? cur_func->import_func_inst->const_cell_num
  5219. : 0);
  5220. }
  5221. else
  5222. #endif
  5223. {
  5224. outs_area->lp = outs_area->operand + cur_func->const_cell_num;
  5225. }
  5226. if ((uint8 *)(outs_area->lp + cur_func->param_cell_num)
  5227. > exec_env->wasm_stack.top_boundary) {
  5228. wasm_set_exception(module, "wasm operand stack overflow");
  5229. goto got_exception;
  5230. }
  5231. for (i = 0; i < cur_func->param_count; i++) {
  5232. if (cur_func->param_types[i] == VALUE_TYPE_I64
  5233. || cur_func->param_types[i] == VALUE_TYPE_F64) {
  5234. PUT_I64_TO_ADDR(
  5235. outs_area->lp,
  5236. GET_OPERAND(uint64, I64,
  5237. 2 * (cur_func->param_count - i - 1)));
  5238. outs_area->lp += 2;
  5239. }
  5240. #if WASM_ENABLE_GC != 0
  5241. else if (wasm_is_type_reftype(cur_func->param_types[i])) {
  5242. PUT_REF_TO_ADDR(
  5243. outs_area->lp,
  5244. GET_OPERAND(void *, REF,
  5245. 2 * (cur_func->param_count - i - 1)));
  5246. CLEAR_FRAME_REF(
  5247. *(uint16 *)(frame_ip
  5248. + (2 * (cur_func->param_count - i - 1))));
  5249. outs_area->lp += REF_CELL_NUM;
  5250. }
  5251. #endif
  5252. else {
  5253. *outs_area->lp = GET_OPERAND(
  5254. uint32, I32, (2 * (cur_func->param_count - i - 1)));
  5255. outs_area->lp++;
  5256. }
  5257. }
  5258. frame_ip += cur_func->param_count * sizeof(int16);
  5259. if (cur_func->ret_cell_num != 0) {
  5260. /* Get the first return value's offset. Since loader emit
  5261. * all return values' offset so we must skip remain return
  5262. * values' offsets.
  5263. */
  5264. WASMFuncType *func_type;
  5265. if (cur_func->is_import_func)
  5266. func_type = cur_func->u.func_import->func_type;
  5267. else
  5268. func_type = cur_func->u.func->func_type;
  5269. frame->ret_offset = GET_OFFSET();
  5270. frame_ip += 2 * (func_type->result_count - 1);
  5271. }
  5272. SYNC_ALL_TO_FRAME();
  5273. prev_frame = frame;
  5274. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5275. is_return_call = false;
  5276. #endif
  5277. }
  5278. call_func_from_entry:
  5279. {
  5280. if (cur_func->is_import_func) {
  5281. #if WASM_ENABLE_MULTI_MODULE != 0
  5282. if (cur_func->import_func_inst) {
  5283. wasm_interp_call_func_import(module, exec_env, cur_func,
  5284. prev_frame);
  5285. }
  5286. else
  5287. #endif
  5288. {
  5289. wasm_interp_call_func_native(module, exec_env, cur_func,
  5290. prev_frame);
  5291. }
  5292. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5293. if (is_return_call) {
  5294. /* the frame was freed before tail calling and
  5295. the prev_frame was set as exec_env's cur_frame,
  5296. so here we recover context from prev_frame */
  5297. RECOVER_CONTEXT(prev_frame);
  5298. }
  5299. else
  5300. #endif
  5301. {
  5302. prev_frame = frame->prev_frame;
  5303. cur_func = frame->function;
  5304. UPDATE_ALL_FROM_FRAME();
  5305. }
  5306. /* update memory size, no need to update memory ptr as
  5307. it isn't changed in wasm_enlarge_memory */
  5308. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  5309. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  5310. || WASM_ENABLE_BULK_MEMORY != 0
  5311. if (memory)
  5312. linear_mem_size = get_linear_mem_size();
  5313. #endif
  5314. if (wasm_copy_exception(module, NULL))
  5315. goto got_exception;
  5316. }
  5317. else {
  5318. WASMFunction *cur_wasm_func = cur_func->u.func;
  5319. uint32 cell_num_of_local_stack;
  5320. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  5321. uint32 i, local_cell_idx;
  5322. #endif
  5323. cell_num_of_local_stack = cur_func->param_cell_num
  5324. + cur_func->local_cell_num
  5325. + cur_wasm_func->max_stack_cell_num;
  5326. all_cell_num = cur_func->const_cell_num + cell_num_of_local_stack;
  5327. #if WASM_ENABLE_GC != 0
  5328. /* area of frame_ref */
  5329. all_cell_num += (cell_num_of_local_stack + 3) / 4;
  5330. /* cells occupied by locals, POP_REF should not clear frame_ref for
  5331. * these cells */
  5332. local_cell_num =
  5333. cur_func->param_cell_num + cur_func->local_cell_num;
  5334. #endif
  5335. /* param_cell_num, local_cell_num, const_cell_num and
  5336. max_stack_cell_num are all no larger than UINT16_MAX (checked
  5337. in loader), all_cell_num must be smaller than 1MB */
  5338. bh_assert(all_cell_num < 1 * BH_MB);
  5339. frame_size = wasm_interp_interp_frame_size(all_cell_num);
  5340. if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) {
  5341. frame = prev_frame;
  5342. goto got_exception;
  5343. }
  5344. /* Initialize the interpreter context. */
  5345. frame->function = cur_func;
  5346. frame_ip = wasm_get_func_code(cur_func);
  5347. frame_ip_end = wasm_get_func_code_end(cur_func);
  5348. frame_lp = frame->lp =
  5349. frame->operand + cur_wasm_func->const_cell_num;
  5350. /* Initialize the consts */
  5351. if (cur_wasm_func->const_cell_num > 0) {
  5352. word_copy(frame->operand, (uint32 *)cur_wasm_func->consts,
  5353. cur_wasm_func->const_cell_num);
  5354. }
  5355. /* Initialize the local variables */
  5356. memset(frame_lp + cur_func->param_cell_num, 0,
  5357. (uint32)(cur_func->local_cell_num * 4));
  5358. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  5359. /* externref/funcref should be NULL_REF rather than 0 */
  5360. local_cell_idx = cur_func->param_cell_num;
  5361. for (i = 0; i < cur_wasm_func->local_count; i++) {
  5362. if (cur_wasm_func->local_types[i] == VALUE_TYPE_EXTERNREF
  5363. || cur_wasm_func->local_types[i] == VALUE_TYPE_FUNCREF) {
  5364. *(frame_lp + local_cell_idx) = NULL_REF;
  5365. }
  5366. local_cell_idx +=
  5367. wasm_value_type_cell_num(cur_wasm_func->local_types[i]);
  5368. }
  5369. #endif
  5370. #if WASM_ENABLE_GC != 0
  5371. /* frame->ip is used during GC root set enumeration, so we must
  5372. * initialized this field here */
  5373. frame->ip = frame_ip;
  5374. frame_ref = frame->frame_ref =
  5375. (uint8 *)(frame->lp + (uint32)cell_num_of_local_stack);
  5376. init_frame_refs(frame_ref, (uint32)cell_num_of_local_stack,
  5377. cur_func);
  5378. #endif
  5379. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)frame);
  5380. }
  5381. #if WASM_ENABLE_THREAD_MGR != 0
  5382. CHECK_SUSPEND_FLAGS();
  5383. #endif
  5384. HANDLE_OP_END();
  5385. }
  5386. return_func:
  5387. {
  5388. FREE_FRAME(exec_env, frame);
  5389. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
  5390. if (!prev_frame->ip)
  5391. /* Called from native. */
  5392. return;
  5393. RECOVER_CONTEXT(prev_frame);
  5394. #if WASM_ENABLE_GC != 0
  5395. local_cell_num = cur_func->param_cell_num + cur_func->local_cell_num;
  5396. #endif
  5397. HANDLE_OP_END();
  5398. }
  5399. (void)frame_ip_end;
  5400. #if WASM_ENABLE_SHARED_MEMORY != 0
  5401. unaligned_atomic:
  5402. wasm_set_exception(module, "unaligned atomic");
  5403. goto got_exception;
  5404. #endif
  5405. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  5406. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  5407. || WASM_ENABLE_BULK_MEMORY != 0
  5408. out_of_bounds:
  5409. wasm_set_exception(module, "out of bounds memory access");
  5410. #endif
  5411. got_exception:
  5412. SYNC_ALL_TO_FRAME();
  5413. return;
  5414. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  5415. }
  5416. #else
  5417. FETCH_OPCODE_AND_DISPATCH();
  5418. #endif
  5419. }
  5420. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  5421. void **
  5422. wasm_interp_get_handle_table(void)
  5423. {
  5424. WASMModuleInstance module;
  5425. memset(&module, 0, sizeof(WASMModuleInstance));
  5426. wasm_interp_call_func_bytecode(&module, NULL, NULL, NULL);
  5427. return global_handle_table;
  5428. }
  5429. #endif
  5430. #if WASM_ENABLE_GC != 0
  5431. bool
  5432. wasm_interp_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
  5433. {
  5434. WASMInterpFrame *frame;
  5435. WASMObjectRef gc_obj;
  5436. WASMFunctionInstance *cur_func;
  5437. uint8 *frame_ref;
  5438. uint32 local_cell_num, i;
  5439. frame = wasm_exec_env_get_cur_frame(exec_env);
  5440. for (; frame; frame = frame->prev_frame) {
  5441. frame_ref = frame->frame_ref;
  5442. cur_func = frame->function;
  5443. if (!cur_func)
  5444. continue;
  5445. local_cell_num = cur_func->param_cell_num;
  5446. if (frame->ip)
  5447. local_cell_num +=
  5448. cur_func->local_cell_num + cur_func->u.func->max_stack_cell_num;
  5449. for (i = 0; i < local_cell_num; i++) {
  5450. if (frame_ref[i]) {
  5451. gc_obj = GET_REF_FROM_ADDR(frame->lp + i);
  5452. if (wasm_obj_is_created_from_heap(gc_obj)) {
  5453. if (mem_allocator_add_root((mem_allocator_t)heap, gc_obj)) {
  5454. return false;
  5455. }
  5456. }
  5457. #if UINTPTR_MAX == UINT64_MAX
  5458. bh_assert(frame_ref[i + 1]);
  5459. i++;
  5460. #endif
  5461. }
  5462. }
  5463. }
  5464. return true;
  5465. }
  5466. #endif
  5467. void
  5468. wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  5469. WASMFunctionInstance *function, uint32 argc,
  5470. uint32 argv[])
  5471. {
  5472. WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  5473. WASMInterpFrame *frame, *outs_area;
  5474. /* Allocate sufficient cells for all kinds of return values. */
  5475. unsigned all_cell_num =
  5476. function->ret_cell_num > 2 ? function->ret_cell_num : 2,
  5477. i;
  5478. /* This frame won't be used by JITed code, so only allocate interp
  5479. frame here. */
  5480. unsigned frame_size;
  5481. #if WASM_ENABLE_GC != 0
  5482. all_cell_num += (all_cell_num + 3) / 4;
  5483. #endif
  5484. frame_size = wasm_interp_interp_frame_size(all_cell_num);
  5485. if (argc < function->param_cell_num) {
  5486. char buf[128];
  5487. snprintf(buf, sizeof(buf),
  5488. "invalid argument count %" PRIu32
  5489. ", must be no smaller than %" PRIu32,
  5490. argc, (uint32)function->param_cell_num);
  5491. wasm_set_exception(module_inst, buf);
  5492. return;
  5493. }
  5494. argc = function->param_cell_num;
  5495. #if defined(OS_ENABLE_HW_BOUND_CHECK) && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  5496. /*
  5497. * wasm_runtime_detect_native_stack_overflow is done by
  5498. * call_wasm_with_hw_bound_check.
  5499. */
  5500. #else
  5501. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  5502. return;
  5503. }
  5504. #endif
  5505. if (!(frame =
  5506. ALLOC_FRAME(exec_env, frame_size, (WASMInterpFrame *)prev_frame)))
  5507. return;
  5508. outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  5509. frame->function = NULL;
  5510. frame->ip = NULL;
  5511. /* There is no local variable. */
  5512. frame->lp = frame->operand + 0;
  5513. #if WASM_ENABLE_GC != 0
  5514. frame->frame_ref =
  5515. (uint8 *)(frame->lp
  5516. + (function->ret_cell_num > 2 ? function->ret_cell_num : 2));
  5517. #endif
  5518. frame->ret_offset = 0;
  5519. if ((uint8 *)(outs_area->operand + function->const_cell_num + argc)
  5520. > exec_env->wasm_stack.top_boundary) {
  5521. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  5522. "wasm operand stack overflow");
  5523. return;
  5524. }
  5525. if (argc > 0)
  5526. word_copy(outs_area->operand + function->const_cell_num, argv, argc);
  5527. wasm_exec_env_set_cur_frame(exec_env, frame);
  5528. #if defined(os_writegsbase)
  5529. {
  5530. WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
  5531. if (memory_inst)
  5532. /* write base addr of linear memory to GS segment register */
  5533. os_writegsbase(memory_inst->memory_data);
  5534. }
  5535. #endif
  5536. if (function->is_import_func) {
  5537. #if WASM_ENABLE_MULTI_MODULE != 0
  5538. if (function->import_module_inst) {
  5539. LOG_DEBUG("it is a function of a sub module");
  5540. wasm_interp_call_func_import(module_inst, exec_env, function,
  5541. frame);
  5542. }
  5543. else
  5544. #endif
  5545. {
  5546. LOG_DEBUG("it is an native function");
  5547. wasm_interp_call_func_native(module_inst, exec_env, function,
  5548. frame);
  5549. }
  5550. }
  5551. else {
  5552. wasm_interp_call_func_bytecode(module_inst, exec_env, function, frame);
  5553. }
  5554. /* Output the return value to the caller */
  5555. if (!wasm_copy_exception(module_inst, NULL)) {
  5556. for (i = 0; i < function->ret_cell_num; i++)
  5557. argv[i] = *(frame->lp + i);
  5558. }
  5559. else {
  5560. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  5561. if (wasm_interp_create_call_stack(exec_env)) {
  5562. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  5563. }
  5564. #endif
  5565. }
  5566. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  5567. FREE_FRAME(exec_env, frame);
  5568. #if WASM_ENABLE_OPCODE_COUNTER != 0
  5569. wasm_interp_dump_op_count();
  5570. #endif
  5571. }