wasm_loader.c 479 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "wasm.h"
  9. #include "wasm_opcode.h"
  10. #include "wasm_runtime.h"
  11. #include "../common/wasm_native.h"
  12. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_GC != 0
  14. #include "../common/gc/gc_type.h"
  15. #include "../common/gc/gc_object.h"
  16. #endif
  17. #if WASM_ENABLE_DEBUG_INTERP != 0
  18. #include "../libraries/debug-engine/debug_engine.h"
  19. #endif
  20. #if WASM_ENABLE_FAST_JIT != 0
  21. #include "../fast-jit/jit_compiler.h"
  22. #include "../fast-jit/jit_codecache.h"
  23. #endif
  24. #if WASM_ENABLE_JIT != 0
  25. #include "../compilation/aot_llvm.h"
  26. #endif
  27. #ifndef TRACE_WASM_LOADER
  28. #define TRACE_WASM_LOADER 0
  29. #endif
  30. /* Read a value of given type from the address pointed to by the given
  31. pointer and increase the pointer to the position just after the
  32. value being read. */
  33. #define TEMPLATE_READ_VALUE(Type, p) \
  34. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  35. static void
  36. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  37. {
  38. if (error_buf != NULL) {
  39. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  40. string);
  41. }
  42. }
  43. static void
  44. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  45. {
  46. va_list args;
  47. char buf[128];
  48. if (error_buf != NULL) {
  49. va_start(args, format);
  50. vsnprintf(buf, sizeof(buf), format, args);
  51. va_end(args);
  52. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  53. }
  54. }
  55. static bool
  56. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  57. char *error_buf, uint32 error_buf_size)
  58. {
  59. if ((uintptr_t)buf + length < (uintptr_t)buf
  60. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  61. set_error_buf(error_buf, error_buf_size,
  62. "unexpected end of section or function");
  63. return false;
  64. }
  65. return true;
  66. }
  67. static bool
  68. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  69. char *error_buf, uint32 error_buf_size)
  70. {
  71. if ((uintptr_t)buf + length < (uintptr_t)buf
  72. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  73. set_error_buf(error_buf, error_buf_size, "unexpected end");
  74. return false;
  75. }
  76. return true;
  77. }
  78. #define CHECK_BUF(buf, buf_end, length) \
  79. do { \
  80. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  81. goto fail; \
  82. } \
  83. } while (0)
  84. #define CHECK_BUF1(buf, buf_end, length) \
  85. do { \
  86. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  87. goto fail; \
  88. } \
  89. } while (0)
  90. #define skip_leb(p) while (*p++ & 0x80)
  91. #define skip_leb_int64(p, p_end) skip_leb(p)
  92. #define skip_leb_uint32(p, p_end) skip_leb(p)
  93. #define skip_leb_int32(p, p_end) skip_leb(p)
  94. static bool
  95. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  96. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  97. {
  98. const uint8 *buf = *p_buf;
  99. uint64 result = 0;
  100. uint32 shift = 0;
  101. uint32 offset = 0, bcnt = 0;
  102. uint64 byte;
  103. while (true) {
  104. /* uN or SN must not exceed ceil(N/7) bytes */
  105. if (bcnt + 1 > (maxbits + 6) / 7) {
  106. set_error_buf(error_buf, error_buf_size,
  107. "integer representation too long");
  108. return false;
  109. }
  110. CHECK_BUF(buf, buf_end, offset + 1);
  111. byte = buf[offset];
  112. offset += 1;
  113. result |= ((byte & 0x7f) << shift);
  114. shift += 7;
  115. bcnt += 1;
  116. if ((byte & 0x80) == 0) {
  117. break;
  118. }
  119. }
  120. if (!sign && maxbits == 32 && shift >= maxbits) {
  121. /* The top bits set represent values > 32 bits */
  122. if (((uint8)byte) & 0xf0)
  123. goto fail_integer_too_large;
  124. }
  125. else if (sign && maxbits == 32) {
  126. if (shift < maxbits) {
  127. /* Sign extend, second highest bit is the sign bit */
  128. if ((uint8)byte & 0x40)
  129. result |= (~((uint64)0)) << shift;
  130. }
  131. else {
  132. /* The top bits should be a sign-extension of the sign bit */
  133. bool sign_bit_set = ((uint8)byte) & 0x8;
  134. int top_bits = ((uint8)byte) & 0xf0;
  135. if ((sign_bit_set && top_bits != 0x70)
  136. || (!sign_bit_set && top_bits != 0))
  137. goto fail_integer_too_large;
  138. }
  139. }
  140. else if (sign && maxbits == 64) {
  141. if (shift < maxbits) {
  142. /* Sign extend, second highest bit is the sign bit */
  143. if ((uint8)byte & 0x40)
  144. result |= (~((uint64)0)) << shift;
  145. }
  146. else {
  147. /* The top bits should be a sign-extension of the sign bit */
  148. bool sign_bit_set = ((uint8)byte) & 0x1;
  149. int top_bits = ((uint8)byte) & 0xfe;
  150. if ((sign_bit_set && top_bits != 0x7e)
  151. || (!sign_bit_set && top_bits != 0))
  152. goto fail_integer_too_large;
  153. }
  154. }
  155. *p_buf += offset;
  156. *p_result = result;
  157. return true;
  158. fail_integer_too_large:
  159. set_error_buf(error_buf, error_buf_size, "integer too large");
  160. fail:
  161. return false;
  162. }
  163. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  164. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  165. #define read_bool(p) TEMPLATE_READ_VALUE(bool, p)
  166. #define read_leb_int64(p, p_end, res) \
  167. do { \
  168. uint64 res64; \
  169. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  170. error_buf_size)) \
  171. goto fail; \
  172. res = (int64)res64; \
  173. } while (0)
  174. #define read_leb_uint32(p, p_end, res) \
  175. do { \
  176. uint64 res64; \
  177. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  178. error_buf_size)) \
  179. goto fail; \
  180. res = (uint32)res64; \
  181. } while (0)
  182. #define read_leb_int32(p, p_end, res) \
  183. do { \
  184. uint64 res64; \
  185. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  186. error_buf_size)) \
  187. goto fail; \
  188. res = (int32)res64; \
  189. } while (0)
  190. static char *
  191. type2str(uint8 type)
  192. {
  193. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  194. #if WASM_ENABLE_GC != 0
  195. char *type_str_ref[] = { "nullref", "arrayref", "structref",
  196. "nullexternref", "nullfuncref", "i31ref",
  197. "(ref ht)", "(ref null ht)", "eqref",
  198. "anyref", "externref", "funcref" };
  199. #endif
  200. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  201. return type_str[type - VALUE_TYPE_V128];
  202. #if WASM_ENABLE_GC != 0
  203. else if (wasm_is_type_reftype(type))
  204. return type_str_ref[type - REF_TYPE_NULLREF];
  205. #endif
  206. else if (type == VALUE_TYPE_FUNCREF)
  207. return "funcref";
  208. else if (type == VALUE_TYPE_EXTERNREF)
  209. return "externref";
  210. else
  211. return "unknown type";
  212. }
  213. static bool
  214. is_32bit_type(uint8 type)
  215. {
  216. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  217. #if WASM_ENABLE_GC != 0
  218. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  219. #elif WASM_ENABLE_REF_TYPES != 0
  220. /* For reference types, we use uint32 index to represent
  221. the funcref and externref */
  222. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  223. #endif
  224. )
  225. return true;
  226. return false;
  227. }
  228. static bool
  229. is_64bit_type(uint8 type)
  230. {
  231. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  232. #if WASM_ENABLE_GC != 0
  233. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  234. #endif
  235. )
  236. return true;
  237. return false;
  238. }
  239. static bool
  240. is_value_type(uint8 type)
  241. {
  242. if (/* I32/I64/F32/F64, 0x7C to 0x7F */
  243. (type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32)
  244. #if WASM_ENABLE_GC != 0
  245. /* reference types, 0x65 to 0x70 */
  246. || wasm_is_type_reftype(type)
  247. #elif WASM_ENABLE_REF_TYPES != 0
  248. || (type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF)
  249. #endif
  250. #if WASM_ENABLE_SIMD != 0
  251. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  252. || type == VALUE_TYPE_V128 /* 0x7B */
  253. #endif
  254. #endif
  255. )
  256. return true;
  257. return false;
  258. }
  259. #if WASM_ENABLE_GC != 0
  260. static bool
  261. is_packed_type(uint8 type)
  262. {
  263. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  264. }
  265. #endif
  266. static bool
  267. is_byte_a_type(uint8 type)
  268. {
  269. return (is_value_type(type) || (type == VALUE_TYPE_VOID)) ? true : false;
  270. }
  271. #if WASM_ENABLE_SIMD != 0
  272. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  273. static V128
  274. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  275. {
  276. V128 result;
  277. uint8 i;
  278. for (i = 0; i != 16; ++i) {
  279. result.i8x16[i] = read_uint8(p_buf);
  280. }
  281. return result;
  282. }
  283. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  284. #endif /* end of WASM_ENABLE_SIMD */
  285. static void *
  286. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  287. {
  288. void *mem;
  289. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  290. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  291. return NULL;
  292. }
  293. memset(mem, 0, (uint32)size);
  294. return mem;
  295. }
  296. static bool
  297. check_utf8_str(const uint8 *str, uint32 len)
  298. {
  299. /* The valid ranges are taken from page 125, below link
  300. https://www.unicode.org/versions/Unicode9.0.0/ch03.pdf */
  301. const uint8 *p = str, *p_end = str + len;
  302. uint8 chr;
  303. while (p < p_end) {
  304. chr = *p;
  305. if (chr < 0x80) {
  306. p++;
  307. }
  308. else if (chr >= 0xC2 && chr <= 0xDF && p + 1 < p_end) {
  309. if (p[1] < 0x80 || p[1] > 0xBF) {
  310. return false;
  311. }
  312. p += 2;
  313. }
  314. else if (chr >= 0xE0 && chr <= 0xEF && p + 2 < p_end) {
  315. if (chr == 0xE0) {
  316. if (p[1] < 0xA0 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  317. return false;
  318. }
  319. }
  320. else if (chr == 0xED) {
  321. if (p[1] < 0x80 || p[1] > 0x9F || p[2] < 0x80 || p[2] > 0xBF) {
  322. return false;
  323. }
  324. }
  325. else if (chr >= 0xE1 && chr <= 0xEF) {
  326. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  327. return false;
  328. }
  329. }
  330. p += 3;
  331. }
  332. else if (chr >= 0xF0 && chr <= 0xF4 && p + 3 < p_end) {
  333. if (chr == 0xF0) {
  334. if (p[1] < 0x90 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  335. || p[3] < 0x80 || p[3] > 0xBF) {
  336. return false;
  337. }
  338. }
  339. else if (chr >= 0xF1 && chr <= 0xF3) {
  340. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  341. || p[3] < 0x80 || p[3] > 0xBF) {
  342. return false;
  343. }
  344. }
  345. else if (chr == 0xF4) {
  346. if (p[1] < 0x80 || p[1] > 0x8F || p[2] < 0x80 || p[2] > 0xBF
  347. || p[3] < 0x80 || p[3] > 0xBF) {
  348. return false;
  349. }
  350. }
  351. p += 4;
  352. }
  353. else {
  354. return false;
  355. }
  356. }
  357. return (p == p_end);
  358. }
  359. static char *
  360. const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
  361. bool is_load_from_file_buf, char *error_buf,
  362. uint32 error_buf_size)
  363. {
  364. StringNode *node, *node_next;
  365. if (!check_utf8_str(str, len)) {
  366. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  367. return NULL;
  368. }
  369. if (len == 0) {
  370. return "";
  371. }
  372. else if (is_load_from_file_buf) {
  373. /* As the file buffer can be referred to after loading, we use
  374. the previous byte of leb encoded size to adjust the string:
  375. move string 1 byte backward and then append '\0' */
  376. char *c_str = (char *)str - 1;
  377. bh_memmove_s(c_str, len + 1, c_str + 1, len);
  378. c_str[len] = '\0';
  379. return c_str;
  380. }
  381. /* Search const str list */
  382. node = module->const_str_list;
  383. while (node) {
  384. node_next = node->next;
  385. if (strlen(node->str) == len && !memcmp(node->str, str, len))
  386. break;
  387. node = node_next;
  388. }
  389. if (node) {
  390. return node->str;
  391. }
  392. if (!(node = loader_malloc(sizeof(StringNode) + len + 1, error_buf,
  393. error_buf_size))) {
  394. return NULL;
  395. }
  396. node->str = ((char *)node) + sizeof(StringNode);
  397. bh_memcpy_s(node->str, len + 1, str, len);
  398. node->str[len] = '\0';
  399. if (!module->const_str_list) {
  400. /* set as head */
  401. module->const_str_list = node;
  402. node->next = NULL;
  403. }
  404. else {
  405. /* insert it */
  406. node->next = module->const_str_list;
  407. module->const_str_list = node;
  408. }
  409. return node->str;
  410. }
  411. #if WASM_ENABLE_GC != 0
  412. static bool
  413. check_type_index(const WASMModule *module, uint32 type_index, char *error_buf,
  414. uint32 error_buf_size)
  415. {
  416. if (type_index >= module->type_count) {
  417. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  418. type_index);
  419. return false;
  420. }
  421. return true;
  422. }
  423. static bool
  424. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  425. uint32 error_buf_size)
  426. {
  427. if (!check_type_index(module, type_index, error_buf, error_buf_size)) {
  428. return false;
  429. }
  430. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  431. set_error_buf(error_buf, error_buf_size, "unkown array type");
  432. return false;
  433. }
  434. return true;
  435. }
  436. #endif
  437. static bool
  438. check_function_index(const WASMModule *module, uint32 function_index,
  439. char *error_buf, uint32 error_buf_size)
  440. {
  441. if (function_index
  442. >= module->import_function_count + module->function_count) {
  443. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  444. function_index);
  445. return false;
  446. }
  447. return true;
  448. }
  449. static bool
  450. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  451. InitializerExpression *init_expr, uint8 type, void *ref_type,
  452. char *error_buf, uint32 error_buf_size)
  453. {
  454. const uint8 *p = *p_buf, *p_end = buf_end;
  455. uint8 flag, end_byte, *p_float;
  456. uint32 i;
  457. #if WASM_ENABLE_GC != 0
  458. uint32 type_idx;
  459. #endif
  460. CHECK_BUF(p, p_end, 1);
  461. init_expr->init_expr_type = read_uint8(p);
  462. flag = init_expr->init_expr_type;
  463. switch (flag) {
  464. /* i32.const */
  465. case INIT_EXPR_TYPE_I32_CONST:
  466. read_leb_int32(p, p_end, init_expr->u.i32);
  467. #if WASM_ENABLE_GC == 0
  468. if (type != VALUE_TYPE_I32)
  469. goto fail_type_mismatch;
  470. #else
  471. if (type != VALUE_TYPE_I32) {
  472. if (!wasm_reftype_is_subtype_of(
  473. type, (WASMRefType *)ref_type, REF_TYPE_I31REF, NULL,
  474. module->types, module->type_count)) {
  475. goto fail_type_mismatch;
  476. }
  477. else {
  478. CHECK_BUF(p, p_end, 2);
  479. if (*p == WASM_OP_GC_PREFIX
  480. && *(p + 1) == WASM_OP_I31_NEW) {
  481. p += 2;
  482. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  483. }
  484. else
  485. goto fail_type_mismatch;
  486. }
  487. }
  488. #endif
  489. break;
  490. /* i64.const */
  491. case INIT_EXPR_TYPE_I64_CONST:
  492. if (type != VALUE_TYPE_I64)
  493. goto fail_type_mismatch;
  494. read_leb_int64(p, p_end, init_expr->u.i64);
  495. break;
  496. /* f32.const */
  497. case INIT_EXPR_TYPE_F32_CONST:
  498. if (type != VALUE_TYPE_F32)
  499. goto fail_type_mismatch;
  500. CHECK_BUF(p, p_end, 4);
  501. p_float = (uint8 *)&init_expr->u.f32;
  502. for (i = 0; i < sizeof(float32); i++)
  503. *p_float++ = *p++;
  504. break;
  505. /* f64.const */
  506. case INIT_EXPR_TYPE_F64_CONST:
  507. if (type != VALUE_TYPE_F64)
  508. goto fail_type_mismatch;
  509. CHECK_BUF(p, p_end, 8);
  510. p_float = (uint8 *)&init_expr->u.f64;
  511. for (i = 0; i < sizeof(float64); i++)
  512. *p_float++ = *p++;
  513. break;
  514. #if WASM_ENABLE_SIMD != 0
  515. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  516. /* v128.const */
  517. case INIT_EXPR_TYPE_V128_CONST:
  518. {
  519. uint64 high, low;
  520. if (type != VALUE_TYPE_V128)
  521. goto fail_type_mismatch;
  522. flag = read_uint8(p);
  523. (void)flag;
  524. CHECK_BUF(p, p_end, 16);
  525. wasm_runtime_read_v128(p, &high, &low);
  526. p += 16;
  527. init_expr->u.v128.i64x2[0] = high;
  528. init_expr->u.v128.i64x2[1] = low;
  529. break;
  530. }
  531. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  532. #endif /* end of WASM_ENABLE_SIMD */
  533. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  534. /* ref.func */
  535. case INIT_EXPR_TYPE_FUNCREF_CONST:
  536. {
  537. #if WASM_ENABLE_GC == 0
  538. if (type != VALUE_TYPE_FUNCREF)
  539. goto fail_type_mismatch;
  540. #else
  541. if (!wasm_reftype_is_subtype_of(type, (WASMRefType *)ref_type,
  542. VALUE_TYPE_FUNCREF, NULL,
  543. module->types, module->type_count))
  544. goto fail_type_mismatch;
  545. #endif
  546. read_leb_uint32(p, p_end, init_expr->u.ref_index);
  547. if (!check_function_index(module, init_expr->u.ref_index, error_buf,
  548. error_buf_size)) {
  549. return false;
  550. }
  551. break;
  552. }
  553. /* ref.null */
  554. case INIT_EXPR_TYPE_REFNULL_CONST:
  555. {
  556. uint8 type1;
  557. #if WASM_ENABLE_GC == 0
  558. CHECK_BUF(p, p_end, 1);
  559. type1 = read_uint8(p);
  560. if (type1 != type)
  561. goto fail_type_mismatch;
  562. init_expr->u.ref_index = NULL_REF;
  563. #else
  564. WASMRefType ref_type1 = { 0 };
  565. type1 = read_uint8(p);
  566. if (!is_byte_a_type(type1)) {
  567. p--;
  568. read_leb_uint32(p, p_end, type_idx);
  569. if (!check_type_index(module, type_idx, error_buf,
  570. error_buf_size))
  571. return false;
  572. wasm_set_refheaptype_typeidx(&ref_type1.ref_ht_typeidx, false,
  573. type_idx);
  574. type1 = ref_type1.ref_type;
  575. }
  576. if (!wasm_reftype_is_subtype_of(type1, &ref_type1, type, ref_type,
  577. module->types,
  578. module->type_count)) {
  579. goto fail_type_mismatch;
  580. }
  581. /* Use UINT32_MAX to indicate that it is an null reference */
  582. init_expr->u.ref_index = UINT32_MAX;
  583. #endif
  584. break;
  585. }
  586. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  587. /* get_global */
  588. case INIT_EXPR_TYPE_GET_GLOBAL:
  589. {
  590. uint32 global_idx;
  591. read_leb_uint32(p, p_end, init_expr->u.global_index);
  592. global_idx = init_expr->u.global_index;
  593. #if WASM_ENABLE_GC == 0
  594. if (global_idx >= module->import_global_count) {
  595. /**
  596. * Currently, constant expressions occurring as initializers
  597. * of globals are further constrained in that contained
  598. * global.get instructions are
  599. * only allowed to refer to imported globals.
  600. */
  601. set_error_buf_v(error_buf, error_buf_size, "unknown global %u",
  602. global_idx);
  603. return false;
  604. }
  605. if (module->import_globals[global_idx].u.global.is_mutable) {
  606. set_error_buf_v(error_buf, error_buf_size,
  607. "constant expression required");
  608. return false;
  609. }
  610. #else
  611. if (global_idx
  612. >= module->import_global_count + module->global_count) {
  613. set_error_buf_v(error_buf, error_buf_size, "unknown global %u",
  614. global_idx);
  615. return false;
  616. }
  617. if (global_idx < module->import_global_count
  618. && module->import_globals[global_idx].u.global.is_mutable) {
  619. set_error_buf_v(error_buf, error_buf_size,
  620. "constant expression required");
  621. return false;
  622. }
  623. #endif
  624. break;
  625. }
  626. #if WASM_ENABLE_GC != 0
  627. /* struct.new and array.new */
  628. case WASM_OP_GC_PREFIX:
  629. {
  630. uint8 opcode1;
  631. CHECK_BUF(p, p_end, 1);
  632. opcode1 = read_uint8(p);
  633. switch (opcode1) {
  634. case WASM_OP_STRUCT_NEW_CANON:
  635. {
  636. set_error_buf(
  637. error_buf, error_buf_size,
  638. "unsuppoted constant expression of struct.new_canon");
  639. return false;
  640. }
  641. case WASM_OP_STRUCT_NEW_CANON_DEFAULT:
  642. {
  643. init_expr->init_expr_type =
  644. INIT_EXPR_TYPE_STRUCT_NEW_CANON_DEFAULT;
  645. read_leb_uint32(p, p_end, init_expr->u.type_index);
  646. type_idx = init_expr->u.type_index;
  647. if (!check_type_index(module, type_idx, error_buf,
  648. error_buf_size)) {
  649. return false;
  650. }
  651. if (module->types[type_idx]->type_flag
  652. != WASM_TYPE_STRUCT) {
  653. set_error_buf(error_buf, error_buf_size,
  654. "unkown struct type");
  655. return false;
  656. }
  657. break;
  658. }
  659. case WASM_OP_ARRAY_NEW_CANON:
  660. {
  661. set_error_buf(
  662. error_buf, error_buf_size,
  663. "unsuppoted constant expression of array.new_canon");
  664. return false;
  665. }
  666. case WASM_OP_ARRAY_NEW_CANON_DEFAULT:
  667. case WASM_OP_ARRAY_NEW_CANON_FIXED:
  668. {
  669. init_expr->init_expr_type =
  670. (opcode1 == WASM_OP_ARRAY_NEW_CANON_DEFAULT)
  671. ? INIT_EXPR_TYPE_ARRAY_NEW_CANON_DEFAULT
  672. : INIT_EXPR_TYPE_ARRAY_NEW_CANON_FIXED;
  673. read_leb_uint32(p, p_end, init_expr->u.type_index);
  674. type_idx = init_expr->u.type_index;
  675. if (!check_array_type(module, type_idx, error_buf,
  676. error_buf_size)) {
  677. return false;
  678. }
  679. if (opcode1 == WASM_OP_ARRAY_NEW_CANON_FIXED) {
  680. read_leb_uint32(p, p_end,
  681. init_expr->u.array_new_canon_fixed.N);
  682. }
  683. break;
  684. }
  685. case WASM_OP_EXTERN_INTERNALIZE:
  686. {
  687. set_error_buf(
  688. error_buf, error_buf_size,
  689. "unsuppoted constant expression of extern.internalize");
  690. return false;
  691. }
  692. case WASM_OP_EXTERN_EXTERNALIZE:
  693. {
  694. set_error_buf(
  695. error_buf, error_buf_size,
  696. "unsuppoted constant expression of extern.externalize");
  697. return false;
  698. }
  699. default:
  700. goto fail_type_mismatch;
  701. }
  702. break;
  703. }
  704. #endif /* end of WASM_ENABLE_GC != 0 */
  705. default:
  706. {
  707. set_error_buf(error_buf, error_buf_size,
  708. "illegal opcode "
  709. "or constant expression required "
  710. "or type mismatch");
  711. goto fail;
  712. }
  713. }
  714. CHECK_BUF(p, p_end, 1);
  715. end_byte = read_uint8(p);
  716. if (end_byte != 0x0b)
  717. goto fail_type_mismatch;
  718. *p_buf = p;
  719. return true;
  720. fail_type_mismatch:
  721. set_error_buf(error_buf, error_buf_size,
  722. "type mismatch or constant expression required");
  723. fail:
  724. return false;
  725. }
  726. static bool
  727. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  728. {
  729. if (mutable >= 2) {
  730. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  731. return false;
  732. }
  733. return true;
  734. }
  735. #if WASM_ENABLE_GC != 0
  736. static void
  737. destroy_func_type(WASMFuncType *type)
  738. {
  739. /* Destroy the reference type hash set */
  740. if (type->ref_type_maps)
  741. wasm_runtime_free(type->ref_type_maps);
  742. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  743. && WASM_ENABLE_LAZY_JIT != 0
  744. if (type->call_to_llvm_jit_from_fast_jit)
  745. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  746. #endif
  747. /* Free the type */
  748. wasm_runtime_free(type);
  749. }
  750. static void
  751. destroy_struct_type(WASMStructType *type)
  752. {
  753. if (type->ref_type_maps)
  754. wasm_runtime_free(type->ref_type_maps);
  755. wasm_runtime_free(type);
  756. }
  757. static void
  758. destroy_array_type(WASMArrayType *type)
  759. {
  760. wasm_runtime_free(type);
  761. }
  762. static void
  763. destroy_wasm_type(WASMType *type)
  764. {
  765. if (type->type_flag == WASM_TYPE_FUNC)
  766. destroy_func_type((WASMFuncType *)type);
  767. else if (type->type_flag == WASM_TYPE_STRUCT)
  768. destroy_struct_type((WASMStructType *)type);
  769. else if (type->type_flag == WASM_TYPE_ARRAY)
  770. destroy_array_type((WASMArrayType *)type);
  771. else {
  772. bh_assert(0);
  773. }
  774. }
  775. /* Resolve (ref null ht) or (ref ht) */
  776. static bool
  777. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  778. WASMModule *module, bool nullable, WASMRefType *ref_type,
  779. char *error_buf, uint32 error_buf_size)
  780. {
  781. const uint8 *p = *p_buf, *p_end = buf_end;
  782. ref_type->ref_type =
  783. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  784. ref_type->ref_ht_common.nullable = nullable;
  785. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  786. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  787. /* heap type is (type i), i : typeidx, >= 0 */
  788. if (!check_type_index(module, ref_type->ref_ht_typeidx.type_idx,
  789. error_buf, error_buf_size)) {
  790. return false;
  791. }
  792. }
  793. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  794. /* heap type is func, extern, any, eq, i31 or data */
  795. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  796. return false;
  797. }
  798. *p_buf = p;
  799. return true;
  800. fail:
  801. return false;
  802. }
  803. static bool
  804. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  805. WASMModule *module, bool *p_need_ref_type_map,
  806. WASMRefType *ref_type, bool allow_packed_type,
  807. char *error_buf, uint32 error_buf_size)
  808. {
  809. const uint8 *p = *p_buf, *p_end = buf_end;
  810. uint8 type;
  811. memset(ref_type, 0, sizeof(WASMRefType));
  812. CHECK_BUF(p, p_end, 1);
  813. type = read_uint8(p);
  814. if (wasm_is_reftype_htref_nullable(type)) {
  815. /* (ref null ht) */
  816. if (!resolve_reftype_htref(&p, p_end, module, true, ref_type, error_buf,
  817. error_buf_size))
  818. return false;
  819. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  820. *p_need_ref_type_map = true;
  821. else {
  822. /* For (ref null func/extern/any/eq/i31/data), they are same as
  823. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  824. multi-byte type to one-byte type to reduce the footprint and
  825. the complexity of type equal/subtype checking */
  826. ref_type->ref_type =
  827. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  828. *p_need_ref_type_map = false;
  829. }
  830. }
  831. else if (wasm_is_reftype_htref_non_nullable(type)) {
  832. /* (ref ht) */
  833. if (!resolve_reftype_htref(&p, p_end, module, false, ref_type,
  834. error_buf, error_buf_size))
  835. return false;
  836. *p_need_ref_type_map = true;
  837. }
  838. else {
  839. /* type which can be represented by one byte */
  840. if (!is_value_type(type)
  841. && !(allow_packed_type && is_packed_type(type))) {
  842. set_error_buf(error_buf, error_buf_size, "type mismatch");
  843. return false;
  844. }
  845. ref_type->ref_type = type;
  846. *p_need_ref_type_map = false;
  847. }
  848. *p_buf = p;
  849. return true;
  850. fail:
  851. return false;
  852. }
  853. static WASMRefType *
  854. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  855. char *error_buf, uint32 error_buf_size)
  856. {
  857. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  858. if (!ret) {
  859. set_error_buf(error_buf, error_buf_size,
  860. "insert ref type to hash set failed");
  861. }
  862. return ret;
  863. }
  864. static bool
  865. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  866. uint32 type_idx, char *error_buf, uint32 error_buf_size)
  867. {
  868. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  869. uint32 param_count, result_count, i, j = 0;
  870. uint32 param_cell_num, ret_cell_num;
  871. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  872. uint64 total_size;
  873. bool need_ref_type_map;
  874. WASMRefType ref_type;
  875. WASMFuncType *type = NULL;
  876. /* Parse first time to resolve param count, result count and
  877. ref type map count */
  878. read_leb_uint32(p, p_end, param_count);
  879. p_org = p;
  880. for (i = 0; i < param_count; i++) {
  881. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  882. &ref_type, false, error_buf, error_buf_size)) {
  883. return false;
  884. }
  885. if (need_ref_type_map)
  886. ref_type_map_count++;
  887. }
  888. read_leb_uint32(p, p_end, result_count);
  889. for (i = 0; i < result_count; i++) {
  890. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  891. &ref_type, false, error_buf, error_buf_size)) {
  892. return false;
  893. }
  894. if (need_ref_type_map) {
  895. ref_type_map_count++;
  896. result_ref_type_map_count++;
  897. }
  898. }
  899. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  900. "ref type map count: %d\n",
  901. type_idx, param_count, result_count, ref_type_map_count);
  902. /* Parse second time to resolve param types, result types and
  903. ref type map info */
  904. p = p_org;
  905. total_size = offsetof(WASMFuncType, types)
  906. + sizeof(uint8) * (uint64)(param_count + result_count);
  907. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  908. return false;
  909. }
  910. if (ref_type_map_count > 0) {
  911. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  912. if (!(type->ref_type_maps =
  913. loader_malloc(total_size, error_buf, error_buf_size))) {
  914. goto fail;
  915. }
  916. }
  917. type->base_type.type_flag = WASM_TYPE_FUNC;
  918. type->param_count = param_count;
  919. type->result_count = result_count;
  920. type->ref_type_map_count = ref_type_map_count;
  921. type->result_ref_type_maps =
  922. type->ref_type_maps + ref_type_map_count - result_ref_type_map_count;
  923. for (i = 0; i < param_count; i++) {
  924. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  925. &ref_type, false, error_buf, error_buf_size)) {
  926. goto fail;
  927. }
  928. type->types[i] = ref_type.ref_type;
  929. if (need_ref_type_map) {
  930. type->ref_type_maps[j].index = i;
  931. if (!(type->ref_type_maps[j++].ref_type =
  932. reftype_set_insert(module->ref_type_set, &ref_type,
  933. error_buf, error_buf_size))) {
  934. goto fail;
  935. }
  936. }
  937. }
  938. read_leb_uint32(p, p_end, result_count);
  939. for (i = 0; i < result_count; i++) {
  940. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  941. &ref_type, false, error_buf, error_buf_size)) {
  942. goto fail;
  943. }
  944. type->types[param_count + i] = ref_type.ref_type;
  945. if (need_ref_type_map) {
  946. type->ref_type_maps[j].index = param_count + i;
  947. if (!(type->ref_type_maps[j++].ref_type =
  948. reftype_set_insert(module->ref_type_set, &ref_type,
  949. error_buf, error_buf_size))) {
  950. goto fail;
  951. }
  952. }
  953. }
  954. bh_assert(j == type->ref_type_map_count);
  955. #if TRACE_WASM_LOADER != 0
  956. os_printf("type %d = ", type_idx);
  957. wasm_dump_func_type(type);
  958. #endif
  959. param_cell_num = wasm_get_cell_num(type->types, param_count);
  960. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  961. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  962. set_error_buf(error_buf, error_buf_size,
  963. "param count or result count too large");
  964. goto fail;
  965. }
  966. type->param_cell_num = (uint16)param_cell_num;
  967. type->ret_cell_num = (uint16)ret_cell_num;
  968. /* Calculate the minimal type index of the type equal to this type */
  969. type->min_type_idx_normalized = type_idx;
  970. for (i = 0; i < type_idx; i++) {
  971. WASMFuncType *func_type = (WASMFuncType *)module->types[i];
  972. if (func_type->base_type.type_flag == WASM_TYPE_FUNC
  973. && wasm_func_type_equal(type, func_type, module->types,
  974. type_idx + 1)) {
  975. type->min_type_idx_normalized = i;
  976. break;
  977. }
  978. }
  979. *p_buf = p;
  980. module->types[type_idx] = (WASMType *)type;
  981. return true;
  982. fail:
  983. if (type)
  984. destroy_func_type(type);
  985. return false;
  986. }
  987. static bool
  988. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  989. WASMModule *module, uint32 type_idx, char *error_buf,
  990. uint32 error_buf_size)
  991. {
  992. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  993. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  994. uint32 i, j = 0, offset;
  995. uint16 *reference_table;
  996. uint64 total_size;
  997. uint8 mutable;
  998. bool need_ref_type_map;
  999. WASMRefType ref_type;
  1000. WASMStructType *type = NULL;
  1001. /* Parse first time to resolve field count and ref type map count */
  1002. read_leb_uint32(p, p_end, field_count);
  1003. p_org = p;
  1004. for (i = 0; i < field_count; i++) {
  1005. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1006. &ref_type, true, error_buf, error_buf_size)) {
  1007. return false;
  1008. }
  1009. if (need_ref_type_map)
  1010. ref_type_map_count++;
  1011. if (wasm_is_type_reftype(ref_type.ref_type))
  1012. ref_field_count++;
  1013. CHECK_BUF(p, p_end, 1);
  1014. mutable = read_uint8(p);
  1015. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1016. return false;
  1017. }
  1018. }
  1019. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d\n",
  1020. type_idx, field_count, ref_type_map_count);
  1021. /* Parse second time to resolve field types and ref type map info */
  1022. p = p_org;
  1023. total_size = offsetof(WASMStructType, fields)
  1024. + sizeof(WASMStructFieldType) * (uint64)field_count
  1025. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1026. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1027. return false;
  1028. }
  1029. if (ref_type_map_count > 0) {
  1030. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1031. if (!(type->ref_type_maps =
  1032. loader_malloc(total_size, error_buf, error_buf_size))) {
  1033. goto fail;
  1034. }
  1035. }
  1036. type->reference_table = reference_table =
  1037. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1038. + sizeof(WASMStructFieldType) * field_count);
  1039. *reference_table++ = ref_field_count;
  1040. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1041. type->field_count = field_count;
  1042. type->ref_type_map_count = ref_type_map_count;
  1043. offset = (uint32)sizeof(WASMStructObject);
  1044. for (i = 0; i < field_count; i++) {
  1045. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1046. &ref_type, true, error_buf, error_buf_size)) {
  1047. goto fail;
  1048. }
  1049. type->fields[i].field_type = ref_type.ref_type;
  1050. if (need_ref_type_map) {
  1051. type->ref_type_maps[j].index = i;
  1052. if (!(type->ref_type_maps[j++].ref_type =
  1053. reftype_set_insert(module->ref_type_set, &ref_type,
  1054. error_buf, error_buf_size))) {
  1055. goto fail;
  1056. }
  1057. }
  1058. type->fields[i].field_flags = read_uint8(p);
  1059. type->fields[i].field_size =
  1060. (uint8)wasm_reftype_size(ref_type.ref_type);
  1061. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1062. || defined(BUILD_TARGET_X86_32))
  1063. if (type->fields[i].field_size == 2)
  1064. offset = align_uint(offset, 2);
  1065. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1066. offset = align_uint(offset, 4);
  1067. #endif
  1068. type->fields[i].field_offset = offset;
  1069. if (wasm_is_type_reftype(ref_type.ref_type))
  1070. *reference_table++ = offset;
  1071. offset += type->fields[i].field_size;
  1072. }
  1073. type->total_size = offset;
  1074. bh_assert(j == type->ref_type_map_count);
  1075. #if TRACE_WASM_LOADER != 0
  1076. os_printf("type %d = ", type_idx);
  1077. wasm_dump_struct_type(type);
  1078. #endif
  1079. *p_buf = p;
  1080. module->types[type_idx] = (WASMType *)type;
  1081. return true;
  1082. fail:
  1083. if (type)
  1084. destroy_struct_type(type);
  1085. return false;
  1086. }
  1087. static bool
  1088. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1089. WASMModule *module, uint32 type_idx, char *error_buf,
  1090. uint32 error_buf_size)
  1091. {
  1092. const uint8 *p = *p_buf, *p_end = buf_end;
  1093. uint8 mutable;
  1094. bool need_ref_type_map;
  1095. WASMRefType ref_type;
  1096. WASMArrayType *type = NULL;
  1097. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map, &ref_type,
  1098. true, error_buf, error_buf_size)) {
  1099. return false;
  1100. }
  1101. CHECK_BUF(p, p_end, 1);
  1102. mutable = read_uint8(p);
  1103. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1104. return false;
  1105. }
  1106. LOG_VERBOSE("type %u: array\n", type_idx);
  1107. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1108. error_buf_size))) {
  1109. return false;
  1110. }
  1111. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1112. type->elem_flags = mutable;
  1113. type->elem_type = ref_type.ref_type;
  1114. if (need_ref_type_map) {
  1115. if (!(type->elem_ref_type =
  1116. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1117. error_buf_size))) {
  1118. goto fail;
  1119. }
  1120. }
  1121. #if TRACE_WASM_LOADER != 0
  1122. os_printf("type %d = ", type_idx);
  1123. wasm_dump_array_type(type);
  1124. #endif
  1125. *p_buf = p;
  1126. module->types[type_idx] = (WASMType *)type;
  1127. return true;
  1128. fail:
  1129. if (type)
  1130. destroy_array_type(type);
  1131. return false;
  1132. }
  1133. #else /* else of WASM_ENABLE_GC != 0 */
  1134. static void
  1135. destroy_wasm_type(WASMType *type)
  1136. {
  1137. if (type->ref_count > 1) {
  1138. /* The type is referenced by other types
  1139. of current wasm module */
  1140. type->ref_count--;
  1141. return;
  1142. }
  1143. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1144. && WASM_ENABLE_LAZY_JIT != 0
  1145. if (type->call_to_llvm_jit_from_fast_jit)
  1146. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1147. #endif
  1148. wasm_runtime_free(type);
  1149. }
  1150. #endif /* end of WASM_ENABLE_GC != 0 */
  1151. static bool
  1152. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1153. char *error_buf, uint32 error_buf_size)
  1154. {
  1155. const uint8 *p = buf, *p_end = buf_end;
  1156. uint32 type_count, i;
  1157. uint64 total_size;
  1158. uint8 flag;
  1159. read_leb_uint32(p, p_end, type_count);
  1160. if (type_count) {
  1161. module->type_count = type_count;
  1162. total_size = sizeof(WASMFuncType *) * (uint64)type_count;
  1163. if (!(module->types =
  1164. loader_malloc(total_size, error_buf, error_buf_size))) {
  1165. return false;
  1166. }
  1167. #if WASM_ENABLE_GC == 0
  1168. for (i = 0; i < type_count; i++) {
  1169. WASMFuncType *type;
  1170. const uint8 *p_org;
  1171. uint32 param_count, result_count, j;
  1172. uint32 param_cell_num, ret_cell_num;
  1173. CHECK_BUF(p, p_end, 1);
  1174. flag = read_uint8(p);
  1175. if (flag != 0x60) {
  1176. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1177. return false;
  1178. }
  1179. read_leb_uint32(p, p_end, param_count);
  1180. /* Resolve param count and result count firstly */
  1181. p_org = p;
  1182. CHECK_BUF(p, p_end, param_count);
  1183. p += param_count;
  1184. read_leb_uint32(p, p_end, result_count);
  1185. CHECK_BUF(p, p_end, result_count);
  1186. p = p_org;
  1187. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1188. set_error_buf(error_buf, error_buf_size,
  1189. "param count or result count too large");
  1190. return false;
  1191. }
  1192. total_size = offsetof(WASMFuncType, types)
  1193. + sizeof(uint8) * (uint64)(param_count + result_count);
  1194. if (!(type = module->types[i] =
  1195. loader_malloc(total_size, error_buf, error_buf_size))) {
  1196. return false;
  1197. }
  1198. /* Resolve param types and result types */
  1199. type->ref_count = 1;
  1200. type->param_count = (uint16)param_count;
  1201. type->result_count = (uint16)result_count;
  1202. for (j = 0; j < param_count; j++) {
  1203. CHECK_BUF(p, p_end, 1);
  1204. type->types[j] = read_uint8(p);
  1205. }
  1206. read_leb_uint32(p, p_end, result_count);
  1207. for (j = 0; j < result_count; j++) {
  1208. CHECK_BUF(p, p_end, 1);
  1209. type->types[param_count + j] = read_uint8(p);
  1210. }
  1211. for (j = 0; j < param_count + result_count; j++) {
  1212. if (!is_value_type(type->types[j])) {
  1213. set_error_buf(error_buf, error_buf_size,
  1214. "unknown value type");
  1215. return false;
  1216. }
  1217. }
  1218. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1219. ret_cell_num =
  1220. wasm_get_cell_num(type->types + param_count, result_count);
  1221. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1222. set_error_buf(error_buf, error_buf_size,
  1223. "param count or result count too large");
  1224. return false;
  1225. }
  1226. type->param_cell_num = (uint16)param_cell_num;
  1227. type->ret_cell_num = (uint16)ret_cell_num;
  1228. /* If there is already a same type created, use it instead */
  1229. for (j = 0; j < i; j++) {
  1230. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1231. if (module->types[j]->ref_count == UINT16_MAX) {
  1232. set_error_buf(error_buf, error_buf_size,
  1233. "wasm type's ref count too large");
  1234. return false;
  1235. }
  1236. destroy_wasm_type(type);
  1237. module->types[i] = module->types[j];
  1238. module->types[j]->ref_count++;
  1239. break;
  1240. }
  1241. }
  1242. }
  1243. #else /* else of WASM_ENABLE_GC == 0 */
  1244. for (i = 0; i < type_count; i++) {
  1245. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1246. bool is_sub_final = true;
  1247. CHECK_BUF(p, p_end, 1);
  1248. flag = read_uint8(p);
  1249. if (flag == DEFINED_TYPE_SUB || flag == DEFINED_TYPE_SUB_FINAL) {
  1250. read_leb_uint32(p, p_end, super_type_count);
  1251. if (super_type_count > 1) {
  1252. set_error_buf(error_buf, error_buf_size,
  1253. "super type count too large");
  1254. return false;
  1255. }
  1256. if (super_type_count > 0) {
  1257. read_leb_uint32(p, p_end, parent_type_idx);
  1258. if (parent_type_idx >= i) {
  1259. set_error_buf_v(error_buf, error_buf_size,
  1260. "unknown type %d", parent_type_idx);
  1261. return false;
  1262. }
  1263. if (module->types[parent_type_idx]->is_sub_final) {
  1264. set_error_buf(error_buf, error_buf_size,
  1265. "sub type can not inherit from "
  1266. "a final super type");
  1267. return false;
  1268. }
  1269. }
  1270. if (flag == DEFINED_TYPE_SUB)
  1271. is_sub_final = false;
  1272. CHECK_BUF(p, p_end, 1);
  1273. flag = read_uint8(p);
  1274. }
  1275. #if WASM_ENABLE_GC_BINARYEN != 0
  1276. else {
  1277. is_sub_final = false;
  1278. }
  1279. #endif
  1280. if (flag == DEFINED_TYPE_FUNC) {
  1281. if (!resolve_func_type(&p, buf_end, module, i, error_buf,
  1282. error_buf_size)) {
  1283. return false;
  1284. }
  1285. }
  1286. else if (flag == DEFINED_TYPE_STRUCT) {
  1287. if (!resolve_struct_type(&p, buf_end, module, i, error_buf,
  1288. error_buf_size)) {
  1289. return false;
  1290. }
  1291. }
  1292. else if (flag == DEFINED_TYPE_ARRAY) {
  1293. if (!resolve_array_type(&p, buf_end, module, i, error_buf,
  1294. error_buf_size)) {
  1295. return false;
  1296. }
  1297. }
  1298. else {
  1299. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1300. return false;
  1301. }
  1302. if (parent_type_idx != (uint32)-1) { /* has parent */
  1303. WASMType *parent_type = module->types[parent_type_idx];
  1304. if (!wasm_type_is_subtype_of(module->types[i], parent_type,
  1305. module->types, i)) {
  1306. set_error_buf(error_buf, error_buf_size,
  1307. "sub type does not match super type");
  1308. return false;
  1309. }
  1310. module->types[i]->parent_type = parent_type;
  1311. module->types[i]->root_type = parent_type->root_type;
  1312. module->types[i]->inherit_depth =
  1313. parent_type->inherit_depth + 1;
  1314. }
  1315. else {
  1316. module->types[i]->parent_type = NULL;
  1317. module->types[i]->root_type = module->types[i];
  1318. module->types[i]->inherit_depth = 0;
  1319. }
  1320. module->types[i]->parent_type_idx = parent_type_idx;
  1321. module->types[i]->is_sub_final = is_sub_final;
  1322. }
  1323. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1324. * module->type_count,
  1325. error_buf, error_buf_size))) {
  1326. return false;
  1327. }
  1328. #endif /* end of WASM_ENABLE_GC == 0 */
  1329. }
  1330. if (p != p_end) {
  1331. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1332. return false;
  1333. }
  1334. LOG_VERBOSE("Load type section success.\n");
  1335. return true;
  1336. fail:
  1337. return false;
  1338. }
  1339. static void
  1340. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1341. {
  1342. uint32 default_max_size =
  1343. init_size * 2 > TABLE_MAX_SIZE ? init_size * 2 : TABLE_MAX_SIZE;
  1344. if (max_size_flag) {
  1345. /* module defines the table limitation */
  1346. bh_assert(init_size <= *max_size);
  1347. if (init_size < *max_size) {
  1348. *max_size =
  1349. *max_size < default_max_size ? *max_size : default_max_size;
  1350. }
  1351. }
  1352. else {
  1353. /* partial defined table limitation, gives a default value */
  1354. *max_size = default_max_size;
  1355. }
  1356. }
  1357. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  1358. /**
  1359. * Find export item of a module with export info:
  1360. * module name, field name and export kind
  1361. */
  1362. static WASMExport *
  1363. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  1364. const char *field_name, uint8 export_kind,
  1365. char *error_buf, uint32 error_buf_size)
  1366. {
  1367. WASMExport *export =
  1368. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  1369. export_kind, error_buf, error_buf_size);
  1370. return export;
  1371. }
  1372. #endif
  1373. #if WASM_ENABLE_MULTI_MODULE != 0
  1374. static WASMFunction *
  1375. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  1376. const WASMFuncType *expected_function_type,
  1377. char *error_buf, uint32 error_buf_size)
  1378. {
  1379. WASMModuleCommon *module_reg;
  1380. WASMFunction *function = NULL;
  1381. WASMExport *export = NULL;
  1382. WASMModule *module = NULL;
  1383. WASMFuncType *target_function_type = NULL;
  1384. module_reg = wasm_runtime_find_module_registered(module_name);
  1385. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1386. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  1387. function_name);
  1388. set_error_buf(error_buf, error_buf_size, "unknown import");
  1389. return NULL;
  1390. }
  1391. module = (WASMModule *)module_reg;
  1392. export =
  1393. wasm_loader_find_export(module, module_name, function_name,
  1394. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  1395. if (!export) {
  1396. return NULL;
  1397. }
  1398. /* resolve function type and function */
  1399. if (export->index < module->import_function_count) {
  1400. target_function_type =
  1401. module->import_functions[export->index].u.function.func_type;
  1402. function = module->import_functions[export->index]
  1403. .u.function.import_func_linked;
  1404. }
  1405. else {
  1406. target_function_type =
  1407. module->functions[export->index - module->import_function_count]
  1408. ->func_type;
  1409. function =
  1410. module->functions[export->index - module->import_function_count];
  1411. }
  1412. /* check function type */
  1413. if (!wasm_type_equal((WASMType *)expected_function_type,
  1414. (WASMType *)target_function_type, module->types,
  1415. module->type_count)) {
  1416. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  1417. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1418. return NULL;
  1419. }
  1420. return function;
  1421. }
  1422. static WASMTable *
  1423. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  1424. uint32 init_size, uint32 max_size, char *error_buf,
  1425. uint32 error_buf_size)
  1426. {
  1427. WASMModuleCommon *module_reg;
  1428. WASMTable *table = NULL;
  1429. WASMExport *export = NULL;
  1430. WASMModule *module = NULL;
  1431. module_reg = wasm_runtime_find_module_registered(module_name);
  1432. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1433. LOG_DEBUG("can not find a module named %s for table", module_name);
  1434. set_error_buf(error_buf, error_buf_size, "unknown import");
  1435. return NULL;
  1436. }
  1437. module = (WASMModule *)module_reg;
  1438. export =
  1439. wasm_loader_find_export(module, module_name, table_name,
  1440. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  1441. if (!export) {
  1442. return NULL;
  1443. }
  1444. /* resolve table and check the init/max size */
  1445. if (export->index < module->import_table_count) {
  1446. table =
  1447. module->import_tables[export->index].u.table.import_table_linked;
  1448. }
  1449. else {
  1450. table = &(module->tables[export->index - module->import_table_count]);
  1451. }
  1452. if (table->init_size < init_size || table->max_size > max_size) {
  1453. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  1454. module_name, table_name, table->init_size, table->max_size,
  1455. init_size, max_size);
  1456. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1457. return NULL;
  1458. }
  1459. return table;
  1460. }
  1461. static WASMMemory *
  1462. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  1463. uint32 init_page_count, uint32 max_page_count,
  1464. char *error_buf, uint32 error_buf_size)
  1465. {
  1466. WASMModuleCommon *module_reg;
  1467. WASMMemory *memory = NULL;
  1468. WASMExport *export = NULL;
  1469. WASMModule *module = NULL;
  1470. module_reg = wasm_runtime_find_module_registered(module_name);
  1471. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1472. LOG_DEBUG("can not find a module named %s for memory", module_name);
  1473. set_error_buf(error_buf, error_buf_size, "unknown import");
  1474. return NULL;
  1475. }
  1476. module = (WASMModule *)module_reg;
  1477. export =
  1478. wasm_loader_find_export(module, module_name, memory_name,
  1479. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  1480. if (!export) {
  1481. return NULL;
  1482. }
  1483. /* resolve memory and check the init/max page count */
  1484. if (export->index < module->import_memory_count) {
  1485. memory = module->import_memories[export->index]
  1486. .u.memory.import_memory_linked;
  1487. }
  1488. else {
  1489. memory =
  1490. &(module->memories[export->index - module->import_memory_count]);
  1491. }
  1492. if (memory->init_page_count < init_page_count
  1493. || memory->max_page_count > max_page_count) {
  1494. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  1495. module_name, memory_name, memory->init_page_count,
  1496. memory->max_page_count, init_page_count, max_page_count);
  1497. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1498. return NULL;
  1499. }
  1500. return memory;
  1501. }
  1502. static WASMGlobal *
  1503. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  1504. uint8 type, bool is_mutable, char *error_buf,
  1505. uint32 error_buf_size)
  1506. {
  1507. WASMModuleCommon *module_reg;
  1508. WASMGlobal *global = NULL;
  1509. WASMExport *export = NULL;
  1510. WASMModule *module = NULL;
  1511. module_reg = wasm_runtime_find_module_registered(module_name);
  1512. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1513. LOG_DEBUG("can not find a module named %s for global", module_name);
  1514. set_error_buf(error_buf, error_buf_size, "unknown import");
  1515. return NULL;
  1516. }
  1517. module = (WASMModule *)module_reg;
  1518. export =
  1519. wasm_loader_find_export(module, module_name, global_name,
  1520. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  1521. if (!export) {
  1522. return NULL;
  1523. }
  1524. /* resolve and check the global */
  1525. if (export->index < module->import_global_count) {
  1526. global =
  1527. module->import_globals[export->index].u.global.import_global_linked;
  1528. }
  1529. else {
  1530. global =
  1531. &(module->globals[export->index - module->import_global_count]);
  1532. }
  1533. if (global->type != type || global->is_mutable != is_mutable) {
  1534. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  1535. module_name, global_name, global->type, global->is_mutable,
  1536. type, is_mutable);
  1537. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1538. return NULL;
  1539. }
  1540. return global;
  1541. }
  1542. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  1543. static bool
  1544. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  1545. const WASMModule *parent_module,
  1546. const char *sub_module_name, const char *function_name,
  1547. WASMFunctionImport *function, char *error_buf,
  1548. uint32 error_buf_size)
  1549. {
  1550. const uint8 *p = *p_buf, *p_end = buf_end;
  1551. uint32 declare_type_index = 0;
  1552. WASMFuncType *declare_func_type = NULL;
  1553. WASMFunction *linked_func = NULL;
  1554. #if WASM_ENABLE_MULTI_MODULE != 0
  1555. WASMModule *sub_module = NULL;
  1556. #endif
  1557. const char *linked_signature = NULL;
  1558. void *linked_attachment = NULL;
  1559. bool linked_call_conv_raw = false;
  1560. bool is_native_symbol = false;
  1561. read_leb_uint32(p, p_end, declare_type_index);
  1562. *p_buf = p;
  1563. if (declare_type_index >= parent_module->type_count) {
  1564. set_error_buf(error_buf, error_buf_size, "unknown type");
  1565. return false;
  1566. }
  1567. #if WASM_ENABLE_GC != 0
  1568. function->type_idx = declare_type_index;
  1569. #endif
  1570. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  1571. declare_type_index = wasm_get_smallest_type_idx(
  1572. parent_module->types, parent_module->type_count, declare_type_index);
  1573. #endif
  1574. declare_func_type =
  1575. (WASMFuncType *)parent_module->types[declare_type_index];
  1576. /* lookup registered native symbols first */
  1577. linked_func = wasm_native_resolve_symbol(
  1578. sub_module_name, function_name, declare_func_type, &linked_signature,
  1579. &linked_attachment, &linked_call_conv_raw);
  1580. if (linked_func) {
  1581. is_native_symbol = true;
  1582. }
  1583. #if WASM_ENABLE_MULTI_MODULE != 0
  1584. else {
  1585. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1586. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  1587. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  1588. error_buf_size);
  1589. if (!sub_module) {
  1590. return false;
  1591. }
  1592. }
  1593. linked_func = wasm_loader_resolve_function(
  1594. sub_module_name, function_name, declare_func_type, error_buf,
  1595. error_buf_size);
  1596. }
  1597. #endif
  1598. function->module_name = (char *)sub_module_name;
  1599. function->field_name = (char *)function_name;
  1600. function->func_type = declare_func_type;
  1601. /* func_ptr_linked is for native registered symbol */
  1602. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  1603. function->signature = linked_signature;
  1604. function->attachment = linked_attachment;
  1605. function->call_conv_raw = linked_call_conv_raw;
  1606. #if WASM_ENABLE_MULTI_MODULE != 0
  1607. function->import_module = is_native_symbol ? NULL : sub_module;
  1608. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  1609. #endif
  1610. return true;
  1611. fail:
  1612. return false;
  1613. }
  1614. static bool
  1615. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  1616. uint32 error_buf_size)
  1617. {
  1618. if (max_size < init_size) {
  1619. set_error_buf(error_buf, error_buf_size,
  1620. "size minimum must not be greater than maximum");
  1621. return false;
  1622. }
  1623. return true;
  1624. }
  1625. static bool
  1626. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  1627. WASMModule *parent_module, const char *sub_module_name,
  1628. const char *table_name, WASMTableImport *table,
  1629. char *error_buf, uint32 error_buf_size)
  1630. {
  1631. const uint8 *p = *p_buf, *p_end = buf_end;
  1632. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  1633. declare_init_size = 0, declare_max_size = 0;
  1634. #if WASM_ENABLE_MULTI_MODULE != 0
  1635. WASMModule *sub_module = NULL;
  1636. WASMTable *linked_table = NULL;
  1637. #endif
  1638. #if WASM_ENABLE_GC != 0
  1639. WASMRefType ref_type;
  1640. bool need_ref_type_map;
  1641. #endif
  1642. #if WASM_ENABLE_GC == 0
  1643. CHECK_BUF(p, p_end, 1);
  1644. /* 0x70 or 0x6F */
  1645. declare_elem_type = read_uint8(p);
  1646. if (VALUE_TYPE_FUNCREF != declare_elem_type
  1647. #if WASM_ENABLE_REF_TYPES != 0
  1648. && VALUE_TYPE_EXTERNREF != declare_elem_type
  1649. #endif
  1650. ) {
  1651. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1652. return false;
  1653. }
  1654. #else /* else of WASM_ENABLE_GC == 0 */
  1655. if (!resolve_value_type(&p, p_end, parent_module, &need_ref_type_map,
  1656. &ref_type, false, error_buf, error_buf_size)) {
  1657. return false;
  1658. }
  1659. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  1660. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1661. return false;
  1662. }
  1663. declare_elem_type = ref_type.ref_type;
  1664. if (need_ref_type_map) {
  1665. if (!(table->elem_ref_type =
  1666. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  1667. error_buf, error_buf_size))) {
  1668. return false;
  1669. }
  1670. }
  1671. #if TRACE_WASM_LOADER != 0
  1672. os_printf("import table type: ");
  1673. wasm_dump_value_type(declare_elem_type, table->elem_ref_type);
  1674. os_printf("\n");
  1675. #endif
  1676. #endif /* end of WASM_ENABLE_GC == 0 */
  1677. read_leb_uint32(p, p_end, declare_max_size_flag);
  1678. if (declare_max_size_flag > 1) {
  1679. set_error_buf(error_buf, error_buf_size, "integer too large");
  1680. return false;
  1681. }
  1682. read_leb_uint32(p, p_end, declare_init_size);
  1683. if (declare_max_size_flag) {
  1684. read_leb_uint32(p, p_end, declare_max_size);
  1685. if (!check_table_max_size(declare_init_size, declare_max_size,
  1686. error_buf, error_buf_size))
  1687. return false;
  1688. }
  1689. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  1690. &declare_max_size);
  1691. *p_buf = p;
  1692. #if WASM_ENABLE_MULTI_MODULE != 0
  1693. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1694. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  1695. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  1696. error_buf_size);
  1697. if (!sub_module) {
  1698. return false;
  1699. }
  1700. linked_table = wasm_loader_resolve_table(
  1701. sub_module_name, table_name, declare_init_size, declare_max_size,
  1702. error_buf, error_buf_size);
  1703. if (!linked_table) {
  1704. return false;
  1705. }
  1706. /* reset with linked table limit */
  1707. declare_elem_type = linked_table->elem_type;
  1708. declare_init_size = linked_table->init_size;
  1709. declare_max_size = linked_table->max_size;
  1710. declare_max_size_flag = linked_table->flags;
  1711. table->import_table_linked = linked_table;
  1712. table->import_module = sub_module;
  1713. }
  1714. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  1715. /* (table (export "table") 10 20 funcref) */
  1716. /* we need this section working in wamrc */
  1717. if (!strcmp("spectest", sub_module_name)) {
  1718. const uint32 spectest_table_init_size = 10;
  1719. const uint32 spectest_table_max_size = 20;
  1720. if (strcmp("table", table_name)) {
  1721. set_error_buf(error_buf, error_buf_size,
  1722. "incompatible import type or unknown import");
  1723. return false;
  1724. }
  1725. if (declare_init_size > spectest_table_init_size
  1726. || declare_max_size < spectest_table_max_size) {
  1727. set_error_buf(error_buf, error_buf_size,
  1728. "incompatible import type");
  1729. return false;
  1730. }
  1731. declare_init_size = spectest_table_init_size;
  1732. declare_max_size = spectest_table_max_size;
  1733. }
  1734. /* now we believe all declaration are ok */
  1735. table->elem_type = declare_elem_type;
  1736. table->init_size = declare_init_size;
  1737. table->flags = declare_max_size_flag;
  1738. table->max_size = declare_max_size;
  1739. (void)parent_module;
  1740. return true;
  1741. fail:
  1742. return false;
  1743. }
  1744. static bool
  1745. check_memory_init_size(uint32 init_size, char *error_buf, uint32 error_buf_size)
  1746. {
  1747. if (init_size > DEFAULT_MAX_PAGES) {
  1748. set_error_buf(error_buf, error_buf_size,
  1749. "memory size must be at most 65536 pages (4GiB)");
  1750. return false;
  1751. }
  1752. return true;
  1753. }
  1754. static bool
  1755. check_memory_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  1756. uint32 error_buf_size)
  1757. {
  1758. if (max_size < init_size) {
  1759. set_error_buf(error_buf, error_buf_size,
  1760. "size minimum must not be greater than maximum");
  1761. return false;
  1762. }
  1763. if (max_size > DEFAULT_MAX_PAGES) {
  1764. set_error_buf(error_buf, error_buf_size,
  1765. "memory size must be at most 65536 pages (4GiB)");
  1766. return false;
  1767. }
  1768. return true;
  1769. }
  1770. static bool
  1771. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  1772. WASMModule *parent_module, const char *sub_module_name,
  1773. const char *memory_name, WASMMemoryImport *memory,
  1774. char *error_buf, uint32 error_buf_size)
  1775. {
  1776. const uint8 *p = *p_buf, *p_end = buf_end;
  1777. #if WASM_ENABLE_APP_FRAMEWORK != 0
  1778. uint32 pool_size = wasm_runtime_memory_pool_size();
  1779. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  1780. / DEFAULT_NUM_BYTES_PER_PAGE;
  1781. #else
  1782. uint32 max_page_count = DEFAULT_MAX_PAGES;
  1783. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  1784. uint32 declare_max_page_count_flag = 0;
  1785. uint32 declare_init_page_count = 0;
  1786. uint32 declare_max_page_count = 0;
  1787. #if WASM_ENABLE_MULTI_MODULE != 0
  1788. WASMModule *sub_module = NULL;
  1789. WASMMemory *linked_memory = NULL;
  1790. #endif
  1791. read_leb_uint32(p, p_end, declare_max_page_count_flag);
  1792. read_leb_uint32(p, p_end, declare_init_page_count);
  1793. if (!check_memory_init_size(declare_init_page_count, error_buf,
  1794. error_buf_size)) {
  1795. return false;
  1796. }
  1797. if (declare_max_page_count_flag & 1) {
  1798. read_leb_uint32(p, p_end, declare_max_page_count);
  1799. if (!check_memory_max_size(declare_init_page_count,
  1800. declare_max_page_count, error_buf,
  1801. error_buf_size)) {
  1802. return false;
  1803. }
  1804. if (declare_max_page_count > max_page_count) {
  1805. declare_max_page_count = max_page_count;
  1806. }
  1807. }
  1808. else {
  1809. /* Limit the maximum memory size to max_page_count */
  1810. declare_max_page_count = max_page_count;
  1811. }
  1812. #if WASM_ENABLE_MULTI_MODULE != 0
  1813. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1814. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  1815. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  1816. error_buf_size);
  1817. if (!sub_module) {
  1818. return false;
  1819. }
  1820. linked_memory = wasm_loader_resolve_memory(
  1821. sub_module_name, memory_name, declare_init_page_count,
  1822. declare_max_page_count, error_buf, error_buf_size);
  1823. if (!linked_memory) {
  1824. return false;
  1825. }
  1826. /**
  1827. * reset with linked memory limit
  1828. */
  1829. memory->import_module = sub_module;
  1830. memory->import_memory_linked = linked_memory;
  1831. declare_init_page_count = linked_memory->init_page_count;
  1832. declare_max_page_count = linked_memory->max_page_count;
  1833. }
  1834. #endif
  1835. /* (memory (export "memory") 1 2) */
  1836. if (!strcmp("spectest", sub_module_name)) {
  1837. uint32 spectest_memory_init_page = 1;
  1838. uint32 spectest_memory_max_page = 2;
  1839. if (strcmp("memory", memory_name)) {
  1840. set_error_buf(error_buf, error_buf_size,
  1841. "incompatible import type or unknown import");
  1842. return false;
  1843. }
  1844. if (declare_init_page_count > spectest_memory_init_page
  1845. || declare_max_page_count < spectest_memory_max_page) {
  1846. set_error_buf(error_buf, error_buf_size,
  1847. "incompatible import type");
  1848. return false;
  1849. }
  1850. declare_init_page_count = spectest_memory_init_page;
  1851. declare_max_page_count = spectest_memory_max_page;
  1852. }
  1853. /* now we believe all declaration are ok */
  1854. memory->flags = declare_max_page_count_flag;
  1855. memory->init_page_count = declare_init_page_count;
  1856. memory->max_page_count = declare_max_page_count;
  1857. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  1858. *p_buf = p;
  1859. (void)parent_module;
  1860. return true;
  1861. fail:
  1862. return false;
  1863. }
  1864. static bool
  1865. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  1866. WASMModule *parent_module, char *sub_module_name,
  1867. char *global_name, WASMGlobalImport *global, char *error_buf,
  1868. uint32 error_buf_size)
  1869. {
  1870. const uint8 *p = *p_buf, *p_end = buf_end;
  1871. uint8 declare_type = 0;
  1872. uint8 declare_mutable = 0;
  1873. #if WASM_ENABLE_MULTI_MODULE != 0
  1874. WASMModule *sub_module = NULL;
  1875. WASMGlobal *linked_global = NULL;
  1876. #endif
  1877. #if WASM_ENABLE_GC != 0
  1878. WASMRefType ref_type;
  1879. bool need_ref_type_map;
  1880. #endif
  1881. bool ret = false;
  1882. #if WASM_ENABLE_GC == 0
  1883. CHECK_BUF(p, p_end, 2);
  1884. declare_type = read_uint8(p);
  1885. declare_mutable = read_uint8(p);
  1886. #else
  1887. if (!resolve_value_type(&p, p_end, parent_module, &need_ref_type_map,
  1888. &ref_type, false, error_buf, error_buf_size)) {
  1889. return false;
  1890. }
  1891. declare_type = ref_type.ref_type;
  1892. if (need_ref_type_map) {
  1893. if (!(global->ref_type =
  1894. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  1895. error_buf, error_buf_size))) {
  1896. return false;
  1897. }
  1898. }
  1899. #if TRACE_WASM_LOADER != 0
  1900. os_printf("import global type: ");
  1901. wasm_dump_value_type(declare_type, global->ref_type);
  1902. os_printf("\n");
  1903. #endif
  1904. CHECK_BUF(p, p_end, 1);
  1905. declare_mutable = read_uint8(p);
  1906. #endif /* end of WASM_ENABLE_GC == 0 */
  1907. *p_buf = p;
  1908. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  1909. return false;
  1910. }
  1911. #if WASM_ENABLE_LIBC_BUILTIN != 0
  1912. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  1913. global);
  1914. if (ret) {
  1915. if (global->type != declare_type
  1916. || global->is_mutable != declare_mutable) {
  1917. set_error_buf(error_buf, error_buf_size,
  1918. "incompatible import type");
  1919. return false;
  1920. }
  1921. global->is_linked = true;
  1922. }
  1923. #endif
  1924. #if WASM_ENABLE_MULTI_MODULE != 0
  1925. if (!global->is_linked
  1926. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  1927. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  1928. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  1929. error_buf_size);
  1930. if (!sub_module) {
  1931. return false;
  1932. }
  1933. /* check sub modules */
  1934. linked_global = wasm_loader_resolve_global(
  1935. sub_module_name, global_name, declare_type, declare_mutable,
  1936. error_buf, error_buf_size);
  1937. if (linked_global) {
  1938. global->import_module = sub_module;
  1939. global->import_global_linked = linked_global;
  1940. global->is_linked = true;
  1941. }
  1942. }
  1943. #endif
  1944. global->module_name = sub_module_name;
  1945. global->field_name = global_name;
  1946. global->type = declare_type;
  1947. global->is_mutable = (declare_mutable == 1);
  1948. (void)parent_module;
  1949. (void)ret;
  1950. return true;
  1951. fail:
  1952. return false;
  1953. }
  1954. static bool
  1955. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1956. WASMTable *table, char *error_buf, uint32 error_buf_size)
  1957. {
  1958. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1959. #if WASM_ENABLE_GC != 0
  1960. WASMRefType ref_type;
  1961. bool need_ref_type_map;
  1962. #endif
  1963. #if WASM_ENABLE_GC == 0
  1964. CHECK_BUF(p, p_end, 1);
  1965. /* 0x70 or 0x6F */
  1966. table->elem_type = read_uint8(p);
  1967. if (VALUE_TYPE_FUNCREF != table->elem_type
  1968. #if WASM_ENABLE_REF_TYPES != 0
  1969. && VALUE_TYPE_EXTERNREF != table->elem_type
  1970. #endif
  1971. ) {
  1972. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1973. return false;
  1974. }
  1975. #else /* else of WASM_ENABLE_GC == 0 */
  1976. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map, &ref_type,
  1977. false, error_buf, error_buf_size)) {
  1978. return false;
  1979. }
  1980. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  1981. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1982. return false;
  1983. }
  1984. table->elem_type = ref_type.ref_type;
  1985. if (need_ref_type_map) {
  1986. if (!(table->elem_ref_type =
  1987. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1988. error_buf_size))) {
  1989. return false;
  1990. }
  1991. }
  1992. #if TRACE_WASM_LOADER != 0
  1993. os_printf("table type: ");
  1994. wasm_dump_value_type(table->elem_type, table->elem_ref_type);
  1995. os_printf("\n");
  1996. #endif
  1997. #endif /* end of WASM_ENABLE_GC == 0 */
  1998. p_org = p;
  1999. read_leb_uint32(p, p_end, table->flags);
  2000. #if WASM_ENABLE_SHARED_MEMORY == 0
  2001. if (p - p_org > 1) {
  2002. set_error_buf(error_buf, error_buf_size,
  2003. "integer representation too long");
  2004. return false;
  2005. }
  2006. if (table->flags > 1) {
  2007. set_error_buf(error_buf, error_buf_size, "integer too large");
  2008. return false;
  2009. }
  2010. #else
  2011. if (p - p_org > 1) {
  2012. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2013. return false;
  2014. }
  2015. if (table->flags == 2) {
  2016. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2017. return false;
  2018. }
  2019. if (table->flags > 1) {
  2020. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2021. return false;
  2022. }
  2023. #endif
  2024. read_leb_uint32(p, p_end, table->init_size);
  2025. if (table->flags) {
  2026. read_leb_uint32(p, p_end, table->max_size);
  2027. if (!check_table_max_size(table->init_size, table->max_size, error_buf,
  2028. error_buf_size))
  2029. return false;
  2030. }
  2031. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  2032. *p_buf = p;
  2033. return true;
  2034. fail:
  2035. return false;
  2036. }
  2037. static bool
  2038. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2039. char *error_buf, uint32 error_buf_size)
  2040. {
  2041. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2042. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2043. uint32 pool_size = wasm_runtime_memory_pool_size();
  2044. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2045. / DEFAULT_NUM_BYTES_PER_PAGE;
  2046. #else
  2047. uint32 max_page_count = DEFAULT_MAX_PAGES;
  2048. #endif
  2049. p_org = p;
  2050. read_leb_uint32(p, p_end, memory->flags);
  2051. #if WASM_ENABLE_SHARED_MEMORY == 0
  2052. if (p - p_org > 1) {
  2053. set_error_buf(error_buf, error_buf_size,
  2054. "integer representation too long");
  2055. return false;
  2056. }
  2057. if (memory->flags > 1) {
  2058. set_error_buf(error_buf, error_buf_size, "integer too large");
  2059. return false;
  2060. }
  2061. #else
  2062. if (p - p_org > 1) {
  2063. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2064. return false;
  2065. }
  2066. if (memory->flags > 3) {
  2067. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2068. return false;
  2069. }
  2070. else if (memory->flags == 2) {
  2071. set_error_buf(error_buf, error_buf_size,
  2072. "shared memory must have maximum");
  2073. return false;
  2074. }
  2075. #endif
  2076. read_leb_uint32(p, p_end, memory->init_page_count);
  2077. if (!check_memory_init_size(memory->init_page_count, error_buf,
  2078. error_buf_size))
  2079. return false;
  2080. if (memory->flags & 1) {
  2081. read_leb_uint32(p, p_end, memory->max_page_count);
  2082. if (!check_memory_max_size(memory->init_page_count,
  2083. memory->max_page_count, error_buf,
  2084. error_buf_size))
  2085. return false;
  2086. if (memory->max_page_count > max_page_count)
  2087. memory->max_page_count = max_page_count;
  2088. }
  2089. else {
  2090. /* Limit the maximum memory size to max_page_count */
  2091. memory->max_page_count = max_page_count;
  2092. }
  2093. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2094. *p_buf = p;
  2095. return true;
  2096. fail:
  2097. return false;
  2098. }
  2099. static bool
  2100. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2101. bool is_load_from_file_buf, char *error_buf,
  2102. uint32 error_buf_size)
  2103. {
  2104. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2105. uint32 import_count, name_len, type_index, i, u32, flags;
  2106. uint64 total_size;
  2107. WASMImport *import;
  2108. WASMImport *import_functions = NULL, *import_tables = NULL;
  2109. WASMImport *import_memories = NULL, *import_globals = NULL;
  2110. char *sub_module_name, *field_name;
  2111. uint8 u8, kind, global_type;
  2112. read_leb_uint32(p, p_end, import_count);
  2113. if (import_count) {
  2114. module->import_count = import_count;
  2115. total_size = sizeof(WASMImport) * (uint64)import_count;
  2116. if (!(module->imports =
  2117. loader_malloc(total_size, error_buf, error_buf_size))) {
  2118. return false;
  2119. }
  2120. p_old = p;
  2121. /* Scan firstly to get import count of each type */
  2122. for (i = 0; i < import_count; i++) {
  2123. /* module name */
  2124. read_leb_uint32(p, p_end, name_len);
  2125. CHECK_BUF(p, p_end, name_len);
  2126. p += name_len;
  2127. /* field name */
  2128. read_leb_uint32(p, p_end, name_len);
  2129. CHECK_BUF(p, p_end, name_len);
  2130. p += name_len;
  2131. CHECK_BUF(p, p_end, 1);
  2132. /* 0x00/0x01/0x02/0x03 */
  2133. kind = read_uint8(p);
  2134. switch (kind) {
  2135. case IMPORT_KIND_FUNC: /* import function */
  2136. read_leb_uint32(p, p_end, type_index);
  2137. module->import_function_count++;
  2138. break;
  2139. case IMPORT_KIND_TABLE: /* import table */
  2140. CHECK_BUF(p, p_end, 1);
  2141. /* 0x70 */
  2142. u8 = read_uint8(p);
  2143. read_leb_uint32(p, p_end, flags);
  2144. read_leb_uint32(p, p_end, u32);
  2145. if (flags & 1)
  2146. read_leb_uint32(p, p_end, u32);
  2147. module->import_table_count++;
  2148. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2149. if (module->import_table_count > 1) {
  2150. set_error_buf(error_buf, error_buf_size,
  2151. "multiple tables");
  2152. return false;
  2153. }
  2154. #endif
  2155. break;
  2156. case IMPORT_KIND_MEMORY: /* import memory */
  2157. read_leb_uint32(p, p_end, flags);
  2158. read_leb_uint32(p, p_end, u32);
  2159. if (flags & 1)
  2160. read_leb_uint32(p, p_end, u32);
  2161. module->import_memory_count++;
  2162. if (module->import_memory_count > 1) {
  2163. set_error_buf(error_buf, error_buf_size,
  2164. "multiple memories");
  2165. return false;
  2166. }
  2167. break;
  2168. case IMPORT_KIND_GLOBAL: /* import global */
  2169. #if WASM_ENABLE_GC != 0
  2170. /* valtype */
  2171. CHECK_BUF(p, p_end, 1);
  2172. global_type = read_uint8(p);
  2173. if (wasm_is_type_multi_byte_type(global_type)) {
  2174. int32 heap_type;
  2175. read_leb_int32(p, p_end, heap_type);
  2176. (void)heap_type;
  2177. }
  2178. /* mutability */
  2179. CHECK_BUF(p, p_end, 1);
  2180. p += 1;
  2181. #else
  2182. CHECK_BUF(p, p_end, 2);
  2183. p += 2;
  2184. #endif
  2185. (void)global_type;
  2186. module->import_global_count++;
  2187. break;
  2188. default:
  2189. set_error_buf(error_buf, error_buf_size,
  2190. "invalid import kind");
  2191. return false;
  2192. }
  2193. }
  2194. if (module->import_function_count)
  2195. import_functions = module->import_functions = module->imports;
  2196. if (module->import_table_count)
  2197. import_tables = module->import_tables =
  2198. module->imports + module->import_function_count;
  2199. if (module->import_memory_count)
  2200. import_memories = module->import_memories =
  2201. module->imports + module->import_function_count
  2202. + module->import_table_count;
  2203. if (module->import_global_count)
  2204. import_globals = module->import_globals =
  2205. module->imports + module->import_function_count
  2206. + module->import_table_count + module->import_memory_count;
  2207. p = p_old;
  2208. /* Scan again to resolve the data */
  2209. for (i = 0; i < import_count; i++) {
  2210. /* load module name */
  2211. read_leb_uint32(p, p_end, name_len);
  2212. CHECK_BUF(p, p_end, name_len);
  2213. if (!(sub_module_name = const_str_list_insert(
  2214. p, name_len, module, is_load_from_file_buf, error_buf,
  2215. error_buf_size))) {
  2216. return false;
  2217. }
  2218. p += name_len;
  2219. /* load field name */
  2220. read_leb_uint32(p, p_end, name_len);
  2221. CHECK_BUF(p, p_end, name_len);
  2222. if (!(field_name = const_str_list_insert(
  2223. p, name_len, module, is_load_from_file_buf, error_buf,
  2224. error_buf_size))) {
  2225. return false;
  2226. }
  2227. p += name_len;
  2228. CHECK_BUF(p, p_end, 1);
  2229. /* 0x00/0x01/0x02/0x03 */
  2230. kind = read_uint8(p);
  2231. switch (kind) {
  2232. case IMPORT_KIND_FUNC: /* import function */
  2233. bh_assert(import_functions);
  2234. import = import_functions++;
  2235. if (!load_function_import(
  2236. &p, p_end, module, sub_module_name, field_name,
  2237. &import->u.function, error_buf, error_buf_size)) {
  2238. return false;
  2239. }
  2240. break;
  2241. case IMPORT_KIND_TABLE: /* import table */
  2242. bh_assert(import_tables);
  2243. import = import_tables++;
  2244. if (!load_table_import(&p, p_end, module, sub_module_name,
  2245. field_name, &import->u.table,
  2246. error_buf, error_buf_size)) {
  2247. LOG_DEBUG("can not import such a table (%s,%s)",
  2248. sub_module_name, field_name);
  2249. return false;
  2250. }
  2251. break;
  2252. case IMPORT_KIND_MEMORY: /* import memory */
  2253. bh_assert(import_memories);
  2254. import = import_memories++;
  2255. if (!load_memory_import(&p, p_end, module, sub_module_name,
  2256. field_name, &import->u.memory,
  2257. error_buf, error_buf_size)) {
  2258. return false;
  2259. }
  2260. break;
  2261. case IMPORT_KIND_GLOBAL: /* import global */
  2262. bh_assert(import_globals);
  2263. import = import_globals++;
  2264. if (!load_global_import(&p, p_end, module, sub_module_name,
  2265. field_name, &import->u.global,
  2266. error_buf, error_buf_size)) {
  2267. return false;
  2268. }
  2269. break;
  2270. default:
  2271. set_error_buf(error_buf, error_buf_size,
  2272. "invalid import kind");
  2273. return false;
  2274. }
  2275. import->kind = kind;
  2276. import->u.names.module_name = sub_module_name;
  2277. import->u.names.field_name = field_name;
  2278. }
  2279. #if WASM_ENABLE_LIBC_WASI != 0
  2280. import = module->import_functions;
  2281. for (i = 0; i < module->import_function_count; i++, import++) {
  2282. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  2283. || !strcmp(import->u.names.module_name,
  2284. "wasi_snapshot_preview1")) {
  2285. module->import_wasi_api = true;
  2286. break;
  2287. }
  2288. }
  2289. #endif
  2290. }
  2291. if (p != p_end) {
  2292. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2293. return false;
  2294. }
  2295. LOG_VERBOSE("Load import section success.\n");
  2296. (void)u8;
  2297. (void)u32;
  2298. (void)type_index;
  2299. return true;
  2300. fail:
  2301. return false;
  2302. }
  2303. static bool
  2304. init_function_local_offsets(WASMFunction *func, char *error_buf,
  2305. uint32 error_buf_size)
  2306. {
  2307. WASMFuncType *param_type = func->func_type;
  2308. uint32 param_count = param_type->param_count;
  2309. uint8 *param_types = param_type->types;
  2310. uint32 local_count = func->local_count;
  2311. uint8 *local_types = func->local_types;
  2312. uint32 i, local_offset = 0;
  2313. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  2314. /*
  2315. * Only allocate memory when total_size is not 0,
  2316. * or the return value of malloc(0) might be NULL on some platforms,
  2317. * which causes wasm loader return false.
  2318. */
  2319. if (total_size > 0
  2320. && !(func->local_offsets =
  2321. loader_malloc(total_size, error_buf, error_buf_size))) {
  2322. return false;
  2323. }
  2324. for (i = 0; i < param_count; i++) {
  2325. func->local_offsets[i] = (uint16)local_offset;
  2326. local_offset += wasm_value_type_cell_num(param_types[i]);
  2327. }
  2328. for (i = 0; i < local_count; i++) {
  2329. func->local_offsets[param_count + i] = (uint16)local_offset;
  2330. local_offset += wasm_value_type_cell_num(local_types[i]);
  2331. }
  2332. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  2333. return true;
  2334. }
  2335. static bool
  2336. load_function_section(const uint8 *buf, const uint8 *buf_end,
  2337. const uint8 *buf_code, const uint8 *buf_code_end,
  2338. WASMModule *module, char *error_buf,
  2339. uint32 error_buf_size)
  2340. {
  2341. const uint8 *p = buf, *p_end = buf_end;
  2342. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  2343. uint32 func_count;
  2344. uint64 total_size;
  2345. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  2346. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  2347. uint8 type;
  2348. WASMFunction *func;
  2349. #if WASM_ENABLE_GC != 0
  2350. bool need_ref_type_map;
  2351. WASMRefType ref_type;
  2352. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  2353. #endif
  2354. read_leb_uint32(p, p_end, func_count);
  2355. if (buf_code)
  2356. read_leb_uint32(p_code, buf_code_end, code_count);
  2357. if (func_count != code_count) {
  2358. set_error_buf(error_buf, error_buf_size,
  2359. "function and code section have inconsistent lengths or "
  2360. "unexpected end");
  2361. return false;
  2362. }
  2363. if (func_count) {
  2364. module->function_count = func_count;
  2365. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  2366. if (!(module->functions =
  2367. loader_malloc(total_size, error_buf, error_buf_size))) {
  2368. return false;
  2369. }
  2370. for (i = 0; i < func_count; i++) {
  2371. /* Resolve function type */
  2372. read_leb_uint32(p, p_end, type_index);
  2373. if (type_index >= module->type_count) {
  2374. set_error_buf(error_buf, error_buf_size, "unknown type");
  2375. return false;
  2376. }
  2377. #if WASM_ENABLE_GC != 0
  2378. type_index_org = type_index;
  2379. #endif
  2380. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2381. type_index = wasm_get_smallest_type_idx(
  2382. module->types, module->type_count, type_index);
  2383. #endif
  2384. read_leb_uint32(p_code, buf_code_end, code_size);
  2385. if (code_size == 0 || p_code + code_size > buf_code_end) {
  2386. set_error_buf(error_buf, error_buf_size,
  2387. "invalid function code size");
  2388. return false;
  2389. }
  2390. /* Resolve local set count */
  2391. p_code_end = p_code + code_size;
  2392. local_count = 0;
  2393. read_leb_uint32(p_code, buf_code_end, local_set_count);
  2394. p_code_save = p_code;
  2395. #if WASM_ENABLE_GC != 0
  2396. ref_type_map_count = 0;
  2397. #endif
  2398. /* Calculate total local count */
  2399. for (j = 0; j < local_set_count; j++) {
  2400. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  2401. if (sub_local_count > UINT32_MAX - local_count) {
  2402. set_error_buf(error_buf, error_buf_size, "too many locals");
  2403. return false;
  2404. }
  2405. #if WASM_ENABLE_GC == 0
  2406. CHECK_BUF(p_code, buf_code_end, 1);
  2407. /* 0x7F/0x7E/0x7D/0x7C */
  2408. type = read_uint8(p_code);
  2409. local_count += sub_local_count;
  2410. #else
  2411. if (!resolve_value_type(&p_code, buf_code_end, module,
  2412. &need_ref_type_map, &ref_type, false,
  2413. error_buf, error_buf_size)) {
  2414. return false;
  2415. }
  2416. local_count += sub_local_count;
  2417. if (need_ref_type_map)
  2418. ref_type_map_count += sub_local_count;
  2419. #endif
  2420. }
  2421. /* Alloc memory, layout: function structure + local types */
  2422. code_size = (uint32)(p_code_end - p_code);
  2423. total_size = sizeof(WASMFunction) + (uint64)local_count;
  2424. if (!(func = module->functions[i] =
  2425. loader_malloc(total_size, error_buf, error_buf_size))) {
  2426. return false;
  2427. }
  2428. #if WASM_ENABLE_GC != 0
  2429. if (ref_type_map_count > 0) {
  2430. total_size =
  2431. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  2432. if (!(func->local_ref_type_maps = loader_malloc(
  2433. total_size, error_buf, error_buf_size))) {
  2434. return false;
  2435. }
  2436. func->local_ref_type_map_count = ref_type_map_count;
  2437. }
  2438. #endif
  2439. /* Set function type, local count, code size and code body */
  2440. func->func_type = (WASMFuncType *)module->types[type_index];
  2441. func->local_count = local_count;
  2442. if (local_count > 0)
  2443. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  2444. func->code_size = code_size;
  2445. /*
  2446. * we shall make a copy of code body [p_code, p_code + code_size]
  2447. * when we are worrying about inappropriate releasing behaviour.
  2448. * all code bodies are actually in a buffer which user allocates in
  2449. * his embedding environment and we don't have power on them.
  2450. * it will be like:
  2451. * code_body_cp = malloc(code_size);
  2452. * memcpy(code_body_cp, p_code, code_size);
  2453. * func->code = code_body_cp;
  2454. */
  2455. func->code = (uint8 *)p_code;
  2456. #if WASM_ENABLE_GC != 0
  2457. func->type_idx = type_index_org;
  2458. #endif
  2459. #if WASM_ENABLE_GC != 0
  2460. t = 0;
  2461. #endif
  2462. /* Load each local type */
  2463. p_code = p_code_save;
  2464. local_type_index = 0;
  2465. for (j = 0; j < local_set_count; j++) {
  2466. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  2467. /* Note: sub_local_count is allowed to be 0 */
  2468. if (local_type_index > UINT32_MAX - sub_local_count
  2469. || local_type_index + sub_local_count > local_count) {
  2470. set_error_buf(error_buf, error_buf_size,
  2471. "invalid local count");
  2472. return false;
  2473. }
  2474. #if WASM_ENABLE_GC == 0
  2475. CHECK_BUF(p_code, buf_code_end, 1);
  2476. /* 0x7F/0x7E/0x7D/0x7C */
  2477. type = read_uint8(p_code);
  2478. if (!is_value_type(type)) {
  2479. if (type == VALUE_TYPE_V128)
  2480. set_error_buf(error_buf, error_buf_size,
  2481. "v128 value type requires simd feature");
  2482. else if (type == VALUE_TYPE_FUNCREF
  2483. || type == VALUE_TYPE_EXTERNREF)
  2484. set_error_buf(error_buf, error_buf_size,
  2485. "ref value type requires "
  2486. "reference types feature");
  2487. else
  2488. set_error_buf_v(error_buf, error_buf_size,
  2489. "invalid local type 0x%02X", type);
  2490. return false;
  2491. }
  2492. #else
  2493. if (!resolve_value_type(&p_code, buf_code_end, module,
  2494. &need_ref_type_map, &ref_type, false,
  2495. error_buf, error_buf_size)) {
  2496. return false;
  2497. }
  2498. if (need_ref_type_map) {
  2499. WASMRefType *ref_type_tmp;
  2500. if (!(ref_type_tmp = reftype_set_insert(
  2501. module->ref_type_set, &ref_type, error_buf,
  2502. error_buf_size))) {
  2503. return false;
  2504. }
  2505. for (k = 0; k < sub_local_count; k++) {
  2506. func->local_ref_type_maps[t + k].ref_type =
  2507. ref_type_tmp;
  2508. func->local_ref_type_maps[t + k].index =
  2509. local_type_index + k;
  2510. }
  2511. t += sub_local_count;
  2512. }
  2513. type = ref_type.ref_type;
  2514. #endif
  2515. for (k = 0; k < sub_local_count; k++) {
  2516. func->local_types[local_type_index++] = type;
  2517. }
  2518. }
  2519. bh_assert(local_type_index == func->local_count);
  2520. #if WASM_ENABLE_GC != 0
  2521. bh_assert(t == func->local_ref_type_map_count);
  2522. #if TRACE_WASM_LOADER != 0
  2523. os_printf("func %u, local types: [", i);
  2524. k = 0;
  2525. for (j = 0; j < func->local_count; j++) {
  2526. WASMRefType *ref_type_tmp = NULL;
  2527. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  2528. bh_assert(j == func->local_ref_type_maps[k].index);
  2529. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  2530. }
  2531. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  2532. if (j < func->local_count - 1)
  2533. os_printf(" ");
  2534. }
  2535. os_printf("]\n");
  2536. #endif
  2537. #endif
  2538. func->param_cell_num = func->func_type->param_cell_num;
  2539. func->ret_cell_num = func->func_type->ret_cell_num;
  2540. local_cell_num =
  2541. wasm_get_cell_num(func->local_types, func->local_count);
  2542. if (local_cell_num > UINT16_MAX) {
  2543. set_error_buf(error_buf, error_buf_size,
  2544. "local count too large");
  2545. return false;
  2546. }
  2547. func->local_cell_num = (uint16)local_cell_num;
  2548. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  2549. return false;
  2550. p_code = p_code_end;
  2551. }
  2552. }
  2553. if (p != p_end) {
  2554. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2555. return false;
  2556. }
  2557. LOG_VERBOSE("Load function section success.\n");
  2558. return true;
  2559. fail:
  2560. return false;
  2561. }
  2562. static bool
  2563. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2564. char *error_buf, uint32 error_buf_size)
  2565. {
  2566. const uint8 *p = buf, *p_end = buf_end;
  2567. uint32 table_count, i;
  2568. uint64 total_size;
  2569. WASMTable *table;
  2570. read_leb_uint32(p, p_end, table_count);
  2571. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2572. if (module->import_table_count + table_count > 1) {
  2573. /* a total of one table is allowed */
  2574. set_error_buf(error_buf, error_buf_size, "multiple tables");
  2575. return false;
  2576. }
  2577. #endif
  2578. if (table_count) {
  2579. module->table_count = table_count;
  2580. total_size = sizeof(WASMTable) * (uint64)table_count;
  2581. if (!(module->tables =
  2582. loader_malloc(total_size, error_buf, error_buf_size))) {
  2583. return false;
  2584. }
  2585. /* load each table */
  2586. table = module->tables;
  2587. for (i = 0; i < table_count; i++, table++)
  2588. if (!load_table(&p, p_end, module, table, error_buf,
  2589. error_buf_size))
  2590. return false;
  2591. }
  2592. if (p != p_end) {
  2593. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2594. return false;
  2595. }
  2596. LOG_VERBOSE("Load table section success.\n");
  2597. return true;
  2598. fail:
  2599. return false;
  2600. }
  2601. static bool
  2602. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2603. char *error_buf, uint32 error_buf_size)
  2604. {
  2605. const uint8 *p = buf, *p_end = buf_end;
  2606. uint32 memory_count, i;
  2607. uint64 total_size;
  2608. WASMMemory *memory;
  2609. read_leb_uint32(p, p_end, memory_count);
  2610. /* a total of one memory is allowed */
  2611. if (module->import_memory_count + memory_count > 1) {
  2612. set_error_buf(error_buf, error_buf_size, "multiple memories");
  2613. return false;
  2614. }
  2615. if (memory_count) {
  2616. module->memory_count = memory_count;
  2617. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  2618. if (!(module->memories =
  2619. loader_malloc(total_size, error_buf, error_buf_size))) {
  2620. return false;
  2621. }
  2622. /* load each memory */
  2623. memory = module->memories;
  2624. for (i = 0; i < memory_count; i++, memory++)
  2625. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  2626. return false;
  2627. }
  2628. if (p != p_end) {
  2629. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2630. return false;
  2631. }
  2632. LOG_VERBOSE("Load memory section success.\n");
  2633. return true;
  2634. fail:
  2635. return false;
  2636. }
  2637. static bool
  2638. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2639. char *error_buf, uint32 error_buf_size)
  2640. {
  2641. const uint8 *p = buf, *p_end = buf_end;
  2642. uint32 global_count, i;
  2643. uint64 total_size;
  2644. WASMGlobal *global;
  2645. uint8 mutable;
  2646. #if WASM_ENABLE_GC != 0
  2647. bool need_ref_type_map;
  2648. WASMRefType ref_type;
  2649. #endif
  2650. read_leb_uint32(p, p_end, global_count);
  2651. if (global_count) {
  2652. module->global_count = global_count;
  2653. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  2654. if (!(module->globals =
  2655. loader_malloc(total_size, error_buf, error_buf_size))) {
  2656. return false;
  2657. }
  2658. global = module->globals;
  2659. for (i = 0; i < global_count; i++, global++) {
  2660. #if WASM_ENABLE_GC == 0
  2661. CHECK_BUF(p, p_end, 2);
  2662. global->type = read_uint8(p);
  2663. mutable = read_uint8(p);
  2664. #else
  2665. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  2666. &ref_type, false, error_buf,
  2667. error_buf_size)) {
  2668. return false;
  2669. }
  2670. global->type = ref_type.ref_type;
  2671. CHECK_BUF(p, p_end, 1);
  2672. mutable = read_uint8(p);
  2673. #endif /* end of WASM_ENABLE_GC */
  2674. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  2675. return false;
  2676. }
  2677. global->is_mutable = mutable ? true : false;
  2678. /* initialize expression */
  2679. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  2680. global->type,
  2681. #if WASM_ENABLE_GC == 0
  2682. NULL,
  2683. #else
  2684. &ref_type,
  2685. #endif
  2686. error_buf, error_buf_size))
  2687. return false;
  2688. #if WASM_ENABLE_GC != 0
  2689. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  2690. uint8 global_type;
  2691. WASMRefType *global_ref_type;
  2692. uint32 global_idx = global->init_expr.u.global_index;
  2693. if (global->init_expr.u.global_index
  2694. >= module->import_global_count + i) {
  2695. set_error_buf(error_buf, error_buf_size, "unknown global");
  2696. return false;
  2697. }
  2698. if (global_idx < module->import_global_count) {
  2699. global_type =
  2700. module->import_globals[global_idx].u.global.type;
  2701. global_ref_type =
  2702. module->import_globals[global_idx].u.global.ref_type;
  2703. }
  2704. else {
  2705. global_type = module->globals[global_idx].type;
  2706. global_ref_type = module->globals[global_idx].ref_type;
  2707. }
  2708. if (!wasm_reftype_is_subtype_of(
  2709. global_type, global_ref_type, global->type,
  2710. global->ref_type, module->types, module->type_count)) {
  2711. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2712. return false;
  2713. }
  2714. }
  2715. if (need_ref_type_map) {
  2716. if (!(global->ref_type =
  2717. reftype_set_insert(module->ref_type_set, &ref_type,
  2718. error_buf, error_buf_size))) {
  2719. return false;
  2720. }
  2721. }
  2722. #if TRACE_WASM_LOADER != 0
  2723. os_printf("global type: ");
  2724. wasm_dump_value_type(global->type, global->ref_type);
  2725. os_printf("\n");
  2726. #endif
  2727. #endif
  2728. }
  2729. }
  2730. if (p != p_end) {
  2731. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2732. return false;
  2733. }
  2734. LOG_VERBOSE("Load global section success.\n");
  2735. return true;
  2736. fail:
  2737. return false;
  2738. }
  2739. static bool
  2740. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2741. bool is_load_from_file_buf, char *error_buf,
  2742. uint32 error_buf_size)
  2743. {
  2744. const uint8 *p = buf, *p_end = buf_end;
  2745. uint32 export_count, i, j, index;
  2746. uint64 total_size;
  2747. uint32 str_len;
  2748. WASMExport *export;
  2749. const char *name;
  2750. read_leb_uint32(p, p_end, export_count);
  2751. if (export_count) {
  2752. module->export_count = export_count;
  2753. total_size = sizeof(WASMExport) * (uint64)export_count;
  2754. if (!(module->exports =
  2755. loader_malloc(total_size, error_buf, error_buf_size))) {
  2756. return false;
  2757. }
  2758. export = module->exports;
  2759. for (i = 0; i < export_count; i++, export ++) {
  2760. #if WASM_ENABLE_THREAD_MGR == 0
  2761. if (p == p_end) {
  2762. /* export section with inconsistent count:
  2763. n export declared, but less than n given */
  2764. set_error_buf(error_buf, error_buf_size,
  2765. "length out of bounds");
  2766. return false;
  2767. }
  2768. #endif
  2769. read_leb_uint32(p, p_end, str_len);
  2770. CHECK_BUF(p, p_end, str_len);
  2771. for (j = 0; j < i; j++) {
  2772. name = module->exports[j].name;
  2773. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  2774. set_error_buf(error_buf, error_buf_size,
  2775. "duplicate export name");
  2776. return false;
  2777. }
  2778. }
  2779. if (!(export->name = const_str_list_insert(
  2780. p, str_len, module, is_load_from_file_buf, error_buf,
  2781. error_buf_size))) {
  2782. return false;
  2783. }
  2784. p += str_len;
  2785. CHECK_BUF(p, p_end, 1);
  2786. export->kind = read_uint8(p);
  2787. read_leb_uint32(p, p_end, index);
  2788. export->index = index;
  2789. switch (export->kind) {
  2790. /* function index */
  2791. case EXPORT_KIND_FUNC:
  2792. if (index >= module->function_count
  2793. + module->import_function_count) {
  2794. set_error_buf(error_buf, error_buf_size,
  2795. "unknown function");
  2796. return false;
  2797. }
  2798. #if WASM_ENABLE_SIMD != 0
  2799. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2800. /* TODO: check func type, if it has v128 param or result,
  2801. report error */
  2802. #endif
  2803. #endif
  2804. break;
  2805. /* table index */
  2806. case EXPORT_KIND_TABLE:
  2807. if (index
  2808. >= module->table_count + module->import_table_count) {
  2809. set_error_buf(error_buf, error_buf_size,
  2810. "unknown table");
  2811. return false;
  2812. }
  2813. break;
  2814. /* memory index */
  2815. case EXPORT_KIND_MEMORY:
  2816. if (index
  2817. >= module->memory_count + module->import_memory_count) {
  2818. set_error_buf(error_buf, error_buf_size,
  2819. "unknown memory");
  2820. return false;
  2821. }
  2822. break;
  2823. /* global index */
  2824. case EXPORT_KIND_GLOBAL:
  2825. if (index
  2826. >= module->global_count + module->import_global_count) {
  2827. set_error_buf(error_buf, error_buf_size,
  2828. "unknown global");
  2829. return false;
  2830. }
  2831. break;
  2832. default:
  2833. set_error_buf(error_buf, error_buf_size,
  2834. "invalid export kind");
  2835. return false;
  2836. }
  2837. }
  2838. }
  2839. if (p != p_end) {
  2840. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2841. return false;
  2842. }
  2843. LOG_VERBOSE("Load export section success.\n");
  2844. return true;
  2845. fail:
  2846. return false;
  2847. }
  2848. static bool
  2849. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  2850. uint32 error_buf_size)
  2851. {
  2852. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2853. if (table_index != 0) {
  2854. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  2855. return false;
  2856. }
  2857. #endif
  2858. if (table_index >= module->import_table_count + module->table_count) {
  2859. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  2860. table_index);
  2861. return false;
  2862. }
  2863. return true;
  2864. }
  2865. static bool
  2866. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2867. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  2868. {
  2869. const uint8 *p = *p_buf, *p_end = buf_end;
  2870. uint32 table_index;
  2871. read_leb_uint32(p, p_end, table_index);
  2872. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  2873. return false;
  2874. }
  2875. *p_table_index = table_index;
  2876. *p_buf = p;
  2877. return true;
  2878. fail:
  2879. return false;
  2880. }
  2881. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2882. static bool
  2883. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  2884. uint32 *p_elem_type,
  2885. #if WASM_ENABLE_GC != 0
  2886. WASMRefType **p_elem_ref_type,
  2887. #endif
  2888. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  2889. {
  2890. const uint8 *p = *p_buf, *p_end = buf_end;
  2891. uint8 elem_type;
  2892. #if WASM_ENABLE_GC != 0
  2893. WASMRefType elem_ref_type;
  2894. bool need_ref_type_map;
  2895. #endif
  2896. CHECK_BUF(p, p_end, 1);
  2897. elem_type = read_uint8(p);
  2898. if (elemkind_zero) {
  2899. if (elem_type != 0) {
  2900. set_error_buf(error_buf, error_buf_size,
  2901. "invalid reference type or unknown type");
  2902. return false;
  2903. }
  2904. else {
  2905. *p_elem_type = VALUE_TYPE_FUNCREF;
  2906. *p_buf = p;
  2907. return true;
  2908. }
  2909. }
  2910. #if WASM_ENABLE_GC == 0
  2911. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  2912. set_error_buf(error_buf, error_buf_size,
  2913. "invalid reference type or unknown type");
  2914. return false;
  2915. }
  2916. *p_elem_type = elem_type;
  2917. #else
  2918. p--;
  2919. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  2920. &need_ref_type_map, &elem_ref_type, false,
  2921. error_buf, error_buf_size)) {
  2922. return false;
  2923. }
  2924. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  2925. set_error_buf(error_buf, error_buf_size,
  2926. "invalid reference type or unknown type");
  2927. return false;
  2928. }
  2929. *p_elem_type = elem_ref_type.ref_type;
  2930. if (need_ref_type_map) {
  2931. if (!(*p_elem_ref_type =
  2932. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  2933. error_buf, error_buf_size))) {
  2934. return false;
  2935. }
  2936. }
  2937. #endif
  2938. *p_buf = p;
  2939. return true;
  2940. fail:
  2941. return false;
  2942. }
  2943. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  2944. static bool
  2945. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  2946. WASMModule *module, WASMTableSeg *table_segment,
  2947. bool use_init_expr, char *error_buf, uint32 error_buf_size)
  2948. {
  2949. const uint8 *p = *p_buf, *p_end = buf_end;
  2950. uint32 function_count, function_index = 0, i;
  2951. uint64 total_size;
  2952. read_leb_uint32(p, p_end, function_count);
  2953. table_segment->function_count = function_count;
  2954. total_size = sizeof(uintptr_t) * (uint64)function_count;
  2955. if (total_size > 0
  2956. && !(table_segment->func_indexes = (uintptr_t *)loader_malloc(
  2957. total_size, error_buf, error_buf_size))) {
  2958. return false;
  2959. }
  2960. for (i = 0; i < function_count; i++) {
  2961. InitializerExpression init_expr = { 0 };
  2962. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  2963. if (!use_init_expr) {
  2964. read_leb_uint32(p, p_end, function_index);
  2965. }
  2966. else {
  2967. if (!load_init_expr(module, &p, p_end, &init_expr,
  2968. table_segment->elem_type,
  2969. #if WASM_ENABLE_GC == 0
  2970. NULL,
  2971. #else
  2972. table_segment->elem_ref_type,
  2973. #endif
  2974. error_buf, error_buf_size))
  2975. return false;
  2976. function_index = init_expr.u.ref_index;
  2977. }
  2978. #else
  2979. read_leb_uint32(p, p_end, function_index);
  2980. #endif
  2981. /* since we are using -1 to indicate ref.null */
  2982. if (init_expr.init_expr_type != INIT_EXPR_TYPE_REFNULL_CONST
  2983. && !check_function_index(module, function_index, error_buf,
  2984. error_buf_size)) {
  2985. return false;
  2986. }
  2987. table_segment->func_indexes[i] = function_index;
  2988. }
  2989. *p_buf = p;
  2990. return true;
  2991. fail:
  2992. return false;
  2993. }
  2994. static bool
  2995. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  2996. WASMModule *module, char *error_buf,
  2997. uint32 error_buf_size)
  2998. {
  2999. const uint8 *p = buf, *p_end = buf_end;
  3000. uint32 table_segment_count, i;
  3001. uint64 total_size;
  3002. WASMTableSeg *table_segment;
  3003. read_leb_uint32(p, p_end, table_segment_count);
  3004. if (table_segment_count) {
  3005. module->table_seg_count = table_segment_count;
  3006. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  3007. if (!(module->table_segments =
  3008. loader_malloc(total_size, error_buf, error_buf_size))) {
  3009. return false;
  3010. }
  3011. table_segment = module->table_segments;
  3012. for (i = 0; i < table_segment_count; i++, table_segment++) {
  3013. if (p >= p_end) {
  3014. set_error_buf(error_buf, error_buf_size,
  3015. "invalid value type or "
  3016. "invalid elements segment kind");
  3017. return false;
  3018. }
  3019. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3020. read_leb_uint32(p, p_end, table_segment->mode);
  3021. /* last three bits */
  3022. table_segment->mode = table_segment->mode & 0x07;
  3023. switch (table_segment->mode) {
  3024. /* elemkind/elemtype + active */
  3025. case 0:
  3026. case 4:
  3027. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  3028. table_segment->table_index = 0;
  3029. if (!check_table_index(module, table_segment->table_index,
  3030. error_buf, error_buf_size))
  3031. return false;
  3032. if (!load_init_expr(
  3033. module, &p, p_end, &table_segment->base_offset,
  3034. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  3035. return false;
  3036. if (!load_func_index_vec(&p, p_end, module, table_segment,
  3037. table_segment->mode == 0 ? false
  3038. : true,
  3039. error_buf, error_buf_size))
  3040. return false;
  3041. break;
  3042. /* elemkind + passive/declarative */
  3043. case 1:
  3044. case 3:
  3045. if (!load_elem_type(module, &p, p_end,
  3046. &table_segment->elem_type,
  3047. #if WASM_ENABLE_GC != 0
  3048. &table_segment->elem_ref_type,
  3049. #endif
  3050. true, error_buf, error_buf_size))
  3051. return false;
  3052. if (!load_func_index_vec(&p, p_end, module, table_segment,
  3053. false, error_buf, error_buf_size))
  3054. return false;
  3055. break;
  3056. /* elemkind/elemtype + table_idx + active */
  3057. case 2:
  3058. case 6:
  3059. if (!load_table_index(&p, p_end, module,
  3060. &table_segment->table_index,
  3061. error_buf, error_buf_size))
  3062. return false;
  3063. if (!load_init_expr(
  3064. module, &p, p_end, &table_segment->base_offset,
  3065. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  3066. return false;
  3067. if (!load_elem_type(module, &p, p_end,
  3068. &table_segment->elem_type,
  3069. #if WASM_ENABLE_GC != 0
  3070. &table_segment->elem_ref_type,
  3071. #endif
  3072. table_segment->mode == 2 ? true : false,
  3073. error_buf, error_buf_size))
  3074. return false;
  3075. if (!load_func_index_vec(&p, p_end, module, table_segment,
  3076. table_segment->mode == 2 ? false
  3077. : true,
  3078. error_buf, error_buf_size))
  3079. return false;
  3080. break;
  3081. case 5:
  3082. case 7:
  3083. if (!load_elem_type(module, &p, p_end,
  3084. &table_segment->elem_type,
  3085. #if WASM_ENABLE_GC != 0
  3086. &table_segment->elem_ref_type,
  3087. #endif
  3088. false, error_buf, error_buf_size))
  3089. return false;
  3090. if (!load_func_index_vec(&p, p_end, module, table_segment,
  3091. true, error_buf, error_buf_size))
  3092. return false;
  3093. break;
  3094. default:
  3095. set_error_buf(error_buf, error_buf_size,
  3096. "unknown element segment kind");
  3097. return false;
  3098. }
  3099. #else
  3100. /*
  3101. * like: 00 41 05 0b 04 00 01 00 01
  3102. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  3103. */
  3104. if (!load_table_index(&p, p_end, module,
  3105. &table_segment->table_index, error_buf,
  3106. error_buf_size))
  3107. return false;
  3108. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  3109. VALUE_TYPE_I32, NULL, error_buf,
  3110. error_buf_size))
  3111. return false;
  3112. if (!load_func_index_vec(&p, p_end, module, table_segment, false,
  3113. error_buf, error_buf_size))
  3114. return false;
  3115. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  3116. }
  3117. }
  3118. if (p != p_end) {
  3119. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3120. return false;
  3121. }
  3122. LOG_VERBOSE("Load table segment section success.\n");
  3123. return true;
  3124. fail:
  3125. return false;
  3126. }
  3127. static bool
  3128. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  3129. WASMModule *module, char *error_buf,
  3130. uint32 error_buf_size)
  3131. {
  3132. const uint8 *p = buf, *p_end = buf_end;
  3133. uint32 data_seg_count, i, mem_index, data_seg_len;
  3134. uint64 total_size;
  3135. WASMDataSeg *dataseg;
  3136. InitializerExpression init_expr;
  3137. #if WASM_ENABLE_BULK_MEMORY != 0
  3138. bool is_passive = false;
  3139. uint32 mem_flag;
  3140. #endif
  3141. read_leb_uint32(p, p_end, data_seg_count);
  3142. #if WASM_ENABLE_BULK_MEMORY != 0
  3143. if ((module->data_seg_count1 != 0)
  3144. && (data_seg_count != module->data_seg_count1)) {
  3145. set_error_buf(error_buf, error_buf_size,
  3146. "data count and data section have inconsistent lengths");
  3147. return false;
  3148. }
  3149. #endif
  3150. if (data_seg_count) {
  3151. module->data_seg_count = data_seg_count;
  3152. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  3153. if (!(module->data_segments =
  3154. loader_malloc(total_size, error_buf, error_buf_size))) {
  3155. return false;
  3156. }
  3157. for (i = 0; i < data_seg_count; i++) {
  3158. read_leb_uint32(p, p_end, mem_index);
  3159. #if WASM_ENABLE_BULK_MEMORY != 0
  3160. is_passive = false;
  3161. mem_flag = mem_index & 0x03;
  3162. switch (mem_flag) {
  3163. case 0x01:
  3164. is_passive = true;
  3165. break;
  3166. case 0x00:
  3167. /* no memory index, treat index as 0 */
  3168. mem_index = 0;
  3169. goto check_mem_index;
  3170. case 0x02:
  3171. /* read following memory index */
  3172. read_leb_uint32(p, p_end, mem_index);
  3173. check_mem_index:
  3174. if (mem_index
  3175. >= module->import_memory_count + module->memory_count) {
  3176. set_error_buf_v(error_buf, error_buf_size,
  3177. "unknown memory %d", mem_index);
  3178. return false;
  3179. }
  3180. break;
  3181. case 0x03:
  3182. default:
  3183. set_error_buf(error_buf, error_buf_size, "unknown memory");
  3184. return false;
  3185. break;
  3186. }
  3187. #else
  3188. if (mem_index
  3189. >= module->import_memory_count + module->memory_count) {
  3190. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  3191. mem_index);
  3192. return false;
  3193. }
  3194. #endif /* WASM_ENABLE_BULK_MEMORY */
  3195. #if WASM_ENABLE_BULK_MEMORY != 0
  3196. if (!is_passive)
  3197. #endif
  3198. if (!load_init_expr(module, &p, p_end, &init_expr,
  3199. VALUE_TYPE_I32, NULL, error_buf,
  3200. error_buf_size))
  3201. return false;
  3202. read_leb_uint32(p, p_end, data_seg_len);
  3203. if (!(dataseg = module->data_segments[i] = loader_malloc(
  3204. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  3205. return false;
  3206. }
  3207. #if WASM_ENABLE_BULK_MEMORY != 0
  3208. dataseg->is_passive = is_passive;
  3209. if (!is_passive)
  3210. #endif
  3211. {
  3212. bh_memcpy_s(&dataseg->base_offset,
  3213. sizeof(InitializerExpression), &init_expr,
  3214. sizeof(InitializerExpression));
  3215. dataseg->memory_index = mem_index;
  3216. }
  3217. dataseg->data_length = data_seg_len;
  3218. CHECK_BUF(p, p_end, data_seg_len);
  3219. dataseg->data = (uint8 *)p;
  3220. p += data_seg_len;
  3221. }
  3222. }
  3223. if (p != p_end) {
  3224. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3225. return false;
  3226. }
  3227. LOG_VERBOSE("Load data segment section success.\n");
  3228. return true;
  3229. fail:
  3230. return false;
  3231. }
  3232. #if WASM_ENABLE_BULK_MEMORY != 0
  3233. static bool
  3234. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  3235. WASMModule *module, char *error_buf,
  3236. uint32 error_buf_size)
  3237. {
  3238. const uint8 *p = buf, *p_end = buf_end;
  3239. uint32 data_seg_count1 = 0;
  3240. read_leb_uint32(p, p_end, data_seg_count1);
  3241. module->data_seg_count1 = data_seg_count1;
  3242. if (p != p_end) {
  3243. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3244. return false;
  3245. }
  3246. LOG_VERBOSE("Load datacount section success.\n");
  3247. return true;
  3248. fail:
  3249. return false;
  3250. }
  3251. #endif
  3252. static bool
  3253. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  3254. const uint8 *buf_func_end, WASMModule *module,
  3255. char *error_buf, uint32 error_buf_size)
  3256. {
  3257. const uint8 *p = buf, *p_end = buf_end;
  3258. const uint8 *p_func = buf_func;
  3259. uint32 func_count = 0, code_count;
  3260. /* code has been loaded in function section, so pass it here, just check
  3261. * whether function and code section have inconsistent lengths */
  3262. read_leb_uint32(p, p_end, code_count);
  3263. if (buf_func)
  3264. read_leb_uint32(p_func, buf_func_end, func_count);
  3265. if (func_count != code_count) {
  3266. set_error_buf(error_buf, error_buf_size,
  3267. "function and code section have inconsistent lengths");
  3268. return false;
  3269. }
  3270. LOG_VERBOSE("Load code segment section success.\n");
  3271. (void)module;
  3272. return true;
  3273. fail:
  3274. return false;
  3275. }
  3276. static bool
  3277. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3278. char *error_buf, uint32 error_buf_size)
  3279. {
  3280. const uint8 *p = buf, *p_end = buf_end;
  3281. WASMFuncType *type;
  3282. uint32 start_function;
  3283. read_leb_uint32(p, p_end, start_function);
  3284. if (start_function
  3285. >= module->function_count + module->import_function_count) {
  3286. set_error_buf(error_buf, error_buf_size, "unknown function");
  3287. return false;
  3288. }
  3289. if (start_function < module->import_function_count)
  3290. type = module->import_functions[start_function].u.function.func_type;
  3291. else
  3292. type = module->functions[start_function - module->import_function_count]
  3293. ->func_type;
  3294. if (type->param_count != 0 || type->result_count != 0) {
  3295. set_error_buf(error_buf, error_buf_size, "invalid start function");
  3296. return false;
  3297. }
  3298. module->start_function = start_function;
  3299. if (p != p_end) {
  3300. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3301. return false;
  3302. }
  3303. LOG_VERBOSE("Load start section success.\n");
  3304. return true;
  3305. fail:
  3306. return false;
  3307. }
  3308. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  3309. static bool
  3310. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3311. bool is_load_from_file_buf, char *error_buf,
  3312. uint32 error_buf_size)
  3313. {
  3314. const uint8 *p = buf, *p_end = buf_end;
  3315. uint32 name_type, subsection_size;
  3316. uint32 previous_name_type = 0;
  3317. uint32 num_func_name;
  3318. uint32 func_index;
  3319. uint32 previous_func_index = ~0U;
  3320. uint32 func_name_len;
  3321. uint32 name_index;
  3322. int i = 0;
  3323. if (p >= p_end) {
  3324. set_error_buf(error_buf, error_buf_size, "unexpected end");
  3325. return false;
  3326. }
  3327. while (p < p_end) {
  3328. read_leb_uint32(p, p_end, name_type);
  3329. if (i != 0) {
  3330. if (name_type == previous_name_type) {
  3331. set_error_buf(error_buf, error_buf_size,
  3332. "duplicate sub-section");
  3333. return false;
  3334. }
  3335. if (name_type < previous_name_type) {
  3336. set_error_buf(error_buf, error_buf_size,
  3337. "out-of-order sub-section");
  3338. return false;
  3339. }
  3340. }
  3341. previous_name_type = name_type;
  3342. read_leb_uint32(p, p_end, subsection_size);
  3343. CHECK_BUF(p, p_end, subsection_size);
  3344. switch (name_type) {
  3345. case SUB_SECTION_TYPE_FUNC:
  3346. if (subsection_size) {
  3347. read_leb_uint32(p, p_end, num_func_name);
  3348. for (name_index = 0; name_index < num_func_name;
  3349. name_index++) {
  3350. read_leb_uint32(p, p_end, func_index);
  3351. if (func_index == previous_func_index) {
  3352. set_error_buf(error_buf, error_buf_size,
  3353. "duplicate function name");
  3354. return false;
  3355. }
  3356. if (func_index < previous_func_index
  3357. && previous_func_index != ~0U) {
  3358. set_error_buf(error_buf, error_buf_size,
  3359. "out-of-order function index ");
  3360. return false;
  3361. }
  3362. previous_func_index = func_index;
  3363. read_leb_uint32(p, p_end, func_name_len);
  3364. CHECK_BUF(p, p_end, func_name_len);
  3365. /* Skip the import functions */
  3366. if (func_index >= module->import_function_count) {
  3367. func_index -= module->import_function_count;
  3368. if (func_index >= module->function_count) {
  3369. set_error_buf(error_buf, error_buf_size,
  3370. "out-of-range function index");
  3371. return false;
  3372. }
  3373. if (!(module->functions[func_index]->field_name =
  3374. const_str_list_insert(
  3375. p, func_name_len, module,
  3376. is_load_from_file_buf, error_buf,
  3377. error_buf_size))) {
  3378. return false;
  3379. }
  3380. }
  3381. p += func_name_len;
  3382. }
  3383. }
  3384. break;
  3385. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  3386. */
  3387. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  3388. default:
  3389. p = p + subsection_size;
  3390. break;
  3391. }
  3392. i++;
  3393. }
  3394. return true;
  3395. fail:
  3396. return false;
  3397. }
  3398. #endif
  3399. static bool
  3400. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3401. bool is_load_from_file_buf, char *error_buf,
  3402. uint32 error_buf_size)
  3403. {
  3404. const uint8 *p = buf, *p_end = buf_end;
  3405. char section_name[32];
  3406. uint32 name_len, buffer_len;
  3407. if (p >= p_end) {
  3408. set_error_buf(error_buf, error_buf_size, "unexpected end");
  3409. return false;
  3410. }
  3411. read_leb_uint32(p, p_end, name_len);
  3412. if (name_len == 0 || p + name_len > p_end) {
  3413. set_error_buf(error_buf, error_buf_size, "unexpected end");
  3414. return false;
  3415. }
  3416. if (!check_utf8_str(p, name_len)) {
  3417. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  3418. return false;
  3419. }
  3420. buffer_len = sizeof(section_name);
  3421. memset(section_name, 0, buffer_len);
  3422. if (name_len < buffer_len) {
  3423. bh_memcpy_s(section_name, buffer_len, p, name_len);
  3424. }
  3425. else {
  3426. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  3427. memset(section_name + buffer_len - 4, '.', 3);
  3428. }
  3429. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  3430. if (memcmp(p, "name", 4) == 0) {
  3431. module->name_section_buf = buf;
  3432. module->name_section_buf_end = buf_end;
  3433. p += name_len;
  3434. handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
  3435. error_buf_size);
  3436. LOG_VERBOSE("Load custom name section success.");
  3437. return true;
  3438. }
  3439. #endif
  3440. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  3441. {
  3442. WASMCustomSection *section =
  3443. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  3444. if (!section) {
  3445. return false;
  3446. }
  3447. section->name_addr = (char *)p;
  3448. section->name_len = name_len;
  3449. section->content_addr = (uint8 *)(p + name_len);
  3450. section->content_len = (uint32)(p_end - p - name_len);
  3451. section->next = module->custom_section_list;
  3452. module->custom_section_list = section;
  3453. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  3454. return true;
  3455. }
  3456. #endif
  3457. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  3458. (void)is_load_from_file_buf;
  3459. (void)module;
  3460. return true;
  3461. fail:
  3462. return false;
  3463. }
  3464. static void
  3465. calculate_global_data_offset(WASMModule *module)
  3466. {
  3467. uint32 i, data_offset;
  3468. data_offset = 0;
  3469. for (i = 0; i < module->import_global_count; i++) {
  3470. WASMGlobalImport *import_global =
  3471. &((module->import_globals + i)->u.global);
  3472. #if WASM_ENABLE_FAST_JIT != 0
  3473. import_global->data_offset = data_offset;
  3474. #endif
  3475. data_offset += wasm_value_type_size(import_global->type);
  3476. }
  3477. for (i = 0; i < module->global_count; i++) {
  3478. WASMGlobal *global = module->globals + i;
  3479. #if WASM_ENABLE_FAST_JIT != 0
  3480. global->data_offset = data_offset;
  3481. #endif
  3482. data_offset += wasm_value_type_size(global->type);
  3483. }
  3484. module->global_data_size = data_offset;
  3485. }
  3486. #if WASM_ENABLE_FAST_JIT != 0
  3487. static bool
  3488. init_fast_jit_functions(WASMModule *module, char *error_buf,
  3489. uint32 error_buf_size)
  3490. {
  3491. #if WASM_ENABLE_LAZY_JIT != 0
  3492. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  3493. #endif
  3494. uint32 i;
  3495. if (!module->function_count)
  3496. return true;
  3497. if (!(module->fast_jit_func_ptrs =
  3498. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  3499. error_buf_size))) {
  3500. return false;
  3501. }
  3502. #if WASM_ENABLE_LAZY_JIT != 0
  3503. for (i = 0; i < module->function_count; i++) {
  3504. module->fast_jit_func_ptrs[i] =
  3505. jit_globals->compile_fast_jit_and_then_call;
  3506. }
  3507. #endif
  3508. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  3509. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  3510. set_error_buf(error_buf, error_buf_size,
  3511. "init fast jit thread lock failed");
  3512. return false;
  3513. }
  3514. module->fast_jit_thread_locks_inited[i] = true;
  3515. }
  3516. return true;
  3517. }
  3518. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  3519. #if WASM_ENABLE_JIT != 0
  3520. static bool
  3521. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  3522. uint32 error_buf_size)
  3523. {
  3524. LLVMJITOptions llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  3525. AOTCompOption option = { 0 };
  3526. char *aot_last_error;
  3527. uint64 size;
  3528. bool gc_enabled =
  3529. #if WASM_ENABLE_GC != 0
  3530. true
  3531. #else
  3532. false
  3533. #endif
  3534. ;
  3535. if (module->function_count == 0)
  3536. return true;
  3537. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3538. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  3539. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  3540. return false;
  3541. }
  3542. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  3543. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  3544. os_mutex_destroy(&module->tierup_wait_lock);
  3545. return false;
  3546. }
  3547. module->tierup_wait_lock_inited = true;
  3548. #endif
  3549. size = sizeof(void *) * (uint64)module->function_count
  3550. + sizeof(bool) * (uint64)module->function_count;
  3551. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  3552. return false;
  3553. }
  3554. module->func_ptrs_compiled =
  3555. (bool *)((uint8 *)module->func_ptrs
  3556. + sizeof(void *) * module->function_count);
  3557. module->comp_data = aot_create_comp_data(module, gc_enabled);
  3558. if (!module->comp_data) {
  3559. aot_last_error = aot_get_last_error();
  3560. bh_assert(aot_last_error != NULL);
  3561. set_error_buf(error_buf, error_buf_size, aot_last_error);
  3562. return false;
  3563. }
  3564. option.is_jit_mode = true;
  3565. llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  3566. option.opt_level = llvm_jit_options.opt_level;
  3567. option.size_level = llvm_jit_options.size_level;
  3568. option.segue_flags = llvm_jit_options.segue_flags;
  3569. #if WASM_ENABLE_BULK_MEMORY != 0
  3570. option.enable_bulk_memory = true;
  3571. #endif
  3572. #if WASM_ENABLE_THREAD_MGR != 0
  3573. option.enable_thread_mgr = true;
  3574. #endif
  3575. #if WASM_ENABLE_TAIL_CALL != 0
  3576. option.enable_tail_call = true;
  3577. #endif
  3578. #if WASM_ENABLE_SIMD != 0
  3579. option.enable_simd = true;
  3580. #endif
  3581. #if WASM_ENABLE_REF_TYPES != 0
  3582. option.enable_ref_types = true;
  3583. #endif
  3584. option.enable_aux_stack_check = true;
  3585. #if (WASM_ENABLE_PERF_PROFILING != 0) || (WASM_ENABLE_DUMP_CALL_STACK != 0) \
  3586. || (WASM_ENABLE_JIT_STACK_FRAME != 0)
  3587. option.enable_aux_stack_frame = true;
  3588. #endif
  3589. #if WASM_ENABLE_MEMORY_PROFILING != 0
  3590. option.enable_stack_estimation = true;
  3591. #endif
  3592. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  3593. if (!module->comp_ctx) {
  3594. aot_last_error = aot_get_last_error();
  3595. bh_assert(aot_last_error != NULL);
  3596. set_error_buf(error_buf, error_buf_size, aot_last_error);
  3597. return false;
  3598. }
  3599. return true;
  3600. }
  3601. static bool
  3602. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  3603. uint32 error_buf_size)
  3604. {
  3605. char *aot_last_error;
  3606. uint32 i;
  3607. if (module->function_count == 0)
  3608. return true;
  3609. if (!aot_compile_wasm(module->comp_ctx)) {
  3610. aot_last_error = aot_get_last_error();
  3611. bh_assert(aot_last_error != NULL);
  3612. set_error_buf(error_buf, error_buf_size, aot_last_error);
  3613. return false;
  3614. }
  3615. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3616. if (module->orcjit_stop_compiling)
  3617. return false;
  3618. #endif
  3619. bh_print_time("Begin to lookup llvm jit functions");
  3620. for (i = 0; i < module->function_count; i++) {
  3621. LLVMOrcJITTargetAddress func_addr = 0;
  3622. LLVMErrorRef error;
  3623. char func_name[48];
  3624. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  3625. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  3626. func_name);
  3627. if (error != LLVMErrorSuccess) {
  3628. char *err_msg = LLVMGetErrorMessage(error);
  3629. set_error_buf_v(error_buf, error_buf_size,
  3630. "failed to compile llvm jit function: %s", err_msg);
  3631. LLVMDisposeErrorMessage(err_msg);
  3632. return false;
  3633. }
  3634. /**
  3635. * No need to lock the func_ptr[func_idx] here as it is basic
  3636. * data type, the load/store for it can be finished by one cpu
  3637. * instruction, and there can be only one cpu instruction
  3638. * loading/storing at the same time.
  3639. */
  3640. module->func_ptrs[i] = (void *)func_addr;
  3641. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3642. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  3643. if (module->orcjit_stop_compiling)
  3644. return false;
  3645. #endif
  3646. }
  3647. bh_print_time("End lookup llvm jit functions");
  3648. return true;
  3649. }
  3650. #endif /* end of WASM_ENABLE_JIT != 0 */
  3651. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3652. && WASM_ENABLE_LAZY_JIT != 0
  3653. static void *
  3654. init_llvm_jit_functions_stage2_callback(void *arg)
  3655. {
  3656. WASMModule *module = (WASMModule *)arg;
  3657. char error_buf[128];
  3658. uint32 error_buf_size = (uint32)sizeof(error_buf);
  3659. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  3660. module->orcjit_stop_compiling = true;
  3661. return NULL;
  3662. }
  3663. os_mutex_lock(&module->tierup_wait_lock);
  3664. module->llvm_jit_inited = true;
  3665. os_cond_broadcast(&module->tierup_wait_cond);
  3666. os_mutex_unlock(&module->tierup_wait_lock);
  3667. return NULL;
  3668. }
  3669. #endif
  3670. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  3671. /* The callback function to compile jit functions */
  3672. static void *
  3673. orcjit_thread_callback(void *arg)
  3674. {
  3675. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  3676. #if WASM_ENABLE_JIT != 0
  3677. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  3678. #endif
  3679. WASMModule *module = thread_arg->module;
  3680. uint32 group_idx = thread_arg->group_idx;
  3681. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  3682. uint32 func_count = module->function_count;
  3683. uint32 i;
  3684. #if WASM_ENABLE_FAST_JIT != 0
  3685. /* Compile fast jit funcitons of this group */
  3686. for (i = group_idx; i < func_count; i += group_stride) {
  3687. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  3688. os_printf("failed to compile fast jit function %u\n", i);
  3689. break;
  3690. }
  3691. if (module->orcjit_stop_compiling) {
  3692. return NULL;
  3693. }
  3694. }
  3695. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3696. os_mutex_lock(&module->tierup_wait_lock);
  3697. module->fast_jit_ready_groups++;
  3698. os_mutex_unlock(&module->tierup_wait_lock);
  3699. #endif
  3700. #endif
  3701. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3702. && WASM_ENABLE_LAZY_JIT != 0
  3703. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  3704. for (i = group_idx; i < func_count;
  3705. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  3706. uint32 j;
  3707. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  3708. if (i + j * group_stride < func_count) {
  3709. if (!jit_compiler_set_call_to_fast_jit(
  3710. module,
  3711. i + j * group_stride + module->import_function_count)) {
  3712. os_printf(
  3713. "failed to compile call_to_fast_jit for func %u\n",
  3714. i + j * group_stride + module->import_function_count);
  3715. module->orcjit_stop_compiling = true;
  3716. return NULL;
  3717. }
  3718. }
  3719. if (module->orcjit_stop_compiling) {
  3720. return NULL;
  3721. }
  3722. }
  3723. }
  3724. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  3725. fast jit functions are compiled */
  3726. os_mutex_lock(&module->tierup_wait_lock);
  3727. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  3728. && module->fast_jit_ready_groups >= group_stride)) {
  3729. os_cond_reltimedwait(&module->tierup_wait_cond,
  3730. &module->tierup_wait_lock, 10000);
  3731. if (module->orcjit_stop_compiling) {
  3732. /* init_llvm_jit_functions_stage2 failed */
  3733. os_mutex_unlock(&module->tierup_wait_lock);
  3734. return NULL;
  3735. }
  3736. }
  3737. os_mutex_unlock(&module->tierup_wait_lock);
  3738. #endif
  3739. #if WASM_ENABLE_JIT != 0
  3740. /* Compile llvm jit functions of this group */
  3741. for (i = group_idx; i < func_count;
  3742. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  3743. LLVMOrcJITTargetAddress func_addr = 0;
  3744. LLVMErrorRef error;
  3745. char func_name[48];
  3746. typedef void (*F)(void);
  3747. union {
  3748. F f;
  3749. void *v;
  3750. } u;
  3751. uint32 j;
  3752. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  3753. "_wrapper");
  3754. LOG_DEBUG("compile llvm jit func %s", func_name);
  3755. error =
  3756. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  3757. if (error != LLVMErrorSuccess) {
  3758. char *err_msg = LLVMGetErrorMessage(error);
  3759. os_printf("failed to compile llvm jit function %u: %s", i, err_msg);
  3760. LLVMDisposeErrorMessage(err_msg);
  3761. break;
  3762. }
  3763. /* Call the jit wrapper function to trigger its compilation, so as
  3764. to compile the actual jit functions, since we add the latter to
  3765. function list in the PartitionFunction callback */
  3766. u.v = (void *)func_addr;
  3767. u.f();
  3768. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  3769. if (i + j * group_stride < func_count) {
  3770. module->func_ptrs_compiled[i + j * group_stride] = true;
  3771. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3772. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  3773. i + j * group_stride);
  3774. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  3775. func_name);
  3776. if (error != LLVMErrorSuccess) {
  3777. char *err_msg = LLVMGetErrorMessage(error);
  3778. os_printf("failed to compile llvm jit function %u: %s", i,
  3779. err_msg);
  3780. LLVMDisposeErrorMessage(err_msg);
  3781. /* Ignore current llvm jit func, as its func ptr is
  3782. previous set to call_to_fast_jit, which also works */
  3783. continue;
  3784. }
  3785. jit_compiler_set_llvm_jit_func_ptr(
  3786. module,
  3787. i + j * group_stride + module->import_function_count,
  3788. (void *)func_addr);
  3789. /* Try to switch to call this llvm jit funtion instead of
  3790. fast jit function from fast jit jitted code */
  3791. jit_compiler_set_call_to_llvm_jit(
  3792. module,
  3793. i + j * group_stride + module->import_function_count);
  3794. #endif
  3795. }
  3796. }
  3797. if (module->orcjit_stop_compiling) {
  3798. break;
  3799. }
  3800. }
  3801. #endif
  3802. return NULL;
  3803. }
  3804. static void
  3805. orcjit_stop_compile_threads(WASMModule *module)
  3806. {
  3807. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  3808. / sizeof(OrcJitThreadArg));
  3809. module->orcjit_stop_compiling = true;
  3810. for (i = 0; i < thread_num; i++) {
  3811. if (module->orcjit_threads[i])
  3812. os_thread_join(module->orcjit_threads[i], NULL);
  3813. }
  3814. }
  3815. static bool
  3816. compile_jit_functions(WASMModule *module, char *error_buf,
  3817. uint32 error_buf_size)
  3818. {
  3819. uint32 thread_num =
  3820. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  3821. uint32 i, j;
  3822. bh_print_time("Begin to compile jit functions");
  3823. /* Create threads to compile the jit functions */
  3824. for (i = 0; i < thread_num && i < module->function_count; i++) {
  3825. #if WASM_ENABLE_JIT != 0
  3826. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  3827. #endif
  3828. module->orcjit_thread_args[i].module = module;
  3829. module->orcjit_thread_args[i].group_idx = i;
  3830. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  3831. (void *)&module->orcjit_thread_args[i],
  3832. APP_THREAD_STACK_SIZE_DEFAULT)
  3833. != 0) {
  3834. set_error_buf(error_buf, error_buf_size,
  3835. "create orcjit compile thread failed");
  3836. /* Terminate the threads created */
  3837. module->orcjit_stop_compiling = true;
  3838. for (j = 0; j < i; j++) {
  3839. os_thread_join(module->orcjit_threads[j], NULL);
  3840. }
  3841. return false;
  3842. }
  3843. }
  3844. #if WASM_ENABLE_LAZY_JIT == 0
  3845. /* Wait until all jit functions are compiled for eager mode */
  3846. for (i = 0; i < thread_num; i++) {
  3847. if (module->orcjit_threads[i])
  3848. os_thread_join(module->orcjit_threads[i], NULL);
  3849. }
  3850. #if WASM_ENABLE_FAST_JIT != 0
  3851. /* Ensure all the fast-jit functions are compiled */
  3852. for (i = 0; i < module->function_count; i++) {
  3853. if (!jit_compiler_is_compiled(module,
  3854. i + module->import_function_count)) {
  3855. set_error_buf(error_buf, error_buf_size,
  3856. "failed to compile fast jit function");
  3857. return false;
  3858. }
  3859. }
  3860. #endif
  3861. #if WASM_ENABLE_JIT != 0
  3862. /* Ensure all the llvm-jit functions are compiled */
  3863. for (i = 0; i < module->function_count; i++) {
  3864. if (!module->func_ptrs_compiled[i]) {
  3865. set_error_buf(error_buf, error_buf_size,
  3866. "failed to compile llvm jit function");
  3867. return false;
  3868. }
  3869. }
  3870. #endif
  3871. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  3872. bh_print_time("End compile jit functions");
  3873. return true;
  3874. }
  3875. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  3876. static bool
  3877. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  3878. uint32 cur_func_idx, char *error_buf,
  3879. uint32 error_buf_size);
  3880. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  3881. void **
  3882. wasm_interp_get_handle_table();
  3883. static void **handle_table;
  3884. #endif
  3885. static bool
  3886. load_from_sections(WASMModule *module, WASMSection *sections,
  3887. bool is_load_from_file_buf, char *error_buf,
  3888. uint32 error_buf_size)
  3889. {
  3890. WASMExport *export;
  3891. WASMSection *section = sections;
  3892. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  3893. *buf_func = NULL, *buf_func_end = NULL;
  3894. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  3895. WASMGlobal *aux_stack_top_global = NULL, *global;
  3896. uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1;
  3897. uint32 aux_stack_top = (uint32)-1, global_index, func_index, i;
  3898. uint32 aux_data_end_global_index = (uint32)-1;
  3899. uint32 aux_heap_base_global_index = (uint32)-1;
  3900. WASMFuncType *func_type;
  3901. /* Find code and function sections if have */
  3902. while (section) {
  3903. if (section->section_type == SECTION_TYPE_CODE) {
  3904. buf_code = section->section_body;
  3905. buf_code_end = buf_code + section->section_body_size;
  3906. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  3907. module->buf_code = (uint8 *)buf_code;
  3908. module->buf_code_size = section->section_body_size;
  3909. #endif
  3910. }
  3911. else if (section->section_type == SECTION_TYPE_FUNC) {
  3912. buf_func = section->section_body;
  3913. buf_func_end = buf_func + section->section_body_size;
  3914. }
  3915. section = section->next;
  3916. }
  3917. section = sections;
  3918. while (section) {
  3919. buf = section->section_body;
  3920. buf_end = buf + section->section_body_size;
  3921. switch (section->section_type) {
  3922. case SECTION_TYPE_USER:
  3923. /* unsupported user section, ignore it. */
  3924. if (!load_user_section(buf, buf_end, module,
  3925. is_load_from_file_buf, error_buf,
  3926. error_buf_size))
  3927. return false;
  3928. break;
  3929. case SECTION_TYPE_TYPE:
  3930. if (!load_type_section(buf, buf_end, module, error_buf,
  3931. error_buf_size))
  3932. return false;
  3933. break;
  3934. case SECTION_TYPE_IMPORT:
  3935. if (!load_import_section(buf, buf_end, module,
  3936. is_load_from_file_buf, error_buf,
  3937. error_buf_size))
  3938. return false;
  3939. break;
  3940. case SECTION_TYPE_FUNC:
  3941. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  3942. module, error_buf, error_buf_size))
  3943. return false;
  3944. break;
  3945. case SECTION_TYPE_TABLE:
  3946. if (!load_table_section(buf, buf_end, module, error_buf,
  3947. error_buf_size))
  3948. return false;
  3949. break;
  3950. case SECTION_TYPE_MEMORY:
  3951. if (!load_memory_section(buf, buf_end, module, error_buf,
  3952. error_buf_size))
  3953. return false;
  3954. break;
  3955. case SECTION_TYPE_GLOBAL:
  3956. if (!load_global_section(buf, buf_end, module, error_buf,
  3957. error_buf_size))
  3958. return false;
  3959. break;
  3960. case SECTION_TYPE_EXPORT:
  3961. if (!load_export_section(buf, buf_end, module,
  3962. is_load_from_file_buf, error_buf,
  3963. error_buf_size))
  3964. return false;
  3965. break;
  3966. case SECTION_TYPE_START:
  3967. if (!load_start_section(buf, buf_end, module, error_buf,
  3968. error_buf_size))
  3969. return false;
  3970. break;
  3971. case SECTION_TYPE_ELEM:
  3972. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  3973. error_buf_size))
  3974. return false;
  3975. break;
  3976. case SECTION_TYPE_CODE:
  3977. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  3978. module, error_buf, error_buf_size))
  3979. return false;
  3980. break;
  3981. case SECTION_TYPE_DATA:
  3982. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  3983. error_buf_size))
  3984. return false;
  3985. break;
  3986. #if WASM_ENABLE_BULK_MEMORY != 0
  3987. case SECTION_TYPE_DATACOUNT:
  3988. if (!load_datacount_section(buf, buf_end, module, error_buf,
  3989. error_buf_size))
  3990. return false;
  3991. break;
  3992. #endif
  3993. default:
  3994. set_error_buf(error_buf, error_buf_size, "invalid section id");
  3995. return false;
  3996. }
  3997. section = section->next;
  3998. }
  3999. module->aux_data_end_global_index = (uint32)-1;
  4000. module->aux_heap_base_global_index = (uint32)-1;
  4001. module->aux_stack_top_global_index = (uint32)-1;
  4002. /* Resolve auxiliary data/stack/heap info and reset memory info */
  4003. export = module->exports;
  4004. for (i = 0; i < module->export_count; i++, export ++) {
  4005. if (export->kind == EXPORT_KIND_GLOBAL) {
  4006. if (!strcmp(export->name, "__heap_base")) {
  4007. global_index = export->index - module->import_global_count;
  4008. global = module->globals + global_index;
  4009. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  4010. && global->init_expr.init_expr_type
  4011. == INIT_EXPR_TYPE_I32_CONST) {
  4012. aux_heap_base_global = global;
  4013. aux_heap_base = global->init_expr.u.i32;
  4014. aux_heap_base_global_index = export->index;
  4015. LOG_VERBOSE("Found aux __heap_base global, value: %d",
  4016. aux_heap_base);
  4017. }
  4018. }
  4019. else if (!strcmp(export->name, "__data_end")) {
  4020. global_index = export->index - module->import_global_count;
  4021. global = module->globals + global_index;
  4022. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  4023. && global->init_expr.init_expr_type
  4024. == INIT_EXPR_TYPE_I32_CONST) {
  4025. aux_data_end_global = global;
  4026. aux_data_end = global->init_expr.u.i32;
  4027. aux_data_end_global_index = export->index;
  4028. LOG_VERBOSE("Found aux __data_end global, value: %d",
  4029. aux_data_end);
  4030. aux_data_end = align_uint(aux_data_end, 16);
  4031. }
  4032. }
  4033. /* For module compiled with -pthread option, the global is:
  4034. [0] stack_top <-- 0
  4035. [1] tls_pointer
  4036. [2] tls_size
  4037. [3] data_end <-- 3
  4038. [4] global_base
  4039. [5] heap_base <-- 5
  4040. [6] dso_handle
  4041. For module compiled without -pthread option:
  4042. [0] stack_top <-- 0
  4043. [1] data_end <-- 1
  4044. [2] global_base
  4045. [3] heap_base <-- 3
  4046. [4] dso_handle
  4047. */
  4048. if (aux_data_end_global && aux_heap_base_global
  4049. && aux_data_end <= aux_heap_base) {
  4050. module->aux_data_end_global_index = aux_data_end_global_index;
  4051. module->aux_data_end = aux_data_end;
  4052. module->aux_heap_base_global_index = aux_heap_base_global_index;
  4053. module->aux_heap_base = aux_heap_base;
  4054. /* Resolve aux stack top global */
  4055. for (global_index = 0; global_index < module->global_count;
  4056. global_index++) {
  4057. global = module->globals + global_index;
  4058. if (global->is_mutable /* heap_base and data_end is
  4059. not mutable */
  4060. && global->type == VALUE_TYPE_I32
  4061. && global->init_expr.init_expr_type
  4062. == INIT_EXPR_TYPE_I32_CONST
  4063. && (uint32)global->init_expr.u.i32 <= aux_heap_base) {
  4064. aux_stack_top_global = global;
  4065. aux_stack_top = (uint32)global->init_expr.u.i32;
  4066. module->aux_stack_top_global_index =
  4067. module->import_global_count + global_index;
  4068. module->aux_stack_bottom = aux_stack_top;
  4069. module->aux_stack_size =
  4070. aux_stack_top > aux_data_end
  4071. ? aux_stack_top - aux_data_end
  4072. : aux_stack_top;
  4073. LOG_VERBOSE("Found aux stack top global, value: %d, "
  4074. "global index: %d, stack size: %d",
  4075. aux_stack_top, global_index,
  4076. module->aux_stack_size);
  4077. break;
  4078. }
  4079. }
  4080. if (!aux_stack_top_global) {
  4081. /* Auxiliary stack global isn't found, it must be unused
  4082. in the wasm app, as if it is used, the global must be
  4083. defined. Here we set it to __heap_base global and set
  4084. its size to 0. */
  4085. aux_stack_top_global = aux_heap_base_global;
  4086. aux_stack_top = aux_heap_base;
  4087. module->aux_stack_top_global_index =
  4088. module->aux_heap_base_global_index;
  4089. module->aux_stack_bottom = aux_stack_top;
  4090. module->aux_stack_size = 0;
  4091. }
  4092. break;
  4093. }
  4094. }
  4095. }
  4096. module->malloc_function = (uint32)-1;
  4097. module->free_function = (uint32)-1;
  4098. module->retain_function = (uint32)-1;
  4099. /* Resolve malloc/free function exported by wasm module */
  4100. export = module->exports;
  4101. for (i = 0; i < module->export_count; i++, export ++) {
  4102. if (export->kind == EXPORT_KIND_FUNC) {
  4103. if (!strcmp(export->name, "malloc")
  4104. && export->index >= module->import_function_count) {
  4105. func_index = export->index - module->import_function_count;
  4106. func_type = module->functions[func_index]->func_type;
  4107. if (func_type->param_count == 1 && func_type->result_count == 1
  4108. && func_type->types[0] == VALUE_TYPE_I32
  4109. && func_type->types[1] == VALUE_TYPE_I32) {
  4110. bh_assert(module->malloc_function == (uint32)-1);
  4111. module->malloc_function = export->index;
  4112. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  4113. export->name, export->index);
  4114. }
  4115. }
  4116. else if (!strcmp(export->name, "__new")
  4117. && export->index >= module->import_function_count) {
  4118. /* __new && __pin for AssemblyScript */
  4119. func_index = export->index - module->import_function_count;
  4120. func_type = module->functions[func_index]->func_type;
  4121. if (func_type->param_count == 2 && func_type->result_count == 1
  4122. && func_type->types[0] == VALUE_TYPE_I32
  4123. && func_type->types[1] == VALUE_TYPE_I32
  4124. && func_type->types[2] == VALUE_TYPE_I32) {
  4125. uint32 j;
  4126. WASMExport *export_tmp;
  4127. bh_assert(module->malloc_function == (uint32)-1);
  4128. module->malloc_function = export->index;
  4129. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  4130. export->name, export->index);
  4131. /* resolve retain function.
  4132. If not found, reset malloc function index */
  4133. export_tmp = module->exports;
  4134. for (j = 0; j < module->export_count; j++, export_tmp++) {
  4135. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  4136. && (!strcmp(export_tmp->name, "__retain")
  4137. || (!strcmp(export_tmp->name, "__pin")))
  4138. && (export_tmp->index
  4139. >= module->import_function_count)) {
  4140. func_index = export_tmp->index
  4141. - module->import_function_count;
  4142. func_type =
  4143. module->functions[func_index]->func_type;
  4144. if (func_type->param_count == 1
  4145. && func_type->result_count == 1
  4146. && func_type->types[0] == VALUE_TYPE_I32
  4147. && func_type->types[1] == VALUE_TYPE_I32) {
  4148. bh_assert(module->retain_function
  4149. == (uint32)-1);
  4150. module->retain_function = export_tmp->index;
  4151. LOG_VERBOSE("Found retain function, name: %s, "
  4152. "index: %u",
  4153. export_tmp->name,
  4154. export_tmp->index);
  4155. break;
  4156. }
  4157. }
  4158. }
  4159. if (j == module->export_count) {
  4160. module->malloc_function = (uint32)-1;
  4161. LOG_VERBOSE("Can't find retain function,"
  4162. "reset malloc function index to -1");
  4163. }
  4164. }
  4165. }
  4166. else if (((!strcmp(export->name, "free"))
  4167. || (!strcmp(export->name, "__release"))
  4168. || (!strcmp(export->name, "__unpin")))
  4169. && export->index >= module->import_function_count) {
  4170. func_index = export->index - module->import_function_count;
  4171. func_type = module->functions[func_index]->func_type;
  4172. if (func_type->param_count == 1 && func_type->result_count == 0
  4173. && func_type->types[0] == VALUE_TYPE_I32) {
  4174. bh_assert(module->free_function == (uint32)-1);
  4175. module->free_function = export->index;
  4176. LOG_VERBOSE("Found free function, name: %s, index: %u",
  4177. export->name, export->index);
  4178. }
  4179. }
  4180. }
  4181. }
  4182. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  4183. handle_table = wasm_interp_get_handle_table();
  4184. #endif
  4185. for (i = 0; i < module->function_count; i++) {
  4186. WASMFunction *func = module->functions[i];
  4187. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  4188. error_buf_size)) {
  4189. return false;
  4190. }
  4191. if (i == module->function_count - 1
  4192. && func->code + func->code_size != buf_code_end) {
  4193. set_error_buf(error_buf, error_buf_size,
  4194. "code section size mismatch");
  4195. return false;
  4196. }
  4197. }
  4198. if (!module->possible_memory_grow) {
  4199. WASMMemoryImport *memory_import;
  4200. WASMMemory *memory;
  4201. if (aux_data_end_global && aux_heap_base_global
  4202. && aux_stack_top_global) {
  4203. uint64 init_memory_size;
  4204. uint32 shrunk_memory_size = align_uint(aux_heap_base, 8);
  4205. if (module->import_memory_count) {
  4206. memory_import = &module->import_memories[0].u.memory;
  4207. init_memory_size = (uint64)memory_import->num_bytes_per_page
  4208. * memory_import->init_page_count;
  4209. if (shrunk_memory_size <= init_memory_size) {
  4210. /* Reset memory info to decrease memory usage */
  4211. memory_import->num_bytes_per_page = shrunk_memory_size;
  4212. memory_import->init_page_count = 1;
  4213. LOG_VERBOSE("Shrink import memory size to %d",
  4214. shrunk_memory_size);
  4215. }
  4216. }
  4217. if (module->memory_count) {
  4218. memory = &module->memories[0];
  4219. init_memory_size = (uint64)memory->num_bytes_per_page
  4220. * memory->init_page_count;
  4221. if (shrunk_memory_size <= init_memory_size) {
  4222. /* Reset memory info to decrease memory usage */
  4223. memory->num_bytes_per_page = shrunk_memory_size;
  4224. memory->init_page_count = 1;
  4225. LOG_VERBOSE("Shrink memory size to %d", shrunk_memory_size);
  4226. }
  4227. }
  4228. }
  4229. #if WASM_ENABLE_MULTI_MODULE == 0
  4230. if (module->import_memory_count) {
  4231. memory_import = &module->import_memories[0].u.memory;
  4232. if (memory_import->init_page_count < DEFAULT_MAX_PAGES)
  4233. memory_import->num_bytes_per_page *=
  4234. memory_import->init_page_count;
  4235. else
  4236. memory_import->num_bytes_per_page = UINT32_MAX;
  4237. if (memory_import->init_page_count > 0)
  4238. memory_import->init_page_count = memory_import->max_page_count =
  4239. 1;
  4240. else
  4241. memory_import->init_page_count = memory_import->max_page_count =
  4242. 0;
  4243. }
  4244. if (module->memory_count) {
  4245. memory = &module->memories[0];
  4246. if (memory->init_page_count < DEFAULT_MAX_PAGES)
  4247. memory->num_bytes_per_page *= memory->init_page_count;
  4248. else
  4249. memory->num_bytes_per_page = UINT32_MAX;
  4250. if (memory->init_page_count > 0)
  4251. memory->init_page_count = memory->max_page_count = 1;
  4252. else
  4253. memory->init_page_count = memory->max_page_count = 0;
  4254. }
  4255. #endif
  4256. }
  4257. calculate_global_data_offset(module);
  4258. #if WASM_ENABLE_FAST_JIT != 0
  4259. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  4260. return false;
  4261. }
  4262. #endif
  4263. #if WASM_ENABLE_JIT != 0
  4264. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  4265. return false;
  4266. }
  4267. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  4268. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4269. return false;
  4270. }
  4271. #else
  4272. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  4273. thread fast jit execution, since applying llvm optimizations in
  4274. aot_compile_wasm may cost a lot of time.
  4275. Create thread with enough native stack to apply llvm optimizations */
  4276. if (os_thread_create(&module->llvm_jit_init_thread,
  4277. init_llvm_jit_functions_stage2_callback,
  4278. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  4279. != 0) {
  4280. set_error_buf(error_buf, error_buf_size,
  4281. "create orcjit compile thread failed");
  4282. return false;
  4283. }
  4284. #endif
  4285. #endif
  4286. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4287. /* Create threads to compile the jit functions */
  4288. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  4289. return false;
  4290. }
  4291. #endif
  4292. #if WASM_ENABLE_MEMORY_TRACING != 0
  4293. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  4294. #endif
  4295. return true;
  4296. }
  4297. static WASMModule *
  4298. create_module(char *error_buf, uint32 error_buf_size)
  4299. {
  4300. WASMModule *module =
  4301. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  4302. bh_list_status ret;
  4303. if (!module) {
  4304. return NULL;
  4305. }
  4306. module->module_type = Wasm_Module_Bytecode;
  4307. /* Set start_function to -1, means no start function */
  4308. module->start_function = (uint32)-1;
  4309. #if WASM_ENABLE_FAST_INTERP == 0
  4310. module->br_table_cache_list = &module->br_table_cache_list_head;
  4311. ret = bh_list_init(module->br_table_cache_list);
  4312. bh_assert(ret == BH_LIST_SUCCESS);
  4313. #endif
  4314. #if WASM_ENABLE_MULTI_MODULE != 0
  4315. module->import_module_list = &module->import_module_list_head;
  4316. ret = bh_list_init(module->import_module_list);
  4317. bh_assert(ret == BH_LIST_SUCCESS);
  4318. #endif
  4319. #if WASM_ENABLE_DEBUG_INTERP != 0
  4320. ret = bh_list_init(&module->fast_opcode_list);
  4321. bh_assert(ret == BH_LIST_SUCCESS);
  4322. #endif
  4323. #if WASM_ENABLE_GC != 0
  4324. if (!(module->ref_type_set =
  4325. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  4326. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  4327. goto fail1;
  4328. }
  4329. if (os_mutex_init(&module->rtt_type_lock)) {
  4330. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  4331. goto fail2;
  4332. }
  4333. #endif
  4334. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  4335. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4336. && WASM_ENABLE_LAZY_JIT != 0)
  4337. if (os_mutex_init(&module->instance_list_lock) != 0) {
  4338. set_error_buf(error_buf, error_buf_size,
  4339. "init instance list lock failed");
  4340. goto fail3;
  4341. }
  4342. #endif
  4343. (void)ret;
  4344. return module;
  4345. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  4346. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  4347. && WASM_ENABLE_LAZY_JIT != 0)
  4348. fail3:
  4349. #endif
  4350. #if WASM_ENABLE_GC != 0
  4351. os_mutex_destroy(&module->rtt_type_lock);
  4352. fail2:
  4353. bh_hash_map_destroy(module->ref_type_set);
  4354. fail1:
  4355. #endif
  4356. wasm_runtime_free(module);
  4357. return NULL;
  4358. }
  4359. #if WASM_ENABLE_DEBUG_INTERP != 0
  4360. static bool
  4361. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  4362. uint32 error_buf_size)
  4363. {
  4364. WASMFastOPCodeNode *fast_op =
  4365. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  4366. if (fast_op) {
  4367. fast_op->offset = pos - module->load_addr;
  4368. fast_op->orig_op = orig_op;
  4369. bh_list_insert(&module->fast_opcode_list, fast_op);
  4370. }
  4371. return fast_op ? true : false;
  4372. }
  4373. #endif
  4374. WASMModule *
  4375. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  4376. uint32 error_buf_size)
  4377. {
  4378. WASMModule *module = create_module(error_buf, error_buf_size);
  4379. if (!module)
  4380. return NULL;
  4381. if (!load_from_sections(module, section_list, false, error_buf,
  4382. error_buf_size)) {
  4383. wasm_loader_unload(module);
  4384. return NULL;
  4385. }
  4386. LOG_VERBOSE("Load module from sections success.\n");
  4387. return module;
  4388. }
  4389. static void
  4390. destroy_sections(WASMSection *section_list)
  4391. {
  4392. WASMSection *section = section_list, *next;
  4393. while (section) {
  4394. next = section->next;
  4395. wasm_runtime_free(section);
  4396. section = next;
  4397. }
  4398. }
  4399. /* clang-format off */
  4400. static uint8 section_ids[] = {
  4401. SECTION_TYPE_USER,
  4402. SECTION_TYPE_TYPE,
  4403. SECTION_TYPE_IMPORT,
  4404. SECTION_TYPE_FUNC,
  4405. SECTION_TYPE_TABLE,
  4406. SECTION_TYPE_MEMORY,
  4407. SECTION_TYPE_GLOBAL,
  4408. SECTION_TYPE_EXPORT,
  4409. SECTION_TYPE_START,
  4410. SECTION_TYPE_ELEM,
  4411. #if WASM_ENABLE_BULK_MEMORY != 0
  4412. SECTION_TYPE_DATACOUNT,
  4413. #endif
  4414. SECTION_TYPE_CODE,
  4415. SECTION_TYPE_DATA
  4416. };
  4417. /* clang-format on */
  4418. static uint8
  4419. get_section_index(uint8 section_type)
  4420. {
  4421. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  4422. for (uint8 i = 0; i < max_id; i++) {
  4423. if (section_type == section_ids[i])
  4424. return i;
  4425. }
  4426. return (uint8)-1;
  4427. }
  4428. static bool
  4429. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  4430. char *error_buf, uint32 error_buf_size)
  4431. {
  4432. WASMSection *section_list_end = NULL, *section;
  4433. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  4434. uint8 section_type, section_index, last_section_index = (uint8)-1;
  4435. uint32 section_size;
  4436. bh_assert(!*p_section_list);
  4437. p += 8;
  4438. while (p < p_end) {
  4439. CHECK_BUF(p, p_end, 1);
  4440. section_type = read_uint8(p);
  4441. section_index = get_section_index(section_type);
  4442. if (section_index != (uint8)-1) {
  4443. if (section_type != SECTION_TYPE_USER) {
  4444. /* Custom sections may be inserted at any place,
  4445. while other sections must occur at most once
  4446. and in prescribed order. */
  4447. if (last_section_index != (uint8)-1
  4448. && (section_index <= last_section_index)) {
  4449. set_error_buf(error_buf, error_buf_size,
  4450. "unexpected content after last section or "
  4451. "junk after last section");
  4452. return false;
  4453. }
  4454. last_section_index = section_index;
  4455. }
  4456. read_leb_uint32(p, p_end, section_size);
  4457. CHECK_BUF1(p, p_end, section_size);
  4458. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  4459. error_buf_size))) {
  4460. return false;
  4461. }
  4462. section->section_type = section_type;
  4463. section->section_body = (uint8 *)p;
  4464. section->section_body_size = section_size;
  4465. if (!section_list_end)
  4466. *p_section_list = section_list_end = section;
  4467. else {
  4468. section_list_end->next = section;
  4469. section_list_end = section;
  4470. }
  4471. p += section_size;
  4472. }
  4473. else {
  4474. set_error_buf(error_buf, error_buf_size, "invalid section id");
  4475. return false;
  4476. }
  4477. }
  4478. return true;
  4479. fail:
  4480. return false;
  4481. }
  4482. static void
  4483. exchange32(uint8 *p_data)
  4484. {
  4485. uint8 value = *p_data;
  4486. *p_data = *(p_data + 3);
  4487. *(p_data + 3) = value;
  4488. value = *(p_data + 1);
  4489. *(p_data + 1) = *(p_data + 2);
  4490. *(p_data + 2) = value;
  4491. }
  4492. static union {
  4493. int a;
  4494. char b;
  4495. } __ue = { .a = 1 };
  4496. #define is_little_endian() (__ue.b == 1)
  4497. static bool
  4498. load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
  4499. uint32 error_buf_size)
  4500. {
  4501. const uint8 *buf_end = buf + size;
  4502. const uint8 *p = buf, *p_end = buf_end;
  4503. uint32 magic_number, version;
  4504. WASMSection *section_list = NULL;
  4505. CHECK_BUF1(p, p_end, sizeof(uint32));
  4506. magic_number = read_uint32(p);
  4507. if (!is_little_endian())
  4508. exchange32((uint8 *)&magic_number);
  4509. if (magic_number != WASM_MAGIC_NUMBER) {
  4510. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  4511. return false;
  4512. }
  4513. CHECK_BUF1(p, p_end, sizeof(uint32));
  4514. version = read_uint32(p);
  4515. if (!is_little_endian())
  4516. exchange32((uint8 *)&version);
  4517. if (version != WASM_CURRENT_VERSION) {
  4518. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  4519. return false;
  4520. }
  4521. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  4522. || !load_from_sections(module, section_list, true, error_buf,
  4523. error_buf_size)) {
  4524. destroy_sections(section_list);
  4525. return false;
  4526. }
  4527. destroy_sections(section_list);
  4528. return true;
  4529. fail:
  4530. return false;
  4531. }
  4532. #if WASM_ENABLE_LIBC_WASI != 0
  4533. /**
  4534. * refer to
  4535. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  4536. */
  4537. static bool
  4538. check_wasi_abi_compatibility(const WASMModule *module,
  4539. #if WASM_ENABLE_MULTI_MODULE != 0
  4540. bool main_module,
  4541. #endif
  4542. char *error_buf, uint32 error_buf_size)
  4543. {
  4544. /**
  4545. * be careful with:
  4546. * wasi compatiable modules(command/reactor) which don't import any wasi
  4547. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  4548. * reactor can depend on nothing. At the same time, each has its own entry
  4549. * point.
  4550. *
  4551. * observations:
  4552. * - clang always injects `_start` into a command
  4553. * - clang always injects `_initialize` into a reactor
  4554. * - `iwasm -f` allows to run a function in the reactor
  4555. *
  4556. * strong assumptions:
  4557. * - no one will define either `_start` or `_initialize` on purpose
  4558. * - `_start` should always be `void _start(void)`
  4559. * - `_initialize` should always be `void _initialize(void)`
  4560. *
  4561. */
  4562. /* clang-format off */
  4563. /**
  4564. *
  4565. * | | import_wasi_api True | | import_wasi_api False | |
  4566. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  4567. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  4568. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  4569. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  4570. */
  4571. /* clang-format on */
  4572. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  4573. /* (func (export "_start") (...) */
  4574. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  4575. error_buf, error_buf_size);
  4576. if (start) {
  4577. WASMFuncType *func_type =
  4578. module->functions[start->index - module->import_function_count]
  4579. ->func_type;
  4580. if (func_type->param_count || func_type->result_count) {
  4581. set_error_buf(error_buf, error_buf_size,
  4582. "the signature of builtin _start function is wrong");
  4583. return false;
  4584. }
  4585. }
  4586. /* (func (export "_initialize") (...) */
  4587. initialize = wasm_loader_find_export(
  4588. module, "", "_initialize", EXPORT_KIND_FUNC, error_buf, error_buf_size);
  4589. if (initialize) {
  4590. WASMFuncType *func_type =
  4591. module->functions[initialize->index - module->import_function_count]
  4592. ->func_type;
  4593. if (func_type->param_count || func_type->result_count) {
  4594. set_error_buf(
  4595. error_buf, error_buf_size,
  4596. "the signature of builtin _initialize function is wrong");
  4597. return false;
  4598. }
  4599. }
  4600. /* filter out non-wasi compatiable modules */
  4601. if (!module->import_wasi_api && !start && !initialize) {
  4602. return true;
  4603. }
  4604. /* should have one at least */
  4605. if (module->import_wasi_api && !start && !initialize) {
  4606. LOG_WARNING("warning: a module with WASI apis should be either "
  4607. "a command or a reactor");
  4608. }
  4609. /*
  4610. * there is at least one of `_start` and `_initialize` in below cases.
  4611. * according to the assumption, they should be all wasi compatiable
  4612. */
  4613. /* always can not have both at the same time */
  4614. if (start && initialize) {
  4615. set_error_buf(
  4616. error_buf, error_buf_size,
  4617. "neither a command nor a reactor can both have _start function "
  4618. "and _initialize function at the same time");
  4619. return false;
  4620. }
  4621. #if WASM_ENABLE_MULTI_MODULE != 0
  4622. /* filter out commands (with `_start`) cases */
  4623. if (start && !main_module) {
  4624. set_error_buf(
  4625. error_buf, error_buf_size,
  4626. "a command (with _start function) can not be a sub-module");
  4627. return false;
  4628. }
  4629. #endif
  4630. /*
  4631. * it is ok a reactor acts as a main module,
  4632. * so skip the check about (with `_initialize`)
  4633. */
  4634. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  4635. error_buf, error_buf_size);
  4636. if (!memory
  4637. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  4638. /*
  4639. * with wasi-threads, it's still an open question if a memory
  4640. * should be exported.
  4641. *
  4642. * https://github.com/WebAssembly/wasi-threads/issues/22
  4643. * https://github.com/WebAssembly/WASI/issues/502
  4644. *
  4645. * Note: this code assumes the number of memories is at most 1.
  4646. */
  4647. && module->import_memory_count == 0
  4648. #endif
  4649. ) {
  4650. set_error_buf(error_buf, error_buf_size,
  4651. "a module with WASI apis must export memory by default");
  4652. return false;
  4653. }
  4654. return true;
  4655. }
  4656. #endif
  4657. WASMModule *
  4658. wasm_loader_load(uint8 *buf, uint32 size,
  4659. #if WASM_ENABLE_MULTI_MODULE != 0
  4660. bool main_module,
  4661. #endif
  4662. char *error_buf, uint32 error_buf_size)
  4663. {
  4664. WASMModule *module = create_module(error_buf, error_buf_size);
  4665. if (!module) {
  4666. return NULL;
  4667. }
  4668. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0
  4669. module->load_addr = (uint8 *)buf;
  4670. module->load_size = size;
  4671. #endif
  4672. if (!load(buf, size, module, error_buf, error_buf_size)) {
  4673. goto fail;
  4674. }
  4675. #if WASM_ENABLE_LIBC_WASI != 0
  4676. /* Check the WASI application ABI */
  4677. if (!check_wasi_abi_compatibility(module,
  4678. #if WASM_ENABLE_MULTI_MODULE != 0
  4679. main_module,
  4680. #endif
  4681. error_buf, error_buf_size)) {
  4682. goto fail;
  4683. }
  4684. #endif
  4685. LOG_VERBOSE("Load module success.\n");
  4686. return module;
  4687. fail:
  4688. wasm_loader_unload(module);
  4689. return NULL;
  4690. }
  4691. void
  4692. wasm_loader_unload(WASMModule *module)
  4693. {
  4694. uint32 i;
  4695. if (!module)
  4696. return;
  4697. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4698. && WASM_ENABLE_LAZY_JIT != 0
  4699. module->orcjit_stop_compiling = true;
  4700. if (module->llvm_jit_init_thread)
  4701. os_thread_join(module->llvm_jit_init_thread, NULL);
  4702. #endif
  4703. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4704. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  4705. module internal data after they were freed */
  4706. orcjit_stop_compile_threads(module);
  4707. #endif
  4708. #if WASM_ENABLE_JIT != 0
  4709. if (module->func_ptrs)
  4710. wasm_runtime_free(module->func_ptrs);
  4711. if (module->comp_ctx)
  4712. aot_destroy_comp_context(module->comp_ctx);
  4713. if (module->comp_data)
  4714. aot_destroy_comp_data(module->comp_data);
  4715. #endif
  4716. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4717. && WASM_ENABLE_LAZY_JIT != 0
  4718. if (module->tierup_wait_lock_inited) {
  4719. os_mutex_destroy(&module->tierup_wait_lock);
  4720. os_cond_destroy(&module->tierup_wait_cond);
  4721. }
  4722. #endif
  4723. if (module->types) {
  4724. for (i = 0; i < module->type_count; i++) {
  4725. if (module->types[i])
  4726. destroy_wasm_type(module->types[i]);
  4727. }
  4728. wasm_runtime_free(module->types);
  4729. }
  4730. if (module->imports)
  4731. wasm_runtime_free(module->imports);
  4732. if (module->functions) {
  4733. for (i = 0; i < module->function_count; i++) {
  4734. if (module->functions[i]) {
  4735. if (module->functions[i]->local_offsets)
  4736. wasm_runtime_free(module->functions[i]->local_offsets);
  4737. #if WASM_ENABLE_FAST_INTERP != 0
  4738. if (module->functions[i]->code_compiled)
  4739. wasm_runtime_free(module->functions[i]->code_compiled);
  4740. if (module->functions[i]->consts)
  4741. wasm_runtime_free(module->functions[i]->consts);
  4742. #endif
  4743. #if WASM_ENABLE_FAST_JIT != 0
  4744. if (module->functions[i]->fast_jit_jitted_code) {
  4745. jit_code_cache_free(
  4746. module->functions[i]->fast_jit_jitted_code);
  4747. }
  4748. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4749. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  4750. jit_code_cache_free(
  4751. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  4752. }
  4753. #endif
  4754. #endif
  4755. #if WASM_ENABLE_GC != 0
  4756. if (module->functions[i]->local_ref_type_maps) {
  4757. wasm_runtime_free(
  4758. module->functions[i]->local_ref_type_maps);
  4759. }
  4760. #endif
  4761. wasm_runtime_free(module->functions[i]);
  4762. }
  4763. }
  4764. wasm_runtime_free(module->functions);
  4765. }
  4766. if (module->tables)
  4767. wasm_runtime_free(module->tables);
  4768. if (module->memories)
  4769. wasm_runtime_free(module->memories);
  4770. if (module->globals)
  4771. wasm_runtime_free(module->globals);
  4772. if (module->exports)
  4773. wasm_runtime_free(module->exports);
  4774. if (module->table_segments) {
  4775. for (i = 0; i < module->table_seg_count; i++) {
  4776. if (module->table_segments[i].func_indexes)
  4777. wasm_runtime_free(module->table_segments[i].func_indexes);
  4778. }
  4779. wasm_runtime_free(module->table_segments);
  4780. }
  4781. if (module->data_segments) {
  4782. for (i = 0; i < module->data_seg_count; i++) {
  4783. if (module->data_segments[i])
  4784. wasm_runtime_free(module->data_segments[i]);
  4785. }
  4786. wasm_runtime_free(module->data_segments);
  4787. }
  4788. if (module->const_str_list) {
  4789. StringNode *node = module->const_str_list, *node_next;
  4790. while (node) {
  4791. node_next = node->next;
  4792. wasm_runtime_free(node);
  4793. node = node_next;
  4794. }
  4795. }
  4796. #if WASM_ENABLE_FAST_INTERP == 0
  4797. if (module->br_table_cache_list) {
  4798. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  4799. BrTableCache *node_next;
  4800. while (node) {
  4801. node_next = bh_list_elem_next(node);
  4802. wasm_runtime_free(node);
  4803. node = node_next;
  4804. }
  4805. }
  4806. #endif
  4807. #if WASM_ENABLE_MULTI_MODULE != 0
  4808. /* just release the sub module list */
  4809. if (module->import_module_list) {
  4810. WASMRegisteredModule *node =
  4811. bh_list_first_elem(module->import_module_list);
  4812. while (node) {
  4813. WASMRegisteredModule *next = bh_list_elem_next(node);
  4814. bh_list_remove(module->import_module_list, node);
  4815. /*
  4816. * unload(sub_module) will be trigged during runtime_destroy().
  4817. * every module in the global module list will be unloaded one by
  4818. * one. so don't worry.
  4819. */
  4820. wasm_runtime_free(node);
  4821. /*
  4822. * the module file reading buffer will be released
  4823. * in runtime_destroy()
  4824. */
  4825. node = next;
  4826. }
  4827. }
  4828. #endif
  4829. #if WASM_ENABLE_DEBUG_INTERP != 0
  4830. WASMFastOPCodeNode *fast_opcode =
  4831. bh_list_first_elem(&module->fast_opcode_list);
  4832. while (fast_opcode) {
  4833. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  4834. wasm_runtime_free(fast_opcode);
  4835. fast_opcode = next;
  4836. }
  4837. #endif
  4838. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  4839. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4840. && WASM_ENABLE_LAZY_JIT != 0)
  4841. os_mutex_destroy(&module->instance_list_lock);
  4842. #endif
  4843. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4844. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  4845. #endif
  4846. #if WASM_ENABLE_FAST_JIT != 0
  4847. if (module->fast_jit_func_ptrs) {
  4848. wasm_runtime_free(module->fast_jit_func_ptrs);
  4849. }
  4850. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4851. if (module->fast_jit_thread_locks_inited[i]) {
  4852. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  4853. }
  4854. }
  4855. #endif
  4856. #if WASM_ENABLE_GC != 0
  4857. os_mutex_destroy(&module->rtt_type_lock);
  4858. bh_hash_map_destroy(module->ref_type_set);
  4859. if (module->rtt_types) {
  4860. for (i = 0; i < module->type_count; i++) {
  4861. if (module->rtt_types[i])
  4862. wasm_runtime_free(module->rtt_types[i]);
  4863. }
  4864. wasm_runtime_free(module->rtt_types);
  4865. }
  4866. #endif
  4867. wasm_runtime_free(module);
  4868. }
  4869. bool
  4870. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  4871. const uint8 *start_addr, const uint8 *code_end_addr,
  4872. uint8 label_type, uint8 **p_else_addr,
  4873. uint8 **p_end_addr)
  4874. {
  4875. const uint8 *p = start_addr, *p_end = code_end_addr;
  4876. uint8 *else_addr = NULL;
  4877. char error_buf[128];
  4878. uint32 block_nested_depth = 1, count, i, j, t;
  4879. uint32 error_buf_size = sizeof(error_buf);
  4880. uint8 opcode, u8;
  4881. BlockAddr block_stack[16] = { { 0 } }, *block;
  4882. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  4883. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  4884. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  4885. if (block[j].start_addr == start_addr) {
  4886. /* Cache hit */
  4887. *p_else_addr = block[j].else_addr;
  4888. *p_end_addr = block[j].end_addr;
  4889. return true;
  4890. }
  4891. }
  4892. /* Cache unhit */
  4893. block_stack[0].start_addr = start_addr;
  4894. while (p < code_end_addr) {
  4895. opcode = *p++;
  4896. #if WASM_ENABLE_DEBUG_INTERP != 0
  4897. op_break_retry:
  4898. #endif
  4899. switch (opcode) {
  4900. case WASM_OP_UNREACHABLE:
  4901. case WASM_OP_NOP:
  4902. break;
  4903. case WASM_OP_BLOCK:
  4904. case WASM_OP_LOOP:
  4905. case WASM_OP_IF:
  4906. {
  4907. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  4908. u8 = read_uint8(p);
  4909. if (is_byte_a_type(u8)) {
  4910. #if WASM_ENABLE_GC != 0
  4911. if (wasm_is_type_multi_byte_type(u8)) {
  4912. /* the possible extra bytes of GC ref type have been
  4913. modified to OP_NOP, no need to resolve them again */
  4914. }
  4915. #endif
  4916. }
  4917. else {
  4918. p--;
  4919. skip_leb_uint32(p, p_end);
  4920. }
  4921. if (block_nested_depth
  4922. < sizeof(block_stack) / sizeof(BlockAddr)) {
  4923. block_stack[block_nested_depth].start_addr = p;
  4924. block_stack[block_nested_depth].else_addr = NULL;
  4925. }
  4926. block_nested_depth++;
  4927. break;
  4928. }
  4929. case EXT_OP_BLOCK:
  4930. case EXT_OP_LOOP:
  4931. case EXT_OP_IF:
  4932. /* block type */
  4933. skip_leb_uint32(p, p_end);
  4934. if (block_nested_depth
  4935. < sizeof(block_stack) / sizeof(BlockAddr)) {
  4936. block_stack[block_nested_depth].start_addr = p;
  4937. block_stack[block_nested_depth].else_addr = NULL;
  4938. }
  4939. block_nested_depth++;
  4940. break;
  4941. case WASM_OP_ELSE:
  4942. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  4943. else_addr = (uint8 *)(p - 1);
  4944. if (block_nested_depth - 1
  4945. < sizeof(block_stack) / sizeof(BlockAddr))
  4946. block_stack[block_nested_depth - 1].else_addr =
  4947. (uint8 *)(p - 1);
  4948. break;
  4949. case WASM_OP_END:
  4950. if (block_nested_depth == 1) {
  4951. if (label_type == LABEL_TYPE_IF)
  4952. *p_else_addr = else_addr;
  4953. *p_end_addr = (uint8 *)(p - 1);
  4954. block_stack[0].end_addr = (uint8 *)(p - 1);
  4955. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  4956. t++) {
  4957. start_addr = block_stack[t].start_addr;
  4958. if (start_addr) {
  4959. i = ((uintptr_t)start_addr)
  4960. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  4961. block =
  4962. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  4963. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  4964. if (!block[j].start_addr)
  4965. break;
  4966. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  4967. memmove(block + 1, block,
  4968. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  4969. * sizeof(BlockAddr));
  4970. j = 0;
  4971. }
  4972. block[j].start_addr = block_stack[t].start_addr;
  4973. block[j].else_addr = block_stack[t].else_addr;
  4974. block[j].end_addr = block_stack[t].end_addr;
  4975. }
  4976. else
  4977. break;
  4978. }
  4979. return true;
  4980. }
  4981. else {
  4982. block_nested_depth--;
  4983. if (block_nested_depth
  4984. < sizeof(block_stack) / sizeof(BlockAddr))
  4985. block_stack[block_nested_depth].end_addr =
  4986. (uint8 *)(p - 1);
  4987. }
  4988. break;
  4989. case WASM_OP_BR:
  4990. case WASM_OP_BR_IF:
  4991. skip_leb_uint32(p, p_end); /* labelidx */
  4992. break;
  4993. case WASM_OP_BR_TABLE:
  4994. read_leb_uint32(p, p_end, count); /* lable num */
  4995. #if WASM_ENABLE_FAST_INTERP != 0
  4996. for (i = 0; i <= count; i++) /* lableidxs */
  4997. skip_leb_uint32(p, p_end);
  4998. #else
  4999. p += count + 1;
  5000. while (*p == WASM_OP_NOP)
  5001. p++;
  5002. #endif
  5003. break;
  5004. #if WASM_ENABLE_FAST_INTERP == 0
  5005. case EXT_OP_BR_TABLE_CACHE:
  5006. read_leb_uint32(p, p_end, count); /* lable num */
  5007. while (*p == WASM_OP_NOP)
  5008. p++;
  5009. break;
  5010. #endif
  5011. case WASM_OP_RETURN:
  5012. break;
  5013. case WASM_OP_CALL:
  5014. #if WASM_ENABLE_TAIL_CALL != 0
  5015. case WASM_OP_RETURN_CALL:
  5016. #endif
  5017. skip_leb_uint32(p, p_end); /* funcidx */
  5018. break;
  5019. case WASM_OP_CALL_INDIRECT:
  5020. #if WASM_ENABLE_TAIL_CALL != 0
  5021. case WASM_OP_RETURN_CALL_INDIRECT:
  5022. #endif
  5023. skip_leb_uint32(p, p_end); /* typeidx */
  5024. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  5025. u8 = read_uint8(p); /* 0x00 */
  5026. #else
  5027. skip_leb_uint32(p, p_end); /* talbeidx */
  5028. #endif
  5029. break;
  5030. #if WASM_ENABLE_GC != 0
  5031. case WASM_OP_CALL_REF:
  5032. case WASM_OP_RETURN_CALL_REF:
  5033. skip_leb_uint32(p, p_end); /* typeidx */
  5034. break;
  5035. #endif
  5036. case WASM_OP_DROP:
  5037. case WASM_OP_SELECT:
  5038. case WASM_OP_DROP_64:
  5039. case WASM_OP_SELECT_64:
  5040. break;
  5041. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  5042. case WASM_OP_SELECT_T:
  5043. {
  5044. skip_leb_uint32(p, p_end); /* vec length */
  5045. u8 = read_uint8(p); /* typeidx */
  5046. /* the possible extra bytes of GC ref type have been
  5047. modified to OP_NOP, no need to resolve them again */
  5048. break;
  5049. }
  5050. case WASM_OP_TABLE_GET:
  5051. case WASM_OP_TABLE_SET:
  5052. skip_leb_uint32(p, p_end); /* table index */
  5053. break;
  5054. case WASM_OP_REF_NULL:
  5055. {
  5056. u8 = read_uint8(p); /* type */
  5057. if (is_byte_a_type(u8)) {
  5058. #if WASM_ENABLE_GC != 0
  5059. if (wasm_is_type_multi_byte_type(u8)) {
  5060. /* the possible extra bytes of GC ref type have been
  5061. modified to OP_NOP, no need to resolve them again */
  5062. }
  5063. #endif
  5064. }
  5065. else {
  5066. p--;
  5067. skip_leb_uint32(p, p_end);
  5068. }
  5069. break;
  5070. }
  5071. case WASM_OP_REF_IS_NULL:
  5072. break;
  5073. case WASM_OP_REF_FUNC:
  5074. skip_leb_uint32(p, p_end); /* func index */
  5075. break;
  5076. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  5077. #if WASM_ENABLE_GC != 0
  5078. case WASM_OP_REF_AS_NON_NULL:
  5079. case WASM_OP_REF_EQ:
  5080. break;
  5081. case WASM_OP_BR_ON_NULL:
  5082. case WASM_OP_BR_ON_NON_NULL:
  5083. skip_leb_uint32(p, p_end); /* label index */
  5084. break;
  5085. #endif /* end of WASM_ENABLE_GC != 0 */
  5086. case WASM_OP_GET_LOCAL:
  5087. case WASM_OP_SET_LOCAL:
  5088. case WASM_OP_TEE_LOCAL:
  5089. case WASM_OP_GET_GLOBAL:
  5090. case WASM_OP_SET_GLOBAL:
  5091. case WASM_OP_GET_GLOBAL_64:
  5092. case WASM_OP_SET_GLOBAL_64:
  5093. case WASM_OP_SET_GLOBAL_AUX_STACK:
  5094. skip_leb_uint32(p, p_end); /* local index */
  5095. break;
  5096. case EXT_OP_GET_LOCAL_FAST:
  5097. case EXT_OP_SET_LOCAL_FAST:
  5098. case EXT_OP_TEE_LOCAL_FAST:
  5099. CHECK_BUF(p, p_end, 1);
  5100. p++;
  5101. break;
  5102. case WASM_OP_I32_LOAD:
  5103. case WASM_OP_I64_LOAD:
  5104. case WASM_OP_F32_LOAD:
  5105. case WASM_OP_F64_LOAD:
  5106. case WASM_OP_I32_LOAD8_S:
  5107. case WASM_OP_I32_LOAD8_U:
  5108. case WASM_OP_I32_LOAD16_S:
  5109. case WASM_OP_I32_LOAD16_U:
  5110. case WASM_OP_I64_LOAD8_S:
  5111. case WASM_OP_I64_LOAD8_U:
  5112. case WASM_OP_I64_LOAD16_S:
  5113. case WASM_OP_I64_LOAD16_U:
  5114. case WASM_OP_I64_LOAD32_S:
  5115. case WASM_OP_I64_LOAD32_U:
  5116. case WASM_OP_I32_STORE:
  5117. case WASM_OP_I64_STORE:
  5118. case WASM_OP_F32_STORE:
  5119. case WASM_OP_F64_STORE:
  5120. case WASM_OP_I32_STORE8:
  5121. case WASM_OP_I32_STORE16:
  5122. case WASM_OP_I64_STORE8:
  5123. case WASM_OP_I64_STORE16:
  5124. case WASM_OP_I64_STORE32:
  5125. skip_leb_uint32(p, p_end); /* align */
  5126. skip_leb_uint32(p, p_end); /* offset */
  5127. break;
  5128. case WASM_OP_MEMORY_SIZE:
  5129. case WASM_OP_MEMORY_GROW:
  5130. skip_leb_uint32(p, p_end); /* 0x00 */
  5131. break;
  5132. case WASM_OP_I32_CONST:
  5133. skip_leb_int32(p, p_end);
  5134. break;
  5135. case WASM_OP_I64_CONST:
  5136. skip_leb_int64(p, p_end);
  5137. break;
  5138. case WASM_OP_F32_CONST:
  5139. p += sizeof(float32);
  5140. break;
  5141. case WASM_OP_F64_CONST:
  5142. p += sizeof(float64);
  5143. break;
  5144. case WASM_OP_I32_EQZ:
  5145. case WASM_OP_I32_EQ:
  5146. case WASM_OP_I32_NE:
  5147. case WASM_OP_I32_LT_S:
  5148. case WASM_OP_I32_LT_U:
  5149. case WASM_OP_I32_GT_S:
  5150. case WASM_OP_I32_GT_U:
  5151. case WASM_OP_I32_LE_S:
  5152. case WASM_OP_I32_LE_U:
  5153. case WASM_OP_I32_GE_S:
  5154. case WASM_OP_I32_GE_U:
  5155. case WASM_OP_I64_EQZ:
  5156. case WASM_OP_I64_EQ:
  5157. case WASM_OP_I64_NE:
  5158. case WASM_OP_I64_LT_S:
  5159. case WASM_OP_I64_LT_U:
  5160. case WASM_OP_I64_GT_S:
  5161. case WASM_OP_I64_GT_U:
  5162. case WASM_OP_I64_LE_S:
  5163. case WASM_OP_I64_LE_U:
  5164. case WASM_OP_I64_GE_S:
  5165. case WASM_OP_I64_GE_U:
  5166. case WASM_OP_F32_EQ:
  5167. case WASM_OP_F32_NE:
  5168. case WASM_OP_F32_LT:
  5169. case WASM_OP_F32_GT:
  5170. case WASM_OP_F32_LE:
  5171. case WASM_OP_F32_GE:
  5172. case WASM_OP_F64_EQ:
  5173. case WASM_OP_F64_NE:
  5174. case WASM_OP_F64_LT:
  5175. case WASM_OP_F64_GT:
  5176. case WASM_OP_F64_LE:
  5177. case WASM_OP_F64_GE:
  5178. case WASM_OP_I32_CLZ:
  5179. case WASM_OP_I32_CTZ:
  5180. case WASM_OP_I32_POPCNT:
  5181. case WASM_OP_I32_ADD:
  5182. case WASM_OP_I32_SUB:
  5183. case WASM_OP_I32_MUL:
  5184. case WASM_OP_I32_DIV_S:
  5185. case WASM_OP_I32_DIV_U:
  5186. case WASM_OP_I32_REM_S:
  5187. case WASM_OP_I32_REM_U:
  5188. case WASM_OP_I32_AND:
  5189. case WASM_OP_I32_OR:
  5190. case WASM_OP_I32_XOR:
  5191. case WASM_OP_I32_SHL:
  5192. case WASM_OP_I32_SHR_S:
  5193. case WASM_OP_I32_SHR_U:
  5194. case WASM_OP_I32_ROTL:
  5195. case WASM_OP_I32_ROTR:
  5196. case WASM_OP_I64_CLZ:
  5197. case WASM_OP_I64_CTZ:
  5198. case WASM_OP_I64_POPCNT:
  5199. case WASM_OP_I64_ADD:
  5200. case WASM_OP_I64_SUB:
  5201. case WASM_OP_I64_MUL:
  5202. case WASM_OP_I64_DIV_S:
  5203. case WASM_OP_I64_DIV_U:
  5204. case WASM_OP_I64_REM_S:
  5205. case WASM_OP_I64_REM_U:
  5206. case WASM_OP_I64_AND:
  5207. case WASM_OP_I64_OR:
  5208. case WASM_OP_I64_XOR:
  5209. case WASM_OP_I64_SHL:
  5210. case WASM_OP_I64_SHR_S:
  5211. case WASM_OP_I64_SHR_U:
  5212. case WASM_OP_I64_ROTL:
  5213. case WASM_OP_I64_ROTR:
  5214. case WASM_OP_F32_ABS:
  5215. case WASM_OP_F32_NEG:
  5216. case WASM_OP_F32_CEIL:
  5217. case WASM_OP_F32_FLOOR:
  5218. case WASM_OP_F32_TRUNC:
  5219. case WASM_OP_F32_NEAREST:
  5220. case WASM_OP_F32_SQRT:
  5221. case WASM_OP_F32_ADD:
  5222. case WASM_OP_F32_SUB:
  5223. case WASM_OP_F32_MUL:
  5224. case WASM_OP_F32_DIV:
  5225. case WASM_OP_F32_MIN:
  5226. case WASM_OP_F32_MAX:
  5227. case WASM_OP_F32_COPYSIGN:
  5228. case WASM_OP_F64_ABS:
  5229. case WASM_OP_F64_NEG:
  5230. case WASM_OP_F64_CEIL:
  5231. case WASM_OP_F64_FLOOR:
  5232. case WASM_OP_F64_TRUNC:
  5233. case WASM_OP_F64_NEAREST:
  5234. case WASM_OP_F64_SQRT:
  5235. case WASM_OP_F64_ADD:
  5236. case WASM_OP_F64_SUB:
  5237. case WASM_OP_F64_MUL:
  5238. case WASM_OP_F64_DIV:
  5239. case WASM_OP_F64_MIN:
  5240. case WASM_OP_F64_MAX:
  5241. case WASM_OP_F64_COPYSIGN:
  5242. case WASM_OP_I32_WRAP_I64:
  5243. case WASM_OP_I32_TRUNC_S_F32:
  5244. case WASM_OP_I32_TRUNC_U_F32:
  5245. case WASM_OP_I32_TRUNC_S_F64:
  5246. case WASM_OP_I32_TRUNC_U_F64:
  5247. case WASM_OP_I64_EXTEND_S_I32:
  5248. case WASM_OP_I64_EXTEND_U_I32:
  5249. case WASM_OP_I64_TRUNC_S_F32:
  5250. case WASM_OP_I64_TRUNC_U_F32:
  5251. case WASM_OP_I64_TRUNC_S_F64:
  5252. case WASM_OP_I64_TRUNC_U_F64:
  5253. case WASM_OP_F32_CONVERT_S_I32:
  5254. case WASM_OP_F32_CONVERT_U_I32:
  5255. case WASM_OP_F32_CONVERT_S_I64:
  5256. case WASM_OP_F32_CONVERT_U_I64:
  5257. case WASM_OP_F32_DEMOTE_F64:
  5258. case WASM_OP_F64_CONVERT_S_I32:
  5259. case WASM_OP_F64_CONVERT_U_I32:
  5260. case WASM_OP_F64_CONVERT_S_I64:
  5261. case WASM_OP_F64_CONVERT_U_I64:
  5262. case WASM_OP_F64_PROMOTE_F32:
  5263. case WASM_OP_I32_REINTERPRET_F32:
  5264. case WASM_OP_I64_REINTERPRET_F64:
  5265. case WASM_OP_F32_REINTERPRET_I32:
  5266. case WASM_OP_F64_REINTERPRET_I64:
  5267. case WASM_OP_I32_EXTEND8_S:
  5268. case WASM_OP_I32_EXTEND16_S:
  5269. case WASM_OP_I64_EXTEND8_S:
  5270. case WASM_OP_I64_EXTEND16_S:
  5271. case WASM_OP_I64_EXTEND32_S:
  5272. break;
  5273. #if WASM_ENABLE_GC != 0
  5274. case WASM_OP_GC_PREFIX:
  5275. {
  5276. uint32 opcode1;
  5277. read_leb_uint32(p, p_end, opcode1);
  5278. switch (opcode1) {
  5279. case WASM_OP_STRUCT_NEW_CANON:
  5280. case WASM_OP_STRUCT_NEW_CANON_DEFAULT:
  5281. skip_leb_uint32(p, p_end); /* typeidx */
  5282. break;
  5283. case WASM_OP_STRUCT_GET:
  5284. case WASM_OP_STRUCT_GET_S:
  5285. case WASM_OP_STRUCT_GET_U:
  5286. case WASM_OP_STRUCT_SET:
  5287. skip_leb_uint32(p, p_end); /* typeidx */
  5288. skip_leb_uint32(p, p_end); /* fieldidx */
  5289. break;
  5290. case WASM_OP_ARRAY_NEW_CANON:
  5291. case WASM_OP_ARRAY_NEW_CANON_DEFAULT:
  5292. case WASM_OP_ARRAY_GET:
  5293. case WASM_OP_ARRAY_GET_S:
  5294. case WASM_OP_ARRAY_GET_U:
  5295. case WASM_OP_ARRAY_SET:
  5296. skip_leb_uint32(p, p_end); /* typeidx */
  5297. break;
  5298. #if WASM_ENABLE_GC_BINARYEN != 0
  5299. case WASM_OP_ARRAY_COPY:
  5300. skip_leb_uint32(p, p_end); /* typeidx1 */
  5301. skip_leb_uint32(p, p_end); /* typeidx2 */
  5302. break;
  5303. #endif
  5304. case WASM_OP_ARRAY_LEN:
  5305. break;
  5306. case WASM_OP_ARRAY_NEW_CANON_FIXED:
  5307. case WASM_OP_ARRAY_NEW_CANON_DATA:
  5308. case WASM_OP_ARRAY_NEW_CANON_ELEM:
  5309. skip_leb_uint32(p, p_end); /* typeidx */
  5310. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  5311. break;
  5312. case WASM_OP_I31_NEW:
  5313. case WASM_OP_I31_GET_S:
  5314. case WASM_OP_I31_GET_U:
  5315. break;
  5316. case WASM_OP_REF_TEST:
  5317. case WASM_OP_REF_CAST:
  5318. case WASM_OP_REF_TEST_NULLABLE:
  5319. case WASM_OP_REF_CAST_NULLABLE:
  5320. skip_leb_int32(p, p_end); /* heaptype */
  5321. break;
  5322. case WASM_OP_BR_ON_CAST:
  5323. case WASM_OP_BR_ON_CAST_FAIL:
  5324. case WASM_OP_BR_ON_CAST_NULLABLE:
  5325. case WASM_OP_BR_ON_CAST_FAIL_NULLABLE:
  5326. skip_leb_uint32(p, p_end); /* labelidx */
  5327. skip_leb_int32(p, p_end); /* heaptype */
  5328. break;
  5329. case WASM_OP_EXTERN_INTERNALIZE:
  5330. case WASM_OP_EXTERN_EXTERNALIZE:
  5331. break;
  5332. default:
  5333. return false;
  5334. }
  5335. break;
  5336. }
  5337. #endif /* end of WASM_ENABLE_GC != 0 */
  5338. case WASM_OP_MISC_PREFIX:
  5339. {
  5340. uint32 opcode1;
  5341. read_leb_uint32(p, p_end, opcode1);
  5342. switch (opcode1) {
  5343. case WASM_OP_I32_TRUNC_SAT_S_F32:
  5344. case WASM_OP_I32_TRUNC_SAT_U_F32:
  5345. case WASM_OP_I32_TRUNC_SAT_S_F64:
  5346. case WASM_OP_I32_TRUNC_SAT_U_F64:
  5347. case WASM_OP_I64_TRUNC_SAT_S_F32:
  5348. case WASM_OP_I64_TRUNC_SAT_U_F32:
  5349. case WASM_OP_I64_TRUNC_SAT_S_F64:
  5350. case WASM_OP_I64_TRUNC_SAT_U_F64:
  5351. break;
  5352. #if WASM_ENABLE_BULK_MEMORY != 0
  5353. case WASM_OP_MEMORY_INIT:
  5354. skip_leb_uint32(p, p_end);
  5355. /* skip memory idx */
  5356. p++;
  5357. break;
  5358. case WASM_OP_DATA_DROP:
  5359. skip_leb_uint32(p, p_end);
  5360. break;
  5361. case WASM_OP_MEMORY_COPY:
  5362. /* skip two memory idx */
  5363. p += 2;
  5364. break;
  5365. case WASM_OP_MEMORY_FILL:
  5366. /* skip memory idx */
  5367. p++;
  5368. break;
  5369. #endif /* WASM_ENABLE_BULK_MEMORY */
  5370. #if WASM_ENABLE_REF_TYPES != 0
  5371. case WASM_OP_TABLE_INIT:
  5372. case WASM_OP_TABLE_COPY:
  5373. /* tableidx */
  5374. skip_leb_uint32(p, p_end);
  5375. /* elemidx */
  5376. skip_leb_uint32(p, p_end);
  5377. break;
  5378. case WASM_OP_ELEM_DROP:
  5379. /* elemidx */
  5380. skip_leb_uint32(p, p_end);
  5381. break;
  5382. case WASM_OP_TABLE_SIZE:
  5383. case WASM_OP_TABLE_GROW:
  5384. case WASM_OP_TABLE_FILL:
  5385. skip_leb_uint32(p, p_end); /* table idx */
  5386. break;
  5387. #endif /* WASM_ENABLE_REF_TYPES */
  5388. default:
  5389. return false;
  5390. }
  5391. break;
  5392. }
  5393. #if WASM_ENABLE_SIMD != 0
  5394. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  5395. case WASM_OP_SIMD_PREFIX:
  5396. {
  5397. /* TODO: shall we ceate a table to be friendly to branch
  5398. * prediction */
  5399. opcode = read_uint8(p);
  5400. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  5401. */
  5402. switch (opcode) {
  5403. case SIMD_v128_load:
  5404. case SIMD_v128_load8x8_s:
  5405. case SIMD_v128_load8x8_u:
  5406. case SIMD_v128_load16x4_s:
  5407. case SIMD_v128_load16x4_u:
  5408. case SIMD_v128_load32x2_s:
  5409. case SIMD_v128_load32x2_u:
  5410. case SIMD_v128_load8_splat:
  5411. case SIMD_v128_load16_splat:
  5412. case SIMD_v128_load32_splat:
  5413. case SIMD_v128_load64_splat:
  5414. case SIMD_v128_store:
  5415. /* memarg align */
  5416. skip_leb_uint32(p, p_end);
  5417. /* memarg offset*/
  5418. skip_leb_uint32(p, p_end);
  5419. break;
  5420. case SIMD_v128_const:
  5421. case SIMD_v8x16_shuffle:
  5422. /* immByte[16] immLaneId[16] */
  5423. CHECK_BUF1(p, p_end, 16);
  5424. p += 16;
  5425. break;
  5426. case SIMD_i8x16_extract_lane_s:
  5427. case SIMD_i8x16_extract_lane_u:
  5428. case SIMD_i8x16_replace_lane:
  5429. case SIMD_i16x8_extract_lane_s:
  5430. case SIMD_i16x8_extract_lane_u:
  5431. case SIMD_i16x8_replace_lane:
  5432. case SIMD_i32x4_extract_lane:
  5433. case SIMD_i32x4_replace_lane:
  5434. case SIMD_i64x2_extract_lane:
  5435. case SIMD_i64x2_replace_lane:
  5436. case SIMD_f32x4_extract_lane:
  5437. case SIMD_f32x4_replace_lane:
  5438. case SIMD_f64x2_extract_lane:
  5439. case SIMD_f64x2_replace_lane:
  5440. /* ImmLaneId */
  5441. CHECK_BUF(p, p_end, 1);
  5442. p++;
  5443. break;
  5444. case SIMD_v128_load8_lane:
  5445. case SIMD_v128_load16_lane:
  5446. case SIMD_v128_load32_lane:
  5447. case SIMD_v128_load64_lane:
  5448. case SIMD_v128_store8_lane:
  5449. case SIMD_v128_store16_lane:
  5450. case SIMD_v128_store32_lane:
  5451. case SIMD_v128_store64_lane:
  5452. /* memarg align */
  5453. skip_leb_uint32(p, p_end);
  5454. /* memarg offset*/
  5455. skip_leb_uint32(p, p_end);
  5456. /* ImmLaneId */
  5457. CHECK_BUF(p, p_end, 1);
  5458. p++;
  5459. break;
  5460. case SIMD_v128_load32_zero:
  5461. case SIMD_v128_load64_zero:
  5462. /* memarg align */
  5463. skip_leb_uint32(p, p_end);
  5464. /* memarg offset*/
  5465. skip_leb_uint32(p, p_end);
  5466. break;
  5467. default:
  5468. /*
  5469. * since latest SIMD specific used almost every value
  5470. * from 0x00 to 0xff, the default branch will present
  5471. * all opcodes without imm
  5472. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  5473. */
  5474. break;
  5475. }
  5476. break;
  5477. }
  5478. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  5479. #endif /* end of WASM_ENABLE_SIMD */
  5480. #if WASM_ENABLE_SHARED_MEMORY != 0
  5481. case WASM_OP_ATOMIC_PREFIX:
  5482. {
  5483. /* atomic_op (1 u8) + memarg (2 u32_leb) */
  5484. opcode = read_uint8(p);
  5485. if (opcode != WASM_OP_ATOMIC_FENCE) {
  5486. skip_leb_uint32(p, p_end); /* align */
  5487. skip_leb_uint32(p, p_end); /* offset */
  5488. }
  5489. else {
  5490. /* atomic.fence doesn't have memarg */
  5491. p++;
  5492. }
  5493. break;
  5494. }
  5495. #endif
  5496. #if WASM_ENABLE_DEBUG_INTERP != 0
  5497. case DEBUG_OP_BREAK:
  5498. {
  5499. WASMDebugInstance *debug_instance =
  5500. wasm_exec_env_get_instance(exec_env);
  5501. char orignal_opcode[1];
  5502. uint64 size = 1;
  5503. WASMModuleInstance *module_inst =
  5504. (WASMModuleInstance *)exec_env->module_inst;
  5505. uint64 offset = (p - 1) >= module_inst->module->load_addr
  5506. ? (p - 1) - module_inst->module->load_addr
  5507. : ~0;
  5508. if (debug_instance) {
  5509. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  5510. orignal_opcode, &size)
  5511. && size == 1) {
  5512. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  5513. "with %02x: ",
  5514. orignal_opcode[0]);
  5515. opcode = orignal_opcode[0];
  5516. goto op_break_retry;
  5517. }
  5518. }
  5519. break;
  5520. }
  5521. #endif
  5522. default:
  5523. return false;
  5524. }
  5525. }
  5526. (void)u8;
  5527. (void)exec_env;
  5528. return false;
  5529. fail:
  5530. return false;
  5531. }
  5532. #if WASM_ENABLE_FAST_INTERP != 0
  5533. #if WASM_DEBUG_PREPROCESSOR != 0
  5534. #define LOG_OP(...) os_printf(__VA_ARGS__)
  5535. #else
  5536. #define LOG_OP(...) (void)0
  5537. #endif
  5538. #define PATCH_ELSE 0
  5539. #define PATCH_END 1
  5540. typedef struct BranchBlockPatch {
  5541. struct BranchBlockPatch *next;
  5542. uint8 patch_type;
  5543. uint8 *code_compiled;
  5544. } BranchBlockPatch;
  5545. #endif
  5546. typedef struct BranchBlock {
  5547. uint8 label_type;
  5548. BlockType block_type;
  5549. uint8 *start_addr;
  5550. uint8 *else_addr;
  5551. uint8 *end_addr;
  5552. uint32 stack_cell_num;
  5553. #if WASM_ENABLE_GC != 0
  5554. uint32 reftype_map_num;
  5555. /* Indicate which local is used inside current block, used to validate
  5556. * local.get with non-nullable ref types */
  5557. uint8 *local_use_mask;
  5558. uint32 local_use_mask_size;
  5559. #endif
  5560. #if WASM_ENABLE_FAST_INTERP != 0
  5561. uint16 dynamic_offset;
  5562. uint8 *code_compiled;
  5563. BranchBlockPatch *patch_list;
  5564. /* This is used to save params frame_offset of of if block */
  5565. int16 *param_frame_offsets;
  5566. #endif
  5567. /* Indicate the operand stack is in polymorphic state.
  5568. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  5569. * to polymorphic state until the block's 'end' opcode is processed.
  5570. * If stack is in polymorphic state and stack is empty, instruction can
  5571. * pop any type of value directly without decreasing stack top pointer
  5572. * and stack cell num. */
  5573. bool is_stack_polymorphic;
  5574. } BranchBlock;
  5575. typedef struct WASMLoaderContext {
  5576. /* frame ref stack */
  5577. uint8 *frame_ref;
  5578. uint8 *frame_ref_bottom;
  5579. uint8 *frame_ref_boundary;
  5580. uint32 frame_ref_size;
  5581. uint32 stack_cell_num;
  5582. uint32 max_stack_cell_num;
  5583. #if WASM_ENABLE_GC != 0
  5584. /* frame reftype map stack */
  5585. WASMRefTypeMap *frame_reftype_map;
  5586. WASMRefTypeMap *frame_reftype_map_bottom;
  5587. WASMRefTypeMap *frame_reftype_map_boundary;
  5588. uint32 frame_reftype_map_size;
  5589. uint32 reftype_map_num;
  5590. uint32 max_reftype_map_num;
  5591. /* Current module */
  5592. WASMModule *module;
  5593. /* Current module's ref_type_set */
  5594. HashMap *ref_type_set;
  5595. /* Always point to local variable ref_type of
  5596. wasm_loader_prepare_bytecode */
  5597. WASMRefType *ref_type_tmp;
  5598. #endif
  5599. /* frame csp stack */
  5600. BranchBlock *frame_csp;
  5601. BranchBlock *frame_csp_bottom;
  5602. BranchBlock *frame_csp_boundary;
  5603. uint32 frame_csp_size;
  5604. uint32 csp_num;
  5605. uint32 max_csp_num;
  5606. #if WASM_ENABLE_FAST_INTERP != 0
  5607. /* frame offset stack */
  5608. int16 *frame_offset;
  5609. int16 *frame_offset_bottom;
  5610. int16 *frame_offset_boundary;
  5611. uint32 frame_offset_size;
  5612. int16 dynamic_offset;
  5613. int16 start_dynamic_offset;
  5614. int16 max_dynamic_offset;
  5615. /* preserved local offset */
  5616. int16 preserved_local_offset;
  5617. /* const buffer */
  5618. uint8 *const_buf;
  5619. uint16 num_const;
  5620. uint16 const_cell_num;
  5621. uint32 const_buf_size;
  5622. /* processed code */
  5623. uint8 *p_code_compiled;
  5624. uint8 *p_code_compiled_end;
  5625. uint32 code_compiled_size;
  5626. /* If the last opcode will be dropped, the peak memory usage will be larger
  5627. * than the final code_compiled_size, we record the peak size to ensure
  5628. * there will not be invalid memory access during second traverse */
  5629. uint32 code_compiled_peak_size;
  5630. #endif
  5631. } WASMLoaderContext;
  5632. typedef struct Const {
  5633. WASMValue value;
  5634. uint16 slot_index;
  5635. uint8 value_type;
  5636. } Const;
  5637. static void *
  5638. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  5639. uint32 error_buf_size)
  5640. {
  5641. uint8 *mem_new;
  5642. bh_assert(size_new > size_old);
  5643. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  5644. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  5645. memset(mem_new + size_old, 0, size_new - size_old);
  5646. wasm_runtime_free(mem_old);
  5647. }
  5648. return mem_new;
  5649. }
  5650. #define MEM_REALLOC(mem, size_old, size_new) \
  5651. do { \
  5652. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  5653. error_buf_size); \
  5654. if (!mem_new) \
  5655. goto fail; \
  5656. mem = mem_new; \
  5657. } while (0)
  5658. #define CHECK_CSP_PUSH() \
  5659. do { \
  5660. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  5661. MEM_REALLOC( \
  5662. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  5663. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  5664. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  5665. ctx->frame_csp_boundary = \
  5666. ctx->frame_csp_bottom \
  5667. + ctx->frame_csp_size / sizeof(BranchBlock); \
  5668. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  5669. } \
  5670. } while (0)
  5671. #define CHECK_CSP_POP() \
  5672. do { \
  5673. if (ctx->csp_num < 1) { \
  5674. set_error_buf(error_buf, error_buf_size, \
  5675. "type mismatch: " \
  5676. "expect data but block stack was empty"); \
  5677. goto fail; \
  5678. } \
  5679. } while (0)
  5680. #if WASM_ENABLE_FAST_INTERP != 0
  5681. static bool
  5682. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  5683. uint32 error_buf_size)
  5684. {
  5685. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  5686. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  5687. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  5688. ctx->frame_offset_size + 16);
  5689. ctx->frame_offset_size += 16;
  5690. ctx->frame_offset_boundary =
  5691. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  5692. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  5693. }
  5694. return true;
  5695. fail:
  5696. return false;
  5697. }
  5698. static bool
  5699. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  5700. {
  5701. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  5702. return false;
  5703. return true;
  5704. }
  5705. static void
  5706. free_label_patch_list(BranchBlock *frame_csp)
  5707. {
  5708. BranchBlockPatch *label_patch = frame_csp->patch_list;
  5709. BranchBlockPatch *next;
  5710. while (label_patch != NULL) {
  5711. next = label_patch->next;
  5712. wasm_runtime_free(label_patch);
  5713. label_patch = next;
  5714. }
  5715. frame_csp->patch_list = NULL;
  5716. }
  5717. static void
  5718. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  5719. {
  5720. BranchBlock *tmp_csp = frame_csp;
  5721. for (uint32 i = 0; i < csp_num; i++) {
  5722. free_label_patch_list(tmp_csp);
  5723. tmp_csp++;
  5724. }
  5725. }
  5726. #endif /* end of WASM_ENABLE_FAST_INTERP */
  5727. #if WASM_ENABLE_GC != 0
  5728. static bool
  5729. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  5730. char *error_buf, uint32 error_buf_size)
  5731. {
  5732. BranchBlock *current_csp = ctx->frame_csp - 1;
  5733. uint32 local_mask_size;
  5734. if (local_count == 0) {
  5735. current_csp->local_use_mask_size = 0;
  5736. return true;
  5737. }
  5738. if (current_csp->local_use_mask) {
  5739. memset(current_csp->local_use_mask, 0,
  5740. current_csp->local_use_mask_size);
  5741. return true;
  5742. }
  5743. local_mask_size = (local_count + 7) / sizeof(uint8);
  5744. if (!(current_csp->local_use_mask =
  5745. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  5746. return false;
  5747. }
  5748. current_csp->local_use_mask_size = local_mask_size;
  5749. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  5750. /* For non-function blocks, inherit the use status from parent block */
  5751. BranchBlock *parent_csp = current_csp - 1;
  5752. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  5753. bh_assert(parent_csp->local_use_mask);
  5754. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  5755. parent_csp->local_use_mask, local_mask_size);
  5756. }
  5757. return true;
  5758. }
  5759. static void
  5760. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  5761. {
  5762. BranchBlock *current_csp = ctx->frame_csp - 1;
  5763. bh_assert(current_csp->local_use_mask
  5764. || current_csp->local_use_mask_size == 0);
  5765. if (current_csp->local_use_mask) {
  5766. wasm_runtime_free(current_csp->local_use_mask);
  5767. }
  5768. current_csp->local_use_mask = NULL;
  5769. current_csp->local_use_mask_size = 0;
  5770. }
  5771. static void
  5772. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  5773. {
  5774. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  5775. uint32 i;
  5776. for (i = 0; i < ctx->csp_num; i++) {
  5777. if (tmp_csp->local_use_mask) {
  5778. wasm_runtime_free(tmp_csp->local_use_mask);
  5779. tmp_csp->local_use_mask = NULL;
  5780. tmp_csp->local_use_mask_size = 0;
  5781. }
  5782. tmp_csp++;
  5783. }
  5784. }
  5785. static void
  5786. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  5787. {
  5788. BranchBlock *current_csp = ctx->frame_csp - 1;
  5789. uint32 byte_offset = index / sizeof(uint8);
  5790. uint32 bit_offset = index % sizeof(uint8);
  5791. bh_assert(byte_offset < current_csp->local_use_mask_size);
  5792. bh_assert(current_csp->local_use_mask);
  5793. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  5794. }
  5795. static bool
  5796. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  5797. {
  5798. BranchBlock *current_csp = ctx->frame_csp - 1;
  5799. uint32 byte_offset = index / sizeof(uint8);
  5800. uint32 bit_offset = index % sizeof(uint8);
  5801. bh_assert(byte_offset < current_csp->local_use_mask_size);
  5802. bh_assert(current_csp->local_use_mask);
  5803. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  5804. ? true
  5805. : false;
  5806. }
  5807. #endif /* end of WASM_ENABLE_GC != 0 */
  5808. static void
  5809. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  5810. {
  5811. if (ctx) {
  5812. if (ctx->frame_ref_bottom)
  5813. wasm_runtime_free(ctx->frame_ref_bottom);
  5814. #if WASM_ENABLE_GC != 0
  5815. if (ctx->frame_reftype_map_bottom)
  5816. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  5817. #endif
  5818. if (ctx->frame_csp_bottom) {
  5819. #if WASM_ENABLE_FAST_INTERP != 0
  5820. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  5821. #endif
  5822. #if WASM_ENABLE_GC != 0
  5823. wasm_loader_clean_all_local_use_masks(ctx);
  5824. #endif
  5825. wasm_runtime_free(ctx->frame_csp_bottom);
  5826. }
  5827. #if WASM_ENABLE_FAST_INTERP != 0
  5828. if (ctx->frame_offset_bottom)
  5829. wasm_runtime_free(ctx->frame_offset_bottom);
  5830. if (ctx->const_buf)
  5831. wasm_runtime_free(ctx->const_buf);
  5832. #endif
  5833. wasm_runtime_free(ctx);
  5834. }
  5835. }
  5836. static WASMLoaderContext *
  5837. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  5838. {
  5839. WASMLoaderContext *loader_ctx =
  5840. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  5841. if (!loader_ctx)
  5842. return NULL;
  5843. loader_ctx->frame_ref_size = 32;
  5844. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  5845. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  5846. goto fail;
  5847. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  5848. #if WASM_ENABLE_GC != 0
  5849. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  5850. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  5851. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  5852. error_buf_size)))
  5853. goto fail;
  5854. loader_ctx->frame_reftype_map_boundary =
  5855. loader_ctx->frame_reftype_map_bottom + 16;
  5856. #endif
  5857. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  5858. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  5859. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  5860. goto fail;
  5861. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  5862. #if WASM_ENABLE_FAST_INTERP != 0
  5863. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  5864. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  5865. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  5866. error_buf_size)))
  5867. goto fail;
  5868. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  5869. loader_ctx->num_const = 0;
  5870. loader_ctx->const_buf_size = sizeof(Const) * 8;
  5871. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  5872. error_buf, error_buf_size)))
  5873. goto fail;
  5874. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  5875. set_error_buf(error_buf, error_buf_size,
  5876. "fast interpreter offset overflow");
  5877. goto fail;
  5878. }
  5879. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  5880. loader_ctx->max_dynamic_offset =
  5881. func->param_cell_num + func->local_cell_num;
  5882. #endif
  5883. return loader_ctx;
  5884. fail:
  5885. wasm_loader_ctx_destroy(loader_ctx);
  5886. return NULL;
  5887. }
  5888. static bool
  5889. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  5890. uint32 error_buf_size)
  5891. {
  5892. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  5893. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  5894. /* Increase the frame ref stack */
  5895. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  5896. ctx->frame_ref_size + 16);
  5897. ctx->frame_ref_size += 16;
  5898. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  5899. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  5900. }
  5901. #if WASM_ENABLE_GC != 0
  5902. if (wasm_is_type_multi_byte_type(type)
  5903. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  5904. /* Increase the frame reftype map stack */
  5905. bh_assert(
  5906. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  5907. * sizeof(WASMRefTypeMap))
  5908. == ctx->frame_reftype_map_size);
  5909. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  5910. ctx->frame_reftype_map_size
  5911. + (uint32)sizeof(WASMRefTypeMap) * 8);
  5912. ctx->frame_reftype_map =
  5913. ctx->frame_reftype_map_bottom
  5914. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  5915. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  5916. ctx->frame_reftype_map_boundary =
  5917. ctx->frame_reftype_map_bottom
  5918. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  5919. }
  5920. #endif
  5921. return true;
  5922. fail:
  5923. return false;
  5924. }
  5925. static bool
  5926. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  5927. uint32 error_buf_size)
  5928. {
  5929. uint32 type_cell_num = wasm_value_type_cell_num(type);
  5930. uint32 i;
  5931. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  5932. return false;
  5933. #if WASM_ENABLE_GC != 0
  5934. if (wasm_is_type_multi_byte_type(type)) {
  5935. WASMRefType *ref_type;
  5936. if (!(ref_type =
  5937. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  5938. error_buf, error_buf_size))) {
  5939. return false;
  5940. }
  5941. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  5942. /* Increase the frame reftype map stack */
  5943. bh_assert((uint32)((ctx->frame_reftype_map
  5944. - ctx->frame_reftype_map_bottom)
  5945. * sizeof(WASMRefTypeMap))
  5946. == ctx->frame_reftype_map_size);
  5947. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  5948. ctx->frame_reftype_map_size,
  5949. ctx->frame_reftype_map_size
  5950. + (uint32)sizeof(WASMRefTypeMap) * 8);
  5951. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  5952. + ctx->frame_reftype_map_size
  5953. / ((uint32)sizeof(WASMRefTypeMap));
  5954. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  5955. ctx->frame_reftype_map_boundary =
  5956. ctx->frame_reftype_map_bottom
  5957. + ctx->frame_reftype_map_size
  5958. / ((uint32)sizeof(WASMRefTypeMap));
  5959. }
  5960. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  5961. ctx->frame_reftype_map->ref_type = ref_type;
  5962. ctx->frame_reftype_map++;
  5963. ctx->reftype_map_num++;
  5964. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  5965. ctx->max_reftype_map_num = ctx->reftype_map_num;
  5966. }
  5967. #endif
  5968. for (i = 0; i < type_cell_num; i++)
  5969. *ctx->frame_ref++ = type;
  5970. ctx->stack_cell_num += type_cell_num;
  5971. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  5972. ctx->max_stack_cell_num = ctx->stack_cell_num;
  5973. if (ctx->max_stack_cell_num > UINT16_MAX) {
  5974. set_error_buf(error_buf, error_buf_size,
  5975. "operand stack depth limit exceeded");
  5976. return false;
  5977. }
  5978. }
  5979. return true;
  5980. #if WASM_ENABLE_GC != 0
  5981. fail:
  5982. return false;
  5983. #endif
  5984. }
  5985. static bool
  5986. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  5987. int32 stack_cell_num,
  5988. #if WASM_ENABLE_GC != 0
  5989. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  5990. #endif
  5991. uint8 type,
  5992. #if WASM_ENABLE_GC != 0
  5993. WASMRefType *ref_type,
  5994. #endif
  5995. char *error_buf, uint32 error_buf_size)
  5996. {
  5997. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  5998. #if WASM_ENABLE_GC != 0
  5999. WASMRefType *frame_reftype = NULL;
  6000. #endif
  6001. if (stack_cell_num < type_cell_num) {
  6002. set_error_buf(error_buf, error_buf_size,
  6003. "type mismatch: expect data but stack was empty");
  6004. return false;
  6005. }
  6006. #if WASM_ENABLE_GC == 0
  6007. for (i = 0; i < type_cell_num; i++) {
  6008. if (*(frame_ref - 1 - i) != type) {
  6009. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  6010. "type mismatch: expect ", type2str(type),
  6011. " but got other");
  6012. return false;
  6013. }
  6014. }
  6015. #else
  6016. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  6017. bh_assert(reftype_map_num > 0);
  6018. frame_reftype = (frame_reftype_map - 1)->ref_type;
  6019. }
  6020. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  6021. ref_type, ctx->module->types,
  6022. ctx->module->type_count)) {
  6023. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  6024. "type mismatch: expect ", type2str(type),
  6025. " but got other");
  6026. return false;
  6027. }
  6028. for (i = 0; i < type_cell_num - 1; i++) {
  6029. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  6030. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  6031. "type mismatch: expect ", type2str(type),
  6032. " but got other");
  6033. return false;
  6034. }
  6035. }
  6036. #endif
  6037. return true;
  6038. }
  6039. static bool
  6040. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  6041. uint32 error_buf_size)
  6042. {
  6043. int32 block_stack_cell_num =
  6044. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  6045. #if WASM_ENABLE_GC != 0
  6046. int32 reftype_map_num =
  6047. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  6048. #endif
  6049. if (block_stack_cell_num > 0) {
  6050. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  6051. /* the stack top is a value of any type, return success */
  6052. return true;
  6053. }
  6054. #if WASM_ENABLE_GC != 0
  6055. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  6056. uint8 stack_top_type = *(ctx->frame_ref - 1);
  6057. WASMRefType *stack_top_ref_type = NULL;
  6058. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  6059. bh_assert(reftype_map_num > 0);
  6060. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  6061. }
  6062. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  6063. ctx->ref_type_tmp, ctx->module->types,
  6064. ctx->module->type_count)) {
  6065. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  6066. uint32 ref_type_struct_size =
  6067. wasm_reftype_struct_size(stack_top_ref_type);
  6068. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  6069. stack_top_ref_type, ref_type_struct_size);
  6070. }
  6071. return true;
  6072. }
  6073. }
  6074. #endif
  6075. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  6076. #if WASM_ENABLE_GC != 0
  6077. ctx->frame_reftype_map, reftype_map_num,
  6078. #endif
  6079. type,
  6080. #if WASM_ENABLE_GC != 0
  6081. ctx->ref_type_tmp,
  6082. #endif
  6083. error_buf, error_buf_size)) {
  6084. return false;
  6085. }
  6086. return true;
  6087. }
  6088. static bool
  6089. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  6090. uint32 error_buf_size)
  6091. {
  6092. BranchBlock *cur_block = ctx->frame_csp - 1;
  6093. int32 available_stack_cell =
  6094. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  6095. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  6096. /* Directly return success if current block is in stack
  6097. polymorphic state while stack is empty. */
  6098. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  6099. return true;
  6100. if (type == VALUE_TYPE_VOID)
  6101. return true;
  6102. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  6103. return false;
  6104. bh_assert(available_stack_cell > 0);
  6105. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  6106. type = VALUE_TYPE_ANY;
  6107. cell_num_to_pop = 1;
  6108. }
  6109. ctx->frame_ref -= cell_num_to_pop;
  6110. ctx->stack_cell_num -= cell_num_to_pop;
  6111. #if WASM_ENABLE_GC != 0
  6112. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  6113. ctx->frame_reftype_map--;
  6114. ctx->reftype_map_num--;
  6115. }
  6116. #endif
  6117. return true;
  6118. }
  6119. #if WASM_ENABLE_GC != 0
  6120. /* Get the stack top element of current block */
  6121. static bool
  6122. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  6123. WASMRefType **p_ref_type, char *error_buf,
  6124. uint32 error_buf_size)
  6125. {
  6126. BranchBlock *cur_block = ctx->frame_csp - 1;
  6127. int32 available_stack_cell =
  6128. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  6129. if (available_stack_cell <= 0) {
  6130. /* Directly return success if current block is in stack
  6131. polymorphic state while stack is empty. */
  6132. if (cur_block->is_stack_polymorphic) {
  6133. *p_type = VALUE_TYPE_ANY;
  6134. return true;
  6135. }
  6136. else {
  6137. set_error_buf(
  6138. error_buf, error_buf_size,
  6139. "type mismatch: expect data but block stack was empty");
  6140. return false;
  6141. }
  6142. }
  6143. *p_type = *(ctx->frame_ref - 1);
  6144. if (wasm_is_type_multi_byte_type(*p_type)) {
  6145. int32 available_reftype_map =
  6146. (int32)(ctx->reftype_map_num
  6147. - (ctx->frame_csp - 1)->reftype_map_num);
  6148. bh_assert(available_reftype_map > 0);
  6149. (void)available_reftype_map;
  6150. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  6151. }
  6152. return true;
  6153. }
  6154. #if WASM_ENABLE_FAST_INTERP != 0
  6155. static bool
  6156. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  6157. char *error_buf, uint32 error_buf_size);
  6158. #endif
  6159. /* Check whether the stack top elem is a heap object, and if yes,
  6160. pop and return it */
  6161. static bool
  6162. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  6163. WASMRefType *ref_ht_ret, char *error_buf,
  6164. uint32 error_buf_size)
  6165. {
  6166. uint8 type = 0;
  6167. WASMRefType *ref_type = NULL;
  6168. /* Get stack top element */
  6169. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  6170. error_buf_size)) {
  6171. return false;
  6172. }
  6173. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  6174. /* stack top isn't a ref type */
  6175. && !wasm_is_type_reftype(type)) {
  6176. set_error_buf(error_buf, error_buf_size,
  6177. "type mismatch: expect heap object but got others");
  6178. return false;
  6179. }
  6180. /* POP stack top */
  6181. if (wasm_is_type_multi_byte_type(type)) {
  6182. bh_assert(ref_type);
  6183. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  6184. wasm_reftype_struct_size(ref_type));
  6185. }
  6186. #if WASM_ENABLE_FAST_INTERP != 0
  6187. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  6188. error_buf_size)) {
  6189. return false;
  6190. }
  6191. #else
  6192. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  6193. return false;
  6194. }
  6195. #endif
  6196. if (p_type)
  6197. *p_type = type;
  6198. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  6199. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  6200. wasm_reftype_struct_size(ref_type));
  6201. }
  6202. return true;
  6203. }
  6204. /* Check whether the stack top elem is subtype of (ref null ht),
  6205. and if yes, pop it and return the converted (ref ht) */
  6206. static bool
  6207. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  6208. WASMRefType *ref_ht_ret, char *error_buf,
  6209. uint32 error_buf_size)
  6210. {
  6211. uint8 type = 0;
  6212. WASMRefType ref_type = { 0 };
  6213. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  6214. error_buf_size)) {
  6215. return false;
  6216. }
  6217. /* Convert to related (ref ht) and return */
  6218. if ((type >= REF_TYPE_EQREF && type <= REF_TYPE_FUNCREF)
  6219. || (type >= REF_TYPE_NULLREF && type <= REF_TYPE_I31REF)) {
  6220. /* Return (ref func/extern/any/eq/i31/nofunc/noextern/struct/array/none)
  6221. */
  6222. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  6223. HEAP_TYPE_FUNC + (type - REF_TYPE_FUNCREF));
  6224. type = ref_ht_ret->ref_type;
  6225. }
  6226. else if (wasm_is_reftype_htref_nullable(type)
  6227. || wasm_is_reftype_htref_non_nullable(type)) {
  6228. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  6229. wasm_reftype_struct_size(&ref_type));
  6230. /* Convert to (ref ht) */
  6231. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  6232. ref_ht_ret->ref_ht_common.nullable = false;
  6233. type = ref_ht_ret->ref_type;
  6234. }
  6235. *p_type = type;
  6236. return true;
  6237. }
  6238. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  6239. and if yes, pop it and return the type_idx */
  6240. static bool
  6241. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  6242. uint32 *p_type_idx, char *error_buf,
  6243. uint32 error_buf_size)
  6244. {
  6245. uint8 type = 0;
  6246. int32 type_idx = -1;
  6247. WASMRefType *ref_type = NULL;
  6248. /* Get stack top element */
  6249. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  6250. error_buf_size)) {
  6251. return false;
  6252. }
  6253. if (type != VALUE_TYPE_ANY) {
  6254. /* stack top isn't (ref null $t) */
  6255. if (!((wasm_is_reftype_htref_nullable(type)
  6256. || wasm_is_reftype_htref_non_nullable(type))
  6257. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  6258. set_error_buf(error_buf, error_buf_size,
  6259. "type mismatch: expect (ref null $t) but got others");
  6260. return false;
  6261. }
  6262. type_idx = ref_type->ref_ht_typeidx.type_idx;
  6263. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  6264. wasm_reftype_struct_size(ref_type));
  6265. }
  6266. /* POP stack top */
  6267. #if WASM_ENABLE_FAST_INTERP != 0
  6268. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  6269. error_buf_size)) {
  6270. return false;
  6271. }
  6272. #else
  6273. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  6274. return false;
  6275. }
  6276. #endif
  6277. /* Convert to type_idx and return */
  6278. *p_type = type;
  6279. if (type != VALUE_TYPE_ANY)
  6280. *p_type_idx = (uint32)type_idx;
  6281. return true;
  6282. }
  6283. #endif /* WASM_ENABLE_GC != 0 */
  6284. #if WASM_ENABLE_FAST_INTERP == 0
  6285. static bool
  6286. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  6287. uint8 type_push, uint8 type_pop, char *error_buf,
  6288. uint32 error_buf_size)
  6289. {
  6290. for (int i = 0; i < pop_cnt; i++) {
  6291. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  6292. error_buf_size))
  6293. return false;
  6294. }
  6295. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  6296. return false;
  6297. return true;
  6298. }
  6299. #endif
  6300. static bool
  6301. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  6302. BlockType block_type, uint8 *start_addr,
  6303. char *error_buf, uint32 error_buf_size)
  6304. {
  6305. CHECK_CSP_PUSH();
  6306. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  6307. ctx->frame_csp->label_type = label_type;
  6308. ctx->frame_csp->block_type = block_type;
  6309. ctx->frame_csp->start_addr = start_addr;
  6310. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  6311. #if WASM_ENABLE_GC != 0
  6312. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  6313. #endif
  6314. #if WASM_ENABLE_FAST_INTERP != 0
  6315. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  6316. ctx->frame_csp->patch_list = NULL;
  6317. #endif
  6318. ctx->frame_csp++;
  6319. ctx->csp_num++;
  6320. if (ctx->csp_num > ctx->max_csp_num) {
  6321. ctx->max_csp_num = ctx->csp_num;
  6322. if (ctx->max_csp_num > UINT16_MAX) {
  6323. set_error_buf(error_buf, error_buf_size,
  6324. "label stack depth limit exceeded");
  6325. return false;
  6326. }
  6327. }
  6328. return true;
  6329. fail:
  6330. return false;
  6331. }
  6332. static bool
  6333. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  6334. uint32 error_buf_size)
  6335. {
  6336. CHECK_CSP_POP();
  6337. #if WASM_ENABLE_FAST_INTERP != 0
  6338. if ((ctx->frame_csp - 1)->param_frame_offsets)
  6339. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  6340. #endif
  6341. ctx->frame_csp--;
  6342. ctx->csp_num--;
  6343. return true;
  6344. fail:
  6345. return false;
  6346. }
  6347. #if WASM_ENABLE_FAST_INTERP != 0
  6348. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  6349. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  6350. #define emit_label(opcode) \
  6351. do { \
  6352. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  6353. LOG_OP("\nemit_op [%02x]\t", opcode); \
  6354. } while (0)
  6355. #define skip_label() \
  6356. do { \
  6357. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  6358. LOG_OP("\ndelete last op\n"); \
  6359. } while (0)
  6360. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6361. #define emit_label(opcode) \
  6362. do { \
  6363. int32 offset = \
  6364. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  6365. if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \
  6366. set_error_buf(error_buf, error_buf_size, \
  6367. "pre-compiled label offset out of range"); \
  6368. goto fail; \
  6369. } \
  6370. wasm_loader_emit_int16(loader_ctx, offset); \
  6371. LOG_OP("\nemit_op [%02x]\t", opcode); \
  6372. } while (0)
  6373. #define skip_label() \
  6374. do { \
  6375. wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \
  6376. LOG_OP("\ndelete last op\n"); \
  6377. } while (0)
  6378. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6379. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  6380. #define emit_label(opcode) \
  6381. do { \
  6382. wasm_loader_emit_uint8(loader_ctx, opcode); \
  6383. LOG_OP("\nemit_op [%02x]\t", opcode); \
  6384. } while (0)
  6385. #define skip_label() \
  6386. do { \
  6387. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  6388. LOG_OP("\ndelete last op\n"); \
  6389. } while (0)
  6390. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  6391. #define emit_empty_label_addr_and_frame_ip(type) \
  6392. do { \
  6393. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  6394. loader_ctx->p_code_compiled, error_buf, \
  6395. error_buf_size)) \
  6396. goto fail; \
  6397. /* label address, to be patched */ \
  6398. wasm_loader_emit_ptr(loader_ctx, NULL); \
  6399. } while (0)
  6400. #define emit_br_info(frame_csp) \
  6401. do { \
  6402. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \
  6403. error_buf_size)) \
  6404. goto fail; \
  6405. } while (0)
  6406. #define LAST_OP_OUTPUT_I32() \
  6407. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  6408. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  6409. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  6410. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  6411. || (last_op >= WASM_OP_I32_WRAP_I64 \
  6412. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  6413. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  6414. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  6415. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  6416. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  6417. || (last_op == EXT_OP_COPY_STACK_TOP)
  6418. #define LAST_OP_OUTPUT_I64() \
  6419. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  6420. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  6421. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  6422. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  6423. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  6424. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  6425. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  6426. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  6427. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  6428. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  6429. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  6430. #define GET_CONST_OFFSET(type, val) \
  6431. do { \
  6432. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  6433. &operand_offset, error_buf, \
  6434. error_buf_size))) \
  6435. goto fail; \
  6436. } while (0)
  6437. #define GET_CONST_F32_OFFSET(type, fval) \
  6438. do { \
  6439. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  6440. &operand_offset, error_buf, \
  6441. error_buf_size))) \
  6442. goto fail; \
  6443. } while (0)
  6444. #define GET_CONST_F64_OFFSET(type, fval) \
  6445. do { \
  6446. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  6447. &operand_offset, error_buf, \
  6448. error_buf_size))) \
  6449. goto fail; \
  6450. } while (0)
  6451. #define emit_operand(ctx, offset) \
  6452. do { \
  6453. wasm_loader_emit_int16(ctx, offset); \
  6454. LOG_OP("%d\t", offset); \
  6455. } while (0)
  6456. #define emit_byte(ctx, byte) \
  6457. do { \
  6458. wasm_loader_emit_uint8(ctx, byte); \
  6459. LOG_OP("%d\t", byte); \
  6460. } while (0)
  6461. #define emit_uint32(ctx, value) \
  6462. do { \
  6463. wasm_loader_emit_uint32(ctx, value); \
  6464. LOG_OP("%d\t", value); \
  6465. } while (0)
  6466. #define emit_uint64(ctx, value) \
  6467. do { \
  6468. wasm_loader_emit_const(ctx, &value, false); \
  6469. LOG_OP("%lld\t", value); \
  6470. } while (0)
  6471. #define emit_float32(ctx, value) \
  6472. do { \
  6473. wasm_loader_emit_const(ctx, &value, true); \
  6474. LOG_OP("%f\t", value); \
  6475. } while (0)
  6476. #define emit_float64(ctx, value) \
  6477. do { \
  6478. wasm_loader_emit_const(ctx, &value, false); \
  6479. LOG_OP("%f\t", value); \
  6480. } while (0)
  6481. static bool
  6482. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  6483. {
  6484. if (!(ctx->p_code_compiled =
  6485. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  6486. return false;
  6487. ctx->p_code_compiled_end =
  6488. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  6489. /* clean up frame ref */
  6490. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  6491. ctx->frame_ref = ctx->frame_ref_bottom;
  6492. ctx->stack_cell_num = 0;
  6493. #if WASM_ENABLE_GC != 0
  6494. /* clean up reftype map */
  6495. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  6496. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  6497. ctx->reftype_map_num = 0;
  6498. #endif
  6499. /* clean up frame csp */
  6500. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  6501. ctx->frame_csp = ctx->frame_csp_bottom;
  6502. ctx->csp_num = 0;
  6503. ctx->max_csp_num = 0;
  6504. /* clean up frame offset */
  6505. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  6506. ctx->frame_offset = ctx->frame_offset_bottom;
  6507. ctx->dynamic_offset = ctx->start_dynamic_offset;
  6508. /* init preserved local offsets */
  6509. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  6510. /* const buf is reserved */
  6511. return true;
  6512. }
  6513. static void
  6514. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  6515. {
  6516. ctx->code_compiled_size += size;
  6517. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  6518. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  6519. }
  6520. }
  6521. static void
  6522. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  6523. {
  6524. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  6525. if (ctx->p_code_compiled) {
  6526. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6527. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6528. #endif
  6529. bh_memcpy_s(ctx->p_code_compiled,
  6530. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  6531. value, size);
  6532. ctx->p_code_compiled += size;
  6533. }
  6534. else {
  6535. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6536. bh_assert((ctx->code_compiled_size & 1) == 0);
  6537. #endif
  6538. increase_compiled_code_space(ctx, size);
  6539. }
  6540. }
  6541. static void
  6542. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  6543. {
  6544. if (ctx->p_code_compiled) {
  6545. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6546. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6547. #endif
  6548. STORE_U32(ctx->p_code_compiled, value);
  6549. ctx->p_code_compiled += sizeof(uint32);
  6550. }
  6551. else {
  6552. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6553. bh_assert((ctx->code_compiled_size & 1) == 0);
  6554. #endif
  6555. increase_compiled_code_space(ctx, sizeof(uint32));
  6556. }
  6557. }
  6558. static void
  6559. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  6560. {
  6561. if (ctx->p_code_compiled) {
  6562. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6563. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6564. #endif
  6565. STORE_U16(ctx->p_code_compiled, (uint16)value);
  6566. ctx->p_code_compiled += sizeof(int16);
  6567. }
  6568. else {
  6569. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6570. bh_assert((ctx->code_compiled_size & 1) == 0);
  6571. #endif
  6572. increase_compiled_code_space(ctx, sizeof(uint16));
  6573. }
  6574. }
  6575. static void
  6576. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  6577. {
  6578. if (ctx->p_code_compiled) {
  6579. *(ctx->p_code_compiled) = value;
  6580. ctx->p_code_compiled += sizeof(uint8);
  6581. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6582. ctx->p_code_compiled++;
  6583. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6584. #endif
  6585. }
  6586. else {
  6587. increase_compiled_code_space(ctx, sizeof(uint8));
  6588. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6589. increase_compiled_code_space(ctx, sizeof(uint8));
  6590. bh_assert((ctx->code_compiled_size & 1) == 0);
  6591. #endif
  6592. }
  6593. }
  6594. static void
  6595. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  6596. {
  6597. if (ctx->p_code_compiled) {
  6598. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6599. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6600. #endif
  6601. STORE_PTR(ctx->p_code_compiled, value);
  6602. ctx->p_code_compiled += sizeof(void *);
  6603. }
  6604. else {
  6605. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6606. bh_assert((ctx->code_compiled_size & 1) == 0);
  6607. #endif
  6608. increase_compiled_code_space(ctx, sizeof(void *));
  6609. }
  6610. }
  6611. static void
  6612. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  6613. {
  6614. if (ctx->p_code_compiled) {
  6615. ctx->p_code_compiled -= size;
  6616. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6617. if (size == sizeof(uint8)) {
  6618. ctx->p_code_compiled--;
  6619. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6620. }
  6621. #endif
  6622. }
  6623. else {
  6624. ctx->code_compiled_size -= size;
  6625. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6626. if (size == sizeof(uint8)) {
  6627. ctx->code_compiled_size--;
  6628. bh_assert((ctx->code_compiled_size & 1) == 0);
  6629. }
  6630. #endif
  6631. }
  6632. }
  6633. static bool
  6634. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  6635. uint32 local_index, uint32 local_type,
  6636. bool *preserved, char *error_buf,
  6637. uint32 error_buf_size)
  6638. {
  6639. uint32 i = 0;
  6640. int16 preserved_offset = (int16)local_index;
  6641. *preserved = false;
  6642. while (i < loader_ctx->stack_cell_num) {
  6643. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  6644. /* move previous local into dynamic space before a set/tee_local opcode
  6645. */
  6646. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  6647. if (!(*preserved)) {
  6648. *preserved = true;
  6649. skip_label();
  6650. preserved_offset = loader_ctx->preserved_local_offset;
  6651. if (loader_ctx->p_code_compiled) {
  6652. bh_assert(preserved_offset != (int16)local_index);
  6653. }
  6654. if (is_32bit_type(local_type)) {
  6655. /* Only increase preserve offset in the second traversal */
  6656. if (loader_ctx->p_code_compiled)
  6657. loader_ctx->preserved_local_offset++;
  6658. emit_label(EXT_OP_COPY_STACK_TOP);
  6659. }
  6660. else {
  6661. if (loader_ctx->p_code_compiled)
  6662. loader_ctx->preserved_local_offset += 2;
  6663. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  6664. }
  6665. emit_operand(loader_ctx, local_index);
  6666. emit_operand(loader_ctx, preserved_offset);
  6667. emit_label(opcode);
  6668. }
  6669. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  6670. }
  6671. if (is_32bit_type(cur_type))
  6672. i++;
  6673. else
  6674. i += 2;
  6675. }
  6676. (void)error_buf;
  6677. (void)error_buf_size;
  6678. return true;
  6679. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  6680. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6681. fail:
  6682. return false;
  6683. #endif
  6684. #endif
  6685. }
  6686. static bool
  6687. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  6688. char *error_buf, uint32 error_buf_size)
  6689. {
  6690. uint32 i = 0;
  6691. bool preserve_local;
  6692. /* preserve locals before blocks to ensure that "tee/set_local" inside
  6693. blocks will not influence the value of these locals */
  6694. while (i < loader_ctx->stack_cell_num) {
  6695. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  6696. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  6697. if ((cur_offset < loader_ctx->start_dynamic_offset)
  6698. && (cur_offset >= 0)) {
  6699. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  6700. cur_type, &preserve_local,
  6701. error_buf, error_buf_size)))
  6702. return false;
  6703. }
  6704. if (is_32bit_type(cur_type)) {
  6705. i++;
  6706. }
  6707. else {
  6708. i += 2;
  6709. }
  6710. }
  6711. return true;
  6712. }
  6713. static bool
  6714. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  6715. uint8 *p_code_compiled, char *error_buf,
  6716. uint32 error_buf_size)
  6717. {
  6718. BranchBlockPatch *patch =
  6719. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  6720. if (!patch) {
  6721. return false;
  6722. }
  6723. patch->patch_type = patch_type;
  6724. patch->code_compiled = p_code_compiled;
  6725. if (!frame_csp->patch_list) {
  6726. frame_csp->patch_list = patch;
  6727. patch->next = NULL;
  6728. }
  6729. else {
  6730. patch->next = frame_csp->patch_list;
  6731. frame_csp->patch_list = patch;
  6732. }
  6733. return true;
  6734. }
  6735. static void
  6736. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  6737. {
  6738. BranchBlock *frame_csp = ctx->frame_csp - depth;
  6739. BranchBlockPatch *node = frame_csp->patch_list;
  6740. BranchBlockPatch *node_prev = NULL, *node_next;
  6741. if (!ctx->p_code_compiled)
  6742. return;
  6743. while (node) {
  6744. node_next = node->next;
  6745. if (node->patch_type == patch_type) {
  6746. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  6747. if (node_prev == NULL) {
  6748. frame_csp->patch_list = node_next;
  6749. }
  6750. else {
  6751. node_prev->next = node_next;
  6752. }
  6753. wasm_runtime_free(node);
  6754. }
  6755. else {
  6756. node_prev = node;
  6757. }
  6758. node = node_next;
  6759. }
  6760. }
  6761. static bool
  6762. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  6763. char *error_buf, uint32 error_buf_size)
  6764. {
  6765. /* br info layout:
  6766. * a) arity of target block
  6767. * b) total cell num of arity values
  6768. * c) each arity value's cell num
  6769. * d) each arity value's src frame offset
  6770. * e) each arity values's dst dynamic offset
  6771. * f) branch target address
  6772. *
  6773. * Note: b-e are omitted when arity is 0 so that
  6774. * interpreter can recover the br info quickly.
  6775. */
  6776. BlockType *block_type = &frame_csp->block_type;
  6777. uint8 *types = NULL, cell;
  6778. #if WASM_ENABLE_GC != 0
  6779. WASMRefTypeMap *reftype_maps;
  6780. uint32 reftype_map_count;
  6781. #endif
  6782. uint32 arity = 0;
  6783. int32 i;
  6784. int16 *frame_offset = ctx->frame_offset;
  6785. uint16 dynamic_offset;
  6786. /* Note: loop's arity is different from if and block. loop's arity is
  6787. * its parameter count while if and block arity is result count.
  6788. */
  6789. #if WASM_ENABLE_GC == 0
  6790. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  6791. arity = block_type_get_param_types(block_type, &types);
  6792. else
  6793. arity = block_type_get_result_types(block_type, &types);
  6794. #else
  6795. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  6796. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  6797. &reftype_map_count);
  6798. else
  6799. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  6800. &reftype_map_count);
  6801. #endif
  6802. /* Part a */
  6803. emit_uint32(ctx, arity);
  6804. if (arity) {
  6805. /* Part b */
  6806. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  6807. /* Part c */
  6808. for (i = (int32)arity - 1; i >= 0; i--) {
  6809. cell = (uint8)wasm_value_type_cell_num(types[i]);
  6810. emit_byte(ctx, cell);
  6811. }
  6812. /* Part d */
  6813. for (i = (int32)arity - 1; i >= 0; i--) {
  6814. cell = (uint8)wasm_value_type_cell_num(types[i]);
  6815. frame_offset -= cell;
  6816. emit_operand(ctx, *(int16 *)(frame_offset));
  6817. }
  6818. /* Part e */
  6819. dynamic_offset =
  6820. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  6821. for (i = (int32)arity - 1; i >= 0; i--) {
  6822. cell = (uint8)wasm_value_type_cell_num(types[i]);
  6823. dynamic_offset -= cell;
  6824. emit_operand(ctx, dynamic_offset);
  6825. }
  6826. }
  6827. /* Part f */
  6828. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  6829. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  6830. }
  6831. else {
  6832. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  6833. error_buf, error_buf_size))
  6834. return false;
  6835. /* label address, to be patched */
  6836. wasm_loader_emit_ptr(ctx, NULL);
  6837. }
  6838. return true;
  6839. }
  6840. static bool
  6841. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  6842. bool disable_emit, int16 operand_offset,
  6843. char *error_buf, uint32 error_buf_size)
  6844. {
  6845. if (type == VALUE_TYPE_VOID)
  6846. return true;
  6847. /* only check memory overflow in first traverse */
  6848. if (ctx->p_code_compiled == NULL) {
  6849. if (!check_offset_push(ctx, error_buf, error_buf_size))
  6850. return false;
  6851. }
  6852. if (disable_emit)
  6853. *(ctx->frame_offset)++ = operand_offset;
  6854. else {
  6855. emit_operand(ctx, ctx->dynamic_offset);
  6856. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  6857. ctx->dynamic_offset++;
  6858. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  6859. ctx->max_dynamic_offset = ctx->dynamic_offset;
  6860. if (ctx->max_dynamic_offset >= INT16_MAX) {
  6861. goto fail;
  6862. }
  6863. }
  6864. }
  6865. if (is_32bit_type(type))
  6866. return true;
  6867. if (ctx->p_code_compiled == NULL) {
  6868. if (!check_offset_push(ctx, error_buf, error_buf_size))
  6869. return false;
  6870. }
  6871. ctx->frame_offset++;
  6872. if (!disable_emit) {
  6873. ctx->dynamic_offset++;
  6874. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  6875. ctx->max_dynamic_offset = ctx->dynamic_offset;
  6876. if (ctx->max_dynamic_offset >= INT16_MAX) {
  6877. goto fail;
  6878. }
  6879. }
  6880. }
  6881. return true;
  6882. fail:
  6883. set_error_buf(error_buf, error_buf_size,
  6884. "fast interpreter offset overflow");
  6885. return false;
  6886. }
  6887. /* This function should be in front of wasm_loader_pop_frame_ref
  6888. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  6889. will be modified by wasm_loader_pop_frame_ref */
  6890. static bool
  6891. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  6892. char *error_buf, uint32 error_buf_size)
  6893. {
  6894. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  6895. then current block is the function block */
  6896. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  6897. BranchBlock *cur_block = ctx->frame_csp - depth;
  6898. int32 available_stack_cell =
  6899. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  6900. /* Directly return success if current block is in stack
  6901. * polymorphic state while stack is empty. */
  6902. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  6903. return true;
  6904. if (type == VALUE_TYPE_VOID)
  6905. return true;
  6906. if (is_32bit_type(type)) {
  6907. /* Check the offset stack bottom to ensure the frame offset
  6908. stack will not go underflow. But we don't thrown error
  6909. and return true here, because the error msg should be
  6910. given in wasm_loader_pop_frame_ref */
  6911. if (!check_offset_pop(ctx, 1))
  6912. return true;
  6913. ctx->frame_offset -= 1;
  6914. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  6915. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  6916. ctx->dynamic_offset -= 1;
  6917. }
  6918. else {
  6919. if (!check_offset_pop(ctx, 2))
  6920. return true;
  6921. ctx->frame_offset -= 2;
  6922. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  6923. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  6924. ctx->dynamic_offset -= 2;
  6925. }
  6926. emit_operand(ctx, *(ctx->frame_offset));
  6927. (void)error_buf;
  6928. (void)error_buf_size;
  6929. return true;
  6930. }
  6931. static bool
  6932. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  6933. bool disable_emit, int16 operand_offset,
  6934. char *error_buf, uint32 error_buf_size)
  6935. {
  6936. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  6937. error_buf, error_buf_size)))
  6938. return false;
  6939. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  6940. return false;
  6941. return true;
  6942. }
  6943. static bool
  6944. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  6945. char *error_buf, uint32 error_buf_size)
  6946. {
  6947. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  6948. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  6949. return false;
  6950. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  6951. return false;
  6952. return true;
  6953. }
  6954. static bool
  6955. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  6956. uint8 type_push, uint8 type_pop,
  6957. bool disable_emit, int16 operand_offset,
  6958. char *error_buf, uint32 error_buf_size)
  6959. {
  6960. uint8 i;
  6961. for (i = 0; i < pop_cnt; i++) {
  6962. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  6963. error_buf_size))
  6964. return false;
  6965. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  6966. error_buf_size))
  6967. return false;
  6968. }
  6969. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  6970. operand_offset, error_buf,
  6971. error_buf_size))
  6972. return false;
  6973. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  6974. return false;
  6975. return true;
  6976. }
  6977. static bool
  6978. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  6979. int16 *offset, char *error_buf,
  6980. uint32 error_buf_size)
  6981. {
  6982. int8 bytes_to_increase;
  6983. int16 operand_offset = 0;
  6984. Const *c;
  6985. /* Search existing constant */
  6986. for (c = (Const *)ctx->const_buf;
  6987. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  6988. /* TODO: handle v128 type? */
  6989. if ((type == c->value_type)
  6990. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  6991. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  6992. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  6993. || (type == VALUE_TYPE_FUNCREF
  6994. && *(int32 *)value == c->value.i32)
  6995. || (type == VALUE_TYPE_EXTERNREF
  6996. && *(int32 *)value == c->value.i32)
  6997. #endif
  6998. || (type == VALUE_TYPE_F64
  6999. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  7000. || (type == VALUE_TYPE_F32
  7001. && (0
  7002. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  7003. operand_offset = c->slot_index;
  7004. break;
  7005. }
  7006. if (is_32bit_type(c->value_type))
  7007. operand_offset += 1;
  7008. else
  7009. operand_offset += 2;
  7010. }
  7011. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  7012. /* New constant, append to the const buffer */
  7013. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  7014. bytes_to_increase = 2;
  7015. }
  7016. else {
  7017. bytes_to_increase = 1;
  7018. }
  7019. /* The max cell num of const buffer is 32768 since the valid index range
  7020. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  7021. * full */
  7022. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  7023. *offset = 0;
  7024. return true;
  7025. }
  7026. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  7027. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  7028. ctx->const_buf_size + 4 * sizeof(Const));
  7029. ctx->const_buf_size += 4 * sizeof(Const);
  7030. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  7031. }
  7032. c->value_type = type;
  7033. switch (type) {
  7034. case VALUE_TYPE_F64:
  7035. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  7036. sizeof(float64));
  7037. ctx->const_cell_num += 2;
  7038. /* The const buf will be reversed, we use the second cell */
  7039. /* of the i64/f64 const so the finnal offset is corrent */
  7040. operand_offset++;
  7041. break;
  7042. case VALUE_TYPE_I64:
  7043. c->value.i64 = *(int64 *)value;
  7044. ctx->const_cell_num += 2;
  7045. operand_offset++;
  7046. break;
  7047. case VALUE_TYPE_F32:
  7048. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  7049. sizeof(float32));
  7050. ctx->const_cell_num++;
  7051. break;
  7052. case VALUE_TYPE_I32:
  7053. c->value.i32 = *(int32 *)value;
  7054. ctx->const_cell_num++;
  7055. break;
  7056. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  7057. case VALUE_TYPE_EXTERNREF:
  7058. case VALUE_TYPE_FUNCREF:
  7059. c->value.i32 = *(int32 *)value;
  7060. ctx->const_cell_num++;
  7061. break;
  7062. #endif
  7063. default:
  7064. break;
  7065. }
  7066. c->slot_index = operand_offset;
  7067. ctx->num_const++;
  7068. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  7069. (int64)c->value.i64);
  7070. }
  7071. /* use negetive index for const */
  7072. operand_offset = -(operand_offset + 1);
  7073. *offset = operand_offset;
  7074. return true;
  7075. fail:
  7076. return false;
  7077. }
  7078. /*
  7079. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  7080. -- Mostly used for the binary / compare operation
  7081. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  7082. -- Mostly used in block / control instructions
  7083. The POP will always emit the offset on the top of the frame_offset stack
  7084. PUSH can be used in two ways:
  7085. 1. directly PUSH:
  7086. PUSH_XXX();
  7087. will allocate a dynamic space and emit
  7088. 2. silent PUSH:
  7089. operand_offset = xxx; disable_emit = true;
  7090. PUSH_XXX();
  7091. only push the frame_offset stack, no emit
  7092. */
  7093. #define TEMPLATE_PUSH(Type) \
  7094. do { \
  7095. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  7096. disable_emit, operand_offset, \
  7097. error_buf, error_buf_size)) \
  7098. goto fail; \
  7099. } while (0)
  7100. #define TEMPLATE_PUSH_REF(Type) \
  7101. do { \
  7102. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  7103. operand_offset, error_buf, \
  7104. error_buf_size)) \
  7105. goto fail; \
  7106. } while (0)
  7107. #define TEMPLATE_POP(Type) \
  7108. do { \
  7109. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  7110. error_buf, error_buf_size)) \
  7111. goto fail; \
  7112. } while (0)
  7113. #define TEMPLATE_POP_REF(Type) \
  7114. do { \
  7115. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  7116. error_buf_size)) \
  7117. goto fail; \
  7118. } while (0)
  7119. #define PUSH_OFFSET_TYPE(type) \
  7120. do { \
  7121. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  7122. operand_offset, error_buf, \
  7123. error_buf_size))) \
  7124. goto fail; \
  7125. } while (0)
  7126. #define POP_OFFSET_TYPE(type) \
  7127. do { \
  7128. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  7129. error_buf_size))) \
  7130. goto fail; \
  7131. } while (0)
  7132. #define POP_AND_PUSH(type_pop, type_push) \
  7133. do { \
  7134. if (!(wasm_loader_push_pop_frame_ref_offset( \
  7135. loader_ctx, 1, type_push, type_pop, disable_emit, \
  7136. operand_offset, error_buf, error_buf_size))) \
  7137. goto fail; \
  7138. } while (0)
  7139. /* type of POPs should be the same */
  7140. #define POP2_AND_PUSH(type_pop, type_push) \
  7141. do { \
  7142. if (!(wasm_loader_push_pop_frame_ref_offset( \
  7143. loader_ctx, 2, type_push, type_pop, disable_emit, \
  7144. operand_offset, error_buf, error_buf_size))) \
  7145. goto fail; \
  7146. } while (0)
  7147. #else /* WASM_ENABLE_FAST_INTERP */
  7148. #define TEMPLATE_PUSH(Type) \
  7149. do { \
  7150. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  7151. error_buf, error_buf_size))) \
  7152. goto fail; \
  7153. } while (0)
  7154. #define TEMPLATE_PUSH_REF(Type) \
  7155. do { \
  7156. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  7157. error_buf_size))) \
  7158. goto fail; \
  7159. } while (0)
  7160. #define TEMPLATE_POP(Type) \
  7161. do { \
  7162. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  7163. error_buf, error_buf_size))) \
  7164. goto fail; \
  7165. } while (0)
  7166. #define TEMPLATE_POP_REF(Type) \
  7167. do { \
  7168. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  7169. error_buf_size))) \
  7170. goto fail; \
  7171. } while (0)
  7172. #define POP_AND_PUSH(type_pop, type_push) \
  7173. do { \
  7174. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  7175. type_pop, error_buf, \
  7176. error_buf_size))) \
  7177. goto fail; \
  7178. } while (0)
  7179. /* type of POPs should be the same */
  7180. #define POP2_AND_PUSH(type_pop, type_push) \
  7181. do { \
  7182. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  7183. type_pop, error_buf, \
  7184. error_buf_size))) \
  7185. goto fail; \
  7186. } while (0)
  7187. #endif /* WASM_ENABLE_FAST_INTERP */
  7188. #define PUSH_I32() TEMPLATE_PUSH(I32)
  7189. #define PUSH_F32() TEMPLATE_PUSH(F32)
  7190. #define PUSH_I64() TEMPLATE_PUSH(I64)
  7191. #define PUSH_F64() TEMPLATE_PUSH(F64)
  7192. #define PUSH_V128() TEMPLATE_PUSH(V128)
  7193. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  7194. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  7195. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  7196. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  7197. #define POP_I32() TEMPLATE_POP(I32)
  7198. #define POP_F32() TEMPLATE_POP(F32)
  7199. #define POP_I64() TEMPLATE_POP(I64)
  7200. #define POP_F64() TEMPLATE_POP(F64)
  7201. #define POP_V128() TEMPLATE_POP(V128)
  7202. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  7203. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  7204. #if WASM_ENABLE_FAST_INTERP != 0
  7205. static bool
  7206. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  7207. bool disable_emit, char *error_buf, uint32 error_buf_size)
  7208. {
  7209. int16 operand_offset = 0;
  7210. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  7211. : loader_ctx->frame_csp;
  7212. BlockType *block_type = &block->block_type;
  7213. uint8 *return_types = NULL;
  7214. #if WASM_ENABLE_GC != 0
  7215. WASMRefTypeMap *reftype_maps = NULL;
  7216. uint32 reftype_map_count;
  7217. #endif
  7218. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  7219. int32 i = 0;
  7220. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  7221. *frame_offset_org = NULL;
  7222. #if WASM_ENABLE_GC == 0
  7223. return_count = block_type_get_result_types(block_type, &return_types);
  7224. #else
  7225. return_count = block_type_get_result_types(
  7226. block_type, &return_types, &reftype_maps, &reftype_map_count);
  7227. #endif
  7228. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  7229. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  7230. if (return_count == 1) {
  7231. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  7232. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  7233. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  7234. /* insert op_copy before else opcode */
  7235. if (opcode == WASM_OP_ELSE)
  7236. skip_label();
  7237. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  7238. : EXT_OP_COPY_STACK_TOP_I64);
  7239. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  7240. emit_operand(loader_ctx, block->dynamic_offset);
  7241. if (opcode == WASM_OP_ELSE) {
  7242. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  7243. }
  7244. else {
  7245. loader_ctx->frame_offset -= cell;
  7246. loader_ctx->dynamic_offset = block->dynamic_offset;
  7247. PUSH_OFFSET_TYPE(return_types[0]);
  7248. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  7249. }
  7250. if (opcode == WASM_OP_ELSE)
  7251. emit_label(opcode);
  7252. }
  7253. return true;
  7254. }
  7255. /* Copy stack top values to block's results which are in dynamic space.
  7256. * The instruction format:
  7257. * Part a: values count
  7258. * Part b: all values total cell num
  7259. * Part c: each value's cell_num, src offset and dst offset
  7260. * Part d: each value's src offset and dst offset
  7261. * Part e: each value's dst offset
  7262. */
  7263. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  7264. dynamic_offset = dynamic_offset_org =
  7265. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  7266. /* First traversal to get the count of values needed to be copied. */
  7267. for (i = (int32)return_count - 1; i >= 0; i--) {
  7268. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  7269. frame_offset -= cells;
  7270. dynamic_offset -= cells;
  7271. if (dynamic_offset != *frame_offset) {
  7272. value_count++;
  7273. total_cel_num += cells;
  7274. }
  7275. }
  7276. if (value_count) {
  7277. uint32 j = 0;
  7278. uint8 *emit_data = NULL, *cells = NULL;
  7279. int16 *src_offsets = NULL;
  7280. uint16 *dst_offsets = NULL;
  7281. uint64 size =
  7282. (uint64)value_count
  7283. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  7284. /* Allocate memory for the emit data */
  7285. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  7286. return false;
  7287. cells = emit_data;
  7288. src_offsets = (int16 *)(cells + value_count);
  7289. dst_offsets = (uint16 *)(src_offsets + value_count);
  7290. /* insert op_copy before else opcode */
  7291. if (opcode == WASM_OP_ELSE)
  7292. skip_label();
  7293. emit_label(EXT_OP_COPY_STACK_VALUES);
  7294. /* Part a) */
  7295. emit_uint32(loader_ctx, value_count);
  7296. /* Part b) */
  7297. emit_uint32(loader_ctx, total_cel_num);
  7298. /* Second traversal to get each value's cell num, src offset and dst
  7299. * offset. */
  7300. frame_offset = frame_offset_org;
  7301. dynamic_offset = dynamic_offset_org;
  7302. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  7303. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  7304. frame_offset -= cell;
  7305. dynamic_offset -= cell;
  7306. if (dynamic_offset != *frame_offset) {
  7307. /* cell num */
  7308. cells[j] = cell;
  7309. /* src offset */
  7310. src_offsets[j] = *frame_offset;
  7311. /* dst offset */
  7312. dst_offsets[j] = dynamic_offset;
  7313. j++;
  7314. }
  7315. if (opcode == WASM_OP_ELSE) {
  7316. *frame_offset = dynamic_offset;
  7317. }
  7318. else {
  7319. loader_ctx->frame_offset = frame_offset;
  7320. loader_ctx->dynamic_offset = dynamic_offset;
  7321. PUSH_OFFSET_TYPE(return_types[i]);
  7322. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  7323. loader_ctx->frame_offset = frame_offset_org;
  7324. loader_ctx->dynamic_offset = dynamic_offset_org;
  7325. }
  7326. }
  7327. bh_assert(j == value_count);
  7328. /* Emit the cells, src_offsets and dst_offsets */
  7329. for (j = 0; j < value_count; j++)
  7330. emit_byte(loader_ctx, cells[j]);
  7331. for (j = 0; j < value_count; j++)
  7332. emit_operand(loader_ctx, src_offsets[j]);
  7333. for (j = 0; j < value_count; j++)
  7334. emit_operand(loader_ctx, dst_offsets[j]);
  7335. if (opcode == WASM_OP_ELSE)
  7336. emit_label(opcode);
  7337. wasm_runtime_free(emit_data);
  7338. }
  7339. return true;
  7340. fail:
  7341. return false;
  7342. }
  7343. #endif /* WASM_ENABLE_FAST_INTERP */
  7344. #define RESERVE_BLOCK_RET() \
  7345. do { \
  7346. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  7347. error_buf_size)) \
  7348. goto fail; \
  7349. } while (0)
  7350. #define PUSH_TYPE(type) \
  7351. do { \
  7352. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  7353. error_buf_size))) \
  7354. goto fail; \
  7355. } while (0)
  7356. #define POP_TYPE(type) \
  7357. do { \
  7358. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  7359. error_buf_size))) \
  7360. goto fail; \
  7361. } while (0)
  7362. #if WASM_ENABLE_GC == 0
  7363. #define PUSH_CSP(label_type, block_type, _start_addr) \
  7364. do { \
  7365. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  7366. _start_addr, error_buf, \
  7367. error_buf_size)) \
  7368. goto fail; \
  7369. } while (0)
  7370. #define POP_CSP() \
  7371. do { \
  7372. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  7373. goto fail; \
  7374. } while (0)
  7375. #else
  7376. #define PUSH_CSP(label_type, block_type, _start_addr) \
  7377. do { \
  7378. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  7379. _start_addr, error_buf, \
  7380. error_buf_size)) \
  7381. goto fail; \
  7382. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  7383. error_buf, error_buf_size)) { \
  7384. goto fail; \
  7385. } \
  7386. } while (0)
  7387. #define POP_CSP() \
  7388. do { \
  7389. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  7390. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  7391. goto fail; \
  7392. } while (0)
  7393. #endif /* end of WASM_ENABLE_GC == 0 */
  7394. #if WASM_ENABLE_GC == 0
  7395. #define GET_LOCAL_REFTYPE() (void)0
  7396. #else
  7397. #define GET_LOCAL_REFTYPE() \
  7398. do { \
  7399. if (wasm_is_type_multi_byte_type(local_type)) { \
  7400. WASMRefType *_ref_type; \
  7401. if (local_idx < param_count) \
  7402. _ref_type = wasm_reftype_map_find( \
  7403. param_reftype_maps, param_reftype_map_count, local_idx); \
  7404. else \
  7405. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  7406. local_reftype_map_count, \
  7407. local_idx - param_count); \
  7408. bh_assert(_ref_type); \
  7409. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  7410. wasm_reftype_struct_size(_ref_type)); \
  7411. } \
  7412. } while (0)
  7413. #endif
  7414. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  7415. do { \
  7416. read_leb_uint32(p, p_end, local_idx); \
  7417. if (local_idx >= param_count + local_count) { \
  7418. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  7419. goto fail; \
  7420. } \
  7421. local_type = local_idx < param_count \
  7422. ? param_types[local_idx] \
  7423. : local_types[local_idx - param_count]; \
  7424. local_offset = local_offsets[local_idx]; \
  7425. GET_LOCAL_REFTYPE(); \
  7426. } while (0)
  7427. #define CHECK_BR(depth) \
  7428. do { \
  7429. if (!wasm_loader_check_br(loader_ctx, depth, error_buf, \
  7430. error_buf_size)) \
  7431. goto fail; \
  7432. } while (0)
  7433. static bool
  7434. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  7435. {
  7436. if (module->memory_count == 0 && module->import_memory_count == 0) {
  7437. set_error_buf(error_buf, error_buf_size, "unknown memory");
  7438. return false;
  7439. }
  7440. return true;
  7441. }
  7442. #define CHECK_MEMORY() \
  7443. do { \
  7444. if (!check_memory(module, error_buf, error_buf_size)) \
  7445. goto fail; \
  7446. } while (0)
  7447. static bool
  7448. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  7449. uint32 error_buf_size)
  7450. {
  7451. uint8 mem_access_aligns[] = {
  7452. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  7453. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  7454. };
  7455. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  7456. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  7457. set_error_buf(error_buf, error_buf_size,
  7458. "alignment must not be larger than natural");
  7459. return false;
  7460. }
  7461. return true;
  7462. }
  7463. #if WASM_ENABLE_SIMD != 0
  7464. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  7465. static bool
  7466. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  7467. uint32 error_buf_size)
  7468. {
  7469. uint8 mem_access_aligns[] = {
  7470. 4, /* load */
  7471. 3, 3, 3, 3, 3, 3, /* load and extend */
  7472. 0, 1, 2, 3, /* load and splat */
  7473. 4, /* store */
  7474. };
  7475. uint8 mem_access_aligns_load_lane[] = {
  7476. 0, 1, 2, 3, /* load lane */
  7477. 0, 1, 2, 3, /* store lane */
  7478. 2, 3 /* store zero */
  7479. };
  7480. if (!((opcode <= SIMD_v128_store)
  7481. || (SIMD_v128_load8_lane <= opcode
  7482. && opcode <= SIMD_v128_load64_zero))) {
  7483. set_error_buf(error_buf, error_buf_size,
  7484. "the opcode doesn't include memarg");
  7485. return false;
  7486. }
  7487. if ((opcode <= SIMD_v128_store
  7488. && align > mem_access_aligns[opcode - SIMD_v128_load])
  7489. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  7490. && align > mem_access_aligns_load_lane[opcode
  7491. - SIMD_v128_load8_lane])) {
  7492. set_error_buf(error_buf, error_buf_size,
  7493. "alignment must not be larger than natural");
  7494. return false;
  7495. }
  7496. return true;
  7497. }
  7498. static bool
  7499. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  7500. uint32 error_buf_size)
  7501. {
  7502. switch (opcode) {
  7503. case SIMD_i8x16_extract_lane_s:
  7504. case SIMD_i8x16_extract_lane_u:
  7505. case SIMD_i8x16_replace_lane:
  7506. if (lane >= 16) {
  7507. goto fail;
  7508. }
  7509. break;
  7510. case SIMD_i16x8_extract_lane_s:
  7511. case SIMD_i16x8_extract_lane_u:
  7512. case SIMD_i16x8_replace_lane:
  7513. if (lane >= 8) {
  7514. goto fail;
  7515. }
  7516. break;
  7517. case SIMD_i32x4_extract_lane:
  7518. case SIMD_i32x4_replace_lane:
  7519. case SIMD_f32x4_extract_lane:
  7520. case SIMD_f32x4_replace_lane:
  7521. if (lane >= 4) {
  7522. goto fail;
  7523. }
  7524. break;
  7525. case SIMD_i64x2_extract_lane:
  7526. case SIMD_i64x2_replace_lane:
  7527. case SIMD_f64x2_extract_lane:
  7528. case SIMD_f64x2_replace_lane:
  7529. if (lane >= 2) {
  7530. goto fail;
  7531. }
  7532. break;
  7533. case SIMD_v128_load8_lane:
  7534. case SIMD_v128_load16_lane:
  7535. case SIMD_v128_load32_lane:
  7536. case SIMD_v128_load64_lane:
  7537. case SIMD_v128_store8_lane:
  7538. case SIMD_v128_store16_lane:
  7539. case SIMD_v128_store32_lane:
  7540. case SIMD_v128_store64_lane:
  7541. case SIMD_v128_load32_zero:
  7542. case SIMD_v128_load64_zero:
  7543. {
  7544. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  7545. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  7546. goto fail;
  7547. }
  7548. break;
  7549. }
  7550. default:
  7551. goto fail;
  7552. }
  7553. return true;
  7554. fail:
  7555. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  7556. return false;
  7557. }
  7558. static bool
  7559. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  7560. {
  7561. uint8 i;
  7562. for (i = 0; i != 16; ++i) {
  7563. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  7564. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  7565. return false;
  7566. }
  7567. }
  7568. return true;
  7569. }
  7570. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  7571. #endif /* end of WASM_ENABLE_SIMD */
  7572. #if WASM_ENABLE_SHARED_MEMORY != 0
  7573. static bool
  7574. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  7575. uint32 error_buf_size)
  7576. {
  7577. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  7578. uint8 mem_access_aligns[] = {
  7579. 2, 3, 0, 1, 0, 1, 2,
  7580. };
  7581. uint8 expect;
  7582. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  7583. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  7584. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  7585. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  7586. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  7587. }
  7588. else {
  7589. /* 7 opcodes in every group */
  7590. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  7591. }
  7592. if (align != expect) {
  7593. set_error_buf(error_buf, error_buf_size,
  7594. "alignment isn't equal to natural");
  7595. return false;
  7596. }
  7597. return true;
  7598. }
  7599. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  7600. static bool
  7601. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
  7602. bool is_br_table, char *error_buf, uint32 error_buf_size)
  7603. {
  7604. BranchBlock *target_block, *cur_block;
  7605. BlockType *target_block_type;
  7606. uint8 type, *types = NULL, *frame_ref;
  7607. uint32 arity = 0;
  7608. int32 i, available_stack_cell;
  7609. uint16 cell_num;
  7610. #if WASM_ENABLE_GC != 0
  7611. WASMRefTypeMap *frame_reftype_map;
  7612. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  7613. WASMRefType *ref_type;
  7614. uint32 reftype_map_count = 0;
  7615. int32 available_reftype_map;
  7616. bool is_type_multi_byte;
  7617. #endif
  7618. if (loader_ctx->csp_num < depth + 1) {
  7619. set_error_buf(error_buf, error_buf_size,
  7620. "unknown label, "
  7621. "unexpected end of section or function");
  7622. return false;
  7623. }
  7624. cur_block = loader_ctx->frame_csp - 1;
  7625. target_block = loader_ctx->frame_csp - (depth + 1);
  7626. target_block_type = &target_block->block_type;
  7627. frame_ref = loader_ctx->frame_ref;
  7628. #if WASM_ENABLE_GC != 0
  7629. frame_reftype_map = loader_ctx->frame_reftype_map;
  7630. #endif
  7631. /* Note: loop's arity is different from if and block. loop's arity is
  7632. * its parameter count while if and block arity is result count.
  7633. */
  7634. #if WASM_ENABLE_GC == 0
  7635. if (target_block->label_type == LABEL_TYPE_LOOP)
  7636. arity = block_type_get_param_types(target_block_type, &types);
  7637. else
  7638. arity = block_type_get_result_types(target_block_type, &types);
  7639. #else
  7640. if (target_block->label_type == LABEL_TYPE_LOOP)
  7641. arity = block_type_get_param_types(target_block_type, &types,
  7642. &reftype_maps, &reftype_map_count);
  7643. else
  7644. arity = block_type_get_result_types(target_block_type, &types,
  7645. &reftype_maps, &reftype_map_count);
  7646. #endif
  7647. /* If the stack is in polymorphic state, just clear the stack
  7648. * and then re-push the values to make the stack top values
  7649. * match block type. */
  7650. if (cur_block->is_stack_polymorphic && !is_br_table) {
  7651. #if WASM_ENABLE_GC != 0
  7652. int32 j = reftype_map_count - 1;
  7653. #endif
  7654. for (i = (int32)arity - 1; i >= 0; i--) {
  7655. #if WASM_ENABLE_GC != 0
  7656. if (wasm_is_type_multi_byte_type(types[i])) {
  7657. bh_assert(reftype_maps[j].index == i);
  7658. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  7659. reftype_maps[j].ref_type,
  7660. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  7661. j--;
  7662. }
  7663. #endif
  7664. #if WASM_ENABLE_FAST_INTERP != 0
  7665. POP_OFFSET_TYPE(types[i]);
  7666. #endif
  7667. POP_TYPE(types[i]);
  7668. }
  7669. #if WASM_ENABLE_GC != 0
  7670. j = 0;
  7671. #endif
  7672. for (i = 0; i < (int32)arity; i++) {
  7673. #if WASM_ENABLE_GC != 0
  7674. if (wasm_is_type_multi_byte_type(types[i])) {
  7675. bh_assert(reftype_maps[j].index == i);
  7676. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  7677. reftype_maps[j].ref_type,
  7678. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  7679. j++;
  7680. }
  7681. #endif
  7682. #if WASM_ENABLE_FAST_INTERP != 0
  7683. bool disable_emit = true;
  7684. int16 operand_offset = 0;
  7685. PUSH_OFFSET_TYPE(types[i]);
  7686. #endif
  7687. PUSH_TYPE(types[i]);
  7688. }
  7689. return true;
  7690. }
  7691. available_stack_cell =
  7692. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  7693. #if WASM_ENABLE_GC != 0
  7694. available_reftype_map =
  7695. (int32)(loader_ctx->reftype_map_num
  7696. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  7697. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  7698. #endif
  7699. /* Check stack top values match target block type */
  7700. for (i = (int32)arity - 1; i >= 0; i--) {
  7701. type = types[i];
  7702. #if WASM_ENABLE_GC != 0
  7703. ref_type = NULL;
  7704. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  7705. if (is_type_multi_byte) {
  7706. bh_assert(reftype_map);
  7707. ref_type = reftype_map->ref_type;
  7708. }
  7709. #endif
  7710. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7711. break;
  7712. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  7713. #if WASM_ENABLE_GC != 0
  7714. frame_reftype_map, available_reftype_map,
  7715. #endif
  7716. type,
  7717. #if WASM_ENABLE_GC != 0
  7718. ref_type,
  7719. #endif
  7720. error_buf, error_buf_size)) {
  7721. return false;
  7722. }
  7723. cell_num = wasm_value_type_cell_num(types[i]);
  7724. frame_ref -= cell_num;
  7725. available_stack_cell -= cell_num;
  7726. #if WASM_ENABLE_GC != 0
  7727. if (is_type_multi_byte) {
  7728. frame_reftype_map--;
  7729. available_reftype_map--;
  7730. reftype_map--;
  7731. }
  7732. #endif
  7733. }
  7734. return true;
  7735. fail:
  7736. return false;
  7737. }
  7738. static BranchBlock *
  7739. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  7740. bool is_br_table, char *error_buf, uint32 error_buf_size)
  7741. {
  7742. uint8 *p = *p_buf, *p_end = buf_end;
  7743. BranchBlock *frame_csp_tmp;
  7744. uint32 depth;
  7745. read_leb_uint32(p, p_end, depth);
  7746. if (!wasm_loader_check_br(loader_ctx, depth, is_br_table, error_buf,
  7747. error_buf_size)) {
  7748. goto fail;
  7749. }
  7750. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  7751. #if WASM_ENABLE_FAST_INTERP != 0
  7752. emit_br_info(frame_csp_tmp);
  7753. #endif
  7754. *p_buf = p;
  7755. return frame_csp_tmp;
  7756. fail:
  7757. return NULL;
  7758. }
  7759. static bool
  7760. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  7761. char *error_buf, uint32 error_buf_size)
  7762. {
  7763. BlockType *block_type = &block->block_type;
  7764. uint8 *return_types = NULL;
  7765. uint32 return_count = 0;
  7766. int32 available_stack_cell, return_cell_num, i;
  7767. uint8 *frame_ref = NULL;
  7768. #if WASM_ENABLE_GC != 0
  7769. WASMRefTypeMap *frame_reftype_map;
  7770. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  7771. WASMRefType *ref_type;
  7772. uint32 param_count, return_reftype_map_count = 0;
  7773. int32 available_reftype_map =
  7774. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  7775. #endif
  7776. available_stack_cell =
  7777. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  7778. #if WASM_ENABLE_GC == 0
  7779. return_count = block_type_get_result_types(block_type, &return_types);
  7780. #else
  7781. return_count = block_type_get_result_types(block_type, &return_types,
  7782. &return_reftype_maps,
  7783. &return_reftype_map_count);
  7784. param_count =
  7785. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  7786. (void)param_count;
  7787. #endif
  7788. return_cell_num =
  7789. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  7790. /* If the stack is in polymorphic state, just clear the stack
  7791. * and then re-push the values to make the stack top values
  7792. * match block type. */
  7793. if (block->is_stack_polymorphic) {
  7794. #if WASM_ENABLE_GC != 0
  7795. int32 j = return_reftype_map_count - 1;
  7796. #endif
  7797. for (i = (int32)return_count - 1; i >= 0; i--) {
  7798. #if WASM_ENABLE_GC != 0
  7799. if (wasm_is_type_multi_byte_type(return_types[i])) {
  7800. bh_assert(return_reftype_maps[j].index == i + param_count);
  7801. bh_memcpy_s(
  7802. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  7803. return_reftype_maps[j].ref_type,
  7804. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  7805. j--;
  7806. }
  7807. #endif
  7808. #if WASM_ENABLE_FAST_INTERP != 0
  7809. POP_OFFSET_TYPE(return_types[i]);
  7810. #endif
  7811. POP_TYPE(return_types[i]);
  7812. }
  7813. /* Check stack is empty */
  7814. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  7815. set_error_buf(
  7816. error_buf, error_buf_size,
  7817. "type mismatch: stack size does not match block type");
  7818. goto fail;
  7819. }
  7820. #if WASM_ENABLE_GC != 0
  7821. j = 0;
  7822. #endif
  7823. for (i = 0; i < (int32)return_count; i++) {
  7824. #if WASM_ENABLE_GC != 0
  7825. if (wasm_is_type_multi_byte_type(return_types[i])) {
  7826. bh_assert(return_reftype_maps[j].index == i + param_count);
  7827. bh_memcpy_s(
  7828. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  7829. return_reftype_maps[j].ref_type,
  7830. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  7831. j++;
  7832. }
  7833. #endif
  7834. #if WASM_ENABLE_FAST_INTERP != 0
  7835. bool disable_emit = true;
  7836. int16 operand_offset = 0;
  7837. PUSH_OFFSET_TYPE(return_types[i]);
  7838. #endif
  7839. PUSH_TYPE(return_types[i]);
  7840. }
  7841. return true;
  7842. }
  7843. /* Check stack cell num equals return cell num */
  7844. if (available_stack_cell != return_cell_num) {
  7845. set_error_buf(error_buf, error_buf_size,
  7846. "type mismatch: stack size does not match block type");
  7847. goto fail;
  7848. }
  7849. /* Check stack values match return types */
  7850. frame_ref = loader_ctx->frame_ref;
  7851. #if WASM_ENABLE_GC != 0
  7852. frame_reftype_map = loader_ctx->frame_reftype_map;
  7853. return_reftype_map =
  7854. return_reftype_map_count
  7855. ? return_reftype_maps + return_reftype_map_count - 1
  7856. : NULL;
  7857. #endif
  7858. for (i = (int32)return_count - 1; i >= 0; i--) {
  7859. uint8 type = return_types[i];
  7860. #if WASM_ENABLE_GC != 0
  7861. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  7862. ref_type = NULL;
  7863. if (is_type_multi_byte) {
  7864. bh_assert(return_reftype_map);
  7865. ref_type = return_reftype_map->ref_type;
  7866. }
  7867. #endif
  7868. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  7869. #if WASM_ENABLE_GC != 0
  7870. frame_reftype_map, available_reftype_map,
  7871. #endif
  7872. type,
  7873. #if WASM_ENABLE_GC != 0
  7874. ref_type,
  7875. #endif
  7876. error_buf, error_buf_size))
  7877. return false;
  7878. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  7879. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  7880. #if WASM_ENABLE_GC != 0
  7881. if (is_type_multi_byte) {
  7882. frame_reftype_map--;
  7883. available_reftype_map--;
  7884. return_reftype_map--;
  7885. }
  7886. #endif
  7887. }
  7888. return true;
  7889. fail:
  7890. return false;
  7891. }
  7892. #if WASM_ENABLE_FAST_INTERP != 0
  7893. /* Copy parameters to dynamic space.
  7894. * 1) POP original parameter out;
  7895. * 2) Push and copy original values to dynamic space.
  7896. * The copy instruction format:
  7897. * Part a: param count
  7898. * Part b: all param total cell num
  7899. * Part c: each param's cell_num, src offset and dst offset
  7900. * Part d: each param's src offset
  7901. * Part e: each param's dst offset
  7902. */
  7903. static bool
  7904. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  7905. char *error_buf, uint32 error_buf_size)
  7906. {
  7907. bool ret = false;
  7908. int16 *frame_offset = NULL;
  7909. uint8 *cells = NULL, cell;
  7910. int16 *src_offsets = NULL;
  7911. uint8 *emit_data = NULL;
  7912. uint32 i;
  7913. BranchBlock *block = loader_ctx->frame_csp - 1;
  7914. BlockType *block_type = &block->block_type;
  7915. WASMFuncType *wasm_type = block_type->u.type;
  7916. uint32 param_count = block_type->u.type->param_count;
  7917. int16 condition_offset = 0;
  7918. bool disable_emit = false;
  7919. int16 operand_offset = 0;
  7920. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  7921. /* For if block, we also need copy the condition operand offset. */
  7922. if (is_if_block)
  7923. size += sizeof(*cells) + sizeof(*src_offsets);
  7924. /* Allocate memory for the emit data */
  7925. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  7926. return false;
  7927. cells = emit_data;
  7928. src_offsets = (int16 *)(cells + param_count);
  7929. if (is_if_block)
  7930. condition_offset = *loader_ctx->frame_offset;
  7931. /* POP original parameter out */
  7932. for (i = 0; i < param_count; i++) {
  7933. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  7934. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  7935. }
  7936. frame_offset = loader_ctx->frame_offset;
  7937. /* Get each param's cell num and src offset */
  7938. for (i = 0; i < param_count; i++) {
  7939. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  7940. cells[i] = cell;
  7941. src_offsets[i] = *frame_offset;
  7942. frame_offset += cell;
  7943. }
  7944. /* emit copy instruction */
  7945. emit_label(EXT_OP_COPY_STACK_VALUES);
  7946. /* Part a) */
  7947. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  7948. /* Part b) */
  7949. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  7950. : wasm_type->param_cell_num);
  7951. /* Part c) */
  7952. for (i = 0; i < param_count; i++)
  7953. emit_byte(loader_ctx, cells[i]);
  7954. if (is_if_block)
  7955. emit_byte(loader_ctx, 1);
  7956. /* Part d) */
  7957. for (i = 0; i < param_count; i++)
  7958. emit_operand(loader_ctx, src_offsets[i]);
  7959. if (is_if_block)
  7960. emit_operand(loader_ctx, condition_offset);
  7961. /* Part e) */
  7962. /* Push to dynamic space. The push will emit the dst offset. */
  7963. for (i = 0; i < param_count; i++)
  7964. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  7965. if (is_if_block)
  7966. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  7967. ret = true;
  7968. fail:
  7969. /* Free the emit data */
  7970. wasm_runtime_free(emit_data);
  7971. return ret;
  7972. }
  7973. #endif
  7974. #if WASM_ENABLE_GC == 0
  7975. #define RESET_REFTYPE_MAP_STACK() (void)0
  7976. #else
  7977. #define RESET_REFTYPE_MAP_STACK() \
  7978. do { \
  7979. loader_ctx->reftype_map_num = \
  7980. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  7981. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  7982. + loader_ctx->reftype_map_num; \
  7983. } while (0)
  7984. #endif
  7985. /* reset the stack to the state of before entering the last block */
  7986. #if WASM_ENABLE_FAST_INTERP != 0
  7987. #define RESET_STACK() \
  7988. do { \
  7989. loader_ctx->stack_cell_num = \
  7990. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  7991. loader_ctx->frame_ref = \
  7992. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  7993. loader_ctx->frame_offset = \
  7994. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  7995. RESET_REFTYPE_MAP_STACK(); \
  7996. } while (0)
  7997. #else
  7998. #define RESET_STACK() \
  7999. do { \
  8000. loader_ctx->stack_cell_num = \
  8001. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  8002. loader_ctx->frame_ref = \
  8003. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  8004. RESET_REFTYPE_MAP_STACK(); \
  8005. } while (0)
  8006. #endif
  8007. /* set current block's stack polymorphic state */
  8008. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  8009. do { \
  8010. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  8011. _cur_block->is_stack_polymorphic = flag; \
  8012. } while (0)
  8013. #define BLOCK_HAS_PARAM(block_type) \
  8014. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  8015. #define PRESERVE_LOCAL_FOR_BLOCK() \
  8016. do { \
  8017. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  8018. error_buf_size))) { \
  8019. goto fail; \
  8020. } \
  8021. } while (0)
  8022. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  8023. static bool
  8024. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  8025. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  8026. uint32 error_buf_size)
  8027. {
  8028. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  8029. return false;
  8030. }
  8031. if (table_idx < module->import_table_count) {
  8032. if (p_elem_type)
  8033. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  8034. #if WASM_ENABLE_GC != 0
  8035. if (p_ref_type)
  8036. *((WASMRefType **)p_ref_type) =
  8037. module->import_tables[table_idx].u.table.elem_ref_type;
  8038. #endif
  8039. }
  8040. else {
  8041. if (p_elem_type)
  8042. *p_elem_type =
  8043. module->tables[module->import_table_count + table_idx]
  8044. .elem_type;
  8045. #if WASM_ENABLE_GC != 0
  8046. if (p_ref_type)
  8047. *((WASMRefType **)p_ref_type) =
  8048. module->tables[module->import_table_count + table_idx]
  8049. .elem_ref_type;
  8050. #endif
  8051. }
  8052. return true;
  8053. }
  8054. static bool
  8055. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  8056. uint8 *p_elem_type, void **p_elem_ref_type,
  8057. char *error_buf, uint32 error_buf_size)
  8058. {
  8059. if (table_seg_idx >= module->table_seg_count) {
  8060. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  8061. table_seg_idx);
  8062. return false;
  8063. }
  8064. if (p_elem_type) {
  8065. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  8066. }
  8067. #if WASM_ENABLE_GC != 0
  8068. if (p_elem_ref_type)
  8069. *((WASMRefType **)p_elem_ref_type) =
  8070. module->table_segments[table_seg_idx].elem_ref_type;
  8071. #endif
  8072. return true;
  8073. }
  8074. #endif
  8075. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  8076. const uint8 *
  8077. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  8078. uint32 *len)
  8079. {
  8080. WASMCustomSection *section = module->custom_section_list;
  8081. while (section) {
  8082. if ((section->name_len == strlen(name))
  8083. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  8084. if (len) {
  8085. *len = section->content_len;
  8086. }
  8087. return section->content_addr;
  8088. }
  8089. section = section->next;
  8090. }
  8091. return false;
  8092. }
  8093. #endif
  8094. static bool
  8095. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  8096. uint32 cur_func_idx, char *error_buf,
  8097. uint32 error_buf_size)
  8098. {
  8099. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  8100. uint32 param_count, local_count, global_count;
  8101. uint8 *param_types, *local_types, local_type, global_type;
  8102. BlockType func_block_type;
  8103. uint16 *local_offsets, local_offset;
  8104. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  8105. uint32 table_seg_idx, data_seg_idx, count, align, mem_offset, i;
  8106. int32 i32_const = 0;
  8107. int64 i64_const;
  8108. uint8 opcode;
  8109. bool return_value = false;
  8110. WASMLoaderContext *loader_ctx;
  8111. BranchBlock *frame_csp_tmp;
  8112. #if WASM_ENABLE_GC != 0
  8113. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  8114. uint32 param_reftype_map_count, local_reftype_map_count;
  8115. int32 heap_type;
  8116. WASMRefType wasm_ref_type = { 0 };
  8117. bool need_ref_type_map;
  8118. #endif
  8119. #if WASM_ENABLE_FAST_INTERP != 0
  8120. uint8 *func_const_end, *func_const = NULL;
  8121. int16 operand_offset = 0;
  8122. uint8 last_op = 0;
  8123. bool disable_emit, preserve_local = false;
  8124. float32 f32_const;
  8125. float64 f64_const;
  8126. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  8127. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  8128. #endif
  8129. global_count = module->import_global_count + module->global_count;
  8130. param_count = func->func_type->param_count;
  8131. param_types = func->func_type->types;
  8132. func_block_type.is_value_type = false;
  8133. func_block_type.u.type = func->func_type;
  8134. local_count = func->local_count;
  8135. local_types = func->local_types;
  8136. local_offsets = func->local_offsets;
  8137. #if WASM_ENABLE_GC != 0
  8138. param_reftype_maps = func->func_type->ref_type_maps;
  8139. param_reftype_map_count = func->func_type->ref_type_map_count;
  8140. local_reftype_maps = func->local_ref_type_maps;
  8141. local_reftype_map_count = func->local_ref_type_map_count;
  8142. #endif
  8143. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  8144. goto fail;
  8145. }
  8146. #if WASM_ENABLE_GC != 0
  8147. loader_ctx->module = module;
  8148. loader_ctx->ref_type_set = module->ref_type_set;
  8149. loader_ctx->ref_type_tmp = &wasm_ref_type;
  8150. #endif
  8151. #if WASM_ENABLE_FAST_INTERP != 0
  8152. /* For the first traverse, the initial value of preserved_local_offset has
  8153. * not been determined, we use the INT16_MAX to represent that a slot has
  8154. * been copied to preserve space. For second traverse, this field will be
  8155. * set to the appropriate value in wasm_loader_ctx_reinit.
  8156. * This is for Issue #1230,
  8157. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  8158. * drop opcodes need to know which slots are preserved, so those slots will
  8159. * not be treated as dynamically allocated slots */
  8160. loader_ctx->preserved_local_offset = INT16_MAX;
  8161. re_scan:
  8162. if (loader_ctx->code_compiled_size > 0) {
  8163. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  8164. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  8165. goto fail;
  8166. }
  8167. p = func->code;
  8168. func->code_compiled = loader_ctx->p_code_compiled;
  8169. func->code_compiled_size = loader_ctx->code_compiled_size;
  8170. }
  8171. #endif
  8172. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  8173. while (p < p_end) {
  8174. opcode = *p++;
  8175. #if WASM_ENABLE_FAST_INTERP != 0
  8176. p_org = p;
  8177. disable_emit = false;
  8178. emit_label(opcode);
  8179. #endif
  8180. switch (opcode) {
  8181. case WASM_OP_UNREACHABLE:
  8182. RESET_STACK();
  8183. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8184. break;
  8185. case WASM_OP_NOP:
  8186. #if WASM_ENABLE_FAST_INTERP != 0
  8187. skip_label();
  8188. #endif
  8189. break;
  8190. case WASM_OP_IF:
  8191. #if WASM_ENABLE_FAST_INTERP != 0
  8192. PRESERVE_LOCAL_FOR_BLOCK();
  8193. #endif
  8194. POP_I32();
  8195. goto handle_op_block_and_loop;
  8196. case WASM_OP_BLOCK:
  8197. case WASM_OP_LOOP:
  8198. #if WASM_ENABLE_FAST_INTERP != 0
  8199. PRESERVE_LOCAL_FOR_BLOCK();
  8200. #endif
  8201. handle_op_block_and_loop:
  8202. {
  8203. uint8 value_type;
  8204. BlockType block_type;
  8205. CHECK_BUF(p, p_end, 1);
  8206. value_type = read_uint8(p);
  8207. if (is_byte_a_type(value_type)) {
  8208. /* If the first byte is one of these special values:
  8209. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  8210. * the single return value. */
  8211. block_type.is_value_type = true;
  8212. block_type.u.value_type.type = value_type;
  8213. #if WASM_ENABLE_GC != 0
  8214. if (value_type != VALUE_TYPE_VOID) {
  8215. p_org = p;
  8216. p--;
  8217. if (!resolve_value_type((const uint8 **)&p, p_end,
  8218. module, &need_ref_type_map,
  8219. &wasm_ref_type, false,
  8220. error_buf, error_buf_size)) {
  8221. goto fail;
  8222. }
  8223. if (need_ref_type_map) {
  8224. block_type.u.value_type.ref_type_map.index = 0;
  8225. if (!(block_type.u.value_type.ref_type_map
  8226. .ref_type = reftype_set_insert(
  8227. module->ref_type_set, &wasm_ref_type,
  8228. error_buf, error_buf_size))) {
  8229. goto fail;
  8230. }
  8231. }
  8232. /* Set again as the type might be changed, e.g.
  8233. (ref null any) to anyref */
  8234. block_type.u.value_type.type = wasm_ref_type.ref_type;
  8235. #if WASM_ENABLE_FAST_INTERP == 0
  8236. while (p_org < p) {
  8237. #if WASM_ENABLE_DEBUG_INTERP != 0
  8238. if (!record_fast_op(module, p_org, *p_org,
  8239. error_buf, error_buf_size)) {
  8240. goto fail;
  8241. }
  8242. #endif
  8243. /* Ignore extra bytes for interpreter */
  8244. *p_org++ = WASM_OP_NOP;
  8245. }
  8246. #endif
  8247. }
  8248. #endif /* end of WASM_ENABLE_GC != 0 */
  8249. }
  8250. else {
  8251. uint32 type_index;
  8252. /* Resolve the leb128 encoded type index as block type */
  8253. p--;
  8254. p_org = p - 1;
  8255. read_leb_uint32(p, p_end, type_index);
  8256. if (type_index >= module->type_count) {
  8257. set_error_buf(error_buf, error_buf_size,
  8258. "unknown type");
  8259. goto fail;
  8260. }
  8261. block_type.is_value_type = false;
  8262. block_type.u.type =
  8263. (WASMFuncType *)module->types[type_index];
  8264. #if WASM_ENABLE_FAST_INTERP == 0
  8265. /* If block use type index as block type, change the opcode
  8266. * to new extended opcode so that interpreter can resolve
  8267. * the block quickly.
  8268. */
  8269. #if WASM_ENABLE_DEBUG_INTERP != 0
  8270. if (!record_fast_op(module, p_org, *p_org, error_buf,
  8271. error_buf_size)) {
  8272. goto fail;
  8273. }
  8274. #endif
  8275. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  8276. #endif
  8277. }
  8278. /* Pop block parameters from stack */
  8279. if (BLOCK_HAS_PARAM(block_type)) {
  8280. WASMFuncType *func_type = block_type.u.type;
  8281. for (i = 0; i < block_type.u.type->param_count; i++)
  8282. POP_TYPE(
  8283. func_type->types[func_type->param_count - i - 1]);
  8284. }
  8285. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  8286. block_type, p);
  8287. /* Pass parameters to block */
  8288. if (BLOCK_HAS_PARAM(block_type)) {
  8289. for (i = 0; i < block_type.u.type->param_count; i++)
  8290. PUSH_TYPE(block_type.u.type->types[i]);
  8291. }
  8292. #if WASM_ENABLE_FAST_INTERP != 0
  8293. if (opcode == WASM_OP_BLOCK) {
  8294. skip_label();
  8295. }
  8296. else if (opcode == WASM_OP_LOOP) {
  8297. skip_label();
  8298. if (BLOCK_HAS_PARAM(block_type)) {
  8299. /* Make sure params are in dynamic space */
  8300. if (!copy_params_to_dynamic_space(
  8301. loader_ctx, false, error_buf, error_buf_size))
  8302. goto fail;
  8303. }
  8304. (loader_ctx->frame_csp - 1)->code_compiled =
  8305. loader_ctx->p_code_compiled;
  8306. }
  8307. else if (opcode == WASM_OP_IF) {
  8308. /* If block has parameters, we should make sure they are in
  8309. * dynamic space. Otherwise, when else branch is missing,
  8310. * the later opcode may consume incorrect operand offset.
  8311. * Spec case:
  8312. * (func (export "params-id") (param i32) (result i32)
  8313. * (i32.const 1)
  8314. * (i32.const 2)
  8315. * (if (param i32 i32) (result i32 i32) (local.get 0)
  8316. * (then)) (i32.add)
  8317. * )
  8318. *
  8319. * So we should emit a copy instruction before the if.
  8320. *
  8321. * And we also need to save the parameter offsets and
  8322. * recover them before entering else branch.
  8323. *
  8324. */
  8325. if (BLOCK_HAS_PARAM(block_type)) {
  8326. BranchBlock *block = loader_ctx->frame_csp - 1;
  8327. uint64 size;
  8328. /* skip the if condition operand offset */
  8329. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8330. /* skip the if label */
  8331. skip_label();
  8332. /* Emit a copy instruction */
  8333. if (!copy_params_to_dynamic_space(
  8334. loader_ctx, true, error_buf, error_buf_size))
  8335. goto fail;
  8336. /* Emit the if instruction */
  8337. emit_label(opcode);
  8338. /* Emit the new condition operand offset */
  8339. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  8340. /* Save top param_count values of frame_offset stack, so
  8341. * that we can recover it before executing else branch
  8342. */
  8343. size = sizeof(int16)
  8344. * (uint64)block_type.u.type->param_cell_num;
  8345. if (!(block->param_frame_offsets = loader_malloc(
  8346. size, error_buf, error_buf_size)))
  8347. goto fail;
  8348. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  8349. loader_ctx->frame_offset
  8350. - size / sizeof(int16),
  8351. (uint32)size);
  8352. }
  8353. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  8354. emit_empty_label_addr_and_frame_ip(PATCH_END);
  8355. }
  8356. #endif
  8357. break;
  8358. }
  8359. case WASM_OP_ELSE:
  8360. {
  8361. BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
  8362. if (loader_ctx->csp_num < 2
  8363. || (loader_ctx->frame_csp - 1)->label_type
  8364. != LABEL_TYPE_IF) {
  8365. set_error_buf(
  8366. error_buf, error_buf_size,
  8367. "opcode else found without matched opcode if");
  8368. goto fail;
  8369. }
  8370. /* check whether if branch's stack matches its result type */
  8371. if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1,
  8372. error_buf, error_buf_size))
  8373. goto fail;
  8374. (loader_ctx->frame_csp - 1)->else_addr = p - 1;
  8375. #if WASM_ENABLE_FAST_INTERP != 0
  8376. /* if the result of if branch is in local or const area, add a
  8377. * copy op */
  8378. RESERVE_BLOCK_RET();
  8379. emit_empty_label_addr_and_frame_ip(PATCH_END);
  8380. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  8381. #endif
  8382. RESET_STACK();
  8383. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  8384. /* Pass parameters to if-false branch */
  8385. if (BLOCK_HAS_PARAM(block_type)) {
  8386. for (i = 0; i < block_type.u.type->param_count; i++)
  8387. PUSH_TYPE(block_type.u.type->types[i]);
  8388. }
  8389. #if WASM_ENABLE_FAST_INTERP != 0
  8390. /* Recover top param_count values of frame_offset stack */
  8391. if (BLOCK_HAS_PARAM((block_type))) {
  8392. uint32 size;
  8393. BranchBlock *block = loader_ctx->frame_csp - 1;
  8394. size = sizeof(int16) * block_type.u.type->param_cell_num;
  8395. bh_memcpy_s(loader_ctx->frame_offset, size,
  8396. block->param_frame_offsets, size);
  8397. loader_ctx->frame_offset += (size / sizeof(int16));
  8398. }
  8399. #endif
  8400. break;
  8401. }
  8402. case WASM_OP_END:
  8403. {
  8404. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  8405. /* check whether block stack matches its result type */
  8406. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  8407. error_buf_size))
  8408. goto fail;
  8409. /* if no else branch, and return types do not match param types,
  8410. report failure */
  8411. if (cur_block->label_type == LABEL_TYPE_IF
  8412. && !cur_block->else_addr) {
  8413. uint32 block_param_count = 0, block_ret_count = 0;
  8414. uint8 *block_param_types = NULL, *block_ret_types = NULL;
  8415. BlockType *cur_block_type = &cur_block->block_type;
  8416. #if WASM_ENABLE_GC != 0
  8417. uint32 block_param_reftype_map_count;
  8418. uint32 block_ret_reftype_map_count;
  8419. WASMRefTypeMap *block_param_reftype_maps;
  8420. WASMRefTypeMap *block_ret_reftype_maps;
  8421. #endif
  8422. block_param_count = block_type_get_param_types(
  8423. cur_block_type, &block_param_types
  8424. #if WASM_ENABLE_GC != 0
  8425. ,
  8426. &block_param_reftype_maps,
  8427. &block_param_reftype_map_count
  8428. #endif
  8429. );
  8430. block_ret_count = block_type_get_result_types(
  8431. cur_block_type, &block_ret_types
  8432. #if WASM_ENABLE_GC != 0
  8433. ,
  8434. &block_ret_reftype_maps, &block_ret_reftype_map_count
  8435. #endif
  8436. );
  8437. if (block_param_count != block_ret_count
  8438. || (block_param_count
  8439. && memcmp(block_param_types, block_ret_types,
  8440. block_param_count))) {
  8441. set_error_buf(error_buf, error_buf_size,
  8442. "type mismatch: else branch missing");
  8443. goto fail;
  8444. }
  8445. #if WASM_ENABLE_GC != 0
  8446. if (block_param_reftype_map_count
  8447. != block_ret_reftype_map_count
  8448. || (block_param_reftype_map_count
  8449. && memcmp(block_param_reftype_maps,
  8450. block_ret_reftype_maps,
  8451. sizeof(WASMRefTypeMap)
  8452. * block_param_reftype_map_count))) {
  8453. set_error_buf(error_buf, error_buf_size,
  8454. "type mismatch: else branch missing");
  8455. goto fail;
  8456. }
  8457. #endif
  8458. }
  8459. POP_CSP();
  8460. #if WASM_ENABLE_FAST_INTERP != 0
  8461. skip_label();
  8462. /* copy the result to the block return address */
  8463. RESERVE_BLOCK_RET();
  8464. apply_label_patch(loader_ctx, 0, PATCH_END);
  8465. free_label_patch_list(loader_ctx->frame_csp);
  8466. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  8467. int32 idx;
  8468. uint8 ret_type;
  8469. emit_label(WASM_OP_RETURN);
  8470. for (idx = (int32)func->func_type->result_count - 1;
  8471. idx >= 0; idx--) {
  8472. ret_type = *(func->func_type->types
  8473. + func->func_type->param_count + idx);
  8474. POP_OFFSET_TYPE(ret_type);
  8475. }
  8476. }
  8477. #endif
  8478. if (loader_ctx->csp_num > 0) {
  8479. loader_ctx->frame_csp->end_addr = p - 1;
  8480. }
  8481. else {
  8482. /* end of function block, function will return */
  8483. if (p < p_end) {
  8484. set_error_buf(error_buf, error_buf_size,
  8485. "section size mismatch");
  8486. goto fail;
  8487. }
  8488. }
  8489. break;
  8490. }
  8491. case WASM_OP_BR:
  8492. {
  8493. if (!(frame_csp_tmp =
  8494. check_branch_block(loader_ctx, &p, p_end, false,
  8495. error_buf, error_buf_size)))
  8496. goto fail;
  8497. RESET_STACK();
  8498. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8499. break;
  8500. }
  8501. case WASM_OP_BR_IF:
  8502. {
  8503. POP_I32();
  8504. if (!(frame_csp_tmp =
  8505. check_branch_block(loader_ctx, &p, p_end, false,
  8506. error_buf, error_buf_size)))
  8507. goto fail;
  8508. break;
  8509. }
  8510. case WASM_OP_BR_TABLE:
  8511. {
  8512. uint32 depth, default_arity, arity = 0;
  8513. BranchBlock *target_block;
  8514. BlockType *target_block_type;
  8515. #if WASM_ENABLE_FAST_INTERP == 0
  8516. BrTableCache *br_table_cache = NULL;
  8517. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  8518. uint32 j;
  8519. #endif
  8520. read_leb_uint32(p, p_end, count);
  8521. #if WASM_ENABLE_FAST_INTERP != 0
  8522. emit_uint32(loader_ctx, count);
  8523. #endif
  8524. POP_I32();
  8525. /* Get the default depth and check it */
  8526. p_org = p;
  8527. for (i = 0; i <= count; i++) {
  8528. read_leb_uint32(p, p_end, depth);
  8529. }
  8530. if (loader_ctx->csp_num < depth + 1) {
  8531. set_error_buf(error_buf, error_buf_size,
  8532. "unknown label, "
  8533. "unexpected end of section or function");
  8534. goto fail;
  8535. }
  8536. p = p_org;
  8537. /* Get the default block's arity */
  8538. target_block = loader_ctx->frame_csp - (depth + 1);
  8539. target_block_type = &target_block->block_type;
  8540. default_arity = block_type_get_arity(target_block_type,
  8541. target_block->label_type);
  8542. #if WASM_ENABLE_FAST_INTERP == 0
  8543. p_depth_begin = p_depth = p;
  8544. #endif
  8545. for (i = 0; i <= count; i++) {
  8546. p_org = p;
  8547. read_leb_uint32(p, p_end, depth);
  8548. if (loader_ctx->csp_num < depth + 1) {
  8549. set_error_buf(error_buf, error_buf_size,
  8550. "unknown label, "
  8551. "unexpected end of section or function");
  8552. goto fail;
  8553. }
  8554. p = p_org;
  8555. /* Get the target block's arity and check it */
  8556. target_block = loader_ctx->frame_csp - (depth + 1);
  8557. target_block_type = &target_block->block_type;
  8558. arity = block_type_get_arity(target_block_type,
  8559. target_block->label_type);
  8560. if (arity != default_arity) {
  8561. set_error_buf(error_buf, error_buf_size,
  8562. "type mismatch: br_table targets must "
  8563. "all use same result type");
  8564. goto fail;
  8565. }
  8566. if (!(frame_csp_tmp =
  8567. check_branch_block(loader_ctx, &p, p_end, true,
  8568. error_buf, error_buf_size))) {
  8569. goto fail;
  8570. }
  8571. #if WASM_ENABLE_FAST_INTERP == 0
  8572. if (br_table_cache) {
  8573. br_table_cache->br_depths[i] = depth;
  8574. }
  8575. else {
  8576. if (depth > 255) {
  8577. /* The depth cannot be stored in one byte,
  8578. create br_table cache to store each depth */
  8579. #if WASM_ENABLE_DEBUG_INTERP != 0
  8580. if (!record_fast_op(module, p_opcode, *p_opcode,
  8581. error_buf, error_buf_size)) {
  8582. goto fail;
  8583. }
  8584. #endif
  8585. if (!(br_table_cache = loader_malloc(
  8586. offsetof(BrTableCache, br_depths)
  8587. + sizeof(uint32)
  8588. * (uint64)(count + 1),
  8589. error_buf, error_buf_size))) {
  8590. goto fail;
  8591. }
  8592. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  8593. br_table_cache->br_table_op_addr = p_opcode;
  8594. br_table_cache->br_count = count;
  8595. /* Copy previous depths which are one byte */
  8596. for (j = 0; j < i; j++) {
  8597. br_table_cache->br_depths[j] = p_depth_begin[j];
  8598. }
  8599. br_table_cache->br_depths[i] = depth;
  8600. bh_list_insert(module->br_table_cache_list,
  8601. br_table_cache);
  8602. }
  8603. else {
  8604. /* The depth can be stored in one byte, use the
  8605. byte of the leb to store it */
  8606. *p_depth++ = (uint8)depth;
  8607. }
  8608. }
  8609. #endif
  8610. }
  8611. #if WASM_ENABLE_FAST_INTERP == 0
  8612. /* Set the tailing bytes to nop */
  8613. if (br_table_cache)
  8614. p_depth = p_depth_begin;
  8615. while (p_depth < p)
  8616. *p_depth++ = WASM_OP_NOP;
  8617. #endif
  8618. RESET_STACK();
  8619. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8620. break;
  8621. }
  8622. case WASM_OP_RETURN:
  8623. {
  8624. int32 idx;
  8625. uint8 ret_type;
  8626. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  8627. idx--) {
  8628. ret_type = *(func->func_type->types
  8629. + func->func_type->param_count + idx);
  8630. POP_TYPE(ret_type);
  8631. #if WASM_ENABLE_FAST_INTERP != 0
  8632. /* emit the offset after return opcode */
  8633. POP_OFFSET_TYPE(ret_type);
  8634. #endif
  8635. }
  8636. RESET_STACK();
  8637. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8638. break;
  8639. }
  8640. case WASM_OP_CALL:
  8641. #if WASM_ENABLE_TAIL_CALL != 0
  8642. case WASM_OP_RETURN_CALL:
  8643. #endif
  8644. #if WASM_ENABLE_GC != 0
  8645. case WASM_OP_CALL_REF:
  8646. case WASM_OP_RETURN_CALL_REF:
  8647. #endif
  8648. {
  8649. WASMFuncType *func_type;
  8650. uint8 type;
  8651. int32 idx;
  8652. #if WASM_ENABLE_GC != 0
  8653. WASMRefType *ref_type;
  8654. uint32 type_idx1;
  8655. int32 j;
  8656. #endif
  8657. #if WASM_ENABLE_GC != 0
  8658. if (opcode == WASM_OP_CALL_REF
  8659. || opcode == WASM_OP_RETURN_CALL_REF) {
  8660. read_leb_uint32(p, p_end, type_idx1);
  8661. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  8662. &type_idx, error_buf,
  8663. error_buf_size)) {
  8664. goto fail;
  8665. }
  8666. if (type == VALUE_TYPE_ANY) {
  8667. type_idx = type_idx1;
  8668. }
  8669. if (!check_type_index(module, type_idx, error_buf,
  8670. error_buf_size)) {
  8671. goto fail;
  8672. }
  8673. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  8674. set_error_buf(error_buf, error_buf_size,
  8675. "unkown function type");
  8676. goto fail;
  8677. }
  8678. if (type_idx != type_idx1) {
  8679. set_error_buf(error_buf, error_buf_size,
  8680. "function type mismatch");
  8681. goto fail;
  8682. }
  8683. func_type = (WASMFuncType *)module->types[type_idx];
  8684. }
  8685. else
  8686. #endif
  8687. {
  8688. read_leb_uint32(p, p_end, func_idx);
  8689. #if WASM_ENABLE_FAST_INTERP != 0
  8690. /* we need to emit func_idx before arguments */
  8691. emit_uint32(loader_ctx, func_idx);
  8692. #endif
  8693. if (!check_function_index(module, func_idx, error_buf,
  8694. error_buf_size)) {
  8695. goto fail;
  8696. }
  8697. if (func_idx < module->import_function_count)
  8698. func_type = module->import_functions[func_idx]
  8699. .u.function.func_type;
  8700. else
  8701. func_type =
  8702. module
  8703. ->functions[func_idx
  8704. - module->import_function_count]
  8705. ->func_type;
  8706. }
  8707. if (func_type->param_count > 0) {
  8708. #if WASM_ENABLE_GC != 0
  8709. j = (int32)(func_type->result_ref_type_maps
  8710. - func_type->ref_type_maps - 1);
  8711. #endif
  8712. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  8713. idx--) {
  8714. #if WASM_ENABLE_GC != 0
  8715. if (wasm_is_type_multi_byte_type(
  8716. func_type->types[idx])) {
  8717. ref_type = func_type->ref_type_maps[j].ref_type;
  8718. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  8719. ref_type,
  8720. wasm_reftype_struct_size(ref_type));
  8721. j--;
  8722. }
  8723. #endif
  8724. POP_TYPE(func_type->types[idx]);
  8725. #if WASM_ENABLE_FAST_INTERP != 0
  8726. POP_OFFSET_TYPE(func_type->types[idx]);
  8727. #endif
  8728. }
  8729. }
  8730. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  8731. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  8732. #endif
  8733. #if WASM_ENABLE_GC != 0
  8734. j = (int32)(func_type->result_ref_type_maps
  8735. - func_type->ref_type_maps);
  8736. #endif
  8737. for (i = 0; i < func_type->result_count; i++) {
  8738. #if WASM_ENABLE_GC != 0
  8739. if (wasm_is_type_multi_byte_type(
  8740. func_type->types[func_type->param_count + i])) {
  8741. ref_type = func_type->ref_type_maps[j].ref_type;
  8742. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  8743. ref_type,
  8744. wasm_reftype_struct_size(ref_type));
  8745. j++;
  8746. }
  8747. #endif
  8748. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  8749. #if WASM_ENABLE_FAST_INTERP != 0
  8750. /* Here we emit each return value's dynamic_offset. But
  8751. * in fact these offsets are continuous, so interpreter
  8752. * only need to get the first return value's offset.
  8753. */
  8754. PUSH_OFFSET_TYPE(
  8755. func_type->types[func_type->param_count + i]);
  8756. #endif
  8757. }
  8758. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  8759. }
  8760. else {
  8761. #if WASM_ENABLE_GC == 0
  8762. if (func_type->result_count
  8763. != func->func_type->result_count) {
  8764. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  8765. "type mismatch: expect ",
  8766. func->func_type->result_count,
  8767. " return values but got other");
  8768. goto fail;
  8769. }
  8770. for (i = 0; i < func_type->result_count; i++) {
  8771. type = func->func_type
  8772. ->types[func->func_type->param_count + i];
  8773. if (func_type->types[func_type->param_count + i]
  8774. != type) {
  8775. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  8776. "type mismatch: expect ",
  8777. type2str(type), " but got other");
  8778. goto fail;
  8779. }
  8780. }
  8781. #else
  8782. if (!wasm_func_type_result_is_subtype_of(
  8783. func_type, func->func_type, module->types,
  8784. module->type_count)) {
  8785. set_error_buf(
  8786. error_buf, error_buf_size,
  8787. "type mismatch: invalid func result types");
  8788. goto fail;
  8789. }
  8790. #endif
  8791. RESET_STACK();
  8792. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8793. }
  8794. #endif
  8795. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  8796. || WASM_ENABLE_WAMR_COMPILER != 0
  8797. func->has_op_func_call = true;
  8798. #endif
  8799. (void)type;
  8800. break;
  8801. }
  8802. /*
  8803. * if disable reference type: call_indirect typeidx, 0x00
  8804. * if enable reference type: call_indirect typeidx, tableidx
  8805. */
  8806. case WASM_OP_CALL_INDIRECT:
  8807. #if WASM_ENABLE_TAIL_CALL != 0
  8808. case WASM_OP_RETURN_CALL_INDIRECT:
  8809. #endif
  8810. {
  8811. int32 idx;
  8812. WASMFuncType *func_type;
  8813. read_leb_uint32(p, p_end, type_idx);
  8814. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  8815. read_leb_uint32(p, p_end, table_idx);
  8816. #else
  8817. CHECK_BUF(p, p_end, 1);
  8818. table_idx = read_uint8(p);
  8819. #endif
  8820. if (!check_table_index(module, table_idx, error_buf,
  8821. error_buf_size)) {
  8822. goto fail;
  8823. }
  8824. #if WASM_ENABLE_FAST_INTERP != 0
  8825. /* we need to emit before arguments */
  8826. #if WASM_ENABLE_TAIL_CALL != 0
  8827. emit_byte(loader_ctx, opcode);
  8828. #endif
  8829. emit_uint32(loader_ctx, type_idx);
  8830. emit_uint32(loader_ctx, table_idx);
  8831. #endif
  8832. /* skip elem idx */
  8833. POP_I32();
  8834. if (type_idx >= module->type_count) {
  8835. set_error_buf(error_buf, error_buf_size, "unknown type");
  8836. goto fail;
  8837. }
  8838. func_type = (WASMFuncType *)module->types[type_idx];
  8839. if (func_type->param_count > 0) {
  8840. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  8841. idx--) {
  8842. POP_TYPE(func_type->types[idx]);
  8843. #if WASM_ENABLE_FAST_INTERP != 0
  8844. POP_OFFSET_TYPE(func_type->types[idx]);
  8845. #endif
  8846. }
  8847. }
  8848. #if WASM_ENABLE_TAIL_CALL != 0
  8849. if (opcode == WASM_OP_CALL_INDIRECT) {
  8850. #endif
  8851. for (i = 0; i < func_type->result_count; i++) {
  8852. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  8853. #if WASM_ENABLE_FAST_INTERP != 0
  8854. PUSH_OFFSET_TYPE(
  8855. func_type->types[func_type->param_count + i]);
  8856. #endif
  8857. }
  8858. #if WASM_ENABLE_TAIL_CALL != 0
  8859. }
  8860. else {
  8861. uint8 type;
  8862. if (func_type->result_count
  8863. != func->func_type->result_count) {
  8864. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  8865. "type mismatch: expect ",
  8866. func->func_type->result_count,
  8867. " return values but got other");
  8868. goto fail;
  8869. }
  8870. for (i = 0; i < func_type->result_count; i++) {
  8871. type = func->func_type
  8872. ->types[func->func_type->param_count + i];
  8873. if (func_type->types[func_type->param_count + i]
  8874. != type) {
  8875. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  8876. "type mismatch: expect ",
  8877. type2str(type), " but got other");
  8878. goto fail;
  8879. }
  8880. }
  8881. RESET_STACK();
  8882. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8883. }
  8884. #endif
  8885. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  8886. || WASM_ENABLE_WAMR_COMPILER != 0
  8887. func->has_op_func_call = true;
  8888. #endif
  8889. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8890. func->has_op_call_indirect = true;
  8891. #endif
  8892. break;
  8893. }
  8894. case WASM_OP_DROP:
  8895. {
  8896. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  8897. int32 available_stack_cell =
  8898. (int32)(loader_ctx->stack_cell_num
  8899. - cur_block->stack_cell_num);
  8900. if (available_stack_cell <= 0
  8901. && !cur_block->is_stack_polymorphic) {
  8902. set_error_buf(error_buf, error_buf_size,
  8903. "type mismatch, opcode drop was found "
  8904. "but stack was empty");
  8905. goto fail;
  8906. }
  8907. if (available_stack_cell > 0) {
  8908. #if WASM_ENABLE_GC != 0
  8909. if (wasm_is_type_multi_byte_type(
  8910. *(loader_ctx->frame_ref - 1))) {
  8911. bh_assert((int32)(loader_ctx->reftype_map_num
  8912. - cur_block->reftype_map_num)
  8913. > 0);
  8914. loader_ctx->frame_reftype_map--;
  8915. loader_ctx->reftype_map_num--;
  8916. }
  8917. #endif
  8918. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  8919. loader_ctx->frame_ref--;
  8920. loader_ctx->stack_cell_num--;
  8921. #if WASM_ENABLE_FAST_INTERP != 0
  8922. skip_label();
  8923. loader_ctx->frame_offset--;
  8924. if ((*(loader_ctx->frame_offset)
  8925. > loader_ctx->start_dynamic_offset)
  8926. && (*(loader_ctx->frame_offset)
  8927. < loader_ctx->max_dynamic_offset))
  8928. loader_ctx->dynamic_offset--;
  8929. #endif
  8930. }
  8931. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  8932. loader_ctx->frame_ref -= 2;
  8933. loader_ctx->stack_cell_num -= 2;
  8934. #if WASM_ENABLE_FAST_INTERP == 0
  8935. *(p - 1) = WASM_OP_DROP_64;
  8936. #endif
  8937. #if WASM_ENABLE_FAST_INTERP != 0
  8938. skip_label();
  8939. loader_ctx->frame_offset -= 2;
  8940. if ((*(loader_ctx->frame_offset)
  8941. > loader_ctx->start_dynamic_offset)
  8942. && (*(loader_ctx->frame_offset)
  8943. < loader_ctx->max_dynamic_offset))
  8944. loader_ctx->dynamic_offset -= 2;
  8945. #endif
  8946. }
  8947. #if WASM_ENABLE_SIMD != 0
  8948. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8949. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  8950. loader_ctx->frame_ref -= 4;
  8951. loader_ctx->stack_cell_num -= 4;
  8952. }
  8953. #endif
  8954. #endif
  8955. else {
  8956. set_error_buf(error_buf, error_buf_size,
  8957. "type mismatch");
  8958. goto fail;
  8959. }
  8960. }
  8961. else {
  8962. #if WASM_ENABLE_FAST_INTERP != 0
  8963. skip_label();
  8964. #endif
  8965. }
  8966. break;
  8967. }
  8968. case WASM_OP_SELECT:
  8969. {
  8970. uint8 ref_type;
  8971. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  8972. int32 available_stack_cell;
  8973. POP_I32();
  8974. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  8975. - cur_block->stack_cell_num);
  8976. if (available_stack_cell <= 0
  8977. && !cur_block->is_stack_polymorphic) {
  8978. set_error_buf(error_buf, error_buf_size,
  8979. "type mismatch or invalid result arity, "
  8980. "opcode select was found "
  8981. "but stack was empty");
  8982. goto fail;
  8983. }
  8984. if (available_stack_cell > 0) {
  8985. switch (*(loader_ctx->frame_ref - 1)) {
  8986. case VALUE_TYPE_I32:
  8987. case VALUE_TYPE_F32:
  8988. break;
  8989. case VALUE_TYPE_I64:
  8990. case VALUE_TYPE_F64:
  8991. #if WASM_ENABLE_FAST_INTERP == 0
  8992. *(p - 1) = WASM_OP_SELECT_64;
  8993. #endif
  8994. #if WASM_ENABLE_FAST_INTERP != 0
  8995. if (loader_ctx->p_code_compiled) {
  8996. uint8 opcode_tmp = WASM_OP_SELECT_64;
  8997. uint8 *p_code_compiled_tmp =
  8998. loader_ctx->p_code_compiled - 2;
  8999. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  9000. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  9001. *(void **)(p_code_compiled_tmp
  9002. - sizeof(void *)) =
  9003. handle_table[opcode_tmp];
  9004. #else
  9005. int32 offset =
  9006. (int32)((uint8 *)handle_table[opcode_tmp]
  9007. - (uint8 *)handle_table[0]);
  9008. if (!(offset >= INT16_MIN
  9009. && offset < INT16_MAX)) {
  9010. set_error_buf(error_buf, error_buf_size,
  9011. "pre-compiled label offset "
  9012. "out of range");
  9013. goto fail;
  9014. }
  9015. *(int16 *)(p_code_compiled_tmp
  9016. - sizeof(int16)) = (int16)offset;
  9017. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  9018. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  9019. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  9020. *(p_code_compiled_tmp - 1) = opcode_tmp;
  9021. #else
  9022. *(p_code_compiled_tmp - 2) = opcode_tmp;
  9023. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  9024. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  9025. }
  9026. #endif /* end of WASM_ENABLE_FAST_INTERP */
  9027. break;
  9028. #if WASM_ENABLE_SIMD != 0
  9029. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  9030. case VALUE_TYPE_V128:
  9031. break;
  9032. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9033. #endif /* WASM_ENABLE_SIMD != 0 */
  9034. default:
  9035. {
  9036. set_error_buf(error_buf, error_buf_size,
  9037. "type mismatch");
  9038. goto fail;
  9039. }
  9040. }
  9041. ref_type = *(loader_ctx->frame_ref - 1);
  9042. #if WASM_ENABLE_FAST_INTERP != 0
  9043. POP_OFFSET_TYPE(ref_type);
  9044. POP_TYPE(ref_type);
  9045. POP_OFFSET_TYPE(ref_type);
  9046. POP_TYPE(ref_type);
  9047. PUSH_OFFSET_TYPE(ref_type);
  9048. PUSH_TYPE(ref_type);
  9049. #else
  9050. POP2_AND_PUSH(ref_type, ref_type);
  9051. #endif
  9052. }
  9053. else {
  9054. #if WASM_ENABLE_FAST_INTERP != 0
  9055. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  9056. #endif
  9057. PUSH_TYPE(VALUE_TYPE_ANY);
  9058. }
  9059. break;
  9060. }
  9061. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9062. case WASM_OP_SELECT_T:
  9063. {
  9064. uint8 vec_len, type;
  9065. #if WASM_ENABLE_GC != 0
  9066. WASMRefType *ref_type = NULL;
  9067. #endif
  9068. #if WASM_ENABLE_FAST_INTERP != 0
  9069. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  9070. #endif
  9071. read_leb_uint32(p, p_end, vec_len);
  9072. if (vec_len != 1) {
  9073. /* typed select must have exactly one result */
  9074. set_error_buf(error_buf, error_buf_size,
  9075. "invalid result arity");
  9076. goto fail;
  9077. }
  9078. #if WASM_ENABLE_GC == 0
  9079. CHECK_BUF(p, p_end, 1);
  9080. type = read_uint8(p);
  9081. if (!is_value_type(type)) {
  9082. set_error_buf(error_buf, error_buf_size,
  9083. "unknown value type");
  9084. goto fail;
  9085. }
  9086. #else
  9087. p_org = p + 1;
  9088. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  9089. &need_ref_type_map, &wasm_ref_type,
  9090. false, error_buf, error_buf_size)) {
  9091. goto fail;
  9092. }
  9093. type = wasm_ref_type.ref_type;
  9094. if (need_ref_type_map) {
  9095. if (!(ref_type = reftype_set_insert(
  9096. module->ref_type_set, &wasm_ref_type, error_buf,
  9097. error_buf_size))) {
  9098. goto fail;
  9099. }
  9100. }
  9101. #if WASM_ENABLE_FAST_INTERP == 0
  9102. while (p_org < p) {
  9103. #if WASM_ENABLE_DEBUG_INTERP != 0
  9104. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9105. error_buf_size)) {
  9106. goto fail;
  9107. }
  9108. #endif
  9109. /* Ignore extra bytes for interpreter */
  9110. *p_org++ = WASM_OP_NOP;
  9111. }
  9112. #endif
  9113. #endif /* end of WASM_ENABLE_GC == 0 */
  9114. POP_I32();
  9115. #if WASM_ENABLE_FAST_INTERP != 0
  9116. if (loader_ctx->p_code_compiled) {
  9117. uint8 opcode_tmp = WASM_OP_SELECT;
  9118. if (type == VALUE_TYPE_V128) {
  9119. #if (WASM_ENABLE_SIMD == 0) \
  9120. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  9121. set_error_buf(error_buf, error_buf_size,
  9122. "SIMD v128 type isn't supported");
  9123. goto fail;
  9124. #endif
  9125. }
  9126. else {
  9127. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  9128. opcode_tmp = WASM_OP_SELECT_64;
  9129. #if WASM_ENABLE_GC != 0
  9130. if (wasm_is_type_reftype(type))
  9131. opcode_tmp = WASM_OP_SELECT_T;
  9132. #endif
  9133. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  9134. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  9135. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  9136. handle_table[opcode_tmp];
  9137. #else
  9138. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  9139. - (uint8 *)handle_table[0]);
  9140. if (!(offset >= INT16_MIN && offset < INT16_MAX)) {
  9141. set_error_buf(
  9142. error_buf, error_buf_size,
  9143. "pre-compiled label offset out of range");
  9144. goto fail;
  9145. }
  9146. *(int16 *)(p_code_compiled_tmp - sizeof(int16)) =
  9147. (int16)offset;
  9148. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  9149. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  9150. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  9151. *(p_code_compiled_tmp - 1) = opcode_tmp;
  9152. #else
  9153. *(p_code_compiled_tmp - 2) = opcode_tmp;
  9154. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  9155. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  9156. }
  9157. }
  9158. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  9159. POP_REF(type);
  9160. #if WASM_ENABLE_GC != 0
  9161. if (need_ref_type_map) {
  9162. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  9163. wasm_reftype_struct_size(ref_type));
  9164. }
  9165. #endif
  9166. POP_REF(type);
  9167. #if WASM_ENABLE_GC != 0
  9168. if (need_ref_type_map) {
  9169. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  9170. wasm_reftype_struct_size(ref_type));
  9171. }
  9172. #endif
  9173. PUSH_REF(type);
  9174. (void)vec_len;
  9175. break;
  9176. }
  9177. /* table.get x. tables[x]. [i32] -> [t] */
  9178. /* table.set x. tables[x]. [i32 t] -> [] */
  9179. case WASM_OP_TABLE_GET:
  9180. case WASM_OP_TABLE_SET:
  9181. {
  9182. uint8 decl_ref_type;
  9183. #if WASM_ENABLE_GC != 0
  9184. WASMRefType *ref_type;
  9185. #endif
  9186. read_leb_uint32(p, p_end, table_idx);
  9187. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  9188. #if WASM_ENABLE_GC != 0
  9189. (void **)&ref_type,
  9190. #else
  9191. NULL,
  9192. #endif
  9193. error_buf, error_buf_size))
  9194. goto fail;
  9195. #if WASM_ENABLE_GC != 0
  9196. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  9197. bh_assert(ref_type);
  9198. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  9199. ref_type, wasm_reftype_struct_size(ref_type));
  9200. }
  9201. #endif
  9202. #if WASM_ENABLE_FAST_INTERP != 0
  9203. emit_uint32(loader_ctx, table_idx);
  9204. #endif
  9205. if (opcode == WASM_OP_TABLE_GET) {
  9206. POP_I32();
  9207. #if WASM_ENABLE_FAST_INTERP != 0
  9208. PUSH_OFFSET_TYPE(decl_ref_type);
  9209. #endif
  9210. PUSH_TYPE(decl_ref_type);
  9211. }
  9212. else {
  9213. #if WASM_ENABLE_FAST_INTERP != 0
  9214. POP_OFFSET_TYPE(decl_ref_type);
  9215. #endif
  9216. POP_TYPE(decl_ref_type);
  9217. POP_I32();
  9218. }
  9219. break;
  9220. }
  9221. case WASM_OP_REF_NULL:
  9222. {
  9223. uint8 ref_type;
  9224. CHECK_BUF(p, p_end, 1);
  9225. ref_type = read_uint8(p);
  9226. #if WASM_ENABLE_GC == 0
  9227. if (ref_type != VALUE_TYPE_FUNCREF
  9228. && ref_type != VALUE_TYPE_EXTERNREF) {
  9229. set_error_buf(error_buf, error_buf_size, "type mismatch");
  9230. goto fail;
  9231. }
  9232. #else
  9233. p--;
  9234. if (is_byte_a_type(ref_type)) {
  9235. p_org = p + 1;
  9236. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  9237. &need_ref_type_map, &wasm_ref_type,
  9238. false, error_buf, error_buf_size)) {
  9239. goto fail;
  9240. }
  9241. ref_type = wasm_ref_type.ref_type;
  9242. #if WASM_ENABLE_FAST_INTERP == 0
  9243. while (p_org < p) {
  9244. #if WASM_ENABLE_DEBUG_INTERP != 0
  9245. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9246. error_buf_size)) {
  9247. goto fail;
  9248. }
  9249. #endif
  9250. /* Ignore extra bytes for interpreter */
  9251. *p_org++ = WASM_OP_NOP;
  9252. }
  9253. #endif
  9254. }
  9255. else {
  9256. read_leb_uint32(p, p_end, type_idx);
  9257. if (!check_type_index(module, type_idx, error_buf,
  9258. error_buf_size)) {
  9259. goto fail;
  9260. }
  9261. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  9262. true, type_idx);
  9263. ref_type = wasm_ref_type.ref_type;
  9264. }
  9265. #endif /* end of WASM_ENABLE_GC == 0 */
  9266. #if WASM_ENABLE_FAST_INTERP != 0
  9267. PUSH_OFFSET_TYPE(ref_type);
  9268. #endif
  9269. PUSH_TYPE(ref_type);
  9270. break;
  9271. }
  9272. case WASM_OP_REF_IS_NULL:
  9273. {
  9274. #if WASM_ENABLE_GC == 0
  9275. #if WASM_ENABLE_FAST_INTERP != 0
  9276. if (!wasm_loader_pop_frame_ref_offset(loader_ctx,
  9277. VALUE_TYPE_FUNCREF,
  9278. error_buf, error_buf_size)
  9279. && !wasm_loader_pop_frame_ref_offset(
  9280. loader_ctx, VALUE_TYPE_EXTERNREF, error_buf,
  9281. error_buf_size)) {
  9282. goto fail;
  9283. }
  9284. #else
  9285. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  9286. error_buf, error_buf_size)
  9287. && !wasm_loader_pop_frame_ref(loader_ctx,
  9288. VALUE_TYPE_EXTERNREF,
  9289. error_buf, error_buf_size)) {
  9290. goto fail;
  9291. }
  9292. #endif
  9293. #else /* else of WASM_ENABLE_GC == 0 */
  9294. uint8 type;
  9295. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  9296. error_buf, error_buf_size)) {
  9297. goto fail;
  9298. }
  9299. #endif
  9300. PUSH_I32();
  9301. break;
  9302. }
  9303. case WASM_OP_REF_FUNC:
  9304. {
  9305. read_leb_uint32(p, p_end, func_idx);
  9306. if (!check_function_index(module, func_idx, error_buf,
  9307. error_buf_size)) {
  9308. goto fail;
  9309. }
  9310. /* Refer to a forward-declared function */
  9311. if (func_idx >= cur_func_idx + module->import_function_count) {
  9312. WASMTableSeg *table_seg = module->table_segments;
  9313. bool func_declared = false;
  9314. uint32 j;
  9315. /* Check whether the function is declared in table segs */
  9316. for (i = 0; i < module->table_seg_count; i++, table_seg++) {
  9317. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  9318. && wasm_elem_is_declarative(table_seg->mode)) {
  9319. for (j = 0; j < table_seg->function_count; j++) {
  9320. if (table_seg->func_indexes[j] == func_idx) {
  9321. func_declared = true;
  9322. break;
  9323. }
  9324. }
  9325. }
  9326. }
  9327. if (!func_declared) {
  9328. /* Check whether the function is exported */
  9329. for (i = 0; i < module->export_count; i++) {
  9330. if (module->exports[i].kind == EXPORT_KIND_FUNC
  9331. && module->exports[i].index == func_idx) {
  9332. func_declared = true;
  9333. break;
  9334. }
  9335. }
  9336. }
  9337. if (!func_declared) {
  9338. set_error_buf(error_buf, error_buf_size,
  9339. "undeclared function reference");
  9340. goto fail;
  9341. }
  9342. }
  9343. #if WASM_ENABLE_FAST_INTERP != 0
  9344. emit_uint32(loader_ctx, func_idx);
  9345. #endif
  9346. #if WASM_ENABLE_GC == 0
  9347. PUSH_FUNCREF();
  9348. #else
  9349. if (func_idx < module->import_function_count)
  9350. type_idx =
  9351. module->import_functions[func_idx].u.function.type_idx;
  9352. else
  9353. type_idx = module
  9354. ->functions[func_idx
  9355. - module->import_function_count]
  9356. ->type_idx;
  9357. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  9358. false, type_idx);
  9359. PUSH_REF(wasm_ref_type.ref_type);
  9360. #endif
  9361. break;
  9362. }
  9363. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  9364. #if WASM_ENABLE_GC != 0
  9365. case WASM_OP_REF_AS_NON_NULL:
  9366. case WASM_OP_BR_ON_NULL:
  9367. {
  9368. uint8 type;
  9369. WASMRefType ref_type;
  9370. /* POP (ref null ht) and get the converted (ref ht) */
  9371. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  9372. error_buf, error_buf_size)) {
  9373. goto fail;
  9374. }
  9375. if (opcode == WASM_OP_BR_ON_NULL) {
  9376. if (!(frame_csp_tmp =
  9377. check_branch_block(loader_ctx, &p, p_end, false,
  9378. error_buf, error_buf_size))) {
  9379. goto fail;
  9380. }
  9381. }
  9382. #if WASM_ENABLE_FAST_INTERP != 0
  9383. disable_emit = true;
  9384. #endif
  9385. /* PUSH the converted (ref ht) */
  9386. if (type != VALUE_TYPE_ANY) {
  9387. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  9388. sizeof(WASMRefType));
  9389. }
  9390. PUSH_REF(type);
  9391. break;
  9392. }
  9393. case WASM_OP_BR_ON_NON_NULL:
  9394. {
  9395. uint8 type;
  9396. WASMRefType ref_type;
  9397. uint32 available_stack_cell =
  9398. loader_ctx->stack_cell_num
  9399. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  9400. /* POP (ref null ht) and get the converted (ref ht) */
  9401. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  9402. error_buf, error_buf_size)) {
  9403. goto fail;
  9404. }
  9405. #if WASM_ENABLE_FAST_INTERP != 0
  9406. disable_emit = true;
  9407. #endif
  9408. /* Temporarily PUSH back (ref ht), check brach block and
  9409. then POP it */
  9410. if (available_stack_cell
  9411. > 0) { /* stack isn't in polymorphic state */
  9412. if (type != VALUE_TYPE_ANY) {
  9413. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  9414. &ref_type, sizeof(WASMRefType));
  9415. }
  9416. PUSH_REF(type);
  9417. }
  9418. if (!(frame_csp_tmp =
  9419. check_branch_block(loader_ctx, &p, p_end, false,
  9420. error_buf, error_buf_size))) {
  9421. goto fail;
  9422. }
  9423. if (available_stack_cell
  9424. > 0) { /* stack isn't in polymorphic state */
  9425. POP_REF(type);
  9426. #if WASM_ENABLE_FAST_INTERP != 0
  9427. /* Erase the opnd offset emitted by POP_REF() */
  9428. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  9429. #endif
  9430. }
  9431. break;
  9432. }
  9433. case WASM_OP_REF_EQ:
  9434. POP_REF(REF_TYPE_EQREF);
  9435. POP_REF(REF_TYPE_EQREF);
  9436. PUSH_I32();
  9437. break;
  9438. #endif /* end of WASM_ENABLE_GC != 0 */
  9439. case WASM_OP_GET_LOCAL:
  9440. {
  9441. p_org = p - 1;
  9442. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  9443. PUSH_TYPE(local_type);
  9444. #if WASM_ENABLE_GC != 0
  9445. /* Cannot get a non-nullable and unset local */
  9446. if (local_idx >= param_count
  9447. && wasm_is_reftype_htref_non_nullable(local_type)
  9448. && !wasm_loader_get_local_status(loader_ctx,
  9449. local_idx - param_count)) {
  9450. set_error_buf(error_buf, error_buf_size,
  9451. "uninitialized local");
  9452. goto fail;
  9453. }
  9454. #endif
  9455. #if WASM_ENABLE_FAST_INTERP != 0
  9456. /* Get Local is optimized out */
  9457. skip_label();
  9458. disable_emit = true;
  9459. operand_offset = local_offset;
  9460. PUSH_OFFSET_TYPE(local_type);
  9461. #else
  9462. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  9463. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  9464. if (local_offset < 0x80
  9465. #if WASM_ENABLE_GC != 0
  9466. && !wasm_is_type_reftype(local_type)
  9467. #endif
  9468. ) {
  9469. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  9470. if (is_32bit_type(local_type)) {
  9471. *p_org++ = (uint8)local_offset;
  9472. }
  9473. else {
  9474. *p_org++ = (uint8)(local_offset | 0x80);
  9475. }
  9476. while (p_org < p) {
  9477. *p_org++ = WASM_OP_NOP;
  9478. }
  9479. }
  9480. #endif
  9481. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  9482. break;
  9483. }
  9484. case WASM_OP_SET_LOCAL:
  9485. {
  9486. p_org = p - 1;
  9487. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  9488. POP_TYPE(local_type);
  9489. #if WASM_ENABLE_FAST_INTERP != 0
  9490. if (!(preserve_referenced_local(
  9491. loader_ctx, opcode, local_offset, local_type,
  9492. &preserve_local, error_buf, error_buf_size)))
  9493. goto fail;
  9494. if (local_offset < 256
  9495. #if WASM_ENABLE_GC != 0
  9496. && !wasm_is_type_reftype(local_type)
  9497. #endif
  9498. ) {
  9499. skip_label();
  9500. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  9501. if (loader_ctx->p_code_compiled)
  9502. STORE_U16(loader_ctx->p_code_compiled - 2,
  9503. local_offset);
  9504. loader_ctx->frame_offset--;
  9505. loader_ctx->dynamic_offset--;
  9506. }
  9507. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  9508. if (loader_ctx->p_code_compiled)
  9509. STORE_U16(loader_ctx->p_code_compiled - 2,
  9510. local_offset);
  9511. loader_ctx->frame_offset -= 2;
  9512. loader_ctx->dynamic_offset -= 2;
  9513. }
  9514. else {
  9515. if (is_32bit_type(local_type)) {
  9516. emit_label(EXT_OP_SET_LOCAL_FAST);
  9517. emit_byte(loader_ctx, (uint8)local_offset);
  9518. }
  9519. else {
  9520. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  9521. emit_byte(loader_ctx, (uint8)local_offset);
  9522. }
  9523. POP_OFFSET_TYPE(local_type);
  9524. }
  9525. }
  9526. else { /* local index larger than 255, reserve leb */
  9527. emit_uint32(loader_ctx, local_idx);
  9528. POP_OFFSET_TYPE(local_type);
  9529. }
  9530. #else
  9531. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  9532. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  9533. if (local_offset < 0x80
  9534. #if WASM_ENABLE_GC != 0
  9535. && !wasm_is_type_reftype(local_type)
  9536. #endif
  9537. ) {
  9538. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  9539. if (is_32bit_type(local_type)) {
  9540. *p_org++ = (uint8)local_offset;
  9541. }
  9542. else {
  9543. *p_org++ = (uint8)(local_offset | 0x80);
  9544. }
  9545. while (p_org < p) {
  9546. *p_org++ = WASM_OP_NOP;
  9547. }
  9548. }
  9549. #endif
  9550. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  9551. #if WASM_ENABLE_GC != 0
  9552. if (local_idx >= param_count) {
  9553. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  9554. }
  9555. #endif
  9556. break;
  9557. }
  9558. case WASM_OP_TEE_LOCAL:
  9559. {
  9560. p_org = p - 1;
  9561. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  9562. #if WASM_ENABLE_FAST_INTERP != 0
  9563. /* If the stack is in polymorphic state, do fake pop and push on
  9564. offset stack to keep the depth of offset stack to be the
  9565. same with ref stack */
  9566. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9567. if (cur_block->is_stack_polymorphic) {
  9568. POP_OFFSET_TYPE(local_type);
  9569. PUSH_OFFSET_TYPE(local_type);
  9570. }
  9571. #endif
  9572. POP_TYPE(local_type);
  9573. PUSH_TYPE(local_type);
  9574. #if WASM_ENABLE_FAST_INTERP != 0
  9575. if (!(preserve_referenced_local(
  9576. loader_ctx, opcode, local_offset, local_type,
  9577. &preserve_local, error_buf, error_buf_size)))
  9578. goto fail;
  9579. if (local_offset < 256
  9580. #if WASM_ENABLE_GC != 0
  9581. && !wasm_is_type_reftype(local_type)
  9582. #endif
  9583. ) {
  9584. skip_label();
  9585. if (is_32bit_type(local_type)) {
  9586. emit_label(EXT_OP_TEE_LOCAL_FAST);
  9587. emit_byte(loader_ctx, (uint8)local_offset);
  9588. }
  9589. else {
  9590. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  9591. emit_byte(loader_ctx, (uint8)local_offset);
  9592. }
  9593. }
  9594. else { /* local index larger than 255, reserve leb */
  9595. emit_uint32(loader_ctx, local_idx);
  9596. }
  9597. emit_operand(loader_ctx,
  9598. *(loader_ctx->frame_offset
  9599. - wasm_value_type_cell_num(local_type)));
  9600. #else
  9601. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  9602. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  9603. if (local_offset < 0x80
  9604. #if WASM_ENABLE_GC != 0
  9605. && !wasm_is_type_reftype(local_type)
  9606. #endif
  9607. ) {
  9608. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  9609. if (is_32bit_type(local_type)) {
  9610. *p_org++ = (uint8)local_offset;
  9611. }
  9612. else {
  9613. *p_org++ = (uint8)(local_offset | 0x80);
  9614. }
  9615. while (p_org < p) {
  9616. *p_org++ = WASM_OP_NOP;
  9617. }
  9618. }
  9619. #endif
  9620. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  9621. #if WASM_ENABLE_GC != 0
  9622. if (local_idx >= param_count) {
  9623. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  9624. }
  9625. #endif
  9626. break;
  9627. }
  9628. case WASM_OP_GET_GLOBAL:
  9629. {
  9630. #if WASM_ENABLE_GC != 0
  9631. WASMRefType *ref_type;
  9632. #endif
  9633. p_org = p - 1;
  9634. read_leb_uint32(p, p_end, global_idx);
  9635. if (global_idx >= global_count) {
  9636. set_error_buf(error_buf, error_buf_size, "unknown global");
  9637. goto fail;
  9638. }
  9639. global_type =
  9640. global_idx < module->import_global_count
  9641. ? module->import_globals[global_idx].u.global.type
  9642. : module
  9643. ->globals[global_idx
  9644. - module->import_global_count]
  9645. .type;
  9646. #if WASM_ENABLE_GC != 0
  9647. ref_type =
  9648. global_idx < module->import_global_count
  9649. ? module->import_globals[global_idx].u.global.ref_type
  9650. : module
  9651. ->globals[global_idx
  9652. - module->import_global_count]
  9653. .ref_type;
  9654. if (wasm_is_type_multi_byte_type(global_type)) {
  9655. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  9656. wasm_reftype_struct_size(ref_type));
  9657. }
  9658. #endif
  9659. PUSH_TYPE(global_type);
  9660. #if WASM_ENABLE_FAST_INTERP == 0
  9661. if (global_type == VALUE_TYPE_I64
  9662. || global_type == VALUE_TYPE_F64) {
  9663. #if WASM_ENABLE_DEBUG_INTERP != 0
  9664. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9665. error_buf_size)) {
  9666. goto fail;
  9667. }
  9668. #endif
  9669. *p_org = WASM_OP_GET_GLOBAL_64;
  9670. }
  9671. #else /* else of WASM_ENABLE_FAST_INTERP */
  9672. if (global_type == VALUE_TYPE_I64
  9673. || global_type == VALUE_TYPE_F64) {
  9674. skip_label();
  9675. emit_label(WASM_OP_GET_GLOBAL_64);
  9676. }
  9677. emit_uint32(loader_ctx, global_idx);
  9678. PUSH_OFFSET_TYPE(global_type);
  9679. #endif /* end of WASM_ENABLE_FAST_INTERP */
  9680. break;
  9681. }
  9682. case WASM_OP_SET_GLOBAL:
  9683. {
  9684. bool is_mutable = false;
  9685. #if WASM_ENABLE_GC != 0
  9686. WASMRefType *ref_type;
  9687. #endif
  9688. p_org = p - 1;
  9689. read_leb_uint32(p, p_end, global_idx);
  9690. if (global_idx >= global_count) {
  9691. set_error_buf(error_buf, error_buf_size, "unknown global");
  9692. goto fail;
  9693. }
  9694. is_mutable =
  9695. global_idx < module->import_global_count
  9696. ? module->import_globals[global_idx].u.global.is_mutable
  9697. : module
  9698. ->globals[global_idx
  9699. - module->import_global_count]
  9700. .is_mutable;
  9701. if (!is_mutable) {
  9702. #if WASM_ENABLE_GC == 0
  9703. set_error_buf(error_buf, error_buf_size,
  9704. "global is immutable");
  9705. #else
  9706. set_error_buf(error_buf, error_buf_size,
  9707. "immutable global");
  9708. #endif
  9709. goto fail;
  9710. }
  9711. global_type =
  9712. global_idx < module->import_global_count
  9713. ? module->import_globals[global_idx].u.global.type
  9714. : module
  9715. ->globals[global_idx
  9716. - module->import_global_count]
  9717. .type;
  9718. #if WASM_ENABLE_GC != 0
  9719. ref_type =
  9720. global_idx < module->import_global_count
  9721. ? module->import_globals[global_idx].u.global.ref_type
  9722. : module
  9723. ->globals[global_idx
  9724. - module->import_global_count]
  9725. .ref_type;
  9726. if (wasm_is_type_multi_byte_type(global_type)) {
  9727. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  9728. wasm_reftype_struct_size(ref_type));
  9729. }
  9730. #endif
  9731. POP_TYPE(global_type);
  9732. #if WASM_ENABLE_FAST_INTERP == 0
  9733. if (global_type == VALUE_TYPE_I64
  9734. || global_type == VALUE_TYPE_F64) {
  9735. #if WASM_ENABLE_DEBUG_INTERP != 0
  9736. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9737. error_buf_size)) {
  9738. goto fail;
  9739. }
  9740. #endif
  9741. *p_org = WASM_OP_SET_GLOBAL_64;
  9742. }
  9743. else if (module->aux_stack_size > 0
  9744. && global_idx == module->aux_stack_top_global_index) {
  9745. #if WASM_ENABLE_DEBUG_INTERP != 0
  9746. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9747. error_buf_size)) {
  9748. goto fail;
  9749. }
  9750. #endif
  9751. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  9752. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  9753. func->has_op_set_global_aux_stack = true;
  9754. #endif
  9755. }
  9756. #else /* else of WASM_ENABLE_FAST_INTERP */
  9757. if (global_type == VALUE_TYPE_I64
  9758. || global_type == VALUE_TYPE_F64) {
  9759. skip_label();
  9760. emit_label(WASM_OP_SET_GLOBAL_64);
  9761. }
  9762. else if (module->aux_stack_size > 0
  9763. && global_idx == module->aux_stack_top_global_index) {
  9764. skip_label();
  9765. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  9766. }
  9767. emit_uint32(loader_ctx, global_idx);
  9768. POP_OFFSET_TYPE(global_type);
  9769. #endif /* end of WASM_ENABLE_FAST_INTERP */
  9770. break;
  9771. }
  9772. /* load */
  9773. case WASM_OP_I32_LOAD:
  9774. case WASM_OP_I32_LOAD8_S:
  9775. case WASM_OP_I32_LOAD8_U:
  9776. case WASM_OP_I32_LOAD16_S:
  9777. case WASM_OP_I32_LOAD16_U:
  9778. case WASM_OP_I64_LOAD:
  9779. case WASM_OP_I64_LOAD8_S:
  9780. case WASM_OP_I64_LOAD8_U:
  9781. case WASM_OP_I64_LOAD16_S:
  9782. case WASM_OP_I64_LOAD16_U:
  9783. case WASM_OP_I64_LOAD32_S:
  9784. case WASM_OP_I64_LOAD32_U:
  9785. case WASM_OP_F32_LOAD:
  9786. case WASM_OP_F64_LOAD:
  9787. /* store */
  9788. case WASM_OP_I32_STORE:
  9789. case WASM_OP_I32_STORE8:
  9790. case WASM_OP_I32_STORE16:
  9791. case WASM_OP_I64_STORE:
  9792. case WASM_OP_I64_STORE8:
  9793. case WASM_OP_I64_STORE16:
  9794. case WASM_OP_I64_STORE32:
  9795. case WASM_OP_F32_STORE:
  9796. case WASM_OP_F64_STORE:
  9797. {
  9798. #if WASM_ENABLE_FAST_INTERP != 0
  9799. /* change F32/F64 into I32/I64 */
  9800. if (opcode == WASM_OP_F32_LOAD) {
  9801. skip_label();
  9802. emit_label(WASM_OP_I32_LOAD);
  9803. }
  9804. else if (opcode == WASM_OP_F64_LOAD) {
  9805. skip_label();
  9806. emit_label(WASM_OP_I64_LOAD);
  9807. }
  9808. else if (opcode == WASM_OP_F32_STORE) {
  9809. skip_label();
  9810. emit_label(WASM_OP_I32_STORE);
  9811. }
  9812. else if (opcode == WASM_OP_F64_STORE) {
  9813. skip_label();
  9814. emit_label(WASM_OP_I64_STORE);
  9815. }
  9816. #endif
  9817. CHECK_MEMORY();
  9818. read_leb_uint32(p, p_end, align); /* align */
  9819. read_leb_uint32(p, p_end, mem_offset); /* offset */
  9820. if (!check_memory_access_align(opcode, align, error_buf,
  9821. error_buf_size)) {
  9822. goto fail;
  9823. }
  9824. #if WASM_ENABLE_FAST_INTERP != 0
  9825. emit_uint32(loader_ctx, mem_offset);
  9826. #endif
  9827. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  9828. func->has_memory_operations = true;
  9829. #endif
  9830. switch (opcode) {
  9831. /* load */
  9832. case WASM_OP_I32_LOAD:
  9833. case WASM_OP_I32_LOAD8_S:
  9834. case WASM_OP_I32_LOAD8_U:
  9835. case WASM_OP_I32_LOAD16_S:
  9836. case WASM_OP_I32_LOAD16_U:
  9837. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  9838. break;
  9839. case WASM_OP_I64_LOAD:
  9840. case WASM_OP_I64_LOAD8_S:
  9841. case WASM_OP_I64_LOAD8_U:
  9842. case WASM_OP_I64_LOAD16_S:
  9843. case WASM_OP_I64_LOAD16_U:
  9844. case WASM_OP_I64_LOAD32_S:
  9845. case WASM_OP_I64_LOAD32_U:
  9846. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  9847. break;
  9848. case WASM_OP_F32_LOAD:
  9849. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  9850. break;
  9851. case WASM_OP_F64_LOAD:
  9852. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  9853. break;
  9854. /* store */
  9855. case WASM_OP_I32_STORE:
  9856. case WASM_OP_I32_STORE8:
  9857. case WASM_OP_I32_STORE16:
  9858. POP_I32();
  9859. POP_I32();
  9860. break;
  9861. case WASM_OP_I64_STORE:
  9862. case WASM_OP_I64_STORE8:
  9863. case WASM_OP_I64_STORE16:
  9864. case WASM_OP_I64_STORE32:
  9865. POP_I64();
  9866. POP_I32();
  9867. break;
  9868. case WASM_OP_F32_STORE:
  9869. POP_F32();
  9870. POP_I32();
  9871. break;
  9872. case WASM_OP_F64_STORE:
  9873. POP_F64();
  9874. POP_I32();
  9875. break;
  9876. default:
  9877. break;
  9878. }
  9879. break;
  9880. }
  9881. case WASM_OP_MEMORY_SIZE:
  9882. CHECK_MEMORY();
  9883. /* reserved byte 0x00 */
  9884. if (*p++ != 0x00) {
  9885. set_error_buf(error_buf, error_buf_size,
  9886. "zero byte expected");
  9887. goto fail;
  9888. }
  9889. PUSH_I32();
  9890. module->possible_memory_grow = true;
  9891. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  9892. func->has_memory_operations = true;
  9893. #endif
  9894. break;
  9895. case WASM_OP_MEMORY_GROW:
  9896. CHECK_MEMORY();
  9897. /* reserved byte 0x00 */
  9898. if (*p++ != 0x00) {
  9899. set_error_buf(error_buf, error_buf_size,
  9900. "zero byte expected");
  9901. goto fail;
  9902. }
  9903. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  9904. module->possible_memory_grow = true;
  9905. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  9906. || WASM_ENABLE_WAMR_COMPILER != 0
  9907. func->has_op_memory_grow = true;
  9908. #endif
  9909. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  9910. func->has_memory_operations = true;
  9911. #endif
  9912. break;
  9913. case WASM_OP_I32_CONST:
  9914. read_leb_int32(p, p_end, i32_const);
  9915. #if WASM_ENABLE_FAST_INTERP != 0
  9916. skip_label();
  9917. disable_emit = true;
  9918. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  9919. if (operand_offset == 0) {
  9920. disable_emit = false;
  9921. emit_label(WASM_OP_I32_CONST);
  9922. emit_uint32(loader_ctx, i32_const);
  9923. }
  9924. #else
  9925. (void)i32_const;
  9926. #endif
  9927. PUSH_I32();
  9928. break;
  9929. case WASM_OP_I64_CONST:
  9930. read_leb_int64(p, p_end, i64_const);
  9931. #if WASM_ENABLE_FAST_INTERP != 0
  9932. skip_label();
  9933. disable_emit = true;
  9934. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  9935. if (operand_offset == 0) {
  9936. disable_emit = false;
  9937. emit_label(WASM_OP_I64_CONST);
  9938. emit_uint64(loader_ctx, i64_const);
  9939. }
  9940. #endif
  9941. PUSH_I64();
  9942. break;
  9943. case WASM_OP_F32_CONST:
  9944. p += sizeof(float32);
  9945. #if WASM_ENABLE_FAST_INTERP != 0
  9946. skip_label();
  9947. disable_emit = true;
  9948. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  9949. sizeof(float32));
  9950. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  9951. if (operand_offset == 0) {
  9952. disable_emit = false;
  9953. emit_label(WASM_OP_F32_CONST);
  9954. emit_float32(loader_ctx, f32_const);
  9955. }
  9956. #endif
  9957. PUSH_F32();
  9958. break;
  9959. case WASM_OP_F64_CONST:
  9960. p += sizeof(float64);
  9961. #if WASM_ENABLE_FAST_INTERP != 0
  9962. skip_label();
  9963. disable_emit = true;
  9964. /* Some MCU may require 8-byte align */
  9965. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  9966. sizeof(float64));
  9967. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  9968. if (operand_offset == 0) {
  9969. disable_emit = false;
  9970. emit_label(WASM_OP_F64_CONST);
  9971. emit_float64(loader_ctx, f64_const);
  9972. }
  9973. #endif
  9974. PUSH_F64();
  9975. break;
  9976. case WASM_OP_I32_EQZ:
  9977. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  9978. break;
  9979. case WASM_OP_I32_EQ:
  9980. case WASM_OP_I32_NE:
  9981. case WASM_OP_I32_LT_S:
  9982. case WASM_OP_I32_LT_U:
  9983. case WASM_OP_I32_GT_S:
  9984. case WASM_OP_I32_GT_U:
  9985. case WASM_OP_I32_LE_S:
  9986. case WASM_OP_I32_LE_U:
  9987. case WASM_OP_I32_GE_S:
  9988. case WASM_OP_I32_GE_U:
  9989. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  9990. break;
  9991. case WASM_OP_I64_EQZ:
  9992. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  9993. break;
  9994. case WASM_OP_I64_EQ:
  9995. case WASM_OP_I64_NE:
  9996. case WASM_OP_I64_LT_S:
  9997. case WASM_OP_I64_LT_U:
  9998. case WASM_OP_I64_GT_S:
  9999. case WASM_OP_I64_GT_U:
  10000. case WASM_OP_I64_LE_S:
  10001. case WASM_OP_I64_LE_U:
  10002. case WASM_OP_I64_GE_S:
  10003. case WASM_OP_I64_GE_U:
  10004. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  10005. break;
  10006. case WASM_OP_F32_EQ:
  10007. case WASM_OP_F32_NE:
  10008. case WASM_OP_F32_LT:
  10009. case WASM_OP_F32_GT:
  10010. case WASM_OP_F32_LE:
  10011. case WASM_OP_F32_GE:
  10012. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  10013. break;
  10014. case WASM_OP_F64_EQ:
  10015. case WASM_OP_F64_NE:
  10016. case WASM_OP_F64_LT:
  10017. case WASM_OP_F64_GT:
  10018. case WASM_OP_F64_LE:
  10019. case WASM_OP_F64_GE:
  10020. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  10021. break;
  10022. case WASM_OP_I32_CLZ:
  10023. case WASM_OP_I32_CTZ:
  10024. case WASM_OP_I32_POPCNT:
  10025. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  10026. break;
  10027. case WASM_OP_I32_ADD:
  10028. case WASM_OP_I32_SUB:
  10029. case WASM_OP_I32_MUL:
  10030. case WASM_OP_I32_DIV_S:
  10031. case WASM_OP_I32_DIV_U:
  10032. case WASM_OP_I32_REM_S:
  10033. case WASM_OP_I32_REM_U:
  10034. case WASM_OP_I32_AND:
  10035. case WASM_OP_I32_OR:
  10036. case WASM_OP_I32_XOR:
  10037. case WASM_OP_I32_SHL:
  10038. case WASM_OP_I32_SHR_S:
  10039. case WASM_OP_I32_SHR_U:
  10040. case WASM_OP_I32_ROTL:
  10041. case WASM_OP_I32_ROTR:
  10042. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  10043. break;
  10044. case WASM_OP_I64_CLZ:
  10045. case WASM_OP_I64_CTZ:
  10046. case WASM_OP_I64_POPCNT:
  10047. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  10048. break;
  10049. case WASM_OP_I64_ADD:
  10050. case WASM_OP_I64_SUB:
  10051. case WASM_OP_I64_MUL:
  10052. case WASM_OP_I64_DIV_S:
  10053. case WASM_OP_I64_DIV_U:
  10054. case WASM_OP_I64_REM_S:
  10055. case WASM_OP_I64_REM_U:
  10056. case WASM_OP_I64_AND:
  10057. case WASM_OP_I64_OR:
  10058. case WASM_OP_I64_XOR:
  10059. case WASM_OP_I64_SHL:
  10060. case WASM_OP_I64_SHR_S:
  10061. case WASM_OP_I64_SHR_U:
  10062. case WASM_OP_I64_ROTL:
  10063. case WASM_OP_I64_ROTR:
  10064. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  10065. break;
  10066. case WASM_OP_F32_ABS:
  10067. case WASM_OP_F32_NEG:
  10068. case WASM_OP_F32_CEIL:
  10069. case WASM_OP_F32_FLOOR:
  10070. case WASM_OP_F32_TRUNC:
  10071. case WASM_OP_F32_NEAREST:
  10072. case WASM_OP_F32_SQRT:
  10073. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  10074. break;
  10075. case WASM_OP_F32_ADD:
  10076. case WASM_OP_F32_SUB:
  10077. case WASM_OP_F32_MUL:
  10078. case WASM_OP_F32_DIV:
  10079. case WASM_OP_F32_MIN:
  10080. case WASM_OP_F32_MAX:
  10081. case WASM_OP_F32_COPYSIGN:
  10082. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  10083. break;
  10084. case WASM_OP_F64_ABS:
  10085. case WASM_OP_F64_NEG:
  10086. case WASM_OP_F64_CEIL:
  10087. case WASM_OP_F64_FLOOR:
  10088. case WASM_OP_F64_TRUNC:
  10089. case WASM_OP_F64_NEAREST:
  10090. case WASM_OP_F64_SQRT:
  10091. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  10092. break;
  10093. case WASM_OP_F64_ADD:
  10094. case WASM_OP_F64_SUB:
  10095. case WASM_OP_F64_MUL:
  10096. case WASM_OP_F64_DIV:
  10097. case WASM_OP_F64_MIN:
  10098. case WASM_OP_F64_MAX:
  10099. case WASM_OP_F64_COPYSIGN:
  10100. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  10101. break;
  10102. case WASM_OP_I32_WRAP_I64:
  10103. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  10104. break;
  10105. case WASM_OP_I32_TRUNC_S_F32:
  10106. case WASM_OP_I32_TRUNC_U_F32:
  10107. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  10108. break;
  10109. case WASM_OP_I32_TRUNC_S_F64:
  10110. case WASM_OP_I32_TRUNC_U_F64:
  10111. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  10112. break;
  10113. case WASM_OP_I64_EXTEND_S_I32:
  10114. case WASM_OP_I64_EXTEND_U_I32:
  10115. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  10116. break;
  10117. case WASM_OP_I64_TRUNC_S_F32:
  10118. case WASM_OP_I64_TRUNC_U_F32:
  10119. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  10120. break;
  10121. case WASM_OP_I64_TRUNC_S_F64:
  10122. case WASM_OP_I64_TRUNC_U_F64:
  10123. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  10124. break;
  10125. case WASM_OP_F32_CONVERT_S_I32:
  10126. case WASM_OP_F32_CONVERT_U_I32:
  10127. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  10128. break;
  10129. case WASM_OP_F32_CONVERT_S_I64:
  10130. case WASM_OP_F32_CONVERT_U_I64:
  10131. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  10132. break;
  10133. case WASM_OP_F32_DEMOTE_F64:
  10134. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  10135. break;
  10136. case WASM_OP_F64_CONVERT_S_I32:
  10137. case WASM_OP_F64_CONVERT_U_I32:
  10138. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  10139. break;
  10140. case WASM_OP_F64_CONVERT_S_I64:
  10141. case WASM_OP_F64_CONVERT_U_I64:
  10142. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  10143. break;
  10144. case WASM_OP_F64_PROMOTE_F32:
  10145. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  10146. break;
  10147. case WASM_OP_I32_REINTERPRET_F32:
  10148. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  10149. break;
  10150. case WASM_OP_I64_REINTERPRET_F64:
  10151. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  10152. break;
  10153. case WASM_OP_F32_REINTERPRET_I32:
  10154. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  10155. break;
  10156. case WASM_OP_F64_REINTERPRET_I64:
  10157. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  10158. break;
  10159. case WASM_OP_I32_EXTEND8_S:
  10160. case WASM_OP_I32_EXTEND16_S:
  10161. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  10162. break;
  10163. case WASM_OP_I64_EXTEND8_S:
  10164. case WASM_OP_I64_EXTEND16_S:
  10165. case WASM_OP_I64_EXTEND32_S:
  10166. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  10167. break;
  10168. #if WASM_ENABLE_GC != 0
  10169. case WASM_OP_GC_PREFIX:
  10170. {
  10171. uint32 opcode1;
  10172. read_leb_uint32(p, p_end, opcode1);
  10173. #if WASM_ENABLE_FAST_INTERP != 0
  10174. emit_byte(loader_ctx, ((uint8)opcode1));
  10175. #endif
  10176. switch (opcode1) {
  10177. case WASM_OP_STRUCT_NEW_CANON:
  10178. case WASM_OP_STRUCT_NEW_CANON_DEFAULT:
  10179. {
  10180. read_leb_uint32(p, p_end, type_idx);
  10181. #if WASM_ENABLE_FAST_INTERP != 0
  10182. emit_uint32(loader_ctx, type_idx);
  10183. #endif
  10184. if (!check_type_index(module, type_idx, error_buf,
  10185. error_buf_size)) {
  10186. goto fail;
  10187. }
  10188. if (module->types[type_idx]->type_flag
  10189. != WASM_TYPE_STRUCT) {
  10190. set_error_buf(error_buf, error_buf_size,
  10191. "unkown struct type");
  10192. goto fail;
  10193. }
  10194. if (opcode1 == WASM_OP_STRUCT_NEW_CANON) {
  10195. int32 j, k;
  10196. uint8 value_type;
  10197. uint32 ref_type_struct_size;
  10198. WASMStructType *struct_type =
  10199. (WASMStructType *)module->types[type_idx];
  10200. k = struct_type->ref_type_map_count - 1;
  10201. for (j = struct_type->field_count - 1; j >= 0;
  10202. j--) {
  10203. value_type = struct_type->fields[j].field_type;
  10204. if (wasm_is_type_reftype(value_type)) {
  10205. if (wasm_is_type_multi_byte_type(
  10206. value_type)) {
  10207. ref_type_struct_size =
  10208. wasm_reftype_struct_size(
  10209. struct_type->ref_type_maps[k]
  10210. .ref_type);
  10211. bh_memcpy_s(
  10212. &wasm_ref_type,
  10213. (uint32)sizeof(WASMRefType),
  10214. struct_type->ref_type_maps[k]
  10215. .ref_type,
  10216. ref_type_struct_size);
  10217. k--;
  10218. }
  10219. POP_REF(value_type);
  10220. }
  10221. else {
  10222. switch (value_type) {
  10223. case VALUE_TYPE_I32:
  10224. case PACKED_TYPE_I8:
  10225. case PACKED_TYPE_I16:
  10226. POP_I32();
  10227. break;
  10228. case VALUE_TYPE_I64:
  10229. POP_I64();
  10230. break;
  10231. case VALUE_TYPE_F32:
  10232. POP_F32();
  10233. break;
  10234. case VALUE_TYPE_F64:
  10235. POP_F64();
  10236. break;
  10237. default:
  10238. set_error_buf(error_buf,
  10239. error_buf_size,
  10240. "unknown type");
  10241. goto fail;
  10242. }
  10243. }
  10244. }
  10245. }
  10246. /* PUSH struct obj, (ref $t) */
  10247. wasm_set_refheaptype_typeidx(
  10248. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  10249. PUSH_REF(wasm_ref_type.ref_type);
  10250. break;
  10251. }
  10252. case WASM_OP_STRUCT_GET:
  10253. case WASM_OP_STRUCT_GET_S:
  10254. case WASM_OP_STRUCT_GET_U:
  10255. case WASM_OP_STRUCT_SET:
  10256. {
  10257. WASMStructType *struct_type;
  10258. WASMRefType *ref_type = NULL;
  10259. uint32 field_idx;
  10260. uint8 field_type;
  10261. read_leb_uint32(p, p_end, type_idx);
  10262. #if WASM_ENABLE_FAST_INTERP != 0
  10263. emit_uint32(loader_ctx, type_idx);
  10264. #endif
  10265. if (!check_type_index(module, type_idx, error_buf,
  10266. error_buf_size)) {
  10267. goto fail;
  10268. }
  10269. if (module->types[type_idx]->type_flag
  10270. != WASM_TYPE_STRUCT) {
  10271. set_error_buf(error_buf, error_buf_size,
  10272. "unknown struct type");
  10273. goto fail;
  10274. }
  10275. struct_type = (WASMStructType *)module->types[type_idx];
  10276. read_leb_uint32(p, p_end, field_idx);
  10277. #if WASM_ENABLE_FAST_INTERP != 0
  10278. emit_uint32(loader_ctx, field_idx);
  10279. #endif
  10280. if (field_idx >= struct_type->field_count) {
  10281. set_error_buf(error_buf, error_buf_size,
  10282. "unknown struct field");
  10283. goto fail;
  10284. }
  10285. if (opcode1 == WASM_OP_STRUCT_SET
  10286. && !(struct_type->fields[field_idx].field_flags
  10287. & 1)) {
  10288. set_error_buf(error_buf, error_buf_size,
  10289. "field is immutable");
  10290. goto fail;
  10291. }
  10292. field_type = struct_type->fields[field_idx].field_type;
  10293. if (is_packed_type(field_type)) {
  10294. if (opcode1 != WASM_OP_STRUCT_GET_S
  10295. && opcode1 != WASM_OP_STRUCT_GET_U) {
  10296. set_error_buf(error_buf, error_buf_size,
  10297. "type mismatch");
  10298. goto fail;
  10299. }
  10300. else {
  10301. field_type = VALUE_TYPE_I32;
  10302. }
  10303. }
  10304. if (wasm_is_type_multi_byte_type(field_type)) {
  10305. ref_type = wasm_reftype_map_find(
  10306. struct_type->ref_type_maps,
  10307. struct_type->ref_type_map_count, field_idx);
  10308. bh_assert(ref_type);
  10309. }
  10310. if (opcode1 == WASM_OP_STRUCT_SET) {
  10311. /* POP field */
  10312. if (wasm_is_type_multi_byte_type(field_type)) {
  10313. bh_memcpy_s(&wasm_ref_type,
  10314. (uint32)sizeof(WASMRefType),
  10315. ref_type,
  10316. wasm_reftype_struct_size(ref_type));
  10317. }
  10318. POP_REF(field_type);
  10319. /* POP struct obj, (ref null $t) */
  10320. wasm_set_refheaptype_typeidx(
  10321. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  10322. POP_REF(wasm_ref_type.ref_type);
  10323. }
  10324. else {
  10325. /* POP struct obj, (ref null $t) */
  10326. wasm_set_refheaptype_typeidx(
  10327. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  10328. POP_REF(wasm_ref_type.ref_type);
  10329. /* PUSH field */
  10330. if (wasm_is_type_multi_byte_type(field_type)) {
  10331. bh_memcpy_s(&wasm_ref_type,
  10332. (uint32)sizeof(WASMRefType),
  10333. ref_type,
  10334. wasm_reftype_struct_size(ref_type));
  10335. }
  10336. PUSH_REF(field_type);
  10337. }
  10338. break;
  10339. }
  10340. case WASM_OP_ARRAY_NEW_CANON:
  10341. case WASM_OP_ARRAY_NEW_CANON_DEFAULT:
  10342. case WASM_OP_ARRAY_NEW_CANON_FIXED:
  10343. case WASM_OP_ARRAY_NEW_CANON_DATA:
  10344. case WASM_OP_ARRAY_NEW_CANON_ELEM:
  10345. {
  10346. WASMArrayType *array_type;
  10347. uint8 elem_type;
  10348. uint32 u32 = 0;
  10349. read_leb_uint32(p, p_end, type_idx);
  10350. #if WASM_ENABLE_FAST_INTERP != 0
  10351. emit_uint32(loader_ctx, type_idx);
  10352. #endif
  10353. if (opcode1 == WASM_OP_ARRAY_NEW_CANON_FIXED
  10354. || opcode1 == WASM_OP_ARRAY_NEW_CANON_DATA
  10355. || opcode1 == WASM_OP_ARRAY_NEW_CANON_ELEM) {
  10356. read_leb_uint32(p, p_end, u32);
  10357. #if WASM_ENABLE_FAST_INTERP != 0
  10358. emit_uint32(loader_ctx, u32);
  10359. #endif
  10360. }
  10361. if (!check_array_type(module, type_idx, error_buf,
  10362. error_buf_size)) {
  10363. goto fail;
  10364. }
  10365. if (opcode1 != WASM_OP_ARRAY_NEW_CANON_FIXED) {
  10366. /* length */
  10367. POP_I32();
  10368. }
  10369. array_type = (WASMArrayType *)module->types[type_idx];
  10370. elem_type = array_type->elem_type;
  10371. if (opcode1 == WASM_OP_ARRAY_NEW_CANON
  10372. || opcode1 == WASM_OP_ARRAY_NEW_CANON_FIXED) {
  10373. if (wasm_is_type_multi_byte_type(elem_type)) {
  10374. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10375. array_type->elem_ref_type,
  10376. wasm_reftype_struct_size(
  10377. array_type->elem_ref_type));
  10378. }
  10379. if (is_packed_type(elem_type)) {
  10380. elem_type = VALUE_TYPE_I32;
  10381. }
  10382. if (opcode1 == WASM_OP_ARRAY_NEW_CANON_FIXED) {
  10383. uint32 N = u32;
  10384. for (i = 0; i < N; i++) {
  10385. if (wasm_is_type_multi_byte_type(
  10386. elem_type)) {
  10387. bh_memcpy_s(
  10388. &wasm_ref_type, sizeof(WASMRefType),
  10389. array_type->elem_ref_type,
  10390. wasm_reftype_struct_size(
  10391. array_type->elem_ref_type));
  10392. }
  10393. POP_REF(elem_type);
  10394. }
  10395. }
  10396. else
  10397. POP_REF(elem_type);
  10398. }
  10399. else if (opcode1 == WASM_OP_ARRAY_NEW_CANON_DATA) {
  10400. /* offset of data segment */
  10401. POP_I32();
  10402. if (u32 >= module->data_seg_count) {
  10403. set_error_buf(error_buf, error_buf_size,
  10404. "unknown data segement");
  10405. goto fail;
  10406. }
  10407. if (wasm_is_type_reftype(elem_type)) {
  10408. set_error_buf(error_buf, error_buf_size,
  10409. "array elem type mismatch");
  10410. goto fail;
  10411. }
  10412. }
  10413. else if (opcode1 == WASM_OP_ARRAY_NEW_CANON_ELEM) {
  10414. WASMTableSeg *table_seg =
  10415. module->table_segments + u32;
  10416. /* offset of element segment */
  10417. POP_I32();
  10418. if (u32 >= module->table_seg_count) {
  10419. set_error_buf(error_buf, error_buf_size,
  10420. "unknown element segement");
  10421. goto fail;
  10422. }
  10423. if (!wasm_reftype_is_subtype_of(
  10424. table_seg->elem_type,
  10425. table_seg->elem_ref_type, elem_type,
  10426. array_type->elem_ref_type, module->types,
  10427. module->type_count)) {
  10428. set_error_buf(error_buf, error_buf_size,
  10429. "array elem type mismatch");
  10430. goto fail;
  10431. }
  10432. }
  10433. /* PUSH array obj, (ref $t) */
  10434. wasm_set_refheaptype_typeidx(
  10435. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  10436. PUSH_REF(wasm_ref_type.ref_type);
  10437. break;
  10438. }
  10439. case WASM_OP_ARRAY_GET:
  10440. case WASM_OP_ARRAY_GET_S:
  10441. case WASM_OP_ARRAY_GET_U:
  10442. case WASM_OP_ARRAY_SET:
  10443. {
  10444. uint8 elem_type;
  10445. WASMArrayType *array_type;
  10446. WASMRefType *ref_type = NULL;
  10447. read_leb_uint32(p, p_end, type_idx);
  10448. #if WASM_ENABLE_FAST_INTERP != 0
  10449. emit_uint32(loader_ctx, type_idx);
  10450. #endif
  10451. if (!check_array_type(module, type_idx, error_buf,
  10452. error_buf_size)) {
  10453. goto fail;
  10454. }
  10455. array_type = (WASMArrayType *)module->types[type_idx];
  10456. if (opcode1 == WASM_OP_ARRAY_SET
  10457. && !(array_type->elem_flags & 1)) {
  10458. set_error_buf(error_buf, error_buf_size,
  10459. "array is immutable");
  10460. goto fail;
  10461. }
  10462. elem_type = array_type->elem_type;
  10463. if (is_packed_type(elem_type)) {
  10464. if (opcode1 != WASM_OP_ARRAY_GET_S
  10465. && opcode1 != WASM_OP_ARRAY_GET_U
  10466. && opcode1 != WASM_OP_ARRAY_SET) {
  10467. set_error_buf(error_buf, error_buf_size,
  10468. "type mismatch");
  10469. goto fail;
  10470. }
  10471. else {
  10472. elem_type = VALUE_TYPE_I32;
  10473. }
  10474. }
  10475. ref_type = array_type->elem_ref_type;
  10476. if (opcode1 == WASM_OP_ARRAY_SET) {
  10477. /* POP elem to set */
  10478. if (wasm_is_type_multi_byte_type(elem_type)) {
  10479. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10480. ref_type,
  10481. wasm_reftype_struct_size(ref_type));
  10482. }
  10483. POP_REF(elem_type);
  10484. }
  10485. /* elem idx */
  10486. POP_I32();
  10487. /* POP array obj, (ref null $t) */
  10488. wasm_set_refheaptype_typeidx(
  10489. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  10490. POP_REF(wasm_ref_type.ref_type);
  10491. if (opcode1 != WASM_OP_ARRAY_SET) {
  10492. /* PUSH elem */
  10493. if (wasm_is_type_multi_byte_type(elem_type)) {
  10494. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10495. ref_type,
  10496. wasm_reftype_struct_size(ref_type));
  10497. }
  10498. PUSH_REF(elem_type);
  10499. }
  10500. break;
  10501. }
  10502. #if WASM_ENABLE_GC_BINARYEN != 0
  10503. case WASM_OP_ARRAY_COPY:
  10504. {
  10505. uint32 src_type_idx;
  10506. WASMRefType src_ref_type = { 0 };
  10507. WASMRefType dst_ref_type = { 0 };
  10508. WASMArrayType *array_type;
  10509. /* typeidx1 */
  10510. read_leb_uint32(p, p_end, type_idx);
  10511. #if WASM_ENABLE_FAST_INTERP != 0
  10512. emit_uint32(loader_ctx, type_idx);
  10513. #endif
  10514. /* typeidx2 */
  10515. read_leb_uint32(p, p_end, src_type_idx);
  10516. #if WASM_ENABLE_FAST_INTERP != 0
  10517. emit_uint32(loader_ctx, src_type_idx);
  10518. #endif
  10519. if (!check_array_type(module, type_idx, error_buf,
  10520. error_buf_size)) {
  10521. goto fail;
  10522. }
  10523. if (!check_array_type(module, src_type_idx, error_buf,
  10524. error_buf_size)) {
  10525. goto fail;
  10526. }
  10527. POP_I32();
  10528. POP_I32();
  10529. /* POP array obj, (ref null $t) */
  10530. wasm_set_refheaptype_typeidx(
  10531. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  10532. POP_REF(wasm_ref_type.ref_type);
  10533. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  10534. &wasm_ref_type,
  10535. wasm_reftype_struct_size(&wasm_ref_type));
  10536. POP_I32();
  10537. /* POP array obj, (ref null $t) */
  10538. wasm_set_refheaptype_typeidx(
  10539. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  10540. POP_REF(wasm_ref_type.ref_type);
  10541. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  10542. &wasm_ref_type,
  10543. wasm_reftype_struct_size(&wasm_ref_type));
  10544. if (!wasm_type_is_subtype_of(
  10545. module->types[src_type_idx],
  10546. module->types[type_idx], module->types,
  10547. module->type_count)) {
  10548. set_error_buf(error_buf, error_buf_size,
  10549. "type mismatch");
  10550. goto fail;
  10551. }
  10552. array_type = (WASMArrayType *)module->types[type_idx];
  10553. if (!(array_type->elem_flags & 1)) {
  10554. set_error_buf(error_buf, error_buf_size,
  10555. "array is immutable");
  10556. goto fail;
  10557. }
  10558. break;
  10559. }
  10560. #endif
  10561. case WASM_OP_ARRAY_LEN:
  10562. {
  10563. POP_REF(REF_TYPE_ARRAYREF);
  10564. /* length */
  10565. PUSH_I32();
  10566. break;
  10567. }
  10568. case WASM_OP_I31_NEW:
  10569. {
  10570. POP_I32();
  10571. wasm_set_refheaptype_common(
  10572. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  10573. PUSH_REF(wasm_ref_type.ref_type);
  10574. break;
  10575. }
  10576. case WASM_OP_I31_GET_S:
  10577. case WASM_OP_I31_GET_U:
  10578. {
  10579. POP_REF(REF_TYPE_I31REF);
  10580. PUSH_I32();
  10581. break;
  10582. }
  10583. case WASM_OP_REF_TEST:
  10584. case WASM_OP_REF_CAST:
  10585. case WASM_OP_REF_TEST_NULLABLE:
  10586. case WASM_OP_REF_CAST_NULLABLE:
  10587. {
  10588. uint8 type;
  10589. read_leb_int32(p, p_end, heap_type);
  10590. #if WASM_ENABLE_FAST_INTERP != 0
  10591. emit_uint32(loader_ctx, (uint32)heap_type);
  10592. #endif
  10593. if (heap_type >= 0) {
  10594. if (!check_type_index(module, heap_type, error_buf,
  10595. error_buf_size)) {
  10596. goto fail;
  10597. }
  10598. }
  10599. else {
  10600. if (heap_type > HEAP_TYPE_FUNC
  10601. || heap_type < HEAP_TYPE_NONE) {
  10602. set_error_buf(error_buf, error_buf_size,
  10603. "unknown type");
  10604. goto fail;
  10605. }
  10606. }
  10607. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  10608. &wasm_ref_type, error_buf,
  10609. error_buf_size)) {
  10610. goto fail;
  10611. }
  10612. if (opcode1 == WASM_OP_REF_TEST
  10613. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  10614. PUSH_I32();
  10615. else {
  10616. bool nullable =
  10617. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  10618. : false;
  10619. wasm_set_refheaptype_typeidx(
  10620. &wasm_ref_type.ref_ht_typeidx, nullable,
  10621. heap_type);
  10622. PUSH_REF(wasm_ref_type.ref_type);
  10623. }
  10624. break;
  10625. }
  10626. case WASM_OP_BR_ON_CAST:
  10627. case WASM_OP_BR_ON_CAST_NULLABLE:
  10628. {
  10629. WASMRefType ref_type_tmp;
  10630. uint8 type_tmp;
  10631. uint32 depth;
  10632. bool nullable;
  10633. nullable = (opcode1 == WASM_OP_BR_ON_CAST_NULLABLE)
  10634. ? true
  10635. : false;
  10636. p_org = p;
  10637. read_leb_uint32(p, p_end, depth);
  10638. read_leb_int32(p, p_end, heap_type);
  10639. #if WASM_ENABLE_FAST_INTERP != 0
  10640. /* Emit heap_type firstly */
  10641. emit_uint32(loader_ctx, (uint32)heap_type);
  10642. #endif
  10643. (void)depth;
  10644. /* Pop and backup the stack top's ref type */
  10645. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  10646. &ref_type_tmp, error_buf,
  10647. error_buf_size)) {
  10648. goto fail;
  10649. }
  10650. if (heap_type >= 0) {
  10651. if (!check_type_index(module, heap_type, error_buf,
  10652. error_buf_size)) {
  10653. goto fail;
  10654. }
  10655. wasm_set_refheaptype_typeidx(
  10656. &wasm_ref_type.ref_ht_typeidx, nullable,
  10657. heap_type);
  10658. }
  10659. else {
  10660. if (heap_type > HEAP_TYPE_FUNC
  10661. || heap_type < HEAP_TYPE_NONE) {
  10662. set_error_buf(error_buf, error_buf_size,
  10663. "unknown type");
  10664. goto fail;
  10665. }
  10666. wasm_set_refheaptype_common(
  10667. &wasm_ref_type.ref_ht_common, nullable,
  10668. heap_type);
  10669. }
  10670. p = p_org;
  10671. /* Push ref type casted for branch block check */
  10672. PUSH_REF(wasm_ref_type.ref_type);
  10673. if (!(frame_csp_tmp = check_branch_block(
  10674. loader_ctx, &p, p_end, false, error_buf,
  10675. error_buf_size))) {
  10676. goto fail;
  10677. }
  10678. /* Ignore heap_type */
  10679. skip_leb_uint32(p, p_end);
  10680. /* Restore the original stack top's ref type */
  10681. POP_REF(wasm_ref_type.ref_type);
  10682. #if WASM_ENABLE_FAST_INTERP != 0
  10683. /* Erase the opnd offset emitted by POP_REF() */
  10684. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  10685. #endif
  10686. if (wasm_is_type_multi_byte_type(type_tmp)) {
  10687. bh_memcpy_s(
  10688. &wasm_ref_type,
  10689. wasm_reftype_struct_size(&ref_type_tmp),
  10690. &ref_type_tmp,
  10691. wasm_reftype_struct_size(&ref_type_tmp));
  10692. }
  10693. PUSH_REF(type_tmp);
  10694. #if WASM_ENABLE_FAST_INTERP != 0
  10695. /* Erase the opnd offset emitted by PUSH_REF() */
  10696. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  10697. #endif
  10698. break;
  10699. }
  10700. case WASM_OP_BR_ON_CAST_FAIL:
  10701. case WASM_OP_BR_ON_CAST_FAIL_NULLABLE:
  10702. {
  10703. uint8 type_tmp;
  10704. uint32 depth;
  10705. bool nullable;
  10706. nullable = (opcode1 == WASM_OP_BR_ON_CAST_FAIL_NULLABLE)
  10707. ? true
  10708. : false;
  10709. p_org = p;
  10710. read_leb_uint32(p, p_end, depth);
  10711. read_leb_int32(p, p_end, heap_type);
  10712. #if WASM_ENABLE_FAST_INTERP != 0
  10713. /* Emit heap_type firstly */
  10714. emit_uint32(loader_ctx, (uint32)heap_type);
  10715. #endif
  10716. (void)depth;
  10717. /* Pop and push the stack top's ref type so fast-interp
  10718. can get the opnd offset */
  10719. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  10720. &wasm_ref_type, error_buf,
  10721. error_buf_size)) {
  10722. goto fail;
  10723. }
  10724. p = p_org;
  10725. PUSH_REF(type_tmp);
  10726. if (!(frame_csp_tmp = check_branch_block(
  10727. loader_ctx, &p, p_end, false, error_buf,
  10728. error_buf_size))) {
  10729. goto fail;
  10730. }
  10731. /* Ignore heap_type */
  10732. skip_leb_uint32(p, p_end);
  10733. /* Pop the original ref type, and push the casted
  10734. ref type */
  10735. POP_REF(type_tmp);
  10736. #if WASM_ENABLE_FAST_INTERP != 0
  10737. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  10738. #endif
  10739. if (heap_type >= 0) {
  10740. if (!check_type_index(module, heap_type, error_buf,
  10741. error_buf_size)) {
  10742. goto fail;
  10743. }
  10744. wasm_set_refheaptype_typeidx(
  10745. &wasm_ref_type.ref_ht_typeidx, nullable,
  10746. heap_type);
  10747. }
  10748. else {
  10749. if (heap_type > HEAP_TYPE_FUNC
  10750. || heap_type < HEAP_TYPE_NONE) {
  10751. set_error_buf(error_buf, error_buf_size,
  10752. "unknown type");
  10753. goto fail;
  10754. }
  10755. wasm_set_refheaptype_common(
  10756. &wasm_ref_type.ref_ht_common, nullable,
  10757. heap_type);
  10758. }
  10759. PUSH_REF(wasm_ref_type.ref_type);
  10760. #if WASM_ENABLE_FAST_INTERP != 0
  10761. /* Erase the opnd offset emitted by PUSH_REF() */
  10762. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  10763. #endif
  10764. break;
  10765. }
  10766. case WASM_OP_EXTERN_INTERNALIZE:
  10767. {
  10768. uint8 type;
  10769. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  10770. &wasm_ref_type, error_buf,
  10771. error_buf_size)) {
  10772. goto fail;
  10773. }
  10774. if (!(type == REF_TYPE_EXTERNREF
  10775. || (type == REF_TYPE_HT_NON_NULLABLE
  10776. && wasm_ref_type.ref_ht_common.heap_type
  10777. == HEAP_TYPE_EXTERN)
  10778. || type == VALUE_TYPE_ANY)) {
  10779. set_error_buf(error_buf, error_buf_size,
  10780. "type mismatch");
  10781. goto fail;
  10782. }
  10783. if (type == REF_TYPE_EXTERNREF)
  10784. type = REF_TYPE_ANYREF;
  10785. else {
  10786. wasm_ref_type.ref_ht_common.heap_type =
  10787. HEAP_TYPE_ANY;
  10788. }
  10789. PUSH_REF(type);
  10790. break;
  10791. }
  10792. case WASM_OP_EXTERN_EXTERNALIZE:
  10793. {
  10794. uint8 type;
  10795. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  10796. &wasm_ref_type, error_buf,
  10797. error_buf_size)) {
  10798. goto fail;
  10799. }
  10800. if (type == REF_TYPE_EXTERNREF
  10801. || ((type == REF_TYPE_HT_NULLABLE
  10802. || type == REF_TYPE_HT_NON_NULLABLE)
  10803. && wasm_ref_type.ref_ht_common.heap_type
  10804. == HEAP_TYPE_EXTERN)) {
  10805. set_error_buf(error_buf, error_buf_size,
  10806. "type mismatch");
  10807. goto fail;
  10808. }
  10809. if (type != REF_TYPE_HT_NON_NULLABLE) {
  10810. /* push (ref null extern) */
  10811. type = REF_TYPE_EXTERNREF;
  10812. }
  10813. else {
  10814. /* push (ref extern) */
  10815. type = REF_TYPE_HT_NON_NULLABLE;
  10816. wasm_set_refheaptype_common(
  10817. &wasm_ref_type.ref_ht_common, false,
  10818. HEAP_TYPE_EXTERN);
  10819. }
  10820. PUSH_REF(type);
  10821. break;
  10822. }
  10823. default:
  10824. set_error_buf_v(error_buf, error_buf_size,
  10825. "%s %02x %02x", "unsupported opcode",
  10826. 0xfb, opcode1);
  10827. goto fail;
  10828. }
  10829. break;
  10830. }
  10831. #endif /* end of WASM_ENABLE_GC != 0 */
  10832. case WASM_OP_MISC_PREFIX:
  10833. {
  10834. uint32 opcode1;
  10835. read_leb_uint32(p, p_end, opcode1);
  10836. #if WASM_ENABLE_FAST_INTERP != 0
  10837. emit_byte(loader_ctx, ((uint8)opcode1));
  10838. #endif
  10839. switch (opcode1) {
  10840. case WASM_OP_I32_TRUNC_SAT_S_F32:
  10841. case WASM_OP_I32_TRUNC_SAT_U_F32:
  10842. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  10843. break;
  10844. case WASM_OP_I32_TRUNC_SAT_S_F64:
  10845. case WASM_OP_I32_TRUNC_SAT_U_F64:
  10846. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  10847. break;
  10848. case WASM_OP_I64_TRUNC_SAT_S_F32:
  10849. case WASM_OP_I64_TRUNC_SAT_U_F32:
  10850. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  10851. break;
  10852. case WASM_OP_I64_TRUNC_SAT_S_F64:
  10853. case WASM_OP_I64_TRUNC_SAT_U_F64:
  10854. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  10855. break;
  10856. #if WASM_ENABLE_BULK_MEMORY != 0
  10857. case WASM_OP_MEMORY_INIT:
  10858. {
  10859. read_leb_uint32(p, p_end, data_seg_idx);
  10860. #if WASM_ENABLE_FAST_INTERP != 0
  10861. emit_uint32(loader_ctx, data_seg_idx);
  10862. #endif
  10863. if (module->import_memory_count == 0
  10864. && module->memory_count == 0)
  10865. goto fail_unknown_memory;
  10866. if (*p++ != 0x00)
  10867. goto fail_zero_byte_expected;
  10868. if (data_seg_idx >= module->data_seg_count) {
  10869. set_error_buf_v(error_buf, error_buf_size,
  10870. "unknown data segment %d",
  10871. data_seg_idx);
  10872. goto fail;
  10873. }
  10874. if (module->data_seg_count1 == 0)
  10875. goto fail_data_cnt_sec_require;
  10876. POP_I32();
  10877. POP_I32();
  10878. POP_I32();
  10879. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10880. func->has_memory_operations = true;
  10881. #endif
  10882. break;
  10883. }
  10884. case WASM_OP_DATA_DROP:
  10885. {
  10886. read_leb_uint32(p, p_end, data_seg_idx);
  10887. #if WASM_ENABLE_FAST_INTERP != 0
  10888. emit_uint32(loader_ctx, data_seg_idx);
  10889. #endif
  10890. if (data_seg_idx >= module->data_seg_count) {
  10891. set_error_buf(error_buf, error_buf_size,
  10892. "unknown data segment");
  10893. goto fail;
  10894. }
  10895. if (module->data_seg_count1 == 0)
  10896. goto fail_data_cnt_sec_require;
  10897. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10898. func->has_memory_operations = true;
  10899. #endif
  10900. break;
  10901. }
  10902. case WASM_OP_MEMORY_COPY:
  10903. {
  10904. /* both src and dst memory index should be 0 */
  10905. if (*(int16 *)p != 0x0000)
  10906. goto fail_zero_byte_expected;
  10907. p += 2;
  10908. if (module->import_memory_count == 0
  10909. && module->memory_count == 0)
  10910. goto fail_unknown_memory;
  10911. POP_I32();
  10912. POP_I32();
  10913. POP_I32();
  10914. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10915. func->has_memory_operations = true;
  10916. #endif
  10917. break;
  10918. }
  10919. case WASM_OP_MEMORY_FILL:
  10920. {
  10921. if (*p++ != 0x00) {
  10922. goto fail_zero_byte_expected;
  10923. }
  10924. if (module->import_memory_count == 0
  10925. && module->memory_count == 0) {
  10926. goto fail_unknown_memory;
  10927. }
  10928. POP_I32();
  10929. POP_I32();
  10930. POP_I32();
  10931. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10932. func->has_memory_operations = true;
  10933. #endif
  10934. break;
  10935. }
  10936. fail_zero_byte_expected:
  10937. set_error_buf(error_buf, error_buf_size,
  10938. "zero byte expected");
  10939. goto fail;
  10940. fail_unknown_memory:
  10941. set_error_buf(error_buf, error_buf_size,
  10942. "unknown memory 0");
  10943. goto fail;
  10944. fail_data_cnt_sec_require:
  10945. set_error_buf(error_buf, error_buf_size,
  10946. "data count section required");
  10947. goto fail;
  10948. #endif /* WASM_ENABLE_BULK_MEMORY */
  10949. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10950. case WASM_OP_TABLE_INIT:
  10951. {
  10952. uint8 seg_type = 0, tbl_type = 0;
  10953. #if WASM_ENABLE_GC != 0
  10954. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  10955. #endif
  10956. read_leb_uint32(p, p_end, table_seg_idx);
  10957. read_leb_uint32(p, p_end, table_idx);
  10958. if (!get_table_elem_type(module, table_idx, &tbl_type,
  10959. #if WASM_ENABLE_GC != 0
  10960. (void **)&tbl_ref_type,
  10961. #else
  10962. NULL,
  10963. #endif
  10964. error_buf, error_buf_size))
  10965. goto fail;
  10966. if (!get_table_seg_elem_type(module, table_seg_idx,
  10967. &seg_type,
  10968. #if WASM_ENABLE_GC != 0
  10969. (void **)&seg_ref_type,
  10970. #else
  10971. NULL,
  10972. #endif
  10973. error_buf, error_buf_size))
  10974. goto fail;
  10975. #if WASM_ENABLE_GC == 0
  10976. if (seg_type != tbl_type) {
  10977. set_error_buf(error_buf, error_buf_size,
  10978. "type mismatch");
  10979. goto fail;
  10980. }
  10981. #else
  10982. if (!wasm_reftype_is_subtype_of(
  10983. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  10984. module->types, module->type_count)) {
  10985. set_error_buf(error_buf, error_buf_size,
  10986. "type mismatch");
  10987. goto fail;
  10988. }
  10989. #endif
  10990. #if WASM_ENABLE_FAST_INTERP != 0
  10991. emit_uint32(loader_ctx, table_seg_idx);
  10992. emit_uint32(loader_ctx, table_idx);
  10993. #endif
  10994. POP_I32();
  10995. POP_I32();
  10996. POP_I32();
  10997. break;
  10998. }
  10999. case WASM_OP_ELEM_DROP:
  11000. {
  11001. read_leb_uint32(p, p_end, table_seg_idx);
  11002. if (!get_table_seg_elem_type(module, table_seg_idx,
  11003. NULL, NULL, error_buf,
  11004. error_buf_size))
  11005. goto fail;
  11006. #if WASM_ENABLE_FAST_INTERP != 0
  11007. emit_uint32(loader_ctx, table_seg_idx);
  11008. #endif
  11009. break;
  11010. }
  11011. case WASM_OP_TABLE_COPY:
  11012. {
  11013. uint8 src_type, dst_type;
  11014. #if WASM_ENABLE_GC != 0
  11015. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  11016. #endif
  11017. uint32 src_tbl_idx, dst_tbl_idx;
  11018. read_leb_uint32(p, p_end, dst_tbl_idx);
  11019. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  11020. #if WASM_ENABLE_GC != 0
  11021. (void **)&dst_ref_type,
  11022. #else
  11023. NULL,
  11024. #endif
  11025. error_buf, error_buf_size))
  11026. goto fail;
  11027. read_leb_uint32(p, p_end, src_tbl_idx);
  11028. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  11029. #if WASM_ENABLE_GC != 0
  11030. (void **)&src_ref_type,
  11031. #else
  11032. NULL,
  11033. #endif
  11034. error_buf, error_buf_size))
  11035. goto fail;
  11036. #if WASM_ENABLE_GC == 0
  11037. if (src_type != dst_type) {
  11038. set_error_buf(error_buf, error_buf_size,
  11039. "type mismatch");
  11040. goto fail;
  11041. }
  11042. #else
  11043. if (!wasm_reftype_is_subtype_of(
  11044. src_type, src_ref_type, dst_type, dst_ref_type,
  11045. module->types, module->type_count)) {
  11046. set_error_buf(error_buf, error_buf_size,
  11047. "type mismatch");
  11048. goto fail;
  11049. }
  11050. #endif
  11051. #if WASM_ENABLE_FAST_INTERP != 0
  11052. emit_uint32(loader_ctx, dst_tbl_idx);
  11053. emit_uint32(loader_ctx, src_tbl_idx);
  11054. #endif
  11055. POP_I32();
  11056. POP_I32();
  11057. POP_I32();
  11058. break;
  11059. }
  11060. case WASM_OP_TABLE_SIZE:
  11061. {
  11062. read_leb_uint32(p, p_end, table_idx);
  11063. /* TODO: shall we create a new function to check
  11064. table idx instead of using below function? */
  11065. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  11066. error_buf, error_buf_size))
  11067. goto fail;
  11068. #if WASM_ENABLE_FAST_INTERP != 0
  11069. emit_uint32(loader_ctx, table_idx);
  11070. #endif
  11071. PUSH_I32();
  11072. break;
  11073. }
  11074. case WASM_OP_TABLE_GROW:
  11075. case WASM_OP_TABLE_FILL:
  11076. {
  11077. uint8 decl_type;
  11078. #if WASM_ENABLE_GC != 0
  11079. WASMRefType *ref_type = NULL;
  11080. #endif
  11081. read_leb_uint32(p, p_end, table_idx);
  11082. if (!get_table_elem_type(module, table_idx, &decl_type,
  11083. #if WASM_ENABLE_GC != 0
  11084. (void **)&ref_type,
  11085. #else
  11086. NULL,
  11087. #endif
  11088. error_buf, error_buf_size))
  11089. goto fail;
  11090. #if WASM_ENABLE_GC != 0
  11091. if (wasm_is_type_multi_byte_type(decl_type)) {
  11092. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11093. ref_type,
  11094. wasm_reftype_struct_size(ref_type));
  11095. }
  11096. #endif
  11097. if (opcode1 == WASM_OP_TABLE_GROW) {
  11098. if (table_idx < module->import_table_count) {
  11099. module->import_tables[table_idx]
  11100. .u.table.possible_grow = true;
  11101. }
  11102. else {
  11103. module
  11104. ->tables[table_idx
  11105. - module->import_table_count]
  11106. .possible_grow = true;
  11107. }
  11108. }
  11109. #if WASM_ENABLE_FAST_INTERP != 0
  11110. emit_uint32(loader_ctx, table_idx);
  11111. #endif
  11112. POP_I32();
  11113. #if WASM_ENABLE_FAST_INTERP != 0
  11114. POP_OFFSET_TYPE(decl_type);
  11115. #endif
  11116. POP_TYPE(decl_type);
  11117. if (opcode1 == WASM_OP_TABLE_GROW)
  11118. PUSH_I32();
  11119. else
  11120. POP_I32();
  11121. break;
  11122. }
  11123. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11124. default:
  11125. set_error_buf_v(error_buf, error_buf_size,
  11126. "%s %02x %02x", "unsupported opcode",
  11127. 0xfc, opcode1);
  11128. goto fail;
  11129. }
  11130. break;
  11131. }
  11132. #if WASM_ENABLE_SIMD != 0
  11133. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  11134. case WASM_OP_SIMD_PREFIX:
  11135. {
  11136. uint32 opcode1;
  11137. opcode1 = read_uint8(p);
  11138. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  11139. */
  11140. switch (opcode1) {
  11141. /* memory instruction */
  11142. case SIMD_v128_load:
  11143. case SIMD_v128_load8x8_s:
  11144. case SIMD_v128_load8x8_u:
  11145. case SIMD_v128_load16x4_s:
  11146. case SIMD_v128_load16x4_u:
  11147. case SIMD_v128_load32x2_s:
  11148. case SIMD_v128_load32x2_u:
  11149. case SIMD_v128_load8_splat:
  11150. case SIMD_v128_load16_splat:
  11151. case SIMD_v128_load32_splat:
  11152. case SIMD_v128_load64_splat:
  11153. {
  11154. CHECK_MEMORY();
  11155. read_leb_uint32(p, p_end, align); /* align */
  11156. if (!check_simd_memory_access_align(
  11157. opcode1, align, error_buf, error_buf_size)) {
  11158. goto fail;
  11159. }
  11160. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11161. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  11162. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11163. func->has_memory_operations = true;
  11164. #endif
  11165. break;
  11166. }
  11167. case SIMD_v128_store:
  11168. {
  11169. CHECK_MEMORY();
  11170. read_leb_uint32(p, p_end, align); /* align */
  11171. if (!check_simd_memory_access_align(
  11172. opcode1, align, error_buf, error_buf_size)) {
  11173. goto fail;
  11174. }
  11175. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11176. POP_V128();
  11177. POP_I32();
  11178. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11179. func->has_memory_operations = true;
  11180. #endif
  11181. break;
  11182. }
  11183. /* basic operation */
  11184. case SIMD_v128_const:
  11185. {
  11186. CHECK_BUF1(p, p_end, 16);
  11187. p += 16;
  11188. PUSH_V128();
  11189. break;
  11190. }
  11191. case SIMD_v8x16_shuffle:
  11192. {
  11193. V128 mask;
  11194. CHECK_BUF1(p, p_end, 16);
  11195. mask = read_i8x16(p, error_buf, error_buf_size);
  11196. p += 16;
  11197. if (!check_simd_shuffle_mask(mask, error_buf,
  11198. error_buf_size)) {
  11199. goto fail;
  11200. }
  11201. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11202. break;
  11203. }
  11204. case SIMD_v8x16_swizzle:
  11205. {
  11206. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11207. break;
  11208. }
  11209. /* splat operation */
  11210. case SIMD_i8x16_splat:
  11211. case SIMD_i16x8_splat:
  11212. case SIMD_i32x4_splat:
  11213. case SIMD_i64x2_splat:
  11214. case SIMD_f32x4_splat:
  11215. case SIMD_f64x2_splat:
  11216. {
  11217. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  11218. VALUE_TYPE_I32, VALUE_TYPE_I64,
  11219. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  11220. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  11221. VALUE_TYPE_V128);
  11222. break;
  11223. }
  11224. /* lane operation */
  11225. case SIMD_i8x16_extract_lane_s:
  11226. case SIMD_i8x16_extract_lane_u:
  11227. case SIMD_i8x16_replace_lane:
  11228. case SIMD_i16x8_extract_lane_s:
  11229. case SIMD_i16x8_extract_lane_u:
  11230. case SIMD_i16x8_replace_lane:
  11231. case SIMD_i32x4_extract_lane:
  11232. case SIMD_i32x4_replace_lane:
  11233. case SIMD_i64x2_extract_lane:
  11234. case SIMD_i64x2_replace_lane:
  11235. case SIMD_f32x4_extract_lane:
  11236. case SIMD_f32x4_replace_lane:
  11237. case SIMD_f64x2_extract_lane:
  11238. case SIMD_f64x2_replace_lane:
  11239. {
  11240. uint8 lane;
  11241. /* clang-format off */
  11242. uint8 replace[] = {
  11243. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  11244. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  11245. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  11246. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  11247. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  11248. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  11249. };
  11250. uint8 push_type[] = {
  11251. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  11252. VALUE_TYPE_V128,
  11253. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  11254. VALUE_TYPE_V128,
  11255. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  11256. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  11257. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  11258. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  11259. };
  11260. /* clang-format on */
  11261. CHECK_BUF(p, p_end, 1);
  11262. lane = read_uint8(p);
  11263. if (!check_simd_access_lane(opcode1, lane, error_buf,
  11264. error_buf_size)) {
  11265. goto fail;
  11266. }
  11267. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  11268. if (!(wasm_loader_pop_frame_ref(
  11269. loader_ctx,
  11270. replace[opcode1
  11271. - SIMD_i8x16_extract_lane_s],
  11272. error_buf, error_buf_size)))
  11273. goto fail;
  11274. }
  11275. POP_AND_PUSH(
  11276. VALUE_TYPE_V128,
  11277. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  11278. break;
  11279. }
  11280. /* i8x16 compare operation */
  11281. case SIMD_i8x16_eq:
  11282. case SIMD_i8x16_ne:
  11283. case SIMD_i8x16_lt_s:
  11284. case SIMD_i8x16_lt_u:
  11285. case SIMD_i8x16_gt_s:
  11286. case SIMD_i8x16_gt_u:
  11287. case SIMD_i8x16_le_s:
  11288. case SIMD_i8x16_le_u:
  11289. case SIMD_i8x16_ge_s:
  11290. case SIMD_i8x16_ge_u:
  11291. /* i16x8 compare operation */
  11292. case SIMD_i16x8_eq:
  11293. case SIMD_i16x8_ne:
  11294. case SIMD_i16x8_lt_s:
  11295. case SIMD_i16x8_lt_u:
  11296. case SIMD_i16x8_gt_s:
  11297. case SIMD_i16x8_gt_u:
  11298. case SIMD_i16x8_le_s:
  11299. case SIMD_i16x8_le_u:
  11300. case SIMD_i16x8_ge_s:
  11301. case SIMD_i16x8_ge_u:
  11302. /* i32x4 compare operation */
  11303. case SIMD_i32x4_eq:
  11304. case SIMD_i32x4_ne:
  11305. case SIMD_i32x4_lt_s:
  11306. case SIMD_i32x4_lt_u:
  11307. case SIMD_i32x4_gt_s:
  11308. case SIMD_i32x4_gt_u:
  11309. case SIMD_i32x4_le_s:
  11310. case SIMD_i32x4_le_u:
  11311. case SIMD_i32x4_ge_s:
  11312. case SIMD_i32x4_ge_u:
  11313. /* f32x4 compare operation */
  11314. case SIMD_f32x4_eq:
  11315. case SIMD_f32x4_ne:
  11316. case SIMD_f32x4_lt:
  11317. case SIMD_f32x4_gt:
  11318. case SIMD_f32x4_le:
  11319. case SIMD_f32x4_ge:
  11320. /* f64x2 compare operation */
  11321. case SIMD_f64x2_eq:
  11322. case SIMD_f64x2_ne:
  11323. case SIMD_f64x2_lt:
  11324. case SIMD_f64x2_gt:
  11325. case SIMD_f64x2_le:
  11326. case SIMD_f64x2_ge:
  11327. {
  11328. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11329. break;
  11330. }
  11331. /* v128 operation */
  11332. case SIMD_v128_not:
  11333. {
  11334. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11335. break;
  11336. }
  11337. case SIMD_v128_and:
  11338. case SIMD_v128_andnot:
  11339. case SIMD_v128_or:
  11340. case SIMD_v128_xor:
  11341. {
  11342. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11343. break;
  11344. }
  11345. case SIMD_v128_bitselect:
  11346. {
  11347. POP_V128();
  11348. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11349. break;
  11350. }
  11351. case SIMD_v128_any_true:
  11352. {
  11353. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  11354. break;
  11355. }
  11356. /* Load Lane Operation */
  11357. case SIMD_v128_load8_lane:
  11358. case SIMD_v128_load16_lane:
  11359. case SIMD_v128_load32_lane:
  11360. case SIMD_v128_load64_lane:
  11361. case SIMD_v128_store8_lane:
  11362. case SIMD_v128_store16_lane:
  11363. case SIMD_v128_store32_lane:
  11364. case SIMD_v128_store64_lane:
  11365. {
  11366. uint8 lane;
  11367. CHECK_MEMORY();
  11368. read_leb_uint32(p, p_end, align); /* align */
  11369. if (!check_simd_memory_access_align(
  11370. opcode1, align, error_buf, error_buf_size)) {
  11371. goto fail;
  11372. }
  11373. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11374. CHECK_BUF(p, p_end, 1);
  11375. lane = read_uint8(p);
  11376. if (!check_simd_access_lane(opcode1, lane, error_buf,
  11377. error_buf_size)) {
  11378. goto fail;
  11379. }
  11380. POP_V128();
  11381. POP_I32();
  11382. if (opcode1 < SIMD_v128_store8_lane) {
  11383. PUSH_V128();
  11384. }
  11385. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11386. func->has_memory_operations = true;
  11387. #endif
  11388. break;
  11389. }
  11390. case SIMD_v128_load32_zero:
  11391. case SIMD_v128_load64_zero:
  11392. {
  11393. CHECK_MEMORY();
  11394. read_leb_uint32(p, p_end, align); /* align */
  11395. if (!check_simd_memory_access_align(
  11396. opcode1, align, error_buf, error_buf_size)) {
  11397. goto fail;
  11398. }
  11399. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11400. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  11401. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11402. func->has_memory_operations = true;
  11403. #endif
  11404. break;
  11405. }
  11406. /* Float conversion */
  11407. case SIMD_f32x4_demote_f64x2_zero:
  11408. case SIMD_f64x2_promote_low_f32x4_zero:
  11409. {
  11410. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11411. break;
  11412. }
  11413. /* i8x16 Operation */
  11414. case SIMD_i8x16_abs:
  11415. case SIMD_i8x16_neg:
  11416. case SIMD_i8x16_popcnt:
  11417. {
  11418. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11419. break;
  11420. }
  11421. case SIMD_i8x16_all_true:
  11422. case SIMD_i8x16_bitmask:
  11423. {
  11424. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  11425. break;
  11426. }
  11427. case SIMD_i8x16_narrow_i16x8_s:
  11428. case SIMD_i8x16_narrow_i16x8_u:
  11429. {
  11430. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11431. break;
  11432. }
  11433. case SIMD_f32x4_ceil:
  11434. case SIMD_f32x4_floor:
  11435. case SIMD_f32x4_trunc:
  11436. case SIMD_f32x4_nearest:
  11437. {
  11438. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11439. break;
  11440. }
  11441. case SIMD_i8x16_shl:
  11442. case SIMD_i8x16_shr_s:
  11443. case SIMD_i8x16_shr_u:
  11444. {
  11445. POP_I32();
  11446. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11447. break;
  11448. }
  11449. case SIMD_i8x16_add:
  11450. case SIMD_i8x16_add_sat_s:
  11451. case SIMD_i8x16_add_sat_u:
  11452. case SIMD_i8x16_sub:
  11453. case SIMD_i8x16_sub_sat_s:
  11454. case SIMD_i8x16_sub_sat_u:
  11455. {
  11456. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11457. break;
  11458. }
  11459. case SIMD_f64x2_ceil:
  11460. case SIMD_f64x2_floor:
  11461. {
  11462. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11463. break;
  11464. }
  11465. case SIMD_i8x16_min_s:
  11466. case SIMD_i8x16_min_u:
  11467. case SIMD_i8x16_max_s:
  11468. case SIMD_i8x16_max_u:
  11469. {
  11470. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11471. break;
  11472. }
  11473. case SIMD_f64x2_trunc:
  11474. {
  11475. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11476. break;
  11477. }
  11478. case SIMD_i8x16_avgr_u:
  11479. {
  11480. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11481. break;
  11482. }
  11483. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  11484. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  11485. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  11486. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  11487. /* i16x8 operation */
  11488. case SIMD_i16x8_abs:
  11489. case SIMD_i16x8_neg:
  11490. {
  11491. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11492. break;
  11493. }
  11494. case SIMD_i16x8_q15mulr_sat_s:
  11495. {
  11496. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11497. break;
  11498. }
  11499. case SIMD_i16x8_all_true:
  11500. case SIMD_i16x8_bitmask:
  11501. {
  11502. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  11503. break;
  11504. }
  11505. case SIMD_i16x8_narrow_i32x4_s:
  11506. case SIMD_i16x8_narrow_i32x4_u:
  11507. {
  11508. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11509. break;
  11510. }
  11511. case SIMD_i16x8_extend_low_i8x16_s:
  11512. case SIMD_i16x8_extend_high_i8x16_s:
  11513. case SIMD_i16x8_extend_low_i8x16_u:
  11514. case SIMD_i16x8_extend_high_i8x16_u:
  11515. {
  11516. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11517. break;
  11518. }
  11519. case SIMD_i16x8_shl:
  11520. case SIMD_i16x8_shr_s:
  11521. case SIMD_i16x8_shr_u:
  11522. {
  11523. POP_I32();
  11524. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11525. break;
  11526. }
  11527. case SIMD_i16x8_add:
  11528. case SIMD_i16x8_add_sat_s:
  11529. case SIMD_i16x8_add_sat_u:
  11530. case SIMD_i16x8_sub:
  11531. case SIMD_i16x8_sub_sat_s:
  11532. case SIMD_i16x8_sub_sat_u:
  11533. {
  11534. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11535. break;
  11536. }
  11537. case SIMD_f64x2_nearest:
  11538. {
  11539. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11540. break;
  11541. }
  11542. case SIMD_i16x8_mul:
  11543. case SIMD_i16x8_min_s:
  11544. case SIMD_i16x8_min_u:
  11545. case SIMD_i16x8_max_s:
  11546. case SIMD_i16x8_max_u:
  11547. case SIMD_i16x8_avgr_u:
  11548. case SIMD_i16x8_extmul_low_i8x16_s:
  11549. case SIMD_i16x8_extmul_high_i8x16_s:
  11550. case SIMD_i16x8_extmul_low_i8x16_u:
  11551. case SIMD_i16x8_extmul_high_i8x16_u:
  11552. {
  11553. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11554. break;
  11555. }
  11556. /* i32x4 operation */
  11557. case SIMD_i32x4_abs:
  11558. case SIMD_i32x4_neg:
  11559. {
  11560. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11561. break;
  11562. }
  11563. case SIMD_i32x4_all_true:
  11564. case SIMD_i32x4_bitmask:
  11565. {
  11566. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  11567. break;
  11568. }
  11569. case SIMD_i32x4_narrow_i64x2_s:
  11570. case SIMD_i32x4_narrow_i64x2_u:
  11571. {
  11572. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11573. break;
  11574. }
  11575. case SIMD_i32x4_extend_low_i16x8_s:
  11576. case SIMD_i32x4_extend_high_i16x8_s:
  11577. case SIMD_i32x4_extend_low_i16x8_u:
  11578. case SIMD_i32x4_extend_high_i16x8_u:
  11579. {
  11580. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11581. break;
  11582. }
  11583. case SIMD_i32x4_shl:
  11584. case SIMD_i32x4_shr_s:
  11585. case SIMD_i32x4_shr_u:
  11586. {
  11587. POP_I32();
  11588. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11589. break;
  11590. }
  11591. case SIMD_i32x4_add:
  11592. case SIMD_i32x4_sub:
  11593. case SIMD_i32x4_mul:
  11594. case SIMD_i32x4_min_s:
  11595. case SIMD_i32x4_min_u:
  11596. case SIMD_i32x4_max_s:
  11597. case SIMD_i32x4_max_u:
  11598. case SIMD_i32x4_dot_i16x8_s:
  11599. case SIMD_i32x4_avgr_u:
  11600. case SIMD_i32x4_extmul_low_i16x8_s:
  11601. case SIMD_i32x4_extmul_high_i16x8_s:
  11602. case SIMD_i32x4_extmul_low_i16x8_u:
  11603. case SIMD_i32x4_extmul_high_i16x8_u:
  11604. {
  11605. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11606. break;
  11607. }
  11608. /* i64x2 operation */
  11609. case SIMD_i64x2_abs:
  11610. case SIMD_i64x2_neg:
  11611. {
  11612. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11613. break;
  11614. }
  11615. case SIMD_i64x2_all_true:
  11616. case SIMD_i64x2_bitmask:
  11617. {
  11618. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  11619. break;
  11620. }
  11621. case SIMD_i64x2_extend_low_i32x4_s:
  11622. case SIMD_i64x2_extend_high_i32x4_s:
  11623. case SIMD_i64x2_extend_low_i32x4_u:
  11624. case SIMD_i64x2_extend_high_i32x4_u:
  11625. {
  11626. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11627. break;
  11628. }
  11629. case SIMD_i64x2_shl:
  11630. case SIMD_i64x2_shr_s:
  11631. case SIMD_i64x2_shr_u:
  11632. {
  11633. POP_I32();
  11634. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11635. break;
  11636. }
  11637. case SIMD_i64x2_add:
  11638. case SIMD_i64x2_sub:
  11639. case SIMD_i64x2_mul:
  11640. case SIMD_i64x2_eq:
  11641. case SIMD_i64x2_ne:
  11642. case SIMD_i64x2_lt_s:
  11643. case SIMD_i64x2_gt_s:
  11644. case SIMD_i64x2_le_s:
  11645. case SIMD_i64x2_ge_s:
  11646. case SIMD_i64x2_extmul_low_i32x4_s:
  11647. case SIMD_i64x2_extmul_high_i32x4_s:
  11648. case SIMD_i64x2_extmul_low_i32x4_u:
  11649. case SIMD_i64x2_extmul_high_i32x4_u:
  11650. {
  11651. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11652. break;
  11653. }
  11654. /* f32x4 operation */
  11655. case SIMD_f32x4_abs:
  11656. case SIMD_f32x4_neg:
  11657. case SIMD_f32x4_round:
  11658. case SIMD_f32x4_sqrt:
  11659. {
  11660. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11661. break;
  11662. }
  11663. case SIMD_f32x4_add:
  11664. case SIMD_f32x4_sub:
  11665. case SIMD_f32x4_mul:
  11666. case SIMD_f32x4_div:
  11667. case SIMD_f32x4_min:
  11668. case SIMD_f32x4_max:
  11669. case SIMD_f32x4_pmin:
  11670. case SIMD_f32x4_pmax:
  11671. {
  11672. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11673. break;
  11674. }
  11675. /* f64x2 operation */
  11676. case SIMD_f64x2_abs:
  11677. case SIMD_f64x2_neg:
  11678. case SIMD_f64x2_round:
  11679. case SIMD_f64x2_sqrt:
  11680. {
  11681. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11682. break;
  11683. }
  11684. case SIMD_f64x2_add:
  11685. case SIMD_f64x2_sub:
  11686. case SIMD_f64x2_mul:
  11687. case SIMD_f64x2_div:
  11688. case SIMD_f64x2_min:
  11689. case SIMD_f64x2_max:
  11690. case SIMD_f64x2_pmin:
  11691. case SIMD_f64x2_pmax:
  11692. {
  11693. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11694. break;
  11695. }
  11696. case SIMD_i32x4_trunc_sat_f32x4_s:
  11697. case SIMD_i32x4_trunc_sat_f32x4_u:
  11698. case SIMD_f32x4_convert_i32x4_s:
  11699. case SIMD_f32x4_convert_i32x4_u:
  11700. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  11701. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  11702. case SIMD_f64x2_convert_low_i32x4_s:
  11703. case SIMD_f64x2_convert_low_i32x4_u:
  11704. {
  11705. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11706. break;
  11707. }
  11708. default:
  11709. {
  11710. if (error_buf != NULL) {
  11711. snprintf(error_buf, error_buf_size,
  11712. "WASM module load failed: "
  11713. "invalid opcode 0xfd %02x.",
  11714. opcode1);
  11715. }
  11716. goto fail;
  11717. }
  11718. }
  11719. break;
  11720. }
  11721. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  11722. #endif /* end of WASM_ENABLE_SIMD */
  11723. #if WASM_ENABLE_SHARED_MEMORY != 0
  11724. case WASM_OP_ATOMIC_PREFIX:
  11725. {
  11726. uint32 opcode1;
  11727. opcode1 = read_uint8(p);
  11728. #if WASM_ENABLE_FAST_INTERP != 0
  11729. emit_byte(loader_ctx, opcode1);
  11730. #endif
  11731. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  11732. CHECK_MEMORY();
  11733. read_leb_uint32(p, p_end, align); /* align */
  11734. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11735. if (!check_memory_align_equal(opcode1, align, error_buf,
  11736. error_buf_size)) {
  11737. goto fail;
  11738. }
  11739. #if WASM_ENABLE_FAST_INTERP != 0
  11740. emit_uint32(loader_ctx, mem_offset);
  11741. #endif
  11742. }
  11743. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11744. func->has_memory_operations = true;
  11745. #endif
  11746. switch (opcode1) {
  11747. case WASM_OP_ATOMIC_NOTIFY:
  11748. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11749. break;
  11750. case WASM_OP_ATOMIC_WAIT32:
  11751. POP_I64();
  11752. POP_I32();
  11753. POP_I32();
  11754. PUSH_I32();
  11755. break;
  11756. case WASM_OP_ATOMIC_WAIT64:
  11757. POP_I64();
  11758. POP_I64();
  11759. POP_I32();
  11760. PUSH_I32();
  11761. break;
  11762. case WASM_OP_ATOMIC_FENCE:
  11763. /* reserved byte 0x00 */
  11764. if (*p++ != 0x00) {
  11765. set_error_buf(error_buf, error_buf_size,
  11766. "zero byte expected");
  11767. goto fail;
  11768. }
  11769. break;
  11770. case WASM_OP_ATOMIC_I32_LOAD:
  11771. case WASM_OP_ATOMIC_I32_LOAD8_U:
  11772. case WASM_OP_ATOMIC_I32_LOAD16_U:
  11773. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11774. break;
  11775. case WASM_OP_ATOMIC_I32_STORE:
  11776. case WASM_OP_ATOMIC_I32_STORE8:
  11777. case WASM_OP_ATOMIC_I32_STORE16:
  11778. POP_I32();
  11779. POP_I32();
  11780. break;
  11781. case WASM_OP_ATOMIC_I64_LOAD:
  11782. case WASM_OP_ATOMIC_I64_LOAD8_U:
  11783. case WASM_OP_ATOMIC_I64_LOAD16_U:
  11784. case WASM_OP_ATOMIC_I64_LOAD32_U:
  11785. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  11786. break;
  11787. case WASM_OP_ATOMIC_I64_STORE:
  11788. case WASM_OP_ATOMIC_I64_STORE8:
  11789. case WASM_OP_ATOMIC_I64_STORE16:
  11790. case WASM_OP_ATOMIC_I64_STORE32:
  11791. POP_I64();
  11792. POP_I32();
  11793. break;
  11794. case WASM_OP_ATOMIC_RMW_I32_ADD:
  11795. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  11796. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  11797. case WASM_OP_ATOMIC_RMW_I32_SUB:
  11798. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  11799. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  11800. case WASM_OP_ATOMIC_RMW_I32_AND:
  11801. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  11802. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  11803. case WASM_OP_ATOMIC_RMW_I32_OR:
  11804. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  11805. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  11806. case WASM_OP_ATOMIC_RMW_I32_XOR:
  11807. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  11808. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  11809. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  11810. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  11811. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  11812. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11813. break;
  11814. case WASM_OP_ATOMIC_RMW_I64_ADD:
  11815. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  11816. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  11817. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  11818. case WASM_OP_ATOMIC_RMW_I64_SUB:
  11819. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  11820. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  11821. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  11822. case WASM_OP_ATOMIC_RMW_I64_AND:
  11823. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  11824. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  11825. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  11826. case WASM_OP_ATOMIC_RMW_I64_OR:
  11827. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  11828. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  11829. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  11830. case WASM_OP_ATOMIC_RMW_I64_XOR:
  11831. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  11832. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  11833. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  11834. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  11835. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  11836. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  11837. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  11838. POP_I64();
  11839. POP_I32();
  11840. PUSH_I64();
  11841. break;
  11842. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  11843. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  11844. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  11845. POP_I32();
  11846. POP_I32();
  11847. POP_I32();
  11848. PUSH_I32();
  11849. break;
  11850. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  11851. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  11852. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  11853. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  11854. POP_I64();
  11855. POP_I64();
  11856. POP_I32();
  11857. PUSH_I64();
  11858. break;
  11859. default:
  11860. set_error_buf_v(error_buf, error_buf_size,
  11861. "%s %02x %02x", "unsupported opcode",
  11862. 0xfe, opcode1);
  11863. goto fail;
  11864. }
  11865. break;
  11866. }
  11867. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  11868. default:
  11869. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  11870. "unsupported opcode", opcode);
  11871. goto fail;
  11872. }
  11873. #if WASM_ENABLE_FAST_INTERP != 0
  11874. last_op = opcode;
  11875. #endif
  11876. }
  11877. if (loader_ctx->csp_num > 0) {
  11878. if (cur_func_idx < module->function_count - 1)
  11879. /* Function with missing end marker (between two functions) */
  11880. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  11881. else
  11882. /* Function with missing end marker
  11883. (at EOF or end of code sections) */
  11884. set_error_buf(error_buf, error_buf_size,
  11885. "unexpected end of section or function, "
  11886. "or section size mismatch");
  11887. goto fail;
  11888. }
  11889. #if WASM_ENABLE_FAST_INTERP != 0
  11890. if (loader_ctx->p_code_compiled == NULL)
  11891. goto re_scan;
  11892. func->const_cell_num = loader_ctx->const_cell_num;
  11893. if (func->const_cell_num > 0) {
  11894. int32 j;
  11895. if (!(func->consts = func_const = loader_malloc(
  11896. func->const_cell_num * 4, error_buf, error_buf_size)))
  11897. goto fail;
  11898. func_const_end = func->consts + func->const_cell_num * 4;
  11899. /* reverse the const buf */
  11900. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  11901. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  11902. if (c->value_type == VALUE_TYPE_F64
  11903. || c->value_type == VALUE_TYPE_I64) {
  11904. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  11905. &(c->value.f64), (uint32)sizeof(int64));
  11906. func_const += sizeof(int64);
  11907. }
  11908. else {
  11909. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  11910. &(c->value.f32), (uint32)sizeof(int32));
  11911. func_const += sizeof(int32);
  11912. }
  11913. }
  11914. }
  11915. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  11916. - loader_ctx->start_dynamic_offset + 1;
  11917. #else
  11918. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  11919. #endif
  11920. func->max_block_num = loader_ctx->max_csp_num;
  11921. return_value = true;
  11922. fail:
  11923. wasm_loader_ctx_destroy(loader_ctx);
  11924. (void)table_idx;
  11925. (void)table_seg_idx;
  11926. (void)data_seg_idx;
  11927. (void)i64_const;
  11928. (void)local_offset;
  11929. (void)p_org;
  11930. (void)mem_offset;
  11931. (void)align;
  11932. return return_value;
  11933. }