wasm_interp_fast.c 228 KB

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