wasm_loader.c 623 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833
  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]->type_flag != WASM_TYPE_ARRAY) {
  364. set_error_buf(error_buf, error_buf_size, "unknown array type");
  365. return false;
  366. }
  367. return true;
  368. }
  369. #endif
  370. /*
  371. * if no GC is enabled, an valid type is always a function type.
  372. * but if GC is enabled, we need to check the type flag
  373. */
  374. static bool
  375. check_function_type(const WASMModule *module, uint32 type_index,
  376. char *error_buf, uint32 error_buf_size)
  377. {
  378. if (!check_type_index(module, module->type_count, type_index, error_buf,
  379. error_buf_size)) {
  380. return false;
  381. }
  382. #if WASM_ENABLE_GC != 0
  383. if (module->types[type_index]->type_flag != WASM_TYPE_FUNC) {
  384. set_error_buf(error_buf, error_buf_size, "unknown function type");
  385. return false;
  386. }
  387. #endif
  388. return true;
  389. }
  390. static bool
  391. check_function_index(const WASMModule *module, uint32 function_index,
  392. char *error_buf, uint32 error_buf_size)
  393. {
  394. if (function_index
  395. >= module->import_function_count + module->function_count) {
  396. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  397. function_index);
  398. return false;
  399. }
  400. return true;
  401. }
  402. typedef struct InitValue {
  403. uint8 type;
  404. uint8 flag;
  405. #if WASM_ENABLE_GC != 0
  406. uint8 gc_opcode;
  407. WASMRefType ref_type;
  408. #endif
  409. WASMValue value;
  410. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  411. InitializerExpression *expr;
  412. #endif
  413. } InitValue;
  414. typedef struct ConstExprContext {
  415. uint32 sp;
  416. uint32 size;
  417. WASMModule *module;
  418. InitValue *stack;
  419. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  420. } ConstExprContext;
  421. static void
  422. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  423. {
  424. ctx->sp = 0;
  425. ctx->module = module;
  426. ctx->stack = ctx->data;
  427. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  428. }
  429. static bool
  430. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  431. #if WASM_ENABLE_GC != 0
  432. WASMRefType *ref_type, uint8 gc_opcode,
  433. #endif
  434. WASMValue *value,
  435. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  436. InitializerExpression *expr,
  437. #endif
  438. char *error_buf, uint32 error_buf_size)
  439. {
  440. InitValue *cur_value;
  441. if (ctx->sp >= ctx->size) {
  442. if (ctx->stack != ctx->data) {
  443. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  444. (ctx->size + 4) * sizeof(InitValue));
  445. }
  446. else {
  447. if (!(ctx->stack =
  448. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  449. error_buf, error_buf_size))) {
  450. goto fail;
  451. }
  452. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  453. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  454. }
  455. ctx->size += 4;
  456. }
  457. cur_value = &ctx->stack[ctx->sp++];
  458. cur_value->type = type;
  459. cur_value->flag = flag;
  460. cur_value->value = *value;
  461. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  462. cur_value->expr = expr;
  463. #endif
  464. #if WASM_ENABLE_GC != 0
  465. cur_value->gc_opcode = gc_opcode;
  466. if (wasm_is_type_multi_byte_type(type)) {
  467. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  468. ref_type, wasm_reftype_struct_size(ref_type));
  469. }
  470. #endif
  471. return true;
  472. fail:
  473. return false;
  474. }
  475. #if WASM_ENABLE_GC != 0
  476. static void
  477. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  478. {
  479. WASMStructNewInitValues *struct_init_values =
  480. (WASMStructNewInitValues *)data;
  481. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  482. WASMType *wasm_type;
  483. uint32 i;
  484. if (!data)
  485. return;
  486. wasm_type = module->types[struct_init_values->type_idx];
  487. /* The data can only be type of `WASMStructNewInitValues *`
  488. or `WASMArrayNewInitValues *` */
  489. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  490. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  491. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  492. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  493. WASMRefType *ref_type;
  494. uint8 field_type;
  495. uint16 ref_type_map_index = 0;
  496. for (i = 0; i < struct_init_values->count; i++) {
  497. field_type = struct_type->fields[i].field_type;
  498. if (wasm_is_type_multi_byte_type(field_type))
  499. ref_type =
  500. struct_type->ref_type_maps[ref_type_map_index++].ref_type;
  501. else
  502. ref_type = NULL;
  503. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  504. REF_TYPE_STRUCTREF, NULL,
  505. module->types, module->type_count)
  506. || wasm_reftype_is_subtype_of(
  507. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  508. module->types, module->type_count)) {
  509. destroy_init_expr_data_recursive(
  510. module, struct_init_values->fields[i].data);
  511. }
  512. }
  513. }
  514. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  515. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  516. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  517. uint8 elem_type = array_type->elem_type;
  518. for (i = 0; i < array_init_values->length; i++) {
  519. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  520. REF_TYPE_STRUCTREF, NULL,
  521. module->types, module->type_count)
  522. || wasm_reftype_is_subtype_of(
  523. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  524. module->types, module->type_count)) {
  525. destroy_init_expr_data_recursive(
  526. module, array_init_values->elem_data[i].data);
  527. }
  528. }
  529. }
  530. wasm_runtime_free(data);
  531. }
  532. #endif
  533. static bool
  534. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  535. #if WASM_ENABLE_GC != 0
  536. WASMRefType *ref_type, uint8 *p_gc_opcode,
  537. #endif
  538. WASMValue *p_value,
  539. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  540. InitializerExpression **p_expr,
  541. #endif
  542. char *error_buf, uint32 error_buf_size)
  543. {
  544. InitValue *cur_value;
  545. if (ctx->sp == 0) {
  546. set_error_buf(error_buf, error_buf_size,
  547. "type mismatch: const expr stack underflow");
  548. return false;
  549. }
  550. cur_value = &ctx->stack[--ctx->sp];
  551. #if WASM_ENABLE_GC == 0
  552. if (cur_value->type != type) {
  553. set_error_buf(error_buf, error_buf_size, "type mismatch");
  554. return false;
  555. }
  556. #else
  557. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  558. ref_type, ctx->module->types,
  559. ctx->module->type_count)) {
  560. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  561. "type mismatch: expect ", type2str(type),
  562. " but got other");
  563. goto fail;
  564. }
  565. #endif
  566. if (p_flag)
  567. *p_flag = cur_value->flag;
  568. if (p_value)
  569. *p_value = cur_value->value;
  570. #if WASM_ENABLE_GC != 0
  571. if (p_gc_opcode)
  572. *p_gc_opcode = cur_value->gc_opcode;
  573. #endif
  574. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  575. if (p_expr)
  576. *p_expr = cur_value->expr;
  577. #endif
  578. return true;
  579. #if WASM_ENABLE_GC != 0
  580. fail:
  581. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  582. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  583. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  584. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  585. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  586. }
  587. return false;
  588. #endif
  589. }
  590. static void
  591. destroy_const_expr_stack(ConstExprContext *ctx, bool free_exprs)
  592. {
  593. #if WASM_ENABLE_GC != 0
  594. uint32 i;
  595. for (i = 0; i < ctx->sp; i++) {
  596. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  597. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  598. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  599. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  600. destroy_init_expr_data_recursive(ctx->module,
  601. ctx->stack[i].value.data);
  602. }
  603. }
  604. #endif
  605. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  606. if (free_exprs) {
  607. for (uint32 j = 0; j < ctx->sp; j++) {
  608. if (is_expr_binary_op(ctx->stack[j].expr->init_expr_type)) {
  609. destroy_init_expr_recursive(ctx->stack[j].expr);
  610. ctx->stack[j].expr = NULL;
  611. }
  612. }
  613. }
  614. #endif
  615. if (ctx->stack != ctx->data) {
  616. wasm_runtime_free(ctx->stack);
  617. }
  618. }
  619. #if WASM_ENABLE_GC != 0 || WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  620. static void
  621. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  622. {
  623. #if WASM_ENABLE_GC != 0
  624. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  625. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  626. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  627. destroy_init_expr_data_recursive(module, expr->u.unary.v.data);
  628. }
  629. #endif
  630. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  631. // free left expr and right exprs for binary oprand
  632. if (!is_expr_binary_op(expr->init_expr_type)) {
  633. return;
  634. }
  635. if (expr->u.binary.l_expr) {
  636. destroy_init_expr_recursive(expr->u.binary.l_expr);
  637. }
  638. if (expr->u.binary.r_expr) {
  639. destroy_init_expr_recursive(expr->u.binary.r_expr);
  640. }
  641. expr->u.binary.l_expr = expr->u.binary.r_expr = NULL;
  642. #endif
  643. }
  644. #endif
  645. /* for init expr
  646. * (data (i32.add (i32.const 0) (i32.sub (i32.const 1) (i32.const 2)))),
  647. * the binary format is
  648. * 0x11: 41 00 ; i32.const 0
  649. * 0x13: 41 01 ; i32.const 1
  650. * 0x15: 41 02 ; i32.const 2
  651. * 0x17: 6b ; i32.sub
  652. * 0x18: 6a ; i32.add
  653. * for traversal: read opcodes and push them onto the stack. When encountering
  654. * a binary opcode, pop two values from the stack which become the left and
  655. * right child nodes of this binary operation node.
  656. */
  657. static bool
  658. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  659. InitializerExpression *init_expr, uint8 type, void *ref_type,
  660. char *error_buf, uint32 error_buf_size)
  661. {
  662. const uint8 *p = *p_buf, *p_end = buf_end;
  663. uint8 flag, *p_float;
  664. uint32 i;
  665. ConstExprContext const_expr_ctx = { 0 };
  666. WASMValue cur_value;
  667. #if WASM_ENABLE_GC != 0
  668. uint32 opcode1, type_idx;
  669. uint8 opcode;
  670. WASMRefType cur_ref_type = { 0 };
  671. #endif
  672. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  673. InitializerExpression *cur_expr = NULL;
  674. #endif
  675. init_const_expr_stack(&const_expr_ctx, module);
  676. CHECK_BUF(p, p_end, 1);
  677. flag = read_uint8(p);
  678. while (flag != WASM_OP_END) {
  679. switch (flag) {
  680. /* i32.const */
  681. case INIT_EXPR_TYPE_I32_CONST:
  682. read_leb_int32(p, p_end, cur_value.i32);
  683. if (!push_const_expr_stack(&const_expr_ctx, flag,
  684. VALUE_TYPE_I32,
  685. #if WASM_ENABLE_GC != 0
  686. NULL, 0,
  687. #endif
  688. &cur_value,
  689. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  690. NULL,
  691. #endif
  692. error_buf, error_buf_size))
  693. goto fail;
  694. break;
  695. /* i64.const */
  696. case INIT_EXPR_TYPE_I64_CONST:
  697. read_leb_int64(p, p_end, cur_value.i64);
  698. if (!push_const_expr_stack(&const_expr_ctx, flag,
  699. VALUE_TYPE_I64,
  700. #if WASM_ENABLE_GC != 0
  701. NULL, 0,
  702. #endif
  703. &cur_value,
  704. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  705. NULL,
  706. #endif
  707. error_buf, error_buf_size))
  708. goto fail;
  709. break;
  710. /* f32.const */
  711. case INIT_EXPR_TYPE_F32_CONST:
  712. CHECK_BUF(p, p_end, 4);
  713. p_float = (uint8 *)&cur_value.f32;
  714. for (i = 0; i < sizeof(float32); i++)
  715. *p_float++ = *p++;
  716. if (!push_const_expr_stack(&const_expr_ctx, flag,
  717. VALUE_TYPE_F32,
  718. #if WASM_ENABLE_GC != 0
  719. NULL, 0,
  720. #endif
  721. &cur_value,
  722. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  723. NULL,
  724. #endif
  725. error_buf, error_buf_size))
  726. goto fail;
  727. break;
  728. /* f64.const */
  729. case INIT_EXPR_TYPE_F64_CONST:
  730. CHECK_BUF(p, p_end, 8);
  731. p_float = (uint8 *)&cur_value.f64;
  732. for (i = 0; i < sizeof(float64); i++)
  733. *p_float++ = *p++;
  734. if (!push_const_expr_stack(&const_expr_ctx, flag,
  735. VALUE_TYPE_F64,
  736. #if WASM_ENABLE_GC != 0
  737. NULL, 0,
  738. #endif
  739. &cur_value,
  740. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  741. NULL,
  742. #endif
  743. error_buf, error_buf_size))
  744. goto fail;
  745. break;
  746. #if WASM_ENABLE_SIMD != 0
  747. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  748. || (WASM_ENABLE_FAST_INTERP != 0)
  749. /* v128.const */
  750. case INIT_EXPR_TYPE_V128_CONST:
  751. {
  752. uint64 high, low;
  753. CHECK_BUF(p, p_end, 1);
  754. (void)read_uint8(p);
  755. CHECK_BUF(p, p_end, 16);
  756. wasm_runtime_read_v128(p, &high, &low);
  757. p += 16;
  758. cur_value.v128.i64x2[0] = high;
  759. cur_value.v128.i64x2[1] = low;
  760. if (!push_const_expr_stack(&const_expr_ctx, flag,
  761. VALUE_TYPE_V128,
  762. #if WASM_ENABLE_GC != 0
  763. NULL, 0,
  764. #endif
  765. &cur_value,
  766. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  767. NULL,
  768. #endif
  769. error_buf, error_buf_size))
  770. goto fail;
  771. #if WASM_ENABLE_WAMR_COMPILER != 0
  772. /* If any init_expr is v128.const, mark SIMD used */
  773. module->is_simd_used = true;
  774. #endif
  775. break;
  776. }
  777. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  778. (WASM_ENABLE_FAST_INTERP != 0) */
  779. #endif /* end of WASM_ENABLE_SIMD */
  780. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  781. case INIT_EXPR_TYPE_I32_ADD:
  782. case INIT_EXPR_TYPE_I32_SUB:
  783. case INIT_EXPR_TYPE_I32_MUL:
  784. case INIT_EXPR_TYPE_I64_ADD:
  785. case INIT_EXPR_TYPE_I64_SUB:
  786. case INIT_EXPR_TYPE_I64_MUL:
  787. {
  788. InitializerExpression *l_expr, *r_expr;
  789. WASMValue l_value, r_value;
  790. uint8 l_flag, r_flag;
  791. uint8 value_type;
  792. if (flag == INIT_EXPR_TYPE_I32_ADD
  793. || flag == INIT_EXPR_TYPE_I32_SUB
  794. || flag == INIT_EXPR_TYPE_I32_MUL) {
  795. value_type = VALUE_TYPE_I32;
  796. }
  797. else {
  798. value_type = VALUE_TYPE_I64;
  799. }
  800. /* If right flag indicates a binary operation, right expr will
  801. * be popped from stack. Otherwise, allocate a new expr for
  802. * right expr. Same for left expr.
  803. */
  804. if (!(pop_const_expr_stack(&const_expr_ctx, &r_flag, value_type,
  805. #if WASM_ENABLE_GC != 0
  806. NULL, NULL,
  807. #endif
  808. &r_value, &r_expr, error_buf,
  809. error_buf_size))) {
  810. goto fail;
  811. }
  812. if (!is_expr_binary_op(r_flag)) {
  813. if (!(r_expr = loader_malloc(sizeof(InitializerExpression),
  814. error_buf, error_buf_size))) {
  815. goto fail;
  816. }
  817. r_expr->init_expr_type = r_flag;
  818. r_expr->u.unary.v = r_value;
  819. }
  820. if (!(pop_const_expr_stack(&const_expr_ctx, &l_flag, value_type,
  821. #if WASM_ENABLE_GC != 0
  822. NULL, NULL,
  823. #endif
  824. &l_value, &l_expr, error_buf,
  825. error_buf_size))) {
  826. destroy_init_expr_recursive(r_expr);
  827. goto fail;
  828. }
  829. if (!is_expr_binary_op(l_flag)) {
  830. if (!(l_expr = loader_malloc(sizeof(InitializerExpression),
  831. error_buf, error_buf_size))) {
  832. destroy_init_expr_recursive(r_expr);
  833. goto fail;
  834. }
  835. l_expr->init_expr_type = l_flag;
  836. l_expr->u.unary.v = l_value;
  837. }
  838. if (!(cur_expr = loader_malloc(sizeof(InitializerExpression),
  839. error_buf, error_buf_size))) {
  840. destroy_init_expr_recursive(l_expr);
  841. destroy_init_expr_recursive(r_expr);
  842. goto fail;
  843. }
  844. cur_expr->init_expr_type = flag;
  845. cur_expr->u.binary.l_expr = l_expr;
  846. cur_expr->u.binary.r_expr = r_expr;
  847. if (!push_const_expr_stack(&const_expr_ctx, flag, value_type,
  848. #if WASM_ENABLE_GC != 0
  849. NULL, 0,
  850. #endif
  851. &cur_value, cur_expr, error_buf,
  852. error_buf_size)) {
  853. destroy_init_expr_recursive(cur_expr);
  854. goto fail;
  855. }
  856. break;
  857. }
  858. #endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR */
  859. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  860. /* ref.func */
  861. case INIT_EXPR_TYPE_FUNCREF_CONST:
  862. {
  863. uint32 func_idx;
  864. read_leb_uint32(p, p_end, func_idx);
  865. cur_value.ref_index = func_idx;
  866. if (!check_function_index(module, func_idx, error_buf,
  867. error_buf_size)) {
  868. goto fail;
  869. }
  870. #if WASM_ENABLE_GC == 0
  871. if (!push_const_expr_stack(&const_expr_ctx, flag,
  872. VALUE_TYPE_FUNCREF, &cur_value,
  873. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  874. NULL,
  875. #endif
  876. error_buf, error_buf_size))
  877. goto fail;
  878. #else
  879. if (func_idx < module->import_function_count) {
  880. type_idx =
  881. module->import_functions[func_idx].u.function.type_idx;
  882. }
  883. else {
  884. type_idx = module
  885. ->functions[func_idx
  886. - module->import_function_count]
  887. ->type_idx;
  888. }
  889. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  890. false, type_idx);
  891. if (!push_const_expr_stack(&const_expr_ctx, flag,
  892. cur_ref_type.ref_type, &cur_ref_type,
  893. 0, &cur_value,
  894. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  895. NULL,
  896. #endif
  897. error_buf, error_buf_size))
  898. goto fail;
  899. #endif
  900. #if WASM_ENABLE_WAMR_COMPILER != 0
  901. module->is_ref_types_used = true;
  902. #endif
  903. break;
  904. }
  905. /* ref.null */
  906. case INIT_EXPR_TYPE_REFNULL_CONST:
  907. {
  908. #if WASM_ENABLE_GC == 0
  909. uint8 type1;
  910. CHECK_BUF(p, p_end, 1);
  911. type1 = read_uint8(p);
  912. cur_value.ref_index = NULL_REF;
  913. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  914. &cur_value,
  915. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  916. NULL,
  917. #endif
  918. error_buf, error_buf_size))
  919. goto fail;
  920. #else
  921. /*
  922. * According to the current GC SPEC rules, the heap_type must be
  923. * validated when ref.null is used. It can be an absheaptype,
  924. * or the type C.types[type_idx] must be defined in the context.
  925. */
  926. int32 heap_type;
  927. read_leb_int32(p, p_end, heap_type);
  928. cur_value.gc_obj = NULL_REF;
  929. /*
  930. * The current check of heap_type can deterministically infer
  931. * the result of the previous condition
  932. * `(!is_byte_a_type(type1) ||
  933. * wasm_is_type_multi_byte_type(type1))`. Therefore, the
  934. * original condition is redundant and has been removed.
  935. *
  936. * This logic is consistent with the implementation of the
  937. * `WASM_OP_REF_NULL` case in the `wasm_loader_prepare_bytecode`
  938. * function.
  939. */
  940. if (heap_type >= 0) {
  941. if (!check_type_index(module, module->type_count, heap_type,
  942. error_buf, error_buf_size)) {
  943. goto fail;
  944. }
  945. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  946. true, heap_type);
  947. if (!push_const_expr_stack(&const_expr_ctx, flag,
  948. cur_ref_type.ref_type,
  949. &cur_ref_type, 0, &cur_value,
  950. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  951. NULL,
  952. #endif
  953. error_buf, error_buf_size))
  954. goto fail;
  955. }
  956. else {
  957. if (!wasm_is_valid_heap_type(heap_type)) {
  958. set_error_buf_v(error_buf, error_buf_size,
  959. "unknown type %d", heap_type);
  960. goto fail;
  961. }
  962. cur_ref_type.ref_ht_common.ref_type =
  963. (uint8)((int32)0x80 + heap_type);
  964. if (!push_const_expr_stack(&const_expr_ctx, flag,
  965. cur_ref_type.ref_type, NULL, 0,
  966. &cur_value,
  967. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  968. NULL,
  969. #endif
  970. error_buf, error_buf_size))
  971. goto fail;
  972. }
  973. #endif
  974. #if WASM_ENABLE_WAMR_COMPILER != 0
  975. module->is_ref_types_used = true;
  976. #endif
  977. break;
  978. }
  979. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  980. /* get_global */
  981. case INIT_EXPR_TYPE_GET_GLOBAL:
  982. {
  983. uint32 global_idx;
  984. uint8 global_type;
  985. read_leb_uint32(p, p_end, cur_value.global_index);
  986. global_idx = cur_value.global_index;
  987. /*
  988. * Currently, constant expressions occurring as initializers
  989. * of globals are further constrained in that contained
  990. * global.get instructions are
  991. * only allowed to refer to imported globals.
  992. *
  993. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  994. */
  995. if (global_idx >= module->import_global_count
  996. /* make spec test happy */
  997. #if WASM_ENABLE_GC != 0
  998. + module->global_count
  999. #endif
  1000. ) {
  1001. set_error_buf_v(error_buf, error_buf_size,
  1002. "unknown global %u", global_idx);
  1003. goto fail;
  1004. }
  1005. if (
  1006. /* make spec test happy */
  1007. #if WASM_ENABLE_GC != 0
  1008. global_idx < module->import_global_count &&
  1009. #endif
  1010. module->import_globals[global_idx]
  1011. .u.global.type.is_mutable) {
  1012. set_error_buf_v(error_buf, error_buf_size,
  1013. "constant expression required");
  1014. goto fail;
  1015. }
  1016. if (global_idx < module->import_global_count) {
  1017. global_type = module->import_globals[global_idx]
  1018. .u.global.type.val_type;
  1019. #if WASM_ENABLE_GC != 0
  1020. if (wasm_is_type_multi_byte_type(global_type)) {
  1021. WASMRefType *global_ref_type =
  1022. module->import_globals[global_idx]
  1023. .u.global.ref_type;
  1024. bh_memcpy_s(&cur_ref_type,
  1025. wasm_reftype_struct_size(global_ref_type),
  1026. global_ref_type,
  1027. wasm_reftype_struct_size(global_ref_type));
  1028. }
  1029. #endif
  1030. }
  1031. else {
  1032. global_type =
  1033. module
  1034. ->globals[global_idx - module->import_global_count]
  1035. .type.val_type;
  1036. #if WASM_ENABLE_GC != 0
  1037. if (wasm_is_type_multi_byte_type(global_type)) {
  1038. WASMRefType *global_ref_type =
  1039. module
  1040. ->globals[global_idx
  1041. - module->import_global_count]
  1042. .ref_type;
  1043. bh_memcpy_s(&cur_ref_type,
  1044. wasm_reftype_struct_size(global_ref_type),
  1045. global_ref_type,
  1046. wasm_reftype_struct_size(global_ref_type));
  1047. }
  1048. #endif
  1049. }
  1050. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  1051. #if WASM_ENABLE_GC != 0
  1052. &cur_ref_type, 0,
  1053. #endif
  1054. &cur_value,
  1055. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1056. NULL,
  1057. #endif
  1058. error_buf, error_buf_size))
  1059. goto fail;
  1060. break;
  1061. }
  1062. #if WASM_ENABLE_GC != 0
  1063. /* struct.new and array.new */
  1064. case WASM_OP_GC_PREFIX:
  1065. {
  1066. read_leb_uint32(p, p_end, opcode1);
  1067. switch (opcode1) {
  1068. case WASM_OP_STRUCT_NEW:
  1069. {
  1070. WASMStructType *struct_type;
  1071. WASMStructNewInitValues *struct_init_values = NULL;
  1072. uint32 field_count;
  1073. read_leb_uint32(p, p_end, type_idx);
  1074. if (!check_type_index(module, module->type_count,
  1075. type_idx, error_buf,
  1076. error_buf_size)) {
  1077. goto fail;
  1078. }
  1079. struct_type = (WASMStructType *)module->types[type_idx];
  1080. if (struct_type->base_type.type_flag
  1081. != WASM_TYPE_STRUCT) {
  1082. set_error_buf(error_buf, error_buf_size,
  1083. "unknown struct type");
  1084. goto fail;
  1085. }
  1086. field_count = struct_type->field_count;
  1087. if (!(struct_init_values = loader_malloc(
  1088. offsetof(WASMStructNewInitValues, fields)
  1089. + (uint64)field_count * sizeof(WASMValue),
  1090. error_buf, error_buf_size))) {
  1091. goto fail;
  1092. }
  1093. struct_init_values->type_idx = type_idx;
  1094. struct_init_values->count = field_count;
  1095. for (i = field_count; i > 0; i--) {
  1096. WASMRefType *field_ref_type = NULL;
  1097. uint32 field_idx = i - 1;
  1098. uint8 field_type =
  1099. struct_type->fields[field_idx].field_type;
  1100. if (wasm_is_type_multi_byte_type(field_type)) {
  1101. field_ref_type = wasm_reftype_map_find(
  1102. struct_type->ref_type_maps,
  1103. struct_type->ref_type_map_count, field_idx);
  1104. }
  1105. if (is_packed_type(field_type)) {
  1106. field_type = VALUE_TYPE_I32;
  1107. }
  1108. if (!pop_const_expr_stack(
  1109. &const_expr_ctx, NULL, field_type,
  1110. field_ref_type, NULL,
  1111. &struct_init_values->fields[field_idx],
  1112. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1113. NULL,
  1114. #endif
  1115. error_buf, error_buf_size)) {
  1116. destroy_init_expr_data_recursive(
  1117. module, struct_init_values);
  1118. goto fail;
  1119. }
  1120. }
  1121. cur_value.data = struct_init_values;
  1122. wasm_set_refheaptype_typeidx(
  1123. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1124. if (!push_const_expr_stack(
  1125. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1126. &cur_ref_type, (uint8)opcode1, &cur_value,
  1127. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1128. NULL,
  1129. #endif
  1130. error_buf, error_buf_size)) {
  1131. destroy_init_expr_data_recursive(
  1132. module, struct_init_values);
  1133. goto fail;
  1134. }
  1135. break;
  1136. }
  1137. case WASM_OP_STRUCT_NEW_DEFAULT:
  1138. {
  1139. read_leb_uint32(p, p_end, cur_value.type_index);
  1140. type_idx = cur_value.type_index;
  1141. if (!check_type_index(module, module->type_count,
  1142. type_idx, error_buf,
  1143. error_buf_size)) {
  1144. goto fail;
  1145. }
  1146. if (module->types[type_idx]->type_flag
  1147. != WASM_TYPE_STRUCT) {
  1148. set_error_buf(error_buf, error_buf_size,
  1149. "unknown struct type");
  1150. goto fail;
  1151. }
  1152. cur_value.type_index = type_idx;
  1153. cur_value.data = NULL;
  1154. wasm_set_refheaptype_typeidx(
  1155. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1156. if (!push_const_expr_stack(
  1157. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1158. &cur_ref_type, (uint8)opcode1, &cur_value,
  1159. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1160. NULL,
  1161. #endif
  1162. error_buf, error_buf_size)) {
  1163. goto fail;
  1164. }
  1165. break;
  1166. }
  1167. case WASM_OP_ARRAY_NEW:
  1168. case WASM_OP_ARRAY_NEW_DEFAULT:
  1169. case WASM_OP_ARRAY_NEW_FIXED:
  1170. {
  1171. WASMArrayNewInitValues *array_init_values = NULL;
  1172. WASMArrayType *array_type = NULL;
  1173. WASMRefType *elem_ref_type = NULL;
  1174. uint64 total_size;
  1175. uint8 elem_type;
  1176. read_leb_uint32(p, p_end, cur_value.type_index);
  1177. type_idx = cur_value.type_index;
  1178. if (!check_type_index(module, module->type_count,
  1179. type_idx, error_buf,
  1180. error_buf_size)) {
  1181. goto fail;
  1182. }
  1183. array_type = (WASMArrayType *)module->types[type_idx];
  1184. if (array_type->base_type.type_flag
  1185. != WASM_TYPE_ARRAY) {
  1186. set_error_buf(error_buf, error_buf_size,
  1187. "unknown array type");
  1188. goto fail;
  1189. }
  1190. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  1191. elem_type = array_type->elem_type;
  1192. if (wasm_is_type_multi_byte_type(elem_type)) {
  1193. elem_ref_type = array_type->elem_ref_type;
  1194. }
  1195. if (is_packed_type(elem_type)) {
  1196. elem_type = VALUE_TYPE_I32;
  1197. }
  1198. if (opcode1 == WASM_OP_ARRAY_NEW) {
  1199. WASMValue len_val = { 0 };
  1200. uint64 size = 0;
  1201. if (!pop_const_expr_stack(
  1202. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  1203. NULL, NULL, &len_val,
  1204. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1205. NULL,
  1206. #endif
  1207. error_buf, error_buf_size)) {
  1208. goto fail;
  1209. }
  1210. size =
  1211. sizeof(WASMArrayNewInitValues)
  1212. + sizeof(WASMValue) * (uint64)len_val.i32;
  1213. if (!(array_init_values = loader_malloc(
  1214. size, error_buf, error_buf_size))) {
  1215. goto fail;
  1216. }
  1217. array_init_values->type_idx = type_idx;
  1218. array_init_values->length = len_val.i32;
  1219. if (!pop_const_expr_stack(
  1220. &const_expr_ctx, NULL, elem_type,
  1221. elem_ref_type, NULL,
  1222. &array_init_values->elem_data[0],
  1223. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1224. NULL,
  1225. #endif
  1226. error_buf, error_buf_size)) {
  1227. destroy_init_expr_data_recursive(
  1228. module, array_init_values);
  1229. goto fail;
  1230. }
  1231. cur_value.data = array_init_values;
  1232. }
  1233. else {
  1234. /* WASM_OP_ARRAY_NEW_FIXED */
  1235. uint32 len;
  1236. read_leb_uint32(p, p_end, len);
  1237. total_size =
  1238. (uint64)offsetof(WASMArrayNewInitValues,
  1239. elem_data)
  1240. + (uint64)sizeof(WASMValue) * len;
  1241. if (!(array_init_values =
  1242. loader_malloc(total_size, error_buf,
  1243. error_buf_size))) {
  1244. goto fail;
  1245. }
  1246. array_init_values->type_idx = type_idx;
  1247. array_init_values->length = len;
  1248. for (i = len; i > 0; i--) {
  1249. if (!pop_const_expr_stack(
  1250. &const_expr_ctx, NULL, elem_type,
  1251. elem_ref_type, NULL,
  1252. &array_init_values
  1253. ->elem_data[i - 1],
  1254. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1255. NULL,
  1256. #endif
  1257. error_buf, error_buf_size)) {
  1258. destroy_init_expr_data_recursive(
  1259. module, array_init_values);
  1260. goto fail;
  1261. }
  1262. }
  1263. cur_value.data = array_init_values;
  1264. }
  1265. }
  1266. else {
  1267. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1268. WASMValue len_val;
  1269. uint32 len;
  1270. /* POP(i32) */
  1271. if (!pop_const_expr_stack(
  1272. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1273. NULL, &len_val,
  1274. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1275. NULL,
  1276. #endif
  1277. error_buf, error_buf_size)) {
  1278. goto fail;
  1279. }
  1280. len = len_val.i32;
  1281. cur_value.array_new_default.type_index = type_idx;
  1282. cur_value.array_new_default.length = len;
  1283. }
  1284. wasm_set_refheaptype_typeidx(
  1285. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1286. if (!push_const_expr_stack(
  1287. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1288. &cur_ref_type, (uint8)opcode1, &cur_value,
  1289. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1290. NULL,
  1291. #endif
  1292. error_buf, error_buf_size)) {
  1293. if (array_init_values) {
  1294. destroy_init_expr_data_recursive(
  1295. module, array_init_values);
  1296. }
  1297. goto fail;
  1298. }
  1299. break;
  1300. }
  1301. case WASM_OP_ANY_CONVERT_EXTERN:
  1302. {
  1303. set_error_buf(error_buf, error_buf_size,
  1304. "unsupported constant expression of "
  1305. "extern.internalize");
  1306. goto fail;
  1307. }
  1308. case WASM_OP_EXTERN_CONVERT_ANY:
  1309. {
  1310. set_error_buf(error_buf, error_buf_size,
  1311. "unsupported constant expression of "
  1312. "extern.externalize");
  1313. goto fail;
  1314. }
  1315. case WASM_OP_REF_I31:
  1316. {
  1317. /* POP(i32) */
  1318. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1319. VALUE_TYPE_I32, NULL, NULL,
  1320. &cur_value,
  1321. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1322. NULL,
  1323. #endif
  1324. error_buf, error_buf_size)) {
  1325. goto fail;
  1326. }
  1327. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1328. false, HEAP_TYPE_I31);
  1329. if (!push_const_expr_stack(
  1330. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1331. &cur_ref_type, (uint8)opcode1, &cur_value,
  1332. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1333. NULL,
  1334. #endif
  1335. error_buf, error_buf_size)) {
  1336. goto fail;
  1337. }
  1338. break;
  1339. }
  1340. default:
  1341. set_error_buf(
  1342. error_buf, error_buf_size,
  1343. "type mismatch or constant expression required");
  1344. goto fail;
  1345. }
  1346. break;
  1347. }
  1348. #endif /* end of WASM_ENABLE_GC != 0 */
  1349. default:
  1350. {
  1351. set_error_buf(error_buf, error_buf_size,
  1352. "illegal opcode "
  1353. "or constant expression required "
  1354. "or type mismatch");
  1355. goto fail;
  1356. }
  1357. }
  1358. CHECK_BUF(p, p_end, 1);
  1359. flag = read_uint8(p);
  1360. }
  1361. /* There should be only one value left on the init value stack */
  1362. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1363. #if WASM_ENABLE_GC != 0
  1364. ref_type, &opcode,
  1365. #endif
  1366. &cur_value,
  1367. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1368. &cur_expr,
  1369. #endif
  1370. error_buf, error_buf_size)) {
  1371. goto fail;
  1372. }
  1373. if (const_expr_ctx.sp != 0) {
  1374. set_error_buf(error_buf, error_buf_size,
  1375. "type mismatch: illegal constant opcode sequence");
  1376. goto fail;
  1377. }
  1378. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1379. if (cur_expr != NULL) {
  1380. bh_memcpy_s(init_expr, sizeof(InitializerExpression), cur_expr,
  1381. sizeof(InitializerExpression));
  1382. wasm_runtime_free(cur_expr);
  1383. }
  1384. else {
  1385. init_expr->init_expr_type = flag;
  1386. init_expr->u.unary.v = cur_value;
  1387. }
  1388. #else
  1389. init_expr->init_expr_type = flag;
  1390. init_expr->u.unary.v = cur_value;
  1391. #endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR != 0 */
  1392. #if WASM_ENABLE_GC != 0
  1393. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1394. switch (opcode) {
  1395. case WASM_OP_STRUCT_NEW:
  1396. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1397. break;
  1398. case WASM_OP_STRUCT_NEW_DEFAULT:
  1399. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1400. break;
  1401. case WASM_OP_ARRAY_NEW:
  1402. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1403. break;
  1404. case WASM_OP_ARRAY_NEW_DEFAULT:
  1405. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1406. break;
  1407. case WASM_OP_ARRAY_NEW_FIXED:
  1408. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1409. break;
  1410. case WASM_OP_REF_I31:
  1411. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1412. break;
  1413. default:
  1414. bh_assert(0);
  1415. break;
  1416. }
  1417. }
  1418. #endif /* end of WASM_ENABLE_GC != 0 */
  1419. *p_buf = p;
  1420. destroy_const_expr_stack(&const_expr_ctx, false);
  1421. return true;
  1422. fail:
  1423. destroy_const_expr_stack(&const_expr_ctx, true);
  1424. return false;
  1425. }
  1426. static bool
  1427. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1428. {
  1429. if (mutable >= 2) {
  1430. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1431. return false;
  1432. }
  1433. return true;
  1434. }
  1435. #if WASM_ENABLE_GC != 0
  1436. static void
  1437. destroy_func_type(WASMFuncType *type)
  1438. {
  1439. /* Destroy the reference type hash set */
  1440. if (type->ref_type_maps)
  1441. wasm_runtime_free(type->ref_type_maps);
  1442. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1443. && WASM_ENABLE_LAZY_JIT != 0
  1444. if (type->call_to_llvm_jit_from_fast_jit)
  1445. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1446. #endif
  1447. /* Free the type */
  1448. wasm_runtime_free(type);
  1449. }
  1450. static void
  1451. destroy_struct_type(WASMStructType *type)
  1452. {
  1453. if (type->ref_type_maps)
  1454. wasm_runtime_free(type->ref_type_maps);
  1455. wasm_runtime_free(type);
  1456. }
  1457. static void
  1458. destroy_array_type(WASMArrayType *type)
  1459. {
  1460. wasm_runtime_free(type);
  1461. }
  1462. static void
  1463. destroy_wasm_type(WASMType *type)
  1464. {
  1465. if (type->ref_count > 1) {
  1466. /* The type is referenced by other types
  1467. of current wasm module */
  1468. type->ref_count--;
  1469. return;
  1470. }
  1471. if (type->type_flag == WASM_TYPE_FUNC)
  1472. destroy_func_type((WASMFuncType *)type);
  1473. else if (type->type_flag == WASM_TYPE_STRUCT)
  1474. destroy_struct_type((WASMStructType *)type);
  1475. else if (type->type_flag == WASM_TYPE_ARRAY)
  1476. destroy_array_type((WASMArrayType *)type);
  1477. else {
  1478. bh_assert(0);
  1479. }
  1480. }
  1481. /* Resolve (ref null ht) or (ref ht) */
  1482. static bool
  1483. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1484. WASMModule *module, uint32 type_count, bool nullable,
  1485. WASMRefType *ref_type, char *error_buf,
  1486. uint32 error_buf_size)
  1487. {
  1488. const uint8 *p = *p_buf, *p_end = buf_end;
  1489. ref_type->ref_type =
  1490. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1491. ref_type->ref_ht_common.nullable = nullable;
  1492. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1493. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1494. /* heap type is (type i), i : typeidx, >= 0 */
  1495. if (!check_type_index(module, type_count,
  1496. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1497. error_buf_size)) {
  1498. return false;
  1499. }
  1500. }
  1501. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1502. /* heap type is func, extern, any, eq, i31 or data */
  1503. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1504. return false;
  1505. }
  1506. *p_buf = p;
  1507. return true;
  1508. fail:
  1509. return false;
  1510. }
  1511. static bool
  1512. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1513. WASMModule *module, uint32 type_count,
  1514. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1515. bool allow_packed_type, char *error_buf,
  1516. uint32 error_buf_size)
  1517. {
  1518. const uint8 *p = *p_buf, *p_end = buf_end;
  1519. uint8 type;
  1520. memset(ref_type, 0, sizeof(WASMRefType));
  1521. CHECK_BUF(p, p_end, 1);
  1522. type = read_uint8(p);
  1523. if (wasm_is_reftype_htref_nullable(type)) {
  1524. /* (ref null ht) */
  1525. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1526. ref_type, error_buf, error_buf_size))
  1527. return false;
  1528. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1529. *p_need_ref_type_map = true;
  1530. else {
  1531. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1532. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1533. multi-byte type to one-byte type to reduce the footprint and
  1534. the complexity of type equal/subtype checking */
  1535. ref_type->ref_type =
  1536. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1537. *p_need_ref_type_map = false;
  1538. }
  1539. }
  1540. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1541. /* (ref ht) */
  1542. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1543. ref_type, error_buf, error_buf_size))
  1544. return false;
  1545. *p_need_ref_type_map = true;
  1546. #if WASM_ENABLE_STRINGREF != 0
  1547. /* covert (ref string) to stringref */
  1548. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1549. ref_type->ref_type =
  1550. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1551. *p_need_ref_type_map = false;
  1552. }
  1553. #endif
  1554. }
  1555. else {
  1556. /* type which can be represented by one byte */
  1557. if (!is_valid_value_type_for_interpreter(type)
  1558. && !(allow_packed_type && is_packed_type(type))) {
  1559. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1560. return false;
  1561. }
  1562. ref_type->ref_type = type;
  1563. *p_need_ref_type_map = false;
  1564. #if WASM_ENABLE_WAMR_COMPILER != 0
  1565. /* If any value's type is v128, mark the module as SIMD used */
  1566. if (type == VALUE_TYPE_V128)
  1567. module->is_simd_used = true;
  1568. #endif
  1569. }
  1570. *p_buf = p;
  1571. return true;
  1572. fail:
  1573. return false;
  1574. }
  1575. static WASMRefType *
  1576. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1577. char *error_buf, uint32 error_buf_size)
  1578. {
  1579. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1580. if (!ret) {
  1581. set_error_buf(error_buf, error_buf_size,
  1582. "insert ref type to hash set failed");
  1583. }
  1584. return ret;
  1585. }
  1586. static bool
  1587. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1588. uint32 type_count, uint32 type_idx, char *error_buf,
  1589. uint32 error_buf_size)
  1590. {
  1591. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1592. uint32 param_count, result_count, i, j = 0;
  1593. uint32 param_cell_num, ret_cell_num;
  1594. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1595. uint64 total_size;
  1596. bool need_ref_type_map;
  1597. WASMRefType ref_type;
  1598. WASMFuncType *type = NULL;
  1599. /* Parse first time to resolve param count, result count and
  1600. ref type map count */
  1601. read_leb_uint32(p, p_end, param_count);
  1602. p_org = p;
  1603. for (i = 0; i < param_count; i++) {
  1604. if (!resolve_value_type(&p, p_end, module, type_count,
  1605. &need_ref_type_map, &ref_type, false, error_buf,
  1606. error_buf_size)) {
  1607. return false;
  1608. }
  1609. if (need_ref_type_map)
  1610. ref_type_map_count++;
  1611. }
  1612. read_leb_uint32(p, p_end, result_count);
  1613. for (i = 0; i < result_count; i++) {
  1614. if (!resolve_value_type(&p, p_end, module, type_count,
  1615. &need_ref_type_map, &ref_type, false, error_buf,
  1616. error_buf_size)) {
  1617. return false;
  1618. }
  1619. if (need_ref_type_map) {
  1620. ref_type_map_count++;
  1621. result_ref_type_map_count++;
  1622. }
  1623. }
  1624. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1625. "ref type map count: %d",
  1626. type_idx, param_count, result_count, ref_type_map_count);
  1627. /* Parse second time to resolve param types, result types and
  1628. ref type map info */
  1629. p = p_org;
  1630. total_size = offsetof(WASMFuncType, types)
  1631. + sizeof(uint8) * (uint64)(param_count + result_count);
  1632. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1633. return false;
  1634. }
  1635. if (ref_type_map_count > 0) {
  1636. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1637. if (!(type->ref_type_maps =
  1638. loader_malloc(total_size, error_buf, error_buf_size))) {
  1639. goto fail;
  1640. }
  1641. }
  1642. type->base_type.type_flag = WASM_TYPE_FUNC;
  1643. type->param_count = param_count;
  1644. type->result_count = result_count;
  1645. type->ref_type_map_count = ref_type_map_count;
  1646. if (ref_type_map_count > 0) {
  1647. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1648. - result_ref_type_map_count;
  1649. }
  1650. for (i = 0; i < param_count; i++) {
  1651. if (!resolve_value_type(&p, p_end, module, type_count,
  1652. &need_ref_type_map, &ref_type, false, error_buf,
  1653. error_buf_size)) {
  1654. goto fail;
  1655. }
  1656. type->types[i] = ref_type.ref_type;
  1657. if (need_ref_type_map) {
  1658. type->ref_type_maps[j].index = i;
  1659. if (!(type->ref_type_maps[j++].ref_type =
  1660. reftype_set_insert(module->ref_type_set, &ref_type,
  1661. error_buf, error_buf_size))) {
  1662. goto fail;
  1663. }
  1664. }
  1665. }
  1666. read_leb_uint32(p, p_end, result_count);
  1667. for (i = 0; i < result_count; i++) {
  1668. if (!resolve_value_type(&p, p_end, module, type_count,
  1669. &need_ref_type_map, &ref_type, false, error_buf,
  1670. error_buf_size)) {
  1671. goto fail;
  1672. }
  1673. type->types[param_count + i] = ref_type.ref_type;
  1674. if (need_ref_type_map) {
  1675. type->ref_type_maps[j].index = param_count + i;
  1676. if (!(type->ref_type_maps[j++].ref_type =
  1677. reftype_set_insert(module->ref_type_set, &ref_type,
  1678. error_buf, error_buf_size))) {
  1679. goto fail;
  1680. }
  1681. }
  1682. }
  1683. bh_assert(j == type->ref_type_map_count);
  1684. #if TRACE_WASM_LOADER != 0
  1685. os_printf("type %d = ", type_idx);
  1686. wasm_dump_func_type(type);
  1687. #endif
  1688. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1689. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1690. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1691. set_error_buf(error_buf, error_buf_size,
  1692. "param count or result count too large");
  1693. goto fail;
  1694. }
  1695. type->param_cell_num = (uint16)param_cell_num;
  1696. type->ret_cell_num = (uint16)ret_cell_num;
  1697. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1698. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1699. #endif
  1700. #if WASM_ENABLE_WAMR_COMPILER != 0
  1701. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1702. if (type->types[i] == VALUE_TYPE_V128)
  1703. module->is_simd_used = true;
  1704. }
  1705. #endif
  1706. *p_buf = p;
  1707. module->types[type_idx] = (WASMType *)type;
  1708. return true;
  1709. fail:
  1710. if (type)
  1711. destroy_func_type(type);
  1712. return false;
  1713. }
  1714. static bool
  1715. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1716. WASMModule *module, uint32 type_count, uint32 type_idx,
  1717. char *error_buf, uint32 error_buf_size)
  1718. {
  1719. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1720. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1721. uint32 i, j = 0, offset;
  1722. uint16 *reference_table;
  1723. uint64 total_size;
  1724. uint8 mutable;
  1725. bool need_ref_type_map;
  1726. WASMRefType ref_type;
  1727. WASMStructType *type = NULL;
  1728. /* Parse first time to resolve field count and ref type map count */
  1729. read_leb_uint32(p, p_end, field_count);
  1730. p_org = p;
  1731. for (i = 0; i < field_count; i++) {
  1732. if (!resolve_value_type(&p, p_end, module, type_count,
  1733. &need_ref_type_map, &ref_type, true, error_buf,
  1734. error_buf_size)) {
  1735. return false;
  1736. }
  1737. if (need_ref_type_map)
  1738. ref_type_map_count++;
  1739. if (wasm_is_type_reftype(ref_type.ref_type))
  1740. ref_field_count++;
  1741. CHECK_BUF(p, p_end, 1);
  1742. mutable = read_uint8(p);
  1743. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1744. return false;
  1745. }
  1746. }
  1747. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1748. type_idx, field_count, ref_type_map_count);
  1749. /* Parse second time to resolve field types and ref type map info */
  1750. p = p_org;
  1751. total_size = offsetof(WASMStructType, fields)
  1752. + sizeof(WASMStructFieldType) * (uint64)field_count
  1753. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1754. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1755. return false;
  1756. }
  1757. if (ref_type_map_count > 0) {
  1758. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1759. if (!(type->ref_type_maps =
  1760. loader_malloc(total_size, error_buf, error_buf_size))) {
  1761. goto fail;
  1762. }
  1763. }
  1764. type->reference_table = reference_table =
  1765. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1766. + sizeof(WASMStructFieldType) * field_count);
  1767. *reference_table++ = ref_field_count;
  1768. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1769. type->field_count = field_count;
  1770. type->ref_type_map_count = ref_type_map_count;
  1771. offset = (uint32)offsetof(WASMStructObject, field_data);
  1772. for (i = 0; i < field_count; i++) {
  1773. if (!resolve_value_type(&p, p_end, module, type_count,
  1774. &need_ref_type_map, &ref_type, true, error_buf,
  1775. error_buf_size)) {
  1776. goto fail;
  1777. }
  1778. type->fields[i].field_type = ref_type.ref_type;
  1779. if (need_ref_type_map) {
  1780. type->ref_type_maps[j].index = i;
  1781. if (!(type->ref_type_maps[j++].ref_type =
  1782. reftype_set_insert(module->ref_type_set, &ref_type,
  1783. error_buf, error_buf_size))) {
  1784. goto fail;
  1785. }
  1786. }
  1787. CHECK_BUF(p, p_end, 1);
  1788. type->fields[i].field_flags = read_uint8(p);
  1789. type->fields[i].field_size =
  1790. (uint8)wasm_reftype_size(ref_type.ref_type);
  1791. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1792. || defined(BUILD_TARGET_X86_32))
  1793. if (type->fields[i].field_size == 2)
  1794. offset = align_uint(offset, 2);
  1795. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1796. offset = align_uint(offset, 4);
  1797. #endif
  1798. type->fields[i].field_offset = offset;
  1799. if (wasm_is_type_reftype(ref_type.ref_type))
  1800. *reference_table++ = offset;
  1801. offset += type->fields[i].field_size;
  1802. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1803. type->fields[i].field_flags, type->fields[i].field_type);
  1804. }
  1805. type->total_size = offset;
  1806. bh_assert(j == type->ref_type_map_count);
  1807. #if TRACE_WASM_LOADER != 0
  1808. os_printf("type %d = ", type_idx);
  1809. wasm_dump_struct_type(type);
  1810. #endif
  1811. *p_buf = p;
  1812. module->types[type_idx] = (WASMType *)type;
  1813. return true;
  1814. fail:
  1815. if (type)
  1816. destroy_struct_type(type);
  1817. return false;
  1818. }
  1819. static bool
  1820. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1821. WASMModule *module, uint32 type_count, uint32 type_idx,
  1822. char *error_buf, uint32 error_buf_size)
  1823. {
  1824. const uint8 *p = *p_buf, *p_end = buf_end;
  1825. uint8 mutable;
  1826. bool need_ref_type_map;
  1827. WASMRefType ref_type;
  1828. WASMArrayType *type = NULL;
  1829. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1830. &ref_type, true, error_buf, error_buf_size)) {
  1831. return false;
  1832. }
  1833. CHECK_BUF(p, p_end, 1);
  1834. mutable = read_uint8(p);
  1835. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1836. return false;
  1837. }
  1838. LOG_VERBOSE("type %u: array", type_idx);
  1839. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1840. error_buf_size))) {
  1841. return false;
  1842. }
  1843. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1844. type->elem_flags = mutable;
  1845. type->elem_type = ref_type.ref_type;
  1846. if (need_ref_type_map) {
  1847. if (!(type->elem_ref_type =
  1848. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1849. error_buf_size))) {
  1850. goto fail;
  1851. }
  1852. }
  1853. #if TRACE_WASM_LOADER != 0
  1854. os_printf("type %d = ", type_idx);
  1855. wasm_dump_array_type(type);
  1856. #endif
  1857. *p_buf = p;
  1858. module->types[type_idx] = (WASMType *)type;
  1859. return true;
  1860. fail:
  1861. if (type)
  1862. destroy_array_type(type);
  1863. return false;
  1864. }
  1865. static bool
  1866. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1867. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1868. {
  1869. if (heap_type >= 0) {
  1870. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1871. error_buf_size)) {
  1872. return false;
  1873. }
  1874. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1875. heap_type);
  1876. }
  1877. else {
  1878. if (!wasm_is_valid_heap_type(heap_type)) {
  1879. set_error_buf(error_buf, error_buf_size, "unknown type");
  1880. return false;
  1881. }
  1882. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1883. heap_type);
  1884. if (nullable) {
  1885. /* For (ref null func/extern/any/eq/i31/data),
  1886. they are same as
  1887. funcref/externref/anyref/eqref/i31ref/dataref,
  1888. we convert the multi-byte type to one-byte
  1889. type to reduce the footprint and the
  1890. complexity of type equal/subtype checking */
  1891. ref_type->ref_type =
  1892. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1893. }
  1894. }
  1895. return true;
  1896. }
  1897. static void
  1898. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1899. WASMRefType *ref_type2)
  1900. {
  1901. /**
  1902. * The difference rt1 ∖ rt2 between two reference types is defined as
  1903. * follows:
  1904. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1905. * (ref ht2) = (ref null?1 ht1)
  1906. */
  1907. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1908. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1909. ref_type1, wasm_reftype_struct_size(ref_type1));
  1910. }
  1911. else {
  1912. ref_type_diff->ref_type = ref_type1->ref_type;
  1913. }
  1914. if (ref_type2->ref_ht_common.nullable) {
  1915. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1916. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1917. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1918. (int32)ref_type_diff->ref_type - 0x80);
  1919. }
  1920. else {
  1921. ref_type_diff->ref_ht_typeidx.nullable = false;
  1922. }
  1923. }
  1924. }
  1925. #else /* else of WASM_ENABLE_GC != 0 */
  1926. static void
  1927. destroy_wasm_type(WASMType *type)
  1928. {
  1929. if (type->ref_count > 1) {
  1930. /* The type is referenced by other types
  1931. of current wasm module */
  1932. type->ref_count--;
  1933. return;
  1934. }
  1935. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1936. && WASM_ENABLE_LAZY_JIT != 0
  1937. if (type->call_to_llvm_jit_from_fast_jit)
  1938. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1939. #endif
  1940. wasm_runtime_free(type);
  1941. }
  1942. #endif /* end of WASM_ENABLE_GC != 0 */
  1943. static bool
  1944. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1945. char *error_buf, uint32 error_buf_size)
  1946. {
  1947. const uint8 *p = buf, *p_end = buf_end;
  1948. uint32 type_count, i;
  1949. uint64 total_size;
  1950. uint8 flag;
  1951. #if WASM_ENABLE_GC != 0
  1952. uint32 processed_type_count = 0;
  1953. #endif
  1954. read_leb_uint32(p, p_end, type_count);
  1955. if (type_count) {
  1956. module->type_count = type_count;
  1957. total_size = sizeof(WASMType *) * (uint64)type_count;
  1958. if (!(module->types =
  1959. loader_malloc(total_size, error_buf, error_buf_size))) {
  1960. return false;
  1961. }
  1962. #if WASM_ENABLE_GC == 0
  1963. for (i = 0; i < type_count; i++) {
  1964. WASMFuncType *type;
  1965. const uint8 *p_org;
  1966. uint32 param_count, result_count, j;
  1967. uint32 param_cell_num, ret_cell_num;
  1968. CHECK_BUF(p, p_end, 1);
  1969. flag = read_uint8(p);
  1970. if (flag != 0x60) {
  1971. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1972. return false;
  1973. }
  1974. read_leb_uint32(p, p_end, param_count);
  1975. /* Resolve param count and result count firstly */
  1976. p_org = p;
  1977. CHECK_BUF(p, p_end, param_count);
  1978. p += param_count;
  1979. read_leb_uint32(p, p_end, result_count);
  1980. CHECK_BUF(p, p_end, result_count);
  1981. p = p_org;
  1982. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1983. set_error_buf(error_buf, error_buf_size,
  1984. "param count or result count too large");
  1985. return false;
  1986. }
  1987. total_size = offsetof(WASMFuncType, types)
  1988. + sizeof(uint8) * (uint64)(param_count + result_count);
  1989. if (!(type = module->types[i] =
  1990. loader_malloc(total_size, error_buf, error_buf_size))) {
  1991. return false;
  1992. }
  1993. /* Resolve param types and result types */
  1994. type->ref_count = 1;
  1995. type->param_count = (uint16)param_count;
  1996. type->result_count = (uint16)result_count;
  1997. for (j = 0; j < param_count; j++) {
  1998. CHECK_BUF(p, p_end, 1);
  1999. type->types[j] = read_uint8(p);
  2000. }
  2001. read_leb_uint32(p, p_end, result_count);
  2002. for (j = 0; j < result_count; j++) {
  2003. CHECK_BUF(p, p_end, 1);
  2004. type->types[param_count + j] = read_uint8(p);
  2005. }
  2006. for (j = 0; j < param_count + result_count; j++) {
  2007. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  2008. set_error_buf(error_buf, error_buf_size,
  2009. "unknown value type");
  2010. return false;
  2011. }
  2012. }
  2013. param_cell_num = wasm_get_cell_num(type->types, param_count);
  2014. ret_cell_num =
  2015. wasm_get_cell_num(type->types + param_count, result_count);
  2016. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  2017. set_error_buf(error_buf, error_buf_size,
  2018. "param count or result count too large");
  2019. return false;
  2020. }
  2021. type->param_cell_num = (uint16)param_cell_num;
  2022. type->ret_cell_num = (uint16)ret_cell_num;
  2023. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  2024. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  2025. #endif
  2026. #if WASM_ENABLE_WAMR_COMPILER != 0
  2027. for (j = 0; j < type->param_count + type->result_count; j++) {
  2028. if (type->types[j] == VALUE_TYPE_V128)
  2029. module->is_simd_used = true;
  2030. else if (type->types[j] == VALUE_TYPE_FUNCREF
  2031. || type->types[j] == VALUE_TYPE_EXTERNREF)
  2032. module->is_ref_types_used = true;
  2033. }
  2034. #endif
  2035. /* If there is already a same type created, use it instead */
  2036. for (j = 0; j < i; j++) {
  2037. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  2038. if (module->types[j]->ref_count == UINT16_MAX) {
  2039. set_error_buf(error_buf, error_buf_size,
  2040. "wasm type's ref count too large");
  2041. return false;
  2042. }
  2043. destroy_wasm_type(type);
  2044. module->types[i] = module->types[j];
  2045. module->types[j]->ref_count++;
  2046. break;
  2047. }
  2048. }
  2049. }
  2050. #else /* else of WASM_ENABLE_GC == 0 */
  2051. for (i = 0; i < type_count; i++) {
  2052. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  2053. uint32 rec_count = 1, j;
  2054. bool is_sub_final = true;
  2055. CHECK_BUF(p, p_end, 1);
  2056. flag = read_uint8(p);
  2057. if (flag == DEFINED_TYPE_REC) {
  2058. read_leb_uint32(p, p_end, rec_count);
  2059. if (rec_count > 1) {
  2060. uint64 new_total_size;
  2061. /* integer overflow */
  2062. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  2063. set_error_buf(error_buf, error_buf_size,
  2064. "recursive type count too large");
  2065. return false;
  2066. }
  2067. new_total_size =
  2068. sizeof(WASMFuncType *)
  2069. * (uint64)(module->type_count + rec_count - 1);
  2070. if (new_total_size > UINT32_MAX) {
  2071. set_error_buf(error_buf, error_buf_size,
  2072. "allocate memory failed");
  2073. return false;
  2074. }
  2075. MEM_REALLOC(module->types, (uint32)total_size,
  2076. (uint32)new_total_size);
  2077. module->type_count += rec_count - 1;
  2078. total_size = new_total_size;
  2079. }
  2080. LOG_VERBOSE("Processing rec group [%d-%d]",
  2081. processed_type_count,
  2082. processed_type_count + rec_count - 1);
  2083. }
  2084. else {
  2085. p--;
  2086. }
  2087. for (j = 0; j < rec_count; j++) {
  2088. WASMType *cur_type = NULL;
  2089. CHECK_BUF(p, p_end, 1);
  2090. flag = read_uint8(p);
  2091. parent_type_idx = -1;
  2092. if (flag == DEFINED_TYPE_SUB
  2093. || flag == DEFINED_TYPE_SUB_FINAL) {
  2094. read_leb_uint32(p, p_end, super_type_count);
  2095. if (super_type_count > 1) {
  2096. set_error_buf(error_buf, error_buf_size,
  2097. "super type count too large");
  2098. return false;
  2099. }
  2100. if (super_type_count > 0) {
  2101. read_leb_uint32(p, p_end, parent_type_idx);
  2102. if (parent_type_idx >= processed_type_count + j) {
  2103. set_error_buf_v(error_buf, error_buf_size,
  2104. "unknown type %d", parent_type_idx);
  2105. return false;
  2106. }
  2107. if (module->types[parent_type_idx]->is_sub_final) {
  2108. set_error_buf(error_buf, error_buf_size,
  2109. "sub type can not inherit from "
  2110. "a final super type");
  2111. return false;
  2112. }
  2113. }
  2114. if (flag == DEFINED_TYPE_SUB)
  2115. is_sub_final = false;
  2116. CHECK_BUF(p, p_end, 1);
  2117. flag = read_uint8(p);
  2118. }
  2119. if (flag == DEFINED_TYPE_FUNC) {
  2120. if (!resolve_func_type(&p, buf_end, module,
  2121. processed_type_count + rec_count,
  2122. processed_type_count + j, error_buf,
  2123. error_buf_size)) {
  2124. return false;
  2125. }
  2126. }
  2127. else if (flag == DEFINED_TYPE_STRUCT) {
  2128. if (!resolve_struct_type(&p, buf_end, module,
  2129. processed_type_count + rec_count,
  2130. processed_type_count + j,
  2131. error_buf, error_buf_size)) {
  2132. return false;
  2133. }
  2134. }
  2135. else if (flag == DEFINED_TYPE_ARRAY) {
  2136. if (!resolve_array_type(&p, buf_end, module,
  2137. processed_type_count + rec_count,
  2138. processed_type_count + j, error_buf,
  2139. error_buf_size)) {
  2140. return false;
  2141. }
  2142. }
  2143. else {
  2144. set_error_buf(error_buf, error_buf_size,
  2145. "invalid type flag");
  2146. return false;
  2147. }
  2148. cur_type = module->types[processed_type_count + j];
  2149. cur_type->ref_count = 1;
  2150. cur_type->parent_type_idx = parent_type_idx;
  2151. cur_type->is_sub_final = is_sub_final;
  2152. cur_type->rec_count = rec_count;
  2153. cur_type->rec_idx = j;
  2154. cur_type->rec_begin_type_idx = processed_type_count;
  2155. }
  2156. /* resolve subtyping relationship in current rec group */
  2157. for (j = 0; j < rec_count; j++) {
  2158. WASMType *cur_type = module->types[processed_type_count + j];
  2159. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  2160. WASMType *parent_type =
  2161. module->types[cur_type->parent_type_idx];
  2162. cur_type->parent_type = parent_type;
  2163. cur_type->root_type = parent_type->root_type;
  2164. if (parent_type->inherit_depth == UINT16_MAX) {
  2165. set_error_buf(error_buf, error_buf_size,
  2166. "parent type's inherit depth too large");
  2167. return false;
  2168. }
  2169. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  2170. }
  2171. else {
  2172. cur_type->parent_type = NULL;
  2173. cur_type->root_type = cur_type;
  2174. cur_type->inherit_depth = 0;
  2175. }
  2176. }
  2177. for (j = 0; j < rec_count; j++) {
  2178. WASMType *cur_type = module->types[processed_type_count + j];
  2179. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  2180. WASMType *parent_type =
  2181. module->types[cur_type->parent_type_idx];
  2182. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  2183. module->types,
  2184. module->type_count)) {
  2185. set_error_buf_v(error_buf, error_buf_size,
  2186. "sub type %u does not match super type",
  2187. processed_type_count + j);
  2188. return false;
  2189. }
  2190. }
  2191. }
  2192. /* If there is already an equivalence type or a group of equivalence
  2193. recursive types created, use it or them instead */
  2194. for (j = 0; j < processed_type_count;) {
  2195. WASMType *src_type = module->types[j];
  2196. WASMType *cur_type = module->types[processed_type_count];
  2197. uint32 k, src_rec_count;
  2198. src_rec_count = src_type->rec_count;
  2199. if (src_rec_count != rec_count) {
  2200. /* no type equivalence */
  2201. j += src_rec_count;
  2202. continue;
  2203. }
  2204. for (k = 0; k < rec_count; k++) {
  2205. src_type = module->types[j + k];
  2206. cur_type = module->types[processed_type_count + k];
  2207. if (!wasm_type_equal(src_type, cur_type, module->types,
  2208. module->type_count)) {
  2209. break;
  2210. }
  2211. }
  2212. if (k < rec_count) {
  2213. /* no type equivalence */
  2214. j += src_rec_count;
  2215. continue;
  2216. }
  2217. /* type equivalence */
  2218. for (k = 0; k < rec_count; k++) {
  2219. if (module->types[j + k]->ref_count == UINT16_MAX) {
  2220. set_error_buf(error_buf, error_buf_size,
  2221. "wasm type's ref count too large");
  2222. return false;
  2223. }
  2224. destroy_wasm_type(module->types[processed_type_count + k]);
  2225. module->types[processed_type_count + k] =
  2226. module->types[j + k];
  2227. module->types[j + k]->ref_count++;
  2228. }
  2229. break;
  2230. }
  2231. if (rec_count > 1) {
  2232. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  2233. processed_type_count,
  2234. processed_type_count + rec_count - 1);
  2235. }
  2236. processed_type_count += rec_count;
  2237. }
  2238. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  2239. * module->type_count,
  2240. error_buf, error_buf_size))) {
  2241. return false;
  2242. }
  2243. #endif /* end of WASM_ENABLE_GC == 0 */
  2244. }
  2245. if (p != p_end) {
  2246. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2247. return false;
  2248. }
  2249. LOG_VERBOSE("Load type section success.\n");
  2250. return true;
  2251. fail:
  2252. return false;
  2253. }
  2254. static void
  2255. adjust_table_max_size(bool is_table64, uint32 init_size, uint32 max_size_flag,
  2256. uint32 *max_size)
  2257. {
  2258. uint32 default_max_size;
  2259. /* TODO: current still use UINT32_MAX as upper limit for table size to keep
  2260. * ABI unchanged */
  2261. (void)is_table64;
  2262. if (UINT32_MAX / 2 > init_size)
  2263. default_max_size = init_size * 2;
  2264. else
  2265. default_max_size = UINT32_MAX;
  2266. if (default_max_size < WASM_TABLE_MAX_SIZE)
  2267. default_max_size = WASM_TABLE_MAX_SIZE;
  2268. if (max_size_flag) {
  2269. /* module defines the table limitation */
  2270. bh_assert(init_size <= *max_size);
  2271. if (init_size < *max_size) {
  2272. *max_size =
  2273. *max_size < default_max_size ? *max_size : default_max_size;
  2274. }
  2275. }
  2276. else {
  2277. /* partial defined table limitation, gives a default value */
  2278. *max_size = default_max_size;
  2279. }
  2280. }
  2281. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  2282. /**
  2283. * Find export item of a module with export info:
  2284. * module name, field name and export kind
  2285. */
  2286. static WASMExport *
  2287. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  2288. const char *field_name, uint8 export_kind,
  2289. char *error_buf, uint32 error_buf_size)
  2290. {
  2291. WASMExport *export =
  2292. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2293. export_kind, error_buf, error_buf_size);
  2294. return export;
  2295. }
  2296. #endif
  2297. #if WASM_ENABLE_MULTI_MODULE != 0
  2298. static WASMTable *
  2299. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2300. uint32 init_size, uint32 max_size, char *error_buf,
  2301. uint32 error_buf_size)
  2302. {
  2303. WASMModuleCommon *module_reg;
  2304. WASMTable *table = NULL;
  2305. WASMExport *export = NULL;
  2306. WASMModule *module = NULL;
  2307. module_reg = wasm_runtime_find_module_registered(module_name);
  2308. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2309. LOG_DEBUG("can not find a module named %s for table", module_name);
  2310. set_error_buf(error_buf, error_buf_size, "unknown import");
  2311. return NULL;
  2312. }
  2313. module = (WASMModule *)module_reg;
  2314. export =
  2315. wasm_loader_find_export(module, module_name, table_name,
  2316. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2317. if (!export) {
  2318. return NULL;
  2319. }
  2320. /* resolve table and check the init/max size */
  2321. if (export->index < module->import_table_count) {
  2322. table =
  2323. module->import_tables[export->index].u.table.import_table_linked;
  2324. }
  2325. else {
  2326. table = &(module->tables[export->index - module->import_table_count]);
  2327. }
  2328. if (table->table_type.init_size < init_size
  2329. || table->table_type.max_size > max_size) {
  2330. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2331. module_name, table_name, table->table_type.init_size,
  2332. table->table_type.max_size, init_size, max_size);
  2333. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2334. return NULL;
  2335. }
  2336. return table;
  2337. }
  2338. static WASMMemory *
  2339. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2340. uint32 init_page_count, uint32 max_page_count,
  2341. char *error_buf, uint32 error_buf_size)
  2342. {
  2343. WASMModuleCommon *module_reg;
  2344. WASMMemory *memory = NULL;
  2345. WASMExport *export = NULL;
  2346. WASMModule *module = NULL;
  2347. module_reg = wasm_runtime_find_module_registered(module_name);
  2348. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2349. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2350. set_error_buf(error_buf, error_buf_size, "unknown import");
  2351. return NULL;
  2352. }
  2353. module = (WASMModule *)module_reg;
  2354. export =
  2355. wasm_loader_find_export(module, module_name, memory_name,
  2356. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2357. if (!export) {
  2358. return NULL;
  2359. }
  2360. /* resolve memory and check the init/max page count */
  2361. if (export->index < module->import_memory_count) {
  2362. memory = module->import_memories[export->index]
  2363. .u.memory.import_memory_linked;
  2364. }
  2365. else {
  2366. memory =
  2367. &(module->memories[export->index - module->import_memory_count]);
  2368. }
  2369. if (memory->init_page_count < init_page_count
  2370. || memory->max_page_count > max_page_count) {
  2371. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2372. module_name, memory_name, memory->init_page_count,
  2373. memory->max_page_count, init_page_count, max_page_count);
  2374. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2375. return NULL;
  2376. }
  2377. return memory;
  2378. }
  2379. static WASMGlobal *
  2380. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2381. uint8 type, bool is_mutable, char *error_buf,
  2382. uint32 error_buf_size)
  2383. {
  2384. WASMModuleCommon *module_reg;
  2385. WASMGlobal *global = NULL;
  2386. WASMExport *export = NULL;
  2387. WASMModule *module = NULL;
  2388. module_reg = wasm_runtime_find_module_registered(module_name);
  2389. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2390. LOG_DEBUG("can not find a module named %s for global", module_name);
  2391. set_error_buf(error_buf, error_buf_size, "unknown import");
  2392. return NULL;
  2393. }
  2394. module = (WASMModule *)module_reg;
  2395. export =
  2396. wasm_loader_find_export(module, module_name, global_name,
  2397. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2398. if (!export) {
  2399. return NULL;
  2400. }
  2401. /* resolve and check the global */
  2402. if (export->index < module->import_global_count) {
  2403. global =
  2404. module->import_globals[export->index].u.global.import_global_linked;
  2405. }
  2406. else {
  2407. global =
  2408. &(module->globals[export->index - module->import_global_count]);
  2409. }
  2410. if (global->type.val_type != type
  2411. || global->type.is_mutable != is_mutable) {
  2412. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2413. module_name, global_name, global->type.val_type,
  2414. global->type.is_mutable, type, is_mutable);
  2415. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2416. return NULL;
  2417. }
  2418. return global;
  2419. }
  2420. #if WASM_ENABLE_TAGS != 0
  2421. static WASMTag *
  2422. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2423. const WASMType *expected_tag_type,
  2424. uint32 *linked_tag_index, char *error_buf,
  2425. uint32 error_buf_size)
  2426. {
  2427. WASMModuleCommon *module_reg;
  2428. WASMTag *tag = NULL;
  2429. WASMExport *export = NULL;
  2430. WASMModule *module = NULL;
  2431. module_reg = wasm_runtime_find_module_registered(module_name);
  2432. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2433. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2434. tag_name);
  2435. set_error_buf(error_buf, error_buf_size, "unknown import");
  2436. return NULL;
  2437. }
  2438. module = (WASMModule *)module_reg;
  2439. export =
  2440. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2441. error_buf, error_buf_size);
  2442. if (!export) {
  2443. return NULL;
  2444. }
  2445. /* resolve tag type and tag */
  2446. if (export->index < module->import_tag_count) {
  2447. /* importing an imported tag from the submodule */
  2448. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2449. }
  2450. else {
  2451. /* importing an section tag from the submodule */
  2452. tag = module->tags[export->index - module->import_tag_count];
  2453. }
  2454. /* check function type */
  2455. if (!wasm_type_equal(expected_tag_type, tag->tag_type, module->types,
  2456. module->type_count)) {
  2457. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2458. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2459. return NULL;
  2460. }
  2461. if (linked_tag_index != NULL) {
  2462. *linked_tag_index = export->index;
  2463. }
  2464. return tag;
  2465. }
  2466. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2467. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2468. static bool
  2469. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2470. const WASMModule *parent_module,
  2471. const char *sub_module_name, const char *function_name,
  2472. WASMFunctionImport *function, bool no_resolve,
  2473. char *error_buf, uint32 error_buf_size)
  2474. {
  2475. const uint8 *p = *p_buf, *p_end = buf_end;
  2476. uint32 declare_type_index = 0;
  2477. read_leb_uint32(p, p_end, declare_type_index);
  2478. *p_buf = p;
  2479. if (!check_function_type(parent_module, declare_type_index, error_buf,
  2480. error_buf_size)) {
  2481. return false;
  2482. }
  2483. #if WASM_ENABLE_GC != 0
  2484. function->type_idx = declare_type_index;
  2485. #endif
  2486. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2487. declare_type_index = wasm_get_smallest_type_idx(
  2488. parent_module->types, parent_module->type_count, declare_type_index);
  2489. #endif
  2490. function->func_type =
  2491. (WASMFuncType *)parent_module->types[declare_type_index];
  2492. function->module_name = (char *)sub_module_name;
  2493. function->field_name = (char *)function_name;
  2494. function->attachment = NULL;
  2495. function->signature = NULL;
  2496. function->call_conv_raw = false;
  2497. /* lookup registered native symbols first */
  2498. if (!no_resolve) {
  2499. wasm_resolve_import_func(parent_module, function);
  2500. }
  2501. return true;
  2502. fail:
  2503. return false;
  2504. }
  2505. static bool
  2506. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2507. uint32 error_buf_size)
  2508. {
  2509. if (max_size < init_size) {
  2510. set_error_buf(error_buf, error_buf_size,
  2511. "size minimum must not be greater than maximum");
  2512. return false;
  2513. }
  2514. return true;
  2515. }
  2516. static bool
  2517. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2518. WASMModule *parent_module, const char *sub_module_name,
  2519. const char *table_name, WASMTableImport *table,
  2520. char *error_buf, uint32 error_buf_size)
  2521. {
  2522. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2523. uint32 declare_elem_type = 0, table_flag = 0, declare_init_size = 0,
  2524. declare_max_size = 0;
  2525. #if WASM_ENABLE_MULTI_MODULE != 0
  2526. WASMModule *sub_module = NULL;
  2527. WASMTable *linked_table = NULL;
  2528. #endif
  2529. #if WASM_ENABLE_GC != 0
  2530. WASMRefType ref_type;
  2531. bool need_ref_type_map;
  2532. #endif
  2533. bool is_table64 = false;
  2534. #if WASM_ENABLE_GC == 0
  2535. CHECK_BUF(p, p_end, 1);
  2536. /* 0x70 or 0x6F */
  2537. declare_elem_type = read_uint8(p);
  2538. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2539. #if WASM_ENABLE_REF_TYPES != 0
  2540. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2541. #endif
  2542. ) {
  2543. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2544. return false;
  2545. }
  2546. #else /* else of WASM_ENABLE_GC == 0 */
  2547. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2548. &need_ref_type_map, &ref_type, false, error_buf,
  2549. error_buf_size)) {
  2550. return false;
  2551. }
  2552. if (!wasm_is_type_reftype(ref_type.ref_type)
  2553. || wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2554. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2555. return false;
  2556. }
  2557. declare_elem_type = ref_type.ref_type;
  2558. if (need_ref_type_map) {
  2559. if (!(table->table_type.elem_ref_type =
  2560. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2561. error_buf, error_buf_size))) {
  2562. return false;
  2563. }
  2564. }
  2565. #if TRACE_WASM_LOADER != 0
  2566. os_printf("import table type: ");
  2567. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2568. os_printf("\n");
  2569. #endif
  2570. #endif /* end of WASM_ENABLE_GC == 0 */
  2571. p_org = p;
  2572. read_leb_uint32(p, p_end, table_flag);
  2573. is_table64 = table_flag & TABLE64_FLAG;
  2574. if (p - p_org > 1) {
  2575. LOG_VERBOSE("integer representation too long(import table)");
  2576. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2577. return false;
  2578. }
  2579. if (!wasm_table_check_flags(table_flag, error_buf, error_buf_size, false)) {
  2580. return false;
  2581. }
  2582. read_leb_uint32(p, p_end, declare_init_size);
  2583. if (table_flag & MAX_TABLE_SIZE_FLAG) {
  2584. read_leb_uint32(p, p_end, declare_max_size);
  2585. if (!check_table_max_size(declare_init_size, declare_max_size,
  2586. error_buf, error_buf_size))
  2587. return false;
  2588. }
  2589. adjust_table_max_size(is_table64, declare_init_size,
  2590. table_flag & MAX_TABLE_SIZE_FLAG, &declare_max_size);
  2591. *p_buf = p;
  2592. #if WASM_ENABLE_MULTI_MODULE != 0
  2593. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2594. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2595. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2596. error_buf_size);
  2597. if (sub_module) {
  2598. linked_table = wasm_loader_resolve_table(
  2599. sub_module_name, table_name, declare_init_size,
  2600. declare_max_size, error_buf, error_buf_size);
  2601. if (linked_table) {
  2602. /* reset with linked table limit */
  2603. declare_elem_type = linked_table->table_type.elem_type;
  2604. declare_init_size = linked_table->table_type.init_size;
  2605. declare_max_size = linked_table->table_type.max_size;
  2606. table_flag = linked_table->table_type.flags;
  2607. table->import_table_linked = linked_table;
  2608. table->import_module = sub_module;
  2609. }
  2610. }
  2611. }
  2612. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2613. /* (table (export "table") 10 20 funcref) */
  2614. /* (table (export "table64") 10 20 funcref) */
  2615. /* we need this section working in wamrc */
  2616. if (!strcmp("spectest", sub_module_name)) {
  2617. const uint32 spectest_table_init_size = 10;
  2618. const uint32 spectest_table_max_size = 20;
  2619. if (strcmp("table", table_name)
  2620. #if WASM_ENABLE_MEMORY64 != 0
  2621. && strcmp("table64", table_name)
  2622. #endif
  2623. ) {
  2624. set_error_buf(error_buf, error_buf_size,
  2625. "incompatible import type or unknown import");
  2626. return false;
  2627. }
  2628. if (declare_init_size > spectest_table_init_size
  2629. || declare_max_size < spectest_table_max_size) {
  2630. set_error_buf(error_buf, error_buf_size,
  2631. "incompatible import type");
  2632. return false;
  2633. }
  2634. declare_init_size = spectest_table_init_size;
  2635. declare_max_size = spectest_table_max_size;
  2636. }
  2637. /* now we believe all declaration are ok */
  2638. table->table_type.elem_type = declare_elem_type;
  2639. table->table_type.init_size = declare_init_size;
  2640. table->table_type.flags = table_flag;
  2641. table->table_type.max_size = declare_max_size;
  2642. #if WASM_ENABLE_WAMR_COMPILER != 0
  2643. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2644. parent_module->is_ref_types_used = true;
  2645. #endif
  2646. (void)parent_module;
  2647. return true;
  2648. fail:
  2649. return false;
  2650. }
  2651. static bool
  2652. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2653. uint32 error_buf_size)
  2654. {
  2655. uint32 default_max_size =
  2656. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2657. if (!is_memory64 && init_size > default_max_size) {
  2658. set_error_buf(error_buf, error_buf_size,
  2659. "memory size must be at most 65536 pages (4GiB)");
  2660. return false;
  2661. }
  2662. #if WASM_ENABLE_MEMORY64 != 0
  2663. else if (is_memory64 && init_size > default_max_size) {
  2664. set_error_buf(
  2665. error_buf, error_buf_size,
  2666. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2667. return false;
  2668. }
  2669. #endif
  2670. return true;
  2671. }
  2672. static bool
  2673. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2674. char *error_buf, uint32 error_buf_size)
  2675. {
  2676. uint32 default_max_size =
  2677. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2678. if (max_size < init_size) {
  2679. set_error_buf(error_buf, error_buf_size,
  2680. "size minimum must not be greater than maximum");
  2681. return false;
  2682. }
  2683. if (!is_memory64 && max_size > default_max_size) {
  2684. set_error_buf(error_buf, error_buf_size,
  2685. "memory size must be at most 65536 pages (4GiB)");
  2686. return false;
  2687. }
  2688. #if WASM_ENABLE_MEMORY64 != 0
  2689. else if (is_memory64 && max_size > default_max_size) {
  2690. set_error_buf(
  2691. error_buf, error_buf_size,
  2692. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2693. return false;
  2694. }
  2695. #endif
  2696. return true;
  2697. }
  2698. static bool
  2699. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2700. WASMModule *parent_module, const char *sub_module_name,
  2701. const char *memory_name, WASMMemoryImport *memory,
  2702. char *error_buf, uint32 error_buf_size)
  2703. {
  2704. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2705. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2706. uint32 pool_size = wasm_runtime_memory_pool_size();
  2707. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2708. / DEFAULT_NUM_BYTES_PER_PAGE;
  2709. #else
  2710. uint32 max_page_count;
  2711. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2712. uint32 mem_flag = 0;
  2713. bool is_memory64 = false;
  2714. uint32 declare_init_page_count = 0;
  2715. uint32 declare_max_page_count = 0;
  2716. #if WASM_ENABLE_MULTI_MODULE != 0
  2717. WASMModule *sub_module = NULL;
  2718. WASMMemory *linked_memory = NULL;
  2719. #endif
  2720. p_org = p;
  2721. read_leb_uint32(p, p_end, mem_flag);
  2722. is_memory64 = mem_flag & MEMORY64_FLAG;
  2723. if (p - p_org > 1) {
  2724. LOG_VERBOSE("integer representation too long(import memory)");
  2725. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2726. return false;
  2727. }
  2728. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2729. return false;
  2730. }
  2731. read_leb_uint32(p, p_end, declare_init_page_count);
  2732. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2733. error_buf_size)) {
  2734. return false;
  2735. }
  2736. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2737. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2738. #endif
  2739. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2740. read_leb_uint32(p, p_end, declare_max_page_count);
  2741. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2742. declare_max_page_count, error_buf,
  2743. error_buf_size)) {
  2744. return false;
  2745. }
  2746. if (declare_max_page_count > max_page_count) {
  2747. declare_max_page_count = max_page_count;
  2748. }
  2749. }
  2750. else {
  2751. /* Limit the maximum memory size to max_page_count */
  2752. declare_max_page_count = max_page_count;
  2753. }
  2754. #if WASM_ENABLE_MULTI_MODULE != 0
  2755. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2756. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2757. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2758. error_buf_size);
  2759. if (sub_module) {
  2760. linked_memory = wasm_loader_resolve_memory(
  2761. sub_module_name, memory_name, declare_init_page_count,
  2762. declare_max_page_count, error_buf, error_buf_size);
  2763. if (linked_memory) {
  2764. /**
  2765. * reset with linked memory limit
  2766. */
  2767. memory->import_module = sub_module;
  2768. memory->import_memory_linked = linked_memory;
  2769. declare_init_page_count = linked_memory->init_page_count;
  2770. declare_max_page_count = linked_memory->max_page_count;
  2771. }
  2772. }
  2773. }
  2774. #endif
  2775. /* (memory (export "memory") 1 2) */
  2776. if (!strcmp("spectest", sub_module_name)) {
  2777. uint32 spectest_memory_init_page = 1;
  2778. uint32 spectest_memory_max_page = 2;
  2779. if (strcmp("memory", memory_name)) {
  2780. set_error_buf(error_buf, error_buf_size,
  2781. "incompatible import type or unknown import");
  2782. return false;
  2783. }
  2784. if (declare_init_page_count > spectest_memory_init_page
  2785. || declare_max_page_count < spectest_memory_max_page) {
  2786. set_error_buf(error_buf, error_buf_size,
  2787. "incompatible import type");
  2788. return false;
  2789. }
  2790. declare_init_page_count = spectest_memory_init_page;
  2791. declare_max_page_count = spectest_memory_max_page;
  2792. }
  2793. #if WASM_ENABLE_WASI_TEST != 0
  2794. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2795. else if (!strcmp("foo", sub_module_name)) {
  2796. uint32 spectest_memory_init_page = 1;
  2797. uint32 spectest_memory_max_page = 1;
  2798. if (strcmp("bar", memory_name)) {
  2799. set_error_buf(error_buf, error_buf_size,
  2800. "incompatible import type or unknown import");
  2801. return false;
  2802. }
  2803. if (declare_init_page_count > spectest_memory_init_page
  2804. || declare_max_page_count < spectest_memory_max_page) {
  2805. set_error_buf(error_buf, error_buf_size,
  2806. "incompatible import type");
  2807. return false;
  2808. }
  2809. declare_init_page_count = spectest_memory_init_page;
  2810. declare_max_page_count = spectest_memory_max_page;
  2811. }
  2812. #endif
  2813. /* now we believe all declaration are ok */
  2814. memory->mem_type.flags = mem_flag;
  2815. memory->mem_type.init_page_count = declare_init_page_count;
  2816. memory->mem_type.max_page_count = declare_max_page_count;
  2817. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2818. *p_buf = p;
  2819. (void)parent_module;
  2820. return true;
  2821. fail:
  2822. return false;
  2823. }
  2824. #if WASM_ENABLE_TAGS != 0
  2825. static bool
  2826. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2827. const WASMModule *parent_module, /* this module ! */
  2828. const char *sub_module_name, const char *tag_name,
  2829. WASMTagImport *tag, /* structure to fill */
  2830. char *error_buf, uint32 error_buf_size)
  2831. {
  2832. /* attribute and type of the import statement */
  2833. uint8 declare_tag_attribute;
  2834. uint32 declare_type_index;
  2835. const uint8 *p = *p_buf, *p_end = buf_end;
  2836. #if WASM_ENABLE_MULTI_MODULE != 0
  2837. WASMModule *sub_module = NULL;
  2838. #endif
  2839. /* get the one byte attribute */
  2840. CHECK_BUF(p, p_end, 1);
  2841. declare_tag_attribute = read_uint8(p);
  2842. if (declare_tag_attribute != 0) {
  2843. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2844. goto fail;
  2845. }
  2846. /* get type */
  2847. read_leb_uint32(p, p_end, declare_type_index);
  2848. /* compare against module->types */
  2849. if (!check_function_type(parent_module, declare_type_index, error_buf,
  2850. error_buf_size)) {
  2851. goto fail;
  2852. }
  2853. WASMFuncType *declare_tag_type =
  2854. (WASMFuncType *)parent_module->types[declare_type_index];
  2855. /* check, that the type of the declared tag returns void */
  2856. if (declare_tag_type->result_count != 0) {
  2857. set_error_buf(error_buf, error_buf_size,
  2858. "tag type signature does not return void");
  2859. goto fail;
  2860. }
  2861. #if WASM_ENABLE_MULTI_MODULE != 0
  2862. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2863. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2864. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2865. error_buf_size);
  2866. if (sub_module) {
  2867. /* wasm_loader_resolve_tag checks, that the imported tag
  2868. * and the declared tag have the same type
  2869. */
  2870. uint32 linked_tag_index = 0;
  2871. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2872. sub_module_name, tag_name, declare_tag_type,
  2873. &linked_tag_index /* out */, error_buf, error_buf_size);
  2874. if (linked_tag) {
  2875. tag->import_module = sub_module;
  2876. tag->import_tag_linked = linked_tag;
  2877. tag->import_tag_index_linked = linked_tag_index;
  2878. }
  2879. }
  2880. }
  2881. #endif
  2882. /* store to module tag declarations */
  2883. tag->attribute = declare_tag_attribute;
  2884. tag->type = declare_type_index;
  2885. tag->module_name = (char *)sub_module_name;
  2886. tag->field_name = (char *)tag_name;
  2887. tag->tag_type = declare_tag_type;
  2888. *p_buf = p;
  2889. (void)parent_module;
  2890. LOG_VERBOSE("Load tag import success\n");
  2891. return true;
  2892. fail:
  2893. return false;
  2894. }
  2895. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2896. static bool
  2897. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2898. WASMModule *parent_module, char *sub_module_name,
  2899. char *global_name, WASMGlobalImport *global, char *error_buf,
  2900. uint32 error_buf_size)
  2901. {
  2902. const uint8 *p = *p_buf, *p_end = buf_end;
  2903. uint8 declare_type = 0;
  2904. uint8 declare_mutable = 0;
  2905. #if WASM_ENABLE_MULTI_MODULE != 0
  2906. WASMModule *sub_module = NULL;
  2907. WASMGlobal *linked_global = NULL;
  2908. #endif
  2909. #if WASM_ENABLE_GC != 0
  2910. WASMRefType ref_type;
  2911. bool need_ref_type_map;
  2912. #endif
  2913. bool ret = false;
  2914. #if WASM_ENABLE_GC == 0
  2915. CHECK_BUF(p, p_end, 2);
  2916. /* global type */
  2917. declare_type = read_uint8(p);
  2918. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2919. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2920. return false;
  2921. }
  2922. declare_mutable = read_uint8(p);
  2923. #else
  2924. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2925. &need_ref_type_map, &ref_type, false, error_buf,
  2926. error_buf_size)) {
  2927. return false;
  2928. }
  2929. declare_type = ref_type.ref_type;
  2930. if (need_ref_type_map) {
  2931. if (!(global->ref_type =
  2932. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2933. error_buf, error_buf_size))) {
  2934. return false;
  2935. }
  2936. }
  2937. #if TRACE_WASM_LOADER != 0
  2938. os_printf("import global type: ");
  2939. wasm_dump_value_type(declare_type, global->ref_type);
  2940. os_printf("\n");
  2941. #endif
  2942. CHECK_BUF(p, p_end, 1);
  2943. declare_mutable = read_uint8(p);
  2944. #endif /* end of WASM_ENABLE_GC == 0 */
  2945. *p_buf = p;
  2946. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2947. return false;
  2948. }
  2949. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2950. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2951. global);
  2952. if (ret) {
  2953. if (global->type.val_type != declare_type
  2954. || global->type.is_mutable != declare_mutable) {
  2955. set_error_buf(error_buf, error_buf_size,
  2956. "incompatible import type");
  2957. return false;
  2958. }
  2959. global->is_linked = true;
  2960. }
  2961. #endif
  2962. #if WASM_ENABLE_MULTI_MODULE != 0
  2963. if (!global->is_linked
  2964. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2965. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2966. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2967. error_buf_size);
  2968. if (sub_module) {
  2969. /* check sub modules */
  2970. linked_global = wasm_loader_resolve_global(
  2971. sub_module_name, global_name, declare_type, declare_mutable,
  2972. error_buf, error_buf_size);
  2973. if (linked_global) {
  2974. global->import_module = sub_module;
  2975. global->import_global_linked = linked_global;
  2976. global->is_linked = true;
  2977. }
  2978. }
  2979. }
  2980. #endif
  2981. global->module_name = sub_module_name;
  2982. global->field_name = global_name;
  2983. global->type.val_type = declare_type;
  2984. global->type.is_mutable = (declare_mutable == 1);
  2985. #if WASM_ENABLE_WAMR_COMPILER != 0
  2986. if (global->type.val_type == VALUE_TYPE_V128)
  2987. parent_module->is_simd_used = true;
  2988. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2989. parent_module->is_ref_types_used = true;
  2990. #endif
  2991. (void)parent_module;
  2992. (void)ret;
  2993. return true;
  2994. fail:
  2995. return false;
  2996. }
  2997. static bool
  2998. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2999. WASMTable *table, char *error_buf, uint32 error_buf_size)
  3000. {
  3001. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  3002. #if WASM_ENABLE_GC != 0
  3003. WASMRefType ref_type;
  3004. bool need_ref_type_map;
  3005. #endif
  3006. bool is_table64 = false;
  3007. #if WASM_ENABLE_GC == 0
  3008. CHECK_BUF(p, p_end, 1);
  3009. /* 0x70 or 0x6F */
  3010. table->table_type.elem_type = read_uint8(p);
  3011. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  3012. #if WASM_ENABLE_REF_TYPES != 0
  3013. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  3014. #endif
  3015. ) {
  3016. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  3017. return false;
  3018. }
  3019. #else /* else of WASM_ENABLE_GC == 0 */
  3020. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3021. &need_ref_type_map, &ref_type, false, error_buf,
  3022. error_buf_size)) {
  3023. return false;
  3024. }
  3025. /*
  3026. * TODO: add this validator
  3027. * `wasm_is_reftype_htref_non_nullable(ref_type.ref_type)`
  3028. * after sync up with the latest GC spec
  3029. */
  3030. if (!wasm_is_type_reftype(ref_type.ref_type)) {
  3031. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3032. return false;
  3033. }
  3034. table->table_type.elem_type = ref_type.ref_type;
  3035. if (need_ref_type_map) {
  3036. if (!(table->table_type.elem_ref_type =
  3037. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  3038. error_buf_size))) {
  3039. return false;
  3040. }
  3041. }
  3042. #if TRACE_WASM_LOADER != 0
  3043. os_printf("table type: ");
  3044. wasm_dump_value_type(table->table_type.elem_type,
  3045. table->table_type.elem_ref_type);
  3046. os_printf("\n");
  3047. #endif
  3048. #endif /* end of WASM_ENABLE_GC == 0 */
  3049. p_org = p;
  3050. read_leb_uint32(p, p_end, table->table_type.flags);
  3051. is_table64 = table->table_type.flags & TABLE64_FLAG;
  3052. if (p - p_org > 1) {
  3053. LOG_VERBOSE("integer representation too long(table)");
  3054. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  3055. return false;
  3056. }
  3057. if (!wasm_table_check_flags(table->table_type.flags, error_buf,
  3058. error_buf_size, false)) {
  3059. return false;
  3060. }
  3061. read_leb_uint32(p, p_end, table->table_type.init_size);
  3062. if (table->table_type.flags & MAX_TABLE_SIZE_FLAG) {
  3063. read_leb_uint32(p, p_end, table->table_type.max_size);
  3064. if (!check_table_max_size(table->table_type.init_size,
  3065. table->table_type.max_size, error_buf,
  3066. error_buf_size))
  3067. return false;
  3068. }
  3069. adjust_table_max_size(is_table64, table->table_type.init_size,
  3070. table->table_type.flags & MAX_TABLE_SIZE_FLAG,
  3071. &table->table_type.max_size);
  3072. #if WASM_ENABLE_WAMR_COMPILER != 0
  3073. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3074. module->is_ref_types_used = true;
  3075. #endif
  3076. *p_buf = p;
  3077. return true;
  3078. fail:
  3079. return false;
  3080. }
  3081. static bool
  3082. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  3083. char *error_buf, uint32 error_buf_size)
  3084. {
  3085. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  3086. #if WASM_ENABLE_APP_FRAMEWORK != 0
  3087. uint32 pool_size = wasm_runtime_memory_pool_size();
  3088. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  3089. / DEFAULT_NUM_BYTES_PER_PAGE;
  3090. #else
  3091. uint32 max_page_count;
  3092. #endif
  3093. bool is_memory64 = false;
  3094. p_org = p;
  3095. read_leb_uint32(p, p_end, memory->flags);
  3096. is_memory64 = memory->flags & MEMORY64_FLAG;
  3097. if (p - p_org > 1) {
  3098. LOG_VERBOSE("integer representation too long(memory)");
  3099. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  3100. return false;
  3101. }
  3102. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  3103. false)) {
  3104. return false;
  3105. }
  3106. read_leb_uint32(p, p_end, memory->init_page_count);
  3107. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  3108. error_buf_size))
  3109. return false;
  3110. #if WASM_ENABLE_APP_FRAMEWORK == 0
  3111. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  3112. #endif
  3113. if (memory->flags & 1) {
  3114. read_leb_uint32(p, p_end, memory->max_page_count);
  3115. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  3116. memory->max_page_count, error_buf,
  3117. error_buf_size))
  3118. return false;
  3119. if (memory->max_page_count > max_page_count)
  3120. memory->max_page_count = max_page_count;
  3121. }
  3122. else {
  3123. /* Limit the maximum memory size to max_page_count */
  3124. memory->max_page_count = max_page_count;
  3125. }
  3126. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  3127. *p_buf = p;
  3128. return true;
  3129. fail:
  3130. return false;
  3131. }
  3132. static int
  3133. cmp_export_name(const void *a, const void *b)
  3134. {
  3135. return strcmp(*(char **)a, *(char **)b);
  3136. }
  3137. static bool
  3138. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3139. bool is_load_from_file_buf, bool no_resolve,
  3140. char *error_buf, uint32 error_buf_size)
  3141. {
  3142. const uint8 *p = buf, *p_end = buf_end, *p_old;
  3143. uint32 import_count, name_len, type_index, i, u32, flags;
  3144. uint64 total_size;
  3145. WASMImport *import;
  3146. WASMImport *import_functions = NULL, *import_tables = NULL;
  3147. WASMImport *import_memories = NULL, *import_globals = NULL;
  3148. #if WASM_ENABLE_TAGS != 0
  3149. WASMImport *import_tags = NULL;
  3150. #endif
  3151. char *sub_module_name, *field_name;
  3152. uint8 u8, kind, global_type;
  3153. read_leb_uint32(p, p_end, import_count);
  3154. if (import_count) {
  3155. module->import_count = import_count;
  3156. total_size = sizeof(WASMImport) * (uint64)import_count;
  3157. if (!(module->imports =
  3158. loader_malloc(total_size, error_buf, error_buf_size))) {
  3159. return false;
  3160. }
  3161. p_old = p;
  3162. /* Scan firstly to get import count of each type */
  3163. for (i = 0; i < import_count; i++) {
  3164. /* module name */
  3165. read_leb_uint32(p, p_end, name_len);
  3166. CHECK_BUF(p, p_end, name_len);
  3167. p += name_len;
  3168. /* field name */
  3169. read_leb_uint32(p, p_end, name_len);
  3170. CHECK_BUF(p, p_end, name_len);
  3171. p += name_len;
  3172. CHECK_BUF(p, p_end, 1);
  3173. /* 0x00/0x01/0x02/0x03/0x04 */
  3174. kind = read_uint8(p);
  3175. switch (kind) {
  3176. case IMPORT_KIND_FUNC: /* import function */
  3177. read_leb_uint32(p, p_end, type_index);
  3178. module->import_function_count++;
  3179. break;
  3180. case IMPORT_KIND_TABLE: /* import table */
  3181. CHECK_BUF(p, p_end, 1);
  3182. /* 0x70 */
  3183. u8 = read_uint8(p);
  3184. #if WASM_ENABLE_GC != 0
  3185. if (wasm_is_reftype_htref_nullable(u8)) {
  3186. int32 heap_type;
  3187. read_leb_int32(p, p_end, heap_type);
  3188. (void)heap_type;
  3189. }
  3190. #endif
  3191. read_leb_uint32(p, p_end, flags);
  3192. read_leb_uint32(p, p_end, u32);
  3193. if (flags & 1)
  3194. read_leb_uint32(p, p_end, u32);
  3195. module->import_table_count++;
  3196. if (module->import_table_count > 1) {
  3197. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3198. set_error_buf(error_buf, error_buf_size,
  3199. "multiple tables");
  3200. return false;
  3201. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3202. module->is_ref_types_used = true;
  3203. #endif
  3204. }
  3205. break;
  3206. case IMPORT_KIND_MEMORY: /* import memory */
  3207. read_leb_uint32(p, p_end, flags);
  3208. read_leb_uint32(p, p_end, u32);
  3209. if (flags & 1)
  3210. read_leb_uint32(p, p_end, u32);
  3211. module->import_memory_count++;
  3212. #if WASM_ENABLE_MULTI_MEMORY == 0
  3213. if (module->import_memory_count > 1) {
  3214. set_error_buf(error_buf, error_buf_size,
  3215. "multiple memories");
  3216. return false;
  3217. }
  3218. #endif
  3219. break;
  3220. #if WASM_ENABLE_TAGS != 0
  3221. case IMPORT_KIND_TAG: /* import tags */
  3222. /* it only counts the number of tags to import */
  3223. module->import_tag_count++;
  3224. CHECK_BUF(p, p_end, 1);
  3225. u8 = read_uint8(p);
  3226. read_leb_uint32(p, p_end, type_index);
  3227. break;
  3228. #endif
  3229. case IMPORT_KIND_GLOBAL: /* import global */
  3230. #if WASM_ENABLE_GC != 0
  3231. /* valtype */
  3232. CHECK_BUF(p, p_end, 1);
  3233. global_type = read_uint8(p);
  3234. if (wasm_is_reftype_htref_nullable(global_type)
  3235. || wasm_is_reftype_htref_non_nullable(global_type)) {
  3236. int32 heap_type;
  3237. read_leb_int32(p, p_end, heap_type);
  3238. (void)heap_type;
  3239. }
  3240. /* mutability */
  3241. CHECK_BUF(p, p_end, 1);
  3242. p += 1;
  3243. #else
  3244. CHECK_BUF(p, p_end, 2);
  3245. p += 2;
  3246. #endif
  3247. (void)global_type;
  3248. module->import_global_count++;
  3249. break;
  3250. default:
  3251. set_error_buf(error_buf, error_buf_size,
  3252. "invalid import kind");
  3253. return false;
  3254. }
  3255. }
  3256. if (module->import_function_count)
  3257. import_functions = module->import_functions = module->imports;
  3258. if (module->import_table_count)
  3259. import_tables = module->import_tables =
  3260. module->imports + module->import_function_count;
  3261. if (module->import_memory_count)
  3262. import_memories = module->import_memories =
  3263. module->imports + module->import_function_count
  3264. + module->import_table_count;
  3265. #if WASM_ENABLE_TAGS != 0
  3266. if (module->import_tag_count)
  3267. import_tags = module->import_tags =
  3268. module->imports + module->import_function_count
  3269. + module->import_table_count + module->import_memory_count;
  3270. if (module->import_global_count)
  3271. import_globals = module->import_globals =
  3272. module->imports + module->import_function_count
  3273. + module->import_table_count + module->import_memory_count
  3274. + module->import_tag_count;
  3275. #else
  3276. if (module->import_global_count)
  3277. import_globals = module->import_globals =
  3278. module->imports + module->import_function_count
  3279. + module->import_table_count + module->import_memory_count;
  3280. #endif
  3281. p = p_old;
  3282. /* Scan again to resolve the data */
  3283. for (i = 0; i < import_count; i++) {
  3284. /* load module name */
  3285. read_leb_uint32(p, p_end, name_len);
  3286. CHECK_BUF(p, p_end, name_len);
  3287. if (!(sub_module_name = wasm_const_str_list_insert(
  3288. p, name_len, module, is_load_from_file_buf, error_buf,
  3289. error_buf_size))) {
  3290. return false;
  3291. }
  3292. p += name_len;
  3293. /* load field name */
  3294. read_leb_uint32(p, p_end, name_len);
  3295. CHECK_BUF(p, p_end, name_len);
  3296. if (!(field_name = wasm_const_str_list_insert(
  3297. p, name_len, module, is_load_from_file_buf, error_buf,
  3298. error_buf_size))) {
  3299. return false;
  3300. }
  3301. p += name_len;
  3302. CHECK_BUF(p, p_end, 1);
  3303. /* 0x00/0x01/0x02/0x03/0x4 */
  3304. kind = read_uint8(p);
  3305. switch (kind) {
  3306. case IMPORT_KIND_FUNC: /* import function */
  3307. bh_assert(import_functions);
  3308. import = import_functions++;
  3309. if (!load_function_import(&p, p_end, module,
  3310. sub_module_name, field_name,
  3311. &import->u.function, no_resolve,
  3312. error_buf, error_buf_size)) {
  3313. return false;
  3314. }
  3315. break;
  3316. case IMPORT_KIND_TABLE: /* import table */
  3317. bh_assert(import_tables);
  3318. import = import_tables++;
  3319. if (!load_table_import(&p, p_end, module, sub_module_name,
  3320. field_name, &import->u.table,
  3321. error_buf, error_buf_size)) {
  3322. LOG_DEBUG("can not import such a table (%s,%s)",
  3323. sub_module_name, field_name);
  3324. return false;
  3325. }
  3326. break;
  3327. case IMPORT_KIND_MEMORY: /* import memory */
  3328. bh_assert(import_memories);
  3329. import = import_memories++;
  3330. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3331. field_name, &import->u.memory,
  3332. error_buf, error_buf_size)) {
  3333. return false;
  3334. }
  3335. break;
  3336. #if WASM_ENABLE_TAGS != 0
  3337. case IMPORT_KIND_TAG:
  3338. bh_assert(import_tags);
  3339. import = import_tags++;
  3340. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3341. field_name, &import->u.tag, error_buf,
  3342. error_buf_size)) {
  3343. return false;
  3344. }
  3345. break;
  3346. #endif
  3347. case IMPORT_KIND_GLOBAL: /* import global */
  3348. bh_assert(import_globals);
  3349. import = import_globals++;
  3350. if (!load_global_import(&p, p_end, module, sub_module_name,
  3351. field_name, &import->u.global,
  3352. error_buf, error_buf_size)) {
  3353. return false;
  3354. }
  3355. break;
  3356. default:
  3357. set_error_buf(error_buf, error_buf_size,
  3358. "invalid import kind");
  3359. return false;
  3360. }
  3361. import->kind = kind;
  3362. import->u.names.module_name = sub_module_name;
  3363. import->u.names.field_name = field_name;
  3364. }
  3365. #if WASM_ENABLE_LIBC_WASI != 0
  3366. import = module->import_functions;
  3367. for (i = 0; i < module->import_function_count; i++, import++) {
  3368. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3369. || !strcmp(import->u.names.module_name,
  3370. "wasi_snapshot_preview1")) {
  3371. module->import_wasi_api = true;
  3372. break;
  3373. }
  3374. }
  3375. #endif
  3376. }
  3377. if (p != p_end) {
  3378. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3379. return false;
  3380. }
  3381. LOG_VERBOSE("Load import section success.\n");
  3382. (void)u8;
  3383. (void)u32;
  3384. (void)type_index;
  3385. return true;
  3386. fail:
  3387. return false;
  3388. }
  3389. static bool
  3390. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3391. uint32 error_buf_size)
  3392. {
  3393. WASMFuncType *param_type = func->func_type;
  3394. uint32 param_count = param_type->param_count;
  3395. uint8 *param_types = param_type->types;
  3396. uint32 local_count = func->local_count;
  3397. uint8 *local_types = func->local_types;
  3398. uint32 i, local_offset = 0;
  3399. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3400. /*
  3401. * Only allocate memory when total_size is not 0,
  3402. * or the return value of malloc(0) might be NULL on some platforms,
  3403. * which causes wasm loader return false.
  3404. */
  3405. if (total_size > 0
  3406. && !(func->local_offsets =
  3407. loader_malloc(total_size, error_buf, error_buf_size))) {
  3408. return false;
  3409. }
  3410. for (i = 0; i < param_count; i++) {
  3411. func->local_offsets[i] = (uint16)local_offset;
  3412. local_offset += wasm_value_type_cell_num(param_types[i]);
  3413. }
  3414. for (i = 0; i < local_count; i++) {
  3415. func->local_offsets[param_count + i] = (uint16)local_offset;
  3416. local_offset += wasm_value_type_cell_num(local_types[i]);
  3417. }
  3418. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3419. return true;
  3420. }
  3421. static bool
  3422. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3423. const uint8 *buf_code, const uint8 *buf_code_end,
  3424. WASMModule *module, char *error_buf,
  3425. uint32 error_buf_size)
  3426. {
  3427. const uint8 *p = buf, *p_end = buf_end;
  3428. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3429. uint32 func_count;
  3430. uint64 total_size;
  3431. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3432. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3433. uint8 type;
  3434. WASMFunction *func;
  3435. #if WASM_ENABLE_GC != 0
  3436. bool need_ref_type_map;
  3437. WASMRefType ref_type;
  3438. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3439. #endif
  3440. read_leb_uint32(p, p_end, func_count);
  3441. if (buf_code)
  3442. read_leb_uint32(p_code, buf_code_end, code_count);
  3443. if (func_count != code_count) {
  3444. set_error_buf(error_buf, error_buf_size,
  3445. "function and code section have inconsistent lengths or "
  3446. "unexpected end");
  3447. return false;
  3448. }
  3449. if (is_indices_overflow(module->import_function_count, func_count,
  3450. error_buf, error_buf_size))
  3451. return false;
  3452. if (func_count) {
  3453. module->function_count = func_count;
  3454. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3455. if (!(module->functions =
  3456. loader_malloc(total_size, error_buf, error_buf_size))) {
  3457. return false;
  3458. }
  3459. for (i = 0; i < func_count; i++) {
  3460. /* Resolve function type */
  3461. read_leb_uint32(p, p_end, type_index);
  3462. if (!check_function_type(module, type_index, error_buf,
  3463. error_buf_size)) {
  3464. return false;
  3465. }
  3466. #if WASM_ENABLE_GC != 0
  3467. type_index_org = type_index;
  3468. #endif
  3469. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3470. && WASM_ENABLE_GC == 0
  3471. type_index = wasm_get_smallest_type_idx(
  3472. module->types, module->type_count, type_index);
  3473. #endif
  3474. read_leb_uint32(p_code, buf_code_end, code_size);
  3475. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3476. set_error_buf(error_buf, error_buf_size,
  3477. "invalid function code size");
  3478. return false;
  3479. }
  3480. /* Resolve local set count */
  3481. p_code_end = p_code + code_size;
  3482. local_count = 0;
  3483. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3484. p_code_save = p_code;
  3485. #if WASM_ENABLE_GC != 0
  3486. ref_type_map_count = 0;
  3487. #endif
  3488. /* Calculate total local count */
  3489. for (j = 0; j < local_set_count; j++) {
  3490. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3491. if (sub_local_count > UINT32_MAX - local_count) {
  3492. set_error_buf(error_buf, error_buf_size, "too many locals");
  3493. return false;
  3494. }
  3495. #if WASM_ENABLE_GC == 0
  3496. CHECK_BUF(p_code, buf_code_end, 1);
  3497. /* 0x7F/0x7E/0x7D/0x7C */
  3498. type = read_uint8(p_code);
  3499. local_count += sub_local_count;
  3500. #if WASM_ENABLE_WAMR_COMPILER != 0
  3501. /* If any value's type is v128, mark the module as SIMD used */
  3502. if (type == VALUE_TYPE_V128)
  3503. module->is_simd_used = true;
  3504. #endif
  3505. #else
  3506. if (!resolve_value_type(&p_code, buf_code_end, module,
  3507. module->type_count, &need_ref_type_map,
  3508. &ref_type, false, error_buf,
  3509. error_buf_size)) {
  3510. return false;
  3511. }
  3512. local_count += sub_local_count;
  3513. if (need_ref_type_map)
  3514. ref_type_map_count += sub_local_count;
  3515. #endif
  3516. }
  3517. /* Code size in code entry can't be smaller than size of vec(locals)
  3518. * + expr(at least 1 for opcode end). And expressions are encoded by
  3519. * their instruction sequence terminated with an explicit 0x0B
  3520. * opcode for end. */
  3521. if (p_code_end <= p_code || *(p_code_end - 1) != WASM_OP_END) {
  3522. set_error_buf(
  3523. error_buf, error_buf_size,
  3524. "section size mismatch: function body END opcode expected");
  3525. return false;
  3526. }
  3527. /* Alloc memory, layout: function structure + local types */
  3528. code_size = (uint32)(p_code_end - p_code);
  3529. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3530. if (!(func = module->functions[i] =
  3531. loader_malloc(total_size, error_buf, error_buf_size))) {
  3532. return false;
  3533. }
  3534. #if WASM_ENABLE_GC != 0
  3535. if (ref_type_map_count > 0) {
  3536. total_size =
  3537. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3538. if (!(func->local_ref_type_maps = loader_malloc(
  3539. total_size, error_buf, error_buf_size))) {
  3540. return false;
  3541. }
  3542. func->local_ref_type_map_count = ref_type_map_count;
  3543. }
  3544. #endif
  3545. /* Set function type, local count, code size and code body */
  3546. func->func_type = (WASMFuncType *)module->types[type_index];
  3547. func->local_count = local_count;
  3548. if (local_count > 0)
  3549. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3550. func->code_size = code_size;
  3551. /*
  3552. * we shall make a copy of code body [p_code, p_code + code_size]
  3553. * when we are worrying about inappropriate releasing behaviour.
  3554. * all code bodies are actually in a buffer which user allocates in
  3555. * their embedding environment and we don't have power over them.
  3556. * it will be like:
  3557. * code_body_cp = malloc(code_size);
  3558. * memcpy(code_body_cp, p_code, code_size);
  3559. * func->code = code_body_cp;
  3560. */
  3561. func->code = (uint8 *)p_code;
  3562. #if WASM_ENABLE_GC != 0
  3563. func->type_idx = type_index_org;
  3564. #endif
  3565. #if WASM_ENABLE_GC != 0
  3566. t = 0;
  3567. #endif
  3568. /* Load each local type */
  3569. p_code = p_code_save;
  3570. local_type_index = 0;
  3571. for (j = 0; j < local_set_count; j++) {
  3572. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3573. /* Note: sub_local_count is allowed to be 0 */
  3574. if (local_type_index > UINT32_MAX - sub_local_count
  3575. || local_type_index + sub_local_count > local_count) {
  3576. set_error_buf(error_buf, error_buf_size,
  3577. "invalid local count");
  3578. return false;
  3579. }
  3580. #if WASM_ENABLE_GC == 0
  3581. CHECK_BUF(p_code, buf_code_end, 1);
  3582. /* 0x7F/0x7E/0x7D/0x7C */
  3583. type = read_uint8(p_code);
  3584. if (!is_valid_value_type_for_interpreter(type)) {
  3585. if (type == VALUE_TYPE_V128)
  3586. set_error_buf(error_buf, error_buf_size,
  3587. "v128 value type requires simd feature");
  3588. else if (type == VALUE_TYPE_FUNCREF
  3589. || type == VALUE_TYPE_EXTERNREF)
  3590. set_error_buf(error_buf, error_buf_size,
  3591. "ref value type requires "
  3592. "reference types feature");
  3593. else
  3594. set_error_buf_v(error_buf, error_buf_size,
  3595. "invalid local type 0x%02X", type);
  3596. return false;
  3597. }
  3598. #else
  3599. if (!resolve_value_type(&p_code, buf_code_end, module,
  3600. module->type_count, &need_ref_type_map,
  3601. &ref_type, false, error_buf,
  3602. error_buf_size)) {
  3603. return false;
  3604. }
  3605. if (need_ref_type_map) {
  3606. WASMRefType *ref_type_tmp;
  3607. if (!(ref_type_tmp = reftype_set_insert(
  3608. module->ref_type_set, &ref_type, error_buf,
  3609. error_buf_size))) {
  3610. return false;
  3611. }
  3612. for (k = 0; k < sub_local_count; k++) {
  3613. func->local_ref_type_maps[t + k].ref_type =
  3614. ref_type_tmp;
  3615. func->local_ref_type_maps[t + k].index =
  3616. local_type_index + k;
  3617. }
  3618. t += sub_local_count;
  3619. }
  3620. type = ref_type.ref_type;
  3621. #endif
  3622. for (k = 0; k < sub_local_count; k++) {
  3623. func->local_types[local_type_index++] = type;
  3624. }
  3625. #if WASM_ENABLE_WAMR_COMPILER != 0
  3626. if (type == VALUE_TYPE_V128)
  3627. module->is_simd_used = true;
  3628. else if (type == VALUE_TYPE_FUNCREF
  3629. || type == VALUE_TYPE_EXTERNREF)
  3630. module->is_ref_types_used = true;
  3631. #endif
  3632. }
  3633. bh_assert(local_type_index == func->local_count);
  3634. #if WASM_ENABLE_GC != 0
  3635. bh_assert(t == func->local_ref_type_map_count);
  3636. #if TRACE_WASM_LOADER != 0
  3637. os_printf("func %u, local types: [", i);
  3638. k = 0;
  3639. for (j = 0; j < func->local_count; j++) {
  3640. WASMRefType *ref_type_tmp = NULL;
  3641. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3642. bh_assert(j == func->local_ref_type_maps[k].index);
  3643. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3644. }
  3645. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3646. if (j < func->local_count - 1)
  3647. os_printf(" ");
  3648. }
  3649. os_printf("]\n");
  3650. #endif
  3651. #endif
  3652. func->param_cell_num = func->func_type->param_cell_num;
  3653. func->ret_cell_num = func->func_type->ret_cell_num;
  3654. local_cell_num =
  3655. wasm_get_cell_num(func->local_types, func->local_count);
  3656. if (local_cell_num > UINT16_MAX) {
  3657. set_error_buf(error_buf, error_buf_size,
  3658. "local count too large");
  3659. return false;
  3660. }
  3661. func->local_cell_num = (uint16)local_cell_num;
  3662. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3663. return false;
  3664. p_code = p_code_end;
  3665. }
  3666. }
  3667. if (p != p_end) {
  3668. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3669. return false;
  3670. }
  3671. LOG_VERBOSE("Load function section success.\n");
  3672. return true;
  3673. fail:
  3674. return false;
  3675. }
  3676. static bool
  3677. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3678. char *error_buf, uint32 error_buf_size)
  3679. {
  3680. const uint8 *p = buf, *p_end = buf_end;
  3681. uint32 table_count, i;
  3682. uint64 total_size;
  3683. WASMTable *table;
  3684. read_leb_uint32(p, p_end, table_count);
  3685. if (module->import_table_count + table_count > 1) {
  3686. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3687. /* a total of one table is allowed */
  3688. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3689. return false;
  3690. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3691. module->is_ref_types_used = true;
  3692. #endif
  3693. }
  3694. if (table_count) {
  3695. module->table_count = table_count;
  3696. total_size = sizeof(WASMTable) * (uint64)table_count;
  3697. if (!(module->tables =
  3698. loader_malloc(total_size, error_buf, error_buf_size))) {
  3699. return false;
  3700. }
  3701. /* load each table */
  3702. table = module->tables;
  3703. for (i = 0; i < table_count; i++, table++) {
  3704. #if WASM_ENABLE_GC != 0
  3705. uint8 flag;
  3706. bool has_init = false;
  3707. CHECK_BUF(p, p_end, 1);
  3708. flag = read_uint8(p);
  3709. if (flag == TABLE_INIT_EXPR_FLAG) {
  3710. CHECK_BUF(p, p_end, 1);
  3711. flag = read_uint8(p);
  3712. if (flag != 0x00) {
  3713. set_error_buf(error_buf, error_buf_size,
  3714. "invalid leading bytes for table");
  3715. return false;
  3716. }
  3717. has_init = true;
  3718. }
  3719. else {
  3720. p--;
  3721. }
  3722. #endif /* end of WASM_ENABLE_GC != 0 */
  3723. if (!load_table(&p, p_end, module, table, error_buf,
  3724. error_buf_size))
  3725. return false;
  3726. #if WASM_ENABLE_GC != 0
  3727. if (has_init) {
  3728. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3729. table->table_type.elem_type,
  3730. table->table_type.elem_ref_type, error_buf,
  3731. error_buf_size))
  3732. return false;
  3733. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3734. && table->init_expr.init_expr_type
  3735. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3736. set_error_buf(
  3737. error_buf, error_buf_size,
  3738. "unsupported initializer expression for table");
  3739. return false;
  3740. }
  3741. }
  3742. else {
  3743. if (wasm_is_reftype_htref_non_nullable(
  3744. table->table_type.elem_type)) {
  3745. set_error_buf(
  3746. error_buf, error_buf_size,
  3747. "type mismatch: non-nullable table without init expr");
  3748. return false;
  3749. }
  3750. }
  3751. #endif /* end of WASM_ENABLE_GC != 0 */
  3752. #if WASM_ENABLE_WAMR_COMPILER != 0
  3753. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3754. module->is_ref_types_used = true;
  3755. #endif
  3756. }
  3757. }
  3758. if (p != p_end) {
  3759. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3760. return false;
  3761. }
  3762. LOG_VERBOSE("Load table section success.\n");
  3763. return true;
  3764. fail:
  3765. return false;
  3766. }
  3767. static bool
  3768. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3769. char *error_buf, uint32 error_buf_size)
  3770. {
  3771. const uint8 *p = buf, *p_end = buf_end;
  3772. uint32 memory_count, i;
  3773. uint64 total_size;
  3774. WASMMemory *memory;
  3775. read_leb_uint32(p, p_end, memory_count);
  3776. #if WASM_ENABLE_MULTI_MEMORY == 0
  3777. /* a total of one memory is allowed */
  3778. if (module->import_memory_count + memory_count > 1) {
  3779. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3780. return false;
  3781. }
  3782. #endif
  3783. if (memory_count) {
  3784. module->memory_count = memory_count;
  3785. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3786. if (!(module->memories =
  3787. loader_malloc(total_size, error_buf, error_buf_size))) {
  3788. return false;
  3789. }
  3790. /* load each memory */
  3791. memory = module->memories;
  3792. for (i = 0; i < memory_count; i++, memory++)
  3793. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3794. return false;
  3795. }
  3796. if (p != p_end) {
  3797. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3798. return false;
  3799. }
  3800. LOG_VERBOSE("Load memory section success.\n");
  3801. return true;
  3802. fail:
  3803. return false;
  3804. }
  3805. static bool
  3806. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3807. char *error_buf, uint32 error_buf_size)
  3808. {
  3809. const uint8 *p = buf, *p_end = buf_end;
  3810. uint32 global_count, i;
  3811. uint64 total_size;
  3812. WASMGlobal *global;
  3813. uint8 mutable;
  3814. #if WASM_ENABLE_GC != 0
  3815. bool need_ref_type_map;
  3816. WASMRefType ref_type;
  3817. #endif
  3818. read_leb_uint32(p, p_end, global_count);
  3819. if (is_indices_overflow(module->import_global_count, global_count,
  3820. error_buf, error_buf_size))
  3821. return false;
  3822. module->global_count = 0;
  3823. if (global_count) {
  3824. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3825. if (!(module->globals =
  3826. loader_malloc(total_size, error_buf, error_buf_size))) {
  3827. return false;
  3828. }
  3829. global = module->globals;
  3830. for (i = 0; i < global_count; i++, global++) {
  3831. #if WASM_ENABLE_GC == 0
  3832. CHECK_BUF(p, p_end, 2);
  3833. /* global type */
  3834. global->type.val_type = read_uint8(p);
  3835. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3836. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3837. return false;
  3838. }
  3839. mutable = read_uint8(p);
  3840. #else
  3841. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3842. &need_ref_type_map, &ref_type, false,
  3843. error_buf, error_buf_size)) {
  3844. return false;
  3845. }
  3846. global->type.val_type = ref_type.ref_type;
  3847. CHECK_BUF(p, p_end, 1);
  3848. mutable = read_uint8(p);
  3849. #endif /* end of WASM_ENABLE_GC */
  3850. #if WASM_ENABLE_WAMR_COMPILER != 0
  3851. if (global->type.val_type == VALUE_TYPE_V128)
  3852. module->is_simd_used = true;
  3853. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3854. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3855. module->is_ref_types_used = true;
  3856. #endif
  3857. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3858. return false;
  3859. }
  3860. global->type.is_mutable = mutable ? true : false;
  3861. /* initialize expression */
  3862. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3863. global->type.val_type,
  3864. #if WASM_ENABLE_GC == 0
  3865. NULL,
  3866. #else
  3867. &ref_type,
  3868. #endif
  3869. error_buf, error_buf_size))
  3870. return false;
  3871. #if WASM_ENABLE_GC != 0
  3872. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3873. uint8 global_type;
  3874. WASMRefType *global_ref_type;
  3875. uint32 global_idx = global->init_expr.u.unary.v.global_index;
  3876. if (global->init_expr.u.unary.v.global_index
  3877. >= module->import_global_count + i) {
  3878. set_error_buf(error_buf, error_buf_size, "unknown global");
  3879. return false;
  3880. }
  3881. if (global_idx < module->import_global_count) {
  3882. global_type = module->import_globals[global_idx]
  3883. .u.global.type.val_type;
  3884. global_ref_type =
  3885. module->import_globals[global_idx].u.global.ref_type;
  3886. }
  3887. else {
  3888. global_type =
  3889. module
  3890. ->globals[global_idx - module->import_global_count]
  3891. .type.val_type;
  3892. global_ref_type =
  3893. module
  3894. ->globals[global_idx - module->import_global_count]
  3895. .ref_type;
  3896. }
  3897. if (!wasm_reftype_is_subtype_of(
  3898. global_type, global_ref_type, global->type.val_type,
  3899. global->ref_type, module->types, module->type_count)) {
  3900. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3901. return false;
  3902. }
  3903. }
  3904. if (need_ref_type_map) {
  3905. if (!(global->ref_type =
  3906. reftype_set_insert(module->ref_type_set, &ref_type,
  3907. error_buf, error_buf_size))) {
  3908. return false;
  3909. }
  3910. }
  3911. #if TRACE_WASM_LOADER != 0
  3912. os_printf("global type: ");
  3913. wasm_dump_value_type(global->type, global->ref_type);
  3914. os_printf("\n");
  3915. #endif
  3916. #endif
  3917. module->global_count++;
  3918. }
  3919. bh_assert(module->global_count == global_count);
  3920. }
  3921. if (p != p_end) {
  3922. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3923. return false;
  3924. }
  3925. LOG_VERBOSE("Load global section success.\n");
  3926. return true;
  3927. fail:
  3928. return false;
  3929. }
  3930. static bool
  3931. check_duplicate_exports(WASMModule *module, char *error_buf,
  3932. uint32 error_buf_size)
  3933. {
  3934. uint32 i;
  3935. bool result = false;
  3936. char *names_buf[32], **names = names_buf;
  3937. if (module->export_count > 32) {
  3938. names = loader_malloc(module->export_count * sizeof(char *), error_buf,
  3939. error_buf_size);
  3940. if (!names) {
  3941. return result;
  3942. }
  3943. }
  3944. for (i = 0; i < module->export_count; i++) {
  3945. names[i] = module->exports[i].name;
  3946. }
  3947. qsort(names, module->export_count, sizeof(char *), cmp_export_name);
  3948. for (i = 1; i < module->export_count; i++) {
  3949. if (!strcmp(names[i], names[i - 1])) {
  3950. set_error_buf(error_buf, error_buf_size, "duplicate export name");
  3951. goto cleanup;
  3952. }
  3953. }
  3954. result = true;
  3955. cleanup:
  3956. if (module->export_count > 32) {
  3957. wasm_runtime_free(names);
  3958. }
  3959. return result;
  3960. }
  3961. static bool
  3962. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3963. bool is_load_from_file_buf, char *error_buf,
  3964. uint32 error_buf_size)
  3965. {
  3966. const uint8 *p = buf, *p_end = buf_end;
  3967. uint32 export_count, i, index;
  3968. uint64 total_size;
  3969. uint32 str_len;
  3970. WASMExport *export;
  3971. read_leb_uint32(p, p_end, export_count);
  3972. if (export_count) {
  3973. module->export_count = export_count;
  3974. total_size = sizeof(WASMExport) * (uint64)export_count;
  3975. if (!(module->exports =
  3976. loader_malloc(total_size, error_buf, error_buf_size))) {
  3977. return false;
  3978. }
  3979. export = module->exports;
  3980. for (i = 0; i < export_count; i++, export ++) {
  3981. #if WASM_ENABLE_THREAD_MGR == 0
  3982. if (p == p_end) {
  3983. /* export section with inconsistent count:
  3984. n export declared, but less than n given */
  3985. set_error_buf(error_buf, error_buf_size,
  3986. "length out of bounds");
  3987. return false;
  3988. }
  3989. #endif
  3990. read_leb_uint32(p, p_end, str_len);
  3991. CHECK_BUF(p, p_end, str_len);
  3992. if (!(export->name = wasm_const_str_list_insert(
  3993. p, str_len, module, is_load_from_file_buf, error_buf,
  3994. error_buf_size))) {
  3995. return false;
  3996. }
  3997. p += str_len;
  3998. CHECK_BUF(p, p_end, 1);
  3999. export->kind = read_uint8(p);
  4000. read_leb_uint32(p, p_end, index);
  4001. export->index = index;
  4002. switch (export->kind) {
  4003. /* function index */
  4004. case EXPORT_KIND_FUNC:
  4005. if (index >= module->function_count
  4006. + module->import_function_count) {
  4007. set_error_buf(error_buf, error_buf_size,
  4008. "unknown function");
  4009. return false;
  4010. }
  4011. #if WASM_ENABLE_SIMD != 0
  4012. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  4013. || (WASM_ENABLE_FAST_INTERP != 0)
  4014. /* TODO: check func type, if it has v128 param or result,
  4015. report error */
  4016. #endif
  4017. #endif
  4018. break;
  4019. /* table index */
  4020. case EXPORT_KIND_TABLE:
  4021. if (index
  4022. >= module->table_count + module->import_table_count) {
  4023. set_error_buf(error_buf, error_buf_size,
  4024. "unknown table");
  4025. return false;
  4026. }
  4027. break;
  4028. /* memory index */
  4029. case EXPORT_KIND_MEMORY:
  4030. if (index
  4031. >= module->memory_count + module->import_memory_count) {
  4032. set_error_buf(error_buf, error_buf_size,
  4033. "unknown memory");
  4034. return false;
  4035. }
  4036. break;
  4037. #if WASM_ENABLE_TAGS != 0
  4038. /* export tag */
  4039. case EXPORT_KIND_TAG:
  4040. if (index >= module->tag_count + module->import_tag_count) {
  4041. set_error_buf(error_buf, error_buf_size, "unknown tag");
  4042. return false;
  4043. }
  4044. break;
  4045. #endif
  4046. /* global index */
  4047. case EXPORT_KIND_GLOBAL:
  4048. if (index
  4049. >= module->global_count + module->import_global_count) {
  4050. set_error_buf(error_buf, error_buf_size,
  4051. "unknown global");
  4052. return false;
  4053. }
  4054. break;
  4055. default:
  4056. set_error_buf(error_buf, error_buf_size,
  4057. "invalid export kind");
  4058. return false;
  4059. }
  4060. }
  4061. if (!check_duplicate_exports(module, error_buf, error_buf_size)) {
  4062. return false;
  4063. }
  4064. }
  4065. if (p != p_end) {
  4066. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4067. return false;
  4068. }
  4069. LOG_VERBOSE("Load export section success.\n");
  4070. return true;
  4071. fail:
  4072. return false;
  4073. }
  4074. static bool
  4075. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  4076. uint32 error_buf_size)
  4077. {
  4078. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  4079. if (table_index != 0) {
  4080. set_error_buf(
  4081. error_buf, error_buf_size,
  4082. "zero byte expected. The module uses reference types feature "
  4083. "which is disabled in the runtime.");
  4084. return false;
  4085. }
  4086. #endif
  4087. if (table_index >= module->import_table_count + module->table_count) {
  4088. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  4089. table_index);
  4090. return false;
  4091. }
  4092. return true;
  4093. }
  4094. static bool
  4095. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  4096. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  4097. {
  4098. const uint8 *p = *p_buf, *p_end = buf_end;
  4099. uint32 table_index;
  4100. read_leb_uint32(p, p_end, table_index);
  4101. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  4102. return false;
  4103. }
  4104. *p_table_index = table_index;
  4105. *p_buf = p;
  4106. return true;
  4107. fail:
  4108. return false;
  4109. }
  4110. /* Element segments must match element type of table */
  4111. static bool
  4112. check_table_elem_type(WASMModule *module, uint32 table_index,
  4113. uint32 type_from_elem_seg, char *error_buf,
  4114. uint32 error_buf_size)
  4115. {
  4116. uint32 table_declared_elem_type;
  4117. if (table_index < module->import_table_count)
  4118. table_declared_elem_type =
  4119. module->import_tables[table_index].u.table.table_type.elem_type;
  4120. else
  4121. table_declared_elem_type =
  4122. module->tables[table_index - module->import_table_count]
  4123. .table_type.elem_type;
  4124. if (table_declared_elem_type == type_from_elem_seg)
  4125. return true;
  4126. #if WASM_ENABLE_GC != 0
  4127. /*
  4128. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  4129. */
  4130. if (table_declared_elem_type == REF_TYPE_ANYREF)
  4131. return true;
  4132. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  4133. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  4134. return true;
  4135. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  4136. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  4137. return true;
  4138. #endif
  4139. set_error_buf(error_buf, error_buf_size, "type mismatch");
  4140. return false;
  4141. }
  4142. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4143. static bool
  4144. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  4145. uint32 *p_elem_type,
  4146. #if WASM_ENABLE_GC != 0
  4147. WASMRefType **p_elem_ref_type,
  4148. #endif
  4149. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  4150. {
  4151. const uint8 *p = *p_buf, *p_end = buf_end;
  4152. uint8 elem_type;
  4153. #if WASM_ENABLE_GC != 0
  4154. WASMRefType elem_ref_type;
  4155. bool need_ref_type_map;
  4156. #endif
  4157. CHECK_BUF(p, p_end, 1);
  4158. elem_type = read_uint8(p);
  4159. if (elemkind_zero) {
  4160. if (elem_type != 0) {
  4161. set_error_buf(error_buf, error_buf_size,
  4162. "invalid reference type or unknown type");
  4163. return false;
  4164. }
  4165. else {
  4166. *p_elem_type = VALUE_TYPE_FUNCREF;
  4167. *p_buf = p;
  4168. return true;
  4169. }
  4170. }
  4171. #if WASM_ENABLE_GC == 0
  4172. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  4173. set_error_buf(error_buf, error_buf_size,
  4174. "invalid reference type or unknown type");
  4175. return false;
  4176. }
  4177. *p_elem_type = elem_type;
  4178. #else
  4179. p--;
  4180. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  4181. module->type_count, &need_ref_type_map,
  4182. &elem_ref_type, false, error_buf, error_buf_size)) {
  4183. return false;
  4184. }
  4185. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  4186. set_error_buf(error_buf, error_buf_size,
  4187. "invalid reference type or unknown type");
  4188. return false;
  4189. }
  4190. *p_elem_type = elem_ref_type.ref_type;
  4191. if (need_ref_type_map) {
  4192. if (!(*p_elem_ref_type =
  4193. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  4194. error_buf, error_buf_size))) {
  4195. return false;
  4196. }
  4197. }
  4198. #endif
  4199. *p_buf = p;
  4200. return true;
  4201. fail:
  4202. return false;
  4203. }
  4204. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4205. static bool
  4206. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  4207. WASMModule *module, WASMTableSeg *table_segment,
  4208. char *error_buf, uint32 error_buf_size)
  4209. {
  4210. const uint8 *p = *p_buf, *p_end = buf_end;
  4211. uint32 function_count, function_index = 0, i;
  4212. uint64 total_size;
  4213. read_leb_uint32(p, p_end, function_count);
  4214. table_segment->value_count = function_count;
  4215. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  4216. if (total_size > 0
  4217. && !(table_segment->init_values =
  4218. (InitializerExpression *)loader_malloc(total_size, error_buf,
  4219. error_buf_size))) {
  4220. return false;
  4221. }
  4222. for (i = 0; i < function_count; i++) {
  4223. InitializerExpression *init_expr = &table_segment->init_values[i];
  4224. read_leb_uint32(p, p_end, function_index);
  4225. if (!check_function_index(module, function_index, error_buf,
  4226. error_buf_size)) {
  4227. return false;
  4228. }
  4229. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  4230. init_expr->u.unary.v.ref_index = function_index;
  4231. }
  4232. *p_buf = p;
  4233. return true;
  4234. fail:
  4235. return false;
  4236. }
  4237. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  4238. static bool
  4239. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  4240. WASMModule *module, WASMTableSeg *table_segment,
  4241. char *error_buf, uint32 error_buf_size)
  4242. {
  4243. const uint8 *p = *p_buf, *p_end = buf_end;
  4244. uint32 ref_count, i;
  4245. uint64 total_size;
  4246. read_leb_uint32(p, p_end, ref_count);
  4247. table_segment->value_count = ref_count;
  4248. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  4249. if (total_size > 0
  4250. && !(table_segment->init_values =
  4251. (InitializerExpression *)loader_malloc(total_size, error_buf,
  4252. error_buf_size))) {
  4253. return false;
  4254. }
  4255. for (i = 0; i < ref_count; i++) {
  4256. InitializerExpression *init_expr = &table_segment->init_values[i];
  4257. if (!load_init_expr(module, &p, p_end, init_expr,
  4258. table_segment->elem_type,
  4259. #if WASM_ENABLE_GC == 0
  4260. NULL,
  4261. #else
  4262. table_segment->elem_ref_type,
  4263. #endif
  4264. error_buf, error_buf_size))
  4265. return false;
  4266. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  4267. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  4268. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  4269. && init_expr->init_expr_type
  4270. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  4271. }
  4272. *p_buf = p;
  4273. return true;
  4274. fail:
  4275. return false;
  4276. }
  4277. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  4278. static bool
  4279. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  4280. WASMModule *module, char *error_buf,
  4281. uint32 error_buf_size)
  4282. {
  4283. const uint8 *p = buf, *p_end = buf_end;
  4284. uint8 table_elem_idx_type;
  4285. uint32 table_segment_count, i;
  4286. uint64 total_size;
  4287. WASMTableSeg *table_segment;
  4288. read_leb_uint32(p, p_end, table_segment_count);
  4289. if (table_segment_count) {
  4290. module->table_seg_count = table_segment_count;
  4291. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  4292. if (!(module->table_segments =
  4293. loader_malloc(total_size, error_buf, error_buf_size))) {
  4294. return false;
  4295. }
  4296. table_segment = module->table_segments;
  4297. for (i = 0; i < table_segment_count; i++, table_segment++) {
  4298. if (p >= p_end) {
  4299. set_error_buf(error_buf, error_buf_size,
  4300. "invalid value type or "
  4301. "invalid elements segment kind");
  4302. return false;
  4303. }
  4304. table_elem_idx_type = VALUE_TYPE_I32;
  4305. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4306. read_leb_uint32(p, p_end, table_segment->mode);
  4307. /* last three bits */
  4308. table_segment->mode = table_segment->mode & 0x07;
  4309. switch (table_segment->mode) {
  4310. /* elemkind/elemtype + active */
  4311. case 0:
  4312. case 4:
  4313. {
  4314. #if WASM_ENABLE_GC != 0
  4315. if (table_segment->mode == 0) {
  4316. /* vec(funcidx), set elem type to (ref func) */
  4317. WASMRefType elem_ref_type = { 0 };
  4318. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4319. wasm_set_refheaptype_common(
  4320. &elem_ref_type.ref_ht_common, false,
  4321. HEAP_TYPE_FUNC);
  4322. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4323. module->ref_type_set, &elem_ref_type,
  4324. error_buf, error_buf_size)))
  4325. return false;
  4326. }
  4327. else {
  4328. /* vec(expr), set elem type to funcref */
  4329. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4330. }
  4331. #else
  4332. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4333. #endif
  4334. table_segment->table_index = 0;
  4335. if (!check_table_index(module, table_segment->table_index,
  4336. error_buf, error_buf_size))
  4337. return false;
  4338. #if WASM_ENABLE_MEMORY64 != 0
  4339. table_elem_idx_type =
  4340. is_table_64bit(module, table_segment->table_index)
  4341. ? VALUE_TYPE_I64
  4342. : VALUE_TYPE_I32;
  4343. #endif
  4344. if (!load_init_expr(module, &p, p_end,
  4345. &table_segment->base_offset,
  4346. table_elem_idx_type, NULL, error_buf,
  4347. error_buf_size))
  4348. return false;
  4349. if (table_segment->mode == 0) {
  4350. /* vec(funcidx) */
  4351. if (!load_func_index_vec(&p, p_end, module,
  4352. table_segment, error_buf,
  4353. error_buf_size))
  4354. return false;
  4355. }
  4356. else {
  4357. /* vec(expr) */
  4358. if (!load_init_expr_vec(&p, p_end, module,
  4359. table_segment, error_buf,
  4360. error_buf_size))
  4361. return false;
  4362. }
  4363. if (!check_table_elem_type(module,
  4364. table_segment->table_index,
  4365. table_segment->elem_type,
  4366. error_buf, error_buf_size))
  4367. return false;
  4368. break;
  4369. }
  4370. /* elemkind + passive/declarative */
  4371. case 1:
  4372. case 3:
  4373. if (!load_elem_type(module, &p, p_end,
  4374. &table_segment->elem_type,
  4375. #if WASM_ENABLE_GC != 0
  4376. &table_segment->elem_ref_type,
  4377. #endif
  4378. true, error_buf, error_buf_size))
  4379. return false;
  4380. /* vec(funcidx) */
  4381. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4382. error_buf, error_buf_size))
  4383. return false;
  4384. break;
  4385. /* elemkind/elemtype + table_idx + active */
  4386. case 2:
  4387. case 6:
  4388. if (!load_table_index(&p, p_end, module,
  4389. &table_segment->table_index,
  4390. error_buf, error_buf_size))
  4391. return false;
  4392. #if WASM_ENABLE_MEMORY64 != 0
  4393. table_elem_idx_type =
  4394. is_table_64bit(module, table_segment->table_index)
  4395. ? VALUE_TYPE_I64
  4396. : VALUE_TYPE_I32;
  4397. #endif
  4398. if (!load_init_expr(module, &p, p_end,
  4399. &table_segment->base_offset,
  4400. table_elem_idx_type, NULL, error_buf,
  4401. error_buf_size))
  4402. return false;
  4403. if (!load_elem_type(module, &p, p_end,
  4404. &table_segment->elem_type,
  4405. #if WASM_ENABLE_GC != 0
  4406. &table_segment->elem_ref_type,
  4407. #endif
  4408. table_segment->mode == 2 ? true : false,
  4409. error_buf, error_buf_size))
  4410. return false;
  4411. if (table_segment->mode == 2) {
  4412. /* vec(funcidx) */
  4413. if (!load_func_index_vec(&p, p_end, module,
  4414. table_segment, error_buf,
  4415. error_buf_size))
  4416. return false;
  4417. }
  4418. else {
  4419. /* vec(expr) */
  4420. if (!load_init_expr_vec(&p, p_end, module,
  4421. table_segment, error_buf,
  4422. error_buf_size))
  4423. return false;
  4424. }
  4425. if (!check_table_elem_type(module,
  4426. table_segment->table_index,
  4427. table_segment->elem_type,
  4428. error_buf, error_buf_size))
  4429. return false;
  4430. break;
  4431. case 5:
  4432. case 7:
  4433. if (!load_elem_type(module, &p, p_end,
  4434. &table_segment->elem_type,
  4435. #if WASM_ENABLE_GC != 0
  4436. &table_segment->elem_ref_type,
  4437. #endif
  4438. false, error_buf, error_buf_size))
  4439. return false;
  4440. /* vec(expr) */
  4441. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4442. error_buf, error_buf_size))
  4443. return false;
  4444. break;
  4445. default:
  4446. set_error_buf(error_buf, error_buf_size,
  4447. "unknown element segment kind");
  4448. return false;
  4449. }
  4450. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4451. /*
  4452. * like: 00 41 05 0b 04 00 01 00 01
  4453. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4454. */
  4455. if (!load_table_index(&p, p_end, module,
  4456. &table_segment->table_index, error_buf,
  4457. error_buf_size))
  4458. return false;
  4459. #if WASM_ENABLE_MEMORY64 != 0
  4460. table_elem_idx_type =
  4461. is_table_64bit(module, table_segment->table_index)
  4462. ? VALUE_TYPE_I64
  4463. : VALUE_TYPE_I32;
  4464. #endif
  4465. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4466. table_elem_idx_type, NULL, error_buf,
  4467. error_buf_size))
  4468. return false;
  4469. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4470. error_buf, error_buf_size))
  4471. return false;
  4472. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4473. if (!check_table_elem_type(module, table_segment->table_index,
  4474. table_segment->elem_type, error_buf,
  4475. error_buf_size))
  4476. return false;
  4477. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4478. #if WASM_ENABLE_MEMORY64 != 0
  4479. if (table_elem_idx_type == VALUE_TYPE_I64
  4480. && table_segment->base_offset.u.unary.v.u64 > UINT32_MAX) {
  4481. set_error_buf(error_buf, error_buf_size,
  4482. "In table64, table base offset can't be "
  4483. "larger than UINT32_MAX");
  4484. return false;
  4485. }
  4486. #endif
  4487. #if WASM_ENABLE_WAMR_COMPILER != 0
  4488. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4489. module->is_ref_types_used = true;
  4490. #endif
  4491. }
  4492. }
  4493. if (p != p_end) {
  4494. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4495. return false;
  4496. }
  4497. LOG_VERBOSE("Load table segment section success.\n");
  4498. return true;
  4499. fail:
  4500. return false;
  4501. }
  4502. #if WASM_ENABLE_BULK_MEMORY != 0
  4503. static bool
  4504. check_data_count_consistency(bool has_datacount_section, int datacount_len,
  4505. int data_seg_len, char *error_buf,
  4506. uint32 error_buf_size)
  4507. {
  4508. if (has_datacount_section && datacount_len != data_seg_len) {
  4509. set_error_buf(error_buf, error_buf_size,
  4510. "data count and data section have inconsistent lengths");
  4511. return false;
  4512. }
  4513. return true;
  4514. }
  4515. #endif
  4516. static bool
  4517. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4518. WASMModule *module,
  4519. #if WASM_ENABLE_BULK_MEMORY != 0
  4520. bool has_datacount_section,
  4521. #endif
  4522. bool clone_data_seg, char *error_buf,
  4523. uint32 error_buf_size)
  4524. {
  4525. const uint8 *p = buf, *p_end = buf_end;
  4526. uint32 data_seg_count, i, mem_index, data_seg_len;
  4527. uint64 total_size;
  4528. WASMDataSeg *dataseg;
  4529. InitializerExpression init_expr;
  4530. #if WASM_ENABLE_BULK_MEMORY != 0
  4531. bool is_passive = false;
  4532. uint32 mem_flag;
  4533. #endif
  4534. uint8 mem_offset_type = VALUE_TYPE_I32;
  4535. read_leb_uint32(p, p_end, data_seg_count);
  4536. #if WASM_ENABLE_BULK_MEMORY != 0
  4537. if (!check_data_count_consistency(has_datacount_section,
  4538. module->data_seg_count1, data_seg_count,
  4539. error_buf, error_buf_size)) {
  4540. return false;
  4541. }
  4542. #endif
  4543. if (data_seg_count) {
  4544. module->data_seg_count = data_seg_count;
  4545. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4546. if (!(module->data_segments =
  4547. loader_malloc(total_size, error_buf, error_buf_size))) {
  4548. return false;
  4549. }
  4550. for (i = 0; i < data_seg_count; i++) {
  4551. read_leb_uint32(p, p_end, mem_index);
  4552. #if WASM_ENABLE_BULK_MEMORY != 0
  4553. is_passive = false;
  4554. mem_flag = mem_index & 0x03;
  4555. switch (mem_flag) {
  4556. case 0x01:
  4557. is_passive = true;
  4558. #if WASM_ENABLE_WAMR_COMPILER != 0
  4559. module->is_bulk_memory_used = true;
  4560. #endif
  4561. break;
  4562. case 0x00:
  4563. /* no memory index, treat index as 0 */
  4564. mem_index = 0;
  4565. goto check_mem_index;
  4566. case 0x02:
  4567. /* read following memory index */
  4568. read_leb_uint32(p, p_end, mem_index);
  4569. #if WASM_ENABLE_WAMR_COMPILER != 0
  4570. module->is_bulk_memory_used = true;
  4571. #endif
  4572. check_mem_index:
  4573. if (mem_index
  4574. >= module->import_memory_count + module->memory_count) {
  4575. set_error_buf_v(error_buf, error_buf_size,
  4576. "unknown memory %d", mem_index);
  4577. return false;
  4578. }
  4579. break;
  4580. case 0x03:
  4581. default:
  4582. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4583. return false;
  4584. break;
  4585. }
  4586. #else
  4587. if (mem_index
  4588. >= module->import_memory_count + module->memory_count) {
  4589. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4590. mem_index);
  4591. return false;
  4592. }
  4593. #endif /* WASM_ENABLE_BULK_MEMORY */
  4594. #if WASM_ENABLE_BULK_MEMORY != 0
  4595. if (!is_passive)
  4596. #endif
  4597. {
  4598. #if WASM_ENABLE_MEMORY64 != 0
  4599. /* This memory_flag is from memory instead of data segment */
  4600. uint8 memory_flag;
  4601. if (module->import_memory_count > 0) {
  4602. memory_flag = module->import_memories[mem_index]
  4603. .u.memory.mem_type.flags;
  4604. }
  4605. else {
  4606. memory_flag =
  4607. module
  4608. ->memories[mem_index - module->import_memory_count]
  4609. .flags;
  4610. }
  4611. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4612. : VALUE_TYPE_I32;
  4613. #else
  4614. mem_offset_type = VALUE_TYPE_I32;
  4615. #endif
  4616. }
  4617. #if WASM_ENABLE_BULK_MEMORY != 0
  4618. if (!is_passive)
  4619. #endif
  4620. if (!load_init_expr(module, &p, p_end, &init_expr,
  4621. mem_offset_type, NULL, error_buf,
  4622. error_buf_size))
  4623. return false;
  4624. read_leb_uint32(p, p_end, data_seg_len);
  4625. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4626. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4627. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  4628. destroy_init_expr(module, &init_expr);
  4629. #endif
  4630. return false;
  4631. }
  4632. #if WASM_ENABLE_BULK_MEMORY != 0
  4633. dataseg->is_passive = is_passive;
  4634. if (!is_passive)
  4635. #endif
  4636. {
  4637. bh_memcpy_s(&dataseg->base_offset,
  4638. sizeof(InitializerExpression), &init_expr,
  4639. sizeof(InitializerExpression));
  4640. dataseg->memory_index = mem_index;
  4641. }
  4642. dataseg->data_length = data_seg_len;
  4643. CHECK_BUF(p, p_end, data_seg_len);
  4644. if (clone_data_seg) {
  4645. if (!(dataseg->data = loader_malloc(
  4646. dataseg->data_length, error_buf, error_buf_size))) {
  4647. return false;
  4648. }
  4649. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4650. data_seg_len);
  4651. }
  4652. else {
  4653. dataseg->data = (uint8 *)p;
  4654. }
  4655. dataseg->is_data_cloned = clone_data_seg;
  4656. p += data_seg_len;
  4657. }
  4658. }
  4659. if (p != p_end) {
  4660. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4661. return false;
  4662. }
  4663. LOG_VERBOSE("Load data segment section success.\n");
  4664. return true;
  4665. fail:
  4666. return false;
  4667. }
  4668. #if WASM_ENABLE_BULK_MEMORY != 0
  4669. static bool
  4670. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4671. WASMModule *module, char *error_buf,
  4672. uint32 error_buf_size)
  4673. {
  4674. const uint8 *p = buf, *p_end = buf_end;
  4675. uint32 data_seg_count1 = 0;
  4676. read_leb_uint32(p, p_end, data_seg_count1);
  4677. module->data_seg_count1 = data_seg_count1;
  4678. if (p != p_end) {
  4679. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4680. return false;
  4681. }
  4682. #if WASM_ENABLE_WAMR_COMPILER != 0
  4683. module->is_bulk_memory_used = true;
  4684. #endif
  4685. LOG_VERBOSE("Load datacount section success.\n");
  4686. return true;
  4687. fail:
  4688. return false;
  4689. }
  4690. #endif
  4691. #if WASM_ENABLE_TAGS != 0
  4692. static bool
  4693. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4694. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4695. uint32 error_buf_size)
  4696. {
  4697. (void)buf_code;
  4698. (void)buf_code_end;
  4699. const uint8 *p = buf, *p_end = buf_end;
  4700. size_t total_size = 0;
  4701. /* number of tags defined in the section */
  4702. uint32 section_tag_count = 0;
  4703. uint8 tag_attribute;
  4704. uint32 tag_type;
  4705. WASMTag *tag = NULL;
  4706. /* get tag count */
  4707. read_leb_uint32(p, p_end, section_tag_count);
  4708. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4709. error_buf, error_buf_size))
  4710. return false;
  4711. module->tag_count = section_tag_count;
  4712. if (section_tag_count) {
  4713. total_size = sizeof(WASMTag *) * module->tag_count;
  4714. if (!(module->tags =
  4715. loader_malloc(total_size, error_buf, error_buf_size))) {
  4716. return false;
  4717. }
  4718. /* load each tag, imported tags precede the tags */
  4719. uint32 tag_index;
  4720. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4721. /* get the one byte attribute */
  4722. CHECK_BUF(p, p_end, 1);
  4723. tag_attribute = read_uint8(p);
  4724. /* get type */
  4725. read_leb_uint32(p, p_end, tag_type);
  4726. /* compare against module->types */
  4727. if (!check_function_type(module, tag_type, error_buf,
  4728. error_buf_size)) {
  4729. return false;
  4730. }
  4731. /* get return type (must be 0) */
  4732. /* check, that the type of the referred tag returns void */
  4733. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4734. if (func_type->result_count != 0) {
  4735. set_error_buf(error_buf, error_buf_size,
  4736. "non-empty tag result type");
  4737. goto fail;
  4738. }
  4739. if (!(tag = module->tags[tag_index] = loader_malloc(
  4740. sizeof(WASMTag), error_buf, error_buf_size))) {
  4741. return false;
  4742. }
  4743. /* store to module tag declarations */
  4744. tag->attribute = tag_attribute;
  4745. tag->type = tag_type;
  4746. tag->tag_type = func_type;
  4747. }
  4748. }
  4749. if (p != p_end) {
  4750. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4751. return false;
  4752. }
  4753. LOG_VERBOSE("Load tag section success.\n");
  4754. return true;
  4755. fail:
  4756. return false;
  4757. }
  4758. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4759. static bool
  4760. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4761. const uint8 *buf_func_end, WASMModule *module,
  4762. char *error_buf, uint32 error_buf_size)
  4763. {
  4764. const uint8 *p = buf, *p_end = buf_end;
  4765. const uint8 *p_func = buf_func;
  4766. uint32 func_count = 0, code_count;
  4767. /* code has been loaded in function section, so pass it here, just check
  4768. * whether function and code section have inconsistent lengths */
  4769. read_leb_uint32(p, p_end, code_count);
  4770. if (buf_func)
  4771. read_leb_uint32(p_func, buf_func_end, func_count);
  4772. if (func_count != code_count) {
  4773. set_error_buf(error_buf, error_buf_size,
  4774. "function and code section have inconsistent lengths");
  4775. return false;
  4776. }
  4777. LOG_VERBOSE("Load code segment section success.\n");
  4778. (void)module;
  4779. return true;
  4780. fail:
  4781. return false;
  4782. }
  4783. static bool
  4784. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4785. char *error_buf, uint32 error_buf_size)
  4786. {
  4787. const uint8 *p = buf, *p_end = buf_end;
  4788. WASMFuncType *type;
  4789. uint32 start_function;
  4790. read_leb_uint32(p, p_end, start_function);
  4791. if (start_function
  4792. >= module->function_count + module->import_function_count) {
  4793. set_error_buf(error_buf, error_buf_size, "unknown function");
  4794. return false;
  4795. }
  4796. if (start_function < module->import_function_count)
  4797. type = module->import_functions[start_function].u.function.func_type;
  4798. else
  4799. type = module->functions[start_function - module->import_function_count]
  4800. ->func_type;
  4801. if (type->param_count != 0 || type->result_count != 0) {
  4802. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4803. return false;
  4804. }
  4805. module->start_function = start_function;
  4806. if (p != p_end) {
  4807. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4808. return false;
  4809. }
  4810. LOG_VERBOSE("Load start section success.\n");
  4811. return true;
  4812. fail:
  4813. return false;
  4814. }
  4815. #if WASM_ENABLE_STRINGREF != 0
  4816. static bool
  4817. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4818. WASMModule *module, bool is_load_from_file_buf,
  4819. char *error_buf, uint32 error_buf_size)
  4820. {
  4821. const uint8 *p = buf, *p_end = buf_end;
  4822. int32 deferred_count, immediate_count, string_length, i;
  4823. uint64 total_size;
  4824. read_leb_uint32(p, p_end, deferred_count);
  4825. read_leb_uint32(p, p_end, immediate_count);
  4826. /* proposal set deferred_count for future extension */
  4827. if (deferred_count != 0) {
  4828. goto fail;
  4829. }
  4830. if (immediate_count > 0) {
  4831. total_size = sizeof(char *) * (uint64)immediate_count;
  4832. if (!(module->string_literal_ptrs =
  4833. loader_malloc(total_size, error_buf, error_buf_size))) {
  4834. goto fail;
  4835. }
  4836. module->string_literal_count = immediate_count;
  4837. total_size = sizeof(uint32) * (uint64)immediate_count;
  4838. if (!(module->string_literal_lengths =
  4839. loader_malloc(total_size, error_buf, error_buf_size))) {
  4840. goto fail;
  4841. }
  4842. for (i = 0; i < immediate_count; i++) {
  4843. read_leb_uint32(p, p_end, string_length);
  4844. CHECK_BUF(p, p_end, string_length);
  4845. module->string_literal_ptrs[i] = p;
  4846. module->string_literal_lengths[i] = string_length;
  4847. p += string_length;
  4848. }
  4849. }
  4850. if (p != p_end) {
  4851. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4852. goto fail;
  4853. }
  4854. LOG_VERBOSE("Load stringref section success.\n");
  4855. return true;
  4856. fail:
  4857. return false;
  4858. }
  4859. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4860. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4861. static bool
  4862. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4863. bool is_load_from_file_buf, char *error_buf,
  4864. uint32 error_buf_size)
  4865. {
  4866. const uint8 *p = buf, *p_end = buf_end;
  4867. uint32 name_type, subsection_size;
  4868. uint32 previous_name_type = 0;
  4869. uint32 num_func_name;
  4870. uint32 func_index;
  4871. uint32 previous_func_index = ~0U;
  4872. uint32 func_name_len;
  4873. uint32 name_index;
  4874. int i = 0;
  4875. if (p >= p_end) {
  4876. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4877. return false;
  4878. }
  4879. while (p < p_end) {
  4880. read_leb_uint32(p, p_end, name_type);
  4881. if (i != 0) {
  4882. if (name_type == previous_name_type) {
  4883. set_error_buf(error_buf, error_buf_size,
  4884. "duplicate sub-section");
  4885. return false;
  4886. }
  4887. if (name_type < previous_name_type) {
  4888. set_error_buf(error_buf, error_buf_size,
  4889. "out-of-order sub-section");
  4890. return false;
  4891. }
  4892. }
  4893. previous_name_type = name_type;
  4894. read_leb_uint32(p, p_end, subsection_size);
  4895. CHECK_BUF(p, p_end, subsection_size);
  4896. switch (name_type) {
  4897. case SUB_SECTION_TYPE_FUNC:
  4898. if (subsection_size) {
  4899. read_leb_uint32(p, p_end, num_func_name);
  4900. for (name_index = 0; name_index < num_func_name;
  4901. name_index++) {
  4902. read_leb_uint32(p, p_end, func_index);
  4903. if (func_index == previous_func_index) {
  4904. set_error_buf(error_buf, error_buf_size,
  4905. "duplicate function name");
  4906. return false;
  4907. }
  4908. if (func_index < previous_func_index
  4909. && previous_func_index != ~0U) {
  4910. set_error_buf(error_buf, error_buf_size,
  4911. "out-of-order function index ");
  4912. return false;
  4913. }
  4914. previous_func_index = func_index;
  4915. read_leb_uint32(p, p_end, func_name_len);
  4916. CHECK_BUF(p, p_end, func_name_len);
  4917. /* Skip the import functions */
  4918. if (func_index >= module->import_function_count) {
  4919. func_index -= module->import_function_count;
  4920. if (func_index >= module->function_count) {
  4921. set_error_buf(error_buf, error_buf_size,
  4922. "out-of-range function index");
  4923. return false;
  4924. }
  4925. if (!(module->functions[func_index]->field_name =
  4926. wasm_const_str_list_insert(
  4927. p, func_name_len, module,
  4928. #if WASM_ENABLE_WAMR_COMPILER != 0
  4929. false,
  4930. #else
  4931. is_load_from_file_buf,
  4932. #endif
  4933. error_buf, error_buf_size))) {
  4934. return false;
  4935. }
  4936. }
  4937. p += func_name_len;
  4938. }
  4939. }
  4940. break;
  4941. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4942. */
  4943. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4944. default:
  4945. p = p + subsection_size;
  4946. break;
  4947. }
  4948. i++;
  4949. }
  4950. return true;
  4951. fail:
  4952. return false;
  4953. }
  4954. #endif
  4955. static bool
  4956. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4957. bool is_load_from_file_buf, char *error_buf,
  4958. uint32 error_buf_size)
  4959. {
  4960. const uint8 *p = buf, *p_end = buf_end;
  4961. char section_name[32];
  4962. uint32 name_len, buffer_len;
  4963. if (p >= p_end) {
  4964. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4965. return false;
  4966. }
  4967. read_leb_uint32(p, p_end, name_len);
  4968. if (p + name_len > p_end) {
  4969. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4970. return false;
  4971. }
  4972. if (!wasm_check_utf8_str(p, name_len)) {
  4973. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4974. return false;
  4975. }
  4976. buffer_len = sizeof(section_name);
  4977. memset(section_name, 0, buffer_len);
  4978. if (name_len < buffer_len) {
  4979. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4980. }
  4981. else {
  4982. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4983. memset(section_name + buffer_len - 4, '.', 3);
  4984. }
  4985. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4986. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4987. module->name_section_buf = buf;
  4988. module->name_section_buf_end = buf_end;
  4989. p += name_len;
  4990. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4991. error_buf, error_buf_size)) {
  4992. return false;
  4993. }
  4994. LOG_VERBOSE("Load custom name section success.");
  4995. }
  4996. #endif
  4997. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4998. {
  4999. WASMCustomSection *section =
  5000. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  5001. if (!section) {
  5002. return false;
  5003. }
  5004. section->name_addr = (char *)p;
  5005. section->name_len = name_len;
  5006. section->content_addr = (uint8 *)(p + name_len);
  5007. section->content_len = (uint32)(p_end - p - name_len);
  5008. section->next = module->custom_section_list;
  5009. module->custom_section_list = section;
  5010. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  5011. return true;
  5012. }
  5013. #endif
  5014. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  5015. (void)is_load_from_file_buf;
  5016. (void)module;
  5017. return true;
  5018. fail:
  5019. return false;
  5020. }
  5021. static void
  5022. calculate_global_data_offset(WASMModule *module)
  5023. {
  5024. uint32 i, data_offset;
  5025. data_offset = 0;
  5026. for (i = 0; i < module->import_global_count; i++) {
  5027. WASMGlobalImport *import_global =
  5028. &((module->import_globals + i)->u.global);
  5029. #if WASM_ENABLE_FAST_JIT != 0
  5030. import_global->data_offset = data_offset;
  5031. #endif
  5032. data_offset += wasm_value_type_size(import_global->type.val_type);
  5033. }
  5034. for (i = 0; i < module->global_count; i++) {
  5035. WASMGlobal *global = module->globals + i;
  5036. #if WASM_ENABLE_FAST_JIT != 0
  5037. global->data_offset = data_offset;
  5038. #endif
  5039. data_offset += wasm_value_type_size(global->type.val_type);
  5040. }
  5041. module->global_data_size = data_offset;
  5042. }
  5043. #if WASM_ENABLE_FAST_JIT != 0
  5044. static bool
  5045. init_fast_jit_functions(WASMModule *module, char *error_buf,
  5046. uint32 error_buf_size)
  5047. {
  5048. #if WASM_ENABLE_LAZY_JIT != 0
  5049. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  5050. #endif
  5051. uint32 i;
  5052. if (!module->function_count)
  5053. return true;
  5054. if (!(module->fast_jit_func_ptrs =
  5055. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  5056. error_buf_size))) {
  5057. return false;
  5058. }
  5059. #if WASM_ENABLE_LAZY_JIT != 0
  5060. for (i = 0; i < module->function_count; i++) {
  5061. module->fast_jit_func_ptrs[i] =
  5062. jit_globals->compile_fast_jit_and_then_call;
  5063. }
  5064. #endif
  5065. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  5066. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  5067. set_error_buf(error_buf, error_buf_size,
  5068. "init fast jit thread lock failed");
  5069. return false;
  5070. }
  5071. module->fast_jit_thread_locks_inited[i] = true;
  5072. }
  5073. return true;
  5074. }
  5075. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  5076. #if WASM_ENABLE_JIT != 0
  5077. static bool
  5078. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  5079. uint32 error_buf_size)
  5080. {
  5081. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  5082. AOTCompOption option = { 0 };
  5083. char *aot_last_error;
  5084. uint64 size;
  5085. #if WASM_ENABLE_GC != 0
  5086. bool gc_enabled = true;
  5087. #else
  5088. bool gc_enabled = false;
  5089. #endif
  5090. if (module->function_count == 0)
  5091. return true;
  5092. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5093. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  5094. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  5095. return false;
  5096. }
  5097. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  5098. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  5099. os_mutex_destroy(&module->tierup_wait_lock);
  5100. return false;
  5101. }
  5102. module->tierup_wait_lock_inited = true;
  5103. #endif
  5104. size = sizeof(void *) * (uint64)module->function_count
  5105. + sizeof(bool) * (uint64)module->function_count;
  5106. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  5107. return false;
  5108. }
  5109. module->func_ptrs_compiled =
  5110. (bool *)((uint8 *)module->func_ptrs
  5111. + sizeof(void *) * module->function_count);
  5112. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  5113. if (!module->comp_data) {
  5114. aot_last_error = aot_get_last_error();
  5115. bh_assert(aot_last_error != NULL);
  5116. set_error_buf(error_buf, error_buf_size, aot_last_error);
  5117. return false;
  5118. }
  5119. option.is_jit_mode = true;
  5120. option.opt_level = llvm_jit_options->opt_level;
  5121. option.size_level = llvm_jit_options->size_level;
  5122. option.segue_flags = llvm_jit_options->segue_flags;
  5123. option.quick_invoke_c_api_import =
  5124. llvm_jit_options->quick_invoke_c_api_import;
  5125. #if WASM_ENABLE_BULK_MEMORY != 0
  5126. option.enable_bulk_memory = true;
  5127. #endif
  5128. #if WASM_ENABLE_THREAD_MGR != 0
  5129. option.enable_thread_mgr = true;
  5130. #endif
  5131. #if WASM_ENABLE_TAIL_CALL != 0
  5132. option.enable_tail_call = true;
  5133. #endif
  5134. #if WASM_ENABLE_SIMD != 0
  5135. option.enable_simd = true;
  5136. #endif
  5137. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5138. option.enable_ref_types = true;
  5139. #elif WASM_ENABLE_GC != 0
  5140. option.enable_gc = true;
  5141. #endif
  5142. option.enable_aux_stack_check = true;
  5143. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  5144. || WASM_ENABLE_AOT_STACK_FRAME != 0
  5145. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  5146. aot_call_stack_features_init_default(&option.call_stack_features);
  5147. #endif
  5148. #if WASM_ENABLE_PERF_PROFILING != 0
  5149. option.enable_perf_profiling = true;
  5150. #endif
  5151. #if WASM_ENABLE_MEMORY_PROFILING != 0
  5152. option.enable_memory_profiling = true;
  5153. option.enable_stack_estimation = true;
  5154. #endif
  5155. #if WASM_ENABLE_SHARED_HEAP != 0
  5156. option.enable_shared_heap = true;
  5157. #endif
  5158. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  5159. if (!module->comp_ctx) {
  5160. aot_last_error = aot_get_last_error();
  5161. bh_assert(aot_last_error != NULL);
  5162. set_error_buf(error_buf, error_buf_size, aot_last_error);
  5163. return false;
  5164. }
  5165. return true;
  5166. }
  5167. static bool
  5168. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  5169. uint32 error_buf_size)
  5170. {
  5171. char *aot_last_error;
  5172. uint32 i;
  5173. if (module->function_count == 0)
  5174. return true;
  5175. if (!aot_compile_wasm(module->comp_ctx)) {
  5176. aot_last_error = aot_get_last_error();
  5177. bh_assert(aot_last_error != NULL);
  5178. set_error_buf(error_buf, error_buf_size, aot_last_error);
  5179. return false;
  5180. }
  5181. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5182. if (module->orcjit_stop_compiling)
  5183. return false;
  5184. #endif
  5185. bh_print_time("Begin to lookup llvm jit functions");
  5186. for (i = 0; i < module->function_count; i++) {
  5187. LLVMOrcJITTargetAddress func_addr = 0;
  5188. LLVMErrorRef error;
  5189. char func_name[48];
  5190. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  5191. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  5192. func_name);
  5193. if (error != LLVMErrorSuccess) {
  5194. char *err_msg = LLVMGetErrorMessage(error);
  5195. set_error_buf_v(error_buf, error_buf_size,
  5196. "failed to compile llvm jit function: %s", err_msg);
  5197. LLVMDisposeErrorMessage(err_msg);
  5198. return false;
  5199. }
  5200. /**
  5201. * No need to lock the func_ptr[func_idx] here as it is basic
  5202. * data type, the load/store for it can be finished by one cpu
  5203. * instruction, and there can be only one cpu instruction
  5204. * loading/storing at the same time.
  5205. */
  5206. module->func_ptrs[i] = (void *)func_addr;
  5207. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5208. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  5209. if (module->orcjit_stop_compiling)
  5210. return false;
  5211. #endif
  5212. }
  5213. bh_print_time("End lookup llvm jit functions");
  5214. return true;
  5215. }
  5216. #endif /* end of WASM_ENABLE_JIT != 0 */
  5217. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5218. && WASM_ENABLE_LAZY_JIT != 0
  5219. static void *
  5220. init_llvm_jit_functions_stage2_callback(void *arg)
  5221. {
  5222. WASMModule *module = (WASMModule *)arg;
  5223. char error_buf[128];
  5224. uint32 error_buf_size = (uint32)sizeof(error_buf);
  5225. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5226. module->orcjit_stop_compiling = true;
  5227. return NULL;
  5228. }
  5229. os_mutex_lock(&module->tierup_wait_lock);
  5230. module->llvm_jit_inited = true;
  5231. os_cond_broadcast(&module->tierup_wait_cond);
  5232. os_mutex_unlock(&module->tierup_wait_lock);
  5233. return NULL;
  5234. }
  5235. #endif
  5236. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5237. /* The callback function to compile jit functions */
  5238. static void *
  5239. orcjit_thread_callback(void *arg)
  5240. {
  5241. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  5242. #if WASM_ENABLE_JIT != 0
  5243. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  5244. #endif
  5245. WASMModule *module = thread_arg->module;
  5246. uint32 group_idx = thread_arg->group_idx;
  5247. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  5248. uint32 func_count = module->function_count;
  5249. uint32 i;
  5250. #if WASM_ENABLE_FAST_JIT != 0
  5251. /* Compile fast jit functions of this group */
  5252. for (i = group_idx; i < func_count; i += group_stride) {
  5253. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  5254. LOG_ERROR("failed to compile fast jit function %u\n", i);
  5255. break;
  5256. }
  5257. if (module->orcjit_stop_compiling) {
  5258. return NULL;
  5259. }
  5260. }
  5261. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5262. os_mutex_lock(&module->tierup_wait_lock);
  5263. module->fast_jit_ready_groups++;
  5264. os_mutex_unlock(&module->tierup_wait_lock);
  5265. #endif
  5266. #endif
  5267. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5268. && WASM_ENABLE_LAZY_JIT != 0
  5269. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  5270. for (i = group_idx; i < func_count;
  5271. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5272. uint32 j;
  5273. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5274. if (i + j * group_stride < func_count) {
  5275. if (!jit_compiler_set_call_to_fast_jit(
  5276. module,
  5277. i + j * group_stride + module->import_function_count)) {
  5278. LOG_ERROR(
  5279. "failed to compile call_to_fast_jit for func %u\n",
  5280. i + j * group_stride + module->import_function_count);
  5281. module->orcjit_stop_compiling = true;
  5282. return NULL;
  5283. }
  5284. }
  5285. if (module->orcjit_stop_compiling) {
  5286. return NULL;
  5287. }
  5288. }
  5289. }
  5290. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  5291. fast jit functions are compiled */
  5292. os_mutex_lock(&module->tierup_wait_lock);
  5293. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  5294. && module->fast_jit_ready_groups >= group_stride)) {
  5295. os_cond_reltimedwait(&module->tierup_wait_cond,
  5296. &module->tierup_wait_lock, 10000);
  5297. if (module->orcjit_stop_compiling) {
  5298. /* init_llvm_jit_functions_stage2 failed */
  5299. os_mutex_unlock(&module->tierup_wait_lock);
  5300. return NULL;
  5301. }
  5302. }
  5303. os_mutex_unlock(&module->tierup_wait_lock);
  5304. #endif
  5305. #if WASM_ENABLE_JIT != 0
  5306. /* Compile llvm jit functions of this group */
  5307. for (i = group_idx; i < func_count;
  5308. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5309. LLVMOrcJITTargetAddress func_addr = 0;
  5310. LLVMErrorRef error;
  5311. char func_name[48];
  5312. typedef void (*F)(void);
  5313. union {
  5314. F f;
  5315. void *v;
  5316. } u;
  5317. uint32 j;
  5318. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  5319. "_wrapper");
  5320. LOG_DEBUG("compile llvm jit func %s", func_name);
  5321. error =
  5322. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  5323. if (error != LLVMErrorSuccess) {
  5324. char *err_msg = LLVMGetErrorMessage(error);
  5325. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  5326. LLVMDisposeErrorMessage(err_msg);
  5327. break;
  5328. }
  5329. /* Call the jit wrapper function to trigger its compilation, so as
  5330. to compile the actual jit functions, since we add the latter to
  5331. function list in the PartitionFunction callback */
  5332. u.v = (void *)func_addr;
  5333. u.f();
  5334. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5335. if (i + j * group_stride < func_count) {
  5336. module->func_ptrs_compiled[i + j * group_stride] = true;
  5337. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5338. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5339. i + j * group_stride);
  5340. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5341. func_name);
  5342. if (error != LLVMErrorSuccess) {
  5343. char *err_msg = LLVMGetErrorMessage(error);
  5344. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5345. err_msg);
  5346. LLVMDisposeErrorMessage(err_msg);
  5347. /* Ignore current llvm jit func, as its func ptr is
  5348. previous set to call_to_fast_jit, which also works */
  5349. continue;
  5350. }
  5351. jit_compiler_set_llvm_jit_func_ptr(
  5352. module,
  5353. i + j * group_stride + module->import_function_count,
  5354. (void *)func_addr);
  5355. /* Try to switch to call this llvm jit function instead of
  5356. fast jit function from fast jit jitted code */
  5357. jit_compiler_set_call_to_llvm_jit(
  5358. module,
  5359. i + j * group_stride + module->import_function_count);
  5360. #endif
  5361. }
  5362. }
  5363. if (module->orcjit_stop_compiling) {
  5364. break;
  5365. }
  5366. }
  5367. #endif
  5368. return NULL;
  5369. }
  5370. static void
  5371. orcjit_stop_compile_threads(WASMModule *module)
  5372. {
  5373. #if WASM_ENABLE_LAZY_JIT != 0
  5374. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5375. / sizeof(OrcJitThreadArg));
  5376. module->orcjit_stop_compiling = true;
  5377. for (i = 0; i < thread_num; i++) {
  5378. if (module->orcjit_threads[i])
  5379. os_thread_join(module->orcjit_threads[i], NULL);
  5380. }
  5381. #endif
  5382. }
  5383. static bool
  5384. compile_jit_functions(WASMModule *module, char *error_buf,
  5385. uint32 error_buf_size)
  5386. {
  5387. uint32 thread_num =
  5388. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5389. uint32 i, j;
  5390. bh_print_time("Begin to compile jit functions");
  5391. /* Create threads to compile the jit functions */
  5392. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5393. #if WASM_ENABLE_JIT != 0
  5394. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5395. #endif
  5396. module->orcjit_thread_args[i].module = module;
  5397. module->orcjit_thread_args[i].group_idx = i;
  5398. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5399. (void *)&module->orcjit_thread_args[i],
  5400. APP_THREAD_STACK_SIZE_DEFAULT)
  5401. != 0) {
  5402. set_error_buf(error_buf, error_buf_size,
  5403. "create orcjit compile thread failed");
  5404. /* Terminate the threads created */
  5405. module->orcjit_stop_compiling = true;
  5406. for (j = 0; j < i; j++) {
  5407. os_thread_join(module->orcjit_threads[j], NULL);
  5408. }
  5409. return false;
  5410. }
  5411. }
  5412. #if WASM_ENABLE_LAZY_JIT == 0
  5413. /* Wait until all jit functions are compiled for eager mode */
  5414. for (i = 0; i < thread_num; i++) {
  5415. if (module->orcjit_threads[i])
  5416. os_thread_join(module->orcjit_threads[i], NULL);
  5417. }
  5418. #if WASM_ENABLE_FAST_JIT != 0
  5419. /* Ensure all the fast-jit functions are compiled */
  5420. for (i = 0; i < module->function_count; i++) {
  5421. if (!jit_compiler_is_compiled(module,
  5422. i + module->import_function_count)) {
  5423. set_error_buf(error_buf, error_buf_size,
  5424. "failed to compile fast jit function");
  5425. return false;
  5426. }
  5427. }
  5428. #endif
  5429. #if WASM_ENABLE_JIT != 0
  5430. /* Ensure all the llvm-jit functions are compiled */
  5431. for (i = 0; i < module->function_count; i++) {
  5432. if (!module->func_ptrs_compiled[i]) {
  5433. set_error_buf(error_buf, error_buf_size,
  5434. "failed to compile llvm jit function");
  5435. return false;
  5436. }
  5437. }
  5438. #endif
  5439. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5440. bh_print_time("End compile jit functions");
  5441. return true;
  5442. }
  5443. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5444. static bool
  5445. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5446. uint32 cur_func_idx, char *error_buf,
  5447. uint32 error_buf_size);
  5448. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5449. void **
  5450. wasm_interp_get_handle_table(void);
  5451. static void **handle_table;
  5452. #endif
  5453. static bool
  5454. load_from_sections(WASMModule *module, WASMSection *sections,
  5455. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5456. bool no_resolve, char *error_buf, uint32 error_buf_size)
  5457. {
  5458. WASMExport *export;
  5459. WASMSection *section = sections;
  5460. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5461. *buf_func = NULL, *buf_func_end = NULL;
  5462. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5463. WASMGlobal *aux_stack_top_global = NULL, *global;
  5464. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5465. aux_stack_top = (uint64)-1LL;
  5466. uint32 global_index, func_index, i;
  5467. uint32 aux_data_end_global_index = (uint32)-1;
  5468. uint32 aux_heap_base_global_index = (uint32)-1;
  5469. WASMFuncType *func_type;
  5470. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5471. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5472. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5473. #if WASM_ENABLE_BULK_MEMORY != 0
  5474. bool has_datacount_section = false;
  5475. #endif
  5476. /* Find code and function sections if have */
  5477. while (section) {
  5478. if (section->section_type == SECTION_TYPE_CODE) {
  5479. buf_code = section->section_body;
  5480. buf_code_end = buf_code + section->section_body_size;
  5481. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5482. module->buf_code = (uint8 *)buf_code;
  5483. module->buf_code_size = section->section_body_size;
  5484. #endif
  5485. }
  5486. else if (section->section_type == SECTION_TYPE_FUNC) {
  5487. buf_func = section->section_body;
  5488. buf_func_end = buf_func + section->section_body_size;
  5489. }
  5490. section = section->next;
  5491. }
  5492. section = sections;
  5493. while (section) {
  5494. buf = section->section_body;
  5495. buf_end = buf + section->section_body_size;
  5496. switch (section->section_type) {
  5497. case SECTION_TYPE_USER:
  5498. /* unsupported user section, ignore it. */
  5499. if (!load_user_section(buf, buf_end, module,
  5500. reuse_const_strings, error_buf,
  5501. error_buf_size))
  5502. return false;
  5503. break;
  5504. case SECTION_TYPE_TYPE:
  5505. if (!load_type_section(buf, buf_end, module, error_buf,
  5506. error_buf_size))
  5507. return false;
  5508. break;
  5509. case SECTION_TYPE_IMPORT:
  5510. if (!load_import_section(buf, buf_end, module,
  5511. reuse_const_strings, no_resolve,
  5512. error_buf, error_buf_size))
  5513. return false;
  5514. break;
  5515. case SECTION_TYPE_FUNC:
  5516. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5517. module, error_buf, error_buf_size))
  5518. return false;
  5519. break;
  5520. case SECTION_TYPE_TABLE:
  5521. if (!load_table_section(buf, buf_end, module, error_buf,
  5522. error_buf_size))
  5523. return false;
  5524. break;
  5525. case SECTION_TYPE_MEMORY:
  5526. if (!load_memory_section(buf, buf_end, module, error_buf,
  5527. error_buf_size))
  5528. return false;
  5529. break;
  5530. #if WASM_ENABLE_TAGS != 0
  5531. case SECTION_TYPE_TAG:
  5532. /* load tag declaration section */
  5533. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5534. module, error_buf, error_buf_size))
  5535. return false;
  5536. break;
  5537. #endif
  5538. case SECTION_TYPE_GLOBAL:
  5539. if (!load_global_section(buf, buf_end, module, error_buf,
  5540. error_buf_size))
  5541. return false;
  5542. break;
  5543. case SECTION_TYPE_EXPORT:
  5544. if (!load_export_section(buf, buf_end, module,
  5545. reuse_const_strings, error_buf,
  5546. error_buf_size))
  5547. return false;
  5548. break;
  5549. case SECTION_TYPE_START:
  5550. if (!load_start_section(buf, buf_end, module, error_buf,
  5551. error_buf_size))
  5552. return false;
  5553. break;
  5554. case SECTION_TYPE_ELEM:
  5555. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5556. error_buf_size))
  5557. return false;
  5558. break;
  5559. case SECTION_TYPE_CODE:
  5560. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5561. module, error_buf, error_buf_size))
  5562. return false;
  5563. break;
  5564. case SECTION_TYPE_DATA:
  5565. if (!load_data_segment_section(buf, buf_end, module,
  5566. #if WASM_ENABLE_BULK_MEMORY != 0
  5567. has_datacount_section,
  5568. #endif
  5569. clone_data_seg, error_buf,
  5570. error_buf_size))
  5571. return false;
  5572. break;
  5573. #if WASM_ENABLE_BULK_MEMORY != 0
  5574. case SECTION_TYPE_DATACOUNT:
  5575. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5576. error_buf_size))
  5577. return false;
  5578. has_datacount_section = true;
  5579. break;
  5580. #endif
  5581. #if WASM_ENABLE_STRINGREF != 0
  5582. case SECTION_TYPE_STRINGREF:
  5583. if (!load_stringref_section(buf, buf_end, module,
  5584. reuse_const_strings, error_buf,
  5585. error_buf_size))
  5586. return false;
  5587. break;
  5588. #endif
  5589. default:
  5590. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5591. return false;
  5592. }
  5593. section = section->next;
  5594. }
  5595. #if WASM_ENABLE_BULK_MEMORY != 0
  5596. if (!check_data_count_consistency(
  5597. has_datacount_section, module->data_seg_count1,
  5598. module->data_seg_count, error_buf, error_buf_size)) {
  5599. return false;
  5600. }
  5601. #endif
  5602. module->aux_data_end_global_index = (uint32)-1;
  5603. module->aux_heap_base_global_index = (uint32)-1;
  5604. module->aux_stack_top_global_index = (uint32)-1;
  5605. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5606. export = module->exports;
  5607. for (i = 0; i < module->export_count; i++, export ++) {
  5608. if (export->kind == EXPORT_KIND_GLOBAL) {
  5609. if (!strcmp(export->name, "__heap_base")) {
  5610. if (export->index < module->import_global_count) {
  5611. LOG_DEBUG("Skip the process if __heap_base is imported "
  5612. "instead of being a local global");
  5613. continue;
  5614. }
  5615. /* only process linker-generated symbols */
  5616. global_index = export->index - module->import_global_count;
  5617. global = module->globals + global_index;
  5618. if (global->type.val_type == VALUE_TYPE_I32
  5619. && !global->type.is_mutable
  5620. && global->init_expr.init_expr_type
  5621. == INIT_EXPR_TYPE_I32_CONST) {
  5622. aux_heap_base_global = global;
  5623. aux_heap_base =
  5624. (uint64)(uint32)global->init_expr.u.unary.v.i32;
  5625. aux_heap_base_global_index = export->index;
  5626. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5627. aux_heap_base);
  5628. }
  5629. }
  5630. else if (!strcmp(export->name, "__data_end")) {
  5631. if (export->index < module->import_global_count) {
  5632. LOG_DEBUG("Skip the process if __data_end is imported "
  5633. "instead of being a local global");
  5634. continue;
  5635. }
  5636. /* only process linker-generated symbols */
  5637. global_index = export->index - module->import_global_count;
  5638. global = module->globals + global_index;
  5639. if (global->type.val_type == VALUE_TYPE_I32
  5640. && !global->type.is_mutable
  5641. && global->init_expr.init_expr_type
  5642. == INIT_EXPR_TYPE_I32_CONST) {
  5643. aux_data_end_global = global;
  5644. aux_data_end =
  5645. (uint64)(uint32)global->init_expr.u.unary.v.i32;
  5646. aux_data_end_global_index = export->index;
  5647. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5648. aux_data_end);
  5649. aux_data_end = align_uint64(aux_data_end, 16);
  5650. }
  5651. }
  5652. /* For module compiled with -pthread option, the global is:
  5653. [0] stack_top <-- 0
  5654. [1] tls_pointer
  5655. [2] tls_size
  5656. [3] data_end <-- 3
  5657. [4] global_base
  5658. [5] heap_base <-- 5
  5659. [6] dso_handle
  5660. For module compiled without -pthread option:
  5661. [0] stack_top <-- 0
  5662. [1] data_end <-- 1
  5663. [2] global_base
  5664. [3] heap_base <-- 3
  5665. [4] dso_handle
  5666. */
  5667. if (aux_data_end_global && aux_heap_base_global
  5668. && aux_data_end <= aux_heap_base) {
  5669. module->aux_data_end_global_index = aux_data_end_global_index;
  5670. module->aux_data_end = aux_data_end;
  5671. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5672. module->aux_heap_base = aux_heap_base;
  5673. /* Resolve aux stack top global */
  5674. for (global_index = 0; global_index < module->global_count;
  5675. global_index++) {
  5676. global = module->globals + global_index;
  5677. if (global->type.is_mutable /* heap_base and data_end is
  5678. not mutable */
  5679. && global->type.val_type == VALUE_TYPE_I32
  5680. && global->init_expr.init_expr_type
  5681. == INIT_EXPR_TYPE_I32_CONST
  5682. && (uint64)(uint32)global->init_expr.u.unary.v.i32
  5683. <= aux_heap_base) {
  5684. aux_stack_top_global = global;
  5685. aux_stack_top =
  5686. (uint64)(uint32)global->init_expr.u.unary.v.i32;
  5687. module->aux_stack_top_global_index =
  5688. module->import_global_count + global_index;
  5689. module->aux_stack_bottom = aux_stack_top;
  5690. module->aux_stack_size =
  5691. aux_stack_top > aux_data_end
  5692. ? (uint32)(aux_stack_top - aux_data_end)
  5693. : (uint32)aux_stack_top;
  5694. LOG_VERBOSE(
  5695. "Found aux stack top global, value: %" PRIu64 ", "
  5696. "global index: %d, stack size: %d",
  5697. aux_stack_top, global_index,
  5698. module->aux_stack_size);
  5699. break;
  5700. }
  5701. }
  5702. if (!aux_stack_top_global) {
  5703. /* Auxiliary stack global isn't found, it must be unused
  5704. in the wasm app, as if it is used, the global must be
  5705. defined. Here we set it to __heap_base global and set
  5706. its size to 0. */
  5707. aux_stack_top_global = aux_heap_base_global;
  5708. aux_stack_top = aux_heap_base;
  5709. module->aux_stack_top_global_index =
  5710. module->aux_heap_base_global_index;
  5711. module->aux_stack_bottom = aux_stack_top;
  5712. module->aux_stack_size = 0;
  5713. }
  5714. break;
  5715. }
  5716. }
  5717. }
  5718. module->malloc_function = (uint32)-1;
  5719. module->free_function = (uint32)-1;
  5720. module->retain_function = (uint32)-1;
  5721. /* Resolve malloc/free function exported by wasm module */
  5722. #if WASM_ENABLE_MEMORY64 != 0
  5723. if (has_module_memory64(module))
  5724. malloc_free_io_type = VALUE_TYPE_I64;
  5725. #endif
  5726. export = module->exports;
  5727. for (i = 0; i < module->export_count; i++, export ++) {
  5728. if (export->kind == EXPORT_KIND_FUNC) {
  5729. if (!strcmp(export->name, "malloc")
  5730. && export->index >= module->import_function_count) {
  5731. func_index = export->index - module->import_function_count;
  5732. func_type = module->functions[func_index]->func_type;
  5733. if (func_type->param_count == 1 && func_type->result_count == 1
  5734. && func_type->types[0] == malloc_free_io_type
  5735. && func_type->types[1] == malloc_free_io_type) {
  5736. bh_assert(module->malloc_function == (uint32)-1);
  5737. module->malloc_function = export->index;
  5738. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5739. export->name, export->index);
  5740. }
  5741. }
  5742. else if (!strcmp(export->name, "__new")
  5743. && export->index >= module->import_function_count) {
  5744. /* __new && __pin for AssemblyScript */
  5745. func_index = export->index - module->import_function_count;
  5746. func_type = module->functions[func_index]->func_type;
  5747. if (func_type->param_count == 2 && func_type->result_count == 1
  5748. && func_type->types[0] == malloc_free_io_type
  5749. && func_type->types[1] == VALUE_TYPE_I32
  5750. && func_type->types[2] == malloc_free_io_type) {
  5751. uint32 j;
  5752. WASMExport *export_tmp;
  5753. bh_assert(module->malloc_function == (uint32)-1);
  5754. module->malloc_function = export->index;
  5755. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5756. export->name, export->index);
  5757. /* resolve retain function.
  5758. If not found, reset malloc function index */
  5759. export_tmp = module->exports;
  5760. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5761. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5762. && (!strcmp(export_tmp->name, "__retain")
  5763. || (!strcmp(export_tmp->name, "__pin")))
  5764. && (export_tmp->index
  5765. >= module->import_function_count)) {
  5766. func_index = export_tmp->index
  5767. - module->import_function_count;
  5768. func_type =
  5769. module->functions[func_index]->func_type;
  5770. if (func_type->param_count == 1
  5771. && func_type->result_count == 1
  5772. && func_type->types[0] == malloc_free_io_type
  5773. && func_type->types[1] == malloc_free_io_type) {
  5774. bh_assert(module->retain_function
  5775. == (uint32)-1);
  5776. module->retain_function = export_tmp->index;
  5777. LOG_VERBOSE("Found retain function, name: %s, "
  5778. "index: %u",
  5779. export_tmp->name,
  5780. export_tmp->index);
  5781. break;
  5782. }
  5783. }
  5784. }
  5785. if (j == module->export_count) {
  5786. module->malloc_function = (uint32)-1;
  5787. LOG_VERBOSE("Can't find retain function,"
  5788. "reset malloc function index to -1");
  5789. }
  5790. }
  5791. }
  5792. else if (((!strcmp(export->name, "free"))
  5793. || (!strcmp(export->name, "__release"))
  5794. || (!strcmp(export->name, "__unpin")))
  5795. && export->index >= module->import_function_count) {
  5796. func_index = export->index - module->import_function_count;
  5797. func_type = module->functions[func_index]->func_type;
  5798. if (func_type->param_count == 1 && func_type->result_count == 0
  5799. && func_type->types[0] == malloc_free_io_type) {
  5800. bh_assert(module->free_function == (uint32)-1);
  5801. module->free_function = export->index;
  5802. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5803. export->name, export->index);
  5804. }
  5805. }
  5806. }
  5807. }
  5808. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5809. handle_table = wasm_interp_get_handle_table();
  5810. #endif
  5811. for (i = 0; i < module->function_count; i++) {
  5812. WASMFunction *func = module->functions[i];
  5813. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5814. error_buf_size)) {
  5815. return false;
  5816. }
  5817. if (i == module->function_count - 1
  5818. && func->code + func->code_size != buf_code_end) {
  5819. set_error_buf(error_buf, error_buf_size,
  5820. "code section size mismatch");
  5821. return false;
  5822. }
  5823. }
  5824. if (!module->possible_memory_grow) {
  5825. #if WASM_ENABLE_SHRUNK_MEMORY != 0
  5826. if (aux_data_end_global && aux_heap_base_global
  5827. && aux_stack_top_global) {
  5828. uint64 init_memory_size;
  5829. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5830. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5831. * valid range of uint32 */
  5832. if (shrunk_memory_size <= UINT32_MAX) {
  5833. if (module->import_memory_count) {
  5834. WASMMemoryImport *memory_import =
  5835. &module->import_memories[0].u.memory;
  5836. init_memory_size =
  5837. (uint64)memory_import->mem_type.num_bytes_per_page
  5838. * memory_import->mem_type.init_page_count;
  5839. if (shrunk_memory_size <= init_memory_size) {
  5840. /* Reset memory info to decrease memory usage */
  5841. memory_import->mem_type.num_bytes_per_page =
  5842. (uint32)shrunk_memory_size;
  5843. memory_import->mem_type.init_page_count = 1;
  5844. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5845. shrunk_memory_size);
  5846. }
  5847. }
  5848. if (module->memory_count) {
  5849. WASMMemory *memory = &module->memories[0];
  5850. init_memory_size = (uint64)memory->num_bytes_per_page
  5851. * memory->init_page_count;
  5852. if (shrunk_memory_size <= init_memory_size) {
  5853. /* Reset memory info to decrease memory usage */
  5854. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5855. memory->init_page_count = 1;
  5856. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5857. shrunk_memory_size);
  5858. }
  5859. }
  5860. }
  5861. }
  5862. #endif /* WASM_ENABLE_SHRUNK_MEMORY != 0 */
  5863. #if WASM_ENABLE_MULTI_MODULE == 0
  5864. if (module->import_memory_count) {
  5865. WASMMemoryImport *memory_import =
  5866. &module->import_memories[0].u.memory;
  5867. /* Only resize the memory to one big page if num_bytes_per_page is
  5868. * in valid range of uint32 */
  5869. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5870. memory_import->mem_type.num_bytes_per_page *=
  5871. memory_import->mem_type.init_page_count;
  5872. if (memory_import->mem_type.init_page_count > 0)
  5873. memory_import->mem_type.init_page_count =
  5874. memory_import->mem_type.max_page_count = 1;
  5875. else
  5876. memory_import->mem_type.init_page_count =
  5877. memory_import->mem_type.max_page_count = 0;
  5878. }
  5879. }
  5880. if (module->memory_count) {
  5881. WASMMemory *memory = &module->memories[0];
  5882. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5883. * valid range of uint32 */
  5884. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5885. memory->num_bytes_per_page *= memory->init_page_count;
  5886. if (memory->init_page_count > 0)
  5887. memory->init_page_count = memory->max_page_count = 1;
  5888. else
  5889. memory->init_page_count = memory->max_page_count = 0;
  5890. }
  5891. }
  5892. #endif
  5893. }
  5894. #if WASM_ENABLE_MEMORY64 != 0
  5895. if (!check_memory64_flags_consistency(module, error_buf, error_buf_size,
  5896. false))
  5897. return false;
  5898. #endif
  5899. calculate_global_data_offset(module);
  5900. #if WASM_ENABLE_FAST_JIT != 0
  5901. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5902. return false;
  5903. }
  5904. #endif
  5905. #if WASM_ENABLE_JIT != 0
  5906. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5907. return false;
  5908. }
  5909. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5910. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5911. return false;
  5912. }
  5913. #else
  5914. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5915. thread fast jit execution, since applying llvm optimizations in
  5916. aot_compile_wasm may cost a lot of time.
  5917. Create thread with enough native stack to apply llvm optimizations */
  5918. if (os_thread_create(&module->llvm_jit_init_thread,
  5919. init_llvm_jit_functions_stage2_callback,
  5920. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5921. != 0) {
  5922. set_error_buf(error_buf, error_buf_size,
  5923. "create orcjit compile thread failed");
  5924. return false;
  5925. }
  5926. #endif
  5927. #endif
  5928. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5929. /* Create threads to compile the jit functions */
  5930. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5931. return false;
  5932. }
  5933. #endif
  5934. #if WASM_ENABLE_MEMORY_TRACING != 0
  5935. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5936. #endif
  5937. return true;
  5938. }
  5939. static WASMModule *
  5940. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5941. {
  5942. WASMModule *module =
  5943. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5944. bh_list_status ret;
  5945. if (!module) {
  5946. return NULL;
  5947. }
  5948. module->module_type = Wasm_Module_Bytecode;
  5949. /* Set start_function to -1, means no start function */
  5950. module->start_function = (uint32)-1;
  5951. module->name = name;
  5952. module->is_binary_freeable = false;
  5953. #if WASM_ENABLE_FAST_INTERP == 0
  5954. module->br_table_cache_list = &module->br_table_cache_list_head;
  5955. ret = bh_list_init(module->br_table_cache_list);
  5956. bh_assert(ret == BH_LIST_SUCCESS);
  5957. #endif
  5958. #if WASM_ENABLE_MULTI_MODULE != 0
  5959. module->import_module_list = &module->import_module_list_head;
  5960. ret = bh_list_init(module->import_module_list);
  5961. bh_assert(ret == BH_LIST_SUCCESS);
  5962. #endif
  5963. #if WASM_ENABLE_DEBUG_INTERP != 0
  5964. ret = bh_list_init(&module->fast_opcode_list);
  5965. bh_assert(ret == BH_LIST_SUCCESS);
  5966. #endif
  5967. #if WASM_ENABLE_GC != 0
  5968. if (!(module->ref_type_set =
  5969. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5970. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5971. goto fail1;
  5972. }
  5973. if (os_mutex_init(&module->rtt_type_lock)) {
  5974. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5975. goto fail2;
  5976. }
  5977. #endif
  5978. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5979. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5980. && WASM_ENABLE_LAZY_JIT != 0)
  5981. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5982. set_error_buf(error_buf, error_buf_size,
  5983. "init instance list lock failed");
  5984. goto fail3;
  5985. }
  5986. #endif
  5987. #if WASM_ENABLE_LIBC_WASI != 0
  5988. #if WASM_ENABLE_UVWASI == 0
  5989. module->wasi_args.stdio[0] = os_invalid_raw_handle();
  5990. module->wasi_args.stdio[1] = os_invalid_raw_handle();
  5991. module->wasi_args.stdio[2] = os_invalid_raw_handle();
  5992. #else
  5993. module->wasi_args.stdio[0] = os_get_invalid_handle();
  5994. module->wasi_args.stdio[1] = os_get_invalid_handle();
  5995. module->wasi_args.stdio[2] = os_get_invalid_handle();
  5996. #endif /* WASM_ENABLE_UVWASI == 0 */
  5997. #endif /* WASM_ENABLE_LIBC_WASI != 0 */
  5998. (void)ret;
  5999. return module;
  6000. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6001. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  6002. && WASM_ENABLE_LAZY_JIT != 0)
  6003. fail3:
  6004. #endif
  6005. #if WASM_ENABLE_GC != 0
  6006. os_mutex_destroy(&module->rtt_type_lock);
  6007. fail2:
  6008. bh_hash_map_destroy(module->ref_type_set);
  6009. fail1:
  6010. #endif
  6011. wasm_runtime_free(module);
  6012. return NULL;
  6013. }
  6014. #if WASM_ENABLE_DEBUG_INTERP != 0
  6015. static bool
  6016. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  6017. uint32 error_buf_size)
  6018. {
  6019. WASMFastOPCodeNode *fast_op =
  6020. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  6021. if (fast_op) {
  6022. fast_op->offset = pos - module->load_addr;
  6023. fast_op->orig_op = orig_op;
  6024. bh_list_insert(&module->fast_opcode_list, fast_op);
  6025. }
  6026. return fast_op ? true : false;
  6027. }
  6028. #endif
  6029. WASMModule *
  6030. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  6031. uint32 error_buf_size)
  6032. {
  6033. WASMModule *module = create_module("", error_buf, error_buf_size);
  6034. if (!module)
  6035. return NULL;
  6036. if (!load_from_sections(module, section_list, false, true, false, error_buf,
  6037. error_buf_size)) {
  6038. wasm_loader_unload(module);
  6039. return NULL;
  6040. }
  6041. LOG_VERBOSE("Load module from sections success.\n");
  6042. return module;
  6043. }
  6044. static void
  6045. destroy_sections(WASMSection *section_list)
  6046. {
  6047. WASMSection *section = section_list, *next;
  6048. while (section) {
  6049. next = section->next;
  6050. wasm_runtime_free(section);
  6051. section = next;
  6052. }
  6053. }
  6054. /* clang-format off */
  6055. static uint8 section_ids[] = {
  6056. SECTION_TYPE_USER,
  6057. SECTION_TYPE_TYPE,
  6058. SECTION_TYPE_IMPORT,
  6059. SECTION_TYPE_FUNC,
  6060. SECTION_TYPE_TABLE,
  6061. SECTION_TYPE_MEMORY,
  6062. #if WASM_ENABLE_TAGS != 0
  6063. SECTION_TYPE_TAG,
  6064. #endif
  6065. #if WASM_ENABLE_STRINGREF != 0
  6066. /* must immediately precede the global section,
  6067. or where the global section would be */
  6068. SECTION_TYPE_STRINGREF,
  6069. #endif
  6070. SECTION_TYPE_GLOBAL,
  6071. SECTION_TYPE_EXPORT,
  6072. SECTION_TYPE_START,
  6073. SECTION_TYPE_ELEM,
  6074. #if WASM_ENABLE_BULK_MEMORY != 0
  6075. SECTION_TYPE_DATACOUNT,
  6076. #endif
  6077. SECTION_TYPE_CODE,
  6078. SECTION_TYPE_DATA
  6079. };
  6080. /* clang-format on */
  6081. static uint8
  6082. get_section_index(uint8 section_type)
  6083. {
  6084. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  6085. for (uint8 i = 0; i < max_id; i++) {
  6086. if (section_type == section_ids[i])
  6087. return i;
  6088. }
  6089. return (uint8)-1;
  6090. }
  6091. static bool
  6092. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  6093. char *error_buf, uint32 error_buf_size)
  6094. {
  6095. WASMSection *section_list_end = NULL, *section;
  6096. const uint8 *p = buf, *p_end = buf + size;
  6097. uint8 section_type, section_index, last_section_index = (uint8)-1;
  6098. uint32 section_size;
  6099. bh_assert(!*p_section_list);
  6100. p += 8;
  6101. while (p < p_end) {
  6102. CHECK_BUF(p, p_end, 1);
  6103. section_type = read_uint8(p);
  6104. section_index = get_section_index(section_type);
  6105. if (section_index != (uint8)-1) {
  6106. if (section_type != SECTION_TYPE_USER) {
  6107. /* Custom sections may be inserted at any place,
  6108. while other sections must occur at most once
  6109. and in prescribed order. */
  6110. if (last_section_index != (uint8)-1
  6111. && (section_index <= last_section_index)) {
  6112. set_error_buf(error_buf, error_buf_size,
  6113. "unexpected content after last section or "
  6114. "junk after last section");
  6115. return false;
  6116. }
  6117. last_section_index = section_index;
  6118. }
  6119. read_leb_uint32(p, p_end, section_size);
  6120. CHECK_BUF1(p, p_end, section_size);
  6121. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  6122. error_buf_size))) {
  6123. return false;
  6124. }
  6125. section->section_type = section_type;
  6126. section->section_body = (uint8 *)p;
  6127. section->section_body_size = section_size;
  6128. if (!section_list_end)
  6129. *p_section_list = section_list_end = section;
  6130. else {
  6131. section_list_end->next = section;
  6132. section_list_end = section;
  6133. }
  6134. p += section_size;
  6135. }
  6136. else {
  6137. set_error_buf(error_buf, error_buf_size, "invalid section id");
  6138. return false;
  6139. }
  6140. }
  6141. return true;
  6142. fail:
  6143. return false;
  6144. }
  6145. static void
  6146. exchange32(uint8 *p_data)
  6147. {
  6148. uint8 value = *p_data;
  6149. *p_data = *(p_data + 3);
  6150. *(p_data + 3) = value;
  6151. value = *(p_data + 1);
  6152. *(p_data + 1) = *(p_data + 2);
  6153. *(p_data + 2) = value;
  6154. }
  6155. static union {
  6156. int a;
  6157. char b;
  6158. } __ue = { .a = 1 };
  6159. #define is_little_endian() (__ue.b == 1)
  6160. static bool
  6161. load(const uint8 *buf, uint32 size, WASMModule *module,
  6162. bool wasm_binary_freeable, bool no_resolve, char *error_buf,
  6163. uint32 error_buf_size)
  6164. {
  6165. const uint8 *buf_end = buf + size;
  6166. const uint8 *p = buf, *p_end = buf_end;
  6167. uint32 magic_number, version;
  6168. WASMSection *section_list = NULL;
  6169. CHECK_BUF1(p, p_end, sizeof(uint32));
  6170. magic_number = read_uint32(p);
  6171. if (!is_little_endian())
  6172. exchange32((uint8 *)&magic_number);
  6173. if (magic_number != WASM_MAGIC_NUMBER) {
  6174. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  6175. return false;
  6176. }
  6177. CHECK_BUF1(p, p_end, sizeof(uint32));
  6178. version = read_uint32(p);
  6179. if (!is_little_endian())
  6180. exchange32((uint8 *)&version);
  6181. if (version != WASM_CURRENT_VERSION) {
  6182. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  6183. return false;
  6184. }
  6185. module->package_version = version;
  6186. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  6187. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  6188. no_resolve, error_buf, error_buf_size)) {
  6189. destroy_sections(section_list);
  6190. return false;
  6191. }
  6192. destroy_sections(section_list);
  6193. return true;
  6194. fail:
  6195. return false;
  6196. }
  6197. #if WASM_ENABLE_LIBC_WASI != 0
  6198. /**
  6199. * refer to
  6200. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  6201. */
  6202. static bool
  6203. check_wasi_abi_compatibility(const WASMModule *module,
  6204. #if WASM_ENABLE_MULTI_MODULE != 0
  6205. bool main_module,
  6206. #endif
  6207. char *error_buf, uint32 error_buf_size)
  6208. {
  6209. /**
  6210. * be careful with:
  6211. * wasi compatible modules(command/reactor) which don't import any wasi
  6212. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  6213. * reactor can depend on nothing. At the same time, each has its own entry
  6214. * point.
  6215. *
  6216. * observations:
  6217. * - clang always injects `_start` into a command
  6218. * - clang always injects `_initialize` into a reactor
  6219. * - `iwasm -f` allows to run a function in the reactor
  6220. *
  6221. * strong assumptions:
  6222. * - no one will define either `_start` or `_initialize` on purpose
  6223. * - `_start` should always be `void _start(void)`
  6224. * - `_initialize` should always be `void _initialize(void)`
  6225. *
  6226. */
  6227. /* clang-format off */
  6228. /**
  6229. *
  6230. * | | import_wasi_api True | | import_wasi_api False | |
  6231. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  6232. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  6233. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  6234. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  6235. */
  6236. /* clang-format on */
  6237. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  6238. uint32 import_function_count = module->import_function_count;
  6239. WASMFuncType *func_type;
  6240. /* (func (export "_start") (...) */
  6241. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  6242. error_buf, error_buf_size);
  6243. if (start) {
  6244. if (start->index < import_function_count) {
  6245. set_error_buf(
  6246. error_buf, error_buf_size,
  6247. "the builtin _start function can not be an import function");
  6248. return false;
  6249. }
  6250. func_type =
  6251. module->functions[start->index - import_function_count]->func_type;
  6252. if (func_type->param_count || func_type->result_count) {
  6253. set_error_buf(error_buf, error_buf_size,
  6254. "the signature of builtin _start function is wrong");
  6255. return false;
  6256. }
  6257. }
  6258. else {
  6259. /* (func (export "_initialize") (...) */
  6260. initialize =
  6261. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  6262. error_buf, error_buf_size);
  6263. if (initialize) {
  6264. if (initialize->index < import_function_count) {
  6265. set_error_buf(error_buf, error_buf_size,
  6266. "the builtin _initialize function can not be an "
  6267. "import function");
  6268. return false;
  6269. }
  6270. func_type =
  6271. module->functions[initialize->index - import_function_count]
  6272. ->func_type;
  6273. if (func_type->param_count || func_type->result_count) {
  6274. set_error_buf(
  6275. error_buf, error_buf_size,
  6276. "the signature of builtin _initialize function is wrong");
  6277. return false;
  6278. }
  6279. }
  6280. }
  6281. /* filter out non-wasi compatible modules */
  6282. if (!module->import_wasi_api && !start && !initialize) {
  6283. return true;
  6284. }
  6285. /* should have one at least */
  6286. if (module->import_wasi_api && !start && !initialize) {
  6287. LOG_WARNING("warning: a module with WASI apis should be either "
  6288. "a command or a reactor");
  6289. }
  6290. /*
  6291. * there is at least one of `_start` and `_initialize` in below cases.
  6292. * according to the assumption, they should be all wasi compatible
  6293. */
  6294. #if WASM_ENABLE_MULTI_MODULE != 0
  6295. /* filter out commands (with `_start`) cases */
  6296. if (start && !main_module) {
  6297. set_error_buf(
  6298. error_buf, error_buf_size,
  6299. "a command (with _start function) can not be a sub-module");
  6300. return false;
  6301. }
  6302. #endif
  6303. /*
  6304. * it is ok a reactor acts as a main module,
  6305. * so skip the check about (with `_initialize`)
  6306. */
  6307. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  6308. error_buf, error_buf_size);
  6309. if (!memory
  6310. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  6311. /*
  6312. * with wasi-threads, it's still an open question if a memory
  6313. * should be exported.
  6314. *
  6315. * https://github.com/WebAssembly/wasi-threads/issues/22
  6316. * https://github.com/WebAssembly/WASI/issues/502
  6317. *
  6318. * Note: this code assumes the number of memories is at most 1.
  6319. */
  6320. && module->import_memory_count == 0
  6321. #endif
  6322. ) {
  6323. set_error_buf(error_buf, error_buf_size,
  6324. "a module with WASI apis must export memory by default");
  6325. return false;
  6326. }
  6327. return true;
  6328. }
  6329. #endif
  6330. WASMModule *
  6331. wasm_loader_load(uint8 *buf, uint32 size,
  6332. #if WASM_ENABLE_MULTI_MODULE != 0
  6333. bool main_module,
  6334. #endif
  6335. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  6336. {
  6337. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  6338. if (!module) {
  6339. return NULL;
  6340. }
  6341. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  6342. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  6343. module->load_addr = (uint8 *)buf;
  6344. module->load_size = size;
  6345. #endif
  6346. if (!load(buf, size, module, args->wasm_binary_freeable, args->no_resolve,
  6347. error_buf, error_buf_size)) {
  6348. goto fail;
  6349. }
  6350. #if WASM_ENABLE_LIBC_WASI != 0
  6351. /* Check the WASI application ABI */
  6352. if (!check_wasi_abi_compatibility(module,
  6353. #if WASM_ENABLE_MULTI_MODULE != 0
  6354. main_module,
  6355. #endif
  6356. error_buf, error_buf_size)) {
  6357. goto fail;
  6358. }
  6359. #endif
  6360. LOG_VERBOSE("Load module success.\n");
  6361. return module;
  6362. fail:
  6363. wasm_loader_unload(module);
  6364. return NULL;
  6365. }
  6366. void
  6367. wasm_loader_unload(WASMModule *module)
  6368. {
  6369. uint32 i;
  6370. if (!module)
  6371. return;
  6372. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6373. && WASM_ENABLE_LAZY_JIT != 0
  6374. module->orcjit_stop_compiling = true;
  6375. if (module->llvm_jit_init_thread)
  6376. os_thread_join(module->llvm_jit_init_thread, NULL);
  6377. #endif
  6378. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6379. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6380. module internal data after they were freed */
  6381. orcjit_stop_compile_threads(module);
  6382. #endif
  6383. #if WASM_ENABLE_JIT != 0
  6384. if (module->func_ptrs)
  6385. wasm_runtime_free(module->func_ptrs);
  6386. if (module->comp_ctx)
  6387. aot_destroy_comp_context(module->comp_ctx);
  6388. if (module->comp_data)
  6389. aot_destroy_comp_data(module->comp_data);
  6390. #endif
  6391. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6392. && WASM_ENABLE_LAZY_JIT != 0
  6393. if (module->tierup_wait_lock_inited) {
  6394. os_mutex_destroy(&module->tierup_wait_lock);
  6395. os_cond_destroy(&module->tierup_wait_cond);
  6396. }
  6397. #endif
  6398. if (module->imports)
  6399. wasm_runtime_free(module->imports);
  6400. if (module->functions) {
  6401. for (i = 0; i < module->function_count; i++) {
  6402. if (module->functions[i]) {
  6403. if (module->functions[i]->local_offsets)
  6404. wasm_runtime_free(module->functions[i]->local_offsets);
  6405. #if WASM_ENABLE_FAST_INTERP != 0
  6406. if (module->functions[i]->code_compiled)
  6407. wasm_runtime_free(module->functions[i]->code_compiled);
  6408. if (module->functions[i]->consts)
  6409. wasm_runtime_free(module->functions[i]->consts);
  6410. #endif
  6411. #if WASM_ENABLE_FAST_JIT != 0
  6412. if (module->functions[i]->fast_jit_jitted_code) {
  6413. jit_code_cache_free(
  6414. module->functions[i]->fast_jit_jitted_code);
  6415. }
  6416. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6417. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6418. jit_code_cache_free(
  6419. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6420. }
  6421. #endif
  6422. #endif
  6423. #if WASM_ENABLE_GC != 0
  6424. if (module->functions[i]->local_ref_type_maps) {
  6425. wasm_runtime_free(
  6426. module->functions[i]->local_ref_type_maps);
  6427. }
  6428. #endif
  6429. wasm_runtime_free(module->functions[i]);
  6430. }
  6431. }
  6432. wasm_runtime_free(module->functions);
  6433. }
  6434. if (module->tables) {
  6435. #if WASM_ENABLE_GC != 0
  6436. for (i = 0; i < module->table_count; i++) {
  6437. destroy_init_expr(module, &module->tables[i].init_expr);
  6438. }
  6439. #endif
  6440. wasm_runtime_free(module->tables);
  6441. }
  6442. if (module->memories)
  6443. wasm_runtime_free(module->memories);
  6444. if (module->globals) {
  6445. #if WASM_ENABLE_GC != 0 || WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  6446. for (i = 0; i < module->global_count; i++) {
  6447. destroy_init_expr(module, &module->globals[i].init_expr);
  6448. }
  6449. #endif
  6450. wasm_runtime_free(module->globals);
  6451. }
  6452. #if WASM_ENABLE_TAGS != 0
  6453. if (module->tags) {
  6454. for (i = 0; i < module->tag_count; i++) {
  6455. if (module->tags[i])
  6456. wasm_runtime_free(module->tags[i]);
  6457. }
  6458. wasm_runtime_free(module->tags);
  6459. }
  6460. #endif
  6461. if (module->exports)
  6462. wasm_runtime_free(module->exports);
  6463. if (module->table_segments) {
  6464. for (i = 0; i < module->table_seg_count; i++) {
  6465. if (module->table_segments[i].init_values) {
  6466. #if WASM_ENABLE_GC != 0
  6467. uint32 j;
  6468. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6469. destroy_init_expr(
  6470. module, &module->table_segments[i].init_values[j]);
  6471. }
  6472. #endif
  6473. wasm_runtime_free(module->table_segments[i].init_values);
  6474. }
  6475. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  6476. destroy_init_expr(module, &module->table_segments[i].base_offset);
  6477. #endif
  6478. }
  6479. wasm_runtime_free(module->table_segments);
  6480. }
  6481. if (module->data_segments) {
  6482. for (i = 0; i < module->data_seg_count; i++) {
  6483. if (module->data_segments[i]) {
  6484. if (module->data_segments[i]->is_data_cloned)
  6485. wasm_runtime_free(module->data_segments[i]->data);
  6486. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  6487. destroy_init_expr(module,
  6488. &(module->data_segments[i]->base_offset));
  6489. #endif
  6490. wasm_runtime_free(module->data_segments[i]);
  6491. }
  6492. }
  6493. wasm_runtime_free(module->data_segments);
  6494. }
  6495. if (module->types) {
  6496. for (i = 0; i < module->type_count; i++) {
  6497. if (module->types[i])
  6498. destroy_wasm_type(module->types[i]);
  6499. }
  6500. wasm_runtime_free(module->types);
  6501. }
  6502. if (module->const_str_list) {
  6503. StringNode *node = module->const_str_list, *node_next;
  6504. while (node) {
  6505. node_next = node->next;
  6506. wasm_runtime_free(node);
  6507. node = node_next;
  6508. }
  6509. }
  6510. #if WASM_ENABLE_STRINGREF != 0
  6511. if (module->string_literal_ptrs) {
  6512. wasm_runtime_free((void *)module->string_literal_ptrs);
  6513. }
  6514. if (module->string_literal_lengths) {
  6515. wasm_runtime_free(module->string_literal_lengths);
  6516. }
  6517. #endif
  6518. #if WASM_ENABLE_FAST_INTERP == 0
  6519. if (module->br_table_cache_list) {
  6520. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6521. BrTableCache *node_next;
  6522. while (node) {
  6523. node_next = bh_list_elem_next(node);
  6524. wasm_runtime_free(node);
  6525. node = node_next;
  6526. }
  6527. }
  6528. #endif
  6529. #if WASM_ENABLE_MULTI_MODULE != 0
  6530. /* just release the sub module list */
  6531. if (module->import_module_list) {
  6532. WASMRegisteredModule *node =
  6533. bh_list_first_elem(module->import_module_list);
  6534. while (node) {
  6535. WASMRegisteredModule *next = bh_list_elem_next(node);
  6536. bh_list_remove(module->import_module_list, node);
  6537. /*
  6538. * unload(sub_module) will be triggered during runtime_destroy().
  6539. * every module in the global module list will be unloaded one by
  6540. * one. so don't worry.
  6541. */
  6542. wasm_runtime_free(node);
  6543. /*
  6544. * the module file reading buffer will be released
  6545. * in runtime_destroy()
  6546. */
  6547. node = next;
  6548. }
  6549. }
  6550. #endif
  6551. #if WASM_ENABLE_DEBUG_INTERP != 0
  6552. WASMFastOPCodeNode *fast_opcode =
  6553. bh_list_first_elem(&module->fast_opcode_list);
  6554. while (fast_opcode) {
  6555. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6556. wasm_runtime_free(fast_opcode);
  6557. fast_opcode = next;
  6558. }
  6559. #endif
  6560. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6561. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6562. && WASM_ENABLE_LAZY_JIT != 0)
  6563. os_mutex_destroy(&module->instance_list_lock);
  6564. #endif
  6565. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6566. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6567. #endif
  6568. #if WASM_ENABLE_FAST_JIT != 0
  6569. if (module->fast_jit_func_ptrs) {
  6570. wasm_runtime_free(module->fast_jit_func_ptrs);
  6571. }
  6572. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6573. if (module->fast_jit_thread_locks_inited[i]) {
  6574. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6575. }
  6576. }
  6577. #endif
  6578. #if WASM_ENABLE_GC != 0
  6579. os_mutex_destroy(&module->rtt_type_lock);
  6580. bh_hash_map_destroy(module->ref_type_set);
  6581. if (module->rtt_types) {
  6582. for (i = 0; i < module->type_count; i++) {
  6583. if (module->rtt_types[i])
  6584. wasm_runtime_free(module->rtt_types[i]);
  6585. }
  6586. wasm_runtime_free(module->rtt_types);
  6587. }
  6588. #if WASM_ENABLE_STRINGREF != 0
  6589. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6590. if (module->stringref_rtts[i])
  6591. wasm_runtime_free(module->stringref_rtts[i]);
  6592. }
  6593. #endif
  6594. #endif
  6595. wasm_runtime_free(module);
  6596. }
  6597. bool
  6598. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6599. const uint8 *start_addr, const uint8 *code_end_addr,
  6600. uint8 label_type, uint8 **p_else_addr,
  6601. uint8 **p_end_addr)
  6602. {
  6603. const uint8 *p = start_addr, *p_end = code_end_addr;
  6604. uint8 *else_addr = NULL;
  6605. char error_buf[128];
  6606. uint32 block_nested_depth = 1, count, i, j, t;
  6607. uint32 error_buf_size = sizeof(error_buf);
  6608. uint8 opcode, u8;
  6609. BlockAddr block_stack[16] = { { 0 } }, *block;
  6610. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6611. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6612. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6613. if (block[j].start_addr == start_addr) {
  6614. /* Cache hit */
  6615. *p_else_addr = block[j].else_addr;
  6616. *p_end_addr = block[j].end_addr;
  6617. return true;
  6618. }
  6619. }
  6620. /* Cache unhit */
  6621. block_stack[0].start_addr = start_addr;
  6622. while (p < code_end_addr) {
  6623. opcode = *p++;
  6624. #if WASM_ENABLE_DEBUG_INTERP != 0
  6625. op_break_retry:
  6626. #endif
  6627. switch (opcode) {
  6628. case WASM_OP_UNREACHABLE:
  6629. case WASM_OP_NOP:
  6630. break;
  6631. #if WASM_ENABLE_EXCE_HANDLING != 0
  6632. case WASM_OP_TRY:
  6633. u8 = read_uint8(p);
  6634. if (block_nested_depth
  6635. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6636. block_stack[block_nested_depth].start_addr = p;
  6637. block_stack[block_nested_depth].else_addr = NULL;
  6638. }
  6639. block_nested_depth++;
  6640. break;
  6641. case EXT_OP_TRY:
  6642. skip_leb_uint32(p, p_end);
  6643. if (block_nested_depth
  6644. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6645. block_stack[block_nested_depth].start_addr = p;
  6646. block_stack[block_nested_depth].else_addr = NULL;
  6647. }
  6648. block_nested_depth++;
  6649. break;
  6650. case WASM_OP_CATCH:
  6651. if (block_nested_depth == 1) {
  6652. *p_end_addr = (uint8 *)(p - 1);
  6653. /* stop search and return the address of the catch block */
  6654. return true;
  6655. }
  6656. break;
  6657. case WASM_OP_CATCH_ALL:
  6658. if (block_nested_depth == 1) {
  6659. *p_end_addr = (uint8 *)(p - 1);
  6660. /* stop search and return the address of the catch_all block
  6661. */
  6662. return true;
  6663. }
  6664. break;
  6665. case WASM_OP_THROW:
  6666. /* skip tag_index */
  6667. skip_leb(p);
  6668. break;
  6669. case WASM_OP_RETHROW:
  6670. /* skip depth */
  6671. skip_leb(p);
  6672. break;
  6673. case WASM_OP_DELEGATE:
  6674. if (block_nested_depth == 1) {
  6675. *p_end_addr = (uint8 *)(p - 1);
  6676. return true;
  6677. }
  6678. else {
  6679. skip_leb(p);
  6680. /* the DELEGATE opcode ends the tryblock, */
  6681. block_nested_depth--;
  6682. if (block_nested_depth
  6683. < sizeof(block_stack) / sizeof(BlockAddr))
  6684. block_stack[block_nested_depth].end_addr =
  6685. (uint8 *)(p - 1);
  6686. }
  6687. break;
  6688. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6689. case WASM_OP_BLOCK:
  6690. case WASM_OP_LOOP:
  6691. case WASM_OP_IF:
  6692. {
  6693. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6694. u8 = read_uint8(p);
  6695. if (is_byte_a_type(u8)) {
  6696. #if WASM_ENABLE_GC != 0
  6697. if (wasm_is_type_multi_byte_type(u8)) {
  6698. /* the possible extra bytes of GC ref type have been
  6699. modified to OP_NOP, no need to resolve them again */
  6700. }
  6701. #endif
  6702. }
  6703. else {
  6704. p--;
  6705. /* block type */
  6706. skip_leb_int32(p, p_end);
  6707. }
  6708. if (block_nested_depth
  6709. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6710. block_stack[block_nested_depth].start_addr = p;
  6711. block_stack[block_nested_depth].else_addr = NULL;
  6712. }
  6713. block_nested_depth++;
  6714. break;
  6715. }
  6716. case EXT_OP_BLOCK:
  6717. case EXT_OP_LOOP:
  6718. case EXT_OP_IF:
  6719. /* block type */
  6720. skip_leb_int32(p, p_end);
  6721. if (block_nested_depth
  6722. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6723. block_stack[block_nested_depth].start_addr = p;
  6724. block_stack[block_nested_depth].else_addr = NULL;
  6725. }
  6726. block_nested_depth++;
  6727. break;
  6728. case WASM_OP_ELSE:
  6729. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6730. else_addr = (uint8 *)(p - 1);
  6731. if (block_nested_depth - 1
  6732. < sizeof(block_stack) / sizeof(BlockAddr))
  6733. block_stack[block_nested_depth - 1].else_addr =
  6734. (uint8 *)(p - 1);
  6735. break;
  6736. case WASM_OP_END:
  6737. if (block_nested_depth == 1) {
  6738. if (label_type == LABEL_TYPE_IF)
  6739. *p_else_addr = else_addr;
  6740. *p_end_addr = (uint8 *)(p - 1);
  6741. block_stack[0].end_addr = (uint8 *)(p - 1);
  6742. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6743. t++) {
  6744. start_addr = block_stack[t].start_addr;
  6745. if (start_addr) {
  6746. i = ((uintptr_t)start_addr)
  6747. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6748. block =
  6749. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6750. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6751. if (!block[j].start_addr)
  6752. break;
  6753. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6754. memmove(block + 1, block,
  6755. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6756. * sizeof(BlockAddr));
  6757. j = 0;
  6758. }
  6759. block[j].start_addr = block_stack[t].start_addr;
  6760. block[j].else_addr = block_stack[t].else_addr;
  6761. block[j].end_addr = block_stack[t].end_addr;
  6762. }
  6763. else
  6764. break;
  6765. }
  6766. return true;
  6767. }
  6768. else {
  6769. block_nested_depth--;
  6770. if (block_nested_depth
  6771. < sizeof(block_stack) / sizeof(BlockAddr))
  6772. block_stack[block_nested_depth].end_addr =
  6773. (uint8 *)(p - 1);
  6774. }
  6775. break;
  6776. case WASM_OP_BR:
  6777. case WASM_OP_BR_IF:
  6778. skip_leb_uint32(p, p_end); /* labelidx */
  6779. break;
  6780. case WASM_OP_BR_TABLE:
  6781. read_leb_uint32(p, p_end, count); /* label num */
  6782. #if WASM_ENABLE_FAST_INTERP != 0
  6783. for (i = 0; i <= count; i++) /* labelidxs */
  6784. skip_leb_uint32(p, p_end);
  6785. #else
  6786. p += count + 1;
  6787. while (*p == WASM_OP_NOP)
  6788. p++;
  6789. #endif
  6790. break;
  6791. #if WASM_ENABLE_FAST_INTERP == 0
  6792. case EXT_OP_BR_TABLE_CACHE:
  6793. read_leb_uint32(p, p_end, count); /* label num */
  6794. while (*p == WASM_OP_NOP)
  6795. p++;
  6796. break;
  6797. #endif
  6798. case WASM_OP_RETURN:
  6799. break;
  6800. case WASM_OP_CALL:
  6801. #if WASM_ENABLE_TAIL_CALL != 0
  6802. case WASM_OP_RETURN_CALL:
  6803. #endif
  6804. skip_leb_uint32(p, p_end); /* funcidx */
  6805. break;
  6806. case WASM_OP_CALL_INDIRECT:
  6807. #if WASM_ENABLE_TAIL_CALL != 0
  6808. case WASM_OP_RETURN_CALL_INDIRECT:
  6809. #endif
  6810. skip_leb_uint32(p, p_end); /* typeidx */
  6811. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6812. skip_leb_uint32(p, p_end); /* tableidx */
  6813. #else
  6814. u8 = read_uint8(p); /* 0x00 */
  6815. #endif
  6816. break;
  6817. #if WASM_ENABLE_GC != 0
  6818. case WASM_OP_CALL_REF:
  6819. case WASM_OP_RETURN_CALL_REF:
  6820. skip_leb_uint32(p, p_end); /* typeidx */
  6821. break;
  6822. #endif
  6823. case WASM_OP_DROP:
  6824. case WASM_OP_SELECT:
  6825. case WASM_OP_DROP_64:
  6826. case WASM_OP_SELECT_64:
  6827. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_SIMD != 0
  6828. case WASM_OP_SELECT_128:
  6829. #endif
  6830. break;
  6831. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6832. case WASM_OP_SELECT_T:
  6833. {
  6834. skip_leb_uint32(p, p_end); /* vec length */
  6835. u8 = read_uint8(p); /* typeidx */
  6836. /* the possible extra bytes of GC ref type have been
  6837. modified to OP_NOP, no need to resolve them again */
  6838. break;
  6839. }
  6840. case WASM_OP_TABLE_GET:
  6841. case WASM_OP_TABLE_SET:
  6842. skip_leb_uint32(p, p_end); /* table index */
  6843. break;
  6844. case WASM_OP_REF_NULL:
  6845. {
  6846. u8 = read_uint8(p); /* type */
  6847. if (is_byte_a_type(u8)) {
  6848. #if WASM_ENABLE_GC != 0
  6849. if (wasm_is_type_multi_byte_type(u8)) {
  6850. /* the possible extra bytes of GC ref type have been
  6851. modified to OP_NOP, no need to resolve them again */
  6852. }
  6853. #endif
  6854. }
  6855. else {
  6856. p--;
  6857. skip_leb_uint32(p, p_end);
  6858. }
  6859. break;
  6860. }
  6861. case WASM_OP_REF_IS_NULL:
  6862. break;
  6863. case WASM_OP_REF_FUNC:
  6864. skip_leb_uint32(p, p_end); /* func index */
  6865. break;
  6866. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6867. #if WASM_ENABLE_GC != 0
  6868. case WASM_OP_REF_AS_NON_NULL:
  6869. case WASM_OP_REF_EQ:
  6870. break;
  6871. case WASM_OP_BR_ON_NULL:
  6872. case WASM_OP_BR_ON_NON_NULL:
  6873. skip_leb_uint32(p, p_end); /* label index */
  6874. break;
  6875. #endif /* end of WASM_ENABLE_GC != 0 */
  6876. case WASM_OP_GET_LOCAL:
  6877. case WASM_OP_SET_LOCAL:
  6878. case WASM_OP_TEE_LOCAL:
  6879. case WASM_OP_GET_GLOBAL:
  6880. case WASM_OP_SET_GLOBAL:
  6881. case WASM_OP_GET_GLOBAL_64:
  6882. case WASM_OP_SET_GLOBAL_64:
  6883. #if WASM_ENABLE_SIMDE != 0
  6884. case WASM_OP_GET_GLOBAL_V128:
  6885. case WASM_OP_SET_GLOBAL_V128:
  6886. #endif
  6887. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6888. skip_leb_uint32(p, p_end); /* local index */
  6889. break;
  6890. case EXT_OP_GET_LOCAL_FAST:
  6891. case EXT_OP_SET_LOCAL_FAST:
  6892. case EXT_OP_TEE_LOCAL_FAST:
  6893. CHECK_BUF(p, p_end, 1);
  6894. p++;
  6895. break;
  6896. case WASM_OP_I32_LOAD:
  6897. case WASM_OP_I64_LOAD:
  6898. case WASM_OP_F32_LOAD:
  6899. case WASM_OP_F64_LOAD:
  6900. case WASM_OP_I32_LOAD8_S:
  6901. case WASM_OP_I32_LOAD8_U:
  6902. case WASM_OP_I32_LOAD16_S:
  6903. case WASM_OP_I32_LOAD16_U:
  6904. case WASM_OP_I64_LOAD8_S:
  6905. case WASM_OP_I64_LOAD8_U:
  6906. case WASM_OP_I64_LOAD16_S:
  6907. case WASM_OP_I64_LOAD16_U:
  6908. case WASM_OP_I64_LOAD32_S:
  6909. case WASM_OP_I64_LOAD32_U:
  6910. case WASM_OP_I32_STORE:
  6911. case WASM_OP_I64_STORE:
  6912. case WASM_OP_F32_STORE:
  6913. case WASM_OP_F64_STORE:
  6914. case WASM_OP_I32_STORE8:
  6915. case WASM_OP_I32_STORE16:
  6916. case WASM_OP_I64_STORE8:
  6917. case WASM_OP_I64_STORE16:
  6918. case WASM_OP_I64_STORE32:
  6919. skip_leb_align(p, p_end); /* align */
  6920. skip_leb_mem_offset(p, p_end); /* offset */
  6921. break;
  6922. case WASM_OP_MEMORY_SIZE:
  6923. case WASM_OP_MEMORY_GROW:
  6924. skip_leb_memidx(p, p_end); /* memidx */
  6925. break;
  6926. case WASM_OP_I32_CONST:
  6927. skip_leb_int32(p, p_end);
  6928. break;
  6929. case WASM_OP_I64_CONST:
  6930. skip_leb_int64(p, p_end);
  6931. break;
  6932. case WASM_OP_F32_CONST:
  6933. p += sizeof(float32);
  6934. break;
  6935. case WASM_OP_F64_CONST:
  6936. p += sizeof(float64);
  6937. break;
  6938. case WASM_OP_I32_EQZ:
  6939. case WASM_OP_I32_EQ:
  6940. case WASM_OP_I32_NE:
  6941. case WASM_OP_I32_LT_S:
  6942. case WASM_OP_I32_LT_U:
  6943. case WASM_OP_I32_GT_S:
  6944. case WASM_OP_I32_GT_U:
  6945. case WASM_OP_I32_LE_S:
  6946. case WASM_OP_I32_LE_U:
  6947. case WASM_OP_I32_GE_S:
  6948. case WASM_OP_I32_GE_U:
  6949. case WASM_OP_I64_EQZ:
  6950. case WASM_OP_I64_EQ:
  6951. case WASM_OP_I64_NE:
  6952. case WASM_OP_I64_LT_S:
  6953. case WASM_OP_I64_LT_U:
  6954. case WASM_OP_I64_GT_S:
  6955. case WASM_OP_I64_GT_U:
  6956. case WASM_OP_I64_LE_S:
  6957. case WASM_OP_I64_LE_U:
  6958. case WASM_OP_I64_GE_S:
  6959. case WASM_OP_I64_GE_U:
  6960. case WASM_OP_F32_EQ:
  6961. case WASM_OP_F32_NE:
  6962. case WASM_OP_F32_LT:
  6963. case WASM_OP_F32_GT:
  6964. case WASM_OP_F32_LE:
  6965. case WASM_OP_F32_GE:
  6966. case WASM_OP_F64_EQ:
  6967. case WASM_OP_F64_NE:
  6968. case WASM_OP_F64_LT:
  6969. case WASM_OP_F64_GT:
  6970. case WASM_OP_F64_LE:
  6971. case WASM_OP_F64_GE:
  6972. case WASM_OP_I32_CLZ:
  6973. case WASM_OP_I32_CTZ:
  6974. case WASM_OP_I32_POPCNT:
  6975. case WASM_OP_I32_ADD:
  6976. case WASM_OP_I32_SUB:
  6977. case WASM_OP_I32_MUL:
  6978. case WASM_OP_I32_DIV_S:
  6979. case WASM_OP_I32_DIV_U:
  6980. case WASM_OP_I32_REM_S:
  6981. case WASM_OP_I32_REM_U:
  6982. case WASM_OP_I32_AND:
  6983. case WASM_OP_I32_OR:
  6984. case WASM_OP_I32_XOR:
  6985. case WASM_OP_I32_SHL:
  6986. case WASM_OP_I32_SHR_S:
  6987. case WASM_OP_I32_SHR_U:
  6988. case WASM_OP_I32_ROTL:
  6989. case WASM_OP_I32_ROTR:
  6990. case WASM_OP_I64_CLZ:
  6991. case WASM_OP_I64_CTZ:
  6992. case WASM_OP_I64_POPCNT:
  6993. case WASM_OP_I64_ADD:
  6994. case WASM_OP_I64_SUB:
  6995. case WASM_OP_I64_MUL:
  6996. case WASM_OP_I64_DIV_S:
  6997. case WASM_OP_I64_DIV_U:
  6998. case WASM_OP_I64_REM_S:
  6999. case WASM_OP_I64_REM_U:
  7000. case WASM_OP_I64_AND:
  7001. case WASM_OP_I64_OR:
  7002. case WASM_OP_I64_XOR:
  7003. case WASM_OP_I64_SHL:
  7004. case WASM_OP_I64_SHR_S:
  7005. case WASM_OP_I64_SHR_U:
  7006. case WASM_OP_I64_ROTL:
  7007. case WASM_OP_I64_ROTR:
  7008. case WASM_OP_F32_ABS:
  7009. case WASM_OP_F32_NEG:
  7010. case WASM_OP_F32_CEIL:
  7011. case WASM_OP_F32_FLOOR:
  7012. case WASM_OP_F32_TRUNC:
  7013. case WASM_OP_F32_NEAREST:
  7014. case WASM_OP_F32_SQRT:
  7015. case WASM_OP_F32_ADD:
  7016. case WASM_OP_F32_SUB:
  7017. case WASM_OP_F32_MUL:
  7018. case WASM_OP_F32_DIV:
  7019. case WASM_OP_F32_MIN:
  7020. case WASM_OP_F32_MAX:
  7021. case WASM_OP_F32_COPYSIGN:
  7022. case WASM_OP_F64_ABS:
  7023. case WASM_OP_F64_NEG:
  7024. case WASM_OP_F64_CEIL:
  7025. case WASM_OP_F64_FLOOR:
  7026. case WASM_OP_F64_TRUNC:
  7027. case WASM_OP_F64_NEAREST:
  7028. case WASM_OP_F64_SQRT:
  7029. case WASM_OP_F64_ADD:
  7030. case WASM_OP_F64_SUB:
  7031. case WASM_OP_F64_MUL:
  7032. case WASM_OP_F64_DIV:
  7033. case WASM_OP_F64_MIN:
  7034. case WASM_OP_F64_MAX:
  7035. case WASM_OP_F64_COPYSIGN:
  7036. case WASM_OP_I32_WRAP_I64:
  7037. case WASM_OP_I32_TRUNC_S_F32:
  7038. case WASM_OP_I32_TRUNC_U_F32:
  7039. case WASM_OP_I32_TRUNC_S_F64:
  7040. case WASM_OP_I32_TRUNC_U_F64:
  7041. case WASM_OP_I64_EXTEND_S_I32:
  7042. case WASM_OP_I64_EXTEND_U_I32:
  7043. case WASM_OP_I64_TRUNC_S_F32:
  7044. case WASM_OP_I64_TRUNC_U_F32:
  7045. case WASM_OP_I64_TRUNC_S_F64:
  7046. case WASM_OP_I64_TRUNC_U_F64:
  7047. case WASM_OP_F32_CONVERT_S_I32:
  7048. case WASM_OP_F32_CONVERT_U_I32:
  7049. case WASM_OP_F32_CONVERT_S_I64:
  7050. case WASM_OP_F32_CONVERT_U_I64:
  7051. case WASM_OP_F32_DEMOTE_F64:
  7052. case WASM_OP_F64_CONVERT_S_I32:
  7053. case WASM_OP_F64_CONVERT_U_I32:
  7054. case WASM_OP_F64_CONVERT_S_I64:
  7055. case WASM_OP_F64_CONVERT_U_I64:
  7056. case WASM_OP_F64_PROMOTE_F32:
  7057. case WASM_OP_I32_REINTERPRET_F32:
  7058. case WASM_OP_I64_REINTERPRET_F64:
  7059. case WASM_OP_F32_REINTERPRET_I32:
  7060. case WASM_OP_F64_REINTERPRET_I64:
  7061. case WASM_OP_I32_EXTEND8_S:
  7062. case WASM_OP_I32_EXTEND16_S:
  7063. case WASM_OP_I64_EXTEND8_S:
  7064. case WASM_OP_I64_EXTEND16_S:
  7065. case WASM_OP_I64_EXTEND32_S:
  7066. break;
  7067. #if WASM_ENABLE_GC != 0
  7068. case WASM_OP_GC_PREFIX:
  7069. {
  7070. uint32 opcode1;
  7071. read_leb_uint32(p, p_end, opcode1);
  7072. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7073. is no larger than UINT8_MAX */
  7074. opcode = (uint8)opcode1;
  7075. switch (opcode) {
  7076. case WASM_OP_STRUCT_NEW:
  7077. case WASM_OP_STRUCT_NEW_DEFAULT:
  7078. skip_leb_uint32(p, p_end); /* typeidx */
  7079. break;
  7080. case WASM_OP_STRUCT_GET:
  7081. case WASM_OP_STRUCT_GET_S:
  7082. case WASM_OP_STRUCT_GET_U:
  7083. case WASM_OP_STRUCT_SET:
  7084. skip_leb_uint32(p, p_end); /* typeidx */
  7085. skip_leb_uint32(p, p_end); /* fieldidx */
  7086. break;
  7087. case WASM_OP_ARRAY_NEW:
  7088. case WASM_OP_ARRAY_NEW_DEFAULT:
  7089. case WASM_OP_ARRAY_GET:
  7090. case WASM_OP_ARRAY_GET_S:
  7091. case WASM_OP_ARRAY_GET_U:
  7092. case WASM_OP_ARRAY_SET:
  7093. case WASM_OP_ARRAY_FILL:
  7094. skip_leb_uint32(p, p_end); /* typeidx */
  7095. break;
  7096. case WASM_OP_ARRAY_COPY:
  7097. skip_leb_uint32(p, p_end); /* typeidx1 */
  7098. skip_leb_uint32(p, p_end); /* typeidx2 */
  7099. break;
  7100. case WASM_OP_ARRAY_LEN:
  7101. break;
  7102. case WASM_OP_ARRAY_NEW_FIXED:
  7103. case WASM_OP_ARRAY_NEW_DATA:
  7104. case WASM_OP_ARRAY_NEW_ELEM:
  7105. skip_leb_uint32(p, p_end); /* typeidx */
  7106. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  7107. break;
  7108. case WASM_OP_REF_I31:
  7109. case WASM_OP_I31_GET_S:
  7110. case WASM_OP_I31_GET_U:
  7111. break;
  7112. case WASM_OP_REF_TEST:
  7113. case WASM_OP_REF_CAST:
  7114. case WASM_OP_REF_TEST_NULLABLE:
  7115. case WASM_OP_REF_CAST_NULLABLE:
  7116. skip_leb_int32(p, p_end); /* heaptype */
  7117. break;
  7118. case WASM_OP_BR_ON_CAST:
  7119. case WASM_OP_BR_ON_CAST_FAIL:
  7120. p += sizeof(uint8); /* castflag */
  7121. skip_leb_uint32(p, p_end); /* labelidx */
  7122. skip_leb_int32(p, p_end); /* heaptype */
  7123. skip_leb_int32(p, p_end); /* heaptype2 */
  7124. break;
  7125. case WASM_OP_ANY_CONVERT_EXTERN:
  7126. case WASM_OP_EXTERN_CONVERT_ANY:
  7127. break;
  7128. #if WASM_ENABLE_STRINGREF != 0
  7129. case WASM_OP_STRING_NEW_UTF8:
  7130. case WASM_OP_STRING_NEW_WTF16:
  7131. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  7132. case WASM_OP_STRING_NEW_WTF8:
  7133. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  7134. break;
  7135. case WASM_OP_STRING_CONST:
  7136. skip_leb_int32(p, p_end); /* contents */
  7137. break;
  7138. case WASM_OP_STRING_MEASURE_UTF8:
  7139. case WASM_OP_STRING_MEASURE_WTF8:
  7140. case WASM_OP_STRING_MEASURE_WTF16:
  7141. break;
  7142. case WASM_OP_STRING_ENCODE_UTF8:
  7143. case WASM_OP_STRING_ENCODE_WTF16:
  7144. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  7145. case WASM_OP_STRING_ENCODE_WTF8:
  7146. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  7147. break;
  7148. case WASM_OP_STRING_CONCAT:
  7149. case WASM_OP_STRING_EQ:
  7150. case WASM_OP_STRING_IS_USV_SEQUENCE:
  7151. case WASM_OP_STRING_AS_WTF8:
  7152. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  7153. break;
  7154. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  7155. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  7156. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  7157. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  7158. break;
  7159. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  7160. case WASM_OP_STRING_AS_WTF16:
  7161. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  7162. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  7163. break;
  7164. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  7165. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  7166. break;
  7167. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  7168. case WASM_OP_STRING_AS_ITER:
  7169. case WASM_OP_STRINGVIEW_ITER_NEXT:
  7170. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  7171. case WASM_OP_STRINGVIEW_ITER_REWIND:
  7172. case WASM_OP_STRINGVIEW_ITER_SLICE:
  7173. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  7174. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  7175. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  7176. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  7177. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  7178. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  7179. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  7180. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  7181. break;
  7182. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  7183. default:
  7184. return false;
  7185. }
  7186. break;
  7187. }
  7188. #endif /* end of WASM_ENABLE_GC != 0 */
  7189. case WASM_OP_MISC_PREFIX:
  7190. {
  7191. uint32 opcode1;
  7192. read_leb_uint32(p, p_end, opcode1);
  7193. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7194. is no larger than UINT8_MAX */
  7195. opcode = (uint8)opcode1;
  7196. switch (opcode) {
  7197. case WASM_OP_I32_TRUNC_SAT_S_F32:
  7198. case WASM_OP_I32_TRUNC_SAT_U_F32:
  7199. case WASM_OP_I32_TRUNC_SAT_S_F64:
  7200. case WASM_OP_I32_TRUNC_SAT_U_F64:
  7201. case WASM_OP_I64_TRUNC_SAT_S_F32:
  7202. case WASM_OP_I64_TRUNC_SAT_U_F32:
  7203. case WASM_OP_I64_TRUNC_SAT_S_F64:
  7204. case WASM_OP_I64_TRUNC_SAT_U_F64:
  7205. break;
  7206. #if WASM_ENABLE_BULK_MEMORY != 0
  7207. case WASM_OP_MEMORY_INIT:
  7208. skip_leb_uint32(p, p_end);
  7209. skip_leb_memidx(p, p_end);
  7210. break;
  7211. case WASM_OP_DATA_DROP:
  7212. skip_leb_uint32(p, p_end);
  7213. break;
  7214. case WASM_OP_MEMORY_COPY:
  7215. skip_leb_memidx(p, p_end);
  7216. skip_leb_memidx(p, p_end);
  7217. break;
  7218. case WASM_OP_MEMORY_FILL:
  7219. skip_leb_memidx(p, p_end);
  7220. break;
  7221. #endif /* WASM_ENABLE_BULK_MEMORY */
  7222. #if WASM_ENABLE_REF_TYPES != 0
  7223. case WASM_OP_TABLE_INIT:
  7224. case WASM_OP_TABLE_COPY:
  7225. /* tableidx */
  7226. skip_leb_uint32(p, p_end);
  7227. /* elemidx */
  7228. skip_leb_uint32(p, p_end);
  7229. break;
  7230. case WASM_OP_ELEM_DROP:
  7231. /* elemidx */
  7232. skip_leb_uint32(p, p_end);
  7233. break;
  7234. case WASM_OP_TABLE_SIZE:
  7235. case WASM_OP_TABLE_GROW:
  7236. case WASM_OP_TABLE_FILL:
  7237. skip_leb_uint32(p, p_end); /* table idx */
  7238. break;
  7239. #endif /* WASM_ENABLE_REF_TYPES */
  7240. default:
  7241. return false;
  7242. }
  7243. break;
  7244. }
  7245. #if WASM_ENABLE_SIMD != 0
  7246. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  7247. || (WASM_ENABLE_FAST_INTERP != 0)
  7248. case WASM_OP_SIMD_PREFIX:
  7249. {
  7250. uint32 opcode1;
  7251. read_leb_uint32(p, p_end, opcode1);
  7252. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7253. is no larger than UINT8_MAX */
  7254. opcode = (uint8)opcode1;
  7255. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  7256. */
  7257. switch (opcode) {
  7258. case SIMD_v128_load:
  7259. case SIMD_v128_load8x8_s:
  7260. case SIMD_v128_load8x8_u:
  7261. case SIMD_v128_load16x4_s:
  7262. case SIMD_v128_load16x4_u:
  7263. case SIMD_v128_load32x2_s:
  7264. case SIMD_v128_load32x2_u:
  7265. case SIMD_v128_load8_splat:
  7266. case SIMD_v128_load16_splat:
  7267. case SIMD_v128_load32_splat:
  7268. case SIMD_v128_load64_splat:
  7269. case SIMD_v128_store:
  7270. /* memarg align */
  7271. skip_leb_uint32(p, p_end);
  7272. /* memarg offset */
  7273. skip_leb_mem_offset(p, p_end);
  7274. break;
  7275. case SIMD_v128_const:
  7276. case SIMD_v8x16_shuffle:
  7277. /* immByte[16] immLaneId[16] */
  7278. CHECK_BUF1(p, p_end, 16);
  7279. p += 16;
  7280. break;
  7281. case SIMD_i8x16_extract_lane_s:
  7282. case SIMD_i8x16_extract_lane_u:
  7283. case SIMD_i8x16_replace_lane:
  7284. case SIMD_i16x8_extract_lane_s:
  7285. case SIMD_i16x8_extract_lane_u:
  7286. case SIMD_i16x8_replace_lane:
  7287. case SIMD_i32x4_extract_lane:
  7288. case SIMD_i32x4_replace_lane:
  7289. case SIMD_i64x2_extract_lane:
  7290. case SIMD_i64x2_replace_lane:
  7291. case SIMD_f32x4_extract_lane:
  7292. case SIMD_f32x4_replace_lane:
  7293. case SIMD_f64x2_extract_lane:
  7294. case SIMD_f64x2_replace_lane:
  7295. /* ImmLaneId */
  7296. CHECK_BUF(p, p_end, 1);
  7297. p++;
  7298. break;
  7299. case SIMD_v128_load8_lane:
  7300. case SIMD_v128_load16_lane:
  7301. case SIMD_v128_load32_lane:
  7302. case SIMD_v128_load64_lane:
  7303. case SIMD_v128_store8_lane:
  7304. case SIMD_v128_store16_lane:
  7305. case SIMD_v128_store32_lane:
  7306. case SIMD_v128_store64_lane:
  7307. /* memarg align */
  7308. skip_leb_uint32(p, p_end);
  7309. /* memarg offset */
  7310. skip_leb_mem_offset(p, p_end);
  7311. /* ImmLaneId */
  7312. CHECK_BUF(p, p_end, 1);
  7313. p++;
  7314. break;
  7315. case SIMD_v128_load32_zero:
  7316. case SIMD_v128_load64_zero:
  7317. /* memarg align */
  7318. skip_leb_uint32(p, p_end);
  7319. /* memarg offset */
  7320. skip_leb_mem_offset(p, p_end);
  7321. break;
  7322. default:
  7323. /*
  7324. * since latest SIMD specific used almost every value
  7325. * from 0x00 to 0xff, the default branch will present
  7326. * all opcodes without imm
  7327. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  7328. */
  7329. break;
  7330. }
  7331. break;
  7332. }
  7333. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  7334. (WASM_ENABLE_FAST_INTERP != 0) */
  7335. #endif /* end of WASM_ENABLE_SIMD */
  7336. #if WASM_ENABLE_SHARED_MEMORY != 0
  7337. case WASM_OP_ATOMIC_PREFIX:
  7338. {
  7339. uint32 opcode1;
  7340. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  7341. read_leb_uint32(p, p_end, opcode1);
  7342. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7343. is no larger than UINT8_MAX */
  7344. opcode = (uint8)opcode1;
  7345. if (opcode != WASM_OP_ATOMIC_FENCE) {
  7346. skip_leb_uint32(p, p_end); /* align */
  7347. skip_leb_mem_offset(p, p_end); /* offset */
  7348. }
  7349. else {
  7350. /* atomic.fence doesn't have memarg */
  7351. p++;
  7352. }
  7353. break;
  7354. }
  7355. #endif
  7356. #if WASM_ENABLE_DEBUG_INTERP != 0
  7357. case DEBUG_OP_BREAK:
  7358. {
  7359. WASMDebugInstance *debug_instance =
  7360. wasm_exec_env_get_instance(exec_env);
  7361. char original_opcode[1];
  7362. uint64 size = 1;
  7363. WASMModuleInstance *module_inst =
  7364. (WASMModuleInstance *)exec_env->module_inst;
  7365. uint64 offset = (p - 1) >= module_inst->module->load_addr
  7366. ? (p - 1) - module_inst->module->load_addr
  7367. : ~0;
  7368. if (debug_instance) {
  7369. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  7370. original_opcode, &size)
  7371. && size == 1) {
  7372. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  7373. "with %02x: ",
  7374. original_opcode[0]);
  7375. opcode = original_opcode[0];
  7376. goto op_break_retry;
  7377. }
  7378. }
  7379. break;
  7380. }
  7381. #endif
  7382. default:
  7383. return false;
  7384. }
  7385. }
  7386. (void)u8;
  7387. (void)exec_env;
  7388. return false;
  7389. fail:
  7390. return false;
  7391. }
  7392. #if WASM_ENABLE_FAST_INTERP != 0
  7393. #if WASM_DEBUG_PREPROCESSOR != 0
  7394. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7395. #else
  7396. #define LOG_OP(...) (void)0
  7397. #endif
  7398. #define PATCH_ELSE 0
  7399. #define PATCH_END 1
  7400. typedef struct BranchBlockPatch {
  7401. struct BranchBlockPatch *next;
  7402. uint8 patch_type;
  7403. uint8 *code_compiled;
  7404. } BranchBlockPatch;
  7405. #endif
  7406. typedef struct BranchBlock {
  7407. uint8 label_type;
  7408. BlockType block_type;
  7409. uint8 *start_addr;
  7410. uint8 *else_addr;
  7411. uint8 *end_addr;
  7412. uint32 stack_cell_num;
  7413. #if WASM_ENABLE_GC != 0
  7414. uint32 reftype_map_num;
  7415. /* Indicate which local is used inside current block, used to validate
  7416. * local.get with non-nullable ref types */
  7417. uint8 *local_use_mask;
  7418. uint32 local_use_mask_size;
  7419. #endif
  7420. #if WASM_ENABLE_FAST_INTERP != 0
  7421. uint16 dynamic_offset;
  7422. uint8 *code_compiled;
  7423. BranchBlockPatch *patch_list;
  7424. /* This is used to save params frame_offset of of if block */
  7425. int16 *param_frame_offsets;
  7426. /* This is used to recover the dynamic offset for else branch,
  7427. * and also to remember the start offset of dynamic space which
  7428. * stores the block arguments for loop block, so we can use it
  7429. * to copy the stack operands to the loop block's arguments in
  7430. * wasm_loader_emit_br_info for opcode br. */
  7431. uint16 start_dynamic_offset;
  7432. #endif
  7433. /* Indicate the operand stack is in polymorphic state.
  7434. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7435. * to polymorphic state until the block's 'end' opcode is processed.
  7436. * If stack is in polymorphic state and stack is empty, instruction can
  7437. * pop any type of value directly without decreasing stack top pointer
  7438. * and stack cell num. */
  7439. bool is_stack_polymorphic;
  7440. } BranchBlock;
  7441. typedef struct WASMLoaderContext {
  7442. /* frame ref stack */
  7443. uint8 *frame_ref;
  7444. uint8 *frame_ref_bottom;
  7445. uint8 *frame_ref_boundary;
  7446. uint32 frame_ref_size;
  7447. uint32 stack_cell_num;
  7448. uint32 max_stack_cell_num;
  7449. #if WASM_ENABLE_GC != 0
  7450. /* frame reftype map stack */
  7451. WASMRefTypeMap *frame_reftype_map;
  7452. WASMRefTypeMap *frame_reftype_map_bottom;
  7453. WASMRefTypeMap *frame_reftype_map_boundary;
  7454. uint32 frame_reftype_map_size;
  7455. uint32 reftype_map_num;
  7456. uint32 max_reftype_map_num;
  7457. /* Current module */
  7458. WASMModule *module;
  7459. /* Current module's ref_type_set */
  7460. HashMap *ref_type_set;
  7461. /* Always point to local variable ref_type of
  7462. wasm_loader_prepare_bytecode */
  7463. WASMRefType *ref_type_tmp;
  7464. #endif
  7465. /* frame csp stack */
  7466. BranchBlock *frame_csp;
  7467. BranchBlock *frame_csp_bottom;
  7468. BranchBlock *frame_csp_boundary;
  7469. uint32 frame_csp_size;
  7470. uint32 csp_num;
  7471. uint32 max_csp_num;
  7472. #if WASM_ENABLE_FAST_INTERP != 0
  7473. /* frame offset stack */
  7474. int16 *frame_offset;
  7475. int16 *frame_offset_bottom;
  7476. int16 *frame_offset_boundary;
  7477. uint32 frame_offset_size;
  7478. int16 dynamic_offset;
  7479. int16 start_dynamic_offset;
  7480. int16 max_dynamic_offset;
  7481. /* preserved local offset */
  7482. int16 preserved_local_offset;
  7483. /* const buffer for i64 and f64 consts, note that the raw bytes
  7484. * of i64 and f64 are the same, so we read an i64 value from an
  7485. * f64 const with its raw bytes, something like `*(int64 *)&f64 */
  7486. int64 *i64_consts;
  7487. uint32 i64_const_max_num;
  7488. uint32 i64_const_num;
  7489. /* const buffer for i32 and f32 consts */
  7490. int32 *i32_consts;
  7491. uint32 i32_const_max_num;
  7492. uint32 i32_const_num;
  7493. /* const buffer for V128 */
  7494. V128 *v128_consts;
  7495. uint32 v128_const_max_num;
  7496. uint32 v128_const_num;
  7497. /* processed code */
  7498. uint8 *p_code_compiled;
  7499. uint8 *p_code_compiled_end;
  7500. uint32 code_compiled_size;
  7501. /* If the last opcode will be dropped, the peak memory usage will be larger
  7502. * than the final code_compiled_size, we record the peak size to ensure
  7503. * there will not be invalid memory access during second traverse */
  7504. uint32 code_compiled_peak_size;
  7505. #endif
  7506. } WASMLoaderContext;
  7507. #define CHECK_CSP_PUSH() \
  7508. do { \
  7509. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7510. MEM_REALLOC( \
  7511. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7512. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7513. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7514. ctx->frame_csp_boundary = \
  7515. ctx->frame_csp_bottom \
  7516. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7517. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7518. } \
  7519. } while (0)
  7520. #define CHECK_CSP_POP() \
  7521. do { \
  7522. if (ctx->csp_num < 1) { \
  7523. set_error_buf(error_buf, error_buf_size, \
  7524. "type mismatch: " \
  7525. "expect data but block stack was empty"); \
  7526. goto fail; \
  7527. } \
  7528. } while (0)
  7529. #if WASM_ENABLE_FAST_INTERP != 0
  7530. static bool
  7531. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7532. uint32 error_buf_size)
  7533. {
  7534. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7535. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7536. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7537. ctx->frame_offset_size + 16);
  7538. ctx->frame_offset_size += 16;
  7539. ctx->frame_offset_boundary =
  7540. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7541. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7542. }
  7543. return true;
  7544. fail:
  7545. return false;
  7546. }
  7547. static bool
  7548. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7549. {
  7550. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7551. return false;
  7552. return true;
  7553. }
  7554. static void
  7555. free_label_patch_list(BranchBlock *frame_csp)
  7556. {
  7557. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7558. BranchBlockPatch *next;
  7559. while (label_patch != NULL) {
  7560. next = label_patch->next;
  7561. wasm_runtime_free(label_patch);
  7562. label_patch = next;
  7563. }
  7564. frame_csp->patch_list = NULL;
  7565. }
  7566. static void
  7567. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7568. {
  7569. BranchBlock *tmp_csp = frame_csp;
  7570. uint32 i;
  7571. for (i = 0; i < csp_num; i++) {
  7572. free_label_patch_list(tmp_csp);
  7573. tmp_csp++;
  7574. }
  7575. }
  7576. static void
  7577. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7578. {
  7579. BranchBlock *tmp_csp = frame_csp;
  7580. uint32 i;
  7581. for (i = 0; i < csp_num; i++) {
  7582. if (tmp_csp->param_frame_offsets)
  7583. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7584. tmp_csp++;
  7585. }
  7586. }
  7587. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7588. #if WASM_ENABLE_GC != 0
  7589. static bool
  7590. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7591. char *error_buf, uint32 error_buf_size)
  7592. {
  7593. BranchBlock *current_csp = ctx->frame_csp - 1;
  7594. uint32 local_mask_size;
  7595. if (local_count == 0) {
  7596. current_csp->local_use_mask_size = 0;
  7597. return true;
  7598. }
  7599. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7600. * else branch, we don't need to allocate memory again */
  7601. if (!current_csp->local_use_mask) {
  7602. local_mask_size = (local_count + 7) / sizeof(uint8);
  7603. if (!(current_csp->local_use_mask =
  7604. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7605. return false;
  7606. }
  7607. current_csp->local_use_mask_size = local_mask_size;
  7608. }
  7609. else {
  7610. local_mask_size = current_csp->local_use_mask_size;
  7611. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7612. }
  7613. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7614. /* For non-function blocks, inherit the use status from parent block */
  7615. BranchBlock *parent_csp = current_csp - 1;
  7616. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7617. bh_assert(parent_csp->local_use_mask);
  7618. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7619. parent_csp->local_use_mask, local_mask_size);
  7620. }
  7621. return true;
  7622. }
  7623. static void
  7624. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7625. {
  7626. BranchBlock *current_csp = ctx->frame_csp - 1;
  7627. bh_assert(current_csp->local_use_mask
  7628. || current_csp->local_use_mask_size == 0);
  7629. if (current_csp->local_use_mask) {
  7630. wasm_runtime_free(current_csp->local_use_mask);
  7631. }
  7632. current_csp->local_use_mask = NULL;
  7633. current_csp->local_use_mask_size = 0;
  7634. }
  7635. static void
  7636. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7637. {
  7638. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7639. uint32 i;
  7640. for (i = 0; i < ctx->csp_num; i++) {
  7641. if (tmp_csp->local_use_mask) {
  7642. wasm_runtime_free(tmp_csp->local_use_mask);
  7643. tmp_csp->local_use_mask = NULL;
  7644. tmp_csp->local_use_mask_size = 0;
  7645. }
  7646. tmp_csp++;
  7647. }
  7648. }
  7649. static void
  7650. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7651. {
  7652. BranchBlock *current_csp = ctx->frame_csp - 1;
  7653. uint32 byte_offset = index / sizeof(uint8);
  7654. uint32 bit_offset = index % sizeof(uint8);
  7655. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7656. bh_assert(current_csp->local_use_mask);
  7657. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7658. }
  7659. static bool
  7660. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7661. {
  7662. BranchBlock *current_csp = ctx->frame_csp - 1;
  7663. uint32 byte_offset = index / sizeof(uint8);
  7664. uint32 bit_offset = index % sizeof(uint8);
  7665. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7666. bh_assert(current_csp->local_use_mask);
  7667. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7668. ? true
  7669. : false;
  7670. }
  7671. #endif /* end of WASM_ENABLE_GC != 0 */
  7672. static void
  7673. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7674. {
  7675. if (ctx) {
  7676. if (ctx->frame_ref_bottom)
  7677. wasm_runtime_free(ctx->frame_ref_bottom);
  7678. #if WASM_ENABLE_GC != 0
  7679. if (ctx->frame_reftype_map_bottom)
  7680. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7681. #endif
  7682. if (ctx->frame_csp_bottom) {
  7683. #if WASM_ENABLE_FAST_INTERP != 0
  7684. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7685. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7686. ctx->csp_num);
  7687. #endif
  7688. #if WASM_ENABLE_GC != 0
  7689. wasm_loader_clean_all_local_use_masks(ctx);
  7690. #endif
  7691. wasm_runtime_free(ctx->frame_csp_bottom);
  7692. }
  7693. #if WASM_ENABLE_FAST_INTERP != 0
  7694. if (ctx->frame_offset_bottom)
  7695. wasm_runtime_free(ctx->frame_offset_bottom);
  7696. if (ctx->i64_consts)
  7697. wasm_runtime_free(ctx->i64_consts);
  7698. if (ctx->i32_consts)
  7699. wasm_runtime_free(ctx->i32_consts);
  7700. if (ctx->v128_consts)
  7701. wasm_runtime_free(ctx->v128_consts);
  7702. #endif
  7703. wasm_runtime_free(ctx);
  7704. }
  7705. }
  7706. static WASMLoaderContext *
  7707. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7708. {
  7709. WASMLoaderContext *loader_ctx =
  7710. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7711. if (!loader_ctx)
  7712. return NULL;
  7713. loader_ctx->frame_ref_size = 32;
  7714. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7715. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7716. goto fail;
  7717. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7718. #if WASM_ENABLE_GC != 0
  7719. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7720. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7721. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7722. error_buf_size)))
  7723. goto fail;
  7724. loader_ctx->frame_reftype_map_boundary =
  7725. loader_ctx->frame_reftype_map_bottom + 16;
  7726. #endif
  7727. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7728. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7729. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7730. goto fail;
  7731. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7732. #if WASM_ENABLE_EXCE_HANDLING != 0
  7733. func->exception_handler_count = 0;
  7734. #endif
  7735. #if WASM_ENABLE_FAST_INTERP != 0
  7736. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7737. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7738. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7739. error_buf_size)))
  7740. goto fail;
  7741. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7742. loader_ctx->i64_const_max_num = 8;
  7743. if (!(loader_ctx->i64_consts =
  7744. loader_malloc(sizeof(int64) * loader_ctx->i64_const_max_num,
  7745. error_buf, error_buf_size)))
  7746. goto fail;
  7747. loader_ctx->i32_const_max_num = 8;
  7748. if (!(loader_ctx->i32_consts =
  7749. loader_malloc(sizeof(int32) * loader_ctx->i32_const_max_num,
  7750. error_buf, error_buf_size)))
  7751. goto fail;
  7752. loader_ctx->v128_const_max_num = 8;
  7753. if (!(loader_ctx->v128_consts =
  7754. loader_malloc(sizeof(V128) * loader_ctx->v128_const_max_num,
  7755. error_buf, error_buf_size)))
  7756. goto fail;
  7757. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7758. set_error_buf(error_buf, error_buf_size,
  7759. "fast interpreter offset overflow");
  7760. goto fail;
  7761. }
  7762. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7763. loader_ctx->max_dynamic_offset =
  7764. func->param_cell_num + func->local_cell_num;
  7765. #endif
  7766. return loader_ctx;
  7767. fail:
  7768. wasm_loader_ctx_destroy(loader_ctx);
  7769. return NULL;
  7770. }
  7771. static bool
  7772. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7773. uint32 error_buf_size)
  7774. {
  7775. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7776. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7777. /* Increase the frame ref stack */
  7778. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7779. ctx->frame_ref_size + 16);
  7780. ctx->frame_ref_size += 16;
  7781. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7782. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7783. }
  7784. #if WASM_ENABLE_GC != 0
  7785. if (wasm_is_type_multi_byte_type(type)
  7786. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7787. /* Increase the frame reftype map stack */
  7788. bh_assert(
  7789. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7790. * sizeof(WASMRefTypeMap))
  7791. == ctx->frame_reftype_map_size);
  7792. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7793. ctx->frame_reftype_map_size
  7794. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7795. ctx->frame_reftype_map =
  7796. ctx->frame_reftype_map_bottom
  7797. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7798. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7799. ctx->frame_reftype_map_boundary =
  7800. ctx->frame_reftype_map_bottom
  7801. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7802. }
  7803. #endif
  7804. return true;
  7805. fail:
  7806. return false;
  7807. }
  7808. static bool
  7809. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7810. uint32 error_buf_size)
  7811. {
  7812. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7813. uint32 i;
  7814. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7815. return false;
  7816. #if WASM_ENABLE_GC != 0
  7817. if (wasm_is_type_multi_byte_type(type)) {
  7818. WASMRefType *ref_type;
  7819. if (!(ref_type =
  7820. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7821. error_buf, error_buf_size))) {
  7822. return false;
  7823. }
  7824. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7825. /* Increase the frame reftype map stack */
  7826. bh_assert((uint32)((ctx->frame_reftype_map
  7827. - ctx->frame_reftype_map_bottom)
  7828. * sizeof(WASMRefTypeMap))
  7829. == ctx->frame_reftype_map_size);
  7830. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7831. ctx->frame_reftype_map_size,
  7832. ctx->frame_reftype_map_size
  7833. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7834. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7835. + ctx->frame_reftype_map_size
  7836. / ((uint32)sizeof(WASMRefTypeMap));
  7837. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7838. ctx->frame_reftype_map_boundary =
  7839. ctx->frame_reftype_map_bottom
  7840. + ctx->frame_reftype_map_size
  7841. / ((uint32)sizeof(WASMRefTypeMap));
  7842. }
  7843. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7844. ctx->frame_reftype_map->ref_type = ref_type;
  7845. ctx->frame_reftype_map++;
  7846. ctx->reftype_map_num++;
  7847. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7848. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7849. }
  7850. #endif
  7851. for (i = 0; i < type_cell_num; i++)
  7852. *ctx->frame_ref++ = type;
  7853. ctx->stack_cell_num += type_cell_num;
  7854. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7855. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7856. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7857. set_error_buf(error_buf, error_buf_size,
  7858. "operand stack depth limit exceeded");
  7859. return false;
  7860. }
  7861. }
  7862. return true;
  7863. #if WASM_ENABLE_GC != 0
  7864. fail:
  7865. return false;
  7866. #endif
  7867. }
  7868. static bool
  7869. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7870. int32 stack_cell_num,
  7871. #if WASM_ENABLE_GC != 0
  7872. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7873. #endif
  7874. uint8 type,
  7875. #if WASM_ENABLE_GC != 0
  7876. WASMRefType *ref_type,
  7877. #endif
  7878. char *error_buf, uint32 error_buf_size)
  7879. {
  7880. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7881. #if WASM_ENABLE_GC != 0
  7882. WASMRefType *frame_reftype = NULL;
  7883. #endif
  7884. if (stack_cell_num < type_cell_num) {
  7885. set_error_buf(error_buf, error_buf_size,
  7886. "type mismatch: expect data but stack was empty");
  7887. return false;
  7888. }
  7889. #if WASM_ENABLE_GC == 0
  7890. for (i = 0; i < type_cell_num; i++) {
  7891. if (*(frame_ref - 1 - i) != type) {
  7892. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7893. "type mismatch: expect ", type2str(type),
  7894. " but got other");
  7895. return false;
  7896. }
  7897. }
  7898. #else
  7899. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7900. bh_assert(reftype_map_num > 0);
  7901. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7902. }
  7903. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7904. ref_type, ctx->module->types,
  7905. ctx->module->type_count)) {
  7906. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7907. "type mismatch: expect ", type2str(type),
  7908. " but got other");
  7909. return false;
  7910. }
  7911. for (i = 0; i < type_cell_num - 1; i++) {
  7912. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7913. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7914. "type mismatch: expect ", type2str(type),
  7915. " but got other");
  7916. return false;
  7917. }
  7918. }
  7919. #endif
  7920. return true;
  7921. }
  7922. static bool
  7923. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7924. uint32 error_buf_size)
  7925. {
  7926. int32 block_stack_cell_num =
  7927. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7928. #if WASM_ENABLE_GC != 0
  7929. int32 reftype_map_num =
  7930. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7931. #endif
  7932. if (block_stack_cell_num > 0) {
  7933. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7934. /* the stack top is a value of any type, return success */
  7935. return true;
  7936. }
  7937. #if WASM_ENABLE_GC != 0
  7938. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7939. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7940. WASMRefType *stack_top_ref_type = NULL;
  7941. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7942. bh_assert(reftype_map_num > 0);
  7943. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7944. }
  7945. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7946. ctx->ref_type_tmp, ctx->module->types,
  7947. ctx->module->type_count)) {
  7948. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7949. uint32 ref_type_struct_size =
  7950. wasm_reftype_struct_size(stack_top_ref_type);
  7951. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7952. stack_top_ref_type, ref_type_struct_size);
  7953. }
  7954. return true;
  7955. }
  7956. }
  7957. #endif
  7958. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7959. #if WASM_ENABLE_GC != 0
  7960. ctx->frame_reftype_map, reftype_map_num,
  7961. #endif
  7962. type,
  7963. #if WASM_ENABLE_GC != 0
  7964. ctx->ref_type_tmp,
  7965. #endif
  7966. error_buf, error_buf_size)) {
  7967. return false;
  7968. }
  7969. return true;
  7970. }
  7971. static bool
  7972. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7973. uint32 error_buf_size)
  7974. {
  7975. BranchBlock *cur_block = ctx->frame_csp - 1;
  7976. int32 available_stack_cell =
  7977. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7978. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7979. /* Directly return success if current block is in stack
  7980. polymorphic state while stack is empty. */
  7981. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7982. return true;
  7983. if (type == VALUE_TYPE_VOID)
  7984. return true;
  7985. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7986. return false;
  7987. bh_assert(available_stack_cell > 0);
  7988. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7989. type = VALUE_TYPE_ANY;
  7990. cell_num_to_pop = 1;
  7991. }
  7992. ctx->frame_ref -= cell_num_to_pop;
  7993. ctx->stack_cell_num -= cell_num_to_pop;
  7994. #if WASM_ENABLE_GC != 0
  7995. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7996. ctx->frame_reftype_map--;
  7997. ctx->reftype_map_num--;
  7998. }
  7999. #endif
  8000. return true;
  8001. }
  8002. #if WASM_ENABLE_GC != 0
  8003. /* Get the stack top element of current block */
  8004. static bool
  8005. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  8006. WASMRefType **p_ref_type, char *error_buf,
  8007. uint32 error_buf_size)
  8008. {
  8009. BranchBlock *cur_block = ctx->frame_csp - 1;
  8010. int32 available_stack_cell =
  8011. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8012. if (available_stack_cell <= 0) {
  8013. /* Directly return success if current block is in stack
  8014. polymorphic state while stack is empty. */
  8015. if (cur_block->is_stack_polymorphic) {
  8016. *p_type = VALUE_TYPE_ANY;
  8017. return true;
  8018. }
  8019. else {
  8020. set_error_buf(
  8021. error_buf, error_buf_size,
  8022. "type mismatch: expect data but block stack was empty");
  8023. return false;
  8024. }
  8025. }
  8026. *p_type = *(ctx->frame_ref - 1);
  8027. if (wasm_is_type_multi_byte_type(*p_type)) {
  8028. int32 available_reftype_map =
  8029. (int32)(ctx->reftype_map_num
  8030. - (ctx->frame_csp - 1)->reftype_map_num);
  8031. bh_assert(available_reftype_map > 0);
  8032. (void)available_reftype_map;
  8033. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  8034. }
  8035. return true;
  8036. }
  8037. #if WASM_ENABLE_FAST_INTERP != 0
  8038. static bool
  8039. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8040. char *error_buf, uint32 error_buf_size);
  8041. #endif
  8042. /* Check whether the stack top elem is a heap object, and if yes,
  8043. pop and return it */
  8044. static bool
  8045. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  8046. WASMRefType *ref_ht_ret, char *error_buf,
  8047. uint32 error_buf_size)
  8048. {
  8049. uint8 type = 0;
  8050. WASMRefType *ref_type = NULL;
  8051. /* Get stack top element */
  8052. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  8053. error_buf_size)) {
  8054. return false;
  8055. }
  8056. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  8057. /* stack top isn't a ref type */
  8058. && !wasm_is_type_reftype(type)) {
  8059. set_error_buf(error_buf, error_buf_size,
  8060. "type mismatch: expect heap object but got others");
  8061. return false;
  8062. }
  8063. /* POP stack top */
  8064. if (wasm_is_type_multi_byte_type(type)) {
  8065. bh_assert(ref_type);
  8066. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  8067. wasm_reftype_struct_size(ref_type));
  8068. }
  8069. #if WASM_ENABLE_FAST_INTERP != 0
  8070. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  8071. error_buf_size)) {
  8072. return false;
  8073. }
  8074. #else
  8075. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  8076. return false;
  8077. }
  8078. #endif
  8079. if (p_type)
  8080. *p_type = type;
  8081. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  8082. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  8083. wasm_reftype_struct_size(ref_type));
  8084. }
  8085. return true;
  8086. }
  8087. /* Check whether the stack top elem is subtype of (ref null ht),
  8088. and if yes, pop it and return the converted (ref ht) */
  8089. static bool
  8090. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  8091. WASMRefType *ref_ht_ret, char *error_buf,
  8092. uint32 error_buf_size)
  8093. {
  8094. uint8 type = 0;
  8095. WASMRefType ref_type = { 0 };
  8096. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  8097. error_buf_size)) {
  8098. return false;
  8099. }
  8100. /* Convert to related (ref ht) and return */
  8101. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  8102. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  8103. */
  8104. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  8105. HEAP_TYPE_ARRAY
  8106. + (type - REF_TYPE_ARRAYREF));
  8107. type = ref_ht_ret->ref_type;
  8108. }
  8109. else if (wasm_is_reftype_htref_nullable(type)
  8110. || wasm_is_reftype_htref_non_nullable(type)) {
  8111. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  8112. wasm_reftype_struct_size(&ref_type));
  8113. /* Convert to (ref ht) */
  8114. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  8115. ref_ht_ret->ref_ht_common.nullable = false;
  8116. type = ref_ht_ret->ref_type;
  8117. }
  8118. *p_type = type;
  8119. return true;
  8120. }
  8121. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  8122. and if yes, pop it and return the type_idx */
  8123. static bool
  8124. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  8125. uint32 *p_type_idx, char *error_buf,
  8126. uint32 error_buf_size)
  8127. {
  8128. uint8 type = 0;
  8129. int32 type_idx = -1;
  8130. WASMRefType *ref_type = NULL;
  8131. /* Get stack top element */
  8132. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  8133. error_buf_size)) {
  8134. return false;
  8135. }
  8136. if (type != VALUE_TYPE_ANY) {
  8137. /* stack top isn't (ref null $t) */
  8138. if (!((wasm_is_reftype_htref_nullable(type)
  8139. || wasm_is_reftype_htref_non_nullable(type))
  8140. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  8141. set_error_buf(error_buf, error_buf_size,
  8142. "type mismatch: expect (ref null $t) but got others");
  8143. return false;
  8144. }
  8145. type_idx = ref_type->ref_ht_typeidx.type_idx;
  8146. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  8147. wasm_reftype_struct_size(ref_type));
  8148. }
  8149. /* POP stack top */
  8150. #if WASM_ENABLE_FAST_INTERP != 0
  8151. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  8152. error_buf_size)) {
  8153. return false;
  8154. }
  8155. #else
  8156. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  8157. return false;
  8158. }
  8159. #endif
  8160. /* Convert to type_idx and return */
  8161. *p_type = type;
  8162. if (type != VALUE_TYPE_ANY)
  8163. *p_type_idx = (uint32)type_idx;
  8164. return true;
  8165. }
  8166. #endif /* WASM_ENABLE_GC != 0 */
  8167. #if WASM_ENABLE_FAST_INTERP == 0
  8168. static bool
  8169. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  8170. uint8 type_push, uint8 type_pop, char *error_buf,
  8171. uint32 error_buf_size)
  8172. {
  8173. for (int i = 0; i < pop_cnt; i++) {
  8174. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8175. error_buf_size))
  8176. return false;
  8177. }
  8178. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8179. return false;
  8180. return true;
  8181. }
  8182. #endif
  8183. static bool
  8184. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  8185. BlockType block_type, uint8 *start_addr,
  8186. char *error_buf, uint32 error_buf_size)
  8187. {
  8188. CHECK_CSP_PUSH();
  8189. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  8190. ctx->frame_csp->label_type = label_type;
  8191. ctx->frame_csp->block_type = block_type;
  8192. ctx->frame_csp->start_addr = start_addr;
  8193. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  8194. #if WASM_ENABLE_GC != 0
  8195. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  8196. #endif
  8197. #if WASM_ENABLE_FAST_INTERP != 0
  8198. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  8199. ctx->frame_csp->patch_list = NULL;
  8200. #endif
  8201. ctx->frame_csp++;
  8202. ctx->csp_num++;
  8203. if (ctx->csp_num > ctx->max_csp_num) {
  8204. ctx->max_csp_num = ctx->csp_num;
  8205. if (ctx->max_csp_num > UINT16_MAX) {
  8206. set_error_buf(error_buf, error_buf_size,
  8207. "label stack depth limit exceeded");
  8208. return false;
  8209. }
  8210. }
  8211. return true;
  8212. fail:
  8213. return false;
  8214. }
  8215. static bool
  8216. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  8217. uint32 error_buf_size)
  8218. {
  8219. CHECK_CSP_POP();
  8220. #if WASM_ENABLE_FAST_INTERP != 0
  8221. if ((ctx->frame_csp - 1)->param_frame_offsets)
  8222. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  8223. #endif
  8224. ctx->frame_csp--;
  8225. ctx->csp_num--;
  8226. return true;
  8227. fail:
  8228. return false;
  8229. }
  8230. #if WASM_ENABLE_FAST_INTERP != 0
  8231. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  8232. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  8233. #define emit_label(opcode) \
  8234. do { \
  8235. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  8236. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8237. } while (0)
  8238. #define skip_label() \
  8239. do { \
  8240. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  8241. LOG_OP("\ndelete last op\n"); \
  8242. } while (0)
  8243. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  8244. #if UINTPTR_MAX == UINT64_MAX
  8245. #define emit_label(opcode) \
  8246. do { \
  8247. int32 offset = \
  8248. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  8249. /* emit int32 relative offset in 64-bit target */ \
  8250. wasm_loader_emit_uint32(loader_ctx, offset); \
  8251. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8252. } while (0)
  8253. #else
  8254. #define emit_label(opcode) \
  8255. do { \
  8256. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  8257. /* emit uint32 label address in 32-bit target */ \
  8258. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  8259. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8260. } while (0)
  8261. #endif
  8262. #define skip_label() \
  8263. do { \
  8264. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  8265. LOG_OP("\ndelete last op\n"); \
  8266. } while (0)
  8267. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  8268. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  8269. #define emit_label(opcode) \
  8270. do { \
  8271. wasm_loader_emit_uint8(loader_ctx, opcode); \
  8272. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8273. } while (0)
  8274. #define skip_label() \
  8275. do { \
  8276. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  8277. LOG_OP("\ndelete last op\n"); \
  8278. } while (0)
  8279. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  8280. #define emit_empty_label_addr_and_frame_ip(type) \
  8281. do { \
  8282. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  8283. loader_ctx->p_code_compiled, error_buf, \
  8284. error_buf_size)) \
  8285. goto fail; \
  8286. /* label address, to be patched */ \
  8287. wasm_loader_emit_ptr(loader_ctx, NULL); \
  8288. } while (0)
  8289. #define emit_br_info(frame_csp, is_br) \
  8290. do { \
  8291. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  8292. error_buf_size)) \
  8293. goto fail; \
  8294. } while (0)
  8295. #define LAST_OP_OUTPUT_I32() \
  8296. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  8297. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  8298. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  8299. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  8300. || (last_op >= WASM_OP_I32_WRAP_I64 \
  8301. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  8302. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  8303. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  8304. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  8305. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  8306. || (last_op == EXT_OP_COPY_STACK_TOP)
  8307. #define LAST_OP_OUTPUT_I64() \
  8308. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  8309. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  8310. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  8311. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  8312. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  8313. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  8314. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  8315. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  8316. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  8317. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  8318. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  8319. #define GET_CONST_OFFSET(type, val) \
  8320. do { \
  8321. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  8322. &operand_offset, error_buf, \
  8323. error_buf_size))) \
  8324. goto fail; \
  8325. } while (0)
  8326. #define GET_CONST_F32_OFFSET(type, fval) \
  8327. do { \
  8328. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  8329. &operand_offset, error_buf, \
  8330. error_buf_size))) \
  8331. goto fail; \
  8332. } while (0)
  8333. #define GET_CONST_F64_OFFSET(type, fval) \
  8334. do { \
  8335. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  8336. &operand_offset, error_buf, \
  8337. error_buf_size))) \
  8338. goto fail; \
  8339. } while (0)
  8340. #define emit_operand(ctx, offset) \
  8341. do { \
  8342. wasm_loader_emit_int16(ctx, offset); \
  8343. LOG_OP("%d\t", offset); \
  8344. } while (0)
  8345. #define emit_byte(ctx, byte) \
  8346. do { \
  8347. wasm_loader_emit_uint8(ctx, byte); \
  8348. LOG_OP("%d\t", byte); \
  8349. } while (0)
  8350. #define emit_uint32(ctx, value) \
  8351. do { \
  8352. wasm_loader_emit_uint32(ctx, value); \
  8353. LOG_OP("%d\t", value); \
  8354. } while (0)
  8355. #define emit_uint64(ctx, value) \
  8356. do { \
  8357. wasm_loader_emit_const(ctx, &value, false); \
  8358. LOG_OP("%lld\t", value); \
  8359. } while (0)
  8360. #define emit_float32(ctx, value) \
  8361. do { \
  8362. wasm_loader_emit_const(ctx, &value, true); \
  8363. LOG_OP("%f\t", value); \
  8364. } while (0)
  8365. #define emit_float64(ctx, value) \
  8366. do { \
  8367. wasm_loader_emit_const(ctx, &value, false); \
  8368. LOG_OP("%f\t", value); \
  8369. } while (0)
  8370. static bool
  8371. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  8372. {
  8373. if (!(ctx->p_code_compiled =
  8374. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  8375. return false;
  8376. ctx->p_code_compiled_end =
  8377. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  8378. /* clean up frame ref */
  8379. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  8380. ctx->frame_ref = ctx->frame_ref_bottom;
  8381. ctx->stack_cell_num = 0;
  8382. #if WASM_ENABLE_GC != 0
  8383. /* clean up reftype map */
  8384. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  8385. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  8386. ctx->reftype_map_num = 0;
  8387. #endif
  8388. /* clean up frame csp */
  8389. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  8390. ctx->frame_csp = ctx->frame_csp_bottom;
  8391. ctx->csp_num = 0;
  8392. ctx->max_csp_num = 0;
  8393. /* clean up frame offset */
  8394. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  8395. ctx->frame_offset = ctx->frame_offset_bottom;
  8396. ctx->dynamic_offset = ctx->start_dynamic_offset;
  8397. /* init preserved local offsets */
  8398. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  8399. /* const buf is reserved */
  8400. return true;
  8401. }
  8402. static void
  8403. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8404. {
  8405. ctx->code_compiled_size += size;
  8406. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8407. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8408. }
  8409. }
  8410. static void
  8411. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8412. {
  8413. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8414. if (ctx->p_code_compiled) {
  8415. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8416. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8417. #endif
  8418. bh_memcpy_s(ctx->p_code_compiled,
  8419. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8420. value, size);
  8421. ctx->p_code_compiled += size;
  8422. }
  8423. else {
  8424. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8425. bh_assert((ctx->code_compiled_size & 1) == 0);
  8426. #endif
  8427. increase_compiled_code_space(ctx, size);
  8428. }
  8429. }
  8430. static void
  8431. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8432. {
  8433. if (ctx->p_code_compiled) {
  8434. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8435. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8436. #endif
  8437. STORE_U32(ctx->p_code_compiled, value);
  8438. ctx->p_code_compiled += sizeof(uint32);
  8439. }
  8440. else {
  8441. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8442. bh_assert((ctx->code_compiled_size & 1) == 0);
  8443. #endif
  8444. increase_compiled_code_space(ctx, sizeof(uint32));
  8445. }
  8446. }
  8447. static void
  8448. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8449. {
  8450. if (ctx->p_code_compiled) {
  8451. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8452. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8453. #endif
  8454. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8455. ctx->p_code_compiled += sizeof(int16);
  8456. }
  8457. else {
  8458. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8459. bh_assert((ctx->code_compiled_size & 1) == 0);
  8460. #endif
  8461. increase_compiled_code_space(ctx, sizeof(uint16));
  8462. }
  8463. }
  8464. static void
  8465. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8466. {
  8467. if (ctx->p_code_compiled) {
  8468. *(ctx->p_code_compiled) = value;
  8469. ctx->p_code_compiled += sizeof(uint8);
  8470. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8471. ctx->p_code_compiled++;
  8472. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8473. #endif
  8474. }
  8475. else {
  8476. increase_compiled_code_space(ctx, sizeof(uint8));
  8477. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8478. increase_compiled_code_space(ctx, sizeof(uint8));
  8479. bh_assert((ctx->code_compiled_size & 1) == 0);
  8480. #endif
  8481. }
  8482. }
  8483. static void
  8484. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8485. {
  8486. if (ctx->p_code_compiled) {
  8487. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8488. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8489. #endif
  8490. STORE_PTR(ctx->p_code_compiled, value);
  8491. ctx->p_code_compiled += sizeof(void *);
  8492. }
  8493. else {
  8494. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8495. bh_assert((ctx->code_compiled_size & 1) == 0);
  8496. #endif
  8497. increase_compiled_code_space(ctx, sizeof(void *));
  8498. }
  8499. }
  8500. static void
  8501. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8502. {
  8503. if (ctx->p_code_compiled) {
  8504. ctx->p_code_compiled -= size;
  8505. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8506. if (size == sizeof(uint8)) {
  8507. ctx->p_code_compiled--;
  8508. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8509. }
  8510. #endif
  8511. }
  8512. else {
  8513. ctx->code_compiled_size -= size;
  8514. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8515. if (size == sizeof(uint8)) {
  8516. ctx->code_compiled_size--;
  8517. bh_assert((ctx->code_compiled_size & 1) == 0);
  8518. }
  8519. #endif
  8520. }
  8521. }
  8522. static bool
  8523. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8524. uint32 local_index, uint32 local_type,
  8525. bool *preserved, char *error_buf,
  8526. uint32 error_buf_size)
  8527. {
  8528. uint32 i = 0;
  8529. int16 preserved_offset = (int16)local_index;
  8530. *preserved = false;
  8531. while (i < loader_ctx->stack_cell_num) {
  8532. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8533. /* move previous local into dynamic space before a set/tee_local opcode
  8534. */
  8535. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8536. if (!(*preserved)) {
  8537. *preserved = true;
  8538. skip_label();
  8539. preserved_offset = loader_ctx->preserved_local_offset;
  8540. if (loader_ctx->p_code_compiled) {
  8541. bh_assert(preserved_offset != (int16)local_index);
  8542. }
  8543. if (is_32bit_type(local_type)) {
  8544. /* Only increase preserve offset in the second traversal */
  8545. if (loader_ctx->p_code_compiled)
  8546. loader_ctx->preserved_local_offset++;
  8547. emit_label(EXT_OP_COPY_STACK_TOP);
  8548. }
  8549. #if WASM_ENABLE_SIMDE != 0
  8550. else if (local_type == VALUE_TYPE_V128) {
  8551. if (loader_ctx->p_code_compiled)
  8552. loader_ctx->preserved_local_offset += 4;
  8553. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  8554. }
  8555. #endif
  8556. else {
  8557. if (loader_ctx->p_code_compiled)
  8558. loader_ctx->preserved_local_offset += 2;
  8559. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8560. }
  8561. /* overflow */
  8562. if (preserved_offset > loader_ctx->preserved_local_offset) {
  8563. set_error_buf_v(error_buf, error_buf_size,
  8564. "too much local cells 0x%x",
  8565. loader_ctx->preserved_local_offset);
  8566. return false;
  8567. }
  8568. emit_operand(loader_ctx, local_index);
  8569. emit_operand(loader_ctx, preserved_offset);
  8570. emit_label(opcode);
  8571. }
  8572. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8573. }
  8574. if (cur_type == VALUE_TYPE_V128) {
  8575. i += 4;
  8576. }
  8577. else if (is_32bit_type(cur_type)) {
  8578. i++;
  8579. }
  8580. else {
  8581. i += 2;
  8582. }
  8583. }
  8584. (void)error_buf;
  8585. (void)error_buf_size;
  8586. return true;
  8587. }
  8588. static bool
  8589. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8590. char *error_buf, uint32 error_buf_size)
  8591. {
  8592. uint32 i = 0;
  8593. bool preserve_local;
  8594. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8595. blocks will not influence the value of these locals */
  8596. while (i < loader_ctx->stack_cell_num) {
  8597. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8598. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8599. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8600. && (cur_offset >= 0)) {
  8601. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8602. cur_type, &preserve_local,
  8603. error_buf, error_buf_size)))
  8604. return false;
  8605. }
  8606. if (cur_type == VALUE_TYPE_V128) {
  8607. i += 4;
  8608. }
  8609. else if (is_32bit_type(cur_type)) {
  8610. i++;
  8611. }
  8612. else {
  8613. i += 2;
  8614. }
  8615. }
  8616. return true;
  8617. }
  8618. static bool
  8619. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8620. uint8 *p_code_compiled, char *error_buf,
  8621. uint32 error_buf_size)
  8622. {
  8623. BranchBlockPatch *patch =
  8624. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8625. if (!patch) {
  8626. return false;
  8627. }
  8628. patch->patch_type = patch_type;
  8629. patch->code_compiled = p_code_compiled;
  8630. if (!frame_csp->patch_list) {
  8631. frame_csp->patch_list = patch;
  8632. patch->next = NULL;
  8633. }
  8634. else {
  8635. patch->next = frame_csp->patch_list;
  8636. frame_csp->patch_list = patch;
  8637. }
  8638. return true;
  8639. }
  8640. static void
  8641. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8642. {
  8643. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8644. BranchBlockPatch *node = frame_csp->patch_list;
  8645. BranchBlockPatch *node_prev = NULL, *node_next;
  8646. if (!ctx->p_code_compiled)
  8647. return;
  8648. while (node) {
  8649. node_next = node->next;
  8650. if (node->patch_type == patch_type) {
  8651. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8652. if (node_prev == NULL) {
  8653. frame_csp->patch_list = node_next;
  8654. }
  8655. else {
  8656. node_prev->next = node_next;
  8657. }
  8658. wasm_runtime_free(node);
  8659. }
  8660. else {
  8661. node_prev = node;
  8662. }
  8663. node = node_next;
  8664. }
  8665. }
  8666. static bool
  8667. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8668. bool is_br, char *error_buf, uint32 error_buf_size)
  8669. {
  8670. /* br info layout:
  8671. * a) arity of target block
  8672. * b) total cell num of arity values
  8673. * c) each arity value's cell num
  8674. * d) each arity value's src frame offset
  8675. * e) each arity values's dst dynamic offset
  8676. * f) branch target address
  8677. *
  8678. * Note: b-e are omitted when arity is 0 so that
  8679. * interpreter can recover the br info quickly.
  8680. */
  8681. BlockType *block_type = &frame_csp->block_type;
  8682. uint8 *types = NULL, cell;
  8683. #if WASM_ENABLE_GC != 0
  8684. WASMRefTypeMap *reftype_maps;
  8685. uint32 reftype_map_count;
  8686. #endif
  8687. uint32 arity = 0;
  8688. int32 i;
  8689. int16 *frame_offset = ctx->frame_offset;
  8690. uint16 dynamic_offset;
  8691. /* Note: loop's arity is different from if and block. loop's arity is
  8692. * its parameter count while if and block arity is result count.
  8693. */
  8694. #if WASM_ENABLE_GC == 0
  8695. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8696. arity = block_type_get_param_types(block_type, &types);
  8697. else
  8698. arity = block_type_get_result_types(block_type, &types);
  8699. #else
  8700. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8701. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8702. &reftype_map_count);
  8703. else
  8704. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8705. &reftype_map_count);
  8706. #endif
  8707. /* Part a */
  8708. emit_uint32(ctx, arity);
  8709. if (arity) {
  8710. /* Part b */
  8711. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8712. /* Part c */
  8713. for (i = (int32)arity - 1; i >= 0; i--) {
  8714. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8715. emit_byte(ctx, cell);
  8716. }
  8717. /* Part d */
  8718. for (i = (int32)arity - 1; i >= 0; i--) {
  8719. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8720. frame_offset -= cell;
  8721. emit_operand(ctx, *(int16 *)(frame_offset));
  8722. }
  8723. /* Part e */
  8724. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8725. /* Use start_dynamic_offset which was set in
  8726. copy_params_to_dynamic_space */
  8727. dynamic_offset = frame_csp->start_dynamic_offset
  8728. + wasm_get_cell_num(types, arity);
  8729. else
  8730. dynamic_offset =
  8731. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8732. if (is_br)
  8733. ctx->dynamic_offset = dynamic_offset;
  8734. for (i = (int32)arity - 1; i >= 0; i--) {
  8735. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8736. dynamic_offset -= cell;
  8737. emit_operand(ctx, dynamic_offset);
  8738. }
  8739. }
  8740. /* Part f */
  8741. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8742. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8743. }
  8744. else {
  8745. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8746. error_buf, error_buf_size))
  8747. return false;
  8748. /* label address, to be patched */
  8749. wasm_loader_emit_ptr(ctx, NULL);
  8750. }
  8751. return true;
  8752. }
  8753. static bool
  8754. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8755. bool disable_emit, int16 operand_offset,
  8756. char *error_buf, uint32 error_buf_size)
  8757. {
  8758. uint32 cell_num_to_push, i;
  8759. if (type == VALUE_TYPE_VOID)
  8760. return true;
  8761. /* only check memory overflow in first traverse */
  8762. if (ctx->p_code_compiled == NULL) {
  8763. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8764. return false;
  8765. }
  8766. if (disable_emit)
  8767. *(ctx->frame_offset)++ = operand_offset;
  8768. else {
  8769. emit_operand(ctx, ctx->dynamic_offset);
  8770. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8771. ctx->dynamic_offset++;
  8772. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8773. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8774. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8775. goto fail;
  8776. }
  8777. }
  8778. }
  8779. if (is_32bit_type(type))
  8780. return true;
  8781. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8782. for (i = 0; i < cell_num_to_push; i++) {
  8783. if (ctx->p_code_compiled == NULL) {
  8784. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8785. return false;
  8786. }
  8787. ctx->frame_offset++;
  8788. if (!disable_emit) {
  8789. ctx->dynamic_offset++;
  8790. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8791. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8792. if (ctx->max_dynamic_offset >= INT16_MAX)
  8793. goto fail;
  8794. }
  8795. }
  8796. }
  8797. return true;
  8798. fail:
  8799. set_error_buf(error_buf, error_buf_size,
  8800. "fast interpreter offset overflow");
  8801. return false;
  8802. }
  8803. /* This function should be in front of wasm_loader_pop_frame_ref
  8804. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8805. will be modified by wasm_loader_pop_frame_ref */
  8806. static bool
  8807. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8808. char *error_buf, uint32 error_buf_size)
  8809. {
  8810. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8811. then current block is the function block */
  8812. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8813. BranchBlock *cur_block = ctx->frame_csp - depth;
  8814. int32 available_stack_cell =
  8815. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8816. uint32 cell_num_to_pop;
  8817. /* Directly return success if current block is in stack
  8818. polymorphic state while stack is empty. */
  8819. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8820. return true;
  8821. if (type == VALUE_TYPE_VOID)
  8822. return true;
  8823. /* Change type to ANY when the stack top is ANY, so as to avoid
  8824. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8825. two offsets */
  8826. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8827. type = VALUE_TYPE_ANY;
  8828. cell_num_to_pop = wasm_value_type_cell_num(type);
  8829. /* Check the offset stack bottom to ensure the frame offset
  8830. stack will not go underflow. But we don't thrown error
  8831. and return true here, because the error msg should be
  8832. given in wasm_loader_pop_frame_ref */
  8833. if (!check_offset_pop(ctx, cell_num_to_pop))
  8834. return true;
  8835. ctx->frame_offset -= cell_num_to_pop;
  8836. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8837. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8838. ctx->dynamic_offset -= cell_num_to_pop;
  8839. emit_operand(ctx, *(ctx->frame_offset));
  8840. (void)error_buf;
  8841. (void)error_buf_size;
  8842. return true;
  8843. }
  8844. static bool
  8845. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8846. bool disable_emit, int16 operand_offset,
  8847. char *error_buf, uint32 error_buf_size)
  8848. {
  8849. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8850. error_buf, error_buf_size)))
  8851. return false;
  8852. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8853. return false;
  8854. return true;
  8855. }
  8856. static bool
  8857. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8858. char *error_buf, uint32 error_buf_size)
  8859. {
  8860. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8861. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8862. return false;
  8863. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8864. return false;
  8865. return true;
  8866. }
  8867. static bool
  8868. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8869. uint8 type_push, uint8 type_pop,
  8870. bool disable_emit, int16 operand_offset,
  8871. char *error_buf, uint32 error_buf_size)
  8872. {
  8873. uint8 i;
  8874. for (i = 0; i < pop_cnt; i++) {
  8875. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8876. error_buf_size))
  8877. return false;
  8878. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8879. error_buf_size))
  8880. return false;
  8881. }
  8882. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8883. operand_offset, error_buf,
  8884. error_buf_size))
  8885. return false;
  8886. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8887. return false;
  8888. return true;
  8889. }
  8890. static int
  8891. cmp_i64_const(const void *p_i64_const1, const void *p_i64_const2)
  8892. {
  8893. int64 i64_const1 = *(int64 *)p_i64_const1;
  8894. int64 i64_const2 = *(int64 *)p_i64_const2;
  8895. return (i64_const1 < i64_const2) ? -1 : (i64_const1 > i64_const2) ? 1 : 0;
  8896. }
  8897. static int
  8898. cmp_i32_const(const void *p_i32_const1, const void *p_i32_const2)
  8899. {
  8900. int32 i32_const1 = *(int32 *)p_i32_const1;
  8901. int32 i32_const2 = *(int32 *)p_i32_const2;
  8902. return (i32_const1 < i32_const2) ? -1 : (i32_const1 > i32_const2) ? 1 : 0;
  8903. }
  8904. static int
  8905. cmp_v128_const(const void *p_v128_const1, const void *p_v128_const2)
  8906. {
  8907. V128 v128_const1 = *(V128 *)p_v128_const1;
  8908. V128 v128_const2 = *(V128 *)p_v128_const2;
  8909. return memcmp(&v128_const1, &v128_const2, sizeof(V128));
  8910. }
  8911. static bool
  8912. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8913. int16 *offset, char *error_buf,
  8914. uint32 error_buf_size)
  8915. {
  8916. if (!ctx->p_code_compiled) {
  8917. /* Treat i64 and f64 as the same by reading i64 value from
  8918. the raw bytes */
  8919. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8920. /* No slot left, emit const instead */
  8921. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 2) {
  8922. *offset = 0;
  8923. return true;
  8924. }
  8925. /* Traverse the list if the const num is small */
  8926. if (ctx->i64_const_num < 10) {
  8927. for (uint32 i = 0; i < ctx->i64_const_num; i++) {
  8928. if (ctx->i64_consts[i] == *(int64 *)value) {
  8929. *offset = -1;
  8930. return true;
  8931. }
  8932. }
  8933. }
  8934. if (ctx->i64_const_num >= ctx->i64_const_max_num) {
  8935. MEM_REALLOC(ctx->i64_consts,
  8936. sizeof(int64) * ctx->i64_const_max_num,
  8937. sizeof(int64) * (ctx->i64_const_max_num * 2));
  8938. ctx->i64_const_max_num *= 2;
  8939. }
  8940. ctx->i64_consts[ctx->i64_const_num++] = *(int64 *)value;
  8941. }
  8942. else if (type == VALUE_TYPE_V128) {
  8943. /* No slot left, emit const instead */
  8944. if (ctx->v128_const_num * 4 > INT16_MAX - 2) {
  8945. *offset = 0;
  8946. return true;
  8947. }
  8948. /* Traverse the list if the const num is small */
  8949. if (ctx->v128_const_num < 10) {
  8950. for (uint32 i = 0; i < ctx->v128_const_num; i++) {
  8951. if (memcmp(&ctx->v128_consts[i], value, sizeof(V128))
  8952. == 0) {
  8953. *offset = -1;
  8954. return true;
  8955. }
  8956. }
  8957. }
  8958. if (ctx->v128_const_num >= ctx->v128_const_max_num) {
  8959. MEM_REALLOC(ctx->v128_consts,
  8960. sizeof(V128) * ctx->v128_const_max_num,
  8961. sizeof(V128) * (ctx->v128_const_max_num * 2));
  8962. ctx->v128_const_max_num *= 2;
  8963. }
  8964. ctx->v128_consts[ctx->v128_const_num++] = *(V128 *)value;
  8965. }
  8966. else {
  8967. /* Treat i32 and f32 as the same by reading i32 value from
  8968. the raw bytes */
  8969. bh_assert(type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32);
  8970. /* No slot left, emit const instead */
  8971. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 1) {
  8972. *offset = 0;
  8973. return true;
  8974. }
  8975. /* Traverse the list if the const num is small */
  8976. if (ctx->i32_const_num < 10) {
  8977. for (uint32 i = 0; i < ctx->i32_const_num; i++) {
  8978. if (ctx->i32_consts[i] == *(int32 *)value) {
  8979. *offset = -1;
  8980. return true;
  8981. }
  8982. }
  8983. }
  8984. if (ctx->i32_const_num >= ctx->i32_const_max_num) {
  8985. MEM_REALLOC(ctx->i32_consts,
  8986. sizeof(int32) * ctx->i32_const_max_num,
  8987. sizeof(int32) * (ctx->i32_const_max_num * 2));
  8988. ctx->i32_const_max_num *= 2;
  8989. }
  8990. ctx->i32_consts[ctx->i32_const_num++] = *(int32 *)value;
  8991. }
  8992. *offset = -1;
  8993. return true;
  8994. }
  8995. else {
  8996. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8997. int64 key = *(int64 *)value, *i64_const;
  8998. i64_const = bsearch(&key, ctx->i64_consts, ctx->i64_const_num,
  8999. sizeof(int64), cmp_i64_const);
  9000. if (!i64_const) { /* not found, emit const instead */
  9001. *offset = 0;
  9002. return true;
  9003. }
  9004. /* constant index is encoded as negative value */
  9005. *offset = -(int32)(ctx->i64_const_num * 2 + ctx->i32_const_num)
  9006. + (int32)(i64_const - ctx->i64_consts) * 2;
  9007. }
  9008. else if (type == VALUE_TYPE_V128) {
  9009. V128 key = *(V128 *)value, *v128_const;
  9010. v128_const = bsearch(&key, ctx->v128_consts, ctx->v128_const_num,
  9011. sizeof(V128), cmp_v128_const);
  9012. if (!v128_const) { /* not found, emit const instead */
  9013. *offset = 0;
  9014. return true;
  9015. }
  9016. /* constant index is encoded as negative value */
  9017. *offset = -(int32)(ctx->v128_const_num)
  9018. + (int32)(v128_const - ctx->v128_consts);
  9019. }
  9020. else {
  9021. int32 key = *(int32 *)value, *i32_const;
  9022. i32_const = bsearch(&key, ctx->i32_consts, ctx->i32_const_num,
  9023. sizeof(int32), cmp_i32_const);
  9024. if (!i32_const) { /* not found, emit const instead */
  9025. *offset = 0;
  9026. return true;
  9027. }
  9028. /* constant index is encoded as negative value */
  9029. *offset = -(int32)(ctx->i32_const_num)
  9030. + (int32)(i32_const - ctx->i32_consts);
  9031. }
  9032. return true;
  9033. }
  9034. fail:
  9035. return false;
  9036. }
  9037. /*
  9038. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  9039. -- Mostly used for the binary / compare operation
  9040. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  9041. -- Mostly used in block / control instructions
  9042. The POP will always emit the offset on the top of the frame_offset stack
  9043. PUSH can be used in two ways:
  9044. 1. directly PUSH:
  9045. PUSH_XXX();
  9046. will allocate a dynamic space and emit
  9047. 2. silent PUSH:
  9048. operand_offset = xxx; disable_emit = true;
  9049. PUSH_XXX();
  9050. only push the frame_offset stack, no emit
  9051. */
  9052. #define TEMPLATE_PUSH(Type) \
  9053. do { \
  9054. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  9055. disable_emit, operand_offset, \
  9056. error_buf, error_buf_size)) \
  9057. goto fail; \
  9058. } while (0)
  9059. #define TEMPLATE_PUSH_REF(Type) \
  9060. do { \
  9061. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  9062. operand_offset, error_buf, \
  9063. error_buf_size)) \
  9064. goto fail; \
  9065. } while (0)
  9066. #define TEMPLATE_POP(Type) \
  9067. do { \
  9068. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  9069. error_buf, error_buf_size)) \
  9070. goto fail; \
  9071. } while (0)
  9072. #define TEMPLATE_POP_REF(Type) \
  9073. do { \
  9074. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  9075. error_buf_size)) \
  9076. goto fail; \
  9077. } while (0)
  9078. #define PUSH_OFFSET_TYPE(type) \
  9079. do { \
  9080. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  9081. operand_offset, error_buf, \
  9082. error_buf_size))) \
  9083. goto fail; \
  9084. } while (0)
  9085. #define POP_OFFSET_TYPE(type) \
  9086. do { \
  9087. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  9088. error_buf_size))) \
  9089. goto fail; \
  9090. } while (0)
  9091. #define POP_AND_PUSH(type_pop, type_push) \
  9092. do { \
  9093. if (!(wasm_loader_push_pop_frame_ref_offset( \
  9094. loader_ctx, 1, type_push, type_pop, disable_emit, \
  9095. operand_offset, error_buf, error_buf_size))) \
  9096. goto fail; \
  9097. } while (0)
  9098. /* type of POPs should be the same */
  9099. #define POP2_AND_PUSH(type_pop, type_push) \
  9100. do { \
  9101. if (!(wasm_loader_push_pop_frame_ref_offset( \
  9102. loader_ctx, 2, type_push, type_pop, disable_emit, \
  9103. operand_offset, error_buf, error_buf_size))) \
  9104. goto fail; \
  9105. } while (0)
  9106. #else /* WASM_ENABLE_FAST_INTERP */
  9107. #define TEMPLATE_PUSH(Type) \
  9108. do { \
  9109. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  9110. error_buf, error_buf_size))) \
  9111. goto fail; \
  9112. } while (0)
  9113. #define TEMPLATE_PUSH_REF(Type) \
  9114. do { \
  9115. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  9116. error_buf_size))) \
  9117. goto fail; \
  9118. } while (0)
  9119. #define TEMPLATE_POP(Type) \
  9120. do { \
  9121. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  9122. error_buf, error_buf_size))) \
  9123. goto fail; \
  9124. } while (0)
  9125. #define TEMPLATE_POP_REF(Type) \
  9126. do { \
  9127. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  9128. error_buf_size))) \
  9129. goto fail; \
  9130. } while (0)
  9131. #define POP_AND_PUSH(type_pop, type_push) \
  9132. do { \
  9133. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  9134. type_pop, error_buf, \
  9135. error_buf_size))) \
  9136. goto fail; \
  9137. } while (0)
  9138. /* type of POPs should be the same */
  9139. #define POP2_AND_PUSH(type_pop, type_push) \
  9140. do { \
  9141. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  9142. type_pop, error_buf, \
  9143. error_buf_size))) \
  9144. goto fail; \
  9145. } while (0)
  9146. #endif /* WASM_ENABLE_FAST_INTERP */
  9147. #define PUSH_I32() TEMPLATE_PUSH(I32)
  9148. #define PUSH_F32() TEMPLATE_PUSH(F32)
  9149. #define PUSH_I64() TEMPLATE_PUSH(I64)
  9150. #define PUSH_F64() TEMPLATE_PUSH(F64)
  9151. #define PUSH_V128() TEMPLATE_PUSH(V128)
  9152. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  9153. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  9154. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  9155. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  9156. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  9157. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  9158. #define PUSH_TBL_ELEM_IDX() TEMPLATE_PUSH_REF(table_elem_idx_type)
  9159. #define POP_I32() TEMPLATE_POP(I32)
  9160. #define POP_F32() TEMPLATE_POP(F32)
  9161. #define POP_I64() TEMPLATE_POP(I64)
  9162. #define POP_F64() TEMPLATE_POP(F64)
  9163. #define POP_V128() TEMPLATE_POP(V128)
  9164. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  9165. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  9166. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  9167. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  9168. #define POP_TBL_ELEM_IDX() TEMPLATE_POP_REF(table_elem_idx_type)
  9169. #if WASM_ENABLE_FAST_INTERP != 0
  9170. static bool
  9171. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  9172. bool disable_emit, char *error_buf, uint32 error_buf_size)
  9173. {
  9174. int16 operand_offset = 0;
  9175. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  9176. : loader_ctx->frame_csp;
  9177. BlockType *block_type = &block->block_type;
  9178. uint8 *return_types = NULL;
  9179. #if WASM_ENABLE_GC != 0
  9180. WASMRefTypeMap *reftype_maps = NULL;
  9181. uint32 reftype_map_count;
  9182. #endif
  9183. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  9184. int32 i = 0;
  9185. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  9186. *frame_offset_org = NULL;
  9187. #if WASM_ENABLE_GC == 0
  9188. return_count = block_type_get_result_types(block_type, &return_types);
  9189. #else
  9190. return_count = block_type_get_result_types(
  9191. block_type, &return_types, &reftype_maps, &reftype_map_count);
  9192. #endif
  9193. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64/V128
  9194. * instead of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  9195. if (return_count == 1) {
  9196. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  9197. if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  9198. /* insert op_copy before else opcode */
  9199. if (opcode == WASM_OP_ELSE)
  9200. skip_label();
  9201. #if WASM_ENABLE_SIMDE != 0
  9202. if (cell == 4) {
  9203. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  9204. }
  9205. #endif
  9206. if (cell <= 2) {
  9207. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  9208. : EXT_OP_COPY_STACK_TOP_I64);
  9209. }
  9210. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  9211. emit_operand(loader_ctx, block->dynamic_offset);
  9212. if (opcode == WASM_OP_ELSE) {
  9213. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  9214. }
  9215. else {
  9216. loader_ctx->frame_offset -= cell;
  9217. loader_ctx->dynamic_offset = block->dynamic_offset;
  9218. PUSH_OFFSET_TYPE(return_types[0]);
  9219. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9220. }
  9221. if (opcode == WASM_OP_ELSE)
  9222. emit_label(opcode);
  9223. }
  9224. return true;
  9225. }
  9226. /* Copy stack top values to block's results which are in dynamic space.
  9227. * The instruction format:
  9228. * Part a: values count
  9229. * Part b: all values total cell num
  9230. * Part c: each value's cell_num, src offset and dst offset
  9231. * Part d: each value's src offset and dst offset
  9232. * Part e: each value's dst offset
  9233. */
  9234. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  9235. dynamic_offset = dynamic_offset_org =
  9236. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  9237. /* First traversal to get the count of values needed to be copied. */
  9238. for (i = (int32)return_count - 1; i >= 0; i--) {
  9239. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  9240. if (frame_offset - cells < loader_ctx->frame_offset_bottom) {
  9241. set_error_buf(error_buf, error_buf_size, "frame offset underflow");
  9242. goto fail;
  9243. }
  9244. if (cells == 4) {
  9245. bool needs_copy = false;
  9246. int16 v128_dynamic = dynamic_offset - cells;
  9247. for (int j = 0; j < 4; j++) {
  9248. if (*(frame_offset - j - 1) != (v128_dynamic + j)) {
  9249. needs_copy = true;
  9250. break;
  9251. }
  9252. }
  9253. if (needs_copy) {
  9254. value_count++;
  9255. total_cel_num += cells;
  9256. }
  9257. frame_offset -= cells;
  9258. dynamic_offset = v128_dynamic;
  9259. }
  9260. else {
  9261. frame_offset -= cells;
  9262. dynamic_offset -= cells;
  9263. if (dynamic_offset != *frame_offset) {
  9264. value_count++;
  9265. total_cel_num += cells;
  9266. }
  9267. }
  9268. }
  9269. if (value_count) {
  9270. uint32 j = 0;
  9271. uint8 *emit_data = NULL, *cells = NULL;
  9272. int16 *src_offsets = NULL;
  9273. uint16 *dst_offsets = NULL;
  9274. uint64 size =
  9275. (uint64)value_count
  9276. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  9277. /* Allocate memory for the emit data */
  9278. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9279. return false;
  9280. cells = emit_data;
  9281. src_offsets = (int16 *)(cells + value_count);
  9282. dst_offsets = (uint16 *)(src_offsets + value_count);
  9283. /* insert op_copy before else opcode */
  9284. if (opcode == WASM_OP_ELSE)
  9285. skip_label();
  9286. emit_label(EXT_OP_COPY_STACK_VALUES);
  9287. /* Part a) */
  9288. emit_uint32(loader_ctx, value_count);
  9289. /* Part b) */
  9290. emit_uint32(loader_ctx, total_cel_num);
  9291. /* Second traversal to get each value's cell num, src offset and dst
  9292. * offset. */
  9293. frame_offset = frame_offset_org;
  9294. dynamic_offset = dynamic_offset_org;
  9295. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  9296. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  9297. if (cell == 4) {
  9298. bool needs_copy = false;
  9299. int16 v128_dynamic = dynamic_offset - cell;
  9300. for (int k = 0; k < 4; k++) {
  9301. if (*(frame_offset - k - 1) != (v128_dynamic + k)) {
  9302. needs_copy = true;
  9303. break;
  9304. }
  9305. }
  9306. if (needs_copy) {
  9307. cells[j] = cell;
  9308. src_offsets[j] = *(frame_offset - cell);
  9309. dst_offsets[j] = v128_dynamic;
  9310. j++;
  9311. }
  9312. frame_offset -= cell;
  9313. dynamic_offset = v128_dynamic;
  9314. }
  9315. else {
  9316. frame_offset -= cell;
  9317. dynamic_offset -= cell;
  9318. if (dynamic_offset != *frame_offset) {
  9319. cells[j] = cell;
  9320. /* src offset */
  9321. src_offsets[j] = *frame_offset;
  9322. /* dst offset */
  9323. dst_offsets[j] = dynamic_offset;
  9324. j++;
  9325. }
  9326. }
  9327. if (opcode == WASM_OP_ELSE) {
  9328. if (cell == 4) {
  9329. for (int k = 0; k < cell; k++) {
  9330. *(frame_offset + k) = dynamic_offset + k;
  9331. }
  9332. }
  9333. else {
  9334. *frame_offset = dynamic_offset;
  9335. }
  9336. }
  9337. else {
  9338. loader_ctx->frame_offset = frame_offset;
  9339. loader_ctx->dynamic_offset = dynamic_offset;
  9340. if (!(wasm_loader_push_frame_offset(
  9341. loader_ctx, return_types[i], disable_emit,
  9342. operand_offset, error_buf, error_buf_size))) {
  9343. wasm_runtime_free(emit_data);
  9344. goto fail;
  9345. }
  9346. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9347. loader_ctx->frame_offset = frame_offset_org;
  9348. loader_ctx->dynamic_offset = dynamic_offset_org;
  9349. }
  9350. }
  9351. bh_assert(j == value_count);
  9352. /* Emit the cells, src_offsets and dst_offsets */
  9353. for (j = 0; j < value_count; j++)
  9354. emit_byte(loader_ctx, cells[j]);
  9355. for (j = 0; j < value_count; j++)
  9356. emit_operand(loader_ctx, src_offsets[j]);
  9357. for (j = 0; j < value_count; j++)
  9358. emit_operand(loader_ctx, dst_offsets[j]);
  9359. if (opcode == WASM_OP_ELSE)
  9360. emit_label(opcode);
  9361. wasm_runtime_free(emit_data);
  9362. }
  9363. return true;
  9364. fail:
  9365. return false;
  9366. }
  9367. #endif /* WASM_ENABLE_FAST_INTERP */
  9368. #define PUSH_TYPE(type) \
  9369. do { \
  9370. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  9371. error_buf_size))) \
  9372. goto fail; \
  9373. } while (0)
  9374. #define POP_TYPE(type) \
  9375. do { \
  9376. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  9377. error_buf_size))) \
  9378. goto fail; \
  9379. } while (0)
  9380. #if WASM_ENABLE_GC == 0
  9381. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9382. do { \
  9383. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9384. _start_addr, error_buf, \
  9385. error_buf_size)) \
  9386. goto fail; \
  9387. } while (0)
  9388. #define POP_CSP() \
  9389. do { \
  9390. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9391. goto fail; \
  9392. } while (0)
  9393. #else
  9394. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9395. do { \
  9396. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9397. _start_addr, error_buf, \
  9398. error_buf_size)) \
  9399. goto fail; \
  9400. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  9401. error_buf, error_buf_size)) { \
  9402. goto fail; \
  9403. } \
  9404. } while (0)
  9405. #define POP_CSP() \
  9406. do { \
  9407. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  9408. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9409. goto fail; \
  9410. } while (0)
  9411. #endif /* end of WASM_ENABLE_GC == 0 */
  9412. #if WASM_ENABLE_GC == 0
  9413. #define GET_LOCAL_REFTYPE() (void)0
  9414. #else
  9415. #define GET_LOCAL_REFTYPE() \
  9416. do { \
  9417. if (wasm_is_type_multi_byte_type(local_type)) { \
  9418. WASMRefType *_ref_type; \
  9419. if (local_idx < param_count) \
  9420. _ref_type = wasm_reftype_map_find( \
  9421. param_reftype_maps, param_reftype_map_count, local_idx); \
  9422. else \
  9423. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  9424. local_reftype_map_count, \
  9425. local_idx - param_count); \
  9426. bh_assert(_ref_type); \
  9427. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  9428. wasm_reftype_struct_size(_ref_type)); \
  9429. } \
  9430. } while (0)
  9431. #endif /* end of WASM_ENABLE_GC == 0 */
  9432. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  9433. do { \
  9434. read_leb_uint32(p, p_end, local_idx); \
  9435. if (local_idx >= param_count + local_count) { \
  9436. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  9437. goto fail; \
  9438. } \
  9439. local_type = local_idx < param_count \
  9440. ? param_types[local_idx] \
  9441. : local_types[local_idx - param_count]; \
  9442. local_offset = local_offsets[local_idx]; \
  9443. GET_LOCAL_REFTYPE(); \
  9444. } while (0)
  9445. static bool
  9446. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  9447. {
  9448. if (module->memory_count == 0 && module->import_memory_count == 0) {
  9449. set_error_buf(error_buf, error_buf_size, "unknown memory");
  9450. return false;
  9451. }
  9452. return true;
  9453. }
  9454. #define CHECK_MEMORY() \
  9455. do { \
  9456. if (!check_memory(module, error_buf, error_buf_size)) \
  9457. goto fail; \
  9458. } while (0)
  9459. static bool
  9460. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9461. uint32 error_buf_size)
  9462. {
  9463. uint8 mem_access_aligns[] = {
  9464. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  9465. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  9466. };
  9467. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  9468. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  9469. set_error_buf(error_buf, error_buf_size,
  9470. "invalid memop flags: alignment must not be larger "
  9471. "than natural");
  9472. return false;
  9473. }
  9474. return true;
  9475. }
  9476. #if WASM_ENABLE_SIMD != 0
  9477. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  9478. || (WASM_ENABLE_FAST_INTERP != 0)
  9479. static bool
  9480. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9481. uint32 error_buf_size)
  9482. {
  9483. uint8 mem_access_aligns[] = {
  9484. 4, /* load */
  9485. 3, 3, 3, 3, 3, 3, /* load and extend */
  9486. 0, 1, 2, 3, /* load and splat */
  9487. 4, /* store */
  9488. };
  9489. uint8 mem_access_aligns_load_lane[] = {
  9490. 0, 1, 2, 3, /* load lane */
  9491. 0, 1, 2, 3, /* store lane */
  9492. 2, 3 /* store zero */
  9493. };
  9494. if (!((opcode <= SIMD_v128_store)
  9495. || (SIMD_v128_load8_lane <= opcode
  9496. && opcode <= SIMD_v128_load64_zero))) {
  9497. set_error_buf(error_buf, error_buf_size,
  9498. "the opcode doesn't include memarg");
  9499. return false;
  9500. }
  9501. if ((opcode <= SIMD_v128_store
  9502. && align > mem_access_aligns[opcode - SIMD_v128_load])
  9503. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  9504. && align > mem_access_aligns_load_lane[opcode
  9505. - SIMD_v128_load8_lane])) {
  9506. set_error_buf(error_buf, error_buf_size,
  9507. "invalid memop flags: alignment must not be larger "
  9508. "than natural");
  9509. return false;
  9510. }
  9511. return true;
  9512. }
  9513. static bool
  9514. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  9515. uint32 error_buf_size)
  9516. {
  9517. switch (opcode) {
  9518. case SIMD_i8x16_extract_lane_s:
  9519. case SIMD_i8x16_extract_lane_u:
  9520. case SIMD_i8x16_replace_lane:
  9521. if (lane >= 16) {
  9522. goto fail;
  9523. }
  9524. break;
  9525. case SIMD_i16x8_extract_lane_s:
  9526. case SIMD_i16x8_extract_lane_u:
  9527. case SIMD_i16x8_replace_lane:
  9528. if (lane >= 8) {
  9529. goto fail;
  9530. }
  9531. break;
  9532. case SIMD_i32x4_extract_lane:
  9533. case SIMD_i32x4_replace_lane:
  9534. case SIMD_f32x4_extract_lane:
  9535. case SIMD_f32x4_replace_lane:
  9536. if (lane >= 4) {
  9537. goto fail;
  9538. }
  9539. break;
  9540. case SIMD_i64x2_extract_lane:
  9541. case SIMD_i64x2_replace_lane:
  9542. case SIMD_f64x2_extract_lane:
  9543. case SIMD_f64x2_replace_lane:
  9544. if (lane >= 2) {
  9545. goto fail;
  9546. }
  9547. break;
  9548. case SIMD_v128_load8_lane:
  9549. case SIMD_v128_load16_lane:
  9550. case SIMD_v128_load32_lane:
  9551. case SIMD_v128_load64_lane:
  9552. case SIMD_v128_store8_lane:
  9553. case SIMD_v128_store16_lane:
  9554. case SIMD_v128_store32_lane:
  9555. case SIMD_v128_store64_lane:
  9556. case SIMD_v128_load32_zero:
  9557. case SIMD_v128_load64_zero:
  9558. {
  9559. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9560. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9561. goto fail;
  9562. }
  9563. break;
  9564. }
  9565. default:
  9566. goto fail;
  9567. }
  9568. return true;
  9569. fail:
  9570. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9571. return false;
  9572. }
  9573. static bool
  9574. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9575. {
  9576. uint8 i;
  9577. for (i = 0; i != 16; ++i) {
  9578. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9579. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9580. return false;
  9581. }
  9582. }
  9583. return true;
  9584. }
  9585. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9586. #endif /* end of WASM_ENABLE_SIMD */
  9587. #if WASM_ENABLE_SHARED_MEMORY != 0
  9588. static bool
  9589. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9590. uint32 error_buf_size)
  9591. {
  9592. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9593. uint8 mem_access_aligns[] = {
  9594. 2, 3, 0, 1, 0, 1, 2,
  9595. };
  9596. uint8 expect;
  9597. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9598. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9599. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9600. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9601. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9602. }
  9603. else {
  9604. /* 7 opcodes in every group */
  9605. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9606. }
  9607. if (align != expect) {
  9608. set_error_buf(error_buf, error_buf_size,
  9609. "alignment isn't equal to natural");
  9610. return false;
  9611. }
  9612. return true;
  9613. }
  9614. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9615. static bool
  9616. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9617. char *error_buf, uint32 error_buf_size)
  9618. {
  9619. BranchBlock *target_block, *cur_block;
  9620. BlockType *target_block_type;
  9621. uint8 type, *types = NULL, *frame_ref;
  9622. uint32 arity = 0;
  9623. int32 i, available_stack_cell;
  9624. uint16 cell_num;
  9625. #if WASM_ENABLE_GC != 0
  9626. WASMRefTypeMap *frame_reftype_map;
  9627. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9628. WASMRefType *ref_type;
  9629. uint32 reftype_map_count = 0;
  9630. int32 available_reftype_map;
  9631. bool is_type_multi_byte;
  9632. #endif
  9633. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9634. uint8 *frame_ref_after_popped = NULL;
  9635. uint8 frame_ref_tmp[4] = { 0 };
  9636. uint8 *frame_ref_buf = frame_ref_tmp;
  9637. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9638. #if WASM_ENABLE_GC != 0
  9639. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9640. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9641. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9642. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9643. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9644. #endif
  9645. #if WASM_ENABLE_FAST_INTERP != 0
  9646. int16 *frame_offset_old = loader_ctx->frame_offset;
  9647. int16 *frame_offset_after_popped = NULL;
  9648. int16 frame_offset_tmp[4] = { 0 };
  9649. int16 *frame_offset_buf = frame_offset_tmp;
  9650. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9651. #endif
  9652. bool ret = false;
  9653. bh_assert(loader_ctx->csp_num > 0);
  9654. if (loader_ctx->csp_num - 1 < depth) {
  9655. set_error_buf(error_buf, error_buf_size,
  9656. "unknown label, "
  9657. "unexpected end of section or function");
  9658. return false;
  9659. }
  9660. cur_block = loader_ctx->frame_csp - 1;
  9661. target_block = loader_ctx->frame_csp - (depth + 1);
  9662. target_block_type = &target_block->block_type;
  9663. frame_ref = loader_ctx->frame_ref;
  9664. #if WASM_ENABLE_GC != 0
  9665. frame_reftype_map = loader_ctx->frame_reftype_map;
  9666. #endif
  9667. /* Note: loop's arity is different from if and block. loop's arity is
  9668. * its parameter count while if and block arity is result count.
  9669. */
  9670. #if WASM_ENABLE_GC == 0
  9671. if (target_block->label_type == LABEL_TYPE_LOOP)
  9672. arity = block_type_get_param_types(target_block_type, &types);
  9673. else
  9674. arity = block_type_get_result_types(target_block_type, &types);
  9675. #else
  9676. if (target_block->label_type == LABEL_TYPE_LOOP)
  9677. arity = block_type_get_param_types(target_block_type, &types,
  9678. &reftype_maps, &reftype_map_count);
  9679. else
  9680. arity = block_type_get_result_types(target_block_type, &types,
  9681. &reftype_maps, &reftype_map_count);
  9682. #endif
  9683. /* If the stack is in polymorphic state, just clear the stack
  9684. * and then re-push the values to make the stack top values
  9685. * match block type. */
  9686. if (cur_block->is_stack_polymorphic) {
  9687. #if WASM_ENABLE_GC != 0
  9688. int32 j = (int32)reftype_map_count - 1;
  9689. #endif
  9690. for (i = (int32)arity - 1; i >= 0; i--) {
  9691. #if WASM_ENABLE_GC != 0
  9692. if (wasm_is_type_multi_byte_type(types[i])) {
  9693. bh_assert(reftype_maps[j].index == i);
  9694. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9695. reftype_maps[j].ref_type,
  9696. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9697. j--;
  9698. }
  9699. #endif
  9700. #if WASM_ENABLE_FAST_INTERP != 0
  9701. POP_OFFSET_TYPE(types[i]);
  9702. #endif
  9703. POP_TYPE(types[i]);
  9704. }
  9705. /* Backup stack data since it may be changed in the below
  9706. push operations, and the stack data may be used when
  9707. checking other target blocks of opcode br_table */
  9708. if (opcode == WASM_OP_BR_TABLE) {
  9709. uint64 total_size;
  9710. frame_ref_after_popped = loader_ctx->frame_ref;
  9711. total_size = (uint64)sizeof(uint8)
  9712. * (frame_ref_old - frame_ref_after_popped);
  9713. if (total_size > sizeof(frame_ref_tmp)
  9714. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9715. error_buf_size))) {
  9716. goto fail;
  9717. }
  9718. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9719. frame_ref_after_popped, (uint32)total_size);
  9720. #if WASM_ENABLE_GC != 0
  9721. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9722. total_size =
  9723. (uint64)sizeof(WASMRefTypeMap)
  9724. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9725. if (total_size > sizeof(frame_reftype_map_tmp)
  9726. && !(frame_reftype_map_buf = loader_malloc(
  9727. total_size, error_buf, error_buf_size))) {
  9728. goto fail;
  9729. }
  9730. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9731. frame_reftype_map_after_popped, (uint32)total_size);
  9732. #endif
  9733. #if WASM_ENABLE_FAST_INTERP != 0
  9734. frame_offset_after_popped = loader_ctx->frame_offset;
  9735. total_size = (uint64)sizeof(int16)
  9736. * (frame_offset_old - frame_offset_after_popped);
  9737. if (total_size > sizeof(frame_offset_tmp)
  9738. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9739. error_buf_size))) {
  9740. goto fail;
  9741. }
  9742. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9743. frame_offset_after_popped, (uint32)total_size);
  9744. #endif
  9745. }
  9746. #if WASM_ENABLE_GC != 0
  9747. j = 0;
  9748. #endif
  9749. for (i = 0; i < (int32)arity; i++) {
  9750. #if WASM_ENABLE_GC != 0
  9751. if (wasm_is_type_multi_byte_type(types[i])) {
  9752. bh_assert(reftype_maps[j].index == i);
  9753. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9754. reftype_maps[j].ref_type,
  9755. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9756. j++;
  9757. }
  9758. #endif
  9759. #if WASM_ENABLE_FAST_INTERP != 0
  9760. bool disable_emit = true;
  9761. int16 operand_offset = 0;
  9762. PUSH_OFFSET_TYPE(types[i]);
  9763. #endif
  9764. PUSH_TYPE(types[i]);
  9765. }
  9766. #if WASM_ENABLE_FAST_INTERP != 0
  9767. emit_br_info(target_block, opcode == WASM_OP_BR);
  9768. #endif
  9769. /* Restore the stack data, note that frame_ref_bottom,
  9770. frame_reftype_map_bottom, frame_offset_bottom may be
  9771. re-allocated in the above push operations */
  9772. if (opcode == WASM_OP_BR_TABLE) {
  9773. uint32 total_size;
  9774. /* The stack operand num should not be smaller than before
  9775. after pop and push operations */
  9776. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9777. loader_ctx->stack_cell_num = stack_cell_num_old;
  9778. loader_ctx->frame_ref =
  9779. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9780. total_size = (uint32)(sizeof(uint8)
  9781. * (frame_ref_old - frame_ref_after_popped));
  9782. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9783. frame_ref_buf, total_size);
  9784. #if WASM_ENABLE_GC != 0
  9785. /* The stack operand num should not be smaller than before
  9786. after pop and push operations */
  9787. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9788. loader_ctx->reftype_map_num = reftype_map_num_old;
  9789. loader_ctx->frame_reftype_map =
  9790. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9791. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9792. * (frame_reftype_map_old
  9793. - frame_reftype_map_after_popped));
  9794. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9795. total_size, frame_reftype_map_buf, total_size);
  9796. #endif
  9797. #if WASM_ENABLE_FAST_INTERP != 0
  9798. loader_ctx->frame_offset =
  9799. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9800. total_size =
  9801. (uint32)(sizeof(int16)
  9802. * (frame_offset_old - frame_offset_after_popped));
  9803. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9804. total_size, frame_offset_buf, total_size);
  9805. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9806. #endif
  9807. }
  9808. ret = true;
  9809. goto cleanup_and_return;
  9810. }
  9811. available_stack_cell =
  9812. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9813. #if WASM_ENABLE_GC != 0
  9814. available_reftype_map =
  9815. (int32)(loader_ctx->reftype_map_num
  9816. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9817. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9818. #endif
  9819. /* Check stack top values match target block type */
  9820. for (i = (int32)arity - 1; i >= 0; i--) {
  9821. type = types[i];
  9822. #if WASM_ENABLE_GC != 0
  9823. ref_type = NULL;
  9824. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9825. if (is_type_multi_byte) {
  9826. bh_assert(reftype_map);
  9827. ref_type = reftype_map->ref_type;
  9828. }
  9829. #endif
  9830. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9831. break;
  9832. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9833. #if WASM_ENABLE_GC != 0
  9834. frame_reftype_map, available_reftype_map,
  9835. #endif
  9836. type,
  9837. #if WASM_ENABLE_GC != 0
  9838. ref_type,
  9839. #endif
  9840. error_buf, error_buf_size)) {
  9841. goto fail;
  9842. }
  9843. cell_num = wasm_value_type_cell_num(types[i]);
  9844. frame_ref -= cell_num;
  9845. available_stack_cell -= cell_num;
  9846. #if WASM_ENABLE_GC != 0
  9847. if (is_type_multi_byte) {
  9848. frame_reftype_map--;
  9849. available_reftype_map--;
  9850. reftype_map--;
  9851. }
  9852. #endif
  9853. }
  9854. #if WASM_ENABLE_FAST_INTERP != 0
  9855. emit_br_info(target_block, opcode == WASM_OP_BR);
  9856. #endif
  9857. ret = true;
  9858. cleanup_and_return:
  9859. fail:
  9860. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9861. wasm_runtime_free(frame_ref_buf);
  9862. #if WASM_ENABLE_GC != 0
  9863. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9864. wasm_runtime_free(frame_reftype_map_buf);
  9865. #endif
  9866. #if WASM_ENABLE_FAST_INTERP != 0
  9867. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9868. wasm_runtime_free(frame_offset_buf);
  9869. #endif
  9870. return ret;
  9871. }
  9872. static BranchBlock *
  9873. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9874. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9875. {
  9876. uint8 *p = *p_buf, *p_end = buf_end;
  9877. BranchBlock *frame_csp_tmp;
  9878. uint32 depth;
  9879. read_leb_uint32(p, p_end, depth);
  9880. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9881. error_buf_size)) {
  9882. goto fail;
  9883. }
  9884. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9885. *p_buf = p;
  9886. return frame_csp_tmp;
  9887. fail:
  9888. return NULL;
  9889. }
  9890. #if WASM_ENABLE_EXCE_HANDLING != 0
  9891. static BranchBlock *
  9892. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9893. uint8 *buf_end, char *error_buf,
  9894. uint32 error_buf_size)
  9895. {
  9896. uint8 *p = *p_buf, *p_end = buf_end;
  9897. BranchBlock *frame_csp_tmp;
  9898. uint32 depth;
  9899. read_leb_uint32(p, p_end, depth);
  9900. /*
  9901. * Note: "delegate 0" means the surrounding block, not the
  9902. * try-delegate block itself.
  9903. *
  9904. * Note: the caller hasn't popped the try-delegate frame yet.
  9905. */
  9906. bh_assert(loader_ctx->csp_num > 0);
  9907. if (loader_ctx->csp_num - 1 <= depth) {
  9908. #if WASM_ENABLE_SPEC_TEST == 0
  9909. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9910. #else
  9911. set_error_buf(error_buf, error_buf_size, "unknown label");
  9912. #endif
  9913. goto fail;
  9914. }
  9915. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9916. #if WASM_ENABLE_FAST_INTERP != 0
  9917. emit_br_info(frame_csp_tmp, false);
  9918. #endif
  9919. *p_buf = p;
  9920. return frame_csp_tmp;
  9921. fail:
  9922. return NULL;
  9923. }
  9924. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9925. static bool
  9926. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9927. char *error_buf, uint32 error_buf_size)
  9928. {
  9929. BlockType *block_type = &block->block_type;
  9930. uint8 *return_types = NULL;
  9931. uint32 return_count = 0;
  9932. int32 available_stack_cell, return_cell_num, i;
  9933. uint8 *frame_ref = NULL;
  9934. #if WASM_ENABLE_GC != 0
  9935. WASMRefTypeMap *frame_reftype_map;
  9936. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9937. WASMRefType *ref_type;
  9938. uint32 param_count, return_reftype_map_count = 0;
  9939. int32 available_reftype_map =
  9940. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9941. #endif
  9942. available_stack_cell =
  9943. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9944. #if WASM_ENABLE_GC == 0
  9945. return_count = block_type_get_result_types(block_type, &return_types);
  9946. #else
  9947. return_count = block_type_get_result_types(block_type, &return_types,
  9948. &return_reftype_maps,
  9949. &return_reftype_map_count);
  9950. param_count =
  9951. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9952. (void)param_count;
  9953. #endif
  9954. return_cell_num =
  9955. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9956. /* If the stack is in polymorphic state, just clear the stack
  9957. * and then re-push the values to make the stack top values
  9958. * match block type. */
  9959. if (block->is_stack_polymorphic) {
  9960. #if WASM_ENABLE_GC != 0
  9961. int32 j = (int32)return_reftype_map_count - 1;
  9962. #endif
  9963. for (i = (int32)return_count - 1; i >= 0; i--) {
  9964. #if WASM_ENABLE_GC != 0
  9965. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9966. bh_assert(return_reftype_maps[j].index == i + param_count);
  9967. bh_memcpy_s(
  9968. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9969. return_reftype_maps[j].ref_type,
  9970. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9971. j--;
  9972. }
  9973. #endif
  9974. #if WASM_ENABLE_FAST_INTERP != 0
  9975. POP_OFFSET_TYPE(return_types[i]);
  9976. #endif
  9977. POP_TYPE(return_types[i]);
  9978. }
  9979. /* Check stack is empty */
  9980. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9981. set_error_buf(
  9982. error_buf, error_buf_size,
  9983. "type mismatch: stack size does not match block type");
  9984. goto fail;
  9985. }
  9986. #if WASM_ENABLE_GC != 0
  9987. j = 0;
  9988. #endif
  9989. for (i = 0; i < (int32)return_count; i++) {
  9990. #if WASM_ENABLE_GC != 0
  9991. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9992. bh_assert(return_reftype_maps[j].index == i + param_count);
  9993. bh_memcpy_s(
  9994. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9995. return_reftype_maps[j].ref_type,
  9996. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9997. j++;
  9998. }
  9999. #endif
  10000. #if WASM_ENABLE_FAST_INTERP != 0
  10001. bool disable_emit = true;
  10002. int16 operand_offset = 0;
  10003. PUSH_OFFSET_TYPE(return_types[i]);
  10004. #endif
  10005. PUSH_TYPE(return_types[i]);
  10006. }
  10007. return true;
  10008. }
  10009. if (available_stack_cell != return_cell_num) {
  10010. #if WASM_ENABLE_EXCE_HANDLING != 0
  10011. /* testspec: this error message format is expected by try_catch.wast */
  10012. snprintf(
  10013. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  10014. block->label_type == LABEL_TYPE_TRY
  10015. || (block->label_type == LABEL_TYPE_CATCH
  10016. && return_cell_num > 0)
  10017. ? "instruction"
  10018. : "block",
  10019. return_cell_num > 0 ? type2str(return_types[0]) : "",
  10020. " but stack has ",
  10021. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  10022. : "");
  10023. goto fail;
  10024. #else
  10025. set_error_buf(error_buf, error_buf_size,
  10026. "type mismatch: stack size does not match block type");
  10027. goto fail;
  10028. #endif
  10029. }
  10030. /* Check stack values match return types */
  10031. frame_ref = loader_ctx->frame_ref;
  10032. #if WASM_ENABLE_GC != 0
  10033. frame_reftype_map = loader_ctx->frame_reftype_map;
  10034. return_reftype_map =
  10035. return_reftype_map_count
  10036. ? return_reftype_maps + return_reftype_map_count - 1
  10037. : NULL;
  10038. #endif
  10039. for (i = (int32)return_count - 1; i >= 0; i--) {
  10040. uint8 type = return_types[i];
  10041. #if WASM_ENABLE_GC != 0
  10042. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  10043. ref_type = NULL;
  10044. if (is_type_multi_byte) {
  10045. bh_assert(return_reftype_map);
  10046. ref_type = return_reftype_map->ref_type;
  10047. }
  10048. #endif
  10049. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  10050. #if WASM_ENABLE_GC != 0
  10051. frame_reftype_map, available_reftype_map,
  10052. #endif
  10053. type,
  10054. #if WASM_ENABLE_GC != 0
  10055. ref_type,
  10056. #endif
  10057. error_buf, error_buf_size))
  10058. return false;
  10059. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  10060. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  10061. #if WASM_ENABLE_GC != 0
  10062. if (is_type_multi_byte) {
  10063. frame_reftype_map--;
  10064. available_reftype_map--;
  10065. return_reftype_map--;
  10066. }
  10067. #endif
  10068. }
  10069. return true;
  10070. fail:
  10071. return false;
  10072. }
  10073. #if WASM_ENABLE_FAST_INTERP != 0
  10074. /* Copy parameters to dynamic space.
  10075. * 1) POP original parameter out;
  10076. * 2) Push and copy original values to dynamic space.
  10077. * The copy instruction format:
  10078. * Part a: param count
  10079. * Part b: all param total cell num
  10080. * Part c: each param's cell_num, src offset and dst offset
  10081. * Part d: each param's src offset
  10082. * Part e: each param's dst offset
  10083. */
  10084. static bool
  10085. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  10086. uint32 error_buf_size)
  10087. {
  10088. bool ret = false;
  10089. int16 *frame_offset = NULL;
  10090. uint8 *cells = NULL, cell;
  10091. int16 *src_offsets = NULL;
  10092. uint8 *emit_data = NULL;
  10093. uint32 i;
  10094. BranchBlock *block = loader_ctx->frame_csp - 1;
  10095. BlockType *block_type = &block->block_type;
  10096. WASMFuncType *wasm_type = block_type->u.type;
  10097. uint32 param_count = block_type->u.type->param_count;
  10098. int16 condition_offset = 0;
  10099. bool disable_emit = false;
  10100. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  10101. int16 operand_offset = 0;
  10102. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  10103. bh_assert(size > 0);
  10104. /* For if block, we also need copy the condition operand offset. */
  10105. if (is_if_block)
  10106. size += sizeof(*cells) + sizeof(*src_offsets);
  10107. /* Allocate memory for the emit data */
  10108. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  10109. return false;
  10110. cells = emit_data;
  10111. src_offsets = (int16 *)(cells + param_count);
  10112. if (is_if_block)
  10113. condition_offset = *loader_ctx->frame_offset;
  10114. /* POP original parameter out */
  10115. for (i = 0; i < param_count; i++) {
  10116. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  10117. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  10118. }
  10119. frame_offset = loader_ctx->frame_offset;
  10120. /* Get each param's cell num and src offset */
  10121. for (i = 0; i < param_count; i++) {
  10122. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  10123. cells[i] = cell;
  10124. src_offsets[i] = *frame_offset;
  10125. frame_offset += cell;
  10126. }
  10127. /* emit copy instruction */
  10128. emit_label(EXT_OP_COPY_STACK_VALUES);
  10129. /* Part a) */
  10130. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  10131. /* Part b) */
  10132. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  10133. : wasm_type->param_cell_num);
  10134. /* Part c) */
  10135. for (i = 0; i < param_count; i++)
  10136. emit_byte(loader_ctx, cells[i]);
  10137. if (is_if_block)
  10138. emit_byte(loader_ctx, 1);
  10139. /* Part d) */
  10140. for (i = 0; i < param_count; i++)
  10141. emit_operand(loader_ctx, src_offsets[i]);
  10142. if (is_if_block)
  10143. emit_operand(loader_ctx, condition_offset);
  10144. /* Since the start offset to save the block's params and
  10145. * the start offset to save the block's results may be
  10146. * different, we remember the dynamic offset for loop block
  10147. * so that we can use it to copy the stack operands to the
  10148. * loop block's params in wasm_loader_emit_br_info. */
  10149. if (block->label_type == LABEL_TYPE_LOOP)
  10150. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10151. /* Part e) */
  10152. /* Push to dynamic space. The push will emit the dst offset. */
  10153. for (i = 0; i < param_count; i++)
  10154. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  10155. if (is_if_block)
  10156. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  10157. ret = true;
  10158. fail:
  10159. /* Free the emit data */
  10160. wasm_runtime_free(emit_data);
  10161. return ret;
  10162. }
  10163. #endif
  10164. #if WASM_ENABLE_GC == 0
  10165. #define RESET_REFTYPE_MAP_STACK() (void)0
  10166. #else
  10167. #define RESET_REFTYPE_MAP_STACK() \
  10168. do { \
  10169. loader_ctx->reftype_map_num = \
  10170. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  10171. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  10172. + loader_ctx->reftype_map_num; \
  10173. } while (0)
  10174. #endif
  10175. /* reset the stack to the state of before entering the last block */
  10176. #if WASM_ENABLE_FAST_INTERP != 0
  10177. #define RESET_STACK() \
  10178. do { \
  10179. loader_ctx->stack_cell_num = \
  10180. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  10181. loader_ctx->frame_ref = \
  10182. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  10183. loader_ctx->frame_offset = \
  10184. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  10185. RESET_REFTYPE_MAP_STACK(); \
  10186. } while (0)
  10187. #else
  10188. #define RESET_STACK() \
  10189. do { \
  10190. loader_ctx->stack_cell_num = \
  10191. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  10192. loader_ctx->frame_ref = \
  10193. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  10194. RESET_REFTYPE_MAP_STACK(); \
  10195. } while (0)
  10196. #endif
  10197. /* set current block's stack polymorphic state */
  10198. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  10199. do { \
  10200. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  10201. _cur_block->is_stack_polymorphic = flag; \
  10202. } while (0)
  10203. #define BLOCK_HAS_PARAM(block_type) \
  10204. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  10205. #define PRESERVE_LOCAL_FOR_BLOCK() \
  10206. do { \
  10207. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  10208. error_buf_size))) { \
  10209. goto fail; \
  10210. } \
  10211. } while (0)
  10212. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10213. static bool
  10214. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  10215. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  10216. uint32 error_buf_size)
  10217. {
  10218. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  10219. return false;
  10220. }
  10221. if (table_idx < module->import_table_count) {
  10222. if (p_elem_type)
  10223. *p_elem_type =
  10224. module->import_tables[table_idx].u.table.table_type.elem_type;
  10225. #if WASM_ENABLE_GC != 0
  10226. if (p_ref_type)
  10227. *((WASMRefType **)p_ref_type) =
  10228. module->import_tables[table_idx]
  10229. .u.table.table_type.elem_ref_type;
  10230. #endif
  10231. }
  10232. else {
  10233. if (p_elem_type)
  10234. *p_elem_type =
  10235. module->tables[table_idx - module->import_table_count]
  10236. .table_type.elem_type;
  10237. #if WASM_ENABLE_GC != 0
  10238. if (p_ref_type)
  10239. *((WASMRefType **)p_ref_type) =
  10240. module->tables[table_idx - module->import_table_count]
  10241. .table_type.elem_ref_type;
  10242. #endif
  10243. }
  10244. return true;
  10245. }
  10246. static bool
  10247. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  10248. uint8 *p_elem_type, void **p_elem_ref_type,
  10249. char *error_buf, uint32 error_buf_size)
  10250. {
  10251. if (table_seg_idx >= module->table_seg_count) {
  10252. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  10253. table_seg_idx);
  10254. return false;
  10255. }
  10256. if (p_elem_type) {
  10257. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  10258. }
  10259. #if WASM_ENABLE_GC != 0
  10260. if (p_elem_ref_type)
  10261. *((WASMRefType **)p_elem_ref_type) =
  10262. module->table_segments[table_seg_idx].elem_ref_type;
  10263. #endif
  10264. return true;
  10265. }
  10266. #endif
  10267. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  10268. const uint8 *
  10269. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  10270. uint32 *len)
  10271. {
  10272. WASMCustomSection *section = module->custom_section_list;
  10273. while (section) {
  10274. if ((section->name_len == strlen(name))
  10275. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  10276. if (len) {
  10277. *len = section->content_len;
  10278. }
  10279. return section->content_addr;
  10280. }
  10281. section = section->next;
  10282. }
  10283. return NULL;
  10284. }
  10285. #endif
  10286. #if 0
  10287. #define HANDLE_OPCODE(opcode) #opcode
  10288. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  10289. #undef HANDLE_OPCODE
  10290. #endif
  10291. #if WASM_ENABLE_FAST_INTERP == 0
  10292. #define pb_read_leb_uint32 read_leb_uint32
  10293. #define pb_read_leb_int32 read_leb_int32
  10294. #define pb_read_leb_int64 read_leb_int64
  10295. #define pb_read_leb_memarg read_leb_memarg
  10296. #define pb_read_leb_mem_offset read_leb_mem_offset
  10297. #else
  10298. /* Read leb without malformed format check */
  10299. static uint64
  10300. read_leb_quick(uint8 **p_buf, uint32 maxbits, bool sign)
  10301. {
  10302. uint8 *buf = *p_buf;
  10303. uint64 result = 0, byte = 0;
  10304. uint32 shift = 0;
  10305. do {
  10306. byte = *buf++;
  10307. result |= ((byte & 0x7f) << shift);
  10308. shift += 7;
  10309. } while (byte & 0x80);
  10310. if (sign && (shift < maxbits) && (byte & 0x40)) {
  10311. /* Sign extend */
  10312. result |= (~((uint64)0)) << shift;
  10313. }
  10314. *p_buf = buf;
  10315. return result;
  10316. }
  10317. #define pb_read_leb_uint32(p, p_end, res) \
  10318. do { \
  10319. if (!loader_ctx->p_code_compiled) \
  10320. /* Enable format check in the first scan */ \
  10321. read_leb_uint32(p, p_end, res); \
  10322. else \
  10323. /* Disable format check in the second scan */ \
  10324. res = (uint32)read_leb_quick(&p, 32, false); \
  10325. } while (0)
  10326. #define pb_read_leb_int32(p, p_end, res) \
  10327. do { \
  10328. if (!loader_ctx->p_code_compiled) \
  10329. /* Enable format check in the first scan */ \
  10330. read_leb_int32(p, p_end, res); \
  10331. else \
  10332. /* Disable format check in the second scan */ \
  10333. res = (int32)read_leb_quick(&p, 32, true); \
  10334. } while (0)
  10335. #define pb_read_leb_int64(p, p_end, res) \
  10336. do { \
  10337. if (!loader_ctx->p_code_compiled) \
  10338. /* Enable format check in the first scan */ \
  10339. read_leb_int64(p, p_end, res); \
  10340. else \
  10341. /* Disable format check in the second scan */ \
  10342. res = (int64)read_leb_quick(&p, 64, true); \
  10343. } while (0)
  10344. #if WASM_ENABLE_MULTI_MEMORY != 0
  10345. #define pb_read_leb_memarg read_leb_memarg
  10346. #else
  10347. #define pb_read_leb_memarg pb_read_leb_uint32
  10348. #endif
  10349. #if WASM_ENABLE_MEMORY64 != 0
  10350. #define pb_read_leb_mem_offset read_leb_mem_offset
  10351. #else
  10352. #define pb_read_leb_mem_offset pb_read_leb_uint32
  10353. #endif
  10354. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  10355. static bool
  10356. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  10357. uint32 cur_func_idx, char *error_buf,
  10358. uint32 error_buf_size)
  10359. {
  10360. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  10361. uint32 param_count, local_count, global_count;
  10362. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type,
  10363. table_elem_idx_type;
  10364. BlockType func_block_type;
  10365. uint16 *local_offsets, local_offset;
  10366. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  10367. uint32 table_seg_idx, data_seg_idx, count, align, i;
  10368. mem_offset_t mem_offset;
  10369. int32 i32_const = 0;
  10370. int64 i64_const;
  10371. uint8 opcode;
  10372. bool return_value = false;
  10373. WASMLoaderContext *loader_ctx;
  10374. BranchBlock *frame_csp_tmp;
  10375. #if WASM_ENABLE_GC != 0
  10376. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  10377. uint32 param_reftype_map_count, local_reftype_map_count;
  10378. int32 heap_type;
  10379. WASMRefType wasm_ref_type = { 0 };
  10380. bool need_ref_type_map;
  10381. #endif
  10382. #if WASM_ENABLE_FAST_INTERP != 0
  10383. int16 operand_offset = 0;
  10384. uint8 last_op = 0;
  10385. bool disable_emit, preserve_local = false, if_condition_available = true;
  10386. float32 f32_const;
  10387. float64 f64_const;
  10388. /*
  10389. * It means that the fast interpreter detected an exception while preparing,
  10390. * typically near the block opcode, but it did not immediately trigger
  10391. * the exception. The loader should be capable of identifying it near
  10392. * the end opcode and then raising the exception.
  10393. */
  10394. bool pending_exception = false;
  10395. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  10396. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  10397. #endif
  10398. #if WASM_ENABLE_MEMORY64 != 0
  10399. bool is_memory64 = has_module_memory64(module);
  10400. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  10401. #else
  10402. mem_offset_type = VALUE_TYPE_I32;
  10403. table_elem_idx_type = VALUE_TYPE_I32;
  10404. #endif
  10405. uint32 memidx;
  10406. global_count = module->import_global_count + module->global_count;
  10407. param_count = func->func_type->param_count;
  10408. param_types = func->func_type->types;
  10409. func_block_type.is_value_type = false;
  10410. func_block_type.u.type = func->func_type;
  10411. local_count = func->local_count;
  10412. local_types = func->local_types;
  10413. local_offsets = func->local_offsets;
  10414. #if WASM_ENABLE_GC != 0
  10415. param_reftype_maps = func->func_type->ref_type_maps;
  10416. param_reftype_map_count = func->func_type->ref_type_map_count;
  10417. local_reftype_maps = func->local_ref_type_maps;
  10418. local_reftype_map_count = func->local_ref_type_map_count;
  10419. #endif
  10420. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  10421. goto fail;
  10422. }
  10423. #if WASM_ENABLE_GC != 0
  10424. loader_ctx->module = module;
  10425. loader_ctx->ref_type_set = module->ref_type_set;
  10426. loader_ctx->ref_type_tmp = &wasm_ref_type;
  10427. #endif
  10428. #if WASM_ENABLE_FAST_INTERP != 0
  10429. /* For the first traverse, the initial value of preserved_local_offset has
  10430. * not been determined, we use the INT16_MAX to represent that a slot has
  10431. * been copied to preserve space. For second traverse, this field will be
  10432. * set to the appropriate value in wasm_loader_ctx_reinit.
  10433. * This is for Issue #1230,
  10434. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  10435. * drop opcodes need to know which slots are preserved, so those slots will
  10436. * not be treated as dynamically allocated slots */
  10437. loader_ctx->preserved_local_offset = INT16_MAX;
  10438. re_scan:
  10439. if (loader_ctx->code_compiled_size > 0) {
  10440. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  10441. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  10442. goto fail;
  10443. }
  10444. p = func->code;
  10445. func->code_compiled = loader_ctx->p_code_compiled;
  10446. func->code_compiled_size = loader_ctx->code_compiled_size;
  10447. if (loader_ctx->i64_const_num > 0) {
  10448. int64 *i64_consts_old = loader_ctx->i64_consts;
  10449. /* Sort the i64 consts */
  10450. qsort(i64_consts_old, loader_ctx->i64_const_num, sizeof(int64),
  10451. cmp_i64_const);
  10452. /* Remove the duplicated i64 consts */
  10453. uint32 k = 1;
  10454. for (i = 1; i < loader_ctx->i64_const_num; i++) {
  10455. if (i64_consts_old[i] != i64_consts_old[i - 1]) {
  10456. i64_consts_old[k++] = i64_consts_old[i];
  10457. }
  10458. }
  10459. if (k < loader_ctx->i64_const_num) {
  10460. int64 *i64_consts_new;
  10461. /* Try to reallocate memory with a smaller size */
  10462. if ((i64_consts_new =
  10463. wasm_runtime_malloc((uint32)sizeof(int64) * k))) {
  10464. bh_memcpy_s(i64_consts_new, (uint32)sizeof(int64) * k,
  10465. i64_consts_old, (uint32)sizeof(int64) * k);
  10466. /* Free the old memory */
  10467. wasm_runtime_free(i64_consts_old);
  10468. loader_ctx->i64_consts = i64_consts_new;
  10469. loader_ctx->i64_const_max_num = k;
  10470. }
  10471. loader_ctx->i64_const_num = k;
  10472. }
  10473. }
  10474. if (loader_ctx->v128_const_num > 0) {
  10475. V128 *v128_consts_old = loader_ctx->v128_consts;
  10476. /* Sort the v128 consts */
  10477. qsort(v128_consts_old, loader_ctx->v128_const_num, sizeof(V128),
  10478. cmp_v128_const);
  10479. /* Remove the duplicated v128 consts */
  10480. uint32 k = 1;
  10481. for (i = 1; i < loader_ctx->v128_const_num; i++) {
  10482. if (!(memcmp(&v128_consts_old[i], &v128_consts_old[i - 1],
  10483. sizeof(V128))
  10484. == 0)) {
  10485. v128_consts_old[k++] = v128_consts_old[i];
  10486. }
  10487. }
  10488. if (k < loader_ctx->v128_const_num) {
  10489. V128 *v128_consts_new;
  10490. /* Try to reallocate memory with a smaller size */
  10491. if ((v128_consts_new =
  10492. wasm_runtime_malloc((uint32)sizeof(V128) * k))) {
  10493. bh_memcpy_s(v128_consts_new, (uint32)sizeof(V128) * k,
  10494. v128_consts_old, (uint32)sizeof(V128) * k);
  10495. /* Free the old memory */
  10496. wasm_runtime_free(v128_consts_old);
  10497. loader_ctx->v128_consts = v128_consts_new;
  10498. loader_ctx->v128_const_max_num = k;
  10499. }
  10500. loader_ctx->v128_const_num = k;
  10501. }
  10502. }
  10503. if (loader_ctx->i32_const_num > 0) {
  10504. int32 *i32_consts_old = loader_ctx->i32_consts;
  10505. /* Sort the i32 consts */
  10506. qsort(i32_consts_old, loader_ctx->i32_const_num, sizeof(int32),
  10507. cmp_i32_const);
  10508. /* Remove the duplicated i32 consts */
  10509. uint32 k = 1;
  10510. for (i = 1; i < loader_ctx->i32_const_num; i++) {
  10511. if (i32_consts_old[i] != i32_consts_old[i - 1]) {
  10512. i32_consts_old[k++] = i32_consts_old[i];
  10513. }
  10514. }
  10515. if (k < loader_ctx->i32_const_num) {
  10516. int32 *i32_consts_new;
  10517. /* Try to reallocate memory with a smaller size */
  10518. if ((i32_consts_new =
  10519. wasm_runtime_malloc((uint32)sizeof(int32) * k))) {
  10520. bh_memcpy_s(i32_consts_new, (uint32)sizeof(int32) * k,
  10521. i32_consts_old, (uint32)sizeof(int32) * k);
  10522. /* Free the old memory */
  10523. wasm_runtime_free(i32_consts_old);
  10524. loader_ctx->i32_consts = i32_consts_new;
  10525. loader_ctx->i32_const_max_num = k;
  10526. }
  10527. loader_ctx->i32_const_num = k;
  10528. }
  10529. }
  10530. }
  10531. #endif
  10532. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  10533. while (p < p_end) {
  10534. opcode = *p++;
  10535. #if WASM_ENABLE_FAST_INTERP != 0
  10536. p_org = p;
  10537. disable_emit = false;
  10538. emit_label(opcode);
  10539. #endif
  10540. switch (opcode) {
  10541. case WASM_OP_UNREACHABLE:
  10542. RESET_STACK();
  10543. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10544. break;
  10545. case WASM_OP_NOP:
  10546. #if WASM_ENABLE_FAST_INTERP != 0
  10547. skip_label();
  10548. #endif
  10549. break;
  10550. case WASM_OP_IF:
  10551. {
  10552. #if WASM_ENABLE_FAST_INTERP != 0
  10553. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  10554. int32 available_stack_cell =
  10555. (int32)(loader_ctx->stack_cell_num
  10556. - parent_block->stack_cell_num);
  10557. if (available_stack_cell <= 0
  10558. && parent_block->is_stack_polymorphic)
  10559. if_condition_available = false;
  10560. else
  10561. if_condition_available = true;
  10562. PRESERVE_LOCAL_FOR_BLOCK();
  10563. #endif
  10564. #if WASM_ENABLE_GC == 0
  10565. POP_I32();
  10566. #endif
  10567. goto handle_op_block_and_loop;
  10568. }
  10569. case WASM_OP_BLOCK:
  10570. case WASM_OP_LOOP:
  10571. #if WASM_ENABLE_EXCE_HANDLING != 0
  10572. case WASM_OP_TRY:
  10573. if (opcode == WASM_OP_TRY) {
  10574. /*
  10575. * keep track of exception handlers to account for
  10576. * memory allocation
  10577. */
  10578. func->exception_handler_count++;
  10579. /*
  10580. * try is a block
  10581. * do nothing special, but execution continues to
  10582. * to handle_op_block_and_loop,
  10583. * and that be pushes the csp
  10584. */
  10585. }
  10586. #endif
  10587. #if WASM_ENABLE_FAST_INTERP != 0
  10588. PRESERVE_LOCAL_FOR_BLOCK();
  10589. #endif
  10590. handle_op_block_and_loop:
  10591. {
  10592. uint8 value_type;
  10593. BlockType block_type;
  10594. #if WASM_ENABLE_FAST_INTERP != 0
  10595. uint32 available_params = 0;
  10596. #endif
  10597. CHECK_BUF(p, p_end, 1);
  10598. value_type = read_uint8(p);
  10599. if (is_byte_a_type(value_type)) {
  10600. /* If the first byte is one of these special values:
  10601. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  10602. * the single return value. */
  10603. block_type.is_value_type = true;
  10604. block_type.u.value_type.type = value_type;
  10605. #if WASM_ENABLE_WAMR_COMPILER != 0
  10606. if (value_type == VALUE_TYPE_V128)
  10607. module->is_simd_used = true;
  10608. else if (value_type == VALUE_TYPE_FUNCREF
  10609. || value_type == VALUE_TYPE_EXTERNREF)
  10610. module->is_ref_types_used = true;
  10611. #endif
  10612. #if WASM_ENABLE_GC != 0
  10613. if (value_type != VALUE_TYPE_VOID) {
  10614. p_org = p;
  10615. p--;
  10616. if (!resolve_value_type((const uint8 **)&p, p_end,
  10617. module, module->type_count,
  10618. &need_ref_type_map,
  10619. &wasm_ref_type, false,
  10620. error_buf, error_buf_size)) {
  10621. goto fail;
  10622. }
  10623. if (need_ref_type_map) {
  10624. block_type.u.value_type.ref_type_map.index = 0;
  10625. if (!(block_type.u.value_type.ref_type_map
  10626. .ref_type = reftype_set_insert(
  10627. module->ref_type_set, &wasm_ref_type,
  10628. error_buf, error_buf_size))) {
  10629. goto fail;
  10630. }
  10631. }
  10632. /* Set again as the type might be changed, e.g.
  10633. (ref null any) to anyref */
  10634. block_type.u.value_type.type = wasm_ref_type.ref_type;
  10635. #if WASM_ENABLE_FAST_INTERP == 0
  10636. while (p_org < p) {
  10637. #if WASM_ENABLE_DEBUG_INTERP != 0
  10638. if (!record_fast_op(module, p_org, *p_org,
  10639. error_buf, error_buf_size)) {
  10640. goto fail;
  10641. }
  10642. #endif
  10643. /* Ignore extra bytes for interpreter */
  10644. *p_org++ = WASM_OP_NOP;
  10645. }
  10646. #endif
  10647. }
  10648. #endif /* end of WASM_ENABLE_GC != 0 */
  10649. }
  10650. else {
  10651. int32 type_index;
  10652. /* Resolve the leb128 encoded type index as block type */
  10653. p--;
  10654. p_org = p - 1;
  10655. pb_read_leb_int32(p, p_end, type_index);
  10656. if (!check_function_type(module, type_index, error_buf,
  10657. error_buf_size)) {
  10658. goto fail;
  10659. }
  10660. block_type.is_value_type = false;
  10661. block_type.u.type =
  10662. (WASMFuncType *)module->types[type_index];
  10663. #if WASM_ENABLE_FAST_INTERP == 0
  10664. /* If block use type index as block type, change the opcode
  10665. * to new extended opcode so that interpreter can resolve
  10666. * the block quickly.
  10667. */
  10668. #if WASM_ENABLE_DEBUG_INTERP != 0
  10669. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10670. error_buf_size)) {
  10671. goto fail;
  10672. }
  10673. #endif
  10674. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  10675. #endif
  10676. }
  10677. #if WASM_ENABLE_GC != 0
  10678. if (opcode == WASM_OP_IF) {
  10679. POP_I32();
  10680. }
  10681. #endif
  10682. /* Pop block parameters from stack */
  10683. if (BLOCK_HAS_PARAM(block_type)) {
  10684. WASMFuncType *wasm_type = block_type.u.type;
  10685. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10686. #if WASM_ENABLE_FAST_INTERP != 0
  10687. uint32 cell_num;
  10688. available_params = block_type.u.type->param_count;
  10689. #endif
  10690. for (i = 0; i < block_type.u.type->param_count; i++) {
  10691. int32 available_stack_cell =
  10692. (int32)(loader_ctx->stack_cell_num
  10693. - cur_block->stack_cell_num);
  10694. if (available_stack_cell <= 0
  10695. && cur_block->is_stack_polymorphic) {
  10696. #if WASM_ENABLE_FAST_INTERP != 0
  10697. available_params = i;
  10698. #endif
  10699. break;
  10700. }
  10701. POP_TYPE(
  10702. wasm_type->types[wasm_type->param_count - i - 1]);
  10703. #if WASM_ENABLE_FAST_INTERP != 0
  10704. /* decrease the frame_offset pointer accordingly to keep
  10705. * consistent with frame_ref stack */
  10706. cell_num = wasm_value_type_cell_num(
  10707. wasm_type->types[wasm_type->param_count - i - 1]);
  10708. loader_ctx->frame_offset -= cell_num;
  10709. if (loader_ctx->frame_offset
  10710. < loader_ctx->frame_offset_bottom) {
  10711. LOG_DEBUG(
  10712. "frame_offset underflow, roll back and "
  10713. "let following stack checker report it\n");
  10714. loader_ctx->frame_offset += cell_num;
  10715. pending_exception = true;
  10716. break;
  10717. }
  10718. #endif
  10719. }
  10720. }
  10721. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10722. block_type, p);
  10723. /* Pass parameters to block */
  10724. if (BLOCK_HAS_PARAM(block_type)) {
  10725. WASMFuncType *func_type = block_type.u.type;
  10726. #if WASM_ENABLE_GC != 0
  10727. WASMRefType *ref_type;
  10728. uint32 j = 0;
  10729. #endif
  10730. for (i = 0; i < func_type->param_count; i++) {
  10731. #if WASM_ENABLE_FAST_INTERP != 0
  10732. uint32 cell_num =
  10733. wasm_value_type_cell_num(func_type->types[i]);
  10734. if (i >= available_params) {
  10735. /* make sure enough space */
  10736. if (loader_ctx->p_code_compiled == NULL) {
  10737. loader_ctx->frame_offset += cell_num;
  10738. if (!check_offset_push(loader_ctx, error_buf,
  10739. error_buf_size))
  10740. goto fail;
  10741. /* for following dummy value assignment */
  10742. loader_ctx->frame_offset -= cell_num;
  10743. }
  10744. /* If there isn't enough data on stack, push a dummy
  10745. * offset to keep the stack consistent with
  10746. * frame_ref.
  10747. * Since the stack is already in polymorphic state,
  10748. * the opcode will not be executed, so the dummy
  10749. * offset won't cause any error */
  10750. for (uint32 n = 0; n < cell_num; n++) {
  10751. *loader_ctx->frame_offset++ = 0;
  10752. }
  10753. }
  10754. else {
  10755. loader_ctx->frame_offset += cell_num;
  10756. }
  10757. #endif
  10758. #if WASM_ENABLE_GC != 0
  10759. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10760. bh_assert(func_type->ref_type_maps[j].index == i);
  10761. ref_type = func_type->ref_type_maps[j].ref_type;
  10762. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10763. ref_type,
  10764. wasm_reftype_struct_size(ref_type));
  10765. j++;
  10766. }
  10767. #endif
  10768. PUSH_TYPE(func_type->types[i]);
  10769. }
  10770. }
  10771. #if WASM_ENABLE_FAST_INTERP != 0
  10772. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10773. skip_label();
  10774. if (BLOCK_HAS_PARAM(block_type)) {
  10775. /* Make sure params are in dynamic space */
  10776. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10777. error_buf_size))
  10778. goto fail;
  10779. }
  10780. if (opcode == WASM_OP_LOOP) {
  10781. (loader_ctx->frame_csp - 1)->code_compiled =
  10782. loader_ctx->p_code_compiled;
  10783. }
  10784. }
  10785. #if WASM_ENABLE_EXCE_HANDLING != 0
  10786. else if (opcode == WASM_OP_TRY) {
  10787. skip_label();
  10788. }
  10789. #endif
  10790. else if (opcode == WASM_OP_IF) {
  10791. BranchBlock *block = loader_ctx->frame_csp - 1;
  10792. /* If block has parameters, we should make sure they are in
  10793. * dynamic space. Otherwise, when else branch is missing,
  10794. * the later opcode may consume incorrect operand offset.
  10795. * Spec case:
  10796. * (func (export "params-id") (param i32) (result i32)
  10797. * (i32.const 1)
  10798. * (i32.const 2)
  10799. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10800. * (then)) (i32.add)
  10801. * )
  10802. *
  10803. * So we should emit a copy instruction before the if.
  10804. *
  10805. * And we also need to save the parameter offsets and
  10806. * recover them before entering else branch.
  10807. *
  10808. */
  10809. if (BLOCK_HAS_PARAM(block_type)) {
  10810. uint64 size;
  10811. /* In polymorphic state, there may be no if condition on
  10812. * the stack, so the offset may not emitted */
  10813. if (if_condition_available) {
  10814. /* skip the if condition operand offset */
  10815. wasm_loader_emit_backspace(loader_ctx,
  10816. sizeof(int16));
  10817. }
  10818. /* skip the if label */
  10819. skip_label();
  10820. /* Emit a copy instruction */
  10821. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10822. error_buf_size))
  10823. goto fail;
  10824. /* Emit the if instruction */
  10825. emit_label(opcode);
  10826. /* Emit the new condition operand offset */
  10827. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10828. /* Save top param_count values of frame_offset stack, so
  10829. * that we can recover it before executing else branch
  10830. */
  10831. size = sizeof(int16)
  10832. * (uint64)block_type.u.type->param_cell_num;
  10833. if (!(block->param_frame_offsets = loader_malloc(
  10834. size, error_buf, error_buf_size)))
  10835. goto fail;
  10836. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10837. loader_ctx->frame_offset
  10838. - size / sizeof(int16),
  10839. (uint32)size);
  10840. }
  10841. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10842. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10843. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10844. }
  10845. #endif
  10846. break;
  10847. }
  10848. #if WASM_ENABLE_EXCE_HANDLING != 0
  10849. case WASM_OP_THROW:
  10850. {
  10851. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10852. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10853. uint8 label_type = cur_block->label_type;
  10854. uint32 tag_index = 0;
  10855. pb_read_leb_int32(p, p_end, tag_index);
  10856. /* check validity of tag_index against module->tag_count */
  10857. /* check tag index is within the tag index space */
  10858. if (tag_index >= module->import_tag_count + module->tag_count) {
  10859. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10860. tag_index);
  10861. goto fail;
  10862. }
  10863. /* the tag_type is stored in either the WASMTag (section tags)
  10864. * or WASMTagImport (import tag) */
  10865. WASMFuncType *tag_type = NULL;
  10866. if (tag_index < module->import_tag_count) {
  10867. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10868. }
  10869. else {
  10870. tag_type =
  10871. module->tags[tag_index - module->import_tag_count]
  10872. ->tag_type;
  10873. }
  10874. if (tag_type->result_count != 0) {
  10875. set_error_buf(error_buf, error_buf_size,
  10876. "tag type signature does not return void");
  10877. goto fail;
  10878. }
  10879. int32 available_stack_cell =
  10880. (int32)(loader_ctx->stack_cell_num
  10881. - cur_block->stack_cell_num);
  10882. int32 tti;
  10883. /* Check stack values match return types by comparing tag param
  10884. * types with stack cells */
  10885. uint8 *frame_ref = loader_ctx->frame_ref;
  10886. #if WASM_ENABLE_GC != 0
  10887. WASMRefTypeMap *frame_reftype_map =
  10888. loader_ctx->frame_reftype_map;
  10889. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10890. /* Temporarily set these values since they may be used in
  10891. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10892. param_reftype_maps = tag_type->ref_type_maps;
  10893. /* For tag_type function, it shouldn't have result_count = 0 */
  10894. param_reftype_map_count = tag_type->ref_type_map_count;
  10895. param_count = tag_type->param_count;
  10896. #endif
  10897. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10898. #if WASM_ENABLE_GC != 0
  10899. local_type = tag_type->types[tti];
  10900. local_idx = tti;
  10901. /* Get the wasm_ref_type if the local_type is multibyte
  10902. type */
  10903. GET_LOCAL_REFTYPE();
  10904. #endif
  10905. if (!check_stack_top_values(
  10906. loader_ctx, frame_ref, available_stack_cell,
  10907. #if WASM_ENABLE_GC != 0
  10908. frame_reftype_map, frame_reftype_map_num,
  10909. #endif
  10910. tag_type->types[tti],
  10911. #if WASM_ENABLE_GC != 0
  10912. &wasm_ref_type,
  10913. #endif
  10914. error_buf, error_buf_size)) {
  10915. snprintf(error_buf, error_buf_size,
  10916. "type mismatch: instruction requires [%s] but "
  10917. "stack has [%s]",
  10918. tag_type->param_count > 0
  10919. ? type2str(tag_type->types[tti])
  10920. : "",
  10921. available_stack_cell > 0
  10922. ? type2str(*(loader_ctx->frame_ref - 1))
  10923. : "");
  10924. goto fail;
  10925. }
  10926. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10927. available_stack_cell -=
  10928. wasm_value_type_cell_num(tag_type->types[tti]);
  10929. }
  10930. #if WASM_ENABLE_GC != 0
  10931. /* Restore the values */
  10932. param_reftype_maps = func->func_type->ref_type_maps;
  10933. param_reftype_map_count = func->func_type->ref_type_map_count;
  10934. param_count = func->func_type->param_count;
  10935. #endif
  10936. /* throw is stack polymorphic */
  10937. (void)label_type;
  10938. RESET_STACK();
  10939. break;
  10940. }
  10941. case WASM_OP_RETHROW:
  10942. {
  10943. /* must be done before checking branch block */
  10944. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10945. /* check the target catching block: LABEL_TYPE_CATCH */
  10946. if (!(frame_csp_tmp =
  10947. check_branch_block(loader_ctx, &p, p_end, opcode,
  10948. error_buf, error_buf_size)))
  10949. goto fail;
  10950. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10951. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10952. /* trap according to spectest (rethrow.wast) */
  10953. set_error_buf(error_buf, error_buf_size,
  10954. "invalid rethrow label");
  10955. goto fail;
  10956. }
  10957. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10958. uint8 label_type = cur_block->label_type;
  10959. (void)label_type;
  10960. /* rethrow is stack polymorphic */
  10961. RESET_STACK();
  10962. break;
  10963. }
  10964. case WASM_OP_DELEGATE:
  10965. {
  10966. /* check target block is valid */
  10967. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10968. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10969. goto fail;
  10970. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10971. uint8 label_type = cur_block->label_type;
  10972. (void)label_type;
  10973. /* DELEGATE ends the block */
  10974. POP_CSP();
  10975. break;
  10976. }
  10977. case WASM_OP_CATCH:
  10978. {
  10979. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10980. uint8 label_type = cur_block->label_type;
  10981. uint32 tag_index = 0;
  10982. pb_read_leb_int32(p, p_end, tag_index);
  10983. /* check validity of tag_index against module->tag_count */
  10984. /* check tag index is within the tag index space */
  10985. if (tag_index >= module->import_tag_count + module->tag_count) {
  10986. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10987. __FUNCTION__);
  10988. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10989. goto fail;
  10990. }
  10991. /* the tag_type is stored in either the WASMTag (section tags)
  10992. * or WASMTagImport (import tag) */
  10993. WASMFuncType *func_type = NULL;
  10994. if (tag_index < module->import_tag_count) {
  10995. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10996. }
  10997. else {
  10998. func_type =
  10999. module->tags[tag_index - module->import_tag_count]
  11000. ->tag_type;
  11001. }
  11002. if (func_type->result_count != 0) {
  11003. set_error_buf(error_buf, error_buf_size,
  11004. "tag type signature does not return void");
  11005. goto fail;
  11006. }
  11007. /* check validity of current label (expect LABEL_TYPE_TRY or
  11008. * LABEL_TYPE_CATCH) */
  11009. if ((LABEL_TYPE_CATCH != label_type)
  11010. && (LABEL_TYPE_TRY != label_type)) {
  11011. set_error_buf(error_buf, error_buf_size,
  11012. "Unexpected block sequence encountered.");
  11013. goto fail;
  11014. }
  11015. /*
  11016. * replace frame_csp by LABEL_TYPE_CATCH
  11017. */
  11018. cur_block->label_type = LABEL_TYPE_CATCH;
  11019. /* RESET_STACK removes the values pushed in TRY or previous
  11020. * CATCH Blocks */
  11021. RESET_STACK();
  11022. #if WASM_ENABLE_GC != 0
  11023. WASMRefType *ref_type;
  11024. uint32 j = 0;
  11025. #endif
  11026. /* push types on the stack according to caught type */
  11027. for (i = 0; i < func_type->param_count; i++) {
  11028. #if WASM_ENABLE_GC != 0
  11029. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  11030. bh_assert(func_type->ref_type_maps[j].index == i);
  11031. ref_type = func_type->ref_type_maps[j].ref_type;
  11032. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11033. ref_type,
  11034. wasm_reftype_struct_size(ref_type));
  11035. j++;
  11036. }
  11037. #endif
  11038. PUSH_TYPE(func_type->types[i]);
  11039. }
  11040. break;
  11041. }
  11042. case WASM_OP_CATCH_ALL:
  11043. {
  11044. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11045. /* expecting a TRY or CATCH, anything else will be considered an
  11046. * error */
  11047. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  11048. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  11049. set_error_buf(error_buf, error_buf_size,
  11050. "Unexpected block sequence encountered.");
  11051. goto fail;
  11052. }
  11053. /* no immediates */
  11054. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  11055. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  11056. /* RESET_STACK removes the values pushed in TRY or previous
  11057. * CATCH Blocks */
  11058. RESET_STACK();
  11059. /* catch_all has no tagtype and therefore no parameters */
  11060. break;
  11061. }
  11062. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  11063. case WASM_OP_ELSE:
  11064. handle_op_else:
  11065. {
  11066. BranchBlock *block = NULL;
  11067. BlockType block_type;
  11068. if (loader_ctx->csp_num < 2
  11069. /* the matched if isn't found */
  11070. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  11071. /* duplicated else is found */
  11072. || (loader_ctx->frame_csp - 1)->else_addr) {
  11073. set_error_buf(
  11074. error_buf, error_buf_size,
  11075. "opcode else found without matched opcode if");
  11076. goto fail;
  11077. }
  11078. block = loader_ctx->frame_csp - 1;
  11079. /* check whether if branch's stack matches its result type */
  11080. if (!check_block_stack(loader_ctx, block, error_buf,
  11081. error_buf_size))
  11082. goto fail;
  11083. block->else_addr = p - 1;
  11084. block_type = block->block_type;
  11085. #if WASM_ENABLE_GC != 0
  11086. if (!wasm_loader_init_local_use_masks(
  11087. loader_ctx, local_count, error_buf, error_buf_size)) {
  11088. goto fail;
  11089. }
  11090. #endif
  11091. #if WASM_ENABLE_FAST_INTERP != 0
  11092. /* if the result of if branch is in local or const area, add a
  11093. * copy op */
  11094. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  11095. error_buf, error_buf_size)) {
  11096. goto fail;
  11097. }
  11098. emit_empty_label_addr_and_frame_ip(PATCH_END);
  11099. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  11100. #endif
  11101. RESET_STACK();
  11102. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  11103. /* Pass parameters to if-false branch */
  11104. if (BLOCK_HAS_PARAM(block_type)) {
  11105. for (i = 0; i < block_type.u.type->param_count; i++)
  11106. PUSH_TYPE(block_type.u.type->types[i]);
  11107. }
  11108. #if WASM_ENABLE_FAST_INTERP != 0
  11109. /* Recover top param_count values of frame_offset stack */
  11110. if (BLOCK_HAS_PARAM((block_type))) {
  11111. uint32 size;
  11112. size = sizeof(int16) * block_type.u.type->param_cell_num;
  11113. bh_memcpy_s(loader_ctx->frame_offset, size,
  11114. block->param_frame_offsets, size);
  11115. loader_ctx->frame_offset += (size / sizeof(int16));
  11116. }
  11117. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  11118. #endif
  11119. break;
  11120. }
  11121. case WASM_OP_END:
  11122. {
  11123. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11124. /* check whether block stack matches its result type */
  11125. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  11126. error_buf_size))
  11127. goto fail;
  11128. /* if there is no else branch, make a virtual else opcode for
  11129. easier integrity check and to copy the correct results to
  11130. the block return address for fast-interp mode:
  11131. change if block from `if ... end` to `if ... else end` */
  11132. if (cur_block->label_type == LABEL_TYPE_IF
  11133. && !cur_block->else_addr) {
  11134. opcode = WASM_OP_ELSE;
  11135. p--;
  11136. #if WASM_ENABLE_FAST_INTERP != 0
  11137. p_org = p;
  11138. skip_label();
  11139. disable_emit = false;
  11140. emit_label(opcode);
  11141. #endif
  11142. goto handle_op_else;
  11143. }
  11144. POP_CSP();
  11145. #if WASM_ENABLE_FAST_INTERP != 0
  11146. skip_label();
  11147. /* copy the result to the block return address */
  11148. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  11149. error_buf, error_buf_size)) {
  11150. /* it could be tmp frame_csp allocated from opcode like
  11151. * OP_BR and not counted in loader_ctx->csp_num, it won't
  11152. * be freed in wasm_loader_ctx_destroy(loader_ctx) so need
  11153. * to free the loader_ctx->frame_csp if fails */
  11154. free_label_patch_list(loader_ctx->frame_csp);
  11155. goto fail;
  11156. }
  11157. apply_label_patch(loader_ctx, 0, PATCH_END);
  11158. free_label_patch_list(loader_ctx->frame_csp);
  11159. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  11160. int32 idx;
  11161. uint8 ret_type;
  11162. emit_label(WASM_OP_RETURN);
  11163. for (idx = (int32)func->func_type->result_count - 1;
  11164. idx >= 0; idx--) {
  11165. ret_type = *(func->func_type->types
  11166. + func->func_type->param_count + idx);
  11167. POP_OFFSET_TYPE(ret_type);
  11168. }
  11169. }
  11170. #endif
  11171. if (loader_ctx->csp_num > 0) {
  11172. loader_ctx->frame_csp->end_addr = p - 1;
  11173. }
  11174. else {
  11175. /* end of function block, function will return */
  11176. if (p < p_end) {
  11177. set_error_buf(error_buf, error_buf_size,
  11178. "section size mismatch");
  11179. goto fail;
  11180. }
  11181. }
  11182. #if WASM_ENABLE_FAST_INTERP != 0
  11183. if (pending_exception) {
  11184. set_error_buf(
  11185. error_buf, error_buf_size,
  11186. "There is a pending exception needs to be handled");
  11187. goto fail;
  11188. }
  11189. #endif
  11190. break;
  11191. }
  11192. case WASM_OP_BR:
  11193. {
  11194. if (!(frame_csp_tmp =
  11195. check_branch_block(loader_ctx, &p, p_end, opcode,
  11196. error_buf, error_buf_size)))
  11197. goto fail;
  11198. RESET_STACK();
  11199. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11200. break;
  11201. }
  11202. case WASM_OP_BR_IF:
  11203. {
  11204. POP_I32();
  11205. if (!(frame_csp_tmp =
  11206. check_branch_block(loader_ctx, &p, p_end, opcode,
  11207. error_buf, error_buf_size)))
  11208. goto fail;
  11209. break;
  11210. }
  11211. case WASM_OP_BR_TABLE:
  11212. {
  11213. uint32 depth = 0, default_arity, arity = 0;
  11214. BranchBlock *target_block;
  11215. BlockType *target_block_type;
  11216. #if WASM_ENABLE_FAST_INTERP == 0
  11217. BrTableCache *br_table_cache = NULL;
  11218. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  11219. uint32 j;
  11220. #endif
  11221. pb_read_leb_uint32(p, p_end, count);
  11222. #if WASM_ENABLE_FAST_INTERP != 0
  11223. emit_uint32(loader_ctx, count);
  11224. #endif
  11225. POP_I32();
  11226. /* Get each depth and check it */
  11227. p_org = p;
  11228. for (i = 0; i <= count; i++) {
  11229. pb_read_leb_uint32(p, p_end, depth);
  11230. bh_assert(loader_ctx->csp_num > 0);
  11231. if (loader_ctx->csp_num - 1 < depth) {
  11232. set_error_buf(error_buf, error_buf_size,
  11233. "unknown label, "
  11234. "unexpected end of section or function");
  11235. goto fail;
  11236. }
  11237. }
  11238. p = p_org;
  11239. /* Get the default block's arity */
  11240. target_block = loader_ctx->frame_csp - (depth + 1);
  11241. target_block_type = &target_block->block_type;
  11242. default_arity = block_type_get_arity(target_block_type,
  11243. target_block->label_type);
  11244. #if WASM_ENABLE_FAST_INTERP == 0
  11245. p_depth_begin = p_depth = p;
  11246. #endif
  11247. for (i = 0; i <= count; i++) {
  11248. p_org = p;
  11249. pb_read_leb_uint32(p, p_end, depth);
  11250. p = p_org;
  11251. /* Get the target block's arity and check it */
  11252. target_block = loader_ctx->frame_csp - (depth + 1);
  11253. target_block_type = &target_block->block_type;
  11254. arity = block_type_get_arity(target_block_type,
  11255. target_block->label_type);
  11256. if (arity != default_arity) {
  11257. set_error_buf(error_buf, error_buf_size,
  11258. "type mismatch: br_table targets must "
  11259. "all use same result type");
  11260. goto fail;
  11261. }
  11262. if (!(frame_csp_tmp =
  11263. check_branch_block(loader_ctx, &p, p_end, opcode,
  11264. error_buf, error_buf_size))) {
  11265. goto fail;
  11266. }
  11267. #if WASM_ENABLE_FAST_INTERP == 0
  11268. if (br_table_cache) {
  11269. br_table_cache->br_depths[i] = depth;
  11270. }
  11271. else {
  11272. if (depth > 255) {
  11273. /* The depth cannot be stored in one byte,
  11274. create br_table cache to store each depth */
  11275. #if WASM_ENABLE_DEBUG_INTERP != 0
  11276. if (!record_fast_op(module, p_opcode, *p_opcode,
  11277. error_buf, error_buf_size)) {
  11278. goto fail;
  11279. }
  11280. #endif
  11281. if (!(br_table_cache = loader_malloc(
  11282. offsetof(BrTableCache, br_depths)
  11283. + sizeof(uint32)
  11284. * (uint64)(count + 1),
  11285. error_buf, error_buf_size))) {
  11286. goto fail;
  11287. }
  11288. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  11289. br_table_cache->br_table_op_addr = p_opcode;
  11290. br_table_cache->br_count = count;
  11291. /* Copy previous depths which are one byte */
  11292. for (j = 0; j < i; j++) {
  11293. br_table_cache->br_depths[j] = p_depth_begin[j];
  11294. }
  11295. br_table_cache->br_depths[i] = depth;
  11296. bh_list_insert(module->br_table_cache_list,
  11297. br_table_cache);
  11298. }
  11299. else {
  11300. /* The depth can be stored in one byte, use the
  11301. byte of the leb to store it */
  11302. *p_depth++ = (uint8)depth;
  11303. }
  11304. }
  11305. #endif
  11306. }
  11307. #if WASM_ENABLE_FAST_INTERP == 0
  11308. /* Set the tailing bytes to nop */
  11309. if (br_table_cache)
  11310. p_depth = p_depth_begin;
  11311. while (p_depth < p)
  11312. *p_depth++ = WASM_OP_NOP;
  11313. #endif
  11314. RESET_STACK();
  11315. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11316. break;
  11317. }
  11318. case WASM_OP_RETURN:
  11319. {
  11320. WASMFuncType *func_type = func->func_type;
  11321. int32 idx;
  11322. uint8 ret_type;
  11323. #if WASM_ENABLE_GC != 0
  11324. uint32 j = func_type->ref_type_map_count - 1;
  11325. #endif
  11326. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  11327. idx--) {
  11328. ret_type =
  11329. *(func_type->types + func_type->param_count + idx);
  11330. #if WASM_ENABLE_GC != 0
  11331. if (wasm_is_type_multi_byte_type(ret_type)) {
  11332. WASMRefType *ref_type =
  11333. func_type->ref_type_maps[j].ref_type;
  11334. bh_assert(func_type->ref_type_maps[j].index
  11335. == func_type->param_count + idx);
  11336. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11337. ref_type,
  11338. wasm_reftype_struct_size(ref_type));
  11339. j--;
  11340. }
  11341. #endif
  11342. #if WASM_ENABLE_FAST_INTERP != 0
  11343. /* emit the offset after return opcode */
  11344. POP_OFFSET_TYPE(ret_type);
  11345. #endif
  11346. POP_TYPE(ret_type);
  11347. }
  11348. RESET_STACK();
  11349. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11350. break;
  11351. }
  11352. case WASM_OP_CALL:
  11353. #if WASM_ENABLE_TAIL_CALL != 0
  11354. case WASM_OP_RETURN_CALL:
  11355. #endif
  11356. #if WASM_ENABLE_GC != 0
  11357. case WASM_OP_CALL_REF:
  11358. case WASM_OP_RETURN_CALL_REF:
  11359. #endif
  11360. {
  11361. WASMFuncType *func_type;
  11362. uint8 type;
  11363. int32 idx;
  11364. #if WASM_ENABLE_GC != 0
  11365. WASMRefType *ref_type;
  11366. uint32 type_idx1;
  11367. int32 j;
  11368. #endif
  11369. #if WASM_ENABLE_GC != 0
  11370. if (opcode == WASM_OP_CALL_REF
  11371. || opcode == WASM_OP_RETURN_CALL_REF) {
  11372. pb_read_leb_uint32(p, p_end, type_idx1);
  11373. if (!check_type_index(module, module->type_count, type_idx1,
  11374. error_buf, error_buf_size)) {
  11375. goto fail;
  11376. }
  11377. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  11378. set_error_buf(error_buf, error_buf_size,
  11379. "unknown function type");
  11380. goto fail;
  11381. }
  11382. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  11383. &type_idx, error_buf,
  11384. error_buf_size)) {
  11385. goto fail;
  11386. }
  11387. if (type == VALUE_TYPE_ANY) {
  11388. type_idx = type_idx1;
  11389. }
  11390. if (!check_type_index(module, module->type_count, type_idx,
  11391. error_buf, error_buf_size)) {
  11392. goto fail;
  11393. }
  11394. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  11395. set_error_buf(error_buf, error_buf_size,
  11396. "unknown function type");
  11397. goto fail;
  11398. }
  11399. if (!wasm_func_type_is_super_of(
  11400. (WASMFuncType *)module->types[type_idx1],
  11401. (WASMFuncType *)module->types[type_idx])) {
  11402. set_error_buf(error_buf, error_buf_size,
  11403. "function type mismatch");
  11404. goto fail;
  11405. }
  11406. func_type = (WASMFuncType *)module->types[type_idx];
  11407. }
  11408. else
  11409. #endif
  11410. {
  11411. pb_read_leb_uint32(p, p_end, func_idx);
  11412. #if WASM_ENABLE_FAST_INTERP != 0
  11413. /* we need to emit func_idx before arguments */
  11414. emit_uint32(loader_ctx, func_idx);
  11415. #endif
  11416. if (!check_function_index(module, func_idx, error_buf,
  11417. error_buf_size)) {
  11418. goto fail;
  11419. }
  11420. if (func_idx < module->import_function_count)
  11421. func_type = module->import_functions[func_idx]
  11422. .u.function.func_type;
  11423. else
  11424. func_type =
  11425. module
  11426. ->functions[func_idx
  11427. - module->import_function_count]
  11428. ->func_type;
  11429. }
  11430. if (func_type->param_count > 0) {
  11431. #if WASM_ENABLE_GC != 0
  11432. j = (int32)(func_type->result_ref_type_maps
  11433. - func_type->ref_type_maps - 1);
  11434. #endif
  11435. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11436. idx--) {
  11437. #if WASM_ENABLE_GC != 0
  11438. if (wasm_is_type_multi_byte_type(
  11439. func_type->types[idx])) {
  11440. ref_type = func_type->ref_type_maps[j].ref_type;
  11441. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11442. ref_type,
  11443. wasm_reftype_struct_size(ref_type));
  11444. j--;
  11445. }
  11446. #endif
  11447. #if WASM_ENABLE_FAST_INTERP != 0
  11448. POP_OFFSET_TYPE(func_type->types[idx]);
  11449. #endif
  11450. POP_TYPE(func_type->types[idx]);
  11451. }
  11452. }
  11453. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11454. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  11455. #endif
  11456. #if WASM_ENABLE_GC != 0
  11457. j = (int32)(func_type->result_ref_type_maps
  11458. - func_type->ref_type_maps);
  11459. #endif
  11460. for (i = 0; i < func_type->result_count; i++) {
  11461. #if WASM_ENABLE_GC != 0
  11462. if (wasm_is_type_multi_byte_type(
  11463. func_type->types[func_type->param_count + i])) {
  11464. ref_type = func_type->ref_type_maps[j].ref_type;
  11465. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11466. ref_type,
  11467. wasm_reftype_struct_size(ref_type));
  11468. j++;
  11469. }
  11470. #endif
  11471. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11472. #if WASM_ENABLE_FAST_INTERP != 0
  11473. /* Here we emit each return value's dynamic_offset. But
  11474. * in fact these offsets are continuous, so interpreter
  11475. * only need to get the first return value's offset.
  11476. */
  11477. PUSH_OFFSET_TYPE(
  11478. func_type->types[func_type->param_count + i]);
  11479. #endif
  11480. }
  11481. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11482. }
  11483. else {
  11484. #if WASM_ENABLE_GC == 0
  11485. if (func_type->result_count
  11486. != func->func_type->result_count) {
  11487. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11488. "type mismatch: expect ",
  11489. func->func_type->result_count,
  11490. " return values but got other");
  11491. goto fail;
  11492. }
  11493. for (i = 0; i < func_type->result_count; i++) {
  11494. type = func->func_type
  11495. ->types[func->func_type->param_count + i];
  11496. if (func_type->types[func_type->param_count + i]
  11497. != type) {
  11498. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11499. "type mismatch: expect ",
  11500. type2str(type), " but got other");
  11501. goto fail;
  11502. }
  11503. }
  11504. #else
  11505. if (!wasm_func_type_result_is_subtype_of(
  11506. func_type, func->func_type, module->types,
  11507. module->type_count)) {
  11508. set_error_buf(
  11509. error_buf, error_buf_size,
  11510. "type mismatch: invalid func result types");
  11511. goto fail;
  11512. }
  11513. #endif
  11514. RESET_STACK();
  11515. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11516. }
  11517. #endif
  11518. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11519. || WASM_ENABLE_WAMR_COMPILER != 0
  11520. func->has_op_func_call = true;
  11521. #endif
  11522. (void)type;
  11523. break;
  11524. }
  11525. /*
  11526. * if disable reference type: call_indirect typeidx, 0x00
  11527. * if enable reference type: call_indirect typeidx, tableidx
  11528. */
  11529. case WASM_OP_CALL_INDIRECT:
  11530. #if WASM_ENABLE_TAIL_CALL != 0
  11531. case WASM_OP_RETURN_CALL_INDIRECT:
  11532. #endif
  11533. {
  11534. int32 idx;
  11535. WASMFuncType *func_type;
  11536. uint32 tbl_elem_type;
  11537. #if WASM_ENABLE_GC != 0
  11538. WASMRefType *elem_ref_type = NULL;
  11539. #endif
  11540. pb_read_leb_uint32(p, p_end, type_idx);
  11541. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11542. #if WASM_ENABLE_WAMR_COMPILER != 0
  11543. if (p + 1 < p_end && *p != 0x00) {
  11544. /*
  11545. * Any non-0x00 byte requires the ref types proposal.
  11546. * This is different from checking the table_idx value
  11547. * since `0x80 0x00` etc. are all valid encodings of zero.
  11548. */
  11549. module->is_ref_types_used = true;
  11550. }
  11551. #endif
  11552. pb_read_leb_uint32(p, p_end, table_idx);
  11553. #else
  11554. CHECK_BUF(p, p_end, 1);
  11555. table_idx = read_uint8(p);
  11556. #endif
  11557. if (!check_table_index(module, table_idx, error_buf,
  11558. error_buf_size)) {
  11559. goto fail;
  11560. }
  11561. tbl_elem_type =
  11562. table_idx < module->import_table_count
  11563. ? module->import_tables[table_idx]
  11564. .u.table.table_type.elem_type
  11565. : module->tables[table_idx - module->import_table_count]
  11566. .table_type.elem_type;
  11567. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  11568. if (tbl_elem_type != VALUE_TYPE_FUNCREF) {
  11569. set_error_buf_v(error_buf, error_buf_size,
  11570. "type mismatch: instruction requires table "
  11571. "of functions but table %u has externref",
  11572. table_idx);
  11573. goto fail;
  11574. }
  11575. #elif WASM_ENABLE_GC != 0
  11576. /* Table element must match type ref null func */
  11577. elem_ref_type =
  11578. table_idx < module->import_table_count
  11579. ? module->import_tables[table_idx]
  11580. .u.table.table_type.elem_ref_type
  11581. : module->tables[table_idx - module->import_table_count]
  11582. .table_type.elem_ref_type;
  11583. if (!wasm_reftype_is_subtype_of(
  11584. tbl_elem_type, elem_ref_type, REF_TYPE_FUNCREF, NULL,
  11585. module->types, module->type_count)) {
  11586. set_error_buf_v(error_buf, error_buf_size,
  11587. "type mismatch: instruction requires "
  11588. "reference type t match type ref null func"
  11589. "in table %u",
  11590. table_idx);
  11591. goto fail;
  11592. }
  11593. #else
  11594. (void)tbl_elem_type;
  11595. #endif
  11596. #if WASM_ENABLE_FAST_INTERP != 0
  11597. /* we need to emit before arguments */
  11598. #if WASM_ENABLE_TAIL_CALL != 0
  11599. emit_byte(loader_ctx, opcode);
  11600. #endif
  11601. emit_uint32(loader_ctx, type_idx);
  11602. emit_uint32(loader_ctx, table_idx);
  11603. #endif
  11604. #if WASM_ENABLE_MEMORY64 != 0
  11605. table_elem_idx_type = is_table_64bit(module, table_idx)
  11606. ? VALUE_TYPE_I64
  11607. : VALUE_TYPE_I32;
  11608. #endif
  11609. /* skip elem idx */
  11610. POP_TBL_ELEM_IDX();
  11611. if (!check_function_type(module, type_idx, error_buf,
  11612. error_buf_size)) {
  11613. goto fail;
  11614. }
  11615. func_type = (WASMFuncType *)module->types[type_idx];
  11616. if (func_type->param_count > 0) {
  11617. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11618. idx--) {
  11619. #if WASM_ENABLE_FAST_INTERP != 0
  11620. POP_OFFSET_TYPE(func_type->types[idx]);
  11621. #endif
  11622. POP_TYPE(func_type->types[idx]);
  11623. }
  11624. }
  11625. #if WASM_ENABLE_TAIL_CALL != 0
  11626. if (opcode == WASM_OP_CALL_INDIRECT) {
  11627. #endif
  11628. for (i = 0; i < func_type->result_count; i++) {
  11629. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11630. #if WASM_ENABLE_FAST_INTERP != 0
  11631. PUSH_OFFSET_TYPE(
  11632. func_type->types[func_type->param_count + i]);
  11633. #endif
  11634. }
  11635. #if WASM_ENABLE_TAIL_CALL != 0
  11636. }
  11637. else {
  11638. uint8 type;
  11639. if (func_type->result_count
  11640. != func->func_type->result_count) {
  11641. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11642. "type mismatch: expect ",
  11643. func->func_type->result_count,
  11644. " return values but got other");
  11645. goto fail;
  11646. }
  11647. for (i = 0; i < func_type->result_count; i++) {
  11648. type = func->func_type
  11649. ->types[func->func_type->param_count + i];
  11650. if (func_type->types[func_type->param_count + i]
  11651. != type) {
  11652. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11653. "type mismatch: expect ",
  11654. type2str(type), " but got other");
  11655. goto fail;
  11656. }
  11657. }
  11658. RESET_STACK();
  11659. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11660. }
  11661. #endif
  11662. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11663. || WASM_ENABLE_WAMR_COMPILER != 0
  11664. func->has_op_func_call = true;
  11665. #endif
  11666. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11667. func->has_op_call_indirect = true;
  11668. #endif
  11669. break;
  11670. }
  11671. case WASM_OP_DROP:
  11672. {
  11673. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11674. int32 available_stack_cell =
  11675. (int32)(loader_ctx->stack_cell_num
  11676. - cur_block->stack_cell_num);
  11677. if (available_stack_cell <= 0
  11678. && !cur_block->is_stack_polymorphic) {
  11679. set_error_buf(error_buf, error_buf_size,
  11680. "type mismatch, opcode drop was found "
  11681. "but stack was empty");
  11682. goto fail;
  11683. }
  11684. if (available_stack_cell > 0) {
  11685. #if WASM_ENABLE_GC != 0
  11686. if (wasm_is_type_multi_byte_type(
  11687. *(loader_ctx->frame_ref - 1))) {
  11688. bh_assert((int32)(loader_ctx->reftype_map_num
  11689. - cur_block->reftype_map_num)
  11690. > 0);
  11691. loader_ctx->frame_reftype_map--;
  11692. loader_ctx->reftype_map_num--;
  11693. }
  11694. #endif
  11695. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  11696. loader_ctx->frame_ref--;
  11697. loader_ctx->stack_cell_num--;
  11698. #if WASM_ENABLE_FAST_INTERP != 0
  11699. skip_label();
  11700. loader_ctx->frame_offset--;
  11701. if ((*(loader_ctx->frame_offset)
  11702. > loader_ctx->start_dynamic_offset)
  11703. && (*(loader_ctx->frame_offset)
  11704. < loader_ctx->max_dynamic_offset))
  11705. loader_ctx->dynamic_offset--;
  11706. #endif
  11707. }
  11708. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  11709. loader_ctx->frame_ref -= 2;
  11710. loader_ctx->stack_cell_num -= 2;
  11711. #if WASM_ENABLE_FAST_INTERP == 0
  11712. *(p - 1) = WASM_OP_DROP_64;
  11713. #endif
  11714. #if WASM_ENABLE_FAST_INTERP != 0
  11715. skip_label();
  11716. loader_ctx->frame_offset -= 2;
  11717. if ((*(loader_ctx->frame_offset)
  11718. > loader_ctx->start_dynamic_offset)
  11719. && (*(loader_ctx->frame_offset)
  11720. < loader_ctx->max_dynamic_offset))
  11721. loader_ctx->dynamic_offset -= 2;
  11722. #endif
  11723. }
  11724. #if WASM_ENABLE_SIMD != 0
  11725. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11726. || (WASM_ENABLE_FAST_INTERP != 0)
  11727. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  11728. loader_ctx->frame_ref -= 4;
  11729. loader_ctx->stack_cell_num -= 4;
  11730. #if WASM_ENABLE_FAST_INTERP != 0
  11731. skip_label();
  11732. loader_ctx->frame_offset -= 4;
  11733. if ((*(loader_ctx->frame_offset)
  11734. > loader_ctx->start_dynamic_offset)
  11735. && (*(loader_ctx->frame_offset)
  11736. < loader_ctx->max_dynamic_offset))
  11737. loader_ctx->dynamic_offset -= 4;
  11738. #endif
  11739. }
  11740. #endif
  11741. #endif
  11742. else {
  11743. set_error_buf(error_buf, error_buf_size,
  11744. "type mismatch");
  11745. goto fail;
  11746. }
  11747. }
  11748. else {
  11749. #if WASM_ENABLE_FAST_INTERP != 0
  11750. skip_label();
  11751. #endif
  11752. }
  11753. break;
  11754. }
  11755. case WASM_OP_SELECT:
  11756. {
  11757. uint8 ref_type;
  11758. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11759. int32 available_stack_cell;
  11760. #if WASM_ENABLE_FAST_INTERP != 0
  11761. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11762. #endif
  11763. POP_I32();
  11764. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  11765. - cur_block->stack_cell_num);
  11766. if (available_stack_cell <= 0
  11767. && !cur_block->is_stack_polymorphic) {
  11768. set_error_buf(error_buf, error_buf_size,
  11769. "type mismatch or invalid result arity, "
  11770. "opcode select was found "
  11771. "but stack was empty");
  11772. goto fail;
  11773. }
  11774. if (available_stack_cell > 0) {
  11775. switch (*(loader_ctx->frame_ref - 1)) {
  11776. case VALUE_TYPE_I32:
  11777. case VALUE_TYPE_F32:
  11778. case VALUE_TYPE_ANY:
  11779. break;
  11780. case VALUE_TYPE_I64:
  11781. case VALUE_TYPE_F64:
  11782. #if WASM_ENABLE_FAST_INTERP == 0
  11783. *(p - 1) = WASM_OP_SELECT_64;
  11784. #else
  11785. if (loader_ctx->p_code_compiled) {
  11786. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11787. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11788. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11789. *(void **)(p_code_compiled_tmp
  11790. - sizeof(void *)) =
  11791. handle_table[opcode_tmp];
  11792. #elif UINTPTR_MAX == UINT64_MAX
  11793. /* emit int32 relative offset in 64-bit target
  11794. */
  11795. int32 offset =
  11796. (int32)((uint8 *)handle_table[opcode_tmp]
  11797. - (uint8 *)handle_table[0]);
  11798. *(int32 *)(p_code_compiled_tmp
  11799. - sizeof(int32)) = offset;
  11800. #else
  11801. /* emit uint32 label address in 32-bit target */
  11802. *(uint32 *)(p_code_compiled_tmp
  11803. - sizeof(uint32)) =
  11804. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11805. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11806. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11807. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11808. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11809. #else
  11810. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11811. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11812. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11813. }
  11814. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11815. break;
  11816. #if WASM_ENABLE_SIMD != 0
  11817. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11818. || (WASM_ENABLE_FAST_INTERP != 0)
  11819. case VALUE_TYPE_V128:
  11820. #if WASM_ENABLE_FAST_INTERP == 0
  11821. *(p - 1) = WASM_OP_SELECT_128;
  11822. #else
  11823. if (loader_ctx->p_code_compiled) {
  11824. uint8 opcode_tmp = WASM_OP_SELECT_128;
  11825. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11826. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11827. *(void **)(p_code_compiled_tmp
  11828. - sizeof(void *)) =
  11829. handle_table[opcode_tmp];
  11830. #elif UINTPTR_MAX == UINT64_MAX
  11831. /* emit int32 relative offset in 64-bit target
  11832. */
  11833. int32 offset =
  11834. (int32)((uint8 *)handle_table[opcode_tmp]
  11835. - (uint8 *)handle_table[0]);
  11836. *(int32 *)(p_code_compiled_tmp
  11837. - sizeof(int32)) = offset;
  11838. #else
  11839. /* emit uint32 label address in 32-bit target */
  11840. *(uint32 *)(p_code_compiled_tmp
  11841. - sizeof(uint32)) =
  11842. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11843. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11844. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11845. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11846. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11847. #else
  11848. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11849. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11850. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11851. }
  11852. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11853. break;
  11854. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  11855. (WASM_ENABLE_FAST_INTERP != 0) */
  11856. #endif /* WASM_ENABLE_SIMD != 0 */
  11857. default:
  11858. {
  11859. set_error_buf(error_buf, error_buf_size,
  11860. "type mismatch");
  11861. goto fail;
  11862. }
  11863. }
  11864. ref_type = *(loader_ctx->frame_ref - 1);
  11865. #if WASM_ENABLE_FAST_INTERP != 0
  11866. POP_OFFSET_TYPE(ref_type);
  11867. POP_TYPE(ref_type);
  11868. POP_OFFSET_TYPE(ref_type);
  11869. POP_TYPE(ref_type);
  11870. PUSH_OFFSET_TYPE(ref_type);
  11871. PUSH_TYPE(ref_type);
  11872. #else
  11873. POP2_AND_PUSH(ref_type, ref_type);
  11874. #endif
  11875. }
  11876. else {
  11877. #if WASM_ENABLE_FAST_INTERP != 0
  11878. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11879. #endif
  11880. PUSH_TYPE(VALUE_TYPE_ANY);
  11881. }
  11882. break;
  11883. }
  11884. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11885. case WASM_OP_SELECT_T:
  11886. {
  11887. uint8 vec_len, type;
  11888. #if WASM_ENABLE_GC != 0
  11889. WASMRefType *ref_type = NULL;
  11890. #endif
  11891. #if WASM_ENABLE_FAST_INTERP != 0
  11892. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11893. #endif
  11894. pb_read_leb_uint32(p, p_end, vec_len);
  11895. if (vec_len != 1) {
  11896. /* typed select must have exactly one result */
  11897. set_error_buf(error_buf, error_buf_size,
  11898. "invalid result arity");
  11899. goto fail;
  11900. }
  11901. #if WASM_ENABLE_GC == 0
  11902. CHECK_BUF(p, p_end, 1);
  11903. type = read_uint8(p);
  11904. if (!is_valid_value_type_for_interpreter(type)) {
  11905. set_error_buf(error_buf, error_buf_size,
  11906. "unknown value type");
  11907. goto fail;
  11908. }
  11909. #else
  11910. p_org = p + 1;
  11911. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11912. module->type_count, &need_ref_type_map,
  11913. &wasm_ref_type, false, error_buf,
  11914. error_buf_size)) {
  11915. goto fail;
  11916. }
  11917. type = wasm_ref_type.ref_type;
  11918. if (need_ref_type_map) {
  11919. if (!(ref_type = reftype_set_insert(
  11920. module->ref_type_set, &wasm_ref_type, error_buf,
  11921. error_buf_size))) {
  11922. goto fail;
  11923. }
  11924. }
  11925. #if WASM_ENABLE_FAST_INTERP == 0
  11926. while (p_org < p) {
  11927. #if WASM_ENABLE_DEBUG_INTERP != 0
  11928. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11929. error_buf_size)) {
  11930. goto fail;
  11931. }
  11932. #endif
  11933. /* Ignore extra bytes for interpreter */
  11934. *p_org++ = WASM_OP_NOP;
  11935. }
  11936. #endif
  11937. #endif /* end of WASM_ENABLE_GC == 0 */
  11938. POP_I32();
  11939. #if WASM_ENABLE_FAST_INTERP != 0
  11940. if (loader_ctx->p_code_compiled) {
  11941. uint8 opcode_tmp = WASM_OP_SELECT;
  11942. if (type == VALUE_TYPE_V128) {
  11943. #if WASM_ENABLE_JIT != 0 \
  11944. || WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_SIMD != 0
  11945. opcode_tmp = WASM_OP_SELECT_128;
  11946. #else
  11947. set_error_buf(error_buf, error_buf_size,
  11948. "v128 value type requires simd feature");
  11949. #endif
  11950. }
  11951. else {
  11952. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11953. opcode_tmp = WASM_OP_SELECT_64;
  11954. #if WASM_ENABLE_GC != 0
  11955. if (wasm_is_type_reftype(type))
  11956. opcode_tmp = WASM_OP_SELECT_T;
  11957. #endif
  11958. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11959. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11960. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11961. handle_table[opcode_tmp];
  11962. #else
  11963. #if UINTPTR_MAX == UINT64_MAX
  11964. /* emit int32 relative offset in 64-bit target */
  11965. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11966. - (uint8 *)handle_table[0]);
  11967. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11968. offset;
  11969. #else
  11970. /* emit uint32 label address in 32-bit target */
  11971. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11972. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11973. #endif
  11974. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11975. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11976. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11977. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11978. #else
  11979. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11980. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11981. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11982. }
  11983. }
  11984. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11985. POP_REF(type);
  11986. #if WASM_ENABLE_GC != 0
  11987. if (need_ref_type_map) {
  11988. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11989. wasm_reftype_struct_size(ref_type));
  11990. }
  11991. #endif
  11992. POP_REF(type);
  11993. #if WASM_ENABLE_GC != 0
  11994. if (need_ref_type_map) {
  11995. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11996. wasm_reftype_struct_size(ref_type));
  11997. }
  11998. #endif
  11999. PUSH_REF(type);
  12000. #if WASM_ENABLE_WAMR_COMPILER != 0
  12001. module->is_ref_types_used = true;
  12002. #endif
  12003. (void)vec_len;
  12004. break;
  12005. }
  12006. /* table.get x. tables[x]. [it] -> [t] */
  12007. /* table.set x. tables[x]. [it t] -> [] */
  12008. case WASM_OP_TABLE_GET:
  12009. case WASM_OP_TABLE_SET:
  12010. {
  12011. uint8 decl_ref_type;
  12012. #if WASM_ENABLE_GC != 0
  12013. WASMRefType *ref_type;
  12014. #endif
  12015. pb_read_leb_uint32(p, p_end, table_idx);
  12016. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  12017. #if WASM_ENABLE_GC != 0
  12018. (void **)&ref_type,
  12019. #else
  12020. NULL,
  12021. #endif
  12022. error_buf, error_buf_size))
  12023. goto fail;
  12024. #if WASM_ENABLE_GC != 0
  12025. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  12026. bh_assert(ref_type);
  12027. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  12028. ref_type, wasm_reftype_struct_size(ref_type));
  12029. }
  12030. #endif
  12031. #if WASM_ENABLE_FAST_INTERP != 0
  12032. emit_uint32(loader_ctx, table_idx);
  12033. #endif
  12034. #if WASM_ENABLE_MEMORY64 != 0
  12035. table_elem_idx_type = is_table_64bit(module, table_idx)
  12036. ? VALUE_TYPE_I64
  12037. : VALUE_TYPE_I32;
  12038. #endif
  12039. if (opcode == WASM_OP_TABLE_GET) {
  12040. POP_TBL_ELEM_IDX();
  12041. #if WASM_ENABLE_FAST_INTERP != 0
  12042. PUSH_OFFSET_TYPE(decl_ref_type);
  12043. #endif
  12044. PUSH_TYPE(decl_ref_type);
  12045. }
  12046. else {
  12047. #if WASM_ENABLE_FAST_INTERP != 0
  12048. POP_OFFSET_TYPE(decl_ref_type);
  12049. #endif
  12050. POP_TYPE(decl_ref_type);
  12051. POP_TBL_ELEM_IDX();
  12052. }
  12053. #if WASM_ENABLE_WAMR_COMPILER != 0
  12054. module->is_ref_types_used = true;
  12055. #endif
  12056. break;
  12057. }
  12058. case WASM_OP_REF_NULL:
  12059. {
  12060. uint8 ref_type;
  12061. #if WASM_ENABLE_GC == 0
  12062. CHECK_BUF(p, p_end, 1);
  12063. ref_type = read_uint8(p);
  12064. if (ref_type != VALUE_TYPE_FUNCREF
  12065. && ref_type != VALUE_TYPE_EXTERNREF) {
  12066. set_error_buf(error_buf, error_buf_size, "type mismatch");
  12067. goto fail;
  12068. }
  12069. #else
  12070. pb_read_leb_int32(p, p_end, heap_type);
  12071. if (heap_type >= 0) {
  12072. if (!check_type_index(module, module->type_count, heap_type,
  12073. error_buf, error_buf_size)) {
  12074. goto fail;
  12075. }
  12076. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  12077. true, heap_type);
  12078. ref_type = wasm_ref_type.ref_type;
  12079. }
  12080. else {
  12081. if (!wasm_is_valid_heap_type(heap_type)) {
  12082. set_error_buf(error_buf, error_buf_size,
  12083. "unknown type");
  12084. goto fail;
  12085. }
  12086. ref_type = (uint8)((int32)0x80 + heap_type);
  12087. }
  12088. #endif /* end of WASM_ENABLE_GC == 0 */
  12089. #if WASM_ENABLE_FAST_INTERP != 0
  12090. PUSH_OFFSET_TYPE(ref_type);
  12091. #endif
  12092. PUSH_TYPE(ref_type);
  12093. #if WASM_ENABLE_WAMR_COMPILER != 0
  12094. module->is_ref_types_used = true;
  12095. #endif
  12096. break;
  12097. }
  12098. case WASM_OP_REF_IS_NULL:
  12099. {
  12100. #if WASM_ENABLE_GC == 0
  12101. #if WASM_ENABLE_FAST_INTERP != 0
  12102. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  12103. int32 block_stack_cell_num =
  12104. (int32)(loader_ctx->stack_cell_num
  12105. - cur_block->stack_cell_num);
  12106. if (block_stack_cell_num <= 0) {
  12107. if (!cur_block->is_stack_polymorphic) {
  12108. set_error_buf(
  12109. error_buf, error_buf_size,
  12110. "type mismatch: expect data but stack was empty");
  12111. goto fail;
  12112. }
  12113. }
  12114. else {
  12115. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  12116. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  12117. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  12118. if (!wasm_loader_pop_frame_ref_offset(
  12119. loader_ctx, *(loader_ctx->frame_ref - 1),
  12120. error_buf, error_buf_size)) {
  12121. goto fail;
  12122. }
  12123. }
  12124. else {
  12125. set_error_buf(error_buf, error_buf_size,
  12126. "type mismatch");
  12127. goto fail;
  12128. }
  12129. }
  12130. #else
  12131. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  12132. error_buf, error_buf_size)
  12133. && !wasm_loader_pop_frame_ref(loader_ctx,
  12134. VALUE_TYPE_EXTERNREF,
  12135. error_buf, error_buf_size)) {
  12136. goto fail;
  12137. }
  12138. #endif
  12139. #else /* else of WASM_ENABLE_GC == 0 */
  12140. uint8 type;
  12141. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  12142. error_buf, error_buf_size)) {
  12143. goto fail;
  12144. }
  12145. #endif
  12146. PUSH_I32();
  12147. #if WASM_ENABLE_WAMR_COMPILER != 0
  12148. module->is_ref_types_used = true;
  12149. #endif
  12150. break;
  12151. }
  12152. case WASM_OP_REF_FUNC:
  12153. {
  12154. pb_read_leb_uint32(p, p_end, func_idx);
  12155. if (!check_function_index(module, func_idx, error_buf,
  12156. error_buf_size)) {
  12157. goto fail;
  12158. }
  12159. /* Refer to a forward-declared function:
  12160. the function must be an import, exported, or present in
  12161. a table elem segment or global initializer to be used as
  12162. the operand to ref.func */
  12163. if (func_idx >= module->import_function_count) {
  12164. WASMTableSeg *table_seg = module->table_segments;
  12165. bool func_declared = false;
  12166. uint32 j;
  12167. for (i = 0; i < module->global_count; i++) {
  12168. if (module->globals[i].type.val_type
  12169. == VALUE_TYPE_FUNCREF
  12170. && module->globals[i].init_expr.init_expr_type
  12171. == INIT_EXPR_TYPE_FUNCREF_CONST
  12172. && module->globals[i].init_expr.u.unary.v.u32
  12173. == func_idx) {
  12174. func_declared = true;
  12175. break;
  12176. }
  12177. }
  12178. if (!func_declared) {
  12179. /* Check whether the function is declared in table segs,
  12180. note that it doesn't matter whether the table seg's
  12181. mode is passive, active or declarative. */
  12182. for (i = 0; i < module->table_seg_count;
  12183. i++, table_seg++) {
  12184. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  12185. #if WASM_ENABLE_GC != 0
  12186. /* elem type is (ref null? func) or
  12187. (ref null? $t) */
  12188. || ((table_seg->elem_type
  12189. == REF_TYPE_HT_NON_NULLABLE
  12190. || table_seg->elem_type
  12191. == REF_TYPE_HT_NULLABLE)
  12192. && (table_seg->elem_ref_type->ref_ht_common
  12193. .heap_type
  12194. == HEAP_TYPE_FUNC
  12195. || table_seg->elem_ref_type
  12196. ->ref_ht_common.heap_type
  12197. > 0))
  12198. #endif
  12199. ) {
  12200. for (j = 0; j < table_seg->value_count; j++) {
  12201. if (table_seg->init_values[j]
  12202. .u.unary.v.ref_index
  12203. == func_idx) {
  12204. func_declared = true;
  12205. break;
  12206. }
  12207. }
  12208. }
  12209. }
  12210. }
  12211. if (!func_declared) {
  12212. /* Check whether the function is exported */
  12213. for (i = 0; i < module->export_count; i++) {
  12214. if (module->exports[i].kind == EXPORT_KIND_FUNC
  12215. && module->exports[i].index == func_idx) {
  12216. func_declared = true;
  12217. break;
  12218. }
  12219. }
  12220. }
  12221. if (!func_declared) {
  12222. set_error_buf(error_buf, error_buf_size,
  12223. "undeclared function reference");
  12224. goto fail;
  12225. }
  12226. }
  12227. #if WASM_ENABLE_FAST_INTERP != 0
  12228. emit_uint32(loader_ctx, func_idx);
  12229. #endif
  12230. #if WASM_ENABLE_GC == 0
  12231. PUSH_FUNCREF();
  12232. #else
  12233. if (func_idx < module->import_function_count)
  12234. type_idx =
  12235. module->import_functions[func_idx].u.function.type_idx;
  12236. else
  12237. type_idx = module
  12238. ->functions[func_idx
  12239. - module->import_function_count]
  12240. ->type_idx;
  12241. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  12242. false, type_idx);
  12243. PUSH_REF(wasm_ref_type.ref_type);
  12244. #endif
  12245. #if WASM_ENABLE_WAMR_COMPILER != 0
  12246. module->is_ref_types_used = true;
  12247. #endif
  12248. break;
  12249. }
  12250. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  12251. #if WASM_ENABLE_GC != 0
  12252. case WASM_OP_REF_AS_NON_NULL:
  12253. case WASM_OP_BR_ON_NULL:
  12254. {
  12255. uint8 type;
  12256. WASMRefType ref_type;
  12257. /* POP (ref null ht) and get the converted (ref ht) */
  12258. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  12259. error_buf, error_buf_size)) {
  12260. goto fail;
  12261. }
  12262. if (opcode == WASM_OP_BR_ON_NULL) {
  12263. if (!(frame_csp_tmp =
  12264. check_branch_block(loader_ctx, &p, p_end, opcode,
  12265. error_buf, error_buf_size))) {
  12266. goto fail;
  12267. }
  12268. #if WASM_ENABLE_FAST_INTERP != 0
  12269. disable_emit = true;
  12270. #endif
  12271. }
  12272. /* PUSH the converted (ref ht) */
  12273. if (type != VALUE_TYPE_ANY) {
  12274. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  12275. sizeof(WASMRefType));
  12276. }
  12277. PUSH_REF(type);
  12278. break;
  12279. }
  12280. case WASM_OP_BR_ON_NON_NULL:
  12281. {
  12282. uint8 type;
  12283. WASMRefType ref_type;
  12284. uint32 available_stack_cell =
  12285. loader_ctx->stack_cell_num
  12286. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  12287. /* POP (ref null ht) and get the converted (ref ht) */
  12288. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  12289. error_buf, error_buf_size)) {
  12290. goto fail;
  12291. }
  12292. #if WASM_ENABLE_FAST_INTERP != 0
  12293. disable_emit = true;
  12294. #endif
  12295. /* Temporarily PUSH back (ref ht), check brach block and
  12296. then POP it */
  12297. if (available_stack_cell
  12298. > 0) { /* stack isn't in polymorphic state */
  12299. if (type != VALUE_TYPE_ANY) {
  12300. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12301. &ref_type, sizeof(WASMRefType));
  12302. }
  12303. PUSH_REF(type);
  12304. }
  12305. if (!(frame_csp_tmp =
  12306. check_branch_block(loader_ctx, &p, p_end, opcode,
  12307. error_buf, error_buf_size))) {
  12308. goto fail;
  12309. }
  12310. if (available_stack_cell
  12311. > 0) { /* stack isn't in polymorphic state */
  12312. POP_REF(type);
  12313. #if WASM_ENABLE_FAST_INTERP != 0
  12314. /* Erase the opnd offset emitted by POP_REF() */
  12315. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12316. #endif
  12317. }
  12318. break;
  12319. }
  12320. case WASM_OP_REF_EQ:
  12321. POP_REF(REF_TYPE_EQREF);
  12322. POP_REF(REF_TYPE_EQREF);
  12323. PUSH_I32();
  12324. break;
  12325. #endif /* end of WASM_ENABLE_GC != 0 */
  12326. case WASM_OP_GET_LOCAL:
  12327. {
  12328. p_org = p - 1;
  12329. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12330. PUSH_TYPE(local_type);
  12331. #if WASM_ENABLE_GC != 0
  12332. /* Cannot get a non-nullable and unset local */
  12333. if (local_idx >= param_count
  12334. && wasm_is_reftype_htref_non_nullable(local_type)
  12335. && !wasm_loader_get_local_status(loader_ctx,
  12336. local_idx - param_count)) {
  12337. set_error_buf(error_buf, error_buf_size,
  12338. "uninitialized local");
  12339. goto fail;
  12340. }
  12341. #endif
  12342. #if WASM_ENABLE_FAST_INTERP != 0
  12343. /* Get Local is optimized out */
  12344. skip_label();
  12345. disable_emit = true;
  12346. operand_offset = local_offset;
  12347. PUSH_OFFSET_TYPE(local_type);
  12348. #else
  12349. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12350. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12351. if (local_offset < 0x80
  12352. #if WASM_ENABLE_GC != 0
  12353. && !wasm_is_type_reftype(local_type)
  12354. #endif
  12355. ) {
  12356. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  12357. if (is_32bit_type(local_type)) {
  12358. *p_org++ = (uint8)local_offset;
  12359. }
  12360. else {
  12361. *p_org++ = (uint8)(local_offset | 0x80);
  12362. }
  12363. while (p_org < p) {
  12364. *p_org++ = WASM_OP_NOP;
  12365. }
  12366. }
  12367. #endif
  12368. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12369. break;
  12370. }
  12371. case WASM_OP_SET_LOCAL:
  12372. {
  12373. p_org = p - 1;
  12374. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12375. #if WASM_ENABLE_FAST_INTERP != 0
  12376. if (!(preserve_referenced_local(
  12377. loader_ctx, opcode, local_offset, local_type,
  12378. &preserve_local, error_buf, error_buf_size)))
  12379. goto fail;
  12380. if (local_offset < 256
  12381. #if WASM_ENABLE_GC != 0
  12382. && !wasm_is_type_reftype(local_type)
  12383. #endif
  12384. ) {
  12385. skip_label();
  12386. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  12387. if (loader_ctx->p_code_compiled)
  12388. STORE_U16(loader_ctx->p_code_compiled - 2,
  12389. local_offset);
  12390. loader_ctx->frame_offset--;
  12391. loader_ctx->dynamic_offset--;
  12392. }
  12393. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  12394. if (loader_ctx->p_code_compiled)
  12395. STORE_U16(loader_ctx->p_code_compiled - 2,
  12396. local_offset);
  12397. loader_ctx->frame_offset -= 2;
  12398. loader_ctx->dynamic_offset -= 2;
  12399. }
  12400. else {
  12401. if (is_32bit_type(local_type)) {
  12402. emit_label(EXT_OP_SET_LOCAL_FAST);
  12403. emit_byte(loader_ctx, (uint8)local_offset);
  12404. }
  12405. else if (is_64bit_type(local_type)) {
  12406. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  12407. emit_byte(loader_ctx, (uint8)local_offset);
  12408. }
  12409. #if WASM_ENABLE_SIMDE != 0
  12410. else if (local_type == VALUE_TYPE_V128) {
  12411. emit_label(EXT_OP_SET_LOCAL_FAST_V128);
  12412. emit_byte(loader_ctx, (uint8)local_offset);
  12413. }
  12414. #endif
  12415. else {
  12416. set_error_buf(error_buf, error_buf_size,
  12417. "unknown local type");
  12418. goto fail;
  12419. }
  12420. POP_OFFSET_TYPE(local_type);
  12421. }
  12422. }
  12423. else { /* local index larger than 255, reserve leb */
  12424. emit_uint32(loader_ctx, local_idx);
  12425. POP_OFFSET_TYPE(local_type);
  12426. }
  12427. #else
  12428. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12429. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12430. if (local_offset < 0x80
  12431. #if WASM_ENABLE_GC != 0
  12432. && !wasm_is_type_reftype(local_type)
  12433. #endif
  12434. ) {
  12435. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  12436. if (is_32bit_type(local_type)) {
  12437. *p_org++ = (uint8)local_offset;
  12438. }
  12439. else {
  12440. *p_org++ = (uint8)(local_offset | 0x80);
  12441. }
  12442. while (p_org < p) {
  12443. *p_org++ = WASM_OP_NOP;
  12444. }
  12445. }
  12446. #endif
  12447. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12448. #if WASM_ENABLE_GC != 0
  12449. if (local_idx >= param_count) {
  12450. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12451. }
  12452. #endif
  12453. POP_TYPE(local_type);
  12454. break;
  12455. }
  12456. case WASM_OP_TEE_LOCAL:
  12457. {
  12458. p_org = p - 1;
  12459. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12460. #if WASM_ENABLE_FAST_INTERP != 0
  12461. /* If the stack is in polymorphic state, do fake pop and push on
  12462. offset stack to keep the depth of offset stack to be the
  12463. same with ref stack */
  12464. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  12465. if (cur_block->is_stack_polymorphic) {
  12466. POP_OFFSET_TYPE(local_type);
  12467. PUSH_OFFSET_TYPE(local_type);
  12468. }
  12469. #endif
  12470. POP_TYPE(local_type);
  12471. PUSH_TYPE(local_type);
  12472. #if WASM_ENABLE_FAST_INTERP != 0
  12473. if (!(preserve_referenced_local(
  12474. loader_ctx, opcode, local_offset, local_type,
  12475. &preserve_local, error_buf, error_buf_size)))
  12476. goto fail;
  12477. if (local_offset < 256
  12478. #if WASM_ENABLE_GC != 0
  12479. && !wasm_is_type_reftype(local_type)
  12480. #endif
  12481. ) {
  12482. skip_label();
  12483. if (is_32bit_type(local_type)) {
  12484. emit_label(EXT_OP_TEE_LOCAL_FAST);
  12485. emit_byte(loader_ctx, (uint8)local_offset);
  12486. }
  12487. #if WASM_ENABLE_SIMDE != 0
  12488. else if (local_type == VALUE_TYPE_V128) {
  12489. emit_label(EXT_OP_TEE_LOCAL_FAST_V128);
  12490. emit_byte(loader_ctx, (uint8)local_offset);
  12491. }
  12492. #endif
  12493. else {
  12494. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  12495. emit_byte(loader_ctx, (uint8)local_offset);
  12496. }
  12497. }
  12498. else { /* local index larger than 255, reserve leb */
  12499. emit_uint32(loader_ctx, local_idx);
  12500. }
  12501. emit_operand(loader_ctx,
  12502. *(loader_ctx->frame_offset
  12503. - wasm_value_type_cell_num(local_type)));
  12504. #else
  12505. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12506. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12507. if (local_offset < 0x80
  12508. #if WASM_ENABLE_GC != 0
  12509. && !wasm_is_type_reftype(local_type)
  12510. #endif
  12511. ) {
  12512. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  12513. if (is_32bit_type(local_type)) {
  12514. *p_org++ = (uint8)local_offset;
  12515. }
  12516. else {
  12517. *p_org++ = (uint8)(local_offset | 0x80);
  12518. }
  12519. while (p_org < p) {
  12520. *p_org++ = WASM_OP_NOP;
  12521. }
  12522. }
  12523. #endif
  12524. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12525. #if WASM_ENABLE_GC != 0
  12526. if (local_idx >= param_count) {
  12527. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12528. }
  12529. #endif
  12530. break;
  12531. }
  12532. case WASM_OP_GET_GLOBAL:
  12533. {
  12534. #if WASM_ENABLE_GC != 0
  12535. WASMRefType *ref_type;
  12536. #endif
  12537. p_org = p - 1;
  12538. pb_read_leb_uint32(p, p_end, global_idx);
  12539. if (global_idx >= global_count) {
  12540. set_error_buf(error_buf, error_buf_size, "unknown global");
  12541. goto fail;
  12542. }
  12543. global_type = global_idx < module->import_global_count
  12544. ? module->import_globals[global_idx]
  12545. .u.global.type.val_type
  12546. : module
  12547. ->globals[global_idx
  12548. - module->import_global_count]
  12549. .type.val_type;
  12550. #if WASM_ENABLE_GC != 0
  12551. ref_type =
  12552. global_idx < module->import_global_count
  12553. ? module->import_globals[global_idx].u.global.ref_type
  12554. : module
  12555. ->globals[global_idx
  12556. - module->import_global_count]
  12557. .ref_type;
  12558. if (wasm_is_type_multi_byte_type(global_type)) {
  12559. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12560. wasm_reftype_struct_size(ref_type));
  12561. }
  12562. #endif
  12563. PUSH_TYPE(global_type);
  12564. #if WASM_ENABLE_FAST_INTERP == 0
  12565. if (global_type == VALUE_TYPE_I64
  12566. || global_type == VALUE_TYPE_F64) {
  12567. #if WASM_ENABLE_DEBUG_INTERP != 0
  12568. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12569. error_buf_size)) {
  12570. goto fail;
  12571. }
  12572. #endif
  12573. *p_org = WASM_OP_GET_GLOBAL_64;
  12574. }
  12575. #else /* else of WASM_ENABLE_FAST_INTERP */
  12576. if (global_type == VALUE_TYPE_I64
  12577. || global_type == VALUE_TYPE_F64) {
  12578. skip_label();
  12579. emit_label(WASM_OP_GET_GLOBAL_64);
  12580. }
  12581. #if WASM_ENABLE_SIMDE != 0
  12582. if (global_type == VALUE_TYPE_V128) {
  12583. skip_label();
  12584. emit_label(WASM_OP_GET_GLOBAL_V128);
  12585. }
  12586. #endif /* end of WASM_ENABLE_SIMDE */
  12587. emit_uint32(loader_ctx, global_idx);
  12588. PUSH_OFFSET_TYPE(global_type);
  12589. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12590. break;
  12591. }
  12592. case WASM_OP_SET_GLOBAL:
  12593. {
  12594. bool is_mutable = false;
  12595. #if WASM_ENABLE_GC != 0
  12596. WASMRefType *ref_type;
  12597. #endif
  12598. p_org = p - 1;
  12599. pb_read_leb_uint32(p, p_end, global_idx);
  12600. if (global_idx >= global_count) {
  12601. set_error_buf(error_buf, error_buf_size, "unknown global");
  12602. goto fail;
  12603. }
  12604. is_mutable = global_idx < module->import_global_count
  12605. ? module->import_globals[global_idx]
  12606. .u.global.type.is_mutable
  12607. : module
  12608. ->globals[global_idx
  12609. - module->import_global_count]
  12610. .type.is_mutable;
  12611. if (!is_mutable) {
  12612. #if WASM_ENABLE_GC == 0
  12613. set_error_buf(error_buf, error_buf_size,
  12614. "global is immutable");
  12615. #else
  12616. set_error_buf(error_buf, error_buf_size,
  12617. "immutable global");
  12618. #endif
  12619. goto fail;
  12620. }
  12621. global_type = global_idx < module->import_global_count
  12622. ? module->import_globals[global_idx]
  12623. .u.global.type.val_type
  12624. : module
  12625. ->globals[global_idx
  12626. - module->import_global_count]
  12627. .type.val_type;
  12628. #if WASM_ENABLE_GC != 0
  12629. ref_type =
  12630. global_idx < module->import_global_count
  12631. ? module->import_globals[global_idx].u.global.ref_type
  12632. : module
  12633. ->globals[global_idx
  12634. - module->import_global_count]
  12635. .ref_type;
  12636. if (wasm_is_type_multi_byte_type(global_type)) {
  12637. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12638. wasm_reftype_struct_size(ref_type));
  12639. }
  12640. #endif
  12641. #if WASM_ENABLE_FAST_INTERP == 0
  12642. if (global_type == VALUE_TYPE_I64
  12643. || global_type == VALUE_TYPE_F64) {
  12644. #if WASM_ENABLE_DEBUG_INTERP != 0
  12645. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12646. error_buf_size)) {
  12647. goto fail;
  12648. }
  12649. #endif
  12650. *p_org = WASM_OP_SET_GLOBAL_64;
  12651. }
  12652. else if (module->aux_stack_size > 0
  12653. && global_idx == module->aux_stack_top_global_index) {
  12654. #if WASM_ENABLE_DEBUG_INTERP != 0
  12655. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12656. error_buf_size)) {
  12657. goto fail;
  12658. }
  12659. #endif
  12660. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  12661. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12662. func->has_op_set_global_aux_stack = true;
  12663. #endif
  12664. }
  12665. #else /* else of WASM_ENABLE_FAST_INTERP */
  12666. if (global_type == VALUE_TYPE_I64
  12667. || global_type == VALUE_TYPE_F64) {
  12668. skip_label();
  12669. emit_label(WASM_OP_SET_GLOBAL_64);
  12670. }
  12671. else if (module->aux_stack_size > 0
  12672. && global_idx == module->aux_stack_top_global_index) {
  12673. skip_label();
  12674. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  12675. }
  12676. #if WASM_ENABLE_SIMDE != 0
  12677. else if (global_type == VALUE_TYPE_V128) {
  12678. skip_label();
  12679. emit_label(WASM_OP_SET_GLOBAL_V128);
  12680. }
  12681. #endif /* end of WASM_ENABLE_SIMDE */
  12682. emit_uint32(loader_ctx, global_idx);
  12683. POP_OFFSET_TYPE(global_type);
  12684. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12685. POP_TYPE(global_type);
  12686. break;
  12687. }
  12688. /* load */
  12689. case WASM_OP_I32_LOAD:
  12690. case WASM_OP_I32_LOAD8_S:
  12691. case WASM_OP_I32_LOAD8_U:
  12692. case WASM_OP_I32_LOAD16_S:
  12693. case WASM_OP_I32_LOAD16_U:
  12694. case WASM_OP_I64_LOAD:
  12695. case WASM_OP_I64_LOAD8_S:
  12696. case WASM_OP_I64_LOAD8_U:
  12697. case WASM_OP_I64_LOAD16_S:
  12698. case WASM_OP_I64_LOAD16_U:
  12699. case WASM_OP_I64_LOAD32_S:
  12700. case WASM_OP_I64_LOAD32_U:
  12701. case WASM_OP_F32_LOAD:
  12702. case WASM_OP_F64_LOAD:
  12703. /* store */
  12704. case WASM_OP_I32_STORE:
  12705. case WASM_OP_I32_STORE8:
  12706. case WASM_OP_I32_STORE16:
  12707. case WASM_OP_I64_STORE:
  12708. case WASM_OP_I64_STORE8:
  12709. case WASM_OP_I64_STORE16:
  12710. case WASM_OP_I64_STORE32:
  12711. case WASM_OP_F32_STORE:
  12712. case WASM_OP_F64_STORE:
  12713. {
  12714. #if WASM_ENABLE_FAST_INTERP != 0
  12715. /* change F32/F64 into I32/I64 */
  12716. if (opcode == WASM_OP_F32_LOAD) {
  12717. skip_label();
  12718. emit_label(WASM_OP_I32_LOAD);
  12719. }
  12720. else if (opcode == WASM_OP_F64_LOAD) {
  12721. skip_label();
  12722. emit_label(WASM_OP_I64_LOAD);
  12723. }
  12724. else if (opcode == WASM_OP_F32_STORE) {
  12725. skip_label();
  12726. emit_label(WASM_OP_I32_STORE);
  12727. }
  12728. else if (opcode == WASM_OP_F64_STORE) {
  12729. skip_label();
  12730. emit_label(WASM_OP_I64_STORE);
  12731. }
  12732. #endif
  12733. CHECK_MEMORY();
  12734. pb_read_leb_memarg(p, p_end, align); /* align */
  12735. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  12736. if (!check_memory_access_align(opcode, align, error_buf,
  12737. error_buf_size)) {
  12738. goto fail;
  12739. }
  12740. #if WASM_ENABLE_FAST_INTERP != 0
  12741. emit_uint32(loader_ctx, mem_offset);
  12742. #endif
  12743. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12744. func->has_memory_operations = true;
  12745. #endif
  12746. switch (opcode) {
  12747. /* load */
  12748. case WASM_OP_I32_LOAD:
  12749. case WASM_OP_I32_LOAD8_S:
  12750. case WASM_OP_I32_LOAD8_U:
  12751. case WASM_OP_I32_LOAD16_S:
  12752. case WASM_OP_I32_LOAD16_U:
  12753. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  12754. break;
  12755. case WASM_OP_I64_LOAD:
  12756. case WASM_OP_I64_LOAD8_S:
  12757. case WASM_OP_I64_LOAD8_U:
  12758. case WASM_OP_I64_LOAD16_S:
  12759. case WASM_OP_I64_LOAD16_U:
  12760. case WASM_OP_I64_LOAD32_S:
  12761. case WASM_OP_I64_LOAD32_U:
  12762. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  12763. break;
  12764. case WASM_OP_F32_LOAD:
  12765. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  12766. break;
  12767. case WASM_OP_F64_LOAD:
  12768. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  12769. break;
  12770. /* store */
  12771. case WASM_OP_I32_STORE:
  12772. case WASM_OP_I32_STORE8:
  12773. case WASM_OP_I32_STORE16:
  12774. POP_I32();
  12775. POP_MEM_OFFSET();
  12776. break;
  12777. case WASM_OP_I64_STORE:
  12778. case WASM_OP_I64_STORE8:
  12779. case WASM_OP_I64_STORE16:
  12780. case WASM_OP_I64_STORE32:
  12781. POP_I64();
  12782. POP_MEM_OFFSET();
  12783. break;
  12784. case WASM_OP_F32_STORE:
  12785. POP_F32();
  12786. POP_MEM_OFFSET();
  12787. break;
  12788. case WASM_OP_F64_STORE:
  12789. POP_F64();
  12790. POP_MEM_OFFSET();
  12791. break;
  12792. default:
  12793. break;
  12794. }
  12795. break;
  12796. }
  12797. case WASM_OP_MEMORY_SIZE:
  12798. CHECK_MEMORY();
  12799. pb_read_leb_uint32(p, p_end, memidx);
  12800. check_memidx(module, memidx);
  12801. PUSH_PAGE_COUNT();
  12802. module->possible_memory_grow = true;
  12803. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12804. func->has_memory_operations = true;
  12805. #endif
  12806. break;
  12807. case WASM_OP_MEMORY_GROW:
  12808. CHECK_MEMORY();
  12809. pb_read_leb_uint32(p, p_end, memidx);
  12810. check_memidx(module, memidx);
  12811. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  12812. module->possible_memory_grow = true;
  12813. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  12814. || WASM_ENABLE_WAMR_COMPILER != 0
  12815. func->has_op_memory_grow = true;
  12816. #endif
  12817. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12818. func->has_memory_operations = true;
  12819. #endif
  12820. break;
  12821. case WASM_OP_I32_CONST:
  12822. pb_read_leb_int32(p, p_end, i32_const);
  12823. #if WASM_ENABLE_FAST_INTERP != 0
  12824. skip_label();
  12825. disable_emit = true;
  12826. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  12827. if (operand_offset == 0) {
  12828. disable_emit = false;
  12829. emit_label(WASM_OP_I32_CONST);
  12830. emit_uint32(loader_ctx, i32_const);
  12831. }
  12832. #else
  12833. (void)i32_const;
  12834. #endif
  12835. PUSH_I32();
  12836. break;
  12837. case WASM_OP_I64_CONST:
  12838. pb_read_leb_int64(p, p_end, i64_const);
  12839. #if WASM_ENABLE_FAST_INTERP != 0
  12840. skip_label();
  12841. disable_emit = true;
  12842. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  12843. if (operand_offset == 0) {
  12844. disable_emit = false;
  12845. emit_label(WASM_OP_I64_CONST);
  12846. emit_uint64(loader_ctx, i64_const);
  12847. }
  12848. #endif
  12849. PUSH_I64();
  12850. break;
  12851. case WASM_OP_F32_CONST:
  12852. CHECK_BUF(p, p_end, sizeof(float32));
  12853. p += sizeof(float32);
  12854. #if WASM_ENABLE_FAST_INTERP != 0
  12855. skip_label();
  12856. disable_emit = true;
  12857. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12858. sizeof(float32));
  12859. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12860. if (operand_offset == 0) {
  12861. disable_emit = false;
  12862. emit_label(WASM_OP_F32_CONST);
  12863. emit_float32(loader_ctx, f32_const);
  12864. }
  12865. #endif
  12866. PUSH_F32();
  12867. break;
  12868. case WASM_OP_F64_CONST:
  12869. CHECK_BUF(p, p_end, sizeof(float64));
  12870. p += sizeof(float64);
  12871. #if WASM_ENABLE_FAST_INTERP != 0
  12872. skip_label();
  12873. disable_emit = true;
  12874. /* Some MCU may require 8-byte align */
  12875. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12876. sizeof(float64));
  12877. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12878. if (operand_offset == 0) {
  12879. disable_emit = false;
  12880. emit_label(WASM_OP_F64_CONST);
  12881. emit_float64(loader_ctx, f64_const);
  12882. }
  12883. #endif
  12884. PUSH_F64();
  12885. break;
  12886. case WASM_OP_I32_EQZ:
  12887. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12888. break;
  12889. case WASM_OP_I32_EQ:
  12890. case WASM_OP_I32_NE:
  12891. case WASM_OP_I32_LT_S:
  12892. case WASM_OP_I32_LT_U:
  12893. case WASM_OP_I32_GT_S:
  12894. case WASM_OP_I32_GT_U:
  12895. case WASM_OP_I32_LE_S:
  12896. case WASM_OP_I32_LE_U:
  12897. case WASM_OP_I32_GE_S:
  12898. case WASM_OP_I32_GE_U:
  12899. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12900. break;
  12901. case WASM_OP_I64_EQZ:
  12902. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12903. break;
  12904. case WASM_OP_I64_EQ:
  12905. case WASM_OP_I64_NE:
  12906. case WASM_OP_I64_LT_S:
  12907. case WASM_OP_I64_LT_U:
  12908. case WASM_OP_I64_GT_S:
  12909. case WASM_OP_I64_GT_U:
  12910. case WASM_OP_I64_LE_S:
  12911. case WASM_OP_I64_LE_U:
  12912. case WASM_OP_I64_GE_S:
  12913. case WASM_OP_I64_GE_U:
  12914. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12915. break;
  12916. case WASM_OP_F32_EQ:
  12917. case WASM_OP_F32_NE:
  12918. case WASM_OP_F32_LT:
  12919. case WASM_OP_F32_GT:
  12920. case WASM_OP_F32_LE:
  12921. case WASM_OP_F32_GE:
  12922. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12923. break;
  12924. case WASM_OP_F64_EQ:
  12925. case WASM_OP_F64_NE:
  12926. case WASM_OP_F64_LT:
  12927. case WASM_OP_F64_GT:
  12928. case WASM_OP_F64_LE:
  12929. case WASM_OP_F64_GE:
  12930. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12931. break;
  12932. case WASM_OP_I32_CLZ:
  12933. case WASM_OP_I32_CTZ:
  12934. case WASM_OP_I32_POPCNT:
  12935. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12936. break;
  12937. case WASM_OP_I32_ADD:
  12938. case WASM_OP_I32_SUB:
  12939. case WASM_OP_I32_MUL:
  12940. case WASM_OP_I32_DIV_S:
  12941. case WASM_OP_I32_DIV_U:
  12942. case WASM_OP_I32_REM_S:
  12943. case WASM_OP_I32_REM_U:
  12944. case WASM_OP_I32_AND:
  12945. case WASM_OP_I32_OR:
  12946. case WASM_OP_I32_XOR:
  12947. case WASM_OP_I32_SHL:
  12948. case WASM_OP_I32_SHR_S:
  12949. case WASM_OP_I32_SHR_U:
  12950. case WASM_OP_I32_ROTL:
  12951. case WASM_OP_I32_ROTR:
  12952. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12953. break;
  12954. case WASM_OP_I64_CLZ:
  12955. case WASM_OP_I64_CTZ:
  12956. case WASM_OP_I64_POPCNT:
  12957. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12958. break;
  12959. case WASM_OP_I64_ADD:
  12960. case WASM_OP_I64_SUB:
  12961. case WASM_OP_I64_MUL:
  12962. case WASM_OP_I64_DIV_S:
  12963. case WASM_OP_I64_DIV_U:
  12964. case WASM_OP_I64_REM_S:
  12965. case WASM_OP_I64_REM_U:
  12966. case WASM_OP_I64_AND:
  12967. case WASM_OP_I64_OR:
  12968. case WASM_OP_I64_XOR:
  12969. case WASM_OP_I64_SHL:
  12970. case WASM_OP_I64_SHR_S:
  12971. case WASM_OP_I64_SHR_U:
  12972. case WASM_OP_I64_ROTL:
  12973. case WASM_OP_I64_ROTR:
  12974. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12975. break;
  12976. case WASM_OP_F32_ABS:
  12977. case WASM_OP_F32_NEG:
  12978. case WASM_OP_F32_CEIL:
  12979. case WASM_OP_F32_FLOOR:
  12980. case WASM_OP_F32_TRUNC:
  12981. case WASM_OP_F32_NEAREST:
  12982. case WASM_OP_F32_SQRT:
  12983. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12984. break;
  12985. case WASM_OP_F32_ADD:
  12986. case WASM_OP_F32_SUB:
  12987. case WASM_OP_F32_MUL:
  12988. case WASM_OP_F32_DIV:
  12989. case WASM_OP_F32_MIN:
  12990. case WASM_OP_F32_MAX:
  12991. case WASM_OP_F32_COPYSIGN:
  12992. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12993. break;
  12994. case WASM_OP_F64_ABS:
  12995. case WASM_OP_F64_NEG:
  12996. case WASM_OP_F64_CEIL:
  12997. case WASM_OP_F64_FLOOR:
  12998. case WASM_OP_F64_TRUNC:
  12999. case WASM_OP_F64_NEAREST:
  13000. case WASM_OP_F64_SQRT:
  13001. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  13002. break;
  13003. case WASM_OP_F64_ADD:
  13004. case WASM_OP_F64_SUB:
  13005. case WASM_OP_F64_MUL:
  13006. case WASM_OP_F64_DIV:
  13007. case WASM_OP_F64_MIN:
  13008. case WASM_OP_F64_MAX:
  13009. case WASM_OP_F64_COPYSIGN:
  13010. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  13011. break;
  13012. case WASM_OP_I32_WRAP_I64:
  13013. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  13014. break;
  13015. case WASM_OP_I32_TRUNC_S_F32:
  13016. case WASM_OP_I32_TRUNC_U_F32:
  13017. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13018. break;
  13019. case WASM_OP_I32_TRUNC_S_F64:
  13020. case WASM_OP_I32_TRUNC_U_F64:
  13021. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13022. break;
  13023. case WASM_OP_I64_EXTEND_S_I32:
  13024. case WASM_OP_I64_EXTEND_U_I32:
  13025. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  13026. break;
  13027. case WASM_OP_I64_TRUNC_S_F32:
  13028. case WASM_OP_I64_TRUNC_U_F32:
  13029. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13030. break;
  13031. case WASM_OP_I64_TRUNC_S_F64:
  13032. case WASM_OP_I64_TRUNC_U_F64:
  13033. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13034. break;
  13035. case WASM_OP_F32_CONVERT_S_I32:
  13036. case WASM_OP_F32_CONVERT_U_I32:
  13037. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  13038. break;
  13039. case WASM_OP_F32_CONVERT_S_I64:
  13040. case WASM_OP_F32_CONVERT_U_I64:
  13041. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  13042. break;
  13043. case WASM_OP_F32_DEMOTE_F64:
  13044. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  13045. break;
  13046. case WASM_OP_F64_CONVERT_S_I32:
  13047. case WASM_OP_F64_CONVERT_U_I32:
  13048. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  13049. break;
  13050. case WASM_OP_F64_CONVERT_S_I64:
  13051. case WASM_OP_F64_CONVERT_U_I64:
  13052. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  13053. break;
  13054. case WASM_OP_F64_PROMOTE_F32:
  13055. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  13056. break;
  13057. case WASM_OP_I32_REINTERPRET_F32:
  13058. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13059. break;
  13060. case WASM_OP_I64_REINTERPRET_F64:
  13061. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13062. break;
  13063. case WASM_OP_F32_REINTERPRET_I32:
  13064. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  13065. break;
  13066. case WASM_OP_F64_REINTERPRET_I64:
  13067. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  13068. break;
  13069. case WASM_OP_I32_EXTEND8_S:
  13070. case WASM_OP_I32_EXTEND16_S:
  13071. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13072. break;
  13073. case WASM_OP_I64_EXTEND8_S:
  13074. case WASM_OP_I64_EXTEND16_S:
  13075. case WASM_OP_I64_EXTEND32_S:
  13076. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  13077. break;
  13078. #if WASM_ENABLE_GC != 0
  13079. case WASM_OP_GC_PREFIX:
  13080. {
  13081. uint32 opcode1;
  13082. pb_read_leb_uint32(p, p_end, opcode1);
  13083. #if WASM_ENABLE_FAST_INTERP != 0
  13084. emit_byte(loader_ctx, ((uint8)opcode1));
  13085. #endif
  13086. switch (opcode1) {
  13087. case WASM_OP_STRUCT_NEW:
  13088. case WASM_OP_STRUCT_NEW_DEFAULT:
  13089. {
  13090. pb_read_leb_uint32(p, p_end, type_idx);
  13091. #if WASM_ENABLE_FAST_INTERP != 0
  13092. emit_uint32(loader_ctx, type_idx);
  13093. #endif
  13094. if (!check_type_index(module, module->type_count,
  13095. type_idx, error_buf,
  13096. error_buf_size)) {
  13097. goto fail;
  13098. }
  13099. if (module->types[type_idx]->type_flag
  13100. != WASM_TYPE_STRUCT) {
  13101. set_error_buf(error_buf, error_buf_size,
  13102. "unknown struct type");
  13103. goto fail;
  13104. }
  13105. if (opcode1 == WASM_OP_STRUCT_NEW) {
  13106. int32 j, k;
  13107. uint8 value_type;
  13108. uint32 ref_type_struct_size;
  13109. WASMStructType *struct_type =
  13110. (WASMStructType *)module->types[type_idx];
  13111. k = struct_type->ref_type_map_count - 1;
  13112. for (j = struct_type->field_count - 1; j >= 0;
  13113. j--) {
  13114. value_type = struct_type->fields[j].field_type;
  13115. if (wasm_is_type_reftype(value_type)) {
  13116. if (wasm_is_type_multi_byte_type(
  13117. value_type)) {
  13118. ref_type_struct_size =
  13119. wasm_reftype_struct_size(
  13120. struct_type->ref_type_maps[k]
  13121. .ref_type);
  13122. bh_memcpy_s(
  13123. &wasm_ref_type,
  13124. (uint32)sizeof(WASMRefType),
  13125. struct_type->ref_type_maps[k]
  13126. .ref_type,
  13127. ref_type_struct_size);
  13128. k--;
  13129. }
  13130. POP_REF(value_type);
  13131. }
  13132. else {
  13133. switch (value_type) {
  13134. case VALUE_TYPE_I32:
  13135. case PACKED_TYPE_I8:
  13136. case PACKED_TYPE_I16:
  13137. POP_I32();
  13138. break;
  13139. case VALUE_TYPE_I64:
  13140. POP_I64();
  13141. break;
  13142. case VALUE_TYPE_F32:
  13143. POP_F32();
  13144. break;
  13145. case VALUE_TYPE_F64:
  13146. POP_F64();
  13147. break;
  13148. default:
  13149. set_error_buf(error_buf,
  13150. error_buf_size,
  13151. "unknown type");
  13152. goto fail;
  13153. }
  13154. }
  13155. }
  13156. }
  13157. /* PUSH struct obj, (ref $t) */
  13158. wasm_set_refheaptype_typeidx(
  13159. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  13160. PUSH_REF(wasm_ref_type.ref_type);
  13161. break;
  13162. }
  13163. case WASM_OP_STRUCT_GET:
  13164. case WASM_OP_STRUCT_GET_S:
  13165. case WASM_OP_STRUCT_GET_U:
  13166. case WASM_OP_STRUCT_SET:
  13167. {
  13168. WASMStructType *struct_type;
  13169. WASMRefType *ref_type = NULL;
  13170. uint32 field_idx;
  13171. uint8 field_type;
  13172. pb_read_leb_uint32(p, p_end, type_idx);
  13173. #if WASM_ENABLE_FAST_INTERP != 0
  13174. emit_uint32(loader_ctx, type_idx);
  13175. #endif
  13176. if (!check_type_index(module, module->type_count,
  13177. type_idx, error_buf,
  13178. error_buf_size)) {
  13179. goto fail;
  13180. }
  13181. if (module->types[type_idx]->type_flag
  13182. != WASM_TYPE_STRUCT) {
  13183. set_error_buf(error_buf, error_buf_size,
  13184. "unknown struct type");
  13185. goto fail;
  13186. }
  13187. struct_type = (WASMStructType *)module->types[type_idx];
  13188. pb_read_leb_uint32(p, p_end, field_idx);
  13189. #if WASM_ENABLE_FAST_INTERP != 0
  13190. emit_uint32(loader_ctx, field_idx);
  13191. #endif
  13192. if (field_idx >= struct_type->field_count) {
  13193. set_error_buf(error_buf, error_buf_size,
  13194. "unknown struct field");
  13195. goto fail;
  13196. }
  13197. if (opcode1 == WASM_OP_STRUCT_SET
  13198. && !(struct_type->fields[field_idx].field_flags
  13199. & 1)) {
  13200. set_error_buf(error_buf, error_buf_size,
  13201. "field is immutable");
  13202. goto fail;
  13203. }
  13204. field_type = struct_type->fields[field_idx].field_type;
  13205. if (is_packed_type(field_type)) {
  13206. if (opcode1 == WASM_OP_STRUCT_GET) {
  13207. set_error_buf(error_buf, error_buf_size,
  13208. "type mismatch");
  13209. goto fail;
  13210. }
  13211. else {
  13212. field_type = VALUE_TYPE_I32;
  13213. }
  13214. }
  13215. if (wasm_is_type_multi_byte_type(field_type)) {
  13216. ref_type = wasm_reftype_map_find(
  13217. struct_type->ref_type_maps,
  13218. struct_type->ref_type_map_count, field_idx);
  13219. bh_assert(ref_type);
  13220. }
  13221. if (opcode1 == WASM_OP_STRUCT_SET) {
  13222. /* POP field */
  13223. if (wasm_is_type_multi_byte_type(field_type)) {
  13224. bh_memcpy_s(&wasm_ref_type,
  13225. (uint32)sizeof(WASMRefType),
  13226. ref_type,
  13227. wasm_reftype_struct_size(ref_type));
  13228. }
  13229. POP_REF(field_type);
  13230. /* POP struct obj, (ref null $t) */
  13231. wasm_set_refheaptype_typeidx(
  13232. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13233. POP_REF(wasm_ref_type.ref_type);
  13234. }
  13235. else {
  13236. /* POP struct obj, (ref null $t) */
  13237. wasm_set_refheaptype_typeidx(
  13238. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13239. POP_REF(wasm_ref_type.ref_type);
  13240. /* PUSH field */
  13241. if (wasm_is_type_multi_byte_type(field_type)) {
  13242. bh_memcpy_s(&wasm_ref_type,
  13243. (uint32)sizeof(WASMRefType),
  13244. ref_type,
  13245. wasm_reftype_struct_size(ref_type));
  13246. }
  13247. PUSH_REF(field_type);
  13248. }
  13249. break;
  13250. }
  13251. case WASM_OP_ARRAY_NEW:
  13252. case WASM_OP_ARRAY_NEW_DEFAULT:
  13253. case WASM_OP_ARRAY_NEW_FIXED:
  13254. case WASM_OP_ARRAY_NEW_DATA:
  13255. case WASM_OP_ARRAY_NEW_ELEM:
  13256. {
  13257. WASMArrayType *array_type;
  13258. uint8 elem_type;
  13259. uint32 u32 = 0;
  13260. pb_read_leb_uint32(p, p_end, type_idx);
  13261. #if WASM_ENABLE_FAST_INTERP != 0
  13262. emit_uint32(loader_ctx, type_idx);
  13263. #endif
  13264. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  13265. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  13266. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  13267. pb_read_leb_uint32(p, p_end, u32);
  13268. #if WASM_ENABLE_FAST_INTERP != 0
  13269. emit_uint32(loader_ctx, u32);
  13270. #endif
  13271. }
  13272. if (!check_array_type(module, type_idx, error_buf,
  13273. error_buf_size)) {
  13274. goto fail;
  13275. }
  13276. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  13277. /* length */
  13278. POP_I32();
  13279. }
  13280. array_type = (WASMArrayType *)module->types[type_idx];
  13281. elem_type = array_type->elem_type;
  13282. if (opcode1 == WASM_OP_ARRAY_NEW
  13283. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  13284. if (wasm_is_type_multi_byte_type(elem_type)) {
  13285. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13286. array_type->elem_ref_type,
  13287. wasm_reftype_struct_size(
  13288. array_type->elem_ref_type));
  13289. }
  13290. if (is_packed_type(elem_type)) {
  13291. elem_type = VALUE_TYPE_I32;
  13292. }
  13293. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  13294. uint32 N = u32;
  13295. for (i = 0; i < N; i++) {
  13296. if (wasm_is_type_multi_byte_type(
  13297. elem_type)) {
  13298. bh_memcpy_s(
  13299. &wasm_ref_type, sizeof(WASMRefType),
  13300. array_type->elem_ref_type,
  13301. wasm_reftype_struct_size(
  13302. array_type->elem_ref_type));
  13303. }
  13304. POP_REF(elem_type);
  13305. }
  13306. }
  13307. else
  13308. POP_REF(elem_type);
  13309. }
  13310. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  13311. /* offset of data segment */
  13312. POP_I32();
  13313. if (u32 >= module->data_seg_count) {
  13314. set_error_buf(error_buf, error_buf_size,
  13315. "unknown data segment");
  13316. goto fail;
  13317. }
  13318. if (wasm_is_type_reftype(elem_type)) {
  13319. set_error_buf(error_buf, error_buf_size,
  13320. "array elem type mismatch");
  13321. goto fail;
  13322. }
  13323. }
  13324. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  13325. WASMTableSeg *table_seg =
  13326. module->table_segments + u32;
  13327. /* offset of element segment */
  13328. POP_I32();
  13329. if (u32 >= module->table_seg_count) {
  13330. set_error_buf(error_buf, error_buf_size,
  13331. "unknown element segment");
  13332. goto fail;
  13333. }
  13334. if (!wasm_reftype_is_subtype_of(
  13335. table_seg->elem_type,
  13336. table_seg->elem_ref_type, elem_type,
  13337. array_type->elem_ref_type, module->types,
  13338. module->type_count)) {
  13339. set_error_buf(error_buf, error_buf_size,
  13340. "array elem type mismatch");
  13341. goto fail;
  13342. }
  13343. }
  13344. /* PUSH array obj, (ref $t) */
  13345. wasm_set_refheaptype_typeidx(
  13346. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  13347. PUSH_REF(wasm_ref_type.ref_type);
  13348. break;
  13349. }
  13350. case WASM_OP_ARRAY_GET:
  13351. case WASM_OP_ARRAY_GET_S:
  13352. case WASM_OP_ARRAY_GET_U:
  13353. case WASM_OP_ARRAY_SET:
  13354. {
  13355. uint8 elem_type;
  13356. WASMArrayType *array_type;
  13357. WASMRefType *ref_type = NULL;
  13358. pb_read_leb_uint32(p, p_end, type_idx);
  13359. #if WASM_ENABLE_FAST_INTERP != 0
  13360. emit_uint32(loader_ctx, type_idx);
  13361. #endif
  13362. if (!check_array_type(module, type_idx, error_buf,
  13363. error_buf_size)) {
  13364. goto fail;
  13365. }
  13366. array_type = (WASMArrayType *)module->types[type_idx];
  13367. if (opcode1 == WASM_OP_ARRAY_SET
  13368. && !(array_type->elem_flags & 1)) {
  13369. set_error_buf(error_buf, error_buf_size,
  13370. "array is immutable");
  13371. goto fail;
  13372. }
  13373. elem_type = array_type->elem_type;
  13374. if (is_packed_type(elem_type)) {
  13375. if (opcode1 != WASM_OP_ARRAY_GET_S
  13376. && opcode1 != WASM_OP_ARRAY_GET_U
  13377. && opcode1 != WASM_OP_ARRAY_SET) {
  13378. set_error_buf(error_buf, error_buf_size,
  13379. "type mismatch");
  13380. goto fail;
  13381. }
  13382. else {
  13383. elem_type = VALUE_TYPE_I32;
  13384. }
  13385. }
  13386. ref_type = array_type->elem_ref_type;
  13387. if (opcode1 == WASM_OP_ARRAY_SET) {
  13388. /* POP elem to set */
  13389. if (wasm_is_type_multi_byte_type(elem_type)) {
  13390. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13391. ref_type,
  13392. wasm_reftype_struct_size(ref_type));
  13393. }
  13394. POP_REF(elem_type);
  13395. }
  13396. /* elem idx */
  13397. POP_I32();
  13398. /* POP array obj, (ref null $t) */
  13399. wasm_set_refheaptype_typeidx(
  13400. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13401. POP_REF(wasm_ref_type.ref_type);
  13402. if (opcode1 != WASM_OP_ARRAY_SET) {
  13403. /* PUSH elem */
  13404. if (wasm_is_type_multi_byte_type(elem_type)) {
  13405. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13406. ref_type,
  13407. wasm_reftype_struct_size(ref_type));
  13408. }
  13409. PUSH_REF(elem_type);
  13410. }
  13411. break;
  13412. }
  13413. case WASM_OP_ARRAY_LEN:
  13414. {
  13415. POP_REF(REF_TYPE_ARRAYREF);
  13416. /* length */
  13417. PUSH_I32();
  13418. break;
  13419. }
  13420. case WASM_OP_ARRAY_FILL:
  13421. {
  13422. WASMArrayType *array_type;
  13423. uint8 elem_type;
  13424. /* typeidx */
  13425. pb_read_leb_uint32(p, p_end, type_idx);
  13426. #if WASM_ENABLE_FAST_INTERP != 0
  13427. emit_uint32(loader_ctx, type_idx);
  13428. #endif
  13429. if (!check_array_type(module, type_idx, error_buf,
  13430. error_buf_size)) {
  13431. goto fail;
  13432. }
  13433. array_type = (WASMArrayType *)module->types[type_idx];
  13434. if (!(array_type->elem_flags & 1)) {
  13435. set_error_buf(error_buf, error_buf_size,
  13436. "array is immutable");
  13437. goto fail;
  13438. }
  13439. elem_type = array_type->elem_type;
  13440. if (is_packed_type(elem_type)) {
  13441. elem_type = VALUE_TYPE_I32;
  13442. }
  13443. POP_I32(); /* length */
  13444. #if WASM_ENABLE_FAST_INTERP != 0
  13445. POP_OFFSET_TYPE(elem_type);
  13446. #endif
  13447. POP_TYPE(elem_type);
  13448. POP_I32(); /* start */
  13449. /* POP array obj, (ref null $t) */
  13450. wasm_set_refheaptype_typeidx(
  13451. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13452. POP_REF(wasm_ref_type.ref_type);
  13453. break;
  13454. }
  13455. case WASM_OP_ARRAY_COPY:
  13456. {
  13457. uint32 src_type_idx;
  13458. uint8 src_elem_type, dst_elem_type;
  13459. WASMRefType src_ref_type = { 0 },
  13460. *src_elem_ref_type = NULL;
  13461. WASMRefType dst_ref_type = { 0 },
  13462. *dst_elem_ref_type = NULL;
  13463. WASMArrayType *array_type;
  13464. /* typeidx1 */
  13465. pb_read_leb_uint32(p, p_end, type_idx);
  13466. #if WASM_ENABLE_FAST_INTERP != 0
  13467. emit_uint32(loader_ctx, type_idx);
  13468. #endif
  13469. /* typeidx2 */
  13470. pb_read_leb_uint32(p, p_end, src_type_idx);
  13471. #if WASM_ENABLE_FAST_INTERP != 0
  13472. emit_uint32(loader_ctx, src_type_idx);
  13473. #endif
  13474. if (!check_array_type(module, type_idx, error_buf,
  13475. error_buf_size)) {
  13476. goto fail;
  13477. }
  13478. if (!check_array_type(module, src_type_idx, error_buf,
  13479. error_buf_size)) {
  13480. goto fail;
  13481. }
  13482. POP_I32();
  13483. POP_I32();
  13484. /* POP array obj, (ref null $t) */
  13485. wasm_set_refheaptype_typeidx(
  13486. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  13487. POP_REF(wasm_ref_type.ref_type);
  13488. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  13489. &wasm_ref_type,
  13490. wasm_reftype_struct_size(&wasm_ref_type));
  13491. POP_I32();
  13492. /* POP array obj, (ref null $t) */
  13493. wasm_set_refheaptype_typeidx(
  13494. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13495. POP_REF(wasm_ref_type.ref_type);
  13496. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  13497. &wasm_ref_type,
  13498. wasm_reftype_struct_size(&wasm_ref_type));
  13499. array_type = (WASMArrayType *)module->types[type_idx];
  13500. if (!(array_type->elem_flags & 1)) {
  13501. set_error_buf(error_buf, error_buf_size,
  13502. "destination array is immutable");
  13503. goto fail;
  13504. }
  13505. dst_elem_type = array_type->elem_type;
  13506. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  13507. dst_elem_ref_type = array_type->elem_ref_type;
  13508. }
  13509. array_type =
  13510. (WASMArrayType *)module->types[src_type_idx];
  13511. src_elem_type = array_type->elem_type;
  13512. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  13513. src_elem_ref_type = array_type->elem_ref_type;
  13514. }
  13515. if (!wasm_reftype_is_subtype_of(
  13516. src_elem_type, src_elem_ref_type, dst_elem_type,
  13517. dst_elem_ref_type, module->types,
  13518. module->type_count)) {
  13519. set_error_buf(error_buf, error_buf_size,
  13520. "array types do not match");
  13521. goto fail;
  13522. }
  13523. break;
  13524. }
  13525. case WASM_OP_REF_I31:
  13526. {
  13527. POP_I32();
  13528. wasm_set_refheaptype_common(
  13529. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  13530. PUSH_REF(wasm_ref_type.ref_type);
  13531. break;
  13532. }
  13533. case WASM_OP_I31_GET_S:
  13534. case WASM_OP_I31_GET_U:
  13535. {
  13536. POP_REF(REF_TYPE_I31REF);
  13537. PUSH_I32();
  13538. break;
  13539. }
  13540. case WASM_OP_REF_TEST:
  13541. case WASM_OP_REF_CAST:
  13542. case WASM_OP_REF_TEST_NULLABLE:
  13543. case WASM_OP_REF_CAST_NULLABLE:
  13544. {
  13545. uint8 type;
  13546. pb_read_leb_int32(p, p_end, heap_type);
  13547. #if WASM_ENABLE_FAST_INTERP != 0
  13548. emit_uint32(loader_ctx, (uint32)heap_type);
  13549. #endif
  13550. if (heap_type >= 0) {
  13551. if (!check_type_index(module, module->type_count,
  13552. heap_type, error_buf,
  13553. error_buf_size)) {
  13554. goto fail;
  13555. }
  13556. }
  13557. else {
  13558. if (!wasm_is_valid_heap_type(heap_type)) {
  13559. set_error_buf(error_buf, error_buf_size,
  13560. "unknown type");
  13561. goto fail;
  13562. }
  13563. }
  13564. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13565. &wasm_ref_type, error_buf,
  13566. error_buf_size)) {
  13567. goto fail;
  13568. }
  13569. if (opcode1 == WASM_OP_REF_TEST
  13570. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  13571. PUSH_I32();
  13572. else {
  13573. bool nullable =
  13574. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  13575. : false;
  13576. if (heap_type >= 0 || !nullable) {
  13577. wasm_set_refheaptype_typeidx(
  13578. &wasm_ref_type.ref_ht_typeidx, nullable,
  13579. heap_type);
  13580. PUSH_REF(wasm_ref_type.ref_type);
  13581. }
  13582. else {
  13583. PUSH_REF((uint8)((int32)0x80 + heap_type));
  13584. }
  13585. }
  13586. break;
  13587. }
  13588. case WASM_OP_BR_ON_CAST:
  13589. case WASM_OP_BR_ON_CAST_FAIL:
  13590. {
  13591. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  13592. ref_type2 = { 0 }, ref_type_diff = { 0 };
  13593. uint8 type_tmp, castflags;
  13594. uint32 depth;
  13595. int32 heap_type_dst;
  13596. bool src_nullable, dst_nullable;
  13597. CHECK_BUF(p, p_end, 1);
  13598. castflags = read_uint8(p);
  13599. #if WASM_ENABLE_FAST_INTERP != 0
  13600. /* Emit heap_type firstly */
  13601. emit_byte(loader_ctx, castflags);
  13602. #endif
  13603. p_org = p;
  13604. pb_read_leb_uint32(p, p_end, depth);
  13605. pb_read_leb_int32(p, p_end, heap_type);
  13606. #if WASM_ENABLE_FAST_INTERP != 0
  13607. /* Emit heap_type firstly */
  13608. emit_uint32(loader_ctx, (uint32)heap_type);
  13609. #endif
  13610. pb_read_leb_int32(p, p_end, heap_type_dst);
  13611. #if WASM_ENABLE_FAST_INTERP != 0
  13612. /* Emit heap_type firstly */
  13613. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  13614. #endif
  13615. (void)depth;
  13616. /*
  13617. * castflags should be 0~3:
  13618. * 0: (non-null, non-null)
  13619. * 1: (null, non-null)
  13620. * 2: (non-null, null)
  13621. * 3: (null, null)
  13622. */
  13623. if (castflags > 3) {
  13624. set_error_buf(error_buf, error_buf_size,
  13625. "invalid castflags");
  13626. break;
  13627. }
  13628. src_nullable =
  13629. (castflags == 1) || (castflags == 3) ? true : false;
  13630. dst_nullable =
  13631. (castflags == 2) || (castflags == 3) ? true : false;
  13632. /* Pop and backup the stack top's ref type */
  13633. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  13634. &ref_type_tmp, error_buf,
  13635. error_buf_size)) {
  13636. goto fail;
  13637. }
  13638. /* The reference type rt1 must be valid */
  13639. if (!init_ref_type(module, &ref_type1, src_nullable,
  13640. heap_type, error_buf,
  13641. error_buf_size)) {
  13642. goto fail;
  13643. }
  13644. /* The reference type rt2 must be valid. */
  13645. if (!init_ref_type(module, &ref_type2, dst_nullable,
  13646. heap_type_dst, error_buf,
  13647. error_buf_size)) {
  13648. goto fail;
  13649. }
  13650. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  13651. &ref_type2);
  13652. /* The reference type rt2 must match rt1. */
  13653. if (!wasm_reftype_is_subtype_of(
  13654. ref_type2.ref_type, &ref_type2,
  13655. ref_type1.ref_type, &ref_type1, module->types,
  13656. module->type_count)) {
  13657. set_error_buf(error_buf, error_buf_size,
  13658. "type mismatch");
  13659. goto fail;
  13660. }
  13661. p = p_org;
  13662. /* Push ref type casted for branch block check */
  13663. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13664. /* The reference type rt2 must match rt′. */
  13665. type_tmp = ref_type2.ref_type;
  13666. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13667. bh_memcpy_s(
  13668. &wasm_ref_type,
  13669. wasm_reftype_struct_size(&ref_type2),
  13670. &ref_type2,
  13671. wasm_reftype_struct_size(&ref_type2));
  13672. }
  13673. }
  13674. else {
  13675. /* The reference type rt′1 must match rt′. */
  13676. type_tmp = ref_type_diff.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_type_diff),
  13681. &ref_type_diff,
  13682. wasm_reftype_struct_size(&ref_type_diff));
  13683. }
  13684. }
  13685. PUSH_REF(type_tmp);
  13686. if (!(frame_csp_tmp = check_branch_block(
  13687. loader_ctx, &p, p_end, opcode, error_buf,
  13688. error_buf_size))) {
  13689. goto fail;
  13690. }
  13691. /* Ignore heap_types */
  13692. skip_leb_uint32(p, p_end);
  13693. skip_leb_uint32(p, p_end);
  13694. /* Restore the original stack top's ref type */
  13695. POP_REF(type_tmp);
  13696. #if WASM_ENABLE_FAST_INTERP != 0
  13697. /* Erase the opnd offset emitted by POP_REF() */
  13698. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13699. #endif
  13700. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13701. type_tmp = ref_type_diff.ref_type;
  13702. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13703. bh_memcpy_s(
  13704. &wasm_ref_type,
  13705. wasm_reftype_struct_size(&ref_type_diff),
  13706. &ref_type_diff,
  13707. wasm_reftype_struct_size(&ref_type_diff));
  13708. }
  13709. }
  13710. else {
  13711. type_tmp = ref_type2.ref_type;
  13712. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13713. bh_memcpy_s(
  13714. &wasm_ref_type,
  13715. wasm_reftype_struct_size(&ref_type2),
  13716. &ref_type2,
  13717. wasm_reftype_struct_size(&ref_type2));
  13718. }
  13719. }
  13720. PUSH_REF(type_tmp);
  13721. #if WASM_ENABLE_FAST_INTERP != 0
  13722. /* Erase the opnd offset emitted by PUSH_REF() */
  13723. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13724. #endif
  13725. break;
  13726. }
  13727. case WASM_OP_ANY_CONVERT_EXTERN:
  13728. {
  13729. uint8 type;
  13730. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13731. &wasm_ref_type, error_buf,
  13732. error_buf_size)) {
  13733. goto fail;
  13734. }
  13735. if (!(type == REF_TYPE_EXTERNREF
  13736. || (type == REF_TYPE_HT_NON_NULLABLE
  13737. && wasm_ref_type.ref_ht_common.heap_type
  13738. == HEAP_TYPE_EXTERN)
  13739. || type == VALUE_TYPE_ANY)) {
  13740. set_error_buf(error_buf, error_buf_size,
  13741. "type mismatch");
  13742. goto fail;
  13743. }
  13744. if (type == REF_TYPE_EXTERNREF)
  13745. type = REF_TYPE_ANYREF;
  13746. else {
  13747. wasm_ref_type.ref_ht_common.heap_type =
  13748. HEAP_TYPE_ANY;
  13749. }
  13750. PUSH_REF(type);
  13751. break;
  13752. }
  13753. case WASM_OP_EXTERN_CONVERT_ANY:
  13754. {
  13755. uint8 type;
  13756. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13757. &wasm_ref_type, error_buf,
  13758. error_buf_size)) {
  13759. goto fail;
  13760. }
  13761. if (type == REF_TYPE_EXTERNREF
  13762. || ((type == REF_TYPE_HT_NULLABLE
  13763. || type == REF_TYPE_HT_NON_NULLABLE)
  13764. && wasm_ref_type.ref_ht_common.heap_type
  13765. == HEAP_TYPE_EXTERN)) {
  13766. set_error_buf(error_buf, error_buf_size,
  13767. "type mismatch");
  13768. goto fail;
  13769. }
  13770. if (type != REF_TYPE_HT_NON_NULLABLE) {
  13771. /* push (ref null extern) */
  13772. type = REF_TYPE_EXTERNREF;
  13773. }
  13774. else {
  13775. /* push (ref extern) */
  13776. type = REF_TYPE_HT_NON_NULLABLE;
  13777. wasm_set_refheaptype_common(
  13778. &wasm_ref_type.ref_ht_common, false,
  13779. HEAP_TYPE_EXTERN);
  13780. }
  13781. PUSH_REF(type);
  13782. break;
  13783. }
  13784. #if WASM_ENABLE_STRINGREF != 0
  13785. case WASM_OP_STRING_NEW_UTF8:
  13786. case WASM_OP_STRING_NEW_WTF16:
  13787. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  13788. case WASM_OP_STRING_NEW_WTF8:
  13789. {
  13790. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13791. func->has_memory_operations = true;
  13792. #endif
  13793. pb_read_leb_uint32(p, p_end, memidx);
  13794. #if WASM_ENABLE_FAST_INTERP != 0
  13795. emit_uint32(loader_ctx, (uint32)memidx);
  13796. #endif
  13797. POP_I32();
  13798. POP_I32();
  13799. PUSH_REF(REF_TYPE_STRINGREF);
  13800. break;
  13801. }
  13802. case WASM_OP_STRING_CONST:
  13803. {
  13804. uint32 contents;
  13805. pb_read_leb_uint32(p, p_end, contents);
  13806. #if WASM_ENABLE_FAST_INTERP != 0
  13807. emit_uint32(loader_ctx, (uint32)contents);
  13808. #endif
  13809. PUSH_REF(REF_TYPE_STRINGREF);
  13810. (void)contents;
  13811. break;
  13812. }
  13813. case WASM_OP_STRING_MEASURE_UTF8:
  13814. case WASM_OP_STRING_MEASURE_WTF8:
  13815. case WASM_OP_STRING_MEASURE_WTF16:
  13816. {
  13817. POP_STRINGREF();
  13818. PUSH_I32();
  13819. break;
  13820. }
  13821. case WASM_OP_STRING_ENCODE_UTF8:
  13822. case WASM_OP_STRING_ENCODE_WTF16:
  13823. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  13824. case WASM_OP_STRING_ENCODE_WTF8:
  13825. {
  13826. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13827. func->has_memory_operations = true;
  13828. #endif
  13829. pb_read_leb_uint32(p, p_end, memidx);
  13830. #if WASM_ENABLE_FAST_INTERP != 0
  13831. emit_uint32(loader_ctx, (uint32)memidx);
  13832. #endif
  13833. POP_I32();
  13834. POP_STRINGREF();
  13835. PUSH_I32();
  13836. break;
  13837. }
  13838. case WASM_OP_STRING_CONCAT:
  13839. {
  13840. POP_STRINGREF();
  13841. POP_STRINGREF();
  13842. PUSH_REF(REF_TYPE_STRINGREF);
  13843. break;
  13844. }
  13845. case WASM_OP_STRING_EQ:
  13846. {
  13847. POP_STRINGREF();
  13848. POP_STRINGREF();
  13849. PUSH_I32();
  13850. break;
  13851. }
  13852. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13853. {
  13854. POP_STRINGREF();
  13855. PUSH_I32();
  13856. break;
  13857. }
  13858. case WASM_OP_STRING_AS_WTF8:
  13859. {
  13860. POP_STRINGREF();
  13861. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13862. break;
  13863. }
  13864. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13865. {
  13866. POP_I32();
  13867. POP_I32();
  13868. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13869. PUSH_I32();
  13870. break;
  13871. }
  13872. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13873. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13874. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13875. {
  13876. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13877. func->has_memory_operations = true;
  13878. #endif
  13879. pb_read_leb_uint32(p, p_end, memidx);
  13880. #if WASM_ENABLE_FAST_INTERP != 0
  13881. emit_uint32(loader_ctx, (uint32)memidx);
  13882. #endif
  13883. POP_I32();
  13884. POP_I32();
  13885. POP_I32();
  13886. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13887. PUSH_I32();
  13888. PUSH_I32();
  13889. break;
  13890. }
  13891. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13892. {
  13893. POP_I32();
  13894. POP_I32();
  13895. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13896. PUSH_REF(REF_TYPE_STRINGREF);
  13897. break;
  13898. }
  13899. case WASM_OP_STRING_AS_WTF16:
  13900. {
  13901. POP_STRINGREF();
  13902. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13903. break;
  13904. }
  13905. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13906. {
  13907. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13908. PUSH_I32();
  13909. break;
  13910. }
  13911. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13912. {
  13913. POP_I32();
  13914. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13915. PUSH_I32();
  13916. break;
  13917. }
  13918. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13919. {
  13920. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13921. func->has_memory_operations = true;
  13922. #endif
  13923. pb_read_leb_uint32(p, p_end, memidx);
  13924. #if WASM_ENABLE_FAST_INTERP != 0
  13925. emit_uint32(loader_ctx, (uint32)memidx);
  13926. #endif
  13927. POP_I32();
  13928. POP_I32();
  13929. POP_I32();
  13930. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13931. PUSH_I32();
  13932. break;
  13933. }
  13934. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13935. {
  13936. POP_I32();
  13937. POP_I32();
  13938. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13939. PUSH_REF(REF_TYPE_STRINGREF);
  13940. break;
  13941. }
  13942. case WASM_OP_STRING_AS_ITER:
  13943. {
  13944. POP_STRINGREF();
  13945. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13946. break;
  13947. }
  13948. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13949. {
  13950. POP_REF(REF_TYPE_STRINGVIEWITER);
  13951. PUSH_I32();
  13952. break;
  13953. }
  13954. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13955. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13956. {
  13957. POP_I32();
  13958. POP_REF(REF_TYPE_STRINGVIEWITER);
  13959. PUSH_I32();
  13960. break;
  13961. }
  13962. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13963. {
  13964. POP_I32();
  13965. POP_REF(REF_TYPE_STRINGVIEWITER);
  13966. PUSH_REF(REF_TYPE_STRINGREF);
  13967. break;
  13968. }
  13969. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13970. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13971. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13972. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13973. {
  13974. POP_I32();
  13975. POP_I32();
  13976. POP_REF(REF_TYPE_ARRAYREF);
  13977. PUSH_REF(REF_TYPE_STRINGREF);
  13978. break;
  13979. }
  13980. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13981. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13982. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13983. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13984. {
  13985. POP_I32();
  13986. POP_REF(REF_TYPE_ARRAYREF);
  13987. POP_STRINGREF();
  13988. PUSH_I32();
  13989. break;
  13990. }
  13991. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13992. default:
  13993. set_error_buf_v(error_buf, error_buf_size,
  13994. "%s %02x %02x", "unsupported opcode",
  13995. 0xfb, opcode1);
  13996. goto fail;
  13997. }
  13998. break;
  13999. }
  14000. #endif /* end of WASM_ENABLE_GC != 0 */
  14001. case WASM_OP_MISC_PREFIX:
  14002. {
  14003. uint32 opcode1;
  14004. pb_read_leb_uint32(p, p_end, opcode1);
  14005. #if WASM_ENABLE_FAST_INTERP != 0
  14006. emit_byte(loader_ctx, ((uint8)opcode1));
  14007. #endif
  14008. switch (opcode1) {
  14009. case WASM_OP_I32_TRUNC_SAT_S_F32:
  14010. case WASM_OP_I32_TRUNC_SAT_U_F32:
  14011. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  14012. break;
  14013. case WASM_OP_I32_TRUNC_SAT_S_F64:
  14014. case WASM_OP_I32_TRUNC_SAT_U_F64:
  14015. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  14016. break;
  14017. case WASM_OP_I64_TRUNC_SAT_S_F32:
  14018. case WASM_OP_I64_TRUNC_SAT_U_F32:
  14019. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  14020. break;
  14021. case WASM_OP_I64_TRUNC_SAT_S_F64:
  14022. case WASM_OP_I64_TRUNC_SAT_U_F64:
  14023. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  14024. break;
  14025. #if WASM_ENABLE_BULK_MEMORY != 0
  14026. case WASM_OP_MEMORY_INIT:
  14027. {
  14028. pb_read_leb_uint32(p, p_end, data_seg_idx);
  14029. #if WASM_ENABLE_FAST_INTERP != 0
  14030. emit_uint32(loader_ctx, data_seg_idx);
  14031. #endif
  14032. if (module->import_memory_count == 0
  14033. && module->memory_count == 0)
  14034. goto fail_unknown_memory;
  14035. pb_read_leb_uint32(p, p_end, memidx);
  14036. check_memidx(module, memidx);
  14037. if (data_seg_idx >= module->data_seg_count) {
  14038. set_error_buf_v(error_buf, error_buf_size,
  14039. "unknown data segment %d",
  14040. data_seg_idx);
  14041. goto fail;
  14042. }
  14043. if (module->data_seg_count1 == 0)
  14044. goto fail_data_cnt_sec_require;
  14045. POP_I32();
  14046. POP_I32();
  14047. POP_MEM_OFFSET();
  14048. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14049. func->has_memory_operations = true;
  14050. #endif
  14051. #if WASM_ENABLE_WAMR_COMPILER != 0
  14052. module->is_bulk_memory_used = true;
  14053. #endif
  14054. break;
  14055. }
  14056. case WASM_OP_DATA_DROP:
  14057. {
  14058. pb_read_leb_uint32(p, p_end, data_seg_idx);
  14059. #if WASM_ENABLE_FAST_INTERP != 0
  14060. emit_uint32(loader_ctx, data_seg_idx);
  14061. #endif
  14062. if (data_seg_idx >= module->data_seg_count) {
  14063. set_error_buf(error_buf, error_buf_size,
  14064. "unknown data segment");
  14065. goto fail;
  14066. }
  14067. if (module->data_seg_count1 == 0)
  14068. goto fail_data_cnt_sec_require;
  14069. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14070. func->has_memory_operations = true;
  14071. #endif
  14072. #if WASM_ENABLE_WAMR_COMPILER != 0
  14073. module->is_bulk_memory_used = true;
  14074. #endif
  14075. break;
  14076. }
  14077. case WASM_OP_MEMORY_COPY:
  14078. {
  14079. CHECK_BUF(p, p_end, sizeof(int16));
  14080. /* check both src and dst memory index */
  14081. pb_read_leb_uint32(p, p_end, memidx);
  14082. check_memidx(module, memidx);
  14083. pb_read_leb_uint32(p, p_end, memidx);
  14084. check_memidx(module, memidx);
  14085. if (module->import_memory_count == 0
  14086. && module->memory_count == 0)
  14087. goto fail_unknown_memory;
  14088. POP_MEM_OFFSET();
  14089. POP_MEM_OFFSET();
  14090. POP_MEM_OFFSET();
  14091. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14092. func->has_memory_operations = true;
  14093. #endif
  14094. #if WASM_ENABLE_WAMR_COMPILER != 0
  14095. module->is_bulk_memory_used = true;
  14096. #endif
  14097. break;
  14098. }
  14099. case WASM_OP_MEMORY_FILL:
  14100. {
  14101. pb_read_leb_uint32(p, p_end, memidx);
  14102. check_memidx(module, memidx);
  14103. if (module->import_memory_count == 0
  14104. && module->memory_count == 0) {
  14105. goto fail_unknown_memory;
  14106. }
  14107. POP_MEM_OFFSET();
  14108. POP_I32();
  14109. POP_MEM_OFFSET();
  14110. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14111. func->has_memory_operations = true;
  14112. #endif
  14113. #if WASM_ENABLE_WAMR_COMPILER != 0
  14114. module->is_bulk_memory_used = true;
  14115. #endif
  14116. break;
  14117. }
  14118. fail_unknown_memory:
  14119. set_error_buf(error_buf, error_buf_size,
  14120. "unknown memory 0");
  14121. goto fail;
  14122. fail_data_cnt_sec_require:
  14123. set_error_buf(error_buf, error_buf_size,
  14124. "data count section required");
  14125. goto fail;
  14126. #endif /* WASM_ENABLE_BULK_MEMORY */
  14127. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  14128. case WASM_OP_TABLE_INIT:
  14129. {
  14130. uint8 seg_type = 0, tbl_type = 0;
  14131. #if WASM_ENABLE_GC != 0
  14132. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  14133. #endif
  14134. pb_read_leb_uint32(p, p_end, table_seg_idx);
  14135. pb_read_leb_uint32(p, p_end, table_idx);
  14136. if (!get_table_elem_type(module, table_idx, &tbl_type,
  14137. #if WASM_ENABLE_GC != 0
  14138. (void **)&tbl_ref_type,
  14139. #else
  14140. NULL,
  14141. #endif
  14142. error_buf, error_buf_size))
  14143. goto fail;
  14144. if (!get_table_seg_elem_type(module, table_seg_idx,
  14145. &seg_type,
  14146. #if WASM_ENABLE_GC != 0
  14147. (void **)&seg_ref_type,
  14148. #else
  14149. NULL,
  14150. #endif
  14151. error_buf, error_buf_size))
  14152. goto fail;
  14153. #if WASM_ENABLE_GC == 0
  14154. if (seg_type != tbl_type) {
  14155. set_error_buf(error_buf, error_buf_size,
  14156. "type mismatch");
  14157. goto fail;
  14158. }
  14159. #else
  14160. if (!wasm_reftype_is_subtype_of(
  14161. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  14162. module->types, module->type_count)) {
  14163. set_error_buf(error_buf, error_buf_size,
  14164. "type mismatch");
  14165. goto fail;
  14166. }
  14167. #endif
  14168. #if WASM_ENABLE_FAST_INTERP != 0
  14169. emit_uint32(loader_ctx, table_seg_idx);
  14170. emit_uint32(loader_ctx, table_idx);
  14171. #endif
  14172. POP_I32();
  14173. POP_I32();
  14174. #if WASM_ENABLE_MEMORY64 != 0
  14175. table_elem_idx_type = is_table_64bit(module, table_idx)
  14176. ? VALUE_TYPE_I64
  14177. : VALUE_TYPE_I32;
  14178. #endif
  14179. POP_TBL_ELEM_IDX();
  14180. #if WASM_ENABLE_WAMR_COMPILER != 0
  14181. module->is_ref_types_used = true;
  14182. #endif
  14183. break;
  14184. }
  14185. case WASM_OP_ELEM_DROP:
  14186. {
  14187. pb_read_leb_uint32(p, p_end, table_seg_idx);
  14188. if (!get_table_seg_elem_type(module, table_seg_idx,
  14189. NULL, NULL, error_buf,
  14190. error_buf_size))
  14191. goto fail;
  14192. #if WASM_ENABLE_FAST_INTERP != 0
  14193. emit_uint32(loader_ctx, table_seg_idx);
  14194. #endif
  14195. #if WASM_ENABLE_WAMR_COMPILER != 0
  14196. module->is_ref_types_used = true;
  14197. #endif
  14198. break;
  14199. }
  14200. case WASM_OP_TABLE_COPY:
  14201. {
  14202. uint8 src_type, dst_type, src_tbl_idx_type,
  14203. dst_tbl_idx_type, min_tbl_idx_type;
  14204. #if WASM_ENABLE_GC != 0
  14205. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  14206. #endif
  14207. uint32 src_tbl_idx, dst_tbl_idx;
  14208. pb_read_leb_uint32(p, p_end, dst_tbl_idx);
  14209. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  14210. #if WASM_ENABLE_GC != 0
  14211. (void **)&dst_ref_type,
  14212. #else
  14213. NULL,
  14214. #endif
  14215. error_buf, error_buf_size))
  14216. goto fail;
  14217. pb_read_leb_uint32(p, p_end, src_tbl_idx);
  14218. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  14219. #if WASM_ENABLE_GC != 0
  14220. (void **)&src_ref_type,
  14221. #else
  14222. NULL,
  14223. #endif
  14224. error_buf, error_buf_size))
  14225. goto fail;
  14226. #if WASM_ENABLE_GC == 0
  14227. if (src_type != dst_type) {
  14228. set_error_buf(error_buf, error_buf_size,
  14229. "type mismatch");
  14230. goto fail;
  14231. }
  14232. #else
  14233. if (!wasm_reftype_is_subtype_of(
  14234. src_type, src_ref_type, dst_type, dst_ref_type,
  14235. module->types, module->type_count)) {
  14236. set_error_buf(error_buf, error_buf_size,
  14237. "type mismatch");
  14238. goto fail;
  14239. }
  14240. #endif
  14241. #if WASM_ENABLE_FAST_INTERP != 0
  14242. emit_uint32(loader_ctx, dst_tbl_idx);
  14243. emit_uint32(loader_ctx, src_tbl_idx);
  14244. #endif
  14245. #if WASM_ENABLE_MEMORY64 != 0
  14246. src_tbl_idx_type = is_table_64bit(module, src_tbl_idx)
  14247. ? VALUE_TYPE_I64
  14248. : VALUE_TYPE_I32;
  14249. dst_tbl_idx_type = is_table_64bit(module, dst_tbl_idx)
  14250. ? VALUE_TYPE_I64
  14251. : VALUE_TYPE_I32;
  14252. min_tbl_idx_type =
  14253. (src_tbl_idx_type == VALUE_TYPE_I32
  14254. || dst_tbl_idx_type == VALUE_TYPE_I32)
  14255. ? VALUE_TYPE_I32
  14256. : VALUE_TYPE_I64;
  14257. #else
  14258. src_tbl_idx_type = VALUE_TYPE_I32;
  14259. dst_tbl_idx_type = VALUE_TYPE_I32;
  14260. min_tbl_idx_type = VALUE_TYPE_I32;
  14261. #endif
  14262. table_elem_idx_type = min_tbl_idx_type;
  14263. POP_TBL_ELEM_IDX();
  14264. table_elem_idx_type = src_tbl_idx_type;
  14265. POP_TBL_ELEM_IDX();
  14266. table_elem_idx_type = dst_tbl_idx_type;
  14267. POP_TBL_ELEM_IDX();
  14268. #if WASM_ENABLE_WAMR_COMPILER != 0
  14269. module->is_ref_types_used = true;
  14270. #endif
  14271. break;
  14272. }
  14273. case WASM_OP_TABLE_SIZE:
  14274. {
  14275. pb_read_leb_uint32(p, p_end, table_idx);
  14276. /* TODO: shall we create a new function to check
  14277. table idx instead of using below function? */
  14278. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  14279. error_buf, error_buf_size))
  14280. goto fail;
  14281. #if WASM_ENABLE_FAST_INTERP != 0
  14282. emit_uint32(loader_ctx, table_idx);
  14283. #endif
  14284. #if WASM_ENABLE_MEMORY64 != 0
  14285. table_elem_idx_type = is_table_64bit(module, table_idx)
  14286. ? VALUE_TYPE_I64
  14287. : VALUE_TYPE_I32;
  14288. #endif
  14289. PUSH_TBL_ELEM_IDX();
  14290. #if WASM_ENABLE_WAMR_COMPILER != 0
  14291. module->is_ref_types_used = true;
  14292. #endif
  14293. break;
  14294. }
  14295. case WASM_OP_TABLE_GROW:
  14296. case WASM_OP_TABLE_FILL:
  14297. {
  14298. uint8 decl_type;
  14299. #if WASM_ENABLE_GC != 0
  14300. WASMRefType *ref_type = NULL;
  14301. #endif
  14302. pb_read_leb_uint32(p, p_end, table_idx);
  14303. if (!get_table_elem_type(module, table_idx, &decl_type,
  14304. #if WASM_ENABLE_GC != 0
  14305. (void **)&ref_type,
  14306. #else
  14307. NULL,
  14308. #endif
  14309. error_buf, error_buf_size))
  14310. goto fail;
  14311. #if WASM_ENABLE_GC != 0
  14312. if (wasm_is_type_multi_byte_type(decl_type)) {
  14313. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  14314. ref_type,
  14315. wasm_reftype_struct_size(ref_type));
  14316. }
  14317. #endif
  14318. if (opcode1 == WASM_OP_TABLE_GROW) {
  14319. if (table_idx < module->import_table_count) {
  14320. module->import_tables[table_idx]
  14321. .u.table.table_type.possible_grow = true;
  14322. }
  14323. else {
  14324. module
  14325. ->tables[table_idx
  14326. - module->import_table_count]
  14327. .table_type.possible_grow = true;
  14328. }
  14329. }
  14330. #if WASM_ENABLE_FAST_INTERP != 0
  14331. emit_uint32(loader_ctx, table_idx);
  14332. #endif
  14333. #if WASM_ENABLE_MEMORY64 != 0
  14334. table_elem_idx_type = is_table_64bit(module, table_idx)
  14335. ? VALUE_TYPE_I64
  14336. : VALUE_TYPE_I32;
  14337. #endif
  14338. POP_TBL_ELEM_IDX();
  14339. #if WASM_ENABLE_FAST_INTERP != 0
  14340. POP_OFFSET_TYPE(decl_type);
  14341. #endif
  14342. POP_TYPE(decl_type);
  14343. if (opcode1 == WASM_OP_TABLE_GROW)
  14344. PUSH_TBL_ELEM_IDX();
  14345. else
  14346. POP_TBL_ELEM_IDX();
  14347. #if WASM_ENABLE_WAMR_COMPILER != 0
  14348. module->is_ref_types_used = true;
  14349. #endif
  14350. break;
  14351. }
  14352. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  14353. default:
  14354. set_error_buf_v(error_buf, error_buf_size,
  14355. "%s %02x %02x", "unsupported opcode",
  14356. 0xfc, opcode1);
  14357. goto fail;
  14358. }
  14359. break;
  14360. }
  14361. #if WASM_ENABLE_SIMD != 0
  14362. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  14363. || (WASM_ENABLE_FAST_INTERP != 0)
  14364. case WASM_OP_SIMD_PREFIX:
  14365. {
  14366. uint32 opcode1;
  14367. #if WASM_ENABLE_WAMR_COMPILER != 0
  14368. /* Mark the SIMD instruction is used in this module */
  14369. module->is_simd_used = true;
  14370. #endif
  14371. pb_read_leb_uint32(p, p_end, opcode1);
  14372. #if WASM_ENABLE_FAST_INTERP != 0
  14373. emit_byte(loader_ctx, opcode1);
  14374. #endif
  14375. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  14376. */
  14377. switch (opcode1) {
  14378. /* memory instruction */
  14379. case SIMD_v128_load:
  14380. case SIMD_v128_load8x8_s:
  14381. case SIMD_v128_load8x8_u:
  14382. case SIMD_v128_load16x4_s:
  14383. case SIMD_v128_load16x4_u:
  14384. case SIMD_v128_load32x2_s:
  14385. case SIMD_v128_load32x2_u:
  14386. case SIMD_v128_load8_splat:
  14387. case SIMD_v128_load16_splat:
  14388. case SIMD_v128_load32_splat:
  14389. case SIMD_v128_load64_splat:
  14390. {
  14391. CHECK_MEMORY();
  14392. pb_read_leb_uint32(p, p_end, align); /* align */
  14393. if (!check_simd_memory_access_align(
  14394. opcode1, align, error_buf, error_buf_size)) {
  14395. goto fail;
  14396. }
  14397. pb_read_leb_mem_offset(p, p_end,
  14398. mem_offset); /* offset */
  14399. #if WASM_ENABLE_FAST_INTERP != 0
  14400. emit_uint32(loader_ctx, mem_offset);
  14401. #endif
  14402. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14403. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14404. func->has_memory_operations = true;
  14405. #endif
  14406. break;
  14407. }
  14408. case SIMD_v128_store:
  14409. {
  14410. CHECK_MEMORY();
  14411. pb_read_leb_uint32(p, p_end, align); /* align */
  14412. if (!check_simd_memory_access_align(
  14413. opcode1, align, error_buf, error_buf_size)) {
  14414. goto fail;
  14415. }
  14416. pb_read_leb_mem_offset(p, p_end,
  14417. mem_offset); /* offset */
  14418. #if WASM_ENABLE_FAST_INTERP != 0
  14419. emit_uint32(loader_ctx, mem_offset);
  14420. #endif
  14421. POP_V128();
  14422. POP_MEM_OFFSET();
  14423. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14424. func->has_memory_operations = true;
  14425. #endif
  14426. break;
  14427. }
  14428. /* basic operation */
  14429. case SIMD_v128_const:
  14430. {
  14431. #if WASM_ENABLE_FAST_INTERP != 0
  14432. uint64 high, low;
  14433. #endif
  14434. CHECK_BUF1(p, p_end, 16);
  14435. #if WASM_ENABLE_FAST_INTERP != 0
  14436. wasm_runtime_read_v128(p, &high, &low);
  14437. emit_uint64(loader_ctx, high);
  14438. emit_uint64(loader_ctx, low);
  14439. #endif
  14440. p += 16;
  14441. PUSH_V128();
  14442. break;
  14443. }
  14444. case SIMD_v8x16_shuffle:
  14445. {
  14446. V128 mask;
  14447. CHECK_BUF1(p, p_end, 16);
  14448. mask = read_i8x16(p, error_buf, error_buf_size);
  14449. if (!check_simd_shuffle_mask(mask, error_buf,
  14450. error_buf_size)) {
  14451. goto fail;
  14452. }
  14453. #if WASM_ENABLE_FAST_INTERP != 0
  14454. uint64 high, low;
  14455. wasm_runtime_read_v128(p, &high, &low);
  14456. emit_uint64(loader_ctx, high);
  14457. emit_uint64(loader_ctx, low);
  14458. #endif
  14459. p += 16;
  14460. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14461. break;
  14462. }
  14463. case SIMD_v8x16_swizzle:
  14464. {
  14465. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14466. break;
  14467. }
  14468. /* splat operation */
  14469. case SIMD_i8x16_splat:
  14470. case SIMD_i16x8_splat:
  14471. case SIMD_i32x4_splat:
  14472. case SIMD_i64x2_splat:
  14473. case SIMD_f32x4_splat:
  14474. case SIMD_f64x2_splat:
  14475. {
  14476. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  14477. VALUE_TYPE_I32, VALUE_TYPE_I64,
  14478. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  14479. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  14480. VALUE_TYPE_V128);
  14481. break;
  14482. }
  14483. /* lane operation */
  14484. case SIMD_i8x16_extract_lane_s:
  14485. case SIMD_i8x16_extract_lane_u:
  14486. case SIMD_i8x16_replace_lane:
  14487. case SIMD_i16x8_extract_lane_s:
  14488. case SIMD_i16x8_extract_lane_u:
  14489. case SIMD_i16x8_replace_lane:
  14490. case SIMD_i32x4_extract_lane:
  14491. case SIMD_i32x4_replace_lane:
  14492. case SIMD_i64x2_extract_lane:
  14493. case SIMD_i64x2_replace_lane:
  14494. case SIMD_f32x4_extract_lane:
  14495. case SIMD_f32x4_replace_lane:
  14496. case SIMD_f64x2_extract_lane:
  14497. case SIMD_f64x2_replace_lane:
  14498. {
  14499. uint8 lane;
  14500. /* clang-format off */
  14501. uint8 replace[] = {
  14502. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  14503. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  14504. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  14505. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  14506. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  14507. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  14508. };
  14509. uint8 push_type[] = {
  14510. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14511. VALUE_TYPE_V128,
  14512. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14513. VALUE_TYPE_V128,
  14514. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  14515. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  14516. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  14517. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  14518. };
  14519. /* clang-format on */
  14520. CHECK_BUF(p, p_end, 1);
  14521. lane = read_uint8(p);
  14522. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14523. error_buf_size)) {
  14524. goto fail;
  14525. }
  14526. #if WASM_ENABLE_FAST_INTERP != 0
  14527. emit_byte(loader_ctx, lane);
  14528. #endif
  14529. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  14530. #if WASM_ENABLE_FAST_INTERP != 0
  14531. if (!(wasm_loader_pop_frame_ref_offset(
  14532. loader_ctx,
  14533. replace[opcode1
  14534. - SIMD_i8x16_extract_lane_s],
  14535. error_buf, error_buf_size)))
  14536. goto fail;
  14537. #else
  14538. if (!(wasm_loader_pop_frame_ref(
  14539. loader_ctx,
  14540. replace[opcode1
  14541. - SIMD_i8x16_extract_lane_s],
  14542. error_buf, error_buf_size)))
  14543. goto fail;
  14544. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  14545. }
  14546. POP_AND_PUSH(
  14547. VALUE_TYPE_V128,
  14548. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  14549. break;
  14550. }
  14551. /* i8x16 compare operation */
  14552. case SIMD_i8x16_eq:
  14553. case SIMD_i8x16_ne:
  14554. case SIMD_i8x16_lt_s:
  14555. case SIMD_i8x16_lt_u:
  14556. case SIMD_i8x16_gt_s:
  14557. case SIMD_i8x16_gt_u:
  14558. case SIMD_i8x16_le_s:
  14559. case SIMD_i8x16_le_u:
  14560. case SIMD_i8x16_ge_s:
  14561. case SIMD_i8x16_ge_u:
  14562. /* i16x8 compare operation */
  14563. case SIMD_i16x8_eq:
  14564. case SIMD_i16x8_ne:
  14565. case SIMD_i16x8_lt_s:
  14566. case SIMD_i16x8_lt_u:
  14567. case SIMD_i16x8_gt_s:
  14568. case SIMD_i16x8_gt_u:
  14569. case SIMD_i16x8_le_s:
  14570. case SIMD_i16x8_le_u:
  14571. case SIMD_i16x8_ge_s:
  14572. case SIMD_i16x8_ge_u:
  14573. /* i32x4 compare operation */
  14574. case SIMD_i32x4_eq:
  14575. case SIMD_i32x4_ne:
  14576. case SIMD_i32x4_lt_s:
  14577. case SIMD_i32x4_lt_u:
  14578. case SIMD_i32x4_gt_s:
  14579. case SIMD_i32x4_gt_u:
  14580. case SIMD_i32x4_le_s:
  14581. case SIMD_i32x4_le_u:
  14582. case SIMD_i32x4_ge_s:
  14583. case SIMD_i32x4_ge_u:
  14584. /* f32x4 compare operation */
  14585. case SIMD_f32x4_eq:
  14586. case SIMD_f32x4_ne:
  14587. case SIMD_f32x4_lt:
  14588. case SIMD_f32x4_gt:
  14589. case SIMD_f32x4_le:
  14590. case SIMD_f32x4_ge:
  14591. /* f64x2 compare operation */
  14592. case SIMD_f64x2_eq:
  14593. case SIMD_f64x2_ne:
  14594. case SIMD_f64x2_lt:
  14595. case SIMD_f64x2_gt:
  14596. case SIMD_f64x2_le:
  14597. case SIMD_f64x2_ge:
  14598. {
  14599. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14600. break;
  14601. }
  14602. /* v128 operation */
  14603. case SIMD_v128_not:
  14604. {
  14605. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14606. break;
  14607. }
  14608. case SIMD_v128_and:
  14609. case SIMD_v128_andnot:
  14610. case SIMD_v128_or:
  14611. case SIMD_v128_xor:
  14612. {
  14613. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14614. break;
  14615. }
  14616. case SIMD_v128_bitselect:
  14617. {
  14618. POP_V128();
  14619. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14620. break;
  14621. }
  14622. case SIMD_v128_any_true:
  14623. {
  14624. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14625. break;
  14626. }
  14627. /* Load Lane Operation */
  14628. case SIMD_v128_load8_lane:
  14629. case SIMD_v128_load16_lane:
  14630. case SIMD_v128_load32_lane:
  14631. case SIMD_v128_load64_lane:
  14632. case SIMD_v128_store8_lane:
  14633. case SIMD_v128_store16_lane:
  14634. case SIMD_v128_store32_lane:
  14635. case SIMD_v128_store64_lane:
  14636. {
  14637. uint8 lane;
  14638. CHECK_MEMORY();
  14639. pb_read_leb_uint32(p, p_end, align); /* align */
  14640. if (!check_simd_memory_access_align(
  14641. opcode1, align, error_buf, error_buf_size)) {
  14642. goto fail;
  14643. }
  14644. pb_read_leb_mem_offset(p, p_end,
  14645. mem_offset); /* offset */
  14646. CHECK_BUF(p, p_end, 1);
  14647. lane = read_uint8(p);
  14648. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14649. error_buf_size)) {
  14650. goto fail;
  14651. }
  14652. #if WASM_ENABLE_FAST_INTERP != 0
  14653. emit_uint32(loader_ctx, mem_offset);
  14654. #endif
  14655. POP_V128();
  14656. POP_MEM_OFFSET();
  14657. #if WASM_ENABLE_FAST_INTERP != 0
  14658. emit_byte(loader_ctx, lane);
  14659. #endif
  14660. if (opcode1 < SIMD_v128_store8_lane) {
  14661. PUSH_V128();
  14662. }
  14663. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14664. func->has_memory_operations = true;
  14665. #endif
  14666. break;
  14667. }
  14668. case SIMD_v128_load32_zero:
  14669. case SIMD_v128_load64_zero:
  14670. {
  14671. CHECK_MEMORY();
  14672. pb_read_leb_uint32(p, p_end, align); /* align */
  14673. if (!check_simd_memory_access_align(
  14674. opcode1, align, error_buf, error_buf_size)) {
  14675. goto fail;
  14676. }
  14677. pb_read_leb_mem_offset(p, p_end,
  14678. mem_offset); /* offset */
  14679. #if WASM_ENABLE_FAST_INTERP != 0
  14680. emit_uint32(loader_ctx, mem_offset);
  14681. #endif
  14682. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14683. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14684. func->has_memory_operations = true;
  14685. #endif
  14686. break;
  14687. }
  14688. /* Float conversion */
  14689. case SIMD_f32x4_demote_f64x2_zero:
  14690. case SIMD_f64x2_promote_low_f32x4_zero:
  14691. {
  14692. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14693. break;
  14694. }
  14695. /* i8x16 Operation */
  14696. case SIMD_i8x16_abs:
  14697. case SIMD_i8x16_neg:
  14698. case SIMD_i8x16_popcnt:
  14699. {
  14700. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14701. break;
  14702. }
  14703. case SIMD_i8x16_all_true:
  14704. case SIMD_i8x16_bitmask:
  14705. {
  14706. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14707. break;
  14708. }
  14709. case SIMD_i8x16_narrow_i16x8_s:
  14710. case SIMD_i8x16_narrow_i16x8_u:
  14711. {
  14712. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14713. break;
  14714. }
  14715. case SIMD_f32x4_ceil:
  14716. case SIMD_f32x4_floor:
  14717. case SIMD_f32x4_trunc:
  14718. case SIMD_f32x4_nearest:
  14719. {
  14720. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14721. break;
  14722. }
  14723. case SIMD_i8x16_shl:
  14724. case SIMD_i8x16_shr_s:
  14725. case SIMD_i8x16_shr_u:
  14726. {
  14727. POP_I32();
  14728. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14729. break;
  14730. }
  14731. case SIMD_i8x16_add:
  14732. case SIMD_i8x16_add_sat_s:
  14733. case SIMD_i8x16_add_sat_u:
  14734. case SIMD_i8x16_sub:
  14735. case SIMD_i8x16_sub_sat_s:
  14736. case SIMD_i8x16_sub_sat_u:
  14737. {
  14738. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14739. break;
  14740. }
  14741. case SIMD_f64x2_ceil:
  14742. case SIMD_f64x2_floor:
  14743. {
  14744. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14745. break;
  14746. }
  14747. case SIMD_i8x16_min_s:
  14748. case SIMD_i8x16_min_u:
  14749. case SIMD_i8x16_max_s:
  14750. case SIMD_i8x16_max_u:
  14751. {
  14752. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14753. break;
  14754. }
  14755. case SIMD_f64x2_trunc:
  14756. {
  14757. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14758. break;
  14759. }
  14760. case SIMD_i8x16_avgr_u:
  14761. {
  14762. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14763. break;
  14764. }
  14765. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  14766. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  14767. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  14768. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  14769. /* i16x8 operation */
  14770. case SIMD_i16x8_abs:
  14771. case SIMD_i16x8_neg:
  14772. {
  14773. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14774. break;
  14775. }
  14776. case SIMD_i16x8_q15mulr_sat_s:
  14777. {
  14778. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14779. break;
  14780. }
  14781. case SIMD_i16x8_all_true:
  14782. case SIMD_i16x8_bitmask:
  14783. {
  14784. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14785. break;
  14786. }
  14787. case SIMD_i16x8_narrow_i32x4_s:
  14788. case SIMD_i16x8_narrow_i32x4_u:
  14789. {
  14790. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14791. break;
  14792. }
  14793. case SIMD_i16x8_extend_low_i8x16_s:
  14794. case SIMD_i16x8_extend_high_i8x16_s:
  14795. case SIMD_i16x8_extend_low_i8x16_u:
  14796. case SIMD_i16x8_extend_high_i8x16_u:
  14797. {
  14798. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14799. break;
  14800. }
  14801. case SIMD_i16x8_shl:
  14802. case SIMD_i16x8_shr_s:
  14803. case SIMD_i16x8_shr_u:
  14804. {
  14805. POP_I32();
  14806. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14807. break;
  14808. }
  14809. case SIMD_i16x8_add:
  14810. case SIMD_i16x8_add_sat_s:
  14811. case SIMD_i16x8_add_sat_u:
  14812. case SIMD_i16x8_sub:
  14813. case SIMD_i16x8_sub_sat_s:
  14814. case SIMD_i16x8_sub_sat_u:
  14815. {
  14816. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14817. break;
  14818. }
  14819. case SIMD_f64x2_nearest:
  14820. {
  14821. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14822. break;
  14823. }
  14824. case SIMD_i16x8_mul:
  14825. case SIMD_i16x8_min_s:
  14826. case SIMD_i16x8_min_u:
  14827. case SIMD_i16x8_max_s:
  14828. case SIMD_i16x8_max_u:
  14829. case SIMD_i16x8_avgr_u:
  14830. case SIMD_i16x8_extmul_low_i8x16_s:
  14831. case SIMD_i16x8_extmul_high_i8x16_s:
  14832. case SIMD_i16x8_extmul_low_i8x16_u:
  14833. case SIMD_i16x8_extmul_high_i8x16_u:
  14834. {
  14835. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14836. break;
  14837. }
  14838. /* i32x4 operation */
  14839. case SIMD_i32x4_abs:
  14840. case SIMD_i32x4_neg:
  14841. {
  14842. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14843. break;
  14844. }
  14845. case SIMD_i32x4_all_true:
  14846. case SIMD_i32x4_bitmask:
  14847. {
  14848. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14849. break;
  14850. }
  14851. case SIMD_i32x4_extend_low_i16x8_s:
  14852. case SIMD_i32x4_extend_high_i16x8_s:
  14853. case SIMD_i32x4_extend_low_i16x8_u:
  14854. case SIMD_i32x4_extend_high_i16x8_u:
  14855. {
  14856. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14857. break;
  14858. }
  14859. case SIMD_i32x4_shl:
  14860. case SIMD_i32x4_shr_s:
  14861. case SIMD_i32x4_shr_u:
  14862. {
  14863. POP_I32();
  14864. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14865. break;
  14866. }
  14867. case SIMD_i32x4_add:
  14868. case SIMD_i32x4_sub:
  14869. case SIMD_i32x4_mul:
  14870. case SIMD_i32x4_min_s:
  14871. case SIMD_i32x4_min_u:
  14872. case SIMD_i32x4_max_s:
  14873. case SIMD_i32x4_max_u:
  14874. case SIMD_i32x4_dot_i16x8_s:
  14875. case SIMD_i32x4_extmul_low_i16x8_s:
  14876. case SIMD_i32x4_extmul_high_i16x8_s:
  14877. case SIMD_i32x4_extmul_low_i16x8_u:
  14878. case SIMD_i32x4_extmul_high_i16x8_u:
  14879. {
  14880. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14881. break;
  14882. }
  14883. /* i64x2 operation */
  14884. case SIMD_i64x2_abs:
  14885. case SIMD_i64x2_neg:
  14886. {
  14887. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14888. break;
  14889. }
  14890. case SIMD_i64x2_all_true:
  14891. case SIMD_i64x2_bitmask:
  14892. {
  14893. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14894. break;
  14895. }
  14896. case SIMD_i64x2_extend_low_i32x4_s:
  14897. case SIMD_i64x2_extend_high_i32x4_s:
  14898. case SIMD_i64x2_extend_low_i32x4_u:
  14899. case SIMD_i64x2_extend_high_i32x4_u:
  14900. {
  14901. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14902. break;
  14903. }
  14904. case SIMD_i64x2_shl:
  14905. case SIMD_i64x2_shr_s:
  14906. case SIMD_i64x2_shr_u:
  14907. {
  14908. POP_I32();
  14909. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14910. break;
  14911. }
  14912. case SIMD_i64x2_add:
  14913. case SIMD_i64x2_sub:
  14914. case SIMD_i64x2_mul:
  14915. case SIMD_i64x2_eq:
  14916. case SIMD_i64x2_ne:
  14917. case SIMD_i64x2_lt_s:
  14918. case SIMD_i64x2_gt_s:
  14919. case SIMD_i64x2_le_s:
  14920. case SIMD_i64x2_ge_s:
  14921. case SIMD_i64x2_extmul_low_i32x4_s:
  14922. case SIMD_i64x2_extmul_high_i32x4_s:
  14923. case SIMD_i64x2_extmul_low_i32x4_u:
  14924. case SIMD_i64x2_extmul_high_i32x4_u:
  14925. {
  14926. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14927. break;
  14928. }
  14929. /* f32x4 operation */
  14930. case SIMD_f32x4_abs:
  14931. case SIMD_f32x4_neg:
  14932. case SIMD_f32x4_sqrt:
  14933. {
  14934. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14935. break;
  14936. }
  14937. case SIMD_f32x4_add:
  14938. case SIMD_f32x4_sub:
  14939. case SIMD_f32x4_mul:
  14940. case SIMD_f32x4_div:
  14941. case SIMD_f32x4_min:
  14942. case SIMD_f32x4_max:
  14943. case SIMD_f32x4_pmin:
  14944. case SIMD_f32x4_pmax:
  14945. {
  14946. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14947. break;
  14948. }
  14949. /* f64x2 operation */
  14950. case SIMD_f64x2_abs:
  14951. case SIMD_f64x2_neg:
  14952. case SIMD_f64x2_sqrt:
  14953. {
  14954. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14955. break;
  14956. }
  14957. case SIMD_f64x2_add:
  14958. case SIMD_f64x2_sub:
  14959. case SIMD_f64x2_mul:
  14960. case SIMD_f64x2_div:
  14961. case SIMD_f64x2_min:
  14962. case SIMD_f64x2_max:
  14963. case SIMD_f64x2_pmin:
  14964. case SIMD_f64x2_pmax:
  14965. {
  14966. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14967. break;
  14968. }
  14969. case SIMD_i32x4_trunc_sat_f32x4_s:
  14970. case SIMD_i32x4_trunc_sat_f32x4_u:
  14971. case SIMD_f32x4_convert_i32x4_s:
  14972. case SIMD_f32x4_convert_i32x4_u:
  14973. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14974. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14975. case SIMD_f64x2_convert_low_i32x4_s:
  14976. case SIMD_f64x2_convert_low_i32x4_u:
  14977. {
  14978. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14979. break;
  14980. }
  14981. default:
  14982. {
  14983. if (error_buf != NULL) {
  14984. snprintf(error_buf, error_buf_size,
  14985. "WASM module load failed: "
  14986. "invalid opcode 0xfd %02x.",
  14987. opcode1);
  14988. }
  14989. goto fail;
  14990. }
  14991. }
  14992. break;
  14993. }
  14994. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  14995. (WASM_ENABLE_FAST_INTERP != 0) */
  14996. #endif /* end of WASM_ENABLE_SIMD */
  14997. #if WASM_ENABLE_SHARED_MEMORY != 0
  14998. case WASM_OP_ATOMIC_PREFIX:
  14999. {
  15000. uint32 opcode1;
  15001. pb_read_leb_uint32(p, p_end, opcode1);
  15002. #if WASM_ENABLE_FAST_INTERP != 0
  15003. emit_byte(loader_ctx, opcode1);
  15004. #endif
  15005. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  15006. CHECK_MEMORY();
  15007. pb_read_leb_uint32(p, p_end, align); /* align */
  15008. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  15009. if (!check_memory_align_equal(opcode1, align, error_buf,
  15010. error_buf_size)) {
  15011. goto fail;
  15012. }
  15013. #if WASM_ENABLE_FAST_INTERP != 0
  15014. emit_uint32(loader_ctx, mem_offset);
  15015. #endif
  15016. }
  15017. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  15018. func->has_memory_operations = true;
  15019. #endif
  15020. switch (opcode1) {
  15021. case WASM_OP_ATOMIC_NOTIFY:
  15022. POP_I32();
  15023. POP_MEM_OFFSET();
  15024. PUSH_I32();
  15025. break;
  15026. case WASM_OP_ATOMIC_WAIT32:
  15027. POP_I64();
  15028. POP_I32();
  15029. POP_MEM_OFFSET();
  15030. PUSH_I32();
  15031. break;
  15032. case WASM_OP_ATOMIC_WAIT64:
  15033. POP_I64();
  15034. POP_I64();
  15035. POP_MEM_OFFSET();
  15036. PUSH_I32();
  15037. break;
  15038. case WASM_OP_ATOMIC_FENCE:
  15039. /* reserved byte 0x00 */
  15040. if (*p++ != 0x00) {
  15041. set_error_buf(error_buf, error_buf_size,
  15042. "zero byte expected");
  15043. goto fail;
  15044. }
  15045. break;
  15046. case WASM_OP_ATOMIC_I32_LOAD:
  15047. case WASM_OP_ATOMIC_I32_LOAD8_U:
  15048. case WASM_OP_ATOMIC_I32_LOAD16_U:
  15049. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  15050. break;
  15051. case WASM_OP_ATOMIC_I32_STORE:
  15052. case WASM_OP_ATOMIC_I32_STORE8:
  15053. case WASM_OP_ATOMIC_I32_STORE16:
  15054. POP_I32();
  15055. POP_MEM_OFFSET();
  15056. break;
  15057. case WASM_OP_ATOMIC_I64_LOAD:
  15058. case WASM_OP_ATOMIC_I64_LOAD8_U:
  15059. case WASM_OP_ATOMIC_I64_LOAD16_U:
  15060. case WASM_OP_ATOMIC_I64_LOAD32_U:
  15061. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  15062. break;
  15063. case WASM_OP_ATOMIC_I64_STORE:
  15064. case WASM_OP_ATOMIC_I64_STORE8:
  15065. case WASM_OP_ATOMIC_I64_STORE16:
  15066. case WASM_OP_ATOMIC_I64_STORE32:
  15067. POP_I64();
  15068. POP_MEM_OFFSET();
  15069. break;
  15070. case WASM_OP_ATOMIC_RMW_I32_ADD:
  15071. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  15072. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  15073. case WASM_OP_ATOMIC_RMW_I32_SUB:
  15074. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  15075. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  15076. case WASM_OP_ATOMIC_RMW_I32_AND:
  15077. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  15078. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  15079. case WASM_OP_ATOMIC_RMW_I32_OR:
  15080. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  15081. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  15082. case WASM_OP_ATOMIC_RMW_I32_XOR:
  15083. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  15084. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  15085. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  15086. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  15087. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  15088. POP_I32();
  15089. POP_MEM_OFFSET();
  15090. PUSH_I32();
  15091. break;
  15092. case WASM_OP_ATOMIC_RMW_I64_ADD:
  15093. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  15094. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  15095. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  15096. case WASM_OP_ATOMIC_RMW_I64_SUB:
  15097. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  15098. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  15099. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  15100. case WASM_OP_ATOMIC_RMW_I64_AND:
  15101. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  15102. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  15103. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  15104. case WASM_OP_ATOMIC_RMW_I64_OR:
  15105. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  15106. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  15107. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  15108. case WASM_OP_ATOMIC_RMW_I64_XOR:
  15109. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  15110. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  15111. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  15112. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  15113. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  15114. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  15115. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  15116. POP_I64();
  15117. POP_MEM_OFFSET();
  15118. PUSH_I64();
  15119. break;
  15120. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  15121. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  15122. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  15123. POP_I32();
  15124. POP_I32();
  15125. POP_MEM_OFFSET();
  15126. PUSH_I32();
  15127. break;
  15128. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  15129. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  15130. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  15131. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  15132. POP_I64();
  15133. POP_I64();
  15134. POP_MEM_OFFSET();
  15135. PUSH_I64();
  15136. break;
  15137. default:
  15138. set_error_buf_v(error_buf, error_buf_size,
  15139. "%s %02x %02x", "unsupported opcode",
  15140. 0xfe, opcode1);
  15141. goto fail;
  15142. }
  15143. break;
  15144. }
  15145. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  15146. default:
  15147. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  15148. "unsupported opcode", opcode);
  15149. goto fail;
  15150. }
  15151. #if WASM_ENABLE_FAST_INTERP != 0
  15152. last_op = opcode;
  15153. #endif
  15154. }
  15155. if (loader_ctx->csp_num > 0) {
  15156. /* unmatched end opcodes result from unbalanced control flow structures,
  15157. * for example, br_table with inconsistent target count (1 declared, 2
  15158. * given), or simply superfluous end opcodes */
  15159. set_error_buf(
  15160. error_buf, error_buf_size,
  15161. "unexpected end opcodes from unbalanced control flow structures");
  15162. goto fail;
  15163. }
  15164. #if WASM_ENABLE_FAST_INTERP != 0
  15165. if (loader_ctx->p_code_compiled == NULL)
  15166. goto re_scan;
  15167. func->const_cell_num = loader_ctx->i64_const_num * 2
  15168. + loader_ctx->v128_const_num * 4
  15169. + loader_ctx->i32_const_num;
  15170. if (func->const_cell_num > 0) {
  15171. if (!(func->consts =
  15172. loader_malloc((uint64)sizeof(uint32) * func->const_cell_num,
  15173. error_buf, error_buf_size)))
  15174. goto fail;
  15175. if (loader_ctx->i64_const_num > 0) {
  15176. bh_memcpy_s(func->consts,
  15177. (uint32)sizeof(int64) * loader_ctx->i64_const_num,
  15178. loader_ctx->i64_consts,
  15179. (uint32)sizeof(int64) * loader_ctx->i64_const_num);
  15180. }
  15181. if (loader_ctx->i32_const_num > 0) {
  15182. bh_memcpy_s(func->consts
  15183. + sizeof(int64) * loader_ctx->i64_const_num,
  15184. (uint32)sizeof(int32) * loader_ctx->i32_const_num,
  15185. loader_ctx->i32_consts,
  15186. (uint32)sizeof(int32) * loader_ctx->i32_const_num);
  15187. }
  15188. if (loader_ctx->v128_const_num > 0) {
  15189. bh_memcpy_s(func->consts,
  15190. (uint32)sizeof(V128) * loader_ctx->v128_const_num,
  15191. loader_ctx->v128_consts,
  15192. (uint32)sizeof(V128) * loader_ctx->v128_const_num);
  15193. }
  15194. }
  15195. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  15196. - loader_ctx->start_dynamic_offset + 1;
  15197. #else
  15198. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  15199. #endif
  15200. func->max_block_num = loader_ctx->max_csp_num;
  15201. return_value = true;
  15202. fail:
  15203. wasm_loader_ctx_destroy(loader_ctx);
  15204. (void)table_idx;
  15205. (void)table_seg_idx;
  15206. (void)data_seg_idx;
  15207. (void)i64_const;
  15208. (void)local_offset;
  15209. (void)p_org;
  15210. (void)mem_offset;
  15211. (void)align;
  15212. return return_value;
  15213. }