wasm_loader.c 585 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840
  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 "wasm_loader_common.h"
  12. #include "../common/wasm_native.h"
  13. #include "../common/wasm_memory.h"
  14. #if WASM_ENABLE_GC != 0
  15. #include "../common/gc/gc_type.h"
  16. #include "../common/gc/gc_object.h"
  17. #endif
  18. #if WASM_ENABLE_DEBUG_INTERP != 0
  19. #include "../libraries/debug-engine/debug_engine.h"
  20. #endif
  21. #if WASM_ENABLE_FAST_JIT != 0
  22. #include "../fast-jit/jit_compiler.h"
  23. #include "../fast-jit/jit_codecache.h"
  24. #endif
  25. #if WASM_ENABLE_JIT != 0
  26. #include "../compilation/aot_llvm.h"
  27. #endif
  28. #ifndef TRACE_WASM_LOADER
  29. #define TRACE_WASM_LOADER 0
  30. #endif
  31. /* Read a value of given type from the address pointed to by the given
  32. pointer and increase the pointer to the position just after the
  33. value being read. */
  34. #define TEMPLATE_READ_VALUE(Type, p) \
  35. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  36. #if WASM_ENABLE_MEMORY64 != 0
  37. static bool
  38. has_module_memory64(WASMModule *module)
  39. {
  40. /* TODO: multi-memories for now assuming the memory idx type is consistent
  41. * across multi-memories */
  42. if (module->import_memory_count > 0)
  43. return !!(module->import_memories[0].u.memory.mem_type.flags
  44. & MEMORY64_FLAG);
  45. else if (module->memory_count > 0)
  46. return !!(module->memories[0].flags & MEMORY64_FLAG);
  47. return false;
  48. }
  49. #endif
  50. static void
  51. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  52. {
  53. wasm_loader_set_error_buf(error_buf, error_buf_size, string, false);
  54. }
  55. #if WASM_ENABLE_MEMORY64 != 0
  56. static void
  57. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  58. {
  59. if (error_buf != NULL) {
  60. snprintf(error_buf, error_buf_size, "offset out of range");
  61. }
  62. }
  63. #endif
  64. static void
  65. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  66. {
  67. va_list args;
  68. char buf[128];
  69. if (error_buf != NULL) {
  70. va_start(args, format);
  71. vsnprintf(buf, sizeof(buf), format, args);
  72. va_end(args);
  73. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  74. }
  75. }
  76. static bool
  77. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  78. char *error_buf, uint32 error_buf_size)
  79. {
  80. if ((uintptr_t)buf + length < (uintptr_t)buf
  81. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  82. set_error_buf(error_buf, error_buf_size,
  83. "unexpected end of section or function");
  84. return false;
  85. }
  86. return true;
  87. }
  88. static bool
  89. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  90. char *error_buf, uint32 error_buf_size)
  91. {
  92. if ((uintptr_t)buf + length < (uintptr_t)buf
  93. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  94. set_error_buf(error_buf, error_buf_size, "unexpected end");
  95. return false;
  96. }
  97. return true;
  98. }
  99. #define CHECK_BUF(buf, buf_end, length) \
  100. do { \
  101. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  102. goto fail; \
  103. } \
  104. } while (0)
  105. #define CHECK_BUF1(buf, buf_end, length) \
  106. do { \
  107. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  108. goto fail; \
  109. } \
  110. } while (0)
  111. #define skip_leb(p) while (*p++ & 0x80)
  112. #define skip_leb_int64(p, p_end) skip_leb(p)
  113. #define skip_leb_uint32(p, p_end) skip_leb(p)
  114. #define skip_leb_int32(p, p_end) skip_leb(p)
  115. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  116. #define skip_leb_memidx(p, p_end) skip_leb(p)
  117. #if WASM_ENABLE_MULTI_MEMORY == 0
  118. #define skip_leb_align(p, p_end) skip_leb(p)
  119. #else
  120. /* Skip the following memidx if applicable */
  121. #define skip_leb_align(p, p_end) \
  122. do { \
  123. if (*p++ & OPT_MEMIDX_FLAG) \
  124. skip_leb_uint32(p, p_end); \
  125. } while (0)
  126. #endif
  127. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  128. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  129. #define read_leb_int64(p, p_end, res) \
  130. do { \
  131. uint64 res64; \
  132. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  133. error_buf_size)) \
  134. goto fail; \
  135. res = (int64)res64; \
  136. } while (0)
  137. #if WASM_ENABLE_MEMORY64 != 0
  138. #define read_leb_mem_offset(p, p_end, res) \
  139. do { \
  140. uint64 res64; \
  141. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  142. &res64, error_buf, error_buf_size)) { \
  143. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  144. goto fail; \
  145. } \
  146. res = (mem_offset_t)res64; \
  147. } while (0)
  148. #else
  149. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  150. #endif
  151. #define read_leb_uint32(p, p_end, res) \
  152. do { \
  153. uint64 res64; \
  154. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  155. error_buf_size)) \
  156. goto fail; \
  157. res = (uint32)res64; \
  158. } while (0)
  159. #define read_leb_int32(p, p_end, res) \
  160. do { \
  161. uint64 res64; \
  162. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  163. error_buf_size)) \
  164. goto fail; \
  165. res = (int32)res64; \
  166. } while (0)
  167. #define read_leb_memidx(p, p_end, res) read_leb_uint32(p, p_end, res)
  168. #if WASM_ENABLE_MULTI_MEMORY != 0
  169. #define check_memidx(module, memidx) \
  170. do { \
  171. if (memidx >= module->import_memory_count + module->memory_count) { \
  172. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d", \
  173. memidx); \
  174. goto fail; \
  175. } \
  176. } while (0)
  177. /* Bit 6(0x40) indicating the optional memidx, and reset bit 6 for
  178. * alignment check */
  179. #define read_leb_memarg(p, p_end, res) \
  180. do { \
  181. read_leb_uint32(p, p_end, res); \
  182. if (res & OPT_MEMIDX_FLAG) { \
  183. res &= ~OPT_MEMIDX_FLAG; \
  184. read_leb_uint32(p, p_end, memidx); /* memidx */ \
  185. check_memidx(module, memidx); \
  186. } \
  187. } while (0)
  188. #else
  189. /* reserved byte 0x00 */
  190. #define check_memidx(module, memidx) \
  191. do { \
  192. (void)module; \
  193. if (memidx != 0) { \
  194. set_error_buf(error_buf, error_buf_size, "zero byte expected"); \
  195. goto fail; \
  196. } \
  197. } while (0)
  198. #define read_leb_memarg(p, p_end, res) read_leb_uint32(p, p_end, res)
  199. #endif
  200. static char *
  201. type2str(uint8 type)
  202. {
  203. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  204. #if WASM_ENABLE_GC != 0
  205. char *type_str_ref[] = { "stringview_iter",
  206. "stringview_wtf16",
  207. "(ref null ht)",
  208. "(ref ht)",
  209. "", /* reserved */
  210. "stringview_wtf8",
  211. "stringref",
  212. "", /* reserved */
  213. "", /* reserved */
  214. "arrayref",
  215. "structref",
  216. "i32ref",
  217. "eqref",
  218. "anyref",
  219. "externref",
  220. "funcref",
  221. "nullref",
  222. "nullexternref",
  223. "nullfuncref" };
  224. #endif
  225. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  226. return type_str[type - VALUE_TYPE_V128];
  227. #if WASM_ENABLE_GC != 0
  228. else if (wasm_is_type_reftype(type))
  229. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  230. #endif
  231. else if (type == VALUE_TYPE_FUNCREF)
  232. return "funcref";
  233. else if (type == VALUE_TYPE_EXTERNREF)
  234. return "externref";
  235. else
  236. return "unknown type";
  237. }
  238. static bool
  239. is_32bit_type(uint8 type)
  240. {
  241. if (type == VALUE_TYPE_I32
  242. || type == VALUE_TYPE_F32
  243. /* the operand stack is in polymorphic state */
  244. || type == VALUE_TYPE_ANY
  245. #if WASM_ENABLE_GC != 0
  246. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  247. #elif WASM_ENABLE_REF_TYPES != 0
  248. /* For reference types, we use uint32 index to represent
  249. the funcref and externref */
  250. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  251. #endif
  252. )
  253. return true;
  254. return false;
  255. }
  256. static bool
  257. is_64bit_type(uint8 type)
  258. {
  259. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  260. #if WASM_ENABLE_GC != 0
  261. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  262. #endif
  263. )
  264. return true;
  265. return false;
  266. }
  267. #if WASM_ENABLE_GC != 0
  268. static bool
  269. is_packed_type(uint8 type)
  270. {
  271. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  272. }
  273. #endif
  274. static bool
  275. is_byte_a_type(uint8 type)
  276. {
  277. return (is_valid_value_type_for_interpreter(type)
  278. || (type == VALUE_TYPE_VOID))
  279. ? true
  280. : false;
  281. }
  282. #if WASM_ENABLE_SIMD != 0
  283. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  284. static V128
  285. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  286. {
  287. V128 result;
  288. uint8 i;
  289. for (i = 0; i != 16; ++i) {
  290. result.i8x16[i] = read_uint8(p_buf);
  291. }
  292. return result;
  293. }
  294. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  295. #endif /* end of WASM_ENABLE_SIMD */
  296. static void *
  297. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  298. {
  299. void *mem;
  300. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  301. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  302. return NULL;
  303. }
  304. memset(mem, 0, (uint32)size);
  305. return mem;
  306. }
  307. static void *
  308. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  309. uint32 error_buf_size)
  310. {
  311. uint8 *mem_new;
  312. bh_assert(size_new > size_old);
  313. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  314. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  315. memset(mem_new + size_old, 0, size_new - size_old);
  316. wasm_runtime_free(mem_old);
  317. }
  318. return mem_new;
  319. }
  320. #define MEM_REALLOC(mem, size_old, size_new) \
  321. do { \
  322. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  323. error_buf_size); \
  324. if (!mem_new) \
  325. goto fail; \
  326. mem = mem_new; \
  327. } while (0)
  328. #if WASM_ENABLE_GC != 0
  329. static bool
  330. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  331. char *error_buf, uint32 error_buf_size)
  332. {
  333. if (type_index >= type_count) {
  334. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  335. type_index);
  336. return false;
  337. }
  338. return true;
  339. }
  340. static bool
  341. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  342. uint32 error_buf_size)
  343. {
  344. if (!check_type_index(module, module->type_count, type_index, error_buf,
  345. error_buf_size)) {
  346. return false;
  347. }
  348. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  349. set_error_buf(error_buf, error_buf_size, "unkown array type");
  350. return false;
  351. }
  352. return true;
  353. }
  354. #endif
  355. static bool
  356. check_function_index(const WASMModule *module, uint32 function_index,
  357. char *error_buf, uint32 error_buf_size)
  358. {
  359. if (function_index
  360. >= module->import_function_count + module->function_count) {
  361. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  362. function_index);
  363. return false;
  364. }
  365. return true;
  366. }
  367. typedef struct InitValue {
  368. uint8 type;
  369. uint8 flag;
  370. #if WASM_ENABLE_GC != 0
  371. uint8 gc_opcode;
  372. WASMRefType ref_type;
  373. #endif
  374. WASMValue value;
  375. } InitValue;
  376. typedef struct ConstExprContext {
  377. uint32 sp;
  378. uint32 size;
  379. WASMModule *module;
  380. InitValue *stack;
  381. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  382. } ConstExprContext;
  383. static void
  384. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  385. {
  386. ctx->sp = 0;
  387. ctx->module = module;
  388. ctx->stack = ctx->data;
  389. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  390. }
  391. static bool
  392. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  393. #if WASM_ENABLE_GC != 0
  394. WASMRefType *ref_type, uint8 gc_opcode,
  395. #endif
  396. WASMValue *value, char *error_buf, uint32 error_buf_size)
  397. {
  398. InitValue *cur_value;
  399. if (ctx->sp >= ctx->size) {
  400. if (ctx->stack != ctx->data) {
  401. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  402. (ctx->size + 4) * sizeof(InitValue));
  403. }
  404. else {
  405. if (!(ctx->stack =
  406. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  407. error_buf, error_buf_size))) {
  408. goto fail;
  409. }
  410. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  411. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  412. }
  413. ctx->size += 4;
  414. }
  415. cur_value = &ctx->stack[ctx->sp++];
  416. cur_value->type = type;
  417. cur_value->flag = flag;
  418. cur_value->value = *value;
  419. #if WASM_ENABLE_GC != 0
  420. cur_value->gc_opcode = gc_opcode;
  421. if (wasm_is_type_multi_byte_type(type)) {
  422. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  423. ref_type, wasm_reftype_struct_size(ref_type));
  424. }
  425. #endif
  426. return true;
  427. fail:
  428. return false;
  429. }
  430. #if WASM_ENABLE_GC != 0
  431. static void
  432. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  433. {
  434. WASMStructNewInitValues *struct_init_values =
  435. (WASMStructNewInitValues *)data;
  436. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  437. WASMType *wasm_type;
  438. uint32 i;
  439. if (!data)
  440. return;
  441. wasm_type = module->types[struct_init_values->type_idx];
  442. /* The data can only be type of `WASMStructNewInitValues *`
  443. or `WASMArrayNewInitValues *` */
  444. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  445. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  446. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  447. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  448. WASMRefTypeMap *ref_type_map = struct_type->ref_type_maps;
  449. WASMRefType *ref_type;
  450. uint8 field_type;
  451. for (i = 0; i < struct_init_values->count; i++) {
  452. field_type = struct_type->fields[i].field_type;
  453. if (wasm_is_type_multi_byte_type(field_type))
  454. ref_type = ref_type_map->ref_type;
  455. else
  456. ref_type = NULL;
  457. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  458. REF_TYPE_STRUCTREF, NULL,
  459. module->types, module->type_count)
  460. || wasm_reftype_is_subtype_of(
  461. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  462. module->types, module->type_count)) {
  463. destroy_init_expr_data_recursive(
  464. module, struct_init_values->fields[i].data);
  465. }
  466. }
  467. }
  468. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  469. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  470. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  471. uint8 elem_type = array_type->elem_type;
  472. for (i = 0; i < array_init_values->length; i++) {
  473. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  474. REF_TYPE_STRUCTREF, NULL,
  475. module->types, module->type_count)
  476. || wasm_reftype_is_subtype_of(
  477. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  478. module->types, module->type_count)) {
  479. destroy_init_expr_data_recursive(
  480. module, array_init_values->elem_data[i].data);
  481. }
  482. }
  483. }
  484. wasm_runtime_free(data);
  485. }
  486. #endif
  487. static bool
  488. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  489. #if WASM_ENABLE_GC != 0
  490. WASMRefType *ref_type, uint8 *p_gc_opcode,
  491. #endif
  492. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  493. {
  494. InitValue *cur_value;
  495. if (ctx->sp == 0) {
  496. set_error_buf(error_buf, error_buf_size,
  497. "type mismatch: const expr stack underflow");
  498. return false;
  499. }
  500. cur_value = &ctx->stack[--ctx->sp];
  501. #if WASM_ENABLE_GC == 0
  502. if (cur_value->type != type) {
  503. set_error_buf(error_buf, error_buf_size, "type mismatch");
  504. return false;
  505. }
  506. #else
  507. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  508. ref_type, ctx->module->types,
  509. ctx->module->type_count)) {
  510. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  511. "type mismatch: expect ", type2str(type),
  512. " but got other");
  513. goto fail;
  514. }
  515. #endif
  516. if (p_flag)
  517. *p_flag = cur_value->flag;
  518. if (p_value)
  519. *p_value = cur_value->value;
  520. #if WASM_ENABLE_GC != 0
  521. if (p_gc_opcode)
  522. *p_gc_opcode = cur_value->gc_opcode;
  523. #endif
  524. return true;
  525. #if WASM_ENABLE_GC != 0
  526. fail:
  527. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  528. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  529. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  530. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  531. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  532. }
  533. return false;
  534. #endif
  535. }
  536. static void
  537. destroy_const_expr_stack(ConstExprContext *ctx)
  538. {
  539. #if WASM_ENABLE_GC != 0
  540. uint32 i;
  541. for (i = 0; i < ctx->sp; i++) {
  542. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  543. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  544. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  545. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  546. destroy_init_expr_data_recursive(ctx->module,
  547. ctx->stack[i].value.data);
  548. }
  549. }
  550. #endif
  551. if (ctx->stack != ctx->data) {
  552. wasm_runtime_free(ctx->stack);
  553. }
  554. }
  555. #if WASM_ENABLE_GC != 0
  556. static void
  557. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  558. {
  559. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  560. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  561. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  562. destroy_init_expr_data_recursive(module, expr->u.data);
  563. }
  564. }
  565. #endif /* end of WASM_ENABLE_GC != 0 */
  566. static bool
  567. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  568. InitializerExpression *init_expr, uint8 type, void *ref_type,
  569. char *error_buf, uint32 error_buf_size)
  570. {
  571. const uint8 *p = *p_buf, *p_end = buf_end;
  572. uint8 flag, *p_float;
  573. uint32 i;
  574. ConstExprContext const_expr_ctx = { 0 };
  575. WASMValue cur_value;
  576. #if WASM_ENABLE_GC != 0
  577. uint32 opcode1, type_idx;
  578. uint8 opcode;
  579. WASMRefType cur_ref_type = { 0 };
  580. #endif
  581. init_const_expr_stack(&const_expr_ctx, module);
  582. CHECK_BUF(p, p_end, 1);
  583. flag = read_uint8(p);
  584. while (flag != WASM_OP_END) {
  585. switch (flag) {
  586. /* i32.const */
  587. case INIT_EXPR_TYPE_I32_CONST:
  588. read_leb_int32(p, p_end, cur_value.i32);
  589. if (!push_const_expr_stack(
  590. &const_expr_ctx, flag, VALUE_TYPE_I32,
  591. #if WASM_ENABLE_GC != 0
  592. NULL, 0,
  593. #endif
  594. &cur_value, error_buf, error_buf_size))
  595. goto fail;
  596. break;
  597. /* i64.const */
  598. case INIT_EXPR_TYPE_I64_CONST:
  599. read_leb_int64(p, p_end, cur_value.i64);
  600. if (!push_const_expr_stack(
  601. &const_expr_ctx, flag, VALUE_TYPE_I64,
  602. #if WASM_ENABLE_GC != 0
  603. NULL, 0,
  604. #endif
  605. &cur_value, error_buf, error_buf_size))
  606. goto fail;
  607. break;
  608. /* f32.const */
  609. case INIT_EXPR_TYPE_F32_CONST:
  610. CHECK_BUF(p, p_end, 4);
  611. p_float = (uint8 *)&cur_value.f32;
  612. for (i = 0; i < sizeof(float32); i++)
  613. *p_float++ = *p++;
  614. if (!push_const_expr_stack(
  615. &const_expr_ctx, flag, VALUE_TYPE_F32,
  616. #if WASM_ENABLE_GC != 0
  617. NULL, 0,
  618. #endif
  619. &cur_value, error_buf, error_buf_size))
  620. goto fail;
  621. break;
  622. /* f64.const */
  623. case INIT_EXPR_TYPE_F64_CONST:
  624. CHECK_BUF(p, p_end, 8);
  625. p_float = (uint8 *)&cur_value.f64;
  626. for (i = 0; i < sizeof(float64); i++)
  627. *p_float++ = *p++;
  628. if (!push_const_expr_stack(
  629. &const_expr_ctx, flag, VALUE_TYPE_F64,
  630. #if WASM_ENABLE_GC != 0
  631. NULL, 0,
  632. #endif
  633. &cur_value, error_buf, error_buf_size))
  634. goto fail;
  635. break;
  636. #if WASM_ENABLE_SIMD != 0
  637. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  638. /* v128.const */
  639. case INIT_EXPR_TYPE_V128_CONST:
  640. {
  641. uint64 high, low;
  642. CHECK_BUF(p, p_end, 1);
  643. (void)read_uint8(p);
  644. CHECK_BUF(p, p_end, 16);
  645. wasm_runtime_read_v128(p, &high, &low);
  646. p += 16;
  647. cur_value.v128.i64x2[0] = high;
  648. cur_value.v128.i64x2[1] = low;
  649. if (!push_const_expr_stack(
  650. &const_expr_ctx, flag, VALUE_TYPE_V128,
  651. #if WASM_ENABLE_GC != 0
  652. NULL, 0,
  653. #endif
  654. &cur_value, error_buf, error_buf_size))
  655. goto fail;
  656. #if WASM_ENABLE_WAMR_COMPILER != 0
  657. /* If any init_expr is v128.const, mark SIMD used */
  658. module->is_simd_used = true;
  659. #endif
  660. break;
  661. }
  662. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  663. #endif /* end of WASM_ENABLE_SIMD */
  664. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  665. /* ref.func */
  666. case INIT_EXPR_TYPE_FUNCREF_CONST:
  667. {
  668. uint32 func_idx;
  669. read_leb_uint32(p, p_end, func_idx);
  670. cur_value.ref_index = func_idx;
  671. if (!check_function_index(module, func_idx, error_buf,
  672. error_buf_size)) {
  673. goto fail;
  674. }
  675. #if WASM_ENABLE_GC == 0
  676. if (!push_const_expr_stack(&const_expr_ctx, flag,
  677. VALUE_TYPE_FUNCREF, &cur_value,
  678. error_buf, error_buf_size))
  679. goto fail;
  680. #else
  681. if (func_idx < module->import_function_count) {
  682. type_idx =
  683. module->import_functions[func_idx].u.function.type_idx;
  684. }
  685. else {
  686. type_idx = module
  687. ->functions[func_idx
  688. - module->import_function_count]
  689. ->type_idx;
  690. }
  691. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  692. false, type_idx);
  693. if (!push_const_expr_stack(&const_expr_ctx, flag,
  694. cur_ref_type.ref_type, &cur_ref_type,
  695. 0, &cur_value, error_buf,
  696. error_buf_size))
  697. goto fail;
  698. #endif
  699. #if WASM_ENABLE_WAMR_COMPILER != 0
  700. module->is_ref_types_used = true;
  701. #endif
  702. break;
  703. }
  704. /* ref.null */
  705. case INIT_EXPR_TYPE_REFNULL_CONST:
  706. {
  707. uint8 type1;
  708. CHECK_BUF(p, p_end, 1);
  709. type1 = read_uint8(p);
  710. #if WASM_ENABLE_GC == 0
  711. cur_value.ref_index = NULL_REF;
  712. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  713. &cur_value, error_buf,
  714. error_buf_size))
  715. goto fail;
  716. #else
  717. cur_value.gc_obj = NULL_REF;
  718. if (!is_byte_a_type(type1)) {
  719. p--;
  720. read_leb_uint32(p, p_end, type_idx);
  721. if (!check_type_index(module, module->type_count, type_idx,
  722. error_buf, error_buf_size))
  723. goto fail;
  724. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  725. true, type_idx);
  726. if (!push_const_expr_stack(&const_expr_ctx, flag,
  727. cur_ref_type.ref_type,
  728. &cur_ref_type, 0, &cur_value,
  729. error_buf, error_buf_size))
  730. goto fail;
  731. }
  732. else {
  733. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  734. NULL, 0, &cur_value, error_buf,
  735. error_buf_size))
  736. goto fail;
  737. }
  738. #endif
  739. #if WASM_ENABLE_WAMR_COMPILER != 0
  740. module->is_ref_types_used = true;
  741. #endif
  742. break;
  743. }
  744. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  745. /* get_global */
  746. case INIT_EXPR_TYPE_GET_GLOBAL:
  747. {
  748. uint32 global_idx;
  749. uint8 global_type;
  750. read_leb_uint32(p, p_end, cur_value.global_index);
  751. global_idx = cur_value.global_index;
  752. /*
  753. * Currently, constant expressions occurring as initializers
  754. * of globals are further constrained in that contained
  755. * global.get instructions are
  756. * only allowed to refer to imported globals.
  757. *
  758. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  759. */
  760. if (global_idx >= module->import_global_count
  761. /* make spec test happy */
  762. #if WASM_ENABLE_GC != 0
  763. + module->global_count
  764. #endif
  765. ) {
  766. set_error_buf_v(error_buf, error_buf_size,
  767. "unknown global %u", global_idx);
  768. goto fail;
  769. }
  770. if (
  771. /* make spec test happy */
  772. #if WASM_ENABLE_GC != 0
  773. global_idx < module->import_global_count &&
  774. #endif
  775. module->import_globals[global_idx]
  776. .u.global.type.is_mutable) {
  777. set_error_buf_v(error_buf, error_buf_size,
  778. "constant expression required");
  779. goto fail;
  780. }
  781. if (global_idx < module->import_global_count) {
  782. global_type = module->import_globals[global_idx]
  783. .u.global.type.val_type;
  784. #if WASM_ENABLE_GC != 0
  785. if (wasm_is_type_multi_byte_type(global_type)) {
  786. WASMRefType *global_ref_type =
  787. module->import_globals[global_idx]
  788. .u.global.ref_type;
  789. bh_memcpy_s(&cur_ref_type,
  790. wasm_reftype_struct_size(global_ref_type),
  791. global_ref_type,
  792. wasm_reftype_struct_size(global_ref_type));
  793. }
  794. #endif
  795. }
  796. else {
  797. global_type =
  798. module
  799. ->globals[global_idx - module->import_global_count]
  800. .type.val_type;
  801. #if WASM_ENABLE_GC != 0
  802. if (wasm_is_type_multi_byte_type(global_type)) {
  803. WASMRefType *global_ref_type =
  804. module
  805. ->globals[global_idx
  806. - module->import_global_count]
  807. .ref_type;
  808. bh_memcpy_s(&cur_ref_type,
  809. wasm_reftype_struct_size(global_ref_type),
  810. global_ref_type,
  811. wasm_reftype_struct_size(global_ref_type));
  812. }
  813. #endif
  814. }
  815. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  816. #if WASM_ENABLE_GC != 0
  817. &cur_ref_type, 0,
  818. #endif
  819. &cur_value, error_buf,
  820. error_buf_size))
  821. goto fail;
  822. break;
  823. }
  824. #if WASM_ENABLE_GC != 0
  825. /* struct.new and array.new */
  826. case WASM_OP_GC_PREFIX:
  827. {
  828. read_leb_uint32(p, p_end, opcode1);
  829. switch (opcode1) {
  830. case WASM_OP_STRUCT_NEW:
  831. {
  832. WASMStructType *struct_type;
  833. WASMStructNewInitValues *struct_init_values = NULL;
  834. uint32 field_count;
  835. read_leb_uint32(p, p_end, type_idx);
  836. if (!check_type_index(module, module->type_count,
  837. type_idx, error_buf,
  838. error_buf_size)) {
  839. goto fail;
  840. }
  841. struct_type = (WASMStructType *)module->types[type_idx];
  842. if (struct_type->base_type.type_flag
  843. != WASM_TYPE_STRUCT) {
  844. set_error_buf(error_buf, error_buf_size,
  845. "unkown struct type");
  846. goto fail;
  847. }
  848. field_count = struct_type->field_count;
  849. if (!(struct_init_values = loader_malloc(
  850. offsetof(WASMStructNewInitValues, fields)
  851. + (uint64)field_count * sizeof(WASMValue),
  852. error_buf, error_buf_size))) {
  853. goto fail;
  854. }
  855. struct_init_values->type_idx = type_idx;
  856. struct_init_values->count = field_count;
  857. for (i = field_count; i > 0; i--) {
  858. WASMRefType *field_ref_type = NULL;
  859. uint32 field_idx = i - 1;
  860. uint8 field_type =
  861. struct_type->fields[field_idx].field_type;
  862. if (wasm_is_type_multi_byte_type(field_type)) {
  863. field_ref_type = wasm_reftype_map_find(
  864. struct_type->ref_type_maps,
  865. struct_type->ref_type_map_count, field_idx);
  866. }
  867. if (is_packed_type(field_type)) {
  868. field_type = VALUE_TYPE_I32;
  869. }
  870. if (!pop_const_expr_stack(
  871. &const_expr_ctx, NULL, field_type,
  872. field_ref_type, NULL,
  873. &struct_init_values->fields[field_idx],
  874. error_buf, error_buf_size)) {
  875. destroy_init_expr_data_recursive(
  876. module, struct_init_values);
  877. goto fail;
  878. }
  879. }
  880. cur_value.data = struct_init_values;
  881. wasm_set_refheaptype_typeidx(
  882. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  883. if (!push_const_expr_stack(
  884. &const_expr_ctx, flag, cur_ref_type.ref_type,
  885. &cur_ref_type, (uint8)opcode1, &cur_value,
  886. error_buf, error_buf_size)) {
  887. destroy_init_expr_data_recursive(
  888. module, struct_init_values);
  889. goto fail;
  890. }
  891. break;
  892. }
  893. case WASM_OP_STRUCT_NEW_DEFAULT:
  894. {
  895. read_leb_uint32(p, p_end, cur_value.type_index);
  896. type_idx = cur_value.type_index;
  897. if (!check_type_index(module, module->type_count,
  898. type_idx, error_buf,
  899. error_buf_size)) {
  900. goto fail;
  901. }
  902. if (module->types[type_idx]->type_flag
  903. != WASM_TYPE_STRUCT) {
  904. set_error_buf(error_buf, error_buf_size,
  905. "unkown struct type");
  906. goto fail;
  907. }
  908. cur_value.type_index = type_idx;
  909. cur_value.data = NULL;
  910. wasm_set_refheaptype_typeidx(
  911. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  912. if (!push_const_expr_stack(
  913. &const_expr_ctx, flag, cur_ref_type.ref_type,
  914. &cur_ref_type, (uint8)opcode1, &cur_value,
  915. error_buf, error_buf_size)) {
  916. goto fail;
  917. }
  918. break;
  919. }
  920. case WASM_OP_ARRAY_NEW:
  921. case WASM_OP_ARRAY_NEW_DEFAULT:
  922. case WASM_OP_ARRAY_NEW_FIXED:
  923. {
  924. WASMArrayNewInitValues *array_init_values = NULL;
  925. WASMArrayType *array_type = NULL;
  926. WASMRefType *elem_ref_type = NULL;
  927. uint64 total_size;
  928. uint8 elem_type;
  929. read_leb_uint32(p, p_end, cur_value.type_index);
  930. type_idx = cur_value.type_index;
  931. if (!check_type_index(module, module->type_count,
  932. type_idx, error_buf,
  933. error_buf_size)) {
  934. goto fail;
  935. }
  936. array_type = (WASMArrayType *)module->types[type_idx];
  937. if (array_type->base_type.type_flag
  938. != WASM_TYPE_ARRAY) {
  939. set_error_buf(error_buf, error_buf_size,
  940. "unkown array type");
  941. goto fail;
  942. }
  943. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  944. elem_type = array_type->elem_type;
  945. if (wasm_is_type_multi_byte_type(elem_type)) {
  946. elem_ref_type = array_type->elem_ref_type;
  947. }
  948. if (is_packed_type(elem_type)) {
  949. elem_type = VALUE_TYPE_I32;
  950. }
  951. if (opcode1 == WASM_OP_ARRAY_NEW) {
  952. WASMValue len_val;
  953. if (!(array_init_values = loader_malloc(
  954. sizeof(WASMArrayNewInitValues),
  955. error_buf, error_buf_size))) {
  956. goto fail;
  957. }
  958. array_init_values->type_idx = type_idx;
  959. if (!pop_const_expr_stack(
  960. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  961. NULL, NULL, &len_val, error_buf,
  962. error_buf_size)) {
  963. destroy_init_expr_data_recursive(
  964. module, array_init_values);
  965. goto fail;
  966. }
  967. array_init_values->length = len_val.i32;
  968. if (!pop_const_expr_stack(
  969. &const_expr_ctx, NULL, elem_type,
  970. elem_ref_type, NULL,
  971. &array_init_values->elem_data[0],
  972. error_buf, error_buf_size)) {
  973. destroy_init_expr_data_recursive(
  974. module, array_init_values);
  975. goto fail;
  976. }
  977. cur_value.data = array_init_values;
  978. }
  979. else {
  980. /* WASM_OP_ARRAY_NEW_FIXED */
  981. uint32 len;
  982. read_leb_uint32(p, p_end, len);
  983. total_size =
  984. (uint64)offsetof(WASMArrayNewInitValues,
  985. elem_data)
  986. + (uint64)sizeof(WASMValue) * len;
  987. if (!(array_init_values =
  988. loader_malloc(total_size, error_buf,
  989. error_buf_size))) {
  990. goto fail;
  991. }
  992. array_init_values->type_idx = type_idx;
  993. array_init_values->length = len;
  994. for (i = len; i > 0; i--) {
  995. if (!pop_const_expr_stack(
  996. &const_expr_ctx, NULL, elem_type,
  997. elem_ref_type, NULL,
  998. &array_init_values
  999. ->elem_data[i - 1],
  1000. error_buf, error_buf_size)) {
  1001. destroy_init_expr_data_recursive(
  1002. module, array_init_values);
  1003. goto fail;
  1004. }
  1005. }
  1006. cur_value.data = array_init_values;
  1007. }
  1008. }
  1009. else {
  1010. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1011. WASMValue len_val;
  1012. uint32 len;
  1013. /* POP(i32) */
  1014. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1015. VALUE_TYPE_I32, NULL,
  1016. NULL, &len_val, error_buf,
  1017. error_buf_size)) {
  1018. goto fail;
  1019. }
  1020. len = len_val.i32;
  1021. cur_value.array_new_default.type_index = type_idx;
  1022. cur_value.array_new_default.length = len;
  1023. }
  1024. wasm_set_refheaptype_typeidx(
  1025. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1026. if (!push_const_expr_stack(
  1027. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1028. &cur_ref_type, (uint8)opcode1, &cur_value,
  1029. error_buf, error_buf_size)) {
  1030. if (array_init_values) {
  1031. destroy_init_expr_data_recursive(
  1032. module, array_init_values);
  1033. }
  1034. goto fail;
  1035. }
  1036. break;
  1037. }
  1038. case WASM_OP_ANY_CONVERT_EXTERN:
  1039. {
  1040. set_error_buf(error_buf, error_buf_size,
  1041. "unsupported constant expression of "
  1042. "extern.internalize");
  1043. goto fail;
  1044. }
  1045. case WASM_OP_EXTERN_CONVERT_ANY:
  1046. {
  1047. set_error_buf(error_buf, error_buf_size,
  1048. "unsupported constant expression of "
  1049. "extern.externalize");
  1050. goto fail;
  1051. }
  1052. case WASM_OP_REF_I31:
  1053. {
  1054. /* POP(i32) */
  1055. if (!pop_const_expr_stack(
  1056. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1057. NULL, &cur_value, error_buf, error_buf_size)) {
  1058. goto fail;
  1059. }
  1060. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1061. false, HEAP_TYPE_I31);
  1062. if (!push_const_expr_stack(
  1063. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1064. &cur_ref_type, (uint8)opcode1, &cur_value,
  1065. error_buf, error_buf_size)) {
  1066. goto fail;
  1067. }
  1068. break;
  1069. }
  1070. default:
  1071. set_error_buf(
  1072. error_buf, error_buf_size,
  1073. "type mismatch or constant expression required");
  1074. goto fail;
  1075. }
  1076. break;
  1077. }
  1078. #endif /* end of WASM_ENABLE_GC != 0 */
  1079. default:
  1080. {
  1081. set_error_buf(error_buf, error_buf_size,
  1082. "illegal opcode "
  1083. "or constant expression required "
  1084. "or type mismatch");
  1085. goto fail;
  1086. }
  1087. }
  1088. CHECK_BUF(p, p_end, 1);
  1089. flag = read_uint8(p);
  1090. }
  1091. /* There should be only one value left on the init value stack */
  1092. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1093. #if WASM_ENABLE_GC != 0
  1094. ref_type, &opcode,
  1095. #endif
  1096. &cur_value, error_buf, error_buf_size)) {
  1097. goto fail;
  1098. }
  1099. if (const_expr_ctx.sp != 0) {
  1100. set_error_buf(error_buf, error_buf_size,
  1101. "type mismatch: illegal constant opcode sequence");
  1102. goto fail;
  1103. }
  1104. init_expr->init_expr_type = flag;
  1105. init_expr->u = cur_value;
  1106. #if WASM_ENABLE_GC != 0
  1107. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1108. switch (opcode) {
  1109. case WASM_OP_STRUCT_NEW:
  1110. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1111. break;
  1112. case WASM_OP_STRUCT_NEW_DEFAULT:
  1113. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1114. break;
  1115. case WASM_OP_ARRAY_NEW:
  1116. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1117. break;
  1118. case WASM_OP_ARRAY_NEW_DEFAULT:
  1119. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1120. break;
  1121. case WASM_OP_ARRAY_NEW_FIXED:
  1122. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1123. break;
  1124. case WASM_OP_REF_I31:
  1125. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1126. break;
  1127. default:
  1128. bh_assert(0);
  1129. break;
  1130. }
  1131. }
  1132. #endif /* end of WASM_ENABLE_GC != 0 */
  1133. *p_buf = p;
  1134. destroy_const_expr_stack(&const_expr_ctx);
  1135. return true;
  1136. fail:
  1137. destroy_const_expr_stack(&const_expr_ctx);
  1138. return false;
  1139. }
  1140. static bool
  1141. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1142. {
  1143. if (mutable >= 2) {
  1144. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1145. return false;
  1146. }
  1147. return true;
  1148. }
  1149. #if WASM_ENABLE_GC != 0
  1150. static void
  1151. destroy_func_type(WASMFuncType *type)
  1152. {
  1153. /* Destroy the reference type hash set */
  1154. if (type->ref_type_maps)
  1155. wasm_runtime_free(type->ref_type_maps);
  1156. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1157. && WASM_ENABLE_LAZY_JIT != 0
  1158. if (type->call_to_llvm_jit_from_fast_jit)
  1159. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1160. #endif
  1161. /* Free the type */
  1162. wasm_runtime_free(type);
  1163. }
  1164. static void
  1165. destroy_struct_type(WASMStructType *type)
  1166. {
  1167. if (type->ref_type_maps)
  1168. wasm_runtime_free(type->ref_type_maps);
  1169. wasm_runtime_free(type);
  1170. }
  1171. static void
  1172. destroy_array_type(WASMArrayType *type)
  1173. {
  1174. wasm_runtime_free(type);
  1175. }
  1176. static void
  1177. destroy_wasm_type(WASMType *type)
  1178. {
  1179. if (type->ref_count > 1) {
  1180. /* The type is referenced by other types
  1181. of current wasm module */
  1182. type->ref_count--;
  1183. return;
  1184. }
  1185. if (type->type_flag == WASM_TYPE_FUNC)
  1186. destroy_func_type((WASMFuncType *)type);
  1187. else if (type->type_flag == WASM_TYPE_STRUCT)
  1188. destroy_struct_type((WASMStructType *)type);
  1189. else if (type->type_flag == WASM_TYPE_ARRAY)
  1190. destroy_array_type((WASMArrayType *)type);
  1191. else {
  1192. bh_assert(0);
  1193. }
  1194. }
  1195. /* Resolve (ref null ht) or (ref ht) */
  1196. static bool
  1197. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1198. WASMModule *module, uint32 type_count, bool nullable,
  1199. WASMRefType *ref_type, char *error_buf,
  1200. uint32 error_buf_size)
  1201. {
  1202. const uint8 *p = *p_buf, *p_end = buf_end;
  1203. ref_type->ref_type =
  1204. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1205. ref_type->ref_ht_common.nullable = nullable;
  1206. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1207. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1208. /* heap type is (type i), i : typeidx, >= 0 */
  1209. if (!check_type_index(module, type_count,
  1210. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1211. error_buf_size)) {
  1212. return false;
  1213. }
  1214. }
  1215. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1216. /* heap type is func, extern, any, eq, i31 or data */
  1217. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1218. return false;
  1219. }
  1220. *p_buf = p;
  1221. return true;
  1222. fail:
  1223. return false;
  1224. }
  1225. static bool
  1226. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1227. WASMModule *module, uint32 type_count,
  1228. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1229. bool allow_packed_type, char *error_buf,
  1230. uint32 error_buf_size)
  1231. {
  1232. const uint8 *p = *p_buf, *p_end = buf_end;
  1233. uint8 type;
  1234. memset(ref_type, 0, sizeof(WASMRefType));
  1235. CHECK_BUF(p, p_end, 1);
  1236. type = read_uint8(p);
  1237. if (wasm_is_reftype_htref_nullable(type)) {
  1238. /* (ref null ht) */
  1239. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1240. ref_type, error_buf, error_buf_size))
  1241. return false;
  1242. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1243. *p_need_ref_type_map = true;
  1244. else {
  1245. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1246. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1247. multi-byte type to one-byte type to reduce the footprint and
  1248. the complexity of type equal/subtype checking */
  1249. ref_type->ref_type =
  1250. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1251. *p_need_ref_type_map = false;
  1252. }
  1253. }
  1254. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1255. /* (ref ht) */
  1256. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1257. ref_type, error_buf, error_buf_size))
  1258. return false;
  1259. *p_need_ref_type_map = true;
  1260. #if WASM_ENABLE_STRINGREF != 0
  1261. /* covert (ref string) to stringref */
  1262. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1263. ref_type->ref_type =
  1264. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1265. *p_need_ref_type_map = false;
  1266. }
  1267. #endif
  1268. }
  1269. else {
  1270. /* type which can be represented by one byte */
  1271. if (!is_valid_value_type_for_interpreter(type)
  1272. && !(allow_packed_type && is_packed_type(type))) {
  1273. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1274. return false;
  1275. }
  1276. ref_type->ref_type = type;
  1277. *p_need_ref_type_map = false;
  1278. #if WASM_ENABLE_WAMR_COMPILER != 0
  1279. /* If any value's type is v128, mark the module as SIMD used */
  1280. if (type == VALUE_TYPE_V128)
  1281. module->is_simd_used = true;
  1282. #endif
  1283. }
  1284. *p_buf = p;
  1285. return true;
  1286. fail:
  1287. return false;
  1288. }
  1289. static WASMRefType *
  1290. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1291. char *error_buf, uint32 error_buf_size)
  1292. {
  1293. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1294. if (!ret) {
  1295. set_error_buf(error_buf, error_buf_size,
  1296. "insert ref type to hash set failed");
  1297. }
  1298. return ret;
  1299. }
  1300. static bool
  1301. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1302. uint32 type_count, uint32 type_idx, char *error_buf,
  1303. uint32 error_buf_size)
  1304. {
  1305. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1306. uint32 param_count, result_count, i, j = 0;
  1307. uint32 param_cell_num, ret_cell_num;
  1308. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1309. uint64 total_size;
  1310. bool need_ref_type_map;
  1311. WASMRefType ref_type;
  1312. WASMFuncType *type = NULL;
  1313. /* Parse first time to resolve param count, result count and
  1314. ref type map count */
  1315. read_leb_uint32(p, p_end, param_count);
  1316. p_org = p;
  1317. for (i = 0; i < param_count; i++) {
  1318. if (!resolve_value_type(&p, p_end, module, type_count,
  1319. &need_ref_type_map, &ref_type, false, error_buf,
  1320. error_buf_size)) {
  1321. return false;
  1322. }
  1323. if (need_ref_type_map)
  1324. ref_type_map_count++;
  1325. }
  1326. read_leb_uint32(p, p_end, result_count);
  1327. for (i = 0; i < result_count; i++) {
  1328. if (!resolve_value_type(&p, p_end, module, type_count,
  1329. &need_ref_type_map, &ref_type, false, error_buf,
  1330. error_buf_size)) {
  1331. return false;
  1332. }
  1333. if (need_ref_type_map) {
  1334. ref_type_map_count++;
  1335. result_ref_type_map_count++;
  1336. }
  1337. }
  1338. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1339. "ref type map count: %d",
  1340. type_idx, param_count, result_count, ref_type_map_count);
  1341. /* Parse second time to resolve param types, result types and
  1342. ref type map info */
  1343. p = p_org;
  1344. total_size = offsetof(WASMFuncType, types)
  1345. + sizeof(uint8) * (uint64)(param_count + result_count);
  1346. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1347. return false;
  1348. }
  1349. if (ref_type_map_count > 0) {
  1350. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1351. if (!(type->ref_type_maps =
  1352. loader_malloc(total_size, error_buf, error_buf_size))) {
  1353. goto fail;
  1354. }
  1355. }
  1356. type->base_type.type_flag = WASM_TYPE_FUNC;
  1357. type->param_count = param_count;
  1358. type->result_count = result_count;
  1359. type->ref_type_map_count = ref_type_map_count;
  1360. if (ref_type_map_count > 0) {
  1361. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1362. - result_ref_type_map_count;
  1363. }
  1364. for (i = 0; i < param_count; i++) {
  1365. if (!resolve_value_type(&p, p_end, module, type_count,
  1366. &need_ref_type_map, &ref_type, false, error_buf,
  1367. error_buf_size)) {
  1368. goto fail;
  1369. }
  1370. type->types[i] = ref_type.ref_type;
  1371. if (need_ref_type_map) {
  1372. type->ref_type_maps[j].index = i;
  1373. if (!(type->ref_type_maps[j++].ref_type =
  1374. reftype_set_insert(module->ref_type_set, &ref_type,
  1375. error_buf, error_buf_size))) {
  1376. goto fail;
  1377. }
  1378. }
  1379. }
  1380. read_leb_uint32(p, p_end, result_count);
  1381. for (i = 0; i < result_count; i++) {
  1382. if (!resolve_value_type(&p, p_end, module, type_count,
  1383. &need_ref_type_map, &ref_type, false, error_buf,
  1384. error_buf_size)) {
  1385. goto fail;
  1386. }
  1387. type->types[param_count + i] = ref_type.ref_type;
  1388. if (need_ref_type_map) {
  1389. type->ref_type_maps[j].index = param_count + i;
  1390. if (!(type->ref_type_maps[j++].ref_type =
  1391. reftype_set_insert(module->ref_type_set, &ref_type,
  1392. error_buf, error_buf_size))) {
  1393. goto fail;
  1394. }
  1395. }
  1396. }
  1397. bh_assert(j == type->ref_type_map_count);
  1398. #if TRACE_WASM_LOADER != 0
  1399. os_printf("type %d = ", type_idx);
  1400. wasm_dump_func_type(type);
  1401. #endif
  1402. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1403. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1404. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1405. set_error_buf(error_buf, error_buf_size,
  1406. "param count or result count too large");
  1407. goto fail;
  1408. }
  1409. type->param_cell_num = (uint16)param_cell_num;
  1410. type->ret_cell_num = (uint16)ret_cell_num;
  1411. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1412. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1413. #endif
  1414. #if WASM_ENABLE_WAMR_COMPILER != 0
  1415. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1416. if (type->types[i] == VALUE_TYPE_V128)
  1417. module->is_simd_used = true;
  1418. }
  1419. #endif
  1420. *p_buf = p;
  1421. module->types[type_idx] = (WASMType *)type;
  1422. return true;
  1423. fail:
  1424. if (type)
  1425. destroy_func_type(type);
  1426. return false;
  1427. }
  1428. static bool
  1429. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1430. WASMModule *module, uint32 type_count, uint32 type_idx,
  1431. char *error_buf, uint32 error_buf_size)
  1432. {
  1433. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1434. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1435. uint32 i, j = 0, offset;
  1436. uint16 *reference_table;
  1437. uint64 total_size;
  1438. uint8 mutable;
  1439. bool need_ref_type_map;
  1440. WASMRefType ref_type;
  1441. WASMStructType *type = NULL;
  1442. /* Parse first time to resolve field count and ref type map count */
  1443. read_leb_uint32(p, p_end, field_count);
  1444. p_org = p;
  1445. for (i = 0; i < field_count; i++) {
  1446. if (!resolve_value_type(&p, p_end, module, type_count,
  1447. &need_ref_type_map, &ref_type, true, error_buf,
  1448. error_buf_size)) {
  1449. return false;
  1450. }
  1451. if (need_ref_type_map)
  1452. ref_type_map_count++;
  1453. if (wasm_is_type_reftype(ref_type.ref_type))
  1454. ref_field_count++;
  1455. CHECK_BUF(p, p_end, 1);
  1456. mutable = read_uint8(p);
  1457. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1458. return false;
  1459. }
  1460. }
  1461. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1462. type_idx, field_count, ref_type_map_count);
  1463. /* Parse second time to resolve field types and ref type map info */
  1464. p = p_org;
  1465. total_size = offsetof(WASMStructType, fields)
  1466. + sizeof(WASMStructFieldType) * (uint64)field_count
  1467. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1468. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1469. return false;
  1470. }
  1471. if (ref_type_map_count > 0) {
  1472. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1473. if (!(type->ref_type_maps =
  1474. loader_malloc(total_size, error_buf, error_buf_size))) {
  1475. goto fail;
  1476. }
  1477. }
  1478. type->reference_table = reference_table =
  1479. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1480. + sizeof(WASMStructFieldType) * field_count);
  1481. *reference_table++ = ref_field_count;
  1482. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1483. type->field_count = field_count;
  1484. type->ref_type_map_count = ref_type_map_count;
  1485. offset = (uint32)offsetof(WASMStructObject, field_data);
  1486. for (i = 0; i < field_count; i++) {
  1487. if (!resolve_value_type(&p, p_end, module, type_count,
  1488. &need_ref_type_map, &ref_type, true, error_buf,
  1489. error_buf_size)) {
  1490. goto fail;
  1491. }
  1492. type->fields[i].field_type = ref_type.ref_type;
  1493. if (need_ref_type_map) {
  1494. type->ref_type_maps[j].index = i;
  1495. if (!(type->ref_type_maps[j++].ref_type =
  1496. reftype_set_insert(module->ref_type_set, &ref_type,
  1497. error_buf, error_buf_size))) {
  1498. goto fail;
  1499. }
  1500. }
  1501. CHECK_BUF(p, p_end, 1);
  1502. type->fields[i].field_flags = read_uint8(p);
  1503. type->fields[i].field_size =
  1504. (uint8)wasm_reftype_size(ref_type.ref_type);
  1505. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1506. || defined(BUILD_TARGET_X86_32))
  1507. if (type->fields[i].field_size == 2)
  1508. offset = align_uint(offset, 2);
  1509. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1510. offset = align_uint(offset, 4);
  1511. #endif
  1512. type->fields[i].field_offset = offset;
  1513. if (wasm_is_type_reftype(ref_type.ref_type))
  1514. *reference_table++ = offset;
  1515. offset += type->fields[i].field_size;
  1516. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1517. type->fields[i].field_flags, type->fields[i].field_type);
  1518. }
  1519. type->total_size = offset;
  1520. bh_assert(j == type->ref_type_map_count);
  1521. #if TRACE_WASM_LOADER != 0
  1522. os_printf("type %d = ", type_idx);
  1523. wasm_dump_struct_type(type);
  1524. #endif
  1525. *p_buf = p;
  1526. module->types[type_idx] = (WASMType *)type;
  1527. return true;
  1528. fail:
  1529. if (type)
  1530. destroy_struct_type(type);
  1531. return false;
  1532. }
  1533. static bool
  1534. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1535. WASMModule *module, uint32 type_count, uint32 type_idx,
  1536. char *error_buf, uint32 error_buf_size)
  1537. {
  1538. const uint8 *p = *p_buf, *p_end = buf_end;
  1539. uint8 mutable;
  1540. bool need_ref_type_map;
  1541. WASMRefType ref_type;
  1542. WASMArrayType *type = NULL;
  1543. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1544. &ref_type, true, error_buf, error_buf_size)) {
  1545. return false;
  1546. }
  1547. CHECK_BUF(p, p_end, 1);
  1548. mutable = read_uint8(p);
  1549. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1550. return false;
  1551. }
  1552. LOG_VERBOSE("type %u: array", type_idx);
  1553. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1554. error_buf_size))) {
  1555. return false;
  1556. }
  1557. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1558. type->elem_flags = mutable;
  1559. type->elem_type = ref_type.ref_type;
  1560. if (need_ref_type_map) {
  1561. if (!(type->elem_ref_type =
  1562. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1563. error_buf_size))) {
  1564. goto fail;
  1565. }
  1566. }
  1567. #if TRACE_WASM_LOADER != 0
  1568. os_printf("type %d = ", type_idx);
  1569. wasm_dump_array_type(type);
  1570. #endif
  1571. *p_buf = p;
  1572. module->types[type_idx] = (WASMType *)type;
  1573. return true;
  1574. fail:
  1575. if (type)
  1576. destroy_array_type(type);
  1577. return false;
  1578. }
  1579. static bool
  1580. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1581. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1582. {
  1583. if (heap_type >= 0) {
  1584. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1585. error_buf_size)) {
  1586. return false;
  1587. }
  1588. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1589. heap_type);
  1590. }
  1591. else {
  1592. if (!wasm_is_valid_heap_type(heap_type)) {
  1593. set_error_buf(error_buf, error_buf_size, "unknown type");
  1594. return false;
  1595. }
  1596. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1597. heap_type);
  1598. if (nullable) {
  1599. /* For (ref null func/extern/any/eq/i31/data),
  1600. they are same as
  1601. funcref/externref/anyref/eqref/i31ref/dataref,
  1602. we convert the multi-byte type to one-byte
  1603. type to reduce the footprint and the
  1604. complexity of type equal/subtype checking */
  1605. ref_type->ref_type =
  1606. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1607. }
  1608. }
  1609. return true;
  1610. }
  1611. static void
  1612. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1613. WASMRefType *ref_type2)
  1614. {
  1615. /**
  1616. * The difference rt1 ∖ rt2 between two reference types is defined as
  1617. * follows:
  1618. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1619. * (ref ht2) = (ref null?1 ht1)
  1620. */
  1621. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1622. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1623. ref_type1, wasm_reftype_struct_size(ref_type1));
  1624. }
  1625. else {
  1626. ref_type_diff->ref_type = ref_type1->ref_type;
  1627. }
  1628. if (ref_type2->ref_ht_common.nullable) {
  1629. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1630. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1631. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1632. (int32)ref_type_diff->ref_type - 0x80);
  1633. }
  1634. else {
  1635. ref_type_diff->ref_ht_typeidx.nullable = false;
  1636. }
  1637. }
  1638. }
  1639. #else /* else of WASM_ENABLE_GC != 0 */
  1640. static void
  1641. destroy_wasm_type(WASMType *type)
  1642. {
  1643. if (type->ref_count > 1) {
  1644. /* The type is referenced by other types
  1645. of current wasm module */
  1646. type->ref_count--;
  1647. return;
  1648. }
  1649. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1650. && WASM_ENABLE_LAZY_JIT != 0
  1651. if (type->call_to_llvm_jit_from_fast_jit)
  1652. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1653. #endif
  1654. wasm_runtime_free(type);
  1655. }
  1656. #endif /* end of WASM_ENABLE_GC != 0 */
  1657. static bool
  1658. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1659. char *error_buf, uint32 error_buf_size)
  1660. {
  1661. const uint8 *p = buf, *p_end = buf_end;
  1662. uint32 type_count, i;
  1663. uint64 total_size;
  1664. uint8 flag;
  1665. #if WASM_ENABLE_GC != 0
  1666. uint32 processed_type_count = 0;
  1667. #endif
  1668. read_leb_uint32(p, p_end, type_count);
  1669. if (type_count) {
  1670. module->type_count = type_count;
  1671. total_size = sizeof(WASMType *) * (uint64)type_count;
  1672. if (!(module->types =
  1673. loader_malloc(total_size, error_buf, error_buf_size))) {
  1674. return false;
  1675. }
  1676. #if WASM_ENABLE_GC == 0
  1677. for (i = 0; i < type_count; i++) {
  1678. WASMFuncType *type;
  1679. const uint8 *p_org;
  1680. uint32 param_count, result_count, j;
  1681. uint32 param_cell_num, ret_cell_num;
  1682. CHECK_BUF(p, p_end, 1);
  1683. flag = read_uint8(p);
  1684. if (flag != 0x60) {
  1685. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1686. return false;
  1687. }
  1688. read_leb_uint32(p, p_end, param_count);
  1689. /* Resolve param count and result count firstly */
  1690. p_org = p;
  1691. CHECK_BUF(p, p_end, param_count);
  1692. p += param_count;
  1693. read_leb_uint32(p, p_end, result_count);
  1694. CHECK_BUF(p, p_end, result_count);
  1695. p = p_org;
  1696. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1697. set_error_buf(error_buf, error_buf_size,
  1698. "param count or result count too large");
  1699. return false;
  1700. }
  1701. total_size = offsetof(WASMFuncType, types)
  1702. + sizeof(uint8) * (uint64)(param_count + result_count);
  1703. if (!(type = module->types[i] =
  1704. loader_malloc(total_size, error_buf, error_buf_size))) {
  1705. return false;
  1706. }
  1707. /* Resolve param types and result types */
  1708. type->ref_count = 1;
  1709. type->param_count = (uint16)param_count;
  1710. type->result_count = (uint16)result_count;
  1711. for (j = 0; j < param_count; j++) {
  1712. CHECK_BUF(p, p_end, 1);
  1713. type->types[j] = read_uint8(p);
  1714. }
  1715. read_leb_uint32(p, p_end, result_count);
  1716. for (j = 0; j < result_count; j++) {
  1717. CHECK_BUF(p, p_end, 1);
  1718. type->types[param_count + j] = read_uint8(p);
  1719. }
  1720. for (j = 0; j < param_count + result_count; j++) {
  1721. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  1722. set_error_buf(error_buf, error_buf_size,
  1723. "unknown value type");
  1724. return false;
  1725. }
  1726. }
  1727. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1728. ret_cell_num =
  1729. wasm_get_cell_num(type->types + param_count, result_count);
  1730. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1731. set_error_buf(error_buf, error_buf_size,
  1732. "param count or result count too large");
  1733. return false;
  1734. }
  1735. type->param_cell_num = (uint16)param_cell_num;
  1736. type->ret_cell_num = (uint16)ret_cell_num;
  1737. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1738. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1739. #endif
  1740. #if WASM_ENABLE_WAMR_COMPILER != 0
  1741. for (j = 0; j < type->param_count + type->result_count; j++) {
  1742. if (type->types[j] == VALUE_TYPE_V128)
  1743. module->is_simd_used = true;
  1744. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1745. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1746. module->is_ref_types_used = true;
  1747. }
  1748. #endif
  1749. /* If there is already a same type created, use it instead */
  1750. for (j = 0; j < i; j++) {
  1751. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1752. if (module->types[j]->ref_count == UINT16_MAX) {
  1753. set_error_buf(error_buf, error_buf_size,
  1754. "wasm type's ref count too large");
  1755. return false;
  1756. }
  1757. destroy_wasm_type(type);
  1758. module->types[i] = module->types[j];
  1759. module->types[j]->ref_count++;
  1760. break;
  1761. }
  1762. }
  1763. }
  1764. #else /* else of WASM_ENABLE_GC == 0 */
  1765. for (i = 0; i < type_count; i++) {
  1766. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1767. uint32 rec_count = 1, j;
  1768. bool is_sub_final = true;
  1769. CHECK_BUF(p, p_end, 1);
  1770. flag = read_uint8(p);
  1771. if (flag == DEFINED_TYPE_REC) {
  1772. read_leb_uint32(p, p_end, rec_count);
  1773. if (rec_count > 1) {
  1774. uint64 new_total_size;
  1775. /* integer overflow */
  1776. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1777. set_error_buf(error_buf, error_buf_size,
  1778. "recursive type count too large");
  1779. return false;
  1780. }
  1781. module->type_count += rec_count - 1;
  1782. new_total_size =
  1783. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1784. if (new_total_size > UINT32_MAX) {
  1785. set_error_buf(error_buf, error_buf_size,
  1786. "allocate memory failed");
  1787. return false;
  1788. }
  1789. MEM_REALLOC(module->types, (uint32)total_size,
  1790. (uint32)new_total_size);
  1791. total_size = new_total_size;
  1792. }
  1793. LOG_VERBOSE("Processing rec group [%d-%d]",
  1794. processed_type_count,
  1795. processed_type_count + rec_count - 1);
  1796. }
  1797. else {
  1798. p--;
  1799. }
  1800. for (j = 0; j < rec_count; j++) {
  1801. WASMType *cur_type = NULL;
  1802. CHECK_BUF(p, p_end, 1);
  1803. flag = read_uint8(p);
  1804. parent_type_idx = -1;
  1805. if (flag == DEFINED_TYPE_SUB
  1806. || flag == DEFINED_TYPE_SUB_FINAL) {
  1807. read_leb_uint32(p, p_end, super_type_count);
  1808. if (super_type_count > 1) {
  1809. set_error_buf(error_buf, error_buf_size,
  1810. "super type count too large");
  1811. return false;
  1812. }
  1813. if (super_type_count > 0) {
  1814. read_leb_uint32(p, p_end, parent_type_idx);
  1815. if (parent_type_idx >= processed_type_count + j) {
  1816. set_error_buf_v(error_buf, error_buf_size,
  1817. "unknown type %d", parent_type_idx);
  1818. return false;
  1819. }
  1820. if (module->types[parent_type_idx]->is_sub_final) {
  1821. set_error_buf(error_buf, error_buf_size,
  1822. "sub type can not inherit from "
  1823. "a final super type");
  1824. return false;
  1825. }
  1826. }
  1827. if (flag == DEFINED_TYPE_SUB)
  1828. is_sub_final = false;
  1829. CHECK_BUF(p, p_end, 1);
  1830. flag = read_uint8(p);
  1831. }
  1832. if (flag == DEFINED_TYPE_FUNC) {
  1833. if (!resolve_func_type(&p, buf_end, module,
  1834. processed_type_count + rec_count,
  1835. processed_type_count + j, error_buf,
  1836. error_buf_size)) {
  1837. return false;
  1838. }
  1839. }
  1840. else if (flag == DEFINED_TYPE_STRUCT) {
  1841. if (!resolve_struct_type(&p, buf_end, module,
  1842. processed_type_count + rec_count,
  1843. processed_type_count + j,
  1844. error_buf, error_buf_size)) {
  1845. return false;
  1846. }
  1847. }
  1848. else if (flag == DEFINED_TYPE_ARRAY) {
  1849. if (!resolve_array_type(&p, buf_end, module,
  1850. processed_type_count + rec_count,
  1851. processed_type_count + j, error_buf,
  1852. error_buf_size)) {
  1853. return false;
  1854. }
  1855. }
  1856. else {
  1857. set_error_buf(error_buf, error_buf_size,
  1858. "invalid type flag");
  1859. return false;
  1860. }
  1861. cur_type = module->types[processed_type_count + j];
  1862. cur_type->ref_count = 1;
  1863. cur_type->parent_type_idx = parent_type_idx;
  1864. cur_type->is_sub_final = is_sub_final;
  1865. cur_type->rec_count = rec_count;
  1866. cur_type->rec_idx = j;
  1867. cur_type->rec_begin_type_idx = processed_type_count;
  1868. }
  1869. /* resolve subtyping relationship in current rec group */
  1870. for (j = 0; j < rec_count; j++) {
  1871. WASMType *cur_type = module->types[processed_type_count + j];
  1872. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1873. WASMType *parent_type =
  1874. module->types[cur_type->parent_type_idx];
  1875. cur_type->parent_type = parent_type;
  1876. cur_type->root_type = parent_type->root_type;
  1877. if (parent_type->inherit_depth == UINT16_MAX) {
  1878. set_error_buf(error_buf, error_buf_size,
  1879. "parent type's inherit depth too large");
  1880. return false;
  1881. }
  1882. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1883. }
  1884. else {
  1885. cur_type->parent_type = NULL;
  1886. cur_type->root_type = cur_type;
  1887. cur_type->inherit_depth = 0;
  1888. }
  1889. }
  1890. for (j = 0; j < rec_count; j++) {
  1891. WASMType *cur_type = module->types[processed_type_count + j];
  1892. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1893. WASMType *parent_type =
  1894. module->types[cur_type->parent_type_idx];
  1895. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1896. module->types,
  1897. module->type_count)) {
  1898. set_error_buf(error_buf, error_buf_size,
  1899. "sub type does not match super type");
  1900. return false;
  1901. }
  1902. }
  1903. }
  1904. /* If there is already an equivalence type or a group of equivalence
  1905. recursive types created, use it or them instead */
  1906. for (j = 0; j < processed_type_count;) {
  1907. WASMType *src_type = module->types[j];
  1908. WASMType *cur_type = module->types[processed_type_count];
  1909. uint32 k, src_rec_count;
  1910. src_rec_count = src_type->rec_count;
  1911. if (src_rec_count != rec_count) {
  1912. /* no type equivalence */
  1913. j += src_rec_count;
  1914. continue;
  1915. }
  1916. for (k = 0; k < rec_count; k++) {
  1917. src_type = module->types[j + k];
  1918. cur_type = module->types[processed_type_count + k];
  1919. if (!wasm_type_equal(src_type, cur_type, module->types,
  1920. module->type_count)) {
  1921. break;
  1922. }
  1923. }
  1924. if (k < rec_count) {
  1925. /* no type equivalence */
  1926. j += src_rec_count;
  1927. continue;
  1928. }
  1929. /* type equivalence */
  1930. for (k = 0; k < rec_count; k++) {
  1931. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1932. set_error_buf(error_buf, error_buf_size,
  1933. "wasm type's ref count too large");
  1934. return false;
  1935. }
  1936. destroy_wasm_type(module->types[processed_type_count + k]);
  1937. module->types[processed_type_count + k] =
  1938. module->types[j + k];
  1939. module->types[j + k]->ref_count++;
  1940. }
  1941. break;
  1942. }
  1943. if (rec_count > 1) {
  1944. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1945. processed_type_count,
  1946. processed_type_count + rec_count - 1);
  1947. }
  1948. processed_type_count += rec_count;
  1949. }
  1950. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1951. * module->type_count,
  1952. error_buf, error_buf_size))) {
  1953. return false;
  1954. }
  1955. #endif /* end of WASM_ENABLE_GC == 0 */
  1956. }
  1957. if (p != p_end) {
  1958. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1959. return false;
  1960. }
  1961. LOG_VERBOSE("Load type section success.\n");
  1962. return true;
  1963. fail:
  1964. return false;
  1965. }
  1966. static void
  1967. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1968. {
  1969. uint32 default_max_size;
  1970. if (UINT32_MAX / 2 > init_size)
  1971. default_max_size = init_size * 2;
  1972. else
  1973. default_max_size = UINT32_MAX;
  1974. if (default_max_size < WASM_TABLE_MAX_SIZE)
  1975. default_max_size = WASM_TABLE_MAX_SIZE;
  1976. if (max_size_flag) {
  1977. /* module defines the table limitation */
  1978. bh_assert(init_size <= *max_size);
  1979. if (init_size < *max_size) {
  1980. *max_size =
  1981. *max_size < default_max_size ? *max_size : default_max_size;
  1982. }
  1983. }
  1984. else {
  1985. /* partial defined table limitation, gives a default value */
  1986. *max_size = default_max_size;
  1987. }
  1988. }
  1989. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  1990. /**
  1991. * Find export item of a module with export info:
  1992. * module name, field name and export kind
  1993. */
  1994. static WASMExport *
  1995. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  1996. const char *field_name, uint8 export_kind,
  1997. char *error_buf, uint32 error_buf_size)
  1998. {
  1999. WASMExport *export =
  2000. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2001. export_kind, error_buf, error_buf_size);
  2002. return export;
  2003. }
  2004. #endif
  2005. #if WASM_ENABLE_MULTI_MODULE != 0
  2006. static WASMFunction *
  2007. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  2008. const WASMFuncType *expected_function_type,
  2009. char *error_buf, uint32 error_buf_size)
  2010. {
  2011. WASMModuleCommon *module_reg;
  2012. WASMFunction *function = NULL;
  2013. WASMExport *export = NULL;
  2014. WASMModule *module = NULL;
  2015. WASMFuncType *target_function_type = NULL;
  2016. module_reg = wasm_runtime_find_module_registered(module_name);
  2017. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2018. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  2019. function_name);
  2020. set_error_buf(error_buf, error_buf_size, "unknown import");
  2021. return NULL;
  2022. }
  2023. module = (WASMModule *)module_reg;
  2024. export =
  2025. wasm_loader_find_export(module, module_name, function_name,
  2026. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  2027. if (!export) {
  2028. return NULL;
  2029. }
  2030. /* resolve function type and function */
  2031. if (export->index < module->import_function_count) {
  2032. target_function_type =
  2033. module->import_functions[export->index].u.function.func_type;
  2034. function = module->import_functions[export->index]
  2035. .u.function.import_func_linked;
  2036. }
  2037. else {
  2038. target_function_type =
  2039. module->functions[export->index - module->import_function_count]
  2040. ->func_type;
  2041. function =
  2042. module->functions[export->index - module->import_function_count];
  2043. }
  2044. /* check function type */
  2045. if (!wasm_type_equal((WASMType *)expected_function_type,
  2046. (WASMType *)target_function_type, module->types,
  2047. module->type_count)) {
  2048. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  2049. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2050. return NULL;
  2051. }
  2052. return function;
  2053. }
  2054. static WASMTable *
  2055. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2056. uint32 init_size, uint32 max_size, char *error_buf,
  2057. uint32 error_buf_size)
  2058. {
  2059. WASMModuleCommon *module_reg;
  2060. WASMTable *table = NULL;
  2061. WASMExport *export = NULL;
  2062. WASMModule *module = NULL;
  2063. module_reg = wasm_runtime_find_module_registered(module_name);
  2064. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2065. LOG_DEBUG("can not find a module named %s for table", module_name);
  2066. set_error_buf(error_buf, error_buf_size, "unknown import");
  2067. return NULL;
  2068. }
  2069. module = (WASMModule *)module_reg;
  2070. export =
  2071. wasm_loader_find_export(module, module_name, table_name,
  2072. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2073. if (!export) {
  2074. return NULL;
  2075. }
  2076. /* resolve table and check the init/max size */
  2077. if (export->index < module->import_table_count) {
  2078. table =
  2079. module->import_tables[export->index].u.table.import_table_linked;
  2080. }
  2081. else {
  2082. table = &(module->tables[export->index - module->import_table_count]);
  2083. }
  2084. if (table->table_type.init_size < init_size
  2085. || table->table_type.max_size > max_size) {
  2086. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2087. module_name, table_name, table->table_type.init_size,
  2088. table->table_type.max_size, init_size, max_size);
  2089. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2090. return NULL;
  2091. }
  2092. return table;
  2093. }
  2094. static WASMMemory *
  2095. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2096. uint32 init_page_count, uint32 max_page_count,
  2097. char *error_buf, uint32 error_buf_size)
  2098. {
  2099. WASMModuleCommon *module_reg;
  2100. WASMMemory *memory = NULL;
  2101. WASMExport *export = NULL;
  2102. WASMModule *module = NULL;
  2103. module_reg = wasm_runtime_find_module_registered(module_name);
  2104. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2105. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2106. set_error_buf(error_buf, error_buf_size, "unknown import");
  2107. return NULL;
  2108. }
  2109. module = (WASMModule *)module_reg;
  2110. export =
  2111. wasm_loader_find_export(module, module_name, memory_name,
  2112. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2113. if (!export) {
  2114. return NULL;
  2115. }
  2116. /* resolve memory and check the init/max page count */
  2117. if (export->index < module->import_memory_count) {
  2118. memory = module->import_memories[export->index]
  2119. .u.memory.import_memory_linked;
  2120. }
  2121. else {
  2122. memory =
  2123. &(module->memories[export->index - module->import_memory_count]);
  2124. }
  2125. if (memory->init_page_count < init_page_count
  2126. || memory->max_page_count > max_page_count) {
  2127. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2128. module_name, memory_name, memory->init_page_count,
  2129. memory->max_page_count, init_page_count, max_page_count);
  2130. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2131. return NULL;
  2132. }
  2133. return memory;
  2134. }
  2135. static WASMGlobal *
  2136. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2137. uint8 type, bool is_mutable, char *error_buf,
  2138. uint32 error_buf_size)
  2139. {
  2140. WASMModuleCommon *module_reg;
  2141. WASMGlobal *global = NULL;
  2142. WASMExport *export = NULL;
  2143. WASMModule *module = NULL;
  2144. module_reg = wasm_runtime_find_module_registered(module_name);
  2145. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2146. LOG_DEBUG("can not find a module named %s for global", module_name);
  2147. set_error_buf(error_buf, error_buf_size, "unknown import");
  2148. return NULL;
  2149. }
  2150. module = (WASMModule *)module_reg;
  2151. export =
  2152. wasm_loader_find_export(module, module_name, global_name,
  2153. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2154. if (!export) {
  2155. return NULL;
  2156. }
  2157. /* resolve and check the global */
  2158. if (export->index < module->import_global_count) {
  2159. global =
  2160. module->import_globals[export->index].u.global.import_global_linked;
  2161. }
  2162. else {
  2163. global =
  2164. &(module->globals[export->index - module->import_global_count]);
  2165. }
  2166. if (global->type.val_type != type
  2167. || global->type.is_mutable != is_mutable) {
  2168. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2169. module_name, global_name, global->type.val_type,
  2170. global->type.is_mutable, type, is_mutable);
  2171. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2172. return NULL;
  2173. }
  2174. return global;
  2175. }
  2176. #if WASM_ENABLE_TAGS != 0
  2177. static WASMTag *
  2178. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2179. const WASMType *expected_tag_type,
  2180. uint32 *linked_tag_index, char *error_buf,
  2181. uint32 error_buf_size)
  2182. {
  2183. WASMModuleCommon *module_reg;
  2184. WASMTag *tag = NULL;
  2185. WASMExport *export = NULL;
  2186. WASMModule *module = NULL;
  2187. module_reg = wasm_runtime_find_module_registered(module_name);
  2188. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2189. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2190. tag_name);
  2191. set_error_buf(error_buf, error_buf_size, "unknown import");
  2192. return NULL;
  2193. }
  2194. module = (WASMModule *)module_reg;
  2195. export =
  2196. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2197. error_buf, error_buf_size);
  2198. if (!export) {
  2199. return NULL;
  2200. }
  2201. /* resolve tag type and tag */
  2202. if (export->index < module->import_tag_count) {
  2203. /* importing an imported tag from the submodule */
  2204. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2205. }
  2206. else {
  2207. /* importing an section tag from the submodule */
  2208. tag = module->tags[export->index - module->import_tag_count];
  2209. }
  2210. /* check function type */
  2211. if (!wasm_type_equal(expected_tag_type, tag->tag_type)) {
  2212. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2213. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2214. return NULL;
  2215. }
  2216. if (linked_tag_index != NULL) {
  2217. *linked_tag_index = export->index;
  2218. }
  2219. return tag;
  2220. }
  2221. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2222. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2223. static bool
  2224. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2225. const WASMModule *parent_module,
  2226. const char *sub_module_name, const char *function_name,
  2227. WASMFunctionImport *function, char *error_buf,
  2228. uint32 error_buf_size)
  2229. {
  2230. const uint8 *p = *p_buf, *p_end = buf_end;
  2231. uint32 declare_type_index = 0;
  2232. WASMFuncType *declare_func_type = NULL;
  2233. WASMFunction *linked_func = NULL;
  2234. #if WASM_ENABLE_MULTI_MODULE != 0
  2235. WASMModule *sub_module = NULL;
  2236. bool is_built_in_module = false;
  2237. #endif
  2238. const char *linked_signature = NULL;
  2239. void *linked_attachment = NULL;
  2240. bool linked_call_conv_raw = false;
  2241. bool is_native_symbol = false;
  2242. read_leb_uint32(p, p_end, declare_type_index);
  2243. *p_buf = p;
  2244. if (declare_type_index >= parent_module->type_count) {
  2245. set_error_buf(error_buf, error_buf_size, "unknown type");
  2246. return false;
  2247. }
  2248. #if WASM_ENABLE_GC != 0
  2249. function->type_idx = declare_type_index;
  2250. #endif
  2251. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2252. declare_type_index = wasm_get_smallest_type_idx(
  2253. parent_module->types, parent_module->type_count, declare_type_index);
  2254. #endif
  2255. declare_func_type =
  2256. (WASMFuncType *)parent_module->types[declare_type_index];
  2257. /* lookup registered native symbols first */
  2258. linked_func = wasm_native_resolve_symbol(
  2259. sub_module_name, function_name, declare_func_type, &linked_signature,
  2260. &linked_attachment, &linked_call_conv_raw);
  2261. if (linked_func) {
  2262. is_native_symbol = true;
  2263. }
  2264. #if WASM_ENABLE_MULTI_MODULE != 0
  2265. else {
  2266. if (!(is_built_in_module =
  2267. wasm_runtime_is_built_in_module(sub_module_name))) {
  2268. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2269. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2270. error_buf_size);
  2271. }
  2272. if (is_built_in_module || sub_module)
  2273. linked_func = wasm_loader_resolve_function(
  2274. sub_module_name, function_name, declare_func_type, error_buf,
  2275. error_buf_size);
  2276. }
  2277. #endif
  2278. function->module_name = (char *)sub_module_name;
  2279. function->field_name = (char *)function_name;
  2280. function->func_type = declare_func_type;
  2281. /* func_ptr_linked is for native registered symbol */
  2282. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  2283. function->signature = linked_signature;
  2284. function->attachment = linked_attachment;
  2285. function->call_conv_raw = linked_call_conv_raw;
  2286. #if WASM_ENABLE_MULTI_MODULE != 0
  2287. function->import_module = is_native_symbol ? NULL : sub_module;
  2288. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  2289. #endif
  2290. return true;
  2291. fail:
  2292. return false;
  2293. }
  2294. static bool
  2295. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2296. uint32 error_buf_size)
  2297. {
  2298. if (max_size < init_size) {
  2299. set_error_buf(error_buf, error_buf_size,
  2300. "size minimum must not be greater than maximum");
  2301. return false;
  2302. }
  2303. return true;
  2304. }
  2305. static bool
  2306. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2307. WASMModule *parent_module, const char *sub_module_name,
  2308. const char *table_name, WASMTableImport *table,
  2309. char *error_buf, uint32 error_buf_size)
  2310. {
  2311. const uint8 *p = *p_buf, *p_end = buf_end;
  2312. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  2313. declare_init_size = 0, declare_max_size = 0;
  2314. #if WASM_ENABLE_MULTI_MODULE != 0
  2315. WASMModule *sub_module = NULL;
  2316. WASMTable *linked_table = NULL;
  2317. #endif
  2318. #if WASM_ENABLE_GC != 0
  2319. WASMRefType ref_type;
  2320. bool need_ref_type_map;
  2321. #endif
  2322. #if WASM_ENABLE_GC == 0
  2323. CHECK_BUF(p, p_end, 1);
  2324. /* 0x70 or 0x6F */
  2325. declare_elem_type = read_uint8(p);
  2326. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2327. #if WASM_ENABLE_REF_TYPES != 0
  2328. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2329. #endif
  2330. ) {
  2331. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2332. return false;
  2333. }
  2334. #else /* else of WASM_ENABLE_GC == 0 */
  2335. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2336. &need_ref_type_map, &ref_type, false, error_buf,
  2337. error_buf_size)) {
  2338. return false;
  2339. }
  2340. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2341. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2342. return false;
  2343. }
  2344. declare_elem_type = ref_type.ref_type;
  2345. if (need_ref_type_map) {
  2346. if (!(table->table_type.elem_ref_type =
  2347. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2348. error_buf, error_buf_size))) {
  2349. return false;
  2350. }
  2351. }
  2352. #if TRACE_WASM_LOADER != 0
  2353. os_printf("import table type: ");
  2354. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2355. os_printf("\n");
  2356. #endif
  2357. #endif /* end of WASM_ENABLE_GC == 0 */
  2358. read_leb_uint32(p, p_end, declare_max_size_flag);
  2359. if (declare_max_size_flag > 1) {
  2360. set_error_buf(error_buf, error_buf_size, "integer too large");
  2361. return false;
  2362. }
  2363. read_leb_uint32(p, p_end, declare_init_size);
  2364. if (declare_max_size_flag) {
  2365. read_leb_uint32(p, p_end, declare_max_size);
  2366. if (!check_table_max_size(declare_init_size, declare_max_size,
  2367. error_buf, error_buf_size))
  2368. return false;
  2369. }
  2370. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  2371. &declare_max_size);
  2372. *p_buf = p;
  2373. #if WASM_ENABLE_MULTI_MODULE != 0
  2374. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2375. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2376. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2377. error_buf_size);
  2378. if (sub_module) {
  2379. linked_table = wasm_loader_resolve_table(
  2380. sub_module_name, table_name, declare_init_size,
  2381. declare_max_size, error_buf, error_buf_size);
  2382. if (linked_table) {
  2383. /* reset with linked table limit */
  2384. declare_elem_type = linked_table->table_type.elem_type;
  2385. declare_init_size = linked_table->table_type.init_size;
  2386. declare_max_size = linked_table->table_type.max_size;
  2387. declare_max_size_flag = linked_table->table_type.flags;
  2388. table->import_table_linked = linked_table;
  2389. table->import_module = sub_module;
  2390. }
  2391. }
  2392. }
  2393. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2394. /* (table (export "table") 10 20 funcref) */
  2395. /* we need this section working in wamrc */
  2396. if (!strcmp("spectest", sub_module_name)) {
  2397. const uint32 spectest_table_init_size = 10;
  2398. const uint32 spectest_table_max_size = 20;
  2399. if (strcmp("table", table_name)) {
  2400. set_error_buf(error_buf, error_buf_size,
  2401. "incompatible import type or unknown import");
  2402. return false;
  2403. }
  2404. if (declare_init_size > spectest_table_init_size
  2405. || declare_max_size < spectest_table_max_size) {
  2406. set_error_buf(error_buf, error_buf_size,
  2407. "incompatible import type");
  2408. return false;
  2409. }
  2410. declare_init_size = spectest_table_init_size;
  2411. declare_max_size = spectest_table_max_size;
  2412. }
  2413. /* now we believe all declaration are ok */
  2414. table->table_type.elem_type = declare_elem_type;
  2415. table->table_type.init_size = declare_init_size;
  2416. table->table_type.flags = declare_max_size_flag;
  2417. table->table_type.max_size = declare_max_size;
  2418. #if WASM_ENABLE_WAMR_COMPILER != 0
  2419. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2420. parent_module->is_ref_types_used = true;
  2421. #endif
  2422. (void)parent_module;
  2423. return true;
  2424. fail:
  2425. return false;
  2426. }
  2427. static bool
  2428. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2429. uint32 error_buf_size)
  2430. {
  2431. uint32 default_max_size =
  2432. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2433. if (!is_memory64 && init_size > default_max_size) {
  2434. set_error_buf(error_buf, error_buf_size,
  2435. "memory size must be at most 65536 pages (4GiB)");
  2436. return false;
  2437. }
  2438. #if WASM_ENABLE_MEMORY64 != 0
  2439. else if (is_memory64 && init_size > default_max_size) {
  2440. set_error_buf(
  2441. error_buf, error_buf_size,
  2442. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2443. return false;
  2444. }
  2445. #endif
  2446. return true;
  2447. }
  2448. static bool
  2449. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2450. char *error_buf, uint32 error_buf_size)
  2451. {
  2452. uint32 default_max_size =
  2453. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2454. if (max_size < init_size) {
  2455. set_error_buf(error_buf, error_buf_size,
  2456. "size minimum must not be greater than maximum");
  2457. return false;
  2458. }
  2459. if (!is_memory64 && max_size > default_max_size) {
  2460. set_error_buf(error_buf, error_buf_size,
  2461. "memory size must be at most 65536 pages (4GiB)");
  2462. return false;
  2463. }
  2464. #if WASM_ENABLE_MEMORY64 != 0
  2465. else if (is_memory64 && max_size > default_max_size) {
  2466. set_error_buf(
  2467. error_buf, error_buf_size,
  2468. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2469. return false;
  2470. }
  2471. #endif
  2472. return true;
  2473. }
  2474. static bool
  2475. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2476. WASMModule *parent_module, const char *sub_module_name,
  2477. const char *memory_name, WASMMemoryImport *memory,
  2478. char *error_buf, uint32 error_buf_size)
  2479. {
  2480. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2481. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2482. uint32 pool_size = wasm_runtime_memory_pool_size();
  2483. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2484. / DEFAULT_NUM_BYTES_PER_PAGE;
  2485. #else
  2486. uint32 max_page_count;
  2487. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2488. uint32 mem_flag = 0;
  2489. bool is_memory64 = false;
  2490. uint32 declare_init_page_count = 0;
  2491. uint32 declare_max_page_count = 0;
  2492. #if WASM_ENABLE_MULTI_MODULE != 0
  2493. WASMModule *sub_module = NULL;
  2494. WASMMemory *linked_memory = NULL;
  2495. #endif
  2496. p_org = p;
  2497. read_leb_uint32(p, p_end, mem_flag);
  2498. is_memory64 = mem_flag & MEMORY64_FLAG;
  2499. if (p - p_org > 1) {
  2500. LOG_VERBOSE("integer representation too long");
  2501. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2502. return false;
  2503. }
  2504. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2505. return false;
  2506. }
  2507. read_leb_uint32(p, p_end, declare_init_page_count);
  2508. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2509. error_buf_size)) {
  2510. return false;
  2511. }
  2512. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2513. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2514. #endif
  2515. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2516. read_leb_uint32(p, p_end, declare_max_page_count);
  2517. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2518. declare_max_page_count, error_buf,
  2519. error_buf_size)) {
  2520. return false;
  2521. }
  2522. if (declare_max_page_count > max_page_count) {
  2523. declare_max_page_count = max_page_count;
  2524. }
  2525. }
  2526. else {
  2527. /* Limit the maximum memory size to max_page_count */
  2528. declare_max_page_count = max_page_count;
  2529. }
  2530. #if WASM_ENABLE_MULTI_MODULE != 0
  2531. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2532. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2533. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2534. error_buf_size);
  2535. if (sub_module) {
  2536. linked_memory = wasm_loader_resolve_memory(
  2537. sub_module_name, memory_name, declare_init_page_count,
  2538. declare_max_page_count, error_buf, error_buf_size);
  2539. if (linked_memory) {
  2540. /**
  2541. * reset with linked memory limit
  2542. */
  2543. memory->import_module = sub_module;
  2544. memory->import_memory_linked = linked_memory;
  2545. declare_init_page_count = linked_memory->init_page_count;
  2546. declare_max_page_count = linked_memory->max_page_count;
  2547. }
  2548. }
  2549. }
  2550. #endif
  2551. /* (memory (export "memory") 1 2) */
  2552. if (!strcmp("spectest", sub_module_name)) {
  2553. uint32 spectest_memory_init_page = 1;
  2554. uint32 spectest_memory_max_page = 2;
  2555. if (strcmp("memory", memory_name)) {
  2556. set_error_buf(error_buf, error_buf_size,
  2557. "incompatible import type or unknown import");
  2558. return false;
  2559. }
  2560. if (declare_init_page_count > spectest_memory_init_page
  2561. || declare_max_page_count < spectest_memory_max_page) {
  2562. set_error_buf(error_buf, error_buf_size,
  2563. "incompatible import type");
  2564. return false;
  2565. }
  2566. declare_init_page_count = spectest_memory_init_page;
  2567. declare_max_page_count = spectest_memory_max_page;
  2568. }
  2569. #if WASM_ENABLE_WASI_TEST != 0
  2570. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2571. else if (!strcmp("foo", sub_module_name)) {
  2572. uint32 spectest_memory_init_page = 1;
  2573. uint32 spectest_memory_max_page = 1;
  2574. if (strcmp("bar", memory_name)) {
  2575. set_error_buf(error_buf, error_buf_size,
  2576. "incompatible import type or unknown import");
  2577. return false;
  2578. }
  2579. if (declare_init_page_count > spectest_memory_init_page
  2580. || declare_max_page_count < spectest_memory_max_page) {
  2581. set_error_buf(error_buf, error_buf_size,
  2582. "incompatible import type");
  2583. return false;
  2584. }
  2585. declare_init_page_count = spectest_memory_init_page;
  2586. declare_max_page_count = spectest_memory_max_page;
  2587. }
  2588. #endif
  2589. /* now we believe all declaration are ok */
  2590. memory->mem_type.flags = mem_flag;
  2591. memory->mem_type.init_page_count = declare_init_page_count;
  2592. memory->mem_type.max_page_count = declare_max_page_count;
  2593. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2594. *p_buf = p;
  2595. (void)parent_module;
  2596. return true;
  2597. fail:
  2598. return false;
  2599. }
  2600. #if WASM_ENABLE_TAGS != 0
  2601. static bool
  2602. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2603. const WASMModule *parent_module, /* this module ! */
  2604. const char *sub_module_name, const char *tag_name,
  2605. WASMTagImport *tag, /* structure to fill */
  2606. char *error_buf, uint32 error_buf_size)
  2607. {
  2608. /* attribute and type of the import statement */
  2609. uint8 declare_tag_attribute;
  2610. uint32 declare_type_index;
  2611. const uint8 *p = *p_buf, *p_end = buf_end;
  2612. #if WASM_ENABLE_MULTI_MODULE != 0
  2613. WASMModule *sub_module = NULL;
  2614. #endif
  2615. /* get the one byte attribute */
  2616. CHECK_BUF(p, p_end, 1);
  2617. declare_tag_attribute = read_uint8(p);
  2618. if (declare_tag_attribute != 0) {
  2619. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2620. goto fail;
  2621. }
  2622. /* get type */
  2623. read_leb_uint32(p, p_end, declare_type_index);
  2624. /* compare against module->types */
  2625. if (declare_type_index >= parent_module->type_count) {
  2626. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2627. goto fail;
  2628. }
  2629. WASMFuncType *declare_tag_type =
  2630. (WASMFuncType *)parent_module->types[declare_type_index];
  2631. /* check, that the type of the declared tag returns void */
  2632. if (declare_tag_type->result_count != 0) {
  2633. set_error_buf(error_buf, error_buf_size,
  2634. "tag type signature does not return void");
  2635. goto fail;
  2636. }
  2637. #if WASM_ENABLE_MULTI_MODULE != 0
  2638. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2639. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2640. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2641. error_buf_size);
  2642. if (sub_module) {
  2643. /* wasm_loader_resolve_tag checks, that the imported tag
  2644. * and the declared tag have the same type
  2645. */
  2646. uint32 linked_tag_index = 0;
  2647. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2648. sub_module_name, tag_name, declare_tag_type,
  2649. &linked_tag_index /* out */, error_buf, error_buf_size);
  2650. if (linked_tag) {
  2651. tag->import_module = sub_module;
  2652. tag->import_tag_linked = linked_tag;
  2653. tag->import_tag_index_linked = linked_tag_index;
  2654. }
  2655. }
  2656. }
  2657. #endif
  2658. /* store to module tag declarations */
  2659. tag->attribute = declare_tag_attribute;
  2660. tag->type = declare_type_index;
  2661. tag->module_name = (char *)sub_module_name;
  2662. tag->field_name = (char *)tag_name;
  2663. tag->tag_type = declare_tag_type;
  2664. *p_buf = p;
  2665. (void)parent_module;
  2666. LOG_VERBOSE("Load tag import success\n");
  2667. return true;
  2668. fail:
  2669. return false;
  2670. }
  2671. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2672. static bool
  2673. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2674. WASMModule *parent_module, char *sub_module_name,
  2675. char *global_name, WASMGlobalImport *global, char *error_buf,
  2676. uint32 error_buf_size)
  2677. {
  2678. const uint8 *p = *p_buf, *p_end = buf_end;
  2679. uint8 declare_type = 0;
  2680. uint8 declare_mutable = 0;
  2681. #if WASM_ENABLE_MULTI_MODULE != 0
  2682. WASMModule *sub_module = NULL;
  2683. WASMGlobal *linked_global = NULL;
  2684. #endif
  2685. #if WASM_ENABLE_GC != 0
  2686. WASMRefType ref_type;
  2687. bool need_ref_type_map;
  2688. #endif
  2689. bool ret = false;
  2690. #if WASM_ENABLE_GC == 0
  2691. CHECK_BUF(p, p_end, 2);
  2692. /* global type */
  2693. declare_type = read_uint8(p);
  2694. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2695. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2696. return false;
  2697. }
  2698. declare_mutable = read_uint8(p);
  2699. #else
  2700. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2701. &need_ref_type_map, &ref_type, false, error_buf,
  2702. error_buf_size)) {
  2703. return false;
  2704. }
  2705. declare_type = ref_type.ref_type;
  2706. if (need_ref_type_map) {
  2707. if (!(global->ref_type =
  2708. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2709. error_buf, error_buf_size))) {
  2710. return false;
  2711. }
  2712. }
  2713. #if TRACE_WASM_LOADER != 0
  2714. os_printf("import global type: ");
  2715. wasm_dump_value_type(declare_type, global->ref_type);
  2716. os_printf("\n");
  2717. #endif
  2718. CHECK_BUF(p, p_end, 1);
  2719. declare_mutable = read_uint8(p);
  2720. #endif /* end of WASM_ENABLE_GC == 0 */
  2721. *p_buf = p;
  2722. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2723. return false;
  2724. }
  2725. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2726. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2727. global);
  2728. if (ret) {
  2729. if (global->type.val_type != declare_type
  2730. || global->type.is_mutable != declare_mutable) {
  2731. set_error_buf(error_buf, error_buf_size,
  2732. "incompatible import type");
  2733. return false;
  2734. }
  2735. global->is_linked = true;
  2736. }
  2737. #endif
  2738. #if WASM_ENABLE_MULTI_MODULE != 0
  2739. if (!global->is_linked
  2740. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2741. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2742. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2743. error_buf_size);
  2744. if (sub_module) {
  2745. /* check sub modules */
  2746. linked_global = wasm_loader_resolve_global(
  2747. sub_module_name, global_name, declare_type, declare_mutable,
  2748. error_buf, error_buf_size);
  2749. if (linked_global) {
  2750. global->import_module = sub_module;
  2751. global->import_global_linked = linked_global;
  2752. global->is_linked = true;
  2753. }
  2754. }
  2755. }
  2756. #endif
  2757. global->module_name = sub_module_name;
  2758. global->field_name = global_name;
  2759. global->type.val_type = declare_type;
  2760. global->type.is_mutable = (declare_mutable == 1);
  2761. #if WASM_ENABLE_WAMR_COMPILER != 0
  2762. if (global->type.val_type == VALUE_TYPE_V128)
  2763. parent_module->is_simd_used = true;
  2764. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2765. parent_module->is_ref_types_used = true;
  2766. #endif
  2767. (void)parent_module;
  2768. (void)ret;
  2769. return true;
  2770. fail:
  2771. return false;
  2772. }
  2773. static bool
  2774. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2775. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2776. {
  2777. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2778. #if WASM_ENABLE_GC != 0
  2779. WASMRefType ref_type;
  2780. bool need_ref_type_map;
  2781. #endif
  2782. #if WASM_ENABLE_GC == 0
  2783. CHECK_BUF(p, p_end, 1);
  2784. /* 0x70 or 0x6F */
  2785. table->table_type.elem_type = read_uint8(p);
  2786. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  2787. #if WASM_ENABLE_REF_TYPES != 0
  2788. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  2789. #endif
  2790. ) {
  2791. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2792. return false;
  2793. }
  2794. #else /* else of WASM_ENABLE_GC == 0 */
  2795. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2796. &need_ref_type_map, &ref_type, false, error_buf,
  2797. error_buf_size)) {
  2798. return false;
  2799. }
  2800. table->table_type.elem_type = ref_type.ref_type;
  2801. if (need_ref_type_map) {
  2802. if (!(table->table_type.elem_ref_type =
  2803. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2804. error_buf_size))) {
  2805. return false;
  2806. }
  2807. }
  2808. #if TRACE_WASM_LOADER != 0
  2809. os_printf("table type: ");
  2810. wasm_dump_value_type(table->table_type.elem_type,
  2811. table->table_type.elem_ref_type);
  2812. os_printf("\n");
  2813. #endif
  2814. #endif /* end of WASM_ENABLE_GC == 0 */
  2815. p_org = p;
  2816. read_leb_uint32(p, p_end, table->table_type.flags);
  2817. #if WASM_ENABLE_SHARED_MEMORY == 0
  2818. if (p - p_org > 1) {
  2819. set_error_buf(error_buf, error_buf_size,
  2820. "integer representation too long");
  2821. return false;
  2822. }
  2823. if (table->table_type.flags > 1) {
  2824. set_error_buf(error_buf, error_buf_size, "integer too large");
  2825. return false;
  2826. }
  2827. #else
  2828. if (p - p_org > 1) {
  2829. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2830. return false;
  2831. }
  2832. if (table->table_type.flags == 2) {
  2833. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2834. return false;
  2835. }
  2836. if (table->table_type.flags > 1) {
  2837. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2838. return false;
  2839. }
  2840. #endif
  2841. read_leb_uint32(p, p_end, table->table_type.init_size);
  2842. if (table->table_type.flags) {
  2843. read_leb_uint32(p, p_end, table->table_type.max_size);
  2844. if (!check_table_max_size(table->table_type.init_size,
  2845. table->table_type.max_size, error_buf,
  2846. error_buf_size))
  2847. return false;
  2848. }
  2849. adjust_table_max_size(table->table_type.init_size, table->table_type.flags,
  2850. &table->table_type.max_size);
  2851. #if WASM_ENABLE_WAMR_COMPILER != 0
  2852. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2853. module->is_ref_types_used = true;
  2854. #endif
  2855. *p_buf = p;
  2856. return true;
  2857. fail:
  2858. return false;
  2859. }
  2860. static bool
  2861. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2862. char *error_buf, uint32 error_buf_size)
  2863. {
  2864. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2865. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2866. uint32 pool_size = wasm_runtime_memory_pool_size();
  2867. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2868. / DEFAULT_NUM_BYTES_PER_PAGE;
  2869. #else
  2870. uint32 max_page_count;
  2871. #endif
  2872. bool is_memory64 = false;
  2873. p_org = p;
  2874. read_leb_uint32(p, p_end, memory->flags);
  2875. is_memory64 = memory->flags & MEMORY64_FLAG;
  2876. if (p - p_org > 1) {
  2877. LOG_VERBOSE("integer representation too long");
  2878. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2879. return false;
  2880. }
  2881. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2882. false)) {
  2883. return false;
  2884. }
  2885. read_leb_uint32(p, p_end, memory->init_page_count);
  2886. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2887. error_buf_size))
  2888. return false;
  2889. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2890. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2891. #endif
  2892. if (memory->flags & 1) {
  2893. read_leb_uint32(p, p_end, memory->max_page_count);
  2894. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2895. memory->max_page_count, error_buf,
  2896. error_buf_size))
  2897. return false;
  2898. if (memory->max_page_count > max_page_count)
  2899. memory->max_page_count = max_page_count;
  2900. }
  2901. else {
  2902. /* Limit the maximum memory size to max_page_count */
  2903. memory->max_page_count = max_page_count;
  2904. }
  2905. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2906. *p_buf = p;
  2907. return true;
  2908. fail:
  2909. return false;
  2910. }
  2911. static bool
  2912. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2913. bool is_load_from_file_buf, char *error_buf,
  2914. uint32 error_buf_size)
  2915. {
  2916. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2917. uint32 import_count, name_len, type_index, i, u32, flags;
  2918. uint64 total_size;
  2919. WASMImport *import;
  2920. WASMImport *import_functions = NULL, *import_tables = NULL;
  2921. WASMImport *import_memories = NULL, *import_globals = NULL;
  2922. #if WASM_ENABLE_TAGS != 0
  2923. WASMImport *import_tags = NULL;
  2924. #endif
  2925. char *sub_module_name, *field_name;
  2926. uint8 u8, kind, global_type;
  2927. read_leb_uint32(p, p_end, import_count);
  2928. if (import_count) {
  2929. module->import_count = import_count;
  2930. total_size = sizeof(WASMImport) * (uint64)import_count;
  2931. if (!(module->imports =
  2932. loader_malloc(total_size, error_buf, error_buf_size))) {
  2933. return false;
  2934. }
  2935. p_old = p;
  2936. /* Scan firstly to get import count of each type */
  2937. for (i = 0; i < import_count; i++) {
  2938. /* module name */
  2939. read_leb_uint32(p, p_end, name_len);
  2940. CHECK_BUF(p, p_end, name_len);
  2941. p += name_len;
  2942. /* field name */
  2943. read_leb_uint32(p, p_end, name_len);
  2944. CHECK_BUF(p, p_end, name_len);
  2945. p += name_len;
  2946. CHECK_BUF(p, p_end, 1);
  2947. /* 0x00/0x01/0x02/0x03/0x04 */
  2948. kind = read_uint8(p);
  2949. switch (kind) {
  2950. case IMPORT_KIND_FUNC: /* import function */
  2951. read_leb_uint32(p, p_end, type_index);
  2952. module->import_function_count++;
  2953. break;
  2954. case IMPORT_KIND_TABLE: /* import table */
  2955. CHECK_BUF(p, p_end, 1);
  2956. /* 0x70 */
  2957. u8 = read_uint8(p);
  2958. read_leb_uint32(p, p_end, flags);
  2959. read_leb_uint32(p, p_end, u32);
  2960. if (flags & 1)
  2961. read_leb_uint32(p, p_end, u32);
  2962. module->import_table_count++;
  2963. if (module->import_table_count > 1) {
  2964. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2965. set_error_buf(error_buf, error_buf_size,
  2966. "multiple tables");
  2967. return false;
  2968. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2969. module->is_ref_types_used = true;
  2970. #endif
  2971. }
  2972. break;
  2973. case IMPORT_KIND_MEMORY: /* import memory */
  2974. read_leb_uint32(p, p_end, flags);
  2975. read_leb_uint32(p, p_end, u32);
  2976. if (flags & 1)
  2977. read_leb_uint32(p, p_end, u32);
  2978. module->import_memory_count++;
  2979. #if WASM_ENABLE_MULTI_MEMORY == 0
  2980. if (module->import_memory_count > 1) {
  2981. set_error_buf(error_buf, error_buf_size,
  2982. "multiple memories");
  2983. return false;
  2984. }
  2985. #endif
  2986. break;
  2987. #if WASM_ENABLE_TAGS != 0
  2988. case IMPORT_KIND_TAG: /* import tags */
  2989. /* it only counts the number of tags to import */
  2990. module->import_tag_count++;
  2991. CHECK_BUF(p, p_end, 1);
  2992. u8 = read_uint8(p);
  2993. read_leb_uint32(p, p_end, type_index);
  2994. break;
  2995. #endif
  2996. case IMPORT_KIND_GLOBAL: /* import global */
  2997. #if WASM_ENABLE_GC != 0
  2998. /* valtype */
  2999. CHECK_BUF(p, p_end, 1);
  3000. global_type = read_uint8(p);
  3001. if (wasm_is_type_multi_byte_type(global_type)) {
  3002. int32 heap_type;
  3003. read_leb_int32(p, p_end, heap_type);
  3004. (void)heap_type;
  3005. }
  3006. /* mutability */
  3007. CHECK_BUF(p, p_end, 1);
  3008. p += 1;
  3009. #else
  3010. CHECK_BUF(p, p_end, 2);
  3011. p += 2;
  3012. #endif
  3013. (void)global_type;
  3014. module->import_global_count++;
  3015. break;
  3016. default:
  3017. set_error_buf(error_buf, error_buf_size,
  3018. "invalid import kind");
  3019. return false;
  3020. }
  3021. }
  3022. if (module->import_function_count)
  3023. import_functions = module->import_functions = module->imports;
  3024. if (module->import_table_count)
  3025. import_tables = module->import_tables =
  3026. module->imports + module->import_function_count;
  3027. if (module->import_memory_count)
  3028. import_memories = module->import_memories =
  3029. module->imports + module->import_function_count
  3030. + module->import_table_count;
  3031. #if WASM_ENABLE_TAGS != 0
  3032. if (module->import_tag_count)
  3033. import_tags = module->import_tags =
  3034. module->imports + module->import_function_count
  3035. + module->import_table_count + module->import_memory_count;
  3036. if (module->import_global_count)
  3037. import_globals = module->import_globals =
  3038. module->imports + module->import_function_count
  3039. + module->import_table_count + module->import_memory_count
  3040. + module->import_tag_count;
  3041. #else
  3042. if (module->import_global_count)
  3043. import_globals = module->import_globals =
  3044. module->imports + module->import_function_count
  3045. + module->import_table_count + module->import_memory_count;
  3046. #endif
  3047. p = p_old;
  3048. /* Scan again to resolve the data */
  3049. for (i = 0; i < import_count; i++) {
  3050. /* load module name */
  3051. read_leb_uint32(p, p_end, name_len);
  3052. CHECK_BUF(p, p_end, name_len);
  3053. if (!(sub_module_name = wasm_const_str_list_insert(
  3054. p, name_len, module, is_load_from_file_buf, error_buf,
  3055. error_buf_size))) {
  3056. return false;
  3057. }
  3058. p += name_len;
  3059. /* load field name */
  3060. read_leb_uint32(p, p_end, name_len);
  3061. CHECK_BUF(p, p_end, name_len);
  3062. if (!(field_name = wasm_const_str_list_insert(
  3063. p, name_len, module, is_load_from_file_buf, error_buf,
  3064. error_buf_size))) {
  3065. return false;
  3066. }
  3067. p += name_len;
  3068. CHECK_BUF(p, p_end, 1);
  3069. /* 0x00/0x01/0x02/0x03/0x4 */
  3070. kind = read_uint8(p);
  3071. switch (kind) {
  3072. case IMPORT_KIND_FUNC: /* import function */
  3073. bh_assert(import_functions);
  3074. import = import_functions++;
  3075. if (!load_function_import(
  3076. &p, p_end, module, sub_module_name, field_name,
  3077. &import->u.function, error_buf, error_buf_size)) {
  3078. return false;
  3079. }
  3080. break;
  3081. case IMPORT_KIND_TABLE: /* import table */
  3082. bh_assert(import_tables);
  3083. import = import_tables++;
  3084. if (!load_table_import(&p, p_end, module, sub_module_name,
  3085. field_name, &import->u.table,
  3086. error_buf, error_buf_size)) {
  3087. LOG_DEBUG("can not import such a table (%s,%s)",
  3088. sub_module_name, field_name);
  3089. return false;
  3090. }
  3091. break;
  3092. case IMPORT_KIND_MEMORY: /* import memory */
  3093. bh_assert(import_memories);
  3094. import = import_memories++;
  3095. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3096. field_name, &import->u.memory,
  3097. error_buf, error_buf_size)) {
  3098. return false;
  3099. }
  3100. break;
  3101. #if WASM_ENABLE_TAGS != 0
  3102. case IMPORT_KIND_TAG:
  3103. bh_assert(import_tags);
  3104. import = import_tags++;
  3105. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3106. field_name, &import->u.tag, error_buf,
  3107. error_buf_size)) {
  3108. return false;
  3109. }
  3110. break;
  3111. #endif
  3112. case IMPORT_KIND_GLOBAL: /* import global */
  3113. bh_assert(import_globals);
  3114. import = import_globals++;
  3115. if (!load_global_import(&p, p_end, module, sub_module_name,
  3116. field_name, &import->u.global,
  3117. error_buf, error_buf_size)) {
  3118. return false;
  3119. }
  3120. break;
  3121. default:
  3122. set_error_buf(error_buf, error_buf_size,
  3123. "invalid import kind");
  3124. return false;
  3125. }
  3126. import->kind = kind;
  3127. import->u.names.module_name = sub_module_name;
  3128. import->u.names.field_name = field_name;
  3129. }
  3130. #if WASM_ENABLE_LIBC_WASI != 0
  3131. import = module->import_functions;
  3132. for (i = 0; i < module->import_function_count; i++, import++) {
  3133. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3134. || !strcmp(import->u.names.module_name,
  3135. "wasi_snapshot_preview1")) {
  3136. module->import_wasi_api = true;
  3137. break;
  3138. }
  3139. }
  3140. #endif
  3141. }
  3142. if (p != p_end) {
  3143. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3144. return false;
  3145. }
  3146. LOG_VERBOSE("Load import section success.\n");
  3147. (void)u8;
  3148. (void)u32;
  3149. (void)type_index;
  3150. return true;
  3151. fail:
  3152. return false;
  3153. }
  3154. static bool
  3155. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3156. uint32 error_buf_size)
  3157. {
  3158. WASMFuncType *param_type = func->func_type;
  3159. uint32 param_count = param_type->param_count;
  3160. uint8 *param_types = param_type->types;
  3161. uint32 local_count = func->local_count;
  3162. uint8 *local_types = func->local_types;
  3163. uint32 i, local_offset = 0;
  3164. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3165. /*
  3166. * Only allocate memory when total_size is not 0,
  3167. * or the return value of malloc(0) might be NULL on some platforms,
  3168. * which causes wasm loader return false.
  3169. */
  3170. if (total_size > 0
  3171. && !(func->local_offsets =
  3172. loader_malloc(total_size, error_buf, error_buf_size))) {
  3173. return false;
  3174. }
  3175. for (i = 0; i < param_count; i++) {
  3176. func->local_offsets[i] = (uint16)local_offset;
  3177. local_offset += wasm_value_type_cell_num(param_types[i]);
  3178. }
  3179. for (i = 0; i < local_count; i++) {
  3180. func->local_offsets[param_count + i] = (uint16)local_offset;
  3181. local_offset += wasm_value_type_cell_num(local_types[i]);
  3182. }
  3183. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3184. return true;
  3185. }
  3186. static bool
  3187. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3188. const uint8 *buf_code, const uint8 *buf_code_end,
  3189. WASMModule *module, char *error_buf,
  3190. uint32 error_buf_size)
  3191. {
  3192. const uint8 *p = buf, *p_end = buf_end;
  3193. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3194. uint32 func_count;
  3195. uint64 total_size;
  3196. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3197. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3198. uint8 type;
  3199. WASMFunction *func;
  3200. #if WASM_ENABLE_GC != 0
  3201. bool need_ref_type_map;
  3202. WASMRefType ref_type;
  3203. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3204. #endif
  3205. read_leb_uint32(p, p_end, func_count);
  3206. if (buf_code)
  3207. read_leb_uint32(p_code, buf_code_end, code_count);
  3208. if (func_count != code_count) {
  3209. set_error_buf(error_buf, error_buf_size,
  3210. "function and code section have inconsistent lengths or "
  3211. "unexpected end");
  3212. return false;
  3213. }
  3214. if (is_indices_overflow(module->import_function_count, func_count,
  3215. error_buf, error_buf_size))
  3216. return false;
  3217. if (func_count) {
  3218. module->function_count = func_count;
  3219. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3220. if (!(module->functions =
  3221. loader_malloc(total_size, error_buf, error_buf_size))) {
  3222. return false;
  3223. }
  3224. for (i = 0; i < func_count; i++) {
  3225. /* Resolve function type */
  3226. read_leb_uint32(p, p_end, type_index);
  3227. if (type_index >= module->type_count) {
  3228. set_error_buf(error_buf, error_buf_size, "unknown type");
  3229. return false;
  3230. }
  3231. #if WASM_ENABLE_GC != 0
  3232. type_index_org = type_index;
  3233. #endif
  3234. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3235. && WASM_ENABLE_GC == 0
  3236. type_index = wasm_get_smallest_type_idx(
  3237. module->types, module->type_count, type_index);
  3238. #endif
  3239. read_leb_uint32(p_code, buf_code_end, code_size);
  3240. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3241. set_error_buf(error_buf, error_buf_size,
  3242. "invalid function code size");
  3243. return false;
  3244. }
  3245. /* Resolve local set count */
  3246. p_code_end = p_code + code_size;
  3247. local_count = 0;
  3248. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3249. p_code_save = p_code;
  3250. #if WASM_ENABLE_GC != 0
  3251. ref_type_map_count = 0;
  3252. #endif
  3253. /* Calculate total local count */
  3254. for (j = 0; j < local_set_count; j++) {
  3255. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3256. if (sub_local_count > UINT32_MAX - local_count) {
  3257. set_error_buf(error_buf, error_buf_size, "too many locals");
  3258. return false;
  3259. }
  3260. #if WASM_ENABLE_GC == 0
  3261. CHECK_BUF(p_code, buf_code_end, 1);
  3262. /* 0x7F/0x7E/0x7D/0x7C */
  3263. type = read_uint8(p_code);
  3264. local_count += sub_local_count;
  3265. #if WASM_ENABLE_WAMR_COMPILER != 0
  3266. /* If any value's type is v128, mark the module as SIMD used */
  3267. if (type == VALUE_TYPE_V128)
  3268. module->is_simd_used = true;
  3269. #endif
  3270. #else
  3271. if (!resolve_value_type(&p_code, buf_code_end, module,
  3272. module->type_count, &need_ref_type_map,
  3273. &ref_type, false, error_buf,
  3274. error_buf_size)) {
  3275. return false;
  3276. }
  3277. local_count += sub_local_count;
  3278. if (need_ref_type_map)
  3279. ref_type_map_count += sub_local_count;
  3280. #endif
  3281. }
  3282. /* Alloc memory, layout: function structure + local types */
  3283. code_size = (uint32)(p_code_end - p_code);
  3284. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3285. if (!(func = module->functions[i] =
  3286. loader_malloc(total_size, error_buf, error_buf_size))) {
  3287. return false;
  3288. }
  3289. #if WASM_ENABLE_GC != 0
  3290. if (ref_type_map_count > 0) {
  3291. total_size =
  3292. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3293. if (!(func->local_ref_type_maps = loader_malloc(
  3294. total_size, error_buf, error_buf_size))) {
  3295. return false;
  3296. }
  3297. func->local_ref_type_map_count = ref_type_map_count;
  3298. }
  3299. #endif
  3300. /* Set function type, local count, code size and code body */
  3301. func->func_type = (WASMFuncType *)module->types[type_index];
  3302. func->local_count = local_count;
  3303. if (local_count > 0)
  3304. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3305. func->code_size = code_size;
  3306. /*
  3307. * we shall make a copy of code body [p_code, p_code + code_size]
  3308. * when we are worrying about inappropriate releasing behaviour.
  3309. * all code bodies are actually in a buffer which user allocates in
  3310. * his embedding environment and we don't have power on them.
  3311. * it will be like:
  3312. * code_body_cp = malloc(code_size);
  3313. * memcpy(code_body_cp, p_code, code_size);
  3314. * func->code = code_body_cp;
  3315. */
  3316. func->code = (uint8 *)p_code;
  3317. #if WASM_ENABLE_GC != 0
  3318. func->type_idx = type_index_org;
  3319. #endif
  3320. #if WASM_ENABLE_GC != 0
  3321. t = 0;
  3322. #endif
  3323. /* Load each local type */
  3324. p_code = p_code_save;
  3325. local_type_index = 0;
  3326. for (j = 0; j < local_set_count; j++) {
  3327. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3328. /* Note: sub_local_count is allowed to be 0 */
  3329. if (local_type_index > UINT32_MAX - sub_local_count
  3330. || local_type_index + sub_local_count > local_count) {
  3331. set_error_buf(error_buf, error_buf_size,
  3332. "invalid local count");
  3333. return false;
  3334. }
  3335. #if WASM_ENABLE_GC == 0
  3336. CHECK_BUF(p_code, buf_code_end, 1);
  3337. /* 0x7F/0x7E/0x7D/0x7C */
  3338. type = read_uint8(p_code);
  3339. if (!is_valid_value_type_for_interpreter(type)) {
  3340. if (type == VALUE_TYPE_V128)
  3341. set_error_buf(error_buf, error_buf_size,
  3342. "v128 value type requires simd feature");
  3343. else if (type == VALUE_TYPE_FUNCREF
  3344. || type == VALUE_TYPE_EXTERNREF)
  3345. set_error_buf(error_buf, error_buf_size,
  3346. "ref value type requires "
  3347. "reference types feature");
  3348. else
  3349. set_error_buf_v(error_buf, error_buf_size,
  3350. "invalid local type 0x%02X", type);
  3351. return false;
  3352. }
  3353. #else
  3354. if (!resolve_value_type(&p_code, buf_code_end, module,
  3355. module->type_count, &need_ref_type_map,
  3356. &ref_type, false, error_buf,
  3357. error_buf_size)) {
  3358. return false;
  3359. }
  3360. if (need_ref_type_map) {
  3361. WASMRefType *ref_type_tmp;
  3362. if (!(ref_type_tmp = reftype_set_insert(
  3363. module->ref_type_set, &ref_type, error_buf,
  3364. error_buf_size))) {
  3365. return false;
  3366. }
  3367. for (k = 0; k < sub_local_count; k++) {
  3368. func->local_ref_type_maps[t + k].ref_type =
  3369. ref_type_tmp;
  3370. func->local_ref_type_maps[t + k].index =
  3371. local_type_index + k;
  3372. }
  3373. t += sub_local_count;
  3374. }
  3375. type = ref_type.ref_type;
  3376. #endif
  3377. for (k = 0; k < sub_local_count; k++) {
  3378. func->local_types[local_type_index++] = type;
  3379. }
  3380. #if WASM_ENABLE_WAMR_COMPILER != 0
  3381. if (type == VALUE_TYPE_V128)
  3382. module->is_simd_used = true;
  3383. else if (type == VALUE_TYPE_FUNCREF
  3384. || type == VALUE_TYPE_EXTERNREF)
  3385. module->is_ref_types_used = true;
  3386. #endif
  3387. }
  3388. bh_assert(local_type_index == func->local_count);
  3389. #if WASM_ENABLE_GC != 0
  3390. bh_assert(t == func->local_ref_type_map_count);
  3391. #if TRACE_WASM_LOADER != 0
  3392. os_printf("func %u, local types: [", i);
  3393. k = 0;
  3394. for (j = 0; j < func->local_count; j++) {
  3395. WASMRefType *ref_type_tmp = NULL;
  3396. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3397. bh_assert(j == func->local_ref_type_maps[k].index);
  3398. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3399. }
  3400. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3401. if (j < func->local_count - 1)
  3402. os_printf(" ");
  3403. }
  3404. os_printf("]\n");
  3405. #endif
  3406. #endif
  3407. func->param_cell_num = func->func_type->param_cell_num;
  3408. func->ret_cell_num = func->func_type->ret_cell_num;
  3409. local_cell_num =
  3410. wasm_get_cell_num(func->local_types, func->local_count);
  3411. if (local_cell_num > UINT16_MAX) {
  3412. set_error_buf(error_buf, error_buf_size,
  3413. "local count too large");
  3414. return false;
  3415. }
  3416. func->local_cell_num = (uint16)local_cell_num;
  3417. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3418. return false;
  3419. p_code = p_code_end;
  3420. }
  3421. }
  3422. if (p != p_end) {
  3423. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3424. return false;
  3425. }
  3426. LOG_VERBOSE("Load function section success.\n");
  3427. return true;
  3428. fail:
  3429. return false;
  3430. }
  3431. static bool
  3432. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3433. char *error_buf, uint32 error_buf_size)
  3434. {
  3435. const uint8 *p = buf, *p_end = buf_end;
  3436. uint32 table_count, i;
  3437. uint64 total_size;
  3438. WASMTable *table;
  3439. read_leb_uint32(p, p_end, table_count);
  3440. if (module->import_table_count + table_count > 1) {
  3441. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3442. /* a total of one table is allowed */
  3443. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3444. return false;
  3445. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3446. module->is_ref_types_used = true;
  3447. #endif
  3448. }
  3449. if (table_count) {
  3450. module->table_count = table_count;
  3451. total_size = sizeof(WASMTable) * (uint64)table_count;
  3452. if (!(module->tables =
  3453. loader_malloc(total_size, error_buf, error_buf_size))) {
  3454. return false;
  3455. }
  3456. /* load each table */
  3457. table = module->tables;
  3458. for (i = 0; i < table_count; i++, table++) {
  3459. #if WASM_ENABLE_GC != 0
  3460. uint8 flag;
  3461. bool has_init = false;
  3462. CHECK_BUF(buf, buf_end, 1);
  3463. flag = read_uint8(p);
  3464. if (flag == TABLE_INIT_EXPR_FLAG) {
  3465. CHECK_BUF(buf, buf_end, 1);
  3466. flag = read_uint8(p);
  3467. if (flag != 0x00) {
  3468. set_error_buf(error_buf, error_buf_size,
  3469. "invalid leading bytes for table");
  3470. return false;
  3471. }
  3472. has_init = true;
  3473. }
  3474. else {
  3475. p--;
  3476. }
  3477. #endif /* end of WASM_ENABLE_GC != 0 */
  3478. if (!load_table(&p, p_end, module, table, error_buf,
  3479. error_buf_size))
  3480. return false;
  3481. #if WASM_ENABLE_GC != 0
  3482. if (has_init) {
  3483. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3484. table->table_type.elem_type,
  3485. table->table_type.elem_ref_type, error_buf,
  3486. error_buf_size))
  3487. return false;
  3488. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3489. && table->init_expr.init_expr_type
  3490. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3491. set_error_buf(
  3492. error_buf, error_buf_size,
  3493. "unsupported initializer expression for table");
  3494. return false;
  3495. }
  3496. }
  3497. else {
  3498. if (wasm_is_reftype_htref_non_nullable(
  3499. table->table_type.elem_type)) {
  3500. set_error_buf(
  3501. error_buf, error_buf_size,
  3502. "type mismatch: non-nullable table without init expr");
  3503. return false;
  3504. }
  3505. }
  3506. #endif /* end of WASM_ENABLE_GC != 0 */
  3507. #if WASM_ENABLE_WAMR_COMPILER != 0
  3508. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3509. module->is_ref_types_used = true;
  3510. #endif
  3511. }
  3512. }
  3513. if (p != p_end) {
  3514. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3515. return false;
  3516. }
  3517. LOG_VERBOSE("Load table section success.\n");
  3518. return true;
  3519. fail:
  3520. return false;
  3521. }
  3522. static bool
  3523. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3524. char *error_buf, uint32 error_buf_size)
  3525. {
  3526. const uint8 *p = buf, *p_end = buf_end;
  3527. uint32 memory_count, i;
  3528. uint64 total_size;
  3529. WASMMemory *memory;
  3530. read_leb_uint32(p, p_end, memory_count);
  3531. #if WASM_ENABLE_MULTI_MEMORY == 0
  3532. /* a total of one memory is allowed */
  3533. if (module->import_memory_count + memory_count > 1) {
  3534. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3535. return false;
  3536. }
  3537. #endif
  3538. if (memory_count) {
  3539. module->memory_count = memory_count;
  3540. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3541. if (!(module->memories =
  3542. loader_malloc(total_size, error_buf, error_buf_size))) {
  3543. return false;
  3544. }
  3545. /* load each memory */
  3546. memory = module->memories;
  3547. for (i = 0; i < memory_count; i++, memory++)
  3548. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3549. return false;
  3550. }
  3551. if (p != p_end) {
  3552. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3553. return false;
  3554. }
  3555. LOG_VERBOSE("Load memory section success.\n");
  3556. return true;
  3557. fail:
  3558. return false;
  3559. }
  3560. static bool
  3561. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3562. char *error_buf, uint32 error_buf_size)
  3563. {
  3564. const uint8 *p = buf, *p_end = buf_end;
  3565. uint32 global_count, i;
  3566. uint64 total_size;
  3567. WASMGlobal *global;
  3568. uint8 mutable;
  3569. #if WASM_ENABLE_GC != 0
  3570. bool need_ref_type_map;
  3571. WASMRefType ref_type;
  3572. #endif
  3573. read_leb_uint32(p, p_end, global_count);
  3574. if (is_indices_overflow(module->import_global_count, global_count,
  3575. error_buf, error_buf_size))
  3576. return false;
  3577. module->global_count = 0;
  3578. if (global_count) {
  3579. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3580. if (!(module->globals =
  3581. loader_malloc(total_size, error_buf, error_buf_size))) {
  3582. return false;
  3583. }
  3584. global = module->globals;
  3585. for (i = 0; i < global_count; i++, global++) {
  3586. #if WASM_ENABLE_GC == 0
  3587. CHECK_BUF(p, p_end, 2);
  3588. /* global type */
  3589. global->type.val_type = read_uint8(p);
  3590. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3591. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3592. return false;
  3593. }
  3594. mutable = read_uint8(p);
  3595. #else
  3596. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3597. &need_ref_type_map, &ref_type, false,
  3598. error_buf, error_buf_size)) {
  3599. return false;
  3600. }
  3601. global->type.val_type = ref_type.ref_type;
  3602. CHECK_BUF(p, p_end, 1);
  3603. mutable = read_uint8(p);
  3604. #endif /* end of WASM_ENABLE_GC */
  3605. #if WASM_ENABLE_WAMR_COMPILER != 0
  3606. if (global->type.val_type == VALUE_TYPE_V128)
  3607. module->is_simd_used = true;
  3608. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3609. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3610. module->is_ref_types_used = true;
  3611. #endif
  3612. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3613. return false;
  3614. }
  3615. global->type.is_mutable = mutable ? true : false;
  3616. /* initialize expression */
  3617. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3618. global->type.val_type,
  3619. #if WASM_ENABLE_GC == 0
  3620. NULL,
  3621. #else
  3622. &ref_type,
  3623. #endif
  3624. error_buf, error_buf_size))
  3625. return false;
  3626. #if WASM_ENABLE_GC != 0
  3627. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3628. uint8 global_type;
  3629. WASMRefType *global_ref_type;
  3630. uint32 global_idx = global->init_expr.u.global_index;
  3631. if (global->init_expr.u.global_index
  3632. >= module->import_global_count + i) {
  3633. set_error_buf(error_buf, error_buf_size, "unknown global");
  3634. return false;
  3635. }
  3636. if (global_idx < module->import_global_count) {
  3637. global_type = module->import_globals[global_idx]
  3638. .u.global.type.val_type;
  3639. global_ref_type =
  3640. module->import_globals[global_idx].u.global.ref_type;
  3641. }
  3642. else {
  3643. global_type =
  3644. module
  3645. ->globals[global_idx - module->import_global_count]
  3646. .type.val_type;
  3647. global_ref_type =
  3648. module
  3649. ->globals[global_idx - module->import_global_count]
  3650. .ref_type;
  3651. }
  3652. if (!wasm_reftype_is_subtype_of(
  3653. global_type, global_ref_type, global->type.val_type,
  3654. global->ref_type, module->types, module->type_count)) {
  3655. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3656. return false;
  3657. }
  3658. }
  3659. if (need_ref_type_map) {
  3660. if (!(global->ref_type =
  3661. reftype_set_insert(module->ref_type_set, &ref_type,
  3662. error_buf, error_buf_size))) {
  3663. return false;
  3664. }
  3665. }
  3666. #if TRACE_WASM_LOADER != 0
  3667. os_printf("global type: ");
  3668. wasm_dump_value_type(global->type, global->ref_type);
  3669. os_printf("\n");
  3670. #endif
  3671. #endif
  3672. module->global_count++;
  3673. }
  3674. bh_assert(module->global_count == global_count);
  3675. }
  3676. if (p != p_end) {
  3677. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3678. return false;
  3679. }
  3680. LOG_VERBOSE("Load global section success.\n");
  3681. return true;
  3682. fail:
  3683. return false;
  3684. }
  3685. static bool
  3686. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3687. bool is_load_from_file_buf, char *error_buf,
  3688. uint32 error_buf_size)
  3689. {
  3690. const uint8 *p = buf, *p_end = buf_end;
  3691. uint32 export_count, i, j, index;
  3692. uint64 total_size;
  3693. uint32 str_len;
  3694. WASMExport *export;
  3695. const char *name;
  3696. read_leb_uint32(p, p_end, export_count);
  3697. if (export_count) {
  3698. module->export_count = export_count;
  3699. total_size = sizeof(WASMExport) * (uint64)export_count;
  3700. if (!(module->exports =
  3701. loader_malloc(total_size, error_buf, error_buf_size))) {
  3702. return false;
  3703. }
  3704. export = module->exports;
  3705. for (i = 0; i < export_count; i++, export ++) {
  3706. #if WASM_ENABLE_THREAD_MGR == 0
  3707. if (p == p_end) {
  3708. /* export section with inconsistent count:
  3709. n export declared, but less than n given */
  3710. set_error_buf(error_buf, error_buf_size,
  3711. "length out of bounds");
  3712. return false;
  3713. }
  3714. #endif
  3715. read_leb_uint32(p, p_end, str_len);
  3716. CHECK_BUF(p, p_end, str_len);
  3717. for (j = 0; j < i; j++) {
  3718. name = module->exports[j].name;
  3719. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  3720. set_error_buf(error_buf, error_buf_size,
  3721. "duplicate export name");
  3722. return false;
  3723. }
  3724. }
  3725. if (!(export->name = wasm_const_str_list_insert(
  3726. p, str_len, module, is_load_from_file_buf, error_buf,
  3727. error_buf_size))) {
  3728. return false;
  3729. }
  3730. p += str_len;
  3731. CHECK_BUF(p, p_end, 1);
  3732. export->kind = read_uint8(p);
  3733. read_leb_uint32(p, p_end, index);
  3734. export->index = index;
  3735. switch (export->kind) {
  3736. /* function index */
  3737. case EXPORT_KIND_FUNC:
  3738. if (index >= module->function_count
  3739. + module->import_function_count) {
  3740. set_error_buf(error_buf, error_buf_size,
  3741. "unknown function");
  3742. return false;
  3743. }
  3744. #if WASM_ENABLE_SIMD != 0
  3745. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3746. /* TODO: check func type, if it has v128 param or result,
  3747. report error */
  3748. #endif
  3749. #endif
  3750. break;
  3751. /* table index */
  3752. case EXPORT_KIND_TABLE:
  3753. if (index
  3754. >= module->table_count + module->import_table_count) {
  3755. set_error_buf(error_buf, error_buf_size,
  3756. "unknown table");
  3757. return false;
  3758. }
  3759. break;
  3760. /* memory index */
  3761. case EXPORT_KIND_MEMORY:
  3762. if (index
  3763. >= module->memory_count + module->import_memory_count) {
  3764. set_error_buf(error_buf, error_buf_size,
  3765. "unknown memory");
  3766. return false;
  3767. }
  3768. break;
  3769. #if WASM_ENABLE_TAGS != 0
  3770. /* export tag */
  3771. case EXPORT_KIND_TAG:
  3772. if (index >= module->tag_count + module->import_tag_count) {
  3773. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3774. return false;
  3775. }
  3776. break;
  3777. #endif
  3778. /* global index */
  3779. case EXPORT_KIND_GLOBAL:
  3780. if (index
  3781. >= module->global_count + module->import_global_count) {
  3782. set_error_buf(error_buf, error_buf_size,
  3783. "unknown global");
  3784. return false;
  3785. }
  3786. break;
  3787. default:
  3788. set_error_buf(error_buf, error_buf_size,
  3789. "invalid export kind");
  3790. return false;
  3791. }
  3792. }
  3793. }
  3794. if (p != p_end) {
  3795. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3796. return false;
  3797. }
  3798. LOG_VERBOSE("Load export section success.\n");
  3799. return true;
  3800. fail:
  3801. return false;
  3802. }
  3803. static bool
  3804. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3805. uint32 error_buf_size)
  3806. {
  3807. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3808. if (table_index != 0) {
  3809. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3810. return false;
  3811. }
  3812. #endif
  3813. if (table_index >= module->import_table_count + module->table_count) {
  3814. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3815. table_index);
  3816. return false;
  3817. }
  3818. return true;
  3819. }
  3820. static bool
  3821. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3822. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3823. {
  3824. const uint8 *p = *p_buf, *p_end = buf_end;
  3825. uint32 table_index;
  3826. read_leb_uint32(p, p_end, table_index);
  3827. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3828. return false;
  3829. }
  3830. *p_table_index = table_index;
  3831. *p_buf = p;
  3832. return true;
  3833. fail:
  3834. return false;
  3835. }
  3836. /* Element segments must match element type of table */
  3837. static bool
  3838. check_table_elem_type(WASMModule *module, uint32 table_index,
  3839. uint32 type_from_elem_seg, char *error_buf,
  3840. uint32 error_buf_size)
  3841. {
  3842. uint32 table_declared_elem_type;
  3843. if (table_index < module->import_table_count)
  3844. table_declared_elem_type =
  3845. module->import_tables[table_index].u.table.table_type.elem_type;
  3846. else
  3847. table_declared_elem_type =
  3848. (module->tables + table_index)->table_type.elem_type;
  3849. if (table_declared_elem_type == type_from_elem_seg)
  3850. return true;
  3851. #if WASM_ENABLE_GC != 0
  3852. /*
  3853. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3854. */
  3855. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3856. return true;
  3857. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3858. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3859. return true;
  3860. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3861. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3862. return true;
  3863. #endif
  3864. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3865. return false;
  3866. }
  3867. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3868. static bool
  3869. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3870. uint32 *p_elem_type,
  3871. #if WASM_ENABLE_GC != 0
  3872. WASMRefType **p_elem_ref_type,
  3873. #endif
  3874. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3875. {
  3876. const uint8 *p = *p_buf, *p_end = buf_end;
  3877. uint8 elem_type;
  3878. #if WASM_ENABLE_GC != 0
  3879. WASMRefType elem_ref_type;
  3880. bool need_ref_type_map;
  3881. #endif
  3882. CHECK_BUF(p, p_end, 1);
  3883. elem_type = read_uint8(p);
  3884. if (elemkind_zero) {
  3885. if (elem_type != 0) {
  3886. set_error_buf(error_buf, error_buf_size,
  3887. "invalid reference type or unknown type");
  3888. return false;
  3889. }
  3890. else {
  3891. *p_elem_type = VALUE_TYPE_FUNCREF;
  3892. *p_buf = p;
  3893. return true;
  3894. }
  3895. }
  3896. #if WASM_ENABLE_GC == 0
  3897. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3898. set_error_buf(error_buf, error_buf_size,
  3899. "invalid reference type or unknown type");
  3900. return false;
  3901. }
  3902. *p_elem_type = elem_type;
  3903. #else
  3904. p--;
  3905. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3906. module->type_count, &need_ref_type_map,
  3907. &elem_ref_type, false, error_buf, error_buf_size)) {
  3908. return false;
  3909. }
  3910. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3911. set_error_buf(error_buf, error_buf_size,
  3912. "invalid reference type or unknown type");
  3913. return false;
  3914. }
  3915. *p_elem_type = elem_ref_type.ref_type;
  3916. if (need_ref_type_map) {
  3917. if (!(*p_elem_ref_type =
  3918. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3919. error_buf, error_buf_size))) {
  3920. return false;
  3921. }
  3922. }
  3923. #endif
  3924. *p_buf = p;
  3925. return true;
  3926. fail:
  3927. return false;
  3928. }
  3929. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3930. static bool
  3931. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3932. WASMModule *module, WASMTableSeg *table_segment,
  3933. char *error_buf, uint32 error_buf_size)
  3934. {
  3935. const uint8 *p = *p_buf, *p_end = buf_end;
  3936. uint32 function_count, function_index = 0, i;
  3937. uint64 total_size;
  3938. read_leb_uint32(p, p_end, function_count);
  3939. table_segment->value_count = function_count;
  3940. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3941. if (total_size > 0
  3942. && !(table_segment->init_values =
  3943. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3944. error_buf_size))) {
  3945. return false;
  3946. }
  3947. for (i = 0; i < function_count; i++) {
  3948. InitializerExpression *init_expr = &table_segment->init_values[i];
  3949. read_leb_uint32(p, p_end, function_index);
  3950. if (!check_function_index(module, function_index, error_buf,
  3951. error_buf_size)) {
  3952. return false;
  3953. }
  3954. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3955. init_expr->u.ref_index = function_index;
  3956. }
  3957. *p_buf = p;
  3958. return true;
  3959. fail:
  3960. return false;
  3961. }
  3962. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3963. static bool
  3964. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3965. WASMModule *module, WASMTableSeg *table_segment,
  3966. char *error_buf, uint32 error_buf_size)
  3967. {
  3968. const uint8 *p = *p_buf, *p_end = buf_end;
  3969. uint32 ref_count, i;
  3970. uint64 total_size;
  3971. read_leb_uint32(p, p_end, ref_count);
  3972. table_segment->value_count = ref_count;
  3973. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3974. if (total_size > 0
  3975. && !(table_segment->init_values =
  3976. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3977. error_buf_size))) {
  3978. return false;
  3979. }
  3980. for (i = 0; i < ref_count; i++) {
  3981. InitializerExpression *init_expr = &table_segment->init_values[i];
  3982. if (!load_init_expr(module, &p, p_end, init_expr,
  3983. table_segment->elem_type,
  3984. #if WASM_ENABLE_GC == 0
  3985. NULL,
  3986. #else
  3987. table_segment->elem_ref_type,
  3988. #endif
  3989. error_buf, error_buf_size))
  3990. return false;
  3991. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3992. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3993. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3994. && init_expr->init_expr_type
  3995. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3996. }
  3997. *p_buf = p;
  3998. return true;
  3999. fail:
  4000. return false;
  4001. }
  4002. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  4003. static bool
  4004. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  4005. WASMModule *module, char *error_buf,
  4006. uint32 error_buf_size)
  4007. {
  4008. const uint8 *p = buf, *p_end = buf_end;
  4009. uint32 table_segment_count, i;
  4010. uint64 total_size;
  4011. WASMTableSeg *table_segment;
  4012. read_leb_uint32(p, p_end, table_segment_count);
  4013. if (table_segment_count) {
  4014. module->table_seg_count = table_segment_count;
  4015. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  4016. if (!(module->table_segments =
  4017. loader_malloc(total_size, error_buf, error_buf_size))) {
  4018. return false;
  4019. }
  4020. table_segment = module->table_segments;
  4021. for (i = 0; i < table_segment_count; i++, table_segment++) {
  4022. if (p >= p_end) {
  4023. set_error_buf(error_buf, error_buf_size,
  4024. "invalid value type or "
  4025. "invalid elements segment kind");
  4026. return false;
  4027. }
  4028. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4029. read_leb_uint32(p, p_end, table_segment->mode);
  4030. /* last three bits */
  4031. table_segment->mode = table_segment->mode & 0x07;
  4032. switch (table_segment->mode) {
  4033. /* elemkind/elemtype + active */
  4034. case 0:
  4035. case 4:
  4036. {
  4037. #if WASM_ENABLE_GC != 0
  4038. if (table_segment->mode == 0) {
  4039. /* vec(funcidx), set elem type to (ref func) */
  4040. WASMRefType elem_ref_type = { 0 };
  4041. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4042. wasm_set_refheaptype_common(
  4043. &elem_ref_type.ref_ht_common, false,
  4044. HEAP_TYPE_FUNC);
  4045. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4046. module->ref_type_set, &elem_ref_type,
  4047. error_buf, error_buf_size)))
  4048. return false;
  4049. }
  4050. else {
  4051. /* vec(expr), set elem type to funcref */
  4052. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4053. }
  4054. #else
  4055. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4056. #endif
  4057. table_segment->table_index = 0;
  4058. if (!check_table_index(module, table_segment->table_index,
  4059. error_buf, error_buf_size))
  4060. return false;
  4061. if (!load_init_expr(
  4062. module, &p, p_end, &table_segment->base_offset,
  4063. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4064. return false;
  4065. if (table_segment->mode == 0) {
  4066. /* vec(funcidx) */
  4067. if (!load_func_index_vec(&p, p_end, module,
  4068. table_segment, error_buf,
  4069. error_buf_size))
  4070. return false;
  4071. }
  4072. else {
  4073. /* vec(expr) */
  4074. if (!load_init_expr_vec(&p, p_end, module,
  4075. table_segment, error_buf,
  4076. error_buf_size))
  4077. return false;
  4078. }
  4079. if (!check_table_elem_type(module,
  4080. table_segment->table_index,
  4081. table_segment->elem_type,
  4082. error_buf, error_buf_size))
  4083. return false;
  4084. break;
  4085. }
  4086. /* elemkind + passive/declarative */
  4087. case 1:
  4088. case 3:
  4089. if (!load_elem_type(module, &p, p_end,
  4090. &table_segment->elem_type,
  4091. #if WASM_ENABLE_GC != 0
  4092. &table_segment->elem_ref_type,
  4093. #endif
  4094. true, error_buf, error_buf_size))
  4095. return false;
  4096. /* vec(funcidx) */
  4097. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4098. error_buf, error_buf_size))
  4099. return false;
  4100. break;
  4101. /* elemkind/elemtype + table_idx + active */
  4102. case 2:
  4103. case 6:
  4104. if (!load_table_index(&p, p_end, module,
  4105. &table_segment->table_index,
  4106. error_buf, error_buf_size))
  4107. return false;
  4108. if (!load_init_expr(
  4109. module, &p, p_end, &table_segment->base_offset,
  4110. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4111. return false;
  4112. if (!load_elem_type(module, &p, p_end,
  4113. &table_segment->elem_type,
  4114. #if WASM_ENABLE_GC != 0
  4115. &table_segment->elem_ref_type,
  4116. #endif
  4117. table_segment->mode == 2 ? true : false,
  4118. error_buf, error_buf_size))
  4119. return false;
  4120. if (table_segment->mode == 2) {
  4121. /* vec(funcidx) */
  4122. if (!load_func_index_vec(&p, p_end, module,
  4123. table_segment, error_buf,
  4124. error_buf_size))
  4125. return false;
  4126. }
  4127. else {
  4128. /* vec(expr) */
  4129. if (!load_init_expr_vec(&p, p_end, module,
  4130. table_segment, error_buf,
  4131. error_buf_size))
  4132. return false;
  4133. }
  4134. if (!check_table_elem_type(module,
  4135. table_segment->table_index,
  4136. table_segment->elem_type,
  4137. error_buf, error_buf_size))
  4138. return false;
  4139. break;
  4140. case 5:
  4141. case 7:
  4142. if (!load_elem_type(module, &p, p_end,
  4143. &table_segment->elem_type,
  4144. #if WASM_ENABLE_GC != 0
  4145. &table_segment->elem_ref_type,
  4146. #endif
  4147. false, error_buf, error_buf_size))
  4148. return false;
  4149. /* vec(expr) */
  4150. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4151. error_buf, error_buf_size))
  4152. return false;
  4153. break;
  4154. default:
  4155. set_error_buf(error_buf, error_buf_size,
  4156. "unknown element segment kind");
  4157. return false;
  4158. }
  4159. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4160. /*
  4161. * like: 00 41 05 0b 04 00 01 00 01
  4162. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4163. */
  4164. if (!load_table_index(&p, p_end, module,
  4165. &table_segment->table_index, error_buf,
  4166. error_buf_size))
  4167. return false;
  4168. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4169. VALUE_TYPE_I32, NULL, error_buf,
  4170. error_buf_size))
  4171. return false;
  4172. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4173. error_buf, error_buf_size))
  4174. return false;
  4175. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4176. if (!check_table_elem_type(module, table_segment->table_index,
  4177. table_segment->elem_type, error_buf,
  4178. error_buf_size))
  4179. return false;
  4180. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4181. #if WASM_ENABLE_WAMR_COMPILER != 0
  4182. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4183. module->is_ref_types_used = true;
  4184. #endif
  4185. }
  4186. }
  4187. if (p != p_end) {
  4188. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4189. return false;
  4190. }
  4191. LOG_VERBOSE("Load table segment section success.\n");
  4192. return true;
  4193. fail:
  4194. return false;
  4195. }
  4196. static bool
  4197. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4198. WASMModule *module,
  4199. #if WASM_ENABLE_BULK_MEMORY != 0
  4200. bool has_datacount_section,
  4201. #endif
  4202. bool clone_data_seg, char *error_buf,
  4203. uint32 error_buf_size)
  4204. {
  4205. const uint8 *p = buf, *p_end = buf_end;
  4206. uint32 data_seg_count, i, mem_index, data_seg_len;
  4207. uint64 total_size;
  4208. WASMDataSeg *dataseg;
  4209. InitializerExpression init_expr;
  4210. #if WASM_ENABLE_BULK_MEMORY != 0
  4211. bool is_passive = false;
  4212. uint32 mem_flag;
  4213. #endif
  4214. uint8 mem_offset_type = VALUE_TYPE_I32;
  4215. read_leb_uint32(p, p_end, data_seg_count);
  4216. #if WASM_ENABLE_BULK_MEMORY != 0
  4217. if (has_datacount_section && data_seg_count != module->data_seg_count1) {
  4218. set_error_buf(error_buf, error_buf_size,
  4219. "data count and data section have inconsistent lengths");
  4220. return false;
  4221. }
  4222. #endif
  4223. if (data_seg_count) {
  4224. module->data_seg_count = data_seg_count;
  4225. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4226. if (!(module->data_segments =
  4227. loader_malloc(total_size, error_buf, error_buf_size))) {
  4228. return false;
  4229. }
  4230. for (i = 0; i < data_seg_count; i++) {
  4231. read_leb_uint32(p, p_end, mem_index);
  4232. #if WASM_ENABLE_BULK_MEMORY != 0
  4233. is_passive = false;
  4234. mem_flag = mem_index & 0x03;
  4235. switch (mem_flag) {
  4236. case 0x01:
  4237. is_passive = true;
  4238. #if WASM_ENABLE_WAMR_COMPILER != 0
  4239. module->is_bulk_memory_used = true;
  4240. #endif
  4241. break;
  4242. case 0x00:
  4243. /* no memory index, treat index as 0 */
  4244. mem_index = 0;
  4245. goto check_mem_index;
  4246. case 0x02:
  4247. /* read following memory index */
  4248. read_leb_uint32(p, p_end, mem_index);
  4249. #if WASM_ENABLE_WAMR_COMPILER != 0
  4250. module->is_bulk_memory_used = true;
  4251. #endif
  4252. check_mem_index:
  4253. if (mem_index
  4254. >= module->import_memory_count + module->memory_count) {
  4255. set_error_buf_v(error_buf, error_buf_size,
  4256. "unknown memory %d", mem_index);
  4257. return false;
  4258. }
  4259. break;
  4260. case 0x03:
  4261. default:
  4262. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4263. return false;
  4264. break;
  4265. }
  4266. #else
  4267. if (mem_index
  4268. >= module->import_memory_count + module->memory_count) {
  4269. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4270. mem_index);
  4271. return false;
  4272. }
  4273. #endif /* WASM_ENABLE_BULK_MEMORY */
  4274. #if WASM_ENABLE_BULK_MEMORY != 0
  4275. if (!is_passive)
  4276. #endif
  4277. {
  4278. #if WASM_ENABLE_MEMORY64 != 0
  4279. /* This memory_flag is from memory instead of data segment */
  4280. uint8 memory_flag;
  4281. if (module->import_memory_count > 0) {
  4282. memory_flag = module->import_memories[mem_index]
  4283. .u.memory.mem_type.flags;
  4284. }
  4285. else {
  4286. memory_flag =
  4287. module
  4288. ->memories[mem_index - module->import_memory_count]
  4289. .flags;
  4290. }
  4291. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4292. : VALUE_TYPE_I32;
  4293. #else
  4294. mem_offset_type = VALUE_TYPE_I32;
  4295. #endif
  4296. }
  4297. #if WASM_ENABLE_BULK_MEMORY != 0
  4298. if (!is_passive)
  4299. #endif
  4300. if (!load_init_expr(module, &p, p_end, &init_expr,
  4301. mem_offset_type, NULL, error_buf,
  4302. error_buf_size))
  4303. return false;
  4304. read_leb_uint32(p, p_end, data_seg_len);
  4305. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4306. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4307. return false;
  4308. }
  4309. #if WASM_ENABLE_BULK_MEMORY != 0
  4310. dataseg->is_passive = is_passive;
  4311. if (!is_passive)
  4312. #endif
  4313. {
  4314. bh_memcpy_s(&dataseg->base_offset,
  4315. sizeof(InitializerExpression), &init_expr,
  4316. sizeof(InitializerExpression));
  4317. dataseg->memory_index = mem_index;
  4318. }
  4319. dataseg->data_length = data_seg_len;
  4320. CHECK_BUF(p, p_end, data_seg_len);
  4321. if (clone_data_seg) {
  4322. if (!(dataseg->data = loader_malloc(
  4323. dataseg->data_length, error_buf, error_buf_size))) {
  4324. return false;
  4325. }
  4326. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4327. data_seg_len);
  4328. }
  4329. else {
  4330. dataseg->data = (uint8 *)p;
  4331. }
  4332. dataseg->is_data_cloned = clone_data_seg;
  4333. p += data_seg_len;
  4334. }
  4335. }
  4336. if (p != p_end) {
  4337. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4338. return false;
  4339. }
  4340. LOG_VERBOSE("Load data segment section success.\n");
  4341. return true;
  4342. fail:
  4343. return false;
  4344. }
  4345. #if WASM_ENABLE_BULK_MEMORY != 0
  4346. static bool
  4347. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4348. WASMModule *module, char *error_buf,
  4349. uint32 error_buf_size)
  4350. {
  4351. const uint8 *p = buf, *p_end = buf_end;
  4352. uint32 data_seg_count1 = 0;
  4353. read_leb_uint32(p, p_end, data_seg_count1);
  4354. module->data_seg_count1 = data_seg_count1;
  4355. if (p != p_end) {
  4356. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4357. return false;
  4358. }
  4359. #if WASM_ENABLE_WAMR_COMPILER != 0
  4360. module->is_bulk_memory_used = true;
  4361. #endif
  4362. LOG_VERBOSE("Load datacount section success.\n");
  4363. return true;
  4364. fail:
  4365. return false;
  4366. }
  4367. #endif
  4368. #if WASM_ENABLE_TAGS != 0
  4369. static bool
  4370. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4371. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4372. uint32 error_buf_size)
  4373. {
  4374. (void)buf_code;
  4375. (void)buf_code_end;
  4376. const uint8 *p = buf, *p_end = buf_end;
  4377. size_t total_size = 0;
  4378. /* number of tags defined in the section */
  4379. uint32 section_tag_count = 0;
  4380. uint8 tag_attribute;
  4381. uint32 tag_type;
  4382. WASMTag *tag = NULL;
  4383. /* get tag count */
  4384. read_leb_uint32(p, p_end, section_tag_count);
  4385. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4386. error_buf, error_buf_size))
  4387. return false;
  4388. module->tag_count = section_tag_count;
  4389. if (section_tag_count) {
  4390. total_size = sizeof(WASMTag *) * module->tag_count;
  4391. if (!(module->tags =
  4392. loader_malloc(total_size, error_buf, error_buf_size))) {
  4393. return false;
  4394. }
  4395. /* load each tag, imported tags precede the tags */
  4396. uint32 tag_index;
  4397. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4398. /* get the one byte attribute */
  4399. CHECK_BUF(p, p_end, 1);
  4400. tag_attribute = read_uint8(p);
  4401. /* get type */
  4402. read_leb_uint32(p, p_end, tag_type);
  4403. /* compare against module->types */
  4404. if (tag_type >= module->type_count) {
  4405. set_error_buf(error_buf, error_buf_size, "unknown type");
  4406. return false;
  4407. }
  4408. /* get return type (must be 0) */
  4409. /* check, that the type of the referred tag returns void */
  4410. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4411. if (func_type->result_count != 0) {
  4412. set_error_buf(error_buf, error_buf_size,
  4413. "non-empty tag result type");
  4414. goto fail;
  4415. }
  4416. if (!(tag = module->tags[tag_index] = loader_malloc(
  4417. sizeof(WASMTag), error_buf, error_buf_size))) {
  4418. return false;
  4419. }
  4420. /* store to module tag declarations */
  4421. tag->attribute = tag_attribute;
  4422. tag->type = tag_type;
  4423. tag->tag_type = func_type;
  4424. }
  4425. }
  4426. if (p != p_end) {
  4427. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4428. return false;
  4429. }
  4430. LOG_VERBOSE("Load tag section success.\n");
  4431. return true;
  4432. fail:
  4433. return false;
  4434. }
  4435. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4436. static bool
  4437. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4438. const uint8 *buf_func_end, WASMModule *module,
  4439. char *error_buf, uint32 error_buf_size)
  4440. {
  4441. const uint8 *p = buf, *p_end = buf_end;
  4442. const uint8 *p_func = buf_func;
  4443. uint32 func_count = 0, code_count;
  4444. /* code has been loaded in function section, so pass it here, just check
  4445. * whether function and code section have inconsistent lengths */
  4446. read_leb_uint32(p, p_end, code_count);
  4447. if (buf_func)
  4448. read_leb_uint32(p_func, buf_func_end, func_count);
  4449. if (func_count != code_count) {
  4450. set_error_buf(error_buf, error_buf_size,
  4451. "function and code section have inconsistent lengths");
  4452. return false;
  4453. }
  4454. LOG_VERBOSE("Load code segment section success.\n");
  4455. (void)module;
  4456. return true;
  4457. fail:
  4458. return false;
  4459. }
  4460. static bool
  4461. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4462. char *error_buf, uint32 error_buf_size)
  4463. {
  4464. const uint8 *p = buf, *p_end = buf_end;
  4465. WASMFuncType *type;
  4466. uint32 start_function;
  4467. read_leb_uint32(p, p_end, start_function);
  4468. if (start_function
  4469. >= module->function_count + module->import_function_count) {
  4470. set_error_buf(error_buf, error_buf_size, "unknown function");
  4471. return false;
  4472. }
  4473. if (start_function < module->import_function_count)
  4474. type = module->import_functions[start_function].u.function.func_type;
  4475. else
  4476. type = module->functions[start_function - module->import_function_count]
  4477. ->func_type;
  4478. if (type->param_count != 0 || type->result_count != 0) {
  4479. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4480. return false;
  4481. }
  4482. module->start_function = start_function;
  4483. if (p != p_end) {
  4484. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4485. return false;
  4486. }
  4487. LOG_VERBOSE("Load start section success.\n");
  4488. return true;
  4489. fail:
  4490. return false;
  4491. }
  4492. #if WASM_ENABLE_STRINGREF != 0
  4493. static bool
  4494. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4495. WASMModule *module, bool is_load_from_file_buf,
  4496. char *error_buf, uint32 error_buf_size)
  4497. {
  4498. const uint8 *p = buf, *p_end = buf_end;
  4499. int32 deferred_count, immediate_count, string_length, i;
  4500. uint64 total_size;
  4501. read_leb_uint32(p, p_end, deferred_count);
  4502. read_leb_uint32(p, p_end, immediate_count);
  4503. /* proposal set deferred_count for future extension */
  4504. if (deferred_count != 0) {
  4505. goto fail;
  4506. }
  4507. if (immediate_count > 0) {
  4508. total_size = sizeof(char *) * (uint64)immediate_count;
  4509. if (!(module->string_literal_ptrs =
  4510. loader_malloc(total_size, error_buf, error_buf_size))) {
  4511. goto fail;
  4512. }
  4513. module->string_literal_count = immediate_count;
  4514. total_size = sizeof(uint32) * (uint64)immediate_count;
  4515. if (!(module->string_literal_lengths =
  4516. loader_malloc(total_size, error_buf, error_buf_size))) {
  4517. goto fail;
  4518. }
  4519. for (i = 0; i < immediate_count; i++) {
  4520. read_leb_uint32(p, p_end, string_length);
  4521. CHECK_BUF(p, p_end, string_length);
  4522. module->string_literal_ptrs[i] = p;
  4523. module->string_literal_lengths[i] = string_length;
  4524. p += string_length;
  4525. }
  4526. }
  4527. if (p != p_end) {
  4528. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4529. goto fail;
  4530. }
  4531. LOG_VERBOSE("Load stringref section success.\n");
  4532. return true;
  4533. fail:
  4534. return false;
  4535. }
  4536. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4537. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4538. static bool
  4539. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4540. bool is_load_from_file_buf, char *error_buf,
  4541. uint32 error_buf_size)
  4542. {
  4543. const uint8 *p = buf, *p_end = buf_end;
  4544. uint32 name_type, subsection_size;
  4545. uint32 previous_name_type = 0;
  4546. uint32 num_func_name;
  4547. uint32 func_index;
  4548. uint32 previous_func_index = ~0U;
  4549. uint32 func_name_len;
  4550. uint32 name_index;
  4551. int i = 0;
  4552. if (p >= p_end) {
  4553. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4554. return false;
  4555. }
  4556. while (p < p_end) {
  4557. read_leb_uint32(p, p_end, name_type);
  4558. if (i != 0) {
  4559. if (name_type == previous_name_type) {
  4560. set_error_buf(error_buf, error_buf_size,
  4561. "duplicate sub-section");
  4562. return false;
  4563. }
  4564. if (name_type < previous_name_type) {
  4565. set_error_buf(error_buf, error_buf_size,
  4566. "out-of-order sub-section");
  4567. return false;
  4568. }
  4569. }
  4570. previous_name_type = name_type;
  4571. read_leb_uint32(p, p_end, subsection_size);
  4572. CHECK_BUF(p, p_end, subsection_size);
  4573. switch (name_type) {
  4574. case SUB_SECTION_TYPE_FUNC:
  4575. if (subsection_size) {
  4576. read_leb_uint32(p, p_end, num_func_name);
  4577. for (name_index = 0; name_index < num_func_name;
  4578. name_index++) {
  4579. read_leb_uint32(p, p_end, func_index);
  4580. if (func_index == previous_func_index) {
  4581. set_error_buf(error_buf, error_buf_size,
  4582. "duplicate function name");
  4583. return false;
  4584. }
  4585. if (func_index < previous_func_index
  4586. && previous_func_index != ~0U) {
  4587. set_error_buf(error_buf, error_buf_size,
  4588. "out-of-order function index ");
  4589. return false;
  4590. }
  4591. previous_func_index = func_index;
  4592. read_leb_uint32(p, p_end, func_name_len);
  4593. CHECK_BUF(p, p_end, func_name_len);
  4594. /* Skip the import functions */
  4595. if (func_index >= module->import_function_count) {
  4596. func_index -= module->import_function_count;
  4597. if (func_index >= module->function_count) {
  4598. set_error_buf(error_buf, error_buf_size,
  4599. "out-of-range function index");
  4600. return false;
  4601. }
  4602. if (!(module->functions[func_index]->field_name =
  4603. wasm_const_str_list_insert(
  4604. p, func_name_len, module,
  4605. #if WASM_ENABLE_WAMR_COMPILER != 0
  4606. false,
  4607. #else
  4608. is_load_from_file_buf,
  4609. #endif
  4610. error_buf, error_buf_size))) {
  4611. return false;
  4612. }
  4613. }
  4614. p += func_name_len;
  4615. }
  4616. }
  4617. break;
  4618. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4619. */
  4620. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4621. default:
  4622. p = p + subsection_size;
  4623. break;
  4624. }
  4625. i++;
  4626. }
  4627. return true;
  4628. fail:
  4629. return false;
  4630. }
  4631. #endif
  4632. static bool
  4633. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4634. bool is_load_from_file_buf, char *error_buf,
  4635. uint32 error_buf_size)
  4636. {
  4637. const uint8 *p = buf, *p_end = buf_end;
  4638. char section_name[32];
  4639. uint32 name_len, buffer_len;
  4640. if (p >= p_end) {
  4641. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4642. return false;
  4643. }
  4644. read_leb_uint32(p, p_end, name_len);
  4645. if (p + name_len > p_end) {
  4646. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4647. return false;
  4648. }
  4649. if (!wasm_check_utf8_str(p, name_len)) {
  4650. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4651. return false;
  4652. }
  4653. buffer_len = sizeof(section_name);
  4654. memset(section_name, 0, buffer_len);
  4655. if (name_len < buffer_len) {
  4656. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4657. }
  4658. else {
  4659. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4660. memset(section_name + buffer_len - 4, '.', 3);
  4661. }
  4662. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4663. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4664. module->name_section_buf = buf;
  4665. module->name_section_buf_end = buf_end;
  4666. p += name_len;
  4667. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4668. error_buf, error_buf_size)) {
  4669. return false;
  4670. }
  4671. LOG_VERBOSE("Load custom name section success.");
  4672. }
  4673. #endif
  4674. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4675. {
  4676. WASMCustomSection *section =
  4677. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4678. if (!section) {
  4679. return false;
  4680. }
  4681. section->name_addr = (char *)p;
  4682. section->name_len = name_len;
  4683. section->content_addr = (uint8 *)(p + name_len);
  4684. section->content_len = (uint32)(p_end - p - name_len);
  4685. section->next = module->custom_section_list;
  4686. module->custom_section_list = section;
  4687. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4688. return true;
  4689. }
  4690. #endif
  4691. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4692. (void)is_load_from_file_buf;
  4693. (void)module;
  4694. return true;
  4695. fail:
  4696. return false;
  4697. }
  4698. static void
  4699. calculate_global_data_offset(WASMModule *module)
  4700. {
  4701. uint32 i, data_offset;
  4702. data_offset = 0;
  4703. for (i = 0; i < module->import_global_count; i++) {
  4704. WASMGlobalImport *import_global =
  4705. &((module->import_globals + i)->u.global);
  4706. #if WASM_ENABLE_FAST_JIT != 0
  4707. import_global->data_offset = data_offset;
  4708. #endif
  4709. data_offset += wasm_value_type_size(import_global->type.val_type);
  4710. }
  4711. for (i = 0; i < module->global_count; i++) {
  4712. WASMGlobal *global = module->globals + i;
  4713. #if WASM_ENABLE_FAST_JIT != 0
  4714. global->data_offset = data_offset;
  4715. #endif
  4716. data_offset += wasm_value_type_size(global->type.val_type);
  4717. }
  4718. module->global_data_size = data_offset;
  4719. }
  4720. #if WASM_ENABLE_FAST_JIT != 0
  4721. static bool
  4722. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4723. uint32 error_buf_size)
  4724. {
  4725. #if WASM_ENABLE_LAZY_JIT != 0
  4726. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4727. #endif
  4728. uint32 i;
  4729. if (!module->function_count)
  4730. return true;
  4731. if (!(module->fast_jit_func_ptrs =
  4732. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4733. error_buf_size))) {
  4734. return false;
  4735. }
  4736. #if WASM_ENABLE_LAZY_JIT != 0
  4737. for (i = 0; i < module->function_count; i++) {
  4738. module->fast_jit_func_ptrs[i] =
  4739. jit_globals->compile_fast_jit_and_then_call;
  4740. }
  4741. #endif
  4742. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4743. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4744. set_error_buf(error_buf, error_buf_size,
  4745. "init fast jit thread lock failed");
  4746. return false;
  4747. }
  4748. module->fast_jit_thread_locks_inited[i] = true;
  4749. }
  4750. return true;
  4751. }
  4752. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4753. #if WASM_ENABLE_JIT != 0
  4754. static bool
  4755. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4756. uint32 error_buf_size)
  4757. {
  4758. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4759. AOTCompOption option = { 0 };
  4760. char *aot_last_error;
  4761. uint64 size;
  4762. #if WASM_ENABLE_GC != 0
  4763. bool gc_enabled = true;
  4764. #else
  4765. bool gc_enabled = false;
  4766. #endif
  4767. if (module->function_count == 0)
  4768. return true;
  4769. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4770. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4771. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4772. return false;
  4773. }
  4774. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4775. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4776. os_mutex_destroy(&module->tierup_wait_lock);
  4777. return false;
  4778. }
  4779. module->tierup_wait_lock_inited = true;
  4780. #endif
  4781. size = sizeof(void *) * (uint64)module->function_count
  4782. + sizeof(bool) * (uint64)module->function_count;
  4783. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4784. return false;
  4785. }
  4786. module->func_ptrs_compiled =
  4787. (bool *)((uint8 *)module->func_ptrs
  4788. + sizeof(void *) * module->function_count);
  4789. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4790. if (!module->comp_data) {
  4791. aot_last_error = aot_get_last_error();
  4792. bh_assert(aot_last_error != NULL);
  4793. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4794. return false;
  4795. }
  4796. option.is_jit_mode = true;
  4797. option.opt_level = llvm_jit_options->opt_level;
  4798. option.size_level = llvm_jit_options->size_level;
  4799. option.segue_flags = llvm_jit_options->segue_flags;
  4800. option.quick_invoke_c_api_import =
  4801. llvm_jit_options->quick_invoke_c_api_import;
  4802. #if WASM_ENABLE_BULK_MEMORY != 0
  4803. option.enable_bulk_memory = true;
  4804. #endif
  4805. #if WASM_ENABLE_THREAD_MGR != 0
  4806. option.enable_thread_mgr = true;
  4807. #endif
  4808. #if WASM_ENABLE_TAIL_CALL != 0
  4809. option.enable_tail_call = true;
  4810. #endif
  4811. #if WASM_ENABLE_SIMD != 0
  4812. option.enable_simd = true;
  4813. #endif
  4814. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4815. option.enable_ref_types = true;
  4816. #elif WASM_ENABLE_GC != 0
  4817. option.enable_gc = true;
  4818. #endif
  4819. option.enable_aux_stack_check = true;
  4820. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4821. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4822. option.enable_aux_stack_frame = true;
  4823. #endif
  4824. #if WASM_ENABLE_PERF_PROFILING != 0
  4825. option.enable_perf_profiling = true;
  4826. #endif
  4827. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4828. option.enable_memory_profiling = true;
  4829. option.enable_stack_estimation = true;
  4830. #endif
  4831. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4832. if (!module->comp_ctx) {
  4833. aot_last_error = aot_get_last_error();
  4834. bh_assert(aot_last_error != NULL);
  4835. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4836. return false;
  4837. }
  4838. return true;
  4839. }
  4840. static bool
  4841. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4842. uint32 error_buf_size)
  4843. {
  4844. char *aot_last_error;
  4845. uint32 i;
  4846. if (module->function_count == 0)
  4847. return true;
  4848. if (!aot_compile_wasm(module->comp_ctx)) {
  4849. aot_last_error = aot_get_last_error();
  4850. bh_assert(aot_last_error != NULL);
  4851. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4852. return false;
  4853. }
  4854. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4855. if (module->orcjit_stop_compiling)
  4856. return false;
  4857. #endif
  4858. bh_print_time("Begin to lookup llvm jit functions");
  4859. for (i = 0; i < module->function_count; i++) {
  4860. LLVMOrcJITTargetAddress func_addr = 0;
  4861. LLVMErrorRef error;
  4862. char func_name[48];
  4863. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4864. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4865. func_name);
  4866. if (error != LLVMErrorSuccess) {
  4867. char *err_msg = LLVMGetErrorMessage(error);
  4868. set_error_buf_v(error_buf, error_buf_size,
  4869. "failed to compile llvm jit function: %s", err_msg);
  4870. LLVMDisposeErrorMessage(err_msg);
  4871. return false;
  4872. }
  4873. /**
  4874. * No need to lock the func_ptr[func_idx] here as it is basic
  4875. * data type, the load/store for it can be finished by one cpu
  4876. * instruction, and there can be only one cpu instruction
  4877. * loading/storing at the same time.
  4878. */
  4879. module->func_ptrs[i] = (void *)func_addr;
  4880. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4881. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4882. if (module->orcjit_stop_compiling)
  4883. return false;
  4884. #endif
  4885. }
  4886. bh_print_time("End lookup llvm jit functions");
  4887. return true;
  4888. }
  4889. #endif /* end of WASM_ENABLE_JIT != 0 */
  4890. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4891. && WASM_ENABLE_LAZY_JIT != 0
  4892. static void *
  4893. init_llvm_jit_functions_stage2_callback(void *arg)
  4894. {
  4895. WASMModule *module = (WASMModule *)arg;
  4896. char error_buf[128];
  4897. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4898. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4899. module->orcjit_stop_compiling = true;
  4900. return NULL;
  4901. }
  4902. os_mutex_lock(&module->tierup_wait_lock);
  4903. module->llvm_jit_inited = true;
  4904. os_cond_broadcast(&module->tierup_wait_cond);
  4905. os_mutex_unlock(&module->tierup_wait_lock);
  4906. return NULL;
  4907. }
  4908. #endif
  4909. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4910. /* The callback function to compile jit functions */
  4911. static void *
  4912. orcjit_thread_callback(void *arg)
  4913. {
  4914. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4915. #if WASM_ENABLE_JIT != 0
  4916. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4917. #endif
  4918. WASMModule *module = thread_arg->module;
  4919. uint32 group_idx = thread_arg->group_idx;
  4920. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4921. uint32 func_count = module->function_count;
  4922. uint32 i;
  4923. #if WASM_ENABLE_FAST_JIT != 0
  4924. /* Compile fast jit functions of this group */
  4925. for (i = group_idx; i < func_count; i += group_stride) {
  4926. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4927. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4928. break;
  4929. }
  4930. if (module->orcjit_stop_compiling) {
  4931. return NULL;
  4932. }
  4933. }
  4934. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4935. os_mutex_lock(&module->tierup_wait_lock);
  4936. module->fast_jit_ready_groups++;
  4937. os_mutex_unlock(&module->tierup_wait_lock);
  4938. #endif
  4939. #endif
  4940. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4941. && WASM_ENABLE_LAZY_JIT != 0
  4942. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4943. for (i = group_idx; i < func_count;
  4944. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4945. uint32 j;
  4946. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4947. if (i + j * group_stride < func_count) {
  4948. if (!jit_compiler_set_call_to_fast_jit(
  4949. module,
  4950. i + j * group_stride + module->import_function_count)) {
  4951. LOG_ERROR(
  4952. "failed to compile call_to_fast_jit for func %u\n",
  4953. i + j * group_stride + module->import_function_count);
  4954. module->orcjit_stop_compiling = true;
  4955. return NULL;
  4956. }
  4957. }
  4958. if (module->orcjit_stop_compiling) {
  4959. return NULL;
  4960. }
  4961. }
  4962. }
  4963. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4964. fast jit functions are compiled */
  4965. os_mutex_lock(&module->tierup_wait_lock);
  4966. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4967. && module->fast_jit_ready_groups >= group_stride)) {
  4968. os_cond_reltimedwait(&module->tierup_wait_cond,
  4969. &module->tierup_wait_lock, 10000);
  4970. if (module->orcjit_stop_compiling) {
  4971. /* init_llvm_jit_functions_stage2 failed */
  4972. os_mutex_unlock(&module->tierup_wait_lock);
  4973. return NULL;
  4974. }
  4975. }
  4976. os_mutex_unlock(&module->tierup_wait_lock);
  4977. #endif
  4978. #if WASM_ENABLE_JIT != 0
  4979. /* Compile llvm jit functions of this group */
  4980. for (i = group_idx; i < func_count;
  4981. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4982. LLVMOrcJITTargetAddress func_addr = 0;
  4983. LLVMErrorRef error;
  4984. char func_name[48];
  4985. typedef void (*F)(void);
  4986. union {
  4987. F f;
  4988. void *v;
  4989. } u;
  4990. uint32 j;
  4991. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  4992. "_wrapper");
  4993. LOG_DEBUG("compile llvm jit func %s", func_name);
  4994. error =
  4995. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  4996. if (error != LLVMErrorSuccess) {
  4997. char *err_msg = LLVMGetErrorMessage(error);
  4998. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  4999. LLVMDisposeErrorMessage(err_msg);
  5000. break;
  5001. }
  5002. /* Call the jit wrapper function to trigger its compilation, so as
  5003. to compile the actual jit functions, since we add the latter to
  5004. function list in the PartitionFunction callback */
  5005. u.v = (void *)func_addr;
  5006. u.f();
  5007. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5008. if (i + j * group_stride < func_count) {
  5009. module->func_ptrs_compiled[i + j * group_stride] = true;
  5010. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5011. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5012. i + j * group_stride);
  5013. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5014. func_name);
  5015. if (error != LLVMErrorSuccess) {
  5016. char *err_msg = LLVMGetErrorMessage(error);
  5017. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5018. err_msg);
  5019. LLVMDisposeErrorMessage(err_msg);
  5020. /* Ignore current llvm jit func, as its func ptr is
  5021. previous set to call_to_fast_jit, which also works */
  5022. continue;
  5023. }
  5024. jit_compiler_set_llvm_jit_func_ptr(
  5025. module,
  5026. i + j * group_stride + module->import_function_count,
  5027. (void *)func_addr);
  5028. /* Try to switch to call this llvm jit function instead of
  5029. fast jit function from fast jit jitted code */
  5030. jit_compiler_set_call_to_llvm_jit(
  5031. module,
  5032. i + j * group_stride + module->import_function_count);
  5033. #endif
  5034. }
  5035. }
  5036. if (module->orcjit_stop_compiling) {
  5037. break;
  5038. }
  5039. }
  5040. #endif
  5041. return NULL;
  5042. }
  5043. static void
  5044. orcjit_stop_compile_threads(WASMModule *module)
  5045. {
  5046. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5047. / sizeof(OrcJitThreadArg));
  5048. module->orcjit_stop_compiling = true;
  5049. for (i = 0; i < thread_num; i++) {
  5050. if (module->orcjit_threads[i])
  5051. os_thread_join(module->orcjit_threads[i], NULL);
  5052. }
  5053. }
  5054. static bool
  5055. compile_jit_functions(WASMModule *module, char *error_buf,
  5056. uint32 error_buf_size)
  5057. {
  5058. uint32 thread_num =
  5059. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5060. uint32 i, j;
  5061. bh_print_time("Begin to compile jit functions");
  5062. /* Create threads to compile the jit functions */
  5063. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5064. #if WASM_ENABLE_JIT != 0
  5065. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5066. #endif
  5067. module->orcjit_thread_args[i].module = module;
  5068. module->orcjit_thread_args[i].group_idx = i;
  5069. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5070. (void *)&module->orcjit_thread_args[i],
  5071. APP_THREAD_STACK_SIZE_DEFAULT)
  5072. != 0) {
  5073. set_error_buf(error_buf, error_buf_size,
  5074. "create orcjit compile thread failed");
  5075. /* Terminate the threads created */
  5076. module->orcjit_stop_compiling = true;
  5077. for (j = 0; j < i; j++) {
  5078. os_thread_join(module->orcjit_threads[j], NULL);
  5079. }
  5080. return false;
  5081. }
  5082. }
  5083. #if WASM_ENABLE_LAZY_JIT == 0
  5084. /* Wait until all jit functions are compiled for eager mode */
  5085. for (i = 0; i < thread_num; i++) {
  5086. if (module->orcjit_threads[i])
  5087. os_thread_join(module->orcjit_threads[i], NULL);
  5088. }
  5089. #if WASM_ENABLE_FAST_JIT != 0
  5090. /* Ensure all the fast-jit functions are compiled */
  5091. for (i = 0; i < module->function_count; i++) {
  5092. if (!jit_compiler_is_compiled(module,
  5093. i + module->import_function_count)) {
  5094. set_error_buf(error_buf, error_buf_size,
  5095. "failed to compile fast jit function");
  5096. return false;
  5097. }
  5098. }
  5099. #endif
  5100. #if WASM_ENABLE_JIT != 0
  5101. /* Ensure all the llvm-jit functions are compiled */
  5102. for (i = 0; i < module->function_count; i++) {
  5103. if (!module->func_ptrs_compiled[i]) {
  5104. set_error_buf(error_buf, error_buf_size,
  5105. "failed to compile llvm jit function");
  5106. return false;
  5107. }
  5108. }
  5109. #endif
  5110. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5111. bh_print_time("End compile jit functions");
  5112. return true;
  5113. }
  5114. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5115. static bool
  5116. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5117. uint32 cur_func_idx, char *error_buf,
  5118. uint32 error_buf_size);
  5119. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5120. void **
  5121. wasm_interp_get_handle_table();
  5122. static void **handle_table;
  5123. #endif
  5124. static bool
  5125. load_from_sections(WASMModule *module, WASMSection *sections,
  5126. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5127. char *error_buf, uint32 error_buf_size)
  5128. {
  5129. WASMExport *export;
  5130. WASMSection *section = sections;
  5131. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5132. *buf_func = NULL, *buf_func_end = NULL;
  5133. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5134. WASMGlobal *aux_stack_top_global = NULL, *global;
  5135. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5136. aux_stack_top = (uint64)-1LL;
  5137. uint32 global_index, func_index, i;
  5138. uint32 aux_data_end_global_index = (uint32)-1;
  5139. uint32 aux_heap_base_global_index = (uint32)-1;
  5140. WASMFuncType *func_type;
  5141. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5142. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5143. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5144. #if WASM_ENABLE_BULK_MEMORY != 0
  5145. bool has_datacount_section = false;
  5146. #endif
  5147. /* Find code and function sections if have */
  5148. while (section) {
  5149. if (section->section_type == SECTION_TYPE_CODE) {
  5150. buf_code = section->section_body;
  5151. buf_code_end = buf_code + section->section_body_size;
  5152. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5153. module->buf_code = (uint8 *)buf_code;
  5154. module->buf_code_size = section->section_body_size;
  5155. #endif
  5156. }
  5157. else if (section->section_type == SECTION_TYPE_FUNC) {
  5158. buf_func = section->section_body;
  5159. buf_func_end = buf_func + section->section_body_size;
  5160. }
  5161. section = section->next;
  5162. }
  5163. section = sections;
  5164. while (section) {
  5165. buf = section->section_body;
  5166. buf_end = buf + section->section_body_size;
  5167. switch (section->section_type) {
  5168. case SECTION_TYPE_USER:
  5169. /* unsupported user section, ignore it. */
  5170. if (!load_user_section(buf, buf_end, module,
  5171. reuse_const_strings, error_buf,
  5172. error_buf_size))
  5173. return false;
  5174. break;
  5175. case SECTION_TYPE_TYPE:
  5176. if (!load_type_section(buf, buf_end, module, error_buf,
  5177. error_buf_size))
  5178. return false;
  5179. break;
  5180. case SECTION_TYPE_IMPORT:
  5181. if (!load_import_section(buf, buf_end, module,
  5182. reuse_const_strings, error_buf,
  5183. error_buf_size))
  5184. return false;
  5185. break;
  5186. case SECTION_TYPE_FUNC:
  5187. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5188. module, error_buf, error_buf_size))
  5189. return false;
  5190. break;
  5191. case SECTION_TYPE_TABLE:
  5192. if (!load_table_section(buf, buf_end, module, error_buf,
  5193. error_buf_size))
  5194. return false;
  5195. break;
  5196. case SECTION_TYPE_MEMORY:
  5197. if (!load_memory_section(buf, buf_end, module, error_buf,
  5198. error_buf_size))
  5199. return false;
  5200. break;
  5201. #if WASM_ENABLE_TAGS != 0
  5202. case SECTION_TYPE_TAG:
  5203. /* load tag declaration section */
  5204. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5205. module, error_buf, error_buf_size))
  5206. return false;
  5207. break;
  5208. #endif
  5209. case SECTION_TYPE_GLOBAL:
  5210. if (!load_global_section(buf, buf_end, module, error_buf,
  5211. error_buf_size))
  5212. return false;
  5213. break;
  5214. case SECTION_TYPE_EXPORT:
  5215. if (!load_export_section(buf, buf_end, module,
  5216. reuse_const_strings, error_buf,
  5217. error_buf_size))
  5218. return false;
  5219. break;
  5220. case SECTION_TYPE_START:
  5221. if (!load_start_section(buf, buf_end, module, error_buf,
  5222. error_buf_size))
  5223. return false;
  5224. break;
  5225. case SECTION_TYPE_ELEM:
  5226. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5227. error_buf_size))
  5228. return false;
  5229. break;
  5230. case SECTION_TYPE_CODE:
  5231. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5232. module, error_buf, error_buf_size))
  5233. return false;
  5234. break;
  5235. case SECTION_TYPE_DATA:
  5236. if (!load_data_segment_section(buf, buf_end, module,
  5237. #if WASM_ENABLE_BULK_MEMORY != 0
  5238. has_datacount_section,
  5239. #endif
  5240. clone_data_seg, error_buf,
  5241. error_buf_size))
  5242. return false;
  5243. break;
  5244. #if WASM_ENABLE_BULK_MEMORY != 0
  5245. case SECTION_TYPE_DATACOUNT:
  5246. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5247. error_buf_size))
  5248. return false;
  5249. has_datacount_section = true;
  5250. break;
  5251. #endif
  5252. #if WASM_ENABLE_STRINGREF != 0
  5253. case SECTION_TYPE_STRINGREF:
  5254. if (!load_stringref_section(buf, buf_end, module,
  5255. reuse_const_strings, error_buf,
  5256. error_buf_size))
  5257. return false;
  5258. break;
  5259. #endif
  5260. default:
  5261. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5262. return false;
  5263. }
  5264. section = section->next;
  5265. }
  5266. module->aux_data_end_global_index = (uint32)-1;
  5267. module->aux_heap_base_global_index = (uint32)-1;
  5268. module->aux_stack_top_global_index = (uint32)-1;
  5269. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5270. export = module->exports;
  5271. for (i = 0; i < module->export_count; i++, export ++) {
  5272. if (export->kind == EXPORT_KIND_GLOBAL) {
  5273. if (!strcmp(export->name, "__heap_base")) {
  5274. global_index = export->index - module->import_global_count;
  5275. global = module->globals + global_index;
  5276. if (global->type.val_type == VALUE_TYPE_I32
  5277. && !global->type.is_mutable
  5278. && global->init_expr.init_expr_type
  5279. == INIT_EXPR_TYPE_I32_CONST) {
  5280. aux_heap_base_global = global;
  5281. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5282. aux_heap_base_global_index = export->index;
  5283. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5284. aux_heap_base);
  5285. }
  5286. }
  5287. else if (!strcmp(export->name, "__data_end")) {
  5288. global_index = export->index - module->import_global_count;
  5289. global = module->globals + global_index;
  5290. if (global->type.val_type == VALUE_TYPE_I32
  5291. && !global->type.is_mutable
  5292. && global->init_expr.init_expr_type
  5293. == INIT_EXPR_TYPE_I32_CONST) {
  5294. aux_data_end_global = global;
  5295. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5296. aux_data_end_global_index = export->index;
  5297. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5298. aux_data_end);
  5299. aux_data_end = align_uint64(aux_data_end, 16);
  5300. }
  5301. }
  5302. /* For module compiled with -pthread option, the global is:
  5303. [0] stack_top <-- 0
  5304. [1] tls_pointer
  5305. [2] tls_size
  5306. [3] data_end <-- 3
  5307. [4] global_base
  5308. [5] heap_base <-- 5
  5309. [6] dso_handle
  5310. For module compiled without -pthread option:
  5311. [0] stack_top <-- 0
  5312. [1] data_end <-- 1
  5313. [2] global_base
  5314. [3] heap_base <-- 3
  5315. [4] dso_handle
  5316. */
  5317. if (aux_data_end_global && aux_heap_base_global
  5318. && aux_data_end <= aux_heap_base) {
  5319. module->aux_data_end_global_index = aux_data_end_global_index;
  5320. module->aux_data_end = aux_data_end;
  5321. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5322. module->aux_heap_base = aux_heap_base;
  5323. /* Resolve aux stack top global */
  5324. for (global_index = 0; global_index < module->global_count;
  5325. global_index++) {
  5326. global = module->globals + global_index;
  5327. if (global->type.is_mutable /* heap_base and data_end is
  5328. not mutable */
  5329. && global->type.val_type == VALUE_TYPE_I32
  5330. && global->init_expr.init_expr_type
  5331. == INIT_EXPR_TYPE_I32_CONST
  5332. && (uint64)(uint32)global->init_expr.u.i32
  5333. <= aux_heap_base) {
  5334. aux_stack_top_global = global;
  5335. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5336. module->aux_stack_top_global_index =
  5337. module->import_global_count + global_index;
  5338. module->aux_stack_bottom = aux_stack_top;
  5339. module->aux_stack_size =
  5340. aux_stack_top > aux_data_end
  5341. ? (uint32)(aux_stack_top - aux_data_end)
  5342. : (uint32)aux_stack_top;
  5343. LOG_VERBOSE(
  5344. "Found aux stack top global, value: %" PRIu64 ", "
  5345. "global index: %d, stack size: %d",
  5346. aux_stack_top, global_index,
  5347. module->aux_stack_size);
  5348. break;
  5349. }
  5350. }
  5351. if (!aux_stack_top_global) {
  5352. /* Auxiliary stack global isn't found, it must be unused
  5353. in the wasm app, as if it is used, the global must be
  5354. defined. Here we set it to __heap_base global and set
  5355. its size to 0. */
  5356. aux_stack_top_global = aux_heap_base_global;
  5357. aux_stack_top = aux_heap_base;
  5358. module->aux_stack_top_global_index =
  5359. module->aux_heap_base_global_index;
  5360. module->aux_stack_bottom = aux_stack_top;
  5361. module->aux_stack_size = 0;
  5362. }
  5363. break;
  5364. }
  5365. }
  5366. }
  5367. module->malloc_function = (uint32)-1;
  5368. module->free_function = (uint32)-1;
  5369. module->retain_function = (uint32)-1;
  5370. /* Resolve malloc/free function exported by wasm module */
  5371. #if WASM_ENABLE_MEMORY64 != 0
  5372. if (has_module_memory64(module))
  5373. malloc_free_io_type = VALUE_TYPE_I64;
  5374. #endif
  5375. export = module->exports;
  5376. for (i = 0; i < module->export_count; i++, export ++) {
  5377. if (export->kind == EXPORT_KIND_FUNC) {
  5378. if (!strcmp(export->name, "malloc")
  5379. && export->index >= module->import_function_count) {
  5380. func_index = export->index - module->import_function_count;
  5381. func_type = module->functions[func_index]->func_type;
  5382. if (func_type->param_count == 1 && func_type->result_count == 1
  5383. && func_type->types[0] == malloc_free_io_type
  5384. && func_type->types[1] == malloc_free_io_type) {
  5385. bh_assert(module->malloc_function == (uint32)-1);
  5386. module->malloc_function = export->index;
  5387. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5388. export->name, export->index);
  5389. }
  5390. }
  5391. else if (!strcmp(export->name, "__new")
  5392. && export->index >= module->import_function_count) {
  5393. /* __new && __pin for AssemblyScript */
  5394. func_index = export->index - module->import_function_count;
  5395. func_type = module->functions[func_index]->func_type;
  5396. if (func_type->param_count == 2 && func_type->result_count == 1
  5397. && func_type->types[0] == malloc_free_io_type
  5398. && func_type->types[1] == VALUE_TYPE_I32
  5399. && func_type->types[2] == malloc_free_io_type) {
  5400. uint32 j;
  5401. WASMExport *export_tmp;
  5402. bh_assert(module->malloc_function == (uint32)-1);
  5403. module->malloc_function = export->index;
  5404. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5405. export->name, export->index);
  5406. /* resolve retain function.
  5407. If not found, reset malloc function index */
  5408. export_tmp = module->exports;
  5409. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5410. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5411. && (!strcmp(export_tmp->name, "__retain")
  5412. || (!strcmp(export_tmp->name, "__pin")))
  5413. && (export_tmp->index
  5414. >= module->import_function_count)) {
  5415. func_index = export_tmp->index
  5416. - module->import_function_count;
  5417. func_type =
  5418. module->functions[func_index]->func_type;
  5419. if (func_type->param_count == 1
  5420. && func_type->result_count == 1
  5421. && func_type->types[0] == malloc_free_io_type
  5422. && func_type->types[1] == malloc_free_io_type) {
  5423. bh_assert(module->retain_function
  5424. == (uint32)-1);
  5425. module->retain_function = export_tmp->index;
  5426. LOG_VERBOSE("Found retain function, name: %s, "
  5427. "index: %u",
  5428. export_tmp->name,
  5429. export_tmp->index);
  5430. break;
  5431. }
  5432. }
  5433. }
  5434. if (j == module->export_count) {
  5435. module->malloc_function = (uint32)-1;
  5436. LOG_VERBOSE("Can't find retain function,"
  5437. "reset malloc function index to -1");
  5438. }
  5439. }
  5440. }
  5441. else if (((!strcmp(export->name, "free"))
  5442. || (!strcmp(export->name, "__release"))
  5443. || (!strcmp(export->name, "__unpin")))
  5444. && export->index >= module->import_function_count) {
  5445. func_index = export->index - module->import_function_count;
  5446. func_type = module->functions[func_index]->func_type;
  5447. if (func_type->param_count == 1 && func_type->result_count == 0
  5448. && func_type->types[0] == malloc_free_io_type) {
  5449. bh_assert(module->free_function == (uint32)-1);
  5450. module->free_function = export->index;
  5451. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5452. export->name, export->index);
  5453. }
  5454. }
  5455. }
  5456. }
  5457. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5458. handle_table = wasm_interp_get_handle_table();
  5459. #endif
  5460. for (i = 0; i < module->function_count; i++) {
  5461. WASMFunction *func = module->functions[i];
  5462. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5463. error_buf_size)) {
  5464. return false;
  5465. }
  5466. if (i == module->function_count - 1
  5467. && func->code + func->code_size != buf_code_end) {
  5468. set_error_buf(error_buf, error_buf_size,
  5469. "code section size mismatch");
  5470. return false;
  5471. }
  5472. }
  5473. if (!module->possible_memory_grow) {
  5474. WASMMemoryImport *memory_import;
  5475. WASMMemory *memory;
  5476. if (aux_data_end_global && aux_heap_base_global
  5477. && aux_stack_top_global) {
  5478. uint64 init_memory_size;
  5479. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5480. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5481. * valid range of uint32 */
  5482. if (shrunk_memory_size <= UINT32_MAX) {
  5483. if (module->import_memory_count) {
  5484. memory_import = &module->import_memories[0].u.memory;
  5485. init_memory_size =
  5486. (uint64)memory_import->mem_type.num_bytes_per_page
  5487. * memory_import->mem_type.init_page_count;
  5488. if (shrunk_memory_size <= init_memory_size) {
  5489. /* Reset memory info to decrease memory usage */
  5490. memory_import->mem_type.num_bytes_per_page =
  5491. (uint32)shrunk_memory_size;
  5492. memory_import->mem_type.init_page_count = 1;
  5493. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5494. shrunk_memory_size);
  5495. }
  5496. }
  5497. if (module->memory_count) {
  5498. memory = &module->memories[0];
  5499. init_memory_size = (uint64)memory->num_bytes_per_page
  5500. * memory->init_page_count;
  5501. if (shrunk_memory_size <= init_memory_size) {
  5502. /* Reset memory info to decrease memory usage */
  5503. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5504. memory->init_page_count = 1;
  5505. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5506. shrunk_memory_size);
  5507. }
  5508. }
  5509. }
  5510. }
  5511. #if WASM_ENABLE_MULTI_MODULE == 0
  5512. if (module->import_memory_count) {
  5513. memory_import = &module->import_memories[0].u.memory;
  5514. /* Only resize the memory to one big page if num_bytes_per_page is
  5515. * in valid range of uint32 */
  5516. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5517. memory_import->mem_type.num_bytes_per_page *=
  5518. memory_import->mem_type.init_page_count;
  5519. if (memory_import->mem_type.init_page_count > 0)
  5520. memory_import->mem_type.init_page_count =
  5521. memory_import->mem_type.max_page_count = 1;
  5522. else
  5523. memory_import->mem_type.init_page_count =
  5524. memory_import->mem_type.max_page_count = 0;
  5525. }
  5526. }
  5527. if (module->memory_count) {
  5528. memory = &module->memories[0];
  5529. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5530. * valid range of uint32 */
  5531. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5532. memory->num_bytes_per_page *= memory->init_page_count;
  5533. if (memory->init_page_count > 0)
  5534. memory->init_page_count = memory->max_page_count = 1;
  5535. else
  5536. memory->init_page_count = memory->max_page_count = 0;
  5537. }
  5538. }
  5539. #endif
  5540. }
  5541. calculate_global_data_offset(module);
  5542. #if WASM_ENABLE_FAST_JIT != 0
  5543. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5544. return false;
  5545. }
  5546. #endif
  5547. #if WASM_ENABLE_JIT != 0
  5548. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5549. return false;
  5550. }
  5551. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5552. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5553. return false;
  5554. }
  5555. #else
  5556. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5557. thread fast jit execution, since applying llvm optimizations in
  5558. aot_compile_wasm may cost a lot of time.
  5559. Create thread with enough native stack to apply llvm optimizations */
  5560. if (os_thread_create(&module->llvm_jit_init_thread,
  5561. init_llvm_jit_functions_stage2_callback,
  5562. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5563. != 0) {
  5564. set_error_buf(error_buf, error_buf_size,
  5565. "create orcjit compile thread failed");
  5566. return false;
  5567. }
  5568. #endif
  5569. #endif
  5570. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5571. /* Create threads to compile the jit functions */
  5572. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5573. return false;
  5574. }
  5575. #endif
  5576. #if WASM_ENABLE_MEMORY_TRACING != 0
  5577. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5578. #endif
  5579. return true;
  5580. }
  5581. static WASMModule *
  5582. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5583. {
  5584. WASMModule *module =
  5585. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5586. bh_list_status ret;
  5587. if (!module) {
  5588. return NULL;
  5589. }
  5590. module->module_type = Wasm_Module_Bytecode;
  5591. /* Set start_function to -1, means no start function */
  5592. module->start_function = (uint32)-1;
  5593. module->name = name;
  5594. module->is_binary_freeable = false;
  5595. #if WASM_ENABLE_FAST_INTERP == 0
  5596. module->br_table_cache_list = &module->br_table_cache_list_head;
  5597. ret = bh_list_init(module->br_table_cache_list);
  5598. bh_assert(ret == BH_LIST_SUCCESS);
  5599. #endif
  5600. #if WASM_ENABLE_MULTI_MODULE != 0
  5601. module->import_module_list = &module->import_module_list_head;
  5602. ret = bh_list_init(module->import_module_list);
  5603. bh_assert(ret == BH_LIST_SUCCESS);
  5604. #endif
  5605. #if WASM_ENABLE_DEBUG_INTERP != 0
  5606. ret = bh_list_init(&module->fast_opcode_list);
  5607. bh_assert(ret == BH_LIST_SUCCESS);
  5608. #endif
  5609. #if WASM_ENABLE_GC != 0
  5610. if (!(module->ref_type_set =
  5611. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5612. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5613. goto fail1;
  5614. }
  5615. if (os_mutex_init(&module->rtt_type_lock)) {
  5616. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5617. goto fail2;
  5618. }
  5619. #endif
  5620. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5621. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5622. && WASM_ENABLE_LAZY_JIT != 0)
  5623. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5624. set_error_buf(error_buf, error_buf_size,
  5625. "init instance list lock failed");
  5626. goto fail3;
  5627. }
  5628. #endif
  5629. (void)ret;
  5630. return module;
  5631. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5632. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5633. && WASM_ENABLE_LAZY_JIT != 0)
  5634. fail3:
  5635. #endif
  5636. #if WASM_ENABLE_GC != 0
  5637. os_mutex_destroy(&module->rtt_type_lock);
  5638. fail2:
  5639. bh_hash_map_destroy(module->ref_type_set);
  5640. fail1:
  5641. #endif
  5642. wasm_runtime_free(module);
  5643. return NULL;
  5644. }
  5645. #if WASM_ENABLE_DEBUG_INTERP != 0
  5646. static bool
  5647. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5648. uint32 error_buf_size)
  5649. {
  5650. WASMFastOPCodeNode *fast_op =
  5651. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5652. if (fast_op) {
  5653. fast_op->offset = pos - module->load_addr;
  5654. fast_op->orig_op = orig_op;
  5655. bh_list_insert(&module->fast_opcode_list, fast_op);
  5656. }
  5657. return fast_op ? true : false;
  5658. }
  5659. #endif
  5660. WASMModule *
  5661. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5662. uint32 error_buf_size)
  5663. {
  5664. WASMModule *module = create_module("", error_buf, error_buf_size);
  5665. if (!module)
  5666. return NULL;
  5667. if (!load_from_sections(module, section_list, false, true, error_buf,
  5668. error_buf_size)) {
  5669. wasm_loader_unload(module);
  5670. return NULL;
  5671. }
  5672. LOG_VERBOSE("Load module from sections success.\n");
  5673. return module;
  5674. }
  5675. static void
  5676. destroy_sections(WASMSection *section_list)
  5677. {
  5678. WASMSection *section = section_list, *next;
  5679. while (section) {
  5680. next = section->next;
  5681. wasm_runtime_free(section);
  5682. section = next;
  5683. }
  5684. }
  5685. /* clang-format off */
  5686. static uint8 section_ids[] = {
  5687. SECTION_TYPE_USER,
  5688. SECTION_TYPE_TYPE,
  5689. SECTION_TYPE_IMPORT,
  5690. SECTION_TYPE_FUNC,
  5691. SECTION_TYPE_TABLE,
  5692. SECTION_TYPE_MEMORY,
  5693. #if WASM_ENABLE_TAGS != 0
  5694. SECTION_TYPE_TAG,
  5695. #endif
  5696. #if WASM_ENABLE_STRINGREF != 0
  5697. /* must immediately precede the global section,
  5698. or where the global section would be */
  5699. SECTION_TYPE_STRINGREF,
  5700. #endif
  5701. SECTION_TYPE_GLOBAL,
  5702. SECTION_TYPE_EXPORT,
  5703. SECTION_TYPE_START,
  5704. SECTION_TYPE_ELEM,
  5705. #if WASM_ENABLE_BULK_MEMORY != 0
  5706. SECTION_TYPE_DATACOUNT,
  5707. #endif
  5708. SECTION_TYPE_CODE,
  5709. SECTION_TYPE_DATA
  5710. };
  5711. /* clang-format on */
  5712. static uint8
  5713. get_section_index(uint8 section_type)
  5714. {
  5715. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5716. for (uint8 i = 0; i < max_id; i++) {
  5717. if (section_type == section_ids[i])
  5718. return i;
  5719. }
  5720. return (uint8)-1;
  5721. }
  5722. static bool
  5723. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5724. char *error_buf, uint32 error_buf_size)
  5725. {
  5726. WASMSection *section_list_end = NULL, *section;
  5727. const uint8 *p = buf, *p_end = buf + size;
  5728. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5729. uint32 section_size;
  5730. bh_assert(!*p_section_list);
  5731. p += 8;
  5732. while (p < p_end) {
  5733. CHECK_BUF(p, p_end, 1);
  5734. section_type = read_uint8(p);
  5735. section_index = get_section_index(section_type);
  5736. if (section_index != (uint8)-1) {
  5737. if (section_type != SECTION_TYPE_USER) {
  5738. /* Custom sections may be inserted at any place,
  5739. while other sections must occur at most once
  5740. and in prescribed order. */
  5741. if (last_section_index != (uint8)-1
  5742. && (section_index <= last_section_index)) {
  5743. set_error_buf(error_buf, error_buf_size,
  5744. "unexpected content after last section or "
  5745. "junk after last section");
  5746. return false;
  5747. }
  5748. last_section_index = section_index;
  5749. }
  5750. read_leb_uint32(p, p_end, section_size);
  5751. CHECK_BUF1(p, p_end, section_size);
  5752. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5753. error_buf_size))) {
  5754. return false;
  5755. }
  5756. section->section_type = section_type;
  5757. section->section_body = (uint8 *)p;
  5758. section->section_body_size = section_size;
  5759. if (!section_list_end)
  5760. *p_section_list = section_list_end = section;
  5761. else {
  5762. section_list_end->next = section;
  5763. section_list_end = section;
  5764. }
  5765. p += section_size;
  5766. }
  5767. else {
  5768. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5769. return false;
  5770. }
  5771. }
  5772. return true;
  5773. fail:
  5774. return false;
  5775. }
  5776. static void
  5777. exchange32(uint8 *p_data)
  5778. {
  5779. uint8 value = *p_data;
  5780. *p_data = *(p_data + 3);
  5781. *(p_data + 3) = value;
  5782. value = *(p_data + 1);
  5783. *(p_data + 1) = *(p_data + 2);
  5784. *(p_data + 2) = value;
  5785. }
  5786. static union {
  5787. int a;
  5788. char b;
  5789. } __ue = { .a = 1 };
  5790. #define is_little_endian() (__ue.b == 1)
  5791. static bool
  5792. load(const uint8 *buf, uint32 size, WASMModule *module,
  5793. bool wasm_binary_freeable, char *error_buf, uint32 error_buf_size)
  5794. {
  5795. const uint8 *buf_end = buf + size;
  5796. const uint8 *p = buf, *p_end = buf_end;
  5797. uint32 magic_number, version;
  5798. WASMSection *section_list = NULL;
  5799. CHECK_BUF1(p, p_end, sizeof(uint32));
  5800. magic_number = read_uint32(p);
  5801. if (!is_little_endian())
  5802. exchange32((uint8 *)&magic_number);
  5803. if (magic_number != WASM_MAGIC_NUMBER) {
  5804. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5805. return false;
  5806. }
  5807. CHECK_BUF1(p, p_end, sizeof(uint32));
  5808. version = read_uint32(p);
  5809. if (!is_little_endian())
  5810. exchange32((uint8 *)&version);
  5811. if (version != WASM_CURRENT_VERSION) {
  5812. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5813. return false;
  5814. }
  5815. module->package_version = version;
  5816. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5817. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  5818. error_buf, error_buf_size)) {
  5819. destroy_sections(section_list);
  5820. return false;
  5821. }
  5822. destroy_sections(section_list);
  5823. return true;
  5824. fail:
  5825. return false;
  5826. }
  5827. #if WASM_ENABLE_LIBC_WASI != 0
  5828. /**
  5829. * refer to
  5830. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5831. */
  5832. static bool
  5833. check_wasi_abi_compatibility(const WASMModule *module,
  5834. #if WASM_ENABLE_MULTI_MODULE != 0
  5835. bool main_module,
  5836. #endif
  5837. char *error_buf, uint32 error_buf_size)
  5838. {
  5839. /**
  5840. * be careful with:
  5841. * wasi compatible modules(command/reactor) which don't import any wasi
  5842. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5843. * reactor can depend on nothing. At the same time, each has its own entry
  5844. * point.
  5845. *
  5846. * observations:
  5847. * - clang always injects `_start` into a command
  5848. * - clang always injects `_initialize` into a reactor
  5849. * - `iwasm -f` allows to run a function in the reactor
  5850. *
  5851. * strong assumptions:
  5852. * - no one will define either `_start` or `_initialize` on purpose
  5853. * - `_start` should always be `void _start(void)`
  5854. * - `_initialize` should always be `void _initialize(void)`
  5855. *
  5856. */
  5857. /* clang-format off */
  5858. /**
  5859. *
  5860. * | | import_wasi_api True | | import_wasi_api False | |
  5861. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5862. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5863. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5864. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5865. */
  5866. /* clang-format on */
  5867. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5868. uint32 import_function_count = module->import_function_count;
  5869. WASMFuncType *func_type;
  5870. /* (func (export "_start") (...) */
  5871. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5872. error_buf, error_buf_size);
  5873. if (start) {
  5874. if (start->index < import_function_count) {
  5875. set_error_buf(
  5876. error_buf, error_buf_size,
  5877. "the builtin _start function can not be an import function");
  5878. return false;
  5879. }
  5880. func_type =
  5881. module->functions[start->index - import_function_count]->func_type;
  5882. if (func_type->param_count || func_type->result_count) {
  5883. set_error_buf(error_buf, error_buf_size,
  5884. "the signature of builtin _start function is wrong");
  5885. return false;
  5886. }
  5887. }
  5888. else {
  5889. /* (func (export "_initialize") (...) */
  5890. initialize =
  5891. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5892. error_buf, error_buf_size);
  5893. if (initialize) {
  5894. if (initialize->index < import_function_count) {
  5895. set_error_buf(error_buf, error_buf_size,
  5896. "the builtin _initialize function can not be an "
  5897. "import function");
  5898. return false;
  5899. }
  5900. func_type =
  5901. module->functions[initialize->index - import_function_count]
  5902. ->func_type;
  5903. if (func_type->param_count || func_type->result_count) {
  5904. set_error_buf(
  5905. error_buf, error_buf_size,
  5906. "the signature of builtin _initialize function is wrong");
  5907. return false;
  5908. }
  5909. }
  5910. }
  5911. /* filter out non-wasi compatible modules */
  5912. if (!module->import_wasi_api && !start && !initialize) {
  5913. return true;
  5914. }
  5915. /* should have one at least */
  5916. if (module->import_wasi_api && !start && !initialize) {
  5917. LOG_WARNING("warning: a module with WASI apis should be either "
  5918. "a command or a reactor");
  5919. }
  5920. /*
  5921. * there is at least one of `_start` and `_initialize` in below cases.
  5922. * according to the assumption, they should be all wasi compatible
  5923. */
  5924. #if WASM_ENABLE_MULTI_MODULE != 0
  5925. /* filter out commands (with `_start`) cases */
  5926. if (start && !main_module) {
  5927. set_error_buf(
  5928. error_buf, error_buf_size,
  5929. "a command (with _start function) can not be a sub-module");
  5930. return false;
  5931. }
  5932. #endif
  5933. /*
  5934. * it is ok a reactor acts as a main module,
  5935. * so skip the check about (with `_initialize`)
  5936. */
  5937. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5938. error_buf, error_buf_size);
  5939. if (!memory
  5940. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5941. /*
  5942. * with wasi-threads, it's still an open question if a memory
  5943. * should be exported.
  5944. *
  5945. * https://github.com/WebAssembly/wasi-threads/issues/22
  5946. * https://github.com/WebAssembly/WASI/issues/502
  5947. *
  5948. * Note: this code assumes the number of memories is at most 1.
  5949. */
  5950. && module->import_memory_count == 0
  5951. #endif
  5952. ) {
  5953. set_error_buf(error_buf, error_buf_size,
  5954. "a module with WASI apis must export memory by default");
  5955. return false;
  5956. }
  5957. return true;
  5958. }
  5959. #endif
  5960. WASMModule *
  5961. wasm_loader_load(uint8 *buf, uint32 size,
  5962. #if WASM_ENABLE_MULTI_MODULE != 0
  5963. bool main_module,
  5964. #endif
  5965. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  5966. {
  5967. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  5968. if (!module) {
  5969. return NULL;
  5970. }
  5971. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5972. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5973. module->load_addr = (uint8 *)buf;
  5974. module->load_size = size;
  5975. #endif
  5976. if (!load(buf, size, module, args->wasm_binary_freeable, error_buf,
  5977. error_buf_size)) {
  5978. goto fail;
  5979. }
  5980. #if WASM_ENABLE_LIBC_WASI != 0
  5981. /* Check the WASI application ABI */
  5982. if (!check_wasi_abi_compatibility(module,
  5983. #if WASM_ENABLE_MULTI_MODULE != 0
  5984. main_module,
  5985. #endif
  5986. error_buf, error_buf_size)) {
  5987. goto fail;
  5988. }
  5989. #endif
  5990. LOG_VERBOSE("Load module success.\n");
  5991. return module;
  5992. fail:
  5993. wasm_loader_unload(module);
  5994. return NULL;
  5995. }
  5996. void
  5997. wasm_loader_unload(WASMModule *module)
  5998. {
  5999. uint32 i;
  6000. if (!module)
  6001. return;
  6002. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6003. && WASM_ENABLE_LAZY_JIT != 0
  6004. module->orcjit_stop_compiling = true;
  6005. if (module->llvm_jit_init_thread)
  6006. os_thread_join(module->llvm_jit_init_thread, NULL);
  6007. #endif
  6008. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6009. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6010. module internal data after they were freed */
  6011. orcjit_stop_compile_threads(module);
  6012. #endif
  6013. #if WASM_ENABLE_JIT != 0
  6014. if (module->func_ptrs)
  6015. wasm_runtime_free(module->func_ptrs);
  6016. if (module->comp_ctx)
  6017. aot_destroy_comp_context(module->comp_ctx);
  6018. if (module->comp_data)
  6019. aot_destroy_comp_data(module->comp_data);
  6020. #endif
  6021. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6022. && WASM_ENABLE_LAZY_JIT != 0
  6023. if (module->tierup_wait_lock_inited) {
  6024. os_mutex_destroy(&module->tierup_wait_lock);
  6025. os_cond_destroy(&module->tierup_wait_cond);
  6026. }
  6027. #endif
  6028. if (module->imports)
  6029. wasm_runtime_free(module->imports);
  6030. if (module->functions) {
  6031. for (i = 0; i < module->function_count; i++) {
  6032. if (module->functions[i]) {
  6033. if (module->functions[i]->local_offsets)
  6034. wasm_runtime_free(module->functions[i]->local_offsets);
  6035. #if WASM_ENABLE_FAST_INTERP != 0
  6036. if (module->functions[i]->code_compiled)
  6037. wasm_runtime_free(module->functions[i]->code_compiled);
  6038. if (module->functions[i]->consts)
  6039. wasm_runtime_free(module->functions[i]->consts);
  6040. #endif
  6041. #if WASM_ENABLE_FAST_JIT != 0
  6042. if (module->functions[i]->fast_jit_jitted_code) {
  6043. jit_code_cache_free(
  6044. module->functions[i]->fast_jit_jitted_code);
  6045. }
  6046. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6047. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6048. jit_code_cache_free(
  6049. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6050. }
  6051. #endif
  6052. #endif
  6053. #if WASM_ENABLE_GC != 0
  6054. if (module->functions[i]->local_ref_type_maps) {
  6055. wasm_runtime_free(
  6056. module->functions[i]->local_ref_type_maps);
  6057. }
  6058. #endif
  6059. wasm_runtime_free(module->functions[i]);
  6060. }
  6061. }
  6062. wasm_runtime_free(module->functions);
  6063. }
  6064. if (module->tables) {
  6065. #if WASM_ENABLE_GC != 0
  6066. for (i = 0; i < module->table_count; i++) {
  6067. destroy_init_expr(module, &module->tables[i].init_expr);
  6068. }
  6069. #endif
  6070. wasm_runtime_free(module->tables);
  6071. }
  6072. if (module->memories)
  6073. wasm_runtime_free(module->memories);
  6074. if (module->globals) {
  6075. #if WASM_ENABLE_GC != 0
  6076. for (i = 0; i < module->global_count; i++) {
  6077. destroy_init_expr(module, &module->globals[i].init_expr);
  6078. }
  6079. #endif
  6080. wasm_runtime_free(module->globals);
  6081. }
  6082. #if WASM_ENABLE_TAGS != 0
  6083. if (module->tags) {
  6084. for (i = 0; i < module->tag_count; i++) {
  6085. if (module->tags[i])
  6086. wasm_runtime_free(module->tags[i]);
  6087. }
  6088. wasm_runtime_free(module->tags);
  6089. }
  6090. #endif
  6091. if (module->exports)
  6092. wasm_runtime_free(module->exports);
  6093. if (module->table_segments) {
  6094. for (i = 0; i < module->table_seg_count; i++) {
  6095. if (module->table_segments[i].init_values) {
  6096. #if WASM_ENABLE_GC != 0
  6097. uint32 j;
  6098. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6099. destroy_init_expr(
  6100. module, &module->table_segments[i].init_values[j]);
  6101. }
  6102. #endif
  6103. wasm_runtime_free(module->table_segments[i].init_values);
  6104. }
  6105. }
  6106. wasm_runtime_free(module->table_segments);
  6107. }
  6108. if (module->data_segments) {
  6109. for (i = 0; i < module->data_seg_count; i++) {
  6110. if (module->data_segments[i]) {
  6111. if (module->data_segments[i]->is_data_cloned)
  6112. wasm_runtime_free(module->data_segments[i]->data);
  6113. wasm_runtime_free(module->data_segments[i]);
  6114. }
  6115. }
  6116. wasm_runtime_free(module->data_segments);
  6117. }
  6118. if (module->types) {
  6119. for (i = 0; i < module->type_count; i++) {
  6120. if (module->types[i])
  6121. destroy_wasm_type(module->types[i]);
  6122. }
  6123. wasm_runtime_free(module->types);
  6124. }
  6125. if (module->const_str_list) {
  6126. StringNode *node = module->const_str_list, *node_next;
  6127. while (node) {
  6128. node_next = node->next;
  6129. wasm_runtime_free(node);
  6130. node = node_next;
  6131. }
  6132. }
  6133. #if WASM_ENABLE_STRINGREF != 0
  6134. if (module->string_literal_ptrs) {
  6135. wasm_runtime_free((void *)module->string_literal_ptrs);
  6136. }
  6137. if (module->string_literal_lengths) {
  6138. wasm_runtime_free(module->string_literal_lengths);
  6139. }
  6140. #endif
  6141. #if WASM_ENABLE_FAST_INTERP == 0
  6142. if (module->br_table_cache_list) {
  6143. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6144. BrTableCache *node_next;
  6145. while (node) {
  6146. node_next = bh_list_elem_next(node);
  6147. wasm_runtime_free(node);
  6148. node = node_next;
  6149. }
  6150. }
  6151. #endif
  6152. #if WASM_ENABLE_MULTI_MODULE != 0
  6153. /* just release the sub module list */
  6154. if (module->import_module_list) {
  6155. WASMRegisteredModule *node =
  6156. bh_list_first_elem(module->import_module_list);
  6157. while (node) {
  6158. WASMRegisteredModule *next = bh_list_elem_next(node);
  6159. bh_list_remove(module->import_module_list, node);
  6160. /*
  6161. * unload(sub_module) will be triggered during runtime_destroy().
  6162. * every module in the global module list will be unloaded one by
  6163. * one. so don't worry.
  6164. */
  6165. wasm_runtime_free(node);
  6166. /*
  6167. * the module file reading buffer will be released
  6168. * in runtime_destroy()
  6169. */
  6170. node = next;
  6171. }
  6172. }
  6173. #endif
  6174. #if WASM_ENABLE_DEBUG_INTERP != 0
  6175. WASMFastOPCodeNode *fast_opcode =
  6176. bh_list_first_elem(&module->fast_opcode_list);
  6177. while (fast_opcode) {
  6178. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6179. wasm_runtime_free(fast_opcode);
  6180. fast_opcode = next;
  6181. }
  6182. #endif
  6183. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6184. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6185. && WASM_ENABLE_LAZY_JIT != 0)
  6186. os_mutex_destroy(&module->instance_list_lock);
  6187. #endif
  6188. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6189. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6190. #endif
  6191. #if WASM_ENABLE_FAST_JIT != 0
  6192. if (module->fast_jit_func_ptrs) {
  6193. wasm_runtime_free(module->fast_jit_func_ptrs);
  6194. }
  6195. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6196. if (module->fast_jit_thread_locks_inited[i]) {
  6197. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6198. }
  6199. }
  6200. #endif
  6201. #if WASM_ENABLE_GC != 0
  6202. os_mutex_destroy(&module->rtt_type_lock);
  6203. bh_hash_map_destroy(module->ref_type_set);
  6204. if (module->rtt_types) {
  6205. for (i = 0; i < module->type_count; i++) {
  6206. if (module->rtt_types[i])
  6207. wasm_runtime_free(module->rtt_types[i]);
  6208. }
  6209. wasm_runtime_free(module->rtt_types);
  6210. }
  6211. #if WASM_ENABLE_STRINGREF != 0
  6212. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6213. if (module->stringref_rtts[i])
  6214. wasm_runtime_free(module->stringref_rtts[i]);
  6215. }
  6216. #endif
  6217. #endif
  6218. wasm_runtime_free(module);
  6219. }
  6220. bool
  6221. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6222. const uint8 *start_addr, const uint8 *code_end_addr,
  6223. uint8 label_type, uint8 **p_else_addr,
  6224. uint8 **p_end_addr)
  6225. {
  6226. const uint8 *p = start_addr, *p_end = code_end_addr;
  6227. uint8 *else_addr = NULL;
  6228. char error_buf[128];
  6229. uint32 block_nested_depth = 1, count, i, j, t;
  6230. uint32 error_buf_size = sizeof(error_buf);
  6231. uint8 opcode, u8;
  6232. BlockAddr block_stack[16] = { { 0 } }, *block;
  6233. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6234. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6235. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6236. if (block[j].start_addr == start_addr) {
  6237. /* Cache hit */
  6238. *p_else_addr = block[j].else_addr;
  6239. *p_end_addr = block[j].end_addr;
  6240. return true;
  6241. }
  6242. }
  6243. /* Cache unhit */
  6244. block_stack[0].start_addr = start_addr;
  6245. while (p < code_end_addr) {
  6246. opcode = *p++;
  6247. #if WASM_ENABLE_DEBUG_INTERP != 0
  6248. op_break_retry:
  6249. #endif
  6250. switch (opcode) {
  6251. case WASM_OP_UNREACHABLE:
  6252. case WASM_OP_NOP:
  6253. break;
  6254. #if WASM_ENABLE_EXCE_HANDLING != 0
  6255. case WASM_OP_TRY:
  6256. u8 = read_uint8(p);
  6257. if (block_nested_depth
  6258. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6259. block_stack[block_nested_depth].start_addr = p;
  6260. block_stack[block_nested_depth].else_addr = NULL;
  6261. }
  6262. block_nested_depth++;
  6263. break;
  6264. case EXT_OP_TRY:
  6265. skip_leb_uint32(p, p_end);
  6266. if (block_nested_depth
  6267. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6268. block_stack[block_nested_depth].start_addr = p;
  6269. block_stack[block_nested_depth].else_addr = NULL;
  6270. }
  6271. block_nested_depth++;
  6272. break;
  6273. case WASM_OP_CATCH:
  6274. if (block_nested_depth == 1) {
  6275. *p_end_addr = (uint8 *)(p - 1);
  6276. /* stop search and return the address of the catch block */
  6277. return true;
  6278. }
  6279. break;
  6280. case WASM_OP_CATCH_ALL:
  6281. if (block_nested_depth == 1) {
  6282. *p_end_addr = (uint8 *)(p - 1);
  6283. /* stop search and return the address of the catch_all block
  6284. */
  6285. return true;
  6286. }
  6287. break;
  6288. case WASM_OP_THROW:
  6289. /* skip tag_index */
  6290. skip_leb(p);
  6291. break;
  6292. case WASM_OP_RETHROW:
  6293. /* skip depth */
  6294. skip_leb(p);
  6295. break;
  6296. case WASM_OP_DELEGATE:
  6297. if (block_nested_depth == 1) {
  6298. *p_end_addr = (uint8 *)(p - 1);
  6299. return true;
  6300. }
  6301. else {
  6302. skip_leb(p);
  6303. /* the DELEGATE opcode ends the tryblock, */
  6304. block_nested_depth--;
  6305. if (block_nested_depth
  6306. < sizeof(block_stack) / sizeof(BlockAddr))
  6307. block_stack[block_nested_depth].end_addr =
  6308. (uint8 *)(p - 1);
  6309. }
  6310. break;
  6311. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6312. case WASM_OP_BLOCK:
  6313. case WASM_OP_LOOP:
  6314. case WASM_OP_IF:
  6315. {
  6316. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6317. u8 = read_uint8(p);
  6318. if (is_byte_a_type(u8)) {
  6319. #if WASM_ENABLE_GC != 0
  6320. if (wasm_is_type_multi_byte_type(u8)) {
  6321. /* the possible extra bytes of GC ref type have been
  6322. modified to OP_NOP, no need to resolve them again */
  6323. }
  6324. #endif
  6325. }
  6326. else {
  6327. p--;
  6328. /* block type */
  6329. skip_leb_int32(p, p_end);
  6330. }
  6331. if (block_nested_depth
  6332. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6333. block_stack[block_nested_depth].start_addr = p;
  6334. block_stack[block_nested_depth].else_addr = NULL;
  6335. }
  6336. block_nested_depth++;
  6337. break;
  6338. }
  6339. case EXT_OP_BLOCK:
  6340. case EXT_OP_LOOP:
  6341. case EXT_OP_IF:
  6342. /* block type */
  6343. skip_leb_int32(p, p_end);
  6344. if (block_nested_depth
  6345. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6346. block_stack[block_nested_depth].start_addr = p;
  6347. block_stack[block_nested_depth].else_addr = NULL;
  6348. }
  6349. block_nested_depth++;
  6350. break;
  6351. case WASM_OP_ELSE:
  6352. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6353. else_addr = (uint8 *)(p - 1);
  6354. if (block_nested_depth - 1
  6355. < sizeof(block_stack) / sizeof(BlockAddr))
  6356. block_stack[block_nested_depth - 1].else_addr =
  6357. (uint8 *)(p - 1);
  6358. break;
  6359. case WASM_OP_END:
  6360. if (block_nested_depth == 1) {
  6361. if (label_type == LABEL_TYPE_IF)
  6362. *p_else_addr = else_addr;
  6363. *p_end_addr = (uint8 *)(p - 1);
  6364. block_stack[0].end_addr = (uint8 *)(p - 1);
  6365. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6366. t++) {
  6367. start_addr = block_stack[t].start_addr;
  6368. if (start_addr) {
  6369. i = ((uintptr_t)start_addr)
  6370. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6371. block =
  6372. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6373. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6374. if (!block[j].start_addr)
  6375. break;
  6376. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6377. memmove(block + 1, block,
  6378. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6379. * sizeof(BlockAddr));
  6380. j = 0;
  6381. }
  6382. block[j].start_addr = block_stack[t].start_addr;
  6383. block[j].else_addr = block_stack[t].else_addr;
  6384. block[j].end_addr = block_stack[t].end_addr;
  6385. }
  6386. else
  6387. break;
  6388. }
  6389. return true;
  6390. }
  6391. else {
  6392. block_nested_depth--;
  6393. if (block_nested_depth
  6394. < sizeof(block_stack) / sizeof(BlockAddr))
  6395. block_stack[block_nested_depth].end_addr =
  6396. (uint8 *)(p - 1);
  6397. }
  6398. break;
  6399. case WASM_OP_BR:
  6400. case WASM_OP_BR_IF:
  6401. skip_leb_uint32(p, p_end); /* labelidx */
  6402. break;
  6403. case WASM_OP_BR_TABLE:
  6404. read_leb_uint32(p, p_end, count); /* label num */
  6405. #if WASM_ENABLE_FAST_INTERP != 0
  6406. for (i = 0; i <= count; i++) /* labelidxs */
  6407. skip_leb_uint32(p, p_end);
  6408. #else
  6409. p += count + 1;
  6410. while (*p == WASM_OP_NOP)
  6411. p++;
  6412. #endif
  6413. break;
  6414. #if WASM_ENABLE_FAST_INTERP == 0
  6415. case EXT_OP_BR_TABLE_CACHE:
  6416. read_leb_uint32(p, p_end, count); /* label num */
  6417. while (*p == WASM_OP_NOP)
  6418. p++;
  6419. break;
  6420. #endif
  6421. case WASM_OP_RETURN:
  6422. break;
  6423. case WASM_OP_CALL:
  6424. #if WASM_ENABLE_TAIL_CALL != 0
  6425. case WASM_OP_RETURN_CALL:
  6426. #endif
  6427. skip_leb_uint32(p, p_end); /* funcidx */
  6428. break;
  6429. case WASM_OP_CALL_INDIRECT:
  6430. #if WASM_ENABLE_TAIL_CALL != 0
  6431. case WASM_OP_RETURN_CALL_INDIRECT:
  6432. #endif
  6433. skip_leb_uint32(p, p_end); /* typeidx */
  6434. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6435. skip_leb_uint32(p, p_end); /* tableidx */
  6436. #else
  6437. u8 = read_uint8(p); /* 0x00 */
  6438. #endif
  6439. break;
  6440. #if WASM_ENABLE_GC != 0
  6441. case WASM_OP_CALL_REF:
  6442. case WASM_OP_RETURN_CALL_REF:
  6443. skip_leb_uint32(p, p_end); /* typeidx */
  6444. break;
  6445. #endif
  6446. case WASM_OP_DROP:
  6447. case WASM_OP_SELECT:
  6448. case WASM_OP_DROP_64:
  6449. case WASM_OP_SELECT_64:
  6450. break;
  6451. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6452. case WASM_OP_SELECT_T:
  6453. {
  6454. skip_leb_uint32(p, p_end); /* vec length */
  6455. u8 = read_uint8(p); /* typeidx */
  6456. /* the possible extra bytes of GC ref type have been
  6457. modified to OP_NOP, no need to resolve them again */
  6458. break;
  6459. }
  6460. case WASM_OP_TABLE_GET:
  6461. case WASM_OP_TABLE_SET:
  6462. skip_leb_uint32(p, p_end); /* table index */
  6463. break;
  6464. case WASM_OP_REF_NULL:
  6465. {
  6466. u8 = read_uint8(p); /* type */
  6467. if (is_byte_a_type(u8)) {
  6468. #if WASM_ENABLE_GC != 0
  6469. if (wasm_is_type_multi_byte_type(u8)) {
  6470. /* the possible extra bytes of GC ref type have been
  6471. modified to OP_NOP, no need to resolve them again */
  6472. }
  6473. #endif
  6474. }
  6475. else {
  6476. p--;
  6477. skip_leb_uint32(p, p_end);
  6478. }
  6479. break;
  6480. }
  6481. case WASM_OP_REF_IS_NULL:
  6482. break;
  6483. case WASM_OP_REF_FUNC:
  6484. skip_leb_uint32(p, p_end); /* func index */
  6485. break;
  6486. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6487. #if WASM_ENABLE_GC != 0
  6488. case WASM_OP_REF_AS_NON_NULL:
  6489. case WASM_OP_REF_EQ:
  6490. break;
  6491. case WASM_OP_BR_ON_NULL:
  6492. case WASM_OP_BR_ON_NON_NULL:
  6493. skip_leb_uint32(p, p_end); /* label index */
  6494. break;
  6495. #endif /* end of WASM_ENABLE_GC != 0 */
  6496. case WASM_OP_GET_LOCAL:
  6497. case WASM_OP_SET_LOCAL:
  6498. case WASM_OP_TEE_LOCAL:
  6499. case WASM_OP_GET_GLOBAL:
  6500. case WASM_OP_SET_GLOBAL:
  6501. case WASM_OP_GET_GLOBAL_64:
  6502. case WASM_OP_SET_GLOBAL_64:
  6503. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6504. skip_leb_uint32(p, p_end); /* local index */
  6505. break;
  6506. case EXT_OP_GET_LOCAL_FAST:
  6507. case EXT_OP_SET_LOCAL_FAST:
  6508. case EXT_OP_TEE_LOCAL_FAST:
  6509. CHECK_BUF(p, p_end, 1);
  6510. p++;
  6511. break;
  6512. case WASM_OP_I32_LOAD:
  6513. case WASM_OP_I64_LOAD:
  6514. case WASM_OP_F32_LOAD:
  6515. case WASM_OP_F64_LOAD:
  6516. case WASM_OP_I32_LOAD8_S:
  6517. case WASM_OP_I32_LOAD8_U:
  6518. case WASM_OP_I32_LOAD16_S:
  6519. case WASM_OP_I32_LOAD16_U:
  6520. case WASM_OP_I64_LOAD8_S:
  6521. case WASM_OP_I64_LOAD8_U:
  6522. case WASM_OP_I64_LOAD16_S:
  6523. case WASM_OP_I64_LOAD16_U:
  6524. case WASM_OP_I64_LOAD32_S:
  6525. case WASM_OP_I64_LOAD32_U:
  6526. case WASM_OP_I32_STORE:
  6527. case WASM_OP_I64_STORE:
  6528. case WASM_OP_F32_STORE:
  6529. case WASM_OP_F64_STORE:
  6530. case WASM_OP_I32_STORE8:
  6531. case WASM_OP_I32_STORE16:
  6532. case WASM_OP_I64_STORE8:
  6533. case WASM_OP_I64_STORE16:
  6534. case WASM_OP_I64_STORE32:
  6535. skip_leb_align(p, p_end); /* align */
  6536. skip_leb_mem_offset(p, p_end); /* offset */
  6537. break;
  6538. case WASM_OP_MEMORY_SIZE:
  6539. case WASM_OP_MEMORY_GROW:
  6540. skip_leb_memidx(p, p_end); /* memidx */
  6541. break;
  6542. case WASM_OP_I32_CONST:
  6543. skip_leb_int32(p, p_end);
  6544. break;
  6545. case WASM_OP_I64_CONST:
  6546. skip_leb_int64(p, p_end);
  6547. break;
  6548. case WASM_OP_F32_CONST:
  6549. p += sizeof(float32);
  6550. break;
  6551. case WASM_OP_F64_CONST:
  6552. p += sizeof(float64);
  6553. break;
  6554. case WASM_OP_I32_EQZ:
  6555. case WASM_OP_I32_EQ:
  6556. case WASM_OP_I32_NE:
  6557. case WASM_OP_I32_LT_S:
  6558. case WASM_OP_I32_LT_U:
  6559. case WASM_OP_I32_GT_S:
  6560. case WASM_OP_I32_GT_U:
  6561. case WASM_OP_I32_LE_S:
  6562. case WASM_OP_I32_LE_U:
  6563. case WASM_OP_I32_GE_S:
  6564. case WASM_OP_I32_GE_U:
  6565. case WASM_OP_I64_EQZ:
  6566. case WASM_OP_I64_EQ:
  6567. case WASM_OP_I64_NE:
  6568. case WASM_OP_I64_LT_S:
  6569. case WASM_OP_I64_LT_U:
  6570. case WASM_OP_I64_GT_S:
  6571. case WASM_OP_I64_GT_U:
  6572. case WASM_OP_I64_LE_S:
  6573. case WASM_OP_I64_LE_U:
  6574. case WASM_OP_I64_GE_S:
  6575. case WASM_OP_I64_GE_U:
  6576. case WASM_OP_F32_EQ:
  6577. case WASM_OP_F32_NE:
  6578. case WASM_OP_F32_LT:
  6579. case WASM_OP_F32_GT:
  6580. case WASM_OP_F32_LE:
  6581. case WASM_OP_F32_GE:
  6582. case WASM_OP_F64_EQ:
  6583. case WASM_OP_F64_NE:
  6584. case WASM_OP_F64_LT:
  6585. case WASM_OP_F64_GT:
  6586. case WASM_OP_F64_LE:
  6587. case WASM_OP_F64_GE:
  6588. case WASM_OP_I32_CLZ:
  6589. case WASM_OP_I32_CTZ:
  6590. case WASM_OP_I32_POPCNT:
  6591. case WASM_OP_I32_ADD:
  6592. case WASM_OP_I32_SUB:
  6593. case WASM_OP_I32_MUL:
  6594. case WASM_OP_I32_DIV_S:
  6595. case WASM_OP_I32_DIV_U:
  6596. case WASM_OP_I32_REM_S:
  6597. case WASM_OP_I32_REM_U:
  6598. case WASM_OP_I32_AND:
  6599. case WASM_OP_I32_OR:
  6600. case WASM_OP_I32_XOR:
  6601. case WASM_OP_I32_SHL:
  6602. case WASM_OP_I32_SHR_S:
  6603. case WASM_OP_I32_SHR_U:
  6604. case WASM_OP_I32_ROTL:
  6605. case WASM_OP_I32_ROTR:
  6606. case WASM_OP_I64_CLZ:
  6607. case WASM_OP_I64_CTZ:
  6608. case WASM_OP_I64_POPCNT:
  6609. case WASM_OP_I64_ADD:
  6610. case WASM_OP_I64_SUB:
  6611. case WASM_OP_I64_MUL:
  6612. case WASM_OP_I64_DIV_S:
  6613. case WASM_OP_I64_DIV_U:
  6614. case WASM_OP_I64_REM_S:
  6615. case WASM_OP_I64_REM_U:
  6616. case WASM_OP_I64_AND:
  6617. case WASM_OP_I64_OR:
  6618. case WASM_OP_I64_XOR:
  6619. case WASM_OP_I64_SHL:
  6620. case WASM_OP_I64_SHR_S:
  6621. case WASM_OP_I64_SHR_U:
  6622. case WASM_OP_I64_ROTL:
  6623. case WASM_OP_I64_ROTR:
  6624. case WASM_OP_F32_ABS:
  6625. case WASM_OP_F32_NEG:
  6626. case WASM_OP_F32_CEIL:
  6627. case WASM_OP_F32_FLOOR:
  6628. case WASM_OP_F32_TRUNC:
  6629. case WASM_OP_F32_NEAREST:
  6630. case WASM_OP_F32_SQRT:
  6631. case WASM_OP_F32_ADD:
  6632. case WASM_OP_F32_SUB:
  6633. case WASM_OP_F32_MUL:
  6634. case WASM_OP_F32_DIV:
  6635. case WASM_OP_F32_MIN:
  6636. case WASM_OP_F32_MAX:
  6637. case WASM_OP_F32_COPYSIGN:
  6638. case WASM_OP_F64_ABS:
  6639. case WASM_OP_F64_NEG:
  6640. case WASM_OP_F64_CEIL:
  6641. case WASM_OP_F64_FLOOR:
  6642. case WASM_OP_F64_TRUNC:
  6643. case WASM_OP_F64_NEAREST:
  6644. case WASM_OP_F64_SQRT:
  6645. case WASM_OP_F64_ADD:
  6646. case WASM_OP_F64_SUB:
  6647. case WASM_OP_F64_MUL:
  6648. case WASM_OP_F64_DIV:
  6649. case WASM_OP_F64_MIN:
  6650. case WASM_OP_F64_MAX:
  6651. case WASM_OP_F64_COPYSIGN:
  6652. case WASM_OP_I32_WRAP_I64:
  6653. case WASM_OP_I32_TRUNC_S_F32:
  6654. case WASM_OP_I32_TRUNC_U_F32:
  6655. case WASM_OP_I32_TRUNC_S_F64:
  6656. case WASM_OP_I32_TRUNC_U_F64:
  6657. case WASM_OP_I64_EXTEND_S_I32:
  6658. case WASM_OP_I64_EXTEND_U_I32:
  6659. case WASM_OP_I64_TRUNC_S_F32:
  6660. case WASM_OP_I64_TRUNC_U_F32:
  6661. case WASM_OP_I64_TRUNC_S_F64:
  6662. case WASM_OP_I64_TRUNC_U_F64:
  6663. case WASM_OP_F32_CONVERT_S_I32:
  6664. case WASM_OP_F32_CONVERT_U_I32:
  6665. case WASM_OP_F32_CONVERT_S_I64:
  6666. case WASM_OP_F32_CONVERT_U_I64:
  6667. case WASM_OP_F32_DEMOTE_F64:
  6668. case WASM_OP_F64_CONVERT_S_I32:
  6669. case WASM_OP_F64_CONVERT_U_I32:
  6670. case WASM_OP_F64_CONVERT_S_I64:
  6671. case WASM_OP_F64_CONVERT_U_I64:
  6672. case WASM_OP_F64_PROMOTE_F32:
  6673. case WASM_OP_I32_REINTERPRET_F32:
  6674. case WASM_OP_I64_REINTERPRET_F64:
  6675. case WASM_OP_F32_REINTERPRET_I32:
  6676. case WASM_OP_F64_REINTERPRET_I64:
  6677. case WASM_OP_I32_EXTEND8_S:
  6678. case WASM_OP_I32_EXTEND16_S:
  6679. case WASM_OP_I64_EXTEND8_S:
  6680. case WASM_OP_I64_EXTEND16_S:
  6681. case WASM_OP_I64_EXTEND32_S:
  6682. break;
  6683. #if WASM_ENABLE_GC != 0
  6684. case WASM_OP_GC_PREFIX:
  6685. {
  6686. uint32 opcode1;
  6687. read_leb_uint32(p, p_end, opcode1);
  6688. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6689. is no larger than UINT8_MAX */
  6690. opcode = (uint8)opcode1;
  6691. switch (opcode) {
  6692. case WASM_OP_STRUCT_NEW:
  6693. case WASM_OP_STRUCT_NEW_DEFAULT:
  6694. skip_leb_uint32(p, p_end); /* typeidx */
  6695. break;
  6696. case WASM_OP_STRUCT_GET:
  6697. case WASM_OP_STRUCT_GET_S:
  6698. case WASM_OP_STRUCT_GET_U:
  6699. case WASM_OP_STRUCT_SET:
  6700. skip_leb_uint32(p, p_end); /* typeidx */
  6701. skip_leb_uint32(p, p_end); /* fieldidx */
  6702. break;
  6703. case WASM_OP_ARRAY_NEW:
  6704. case WASM_OP_ARRAY_NEW_DEFAULT:
  6705. case WASM_OP_ARRAY_GET:
  6706. case WASM_OP_ARRAY_GET_S:
  6707. case WASM_OP_ARRAY_GET_U:
  6708. case WASM_OP_ARRAY_SET:
  6709. case WASM_OP_ARRAY_FILL:
  6710. skip_leb_uint32(p, p_end); /* typeidx */
  6711. break;
  6712. case WASM_OP_ARRAY_COPY:
  6713. skip_leb_uint32(p, p_end); /* typeidx1 */
  6714. skip_leb_uint32(p, p_end); /* typeidx2 */
  6715. break;
  6716. case WASM_OP_ARRAY_LEN:
  6717. break;
  6718. case WASM_OP_ARRAY_NEW_FIXED:
  6719. case WASM_OP_ARRAY_NEW_DATA:
  6720. case WASM_OP_ARRAY_NEW_ELEM:
  6721. skip_leb_uint32(p, p_end); /* typeidx */
  6722. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6723. break;
  6724. case WASM_OP_REF_I31:
  6725. case WASM_OP_I31_GET_S:
  6726. case WASM_OP_I31_GET_U:
  6727. break;
  6728. case WASM_OP_REF_TEST:
  6729. case WASM_OP_REF_CAST:
  6730. case WASM_OP_REF_TEST_NULLABLE:
  6731. case WASM_OP_REF_CAST_NULLABLE:
  6732. skip_leb_int32(p, p_end); /* heaptype */
  6733. break;
  6734. case WASM_OP_BR_ON_CAST:
  6735. case WASM_OP_BR_ON_CAST_FAIL:
  6736. p += sizeof(uint8); /* castflag */
  6737. skip_leb_uint32(p, p_end); /* labelidx */
  6738. skip_leb_int32(p, p_end); /* heaptype */
  6739. skip_leb_int32(p, p_end); /* heaptype2 */
  6740. break;
  6741. case WASM_OP_ANY_CONVERT_EXTERN:
  6742. case WASM_OP_EXTERN_CONVERT_ANY:
  6743. break;
  6744. #if WASM_ENABLE_STRINGREF != 0
  6745. case WASM_OP_STRING_NEW_UTF8:
  6746. case WASM_OP_STRING_NEW_WTF16:
  6747. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6748. case WASM_OP_STRING_NEW_WTF8:
  6749. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6750. break;
  6751. case WASM_OP_STRING_CONST:
  6752. skip_leb_int32(p, p_end); /* contents */
  6753. break;
  6754. case WASM_OP_STRING_MEASURE_UTF8:
  6755. case WASM_OP_STRING_MEASURE_WTF8:
  6756. case WASM_OP_STRING_MEASURE_WTF16:
  6757. break;
  6758. case WASM_OP_STRING_ENCODE_UTF8:
  6759. case WASM_OP_STRING_ENCODE_WTF16:
  6760. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6761. case WASM_OP_STRING_ENCODE_WTF8:
  6762. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6763. break;
  6764. case WASM_OP_STRING_CONCAT:
  6765. case WASM_OP_STRING_EQ:
  6766. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6767. case WASM_OP_STRING_AS_WTF8:
  6768. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6769. break;
  6770. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6771. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6772. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6773. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6774. break;
  6775. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6776. case WASM_OP_STRING_AS_WTF16:
  6777. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6778. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6779. break;
  6780. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6781. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6782. break;
  6783. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6784. case WASM_OP_STRING_AS_ITER:
  6785. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6786. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6787. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6788. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6789. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6790. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6791. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6792. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6793. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6794. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6795. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6796. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6797. break;
  6798. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6799. default:
  6800. return false;
  6801. }
  6802. break;
  6803. }
  6804. #endif /* end of WASM_ENABLE_GC != 0 */
  6805. case WASM_OP_MISC_PREFIX:
  6806. {
  6807. uint32 opcode1;
  6808. read_leb_uint32(p, p_end, opcode1);
  6809. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6810. is no larger than UINT8_MAX */
  6811. opcode = (uint8)opcode1;
  6812. switch (opcode) {
  6813. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6814. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6815. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6816. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6817. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6818. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6819. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6820. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6821. break;
  6822. #if WASM_ENABLE_BULK_MEMORY != 0
  6823. case WASM_OP_MEMORY_INIT:
  6824. skip_leb_uint32(p, p_end);
  6825. skip_leb_memidx(p, p_end);
  6826. break;
  6827. case WASM_OP_DATA_DROP:
  6828. skip_leb_uint32(p, p_end);
  6829. break;
  6830. case WASM_OP_MEMORY_COPY:
  6831. skip_leb_memidx(p, p_end);
  6832. skip_leb_memidx(p, p_end);
  6833. break;
  6834. case WASM_OP_MEMORY_FILL:
  6835. skip_leb_memidx(p, p_end);
  6836. break;
  6837. #endif /* WASM_ENABLE_BULK_MEMORY */
  6838. #if WASM_ENABLE_REF_TYPES != 0
  6839. case WASM_OP_TABLE_INIT:
  6840. case WASM_OP_TABLE_COPY:
  6841. /* tableidx */
  6842. skip_leb_uint32(p, p_end);
  6843. /* elemidx */
  6844. skip_leb_uint32(p, p_end);
  6845. break;
  6846. case WASM_OP_ELEM_DROP:
  6847. /* elemidx */
  6848. skip_leb_uint32(p, p_end);
  6849. break;
  6850. case WASM_OP_TABLE_SIZE:
  6851. case WASM_OP_TABLE_GROW:
  6852. case WASM_OP_TABLE_FILL:
  6853. skip_leb_uint32(p, p_end); /* table idx */
  6854. break;
  6855. #endif /* WASM_ENABLE_REF_TYPES */
  6856. default:
  6857. return false;
  6858. }
  6859. break;
  6860. }
  6861. #if WASM_ENABLE_SIMD != 0
  6862. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6863. case WASM_OP_SIMD_PREFIX:
  6864. {
  6865. uint32 opcode1;
  6866. read_leb_uint32(p, p_end, opcode1);
  6867. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6868. is no larger than UINT8_MAX */
  6869. opcode = (uint8)opcode1;
  6870. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6871. */
  6872. switch (opcode) {
  6873. case SIMD_v128_load:
  6874. case SIMD_v128_load8x8_s:
  6875. case SIMD_v128_load8x8_u:
  6876. case SIMD_v128_load16x4_s:
  6877. case SIMD_v128_load16x4_u:
  6878. case SIMD_v128_load32x2_s:
  6879. case SIMD_v128_load32x2_u:
  6880. case SIMD_v128_load8_splat:
  6881. case SIMD_v128_load16_splat:
  6882. case SIMD_v128_load32_splat:
  6883. case SIMD_v128_load64_splat:
  6884. case SIMD_v128_store:
  6885. /* memarg align */
  6886. skip_leb_uint32(p, p_end);
  6887. /* memarg offset */
  6888. skip_leb_mem_offset(p, p_end);
  6889. break;
  6890. case SIMD_v128_const:
  6891. case SIMD_v8x16_shuffle:
  6892. /* immByte[16] immLaneId[16] */
  6893. CHECK_BUF1(p, p_end, 16);
  6894. p += 16;
  6895. break;
  6896. case SIMD_i8x16_extract_lane_s:
  6897. case SIMD_i8x16_extract_lane_u:
  6898. case SIMD_i8x16_replace_lane:
  6899. case SIMD_i16x8_extract_lane_s:
  6900. case SIMD_i16x8_extract_lane_u:
  6901. case SIMD_i16x8_replace_lane:
  6902. case SIMD_i32x4_extract_lane:
  6903. case SIMD_i32x4_replace_lane:
  6904. case SIMD_i64x2_extract_lane:
  6905. case SIMD_i64x2_replace_lane:
  6906. case SIMD_f32x4_extract_lane:
  6907. case SIMD_f32x4_replace_lane:
  6908. case SIMD_f64x2_extract_lane:
  6909. case SIMD_f64x2_replace_lane:
  6910. /* ImmLaneId */
  6911. CHECK_BUF(p, p_end, 1);
  6912. p++;
  6913. break;
  6914. case SIMD_v128_load8_lane:
  6915. case SIMD_v128_load16_lane:
  6916. case SIMD_v128_load32_lane:
  6917. case SIMD_v128_load64_lane:
  6918. case SIMD_v128_store8_lane:
  6919. case SIMD_v128_store16_lane:
  6920. case SIMD_v128_store32_lane:
  6921. case SIMD_v128_store64_lane:
  6922. /* memarg align */
  6923. skip_leb_uint32(p, p_end);
  6924. /* memarg offset */
  6925. skip_leb_mem_offset(p, p_end);
  6926. /* ImmLaneId */
  6927. CHECK_BUF(p, p_end, 1);
  6928. p++;
  6929. break;
  6930. case SIMD_v128_load32_zero:
  6931. case SIMD_v128_load64_zero:
  6932. /* memarg align */
  6933. skip_leb_uint32(p, p_end);
  6934. /* memarg offset */
  6935. skip_leb_mem_offset(p, p_end);
  6936. break;
  6937. default:
  6938. /*
  6939. * since latest SIMD specific used almost every value
  6940. * from 0x00 to 0xff, the default branch will present
  6941. * all opcodes without imm
  6942. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6943. */
  6944. break;
  6945. }
  6946. break;
  6947. }
  6948. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6949. #endif /* end of WASM_ENABLE_SIMD */
  6950. #if WASM_ENABLE_SHARED_MEMORY != 0
  6951. case WASM_OP_ATOMIC_PREFIX:
  6952. {
  6953. uint32 opcode1;
  6954. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6955. read_leb_uint32(p, p_end, opcode1);
  6956. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6957. is no larger than UINT8_MAX */
  6958. opcode = (uint8)opcode1;
  6959. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6960. skip_leb_uint32(p, p_end); /* align */
  6961. skip_leb_mem_offset(p, p_end); /* offset */
  6962. }
  6963. else {
  6964. /* atomic.fence doesn't have memarg */
  6965. p++;
  6966. }
  6967. break;
  6968. }
  6969. #endif
  6970. #if WASM_ENABLE_DEBUG_INTERP != 0
  6971. case DEBUG_OP_BREAK:
  6972. {
  6973. WASMDebugInstance *debug_instance =
  6974. wasm_exec_env_get_instance(exec_env);
  6975. char original_opcode[1];
  6976. uint64 size = 1;
  6977. WASMModuleInstance *module_inst =
  6978. (WASMModuleInstance *)exec_env->module_inst;
  6979. uint64 offset = (p - 1) >= module_inst->module->load_addr
  6980. ? (p - 1) - module_inst->module->load_addr
  6981. : ~0;
  6982. if (debug_instance) {
  6983. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  6984. original_opcode, &size)
  6985. && size == 1) {
  6986. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  6987. "with %02x: ",
  6988. original_opcode[0]);
  6989. opcode = original_opcode[0];
  6990. goto op_break_retry;
  6991. }
  6992. }
  6993. break;
  6994. }
  6995. #endif
  6996. default:
  6997. return false;
  6998. }
  6999. }
  7000. (void)u8;
  7001. (void)exec_env;
  7002. return false;
  7003. fail:
  7004. return false;
  7005. }
  7006. #if WASM_ENABLE_FAST_INTERP != 0
  7007. #if WASM_DEBUG_PREPROCESSOR != 0
  7008. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7009. #else
  7010. #define LOG_OP(...) (void)0
  7011. #endif
  7012. #define PATCH_ELSE 0
  7013. #define PATCH_END 1
  7014. typedef struct BranchBlockPatch {
  7015. struct BranchBlockPatch *next;
  7016. uint8 patch_type;
  7017. uint8 *code_compiled;
  7018. } BranchBlockPatch;
  7019. #endif
  7020. typedef struct BranchBlock {
  7021. uint8 label_type;
  7022. BlockType block_type;
  7023. uint8 *start_addr;
  7024. uint8 *else_addr;
  7025. uint8 *end_addr;
  7026. uint32 stack_cell_num;
  7027. #if WASM_ENABLE_GC != 0
  7028. uint32 reftype_map_num;
  7029. /* Indicate which local is used inside current block, used to validate
  7030. * local.get with non-nullable ref types */
  7031. uint8 *local_use_mask;
  7032. uint32 local_use_mask_size;
  7033. #endif
  7034. #if WASM_ENABLE_FAST_INTERP != 0
  7035. uint16 dynamic_offset;
  7036. uint8 *code_compiled;
  7037. BranchBlockPatch *patch_list;
  7038. /* This is used to save params frame_offset of of if block */
  7039. int16 *param_frame_offsets;
  7040. /* This is used to recover the dynamic offset for else branch,
  7041. * and also to remember the start offset of dynamic space which
  7042. * stores the block arguments for loop block, so we can use it
  7043. * to copy the stack operands to the loop block's arguments in
  7044. * wasm_loader_emit_br_info for opcode br. */
  7045. uint16 start_dynamic_offset;
  7046. #endif
  7047. /* Indicate the operand stack is in polymorphic state.
  7048. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7049. * to polymorphic state until the block's 'end' opcode is processed.
  7050. * If stack is in polymorphic state and stack is empty, instruction can
  7051. * pop any type of value directly without decreasing stack top pointer
  7052. * and stack cell num. */
  7053. bool is_stack_polymorphic;
  7054. } BranchBlock;
  7055. typedef struct WASMLoaderContext {
  7056. /* frame ref stack */
  7057. uint8 *frame_ref;
  7058. uint8 *frame_ref_bottom;
  7059. uint8 *frame_ref_boundary;
  7060. uint32 frame_ref_size;
  7061. uint32 stack_cell_num;
  7062. uint32 max_stack_cell_num;
  7063. #if WASM_ENABLE_GC != 0
  7064. /* frame reftype map stack */
  7065. WASMRefTypeMap *frame_reftype_map;
  7066. WASMRefTypeMap *frame_reftype_map_bottom;
  7067. WASMRefTypeMap *frame_reftype_map_boundary;
  7068. uint32 frame_reftype_map_size;
  7069. uint32 reftype_map_num;
  7070. uint32 max_reftype_map_num;
  7071. /* Current module */
  7072. WASMModule *module;
  7073. /* Current module's ref_type_set */
  7074. HashMap *ref_type_set;
  7075. /* Always point to local variable ref_type of
  7076. wasm_loader_prepare_bytecode */
  7077. WASMRefType *ref_type_tmp;
  7078. #endif
  7079. /* frame csp stack */
  7080. BranchBlock *frame_csp;
  7081. BranchBlock *frame_csp_bottom;
  7082. BranchBlock *frame_csp_boundary;
  7083. uint32 frame_csp_size;
  7084. uint32 csp_num;
  7085. uint32 max_csp_num;
  7086. #if WASM_ENABLE_FAST_INTERP != 0
  7087. /* frame offset stack */
  7088. int16 *frame_offset;
  7089. int16 *frame_offset_bottom;
  7090. int16 *frame_offset_boundary;
  7091. uint32 frame_offset_size;
  7092. int16 dynamic_offset;
  7093. int16 start_dynamic_offset;
  7094. int16 max_dynamic_offset;
  7095. /* preserved local offset */
  7096. int16 preserved_local_offset;
  7097. /* const buffer */
  7098. uint8 *const_buf;
  7099. uint16 num_const;
  7100. uint16 const_cell_num;
  7101. uint32 const_buf_size;
  7102. /* processed code */
  7103. uint8 *p_code_compiled;
  7104. uint8 *p_code_compiled_end;
  7105. uint32 code_compiled_size;
  7106. /* If the last opcode will be dropped, the peak memory usage will be larger
  7107. * than the final code_compiled_size, we record the peak size to ensure
  7108. * there will not be invalid memory access during second traverse */
  7109. uint32 code_compiled_peak_size;
  7110. #endif
  7111. } WASMLoaderContext;
  7112. typedef struct Const {
  7113. WASMValue value;
  7114. uint16 slot_index;
  7115. uint8 value_type;
  7116. } Const;
  7117. #define CHECK_CSP_PUSH() \
  7118. do { \
  7119. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7120. MEM_REALLOC( \
  7121. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7122. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7123. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7124. ctx->frame_csp_boundary = \
  7125. ctx->frame_csp_bottom \
  7126. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7127. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7128. } \
  7129. } while (0)
  7130. #define CHECK_CSP_POP() \
  7131. do { \
  7132. if (ctx->csp_num < 1) { \
  7133. set_error_buf(error_buf, error_buf_size, \
  7134. "type mismatch: " \
  7135. "expect data but block stack was empty"); \
  7136. goto fail; \
  7137. } \
  7138. } while (0)
  7139. #if WASM_ENABLE_FAST_INTERP != 0
  7140. static bool
  7141. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7142. uint32 error_buf_size)
  7143. {
  7144. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7145. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7146. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7147. ctx->frame_offset_size + 16);
  7148. ctx->frame_offset_size += 16;
  7149. ctx->frame_offset_boundary =
  7150. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7151. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7152. }
  7153. return true;
  7154. fail:
  7155. return false;
  7156. }
  7157. static bool
  7158. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7159. {
  7160. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7161. return false;
  7162. return true;
  7163. }
  7164. static void
  7165. free_label_patch_list(BranchBlock *frame_csp)
  7166. {
  7167. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7168. BranchBlockPatch *next;
  7169. while (label_patch != NULL) {
  7170. next = label_patch->next;
  7171. wasm_runtime_free(label_patch);
  7172. label_patch = next;
  7173. }
  7174. frame_csp->patch_list = NULL;
  7175. }
  7176. static void
  7177. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7178. {
  7179. BranchBlock *tmp_csp = frame_csp;
  7180. uint32 i;
  7181. for (i = 0; i < csp_num; i++) {
  7182. free_label_patch_list(tmp_csp);
  7183. tmp_csp++;
  7184. }
  7185. }
  7186. static void
  7187. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7188. {
  7189. BranchBlock *tmp_csp = frame_csp;
  7190. uint32 i;
  7191. for (i = 0; i < csp_num; i++) {
  7192. if (tmp_csp->param_frame_offsets)
  7193. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7194. tmp_csp++;
  7195. }
  7196. }
  7197. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7198. #if WASM_ENABLE_GC != 0
  7199. static bool
  7200. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7201. char *error_buf, uint32 error_buf_size)
  7202. {
  7203. BranchBlock *current_csp = ctx->frame_csp - 1;
  7204. uint32 local_mask_size;
  7205. if (local_count == 0) {
  7206. current_csp->local_use_mask_size = 0;
  7207. return true;
  7208. }
  7209. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7210. * else branch, we don't need to allocate memory again */
  7211. if (!current_csp->local_use_mask) {
  7212. local_mask_size = (local_count + 7) / sizeof(uint8);
  7213. if (!(current_csp->local_use_mask =
  7214. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7215. return false;
  7216. }
  7217. current_csp->local_use_mask_size = local_mask_size;
  7218. }
  7219. else {
  7220. local_mask_size = current_csp->local_use_mask_size;
  7221. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7222. }
  7223. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7224. /* For non-function blocks, inherit the use status from parent block */
  7225. BranchBlock *parent_csp = current_csp - 1;
  7226. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7227. bh_assert(parent_csp->local_use_mask);
  7228. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7229. parent_csp->local_use_mask, local_mask_size);
  7230. }
  7231. return true;
  7232. }
  7233. static void
  7234. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7235. {
  7236. BranchBlock *current_csp = ctx->frame_csp - 1;
  7237. bh_assert(current_csp->local_use_mask
  7238. || current_csp->local_use_mask_size == 0);
  7239. if (current_csp->local_use_mask) {
  7240. wasm_runtime_free(current_csp->local_use_mask);
  7241. }
  7242. current_csp->local_use_mask = NULL;
  7243. current_csp->local_use_mask_size = 0;
  7244. }
  7245. static void
  7246. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7247. {
  7248. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7249. uint32 i;
  7250. for (i = 0; i < ctx->csp_num; i++) {
  7251. if (tmp_csp->local_use_mask) {
  7252. wasm_runtime_free(tmp_csp->local_use_mask);
  7253. tmp_csp->local_use_mask = NULL;
  7254. tmp_csp->local_use_mask_size = 0;
  7255. }
  7256. tmp_csp++;
  7257. }
  7258. }
  7259. static void
  7260. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7261. {
  7262. BranchBlock *current_csp = ctx->frame_csp - 1;
  7263. uint32 byte_offset = index / sizeof(uint8);
  7264. uint32 bit_offset = index % sizeof(uint8);
  7265. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7266. bh_assert(current_csp->local_use_mask);
  7267. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7268. }
  7269. static bool
  7270. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7271. {
  7272. BranchBlock *current_csp = ctx->frame_csp - 1;
  7273. uint32 byte_offset = index / sizeof(uint8);
  7274. uint32 bit_offset = index % sizeof(uint8);
  7275. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7276. bh_assert(current_csp->local_use_mask);
  7277. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7278. ? true
  7279. : false;
  7280. }
  7281. #endif /* end of WASM_ENABLE_GC != 0 */
  7282. static void
  7283. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7284. {
  7285. if (ctx) {
  7286. if (ctx->frame_ref_bottom)
  7287. wasm_runtime_free(ctx->frame_ref_bottom);
  7288. #if WASM_ENABLE_GC != 0
  7289. if (ctx->frame_reftype_map_bottom)
  7290. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7291. #endif
  7292. if (ctx->frame_csp_bottom) {
  7293. #if WASM_ENABLE_FAST_INTERP != 0
  7294. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7295. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7296. ctx->csp_num);
  7297. #endif
  7298. #if WASM_ENABLE_GC != 0
  7299. wasm_loader_clean_all_local_use_masks(ctx);
  7300. #endif
  7301. wasm_runtime_free(ctx->frame_csp_bottom);
  7302. }
  7303. #if WASM_ENABLE_FAST_INTERP != 0
  7304. if (ctx->frame_offset_bottom)
  7305. wasm_runtime_free(ctx->frame_offset_bottom);
  7306. if (ctx->const_buf)
  7307. wasm_runtime_free(ctx->const_buf);
  7308. #endif
  7309. wasm_runtime_free(ctx);
  7310. }
  7311. }
  7312. static WASMLoaderContext *
  7313. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7314. {
  7315. WASMLoaderContext *loader_ctx =
  7316. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7317. if (!loader_ctx)
  7318. return NULL;
  7319. loader_ctx->frame_ref_size = 32;
  7320. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7321. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7322. goto fail;
  7323. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7324. #if WASM_ENABLE_GC != 0
  7325. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7326. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7327. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7328. error_buf_size)))
  7329. goto fail;
  7330. loader_ctx->frame_reftype_map_boundary =
  7331. loader_ctx->frame_reftype_map_bottom + 16;
  7332. #endif
  7333. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7334. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7335. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7336. goto fail;
  7337. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7338. #if WASM_ENABLE_EXCE_HANDLING != 0
  7339. func->exception_handler_count = 0;
  7340. #endif
  7341. #if WASM_ENABLE_FAST_INTERP != 0
  7342. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7343. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7344. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7345. error_buf_size)))
  7346. goto fail;
  7347. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7348. loader_ctx->num_const = 0;
  7349. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7350. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7351. error_buf, error_buf_size)))
  7352. goto fail;
  7353. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7354. set_error_buf(error_buf, error_buf_size,
  7355. "fast interpreter offset overflow");
  7356. goto fail;
  7357. }
  7358. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7359. loader_ctx->max_dynamic_offset =
  7360. func->param_cell_num + func->local_cell_num;
  7361. #endif
  7362. return loader_ctx;
  7363. fail:
  7364. wasm_loader_ctx_destroy(loader_ctx);
  7365. return NULL;
  7366. }
  7367. static bool
  7368. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7369. uint32 error_buf_size)
  7370. {
  7371. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7372. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7373. /* Increase the frame ref stack */
  7374. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7375. ctx->frame_ref_size + 16);
  7376. ctx->frame_ref_size += 16;
  7377. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7378. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7379. }
  7380. #if WASM_ENABLE_GC != 0
  7381. if (wasm_is_type_multi_byte_type(type)
  7382. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7383. /* Increase the frame reftype map stack */
  7384. bh_assert(
  7385. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7386. * sizeof(WASMRefTypeMap))
  7387. == ctx->frame_reftype_map_size);
  7388. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7389. ctx->frame_reftype_map_size
  7390. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7391. ctx->frame_reftype_map =
  7392. ctx->frame_reftype_map_bottom
  7393. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7394. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7395. ctx->frame_reftype_map_boundary =
  7396. ctx->frame_reftype_map_bottom
  7397. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7398. }
  7399. #endif
  7400. return true;
  7401. fail:
  7402. return false;
  7403. }
  7404. static bool
  7405. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7406. uint32 error_buf_size)
  7407. {
  7408. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7409. uint32 i;
  7410. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7411. return false;
  7412. #if WASM_ENABLE_GC != 0
  7413. if (wasm_is_type_multi_byte_type(type)) {
  7414. WASMRefType *ref_type;
  7415. if (!(ref_type =
  7416. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7417. error_buf, error_buf_size))) {
  7418. return false;
  7419. }
  7420. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7421. /* Increase the frame reftype map stack */
  7422. bh_assert((uint32)((ctx->frame_reftype_map
  7423. - ctx->frame_reftype_map_bottom)
  7424. * sizeof(WASMRefTypeMap))
  7425. == ctx->frame_reftype_map_size);
  7426. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7427. ctx->frame_reftype_map_size,
  7428. ctx->frame_reftype_map_size
  7429. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7430. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7431. + ctx->frame_reftype_map_size
  7432. / ((uint32)sizeof(WASMRefTypeMap));
  7433. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7434. ctx->frame_reftype_map_boundary =
  7435. ctx->frame_reftype_map_bottom
  7436. + ctx->frame_reftype_map_size
  7437. / ((uint32)sizeof(WASMRefTypeMap));
  7438. }
  7439. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7440. ctx->frame_reftype_map->ref_type = ref_type;
  7441. ctx->frame_reftype_map++;
  7442. ctx->reftype_map_num++;
  7443. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7444. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7445. }
  7446. #endif
  7447. for (i = 0; i < type_cell_num; i++)
  7448. *ctx->frame_ref++ = type;
  7449. ctx->stack_cell_num += type_cell_num;
  7450. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7451. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7452. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7453. set_error_buf(error_buf, error_buf_size,
  7454. "operand stack depth limit exceeded");
  7455. return false;
  7456. }
  7457. }
  7458. return true;
  7459. #if WASM_ENABLE_GC != 0
  7460. fail:
  7461. return false;
  7462. #endif
  7463. }
  7464. static bool
  7465. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7466. int32 stack_cell_num,
  7467. #if WASM_ENABLE_GC != 0
  7468. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7469. #endif
  7470. uint8 type,
  7471. #if WASM_ENABLE_GC != 0
  7472. WASMRefType *ref_type,
  7473. #endif
  7474. char *error_buf, uint32 error_buf_size)
  7475. {
  7476. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7477. #if WASM_ENABLE_GC != 0
  7478. WASMRefType *frame_reftype = NULL;
  7479. #endif
  7480. if (stack_cell_num < type_cell_num) {
  7481. set_error_buf(error_buf, error_buf_size,
  7482. "type mismatch: expect data but stack was empty");
  7483. return false;
  7484. }
  7485. #if WASM_ENABLE_GC == 0
  7486. for (i = 0; i < type_cell_num; i++) {
  7487. if (*(frame_ref - 1 - i) != type) {
  7488. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7489. "type mismatch: expect ", type2str(type),
  7490. " but got other");
  7491. return false;
  7492. }
  7493. }
  7494. #else
  7495. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7496. bh_assert(reftype_map_num > 0);
  7497. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7498. }
  7499. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7500. ref_type, ctx->module->types,
  7501. ctx->module->type_count)) {
  7502. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7503. "type mismatch: expect ", type2str(type),
  7504. " but got other");
  7505. return false;
  7506. }
  7507. for (i = 0; i < type_cell_num - 1; i++) {
  7508. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7509. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7510. "type mismatch: expect ", type2str(type),
  7511. " but got other");
  7512. return false;
  7513. }
  7514. }
  7515. #endif
  7516. return true;
  7517. }
  7518. static bool
  7519. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7520. uint32 error_buf_size)
  7521. {
  7522. int32 block_stack_cell_num =
  7523. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7524. #if WASM_ENABLE_GC != 0
  7525. int32 reftype_map_num =
  7526. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7527. #endif
  7528. if (block_stack_cell_num > 0) {
  7529. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7530. /* the stack top is a value of any type, return success */
  7531. return true;
  7532. }
  7533. #if WASM_ENABLE_GC != 0
  7534. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7535. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7536. WASMRefType *stack_top_ref_type = NULL;
  7537. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7538. bh_assert(reftype_map_num > 0);
  7539. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7540. }
  7541. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7542. ctx->ref_type_tmp, ctx->module->types,
  7543. ctx->module->type_count)) {
  7544. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7545. uint32 ref_type_struct_size =
  7546. wasm_reftype_struct_size(stack_top_ref_type);
  7547. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7548. stack_top_ref_type, ref_type_struct_size);
  7549. }
  7550. return true;
  7551. }
  7552. }
  7553. #endif
  7554. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7555. #if WASM_ENABLE_GC != 0
  7556. ctx->frame_reftype_map, reftype_map_num,
  7557. #endif
  7558. type,
  7559. #if WASM_ENABLE_GC != 0
  7560. ctx->ref_type_tmp,
  7561. #endif
  7562. error_buf, error_buf_size)) {
  7563. return false;
  7564. }
  7565. return true;
  7566. }
  7567. static bool
  7568. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7569. uint32 error_buf_size)
  7570. {
  7571. BranchBlock *cur_block = ctx->frame_csp - 1;
  7572. int32 available_stack_cell =
  7573. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7574. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7575. /* Directly return success if current block is in stack
  7576. polymorphic state while stack is empty. */
  7577. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7578. return true;
  7579. if (type == VALUE_TYPE_VOID)
  7580. return true;
  7581. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7582. return false;
  7583. bh_assert(available_stack_cell > 0);
  7584. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7585. type = VALUE_TYPE_ANY;
  7586. cell_num_to_pop = 1;
  7587. }
  7588. ctx->frame_ref -= cell_num_to_pop;
  7589. ctx->stack_cell_num -= cell_num_to_pop;
  7590. #if WASM_ENABLE_GC != 0
  7591. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7592. ctx->frame_reftype_map--;
  7593. ctx->reftype_map_num--;
  7594. }
  7595. #endif
  7596. return true;
  7597. }
  7598. #if WASM_ENABLE_GC != 0
  7599. /* Get the stack top element of current block */
  7600. static bool
  7601. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7602. WASMRefType **p_ref_type, char *error_buf,
  7603. uint32 error_buf_size)
  7604. {
  7605. BranchBlock *cur_block = ctx->frame_csp - 1;
  7606. int32 available_stack_cell =
  7607. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7608. if (available_stack_cell <= 0) {
  7609. /* Directly return success if current block is in stack
  7610. polymorphic state while stack is empty. */
  7611. if (cur_block->is_stack_polymorphic) {
  7612. *p_type = VALUE_TYPE_ANY;
  7613. return true;
  7614. }
  7615. else {
  7616. set_error_buf(
  7617. error_buf, error_buf_size,
  7618. "type mismatch: expect data but block stack was empty");
  7619. return false;
  7620. }
  7621. }
  7622. *p_type = *(ctx->frame_ref - 1);
  7623. if (wasm_is_type_multi_byte_type(*p_type)) {
  7624. int32 available_reftype_map =
  7625. (int32)(ctx->reftype_map_num
  7626. - (ctx->frame_csp - 1)->reftype_map_num);
  7627. bh_assert(available_reftype_map > 0);
  7628. (void)available_reftype_map;
  7629. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7630. }
  7631. return true;
  7632. }
  7633. #if WASM_ENABLE_FAST_INTERP != 0
  7634. static bool
  7635. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7636. char *error_buf, uint32 error_buf_size);
  7637. #endif
  7638. /* Check whether the stack top elem is a heap object, and if yes,
  7639. pop and return it */
  7640. static bool
  7641. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7642. WASMRefType *ref_ht_ret, char *error_buf,
  7643. uint32 error_buf_size)
  7644. {
  7645. uint8 type = 0;
  7646. WASMRefType *ref_type = NULL;
  7647. /* Get stack top element */
  7648. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7649. error_buf_size)) {
  7650. return false;
  7651. }
  7652. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7653. /* stack top isn't a ref type */
  7654. && !wasm_is_type_reftype(type)) {
  7655. set_error_buf(error_buf, error_buf_size,
  7656. "type mismatch: expect heap object but got others");
  7657. return false;
  7658. }
  7659. /* POP stack top */
  7660. if (wasm_is_type_multi_byte_type(type)) {
  7661. bh_assert(ref_type);
  7662. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7663. wasm_reftype_struct_size(ref_type));
  7664. }
  7665. #if WASM_ENABLE_FAST_INTERP != 0
  7666. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7667. error_buf_size)) {
  7668. return false;
  7669. }
  7670. #else
  7671. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7672. return false;
  7673. }
  7674. #endif
  7675. if (p_type)
  7676. *p_type = type;
  7677. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7678. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7679. wasm_reftype_struct_size(ref_type));
  7680. }
  7681. return true;
  7682. }
  7683. /* Check whether the stack top elem is subtype of (ref null ht),
  7684. and if yes, pop it and return the converted (ref ht) */
  7685. static bool
  7686. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7687. WASMRefType *ref_ht_ret, char *error_buf,
  7688. uint32 error_buf_size)
  7689. {
  7690. uint8 type = 0;
  7691. WASMRefType ref_type = { 0 };
  7692. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7693. error_buf_size)) {
  7694. return false;
  7695. }
  7696. /* Convert to related (ref ht) and return */
  7697. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7698. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7699. */
  7700. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7701. HEAP_TYPE_ARRAY
  7702. + (type - REF_TYPE_ARRAYREF));
  7703. type = ref_ht_ret->ref_type;
  7704. }
  7705. else if (wasm_is_reftype_htref_nullable(type)
  7706. || wasm_is_reftype_htref_non_nullable(type)) {
  7707. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7708. wasm_reftype_struct_size(&ref_type));
  7709. /* Convert to (ref ht) */
  7710. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7711. ref_ht_ret->ref_ht_common.nullable = false;
  7712. type = ref_ht_ret->ref_type;
  7713. }
  7714. *p_type = type;
  7715. return true;
  7716. }
  7717. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7718. and if yes, pop it and return the type_idx */
  7719. static bool
  7720. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7721. uint32 *p_type_idx, char *error_buf,
  7722. uint32 error_buf_size)
  7723. {
  7724. uint8 type = 0;
  7725. int32 type_idx = -1;
  7726. WASMRefType *ref_type = NULL;
  7727. /* Get stack top element */
  7728. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7729. error_buf_size)) {
  7730. return false;
  7731. }
  7732. if (type != VALUE_TYPE_ANY) {
  7733. /* stack top isn't (ref null $t) */
  7734. if (!((wasm_is_reftype_htref_nullable(type)
  7735. || wasm_is_reftype_htref_non_nullable(type))
  7736. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7737. set_error_buf(error_buf, error_buf_size,
  7738. "type mismatch: expect (ref null $t) but got others");
  7739. return false;
  7740. }
  7741. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7742. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7743. wasm_reftype_struct_size(ref_type));
  7744. }
  7745. /* POP stack top */
  7746. #if WASM_ENABLE_FAST_INTERP != 0
  7747. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7748. error_buf_size)) {
  7749. return false;
  7750. }
  7751. #else
  7752. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7753. return false;
  7754. }
  7755. #endif
  7756. /* Convert to type_idx and return */
  7757. *p_type = type;
  7758. if (type != VALUE_TYPE_ANY)
  7759. *p_type_idx = (uint32)type_idx;
  7760. return true;
  7761. }
  7762. #endif /* WASM_ENABLE_GC != 0 */
  7763. #if WASM_ENABLE_FAST_INTERP == 0
  7764. static bool
  7765. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7766. uint8 type_push, uint8 type_pop, char *error_buf,
  7767. uint32 error_buf_size)
  7768. {
  7769. for (int i = 0; i < pop_cnt; i++) {
  7770. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7771. error_buf_size))
  7772. return false;
  7773. }
  7774. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7775. return false;
  7776. return true;
  7777. }
  7778. #endif
  7779. static bool
  7780. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7781. BlockType block_type, uint8 *start_addr,
  7782. char *error_buf, uint32 error_buf_size)
  7783. {
  7784. CHECK_CSP_PUSH();
  7785. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7786. ctx->frame_csp->label_type = label_type;
  7787. ctx->frame_csp->block_type = block_type;
  7788. ctx->frame_csp->start_addr = start_addr;
  7789. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7790. #if WASM_ENABLE_GC != 0
  7791. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7792. #endif
  7793. #if WASM_ENABLE_FAST_INTERP != 0
  7794. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7795. ctx->frame_csp->patch_list = NULL;
  7796. #endif
  7797. ctx->frame_csp++;
  7798. ctx->csp_num++;
  7799. if (ctx->csp_num > ctx->max_csp_num) {
  7800. ctx->max_csp_num = ctx->csp_num;
  7801. if (ctx->max_csp_num > UINT16_MAX) {
  7802. set_error_buf(error_buf, error_buf_size,
  7803. "label stack depth limit exceeded");
  7804. return false;
  7805. }
  7806. }
  7807. return true;
  7808. fail:
  7809. return false;
  7810. }
  7811. static bool
  7812. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7813. uint32 error_buf_size)
  7814. {
  7815. CHECK_CSP_POP();
  7816. #if WASM_ENABLE_FAST_INTERP != 0
  7817. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7818. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7819. #endif
  7820. ctx->frame_csp--;
  7821. ctx->csp_num--;
  7822. return true;
  7823. fail:
  7824. return false;
  7825. }
  7826. #if WASM_ENABLE_FAST_INTERP != 0
  7827. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7828. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7829. #define emit_label(opcode) \
  7830. do { \
  7831. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7832. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7833. } while (0)
  7834. #define skip_label() \
  7835. do { \
  7836. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7837. LOG_OP("\ndelete last op\n"); \
  7838. } while (0)
  7839. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7840. #if UINTPTR_MAX == UINT64_MAX
  7841. #define emit_label(opcode) \
  7842. do { \
  7843. int32 offset = \
  7844. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7845. /* emit int32 relative offset in 64-bit target */ \
  7846. wasm_loader_emit_uint32(loader_ctx, offset); \
  7847. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7848. } while (0)
  7849. #else
  7850. #define emit_label(opcode) \
  7851. do { \
  7852. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7853. /* emit uint32 label address in 32-bit target */ \
  7854. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7855. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7856. } while (0)
  7857. #endif
  7858. #define skip_label() \
  7859. do { \
  7860. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7861. LOG_OP("\ndelete last op\n"); \
  7862. } while (0)
  7863. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7864. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7865. #define emit_label(opcode) \
  7866. do { \
  7867. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7868. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7869. } while (0)
  7870. #define skip_label() \
  7871. do { \
  7872. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7873. LOG_OP("\ndelete last op\n"); \
  7874. } while (0)
  7875. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7876. #define emit_empty_label_addr_and_frame_ip(type) \
  7877. do { \
  7878. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7879. loader_ctx->p_code_compiled, error_buf, \
  7880. error_buf_size)) \
  7881. goto fail; \
  7882. /* label address, to be patched */ \
  7883. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7884. } while (0)
  7885. #define emit_br_info(frame_csp, is_br) \
  7886. do { \
  7887. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7888. error_buf_size)) \
  7889. goto fail; \
  7890. } while (0)
  7891. #define LAST_OP_OUTPUT_I32() \
  7892. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7893. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7894. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7895. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7896. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7897. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7898. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7899. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7900. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7901. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7902. || (last_op == EXT_OP_COPY_STACK_TOP)
  7903. #define LAST_OP_OUTPUT_I64() \
  7904. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7905. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7906. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7907. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7908. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7909. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7910. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7911. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7912. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7913. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7914. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7915. #define GET_CONST_OFFSET(type, val) \
  7916. do { \
  7917. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7918. &operand_offset, error_buf, \
  7919. error_buf_size))) \
  7920. goto fail; \
  7921. } while (0)
  7922. #define GET_CONST_F32_OFFSET(type, fval) \
  7923. do { \
  7924. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7925. &operand_offset, error_buf, \
  7926. error_buf_size))) \
  7927. goto fail; \
  7928. } while (0)
  7929. #define GET_CONST_F64_OFFSET(type, fval) \
  7930. do { \
  7931. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7932. &operand_offset, error_buf, \
  7933. error_buf_size))) \
  7934. goto fail; \
  7935. } while (0)
  7936. #define emit_operand(ctx, offset) \
  7937. do { \
  7938. wasm_loader_emit_int16(ctx, offset); \
  7939. LOG_OP("%d\t", offset); \
  7940. } while (0)
  7941. #define emit_byte(ctx, byte) \
  7942. do { \
  7943. wasm_loader_emit_uint8(ctx, byte); \
  7944. LOG_OP("%d\t", byte); \
  7945. } while (0)
  7946. #define emit_uint32(ctx, value) \
  7947. do { \
  7948. wasm_loader_emit_uint32(ctx, value); \
  7949. LOG_OP("%d\t", value); \
  7950. } while (0)
  7951. #define emit_uint64(ctx, value) \
  7952. do { \
  7953. wasm_loader_emit_const(ctx, &value, false); \
  7954. LOG_OP("%lld\t", value); \
  7955. } while (0)
  7956. #define emit_float32(ctx, value) \
  7957. do { \
  7958. wasm_loader_emit_const(ctx, &value, true); \
  7959. LOG_OP("%f\t", value); \
  7960. } while (0)
  7961. #define emit_float64(ctx, value) \
  7962. do { \
  7963. wasm_loader_emit_const(ctx, &value, false); \
  7964. LOG_OP("%f\t", value); \
  7965. } while (0)
  7966. static bool
  7967. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7968. {
  7969. if (!(ctx->p_code_compiled =
  7970. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7971. return false;
  7972. ctx->p_code_compiled_end =
  7973. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  7974. /* clean up frame ref */
  7975. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  7976. ctx->frame_ref = ctx->frame_ref_bottom;
  7977. ctx->stack_cell_num = 0;
  7978. #if WASM_ENABLE_GC != 0
  7979. /* clean up reftype map */
  7980. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  7981. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  7982. ctx->reftype_map_num = 0;
  7983. #endif
  7984. /* clean up frame csp */
  7985. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  7986. ctx->frame_csp = ctx->frame_csp_bottom;
  7987. ctx->csp_num = 0;
  7988. ctx->max_csp_num = 0;
  7989. /* clean up frame offset */
  7990. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  7991. ctx->frame_offset = ctx->frame_offset_bottom;
  7992. ctx->dynamic_offset = ctx->start_dynamic_offset;
  7993. /* init preserved local offsets */
  7994. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  7995. /* const buf is reserved */
  7996. return true;
  7997. }
  7998. static void
  7999. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8000. {
  8001. ctx->code_compiled_size += size;
  8002. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8003. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8004. }
  8005. }
  8006. static void
  8007. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8008. {
  8009. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8010. if (ctx->p_code_compiled) {
  8011. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8012. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8013. #endif
  8014. bh_memcpy_s(ctx->p_code_compiled,
  8015. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8016. value, size);
  8017. ctx->p_code_compiled += size;
  8018. }
  8019. else {
  8020. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8021. bh_assert((ctx->code_compiled_size & 1) == 0);
  8022. #endif
  8023. increase_compiled_code_space(ctx, size);
  8024. }
  8025. }
  8026. static void
  8027. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8028. {
  8029. if (ctx->p_code_compiled) {
  8030. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8031. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8032. #endif
  8033. STORE_U32(ctx->p_code_compiled, value);
  8034. ctx->p_code_compiled += sizeof(uint32);
  8035. }
  8036. else {
  8037. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8038. bh_assert((ctx->code_compiled_size & 1) == 0);
  8039. #endif
  8040. increase_compiled_code_space(ctx, sizeof(uint32));
  8041. }
  8042. }
  8043. static void
  8044. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8045. {
  8046. if (ctx->p_code_compiled) {
  8047. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8048. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8049. #endif
  8050. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8051. ctx->p_code_compiled += sizeof(int16);
  8052. }
  8053. else {
  8054. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8055. bh_assert((ctx->code_compiled_size & 1) == 0);
  8056. #endif
  8057. increase_compiled_code_space(ctx, sizeof(uint16));
  8058. }
  8059. }
  8060. static void
  8061. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8062. {
  8063. if (ctx->p_code_compiled) {
  8064. *(ctx->p_code_compiled) = value;
  8065. ctx->p_code_compiled += sizeof(uint8);
  8066. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8067. ctx->p_code_compiled++;
  8068. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8069. #endif
  8070. }
  8071. else {
  8072. increase_compiled_code_space(ctx, sizeof(uint8));
  8073. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8074. increase_compiled_code_space(ctx, sizeof(uint8));
  8075. bh_assert((ctx->code_compiled_size & 1) == 0);
  8076. #endif
  8077. }
  8078. }
  8079. static void
  8080. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8081. {
  8082. if (ctx->p_code_compiled) {
  8083. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8084. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8085. #endif
  8086. STORE_PTR(ctx->p_code_compiled, value);
  8087. ctx->p_code_compiled += sizeof(void *);
  8088. }
  8089. else {
  8090. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8091. bh_assert((ctx->code_compiled_size & 1) == 0);
  8092. #endif
  8093. increase_compiled_code_space(ctx, sizeof(void *));
  8094. }
  8095. }
  8096. static void
  8097. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8098. {
  8099. if (ctx->p_code_compiled) {
  8100. ctx->p_code_compiled -= size;
  8101. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8102. if (size == sizeof(uint8)) {
  8103. ctx->p_code_compiled--;
  8104. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8105. }
  8106. #endif
  8107. }
  8108. else {
  8109. ctx->code_compiled_size -= size;
  8110. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8111. if (size == sizeof(uint8)) {
  8112. ctx->code_compiled_size--;
  8113. bh_assert((ctx->code_compiled_size & 1) == 0);
  8114. }
  8115. #endif
  8116. }
  8117. }
  8118. static bool
  8119. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8120. uint32 local_index, uint32 local_type,
  8121. bool *preserved, char *error_buf,
  8122. uint32 error_buf_size)
  8123. {
  8124. uint32 i = 0;
  8125. int16 preserved_offset = (int16)local_index;
  8126. *preserved = false;
  8127. while (i < loader_ctx->stack_cell_num) {
  8128. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8129. /* move previous local into dynamic space before a set/tee_local opcode
  8130. */
  8131. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8132. if (!(*preserved)) {
  8133. *preserved = true;
  8134. skip_label();
  8135. preserved_offset = loader_ctx->preserved_local_offset;
  8136. if (loader_ctx->p_code_compiled) {
  8137. bh_assert(preserved_offset != (int16)local_index);
  8138. }
  8139. if (is_32bit_type(local_type)) {
  8140. /* Only increase preserve offset in the second traversal */
  8141. if (loader_ctx->p_code_compiled)
  8142. loader_ctx->preserved_local_offset++;
  8143. emit_label(EXT_OP_COPY_STACK_TOP);
  8144. }
  8145. else {
  8146. if (loader_ctx->p_code_compiled)
  8147. loader_ctx->preserved_local_offset += 2;
  8148. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8149. }
  8150. emit_operand(loader_ctx, local_index);
  8151. emit_operand(loader_ctx, preserved_offset);
  8152. emit_label(opcode);
  8153. }
  8154. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8155. }
  8156. if (is_32bit_type(cur_type))
  8157. i++;
  8158. else
  8159. i += 2;
  8160. }
  8161. (void)error_buf;
  8162. (void)error_buf_size;
  8163. return true;
  8164. }
  8165. static bool
  8166. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8167. char *error_buf, uint32 error_buf_size)
  8168. {
  8169. uint32 i = 0;
  8170. bool preserve_local;
  8171. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8172. blocks will not influence the value of these locals */
  8173. while (i < loader_ctx->stack_cell_num) {
  8174. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8175. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8176. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8177. && (cur_offset >= 0)) {
  8178. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8179. cur_type, &preserve_local,
  8180. error_buf, error_buf_size)))
  8181. return false;
  8182. }
  8183. if (is_32bit_type(cur_type)) {
  8184. i++;
  8185. }
  8186. else {
  8187. i += 2;
  8188. }
  8189. }
  8190. return true;
  8191. }
  8192. static bool
  8193. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8194. uint8 *p_code_compiled, char *error_buf,
  8195. uint32 error_buf_size)
  8196. {
  8197. BranchBlockPatch *patch =
  8198. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8199. if (!patch) {
  8200. return false;
  8201. }
  8202. patch->patch_type = patch_type;
  8203. patch->code_compiled = p_code_compiled;
  8204. if (!frame_csp->patch_list) {
  8205. frame_csp->patch_list = patch;
  8206. patch->next = NULL;
  8207. }
  8208. else {
  8209. patch->next = frame_csp->patch_list;
  8210. frame_csp->patch_list = patch;
  8211. }
  8212. return true;
  8213. }
  8214. static void
  8215. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8216. {
  8217. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8218. BranchBlockPatch *node = frame_csp->patch_list;
  8219. BranchBlockPatch *node_prev = NULL, *node_next;
  8220. if (!ctx->p_code_compiled)
  8221. return;
  8222. while (node) {
  8223. node_next = node->next;
  8224. if (node->patch_type == patch_type) {
  8225. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8226. if (node_prev == NULL) {
  8227. frame_csp->patch_list = node_next;
  8228. }
  8229. else {
  8230. node_prev->next = node_next;
  8231. }
  8232. wasm_runtime_free(node);
  8233. }
  8234. else {
  8235. node_prev = node;
  8236. }
  8237. node = node_next;
  8238. }
  8239. }
  8240. static bool
  8241. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8242. bool is_br, char *error_buf, uint32 error_buf_size)
  8243. {
  8244. /* br info layout:
  8245. * a) arity of target block
  8246. * b) total cell num of arity values
  8247. * c) each arity value's cell num
  8248. * d) each arity value's src frame offset
  8249. * e) each arity values's dst dynamic offset
  8250. * f) branch target address
  8251. *
  8252. * Note: b-e are omitted when arity is 0 so that
  8253. * interpreter can recover the br info quickly.
  8254. */
  8255. BlockType *block_type = &frame_csp->block_type;
  8256. uint8 *types = NULL, cell;
  8257. #if WASM_ENABLE_GC != 0
  8258. WASMRefTypeMap *reftype_maps;
  8259. uint32 reftype_map_count;
  8260. #endif
  8261. uint32 arity = 0;
  8262. int32 i;
  8263. int16 *frame_offset = ctx->frame_offset;
  8264. uint16 dynamic_offset;
  8265. /* Note: loop's arity is different from if and block. loop's arity is
  8266. * its parameter count while if and block arity is result count.
  8267. */
  8268. #if WASM_ENABLE_GC == 0
  8269. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8270. arity = block_type_get_param_types(block_type, &types);
  8271. else
  8272. arity = block_type_get_result_types(block_type, &types);
  8273. #else
  8274. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8275. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8276. &reftype_map_count);
  8277. else
  8278. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8279. &reftype_map_count);
  8280. #endif
  8281. /* Part a */
  8282. emit_uint32(ctx, arity);
  8283. if (arity) {
  8284. /* Part b */
  8285. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8286. /* Part c */
  8287. for (i = (int32)arity - 1; i >= 0; i--) {
  8288. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8289. emit_byte(ctx, cell);
  8290. }
  8291. /* Part d */
  8292. for (i = (int32)arity - 1; i >= 0; i--) {
  8293. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8294. frame_offset -= cell;
  8295. emit_operand(ctx, *(int16 *)(frame_offset));
  8296. }
  8297. /* Part e */
  8298. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8299. /* Use start_dynamic_offset which was set in
  8300. copy_params_to_dynamic_space */
  8301. dynamic_offset = frame_csp->start_dynamic_offset
  8302. + wasm_get_cell_num(types, arity);
  8303. else
  8304. dynamic_offset =
  8305. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8306. if (is_br)
  8307. ctx->dynamic_offset = dynamic_offset;
  8308. for (i = (int32)arity - 1; i >= 0; i--) {
  8309. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8310. dynamic_offset -= cell;
  8311. emit_operand(ctx, dynamic_offset);
  8312. }
  8313. }
  8314. /* Part f */
  8315. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8316. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8317. }
  8318. else {
  8319. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8320. error_buf, error_buf_size))
  8321. return false;
  8322. /* label address, to be patched */
  8323. wasm_loader_emit_ptr(ctx, NULL);
  8324. }
  8325. return true;
  8326. }
  8327. static bool
  8328. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8329. bool disable_emit, int16 operand_offset,
  8330. char *error_buf, uint32 error_buf_size)
  8331. {
  8332. uint32 cell_num_to_push, i;
  8333. if (type == VALUE_TYPE_VOID)
  8334. return true;
  8335. /* only check memory overflow in first traverse */
  8336. if (ctx->p_code_compiled == NULL) {
  8337. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8338. return false;
  8339. }
  8340. if (disable_emit)
  8341. *(ctx->frame_offset)++ = operand_offset;
  8342. else {
  8343. emit_operand(ctx, ctx->dynamic_offset);
  8344. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8345. ctx->dynamic_offset++;
  8346. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8347. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8348. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8349. goto fail;
  8350. }
  8351. }
  8352. }
  8353. if (is_32bit_type(type))
  8354. return true;
  8355. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8356. for (i = 0; i < cell_num_to_push; i++) {
  8357. if (ctx->p_code_compiled == NULL) {
  8358. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8359. return false;
  8360. }
  8361. ctx->frame_offset++;
  8362. if (!disable_emit) {
  8363. ctx->dynamic_offset++;
  8364. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8365. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8366. if (ctx->max_dynamic_offset >= INT16_MAX)
  8367. goto fail;
  8368. }
  8369. }
  8370. }
  8371. return true;
  8372. fail:
  8373. set_error_buf(error_buf, error_buf_size,
  8374. "fast interpreter offset overflow");
  8375. return false;
  8376. }
  8377. /* This function should be in front of wasm_loader_pop_frame_ref
  8378. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8379. will be modified by wasm_loader_pop_frame_ref */
  8380. static bool
  8381. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8382. char *error_buf, uint32 error_buf_size)
  8383. {
  8384. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8385. then current block is the function block */
  8386. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8387. BranchBlock *cur_block = ctx->frame_csp - depth;
  8388. int32 available_stack_cell =
  8389. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8390. uint32 cell_num_to_pop;
  8391. /* Directly return success if current block is in stack
  8392. polymorphic state while stack is empty. */
  8393. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8394. return true;
  8395. if (type == VALUE_TYPE_VOID)
  8396. return true;
  8397. /* Change type to ANY when the stack top is ANY, so as to avoid
  8398. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8399. two offsets */
  8400. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8401. type = VALUE_TYPE_ANY;
  8402. cell_num_to_pop = wasm_value_type_cell_num(type);
  8403. /* Check the offset stack bottom to ensure the frame offset
  8404. stack will not go underflow. But we don't thrown error
  8405. and return true here, because the error msg should be
  8406. given in wasm_loader_pop_frame_ref */
  8407. if (!check_offset_pop(ctx, cell_num_to_pop))
  8408. return true;
  8409. ctx->frame_offset -= cell_num_to_pop;
  8410. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8411. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8412. ctx->dynamic_offset -= cell_num_to_pop;
  8413. emit_operand(ctx, *(ctx->frame_offset));
  8414. (void)error_buf;
  8415. (void)error_buf_size;
  8416. return true;
  8417. }
  8418. static bool
  8419. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8420. bool disable_emit, int16 operand_offset,
  8421. char *error_buf, uint32 error_buf_size)
  8422. {
  8423. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8424. error_buf, error_buf_size)))
  8425. return false;
  8426. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8427. return false;
  8428. return true;
  8429. }
  8430. static bool
  8431. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8432. char *error_buf, uint32 error_buf_size)
  8433. {
  8434. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8435. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8436. return false;
  8437. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8438. return false;
  8439. return true;
  8440. }
  8441. static bool
  8442. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8443. uint8 type_push, uint8 type_pop,
  8444. bool disable_emit, int16 operand_offset,
  8445. char *error_buf, uint32 error_buf_size)
  8446. {
  8447. uint8 i;
  8448. for (i = 0; i < pop_cnt; i++) {
  8449. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8450. error_buf_size))
  8451. return false;
  8452. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8453. error_buf_size))
  8454. return false;
  8455. }
  8456. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8457. operand_offset, error_buf,
  8458. error_buf_size))
  8459. return false;
  8460. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8461. return false;
  8462. return true;
  8463. }
  8464. static bool
  8465. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8466. int16 *offset, char *error_buf,
  8467. uint32 error_buf_size)
  8468. {
  8469. int8 bytes_to_increase;
  8470. int16 operand_offset = 0;
  8471. Const *c;
  8472. /* Search existing constant */
  8473. for (c = (Const *)ctx->const_buf;
  8474. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8475. /* TODO: handle v128 type? */
  8476. if ((type == c->value_type)
  8477. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8478. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8479. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8480. || (type == VALUE_TYPE_FUNCREF
  8481. && *(int32 *)value == c->value.i32)
  8482. || (type == VALUE_TYPE_EXTERNREF
  8483. && *(int32 *)value == c->value.i32)
  8484. #endif
  8485. || (type == VALUE_TYPE_F64
  8486. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8487. || (type == VALUE_TYPE_F32
  8488. && (0
  8489. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8490. operand_offset = c->slot_index;
  8491. break;
  8492. }
  8493. if (is_32bit_type(c->value_type))
  8494. operand_offset += 1;
  8495. else
  8496. operand_offset += 2;
  8497. }
  8498. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8499. /* New constant, append to the const buffer */
  8500. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8501. bytes_to_increase = 2;
  8502. }
  8503. else {
  8504. bytes_to_increase = 1;
  8505. }
  8506. /* The max cell num of const buffer is 32768 since the valid index range
  8507. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8508. * full */
  8509. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8510. *offset = 0;
  8511. return true;
  8512. }
  8513. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8514. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8515. ctx->const_buf_size + 4 * sizeof(Const));
  8516. ctx->const_buf_size += 4 * sizeof(Const);
  8517. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8518. }
  8519. c->value_type = type;
  8520. switch (type) {
  8521. case VALUE_TYPE_F64:
  8522. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8523. sizeof(float64));
  8524. ctx->const_cell_num += 2;
  8525. /* The const buf will be reversed, we use the second cell */
  8526. /* of the i64/f64 const so the final offset is correct */
  8527. operand_offset++;
  8528. break;
  8529. case VALUE_TYPE_I64:
  8530. c->value.i64 = *(int64 *)value;
  8531. ctx->const_cell_num += 2;
  8532. operand_offset++;
  8533. break;
  8534. case VALUE_TYPE_F32:
  8535. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8536. sizeof(float32));
  8537. ctx->const_cell_num++;
  8538. break;
  8539. case VALUE_TYPE_I32:
  8540. c->value.i32 = *(int32 *)value;
  8541. ctx->const_cell_num++;
  8542. break;
  8543. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8544. case VALUE_TYPE_EXTERNREF:
  8545. case VALUE_TYPE_FUNCREF:
  8546. c->value.i32 = *(int32 *)value;
  8547. ctx->const_cell_num++;
  8548. break;
  8549. #endif
  8550. default:
  8551. break;
  8552. }
  8553. c->slot_index = operand_offset;
  8554. ctx->num_const++;
  8555. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8556. (int64)c->value.i64);
  8557. }
  8558. /* use negative index for const */
  8559. operand_offset = -(operand_offset + 1);
  8560. *offset = operand_offset;
  8561. return true;
  8562. fail:
  8563. return false;
  8564. }
  8565. /*
  8566. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8567. -- Mostly used for the binary / compare operation
  8568. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8569. -- Mostly used in block / control instructions
  8570. The POP will always emit the offset on the top of the frame_offset stack
  8571. PUSH can be used in two ways:
  8572. 1. directly PUSH:
  8573. PUSH_XXX();
  8574. will allocate a dynamic space and emit
  8575. 2. silent PUSH:
  8576. operand_offset = xxx; disable_emit = true;
  8577. PUSH_XXX();
  8578. only push the frame_offset stack, no emit
  8579. */
  8580. #define TEMPLATE_PUSH(Type) \
  8581. do { \
  8582. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8583. disable_emit, operand_offset, \
  8584. error_buf, error_buf_size)) \
  8585. goto fail; \
  8586. } while (0)
  8587. #define TEMPLATE_PUSH_REF(Type) \
  8588. do { \
  8589. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8590. operand_offset, error_buf, \
  8591. error_buf_size)) \
  8592. goto fail; \
  8593. } while (0)
  8594. #define TEMPLATE_POP(Type) \
  8595. do { \
  8596. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8597. error_buf, error_buf_size)) \
  8598. goto fail; \
  8599. } while (0)
  8600. #define TEMPLATE_POP_REF(Type) \
  8601. do { \
  8602. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8603. error_buf_size)) \
  8604. goto fail; \
  8605. } while (0)
  8606. #define PUSH_OFFSET_TYPE(type) \
  8607. do { \
  8608. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8609. operand_offset, error_buf, \
  8610. error_buf_size))) \
  8611. goto fail; \
  8612. } while (0)
  8613. #define POP_OFFSET_TYPE(type) \
  8614. do { \
  8615. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8616. error_buf_size))) \
  8617. goto fail; \
  8618. } while (0)
  8619. #define POP_AND_PUSH(type_pop, type_push) \
  8620. do { \
  8621. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8622. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8623. operand_offset, error_buf, error_buf_size))) \
  8624. goto fail; \
  8625. } while (0)
  8626. /* type of POPs should be the same */
  8627. #define POP2_AND_PUSH(type_pop, type_push) \
  8628. do { \
  8629. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8630. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8631. operand_offset, error_buf, error_buf_size))) \
  8632. goto fail; \
  8633. } while (0)
  8634. #else /* WASM_ENABLE_FAST_INTERP */
  8635. #define TEMPLATE_PUSH(Type) \
  8636. do { \
  8637. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8638. error_buf, error_buf_size))) \
  8639. goto fail; \
  8640. } while (0)
  8641. #define TEMPLATE_PUSH_REF(Type) \
  8642. do { \
  8643. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8644. error_buf_size))) \
  8645. goto fail; \
  8646. } while (0)
  8647. #define TEMPLATE_POP(Type) \
  8648. do { \
  8649. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8650. error_buf, error_buf_size))) \
  8651. goto fail; \
  8652. } while (0)
  8653. #define TEMPLATE_POP_REF(Type) \
  8654. do { \
  8655. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8656. error_buf_size))) \
  8657. goto fail; \
  8658. } while (0)
  8659. #define POP_AND_PUSH(type_pop, type_push) \
  8660. do { \
  8661. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8662. type_pop, error_buf, \
  8663. error_buf_size))) \
  8664. goto fail; \
  8665. } while (0)
  8666. /* type of POPs should be the same */
  8667. #define POP2_AND_PUSH(type_pop, type_push) \
  8668. do { \
  8669. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8670. type_pop, error_buf, \
  8671. error_buf_size))) \
  8672. goto fail; \
  8673. } while (0)
  8674. #endif /* WASM_ENABLE_FAST_INTERP */
  8675. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8676. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8677. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8678. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8679. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8680. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8681. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8682. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8683. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8684. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8685. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8686. #define POP_I32() TEMPLATE_POP(I32)
  8687. #define POP_F32() TEMPLATE_POP(F32)
  8688. #define POP_I64() TEMPLATE_POP(I64)
  8689. #define POP_F64() TEMPLATE_POP(F64)
  8690. #define POP_V128() TEMPLATE_POP(V128)
  8691. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8692. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8693. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8694. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8695. #if WASM_ENABLE_FAST_INTERP != 0
  8696. static bool
  8697. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8698. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8699. {
  8700. int16 operand_offset = 0;
  8701. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8702. : loader_ctx->frame_csp;
  8703. BlockType *block_type = &block->block_type;
  8704. uint8 *return_types = NULL;
  8705. #if WASM_ENABLE_GC != 0
  8706. WASMRefTypeMap *reftype_maps = NULL;
  8707. uint32 reftype_map_count;
  8708. #endif
  8709. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8710. int32 i = 0;
  8711. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8712. *frame_offset_org = NULL;
  8713. #if WASM_ENABLE_GC == 0
  8714. return_count = block_type_get_result_types(block_type, &return_types);
  8715. #else
  8716. return_count = block_type_get_result_types(
  8717. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8718. #endif
  8719. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8720. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8721. if (return_count == 1) {
  8722. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8723. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8724. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8725. /* insert op_copy before else opcode */
  8726. if (opcode == WASM_OP_ELSE)
  8727. skip_label();
  8728. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8729. : EXT_OP_COPY_STACK_TOP_I64);
  8730. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8731. emit_operand(loader_ctx, block->dynamic_offset);
  8732. if (opcode == WASM_OP_ELSE) {
  8733. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8734. }
  8735. else {
  8736. loader_ctx->frame_offset -= cell;
  8737. loader_ctx->dynamic_offset = block->dynamic_offset;
  8738. PUSH_OFFSET_TYPE(return_types[0]);
  8739. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8740. }
  8741. if (opcode == WASM_OP_ELSE)
  8742. emit_label(opcode);
  8743. }
  8744. return true;
  8745. }
  8746. /* Copy stack top values to block's results which are in dynamic space.
  8747. * The instruction format:
  8748. * Part a: values count
  8749. * Part b: all values total cell num
  8750. * Part c: each value's cell_num, src offset and dst offset
  8751. * Part d: each value's src offset and dst offset
  8752. * Part e: each value's dst offset
  8753. */
  8754. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8755. dynamic_offset = dynamic_offset_org =
  8756. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8757. /* First traversal to get the count of values needed to be copied. */
  8758. for (i = (int32)return_count - 1; i >= 0; i--) {
  8759. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8760. frame_offset -= cells;
  8761. dynamic_offset -= cells;
  8762. if (dynamic_offset != *frame_offset) {
  8763. value_count++;
  8764. total_cel_num += cells;
  8765. }
  8766. }
  8767. if (value_count) {
  8768. uint32 j = 0;
  8769. uint8 *emit_data = NULL, *cells = NULL;
  8770. int16 *src_offsets = NULL;
  8771. uint16 *dst_offsets = NULL;
  8772. uint64 size =
  8773. (uint64)value_count
  8774. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8775. /* Allocate memory for the emit data */
  8776. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8777. return false;
  8778. cells = emit_data;
  8779. src_offsets = (int16 *)(cells + value_count);
  8780. dst_offsets = (uint16 *)(src_offsets + value_count);
  8781. /* insert op_copy before else opcode */
  8782. if (opcode == WASM_OP_ELSE)
  8783. skip_label();
  8784. emit_label(EXT_OP_COPY_STACK_VALUES);
  8785. /* Part a) */
  8786. emit_uint32(loader_ctx, value_count);
  8787. /* Part b) */
  8788. emit_uint32(loader_ctx, total_cel_num);
  8789. /* Second traversal to get each value's cell num, src offset and dst
  8790. * offset. */
  8791. frame_offset = frame_offset_org;
  8792. dynamic_offset = dynamic_offset_org;
  8793. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8794. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8795. frame_offset -= cell;
  8796. dynamic_offset -= cell;
  8797. if (dynamic_offset != *frame_offset) {
  8798. /* cell num */
  8799. cells[j] = cell;
  8800. /* src offset */
  8801. src_offsets[j] = *frame_offset;
  8802. /* dst offset */
  8803. dst_offsets[j] = dynamic_offset;
  8804. j++;
  8805. }
  8806. if (opcode == WASM_OP_ELSE) {
  8807. *frame_offset = dynamic_offset;
  8808. }
  8809. else {
  8810. loader_ctx->frame_offset = frame_offset;
  8811. loader_ctx->dynamic_offset = dynamic_offset;
  8812. PUSH_OFFSET_TYPE(return_types[i]);
  8813. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8814. loader_ctx->frame_offset = frame_offset_org;
  8815. loader_ctx->dynamic_offset = dynamic_offset_org;
  8816. }
  8817. }
  8818. bh_assert(j == value_count);
  8819. /* Emit the cells, src_offsets and dst_offsets */
  8820. for (j = 0; j < value_count; j++)
  8821. emit_byte(loader_ctx, cells[j]);
  8822. for (j = 0; j < value_count; j++)
  8823. emit_operand(loader_ctx, src_offsets[j]);
  8824. for (j = 0; j < value_count; j++)
  8825. emit_operand(loader_ctx, dst_offsets[j]);
  8826. if (opcode == WASM_OP_ELSE)
  8827. emit_label(opcode);
  8828. wasm_runtime_free(emit_data);
  8829. }
  8830. return true;
  8831. fail:
  8832. return false;
  8833. }
  8834. #endif /* WASM_ENABLE_FAST_INTERP */
  8835. #define RESERVE_BLOCK_RET() \
  8836. do { \
  8837. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  8838. error_buf_size)) \
  8839. goto fail; \
  8840. } while (0)
  8841. #define PUSH_TYPE(type) \
  8842. do { \
  8843. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8844. error_buf_size))) \
  8845. goto fail; \
  8846. } while (0)
  8847. #define POP_TYPE(type) \
  8848. do { \
  8849. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8850. error_buf_size))) \
  8851. goto fail; \
  8852. } while (0)
  8853. #if WASM_ENABLE_GC == 0
  8854. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8855. do { \
  8856. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8857. _start_addr, error_buf, \
  8858. error_buf_size)) \
  8859. goto fail; \
  8860. } while (0)
  8861. #define POP_CSP() \
  8862. do { \
  8863. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8864. goto fail; \
  8865. } while (0)
  8866. #else
  8867. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8868. do { \
  8869. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8870. _start_addr, error_buf, \
  8871. error_buf_size)) \
  8872. goto fail; \
  8873. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8874. error_buf, error_buf_size)) { \
  8875. goto fail; \
  8876. } \
  8877. } while (0)
  8878. #define POP_CSP() \
  8879. do { \
  8880. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8881. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8882. goto fail; \
  8883. } while (0)
  8884. #endif /* end of WASM_ENABLE_GC == 0 */
  8885. #if WASM_ENABLE_GC == 0
  8886. #define GET_LOCAL_REFTYPE() (void)0
  8887. #else
  8888. #define GET_LOCAL_REFTYPE() \
  8889. do { \
  8890. if (wasm_is_type_multi_byte_type(local_type)) { \
  8891. WASMRefType *_ref_type; \
  8892. if (local_idx < param_count) \
  8893. _ref_type = wasm_reftype_map_find( \
  8894. param_reftype_maps, param_reftype_map_count, local_idx); \
  8895. else \
  8896. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8897. local_reftype_map_count, \
  8898. local_idx - param_count); \
  8899. bh_assert(_ref_type); \
  8900. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8901. wasm_reftype_struct_size(_ref_type)); \
  8902. } \
  8903. } while (0)
  8904. #endif /* end of WASM_ENABLE_GC == 0 */
  8905. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8906. do { \
  8907. read_leb_uint32(p, p_end, local_idx); \
  8908. if (local_idx >= param_count + local_count) { \
  8909. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8910. goto fail; \
  8911. } \
  8912. local_type = local_idx < param_count \
  8913. ? param_types[local_idx] \
  8914. : local_types[local_idx - param_count]; \
  8915. local_offset = local_offsets[local_idx]; \
  8916. GET_LOCAL_REFTYPE(); \
  8917. } while (0)
  8918. static bool
  8919. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8920. {
  8921. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8922. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8923. return false;
  8924. }
  8925. return true;
  8926. }
  8927. #define CHECK_MEMORY() \
  8928. do { \
  8929. if (!check_memory(module, error_buf, error_buf_size)) \
  8930. goto fail; \
  8931. } while (0)
  8932. static bool
  8933. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8934. uint32 error_buf_size)
  8935. {
  8936. uint8 mem_access_aligns[] = {
  8937. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8938. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8939. };
  8940. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8941. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8942. set_error_buf(error_buf, error_buf_size,
  8943. "alignment must not be larger than natural");
  8944. return false;
  8945. }
  8946. return true;
  8947. }
  8948. #if WASM_ENABLE_SIMD != 0
  8949. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8950. static bool
  8951. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8952. uint32 error_buf_size)
  8953. {
  8954. uint8 mem_access_aligns[] = {
  8955. 4, /* load */
  8956. 3, 3, 3, 3, 3, 3, /* load and extend */
  8957. 0, 1, 2, 3, /* load and splat */
  8958. 4, /* store */
  8959. };
  8960. uint8 mem_access_aligns_load_lane[] = {
  8961. 0, 1, 2, 3, /* load lane */
  8962. 0, 1, 2, 3, /* store lane */
  8963. 2, 3 /* store zero */
  8964. };
  8965. if (!((opcode <= SIMD_v128_store)
  8966. || (SIMD_v128_load8_lane <= opcode
  8967. && opcode <= SIMD_v128_load64_zero))) {
  8968. set_error_buf(error_buf, error_buf_size,
  8969. "the opcode doesn't include memarg");
  8970. return false;
  8971. }
  8972. if ((opcode <= SIMD_v128_store
  8973. && align > mem_access_aligns[opcode - SIMD_v128_load])
  8974. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  8975. && align > mem_access_aligns_load_lane[opcode
  8976. - SIMD_v128_load8_lane])) {
  8977. set_error_buf(error_buf, error_buf_size,
  8978. "alignment must not be larger than natural");
  8979. return false;
  8980. }
  8981. return true;
  8982. }
  8983. static bool
  8984. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  8985. uint32 error_buf_size)
  8986. {
  8987. switch (opcode) {
  8988. case SIMD_i8x16_extract_lane_s:
  8989. case SIMD_i8x16_extract_lane_u:
  8990. case SIMD_i8x16_replace_lane:
  8991. if (lane >= 16) {
  8992. goto fail;
  8993. }
  8994. break;
  8995. case SIMD_i16x8_extract_lane_s:
  8996. case SIMD_i16x8_extract_lane_u:
  8997. case SIMD_i16x8_replace_lane:
  8998. if (lane >= 8) {
  8999. goto fail;
  9000. }
  9001. break;
  9002. case SIMD_i32x4_extract_lane:
  9003. case SIMD_i32x4_replace_lane:
  9004. case SIMD_f32x4_extract_lane:
  9005. case SIMD_f32x4_replace_lane:
  9006. if (lane >= 4) {
  9007. goto fail;
  9008. }
  9009. break;
  9010. case SIMD_i64x2_extract_lane:
  9011. case SIMD_i64x2_replace_lane:
  9012. case SIMD_f64x2_extract_lane:
  9013. case SIMD_f64x2_replace_lane:
  9014. if (lane >= 2) {
  9015. goto fail;
  9016. }
  9017. break;
  9018. case SIMD_v128_load8_lane:
  9019. case SIMD_v128_load16_lane:
  9020. case SIMD_v128_load32_lane:
  9021. case SIMD_v128_load64_lane:
  9022. case SIMD_v128_store8_lane:
  9023. case SIMD_v128_store16_lane:
  9024. case SIMD_v128_store32_lane:
  9025. case SIMD_v128_store64_lane:
  9026. case SIMD_v128_load32_zero:
  9027. case SIMD_v128_load64_zero:
  9028. {
  9029. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9030. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9031. goto fail;
  9032. }
  9033. break;
  9034. }
  9035. default:
  9036. goto fail;
  9037. }
  9038. return true;
  9039. fail:
  9040. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9041. return false;
  9042. }
  9043. static bool
  9044. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9045. {
  9046. uint8 i;
  9047. for (i = 0; i != 16; ++i) {
  9048. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9049. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9050. return false;
  9051. }
  9052. }
  9053. return true;
  9054. }
  9055. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9056. #endif /* end of WASM_ENABLE_SIMD */
  9057. #if WASM_ENABLE_SHARED_MEMORY != 0
  9058. static bool
  9059. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9060. uint32 error_buf_size)
  9061. {
  9062. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9063. uint8 mem_access_aligns[] = {
  9064. 2, 3, 0, 1, 0, 1, 2,
  9065. };
  9066. uint8 expect;
  9067. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9068. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9069. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9070. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9071. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9072. }
  9073. else {
  9074. /* 7 opcodes in every group */
  9075. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9076. }
  9077. if (align != expect) {
  9078. set_error_buf(error_buf, error_buf_size,
  9079. "alignment isn't equal to natural");
  9080. return false;
  9081. }
  9082. return true;
  9083. }
  9084. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9085. static bool
  9086. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9087. char *error_buf, uint32 error_buf_size)
  9088. {
  9089. BranchBlock *target_block, *cur_block;
  9090. BlockType *target_block_type;
  9091. uint8 type, *types = NULL, *frame_ref;
  9092. uint32 arity = 0;
  9093. int32 i, available_stack_cell;
  9094. uint16 cell_num;
  9095. #if WASM_ENABLE_GC != 0
  9096. WASMRefTypeMap *frame_reftype_map;
  9097. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9098. WASMRefType *ref_type;
  9099. uint32 reftype_map_count = 0;
  9100. int32 available_reftype_map;
  9101. bool is_type_multi_byte;
  9102. #endif
  9103. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9104. uint8 *frame_ref_after_popped = NULL;
  9105. uint8 frame_ref_tmp[4] = { 0 };
  9106. uint8 *frame_ref_buf = frame_ref_tmp;
  9107. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9108. #if WASM_ENABLE_GC != 0
  9109. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9110. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9111. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9112. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9113. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9114. #endif
  9115. #if WASM_ENABLE_FAST_INTERP != 0
  9116. int16 *frame_offset_old = loader_ctx->frame_offset;
  9117. int16 *frame_offset_after_popped = NULL;
  9118. int16 frame_offset_tmp[4] = { 0 };
  9119. int16 *frame_offset_buf = frame_offset_tmp;
  9120. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9121. #endif
  9122. bool ret = false;
  9123. bh_assert(loader_ctx->csp_num > 0);
  9124. if (loader_ctx->csp_num - 1 < depth) {
  9125. set_error_buf(error_buf, error_buf_size,
  9126. "unknown label, "
  9127. "unexpected end of section or function");
  9128. return false;
  9129. }
  9130. cur_block = loader_ctx->frame_csp - 1;
  9131. target_block = loader_ctx->frame_csp - (depth + 1);
  9132. target_block_type = &target_block->block_type;
  9133. frame_ref = loader_ctx->frame_ref;
  9134. #if WASM_ENABLE_GC != 0
  9135. frame_reftype_map = loader_ctx->frame_reftype_map;
  9136. #endif
  9137. /* Note: loop's arity is different from if and block. loop's arity is
  9138. * its parameter count while if and block arity is result count.
  9139. */
  9140. #if WASM_ENABLE_GC == 0
  9141. if (target_block->label_type == LABEL_TYPE_LOOP)
  9142. arity = block_type_get_param_types(target_block_type, &types);
  9143. else
  9144. arity = block_type_get_result_types(target_block_type, &types);
  9145. #else
  9146. if (target_block->label_type == LABEL_TYPE_LOOP)
  9147. arity = block_type_get_param_types(target_block_type, &types,
  9148. &reftype_maps, &reftype_map_count);
  9149. else
  9150. arity = block_type_get_result_types(target_block_type, &types,
  9151. &reftype_maps, &reftype_map_count);
  9152. #endif
  9153. /* If the stack is in polymorphic state, just clear the stack
  9154. * and then re-push the values to make the stack top values
  9155. * match block type. */
  9156. if (cur_block->is_stack_polymorphic) {
  9157. #if WASM_ENABLE_GC != 0
  9158. int32 j = reftype_map_count - 1;
  9159. #endif
  9160. for (i = (int32)arity - 1; i >= 0; i--) {
  9161. #if WASM_ENABLE_GC != 0
  9162. if (wasm_is_type_multi_byte_type(types[i])) {
  9163. bh_assert(reftype_maps[j].index == i);
  9164. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9165. reftype_maps[j].ref_type,
  9166. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9167. j--;
  9168. }
  9169. #endif
  9170. #if WASM_ENABLE_FAST_INTERP != 0
  9171. POP_OFFSET_TYPE(types[i]);
  9172. #endif
  9173. POP_TYPE(types[i]);
  9174. }
  9175. /* Backup stack data since it may be changed in the below
  9176. push operations, and the stack data may be used when
  9177. checking other target blocks of opcode br_table */
  9178. if (opcode == WASM_OP_BR_TABLE) {
  9179. uint64 total_size;
  9180. frame_ref_after_popped = loader_ctx->frame_ref;
  9181. total_size = (uint64)sizeof(uint8)
  9182. * (frame_ref_old - frame_ref_after_popped);
  9183. if (total_size > sizeof(frame_ref_tmp)
  9184. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9185. error_buf_size))) {
  9186. goto fail;
  9187. }
  9188. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9189. frame_ref_after_popped, (uint32)total_size);
  9190. #if WASM_ENABLE_GC != 0
  9191. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9192. total_size =
  9193. (uint64)sizeof(WASMRefTypeMap)
  9194. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9195. if (total_size > sizeof(frame_reftype_map_tmp)
  9196. && !(frame_reftype_map_buf = loader_malloc(
  9197. total_size, error_buf, error_buf_size))) {
  9198. goto fail;
  9199. }
  9200. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9201. frame_reftype_map_after_popped, (uint32)total_size);
  9202. #endif
  9203. #if WASM_ENABLE_FAST_INTERP != 0
  9204. frame_offset_after_popped = loader_ctx->frame_offset;
  9205. total_size = (uint64)sizeof(int16)
  9206. * (frame_offset_old - frame_offset_after_popped);
  9207. if (total_size > sizeof(frame_offset_tmp)
  9208. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9209. error_buf_size))) {
  9210. goto fail;
  9211. }
  9212. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9213. frame_offset_after_popped, (uint32)total_size);
  9214. #endif
  9215. }
  9216. #if WASM_ENABLE_GC != 0
  9217. j = 0;
  9218. #endif
  9219. for (i = 0; i < (int32)arity; i++) {
  9220. #if WASM_ENABLE_GC != 0
  9221. if (wasm_is_type_multi_byte_type(types[i])) {
  9222. bh_assert(reftype_maps[j].index == i);
  9223. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9224. reftype_maps[j].ref_type,
  9225. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9226. j++;
  9227. }
  9228. #endif
  9229. #if WASM_ENABLE_FAST_INTERP != 0
  9230. bool disable_emit = true;
  9231. int16 operand_offset = 0;
  9232. PUSH_OFFSET_TYPE(types[i]);
  9233. #endif
  9234. PUSH_TYPE(types[i]);
  9235. }
  9236. #if WASM_ENABLE_FAST_INTERP != 0
  9237. emit_br_info(target_block, opcode == WASM_OP_BR);
  9238. #endif
  9239. /* Restore the stack data, note that frame_ref_bottom,
  9240. frame_reftype_map_bottom, frame_offset_bottom may be
  9241. re-allocated in the above push operations */
  9242. if (opcode == WASM_OP_BR_TABLE) {
  9243. uint32 total_size;
  9244. /* The stack operand num should not be smaller than before
  9245. after pop and push operations */
  9246. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9247. loader_ctx->stack_cell_num = stack_cell_num_old;
  9248. loader_ctx->frame_ref =
  9249. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9250. total_size = (uint32)(sizeof(uint8)
  9251. * (frame_ref_old - frame_ref_after_popped));
  9252. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9253. frame_ref_buf, total_size);
  9254. #if WASM_ENABLE_GC != 0
  9255. /* The stack operand num should not be smaller than before
  9256. after pop and push operations */
  9257. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9258. loader_ctx->reftype_map_num = reftype_map_num_old;
  9259. loader_ctx->frame_reftype_map =
  9260. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9261. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9262. * (frame_reftype_map_old
  9263. - frame_reftype_map_after_popped));
  9264. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9265. total_size, frame_reftype_map_buf, total_size);
  9266. #endif
  9267. #if WASM_ENABLE_FAST_INTERP != 0
  9268. loader_ctx->frame_offset =
  9269. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9270. total_size =
  9271. (uint32)(sizeof(int16)
  9272. * (frame_offset_old - frame_offset_after_popped));
  9273. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9274. total_size, frame_offset_buf, total_size);
  9275. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9276. #endif
  9277. }
  9278. ret = true;
  9279. goto cleanup_and_return;
  9280. }
  9281. available_stack_cell =
  9282. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9283. #if WASM_ENABLE_GC != 0
  9284. available_reftype_map =
  9285. (int32)(loader_ctx->reftype_map_num
  9286. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9287. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9288. #endif
  9289. /* Check stack top values match target block type */
  9290. for (i = (int32)arity - 1; i >= 0; i--) {
  9291. type = types[i];
  9292. #if WASM_ENABLE_GC != 0
  9293. ref_type = NULL;
  9294. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9295. if (is_type_multi_byte) {
  9296. bh_assert(reftype_map);
  9297. ref_type = reftype_map->ref_type;
  9298. }
  9299. #endif
  9300. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9301. break;
  9302. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9303. #if WASM_ENABLE_GC != 0
  9304. frame_reftype_map, available_reftype_map,
  9305. #endif
  9306. type,
  9307. #if WASM_ENABLE_GC != 0
  9308. ref_type,
  9309. #endif
  9310. error_buf, error_buf_size)) {
  9311. goto fail;
  9312. }
  9313. cell_num = wasm_value_type_cell_num(types[i]);
  9314. frame_ref -= cell_num;
  9315. available_stack_cell -= cell_num;
  9316. #if WASM_ENABLE_GC != 0
  9317. if (is_type_multi_byte) {
  9318. frame_reftype_map--;
  9319. available_reftype_map--;
  9320. reftype_map--;
  9321. }
  9322. #endif
  9323. }
  9324. #if WASM_ENABLE_FAST_INTERP != 0
  9325. emit_br_info(target_block, opcode == WASM_OP_BR);
  9326. #endif
  9327. ret = true;
  9328. cleanup_and_return:
  9329. fail:
  9330. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9331. wasm_runtime_free(frame_ref_buf);
  9332. #if WASM_ENABLE_GC != 0
  9333. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9334. wasm_runtime_free(frame_reftype_map_buf);
  9335. #endif
  9336. #if WASM_ENABLE_FAST_INTERP != 0
  9337. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9338. wasm_runtime_free(frame_offset_buf);
  9339. #endif
  9340. return ret;
  9341. }
  9342. static BranchBlock *
  9343. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9344. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9345. {
  9346. uint8 *p = *p_buf, *p_end = buf_end;
  9347. BranchBlock *frame_csp_tmp;
  9348. uint32 depth;
  9349. read_leb_uint32(p, p_end, depth);
  9350. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9351. error_buf_size)) {
  9352. goto fail;
  9353. }
  9354. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9355. *p_buf = p;
  9356. return frame_csp_tmp;
  9357. fail:
  9358. return NULL;
  9359. }
  9360. #if WASM_ENABLE_EXCE_HANDLING != 0
  9361. static BranchBlock *
  9362. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9363. uint8 *buf_end, char *error_buf,
  9364. uint32 error_buf_size)
  9365. {
  9366. uint8 *p = *p_buf, *p_end = buf_end;
  9367. BranchBlock *frame_csp_tmp;
  9368. uint32 depth;
  9369. read_leb_uint32(p, p_end, depth);
  9370. /*
  9371. * Note: "delegate 0" means the surrounding block, not the
  9372. * try-delegate block itself.
  9373. *
  9374. * Note: the caller hasn't popped the try-delegate frame yet.
  9375. */
  9376. bh_assert(loader_ctx->csp_num > 0);
  9377. if (loader_ctx->csp_num - 1 <= depth) {
  9378. #if WASM_ENABLE_SPEC_TEST == 0
  9379. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9380. #else
  9381. set_error_buf(error_buf, error_buf_size, "unknown label");
  9382. #endif
  9383. goto fail;
  9384. }
  9385. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9386. #if WASM_ENABLE_FAST_INTERP != 0
  9387. emit_br_info(frame_csp_tmp, false);
  9388. #endif
  9389. *p_buf = p;
  9390. return frame_csp_tmp;
  9391. fail:
  9392. return NULL;
  9393. }
  9394. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9395. static bool
  9396. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9397. char *error_buf, uint32 error_buf_size)
  9398. {
  9399. BlockType *block_type = &block->block_type;
  9400. uint8 *return_types = NULL;
  9401. uint32 return_count = 0;
  9402. int32 available_stack_cell, return_cell_num, i;
  9403. uint8 *frame_ref = NULL;
  9404. #if WASM_ENABLE_GC != 0
  9405. WASMRefTypeMap *frame_reftype_map;
  9406. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9407. WASMRefType *ref_type;
  9408. uint32 param_count, return_reftype_map_count = 0;
  9409. int32 available_reftype_map =
  9410. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9411. #endif
  9412. available_stack_cell =
  9413. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9414. #if WASM_ENABLE_GC == 0
  9415. return_count = block_type_get_result_types(block_type, &return_types);
  9416. #else
  9417. return_count = block_type_get_result_types(block_type, &return_types,
  9418. &return_reftype_maps,
  9419. &return_reftype_map_count);
  9420. param_count =
  9421. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9422. (void)param_count;
  9423. #endif
  9424. return_cell_num =
  9425. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9426. /* If the stack is in polymorphic state, just clear the stack
  9427. * and then re-push the values to make the stack top values
  9428. * match block type. */
  9429. if (block->is_stack_polymorphic) {
  9430. #if WASM_ENABLE_GC != 0
  9431. int32 j = return_reftype_map_count - 1;
  9432. #endif
  9433. for (i = (int32)return_count - 1; i >= 0; i--) {
  9434. #if WASM_ENABLE_GC != 0
  9435. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9436. bh_assert(return_reftype_maps[j].index == i + param_count);
  9437. bh_memcpy_s(
  9438. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9439. return_reftype_maps[j].ref_type,
  9440. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9441. j--;
  9442. }
  9443. #endif
  9444. #if WASM_ENABLE_FAST_INTERP != 0
  9445. POP_OFFSET_TYPE(return_types[i]);
  9446. #endif
  9447. POP_TYPE(return_types[i]);
  9448. }
  9449. /* Check stack is empty */
  9450. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9451. set_error_buf(
  9452. error_buf, error_buf_size,
  9453. "type mismatch: stack size does not match block type");
  9454. goto fail;
  9455. }
  9456. #if WASM_ENABLE_GC != 0
  9457. j = 0;
  9458. #endif
  9459. for (i = 0; i < (int32)return_count; i++) {
  9460. #if WASM_ENABLE_GC != 0
  9461. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9462. bh_assert(return_reftype_maps[j].index == i + param_count);
  9463. bh_memcpy_s(
  9464. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9465. return_reftype_maps[j].ref_type,
  9466. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9467. j++;
  9468. }
  9469. #endif
  9470. #if WASM_ENABLE_FAST_INTERP != 0
  9471. bool disable_emit = true;
  9472. int16 operand_offset = 0;
  9473. PUSH_OFFSET_TYPE(return_types[i]);
  9474. #endif
  9475. PUSH_TYPE(return_types[i]);
  9476. }
  9477. return true;
  9478. }
  9479. if (available_stack_cell != return_cell_num) {
  9480. #if WASM_ENABLE_EXCE_HANDLING != 0
  9481. /* testspec: this error message format is expected by try_catch.wast */
  9482. snprintf(
  9483. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9484. block->label_type == LABEL_TYPE_TRY
  9485. || (block->label_type == LABEL_TYPE_CATCH
  9486. && return_cell_num > 0)
  9487. ? "instruction"
  9488. : "block",
  9489. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9490. " but stack has ",
  9491. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9492. : "");
  9493. goto fail;
  9494. #else
  9495. set_error_buf(error_buf, error_buf_size,
  9496. "type mismatch: stack size does not match block type");
  9497. goto fail;
  9498. #endif
  9499. }
  9500. /* Check stack values match return types */
  9501. frame_ref = loader_ctx->frame_ref;
  9502. #if WASM_ENABLE_GC != 0
  9503. frame_reftype_map = loader_ctx->frame_reftype_map;
  9504. return_reftype_map =
  9505. return_reftype_map_count
  9506. ? return_reftype_maps + return_reftype_map_count - 1
  9507. : NULL;
  9508. #endif
  9509. for (i = (int32)return_count - 1; i >= 0; i--) {
  9510. uint8 type = return_types[i];
  9511. #if WASM_ENABLE_GC != 0
  9512. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9513. ref_type = NULL;
  9514. if (is_type_multi_byte) {
  9515. bh_assert(return_reftype_map);
  9516. ref_type = return_reftype_map->ref_type;
  9517. }
  9518. #endif
  9519. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9520. #if WASM_ENABLE_GC != 0
  9521. frame_reftype_map, available_reftype_map,
  9522. #endif
  9523. type,
  9524. #if WASM_ENABLE_GC != 0
  9525. ref_type,
  9526. #endif
  9527. error_buf, error_buf_size))
  9528. return false;
  9529. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9530. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9531. #if WASM_ENABLE_GC != 0
  9532. if (is_type_multi_byte) {
  9533. frame_reftype_map--;
  9534. available_reftype_map--;
  9535. return_reftype_map--;
  9536. }
  9537. #endif
  9538. }
  9539. return true;
  9540. fail:
  9541. return false;
  9542. }
  9543. #if WASM_ENABLE_FAST_INTERP != 0
  9544. /* Copy parameters to dynamic space.
  9545. * 1) POP original parameter out;
  9546. * 2) Push and copy original values to dynamic space.
  9547. * The copy instruction format:
  9548. * Part a: param count
  9549. * Part b: all param total cell num
  9550. * Part c: each param's cell_num, src offset and dst offset
  9551. * Part d: each param's src offset
  9552. * Part e: each param's dst offset
  9553. */
  9554. static bool
  9555. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9556. uint32 error_buf_size)
  9557. {
  9558. bool ret = false;
  9559. int16 *frame_offset = NULL;
  9560. uint8 *cells = NULL, cell;
  9561. int16 *src_offsets = NULL;
  9562. uint8 *emit_data = NULL;
  9563. uint32 i;
  9564. BranchBlock *block = loader_ctx->frame_csp - 1;
  9565. BlockType *block_type = &block->block_type;
  9566. WASMFuncType *wasm_type = block_type->u.type;
  9567. uint32 param_count = block_type->u.type->param_count;
  9568. int16 condition_offset = 0;
  9569. bool disable_emit = false;
  9570. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9571. int16 operand_offset = 0;
  9572. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9573. bh_assert(size > 0);
  9574. /* For if block, we also need copy the condition operand offset. */
  9575. if (is_if_block)
  9576. size += sizeof(*cells) + sizeof(*src_offsets);
  9577. /* Allocate memory for the emit data */
  9578. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9579. return false;
  9580. cells = emit_data;
  9581. src_offsets = (int16 *)(cells + param_count);
  9582. if (is_if_block)
  9583. condition_offset = *loader_ctx->frame_offset;
  9584. /* POP original parameter out */
  9585. for (i = 0; i < param_count; i++) {
  9586. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9587. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9588. }
  9589. frame_offset = loader_ctx->frame_offset;
  9590. /* Get each param's cell num and src offset */
  9591. for (i = 0; i < param_count; i++) {
  9592. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9593. cells[i] = cell;
  9594. src_offsets[i] = *frame_offset;
  9595. frame_offset += cell;
  9596. }
  9597. /* emit copy instruction */
  9598. emit_label(EXT_OP_COPY_STACK_VALUES);
  9599. /* Part a) */
  9600. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9601. /* Part b) */
  9602. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9603. : wasm_type->param_cell_num);
  9604. /* Part c) */
  9605. for (i = 0; i < param_count; i++)
  9606. emit_byte(loader_ctx, cells[i]);
  9607. if (is_if_block)
  9608. emit_byte(loader_ctx, 1);
  9609. /* Part d) */
  9610. for (i = 0; i < param_count; i++)
  9611. emit_operand(loader_ctx, src_offsets[i]);
  9612. if (is_if_block)
  9613. emit_operand(loader_ctx, condition_offset);
  9614. /* Since the start offset to save the block's params and
  9615. * the start offset to save the block's results may be
  9616. * different, we remember the dynamic offset for loop block
  9617. * so that we can use it to copy the stack operands to the
  9618. * loop block's params in wasm_loader_emit_br_info. */
  9619. if (block->label_type == LABEL_TYPE_LOOP)
  9620. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9621. /* Part e) */
  9622. /* Push to dynamic space. The push will emit the dst offset. */
  9623. for (i = 0; i < param_count; i++)
  9624. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9625. if (is_if_block)
  9626. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9627. ret = true;
  9628. fail:
  9629. /* Free the emit data */
  9630. wasm_runtime_free(emit_data);
  9631. return ret;
  9632. }
  9633. #endif
  9634. #if WASM_ENABLE_GC == 0
  9635. #define RESET_REFTYPE_MAP_STACK() (void)0
  9636. #else
  9637. #define RESET_REFTYPE_MAP_STACK() \
  9638. do { \
  9639. loader_ctx->reftype_map_num = \
  9640. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9641. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9642. + loader_ctx->reftype_map_num; \
  9643. } while (0)
  9644. #endif
  9645. /* reset the stack to the state of before entering the last block */
  9646. #if WASM_ENABLE_FAST_INTERP != 0
  9647. #define RESET_STACK() \
  9648. do { \
  9649. loader_ctx->stack_cell_num = \
  9650. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9651. loader_ctx->frame_ref = \
  9652. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9653. loader_ctx->frame_offset = \
  9654. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9655. RESET_REFTYPE_MAP_STACK(); \
  9656. } while (0)
  9657. #else
  9658. #define RESET_STACK() \
  9659. do { \
  9660. loader_ctx->stack_cell_num = \
  9661. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9662. loader_ctx->frame_ref = \
  9663. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9664. RESET_REFTYPE_MAP_STACK(); \
  9665. } while (0)
  9666. #endif
  9667. /* set current block's stack polymorphic state */
  9668. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9669. do { \
  9670. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9671. _cur_block->is_stack_polymorphic = flag; \
  9672. } while (0)
  9673. #define BLOCK_HAS_PARAM(block_type) \
  9674. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9675. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9676. do { \
  9677. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9678. error_buf_size))) { \
  9679. goto fail; \
  9680. } \
  9681. } while (0)
  9682. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9683. static bool
  9684. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9685. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9686. uint32 error_buf_size)
  9687. {
  9688. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9689. return false;
  9690. }
  9691. if (table_idx < module->import_table_count) {
  9692. if (p_elem_type)
  9693. *p_elem_type =
  9694. module->import_tables[table_idx].u.table.table_type.elem_type;
  9695. #if WASM_ENABLE_GC != 0
  9696. if (p_ref_type)
  9697. *((WASMRefType **)p_ref_type) =
  9698. module->import_tables[table_idx]
  9699. .u.table.table_type.elem_ref_type;
  9700. #endif
  9701. }
  9702. else {
  9703. if (p_elem_type)
  9704. *p_elem_type =
  9705. module->tables[module->import_table_count + table_idx]
  9706. .table_type.elem_type;
  9707. #if WASM_ENABLE_GC != 0
  9708. if (p_ref_type)
  9709. *((WASMRefType **)p_ref_type) =
  9710. module->tables[module->import_table_count + table_idx]
  9711. .table_type.elem_ref_type;
  9712. #endif
  9713. }
  9714. return true;
  9715. }
  9716. static bool
  9717. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9718. uint8 *p_elem_type, void **p_elem_ref_type,
  9719. char *error_buf, uint32 error_buf_size)
  9720. {
  9721. if (table_seg_idx >= module->table_seg_count) {
  9722. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9723. table_seg_idx);
  9724. return false;
  9725. }
  9726. if (p_elem_type) {
  9727. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9728. }
  9729. #if WASM_ENABLE_GC != 0
  9730. if (p_elem_ref_type)
  9731. *((WASMRefType **)p_elem_ref_type) =
  9732. module->table_segments[table_seg_idx].elem_ref_type;
  9733. #endif
  9734. return true;
  9735. }
  9736. #endif
  9737. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9738. const uint8 *
  9739. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9740. uint32 *len)
  9741. {
  9742. WASMCustomSection *section = module->custom_section_list;
  9743. while (section) {
  9744. if ((section->name_len == strlen(name))
  9745. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9746. if (len) {
  9747. *len = section->content_len;
  9748. }
  9749. return section->content_addr;
  9750. }
  9751. section = section->next;
  9752. }
  9753. return NULL;
  9754. }
  9755. #endif
  9756. #if 0
  9757. #define HANDLE_OPCODE(opcode) #opcode
  9758. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  9759. #undef HANDLE_OPCODE
  9760. #endif
  9761. static bool
  9762. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9763. uint32 cur_func_idx, char *error_buf,
  9764. uint32 error_buf_size)
  9765. {
  9766. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9767. uint32 param_count, local_count, global_count;
  9768. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type;
  9769. BlockType func_block_type;
  9770. uint16 *local_offsets, local_offset;
  9771. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9772. uint32 table_seg_idx, data_seg_idx, count, align, i;
  9773. mem_offset_t mem_offset;
  9774. int32 i32_const = 0;
  9775. int64 i64_const;
  9776. uint8 opcode;
  9777. bool return_value = false;
  9778. WASMLoaderContext *loader_ctx;
  9779. BranchBlock *frame_csp_tmp;
  9780. #if WASM_ENABLE_GC != 0
  9781. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9782. uint32 param_reftype_map_count, local_reftype_map_count;
  9783. int32 heap_type;
  9784. WASMRefType wasm_ref_type = { 0 };
  9785. bool need_ref_type_map;
  9786. #endif
  9787. #if WASM_ENABLE_FAST_INTERP != 0
  9788. uint8 *func_const_end, *func_const = NULL;
  9789. int16 operand_offset = 0;
  9790. uint8 last_op = 0;
  9791. bool disable_emit, preserve_local = false, if_condition_available = true;
  9792. float32 f32_const;
  9793. float64 f64_const;
  9794. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9795. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9796. #endif
  9797. #if WASM_ENABLE_MEMORY64 != 0
  9798. bool is_memory64 = has_module_memory64(module);
  9799. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  9800. #else
  9801. mem_offset_type = VALUE_TYPE_I32;
  9802. #endif
  9803. uint32 memidx;
  9804. global_count = module->import_global_count + module->global_count;
  9805. param_count = func->func_type->param_count;
  9806. param_types = func->func_type->types;
  9807. func_block_type.is_value_type = false;
  9808. func_block_type.u.type = func->func_type;
  9809. local_count = func->local_count;
  9810. local_types = func->local_types;
  9811. local_offsets = func->local_offsets;
  9812. #if WASM_ENABLE_GC != 0
  9813. param_reftype_maps = func->func_type->ref_type_maps;
  9814. param_reftype_map_count = func->func_type->ref_type_map_count;
  9815. local_reftype_maps = func->local_ref_type_maps;
  9816. local_reftype_map_count = func->local_ref_type_map_count;
  9817. #endif
  9818. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9819. goto fail;
  9820. }
  9821. #if WASM_ENABLE_GC != 0
  9822. loader_ctx->module = module;
  9823. loader_ctx->ref_type_set = module->ref_type_set;
  9824. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9825. #endif
  9826. #if WASM_ENABLE_FAST_INTERP != 0
  9827. /* For the first traverse, the initial value of preserved_local_offset has
  9828. * not been determined, we use the INT16_MAX to represent that a slot has
  9829. * been copied to preserve space. For second traverse, this field will be
  9830. * set to the appropriate value in wasm_loader_ctx_reinit.
  9831. * This is for Issue #1230,
  9832. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9833. * drop opcodes need to know which slots are preserved, so those slots will
  9834. * not be treated as dynamically allocated slots */
  9835. loader_ctx->preserved_local_offset = INT16_MAX;
  9836. re_scan:
  9837. if (loader_ctx->code_compiled_size > 0) {
  9838. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9839. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9840. goto fail;
  9841. }
  9842. p = func->code;
  9843. func->code_compiled = loader_ctx->p_code_compiled;
  9844. func->code_compiled_size = loader_ctx->code_compiled_size;
  9845. }
  9846. #endif
  9847. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9848. while (p < p_end) {
  9849. opcode = *p++;
  9850. #if WASM_ENABLE_FAST_INTERP != 0
  9851. p_org = p;
  9852. disable_emit = false;
  9853. emit_label(opcode);
  9854. #endif
  9855. switch (opcode) {
  9856. case WASM_OP_UNREACHABLE:
  9857. RESET_STACK();
  9858. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9859. break;
  9860. case WASM_OP_NOP:
  9861. #if WASM_ENABLE_FAST_INTERP != 0
  9862. skip_label();
  9863. #endif
  9864. break;
  9865. case WASM_OP_IF:
  9866. {
  9867. #if WASM_ENABLE_FAST_INTERP != 0
  9868. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9869. int32 available_stack_cell =
  9870. (int32)(loader_ctx->stack_cell_num
  9871. - parent_block->stack_cell_num);
  9872. if (available_stack_cell <= 0
  9873. && parent_block->is_stack_polymorphic)
  9874. if_condition_available = false;
  9875. else
  9876. if_condition_available = true;
  9877. PRESERVE_LOCAL_FOR_BLOCK();
  9878. #endif
  9879. #if WASM_ENABLE_GC == 0
  9880. POP_I32();
  9881. #endif
  9882. goto handle_op_block_and_loop;
  9883. }
  9884. case WASM_OP_BLOCK:
  9885. case WASM_OP_LOOP:
  9886. #if WASM_ENABLE_EXCE_HANDLING != 0
  9887. case WASM_OP_TRY:
  9888. if (opcode == WASM_OP_TRY) {
  9889. /*
  9890. * keep track of exception handlers to account for
  9891. * memory allocation
  9892. */
  9893. func->exception_handler_count++;
  9894. /*
  9895. * try is a block
  9896. * do nothing special, but execution continues to
  9897. * to handle_op_block_and_loop,
  9898. * and that be pushes the csp
  9899. */
  9900. }
  9901. #endif
  9902. #if WASM_ENABLE_FAST_INTERP != 0
  9903. PRESERVE_LOCAL_FOR_BLOCK();
  9904. #endif
  9905. handle_op_block_and_loop:
  9906. {
  9907. uint8 value_type;
  9908. BlockType block_type;
  9909. #if WASM_ENABLE_FAST_INTERP != 0
  9910. uint32 available_params = 0;
  9911. #endif
  9912. CHECK_BUF(p, p_end, 1);
  9913. value_type = read_uint8(p);
  9914. if (is_byte_a_type(value_type)) {
  9915. /* If the first byte is one of these special values:
  9916. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9917. * the single return value. */
  9918. block_type.is_value_type = true;
  9919. block_type.u.value_type.type = value_type;
  9920. #if WASM_ENABLE_WAMR_COMPILER != 0
  9921. if (value_type == VALUE_TYPE_V128)
  9922. module->is_simd_used = true;
  9923. else if (value_type == VALUE_TYPE_FUNCREF
  9924. || value_type == VALUE_TYPE_EXTERNREF)
  9925. module->is_ref_types_used = true;
  9926. #endif
  9927. #if WASM_ENABLE_GC != 0
  9928. if (value_type != VALUE_TYPE_VOID) {
  9929. p_org = p;
  9930. p--;
  9931. if (!resolve_value_type((const uint8 **)&p, p_end,
  9932. module, module->type_count,
  9933. &need_ref_type_map,
  9934. &wasm_ref_type, false,
  9935. error_buf, error_buf_size)) {
  9936. goto fail;
  9937. }
  9938. if (need_ref_type_map) {
  9939. block_type.u.value_type.ref_type_map.index = 0;
  9940. if (!(block_type.u.value_type.ref_type_map
  9941. .ref_type = reftype_set_insert(
  9942. module->ref_type_set, &wasm_ref_type,
  9943. error_buf, error_buf_size))) {
  9944. goto fail;
  9945. }
  9946. }
  9947. /* Set again as the type might be changed, e.g.
  9948. (ref null any) to anyref */
  9949. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9950. #if WASM_ENABLE_FAST_INTERP == 0
  9951. while (p_org < p) {
  9952. #if WASM_ENABLE_DEBUG_INTERP != 0
  9953. if (!record_fast_op(module, p_org, *p_org,
  9954. error_buf, error_buf_size)) {
  9955. goto fail;
  9956. }
  9957. #endif
  9958. /* Ignore extra bytes for interpreter */
  9959. *p_org++ = WASM_OP_NOP;
  9960. }
  9961. #endif
  9962. }
  9963. #endif /* end of WASM_ENABLE_GC != 0 */
  9964. }
  9965. else {
  9966. int32 type_index;
  9967. /* Resolve the leb128 encoded type index as block type */
  9968. p--;
  9969. p_org = p - 1;
  9970. read_leb_int32(p, p_end, type_index);
  9971. if ((uint32)type_index >= module->type_count) {
  9972. set_error_buf(error_buf, error_buf_size,
  9973. "unknown type");
  9974. goto fail;
  9975. }
  9976. block_type.is_value_type = false;
  9977. block_type.u.type =
  9978. (WASMFuncType *)module->types[type_index];
  9979. #if WASM_ENABLE_FAST_INTERP == 0
  9980. /* If block use type index as block type, change the opcode
  9981. * to new extended opcode so that interpreter can resolve
  9982. * the block quickly.
  9983. */
  9984. #if WASM_ENABLE_DEBUG_INTERP != 0
  9985. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9986. error_buf_size)) {
  9987. goto fail;
  9988. }
  9989. #endif
  9990. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  9991. #endif
  9992. }
  9993. #if WASM_ENABLE_GC != 0
  9994. if (opcode == WASM_OP_IF) {
  9995. POP_I32();
  9996. }
  9997. #endif
  9998. /* Pop block parameters from stack */
  9999. if (BLOCK_HAS_PARAM(block_type)) {
  10000. WASMFuncType *wasm_type = block_type.u.type;
  10001. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10002. #if WASM_ENABLE_FAST_INTERP != 0
  10003. uint32 cell_num;
  10004. available_params = block_type.u.type->param_count;
  10005. #endif
  10006. for (i = 0; i < block_type.u.type->param_count; i++) {
  10007. int32 available_stack_cell =
  10008. (int32)(loader_ctx->stack_cell_num
  10009. - cur_block->stack_cell_num);
  10010. if (available_stack_cell <= 0
  10011. && cur_block->is_stack_polymorphic) {
  10012. #if WASM_ENABLE_FAST_INTERP != 0
  10013. available_params = i;
  10014. #endif
  10015. break;
  10016. }
  10017. POP_TYPE(
  10018. wasm_type->types[wasm_type->param_count - i - 1]);
  10019. #if WASM_ENABLE_FAST_INTERP != 0
  10020. /* decrease the frame_offset pointer accordingly to keep
  10021. * consistent with frame_ref stack */
  10022. cell_num = wasm_value_type_cell_num(
  10023. wasm_type->types[wasm_type->param_count - i - 1]);
  10024. loader_ctx->frame_offset -= cell_num;
  10025. #endif
  10026. }
  10027. }
  10028. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10029. block_type, p);
  10030. /* Pass parameters to block */
  10031. if (BLOCK_HAS_PARAM(block_type)) {
  10032. WASMFuncType *func_type = block_type.u.type;
  10033. #if WASM_ENABLE_GC != 0
  10034. WASMRefType *ref_type;
  10035. uint32 j = 0;
  10036. #endif
  10037. for (i = 0; i < func_type->param_count; i++) {
  10038. #if WASM_ENABLE_FAST_INTERP != 0
  10039. uint32 cell_num =
  10040. wasm_value_type_cell_num(func_type->types[i]);
  10041. if (i >= available_params) {
  10042. /* If there isn't enough data on stack, push a dummy
  10043. * offset to keep the stack consistent with
  10044. * frame_ref.
  10045. * Since the stack is already in polymorphic state,
  10046. * the opcode will not be executed, so the dummy
  10047. * offset won't cause any error */
  10048. uint32 n;
  10049. for (n = 0; n < cell_num; n++) {
  10050. if (loader_ctx->p_code_compiled == NULL) {
  10051. if (!check_offset_push(loader_ctx,
  10052. error_buf,
  10053. error_buf_size))
  10054. goto fail;
  10055. }
  10056. *loader_ctx->frame_offset++ = 0;
  10057. }
  10058. }
  10059. else {
  10060. loader_ctx->frame_offset += cell_num;
  10061. }
  10062. #endif
  10063. #if WASM_ENABLE_GC != 0
  10064. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10065. bh_assert(func_type->ref_type_maps[j].index == i);
  10066. ref_type = func_type->ref_type_maps[j].ref_type;
  10067. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10068. ref_type,
  10069. wasm_reftype_struct_size(ref_type));
  10070. j++;
  10071. }
  10072. #endif
  10073. PUSH_TYPE(func_type->types[i]);
  10074. }
  10075. }
  10076. #if WASM_ENABLE_FAST_INTERP != 0
  10077. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10078. skip_label();
  10079. if (BLOCK_HAS_PARAM(block_type)) {
  10080. /* Make sure params are in dynamic space */
  10081. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10082. error_buf_size))
  10083. goto fail;
  10084. }
  10085. if (opcode == WASM_OP_LOOP) {
  10086. (loader_ctx->frame_csp - 1)->code_compiled =
  10087. loader_ctx->p_code_compiled;
  10088. }
  10089. }
  10090. #if WASM_ENABLE_EXCE_HANDLING != 0
  10091. else if (opcode == WASM_OP_TRY) {
  10092. skip_label();
  10093. }
  10094. #endif
  10095. else if (opcode == WASM_OP_IF) {
  10096. BranchBlock *block = loader_ctx->frame_csp - 1;
  10097. /* If block has parameters, we should make sure they are in
  10098. * dynamic space. Otherwise, when else branch is missing,
  10099. * the later opcode may consume incorrect operand offset.
  10100. * Spec case:
  10101. * (func (export "params-id") (param i32) (result i32)
  10102. * (i32.const 1)
  10103. * (i32.const 2)
  10104. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10105. * (then)) (i32.add)
  10106. * )
  10107. *
  10108. * So we should emit a copy instruction before the if.
  10109. *
  10110. * And we also need to save the parameter offsets and
  10111. * recover them before entering else branch.
  10112. *
  10113. */
  10114. if (BLOCK_HAS_PARAM(block_type)) {
  10115. uint64 size;
  10116. /* In polymorphic state, there may be no if condition on
  10117. * the stack, so the offset may not emitted */
  10118. if (if_condition_available) {
  10119. /* skip the if condition operand offset */
  10120. wasm_loader_emit_backspace(loader_ctx,
  10121. sizeof(int16));
  10122. }
  10123. /* skip the if label */
  10124. skip_label();
  10125. /* Emit a copy instruction */
  10126. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10127. error_buf_size))
  10128. goto fail;
  10129. /* Emit the if instruction */
  10130. emit_label(opcode);
  10131. /* Emit the new condition operand offset */
  10132. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10133. /* Save top param_count values of frame_offset stack, so
  10134. * that we can recover it before executing else branch
  10135. */
  10136. size = sizeof(int16)
  10137. * (uint64)block_type.u.type->param_cell_num;
  10138. if (!(block->param_frame_offsets = loader_malloc(
  10139. size, error_buf, error_buf_size)))
  10140. goto fail;
  10141. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10142. loader_ctx->frame_offset
  10143. - size / sizeof(int16),
  10144. (uint32)size);
  10145. }
  10146. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10147. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10148. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10149. }
  10150. #endif
  10151. break;
  10152. }
  10153. #if WASM_ENABLE_EXCE_HANDLING != 0
  10154. case WASM_OP_THROW:
  10155. {
  10156. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10157. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10158. uint8 label_type = cur_block->label_type;
  10159. uint32 tag_index = 0;
  10160. read_leb_int32(p, p_end, tag_index);
  10161. /* check validity of tag_index against module->tag_count */
  10162. /* check tag index is within the tag index space */
  10163. if (tag_index >= module->import_tag_count + module->tag_count) {
  10164. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10165. tag_index);
  10166. goto fail;
  10167. }
  10168. /* the tag_type is stored in either the WASMTag (section tags)
  10169. * or WASMTagImport (import tag) */
  10170. WASMFuncType *tag_type = NULL;
  10171. if (tag_index < module->import_tag_count) {
  10172. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10173. }
  10174. else {
  10175. tag_type =
  10176. module->tags[tag_index - module->import_tag_count]
  10177. ->tag_type;
  10178. }
  10179. if (tag_type->result_count != 0) {
  10180. set_error_buf(error_buf, error_buf_size,
  10181. "tag type signature does not return void");
  10182. goto fail;
  10183. }
  10184. int32 available_stack_cell =
  10185. (int32)(loader_ctx->stack_cell_num
  10186. - cur_block->stack_cell_num);
  10187. int32 tti;
  10188. /* Check stack values match return types by comparing tag param
  10189. * types with stack cells */
  10190. uint8 *frame_ref = loader_ctx->frame_ref;
  10191. #if WASM_ENABLE_GC != 0
  10192. WASMRefTypeMap *frame_reftype_map =
  10193. loader_ctx->frame_reftype_map;
  10194. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10195. /* Temporarily set these values since they may be used in
  10196. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10197. param_reftype_maps = tag_type->ref_type_maps;
  10198. /* For tag_type function, it shouldn't have result_count = 0 */
  10199. param_reftype_map_count = tag_type->ref_type_map_count;
  10200. param_count = tag_type->param_count;
  10201. #endif
  10202. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10203. #if WASM_ENABLE_GC != 0
  10204. local_type = tag_type->types[tti];
  10205. local_idx = tti;
  10206. /* Get the wasm_ref_type if the local_type is multibyte
  10207. type */
  10208. GET_LOCAL_REFTYPE();
  10209. #endif
  10210. if (!check_stack_top_values(
  10211. loader_ctx, frame_ref, available_stack_cell,
  10212. #if WASM_ENABLE_GC != 0
  10213. frame_reftype_map, frame_reftype_map_num,
  10214. #endif
  10215. tag_type->types[tti],
  10216. #if WASM_ENABLE_GC != 0
  10217. &wasm_ref_type,
  10218. #endif
  10219. error_buf, error_buf_size)) {
  10220. snprintf(error_buf, error_buf_size,
  10221. "type mismatch: instruction requires [%s] but "
  10222. "stack has [%s]",
  10223. tag_type->param_count > 0
  10224. ? type2str(tag_type->types[tti])
  10225. : "",
  10226. available_stack_cell > 0
  10227. ? type2str(*(loader_ctx->frame_ref - 1))
  10228. : "");
  10229. goto fail;
  10230. }
  10231. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10232. available_stack_cell -=
  10233. wasm_value_type_cell_num(tag_type->types[tti]);
  10234. }
  10235. #if WASM_ENABLE_GC != 0
  10236. /* Restore the values */
  10237. param_reftype_maps = func->func_type->ref_type_maps;
  10238. param_reftype_map_count = func->func_type->ref_type_map_count;
  10239. param_count = func->func_type->param_count;
  10240. #endif
  10241. /* throw is stack polymorphic */
  10242. (void)label_type;
  10243. RESET_STACK();
  10244. break;
  10245. }
  10246. case WASM_OP_RETHROW:
  10247. {
  10248. /* must be done before checking branch block */
  10249. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10250. /* check the target catching block: LABEL_TYPE_CATCH */
  10251. if (!(frame_csp_tmp =
  10252. check_branch_block(loader_ctx, &p, p_end, opcode,
  10253. error_buf, error_buf_size)))
  10254. goto fail;
  10255. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10256. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10257. /* trap according to spectest (rethrow.wast) */
  10258. set_error_buf(error_buf, error_buf_size,
  10259. "invalid rethrow label");
  10260. goto fail;
  10261. }
  10262. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10263. uint8 label_type = cur_block->label_type;
  10264. (void)label_type;
  10265. /* rethrow is stack polymorphic */
  10266. RESET_STACK();
  10267. break;
  10268. }
  10269. case WASM_OP_DELEGATE:
  10270. {
  10271. /* check target block is valid */
  10272. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10273. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10274. goto fail;
  10275. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10276. uint8 label_type = cur_block->label_type;
  10277. (void)label_type;
  10278. /* DELEGATE ends the block */
  10279. POP_CSP();
  10280. break;
  10281. }
  10282. case WASM_OP_CATCH:
  10283. {
  10284. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10285. uint8 label_type = cur_block->label_type;
  10286. uint32 tag_index = 0;
  10287. read_leb_int32(p, p_end, tag_index);
  10288. /* check validity of tag_index against module->tag_count */
  10289. /* check tag index is within the tag index space */
  10290. if (tag_index >= module->import_tag_count + module->tag_count) {
  10291. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10292. __FUNCTION__);
  10293. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10294. goto fail;
  10295. }
  10296. /* the tag_type is stored in either the WASMTag (section tags)
  10297. * or WASMTagImport (import tag) */
  10298. WASMFuncType *func_type = NULL;
  10299. if (tag_index < module->import_tag_count) {
  10300. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10301. }
  10302. else {
  10303. func_type =
  10304. module->tags[tag_index - module->import_tag_count]
  10305. ->tag_type;
  10306. }
  10307. if (func_type->result_count != 0) {
  10308. set_error_buf(error_buf, error_buf_size,
  10309. "tag type signature does not return void");
  10310. goto fail;
  10311. }
  10312. /* check validity of current label (expect LABEL_TYPE_TRY or
  10313. * LABEL_TYPE_CATCH) */
  10314. if ((LABEL_TYPE_CATCH != label_type)
  10315. && (LABEL_TYPE_TRY != label_type)) {
  10316. set_error_buf(error_buf, error_buf_size,
  10317. "Unexpected block sequence encountered.");
  10318. goto fail;
  10319. }
  10320. /*
  10321. * replace frame_csp by LABEL_TYPE_CATCH
  10322. */
  10323. cur_block->label_type = LABEL_TYPE_CATCH;
  10324. /* RESET_STACK removes the values pushed in TRY or pervious
  10325. * CATCH Blocks */
  10326. RESET_STACK();
  10327. #if WASM_ENABLE_GC != 0
  10328. WASMRefType *ref_type;
  10329. uint32 j = 0;
  10330. #endif
  10331. /* push types on the stack according to caught type */
  10332. for (i = 0; i < func_type->param_count; i++) {
  10333. #if WASM_ENABLE_GC != 0
  10334. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10335. bh_assert(func_type->ref_type_maps[j].index == i);
  10336. ref_type = func_type->ref_type_maps[j].ref_type;
  10337. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10338. ref_type,
  10339. wasm_reftype_struct_size(ref_type));
  10340. j++;
  10341. }
  10342. #endif
  10343. PUSH_TYPE(func_type->types[i]);
  10344. }
  10345. break;
  10346. }
  10347. case WASM_OP_CATCH_ALL:
  10348. {
  10349. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10350. /* expecting a TRY or CATCH, anything else will be considered an
  10351. * error */
  10352. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10353. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10354. set_error_buf(error_buf, error_buf_size,
  10355. "Unexpected block sequence encountered.");
  10356. goto fail;
  10357. }
  10358. /* no immediates */
  10359. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10360. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10361. /* RESET_STACK removes the values pushed in TRY or pervious
  10362. * CATCH Blocks */
  10363. RESET_STACK();
  10364. /* catch_all has no tagtype and therefore no parameters */
  10365. break;
  10366. }
  10367. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10368. case WASM_OP_ELSE:
  10369. handle_op_else:
  10370. {
  10371. BranchBlock *block = NULL;
  10372. BlockType block_type;
  10373. if (loader_ctx->csp_num < 2
  10374. /* the matched if isn't found */
  10375. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10376. /* duplicated else is found */
  10377. || (loader_ctx->frame_csp - 1)->else_addr) {
  10378. set_error_buf(
  10379. error_buf, error_buf_size,
  10380. "opcode else found without matched opcode if");
  10381. goto fail;
  10382. }
  10383. block = loader_ctx->frame_csp - 1;
  10384. /* check whether if branch's stack matches its result type */
  10385. if (!check_block_stack(loader_ctx, block, error_buf,
  10386. error_buf_size))
  10387. goto fail;
  10388. block->else_addr = p - 1;
  10389. block_type = block->block_type;
  10390. #if WASM_ENABLE_GC != 0
  10391. if (!wasm_loader_init_local_use_masks(
  10392. loader_ctx, local_count, error_buf, error_buf_size)) {
  10393. goto fail;
  10394. }
  10395. #endif
  10396. #if WASM_ENABLE_FAST_INTERP != 0
  10397. /* if the result of if branch is in local or const area, add a
  10398. * copy op */
  10399. RESERVE_BLOCK_RET();
  10400. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10401. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10402. #endif
  10403. RESET_STACK();
  10404. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10405. /* Pass parameters to if-false branch */
  10406. if (BLOCK_HAS_PARAM(block_type)) {
  10407. for (i = 0; i < block_type.u.type->param_count; i++)
  10408. PUSH_TYPE(block_type.u.type->types[i]);
  10409. }
  10410. #if WASM_ENABLE_FAST_INTERP != 0
  10411. /* Recover top param_count values of frame_offset stack */
  10412. if (BLOCK_HAS_PARAM((block_type))) {
  10413. uint32 size;
  10414. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10415. bh_memcpy_s(loader_ctx->frame_offset, size,
  10416. block->param_frame_offsets, size);
  10417. loader_ctx->frame_offset += (size / sizeof(int16));
  10418. }
  10419. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10420. #endif
  10421. break;
  10422. }
  10423. case WASM_OP_END:
  10424. {
  10425. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10426. /* check whether block stack matches its result type */
  10427. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10428. error_buf_size))
  10429. goto fail;
  10430. /* if there is no else branch, make a virtual else opcode for
  10431. easier integrity check and to copy the correct results to
  10432. the block return address for fast-interp mode:
  10433. change if block from `if ... end` to `if ... else end` */
  10434. if (cur_block->label_type == LABEL_TYPE_IF
  10435. && !cur_block->else_addr) {
  10436. opcode = WASM_OP_ELSE;
  10437. p--;
  10438. #if WASM_ENABLE_FAST_INTERP != 0
  10439. p_org = p;
  10440. skip_label();
  10441. disable_emit = false;
  10442. emit_label(opcode);
  10443. #endif
  10444. goto handle_op_else;
  10445. }
  10446. POP_CSP();
  10447. #if WASM_ENABLE_FAST_INTERP != 0
  10448. skip_label();
  10449. /* copy the result to the block return address */
  10450. RESERVE_BLOCK_RET();
  10451. apply_label_patch(loader_ctx, 0, PATCH_END);
  10452. free_label_patch_list(loader_ctx->frame_csp);
  10453. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10454. int32 idx;
  10455. uint8 ret_type;
  10456. emit_label(WASM_OP_RETURN);
  10457. for (idx = (int32)func->func_type->result_count - 1;
  10458. idx >= 0; idx--) {
  10459. ret_type = *(func->func_type->types
  10460. + func->func_type->param_count + idx);
  10461. POP_OFFSET_TYPE(ret_type);
  10462. }
  10463. }
  10464. #endif
  10465. if (loader_ctx->csp_num > 0) {
  10466. loader_ctx->frame_csp->end_addr = p - 1;
  10467. }
  10468. else {
  10469. /* end of function block, function will return */
  10470. if (p < p_end) {
  10471. set_error_buf(error_buf, error_buf_size,
  10472. "section size mismatch");
  10473. goto fail;
  10474. }
  10475. }
  10476. break;
  10477. }
  10478. case WASM_OP_BR:
  10479. {
  10480. if (!(frame_csp_tmp =
  10481. check_branch_block(loader_ctx, &p, p_end, opcode,
  10482. error_buf, error_buf_size)))
  10483. goto fail;
  10484. RESET_STACK();
  10485. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10486. break;
  10487. }
  10488. case WASM_OP_BR_IF:
  10489. {
  10490. POP_I32();
  10491. if (!(frame_csp_tmp =
  10492. check_branch_block(loader_ctx, &p, p_end, opcode,
  10493. error_buf, error_buf_size)))
  10494. goto fail;
  10495. break;
  10496. }
  10497. case WASM_OP_BR_TABLE:
  10498. {
  10499. uint32 depth = 0, default_arity, arity = 0;
  10500. BranchBlock *target_block;
  10501. BlockType *target_block_type;
  10502. #if WASM_ENABLE_FAST_INTERP == 0
  10503. BrTableCache *br_table_cache = NULL;
  10504. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10505. uint32 j;
  10506. #endif
  10507. read_leb_uint32(p, p_end, count);
  10508. #if WASM_ENABLE_FAST_INTERP != 0
  10509. emit_uint32(loader_ctx, count);
  10510. #endif
  10511. POP_I32();
  10512. /* Get each depth and check it */
  10513. p_org = p;
  10514. for (i = 0; i <= count; i++) {
  10515. read_leb_uint32(p, p_end, depth);
  10516. bh_assert(loader_ctx->csp_num > 0);
  10517. if (loader_ctx->csp_num - 1 < depth) {
  10518. set_error_buf(error_buf, error_buf_size,
  10519. "unknown label, "
  10520. "unexpected end of section or function");
  10521. goto fail;
  10522. }
  10523. }
  10524. p = p_org;
  10525. /* Get the default block's arity */
  10526. target_block = loader_ctx->frame_csp - (depth + 1);
  10527. target_block_type = &target_block->block_type;
  10528. default_arity = block_type_get_arity(target_block_type,
  10529. target_block->label_type);
  10530. #if WASM_ENABLE_FAST_INTERP == 0
  10531. p_depth_begin = p_depth = p;
  10532. #endif
  10533. for (i = 0; i <= count; i++) {
  10534. p_org = p;
  10535. read_leb_uint32(p, p_end, depth);
  10536. p = p_org;
  10537. /* Get the target block's arity and check it */
  10538. target_block = loader_ctx->frame_csp - (depth + 1);
  10539. target_block_type = &target_block->block_type;
  10540. arity = block_type_get_arity(target_block_type,
  10541. target_block->label_type);
  10542. if (arity != default_arity) {
  10543. set_error_buf(error_buf, error_buf_size,
  10544. "type mismatch: br_table targets must "
  10545. "all use same result type");
  10546. goto fail;
  10547. }
  10548. if (!(frame_csp_tmp =
  10549. check_branch_block(loader_ctx, &p, p_end, opcode,
  10550. error_buf, error_buf_size))) {
  10551. goto fail;
  10552. }
  10553. #if WASM_ENABLE_FAST_INTERP == 0
  10554. if (br_table_cache) {
  10555. br_table_cache->br_depths[i] = depth;
  10556. }
  10557. else {
  10558. if (depth > 255) {
  10559. /* The depth cannot be stored in one byte,
  10560. create br_table cache to store each depth */
  10561. #if WASM_ENABLE_DEBUG_INTERP != 0
  10562. if (!record_fast_op(module, p_opcode, *p_opcode,
  10563. error_buf, error_buf_size)) {
  10564. goto fail;
  10565. }
  10566. #endif
  10567. if (!(br_table_cache = loader_malloc(
  10568. offsetof(BrTableCache, br_depths)
  10569. + sizeof(uint32)
  10570. * (uint64)(count + 1),
  10571. error_buf, error_buf_size))) {
  10572. goto fail;
  10573. }
  10574. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10575. br_table_cache->br_table_op_addr = p_opcode;
  10576. br_table_cache->br_count = count;
  10577. /* Copy previous depths which are one byte */
  10578. for (j = 0; j < i; j++) {
  10579. br_table_cache->br_depths[j] = p_depth_begin[j];
  10580. }
  10581. br_table_cache->br_depths[i] = depth;
  10582. bh_list_insert(module->br_table_cache_list,
  10583. br_table_cache);
  10584. }
  10585. else {
  10586. /* The depth can be stored in one byte, use the
  10587. byte of the leb to store it */
  10588. *p_depth++ = (uint8)depth;
  10589. }
  10590. }
  10591. #endif
  10592. }
  10593. #if WASM_ENABLE_FAST_INTERP == 0
  10594. /* Set the tailing bytes to nop */
  10595. if (br_table_cache)
  10596. p_depth = p_depth_begin;
  10597. while (p_depth < p)
  10598. *p_depth++ = WASM_OP_NOP;
  10599. #endif
  10600. RESET_STACK();
  10601. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10602. break;
  10603. }
  10604. case WASM_OP_RETURN:
  10605. {
  10606. WASMFuncType *func_type = func->func_type;
  10607. int32 idx;
  10608. uint8 ret_type;
  10609. #if WASM_ENABLE_GC != 0
  10610. uint32 j = func_type->ref_type_map_count - 1;
  10611. #endif
  10612. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  10613. idx--) {
  10614. ret_type =
  10615. *(func_type->types + func_type->param_count + idx);
  10616. #if WASM_ENABLE_GC != 0
  10617. if (wasm_is_type_multi_byte_type(ret_type)) {
  10618. WASMRefType *ref_type =
  10619. func_type->ref_type_maps[j].ref_type;
  10620. bh_assert(func_type->ref_type_maps[j].index
  10621. == func_type->param_count + idx);
  10622. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10623. ref_type,
  10624. wasm_reftype_struct_size(ref_type));
  10625. j--;
  10626. }
  10627. #endif
  10628. #if WASM_ENABLE_FAST_INTERP != 0
  10629. /* emit the offset after return opcode */
  10630. POP_OFFSET_TYPE(ret_type);
  10631. #endif
  10632. POP_TYPE(ret_type);
  10633. }
  10634. RESET_STACK();
  10635. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10636. break;
  10637. }
  10638. case WASM_OP_CALL:
  10639. #if WASM_ENABLE_TAIL_CALL != 0
  10640. case WASM_OP_RETURN_CALL:
  10641. #endif
  10642. #if WASM_ENABLE_GC != 0
  10643. case WASM_OP_CALL_REF:
  10644. case WASM_OP_RETURN_CALL_REF:
  10645. #endif
  10646. {
  10647. WASMFuncType *func_type;
  10648. uint8 type;
  10649. int32 idx;
  10650. #if WASM_ENABLE_GC != 0
  10651. WASMRefType *ref_type;
  10652. uint32 type_idx1;
  10653. int32 j;
  10654. #endif
  10655. #if WASM_ENABLE_GC != 0
  10656. if (opcode == WASM_OP_CALL_REF
  10657. || opcode == WASM_OP_RETURN_CALL_REF) {
  10658. read_leb_uint32(p, p_end, type_idx1);
  10659. if (!check_type_index(module, module->type_count, type_idx1,
  10660. error_buf, error_buf_size)) {
  10661. goto fail;
  10662. }
  10663. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  10664. set_error_buf(error_buf, error_buf_size,
  10665. "unkown function type");
  10666. goto fail;
  10667. }
  10668. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10669. &type_idx, error_buf,
  10670. error_buf_size)) {
  10671. goto fail;
  10672. }
  10673. if (type == VALUE_TYPE_ANY) {
  10674. type_idx = type_idx1;
  10675. }
  10676. if (!check_type_index(module, module->type_count, type_idx,
  10677. error_buf, error_buf_size)) {
  10678. goto fail;
  10679. }
  10680. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10681. set_error_buf(error_buf, error_buf_size,
  10682. "unkown function type");
  10683. goto fail;
  10684. }
  10685. if (!wasm_func_type_is_super_of(
  10686. (WASMFuncType *)module->types[type_idx1],
  10687. (WASMFuncType *)module->types[type_idx])) {
  10688. set_error_buf(error_buf, error_buf_size,
  10689. "function type mismatch");
  10690. goto fail;
  10691. }
  10692. func_type = (WASMFuncType *)module->types[type_idx];
  10693. }
  10694. else
  10695. #endif
  10696. {
  10697. read_leb_uint32(p, p_end, func_idx);
  10698. #if WASM_ENABLE_FAST_INTERP != 0
  10699. /* we need to emit func_idx before arguments */
  10700. emit_uint32(loader_ctx, func_idx);
  10701. #endif
  10702. if (!check_function_index(module, func_idx, error_buf,
  10703. error_buf_size)) {
  10704. goto fail;
  10705. }
  10706. if (func_idx < module->import_function_count)
  10707. func_type = module->import_functions[func_idx]
  10708. .u.function.func_type;
  10709. else
  10710. func_type =
  10711. module
  10712. ->functions[func_idx
  10713. - module->import_function_count]
  10714. ->func_type;
  10715. }
  10716. if (func_type->param_count > 0) {
  10717. #if WASM_ENABLE_GC != 0
  10718. j = (int32)(func_type->result_ref_type_maps
  10719. - func_type->ref_type_maps - 1);
  10720. #endif
  10721. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10722. idx--) {
  10723. #if WASM_ENABLE_GC != 0
  10724. if (wasm_is_type_multi_byte_type(
  10725. func_type->types[idx])) {
  10726. ref_type = func_type->ref_type_maps[j].ref_type;
  10727. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10728. ref_type,
  10729. wasm_reftype_struct_size(ref_type));
  10730. j--;
  10731. }
  10732. #endif
  10733. #if WASM_ENABLE_FAST_INTERP != 0
  10734. POP_OFFSET_TYPE(func_type->types[idx]);
  10735. #endif
  10736. POP_TYPE(func_type->types[idx]);
  10737. }
  10738. }
  10739. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10740. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10741. #endif
  10742. #if WASM_ENABLE_GC != 0
  10743. j = (int32)(func_type->result_ref_type_maps
  10744. - func_type->ref_type_maps);
  10745. #endif
  10746. for (i = 0; i < func_type->result_count; i++) {
  10747. #if WASM_ENABLE_GC != 0
  10748. if (wasm_is_type_multi_byte_type(
  10749. func_type->types[func_type->param_count + i])) {
  10750. ref_type = func_type->ref_type_maps[j].ref_type;
  10751. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10752. ref_type,
  10753. wasm_reftype_struct_size(ref_type));
  10754. j++;
  10755. }
  10756. #endif
  10757. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10758. #if WASM_ENABLE_FAST_INTERP != 0
  10759. /* Here we emit each return value's dynamic_offset. But
  10760. * in fact these offsets are continuous, so interpreter
  10761. * only need to get the first return value's offset.
  10762. */
  10763. PUSH_OFFSET_TYPE(
  10764. func_type->types[func_type->param_count + i]);
  10765. #endif
  10766. }
  10767. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10768. }
  10769. else {
  10770. #if WASM_ENABLE_GC == 0
  10771. if (func_type->result_count
  10772. != func->func_type->result_count) {
  10773. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10774. "type mismatch: expect ",
  10775. func->func_type->result_count,
  10776. " return values but got other");
  10777. goto fail;
  10778. }
  10779. for (i = 0; i < func_type->result_count; i++) {
  10780. type = func->func_type
  10781. ->types[func->func_type->param_count + i];
  10782. if (func_type->types[func_type->param_count + i]
  10783. != type) {
  10784. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10785. "type mismatch: expect ",
  10786. type2str(type), " but got other");
  10787. goto fail;
  10788. }
  10789. }
  10790. #else
  10791. if (!wasm_func_type_result_is_subtype_of(
  10792. func_type, func->func_type, module->types,
  10793. module->type_count)) {
  10794. set_error_buf(
  10795. error_buf, error_buf_size,
  10796. "type mismatch: invalid func result types");
  10797. goto fail;
  10798. }
  10799. #endif
  10800. RESET_STACK();
  10801. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10802. }
  10803. #endif
  10804. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10805. || WASM_ENABLE_WAMR_COMPILER != 0
  10806. func->has_op_func_call = true;
  10807. #endif
  10808. (void)type;
  10809. break;
  10810. }
  10811. /*
  10812. * if disable reference type: call_indirect typeidx, 0x00
  10813. * if enable reference type: call_indirect typeidx, tableidx
  10814. */
  10815. case WASM_OP_CALL_INDIRECT:
  10816. #if WASM_ENABLE_TAIL_CALL != 0
  10817. case WASM_OP_RETURN_CALL_INDIRECT:
  10818. #endif
  10819. {
  10820. int32 idx;
  10821. WASMFuncType *func_type;
  10822. read_leb_uint32(p, p_end, type_idx);
  10823. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10824. #if WASM_ENABLE_WAMR_COMPILER != 0
  10825. if (p + 1 < p_end && *p != 0x00) {
  10826. /*
  10827. * Any non-0x00 byte requires the ref types proposal.
  10828. * This is different from checking the table_idx value
  10829. * since `0x80 0x00` etc. are all valid encodings of zero.
  10830. */
  10831. module->is_ref_types_used = true;
  10832. }
  10833. #endif
  10834. read_leb_uint32(p, p_end, table_idx);
  10835. #else
  10836. CHECK_BUF(p, p_end, 1);
  10837. table_idx = read_uint8(p);
  10838. #endif
  10839. if (!check_table_index(module, table_idx, error_buf,
  10840. error_buf_size)) {
  10841. goto fail;
  10842. }
  10843. #if WASM_ENABLE_FAST_INTERP != 0
  10844. /* we need to emit before arguments */
  10845. #if WASM_ENABLE_TAIL_CALL != 0
  10846. emit_byte(loader_ctx, opcode);
  10847. #endif
  10848. emit_uint32(loader_ctx, type_idx);
  10849. emit_uint32(loader_ctx, table_idx);
  10850. #endif
  10851. /* skip elem idx */
  10852. POP_I32();
  10853. if (type_idx >= module->type_count) {
  10854. set_error_buf(error_buf, error_buf_size, "unknown type");
  10855. goto fail;
  10856. }
  10857. func_type = (WASMFuncType *)module->types[type_idx];
  10858. if (func_type->param_count > 0) {
  10859. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10860. idx--) {
  10861. #if WASM_ENABLE_FAST_INTERP != 0
  10862. POP_OFFSET_TYPE(func_type->types[idx]);
  10863. #endif
  10864. POP_TYPE(func_type->types[idx]);
  10865. }
  10866. }
  10867. #if WASM_ENABLE_TAIL_CALL != 0
  10868. if (opcode == WASM_OP_CALL_INDIRECT) {
  10869. #endif
  10870. for (i = 0; i < func_type->result_count; i++) {
  10871. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10872. #if WASM_ENABLE_FAST_INTERP != 0
  10873. PUSH_OFFSET_TYPE(
  10874. func_type->types[func_type->param_count + i]);
  10875. #endif
  10876. }
  10877. #if WASM_ENABLE_TAIL_CALL != 0
  10878. }
  10879. else {
  10880. uint8 type;
  10881. if (func_type->result_count
  10882. != func->func_type->result_count) {
  10883. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10884. "type mismatch: expect ",
  10885. func->func_type->result_count,
  10886. " return values but got other");
  10887. goto fail;
  10888. }
  10889. for (i = 0; i < func_type->result_count; i++) {
  10890. type = func->func_type
  10891. ->types[func->func_type->param_count + i];
  10892. if (func_type->types[func_type->param_count + i]
  10893. != type) {
  10894. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10895. "type mismatch: expect ",
  10896. type2str(type), " but got other");
  10897. goto fail;
  10898. }
  10899. }
  10900. RESET_STACK();
  10901. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10902. }
  10903. #endif
  10904. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10905. || WASM_ENABLE_WAMR_COMPILER != 0
  10906. func->has_op_func_call = true;
  10907. #endif
  10908. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10909. func->has_op_call_indirect = true;
  10910. #endif
  10911. break;
  10912. }
  10913. case WASM_OP_DROP:
  10914. {
  10915. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10916. int32 available_stack_cell =
  10917. (int32)(loader_ctx->stack_cell_num
  10918. - cur_block->stack_cell_num);
  10919. if (available_stack_cell <= 0
  10920. && !cur_block->is_stack_polymorphic) {
  10921. set_error_buf(error_buf, error_buf_size,
  10922. "type mismatch, opcode drop was found "
  10923. "but stack was empty");
  10924. goto fail;
  10925. }
  10926. if (available_stack_cell > 0) {
  10927. #if WASM_ENABLE_GC != 0
  10928. if (wasm_is_type_multi_byte_type(
  10929. *(loader_ctx->frame_ref - 1))) {
  10930. bh_assert((int32)(loader_ctx->reftype_map_num
  10931. - cur_block->reftype_map_num)
  10932. > 0);
  10933. loader_ctx->frame_reftype_map--;
  10934. loader_ctx->reftype_map_num--;
  10935. }
  10936. #endif
  10937. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  10938. loader_ctx->frame_ref--;
  10939. loader_ctx->stack_cell_num--;
  10940. #if WASM_ENABLE_FAST_INTERP != 0
  10941. skip_label();
  10942. loader_ctx->frame_offset--;
  10943. if ((*(loader_ctx->frame_offset)
  10944. > loader_ctx->start_dynamic_offset)
  10945. && (*(loader_ctx->frame_offset)
  10946. < loader_ctx->max_dynamic_offset))
  10947. loader_ctx->dynamic_offset--;
  10948. #endif
  10949. }
  10950. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10951. loader_ctx->frame_ref -= 2;
  10952. loader_ctx->stack_cell_num -= 2;
  10953. #if WASM_ENABLE_FAST_INTERP == 0
  10954. *(p - 1) = WASM_OP_DROP_64;
  10955. #endif
  10956. #if WASM_ENABLE_FAST_INTERP != 0
  10957. skip_label();
  10958. loader_ctx->frame_offset -= 2;
  10959. if ((*(loader_ctx->frame_offset)
  10960. > loader_ctx->start_dynamic_offset)
  10961. && (*(loader_ctx->frame_offset)
  10962. < loader_ctx->max_dynamic_offset))
  10963. loader_ctx->dynamic_offset -= 2;
  10964. #endif
  10965. }
  10966. #if WASM_ENABLE_SIMD != 0
  10967. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10968. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10969. loader_ctx->frame_ref -= 4;
  10970. loader_ctx->stack_cell_num -= 4;
  10971. }
  10972. #endif
  10973. #endif
  10974. else {
  10975. set_error_buf(error_buf, error_buf_size,
  10976. "type mismatch");
  10977. goto fail;
  10978. }
  10979. }
  10980. else {
  10981. #if WASM_ENABLE_FAST_INTERP != 0
  10982. skip_label();
  10983. #endif
  10984. }
  10985. break;
  10986. }
  10987. case WASM_OP_SELECT:
  10988. {
  10989. uint8 ref_type;
  10990. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10991. int32 available_stack_cell;
  10992. #if WASM_ENABLE_FAST_INTERP != 0
  10993. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10994. #endif
  10995. POP_I32();
  10996. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  10997. - cur_block->stack_cell_num);
  10998. if (available_stack_cell <= 0
  10999. && !cur_block->is_stack_polymorphic) {
  11000. set_error_buf(error_buf, error_buf_size,
  11001. "type mismatch or invalid result arity, "
  11002. "opcode select was found "
  11003. "but stack was empty");
  11004. goto fail;
  11005. }
  11006. if (available_stack_cell > 0) {
  11007. switch (*(loader_ctx->frame_ref - 1)) {
  11008. case VALUE_TYPE_I32:
  11009. case VALUE_TYPE_F32:
  11010. case VALUE_TYPE_ANY:
  11011. break;
  11012. case VALUE_TYPE_I64:
  11013. case VALUE_TYPE_F64:
  11014. #if WASM_ENABLE_FAST_INTERP == 0
  11015. *(p - 1) = WASM_OP_SELECT_64;
  11016. #endif
  11017. #if WASM_ENABLE_FAST_INTERP != 0
  11018. if (loader_ctx->p_code_compiled) {
  11019. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11020. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11021. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11022. *(void **)(p_code_compiled_tmp
  11023. - sizeof(void *)) =
  11024. handle_table[opcode_tmp];
  11025. #else
  11026. #if UINTPTR_MAX == UINT64_MAX
  11027. /* emit int32 relative offset in 64-bit target
  11028. */
  11029. int32 offset =
  11030. (int32)((uint8 *)handle_table[opcode_tmp]
  11031. - (uint8 *)handle_table[0]);
  11032. *(int32 *)(p_code_compiled_tmp
  11033. - sizeof(int32)) = offset;
  11034. #else
  11035. /* emit uint32 label address in 32-bit target */
  11036. *(uint32 *)(p_code_compiled_tmp
  11037. - sizeof(uint32)) =
  11038. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11039. #endif
  11040. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11041. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11042. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11043. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11044. #else
  11045. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11046. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11047. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11048. }
  11049. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11050. break;
  11051. #if WASM_ENABLE_SIMD != 0
  11052. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  11053. case VALUE_TYPE_V128:
  11054. break;
  11055. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  11056. #endif /* WASM_ENABLE_SIMD != 0 */
  11057. default:
  11058. {
  11059. set_error_buf(error_buf, error_buf_size,
  11060. "type mismatch");
  11061. goto fail;
  11062. }
  11063. }
  11064. ref_type = *(loader_ctx->frame_ref - 1);
  11065. #if WASM_ENABLE_FAST_INTERP != 0
  11066. POP_OFFSET_TYPE(ref_type);
  11067. POP_TYPE(ref_type);
  11068. POP_OFFSET_TYPE(ref_type);
  11069. POP_TYPE(ref_type);
  11070. PUSH_OFFSET_TYPE(ref_type);
  11071. PUSH_TYPE(ref_type);
  11072. #else
  11073. POP2_AND_PUSH(ref_type, ref_type);
  11074. #endif
  11075. }
  11076. else {
  11077. #if WASM_ENABLE_FAST_INTERP != 0
  11078. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11079. #endif
  11080. PUSH_TYPE(VALUE_TYPE_ANY);
  11081. }
  11082. break;
  11083. }
  11084. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11085. case WASM_OP_SELECT_T:
  11086. {
  11087. uint8 vec_len, type;
  11088. #if WASM_ENABLE_GC != 0
  11089. WASMRefType *ref_type = NULL;
  11090. #endif
  11091. #if WASM_ENABLE_FAST_INTERP != 0
  11092. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11093. #endif
  11094. read_leb_uint32(p, p_end, vec_len);
  11095. if (vec_len != 1) {
  11096. /* typed select must have exactly one result */
  11097. set_error_buf(error_buf, error_buf_size,
  11098. "invalid result arity");
  11099. goto fail;
  11100. }
  11101. #if WASM_ENABLE_GC == 0
  11102. CHECK_BUF(p, p_end, 1);
  11103. type = read_uint8(p);
  11104. if (!is_valid_value_type_for_interpreter(type)) {
  11105. set_error_buf(error_buf, error_buf_size,
  11106. "unknown value type");
  11107. goto fail;
  11108. }
  11109. #else
  11110. p_org = p + 1;
  11111. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11112. module->type_count, &need_ref_type_map,
  11113. &wasm_ref_type, false, error_buf,
  11114. error_buf_size)) {
  11115. goto fail;
  11116. }
  11117. type = wasm_ref_type.ref_type;
  11118. if (need_ref_type_map) {
  11119. if (!(ref_type = reftype_set_insert(
  11120. module->ref_type_set, &wasm_ref_type, error_buf,
  11121. error_buf_size))) {
  11122. goto fail;
  11123. }
  11124. }
  11125. #if WASM_ENABLE_FAST_INTERP == 0
  11126. while (p_org < p) {
  11127. #if WASM_ENABLE_DEBUG_INTERP != 0
  11128. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11129. error_buf_size)) {
  11130. goto fail;
  11131. }
  11132. #endif
  11133. /* Ignore extra bytes for interpreter */
  11134. *p_org++ = WASM_OP_NOP;
  11135. }
  11136. #endif
  11137. #endif /* end of WASM_ENABLE_GC == 0 */
  11138. POP_I32();
  11139. #if WASM_ENABLE_FAST_INTERP != 0
  11140. if (loader_ctx->p_code_compiled) {
  11141. uint8 opcode_tmp = WASM_OP_SELECT;
  11142. if (type == VALUE_TYPE_V128) {
  11143. #if (WASM_ENABLE_SIMD == 0) \
  11144. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  11145. set_error_buf(error_buf, error_buf_size,
  11146. "SIMD v128 type isn't supported");
  11147. goto fail;
  11148. #endif
  11149. }
  11150. else {
  11151. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11152. opcode_tmp = WASM_OP_SELECT_64;
  11153. #if WASM_ENABLE_GC != 0
  11154. if (wasm_is_type_reftype(type))
  11155. opcode_tmp = WASM_OP_SELECT_T;
  11156. #endif
  11157. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11158. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11159. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11160. handle_table[opcode_tmp];
  11161. #else
  11162. #if UINTPTR_MAX == UINT64_MAX
  11163. /* emit int32 relative offset in 64-bit target */
  11164. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11165. - (uint8 *)handle_table[0]);
  11166. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11167. offset;
  11168. #else
  11169. /* emit uint32 label address in 32-bit target */
  11170. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11171. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11172. #endif
  11173. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11174. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11175. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11176. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11177. #else
  11178. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11179. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11180. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11181. }
  11182. }
  11183. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11184. POP_REF(type);
  11185. #if WASM_ENABLE_GC != 0
  11186. if (need_ref_type_map) {
  11187. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11188. wasm_reftype_struct_size(ref_type));
  11189. }
  11190. #endif
  11191. POP_REF(type);
  11192. #if WASM_ENABLE_GC != 0
  11193. if (need_ref_type_map) {
  11194. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11195. wasm_reftype_struct_size(ref_type));
  11196. }
  11197. #endif
  11198. PUSH_REF(type);
  11199. #if WASM_ENABLE_WAMR_COMPILER != 0
  11200. module->is_ref_types_used = true;
  11201. #endif
  11202. (void)vec_len;
  11203. break;
  11204. }
  11205. /* table.get x. tables[x]. [i32] -> [t] */
  11206. /* table.set x. tables[x]. [i32 t] -> [] */
  11207. case WASM_OP_TABLE_GET:
  11208. case WASM_OP_TABLE_SET:
  11209. {
  11210. uint8 decl_ref_type;
  11211. #if WASM_ENABLE_GC != 0
  11212. WASMRefType *ref_type;
  11213. #endif
  11214. read_leb_uint32(p, p_end, table_idx);
  11215. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11216. #if WASM_ENABLE_GC != 0
  11217. (void **)&ref_type,
  11218. #else
  11219. NULL,
  11220. #endif
  11221. error_buf, error_buf_size))
  11222. goto fail;
  11223. #if WASM_ENABLE_GC != 0
  11224. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11225. bh_assert(ref_type);
  11226. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11227. ref_type, wasm_reftype_struct_size(ref_type));
  11228. }
  11229. #endif
  11230. #if WASM_ENABLE_FAST_INTERP != 0
  11231. emit_uint32(loader_ctx, table_idx);
  11232. #endif
  11233. if (opcode == WASM_OP_TABLE_GET) {
  11234. POP_I32();
  11235. #if WASM_ENABLE_FAST_INTERP != 0
  11236. PUSH_OFFSET_TYPE(decl_ref_type);
  11237. #endif
  11238. PUSH_TYPE(decl_ref_type);
  11239. }
  11240. else {
  11241. #if WASM_ENABLE_FAST_INTERP != 0
  11242. POP_OFFSET_TYPE(decl_ref_type);
  11243. #endif
  11244. POP_TYPE(decl_ref_type);
  11245. POP_I32();
  11246. }
  11247. #if WASM_ENABLE_WAMR_COMPILER != 0
  11248. module->is_ref_types_used = true;
  11249. #endif
  11250. break;
  11251. }
  11252. case WASM_OP_REF_NULL:
  11253. {
  11254. uint8 ref_type;
  11255. #if WASM_ENABLE_GC == 0
  11256. CHECK_BUF(p, p_end, 1);
  11257. ref_type = read_uint8(p);
  11258. if (ref_type != VALUE_TYPE_FUNCREF
  11259. && ref_type != VALUE_TYPE_EXTERNREF) {
  11260. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11261. goto fail;
  11262. }
  11263. #else
  11264. read_leb_int32(p, p_end, heap_type);
  11265. if (heap_type >= 0) {
  11266. if (!check_type_index(module, module->type_count, heap_type,
  11267. error_buf, error_buf_size)) {
  11268. goto fail;
  11269. }
  11270. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11271. true, heap_type);
  11272. ref_type = wasm_ref_type.ref_type;
  11273. }
  11274. else {
  11275. if (!wasm_is_valid_heap_type(heap_type)) {
  11276. set_error_buf(error_buf, error_buf_size,
  11277. "unknown type");
  11278. goto fail;
  11279. }
  11280. ref_type = (uint8)((int32)0x80 + heap_type);
  11281. }
  11282. #endif /* end of WASM_ENABLE_GC == 0 */
  11283. #if WASM_ENABLE_FAST_INTERP != 0
  11284. PUSH_OFFSET_TYPE(ref_type);
  11285. #endif
  11286. PUSH_TYPE(ref_type);
  11287. #if WASM_ENABLE_WAMR_COMPILER != 0
  11288. module->is_ref_types_used = true;
  11289. #endif
  11290. break;
  11291. }
  11292. case WASM_OP_REF_IS_NULL:
  11293. {
  11294. #if WASM_ENABLE_GC == 0
  11295. #if WASM_ENABLE_FAST_INTERP != 0
  11296. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11297. int32 block_stack_cell_num =
  11298. (int32)(loader_ctx->stack_cell_num
  11299. - cur_block->stack_cell_num);
  11300. if (block_stack_cell_num <= 0) {
  11301. if (!cur_block->is_stack_polymorphic) {
  11302. set_error_buf(
  11303. error_buf, error_buf_size,
  11304. "type mismatch: expect data but stack was empty");
  11305. goto fail;
  11306. }
  11307. }
  11308. else {
  11309. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11310. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11311. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11312. if (!wasm_loader_pop_frame_ref_offset(
  11313. loader_ctx, *(loader_ctx->frame_ref - 1),
  11314. error_buf, error_buf_size)) {
  11315. goto fail;
  11316. }
  11317. }
  11318. else {
  11319. set_error_buf(error_buf, error_buf_size,
  11320. "type mismatch");
  11321. goto fail;
  11322. }
  11323. }
  11324. #else
  11325. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11326. error_buf, error_buf_size)
  11327. && !wasm_loader_pop_frame_ref(loader_ctx,
  11328. VALUE_TYPE_EXTERNREF,
  11329. error_buf, error_buf_size)) {
  11330. goto fail;
  11331. }
  11332. #endif
  11333. #else /* else of WASM_ENABLE_GC == 0 */
  11334. uint8 type;
  11335. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11336. error_buf, error_buf_size)) {
  11337. goto fail;
  11338. }
  11339. #endif
  11340. PUSH_I32();
  11341. #if WASM_ENABLE_WAMR_COMPILER != 0
  11342. module->is_ref_types_used = true;
  11343. #endif
  11344. break;
  11345. }
  11346. case WASM_OP_REF_FUNC:
  11347. {
  11348. read_leb_uint32(p, p_end, func_idx);
  11349. if (!check_function_index(module, func_idx, error_buf,
  11350. error_buf_size)) {
  11351. goto fail;
  11352. }
  11353. /* Refer to a forward-declared function:
  11354. the function must be an import, exported, or present in
  11355. a table elem segment or global initializer to be used as
  11356. the operand to ref.func */
  11357. if (func_idx >= module->import_function_count) {
  11358. WASMTableSeg *table_seg = module->table_segments;
  11359. bool func_declared = false;
  11360. uint32 j;
  11361. for (i = 0; i < module->global_count; i++) {
  11362. if (module->globals[i].type.val_type
  11363. == VALUE_TYPE_FUNCREF
  11364. && module->globals[i].init_expr.init_expr_type
  11365. == INIT_EXPR_TYPE_FUNCREF_CONST
  11366. && module->globals[i].init_expr.u.u32 == func_idx) {
  11367. func_declared = true;
  11368. break;
  11369. }
  11370. }
  11371. if (!func_declared) {
  11372. /* Check whether the function is declared in table segs,
  11373. note that it doesn't matter whether the table seg's
  11374. mode is passive, active or declarative. */
  11375. for (i = 0; i < module->table_seg_count;
  11376. i++, table_seg++) {
  11377. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11378. #if WASM_ENABLE_GC != 0
  11379. /* elem type is (ref null? func) or
  11380. (ref null? $t) */
  11381. || ((table_seg->elem_type
  11382. == REF_TYPE_HT_NON_NULLABLE
  11383. || table_seg->elem_type
  11384. == REF_TYPE_HT_NULLABLE)
  11385. && (table_seg->elem_ref_type->ref_ht_common
  11386. .heap_type
  11387. == HEAP_TYPE_FUNC
  11388. || table_seg->elem_ref_type
  11389. ->ref_ht_common.heap_type
  11390. > 0))
  11391. #endif
  11392. ) {
  11393. for (j = 0; j < table_seg->value_count; j++) {
  11394. if (table_seg->init_values[j].u.ref_index
  11395. == func_idx) {
  11396. func_declared = true;
  11397. break;
  11398. }
  11399. }
  11400. }
  11401. }
  11402. }
  11403. if (!func_declared) {
  11404. /* Check whether the function is exported */
  11405. for (i = 0; i < module->export_count; i++) {
  11406. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11407. && module->exports[i].index == func_idx) {
  11408. func_declared = true;
  11409. break;
  11410. }
  11411. }
  11412. }
  11413. if (!func_declared) {
  11414. set_error_buf(error_buf, error_buf_size,
  11415. "undeclared function reference");
  11416. goto fail;
  11417. }
  11418. }
  11419. #if WASM_ENABLE_FAST_INTERP != 0
  11420. emit_uint32(loader_ctx, func_idx);
  11421. #endif
  11422. #if WASM_ENABLE_GC == 0
  11423. PUSH_FUNCREF();
  11424. #else
  11425. if (func_idx < module->import_function_count)
  11426. type_idx =
  11427. module->import_functions[func_idx].u.function.type_idx;
  11428. else
  11429. type_idx = module
  11430. ->functions[func_idx
  11431. - module->import_function_count]
  11432. ->type_idx;
  11433. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11434. false, type_idx);
  11435. PUSH_REF(wasm_ref_type.ref_type);
  11436. #endif
  11437. #if WASM_ENABLE_WAMR_COMPILER != 0
  11438. module->is_ref_types_used = true;
  11439. #endif
  11440. break;
  11441. }
  11442. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11443. #if WASM_ENABLE_GC != 0
  11444. case WASM_OP_REF_AS_NON_NULL:
  11445. case WASM_OP_BR_ON_NULL:
  11446. {
  11447. uint8 type;
  11448. WASMRefType ref_type;
  11449. /* POP (ref null ht) and get the converted (ref ht) */
  11450. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11451. error_buf, error_buf_size)) {
  11452. goto fail;
  11453. }
  11454. if (opcode == WASM_OP_BR_ON_NULL) {
  11455. if (!(frame_csp_tmp =
  11456. check_branch_block(loader_ctx, &p, p_end, opcode,
  11457. error_buf, error_buf_size))) {
  11458. goto fail;
  11459. }
  11460. #if WASM_ENABLE_FAST_INTERP != 0
  11461. disable_emit = true;
  11462. #endif
  11463. }
  11464. /* PUSH the converted (ref ht) */
  11465. if (type != VALUE_TYPE_ANY) {
  11466. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11467. sizeof(WASMRefType));
  11468. }
  11469. PUSH_REF(type);
  11470. break;
  11471. }
  11472. case WASM_OP_BR_ON_NON_NULL:
  11473. {
  11474. uint8 type;
  11475. WASMRefType ref_type;
  11476. uint32 available_stack_cell =
  11477. loader_ctx->stack_cell_num
  11478. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11479. /* POP (ref null ht) and get the converted (ref ht) */
  11480. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11481. error_buf, error_buf_size)) {
  11482. goto fail;
  11483. }
  11484. #if WASM_ENABLE_FAST_INTERP != 0
  11485. disable_emit = true;
  11486. #endif
  11487. /* Temporarily PUSH back (ref ht), check brach block and
  11488. then POP it */
  11489. if (available_stack_cell
  11490. > 0) { /* stack isn't in polymorphic state */
  11491. if (type != VALUE_TYPE_ANY) {
  11492. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11493. &ref_type, sizeof(WASMRefType));
  11494. }
  11495. PUSH_REF(type);
  11496. }
  11497. if (!(frame_csp_tmp =
  11498. check_branch_block(loader_ctx, &p, p_end, opcode,
  11499. error_buf, error_buf_size))) {
  11500. goto fail;
  11501. }
  11502. if (available_stack_cell
  11503. > 0) { /* stack isn't in polymorphic state */
  11504. POP_REF(type);
  11505. #if WASM_ENABLE_FAST_INTERP != 0
  11506. /* Erase the opnd offset emitted by POP_REF() */
  11507. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11508. #endif
  11509. }
  11510. break;
  11511. }
  11512. case WASM_OP_REF_EQ:
  11513. POP_REF(REF_TYPE_EQREF);
  11514. POP_REF(REF_TYPE_EQREF);
  11515. PUSH_I32();
  11516. break;
  11517. #endif /* end of WASM_ENABLE_GC != 0 */
  11518. case WASM_OP_GET_LOCAL:
  11519. {
  11520. p_org = p - 1;
  11521. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11522. PUSH_TYPE(local_type);
  11523. #if WASM_ENABLE_GC != 0
  11524. /* Cannot get a non-nullable and unset local */
  11525. if (local_idx >= param_count
  11526. && wasm_is_reftype_htref_non_nullable(local_type)
  11527. && !wasm_loader_get_local_status(loader_ctx,
  11528. local_idx - param_count)) {
  11529. set_error_buf(error_buf, error_buf_size,
  11530. "uninitialized local");
  11531. goto fail;
  11532. }
  11533. #endif
  11534. #if WASM_ENABLE_FAST_INTERP != 0
  11535. /* Get Local is optimized out */
  11536. skip_label();
  11537. disable_emit = true;
  11538. operand_offset = local_offset;
  11539. PUSH_OFFSET_TYPE(local_type);
  11540. #else
  11541. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11542. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11543. if (local_offset < 0x80
  11544. #if WASM_ENABLE_GC != 0
  11545. && !wasm_is_type_reftype(local_type)
  11546. #endif
  11547. ) {
  11548. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11549. if (is_32bit_type(local_type)) {
  11550. *p_org++ = (uint8)local_offset;
  11551. }
  11552. else {
  11553. *p_org++ = (uint8)(local_offset | 0x80);
  11554. }
  11555. while (p_org < p) {
  11556. *p_org++ = WASM_OP_NOP;
  11557. }
  11558. }
  11559. #endif
  11560. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11561. break;
  11562. }
  11563. case WASM_OP_SET_LOCAL:
  11564. {
  11565. p_org = p - 1;
  11566. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11567. #if WASM_ENABLE_FAST_INTERP != 0
  11568. if (!(preserve_referenced_local(
  11569. loader_ctx, opcode, local_offset, local_type,
  11570. &preserve_local, error_buf, error_buf_size)))
  11571. goto fail;
  11572. if (local_offset < 256
  11573. #if WASM_ENABLE_GC != 0
  11574. && !wasm_is_type_reftype(local_type)
  11575. #endif
  11576. ) {
  11577. skip_label();
  11578. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11579. if (loader_ctx->p_code_compiled)
  11580. STORE_U16(loader_ctx->p_code_compiled - 2,
  11581. local_offset);
  11582. loader_ctx->frame_offset--;
  11583. loader_ctx->dynamic_offset--;
  11584. }
  11585. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11586. if (loader_ctx->p_code_compiled)
  11587. STORE_U16(loader_ctx->p_code_compiled - 2,
  11588. local_offset);
  11589. loader_ctx->frame_offset -= 2;
  11590. loader_ctx->dynamic_offset -= 2;
  11591. }
  11592. else {
  11593. if (is_32bit_type(local_type)) {
  11594. emit_label(EXT_OP_SET_LOCAL_FAST);
  11595. emit_byte(loader_ctx, (uint8)local_offset);
  11596. }
  11597. else {
  11598. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11599. emit_byte(loader_ctx, (uint8)local_offset);
  11600. }
  11601. POP_OFFSET_TYPE(local_type);
  11602. }
  11603. }
  11604. else { /* local index larger than 255, reserve leb */
  11605. emit_uint32(loader_ctx, local_idx);
  11606. POP_OFFSET_TYPE(local_type);
  11607. }
  11608. #else
  11609. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11610. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11611. if (local_offset < 0x80
  11612. #if WASM_ENABLE_GC != 0
  11613. && !wasm_is_type_reftype(local_type)
  11614. #endif
  11615. ) {
  11616. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11617. if (is_32bit_type(local_type)) {
  11618. *p_org++ = (uint8)local_offset;
  11619. }
  11620. else {
  11621. *p_org++ = (uint8)(local_offset | 0x80);
  11622. }
  11623. while (p_org < p) {
  11624. *p_org++ = WASM_OP_NOP;
  11625. }
  11626. }
  11627. #endif
  11628. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11629. #if WASM_ENABLE_GC != 0
  11630. if (local_idx >= param_count) {
  11631. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11632. }
  11633. #endif
  11634. POP_TYPE(local_type);
  11635. break;
  11636. }
  11637. case WASM_OP_TEE_LOCAL:
  11638. {
  11639. p_org = p - 1;
  11640. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11641. #if WASM_ENABLE_FAST_INTERP != 0
  11642. /* If the stack is in polymorphic state, do fake pop and push on
  11643. offset stack to keep the depth of offset stack to be the
  11644. same with ref stack */
  11645. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11646. if (cur_block->is_stack_polymorphic) {
  11647. POP_OFFSET_TYPE(local_type);
  11648. PUSH_OFFSET_TYPE(local_type);
  11649. }
  11650. #endif
  11651. POP_TYPE(local_type);
  11652. PUSH_TYPE(local_type);
  11653. #if WASM_ENABLE_FAST_INTERP != 0
  11654. if (!(preserve_referenced_local(
  11655. loader_ctx, opcode, local_offset, local_type,
  11656. &preserve_local, error_buf, error_buf_size)))
  11657. goto fail;
  11658. if (local_offset < 256
  11659. #if WASM_ENABLE_GC != 0
  11660. && !wasm_is_type_reftype(local_type)
  11661. #endif
  11662. ) {
  11663. skip_label();
  11664. if (is_32bit_type(local_type)) {
  11665. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11666. emit_byte(loader_ctx, (uint8)local_offset);
  11667. }
  11668. else {
  11669. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11670. emit_byte(loader_ctx, (uint8)local_offset);
  11671. }
  11672. }
  11673. else { /* local index larger than 255, reserve leb */
  11674. emit_uint32(loader_ctx, local_idx);
  11675. }
  11676. emit_operand(loader_ctx,
  11677. *(loader_ctx->frame_offset
  11678. - wasm_value_type_cell_num(local_type)));
  11679. #else
  11680. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11681. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11682. if (local_offset < 0x80
  11683. #if WASM_ENABLE_GC != 0
  11684. && !wasm_is_type_reftype(local_type)
  11685. #endif
  11686. ) {
  11687. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11688. if (is_32bit_type(local_type)) {
  11689. *p_org++ = (uint8)local_offset;
  11690. }
  11691. else {
  11692. *p_org++ = (uint8)(local_offset | 0x80);
  11693. }
  11694. while (p_org < p) {
  11695. *p_org++ = WASM_OP_NOP;
  11696. }
  11697. }
  11698. #endif
  11699. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11700. #if WASM_ENABLE_GC != 0
  11701. if (local_idx >= param_count) {
  11702. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11703. }
  11704. #endif
  11705. break;
  11706. }
  11707. case WASM_OP_GET_GLOBAL:
  11708. {
  11709. #if WASM_ENABLE_GC != 0
  11710. WASMRefType *ref_type;
  11711. #endif
  11712. p_org = p - 1;
  11713. read_leb_uint32(p, p_end, global_idx);
  11714. if (global_idx >= global_count) {
  11715. set_error_buf(error_buf, error_buf_size, "unknown global");
  11716. goto fail;
  11717. }
  11718. global_type = global_idx < module->import_global_count
  11719. ? module->import_globals[global_idx]
  11720. .u.global.type.val_type
  11721. : module
  11722. ->globals[global_idx
  11723. - module->import_global_count]
  11724. .type.val_type;
  11725. #if WASM_ENABLE_GC != 0
  11726. ref_type =
  11727. global_idx < module->import_global_count
  11728. ? module->import_globals[global_idx].u.global.ref_type
  11729. : module
  11730. ->globals[global_idx
  11731. - module->import_global_count]
  11732. .ref_type;
  11733. if (wasm_is_type_multi_byte_type(global_type)) {
  11734. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11735. wasm_reftype_struct_size(ref_type));
  11736. }
  11737. #endif
  11738. PUSH_TYPE(global_type);
  11739. #if WASM_ENABLE_FAST_INTERP == 0
  11740. if (global_type == VALUE_TYPE_I64
  11741. || global_type == VALUE_TYPE_F64) {
  11742. #if WASM_ENABLE_DEBUG_INTERP != 0
  11743. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11744. error_buf_size)) {
  11745. goto fail;
  11746. }
  11747. #endif
  11748. *p_org = WASM_OP_GET_GLOBAL_64;
  11749. }
  11750. #else /* else of WASM_ENABLE_FAST_INTERP */
  11751. if (global_type == VALUE_TYPE_I64
  11752. || global_type == VALUE_TYPE_F64) {
  11753. skip_label();
  11754. emit_label(WASM_OP_GET_GLOBAL_64);
  11755. }
  11756. emit_uint32(loader_ctx, global_idx);
  11757. PUSH_OFFSET_TYPE(global_type);
  11758. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11759. break;
  11760. }
  11761. case WASM_OP_SET_GLOBAL:
  11762. {
  11763. bool is_mutable = false;
  11764. #if WASM_ENABLE_GC != 0
  11765. WASMRefType *ref_type;
  11766. #endif
  11767. p_org = p - 1;
  11768. read_leb_uint32(p, p_end, global_idx);
  11769. if (global_idx >= global_count) {
  11770. set_error_buf(error_buf, error_buf_size, "unknown global");
  11771. goto fail;
  11772. }
  11773. is_mutable = global_idx < module->import_global_count
  11774. ? module->import_globals[global_idx]
  11775. .u.global.type.is_mutable
  11776. : module
  11777. ->globals[global_idx
  11778. - module->import_global_count]
  11779. .type.is_mutable;
  11780. if (!is_mutable) {
  11781. #if WASM_ENABLE_GC == 0
  11782. set_error_buf(error_buf, error_buf_size,
  11783. "global is immutable");
  11784. #else
  11785. set_error_buf(error_buf, error_buf_size,
  11786. "immutable global");
  11787. #endif
  11788. goto fail;
  11789. }
  11790. global_type = global_idx < module->import_global_count
  11791. ? module->import_globals[global_idx]
  11792. .u.global.type.val_type
  11793. : module
  11794. ->globals[global_idx
  11795. - module->import_global_count]
  11796. .type.val_type;
  11797. #if WASM_ENABLE_GC != 0
  11798. ref_type =
  11799. global_idx < module->import_global_count
  11800. ? module->import_globals[global_idx].u.global.ref_type
  11801. : module
  11802. ->globals[global_idx
  11803. - module->import_global_count]
  11804. .ref_type;
  11805. if (wasm_is_type_multi_byte_type(global_type)) {
  11806. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11807. wasm_reftype_struct_size(ref_type));
  11808. }
  11809. #endif
  11810. #if WASM_ENABLE_FAST_INTERP == 0
  11811. if (global_type == VALUE_TYPE_I64
  11812. || global_type == VALUE_TYPE_F64) {
  11813. #if WASM_ENABLE_DEBUG_INTERP != 0
  11814. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11815. error_buf_size)) {
  11816. goto fail;
  11817. }
  11818. #endif
  11819. *p_org = WASM_OP_SET_GLOBAL_64;
  11820. }
  11821. else if (module->aux_stack_size > 0
  11822. && global_idx == module->aux_stack_top_global_index) {
  11823. #if WASM_ENABLE_DEBUG_INTERP != 0
  11824. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11825. error_buf_size)) {
  11826. goto fail;
  11827. }
  11828. #endif
  11829. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11830. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11831. func->has_op_set_global_aux_stack = true;
  11832. #endif
  11833. }
  11834. #else /* else of WASM_ENABLE_FAST_INTERP */
  11835. if (global_type == VALUE_TYPE_I64
  11836. || global_type == VALUE_TYPE_F64) {
  11837. skip_label();
  11838. emit_label(WASM_OP_SET_GLOBAL_64);
  11839. }
  11840. else if (module->aux_stack_size > 0
  11841. && global_idx == module->aux_stack_top_global_index) {
  11842. skip_label();
  11843. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11844. }
  11845. emit_uint32(loader_ctx, global_idx);
  11846. POP_OFFSET_TYPE(global_type);
  11847. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11848. POP_TYPE(global_type);
  11849. break;
  11850. }
  11851. /* load */
  11852. case WASM_OP_I32_LOAD:
  11853. case WASM_OP_I32_LOAD8_S:
  11854. case WASM_OP_I32_LOAD8_U:
  11855. case WASM_OP_I32_LOAD16_S:
  11856. case WASM_OP_I32_LOAD16_U:
  11857. case WASM_OP_I64_LOAD:
  11858. case WASM_OP_I64_LOAD8_S:
  11859. case WASM_OP_I64_LOAD8_U:
  11860. case WASM_OP_I64_LOAD16_S:
  11861. case WASM_OP_I64_LOAD16_U:
  11862. case WASM_OP_I64_LOAD32_S:
  11863. case WASM_OP_I64_LOAD32_U:
  11864. case WASM_OP_F32_LOAD:
  11865. case WASM_OP_F64_LOAD:
  11866. /* store */
  11867. case WASM_OP_I32_STORE:
  11868. case WASM_OP_I32_STORE8:
  11869. case WASM_OP_I32_STORE16:
  11870. case WASM_OP_I64_STORE:
  11871. case WASM_OP_I64_STORE8:
  11872. case WASM_OP_I64_STORE16:
  11873. case WASM_OP_I64_STORE32:
  11874. case WASM_OP_F32_STORE:
  11875. case WASM_OP_F64_STORE:
  11876. {
  11877. #if WASM_ENABLE_FAST_INTERP != 0
  11878. /* change F32/F64 into I32/I64 */
  11879. if (opcode == WASM_OP_F32_LOAD) {
  11880. skip_label();
  11881. emit_label(WASM_OP_I32_LOAD);
  11882. }
  11883. else if (opcode == WASM_OP_F64_LOAD) {
  11884. skip_label();
  11885. emit_label(WASM_OP_I64_LOAD);
  11886. }
  11887. else if (opcode == WASM_OP_F32_STORE) {
  11888. skip_label();
  11889. emit_label(WASM_OP_I32_STORE);
  11890. }
  11891. else if (opcode == WASM_OP_F64_STORE) {
  11892. skip_label();
  11893. emit_label(WASM_OP_I64_STORE);
  11894. }
  11895. #endif
  11896. CHECK_MEMORY();
  11897. read_leb_memarg(p, p_end, align); /* align */
  11898. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  11899. if (!check_memory_access_align(opcode, align, error_buf,
  11900. error_buf_size)) {
  11901. goto fail;
  11902. }
  11903. #if WASM_ENABLE_FAST_INTERP != 0
  11904. emit_uint32(loader_ctx, mem_offset);
  11905. #endif
  11906. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11907. func->has_memory_operations = true;
  11908. #endif
  11909. switch (opcode) {
  11910. /* load */
  11911. case WASM_OP_I32_LOAD:
  11912. case WASM_OP_I32_LOAD8_S:
  11913. case WASM_OP_I32_LOAD8_U:
  11914. case WASM_OP_I32_LOAD16_S:
  11915. case WASM_OP_I32_LOAD16_U:
  11916. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  11917. break;
  11918. case WASM_OP_I64_LOAD:
  11919. case WASM_OP_I64_LOAD8_S:
  11920. case WASM_OP_I64_LOAD8_U:
  11921. case WASM_OP_I64_LOAD16_S:
  11922. case WASM_OP_I64_LOAD16_U:
  11923. case WASM_OP_I64_LOAD32_S:
  11924. case WASM_OP_I64_LOAD32_U:
  11925. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  11926. break;
  11927. case WASM_OP_F32_LOAD:
  11928. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  11929. break;
  11930. case WASM_OP_F64_LOAD:
  11931. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  11932. break;
  11933. /* store */
  11934. case WASM_OP_I32_STORE:
  11935. case WASM_OP_I32_STORE8:
  11936. case WASM_OP_I32_STORE16:
  11937. POP_I32();
  11938. POP_MEM_OFFSET();
  11939. break;
  11940. case WASM_OP_I64_STORE:
  11941. case WASM_OP_I64_STORE8:
  11942. case WASM_OP_I64_STORE16:
  11943. case WASM_OP_I64_STORE32:
  11944. POP_I64();
  11945. POP_MEM_OFFSET();
  11946. break;
  11947. case WASM_OP_F32_STORE:
  11948. POP_F32();
  11949. POP_MEM_OFFSET();
  11950. break;
  11951. case WASM_OP_F64_STORE:
  11952. POP_F64();
  11953. POP_MEM_OFFSET();
  11954. break;
  11955. default:
  11956. break;
  11957. }
  11958. break;
  11959. }
  11960. case WASM_OP_MEMORY_SIZE:
  11961. CHECK_MEMORY();
  11962. read_leb_uint32(p, p_end, memidx);
  11963. check_memidx(module, memidx);
  11964. PUSH_PAGE_COUNT();
  11965. module->possible_memory_grow = true;
  11966. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11967. func->has_memory_operations = true;
  11968. #endif
  11969. break;
  11970. case WASM_OP_MEMORY_GROW:
  11971. CHECK_MEMORY();
  11972. read_leb_uint32(p, p_end, memidx);
  11973. check_memidx(module, memidx);
  11974. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  11975. module->possible_memory_grow = true;
  11976. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11977. || WASM_ENABLE_WAMR_COMPILER != 0
  11978. func->has_op_memory_grow = true;
  11979. #endif
  11980. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11981. func->has_memory_operations = true;
  11982. #endif
  11983. break;
  11984. case WASM_OP_I32_CONST:
  11985. read_leb_int32(p, p_end, i32_const);
  11986. #if WASM_ENABLE_FAST_INTERP != 0
  11987. skip_label();
  11988. disable_emit = true;
  11989. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  11990. if (operand_offset == 0) {
  11991. disable_emit = false;
  11992. emit_label(WASM_OP_I32_CONST);
  11993. emit_uint32(loader_ctx, i32_const);
  11994. }
  11995. #else
  11996. (void)i32_const;
  11997. #endif
  11998. PUSH_I32();
  11999. break;
  12000. case WASM_OP_I64_CONST:
  12001. read_leb_int64(p, p_end, i64_const);
  12002. #if WASM_ENABLE_FAST_INTERP != 0
  12003. skip_label();
  12004. disable_emit = true;
  12005. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  12006. if (operand_offset == 0) {
  12007. disable_emit = false;
  12008. emit_label(WASM_OP_I64_CONST);
  12009. emit_uint64(loader_ctx, i64_const);
  12010. }
  12011. #endif
  12012. PUSH_I64();
  12013. break;
  12014. case WASM_OP_F32_CONST:
  12015. CHECK_BUF(p, p_end, sizeof(float32));
  12016. p += sizeof(float32);
  12017. #if WASM_ENABLE_FAST_INTERP != 0
  12018. skip_label();
  12019. disable_emit = true;
  12020. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12021. sizeof(float32));
  12022. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12023. if (operand_offset == 0) {
  12024. disable_emit = false;
  12025. emit_label(WASM_OP_F32_CONST);
  12026. emit_float32(loader_ctx, f32_const);
  12027. }
  12028. #endif
  12029. PUSH_F32();
  12030. break;
  12031. case WASM_OP_F64_CONST:
  12032. CHECK_BUF(p, p_end, sizeof(float64));
  12033. p += sizeof(float64);
  12034. #if WASM_ENABLE_FAST_INTERP != 0
  12035. skip_label();
  12036. disable_emit = true;
  12037. /* Some MCU may require 8-byte align */
  12038. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12039. sizeof(float64));
  12040. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12041. if (operand_offset == 0) {
  12042. disable_emit = false;
  12043. emit_label(WASM_OP_F64_CONST);
  12044. emit_float64(loader_ctx, f64_const);
  12045. }
  12046. #endif
  12047. PUSH_F64();
  12048. break;
  12049. case WASM_OP_I32_EQZ:
  12050. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12051. break;
  12052. case WASM_OP_I32_EQ:
  12053. case WASM_OP_I32_NE:
  12054. case WASM_OP_I32_LT_S:
  12055. case WASM_OP_I32_LT_U:
  12056. case WASM_OP_I32_GT_S:
  12057. case WASM_OP_I32_GT_U:
  12058. case WASM_OP_I32_LE_S:
  12059. case WASM_OP_I32_LE_U:
  12060. case WASM_OP_I32_GE_S:
  12061. case WASM_OP_I32_GE_U:
  12062. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12063. break;
  12064. case WASM_OP_I64_EQZ:
  12065. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12066. break;
  12067. case WASM_OP_I64_EQ:
  12068. case WASM_OP_I64_NE:
  12069. case WASM_OP_I64_LT_S:
  12070. case WASM_OP_I64_LT_U:
  12071. case WASM_OP_I64_GT_S:
  12072. case WASM_OP_I64_GT_U:
  12073. case WASM_OP_I64_LE_S:
  12074. case WASM_OP_I64_LE_U:
  12075. case WASM_OP_I64_GE_S:
  12076. case WASM_OP_I64_GE_U:
  12077. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12078. break;
  12079. case WASM_OP_F32_EQ:
  12080. case WASM_OP_F32_NE:
  12081. case WASM_OP_F32_LT:
  12082. case WASM_OP_F32_GT:
  12083. case WASM_OP_F32_LE:
  12084. case WASM_OP_F32_GE:
  12085. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12086. break;
  12087. case WASM_OP_F64_EQ:
  12088. case WASM_OP_F64_NE:
  12089. case WASM_OP_F64_LT:
  12090. case WASM_OP_F64_GT:
  12091. case WASM_OP_F64_LE:
  12092. case WASM_OP_F64_GE:
  12093. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12094. break;
  12095. case WASM_OP_I32_CLZ:
  12096. case WASM_OP_I32_CTZ:
  12097. case WASM_OP_I32_POPCNT:
  12098. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12099. break;
  12100. case WASM_OP_I32_ADD:
  12101. case WASM_OP_I32_SUB:
  12102. case WASM_OP_I32_MUL:
  12103. case WASM_OP_I32_DIV_S:
  12104. case WASM_OP_I32_DIV_U:
  12105. case WASM_OP_I32_REM_S:
  12106. case WASM_OP_I32_REM_U:
  12107. case WASM_OP_I32_AND:
  12108. case WASM_OP_I32_OR:
  12109. case WASM_OP_I32_XOR:
  12110. case WASM_OP_I32_SHL:
  12111. case WASM_OP_I32_SHR_S:
  12112. case WASM_OP_I32_SHR_U:
  12113. case WASM_OP_I32_ROTL:
  12114. case WASM_OP_I32_ROTR:
  12115. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12116. break;
  12117. case WASM_OP_I64_CLZ:
  12118. case WASM_OP_I64_CTZ:
  12119. case WASM_OP_I64_POPCNT:
  12120. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12121. break;
  12122. case WASM_OP_I64_ADD:
  12123. case WASM_OP_I64_SUB:
  12124. case WASM_OP_I64_MUL:
  12125. case WASM_OP_I64_DIV_S:
  12126. case WASM_OP_I64_DIV_U:
  12127. case WASM_OP_I64_REM_S:
  12128. case WASM_OP_I64_REM_U:
  12129. case WASM_OP_I64_AND:
  12130. case WASM_OP_I64_OR:
  12131. case WASM_OP_I64_XOR:
  12132. case WASM_OP_I64_SHL:
  12133. case WASM_OP_I64_SHR_S:
  12134. case WASM_OP_I64_SHR_U:
  12135. case WASM_OP_I64_ROTL:
  12136. case WASM_OP_I64_ROTR:
  12137. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12138. break;
  12139. case WASM_OP_F32_ABS:
  12140. case WASM_OP_F32_NEG:
  12141. case WASM_OP_F32_CEIL:
  12142. case WASM_OP_F32_FLOOR:
  12143. case WASM_OP_F32_TRUNC:
  12144. case WASM_OP_F32_NEAREST:
  12145. case WASM_OP_F32_SQRT:
  12146. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12147. break;
  12148. case WASM_OP_F32_ADD:
  12149. case WASM_OP_F32_SUB:
  12150. case WASM_OP_F32_MUL:
  12151. case WASM_OP_F32_DIV:
  12152. case WASM_OP_F32_MIN:
  12153. case WASM_OP_F32_MAX:
  12154. case WASM_OP_F32_COPYSIGN:
  12155. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12156. break;
  12157. case WASM_OP_F64_ABS:
  12158. case WASM_OP_F64_NEG:
  12159. case WASM_OP_F64_CEIL:
  12160. case WASM_OP_F64_FLOOR:
  12161. case WASM_OP_F64_TRUNC:
  12162. case WASM_OP_F64_NEAREST:
  12163. case WASM_OP_F64_SQRT:
  12164. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12165. break;
  12166. case WASM_OP_F64_ADD:
  12167. case WASM_OP_F64_SUB:
  12168. case WASM_OP_F64_MUL:
  12169. case WASM_OP_F64_DIV:
  12170. case WASM_OP_F64_MIN:
  12171. case WASM_OP_F64_MAX:
  12172. case WASM_OP_F64_COPYSIGN:
  12173. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12174. break;
  12175. case WASM_OP_I32_WRAP_I64:
  12176. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12177. break;
  12178. case WASM_OP_I32_TRUNC_S_F32:
  12179. case WASM_OP_I32_TRUNC_U_F32:
  12180. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12181. break;
  12182. case WASM_OP_I32_TRUNC_S_F64:
  12183. case WASM_OP_I32_TRUNC_U_F64:
  12184. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12185. break;
  12186. case WASM_OP_I64_EXTEND_S_I32:
  12187. case WASM_OP_I64_EXTEND_U_I32:
  12188. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12189. break;
  12190. case WASM_OP_I64_TRUNC_S_F32:
  12191. case WASM_OP_I64_TRUNC_U_F32:
  12192. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12193. break;
  12194. case WASM_OP_I64_TRUNC_S_F64:
  12195. case WASM_OP_I64_TRUNC_U_F64:
  12196. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12197. break;
  12198. case WASM_OP_F32_CONVERT_S_I32:
  12199. case WASM_OP_F32_CONVERT_U_I32:
  12200. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12201. break;
  12202. case WASM_OP_F32_CONVERT_S_I64:
  12203. case WASM_OP_F32_CONVERT_U_I64:
  12204. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12205. break;
  12206. case WASM_OP_F32_DEMOTE_F64:
  12207. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12208. break;
  12209. case WASM_OP_F64_CONVERT_S_I32:
  12210. case WASM_OP_F64_CONVERT_U_I32:
  12211. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12212. break;
  12213. case WASM_OP_F64_CONVERT_S_I64:
  12214. case WASM_OP_F64_CONVERT_U_I64:
  12215. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12216. break;
  12217. case WASM_OP_F64_PROMOTE_F32:
  12218. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12219. break;
  12220. case WASM_OP_I32_REINTERPRET_F32:
  12221. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12222. break;
  12223. case WASM_OP_I64_REINTERPRET_F64:
  12224. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12225. break;
  12226. case WASM_OP_F32_REINTERPRET_I32:
  12227. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12228. break;
  12229. case WASM_OP_F64_REINTERPRET_I64:
  12230. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12231. break;
  12232. case WASM_OP_I32_EXTEND8_S:
  12233. case WASM_OP_I32_EXTEND16_S:
  12234. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12235. break;
  12236. case WASM_OP_I64_EXTEND8_S:
  12237. case WASM_OP_I64_EXTEND16_S:
  12238. case WASM_OP_I64_EXTEND32_S:
  12239. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12240. break;
  12241. #if WASM_ENABLE_GC != 0
  12242. case WASM_OP_GC_PREFIX:
  12243. {
  12244. uint32 opcode1;
  12245. read_leb_uint32(p, p_end, opcode1);
  12246. #if WASM_ENABLE_FAST_INTERP != 0
  12247. emit_byte(loader_ctx, ((uint8)opcode1));
  12248. #endif
  12249. switch (opcode1) {
  12250. case WASM_OP_STRUCT_NEW:
  12251. case WASM_OP_STRUCT_NEW_DEFAULT:
  12252. {
  12253. read_leb_uint32(p, p_end, type_idx);
  12254. #if WASM_ENABLE_FAST_INTERP != 0
  12255. emit_uint32(loader_ctx, type_idx);
  12256. #endif
  12257. if (!check_type_index(module, module->type_count,
  12258. type_idx, error_buf,
  12259. error_buf_size)) {
  12260. goto fail;
  12261. }
  12262. if (module->types[type_idx]->type_flag
  12263. != WASM_TYPE_STRUCT) {
  12264. set_error_buf(error_buf, error_buf_size,
  12265. "unkown struct type");
  12266. goto fail;
  12267. }
  12268. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12269. int32 j, k;
  12270. uint8 value_type;
  12271. uint32 ref_type_struct_size;
  12272. WASMStructType *struct_type =
  12273. (WASMStructType *)module->types[type_idx];
  12274. k = struct_type->ref_type_map_count - 1;
  12275. for (j = struct_type->field_count - 1; j >= 0;
  12276. j--) {
  12277. value_type = struct_type->fields[j].field_type;
  12278. if (wasm_is_type_reftype(value_type)) {
  12279. if (wasm_is_type_multi_byte_type(
  12280. value_type)) {
  12281. ref_type_struct_size =
  12282. wasm_reftype_struct_size(
  12283. struct_type->ref_type_maps[k]
  12284. .ref_type);
  12285. bh_memcpy_s(
  12286. &wasm_ref_type,
  12287. (uint32)sizeof(WASMRefType),
  12288. struct_type->ref_type_maps[k]
  12289. .ref_type,
  12290. ref_type_struct_size);
  12291. k--;
  12292. }
  12293. POP_REF(value_type);
  12294. }
  12295. else {
  12296. switch (value_type) {
  12297. case VALUE_TYPE_I32:
  12298. case PACKED_TYPE_I8:
  12299. case PACKED_TYPE_I16:
  12300. POP_I32();
  12301. break;
  12302. case VALUE_TYPE_I64:
  12303. POP_I64();
  12304. break;
  12305. case VALUE_TYPE_F32:
  12306. POP_F32();
  12307. break;
  12308. case VALUE_TYPE_F64:
  12309. POP_F64();
  12310. break;
  12311. default:
  12312. set_error_buf(error_buf,
  12313. error_buf_size,
  12314. "unknown type");
  12315. goto fail;
  12316. }
  12317. }
  12318. }
  12319. }
  12320. /* PUSH struct obj, (ref $t) */
  12321. wasm_set_refheaptype_typeidx(
  12322. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12323. PUSH_REF(wasm_ref_type.ref_type);
  12324. break;
  12325. }
  12326. case WASM_OP_STRUCT_GET:
  12327. case WASM_OP_STRUCT_GET_S:
  12328. case WASM_OP_STRUCT_GET_U:
  12329. case WASM_OP_STRUCT_SET:
  12330. {
  12331. WASMStructType *struct_type;
  12332. WASMRefType *ref_type = NULL;
  12333. uint32 field_idx;
  12334. uint8 field_type;
  12335. read_leb_uint32(p, p_end, type_idx);
  12336. #if WASM_ENABLE_FAST_INTERP != 0
  12337. emit_uint32(loader_ctx, type_idx);
  12338. #endif
  12339. if (!check_type_index(module, module->type_count,
  12340. type_idx, error_buf,
  12341. error_buf_size)) {
  12342. goto fail;
  12343. }
  12344. if (module->types[type_idx]->type_flag
  12345. != WASM_TYPE_STRUCT) {
  12346. set_error_buf(error_buf, error_buf_size,
  12347. "unknown struct type");
  12348. goto fail;
  12349. }
  12350. struct_type = (WASMStructType *)module->types[type_idx];
  12351. read_leb_uint32(p, p_end, field_idx);
  12352. #if WASM_ENABLE_FAST_INTERP != 0
  12353. emit_uint32(loader_ctx, field_idx);
  12354. #endif
  12355. if (field_idx >= struct_type->field_count) {
  12356. set_error_buf(error_buf, error_buf_size,
  12357. "unknown struct field");
  12358. goto fail;
  12359. }
  12360. if (opcode1 == WASM_OP_STRUCT_SET
  12361. && !(struct_type->fields[field_idx].field_flags
  12362. & 1)) {
  12363. set_error_buf(error_buf, error_buf_size,
  12364. "field is immutable");
  12365. goto fail;
  12366. }
  12367. field_type = struct_type->fields[field_idx].field_type;
  12368. if (is_packed_type(field_type)) {
  12369. if (opcode1 == WASM_OP_STRUCT_GET) {
  12370. set_error_buf(error_buf, error_buf_size,
  12371. "type mismatch");
  12372. goto fail;
  12373. }
  12374. else {
  12375. field_type = VALUE_TYPE_I32;
  12376. }
  12377. }
  12378. if (wasm_is_type_multi_byte_type(field_type)) {
  12379. ref_type = wasm_reftype_map_find(
  12380. struct_type->ref_type_maps,
  12381. struct_type->ref_type_map_count, field_idx);
  12382. bh_assert(ref_type);
  12383. }
  12384. if (opcode1 == WASM_OP_STRUCT_SET) {
  12385. /* POP field */
  12386. if (wasm_is_type_multi_byte_type(field_type)) {
  12387. bh_memcpy_s(&wasm_ref_type,
  12388. (uint32)sizeof(WASMRefType),
  12389. ref_type,
  12390. wasm_reftype_struct_size(ref_type));
  12391. }
  12392. POP_REF(field_type);
  12393. /* POP struct obj, (ref null $t) */
  12394. wasm_set_refheaptype_typeidx(
  12395. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12396. POP_REF(wasm_ref_type.ref_type);
  12397. }
  12398. else {
  12399. /* POP struct obj, (ref null $t) */
  12400. wasm_set_refheaptype_typeidx(
  12401. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12402. POP_REF(wasm_ref_type.ref_type);
  12403. /* PUSH field */
  12404. if (wasm_is_type_multi_byte_type(field_type)) {
  12405. bh_memcpy_s(&wasm_ref_type,
  12406. (uint32)sizeof(WASMRefType),
  12407. ref_type,
  12408. wasm_reftype_struct_size(ref_type));
  12409. }
  12410. PUSH_REF(field_type);
  12411. }
  12412. break;
  12413. }
  12414. case WASM_OP_ARRAY_NEW:
  12415. case WASM_OP_ARRAY_NEW_DEFAULT:
  12416. case WASM_OP_ARRAY_NEW_FIXED:
  12417. case WASM_OP_ARRAY_NEW_DATA:
  12418. case WASM_OP_ARRAY_NEW_ELEM:
  12419. {
  12420. WASMArrayType *array_type;
  12421. uint8 elem_type;
  12422. uint32 u32 = 0;
  12423. read_leb_uint32(p, p_end, type_idx);
  12424. #if WASM_ENABLE_FAST_INTERP != 0
  12425. emit_uint32(loader_ctx, type_idx);
  12426. #endif
  12427. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12428. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12429. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12430. read_leb_uint32(p, p_end, u32);
  12431. #if WASM_ENABLE_FAST_INTERP != 0
  12432. emit_uint32(loader_ctx, u32);
  12433. #endif
  12434. }
  12435. if (!check_array_type(module, type_idx, error_buf,
  12436. error_buf_size)) {
  12437. goto fail;
  12438. }
  12439. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12440. /* length */
  12441. POP_I32();
  12442. }
  12443. array_type = (WASMArrayType *)module->types[type_idx];
  12444. elem_type = array_type->elem_type;
  12445. if (opcode1 == WASM_OP_ARRAY_NEW
  12446. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12447. if (wasm_is_type_multi_byte_type(elem_type)) {
  12448. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12449. array_type->elem_ref_type,
  12450. wasm_reftype_struct_size(
  12451. array_type->elem_ref_type));
  12452. }
  12453. if (is_packed_type(elem_type)) {
  12454. elem_type = VALUE_TYPE_I32;
  12455. }
  12456. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12457. uint32 N = u32;
  12458. for (i = 0; i < N; i++) {
  12459. if (wasm_is_type_multi_byte_type(
  12460. elem_type)) {
  12461. bh_memcpy_s(
  12462. &wasm_ref_type, sizeof(WASMRefType),
  12463. array_type->elem_ref_type,
  12464. wasm_reftype_struct_size(
  12465. array_type->elem_ref_type));
  12466. }
  12467. POP_REF(elem_type);
  12468. }
  12469. }
  12470. else
  12471. POP_REF(elem_type);
  12472. }
  12473. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12474. /* offset of data segment */
  12475. POP_I32();
  12476. if (u32 >= module->data_seg_count) {
  12477. set_error_buf(error_buf, error_buf_size,
  12478. "unknown data segment");
  12479. goto fail;
  12480. }
  12481. if (wasm_is_type_reftype(elem_type)) {
  12482. set_error_buf(error_buf, error_buf_size,
  12483. "array elem type mismatch");
  12484. goto fail;
  12485. }
  12486. }
  12487. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12488. WASMTableSeg *table_seg =
  12489. module->table_segments + u32;
  12490. /* offset of element segment */
  12491. POP_I32();
  12492. if (u32 >= module->table_seg_count) {
  12493. set_error_buf(error_buf, error_buf_size,
  12494. "unknown element segment");
  12495. goto fail;
  12496. }
  12497. if (!wasm_reftype_is_subtype_of(
  12498. table_seg->elem_type,
  12499. table_seg->elem_ref_type, elem_type,
  12500. array_type->elem_ref_type, module->types,
  12501. module->type_count)) {
  12502. set_error_buf(error_buf, error_buf_size,
  12503. "array elem type mismatch");
  12504. goto fail;
  12505. }
  12506. }
  12507. /* PUSH array obj, (ref $t) */
  12508. wasm_set_refheaptype_typeidx(
  12509. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12510. PUSH_REF(wasm_ref_type.ref_type);
  12511. break;
  12512. }
  12513. case WASM_OP_ARRAY_GET:
  12514. case WASM_OP_ARRAY_GET_S:
  12515. case WASM_OP_ARRAY_GET_U:
  12516. case WASM_OP_ARRAY_SET:
  12517. {
  12518. uint8 elem_type;
  12519. WASMArrayType *array_type;
  12520. WASMRefType *ref_type = NULL;
  12521. read_leb_uint32(p, p_end, type_idx);
  12522. #if WASM_ENABLE_FAST_INTERP != 0
  12523. emit_uint32(loader_ctx, type_idx);
  12524. #endif
  12525. if (!check_array_type(module, type_idx, error_buf,
  12526. error_buf_size)) {
  12527. goto fail;
  12528. }
  12529. array_type = (WASMArrayType *)module->types[type_idx];
  12530. if (opcode1 == WASM_OP_ARRAY_SET
  12531. && !(array_type->elem_flags & 1)) {
  12532. set_error_buf(error_buf, error_buf_size,
  12533. "array is immutable");
  12534. goto fail;
  12535. }
  12536. elem_type = array_type->elem_type;
  12537. if (is_packed_type(elem_type)) {
  12538. if (opcode1 != WASM_OP_ARRAY_GET_S
  12539. && opcode1 != WASM_OP_ARRAY_GET_U
  12540. && opcode1 != WASM_OP_ARRAY_SET) {
  12541. set_error_buf(error_buf, error_buf_size,
  12542. "type mismatch");
  12543. goto fail;
  12544. }
  12545. else {
  12546. elem_type = VALUE_TYPE_I32;
  12547. }
  12548. }
  12549. ref_type = array_type->elem_ref_type;
  12550. if (opcode1 == WASM_OP_ARRAY_SET) {
  12551. /* POP elem to set */
  12552. if (wasm_is_type_multi_byte_type(elem_type)) {
  12553. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12554. ref_type,
  12555. wasm_reftype_struct_size(ref_type));
  12556. }
  12557. POP_REF(elem_type);
  12558. }
  12559. /* elem idx */
  12560. POP_I32();
  12561. /* POP array obj, (ref null $t) */
  12562. wasm_set_refheaptype_typeidx(
  12563. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12564. POP_REF(wasm_ref_type.ref_type);
  12565. if (opcode1 != WASM_OP_ARRAY_SET) {
  12566. /* PUSH elem */
  12567. if (wasm_is_type_multi_byte_type(elem_type)) {
  12568. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12569. ref_type,
  12570. wasm_reftype_struct_size(ref_type));
  12571. }
  12572. PUSH_REF(elem_type);
  12573. }
  12574. break;
  12575. }
  12576. case WASM_OP_ARRAY_LEN:
  12577. {
  12578. POP_REF(REF_TYPE_ARRAYREF);
  12579. /* length */
  12580. PUSH_I32();
  12581. break;
  12582. }
  12583. case WASM_OP_ARRAY_FILL:
  12584. {
  12585. WASMArrayType *array_type;
  12586. uint8 elem_type;
  12587. /* typeidx */
  12588. read_leb_uint32(p, p_end, type_idx);
  12589. #if WASM_ENABLE_FAST_INTERP != 0
  12590. emit_uint32(loader_ctx, type_idx);
  12591. #endif
  12592. if (!check_array_type(module, type_idx, error_buf,
  12593. error_buf_size)) {
  12594. goto fail;
  12595. }
  12596. array_type = (WASMArrayType *)module->types[type_idx];
  12597. if (!(array_type->elem_flags & 1)) {
  12598. set_error_buf(error_buf, error_buf_size,
  12599. "array is immutable");
  12600. goto fail;
  12601. }
  12602. elem_type = array_type->elem_type;
  12603. if (is_packed_type(elem_type)) {
  12604. elem_type = VALUE_TYPE_I32;
  12605. }
  12606. POP_I32(); /* length */
  12607. #if WASM_ENABLE_FAST_INTERP != 0
  12608. POP_OFFSET_TYPE(elem_type);
  12609. #endif
  12610. POP_TYPE(elem_type);
  12611. POP_I32(); /* start */
  12612. /* POP array obj, (ref null $t) */
  12613. wasm_set_refheaptype_typeidx(
  12614. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12615. POP_REF(wasm_ref_type.ref_type);
  12616. break;
  12617. }
  12618. case WASM_OP_ARRAY_COPY:
  12619. {
  12620. uint32 src_type_idx;
  12621. uint8 src_elem_type, dst_elem_type;
  12622. WASMRefType src_ref_type = { 0 },
  12623. *src_elem_ref_type = NULL;
  12624. WASMRefType dst_ref_type = { 0 },
  12625. *dst_elem_ref_type = NULL;
  12626. WASMArrayType *array_type;
  12627. /* typeidx1 */
  12628. read_leb_uint32(p, p_end, type_idx);
  12629. #if WASM_ENABLE_FAST_INTERP != 0
  12630. emit_uint32(loader_ctx, type_idx);
  12631. #endif
  12632. /* typeidx2 */
  12633. read_leb_uint32(p, p_end, src_type_idx);
  12634. #if WASM_ENABLE_FAST_INTERP != 0
  12635. emit_uint32(loader_ctx, src_type_idx);
  12636. #endif
  12637. if (!check_array_type(module, type_idx, error_buf,
  12638. error_buf_size)) {
  12639. goto fail;
  12640. }
  12641. if (!check_array_type(module, src_type_idx, error_buf,
  12642. error_buf_size)) {
  12643. goto fail;
  12644. }
  12645. POP_I32();
  12646. POP_I32();
  12647. /* POP array obj, (ref null $t) */
  12648. wasm_set_refheaptype_typeidx(
  12649. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12650. POP_REF(wasm_ref_type.ref_type);
  12651. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12652. &wasm_ref_type,
  12653. wasm_reftype_struct_size(&wasm_ref_type));
  12654. POP_I32();
  12655. /* POP array obj, (ref null $t) */
  12656. wasm_set_refheaptype_typeidx(
  12657. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12658. POP_REF(wasm_ref_type.ref_type);
  12659. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12660. &wasm_ref_type,
  12661. wasm_reftype_struct_size(&wasm_ref_type));
  12662. array_type = (WASMArrayType *)module->types[type_idx];
  12663. if (!(array_type->elem_flags & 1)) {
  12664. set_error_buf(error_buf, error_buf_size,
  12665. "destination array is immutable");
  12666. goto fail;
  12667. }
  12668. dst_elem_type = array_type->elem_type;
  12669. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12670. dst_elem_ref_type = array_type->elem_ref_type;
  12671. }
  12672. array_type =
  12673. (WASMArrayType *)module->types[src_type_idx];
  12674. src_elem_type = array_type->elem_type;
  12675. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12676. src_elem_ref_type = array_type->elem_ref_type;
  12677. }
  12678. if (!wasm_reftype_is_subtype_of(
  12679. src_elem_type, src_elem_ref_type, dst_elem_type,
  12680. dst_elem_ref_type, module->types,
  12681. module->type_count)) {
  12682. set_error_buf(error_buf, error_buf_size,
  12683. "array types do not match");
  12684. goto fail;
  12685. }
  12686. break;
  12687. }
  12688. case WASM_OP_REF_I31:
  12689. {
  12690. POP_I32();
  12691. wasm_set_refheaptype_common(
  12692. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12693. PUSH_REF(wasm_ref_type.ref_type);
  12694. break;
  12695. }
  12696. case WASM_OP_I31_GET_S:
  12697. case WASM_OP_I31_GET_U:
  12698. {
  12699. POP_REF(REF_TYPE_I31REF);
  12700. PUSH_I32();
  12701. break;
  12702. }
  12703. case WASM_OP_REF_TEST:
  12704. case WASM_OP_REF_CAST:
  12705. case WASM_OP_REF_TEST_NULLABLE:
  12706. case WASM_OP_REF_CAST_NULLABLE:
  12707. {
  12708. uint8 type;
  12709. read_leb_int32(p, p_end, heap_type);
  12710. #if WASM_ENABLE_FAST_INTERP != 0
  12711. emit_uint32(loader_ctx, (uint32)heap_type);
  12712. #endif
  12713. if (heap_type >= 0) {
  12714. if (!check_type_index(module, module->type_count,
  12715. heap_type, error_buf,
  12716. error_buf_size)) {
  12717. goto fail;
  12718. }
  12719. }
  12720. else {
  12721. if (!wasm_is_valid_heap_type(heap_type)) {
  12722. set_error_buf(error_buf, error_buf_size,
  12723. "unknown type");
  12724. goto fail;
  12725. }
  12726. }
  12727. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12728. &wasm_ref_type, error_buf,
  12729. error_buf_size)) {
  12730. goto fail;
  12731. }
  12732. if (opcode1 == WASM_OP_REF_TEST
  12733. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12734. PUSH_I32();
  12735. else {
  12736. bool nullable =
  12737. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12738. : false;
  12739. if (heap_type >= 0 || !nullable) {
  12740. wasm_set_refheaptype_typeidx(
  12741. &wasm_ref_type.ref_ht_typeidx, nullable,
  12742. heap_type);
  12743. PUSH_REF(wasm_ref_type.ref_type);
  12744. }
  12745. else {
  12746. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12747. }
  12748. }
  12749. break;
  12750. }
  12751. case WASM_OP_BR_ON_CAST:
  12752. case WASM_OP_BR_ON_CAST_FAIL:
  12753. {
  12754. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12755. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12756. uint8 type_tmp, castflags;
  12757. uint32 depth;
  12758. int32 heap_type_dst;
  12759. bool src_nullable, dst_nullable;
  12760. CHECK_BUF(p, p_end, 1);
  12761. castflags = read_uint8(p);
  12762. #if WASM_ENABLE_FAST_INTERP != 0
  12763. /* Emit heap_type firstly */
  12764. emit_byte(loader_ctx, castflags);
  12765. #endif
  12766. p_org = p;
  12767. read_leb_uint32(p, p_end, depth);
  12768. read_leb_int32(p, p_end, heap_type);
  12769. #if WASM_ENABLE_FAST_INTERP != 0
  12770. /* Emit heap_type firstly */
  12771. emit_uint32(loader_ctx, (uint32)heap_type);
  12772. #endif
  12773. read_leb_int32(p, p_end, heap_type_dst);
  12774. #if WASM_ENABLE_FAST_INTERP != 0
  12775. /* Emit heap_type firstly */
  12776. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12777. #endif
  12778. (void)depth;
  12779. /*
  12780. * castflags should be 0~3:
  12781. * 0: (non-null, non-null)
  12782. * 1: (null, non-null)
  12783. * 2: (non-null, null)
  12784. * 3: (null, null)
  12785. */
  12786. if (castflags > 3) {
  12787. set_error_buf(error_buf, error_buf_size,
  12788. "invalid castflags");
  12789. break;
  12790. }
  12791. src_nullable =
  12792. (castflags == 1) || (castflags == 3) ? true : false;
  12793. dst_nullable =
  12794. (castflags == 2) || (castflags == 3) ? true : false;
  12795. /* Pop and backup the stack top's ref type */
  12796. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12797. &ref_type_tmp, error_buf,
  12798. error_buf_size)) {
  12799. goto fail;
  12800. }
  12801. /* The reference type rt1 must be valid */
  12802. if (!init_ref_type(module, &ref_type1, src_nullable,
  12803. heap_type, error_buf,
  12804. error_buf_size)) {
  12805. goto fail;
  12806. }
  12807. /* The reference type rt2 must be valid. */
  12808. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12809. heap_type_dst, error_buf,
  12810. error_buf_size)) {
  12811. goto fail;
  12812. }
  12813. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12814. &ref_type2);
  12815. /* The reference type rt2 must match rt1. */
  12816. if (!wasm_reftype_is_subtype_of(
  12817. ref_type2.ref_type, &ref_type2,
  12818. ref_type1.ref_type, &ref_type1, module->types,
  12819. module->type_count)) {
  12820. set_error_buf(error_buf, error_buf_size,
  12821. "type mismatch");
  12822. goto fail;
  12823. }
  12824. p = p_org;
  12825. /* Push ref type casted for branch block check */
  12826. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12827. /* The reference type rt2 must match rt′. */
  12828. type_tmp = ref_type2.ref_type;
  12829. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12830. bh_memcpy_s(
  12831. &wasm_ref_type,
  12832. wasm_reftype_struct_size(&ref_type2),
  12833. &ref_type2,
  12834. wasm_reftype_struct_size(&ref_type2));
  12835. }
  12836. }
  12837. else {
  12838. /* The reference type rt′1 must match rt′. */
  12839. type_tmp = ref_type_diff.ref_type;
  12840. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12841. bh_memcpy_s(
  12842. &wasm_ref_type,
  12843. wasm_reftype_struct_size(&ref_type_diff),
  12844. &ref_type_diff,
  12845. wasm_reftype_struct_size(&ref_type_diff));
  12846. }
  12847. }
  12848. PUSH_REF(type_tmp);
  12849. if (!(frame_csp_tmp = check_branch_block(
  12850. loader_ctx, &p, p_end, opcode, error_buf,
  12851. error_buf_size))) {
  12852. goto fail;
  12853. }
  12854. /* Ignore heap_types */
  12855. skip_leb_uint32(p, p_end);
  12856. skip_leb_uint32(p, p_end);
  12857. /* Restore the original stack top's ref type */
  12858. POP_REF(type_tmp);
  12859. #if WASM_ENABLE_FAST_INTERP != 0
  12860. /* Erase the opnd offset emitted by POP_REF() */
  12861. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12862. #endif
  12863. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12864. type_tmp = ref_type_diff.ref_type;
  12865. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12866. bh_memcpy_s(
  12867. &wasm_ref_type,
  12868. wasm_reftype_struct_size(&ref_type_diff),
  12869. &ref_type_diff,
  12870. wasm_reftype_struct_size(&ref_type_diff));
  12871. }
  12872. }
  12873. else {
  12874. type_tmp = ref_type2.ref_type;
  12875. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12876. bh_memcpy_s(
  12877. &wasm_ref_type,
  12878. wasm_reftype_struct_size(&ref_type2),
  12879. &ref_type2,
  12880. wasm_reftype_struct_size(&ref_type2));
  12881. }
  12882. }
  12883. PUSH_REF(type_tmp);
  12884. #if WASM_ENABLE_FAST_INTERP != 0
  12885. /* Erase the opnd offset emitted by PUSH_REF() */
  12886. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12887. #endif
  12888. break;
  12889. }
  12890. case WASM_OP_ANY_CONVERT_EXTERN:
  12891. {
  12892. uint8 type;
  12893. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12894. &wasm_ref_type, error_buf,
  12895. error_buf_size)) {
  12896. goto fail;
  12897. }
  12898. if (!(type == REF_TYPE_EXTERNREF
  12899. || (type == REF_TYPE_HT_NON_NULLABLE
  12900. && wasm_ref_type.ref_ht_common.heap_type
  12901. == HEAP_TYPE_EXTERN)
  12902. || type == VALUE_TYPE_ANY)) {
  12903. set_error_buf(error_buf, error_buf_size,
  12904. "type mismatch");
  12905. goto fail;
  12906. }
  12907. if (type == REF_TYPE_EXTERNREF)
  12908. type = REF_TYPE_ANYREF;
  12909. else {
  12910. wasm_ref_type.ref_ht_common.heap_type =
  12911. HEAP_TYPE_ANY;
  12912. }
  12913. PUSH_REF(type);
  12914. break;
  12915. }
  12916. case WASM_OP_EXTERN_CONVERT_ANY:
  12917. {
  12918. uint8 type;
  12919. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12920. &wasm_ref_type, error_buf,
  12921. error_buf_size)) {
  12922. goto fail;
  12923. }
  12924. if (type == REF_TYPE_EXTERNREF
  12925. || ((type == REF_TYPE_HT_NULLABLE
  12926. || type == REF_TYPE_HT_NON_NULLABLE)
  12927. && wasm_ref_type.ref_ht_common.heap_type
  12928. == HEAP_TYPE_EXTERN)) {
  12929. set_error_buf(error_buf, error_buf_size,
  12930. "type mismatch");
  12931. goto fail;
  12932. }
  12933. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12934. /* push (ref null extern) */
  12935. type = REF_TYPE_EXTERNREF;
  12936. }
  12937. else {
  12938. /* push (ref extern) */
  12939. type = REF_TYPE_HT_NON_NULLABLE;
  12940. wasm_set_refheaptype_common(
  12941. &wasm_ref_type.ref_ht_common, false,
  12942. HEAP_TYPE_EXTERN);
  12943. }
  12944. PUSH_REF(type);
  12945. break;
  12946. }
  12947. #if WASM_ENABLE_STRINGREF != 0
  12948. case WASM_OP_STRING_NEW_UTF8:
  12949. case WASM_OP_STRING_NEW_WTF16:
  12950. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12951. case WASM_OP_STRING_NEW_WTF8:
  12952. {
  12953. uint32 memidx;
  12954. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12955. func->has_memory_operations = true;
  12956. #endif
  12957. read_leb_uint32(p, p_end, memidx);
  12958. #if WASM_ENABLE_FAST_INTERP != 0
  12959. emit_uint32(loader_ctx, (uint32)memidx);
  12960. #endif
  12961. POP_I32();
  12962. POP_I32();
  12963. PUSH_REF(REF_TYPE_STRINGREF);
  12964. (void)memidx;
  12965. break;
  12966. }
  12967. case WASM_OP_STRING_CONST:
  12968. {
  12969. uint32 contents;
  12970. read_leb_uint32(p, p_end, contents);
  12971. #if WASM_ENABLE_FAST_INTERP != 0
  12972. emit_uint32(loader_ctx, (uint32)contents);
  12973. #endif
  12974. PUSH_REF(REF_TYPE_STRINGREF);
  12975. (void)contents;
  12976. break;
  12977. }
  12978. case WASM_OP_STRING_MEASURE_UTF8:
  12979. case WASM_OP_STRING_MEASURE_WTF8:
  12980. case WASM_OP_STRING_MEASURE_WTF16:
  12981. {
  12982. POP_STRINGREF();
  12983. PUSH_I32();
  12984. break;
  12985. }
  12986. case WASM_OP_STRING_ENCODE_UTF8:
  12987. case WASM_OP_STRING_ENCODE_WTF16:
  12988. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  12989. case WASM_OP_STRING_ENCODE_WTF8:
  12990. {
  12991. uint32 memidx;
  12992. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12993. func->has_memory_operations = true;
  12994. #endif
  12995. read_leb_uint32(p, p_end, memidx);
  12996. #if WASM_ENABLE_FAST_INTERP != 0
  12997. emit_uint32(loader_ctx, (uint32)memidx);
  12998. #endif
  12999. POP_I32();
  13000. POP_STRINGREF();
  13001. PUSH_I32();
  13002. (void)memidx;
  13003. break;
  13004. }
  13005. case WASM_OP_STRING_CONCAT:
  13006. {
  13007. POP_STRINGREF();
  13008. POP_STRINGREF();
  13009. PUSH_REF(REF_TYPE_STRINGREF);
  13010. break;
  13011. }
  13012. case WASM_OP_STRING_EQ:
  13013. {
  13014. POP_STRINGREF();
  13015. POP_STRINGREF();
  13016. PUSH_I32();
  13017. break;
  13018. }
  13019. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13020. {
  13021. POP_STRINGREF();
  13022. PUSH_I32();
  13023. break;
  13024. }
  13025. case WASM_OP_STRING_AS_WTF8:
  13026. {
  13027. POP_STRINGREF();
  13028. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13029. break;
  13030. }
  13031. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13032. {
  13033. POP_I32();
  13034. POP_I32();
  13035. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13036. PUSH_I32();
  13037. break;
  13038. }
  13039. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13040. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13041. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13042. {
  13043. uint32 memidx;
  13044. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13045. func->has_memory_operations = true;
  13046. #endif
  13047. read_leb_uint32(p, p_end, memidx);
  13048. #if WASM_ENABLE_FAST_INTERP != 0
  13049. emit_uint32(loader_ctx, (uint32)memidx);
  13050. #endif
  13051. POP_I32();
  13052. POP_I32();
  13053. POP_I32();
  13054. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13055. PUSH_I32();
  13056. PUSH_I32();
  13057. (void)memidx;
  13058. break;
  13059. }
  13060. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13061. {
  13062. POP_I32();
  13063. POP_I32();
  13064. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13065. PUSH_REF(REF_TYPE_STRINGREF);
  13066. break;
  13067. }
  13068. case WASM_OP_STRING_AS_WTF16:
  13069. {
  13070. POP_STRINGREF();
  13071. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13072. break;
  13073. }
  13074. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13075. {
  13076. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13077. PUSH_I32();
  13078. break;
  13079. }
  13080. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13081. {
  13082. POP_I32();
  13083. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13084. PUSH_I32();
  13085. break;
  13086. }
  13087. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13088. {
  13089. uint32 memidx;
  13090. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13091. func->has_memory_operations = true;
  13092. #endif
  13093. read_leb_uint32(p, p_end, memidx);
  13094. #if WASM_ENABLE_FAST_INTERP != 0
  13095. emit_uint32(loader_ctx, (uint32)memidx);
  13096. #endif
  13097. POP_I32();
  13098. POP_I32();
  13099. POP_I32();
  13100. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13101. PUSH_I32();
  13102. (void)memidx;
  13103. break;
  13104. }
  13105. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13106. {
  13107. POP_I32();
  13108. POP_I32();
  13109. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13110. PUSH_REF(REF_TYPE_STRINGREF);
  13111. break;
  13112. }
  13113. case WASM_OP_STRING_AS_ITER:
  13114. {
  13115. POP_STRINGREF();
  13116. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13117. break;
  13118. }
  13119. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13120. {
  13121. POP_REF(REF_TYPE_STRINGVIEWITER);
  13122. PUSH_I32();
  13123. break;
  13124. }
  13125. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13126. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13127. {
  13128. POP_I32();
  13129. POP_REF(REF_TYPE_STRINGVIEWITER);
  13130. PUSH_I32();
  13131. break;
  13132. }
  13133. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13134. {
  13135. POP_I32();
  13136. POP_REF(REF_TYPE_STRINGVIEWITER);
  13137. PUSH_REF(REF_TYPE_STRINGREF);
  13138. break;
  13139. }
  13140. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13141. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13142. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13143. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13144. {
  13145. POP_I32();
  13146. POP_I32();
  13147. POP_REF(REF_TYPE_ARRAYREF);
  13148. PUSH_REF(REF_TYPE_STRINGREF);
  13149. break;
  13150. }
  13151. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13152. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13153. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13154. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13155. {
  13156. POP_I32();
  13157. POP_REF(REF_TYPE_ARRAYREF);
  13158. POP_STRINGREF();
  13159. PUSH_I32();
  13160. break;
  13161. }
  13162. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13163. default:
  13164. set_error_buf_v(error_buf, error_buf_size,
  13165. "%s %02x %02x", "unsupported opcode",
  13166. 0xfb, opcode1);
  13167. goto fail;
  13168. }
  13169. break;
  13170. }
  13171. #endif /* end of WASM_ENABLE_GC != 0 */
  13172. case WASM_OP_MISC_PREFIX:
  13173. {
  13174. uint32 opcode1;
  13175. read_leb_uint32(p, p_end, opcode1);
  13176. #if WASM_ENABLE_FAST_INTERP != 0
  13177. emit_byte(loader_ctx, ((uint8)opcode1));
  13178. #endif
  13179. switch (opcode1) {
  13180. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13181. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13182. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13183. break;
  13184. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13185. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13186. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13187. break;
  13188. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13189. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13190. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13191. break;
  13192. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13193. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13194. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13195. break;
  13196. #if WASM_ENABLE_BULK_MEMORY != 0
  13197. case WASM_OP_MEMORY_INIT:
  13198. {
  13199. read_leb_uint32(p, p_end, data_seg_idx);
  13200. #if WASM_ENABLE_FAST_INTERP != 0
  13201. emit_uint32(loader_ctx, data_seg_idx);
  13202. #endif
  13203. if (module->import_memory_count == 0
  13204. && module->memory_count == 0)
  13205. goto fail_unknown_memory;
  13206. read_leb_uint32(p, p_end, memidx);
  13207. check_memidx(module, memidx);
  13208. if (data_seg_idx >= module->data_seg_count) {
  13209. set_error_buf_v(error_buf, error_buf_size,
  13210. "unknown data segment %d",
  13211. data_seg_idx);
  13212. goto fail;
  13213. }
  13214. if (module->data_seg_count1 == 0)
  13215. goto fail_data_cnt_sec_require;
  13216. POP_I32();
  13217. POP_I32();
  13218. POP_MEM_OFFSET();
  13219. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13220. func->has_memory_operations = true;
  13221. #endif
  13222. #if WASM_ENABLE_WAMR_COMPILER != 0
  13223. module->is_bulk_memory_used = true;
  13224. #endif
  13225. break;
  13226. }
  13227. case WASM_OP_DATA_DROP:
  13228. {
  13229. read_leb_uint32(p, p_end, data_seg_idx);
  13230. #if WASM_ENABLE_FAST_INTERP != 0
  13231. emit_uint32(loader_ctx, data_seg_idx);
  13232. #endif
  13233. if (data_seg_idx >= module->data_seg_count) {
  13234. set_error_buf(error_buf, error_buf_size,
  13235. "unknown data segment");
  13236. goto fail;
  13237. }
  13238. if (module->data_seg_count1 == 0)
  13239. goto fail_data_cnt_sec_require;
  13240. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13241. func->has_memory_operations = true;
  13242. #endif
  13243. #if WASM_ENABLE_WAMR_COMPILER != 0
  13244. module->is_bulk_memory_used = true;
  13245. #endif
  13246. break;
  13247. }
  13248. case WASM_OP_MEMORY_COPY:
  13249. {
  13250. CHECK_BUF(p, p_end, sizeof(int16));
  13251. /* check both src and dst memory index */
  13252. read_leb_uint32(p, p_end, memidx);
  13253. check_memidx(module, memidx);
  13254. read_leb_uint32(p, p_end, memidx);
  13255. check_memidx(module, memidx);
  13256. if (module->import_memory_count == 0
  13257. && module->memory_count == 0)
  13258. goto fail_unknown_memory;
  13259. POP_MEM_OFFSET();
  13260. POP_MEM_OFFSET();
  13261. POP_MEM_OFFSET();
  13262. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13263. func->has_memory_operations = true;
  13264. #endif
  13265. #if WASM_ENABLE_WAMR_COMPILER != 0
  13266. module->is_bulk_memory_used = true;
  13267. #endif
  13268. break;
  13269. }
  13270. case WASM_OP_MEMORY_FILL:
  13271. {
  13272. read_leb_uint32(p, p_end, memidx);
  13273. check_memidx(module, memidx);
  13274. if (module->import_memory_count == 0
  13275. && module->memory_count == 0) {
  13276. goto fail_unknown_memory;
  13277. }
  13278. POP_MEM_OFFSET();
  13279. POP_I32();
  13280. POP_MEM_OFFSET();
  13281. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13282. func->has_memory_operations = true;
  13283. #endif
  13284. #if WASM_ENABLE_WAMR_COMPILER != 0
  13285. module->is_bulk_memory_used = true;
  13286. #endif
  13287. break;
  13288. }
  13289. fail_unknown_memory:
  13290. set_error_buf(error_buf, error_buf_size,
  13291. "unknown memory 0");
  13292. goto fail;
  13293. fail_data_cnt_sec_require:
  13294. set_error_buf(error_buf, error_buf_size,
  13295. "data count section required");
  13296. goto fail;
  13297. #endif /* WASM_ENABLE_BULK_MEMORY */
  13298. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13299. case WASM_OP_TABLE_INIT:
  13300. {
  13301. uint8 seg_type = 0, tbl_type = 0;
  13302. #if WASM_ENABLE_GC != 0
  13303. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13304. #endif
  13305. read_leb_uint32(p, p_end, table_seg_idx);
  13306. read_leb_uint32(p, p_end, table_idx);
  13307. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13308. #if WASM_ENABLE_GC != 0
  13309. (void **)&tbl_ref_type,
  13310. #else
  13311. NULL,
  13312. #endif
  13313. error_buf, error_buf_size))
  13314. goto fail;
  13315. if (!get_table_seg_elem_type(module, table_seg_idx,
  13316. &seg_type,
  13317. #if WASM_ENABLE_GC != 0
  13318. (void **)&seg_ref_type,
  13319. #else
  13320. NULL,
  13321. #endif
  13322. error_buf, error_buf_size))
  13323. goto fail;
  13324. #if WASM_ENABLE_GC == 0
  13325. if (seg_type != tbl_type) {
  13326. set_error_buf(error_buf, error_buf_size,
  13327. "type mismatch");
  13328. goto fail;
  13329. }
  13330. #else
  13331. if (!wasm_reftype_is_subtype_of(
  13332. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13333. module->types, module->type_count)) {
  13334. set_error_buf(error_buf, error_buf_size,
  13335. "type mismatch");
  13336. goto fail;
  13337. }
  13338. #endif
  13339. #if WASM_ENABLE_FAST_INTERP != 0
  13340. emit_uint32(loader_ctx, table_seg_idx);
  13341. emit_uint32(loader_ctx, table_idx);
  13342. #endif
  13343. POP_I32();
  13344. POP_I32();
  13345. POP_I32();
  13346. #if WASM_ENABLE_WAMR_COMPILER != 0
  13347. module->is_ref_types_used = true;
  13348. #endif
  13349. break;
  13350. }
  13351. case WASM_OP_ELEM_DROP:
  13352. {
  13353. read_leb_uint32(p, p_end, table_seg_idx);
  13354. if (!get_table_seg_elem_type(module, table_seg_idx,
  13355. NULL, NULL, error_buf,
  13356. error_buf_size))
  13357. goto fail;
  13358. #if WASM_ENABLE_FAST_INTERP != 0
  13359. emit_uint32(loader_ctx, table_seg_idx);
  13360. #endif
  13361. #if WASM_ENABLE_WAMR_COMPILER != 0
  13362. module->is_ref_types_used = true;
  13363. #endif
  13364. break;
  13365. }
  13366. case WASM_OP_TABLE_COPY:
  13367. {
  13368. uint8 src_type, dst_type;
  13369. #if WASM_ENABLE_GC != 0
  13370. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13371. #endif
  13372. uint32 src_tbl_idx, dst_tbl_idx;
  13373. read_leb_uint32(p, p_end, dst_tbl_idx);
  13374. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13375. #if WASM_ENABLE_GC != 0
  13376. (void **)&dst_ref_type,
  13377. #else
  13378. NULL,
  13379. #endif
  13380. error_buf, error_buf_size))
  13381. goto fail;
  13382. read_leb_uint32(p, p_end, src_tbl_idx);
  13383. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13384. #if WASM_ENABLE_GC != 0
  13385. (void **)&src_ref_type,
  13386. #else
  13387. NULL,
  13388. #endif
  13389. error_buf, error_buf_size))
  13390. goto fail;
  13391. #if WASM_ENABLE_GC == 0
  13392. if (src_type != dst_type) {
  13393. set_error_buf(error_buf, error_buf_size,
  13394. "type mismatch");
  13395. goto fail;
  13396. }
  13397. #else
  13398. if (!wasm_reftype_is_subtype_of(
  13399. src_type, src_ref_type, dst_type, dst_ref_type,
  13400. module->types, module->type_count)) {
  13401. set_error_buf(error_buf, error_buf_size,
  13402. "type mismatch");
  13403. goto fail;
  13404. }
  13405. #endif
  13406. #if WASM_ENABLE_FAST_INTERP != 0
  13407. emit_uint32(loader_ctx, dst_tbl_idx);
  13408. emit_uint32(loader_ctx, src_tbl_idx);
  13409. #endif
  13410. POP_I32();
  13411. POP_I32();
  13412. POP_I32();
  13413. #if WASM_ENABLE_WAMR_COMPILER != 0
  13414. module->is_ref_types_used = true;
  13415. #endif
  13416. break;
  13417. }
  13418. case WASM_OP_TABLE_SIZE:
  13419. {
  13420. read_leb_uint32(p, p_end, table_idx);
  13421. /* TODO: shall we create a new function to check
  13422. table idx instead of using below function? */
  13423. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13424. error_buf, error_buf_size))
  13425. goto fail;
  13426. #if WASM_ENABLE_FAST_INTERP != 0
  13427. emit_uint32(loader_ctx, table_idx);
  13428. #endif
  13429. PUSH_I32();
  13430. #if WASM_ENABLE_WAMR_COMPILER != 0
  13431. module->is_ref_types_used = true;
  13432. #endif
  13433. break;
  13434. }
  13435. case WASM_OP_TABLE_GROW:
  13436. case WASM_OP_TABLE_FILL:
  13437. {
  13438. uint8 decl_type;
  13439. #if WASM_ENABLE_GC != 0
  13440. WASMRefType *ref_type = NULL;
  13441. #endif
  13442. read_leb_uint32(p, p_end, table_idx);
  13443. if (!get_table_elem_type(module, table_idx, &decl_type,
  13444. #if WASM_ENABLE_GC != 0
  13445. (void **)&ref_type,
  13446. #else
  13447. NULL,
  13448. #endif
  13449. error_buf, error_buf_size))
  13450. goto fail;
  13451. #if WASM_ENABLE_GC != 0
  13452. if (wasm_is_type_multi_byte_type(decl_type)) {
  13453. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13454. ref_type,
  13455. wasm_reftype_struct_size(ref_type));
  13456. }
  13457. #endif
  13458. if (opcode1 == WASM_OP_TABLE_GROW) {
  13459. if (table_idx < module->import_table_count) {
  13460. module->import_tables[table_idx]
  13461. .u.table.table_type.possible_grow = true;
  13462. }
  13463. else {
  13464. module
  13465. ->tables[table_idx
  13466. - module->import_table_count]
  13467. .table_type.possible_grow = true;
  13468. }
  13469. }
  13470. #if WASM_ENABLE_FAST_INTERP != 0
  13471. emit_uint32(loader_ctx, table_idx);
  13472. #endif
  13473. POP_I32();
  13474. #if WASM_ENABLE_FAST_INTERP != 0
  13475. POP_OFFSET_TYPE(decl_type);
  13476. #endif
  13477. POP_TYPE(decl_type);
  13478. if (opcode1 == WASM_OP_TABLE_GROW)
  13479. PUSH_I32();
  13480. else
  13481. POP_I32();
  13482. #if WASM_ENABLE_WAMR_COMPILER != 0
  13483. module->is_ref_types_used = true;
  13484. #endif
  13485. break;
  13486. }
  13487. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13488. default:
  13489. set_error_buf_v(error_buf, error_buf_size,
  13490. "%s %02x %02x", "unsupported opcode",
  13491. 0xfc, opcode1);
  13492. goto fail;
  13493. }
  13494. break;
  13495. }
  13496. #if WASM_ENABLE_SIMD != 0
  13497. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13498. case WASM_OP_SIMD_PREFIX:
  13499. {
  13500. uint32 opcode1;
  13501. #if WASM_ENABLE_WAMR_COMPILER != 0
  13502. /* Mark the SIMD instruction is used in this module */
  13503. module->is_simd_used = true;
  13504. #endif
  13505. read_leb_uint32(p, p_end, opcode1);
  13506. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13507. */
  13508. switch (opcode1) {
  13509. /* memory instruction */
  13510. case SIMD_v128_load:
  13511. case SIMD_v128_load8x8_s:
  13512. case SIMD_v128_load8x8_u:
  13513. case SIMD_v128_load16x4_s:
  13514. case SIMD_v128_load16x4_u:
  13515. case SIMD_v128_load32x2_s:
  13516. case SIMD_v128_load32x2_u:
  13517. case SIMD_v128_load8_splat:
  13518. case SIMD_v128_load16_splat:
  13519. case SIMD_v128_load32_splat:
  13520. case SIMD_v128_load64_splat:
  13521. {
  13522. CHECK_MEMORY();
  13523. read_leb_uint32(p, p_end, align); /* align */
  13524. if (!check_simd_memory_access_align(
  13525. opcode1, align, error_buf, error_buf_size)) {
  13526. goto fail;
  13527. }
  13528. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13529. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13530. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13531. func->has_memory_operations = true;
  13532. #endif
  13533. break;
  13534. }
  13535. case SIMD_v128_store:
  13536. {
  13537. CHECK_MEMORY();
  13538. read_leb_uint32(p, p_end, align); /* align */
  13539. if (!check_simd_memory_access_align(
  13540. opcode1, align, error_buf, error_buf_size)) {
  13541. goto fail;
  13542. }
  13543. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13544. POP_V128();
  13545. POP_MEM_OFFSET();
  13546. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13547. func->has_memory_operations = true;
  13548. #endif
  13549. break;
  13550. }
  13551. /* basic operation */
  13552. case SIMD_v128_const:
  13553. {
  13554. CHECK_BUF1(p, p_end, 16);
  13555. p += 16;
  13556. PUSH_V128();
  13557. break;
  13558. }
  13559. case SIMD_v8x16_shuffle:
  13560. {
  13561. V128 mask;
  13562. CHECK_BUF1(p, p_end, 16);
  13563. mask = read_i8x16(p, error_buf, error_buf_size);
  13564. p += 16;
  13565. if (!check_simd_shuffle_mask(mask, error_buf,
  13566. error_buf_size)) {
  13567. goto fail;
  13568. }
  13569. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13570. break;
  13571. }
  13572. case SIMD_v8x16_swizzle:
  13573. {
  13574. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13575. break;
  13576. }
  13577. /* splat operation */
  13578. case SIMD_i8x16_splat:
  13579. case SIMD_i16x8_splat:
  13580. case SIMD_i32x4_splat:
  13581. case SIMD_i64x2_splat:
  13582. case SIMD_f32x4_splat:
  13583. case SIMD_f64x2_splat:
  13584. {
  13585. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13586. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13587. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13588. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13589. VALUE_TYPE_V128);
  13590. break;
  13591. }
  13592. /* lane operation */
  13593. case SIMD_i8x16_extract_lane_s:
  13594. case SIMD_i8x16_extract_lane_u:
  13595. case SIMD_i8x16_replace_lane:
  13596. case SIMD_i16x8_extract_lane_s:
  13597. case SIMD_i16x8_extract_lane_u:
  13598. case SIMD_i16x8_replace_lane:
  13599. case SIMD_i32x4_extract_lane:
  13600. case SIMD_i32x4_replace_lane:
  13601. case SIMD_i64x2_extract_lane:
  13602. case SIMD_i64x2_replace_lane:
  13603. case SIMD_f32x4_extract_lane:
  13604. case SIMD_f32x4_replace_lane:
  13605. case SIMD_f64x2_extract_lane:
  13606. case SIMD_f64x2_replace_lane:
  13607. {
  13608. uint8 lane;
  13609. /* clang-format off */
  13610. uint8 replace[] = {
  13611. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13612. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13613. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13614. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13615. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13616. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13617. };
  13618. uint8 push_type[] = {
  13619. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13620. VALUE_TYPE_V128,
  13621. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13622. VALUE_TYPE_V128,
  13623. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13624. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13625. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13626. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13627. };
  13628. /* clang-format on */
  13629. CHECK_BUF(p, p_end, 1);
  13630. lane = read_uint8(p);
  13631. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13632. error_buf_size)) {
  13633. goto fail;
  13634. }
  13635. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13636. if (!(wasm_loader_pop_frame_ref(
  13637. loader_ctx,
  13638. replace[opcode1
  13639. - SIMD_i8x16_extract_lane_s],
  13640. error_buf, error_buf_size)))
  13641. goto fail;
  13642. }
  13643. POP_AND_PUSH(
  13644. VALUE_TYPE_V128,
  13645. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13646. break;
  13647. }
  13648. /* i8x16 compare operation */
  13649. case SIMD_i8x16_eq:
  13650. case SIMD_i8x16_ne:
  13651. case SIMD_i8x16_lt_s:
  13652. case SIMD_i8x16_lt_u:
  13653. case SIMD_i8x16_gt_s:
  13654. case SIMD_i8x16_gt_u:
  13655. case SIMD_i8x16_le_s:
  13656. case SIMD_i8x16_le_u:
  13657. case SIMD_i8x16_ge_s:
  13658. case SIMD_i8x16_ge_u:
  13659. /* i16x8 compare operation */
  13660. case SIMD_i16x8_eq:
  13661. case SIMD_i16x8_ne:
  13662. case SIMD_i16x8_lt_s:
  13663. case SIMD_i16x8_lt_u:
  13664. case SIMD_i16x8_gt_s:
  13665. case SIMD_i16x8_gt_u:
  13666. case SIMD_i16x8_le_s:
  13667. case SIMD_i16x8_le_u:
  13668. case SIMD_i16x8_ge_s:
  13669. case SIMD_i16x8_ge_u:
  13670. /* i32x4 compare operation */
  13671. case SIMD_i32x4_eq:
  13672. case SIMD_i32x4_ne:
  13673. case SIMD_i32x4_lt_s:
  13674. case SIMD_i32x4_lt_u:
  13675. case SIMD_i32x4_gt_s:
  13676. case SIMD_i32x4_gt_u:
  13677. case SIMD_i32x4_le_s:
  13678. case SIMD_i32x4_le_u:
  13679. case SIMD_i32x4_ge_s:
  13680. case SIMD_i32x4_ge_u:
  13681. /* f32x4 compare operation */
  13682. case SIMD_f32x4_eq:
  13683. case SIMD_f32x4_ne:
  13684. case SIMD_f32x4_lt:
  13685. case SIMD_f32x4_gt:
  13686. case SIMD_f32x4_le:
  13687. case SIMD_f32x4_ge:
  13688. /* f64x2 compare operation */
  13689. case SIMD_f64x2_eq:
  13690. case SIMD_f64x2_ne:
  13691. case SIMD_f64x2_lt:
  13692. case SIMD_f64x2_gt:
  13693. case SIMD_f64x2_le:
  13694. case SIMD_f64x2_ge:
  13695. {
  13696. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13697. break;
  13698. }
  13699. /* v128 operation */
  13700. case SIMD_v128_not:
  13701. {
  13702. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13703. break;
  13704. }
  13705. case SIMD_v128_and:
  13706. case SIMD_v128_andnot:
  13707. case SIMD_v128_or:
  13708. case SIMD_v128_xor:
  13709. {
  13710. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13711. break;
  13712. }
  13713. case SIMD_v128_bitselect:
  13714. {
  13715. POP_V128();
  13716. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13717. break;
  13718. }
  13719. case SIMD_v128_any_true:
  13720. {
  13721. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13722. break;
  13723. }
  13724. /* Load Lane Operation */
  13725. case SIMD_v128_load8_lane:
  13726. case SIMD_v128_load16_lane:
  13727. case SIMD_v128_load32_lane:
  13728. case SIMD_v128_load64_lane:
  13729. case SIMD_v128_store8_lane:
  13730. case SIMD_v128_store16_lane:
  13731. case SIMD_v128_store32_lane:
  13732. case SIMD_v128_store64_lane:
  13733. {
  13734. uint8 lane;
  13735. CHECK_MEMORY();
  13736. read_leb_uint32(p, p_end, align); /* align */
  13737. if (!check_simd_memory_access_align(
  13738. opcode1, align, error_buf, error_buf_size)) {
  13739. goto fail;
  13740. }
  13741. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13742. CHECK_BUF(p, p_end, 1);
  13743. lane = read_uint8(p);
  13744. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13745. error_buf_size)) {
  13746. goto fail;
  13747. }
  13748. POP_V128();
  13749. POP_MEM_OFFSET();
  13750. if (opcode1 < SIMD_v128_store8_lane) {
  13751. PUSH_V128();
  13752. }
  13753. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13754. func->has_memory_operations = true;
  13755. #endif
  13756. break;
  13757. }
  13758. case SIMD_v128_load32_zero:
  13759. case SIMD_v128_load64_zero:
  13760. {
  13761. CHECK_MEMORY();
  13762. read_leb_uint32(p, p_end, align); /* align */
  13763. if (!check_simd_memory_access_align(
  13764. opcode1, align, error_buf, error_buf_size)) {
  13765. goto fail;
  13766. }
  13767. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13768. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13769. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13770. func->has_memory_operations = true;
  13771. #endif
  13772. break;
  13773. }
  13774. /* Float conversion */
  13775. case SIMD_f32x4_demote_f64x2_zero:
  13776. case SIMD_f64x2_promote_low_f32x4_zero:
  13777. {
  13778. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13779. break;
  13780. }
  13781. /* i8x16 Operation */
  13782. case SIMD_i8x16_abs:
  13783. case SIMD_i8x16_neg:
  13784. case SIMD_i8x16_popcnt:
  13785. {
  13786. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13787. break;
  13788. }
  13789. case SIMD_i8x16_all_true:
  13790. case SIMD_i8x16_bitmask:
  13791. {
  13792. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13793. break;
  13794. }
  13795. case SIMD_i8x16_narrow_i16x8_s:
  13796. case SIMD_i8x16_narrow_i16x8_u:
  13797. {
  13798. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13799. break;
  13800. }
  13801. case SIMD_f32x4_ceil:
  13802. case SIMD_f32x4_floor:
  13803. case SIMD_f32x4_trunc:
  13804. case SIMD_f32x4_nearest:
  13805. {
  13806. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13807. break;
  13808. }
  13809. case SIMD_i8x16_shl:
  13810. case SIMD_i8x16_shr_s:
  13811. case SIMD_i8x16_shr_u:
  13812. {
  13813. POP_I32();
  13814. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13815. break;
  13816. }
  13817. case SIMD_i8x16_add:
  13818. case SIMD_i8x16_add_sat_s:
  13819. case SIMD_i8x16_add_sat_u:
  13820. case SIMD_i8x16_sub:
  13821. case SIMD_i8x16_sub_sat_s:
  13822. case SIMD_i8x16_sub_sat_u:
  13823. {
  13824. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13825. break;
  13826. }
  13827. case SIMD_f64x2_ceil:
  13828. case SIMD_f64x2_floor:
  13829. {
  13830. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13831. break;
  13832. }
  13833. case SIMD_i8x16_min_s:
  13834. case SIMD_i8x16_min_u:
  13835. case SIMD_i8x16_max_s:
  13836. case SIMD_i8x16_max_u:
  13837. {
  13838. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13839. break;
  13840. }
  13841. case SIMD_f64x2_trunc:
  13842. {
  13843. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13844. break;
  13845. }
  13846. case SIMD_i8x16_avgr_u:
  13847. {
  13848. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13849. break;
  13850. }
  13851. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13852. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13853. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13854. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13855. /* i16x8 operation */
  13856. case SIMD_i16x8_abs:
  13857. case SIMD_i16x8_neg:
  13858. {
  13859. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13860. break;
  13861. }
  13862. case SIMD_i16x8_q15mulr_sat_s:
  13863. {
  13864. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13865. break;
  13866. }
  13867. case SIMD_i16x8_all_true:
  13868. case SIMD_i16x8_bitmask:
  13869. {
  13870. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13871. break;
  13872. }
  13873. case SIMD_i16x8_narrow_i32x4_s:
  13874. case SIMD_i16x8_narrow_i32x4_u:
  13875. {
  13876. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13877. break;
  13878. }
  13879. case SIMD_i16x8_extend_low_i8x16_s:
  13880. case SIMD_i16x8_extend_high_i8x16_s:
  13881. case SIMD_i16x8_extend_low_i8x16_u:
  13882. case SIMD_i16x8_extend_high_i8x16_u:
  13883. {
  13884. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13885. break;
  13886. }
  13887. case SIMD_i16x8_shl:
  13888. case SIMD_i16x8_shr_s:
  13889. case SIMD_i16x8_shr_u:
  13890. {
  13891. POP_I32();
  13892. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13893. break;
  13894. }
  13895. case SIMD_i16x8_add:
  13896. case SIMD_i16x8_add_sat_s:
  13897. case SIMD_i16x8_add_sat_u:
  13898. case SIMD_i16x8_sub:
  13899. case SIMD_i16x8_sub_sat_s:
  13900. case SIMD_i16x8_sub_sat_u:
  13901. {
  13902. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13903. break;
  13904. }
  13905. case SIMD_f64x2_nearest:
  13906. {
  13907. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13908. break;
  13909. }
  13910. case SIMD_i16x8_mul:
  13911. case SIMD_i16x8_min_s:
  13912. case SIMD_i16x8_min_u:
  13913. case SIMD_i16x8_max_s:
  13914. case SIMD_i16x8_max_u:
  13915. case SIMD_i16x8_avgr_u:
  13916. case SIMD_i16x8_extmul_low_i8x16_s:
  13917. case SIMD_i16x8_extmul_high_i8x16_s:
  13918. case SIMD_i16x8_extmul_low_i8x16_u:
  13919. case SIMD_i16x8_extmul_high_i8x16_u:
  13920. {
  13921. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13922. break;
  13923. }
  13924. /* i32x4 operation */
  13925. case SIMD_i32x4_abs:
  13926. case SIMD_i32x4_neg:
  13927. {
  13928. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13929. break;
  13930. }
  13931. case SIMD_i32x4_all_true:
  13932. case SIMD_i32x4_bitmask:
  13933. {
  13934. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13935. break;
  13936. }
  13937. case SIMD_i32x4_extend_low_i16x8_s:
  13938. case SIMD_i32x4_extend_high_i16x8_s:
  13939. case SIMD_i32x4_extend_low_i16x8_u:
  13940. case SIMD_i32x4_extend_high_i16x8_u:
  13941. {
  13942. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13943. break;
  13944. }
  13945. case SIMD_i32x4_shl:
  13946. case SIMD_i32x4_shr_s:
  13947. case SIMD_i32x4_shr_u:
  13948. {
  13949. POP_I32();
  13950. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13951. break;
  13952. }
  13953. case SIMD_i32x4_add:
  13954. case SIMD_i32x4_sub:
  13955. case SIMD_i32x4_mul:
  13956. case SIMD_i32x4_min_s:
  13957. case SIMD_i32x4_min_u:
  13958. case SIMD_i32x4_max_s:
  13959. case SIMD_i32x4_max_u:
  13960. case SIMD_i32x4_dot_i16x8_s:
  13961. case SIMD_i32x4_extmul_low_i16x8_s:
  13962. case SIMD_i32x4_extmul_high_i16x8_s:
  13963. case SIMD_i32x4_extmul_low_i16x8_u:
  13964. case SIMD_i32x4_extmul_high_i16x8_u:
  13965. {
  13966. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13967. break;
  13968. }
  13969. /* i64x2 operation */
  13970. case SIMD_i64x2_abs:
  13971. case SIMD_i64x2_neg:
  13972. {
  13973. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13974. break;
  13975. }
  13976. case SIMD_i64x2_all_true:
  13977. case SIMD_i64x2_bitmask:
  13978. {
  13979. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13980. break;
  13981. }
  13982. case SIMD_i64x2_extend_low_i32x4_s:
  13983. case SIMD_i64x2_extend_high_i32x4_s:
  13984. case SIMD_i64x2_extend_low_i32x4_u:
  13985. case SIMD_i64x2_extend_high_i32x4_u:
  13986. {
  13987. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13988. break;
  13989. }
  13990. case SIMD_i64x2_shl:
  13991. case SIMD_i64x2_shr_s:
  13992. case SIMD_i64x2_shr_u:
  13993. {
  13994. POP_I32();
  13995. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13996. break;
  13997. }
  13998. case SIMD_i64x2_add:
  13999. case SIMD_i64x2_sub:
  14000. case SIMD_i64x2_mul:
  14001. case SIMD_i64x2_eq:
  14002. case SIMD_i64x2_ne:
  14003. case SIMD_i64x2_lt_s:
  14004. case SIMD_i64x2_gt_s:
  14005. case SIMD_i64x2_le_s:
  14006. case SIMD_i64x2_ge_s:
  14007. case SIMD_i64x2_extmul_low_i32x4_s:
  14008. case SIMD_i64x2_extmul_high_i32x4_s:
  14009. case SIMD_i64x2_extmul_low_i32x4_u:
  14010. case SIMD_i64x2_extmul_high_i32x4_u:
  14011. {
  14012. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14013. break;
  14014. }
  14015. /* f32x4 operation */
  14016. case SIMD_f32x4_abs:
  14017. case SIMD_f32x4_neg:
  14018. case SIMD_f32x4_sqrt:
  14019. {
  14020. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14021. break;
  14022. }
  14023. case SIMD_f32x4_add:
  14024. case SIMD_f32x4_sub:
  14025. case SIMD_f32x4_mul:
  14026. case SIMD_f32x4_div:
  14027. case SIMD_f32x4_min:
  14028. case SIMD_f32x4_max:
  14029. case SIMD_f32x4_pmin:
  14030. case SIMD_f32x4_pmax:
  14031. {
  14032. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14033. break;
  14034. }
  14035. /* f64x2 operation */
  14036. case SIMD_f64x2_abs:
  14037. case SIMD_f64x2_neg:
  14038. case SIMD_f64x2_sqrt:
  14039. {
  14040. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14041. break;
  14042. }
  14043. case SIMD_f64x2_add:
  14044. case SIMD_f64x2_sub:
  14045. case SIMD_f64x2_mul:
  14046. case SIMD_f64x2_div:
  14047. case SIMD_f64x2_min:
  14048. case SIMD_f64x2_max:
  14049. case SIMD_f64x2_pmin:
  14050. case SIMD_f64x2_pmax:
  14051. {
  14052. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14053. break;
  14054. }
  14055. case SIMD_i32x4_trunc_sat_f32x4_s:
  14056. case SIMD_i32x4_trunc_sat_f32x4_u:
  14057. case SIMD_f32x4_convert_i32x4_s:
  14058. case SIMD_f32x4_convert_i32x4_u:
  14059. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14060. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14061. case SIMD_f64x2_convert_low_i32x4_s:
  14062. case SIMD_f64x2_convert_low_i32x4_u:
  14063. {
  14064. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14065. break;
  14066. }
  14067. default:
  14068. {
  14069. if (error_buf != NULL) {
  14070. snprintf(error_buf, error_buf_size,
  14071. "WASM module load failed: "
  14072. "invalid opcode 0xfd %02x.",
  14073. opcode1);
  14074. }
  14075. goto fail;
  14076. }
  14077. }
  14078. break;
  14079. }
  14080. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  14081. #endif /* end of WASM_ENABLE_SIMD */
  14082. #if WASM_ENABLE_SHARED_MEMORY != 0
  14083. case WASM_OP_ATOMIC_PREFIX:
  14084. {
  14085. uint32 opcode1;
  14086. read_leb_uint32(p, p_end, opcode1);
  14087. #if WASM_ENABLE_FAST_INTERP != 0
  14088. emit_byte(loader_ctx, opcode1);
  14089. #endif
  14090. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14091. CHECK_MEMORY();
  14092. read_leb_uint32(p, p_end, align); /* align */
  14093. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14094. if (!check_memory_align_equal(opcode1, align, error_buf,
  14095. error_buf_size)) {
  14096. goto fail;
  14097. }
  14098. #if WASM_ENABLE_FAST_INTERP != 0
  14099. emit_uint32(loader_ctx, mem_offset);
  14100. #endif
  14101. }
  14102. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14103. func->has_memory_operations = true;
  14104. #endif
  14105. switch (opcode1) {
  14106. case WASM_OP_ATOMIC_NOTIFY:
  14107. POP_I32();
  14108. POP_MEM_OFFSET();
  14109. PUSH_I32();
  14110. break;
  14111. case WASM_OP_ATOMIC_WAIT32:
  14112. POP_I64();
  14113. POP_I32();
  14114. POP_MEM_OFFSET();
  14115. PUSH_I32();
  14116. break;
  14117. case WASM_OP_ATOMIC_WAIT64:
  14118. POP_I64();
  14119. POP_I64();
  14120. POP_MEM_OFFSET();
  14121. PUSH_I32();
  14122. break;
  14123. case WASM_OP_ATOMIC_FENCE:
  14124. /* reserved byte 0x00 */
  14125. if (*p++ != 0x00) {
  14126. set_error_buf(error_buf, error_buf_size,
  14127. "zero byte expected");
  14128. goto fail;
  14129. }
  14130. break;
  14131. case WASM_OP_ATOMIC_I32_LOAD:
  14132. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14133. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14134. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14135. break;
  14136. case WASM_OP_ATOMIC_I32_STORE:
  14137. case WASM_OP_ATOMIC_I32_STORE8:
  14138. case WASM_OP_ATOMIC_I32_STORE16:
  14139. POP_I32();
  14140. POP_MEM_OFFSET();
  14141. break;
  14142. case WASM_OP_ATOMIC_I64_LOAD:
  14143. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14144. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14145. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14146. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14147. break;
  14148. case WASM_OP_ATOMIC_I64_STORE:
  14149. case WASM_OP_ATOMIC_I64_STORE8:
  14150. case WASM_OP_ATOMIC_I64_STORE16:
  14151. case WASM_OP_ATOMIC_I64_STORE32:
  14152. POP_I64();
  14153. POP_MEM_OFFSET();
  14154. break;
  14155. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14156. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14157. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14158. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14159. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14160. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14161. case WASM_OP_ATOMIC_RMW_I32_AND:
  14162. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14163. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14164. case WASM_OP_ATOMIC_RMW_I32_OR:
  14165. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14166. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14167. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14168. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14169. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14170. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14171. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14172. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14173. POP_I32();
  14174. POP_MEM_OFFSET();
  14175. PUSH_I32();
  14176. break;
  14177. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14178. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14179. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14180. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14181. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14182. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14183. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14184. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14185. case WASM_OP_ATOMIC_RMW_I64_AND:
  14186. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14187. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14188. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14189. case WASM_OP_ATOMIC_RMW_I64_OR:
  14190. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14191. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14192. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14193. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14194. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14195. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14196. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14197. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14198. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14199. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14200. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14201. POP_I64();
  14202. POP_MEM_OFFSET();
  14203. PUSH_I64();
  14204. break;
  14205. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14206. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14207. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14208. POP_I32();
  14209. POP_I32();
  14210. POP_MEM_OFFSET();
  14211. PUSH_I32();
  14212. break;
  14213. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14214. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14215. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14216. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14217. POP_I64();
  14218. POP_I64();
  14219. POP_MEM_OFFSET();
  14220. PUSH_I64();
  14221. break;
  14222. default:
  14223. set_error_buf_v(error_buf, error_buf_size,
  14224. "%s %02x %02x", "unsupported opcode",
  14225. 0xfe, opcode1);
  14226. goto fail;
  14227. }
  14228. break;
  14229. }
  14230. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14231. default:
  14232. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14233. "unsupported opcode", opcode);
  14234. goto fail;
  14235. }
  14236. #if WASM_ENABLE_FAST_INTERP != 0
  14237. last_op = opcode;
  14238. #endif
  14239. }
  14240. if (loader_ctx->csp_num > 0) {
  14241. if (cur_func_idx < module->function_count - 1)
  14242. /* Function with missing end marker (between two functions) */
  14243. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  14244. else
  14245. /* Function with missing end marker
  14246. (at EOF or end of code sections) */
  14247. set_error_buf(error_buf, error_buf_size,
  14248. "unexpected end of section or function, "
  14249. "or section size mismatch");
  14250. goto fail;
  14251. }
  14252. #if WASM_ENABLE_FAST_INTERP != 0
  14253. if (loader_ctx->p_code_compiled == NULL)
  14254. goto re_scan;
  14255. func->const_cell_num = loader_ctx->const_cell_num;
  14256. if (func->const_cell_num > 0) {
  14257. int32 j;
  14258. if (!(func->consts = func_const = loader_malloc(
  14259. func->const_cell_num * 4, error_buf, error_buf_size)))
  14260. goto fail;
  14261. func_const_end = func->consts + func->const_cell_num * 4;
  14262. /* reverse the const buf */
  14263. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  14264. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  14265. if (c->value_type == VALUE_TYPE_F64
  14266. || c->value_type == VALUE_TYPE_I64) {
  14267. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14268. &(c->value.f64), (uint32)sizeof(int64));
  14269. func_const += sizeof(int64);
  14270. }
  14271. else {
  14272. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14273. &(c->value.f32), (uint32)sizeof(int32));
  14274. func_const += sizeof(int32);
  14275. }
  14276. }
  14277. }
  14278. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14279. - loader_ctx->start_dynamic_offset + 1;
  14280. #else
  14281. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14282. #endif
  14283. func->max_block_num = loader_ctx->max_csp_num;
  14284. return_value = true;
  14285. fail:
  14286. wasm_loader_ctx_destroy(loader_ctx);
  14287. (void)table_idx;
  14288. (void)table_seg_idx;
  14289. (void)data_seg_idx;
  14290. (void)i64_const;
  14291. (void)local_offset;
  14292. (void)p_org;
  14293. (void)mem_offset;
  14294. (void)align;
  14295. return return_value;
  14296. }