wasm_loader.c 624 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844
  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_platform.h"
  7. #include "wasm.h"
  8. #include "wasm_opcode.h"
  9. #include "wasm_runtime.h"
  10. #include "wasm_loader_common.h"
  11. #include "../common/wasm_native.h"
  12. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_GC != 0
  14. #include "../common/gc/gc_type.h"
  15. #include "../common/gc/gc_object.h"
  16. #endif
  17. #if WASM_ENABLE_DEBUG_INTERP != 0
  18. #include "../libraries/debug-engine/debug_engine.h"
  19. #endif
  20. #if WASM_ENABLE_FAST_JIT != 0
  21. #include "../fast-jit/jit_compiler.h"
  22. #include "../fast-jit/jit_codecache.h"
  23. #endif
  24. #if WASM_ENABLE_JIT != 0
  25. #include "../compilation/aot_llvm.h"
  26. #endif
  27. #ifndef TRACE_WASM_LOADER
  28. #define TRACE_WASM_LOADER 0
  29. #endif
  30. /* Read a value of given type from the address pointed to by the given
  31. pointer and increase the pointer to the position just after the
  32. value being read. */
  33. #define TEMPLATE_READ_VALUE(Type, p) \
  34. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  35. #if WASM_ENABLE_MEMORY64 != 0
  36. static bool
  37. has_module_memory64(WASMModule *module)
  38. {
  39. /* TODO: multi-memories for now assuming the memory idx type is consistent
  40. * across multi-memories */
  41. if (module->import_memory_count > 0)
  42. return !!(module->import_memories[0].u.memory.mem_type.flags
  43. & MEMORY64_FLAG);
  44. else if (module->memory_count > 0)
  45. return !!(module->memories[0].flags & MEMORY64_FLAG);
  46. return false;
  47. }
  48. static bool
  49. is_table_64bit(WASMModule *module, uint32 table_idx)
  50. {
  51. if (table_idx < module->import_table_count)
  52. return !!(module->import_tables[table_idx].u.table.table_type.flags
  53. & TABLE64_FLAG);
  54. else
  55. return !!(module->tables[table_idx - module->import_table_count]
  56. .table_type.flags
  57. & TABLE64_FLAG);
  58. return false;
  59. }
  60. #endif
  61. static void
  62. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  63. {
  64. wasm_loader_set_error_buf(error_buf, error_buf_size, string, false);
  65. }
  66. #if WASM_ENABLE_MEMORY64 != 0
  67. static void
  68. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  69. {
  70. if (error_buf != NULL) {
  71. snprintf(error_buf, error_buf_size, "offset out of range");
  72. }
  73. }
  74. #endif
  75. static void
  76. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  77. {
  78. va_list args;
  79. char buf[128];
  80. if (error_buf != NULL) {
  81. va_start(args, format);
  82. vsnprintf(buf, sizeof(buf), format, args);
  83. va_end(args);
  84. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  85. }
  86. }
  87. static bool
  88. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  89. char *error_buf, uint32 error_buf_size)
  90. {
  91. if ((uintptr_t)buf + length < (uintptr_t)buf
  92. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  93. set_error_buf(error_buf, error_buf_size,
  94. "unexpected end of section or function");
  95. return false;
  96. }
  97. return true;
  98. }
  99. static bool
  100. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  101. char *error_buf, uint32 error_buf_size)
  102. {
  103. if ((uintptr_t)buf + length < (uintptr_t)buf
  104. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  105. set_error_buf(error_buf, error_buf_size, "unexpected end");
  106. return false;
  107. }
  108. return true;
  109. }
  110. #define CHECK_BUF(buf, buf_end, length) \
  111. do { \
  112. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  113. goto fail; \
  114. } \
  115. } while (0)
  116. #define CHECK_BUF1(buf, buf_end, length) \
  117. do { \
  118. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  119. goto fail; \
  120. } \
  121. } while (0)
  122. #define skip_leb(p) while (*p++ & 0x80)
  123. #define skip_leb_int64(p, p_end) skip_leb(p)
  124. #define skip_leb_uint32(p, p_end) skip_leb(p)
  125. #define skip_leb_int32(p, p_end) skip_leb(p)
  126. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  127. #define skip_leb_memidx(p, p_end) skip_leb(p)
  128. #if WASM_ENABLE_MULTI_MEMORY == 0
  129. #define skip_leb_align(p, p_end) skip_leb(p)
  130. #else
  131. /* Skip the following memidx if applicable */
  132. #define skip_leb_align(p, p_end) \
  133. do { \
  134. if (*p++ & OPT_MEMIDX_FLAG) \
  135. skip_leb_uint32(p, p_end); \
  136. } while (0)
  137. #endif
  138. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  139. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  140. #define read_leb_int64(p, p_end, res) \
  141. do { \
  142. uint64 res64; \
  143. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  144. error_buf_size)) \
  145. goto fail; \
  146. res = (int64)res64; \
  147. } while (0)
  148. #if WASM_ENABLE_MEMORY64 != 0
  149. #define read_leb_mem_offset(p, p_end, res) \
  150. do { \
  151. uint64 res64; \
  152. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  153. &res64, error_buf, error_buf_size)) { \
  154. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  155. goto fail; \
  156. } \
  157. res = (mem_offset_t)res64; \
  158. } while (0)
  159. #else
  160. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  161. #endif
  162. #define read_leb_uint32(p, p_end, res) \
  163. do { \
  164. uint64 res64; \
  165. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  166. error_buf_size)) \
  167. goto fail; \
  168. res = (uint32)res64; \
  169. } while (0)
  170. #define read_leb_int32(p, p_end, res) \
  171. do { \
  172. uint64 res64; \
  173. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  174. error_buf_size)) \
  175. goto fail; \
  176. res = (int32)res64; \
  177. } while (0)
  178. #if WASM_ENABLE_MULTI_MEMORY != 0
  179. #define check_memidx(module, memidx) \
  180. do { \
  181. if (memidx >= module->import_memory_count + module->memory_count) { \
  182. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d", \
  183. memidx); \
  184. goto fail; \
  185. } \
  186. } while (0)
  187. /* Bit 6(0x40) indicating the optional memidx, and reset bit 6 for
  188. * alignment check */
  189. #define read_leb_memarg(p, p_end, res) \
  190. do { \
  191. read_leb_uint32(p, p_end, res); \
  192. if (res & OPT_MEMIDX_FLAG) { \
  193. res &= ~OPT_MEMIDX_FLAG; \
  194. read_leb_uint32(p, p_end, memidx); /* memidx */ \
  195. check_memidx(module, memidx); \
  196. } \
  197. } while (0)
  198. #else
  199. /* reserved byte 0x00 */
  200. #define check_memidx(module, memidx) \
  201. do { \
  202. (void)module; \
  203. if (memidx != 0) { \
  204. set_error_buf(error_buf, error_buf_size, "zero byte expected"); \
  205. goto fail; \
  206. } \
  207. } while (0)
  208. #define read_leb_memarg(p, p_end, res) read_leb_uint32(p, p_end, res)
  209. #endif
  210. static char *
  211. type2str(uint8 type)
  212. {
  213. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  214. #if WASM_ENABLE_GC != 0
  215. char *type_str_ref[] = { "stringview_iter",
  216. "stringview_wtf16",
  217. "(ref null ht)",
  218. "(ref ht)",
  219. "", /* reserved */
  220. "stringview_wtf8",
  221. "stringref",
  222. "", /* reserved */
  223. "", /* reserved */
  224. "arrayref",
  225. "structref",
  226. "i31ref",
  227. "eqref",
  228. "anyref",
  229. "externref",
  230. "funcref",
  231. "nullref",
  232. "nullexternref",
  233. "nullfuncref" };
  234. #endif
  235. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  236. return type_str[type - VALUE_TYPE_V128];
  237. #if WASM_ENABLE_GC != 0
  238. else if (wasm_is_type_reftype(type))
  239. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  240. #endif
  241. else if (type == VALUE_TYPE_FUNCREF)
  242. return "funcref";
  243. else if (type == VALUE_TYPE_EXTERNREF)
  244. return "externref";
  245. else
  246. return "unknown type";
  247. }
  248. static bool
  249. is_32bit_type(uint8 type)
  250. {
  251. if (type == VALUE_TYPE_I32
  252. || type == VALUE_TYPE_F32
  253. /* the operand stack is in polymorphic state */
  254. || type == VALUE_TYPE_ANY
  255. #if WASM_ENABLE_GC != 0
  256. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  257. #elif WASM_ENABLE_REF_TYPES != 0
  258. /* For reference types, we use uint32 index to represent
  259. the funcref and externref */
  260. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  261. #endif
  262. )
  263. return true;
  264. return false;
  265. }
  266. static bool
  267. is_64bit_type(uint8 type)
  268. {
  269. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  270. #if WASM_ENABLE_GC != 0
  271. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  272. #endif
  273. )
  274. return true;
  275. return false;
  276. }
  277. #if WASM_ENABLE_GC != 0
  278. static bool
  279. is_packed_type(uint8 type)
  280. {
  281. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  282. }
  283. #endif
  284. static bool
  285. is_byte_a_type(uint8 type)
  286. {
  287. return (is_valid_value_type_for_interpreter(type)
  288. || (type == VALUE_TYPE_VOID))
  289. ? true
  290. : false;
  291. }
  292. #if WASM_ENABLE_SIMD != 0
  293. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  294. || (WASM_ENABLE_FAST_INTERP != 0)
  295. static V128
  296. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  297. {
  298. V128 result;
  299. uint8 i;
  300. for (i = 0; i != 16; ++i) {
  301. result.i8x16[i] = read_uint8(p_buf);
  302. }
  303. return result;
  304. }
  305. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  306. (WASM_ENABLE_FAST_INTERP != 0) */
  307. #endif /* end of WASM_ENABLE_SIMD */
  308. static void *
  309. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  310. {
  311. void *mem;
  312. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  313. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  314. return NULL;
  315. }
  316. memset(mem, 0, (uint32)size);
  317. return mem;
  318. }
  319. static void *
  320. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  321. uint32 error_buf_size)
  322. {
  323. uint8 *mem_new;
  324. bh_assert(size_new > size_old);
  325. if ((mem_new = wasm_runtime_realloc(mem_old, size_new))) {
  326. memset(mem_new + size_old, 0, size_new - size_old);
  327. return mem_new;
  328. }
  329. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  330. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  331. wasm_runtime_free(mem_old);
  332. }
  333. return mem_new;
  334. }
  335. #define MEM_REALLOC(mem, size_old, size_new) \
  336. do { \
  337. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  338. error_buf_size); \
  339. if (!mem_new) \
  340. goto fail; \
  341. mem = mem_new; \
  342. } while (0)
  343. static bool
  344. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  345. char *error_buf, uint32 error_buf_size)
  346. {
  347. if (type_index >= type_count) {
  348. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  349. type_index);
  350. return false;
  351. }
  352. return true;
  353. }
  354. #if WASM_ENABLE_GC != 0
  355. static bool
  356. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  357. uint32 error_buf_size)
  358. {
  359. if (!check_type_index(module, module->type_count, type_index, error_buf,
  360. error_buf_size)) {
  361. return false;
  362. }
  363. if (module->types[type_index] == NULL
  364. || module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  365. set_error_buf(error_buf, error_buf_size, "unknown array type");
  366. return false;
  367. }
  368. return true;
  369. }
  370. #endif
  371. /*
  372. * if no GC is enabled, an valid type is always a function type.
  373. * but if GC is enabled, we need to check the type flag
  374. */
  375. static bool
  376. check_function_type(const WASMModule *module, uint32 type_index,
  377. char *error_buf, uint32 error_buf_size)
  378. {
  379. if (!check_type_index(module, module->type_count, type_index, error_buf,
  380. error_buf_size)) {
  381. return false;
  382. }
  383. #if WASM_ENABLE_GC != 0
  384. if (module->types[type_index] == NULL
  385. || module->types[type_index]->type_flag != WASM_TYPE_FUNC) {
  386. set_error_buf(error_buf, error_buf_size, "unknown function type");
  387. return false;
  388. }
  389. #endif
  390. return true;
  391. }
  392. static bool
  393. check_function_index(const WASMModule *module, uint32 function_index,
  394. char *error_buf, uint32 error_buf_size)
  395. {
  396. if (function_index
  397. >= module->import_function_count + module->function_count) {
  398. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  399. function_index);
  400. return false;
  401. }
  402. return true;
  403. }
  404. typedef struct InitValue {
  405. uint8 type;
  406. uint8 flag;
  407. #if WASM_ENABLE_GC != 0
  408. uint8 gc_opcode;
  409. WASMRefType ref_type;
  410. #endif
  411. WASMValue value;
  412. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  413. InitializerExpression *expr;
  414. #endif
  415. } InitValue;
  416. typedef struct ConstExprContext {
  417. uint32 sp;
  418. uint32 size;
  419. WASMModule *module;
  420. InitValue *stack;
  421. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  422. } ConstExprContext;
  423. static void
  424. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  425. {
  426. ctx->sp = 0;
  427. ctx->module = module;
  428. ctx->stack = ctx->data;
  429. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  430. }
  431. static bool
  432. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  433. #if WASM_ENABLE_GC != 0
  434. WASMRefType *ref_type, uint8 gc_opcode,
  435. #endif
  436. WASMValue *value,
  437. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  438. InitializerExpression *expr,
  439. #endif
  440. char *error_buf, uint32 error_buf_size)
  441. {
  442. InitValue *cur_value;
  443. if (ctx->sp >= ctx->size) {
  444. if (ctx->stack != ctx->data) {
  445. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  446. (ctx->size + 4) * sizeof(InitValue));
  447. }
  448. else {
  449. if (!(ctx->stack =
  450. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  451. error_buf, error_buf_size))) {
  452. goto fail;
  453. }
  454. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  455. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  456. }
  457. ctx->size += 4;
  458. }
  459. cur_value = &ctx->stack[ctx->sp++];
  460. cur_value->type = type;
  461. cur_value->flag = flag;
  462. cur_value->value = *value;
  463. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  464. cur_value->expr = expr;
  465. #endif
  466. #if WASM_ENABLE_GC != 0
  467. cur_value->gc_opcode = gc_opcode;
  468. if (wasm_is_type_multi_byte_type(type)) {
  469. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  470. ref_type, wasm_reftype_struct_size(ref_type));
  471. }
  472. #endif
  473. return true;
  474. fail:
  475. return false;
  476. }
  477. #if WASM_ENABLE_GC != 0
  478. static void
  479. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  480. {
  481. WASMStructNewInitValues *struct_init_values =
  482. (WASMStructNewInitValues *)data;
  483. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  484. WASMType *wasm_type;
  485. uint32 i;
  486. if (!data)
  487. return;
  488. wasm_type = module->types[struct_init_values->type_idx];
  489. /* The data can only be type of `WASMStructNewInitValues *`
  490. or `WASMArrayNewInitValues *` */
  491. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  492. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  493. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  494. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  495. WASMRefType *ref_type;
  496. uint8 field_type;
  497. uint16 ref_type_map_index = 0;
  498. for (i = 0; i < struct_init_values->count; i++) {
  499. field_type = struct_type->fields[i].field_type;
  500. if (wasm_is_type_multi_byte_type(field_type))
  501. ref_type =
  502. struct_type->ref_type_maps[ref_type_map_index++].ref_type;
  503. else
  504. ref_type = NULL;
  505. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  506. REF_TYPE_STRUCTREF, NULL,
  507. module->types, module->type_count)
  508. || wasm_reftype_is_subtype_of(
  509. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  510. module->types, module->type_count)) {
  511. destroy_init_expr_data_recursive(
  512. module, struct_init_values->fields[i].data);
  513. }
  514. }
  515. }
  516. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  517. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  518. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  519. uint8 elem_type = array_type->elem_type;
  520. for (i = 0; i < array_init_values->length; i++) {
  521. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  522. REF_TYPE_STRUCTREF, NULL,
  523. module->types, module->type_count)
  524. || wasm_reftype_is_subtype_of(
  525. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  526. module->types, module->type_count)) {
  527. destroy_init_expr_data_recursive(
  528. module, array_init_values->elem_data[i].data);
  529. }
  530. }
  531. }
  532. wasm_runtime_free(data);
  533. }
  534. #endif
  535. static bool
  536. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  537. #if WASM_ENABLE_GC != 0
  538. WASMRefType *ref_type, uint8 *p_gc_opcode,
  539. #endif
  540. WASMValue *p_value,
  541. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  542. InitializerExpression **p_expr,
  543. #endif
  544. char *error_buf, uint32 error_buf_size)
  545. {
  546. InitValue *cur_value;
  547. if (ctx->sp == 0) {
  548. set_error_buf(error_buf, error_buf_size,
  549. "type mismatch: const expr stack underflow");
  550. return false;
  551. }
  552. cur_value = &ctx->stack[--ctx->sp];
  553. #if WASM_ENABLE_GC == 0
  554. if (cur_value->type != type) {
  555. set_error_buf(error_buf, error_buf_size, "type mismatch");
  556. return false;
  557. }
  558. #else
  559. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  560. ref_type, ctx->module->types,
  561. ctx->module->type_count)) {
  562. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  563. "type mismatch: expect ", type2str(type),
  564. " but got other");
  565. goto fail;
  566. }
  567. #endif
  568. if (p_flag)
  569. *p_flag = cur_value->flag;
  570. if (p_value)
  571. *p_value = cur_value->value;
  572. #if WASM_ENABLE_GC != 0
  573. if (p_gc_opcode)
  574. *p_gc_opcode = cur_value->gc_opcode;
  575. #endif
  576. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  577. if (p_expr)
  578. *p_expr = cur_value->expr;
  579. #endif
  580. return true;
  581. #if WASM_ENABLE_GC != 0
  582. fail:
  583. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  584. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  585. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  586. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  587. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  588. }
  589. return false;
  590. #endif
  591. }
  592. static void
  593. destroy_const_expr_stack(ConstExprContext *ctx, bool free_exprs)
  594. {
  595. #if WASM_ENABLE_GC != 0
  596. uint32 i;
  597. for (i = 0; i < ctx->sp; i++) {
  598. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  599. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  600. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  601. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  602. destroy_init_expr_data_recursive(ctx->module,
  603. ctx->stack[i].value.data);
  604. }
  605. }
  606. #endif
  607. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  608. if (free_exprs) {
  609. for (uint32 j = 0; j < ctx->sp; j++) {
  610. if (is_expr_binary_op(ctx->stack[j].expr->init_expr_type)) {
  611. destroy_init_expr_recursive(ctx->stack[j].expr);
  612. ctx->stack[j].expr = NULL;
  613. }
  614. }
  615. }
  616. #endif
  617. if (ctx->stack != ctx->data) {
  618. wasm_runtime_free(ctx->stack);
  619. }
  620. }
  621. #if WASM_ENABLE_GC != 0 || WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  622. static void
  623. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  624. {
  625. #if WASM_ENABLE_GC != 0
  626. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  627. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  628. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  629. destroy_init_expr_data_recursive(module, expr->u.unary.v.data);
  630. }
  631. #endif
  632. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  633. // free left expr and right exprs for binary oprand
  634. if (!is_expr_binary_op(expr->init_expr_type)) {
  635. return;
  636. }
  637. if (expr->u.binary.l_expr) {
  638. destroy_init_expr_recursive(expr->u.binary.l_expr);
  639. }
  640. if (expr->u.binary.r_expr) {
  641. destroy_init_expr_recursive(expr->u.binary.r_expr);
  642. }
  643. expr->u.binary.l_expr = expr->u.binary.r_expr = NULL;
  644. #endif
  645. }
  646. #endif
  647. /* for init expr
  648. * (data (i32.add (i32.const 0) (i32.sub (i32.const 1) (i32.const 2)))),
  649. * the binary format is
  650. * 0x11: 41 00 ; i32.const 0
  651. * 0x13: 41 01 ; i32.const 1
  652. * 0x15: 41 02 ; i32.const 2
  653. * 0x17: 6b ; i32.sub
  654. * 0x18: 6a ; i32.add
  655. * for traversal: read opcodes and push them onto the stack. When encountering
  656. * a binary opcode, pop two values from the stack which become the left and
  657. * right child nodes of this binary operation node.
  658. */
  659. static bool
  660. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  661. InitializerExpression *init_expr, uint8 type, void *ref_type,
  662. char *error_buf, uint32 error_buf_size)
  663. {
  664. const uint8 *p = *p_buf, *p_end = buf_end;
  665. uint8 flag, *p_float;
  666. uint32 i;
  667. ConstExprContext const_expr_ctx = { 0 };
  668. WASMValue cur_value;
  669. #if WASM_ENABLE_GC != 0
  670. uint32 opcode1, type_idx;
  671. uint8 opcode;
  672. WASMRefType cur_ref_type = { 0 };
  673. #endif
  674. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  675. InitializerExpression *cur_expr = NULL;
  676. #endif
  677. init_const_expr_stack(&const_expr_ctx, module);
  678. CHECK_BUF(p, p_end, 1);
  679. flag = read_uint8(p);
  680. while (flag != WASM_OP_END) {
  681. switch (flag) {
  682. /* i32.const */
  683. case INIT_EXPR_TYPE_I32_CONST:
  684. read_leb_int32(p, p_end, cur_value.i32);
  685. if (!push_const_expr_stack(&const_expr_ctx, flag,
  686. VALUE_TYPE_I32,
  687. #if WASM_ENABLE_GC != 0
  688. NULL, 0,
  689. #endif
  690. &cur_value,
  691. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  692. NULL,
  693. #endif
  694. error_buf, error_buf_size))
  695. goto fail;
  696. break;
  697. /* i64.const */
  698. case INIT_EXPR_TYPE_I64_CONST:
  699. read_leb_int64(p, p_end, cur_value.i64);
  700. if (!push_const_expr_stack(&const_expr_ctx, flag,
  701. VALUE_TYPE_I64,
  702. #if WASM_ENABLE_GC != 0
  703. NULL, 0,
  704. #endif
  705. &cur_value,
  706. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  707. NULL,
  708. #endif
  709. error_buf, error_buf_size))
  710. goto fail;
  711. break;
  712. /* f32.const */
  713. case INIT_EXPR_TYPE_F32_CONST:
  714. CHECK_BUF(p, p_end, 4);
  715. p_float = (uint8 *)&cur_value.f32;
  716. for (i = 0; i < sizeof(float32); i++)
  717. *p_float++ = *p++;
  718. if (!push_const_expr_stack(&const_expr_ctx, flag,
  719. VALUE_TYPE_F32,
  720. #if WASM_ENABLE_GC != 0
  721. NULL, 0,
  722. #endif
  723. &cur_value,
  724. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  725. NULL,
  726. #endif
  727. error_buf, error_buf_size))
  728. goto fail;
  729. break;
  730. /* f64.const */
  731. case INIT_EXPR_TYPE_F64_CONST:
  732. CHECK_BUF(p, p_end, 8);
  733. p_float = (uint8 *)&cur_value.f64;
  734. for (i = 0; i < sizeof(float64); i++)
  735. *p_float++ = *p++;
  736. if (!push_const_expr_stack(&const_expr_ctx, flag,
  737. VALUE_TYPE_F64,
  738. #if WASM_ENABLE_GC != 0
  739. NULL, 0,
  740. #endif
  741. &cur_value,
  742. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  743. NULL,
  744. #endif
  745. error_buf, error_buf_size))
  746. goto fail;
  747. break;
  748. #if WASM_ENABLE_SIMD != 0
  749. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  750. || (WASM_ENABLE_FAST_INTERP != 0)
  751. /* v128.const */
  752. case INIT_EXPR_TYPE_V128_CONST:
  753. {
  754. uint64 high, low;
  755. CHECK_BUF(p, p_end, 1);
  756. (void)read_uint8(p);
  757. CHECK_BUF(p, p_end, 16);
  758. wasm_runtime_read_v128(p, &high, &low);
  759. p += 16;
  760. cur_value.v128.i64x2[0] = high;
  761. cur_value.v128.i64x2[1] = low;
  762. if (!push_const_expr_stack(&const_expr_ctx, flag,
  763. VALUE_TYPE_V128,
  764. #if WASM_ENABLE_GC != 0
  765. NULL, 0,
  766. #endif
  767. &cur_value,
  768. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  769. NULL,
  770. #endif
  771. error_buf, error_buf_size))
  772. goto fail;
  773. #if WASM_ENABLE_WAMR_COMPILER != 0
  774. /* If any init_expr is v128.const, mark SIMD used */
  775. module->is_simd_used = true;
  776. #endif
  777. break;
  778. }
  779. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  780. (WASM_ENABLE_FAST_INTERP != 0) */
  781. #endif /* end of WASM_ENABLE_SIMD */
  782. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  783. case INIT_EXPR_TYPE_I32_ADD:
  784. case INIT_EXPR_TYPE_I32_SUB:
  785. case INIT_EXPR_TYPE_I32_MUL:
  786. case INIT_EXPR_TYPE_I64_ADD:
  787. case INIT_EXPR_TYPE_I64_SUB:
  788. case INIT_EXPR_TYPE_I64_MUL:
  789. {
  790. InitializerExpression *l_expr, *r_expr;
  791. WASMValue l_value, r_value;
  792. uint8 l_flag, r_flag;
  793. uint8 value_type;
  794. if (flag == INIT_EXPR_TYPE_I32_ADD
  795. || flag == INIT_EXPR_TYPE_I32_SUB
  796. || flag == INIT_EXPR_TYPE_I32_MUL) {
  797. value_type = VALUE_TYPE_I32;
  798. }
  799. else {
  800. value_type = VALUE_TYPE_I64;
  801. }
  802. /* If right flag indicates a binary operation, right expr will
  803. * be popped from stack. Otherwise, allocate a new expr for
  804. * right expr. Same for left expr.
  805. */
  806. if (!(pop_const_expr_stack(&const_expr_ctx, &r_flag, value_type,
  807. #if WASM_ENABLE_GC != 0
  808. NULL, NULL,
  809. #endif
  810. &r_value, &r_expr, error_buf,
  811. error_buf_size))) {
  812. goto fail;
  813. }
  814. if (!is_expr_binary_op(r_flag)) {
  815. if (!(r_expr = loader_malloc(sizeof(InitializerExpression),
  816. error_buf, error_buf_size))) {
  817. goto fail;
  818. }
  819. r_expr->init_expr_type = r_flag;
  820. r_expr->u.unary.v = r_value;
  821. }
  822. if (!(pop_const_expr_stack(&const_expr_ctx, &l_flag, value_type,
  823. #if WASM_ENABLE_GC != 0
  824. NULL, NULL,
  825. #endif
  826. &l_value, &l_expr, error_buf,
  827. error_buf_size))) {
  828. destroy_init_expr_recursive(r_expr);
  829. goto fail;
  830. }
  831. if (!is_expr_binary_op(l_flag)) {
  832. if (!(l_expr = loader_malloc(sizeof(InitializerExpression),
  833. error_buf, error_buf_size))) {
  834. destroy_init_expr_recursive(r_expr);
  835. goto fail;
  836. }
  837. l_expr->init_expr_type = l_flag;
  838. l_expr->u.unary.v = l_value;
  839. }
  840. if (!(cur_expr = loader_malloc(sizeof(InitializerExpression),
  841. error_buf, error_buf_size))) {
  842. destroy_init_expr_recursive(l_expr);
  843. destroy_init_expr_recursive(r_expr);
  844. goto fail;
  845. }
  846. cur_expr->init_expr_type = flag;
  847. cur_expr->u.binary.l_expr = l_expr;
  848. cur_expr->u.binary.r_expr = r_expr;
  849. if (!push_const_expr_stack(&const_expr_ctx, flag, value_type,
  850. #if WASM_ENABLE_GC != 0
  851. NULL, 0,
  852. #endif
  853. &cur_value, cur_expr, error_buf,
  854. error_buf_size)) {
  855. destroy_init_expr_recursive(cur_expr);
  856. goto fail;
  857. }
  858. break;
  859. }
  860. #endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR */
  861. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  862. /* ref.func */
  863. case INIT_EXPR_TYPE_FUNCREF_CONST:
  864. {
  865. uint32 func_idx;
  866. read_leb_uint32(p, p_end, func_idx);
  867. cur_value.ref_index = func_idx;
  868. if (!check_function_index(module, func_idx, error_buf,
  869. error_buf_size)) {
  870. goto fail;
  871. }
  872. #if WASM_ENABLE_GC == 0
  873. if (!push_const_expr_stack(&const_expr_ctx, flag,
  874. VALUE_TYPE_FUNCREF, &cur_value,
  875. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  876. NULL,
  877. #endif
  878. error_buf, error_buf_size))
  879. goto fail;
  880. #else
  881. if (func_idx < module->import_function_count) {
  882. type_idx =
  883. module->import_functions[func_idx].u.function.type_idx;
  884. }
  885. else {
  886. type_idx = module
  887. ->functions[func_idx
  888. - module->import_function_count]
  889. ->type_idx;
  890. }
  891. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  892. false, type_idx);
  893. if (!push_const_expr_stack(&const_expr_ctx, flag,
  894. cur_ref_type.ref_type, &cur_ref_type,
  895. 0, &cur_value,
  896. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  897. NULL,
  898. #endif
  899. error_buf, error_buf_size))
  900. goto fail;
  901. #endif
  902. #if WASM_ENABLE_WAMR_COMPILER != 0
  903. module->is_ref_types_used = true;
  904. #endif
  905. break;
  906. }
  907. /* ref.null */
  908. case INIT_EXPR_TYPE_REFNULL_CONST:
  909. {
  910. #if WASM_ENABLE_GC == 0
  911. uint8 type1;
  912. CHECK_BUF(p, p_end, 1);
  913. type1 = read_uint8(p);
  914. cur_value.ref_index = NULL_REF;
  915. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  916. &cur_value,
  917. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  918. NULL,
  919. #endif
  920. error_buf, error_buf_size))
  921. goto fail;
  922. #else
  923. /*
  924. * According to the current GC SPEC rules, the heap_type must be
  925. * validated when ref.null is used. It can be an absheaptype,
  926. * or the type C.types[type_idx] must be defined in the context.
  927. */
  928. int32 heap_type;
  929. read_leb_int32(p, p_end, heap_type);
  930. cur_value.gc_obj = NULL_REF;
  931. /*
  932. * The current check of heap_type can deterministically infer
  933. * the result of the previous condition
  934. * `(!is_byte_a_type(type1) ||
  935. * wasm_is_type_multi_byte_type(type1))`. Therefore, the
  936. * original condition is redundant and has been removed.
  937. *
  938. * This logic is consistent with the implementation of the
  939. * `WASM_OP_REF_NULL` case in the `wasm_loader_prepare_bytecode`
  940. * function.
  941. */
  942. if (heap_type >= 0) {
  943. if (!check_type_index(module, module->type_count, heap_type,
  944. error_buf, error_buf_size)) {
  945. goto fail;
  946. }
  947. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  948. true, heap_type);
  949. if (!push_const_expr_stack(&const_expr_ctx, flag,
  950. cur_ref_type.ref_type,
  951. &cur_ref_type, 0, &cur_value,
  952. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  953. NULL,
  954. #endif
  955. error_buf, error_buf_size))
  956. goto fail;
  957. }
  958. else {
  959. if (!wasm_is_valid_heap_type(heap_type)) {
  960. set_error_buf_v(error_buf, error_buf_size,
  961. "unknown type %d", heap_type);
  962. goto fail;
  963. }
  964. cur_ref_type.ref_ht_common.ref_type =
  965. (uint8)((int32)0x80 + heap_type);
  966. if (!push_const_expr_stack(&const_expr_ctx, flag,
  967. cur_ref_type.ref_type, NULL, 0,
  968. &cur_value,
  969. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  970. NULL,
  971. #endif
  972. error_buf, error_buf_size))
  973. goto fail;
  974. }
  975. #endif
  976. #if WASM_ENABLE_WAMR_COMPILER != 0
  977. module->is_ref_types_used = true;
  978. #endif
  979. break;
  980. }
  981. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  982. /* get_global */
  983. case INIT_EXPR_TYPE_GET_GLOBAL:
  984. {
  985. uint32 global_idx;
  986. uint8 global_type;
  987. read_leb_uint32(p, p_end, cur_value.global_index);
  988. global_idx = cur_value.global_index;
  989. /*
  990. * Currently, constant expressions occurring as initializers
  991. * of globals are further constrained in that contained
  992. * global.get instructions are
  993. * only allowed to refer to imported globals.
  994. *
  995. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  996. */
  997. if (global_idx >= module->import_global_count
  998. /* make spec test happy */
  999. #if WASM_ENABLE_GC != 0
  1000. + module->global_count
  1001. #endif
  1002. ) {
  1003. set_error_buf_v(error_buf, error_buf_size,
  1004. "unknown global %u", global_idx);
  1005. goto fail;
  1006. }
  1007. if (
  1008. /* make spec test happy */
  1009. #if WASM_ENABLE_GC != 0
  1010. global_idx < module->import_global_count &&
  1011. #endif
  1012. module->import_globals[global_idx]
  1013. .u.global.type.is_mutable) {
  1014. set_error_buf_v(error_buf, error_buf_size,
  1015. "constant expression required");
  1016. goto fail;
  1017. }
  1018. if (global_idx < module->import_global_count) {
  1019. global_type = module->import_globals[global_idx]
  1020. .u.global.type.val_type;
  1021. #if WASM_ENABLE_GC != 0
  1022. if (wasm_is_type_multi_byte_type(global_type)) {
  1023. WASMRefType *global_ref_type =
  1024. module->import_globals[global_idx]
  1025. .u.global.ref_type;
  1026. bh_memcpy_s(&cur_ref_type,
  1027. wasm_reftype_struct_size(global_ref_type),
  1028. global_ref_type,
  1029. wasm_reftype_struct_size(global_ref_type));
  1030. }
  1031. #endif
  1032. }
  1033. else {
  1034. global_type =
  1035. module
  1036. ->globals[global_idx - module->import_global_count]
  1037. .type.val_type;
  1038. #if WASM_ENABLE_GC != 0
  1039. if (wasm_is_type_multi_byte_type(global_type)) {
  1040. WASMRefType *global_ref_type =
  1041. module
  1042. ->globals[global_idx
  1043. - module->import_global_count]
  1044. .ref_type;
  1045. bh_memcpy_s(&cur_ref_type,
  1046. wasm_reftype_struct_size(global_ref_type),
  1047. global_ref_type,
  1048. wasm_reftype_struct_size(global_ref_type));
  1049. }
  1050. #endif
  1051. }
  1052. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  1053. #if WASM_ENABLE_GC != 0
  1054. &cur_ref_type, 0,
  1055. #endif
  1056. &cur_value,
  1057. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1058. NULL,
  1059. #endif
  1060. error_buf, error_buf_size))
  1061. goto fail;
  1062. break;
  1063. }
  1064. #if WASM_ENABLE_GC != 0
  1065. /* struct.new and array.new */
  1066. case WASM_OP_GC_PREFIX:
  1067. {
  1068. read_leb_uint32(p, p_end, opcode1);
  1069. switch (opcode1) {
  1070. case WASM_OP_STRUCT_NEW:
  1071. {
  1072. WASMStructType *struct_type;
  1073. WASMStructNewInitValues *struct_init_values = NULL;
  1074. uint32 field_count;
  1075. read_leb_uint32(p, p_end, type_idx);
  1076. if (!check_type_index(module, module->type_count,
  1077. type_idx, error_buf,
  1078. error_buf_size)) {
  1079. goto fail;
  1080. }
  1081. struct_type = (WASMStructType *)module->types[type_idx];
  1082. if (struct_type->base_type.type_flag
  1083. != WASM_TYPE_STRUCT) {
  1084. set_error_buf(error_buf, error_buf_size,
  1085. "unknown struct type");
  1086. goto fail;
  1087. }
  1088. field_count = struct_type->field_count;
  1089. if (!(struct_init_values = loader_malloc(
  1090. offsetof(WASMStructNewInitValues, fields)
  1091. + (uint64)field_count * sizeof(WASMValue),
  1092. error_buf, error_buf_size))) {
  1093. goto fail;
  1094. }
  1095. struct_init_values->type_idx = type_idx;
  1096. struct_init_values->count = field_count;
  1097. for (i = field_count; i > 0; i--) {
  1098. WASMRefType *field_ref_type = NULL;
  1099. uint32 field_idx = i - 1;
  1100. uint8 field_type =
  1101. struct_type->fields[field_idx].field_type;
  1102. if (wasm_is_type_multi_byte_type(field_type)) {
  1103. field_ref_type = wasm_reftype_map_find(
  1104. struct_type->ref_type_maps,
  1105. struct_type->ref_type_map_count, field_idx);
  1106. }
  1107. if (is_packed_type(field_type)) {
  1108. field_type = VALUE_TYPE_I32;
  1109. }
  1110. if (!pop_const_expr_stack(
  1111. &const_expr_ctx, NULL, field_type,
  1112. field_ref_type, NULL,
  1113. &struct_init_values->fields[field_idx],
  1114. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1115. NULL,
  1116. #endif
  1117. error_buf, error_buf_size)) {
  1118. destroy_init_expr_data_recursive(
  1119. module, struct_init_values);
  1120. goto fail;
  1121. }
  1122. }
  1123. cur_value.data = struct_init_values;
  1124. wasm_set_refheaptype_typeidx(
  1125. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1126. if (!push_const_expr_stack(
  1127. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1128. &cur_ref_type, (uint8)opcode1, &cur_value,
  1129. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1130. NULL,
  1131. #endif
  1132. error_buf, error_buf_size)) {
  1133. destroy_init_expr_data_recursive(
  1134. module, struct_init_values);
  1135. goto fail;
  1136. }
  1137. break;
  1138. }
  1139. case WASM_OP_STRUCT_NEW_DEFAULT:
  1140. {
  1141. read_leb_uint32(p, p_end, cur_value.type_index);
  1142. type_idx = cur_value.type_index;
  1143. if (!check_type_index(module, module->type_count,
  1144. type_idx, error_buf,
  1145. error_buf_size)) {
  1146. goto fail;
  1147. }
  1148. if (module->types[type_idx] == NULL
  1149. || module->types[type_idx]->type_flag
  1150. != WASM_TYPE_STRUCT) {
  1151. set_error_buf(error_buf, error_buf_size,
  1152. "unknown struct type");
  1153. goto fail;
  1154. }
  1155. cur_value.type_index = type_idx;
  1156. cur_value.data = NULL;
  1157. wasm_set_refheaptype_typeidx(
  1158. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1159. if (!push_const_expr_stack(
  1160. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1161. &cur_ref_type, (uint8)opcode1, &cur_value,
  1162. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1163. NULL,
  1164. #endif
  1165. error_buf, error_buf_size)) {
  1166. goto fail;
  1167. }
  1168. break;
  1169. }
  1170. case WASM_OP_ARRAY_NEW:
  1171. case WASM_OP_ARRAY_NEW_DEFAULT:
  1172. case WASM_OP_ARRAY_NEW_FIXED:
  1173. {
  1174. WASMArrayNewInitValues *array_init_values = NULL;
  1175. WASMArrayType *array_type = NULL;
  1176. WASMRefType *elem_ref_type = NULL;
  1177. uint64 total_size;
  1178. uint8 elem_type;
  1179. read_leb_uint32(p, p_end, cur_value.type_index);
  1180. type_idx = cur_value.type_index;
  1181. if (!check_type_index(module, module->type_count,
  1182. type_idx, error_buf,
  1183. error_buf_size)) {
  1184. goto fail;
  1185. }
  1186. array_type = (WASMArrayType *)module->types[type_idx];
  1187. if (array_type->base_type.type_flag
  1188. != WASM_TYPE_ARRAY) {
  1189. set_error_buf(error_buf, error_buf_size,
  1190. "unknown array type");
  1191. goto fail;
  1192. }
  1193. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  1194. elem_type = array_type->elem_type;
  1195. if (wasm_is_type_multi_byte_type(elem_type)) {
  1196. elem_ref_type = array_type->elem_ref_type;
  1197. }
  1198. if (is_packed_type(elem_type)) {
  1199. elem_type = VALUE_TYPE_I32;
  1200. }
  1201. if (opcode1 == WASM_OP_ARRAY_NEW) {
  1202. WASMValue len_val = { 0 };
  1203. uint64 size = 0;
  1204. if (!pop_const_expr_stack(
  1205. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  1206. NULL, NULL, &len_val,
  1207. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1208. NULL,
  1209. #endif
  1210. error_buf, error_buf_size)) {
  1211. goto fail;
  1212. }
  1213. size =
  1214. sizeof(WASMArrayNewInitValues)
  1215. + sizeof(WASMValue) * (uint64)len_val.i32;
  1216. if (!(array_init_values = loader_malloc(
  1217. size, error_buf, error_buf_size))) {
  1218. goto fail;
  1219. }
  1220. array_init_values->type_idx = type_idx;
  1221. array_init_values->length = len_val.i32;
  1222. if (!pop_const_expr_stack(
  1223. &const_expr_ctx, NULL, elem_type,
  1224. elem_ref_type, NULL,
  1225. &array_init_values->elem_data[0],
  1226. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1227. NULL,
  1228. #endif
  1229. error_buf, error_buf_size)) {
  1230. destroy_init_expr_data_recursive(
  1231. module, array_init_values);
  1232. goto fail;
  1233. }
  1234. cur_value.data = array_init_values;
  1235. }
  1236. else {
  1237. /* WASM_OP_ARRAY_NEW_FIXED */
  1238. uint32 len;
  1239. read_leb_uint32(p, p_end, len);
  1240. total_size =
  1241. (uint64)offsetof(WASMArrayNewInitValues,
  1242. elem_data)
  1243. + (uint64)sizeof(WASMValue) * len;
  1244. if (!(array_init_values =
  1245. loader_malloc(total_size, error_buf,
  1246. error_buf_size))) {
  1247. goto fail;
  1248. }
  1249. array_init_values->type_idx = type_idx;
  1250. array_init_values->length = len;
  1251. for (i = len; i > 0; i--) {
  1252. if (!pop_const_expr_stack(
  1253. &const_expr_ctx, NULL, elem_type,
  1254. elem_ref_type, NULL,
  1255. &array_init_values
  1256. ->elem_data[i - 1],
  1257. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1258. NULL,
  1259. #endif
  1260. error_buf, error_buf_size)) {
  1261. destroy_init_expr_data_recursive(
  1262. module, array_init_values);
  1263. goto fail;
  1264. }
  1265. }
  1266. cur_value.data = array_init_values;
  1267. }
  1268. }
  1269. else {
  1270. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1271. WASMValue len_val;
  1272. uint32 len;
  1273. /* POP(i32) */
  1274. if (!pop_const_expr_stack(
  1275. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1276. NULL, &len_val,
  1277. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1278. NULL,
  1279. #endif
  1280. error_buf, error_buf_size)) {
  1281. goto fail;
  1282. }
  1283. len = len_val.i32;
  1284. cur_value.array_new_default.type_index = type_idx;
  1285. cur_value.array_new_default.length = len;
  1286. }
  1287. wasm_set_refheaptype_typeidx(
  1288. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1289. if (!push_const_expr_stack(
  1290. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1291. &cur_ref_type, (uint8)opcode1, &cur_value,
  1292. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1293. NULL,
  1294. #endif
  1295. error_buf, error_buf_size)) {
  1296. if (array_init_values) {
  1297. destroy_init_expr_data_recursive(
  1298. module, array_init_values);
  1299. }
  1300. goto fail;
  1301. }
  1302. break;
  1303. }
  1304. case WASM_OP_ANY_CONVERT_EXTERN:
  1305. {
  1306. set_error_buf(error_buf, error_buf_size,
  1307. "unsupported constant expression of "
  1308. "extern.internalize");
  1309. goto fail;
  1310. }
  1311. case WASM_OP_EXTERN_CONVERT_ANY:
  1312. {
  1313. set_error_buf(error_buf, error_buf_size,
  1314. "unsupported constant expression of "
  1315. "extern.externalize");
  1316. goto fail;
  1317. }
  1318. case WASM_OP_REF_I31:
  1319. {
  1320. /* POP(i32) */
  1321. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1322. VALUE_TYPE_I32, NULL, NULL,
  1323. &cur_value,
  1324. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1325. NULL,
  1326. #endif
  1327. error_buf, error_buf_size)) {
  1328. goto fail;
  1329. }
  1330. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1331. false, HEAP_TYPE_I31);
  1332. if (!push_const_expr_stack(
  1333. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1334. &cur_ref_type, (uint8)opcode1, &cur_value,
  1335. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1336. NULL,
  1337. #endif
  1338. error_buf, error_buf_size)) {
  1339. goto fail;
  1340. }
  1341. break;
  1342. }
  1343. default:
  1344. set_error_buf(
  1345. error_buf, error_buf_size,
  1346. "type mismatch or constant expression required");
  1347. goto fail;
  1348. }
  1349. break;
  1350. }
  1351. #endif /* end of WASM_ENABLE_GC != 0 */
  1352. default:
  1353. {
  1354. set_error_buf(error_buf, error_buf_size,
  1355. "illegal opcode "
  1356. "or constant expression required "
  1357. "or type mismatch");
  1358. goto fail;
  1359. }
  1360. }
  1361. CHECK_BUF(p, p_end, 1);
  1362. flag = read_uint8(p);
  1363. }
  1364. /* There should be only one value left on the init value stack */
  1365. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1366. #if WASM_ENABLE_GC != 0
  1367. ref_type, &opcode,
  1368. #endif
  1369. &cur_value,
  1370. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1371. &cur_expr,
  1372. #endif
  1373. error_buf, error_buf_size)) {
  1374. goto fail;
  1375. }
  1376. if (const_expr_ctx.sp != 0) {
  1377. set_error_buf(error_buf, error_buf_size,
  1378. "type mismatch: illegal constant opcode sequence");
  1379. goto fail;
  1380. }
  1381. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1382. if (cur_expr != NULL) {
  1383. bh_memcpy_s(init_expr, sizeof(InitializerExpression), cur_expr,
  1384. sizeof(InitializerExpression));
  1385. wasm_runtime_free(cur_expr);
  1386. }
  1387. else {
  1388. init_expr->init_expr_type = flag;
  1389. init_expr->u.unary.v = cur_value;
  1390. }
  1391. #else
  1392. init_expr->init_expr_type = flag;
  1393. init_expr->u.unary.v = cur_value;
  1394. #endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR != 0 */
  1395. #if WASM_ENABLE_GC != 0
  1396. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1397. switch (opcode) {
  1398. case WASM_OP_STRUCT_NEW:
  1399. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1400. break;
  1401. case WASM_OP_STRUCT_NEW_DEFAULT:
  1402. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1403. break;
  1404. case WASM_OP_ARRAY_NEW:
  1405. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1406. break;
  1407. case WASM_OP_ARRAY_NEW_DEFAULT:
  1408. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1409. break;
  1410. case WASM_OP_ARRAY_NEW_FIXED:
  1411. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1412. break;
  1413. case WASM_OP_REF_I31:
  1414. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1415. break;
  1416. default:
  1417. bh_assert(0);
  1418. break;
  1419. }
  1420. }
  1421. #endif /* end of WASM_ENABLE_GC != 0 */
  1422. *p_buf = p;
  1423. destroy_const_expr_stack(&const_expr_ctx, false);
  1424. return true;
  1425. fail:
  1426. destroy_const_expr_stack(&const_expr_ctx, true);
  1427. return false;
  1428. }
  1429. static bool
  1430. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1431. {
  1432. if (mutable >= 2) {
  1433. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1434. return false;
  1435. }
  1436. return true;
  1437. }
  1438. #if WASM_ENABLE_GC != 0
  1439. static void
  1440. destroy_func_type(WASMFuncType *type)
  1441. {
  1442. /* Destroy the reference type hash set */
  1443. if (type->ref_type_maps)
  1444. wasm_runtime_free(type->ref_type_maps);
  1445. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1446. && WASM_ENABLE_LAZY_JIT != 0
  1447. if (type->call_to_llvm_jit_from_fast_jit)
  1448. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1449. #endif
  1450. /* Free the type */
  1451. wasm_runtime_free(type);
  1452. }
  1453. static void
  1454. destroy_struct_type(WASMStructType *type)
  1455. {
  1456. if (type->ref_type_maps)
  1457. wasm_runtime_free(type->ref_type_maps);
  1458. wasm_runtime_free(type);
  1459. }
  1460. static void
  1461. destroy_array_type(WASMArrayType *type)
  1462. {
  1463. wasm_runtime_free(type);
  1464. }
  1465. static void
  1466. destroy_wasm_type(WASMType *type)
  1467. {
  1468. if (type->ref_count > 1) {
  1469. /* The type is referenced by other types
  1470. of current wasm module */
  1471. type->ref_count--;
  1472. return;
  1473. }
  1474. if (type->type_flag == WASM_TYPE_FUNC)
  1475. destroy_func_type((WASMFuncType *)type);
  1476. else if (type->type_flag == WASM_TYPE_STRUCT)
  1477. destroy_struct_type((WASMStructType *)type);
  1478. else if (type->type_flag == WASM_TYPE_ARRAY)
  1479. destroy_array_type((WASMArrayType *)type);
  1480. else {
  1481. bh_assert(0);
  1482. }
  1483. }
  1484. /* Resolve (ref null ht) or (ref ht) */
  1485. static bool
  1486. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1487. WASMModule *module, uint32 type_count, bool nullable,
  1488. WASMRefType *ref_type, char *error_buf,
  1489. uint32 error_buf_size)
  1490. {
  1491. const uint8 *p = *p_buf, *p_end = buf_end;
  1492. ref_type->ref_type =
  1493. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1494. ref_type->ref_ht_common.nullable = nullable;
  1495. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1496. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1497. /* heap type is (type i), i : typeidx, >= 0 */
  1498. if (!check_type_index(module, type_count,
  1499. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1500. error_buf_size)) {
  1501. return false;
  1502. }
  1503. }
  1504. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1505. /* heap type is func, extern, any, eq, i31 or data */
  1506. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1507. return false;
  1508. }
  1509. *p_buf = p;
  1510. return true;
  1511. fail:
  1512. return false;
  1513. }
  1514. static bool
  1515. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1516. WASMModule *module, uint32 type_count,
  1517. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1518. bool allow_packed_type, char *error_buf,
  1519. uint32 error_buf_size)
  1520. {
  1521. const uint8 *p = *p_buf, *p_end = buf_end;
  1522. uint8 type;
  1523. memset(ref_type, 0, sizeof(WASMRefType));
  1524. CHECK_BUF(p, p_end, 1);
  1525. type = read_uint8(p);
  1526. if (wasm_is_reftype_htref_nullable(type)) {
  1527. /* (ref null ht) */
  1528. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1529. ref_type, error_buf, error_buf_size))
  1530. return false;
  1531. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1532. *p_need_ref_type_map = true;
  1533. else {
  1534. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1535. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1536. multi-byte type to one-byte type to reduce the footprint and
  1537. the complexity of type equal/subtype checking */
  1538. ref_type->ref_type =
  1539. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1540. *p_need_ref_type_map = false;
  1541. }
  1542. }
  1543. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1544. /* (ref ht) */
  1545. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1546. ref_type, error_buf, error_buf_size))
  1547. return false;
  1548. *p_need_ref_type_map = true;
  1549. #if WASM_ENABLE_STRINGREF != 0
  1550. /* covert (ref string) to stringref */
  1551. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1552. ref_type->ref_type =
  1553. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1554. *p_need_ref_type_map = false;
  1555. }
  1556. #endif
  1557. }
  1558. else {
  1559. /* type which can be represented by one byte */
  1560. if (!is_valid_value_type_for_interpreter(type)
  1561. && !(allow_packed_type && is_packed_type(type))) {
  1562. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1563. return false;
  1564. }
  1565. ref_type->ref_type = type;
  1566. *p_need_ref_type_map = false;
  1567. #if WASM_ENABLE_WAMR_COMPILER != 0
  1568. /* If any value's type is v128, mark the module as SIMD used */
  1569. if (type == VALUE_TYPE_V128)
  1570. module->is_simd_used = true;
  1571. #endif
  1572. }
  1573. *p_buf = p;
  1574. return true;
  1575. fail:
  1576. return false;
  1577. }
  1578. static WASMRefType *
  1579. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1580. char *error_buf, uint32 error_buf_size)
  1581. {
  1582. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1583. if (!ret) {
  1584. set_error_buf(error_buf, error_buf_size,
  1585. "insert ref type to hash set failed");
  1586. }
  1587. return ret;
  1588. }
  1589. static bool
  1590. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1591. uint32 type_count, uint32 type_idx, char *error_buf,
  1592. uint32 error_buf_size)
  1593. {
  1594. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1595. uint32 param_count, result_count, i, j = 0;
  1596. uint32 param_cell_num, ret_cell_num;
  1597. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1598. uint64 total_size;
  1599. bool need_ref_type_map;
  1600. WASMRefType ref_type;
  1601. WASMFuncType *type = NULL;
  1602. /* Parse first time to resolve param count, result count and
  1603. ref type map count */
  1604. read_leb_uint32(p, p_end, param_count);
  1605. p_org = p;
  1606. for (i = 0; i < param_count; i++) {
  1607. if (!resolve_value_type(&p, p_end, module, type_count,
  1608. &need_ref_type_map, &ref_type, false, error_buf,
  1609. error_buf_size)) {
  1610. return false;
  1611. }
  1612. if (need_ref_type_map)
  1613. ref_type_map_count++;
  1614. }
  1615. read_leb_uint32(p, p_end, result_count);
  1616. for (i = 0; i < result_count; i++) {
  1617. if (!resolve_value_type(&p, p_end, module, type_count,
  1618. &need_ref_type_map, &ref_type, false, error_buf,
  1619. error_buf_size)) {
  1620. return false;
  1621. }
  1622. if (need_ref_type_map) {
  1623. ref_type_map_count++;
  1624. result_ref_type_map_count++;
  1625. }
  1626. }
  1627. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1628. "ref type map count: %d",
  1629. type_idx, param_count, result_count, ref_type_map_count);
  1630. /* Parse second time to resolve param types, result types and
  1631. ref type map info */
  1632. p = p_org;
  1633. total_size = offsetof(WASMFuncType, types)
  1634. + sizeof(uint8) * (uint64)(param_count + result_count);
  1635. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1636. return false;
  1637. }
  1638. if (ref_type_map_count > 0) {
  1639. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1640. if (!(type->ref_type_maps =
  1641. loader_malloc(total_size, error_buf, error_buf_size))) {
  1642. goto fail;
  1643. }
  1644. }
  1645. type->base_type.type_flag = WASM_TYPE_FUNC;
  1646. type->param_count = param_count;
  1647. type->result_count = result_count;
  1648. type->ref_type_map_count = ref_type_map_count;
  1649. if (ref_type_map_count > 0) {
  1650. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1651. - result_ref_type_map_count;
  1652. }
  1653. for (i = 0; i < param_count; i++) {
  1654. if (!resolve_value_type(&p, p_end, module, type_count,
  1655. &need_ref_type_map, &ref_type, false, error_buf,
  1656. error_buf_size)) {
  1657. goto fail;
  1658. }
  1659. type->types[i] = ref_type.ref_type;
  1660. if (need_ref_type_map) {
  1661. type->ref_type_maps[j].index = i;
  1662. if (!(type->ref_type_maps[j++].ref_type =
  1663. reftype_set_insert(module->ref_type_set, &ref_type,
  1664. error_buf, error_buf_size))) {
  1665. goto fail;
  1666. }
  1667. }
  1668. }
  1669. read_leb_uint32(p, p_end, result_count);
  1670. for (i = 0; i < result_count; i++) {
  1671. if (!resolve_value_type(&p, p_end, module, type_count,
  1672. &need_ref_type_map, &ref_type, false, error_buf,
  1673. error_buf_size)) {
  1674. goto fail;
  1675. }
  1676. type->types[param_count + i] = ref_type.ref_type;
  1677. if (need_ref_type_map) {
  1678. type->ref_type_maps[j].index = param_count + i;
  1679. if (!(type->ref_type_maps[j++].ref_type =
  1680. reftype_set_insert(module->ref_type_set, &ref_type,
  1681. error_buf, error_buf_size))) {
  1682. goto fail;
  1683. }
  1684. }
  1685. }
  1686. bh_assert(j == type->ref_type_map_count);
  1687. #if TRACE_WASM_LOADER != 0
  1688. os_printf("type %d = ", type_idx);
  1689. wasm_dump_func_type(type);
  1690. #endif
  1691. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1692. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1693. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1694. set_error_buf(error_buf, error_buf_size,
  1695. "param count or result count too large");
  1696. goto fail;
  1697. }
  1698. type->param_cell_num = (uint16)param_cell_num;
  1699. type->ret_cell_num = (uint16)ret_cell_num;
  1700. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1701. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1702. #endif
  1703. #if WASM_ENABLE_WAMR_COMPILER != 0
  1704. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1705. if (type->types[i] == VALUE_TYPE_V128)
  1706. module->is_simd_used = true;
  1707. }
  1708. #endif
  1709. *p_buf = p;
  1710. module->types[type_idx] = (WASMType *)type;
  1711. return true;
  1712. fail:
  1713. if (type)
  1714. destroy_func_type(type);
  1715. return false;
  1716. }
  1717. static bool
  1718. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1719. WASMModule *module, uint32 type_count, uint32 type_idx,
  1720. char *error_buf, uint32 error_buf_size)
  1721. {
  1722. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1723. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1724. uint32 i, j = 0, offset;
  1725. uint16 *reference_table;
  1726. uint64 total_size;
  1727. uint8 mutable;
  1728. bool need_ref_type_map;
  1729. WASMRefType ref_type;
  1730. WASMStructType *type = NULL;
  1731. /* Parse first time to resolve field count and ref type map count */
  1732. read_leb_uint32(p, p_end, field_count);
  1733. p_org = p;
  1734. for (i = 0; i < field_count; i++) {
  1735. if (!resolve_value_type(&p, p_end, module, type_count,
  1736. &need_ref_type_map, &ref_type, true, error_buf,
  1737. error_buf_size)) {
  1738. return false;
  1739. }
  1740. if (need_ref_type_map)
  1741. ref_type_map_count++;
  1742. if (wasm_is_type_reftype(ref_type.ref_type))
  1743. ref_field_count++;
  1744. CHECK_BUF(p, p_end, 1);
  1745. mutable = read_uint8(p);
  1746. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1747. return false;
  1748. }
  1749. }
  1750. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1751. type_idx, field_count, ref_type_map_count);
  1752. /* Parse second time to resolve field types and ref type map info */
  1753. p = p_org;
  1754. total_size = offsetof(WASMStructType, fields)
  1755. + sizeof(WASMStructFieldType) * (uint64)field_count
  1756. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1757. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1758. return false;
  1759. }
  1760. if (ref_type_map_count > 0) {
  1761. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1762. if (!(type->ref_type_maps =
  1763. loader_malloc(total_size, error_buf, error_buf_size))) {
  1764. goto fail;
  1765. }
  1766. }
  1767. type->reference_table = reference_table =
  1768. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1769. + sizeof(WASMStructFieldType) * field_count);
  1770. *reference_table++ = ref_field_count;
  1771. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1772. type->field_count = field_count;
  1773. type->ref_type_map_count = ref_type_map_count;
  1774. offset = (uint32)offsetof(WASMStructObject, field_data);
  1775. for (i = 0; i < field_count; i++) {
  1776. if (!resolve_value_type(&p, p_end, module, type_count,
  1777. &need_ref_type_map, &ref_type, true, error_buf,
  1778. error_buf_size)) {
  1779. goto fail;
  1780. }
  1781. type->fields[i].field_type = ref_type.ref_type;
  1782. if (need_ref_type_map) {
  1783. type->ref_type_maps[j].index = i;
  1784. if (!(type->ref_type_maps[j++].ref_type =
  1785. reftype_set_insert(module->ref_type_set, &ref_type,
  1786. error_buf, error_buf_size))) {
  1787. goto fail;
  1788. }
  1789. }
  1790. CHECK_BUF(p, p_end, 1);
  1791. type->fields[i].field_flags = read_uint8(p);
  1792. type->fields[i].field_size =
  1793. (uint8)wasm_reftype_size(ref_type.ref_type);
  1794. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1795. || defined(BUILD_TARGET_X86_32))
  1796. if (type->fields[i].field_size == 2)
  1797. offset = align_uint(offset, 2);
  1798. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1799. offset = align_uint(offset, 4);
  1800. #endif
  1801. type->fields[i].field_offset = offset;
  1802. if (wasm_is_type_reftype(ref_type.ref_type))
  1803. *reference_table++ = offset;
  1804. offset += type->fields[i].field_size;
  1805. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1806. type->fields[i].field_flags, type->fields[i].field_type);
  1807. }
  1808. type->total_size = offset;
  1809. bh_assert(j == type->ref_type_map_count);
  1810. #if TRACE_WASM_LOADER != 0
  1811. os_printf("type %d = ", type_idx);
  1812. wasm_dump_struct_type(type);
  1813. #endif
  1814. *p_buf = p;
  1815. module->types[type_idx] = (WASMType *)type;
  1816. return true;
  1817. fail:
  1818. if (type)
  1819. destroy_struct_type(type);
  1820. return false;
  1821. }
  1822. static bool
  1823. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1824. WASMModule *module, uint32 type_count, uint32 type_idx,
  1825. char *error_buf, uint32 error_buf_size)
  1826. {
  1827. const uint8 *p = *p_buf, *p_end = buf_end;
  1828. uint8 mutable;
  1829. bool need_ref_type_map;
  1830. WASMRefType ref_type;
  1831. WASMArrayType *type = NULL;
  1832. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1833. &ref_type, true, error_buf, error_buf_size)) {
  1834. return false;
  1835. }
  1836. CHECK_BUF(p, p_end, 1);
  1837. mutable = read_uint8(p);
  1838. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1839. return false;
  1840. }
  1841. LOG_VERBOSE("type %u: array", type_idx);
  1842. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1843. error_buf_size))) {
  1844. return false;
  1845. }
  1846. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1847. type->elem_flags = mutable;
  1848. type->elem_type = ref_type.ref_type;
  1849. if (need_ref_type_map) {
  1850. if (!(type->elem_ref_type =
  1851. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1852. error_buf_size))) {
  1853. goto fail;
  1854. }
  1855. }
  1856. #if TRACE_WASM_LOADER != 0
  1857. os_printf("type %d = ", type_idx);
  1858. wasm_dump_array_type(type);
  1859. #endif
  1860. *p_buf = p;
  1861. module->types[type_idx] = (WASMType *)type;
  1862. return true;
  1863. fail:
  1864. if (type)
  1865. destroy_array_type(type);
  1866. return false;
  1867. }
  1868. static bool
  1869. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1870. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1871. {
  1872. if (heap_type >= 0) {
  1873. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1874. error_buf_size)) {
  1875. return false;
  1876. }
  1877. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1878. heap_type);
  1879. }
  1880. else {
  1881. if (!wasm_is_valid_heap_type(heap_type)) {
  1882. set_error_buf(error_buf, error_buf_size, "unknown type");
  1883. return false;
  1884. }
  1885. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1886. heap_type);
  1887. if (nullable) {
  1888. /* For (ref null func/extern/any/eq/i31/data),
  1889. they are same as
  1890. funcref/externref/anyref/eqref/i31ref/dataref,
  1891. we convert the multi-byte type to one-byte
  1892. type to reduce the footprint and the
  1893. complexity of type equal/subtype checking */
  1894. ref_type->ref_type =
  1895. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1896. }
  1897. }
  1898. return true;
  1899. }
  1900. static void
  1901. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1902. WASMRefType *ref_type2)
  1903. {
  1904. /**
  1905. * The difference rt1 ∖ rt2 between two reference types is defined as
  1906. * follows:
  1907. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1908. * (ref ht2) = (ref null?1 ht1)
  1909. */
  1910. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1911. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1912. ref_type1, wasm_reftype_struct_size(ref_type1));
  1913. }
  1914. else {
  1915. ref_type_diff->ref_type = ref_type1->ref_type;
  1916. }
  1917. if (ref_type2->ref_ht_common.nullable) {
  1918. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1919. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1920. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1921. (int32)ref_type_diff->ref_type - 0x80);
  1922. }
  1923. else {
  1924. ref_type_diff->ref_ht_typeidx.nullable = false;
  1925. }
  1926. }
  1927. }
  1928. #else /* else of WASM_ENABLE_GC != 0 */
  1929. static void
  1930. destroy_wasm_type(WASMType *type)
  1931. {
  1932. if (type->ref_count > 1) {
  1933. /* The type is referenced by other types
  1934. of current wasm module */
  1935. type->ref_count--;
  1936. return;
  1937. }
  1938. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1939. && WASM_ENABLE_LAZY_JIT != 0
  1940. if (type->call_to_llvm_jit_from_fast_jit)
  1941. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1942. #endif
  1943. wasm_runtime_free(type);
  1944. }
  1945. #endif /* end of WASM_ENABLE_GC != 0 */
  1946. static bool
  1947. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1948. char *error_buf, uint32 error_buf_size)
  1949. {
  1950. const uint8 *p = buf, *p_end = buf_end;
  1951. uint32 type_count, i;
  1952. uint64 total_size;
  1953. uint8 flag;
  1954. #if WASM_ENABLE_GC != 0
  1955. uint32 processed_type_count = 0;
  1956. #endif
  1957. read_leb_uint32(p, p_end, type_count);
  1958. if (type_count) {
  1959. module->type_count = type_count;
  1960. total_size = sizeof(WASMType *) * (uint64)type_count;
  1961. if (!(module->types =
  1962. loader_malloc(total_size, error_buf, error_buf_size))) {
  1963. return false;
  1964. }
  1965. #if WASM_ENABLE_GC == 0
  1966. for (i = 0; i < type_count; i++) {
  1967. WASMFuncType *type;
  1968. const uint8 *p_org;
  1969. uint32 param_count, result_count, j;
  1970. uint32 param_cell_num, ret_cell_num;
  1971. CHECK_BUF(p, p_end, 1);
  1972. flag = read_uint8(p);
  1973. if (flag != 0x60) {
  1974. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1975. return false;
  1976. }
  1977. read_leb_uint32(p, p_end, param_count);
  1978. /* Resolve param count and result count firstly */
  1979. p_org = p;
  1980. CHECK_BUF(p, p_end, param_count);
  1981. p += param_count;
  1982. read_leb_uint32(p, p_end, result_count);
  1983. CHECK_BUF(p, p_end, result_count);
  1984. p = p_org;
  1985. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1986. set_error_buf(error_buf, error_buf_size,
  1987. "param count or result count too large");
  1988. return false;
  1989. }
  1990. total_size = offsetof(WASMFuncType, types)
  1991. + sizeof(uint8) * (uint64)(param_count + result_count);
  1992. if (!(type = module->types[i] =
  1993. loader_malloc(total_size, error_buf, error_buf_size))) {
  1994. return false;
  1995. }
  1996. /* Resolve param types and result types */
  1997. type->ref_count = 1;
  1998. type->param_count = (uint16)param_count;
  1999. type->result_count = (uint16)result_count;
  2000. for (j = 0; j < param_count; j++) {
  2001. CHECK_BUF(p, p_end, 1);
  2002. type->types[j] = read_uint8(p);
  2003. }
  2004. read_leb_uint32(p, p_end, result_count);
  2005. for (j = 0; j < result_count; j++) {
  2006. CHECK_BUF(p, p_end, 1);
  2007. type->types[param_count + j] = read_uint8(p);
  2008. }
  2009. for (j = 0; j < param_count + result_count; j++) {
  2010. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  2011. set_error_buf(error_buf, error_buf_size,
  2012. "unknown value type");
  2013. return false;
  2014. }
  2015. }
  2016. param_cell_num = wasm_get_cell_num(type->types, param_count);
  2017. ret_cell_num =
  2018. wasm_get_cell_num(type->types + param_count, result_count);
  2019. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  2020. set_error_buf(error_buf, error_buf_size,
  2021. "param count or result count too large");
  2022. return false;
  2023. }
  2024. type->param_cell_num = (uint16)param_cell_num;
  2025. type->ret_cell_num = (uint16)ret_cell_num;
  2026. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  2027. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  2028. #endif
  2029. #if WASM_ENABLE_WAMR_COMPILER != 0
  2030. for (j = 0; j < type->param_count + type->result_count; j++) {
  2031. if (type->types[j] == VALUE_TYPE_V128)
  2032. module->is_simd_used = true;
  2033. else if (type->types[j] == VALUE_TYPE_FUNCREF
  2034. || type->types[j] == VALUE_TYPE_EXTERNREF)
  2035. module->is_ref_types_used = true;
  2036. }
  2037. #endif
  2038. /* If there is already a same type created, use it instead */
  2039. for (j = 0; j < i; j++) {
  2040. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  2041. if (module->types[j]->ref_count == UINT16_MAX) {
  2042. set_error_buf(error_buf, error_buf_size,
  2043. "wasm type's ref count too large");
  2044. return false;
  2045. }
  2046. destroy_wasm_type(type);
  2047. module->types[i] = module->types[j];
  2048. module->types[j]->ref_count++;
  2049. break;
  2050. }
  2051. }
  2052. }
  2053. #else /* else of WASM_ENABLE_GC == 0 */
  2054. for (i = 0; i < type_count; i++) {
  2055. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  2056. uint32 rec_count = 1, j;
  2057. bool is_sub_final = true;
  2058. CHECK_BUF(p, p_end, 1);
  2059. flag = read_uint8(p);
  2060. if (flag == DEFINED_TYPE_REC) {
  2061. read_leb_uint32(p, p_end, rec_count);
  2062. if (rec_count > 1) {
  2063. uint64 new_total_size;
  2064. /* integer overflow */
  2065. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  2066. set_error_buf(error_buf, error_buf_size,
  2067. "recursive type count too large");
  2068. return false;
  2069. }
  2070. new_total_size =
  2071. sizeof(WASMFuncType *)
  2072. * (uint64)(module->type_count + rec_count - 1);
  2073. if (new_total_size > UINT32_MAX) {
  2074. set_error_buf(error_buf, error_buf_size,
  2075. "allocate memory failed");
  2076. return false;
  2077. }
  2078. MEM_REALLOC(module->types, (uint32)total_size,
  2079. (uint32)new_total_size);
  2080. module->type_count += rec_count - 1;
  2081. total_size = new_total_size;
  2082. }
  2083. if (rec_count < 1) {
  2084. LOG_VERBOSE("Processing 0-entry rec group");
  2085. }
  2086. else {
  2087. LOG_VERBOSE("Processing rec group [%d-%d]",
  2088. processed_type_count,
  2089. processed_type_count + rec_count - 1);
  2090. }
  2091. }
  2092. else {
  2093. p--;
  2094. }
  2095. for (j = 0; j < rec_count; j++) {
  2096. WASMType *cur_type = NULL;
  2097. CHECK_BUF(p, p_end, 1);
  2098. flag = read_uint8(p);
  2099. parent_type_idx = -1;
  2100. if (flag == DEFINED_TYPE_SUB
  2101. || flag == DEFINED_TYPE_SUB_FINAL) {
  2102. read_leb_uint32(p, p_end, super_type_count);
  2103. if (super_type_count > 1) {
  2104. set_error_buf(error_buf, error_buf_size,
  2105. "super type count too large");
  2106. return false;
  2107. }
  2108. if (super_type_count > 0) {
  2109. read_leb_uint32(p, p_end, parent_type_idx);
  2110. if (parent_type_idx >= processed_type_count + j) {
  2111. set_error_buf_v(error_buf, error_buf_size,
  2112. "unknown type %d", parent_type_idx);
  2113. return false;
  2114. }
  2115. if (module->types[parent_type_idx]->is_sub_final) {
  2116. set_error_buf(error_buf, error_buf_size,
  2117. "sub type can not inherit from "
  2118. "a final super type");
  2119. return false;
  2120. }
  2121. }
  2122. if (flag == DEFINED_TYPE_SUB)
  2123. is_sub_final = false;
  2124. CHECK_BUF(p, p_end, 1);
  2125. flag = read_uint8(p);
  2126. }
  2127. if (flag == DEFINED_TYPE_FUNC) {
  2128. if (!resolve_func_type(&p, buf_end, module,
  2129. processed_type_count + rec_count,
  2130. processed_type_count + j, error_buf,
  2131. error_buf_size)) {
  2132. return false;
  2133. }
  2134. }
  2135. else if (flag == DEFINED_TYPE_STRUCT) {
  2136. if (!resolve_struct_type(&p, buf_end, module,
  2137. processed_type_count + rec_count,
  2138. processed_type_count + j,
  2139. error_buf, error_buf_size)) {
  2140. return false;
  2141. }
  2142. }
  2143. else if (flag == DEFINED_TYPE_ARRAY) {
  2144. if (!resolve_array_type(&p, buf_end, module,
  2145. processed_type_count + rec_count,
  2146. processed_type_count + j, error_buf,
  2147. error_buf_size)) {
  2148. return false;
  2149. }
  2150. }
  2151. else {
  2152. set_error_buf(error_buf, error_buf_size,
  2153. "invalid type flag");
  2154. return false;
  2155. }
  2156. cur_type = module->types[processed_type_count + j];
  2157. cur_type->ref_count = 1;
  2158. cur_type->parent_type_idx = parent_type_idx;
  2159. cur_type->is_sub_final = is_sub_final;
  2160. cur_type->rec_count = rec_count;
  2161. cur_type->rec_idx = j;
  2162. cur_type->rec_begin_type_idx = processed_type_count;
  2163. }
  2164. /* resolve subtyping relationship in current rec group */
  2165. for (j = 0; j < rec_count; j++) {
  2166. WASMType *cur_type = module->types[processed_type_count + j];
  2167. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  2168. WASMType *parent_type =
  2169. module->types[cur_type->parent_type_idx];
  2170. cur_type->parent_type = parent_type;
  2171. cur_type->root_type = parent_type->root_type;
  2172. if (parent_type->inherit_depth == UINT16_MAX) {
  2173. set_error_buf(error_buf, error_buf_size,
  2174. "parent type's inherit depth too large");
  2175. return false;
  2176. }
  2177. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  2178. }
  2179. else {
  2180. cur_type->parent_type = NULL;
  2181. cur_type->root_type = cur_type;
  2182. cur_type->inherit_depth = 0;
  2183. }
  2184. }
  2185. for (j = 0; j < rec_count; j++) {
  2186. WASMType *cur_type = module->types[processed_type_count + j];
  2187. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  2188. WASMType *parent_type =
  2189. module->types[cur_type->parent_type_idx];
  2190. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  2191. module->types,
  2192. module->type_count)) {
  2193. set_error_buf_v(error_buf, error_buf_size,
  2194. "sub type %u does not match super type",
  2195. processed_type_count + j);
  2196. return false;
  2197. }
  2198. }
  2199. }
  2200. /* If there is already an equivalence type or a group of equivalence
  2201. recursive types created, use it or them instead */
  2202. for (j = 0; j < processed_type_count;) {
  2203. WASMType *src_type = module->types[j];
  2204. WASMType *cur_type = module->types[processed_type_count];
  2205. uint32 k, src_rec_count;
  2206. src_rec_count = src_type->rec_count;
  2207. if (src_rec_count != rec_count) {
  2208. /* no type equivalence */
  2209. j += src_rec_count;
  2210. continue;
  2211. }
  2212. for (k = 0; k < rec_count; k++) {
  2213. src_type = module->types[j + k];
  2214. cur_type = module->types[processed_type_count + k];
  2215. if (!wasm_type_equal(src_type, cur_type, module->types,
  2216. module->type_count)) {
  2217. break;
  2218. }
  2219. }
  2220. if (k < rec_count) {
  2221. /* no type equivalence */
  2222. j += src_rec_count;
  2223. continue;
  2224. }
  2225. /* type equivalence */
  2226. for (k = 0; k < rec_count; k++) {
  2227. if (module->types[j + k]->ref_count == UINT16_MAX) {
  2228. set_error_buf(error_buf, error_buf_size,
  2229. "wasm type's ref count too large");
  2230. return false;
  2231. }
  2232. destroy_wasm_type(module->types[processed_type_count + k]);
  2233. module->types[processed_type_count + k] =
  2234. module->types[j + k];
  2235. module->types[j + k]->ref_count++;
  2236. }
  2237. break;
  2238. }
  2239. if (rec_count > 1) {
  2240. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  2241. processed_type_count,
  2242. processed_type_count + rec_count - 1);
  2243. }
  2244. processed_type_count += rec_count;
  2245. }
  2246. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  2247. * module->type_count,
  2248. error_buf, error_buf_size))) {
  2249. return false;
  2250. }
  2251. #endif /* end of WASM_ENABLE_GC == 0 */
  2252. }
  2253. if (p != p_end) {
  2254. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2255. return false;
  2256. }
  2257. LOG_VERBOSE("Load type section success.\n");
  2258. return true;
  2259. fail:
  2260. return false;
  2261. }
  2262. static void
  2263. adjust_table_max_size(bool is_table64, uint32 init_size, uint32 max_size_flag,
  2264. uint32 *max_size)
  2265. {
  2266. uint32 default_max_size;
  2267. /* TODO: current still use UINT32_MAX as upper limit for table size to keep
  2268. * ABI unchanged */
  2269. (void)is_table64;
  2270. if (UINT32_MAX / 2 > init_size)
  2271. default_max_size = init_size * 2;
  2272. else
  2273. default_max_size = UINT32_MAX;
  2274. if (default_max_size < WASM_TABLE_MAX_SIZE)
  2275. default_max_size = WASM_TABLE_MAX_SIZE;
  2276. if (max_size_flag) {
  2277. /* module defines the table limitation */
  2278. bh_assert(init_size <= *max_size);
  2279. if (init_size < *max_size) {
  2280. *max_size =
  2281. *max_size < default_max_size ? *max_size : default_max_size;
  2282. }
  2283. }
  2284. else {
  2285. /* partial defined table limitation, gives a default value */
  2286. *max_size = default_max_size;
  2287. }
  2288. }
  2289. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  2290. /**
  2291. * Find export item of a module with export info:
  2292. * module name, field name and export kind
  2293. */
  2294. static WASMExport *
  2295. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  2296. const char *field_name, uint8 export_kind,
  2297. char *error_buf, uint32 error_buf_size)
  2298. {
  2299. WASMExport *export =
  2300. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2301. export_kind, error_buf, error_buf_size);
  2302. return export;
  2303. }
  2304. #endif
  2305. #if WASM_ENABLE_MULTI_MODULE != 0
  2306. static WASMTable *
  2307. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2308. uint32 init_size, uint32 max_size, char *error_buf,
  2309. uint32 error_buf_size)
  2310. {
  2311. WASMModuleCommon *module_reg;
  2312. WASMTable *table = NULL;
  2313. WASMExport *export = NULL;
  2314. WASMModule *module = NULL;
  2315. module_reg = wasm_runtime_find_module_registered(module_name);
  2316. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2317. LOG_DEBUG("can not find a module named %s for table", module_name);
  2318. set_error_buf(error_buf, error_buf_size, "unknown import");
  2319. return NULL;
  2320. }
  2321. module = (WASMModule *)module_reg;
  2322. export =
  2323. wasm_loader_find_export(module, module_name, table_name,
  2324. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2325. if (!export) {
  2326. return NULL;
  2327. }
  2328. /* resolve table and check the init/max size */
  2329. if (export->index < module->import_table_count) {
  2330. table =
  2331. module->import_tables[export->index].u.table.import_table_linked;
  2332. }
  2333. else {
  2334. table = &(module->tables[export->index - module->import_table_count]);
  2335. }
  2336. if (table->table_type.init_size < init_size
  2337. || table->table_type.max_size > max_size) {
  2338. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2339. module_name, table_name, table->table_type.init_size,
  2340. table->table_type.max_size, init_size, max_size);
  2341. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2342. return NULL;
  2343. }
  2344. return table;
  2345. }
  2346. static WASMMemory *
  2347. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2348. uint32 init_page_count, uint32 max_page_count,
  2349. char *error_buf, uint32 error_buf_size)
  2350. {
  2351. WASMModuleCommon *module_reg;
  2352. WASMMemory *memory = NULL;
  2353. WASMExport *export = NULL;
  2354. WASMModule *module = NULL;
  2355. module_reg = wasm_runtime_find_module_registered(module_name);
  2356. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2357. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2358. set_error_buf(error_buf, error_buf_size, "unknown import");
  2359. return NULL;
  2360. }
  2361. module = (WASMModule *)module_reg;
  2362. export =
  2363. wasm_loader_find_export(module, module_name, memory_name,
  2364. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2365. if (!export) {
  2366. return NULL;
  2367. }
  2368. /* resolve memory and check the init/max page count */
  2369. if (export->index < module->import_memory_count) {
  2370. memory = module->import_memories[export->index]
  2371. .u.memory.import_memory_linked;
  2372. }
  2373. else {
  2374. memory =
  2375. &(module->memories[export->index - module->import_memory_count]);
  2376. }
  2377. if (memory->init_page_count < init_page_count
  2378. || memory->max_page_count > max_page_count) {
  2379. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2380. module_name, memory_name, memory->init_page_count,
  2381. memory->max_page_count, init_page_count, max_page_count);
  2382. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2383. return NULL;
  2384. }
  2385. return memory;
  2386. }
  2387. static WASMGlobal *
  2388. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2389. uint8 type, bool is_mutable, char *error_buf,
  2390. uint32 error_buf_size)
  2391. {
  2392. WASMModuleCommon *module_reg;
  2393. WASMGlobal *global = NULL;
  2394. WASMExport *export = NULL;
  2395. WASMModule *module = NULL;
  2396. module_reg = wasm_runtime_find_module_registered(module_name);
  2397. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2398. LOG_DEBUG("can not find a module named %s for global", module_name);
  2399. set_error_buf(error_buf, error_buf_size, "unknown import");
  2400. return NULL;
  2401. }
  2402. module = (WASMModule *)module_reg;
  2403. export =
  2404. wasm_loader_find_export(module, module_name, global_name,
  2405. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2406. if (!export) {
  2407. return NULL;
  2408. }
  2409. /* resolve and check the global */
  2410. if (export->index < module->import_global_count) {
  2411. global =
  2412. module->import_globals[export->index].u.global.import_global_linked;
  2413. }
  2414. else {
  2415. global =
  2416. &(module->globals[export->index - module->import_global_count]);
  2417. }
  2418. if (global->type.val_type != type
  2419. || global->type.is_mutable != is_mutable) {
  2420. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2421. module_name, global_name, global->type.val_type,
  2422. global->type.is_mutable, type, is_mutable);
  2423. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2424. return NULL;
  2425. }
  2426. return global;
  2427. }
  2428. #if WASM_ENABLE_TAGS != 0
  2429. static WASMTag *
  2430. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2431. const WASMType *expected_tag_type,
  2432. uint32 *linked_tag_index, char *error_buf,
  2433. uint32 error_buf_size)
  2434. {
  2435. WASMModuleCommon *module_reg;
  2436. WASMTag *tag = NULL;
  2437. WASMExport *export = NULL;
  2438. WASMModule *module = NULL;
  2439. module_reg = wasm_runtime_find_module_registered(module_name);
  2440. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2441. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2442. tag_name);
  2443. set_error_buf(error_buf, error_buf_size, "unknown import");
  2444. return NULL;
  2445. }
  2446. module = (WASMModule *)module_reg;
  2447. export =
  2448. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2449. error_buf, error_buf_size);
  2450. if (!export) {
  2451. return NULL;
  2452. }
  2453. /* resolve tag type and tag */
  2454. if (export->index < module->import_tag_count) {
  2455. /* importing an imported tag from the submodule */
  2456. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2457. }
  2458. else {
  2459. /* importing an section tag from the submodule */
  2460. tag = module->tags[export->index - module->import_tag_count];
  2461. }
  2462. /* check function type */
  2463. if (!wasm_type_equal(expected_tag_type, tag->tag_type, module->types,
  2464. module->type_count)) {
  2465. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2466. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2467. return NULL;
  2468. }
  2469. if (linked_tag_index != NULL) {
  2470. *linked_tag_index = export->index;
  2471. }
  2472. return tag;
  2473. }
  2474. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2475. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2476. static bool
  2477. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2478. const WASMModule *parent_module,
  2479. const char *sub_module_name, const char *function_name,
  2480. WASMFunctionImport *function, bool no_resolve,
  2481. char *error_buf, uint32 error_buf_size)
  2482. {
  2483. const uint8 *p = *p_buf, *p_end = buf_end;
  2484. uint32 declare_type_index = 0;
  2485. read_leb_uint32(p, p_end, declare_type_index);
  2486. *p_buf = p;
  2487. if (!check_function_type(parent_module, declare_type_index, error_buf,
  2488. error_buf_size)) {
  2489. return false;
  2490. }
  2491. #if WASM_ENABLE_GC != 0
  2492. function->type_idx = declare_type_index;
  2493. #endif
  2494. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2495. declare_type_index = wasm_get_smallest_type_idx(
  2496. parent_module->types, parent_module->type_count, declare_type_index);
  2497. #endif
  2498. function->func_type =
  2499. (WASMFuncType *)parent_module->types[declare_type_index];
  2500. function->module_name = (char *)sub_module_name;
  2501. function->field_name = (char *)function_name;
  2502. function->attachment = NULL;
  2503. function->signature = NULL;
  2504. function->call_conv_raw = false;
  2505. /* lookup registered native symbols first */
  2506. if (!no_resolve) {
  2507. wasm_resolve_import_func(parent_module, function);
  2508. }
  2509. return true;
  2510. fail:
  2511. return false;
  2512. }
  2513. static bool
  2514. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2515. uint32 error_buf_size)
  2516. {
  2517. if (max_size < init_size) {
  2518. set_error_buf(error_buf, error_buf_size,
  2519. "size minimum must not be greater than maximum");
  2520. return false;
  2521. }
  2522. return true;
  2523. }
  2524. static bool
  2525. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2526. WASMModule *parent_module, const char *sub_module_name,
  2527. const char *table_name, WASMTableImport *table,
  2528. char *error_buf, uint32 error_buf_size)
  2529. {
  2530. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2531. uint32 declare_elem_type = 0, table_flag = 0, declare_init_size = 0,
  2532. declare_max_size = 0;
  2533. #if WASM_ENABLE_MULTI_MODULE != 0
  2534. WASMModule *sub_module = NULL;
  2535. WASMTable *linked_table = NULL;
  2536. #endif
  2537. #if WASM_ENABLE_GC != 0
  2538. WASMRefType ref_type;
  2539. bool need_ref_type_map;
  2540. #endif
  2541. bool is_table64 = false;
  2542. #if WASM_ENABLE_GC == 0
  2543. CHECK_BUF(p, p_end, 1);
  2544. /* 0x70 or 0x6F */
  2545. declare_elem_type = read_uint8(p);
  2546. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2547. #if WASM_ENABLE_REF_TYPES != 0
  2548. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2549. #endif
  2550. ) {
  2551. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2552. return false;
  2553. }
  2554. #else /* else of WASM_ENABLE_GC == 0 */
  2555. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2556. &need_ref_type_map, &ref_type, false, error_buf,
  2557. error_buf_size)) {
  2558. return false;
  2559. }
  2560. if (!wasm_is_type_reftype(ref_type.ref_type)
  2561. || wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2562. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2563. return false;
  2564. }
  2565. declare_elem_type = ref_type.ref_type;
  2566. if (need_ref_type_map) {
  2567. if (!(table->table_type.elem_ref_type =
  2568. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2569. error_buf, error_buf_size))) {
  2570. return false;
  2571. }
  2572. }
  2573. #if TRACE_WASM_LOADER != 0
  2574. os_printf("import table type: ");
  2575. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2576. os_printf("\n");
  2577. #endif
  2578. #endif /* end of WASM_ENABLE_GC == 0 */
  2579. p_org = p;
  2580. read_leb_uint32(p, p_end, table_flag);
  2581. is_table64 = table_flag & TABLE64_FLAG;
  2582. if (p - p_org > 1) {
  2583. LOG_VERBOSE("integer representation too long(import table)");
  2584. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2585. return false;
  2586. }
  2587. if (!wasm_table_check_flags(table_flag, error_buf, error_buf_size, false)) {
  2588. return false;
  2589. }
  2590. read_leb_uint32(p, p_end, declare_init_size);
  2591. if (table_flag & MAX_TABLE_SIZE_FLAG) {
  2592. read_leb_uint32(p, p_end, declare_max_size);
  2593. if (!check_table_max_size(declare_init_size, declare_max_size,
  2594. error_buf, error_buf_size))
  2595. return false;
  2596. }
  2597. adjust_table_max_size(is_table64, declare_init_size,
  2598. table_flag & MAX_TABLE_SIZE_FLAG, &declare_max_size);
  2599. *p_buf = p;
  2600. #if WASM_ENABLE_MULTI_MODULE != 0
  2601. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2602. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2603. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2604. error_buf_size);
  2605. if (sub_module) {
  2606. linked_table = wasm_loader_resolve_table(
  2607. sub_module_name, table_name, declare_init_size,
  2608. declare_max_size, error_buf, error_buf_size);
  2609. if (linked_table) {
  2610. /* reset with linked table limit */
  2611. declare_elem_type = linked_table->table_type.elem_type;
  2612. declare_init_size = linked_table->table_type.init_size;
  2613. declare_max_size = linked_table->table_type.max_size;
  2614. table_flag = linked_table->table_type.flags;
  2615. table->import_table_linked = linked_table;
  2616. table->import_module = sub_module;
  2617. }
  2618. }
  2619. }
  2620. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2621. /* (table (export "table") 10 20 funcref) */
  2622. /* (table (export "table64") 10 20 funcref) */
  2623. /* we need this section working in wamrc */
  2624. if (!strcmp("spectest", sub_module_name)) {
  2625. const uint32 spectest_table_init_size = 10;
  2626. const uint32 spectest_table_max_size = 20;
  2627. if (strcmp("table", table_name)
  2628. #if WASM_ENABLE_MEMORY64 != 0
  2629. && strcmp("table64", table_name)
  2630. #endif
  2631. ) {
  2632. set_error_buf(error_buf, error_buf_size,
  2633. "incompatible import type or unknown import");
  2634. return false;
  2635. }
  2636. if (declare_init_size > spectest_table_init_size
  2637. || declare_max_size < spectest_table_max_size) {
  2638. set_error_buf(error_buf, error_buf_size,
  2639. "incompatible import type");
  2640. return false;
  2641. }
  2642. declare_init_size = spectest_table_init_size;
  2643. declare_max_size = spectest_table_max_size;
  2644. }
  2645. /* now we believe all declaration are ok */
  2646. table->table_type.elem_type = declare_elem_type;
  2647. table->table_type.init_size = declare_init_size;
  2648. table->table_type.flags = table_flag;
  2649. table->table_type.max_size = declare_max_size;
  2650. #if WASM_ENABLE_WAMR_COMPILER != 0
  2651. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2652. parent_module->is_ref_types_used = true;
  2653. #endif
  2654. (void)parent_module;
  2655. return true;
  2656. fail:
  2657. return false;
  2658. }
  2659. static bool
  2660. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2661. uint32 error_buf_size)
  2662. {
  2663. uint32 default_max_size =
  2664. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2665. if (!is_memory64 && init_size > default_max_size) {
  2666. set_error_buf(error_buf, error_buf_size,
  2667. "memory size must be at most 65536 pages (4GiB)");
  2668. return false;
  2669. }
  2670. #if WASM_ENABLE_MEMORY64 != 0
  2671. else if (is_memory64 && init_size > default_max_size) {
  2672. set_error_buf(
  2673. error_buf, error_buf_size,
  2674. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2675. return false;
  2676. }
  2677. #endif
  2678. return true;
  2679. }
  2680. static bool
  2681. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2682. char *error_buf, uint32 error_buf_size)
  2683. {
  2684. uint32 default_max_size =
  2685. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2686. if (max_size < init_size) {
  2687. set_error_buf(error_buf, error_buf_size,
  2688. "size minimum must not be greater than maximum");
  2689. return false;
  2690. }
  2691. if (!is_memory64 && max_size > default_max_size) {
  2692. set_error_buf(error_buf, error_buf_size,
  2693. "memory size must be at most 65536 pages (4GiB)");
  2694. return false;
  2695. }
  2696. #if WASM_ENABLE_MEMORY64 != 0
  2697. else if (is_memory64 && max_size > default_max_size) {
  2698. set_error_buf(
  2699. error_buf, error_buf_size,
  2700. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2701. return false;
  2702. }
  2703. #endif
  2704. return true;
  2705. }
  2706. static bool
  2707. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2708. WASMModule *parent_module, const char *sub_module_name,
  2709. const char *memory_name, WASMMemoryImport *memory,
  2710. char *error_buf, uint32 error_buf_size)
  2711. {
  2712. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2713. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2714. uint32 pool_size = wasm_runtime_memory_pool_size();
  2715. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2716. / DEFAULT_NUM_BYTES_PER_PAGE;
  2717. #else
  2718. uint32 max_page_count;
  2719. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2720. uint32 mem_flag = 0;
  2721. bool is_memory64 = false;
  2722. uint32 declare_init_page_count = 0;
  2723. uint32 declare_max_page_count = 0;
  2724. #if WASM_ENABLE_MULTI_MODULE != 0
  2725. WASMModule *sub_module = NULL;
  2726. WASMMemory *linked_memory = NULL;
  2727. #endif
  2728. p_org = p;
  2729. read_leb_uint32(p, p_end, mem_flag);
  2730. is_memory64 = mem_flag & MEMORY64_FLAG;
  2731. if (p - p_org > 1) {
  2732. LOG_VERBOSE("integer representation too long(import memory)");
  2733. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2734. return false;
  2735. }
  2736. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2737. return false;
  2738. }
  2739. read_leb_uint32(p, p_end, declare_init_page_count);
  2740. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2741. error_buf_size)) {
  2742. return false;
  2743. }
  2744. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2745. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2746. #endif
  2747. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2748. read_leb_uint32(p, p_end, declare_max_page_count);
  2749. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2750. declare_max_page_count, error_buf,
  2751. error_buf_size)) {
  2752. return false;
  2753. }
  2754. if (declare_max_page_count > max_page_count) {
  2755. declare_max_page_count = max_page_count;
  2756. }
  2757. }
  2758. else {
  2759. /* Limit the maximum memory size to max_page_count */
  2760. declare_max_page_count = max_page_count;
  2761. }
  2762. #if WASM_ENABLE_MULTI_MODULE != 0
  2763. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2764. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2765. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2766. error_buf_size);
  2767. if (sub_module) {
  2768. linked_memory = wasm_loader_resolve_memory(
  2769. sub_module_name, memory_name, declare_init_page_count,
  2770. declare_max_page_count, error_buf, error_buf_size);
  2771. if (linked_memory) {
  2772. /**
  2773. * reset with linked memory limit
  2774. */
  2775. memory->import_module = sub_module;
  2776. memory->import_memory_linked = linked_memory;
  2777. declare_init_page_count = linked_memory->init_page_count;
  2778. declare_max_page_count = linked_memory->max_page_count;
  2779. }
  2780. }
  2781. }
  2782. #endif
  2783. /* (memory (export "memory") 1 2) */
  2784. if (!strcmp("spectest", sub_module_name)) {
  2785. uint32 spectest_memory_init_page = 1;
  2786. uint32 spectest_memory_max_page = 2;
  2787. if (strcmp("memory", memory_name)) {
  2788. set_error_buf(error_buf, error_buf_size,
  2789. "incompatible import type or unknown import");
  2790. return false;
  2791. }
  2792. if (declare_init_page_count > spectest_memory_init_page
  2793. || declare_max_page_count < spectest_memory_max_page) {
  2794. set_error_buf(error_buf, error_buf_size,
  2795. "incompatible import type");
  2796. return false;
  2797. }
  2798. declare_init_page_count = spectest_memory_init_page;
  2799. declare_max_page_count = spectest_memory_max_page;
  2800. }
  2801. #if WASM_ENABLE_WASI_TEST != 0
  2802. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2803. else if (!strcmp("foo", sub_module_name)) {
  2804. uint32 spectest_memory_init_page = 1;
  2805. uint32 spectest_memory_max_page = 1;
  2806. if (strcmp("bar", memory_name)) {
  2807. set_error_buf(error_buf, error_buf_size,
  2808. "incompatible import type or unknown import");
  2809. return false;
  2810. }
  2811. if (declare_init_page_count > spectest_memory_init_page
  2812. || declare_max_page_count < spectest_memory_max_page) {
  2813. set_error_buf(error_buf, error_buf_size,
  2814. "incompatible import type");
  2815. return false;
  2816. }
  2817. declare_init_page_count = spectest_memory_init_page;
  2818. declare_max_page_count = spectest_memory_max_page;
  2819. }
  2820. #endif
  2821. /* now we believe all declaration are ok */
  2822. memory->mem_type.flags = mem_flag;
  2823. memory->mem_type.init_page_count = declare_init_page_count;
  2824. memory->mem_type.max_page_count = declare_max_page_count;
  2825. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2826. *p_buf = p;
  2827. (void)parent_module;
  2828. return true;
  2829. fail:
  2830. return false;
  2831. }
  2832. #if WASM_ENABLE_TAGS != 0
  2833. static bool
  2834. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2835. const WASMModule *parent_module, /* this module ! */
  2836. const char *sub_module_name, const char *tag_name,
  2837. WASMTagImport *tag, /* structure to fill */
  2838. char *error_buf, uint32 error_buf_size)
  2839. {
  2840. /* attribute and type of the import statement */
  2841. uint8 declare_tag_attribute;
  2842. uint32 declare_type_index;
  2843. const uint8 *p = *p_buf, *p_end = buf_end;
  2844. #if WASM_ENABLE_MULTI_MODULE != 0
  2845. WASMModule *sub_module = NULL;
  2846. #endif
  2847. /* get the one byte attribute */
  2848. CHECK_BUF(p, p_end, 1);
  2849. declare_tag_attribute = read_uint8(p);
  2850. if (declare_tag_attribute != 0) {
  2851. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2852. goto fail;
  2853. }
  2854. /* get type */
  2855. read_leb_uint32(p, p_end, declare_type_index);
  2856. /* compare against module->types */
  2857. if (!check_function_type(parent_module, declare_type_index, error_buf,
  2858. error_buf_size)) {
  2859. goto fail;
  2860. }
  2861. WASMFuncType *declare_tag_type =
  2862. (WASMFuncType *)parent_module->types[declare_type_index];
  2863. /* check, that the type of the declared tag returns void */
  2864. if (declare_tag_type->result_count != 0) {
  2865. set_error_buf(error_buf, error_buf_size,
  2866. "tag type signature does not return void");
  2867. goto fail;
  2868. }
  2869. #if WASM_ENABLE_MULTI_MODULE != 0
  2870. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2871. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2872. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2873. error_buf_size);
  2874. if (sub_module) {
  2875. /* wasm_loader_resolve_tag checks, that the imported tag
  2876. * and the declared tag have the same type
  2877. */
  2878. uint32 linked_tag_index = 0;
  2879. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2880. sub_module_name, tag_name, declare_tag_type,
  2881. &linked_tag_index /* out */, error_buf, error_buf_size);
  2882. if (linked_tag) {
  2883. tag->import_module = sub_module;
  2884. tag->import_tag_linked = linked_tag;
  2885. tag->import_tag_index_linked = linked_tag_index;
  2886. }
  2887. }
  2888. }
  2889. #endif
  2890. /* store to module tag declarations */
  2891. tag->attribute = declare_tag_attribute;
  2892. tag->type = declare_type_index;
  2893. tag->module_name = (char *)sub_module_name;
  2894. tag->field_name = (char *)tag_name;
  2895. tag->tag_type = declare_tag_type;
  2896. *p_buf = p;
  2897. (void)parent_module;
  2898. LOG_VERBOSE("Load tag import success\n");
  2899. return true;
  2900. fail:
  2901. return false;
  2902. }
  2903. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2904. static bool
  2905. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2906. WASMModule *parent_module, char *sub_module_name,
  2907. char *global_name, WASMGlobalImport *global, char *error_buf,
  2908. uint32 error_buf_size)
  2909. {
  2910. const uint8 *p = *p_buf, *p_end = buf_end;
  2911. uint8 declare_type = 0;
  2912. uint8 declare_mutable = 0;
  2913. #if WASM_ENABLE_MULTI_MODULE != 0
  2914. WASMModule *sub_module = NULL;
  2915. WASMGlobal *linked_global = NULL;
  2916. #endif
  2917. #if WASM_ENABLE_GC != 0
  2918. WASMRefType ref_type;
  2919. bool need_ref_type_map;
  2920. #endif
  2921. bool ret = false;
  2922. #if WASM_ENABLE_GC == 0
  2923. CHECK_BUF(p, p_end, 2);
  2924. /* global type */
  2925. declare_type = read_uint8(p);
  2926. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2927. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2928. return false;
  2929. }
  2930. declare_mutable = read_uint8(p);
  2931. #else
  2932. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2933. &need_ref_type_map, &ref_type, false, error_buf,
  2934. error_buf_size)) {
  2935. return false;
  2936. }
  2937. declare_type = ref_type.ref_type;
  2938. if (need_ref_type_map) {
  2939. if (!(global->ref_type =
  2940. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2941. error_buf, error_buf_size))) {
  2942. return false;
  2943. }
  2944. }
  2945. #if TRACE_WASM_LOADER != 0
  2946. os_printf("import global type: ");
  2947. wasm_dump_value_type(declare_type, global->ref_type);
  2948. os_printf("\n");
  2949. #endif
  2950. CHECK_BUF(p, p_end, 1);
  2951. declare_mutable = read_uint8(p);
  2952. #endif /* end of WASM_ENABLE_GC == 0 */
  2953. *p_buf = p;
  2954. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2955. return false;
  2956. }
  2957. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2958. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2959. global);
  2960. if (ret) {
  2961. if (global->type.val_type != declare_type
  2962. || global->type.is_mutable != declare_mutable) {
  2963. set_error_buf(error_buf, error_buf_size,
  2964. "incompatible import type");
  2965. return false;
  2966. }
  2967. global->is_linked = true;
  2968. }
  2969. #endif
  2970. #if WASM_ENABLE_MULTI_MODULE != 0
  2971. if (!global->is_linked
  2972. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2973. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2974. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2975. error_buf_size);
  2976. if (sub_module) {
  2977. /* check sub modules */
  2978. linked_global = wasm_loader_resolve_global(
  2979. sub_module_name, global_name, declare_type, declare_mutable,
  2980. error_buf, error_buf_size);
  2981. if (linked_global) {
  2982. global->import_module = sub_module;
  2983. global->import_global_linked = linked_global;
  2984. global->is_linked = true;
  2985. }
  2986. }
  2987. }
  2988. #endif
  2989. global->module_name = sub_module_name;
  2990. global->field_name = global_name;
  2991. global->type.val_type = declare_type;
  2992. global->type.is_mutable = (declare_mutable == 1);
  2993. #if WASM_ENABLE_WAMR_COMPILER != 0
  2994. if (global->type.val_type == VALUE_TYPE_V128)
  2995. parent_module->is_simd_used = true;
  2996. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2997. parent_module->is_ref_types_used = true;
  2998. #endif
  2999. (void)parent_module;
  3000. (void)ret;
  3001. return true;
  3002. fail:
  3003. return false;
  3004. }
  3005. static bool
  3006. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3007. WASMTable *table, char *error_buf, uint32 error_buf_size)
  3008. {
  3009. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  3010. #if WASM_ENABLE_GC != 0
  3011. WASMRefType ref_type;
  3012. bool need_ref_type_map;
  3013. #endif
  3014. bool is_table64 = false;
  3015. #if WASM_ENABLE_GC == 0
  3016. CHECK_BUF(p, p_end, 1);
  3017. /* 0x70 or 0x6F */
  3018. table->table_type.elem_type = read_uint8(p);
  3019. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  3020. #if WASM_ENABLE_REF_TYPES != 0
  3021. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  3022. #endif
  3023. ) {
  3024. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  3025. return false;
  3026. }
  3027. #else /* else of WASM_ENABLE_GC == 0 */
  3028. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3029. &need_ref_type_map, &ref_type, false, error_buf,
  3030. error_buf_size)) {
  3031. return false;
  3032. }
  3033. /*
  3034. * TODO: add this validator
  3035. * `wasm_is_reftype_htref_non_nullable(ref_type.ref_type)`
  3036. * after sync up with the latest GC spec
  3037. */
  3038. if (!wasm_is_type_reftype(ref_type.ref_type)) {
  3039. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3040. return false;
  3041. }
  3042. table->table_type.elem_type = ref_type.ref_type;
  3043. if (need_ref_type_map) {
  3044. if (!(table->table_type.elem_ref_type =
  3045. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  3046. error_buf_size))) {
  3047. return false;
  3048. }
  3049. }
  3050. #if TRACE_WASM_LOADER != 0
  3051. os_printf("table type: ");
  3052. wasm_dump_value_type(table->table_type.elem_type,
  3053. table->table_type.elem_ref_type);
  3054. os_printf("\n");
  3055. #endif
  3056. #endif /* end of WASM_ENABLE_GC == 0 */
  3057. p_org = p;
  3058. read_leb_uint32(p, p_end, table->table_type.flags);
  3059. is_table64 = table->table_type.flags & TABLE64_FLAG;
  3060. if (p - p_org > 1) {
  3061. LOG_VERBOSE("integer representation too long(table)");
  3062. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  3063. return false;
  3064. }
  3065. if (!wasm_table_check_flags(table->table_type.flags, error_buf,
  3066. error_buf_size, false)) {
  3067. return false;
  3068. }
  3069. read_leb_uint32(p, p_end, table->table_type.init_size);
  3070. if (table->table_type.flags & MAX_TABLE_SIZE_FLAG) {
  3071. read_leb_uint32(p, p_end, table->table_type.max_size);
  3072. if (!check_table_max_size(table->table_type.init_size,
  3073. table->table_type.max_size, error_buf,
  3074. error_buf_size))
  3075. return false;
  3076. }
  3077. adjust_table_max_size(is_table64, table->table_type.init_size,
  3078. table->table_type.flags & MAX_TABLE_SIZE_FLAG,
  3079. &table->table_type.max_size);
  3080. #if WASM_ENABLE_WAMR_COMPILER != 0
  3081. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3082. module->is_ref_types_used = true;
  3083. #endif
  3084. *p_buf = p;
  3085. return true;
  3086. fail:
  3087. return false;
  3088. }
  3089. static bool
  3090. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  3091. char *error_buf, uint32 error_buf_size)
  3092. {
  3093. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  3094. #if WASM_ENABLE_APP_FRAMEWORK != 0
  3095. uint32 pool_size = wasm_runtime_memory_pool_size();
  3096. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  3097. / DEFAULT_NUM_BYTES_PER_PAGE;
  3098. #else
  3099. uint32 max_page_count;
  3100. #endif
  3101. bool is_memory64 = false;
  3102. p_org = p;
  3103. read_leb_uint32(p, p_end, memory->flags);
  3104. is_memory64 = memory->flags & MEMORY64_FLAG;
  3105. if (p - p_org > 1) {
  3106. LOG_VERBOSE("integer representation too long(memory)");
  3107. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  3108. return false;
  3109. }
  3110. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  3111. false)) {
  3112. return false;
  3113. }
  3114. read_leb_uint32(p, p_end, memory->init_page_count);
  3115. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  3116. error_buf_size))
  3117. return false;
  3118. #if WASM_ENABLE_APP_FRAMEWORK == 0
  3119. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  3120. #endif
  3121. if (memory->flags & 1) {
  3122. read_leb_uint32(p, p_end, memory->max_page_count);
  3123. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  3124. memory->max_page_count, error_buf,
  3125. error_buf_size))
  3126. return false;
  3127. if (memory->max_page_count > max_page_count)
  3128. memory->max_page_count = max_page_count;
  3129. }
  3130. else {
  3131. /* Limit the maximum memory size to max_page_count */
  3132. memory->max_page_count = max_page_count;
  3133. }
  3134. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  3135. *p_buf = p;
  3136. return true;
  3137. fail:
  3138. return false;
  3139. }
  3140. static int
  3141. cmp_export_name(const void *a, const void *b)
  3142. {
  3143. return strcmp(*(char **)a, *(char **)b);
  3144. }
  3145. static bool
  3146. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3147. bool is_load_from_file_buf, bool no_resolve,
  3148. char *error_buf, uint32 error_buf_size)
  3149. {
  3150. const uint8 *p = buf, *p_end = buf_end, *p_old;
  3151. uint32 import_count, name_len, type_index, i, u32, flags;
  3152. uint64 total_size;
  3153. WASMImport *import;
  3154. WASMImport *import_functions = NULL, *import_tables = NULL;
  3155. WASMImport *import_memories = NULL, *import_globals = NULL;
  3156. #if WASM_ENABLE_TAGS != 0
  3157. WASMImport *import_tags = NULL;
  3158. #endif
  3159. char *sub_module_name, *field_name;
  3160. uint8 u8, kind, global_type;
  3161. read_leb_uint32(p, p_end, import_count);
  3162. if (import_count) {
  3163. module->import_count = import_count;
  3164. total_size = sizeof(WASMImport) * (uint64)import_count;
  3165. if (!(module->imports =
  3166. loader_malloc(total_size, error_buf, error_buf_size))) {
  3167. return false;
  3168. }
  3169. p_old = p;
  3170. /* Scan firstly to get import count of each type */
  3171. for (i = 0; i < import_count; i++) {
  3172. /* module name */
  3173. read_leb_uint32(p, p_end, name_len);
  3174. CHECK_BUF(p, p_end, name_len);
  3175. p += name_len;
  3176. /* field name */
  3177. read_leb_uint32(p, p_end, name_len);
  3178. CHECK_BUF(p, p_end, name_len);
  3179. p += name_len;
  3180. CHECK_BUF(p, p_end, 1);
  3181. /* 0x00/0x01/0x02/0x03/0x04 */
  3182. kind = read_uint8(p);
  3183. switch (kind) {
  3184. case IMPORT_KIND_FUNC: /* import function */
  3185. read_leb_uint32(p, p_end, type_index);
  3186. module->import_function_count++;
  3187. break;
  3188. case IMPORT_KIND_TABLE: /* import table */
  3189. CHECK_BUF(p, p_end, 1);
  3190. /* 0x70 */
  3191. u8 = read_uint8(p);
  3192. #if WASM_ENABLE_GC != 0
  3193. if (wasm_is_reftype_htref_nullable(u8)) {
  3194. int32 heap_type;
  3195. read_leb_int32(p, p_end, heap_type);
  3196. (void)heap_type;
  3197. }
  3198. #endif
  3199. read_leb_uint32(p, p_end, flags);
  3200. read_leb_uint32(p, p_end, u32);
  3201. if (flags & 1)
  3202. read_leb_uint32(p, p_end, u32);
  3203. module->import_table_count++;
  3204. if (module->import_table_count > 1) {
  3205. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3206. set_error_buf(error_buf, error_buf_size,
  3207. "multiple tables");
  3208. return false;
  3209. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3210. module->is_ref_types_used = true;
  3211. #endif
  3212. }
  3213. break;
  3214. case IMPORT_KIND_MEMORY: /* import memory */
  3215. read_leb_uint32(p, p_end, flags);
  3216. read_leb_uint32(p, p_end, u32);
  3217. if (flags & 1)
  3218. read_leb_uint32(p, p_end, u32);
  3219. module->import_memory_count++;
  3220. #if WASM_ENABLE_MULTI_MEMORY == 0
  3221. if (module->import_memory_count > 1) {
  3222. set_error_buf(error_buf, error_buf_size,
  3223. "multiple memories");
  3224. return false;
  3225. }
  3226. #endif
  3227. break;
  3228. #if WASM_ENABLE_TAGS != 0
  3229. case IMPORT_KIND_TAG: /* import tags */
  3230. /* it only counts the number of tags to import */
  3231. module->import_tag_count++;
  3232. CHECK_BUF(p, p_end, 1);
  3233. u8 = read_uint8(p);
  3234. read_leb_uint32(p, p_end, type_index);
  3235. break;
  3236. #endif
  3237. case IMPORT_KIND_GLOBAL: /* import global */
  3238. #if WASM_ENABLE_GC != 0
  3239. /* valtype */
  3240. CHECK_BUF(p, p_end, 1);
  3241. global_type = read_uint8(p);
  3242. if (wasm_is_reftype_htref_nullable(global_type)
  3243. || wasm_is_reftype_htref_non_nullable(global_type)) {
  3244. int32 heap_type;
  3245. read_leb_int32(p, p_end, heap_type);
  3246. (void)heap_type;
  3247. }
  3248. /* mutability */
  3249. CHECK_BUF(p, p_end, 1);
  3250. p += 1;
  3251. #else
  3252. CHECK_BUF(p, p_end, 2);
  3253. p += 2;
  3254. #endif
  3255. (void)global_type;
  3256. module->import_global_count++;
  3257. break;
  3258. default:
  3259. set_error_buf(error_buf, error_buf_size,
  3260. "invalid import kind");
  3261. return false;
  3262. }
  3263. }
  3264. if (module->import_function_count)
  3265. import_functions = module->import_functions = module->imports;
  3266. if (module->import_table_count)
  3267. import_tables = module->import_tables =
  3268. module->imports + module->import_function_count;
  3269. if (module->import_memory_count)
  3270. import_memories = module->import_memories =
  3271. module->imports + module->import_function_count
  3272. + module->import_table_count;
  3273. #if WASM_ENABLE_TAGS != 0
  3274. if (module->import_tag_count)
  3275. import_tags = module->import_tags =
  3276. module->imports + module->import_function_count
  3277. + module->import_table_count + module->import_memory_count;
  3278. if (module->import_global_count)
  3279. import_globals = module->import_globals =
  3280. module->imports + module->import_function_count
  3281. + module->import_table_count + module->import_memory_count
  3282. + module->import_tag_count;
  3283. #else
  3284. if (module->import_global_count)
  3285. import_globals = module->import_globals =
  3286. module->imports + module->import_function_count
  3287. + module->import_table_count + module->import_memory_count;
  3288. #endif
  3289. p = p_old;
  3290. /* Scan again to resolve the data */
  3291. for (i = 0; i < import_count; i++) {
  3292. /* load module name */
  3293. read_leb_uint32(p, p_end, name_len);
  3294. CHECK_BUF(p, p_end, name_len);
  3295. if (!(sub_module_name = wasm_const_str_list_insert(
  3296. p, name_len, module, is_load_from_file_buf, error_buf,
  3297. error_buf_size))) {
  3298. return false;
  3299. }
  3300. p += name_len;
  3301. /* load field name */
  3302. read_leb_uint32(p, p_end, name_len);
  3303. CHECK_BUF(p, p_end, name_len);
  3304. if (!(field_name = wasm_const_str_list_insert(
  3305. p, name_len, module, is_load_from_file_buf, error_buf,
  3306. error_buf_size))) {
  3307. return false;
  3308. }
  3309. p += name_len;
  3310. CHECK_BUF(p, p_end, 1);
  3311. /* 0x00/0x01/0x02/0x03/0x4 */
  3312. kind = read_uint8(p);
  3313. switch (kind) {
  3314. case IMPORT_KIND_FUNC: /* import function */
  3315. bh_assert(import_functions);
  3316. import = import_functions++;
  3317. if (!load_function_import(&p, p_end, module,
  3318. sub_module_name, field_name,
  3319. &import->u.function, no_resolve,
  3320. error_buf, error_buf_size)) {
  3321. return false;
  3322. }
  3323. break;
  3324. case IMPORT_KIND_TABLE: /* import table */
  3325. bh_assert(import_tables);
  3326. import = import_tables++;
  3327. if (!load_table_import(&p, p_end, module, sub_module_name,
  3328. field_name, &import->u.table,
  3329. error_buf, error_buf_size)) {
  3330. LOG_DEBUG("can not import such a table (%s,%s)",
  3331. sub_module_name, field_name);
  3332. return false;
  3333. }
  3334. break;
  3335. case IMPORT_KIND_MEMORY: /* import memory */
  3336. bh_assert(import_memories);
  3337. import = import_memories++;
  3338. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3339. field_name, &import->u.memory,
  3340. error_buf, error_buf_size)) {
  3341. return false;
  3342. }
  3343. break;
  3344. #if WASM_ENABLE_TAGS != 0
  3345. case IMPORT_KIND_TAG:
  3346. bh_assert(import_tags);
  3347. import = import_tags++;
  3348. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3349. field_name, &import->u.tag, error_buf,
  3350. error_buf_size)) {
  3351. return false;
  3352. }
  3353. break;
  3354. #endif
  3355. case IMPORT_KIND_GLOBAL: /* import global */
  3356. bh_assert(import_globals);
  3357. import = import_globals++;
  3358. if (!load_global_import(&p, p_end, module, sub_module_name,
  3359. field_name, &import->u.global,
  3360. error_buf, error_buf_size)) {
  3361. return false;
  3362. }
  3363. break;
  3364. default:
  3365. set_error_buf(error_buf, error_buf_size,
  3366. "invalid import kind");
  3367. return false;
  3368. }
  3369. import->kind = kind;
  3370. import->u.names.module_name = sub_module_name;
  3371. import->u.names.field_name = field_name;
  3372. }
  3373. #if WASM_ENABLE_LIBC_WASI != 0
  3374. import = module->import_functions;
  3375. for (i = 0; i < module->import_function_count; i++, import++) {
  3376. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3377. || !strcmp(import->u.names.module_name,
  3378. "wasi_snapshot_preview1")) {
  3379. module->import_wasi_api = true;
  3380. break;
  3381. }
  3382. }
  3383. #endif
  3384. }
  3385. if (p != p_end) {
  3386. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3387. return false;
  3388. }
  3389. LOG_VERBOSE("Load import section success.\n");
  3390. (void)u8;
  3391. (void)u32;
  3392. (void)type_index;
  3393. return true;
  3394. fail:
  3395. return false;
  3396. }
  3397. static bool
  3398. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3399. uint32 error_buf_size)
  3400. {
  3401. WASMFuncType *param_type = func->func_type;
  3402. uint32 param_count = param_type->param_count;
  3403. uint8 *param_types = param_type->types;
  3404. uint32 local_count = func->local_count;
  3405. uint8 *local_types = func->local_types;
  3406. uint32 i, local_offset = 0;
  3407. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3408. /*
  3409. * Only allocate memory when total_size is not 0,
  3410. * or the return value of malloc(0) might be NULL on some platforms,
  3411. * which causes wasm loader return false.
  3412. */
  3413. if (total_size > 0
  3414. && !(func->local_offsets =
  3415. loader_malloc(total_size, error_buf, error_buf_size))) {
  3416. return false;
  3417. }
  3418. for (i = 0; i < param_count; i++) {
  3419. func->local_offsets[i] = (uint16)local_offset;
  3420. local_offset += wasm_value_type_cell_num(param_types[i]);
  3421. }
  3422. for (i = 0; i < local_count; i++) {
  3423. func->local_offsets[param_count + i] = (uint16)local_offset;
  3424. local_offset += wasm_value_type_cell_num(local_types[i]);
  3425. }
  3426. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3427. return true;
  3428. }
  3429. static bool
  3430. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3431. const uint8 *buf_code, const uint8 *buf_code_end,
  3432. WASMModule *module, char *error_buf,
  3433. uint32 error_buf_size)
  3434. {
  3435. const uint8 *p = buf, *p_end = buf_end;
  3436. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3437. uint32 func_count;
  3438. uint64 total_size;
  3439. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3440. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3441. uint8 type;
  3442. WASMFunction *func;
  3443. #if WASM_ENABLE_GC != 0
  3444. bool need_ref_type_map;
  3445. WASMRefType ref_type;
  3446. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3447. #endif
  3448. read_leb_uint32(p, p_end, func_count);
  3449. if (buf_code)
  3450. read_leb_uint32(p_code, buf_code_end, code_count);
  3451. if (func_count != code_count) {
  3452. set_error_buf(error_buf, error_buf_size,
  3453. "function and code section have inconsistent lengths or "
  3454. "unexpected end");
  3455. return false;
  3456. }
  3457. if (is_indices_overflow(module->import_function_count, func_count,
  3458. error_buf, error_buf_size))
  3459. return false;
  3460. if (func_count) {
  3461. module->function_count = func_count;
  3462. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3463. if (!(module->functions =
  3464. loader_malloc(total_size, error_buf, error_buf_size))) {
  3465. return false;
  3466. }
  3467. for (i = 0; i < func_count; i++) {
  3468. /* Resolve function type */
  3469. read_leb_uint32(p, p_end, type_index);
  3470. if (!check_function_type(module, type_index, error_buf,
  3471. error_buf_size)) {
  3472. return false;
  3473. }
  3474. #if WASM_ENABLE_GC != 0
  3475. type_index_org = type_index;
  3476. #endif
  3477. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3478. && WASM_ENABLE_GC == 0
  3479. type_index = wasm_get_smallest_type_idx(
  3480. module->types, module->type_count, type_index);
  3481. #endif
  3482. read_leb_uint32(p_code, buf_code_end, code_size);
  3483. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3484. set_error_buf(error_buf, error_buf_size,
  3485. "invalid function code size");
  3486. return false;
  3487. }
  3488. /* Resolve local set count */
  3489. p_code_end = p_code + code_size;
  3490. local_count = 0;
  3491. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3492. p_code_save = p_code;
  3493. #if WASM_ENABLE_GC != 0
  3494. ref_type_map_count = 0;
  3495. #endif
  3496. /* Calculate total local count */
  3497. for (j = 0; j < local_set_count; j++) {
  3498. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3499. if (sub_local_count > UINT32_MAX - local_count) {
  3500. set_error_buf(error_buf, error_buf_size, "too many locals");
  3501. return false;
  3502. }
  3503. #if WASM_ENABLE_GC == 0
  3504. CHECK_BUF(p_code, buf_code_end, 1);
  3505. /* 0x7F/0x7E/0x7D/0x7C */
  3506. type = read_uint8(p_code);
  3507. local_count += sub_local_count;
  3508. #if WASM_ENABLE_WAMR_COMPILER != 0
  3509. /* If any value's type is v128, mark the module as SIMD used */
  3510. if (type == VALUE_TYPE_V128)
  3511. module->is_simd_used = true;
  3512. #endif
  3513. #else
  3514. if (!resolve_value_type(&p_code, buf_code_end, module,
  3515. module->type_count, &need_ref_type_map,
  3516. &ref_type, false, error_buf,
  3517. error_buf_size)) {
  3518. return false;
  3519. }
  3520. local_count += sub_local_count;
  3521. if (need_ref_type_map)
  3522. ref_type_map_count += sub_local_count;
  3523. #endif
  3524. }
  3525. /* Code size in code entry can't be smaller than size of vec(locals)
  3526. * + expr(at least 1 for opcode end). And expressions are encoded by
  3527. * their instruction sequence terminated with an explicit 0x0B
  3528. * opcode for end. */
  3529. if (p_code_end <= p_code || *(p_code_end - 1) != WASM_OP_END) {
  3530. set_error_buf(
  3531. error_buf, error_buf_size,
  3532. "section size mismatch: function body END opcode expected");
  3533. return false;
  3534. }
  3535. /* Alloc memory, layout: function structure + local types */
  3536. code_size = (uint32)(p_code_end - p_code);
  3537. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3538. if (!(func = module->functions[i] =
  3539. loader_malloc(total_size, error_buf, error_buf_size))) {
  3540. return false;
  3541. }
  3542. #if WASM_ENABLE_GC != 0
  3543. if (ref_type_map_count > 0) {
  3544. total_size =
  3545. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3546. if (!(func->local_ref_type_maps = loader_malloc(
  3547. total_size, error_buf, error_buf_size))) {
  3548. return false;
  3549. }
  3550. func->local_ref_type_map_count = ref_type_map_count;
  3551. }
  3552. #endif
  3553. /* Set function type, local count, code size and code body */
  3554. func->func_type = (WASMFuncType *)module->types[type_index];
  3555. func->local_count = local_count;
  3556. if (local_count > 0)
  3557. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3558. func->code_size = code_size;
  3559. /*
  3560. * we shall make a copy of code body [p_code, p_code + code_size]
  3561. * when we are worrying about inappropriate releasing behaviour.
  3562. * all code bodies are actually in a buffer which user allocates in
  3563. * their embedding environment and we don't have power over them.
  3564. * it will be like:
  3565. * code_body_cp = malloc(code_size);
  3566. * memcpy(code_body_cp, p_code, code_size);
  3567. * func->code = code_body_cp;
  3568. */
  3569. func->code = (uint8 *)p_code;
  3570. #if WASM_ENABLE_GC != 0
  3571. func->type_idx = type_index_org;
  3572. #endif
  3573. #if WASM_ENABLE_GC != 0
  3574. t = 0;
  3575. #endif
  3576. /* Load each local type */
  3577. p_code = p_code_save;
  3578. local_type_index = 0;
  3579. for (j = 0; j < local_set_count; j++) {
  3580. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3581. /* Note: sub_local_count is allowed to be 0 */
  3582. if (local_type_index > UINT32_MAX - sub_local_count
  3583. || local_type_index + sub_local_count > local_count) {
  3584. set_error_buf(error_buf, error_buf_size,
  3585. "invalid local count");
  3586. return false;
  3587. }
  3588. #if WASM_ENABLE_GC == 0
  3589. CHECK_BUF(p_code, buf_code_end, 1);
  3590. /* 0x7F/0x7E/0x7D/0x7C */
  3591. type = read_uint8(p_code);
  3592. if (!is_valid_value_type_for_interpreter(type)) {
  3593. if (type == VALUE_TYPE_V128)
  3594. set_error_buf(error_buf, error_buf_size,
  3595. "v128 value type requires simd feature");
  3596. else if (type == VALUE_TYPE_FUNCREF
  3597. || type == VALUE_TYPE_EXTERNREF)
  3598. set_error_buf(error_buf, error_buf_size,
  3599. "ref value type requires "
  3600. "reference types feature");
  3601. else
  3602. set_error_buf_v(error_buf, error_buf_size,
  3603. "invalid local type 0x%02X", type);
  3604. return false;
  3605. }
  3606. #else
  3607. if (!resolve_value_type(&p_code, buf_code_end, module,
  3608. module->type_count, &need_ref_type_map,
  3609. &ref_type, false, error_buf,
  3610. error_buf_size)) {
  3611. return false;
  3612. }
  3613. if (need_ref_type_map) {
  3614. WASMRefType *ref_type_tmp;
  3615. if (!(ref_type_tmp = reftype_set_insert(
  3616. module->ref_type_set, &ref_type, error_buf,
  3617. error_buf_size))) {
  3618. return false;
  3619. }
  3620. for (k = 0; k < sub_local_count; k++) {
  3621. func->local_ref_type_maps[t + k].ref_type =
  3622. ref_type_tmp;
  3623. func->local_ref_type_maps[t + k].index =
  3624. local_type_index + k;
  3625. }
  3626. t += sub_local_count;
  3627. }
  3628. type = ref_type.ref_type;
  3629. #endif
  3630. for (k = 0; k < sub_local_count; k++) {
  3631. func->local_types[local_type_index++] = type;
  3632. }
  3633. #if WASM_ENABLE_WAMR_COMPILER != 0
  3634. if (type == VALUE_TYPE_V128)
  3635. module->is_simd_used = true;
  3636. else if (type == VALUE_TYPE_FUNCREF
  3637. || type == VALUE_TYPE_EXTERNREF)
  3638. module->is_ref_types_used = true;
  3639. #endif
  3640. }
  3641. bh_assert(local_type_index == func->local_count);
  3642. #if WASM_ENABLE_GC != 0
  3643. bh_assert(t == func->local_ref_type_map_count);
  3644. #if TRACE_WASM_LOADER != 0
  3645. os_printf("func %u, local types: [", i);
  3646. k = 0;
  3647. for (j = 0; j < func->local_count; j++) {
  3648. WASMRefType *ref_type_tmp = NULL;
  3649. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3650. bh_assert(j == func->local_ref_type_maps[k].index);
  3651. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3652. }
  3653. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3654. if (j < func->local_count - 1)
  3655. os_printf(" ");
  3656. }
  3657. os_printf("]\n");
  3658. #endif
  3659. #endif
  3660. func->param_cell_num = func->func_type->param_cell_num;
  3661. func->ret_cell_num = func->func_type->ret_cell_num;
  3662. local_cell_num =
  3663. wasm_get_cell_num(func->local_types, func->local_count);
  3664. if (local_cell_num > UINT16_MAX) {
  3665. set_error_buf(error_buf, error_buf_size,
  3666. "local count too large");
  3667. return false;
  3668. }
  3669. func->local_cell_num = (uint16)local_cell_num;
  3670. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3671. return false;
  3672. p_code = p_code_end;
  3673. }
  3674. }
  3675. if (p != p_end) {
  3676. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3677. return false;
  3678. }
  3679. LOG_VERBOSE("Load function section success.\n");
  3680. return true;
  3681. fail:
  3682. return false;
  3683. }
  3684. static bool
  3685. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3686. char *error_buf, uint32 error_buf_size)
  3687. {
  3688. const uint8 *p = buf, *p_end = buf_end;
  3689. uint32 table_count, i;
  3690. uint64 total_size;
  3691. WASMTable *table;
  3692. read_leb_uint32(p, p_end, table_count);
  3693. if (module->import_table_count + table_count > 1) {
  3694. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3695. /* a total of one table is allowed */
  3696. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3697. return false;
  3698. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3699. module->is_ref_types_used = true;
  3700. #endif
  3701. }
  3702. if (table_count) {
  3703. module->table_count = table_count;
  3704. total_size = sizeof(WASMTable) * (uint64)table_count;
  3705. if (!(module->tables =
  3706. loader_malloc(total_size, error_buf, error_buf_size))) {
  3707. return false;
  3708. }
  3709. /* load each table */
  3710. table = module->tables;
  3711. for (i = 0; i < table_count; i++, table++) {
  3712. #if WASM_ENABLE_GC != 0
  3713. uint8 flag;
  3714. bool has_init = false;
  3715. CHECK_BUF(p, p_end, 1);
  3716. flag = read_uint8(p);
  3717. if (flag == TABLE_INIT_EXPR_FLAG) {
  3718. CHECK_BUF(p, p_end, 1);
  3719. flag = read_uint8(p);
  3720. if (flag != 0x00) {
  3721. set_error_buf(error_buf, error_buf_size,
  3722. "invalid leading bytes for table");
  3723. return false;
  3724. }
  3725. has_init = true;
  3726. }
  3727. else {
  3728. p--;
  3729. }
  3730. #endif /* end of WASM_ENABLE_GC != 0 */
  3731. if (!load_table(&p, p_end, module, table, error_buf,
  3732. error_buf_size))
  3733. return false;
  3734. #if WASM_ENABLE_GC != 0
  3735. if (has_init) {
  3736. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3737. table->table_type.elem_type,
  3738. table->table_type.elem_ref_type, error_buf,
  3739. error_buf_size))
  3740. return false;
  3741. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3742. && table->init_expr.init_expr_type
  3743. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3744. set_error_buf(
  3745. error_buf, error_buf_size,
  3746. "unsupported initializer expression for table");
  3747. return false;
  3748. }
  3749. }
  3750. else {
  3751. if (wasm_is_reftype_htref_non_nullable(
  3752. table->table_type.elem_type)) {
  3753. set_error_buf(
  3754. error_buf, error_buf_size,
  3755. "type mismatch: non-nullable table without init expr");
  3756. return false;
  3757. }
  3758. }
  3759. #endif /* end of WASM_ENABLE_GC != 0 */
  3760. #if WASM_ENABLE_WAMR_COMPILER != 0
  3761. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3762. module->is_ref_types_used = true;
  3763. #endif
  3764. }
  3765. }
  3766. if (p != p_end) {
  3767. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3768. return false;
  3769. }
  3770. LOG_VERBOSE("Load table section success.\n");
  3771. return true;
  3772. fail:
  3773. return false;
  3774. }
  3775. static bool
  3776. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3777. char *error_buf, uint32 error_buf_size)
  3778. {
  3779. const uint8 *p = buf, *p_end = buf_end;
  3780. uint32 memory_count, i;
  3781. uint64 total_size;
  3782. WASMMemory *memory;
  3783. read_leb_uint32(p, p_end, memory_count);
  3784. #if WASM_ENABLE_MULTI_MEMORY == 0
  3785. /* a total of one memory is allowed */
  3786. if (module->import_memory_count + memory_count > 1) {
  3787. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3788. return false;
  3789. }
  3790. #endif
  3791. if (memory_count) {
  3792. module->memory_count = memory_count;
  3793. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3794. if (!(module->memories =
  3795. loader_malloc(total_size, error_buf, error_buf_size))) {
  3796. return false;
  3797. }
  3798. /* load each memory */
  3799. memory = module->memories;
  3800. for (i = 0; i < memory_count; i++, memory++)
  3801. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3802. return false;
  3803. }
  3804. if (p != p_end) {
  3805. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3806. return false;
  3807. }
  3808. LOG_VERBOSE("Load memory section success.\n");
  3809. return true;
  3810. fail:
  3811. return false;
  3812. }
  3813. static bool
  3814. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3815. char *error_buf, uint32 error_buf_size)
  3816. {
  3817. const uint8 *p = buf, *p_end = buf_end;
  3818. uint32 global_count, i;
  3819. uint64 total_size;
  3820. WASMGlobal *global;
  3821. uint8 mutable;
  3822. #if WASM_ENABLE_GC != 0
  3823. bool need_ref_type_map;
  3824. WASMRefType ref_type;
  3825. #endif
  3826. read_leb_uint32(p, p_end, global_count);
  3827. if (is_indices_overflow(module->import_global_count, global_count,
  3828. error_buf, error_buf_size))
  3829. return false;
  3830. module->global_count = 0;
  3831. if (global_count) {
  3832. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3833. if (!(module->globals =
  3834. loader_malloc(total_size, error_buf, error_buf_size))) {
  3835. return false;
  3836. }
  3837. global = module->globals;
  3838. for (i = 0; i < global_count; i++, global++) {
  3839. #if WASM_ENABLE_GC == 0
  3840. CHECK_BUF(p, p_end, 2);
  3841. /* global type */
  3842. global->type.val_type = read_uint8(p);
  3843. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3844. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3845. return false;
  3846. }
  3847. mutable = read_uint8(p);
  3848. #else
  3849. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3850. &need_ref_type_map, &ref_type, false,
  3851. error_buf, error_buf_size)) {
  3852. return false;
  3853. }
  3854. global->type.val_type = ref_type.ref_type;
  3855. CHECK_BUF(p, p_end, 1);
  3856. mutable = read_uint8(p);
  3857. #endif /* end of WASM_ENABLE_GC */
  3858. #if WASM_ENABLE_WAMR_COMPILER != 0
  3859. if (global->type.val_type == VALUE_TYPE_V128)
  3860. module->is_simd_used = true;
  3861. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3862. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3863. module->is_ref_types_used = true;
  3864. #endif
  3865. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3866. return false;
  3867. }
  3868. global->type.is_mutable = mutable ? true : false;
  3869. /* initialize expression */
  3870. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3871. global->type.val_type,
  3872. #if WASM_ENABLE_GC == 0
  3873. NULL,
  3874. #else
  3875. &ref_type,
  3876. #endif
  3877. error_buf, error_buf_size))
  3878. return false;
  3879. #if WASM_ENABLE_GC != 0
  3880. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3881. uint8 global_type;
  3882. WASMRefType *global_ref_type;
  3883. uint32 global_idx = global->init_expr.u.unary.v.global_index;
  3884. if (global->init_expr.u.unary.v.global_index
  3885. >= module->import_global_count + i) {
  3886. set_error_buf(error_buf, error_buf_size, "unknown global");
  3887. return false;
  3888. }
  3889. if (global_idx < module->import_global_count) {
  3890. global_type = module->import_globals[global_idx]
  3891. .u.global.type.val_type;
  3892. global_ref_type =
  3893. module->import_globals[global_idx].u.global.ref_type;
  3894. }
  3895. else {
  3896. global_type =
  3897. module
  3898. ->globals[global_idx - module->import_global_count]
  3899. .type.val_type;
  3900. global_ref_type =
  3901. module
  3902. ->globals[global_idx - module->import_global_count]
  3903. .ref_type;
  3904. }
  3905. if (!wasm_reftype_is_subtype_of(
  3906. global_type, global_ref_type, global->type.val_type,
  3907. global->ref_type, module->types, module->type_count)) {
  3908. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3909. return false;
  3910. }
  3911. }
  3912. if (need_ref_type_map) {
  3913. if (!(global->ref_type =
  3914. reftype_set_insert(module->ref_type_set, &ref_type,
  3915. error_buf, error_buf_size))) {
  3916. return false;
  3917. }
  3918. }
  3919. #if TRACE_WASM_LOADER != 0
  3920. os_printf("global type: ");
  3921. wasm_dump_value_type(global->type, global->ref_type);
  3922. os_printf("\n");
  3923. #endif
  3924. #endif
  3925. module->global_count++;
  3926. }
  3927. bh_assert(module->global_count == global_count);
  3928. }
  3929. if (p != p_end) {
  3930. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3931. return false;
  3932. }
  3933. LOG_VERBOSE("Load global section success.\n");
  3934. return true;
  3935. fail:
  3936. return false;
  3937. }
  3938. static bool
  3939. check_duplicate_exports(WASMModule *module, char *error_buf,
  3940. uint32 error_buf_size)
  3941. {
  3942. uint32 i;
  3943. bool result = false;
  3944. char *names_buf[32], **names = names_buf;
  3945. if (module->export_count > 32) {
  3946. names = loader_malloc(module->export_count * sizeof(char *), error_buf,
  3947. error_buf_size);
  3948. if (!names) {
  3949. return result;
  3950. }
  3951. }
  3952. for (i = 0; i < module->export_count; i++) {
  3953. names[i] = module->exports[i].name;
  3954. }
  3955. qsort(names, module->export_count, sizeof(char *), cmp_export_name);
  3956. for (i = 1; i < module->export_count; i++) {
  3957. if (!strcmp(names[i], names[i - 1])) {
  3958. set_error_buf(error_buf, error_buf_size, "duplicate export name");
  3959. goto cleanup;
  3960. }
  3961. }
  3962. result = true;
  3963. cleanup:
  3964. if (module->export_count > 32) {
  3965. wasm_runtime_free(names);
  3966. }
  3967. return result;
  3968. }
  3969. static bool
  3970. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3971. bool is_load_from_file_buf, char *error_buf,
  3972. uint32 error_buf_size)
  3973. {
  3974. const uint8 *p = buf, *p_end = buf_end;
  3975. uint32 export_count, i, index;
  3976. uint64 total_size;
  3977. uint32 str_len;
  3978. WASMExport *export;
  3979. read_leb_uint32(p, p_end, export_count);
  3980. if (export_count) {
  3981. module->export_count = export_count;
  3982. total_size = sizeof(WASMExport) * (uint64)export_count;
  3983. if (!(module->exports =
  3984. loader_malloc(total_size, error_buf, error_buf_size))) {
  3985. return false;
  3986. }
  3987. export = module->exports;
  3988. for (i = 0; i < export_count; i++, export ++) {
  3989. #if WASM_ENABLE_THREAD_MGR == 0
  3990. if (p == p_end) {
  3991. /* export section with inconsistent count:
  3992. n export declared, but less than n given */
  3993. set_error_buf(error_buf, error_buf_size,
  3994. "length out of bounds");
  3995. return false;
  3996. }
  3997. #endif
  3998. read_leb_uint32(p, p_end, str_len);
  3999. CHECK_BUF(p, p_end, str_len);
  4000. if (!(export->name = wasm_const_str_list_insert(
  4001. p, str_len, module, is_load_from_file_buf, error_buf,
  4002. error_buf_size))) {
  4003. return false;
  4004. }
  4005. p += str_len;
  4006. CHECK_BUF(p, p_end, 1);
  4007. export->kind = read_uint8(p);
  4008. read_leb_uint32(p, p_end, index);
  4009. export->index = index;
  4010. switch (export->kind) {
  4011. /* function index */
  4012. case EXPORT_KIND_FUNC:
  4013. if (index >= module->function_count
  4014. + module->import_function_count) {
  4015. set_error_buf(error_buf, error_buf_size,
  4016. "unknown function");
  4017. return false;
  4018. }
  4019. #if WASM_ENABLE_SIMD != 0
  4020. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  4021. || (WASM_ENABLE_FAST_INTERP != 0)
  4022. /* TODO: check func type, if it has v128 param or result,
  4023. report error */
  4024. #endif
  4025. #endif
  4026. break;
  4027. /* table index */
  4028. case EXPORT_KIND_TABLE:
  4029. if (index
  4030. >= module->table_count + module->import_table_count) {
  4031. set_error_buf(error_buf, error_buf_size,
  4032. "unknown table");
  4033. return false;
  4034. }
  4035. break;
  4036. /* memory index */
  4037. case EXPORT_KIND_MEMORY:
  4038. if (index
  4039. >= module->memory_count + module->import_memory_count) {
  4040. set_error_buf(error_buf, error_buf_size,
  4041. "unknown memory");
  4042. return false;
  4043. }
  4044. break;
  4045. #if WASM_ENABLE_TAGS != 0
  4046. /* export tag */
  4047. case EXPORT_KIND_TAG:
  4048. if (index >= module->tag_count + module->import_tag_count) {
  4049. set_error_buf(error_buf, error_buf_size, "unknown tag");
  4050. return false;
  4051. }
  4052. break;
  4053. #endif
  4054. /* global index */
  4055. case EXPORT_KIND_GLOBAL:
  4056. if (index
  4057. >= module->global_count + module->import_global_count) {
  4058. set_error_buf(error_buf, error_buf_size,
  4059. "unknown global");
  4060. return false;
  4061. }
  4062. break;
  4063. default:
  4064. set_error_buf(error_buf, error_buf_size,
  4065. "invalid export kind");
  4066. return false;
  4067. }
  4068. }
  4069. if (!check_duplicate_exports(module, error_buf, error_buf_size)) {
  4070. return false;
  4071. }
  4072. }
  4073. if (p != p_end) {
  4074. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4075. return false;
  4076. }
  4077. LOG_VERBOSE("Load export section success.\n");
  4078. return true;
  4079. fail:
  4080. return false;
  4081. }
  4082. static bool
  4083. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  4084. uint32 error_buf_size)
  4085. {
  4086. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  4087. if (table_index != 0) {
  4088. set_error_buf(
  4089. error_buf, error_buf_size,
  4090. "zero byte expected. The module uses reference types feature "
  4091. "which is disabled in the runtime.");
  4092. return false;
  4093. }
  4094. #endif
  4095. if (table_index >= module->import_table_count + module->table_count) {
  4096. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  4097. table_index);
  4098. return false;
  4099. }
  4100. return true;
  4101. }
  4102. static bool
  4103. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  4104. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  4105. {
  4106. const uint8 *p = *p_buf, *p_end = buf_end;
  4107. uint32 table_index;
  4108. read_leb_uint32(p, p_end, table_index);
  4109. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  4110. return false;
  4111. }
  4112. *p_table_index = table_index;
  4113. *p_buf = p;
  4114. return true;
  4115. fail:
  4116. return false;
  4117. }
  4118. /* Element segments must match element type of table */
  4119. static bool
  4120. check_table_elem_type(WASMModule *module, uint32 table_index,
  4121. uint32 type_from_elem_seg, char *error_buf,
  4122. uint32 error_buf_size)
  4123. {
  4124. uint32 table_declared_elem_type;
  4125. if (table_index < module->import_table_count)
  4126. table_declared_elem_type =
  4127. module->import_tables[table_index].u.table.table_type.elem_type;
  4128. else
  4129. table_declared_elem_type =
  4130. module->tables[table_index - module->import_table_count]
  4131. .table_type.elem_type;
  4132. if (table_declared_elem_type == type_from_elem_seg)
  4133. return true;
  4134. #if WASM_ENABLE_GC != 0
  4135. /*
  4136. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  4137. */
  4138. if (table_declared_elem_type == REF_TYPE_ANYREF)
  4139. return true;
  4140. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  4141. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  4142. return true;
  4143. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  4144. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  4145. return true;
  4146. #endif
  4147. set_error_buf(error_buf, error_buf_size, "type mismatch");
  4148. return false;
  4149. }
  4150. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4151. static bool
  4152. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  4153. uint32 *p_elem_type,
  4154. #if WASM_ENABLE_GC != 0
  4155. WASMRefType **p_elem_ref_type,
  4156. #endif
  4157. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  4158. {
  4159. const uint8 *p = *p_buf, *p_end = buf_end;
  4160. uint8 elem_type;
  4161. #if WASM_ENABLE_GC != 0
  4162. WASMRefType elem_ref_type;
  4163. bool need_ref_type_map;
  4164. #endif
  4165. CHECK_BUF(p, p_end, 1);
  4166. elem_type = read_uint8(p);
  4167. if (elemkind_zero) {
  4168. if (elem_type != 0) {
  4169. set_error_buf(error_buf, error_buf_size,
  4170. "invalid reference type or unknown type");
  4171. return false;
  4172. }
  4173. else {
  4174. *p_elem_type = VALUE_TYPE_FUNCREF;
  4175. *p_buf = p;
  4176. return true;
  4177. }
  4178. }
  4179. #if WASM_ENABLE_GC == 0
  4180. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  4181. set_error_buf(error_buf, error_buf_size,
  4182. "invalid reference type or unknown type");
  4183. return false;
  4184. }
  4185. *p_elem_type = elem_type;
  4186. #else
  4187. p--;
  4188. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  4189. module->type_count, &need_ref_type_map,
  4190. &elem_ref_type, false, error_buf, error_buf_size)) {
  4191. return false;
  4192. }
  4193. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  4194. set_error_buf(error_buf, error_buf_size,
  4195. "invalid reference type or unknown type");
  4196. return false;
  4197. }
  4198. *p_elem_type = elem_ref_type.ref_type;
  4199. if (need_ref_type_map) {
  4200. if (!(*p_elem_ref_type =
  4201. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  4202. error_buf, error_buf_size))) {
  4203. return false;
  4204. }
  4205. }
  4206. #endif
  4207. *p_buf = p;
  4208. return true;
  4209. fail:
  4210. return false;
  4211. }
  4212. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4213. static bool
  4214. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  4215. WASMModule *module, WASMTableSeg *table_segment,
  4216. char *error_buf, uint32 error_buf_size)
  4217. {
  4218. const uint8 *p = *p_buf, *p_end = buf_end;
  4219. uint32 function_count, function_index = 0, i;
  4220. uint64 total_size;
  4221. read_leb_uint32(p, p_end, function_count);
  4222. table_segment->value_count = function_count;
  4223. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  4224. if (total_size > 0
  4225. && !(table_segment->init_values =
  4226. (InitializerExpression *)loader_malloc(total_size, error_buf,
  4227. error_buf_size))) {
  4228. return false;
  4229. }
  4230. for (i = 0; i < function_count; i++) {
  4231. InitializerExpression *init_expr = &table_segment->init_values[i];
  4232. read_leb_uint32(p, p_end, function_index);
  4233. if (!check_function_index(module, function_index, error_buf,
  4234. error_buf_size)) {
  4235. return false;
  4236. }
  4237. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  4238. init_expr->u.unary.v.ref_index = function_index;
  4239. }
  4240. *p_buf = p;
  4241. return true;
  4242. fail:
  4243. return false;
  4244. }
  4245. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  4246. static bool
  4247. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  4248. WASMModule *module, WASMTableSeg *table_segment,
  4249. char *error_buf, uint32 error_buf_size)
  4250. {
  4251. const uint8 *p = *p_buf, *p_end = buf_end;
  4252. uint32 ref_count, i;
  4253. uint64 total_size;
  4254. read_leb_uint32(p, p_end, ref_count);
  4255. table_segment->value_count = ref_count;
  4256. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  4257. if (total_size > 0
  4258. && !(table_segment->init_values =
  4259. (InitializerExpression *)loader_malloc(total_size, error_buf,
  4260. error_buf_size))) {
  4261. return false;
  4262. }
  4263. for (i = 0; i < ref_count; i++) {
  4264. InitializerExpression *init_expr = &table_segment->init_values[i];
  4265. if (!load_init_expr(module, &p, p_end, init_expr,
  4266. table_segment->elem_type,
  4267. #if WASM_ENABLE_GC == 0
  4268. NULL,
  4269. #else
  4270. table_segment->elem_ref_type,
  4271. #endif
  4272. error_buf, error_buf_size))
  4273. return false;
  4274. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  4275. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  4276. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  4277. && init_expr->init_expr_type
  4278. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  4279. }
  4280. *p_buf = p;
  4281. return true;
  4282. fail:
  4283. return false;
  4284. }
  4285. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  4286. static bool
  4287. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  4288. WASMModule *module, char *error_buf,
  4289. uint32 error_buf_size)
  4290. {
  4291. const uint8 *p = buf, *p_end = buf_end;
  4292. uint8 table_elem_idx_type;
  4293. uint32 table_segment_count, i;
  4294. uint64 total_size;
  4295. WASMTableSeg *table_segment;
  4296. read_leb_uint32(p, p_end, table_segment_count);
  4297. if (table_segment_count) {
  4298. module->table_seg_count = table_segment_count;
  4299. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  4300. if (!(module->table_segments =
  4301. loader_malloc(total_size, error_buf, error_buf_size))) {
  4302. return false;
  4303. }
  4304. table_segment = module->table_segments;
  4305. for (i = 0; i < table_segment_count; i++, table_segment++) {
  4306. if (p >= p_end) {
  4307. set_error_buf(error_buf, error_buf_size,
  4308. "invalid value type or "
  4309. "invalid elements segment kind");
  4310. return false;
  4311. }
  4312. table_elem_idx_type = VALUE_TYPE_I32;
  4313. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4314. read_leb_uint32(p, p_end, table_segment->mode);
  4315. /* last three bits */
  4316. table_segment->mode = table_segment->mode & 0x07;
  4317. switch (table_segment->mode) {
  4318. /* elemkind/elemtype + active */
  4319. case 0:
  4320. case 4:
  4321. {
  4322. #if WASM_ENABLE_GC != 0
  4323. if (table_segment->mode == 0) {
  4324. /* vec(funcidx), set elem type to (ref func) */
  4325. WASMRefType elem_ref_type = { 0 };
  4326. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4327. wasm_set_refheaptype_common(
  4328. &elem_ref_type.ref_ht_common, false,
  4329. HEAP_TYPE_FUNC);
  4330. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4331. module->ref_type_set, &elem_ref_type,
  4332. error_buf, error_buf_size)))
  4333. return false;
  4334. }
  4335. else {
  4336. /* vec(expr), set elem type to funcref */
  4337. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4338. }
  4339. #else
  4340. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4341. #endif
  4342. table_segment->table_index = 0;
  4343. if (!check_table_index(module, table_segment->table_index,
  4344. error_buf, error_buf_size))
  4345. return false;
  4346. #if WASM_ENABLE_MEMORY64 != 0
  4347. table_elem_idx_type =
  4348. is_table_64bit(module, table_segment->table_index)
  4349. ? VALUE_TYPE_I64
  4350. : VALUE_TYPE_I32;
  4351. #endif
  4352. if (!load_init_expr(module, &p, p_end,
  4353. &table_segment->base_offset,
  4354. table_elem_idx_type, NULL, error_buf,
  4355. error_buf_size))
  4356. return false;
  4357. if (table_segment->mode == 0) {
  4358. /* vec(funcidx) */
  4359. if (!load_func_index_vec(&p, p_end, module,
  4360. table_segment, error_buf,
  4361. error_buf_size))
  4362. return false;
  4363. }
  4364. else {
  4365. /* vec(expr) */
  4366. if (!load_init_expr_vec(&p, p_end, module,
  4367. table_segment, error_buf,
  4368. error_buf_size))
  4369. return false;
  4370. }
  4371. if (!check_table_elem_type(module,
  4372. table_segment->table_index,
  4373. table_segment->elem_type,
  4374. error_buf, error_buf_size))
  4375. return false;
  4376. break;
  4377. }
  4378. /* elemkind + passive/declarative */
  4379. case 1:
  4380. case 3:
  4381. if (!load_elem_type(module, &p, p_end,
  4382. &table_segment->elem_type,
  4383. #if WASM_ENABLE_GC != 0
  4384. &table_segment->elem_ref_type,
  4385. #endif
  4386. true, error_buf, error_buf_size))
  4387. return false;
  4388. /* vec(funcidx) */
  4389. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4390. error_buf, error_buf_size))
  4391. return false;
  4392. break;
  4393. /* elemkind/elemtype + table_idx + active */
  4394. case 2:
  4395. case 6:
  4396. if (!load_table_index(&p, p_end, module,
  4397. &table_segment->table_index,
  4398. error_buf, error_buf_size))
  4399. return false;
  4400. #if WASM_ENABLE_MEMORY64 != 0
  4401. table_elem_idx_type =
  4402. is_table_64bit(module, table_segment->table_index)
  4403. ? VALUE_TYPE_I64
  4404. : VALUE_TYPE_I32;
  4405. #endif
  4406. if (!load_init_expr(module, &p, p_end,
  4407. &table_segment->base_offset,
  4408. table_elem_idx_type, NULL, error_buf,
  4409. error_buf_size))
  4410. return false;
  4411. if (!load_elem_type(module, &p, p_end,
  4412. &table_segment->elem_type,
  4413. #if WASM_ENABLE_GC != 0
  4414. &table_segment->elem_ref_type,
  4415. #endif
  4416. table_segment->mode == 2 ? true : false,
  4417. error_buf, error_buf_size))
  4418. return false;
  4419. if (table_segment->mode == 2) {
  4420. /* vec(funcidx) */
  4421. if (!load_func_index_vec(&p, p_end, module,
  4422. table_segment, error_buf,
  4423. error_buf_size))
  4424. return false;
  4425. }
  4426. else {
  4427. /* vec(expr) */
  4428. if (!load_init_expr_vec(&p, p_end, module,
  4429. table_segment, error_buf,
  4430. error_buf_size))
  4431. return false;
  4432. }
  4433. if (!check_table_elem_type(module,
  4434. table_segment->table_index,
  4435. table_segment->elem_type,
  4436. error_buf, error_buf_size))
  4437. return false;
  4438. break;
  4439. case 5:
  4440. case 7:
  4441. if (!load_elem_type(module, &p, p_end,
  4442. &table_segment->elem_type,
  4443. #if WASM_ENABLE_GC != 0
  4444. &table_segment->elem_ref_type,
  4445. #endif
  4446. false, error_buf, error_buf_size))
  4447. return false;
  4448. /* vec(expr) */
  4449. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4450. error_buf, error_buf_size))
  4451. return false;
  4452. break;
  4453. default:
  4454. set_error_buf(error_buf, error_buf_size,
  4455. "unknown element segment kind");
  4456. return false;
  4457. }
  4458. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4459. /*
  4460. * like: 00 41 05 0b 04 00 01 00 01
  4461. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4462. */
  4463. if (!load_table_index(&p, p_end, module,
  4464. &table_segment->table_index, error_buf,
  4465. error_buf_size))
  4466. return false;
  4467. #if WASM_ENABLE_MEMORY64 != 0
  4468. table_elem_idx_type =
  4469. is_table_64bit(module, table_segment->table_index)
  4470. ? VALUE_TYPE_I64
  4471. : VALUE_TYPE_I32;
  4472. #endif
  4473. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4474. table_elem_idx_type, NULL, error_buf,
  4475. error_buf_size))
  4476. return false;
  4477. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4478. error_buf, error_buf_size))
  4479. return false;
  4480. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4481. if (!check_table_elem_type(module, table_segment->table_index,
  4482. table_segment->elem_type, error_buf,
  4483. error_buf_size))
  4484. return false;
  4485. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4486. #if WASM_ENABLE_MEMORY64 != 0
  4487. if (table_elem_idx_type == VALUE_TYPE_I64
  4488. && table_segment->base_offset.u.unary.v.u64 > UINT32_MAX) {
  4489. set_error_buf(error_buf, error_buf_size,
  4490. "In table64, table base offset can't be "
  4491. "larger than UINT32_MAX");
  4492. return false;
  4493. }
  4494. #endif
  4495. #if WASM_ENABLE_WAMR_COMPILER != 0
  4496. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4497. module->is_ref_types_used = true;
  4498. #endif
  4499. }
  4500. }
  4501. if (p != p_end) {
  4502. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4503. return false;
  4504. }
  4505. LOG_VERBOSE("Load table segment section success.\n");
  4506. return true;
  4507. fail:
  4508. return false;
  4509. }
  4510. #if WASM_ENABLE_BULK_MEMORY != 0
  4511. static bool
  4512. check_data_count_consistency(bool has_datacount_section, int datacount_len,
  4513. int data_seg_len, char *error_buf,
  4514. uint32 error_buf_size)
  4515. {
  4516. if (has_datacount_section && datacount_len != data_seg_len) {
  4517. set_error_buf(error_buf, error_buf_size,
  4518. "data count and data section have inconsistent lengths");
  4519. return false;
  4520. }
  4521. return true;
  4522. }
  4523. #endif
  4524. static bool
  4525. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4526. WASMModule *module,
  4527. #if WASM_ENABLE_BULK_MEMORY != 0
  4528. bool has_datacount_section,
  4529. #endif
  4530. bool clone_data_seg, char *error_buf,
  4531. uint32 error_buf_size)
  4532. {
  4533. const uint8 *p = buf, *p_end = buf_end;
  4534. uint32 data_seg_count, i, mem_index, data_seg_len;
  4535. uint64 total_size;
  4536. WASMDataSeg *dataseg;
  4537. InitializerExpression init_expr;
  4538. #if WASM_ENABLE_BULK_MEMORY != 0
  4539. bool is_passive = false;
  4540. uint32 mem_flag;
  4541. #endif
  4542. uint8 mem_offset_type = VALUE_TYPE_I32;
  4543. read_leb_uint32(p, p_end, data_seg_count);
  4544. #if WASM_ENABLE_BULK_MEMORY != 0
  4545. if (!check_data_count_consistency(has_datacount_section,
  4546. module->data_seg_count1, data_seg_count,
  4547. error_buf, error_buf_size)) {
  4548. return false;
  4549. }
  4550. #endif
  4551. if (data_seg_count) {
  4552. module->data_seg_count = data_seg_count;
  4553. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4554. if (!(module->data_segments =
  4555. loader_malloc(total_size, error_buf, error_buf_size))) {
  4556. return false;
  4557. }
  4558. for (i = 0; i < data_seg_count; i++) {
  4559. read_leb_uint32(p, p_end, mem_index);
  4560. #if WASM_ENABLE_BULK_MEMORY != 0
  4561. is_passive = false;
  4562. mem_flag = mem_index & 0x03;
  4563. switch (mem_flag) {
  4564. case 0x01:
  4565. is_passive = true;
  4566. #if WASM_ENABLE_WAMR_COMPILER != 0
  4567. module->is_bulk_memory_used = true;
  4568. #endif
  4569. break;
  4570. case 0x00:
  4571. /* no memory index, treat index as 0 */
  4572. mem_index = 0;
  4573. goto check_mem_index;
  4574. case 0x02:
  4575. /* read following memory index */
  4576. read_leb_uint32(p, p_end, mem_index);
  4577. #if WASM_ENABLE_WAMR_COMPILER != 0
  4578. module->is_bulk_memory_used = true;
  4579. #endif
  4580. check_mem_index:
  4581. if (mem_index
  4582. >= module->import_memory_count + module->memory_count) {
  4583. set_error_buf_v(error_buf, error_buf_size,
  4584. "unknown memory %d", mem_index);
  4585. return false;
  4586. }
  4587. break;
  4588. case 0x03:
  4589. default:
  4590. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4591. return false;
  4592. break;
  4593. }
  4594. #else
  4595. if (mem_index
  4596. >= module->import_memory_count + module->memory_count) {
  4597. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4598. mem_index);
  4599. return false;
  4600. }
  4601. #endif /* WASM_ENABLE_BULK_MEMORY */
  4602. #if WASM_ENABLE_BULK_MEMORY != 0
  4603. if (!is_passive)
  4604. #endif
  4605. {
  4606. #if WASM_ENABLE_MEMORY64 != 0
  4607. /* This memory_flag is from memory instead of data segment */
  4608. uint8 memory_flag;
  4609. if (module->import_memory_count > 0) {
  4610. memory_flag = module->import_memories[mem_index]
  4611. .u.memory.mem_type.flags;
  4612. }
  4613. else {
  4614. memory_flag =
  4615. module
  4616. ->memories[mem_index - module->import_memory_count]
  4617. .flags;
  4618. }
  4619. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4620. : VALUE_TYPE_I32;
  4621. #else
  4622. mem_offset_type = VALUE_TYPE_I32;
  4623. #endif
  4624. }
  4625. #if WASM_ENABLE_BULK_MEMORY != 0
  4626. if (!is_passive)
  4627. #endif
  4628. if (!load_init_expr(module, &p, p_end, &init_expr,
  4629. mem_offset_type, NULL, error_buf,
  4630. error_buf_size))
  4631. return false;
  4632. read_leb_uint32(p, p_end, data_seg_len);
  4633. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4634. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4635. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  4636. destroy_init_expr(module, &init_expr);
  4637. #endif
  4638. return false;
  4639. }
  4640. #if WASM_ENABLE_BULK_MEMORY != 0
  4641. dataseg->is_passive = is_passive;
  4642. if (!is_passive)
  4643. #endif
  4644. {
  4645. bh_memcpy_s(&dataseg->base_offset,
  4646. sizeof(InitializerExpression), &init_expr,
  4647. sizeof(InitializerExpression));
  4648. dataseg->memory_index = mem_index;
  4649. }
  4650. dataseg->data_length = data_seg_len;
  4651. CHECK_BUF(p, p_end, data_seg_len);
  4652. if (clone_data_seg) {
  4653. if (!(dataseg->data = loader_malloc(
  4654. dataseg->data_length, error_buf, error_buf_size))) {
  4655. return false;
  4656. }
  4657. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4658. data_seg_len);
  4659. }
  4660. else {
  4661. dataseg->data = (uint8 *)p;
  4662. }
  4663. dataseg->is_data_cloned = clone_data_seg;
  4664. p += data_seg_len;
  4665. }
  4666. }
  4667. if (p != p_end) {
  4668. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4669. return false;
  4670. }
  4671. LOG_VERBOSE("Load data segment section success.\n");
  4672. return true;
  4673. fail:
  4674. return false;
  4675. }
  4676. #if WASM_ENABLE_BULK_MEMORY != 0
  4677. static bool
  4678. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4679. WASMModule *module, char *error_buf,
  4680. uint32 error_buf_size)
  4681. {
  4682. const uint8 *p = buf, *p_end = buf_end;
  4683. uint32 data_seg_count1 = 0;
  4684. read_leb_uint32(p, p_end, data_seg_count1);
  4685. module->data_seg_count1 = data_seg_count1;
  4686. if (p != p_end) {
  4687. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4688. return false;
  4689. }
  4690. #if WASM_ENABLE_WAMR_COMPILER != 0
  4691. module->is_bulk_memory_used = true;
  4692. #endif
  4693. LOG_VERBOSE("Load datacount section success.\n");
  4694. return true;
  4695. fail:
  4696. return false;
  4697. }
  4698. #endif
  4699. #if WASM_ENABLE_TAGS != 0
  4700. static bool
  4701. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4702. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4703. uint32 error_buf_size)
  4704. {
  4705. (void)buf_code;
  4706. (void)buf_code_end;
  4707. const uint8 *p = buf, *p_end = buf_end;
  4708. size_t total_size = 0;
  4709. /* number of tags defined in the section */
  4710. uint32 section_tag_count = 0;
  4711. uint8 tag_attribute;
  4712. uint32 tag_type;
  4713. WASMTag *tag = NULL;
  4714. /* get tag count */
  4715. read_leb_uint32(p, p_end, section_tag_count);
  4716. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4717. error_buf, error_buf_size))
  4718. return false;
  4719. module->tag_count = section_tag_count;
  4720. if (section_tag_count) {
  4721. total_size = sizeof(WASMTag *) * module->tag_count;
  4722. if (!(module->tags =
  4723. loader_malloc(total_size, error_buf, error_buf_size))) {
  4724. return false;
  4725. }
  4726. /* load each tag, imported tags precede the tags */
  4727. uint32 tag_index;
  4728. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4729. /* get the one byte attribute */
  4730. CHECK_BUF(p, p_end, 1);
  4731. tag_attribute = read_uint8(p);
  4732. /* get type */
  4733. read_leb_uint32(p, p_end, tag_type);
  4734. /* compare against module->types */
  4735. if (!check_function_type(module, tag_type, error_buf,
  4736. error_buf_size)) {
  4737. return false;
  4738. }
  4739. /* get return type (must be 0) */
  4740. /* check, that the type of the referred tag returns void */
  4741. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4742. if (func_type->result_count != 0) {
  4743. set_error_buf(error_buf, error_buf_size,
  4744. "non-empty tag result type");
  4745. goto fail;
  4746. }
  4747. if (!(tag = module->tags[tag_index] = loader_malloc(
  4748. sizeof(WASMTag), error_buf, error_buf_size))) {
  4749. return false;
  4750. }
  4751. /* store to module tag declarations */
  4752. tag->attribute = tag_attribute;
  4753. tag->type = tag_type;
  4754. tag->tag_type = func_type;
  4755. }
  4756. }
  4757. if (p != p_end) {
  4758. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4759. return false;
  4760. }
  4761. LOG_VERBOSE("Load tag section success.\n");
  4762. return true;
  4763. fail:
  4764. return false;
  4765. }
  4766. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4767. static bool
  4768. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4769. const uint8 *buf_func_end, WASMModule *module,
  4770. char *error_buf, uint32 error_buf_size)
  4771. {
  4772. const uint8 *p = buf, *p_end = buf_end;
  4773. const uint8 *p_func = buf_func;
  4774. uint32 func_count = 0, code_count;
  4775. /* code has been loaded in function section, so pass it here, just check
  4776. * whether function and code section have inconsistent lengths */
  4777. read_leb_uint32(p, p_end, code_count);
  4778. if (buf_func)
  4779. read_leb_uint32(p_func, buf_func_end, func_count);
  4780. if (func_count != code_count) {
  4781. set_error_buf(error_buf, error_buf_size,
  4782. "function and code section have inconsistent lengths");
  4783. return false;
  4784. }
  4785. LOG_VERBOSE("Load code segment section success.\n");
  4786. (void)module;
  4787. return true;
  4788. fail:
  4789. return false;
  4790. }
  4791. static bool
  4792. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4793. char *error_buf, uint32 error_buf_size)
  4794. {
  4795. const uint8 *p = buf, *p_end = buf_end;
  4796. WASMFuncType *type;
  4797. uint32 start_function;
  4798. read_leb_uint32(p, p_end, start_function);
  4799. if (start_function
  4800. >= module->function_count + module->import_function_count) {
  4801. set_error_buf(error_buf, error_buf_size, "unknown function");
  4802. return false;
  4803. }
  4804. if (start_function < module->import_function_count)
  4805. type = module->import_functions[start_function].u.function.func_type;
  4806. else
  4807. type = module->functions[start_function - module->import_function_count]
  4808. ->func_type;
  4809. if (type->param_count != 0 || type->result_count != 0) {
  4810. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4811. return false;
  4812. }
  4813. module->start_function = start_function;
  4814. if (p != p_end) {
  4815. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4816. return false;
  4817. }
  4818. LOG_VERBOSE("Load start section success.\n");
  4819. return true;
  4820. fail:
  4821. return false;
  4822. }
  4823. #if WASM_ENABLE_STRINGREF != 0
  4824. static bool
  4825. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4826. WASMModule *module, bool is_load_from_file_buf,
  4827. char *error_buf, uint32 error_buf_size)
  4828. {
  4829. const uint8 *p = buf, *p_end = buf_end;
  4830. int32 deferred_count, immediate_count, string_length, i;
  4831. uint64 total_size;
  4832. read_leb_uint32(p, p_end, deferred_count);
  4833. read_leb_uint32(p, p_end, immediate_count);
  4834. /* proposal set deferred_count for future extension */
  4835. if (deferred_count != 0) {
  4836. goto fail;
  4837. }
  4838. if (immediate_count > 0) {
  4839. total_size = sizeof(char *) * (uint64)immediate_count;
  4840. if (!(module->string_literal_ptrs =
  4841. loader_malloc(total_size, error_buf, error_buf_size))) {
  4842. goto fail;
  4843. }
  4844. module->string_literal_count = immediate_count;
  4845. total_size = sizeof(uint32) * (uint64)immediate_count;
  4846. if (!(module->string_literal_lengths =
  4847. loader_malloc(total_size, error_buf, error_buf_size))) {
  4848. goto fail;
  4849. }
  4850. for (i = 0; i < immediate_count; i++) {
  4851. read_leb_uint32(p, p_end, string_length);
  4852. CHECK_BUF(p, p_end, string_length);
  4853. module->string_literal_ptrs[i] = p;
  4854. module->string_literal_lengths[i] = string_length;
  4855. p += string_length;
  4856. }
  4857. }
  4858. if (p != p_end) {
  4859. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4860. goto fail;
  4861. }
  4862. LOG_VERBOSE("Load stringref section success.\n");
  4863. return true;
  4864. fail:
  4865. return false;
  4866. }
  4867. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4868. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4869. static bool
  4870. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4871. bool is_load_from_file_buf, char *error_buf,
  4872. uint32 error_buf_size)
  4873. {
  4874. const uint8 *p = buf, *p_end = buf_end;
  4875. uint32 name_type, subsection_size;
  4876. uint32 previous_name_type = 0;
  4877. uint32 num_func_name;
  4878. uint32 func_index;
  4879. uint32 previous_func_index = ~0U;
  4880. uint32 func_name_len;
  4881. uint32 name_index;
  4882. int i = 0;
  4883. if (p >= p_end) {
  4884. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4885. return false;
  4886. }
  4887. while (p < p_end) {
  4888. read_leb_uint32(p, p_end, name_type);
  4889. if (i != 0) {
  4890. if (name_type == previous_name_type) {
  4891. set_error_buf(error_buf, error_buf_size,
  4892. "duplicate sub-section");
  4893. return false;
  4894. }
  4895. if (name_type < previous_name_type) {
  4896. set_error_buf(error_buf, error_buf_size,
  4897. "out-of-order sub-section");
  4898. return false;
  4899. }
  4900. }
  4901. previous_name_type = name_type;
  4902. read_leb_uint32(p, p_end, subsection_size);
  4903. CHECK_BUF(p, p_end, subsection_size);
  4904. switch (name_type) {
  4905. case SUB_SECTION_TYPE_FUNC:
  4906. if (subsection_size) {
  4907. read_leb_uint32(p, p_end, num_func_name);
  4908. for (name_index = 0; name_index < num_func_name;
  4909. name_index++) {
  4910. read_leb_uint32(p, p_end, func_index);
  4911. if (func_index == previous_func_index) {
  4912. set_error_buf(error_buf, error_buf_size,
  4913. "duplicate function name");
  4914. return false;
  4915. }
  4916. if (func_index < previous_func_index
  4917. && previous_func_index != ~0U) {
  4918. set_error_buf(error_buf, error_buf_size,
  4919. "out-of-order function index ");
  4920. return false;
  4921. }
  4922. previous_func_index = func_index;
  4923. read_leb_uint32(p, p_end, func_name_len);
  4924. CHECK_BUF(p, p_end, func_name_len);
  4925. /* Skip the import functions */
  4926. if (func_index >= module->import_function_count) {
  4927. func_index -= module->import_function_count;
  4928. if (func_index >= module->function_count) {
  4929. set_error_buf(error_buf, error_buf_size,
  4930. "out-of-range function index");
  4931. return false;
  4932. }
  4933. if (!(module->functions[func_index]->field_name =
  4934. wasm_const_str_list_insert(
  4935. p, func_name_len, module,
  4936. #if WASM_ENABLE_WAMR_COMPILER != 0
  4937. false,
  4938. #else
  4939. is_load_from_file_buf,
  4940. #endif
  4941. error_buf, error_buf_size))) {
  4942. return false;
  4943. }
  4944. }
  4945. p += func_name_len;
  4946. }
  4947. }
  4948. break;
  4949. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4950. */
  4951. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4952. default:
  4953. p = p + subsection_size;
  4954. break;
  4955. }
  4956. i++;
  4957. }
  4958. return true;
  4959. fail:
  4960. return false;
  4961. }
  4962. #endif
  4963. static bool
  4964. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4965. bool is_load_from_file_buf, char *error_buf,
  4966. uint32 error_buf_size)
  4967. {
  4968. const uint8 *p = buf, *p_end = buf_end;
  4969. char section_name[32];
  4970. uint32 name_len, buffer_len;
  4971. if (p >= p_end) {
  4972. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4973. return false;
  4974. }
  4975. read_leb_uint32(p, p_end, name_len);
  4976. if (p + name_len > p_end) {
  4977. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4978. return false;
  4979. }
  4980. if (!wasm_check_utf8_str(p, name_len)) {
  4981. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4982. return false;
  4983. }
  4984. buffer_len = sizeof(section_name);
  4985. memset(section_name, 0, buffer_len);
  4986. if (name_len < buffer_len) {
  4987. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4988. }
  4989. else {
  4990. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4991. memset(section_name + buffer_len - 4, '.', 3);
  4992. }
  4993. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4994. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4995. module->name_section_buf = buf;
  4996. module->name_section_buf_end = buf_end;
  4997. p += name_len;
  4998. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4999. error_buf, error_buf_size)) {
  5000. return false;
  5001. }
  5002. LOG_VERBOSE("Load custom name section success.");
  5003. }
  5004. #endif
  5005. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  5006. {
  5007. WASMCustomSection *section =
  5008. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  5009. if (!section) {
  5010. return false;
  5011. }
  5012. section->name_addr = (char *)p;
  5013. section->name_len = name_len;
  5014. section->content_addr = (uint8 *)(p + name_len);
  5015. section->content_len = (uint32)(p_end - p - name_len);
  5016. section->next = module->custom_section_list;
  5017. module->custom_section_list = section;
  5018. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  5019. return true;
  5020. }
  5021. #endif
  5022. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  5023. (void)is_load_from_file_buf;
  5024. (void)module;
  5025. return true;
  5026. fail:
  5027. return false;
  5028. }
  5029. static void
  5030. calculate_global_data_offset(WASMModule *module)
  5031. {
  5032. uint32 i, data_offset;
  5033. data_offset = 0;
  5034. for (i = 0; i < module->import_global_count; i++) {
  5035. WASMGlobalImport *import_global =
  5036. &((module->import_globals + i)->u.global);
  5037. #if WASM_ENABLE_FAST_JIT != 0
  5038. import_global->data_offset = data_offset;
  5039. #endif
  5040. data_offset += wasm_value_type_size(import_global->type.val_type);
  5041. }
  5042. for (i = 0; i < module->global_count; i++) {
  5043. WASMGlobal *global = module->globals + i;
  5044. #if WASM_ENABLE_FAST_JIT != 0
  5045. global->data_offset = data_offset;
  5046. #endif
  5047. data_offset += wasm_value_type_size(global->type.val_type);
  5048. }
  5049. module->global_data_size = data_offset;
  5050. }
  5051. #if WASM_ENABLE_FAST_JIT != 0
  5052. static bool
  5053. init_fast_jit_functions(WASMModule *module, char *error_buf,
  5054. uint32 error_buf_size)
  5055. {
  5056. #if WASM_ENABLE_LAZY_JIT != 0
  5057. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  5058. #endif
  5059. uint32 i;
  5060. if (!module->function_count)
  5061. return true;
  5062. if (!(module->fast_jit_func_ptrs =
  5063. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  5064. error_buf_size))) {
  5065. return false;
  5066. }
  5067. #if WASM_ENABLE_LAZY_JIT != 0
  5068. for (i = 0; i < module->function_count; i++) {
  5069. module->fast_jit_func_ptrs[i] =
  5070. jit_globals->compile_fast_jit_and_then_call;
  5071. }
  5072. #endif
  5073. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  5074. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  5075. set_error_buf(error_buf, error_buf_size,
  5076. "init fast jit thread lock failed");
  5077. return false;
  5078. }
  5079. module->fast_jit_thread_locks_inited[i] = true;
  5080. }
  5081. return true;
  5082. }
  5083. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  5084. #if WASM_ENABLE_JIT != 0
  5085. static bool
  5086. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  5087. uint32 error_buf_size)
  5088. {
  5089. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  5090. AOTCompOption option = { 0 };
  5091. char *aot_last_error;
  5092. uint64 size;
  5093. #if WASM_ENABLE_GC != 0
  5094. bool gc_enabled = true;
  5095. #else
  5096. bool gc_enabled = false;
  5097. #endif
  5098. if (module->function_count == 0)
  5099. return true;
  5100. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5101. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  5102. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  5103. return false;
  5104. }
  5105. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  5106. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  5107. os_mutex_destroy(&module->tierup_wait_lock);
  5108. return false;
  5109. }
  5110. module->tierup_wait_lock_inited = true;
  5111. #endif
  5112. size = sizeof(void *) * (uint64)module->function_count
  5113. + sizeof(bool) * (uint64)module->function_count;
  5114. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  5115. return false;
  5116. }
  5117. module->func_ptrs_compiled =
  5118. (bool *)((uint8 *)module->func_ptrs
  5119. + sizeof(void *) * module->function_count);
  5120. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  5121. if (!module->comp_data) {
  5122. aot_last_error = aot_get_last_error();
  5123. bh_assert(aot_last_error != NULL);
  5124. set_error_buf(error_buf, error_buf_size, aot_last_error);
  5125. return false;
  5126. }
  5127. option.is_jit_mode = true;
  5128. option.opt_level = llvm_jit_options->opt_level;
  5129. option.size_level = llvm_jit_options->size_level;
  5130. option.segue_flags = llvm_jit_options->segue_flags;
  5131. option.quick_invoke_c_api_import =
  5132. llvm_jit_options->quick_invoke_c_api_import;
  5133. #if WASM_ENABLE_BULK_MEMORY != 0
  5134. option.enable_bulk_memory = true;
  5135. #endif
  5136. #if WASM_ENABLE_THREAD_MGR != 0
  5137. option.enable_thread_mgr = true;
  5138. #endif
  5139. #if WASM_ENABLE_TAIL_CALL != 0
  5140. option.enable_tail_call = true;
  5141. #endif
  5142. #if WASM_ENABLE_SIMD != 0
  5143. option.enable_simd = true;
  5144. #endif
  5145. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5146. option.enable_ref_types = true;
  5147. #elif WASM_ENABLE_GC != 0
  5148. option.enable_gc = true;
  5149. #endif
  5150. option.enable_aux_stack_check = true;
  5151. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  5152. || WASM_ENABLE_AOT_STACK_FRAME != 0
  5153. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  5154. aot_call_stack_features_init_default(&option.call_stack_features);
  5155. #endif
  5156. #if WASM_ENABLE_PERF_PROFILING != 0
  5157. option.enable_perf_profiling = true;
  5158. #endif
  5159. #if WASM_ENABLE_MEMORY_PROFILING != 0
  5160. option.enable_memory_profiling = true;
  5161. option.enable_stack_estimation = true;
  5162. #endif
  5163. #if WASM_ENABLE_SHARED_HEAP != 0
  5164. option.enable_shared_heap = true;
  5165. #endif
  5166. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  5167. if (!module->comp_ctx) {
  5168. aot_last_error = aot_get_last_error();
  5169. bh_assert(aot_last_error != NULL);
  5170. set_error_buf(error_buf, error_buf_size, aot_last_error);
  5171. return false;
  5172. }
  5173. return true;
  5174. }
  5175. static bool
  5176. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  5177. uint32 error_buf_size)
  5178. {
  5179. char *aot_last_error;
  5180. uint32 i;
  5181. if (module->function_count == 0)
  5182. return true;
  5183. if (!aot_compile_wasm(module->comp_ctx)) {
  5184. aot_last_error = aot_get_last_error();
  5185. bh_assert(aot_last_error != NULL);
  5186. set_error_buf(error_buf, error_buf_size, aot_last_error);
  5187. return false;
  5188. }
  5189. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5190. if (module->orcjit_stop_compiling)
  5191. return false;
  5192. #endif
  5193. bh_print_time("Begin to lookup llvm jit functions");
  5194. for (i = 0; i < module->function_count; i++) {
  5195. LLVMOrcJITTargetAddress func_addr = 0;
  5196. LLVMErrorRef error;
  5197. char func_name[48];
  5198. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  5199. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  5200. func_name);
  5201. if (error != LLVMErrorSuccess) {
  5202. char *err_msg = LLVMGetErrorMessage(error);
  5203. set_error_buf_v(error_buf, error_buf_size,
  5204. "failed to compile llvm jit function: %s", err_msg);
  5205. LLVMDisposeErrorMessage(err_msg);
  5206. return false;
  5207. }
  5208. /**
  5209. * No need to lock the func_ptr[func_idx] here as it is basic
  5210. * data type, the load/store for it can be finished by one cpu
  5211. * instruction, and there can be only one cpu instruction
  5212. * loading/storing at the same time.
  5213. */
  5214. module->func_ptrs[i] = (void *)func_addr;
  5215. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5216. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  5217. if (module->orcjit_stop_compiling)
  5218. return false;
  5219. #endif
  5220. }
  5221. bh_print_time("End lookup llvm jit functions");
  5222. return true;
  5223. }
  5224. #endif /* end of WASM_ENABLE_JIT != 0 */
  5225. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5226. && WASM_ENABLE_LAZY_JIT != 0
  5227. static void *
  5228. init_llvm_jit_functions_stage2_callback(void *arg)
  5229. {
  5230. WASMModule *module = (WASMModule *)arg;
  5231. char error_buf[128];
  5232. uint32 error_buf_size = (uint32)sizeof(error_buf);
  5233. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5234. module->orcjit_stop_compiling = true;
  5235. return NULL;
  5236. }
  5237. os_mutex_lock(&module->tierup_wait_lock);
  5238. module->llvm_jit_inited = true;
  5239. os_cond_broadcast(&module->tierup_wait_cond);
  5240. os_mutex_unlock(&module->tierup_wait_lock);
  5241. return NULL;
  5242. }
  5243. #endif
  5244. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5245. /* The callback function to compile jit functions */
  5246. static void *
  5247. orcjit_thread_callback(void *arg)
  5248. {
  5249. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  5250. #if WASM_ENABLE_JIT != 0
  5251. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  5252. #endif
  5253. WASMModule *module = thread_arg->module;
  5254. uint32 group_idx = thread_arg->group_idx;
  5255. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  5256. uint32 func_count = module->function_count;
  5257. uint32 i;
  5258. #if WASM_ENABLE_FAST_JIT != 0
  5259. /* Compile fast jit functions of this group */
  5260. for (i = group_idx; i < func_count; i += group_stride) {
  5261. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  5262. LOG_ERROR("failed to compile fast jit function %u\n", i);
  5263. break;
  5264. }
  5265. if (module->orcjit_stop_compiling) {
  5266. return NULL;
  5267. }
  5268. }
  5269. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5270. os_mutex_lock(&module->tierup_wait_lock);
  5271. module->fast_jit_ready_groups++;
  5272. os_mutex_unlock(&module->tierup_wait_lock);
  5273. #endif
  5274. #endif
  5275. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5276. && WASM_ENABLE_LAZY_JIT != 0
  5277. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  5278. for (i = group_idx; i < func_count;
  5279. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5280. uint32 j;
  5281. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5282. if (i + j * group_stride < func_count) {
  5283. if (!jit_compiler_set_call_to_fast_jit(
  5284. module,
  5285. i + j * group_stride + module->import_function_count)) {
  5286. LOG_ERROR(
  5287. "failed to compile call_to_fast_jit for func %u\n",
  5288. i + j * group_stride + module->import_function_count);
  5289. module->orcjit_stop_compiling = true;
  5290. return NULL;
  5291. }
  5292. }
  5293. if (module->orcjit_stop_compiling) {
  5294. return NULL;
  5295. }
  5296. }
  5297. }
  5298. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  5299. fast jit functions are compiled */
  5300. os_mutex_lock(&module->tierup_wait_lock);
  5301. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  5302. && module->fast_jit_ready_groups >= group_stride)) {
  5303. os_cond_reltimedwait(&module->tierup_wait_cond,
  5304. &module->tierup_wait_lock, 10000);
  5305. if (module->orcjit_stop_compiling) {
  5306. /* init_llvm_jit_functions_stage2 failed */
  5307. os_mutex_unlock(&module->tierup_wait_lock);
  5308. return NULL;
  5309. }
  5310. }
  5311. os_mutex_unlock(&module->tierup_wait_lock);
  5312. #endif
  5313. #if WASM_ENABLE_JIT != 0
  5314. /* Compile llvm jit functions of this group */
  5315. for (i = group_idx; i < func_count;
  5316. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5317. LLVMOrcJITTargetAddress func_addr = 0;
  5318. LLVMErrorRef error;
  5319. char func_name[48];
  5320. typedef void (*F)(void);
  5321. union {
  5322. F f;
  5323. void *v;
  5324. } u;
  5325. uint32 j;
  5326. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  5327. "_wrapper");
  5328. LOG_DEBUG("compile llvm jit func %s", func_name);
  5329. error =
  5330. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  5331. if (error != LLVMErrorSuccess) {
  5332. char *err_msg = LLVMGetErrorMessage(error);
  5333. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  5334. LLVMDisposeErrorMessage(err_msg);
  5335. break;
  5336. }
  5337. /* Call the jit wrapper function to trigger its compilation, so as
  5338. to compile the actual jit functions, since we add the latter to
  5339. function list in the PartitionFunction callback */
  5340. u.v = (void *)func_addr;
  5341. u.f();
  5342. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5343. if (i + j * group_stride < func_count) {
  5344. module->func_ptrs_compiled[i + j * group_stride] = true;
  5345. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5346. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5347. i + j * group_stride);
  5348. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5349. func_name);
  5350. if (error != LLVMErrorSuccess) {
  5351. char *err_msg = LLVMGetErrorMessage(error);
  5352. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5353. err_msg);
  5354. LLVMDisposeErrorMessage(err_msg);
  5355. /* Ignore current llvm jit func, as its func ptr is
  5356. previous set to call_to_fast_jit, which also works */
  5357. continue;
  5358. }
  5359. jit_compiler_set_llvm_jit_func_ptr(
  5360. module,
  5361. i + j * group_stride + module->import_function_count,
  5362. (void *)func_addr);
  5363. /* Try to switch to call this llvm jit function instead of
  5364. fast jit function from fast jit jitted code */
  5365. jit_compiler_set_call_to_llvm_jit(
  5366. module,
  5367. i + j * group_stride + module->import_function_count);
  5368. #endif
  5369. }
  5370. }
  5371. if (module->orcjit_stop_compiling) {
  5372. break;
  5373. }
  5374. }
  5375. #endif
  5376. return NULL;
  5377. }
  5378. static void
  5379. orcjit_stop_compile_threads(WASMModule *module)
  5380. {
  5381. #if WASM_ENABLE_LAZY_JIT != 0
  5382. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5383. / sizeof(OrcJitThreadArg));
  5384. module->orcjit_stop_compiling = true;
  5385. for (i = 0; i < thread_num; i++) {
  5386. if (module->orcjit_threads[i])
  5387. os_thread_join(module->orcjit_threads[i], NULL);
  5388. }
  5389. #endif
  5390. }
  5391. static bool
  5392. compile_jit_functions(WASMModule *module, char *error_buf,
  5393. uint32 error_buf_size)
  5394. {
  5395. uint32 thread_num =
  5396. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5397. uint32 i, j;
  5398. bh_print_time("Begin to compile jit functions");
  5399. /* Create threads to compile the jit functions */
  5400. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5401. #if WASM_ENABLE_JIT != 0
  5402. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5403. #endif
  5404. module->orcjit_thread_args[i].module = module;
  5405. module->orcjit_thread_args[i].group_idx = i;
  5406. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5407. (void *)&module->orcjit_thread_args[i],
  5408. APP_THREAD_STACK_SIZE_DEFAULT)
  5409. != 0) {
  5410. set_error_buf(error_buf, error_buf_size,
  5411. "create orcjit compile thread failed");
  5412. /* Terminate the threads created */
  5413. module->orcjit_stop_compiling = true;
  5414. for (j = 0; j < i; j++) {
  5415. os_thread_join(module->orcjit_threads[j], NULL);
  5416. }
  5417. return false;
  5418. }
  5419. }
  5420. #if WASM_ENABLE_LAZY_JIT == 0
  5421. /* Wait until all jit functions are compiled for eager mode */
  5422. for (i = 0; i < thread_num; i++) {
  5423. if (module->orcjit_threads[i])
  5424. os_thread_join(module->orcjit_threads[i], NULL);
  5425. }
  5426. #if WASM_ENABLE_FAST_JIT != 0
  5427. /* Ensure all the fast-jit functions are compiled */
  5428. for (i = 0; i < module->function_count; i++) {
  5429. if (!jit_compiler_is_compiled(module,
  5430. i + module->import_function_count)) {
  5431. set_error_buf(error_buf, error_buf_size,
  5432. "failed to compile fast jit function");
  5433. return false;
  5434. }
  5435. }
  5436. #endif
  5437. #if WASM_ENABLE_JIT != 0
  5438. /* Ensure all the llvm-jit functions are compiled */
  5439. for (i = 0; i < module->function_count; i++) {
  5440. if (!module->func_ptrs_compiled[i]) {
  5441. set_error_buf(error_buf, error_buf_size,
  5442. "failed to compile llvm jit function");
  5443. return false;
  5444. }
  5445. }
  5446. #endif
  5447. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5448. bh_print_time("End compile jit functions");
  5449. return true;
  5450. }
  5451. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5452. static bool
  5453. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5454. uint32 cur_func_idx, char *error_buf,
  5455. uint32 error_buf_size);
  5456. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5457. void **
  5458. wasm_interp_get_handle_table(void);
  5459. static void **handle_table;
  5460. #endif
  5461. static bool
  5462. load_from_sections(WASMModule *module, WASMSection *sections,
  5463. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5464. bool no_resolve, char *error_buf, uint32 error_buf_size)
  5465. {
  5466. WASMExport *export;
  5467. WASMSection *section = sections;
  5468. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5469. *buf_func = NULL, *buf_func_end = NULL;
  5470. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5471. WASMGlobal *aux_stack_top_global = NULL, *global;
  5472. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5473. aux_stack_top = (uint64)-1LL;
  5474. uint32 global_index, func_index, i;
  5475. uint32 aux_data_end_global_index = (uint32)-1;
  5476. uint32 aux_heap_base_global_index = (uint32)-1;
  5477. WASMFuncType *func_type;
  5478. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5479. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5480. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5481. #if WASM_ENABLE_BULK_MEMORY != 0
  5482. bool has_datacount_section = false;
  5483. #endif
  5484. /* Find code and function sections if have */
  5485. while (section) {
  5486. if (section->section_type == SECTION_TYPE_CODE) {
  5487. buf_code = section->section_body;
  5488. buf_code_end = buf_code + section->section_body_size;
  5489. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5490. module->buf_code = (uint8 *)buf_code;
  5491. module->buf_code_size = section->section_body_size;
  5492. #endif
  5493. }
  5494. else if (section->section_type == SECTION_TYPE_FUNC) {
  5495. buf_func = section->section_body;
  5496. buf_func_end = buf_func + section->section_body_size;
  5497. }
  5498. section = section->next;
  5499. }
  5500. section = sections;
  5501. while (section) {
  5502. buf = section->section_body;
  5503. buf_end = buf + section->section_body_size;
  5504. switch (section->section_type) {
  5505. case SECTION_TYPE_USER:
  5506. /* unsupported user section, ignore it. */
  5507. if (!load_user_section(buf, buf_end, module,
  5508. reuse_const_strings, error_buf,
  5509. error_buf_size))
  5510. return false;
  5511. break;
  5512. case SECTION_TYPE_TYPE:
  5513. if (!load_type_section(buf, buf_end, module, error_buf,
  5514. error_buf_size))
  5515. return false;
  5516. break;
  5517. case SECTION_TYPE_IMPORT:
  5518. if (!load_import_section(buf, buf_end, module,
  5519. reuse_const_strings, no_resolve,
  5520. error_buf, error_buf_size))
  5521. return false;
  5522. break;
  5523. case SECTION_TYPE_FUNC:
  5524. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5525. module, error_buf, error_buf_size))
  5526. return false;
  5527. break;
  5528. case SECTION_TYPE_TABLE:
  5529. if (!load_table_section(buf, buf_end, module, error_buf,
  5530. error_buf_size))
  5531. return false;
  5532. break;
  5533. case SECTION_TYPE_MEMORY:
  5534. if (!load_memory_section(buf, buf_end, module, error_buf,
  5535. error_buf_size))
  5536. return false;
  5537. break;
  5538. #if WASM_ENABLE_TAGS != 0
  5539. case SECTION_TYPE_TAG:
  5540. /* load tag declaration section */
  5541. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5542. module, error_buf, error_buf_size))
  5543. return false;
  5544. break;
  5545. #endif
  5546. case SECTION_TYPE_GLOBAL:
  5547. if (!load_global_section(buf, buf_end, module, error_buf,
  5548. error_buf_size))
  5549. return false;
  5550. break;
  5551. case SECTION_TYPE_EXPORT:
  5552. if (!load_export_section(buf, buf_end, module,
  5553. reuse_const_strings, error_buf,
  5554. error_buf_size))
  5555. return false;
  5556. break;
  5557. case SECTION_TYPE_START:
  5558. if (!load_start_section(buf, buf_end, module, error_buf,
  5559. error_buf_size))
  5560. return false;
  5561. break;
  5562. case SECTION_TYPE_ELEM:
  5563. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5564. error_buf_size))
  5565. return false;
  5566. break;
  5567. case SECTION_TYPE_CODE:
  5568. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5569. module, error_buf, error_buf_size))
  5570. return false;
  5571. break;
  5572. case SECTION_TYPE_DATA:
  5573. if (!load_data_segment_section(buf, buf_end, module,
  5574. #if WASM_ENABLE_BULK_MEMORY != 0
  5575. has_datacount_section,
  5576. #endif
  5577. clone_data_seg, error_buf,
  5578. error_buf_size))
  5579. return false;
  5580. break;
  5581. #if WASM_ENABLE_BULK_MEMORY != 0
  5582. case SECTION_TYPE_DATACOUNT:
  5583. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5584. error_buf_size))
  5585. return false;
  5586. has_datacount_section = true;
  5587. break;
  5588. #endif
  5589. #if WASM_ENABLE_STRINGREF != 0
  5590. case SECTION_TYPE_STRINGREF:
  5591. if (!load_stringref_section(buf, buf_end, module,
  5592. reuse_const_strings, error_buf,
  5593. error_buf_size))
  5594. return false;
  5595. break;
  5596. #endif
  5597. default:
  5598. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5599. return false;
  5600. }
  5601. section = section->next;
  5602. }
  5603. #if WASM_ENABLE_BULK_MEMORY != 0
  5604. if (!check_data_count_consistency(
  5605. has_datacount_section, module->data_seg_count1,
  5606. module->data_seg_count, error_buf, error_buf_size)) {
  5607. return false;
  5608. }
  5609. #endif
  5610. module->aux_data_end_global_index = (uint32)-1;
  5611. module->aux_heap_base_global_index = (uint32)-1;
  5612. module->aux_stack_top_global_index = (uint32)-1;
  5613. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5614. export = module->exports;
  5615. for (i = 0; i < module->export_count; i++, export ++) {
  5616. if (export->kind == EXPORT_KIND_GLOBAL) {
  5617. if (!strcmp(export->name, "__heap_base")) {
  5618. if (export->index < module->import_global_count) {
  5619. LOG_DEBUG("Skip the process if __heap_base is imported "
  5620. "instead of being a local global");
  5621. continue;
  5622. }
  5623. /* only process linker-generated symbols */
  5624. global_index = export->index - module->import_global_count;
  5625. global = module->globals + global_index;
  5626. if (global->type.val_type == VALUE_TYPE_I32
  5627. && !global->type.is_mutable
  5628. && global->init_expr.init_expr_type
  5629. == INIT_EXPR_TYPE_I32_CONST) {
  5630. aux_heap_base_global = global;
  5631. aux_heap_base =
  5632. (uint64)(uint32)global->init_expr.u.unary.v.i32;
  5633. aux_heap_base_global_index = export->index;
  5634. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5635. aux_heap_base);
  5636. }
  5637. }
  5638. else if (!strcmp(export->name, "__data_end")) {
  5639. if (export->index < module->import_global_count) {
  5640. LOG_DEBUG("Skip the process if __data_end is imported "
  5641. "instead of being a local global");
  5642. continue;
  5643. }
  5644. /* only process linker-generated symbols */
  5645. global_index = export->index - module->import_global_count;
  5646. global = module->globals + global_index;
  5647. if (global->type.val_type == VALUE_TYPE_I32
  5648. && !global->type.is_mutable
  5649. && global->init_expr.init_expr_type
  5650. == INIT_EXPR_TYPE_I32_CONST) {
  5651. aux_data_end_global = global;
  5652. aux_data_end =
  5653. (uint64)(uint32)global->init_expr.u.unary.v.i32;
  5654. aux_data_end_global_index = export->index;
  5655. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5656. aux_data_end);
  5657. aux_data_end = align_uint64(aux_data_end, 16);
  5658. }
  5659. }
  5660. /* For module compiled with -pthread option, the global is:
  5661. [0] stack_top <-- 0
  5662. [1] tls_pointer
  5663. [2] tls_size
  5664. [3] data_end <-- 3
  5665. [4] global_base
  5666. [5] heap_base <-- 5
  5667. [6] dso_handle
  5668. For module compiled without -pthread option:
  5669. [0] stack_top <-- 0
  5670. [1] data_end <-- 1
  5671. [2] global_base
  5672. [3] heap_base <-- 3
  5673. [4] dso_handle
  5674. */
  5675. if (aux_data_end_global && aux_heap_base_global
  5676. && aux_data_end <= aux_heap_base) {
  5677. module->aux_data_end_global_index = aux_data_end_global_index;
  5678. module->aux_data_end = aux_data_end;
  5679. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5680. module->aux_heap_base = aux_heap_base;
  5681. /* Resolve aux stack top global */
  5682. for (global_index = 0; global_index < module->global_count;
  5683. global_index++) {
  5684. global = module->globals + global_index;
  5685. if (global->type.is_mutable /* heap_base and data_end is
  5686. not mutable */
  5687. && global->type.val_type == VALUE_TYPE_I32
  5688. && global->init_expr.init_expr_type
  5689. == INIT_EXPR_TYPE_I32_CONST
  5690. && (uint64)(uint32)global->init_expr.u.unary.v.i32
  5691. <= aux_heap_base) {
  5692. aux_stack_top_global = global;
  5693. aux_stack_top =
  5694. (uint64)(uint32)global->init_expr.u.unary.v.i32;
  5695. module->aux_stack_top_global_index =
  5696. module->import_global_count + global_index;
  5697. module->aux_stack_bottom = aux_stack_top;
  5698. module->aux_stack_size =
  5699. aux_stack_top > aux_data_end
  5700. ? (uint32)(aux_stack_top - aux_data_end)
  5701. : (uint32)aux_stack_top;
  5702. LOG_VERBOSE(
  5703. "Found aux stack top global, value: %" PRIu64 ", "
  5704. "global index: %d, stack size: %d",
  5705. aux_stack_top, global_index,
  5706. module->aux_stack_size);
  5707. break;
  5708. }
  5709. }
  5710. if (!aux_stack_top_global) {
  5711. /* Auxiliary stack global isn't found, it must be unused
  5712. in the wasm app, as if it is used, the global must be
  5713. defined. Here we set it to __heap_base global and set
  5714. its size to 0. */
  5715. aux_stack_top_global = aux_heap_base_global;
  5716. aux_stack_top = aux_heap_base;
  5717. module->aux_stack_top_global_index =
  5718. module->aux_heap_base_global_index;
  5719. module->aux_stack_bottom = aux_stack_top;
  5720. module->aux_stack_size = 0;
  5721. }
  5722. break;
  5723. }
  5724. }
  5725. }
  5726. module->malloc_function = (uint32)-1;
  5727. module->free_function = (uint32)-1;
  5728. module->retain_function = (uint32)-1;
  5729. /* Resolve malloc/free function exported by wasm module */
  5730. #if WASM_ENABLE_MEMORY64 != 0
  5731. if (has_module_memory64(module))
  5732. malloc_free_io_type = VALUE_TYPE_I64;
  5733. #endif
  5734. export = module->exports;
  5735. for (i = 0; i < module->export_count; i++, export ++) {
  5736. if (export->kind == EXPORT_KIND_FUNC) {
  5737. if (!strcmp(export->name, "malloc")
  5738. && export->index >= module->import_function_count) {
  5739. func_index = export->index - module->import_function_count;
  5740. func_type = module->functions[func_index]->func_type;
  5741. if (func_type->param_count == 1 && func_type->result_count == 1
  5742. && func_type->types[0] == malloc_free_io_type
  5743. && func_type->types[1] == malloc_free_io_type) {
  5744. bh_assert(module->malloc_function == (uint32)-1);
  5745. module->malloc_function = export->index;
  5746. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5747. export->name, export->index);
  5748. }
  5749. }
  5750. else if (!strcmp(export->name, "__new")
  5751. && export->index >= module->import_function_count) {
  5752. /* __new && __pin for AssemblyScript */
  5753. func_index = export->index - module->import_function_count;
  5754. func_type = module->functions[func_index]->func_type;
  5755. if (func_type->param_count == 2 && func_type->result_count == 1
  5756. && func_type->types[0] == malloc_free_io_type
  5757. && func_type->types[1] == VALUE_TYPE_I32
  5758. && func_type->types[2] == malloc_free_io_type) {
  5759. uint32 j;
  5760. WASMExport *export_tmp;
  5761. bh_assert(module->malloc_function == (uint32)-1);
  5762. module->malloc_function = export->index;
  5763. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5764. export->name, export->index);
  5765. /* resolve retain function.
  5766. If not found, reset malloc function index */
  5767. export_tmp = module->exports;
  5768. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5769. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5770. && (!strcmp(export_tmp->name, "__retain")
  5771. || (!strcmp(export_tmp->name, "__pin")))
  5772. && (export_tmp->index
  5773. >= module->import_function_count)) {
  5774. func_index = export_tmp->index
  5775. - module->import_function_count;
  5776. func_type =
  5777. module->functions[func_index]->func_type;
  5778. if (func_type->param_count == 1
  5779. && func_type->result_count == 1
  5780. && func_type->types[0] == malloc_free_io_type
  5781. && func_type->types[1] == malloc_free_io_type) {
  5782. bh_assert(module->retain_function
  5783. == (uint32)-1);
  5784. module->retain_function = export_tmp->index;
  5785. LOG_VERBOSE("Found retain function, name: %s, "
  5786. "index: %u",
  5787. export_tmp->name,
  5788. export_tmp->index);
  5789. break;
  5790. }
  5791. }
  5792. }
  5793. if (j == module->export_count) {
  5794. module->malloc_function = (uint32)-1;
  5795. LOG_VERBOSE("Can't find retain function,"
  5796. "reset malloc function index to -1");
  5797. }
  5798. }
  5799. }
  5800. else if (((!strcmp(export->name, "free"))
  5801. || (!strcmp(export->name, "__release"))
  5802. || (!strcmp(export->name, "__unpin")))
  5803. && export->index >= module->import_function_count) {
  5804. func_index = export->index - module->import_function_count;
  5805. func_type = module->functions[func_index]->func_type;
  5806. if (func_type->param_count == 1 && func_type->result_count == 0
  5807. && func_type->types[0] == malloc_free_io_type) {
  5808. bh_assert(module->free_function == (uint32)-1);
  5809. module->free_function = export->index;
  5810. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5811. export->name, export->index);
  5812. }
  5813. }
  5814. }
  5815. }
  5816. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5817. handle_table = wasm_interp_get_handle_table();
  5818. #endif
  5819. for (i = 0; i < module->function_count; i++) {
  5820. WASMFunction *func = module->functions[i];
  5821. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5822. error_buf_size)) {
  5823. return false;
  5824. }
  5825. if (i == module->function_count - 1
  5826. && func->code + func->code_size != buf_code_end) {
  5827. set_error_buf(error_buf, error_buf_size,
  5828. "code section size mismatch");
  5829. return false;
  5830. }
  5831. }
  5832. if (!module->possible_memory_grow) {
  5833. #if WASM_ENABLE_SHRUNK_MEMORY != 0
  5834. if (aux_data_end_global && aux_heap_base_global
  5835. && aux_stack_top_global) {
  5836. uint64 init_memory_size;
  5837. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5838. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5839. * valid range of uint32 */
  5840. if (shrunk_memory_size <= UINT32_MAX) {
  5841. if (module->import_memory_count) {
  5842. WASMMemoryImport *memory_import =
  5843. &module->import_memories[0].u.memory;
  5844. init_memory_size =
  5845. (uint64)memory_import->mem_type.num_bytes_per_page
  5846. * memory_import->mem_type.init_page_count;
  5847. if (shrunk_memory_size <= init_memory_size) {
  5848. /* Reset memory info to decrease memory usage */
  5849. memory_import->mem_type.num_bytes_per_page =
  5850. (uint32)shrunk_memory_size;
  5851. memory_import->mem_type.init_page_count = 1;
  5852. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5853. shrunk_memory_size);
  5854. }
  5855. }
  5856. if (module->memory_count) {
  5857. WASMMemory *memory = &module->memories[0];
  5858. init_memory_size = (uint64)memory->num_bytes_per_page
  5859. * memory->init_page_count;
  5860. if (shrunk_memory_size <= init_memory_size) {
  5861. /* Reset memory info to decrease memory usage */
  5862. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5863. memory->init_page_count = 1;
  5864. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5865. shrunk_memory_size);
  5866. }
  5867. }
  5868. }
  5869. }
  5870. #endif /* WASM_ENABLE_SHRUNK_MEMORY != 0 */
  5871. #if WASM_ENABLE_MULTI_MODULE == 0
  5872. if (module->import_memory_count) {
  5873. WASMMemoryImport *memory_import =
  5874. &module->import_memories[0].u.memory;
  5875. /* Only resize the memory to one big page if num_bytes_per_page is
  5876. * in valid range of uint32 */
  5877. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5878. memory_import->mem_type.num_bytes_per_page *=
  5879. memory_import->mem_type.init_page_count;
  5880. if (memory_import->mem_type.init_page_count > 0)
  5881. memory_import->mem_type.init_page_count =
  5882. memory_import->mem_type.max_page_count = 1;
  5883. else
  5884. memory_import->mem_type.init_page_count =
  5885. memory_import->mem_type.max_page_count = 0;
  5886. }
  5887. }
  5888. if (module->memory_count) {
  5889. WASMMemory *memory = &module->memories[0];
  5890. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5891. * valid range of uint32 */
  5892. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5893. memory->num_bytes_per_page *= memory->init_page_count;
  5894. if (memory->init_page_count > 0)
  5895. memory->init_page_count = memory->max_page_count = 1;
  5896. else
  5897. memory->init_page_count = memory->max_page_count = 0;
  5898. }
  5899. }
  5900. #endif
  5901. }
  5902. #if WASM_ENABLE_MEMORY64 != 0
  5903. if (!check_memory64_flags_consistency(module, error_buf, error_buf_size,
  5904. false))
  5905. return false;
  5906. #endif
  5907. calculate_global_data_offset(module);
  5908. #if WASM_ENABLE_FAST_JIT != 0
  5909. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5910. return false;
  5911. }
  5912. #endif
  5913. #if WASM_ENABLE_JIT != 0
  5914. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5915. return false;
  5916. }
  5917. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5918. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5919. return false;
  5920. }
  5921. #else
  5922. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5923. thread fast jit execution, since applying llvm optimizations in
  5924. aot_compile_wasm may cost a lot of time.
  5925. Create thread with enough native stack to apply llvm optimizations */
  5926. if (os_thread_create(&module->llvm_jit_init_thread,
  5927. init_llvm_jit_functions_stage2_callback,
  5928. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5929. != 0) {
  5930. set_error_buf(error_buf, error_buf_size,
  5931. "create orcjit compile thread failed");
  5932. return false;
  5933. }
  5934. #endif
  5935. #endif
  5936. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5937. /* Create threads to compile the jit functions */
  5938. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5939. return false;
  5940. }
  5941. #endif
  5942. #if WASM_ENABLE_MEMORY_TRACING != 0
  5943. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5944. #endif
  5945. return true;
  5946. }
  5947. static WASMModule *
  5948. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5949. {
  5950. WASMModule *module =
  5951. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5952. bh_list_status ret;
  5953. if (!module) {
  5954. return NULL;
  5955. }
  5956. module->module_type = Wasm_Module_Bytecode;
  5957. /* Set start_function to -1, means no start function */
  5958. module->start_function = (uint32)-1;
  5959. module->name = name;
  5960. module->is_binary_freeable = false;
  5961. #if WASM_ENABLE_FAST_INTERP == 0
  5962. module->br_table_cache_list = &module->br_table_cache_list_head;
  5963. ret = bh_list_init(module->br_table_cache_list);
  5964. bh_assert(ret == BH_LIST_SUCCESS);
  5965. #endif
  5966. #if WASM_ENABLE_MULTI_MODULE != 0
  5967. module->import_module_list = &module->import_module_list_head;
  5968. ret = bh_list_init(module->import_module_list);
  5969. bh_assert(ret == BH_LIST_SUCCESS);
  5970. #endif
  5971. #if WASM_ENABLE_DEBUG_INTERP != 0
  5972. ret = bh_list_init(&module->fast_opcode_list);
  5973. bh_assert(ret == BH_LIST_SUCCESS);
  5974. #endif
  5975. #if WASM_ENABLE_GC != 0
  5976. if (!(module->ref_type_set =
  5977. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5978. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5979. goto fail1;
  5980. }
  5981. if (os_mutex_init(&module->rtt_type_lock)) {
  5982. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5983. goto fail2;
  5984. }
  5985. #endif
  5986. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5987. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5988. && WASM_ENABLE_LAZY_JIT != 0)
  5989. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5990. set_error_buf(error_buf, error_buf_size,
  5991. "init instance list lock failed");
  5992. goto fail3;
  5993. }
  5994. #endif
  5995. #if WASM_ENABLE_LIBC_WASI != 0
  5996. #if WASM_ENABLE_UVWASI == 0
  5997. module->wasi_args.stdio[0] = os_invalid_raw_handle();
  5998. module->wasi_args.stdio[1] = os_invalid_raw_handle();
  5999. module->wasi_args.stdio[2] = os_invalid_raw_handle();
  6000. #else
  6001. module->wasi_args.stdio[0] = os_get_invalid_handle();
  6002. module->wasi_args.stdio[1] = os_get_invalid_handle();
  6003. module->wasi_args.stdio[2] = os_get_invalid_handle();
  6004. #endif /* WASM_ENABLE_UVWASI == 0 */
  6005. #endif /* WASM_ENABLE_LIBC_WASI != 0 */
  6006. (void)ret;
  6007. return module;
  6008. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6009. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  6010. && WASM_ENABLE_LAZY_JIT != 0)
  6011. fail3:
  6012. #endif
  6013. #if WASM_ENABLE_GC != 0
  6014. os_mutex_destroy(&module->rtt_type_lock);
  6015. fail2:
  6016. bh_hash_map_destroy(module->ref_type_set);
  6017. fail1:
  6018. #endif
  6019. wasm_runtime_free(module);
  6020. return NULL;
  6021. }
  6022. #if WASM_ENABLE_DEBUG_INTERP != 0
  6023. static bool
  6024. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  6025. uint32 error_buf_size)
  6026. {
  6027. WASMFastOPCodeNode *fast_op =
  6028. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  6029. if (fast_op) {
  6030. fast_op->offset = pos - module->load_addr;
  6031. fast_op->orig_op = orig_op;
  6032. bh_list_insert(&module->fast_opcode_list, fast_op);
  6033. }
  6034. return fast_op ? true : false;
  6035. }
  6036. #endif
  6037. WASMModule *
  6038. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  6039. uint32 error_buf_size)
  6040. {
  6041. WASMModule *module = create_module("", error_buf, error_buf_size);
  6042. if (!module)
  6043. return NULL;
  6044. if (!load_from_sections(module, section_list, false, true, false, error_buf,
  6045. error_buf_size)) {
  6046. wasm_loader_unload(module);
  6047. return NULL;
  6048. }
  6049. LOG_VERBOSE("Load module from sections success.\n");
  6050. return module;
  6051. }
  6052. static void
  6053. destroy_sections(WASMSection *section_list)
  6054. {
  6055. WASMSection *section = section_list, *next;
  6056. while (section) {
  6057. next = section->next;
  6058. wasm_runtime_free(section);
  6059. section = next;
  6060. }
  6061. }
  6062. /* clang-format off */
  6063. static uint8 section_ids[] = {
  6064. SECTION_TYPE_USER,
  6065. SECTION_TYPE_TYPE,
  6066. SECTION_TYPE_IMPORT,
  6067. SECTION_TYPE_FUNC,
  6068. SECTION_TYPE_TABLE,
  6069. SECTION_TYPE_MEMORY,
  6070. #if WASM_ENABLE_TAGS != 0
  6071. SECTION_TYPE_TAG,
  6072. #endif
  6073. #if WASM_ENABLE_STRINGREF != 0
  6074. /* must immediately precede the global section,
  6075. or where the global section would be */
  6076. SECTION_TYPE_STRINGREF,
  6077. #endif
  6078. SECTION_TYPE_GLOBAL,
  6079. SECTION_TYPE_EXPORT,
  6080. SECTION_TYPE_START,
  6081. SECTION_TYPE_ELEM,
  6082. #if WASM_ENABLE_BULK_MEMORY != 0
  6083. SECTION_TYPE_DATACOUNT,
  6084. #endif
  6085. SECTION_TYPE_CODE,
  6086. SECTION_TYPE_DATA
  6087. };
  6088. /* clang-format on */
  6089. static uint8
  6090. get_section_index(uint8 section_type)
  6091. {
  6092. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  6093. for (uint8 i = 0; i < max_id; i++) {
  6094. if (section_type == section_ids[i])
  6095. return i;
  6096. }
  6097. return (uint8)-1;
  6098. }
  6099. static bool
  6100. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  6101. char *error_buf, uint32 error_buf_size)
  6102. {
  6103. WASMSection *section_list_end = NULL, *section;
  6104. const uint8 *p = buf, *p_end = buf + size;
  6105. uint8 section_type, section_index, last_section_index = (uint8)-1;
  6106. uint32 section_size;
  6107. bh_assert(!*p_section_list);
  6108. p += 8;
  6109. while (p < p_end) {
  6110. CHECK_BUF(p, p_end, 1);
  6111. section_type = read_uint8(p);
  6112. section_index = get_section_index(section_type);
  6113. if (section_index != (uint8)-1) {
  6114. if (section_type != SECTION_TYPE_USER) {
  6115. /* Custom sections may be inserted at any place,
  6116. while other sections must occur at most once
  6117. and in prescribed order. */
  6118. if (last_section_index != (uint8)-1
  6119. && (section_index <= last_section_index)) {
  6120. set_error_buf(error_buf, error_buf_size,
  6121. "unexpected content after last section or "
  6122. "junk after last section");
  6123. return false;
  6124. }
  6125. last_section_index = section_index;
  6126. }
  6127. read_leb_uint32(p, p_end, section_size);
  6128. CHECK_BUF1(p, p_end, section_size);
  6129. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  6130. error_buf_size))) {
  6131. return false;
  6132. }
  6133. section->section_type = section_type;
  6134. section->section_body = (uint8 *)p;
  6135. section->section_body_size = section_size;
  6136. if (!section_list_end)
  6137. *p_section_list = section_list_end = section;
  6138. else {
  6139. section_list_end->next = section;
  6140. section_list_end = section;
  6141. }
  6142. p += section_size;
  6143. }
  6144. else {
  6145. set_error_buf(error_buf, error_buf_size, "invalid section id");
  6146. return false;
  6147. }
  6148. }
  6149. return true;
  6150. fail:
  6151. return false;
  6152. }
  6153. static void
  6154. exchange32(uint8 *p_data)
  6155. {
  6156. uint8 value = *p_data;
  6157. *p_data = *(p_data + 3);
  6158. *(p_data + 3) = value;
  6159. value = *(p_data + 1);
  6160. *(p_data + 1) = *(p_data + 2);
  6161. *(p_data + 2) = value;
  6162. }
  6163. static union {
  6164. int a;
  6165. char b;
  6166. } __ue = { .a = 1 };
  6167. #define is_little_endian() (__ue.b == 1)
  6168. static bool
  6169. load(const uint8 *buf, uint32 size, WASMModule *module,
  6170. bool wasm_binary_freeable, bool no_resolve, char *error_buf,
  6171. uint32 error_buf_size)
  6172. {
  6173. const uint8 *buf_end = buf + size;
  6174. const uint8 *p = buf, *p_end = buf_end;
  6175. uint32 magic_number, version;
  6176. WASMSection *section_list = NULL;
  6177. CHECK_BUF1(p, p_end, sizeof(uint32));
  6178. magic_number = read_uint32(p);
  6179. if (!is_little_endian())
  6180. exchange32((uint8 *)&magic_number);
  6181. if (magic_number != WASM_MAGIC_NUMBER) {
  6182. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  6183. return false;
  6184. }
  6185. CHECK_BUF1(p, p_end, sizeof(uint32));
  6186. version = read_uint32(p);
  6187. if (!is_little_endian())
  6188. exchange32((uint8 *)&version);
  6189. if (version != WASM_CURRENT_VERSION) {
  6190. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  6191. return false;
  6192. }
  6193. module->package_version = version;
  6194. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  6195. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  6196. no_resolve, error_buf, error_buf_size)) {
  6197. destroy_sections(section_list);
  6198. return false;
  6199. }
  6200. destroy_sections(section_list);
  6201. return true;
  6202. fail:
  6203. return false;
  6204. }
  6205. #if WASM_ENABLE_LIBC_WASI != 0
  6206. /**
  6207. * refer to
  6208. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  6209. */
  6210. static bool
  6211. check_wasi_abi_compatibility(const WASMModule *module,
  6212. #if WASM_ENABLE_MULTI_MODULE != 0
  6213. bool main_module,
  6214. #endif
  6215. char *error_buf, uint32 error_buf_size)
  6216. {
  6217. /**
  6218. * be careful with:
  6219. * wasi compatible modules(command/reactor) which don't import any wasi
  6220. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  6221. * reactor can depend on nothing. At the same time, each has its own entry
  6222. * point.
  6223. *
  6224. * observations:
  6225. * - clang always injects `_start` into a command
  6226. * - clang always injects `_initialize` into a reactor
  6227. * - `iwasm -f` allows to run a function in the reactor
  6228. *
  6229. * strong assumptions:
  6230. * - no one will define either `_start` or `_initialize` on purpose
  6231. * - `_start` should always be `void _start(void)`
  6232. * - `_initialize` should always be `void _initialize(void)`
  6233. *
  6234. */
  6235. /* clang-format off */
  6236. /**
  6237. *
  6238. * | | import_wasi_api True | | import_wasi_api False | |
  6239. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  6240. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  6241. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  6242. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  6243. */
  6244. /* clang-format on */
  6245. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  6246. uint32 import_function_count = module->import_function_count;
  6247. WASMFuncType *func_type;
  6248. /* (func (export "_start") (...) */
  6249. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  6250. error_buf, error_buf_size);
  6251. if (start) {
  6252. if (start->index < import_function_count) {
  6253. set_error_buf(
  6254. error_buf, error_buf_size,
  6255. "the builtin _start function can not be an import function");
  6256. return false;
  6257. }
  6258. func_type =
  6259. module->functions[start->index - import_function_count]->func_type;
  6260. if (func_type->param_count || func_type->result_count) {
  6261. set_error_buf(error_buf, error_buf_size,
  6262. "the signature of builtin _start function is wrong");
  6263. return false;
  6264. }
  6265. }
  6266. else {
  6267. /* (func (export "_initialize") (...) */
  6268. initialize =
  6269. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  6270. error_buf, error_buf_size);
  6271. if (initialize) {
  6272. if (initialize->index < import_function_count) {
  6273. set_error_buf(error_buf, error_buf_size,
  6274. "the builtin _initialize function can not be an "
  6275. "import function");
  6276. return false;
  6277. }
  6278. func_type =
  6279. module->functions[initialize->index - import_function_count]
  6280. ->func_type;
  6281. if (func_type->param_count || func_type->result_count) {
  6282. set_error_buf(
  6283. error_buf, error_buf_size,
  6284. "the signature of builtin _initialize function is wrong");
  6285. return false;
  6286. }
  6287. }
  6288. }
  6289. /* filter out non-wasi compatible modules */
  6290. if (!module->import_wasi_api && !start && !initialize) {
  6291. return true;
  6292. }
  6293. /* should have one at least */
  6294. if (module->import_wasi_api && !start && !initialize) {
  6295. LOG_WARNING("warning: a module with WASI apis should be either "
  6296. "a command or a reactor");
  6297. }
  6298. /*
  6299. * there is at least one of `_start` and `_initialize` in below cases.
  6300. * according to the assumption, they should be all wasi compatible
  6301. */
  6302. #if WASM_ENABLE_MULTI_MODULE != 0
  6303. /* filter out commands (with `_start`) cases */
  6304. if (start && !main_module) {
  6305. set_error_buf(
  6306. error_buf, error_buf_size,
  6307. "a command (with _start function) can not be a sub-module");
  6308. return false;
  6309. }
  6310. #endif
  6311. /*
  6312. * it is ok a reactor acts as a main module,
  6313. * so skip the check about (with `_initialize`)
  6314. */
  6315. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  6316. error_buf, error_buf_size);
  6317. if (!memory
  6318. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  6319. /*
  6320. * with wasi-threads, it's still an open question if a memory
  6321. * should be exported.
  6322. *
  6323. * https://github.com/WebAssembly/wasi-threads/issues/22
  6324. * https://github.com/WebAssembly/WASI/issues/502
  6325. *
  6326. * Note: this code assumes the number of memories is at most 1.
  6327. */
  6328. && module->import_memory_count == 0
  6329. #endif
  6330. ) {
  6331. set_error_buf(error_buf, error_buf_size,
  6332. "a module with WASI apis must export memory by default");
  6333. return false;
  6334. }
  6335. return true;
  6336. }
  6337. #endif
  6338. WASMModule *
  6339. wasm_loader_load(uint8 *buf, uint32 size,
  6340. #if WASM_ENABLE_MULTI_MODULE != 0
  6341. bool main_module,
  6342. #endif
  6343. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  6344. {
  6345. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  6346. if (!module) {
  6347. return NULL;
  6348. }
  6349. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  6350. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  6351. module->load_addr = (uint8 *)buf;
  6352. module->load_size = size;
  6353. #endif
  6354. if (!load(buf, size, module, args->wasm_binary_freeable, args->no_resolve,
  6355. error_buf, error_buf_size)) {
  6356. goto fail;
  6357. }
  6358. #if WASM_ENABLE_LIBC_WASI != 0
  6359. /* Check the WASI application ABI */
  6360. if (!check_wasi_abi_compatibility(module,
  6361. #if WASM_ENABLE_MULTI_MODULE != 0
  6362. main_module,
  6363. #endif
  6364. error_buf, error_buf_size)) {
  6365. goto fail;
  6366. }
  6367. #endif
  6368. LOG_VERBOSE("Load module success.\n");
  6369. return module;
  6370. fail:
  6371. wasm_loader_unload(module);
  6372. return NULL;
  6373. }
  6374. void
  6375. wasm_loader_unload(WASMModule *module)
  6376. {
  6377. uint32 i;
  6378. if (!module)
  6379. return;
  6380. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6381. && WASM_ENABLE_LAZY_JIT != 0
  6382. module->orcjit_stop_compiling = true;
  6383. if (module->llvm_jit_init_thread)
  6384. os_thread_join(module->llvm_jit_init_thread, NULL);
  6385. #endif
  6386. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6387. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6388. module internal data after they were freed */
  6389. orcjit_stop_compile_threads(module);
  6390. #endif
  6391. #if WASM_ENABLE_JIT != 0
  6392. if (module->func_ptrs)
  6393. wasm_runtime_free(module->func_ptrs);
  6394. if (module->comp_ctx)
  6395. aot_destroy_comp_context(module->comp_ctx);
  6396. if (module->comp_data)
  6397. aot_destroy_comp_data(module->comp_data);
  6398. #endif
  6399. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6400. && WASM_ENABLE_LAZY_JIT != 0
  6401. if (module->tierup_wait_lock_inited) {
  6402. os_mutex_destroy(&module->tierup_wait_lock);
  6403. os_cond_destroy(&module->tierup_wait_cond);
  6404. }
  6405. #endif
  6406. if (module->imports)
  6407. wasm_runtime_free(module->imports);
  6408. if (module->functions) {
  6409. for (i = 0; i < module->function_count; i++) {
  6410. if (module->functions[i]) {
  6411. if (module->functions[i]->local_offsets)
  6412. wasm_runtime_free(module->functions[i]->local_offsets);
  6413. #if WASM_ENABLE_FAST_INTERP != 0
  6414. if (module->functions[i]->code_compiled)
  6415. wasm_runtime_free(module->functions[i]->code_compiled);
  6416. if (module->functions[i]->consts)
  6417. wasm_runtime_free(module->functions[i]->consts);
  6418. #endif
  6419. #if WASM_ENABLE_FAST_JIT != 0
  6420. if (module->functions[i]->fast_jit_jitted_code) {
  6421. jit_code_cache_free(
  6422. module->functions[i]->fast_jit_jitted_code);
  6423. }
  6424. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6425. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6426. jit_code_cache_free(
  6427. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6428. }
  6429. #endif
  6430. #endif
  6431. #if WASM_ENABLE_GC != 0
  6432. if (module->functions[i]->local_ref_type_maps) {
  6433. wasm_runtime_free(
  6434. module->functions[i]->local_ref_type_maps);
  6435. }
  6436. #endif
  6437. wasm_runtime_free(module->functions[i]);
  6438. }
  6439. }
  6440. wasm_runtime_free(module->functions);
  6441. }
  6442. if (module->tables) {
  6443. #if WASM_ENABLE_GC != 0
  6444. for (i = 0; i < module->table_count; i++) {
  6445. destroy_init_expr(module, &module->tables[i].init_expr);
  6446. }
  6447. #endif
  6448. wasm_runtime_free(module->tables);
  6449. }
  6450. if (module->memories)
  6451. wasm_runtime_free(module->memories);
  6452. if (module->globals) {
  6453. #if WASM_ENABLE_GC != 0 || WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  6454. for (i = 0; i < module->global_count; i++) {
  6455. destroy_init_expr(module, &module->globals[i].init_expr);
  6456. }
  6457. #endif
  6458. wasm_runtime_free(module->globals);
  6459. }
  6460. #if WASM_ENABLE_TAGS != 0
  6461. if (module->tags) {
  6462. for (i = 0; i < module->tag_count; i++) {
  6463. if (module->tags[i])
  6464. wasm_runtime_free(module->tags[i]);
  6465. }
  6466. wasm_runtime_free(module->tags);
  6467. }
  6468. #endif
  6469. if (module->exports)
  6470. wasm_runtime_free(module->exports);
  6471. if (module->table_segments) {
  6472. for (i = 0; i < module->table_seg_count; i++) {
  6473. if (module->table_segments[i].init_values) {
  6474. #if WASM_ENABLE_GC != 0
  6475. uint32 j;
  6476. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6477. destroy_init_expr(
  6478. module, &module->table_segments[i].init_values[j]);
  6479. }
  6480. #endif
  6481. wasm_runtime_free(module->table_segments[i].init_values);
  6482. }
  6483. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  6484. destroy_init_expr(module, &module->table_segments[i].base_offset);
  6485. #endif
  6486. }
  6487. wasm_runtime_free(module->table_segments);
  6488. }
  6489. if (module->data_segments) {
  6490. for (i = 0; i < module->data_seg_count; i++) {
  6491. if (module->data_segments[i]) {
  6492. if (module->data_segments[i]->is_data_cloned)
  6493. wasm_runtime_free(module->data_segments[i]->data);
  6494. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  6495. destroy_init_expr(module,
  6496. &(module->data_segments[i]->base_offset));
  6497. #endif
  6498. wasm_runtime_free(module->data_segments[i]);
  6499. }
  6500. }
  6501. wasm_runtime_free(module->data_segments);
  6502. }
  6503. if (module->types) {
  6504. for (i = 0; i < module->type_count; i++) {
  6505. if (module->types[i])
  6506. destroy_wasm_type(module->types[i]);
  6507. }
  6508. wasm_runtime_free(module->types);
  6509. }
  6510. if (module->const_str_list) {
  6511. StringNode *node = module->const_str_list, *node_next;
  6512. while (node) {
  6513. node_next = node->next;
  6514. wasm_runtime_free(node);
  6515. node = node_next;
  6516. }
  6517. }
  6518. #if WASM_ENABLE_STRINGREF != 0
  6519. if (module->string_literal_ptrs) {
  6520. wasm_runtime_free((void *)module->string_literal_ptrs);
  6521. }
  6522. if (module->string_literal_lengths) {
  6523. wasm_runtime_free(module->string_literal_lengths);
  6524. }
  6525. #endif
  6526. #if WASM_ENABLE_FAST_INTERP == 0
  6527. if (module->br_table_cache_list) {
  6528. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6529. BrTableCache *node_next;
  6530. while (node) {
  6531. node_next = bh_list_elem_next(node);
  6532. wasm_runtime_free(node);
  6533. node = node_next;
  6534. }
  6535. }
  6536. #endif
  6537. #if WASM_ENABLE_MULTI_MODULE != 0
  6538. /* just release the sub module list */
  6539. if (module->import_module_list) {
  6540. WASMRegisteredModule *node =
  6541. bh_list_first_elem(module->import_module_list);
  6542. while (node) {
  6543. WASMRegisteredModule *next = bh_list_elem_next(node);
  6544. bh_list_remove(module->import_module_list, node);
  6545. /*
  6546. * unload(sub_module) will be triggered during runtime_destroy().
  6547. * every module in the global module list will be unloaded one by
  6548. * one. so don't worry.
  6549. */
  6550. wasm_runtime_free(node);
  6551. /*
  6552. * the module file reading buffer will be released
  6553. * in runtime_destroy()
  6554. */
  6555. node = next;
  6556. }
  6557. }
  6558. #endif
  6559. #if WASM_ENABLE_DEBUG_INTERP != 0
  6560. WASMFastOPCodeNode *fast_opcode =
  6561. bh_list_first_elem(&module->fast_opcode_list);
  6562. while (fast_opcode) {
  6563. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6564. wasm_runtime_free(fast_opcode);
  6565. fast_opcode = next;
  6566. }
  6567. #endif
  6568. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6569. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6570. && WASM_ENABLE_LAZY_JIT != 0)
  6571. os_mutex_destroy(&module->instance_list_lock);
  6572. #endif
  6573. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6574. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6575. #endif
  6576. #if WASM_ENABLE_FAST_JIT != 0
  6577. if (module->fast_jit_func_ptrs) {
  6578. wasm_runtime_free(module->fast_jit_func_ptrs);
  6579. }
  6580. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6581. if (module->fast_jit_thread_locks_inited[i]) {
  6582. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6583. }
  6584. }
  6585. #endif
  6586. #if WASM_ENABLE_GC != 0
  6587. os_mutex_destroy(&module->rtt_type_lock);
  6588. bh_hash_map_destroy(module->ref_type_set);
  6589. if (module->rtt_types) {
  6590. for (i = 0; i < module->type_count; i++) {
  6591. if (module->rtt_types[i])
  6592. wasm_runtime_free(module->rtt_types[i]);
  6593. }
  6594. wasm_runtime_free(module->rtt_types);
  6595. }
  6596. #if WASM_ENABLE_STRINGREF != 0
  6597. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6598. if (module->stringref_rtts[i])
  6599. wasm_runtime_free(module->stringref_rtts[i]);
  6600. }
  6601. #endif
  6602. #endif
  6603. wasm_runtime_free(module);
  6604. }
  6605. bool
  6606. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6607. const uint8 *start_addr, const uint8 *code_end_addr,
  6608. uint8 label_type, uint8 **p_else_addr,
  6609. uint8 **p_end_addr)
  6610. {
  6611. const uint8 *p = start_addr, *p_end = code_end_addr;
  6612. uint8 *else_addr = NULL;
  6613. char error_buf[128];
  6614. uint32 block_nested_depth = 1, count, i, j, t;
  6615. uint32 error_buf_size = sizeof(error_buf);
  6616. uint8 opcode, u8;
  6617. BlockAddr block_stack[16] = { { 0 } }, *block;
  6618. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6619. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6620. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6621. if (block[j].start_addr == start_addr) {
  6622. /* Cache hit */
  6623. *p_else_addr = block[j].else_addr;
  6624. *p_end_addr = block[j].end_addr;
  6625. return true;
  6626. }
  6627. }
  6628. /* Cache unhit */
  6629. block_stack[0].start_addr = start_addr;
  6630. while (p < code_end_addr) {
  6631. opcode = *p++;
  6632. #if WASM_ENABLE_DEBUG_INTERP != 0
  6633. op_break_retry:
  6634. #endif
  6635. switch (opcode) {
  6636. case WASM_OP_UNREACHABLE:
  6637. case WASM_OP_NOP:
  6638. break;
  6639. #if WASM_ENABLE_EXCE_HANDLING != 0
  6640. case WASM_OP_TRY:
  6641. u8 = read_uint8(p);
  6642. if (block_nested_depth
  6643. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6644. block_stack[block_nested_depth].start_addr = p;
  6645. block_stack[block_nested_depth].else_addr = NULL;
  6646. }
  6647. block_nested_depth++;
  6648. break;
  6649. case EXT_OP_TRY:
  6650. skip_leb_uint32(p, p_end);
  6651. if (block_nested_depth
  6652. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6653. block_stack[block_nested_depth].start_addr = p;
  6654. block_stack[block_nested_depth].else_addr = NULL;
  6655. }
  6656. block_nested_depth++;
  6657. break;
  6658. case WASM_OP_CATCH:
  6659. if (block_nested_depth == 1) {
  6660. *p_end_addr = (uint8 *)(p - 1);
  6661. /* stop search and return the address of the catch block */
  6662. return true;
  6663. }
  6664. break;
  6665. case WASM_OP_CATCH_ALL:
  6666. if (block_nested_depth == 1) {
  6667. *p_end_addr = (uint8 *)(p - 1);
  6668. /* stop search and return the address of the catch_all block
  6669. */
  6670. return true;
  6671. }
  6672. break;
  6673. case WASM_OP_THROW:
  6674. /* skip tag_index */
  6675. skip_leb(p);
  6676. break;
  6677. case WASM_OP_RETHROW:
  6678. /* skip depth */
  6679. skip_leb(p);
  6680. break;
  6681. case WASM_OP_DELEGATE:
  6682. if (block_nested_depth == 1) {
  6683. *p_end_addr = (uint8 *)(p - 1);
  6684. return true;
  6685. }
  6686. else {
  6687. skip_leb(p);
  6688. /* the DELEGATE opcode ends the tryblock, */
  6689. block_nested_depth--;
  6690. if (block_nested_depth
  6691. < sizeof(block_stack) / sizeof(BlockAddr))
  6692. block_stack[block_nested_depth].end_addr =
  6693. (uint8 *)(p - 1);
  6694. }
  6695. break;
  6696. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6697. case WASM_OP_BLOCK:
  6698. case WASM_OP_LOOP:
  6699. case WASM_OP_IF:
  6700. {
  6701. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6702. u8 = read_uint8(p);
  6703. if (is_byte_a_type(u8)) {
  6704. #if WASM_ENABLE_GC != 0
  6705. if (wasm_is_type_multi_byte_type(u8)) {
  6706. /* the possible extra bytes of GC ref type have been
  6707. modified to OP_NOP, no need to resolve them again */
  6708. }
  6709. #endif
  6710. }
  6711. else {
  6712. p--;
  6713. /* block type */
  6714. skip_leb_int32(p, p_end);
  6715. }
  6716. if (block_nested_depth
  6717. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6718. block_stack[block_nested_depth].start_addr = p;
  6719. block_stack[block_nested_depth].else_addr = NULL;
  6720. }
  6721. block_nested_depth++;
  6722. break;
  6723. }
  6724. case EXT_OP_BLOCK:
  6725. case EXT_OP_LOOP:
  6726. case EXT_OP_IF:
  6727. /* block type */
  6728. skip_leb_int32(p, p_end);
  6729. if (block_nested_depth
  6730. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6731. block_stack[block_nested_depth].start_addr = p;
  6732. block_stack[block_nested_depth].else_addr = NULL;
  6733. }
  6734. block_nested_depth++;
  6735. break;
  6736. case WASM_OP_ELSE:
  6737. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6738. else_addr = (uint8 *)(p - 1);
  6739. if (block_nested_depth - 1
  6740. < sizeof(block_stack) / sizeof(BlockAddr))
  6741. block_stack[block_nested_depth - 1].else_addr =
  6742. (uint8 *)(p - 1);
  6743. break;
  6744. case WASM_OP_END:
  6745. if (block_nested_depth == 1) {
  6746. if (label_type == LABEL_TYPE_IF)
  6747. *p_else_addr = else_addr;
  6748. *p_end_addr = (uint8 *)(p - 1);
  6749. block_stack[0].end_addr = (uint8 *)(p - 1);
  6750. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6751. t++) {
  6752. start_addr = block_stack[t].start_addr;
  6753. if (start_addr) {
  6754. i = ((uintptr_t)start_addr)
  6755. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6756. block =
  6757. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6758. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6759. if (!block[j].start_addr)
  6760. break;
  6761. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6762. memmove(block + 1, block,
  6763. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6764. * sizeof(BlockAddr));
  6765. j = 0;
  6766. }
  6767. block[j].start_addr = block_stack[t].start_addr;
  6768. block[j].else_addr = block_stack[t].else_addr;
  6769. block[j].end_addr = block_stack[t].end_addr;
  6770. }
  6771. else
  6772. break;
  6773. }
  6774. return true;
  6775. }
  6776. else {
  6777. block_nested_depth--;
  6778. if (block_nested_depth
  6779. < sizeof(block_stack) / sizeof(BlockAddr))
  6780. block_stack[block_nested_depth].end_addr =
  6781. (uint8 *)(p - 1);
  6782. }
  6783. break;
  6784. case WASM_OP_BR:
  6785. case WASM_OP_BR_IF:
  6786. skip_leb_uint32(p, p_end); /* labelidx */
  6787. break;
  6788. case WASM_OP_BR_TABLE:
  6789. read_leb_uint32(p, p_end, count); /* label num */
  6790. #if WASM_ENABLE_FAST_INTERP != 0
  6791. for (i = 0; i <= count; i++) /* labelidxs */
  6792. skip_leb_uint32(p, p_end);
  6793. #else
  6794. p += count + 1;
  6795. while (*p == WASM_OP_NOP)
  6796. p++;
  6797. #endif
  6798. break;
  6799. #if WASM_ENABLE_FAST_INTERP == 0
  6800. case EXT_OP_BR_TABLE_CACHE:
  6801. read_leb_uint32(p, p_end, count); /* label num */
  6802. while (*p == WASM_OP_NOP)
  6803. p++;
  6804. break;
  6805. #endif
  6806. case WASM_OP_RETURN:
  6807. break;
  6808. case WASM_OP_CALL:
  6809. #if WASM_ENABLE_TAIL_CALL != 0
  6810. case WASM_OP_RETURN_CALL:
  6811. #endif
  6812. skip_leb_uint32(p, p_end); /* funcidx */
  6813. break;
  6814. case WASM_OP_CALL_INDIRECT:
  6815. #if WASM_ENABLE_TAIL_CALL != 0
  6816. case WASM_OP_RETURN_CALL_INDIRECT:
  6817. #endif
  6818. skip_leb_uint32(p, p_end); /* typeidx */
  6819. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6820. skip_leb_uint32(p, p_end); /* tableidx */
  6821. #else
  6822. u8 = read_uint8(p); /* 0x00 */
  6823. #endif
  6824. break;
  6825. #if WASM_ENABLE_GC != 0
  6826. case WASM_OP_CALL_REF:
  6827. case WASM_OP_RETURN_CALL_REF:
  6828. skip_leb_uint32(p, p_end); /* typeidx */
  6829. break;
  6830. #endif
  6831. case WASM_OP_DROP:
  6832. case WASM_OP_SELECT:
  6833. case WASM_OP_DROP_64:
  6834. case WASM_OP_SELECT_64:
  6835. #if WASM_ENABLE_SIMDE != 0
  6836. case WASM_OP_SELECT_128:
  6837. #endif
  6838. break;
  6839. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6840. case WASM_OP_SELECT_T:
  6841. {
  6842. skip_leb_uint32(p, p_end); /* vec length */
  6843. u8 = read_uint8(p); /* typeidx */
  6844. /* the possible extra bytes of GC ref type have been
  6845. modified to OP_NOP, no need to resolve them again */
  6846. break;
  6847. }
  6848. case WASM_OP_TABLE_GET:
  6849. case WASM_OP_TABLE_SET:
  6850. skip_leb_uint32(p, p_end); /* table index */
  6851. break;
  6852. case WASM_OP_REF_NULL:
  6853. {
  6854. u8 = read_uint8(p); /* type */
  6855. if (is_byte_a_type(u8)) {
  6856. #if WASM_ENABLE_GC != 0
  6857. if (wasm_is_type_multi_byte_type(u8)) {
  6858. /* the possible extra bytes of GC ref type have been
  6859. modified to OP_NOP, no need to resolve them again */
  6860. }
  6861. #endif
  6862. }
  6863. else {
  6864. p--;
  6865. skip_leb_uint32(p, p_end);
  6866. }
  6867. break;
  6868. }
  6869. case WASM_OP_REF_IS_NULL:
  6870. break;
  6871. case WASM_OP_REF_FUNC:
  6872. skip_leb_uint32(p, p_end); /* func index */
  6873. break;
  6874. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6875. #if WASM_ENABLE_GC != 0
  6876. case WASM_OP_REF_AS_NON_NULL:
  6877. case WASM_OP_REF_EQ:
  6878. break;
  6879. case WASM_OP_BR_ON_NULL:
  6880. case WASM_OP_BR_ON_NON_NULL:
  6881. skip_leb_uint32(p, p_end); /* label index */
  6882. break;
  6883. #endif /* end of WASM_ENABLE_GC != 0 */
  6884. case WASM_OP_GET_LOCAL:
  6885. case WASM_OP_SET_LOCAL:
  6886. case WASM_OP_TEE_LOCAL:
  6887. case WASM_OP_GET_GLOBAL:
  6888. case WASM_OP_SET_GLOBAL:
  6889. case WASM_OP_GET_GLOBAL_64:
  6890. case WASM_OP_SET_GLOBAL_64:
  6891. #if WASM_ENABLE_SIMDE != 0
  6892. case WASM_OP_GET_GLOBAL_V128:
  6893. case WASM_OP_SET_GLOBAL_V128:
  6894. #endif
  6895. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6896. skip_leb_uint32(p, p_end); /* local index */
  6897. break;
  6898. case EXT_OP_GET_LOCAL_FAST:
  6899. case EXT_OP_SET_LOCAL_FAST:
  6900. case EXT_OP_TEE_LOCAL_FAST:
  6901. CHECK_BUF(p, p_end, 1);
  6902. p++;
  6903. break;
  6904. case WASM_OP_I32_LOAD:
  6905. case WASM_OP_I64_LOAD:
  6906. case WASM_OP_F32_LOAD:
  6907. case WASM_OP_F64_LOAD:
  6908. case WASM_OP_I32_LOAD8_S:
  6909. case WASM_OP_I32_LOAD8_U:
  6910. case WASM_OP_I32_LOAD16_S:
  6911. case WASM_OP_I32_LOAD16_U:
  6912. case WASM_OP_I64_LOAD8_S:
  6913. case WASM_OP_I64_LOAD8_U:
  6914. case WASM_OP_I64_LOAD16_S:
  6915. case WASM_OP_I64_LOAD16_U:
  6916. case WASM_OP_I64_LOAD32_S:
  6917. case WASM_OP_I64_LOAD32_U:
  6918. case WASM_OP_I32_STORE:
  6919. case WASM_OP_I64_STORE:
  6920. case WASM_OP_F32_STORE:
  6921. case WASM_OP_F64_STORE:
  6922. case WASM_OP_I32_STORE8:
  6923. case WASM_OP_I32_STORE16:
  6924. case WASM_OP_I64_STORE8:
  6925. case WASM_OP_I64_STORE16:
  6926. case WASM_OP_I64_STORE32:
  6927. skip_leb_align(p, p_end); /* align */
  6928. skip_leb_mem_offset(p, p_end); /* offset */
  6929. break;
  6930. case WASM_OP_MEMORY_SIZE:
  6931. case WASM_OP_MEMORY_GROW:
  6932. skip_leb_memidx(p, p_end); /* memidx */
  6933. break;
  6934. case WASM_OP_I32_CONST:
  6935. skip_leb_int32(p, p_end);
  6936. break;
  6937. case WASM_OP_I64_CONST:
  6938. skip_leb_int64(p, p_end);
  6939. break;
  6940. case WASM_OP_F32_CONST:
  6941. p += sizeof(float32);
  6942. break;
  6943. case WASM_OP_F64_CONST:
  6944. p += sizeof(float64);
  6945. break;
  6946. case WASM_OP_I32_EQZ:
  6947. case WASM_OP_I32_EQ:
  6948. case WASM_OP_I32_NE:
  6949. case WASM_OP_I32_LT_S:
  6950. case WASM_OP_I32_LT_U:
  6951. case WASM_OP_I32_GT_S:
  6952. case WASM_OP_I32_GT_U:
  6953. case WASM_OP_I32_LE_S:
  6954. case WASM_OP_I32_LE_U:
  6955. case WASM_OP_I32_GE_S:
  6956. case WASM_OP_I32_GE_U:
  6957. case WASM_OP_I64_EQZ:
  6958. case WASM_OP_I64_EQ:
  6959. case WASM_OP_I64_NE:
  6960. case WASM_OP_I64_LT_S:
  6961. case WASM_OP_I64_LT_U:
  6962. case WASM_OP_I64_GT_S:
  6963. case WASM_OP_I64_GT_U:
  6964. case WASM_OP_I64_LE_S:
  6965. case WASM_OP_I64_LE_U:
  6966. case WASM_OP_I64_GE_S:
  6967. case WASM_OP_I64_GE_U:
  6968. case WASM_OP_F32_EQ:
  6969. case WASM_OP_F32_NE:
  6970. case WASM_OP_F32_LT:
  6971. case WASM_OP_F32_GT:
  6972. case WASM_OP_F32_LE:
  6973. case WASM_OP_F32_GE:
  6974. case WASM_OP_F64_EQ:
  6975. case WASM_OP_F64_NE:
  6976. case WASM_OP_F64_LT:
  6977. case WASM_OP_F64_GT:
  6978. case WASM_OP_F64_LE:
  6979. case WASM_OP_F64_GE:
  6980. case WASM_OP_I32_CLZ:
  6981. case WASM_OP_I32_CTZ:
  6982. case WASM_OP_I32_POPCNT:
  6983. case WASM_OP_I32_ADD:
  6984. case WASM_OP_I32_SUB:
  6985. case WASM_OP_I32_MUL:
  6986. case WASM_OP_I32_DIV_S:
  6987. case WASM_OP_I32_DIV_U:
  6988. case WASM_OP_I32_REM_S:
  6989. case WASM_OP_I32_REM_U:
  6990. case WASM_OP_I32_AND:
  6991. case WASM_OP_I32_OR:
  6992. case WASM_OP_I32_XOR:
  6993. case WASM_OP_I32_SHL:
  6994. case WASM_OP_I32_SHR_S:
  6995. case WASM_OP_I32_SHR_U:
  6996. case WASM_OP_I32_ROTL:
  6997. case WASM_OP_I32_ROTR:
  6998. case WASM_OP_I64_CLZ:
  6999. case WASM_OP_I64_CTZ:
  7000. case WASM_OP_I64_POPCNT:
  7001. case WASM_OP_I64_ADD:
  7002. case WASM_OP_I64_SUB:
  7003. case WASM_OP_I64_MUL:
  7004. case WASM_OP_I64_DIV_S:
  7005. case WASM_OP_I64_DIV_U:
  7006. case WASM_OP_I64_REM_S:
  7007. case WASM_OP_I64_REM_U:
  7008. case WASM_OP_I64_AND:
  7009. case WASM_OP_I64_OR:
  7010. case WASM_OP_I64_XOR:
  7011. case WASM_OP_I64_SHL:
  7012. case WASM_OP_I64_SHR_S:
  7013. case WASM_OP_I64_SHR_U:
  7014. case WASM_OP_I64_ROTL:
  7015. case WASM_OP_I64_ROTR:
  7016. case WASM_OP_F32_ABS:
  7017. case WASM_OP_F32_NEG:
  7018. case WASM_OP_F32_CEIL:
  7019. case WASM_OP_F32_FLOOR:
  7020. case WASM_OP_F32_TRUNC:
  7021. case WASM_OP_F32_NEAREST:
  7022. case WASM_OP_F32_SQRT:
  7023. case WASM_OP_F32_ADD:
  7024. case WASM_OP_F32_SUB:
  7025. case WASM_OP_F32_MUL:
  7026. case WASM_OP_F32_DIV:
  7027. case WASM_OP_F32_MIN:
  7028. case WASM_OP_F32_MAX:
  7029. case WASM_OP_F32_COPYSIGN:
  7030. case WASM_OP_F64_ABS:
  7031. case WASM_OP_F64_NEG:
  7032. case WASM_OP_F64_CEIL:
  7033. case WASM_OP_F64_FLOOR:
  7034. case WASM_OP_F64_TRUNC:
  7035. case WASM_OP_F64_NEAREST:
  7036. case WASM_OP_F64_SQRT:
  7037. case WASM_OP_F64_ADD:
  7038. case WASM_OP_F64_SUB:
  7039. case WASM_OP_F64_MUL:
  7040. case WASM_OP_F64_DIV:
  7041. case WASM_OP_F64_MIN:
  7042. case WASM_OP_F64_MAX:
  7043. case WASM_OP_F64_COPYSIGN:
  7044. case WASM_OP_I32_WRAP_I64:
  7045. case WASM_OP_I32_TRUNC_S_F32:
  7046. case WASM_OP_I32_TRUNC_U_F32:
  7047. case WASM_OP_I32_TRUNC_S_F64:
  7048. case WASM_OP_I32_TRUNC_U_F64:
  7049. case WASM_OP_I64_EXTEND_S_I32:
  7050. case WASM_OP_I64_EXTEND_U_I32:
  7051. case WASM_OP_I64_TRUNC_S_F32:
  7052. case WASM_OP_I64_TRUNC_U_F32:
  7053. case WASM_OP_I64_TRUNC_S_F64:
  7054. case WASM_OP_I64_TRUNC_U_F64:
  7055. case WASM_OP_F32_CONVERT_S_I32:
  7056. case WASM_OP_F32_CONVERT_U_I32:
  7057. case WASM_OP_F32_CONVERT_S_I64:
  7058. case WASM_OP_F32_CONVERT_U_I64:
  7059. case WASM_OP_F32_DEMOTE_F64:
  7060. case WASM_OP_F64_CONVERT_S_I32:
  7061. case WASM_OP_F64_CONVERT_U_I32:
  7062. case WASM_OP_F64_CONVERT_S_I64:
  7063. case WASM_OP_F64_CONVERT_U_I64:
  7064. case WASM_OP_F64_PROMOTE_F32:
  7065. case WASM_OP_I32_REINTERPRET_F32:
  7066. case WASM_OP_I64_REINTERPRET_F64:
  7067. case WASM_OP_F32_REINTERPRET_I32:
  7068. case WASM_OP_F64_REINTERPRET_I64:
  7069. case WASM_OP_I32_EXTEND8_S:
  7070. case WASM_OP_I32_EXTEND16_S:
  7071. case WASM_OP_I64_EXTEND8_S:
  7072. case WASM_OP_I64_EXTEND16_S:
  7073. case WASM_OP_I64_EXTEND32_S:
  7074. break;
  7075. #if WASM_ENABLE_GC != 0
  7076. case WASM_OP_GC_PREFIX:
  7077. {
  7078. uint32 opcode1;
  7079. read_leb_uint32(p, p_end, opcode1);
  7080. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7081. is no larger than UINT8_MAX */
  7082. opcode = (uint8)opcode1;
  7083. switch (opcode) {
  7084. case WASM_OP_STRUCT_NEW:
  7085. case WASM_OP_STRUCT_NEW_DEFAULT:
  7086. skip_leb_uint32(p, p_end); /* typeidx */
  7087. break;
  7088. case WASM_OP_STRUCT_GET:
  7089. case WASM_OP_STRUCT_GET_S:
  7090. case WASM_OP_STRUCT_GET_U:
  7091. case WASM_OP_STRUCT_SET:
  7092. skip_leb_uint32(p, p_end); /* typeidx */
  7093. skip_leb_uint32(p, p_end); /* fieldidx */
  7094. break;
  7095. case WASM_OP_ARRAY_NEW:
  7096. case WASM_OP_ARRAY_NEW_DEFAULT:
  7097. case WASM_OP_ARRAY_GET:
  7098. case WASM_OP_ARRAY_GET_S:
  7099. case WASM_OP_ARRAY_GET_U:
  7100. case WASM_OP_ARRAY_SET:
  7101. case WASM_OP_ARRAY_FILL:
  7102. skip_leb_uint32(p, p_end); /* typeidx */
  7103. break;
  7104. case WASM_OP_ARRAY_COPY:
  7105. skip_leb_uint32(p, p_end); /* typeidx1 */
  7106. skip_leb_uint32(p, p_end); /* typeidx2 */
  7107. break;
  7108. case WASM_OP_ARRAY_LEN:
  7109. break;
  7110. case WASM_OP_ARRAY_NEW_FIXED:
  7111. case WASM_OP_ARRAY_NEW_DATA:
  7112. case WASM_OP_ARRAY_NEW_ELEM:
  7113. skip_leb_uint32(p, p_end); /* typeidx */
  7114. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  7115. break;
  7116. case WASM_OP_REF_I31:
  7117. case WASM_OP_I31_GET_S:
  7118. case WASM_OP_I31_GET_U:
  7119. break;
  7120. case WASM_OP_REF_TEST:
  7121. case WASM_OP_REF_CAST:
  7122. case WASM_OP_REF_TEST_NULLABLE:
  7123. case WASM_OP_REF_CAST_NULLABLE:
  7124. skip_leb_int32(p, p_end); /* heaptype */
  7125. break;
  7126. case WASM_OP_BR_ON_CAST:
  7127. case WASM_OP_BR_ON_CAST_FAIL:
  7128. p += sizeof(uint8); /* castflag */
  7129. skip_leb_uint32(p, p_end); /* labelidx */
  7130. skip_leb_int32(p, p_end); /* heaptype */
  7131. skip_leb_int32(p, p_end); /* heaptype2 */
  7132. break;
  7133. case WASM_OP_ANY_CONVERT_EXTERN:
  7134. case WASM_OP_EXTERN_CONVERT_ANY:
  7135. break;
  7136. #if WASM_ENABLE_STRINGREF != 0
  7137. case WASM_OP_STRING_NEW_UTF8:
  7138. case WASM_OP_STRING_NEW_WTF16:
  7139. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  7140. case WASM_OP_STRING_NEW_WTF8:
  7141. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  7142. break;
  7143. case WASM_OP_STRING_CONST:
  7144. skip_leb_int32(p, p_end); /* contents */
  7145. break;
  7146. case WASM_OP_STRING_MEASURE_UTF8:
  7147. case WASM_OP_STRING_MEASURE_WTF8:
  7148. case WASM_OP_STRING_MEASURE_WTF16:
  7149. break;
  7150. case WASM_OP_STRING_ENCODE_UTF8:
  7151. case WASM_OP_STRING_ENCODE_WTF16:
  7152. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  7153. case WASM_OP_STRING_ENCODE_WTF8:
  7154. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  7155. break;
  7156. case WASM_OP_STRING_CONCAT:
  7157. case WASM_OP_STRING_EQ:
  7158. case WASM_OP_STRING_IS_USV_SEQUENCE:
  7159. case WASM_OP_STRING_AS_WTF8:
  7160. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  7161. break;
  7162. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  7163. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  7164. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  7165. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  7166. break;
  7167. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  7168. case WASM_OP_STRING_AS_WTF16:
  7169. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  7170. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  7171. break;
  7172. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  7173. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  7174. break;
  7175. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  7176. case WASM_OP_STRING_AS_ITER:
  7177. case WASM_OP_STRINGVIEW_ITER_NEXT:
  7178. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  7179. case WASM_OP_STRINGVIEW_ITER_REWIND:
  7180. case WASM_OP_STRINGVIEW_ITER_SLICE:
  7181. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  7182. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  7183. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  7184. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  7185. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  7186. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  7187. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  7188. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  7189. break;
  7190. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  7191. default:
  7192. return false;
  7193. }
  7194. break;
  7195. }
  7196. #endif /* end of WASM_ENABLE_GC != 0 */
  7197. case WASM_OP_MISC_PREFIX:
  7198. {
  7199. uint32 opcode1;
  7200. read_leb_uint32(p, p_end, opcode1);
  7201. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7202. is no larger than UINT8_MAX */
  7203. opcode = (uint8)opcode1;
  7204. switch (opcode) {
  7205. case WASM_OP_I32_TRUNC_SAT_S_F32:
  7206. case WASM_OP_I32_TRUNC_SAT_U_F32:
  7207. case WASM_OP_I32_TRUNC_SAT_S_F64:
  7208. case WASM_OP_I32_TRUNC_SAT_U_F64:
  7209. case WASM_OP_I64_TRUNC_SAT_S_F32:
  7210. case WASM_OP_I64_TRUNC_SAT_U_F32:
  7211. case WASM_OP_I64_TRUNC_SAT_S_F64:
  7212. case WASM_OP_I64_TRUNC_SAT_U_F64:
  7213. break;
  7214. #if WASM_ENABLE_BULK_MEMORY != 0
  7215. case WASM_OP_MEMORY_INIT:
  7216. skip_leb_uint32(p, p_end);
  7217. skip_leb_memidx(p, p_end);
  7218. break;
  7219. case WASM_OP_DATA_DROP:
  7220. skip_leb_uint32(p, p_end);
  7221. break;
  7222. case WASM_OP_MEMORY_COPY:
  7223. skip_leb_memidx(p, p_end);
  7224. skip_leb_memidx(p, p_end);
  7225. break;
  7226. case WASM_OP_MEMORY_FILL:
  7227. skip_leb_memidx(p, p_end);
  7228. break;
  7229. #endif /* WASM_ENABLE_BULK_MEMORY */
  7230. #if WASM_ENABLE_REF_TYPES != 0
  7231. case WASM_OP_TABLE_INIT:
  7232. case WASM_OP_TABLE_COPY:
  7233. /* tableidx */
  7234. skip_leb_uint32(p, p_end);
  7235. /* elemidx */
  7236. skip_leb_uint32(p, p_end);
  7237. break;
  7238. case WASM_OP_ELEM_DROP:
  7239. /* elemidx */
  7240. skip_leb_uint32(p, p_end);
  7241. break;
  7242. case WASM_OP_TABLE_SIZE:
  7243. case WASM_OP_TABLE_GROW:
  7244. case WASM_OP_TABLE_FILL:
  7245. skip_leb_uint32(p, p_end); /* table idx */
  7246. break;
  7247. #endif /* WASM_ENABLE_REF_TYPES */
  7248. default:
  7249. return false;
  7250. }
  7251. break;
  7252. }
  7253. #if WASM_ENABLE_SIMD != 0
  7254. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  7255. || (WASM_ENABLE_FAST_INTERP != 0)
  7256. case WASM_OP_SIMD_PREFIX:
  7257. {
  7258. uint32 opcode1;
  7259. read_leb_uint32(p, p_end, opcode1);
  7260. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7261. is no larger than UINT8_MAX */
  7262. opcode = (uint8)opcode1;
  7263. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  7264. */
  7265. switch (opcode) {
  7266. case SIMD_v128_load:
  7267. case SIMD_v128_load8x8_s:
  7268. case SIMD_v128_load8x8_u:
  7269. case SIMD_v128_load16x4_s:
  7270. case SIMD_v128_load16x4_u:
  7271. case SIMD_v128_load32x2_s:
  7272. case SIMD_v128_load32x2_u:
  7273. case SIMD_v128_load8_splat:
  7274. case SIMD_v128_load16_splat:
  7275. case SIMD_v128_load32_splat:
  7276. case SIMD_v128_load64_splat:
  7277. case SIMD_v128_store:
  7278. /* memarg align */
  7279. skip_leb_uint32(p, p_end);
  7280. /* memarg offset */
  7281. skip_leb_mem_offset(p, p_end);
  7282. break;
  7283. case SIMD_v128_const:
  7284. case SIMD_v8x16_shuffle:
  7285. /* immByte[16] immLaneId[16] */
  7286. CHECK_BUF1(p, p_end, 16);
  7287. p += 16;
  7288. break;
  7289. case SIMD_i8x16_extract_lane_s:
  7290. case SIMD_i8x16_extract_lane_u:
  7291. case SIMD_i8x16_replace_lane:
  7292. case SIMD_i16x8_extract_lane_s:
  7293. case SIMD_i16x8_extract_lane_u:
  7294. case SIMD_i16x8_replace_lane:
  7295. case SIMD_i32x4_extract_lane:
  7296. case SIMD_i32x4_replace_lane:
  7297. case SIMD_i64x2_extract_lane:
  7298. case SIMD_i64x2_replace_lane:
  7299. case SIMD_f32x4_extract_lane:
  7300. case SIMD_f32x4_replace_lane:
  7301. case SIMD_f64x2_extract_lane:
  7302. case SIMD_f64x2_replace_lane:
  7303. /* ImmLaneId */
  7304. CHECK_BUF(p, p_end, 1);
  7305. p++;
  7306. break;
  7307. case SIMD_v128_load8_lane:
  7308. case SIMD_v128_load16_lane:
  7309. case SIMD_v128_load32_lane:
  7310. case SIMD_v128_load64_lane:
  7311. case SIMD_v128_store8_lane:
  7312. case SIMD_v128_store16_lane:
  7313. case SIMD_v128_store32_lane:
  7314. case SIMD_v128_store64_lane:
  7315. /* memarg align */
  7316. skip_leb_uint32(p, p_end);
  7317. /* memarg offset */
  7318. skip_leb_mem_offset(p, p_end);
  7319. /* ImmLaneId */
  7320. CHECK_BUF(p, p_end, 1);
  7321. p++;
  7322. break;
  7323. case SIMD_v128_load32_zero:
  7324. case SIMD_v128_load64_zero:
  7325. /* memarg align */
  7326. skip_leb_uint32(p, p_end);
  7327. /* memarg offset */
  7328. skip_leb_mem_offset(p, p_end);
  7329. break;
  7330. default:
  7331. /*
  7332. * since latest SIMD specific used almost every value
  7333. * from 0x00 to 0xff, the default branch will present
  7334. * all opcodes without imm
  7335. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  7336. */
  7337. break;
  7338. }
  7339. break;
  7340. }
  7341. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  7342. (WASM_ENABLE_FAST_INTERP != 0) */
  7343. #endif /* end of WASM_ENABLE_SIMD */
  7344. #if WASM_ENABLE_SHARED_MEMORY != 0
  7345. case WASM_OP_ATOMIC_PREFIX:
  7346. {
  7347. uint32 opcode1;
  7348. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  7349. read_leb_uint32(p, p_end, opcode1);
  7350. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7351. is no larger than UINT8_MAX */
  7352. opcode = (uint8)opcode1;
  7353. if (opcode != WASM_OP_ATOMIC_FENCE) {
  7354. skip_leb_uint32(p, p_end); /* align */
  7355. skip_leb_mem_offset(p, p_end); /* offset */
  7356. }
  7357. else {
  7358. /* atomic.fence doesn't have memarg */
  7359. p++;
  7360. }
  7361. break;
  7362. }
  7363. #endif
  7364. #if WASM_ENABLE_DEBUG_INTERP != 0
  7365. case DEBUG_OP_BREAK:
  7366. {
  7367. WASMDebugInstance *debug_instance =
  7368. wasm_exec_env_get_instance(exec_env);
  7369. char original_opcode[1];
  7370. uint64 size = 1;
  7371. WASMModuleInstance *module_inst =
  7372. (WASMModuleInstance *)exec_env->module_inst;
  7373. uint64 offset = (p - 1) >= module_inst->module->load_addr
  7374. ? (p - 1) - module_inst->module->load_addr
  7375. : ~0;
  7376. if (debug_instance) {
  7377. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  7378. original_opcode, &size)
  7379. && size == 1) {
  7380. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  7381. "with %02x: ",
  7382. original_opcode[0]);
  7383. opcode = original_opcode[0];
  7384. goto op_break_retry;
  7385. }
  7386. }
  7387. break;
  7388. }
  7389. #endif
  7390. default:
  7391. return false;
  7392. }
  7393. }
  7394. (void)u8;
  7395. (void)exec_env;
  7396. return false;
  7397. fail:
  7398. return false;
  7399. }
  7400. #if WASM_ENABLE_FAST_INTERP != 0
  7401. #if WASM_DEBUG_PREPROCESSOR != 0
  7402. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7403. #else
  7404. #define LOG_OP(...) (void)0
  7405. #endif
  7406. #define PATCH_ELSE 0
  7407. #define PATCH_END 1
  7408. typedef struct BranchBlockPatch {
  7409. struct BranchBlockPatch *next;
  7410. uint8 patch_type;
  7411. uint8 *code_compiled;
  7412. } BranchBlockPatch;
  7413. #endif
  7414. typedef struct BranchBlock {
  7415. uint8 label_type;
  7416. BlockType block_type;
  7417. uint8 *start_addr;
  7418. uint8 *else_addr;
  7419. uint8 *end_addr;
  7420. uint32 stack_cell_num;
  7421. #if WASM_ENABLE_GC != 0
  7422. uint32 reftype_map_num;
  7423. /* Indicate which local is used inside current block, used to validate
  7424. * local.get with non-nullable ref types */
  7425. uint8 *local_use_mask;
  7426. uint32 local_use_mask_size;
  7427. #endif
  7428. #if WASM_ENABLE_FAST_INTERP != 0
  7429. uint16 dynamic_offset;
  7430. uint8 *code_compiled;
  7431. BranchBlockPatch *patch_list;
  7432. /* This is used to save params frame_offset of of if block */
  7433. int16 *param_frame_offsets;
  7434. /* This is used to recover the dynamic offset for else branch,
  7435. * and also to remember the start offset of dynamic space which
  7436. * stores the block arguments for loop block, so we can use it
  7437. * to copy the stack operands to the loop block's arguments in
  7438. * wasm_loader_emit_br_info for opcode br. */
  7439. uint16 start_dynamic_offset;
  7440. #endif
  7441. /* Indicate the operand stack is in polymorphic state.
  7442. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7443. * to polymorphic state until the block's 'end' opcode is processed.
  7444. * If stack is in polymorphic state and stack is empty, instruction can
  7445. * pop any type of value directly without decreasing stack top pointer
  7446. * and stack cell num. */
  7447. bool is_stack_polymorphic;
  7448. } BranchBlock;
  7449. typedef struct WASMLoaderContext {
  7450. /* frame ref stack */
  7451. uint8 *frame_ref;
  7452. uint8 *frame_ref_bottom;
  7453. uint8 *frame_ref_boundary;
  7454. uint32 frame_ref_size;
  7455. uint32 stack_cell_num;
  7456. uint32 max_stack_cell_num;
  7457. #if WASM_ENABLE_GC != 0
  7458. /* frame reftype map stack */
  7459. WASMRefTypeMap *frame_reftype_map;
  7460. WASMRefTypeMap *frame_reftype_map_bottom;
  7461. WASMRefTypeMap *frame_reftype_map_boundary;
  7462. uint32 frame_reftype_map_size;
  7463. uint32 reftype_map_num;
  7464. uint32 max_reftype_map_num;
  7465. /* Current module */
  7466. WASMModule *module;
  7467. /* Current module's ref_type_set */
  7468. HashMap *ref_type_set;
  7469. /* Always point to local variable ref_type of
  7470. wasm_loader_prepare_bytecode */
  7471. WASMRefType *ref_type_tmp;
  7472. #endif
  7473. /* frame csp stack */
  7474. BranchBlock *frame_csp;
  7475. BranchBlock *frame_csp_bottom;
  7476. BranchBlock *frame_csp_boundary;
  7477. uint32 frame_csp_size;
  7478. uint32 csp_num;
  7479. uint32 max_csp_num;
  7480. #if WASM_ENABLE_FAST_INTERP != 0
  7481. /* frame offset stack */
  7482. int16 *frame_offset;
  7483. int16 *frame_offset_bottom;
  7484. int16 *frame_offset_boundary;
  7485. uint32 frame_offset_size;
  7486. int16 dynamic_offset;
  7487. int16 start_dynamic_offset;
  7488. int16 max_dynamic_offset;
  7489. /* preserved local offset */
  7490. int16 preserved_local_offset;
  7491. /* const buffer for i64 and f64 consts, note that the raw bytes
  7492. * of i64 and f64 are the same, so we read an i64 value from an
  7493. * f64 const with its raw bytes, something like `*(int64 *)&f64 */
  7494. int64 *i64_consts;
  7495. uint32 i64_const_max_num;
  7496. uint32 i64_const_num;
  7497. /* const buffer for i32 and f32 consts */
  7498. int32 *i32_consts;
  7499. uint32 i32_const_max_num;
  7500. uint32 i32_const_num;
  7501. /* const buffer for V128 */
  7502. V128 *v128_consts;
  7503. uint32 v128_const_max_num;
  7504. uint32 v128_const_num;
  7505. /* processed code */
  7506. uint8 *p_code_compiled;
  7507. uint8 *p_code_compiled_end;
  7508. uint32 code_compiled_size;
  7509. /* If the last opcode will be dropped, the peak memory usage will be larger
  7510. * than the final code_compiled_size, we record the peak size to ensure
  7511. * there will not be invalid memory access during second traverse */
  7512. uint32 code_compiled_peak_size;
  7513. #endif
  7514. } WASMLoaderContext;
  7515. #define CHECK_CSP_PUSH() \
  7516. do { \
  7517. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7518. MEM_REALLOC( \
  7519. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7520. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7521. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7522. ctx->frame_csp_boundary = \
  7523. ctx->frame_csp_bottom \
  7524. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7525. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7526. } \
  7527. } while (0)
  7528. #define CHECK_CSP_POP() \
  7529. do { \
  7530. if (ctx->csp_num < 1) { \
  7531. set_error_buf(error_buf, error_buf_size, \
  7532. "type mismatch: " \
  7533. "expect data but block stack was empty"); \
  7534. goto fail; \
  7535. } \
  7536. } while (0)
  7537. #if WASM_ENABLE_FAST_INTERP != 0
  7538. static bool
  7539. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7540. uint32 error_buf_size)
  7541. {
  7542. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7543. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7544. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7545. ctx->frame_offset_size + 16);
  7546. ctx->frame_offset_size += 16;
  7547. ctx->frame_offset_boundary =
  7548. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7549. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7550. }
  7551. return true;
  7552. fail:
  7553. return false;
  7554. }
  7555. static bool
  7556. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7557. {
  7558. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7559. return false;
  7560. return true;
  7561. }
  7562. static void
  7563. free_label_patch_list(BranchBlock *frame_csp)
  7564. {
  7565. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7566. BranchBlockPatch *next;
  7567. while (label_patch != NULL) {
  7568. next = label_patch->next;
  7569. wasm_runtime_free(label_patch);
  7570. label_patch = next;
  7571. }
  7572. frame_csp->patch_list = NULL;
  7573. }
  7574. static void
  7575. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7576. {
  7577. BranchBlock *tmp_csp = frame_csp;
  7578. uint32 i;
  7579. for (i = 0; i < csp_num; i++) {
  7580. free_label_patch_list(tmp_csp);
  7581. tmp_csp++;
  7582. }
  7583. }
  7584. static void
  7585. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7586. {
  7587. BranchBlock *tmp_csp = frame_csp;
  7588. uint32 i;
  7589. for (i = 0; i < csp_num; i++) {
  7590. if (tmp_csp->param_frame_offsets)
  7591. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7592. tmp_csp++;
  7593. }
  7594. }
  7595. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7596. #if WASM_ENABLE_GC != 0
  7597. static bool
  7598. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7599. char *error_buf, uint32 error_buf_size)
  7600. {
  7601. BranchBlock *current_csp = ctx->frame_csp - 1;
  7602. uint32 local_mask_size;
  7603. if (local_count == 0) {
  7604. current_csp->local_use_mask_size = 0;
  7605. return true;
  7606. }
  7607. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7608. * else branch, we don't need to allocate memory again */
  7609. if (!current_csp->local_use_mask) {
  7610. local_mask_size = (local_count + 7) / sizeof(uint8);
  7611. if (!(current_csp->local_use_mask =
  7612. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7613. return false;
  7614. }
  7615. current_csp->local_use_mask_size = local_mask_size;
  7616. }
  7617. else {
  7618. local_mask_size = current_csp->local_use_mask_size;
  7619. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7620. }
  7621. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7622. /* For non-function blocks, inherit the use status from parent block */
  7623. BranchBlock *parent_csp = current_csp - 1;
  7624. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7625. bh_assert(parent_csp->local_use_mask);
  7626. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7627. parent_csp->local_use_mask, local_mask_size);
  7628. }
  7629. return true;
  7630. }
  7631. static void
  7632. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7633. {
  7634. BranchBlock *current_csp = ctx->frame_csp - 1;
  7635. bh_assert(current_csp->local_use_mask
  7636. || current_csp->local_use_mask_size == 0);
  7637. if (current_csp->local_use_mask) {
  7638. wasm_runtime_free(current_csp->local_use_mask);
  7639. }
  7640. current_csp->local_use_mask = NULL;
  7641. current_csp->local_use_mask_size = 0;
  7642. }
  7643. static void
  7644. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7645. {
  7646. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7647. uint32 i;
  7648. for (i = 0; i < ctx->csp_num; i++) {
  7649. if (tmp_csp->local_use_mask) {
  7650. wasm_runtime_free(tmp_csp->local_use_mask);
  7651. tmp_csp->local_use_mask = NULL;
  7652. tmp_csp->local_use_mask_size = 0;
  7653. }
  7654. tmp_csp++;
  7655. }
  7656. }
  7657. static void
  7658. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7659. {
  7660. BranchBlock *current_csp = ctx->frame_csp - 1;
  7661. uint32 byte_offset = index / sizeof(uint8);
  7662. uint32 bit_offset = index % sizeof(uint8);
  7663. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7664. bh_assert(current_csp->local_use_mask);
  7665. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7666. }
  7667. static bool
  7668. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7669. {
  7670. BranchBlock *current_csp = ctx->frame_csp - 1;
  7671. uint32 byte_offset = index / sizeof(uint8);
  7672. uint32 bit_offset = index % sizeof(uint8);
  7673. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7674. bh_assert(current_csp->local_use_mask);
  7675. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7676. ? true
  7677. : false;
  7678. }
  7679. #endif /* end of WASM_ENABLE_GC != 0 */
  7680. static void
  7681. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7682. {
  7683. if (ctx) {
  7684. if (ctx->frame_ref_bottom)
  7685. wasm_runtime_free(ctx->frame_ref_bottom);
  7686. #if WASM_ENABLE_GC != 0
  7687. if (ctx->frame_reftype_map_bottom)
  7688. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7689. #endif
  7690. if (ctx->frame_csp_bottom) {
  7691. #if WASM_ENABLE_FAST_INTERP != 0
  7692. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7693. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7694. ctx->csp_num);
  7695. #endif
  7696. #if WASM_ENABLE_GC != 0
  7697. wasm_loader_clean_all_local_use_masks(ctx);
  7698. #endif
  7699. wasm_runtime_free(ctx->frame_csp_bottom);
  7700. }
  7701. #if WASM_ENABLE_FAST_INTERP != 0
  7702. if (ctx->frame_offset_bottom)
  7703. wasm_runtime_free(ctx->frame_offset_bottom);
  7704. if (ctx->i64_consts)
  7705. wasm_runtime_free(ctx->i64_consts);
  7706. if (ctx->i32_consts)
  7707. wasm_runtime_free(ctx->i32_consts);
  7708. if (ctx->v128_consts)
  7709. wasm_runtime_free(ctx->v128_consts);
  7710. #endif
  7711. wasm_runtime_free(ctx);
  7712. }
  7713. }
  7714. static WASMLoaderContext *
  7715. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7716. {
  7717. WASMLoaderContext *loader_ctx =
  7718. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7719. if (!loader_ctx)
  7720. return NULL;
  7721. loader_ctx->frame_ref_size = 32;
  7722. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7723. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7724. goto fail;
  7725. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7726. #if WASM_ENABLE_GC != 0
  7727. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7728. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7729. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7730. error_buf_size)))
  7731. goto fail;
  7732. loader_ctx->frame_reftype_map_boundary =
  7733. loader_ctx->frame_reftype_map_bottom + 16;
  7734. #endif
  7735. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7736. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7737. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7738. goto fail;
  7739. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7740. #if WASM_ENABLE_EXCE_HANDLING != 0
  7741. func->exception_handler_count = 0;
  7742. #endif
  7743. #if WASM_ENABLE_FAST_INTERP != 0
  7744. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7745. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7746. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7747. error_buf_size)))
  7748. goto fail;
  7749. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7750. loader_ctx->i64_const_max_num = 8;
  7751. if (!(loader_ctx->i64_consts =
  7752. loader_malloc(sizeof(int64) * loader_ctx->i64_const_max_num,
  7753. error_buf, error_buf_size)))
  7754. goto fail;
  7755. loader_ctx->i32_const_max_num = 8;
  7756. if (!(loader_ctx->i32_consts =
  7757. loader_malloc(sizeof(int32) * loader_ctx->i32_const_max_num,
  7758. error_buf, error_buf_size)))
  7759. goto fail;
  7760. loader_ctx->v128_const_max_num = 8;
  7761. if (!(loader_ctx->v128_consts =
  7762. loader_malloc(sizeof(V128) * loader_ctx->v128_const_max_num,
  7763. error_buf, error_buf_size)))
  7764. goto fail;
  7765. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7766. set_error_buf(error_buf, error_buf_size,
  7767. "fast interpreter offset overflow");
  7768. goto fail;
  7769. }
  7770. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7771. loader_ctx->max_dynamic_offset =
  7772. func->param_cell_num + func->local_cell_num;
  7773. #endif
  7774. return loader_ctx;
  7775. fail:
  7776. wasm_loader_ctx_destroy(loader_ctx);
  7777. return NULL;
  7778. }
  7779. static bool
  7780. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7781. uint32 error_buf_size)
  7782. {
  7783. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7784. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7785. /* Increase the frame ref stack */
  7786. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7787. ctx->frame_ref_size + 16);
  7788. ctx->frame_ref_size += 16;
  7789. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7790. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7791. }
  7792. #if WASM_ENABLE_GC != 0
  7793. if (wasm_is_type_multi_byte_type(type)
  7794. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7795. /* Increase the frame reftype map stack */
  7796. bh_assert(
  7797. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7798. * sizeof(WASMRefTypeMap))
  7799. == ctx->frame_reftype_map_size);
  7800. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7801. ctx->frame_reftype_map_size
  7802. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7803. ctx->frame_reftype_map =
  7804. ctx->frame_reftype_map_bottom
  7805. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7806. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7807. ctx->frame_reftype_map_boundary =
  7808. ctx->frame_reftype_map_bottom
  7809. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7810. }
  7811. #endif
  7812. return true;
  7813. fail:
  7814. return false;
  7815. }
  7816. static bool
  7817. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7818. uint32 error_buf_size)
  7819. {
  7820. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7821. uint32 i;
  7822. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7823. return false;
  7824. #if WASM_ENABLE_GC != 0
  7825. if (wasm_is_type_multi_byte_type(type)) {
  7826. WASMRefType *ref_type;
  7827. if (!(ref_type =
  7828. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7829. error_buf, error_buf_size))) {
  7830. return false;
  7831. }
  7832. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7833. /* Increase the frame reftype map stack */
  7834. bh_assert((uint32)((ctx->frame_reftype_map
  7835. - ctx->frame_reftype_map_bottom)
  7836. * sizeof(WASMRefTypeMap))
  7837. == ctx->frame_reftype_map_size);
  7838. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7839. ctx->frame_reftype_map_size,
  7840. ctx->frame_reftype_map_size
  7841. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7842. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7843. + ctx->frame_reftype_map_size
  7844. / ((uint32)sizeof(WASMRefTypeMap));
  7845. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7846. ctx->frame_reftype_map_boundary =
  7847. ctx->frame_reftype_map_bottom
  7848. + ctx->frame_reftype_map_size
  7849. / ((uint32)sizeof(WASMRefTypeMap));
  7850. }
  7851. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7852. ctx->frame_reftype_map->ref_type = ref_type;
  7853. ctx->frame_reftype_map++;
  7854. ctx->reftype_map_num++;
  7855. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7856. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7857. }
  7858. #endif
  7859. for (i = 0; i < type_cell_num; i++)
  7860. *ctx->frame_ref++ = type;
  7861. ctx->stack_cell_num += type_cell_num;
  7862. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7863. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7864. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7865. set_error_buf(error_buf, error_buf_size,
  7866. "operand stack depth limit exceeded");
  7867. return false;
  7868. }
  7869. }
  7870. return true;
  7871. #if WASM_ENABLE_GC != 0
  7872. fail:
  7873. return false;
  7874. #endif
  7875. }
  7876. static bool
  7877. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7878. int32 stack_cell_num,
  7879. #if WASM_ENABLE_GC != 0
  7880. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7881. #endif
  7882. uint8 type,
  7883. #if WASM_ENABLE_GC != 0
  7884. WASMRefType *ref_type,
  7885. #endif
  7886. char *error_buf, uint32 error_buf_size)
  7887. {
  7888. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7889. #if WASM_ENABLE_GC != 0
  7890. WASMRefType *frame_reftype = NULL;
  7891. #endif
  7892. if (stack_cell_num < type_cell_num) {
  7893. set_error_buf(error_buf, error_buf_size,
  7894. "type mismatch: expect data but stack was empty");
  7895. return false;
  7896. }
  7897. #if WASM_ENABLE_GC == 0
  7898. for (i = 0; i < type_cell_num; i++) {
  7899. if (*(frame_ref - 1 - i) != type) {
  7900. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7901. "type mismatch: expect ", type2str(type),
  7902. " but got other");
  7903. return false;
  7904. }
  7905. }
  7906. #else
  7907. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7908. bh_assert(reftype_map_num > 0);
  7909. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7910. }
  7911. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7912. ref_type, ctx->module->types,
  7913. ctx->module->type_count)) {
  7914. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7915. "type mismatch: expect ", type2str(type),
  7916. " but got other");
  7917. return false;
  7918. }
  7919. for (i = 0; i < type_cell_num - 1; i++) {
  7920. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7921. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7922. "type mismatch: expect ", type2str(type),
  7923. " but got other");
  7924. return false;
  7925. }
  7926. }
  7927. #endif
  7928. return true;
  7929. }
  7930. static bool
  7931. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7932. uint32 error_buf_size)
  7933. {
  7934. int32 block_stack_cell_num =
  7935. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7936. #if WASM_ENABLE_GC != 0
  7937. int32 reftype_map_num =
  7938. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7939. #endif
  7940. if (block_stack_cell_num > 0) {
  7941. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7942. /* the stack top is a value of any type, return success */
  7943. return true;
  7944. }
  7945. #if WASM_ENABLE_GC != 0
  7946. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7947. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7948. WASMRefType *stack_top_ref_type = NULL;
  7949. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7950. bh_assert(reftype_map_num > 0);
  7951. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7952. }
  7953. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7954. ctx->ref_type_tmp, ctx->module->types,
  7955. ctx->module->type_count)) {
  7956. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7957. uint32 ref_type_struct_size =
  7958. wasm_reftype_struct_size(stack_top_ref_type);
  7959. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7960. stack_top_ref_type, ref_type_struct_size);
  7961. }
  7962. return true;
  7963. }
  7964. }
  7965. #endif
  7966. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7967. #if WASM_ENABLE_GC != 0
  7968. ctx->frame_reftype_map, reftype_map_num,
  7969. #endif
  7970. type,
  7971. #if WASM_ENABLE_GC != 0
  7972. ctx->ref_type_tmp,
  7973. #endif
  7974. error_buf, error_buf_size)) {
  7975. return false;
  7976. }
  7977. return true;
  7978. }
  7979. static bool
  7980. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7981. uint32 error_buf_size)
  7982. {
  7983. BranchBlock *cur_block = ctx->frame_csp - 1;
  7984. int32 available_stack_cell =
  7985. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7986. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7987. /* Directly return success if current block is in stack
  7988. polymorphic state while stack is empty. */
  7989. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7990. return true;
  7991. if (type == VALUE_TYPE_VOID)
  7992. return true;
  7993. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7994. return false;
  7995. bh_assert(available_stack_cell > 0);
  7996. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7997. type = VALUE_TYPE_ANY;
  7998. cell_num_to_pop = 1;
  7999. }
  8000. ctx->frame_ref -= cell_num_to_pop;
  8001. ctx->stack_cell_num -= cell_num_to_pop;
  8002. #if WASM_ENABLE_GC != 0
  8003. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  8004. ctx->frame_reftype_map--;
  8005. ctx->reftype_map_num--;
  8006. }
  8007. #endif
  8008. return true;
  8009. }
  8010. #if WASM_ENABLE_GC != 0
  8011. /* Get the stack top element of current block */
  8012. static bool
  8013. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  8014. WASMRefType **p_ref_type, char *error_buf,
  8015. uint32 error_buf_size)
  8016. {
  8017. BranchBlock *cur_block = ctx->frame_csp - 1;
  8018. int32 available_stack_cell =
  8019. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8020. if (available_stack_cell <= 0) {
  8021. /* Directly return success if current block is in stack
  8022. polymorphic state while stack is empty. */
  8023. if (cur_block->is_stack_polymorphic) {
  8024. *p_type = VALUE_TYPE_ANY;
  8025. return true;
  8026. }
  8027. else {
  8028. set_error_buf(
  8029. error_buf, error_buf_size,
  8030. "type mismatch: expect data but block stack was empty");
  8031. return false;
  8032. }
  8033. }
  8034. *p_type = *(ctx->frame_ref - 1);
  8035. if (wasm_is_type_multi_byte_type(*p_type)) {
  8036. int32 available_reftype_map =
  8037. (int32)(ctx->reftype_map_num
  8038. - (ctx->frame_csp - 1)->reftype_map_num);
  8039. bh_assert(available_reftype_map > 0);
  8040. (void)available_reftype_map;
  8041. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  8042. }
  8043. return true;
  8044. }
  8045. #if WASM_ENABLE_FAST_INTERP != 0
  8046. static bool
  8047. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8048. char *error_buf, uint32 error_buf_size);
  8049. #endif
  8050. /* Check whether the stack top elem is a heap object, and if yes,
  8051. pop and return it */
  8052. static bool
  8053. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  8054. WASMRefType *ref_ht_ret, char *error_buf,
  8055. uint32 error_buf_size)
  8056. {
  8057. uint8 type = 0;
  8058. WASMRefType *ref_type = NULL;
  8059. /* Get stack top element */
  8060. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  8061. error_buf_size)) {
  8062. return false;
  8063. }
  8064. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  8065. /* stack top isn't a ref type */
  8066. && !wasm_is_type_reftype(type)) {
  8067. set_error_buf(error_buf, error_buf_size,
  8068. "type mismatch: expect heap object but got others");
  8069. return false;
  8070. }
  8071. /* POP stack top */
  8072. if (wasm_is_type_multi_byte_type(type)) {
  8073. bh_assert(ref_type);
  8074. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  8075. wasm_reftype_struct_size(ref_type));
  8076. }
  8077. #if WASM_ENABLE_FAST_INTERP != 0
  8078. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  8079. error_buf_size)) {
  8080. return false;
  8081. }
  8082. #else
  8083. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  8084. return false;
  8085. }
  8086. #endif
  8087. if (p_type)
  8088. *p_type = type;
  8089. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  8090. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  8091. wasm_reftype_struct_size(ref_type));
  8092. }
  8093. return true;
  8094. }
  8095. /* Check whether the stack top elem is subtype of (ref null ht),
  8096. and if yes, pop it and return the converted (ref ht) */
  8097. static bool
  8098. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  8099. WASMRefType *ref_ht_ret, char *error_buf,
  8100. uint32 error_buf_size)
  8101. {
  8102. uint8 type = 0;
  8103. WASMRefType ref_type = { 0 };
  8104. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  8105. error_buf_size)) {
  8106. return false;
  8107. }
  8108. /* Convert to related (ref ht) and return */
  8109. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  8110. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  8111. */
  8112. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  8113. HEAP_TYPE_ARRAY
  8114. + (type - REF_TYPE_ARRAYREF));
  8115. type = ref_ht_ret->ref_type;
  8116. }
  8117. else if (wasm_is_reftype_htref_nullable(type)
  8118. || wasm_is_reftype_htref_non_nullable(type)) {
  8119. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  8120. wasm_reftype_struct_size(&ref_type));
  8121. /* Convert to (ref ht) */
  8122. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  8123. ref_ht_ret->ref_ht_common.nullable = false;
  8124. type = ref_ht_ret->ref_type;
  8125. }
  8126. *p_type = type;
  8127. return true;
  8128. }
  8129. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  8130. and if yes, pop it and return the type_idx */
  8131. static bool
  8132. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  8133. uint32 *p_type_idx, char *error_buf,
  8134. uint32 error_buf_size)
  8135. {
  8136. uint8 type = 0;
  8137. int32 type_idx = -1;
  8138. WASMRefType *ref_type = NULL;
  8139. /* Get stack top element */
  8140. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  8141. error_buf_size)) {
  8142. return false;
  8143. }
  8144. if (type != VALUE_TYPE_ANY) {
  8145. /* stack top isn't (ref null $t) */
  8146. if (!((wasm_is_reftype_htref_nullable(type)
  8147. || wasm_is_reftype_htref_non_nullable(type))
  8148. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  8149. set_error_buf(error_buf, error_buf_size,
  8150. "type mismatch: expect (ref null $t) but got others");
  8151. return false;
  8152. }
  8153. type_idx = ref_type->ref_ht_typeidx.type_idx;
  8154. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  8155. wasm_reftype_struct_size(ref_type));
  8156. }
  8157. /* POP stack top */
  8158. #if WASM_ENABLE_FAST_INTERP != 0
  8159. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  8160. error_buf_size)) {
  8161. return false;
  8162. }
  8163. #else
  8164. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  8165. return false;
  8166. }
  8167. #endif
  8168. /* Convert to type_idx and return */
  8169. *p_type = type;
  8170. if (type != VALUE_TYPE_ANY)
  8171. *p_type_idx = (uint32)type_idx;
  8172. return true;
  8173. }
  8174. #endif /* WASM_ENABLE_GC != 0 */
  8175. #if WASM_ENABLE_FAST_INTERP == 0
  8176. static bool
  8177. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  8178. uint8 type_push, uint8 type_pop, char *error_buf,
  8179. uint32 error_buf_size)
  8180. {
  8181. for (int i = 0; i < pop_cnt; i++) {
  8182. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8183. error_buf_size))
  8184. return false;
  8185. }
  8186. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8187. return false;
  8188. return true;
  8189. }
  8190. #endif
  8191. static bool
  8192. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  8193. BlockType block_type, uint8 *start_addr,
  8194. char *error_buf, uint32 error_buf_size)
  8195. {
  8196. CHECK_CSP_PUSH();
  8197. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  8198. ctx->frame_csp->label_type = label_type;
  8199. ctx->frame_csp->block_type = block_type;
  8200. ctx->frame_csp->start_addr = start_addr;
  8201. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  8202. #if WASM_ENABLE_GC != 0
  8203. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  8204. #endif
  8205. #if WASM_ENABLE_FAST_INTERP != 0
  8206. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  8207. ctx->frame_csp->patch_list = NULL;
  8208. #endif
  8209. ctx->frame_csp++;
  8210. ctx->csp_num++;
  8211. if (ctx->csp_num > ctx->max_csp_num) {
  8212. ctx->max_csp_num = ctx->csp_num;
  8213. if (ctx->max_csp_num > UINT16_MAX) {
  8214. set_error_buf(error_buf, error_buf_size,
  8215. "label stack depth limit exceeded");
  8216. return false;
  8217. }
  8218. }
  8219. return true;
  8220. fail:
  8221. return false;
  8222. }
  8223. static bool
  8224. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  8225. uint32 error_buf_size)
  8226. {
  8227. CHECK_CSP_POP();
  8228. #if WASM_ENABLE_FAST_INTERP != 0
  8229. if ((ctx->frame_csp - 1)->param_frame_offsets)
  8230. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  8231. #endif
  8232. ctx->frame_csp--;
  8233. ctx->csp_num--;
  8234. return true;
  8235. fail:
  8236. return false;
  8237. }
  8238. #if WASM_ENABLE_FAST_INTERP != 0
  8239. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  8240. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  8241. #define emit_label(opcode) \
  8242. do { \
  8243. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  8244. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8245. } while (0)
  8246. #define skip_label() \
  8247. do { \
  8248. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  8249. LOG_OP("\ndelete last op\n"); \
  8250. } while (0)
  8251. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  8252. #if UINTPTR_MAX == UINT64_MAX
  8253. #define emit_label(opcode) \
  8254. do { \
  8255. int32 offset = \
  8256. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  8257. /* emit int32 relative offset in 64-bit target */ \
  8258. wasm_loader_emit_uint32(loader_ctx, offset); \
  8259. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8260. } while (0)
  8261. #else
  8262. #define emit_label(opcode) \
  8263. do { \
  8264. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  8265. /* emit uint32 label address in 32-bit target */ \
  8266. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  8267. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8268. } while (0)
  8269. #endif
  8270. #define skip_label() \
  8271. do { \
  8272. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  8273. LOG_OP("\ndelete last op\n"); \
  8274. } while (0)
  8275. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  8276. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  8277. #define emit_label(opcode) \
  8278. do { \
  8279. wasm_loader_emit_uint8(loader_ctx, opcode); \
  8280. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8281. } while (0)
  8282. #define skip_label() \
  8283. do { \
  8284. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  8285. LOG_OP("\ndelete last op\n"); \
  8286. } while (0)
  8287. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  8288. #define emit_empty_label_addr_and_frame_ip(type) \
  8289. do { \
  8290. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  8291. loader_ctx->p_code_compiled, error_buf, \
  8292. error_buf_size)) \
  8293. goto fail; \
  8294. /* label address, to be patched */ \
  8295. wasm_loader_emit_ptr(loader_ctx, NULL); \
  8296. } while (0)
  8297. #define emit_br_info(frame_csp, is_br) \
  8298. do { \
  8299. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  8300. error_buf_size)) \
  8301. goto fail; \
  8302. } while (0)
  8303. #define LAST_OP_OUTPUT_I32() \
  8304. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  8305. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  8306. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  8307. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  8308. || (last_op >= WASM_OP_I32_WRAP_I64 \
  8309. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  8310. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  8311. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  8312. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  8313. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  8314. || (last_op == EXT_OP_COPY_STACK_TOP)
  8315. #define LAST_OP_OUTPUT_I64() \
  8316. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  8317. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  8318. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  8319. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  8320. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  8321. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  8322. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  8323. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  8324. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  8325. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  8326. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  8327. #define GET_CONST_OFFSET(type, val) \
  8328. do { \
  8329. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  8330. &operand_offset, error_buf, \
  8331. error_buf_size))) \
  8332. goto fail; \
  8333. } while (0)
  8334. #define GET_CONST_F32_OFFSET(type, fval) \
  8335. do { \
  8336. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  8337. &operand_offset, error_buf, \
  8338. error_buf_size))) \
  8339. goto fail; \
  8340. } while (0)
  8341. #define GET_CONST_F64_OFFSET(type, fval) \
  8342. do { \
  8343. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  8344. &operand_offset, error_buf, \
  8345. error_buf_size))) \
  8346. goto fail; \
  8347. } while (0)
  8348. #define emit_operand(ctx, offset) \
  8349. do { \
  8350. wasm_loader_emit_int16(ctx, offset); \
  8351. LOG_OP("%d\t", offset); \
  8352. } while (0)
  8353. #define emit_byte(ctx, byte) \
  8354. do { \
  8355. wasm_loader_emit_uint8(ctx, byte); \
  8356. LOG_OP("%d\t", byte); \
  8357. } while (0)
  8358. #define emit_uint32(ctx, value) \
  8359. do { \
  8360. wasm_loader_emit_uint32(ctx, value); \
  8361. LOG_OP("%d\t", value); \
  8362. } while (0)
  8363. #define emit_uint64(ctx, value) \
  8364. do { \
  8365. wasm_loader_emit_const(ctx, &value, false); \
  8366. LOG_OP("%lld\t", value); \
  8367. } while (0)
  8368. #define emit_float32(ctx, value) \
  8369. do { \
  8370. wasm_loader_emit_const(ctx, &value, true); \
  8371. LOG_OP("%f\t", value); \
  8372. } while (0)
  8373. #define emit_float64(ctx, value) \
  8374. do { \
  8375. wasm_loader_emit_const(ctx, &value, false); \
  8376. LOG_OP("%f\t", value); \
  8377. } while (0)
  8378. static bool
  8379. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  8380. {
  8381. if (!(ctx->p_code_compiled =
  8382. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  8383. return false;
  8384. ctx->p_code_compiled_end =
  8385. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  8386. /* clean up frame ref */
  8387. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  8388. ctx->frame_ref = ctx->frame_ref_bottom;
  8389. ctx->stack_cell_num = 0;
  8390. #if WASM_ENABLE_GC != 0
  8391. /* clean up reftype map */
  8392. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  8393. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  8394. ctx->reftype_map_num = 0;
  8395. #endif
  8396. /* clean up frame csp */
  8397. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  8398. ctx->frame_csp = ctx->frame_csp_bottom;
  8399. ctx->csp_num = 0;
  8400. ctx->max_csp_num = 0;
  8401. /* clean up frame offset */
  8402. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  8403. ctx->frame_offset = ctx->frame_offset_bottom;
  8404. ctx->dynamic_offset = ctx->start_dynamic_offset;
  8405. /* init preserved local offsets */
  8406. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  8407. /* const buf is reserved */
  8408. return true;
  8409. }
  8410. static void
  8411. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8412. {
  8413. ctx->code_compiled_size += size;
  8414. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8415. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8416. }
  8417. }
  8418. static void
  8419. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8420. {
  8421. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8422. if (ctx->p_code_compiled) {
  8423. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8424. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8425. #endif
  8426. bh_memcpy_s(ctx->p_code_compiled,
  8427. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8428. value, size);
  8429. ctx->p_code_compiled += size;
  8430. }
  8431. else {
  8432. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8433. bh_assert((ctx->code_compiled_size & 1) == 0);
  8434. #endif
  8435. increase_compiled_code_space(ctx, size);
  8436. }
  8437. }
  8438. static void
  8439. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8440. {
  8441. if (ctx->p_code_compiled) {
  8442. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8443. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8444. #endif
  8445. STORE_U32(ctx->p_code_compiled, value);
  8446. ctx->p_code_compiled += sizeof(uint32);
  8447. }
  8448. else {
  8449. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8450. bh_assert((ctx->code_compiled_size & 1) == 0);
  8451. #endif
  8452. increase_compiled_code_space(ctx, sizeof(uint32));
  8453. }
  8454. }
  8455. static void
  8456. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8457. {
  8458. if (ctx->p_code_compiled) {
  8459. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8460. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8461. #endif
  8462. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8463. ctx->p_code_compiled += sizeof(int16);
  8464. }
  8465. else {
  8466. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8467. bh_assert((ctx->code_compiled_size & 1) == 0);
  8468. #endif
  8469. increase_compiled_code_space(ctx, sizeof(uint16));
  8470. }
  8471. }
  8472. static void
  8473. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8474. {
  8475. if (ctx->p_code_compiled) {
  8476. *(ctx->p_code_compiled) = value;
  8477. ctx->p_code_compiled += sizeof(uint8);
  8478. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8479. ctx->p_code_compiled++;
  8480. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8481. #endif
  8482. }
  8483. else {
  8484. increase_compiled_code_space(ctx, sizeof(uint8));
  8485. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8486. increase_compiled_code_space(ctx, sizeof(uint8));
  8487. bh_assert((ctx->code_compiled_size & 1) == 0);
  8488. #endif
  8489. }
  8490. }
  8491. static void
  8492. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8493. {
  8494. if (ctx->p_code_compiled) {
  8495. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8496. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8497. #endif
  8498. STORE_PTR(ctx->p_code_compiled, value);
  8499. ctx->p_code_compiled += sizeof(void *);
  8500. }
  8501. else {
  8502. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8503. bh_assert((ctx->code_compiled_size & 1) == 0);
  8504. #endif
  8505. increase_compiled_code_space(ctx, sizeof(void *));
  8506. }
  8507. }
  8508. static void
  8509. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8510. {
  8511. if (ctx->p_code_compiled) {
  8512. ctx->p_code_compiled -= size;
  8513. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8514. if (size == sizeof(uint8)) {
  8515. ctx->p_code_compiled--;
  8516. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8517. }
  8518. #endif
  8519. }
  8520. else {
  8521. ctx->code_compiled_size -= size;
  8522. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8523. if (size == sizeof(uint8)) {
  8524. ctx->code_compiled_size--;
  8525. bh_assert((ctx->code_compiled_size & 1) == 0);
  8526. }
  8527. #endif
  8528. }
  8529. }
  8530. static bool
  8531. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8532. uint32 local_index, uint32 local_type,
  8533. bool *preserved, char *error_buf,
  8534. uint32 error_buf_size)
  8535. {
  8536. uint32 i = 0;
  8537. int16 preserved_offset = (int16)local_index;
  8538. *preserved = false;
  8539. while (i < loader_ctx->stack_cell_num) {
  8540. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8541. /* move previous local into dynamic space before a set/tee_local opcode
  8542. */
  8543. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8544. if (!(*preserved)) {
  8545. *preserved = true;
  8546. skip_label();
  8547. preserved_offset = loader_ctx->preserved_local_offset;
  8548. if (loader_ctx->p_code_compiled) {
  8549. bh_assert(preserved_offset != (int16)local_index);
  8550. }
  8551. if (is_32bit_type(local_type)) {
  8552. /* Only increase preserve offset in the second traversal */
  8553. if (loader_ctx->p_code_compiled)
  8554. loader_ctx->preserved_local_offset++;
  8555. emit_label(EXT_OP_COPY_STACK_TOP);
  8556. }
  8557. #if WASM_ENABLE_SIMDE != 0
  8558. else if (local_type == VALUE_TYPE_V128) {
  8559. if (loader_ctx->p_code_compiled)
  8560. loader_ctx->preserved_local_offset += 4;
  8561. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  8562. }
  8563. #endif
  8564. else {
  8565. if (loader_ctx->p_code_compiled)
  8566. loader_ctx->preserved_local_offset += 2;
  8567. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8568. }
  8569. /* overflow */
  8570. if (preserved_offset > loader_ctx->preserved_local_offset) {
  8571. set_error_buf_v(error_buf, error_buf_size,
  8572. "too much local cells 0x%x",
  8573. loader_ctx->preserved_local_offset);
  8574. return false;
  8575. }
  8576. emit_operand(loader_ctx, local_index);
  8577. emit_operand(loader_ctx, preserved_offset);
  8578. emit_label(opcode);
  8579. }
  8580. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8581. }
  8582. if (cur_type == VALUE_TYPE_V128) {
  8583. i += 4;
  8584. }
  8585. else if (is_32bit_type(cur_type)) {
  8586. i++;
  8587. }
  8588. else {
  8589. i += 2;
  8590. }
  8591. }
  8592. (void)error_buf;
  8593. (void)error_buf_size;
  8594. return true;
  8595. }
  8596. static bool
  8597. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8598. char *error_buf, uint32 error_buf_size)
  8599. {
  8600. uint32 i = 0;
  8601. bool preserve_local;
  8602. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8603. blocks will not influence the value of these locals */
  8604. while (i < loader_ctx->stack_cell_num) {
  8605. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8606. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8607. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8608. && (cur_offset >= 0)) {
  8609. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8610. cur_type, &preserve_local,
  8611. error_buf, error_buf_size)))
  8612. return false;
  8613. }
  8614. if (cur_type == VALUE_TYPE_V128) {
  8615. i += 4;
  8616. }
  8617. else if (is_32bit_type(cur_type)) {
  8618. i++;
  8619. }
  8620. else {
  8621. i += 2;
  8622. }
  8623. }
  8624. return true;
  8625. }
  8626. static bool
  8627. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8628. uint8 *p_code_compiled, char *error_buf,
  8629. uint32 error_buf_size)
  8630. {
  8631. BranchBlockPatch *patch =
  8632. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8633. if (!patch) {
  8634. return false;
  8635. }
  8636. patch->patch_type = patch_type;
  8637. patch->code_compiled = p_code_compiled;
  8638. if (!frame_csp->patch_list) {
  8639. frame_csp->patch_list = patch;
  8640. patch->next = NULL;
  8641. }
  8642. else {
  8643. patch->next = frame_csp->patch_list;
  8644. frame_csp->patch_list = patch;
  8645. }
  8646. return true;
  8647. }
  8648. static void
  8649. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8650. {
  8651. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8652. BranchBlockPatch *node = frame_csp->patch_list;
  8653. BranchBlockPatch *node_prev = NULL, *node_next;
  8654. if (!ctx->p_code_compiled)
  8655. return;
  8656. while (node) {
  8657. node_next = node->next;
  8658. if (node->patch_type == patch_type) {
  8659. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8660. if (node_prev == NULL) {
  8661. frame_csp->patch_list = node_next;
  8662. }
  8663. else {
  8664. node_prev->next = node_next;
  8665. }
  8666. wasm_runtime_free(node);
  8667. }
  8668. else {
  8669. node_prev = node;
  8670. }
  8671. node = node_next;
  8672. }
  8673. }
  8674. static bool
  8675. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8676. bool is_br, char *error_buf, uint32 error_buf_size)
  8677. {
  8678. /* br info layout:
  8679. * a) arity of target block
  8680. * b) total cell num of arity values
  8681. * c) each arity value's cell num
  8682. * d) each arity value's src frame offset
  8683. * e) each arity values's dst dynamic offset
  8684. * f) branch target address
  8685. *
  8686. * Note: b-e are omitted when arity is 0 so that
  8687. * interpreter can recover the br info quickly.
  8688. */
  8689. BlockType *block_type = &frame_csp->block_type;
  8690. uint8 *types = NULL, cell;
  8691. #if WASM_ENABLE_GC != 0
  8692. WASMRefTypeMap *reftype_maps;
  8693. uint32 reftype_map_count;
  8694. #endif
  8695. uint32 arity = 0;
  8696. int32 i;
  8697. int16 *frame_offset = ctx->frame_offset;
  8698. uint16 dynamic_offset;
  8699. /* Note: loop's arity is different from if and block. loop's arity is
  8700. * its parameter count while if and block arity is result count.
  8701. */
  8702. #if WASM_ENABLE_GC == 0
  8703. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8704. arity = block_type_get_param_types(block_type, &types);
  8705. else
  8706. arity = block_type_get_result_types(block_type, &types);
  8707. #else
  8708. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8709. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8710. &reftype_map_count);
  8711. else
  8712. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8713. &reftype_map_count);
  8714. #endif
  8715. /* Part a */
  8716. emit_uint32(ctx, arity);
  8717. if (arity) {
  8718. /* Part b */
  8719. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8720. /* Part c */
  8721. for (i = (int32)arity - 1; i >= 0; i--) {
  8722. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8723. emit_byte(ctx, cell);
  8724. }
  8725. /* Part d */
  8726. for (i = (int32)arity - 1; i >= 0; i--) {
  8727. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8728. frame_offset -= cell;
  8729. emit_operand(ctx, *(int16 *)(frame_offset));
  8730. }
  8731. /* Part e */
  8732. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8733. /* Use start_dynamic_offset which was set in
  8734. copy_params_to_dynamic_space */
  8735. dynamic_offset = frame_csp->start_dynamic_offset
  8736. + wasm_get_cell_num(types, arity);
  8737. else
  8738. dynamic_offset =
  8739. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8740. if (is_br)
  8741. ctx->dynamic_offset = dynamic_offset;
  8742. for (i = (int32)arity - 1; i >= 0; i--) {
  8743. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8744. dynamic_offset -= cell;
  8745. emit_operand(ctx, dynamic_offset);
  8746. }
  8747. }
  8748. /* Part f */
  8749. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8750. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8751. }
  8752. else {
  8753. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8754. error_buf, error_buf_size))
  8755. return false;
  8756. /* label address, to be patched */
  8757. wasm_loader_emit_ptr(ctx, NULL);
  8758. }
  8759. return true;
  8760. }
  8761. static bool
  8762. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8763. bool disable_emit, int16 operand_offset,
  8764. char *error_buf, uint32 error_buf_size)
  8765. {
  8766. uint32 cell_num_to_push, i;
  8767. if (type == VALUE_TYPE_VOID)
  8768. return true;
  8769. /* only check memory overflow in first traverse */
  8770. if (ctx->p_code_compiled == NULL) {
  8771. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8772. return false;
  8773. }
  8774. if (disable_emit)
  8775. *(ctx->frame_offset)++ = operand_offset;
  8776. else {
  8777. emit_operand(ctx, ctx->dynamic_offset);
  8778. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8779. ctx->dynamic_offset++;
  8780. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8781. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8782. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8783. goto fail;
  8784. }
  8785. }
  8786. }
  8787. if (is_32bit_type(type))
  8788. return true;
  8789. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8790. for (i = 0; i < cell_num_to_push; i++) {
  8791. if (ctx->p_code_compiled == NULL) {
  8792. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8793. return false;
  8794. }
  8795. ctx->frame_offset++;
  8796. if (!disable_emit) {
  8797. ctx->dynamic_offset++;
  8798. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8799. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8800. if (ctx->max_dynamic_offset >= INT16_MAX)
  8801. goto fail;
  8802. }
  8803. }
  8804. }
  8805. return true;
  8806. fail:
  8807. set_error_buf(error_buf, error_buf_size,
  8808. "fast interpreter offset overflow");
  8809. return false;
  8810. }
  8811. /* This function should be in front of wasm_loader_pop_frame_ref
  8812. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8813. will be modified by wasm_loader_pop_frame_ref */
  8814. static bool
  8815. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8816. char *error_buf, uint32 error_buf_size)
  8817. {
  8818. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8819. then current block is the function block */
  8820. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8821. BranchBlock *cur_block = ctx->frame_csp - depth;
  8822. int32 available_stack_cell =
  8823. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8824. uint32 cell_num_to_pop;
  8825. /* Directly return success if current block is in stack
  8826. polymorphic state while stack is empty. */
  8827. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8828. return true;
  8829. if (type == VALUE_TYPE_VOID)
  8830. return true;
  8831. /* Change type to ANY when the stack top is ANY, so as to avoid
  8832. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8833. two offsets */
  8834. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8835. type = VALUE_TYPE_ANY;
  8836. cell_num_to_pop = wasm_value_type_cell_num(type);
  8837. /* Check the offset stack bottom to ensure the frame offset
  8838. stack will not go underflow. But we don't thrown error
  8839. and return true here, because the error msg should be
  8840. given in wasm_loader_pop_frame_ref */
  8841. if (!check_offset_pop(ctx, cell_num_to_pop))
  8842. return true;
  8843. ctx->frame_offset -= cell_num_to_pop;
  8844. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8845. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8846. ctx->dynamic_offset -= cell_num_to_pop;
  8847. emit_operand(ctx, *(ctx->frame_offset));
  8848. (void)error_buf;
  8849. (void)error_buf_size;
  8850. return true;
  8851. }
  8852. static bool
  8853. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8854. bool disable_emit, int16 operand_offset,
  8855. char *error_buf, uint32 error_buf_size)
  8856. {
  8857. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8858. error_buf, error_buf_size)))
  8859. return false;
  8860. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8861. return false;
  8862. return true;
  8863. }
  8864. static bool
  8865. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8866. char *error_buf, uint32 error_buf_size)
  8867. {
  8868. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8869. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8870. return false;
  8871. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8872. return false;
  8873. return true;
  8874. }
  8875. static bool
  8876. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8877. uint8 type_push, uint8 type_pop,
  8878. bool disable_emit, int16 operand_offset,
  8879. char *error_buf, uint32 error_buf_size)
  8880. {
  8881. uint8 i;
  8882. for (i = 0; i < pop_cnt; i++) {
  8883. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8884. error_buf_size))
  8885. return false;
  8886. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8887. error_buf_size))
  8888. return false;
  8889. }
  8890. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8891. operand_offset, error_buf,
  8892. error_buf_size))
  8893. return false;
  8894. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8895. return false;
  8896. return true;
  8897. }
  8898. static int
  8899. cmp_i64_const(const void *p_i64_const1, const void *p_i64_const2)
  8900. {
  8901. int64 i64_const1 = *(int64 *)p_i64_const1;
  8902. int64 i64_const2 = *(int64 *)p_i64_const2;
  8903. return (i64_const1 < i64_const2) ? -1 : (i64_const1 > i64_const2) ? 1 : 0;
  8904. }
  8905. static int
  8906. cmp_i32_const(const void *p_i32_const1, const void *p_i32_const2)
  8907. {
  8908. int32 i32_const1 = *(int32 *)p_i32_const1;
  8909. int32 i32_const2 = *(int32 *)p_i32_const2;
  8910. return (i32_const1 < i32_const2) ? -1 : (i32_const1 > i32_const2) ? 1 : 0;
  8911. }
  8912. static int
  8913. cmp_v128_const(const void *p_v128_const1, const void *p_v128_const2)
  8914. {
  8915. V128 v128_const1 = *(V128 *)p_v128_const1;
  8916. V128 v128_const2 = *(V128 *)p_v128_const2;
  8917. return memcmp(&v128_const1, &v128_const2, sizeof(V128));
  8918. }
  8919. static bool
  8920. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8921. int16 *offset, char *error_buf,
  8922. uint32 error_buf_size)
  8923. {
  8924. if (!ctx->p_code_compiled) {
  8925. /* Treat i64 and f64 as the same by reading i64 value from
  8926. the raw bytes */
  8927. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8928. /* No slot left, emit const instead */
  8929. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 2) {
  8930. *offset = 0;
  8931. return true;
  8932. }
  8933. /* Traverse the list if the const num is small */
  8934. if (ctx->i64_const_num < 10) {
  8935. for (uint32 i = 0; i < ctx->i64_const_num; i++) {
  8936. if (ctx->i64_consts[i] == *(int64 *)value) {
  8937. *offset = -1;
  8938. return true;
  8939. }
  8940. }
  8941. }
  8942. if (ctx->i64_const_num >= ctx->i64_const_max_num) {
  8943. MEM_REALLOC(ctx->i64_consts,
  8944. sizeof(int64) * ctx->i64_const_max_num,
  8945. sizeof(int64) * (ctx->i64_const_max_num * 2));
  8946. ctx->i64_const_max_num *= 2;
  8947. }
  8948. ctx->i64_consts[ctx->i64_const_num++] = *(int64 *)value;
  8949. }
  8950. else if (type == VALUE_TYPE_V128) {
  8951. /* No slot left, emit const instead */
  8952. if (ctx->v128_const_num * 4 > INT16_MAX - 2) {
  8953. *offset = 0;
  8954. return true;
  8955. }
  8956. /* Traverse the list if the const num is small */
  8957. if (ctx->v128_const_num < 10) {
  8958. for (uint32 i = 0; i < ctx->v128_const_num; i++) {
  8959. if (memcmp(&ctx->v128_consts[i], value, sizeof(V128))
  8960. == 0) {
  8961. *offset = -1;
  8962. return true;
  8963. }
  8964. }
  8965. }
  8966. if (ctx->v128_const_num >= ctx->v128_const_max_num) {
  8967. MEM_REALLOC(ctx->v128_consts,
  8968. sizeof(V128) * ctx->v128_const_max_num,
  8969. sizeof(V128) * (ctx->v128_const_max_num * 2));
  8970. ctx->v128_const_max_num *= 2;
  8971. }
  8972. ctx->v128_consts[ctx->v128_const_num++] = *(V128 *)value;
  8973. }
  8974. else {
  8975. /* Treat i32 and f32 as the same by reading i32 value from
  8976. the raw bytes */
  8977. bh_assert(type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32);
  8978. /* No slot left, emit const instead */
  8979. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 1) {
  8980. *offset = 0;
  8981. return true;
  8982. }
  8983. /* Traverse the list if the const num is small */
  8984. if (ctx->i32_const_num < 10) {
  8985. for (uint32 i = 0; i < ctx->i32_const_num; i++) {
  8986. if (ctx->i32_consts[i] == *(int32 *)value) {
  8987. *offset = -1;
  8988. return true;
  8989. }
  8990. }
  8991. }
  8992. if (ctx->i32_const_num >= ctx->i32_const_max_num) {
  8993. MEM_REALLOC(ctx->i32_consts,
  8994. sizeof(int32) * ctx->i32_const_max_num,
  8995. sizeof(int32) * (ctx->i32_const_max_num * 2));
  8996. ctx->i32_const_max_num *= 2;
  8997. }
  8998. ctx->i32_consts[ctx->i32_const_num++] = *(int32 *)value;
  8999. }
  9000. *offset = -1;
  9001. return true;
  9002. }
  9003. else {
  9004. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  9005. int64 key = *(int64 *)value, *i64_const;
  9006. i64_const = bsearch(&key, ctx->i64_consts, ctx->i64_const_num,
  9007. sizeof(int64), cmp_i64_const);
  9008. if (!i64_const) { /* not found, emit const instead */
  9009. *offset = 0;
  9010. return true;
  9011. }
  9012. /* constant index is encoded as negative value */
  9013. *offset = -(int32)(ctx->i64_const_num * 2 + ctx->i32_const_num)
  9014. + (int32)(i64_const - ctx->i64_consts) * 2;
  9015. }
  9016. else if (type == VALUE_TYPE_V128) {
  9017. V128 key = *(V128 *)value, *v128_const;
  9018. v128_const = bsearch(&key, ctx->v128_consts, ctx->v128_const_num,
  9019. sizeof(V128), cmp_v128_const);
  9020. if (!v128_const) { /* not found, emit const instead */
  9021. *offset = 0;
  9022. return true;
  9023. }
  9024. /* constant index is encoded as negative value */
  9025. *offset = -(int32)(ctx->v128_const_num)
  9026. + (int32)(v128_const - ctx->v128_consts);
  9027. }
  9028. else {
  9029. int32 key = *(int32 *)value, *i32_const;
  9030. i32_const = bsearch(&key, ctx->i32_consts, ctx->i32_const_num,
  9031. sizeof(int32), cmp_i32_const);
  9032. if (!i32_const) { /* not found, emit const instead */
  9033. *offset = 0;
  9034. return true;
  9035. }
  9036. /* constant index is encoded as negative value */
  9037. *offset = -(int32)(ctx->i32_const_num)
  9038. + (int32)(i32_const - ctx->i32_consts);
  9039. }
  9040. return true;
  9041. }
  9042. fail:
  9043. return false;
  9044. }
  9045. /*
  9046. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  9047. -- Mostly used for the binary / compare operation
  9048. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  9049. -- Mostly used in block / control instructions
  9050. The POP will always emit the offset on the top of the frame_offset stack
  9051. PUSH can be used in two ways:
  9052. 1. directly PUSH:
  9053. PUSH_XXX();
  9054. will allocate a dynamic space and emit
  9055. 2. silent PUSH:
  9056. operand_offset = xxx; disable_emit = true;
  9057. PUSH_XXX();
  9058. only push the frame_offset stack, no emit
  9059. */
  9060. #define TEMPLATE_PUSH(Type) \
  9061. do { \
  9062. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  9063. disable_emit, operand_offset, \
  9064. error_buf, error_buf_size)) \
  9065. goto fail; \
  9066. } while (0)
  9067. #define TEMPLATE_PUSH_REF(Type) \
  9068. do { \
  9069. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  9070. operand_offset, error_buf, \
  9071. error_buf_size)) \
  9072. goto fail; \
  9073. } while (0)
  9074. #define TEMPLATE_POP(Type) \
  9075. do { \
  9076. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  9077. error_buf, error_buf_size)) \
  9078. goto fail; \
  9079. } while (0)
  9080. #define TEMPLATE_POP_REF(Type) \
  9081. do { \
  9082. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  9083. error_buf_size)) \
  9084. goto fail; \
  9085. } while (0)
  9086. #define PUSH_OFFSET_TYPE(type) \
  9087. do { \
  9088. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  9089. operand_offset, error_buf, \
  9090. error_buf_size))) \
  9091. goto fail; \
  9092. } while (0)
  9093. #define POP_OFFSET_TYPE(type) \
  9094. do { \
  9095. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  9096. error_buf_size))) \
  9097. goto fail; \
  9098. } while (0)
  9099. #define POP_AND_PUSH(type_pop, type_push) \
  9100. do { \
  9101. if (!(wasm_loader_push_pop_frame_ref_offset( \
  9102. loader_ctx, 1, type_push, type_pop, disable_emit, \
  9103. operand_offset, error_buf, error_buf_size))) \
  9104. goto fail; \
  9105. } while (0)
  9106. /* type of POPs should be the same */
  9107. #define POP2_AND_PUSH(type_pop, type_push) \
  9108. do { \
  9109. if (!(wasm_loader_push_pop_frame_ref_offset( \
  9110. loader_ctx, 2, type_push, type_pop, disable_emit, \
  9111. operand_offset, error_buf, error_buf_size))) \
  9112. goto fail; \
  9113. } while (0)
  9114. #else /* WASM_ENABLE_FAST_INTERP */
  9115. #define TEMPLATE_PUSH(Type) \
  9116. do { \
  9117. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  9118. error_buf, error_buf_size))) \
  9119. goto fail; \
  9120. } while (0)
  9121. #define TEMPLATE_PUSH_REF(Type) \
  9122. do { \
  9123. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  9124. error_buf_size))) \
  9125. goto fail; \
  9126. } while (0)
  9127. #define TEMPLATE_POP(Type) \
  9128. do { \
  9129. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  9130. error_buf, error_buf_size))) \
  9131. goto fail; \
  9132. } while (0)
  9133. #define TEMPLATE_POP_REF(Type) \
  9134. do { \
  9135. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  9136. error_buf_size))) \
  9137. goto fail; \
  9138. } while (0)
  9139. #define POP_AND_PUSH(type_pop, type_push) \
  9140. do { \
  9141. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  9142. type_pop, error_buf, \
  9143. error_buf_size))) \
  9144. goto fail; \
  9145. } while (0)
  9146. /* type of POPs should be the same */
  9147. #define POP2_AND_PUSH(type_pop, type_push) \
  9148. do { \
  9149. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  9150. type_pop, error_buf, \
  9151. error_buf_size))) \
  9152. goto fail; \
  9153. } while (0)
  9154. #endif /* WASM_ENABLE_FAST_INTERP */
  9155. #define PUSH_I32() TEMPLATE_PUSH(I32)
  9156. #define PUSH_F32() TEMPLATE_PUSH(F32)
  9157. #define PUSH_I64() TEMPLATE_PUSH(I64)
  9158. #define PUSH_F64() TEMPLATE_PUSH(F64)
  9159. #define PUSH_V128() TEMPLATE_PUSH(V128)
  9160. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  9161. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  9162. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  9163. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  9164. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  9165. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  9166. #define PUSH_TBL_ELEM_IDX() TEMPLATE_PUSH_REF(table_elem_idx_type)
  9167. #define POP_I32() TEMPLATE_POP(I32)
  9168. #define POP_F32() TEMPLATE_POP(F32)
  9169. #define POP_I64() TEMPLATE_POP(I64)
  9170. #define POP_F64() TEMPLATE_POP(F64)
  9171. #define POP_V128() TEMPLATE_POP(V128)
  9172. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  9173. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  9174. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  9175. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  9176. #define POP_TBL_ELEM_IDX() TEMPLATE_POP_REF(table_elem_idx_type)
  9177. #if WASM_ENABLE_FAST_INTERP != 0
  9178. static bool
  9179. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  9180. bool disable_emit, char *error_buf, uint32 error_buf_size)
  9181. {
  9182. int16 operand_offset = 0;
  9183. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  9184. : loader_ctx->frame_csp;
  9185. BlockType *block_type = &block->block_type;
  9186. uint8 *return_types = NULL;
  9187. #if WASM_ENABLE_GC != 0
  9188. WASMRefTypeMap *reftype_maps = NULL;
  9189. uint32 reftype_map_count;
  9190. #endif
  9191. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  9192. int32 i = 0;
  9193. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  9194. *frame_offset_org = NULL;
  9195. #if WASM_ENABLE_GC == 0
  9196. return_count = block_type_get_result_types(block_type, &return_types);
  9197. #else
  9198. return_count = block_type_get_result_types(
  9199. block_type, &return_types, &reftype_maps, &reftype_map_count);
  9200. #endif
  9201. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64/V128
  9202. * instead of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  9203. if (return_count == 1) {
  9204. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  9205. if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  9206. /* insert op_copy before else opcode */
  9207. if (opcode == WASM_OP_ELSE)
  9208. skip_label();
  9209. #if WASM_ENABLE_SIMDE != 0
  9210. if (cell == 4) {
  9211. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  9212. }
  9213. #endif
  9214. if (cell <= 2) {
  9215. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  9216. : EXT_OP_COPY_STACK_TOP_I64);
  9217. }
  9218. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  9219. emit_operand(loader_ctx, block->dynamic_offset);
  9220. if (opcode == WASM_OP_ELSE) {
  9221. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  9222. }
  9223. else {
  9224. loader_ctx->frame_offset -= cell;
  9225. loader_ctx->dynamic_offset = block->dynamic_offset;
  9226. PUSH_OFFSET_TYPE(return_types[0]);
  9227. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9228. }
  9229. if (opcode == WASM_OP_ELSE)
  9230. emit_label(opcode);
  9231. }
  9232. return true;
  9233. }
  9234. /* Copy stack top values to block's results which are in dynamic space.
  9235. * The instruction format:
  9236. * Part a: values count
  9237. * Part b: all values total cell num
  9238. * Part c: each value's cell_num, src offset and dst offset
  9239. * Part d: each value's src offset and dst offset
  9240. * Part e: each value's dst offset
  9241. */
  9242. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  9243. dynamic_offset = dynamic_offset_org =
  9244. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  9245. /* First traversal to get the count of values needed to be copied. */
  9246. for (i = (int32)return_count - 1; i >= 0; i--) {
  9247. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  9248. if (frame_offset - cells < loader_ctx->frame_offset_bottom) {
  9249. set_error_buf(error_buf, error_buf_size, "frame offset underflow");
  9250. goto fail;
  9251. }
  9252. if (cells == 4) {
  9253. bool needs_copy = false;
  9254. int16 v128_dynamic = dynamic_offset - cells;
  9255. for (int j = 0; j < 4; j++) {
  9256. if (*(frame_offset - j - 1) != (v128_dynamic + j)) {
  9257. needs_copy = true;
  9258. break;
  9259. }
  9260. }
  9261. if (needs_copy) {
  9262. value_count++;
  9263. total_cel_num += cells;
  9264. }
  9265. frame_offset -= cells;
  9266. dynamic_offset = v128_dynamic;
  9267. }
  9268. else {
  9269. frame_offset -= cells;
  9270. dynamic_offset -= cells;
  9271. if (dynamic_offset != *frame_offset) {
  9272. value_count++;
  9273. total_cel_num += cells;
  9274. }
  9275. }
  9276. }
  9277. if (value_count) {
  9278. uint32 j = 0;
  9279. uint8 *emit_data = NULL, *cells = NULL;
  9280. int16 *src_offsets = NULL;
  9281. uint16 *dst_offsets = NULL;
  9282. uint64 size =
  9283. (uint64)value_count
  9284. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  9285. /* Allocate memory for the emit data */
  9286. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9287. return false;
  9288. cells = emit_data;
  9289. src_offsets = (int16 *)(cells + value_count);
  9290. dst_offsets = (uint16 *)(src_offsets + value_count);
  9291. /* insert op_copy before else opcode */
  9292. if (opcode == WASM_OP_ELSE)
  9293. skip_label();
  9294. emit_label(EXT_OP_COPY_STACK_VALUES);
  9295. /* Part a) */
  9296. emit_uint32(loader_ctx, value_count);
  9297. /* Part b) */
  9298. emit_uint32(loader_ctx, total_cel_num);
  9299. /* Second traversal to get each value's cell num, src offset and dst
  9300. * offset. */
  9301. frame_offset = frame_offset_org;
  9302. dynamic_offset = dynamic_offset_org;
  9303. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  9304. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  9305. if (cell == 4) {
  9306. bool needs_copy = false;
  9307. int16 v128_dynamic = dynamic_offset - cell;
  9308. for (int k = 0; k < 4; k++) {
  9309. if (*(frame_offset - k - 1) != (v128_dynamic + k)) {
  9310. needs_copy = true;
  9311. break;
  9312. }
  9313. }
  9314. if (needs_copy) {
  9315. cells[j] = cell;
  9316. src_offsets[j] = *(frame_offset - cell);
  9317. dst_offsets[j] = v128_dynamic;
  9318. j++;
  9319. }
  9320. frame_offset -= cell;
  9321. dynamic_offset = v128_dynamic;
  9322. }
  9323. else {
  9324. frame_offset -= cell;
  9325. dynamic_offset -= cell;
  9326. if (dynamic_offset != *frame_offset) {
  9327. cells[j] = cell;
  9328. /* src offset */
  9329. src_offsets[j] = *frame_offset;
  9330. /* dst offset */
  9331. dst_offsets[j] = dynamic_offset;
  9332. j++;
  9333. }
  9334. }
  9335. if (opcode == WASM_OP_ELSE) {
  9336. if (cell == 4) {
  9337. for (int k = 0; k < cell; k++) {
  9338. *(frame_offset + k) = dynamic_offset + k;
  9339. }
  9340. }
  9341. else {
  9342. *frame_offset = dynamic_offset;
  9343. }
  9344. }
  9345. else {
  9346. loader_ctx->frame_offset = frame_offset;
  9347. loader_ctx->dynamic_offset = dynamic_offset;
  9348. if (!(wasm_loader_push_frame_offset(
  9349. loader_ctx, return_types[i], disable_emit,
  9350. operand_offset, error_buf, error_buf_size))) {
  9351. wasm_runtime_free(emit_data);
  9352. goto fail;
  9353. }
  9354. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9355. loader_ctx->frame_offset = frame_offset_org;
  9356. loader_ctx->dynamic_offset = dynamic_offset_org;
  9357. }
  9358. }
  9359. bh_assert(j == value_count);
  9360. /* Emit the cells, src_offsets and dst_offsets */
  9361. for (j = 0; j < value_count; j++)
  9362. emit_byte(loader_ctx, cells[j]);
  9363. for (j = 0; j < value_count; j++)
  9364. emit_operand(loader_ctx, src_offsets[j]);
  9365. for (j = 0; j < value_count; j++)
  9366. emit_operand(loader_ctx, dst_offsets[j]);
  9367. if (opcode == WASM_OP_ELSE)
  9368. emit_label(opcode);
  9369. wasm_runtime_free(emit_data);
  9370. }
  9371. return true;
  9372. fail:
  9373. return false;
  9374. }
  9375. #endif /* WASM_ENABLE_FAST_INTERP */
  9376. #define PUSH_TYPE(type) \
  9377. do { \
  9378. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  9379. error_buf_size))) \
  9380. goto fail; \
  9381. } while (0)
  9382. #define POP_TYPE(type) \
  9383. do { \
  9384. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  9385. error_buf_size))) \
  9386. goto fail; \
  9387. } while (0)
  9388. #if WASM_ENABLE_GC == 0
  9389. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9390. do { \
  9391. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9392. _start_addr, error_buf, \
  9393. error_buf_size)) \
  9394. goto fail; \
  9395. } while (0)
  9396. #define POP_CSP() \
  9397. do { \
  9398. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9399. goto fail; \
  9400. } while (0)
  9401. #else
  9402. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9403. do { \
  9404. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9405. _start_addr, error_buf, \
  9406. error_buf_size)) \
  9407. goto fail; \
  9408. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  9409. error_buf, error_buf_size)) { \
  9410. goto fail; \
  9411. } \
  9412. } while (0)
  9413. #define POP_CSP() \
  9414. do { \
  9415. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  9416. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9417. goto fail; \
  9418. } while (0)
  9419. #endif /* end of WASM_ENABLE_GC == 0 */
  9420. #if WASM_ENABLE_GC == 0
  9421. #define GET_LOCAL_REFTYPE() (void)0
  9422. #else
  9423. #define GET_LOCAL_REFTYPE() \
  9424. do { \
  9425. if (wasm_is_type_multi_byte_type(local_type)) { \
  9426. WASMRefType *_ref_type; \
  9427. if (local_idx < param_count) \
  9428. _ref_type = wasm_reftype_map_find( \
  9429. param_reftype_maps, param_reftype_map_count, local_idx); \
  9430. else \
  9431. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  9432. local_reftype_map_count, \
  9433. local_idx - param_count); \
  9434. bh_assert(_ref_type); \
  9435. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  9436. wasm_reftype_struct_size(_ref_type)); \
  9437. } \
  9438. } while (0)
  9439. #endif /* end of WASM_ENABLE_GC == 0 */
  9440. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  9441. do { \
  9442. read_leb_uint32(p, p_end, local_idx); \
  9443. if (local_idx >= param_count + local_count) { \
  9444. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  9445. goto fail; \
  9446. } \
  9447. local_type = local_idx < param_count \
  9448. ? param_types[local_idx] \
  9449. : local_types[local_idx - param_count]; \
  9450. local_offset = local_offsets[local_idx]; \
  9451. GET_LOCAL_REFTYPE(); \
  9452. } while (0)
  9453. static bool
  9454. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  9455. {
  9456. if (module->memory_count == 0 && module->import_memory_count == 0) {
  9457. set_error_buf(error_buf, error_buf_size, "unknown memory");
  9458. return false;
  9459. }
  9460. return true;
  9461. }
  9462. #define CHECK_MEMORY() \
  9463. do { \
  9464. if (!check_memory(module, error_buf, error_buf_size)) \
  9465. goto fail; \
  9466. } while (0)
  9467. static bool
  9468. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9469. uint32 error_buf_size)
  9470. {
  9471. uint8 mem_access_aligns[] = {
  9472. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  9473. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  9474. };
  9475. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  9476. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  9477. set_error_buf(error_buf, error_buf_size,
  9478. "invalid memop flags: alignment must not be larger "
  9479. "than natural");
  9480. return false;
  9481. }
  9482. return true;
  9483. }
  9484. #if WASM_ENABLE_SIMD != 0
  9485. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  9486. || (WASM_ENABLE_FAST_INTERP != 0)
  9487. static bool
  9488. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9489. uint32 error_buf_size)
  9490. {
  9491. uint8 mem_access_aligns[] = {
  9492. 4, /* load */
  9493. 3, 3, 3, 3, 3, 3, /* load and extend */
  9494. 0, 1, 2, 3, /* load and splat */
  9495. 4, /* store */
  9496. };
  9497. uint8 mem_access_aligns_load_lane[] = {
  9498. 0, 1, 2, 3, /* load lane */
  9499. 0, 1, 2, 3, /* store lane */
  9500. 2, 3 /* store zero */
  9501. };
  9502. if (!((opcode <= SIMD_v128_store)
  9503. || (SIMD_v128_load8_lane <= opcode
  9504. && opcode <= SIMD_v128_load64_zero))) {
  9505. set_error_buf(error_buf, error_buf_size,
  9506. "the opcode doesn't include memarg");
  9507. return false;
  9508. }
  9509. if ((opcode <= SIMD_v128_store
  9510. && align > mem_access_aligns[opcode - SIMD_v128_load])
  9511. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  9512. && align > mem_access_aligns_load_lane[opcode
  9513. - SIMD_v128_load8_lane])) {
  9514. set_error_buf(error_buf, error_buf_size,
  9515. "invalid memop flags: alignment must not be larger "
  9516. "than natural");
  9517. return false;
  9518. }
  9519. return true;
  9520. }
  9521. static bool
  9522. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  9523. uint32 error_buf_size)
  9524. {
  9525. switch (opcode) {
  9526. case SIMD_i8x16_extract_lane_s:
  9527. case SIMD_i8x16_extract_lane_u:
  9528. case SIMD_i8x16_replace_lane:
  9529. if (lane >= 16) {
  9530. goto fail;
  9531. }
  9532. break;
  9533. case SIMD_i16x8_extract_lane_s:
  9534. case SIMD_i16x8_extract_lane_u:
  9535. case SIMD_i16x8_replace_lane:
  9536. if (lane >= 8) {
  9537. goto fail;
  9538. }
  9539. break;
  9540. case SIMD_i32x4_extract_lane:
  9541. case SIMD_i32x4_replace_lane:
  9542. case SIMD_f32x4_extract_lane:
  9543. case SIMD_f32x4_replace_lane:
  9544. if (lane >= 4) {
  9545. goto fail;
  9546. }
  9547. break;
  9548. case SIMD_i64x2_extract_lane:
  9549. case SIMD_i64x2_replace_lane:
  9550. case SIMD_f64x2_extract_lane:
  9551. case SIMD_f64x2_replace_lane:
  9552. if (lane >= 2) {
  9553. goto fail;
  9554. }
  9555. break;
  9556. case SIMD_v128_load8_lane:
  9557. case SIMD_v128_load16_lane:
  9558. case SIMD_v128_load32_lane:
  9559. case SIMD_v128_load64_lane:
  9560. case SIMD_v128_store8_lane:
  9561. case SIMD_v128_store16_lane:
  9562. case SIMD_v128_store32_lane:
  9563. case SIMD_v128_store64_lane:
  9564. case SIMD_v128_load32_zero:
  9565. case SIMD_v128_load64_zero:
  9566. {
  9567. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9568. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9569. goto fail;
  9570. }
  9571. break;
  9572. }
  9573. default:
  9574. goto fail;
  9575. }
  9576. return true;
  9577. fail:
  9578. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9579. return false;
  9580. }
  9581. static bool
  9582. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9583. {
  9584. uint8 i;
  9585. for (i = 0; i != 16; ++i) {
  9586. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9587. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9588. return false;
  9589. }
  9590. }
  9591. return true;
  9592. }
  9593. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9594. #endif /* end of WASM_ENABLE_SIMD */
  9595. #if WASM_ENABLE_SHARED_MEMORY != 0
  9596. static bool
  9597. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9598. uint32 error_buf_size)
  9599. {
  9600. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9601. uint8 mem_access_aligns[] = {
  9602. 2, 3, 0, 1, 0, 1, 2,
  9603. };
  9604. uint8 expect;
  9605. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9606. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9607. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9608. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9609. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9610. }
  9611. else {
  9612. /* 7 opcodes in every group */
  9613. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9614. }
  9615. if (align != expect) {
  9616. set_error_buf(error_buf, error_buf_size,
  9617. "alignment isn't equal to natural");
  9618. return false;
  9619. }
  9620. return true;
  9621. }
  9622. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9623. static bool
  9624. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9625. char *error_buf, uint32 error_buf_size)
  9626. {
  9627. BranchBlock *target_block, *cur_block;
  9628. BlockType *target_block_type;
  9629. uint8 type, *types = NULL, *frame_ref;
  9630. uint32 arity = 0;
  9631. int32 i, available_stack_cell;
  9632. uint16 cell_num;
  9633. #if WASM_ENABLE_GC != 0
  9634. WASMRefTypeMap *frame_reftype_map;
  9635. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9636. WASMRefType *ref_type;
  9637. uint32 reftype_map_count = 0;
  9638. int32 available_reftype_map;
  9639. bool is_type_multi_byte;
  9640. #endif
  9641. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9642. uint8 *frame_ref_after_popped = NULL;
  9643. uint8 frame_ref_tmp[4] = { 0 };
  9644. uint8 *frame_ref_buf = frame_ref_tmp;
  9645. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9646. #if WASM_ENABLE_GC != 0
  9647. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9648. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9649. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9650. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9651. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9652. #endif
  9653. #if WASM_ENABLE_FAST_INTERP != 0
  9654. int16 *frame_offset_old = loader_ctx->frame_offset;
  9655. int16 *frame_offset_after_popped = NULL;
  9656. int16 frame_offset_tmp[4] = { 0 };
  9657. int16 *frame_offset_buf = frame_offset_tmp;
  9658. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9659. #endif
  9660. bool ret = false;
  9661. bh_assert(loader_ctx->csp_num > 0);
  9662. if (loader_ctx->csp_num - 1 < depth) {
  9663. set_error_buf(error_buf, error_buf_size,
  9664. "unknown label, "
  9665. "unexpected end of section or function");
  9666. return false;
  9667. }
  9668. cur_block = loader_ctx->frame_csp - 1;
  9669. target_block = loader_ctx->frame_csp - (depth + 1);
  9670. target_block_type = &target_block->block_type;
  9671. frame_ref = loader_ctx->frame_ref;
  9672. #if WASM_ENABLE_GC != 0
  9673. frame_reftype_map = loader_ctx->frame_reftype_map;
  9674. #endif
  9675. /* Note: loop's arity is different from if and block. loop's arity is
  9676. * its parameter count while if and block arity is result count.
  9677. */
  9678. #if WASM_ENABLE_GC == 0
  9679. if (target_block->label_type == LABEL_TYPE_LOOP)
  9680. arity = block_type_get_param_types(target_block_type, &types);
  9681. else
  9682. arity = block_type_get_result_types(target_block_type, &types);
  9683. #else
  9684. if (target_block->label_type == LABEL_TYPE_LOOP)
  9685. arity = block_type_get_param_types(target_block_type, &types,
  9686. &reftype_maps, &reftype_map_count);
  9687. else
  9688. arity = block_type_get_result_types(target_block_type, &types,
  9689. &reftype_maps, &reftype_map_count);
  9690. #endif
  9691. /* If the stack is in polymorphic state, just clear the stack
  9692. * and then re-push the values to make the stack top values
  9693. * match block type. */
  9694. if (cur_block->is_stack_polymorphic) {
  9695. #if WASM_ENABLE_GC != 0
  9696. int32 j = (int32)reftype_map_count - 1;
  9697. #endif
  9698. for (i = (int32)arity - 1; i >= 0; i--) {
  9699. #if WASM_ENABLE_GC != 0
  9700. if (wasm_is_type_multi_byte_type(types[i])) {
  9701. bh_assert(reftype_maps[j].index == i);
  9702. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9703. reftype_maps[j].ref_type,
  9704. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9705. j--;
  9706. }
  9707. #endif
  9708. #if WASM_ENABLE_FAST_INTERP != 0
  9709. POP_OFFSET_TYPE(types[i]);
  9710. #endif
  9711. POP_TYPE(types[i]);
  9712. }
  9713. /* Backup stack data since it may be changed in the below
  9714. push operations, and the stack data may be used when
  9715. checking other target blocks of opcode br_table */
  9716. if (opcode == WASM_OP_BR_TABLE) {
  9717. uint64 total_size;
  9718. frame_ref_after_popped = loader_ctx->frame_ref;
  9719. total_size = (uint64)sizeof(uint8)
  9720. * (frame_ref_old - frame_ref_after_popped);
  9721. if (total_size > sizeof(frame_ref_tmp)
  9722. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9723. error_buf_size))) {
  9724. goto fail;
  9725. }
  9726. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9727. frame_ref_after_popped, (uint32)total_size);
  9728. #if WASM_ENABLE_GC != 0
  9729. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9730. total_size =
  9731. (uint64)sizeof(WASMRefTypeMap)
  9732. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9733. if (total_size > sizeof(frame_reftype_map_tmp)
  9734. && !(frame_reftype_map_buf = loader_malloc(
  9735. total_size, error_buf, error_buf_size))) {
  9736. goto fail;
  9737. }
  9738. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9739. frame_reftype_map_after_popped, (uint32)total_size);
  9740. #endif
  9741. #if WASM_ENABLE_FAST_INTERP != 0
  9742. frame_offset_after_popped = loader_ctx->frame_offset;
  9743. total_size = (uint64)sizeof(int16)
  9744. * (frame_offset_old - frame_offset_after_popped);
  9745. if (total_size > sizeof(frame_offset_tmp)
  9746. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9747. error_buf_size))) {
  9748. goto fail;
  9749. }
  9750. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9751. frame_offset_after_popped, (uint32)total_size);
  9752. #endif
  9753. }
  9754. #if WASM_ENABLE_GC != 0
  9755. j = 0;
  9756. #endif
  9757. for (i = 0; i < (int32)arity; i++) {
  9758. #if WASM_ENABLE_GC != 0
  9759. if (wasm_is_type_multi_byte_type(types[i])) {
  9760. bh_assert(reftype_maps[j].index == i);
  9761. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9762. reftype_maps[j].ref_type,
  9763. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9764. j++;
  9765. }
  9766. #endif
  9767. #if WASM_ENABLE_FAST_INTERP != 0
  9768. bool disable_emit = true;
  9769. int16 operand_offset = 0;
  9770. PUSH_OFFSET_TYPE(types[i]);
  9771. #endif
  9772. PUSH_TYPE(types[i]);
  9773. }
  9774. #if WASM_ENABLE_FAST_INTERP != 0
  9775. emit_br_info(target_block, opcode == WASM_OP_BR);
  9776. #endif
  9777. /* Restore the stack data, note that frame_ref_bottom,
  9778. frame_reftype_map_bottom, frame_offset_bottom may be
  9779. re-allocated in the above push operations */
  9780. if (opcode == WASM_OP_BR_TABLE) {
  9781. uint32 total_size;
  9782. /* The stack operand num should not be smaller than before
  9783. after pop and push operations */
  9784. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9785. loader_ctx->stack_cell_num = stack_cell_num_old;
  9786. loader_ctx->frame_ref =
  9787. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9788. total_size = (uint32)(sizeof(uint8)
  9789. * (frame_ref_old - frame_ref_after_popped));
  9790. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9791. frame_ref_buf, total_size);
  9792. #if WASM_ENABLE_GC != 0
  9793. /* The stack operand num should not be smaller than before
  9794. after pop and push operations */
  9795. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9796. loader_ctx->reftype_map_num = reftype_map_num_old;
  9797. loader_ctx->frame_reftype_map =
  9798. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9799. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9800. * (frame_reftype_map_old
  9801. - frame_reftype_map_after_popped));
  9802. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9803. total_size, frame_reftype_map_buf, total_size);
  9804. #endif
  9805. #if WASM_ENABLE_FAST_INTERP != 0
  9806. loader_ctx->frame_offset =
  9807. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9808. total_size =
  9809. (uint32)(sizeof(int16)
  9810. * (frame_offset_old - frame_offset_after_popped));
  9811. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9812. total_size, frame_offset_buf, total_size);
  9813. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9814. #endif
  9815. }
  9816. ret = true;
  9817. goto cleanup_and_return;
  9818. }
  9819. available_stack_cell =
  9820. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9821. #if WASM_ENABLE_GC != 0
  9822. available_reftype_map =
  9823. (int32)(loader_ctx->reftype_map_num
  9824. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9825. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9826. #endif
  9827. /* Check stack top values match target block type */
  9828. for (i = (int32)arity - 1; i >= 0; i--) {
  9829. type = types[i];
  9830. #if WASM_ENABLE_GC != 0
  9831. ref_type = NULL;
  9832. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9833. if (is_type_multi_byte) {
  9834. bh_assert(reftype_map);
  9835. ref_type = reftype_map->ref_type;
  9836. }
  9837. #endif
  9838. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9839. break;
  9840. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9841. #if WASM_ENABLE_GC != 0
  9842. frame_reftype_map, available_reftype_map,
  9843. #endif
  9844. type,
  9845. #if WASM_ENABLE_GC != 0
  9846. ref_type,
  9847. #endif
  9848. error_buf, error_buf_size)) {
  9849. goto fail;
  9850. }
  9851. cell_num = wasm_value_type_cell_num(types[i]);
  9852. frame_ref -= cell_num;
  9853. available_stack_cell -= cell_num;
  9854. #if WASM_ENABLE_GC != 0
  9855. if (is_type_multi_byte) {
  9856. frame_reftype_map--;
  9857. available_reftype_map--;
  9858. reftype_map--;
  9859. }
  9860. #endif
  9861. }
  9862. #if WASM_ENABLE_FAST_INTERP != 0
  9863. emit_br_info(target_block, opcode == WASM_OP_BR);
  9864. #endif
  9865. ret = true;
  9866. cleanup_and_return:
  9867. fail:
  9868. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9869. wasm_runtime_free(frame_ref_buf);
  9870. #if WASM_ENABLE_GC != 0
  9871. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9872. wasm_runtime_free(frame_reftype_map_buf);
  9873. #endif
  9874. #if WASM_ENABLE_FAST_INTERP != 0
  9875. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9876. wasm_runtime_free(frame_offset_buf);
  9877. #endif
  9878. return ret;
  9879. }
  9880. static BranchBlock *
  9881. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9882. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9883. {
  9884. uint8 *p = *p_buf, *p_end = buf_end;
  9885. BranchBlock *frame_csp_tmp;
  9886. uint32 depth;
  9887. read_leb_uint32(p, p_end, depth);
  9888. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9889. error_buf_size)) {
  9890. goto fail;
  9891. }
  9892. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9893. *p_buf = p;
  9894. return frame_csp_tmp;
  9895. fail:
  9896. return NULL;
  9897. }
  9898. #if WASM_ENABLE_EXCE_HANDLING != 0
  9899. static BranchBlock *
  9900. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9901. uint8 *buf_end, char *error_buf,
  9902. uint32 error_buf_size)
  9903. {
  9904. uint8 *p = *p_buf, *p_end = buf_end;
  9905. BranchBlock *frame_csp_tmp;
  9906. uint32 depth;
  9907. read_leb_uint32(p, p_end, depth);
  9908. /*
  9909. * Note: "delegate 0" means the surrounding block, not the
  9910. * try-delegate block itself.
  9911. *
  9912. * Note: the caller hasn't popped the try-delegate frame yet.
  9913. */
  9914. bh_assert(loader_ctx->csp_num > 0);
  9915. if (loader_ctx->csp_num - 1 <= depth) {
  9916. #if WASM_ENABLE_SPEC_TEST == 0
  9917. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9918. #else
  9919. set_error_buf(error_buf, error_buf_size, "unknown label");
  9920. #endif
  9921. goto fail;
  9922. }
  9923. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9924. #if WASM_ENABLE_FAST_INTERP != 0
  9925. emit_br_info(frame_csp_tmp, false);
  9926. #endif
  9927. *p_buf = p;
  9928. return frame_csp_tmp;
  9929. fail:
  9930. return NULL;
  9931. }
  9932. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9933. static bool
  9934. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9935. char *error_buf, uint32 error_buf_size)
  9936. {
  9937. BlockType *block_type = &block->block_type;
  9938. uint8 *return_types = NULL;
  9939. uint32 return_count = 0;
  9940. int32 available_stack_cell, return_cell_num, i;
  9941. uint8 *frame_ref = NULL;
  9942. #if WASM_ENABLE_GC != 0
  9943. WASMRefTypeMap *frame_reftype_map;
  9944. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9945. WASMRefType *ref_type;
  9946. uint32 param_count, return_reftype_map_count = 0;
  9947. int32 available_reftype_map =
  9948. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9949. #endif
  9950. available_stack_cell =
  9951. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9952. #if WASM_ENABLE_GC == 0
  9953. return_count = block_type_get_result_types(block_type, &return_types);
  9954. #else
  9955. return_count = block_type_get_result_types(block_type, &return_types,
  9956. &return_reftype_maps,
  9957. &return_reftype_map_count);
  9958. param_count =
  9959. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9960. (void)param_count;
  9961. #endif
  9962. return_cell_num =
  9963. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9964. /* If the stack is in polymorphic state, just clear the stack
  9965. * and then re-push the values to make the stack top values
  9966. * match block type. */
  9967. if (block->is_stack_polymorphic) {
  9968. #if WASM_ENABLE_GC != 0
  9969. int32 j = (int32)return_reftype_map_count - 1;
  9970. #endif
  9971. for (i = (int32)return_count - 1; i >= 0; i--) {
  9972. #if WASM_ENABLE_GC != 0
  9973. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9974. bh_assert(return_reftype_maps[j].index == i + param_count);
  9975. bh_memcpy_s(
  9976. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9977. return_reftype_maps[j].ref_type,
  9978. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9979. j--;
  9980. }
  9981. #endif
  9982. #if WASM_ENABLE_FAST_INTERP != 0
  9983. POP_OFFSET_TYPE(return_types[i]);
  9984. #endif
  9985. POP_TYPE(return_types[i]);
  9986. }
  9987. /* Check stack is empty */
  9988. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9989. set_error_buf(
  9990. error_buf, error_buf_size,
  9991. "type mismatch: stack size does not match block type");
  9992. goto fail;
  9993. }
  9994. #if WASM_ENABLE_GC != 0
  9995. j = 0;
  9996. #endif
  9997. for (i = 0; i < (int32)return_count; i++) {
  9998. #if WASM_ENABLE_GC != 0
  9999. if (wasm_is_type_multi_byte_type(return_types[i])) {
  10000. bh_assert(return_reftype_maps[j].index == i + param_count);
  10001. bh_memcpy_s(
  10002. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  10003. return_reftype_maps[j].ref_type,
  10004. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  10005. j++;
  10006. }
  10007. #endif
  10008. #if WASM_ENABLE_FAST_INTERP != 0
  10009. bool disable_emit = true;
  10010. int16 operand_offset = 0;
  10011. PUSH_OFFSET_TYPE(return_types[i]);
  10012. #endif
  10013. PUSH_TYPE(return_types[i]);
  10014. }
  10015. return true;
  10016. }
  10017. if (available_stack_cell != return_cell_num) {
  10018. #if WASM_ENABLE_EXCE_HANDLING != 0
  10019. /* testspec: this error message format is expected by try_catch.wast */
  10020. snprintf(
  10021. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  10022. block->label_type == LABEL_TYPE_TRY
  10023. || (block->label_type == LABEL_TYPE_CATCH
  10024. && return_cell_num > 0)
  10025. ? "instruction"
  10026. : "block",
  10027. return_cell_num > 0 ? type2str(return_types[0]) : "",
  10028. " but stack has ",
  10029. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  10030. : "");
  10031. goto fail;
  10032. #else
  10033. set_error_buf(error_buf, error_buf_size,
  10034. "type mismatch: stack size does not match block type");
  10035. goto fail;
  10036. #endif
  10037. }
  10038. /* Check stack values match return types */
  10039. frame_ref = loader_ctx->frame_ref;
  10040. #if WASM_ENABLE_GC != 0
  10041. frame_reftype_map = loader_ctx->frame_reftype_map;
  10042. return_reftype_map =
  10043. return_reftype_map_count
  10044. ? return_reftype_maps + return_reftype_map_count - 1
  10045. : NULL;
  10046. #endif
  10047. for (i = (int32)return_count - 1; i >= 0; i--) {
  10048. uint8 type = return_types[i];
  10049. #if WASM_ENABLE_GC != 0
  10050. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  10051. ref_type = NULL;
  10052. if (is_type_multi_byte) {
  10053. bh_assert(return_reftype_map);
  10054. ref_type = return_reftype_map->ref_type;
  10055. }
  10056. #endif
  10057. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  10058. #if WASM_ENABLE_GC != 0
  10059. frame_reftype_map, available_reftype_map,
  10060. #endif
  10061. type,
  10062. #if WASM_ENABLE_GC != 0
  10063. ref_type,
  10064. #endif
  10065. error_buf, error_buf_size))
  10066. return false;
  10067. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  10068. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  10069. #if WASM_ENABLE_GC != 0
  10070. if (is_type_multi_byte) {
  10071. frame_reftype_map--;
  10072. available_reftype_map--;
  10073. return_reftype_map--;
  10074. }
  10075. #endif
  10076. }
  10077. return true;
  10078. fail:
  10079. return false;
  10080. }
  10081. #if WASM_ENABLE_FAST_INTERP != 0
  10082. /* Copy parameters to dynamic space.
  10083. * 1) POP original parameter out;
  10084. * 2) Push and copy original values to dynamic space.
  10085. * The copy instruction format:
  10086. * Part a: param count
  10087. * Part b: all param total cell num
  10088. * Part c: each param's cell_num, src offset and dst offset
  10089. * Part d: each param's src offset
  10090. * Part e: each param's dst offset
  10091. */
  10092. static bool
  10093. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  10094. uint32 error_buf_size)
  10095. {
  10096. bool ret = false;
  10097. int16 *frame_offset = NULL;
  10098. uint8 *cells = NULL, cell;
  10099. int16 *src_offsets = NULL;
  10100. uint8 *emit_data = NULL;
  10101. uint32 i;
  10102. BranchBlock *block = loader_ctx->frame_csp - 1;
  10103. BlockType *block_type = &block->block_type;
  10104. WASMFuncType *wasm_type = block_type->u.type;
  10105. uint32 param_count = block_type->u.type->param_count;
  10106. int16 condition_offset = 0;
  10107. bool disable_emit = false;
  10108. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  10109. int16 operand_offset = 0;
  10110. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  10111. bh_assert(size > 0);
  10112. /* For if block, we also need copy the condition operand offset. */
  10113. if (is_if_block)
  10114. size += sizeof(*cells) + sizeof(*src_offsets);
  10115. /* Allocate memory for the emit data */
  10116. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  10117. return false;
  10118. cells = emit_data;
  10119. src_offsets = (int16 *)(cells + param_count);
  10120. if (is_if_block)
  10121. condition_offset = *loader_ctx->frame_offset;
  10122. /* POP original parameter out */
  10123. for (i = 0; i < param_count; i++) {
  10124. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  10125. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  10126. }
  10127. frame_offset = loader_ctx->frame_offset;
  10128. /* Get each param's cell num and src offset */
  10129. for (i = 0; i < param_count; i++) {
  10130. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  10131. cells[i] = cell;
  10132. src_offsets[i] = *frame_offset;
  10133. frame_offset += cell;
  10134. }
  10135. /* emit copy instruction */
  10136. emit_label(EXT_OP_COPY_STACK_VALUES);
  10137. /* Part a) */
  10138. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  10139. /* Part b) */
  10140. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  10141. : wasm_type->param_cell_num);
  10142. /* Part c) */
  10143. for (i = 0; i < param_count; i++)
  10144. emit_byte(loader_ctx, cells[i]);
  10145. if (is_if_block)
  10146. emit_byte(loader_ctx, 1);
  10147. /* Part d) */
  10148. for (i = 0; i < param_count; i++)
  10149. emit_operand(loader_ctx, src_offsets[i]);
  10150. if (is_if_block)
  10151. emit_operand(loader_ctx, condition_offset);
  10152. /* Since the start offset to save the block's params and
  10153. * the start offset to save the block's results may be
  10154. * different, we remember the dynamic offset for loop block
  10155. * so that we can use it to copy the stack operands to the
  10156. * loop block's params in wasm_loader_emit_br_info. */
  10157. if (block->label_type == LABEL_TYPE_LOOP)
  10158. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10159. /* Part e) */
  10160. /* Push to dynamic space. The push will emit the dst offset. */
  10161. for (i = 0; i < param_count; i++)
  10162. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  10163. if (is_if_block)
  10164. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  10165. ret = true;
  10166. fail:
  10167. /* Free the emit data */
  10168. wasm_runtime_free(emit_data);
  10169. return ret;
  10170. }
  10171. #endif
  10172. #if WASM_ENABLE_GC == 0
  10173. #define RESET_REFTYPE_MAP_STACK() (void)0
  10174. #else
  10175. #define RESET_REFTYPE_MAP_STACK() \
  10176. do { \
  10177. loader_ctx->reftype_map_num = \
  10178. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  10179. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  10180. + loader_ctx->reftype_map_num; \
  10181. } while (0)
  10182. #endif
  10183. /* reset the stack to the state of before entering the last block */
  10184. #if WASM_ENABLE_FAST_INTERP != 0
  10185. #define RESET_STACK() \
  10186. do { \
  10187. loader_ctx->stack_cell_num = \
  10188. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  10189. loader_ctx->frame_ref = \
  10190. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  10191. loader_ctx->frame_offset = \
  10192. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  10193. RESET_REFTYPE_MAP_STACK(); \
  10194. } while (0)
  10195. #else
  10196. #define RESET_STACK() \
  10197. do { \
  10198. loader_ctx->stack_cell_num = \
  10199. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  10200. loader_ctx->frame_ref = \
  10201. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  10202. RESET_REFTYPE_MAP_STACK(); \
  10203. } while (0)
  10204. #endif
  10205. /* set current block's stack polymorphic state */
  10206. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  10207. do { \
  10208. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  10209. _cur_block->is_stack_polymorphic = flag; \
  10210. } while (0)
  10211. #define BLOCK_HAS_PARAM(block_type) \
  10212. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  10213. #define PRESERVE_LOCAL_FOR_BLOCK() \
  10214. do { \
  10215. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  10216. error_buf_size))) { \
  10217. goto fail; \
  10218. } \
  10219. } while (0)
  10220. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10221. static bool
  10222. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  10223. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  10224. uint32 error_buf_size)
  10225. {
  10226. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  10227. return false;
  10228. }
  10229. if (table_idx < module->import_table_count) {
  10230. if (p_elem_type)
  10231. *p_elem_type =
  10232. module->import_tables[table_idx].u.table.table_type.elem_type;
  10233. #if WASM_ENABLE_GC != 0
  10234. if (p_ref_type)
  10235. *((WASMRefType **)p_ref_type) =
  10236. module->import_tables[table_idx]
  10237. .u.table.table_type.elem_ref_type;
  10238. #endif
  10239. }
  10240. else {
  10241. if (p_elem_type)
  10242. *p_elem_type =
  10243. module->tables[table_idx - module->import_table_count]
  10244. .table_type.elem_type;
  10245. #if WASM_ENABLE_GC != 0
  10246. if (p_ref_type)
  10247. *((WASMRefType **)p_ref_type) =
  10248. module->tables[table_idx - module->import_table_count]
  10249. .table_type.elem_ref_type;
  10250. #endif
  10251. }
  10252. return true;
  10253. }
  10254. static bool
  10255. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  10256. uint8 *p_elem_type, void **p_elem_ref_type,
  10257. char *error_buf, uint32 error_buf_size)
  10258. {
  10259. if (table_seg_idx >= module->table_seg_count) {
  10260. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  10261. table_seg_idx);
  10262. return false;
  10263. }
  10264. if (p_elem_type) {
  10265. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  10266. }
  10267. #if WASM_ENABLE_GC != 0
  10268. if (p_elem_ref_type)
  10269. *((WASMRefType **)p_elem_ref_type) =
  10270. module->table_segments[table_seg_idx].elem_ref_type;
  10271. #endif
  10272. return true;
  10273. }
  10274. #endif
  10275. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  10276. const uint8 *
  10277. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  10278. uint32 *len)
  10279. {
  10280. WASMCustomSection *section = module->custom_section_list;
  10281. while (section) {
  10282. if ((section->name_len == strlen(name))
  10283. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  10284. if (len) {
  10285. *len = section->content_len;
  10286. }
  10287. return section->content_addr;
  10288. }
  10289. section = section->next;
  10290. }
  10291. return NULL;
  10292. }
  10293. #endif
  10294. #if 0
  10295. #define HANDLE_OPCODE(opcode) #opcode
  10296. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  10297. #undef HANDLE_OPCODE
  10298. #endif
  10299. #if WASM_ENABLE_FAST_INTERP == 0
  10300. #define pb_read_leb_uint32 read_leb_uint32
  10301. #define pb_read_leb_int32 read_leb_int32
  10302. #define pb_read_leb_int64 read_leb_int64
  10303. #define pb_read_leb_memarg read_leb_memarg
  10304. #define pb_read_leb_mem_offset read_leb_mem_offset
  10305. #else
  10306. /* Read leb without malformed format check */
  10307. static uint64
  10308. read_leb_quick(uint8 **p_buf, uint32 maxbits, bool sign)
  10309. {
  10310. uint8 *buf = *p_buf;
  10311. uint64 result = 0, byte = 0;
  10312. uint32 shift = 0;
  10313. do {
  10314. byte = *buf++;
  10315. result |= ((byte & 0x7f) << shift);
  10316. shift += 7;
  10317. } while (byte & 0x80);
  10318. if (sign && (shift < maxbits) && (byte & 0x40)) {
  10319. /* Sign extend */
  10320. result |= (~((uint64)0)) << shift;
  10321. }
  10322. *p_buf = buf;
  10323. return result;
  10324. }
  10325. #define pb_read_leb_uint32(p, p_end, res) \
  10326. do { \
  10327. if (!loader_ctx->p_code_compiled) \
  10328. /* Enable format check in the first scan */ \
  10329. read_leb_uint32(p, p_end, res); \
  10330. else \
  10331. /* Disable format check in the second scan */ \
  10332. res = (uint32)read_leb_quick(&p, 32, false); \
  10333. } while (0)
  10334. #define pb_read_leb_int32(p, p_end, res) \
  10335. do { \
  10336. if (!loader_ctx->p_code_compiled) \
  10337. /* Enable format check in the first scan */ \
  10338. read_leb_int32(p, p_end, res); \
  10339. else \
  10340. /* Disable format check in the second scan */ \
  10341. res = (int32)read_leb_quick(&p, 32, true); \
  10342. } while (0)
  10343. #define pb_read_leb_int64(p, p_end, res) \
  10344. do { \
  10345. if (!loader_ctx->p_code_compiled) \
  10346. /* Enable format check in the first scan */ \
  10347. read_leb_int64(p, p_end, res); \
  10348. else \
  10349. /* Disable format check in the second scan */ \
  10350. res = (int64)read_leb_quick(&p, 64, true); \
  10351. } while (0)
  10352. #if WASM_ENABLE_MULTI_MEMORY != 0
  10353. #define pb_read_leb_memarg read_leb_memarg
  10354. #else
  10355. #define pb_read_leb_memarg pb_read_leb_uint32
  10356. #endif
  10357. #if WASM_ENABLE_MEMORY64 != 0
  10358. #define pb_read_leb_mem_offset read_leb_mem_offset
  10359. #else
  10360. #define pb_read_leb_mem_offset pb_read_leb_uint32
  10361. #endif
  10362. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  10363. static bool
  10364. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  10365. uint32 cur_func_idx, char *error_buf,
  10366. uint32 error_buf_size)
  10367. {
  10368. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  10369. uint32 param_count, local_count, global_count;
  10370. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type,
  10371. table_elem_idx_type;
  10372. BlockType func_block_type;
  10373. uint16 *local_offsets, local_offset;
  10374. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  10375. uint32 table_seg_idx, data_seg_idx, count, align, i;
  10376. mem_offset_t mem_offset;
  10377. int32 i32_const = 0;
  10378. int64 i64_const;
  10379. uint8 opcode;
  10380. bool return_value = false;
  10381. WASMLoaderContext *loader_ctx;
  10382. BranchBlock *frame_csp_tmp;
  10383. #if WASM_ENABLE_GC != 0
  10384. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  10385. uint32 param_reftype_map_count, local_reftype_map_count;
  10386. int32 heap_type;
  10387. WASMRefType wasm_ref_type = { 0 };
  10388. bool need_ref_type_map;
  10389. #endif
  10390. #if WASM_ENABLE_FAST_INTERP != 0
  10391. int16 operand_offset = 0;
  10392. uint8 last_op = 0;
  10393. bool disable_emit, preserve_local = false, if_condition_available = true;
  10394. float32 f32_const;
  10395. float64 f64_const;
  10396. /*
  10397. * It means that the fast interpreter detected an exception while preparing,
  10398. * typically near the block opcode, but it did not immediately trigger
  10399. * the exception. The loader should be capable of identifying it near
  10400. * the end opcode and then raising the exception.
  10401. */
  10402. bool pending_exception = false;
  10403. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  10404. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  10405. #endif
  10406. #if WASM_ENABLE_MEMORY64 != 0
  10407. bool is_memory64 = has_module_memory64(module);
  10408. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  10409. #else
  10410. mem_offset_type = VALUE_TYPE_I32;
  10411. table_elem_idx_type = VALUE_TYPE_I32;
  10412. #endif
  10413. uint32 memidx;
  10414. global_count = module->import_global_count + module->global_count;
  10415. param_count = func->func_type->param_count;
  10416. param_types = func->func_type->types;
  10417. func_block_type.is_value_type = false;
  10418. func_block_type.u.type = func->func_type;
  10419. local_count = func->local_count;
  10420. local_types = func->local_types;
  10421. local_offsets = func->local_offsets;
  10422. #if WASM_ENABLE_GC != 0
  10423. param_reftype_maps = func->func_type->ref_type_maps;
  10424. param_reftype_map_count = func->func_type->ref_type_map_count;
  10425. local_reftype_maps = func->local_ref_type_maps;
  10426. local_reftype_map_count = func->local_ref_type_map_count;
  10427. #endif
  10428. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  10429. goto fail;
  10430. }
  10431. #if WASM_ENABLE_GC != 0
  10432. loader_ctx->module = module;
  10433. loader_ctx->ref_type_set = module->ref_type_set;
  10434. loader_ctx->ref_type_tmp = &wasm_ref_type;
  10435. #endif
  10436. #if WASM_ENABLE_FAST_INTERP != 0
  10437. /* For the first traverse, the initial value of preserved_local_offset has
  10438. * not been determined, we use the INT16_MAX to represent that a slot has
  10439. * been copied to preserve space. For second traverse, this field will be
  10440. * set to the appropriate value in wasm_loader_ctx_reinit.
  10441. * This is for Issue #1230,
  10442. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  10443. * drop opcodes need to know which slots are preserved, so those slots will
  10444. * not be treated as dynamically allocated slots */
  10445. loader_ctx->preserved_local_offset = INT16_MAX;
  10446. re_scan:
  10447. if (loader_ctx->code_compiled_size > 0) {
  10448. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  10449. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  10450. goto fail;
  10451. }
  10452. p = func->code;
  10453. func->code_compiled = loader_ctx->p_code_compiled;
  10454. func->code_compiled_size = loader_ctx->code_compiled_size;
  10455. if (loader_ctx->i64_const_num > 0) {
  10456. int64 *i64_consts_old = loader_ctx->i64_consts;
  10457. /* Sort the i64 consts */
  10458. qsort(i64_consts_old, loader_ctx->i64_const_num, sizeof(int64),
  10459. cmp_i64_const);
  10460. /* Remove the duplicated i64 consts */
  10461. uint32 k = 1;
  10462. for (i = 1; i < loader_ctx->i64_const_num; i++) {
  10463. if (i64_consts_old[i] != i64_consts_old[i - 1]) {
  10464. i64_consts_old[k++] = i64_consts_old[i];
  10465. }
  10466. }
  10467. if (k < loader_ctx->i64_const_num) {
  10468. int64 *i64_consts_new;
  10469. /* Try to reallocate memory with a smaller size */
  10470. if ((i64_consts_new =
  10471. wasm_runtime_malloc((uint32)sizeof(int64) * k))) {
  10472. bh_memcpy_s(i64_consts_new, (uint32)sizeof(int64) * k,
  10473. i64_consts_old, (uint32)sizeof(int64) * k);
  10474. /* Free the old memory */
  10475. wasm_runtime_free(i64_consts_old);
  10476. loader_ctx->i64_consts = i64_consts_new;
  10477. loader_ctx->i64_const_max_num = k;
  10478. }
  10479. loader_ctx->i64_const_num = k;
  10480. }
  10481. }
  10482. if (loader_ctx->v128_const_num > 0) {
  10483. V128 *v128_consts_old = loader_ctx->v128_consts;
  10484. /* Sort the v128 consts */
  10485. qsort(v128_consts_old, loader_ctx->v128_const_num, sizeof(V128),
  10486. cmp_v128_const);
  10487. /* Remove the duplicated v128 consts */
  10488. uint32 k = 1;
  10489. for (i = 1; i < loader_ctx->v128_const_num; i++) {
  10490. if (!(memcmp(&v128_consts_old[i], &v128_consts_old[i - 1],
  10491. sizeof(V128))
  10492. == 0)) {
  10493. v128_consts_old[k++] = v128_consts_old[i];
  10494. }
  10495. }
  10496. if (k < loader_ctx->v128_const_num) {
  10497. V128 *v128_consts_new;
  10498. /* Try to reallocate memory with a smaller size */
  10499. if ((v128_consts_new =
  10500. wasm_runtime_malloc((uint32)sizeof(V128) * k))) {
  10501. bh_memcpy_s(v128_consts_new, (uint32)sizeof(V128) * k,
  10502. v128_consts_old, (uint32)sizeof(V128) * k);
  10503. /* Free the old memory */
  10504. wasm_runtime_free(v128_consts_old);
  10505. loader_ctx->v128_consts = v128_consts_new;
  10506. loader_ctx->v128_const_max_num = k;
  10507. }
  10508. loader_ctx->v128_const_num = k;
  10509. }
  10510. }
  10511. if (loader_ctx->i32_const_num > 0) {
  10512. int32 *i32_consts_old = loader_ctx->i32_consts;
  10513. /* Sort the i32 consts */
  10514. qsort(i32_consts_old, loader_ctx->i32_const_num, sizeof(int32),
  10515. cmp_i32_const);
  10516. /* Remove the duplicated i32 consts */
  10517. uint32 k = 1;
  10518. for (i = 1; i < loader_ctx->i32_const_num; i++) {
  10519. if (i32_consts_old[i] != i32_consts_old[i - 1]) {
  10520. i32_consts_old[k++] = i32_consts_old[i];
  10521. }
  10522. }
  10523. if (k < loader_ctx->i32_const_num) {
  10524. int32 *i32_consts_new;
  10525. /* Try to reallocate memory with a smaller size */
  10526. if ((i32_consts_new =
  10527. wasm_runtime_malloc((uint32)sizeof(int32) * k))) {
  10528. bh_memcpy_s(i32_consts_new, (uint32)sizeof(int32) * k,
  10529. i32_consts_old, (uint32)sizeof(int32) * k);
  10530. /* Free the old memory */
  10531. wasm_runtime_free(i32_consts_old);
  10532. loader_ctx->i32_consts = i32_consts_new;
  10533. loader_ctx->i32_const_max_num = k;
  10534. }
  10535. loader_ctx->i32_const_num = k;
  10536. }
  10537. }
  10538. }
  10539. #endif
  10540. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  10541. while (p < p_end) {
  10542. opcode = *p++;
  10543. #if WASM_ENABLE_FAST_INTERP != 0
  10544. p_org = p;
  10545. disable_emit = false;
  10546. emit_label(opcode);
  10547. #endif
  10548. switch (opcode) {
  10549. case WASM_OP_UNREACHABLE:
  10550. RESET_STACK();
  10551. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10552. break;
  10553. case WASM_OP_NOP:
  10554. #if WASM_ENABLE_FAST_INTERP != 0
  10555. skip_label();
  10556. #endif
  10557. break;
  10558. case WASM_OP_IF:
  10559. {
  10560. #if WASM_ENABLE_FAST_INTERP != 0
  10561. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  10562. int32 available_stack_cell =
  10563. (int32)(loader_ctx->stack_cell_num
  10564. - parent_block->stack_cell_num);
  10565. if (available_stack_cell <= 0
  10566. && parent_block->is_stack_polymorphic)
  10567. if_condition_available = false;
  10568. else
  10569. if_condition_available = true;
  10570. PRESERVE_LOCAL_FOR_BLOCK();
  10571. #endif
  10572. #if WASM_ENABLE_GC == 0
  10573. POP_I32();
  10574. #endif
  10575. goto handle_op_block_and_loop;
  10576. }
  10577. case WASM_OP_BLOCK:
  10578. case WASM_OP_LOOP:
  10579. #if WASM_ENABLE_EXCE_HANDLING != 0
  10580. case WASM_OP_TRY:
  10581. if (opcode == WASM_OP_TRY) {
  10582. /*
  10583. * keep track of exception handlers to account for
  10584. * memory allocation
  10585. */
  10586. func->exception_handler_count++;
  10587. /*
  10588. * try is a block
  10589. * do nothing special, but execution continues to
  10590. * to handle_op_block_and_loop,
  10591. * and that be pushes the csp
  10592. */
  10593. }
  10594. #endif
  10595. #if WASM_ENABLE_FAST_INTERP != 0
  10596. PRESERVE_LOCAL_FOR_BLOCK();
  10597. #endif
  10598. handle_op_block_and_loop:
  10599. {
  10600. uint8 value_type;
  10601. BlockType block_type;
  10602. #if WASM_ENABLE_FAST_INTERP != 0
  10603. uint32 available_params = 0;
  10604. #endif
  10605. CHECK_BUF(p, p_end, 1);
  10606. value_type = read_uint8(p);
  10607. if (is_byte_a_type(value_type)) {
  10608. /* If the first byte is one of these special values:
  10609. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  10610. * the single return value. */
  10611. block_type.is_value_type = true;
  10612. block_type.u.value_type.type = value_type;
  10613. #if WASM_ENABLE_WAMR_COMPILER != 0
  10614. if (value_type == VALUE_TYPE_V128)
  10615. module->is_simd_used = true;
  10616. else if (value_type == VALUE_TYPE_FUNCREF
  10617. || value_type == VALUE_TYPE_EXTERNREF)
  10618. module->is_ref_types_used = true;
  10619. #endif
  10620. #if WASM_ENABLE_GC != 0
  10621. if (value_type != VALUE_TYPE_VOID) {
  10622. p_org = p;
  10623. p--;
  10624. if (!resolve_value_type((const uint8 **)&p, p_end,
  10625. module, module->type_count,
  10626. &need_ref_type_map,
  10627. &wasm_ref_type, false,
  10628. error_buf, error_buf_size)) {
  10629. goto fail;
  10630. }
  10631. if (need_ref_type_map) {
  10632. block_type.u.value_type.ref_type_map.index = 0;
  10633. if (!(block_type.u.value_type.ref_type_map
  10634. .ref_type = reftype_set_insert(
  10635. module->ref_type_set, &wasm_ref_type,
  10636. error_buf, error_buf_size))) {
  10637. goto fail;
  10638. }
  10639. }
  10640. /* Set again as the type might be changed, e.g.
  10641. (ref null any) to anyref */
  10642. block_type.u.value_type.type = wasm_ref_type.ref_type;
  10643. #if WASM_ENABLE_FAST_INTERP == 0
  10644. while (p_org < p) {
  10645. #if WASM_ENABLE_DEBUG_INTERP != 0
  10646. if (!record_fast_op(module, p_org, *p_org,
  10647. error_buf, error_buf_size)) {
  10648. goto fail;
  10649. }
  10650. #endif
  10651. /* Ignore extra bytes for interpreter */
  10652. *p_org++ = WASM_OP_NOP;
  10653. }
  10654. #endif
  10655. }
  10656. #endif /* end of WASM_ENABLE_GC != 0 */
  10657. }
  10658. else {
  10659. int32 type_index;
  10660. /* Resolve the leb128 encoded type index as block type */
  10661. p--;
  10662. p_org = p - 1;
  10663. pb_read_leb_int32(p, p_end, type_index);
  10664. if (!check_function_type(module, type_index, error_buf,
  10665. error_buf_size)) {
  10666. goto fail;
  10667. }
  10668. block_type.is_value_type = false;
  10669. block_type.u.type =
  10670. (WASMFuncType *)module->types[type_index];
  10671. #if WASM_ENABLE_FAST_INTERP == 0
  10672. /* If block use type index as block type, change the opcode
  10673. * to new extended opcode so that interpreter can resolve
  10674. * the block quickly.
  10675. */
  10676. #if WASM_ENABLE_DEBUG_INTERP != 0
  10677. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10678. error_buf_size)) {
  10679. goto fail;
  10680. }
  10681. #endif
  10682. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  10683. #endif
  10684. }
  10685. #if WASM_ENABLE_GC != 0
  10686. if (opcode == WASM_OP_IF) {
  10687. POP_I32();
  10688. }
  10689. #endif
  10690. /* Pop block parameters from stack */
  10691. if (BLOCK_HAS_PARAM(block_type)) {
  10692. WASMFuncType *wasm_type = block_type.u.type;
  10693. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10694. #if WASM_ENABLE_FAST_INTERP != 0
  10695. uint32 cell_num;
  10696. available_params = block_type.u.type->param_count;
  10697. #endif
  10698. for (i = 0; i < block_type.u.type->param_count; i++) {
  10699. int32 available_stack_cell =
  10700. (int32)(loader_ctx->stack_cell_num
  10701. - cur_block->stack_cell_num);
  10702. if (available_stack_cell <= 0
  10703. && cur_block->is_stack_polymorphic) {
  10704. #if WASM_ENABLE_FAST_INTERP != 0
  10705. available_params = i;
  10706. #endif
  10707. break;
  10708. }
  10709. POP_TYPE(
  10710. wasm_type->types[wasm_type->param_count - i - 1]);
  10711. #if WASM_ENABLE_FAST_INTERP != 0
  10712. /* decrease the frame_offset pointer accordingly to keep
  10713. * consistent with frame_ref stack */
  10714. cell_num = wasm_value_type_cell_num(
  10715. wasm_type->types[wasm_type->param_count - i - 1]);
  10716. loader_ctx->frame_offset -= cell_num;
  10717. if (loader_ctx->frame_offset
  10718. < loader_ctx->frame_offset_bottom) {
  10719. LOG_DEBUG(
  10720. "frame_offset underflow, roll back and "
  10721. "let following stack checker report it\n");
  10722. loader_ctx->frame_offset += cell_num;
  10723. pending_exception = true;
  10724. break;
  10725. }
  10726. #endif
  10727. }
  10728. }
  10729. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10730. block_type, p);
  10731. /* Pass parameters to block */
  10732. if (BLOCK_HAS_PARAM(block_type)) {
  10733. WASMFuncType *func_type = block_type.u.type;
  10734. #if WASM_ENABLE_GC != 0
  10735. WASMRefType *ref_type;
  10736. uint32 j = 0;
  10737. #endif
  10738. for (i = 0; i < func_type->param_count; i++) {
  10739. #if WASM_ENABLE_FAST_INTERP != 0
  10740. uint32 cell_num =
  10741. wasm_value_type_cell_num(func_type->types[i]);
  10742. if (i >= available_params) {
  10743. /* make sure enough space */
  10744. if (loader_ctx->p_code_compiled == NULL) {
  10745. loader_ctx->frame_offset += cell_num;
  10746. if (!check_offset_push(loader_ctx, error_buf,
  10747. error_buf_size))
  10748. goto fail;
  10749. /* for following dummy value assignment */
  10750. loader_ctx->frame_offset -= cell_num;
  10751. }
  10752. /* If there isn't enough data on stack, push a dummy
  10753. * offset to keep the stack consistent with
  10754. * frame_ref.
  10755. * Since the stack is already in polymorphic state,
  10756. * the opcode will not be executed, so the dummy
  10757. * offset won't cause any error */
  10758. for (uint32 n = 0; n < cell_num; n++) {
  10759. *loader_ctx->frame_offset++ = 0;
  10760. }
  10761. }
  10762. else {
  10763. loader_ctx->frame_offset += cell_num;
  10764. }
  10765. #endif
  10766. #if WASM_ENABLE_GC != 0
  10767. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10768. bh_assert(func_type->ref_type_maps[j].index == i);
  10769. ref_type = func_type->ref_type_maps[j].ref_type;
  10770. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10771. ref_type,
  10772. wasm_reftype_struct_size(ref_type));
  10773. j++;
  10774. }
  10775. #endif
  10776. PUSH_TYPE(func_type->types[i]);
  10777. }
  10778. }
  10779. #if WASM_ENABLE_FAST_INTERP != 0
  10780. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10781. skip_label();
  10782. if (BLOCK_HAS_PARAM(block_type)) {
  10783. /* Make sure params are in dynamic space */
  10784. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10785. error_buf_size))
  10786. goto fail;
  10787. }
  10788. if (opcode == WASM_OP_LOOP) {
  10789. (loader_ctx->frame_csp - 1)->code_compiled =
  10790. loader_ctx->p_code_compiled;
  10791. }
  10792. }
  10793. #if WASM_ENABLE_EXCE_HANDLING != 0
  10794. else if (opcode == WASM_OP_TRY) {
  10795. skip_label();
  10796. }
  10797. #endif
  10798. else if (opcode == WASM_OP_IF) {
  10799. BranchBlock *block = loader_ctx->frame_csp - 1;
  10800. /* If block has parameters, we should make sure they are in
  10801. * dynamic space. Otherwise, when else branch is missing,
  10802. * the later opcode may consume incorrect operand offset.
  10803. * Spec case:
  10804. * (func (export "params-id") (param i32) (result i32)
  10805. * (i32.const 1)
  10806. * (i32.const 2)
  10807. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10808. * (then)) (i32.add)
  10809. * )
  10810. *
  10811. * So we should emit a copy instruction before the if.
  10812. *
  10813. * And we also need to save the parameter offsets and
  10814. * recover them before entering else branch.
  10815. *
  10816. */
  10817. if (BLOCK_HAS_PARAM(block_type)) {
  10818. uint64 size;
  10819. /* In polymorphic state, there may be no if condition on
  10820. * the stack, so the offset may not emitted */
  10821. if (if_condition_available) {
  10822. /* skip the if condition operand offset */
  10823. wasm_loader_emit_backspace(loader_ctx,
  10824. sizeof(int16));
  10825. }
  10826. /* skip the if label */
  10827. skip_label();
  10828. /* Emit a copy instruction */
  10829. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10830. error_buf_size))
  10831. goto fail;
  10832. /* Emit the if instruction */
  10833. emit_label(opcode);
  10834. /* Emit the new condition operand offset */
  10835. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10836. /* Save top param_count values of frame_offset stack, so
  10837. * that we can recover it before executing else branch
  10838. */
  10839. size = sizeof(int16)
  10840. * (uint64)block_type.u.type->param_cell_num;
  10841. if (!(block->param_frame_offsets = loader_malloc(
  10842. size, error_buf, error_buf_size)))
  10843. goto fail;
  10844. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10845. loader_ctx->frame_offset
  10846. - size / sizeof(int16),
  10847. (uint32)size);
  10848. }
  10849. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10850. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10851. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10852. }
  10853. #endif
  10854. break;
  10855. }
  10856. #if WASM_ENABLE_EXCE_HANDLING != 0
  10857. case WASM_OP_THROW:
  10858. {
  10859. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10860. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10861. uint8 label_type = cur_block->label_type;
  10862. uint32 tag_index = 0;
  10863. pb_read_leb_int32(p, p_end, tag_index);
  10864. /* check validity of tag_index against module->tag_count */
  10865. /* check tag index is within the tag index space */
  10866. if (tag_index >= module->import_tag_count + module->tag_count) {
  10867. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10868. tag_index);
  10869. goto fail;
  10870. }
  10871. /* the tag_type is stored in either the WASMTag (section tags)
  10872. * or WASMTagImport (import tag) */
  10873. WASMFuncType *tag_type = NULL;
  10874. if (tag_index < module->import_tag_count) {
  10875. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10876. }
  10877. else {
  10878. tag_type =
  10879. module->tags[tag_index - module->import_tag_count]
  10880. ->tag_type;
  10881. }
  10882. if (tag_type->result_count != 0) {
  10883. set_error_buf(error_buf, error_buf_size,
  10884. "tag type signature does not return void");
  10885. goto fail;
  10886. }
  10887. int32 available_stack_cell =
  10888. (int32)(loader_ctx->stack_cell_num
  10889. - cur_block->stack_cell_num);
  10890. int32 tti;
  10891. /* Check stack values match return types by comparing tag param
  10892. * types with stack cells */
  10893. uint8 *frame_ref = loader_ctx->frame_ref;
  10894. #if WASM_ENABLE_GC != 0
  10895. WASMRefTypeMap *frame_reftype_map =
  10896. loader_ctx->frame_reftype_map;
  10897. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10898. /* Temporarily set these values since they may be used in
  10899. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10900. param_reftype_maps = tag_type->ref_type_maps;
  10901. /* For tag_type function, it shouldn't have result_count = 0 */
  10902. param_reftype_map_count = tag_type->ref_type_map_count;
  10903. param_count = tag_type->param_count;
  10904. #endif
  10905. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10906. #if WASM_ENABLE_GC != 0
  10907. local_type = tag_type->types[tti];
  10908. local_idx = tti;
  10909. /* Get the wasm_ref_type if the local_type is multibyte
  10910. type */
  10911. GET_LOCAL_REFTYPE();
  10912. #endif
  10913. if (!check_stack_top_values(
  10914. loader_ctx, frame_ref, available_stack_cell,
  10915. #if WASM_ENABLE_GC != 0
  10916. frame_reftype_map, frame_reftype_map_num,
  10917. #endif
  10918. tag_type->types[tti],
  10919. #if WASM_ENABLE_GC != 0
  10920. &wasm_ref_type,
  10921. #endif
  10922. error_buf, error_buf_size)) {
  10923. snprintf(error_buf, error_buf_size,
  10924. "type mismatch: instruction requires [%s] but "
  10925. "stack has [%s]",
  10926. tag_type->param_count > 0
  10927. ? type2str(tag_type->types[tti])
  10928. : "",
  10929. available_stack_cell > 0
  10930. ? type2str(*(loader_ctx->frame_ref - 1))
  10931. : "");
  10932. goto fail;
  10933. }
  10934. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10935. available_stack_cell -=
  10936. wasm_value_type_cell_num(tag_type->types[tti]);
  10937. }
  10938. #if WASM_ENABLE_GC != 0
  10939. /* Restore the values */
  10940. param_reftype_maps = func->func_type->ref_type_maps;
  10941. param_reftype_map_count = func->func_type->ref_type_map_count;
  10942. param_count = func->func_type->param_count;
  10943. #endif
  10944. /* throw is stack polymorphic */
  10945. (void)label_type;
  10946. RESET_STACK();
  10947. break;
  10948. }
  10949. case WASM_OP_RETHROW:
  10950. {
  10951. /* must be done before checking branch block */
  10952. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10953. /* check the target catching block: LABEL_TYPE_CATCH */
  10954. if (!(frame_csp_tmp =
  10955. check_branch_block(loader_ctx, &p, p_end, opcode,
  10956. error_buf, error_buf_size)))
  10957. goto fail;
  10958. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10959. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10960. /* trap according to spectest (rethrow.wast) */
  10961. set_error_buf(error_buf, error_buf_size,
  10962. "invalid rethrow label");
  10963. goto fail;
  10964. }
  10965. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10966. uint8 label_type = cur_block->label_type;
  10967. (void)label_type;
  10968. /* rethrow is stack polymorphic */
  10969. RESET_STACK();
  10970. break;
  10971. }
  10972. case WASM_OP_DELEGATE:
  10973. {
  10974. /* check target block is valid */
  10975. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10976. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10977. goto fail;
  10978. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10979. uint8 label_type = cur_block->label_type;
  10980. (void)label_type;
  10981. /* DELEGATE ends the block */
  10982. POP_CSP();
  10983. break;
  10984. }
  10985. case WASM_OP_CATCH:
  10986. {
  10987. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10988. uint8 label_type = cur_block->label_type;
  10989. uint32 tag_index = 0;
  10990. pb_read_leb_int32(p, p_end, tag_index);
  10991. /* check validity of tag_index against module->tag_count */
  10992. /* check tag index is within the tag index space */
  10993. if (tag_index >= module->import_tag_count + module->tag_count) {
  10994. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10995. __FUNCTION__);
  10996. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10997. goto fail;
  10998. }
  10999. /* the tag_type is stored in either the WASMTag (section tags)
  11000. * or WASMTagImport (import tag) */
  11001. WASMFuncType *func_type = NULL;
  11002. if (tag_index < module->import_tag_count) {
  11003. func_type = module->import_tags[tag_index].u.tag.tag_type;
  11004. }
  11005. else {
  11006. func_type =
  11007. module->tags[tag_index - module->import_tag_count]
  11008. ->tag_type;
  11009. }
  11010. if (func_type->result_count != 0) {
  11011. set_error_buf(error_buf, error_buf_size,
  11012. "tag type signature does not return void");
  11013. goto fail;
  11014. }
  11015. /* check validity of current label (expect LABEL_TYPE_TRY or
  11016. * LABEL_TYPE_CATCH) */
  11017. if ((LABEL_TYPE_CATCH != label_type)
  11018. && (LABEL_TYPE_TRY != label_type)) {
  11019. set_error_buf(error_buf, error_buf_size,
  11020. "Unexpected block sequence encountered.");
  11021. goto fail;
  11022. }
  11023. /*
  11024. * replace frame_csp by LABEL_TYPE_CATCH
  11025. */
  11026. cur_block->label_type = LABEL_TYPE_CATCH;
  11027. /* RESET_STACK removes the values pushed in TRY or previous
  11028. * CATCH Blocks */
  11029. RESET_STACK();
  11030. #if WASM_ENABLE_GC != 0
  11031. WASMRefType *ref_type;
  11032. uint32 j = 0;
  11033. #endif
  11034. /* push types on the stack according to caught type */
  11035. for (i = 0; i < func_type->param_count; i++) {
  11036. #if WASM_ENABLE_GC != 0
  11037. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  11038. bh_assert(func_type->ref_type_maps[j].index == i);
  11039. ref_type = func_type->ref_type_maps[j].ref_type;
  11040. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11041. ref_type,
  11042. wasm_reftype_struct_size(ref_type));
  11043. j++;
  11044. }
  11045. #endif
  11046. PUSH_TYPE(func_type->types[i]);
  11047. }
  11048. break;
  11049. }
  11050. case WASM_OP_CATCH_ALL:
  11051. {
  11052. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11053. /* expecting a TRY or CATCH, anything else will be considered an
  11054. * error */
  11055. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  11056. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  11057. set_error_buf(error_buf, error_buf_size,
  11058. "Unexpected block sequence encountered.");
  11059. goto fail;
  11060. }
  11061. /* no immediates */
  11062. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  11063. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  11064. /* RESET_STACK removes the values pushed in TRY or previous
  11065. * CATCH Blocks */
  11066. RESET_STACK();
  11067. /* catch_all has no tagtype and therefore no parameters */
  11068. break;
  11069. }
  11070. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  11071. case WASM_OP_ELSE:
  11072. handle_op_else:
  11073. {
  11074. BranchBlock *block = NULL;
  11075. BlockType block_type;
  11076. if (loader_ctx->csp_num < 2
  11077. /* the matched if isn't found */
  11078. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  11079. /* duplicated else is found */
  11080. || (loader_ctx->frame_csp - 1)->else_addr) {
  11081. set_error_buf(
  11082. error_buf, error_buf_size,
  11083. "opcode else found without matched opcode if");
  11084. goto fail;
  11085. }
  11086. block = loader_ctx->frame_csp - 1;
  11087. /* check whether if branch's stack matches its result type */
  11088. if (!check_block_stack(loader_ctx, block, error_buf,
  11089. error_buf_size))
  11090. goto fail;
  11091. block->else_addr = p - 1;
  11092. block_type = block->block_type;
  11093. #if WASM_ENABLE_GC != 0
  11094. if (!wasm_loader_init_local_use_masks(
  11095. loader_ctx, local_count, error_buf, error_buf_size)) {
  11096. goto fail;
  11097. }
  11098. #endif
  11099. #if WASM_ENABLE_FAST_INTERP != 0
  11100. /* if the result of if branch is in local or const area, add a
  11101. * copy op */
  11102. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  11103. error_buf, error_buf_size)) {
  11104. goto fail;
  11105. }
  11106. emit_empty_label_addr_and_frame_ip(PATCH_END);
  11107. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  11108. #endif
  11109. RESET_STACK();
  11110. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  11111. /* Pass parameters to if-false branch */
  11112. if (BLOCK_HAS_PARAM(block_type)) {
  11113. for (i = 0; i < block_type.u.type->param_count; i++)
  11114. PUSH_TYPE(block_type.u.type->types[i]);
  11115. }
  11116. #if WASM_ENABLE_FAST_INTERP != 0
  11117. /* Recover top param_count values of frame_offset stack */
  11118. if (BLOCK_HAS_PARAM((block_type))) {
  11119. uint32 size;
  11120. size = sizeof(int16) * block_type.u.type->param_cell_num;
  11121. bh_memcpy_s(loader_ctx->frame_offset, size,
  11122. block->param_frame_offsets, size);
  11123. loader_ctx->frame_offset += (size / sizeof(int16));
  11124. }
  11125. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  11126. #endif
  11127. break;
  11128. }
  11129. case WASM_OP_END:
  11130. {
  11131. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11132. /* check whether block stack matches its result type */
  11133. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  11134. error_buf_size))
  11135. goto fail;
  11136. /* if there is no else branch, make a virtual else opcode for
  11137. easier integrity check and to copy the correct results to
  11138. the block return address for fast-interp mode:
  11139. change if block from `if ... end` to `if ... else end` */
  11140. if (cur_block->label_type == LABEL_TYPE_IF
  11141. && !cur_block->else_addr) {
  11142. opcode = WASM_OP_ELSE;
  11143. p--;
  11144. #if WASM_ENABLE_FAST_INTERP != 0
  11145. p_org = p;
  11146. skip_label();
  11147. disable_emit = false;
  11148. emit_label(opcode);
  11149. #endif
  11150. goto handle_op_else;
  11151. }
  11152. POP_CSP();
  11153. #if WASM_ENABLE_FAST_INTERP != 0
  11154. skip_label();
  11155. /* copy the result to the block return address */
  11156. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  11157. error_buf, error_buf_size)) {
  11158. /* it could be tmp frame_csp allocated from opcode like
  11159. * OP_BR and not counted in loader_ctx->csp_num, it won't
  11160. * be freed in wasm_loader_ctx_destroy(loader_ctx) so need
  11161. * to free the loader_ctx->frame_csp if fails */
  11162. free_label_patch_list(loader_ctx->frame_csp);
  11163. goto fail;
  11164. }
  11165. apply_label_patch(loader_ctx, 0, PATCH_END);
  11166. free_label_patch_list(loader_ctx->frame_csp);
  11167. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  11168. int32 idx;
  11169. uint8 ret_type;
  11170. emit_label(WASM_OP_RETURN);
  11171. for (idx = (int32)func->func_type->result_count - 1;
  11172. idx >= 0; idx--) {
  11173. ret_type = *(func->func_type->types
  11174. + func->func_type->param_count + idx);
  11175. POP_OFFSET_TYPE(ret_type);
  11176. }
  11177. }
  11178. #endif
  11179. if (loader_ctx->csp_num > 0) {
  11180. loader_ctx->frame_csp->end_addr = p - 1;
  11181. }
  11182. else {
  11183. /* end of function block, function will return */
  11184. if (p < p_end) {
  11185. set_error_buf(error_buf, error_buf_size,
  11186. "section size mismatch");
  11187. goto fail;
  11188. }
  11189. }
  11190. #if WASM_ENABLE_FAST_INTERP != 0
  11191. if (pending_exception) {
  11192. set_error_buf(
  11193. error_buf, error_buf_size,
  11194. "There is a pending exception needs to be handled");
  11195. goto fail;
  11196. }
  11197. #endif
  11198. break;
  11199. }
  11200. case WASM_OP_BR:
  11201. {
  11202. if (!(frame_csp_tmp =
  11203. check_branch_block(loader_ctx, &p, p_end, opcode,
  11204. error_buf, error_buf_size)))
  11205. goto fail;
  11206. RESET_STACK();
  11207. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11208. break;
  11209. }
  11210. case WASM_OP_BR_IF:
  11211. {
  11212. POP_I32();
  11213. if (!(frame_csp_tmp =
  11214. check_branch_block(loader_ctx, &p, p_end, opcode,
  11215. error_buf, error_buf_size)))
  11216. goto fail;
  11217. break;
  11218. }
  11219. case WASM_OP_BR_TABLE:
  11220. {
  11221. uint32 depth = 0, default_arity, arity = 0;
  11222. BranchBlock *target_block;
  11223. BlockType *target_block_type;
  11224. #if WASM_ENABLE_FAST_INTERP == 0
  11225. BrTableCache *br_table_cache = NULL;
  11226. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  11227. uint32 j;
  11228. #endif
  11229. pb_read_leb_uint32(p, p_end, count);
  11230. #if WASM_ENABLE_FAST_INTERP != 0
  11231. emit_uint32(loader_ctx, count);
  11232. #endif
  11233. POP_I32();
  11234. /* Get each depth and check it */
  11235. p_org = p;
  11236. for (i = 0; i <= count; i++) {
  11237. pb_read_leb_uint32(p, p_end, depth);
  11238. bh_assert(loader_ctx->csp_num > 0);
  11239. if (loader_ctx->csp_num - 1 < depth) {
  11240. set_error_buf(error_buf, error_buf_size,
  11241. "unknown label, "
  11242. "unexpected end of section or function");
  11243. goto fail;
  11244. }
  11245. }
  11246. p = p_org;
  11247. /* Get the default block's arity */
  11248. target_block = loader_ctx->frame_csp - (depth + 1);
  11249. target_block_type = &target_block->block_type;
  11250. default_arity = block_type_get_arity(target_block_type,
  11251. target_block->label_type);
  11252. #if WASM_ENABLE_FAST_INTERP == 0
  11253. p_depth_begin = p_depth = p;
  11254. #endif
  11255. for (i = 0; i <= count; i++) {
  11256. p_org = p;
  11257. pb_read_leb_uint32(p, p_end, depth);
  11258. p = p_org;
  11259. /* Get the target block's arity and check it */
  11260. target_block = loader_ctx->frame_csp - (depth + 1);
  11261. target_block_type = &target_block->block_type;
  11262. arity = block_type_get_arity(target_block_type,
  11263. target_block->label_type);
  11264. if (arity != default_arity) {
  11265. set_error_buf(error_buf, error_buf_size,
  11266. "type mismatch: br_table targets must "
  11267. "all use same result type");
  11268. goto fail;
  11269. }
  11270. if (!(frame_csp_tmp =
  11271. check_branch_block(loader_ctx, &p, p_end, opcode,
  11272. error_buf, error_buf_size))) {
  11273. goto fail;
  11274. }
  11275. #if WASM_ENABLE_FAST_INTERP == 0
  11276. if (br_table_cache) {
  11277. br_table_cache->br_depths[i] = depth;
  11278. }
  11279. else {
  11280. if (depth > 255) {
  11281. /* The depth cannot be stored in one byte,
  11282. create br_table cache to store each depth */
  11283. #if WASM_ENABLE_DEBUG_INTERP != 0
  11284. if (!record_fast_op(module, p_opcode, *p_opcode,
  11285. error_buf, error_buf_size)) {
  11286. goto fail;
  11287. }
  11288. #endif
  11289. if (!(br_table_cache = loader_malloc(
  11290. offsetof(BrTableCache, br_depths)
  11291. + sizeof(uint32)
  11292. * (uint64)(count + 1),
  11293. error_buf, error_buf_size))) {
  11294. goto fail;
  11295. }
  11296. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  11297. br_table_cache->br_table_op_addr = p_opcode;
  11298. br_table_cache->br_count = count;
  11299. /* Copy previous depths which are one byte */
  11300. for (j = 0; j < i; j++) {
  11301. br_table_cache->br_depths[j] = p_depth_begin[j];
  11302. }
  11303. br_table_cache->br_depths[i] = depth;
  11304. bh_list_insert(module->br_table_cache_list,
  11305. br_table_cache);
  11306. }
  11307. else {
  11308. /* The depth can be stored in one byte, use the
  11309. byte of the leb to store it */
  11310. *p_depth++ = (uint8)depth;
  11311. }
  11312. }
  11313. #endif
  11314. }
  11315. #if WASM_ENABLE_FAST_INTERP == 0
  11316. /* Set the tailing bytes to nop */
  11317. if (br_table_cache)
  11318. p_depth = p_depth_begin;
  11319. while (p_depth < p)
  11320. *p_depth++ = WASM_OP_NOP;
  11321. #endif
  11322. RESET_STACK();
  11323. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11324. break;
  11325. }
  11326. case WASM_OP_RETURN:
  11327. {
  11328. WASMFuncType *func_type = func->func_type;
  11329. int32 idx;
  11330. uint8 ret_type;
  11331. #if WASM_ENABLE_GC != 0
  11332. uint32 j = func_type->ref_type_map_count - 1;
  11333. #endif
  11334. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  11335. idx--) {
  11336. ret_type =
  11337. *(func_type->types + func_type->param_count + idx);
  11338. #if WASM_ENABLE_GC != 0
  11339. if (wasm_is_type_multi_byte_type(ret_type)) {
  11340. WASMRefType *ref_type =
  11341. func_type->ref_type_maps[j].ref_type;
  11342. bh_assert(func_type->ref_type_maps[j].index
  11343. == func_type->param_count + idx);
  11344. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11345. ref_type,
  11346. wasm_reftype_struct_size(ref_type));
  11347. j--;
  11348. }
  11349. #endif
  11350. #if WASM_ENABLE_FAST_INTERP != 0
  11351. /* emit the offset after return opcode */
  11352. POP_OFFSET_TYPE(ret_type);
  11353. #endif
  11354. POP_TYPE(ret_type);
  11355. }
  11356. RESET_STACK();
  11357. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11358. break;
  11359. }
  11360. case WASM_OP_CALL:
  11361. #if WASM_ENABLE_TAIL_CALL != 0
  11362. case WASM_OP_RETURN_CALL:
  11363. #endif
  11364. #if WASM_ENABLE_GC != 0
  11365. case WASM_OP_CALL_REF:
  11366. case WASM_OP_RETURN_CALL_REF:
  11367. #endif
  11368. {
  11369. WASMFuncType *func_type;
  11370. uint8 type;
  11371. int32 idx;
  11372. #if WASM_ENABLE_GC != 0
  11373. WASMRefType *ref_type;
  11374. uint32 type_idx1;
  11375. int32 j;
  11376. #endif
  11377. #if WASM_ENABLE_GC != 0
  11378. if (opcode == WASM_OP_CALL_REF
  11379. || opcode == WASM_OP_RETURN_CALL_REF) {
  11380. pb_read_leb_uint32(p, p_end, type_idx1);
  11381. if (!check_type_index(module, module->type_count, type_idx1,
  11382. error_buf, error_buf_size)) {
  11383. goto fail;
  11384. }
  11385. if (module->types[type_idx1] == NULL
  11386. || module->types[type_idx1]->type_flag
  11387. != WASM_TYPE_FUNC) {
  11388. set_error_buf(error_buf, error_buf_size,
  11389. "unknown function type");
  11390. goto fail;
  11391. }
  11392. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  11393. &type_idx, error_buf,
  11394. error_buf_size)) {
  11395. goto fail;
  11396. }
  11397. if (type == VALUE_TYPE_ANY) {
  11398. type_idx = type_idx1;
  11399. }
  11400. if (!check_type_index(module, module->type_count, type_idx,
  11401. error_buf, error_buf_size)) {
  11402. goto fail;
  11403. }
  11404. if (module->types[type_idx] == NULL
  11405. || module->types[type_idx]->type_flag
  11406. != WASM_TYPE_FUNC) {
  11407. set_error_buf(error_buf, error_buf_size,
  11408. "unknown function type");
  11409. goto fail;
  11410. }
  11411. if (!wasm_func_type_is_super_of(
  11412. (WASMFuncType *)module->types[type_idx1],
  11413. (WASMFuncType *)module->types[type_idx])) {
  11414. set_error_buf(error_buf, error_buf_size,
  11415. "function type mismatch");
  11416. goto fail;
  11417. }
  11418. func_type = (WASMFuncType *)module->types[type_idx];
  11419. }
  11420. else
  11421. #endif
  11422. {
  11423. pb_read_leb_uint32(p, p_end, func_idx);
  11424. #if WASM_ENABLE_FAST_INTERP != 0
  11425. /* we need to emit func_idx before arguments */
  11426. emit_uint32(loader_ctx, func_idx);
  11427. #endif
  11428. if (!check_function_index(module, func_idx, error_buf,
  11429. error_buf_size)) {
  11430. goto fail;
  11431. }
  11432. if (func_idx < module->import_function_count)
  11433. func_type = module->import_functions[func_idx]
  11434. .u.function.func_type;
  11435. else
  11436. func_type =
  11437. module
  11438. ->functions[func_idx
  11439. - module->import_function_count]
  11440. ->func_type;
  11441. }
  11442. if (func_type->param_count > 0) {
  11443. #if WASM_ENABLE_GC != 0
  11444. j = (int32)(func_type->result_ref_type_maps
  11445. - func_type->ref_type_maps - 1);
  11446. #endif
  11447. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11448. idx--) {
  11449. #if WASM_ENABLE_GC != 0
  11450. if (wasm_is_type_multi_byte_type(
  11451. func_type->types[idx])) {
  11452. ref_type = func_type->ref_type_maps[j].ref_type;
  11453. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11454. ref_type,
  11455. wasm_reftype_struct_size(ref_type));
  11456. j--;
  11457. }
  11458. #endif
  11459. #if WASM_ENABLE_FAST_INTERP != 0
  11460. POP_OFFSET_TYPE(func_type->types[idx]);
  11461. #endif
  11462. POP_TYPE(func_type->types[idx]);
  11463. }
  11464. }
  11465. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11466. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  11467. #endif
  11468. #if WASM_ENABLE_GC != 0
  11469. j = (int32)(func_type->result_ref_type_maps
  11470. - func_type->ref_type_maps);
  11471. #endif
  11472. for (i = 0; i < func_type->result_count; i++) {
  11473. #if WASM_ENABLE_GC != 0
  11474. if (wasm_is_type_multi_byte_type(
  11475. func_type->types[func_type->param_count + i])) {
  11476. ref_type = func_type->ref_type_maps[j].ref_type;
  11477. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11478. ref_type,
  11479. wasm_reftype_struct_size(ref_type));
  11480. j++;
  11481. }
  11482. #endif
  11483. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11484. #if WASM_ENABLE_FAST_INTERP != 0
  11485. /* Here we emit each return value's dynamic_offset. But
  11486. * in fact these offsets are continuous, so interpreter
  11487. * only need to get the first return value's offset.
  11488. */
  11489. PUSH_OFFSET_TYPE(
  11490. func_type->types[func_type->param_count + i]);
  11491. #endif
  11492. }
  11493. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11494. }
  11495. else {
  11496. #if WASM_ENABLE_GC == 0
  11497. if (func_type->result_count
  11498. != func->func_type->result_count) {
  11499. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11500. "type mismatch: expect ",
  11501. func->func_type->result_count,
  11502. " return values but got other");
  11503. goto fail;
  11504. }
  11505. for (i = 0; i < func_type->result_count; i++) {
  11506. type = func->func_type
  11507. ->types[func->func_type->param_count + i];
  11508. if (func_type->types[func_type->param_count + i]
  11509. != type) {
  11510. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11511. "type mismatch: expect ",
  11512. type2str(type), " but got other");
  11513. goto fail;
  11514. }
  11515. }
  11516. #else
  11517. if (!wasm_func_type_result_is_subtype_of(
  11518. func_type, func->func_type, module->types,
  11519. module->type_count)) {
  11520. set_error_buf(
  11521. error_buf, error_buf_size,
  11522. "type mismatch: invalid func result types");
  11523. goto fail;
  11524. }
  11525. #endif
  11526. RESET_STACK();
  11527. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11528. }
  11529. #endif
  11530. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11531. || WASM_ENABLE_WAMR_COMPILER != 0
  11532. func->has_op_func_call = true;
  11533. #endif
  11534. (void)type;
  11535. break;
  11536. }
  11537. /*
  11538. * if disable reference type: call_indirect typeidx, 0x00
  11539. * if enable reference type: call_indirect typeidx, tableidx
  11540. */
  11541. case WASM_OP_CALL_INDIRECT:
  11542. #if WASM_ENABLE_TAIL_CALL != 0
  11543. case WASM_OP_RETURN_CALL_INDIRECT:
  11544. #endif
  11545. {
  11546. int32 idx;
  11547. WASMFuncType *func_type;
  11548. uint32 tbl_elem_type;
  11549. #if WASM_ENABLE_GC != 0
  11550. WASMRefType *elem_ref_type = NULL;
  11551. #endif
  11552. pb_read_leb_uint32(p, p_end, type_idx);
  11553. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11554. #if WASM_ENABLE_WAMR_COMPILER != 0
  11555. if (p + 1 < p_end && *p != 0x00) {
  11556. /*
  11557. * Any non-0x00 byte requires the ref types proposal.
  11558. * This is different from checking the table_idx value
  11559. * since `0x80 0x00` etc. are all valid encodings of zero.
  11560. */
  11561. module->is_ref_types_used = true;
  11562. }
  11563. #endif
  11564. pb_read_leb_uint32(p, p_end, table_idx);
  11565. #else
  11566. CHECK_BUF(p, p_end, 1);
  11567. table_idx = read_uint8(p);
  11568. #endif
  11569. if (!check_table_index(module, table_idx, error_buf,
  11570. error_buf_size)) {
  11571. goto fail;
  11572. }
  11573. tbl_elem_type =
  11574. table_idx < module->import_table_count
  11575. ? module->import_tables[table_idx]
  11576. .u.table.table_type.elem_type
  11577. : module->tables[table_idx - module->import_table_count]
  11578. .table_type.elem_type;
  11579. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  11580. if (tbl_elem_type != VALUE_TYPE_FUNCREF) {
  11581. set_error_buf_v(error_buf, error_buf_size,
  11582. "type mismatch: instruction requires table "
  11583. "of functions but table %u has externref",
  11584. table_idx);
  11585. goto fail;
  11586. }
  11587. #elif WASM_ENABLE_GC != 0
  11588. /* Table element must match type ref null func */
  11589. elem_ref_type =
  11590. table_idx < module->import_table_count
  11591. ? module->import_tables[table_idx]
  11592. .u.table.table_type.elem_ref_type
  11593. : module->tables[table_idx - module->import_table_count]
  11594. .table_type.elem_ref_type;
  11595. if (!wasm_reftype_is_subtype_of(
  11596. tbl_elem_type, elem_ref_type, REF_TYPE_FUNCREF, NULL,
  11597. module->types, module->type_count)) {
  11598. set_error_buf_v(error_buf, error_buf_size,
  11599. "type mismatch: instruction requires "
  11600. "reference type t match type ref null func"
  11601. "in table %u",
  11602. table_idx);
  11603. goto fail;
  11604. }
  11605. #else
  11606. (void)tbl_elem_type;
  11607. #endif
  11608. #if WASM_ENABLE_FAST_INTERP != 0
  11609. /* we need to emit before arguments */
  11610. #if WASM_ENABLE_TAIL_CALL != 0
  11611. emit_byte(loader_ctx, opcode);
  11612. #endif
  11613. emit_uint32(loader_ctx, type_idx);
  11614. emit_uint32(loader_ctx, table_idx);
  11615. #endif
  11616. #if WASM_ENABLE_MEMORY64 != 0
  11617. table_elem_idx_type = is_table_64bit(module, table_idx)
  11618. ? VALUE_TYPE_I64
  11619. : VALUE_TYPE_I32;
  11620. #endif
  11621. /* skip elem idx */
  11622. POP_TBL_ELEM_IDX();
  11623. if (!check_function_type(module, type_idx, error_buf,
  11624. error_buf_size)) {
  11625. goto fail;
  11626. }
  11627. func_type = (WASMFuncType *)module->types[type_idx];
  11628. if (func_type->param_count > 0) {
  11629. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11630. idx--) {
  11631. #if WASM_ENABLE_FAST_INTERP != 0
  11632. POP_OFFSET_TYPE(func_type->types[idx]);
  11633. #endif
  11634. POP_TYPE(func_type->types[idx]);
  11635. }
  11636. }
  11637. #if WASM_ENABLE_TAIL_CALL != 0
  11638. if (opcode == WASM_OP_CALL_INDIRECT) {
  11639. #endif
  11640. for (i = 0; i < func_type->result_count; i++) {
  11641. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11642. #if WASM_ENABLE_FAST_INTERP != 0
  11643. PUSH_OFFSET_TYPE(
  11644. func_type->types[func_type->param_count + i]);
  11645. #endif
  11646. }
  11647. #if WASM_ENABLE_TAIL_CALL != 0
  11648. }
  11649. else {
  11650. uint8 type;
  11651. if (func_type->result_count
  11652. != func->func_type->result_count) {
  11653. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11654. "type mismatch: expect ",
  11655. func->func_type->result_count,
  11656. " return values but got other");
  11657. goto fail;
  11658. }
  11659. for (i = 0; i < func_type->result_count; i++) {
  11660. type = func->func_type
  11661. ->types[func->func_type->param_count + i];
  11662. if (func_type->types[func_type->param_count + i]
  11663. != type) {
  11664. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11665. "type mismatch: expect ",
  11666. type2str(type), " but got other");
  11667. goto fail;
  11668. }
  11669. }
  11670. RESET_STACK();
  11671. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11672. }
  11673. #endif
  11674. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11675. || WASM_ENABLE_WAMR_COMPILER != 0
  11676. func->has_op_func_call = true;
  11677. #endif
  11678. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11679. func->has_op_call_indirect = true;
  11680. #endif
  11681. break;
  11682. }
  11683. case WASM_OP_DROP:
  11684. {
  11685. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11686. int32 available_stack_cell =
  11687. (int32)(loader_ctx->stack_cell_num
  11688. - cur_block->stack_cell_num);
  11689. if (available_stack_cell <= 0
  11690. && !cur_block->is_stack_polymorphic) {
  11691. set_error_buf(error_buf, error_buf_size,
  11692. "type mismatch, opcode drop was found "
  11693. "but stack was empty");
  11694. goto fail;
  11695. }
  11696. if (available_stack_cell > 0) {
  11697. #if WASM_ENABLE_GC != 0
  11698. if (wasm_is_type_multi_byte_type(
  11699. *(loader_ctx->frame_ref - 1))) {
  11700. bh_assert((int32)(loader_ctx->reftype_map_num
  11701. - cur_block->reftype_map_num)
  11702. > 0);
  11703. loader_ctx->frame_reftype_map--;
  11704. loader_ctx->reftype_map_num--;
  11705. }
  11706. #endif
  11707. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  11708. loader_ctx->frame_ref--;
  11709. loader_ctx->stack_cell_num--;
  11710. #if WASM_ENABLE_FAST_INTERP != 0
  11711. skip_label();
  11712. loader_ctx->frame_offset--;
  11713. if ((*(loader_ctx->frame_offset)
  11714. > loader_ctx->start_dynamic_offset)
  11715. && (*(loader_ctx->frame_offset)
  11716. < loader_ctx->max_dynamic_offset))
  11717. loader_ctx->dynamic_offset--;
  11718. #endif
  11719. }
  11720. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  11721. loader_ctx->frame_ref -= 2;
  11722. loader_ctx->stack_cell_num -= 2;
  11723. #if WASM_ENABLE_FAST_INTERP == 0
  11724. *(p - 1) = WASM_OP_DROP_64;
  11725. #endif
  11726. #if WASM_ENABLE_FAST_INTERP != 0
  11727. skip_label();
  11728. loader_ctx->frame_offset -= 2;
  11729. if ((*(loader_ctx->frame_offset)
  11730. > loader_ctx->start_dynamic_offset)
  11731. && (*(loader_ctx->frame_offset)
  11732. < loader_ctx->max_dynamic_offset))
  11733. loader_ctx->dynamic_offset -= 2;
  11734. #endif
  11735. }
  11736. #if WASM_ENABLE_SIMD != 0
  11737. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11738. || (WASM_ENABLE_FAST_INTERP != 0)
  11739. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  11740. loader_ctx->frame_ref -= 4;
  11741. loader_ctx->stack_cell_num -= 4;
  11742. #if WASM_ENABLE_FAST_INTERP != 0
  11743. skip_label();
  11744. loader_ctx->frame_offset -= 4;
  11745. if ((*(loader_ctx->frame_offset)
  11746. > loader_ctx->start_dynamic_offset)
  11747. && (*(loader_ctx->frame_offset)
  11748. < loader_ctx->max_dynamic_offset))
  11749. loader_ctx->dynamic_offset -= 4;
  11750. #endif
  11751. }
  11752. #endif
  11753. #endif
  11754. else {
  11755. set_error_buf(error_buf, error_buf_size,
  11756. "type mismatch");
  11757. goto fail;
  11758. }
  11759. }
  11760. else {
  11761. #if WASM_ENABLE_FAST_INTERP != 0
  11762. skip_label();
  11763. #endif
  11764. }
  11765. break;
  11766. }
  11767. case WASM_OP_SELECT:
  11768. {
  11769. uint8 ref_type;
  11770. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11771. int32 available_stack_cell;
  11772. #if WASM_ENABLE_FAST_INTERP != 0
  11773. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11774. #endif
  11775. POP_I32();
  11776. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  11777. - cur_block->stack_cell_num);
  11778. if (available_stack_cell <= 0
  11779. && !cur_block->is_stack_polymorphic) {
  11780. set_error_buf(error_buf, error_buf_size,
  11781. "type mismatch or invalid result arity, "
  11782. "opcode select was found "
  11783. "but stack was empty");
  11784. goto fail;
  11785. }
  11786. if (available_stack_cell > 0) {
  11787. switch (*(loader_ctx->frame_ref - 1)) {
  11788. case VALUE_TYPE_I32:
  11789. case VALUE_TYPE_F32:
  11790. case VALUE_TYPE_ANY:
  11791. break;
  11792. case VALUE_TYPE_I64:
  11793. case VALUE_TYPE_F64:
  11794. #if WASM_ENABLE_FAST_INTERP == 0
  11795. *(p - 1) = WASM_OP_SELECT_64;
  11796. #else
  11797. if (loader_ctx->p_code_compiled) {
  11798. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11799. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11800. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11801. *(void **)(p_code_compiled_tmp
  11802. - sizeof(void *)) =
  11803. handle_table[opcode_tmp];
  11804. #elif UINTPTR_MAX == UINT64_MAX
  11805. /* emit int32 relative offset in 64-bit target
  11806. */
  11807. int32 offset =
  11808. (int32)((uint8 *)handle_table[opcode_tmp]
  11809. - (uint8 *)handle_table[0]);
  11810. *(int32 *)(p_code_compiled_tmp
  11811. - sizeof(int32)) = offset;
  11812. #else
  11813. /* emit uint32 label address in 32-bit target */
  11814. *(uint32 *)(p_code_compiled_tmp
  11815. - sizeof(uint32)) =
  11816. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11817. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11818. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11819. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11820. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11821. #else
  11822. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11823. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11824. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11825. }
  11826. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11827. break;
  11828. #if WASM_ENABLE_SIMD != 0
  11829. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11830. || (WASM_ENABLE_FAST_INTERP != 0)
  11831. case VALUE_TYPE_V128:
  11832. #if WASM_ENABLE_SIMDE != 0
  11833. if (loader_ctx->p_code_compiled) {
  11834. uint8 opcode_tmp = WASM_OP_SELECT_128;
  11835. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11836. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11837. *(void **)(p_code_compiled_tmp
  11838. - sizeof(void *)) =
  11839. handle_table[opcode_tmp];
  11840. #elif UINTPTR_MAX == UINT64_MAX
  11841. /* emit int32 relative offset in 64-bit target
  11842. */
  11843. int32 offset =
  11844. (int32)((uint8 *)handle_table[opcode_tmp]
  11845. - (uint8 *)handle_table[0]);
  11846. *(int32 *)(p_code_compiled_tmp
  11847. - sizeof(int32)) = offset;
  11848. #else
  11849. /* emit uint32 label address in 32-bit target */
  11850. *(uint32 *)(p_code_compiled_tmp
  11851. - sizeof(uint32)) =
  11852. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11853. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11854. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11855. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11856. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11857. #else
  11858. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11859. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11860. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11861. }
  11862. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11863. break;
  11864. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  11865. (WASM_ENABLE_FAST_INTERP != 0) */
  11866. #endif /* WASM_ENABLE_SIMD != 0 */
  11867. default:
  11868. {
  11869. set_error_buf(error_buf, error_buf_size,
  11870. "type mismatch");
  11871. goto fail;
  11872. }
  11873. }
  11874. ref_type = *(loader_ctx->frame_ref - 1);
  11875. #if WASM_ENABLE_FAST_INTERP != 0
  11876. POP_OFFSET_TYPE(ref_type);
  11877. POP_TYPE(ref_type);
  11878. POP_OFFSET_TYPE(ref_type);
  11879. POP_TYPE(ref_type);
  11880. PUSH_OFFSET_TYPE(ref_type);
  11881. PUSH_TYPE(ref_type);
  11882. #else
  11883. POP2_AND_PUSH(ref_type, ref_type);
  11884. #endif
  11885. }
  11886. else {
  11887. #if WASM_ENABLE_FAST_INTERP != 0
  11888. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11889. #endif
  11890. PUSH_TYPE(VALUE_TYPE_ANY);
  11891. }
  11892. break;
  11893. }
  11894. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11895. case WASM_OP_SELECT_T:
  11896. {
  11897. uint8 vec_len, type;
  11898. #if WASM_ENABLE_GC != 0
  11899. WASMRefType *ref_type = NULL;
  11900. #endif
  11901. #if WASM_ENABLE_FAST_INTERP != 0
  11902. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11903. #endif
  11904. pb_read_leb_uint32(p, p_end, vec_len);
  11905. if (vec_len != 1) {
  11906. /* typed select must have exactly one result */
  11907. set_error_buf(error_buf, error_buf_size,
  11908. "invalid result arity");
  11909. goto fail;
  11910. }
  11911. #if WASM_ENABLE_GC == 0
  11912. CHECK_BUF(p, p_end, 1);
  11913. type = read_uint8(p);
  11914. if (!is_valid_value_type_for_interpreter(type)) {
  11915. set_error_buf(error_buf, error_buf_size,
  11916. "unknown value type");
  11917. goto fail;
  11918. }
  11919. #else
  11920. p_org = p + 1;
  11921. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11922. module->type_count, &need_ref_type_map,
  11923. &wasm_ref_type, false, error_buf,
  11924. error_buf_size)) {
  11925. goto fail;
  11926. }
  11927. type = wasm_ref_type.ref_type;
  11928. if (need_ref_type_map) {
  11929. if (!(ref_type = reftype_set_insert(
  11930. module->ref_type_set, &wasm_ref_type, error_buf,
  11931. error_buf_size))) {
  11932. goto fail;
  11933. }
  11934. }
  11935. #if WASM_ENABLE_FAST_INTERP == 0
  11936. while (p_org < p) {
  11937. #if WASM_ENABLE_DEBUG_INTERP != 0
  11938. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11939. error_buf_size)) {
  11940. goto fail;
  11941. }
  11942. #endif
  11943. /* Ignore extra bytes for interpreter */
  11944. *p_org++ = WASM_OP_NOP;
  11945. }
  11946. #endif
  11947. #endif /* end of WASM_ENABLE_GC == 0 */
  11948. POP_I32();
  11949. #if WASM_ENABLE_FAST_INTERP != 0
  11950. if (loader_ctx->p_code_compiled) {
  11951. uint8 opcode_tmp = WASM_OP_SELECT;
  11952. if (type == VALUE_TYPE_V128) {
  11953. #if WASM_ENABLE_SIMDE != 0
  11954. opcode_tmp = WASM_OP_SELECT_128;
  11955. #else
  11956. set_error_buf(error_buf, error_buf_size,
  11957. "v128 value type requires simd feature");
  11958. #endif
  11959. }
  11960. else {
  11961. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11962. opcode_tmp = WASM_OP_SELECT_64;
  11963. #if WASM_ENABLE_GC != 0
  11964. if (wasm_is_type_reftype(type))
  11965. opcode_tmp = WASM_OP_SELECT_T;
  11966. #endif
  11967. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11968. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11969. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11970. handle_table[opcode_tmp];
  11971. #else
  11972. #if UINTPTR_MAX == UINT64_MAX
  11973. /* emit int32 relative offset in 64-bit target */
  11974. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11975. - (uint8 *)handle_table[0]);
  11976. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11977. offset;
  11978. #else
  11979. /* emit uint32 label address in 32-bit target */
  11980. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11981. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11982. #endif
  11983. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11984. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11985. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11986. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11987. #else
  11988. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11989. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11990. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11991. }
  11992. }
  11993. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11994. POP_REF(type);
  11995. #if WASM_ENABLE_GC != 0
  11996. if (need_ref_type_map) {
  11997. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11998. wasm_reftype_struct_size(ref_type));
  11999. }
  12000. #endif
  12001. POP_REF(type);
  12002. #if WASM_ENABLE_GC != 0
  12003. if (need_ref_type_map) {
  12004. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12005. wasm_reftype_struct_size(ref_type));
  12006. }
  12007. #endif
  12008. PUSH_REF(type);
  12009. #if WASM_ENABLE_WAMR_COMPILER != 0
  12010. module->is_ref_types_used = true;
  12011. #endif
  12012. (void)vec_len;
  12013. break;
  12014. }
  12015. /* table.get x. tables[x]. [it] -> [t] */
  12016. /* table.set x. tables[x]. [it t] -> [] */
  12017. case WASM_OP_TABLE_GET:
  12018. case WASM_OP_TABLE_SET:
  12019. {
  12020. uint8 decl_ref_type;
  12021. #if WASM_ENABLE_GC != 0
  12022. WASMRefType *ref_type;
  12023. #endif
  12024. pb_read_leb_uint32(p, p_end, table_idx);
  12025. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  12026. #if WASM_ENABLE_GC != 0
  12027. (void **)&ref_type,
  12028. #else
  12029. NULL,
  12030. #endif
  12031. error_buf, error_buf_size))
  12032. goto fail;
  12033. #if WASM_ENABLE_GC != 0
  12034. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  12035. bh_assert(ref_type);
  12036. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  12037. ref_type, wasm_reftype_struct_size(ref_type));
  12038. }
  12039. #endif
  12040. #if WASM_ENABLE_FAST_INTERP != 0
  12041. emit_uint32(loader_ctx, table_idx);
  12042. #endif
  12043. #if WASM_ENABLE_MEMORY64 != 0
  12044. table_elem_idx_type = is_table_64bit(module, table_idx)
  12045. ? VALUE_TYPE_I64
  12046. : VALUE_TYPE_I32;
  12047. #endif
  12048. if (opcode == WASM_OP_TABLE_GET) {
  12049. POP_TBL_ELEM_IDX();
  12050. #if WASM_ENABLE_FAST_INTERP != 0
  12051. PUSH_OFFSET_TYPE(decl_ref_type);
  12052. #endif
  12053. PUSH_TYPE(decl_ref_type);
  12054. }
  12055. else {
  12056. #if WASM_ENABLE_FAST_INTERP != 0
  12057. POP_OFFSET_TYPE(decl_ref_type);
  12058. #endif
  12059. POP_TYPE(decl_ref_type);
  12060. POP_TBL_ELEM_IDX();
  12061. }
  12062. #if WASM_ENABLE_WAMR_COMPILER != 0
  12063. module->is_ref_types_used = true;
  12064. #endif
  12065. break;
  12066. }
  12067. case WASM_OP_REF_NULL:
  12068. {
  12069. uint8 ref_type;
  12070. #if WASM_ENABLE_GC == 0
  12071. CHECK_BUF(p, p_end, 1);
  12072. ref_type = read_uint8(p);
  12073. if (ref_type != VALUE_TYPE_FUNCREF
  12074. && ref_type != VALUE_TYPE_EXTERNREF) {
  12075. set_error_buf(error_buf, error_buf_size, "type mismatch");
  12076. goto fail;
  12077. }
  12078. #else
  12079. pb_read_leb_int32(p, p_end, heap_type);
  12080. if (heap_type >= 0) {
  12081. if (!check_type_index(module, module->type_count, heap_type,
  12082. error_buf, error_buf_size)) {
  12083. goto fail;
  12084. }
  12085. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  12086. true, heap_type);
  12087. ref_type = wasm_ref_type.ref_type;
  12088. }
  12089. else {
  12090. if (!wasm_is_valid_heap_type(heap_type)) {
  12091. set_error_buf(error_buf, error_buf_size,
  12092. "unknown type");
  12093. goto fail;
  12094. }
  12095. ref_type = (uint8)((int32)0x80 + heap_type);
  12096. }
  12097. #endif /* end of WASM_ENABLE_GC == 0 */
  12098. #if WASM_ENABLE_FAST_INTERP != 0
  12099. PUSH_OFFSET_TYPE(ref_type);
  12100. #endif
  12101. PUSH_TYPE(ref_type);
  12102. #if WASM_ENABLE_WAMR_COMPILER != 0
  12103. module->is_ref_types_used = true;
  12104. #endif
  12105. break;
  12106. }
  12107. case WASM_OP_REF_IS_NULL:
  12108. {
  12109. #if WASM_ENABLE_GC == 0
  12110. #if WASM_ENABLE_FAST_INTERP != 0
  12111. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  12112. int32 block_stack_cell_num =
  12113. (int32)(loader_ctx->stack_cell_num
  12114. - cur_block->stack_cell_num);
  12115. if (block_stack_cell_num <= 0) {
  12116. if (!cur_block->is_stack_polymorphic) {
  12117. set_error_buf(
  12118. error_buf, error_buf_size,
  12119. "type mismatch: expect data but stack was empty");
  12120. goto fail;
  12121. }
  12122. }
  12123. else {
  12124. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  12125. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  12126. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  12127. if (!wasm_loader_pop_frame_ref_offset(
  12128. loader_ctx, *(loader_ctx->frame_ref - 1),
  12129. error_buf, error_buf_size)) {
  12130. goto fail;
  12131. }
  12132. }
  12133. else {
  12134. set_error_buf(error_buf, error_buf_size,
  12135. "type mismatch");
  12136. goto fail;
  12137. }
  12138. }
  12139. #else
  12140. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  12141. error_buf, error_buf_size)
  12142. && !wasm_loader_pop_frame_ref(loader_ctx,
  12143. VALUE_TYPE_EXTERNREF,
  12144. error_buf, error_buf_size)) {
  12145. goto fail;
  12146. }
  12147. #endif
  12148. #else /* else of WASM_ENABLE_GC == 0 */
  12149. uint8 type;
  12150. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  12151. error_buf, error_buf_size)) {
  12152. goto fail;
  12153. }
  12154. #endif
  12155. PUSH_I32();
  12156. #if WASM_ENABLE_WAMR_COMPILER != 0
  12157. module->is_ref_types_used = true;
  12158. #endif
  12159. break;
  12160. }
  12161. case WASM_OP_REF_FUNC:
  12162. {
  12163. pb_read_leb_uint32(p, p_end, func_idx);
  12164. if (!check_function_index(module, func_idx, error_buf,
  12165. error_buf_size)) {
  12166. goto fail;
  12167. }
  12168. /* Refer to a forward-declared function:
  12169. the function must be an import, exported, or present in
  12170. a table elem segment or global initializer to be used as
  12171. the operand to ref.func */
  12172. if (func_idx >= module->import_function_count) {
  12173. WASMTableSeg *table_seg = module->table_segments;
  12174. bool func_declared = false;
  12175. uint32 j;
  12176. for (i = 0; i < module->global_count; i++) {
  12177. if (module->globals[i].type.val_type
  12178. == VALUE_TYPE_FUNCREF
  12179. && module->globals[i].init_expr.init_expr_type
  12180. == INIT_EXPR_TYPE_FUNCREF_CONST
  12181. && module->globals[i].init_expr.u.unary.v.u32
  12182. == func_idx) {
  12183. func_declared = true;
  12184. break;
  12185. }
  12186. }
  12187. if (!func_declared) {
  12188. /* Check whether the function is declared in table segs,
  12189. note that it doesn't matter whether the table seg's
  12190. mode is passive, active or declarative. */
  12191. for (i = 0; i < module->table_seg_count;
  12192. i++, table_seg++) {
  12193. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  12194. #if WASM_ENABLE_GC != 0
  12195. /* elem type is (ref null? func) or
  12196. (ref null? $t) */
  12197. || ((table_seg->elem_type
  12198. == REF_TYPE_HT_NON_NULLABLE
  12199. || table_seg->elem_type
  12200. == REF_TYPE_HT_NULLABLE)
  12201. && (table_seg->elem_ref_type->ref_ht_common
  12202. .heap_type
  12203. == HEAP_TYPE_FUNC
  12204. || table_seg->elem_ref_type
  12205. ->ref_ht_common.heap_type
  12206. > 0))
  12207. #endif
  12208. ) {
  12209. for (j = 0; j < table_seg->value_count; j++) {
  12210. if (table_seg->init_values[j]
  12211. .u.unary.v.ref_index
  12212. == func_idx) {
  12213. func_declared = true;
  12214. break;
  12215. }
  12216. }
  12217. }
  12218. }
  12219. }
  12220. if (!func_declared) {
  12221. /* Check whether the function is exported */
  12222. for (i = 0; i < module->export_count; i++) {
  12223. if (module->exports[i].kind == EXPORT_KIND_FUNC
  12224. && module->exports[i].index == func_idx) {
  12225. func_declared = true;
  12226. break;
  12227. }
  12228. }
  12229. }
  12230. if (!func_declared) {
  12231. set_error_buf(error_buf, error_buf_size,
  12232. "undeclared function reference");
  12233. goto fail;
  12234. }
  12235. }
  12236. #if WASM_ENABLE_FAST_INTERP != 0
  12237. emit_uint32(loader_ctx, func_idx);
  12238. #endif
  12239. #if WASM_ENABLE_GC == 0
  12240. PUSH_FUNCREF();
  12241. #else
  12242. if (func_idx < module->import_function_count)
  12243. type_idx =
  12244. module->import_functions[func_idx].u.function.type_idx;
  12245. else
  12246. type_idx = module
  12247. ->functions[func_idx
  12248. - module->import_function_count]
  12249. ->type_idx;
  12250. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  12251. false, type_idx);
  12252. PUSH_REF(wasm_ref_type.ref_type);
  12253. #endif
  12254. #if WASM_ENABLE_WAMR_COMPILER != 0
  12255. module->is_ref_types_used = true;
  12256. #endif
  12257. break;
  12258. }
  12259. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  12260. #if WASM_ENABLE_GC != 0
  12261. case WASM_OP_REF_AS_NON_NULL:
  12262. case WASM_OP_BR_ON_NULL:
  12263. {
  12264. uint8 type;
  12265. WASMRefType ref_type;
  12266. /* POP (ref null ht) and get the converted (ref ht) */
  12267. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  12268. error_buf, error_buf_size)) {
  12269. goto fail;
  12270. }
  12271. if (opcode == WASM_OP_BR_ON_NULL) {
  12272. if (!(frame_csp_tmp =
  12273. check_branch_block(loader_ctx, &p, p_end, opcode,
  12274. error_buf, error_buf_size))) {
  12275. goto fail;
  12276. }
  12277. #if WASM_ENABLE_FAST_INTERP != 0
  12278. disable_emit = true;
  12279. #endif
  12280. }
  12281. /* PUSH the converted (ref ht) */
  12282. if (type != VALUE_TYPE_ANY) {
  12283. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  12284. sizeof(WASMRefType));
  12285. }
  12286. PUSH_REF(type);
  12287. break;
  12288. }
  12289. case WASM_OP_BR_ON_NON_NULL:
  12290. {
  12291. uint8 type;
  12292. WASMRefType ref_type;
  12293. uint32 available_stack_cell =
  12294. loader_ctx->stack_cell_num
  12295. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  12296. /* POP (ref null ht) and get the converted (ref ht) */
  12297. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  12298. error_buf, error_buf_size)) {
  12299. goto fail;
  12300. }
  12301. #if WASM_ENABLE_FAST_INTERP != 0
  12302. disable_emit = true;
  12303. #endif
  12304. /* Temporarily PUSH back (ref ht), check brach block and
  12305. then POP it */
  12306. if (available_stack_cell
  12307. > 0) { /* stack isn't in polymorphic state */
  12308. if (type != VALUE_TYPE_ANY) {
  12309. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12310. &ref_type, sizeof(WASMRefType));
  12311. }
  12312. PUSH_REF(type);
  12313. }
  12314. if (!(frame_csp_tmp =
  12315. check_branch_block(loader_ctx, &p, p_end, opcode,
  12316. error_buf, error_buf_size))) {
  12317. goto fail;
  12318. }
  12319. if (available_stack_cell
  12320. > 0) { /* stack isn't in polymorphic state */
  12321. POP_REF(type);
  12322. #if WASM_ENABLE_FAST_INTERP != 0
  12323. /* Erase the opnd offset emitted by POP_REF() */
  12324. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12325. #endif
  12326. }
  12327. break;
  12328. }
  12329. case WASM_OP_REF_EQ:
  12330. POP_REF(REF_TYPE_EQREF);
  12331. POP_REF(REF_TYPE_EQREF);
  12332. PUSH_I32();
  12333. break;
  12334. #endif /* end of WASM_ENABLE_GC != 0 */
  12335. case WASM_OP_GET_LOCAL:
  12336. {
  12337. p_org = p - 1;
  12338. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12339. PUSH_TYPE(local_type);
  12340. #if WASM_ENABLE_GC != 0
  12341. /* Cannot get a non-nullable and unset local */
  12342. if (local_idx >= param_count
  12343. && wasm_is_reftype_htref_non_nullable(local_type)
  12344. && !wasm_loader_get_local_status(loader_ctx,
  12345. local_idx - param_count)) {
  12346. set_error_buf(error_buf, error_buf_size,
  12347. "uninitialized local");
  12348. goto fail;
  12349. }
  12350. #endif
  12351. #if WASM_ENABLE_FAST_INTERP != 0
  12352. /* Get Local is optimized out */
  12353. skip_label();
  12354. disable_emit = true;
  12355. operand_offset = local_offset;
  12356. PUSH_OFFSET_TYPE(local_type);
  12357. #else
  12358. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12359. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12360. if (local_offset < 0x80
  12361. #if WASM_ENABLE_GC != 0
  12362. && !wasm_is_type_reftype(local_type)
  12363. #endif
  12364. ) {
  12365. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  12366. if (is_32bit_type(local_type)) {
  12367. *p_org++ = (uint8)local_offset;
  12368. }
  12369. else {
  12370. *p_org++ = (uint8)(local_offset | 0x80);
  12371. }
  12372. while (p_org < p) {
  12373. *p_org++ = WASM_OP_NOP;
  12374. }
  12375. }
  12376. #endif
  12377. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12378. break;
  12379. }
  12380. case WASM_OP_SET_LOCAL:
  12381. {
  12382. p_org = p - 1;
  12383. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12384. #if WASM_ENABLE_FAST_INTERP != 0
  12385. if (!(preserve_referenced_local(
  12386. loader_ctx, opcode, local_offset, local_type,
  12387. &preserve_local, error_buf, error_buf_size)))
  12388. goto fail;
  12389. if (local_offset < 256
  12390. #if WASM_ENABLE_GC != 0
  12391. && !wasm_is_type_reftype(local_type)
  12392. #endif
  12393. ) {
  12394. skip_label();
  12395. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  12396. if (loader_ctx->p_code_compiled)
  12397. STORE_U16(loader_ctx->p_code_compiled - 2,
  12398. local_offset);
  12399. loader_ctx->frame_offset--;
  12400. loader_ctx->dynamic_offset--;
  12401. }
  12402. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  12403. if (loader_ctx->p_code_compiled)
  12404. STORE_U16(loader_ctx->p_code_compiled - 2,
  12405. local_offset);
  12406. loader_ctx->frame_offset -= 2;
  12407. loader_ctx->dynamic_offset -= 2;
  12408. }
  12409. else {
  12410. if (is_32bit_type(local_type)) {
  12411. emit_label(EXT_OP_SET_LOCAL_FAST);
  12412. emit_byte(loader_ctx, (uint8)local_offset);
  12413. }
  12414. else if (is_64bit_type(local_type)) {
  12415. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  12416. emit_byte(loader_ctx, (uint8)local_offset);
  12417. }
  12418. #if WASM_ENABLE_SIMDE != 0
  12419. else if (local_type == VALUE_TYPE_V128) {
  12420. emit_label(EXT_OP_SET_LOCAL_FAST_V128);
  12421. emit_byte(loader_ctx, (uint8)local_offset);
  12422. }
  12423. #endif
  12424. else {
  12425. set_error_buf(error_buf, error_buf_size,
  12426. "unknown local type");
  12427. goto fail;
  12428. }
  12429. POP_OFFSET_TYPE(local_type);
  12430. }
  12431. }
  12432. else { /* local index larger than 255, reserve leb */
  12433. emit_uint32(loader_ctx, local_idx);
  12434. POP_OFFSET_TYPE(local_type);
  12435. }
  12436. #else
  12437. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12438. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12439. if (local_offset < 0x80
  12440. #if WASM_ENABLE_GC != 0
  12441. && !wasm_is_type_reftype(local_type)
  12442. #endif
  12443. ) {
  12444. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  12445. if (is_32bit_type(local_type)) {
  12446. *p_org++ = (uint8)local_offset;
  12447. }
  12448. else {
  12449. *p_org++ = (uint8)(local_offset | 0x80);
  12450. }
  12451. while (p_org < p) {
  12452. *p_org++ = WASM_OP_NOP;
  12453. }
  12454. }
  12455. #endif
  12456. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12457. #if WASM_ENABLE_GC != 0
  12458. if (local_idx >= param_count) {
  12459. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12460. }
  12461. #endif
  12462. POP_TYPE(local_type);
  12463. break;
  12464. }
  12465. case WASM_OP_TEE_LOCAL:
  12466. {
  12467. p_org = p - 1;
  12468. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12469. #if WASM_ENABLE_FAST_INTERP != 0
  12470. /* If the stack is in polymorphic state, do fake pop and push on
  12471. offset stack to keep the depth of offset stack to be the
  12472. same with ref stack */
  12473. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  12474. if (cur_block->is_stack_polymorphic) {
  12475. POP_OFFSET_TYPE(local_type);
  12476. PUSH_OFFSET_TYPE(local_type);
  12477. }
  12478. #endif
  12479. POP_TYPE(local_type);
  12480. PUSH_TYPE(local_type);
  12481. #if WASM_ENABLE_FAST_INTERP != 0
  12482. if (!(preserve_referenced_local(
  12483. loader_ctx, opcode, local_offset, local_type,
  12484. &preserve_local, error_buf, error_buf_size)))
  12485. goto fail;
  12486. if (local_offset < 256
  12487. #if WASM_ENABLE_GC != 0
  12488. && !wasm_is_type_reftype(local_type)
  12489. #endif
  12490. ) {
  12491. skip_label();
  12492. if (is_32bit_type(local_type)) {
  12493. emit_label(EXT_OP_TEE_LOCAL_FAST);
  12494. emit_byte(loader_ctx, (uint8)local_offset);
  12495. }
  12496. #if WASM_ENABLE_SIMDE != 0
  12497. else if (local_type == VALUE_TYPE_V128) {
  12498. emit_label(EXT_OP_TEE_LOCAL_FAST_V128);
  12499. emit_byte(loader_ctx, (uint8)local_offset);
  12500. }
  12501. #endif
  12502. else {
  12503. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  12504. emit_byte(loader_ctx, (uint8)local_offset);
  12505. }
  12506. }
  12507. else { /* local index larger than 255, reserve leb */
  12508. emit_uint32(loader_ctx, local_idx);
  12509. }
  12510. emit_operand(loader_ctx,
  12511. *(loader_ctx->frame_offset
  12512. - wasm_value_type_cell_num(local_type)));
  12513. #else
  12514. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12515. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12516. if (local_offset < 0x80
  12517. #if WASM_ENABLE_GC != 0
  12518. && !wasm_is_type_reftype(local_type)
  12519. #endif
  12520. ) {
  12521. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  12522. if (is_32bit_type(local_type)) {
  12523. *p_org++ = (uint8)local_offset;
  12524. }
  12525. else {
  12526. *p_org++ = (uint8)(local_offset | 0x80);
  12527. }
  12528. while (p_org < p) {
  12529. *p_org++ = WASM_OP_NOP;
  12530. }
  12531. }
  12532. #endif
  12533. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12534. #if WASM_ENABLE_GC != 0
  12535. if (local_idx >= param_count) {
  12536. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12537. }
  12538. #endif
  12539. break;
  12540. }
  12541. case WASM_OP_GET_GLOBAL:
  12542. {
  12543. #if WASM_ENABLE_GC != 0
  12544. WASMRefType *ref_type;
  12545. #endif
  12546. p_org = p - 1;
  12547. pb_read_leb_uint32(p, p_end, global_idx);
  12548. if (global_idx >= global_count) {
  12549. set_error_buf(error_buf, error_buf_size, "unknown global");
  12550. goto fail;
  12551. }
  12552. global_type = global_idx < module->import_global_count
  12553. ? module->import_globals[global_idx]
  12554. .u.global.type.val_type
  12555. : module
  12556. ->globals[global_idx
  12557. - module->import_global_count]
  12558. .type.val_type;
  12559. #if WASM_ENABLE_GC != 0
  12560. ref_type =
  12561. global_idx < module->import_global_count
  12562. ? module->import_globals[global_idx].u.global.ref_type
  12563. : module
  12564. ->globals[global_idx
  12565. - module->import_global_count]
  12566. .ref_type;
  12567. if (wasm_is_type_multi_byte_type(global_type)) {
  12568. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12569. wasm_reftype_struct_size(ref_type));
  12570. }
  12571. #endif
  12572. PUSH_TYPE(global_type);
  12573. #if WASM_ENABLE_FAST_INTERP == 0
  12574. if (global_type == VALUE_TYPE_I64
  12575. || global_type == VALUE_TYPE_F64) {
  12576. #if WASM_ENABLE_DEBUG_INTERP != 0
  12577. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12578. error_buf_size)) {
  12579. goto fail;
  12580. }
  12581. #endif
  12582. *p_org = WASM_OP_GET_GLOBAL_64;
  12583. }
  12584. #else /* else of WASM_ENABLE_FAST_INTERP */
  12585. if (global_type == VALUE_TYPE_I64
  12586. || global_type == VALUE_TYPE_F64) {
  12587. skip_label();
  12588. emit_label(WASM_OP_GET_GLOBAL_64);
  12589. }
  12590. #if WASM_ENABLE_SIMDE != 0
  12591. if (global_type == VALUE_TYPE_V128) {
  12592. skip_label();
  12593. emit_label(WASM_OP_GET_GLOBAL_V128);
  12594. }
  12595. #endif /* end of WASM_ENABLE_SIMDE */
  12596. emit_uint32(loader_ctx, global_idx);
  12597. PUSH_OFFSET_TYPE(global_type);
  12598. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12599. break;
  12600. }
  12601. case WASM_OP_SET_GLOBAL:
  12602. {
  12603. bool is_mutable = false;
  12604. #if WASM_ENABLE_GC != 0
  12605. WASMRefType *ref_type;
  12606. #endif
  12607. p_org = p - 1;
  12608. pb_read_leb_uint32(p, p_end, global_idx);
  12609. if (global_idx >= global_count) {
  12610. set_error_buf(error_buf, error_buf_size, "unknown global");
  12611. goto fail;
  12612. }
  12613. is_mutable = global_idx < module->import_global_count
  12614. ? module->import_globals[global_idx]
  12615. .u.global.type.is_mutable
  12616. : module
  12617. ->globals[global_idx
  12618. - module->import_global_count]
  12619. .type.is_mutable;
  12620. if (!is_mutable) {
  12621. #if WASM_ENABLE_GC == 0
  12622. set_error_buf(error_buf, error_buf_size,
  12623. "global is immutable");
  12624. #else
  12625. set_error_buf(error_buf, error_buf_size,
  12626. "immutable global");
  12627. #endif
  12628. goto fail;
  12629. }
  12630. global_type = global_idx < module->import_global_count
  12631. ? module->import_globals[global_idx]
  12632. .u.global.type.val_type
  12633. : module
  12634. ->globals[global_idx
  12635. - module->import_global_count]
  12636. .type.val_type;
  12637. #if WASM_ENABLE_GC != 0
  12638. ref_type =
  12639. global_idx < module->import_global_count
  12640. ? module->import_globals[global_idx].u.global.ref_type
  12641. : module
  12642. ->globals[global_idx
  12643. - module->import_global_count]
  12644. .ref_type;
  12645. if (wasm_is_type_multi_byte_type(global_type)) {
  12646. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12647. wasm_reftype_struct_size(ref_type));
  12648. }
  12649. #endif
  12650. #if WASM_ENABLE_FAST_INTERP == 0
  12651. if (global_type == VALUE_TYPE_I64
  12652. || global_type == VALUE_TYPE_F64) {
  12653. #if WASM_ENABLE_DEBUG_INTERP != 0
  12654. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12655. error_buf_size)) {
  12656. goto fail;
  12657. }
  12658. #endif
  12659. *p_org = WASM_OP_SET_GLOBAL_64;
  12660. }
  12661. else if (module->aux_stack_size > 0
  12662. && global_idx == module->aux_stack_top_global_index) {
  12663. #if WASM_ENABLE_DEBUG_INTERP != 0
  12664. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12665. error_buf_size)) {
  12666. goto fail;
  12667. }
  12668. #endif
  12669. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  12670. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12671. func->has_op_set_global_aux_stack = true;
  12672. #endif
  12673. }
  12674. #else /* else of WASM_ENABLE_FAST_INTERP */
  12675. if (global_type == VALUE_TYPE_I64
  12676. || global_type == VALUE_TYPE_F64) {
  12677. skip_label();
  12678. emit_label(WASM_OP_SET_GLOBAL_64);
  12679. }
  12680. else if (module->aux_stack_size > 0
  12681. && global_idx == module->aux_stack_top_global_index) {
  12682. skip_label();
  12683. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  12684. }
  12685. #if WASM_ENABLE_SIMDE != 0
  12686. else if (global_type == VALUE_TYPE_V128) {
  12687. skip_label();
  12688. emit_label(WASM_OP_SET_GLOBAL_V128);
  12689. }
  12690. #endif /* end of WASM_ENABLE_SIMDE */
  12691. emit_uint32(loader_ctx, global_idx);
  12692. POP_OFFSET_TYPE(global_type);
  12693. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12694. POP_TYPE(global_type);
  12695. break;
  12696. }
  12697. /* load */
  12698. case WASM_OP_I32_LOAD:
  12699. case WASM_OP_I32_LOAD8_S:
  12700. case WASM_OP_I32_LOAD8_U:
  12701. case WASM_OP_I32_LOAD16_S:
  12702. case WASM_OP_I32_LOAD16_U:
  12703. case WASM_OP_I64_LOAD:
  12704. case WASM_OP_I64_LOAD8_S:
  12705. case WASM_OP_I64_LOAD8_U:
  12706. case WASM_OP_I64_LOAD16_S:
  12707. case WASM_OP_I64_LOAD16_U:
  12708. case WASM_OP_I64_LOAD32_S:
  12709. case WASM_OP_I64_LOAD32_U:
  12710. case WASM_OP_F32_LOAD:
  12711. case WASM_OP_F64_LOAD:
  12712. /* store */
  12713. case WASM_OP_I32_STORE:
  12714. case WASM_OP_I32_STORE8:
  12715. case WASM_OP_I32_STORE16:
  12716. case WASM_OP_I64_STORE:
  12717. case WASM_OP_I64_STORE8:
  12718. case WASM_OP_I64_STORE16:
  12719. case WASM_OP_I64_STORE32:
  12720. case WASM_OP_F32_STORE:
  12721. case WASM_OP_F64_STORE:
  12722. {
  12723. #if WASM_ENABLE_FAST_INTERP != 0
  12724. /* change F32/F64 into I32/I64 */
  12725. if (opcode == WASM_OP_F32_LOAD) {
  12726. skip_label();
  12727. emit_label(WASM_OP_I32_LOAD);
  12728. }
  12729. else if (opcode == WASM_OP_F64_LOAD) {
  12730. skip_label();
  12731. emit_label(WASM_OP_I64_LOAD);
  12732. }
  12733. else if (opcode == WASM_OP_F32_STORE) {
  12734. skip_label();
  12735. emit_label(WASM_OP_I32_STORE);
  12736. }
  12737. else if (opcode == WASM_OP_F64_STORE) {
  12738. skip_label();
  12739. emit_label(WASM_OP_I64_STORE);
  12740. }
  12741. #endif
  12742. CHECK_MEMORY();
  12743. pb_read_leb_memarg(p, p_end, align); /* align */
  12744. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  12745. if (!check_memory_access_align(opcode, align, error_buf,
  12746. error_buf_size)) {
  12747. goto fail;
  12748. }
  12749. #if WASM_ENABLE_FAST_INTERP != 0
  12750. emit_uint32(loader_ctx, mem_offset);
  12751. #endif
  12752. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12753. func->has_memory_operations = true;
  12754. #endif
  12755. switch (opcode) {
  12756. /* load */
  12757. case WASM_OP_I32_LOAD:
  12758. case WASM_OP_I32_LOAD8_S:
  12759. case WASM_OP_I32_LOAD8_U:
  12760. case WASM_OP_I32_LOAD16_S:
  12761. case WASM_OP_I32_LOAD16_U:
  12762. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  12763. break;
  12764. case WASM_OP_I64_LOAD:
  12765. case WASM_OP_I64_LOAD8_S:
  12766. case WASM_OP_I64_LOAD8_U:
  12767. case WASM_OP_I64_LOAD16_S:
  12768. case WASM_OP_I64_LOAD16_U:
  12769. case WASM_OP_I64_LOAD32_S:
  12770. case WASM_OP_I64_LOAD32_U:
  12771. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  12772. break;
  12773. case WASM_OP_F32_LOAD:
  12774. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  12775. break;
  12776. case WASM_OP_F64_LOAD:
  12777. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  12778. break;
  12779. /* store */
  12780. case WASM_OP_I32_STORE:
  12781. case WASM_OP_I32_STORE8:
  12782. case WASM_OP_I32_STORE16:
  12783. POP_I32();
  12784. POP_MEM_OFFSET();
  12785. break;
  12786. case WASM_OP_I64_STORE:
  12787. case WASM_OP_I64_STORE8:
  12788. case WASM_OP_I64_STORE16:
  12789. case WASM_OP_I64_STORE32:
  12790. POP_I64();
  12791. POP_MEM_OFFSET();
  12792. break;
  12793. case WASM_OP_F32_STORE:
  12794. POP_F32();
  12795. POP_MEM_OFFSET();
  12796. break;
  12797. case WASM_OP_F64_STORE:
  12798. POP_F64();
  12799. POP_MEM_OFFSET();
  12800. break;
  12801. default:
  12802. break;
  12803. }
  12804. break;
  12805. }
  12806. case WASM_OP_MEMORY_SIZE:
  12807. CHECK_MEMORY();
  12808. pb_read_leb_uint32(p, p_end, memidx);
  12809. check_memidx(module, memidx);
  12810. PUSH_PAGE_COUNT();
  12811. module->possible_memory_grow = true;
  12812. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12813. func->has_memory_operations = true;
  12814. #endif
  12815. break;
  12816. case WASM_OP_MEMORY_GROW:
  12817. CHECK_MEMORY();
  12818. pb_read_leb_uint32(p, p_end, memidx);
  12819. check_memidx(module, memidx);
  12820. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  12821. module->possible_memory_grow = true;
  12822. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  12823. || WASM_ENABLE_WAMR_COMPILER != 0
  12824. func->has_op_memory_grow = true;
  12825. #endif
  12826. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12827. func->has_memory_operations = true;
  12828. #endif
  12829. break;
  12830. case WASM_OP_I32_CONST:
  12831. pb_read_leb_int32(p, p_end, i32_const);
  12832. #if WASM_ENABLE_FAST_INTERP != 0
  12833. skip_label();
  12834. disable_emit = true;
  12835. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  12836. if (operand_offset == 0) {
  12837. disable_emit = false;
  12838. emit_label(WASM_OP_I32_CONST);
  12839. emit_uint32(loader_ctx, i32_const);
  12840. }
  12841. #else
  12842. (void)i32_const;
  12843. #endif
  12844. PUSH_I32();
  12845. break;
  12846. case WASM_OP_I64_CONST:
  12847. pb_read_leb_int64(p, p_end, i64_const);
  12848. #if WASM_ENABLE_FAST_INTERP != 0
  12849. skip_label();
  12850. disable_emit = true;
  12851. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  12852. if (operand_offset == 0) {
  12853. disable_emit = false;
  12854. emit_label(WASM_OP_I64_CONST);
  12855. emit_uint64(loader_ctx, i64_const);
  12856. }
  12857. #endif
  12858. PUSH_I64();
  12859. break;
  12860. case WASM_OP_F32_CONST:
  12861. CHECK_BUF(p, p_end, sizeof(float32));
  12862. p += sizeof(float32);
  12863. #if WASM_ENABLE_FAST_INTERP != 0
  12864. skip_label();
  12865. disable_emit = true;
  12866. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12867. sizeof(float32));
  12868. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12869. if (operand_offset == 0) {
  12870. disable_emit = false;
  12871. emit_label(WASM_OP_F32_CONST);
  12872. emit_float32(loader_ctx, f32_const);
  12873. }
  12874. #endif
  12875. PUSH_F32();
  12876. break;
  12877. case WASM_OP_F64_CONST:
  12878. CHECK_BUF(p, p_end, sizeof(float64));
  12879. p += sizeof(float64);
  12880. #if WASM_ENABLE_FAST_INTERP != 0
  12881. skip_label();
  12882. disable_emit = true;
  12883. /* Some MCU may require 8-byte align */
  12884. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12885. sizeof(float64));
  12886. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12887. if (operand_offset == 0) {
  12888. disable_emit = false;
  12889. emit_label(WASM_OP_F64_CONST);
  12890. emit_float64(loader_ctx, f64_const);
  12891. }
  12892. #endif
  12893. PUSH_F64();
  12894. break;
  12895. case WASM_OP_I32_EQZ:
  12896. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12897. break;
  12898. case WASM_OP_I32_EQ:
  12899. case WASM_OP_I32_NE:
  12900. case WASM_OP_I32_LT_S:
  12901. case WASM_OP_I32_LT_U:
  12902. case WASM_OP_I32_GT_S:
  12903. case WASM_OP_I32_GT_U:
  12904. case WASM_OP_I32_LE_S:
  12905. case WASM_OP_I32_LE_U:
  12906. case WASM_OP_I32_GE_S:
  12907. case WASM_OP_I32_GE_U:
  12908. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12909. break;
  12910. case WASM_OP_I64_EQZ:
  12911. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12912. break;
  12913. case WASM_OP_I64_EQ:
  12914. case WASM_OP_I64_NE:
  12915. case WASM_OP_I64_LT_S:
  12916. case WASM_OP_I64_LT_U:
  12917. case WASM_OP_I64_GT_S:
  12918. case WASM_OP_I64_GT_U:
  12919. case WASM_OP_I64_LE_S:
  12920. case WASM_OP_I64_LE_U:
  12921. case WASM_OP_I64_GE_S:
  12922. case WASM_OP_I64_GE_U:
  12923. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12924. break;
  12925. case WASM_OP_F32_EQ:
  12926. case WASM_OP_F32_NE:
  12927. case WASM_OP_F32_LT:
  12928. case WASM_OP_F32_GT:
  12929. case WASM_OP_F32_LE:
  12930. case WASM_OP_F32_GE:
  12931. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12932. break;
  12933. case WASM_OP_F64_EQ:
  12934. case WASM_OP_F64_NE:
  12935. case WASM_OP_F64_LT:
  12936. case WASM_OP_F64_GT:
  12937. case WASM_OP_F64_LE:
  12938. case WASM_OP_F64_GE:
  12939. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12940. break;
  12941. case WASM_OP_I32_CLZ:
  12942. case WASM_OP_I32_CTZ:
  12943. case WASM_OP_I32_POPCNT:
  12944. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12945. break;
  12946. case WASM_OP_I32_ADD:
  12947. case WASM_OP_I32_SUB:
  12948. case WASM_OP_I32_MUL:
  12949. case WASM_OP_I32_DIV_S:
  12950. case WASM_OP_I32_DIV_U:
  12951. case WASM_OP_I32_REM_S:
  12952. case WASM_OP_I32_REM_U:
  12953. case WASM_OP_I32_AND:
  12954. case WASM_OP_I32_OR:
  12955. case WASM_OP_I32_XOR:
  12956. case WASM_OP_I32_SHL:
  12957. case WASM_OP_I32_SHR_S:
  12958. case WASM_OP_I32_SHR_U:
  12959. case WASM_OP_I32_ROTL:
  12960. case WASM_OP_I32_ROTR:
  12961. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12962. break;
  12963. case WASM_OP_I64_CLZ:
  12964. case WASM_OP_I64_CTZ:
  12965. case WASM_OP_I64_POPCNT:
  12966. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12967. break;
  12968. case WASM_OP_I64_ADD:
  12969. case WASM_OP_I64_SUB:
  12970. case WASM_OP_I64_MUL:
  12971. case WASM_OP_I64_DIV_S:
  12972. case WASM_OP_I64_DIV_U:
  12973. case WASM_OP_I64_REM_S:
  12974. case WASM_OP_I64_REM_U:
  12975. case WASM_OP_I64_AND:
  12976. case WASM_OP_I64_OR:
  12977. case WASM_OP_I64_XOR:
  12978. case WASM_OP_I64_SHL:
  12979. case WASM_OP_I64_SHR_S:
  12980. case WASM_OP_I64_SHR_U:
  12981. case WASM_OP_I64_ROTL:
  12982. case WASM_OP_I64_ROTR:
  12983. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12984. break;
  12985. case WASM_OP_F32_ABS:
  12986. case WASM_OP_F32_NEG:
  12987. case WASM_OP_F32_CEIL:
  12988. case WASM_OP_F32_FLOOR:
  12989. case WASM_OP_F32_TRUNC:
  12990. case WASM_OP_F32_NEAREST:
  12991. case WASM_OP_F32_SQRT:
  12992. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12993. break;
  12994. case WASM_OP_F32_ADD:
  12995. case WASM_OP_F32_SUB:
  12996. case WASM_OP_F32_MUL:
  12997. case WASM_OP_F32_DIV:
  12998. case WASM_OP_F32_MIN:
  12999. case WASM_OP_F32_MAX:
  13000. case WASM_OP_F32_COPYSIGN:
  13001. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  13002. break;
  13003. case WASM_OP_F64_ABS:
  13004. case WASM_OP_F64_NEG:
  13005. case WASM_OP_F64_CEIL:
  13006. case WASM_OP_F64_FLOOR:
  13007. case WASM_OP_F64_TRUNC:
  13008. case WASM_OP_F64_NEAREST:
  13009. case WASM_OP_F64_SQRT:
  13010. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  13011. break;
  13012. case WASM_OP_F64_ADD:
  13013. case WASM_OP_F64_SUB:
  13014. case WASM_OP_F64_MUL:
  13015. case WASM_OP_F64_DIV:
  13016. case WASM_OP_F64_MIN:
  13017. case WASM_OP_F64_MAX:
  13018. case WASM_OP_F64_COPYSIGN:
  13019. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  13020. break;
  13021. case WASM_OP_I32_WRAP_I64:
  13022. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  13023. break;
  13024. case WASM_OP_I32_TRUNC_S_F32:
  13025. case WASM_OP_I32_TRUNC_U_F32:
  13026. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13027. break;
  13028. case WASM_OP_I32_TRUNC_S_F64:
  13029. case WASM_OP_I32_TRUNC_U_F64:
  13030. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13031. break;
  13032. case WASM_OP_I64_EXTEND_S_I32:
  13033. case WASM_OP_I64_EXTEND_U_I32:
  13034. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  13035. break;
  13036. case WASM_OP_I64_TRUNC_S_F32:
  13037. case WASM_OP_I64_TRUNC_U_F32:
  13038. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13039. break;
  13040. case WASM_OP_I64_TRUNC_S_F64:
  13041. case WASM_OP_I64_TRUNC_U_F64:
  13042. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13043. break;
  13044. case WASM_OP_F32_CONVERT_S_I32:
  13045. case WASM_OP_F32_CONVERT_U_I32:
  13046. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  13047. break;
  13048. case WASM_OP_F32_CONVERT_S_I64:
  13049. case WASM_OP_F32_CONVERT_U_I64:
  13050. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  13051. break;
  13052. case WASM_OP_F32_DEMOTE_F64:
  13053. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  13054. break;
  13055. case WASM_OP_F64_CONVERT_S_I32:
  13056. case WASM_OP_F64_CONVERT_U_I32:
  13057. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  13058. break;
  13059. case WASM_OP_F64_CONVERT_S_I64:
  13060. case WASM_OP_F64_CONVERT_U_I64:
  13061. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  13062. break;
  13063. case WASM_OP_F64_PROMOTE_F32:
  13064. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  13065. break;
  13066. case WASM_OP_I32_REINTERPRET_F32:
  13067. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13068. break;
  13069. case WASM_OP_I64_REINTERPRET_F64:
  13070. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13071. break;
  13072. case WASM_OP_F32_REINTERPRET_I32:
  13073. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  13074. break;
  13075. case WASM_OP_F64_REINTERPRET_I64:
  13076. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  13077. break;
  13078. case WASM_OP_I32_EXTEND8_S:
  13079. case WASM_OP_I32_EXTEND16_S:
  13080. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13081. break;
  13082. case WASM_OP_I64_EXTEND8_S:
  13083. case WASM_OP_I64_EXTEND16_S:
  13084. case WASM_OP_I64_EXTEND32_S:
  13085. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  13086. break;
  13087. #if WASM_ENABLE_GC != 0
  13088. case WASM_OP_GC_PREFIX:
  13089. {
  13090. uint32 opcode1;
  13091. pb_read_leb_uint32(p, p_end, opcode1);
  13092. #if WASM_ENABLE_FAST_INTERP != 0
  13093. emit_byte(loader_ctx, ((uint8)opcode1));
  13094. #endif
  13095. switch (opcode1) {
  13096. case WASM_OP_STRUCT_NEW:
  13097. case WASM_OP_STRUCT_NEW_DEFAULT:
  13098. {
  13099. pb_read_leb_uint32(p, p_end, type_idx);
  13100. #if WASM_ENABLE_FAST_INTERP != 0
  13101. emit_uint32(loader_ctx, type_idx);
  13102. #endif
  13103. if (!check_type_index(module, module->type_count,
  13104. type_idx, error_buf,
  13105. error_buf_size)) {
  13106. goto fail;
  13107. }
  13108. if (module->types[type_idx] == NULL
  13109. || module->types[type_idx]->type_flag
  13110. != WASM_TYPE_STRUCT) {
  13111. set_error_buf(error_buf, error_buf_size,
  13112. "unknown struct type");
  13113. goto fail;
  13114. }
  13115. if (opcode1 == WASM_OP_STRUCT_NEW) {
  13116. int32 j, k;
  13117. uint8 value_type;
  13118. uint32 ref_type_struct_size;
  13119. WASMStructType *struct_type =
  13120. (WASMStructType *)module->types[type_idx];
  13121. k = struct_type->ref_type_map_count - 1;
  13122. for (j = struct_type->field_count - 1; j >= 0;
  13123. j--) {
  13124. value_type = struct_type->fields[j].field_type;
  13125. if (wasm_is_type_reftype(value_type)) {
  13126. if (wasm_is_type_multi_byte_type(
  13127. value_type)) {
  13128. ref_type_struct_size =
  13129. wasm_reftype_struct_size(
  13130. struct_type->ref_type_maps[k]
  13131. .ref_type);
  13132. bh_memcpy_s(
  13133. &wasm_ref_type,
  13134. (uint32)sizeof(WASMRefType),
  13135. struct_type->ref_type_maps[k]
  13136. .ref_type,
  13137. ref_type_struct_size);
  13138. k--;
  13139. }
  13140. POP_REF(value_type);
  13141. }
  13142. else {
  13143. switch (value_type) {
  13144. case VALUE_TYPE_I32:
  13145. case PACKED_TYPE_I8:
  13146. case PACKED_TYPE_I16:
  13147. POP_I32();
  13148. break;
  13149. case VALUE_TYPE_I64:
  13150. POP_I64();
  13151. break;
  13152. case VALUE_TYPE_F32:
  13153. POP_F32();
  13154. break;
  13155. case VALUE_TYPE_F64:
  13156. POP_F64();
  13157. break;
  13158. default:
  13159. set_error_buf(error_buf,
  13160. error_buf_size,
  13161. "unknown type");
  13162. goto fail;
  13163. }
  13164. }
  13165. }
  13166. }
  13167. /* PUSH struct obj, (ref $t) */
  13168. wasm_set_refheaptype_typeidx(
  13169. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  13170. PUSH_REF(wasm_ref_type.ref_type);
  13171. break;
  13172. }
  13173. case WASM_OP_STRUCT_GET:
  13174. case WASM_OP_STRUCT_GET_S:
  13175. case WASM_OP_STRUCT_GET_U:
  13176. case WASM_OP_STRUCT_SET:
  13177. {
  13178. WASMStructType *struct_type;
  13179. WASMRefType *ref_type = NULL;
  13180. uint32 field_idx;
  13181. uint8 field_type;
  13182. pb_read_leb_uint32(p, p_end, type_idx);
  13183. #if WASM_ENABLE_FAST_INTERP != 0
  13184. emit_uint32(loader_ctx, type_idx);
  13185. #endif
  13186. if (!check_type_index(module, module->type_count,
  13187. type_idx, error_buf,
  13188. error_buf_size)) {
  13189. goto fail;
  13190. }
  13191. if (module->types[type_idx] == NULL
  13192. || module->types[type_idx]->type_flag
  13193. != WASM_TYPE_STRUCT) {
  13194. set_error_buf(error_buf, error_buf_size,
  13195. "unknown struct type");
  13196. goto fail;
  13197. }
  13198. struct_type = (WASMStructType *)module->types[type_idx];
  13199. pb_read_leb_uint32(p, p_end, field_idx);
  13200. #if WASM_ENABLE_FAST_INTERP != 0
  13201. emit_uint32(loader_ctx, field_idx);
  13202. #endif
  13203. if (field_idx >= struct_type->field_count) {
  13204. set_error_buf(error_buf, error_buf_size,
  13205. "unknown struct field");
  13206. goto fail;
  13207. }
  13208. if (opcode1 == WASM_OP_STRUCT_SET
  13209. && !(struct_type->fields[field_idx].field_flags
  13210. & 1)) {
  13211. set_error_buf(error_buf, error_buf_size,
  13212. "field is immutable");
  13213. goto fail;
  13214. }
  13215. field_type = struct_type->fields[field_idx].field_type;
  13216. if (is_packed_type(field_type)) {
  13217. if (opcode1 == WASM_OP_STRUCT_GET) {
  13218. set_error_buf(error_buf, error_buf_size,
  13219. "type mismatch");
  13220. goto fail;
  13221. }
  13222. else {
  13223. field_type = VALUE_TYPE_I32;
  13224. }
  13225. }
  13226. if (wasm_is_type_multi_byte_type(field_type)) {
  13227. ref_type = wasm_reftype_map_find(
  13228. struct_type->ref_type_maps,
  13229. struct_type->ref_type_map_count, field_idx);
  13230. bh_assert(ref_type);
  13231. }
  13232. if (opcode1 == WASM_OP_STRUCT_SET) {
  13233. /* POP field */
  13234. if (wasm_is_type_multi_byte_type(field_type)) {
  13235. bh_memcpy_s(&wasm_ref_type,
  13236. (uint32)sizeof(WASMRefType),
  13237. ref_type,
  13238. wasm_reftype_struct_size(ref_type));
  13239. }
  13240. POP_REF(field_type);
  13241. /* POP struct obj, (ref null $t) */
  13242. wasm_set_refheaptype_typeidx(
  13243. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13244. POP_REF(wasm_ref_type.ref_type);
  13245. }
  13246. else {
  13247. /* POP struct obj, (ref null $t) */
  13248. wasm_set_refheaptype_typeidx(
  13249. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13250. POP_REF(wasm_ref_type.ref_type);
  13251. /* PUSH field */
  13252. if (wasm_is_type_multi_byte_type(field_type)) {
  13253. bh_memcpy_s(&wasm_ref_type,
  13254. (uint32)sizeof(WASMRefType),
  13255. ref_type,
  13256. wasm_reftype_struct_size(ref_type));
  13257. }
  13258. PUSH_REF(field_type);
  13259. }
  13260. break;
  13261. }
  13262. case WASM_OP_ARRAY_NEW:
  13263. case WASM_OP_ARRAY_NEW_DEFAULT:
  13264. case WASM_OP_ARRAY_NEW_FIXED:
  13265. case WASM_OP_ARRAY_NEW_DATA:
  13266. case WASM_OP_ARRAY_NEW_ELEM:
  13267. {
  13268. WASMArrayType *array_type;
  13269. uint8 elem_type;
  13270. uint32 u32 = 0;
  13271. pb_read_leb_uint32(p, p_end, type_idx);
  13272. #if WASM_ENABLE_FAST_INTERP != 0
  13273. emit_uint32(loader_ctx, type_idx);
  13274. #endif
  13275. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  13276. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  13277. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  13278. pb_read_leb_uint32(p, p_end, u32);
  13279. #if WASM_ENABLE_FAST_INTERP != 0
  13280. emit_uint32(loader_ctx, u32);
  13281. #endif
  13282. }
  13283. if (!check_array_type(module, type_idx, error_buf,
  13284. error_buf_size)) {
  13285. goto fail;
  13286. }
  13287. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  13288. /* length */
  13289. POP_I32();
  13290. }
  13291. array_type = (WASMArrayType *)module->types[type_idx];
  13292. elem_type = array_type->elem_type;
  13293. if (opcode1 == WASM_OP_ARRAY_NEW
  13294. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  13295. if (wasm_is_type_multi_byte_type(elem_type)) {
  13296. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13297. array_type->elem_ref_type,
  13298. wasm_reftype_struct_size(
  13299. array_type->elem_ref_type));
  13300. }
  13301. if (is_packed_type(elem_type)) {
  13302. elem_type = VALUE_TYPE_I32;
  13303. }
  13304. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  13305. uint32 N = u32;
  13306. for (i = 0; i < N; i++) {
  13307. if (wasm_is_type_multi_byte_type(
  13308. elem_type)) {
  13309. bh_memcpy_s(
  13310. &wasm_ref_type, sizeof(WASMRefType),
  13311. array_type->elem_ref_type,
  13312. wasm_reftype_struct_size(
  13313. array_type->elem_ref_type));
  13314. }
  13315. POP_REF(elem_type);
  13316. }
  13317. }
  13318. else
  13319. POP_REF(elem_type);
  13320. }
  13321. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  13322. /* offset of data segment */
  13323. POP_I32();
  13324. if (u32 >= module->data_seg_count) {
  13325. set_error_buf(error_buf, error_buf_size,
  13326. "unknown data segment");
  13327. goto fail;
  13328. }
  13329. if (wasm_is_type_reftype(elem_type)) {
  13330. set_error_buf(error_buf, error_buf_size,
  13331. "array elem type mismatch");
  13332. goto fail;
  13333. }
  13334. }
  13335. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  13336. WASMTableSeg *table_seg =
  13337. module->table_segments + u32;
  13338. /* offset of element segment */
  13339. POP_I32();
  13340. if (u32 >= module->table_seg_count) {
  13341. set_error_buf(error_buf, error_buf_size,
  13342. "unknown element segment");
  13343. goto fail;
  13344. }
  13345. if (!wasm_reftype_is_subtype_of(
  13346. table_seg->elem_type,
  13347. table_seg->elem_ref_type, elem_type,
  13348. array_type->elem_ref_type, module->types,
  13349. module->type_count)) {
  13350. set_error_buf(error_buf, error_buf_size,
  13351. "array elem type mismatch");
  13352. goto fail;
  13353. }
  13354. }
  13355. /* PUSH array obj, (ref $t) */
  13356. wasm_set_refheaptype_typeidx(
  13357. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  13358. PUSH_REF(wasm_ref_type.ref_type);
  13359. break;
  13360. }
  13361. case WASM_OP_ARRAY_GET:
  13362. case WASM_OP_ARRAY_GET_S:
  13363. case WASM_OP_ARRAY_GET_U:
  13364. case WASM_OP_ARRAY_SET:
  13365. {
  13366. uint8 elem_type;
  13367. WASMArrayType *array_type;
  13368. WASMRefType *ref_type = NULL;
  13369. pb_read_leb_uint32(p, p_end, type_idx);
  13370. #if WASM_ENABLE_FAST_INTERP != 0
  13371. emit_uint32(loader_ctx, type_idx);
  13372. #endif
  13373. if (!check_array_type(module, type_idx, error_buf,
  13374. error_buf_size)) {
  13375. goto fail;
  13376. }
  13377. array_type = (WASMArrayType *)module->types[type_idx];
  13378. if (opcode1 == WASM_OP_ARRAY_SET
  13379. && !(array_type->elem_flags & 1)) {
  13380. set_error_buf(error_buf, error_buf_size,
  13381. "array is immutable");
  13382. goto fail;
  13383. }
  13384. elem_type = array_type->elem_type;
  13385. if (is_packed_type(elem_type)) {
  13386. if (opcode1 != WASM_OP_ARRAY_GET_S
  13387. && opcode1 != WASM_OP_ARRAY_GET_U
  13388. && opcode1 != WASM_OP_ARRAY_SET) {
  13389. set_error_buf(error_buf, error_buf_size,
  13390. "type mismatch");
  13391. goto fail;
  13392. }
  13393. else {
  13394. elem_type = VALUE_TYPE_I32;
  13395. }
  13396. }
  13397. ref_type = array_type->elem_ref_type;
  13398. if (opcode1 == WASM_OP_ARRAY_SET) {
  13399. /* POP elem to set */
  13400. if (wasm_is_type_multi_byte_type(elem_type)) {
  13401. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13402. ref_type,
  13403. wasm_reftype_struct_size(ref_type));
  13404. }
  13405. POP_REF(elem_type);
  13406. }
  13407. /* elem idx */
  13408. POP_I32();
  13409. /* POP array obj, (ref null $t) */
  13410. wasm_set_refheaptype_typeidx(
  13411. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13412. POP_REF(wasm_ref_type.ref_type);
  13413. if (opcode1 != WASM_OP_ARRAY_SET) {
  13414. /* PUSH elem */
  13415. if (wasm_is_type_multi_byte_type(elem_type)) {
  13416. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13417. ref_type,
  13418. wasm_reftype_struct_size(ref_type));
  13419. }
  13420. PUSH_REF(elem_type);
  13421. }
  13422. break;
  13423. }
  13424. case WASM_OP_ARRAY_LEN:
  13425. {
  13426. POP_REF(REF_TYPE_ARRAYREF);
  13427. /* length */
  13428. PUSH_I32();
  13429. break;
  13430. }
  13431. case WASM_OP_ARRAY_FILL:
  13432. {
  13433. WASMArrayType *array_type;
  13434. uint8 elem_type;
  13435. /* typeidx */
  13436. pb_read_leb_uint32(p, p_end, type_idx);
  13437. #if WASM_ENABLE_FAST_INTERP != 0
  13438. emit_uint32(loader_ctx, type_idx);
  13439. #endif
  13440. if (!check_array_type(module, type_idx, error_buf,
  13441. error_buf_size)) {
  13442. goto fail;
  13443. }
  13444. array_type = (WASMArrayType *)module->types[type_idx];
  13445. if (!(array_type->elem_flags & 1)) {
  13446. set_error_buf(error_buf, error_buf_size,
  13447. "array is immutable");
  13448. goto fail;
  13449. }
  13450. elem_type = array_type->elem_type;
  13451. if (is_packed_type(elem_type)) {
  13452. elem_type = VALUE_TYPE_I32;
  13453. }
  13454. POP_I32(); /* length */
  13455. #if WASM_ENABLE_FAST_INTERP != 0
  13456. POP_OFFSET_TYPE(elem_type);
  13457. #endif
  13458. POP_TYPE(elem_type);
  13459. POP_I32(); /* start */
  13460. /* POP array obj, (ref null $t) */
  13461. wasm_set_refheaptype_typeidx(
  13462. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13463. POP_REF(wasm_ref_type.ref_type);
  13464. break;
  13465. }
  13466. case WASM_OP_ARRAY_COPY:
  13467. {
  13468. uint32 src_type_idx;
  13469. uint8 src_elem_type, dst_elem_type;
  13470. WASMRefType src_ref_type = { 0 },
  13471. *src_elem_ref_type = NULL;
  13472. WASMRefType dst_ref_type = { 0 },
  13473. *dst_elem_ref_type = NULL;
  13474. WASMArrayType *array_type;
  13475. /* typeidx1 */
  13476. pb_read_leb_uint32(p, p_end, type_idx);
  13477. #if WASM_ENABLE_FAST_INTERP != 0
  13478. emit_uint32(loader_ctx, type_idx);
  13479. #endif
  13480. /* typeidx2 */
  13481. pb_read_leb_uint32(p, p_end, src_type_idx);
  13482. #if WASM_ENABLE_FAST_INTERP != 0
  13483. emit_uint32(loader_ctx, src_type_idx);
  13484. #endif
  13485. if (!check_array_type(module, type_idx, error_buf,
  13486. error_buf_size)) {
  13487. goto fail;
  13488. }
  13489. if (!check_array_type(module, src_type_idx, error_buf,
  13490. error_buf_size)) {
  13491. goto fail;
  13492. }
  13493. POP_I32();
  13494. POP_I32();
  13495. /* POP array obj, (ref null $t) */
  13496. wasm_set_refheaptype_typeidx(
  13497. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  13498. POP_REF(wasm_ref_type.ref_type);
  13499. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  13500. &wasm_ref_type,
  13501. wasm_reftype_struct_size(&wasm_ref_type));
  13502. POP_I32();
  13503. /* POP array obj, (ref null $t) */
  13504. wasm_set_refheaptype_typeidx(
  13505. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13506. POP_REF(wasm_ref_type.ref_type);
  13507. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  13508. &wasm_ref_type,
  13509. wasm_reftype_struct_size(&wasm_ref_type));
  13510. array_type = (WASMArrayType *)module->types[type_idx];
  13511. if (!(array_type->elem_flags & 1)) {
  13512. set_error_buf(error_buf, error_buf_size,
  13513. "destination array is immutable");
  13514. goto fail;
  13515. }
  13516. dst_elem_type = array_type->elem_type;
  13517. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  13518. dst_elem_ref_type = array_type->elem_ref_type;
  13519. }
  13520. array_type =
  13521. (WASMArrayType *)module->types[src_type_idx];
  13522. src_elem_type = array_type->elem_type;
  13523. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  13524. src_elem_ref_type = array_type->elem_ref_type;
  13525. }
  13526. if (!wasm_reftype_is_subtype_of(
  13527. src_elem_type, src_elem_ref_type, dst_elem_type,
  13528. dst_elem_ref_type, module->types,
  13529. module->type_count)) {
  13530. set_error_buf(error_buf, error_buf_size,
  13531. "array types do not match");
  13532. goto fail;
  13533. }
  13534. break;
  13535. }
  13536. case WASM_OP_REF_I31:
  13537. {
  13538. POP_I32();
  13539. wasm_set_refheaptype_common(
  13540. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  13541. PUSH_REF(wasm_ref_type.ref_type);
  13542. break;
  13543. }
  13544. case WASM_OP_I31_GET_S:
  13545. case WASM_OP_I31_GET_U:
  13546. {
  13547. POP_REF(REF_TYPE_I31REF);
  13548. PUSH_I32();
  13549. break;
  13550. }
  13551. case WASM_OP_REF_TEST:
  13552. case WASM_OP_REF_CAST:
  13553. case WASM_OP_REF_TEST_NULLABLE:
  13554. case WASM_OP_REF_CAST_NULLABLE:
  13555. {
  13556. uint8 type;
  13557. pb_read_leb_int32(p, p_end, heap_type);
  13558. #if WASM_ENABLE_FAST_INTERP != 0
  13559. emit_uint32(loader_ctx, (uint32)heap_type);
  13560. #endif
  13561. if (heap_type >= 0) {
  13562. if (!check_type_index(module, module->type_count,
  13563. heap_type, error_buf,
  13564. error_buf_size)) {
  13565. goto fail;
  13566. }
  13567. }
  13568. else {
  13569. if (!wasm_is_valid_heap_type(heap_type)) {
  13570. set_error_buf(error_buf, error_buf_size,
  13571. "unknown type");
  13572. goto fail;
  13573. }
  13574. }
  13575. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13576. &wasm_ref_type, error_buf,
  13577. error_buf_size)) {
  13578. goto fail;
  13579. }
  13580. if (opcode1 == WASM_OP_REF_TEST
  13581. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  13582. PUSH_I32();
  13583. else {
  13584. bool nullable =
  13585. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  13586. : false;
  13587. if (heap_type >= 0 || !nullable) {
  13588. wasm_set_refheaptype_typeidx(
  13589. &wasm_ref_type.ref_ht_typeidx, nullable,
  13590. heap_type);
  13591. PUSH_REF(wasm_ref_type.ref_type);
  13592. }
  13593. else {
  13594. PUSH_REF((uint8)((int32)0x80 + heap_type));
  13595. }
  13596. }
  13597. break;
  13598. }
  13599. case WASM_OP_BR_ON_CAST:
  13600. case WASM_OP_BR_ON_CAST_FAIL:
  13601. {
  13602. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  13603. ref_type2 = { 0 }, ref_type_diff = { 0 };
  13604. uint8 type_tmp, castflags;
  13605. uint32 depth;
  13606. int32 heap_type_dst;
  13607. bool src_nullable, dst_nullable;
  13608. CHECK_BUF(p, p_end, 1);
  13609. castflags = read_uint8(p);
  13610. #if WASM_ENABLE_FAST_INTERP != 0
  13611. /* Emit heap_type firstly */
  13612. emit_byte(loader_ctx, castflags);
  13613. #endif
  13614. p_org = p;
  13615. pb_read_leb_uint32(p, p_end, depth);
  13616. pb_read_leb_int32(p, p_end, heap_type);
  13617. #if WASM_ENABLE_FAST_INTERP != 0
  13618. /* Emit heap_type firstly */
  13619. emit_uint32(loader_ctx, (uint32)heap_type);
  13620. #endif
  13621. pb_read_leb_int32(p, p_end, heap_type_dst);
  13622. #if WASM_ENABLE_FAST_INTERP != 0
  13623. /* Emit heap_type firstly */
  13624. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  13625. #endif
  13626. (void)depth;
  13627. /*
  13628. * castflags should be 0~3:
  13629. * 0: (non-null, non-null)
  13630. * 1: (null, non-null)
  13631. * 2: (non-null, null)
  13632. * 3: (null, null)
  13633. */
  13634. if (castflags > 3) {
  13635. set_error_buf(error_buf, error_buf_size,
  13636. "invalid castflags");
  13637. break;
  13638. }
  13639. src_nullable =
  13640. (castflags == 1) || (castflags == 3) ? true : false;
  13641. dst_nullable =
  13642. (castflags == 2) || (castflags == 3) ? true : false;
  13643. /* Pop and backup the stack top's ref type */
  13644. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  13645. &ref_type_tmp, error_buf,
  13646. error_buf_size)) {
  13647. goto fail;
  13648. }
  13649. /* The reference type rt1 must be valid */
  13650. if (!init_ref_type(module, &ref_type1, src_nullable,
  13651. heap_type, error_buf,
  13652. error_buf_size)) {
  13653. goto fail;
  13654. }
  13655. /* The reference type rt2 must be valid. */
  13656. if (!init_ref_type(module, &ref_type2, dst_nullable,
  13657. heap_type_dst, error_buf,
  13658. error_buf_size)) {
  13659. goto fail;
  13660. }
  13661. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  13662. &ref_type2);
  13663. /* The reference type rt2 must match rt1. */
  13664. if (!wasm_reftype_is_subtype_of(
  13665. ref_type2.ref_type, &ref_type2,
  13666. ref_type1.ref_type, &ref_type1, module->types,
  13667. module->type_count)) {
  13668. set_error_buf(error_buf, error_buf_size,
  13669. "type mismatch");
  13670. goto fail;
  13671. }
  13672. p = p_org;
  13673. /* Push ref type casted for branch block check */
  13674. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13675. /* The reference type rt2 must match rt′. */
  13676. type_tmp = ref_type2.ref_type;
  13677. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13678. bh_memcpy_s(
  13679. &wasm_ref_type,
  13680. wasm_reftype_struct_size(&ref_type2),
  13681. &ref_type2,
  13682. wasm_reftype_struct_size(&ref_type2));
  13683. }
  13684. }
  13685. else {
  13686. /* The reference type rt′1 must match rt′. */
  13687. type_tmp = ref_type_diff.ref_type;
  13688. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13689. bh_memcpy_s(
  13690. &wasm_ref_type,
  13691. wasm_reftype_struct_size(&ref_type_diff),
  13692. &ref_type_diff,
  13693. wasm_reftype_struct_size(&ref_type_diff));
  13694. }
  13695. }
  13696. PUSH_REF(type_tmp);
  13697. if (!(frame_csp_tmp = check_branch_block(
  13698. loader_ctx, &p, p_end, opcode, error_buf,
  13699. error_buf_size))) {
  13700. goto fail;
  13701. }
  13702. /* Ignore heap_types */
  13703. skip_leb_uint32(p, p_end);
  13704. skip_leb_uint32(p, p_end);
  13705. /* Restore the original stack top's ref type */
  13706. POP_REF(type_tmp);
  13707. #if WASM_ENABLE_FAST_INTERP != 0
  13708. /* Erase the opnd offset emitted by POP_REF() */
  13709. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13710. #endif
  13711. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13712. type_tmp = ref_type_diff.ref_type;
  13713. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13714. bh_memcpy_s(
  13715. &wasm_ref_type,
  13716. wasm_reftype_struct_size(&ref_type_diff),
  13717. &ref_type_diff,
  13718. wasm_reftype_struct_size(&ref_type_diff));
  13719. }
  13720. }
  13721. else {
  13722. type_tmp = ref_type2.ref_type;
  13723. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13724. bh_memcpy_s(
  13725. &wasm_ref_type,
  13726. wasm_reftype_struct_size(&ref_type2),
  13727. &ref_type2,
  13728. wasm_reftype_struct_size(&ref_type2));
  13729. }
  13730. }
  13731. PUSH_REF(type_tmp);
  13732. #if WASM_ENABLE_FAST_INTERP != 0
  13733. /* Erase the opnd offset emitted by PUSH_REF() */
  13734. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13735. #endif
  13736. break;
  13737. }
  13738. case WASM_OP_ANY_CONVERT_EXTERN:
  13739. {
  13740. uint8 type;
  13741. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13742. &wasm_ref_type, error_buf,
  13743. error_buf_size)) {
  13744. goto fail;
  13745. }
  13746. if (!(type == REF_TYPE_EXTERNREF
  13747. || (type == REF_TYPE_HT_NON_NULLABLE
  13748. && wasm_ref_type.ref_ht_common.heap_type
  13749. == HEAP_TYPE_EXTERN)
  13750. || type == VALUE_TYPE_ANY)) {
  13751. set_error_buf(error_buf, error_buf_size,
  13752. "type mismatch");
  13753. goto fail;
  13754. }
  13755. if (type == REF_TYPE_EXTERNREF)
  13756. type = REF_TYPE_ANYREF;
  13757. else {
  13758. wasm_ref_type.ref_ht_common.heap_type =
  13759. HEAP_TYPE_ANY;
  13760. }
  13761. PUSH_REF(type);
  13762. break;
  13763. }
  13764. case WASM_OP_EXTERN_CONVERT_ANY:
  13765. {
  13766. uint8 type;
  13767. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13768. &wasm_ref_type, error_buf,
  13769. error_buf_size)) {
  13770. goto fail;
  13771. }
  13772. if (type == REF_TYPE_EXTERNREF
  13773. || ((type == REF_TYPE_HT_NULLABLE
  13774. || type == REF_TYPE_HT_NON_NULLABLE)
  13775. && wasm_ref_type.ref_ht_common.heap_type
  13776. == HEAP_TYPE_EXTERN)) {
  13777. set_error_buf(error_buf, error_buf_size,
  13778. "type mismatch");
  13779. goto fail;
  13780. }
  13781. if (type != REF_TYPE_HT_NON_NULLABLE) {
  13782. /* push (ref null extern) */
  13783. type = REF_TYPE_EXTERNREF;
  13784. }
  13785. else {
  13786. /* push (ref extern) */
  13787. type = REF_TYPE_HT_NON_NULLABLE;
  13788. wasm_set_refheaptype_common(
  13789. &wasm_ref_type.ref_ht_common, false,
  13790. HEAP_TYPE_EXTERN);
  13791. }
  13792. PUSH_REF(type);
  13793. break;
  13794. }
  13795. #if WASM_ENABLE_STRINGREF != 0
  13796. case WASM_OP_STRING_NEW_UTF8:
  13797. case WASM_OP_STRING_NEW_WTF16:
  13798. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  13799. case WASM_OP_STRING_NEW_WTF8:
  13800. {
  13801. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13802. func->has_memory_operations = true;
  13803. #endif
  13804. pb_read_leb_uint32(p, p_end, memidx);
  13805. #if WASM_ENABLE_FAST_INTERP != 0
  13806. emit_uint32(loader_ctx, (uint32)memidx);
  13807. #endif
  13808. POP_I32();
  13809. POP_I32();
  13810. PUSH_REF(REF_TYPE_STRINGREF);
  13811. break;
  13812. }
  13813. case WASM_OP_STRING_CONST:
  13814. {
  13815. uint32 contents;
  13816. pb_read_leb_uint32(p, p_end, contents);
  13817. #if WASM_ENABLE_FAST_INTERP != 0
  13818. emit_uint32(loader_ctx, (uint32)contents);
  13819. #endif
  13820. PUSH_REF(REF_TYPE_STRINGREF);
  13821. (void)contents;
  13822. break;
  13823. }
  13824. case WASM_OP_STRING_MEASURE_UTF8:
  13825. case WASM_OP_STRING_MEASURE_WTF8:
  13826. case WASM_OP_STRING_MEASURE_WTF16:
  13827. {
  13828. POP_STRINGREF();
  13829. PUSH_I32();
  13830. break;
  13831. }
  13832. case WASM_OP_STRING_ENCODE_UTF8:
  13833. case WASM_OP_STRING_ENCODE_WTF16:
  13834. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  13835. case WASM_OP_STRING_ENCODE_WTF8:
  13836. {
  13837. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13838. func->has_memory_operations = true;
  13839. #endif
  13840. pb_read_leb_uint32(p, p_end, memidx);
  13841. #if WASM_ENABLE_FAST_INTERP != 0
  13842. emit_uint32(loader_ctx, (uint32)memidx);
  13843. #endif
  13844. POP_I32();
  13845. POP_STRINGREF();
  13846. PUSH_I32();
  13847. break;
  13848. }
  13849. case WASM_OP_STRING_CONCAT:
  13850. {
  13851. POP_STRINGREF();
  13852. POP_STRINGREF();
  13853. PUSH_REF(REF_TYPE_STRINGREF);
  13854. break;
  13855. }
  13856. case WASM_OP_STRING_EQ:
  13857. {
  13858. POP_STRINGREF();
  13859. POP_STRINGREF();
  13860. PUSH_I32();
  13861. break;
  13862. }
  13863. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13864. {
  13865. POP_STRINGREF();
  13866. PUSH_I32();
  13867. break;
  13868. }
  13869. case WASM_OP_STRING_AS_WTF8:
  13870. {
  13871. POP_STRINGREF();
  13872. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13873. break;
  13874. }
  13875. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13876. {
  13877. POP_I32();
  13878. POP_I32();
  13879. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13880. PUSH_I32();
  13881. break;
  13882. }
  13883. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13884. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13885. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13886. {
  13887. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13888. func->has_memory_operations = true;
  13889. #endif
  13890. pb_read_leb_uint32(p, p_end, memidx);
  13891. #if WASM_ENABLE_FAST_INTERP != 0
  13892. emit_uint32(loader_ctx, (uint32)memidx);
  13893. #endif
  13894. POP_I32();
  13895. POP_I32();
  13896. POP_I32();
  13897. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13898. PUSH_I32();
  13899. PUSH_I32();
  13900. break;
  13901. }
  13902. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13903. {
  13904. POP_I32();
  13905. POP_I32();
  13906. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13907. PUSH_REF(REF_TYPE_STRINGREF);
  13908. break;
  13909. }
  13910. case WASM_OP_STRING_AS_WTF16:
  13911. {
  13912. POP_STRINGREF();
  13913. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13914. break;
  13915. }
  13916. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13917. {
  13918. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13919. PUSH_I32();
  13920. break;
  13921. }
  13922. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13923. {
  13924. POP_I32();
  13925. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13926. PUSH_I32();
  13927. break;
  13928. }
  13929. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13930. {
  13931. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13932. func->has_memory_operations = true;
  13933. #endif
  13934. pb_read_leb_uint32(p, p_end, memidx);
  13935. #if WASM_ENABLE_FAST_INTERP != 0
  13936. emit_uint32(loader_ctx, (uint32)memidx);
  13937. #endif
  13938. POP_I32();
  13939. POP_I32();
  13940. POP_I32();
  13941. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13942. PUSH_I32();
  13943. break;
  13944. }
  13945. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13946. {
  13947. POP_I32();
  13948. POP_I32();
  13949. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13950. PUSH_REF(REF_TYPE_STRINGREF);
  13951. break;
  13952. }
  13953. case WASM_OP_STRING_AS_ITER:
  13954. {
  13955. POP_STRINGREF();
  13956. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13957. break;
  13958. }
  13959. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13960. {
  13961. POP_REF(REF_TYPE_STRINGVIEWITER);
  13962. PUSH_I32();
  13963. break;
  13964. }
  13965. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13966. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13967. {
  13968. POP_I32();
  13969. POP_REF(REF_TYPE_STRINGVIEWITER);
  13970. PUSH_I32();
  13971. break;
  13972. }
  13973. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13974. {
  13975. POP_I32();
  13976. POP_REF(REF_TYPE_STRINGVIEWITER);
  13977. PUSH_REF(REF_TYPE_STRINGREF);
  13978. break;
  13979. }
  13980. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13981. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13982. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13983. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13984. {
  13985. POP_I32();
  13986. POP_I32();
  13987. POP_REF(REF_TYPE_ARRAYREF);
  13988. PUSH_REF(REF_TYPE_STRINGREF);
  13989. break;
  13990. }
  13991. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13992. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13993. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13994. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13995. {
  13996. POP_I32();
  13997. POP_REF(REF_TYPE_ARRAYREF);
  13998. POP_STRINGREF();
  13999. PUSH_I32();
  14000. break;
  14001. }
  14002. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  14003. default:
  14004. set_error_buf_v(error_buf, error_buf_size,
  14005. "%s %02x %02x", "unsupported opcode",
  14006. 0xfb, opcode1);
  14007. goto fail;
  14008. }
  14009. break;
  14010. }
  14011. #endif /* end of WASM_ENABLE_GC != 0 */
  14012. case WASM_OP_MISC_PREFIX:
  14013. {
  14014. uint32 opcode1;
  14015. pb_read_leb_uint32(p, p_end, opcode1);
  14016. #if WASM_ENABLE_FAST_INTERP != 0
  14017. emit_byte(loader_ctx, ((uint8)opcode1));
  14018. #endif
  14019. switch (opcode1) {
  14020. case WASM_OP_I32_TRUNC_SAT_S_F32:
  14021. case WASM_OP_I32_TRUNC_SAT_U_F32:
  14022. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  14023. break;
  14024. case WASM_OP_I32_TRUNC_SAT_S_F64:
  14025. case WASM_OP_I32_TRUNC_SAT_U_F64:
  14026. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  14027. break;
  14028. case WASM_OP_I64_TRUNC_SAT_S_F32:
  14029. case WASM_OP_I64_TRUNC_SAT_U_F32:
  14030. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  14031. break;
  14032. case WASM_OP_I64_TRUNC_SAT_S_F64:
  14033. case WASM_OP_I64_TRUNC_SAT_U_F64:
  14034. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  14035. break;
  14036. #if WASM_ENABLE_BULK_MEMORY != 0
  14037. case WASM_OP_MEMORY_INIT:
  14038. {
  14039. pb_read_leb_uint32(p, p_end, data_seg_idx);
  14040. #if WASM_ENABLE_FAST_INTERP != 0
  14041. emit_uint32(loader_ctx, data_seg_idx);
  14042. #endif
  14043. if (module->import_memory_count == 0
  14044. && module->memory_count == 0)
  14045. goto fail_unknown_memory;
  14046. pb_read_leb_uint32(p, p_end, memidx);
  14047. check_memidx(module, memidx);
  14048. if (data_seg_idx >= module->data_seg_count) {
  14049. set_error_buf_v(error_buf, error_buf_size,
  14050. "unknown data segment %d",
  14051. data_seg_idx);
  14052. goto fail;
  14053. }
  14054. if (module->data_seg_count1 == 0)
  14055. goto fail_data_cnt_sec_require;
  14056. POP_I32();
  14057. POP_I32();
  14058. POP_MEM_OFFSET();
  14059. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14060. func->has_memory_operations = true;
  14061. #endif
  14062. #if WASM_ENABLE_WAMR_COMPILER != 0
  14063. module->is_bulk_memory_used = true;
  14064. #endif
  14065. break;
  14066. }
  14067. case WASM_OP_DATA_DROP:
  14068. {
  14069. pb_read_leb_uint32(p, p_end, data_seg_idx);
  14070. #if WASM_ENABLE_FAST_INTERP != 0
  14071. emit_uint32(loader_ctx, data_seg_idx);
  14072. #endif
  14073. if (data_seg_idx >= module->data_seg_count) {
  14074. set_error_buf(error_buf, error_buf_size,
  14075. "unknown data segment");
  14076. goto fail;
  14077. }
  14078. if (module->data_seg_count1 == 0)
  14079. goto fail_data_cnt_sec_require;
  14080. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14081. func->has_memory_operations = true;
  14082. #endif
  14083. #if WASM_ENABLE_WAMR_COMPILER != 0
  14084. module->is_bulk_memory_used = true;
  14085. #endif
  14086. break;
  14087. }
  14088. case WASM_OP_MEMORY_COPY:
  14089. {
  14090. CHECK_BUF(p, p_end, sizeof(int16));
  14091. /* check both src and dst memory index */
  14092. pb_read_leb_uint32(p, p_end, memidx);
  14093. check_memidx(module, memidx);
  14094. pb_read_leb_uint32(p, p_end, memidx);
  14095. check_memidx(module, memidx);
  14096. if (module->import_memory_count == 0
  14097. && module->memory_count == 0)
  14098. goto fail_unknown_memory;
  14099. POP_MEM_OFFSET();
  14100. POP_MEM_OFFSET();
  14101. POP_MEM_OFFSET();
  14102. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14103. func->has_memory_operations = true;
  14104. #endif
  14105. #if WASM_ENABLE_WAMR_COMPILER != 0
  14106. module->is_bulk_memory_used = true;
  14107. #endif
  14108. break;
  14109. }
  14110. case WASM_OP_MEMORY_FILL:
  14111. {
  14112. pb_read_leb_uint32(p, p_end, memidx);
  14113. check_memidx(module, memidx);
  14114. if (module->import_memory_count == 0
  14115. && module->memory_count == 0) {
  14116. goto fail_unknown_memory;
  14117. }
  14118. POP_MEM_OFFSET();
  14119. POP_I32();
  14120. POP_MEM_OFFSET();
  14121. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14122. func->has_memory_operations = true;
  14123. #endif
  14124. #if WASM_ENABLE_WAMR_COMPILER != 0
  14125. module->is_bulk_memory_used = true;
  14126. #endif
  14127. break;
  14128. }
  14129. fail_unknown_memory:
  14130. set_error_buf(error_buf, error_buf_size,
  14131. "unknown memory 0");
  14132. goto fail;
  14133. fail_data_cnt_sec_require:
  14134. set_error_buf(error_buf, error_buf_size,
  14135. "data count section required");
  14136. goto fail;
  14137. #endif /* WASM_ENABLE_BULK_MEMORY */
  14138. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  14139. case WASM_OP_TABLE_INIT:
  14140. {
  14141. uint8 seg_type = 0, tbl_type = 0;
  14142. #if WASM_ENABLE_GC != 0
  14143. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  14144. #endif
  14145. pb_read_leb_uint32(p, p_end, table_seg_idx);
  14146. pb_read_leb_uint32(p, p_end, table_idx);
  14147. if (!get_table_elem_type(module, table_idx, &tbl_type,
  14148. #if WASM_ENABLE_GC != 0
  14149. (void **)&tbl_ref_type,
  14150. #else
  14151. NULL,
  14152. #endif
  14153. error_buf, error_buf_size))
  14154. goto fail;
  14155. if (!get_table_seg_elem_type(module, table_seg_idx,
  14156. &seg_type,
  14157. #if WASM_ENABLE_GC != 0
  14158. (void **)&seg_ref_type,
  14159. #else
  14160. NULL,
  14161. #endif
  14162. error_buf, error_buf_size))
  14163. goto fail;
  14164. #if WASM_ENABLE_GC == 0
  14165. if (seg_type != tbl_type) {
  14166. set_error_buf(error_buf, error_buf_size,
  14167. "type mismatch");
  14168. goto fail;
  14169. }
  14170. #else
  14171. if (!wasm_reftype_is_subtype_of(
  14172. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  14173. module->types, module->type_count)) {
  14174. set_error_buf(error_buf, error_buf_size,
  14175. "type mismatch");
  14176. goto fail;
  14177. }
  14178. #endif
  14179. #if WASM_ENABLE_FAST_INTERP != 0
  14180. emit_uint32(loader_ctx, table_seg_idx);
  14181. emit_uint32(loader_ctx, table_idx);
  14182. #endif
  14183. POP_I32();
  14184. POP_I32();
  14185. #if WASM_ENABLE_MEMORY64 != 0
  14186. table_elem_idx_type = is_table_64bit(module, table_idx)
  14187. ? VALUE_TYPE_I64
  14188. : VALUE_TYPE_I32;
  14189. #endif
  14190. POP_TBL_ELEM_IDX();
  14191. #if WASM_ENABLE_WAMR_COMPILER != 0
  14192. module->is_ref_types_used = true;
  14193. #endif
  14194. break;
  14195. }
  14196. case WASM_OP_ELEM_DROP:
  14197. {
  14198. pb_read_leb_uint32(p, p_end, table_seg_idx);
  14199. if (!get_table_seg_elem_type(module, table_seg_idx,
  14200. NULL, NULL, error_buf,
  14201. error_buf_size))
  14202. goto fail;
  14203. #if WASM_ENABLE_FAST_INTERP != 0
  14204. emit_uint32(loader_ctx, table_seg_idx);
  14205. #endif
  14206. #if WASM_ENABLE_WAMR_COMPILER != 0
  14207. module->is_ref_types_used = true;
  14208. #endif
  14209. break;
  14210. }
  14211. case WASM_OP_TABLE_COPY:
  14212. {
  14213. uint8 src_type, dst_type, src_tbl_idx_type,
  14214. dst_tbl_idx_type, min_tbl_idx_type;
  14215. #if WASM_ENABLE_GC != 0
  14216. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  14217. #endif
  14218. uint32 src_tbl_idx, dst_tbl_idx;
  14219. pb_read_leb_uint32(p, p_end, dst_tbl_idx);
  14220. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  14221. #if WASM_ENABLE_GC != 0
  14222. (void **)&dst_ref_type,
  14223. #else
  14224. NULL,
  14225. #endif
  14226. error_buf, error_buf_size))
  14227. goto fail;
  14228. pb_read_leb_uint32(p, p_end, src_tbl_idx);
  14229. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  14230. #if WASM_ENABLE_GC != 0
  14231. (void **)&src_ref_type,
  14232. #else
  14233. NULL,
  14234. #endif
  14235. error_buf, error_buf_size))
  14236. goto fail;
  14237. #if WASM_ENABLE_GC == 0
  14238. if (src_type != dst_type) {
  14239. set_error_buf(error_buf, error_buf_size,
  14240. "type mismatch");
  14241. goto fail;
  14242. }
  14243. #else
  14244. if (!wasm_reftype_is_subtype_of(
  14245. src_type, src_ref_type, dst_type, dst_ref_type,
  14246. module->types, module->type_count)) {
  14247. set_error_buf(error_buf, error_buf_size,
  14248. "type mismatch");
  14249. goto fail;
  14250. }
  14251. #endif
  14252. #if WASM_ENABLE_FAST_INTERP != 0
  14253. emit_uint32(loader_ctx, dst_tbl_idx);
  14254. emit_uint32(loader_ctx, src_tbl_idx);
  14255. #endif
  14256. #if WASM_ENABLE_MEMORY64 != 0
  14257. src_tbl_idx_type = is_table_64bit(module, src_tbl_idx)
  14258. ? VALUE_TYPE_I64
  14259. : VALUE_TYPE_I32;
  14260. dst_tbl_idx_type = is_table_64bit(module, dst_tbl_idx)
  14261. ? VALUE_TYPE_I64
  14262. : VALUE_TYPE_I32;
  14263. min_tbl_idx_type =
  14264. (src_tbl_idx_type == VALUE_TYPE_I32
  14265. || dst_tbl_idx_type == VALUE_TYPE_I32)
  14266. ? VALUE_TYPE_I32
  14267. : VALUE_TYPE_I64;
  14268. #else
  14269. src_tbl_idx_type = VALUE_TYPE_I32;
  14270. dst_tbl_idx_type = VALUE_TYPE_I32;
  14271. min_tbl_idx_type = VALUE_TYPE_I32;
  14272. #endif
  14273. table_elem_idx_type = min_tbl_idx_type;
  14274. POP_TBL_ELEM_IDX();
  14275. table_elem_idx_type = src_tbl_idx_type;
  14276. POP_TBL_ELEM_IDX();
  14277. table_elem_idx_type = dst_tbl_idx_type;
  14278. POP_TBL_ELEM_IDX();
  14279. #if WASM_ENABLE_WAMR_COMPILER != 0
  14280. module->is_ref_types_used = true;
  14281. #endif
  14282. break;
  14283. }
  14284. case WASM_OP_TABLE_SIZE:
  14285. {
  14286. pb_read_leb_uint32(p, p_end, table_idx);
  14287. /* TODO: shall we create a new function to check
  14288. table idx instead of using below function? */
  14289. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  14290. error_buf, error_buf_size))
  14291. goto fail;
  14292. #if WASM_ENABLE_FAST_INTERP != 0
  14293. emit_uint32(loader_ctx, table_idx);
  14294. #endif
  14295. #if WASM_ENABLE_MEMORY64 != 0
  14296. table_elem_idx_type = is_table_64bit(module, table_idx)
  14297. ? VALUE_TYPE_I64
  14298. : VALUE_TYPE_I32;
  14299. #endif
  14300. PUSH_TBL_ELEM_IDX();
  14301. #if WASM_ENABLE_WAMR_COMPILER != 0
  14302. module->is_ref_types_used = true;
  14303. #endif
  14304. break;
  14305. }
  14306. case WASM_OP_TABLE_GROW:
  14307. case WASM_OP_TABLE_FILL:
  14308. {
  14309. uint8 decl_type;
  14310. #if WASM_ENABLE_GC != 0
  14311. WASMRefType *ref_type = NULL;
  14312. #endif
  14313. pb_read_leb_uint32(p, p_end, table_idx);
  14314. if (!get_table_elem_type(module, table_idx, &decl_type,
  14315. #if WASM_ENABLE_GC != 0
  14316. (void **)&ref_type,
  14317. #else
  14318. NULL,
  14319. #endif
  14320. error_buf, error_buf_size))
  14321. goto fail;
  14322. #if WASM_ENABLE_GC != 0
  14323. if (wasm_is_type_multi_byte_type(decl_type)) {
  14324. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  14325. ref_type,
  14326. wasm_reftype_struct_size(ref_type));
  14327. }
  14328. #endif
  14329. if (opcode1 == WASM_OP_TABLE_GROW) {
  14330. if (table_idx < module->import_table_count) {
  14331. module->import_tables[table_idx]
  14332. .u.table.table_type.possible_grow = true;
  14333. }
  14334. else {
  14335. module
  14336. ->tables[table_idx
  14337. - module->import_table_count]
  14338. .table_type.possible_grow = true;
  14339. }
  14340. }
  14341. #if WASM_ENABLE_FAST_INTERP != 0
  14342. emit_uint32(loader_ctx, table_idx);
  14343. #endif
  14344. #if WASM_ENABLE_MEMORY64 != 0
  14345. table_elem_idx_type = is_table_64bit(module, table_idx)
  14346. ? VALUE_TYPE_I64
  14347. : VALUE_TYPE_I32;
  14348. #endif
  14349. POP_TBL_ELEM_IDX();
  14350. #if WASM_ENABLE_FAST_INTERP != 0
  14351. POP_OFFSET_TYPE(decl_type);
  14352. #endif
  14353. POP_TYPE(decl_type);
  14354. if (opcode1 == WASM_OP_TABLE_GROW)
  14355. PUSH_TBL_ELEM_IDX();
  14356. else
  14357. POP_TBL_ELEM_IDX();
  14358. #if WASM_ENABLE_WAMR_COMPILER != 0
  14359. module->is_ref_types_used = true;
  14360. #endif
  14361. break;
  14362. }
  14363. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  14364. default:
  14365. set_error_buf_v(error_buf, error_buf_size,
  14366. "%s %02x %02x", "unsupported opcode",
  14367. 0xfc, opcode1);
  14368. goto fail;
  14369. }
  14370. break;
  14371. }
  14372. #if WASM_ENABLE_SIMD != 0
  14373. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  14374. || (WASM_ENABLE_FAST_INTERP != 0)
  14375. case WASM_OP_SIMD_PREFIX:
  14376. {
  14377. uint32 opcode1;
  14378. #if WASM_ENABLE_WAMR_COMPILER != 0
  14379. /* Mark the SIMD instruction is used in this module */
  14380. module->is_simd_used = true;
  14381. #endif
  14382. pb_read_leb_uint32(p, p_end, opcode1);
  14383. #if WASM_ENABLE_FAST_INTERP != 0
  14384. emit_byte(loader_ctx, opcode1);
  14385. #endif
  14386. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  14387. */
  14388. switch (opcode1) {
  14389. /* memory instruction */
  14390. case SIMD_v128_load:
  14391. case SIMD_v128_load8x8_s:
  14392. case SIMD_v128_load8x8_u:
  14393. case SIMD_v128_load16x4_s:
  14394. case SIMD_v128_load16x4_u:
  14395. case SIMD_v128_load32x2_s:
  14396. case SIMD_v128_load32x2_u:
  14397. case SIMD_v128_load8_splat:
  14398. case SIMD_v128_load16_splat:
  14399. case SIMD_v128_load32_splat:
  14400. case SIMD_v128_load64_splat:
  14401. {
  14402. CHECK_MEMORY();
  14403. pb_read_leb_uint32(p, p_end, align); /* align */
  14404. if (!check_simd_memory_access_align(
  14405. opcode1, align, error_buf, error_buf_size)) {
  14406. goto fail;
  14407. }
  14408. pb_read_leb_mem_offset(p, p_end,
  14409. mem_offset); /* offset */
  14410. #if WASM_ENABLE_FAST_INTERP != 0
  14411. emit_uint32(loader_ctx, mem_offset);
  14412. #endif
  14413. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14414. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14415. func->has_memory_operations = true;
  14416. #endif
  14417. break;
  14418. }
  14419. case SIMD_v128_store:
  14420. {
  14421. CHECK_MEMORY();
  14422. pb_read_leb_uint32(p, p_end, align); /* align */
  14423. if (!check_simd_memory_access_align(
  14424. opcode1, align, error_buf, error_buf_size)) {
  14425. goto fail;
  14426. }
  14427. pb_read_leb_mem_offset(p, p_end,
  14428. mem_offset); /* offset */
  14429. #if WASM_ENABLE_FAST_INTERP != 0
  14430. emit_uint32(loader_ctx, mem_offset);
  14431. #endif
  14432. POP_V128();
  14433. POP_MEM_OFFSET();
  14434. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14435. func->has_memory_operations = true;
  14436. #endif
  14437. break;
  14438. }
  14439. /* basic operation */
  14440. case SIMD_v128_const:
  14441. {
  14442. #if WASM_ENABLE_FAST_INTERP != 0
  14443. uint64 high, low;
  14444. #endif
  14445. CHECK_BUF1(p, p_end, 16);
  14446. #if WASM_ENABLE_FAST_INTERP != 0
  14447. wasm_runtime_read_v128(p, &high, &low);
  14448. emit_uint64(loader_ctx, high);
  14449. emit_uint64(loader_ctx, low);
  14450. #endif
  14451. p += 16;
  14452. PUSH_V128();
  14453. break;
  14454. }
  14455. case SIMD_v8x16_shuffle:
  14456. {
  14457. V128 mask;
  14458. CHECK_BUF1(p, p_end, 16);
  14459. mask = read_i8x16(p, error_buf, error_buf_size);
  14460. if (!check_simd_shuffle_mask(mask, error_buf,
  14461. error_buf_size)) {
  14462. goto fail;
  14463. }
  14464. #if WASM_ENABLE_FAST_INTERP != 0
  14465. uint64 high, low;
  14466. wasm_runtime_read_v128(p, &high, &low);
  14467. emit_uint64(loader_ctx, high);
  14468. emit_uint64(loader_ctx, low);
  14469. #endif
  14470. p += 16;
  14471. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14472. break;
  14473. }
  14474. case SIMD_v8x16_swizzle:
  14475. {
  14476. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14477. break;
  14478. }
  14479. /* splat operation */
  14480. case SIMD_i8x16_splat:
  14481. case SIMD_i16x8_splat:
  14482. case SIMD_i32x4_splat:
  14483. case SIMD_i64x2_splat:
  14484. case SIMD_f32x4_splat:
  14485. case SIMD_f64x2_splat:
  14486. {
  14487. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  14488. VALUE_TYPE_I32, VALUE_TYPE_I64,
  14489. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  14490. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  14491. VALUE_TYPE_V128);
  14492. break;
  14493. }
  14494. /* lane operation */
  14495. case SIMD_i8x16_extract_lane_s:
  14496. case SIMD_i8x16_extract_lane_u:
  14497. case SIMD_i8x16_replace_lane:
  14498. case SIMD_i16x8_extract_lane_s:
  14499. case SIMD_i16x8_extract_lane_u:
  14500. case SIMD_i16x8_replace_lane:
  14501. case SIMD_i32x4_extract_lane:
  14502. case SIMD_i32x4_replace_lane:
  14503. case SIMD_i64x2_extract_lane:
  14504. case SIMD_i64x2_replace_lane:
  14505. case SIMD_f32x4_extract_lane:
  14506. case SIMD_f32x4_replace_lane:
  14507. case SIMD_f64x2_extract_lane:
  14508. case SIMD_f64x2_replace_lane:
  14509. {
  14510. uint8 lane;
  14511. /* clang-format off */
  14512. uint8 replace[] = {
  14513. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  14514. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  14515. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  14516. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  14517. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  14518. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  14519. };
  14520. uint8 push_type[] = {
  14521. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14522. VALUE_TYPE_V128,
  14523. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14524. VALUE_TYPE_V128,
  14525. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  14526. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  14527. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  14528. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  14529. };
  14530. /* clang-format on */
  14531. CHECK_BUF(p, p_end, 1);
  14532. lane = read_uint8(p);
  14533. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14534. error_buf_size)) {
  14535. goto fail;
  14536. }
  14537. #if WASM_ENABLE_FAST_INTERP != 0
  14538. emit_byte(loader_ctx, lane);
  14539. #endif
  14540. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  14541. #if WASM_ENABLE_FAST_INTERP != 0
  14542. if (!(wasm_loader_pop_frame_ref_offset(
  14543. loader_ctx,
  14544. replace[opcode1
  14545. - SIMD_i8x16_extract_lane_s],
  14546. error_buf, error_buf_size)))
  14547. goto fail;
  14548. #else
  14549. if (!(wasm_loader_pop_frame_ref(
  14550. loader_ctx,
  14551. replace[opcode1
  14552. - SIMD_i8x16_extract_lane_s],
  14553. error_buf, error_buf_size)))
  14554. goto fail;
  14555. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  14556. }
  14557. POP_AND_PUSH(
  14558. VALUE_TYPE_V128,
  14559. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  14560. break;
  14561. }
  14562. /* i8x16 compare operation */
  14563. case SIMD_i8x16_eq:
  14564. case SIMD_i8x16_ne:
  14565. case SIMD_i8x16_lt_s:
  14566. case SIMD_i8x16_lt_u:
  14567. case SIMD_i8x16_gt_s:
  14568. case SIMD_i8x16_gt_u:
  14569. case SIMD_i8x16_le_s:
  14570. case SIMD_i8x16_le_u:
  14571. case SIMD_i8x16_ge_s:
  14572. case SIMD_i8x16_ge_u:
  14573. /* i16x8 compare operation */
  14574. case SIMD_i16x8_eq:
  14575. case SIMD_i16x8_ne:
  14576. case SIMD_i16x8_lt_s:
  14577. case SIMD_i16x8_lt_u:
  14578. case SIMD_i16x8_gt_s:
  14579. case SIMD_i16x8_gt_u:
  14580. case SIMD_i16x8_le_s:
  14581. case SIMD_i16x8_le_u:
  14582. case SIMD_i16x8_ge_s:
  14583. case SIMD_i16x8_ge_u:
  14584. /* i32x4 compare operation */
  14585. case SIMD_i32x4_eq:
  14586. case SIMD_i32x4_ne:
  14587. case SIMD_i32x4_lt_s:
  14588. case SIMD_i32x4_lt_u:
  14589. case SIMD_i32x4_gt_s:
  14590. case SIMD_i32x4_gt_u:
  14591. case SIMD_i32x4_le_s:
  14592. case SIMD_i32x4_le_u:
  14593. case SIMD_i32x4_ge_s:
  14594. case SIMD_i32x4_ge_u:
  14595. /* f32x4 compare operation */
  14596. case SIMD_f32x4_eq:
  14597. case SIMD_f32x4_ne:
  14598. case SIMD_f32x4_lt:
  14599. case SIMD_f32x4_gt:
  14600. case SIMD_f32x4_le:
  14601. case SIMD_f32x4_ge:
  14602. /* f64x2 compare operation */
  14603. case SIMD_f64x2_eq:
  14604. case SIMD_f64x2_ne:
  14605. case SIMD_f64x2_lt:
  14606. case SIMD_f64x2_gt:
  14607. case SIMD_f64x2_le:
  14608. case SIMD_f64x2_ge:
  14609. {
  14610. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14611. break;
  14612. }
  14613. /* v128 operation */
  14614. case SIMD_v128_not:
  14615. {
  14616. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14617. break;
  14618. }
  14619. case SIMD_v128_and:
  14620. case SIMD_v128_andnot:
  14621. case SIMD_v128_or:
  14622. case SIMD_v128_xor:
  14623. {
  14624. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14625. break;
  14626. }
  14627. case SIMD_v128_bitselect:
  14628. {
  14629. POP_V128();
  14630. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14631. break;
  14632. }
  14633. case SIMD_v128_any_true:
  14634. {
  14635. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14636. break;
  14637. }
  14638. /* Load Lane Operation */
  14639. case SIMD_v128_load8_lane:
  14640. case SIMD_v128_load16_lane:
  14641. case SIMD_v128_load32_lane:
  14642. case SIMD_v128_load64_lane:
  14643. case SIMD_v128_store8_lane:
  14644. case SIMD_v128_store16_lane:
  14645. case SIMD_v128_store32_lane:
  14646. case SIMD_v128_store64_lane:
  14647. {
  14648. uint8 lane;
  14649. CHECK_MEMORY();
  14650. pb_read_leb_uint32(p, p_end, align); /* align */
  14651. if (!check_simd_memory_access_align(
  14652. opcode1, align, error_buf, error_buf_size)) {
  14653. goto fail;
  14654. }
  14655. pb_read_leb_mem_offset(p, p_end,
  14656. mem_offset); /* offset */
  14657. CHECK_BUF(p, p_end, 1);
  14658. lane = read_uint8(p);
  14659. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14660. error_buf_size)) {
  14661. goto fail;
  14662. }
  14663. #if WASM_ENABLE_FAST_INTERP != 0
  14664. emit_uint32(loader_ctx, mem_offset);
  14665. #endif
  14666. POP_V128();
  14667. POP_MEM_OFFSET();
  14668. #if WASM_ENABLE_FAST_INTERP != 0
  14669. emit_byte(loader_ctx, lane);
  14670. #endif
  14671. if (opcode1 < SIMD_v128_store8_lane) {
  14672. PUSH_V128();
  14673. }
  14674. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14675. func->has_memory_operations = true;
  14676. #endif
  14677. break;
  14678. }
  14679. case SIMD_v128_load32_zero:
  14680. case SIMD_v128_load64_zero:
  14681. {
  14682. CHECK_MEMORY();
  14683. pb_read_leb_uint32(p, p_end, align); /* align */
  14684. if (!check_simd_memory_access_align(
  14685. opcode1, align, error_buf, error_buf_size)) {
  14686. goto fail;
  14687. }
  14688. pb_read_leb_mem_offset(p, p_end,
  14689. mem_offset); /* offset */
  14690. #if WASM_ENABLE_FAST_INTERP != 0
  14691. emit_uint32(loader_ctx, mem_offset);
  14692. #endif
  14693. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14694. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14695. func->has_memory_operations = true;
  14696. #endif
  14697. break;
  14698. }
  14699. /* Float conversion */
  14700. case SIMD_f32x4_demote_f64x2_zero:
  14701. case SIMD_f64x2_promote_low_f32x4_zero:
  14702. {
  14703. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14704. break;
  14705. }
  14706. /* i8x16 Operation */
  14707. case SIMD_i8x16_abs:
  14708. case SIMD_i8x16_neg:
  14709. case SIMD_i8x16_popcnt:
  14710. {
  14711. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14712. break;
  14713. }
  14714. case SIMD_i8x16_all_true:
  14715. case SIMD_i8x16_bitmask:
  14716. {
  14717. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14718. break;
  14719. }
  14720. case SIMD_i8x16_narrow_i16x8_s:
  14721. case SIMD_i8x16_narrow_i16x8_u:
  14722. {
  14723. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14724. break;
  14725. }
  14726. case SIMD_f32x4_ceil:
  14727. case SIMD_f32x4_floor:
  14728. case SIMD_f32x4_trunc:
  14729. case SIMD_f32x4_nearest:
  14730. {
  14731. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14732. break;
  14733. }
  14734. case SIMD_i8x16_shl:
  14735. case SIMD_i8x16_shr_s:
  14736. case SIMD_i8x16_shr_u:
  14737. {
  14738. POP_I32();
  14739. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14740. break;
  14741. }
  14742. case SIMD_i8x16_add:
  14743. case SIMD_i8x16_add_sat_s:
  14744. case SIMD_i8x16_add_sat_u:
  14745. case SIMD_i8x16_sub:
  14746. case SIMD_i8x16_sub_sat_s:
  14747. case SIMD_i8x16_sub_sat_u:
  14748. {
  14749. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14750. break;
  14751. }
  14752. case SIMD_f64x2_ceil:
  14753. case SIMD_f64x2_floor:
  14754. {
  14755. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14756. break;
  14757. }
  14758. case SIMD_i8x16_min_s:
  14759. case SIMD_i8x16_min_u:
  14760. case SIMD_i8x16_max_s:
  14761. case SIMD_i8x16_max_u:
  14762. {
  14763. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14764. break;
  14765. }
  14766. case SIMD_f64x2_trunc:
  14767. {
  14768. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14769. break;
  14770. }
  14771. case SIMD_i8x16_avgr_u:
  14772. {
  14773. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14774. break;
  14775. }
  14776. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  14777. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  14778. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  14779. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  14780. /* i16x8 operation */
  14781. case SIMD_i16x8_abs:
  14782. case SIMD_i16x8_neg:
  14783. {
  14784. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14785. break;
  14786. }
  14787. case SIMD_i16x8_q15mulr_sat_s:
  14788. {
  14789. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14790. break;
  14791. }
  14792. case SIMD_i16x8_all_true:
  14793. case SIMD_i16x8_bitmask:
  14794. {
  14795. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14796. break;
  14797. }
  14798. case SIMD_i16x8_narrow_i32x4_s:
  14799. case SIMD_i16x8_narrow_i32x4_u:
  14800. {
  14801. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14802. break;
  14803. }
  14804. case SIMD_i16x8_extend_low_i8x16_s:
  14805. case SIMD_i16x8_extend_high_i8x16_s:
  14806. case SIMD_i16x8_extend_low_i8x16_u:
  14807. case SIMD_i16x8_extend_high_i8x16_u:
  14808. {
  14809. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14810. break;
  14811. }
  14812. case SIMD_i16x8_shl:
  14813. case SIMD_i16x8_shr_s:
  14814. case SIMD_i16x8_shr_u:
  14815. {
  14816. POP_I32();
  14817. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14818. break;
  14819. }
  14820. case SIMD_i16x8_add:
  14821. case SIMD_i16x8_add_sat_s:
  14822. case SIMD_i16x8_add_sat_u:
  14823. case SIMD_i16x8_sub:
  14824. case SIMD_i16x8_sub_sat_s:
  14825. case SIMD_i16x8_sub_sat_u:
  14826. {
  14827. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14828. break;
  14829. }
  14830. case SIMD_f64x2_nearest:
  14831. {
  14832. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14833. break;
  14834. }
  14835. case SIMD_i16x8_mul:
  14836. case SIMD_i16x8_min_s:
  14837. case SIMD_i16x8_min_u:
  14838. case SIMD_i16x8_max_s:
  14839. case SIMD_i16x8_max_u:
  14840. case SIMD_i16x8_avgr_u:
  14841. case SIMD_i16x8_extmul_low_i8x16_s:
  14842. case SIMD_i16x8_extmul_high_i8x16_s:
  14843. case SIMD_i16x8_extmul_low_i8x16_u:
  14844. case SIMD_i16x8_extmul_high_i8x16_u:
  14845. {
  14846. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14847. break;
  14848. }
  14849. /* i32x4 operation */
  14850. case SIMD_i32x4_abs:
  14851. case SIMD_i32x4_neg:
  14852. {
  14853. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14854. break;
  14855. }
  14856. case SIMD_i32x4_all_true:
  14857. case SIMD_i32x4_bitmask:
  14858. {
  14859. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14860. break;
  14861. }
  14862. case SIMD_i32x4_extend_low_i16x8_s:
  14863. case SIMD_i32x4_extend_high_i16x8_s:
  14864. case SIMD_i32x4_extend_low_i16x8_u:
  14865. case SIMD_i32x4_extend_high_i16x8_u:
  14866. {
  14867. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14868. break;
  14869. }
  14870. case SIMD_i32x4_shl:
  14871. case SIMD_i32x4_shr_s:
  14872. case SIMD_i32x4_shr_u:
  14873. {
  14874. POP_I32();
  14875. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14876. break;
  14877. }
  14878. case SIMD_i32x4_add:
  14879. case SIMD_i32x4_sub:
  14880. case SIMD_i32x4_mul:
  14881. case SIMD_i32x4_min_s:
  14882. case SIMD_i32x4_min_u:
  14883. case SIMD_i32x4_max_s:
  14884. case SIMD_i32x4_max_u:
  14885. case SIMD_i32x4_dot_i16x8_s:
  14886. case SIMD_i32x4_extmul_low_i16x8_s:
  14887. case SIMD_i32x4_extmul_high_i16x8_s:
  14888. case SIMD_i32x4_extmul_low_i16x8_u:
  14889. case SIMD_i32x4_extmul_high_i16x8_u:
  14890. {
  14891. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14892. break;
  14893. }
  14894. /* i64x2 operation */
  14895. case SIMD_i64x2_abs:
  14896. case SIMD_i64x2_neg:
  14897. {
  14898. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14899. break;
  14900. }
  14901. case SIMD_i64x2_all_true:
  14902. case SIMD_i64x2_bitmask:
  14903. {
  14904. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14905. break;
  14906. }
  14907. case SIMD_i64x2_extend_low_i32x4_s:
  14908. case SIMD_i64x2_extend_high_i32x4_s:
  14909. case SIMD_i64x2_extend_low_i32x4_u:
  14910. case SIMD_i64x2_extend_high_i32x4_u:
  14911. {
  14912. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14913. break;
  14914. }
  14915. case SIMD_i64x2_shl:
  14916. case SIMD_i64x2_shr_s:
  14917. case SIMD_i64x2_shr_u:
  14918. {
  14919. POP_I32();
  14920. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14921. break;
  14922. }
  14923. case SIMD_i64x2_add:
  14924. case SIMD_i64x2_sub:
  14925. case SIMD_i64x2_mul:
  14926. case SIMD_i64x2_eq:
  14927. case SIMD_i64x2_ne:
  14928. case SIMD_i64x2_lt_s:
  14929. case SIMD_i64x2_gt_s:
  14930. case SIMD_i64x2_le_s:
  14931. case SIMD_i64x2_ge_s:
  14932. case SIMD_i64x2_extmul_low_i32x4_s:
  14933. case SIMD_i64x2_extmul_high_i32x4_s:
  14934. case SIMD_i64x2_extmul_low_i32x4_u:
  14935. case SIMD_i64x2_extmul_high_i32x4_u:
  14936. {
  14937. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14938. break;
  14939. }
  14940. /* f32x4 operation */
  14941. case SIMD_f32x4_abs:
  14942. case SIMD_f32x4_neg:
  14943. case SIMD_f32x4_sqrt:
  14944. {
  14945. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14946. break;
  14947. }
  14948. case SIMD_f32x4_add:
  14949. case SIMD_f32x4_sub:
  14950. case SIMD_f32x4_mul:
  14951. case SIMD_f32x4_div:
  14952. case SIMD_f32x4_min:
  14953. case SIMD_f32x4_max:
  14954. case SIMD_f32x4_pmin:
  14955. case SIMD_f32x4_pmax:
  14956. {
  14957. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14958. break;
  14959. }
  14960. /* f64x2 operation */
  14961. case SIMD_f64x2_abs:
  14962. case SIMD_f64x2_neg:
  14963. case SIMD_f64x2_sqrt:
  14964. {
  14965. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14966. break;
  14967. }
  14968. case SIMD_f64x2_add:
  14969. case SIMD_f64x2_sub:
  14970. case SIMD_f64x2_mul:
  14971. case SIMD_f64x2_div:
  14972. case SIMD_f64x2_min:
  14973. case SIMD_f64x2_max:
  14974. case SIMD_f64x2_pmin:
  14975. case SIMD_f64x2_pmax:
  14976. {
  14977. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14978. break;
  14979. }
  14980. case SIMD_i32x4_trunc_sat_f32x4_s:
  14981. case SIMD_i32x4_trunc_sat_f32x4_u:
  14982. case SIMD_f32x4_convert_i32x4_s:
  14983. case SIMD_f32x4_convert_i32x4_u:
  14984. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14985. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14986. case SIMD_f64x2_convert_low_i32x4_s:
  14987. case SIMD_f64x2_convert_low_i32x4_u:
  14988. {
  14989. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14990. break;
  14991. }
  14992. default:
  14993. {
  14994. if (error_buf != NULL) {
  14995. snprintf(error_buf, error_buf_size,
  14996. "WASM module load failed: "
  14997. "invalid opcode 0xfd %02x.",
  14998. opcode1);
  14999. }
  15000. goto fail;
  15001. }
  15002. }
  15003. break;
  15004. }
  15005. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  15006. (WASM_ENABLE_FAST_INTERP != 0) */
  15007. #endif /* end of WASM_ENABLE_SIMD */
  15008. #if WASM_ENABLE_SHARED_MEMORY != 0
  15009. case WASM_OP_ATOMIC_PREFIX:
  15010. {
  15011. uint32 opcode1;
  15012. pb_read_leb_uint32(p, p_end, opcode1);
  15013. #if WASM_ENABLE_FAST_INTERP != 0
  15014. emit_byte(loader_ctx, opcode1);
  15015. #endif
  15016. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  15017. CHECK_MEMORY();
  15018. pb_read_leb_uint32(p, p_end, align); /* align */
  15019. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  15020. if (!check_memory_align_equal(opcode1, align, error_buf,
  15021. error_buf_size)) {
  15022. goto fail;
  15023. }
  15024. #if WASM_ENABLE_FAST_INTERP != 0
  15025. emit_uint32(loader_ctx, mem_offset);
  15026. #endif
  15027. }
  15028. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  15029. func->has_memory_operations = true;
  15030. #endif
  15031. switch (opcode1) {
  15032. case WASM_OP_ATOMIC_NOTIFY:
  15033. POP_I32();
  15034. POP_MEM_OFFSET();
  15035. PUSH_I32();
  15036. break;
  15037. case WASM_OP_ATOMIC_WAIT32:
  15038. POP_I64();
  15039. POP_I32();
  15040. POP_MEM_OFFSET();
  15041. PUSH_I32();
  15042. break;
  15043. case WASM_OP_ATOMIC_WAIT64:
  15044. POP_I64();
  15045. POP_I64();
  15046. POP_MEM_OFFSET();
  15047. PUSH_I32();
  15048. break;
  15049. case WASM_OP_ATOMIC_FENCE:
  15050. /* reserved byte 0x00 */
  15051. if (*p++ != 0x00) {
  15052. set_error_buf(error_buf, error_buf_size,
  15053. "zero byte expected");
  15054. goto fail;
  15055. }
  15056. break;
  15057. case WASM_OP_ATOMIC_I32_LOAD:
  15058. case WASM_OP_ATOMIC_I32_LOAD8_U:
  15059. case WASM_OP_ATOMIC_I32_LOAD16_U:
  15060. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  15061. break;
  15062. case WASM_OP_ATOMIC_I32_STORE:
  15063. case WASM_OP_ATOMIC_I32_STORE8:
  15064. case WASM_OP_ATOMIC_I32_STORE16:
  15065. POP_I32();
  15066. POP_MEM_OFFSET();
  15067. break;
  15068. case WASM_OP_ATOMIC_I64_LOAD:
  15069. case WASM_OP_ATOMIC_I64_LOAD8_U:
  15070. case WASM_OP_ATOMIC_I64_LOAD16_U:
  15071. case WASM_OP_ATOMIC_I64_LOAD32_U:
  15072. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  15073. break;
  15074. case WASM_OP_ATOMIC_I64_STORE:
  15075. case WASM_OP_ATOMIC_I64_STORE8:
  15076. case WASM_OP_ATOMIC_I64_STORE16:
  15077. case WASM_OP_ATOMIC_I64_STORE32:
  15078. POP_I64();
  15079. POP_MEM_OFFSET();
  15080. break;
  15081. case WASM_OP_ATOMIC_RMW_I32_ADD:
  15082. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  15083. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  15084. case WASM_OP_ATOMIC_RMW_I32_SUB:
  15085. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  15086. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  15087. case WASM_OP_ATOMIC_RMW_I32_AND:
  15088. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  15089. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  15090. case WASM_OP_ATOMIC_RMW_I32_OR:
  15091. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  15092. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  15093. case WASM_OP_ATOMIC_RMW_I32_XOR:
  15094. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  15095. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  15096. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  15097. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  15098. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  15099. POP_I32();
  15100. POP_MEM_OFFSET();
  15101. PUSH_I32();
  15102. break;
  15103. case WASM_OP_ATOMIC_RMW_I64_ADD:
  15104. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  15105. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  15106. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  15107. case WASM_OP_ATOMIC_RMW_I64_SUB:
  15108. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  15109. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  15110. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  15111. case WASM_OP_ATOMIC_RMW_I64_AND:
  15112. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  15113. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  15114. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  15115. case WASM_OP_ATOMIC_RMW_I64_OR:
  15116. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  15117. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  15118. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  15119. case WASM_OP_ATOMIC_RMW_I64_XOR:
  15120. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  15121. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  15122. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  15123. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  15124. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  15125. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  15126. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  15127. POP_I64();
  15128. POP_MEM_OFFSET();
  15129. PUSH_I64();
  15130. break;
  15131. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  15132. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  15133. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  15134. POP_I32();
  15135. POP_I32();
  15136. POP_MEM_OFFSET();
  15137. PUSH_I32();
  15138. break;
  15139. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  15140. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  15141. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  15142. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  15143. POP_I64();
  15144. POP_I64();
  15145. POP_MEM_OFFSET();
  15146. PUSH_I64();
  15147. break;
  15148. default:
  15149. set_error_buf_v(error_buf, error_buf_size,
  15150. "%s %02x %02x", "unsupported opcode",
  15151. 0xfe, opcode1);
  15152. goto fail;
  15153. }
  15154. break;
  15155. }
  15156. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  15157. default:
  15158. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  15159. "unsupported opcode", opcode);
  15160. goto fail;
  15161. }
  15162. #if WASM_ENABLE_FAST_INTERP != 0
  15163. last_op = opcode;
  15164. #endif
  15165. }
  15166. if (loader_ctx->csp_num > 0) {
  15167. /* unmatched end opcodes result from unbalanced control flow structures,
  15168. * for example, br_table with inconsistent target count (1 declared, 2
  15169. * given), or simply superfluous end opcodes */
  15170. set_error_buf(
  15171. error_buf, error_buf_size,
  15172. "unexpected end opcodes from unbalanced control flow structures");
  15173. goto fail;
  15174. }
  15175. #if WASM_ENABLE_FAST_INTERP != 0
  15176. if (loader_ctx->p_code_compiled == NULL)
  15177. goto re_scan;
  15178. func->const_cell_num = loader_ctx->i64_const_num * 2
  15179. + loader_ctx->v128_const_num * 4
  15180. + loader_ctx->i32_const_num;
  15181. if (func->const_cell_num > 0) {
  15182. if (!(func->consts =
  15183. loader_malloc((uint64)sizeof(uint32) * func->const_cell_num,
  15184. error_buf, error_buf_size)))
  15185. goto fail;
  15186. if (loader_ctx->i64_const_num > 0) {
  15187. bh_memcpy_s(func->consts,
  15188. (uint32)sizeof(int64) * loader_ctx->i64_const_num,
  15189. loader_ctx->i64_consts,
  15190. (uint32)sizeof(int64) * loader_ctx->i64_const_num);
  15191. }
  15192. if (loader_ctx->i32_const_num > 0) {
  15193. bh_memcpy_s(func->consts
  15194. + sizeof(int64) * loader_ctx->i64_const_num,
  15195. (uint32)sizeof(int32) * loader_ctx->i32_const_num,
  15196. loader_ctx->i32_consts,
  15197. (uint32)sizeof(int32) * loader_ctx->i32_const_num);
  15198. }
  15199. if (loader_ctx->v128_const_num > 0) {
  15200. bh_memcpy_s(func->consts,
  15201. (uint32)sizeof(V128) * loader_ctx->v128_const_num,
  15202. loader_ctx->v128_consts,
  15203. (uint32)sizeof(V128) * loader_ctx->v128_const_num);
  15204. }
  15205. }
  15206. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  15207. - loader_ctx->start_dynamic_offset + 1;
  15208. #else
  15209. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  15210. #endif
  15211. func->max_block_num = loader_ctx->max_csp_num;
  15212. return_value = true;
  15213. fail:
  15214. wasm_loader_ctx_destroy(loader_ctx);
  15215. (void)table_idx;
  15216. (void)table_seg_idx;
  15217. (void)data_seg_idx;
  15218. (void)i64_const;
  15219. (void)local_offset;
  15220. (void)p_org;
  15221. (void)mem_offset;
  15222. (void)align;
  15223. return return_value;
  15224. }