wasm_interp_fast.c 228 KB

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