wasm_interp_classic.c 272 KB

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