wasm_interp_classic.c 272 KB

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