wasm_loader.c 582 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "wasm.h"
  9. #include "wasm_opcode.h"
  10. #include "wasm_runtime.h"
  11. #include "wasm_loader_common.h"
  12. #include "../common/wasm_native.h"
  13. #include "../common/wasm_memory.h"
  14. #if WASM_ENABLE_GC != 0
  15. #include "../common/gc/gc_type.h"
  16. #include "../common/gc/gc_object.h"
  17. #endif
  18. #if WASM_ENABLE_DEBUG_INTERP != 0
  19. #include "../libraries/debug-engine/debug_engine.h"
  20. #endif
  21. #if WASM_ENABLE_FAST_JIT != 0
  22. #include "../fast-jit/jit_compiler.h"
  23. #include "../fast-jit/jit_codecache.h"
  24. #endif
  25. #if WASM_ENABLE_JIT != 0
  26. #include "../compilation/aot_llvm.h"
  27. #endif
  28. #ifndef TRACE_WASM_LOADER
  29. #define TRACE_WASM_LOADER 0
  30. #endif
  31. /* Read a value of given type from the address pointed to by the given
  32. pointer and increase the pointer to the position just after the
  33. value being read. */
  34. #define TEMPLATE_READ_VALUE(Type, p) \
  35. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  36. #if WASM_ENABLE_MEMORY64 != 0
  37. static bool
  38. has_module_memory64(WASMModule *module)
  39. {
  40. /* TODO: multi-memories for now assuming the memory idx type is consistent
  41. * across multi-memories */
  42. if (module->import_memory_count > 0)
  43. return !!(module->import_memories[0].u.memory.mem_type.flags
  44. & MEMORY64_FLAG);
  45. else if (module->memory_count > 0)
  46. return !!(module->memories[0].flags & MEMORY64_FLAG);
  47. return false;
  48. }
  49. #endif
  50. static void
  51. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  52. {
  53. wasm_loader_set_error_buf(error_buf, error_buf_size, string, false);
  54. }
  55. #if WASM_ENABLE_MEMORY64 != 0
  56. static void
  57. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  58. {
  59. if (error_buf != NULL) {
  60. snprintf(error_buf, error_buf_size, "offset out of range");
  61. }
  62. }
  63. #endif
  64. static void
  65. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  66. {
  67. va_list args;
  68. char buf[128];
  69. if (error_buf != NULL) {
  70. va_start(args, format);
  71. vsnprintf(buf, sizeof(buf), format, args);
  72. va_end(args);
  73. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  74. }
  75. }
  76. static bool
  77. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  78. char *error_buf, uint32 error_buf_size)
  79. {
  80. if ((uintptr_t)buf + length < (uintptr_t)buf
  81. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  82. set_error_buf(error_buf, error_buf_size,
  83. "unexpected end of section or function");
  84. return false;
  85. }
  86. return true;
  87. }
  88. static bool
  89. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  90. char *error_buf, uint32 error_buf_size)
  91. {
  92. if ((uintptr_t)buf + length < (uintptr_t)buf
  93. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  94. set_error_buf(error_buf, error_buf_size, "unexpected end");
  95. return false;
  96. }
  97. return true;
  98. }
  99. #define CHECK_BUF(buf, buf_end, length) \
  100. do { \
  101. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  102. goto fail; \
  103. } \
  104. } while (0)
  105. #define CHECK_BUF1(buf, buf_end, length) \
  106. do { \
  107. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  108. goto fail; \
  109. } \
  110. } while (0)
  111. #define skip_leb(p) while (*p++ & 0x80)
  112. #define skip_leb_int64(p, p_end) skip_leb(p)
  113. #define skip_leb_uint32(p, p_end) skip_leb(p)
  114. #define skip_leb_int32(p, p_end) skip_leb(p)
  115. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  116. #define skip_leb_memidx(p, p_end) skip_leb(p)
  117. #if WASM_ENABLE_MULTI_MEMORY == 0
  118. #define skip_leb_align(p, p_end) skip_leb(p)
  119. #else
  120. /* Skip the following memidx if applicable */
  121. #define skip_leb_align(p, p_end) \
  122. do { \
  123. if (*p++ & OPT_MEMIDX_FLAG) \
  124. skip_leb_uint32(p, p_end); \
  125. } while (0)
  126. #endif
  127. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  128. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  129. #define read_leb_int64(p, p_end, res) \
  130. do { \
  131. uint64 res64; \
  132. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  133. error_buf_size)) \
  134. goto fail; \
  135. res = (int64)res64; \
  136. } while (0)
  137. #if WASM_ENABLE_MEMORY64 != 0
  138. #define read_leb_mem_offset(p, p_end, res) \
  139. do { \
  140. uint64 res64; \
  141. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  142. &res64, error_buf, error_buf_size)) { \
  143. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  144. goto fail; \
  145. } \
  146. res = (mem_offset_t)res64; \
  147. } while (0)
  148. #else
  149. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  150. #endif
  151. #define read_leb_uint32(p, p_end, res) \
  152. do { \
  153. uint64 res64; \
  154. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  155. error_buf_size)) \
  156. goto fail; \
  157. res = (uint32)res64; \
  158. } while (0)
  159. #define read_leb_int32(p, p_end, res) \
  160. do { \
  161. uint64 res64; \
  162. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  163. error_buf_size)) \
  164. goto fail; \
  165. res = (int32)res64; \
  166. } while (0)
  167. #define read_leb_memidx(p, p_end, res) read_leb_uint32(p, p_end, res)
  168. #if WASM_ENABLE_MULTI_MEMORY != 0
  169. #define check_memidx(module, memidx) \
  170. do { \
  171. if (memidx >= module->import_memory_count + module->memory_count) { \
  172. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d", \
  173. memidx); \
  174. goto fail; \
  175. } \
  176. } while (0)
  177. /* Bit 6(0x40) indicating the optional memidx, and reset bit 6 for
  178. * alignment check */
  179. #define read_leb_memarg(p, p_end, res) \
  180. do { \
  181. read_leb_uint32(p, p_end, res); \
  182. if (res & OPT_MEMIDX_FLAG) { \
  183. res &= ~OPT_MEMIDX_FLAG; \
  184. read_leb_uint32(p, p_end, memidx); /* memidx */ \
  185. check_memidx(module, memidx); \
  186. } \
  187. } while (0)
  188. #else
  189. /* reserved byte 0x00 */
  190. #define check_memidx(module, memidx) \
  191. do { \
  192. (void)module; \
  193. if (memidx != 0) { \
  194. set_error_buf(error_buf, error_buf_size, "zero byte expected"); \
  195. goto fail; \
  196. } \
  197. } while (0)
  198. #define read_leb_memarg(p, p_end, res) read_leb_uint32(p, p_end, res)
  199. #endif
  200. static char *
  201. type2str(uint8 type)
  202. {
  203. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  204. #if WASM_ENABLE_GC != 0
  205. char *type_str_ref[] = { "stringview_iter",
  206. "stringview_wtf16",
  207. "(ref null ht)",
  208. "(ref ht)",
  209. "", /* reserved */
  210. "stringview_wtf8",
  211. "stringref",
  212. "", /* reserved */
  213. "", /* reserved */
  214. "arrayref",
  215. "structref",
  216. "i32ref",
  217. "eqref",
  218. "anyref",
  219. "externref",
  220. "funcref",
  221. "nullref",
  222. "nullexternref",
  223. "nullfuncref" };
  224. #endif
  225. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  226. return type_str[type - VALUE_TYPE_V128];
  227. #if WASM_ENABLE_GC != 0
  228. else if (wasm_is_type_reftype(type))
  229. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  230. #endif
  231. else if (type == VALUE_TYPE_FUNCREF)
  232. return "funcref";
  233. else if (type == VALUE_TYPE_EXTERNREF)
  234. return "externref";
  235. else
  236. return "unknown type";
  237. }
  238. static bool
  239. is_32bit_type(uint8 type)
  240. {
  241. if (type == VALUE_TYPE_I32
  242. || type == VALUE_TYPE_F32
  243. /* the operand stack is in polymorphic state */
  244. || type == VALUE_TYPE_ANY
  245. #if WASM_ENABLE_GC != 0
  246. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  247. #elif WASM_ENABLE_REF_TYPES != 0
  248. /* For reference types, we use uint32 index to represent
  249. the funcref and externref */
  250. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  251. #endif
  252. )
  253. return true;
  254. return false;
  255. }
  256. static bool
  257. is_64bit_type(uint8 type)
  258. {
  259. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  260. #if WASM_ENABLE_GC != 0
  261. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  262. #endif
  263. )
  264. return true;
  265. return false;
  266. }
  267. #if WASM_ENABLE_GC != 0
  268. static bool
  269. is_packed_type(uint8 type)
  270. {
  271. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  272. }
  273. #endif
  274. static bool
  275. is_byte_a_type(uint8 type)
  276. {
  277. return (is_valid_value_type_for_interpreter(type)
  278. || (type == VALUE_TYPE_VOID))
  279. ? true
  280. : false;
  281. }
  282. #if WASM_ENABLE_SIMD != 0
  283. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  284. static V128
  285. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  286. {
  287. V128 result;
  288. uint8 i;
  289. for (i = 0; i != 16; ++i) {
  290. result.i8x16[i] = read_uint8(p_buf);
  291. }
  292. return result;
  293. }
  294. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  295. #endif /* end of WASM_ENABLE_SIMD */
  296. static void *
  297. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  298. {
  299. void *mem;
  300. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  301. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  302. return NULL;
  303. }
  304. memset(mem, 0, (uint32)size);
  305. return mem;
  306. }
  307. static void *
  308. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  309. uint32 error_buf_size)
  310. {
  311. uint8 *mem_new;
  312. bh_assert(size_new > size_old);
  313. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  314. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  315. memset(mem_new + size_old, 0, size_new - size_old);
  316. wasm_runtime_free(mem_old);
  317. }
  318. return mem_new;
  319. }
  320. #define MEM_REALLOC(mem, size_old, size_new) \
  321. do { \
  322. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  323. error_buf_size); \
  324. if (!mem_new) \
  325. goto fail; \
  326. mem = mem_new; \
  327. } while (0)
  328. #if WASM_ENABLE_GC != 0
  329. static bool
  330. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  331. char *error_buf, uint32 error_buf_size)
  332. {
  333. if (type_index >= type_count) {
  334. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  335. type_index);
  336. return false;
  337. }
  338. return true;
  339. }
  340. static bool
  341. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  342. uint32 error_buf_size)
  343. {
  344. if (!check_type_index(module, module->type_count, type_index, error_buf,
  345. error_buf_size)) {
  346. return false;
  347. }
  348. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  349. set_error_buf(error_buf, error_buf_size, "unkown array type");
  350. return false;
  351. }
  352. return true;
  353. }
  354. #endif
  355. static bool
  356. check_function_index(const WASMModule *module, uint32 function_index,
  357. char *error_buf, uint32 error_buf_size)
  358. {
  359. if (function_index
  360. >= module->import_function_count + module->function_count) {
  361. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  362. function_index);
  363. return false;
  364. }
  365. return true;
  366. }
  367. typedef struct InitValue {
  368. uint8 type;
  369. uint8 flag;
  370. #if WASM_ENABLE_GC != 0
  371. uint8 gc_opcode;
  372. WASMRefType ref_type;
  373. #endif
  374. WASMValue value;
  375. } InitValue;
  376. typedef struct ConstExprContext {
  377. uint32 sp;
  378. uint32 size;
  379. WASMModule *module;
  380. InitValue *stack;
  381. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  382. } ConstExprContext;
  383. static void
  384. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  385. {
  386. ctx->sp = 0;
  387. ctx->module = module;
  388. ctx->stack = ctx->data;
  389. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  390. }
  391. static bool
  392. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  393. #if WASM_ENABLE_GC != 0
  394. WASMRefType *ref_type, uint8 gc_opcode,
  395. #endif
  396. WASMValue *value, char *error_buf, uint32 error_buf_size)
  397. {
  398. InitValue *cur_value;
  399. if (ctx->sp >= ctx->size) {
  400. if (ctx->stack != ctx->data) {
  401. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  402. (ctx->size + 4) * sizeof(InitValue));
  403. }
  404. else {
  405. if (!(ctx->stack =
  406. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  407. error_buf, error_buf_size))) {
  408. goto fail;
  409. }
  410. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  411. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  412. }
  413. ctx->size += 4;
  414. }
  415. cur_value = &ctx->stack[ctx->sp++];
  416. cur_value->type = type;
  417. cur_value->flag = flag;
  418. cur_value->value = *value;
  419. #if WASM_ENABLE_GC != 0
  420. cur_value->gc_opcode = gc_opcode;
  421. if (wasm_is_type_multi_byte_type(type)) {
  422. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  423. ref_type, wasm_reftype_struct_size(ref_type));
  424. }
  425. #endif
  426. return true;
  427. fail:
  428. return false;
  429. }
  430. #if WASM_ENABLE_GC != 0
  431. static void
  432. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  433. {
  434. WASMStructNewInitValues *struct_init_values =
  435. (WASMStructNewInitValues *)data;
  436. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  437. WASMType *wasm_type;
  438. uint32 i;
  439. if (!data)
  440. return;
  441. wasm_type = module->types[struct_init_values->type_idx];
  442. /* The data can only be type of `WASMStructNewInitValues *`
  443. or `WASMArrayNewInitValues *` */
  444. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  445. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  446. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  447. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  448. WASMRefTypeMap *ref_type_map = struct_type->ref_type_maps;
  449. WASMRefType *ref_type;
  450. uint8 field_type;
  451. for (i = 0; i < struct_init_values->count; i++) {
  452. field_type = struct_type->fields[i].field_type;
  453. if (wasm_is_type_multi_byte_type(field_type))
  454. ref_type = ref_type_map->ref_type;
  455. else
  456. ref_type = NULL;
  457. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  458. REF_TYPE_STRUCTREF, NULL,
  459. module->types, module->type_count)
  460. || wasm_reftype_is_subtype_of(
  461. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  462. module->types, module->type_count)) {
  463. destroy_init_expr_data_recursive(
  464. module, struct_init_values->fields[i].data);
  465. }
  466. }
  467. }
  468. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  469. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  470. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  471. uint8 elem_type = array_type->elem_type;
  472. for (i = 0; i < array_init_values->length; i++) {
  473. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  474. REF_TYPE_STRUCTREF, NULL,
  475. module->types, module->type_count)
  476. || wasm_reftype_is_subtype_of(
  477. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  478. module->types, module->type_count)) {
  479. destroy_init_expr_data_recursive(
  480. module, array_init_values->elem_data[i].data);
  481. }
  482. }
  483. }
  484. wasm_runtime_free(data);
  485. }
  486. #endif
  487. static bool
  488. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  489. #if WASM_ENABLE_GC != 0
  490. WASMRefType *ref_type, uint8 *p_gc_opcode,
  491. #endif
  492. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  493. {
  494. InitValue *cur_value;
  495. if (ctx->sp == 0) {
  496. set_error_buf(error_buf, error_buf_size,
  497. "type mismatch: const expr stack underflow");
  498. return false;
  499. }
  500. cur_value = &ctx->stack[--ctx->sp];
  501. #if WASM_ENABLE_GC == 0
  502. if (cur_value->type != type) {
  503. set_error_buf(error_buf, error_buf_size, "type mismatch");
  504. return false;
  505. }
  506. #else
  507. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  508. ref_type, ctx->module->types,
  509. ctx->module->type_count)) {
  510. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  511. "type mismatch: expect ", type2str(type),
  512. " but got other");
  513. goto fail;
  514. }
  515. #endif
  516. if (p_flag)
  517. *p_flag = cur_value->flag;
  518. if (p_value)
  519. *p_value = cur_value->value;
  520. #if WASM_ENABLE_GC != 0
  521. if (p_gc_opcode)
  522. *p_gc_opcode = cur_value->gc_opcode;
  523. #endif
  524. return true;
  525. #if WASM_ENABLE_GC != 0
  526. fail:
  527. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  528. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  529. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  530. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  531. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  532. }
  533. return false;
  534. #endif
  535. }
  536. static void
  537. destroy_const_expr_stack(ConstExprContext *ctx)
  538. {
  539. #if WASM_ENABLE_GC != 0
  540. uint32 i;
  541. for (i = 0; i < ctx->sp; i++) {
  542. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  543. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  544. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  545. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  546. destroy_init_expr_data_recursive(ctx->module,
  547. ctx->stack[i].value.data);
  548. }
  549. }
  550. #endif
  551. if (ctx->stack != ctx->data) {
  552. wasm_runtime_free(ctx->stack);
  553. }
  554. }
  555. #if WASM_ENABLE_GC != 0
  556. static void
  557. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  558. {
  559. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  560. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  561. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  562. destroy_init_expr_data_recursive(module, expr->u.data);
  563. }
  564. }
  565. #endif /* end of WASM_ENABLE_GC != 0 */
  566. static bool
  567. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  568. InitializerExpression *init_expr, uint8 type, void *ref_type,
  569. char *error_buf, uint32 error_buf_size)
  570. {
  571. const uint8 *p = *p_buf, *p_end = buf_end;
  572. uint8 flag, *p_float;
  573. uint32 i;
  574. ConstExprContext const_expr_ctx = { 0 };
  575. WASMValue cur_value;
  576. #if WASM_ENABLE_GC != 0
  577. uint32 opcode1, type_idx;
  578. uint8 opcode;
  579. WASMRefType cur_ref_type = { 0 };
  580. #endif
  581. init_const_expr_stack(&const_expr_ctx, module);
  582. CHECK_BUF(p, p_end, 1);
  583. flag = read_uint8(p);
  584. while (flag != WASM_OP_END) {
  585. switch (flag) {
  586. /* i32.const */
  587. case INIT_EXPR_TYPE_I32_CONST:
  588. read_leb_int32(p, p_end, cur_value.i32);
  589. if (!push_const_expr_stack(
  590. &const_expr_ctx, flag, VALUE_TYPE_I32,
  591. #if WASM_ENABLE_GC != 0
  592. NULL, 0,
  593. #endif
  594. &cur_value, error_buf, error_buf_size))
  595. goto fail;
  596. break;
  597. /* i64.const */
  598. case INIT_EXPR_TYPE_I64_CONST:
  599. read_leb_int64(p, p_end, cur_value.i64);
  600. if (!push_const_expr_stack(
  601. &const_expr_ctx, flag, VALUE_TYPE_I64,
  602. #if WASM_ENABLE_GC != 0
  603. NULL, 0,
  604. #endif
  605. &cur_value, error_buf, error_buf_size))
  606. goto fail;
  607. break;
  608. /* f32.const */
  609. case INIT_EXPR_TYPE_F32_CONST:
  610. CHECK_BUF(p, p_end, 4);
  611. p_float = (uint8 *)&cur_value.f32;
  612. for (i = 0; i < sizeof(float32); i++)
  613. *p_float++ = *p++;
  614. if (!push_const_expr_stack(
  615. &const_expr_ctx, flag, VALUE_TYPE_F32,
  616. #if WASM_ENABLE_GC != 0
  617. NULL, 0,
  618. #endif
  619. &cur_value, error_buf, error_buf_size))
  620. goto fail;
  621. break;
  622. /* f64.const */
  623. case INIT_EXPR_TYPE_F64_CONST:
  624. CHECK_BUF(p, p_end, 8);
  625. p_float = (uint8 *)&cur_value.f64;
  626. for (i = 0; i < sizeof(float64); i++)
  627. *p_float++ = *p++;
  628. if (!push_const_expr_stack(
  629. &const_expr_ctx, flag, VALUE_TYPE_F64,
  630. #if WASM_ENABLE_GC != 0
  631. NULL, 0,
  632. #endif
  633. &cur_value, error_buf, error_buf_size))
  634. goto fail;
  635. break;
  636. #if WASM_ENABLE_SIMD != 0
  637. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  638. /* v128.const */
  639. case INIT_EXPR_TYPE_V128_CONST:
  640. {
  641. uint64 high, low;
  642. CHECK_BUF(p, p_end, 1);
  643. (void)read_uint8(p);
  644. CHECK_BUF(p, p_end, 16);
  645. wasm_runtime_read_v128(p, &high, &low);
  646. p += 16;
  647. cur_value.v128.i64x2[0] = high;
  648. cur_value.v128.i64x2[1] = low;
  649. if (!push_const_expr_stack(
  650. &const_expr_ctx, flag, VALUE_TYPE_V128,
  651. #if WASM_ENABLE_GC != 0
  652. NULL, 0,
  653. #endif
  654. &cur_value, error_buf, error_buf_size))
  655. goto fail;
  656. #if WASM_ENABLE_WAMR_COMPILER != 0
  657. /* If any init_expr is v128.const, mark SIMD used */
  658. module->is_simd_used = true;
  659. #endif
  660. break;
  661. }
  662. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  663. #endif /* end of WASM_ENABLE_SIMD */
  664. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  665. /* ref.func */
  666. case INIT_EXPR_TYPE_FUNCREF_CONST:
  667. {
  668. uint32 func_idx;
  669. read_leb_uint32(p, p_end, func_idx);
  670. cur_value.ref_index = func_idx;
  671. if (!check_function_index(module, func_idx, error_buf,
  672. error_buf_size)) {
  673. goto fail;
  674. }
  675. #if WASM_ENABLE_GC == 0
  676. if (!push_const_expr_stack(&const_expr_ctx, flag,
  677. VALUE_TYPE_FUNCREF, &cur_value,
  678. error_buf, error_buf_size))
  679. goto fail;
  680. #else
  681. if (func_idx < module->import_function_count) {
  682. type_idx =
  683. module->import_functions[func_idx].u.function.type_idx;
  684. }
  685. else {
  686. type_idx = module
  687. ->functions[func_idx
  688. - module->import_function_count]
  689. ->type_idx;
  690. }
  691. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  692. false, type_idx);
  693. if (!push_const_expr_stack(&const_expr_ctx, flag,
  694. cur_ref_type.ref_type, &cur_ref_type,
  695. 0, &cur_value, error_buf,
  696. error_buf_size))
  697. goto fail;
  698. #endif
  699. #if WASM_ENABLE_WAMR_COMPILER != 0
  700. module->is_ref_types_used = true;
  701. #endif
  702. break;
  703. }
  704. /* ref.null */
  705. case INIT_EXPR_TYPE_REFNULL_CONST:
  706. {
  707. uint8 type1;
  708. CHECK_BUF(p, p_end, 1);
  709. type1 = read_uint8(p);
  710. #if WASM_ENABLE_GC == 0
  711. cur_value.ref_index = NULL_REF;
  712. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  713. &cur_value, error_buf,
  714. error_buf_size))
  715. goto fail;
  716. #else
  717. cur_value.gc_obj = NULL_REF;
  718. if (!is_byte_a_type(type1)) {
  719. p--;
  720. read_leb_uint32(p, p_end, type_idx);
  721. if (!check_type_index(module, module->type_count, type_idx,
  722. error_buf, error_buf_size))
  723. goto fail;
  724. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  725. true, type_idx);
  726. if (!push_const_expr_stack(&const_expr_ctx, flag,
  727. cur_ref_type.ref_type,
  728. &cur_ref_type, 0, &cur_value,
  729. error_buf, error_buf_size))
  730. goto fail;
  731. }
  732. else {
  733. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  734. NULL, 0, &cur_value, error_buf,
  735. error_buf_size))
  736. goto fail;
  737. }
  738. #endif
  739. #if WASM_ENABLE_WAMR_COMPILER != 0
  740. module->is_ref_types_used = true;
  741. #endif
  742. break;
  743. }
  744. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  745. /* get_global */
  746. case INIT_EXPR_TYPE_GET_GLOBAL:
  747. {
  748. uint32 global_idx;
  749. uint8 global_type;
  750. read_leb_uint32(p, p_end, cur_value.global_index);
  751. global_idx = cur_value.global_index;
  752. /*
  753. * Currently, constant expressions occurring as initializers
  754. * of globals are further constrained in that contained
  755. * global.get instructions are
  756. * only allowed to refer to imported globals.
  757. *
  758. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  759. */
  760. if (global_idx >= module->import_global_count
  761. /* make spec test happy */
  762. #if WASM_ENABLE_GC != 0
  763. + module->global_count
  764. #endif
  765. ) {
  766. set_error_buf_v(error_buf, error_buf_size,
  767. "unknown global %u", global_idx);
  768. goto fail;
  769. }
  770. if (
  771. /* make spec test happy */
  772. #if WASM_ENABLE_GC != 0
  773. global_idx < module->import_global_count &&
  774. #endif
  775. module->import_globals[global_idx]
  776. .u.global.type.is_mutable) {
  777. set_error_buf_v(error_buf, error_buf_size,
  778. "constant expression required");
  779. goto fail;
  780. }
  781. if (global_idx < module->import_global_count) {
  782. global_type = module->import_globals[global_idx]
  783. .u.global.type.val_type;
  784. #if WASM_ENABLE_GC != 0
  785. if (wasm_is_type_multi_byte_type(global_type)) {
  786. WASMRefType *global_ref_type =
  787. module->import_globals[global_idx]
  788. .u.global.ref_type;
  789. bh_memcpy_s(&cur_ref_type,
  790. wasm_reftype_struct_size(global_ref_type),
  791. global_ref_type,
  792. wasm_reftype_struct_size(global_ref_type));
  793. }
  794. #endif
  795. }
  796. else {
  797. global_type =
  798. module
  799. ->globals[global_idx - module->import_global_count]
  800. .type.val_type;
  801. #if WASM_ENABLE_GC != 0
  802. if (wasm_is_type_multi_byte_type(global_type)) {
  803. WASMRefType *global_ref_type =
  804. module
  805. ->globals[global_idx
  806. - module->import_global_count]
  807. .ref_type;
  808. bh_memcpy_s(&cur_ref_type,
  809. wasm_reftype_struct_size(global_ref_type),
  810. global_ref_type,
  811. wasm_reftype_struct_size(global_ref_type));
  812. }
  813. #endif
  814. }
  815. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  816. #if WASM_ENABLE_GC != 0
  817. &cur_ref_type, 0,
  818. #endif
  819. &cur_value, error_buf,
  820. error_buf_size))
  821. goto fail;
  822. break;
  823. }
  824. #if WASM_ENABLE_GC != 0
  825. /* struct.new and array.new */
  826. case WASM_OP_GC_PREFIX:
  827. {
  828. read_leb_uint32(p, p_end, opcode1);
  829. switch (opcode1) {
  830. case WASM_OP_STRUCT_NEW:
  831. {
  832. WASMStructType *struct_type;
  833. WASMStructNewInitValues *struct_init_values = NULL;
  834. uint32 field_count;
  835. read_leb_uint32(p, p_end, type_idx);
  836. if (!check_type_index(module, module->type_count,
  837. type_idx, error_buf,
  838. error_buf_size)) {
  839. goto fail;
  840. }
  841. struct_type = (WASMStructType *)module->types[type_idx];
  842. if (struct_type->base_type.type_flag
  843. != WASM_TYPE_STRUCT) {
  844. set_error_buf(error_buf, error_buf_size,
  845. "unkown struct type");
  846. goto fail;
  847. }
  848. field_count = struct_type->field_count;
  849. if (!(struct_init_values = loader_malloc(
  850. offsetof(WASMStructNewInitValues, fields)
  851. + (uint64)field_count * sizeof(WASMValue),
  852. error_buf, error_buf_size))) {
  853. goto fail;
  854. }
  855. struct_init_values->type_idx = type_idx;
  856. struct_init_values->count = field_count;
  857. for (i = field_count; i > 0; i--) {
  858. WASMRefType *field_ref_type = NULL;
  859. uint32 field_idx = i - 1;
  860. uint8 field_type =
  861. struct_type->fields[field_idx].field_type;
  862. if (wasm_is_type_multi_byte_type(field_type)) {
  863. field_ref_type = wasm_reftype_map_find(
  864. struct_type->ref_type_maps,
  865. struct_type->ref_type_map_count, field_idx);
  866. }
  867. if (is_packed_type(field_type)) {
  868. field_type = VALUE_TYPE_I32;
  869. }
  870. if (!pop_const_expr_stack(
  871. &const_expr_ctx, NULL, field_type,
  872. field_ref_type, NULL,
  873. &struct_init_values->fields[field_idx],
  874. error_buf, error_buf_size)) {
  875. destroy_init_expr_data_recursive(
  876. module, struct_init_values);
  877. goto fail;
  878. }
  879. }
  880. cur_value.data = struct_init_values;
  881. wasm_set_refheaptype_typeidx(
  882. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  883. if (!push_const_expr_stack(
  884. &const_expr_ctx, flag, cur_ref_type.ref_type,
  885. &cur_ref_type, (uint8)opcode1, &cur_value,
  886. error_buf, error_buf_size)) {
  887. destroy_init_expr_data_recursive(
  888. module, struct_init_values);
  889. goto fail;
  890. }
  891. break;
  892. }
  893. case WASM_OP_STRUCT_NEW_DEFAULT:
  894. {
  895. read_leb_uint32(p, p_end, cur_value.type_index);
  896. type_idx = cur_value.type_index;
  897. if (!check_type_index(module, module->type_count,
  898. type_idx, error_buf,
  899. error_buf_size)) {
  900. goto fail;
  901. }
  902. if (module->types[type_idx]->type_flag
  903. != WASM_TYPE_STRUCT) {
  904. set_error_buf(error_buf, error_buf_size,
  905. "unkown struct type");
  906. goto fail;
  907. }
  908. cur_value.type_index = type_idx;
  909. cur_value.data = NULL;
  910. wasm_set_refheaptype_typeidx(
  911. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  912. if (!push_const_expr_stack(
  913. &const_expr_ctx, flag, cur_ref_type.ref_type,
  914. &cur_ref_type, (uint8)opcode1, &cur_value,
  915. error_buf, error_buf_size)) {
  916. goto fail;
  917. }
  918. break;
  919. }
  920. case WASM_OP_ARRAY_NEW:
  921. case WASM_OP_ARRAY_NEW_DEFAULT:
  922. case WASM_OP_ARRAY_NEW_FIXED:
  923. {
  924. WASMArrayNewInitValues *array_init_values = NULL;
  925. WASMArrayType *array_type = NULL;
  926. WASMRefType *elem_ref_type = NULL;
  927. uint64 total_size;
  928. uint8 elem_type;
  929. read_leb_uint32(p, p_end, cur_value.type_index);
  930. type_idx = cur_value.type_index;
  931. if (!check_type_index(module, module->type_count,
  932. type_idx, error_buf,
  933. error_buf_size)) {
  934. goto fail;
  935. }
  936. array_type = (WASMArrayType *)module->types[type_idx];
  937. if (array_type->base_type.type_flag
  938. != WASM_TYPE_ARRAY) {
  939. set_error_buf(error_buf, error_buf_size,
  940. "unkown array type");
  941. goto fail;
  942. }
  943. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  944. elem_type = array_type->elem_type;
  945. if (wasm_is_type_multi_byte_type(elem_type)) {
  946. elem_ref_type = array_type->elem_ref_type;
  947. }
  948. if (is_packed_type(elem_type)) {
  949. elem_type = VALUE_TYPE_I32;
  950. }
  951. if (opcode1 == WASM_OP_ARRAY_NEW) {
  952. WASMValue len_val;
  953. if (!(array_init_values = loader_malloc(
  954. sizeof(WASMArrayNewInitValues),
  955. error_buf, error_buf_size))) {
  956. goto fail;
  957. }
  958. array_init_values->type_idx = type_idx;
  959. if (!pop_const_expr_stack(
  960. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  961. NULL, NULL, &len_val, error_buf,
  962. error_buf_size)) {
  963. destroy_init_expr_data_recursive(
  964. module, array_init_values);
  965. goto fail;
  966. }
  967. array_init_values->length = len_val.i32;
  968. if (!pop_const_expr_stack(
  969. &const_expr_ctx, NULL, elem_type,
  970. elem_ref_type, NULL,
  971. &array_init_values->elem_data[0],
  972. error_buf, error_buf_size)) {
  973. destroy_init_expr_data_recursive(
  974. module, array_init_values);
  975. goto fail;
  976. }
  977. cur_value.data = array_init_values;
  978. }
  979. else {
  980. /* WASM_OP_ARRAY_NEW_FIXED */
  981. uint32 len;
  982. read_leb_uint32(p, p_end, len);
  983. total_size =
  984. (uint64)offsetof(WASMArrayNewInitValues,
  985. elem_data)
  986. + (uint64)sizeof(WASMValue) * len;
  987. if (!(array_init_values =
  988. loader_malloc(total_size, error_buf,
  989. error_buf_size))) {
  990. goto fail;
  991. }
  992. array_init_values->type_idx = type_idx;
  993. array_init_values->length = len;
  994. for (i = len; i > 0; i--) {
  995. if (!pop_const_expr_stack(
  996. &const_expr_ctx, NULL, elem_type,
  997. elem_ref_type, NULL,
  998. &array_init_values
  999. ->elem_data[i - 1],
  1000. error_buf, error_buf_size)) {
  1001. destroy_init_expr_data_recursive(
  1002. module, array_init_values);
  1003. goto fail;
  1004. }
  1005. }
  1006. cur_value.data = array_init_values;
  1007. }
  1008. }
  1009. else {
  1010. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1011. WASMValue len_val;
  1012. uint32 len;
  1013. /* POP(i32) */
  1014. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1015. VALUE_TYPE_I32, NULL,
  1016. NULL, &len_val, error_buf,
  1017. error_buf_size)) {
  1018. goto fail;
  1019. }
  1020. len = len_val.i32;
  1021. cur_value.array_new_default.type_index = type_idx;
  1022. cur_value.array_new_default.length = len;
  1023. }
  1024. wasm_set_refheaptype_typeidx(
  1025. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1026. if (!push_const_expr_stack(
  1027. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1028. &cur_ref_type, (uint8)opcode1, &cur_value,
  1029. error_buf, error_buf_size)) {
  1030. if (array_init_values) {
  1031. destroy_init_expr_data_recursive(
  1032. module, array_init_values);
  1033. }
  1034. goto fail;
  1035. }
  1036. break;
  1037. }
  1038. case WASM_OP_ANY_CONVERT_EXTERN:
  1039. {
  1040. set_error_buf(error_buf, error_buf_size,
  1041. "unsupported constant expression of "
  1042. "extern.internalize");
  1043. goto fail;
  1044. }
  1045. case WASM_OP_EXTERN_CONVERT_ANY:
  1046. {
  1047. set_error_buf(error_buf, error_buf_size,
  1048. "unsupported constant expression of "
  1049. "extern.externalize");
  1050. goto fail;
  1051. }
  1052. case WASM_OP_REF_I31:
  1053. {
  1054. /* POP(i32) */
  1055. if (!pop_const_expr_stack(
  1056. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1057. NULL, &cur_value, error_buf, error_buf_size)) {
  1058. goto fail;
  1059. }
  1060. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1061. false, HEAP_TYPE_I31);
  1062. if (!push_const_expr_stack(
  1063. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1064. &cur_ref_type, (uint8)opcode1, &cur_value,
  1065. error_buf, error_buf_size)) {
  1066. goto fail;
  1067. }
  1068. break;
  1069. }
  1070. default:
  1071. set_error_buf(
  1072. error_buf, error_buf_size,
  1073. "type mismatch or constant expression required");
  1074. goto fail;
  1075. }
  1076. break;
  1077. }
  1078. #endif /* end of WASM_ENABLE_GC != 0 */
  1079. default:
  1080. {
  1081. set_error_buf(error_buf, error_buf_size,
  1082. "illegal opcode "
  1083. "or constant expression required "
  1084. "or type mismatch");
  1085. goto fail;
  1086. }
  1087. }
  1088. CHECK_BUF(p, p_end, 1);
  1089. flag = read_uint8(p);
  1090. }
  1091. /* There should be only one value left on the init value stack */
  1092. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1093. #if WASM_ENABLE_GC != 0
  1094. ref_type, &opcode,
  1095. #endif
  1096. &cur_value, error_buf, error_buf_size)) {
  1097. goto fail;
  1098. }
  1099. if (const_expr_ctx.sp != 0) {
  1100. set_error_buf(error_buf, error_buf_size,
  1101. "type mismatch: illegal constant opcode sequence");
  1102. goto fail;
  1103. }
  1104. init_expr->init_expr_type = flag;
  1105. init_expr->u = cur_value;
  1106. #if WASM_ENABLE_GC != 0
  1107. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1108. switch (opcode) {
  1109. case WASM_OP_STRUCT_NEW:
  1110. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1111. break;
  1112. case WASM_OP_STRUCT_NEW_DEFAULT:
  1113. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1114. break;
  1115. case WASM_OP_ARRAY_NEW:
  1116. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1117. break;
  1118. case WASM_OP_ARRAY_NEW_DEFAULT:
  1119. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1120. break;
  1121. case WASM_OP_ARRAY_NEW_FIXED:
  1122. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1123. break;
  1124. case WASM_OP_REF_I31:
  1125. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1126. break;
  1127. default:
  1128. bh_assert(0);
  1129. break;
  1130. }
  1131. }
  1132. #endif /* end of WASM_ENABLE_GC != 0 */
  1133. *p_buf = p;
  1134. destroy_const_expr_stack(&const_expr_ctx);
  1135. return true;
  1136. fail:
  1137. destroy_const_expr_stack(&const_expr_ctx);
  1138. return false;
  1139. }
  1140. static bool
  1141. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1142. {
  1143. if (mutable >= 2) {
  1144. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1145. return false;
  1146. }
  1147. return true;
  1148. }
  1149. #if WASM_ENABLE_GC != 0
  1150. static void
  1151. destroy_func_type(WASMFuncType *type)
  1152. {
  1153. /* Destroy the reference type hash set */
  1154. if (type->ref_type_maps)
  1155. wasm_runtime_free(type->ref_type_maps);
  1156. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1157. && WASM_ENABLE_LAZY_JIT != 0
  1158. if (type->call_to_llvm_jit_from_fast_jit)
  1159. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1160. #endif
  1161. /* Free the type */
  1162. wasm_runtime_free(type);
  1163. }
  1164. static void
  1165. destroy_struct_type(WASMStructType *type)
  1166. {
  1167. if (type->ref_type_maps)
  1168. wasm_runtime_free(type->ref_type_maps);
  1169. wasm_runtime_free(type);
  1170. }
  1171. static void
  1172. destroy_array_type(WASMArrayType *type)
  1173. {
  1174. wasm_runtime_free(type);
  1175. }
  1176. static void
  1177. destroy_wasm_type(WASMType *type)
  1178. {
  1179. if (type->ref_count > 1) {
  1180. /* The type is referenced by other types
  1181. of current wasm module */
  1182. type->ref_count--;
  1183. return;
  1184. }
  1185. if (type->type_flag == WASM_TYPE_FUNC)
  1186. destroy_func_type((WASMFuncType *)type);
  1187. else if (type->type_flag == WASM_TYPE_STRUCT)
  1188. destroy_struct_type((WASMStructType *)type);
  1189. else if (type->type_flag == WASM_TYPE_ARRAY)
  1190. destroy_array_type((WASMArrayType *)type);
  1191. else {
  1192. bh_assert(0);
  1193. }
  1194. }
  1195. /* Resolve (ref null ht) or (ref ht) */
  1196. static bool
  1197. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1198. WASMModule *module, uint32 type_count, bool nullable,
  1199. WASMRefType *ref_type, char *error_buf,
  1200. uint32 error_buf_size)
  1201. {
  1202. const uint8 *p = *p_buf, *p_end = buf_end;
  1203. ref_type->ref_type =
  1204. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1205. ref_type->ref_ht_common.nullable = nullable;
  1206. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1207. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1208. /* heap type is (type i), i : typeidx, >= 0 */
  1209. if (!check_type_index(module, type_count,
  1210. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1211. error_buf_size)) {
  1212. return false;
  1213. }
  1214. }
  1215. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1216. /* heap type is func, extern, any, eq, i31 or data */
  1217. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1218. return false;
  1219. }
  1220. *p_buf = p;
  1221. return true;
  1222. fail:
  1223. return false;
  1224. }
  1225. static bool
  1226. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1227. WASMModule *module, uint32 type_count,
  1228. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1229. bool allow_packed_type, char *error_buf,
  1230. uint32 error_buf_size)
  1231. {
  1232. const uint8 *p = *p_buf, *p_end = buf_end;
  1233. uint8 type;
  1234. memset(ref_type, 0, sizeof(WASMRefType));
  1235. CHECK_BUF(p, p_end, 1);
  1236. type = read_uint8(p);
  1237. if (wasm_is_reftype_htref_nullable(type)) {
  1238. /* (ref null ht) */
  1239. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1240. ref_type, error_buf, error_buf_size))
  1241. return false;
  1242. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1243. *p_need_ref_type_map = true;
  1244. else {
  1245. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1246. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1247. multi-byte type to one-byte type to reduce the footprint and
  1248. the complexity of type equal/subtype checking */
  1249. ref_type->ref_type =
  1250. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1251. *p_need_ref_type_map = false;
  1252. }
  1253. }
  1254. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1255. /* (ref ht) */
  1256. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1257. ref_type, error_buf, error_buf_size))
  1258. return false;
  1259. *p_need_ref_type_map = true;
  1260. #if WASM_ENABLE_STRINGREF != 0
  1261. /* covert (ref string) to stringref */
  1262. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1263. ref_type->ref_type =
  1264. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1265. *p_need_ref_type_map = false;
  1266. }
  1267. #endif
  1268. }
  1269. else {
  1270. /* type which can be represented by one byte */
  1271. if (!is_valid_value_type_for_interpreter(type)
  1272. && !(allow_packed_type && is_packed_type(type))) {
  1273. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1274. return false;
  1275. }
  1276. ref_type->ref_type = type;
  1277. *p_need_ref_type_map = false;
  1278. #if WASM_ENABLE_WAMR_COMPILER != 0
  1279. /* If any value's type is v128, mark the module as SIMD used */
  1280. if (type == VALUE_TYPE_V128)
  1281. module->is_simd_used = true;
  1282. #endif
  1283. }
  1284. *p_buf = p;
  1285. return true;
  1286. fail:
  1287. return false;
  1288. }
  1289. static WASMRefType *
  1290. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1291. char *error_buf, uint32 error_buf_size)
  1292. {
  1293. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1294. if (!ret) {
  1295. set_error_buf(error_buf, error_buf_size,
  1296. "insert ref type to hash set failed");
  1297. }
  1298. return ret;
  1299. }
  1300. static bool
  1301. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1302. uint32 type_count, uint32 type_idx, char *error_buf,
  1303. uint32 error_buf_size)
  1304. {
  1305. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1306. uint32 param_count, result_count, i, j = 0;
  1307. uint32 param_cell_num, ret_cell_num;
  1308. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1309. uint64 total_size;
  1310. bool need_ref_type_map;
  1311. WASMRefType ref_type;
  1312. WASMFuncType *type = NULL;
  1313. /* Parse first time to resolve param count, result count and
  1314. ref type map count */
  1315. read_leb_uint32(p, p_end, param_count);
  1316. p_org = p;
  1317. for (i = 0; i < param_count; i++) {
  1318. if (!resolve_value_type(&p, p_end, module, type_count,
  1319. &need_ref_type_map, &ref_type, false, error_buf,
  1320. error_buf_size)) {
  1321. return false;
  1322. }
  1323. if (need_ref_type_map)
  1324. ref_type_map_count++;
  1325. }
  1326. read_leb_uint32(p, p_end, result_count);
  1327. for (i = 0; i < result_count; i++) {
  1328. if (!resolve_value_type(&p, p_end, module, type_count,
  1329. &need_ref_type_map, &ref_type, false, error_buf,
  1330. error_buf_size)) {
  1331. return false;
  1332. }
  1333. if (need_ref_type_map) {
  1334. ref_type_map_count++;
  1335. result_ref_type_map_count++;
  1336. }
  1337. }
  1338. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1339. "ref type map count: %d",
  1340. type_idx, param_count, result_count, ref_type_map_count);
  1341. /* Parse second time to resolve param types, result types and
  1342. ref type map info */
  1343. p = p_org;
  1344. total_size = offsetof(WASMFuncType, types)
  1345. + sizeof(uint8) * (uint64)(param_count + result_count);
  1346. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1347. return false;
  1348. }
  1349. if (ref_type_map_count > 0) {
  1350. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1351. if (!(type->ref_type_maps =
  1352. loader_malloc(total_size, error_buf, error_buf_size))) {
  1353. goto fail;
  1354. }
  1355. }
  1356. type->base_type.type_flag = WASM_TYPE_FUNC;
  1357. type->param_count = param_count;
  1358. type->result_count = result_count;
  1359. type->ref_type_map_count = ref_type_map_count;
  1360. if (ref_type_map_count > 0) {
  1361. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1362. - result_ref_type_map_count;
  1363. }
  1364. for (i = 0; i < param_count; i++) {
  1365. if (!resolve_value_type(&p, p_end, module, type_count,
  1366. &need_ref_type_map, &ref_type, false, error_buf,
  1367. error_buf_size)) {
  1368. goto fail;
  1369. }
  1370. type->types[i] = ref_type.ref_type;
  1371. if (need_ref_type_map) {
  1372. type->ref_type_maps[j].index = i;
  1373. if (!(type->ref_type_maps[j++].ref_type =
  1374. reftype_set_insert(module->ref_type_set, &ref_type,
  1375. error_buf, error_buf_size))) {
  1376. goto fail;
  1377. }
  1378. }
  1379. }
  1380. read_leb_uint32(p, p_end, result_count);
  1381. for (i = 0; i < result_count; i++) {
  1382. if (!resolve_value_type(&p, p_end, module, type_count,
  1383. &need_ref_type_map, &ref_type, false, error_buf,
  1384. error_buf_size)) {
  1385. goto fail;
  1386. }
  1387. type->types[param_count + i] = ref_type.ref_type;
  1388. if (need_ref_type_map) {
  1389. type->ref_type_maps[j].index = param_count + i;
  1390. if (!(type->ref_type_maps[j++].ref_type =
  1391. reftype_set_insert(module->ref_type_set, &ref_type,
  1392. error_buf, error_buf_size))) {
  1393. goto fail;
  1394. }
  1395. }
  1396. }
  1397. bh_assert(j == type->ref_type_map_count);
  1398. #if TRACE_WASM_LOADER != 0
  1399. os_printf("type %d = ", type_idx);
  1400. wasm_dump_func_type(type);
  1401. #endif
  1402. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1403. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1404. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1405. set_error_buf(error_buf, error_buf_size,
  1406. "param count or result count too large");
  1407. goto fail;
  1408. }
  1409. type->param_cell_num = (uint16)param_cell_num;
  1410. type->ret_cell_num = (uint16)ret_cell_num;
  1411. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1412. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1413. #endif
  1414. #if WASM_ENABLE_WAMR_COMPILER != 0
  1415. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1416. if (type->types[i] == VALUE_TYPE_V128)
  1417. module->is_simd_used = true;
  1418. }
  1419. #endif
  1420. *p_buf = p;
  1421. module->types[type_idx] = (WASMType *)type;
  1422. return true;
  1423. fail:
  1424. if (type)
  1425. destroy_func_type(type);
  1426. return false;
  1427. }
  1428. static bool
  1429. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1430. WASMModule *module, uint32 type_count, uint32 type_idx,
  1431. char *error_buf, uint32 error_buf_size)
  1432. {
  1433. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1434. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1435. uint32 i, j = 0, offset;
  1436. uint16 *reference_table;
  1437. uint64 total_size;
  1438. uint8 mutable;
  1439. bool need_ref_type_map;
  1440. WASMRefType ref_type;
  1441. WASMStructType *type = NULL;
  1442. /* Parse first time to resolve field count and ref type map count */
  1443. read_leb_uint32(p, p_end, field_count);
  1444. p_org = p;
  1445. for (i = 0; i < field_count; i++) {
  1446. if (!resolve_value_type(&p, p_end, module, type_count,
  1447. &need_ref_type_map, &ref_type, true, error_buf,
  1448. error_buf_size)) {
  1449. return false;
  1450. }
  1451. if (need_ref_type_map)
  1452. ref_type_map_count++;
  1453. if (wasm_is_type_reftype(ref_type.ref_type))
  1454. ref_field_count++;
  1455. CHECK_BUF(p, p_end, 1);
  1456. mutable = read_uint8(p);
  1457. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1458. return false;
  1459. }
  1460. }
  1461. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1462. type_idx, field_count, ref_type_map_count);
  1463. /* Parse second time to resolve field types and ref type map info */
  1464. p = p_org;
  1465. total_size = offsetof(WASMStructType, fields)
  1466. + sizeof(WASMStructFieldType) * (uint64)field_count
  1467. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1468. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1469. return false;
  1470. }
  1471. if (ref_type_map_count > 0) {
  1472. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1473. if (!(type->ref_type_maps =
  1474. loader_malloc(total_size, error_buf, error_buf_size))) {
  1475. goto fail;
  1476. }
  1477. }
  1478. type->reference_table = reference_table =
  1479. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1480. + sizeof(WASMStructFieldType) * field_count);
  1481. *reference_table++ = ref_field_count;
  1482. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1483. type->field_count = field_count;
  1484. type->ref_type_map_count = ref_type_map_count;
  1485. offset = (uint32)offsetof(WASMStructObject, field_data);
  1486. for (i = 0; i < field_count; i++) {
  1487. if (!resolve_value_type(&p, p_end, module, type_count,
  1488. &need_ref_type_map, &ref_type, true, error_buf,
  1489. error_buf_size)) {
  1490. goto fail;
  1491. }
  1492. type->fields[i].field_type = ref_type.ref_type;
  1493. if (need_ref_type_map) {
  1494. type->ref_type_maps[j].index = i;
  1495. if (!(type->ref_type_maps[j++].ref_type =
  1496. reftype_set_insert(module->ref_type_set, &ref_type,
  1497. error_buf, error_buf_size))) {
  1498. goto fail;
  1499. }
  1500. }
  1501. CHECK_BUF(p, p_end, 1);
  1502. type->fields[i].field_flags = read_uint8(p);
  1503. type->fields[i].field_size =
  1504. (uint8)wasm_reftype_size(ref_type.ref_type);
  1505. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1506. || defined(BUILD_TARGET_X86_32))
  1507. if (type->fields[i].field_size == 2)
  1508. offset = align_uint(offset, 2);
  1509. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1510. offset = align_uint(offset, 4);
  1511. #endif
  1512. type->fields[i].field_offset = offset;
  1513. if (wasm_is_type_reftype(ref_type.ref_type))
  1514. *reference_table++ = offset;
  1515. offset += type->fields[i].field_size;
  1516. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1517. type->fields[i].field_flags, type->fields[i].field_type);
  1518. }
  1519. type->total_size = offset;
  1520. bh_assert(j == type->ref_type_map_count);
  1521. #if TRACE_WASM_LOADER != 0
  1522. os_printf("type %d = ", type_idx);
  1523. wasm_dump_struct_type(type);
  1524. #endif
  1525. *p_buf = p;
  1526. module->types[type_idx] = (WASMType *)type;
  1527. return true;
  1528. fail:
  1529. if (type)
  1530. destroy_struct_type(type);
  1531. return false;
  1532. }
  1533. static bool
  1534. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1535. WASMModule *module, uint32 type_count, uint32 type_idx,
  1536. char *error_buf, uint32 error_buf_size)
  1537. {
  1538. const uint8 *p = *p_buf, *p_end = buf_end;
  1539. uint8 mutable;
  1540. bool need_ref_type_map;
  1541. WASMRefType ref_type;
  1542. WASMArrayType *type = NULL;
  1543. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1544. &ref_type, true, error_buf, error_buf_size)) {
  1545. return false;
  1546. }
  1547. CHECK_BUF(p, p_end, 1);
  1548. mutable = read_uint8(p);
  1549. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1550. return false;
  1551. }
  1552. LOG_VERBOSE("type %u: array", type_idx);
  1553. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1554. error_buf_size))) {
  1555. return false;
  1556. }
  1557. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1558. type->elem_flags = mutable;
  1559. type->elem_type = ref_type.ref_type;
  1560. if (need_ref_type_map) {
  1561. if (!(type->elem_ref_type =
  1562. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1563. error_buf_size))) {
  1564. goto fail;
  1565. }
  1566. }
  1567. #if TRACE_WASM_LOADER != 0
  1568. os_printf("type %d = ", type_idx);
  1569. wasm_dump_array_type(type);
  1570. #endif
  1571. *p_buf = p;
  1572. module->types[type_idx] = (WASMType *)type;
  1573. return true;
  1574. fail:
  1575. if (type)
  1576. destroy_array_type(type);
  1577. return false;
  1578. }
  1579. static bool
  1580. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1581. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1582. {
  1583. if (heap_type >= 0) {
  1584. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1585. error_buf_size)) {
  1586. return false;
  1587. }
  1588. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1589. heap_type);
  1590. }
  1591. else {
  1592. if (!wasm_is_valid_heap_type(heap_type)) {
  1593. set_error_buf(error_buf, error_buf_size, "unknown type");
  1594. return false;
  1595. }
  1596. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1597. heap_type);
  1598. if (nullable) {
  1599. /* For (ref null func/extern/any/eq/i31/data),
  1600. they are same as
  1601. funcref/externref/anyref/eqref/i31ref/dataref,
  1602. we convert the multi-byte type to one-byte
  1603. type to reduce the footprint and the
  1604. complexity of type equal/subtype checking */
  1605. ref_type->ref_type =
  1606. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1607. }
  1608. }
  1609. return true;
  1610. }
  1611. static void
  1612. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1613. WASMRefType *ref_type2)
  1614. {
  1615. /**
  1616. * The difference rt1 ∖ rt2 between two reference types is defined as
  1617. * follows:
  1618. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1619. * (ref ht2) = (ref null?1 ht1)
  1620. */
  1621. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1622. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1623. ref_type1, wasm_reftype_struct_size(ref_type1));
  1624. }
  1625. else {
  1626. ref_type_diff->ref_type = ref_type1->ref_type;
  1627. }
  1628. if (ref_type2->ref_ht_common.nullable) {
  1629. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1630. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1631. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1632. (int32)ref_type_diff->ref_type - 0x80);
  1633. }
  1634. else {
  1635. ref_type_diff->ref_ht_typeidx.nullable = false;
  1636. }
  1637. }
  1638. }
  1639. #else /* else of WASM_ENABLE_GC != 0 */
  1640. static void
  1641. destroy_wasm_type(WASMType *type)
  1642. {
  1643. if (type->ref_count > 1) {
  1644. /* The type is referenced by other types
  1645. of current wasm module */
  1646. type->ref_count--;
  1647. return;
  1648. }
  1649. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1650. && WASM_ENABLE_LAZY_JIT != 0
  1651. if (type->call_to_llvm_jit_from_fast_jit)
  1652. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1653. #endif
  1654. wasm_runtime_free(type);
  1655. }
  1656. #endif /* end of WASM_ENABLE_GC != 0 */
  1657. static bool
  1658. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1659. char *error_buf, uint32 error_buf_size)
  1660. {
  1661. const uint8 *p = buf, *p_end = buf_end;
  1662. uint32 type_count, i;
  1663. uint64 total_size;
  1664. uint8 flag;
  1665. #if WASM_ENABLE_GC != 0
  1666. uint32 processed_type_count = 0;
  1667. #endif
  1668. read_leb_uint32(p, p_end, type_count);
  1669. if (type_count) {
  1670. module->type_count = type_count;
  1671. total_size = sizeof(WASMType *) * (uint64)type_count;
  1672. if (!(module->types =
  1673. loader_malloc(total_size, error_buf, error_buf_size))) {
  1674. return false;
  1675. }
  1676. #if WASM_ENABLE_GC == 0
  1677. for (i = 0; i < type_count; i++) {
  1678. WASMFuncType *type;
  1679. const uint8 *p_org;
  1680. uint32 param_count, result_count, j;
  1681. uint32 param_cell_num, ret_cell_num;
  1682. CHECK_BUF(p, p_end, 1);
  1683. flag = read_uint8(p);
  1684. if (flag != 0x60) {
  1685. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1686. return false;
  1687. }
  1688. read_leb_uint32(p, p_end, param_count);
  1689. /* Resolve param count and result count firstly */
  1690. p_org = p;
  1691. CHECK_BUF(p, p_end, param_count);
  1692. p += param_count;
  1693. read_leb_uint32(p, p_end, result_count);
  1694. CHECK_BUF(p, p_end, result_count);
  1695. p = p_org;
  1696. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1697. set_error_buf(error_buf, error_buf_size,
  1698. "param count or result count too large");
  1699. return false;
  1700. }
  1701. total_size = offsetof(WASMFuncType, types)
  1702. + sizeof(uint8) * (uint64)(param_count + result_count);
  1703. if (!(type = module->types[i] =
  1704. loader_malloc(total_size, error_buf, error_buf_size))) {
  1705. return false;
  1706. }
  1707. /* Resolve param types and result types */
  1708. type->ref_count = 1;
  1709. type->param_count = (uint16)param_count;
  1710. type->result_count = (uint16)result_count;
  1711. for (j = 0; j < param_count; j++) {
  1712. CHECK_BUF(p, p_end, 1);
  1713. type->types[j] = read_uint8(p);
  1714. }
  1715. read_leb_uint32(p, p_end, result_count);
  1716. for (j = 0; j < result_count; j++) {
  1717. CHECK_BUF(p, p_end, 1);
  1718. type->types[param_count + j] = read_uint8(p);
  1719. }
  1720. for (j = 0; j < param_count + result_count; j++) {
  1721. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  1722. set_error_buf(error_buf, error_buf_size,
  1723. "unknown value type");
  1724. return false;
  1725. }
  1726. }
  1727. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1728. ret_cell_num =
  1729. wasm_get_cell_num(type->types + param_count, result_count);
  1730. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1731. set_error_buf(error_buf, error_buf_size,
  1732. "param count or result count too large");
  1733. return false;
  1734. }
  1735. type->param_cell_num = (uint16)param_cell_num;
  1736. type->ret_cell_num = (uint16)ret_cell_num;
  1737. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1738. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1739. #endif
  1740. #if WASM_ENABLE_WAMR_COMPILER != 0
  1741. for (j = 0; j < type->param_count + type->result_count; j++) {
  1742. if (type->types[j] == VALUE_TYPE_V128)
  1743. module->is_simd_used = true;
  1744. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1745. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1746. module->is_ref_types_used = true;
  1747. }
  1748. #endif
  1749. /* If there is already a same type created, use it instead */
  1750. for (j = 0; j < i; j++) {
  1751. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1752. if (module->types[j]->ref_count == UINT16_MAX) {
  1753. set_error_buf(error_buf, error_buf_size,
  1754. "wasm type's ref count too large");
  1755. return false;
  1756. }
  1757. destroy_wasm_type(type);
  1758. module->types[i] = module->types[j];
  1759. module->types[j]->ref_count++;
  1760. break;
  1761. }
  1762. }
  1763. }
  1764. #else /* else of WASM_ENABLE_GC == 0 */
  1765. for (i = 0; i < type_count; i++) {
  1766. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1767. uint32 rec_count = 1, j;
  1768. bool is_sub_final = true;
  1769. CHECK_BUF(p, p_end, 1);
  1770. flag = read_uint8(p);
  1771. if (flag == DEFINED_TYPE_REC) {
  1772. read_leb_uint32(p, p_end, rec_count);
  1773. if (rec_count > 1) {
  1774. uint64 new_total_size;
  1775. /* integer overflow */
  1776. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1777. set_error_buf(error_buf, error_buf_size,
  1778. "recursive type count too large");
  1779. return false;
  1780. }
  1781. module->type_count += rec_count - 1;
  1782. new_total_size =
  1783. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1784. if (new_total_size > UINT32_MAX) {
  1785. set_error_buf(error_buf, error_buf_size,
  1786. "allocate memory failed");
  1787. return false;
  1788. }
  1789. MEM_REALLOC(module->types, (uint32)total_size,
  1790. (uint32)new_total_size);
  1791. total_size = new_total_size;
  1792. }
  1793. LOG_VERBOSE("Processing rec group [%d-%d]",
  1794. processed_type_count,
  1795. processed_type_count + rec_count - 1);
  1796. }
  1797. else {
  1798. p--;
  1799. }
  1800. for (j = 0; j < rec_count; j++) {
  1801. WASMType *cur_type = NULL;
  1802. CHECK_BUF(p, p_end, 1);
  1803. flag = read_uint8(p);
  1804. parent_type_idx = -1;
  1805. if (flag == DEFINED_TYPE_SUB
  1806. || flag == DEFINED_TYPE_SUB_FINAL) {
  1807. read_leb_uint32(p, p_end, super_type_count);
  1808. if (super_type_count > 1) {
  1809. set_error_buf(error_buf, error_buf_size,
  1810. "super type count too large");
  1811. return false;
  1812. }
  1813. if (super_type_count > 0) {
  1814. read_leb_uint32(p, p_end, parent_type_idx);
  1815. if (parent_type_idx >= processed_type_count + j) {
  1816. set_error_buf_v(error_buf, error_buf_size,
  1817. "unknown type %d", parent_type_idx);
  1818. return false;
  1819. }
  1820. if (module->types[parent_type_idx]->is_sub_final) {
  1821. set_error_buf(error_buf, error_buf_size,
  1822. "sub type can not inherit from "
  1823. "a final super type");
  1824. return false;
  1825. }
  1826. }
  1827. if (flag == DEFINED_TYPE_SUB)
  1828. is_sub_final = false;
  1829. CHECK_BUF(p, p_end, 1);
  1830. flag = read_uint8(p);
  1831. }
  1832. if (flag == DEFINED_TYPE_FUNC) {
  1833. if (!resolve_func_type(&p, buf_end, module,
  1834. processed_type_count + rec_count,
  1835. processed_type_count + j, error_buf,
  1836. error_buf_size)) {
  1837. return false;
  1838. }
  1839. }
  1840. else if (flag == DEFINED_TYPE_STRUCT) {
  1841. if (!resolve_struct_type(&p, buf_end, module,
  1842. processed_type_count + rec_count,
  1843. processed_type_count + j,
  1844. error_buf, error_buf_size)) {
  1845. return false;
  1846. }
  1847. }
  1848. else if (flag == DEFINED_TYPE_ARRAY) {
  1849. if (!resolve_array_type(&p, buf_end, module,
  1850. processed_type_count + rec_count,
  1851. processed_type_count + j, error_buf,
  1852. error_buf_size)) {
  1853. return false;
  1854. }
  1855. }
  1856. else {
  1857. set_error_buf(error_buf, error_buf_size,
  1858. "invalid type flag");
  1859. return false;
  1860. }
  1861. cur_type = module->types[processed_type_count + j];
  1862. cur_type->ref_count = 1;
  1863. cur_type->parent_type_idx = parent_type_idx;
  1864. cur_type->is_sub_final = is_sub_final;
  1865. cur_type->rec_count = rec_count;
  1866. cur_type->rec_idx = j;
  1867. cur_type->rec_begin_type_idx = processed_type_count;
  1868. }
  1869. /* resolve subtyping relationship in current rec group */
  1870. for (j = 0; j < rec_count; j++) {
  1871. WASMType *cur_type = module->types[processed_type_count + j];
  1872. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1873. WASMType *parent_type =
  1874. module->types[cur_type->parent_type_idx];
  1875. cur_type->parent_type = parent_type;
  1876. cur_type->root_type = parent_type->root_type;
  1877. if (parent_type->inherit_depth == UINT16_MAX) {
  1878. set_error_buf(error_buf, error_buf_size,
  1879. "parent type's inherit depth too large");
  1880. return false;
  1881. }
  1882. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1883. }
  1884. else {
  1885. cur_type->parent_type = NULL;
  1886. cur_type->root_type = cur_type;
  1887. cur_type->inherit_depth = 0;
  1888. }
  1889. }
  1890. for (j = 0; j < rec_count; j++) {
  1891. WASMType *cur_type = module->types[processed_type_count + j];
  1892. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1893. WASMType *parent_type =
  1894. module->types[cur_type->parent_type_idx];
  1895. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1896. module->types,
  1897. module->type_count)) {
  1898. set_error_buf(error_buf, error_buf_size,
  1899. "sub type does not match super type");
  1900. return false;
  1901. }
  1902. }
  1903. }
  1904. /* If there is already an equivalence type or a group of equivalence
  1905. recursive types created, use it or them instead */
  1906. for (j = 0; j < processed_type_count;) {
  1907. WASMType *src_type = module->types[j];
  1908. WASMType *cur_type = module->types[processed_type_count];
  1909. uint32 k, src_rec_count;
  1910. src_rec_count = src_type->rec_count;
  1911. if (src_rec_count != rec_count) {
  1912. /* no type equivalence */
  1913. j += src_rec_count;
  1914. continue;
  1915. }
  1916. for (k = 0; k < rec_count; k++) {
  1917. src_type = module->types[j + k];
  1918. cur_type = module->types[processed_type_count + k];
  1919. if (!wasm_type_equal(src_type, cur_type, module->types,
  1920. module->type_count)) {
  1921. break;
  1922. }
  1923. }
  1924. if (k < rec_count) {
  1925. /* no type equivalence */
  1926. j += src_rec_count;
  1927. continue;
  1928. }
  1929. /* type equivalence */
  1930. for (k = 0; k < rec_count; k++) {
  1931. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1932. set_error_buf(error_buf, error_buf_size,
  1933. "wasm type's ref count too large");
  1934. return false;
  1935. }
  1936. destroy_wasm_type(module->types[processed_type_count + k]);
  1937. module->types[processed_type_count + k] =
  1938. module->types[j + k];
  1939. module->types[j + k]->ref_count++;
  1940. }
  1941. break;
  1942. }
  1943. if (rec_count > 1) {
  1944. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1945. processed_type_count,
  1946. processed_type_count + rec_count - 1);
  1947. }
  1948. processed_type_count += rec_count;
  1949. }
  1950. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1951. * module->type_count,
  1952. error_buf, error_buf_size))) {
  1953. return false;
  1954. }
  1955. #endif /* end of WASM_ENABLE_GC == 0 */
  1956. }
  1957. if (p != p_end) {
  1958. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1959. return false;
  1960. }
  1961. LOG_VERBOSE("Load type section success.\n");
  1962. return true;
  1963. fail:
  1964. return false;
  1965. }
  1966. static void
  1967. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1968. {
  1969. uint32 default_max_size;
  1970. if (UINT32_MAX / 2 > init_size)
  1971. default_max_size = init_size * 2;
  1972. else
  1973. default_max_size = UINT32_MAX;
  1974. if (default_max_size < WASM_TABLE_MAX_SIZE)
  1975. default_max_size = WASM_TABLE_MAX_SIZE;
  1976. if (max_size_flag) {
  1977. /* module defines the table limitation */
  1978. bh_assert(init_size <= *max_size);
  1979. if (init_size < *max_size) {
  1980. *max_size =
  1981. *max_size < default_max_size ? *max_size : default_max_size;
  1982. }
  1983. }
  1984. else {
  1985. /* partial defined table limitation, gives a default value */
  1986. *max_size = default_max_size;
  1987. }
  1988. }
  1989. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  1990. /**
  1991. * Find export item of a module with export info:
  1992. * module name, field name and export kind
  1993. */
  1994. static WASMExport *
  1995. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  1996. const char *field_name, uint8 export_kind,
  1997. char *error_buf, uint32 error_buf_size)
  1998. {
  1999. WASMExport *export =
  2000. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2001. export_kind, error_buf, error_buf_size);
  2002. return export;
  2003. }
  2004. #endif
  2005. #if WASM_ENABLE_MULTI_MODULE != 0
  2006. static WASMTable *
  2007. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2008. uint32 init_size, uint32 max_size, char *error_buf,
  2009. uint32 error_buf_size)
  2010. {
  2011. WASMModuleCommon *module_reg;
  2012. WASMTable *table = NULL;
  2013. WASMExport *export = NULL;
  2014. WASMModule *module = NULL;
  2015. module_reg = wasm_runtime_find_module_registered(module_name);
  2016. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2017. LOG_DEBUG("can not find a module named %s for table", module_name);
  2018. set_error_buf(error_buf, error_buf_size, "unknown import");
  2019. return NULL;
  2020. }
  2021. module = (WASMModule *)module_reg;
  2022. export =
  2023. wasm_loader_find_export(module, module_name, table_name,
  2024. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2025. if (!export) {
  2026. return NULL;
  2027. }
  2028. /* resolve table and check the init/max size */
  2029. if (export->index < module->import_table_count) {
  2030. table =
  2031. module->import_tables[export->index].u.table.import_table_linked;
  2032. }
  2033. else {
  2034. table = &(module->tables[export->index - module->import_table_count]);
  2035. }
  2036. if (table->table_type.init_size < init_size
  2037. || table->table_type.max_size > max_size) {
  2038. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2039. module_name, table_name, table->table_type.init_size,
  2040. table->table_type.max_size, init_size, max_size);
  2041. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2042. return NULL;
  2043. }
  2044. return table;
  2045. }
  2046. static WASMMemory *
  2047. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2048. uint32 init_page_count, uint32 max_page_count,
  2049. char *error_buf, uint32 error_buf_size)
  2050. {
  2051. WASMModuleCommon *module_reg;
  2052. WASMMemory *memory = NULL;
  2053. WASMExport *export = NULL;
  2054. WASMModule *module = NULL;
  2055. module_reg = wasm_runtime_find_module_registered(module_name);
  2056. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2057. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2058. set_error_buf(error_buf, error_buf_size, "unknown import");
  2059. return NULL;
  2060. }
  2061. module = (WASMModule *)module_reg;
  2062. export =
  2063. wasm_loader_find_export(module, module_name, memory_name,
  2064. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2065. if (!export) {
  2066. return NULL;
  2067. }
  2068. /* resolve memory and check the init/max page count */
  2069. if (export->index < module->import_memory_count) {
  2070. memory = module->import_memories[export->index]
  2071. .u.memory.import_memory_linked;
  2072. }
  2073. else {
  2074. memory =
  2075. &(module->memories[export->index - module->import_memory_count]);
  2076. }
  2077. if (memory->init_page_count < init_page_count
  2078. || memory->max_page_count > max_page_count) {
  2079. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2080. module_name, memory_name, memory->init_page_count,
  2081. memory->max_page_count, init_page_count, max_page_count);
  2082. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2083. return NULL;
  2084. }
  2085. return memory;
  2086. }
  2087. static WASMGlobal *
  2088. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2089. uint8 type, bool is_mutable, char *error_buf,
  2090. uint32 error_buf_size)
  2091. {
  2092. WASMModuleCommon *module_reg;
  2093. WASMGlobal *global = NULL;
  2094. WASMExport *export = NULL;
  2095. WASMModule *module = NULL;
  2096. module_reg = wasm_runtime_find_module_registered(module_name);
  2097. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2098. LOG_DEBUG("can not find a module named %s for global", module_name);
  2099. set_error_buf(error_buf, error_buf_size, "unknown import");
  2100. return NULL;
  2101. }
  2102. module = (WASMModule *)module_reg;
  2103. export =
  2104. wasm_loader_find_export(module, module_name, global_name,
  2105. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2106. if (!export) {
  2107. return NULL;
  2108. }
  2109. /* resolve and check the global */
  2110. if (export->index < module->import_global_count) {
  2111. global =
  2112. module->import_globals[export->index].u.global.import_global_linked;
  2113. }
  2114. else {
  2115. global =
  2116. &(module->globals[export->index - module->import_global_count]);
  2117. }
  2118. if (global->type.val_type != type
  2119. || global->type.is_mutable != is_mutable) {
  2120. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2121. module_name, global_name, global->type.val_type,
  2122. global->type.is_mutable, type, is_mutable);
  2123. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2124. return NULL;
  2125. }
  2126. return global;
  2127. }
  2128. #if WASM_ENABLE_TAGS != 0
  2129. static WASMTag *
  2130. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2131. const WASMType *expected_tag_type,
  2132. uint32 *linked_tag_index, char *error_buf,
  2133. uint32 error_buf_size)
  2134. {
  2135. WASMModuleCommon *module_reg;
  2136. WASMTag *tag = NULL;
  2137. WASMExport *export = NULL;
  2138. WASMModule *module = NULL;
  2139. module_reg = wasm_runtime_find_module_registered(module_name);
  2140. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2141. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2142. tag_name);
  2143. set_error_buf(error_buf, error_buf_size, "unknown import");
  2144. return NULL;
  2145. }
  2146. module = (WASMModule *)module_reg;
  2147. export =
  2148. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2149. error_buf, error_buf_size);
  2150. if (!export) {
  2151. return NULL;
  2152. }
  2153. /* resolve tag type and tag */
  2154. if (export->index < module->import_tag_count) {
  2155. /* importing an imported tag from the submodule */
  2156. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2157. }
  2158. else {
  2159. /* importing an section tag from the submodule */
  2160. tag = module->tags[export->index - module->import_tag_count];
  2161. }
  2162. /* check function type */
  2163. if (!wasm_type_equal(expected_tag_type, tag->tag_type, module->types,
  2164. module->type_count)) {
  2165. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2166. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2167. return NULL;
  2168. }
  2169. if (linked_tag_index != NULL) {
  2170. *linked_tag_index = export->index;
  2171. }
  2172. return tag;
  2173. }
  2174. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2175. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2176. static bool
  2177. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2178. const WASMModule *parent_module,
  2179. const char *sub_module_name, const char *function_name,
  2180. WASMFunctionImport *function, bool no_resolve,
  2181. char *error_buf, uint32 error_buf_size)
  2182. {
  2183. const uint8 *p = *p_buf, *p_end = buf_end;
  2184. uint32 declare_type_index = 0;
  2185. read_leb_uint32(p, p_end, declare_type_index);
  2186. *p_buf = p;
  2187. if (declare_type_index >= parent_module->type_count) {
  2188. set_error_buf(error_buf, error_buf_size, "unknown type");
  2189. return false;
  2190. }
  2191. #if WASM_ENABLE_GC != 0
  2192. function->type_idx = declare_type_index;
  2193. #endif
  2194. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2195. declare_type_index = wasm_get_smallest_type_idx(
  2196. parent_module->types, parent_module->type_count, declare_type_index);
  2197. #endif
  2198. function->func_type =
  2199. (WASMFuncType *)parent_module->types[declare_type_index];
  2200. function->module_name = (char *)sub_module_name;
  2201. function->field_name = (char *)function_name;
  2202. function->attachment = NULL;
  2203. function->signature = NULL;
  2204. function->call_conv_raw = false;
  2205. /* lookup registered native symbols first */
  2206. if (!no_resolve) {
  2207. wasm_resolve_import_func(parent_module, function);
  2208. }
  2209. return true;
  2210. fail:
  2211. return false;
  2212. }
  2213. static bool
  2214. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2215. uint32 error_buf_size)
  2216. {
  2217. if (max_size < init_size) {
  2218. set_error_buf(error_buf, error_buf_size,
  2219. "size minimum must not be greater than maximum");
  2220. return false;
  2221. }
  2222. return true;
  2223. }
  2224. static bool
  2225. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2226. WASMModule *parent_module, const char *sub_module_name,
  2227. const char *table_name, WASMTableImport *table,
  2228. char *error_buf, uint32 error_buf_size)
  2229. {
  2230. const uint8 *p = *p_buf, *p_end = buf_end;
  2231. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  2232. declare_init_size = 0, declare_max_size = 0;
  2233. #if WASM_ENABLE_MULTI_MODULE != 0
  2234. WASMModule *sub_module = NULL;
  2235. WASMTable *linked_table = NULL;
  2236. #endif
  2237. #if WASM_ENABLE_GC != 0
  2238. WASMRefType ref_type;
  2239. bool need_ref_type_map;
  2240. #endif
  2241. #if WASM_ENABLE_GC == 0
  2242. CHECK_BUF(p, p_end, 1);
  2243. /* 0x70 or 0x6F */
  2244. declare_elem_type = read_uint8(p);
  2245. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2246. #if WASM_ENABLE_REF_TYPES != 0
  2247. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2248. #endif
  2249. ) {
  2250. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2251. return false;
  2252. }
  2253. #else /* else of WASM_ENABLE_GC == 0 */
  2254. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2255. &need_ref_type_map, &ref_type, false, error_buf,
  2256. error_buf_size)) {
  2257. return false;
  2258. }
  2259. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2260. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2261. return false;
  2262. }
  2263. declare_elem_type = ref_type.ref_type;
  2264. if (need_ref_type_map) {
  2265. if (!(table->table_type.elem_ref_type =
  2266. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2267. error_buf, error_buf_size))) {
  2268. return false;
  2269. }
  2270. }
  2271. #if TRACE_WASM_LOADER != 0
  2272. os_printf("import table type: ");
  2273. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2274. os_printf("\n");
  2275. #endif
  2276. #endif /* end of WASM_ENABLE_GC == 0 */
  2277. read_leb_uint32(p, p_end, declare_max_size_flag);
  2278. if (declare_max_size_flag > 1) {
  2279. set_error_buf(error_buf, error_buf_size, "integer too large");
  2280. return false;
  2281. }
  2282. read_leb_uint32(p, p_end, declare_init_size);
  2283. if (declare_max_size_flag) {
  2284. read_leb_uint32(p, p_end, declare_max_size);
  2285. if (!check_table_max_size(declare_init_size, declare_max_size,
  2286. error_buf, error_buf_size))
  2287. return false;
  2288. }
  2289. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  2290. &declare_max_size);
  2291. *p_buf = p;
  2292. #if WASM_ENABLE_MULTI_MODULE != 0
  2293. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2294. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2295. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2296. error_buf_size);
  2297. if (sub_module) {
  2298. linked_table = wasm_loader_resolve_table(
  2299. sub_module_name, table_name, declare_init_size,
  2300. declare_max_size, error_buf, error_buf_size);
  2301. if (linked_table) {
  2302. /* reset with linked table limit */
  2303. declare_elem_type = linked_table->table_type.elem_type;
  2304. declare_init_size = linked_table->table_type.init_size;
  2305. declare_max_size = linked_table->table_type.max_size;
  2306. declare_max_size_flag = linked_table->table_type.flags;
  2307. table->import_table_linked = linked_table;
  2308. table->import_module = sub_module;
  2309. }
  2310. }
  2311. }
  2312. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2313. /* (table (export "table") 10 20 funcref) */
  2314. /* we need this section working in wamrc */
  2315. if (!strcmp("spectest", sub_module_name)) {
  2316. const uint32 spectest_table_init_size = 10;
  2317. const uint32 spectest_table_max_size = 20;
  2318. if (strcmp("table", table_name)) {
  2319. set_error_buf(error_buf, error_buf_size,
  2320. "incompatible import type or unknown import");
  2321. return false;
  2322. }
  2323. if (declare_init_size > spectest_table_init_size
  2324. || declare_max_size < spectest_table_max_size) {
  2325. set_error_buf(error_buf, error_buf_size,
  2326. "incompatible import type");
  2327. return false;
  2328. }
  2329. declare_init_size = spectest_table_init_size;
  2330. declare_max_size = spectest_table_max_size;
  2331. }
  2332. /* now we believe all declaration are ok */
  2333. table->table_type.elem_type = declare_elem_type;
  2334. table->table_type.init_size = declare_init_size;
  2335. table->table_type.flags = declare_max_size_flag;
  2336. table->table_type.max_size = declare_max_size;
  2337. #if WASM_ENABLE_WAMR_COMPILER != 0
  2338. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2339. parent_module->is_ref_types_used = true;
  2340. #endif
  2341. (void)parent_module;
  2342. return true;
  2343. fail:
  2344. return false;
  2345. }
  2346. static bool
  2347. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2348. uint32 error_buf_size)
  2349. {
  2350. uint32 default_max_size =
  2351. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2352. if (!is_memory64 && init_size > default_max_size) {
  2353. set_error_buf(error_buf, error_buf_size,
  2354. "memory size must be at most 65536 pages (4GiB)");
  2355. return false;
  2356. }
  2357. #if WASM_ENABLE_MEMORY64 != 0
  2358. else if (is_memory64 && init_size > default_max_size) {
  2359. set_error_buf(
  2360. error_buf, error_buf_size,
  2361. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2362. return false;
  2363. }
  2364. #endif
  2365. return true;
  2366. }
  2367. static bool
  2368. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2369. char *error_buf, uint32 error_buf_size)
  2370. {
  2371. uint32 default_max_size =
  2372. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2373. if (max_size < init_size) {
  2374. set_error_buf(error_buf, error_buf_size,
  2375. "size minimum must not be greater than maximum");
  2376. return false;
  2377. }
  2378. if (!is_memory64 && max_size > default_max_size) {
  2379. set_error_buf(error_buf, error_buf_size,
  2380. "memory size must be at most 65536 pages (4GiB)");
  2381. return false;
  2382. }
  2383. #if WASM_ENABLE_MEMORY64 != 0
  2384. else if (is_memory64 && max_size > default_max_size) {
  2385. set_error_buf(
  2386. error_buf, error_buf_size,
  2387. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2388. return false;
  2389. }
  2390. #endif
  2391. return true;
  2392. }
  2393. static bool
  2394. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2395. WASMModule *parent_module, const char *sub_module_name,
  2396. const char *memory_name, WASMMemoryImport *memory,
  2397. char *error_buf, uint32 error_buf_size)
  2398. {
  2399. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2400. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2401. uint32 pool_size = wasm_runtime_memory_pool_size();
  2402. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2403. / DEFAULT_NUM_BYTES_PER_PAGE;
  2404. #else
  2405. uint32 max_page_count;
  2406. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2407. uint32 mem_flag = 0;
  2408. bool is_memory64 = false;
  2409. uint32 declare_init_page_count = 0;
  2410. uint32 declare_max_page_count = 0;
  2411. #if WASM_ENABLE_MULTI_MODULE != 0
  2412. WASMModule *sub_module = NULL;
  2413. WASMMemory *linked_memory = NULL;
  2414. #endif
  2415. p_org = p;
  2416. read_leb_uint32(p, p_end, mem_flag);
  2417. is_memory64 = mem_flag & MEMORY64_FLAG;
  2418. if (p - p_org > 1) {
  2419. LOG_VERBOSE("integer representation too long");
  2420. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2421. return false;
  2422. }
  2423. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2424. return false;
  2425. }
  2426. read_leb_uint32(p, p_end, declare_init_page_count);
  2427. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2428. error_buf_size)) {
  2429. return false;
  2430. }
  2431. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2432. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2433. #endif
  2434. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2435. read_leb_uint32(p, p_end, declare_max_page_count);
  2436. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2437. declare_max_page_count, error_buf,
  2438. error_buf_size)) {
  2439. return false;
  2440. }
  2441. if (declare_max_page_count > max_page_count) {
  2442. declare_max_page_count = max_page_count;
  2443. }
  2444. }
  2445. else {
  2446. /* Limit the maximum memory size to max_page_count */
  2447. declare_max_page_count = max_page_count;
  2448. }
  2449. #if WASM_ENABLE_MULTI_MODULE != 0
  2450. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2451. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2452. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2453. error_buf_size);
  2454. if (sub_module) {
  2455. linked_memory = wasm_loader_resolve_memory(
  2456. sub_module_name, memory_name, declare_init_page_count,
  2457. declare_max_page_count, error_buf, error_buf_size);
  2458. if (linked_memory) {
  2459. /**
  2460. * reset with linked memory limit
  2461. */
  2462. memory->import_module = sub_module;
  2463. memory->import_memory_linked = linked_memory;
  2464. declare_init_page_count = linked_memory->init_page_count;
  2465. declare_max_page_count = linked_memory->max_page_count;
  2466. }
  2467. }
  2468. }
  2469. #endif
  2470. /* (memory (export "memory") 1 2) */
  2471. if (!strcmp("spectest", sub_module_name)) {
  2472. uint32 spectest_memory_init_page = 1;
  2473. uint32 spectest_memory_max_page = 2;
  2474. if (strcmp("memory", memory_name)) {
  2475. set_error_buf(error_buf, error_buf_size,
  2476. "incompatible import type or unknown import");
  2477. return false;
  2478. }
  2479. if (declare_init_page_count > spectest_memory_init_page
  2480. || declare_max_page_count < spectest_memory_max_page) {
  2481. set_error_buf(error_buf, error_buf_size,
  2482. "incompatible import type");
  2483. return false;
  2484. }
  2485. declare_init_page_count = spectest_memory_init_page;
  2486. declare_max_page_count = spectest_memory_max_page;
  2487. }
  2488. #if WASM_ENABLE_WASI_TEST != 0
  2489. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2490. else if (!strcmp("foo", sub_module_name)) {
  2491. uint32 spectest_memory_init_page = 1;
  2492. uint32 spectest_memory_max_page = 1;
  2493. if (strcmp("bar", memory_name)) {
  2494. set_error_buf(error_buf, error_buf_size,
  2495. "incompatible import type or unknown import");
  2496. return false;
  2497. }
  2498. if (declare_init_page_count > spectest_memory_init_page
  2499. || declare_max_page_count < spectest_memory_max_page) {
  2500. set_error_buf(error_buf, error_buf_size,
  2501. "incompatible import type");
  2502. return false;
  2503. }
  2504. declare_init_page_count = spectest_memory_init_page;
  2505. declare_max_page_count = spectest_memory_max_page;
  2506. }
  2507. #endif
  2508. /* now we believe all declaration are ok */
  2509. memory->mem_type.flags = mem_flag;
  2510. memory->mem_type.init_page_count = declare_init_page_count;
  2511. memory->mem_type.max_page_count = declare_max_page_count;
  2512. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2513. *p_buf = p;
  2514. (void)parent_module;
  2515. return true;
  2516. fail:
  2517. return false;
  2518. }
  2519. #if WASM_ENABLE_TAGS != 0
  2520. static bool
  2521. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2522. const WASMModule *parent_module, /* this module ! */
  2523. const char *sub_module_name, const char *tag_name,
  2524. WASMTagImport *tag, /* structure to fill */
  2525. char *error_buf, uint32 error_buf_size)
  2526. {
  2527. /* attribute and type of the import statement */
  2528. uint8 declare_tag_attribute;
  2529. uint32 declare_type_index;
  2530. const uint8 *p = *p_buf, *p_end = buf_end;
  2531. #if WASM_ENABLE_MULTI_MODULE != 0
  2532. WASMModule *sub_module = NULL;
  2533. #endif
  2534. /* get the one byte attribute */
  2535. CHECK_BUF(p, p_end, 1);
  2536. declare_tag_attribute = read_uint8(p);
  2537. if (declare_tag_attribute != 0) {
  2538. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2539. goto fail;
  2540. }
  2541. /* get type */
  2542. read_leb_uint32(p, p_end, declare_type_index);
  2543. /* compare against module->types */
  2544. if (declare_type_index >= parent_module->type_count) {
  2545. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2546. goto fail;
  2547. }
  2548. WASMFuncType *declare_tag_type =
  2549. (WASMFuncType *)parent_module->types[declare_type_index];
  2550. /* check, that the type of the declared tag returns void */
  2551. if (declare_tag_type->result_count != 0) {
  2552. set_error_buf(error_buf, error_buf_size,
  2553. "tag type signature does not return void");
  2554. goto fail;
  2555. }
  2556. #if WASM_ENABLE_MULTI_MODULE != 0
  2557. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2558. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2559. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2560. error_buf_size);
  2561. if (sub_module) {
  2562. /* wasm_loader_resolve_tag checks, that the imported tag
  2563. * and the declared tag have the same type
  2564. */
  2565. uint32 linked_tag_index = 0;
  2566. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2567. sub_module_name, tag_name, declare_tag_type,
  2568. &linked_tag_index /* out */, error_buf, error_buf_size);
  2569. if (linked_tag) {
  2570. tag->import_module = sub_module;
  2571. tag->import_tag_linked = linked_tag;
  2572. tag->import_tag_index_linked = linked_tag_index;
  2573. }
  2574. }
  2575. }
  2576. #endif
  2577. /* store to module tag declarations */
  2578. tag->attribute = declare_tag_attribute;
  2579. tag->type = declare_type_index;
  2580. tag->module_name = (char *)sub_module_name;
  2581. tag->field_name = (char *)tag_name;
  2582. tag->tag_type = declare_tag_type;
  2583. *p_buf = p;
  2584. (void)parent_module;
  2585. LOG_VERBOSE("Load tag import success\n");
  2586. return true;
  2587. fail:
  2588. return false;
  2589. }
  2590. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2591. static bool
  2592. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2593. WASMModule *parent_module, char *sub_module_name,
  2594. char *global_name, WASMGlobalImport *global, char *error_buf,
  2595. uint32 error_buf_size)
  2596. {
  2597. const uint8 *p = *p_buf, *p_end = buf_end;
  2598. uint8 declare_type = 0;
  2599. uint8 declare_mutable = 0;
  2600. #if WASM_ENABLE_MULTI_MODULE != 0
  2601. WASMModule *sub_module = NULL;
  2602. WASMGlobal *linked_global = NULL;
  2603. #endif
  2604. #if WASM_ENABLE_GC != 0
  2605. WASMRefType ref_type;
  2606. bool need_ref_type_map;
  2607. #endif
  2608. bool ret = false;
  2609. #if WASM_ENABLE_GC == 0
  2610. CHECK_BUF(p, p_end, 2);
  2611. /* global type */
  2612. declare_type = read_uint8(p);
  2613. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2614. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2615. return false;
  2616. }
  2617. declare_mutable = read_uint8(p);
  2618. #else
  2619. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2620. &need_ref_type_map, &ref_type, false, error_buf,
  2621. error_buf_size)) {
  2622. return false;
  2623. }
  2624. declare_type = ref_type.ref_type;
  2625. if (need_ref_type_map) {
  2626. if (!(global->ref_type =
  2627. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2628. error_buf, error_buf_size))) {
  2629. return false;
  2630. }
  2631. }
  2632. #if TRACE_WASM_LOADER != 0
  2633. os_printf("import global type: ");
  2634. wasm_dump_value_type(declare_type, global->ref_type);
  2635. os_printf("\n");
  2636. #endif
  2637. CHECK_BUF(p, p_end, 1);
  2638. declare_mutable = read_uint8(p);
  2639. #endif /* end of WASM_ENABLE_GC == 0 */
  2640. *p_buf = p;
  2641. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2642. return false;
  2643. }
  2644. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2645. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2646. global);
  2647. if (ret) {
  2648. if (global->type.val_type != declare_type
  2649. || global->type.is_mutable != declare_mutable) {
  2650. set_error_buf(error_buf, error_buf_size,
  2651. "incompatible import type");
  2652. return false;
  2653. }
  2654. global->is_linked = true;
  2655. }
  2656. #endif
  2657. #if WASM_ENABLE_MULTI_MODULE != 0
  2658. if (!global->is_linked
  2659. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2660. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2661. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2662. error_buf_size);
  2663. if (sub_module) {
  2664. /* check sub modules */
  2665. linked_global = wasm_loader_resolve_global(
  2666. sub_module_name, global_name, declare_type, declare_mutable,
  2667. error_buf, error_buf_size);
  2668. if (linked_global) {
  2669. global->import_module = sub_module;
  2670. global->import_global_linked = linked_global;
  2671. global->is_linked = true;
  2672. }
  2673. }
  2674. }
  2675. #endif
  2676. global->module_name = sub_module_name;
  2677. global->field_name = global_name;
  2678. global->type.val_type = declare_type;
  2679. global->type.is_mutable = (declare_mutable == 1);
  2680. #if WASM_ENABLE_WAMR_COMPILER != 0
  2681. if (global->type.val_type == VALUE_TYPE_V128)
  2682. parent_module->is_simd_used = true;
  2683. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2684. parent_module->is_ref_types_used = true;
  2685. #endif
  2686. (void)parent_module;
  2687. (void)ret;
  2688. return true;
  2689. fail:
  2690. return false;
  2691. }
  2692. static bool
  2693. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2694. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2695. {
  2696. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2697. #if WASM_ENABLE_GC != 0
  2698. WASMRefType ref_type;
  2699. bool need_ref_type_map;
  2700. #endif
  2701. #if WASM_ENABLE_GC == 0
  2702. CHECK_BUF(p, p_end, 1);
  2703. /* 0x70 or 0x6F */
  2704. table->table_type.elem_type = read_uint8(p);
  2705. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  2706. #if WASM_ENABLE_REF_TYPES != 0
  2707. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  2708. #endif
  2709. ) {
  2710. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2711. return false;
  2712. }
  2713. #else /* else of WASM_ENABLE_GC == 0 */
  2714. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2715. &need_ref_type_map, &ref_type, false, error_buf,
  2716. error_buf_size)) {
  2717. return false;
  2718. }
  2719. table->table_type.elem_type = ref_type.ref_type;
  2720. if (need_ref_type_map) {
  2721. if (!(table->table_type.elem_ref_type =
  2722. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2723. error_buf_size))) {
  2724. return false;
  2725. }
  2726. }
  2727. #if TRACE_WASM_LOADER != 0
  2728. os_printf("table type: ");
  2729. wasm_dump_value_type(table->table_type.elem_type,
  2730. table->table_type.elem_ref_type);
  2731. os_printf("\n");
  2732. #endif
  2733. #endif /* end of WASM_ENABLE_GC == 0 */
  2734. p_org = p;
  2735. read_leb_uint32(p, p_end, table->table_type.flags);
  2736. #if WASM_ENABLE_SHARED_MEMORY == 0
  2737. if (p - p_org > 1) {
  2738. set_error_buf(error_buf, error_buf_size,
  2739. "integer representation too long");
  2740. return false;
  2741. }
  2742. if (table->table_type.flags > 1) {
  2743. set_error_buf(error_buf, error_buf_size, "integer too large");
  2744. return false;
  2745. }
  2746. #else
  2747. if (p - p_org > 1) {
  2748. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2749. return false;
  2750. }
  2751. if (table->table_type.flags == 2) {
  2752. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2753. return false;
  2754. }
  2755. if (table->table_type.flags > 1) {
  2756. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2757. return false;
  2758. }
  2759. #endif
  2760. read_leb_uint32(p, p_end, table->table_type.init_size);
  2761. if (table->table_type.flags) {
  2762. read_leb_uint32(p, p_end, table->table_type.max_size);
  2763. if (!check_table_max_size(table->table_type.init_size,
  2764. table->table_type.max_size, error_buf,
  2765. error_buf_size))
  2766. return false;
  2767. }
  2768. adjust_table_max_size(table->table_type.init_size, table->table_type.flags,
  2769. &table->table_type.max_size);
  2770. #if WASM_ENABLE_WAMR_COMPILER != 0
  2771. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2772. module->is_ref_types_used = true;
  2773. #endif
  2774. *p_buf = p;
  2775. return true;
  2776. fail:
  2777. return false;
  2778. }
  2779. static bool
  2780. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2781. char *error_buf, uint32 error_buf_size)
  2782. {
  2783. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2784. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2785. uint32 pool_size = wasm_runtime_memory_pool_size();
  2786. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2787. / DEFAULT_NUM_BYTES_PER_PAGE;
  2788. #else
  2789. uint32 max_page_count;
  2790. #endif
  2791. bool is_memory64 = false;
  2792. p_org = p;
  2793. read_leb_uint32(p, p_end, memory->flags);
  2794. is_memory64 = memory->flags & MEMORY64_FLAG;
  2795. if (p - p_org > 1) {
  2796. LOG_VERBOSE("integer representation too long");
  2797. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2798. return false;
  2799. }
  2800. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2801. false)) {
  2802. return false;
  2803. }
  2804. read_leb_uint32(p, p_end, memory->init_page_count);
  2805. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2806. error_buf_size))
  2807. return false;
  2808. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2809. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2810. #endif
  2811. if (memory->flags & 1) {
  2812. read_leb_uint32(p, p_end, memory->max_page_count);
  2813. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2814. memory->max_page_count, error_buf,
  2815. error_buf_size))
  2816. return false;
  2817. if (memory->max_page_count > max_page_count)
  2818. memory->max_page_count = max_page_count;
  2819. }
  2820. else {
  2821. /* Limit the maximum memory size to max_page_count */
  2822. memory->max_page_count = max_page_count;
  2823. }
  2824. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2825. *p_buf = p;
  2826. return true;
  2827. fail:
  2828. return false;
  2829. }
  2830. static bool
  2831. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2832. bool is_load_from_file_buf, bool no_resolve,
  2833. char *error_buf, uint32 error_buf_size)
  2834. {
  2835. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2836. uint32 import_count, name_len, type_index, i, u32, flags;
  2837. uint64 total_size;
  2838. WASMImport *import;
  2839. WASMImport *import_functions = NULL, *import_tables = NULL;
  2840. WASMImport *import_memories = NULL, *import_globals = NULL;
  2841. #if WASM_ENABLE_TAGS != 0
  2842. WASMImport *import_tags = NULL;
  2843. #endif
  2844. char *sub_module_name, *field_name;
  2845. uint8 u8, kind, global_type;
  2846. read_leb_uint32(p, p_end, import_count);
  2847. if (import_count) {
  2848. module->import_count = import_count;
  2849. total_size = sizeof(WASMImport) * (uint64)import_count;
  2850. if (!(module->imports =
  2851. loader_malloc(total_size, error_buf, error_buf_size))) {
  2852. return false;
  2853. }
  2854. p_old = p;
  2855. /* Scan firstly to get import count of each type */
  2856. for (i = 0; i < import_count; i++) {
  2857. /* module name */
  2858. read_leb_uint32(p, p_end, name_len);
  2859. CHECK_BUF(p, p_end, name_len);
  2860. p += name_len;
  2861. /* field name */
  2862. read_leb_uint32(p, p_end, name_len);
  2863. CHECK_BUF(p, p_end, name_len);
  2864. p += name_len;
  2865. CHECK_BUF(p, p_end, 1);
  2866. /* 0x00/0x01/0x02/0x03/0x04 */
  2867. kind = read_uint8(p);
  2868. switch (kind) {
  2869. case IMPORT_KIND_FUNC: /* import function */
  2870. read_leb_uint32(p, p_end, type_index);
  2871. module->import_function_count++;
  2872. break;
  2873. case IMPORT_KIND_TABLE: /* import table */
  2874. CHECK_BUF(p, p_end, 1);
  2875. /* 0x70 */
  2876. u8 = read_uint8(p);
  2877. read_leb_uint32(p, p_end, flags);
  2878. read_leb_uint32(p, p_end, u32);
  2879. if (flags & 1)
  2880. read_leb_uint32(p, p_end, u32);
  2881. module->import_table_count++;
  2882. if (module->import_table_count > 1) {
  2883. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2884. set_error_buf(error_buf, error_buf_size,
  2885. "multiple tables");
  2886. return false;
  2887. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2888. module->is_ref_types_used = true;
  2889. #endif
  2890. }
  2891. break;
  2892. case IMPORT_KIND_MEMORY: /* import memory */
  2893. read_leb_uint32(p, p_end, flags);
  2894. read_leb_uint32(p, p_end, u32);
  2895. if (flags & 1)
  2896. read_leb_uint32(p, p_end, u32);
  2897. module->import_memory_count++;
  2898. #if WASM_ENABLE_MULTI_MEMORY == 0
  2899. if (module->import_memory_count > 1) {
  2900. set_error_buf(error_buf, error_buf_size,
  2901. "multiple memories");
  2902. return false;
  2903. }
  2904. #endif
  2905. break;
  2906. #if WASM_ENABLE_TAGS != 0
  2907. case IMPORT_KIND_TAG: /* import tags */
  2908. /* it only counts the number of tags to import */
  2909. module->import_tag_count++;
  2910. CHECK_BUF(p, p_end, 1);
  2911. u8 = read_uint8(p);
  2912. read_leb_uint32(p, p_end, type_index);
  2913. break;
  2914. #endif
  2915. case IMPORT_KIND_GLOBAL: /* import global */
  2916. #if WASM_ENABLE_GC != 0
  2917. /* valtype */
  2918. CHECK_BUF(p, p_end, 1);
  2919. global_type = read_uint8(p);
  2920. if (wasm_is_type_multi_byte_type(global_type)) {
  2921. int32 heap_type;
  2922. read_leb_int32(p, p_end, heap_type);
  2923. (void)heap_type;
  2924. }
  2925. /* mutability */
  2926. CHECK_BUF(p, p_end, 1);
  2927. p += 1;
  2928. #else
  2929. CHECK_BUF(p, p_end, 2);
  2930. p += 2;
  2931. #endif
  2932. (void)global_type;
  2933. module->import_global_count++;
  2934. break;
  2935. default:
  2936. set_error_buf(error_buf, error_buf_size,
  2937. "invalid import kind");
  2938. return false;
  2939. }
  2940. }
  2941. if (module->import_function_count)
  2942. import_functions = module->import_functions = module->imports;
  2943. if (module->import_table_count)
  2944. import_tables = module->import_tables =
  2945. module->imports + module->import_function_count;
  2946. if (module->import_memory_count)
  2947. import_memories = module->import_memories =
  2948. module->imports + module->import_function_count
  2949. + module->import_table_count;
  2950. #if WASM_ENABLE_TAGS != 0
  2951. if (module->import_tag_count)
  2952. import_tags = module->import_tags =
  2953. module->imports + module->import_function_count
  2954. + module->import_table_count + module->import_memory_count;
  2955. if (module->import_global_count)
  2956. import_globals = module->import_globals =
  2957. module->imports + module->import_function_count
  2958. + module->import_table_count + module->import_memory_count
  2959. + module->import_tag_count;
  2960. #else
  2961. if (module->import_global_count)
  2962. import_globals = module->import_globals =
  2963. module->imports + module->import_function_count
  2964. + module->import_table_count + module->import_memory_count;
  2965. #endif
  2966. p = p_old;
  2967. /* Scan again to resolve the data */
  2968. for (i = 0; i < import_count; i++) {
  2969. /* load module name */
  2970. read_leb_uint32(p, p_end, name_len);
  2971. CHECK_BUF(p, p_end, name_len);
  2972. if (!(sub_module_name = wasm_const_str_list_insert(
  2973. p, name_len, module, is_load_from_file_buf, error_buf,
  2974. error_buf_size))) {
  2975. return false;
  2976. }
  2977. p += name_len;
  2978. /* load field name */
  2979. read_leb_uint32(p, p_end, name_len);
  2980. CHECK_BUF(p, p_end, name_len);
  2981. if (!(field_name = wasm_const_str_list_insert(
  2982. p, name_len, module, is_load_from_file_buf, error_buf,
  2983. error_buf_size))) {
  2984. return false;
  2985. }
  2986. p += name_len;
  2987. CHECK_BUF(p, p_end, 1);
  2988. /* 0x00/0x01/0x02/0x03/0x4 */
  2989. kind = read_uint8(p);
  2990. switch (kind) {
  2991. case IMPORT_KIND_FUNC: /* import function */
  2992. bh_assert(import_functions);
  2993. import = import_functions++;
  2994. if (!load_function_import(&p, p_end, module,
  2995. sub_module_name, field_name,
  2996. &import->u.function, no_resolve,
  2997. error_buf, error_buf_size)) {
  2998. return false;
  2999. }
  3000. break;
  3001. case IMPORT_KIND_TABLE: /* import table */
  3002. bh_assert(import_tables);
  3003. import = import_tables++;
  3004. if (!load_table_import(&p, p_end, module, sub_module_name,
  3005. field_name, &import->u.table,
  3006. error_buf, error_buf_size)) {
  3007. LOG_DEBUG("can not import such a table (%s,%s)",
  3008. sub_module_name, field_name);
  3009. return false;
  3010. }
  3011. break;
  3012. case IMPORT_KIND_MEMORY: /* import memory */
  3013. bh_assert(import_memories);
  3014. import = import_memories++;
  3015. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3016. field_name, &import->u.memory,
  3017. error_buf, error_buf_size)) {
  3018. return false;
  3019. }
  3020. break;
  3021. #if WASM_ENABLE_TAGS != 0
  3022. case IMPORT_KIND_TAG:
  3023. bh_assert(import_tags);
  3024. import = import_tags++;
  3025. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3026. field_name, &import->u.tag, error_buf,
  3027. error_buf_size)) {
  3028. return false;
  3029. }
  3030. break;
  3031. #endif
  3032. case IMPORT_KIND_GLOBAL: /* import global */
  3033. bh_assert(import_globals);
  3034. import = import_globals++;
  3035. if (!load_global_import(&p, p_end, module, sub_module_name,
  3036. field_name, &import->u.global,
  3037. error_buf, error_buf_size)) {
  3038. return false;
  3039. }
  3040. break;
  3041. default:
  3042. set_error_buf(error_buf, error_buf_size,
  3043. "invalid import kind");
  3044. return false;
  3045. }
  3046. import->kind = kind;
  3047. import->u.names.module_name = sub_module_name;
  3048. import->u.names.field_name = field_name;
  3049. }
  3050. #if WASM_ENABLE_LIBC_WASI != 0
  3051. import = module->import_functions;
  3052. for (i = 0; i < module->import_function_count; i++, import++) {
  3053. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3054. || !strcmp(import->u.names.module_name,
  3055. "wasi_snapshot_preview1")) {
  3056. module->import_wasi_api = true;
  3057. break;
  3058. }
  3059. }
  3060. #endif
  3061. }
  3062. if (p != p_end) {
  3063. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3064. return false;
  3065. }
  3066. LOG_VERBOSE("Load import section success.\n");
  3067. (void)u8;
  3068. (void)u32;
  3069. (void)type_index;
  3070. return true;
  3071. fail:
  3072. return false;
  3073. }
  3074. static bool
  3075. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3076. uint32 error_buf_size)
  3077. {
  3078. WASMFuncType *param_type = func->func_type;
  3079. uint32 param_count = param_type->param_count;
  3080. uint8 *param_types = param_type->types;
  3081. uint32 local_count = func->local_count;
  3082. uint8 *local_types = func->local_types;
  3083. uint32 i, local_offset = 0;
  3084. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3085. /*
  3086. * Only allocate memory when total_size is not 0,
  3087. * or the return value of malloc(0) might be NULL on some platforms,
  3088. * which causes wasm loader return false.
  3089. */
  3090. if (total_size > 0
  3091. && !(func->local_offsets =
  3092. loader_malloc(total_size, error_buf, error_buf_size))) {
  3093. return false;
  3094. }
  3095. for (i = 0; i < param_count; i++) {
  3096. func->local_offsets[i] = (uint16)local_offset;
  3097. local_offset += wasm_value_type_cell_num(param_types[i]);
  3098. }
  3099. for (i = 0; i < local_count; i++) {
  3100. func->local_offsets[param_count + i] = (uint16)local_offset;
  3101. local_offset += wasm_value_type_cell_num(local_types[i]);
  3102. }
  3103. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3104. return true;
  3105. }
  3106. static bool
  3107. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3108. const uint8 *buf_code, const uint8 *buf_code_end,
  3109. WASMModule *module, char *error_buf,
  3110. uint32 error_buf_size)
  3111. {
  3112. const uint8 *p = buf, *p_end = buf_end;
  3113. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3114. uint32 func_count;
  3115. uint64 total_size;
  3116. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3117. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3118. uint8 type;
  3119. WASMFunction *func;
  3120. #if WASM_ENABLE_GC != 0
  3121. bool need_ref_type_map;
  3122. WASMRefType ref_type;
  3123. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3124. #endif
  3125. read_leb_uint32(p, p_end, func_count);
  3126. if (buf_code)
  3127. read_leb_uint32(p_code, buf_code_end, code_count);
  3128. if (func_count != code_count) {
  3129. set_error_buf(error_buf, error_buf_size,
  3130. "function and code section have inconsistent lengths or "
  3131. "unexpected end");
  3132. return false;
  3133. }
  3134. if (is_indices_overflow(module->import_function_count, func_count,
  3135. error_buf, error_buf_size))
  3136. return false;
  3137. if (func_count) {
  3138. module->function_count = func_count;
  3139. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3140. if (!(module->functions =
  3141. loader_malloc(total_size, error_buf, error_buf_size))) {
  3142. return false;
  3143. }
  3144. for (i = 0; i < func_count; i++) {
  3145. /* Resolve function type */
  3146. read_leb_uint32(p, p_end, type_index);
  3147. if (type_index >= module->type_count) {
  3148. set_error_buf(error_buf, error_buf_size, "unknown type");
  3149. return false;
  3150. }
  3151. #if WASM_ENABLE_GC != 0
  3152. type_index_org = type_index;
  3153. #endif
  3154. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3155. && WASM_ENABLE_GC == 0
  3156. type_index = wasm_get_smallest_type_idx(
  3157. module->types, module->type_count, type_index);
  3158. #endif
  3159. read_leb_uint32(p_code, buf_code_end, code_size);
  3160. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3161. set_error_buf(error_buf, error_buf_size,
  3162. "invalid function code size");
  3163. return false;
  3164. }
  3165. /* Resolve local set count */
  3166. p_code_end = p_code + code_size;
  3167. local_count = 0;
  3168. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3169. p_code_save = p_code;
  3170. #if WASM_ENABLE_GC != 0
  3171. ref_type_map_count = 0;
  3172. #endif
  3173. /* Calculate total local count */
  3174. for (j = 0; j < local_set_count; j++) {
  3175. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3176. if (sub_local_count > UINT32_MAX - local_count) {
  3177. set_error_buf(error_buf, error_buf_size, "too many locals");
  3178. return false;
  3179. }
  3180. #if WASM_ENABLE_GC == 0
  3181. CHECK_BUF(p_code, buf_code_end, 1);
  3182. /* 0x7F/0x7E/0x7D/0x7C */
  3183. type = read_uint8(p_code);
  3184. local_count += sub_local_count;
  3185. #if WASM_ENABLE_WAMR_COMPILER != 0
  3186. /* If any value's type is v128, mark the module as SIMD used */
  3187. if (type == VALUE_TYPE_V128)
  3188. module->is_simd_used = true;
  3189. #endif
  3190. #else
  3191. if (!resolve_value_type(&p_code, buf_code_end, module,
  3192. module->type_count, &need_ref_type_map,
  3193. &ref_type, false, error_buf,
  3194. error_buf_size)) {
  3195. return false;
  3196. }
  3197. local_count += sub_local_count;
  3198. if (need_ref_type_map)
  3199. ref_type_map_count += sub_local_count;
  3200. #endif
  3201. }
  3202. /* Alloc memory, layout: function structure + local types */
  3203. code_size = (uint32)(p_code_end - p_code);
  3204. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3205. if (!(func = module->functions[i] =
  3206. loader_malloc(total_size, error_buf, error_buf_size))) {
  3207. return false;
  3208. }
  3209. #if WASM_ENABLE_GC != 0
  3210. if (ref_type_map_count > 0) {
  3211. total_size =
  3212. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3213. if (!(func->local_ref_type_maps = loader_malloc(
  3214. total_size, error_buf, error_buf_size))) {
  3215. return false;
  3216. }
  3217. func->local_ref_type_map_count = ref_type_map_count;
  3218. }
  3219. #endif
  3220. /* Set function type, local count, code size and code body */
  3221. func->func_type = (WASMFuncType *)module->types[type_index];
  3222. func->local_count = local_count;
  3223. if (local_count > 0)
  3224. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3225. func->code_size = code_size;
  3226. /*
  3227. * we shall make a copy of code body [p_code, p_code + code_size]
  3228. * when we are worrying about inappropriate releasing behaviour.
  3229. * all code bodies are actually in a buffer which user allocates in
  3230. * his embedding environment and we don't have power on them.
  3231. * it will be like:
  3232. * code_body_cp = malloc(code_size);
  3233. * memcpy(code_body_cp, p_code, code_size);
  3234. * func->code = code_body_cp;
  3235. */
  3236. func->code = (uint8 *)p_code;
  3237. #if WASM_ENABLE_GC != 0
  3238. func->type_idx = type_index_org;
  3239. #endif
  3240. #if WASM_ENABLE_GC != 0
  3241. t = 0;
  3242. #endif
  3243. /* Load each local type */
  3244. p_code = p_code_save;
  3245. local_type_index = 0;
  3246. for (j = 0; j < local_set_count; j++) {
  3247. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3248. /* Note: sub_local_count is allowed to be 0 */
  3249. if (local_type_index > UINT32_MAX - sub_local_count
  3250. || local_type_index + sub_local_count > local_count) {
  3251. set_error_buf(error_buf, error_buf_size,
  3252. "invalid local count");
  3253. return false;
  3254. }
  3255. #if WASM_ENABLE_GC == 0
  3256. CHECK_BUF(p_code, buf_code_end, 1);
  3257. /* 0x7F/0x7E/0x7D/0x7C */
  3258. type = read_uint8(p_code);
  3259. if (!is_valid_value_type_for_interpreter(type)) {
  3260. if (type == VALUE_TYPE_V128)
  3261. set_error_buf(error_buf, error_buf_size,
  3262. "v128 value type requires simd feature");
  3263. else if (type == VALUE_TYPE_FUNCREF
  3264. || type == VALUE_TYPE_EXTERNREF)
  3265. set_error_buf(error_buf, error_buf_size,
  3266. "ref value type requires "
  3267. "reference types feature");
  3268. else
  3269. set_error_buf_v(error_buf, error_buf_size,
  3270. "invalid local type 0x%02X", type);
  3271. return false;
  3272. }
  3273. #else
  3274. if (!resolve_value_type(&p_code, buf_code_end, module,
  3275. module->type_count, &need_ref_type_map,
  3276. &ref_type, false, error_buf,
  3277. error_buf_size)) {
  3278. return false;
  3279. }
  3280. if (need_ref_type_map) {
  3281. WASMRefType *ref_type_tmp;
  3282. if (!(ref_type_tmp = reftype_set_insert(
  3283. module->ref_type_set, &ref_type, error_buf,
  3284. error_buf_size))) {
  3285. return false;
  3286. }
  3287. for (k = 0; k < sub_local_count; k++) {
  3288. func->local_ref_type_maps[t + k].ref_type =
  3289. ref_type_tmp;
  3290. func->local_ref_type_maps[t + k].index =
  3291. local_type_index + k;
  3292. }
  3293. t += sub_local_count;
  3294. }
  3295. type = ref_type.ref_type;
  3296. #endif
  3297. for (k = 0; k < sub_local_count; k++) {
  3298. func->local_types[local_type_index++] = type;
  3299. }
  3300. #if WASM_ENABLE_WAMR_COMPILER != 0
  3301. if (type == VALUE_TYPE_V128)
  3302. module->is_simd_used = true;
  3303. else if (type == VALUE_TYPE_FUNCREF
  3304. || type == VALUE_TYPE_EXTERNREF)
  3305. module->is_ref_types_used = true;
  3306. #endif
  3307. }
  3308. bh_assert(local_type_index == func->local_count);
  3309. #if WASM_ENABLE_GC != 0
  3310. bh_assert(t == func->local_ref_type_map_count);
  3311. #if TRACE_WASM_LOADER != 0
  3312. os_printf("func %u, local types: [", i);
  3313. k = 0;
  3314. for (j = 0; j < func->local_count; j++) {
  3315. WASMRefType *ref_type_tmp = NULL;
  3316. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3317. bh_assert(j == func->local_ref_type_maps[k].index);
  3318. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3319. }
  3320. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3321. if (j < func->local_count - 1)
  3322. os_printf(" ");
  3323. }
  3324. os_printf("]\n");
  3325. #endif
  3326. #endif
  3327. func->param_cell_num = func->func_type->param_cell_num;
  3328. func->ret_cell_num = func->func_type->ret_cell_num;
  3329. local_cell_num =
  3330. wasm_get_cell_num(func->local_types, func->local_count);
  3331. if (local_cell_num > UINT16_MAX) {
  3332. set_error_buf(error_buf, error_buf_size,
  3333. "local count too large");
  3334. return false;
  3335. }
  3336. func->local_cell_num = (uint16)local_cell_num;
  3337. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3338. return false;
  3339. p_code = p_code_end;
  3340. }
  3341. }
  3342. if (p != p_end) {
  3343. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3344. return false;
  3345. }
  3346. LOG_VERBOSE("Load function section success.\n");
  3347. return true;
  3348. fail:
  3349. return false;
  3350. }
  3351. static bool
  3352. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3353. char *error_buf, uint32 error_buf_size)
  3354. {
  3355. const uint8 *p = buf, *p_end = buf_end;
  3356. uint32 table_count, i;
  3357. uint64 total_size;
  3358. WASMTable *table;
  3359. read_leb_uint32(p, p_end, table_count);
  3360. if (module->import_table_count + table_count > 1) {
  3361. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3362. /* a total of one table is allowed */
  3363. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3364. return false;
  3365. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3366. module->is_ref_types_used = true;
  3367. #endif
  3368. }
  3369. if (table_count) {
  3370. module->table_count = table_count;
  3371. total_size = sizeof(WASMTable) * (uint64)table_count;
  3372. if (!(module->tables =
  3373. loader_malloc(total_size, error_buf, error_buf_size))) {
  3374. return false;
  3375. }
  3376. /* load each table */
  3377. table = module->tables;
  3378. for (i = 0; i < table_count; i++, table++) {
  3379. #if WASM_ENABLE_GC != 0
  3380. uint8 flag;
  3381. bool has_init = false;
  3382. CHECK_BUF(buf, buf_end, 1);
  3383. flag = read_uint8(p);
  3384. if (flag == TABLE_INIT_EXPR_FLAG) {
  3385. CHECK_BUF(buf, buf_end, 1);
  3386. flag = read_uint8(p);
  3387. if (flag != 0x00) {
  3388. set_error_buf(error_buf, error_buf_size,
  3389. "invalid leading bytes for table");
  3390. return false;
  3391. }
  3392. has_init = true;
  3393. }
  3394. else {
  3395. p--;
  3396. }
  3397. #endif /* end of WASM_ENABLE_GC != 0 */
  3398. if (!load_table(&p, p_end, module, table, error_buf,
  3399. error_buf_size))
  3400. return false;
  3401. #if WASM_ENABLE_GC != 0
  3402. if (has_init) {
  3403. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3404. table->table_type.elem_type,
  3405. table->table_type.elem_ref_type, error_buf,
  3406. error_buf_size))
  3407. return false;
  3408. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3409. && table->init_expr.init_expr_type
  3410. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3411. set_error_buf(
  3412. error_buf, error_buf_size,
  3413. "unsupported initializer expression for table");
  3414. return false;
  3415. }
  3416. }
  3417. else {
  3418. if (wasm_is_reftype_htref_non_nullable(
  3419. table->table_type.elem_type)) {
  3420. set_error_buf(
  3421. error_buf, error_buf_size,
  3422. "type mismatch: non-nullable table without init expr");
  3423. return false;
  3424. }
  3425. }
  3426. #endif /* end of WASM_ENABLE_GC != 0 */
  3427. #if WASM_ENABLE_WAMR_COMPILER != 0
  3428. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3429. module->is_ref_types_used = true;
  3430. #endif
  3431. }
  3432. }
  3433. if (p != p_end) {
  3434. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3435. return false;
  3436. }
  3437. LOG_VERBOSE("Load table section success.\n");
  3438. return true;
  3439. fail:
  3440. return false;
  3441. }
  3442. static bool
  3443. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3444. char *error_buf, uint32 error_buf_size)
  3445. {
  3446. const uint8 *p = buf, *p_end = buf_end;
  3447. uint32 memory_count, i;
  3448. uint64 total_size;
  3449. WASMMemory *memory;
  3450. read_leb_uint32(p, p_end, memory_count);
  3451. #if WASM_ENABLE_MULTI_MEMORY == 0
  3452. /* a total of one memory is allowed */
  3453. if (module->import_memory_count + memory_count > 1) {
  3454. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3455. return false;
  3456. }
  3457. #endif
  3458. if (memory_count) {
  3459. module->memory_count = memory_count;
  3460. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3461. if (!(module->memories =
  3462. loader_malloc(total_size, error_buf, error_buf_size))) {
  3463. return false;
  3464. }
  3465. /* load each memory */
  3466. memory = module->memories;
  3467. for (i = 0; i < memory_count; i++, memory++)
  3468. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3469. return false;
  3470. }
  3471. if (p != p_end) {
  3472. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3473. return false;
  3474. }
  3475. LOG_VERBOSE("Load memory section success.\n");
  3476. return true;
  3477. fail:
  3478. return false;
  3479. }
  3480. static bool
  3481. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3482. char *error_buf, uint32 error_buf_size)
  3483. {
  3484. const uint8 *p = buf, *p_end = buf_end;
  3485. uint32 global_count, i;
  3486. uint64 total_size;
  3487. WASMGlobal *global;
  3488. uint8 mutable;
  3489. #if WASM_ENABLE_GC != 0
  3490. bool need_ref_type_map;
  3491. WASMRefType ref_type;
  3492. #endif
  3493. read_leb_uint32(p, p_end, global_count);
  3494. if (is_indices_overflow(module->import_global_count, global_count,
  3495. error_buf, error_buf_size))
  3496. return false;
  3497. module->global_count = 0;
  3498. if (global_count) {
  3499. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3500. if (!(module->globals =
  3501. loader_malloc(total_size, error_buf, error_buf_size))) {
  3502. return false;
  3503. }
  3504. global = module->globals;
  3505. for (i = 0; i < global_count; i++, global++) {
  3506. #if WASM_ENABLE_GC == 0
  3507. CHECK_BUF(p, p_end, 2);
  3508. /* global type */
  3509. global->type.val_type = read_uint8(p);
  3510. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3511. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3512. return false;
  3513. }
  3514. mutable = read_uint8(p);
  3515. #else
  3516. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3517. &need_ref_type_map, &ref_type, false,
  3518. error_buf, error_buf_size)) {
  3519. return false;
  3520. }
  3521. global->type.val_type = ref_type.ref_type;
  3522. CHECK_BUF(p, p_end, 1);
  3523. mutable = read_uint8(p);
  3524. #endif /* end of WASM_ENABLE_GC */
  3525. #if WASM_ENABLE_WAMR_COMPILER != 0
  3526. if (global->type.val_type == VALUE_TYPE_V128)
  3527. module->is_simd_used = true;
  3528. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3529. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3530. module->is_ref_types_used = true;
  3531. #endif
  3532. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3533. return false;
  3534. }
  3535. global->type.is_mutable = mutable ? true : false;
  3536. /* initialize expression */
  3537. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3538. global->type.val_type,
  3539. #if WASM_ENABLE_GC == 0
  3540. NULL,
  3541. #else
  3542. &ref_type,
  3543. #endif
  3544. error_buf, error_buf_size))
  3545. return false;
  3546. #if WASM_ENABLE_GC != 0
  3547. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3548. uint8 global_type;
  3549. WASMRefType *global_ref_type;
  3550. uint32 global_idx = global->init_expr.u.global_index;
  3551. if (global->init_expr.u.global_index
  3552. >= module->import_global_count + i) {
  3553. set_error_buf(error_buf, error_buf_size, "unknown global");
  3554. return false;
  3555. }
  3556. if (global_idx < module->import_global_count) {
  3557. global_type = module->import_globals[global_idx]
  3558. .u.global.type.val_type;
  3559. global_ref_type =
  3560. module->import_globals[global_idx].u.global.ref_type;
  3561. }
  3562. else {
  3563. global_type =
  3564. module
  3565. ->globals[global_idx - module->import_global_count]
  3566. .type.val_type;
  3567. global_ref_type =
  3568. module
  3569. ->globals[global_idx - module->import_global_count]
  3570. .ref_type;
  3571. }
  3572. if (!wasm_reftype_is_subtype_of(
  3573. global_type, global_ref_type, global->type.val_type,
  3574. global->ref_type, module->types, module->type_count)) {
  3575. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3576. return false;
  3577. }
  3578. }
  3579. if (need_ref_type_map) {
  3580. if (!(global->ref_type =
  3581. reftype_set_insert(module->ref_type_set, &ref_type,
  3582. error_buf, error_buf_size))) {
  3583. return false;
  3584. }
  3585. }
  3586. #if TRACE_WASM_LOADER != 0
  3587. os_printf("global type: ");
  3588. wasm_dump_value_type(global->type, global->ref_type);
  3589. os_printf("\n");
  3590. #endif
  3591. #endif
  3592. module->global_count++;
  3593. }
  3594. bh_assert(module->global_count == global_count);
  3595. }
  3596. if (p != p_end) {
  3597. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3598. return false;
  3599. }
  3600. LOG_VERBOSE("Load global section success.\n");
  3601. return true;
  3602. fail:
  3603. return false;
  3604. }
  3605. static bool
  3606. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3607. bool is_load_from_file_buf, char *error_buf,
  3608. uint32 error_buf_size)
  3609. {
  3610. const uint8 *p = buf, *p_end = buf_end;
  3611. uint32 export_count, i, j, index;
  3612. uint64 total_size;
  3613. uint32 str_len;
  3614. WASMExport *export;
  3615. const char *name;
  3616. read_leb_uint32(p, p_end, export_count);
  3617. if (export_count) {
  3618. module->export_count = export_count;
  3619. total_size = sizeof(WASMExport) * (uint64)export_count;
  3620. if (!(module->exports =
  3621. loader_malloc(total_size, error_buf, error_buf_size))) {
  3622. return false;
  3623. }
  3624. export = module->exports;
  3625. for (i = 0; i < export_count; i++, export ++) {
  3626. #if WASM_ENABLE_THREAD_MGR == 0
  3627. if (p == p_end) {
  3628. /* export section with inconsistent count:
  3629. n export declared, but less than n given */
  3630. set_error_buf(error_buf, error_buf_size,
  3631. "length out of bounds");
  3632. return false;
  3633. }
  3634. #endif
  3635. read_leb_uint32(p, p_end, str_len);
  3636. CHECK_BUF(p, p_end, str_len);
  3637. for (j = 0; j < i; j++) {
  3638. name = module->exports[j].name;
  3639. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  3640. set_error_buf(error_buf, error_buf_size,
  3641. "duplicate export name");
  3642. return false;
  3643. }
  3644. }
  3645. if (!(export->name = wasm_const_str_list_insert(
  3646. p, str_len, module, is_load_from_file_buf, error_buf,
  3647. error_buf_size))) {
  3648. return false;
  3649. }
  3650. p += str_len;
  3651. CHECK_BUF(p, p_end, 1);
  3652. export->kind = read_uint8(p);
  3653. read_leb_uint32(p, p_end, index);
  3654. export->index = index;
  3655. switch (export->kind) {
  3656. /* function index */
  3657. case EXPORT_KIND_FUNC:
  3658. if (index >= module->function_count
  3659. + module->import_function_count) {
  3660. set_error_buf(error_buf, error_buf_size,
  3661. "unknown function");
  3662. return false;
  3663. }
  3664. #if WASM_ENABLE_SIMD != 0
  3665. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3666. /* TODO: check func type, if it has v128 param or result,
  3667. report error */
  3668. #endif
  3669. #endif
  3670. break;
  3671. /* table index */
  3672. case EXPORT_KIND_TABLE:
  3673. if (index
  3674. >= module->table_count + module->import_table_count) {
  3675. set_error_buf(error_buf, error_buf_size,
  3676. "unknown table");
  3677. return false;
  3678. }
  3679. break;
  3680. /* memory index */
  3681. case EXPORT_KIND_MEMORY:
  3682. if (index
  3683. >= module->memory_count + module->import_memory_count) {
  3684. set_error_buf(error_buf, error_buf_size,
  3685. "unknown memory");
  3686. return false;
  3687. }
  3688. break;
  3689. #if WASM_ENABLE_TAGS != 0
  3690. /* export tag */
  3691. case EXPORT_KIND_TAG:
  3692. if (index >= module->tag_count + module->import_tag_count) {
  3693. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3694. return false;
  3695. }
  3696. break;
  3697. #endif
  3698. /* global index */
  3699. case EXPORT_KIND_GLOBAL:
  3700. if (index
  3701. >= module->global_count + module->import_global_count) {
  3702. set_error_buf(error_buf, error_buf_size,
  3703. "unknown global");
  3704. return false;
  3705. }
  3706. break;
  3707. default:
  3708. set_error_buf(error_buf, error_buf_size,
  3709. "invalid export kind");
  3710. return false;
  3711. }
  3712. }
  3713. }
  3714. if (p != p_end) {
  3715. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3716. return false;
  3717. }
  3718. LOG_VERBOSE("Load export section success.\n");
  3719. return true;
  3720. fail:
  3721. return false;
  3722. }
  3723. static bool
  3724. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3725. uint32 error_buf_size)
  3726. {
  3727. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3728. if (table_index != 0) {
  3729. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3730. return false;
  3731. }
  3732. #endif
  3733. if (table_index >= module->import_table_count + module->table_count) {
  3734. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3735. table_index);
  3736. return false;
  3737. }
  3738. return true;
  3739. }
  3740. static bool
  3741. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3742. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3743. {
  3744. const uint8 *p = *p_buf, *p_end = buf_end;
  3745. uint32 table_index;
  3746. read_leb_uint32(p, p_end, table_index);
  3747. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3748. return false;
  3749. }
  3750. *p_table_index = table_index;
  3751. *p_buf = p;
  3752. return true;
  3753. fail:
  3754. return false;
  3755. }
  3756. /* Element segments must match element type of table */
  3757. static bool
  3758. check_table_elem_type(WASMModule *module, uint32 table_index,
  3759. uint32 type_from_elem_seg, char *error_buf,
  3760. uint32 error_buf_size)
  3761. {
  3762. uint32 table_declared_elem_type;
  3763. if (table_index < module->import_table_count)
  3764. table_declared_elem_type =
  3765. module->import_tables[table_index].u.table.table_type.elem_type;
  3766. else
  3767. table_declared_elem_type =
  3768. (module->tables + table_index)->table_type.elem_type;
  3769. if (table_declared_elem_type == type_from_elem_seg)
  3770. return true;
  3771. #if WASM_ENABLE_GC != 0
  3772. /*
  3773. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3774. */
  3775. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3776. return true;
  3777. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3778. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3779. return true;
  3780. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3781. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3782. return true;
  3783. #endif
  3784. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3785. return false;
  3786. }
  3787. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3788. static bool
  3789. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3790. uint32 *p_elem_type,
  3791. #if WASM_ENABLE_GC != 0
  3792. WASMRefType **p_elem_ref_type,
  3793. #endif
  3794. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3795. {
  3796. const uint8 *p = *p_buf, *p_end = buf_end;
  3797. uint8 elem_type;
  3798. #if WASM_ENABLE_GC != 0
  3799. WASMRefType elem_ref_type;
  3800. bool need_ref_type_map;
  3801. #endif
  3802. CHECK_BUF(p, p_end, 1);
  3803. elem_type = read_uint8(p);
  3804. if (elemkind_zero) {
  3805. if (elem_type != 0) {
  3806. set_error_buf(error_buf, error_buf_size,
  3807. "invalid reference type or unknown type");
  3808. return false;
  3809. }
  3810. else {
  3811. *p_elem_type = VALUE_TYPE_FUNCREF;
  3812. *p_buf = p;
  3813. return true;
  3814. }
  3815. }
  3816. #if WASM_ENABLE_GC == 0
  3817. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3818. set_error_buf(error_buf, error_buf_size,
  3819. "invalid reference type or unknown type");
  3820. return false;
  3821. }
  3822. *p_elem_type = elem_type;
  3823. #else
  3824. p--;
  3825. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3826. module->type_count, &need_ref_type_map,
  3827. &elem_ref_type, false, error_buf, error_buf_size)) {
  3828. return false;
  3829. }
  3830. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3831. set_error_buf(error_buf, error_buf_size,
  3832. "invalid reference type or unknown type");
  3833. return false;
  3834. }
  3835. *p_elem_type = elem_ref_type.ref_type;
  3836. if (need_ref_type_map) {
  3837. if (!(*p_elem_ref_type =
  3838. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3839. error_buf, error_buf_size))) {
  3840. return false;
  3841. }
  3842. }
  3843. #endif
  3844. *p_buf = p;
  3845. return true;
  3846. fail:
  3847. return false;
  3848. }
  3849. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3850. static bool
  3851. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3852. WASMModule *module, WASMTableSeg *table_segment,
  3853. char *error_buf, uint32 error_buf_size)
  3854. {
  3855. const uint8 *p = *p_buf, *p_end = buf_end;
  3856. uint32 function_count, function_index = 0, i;
  3857. uint64 total_size;
  3858. read_leb_uint32(p, p_end, function_count);
  3859. table_segment->value_count = function_count;
  3860. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3861. if (total_size > 0
  3862. && !(table_segment->init_values =
  3863. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3864. error_buf_size))) {
  3865. return false;
  3866. }
  3867. for (i = 0; i < function_count; i++) {
  3868. InitializerExpression *init_expr = &table_segment->init_values[i];
  3869. read_leb_uint32(p, p_end, function_index);
  3870. if (!check_function_index(module, function_index, error_buf,
  3871. error_buf_size)) {
  3872. return false;
  3873. }
  3874. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3875. init_expr->u.ref_index = function_index;
  3876. }
  3877. *p_buf = p;
  3878. return true;
  3879. fail:
  3880. return false;
  3881. }
  3882. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3883. static bool
  3884. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3885. WASMModule *module, WASMTableSeg *table_segment,
  3886. char *error_buf, uint32 error_buf_size)
  3887. {
  3888. const uint8 *p = *p_buf, *p_end = buf_end;
  3889. uint32 ref_count, i;
  3890. uint64 total_size;
  3891. read_leb_uint32(p, p_end, ref_count);
  3892. table_segment->value_count = ref_count;
  3893. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3894. if (total_size > 0
  3895. && !(table_segment->init_values =
  3896. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3897. error_buf_size))) {
  3898. return false;
  3899. }
  3900. for (i = 0; i < ref_count; i++) {
  3901. InitializerExpression *init_expr = &table_segment->init_values[i];
  3902. if (!load_init_expr(module, &p, p_end, init_expr,
  3903. table_segment->elem_type,
  3904. #if WASM_ENABLE_GC == 0
  3905. NULL,
  3906. #else
  3907. table_segment->elem_ref_type,
  3908. #endif
  3909. error_buf, error_buf_size))
  3910. return false;
  3911. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3912. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3913. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3914. && init_expr->init_expr_type
  3915. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3916. }
  3917. *p_buf = p;
  3918. return true;
  3919. fail:
  3920. return false;
  3921. }
  3922. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  3923. static bool
  3924. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3925. WASMModule *module, char *error_buf,
  3926. uint32 error_buf_size)
  3927. {
  3928. const uint8 *p = buf, *p_end = buf_end;
  3929. uint32 table_segment_count, i;
  3930. uint64 total_size;
  3931. WASMTableSeg *table_segment;
  3932. read_leb_uint32(p, p_end, table_segment_count);
  3933. if (table_segment_count) {
  3934. module->table_seg_count = table_segment_count;
  3935. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  3936. if (!(module->table_segments =
  3937. loader_malloc(total_size, error_buf, error_buf_size))) {
  3938. return false;
  3939. }
  3940. table_segment = module->table_segments;
  3941. for (i = 0; i < table_segment_count; i++, table_segment++) {
  3942. if (p >= p_end) {
  3943. set_error_buf(error_buf, error_buf_size,
  3944. "invalid value type or "
  3945. "invalid elements segment kind");
  3946. return false;
  3947. }
  3948. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3949. read_leb_uint32(p, p_end, table_segment->mode);
  3950. /* last three bits */
  3951. table_segment->mode = table_segment->mode & 0x07;
  3952. switch (table_segment->mode) {
  3953. /* elemkind/elemtype + active */
  3954. case 0:
  3955. case 4:
  3956. {
  3957. #if WASM_ENABLE_GC != 0
  3958. if (table_segment->mode == 0) {
  3959. /* vec(funcidx), set elem type to (ref func) */
  3960. WASMRefType elem_ref_type = { 0 };
  3961. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  3962. wasm_set_refheaptype_common(
  3963. &elem_ref_type.ref_ht_common, false,
  3964. HEAP_TYPE_FUNC);
  3965. if (!(table_segment->elem_ref_type = reftype_set_insert(
  3966. module->ref_type_set, &elem_ref_type,
  3967. error_buf, error_buf_size)))
  3968. return false;
  3969. }
  3970. else {
  3971. /* vec(expr), set elem type to funcref */
  3972. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  3973. }
  3974. #else
  3975. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  3976. #endif
  3977. table_segment->table_index = 0;
  3978. if (!check_table_index(module, table_segment->table_index,
  3979. error_buf, error_buf_size))
  3980. return false;
  3981. if (!load_init_expr(
  3982. module, &p, p_end, &table_segment->base_offset,
  3983. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  3984. return false;
  3985. if (table_segment->mode == 0) {
  3986. /* vec(funcidx) */
  3987. if (!load_func_index_vec(&p, p_end, module,
  3988. table_segment, error_buf,
  3989. error_buf_size))
  3990. return false;
  3991. }
  3992. else {
  3993. /* vec(expr) */
  3994. if (!load_init_expr_vec(&p, p_end, module,
  3995. table_segment, error_buf,
  3996. error_buf_size))
  3997. return false;
  3998. }
  3999. if (!check_table_elem_type(module,
  4000. table_segment->table_index,
  4001. table_segment->elem_type,
  4002. error_buf, error_buf_size))
  4003. return false;
  4004. break;
  4005. }
  4006. /* elemkind + passive/declarative */
  4007. case 1:
  4008. case 3:
  4009. if (!load_elem_type(module, &p, p_end,
  4010. &table_segment->elem_type,
  4011. #if WASM_ENABLE_GC != 0
  4012. &table_segment->elem_ref_type,
  4013. #endif
  4014. true, error_buf, error_buf_size))
  4015. return false;
  4016. /* vec(funcidx) */
  4017. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4018. error_buf, error_buf_size))
  4019. return false;
  4020. break;
  4021. /* elemkind/elemtype + table_idx + active */
  4022. case 2:
  4023. case 6:
  4024. if (!load_table_index(&p, p_end, module,
  4025. &table_segment->table_index,
  4026. error_buf, error_buf_size))
  4027. return false;
  4028. if (!load_init_expr(
  4029. module, &p, p_end, &table_segment->base_offset,
  4030. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4031. return false;
  4032. if (!load_elem_type(module, &p, p_end,
  4033. &table_segment->elem_type,
  4034. #if WASM_ENABLE_GC != 0
  4035. &table_segment->elem_ref_type,
  4036. #endif
  4037. table_segment->mode == 2 ? true : false,
  4038. error_buf, error_buf_size))
  4039. return false;
  4040. if (table_segment->mode == 2) {
  4041. /* vec(funcidx) */
  4042. if (!load_func_index_vec(&p, p_end, module,
  4043. table_segment, error_buf,
  4044. error_buf_size))
  4045. return false;
  4046. }
  4047. else {
  4048. /* vec(expr) */
  4049. if (!load_init_expr_vec(&p, p_end, module,
  4050. table_segment, error_buf,
  4051. error_buf_size))
  4052. return false;
  4053. }
  4054. if (!check_table_elem_type(module,
  4055. table_segment->table_index,
  4056. table_segment->elem_type,
  4057. error_buf, error_buf_size))
  4058. return false;
  4059. break;
  4060. case 5:
  4061. case 7:
  4062. if (!load_elem_type(module, &p, p_end,
  4063. &table_segment->elem_type,
  4064. #if WASM_ENABLE_GC != 0
  4065. &table_segment->elem_ref_type,
  4066. #endif
  4067. false, error_buf, error_buf_size))
  4068. return false;
  4069. /* vec(expr) */
  4070. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4071. error_buf, error_buf_size))
  4072. return false;
  4073. break;
  4074. default:
  4075. set_error_buf(error_buf, error_buf_size,
  4076. "unknown element segment kind");
  4077. return false;
  4078. }
  4079. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4080. /*
  4081. * like: 00 41 05 0b 04 00 01 00 01
  4082. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4083. */
  4084. if (!load_table_index(&p, p_end, module,
  4085. &table_segment->table_index, error_buf,
  4086. error_buf_size))
  4087. return false;
  4088. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4089. VALUE_TYPE_I32, NULL, error_buf,
  4090. error_buf_size))
  4091. return false;
  4092. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4093. error_buf, error_buf_size))
  4094. return false;
  4095. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4096. if (!check_table_elem_type(module, table_segment->table_index,
  4097. table_segment->elem_type, error_buf,
  4098. error_buf_size))
  4099. return false;
  4100. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4101. #if WASM_ENABLE_WAMR_COMPILER != 0
  4102. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4103. module->is_ref_types_used = true;
  4104. #endif
  4105. }
  4106. }
  4107. if (p != p_end) {
  4108. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4109. return false;
  4110. }
  4111. LOG_VERBOSE("Load table segment section success.\n");
  4112. return true;
  4113. fail:
  4114. return false;
  4115. }
  4116. static bool
  4117. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4118. WASMModule *module,
  4119. #if WASM_ENABLE_BULK_MEMORY != 0
  4120. bool has_datacount_section,
  4121. #endif
  4122. bool clone_data_seg, char *error_buf,
  4123. uint32 error_buf_size)
  4124. {
  4125. const uint8 *p = buf, *p_end = buf_end;
  4126. uint32 data_seg_count, i, mem_index, data_seg_len;
  4127. uint64 total_size;
  4128. WASMDataSeg *dataseg;
  4129. InitializerExpression init_expr;
  4130. #if WASM_ENABLE_BULK_MEMORY != 0
  4131. bool is_passive = false;
  4132. uint32 mem_flag;
  4133. #endif
  4134. uint8 mem_offset_type = VALUE_TYPE_I32;
  4135. read_leb_uint32(p, p_end, data_seg_count);
  4136. #if WASM_ENABLE_BULK_MEMORY != 0
  4137. if (has_datacount_section && data_seg_count != module->data_seg_count1) {
  4138. set_error_buf(error_buf, error_buf_size,
  4139. "data count and data section have inconsistent lengths");
  4140. return false;
  4141. }
  4142. #endif
  4143. if (data_seg_count) {
  4144. module->data_seg_count = data_seg_count;
  4145. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4146. if (!(module->data_segments =
  4147. loader_malloc(total_size, error_buf, error_buf_size))) {
  4148. return false;
  4149. }
  4150. for (i = 0; i < data_seg_count; i++) {
  4151. read_leb_uint32(p, p_end, mem_index);
  4152. #if WASM_ENABLE_BULK_MEMORY != 0
  4153. is_passive = false;
  4154. mem_flag = mem_index & 0x03;
  4155. switch (mem_flag) {
  4156. case 0x01:
  4157. is_passive = true;
  4158. #if WASM_ENABLE_WAMR_COMPILER != 0
  4159. module->is_bulk_memory_used = true;
  4160. #endif
  4161. break;
  4162. case 0x00:
  4163. /* no memory index, treat index as 0 */
  4164. mem_index = 0;
  4165. goto check_mem_index;
  4166. case 0x02:
  4167. /* read following memory index */
  4168. read_leb_uint32(p, p_end, mem_index);
  4169. #if WASM_ENABLE_WAMR_COMPILER != 0
  4170. module->is_bulk_memory_used = true;
  4171. #endif
  4172. check_mem_index:
  4173. if (mem_index
  4174. >= module->import_memory_count + module->memory_count) {
  4175. set_error_buf_v(error_buf, error_buf_size,
  4176. "unknown memory %d", mem_index);
  4177. return false;
  4178. }
  4179. break;
  4180. case 0x03:
  4181. default:
  4182. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4183. return false;
  4184. break;
  4185. }
  4186. #else
  4187. if (mem_index
  4188. >= module->import_memory_count + module->memory_count) {
  4189. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4190. mem_index);
  4191. return false;
  4192. }
  4193. #endif /* WASM_ENABLE_BULK_MEMORY */
  4194. #if WASM_ENABLE_BULK_MEMORY != 0
  4195. if (!is_passive)
  4196. #endif
  4197. {
  4198. #if WASM_ENABLE_MEMORY64 != 0
  4199. /* This memory_flag is from memory instead of data segment */
  4200. uint8 memory_flag;
  4201. if (module->import_memory_count > 0) {
  4202. memory_flag = module->import_memories[mem_index]
  4203. .u.memory.mem_type.flags;
  4204. }
  4205. else {
  4206. memory_flag =
  4207. module
  4208. ->memories[mem_index - module->import_memory_count]
  4209. .flags;
  4210. }
  4211. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4212. : VALUE_TYPE_I32;
  4213. #else
  4214. mem_offset_type = VALUE_TYPE_I32;
  4215. #endif
  4216. }
  4217. #if WASM_ENABLE_BULK_MEMORY != 0
  4218. if (!is_passive)
  4219. #endif
  4220. if (!load_init_expr(module, &p, p_end, &init_expr,
  4221. mem_offset_type, NULL, error_buf,
  4222. error_buf_size))
  4223. return false;
  4224. read_leb_uint32(p, p_end, data_seg_len);
  4225. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4226. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4227. return false;
  4228. }
  4229. #if WASM_ENABLE_BULK_MEMORY != 0
  4230. dataseg->is_passive = is_passive;
  4231. if (!is_passive)
  4232. #endif
  4233. {
  4234. bh_memcpy_s(&dataseg->base_offset,
  4235. sizeof(InitializerExpression), &init_expr,
  4236. sizeof(InitializerExpression));
  4237. dataseg->memory_index = mem_index;
  4238. }
  4239. dataseg->data_length = data_seg_len;
  4240. CHECK_BUF(p, p_end, data_seg_len);
  4241. if (clone_data_seg) {
  4242. if (!(dataseg->data = loader_malloc(
  4243. dataseg->data_length, error_buf, error_buf_size))) {
  4244. return false;
  4245. }
  4246. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4247. data_seg_len);
  4248. }
  4249. else {
  4250. dataseg->data = (uint8 *)p;
  4251. }
  4252. dataseg->is_data_cloned = clone_data_seg;
  4253. p += data_seg_len;
  4254. }
  4255. }
  4256. if (p != p_end) {
  4257. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4258. return false;
  4259. }
  4260. LOG_VERBOSE("Load data segment section success.\n");
  4261. return true;
  4262. fail:
  4263. return false;
  4264. }
  4265. #if WASM_ENABLE_BULK_MEMORY != 0
  4266. static bool
  4267. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4268. WASMModule *module, char *error_buf,
  4269. uint32 error_buf_size)
  4270. {
  4271. const uint8 *p = buf, *p_end = buf_end;
  4272. uint32 data_seg_count1 = 0;
  4273. read_leb_uint32(p, p_end, data_seg_count1);
  4274. module->data_seg_count1 = data_seg_count1;
  4275. if (p != p_end) {
  4276. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4277. return false;
  4278. }
  4279. #if WASM_ENABLE_WAMR_COMPILER != 0
  4280. module->is_bulk_memory_used = true;
  4281. #endif
  4282. LOG_VERBOSE("Load datacount section success.\n");
  4283. return true;
  4284. fail:
  4285. return false;
  4286. }
  4287. #endif
  4288. #if WASM_ENABLE_TAGS != 0
  4289. static bool
  4290. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4291. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4292. uint32 error_buf_size)
  4293. {
  4294. (void)buf_code;
  4295. (void)buf_code_end;
  4296. const uint8 *p = buf, *p_end = buf_end;
  4297. size_t total_size = 0;
  4298. /* number of tags defined in the section */
  4299. uint32 section_tag_count = 0;
  4300. uint8 tag_attribute;
  4301. uint32 tag_type;
  4302. WASMTag *tag = NULL;
  4303. /* get tag count */
  4304. read_leb_uint32(p, p_end, section_tag_count);
  4305. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4306. error_buf, error_buf_size))
  4307. return false;
  4308. module->tag_count = section_tag_count;
  4309. if (section_tag_count) {
  4310. total_size = sizeof(WASMTag *) * module->tag_count;
  4311. if (!(module->tags =
  4312. loader_malloc(total_size, error_buf, error_buf_size))) {
  4313. return false;
  4314. }
  4315. /* load each tag, imported tags precede the tags */
  4316. uint32 tag_index;
  4317. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4318. /* get the one byte attribute */
  4319. CHECK_BUF(p, p_end, 1);
  4320. tag_attribute = read_uint8(p);
  4321. /* get type */
  4322. read_leb_uint32(p, p_end, tag_type);
  4323. /* compare against module->types */
  4324. if (tag_type >= module->type_count) {
  4325. set_error_buf(error_buf, error_buf_size, "unknown type");
  4326. return false;
  4327. }
  4328. /* get return type (must be 0) */
  4329. /* check, that the type of the referred tag returns void */
  4330. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4331. if (func_type->result_count != 0) {
  4332. set_error_buf(error_buf, error_buf_size,
  4333. "non-empty tag result type");
  4334. goto fail;
  4335. }
  4336. if (!(tag = module->tags[tag_index] = loader_malloc(
  4337. sizeof(WASMTag), error_buf, error_buf_size))) {
  4338. return false;
  4339. }
  4340. /* store to module tag declarations */
  4341. tag->attribute = tag_attribute;
  4342. tag->type = tag_type;
  4343. tag->tag_type = func_type;
  4344. }
  4345. }
  4346. if (p != p_end) {
  4347. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4348. return false;
  4349. }
  4350. LOG_VERBOSE("Load tag section success.\n");
  4351. return true;
  4352. fail:
  4353. return false;
  4354. }
  4355. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4356. static bool
  4357. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4358. const uint8 *buf_func_end, WASMModule *module,
  4359. char *error_buf, uint32 error_buf_size)
  4360. {
  4361. const uint8 *p = buf, *p_end = buf_end;
  4362. const uint8 *p_func = buf_func;
  4363. uint32 func_count = 0, code_count;
  4364. /* code has been loaded in function section, so pass it here, just check
  4365. * whether function and code section have inconsistent lengths */
  4366. read_leb_uint32(p, p_end, code_count);
  4367. if (buf_func)
  4368. read_leb_uint32(p_func, buf_func_end, func_count);
  4369. if (func_count != code_count) {
  4370. set_error_buf(error_buf, error_buf_size,
  4371. "function and code section have inconsistent lengths");
  4372. return false;
  4373. }
  4374. LOG_VERBOSE("Load code segment section success.\n");
  4375. (void)module;
  4376. return true;
  4377. fail:
  4378. return false;
  4379. }
  4380. static bool
  4381. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4382. char *error_buf, uint32 error_buf_size)
  4383. {
  4384. const uint8 *p = buf, *p_end = buf_end;
  4385. WASMFuncType *type;
  4386. uint32 start_function;
  4387. read_leb_uint32(p, p_end, start_function);
  4388. if (start_function
  4389. >= module->function_count + module->import_function_count) {
  4390. set_error_buf(error_buf, error_buf_size, "unknown function");
  4391. return false;
  4392. }
  4393. if (start_function < module->import_function_count)
  4394. type = module->import_functions[start_function].u.function.func_type;
  4395. else
  4396. type = module->functions[start_function - module->import_function_count]
  4397. ->func_type;
  4398. if (type->param_count != 0 || type->result_count != 0) {
  4399. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4400. return false;
  4401. }
  4402. module->start_function = start_function;
  4403. if (p != p_end) {
  4404. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4405. return false;
  4406. }
  4407. LOG_VERBOSE("Load start section success.\n");
  4408. return true;
  4409. fail:
  4410. return false;
  4411. }
  4412. #if WASM_ENABLE_STRINGREF != 0
  4413. static bool
  4414. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4415. WASMModule *module, bool is_load_from_file_buf,
  4416. char *error_buf, uint32 error_buf_size)
  4417. {
  4418. const uint8 *p = buf, *p_end = buf_end;
  4419. int32 deferred_count, immediate_count, string_length, i;
  4420. uint64 total_size;
  4421. read_leb_uint32(p, p_end, deferred_count);
  4422. read_leb_uint32(p, p_end, immediate_count);
  4423. /* proposal set deferred_count for future extension */
  4424. if (deferred_count != 0) {
  4425. goto fail;
  4426. }
  4427. if (immediate_count > 0) {
  4428. total_size = sizeof(char *) * (uint64)immediate_count;
  4429. if (!(module->string_literal_ptrs =
  4430. loader_malloc(total_size, error_buf, error_buf_size))) {
  4431. goto fail;
  4432. }
  4433. module->string_literal_count = immediate_count;
  4434. total_size = sizeof(uint32) * (uint64)immediate_count;
  4435. if (!(module->string_literal_lengths =
  4436. loader_malloc(total_size, error_buf, error_buf_size))) {
  4437. goto fail;
  4438. }
  4439. for (i = 0; i < immediate_count; i++) {
  4440. read_leb_uint32(p, p_end, string_length);
  4441. CHECK_BUF(p, p_end, string_length);
  4442. module->string_literal_ptrs[i] = p;
  4443. module->string_literal_lengths[i] = string_length;
  4444. p += string_length;
  4445. }
  4446. }
  4447. if (p != p_end) {
  4448. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4449. goto fail;
  4450. }
  4451. LOG_VERBOSE("Load stringref section success.\n");
  4452. return true;
  4453. fail:
  4454. return false;
  4455. }
  4456. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4457. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4458. static bool
  4459. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4460. bool is_load_from_file_buf, char *error_buf,
  4461. uint32 error_buf_size)
  4462. {
  4463. const uint8 *p = buf, *p_end = buf_end;
  4464. uint32 name_type, subsection_size;
  4465. uint32 previous_name_type = 0;
  4466. uint32 num_func_name;
  4467. uint32 func_index;
  4468. uint32 previous_func_index = ~0U;
  4469. uint32 func_name_len;
  4470. uint32 name_index;
  4471. int i = 0;
  4472. if (p >= p_end) {
  4473. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4474. return false;
  4475. }
  4476. while (p < p_end) {
  4477. read_leb_uint32(p, p_end, name_type);
  4478. if (i != 0) {
  4479. if (name_type == previous_name_type) {
  4480. set_error_buf(error_buf, error_buf_size,
  4481. "duplicate sub-section");
  4482. return false;
  4483. }
  4484. if (name_type < previous_name_type) {
  4485. set_error_buf(error_buf, error_buf_size,
  4486. "out-of-order sub-section");
  4487. return false;
  4488. }
  4489. }
  4490. previous_name_type = name_type;
  4491. read_leb_uint32(p, p_end, subsection_size);
  4492. CHECK_BUF(p, p_end, subsection_size);
  4493. switch (name_type) {
  4494. case SUB_SECTION_TYPE_FUNC:
  4495. if (subsection_size) {
  4496. read_leb_uint32(p, p_end, num_func_name);
  4497. for (name_index = 0; name_index < num_func_name;
  4498. name_index++) {
  4499. read_leb_uint32(p, p_end, func_index);
  4500. if (func_index == previous_func_index) {
  4501. set_error_buf(error_buf, error_buf_size,
  4502. "duplicate function name");
  4503. return false;
  4504. }
  4505. if (func_index < previous_func_index
  4506. && previous_func_index != ~0U) {
  4507. set_error_buf(error_buf, error_buf_size,
  4508. "out-of-order function index ");
  4509. return false;
  4510. }
  4511. previous_func_index = func_index;
  4512. read_leb_uint32(p, p_end, func_name_len);
  4513. CHECK_BUF(p, p_end, func_name_len);
  4514. /* Skip the import functions */
  4515. if (func_index >= module->import_function_count) {
  4516. func_index -= module->import_function_count;
  4517. if (func_index >= module->function_count) {
  4518. set_error_buf(error_buf, error_buf_size,
  4519. "out-of-range function index");
  4520. return false;
  4521. }
  4522. if (!(module->functions[func_index]->field_name =
  4523. wasm_const_str_list_insert(
  4524. p, func_name_len, module,
  4525. #if WASM_ENABLE_WAMR_COMPILER != 0
  4526. false,
  4527. #else
  4528. is_load_from_file_buf,
  4529. #endif
  4530. error_buf, error_buf_size))) {
  4531. return false;
  4532. }
  4533. }
  4534. p += func_name_len;
  4535. }
  4536. }
  4537. break;
  4538. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4539. */
  4540. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4541. default:
  4542. p = p + subsection_size;
  4543. break;
  4544. }
  4545. i++;
  4546. }
  4547. return true;
  4548. fail:
  4549. return false;
  4550. }
  4551. #endif
  4552. static bool
  4553. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4554. bool is_load_from_file_buf, char *error_buf,
  4555. uint32 error_buf_size)
  4556. {
  4557. const uint8 *p = buf, *p_end = buf_end;
  4558. char section_name[32];
  4559. uint32 name_len, buffer_len;
  4560. if (p >= p_end) {
  4561. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4562. return false;
  4563. }
  4564. read_leb_uint32(p, p_end, name_len);
  4565. if (p + name_len > p_end) {
  4566. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4567. return false;
  4568. }
  4569. if (!wasm_check_utf8_str(p, name_len)) {
  4570. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4571. return false;
  4572. }
  4573. buffer_len = sizeof(section_name);
  4574. memset(section_name, 0, buffer_len);
  4575. if (name_len < buffer_len) {
  4576. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4577. }
  4578. else {
  4579. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4580. memset(section_name + buffer_len - 4, '.', 3);
  4581. }
  4582. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4583. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4584. module->name_section_buf = buf;
  4585. module->name_section_buf_end = buf_end;
  4586. p += name_len;
  4587. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4588. error_buf, error_buf_size)) {
  4589. return false;
  4590. }
  4591. LOG_VERBOSE("Load custom name section success.");
  4592. }
  4593. #endif
  4594. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4595. {
  4596. WASMCustomSection *section =
  4597. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4598. if (!section) {
  4599. return false;
  4600. }
  4601. section->name_addr = (char *)p;
  4602. section->name_len = name_len;
  4603. section->content_addr = (uint8 *)(p + name_len);
  4604. section->content_len = (uint32)(p_end - p - name_len);
  4605. section->next = module->custom_section_list;
  4606. module->custom_section_list = section;
  4607. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4608. return true;
  4609. }
  4610. #endif
  4611. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4612. (void)is_load_from_file_buf;
  4613. (void)module;
  4614. return true;
  4615. fail:
  4616. return false;
  4617. }
  4618. static void
  4619. calculate_global_data_offset(WASMModule *module)
  4620. {
  4621. uint32 i, data_offset;
  4622. data_offset = 0;
  4623. for (i = 0; i < module->import_global_count; i++) {
  4624. WASMGlobalImport *import_global =
  4625. &((module->import_globals + i)->u.global);
  4626. #if WASM_ENABLE_FAST_JIT != 0
  4627. import_global->data_offset = data_offset;
  4628. #endif
  4629. data_offset += wasm_value_type_size(import_global->type.val_type);
  4630. }
  4631. for (i = 0; i < module->global_count; i++) {
  4632. WASMGlobal *global = module->globals + i;
  4633. #if WASM_ENABLE_FAST_JIT != 0
  4634. global->data_offset = data_offset;
  4635. #endif
  4636. data_offset += wasm_value_type_size(global->type.val_type);
  4637. }
  4638. module->global_data_size = data_offset;
  4639. }
  4640. #if WASM_ENABLE_FAST_JIT != 0
  4641. static bool
  4642. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4643. uint32 error_buf_size)
  4644. {
  4645. #if WASM_ENABLE_LAZY_JIT != 0
  4646. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4647. #endif
  4648. uint32 i;
  4649. if (!module->function_count)
  4650. return true;
  4651. if (!(module->fast_jit_func_ptrs =
  4652. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4653. error_buf_size))) {
  4654. return false;
  4655. }
  4656. #if WASM_ENABLE_LAZY_JIT != 0
  4657. for (i = 0; i < module->function_count; i++) {
  4658. module->fast_jit_func_ptrs[i] =
  4659. jit_globals->compile_fast_jit_and_then_call;
  4660. }
  4661. #endif
  4662. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4663. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4664. set_error_buf(error_buf, error_buf_size,
  4665. "init fast jit thread lock failed");
  4666. return false;
  4667. }
  4668. module->fast_jit_thread_locks_inited[i] = true;
  4669. }
  4670. return true;
  4671. }
  4672. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4673. #if WASM_ENABLE_JIT != 0
  4674. static bool
  4675. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4676. uint32 error_buf_size)
  4677. {
  4678. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4679. AOTCompOption option = { 0 };
  4680. char *aot_last_error;
  4681. uint64 size;
  4682. #if WASM_ENABLE_GC != 0
  4683. bool gc_enabled = true;
  4684. #else
  4685. bool gc_enabled = false;
  4686. #endif
  4687. if (module->function_count == 0)
  4688. return true;
  4689. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4690. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4691. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4692. return false;
  4693. }
  4694. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4695. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4696. os_mutex_destroy(&module->tierup_wait_lock);
  4697. return false;
  4698. }
  4699. module->tierup_wait_lock_inited = true;
  4700. #endif
  4701. size = sizeof(void *) * (uint64)module->function_count
  4702. + sizeof(bool) * (uint64)module->function_count;
  4703. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4704. return false;
  4705. }
  4706. module->func_ptrs_compiled =
  4707. (bool *)((uint8 *)module->func_ptrs
  4708. + sizeof(void *) * module->function_count);
  4709. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4710. if (!module->comp_data) {
  4711. aot_last_error = aot_get_last_error();
  4712. bh_assert(aot_last_error != NULL);
  4713. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4714. return false;
  4715. }
  4716. option.is_jit_mode = true;
  4717. option.opt_level = llvm_jit_options->opt_level;
  4718. option.size_level = llvm_jit_options->size_level;
  4719. option.segue_flags = llvm_jit_options->segue_flags;
  4720. option.quick_invoke_c_api_import =
  4721. llvm_jit_options->quick_invoke_c_api_import;
  4722. #if WASM_ENABLE_BULK_MEMORY != 0
  4723. option.enable_bulk_memory = true;
  4724. #endif
  4725. #if WASM_ENABLE_THREAD_MGR != 0
  4726. option.enable_thread_mgr = true;
  4727. #endif
  4728. #if WASM_ENABLE_TAIL_CALL != 0
  4729. option.enable_tail_call = true;
  4730. #endif
  4731. #if WASM_ENABLE_SIMD != 0
  4732. option.enable_simd = true;
  4733. #endif
  4734. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4735. option.enable_ref_types = true;
  4736. #elif WASM_ENABLE_GC != 0
  4737. option.enable_gc = true;
  4738. #endif
  4739. option.enable_aux_stack_check = true;
  4740. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4741. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4742. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  4743. aot_call_stack_features_init_default(&option.call_stack_features);
  4744. #endif
  4745. #if WASM_ENABLE_PERF_PROFILING != 0
  4746. option.enable_perf_profiling = true;
  4747. #endif
  4748. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4749. option.enable_memory_profiling = true;
  4750. option.enable_stack_estimation = true;
  4751. #endif
  4752. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4753. if (!module->comp_ctx) {
  4754. aot_last_error = aot_get_last_error();
  4755. bh_assert(aot_last_error != NULL);
  4756. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4757. return false;
  4758. }
  4759. return true;
  4760. }
  4761. static bool
  4762. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4763. uint32 error_buf_size)
  4764. {
  4765. char *aot_last_error;
  4766. uint32 i;
  4767. if (module->function_count == 0)
  4768. return true;
  4769. if (!aot_compile_wasm(module->comp_ctx)) {
  4770. aot_last_error = aot_get_last_error();
  4771. bh_assert(aot_last_error != NULL);
  4772. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4773. return false;
  4774. }
  4775. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4776. if (module->orcjit_stop_compiling)
  4777. return false;
  4778. #endif
  4779. bh_print_time("Begin to lookup llvm jit functions");
  4780. for (i = 0; i < module->function_count; i++) {
  4781. LLVMOrcJITTargetAddress func_addr = 0;
  4782. LLVMErrorRef error;
  4783. char func_name[48];
  4784. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4785. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4786. func_name);
  4787. if (error != LLVMErrorSuccess) {
  4788. char *err_msg = LLVMGetErrorMessage(error);
  4789. set_error_buf_v(error_buf, error_buf_size,
  4790. "failed to compile llvm jit function: %s", err_msg);
  4791. LLVMDisposeErrorMessage(err_msg);
  4792. return false;
  4793. }
  4794. /**
  4795. * No need to lock the func_ptr[func_idx] here as it is basic
  4796. * data type, the load/store for it can be finished by one cpu
  4797. * instruction, and there can be only one cpu instruction
  4798. * loading/storing at the same time.
  4799. */
  4800. module->func_ptrs[i] = (void *)func_addr;
  4801. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4802. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4803. if (module->orcjit_stop_compiling)
  4804. return false;
  4805. #endif
  4806. }
  4807. bh_print_time("End lookup llvm jit functions");
  4808. return true;
  4809. }
  4810. #endif /* end of WASM_ENABLE_JIT != 0 */
  4811. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4812. && WASM_ENABLE_LAZY_JIT != 0
  4813. static void *
  4814. init_llvm_jit_functions_stage2_callback(void *arg)
  4815. {
  4816. WASMModule *module = (WASMModule *)arg;
  4817. char error_buf[128];
  4818. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4819. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4820. module->orcjit_stop_compiling = true;
  4821. return NULL;
  4822. }
  4823. os_mutex_lock(&module->tierup_wait_lock);
  4824. module->llvm_jit_inited = true;
  4825. os_cond_broadcast(&module->tierup_wait_cond);
  4826. os_mutex_unlock(&module->tierup_wait_lock);
  4827. return NULL;
  4828. }
  4829. #endif
  4830. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4831. /* The callback function to compile jit functions */
  4832. static void *
  4833. orcjit_thread_callback(void *arg)
  4834. {
  4835. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4836. #if WASM_ENABLE_JIT != 0
  4837. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4838. #endif
  4839. WASMModule *module = thread_arg->module;
  4840. uint32 group_idx = thread_arg->group_idx;
  4841. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4842. uint32 func_count = module->function_count;
  4843. uint32 i;
  4844. #if WASM_ENABLE_FAST_JIT != 0
  4845. /* Compile fast jit functions of this group */
  4846. for (i = group_idx; i < func_count; i += group_stride) {
  4847. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4848. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4849. break;
  4850. }
  4851. if (module->orcjit_stop_compiling) {
  4852. return NULL;
  4853. }
  4854. }
  4855. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4856. os_mutex_lock(&module->tierup_wait_lock);
  4857. module->fast_jit_ready_groups++;
  4858. os_mutex_unlock(&module->tierup_wait_lock);
  4859. #endif
  4860. #endif
  4861. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4862. && WASM_ENABLE_LAZY_JIT != 0
  4863. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4864. for (i = group_idx; i < func_count;
  4865. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4866. uint32 j;
  4867. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4868. if (i + j * group_stride < func_count) {
  4869. if (!jit_compiler_set_call_to_fast_jit(
  4870. module,
  4871. i + j * group_stride + module->import_function_count)) {
  4872. LOG_ERROR(
  4873. "failed to compile call_to_fast_jit for func %u\n",
  4874. i + j * group_stride + module->import_function_count);
  4875. module->orcjit_stop_compiling = true;
  4876. return NULL;
  4877. }
  4878. }
  4879. if (module->orcjit_stop_compiling) {
  4880. return NULL;
  4881. }
  4882. }
  4883. }
  4884. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4885. fast jit functions are compiled */
  4886. os_mutex_lock(&module->tierup_wait_lock);
  4887. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4888. && module->fast_jit_ready_groups >= group_stride)) {
  4889. os_cond_reltimedwait(&module->tierup_wait_cond,
  4890. &module->tierup_wait_lock, 10000);
  4891. if (module->orcjit_stop_compiling) {
  4892. /* init_llvm_jit_functions_stage2 failed */
  4893. os_mutex_unlock(&module->tierup_wait_lock);
  4894. return NULL;
  4895. }
  4896. }
  4897. os_mutex_unlock(&module->tierup_wait_lock);
  4898. #endif
  4899. #if WASM_ENABLE_JIT != 0
  4900. /* Compile llvm jit functions of this group */
  4901. for (i = group_idx; i < func_count;
  4902. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4903. LLVMOrcJITTargetAddress func_addr = 0;
  4904. LLVMErrorRef error;
  4905. char func_name[48];
  4906. typedef void (*F)(void);
  4907. union {
  4908. F f;
  4909. void *v;
  4910. } u;
  4911. uint32 j;
  4912. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  4913. "_wrapper");
  4914. LOG_DEBUG("compile llvm jit func %s", func_name);
  4915. error =
  4916. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  4917. if (error != LLVMErrorSuccess) {
  4918. char *err_msg = LLVMGetErrorMessage(error);
  4919. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  4920. LLVMDisposeErrorMessage(err_msg);
  4921. break;
  4922. }
  4923. /* Call the jit wrapper function to trigger its compilation, so as
  4924. to compile the actual jit functions, since we add the latter to
  4925. function list in the PartitionFunction callback */
  4926. u.v = (void *)func_addr;
  4927. u.f();
  4928. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4929. if (i + j * group_stride < func_count) {
  4930. module->func_ptrs_compiled[i + j * group_stride] = true;
  4931. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4932. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  4933. i + j * group_stride);
  4934. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  4935. func_name);
  4936. if (error != LLVMErrorSuccess) {
  4937. char *err_msg = LLVMGetErrorMessage(error);
  4938. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  4939. err_msg);
  4940. LLVMDisposeErrorMessage(err_msg);
  4941. /* Ignore current llvm jit func, as its func ptr is
  4942. previous set to call_to_fast_jit, which also works */
  4943. continue;
  4944. }
  4945. jit_compiler_set_llvm_jit_func_ptr(
  4946. module,
  4947. i + j * group_stride + module->import_function_count,
  4948. (void *)func_addr);
  4949. /* Try to switch to call this llvm jit function instead of
  4950. fast jit function from fast jit jitted code */
  4951. jit_compiler_set_call_to_llvm_jit(
  4952. module,
  4953. i + j * group_stride + module->import_function_count);
  4954. #endif
  4955. }
  4956. }
  4957. if (module->orcjit_stop_compiling) {
  4958. break;
  4959. }
  4960. }
  4961. #endif
  4962. return NULL;
  4963. }
  4964. static void
  4965. orcjit_stop_compile_threads(WASMModule *module)
  4966. {
  4967. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  4968. / sizeof(OrcJitThreadArg));
  4969. module->orcjit_stop_compiling = true;
  4970. for (i = 0; i < thread_num; i++) {
  4971. if (module->orcjit_threads[i])
  4972. os_thread_join(module->orcjit_threads[i], NULL);
  4973. }
  4974. }
  4975. static bool
  4976. compile_jit_functions(WASMModule *module, char *error_buf,
  4977. uint32 error_buf_size)
  4978. {
  4979. uint32 thread_num =
  4980. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  4981. uint32 i, j;
  4982. bh_print_time("Begin to compile jit functions");
  4983. /* Create threads to compile the jit functions */
  4984. for (i = 0; i < thread_num && i < module->function_count; i++) {
  4985. #if WASM_ENABLE_JIT != 0
  4986. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  4987. #endif
  4988. module->orcjit_thread_args[i].module = module;
  4989. module->orcjit_thread_args[i].group_idx = i;
  4990. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  4991. (void *)&module->orcjit_thread_args[i],
  4992. APP_THREAD_STACK_SIZE_DEFAULT)
  4993. != 0) {
  4994. set_error_buf(error_buf, error_buf_size,
  4995. "create orcjit compile thread failed");
  4996. /* Terminate the threads created */
  4997. module->orcjit_stop_compiling = true;
  4998. for (j = 0; j < i; j++) {
  4999. os_thread_join(module->orcjit_threads[j], NULL);
  5000. }
  5001. return false;
  5002. }
  5003. }
  5004. #if WASM_ENABLE_LAZY_JIT == 0
  5005. /* Wait until all jit functions are compiled for eager mode */
  5006. for (i = 0; i < thread_num; i++) {
  5007. if (module->orcjit_threads[i])
  5008. os_thread_join(module->orcjit_threads[i], NULL);
  5009. }
  5010. #if WASM_ENABLE_FAST_JIT != 0
  5011. /* Ensure all the fast-jit functions are compiled */
  5012. for (i = 0; i < module->function_count; i++) {
  5013. if (!jit_compiler_is_compiled(module,
  5014. i + module->import_function_count)) {
  5015. set_error_buf(error_buf, error_buf_size,
  5016. "failed to compile fast jit function");
  5017. return false;
  5018. }
  5019. }
  5020. #endif
  5021. #if WASM_ENABLE_JIT != 0
  5022. /* Ensure all the llvm-jit functions are compiled */
  5023. for (i = 0; i < module->function_count; i++) {
  5024. if (!module->func_ptrs_compiled[i]) {
  5025. set_error_buf(error_buf, error_buf_size,
  5026. "failed to compile llvm jit function");
  5027. return false;
  5028. }
  5029. }
  5030. #endif
  5031. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5032. bh_print_time("End compile jit functions");
  5033. return true;
  5034. }
  5035. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5036. static bool
  5037. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5038. uint32 cur_func_idx, char *error_buf,
  5039. uint32 error_buf_size);
  5040. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5041. void **
  5042. wasm_interp_get_handle_table(void);
  5043. static void **handle_table;
  5044. #endif
  5045. static bool
  5046. load_from_sections(WASMModule *module, WASMSection *sections,
  5047. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5048. bool no_resolve, char *error_buf, uint32 error_buf_size)
  5049. {
  5050. WASMExport *export;
  5051. WASMSection *section = sections;
  5052. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5053. *buf_func = NULL, *buf_func_end = NULL;
  5054. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5055. WASMGlobal *aux_stack_top_global = NULL, *global;
  5056. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5057. aux_stack_top = (uint64)-1LL;
  5058. uint32 global_index, func_index, i;
  5059. uint32 aux_data_end_global_index = (uint32)-1;
  5060. uint32 aux_heap_base_global_index = (uint32)-1;
  5061. WASMFuncType *func_type;
  5062. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5063. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5064. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5065. #if WASM_ENABLE_BULK_MEMORY != 0
  5066. bool has_datacount_section = false;
  5067. #endif
  5068. /* Find code and function sections if have */
  5069. while (section) {
  5070. if (section->section_type == SECTION_TYPE_CODE) {
  5071. buf_code = section->section_body;
  5072. buf_code_end = buf_code + section->section_body_size;
  5073. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5074. module->buf_code = (uint8 *)buf_code;
  5075. module->buf_code_size = section->section_body_size;
  5076. #endif
  5077. }
  5078. else if (section->section_type == SECTION_TYPE_FUNC) {
  5079. buf_func = section->section_body;
  5080. buf_func_end = buf_func + section->section_body_size;
  5081. }
  5082. section = section->next;
  5083. }
  5084. section = sections;
  5085. while (section) {
  5086. buf = section->section_body;
  5087. buf_end = buf + section->section_body_size;
  5088. switch (section->section_type) {
  5089. case SECTION_TYPE_USER:
  5090. /* unsupported user section, ignore it. */
  5091. if (!load_user_section(buf, buf_end, module,
  5092. reuse_const_strings, error_buf,
  5093. error_buf_size))
  5094. return false;
  5095. break;
  5096. case SECTION_TYPE_TYPE:
  5097. if (!load_type_section(buf, buf_end, module, error_buf,
  5098. error_buf_size))
  5099. return false;
  5100. break;
  5101. case SECTION_TYPE_IMPORT:
  5102. if (!load_import_section(buf, buf_end, module,
  5103. reuse_const_strings, no_resolve,
  5104. error_buf, error_buf_size))
  5105. return false;
  5106. break;
  5107. case SECTION_TYPE_FUNC:
  5108. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5109. module, error_buf, error_buf_size))
  5110. return false;
  5111. break;
  5112. case SECTION_TYPE_TABLE:
  5113. if (!load_table_section(buf, buf_end, module, error_buf,
  5114. error_buf_size))
  5115. return false;
  5116. break;
  5117. case SECTION_TYPE_MEMORY:
  5118. if (!load_memory_section(buf, buf_end, module, error_buf,
  5119. error_buf_size))
  5120. return false;
  5121. break;
  5122. #if WASM_ENABLE_TAGS != 0
  5123. case SECTION_TYPE_TAG:
  5124. /* load tag declaration section */
  5125. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5126. module, error_buf, error_buf_size))
  5127. return false;
  5128. break;
  5129. #endif
  5130. case SECTION_TYPE_GLOBAL:
  5131. if (!load_global_section(buf, buf_end, module, error_buf,
  5132. error_buf_size))
  5133. return false;
  5134. break;
  5135. case SECTION_TYPE_EXPORT:
  5136. if (!load_export_section(buf, buf_end, module,
  5137. reuse_const_strings, error_buf,
  5138. error_buf_size))
  5139. return false;
  5140. break;
  5141. case SECTION_TYPE_START:
  5142. if (!load_start_section(buf, buf_end, module, error_buf,
  5143. error_buf_size))
  5144. return false;
  5145. break;
  5146. case SECTION_TYPE_ELEM:
  5147. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5148. error_buf_size))
  5149. return false;
  5150. break;
  5151. case SECTION_TYPE_CODE:
  5152. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5153. module, error_buf, error_buf_size))
  5154. return false;
  5155. break;
  5156. case SECTION_TYPE_DATA:
  5157. if (!load_data_segment_section(buf, buf_end, module,
  5158. #if WASM_ENABLE_BULK_MEMORY != 0
  5159. has_datacount_section,
  5160. #endif
  5161. clone_data_seg, error_buf,
  5162. error_buf_size))
  5163. return false;
  5164. break;
  5165. #if WASM_ENABLE_BULK_MEMORY != 0
  5166. case SECTION_TYPE_DATACOUNT:
  5167. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5168. error_buf_size))
  5169. return false;
  5170. has_datacount_section = true;
  5171. break;
  5172. #endif
  5173. #if WASM_ENABLE_STRINGREF != 0
  5174. case SECTION_TYPE_STRINGREF:
  5175. if (!load_stringref_section(buf, buf_end, module,
  5176. reuse_const_strings, error_buf,
  5177. error_buf_size))
  5178. return false;
  5179. break;
  5180. #endif
  5181. default:
  5182. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5183. return false;
  5184. }
  5185. section = section->next;
  5186. }
  5187. module->aux_data_end_global_index = (uint32)-1;
  5188. module->aux_heap_base_global_index = (uint32)-1;
  5189. module->aux_stack_top_global_index = (uint32)-1;
  5190. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5191. export = module->exports;
  5192. for (i = 0; i < module->export_count; i++, export ++) {
  5193. if (export->kind == EXPORT_KIND_GLOBAL) {
  5194. if (!strcmp(export->name, "__heap_base")) {
  5195. global_index = export->index - module->import_global_count;
  5196. global = module->globals + global_index;
  5197. if (global->type.val_type == VALUE_TYPE_I32
  5198. && !global->type.is_mutable
  5199. && global->init_expr.init_expr_type
  5200. == INIT_EXPR_TYPE_I32_CONST) {
  5201. aux_heap_base_global = global;
  5202. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5203. aux_heap_base_global_index = export->index;
  5204. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5205. aux_heap_base);
  5206. }
  5207. }
  5208. else if (!strcmp(export->name, "__data_end")) {
  5209. global_index = export->index - module->import_global_count;
  5210. global = module->globals + global_index;
  5211. if (global->type.val_type == VALUE_TYPE_I32
  5212. && !global->type.is_mutable
  5213. && global->init_expr.init_expr_type
  5214. == INIT_EXPR_TYPE_I32_CONST) {
  5215. aux_data_end_global = global;
  5216. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5217. aux_data_end_global_index = export->index;
  5218. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5219. aux_data_end);
  5220. aux_data_end = align_uint64(aux_data_end, 16);
  5221. }
  5222. }
  5223. /* For module compiled with -pthread option, the global is:
  5224. [0] stack_top <-- 0
  5225. [1] tls_pointer
  5226. [2] tls_size
  5227. [3] data_end <-- 3
  5228. [4] global_base
  5229. [5] heap_base <-- 5
  5230. [6] dso_handle
  5231. For module compiled without -pthread option:
  5232. [0] stack_top <-- 0
  5233. [1] data_end <-- 1
  5234. [2] global_base
  5235. [3] heap_base <-- 3
  5236. [4] dso_handle
  5237. */
  5238. if (aux_data_end_global && aux_heap_base_global
  5239. && aux_data_end <= aux_heap_base) {
  5240. module->aux_data_end_global_index = aux_data_end_global_index;
  5241. module->aux_data_end = aux_data_end;
  5242. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5243. module->aux_heap_base = aux_heap_base;
  5244. /* Resolve aux stack top global */
  5245. for (global_index = 0; global_index < module->global_count;
  5246. global_index++) {
  5247. global = module->globals + global_index;
  5248. if (global->type.is_mutable /* heap_base and data_end is
  5249. not mutable */
  5250. && global->type.val_type == VALUE_TYPE_I32
  5251. && global->init_expr.init_expr_type
  5252. == INIT_EXPR_TYPE_I32_CONST
  5253. && (uint64)(uint32)global->init_expr.u.i32
  5254. <= aux_heap_base) {
  5255. aux_stack_top_global = global;
  5256. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5257. module->aux_stack_top_global_index =
  5258. module->import_global_count + global_index;
  5259. module->aux_stack_bottom = aux_stack_top;
  5260. module->aux_stack_size =
  5261. aux_stack_top > aux_data_end
  5262. ? (uint32)(aux_stack_top - aux_data_end)
  5263. : (uint32)aux_stack_top;
  5264. LOG_VERBOSE(
  5265. "Found aux stack top global, value: %" PRIu64 ", "
  5266. "global index: %d, stack size: %d",
  5267. aux_stack_top, global_index,
  5268. module->aux_stack_size);
  5269. break;
  5270. }
  5271. }
  5272. if (!aux_stack_top_global) {
  5273. /* Auxiliary stack global isn't found, it must be unused
  5274. in the wasm app, as if it is used, the global must be
  5275. defined. Here we set it to __heap_base global and set
  5276. its size to 0. */
  5277. aux_stack_top_global = aux_heap_base_global;
  5278. aux_stack_top = aux_heap_base;
  5279. module->aux_stack_top_global_index =
  5280. module->aux_heap_base_global_index;
  5281. module->aux_stack_bottom = aux_stack_top;
  5282. module->aux_stack_size = 0;
  5283. }
  5284. break;
  5285. }
  5286. }
  5287. }
  5288. module->malloc_function = (uint32)-1;
  5289. module->free_function = (uint32)-1;
  5290. module->retain_function = (uint32)-1;
  5291. /* Resolve malloc/free function exported by wasm module */
  5292. #if WASM_ENABLE_MEMORY64 != 0
  5293. if (has_module_memory64(module))
  5294. malloc_free_io_type = VALUE_TYPE_I64;
  5295. #endif
  5296. export = module->exports;
  5297. for (i = 0; i < module->export_count; i++, export ++) {
  5298. if (export->kind == EXPORT_KIND_FUNC) {
  5299. if (!strcmp(export->name, "malloc")
  5300. && export->index >= module->import_function_count) {
  5301. func_index = export->index - module->import_function_count;
  5302. func_type = module->functions[func_index]->func_type;
  5303. if (func_type->param_count == 1 && func_type->result_count == 1
  5304. && func_type->types[0] == malloc_free_io_type
  5305. && func_type->types[1] == malloc_free_io_type) {
  5306. bh_assert(module->malloc_function == (uint32)-1);
  5307. module->malloc_function = export->index;
  5308. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5309. export->name, export->index);
  5310. }
  5311. }
  5312. else if (!strcmp(export->name, "__new")
  5313. && export->index >= module->import_function_count) {
  5314. /* __new && __pin for AssemblyScript */
  5315. func_index = export->index - module->import_function_count;
  5316. func_type = module->functions[func_index]->func_type;
  5317. if (func_type->param_count == 2 && func_type->result_count == 1
  5318. && func_type->types[0] == malloc_free_io_type
  5319. && func_type->types[1] == VALUE_TYPE_I32
  5320. && func_type->types[2] == malloc_free_io_type) {
  5321. uint32 j;
  5322. WASMExport *export_tmp;
  5323. bh_assert(module->malloc_function == (uint32)-1);
  5324. module->malloc_function = export->index;
  5325. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5326. export->name, export->index);
  5327. /* resolve retain function.
  5328. If not found, reset malloc function index */
  5329. export_tmp = module->exports;
  5330. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5331. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5332. && (!strcmp(export_tmp->name, "__retain")
  5333. || (!strcmp(export_tmp->name, "__pin")))
  5334. && (export_tmp->index
  5335. >= module->import_function_count)) {
  5336. func_index = export_tmp->index
  5337. - module->import_function_count;
  5338. func_type =
  5339. module->functions[func_index]->func_type;
  5340. if (func_type->param_count == 1
  5341. && func_type->result_count == 1
  5342. && func_type->types[0] == malloc_free_io_type
  5343. && func_type->types[1] == malloc_free_io_type) {
  5344. bh_assert(module->retain_function
  5345. == (uint32)-1);
  5346. module->retain_function = export_tmp->index;
  5347. LOG_VERBOSE("Found retain function, name: %s, "
  5348. "index: %u",
  5349. export_tmp->name,
  5350. export_tmp->index);
  5351. break;
  5352. }
  5353. }
  5354. }
  5355. if (j == module->export_count) {
  5356. module->malloc_function = (uint32)-1;
  5357. LOG_VERBOSE("Can't find retain function,"
  5358. "reset malloc function index to -1");
  5359. }
  5360. }
  5361. }
  5362. else if (((!strcmp(export->name, "free"))
  5363. || (!strcmp(export->name, "__release"))
  5364. || (!strcmp(export->name, "__unpin")))
  5365. && export->index >= module->import_function_count) {
  5366. func_index = export->index - module->import_function_count;
  5367. func_type = module->functions[func_index]->func_type;
  5368. if (func_type->param_count == 1 && func_type->result_count == 0
  5369. && func_type->types[0] == malloc_free_io_type) {
  5370. bh_assert(module->free_function == (uint32)-1);
  5371. module->free_function = export->index;
  5372. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5373. export->name, export->index);
  5374. }
  5375. }
  5376. }
  5377. }
  5378. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5379. handle_table = wasm_interp_get_handle_table();
  5380. #endif
  5381. for (i = 0; i < module->function_count; i++) {
  5382. WASMFunction *func = module->functions[i];
  5383. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5384. error_buf_size)) {
  5385. return false;
  5386. }
  5387. if (i == module->function_count - 1
  5388. && func->code + func->code_size != buf_code_end) {
  5389. set_error_buf(error_buf, error_buf_size,
  5390. "code section size mismatch");
  5391. return false;
  5392. }
  5393. }
  5394. if (!module->possible_memory_grow) {
  5395. WASMMemoryImport *memory_import;
  5396. WASMMemory *memory;
  5397. if (aux_data_end_global && aux_heap_base_global
  5398. && aux_stack_top_global) {
  5399. uint64 init_memory_size;
  5400. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5401. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5402. * valid range of uint32 */
  5403. if (shrunk_memory_size <= UINT32_MAX) {
  5404. if (module->import_memory_count) {
  5405. memory_import = &module->import_memories[0].u.memory;
  5406. init_memory_size =
  5407. (uint64)memory_import->mem_type.num_bytes_per_page
  5408. * memory_import->mem_type.init_page_count;
  5409. if (shrunk_memory_size <= init_memory_size) {
  5410. /* Reset memory info to decrease memory usage */
  5411. memory_import->mem_type.num_bytes_per_page =
  5412. (uint32)shrunk_memory_size;
  5413. memory_import->mem_type.init_page_count = 1;
  5414. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5415. shrunk_memory_size);
  5416. }
  5417. }
  5418. if (module->memory_count) {
  5419. memory = &module->memories[0];
  5420. init_memory_size = (uint64)memory->num_bytes_per_page
  5421. * memory->init_page_count;
  5422. if (shrunk_memory_size <= init_memory_size) {
  5423. /* Reset memory info to decrease memory usage */
  5424. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5425. memory->init_page_count = 1;
  5426. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5427. shrunk_memory_size);
  5428. }
  5429. }
  5430. }
  5431. }
  5432. #if WASM_ENABLE_MULTI_MODULE == 0
  5433. if (module->import_memory_count) {
  5434. memory_import = &module->import_memories[0].u.memory;
  5435. /* Only resize the memory to one big page if num_bytes_per_page is
  5436. * in valid range of uint32 */
  5437. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5438. memory_import->mem_type.num_bytes_per_page *=
  5439. memory_import->mem_type.init_page_count;
  5440. if (memory_import->mem_type.init_page_count > 0)
  5441. memory_import->mem_type.init_page_count =
  5442. memory_import->mem_type.max_page_count = 1;
  5443. else
  5444. memory_import->mem_type.init_page_count =
  5445. memory_import->mem_type.max_page_count = 0;
  5446. }
  5447. }
  5448. if (module->memory_count) {
  5449. memory = &module->memories[0];
  5450. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5451. * valid range of uint32 */
  5452. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5453. memory->num_bytes_per_page *= memory->init_page_count;
  5454. if (memory->init_page_count > 0)
  5455. memory->init_page_count = memory->max_page_count = 1;
  5456. else
  5457. memory->init_page_count = memory->max_page_count = 0;
  5458. }
  5459. }
  5460. #endif
  5461. }
  5462. calculate_global_data_offset(module);
  5463. #if WASM_ENABLE_FAST_JIT != 0
  5464. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5465. return false;
  5466. }
  5467. #endif
  5468. #if WASM_ENABLE_JIT != 0
  5469. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5470. return false;
  5471. }
  5472. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5473. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5474. return false;
  5475. }
  5476. #else
  5477. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5478. thread fast jit execution, since applying llvm optimizations in
  5479. aot_compile_wasm may cost a lot of time.
  5480. Create thread with enough native stack to apply llvm optimizations */
  5481. if (os_thread_create(&module->llvm_jit_init_thread,
  5482. init_llvm_jit_functions_stage2_callback,
  5483. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5484. != 0) {
  5485. set_error_buf(error_buf, error_buf_size,
  5486. "create orcjit compile thread failed");
  5487. return false;
  5488. }
  5489. #endif
  5490. #endif
  5491. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5492. /* Create threads to compile the jit functions */
  5493. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5494. return false;
  5495. }
  5496. #endif
  5497. #if WASM_ENABLE_MEMORY_TRACING != 0
  5498. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5499. #endif
  5500. return true;
  5501. }
  5502. static WASMModule *
  5503. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5504. {
  5505. WASMModule *module =
  5506. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5507. bh_list_status ret;
  5508. if (!module) {
  5509. return NULL;
  5510. }
  5511. module->module_type = Wasm_Module_Bytecode;
  5512. /* Set start_function to -1, means no start function */
  5513. module->start_function = (uint32)-1;
  5514. module->name = name;
  5515. module->is_binary_freeable = false;
  5516. #if WASM_ENABLE_FAST_INTERP == 0
  5517. module->br_table_cache_list = &module->br_table_cache_list_head;
  5518. ret = bh_list_init(module->br_table_cache_list);
  5519. bh_assert(ret == BH_LIST_SUCCESS);
  5520. #endif
  5521. #if WASM_ENABLE_MULTI_MODULE != 0
  5522. module->import_module_list = &module->import_module_list_head;
  5523. ret = bh_list_init(module->import_module_list);
  5524. bh_assert(ret == BH_LIST_SUCCESS);
  5525. #endif
  5526. #if WASM_ENABLE_DEBUG_INTERP != 0
  5527. ret = bh_list_init(&module->fast_opcode_list);
  5528. bh_assert(ret == BH_LIST_SUCCESS);
  5529. #endif
  5530. #if WASM_ENABLE_GC != 0
  5531. if (!(module->ref_type_set =
  5532. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5533. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5534. goto fail1;
  5535. }
  5536. if (os_mutex_init(&module->rtt_type_lock)) {
  5537. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5538. goto fail2;
  5539. }
  5540. #endif
  5541. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5542. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5543. && WASM_ENABLE_LAZY_JIT != 0)
  5544. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5545. set_error_buf(error_buf, error_buf_size,
  5546. "init instance list lock failed");
  5547. goto fail3;
  5548. }
  5549. #endif
  5550. (void)ret;
  5551. return module;
  5552. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5553. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5554. && WASM_ENABLE_LAZY_JIT != 0)
  5555. fail3:
  5556. #endif
  5557. #if WASM_ENABLE_GC != 0
  5558. os_mutex_destroy(&module->rtt_type_lock);
  5559. fail2:
  5560. bh_hash_map_destroy(module->ref_type_set);
  5561. fail1:
  5562. #endif
  5563. wasm_runtime_free(module);
  5564. return NULL;
  5565. }
  5566. #if WASM_ENABLE_DEBUG_INTERP != 0
  5567. static bool
  5568. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5569. uint32 error_buf_size)
  5570. {
  5571. WASMFastOPCodeNode *fast_op =
  5572. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5573. if (fast_op) {
  5574. fast_op->offset = pos - module->load_addr;
  5575. fast_op->orig_op = orig_op;
  5576. bh_list_insert(&module->fast_opcode_list, fast_op);
  5577. }
  5578. return fast_op ? true : false;
  5579. }
  5580. #endif
  5581. WASMModule *
  5582. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5583. uint32 error_buf_size)
  5584. {
  5585. WASMModule *module = create_module("", error_buf, error_buf_size);
  5586. if (!module)
  5587. return NULL;
  5588. if (!load_from_sections(module, section_list, false, true, false, error_buf,
  5589. error_buf_size)) {
  5590. wasm_loader_unload(module);
  5591. return NULL;
  5592. }
  5593. LOG_VERBOSE("Load module from sections success.\n");
  5594. return module;
  5595. }
  5596. static void
  5597. destroy_sections(WASMSection *section_list)
  5598. {
  5599. WASMSection *section = section_list, *next;
  5600. while (section) {
  5601. next = section->next;
  5602. wasm_runtime_free(section);
  5603. section = next;
  5604. }
  5605. }
  5606. /* clang-format off */
  5607. static uint8 section_ids[] = {
  5608. SECTION_TYPE_USER,
  5609. SECTION_TYPE_TYPE,
  5610. SECTION_TYPE_IMPORT,
  5611. SECTION_TYPE_FUNC,
  5612. SECTION_TYPE_TABLE,
  5613. SECTION_TYPE_MEMORY,
  5614. #if WASM_ENABLE_TAGS != 0
  5615. SECTION_TYPE_TAG,
  5616. #endif
  5617. #if WASM_ENABLE_STRINGREF != 0
  5618. /* must immediately precede the global section,
  5619. or where the global section would be */
  5620. SECTION_TYPE_STRINGREF,
  5621. #endif
  5622. SECTION_TYPE_GLOBAL,
  5623. SECTION_TYPE_EXPORT,
  5624. SECTION_TYPE_START,
  5625. SECTION_TYPE_ELEM,
  5626. #if WASM_ENABLE_BULK_MEMORY != 0
  5627. SECTION_TYPE_DATACOUNT,
  5628. #endif
  5629. SECTION_TYPE_CODE,
  5630. SECTION_TYPE_DATA
  5631. };
  5632. /* clang-format on */
  5633. static uint8
  5634. get_section_index(uint8 section_type)
  5635. {
  5636. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5637. for (uint8 i = 0; i < max_id; i++) {
  5638. if (section_type == section_ids[i])
  5639. return i;
  5640. }
  5641. return (uint8)-1;
  5642. }
  5643. static bool
  5644. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5645. char *error_buf, uint32 error_buf_size)
  5646. {
  5647. WASMSection *section_list_end = NULL, *section;
  5648. const uint8 *p = buf, *p_end = buf + size;
  5649. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5650. uint32 section_size;
  5651. bh_assert(!*p_section_list);
  5652. p += 8;
  5653. while (p < p_end) {
  5654. CHECK_BUF(p, p_end, 1);
  5655. section_type = read_uint8(p);
  5656. section_index = get_section_index(section_type);
  5657. if (section_index != (uint8)-1) {
  5658. if (section_type != SECTION_TYPE_USER) {
  5659. /* Custom sections may be inserted at any place,
  5660. while other sections must occur at most once
  5661. and in prescribed order. */
  5662. if (last_section_index != (uint8)-1
  5663. && (section_index <= last_section_index)) {
  5664. set_error_buf(error_buf, error_buf_size,
  5665. "unexpected content after last section or "
  5666. "junk after last section");
  5667. return false;
  5668. }
  5669. last_section_index = section_index;
  5670. }
  5671. read_leb_uint32(p, p_end, section_size);
  5672. CHECK_BUF1(p, p_end, section_size);
  5673. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5674. error_buf_size))) {
  5675. return false;
  5676. }
  5677. section->section_type = section_type;
  5678. section->section_body = (uint8 *)p;
  5679. section->section_body_size = section_size;
  5680. if (!section_list_end)
  5681. *p_section_list = section_list_end = section;
  5682. else {
  5683. section_list_end->next = section;
  5684. section_list_end = section;
  5685. }
  5686. p += section_size;
  5687. }
  5688. else {
  5689. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5690. return false;
  5691. }
  5692. }
  5693. return true;
  5694. fail:
  5695. return false;
  5696. }
  5697. static void
  5698. exchange32(uint8 *p_data)
  5699. {
  5700. uint8 value = *p_data;
  5701. *p_data = *(p_data + 3);
  5702. *(p_data + 3) = value;
  5703. value = *(p_data + 1);
  5704. *(p_data + 1) = *(p_data + 2);
  5705. *(p_data + 2) = value;
  5706. }
  5707. static union {
  5708. int a;
  5709. char b;
  5710. } __ue = { .a = 1 };
  5711. #define is_little_endian() (__ue.b == 1)
  5712. static bool
  5713. load(const uint8 *buf, uint32 size, WASMModule *module,
  5714. bool wasm_binary_freeable, bool no_resolve, char *error_buf,
  5715. uint32 error_buf_size)
  5716. {
  5717. const uint8 *buf_end = buf + size;
  5718. const uint8 *p = buf, *p_end = buf_end;
  5719. uint32 magic_number, version;
  5720. WASMSection *section_list = NULL;
  5721. CHECK_BUF1(p, p_end, sizeof(uint32));
  5722. magic_number = read_uint32(p);
  5723. if (!is_little_endian())
  5724. exchange32((uint8 *)&magic_number);
  5725. if (magic_number != WASM_MAGIC_NUMBER) {
  5726. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5727. return false;
  5728. }
  5729. CHECK_BUF1(p, p_end, sizeof(uint32));
  5730. version = read_uint32(p);
  5731. if (!is_little_endian())
  5732. exchange32((uint8 *)&version);
  5733. if (version != WASM_CURRENT_VERSION) {
  5734. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5735. return false;
  5736. }
  5737. module->package_version = version;
  5738. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5739. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  5740. no_resolve, error_buf, error_buf_size)) {
  5741. destroy_sections(section_list);
  5742. return false;
  5743. }
  5744. destroy_sections(section_list);
  5745. return true;
  5746. fail:
  5747. return false;
  5748. }
  5749. #if WASM_ENABLE_LIBC_WASI != 0
  5750. /**
  5751. * refer to
  5752. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5753. */
  5754. static bool
  5755. check_wasi_abi_compatibility(const WASMModule *module,
  5756. #if WASM_ENABLE_MULTI_MODULE != 0
  5757. bool main_module,
  5758. #endif
  5759. char *error_buf, uint32 error_buf_size)
  5760. {
  5761. /**
  5762. * be careful with:
  5763. * wasi compatible modules(command/reactor) which don't import any wasi
  5764. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5765. * reactor can depend on nothing. At the same time, each has its own entry
  5766. * point.
  5767. *
  5768. * observations:
  5769. * - clang always injects `_start` into a command
  5770. * - clang always injects `_initialize` into a reactor
  5771. * - `iwasm -f` allows to run a function in the reactor
  5772. *
  5773. * strong assumptions:
  5774. * - no one will define either `_start` or `_initialize` on purpose
  5775. * - `_start` should always be `void _start(void)`
  5776. * - `_initialize` should always be `void _initialize(void)`
  5777. *
  5778. */
  5779. /* clang-format off */
  5780. /**
  5781. *
  5782. * | | import_wasi_api True | | import_wasi_api False | |
  5783. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5784. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5785. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5786. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5787. */
  5788. /* clang-format on */
  5789. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5790. uint32 import_function_count = module->import_function_count;
  5791. WASMFuncType *func_type;
  5792. /* (func (export "_start") (...) */
  5793. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5794. error_buf, error_buf_size);
  5795. if (start) {
  5796. if (start->index < import_function_count) {
  5797. set_error_buf(
  5798. error_buf, error_buf_size,
  5799. "the builtin _start function can not be an import function");
  5800. return false;
  5801. }
  5802. func_type =
  5803. module->functions[start->index - import_function_count]->func_type;
  5804. if (func_type->param_count || func_type->result_count) {
  5805. set_error_buf(error_buf, error_buf_size,
  5806. "the signature of builtin _start function is wrong");
  5807. return false;
  5808. }
  5809. }
  5810. else {
  5811. /* (func (export "_initialize") (...) */
  5812. initialize =
  5813. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5814. error_buf, error_buf_size);
  5815. if (initialize) {
  5816. if (initialize->index < import_function_count) {
  5817. set_error_buf(error_buf, error_buf_size,
  5818. "the builtin _initialize function can not be an "
  5819. "import function");
  5820. return false;
  5821. }
  5822. func_type =
  5823. module->functions[initialize->index - import_function_count]
  5824. ->func_type;
  5825. if (func_type->param_count || func_type->result_count) {
  5826. set_error_buf(
  5827. error_buf, error_buf_size,
  5828. "the signature of builtin _initialize function is wrong");
  5829. return false;
  5830. }
  5831. }
  5832. }
  5833. /* filter out non-wasi compatible modules */
  5834. if (!module->import_wasi_api && !start && !initialize) {
  5835. return true;
  5836. }
  5837. /* should have one at least */
  5838. if (module->import_wasi_api && !start && !initialize) {
  5839. LOG_WARNING("warning: a module with WASI apis should be either "
  5840. "a command or a reactor");
  5841. }
  5842. /*
  5843. * there is at least one of `_start` and `_initialize` in below cases.
  5844. * according to the assumption, they should be all wasi compatible
  5845. */
  5846. #if WASM_ENABLE_MULTI_MODULE != 0
  5847. /* filter out commands (with `_start`) cases */
  5848. if (start && !main_module) {
  5849. set_error_buf(
  5850. error_buf, error_buf_size,
  5851. "a command (with _start function) can not be a sub-module");
  5852. return false;
  5853. }
  5854. #endif
  5855. /*
  5856. * it is ok a reactor acts as a main module,
  5857. * so skip the check about (with `_initialize`)
  5858. */
  5859. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5860. error_buf, error_buf_size);
  5861. if (!memory
  5862. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5863. /*
  5864. * with wasi-threads, it's still an open question if a memory
  5865. * should be exported.
  5866. *
  5867. * https://github.com/WebAssembly/wasi-threads/issues/22
  5868. * https://github.com/WebAssembly/WASI/issues/502
  5869. *
  5870. * Note: this code assumes the number of memories is at most 1.
  5871. */
  5872. && module->import_memory_count == 0
  5873. #endif
  5874. ) {
  5875. set_error_buf(error_buf, error_buf_size,
  5876. "a module with WASI apis must export memory by default");
  5877. return false;
  5878. }
  5879. return true;
  5880. }
  5881. #endif
  5882. WASMModule *
  5883. wasm_loader_load(uint8 *buf, uint32 size,
  5884. #if WASM_ENABLE_MULTI_MODULE != 0
  5885. bool main_module,
  5886. #endif
  5887. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  5888. {
  5889. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  5890. if (!module) {
  5891. return NULL;
  5892. }
  5893. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5894. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5895. module->load_addr = (uint8 *)buf;
  5896. module->load_size = size;
  5897. #endif
  5898. if (!load(buf, size, module, args->wasm_binary_freeable, args->no_resolve,
  5899. error_buf, error_buf_size)) {
  5900. goto fail;
  5901. }
  5902. #if WASM_ENABLE_LIBC_WASI != 0
  5903. /* Check the WASI application ABI */
  5904. if (!check_wasi_abi_compatibility(module,
  5905. #if WASM_ENABLE_MULTI_MODULE != 0
  5906. main_module,
  5907. #endif
  5908. error_buf, error_buf_size)) {
  5909. goto fail;
  5910. }
  5911. #endif
  5912. LOG_VERBOSE("Load module success.\n");
  5913. return module;
  5914. fail:
  5915. wasm_loader_unload(module);
  5916. return NULL;
  5917. }
  5918. void
  5919. wasm_loader_unload(WASMModule *module)
  5920. {
  5921. uint32 i;
  5922. if (!module)
  5923. return;
  5924. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5925. && WASM_ENABLE_LAZY_JIT != 0
  5926. module->orcjit_stop_compiling = true;
  5927. if (module->llvm_jit_init_thread)
  5928. os_thread_join(module->llvm_jit_init_thread, NULL);
  5929. #endif
  5930. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5931. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  5932. module internal data after they were freed */
  5933. orcjit_stop_compile_threads(module);
  5934. #endif
  5935. #if WASM_ENABLE_JIT != 0
  5936. if (module->func_ptrs)
  5937. wasm_runtime_free(module->func_ptrs);
  5938. if (module->comp_ctx)
  5939. aot_destroy_comp_context(module->comp_ctx);
  5940. if (module->comp_data)
  5941. aot_destroy_comp_data(module->comp_data);
  5942. #endif
  5943. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5944. && WASM_ENABLE_LAZY_JIT != 0
  5945. if (module->tierup_wait_lock_inited) {
  5946. os_mutex_destroy(&module->tierup_wait_lock);
  5947. os_cond_destroy(&module->tierup_wait_cond);
  5948. }
  5949. #endif
  5950. if (module->imports)
  5951. wasm_runtime_free(module->imports);
  5952. if (module->functions) {
  5953. for (i = 0; i < module->function_count; i++) {
  5954. if (module->functions[i]) {
  5955. if (module->functions[i]->local_offsets)
  5956. wasm_runtime_free(module->functions[i]->local_offsets);
  5957. #if WASM_ENABLE_FAST_INTERP != 0
  5958. if (module->functions[i]->code_compiled)
  5959. wasm_runtime_free(module->functions[i]->code_compiled);
  5960. if (module->functions[i]->consts)
  5961. wasm_runtime_free(module->functions[i]->consts);
  5962. #endif
  5963. #if WASM_ENABLE_FAST_JIT != 0
  5964. if (module->functions[i]->fast_jit_jitted_code) {
  5965. jit_code_cache_free(
  5966. module->functions[i]->fast_jit_jitted_code);
  5967. }
  5968. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5969. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  5970. jit_code_cache_free(
  5971. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  5972. }
  5973. #endif
  5974. #endif
  5975. #if WASM_ENABLE_GC != 0
  5976. if (module->functions[i]->local_ref_type_maps) {
  5977. wasm_runtime_free(
  5978. module->functions[i]->local_ref_type_maps);
  5979. }
  5980. #endif
  5981. wasm_runtime_free(module->functions[i]);
  5982. }
  5983. }
  5984. wasm_runtime_free(module->functions);
  5985. }
  5986. if (module->tables) {
  5987. #if WASM_ENABLE_GC != 0
  5988. for (i = 0; i < module->table_count; i++) {
  5989. destroy_init_expr(module, &module->tables[i].init_expr);
  5990. }
  5991. #endif
  5992. wasm_runtime_free(module->tables);
  5993. }
  5994. if (module->memories)
  5995. wasm_runtime_free(module->memories);
  5996. if (module->globals) {
  5997. #if WASM_ENABLE_GC != 0
  5998. for (i = 0; i < module->global_count; i++) {
  5999. destroy_init_expr(module, &module->globals[i].init_expr);
  6000. }
  6001. #endif
  6002. wasm_runtime_free(module->globals);
  6003. }
  6004. #if WASM_ENABLE_TAGS != 0
  6005. if (module->tags) {
  6006. for (i = 0; i < module->tag_count; i++) {
  6007. if (module->tags[i])
  6008. wasm_runtime_free(module->tags[i]);
  6009. }
  6010. wasm_runtime_free(module->tags);
  6011. }
  6012. #endif
  6013. if (module->exports)
  6014. wasm_runtime_free(module->exports);
  6015. if (module->table_segments) {
  6016. for (i = 0; i < module->table_seg_count; i++) {
  6017. if (module->table_segments[i].init_values) {
  6018. #if WASM_ENABLE_GC != 0
  6019. uint32 j;
  6020. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6021. destroy_init_expr(
  6022. module, &module->table_segments[i].init_values[j]);
  6023. }
  6024. #endif
  6025. wasm_runtime_free(module->table_segments[i].init_values);
  6026. }
  6027. }
  6028. wasm_runtime_free(module->table_segments);
  6029. }
  6030. if (module->data_segments) {
  6031. for (i = 0; i < module->data_seg_count; i++) {
  6032. if (module->data_segments[i]) {
  6033. if (module->data_segments[i]->is_data_cloned)
  6034. wasm_runtime_free(module->data_segments[i]->data);
  6035. wasm_runtime_free(module->data_segments[i]);
  6036. }
  6037. }
  6038. wasm_runtime_free(module->data_segments);
  6039. }
  6040. if (module->types) {
  6041. for (i = 0; i < module->type_count; i++) {
  6042. if (module->types[i])
  6043. destroy_wasm_type(module->types[i]);
  6044. }
  6045. wasm_runtime_free(module->types);
  6046. }
  6047. if (module->const_str_list) {
  6048. StringNode *node = module->const_str_list, *node_next;
  6049. while (node) {
  6050. node_next = node->next;
  6051. wasm_runtime_free(node);
  6052. node = node_next;
  6053. }
  6054. }
  6055. #if WASM_ENABLE_STRINGREF != 0
  6056. if (module->string_literal_ptrs) {
  6057. wasm_runtime_free((void *)module->string_literal_ptrs);
  6058. }
  6059. if (module->string_literal_lengths) {
  6060. wasm_runtime_free(module->string_literal_lengths);
  6061. }
  6062. #endif
  6063. #if WASM_ENABLE_FAST_INTERP == 0
  6064. if (module->br_table_cache_list) {
  6065. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6066. BrTableCache *node_next;
  6067. while (node) {
  6068. node_next = bh_list_elem_next(node);
  6069. wasm_runtime_free(node);
  6070. node = node_next;
  6071. }
  6072. }
  6073. #endif
  6074. #if WASM_ENABLE_MULTI_MODULE != 0
  6075. /* just release the sub module list */
  6076. if (module->import_module_list) {
  6077. WASMRegisteredModule *node =
  6078. bh_list_first_elem(module->import_module_list);
  6079. while (node) {
  6080. WASMRegisteredModule *next = bh_list_elem_next(node);
  6081. bh_list_remove(module->import_module_list, node);
  6082. /*
  6083. * unload(sub_module) will be triggered during runtime_destroy().
  6084. * every module in the global module list will be unloaded one by
  6085. * one. so don't worry.
  6086. */
  6087. wasm_runtime_free(node);
  6088. /*
  6089. * the module file reading buffer will be released
  6090. * in runtime_destroy()
  6091. */
  6092. node = next;
  6093. }
  6094. }
  6095. #endif
  6096. #if WASM_ENABLE_DEBUG_INTERP != 0
  6097. WASMFastOPCodeNode *fast_opcode =
  6098. bh_list_first_elem(&module->fast_opcode_list);
  6099. while (fast_opcode) {
  6100. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6101. wasm_runtime_free(fast_opcode);
  6102. fast_opcode = next;
  6103. }
  6104. #endif
  6105. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6106. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6107. && WASM_ENABLE_LAZY_JIT != 0)
  6108. os_mutex_destroy(&module->instance_list_lock);
  6109. #endif
  6110. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6111. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6112. #endif
  6113. #if WASM_ENABLE_FAST_JIT != 0
  6114. if (module->fast_jit_func_ptrs) {
  6115. wasm_runtime_free(module->fast_jit_func_ptrs);
  6116. }
  6117. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6118. if (module->fast_jit_thread_locks_inited[i]) {
  6119. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6120. }
  6121. }
  6122. #endif
  6123. #if WASM_ENABLE_GC != 0
  6124. os_mutex_destroy(&module->rtt_type_lock);
  6125. bh_hash_map_destroy(module->ref_type_set);
  6126. if (module->rtt_types) {
  6127. for (i = 0; i < module->type_count; i++) {
  6128. if (module->rtt_types[i])
  6129. wasm_runtime_free(module->rtt_types[i]);
  6130. }
  6131. wasm_runtime_free(module->rtt_types);
  6132. }
  6133. #if WASM_ENABLE_STRINGREF != 0
  6134. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6135. if (module->stringref_rtts[i])
  6136. wasm_runtime_free(module->stringref_rtts[i]);
  6137. }
  6138. #endif
  6139. #endif
  6140. wasm_runtime_free(module);
  6141. }
  6142. bool
  6143. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6144. const uint8 *start_addr, const uint8 *code_end_addr,
  6145. uint8 label_type, uint8 **p_else_addr,
  6146. uint8 **p_end_addr)
  6147. {
  6148. const uint8 *p = start_addr, *p_end = code_end_addr;
  6149. uint8 *else_addr = NULL;
  6150. char error_buf[128];
  6151. uint32 block_nested_depth = 1, count, i, j, t;
  6152. uint32 error_buf_size = sizeof(error_buf);
  6153. uint8 opcode, u8;
  6154. BlockAddr block_stack[16] = { { 0 } }, *block;
  6155. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6156. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6157. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6158. if (block[j].start_addr == start_addr) {
  6159. /* Cache hit */
  6160. *p_else_addr = block[j].else_addr;
  6161. *p_end_addr = block[j].end_addr;
  6162. return true;
  6163. }
  6164. }
  6165. /* Cache unhit */
  6166. block_stack[0].start_addr = start_addr;
  6167. while (p < code_end_addr) {
  6168. opcode = *p++;
  6169. #if WASM_ENABLE_DEBUG_INTERP != 0
  6170. op_break_retry:
  6171. #endif
  6172. switch (opcode) {
  6173. case WASM_OP_UNREACHABLE:
  6174. case WASM_OP_NOP:
  6175. break;
  6176. #if WASM_ENABLE_EXCE_HANDLING != 0
  6177. case WASM_OP_TRY:
  6178. u8 = read_uint8(p);
  6179. if (block_nested_depth
  6180. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6181. block_stack[block_nested_depth].start_addr = p;
  6182. block_stack[block_nested_depth].else_addr = NULL;
  6183. }
  6184. block_nested_depth++;
  6185. break;
  6186. case EXT_OP_TRY:
  6187. skip_leb_uint32(p, p_end);
  6188. if (block_nested_depth
  6189. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6190. block_stack[block_nested_depth].start_addr = p;
  6191. block_stack[block_nested_depth].else_addr = NULL;
  6192. }
  6193. block_nested_depth++;
  6194. break;
  6195. case WASM_OP_CATCH:
  6196. if (block_nested_depth == 1) {
  6197. *p_end_addr = (uint8 *)(p - 1);
  6198. /* stop search and return the address of the catch block */
  6199. return true;
  6200. }
  6201. break;
  6202. case WASM_OP_CATCH_ALL:
  6203. if (block_nested_depth == 1) {
  6204. *p_end_addr = (uint8 *)(p - 1);
  6205. /* stop search and return the address of the catch_all block
  6206. */
  6207. return true;
  6208. }
  6209. break;
  6210. case WASM_OP_THROW:
  6211. /* skip tag_index */
  6212. skip_leb(p);
  6213. break;
  6214. case WASM_OP_RETHROW:
  6215. /* skip depth */
  6216. skip_leb(p);
  6217. break;
  6218. case WASM_OP_DELEGATE:
  6219. if (block_nested_depth == 1) {
  6220. *p_end_addr = (uint8 *)(p - 1);
  6221. return true;
  6222. }
  6223. else {
  6224. skip_leb(p);
  6225. /* the DELEGATE opcode ends the tryblock, */
  6226. block_nested_depth--;
  6227. if (block_nested_depth
  6228. < sizeof(block_stack) / sizeof(BlockAddr))
  6229. block_stack[block_nested_depth].end_addr =
  6230. (uint8 *)(p - 1);
  6231. }
  6232. break;
  6233. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6234. case WASM_OP_BLOCK:
  6235. case WASM_OP_LOOP:
  6236. case WASM_OP_IF:
  6237. {
  6238. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6239. u8 = read_uint8(p);
  6240. if (is_byte_a_type(u8)) {
  6241. #if WASM_ENABLE_GC != 0
  6242. if (wasm_is_type_multi_byte_type(u8)) {
  6243. /* the possible extra bytes of GC ref type have been
  6244. modified to OP_NOP, no need to resolve them again */
  6245. }
  6246. #endif
  6247. }
  6248. else {
  6249. p--;
  6250. /* block type */
  6251. skip_leb_int32(p, p_end);
  6252. }
  6253. if (block_nested_depth
  6254. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6255. block_stack[block_nested_depth].start_addr = p;
  6256. block_stack[block_nested_depth].else_addr = NULL;
  6257. }
  6258. block_nested_depth++;
  6259. break;
  6260. }
  6261. case EXT_OP_BLOCK:
  6262. case EXT_OP_LOOP:
  6263. case EXT_OP_IF:
  6264. /* block type */
  6265. skip_leb_int32(p, p_end);
  6266. if (block_nested_depth
  6267. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6268. block_stack[block_nested_depth].start_addr = p;
  6269. block_stack[block_nested_depth].else_addr = NULL;
  6270. }
  6271. block_nested_depth++;
  6272. break;
  6273. case WASM_OP_ELSE:
  6274. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6275. else_addr = (uint8 *)(p - 1);
  6276. if (block_nested_depth - 1
  6277. < sizeof(block_stack) / sizeof(BlockAddr))
  6278. block_stack[block_nested_depth - 1].else_addr =
  6279. (uint8 *)(p - 1);
  6280. break;
  6281. case WASM_OP_END:
  6282. if (block_nested_depth == 1) {
  6283. if (label_type == LABEL_TYPE_IF)
  6284. *p_else_addr = else_addr;
  6285. *p_end_addr = (uint8 *)(p - 1);
  6286. block_stack[0].end_addr = (uint8 *)(p - 1);
  6287. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6288. t++) {
  6289. start_addr = block_stack[t].start_addr;
  6290. if (start_addr) {
  6291. i = ((uintptr_t)start_addr)
  6292. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6293. block =
  6294. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6295. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6296. if (!block[j].start_addr)
  6297. break;
  6298. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6299. memmove(block + 1, block,
  6300. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6301. * sizeof(BlockAddr));
  6302. j = 0;
  6303. }
  6304. block[j].start_addr = block_stack[t].start_addr;
  6305. block[j].else_addr = block_stack[t].else_addr;
  6306. block[j].end_addr = block_stack[t].end_addr;
  6307. }
  6308. else
  6309. break;
  6310. }
  6311. return true;
  6312. }
  6313. else {
  6314. block_nested_depth--;
  6315. if (block_nested_depth
  6316. < sizeof(block_stack) / sizeof(BlockAddr))
  6317. block_stack[block_nested_depth].end_addr =
  6318. (uint8 *)(p - 1);
  6319. }
  6320. break;
  6321. case WASM_OP_BR:
  6322. case WASM_OP_BR_IF:
  6323. skip_leb_uint32(p, p_end); /* labelidx */
  6324. break;
  6325. case WASM_OP_BR_TABLE:
  6326. read_leb_uint32(p, p_end, count); /* label num */
  6327. #if WASM_ENABLE_FAST_INTERP != 0
  6328. for (i = 0; i <= count; i++) /* labelidxs */
  6329. skip_leb_uint32(p, p_end);
  6330. #else
  6331. p += count + 1;
  6332. while (*p == WASM_OP_NOP)
  6333. p++;
  6334. #endif
  6335. break;
  6336. #if WASM_ENABLE_FAST_INTERP == 0
  6337. case EXT_OP_BR_TABLE_CACHE:
  6338. read_leb_uint32(p, p_end, count); /* label num */
  6339. while (*p == WASM_OP_NOP)
  6340. p++;
  6341. break;
  6342. #endif
  6343. case WASM_OP_RETURN:
  6344. break;
  6345. case WASM_OP_CALL:
  6346. #if WASM_ENABLE_TAIL_CALL != 0
  6347. case WASM_OP_RETURN_CALL:
  6348. #endif
  6349. skip_leb_uint32(p, p_end); /* funcidx */
  6350. break;
  6351. case WASM_OP_CALL_INDIRECT:
  6352. #if WASM_ENABLE_TAIL_CALL != 0
  6353. case WASM_OP_RETURN_CALL_INDIRECT:
  6354. #endif
  6355. skip_leb_uint32(p, p_end); /* typeidx */
  6356. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6357. skip_leb_uint32(p, p_end); /* tableidx */
  6358. #else
  6359. u8 = read_uint8(p); /* 0x00 */
  6360. #endif
  6361. break;
  6362. #if WASM_ENABLE_GC != 0
  6363. case WASM_OP_CALL_REF:
  6364. case WASM_OP_RETURN_CALL_REF:
  6365. skip_leb_uint32(p, p_end); /* typeidx */
  6366. break;
  6367. #endif
  6368. case WASM_OP_DROP:
  6369. case WASM_OP_SELECT:
  6370. case WASM_OP_DROP_64:
  6371. case WASM_OP_SELECT_64:
  6372. break;
  6373. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6374. case WASM_OP_SELECT_T:
  6375. {
  6376. skip_leb_uint32(p, p_end); /* vec length */
  6377. u8 = read_uint8(p); /* typeidx */
  6378. /* the possible extra bytes of GC ref type have been
  6379. modified to OP_NOP, no need to resolve them again */
  6380. break;
  6381. }
  6382. case WASM_OP_TABLE_GET:
  6383. case WASM_OP_TABLE_SET:
  6384. skip_leb_uint32(p, p_end); /* table index */
  6385. break;
  6386. case WASM_OP_REF_NULL:
  6387. {
  6388. u8 = read_uint8(p); /* type */
  6389. if (is_byte_a_type(u8)) {
  6390. #if WASM_ENABLE_GC != 0
  6391. if (wasm_is_type_multi_byte_type(u8)) {
  6392. /* the possible extra bytes of GC ref type have been
  6393. modified to OP_NOP, no need to resolve them again */
  6394. }
  6395. #endif
  6396. }
  6397. else {
  6398. p--;
  6399. skip_leb_uint32(p, p_end);
  6400. }
  6401. break;
  6402. }
  6403. case WASM_OP_REF_IS_NULL:
  6404. break;
  6405. case WASM_OP_REF_FUNC:
  6406. skip_leb_uint32(p, p_end); /* func index */
  6407. break;
  6408. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6409. #if WASM_ENABLE_GC != 0
  6410. case WASM_OP_REF_AS_NON_NULL:
  6411. case WASM_OP_REF_EQ:
  6412. break;
  6413. case WASM_OP_BR_ON_NULL:
  6414. case WASM_OP_BR_ON_NON_NULL:
  6415. skip_leb_uint32(p, p_end); /* label index */
  6416. break;
  6417. #endif /* end of WASM_ENABLE_GC != 0 */
  6418. case WASM_OP_GET_LOCAL:
  6419. case WASM_OP_SET_LOCAL:
  6420. case WASM_OP_TEE_LOCAL:
  6421. case WASM_OP_GET_GLOBAL:
  6422. case WASM_OP_SET_GLOBAL:
  6423. case WASM_OP_GET_GLOBAL_64:
  6424. case WASM_OP_SET_GLOBAL_64:
  6425. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6426. skip_leb_uint32(p, p_end); /* local index */
  6427. break;
  6428. case EXT_OP_GET_LOCAL_FAST:
  6429. case EXT_OP_SET_LOCAL_FAST:
  6430. case EXT_OP_TEE_LOCAL_FAST:
  6431. CHECK_BUF(p, p_end, 1);
  6432. p++;
  6433. break;
  6434. case WASM_OP_I32_LOAD:
  6435. case WASM_OP_I64_LOAD:
  6436. case WASM_OP_F32_LOAD:
  6437. case WASM_OP_F64_LOAD:
  6438. case WASM_OP_I32_LOAD8_S:
  6439. case WASM_OP_I32_LOAD8_U:
  6440. case WASM_OP_I32_LOAD16_S:
  6441. case WASM_OP_I32_LOAD16_U:
  6442. case WASM_OP_I64_LOAD8_S:
  6443. case WASM_OP_I64_LOAD8_U:
  6444. case WASM_OP_I64_LOAD16_S:
  6445. case WASM_OP_I64_LOAD16_U:
  6446. case WASM_OP_I64_LOAD32_S:
  6447. case WASM_OP_I64_LOAD32_U:
  6448. case WASM_OP_I32_STORE:
  6449. case WASM_OP_I64_STORE:
  6450. case WASM_OP_F32_STORE:
  6451. case WASM_OP_F64_STORE:
  6452. case WASM_OP_I32_STORE8:
  6453. case WASM_OP_I32_STORE16:
  6454. case WASM_OP_I64_STORE8:
  6455. case WASM_OP_I64_STORE16:
  6456. case WASM_OP_I64_STORE32:
  6457. skip_leb_align(p, p_end); /* align */
  6458. skip_leb_mem_offset(p, p_end); /* offset */
  6459. break;
  6460. case WASM_OP_MEMORY_SIZE:
  6461. case WASM_OP_MEMORY_GROW:
  6462. skip_leb_memidx(p, p_end); /* memidx */
  6463. break;
  6464. case WASM_OP_I32_CONST:
  6465. skip_leb_int32(p, p_end);
  6466. break;
  6467. case WASM_OP_I64_CONST:
  6468. skip_leb_int64(p, p_end);
  6469. break;
  6470. case WASM_OP_F32_CONST:
  6471. p += sizeof(float32);
  6472. break;
  6473. case WASM_OP_F64_CONST:
  6474. p += sizeof(float64);
  6475. break;
  6476. case WASM_OP_I32_EQZ:
  6477. case WASM_OP_I32_EQ:
  6478. case WASM_OP_I32_NE:
  6479. case WASM_OP_I32_LT_S:
  6480. case WASM_OP_I32_LT_U:
  6481. case WASM_OP_I32_GT_S:
  6482. case WASM_OP_I32_GT_U:
  6483. case WASM_OP_I32_LE_S:
  6484. case WASM_OP_I32_LE_U:
  6485. case WASM_OP_I32_GE_S:
  6486. case WASM_OP_I32_GE_U:
  6487. case WASM_OP_I64_EQZ:
  6488. case WASM_OP_I64_EQ:
  6489. case WASM_OP_I64_NE:
  6490. case WASM_OP_I64_LT_S:
  6491. case WASM_OP_I64_LT_U:
  6492. case WASM_OP_I64_GT_S:
  6493. case WASM_OP_I64_GT_U:
  6494. case WASM_OP_I64_LE_S:
  6495. case WASM_OP_I64_LE_U:
  6496. case WASM_OP_I64_GE_S:
  6497. case WASM_OP_I64_GE_U:
  6498. case WASM_OP_F32_EQ:
  6499. case WASM_OP_F32_NE:
  6500. case WASM_OP_F32_LT:
  6501. case WASM_OP_F32_GT:
  6502. case WASM_OP_F32_LE:
  6503. case WASM_OP_F32_GE:
  6504. case WASM_OP_F64_EQ:
  6505. case WASM_OP_F64_NE:
  6506. case WASM_OP_F64_LT:
  6507. case WASM_OP_F64_GT:
  6508. case WASM_OP_F64_LE:
  6509. case WASM_OP_F64_GE:
  6510. case WASM_OP_I32_CLZ:
  6511. case WASM_OP_I32_CTZ:
  6512. case WASM_OP_I32_POPCNT:
  6513. case WASM_OP_I32_ADD:
  6514. case WASM_OP_I32_SUB:
  6515. case WASM_OP_I32_MUL:
  6516. case WASM_OP_I32_DIV_S:
  6517. case WASM_OP_I32_DIV_U:
  6518. case WASM_OP_I32_REM_S:
  6519. case WASM_OP_I32_REM_U:
  6520. case WASM_OP_I32_AND:
  6521. case WASM_OP_I32_OR:
  6522. case WASM_OP_I32_XOR:
  6523. case WASM_OP_I32_SHL:
  6524. case WASM_OP_I32_SHR_S:
  6525. case WASM_OP_I32_SHR_U:
  6526. case WASM_OP_I32_ROTL:
  6527. case WASM_OP_I32_ROTR:
  6528. case WASM_OP_I64_CLZ:
  6529. case WASM_OP_I64_CTZ:
  6530. case WASM_OP_I64_POPCNT:
  6531. case WASM_OP_I64_ADD:
  6532. case WASM_OP_I64_SUB:
  6533. case WASM_OP_I64_MUL:
  6534. case WASM_OP_I64_DIV_S:
  6535. case WASM_OP_I64_DIV_U:
  6536. case WASM_OP_I64_REM_S:
  6537. case WASM_OP_I64_REM_U:
  6538. case WASM_OP_I64_AND:
  6539. case WASM_OP_I64_OR:
  6540. case WASM_OP_I64_XOR:
  6541. case WASM_OP_I64_SHL:
  6542. case WASM_OP_I64_SHR_S:
  6543. case WASM_OP_I64_SHR_U:
  6544. case WASM_OP_I64_ROTL:
  6545. case WASM_OP_I64_ROTR:
  6546. case WASM_OP_F32_ABS:
  6547. case WASM_OP_F32_NEG:
  6548. case WASM_OP_F32_CEIL:
  6549. case WASM_OP_F32_FLOOR:
  6550. case WASM_OP_F32_TRUNC:
  6551. case WASM_OP_F32_NEAREST:
  6552. case WASM_OP_F32_SQRT:
  6553. case WASM_OP_F32_ADD:
  6554. case WASM_OP_F32_SUB:
  6555. case WASM_OP_F32_MUL:
  6556. case WASM_OP_F32_DIV:
  6557. case WASM_OP_F32_MIN:
  6558. case WASM_OP_F32_MAX:
  6559. case WASM_OP_F32_COPYSIGN:
  6560. case WASM_OP_F64_ABS:
  6561. case WASM_OP_F64_NEG:
  6562. case WASM_OP_F64_CEIL:
  6563. case WASM_OP_F64_FLOOR:
  6564. case WASM_OP_F64_TRUNC:
  6565. case WASM_OP_F64_NEAREST:
  6566. case WASM_OP_F64_SQRT:
  6567. case WASM_OP_F64_ADD:
  6568. case WASM_OP_F64_SUB:
  6569. case WASM_OP_F64_MUL:
  6570. case WASM_OP_F64_DIV:
  6571. case WASM_OP_F64_MIN:
  6572. case WASM_OP_F64_MAX:
  6573. case WASM_OP_F64_COPYSIGN:
  6574. case WASM_OP_I32_WRAP_I64:
  6575. case WASM_OP_I32_TRUNC_S_F32:
  6576. case WASM_OP_I32_TRUNC_U_F32:
  6577. case WASM_OP_I32_TRUNC_S_F64:
  6578. case WASM_OP_I32_TRUNC_U_F64:
  6579. case WASM_OP_I64_EXTEND_S_I32:
  6580. case WASM_OP_I64_EXTEND_U_I32:
  6581. case WASM_OP_I64_TRUNC_S_F32:
  6582. case WASM_OP_I64_TRUNC_U_F32:
  6583. case WASM_OP_I64_TRUNC_S_F64:
  6584. case WASM_OP_I64_TRUNC_U_F64:
  6585. case WASM_OP_F32_CONVERT_S_I32:
  6586. case WASM_OP_F32_CONVERT_U_I32:
  6587. case WASM_OP_F32_CONVERT_S_I64:
  6588. case WASM_OP_F32_CONVERT_U_I64:
  6589. case WASM_OP_F32_DEMOTE_F64:
  6590. case WASM_OP_F64_CONVERT_S_I32:
  6591. case WASM_OP_F64_CONVERT_U_I32:
  6592. case WASM_OP_F64_CONVERT_S_I64:
  6593. case WASM_OP_F64_CONVERT_U_I64:
  6594. case WASM_OP_F64_PROMOTE_F32:
  6595. case WASM_OP_I32_REINTERPRET_F32:
  6596. case WASM_OP_I64_REINTERPRET_F64:
  6597. case WASM_OP_F32_REINTERPRET_I32:
  6598. case WASM_OP_F64_REINTERPRET_I64:
  6599. case WASM_OP_I32_EXTEND8_S:
  6600. case WASM_OP_I32_EXTEND16_S:
  6601. case WASM_OP_I64_EXTEND8_S:
  6602. case WASM_OP_I64_EXTEND16_S:
  6603. case WASM_OP_I64_EXTEND32_S:
  6604. break;
  6605. #if WASM_ENABLE_GC != 0
  6606. case WASM_OP_GC_PREFIX:
  6607. {
  6608. uint32 opcode1;
  6609. read_leb_uint32(p, p_end, opcode1);
  6610. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6611. is no larger than UINT8_MAX */
  6612. opcode = (uint8)opcode1;
  6613. switch (opcode) {
  6614. case WASM_OP_STRUCT_NEW:
  6615. case WASM_OP_STRUCT_NEW_DEFAULT:
  6616. skip_leb_uint32(p, p_end); /* typeidx */
  6617. break;
  6618. case WASM_OP_STRUCT_GET:
  6619. case WASM_OP_STRUCT_GET_S:
  6620. case WASM_OP_STRUCT_GET_U:
  6621. case WASM_OP_STRUCT_SET:
  6622. skip_leb_uint32(p, p_end); /* typeidx */
  6623. skip_leb_uint32(p, p_end); /* fieldidx */
  6624. break;
  6625. case WASM_OP_ARRAY_NEW:
  6626. case WASM_OP_ARRAY_NEW_DEFAULT:
  6627. case WASM_OP_ARRAY_GET:
  6628. case WASM_OP_ARRAY_GET_S:
  6629. case WASM_OP_ARRAY_GET_U:
  6630. case WASM_OP_ARRAY_SET:
  6631. case WASM_OP_ARRAY_FILL:
  6632. skip_leb_uint32(p, p_end); /* typeidx */
  6633. break;
  6634. case WASM_OP_ARRAY_COPY:
  6635. skip_leb_uint32(p, p_end); /* typeidx1 */
  6636. skip_leb_uint32(p, p_end); /* typeidx2 */
  6637. break;
  6638. case WASM_OP_ARRAY_LEN:
  6639. break;
  6640. case WASM_OP_ARRAY_NEW_FIXED:
  6641. case WASM_OP_ARRAY_NEW_DATA:
  6642. case WASM_OP_ARRAY_NEW_ELEM:
  6643. skip_leb_uint32(p, p_end); /* typeidx */
  6644. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6645. break;
  6646. case WASM_OP_REF_I31:
  6647. case WASM_OP_I31_GET_S:
  6648. case WASM_OP_I31_GET_U:
  6649. break;
  6650. case WASM_OP_REF_TEST:
  6651. case WASM_OP_REF_CAST:
  6652. case WASM_OP_REF_TEST_NULLABLE:
  6653. case WASM_OP_REF_CAST_NULLABLE:
  6654. skip_leb_int32(p, p_end); /* heaptype */
  6655. break;
  6656. case WASM_OP_BR_ON_CAST:
  6657. case WASM_OP_BR_ON_CAST_FAIL:
  6658. p += sizeof(uint8); /* castflag */
  6659. skip_leb_uint32(p, p_end); /* labelidx */
  6660. skip_leb_int32(p, p_end); /* heaptype */
  6661. skip_leb_int32(p, p_end); /* heaptype2 */
  6662. break;
  6663. case WASM_OP_ANY_CONVERT_EXTERN:
  6664. case WASM_OP_EXTERN_CONVERT_ANY:
  6665. break;
  6666. #if WASM_ENABLE_STRINGREF != 0
  6667. case WASM_OP_STRING_NEW_UTF8:
  6668. case WASM_OP_STRING_NEW_WTF16:
  6669. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6670. case WASM_OP_STRING_NEW_WTF8:
  6671. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6672. break;
  6673. case WASM_OP_STRING_CONST:
  6674. skip_leb_int32(p, p_end); /* contents */
  6675. break;
  6676. case WASM_OP_STRING_MEASURE_UTF8:
  6677. case WASM_OP_STRING_MEASURE_WTF8:
  6678. case WASM_OP_STRING_MEASURE_WTF16:
  6679. break;
  6680. case WASM_OP_STRING_ENCODE_UTF8:
  6681. case WASM_OP_STRING_ENCODE_WTF16:
  6682. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6683. case WASM_OP_STRING_ENCODE_WTF8:
  6684. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6685. break;
  6686. case WASM_OP_STRING_CONCAT:
  6687. case WASM_OP_STRING_EQ:
  6688. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6689. case WASM_OP_STRING_AS_WTF8:
  6690. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6691. break;
  6692. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6693. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6694. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6695. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6696. break;
  6697. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6698. case WASM_OP_STRING_AS_WTF16:
  6699. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6700. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6701. break;
  6702. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6703. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6704. break;
  6705. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6706. case WASM_OP_STRING_AS_ITER:
  6707. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6708. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6709. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6710. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6711. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6712. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6713. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6714. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6715. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6716. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6717. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6718. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6719. break;
  6720. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6721. default:
  6722. return false;
  6723. }
  6724. break;
  6725. }
  6726. #endif /* end of WASM_ENABLE_GC != 0 */
  6727. case WASM_OP_MISC_PREFIX:
  6728. {
  6729. uint32 opcode1;
  6730. read_leb_uint32(p, p_end, opcode1);
  6731. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6732. is no larger than UINT8_MAX */
  6733. opcode = (uint8)opcode1;
  6734. switch (opcode) {
  6735. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6736. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6737. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6738. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6739. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6740. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6741. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6742. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6743. break;
  6744. #if WASM_ENABLE_BULK_MEMORY != 0
  6745. case WASM_OP_MEMORY_INIT:
  6746. skip_leb_uint32(p, p_end);
  6747. skip_leb_memidx(p, p_end);
  6748. break;
  6749. case WASM_OP_DATA_DROP:
  6750. skip_leb_uint32(p, p_end);
  6751. break;
  6752. case WASM_OP_MEMORY_COPY:
  6753. skip_leb_memidx(p, p_end);
  6754. skip_leb_memidx(p, p_end);
  6755. break;
  6756. case WASM_OP_MEMORY_FILL:
  6757. skip_leb_memidx(p, p_end);
  6758. break;
  6759. #endif /* WASM_ENABLE_BULK_MEMORY */
  6760. #if WASM_ENABLE_REF_TYPES != 0
  6761. case WASM_OP_TABLE_INIT:
  6762. case WASM_OP_TABLE_COPY:
  6763. /* tableidx */
  6764. skip_leb_uint32(p, p_end);
  6765. /* elemidx */
  6766. skip_leb_uint32(p, p_end);
  6767. break;
  6768. case WASM_OP_ELEM_DROP:
  6769. /* elemidx */
  6770. skip_leb_uint32(p, p_end);
  6771. break;
  6772. case WASM_OP_TABLE_SIZE:
  6773. case WASM_OP_TABLE_GROW:
  6774. case WASM_OP_TABLE_FILL:
  6775. skip_leb_uint32(p, p_end); /* table idx */
  6776. break;
  6777. #endif /* WASM_ENABLE_REF_TYPES */
  6778. default:
  6779. return false;
  6780. }
  6781. break;
  6782. }
  6783. #if WASM_ENABLE_SIMD != 0
  6784. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6785. case WASM_OP_SIMD_PREFIX:
  6786. {
  6787. uint32 opcode1;
  6788. read_leb_uint32(p, p_end, opcode1);
  6789. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6790. is no larger than UINT8_MAX */
  6791. opcode = (uint8)opcode1;
  6792. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6793. */
  6794. switch (opcode) {
  6795. case SIMD_v128_load:
  6796. case SIMD_v128_load8x8_s:
  6797. case SIMD_v128_load8x8_u:
  6798. case SIMD_v128_load16x4_s:
  6799. case SIMD_v128_load16x4_u:
  6800. case SIMD_v128_load32x2_s:
  6801. case SIMD_v128_load32x2_u:
  6802. case SIMD_v128_load8_splat:
  6803. case SIMD_v128_load16_splat:
  6804. case SIMD_v128_load32_splat:
  6805. case SIMD_v128_load64_splat:
  6806. case SIMD_v128_store:
  6807. /* memarg align */
  6808. skip_leb_uint32(p, p_end);
  6809. /* memarg offset */
  6810. skip_leb_mem_offset(p, p_end);
  6811. break;
  6812. case SIMD_v128_const:
  6813. case SIMD_v8x16_shuffle:
  6814. /* immByte[16] immLaneId[16] */
  6815. CHECK_BUF1(p, p_end, 16);
  6816. p += 16;
  6817. break;
  6818. case SIMD_i8x16_extract_lane_s:
  6819. case SIMD_i8x16_extract_lane_u:
  6820. case SIMD_i8x16_replace_lane:
  6821. case SIMD_i16x8_extract_lane_s:
  6822. case SIMD_i16x8_extract_lane_u:
  6823. case SIMD_i16x8_replace_lane:
  6824. case SIMD_i32x4_extract_lane:
  6825. case SIMD_i32x4_replace_lane:
  6826. case SIMD_i64x2_extract_lane:
  6827. case SIMD_i64x2_replace_lane:
  6828. case SIMD_f32x4_extract_lane:
  6829. case SIMD_f32x4_replace_lane:
  6830. case SIMD_f64x2_extract_lane:
  6831. case SIMD_f64x2_replace_lane:
  6832. /* ImmLaneId */
  6833. CHECK_BUF(p, p_end, 1);
  6834. p++;
  6835. break;
  6836. case SIMD_v128_load8_lane:
  6837. case SIMD_v128_load16_lane:
  6838. case SIMD_v128_load32_lane:
  6839. case SIMD_v128_load64_lane:
  6840. case SIMD_v128_store8_lane:
  6841. case SIMD_v128_store16_lane:
  6842. case SIMD_v128_store32_lane:
  6843. case SIMD_v128_store64_lane:
  6844. /* memarg align */
  6845. skip_leb_uint32(p, p_end);
  6846. /* memarg offset */
  6847. skip_leb_mem_offset(p, p_end);
  6848. /* ImmLaneId */
  6849. CHECK_BUF(p, p_end, 1);
  6850. p++;
  6851. break;
  6852. case SIMD_v128_load32_zero:
  6853. case SIMD_v128_load64_zero:
  6854. /* memarg align */
  6855. skip_leb_uint32(p, p_end);
  6856. /* memarg offset */
  6857. skip_leb_mem_offset(p, p_end);
  6858. break;
  6859. default:
  6860. /*
  6861. * since latest SIMD specific used almost every value
  6862. * from 0x00 to 0xff, the default branch will present
  6863. * all opcodes without imm
  6864. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6865. */
  6866. break;
  6867. }
  6868. break;
  6869. }
  6870. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6871. #endif /* end of WASM_ENABLE_SIMD */
  6872. #if WASM_ENABLE_SHARED_MEMORY != 0
  6873. case WASM_OP_ATOMIC_PREFIX:
  6874. {
  6875. uint32 opcode1;
  6876. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6877. read_leb_uint32(p, p_end, opcode1);
  6878. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6879. is no larger than UINT8_MAX */
  6880. opcode = (uint8)opcode1;
  6881. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6882. skip_leb_uint32(p, p_end); /* align */
  6883. skip_leb_mem_offset(p, p_end); /* offset */
  6884. }
  6885. else {
  6886. /* atomic.fence doesn't have memarg */
  6887. p++;
  6888. }
  6889. break;
  6890. }
  6891. #endif
  6892. #if WASM_ENABLE_DEBUG_INTERP != 0
  6893. case DEBUG_OP_BREAK:
  6894. {
  6895. WASMDebugInstance *debug_instance =
  6896. wasm_exec_env_get_instance(exec_env);
  6897. char original_opcode[1];
  6898. uint64 size = 1;
  6899. WASMModuleInstance *module_inst =
  6900. (WASMModuleInstance *)exec_env->module_inst;
  6901. uint64 offset = (p - 1) >= module_inst->module->load_addr
  6902. ? (p - 1) - module_inst->module->load_addr
  6903. : ~0;
  6904. if (debug_instance) {
  6905. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  6906. original_opcode, &size)
  6907. && size == 1) {
  6908. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  6909. "with %02x: ",
  6910. original_opcode[0]);
  6911. opcode = original_opcode[0];
  6912. goto op_break_retry;
  6913. }
  6914. }
  6915. break;
  6916. }
  6917. #endif
  6918. default:
  6919. return false;
  6920. }
  6921. }
  6922. (void)u8;
  6923. (void)exec_env;
  6924. return false;
  6925. fail:
  6926. return false;
  6927. }
  6928. #if WASM_ENABLE_FAST_INTERP != 0
  6929. #if WASM_DEBUG_PREPROCESSOR != 0
  6930. #define LOG_OP(...) os_printf(__VA_ARGS__)
  6931. #else
  6932. #define LOG_OP(...) (void)0
  6933. #endif
  6934. #define PATCH_ELSE 0
  6935. #define PATCH_END 1
  6936. typedef struct BranchBlockPatch {
  6937. struct BranchBlockPatch *next;
  6938. uint8 patch_type;
  6939. uint8 *code_compiled;
  6940. } BranchBlockPatch;
  6941. #endif
  6942. typedef struct BranchBlock {
  6943. uint8 label_type;
  6944. BlockType block_type;
  6945. uint8 *start_addr;
  6946. uint8 *else_addr;
  6947. uint8 *end_addr;
  6948. uint32 stack_cell_num;
  6949. #if WASM_ENABLE_GC != 0
  6950. uint32 reftype_map_num;
  6951. /* Indicate which local is used inside current block, used to validate
  6952. * local.get with non-nullable ref types */
  6953. uint8 *local_use_mask;
  6954. uint32 local_use_mask_size;
  6955. #endif
  6956. #if WASM_ENABLE_FAST_INTERP != 0
  6957. uint16 dynamic_offset;
  6958. uint8 *code_compiled;
  6959. BranchBlockPatch *patch_list;
  6960. /* This is used to save params frame_offset of of if block */
  6961. int16 *param_frame_offsets;
  6962. /* This is used to recover the dynamic offset for else branch,
  6963. * and also to remember the start offset of dynamic space which
  6964. * stores the block arguments for loop block, so we can use it
  6965. * to copy the stack operands to the loop block's arguments in
  6966. * wasm_loader_emit_br_info for opcode br. */
  6967. uint16 start_dynamic_offset;
  6968. #endif
  6969. /* Indicate the operand stack is in polymorphic state.
  6970. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  6971. * to polymorphic state until the block's 'end' opcode is processed.
  6972. * If stack is in polymorphic state and stack is empty, instruction can
  6973. * pop any type of value directly without decreasing stack top pointer
  6974. * and stack cell num. */
  6975. bool is_stack_polymorphic;
  6976. } BranchBlock;
  6977. typedef struct WASMLoaderContext {
  6978. /* frame ref stack */
  6979. uint8 *frame_ref;
  6980. uint8 *frame_ref_bottom;
  6981. uint8 *frame_ref_boundary;
  6982. uint32 frame_ref_size;
  6983. uint32 stack_cell_num;
  6984. uint32 max_stack_cell_num;
  6985. #if WASM_ENABLE_GC != 0
  6986. /* frame reftype map stack */
  6987. WASMRefTypeMap *frame_reftype_map;
  6988. WASMRefTypeMap *frame_reftype_map_bottom;
  6989. WASMRefTypeMap *frame_reftype_map_boundary;
  6990. uint32 frame_reftype_map_size;
  6991. uint32 reftype_map_num;
  6992. uint32 max_reftype_map_num;
  6993. /* Current module */
  6994. WASMModule *module;
  6995. /* Current module's ref_type_set */
  6996. HashMap *ref_type_set;
  6997. /* Always point to local variable ref_type of
  6998. wasm_loader_prepare_bytecode */
  6999. WASMRefType *ref_type_tmp;
  7000. #endif
  7001. /* frame csp stack */
  7002. BranchBlock *frame_csp;
  7003. BranchBlock *frame_csp_bottom;
  7004. BranchBlock *frame_csp_boundary;
  7005. uint32 frame_csp_size;
  7006. uint32 csp_num;
  7007. uint32 max_csp_num;
  7008. #if WASM_ENABLE_FAST_INTERP != 0
  7009. /* frame offset stack */
  7010. int16 *frame_offset;
  7011. int16 *frame_offset_bottom;
  7012. int16 *frame_offset_boundary;
  7013. uint32 frame_offset_size;
  7014. int16 dynamic_offset;
  7015. int16 start_dynamic_offset;
  7016. int16 max_dynamic_offset;
  7017. /* preserved local offset */
  7018. int16 preserved_local_offset;
  7019. /* const buffer */
  7020. uint8 *const_buf;
  7021. uint16 num_const;
  7022. uint16 const_cell_num;
  7023. uint32 const_buf_size;
  7024. /* processed code */
  7025. uint8 *p_code_compiled;
  7026. uint8 *p_code_compiled_end;
  7027. uint32 code_compiled_size;
  7028. /* If the last opcode will be dropped, the peak memory usage will be larger
  7029. * than the final code_compiled_size, we record the peak size to ensure
  7030. * there will not be invalid memory access during second traverse */
  7031. uint32 code_compiled_peak_size;
  7032. #endif
  7033. } WASMLoaderContext;
  7034. typedef struct Const {
  7035. WASMValue value;
  7036. uint16 slot_index;
  7037. uint8 value_type;
  7038. } Const;
  7039. #define CHECK_CSP_PUSH() \
  7040. do { \
  7041. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7042. MEM_REALLOC( \
  7043. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7044. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7045. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7046. ctx->frame_csp_boundary = \
  7047. ctx->frame_csp_bottom \
  7048. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7049. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7050. } \
  7051. } while (0)
  7052. #define CHECK_CSP_POP() \
  7053. do { \
  7054. if (ctx->csp_num < 1) { \
  7055. set_error_buf(error_buf, error_buf_size, \
  7056. "type mismatch: " \
  7057. "expect data but block stack was empty"); \
  7058. goto fail; \
  7059. } \
  7060. } while (0)
  7061. #if WASM_ENABLE_FAST_INTERP != 0
  7062. static bool
  7063. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7064. uint32 error_buf_size)
  7065. {
  7066. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7067. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7068. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7069. ctx->frame_offset_size + 16);
  7070. ctx->frame_offset_size += 16;
  7071. ctx->frame_offset_boundary =
  7072. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7073. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7074. }
  7075. return true;
  7076. fail:
  7077. return false;
  7078. }
  7079. static bool
  7080. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7081. {
  7082. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7083. return false;
  7084. return true;
  7085. }
  7086. static void
  7087. free_label_patch_list(BranchBlock *frame_csp)
  7088. {
  7089. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7090. BranchBlockPatch *next;
  7091. while (label_patch != NULL) {
  7092. next = label_patch->next;
  7093. wasm_runtime_free(label_patch);
  7094. label_patch = next;
  7095. }
  7096. frame_csp->patch_list = NULL;
  7097. }
  7098. static void
  7099. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7100. {
  7101. BranchBlock *tmp_csp = frame_csp;
  7102. uint32 i;
  7103. for (i = 0; i < csp_num; i++) {
  7104. free_label_patch_list(tmp_csp);
  7105. tmp_csp++;
  7106. }
  7107. }
  7108. static void
  7109. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7110. {
  7111. BranchBlock *tmp_csp = frame_csp;
  7112. uint32 i;
  7113. for (i = 0; i < csp_num; i++) {
  7114. if (tmp_csp->param_frame_offsets)
  7115. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7116. tmp_csp++;
  7117. }
  7118. }
  7119. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7120. #if WASM_ENABLE_GC != 0
  7121. static bool
  7122. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7123. char *error_buf, uint32 error_buf_size)
  7124. {
  7125. BranchBlock *current_csp = ctx->frame_csp - 1;
  7126. uint32 local_mask_size;
  7127. if (local_count == 0) {
  7128. current_csp->local_use_mask_size = 0;
  7129. return true;
  7130. }
  7131. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7132. * else branch, we don't need to allocate memory again */
  7133. if (!current_csp->local_use_mask) {
  7134. local_mask_size = (local_count + 7) / sizeof(uint8);
  7135. if (!(current_csp->local_use_mask =
  7136. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7137. return false;
  7138. }
  7139. current_csp->local_use_mask_size = local_mask_size;
  7140. }
  7141. else {
  7142. local_mask_size = current_csp->local_use_mask_size;
  7143. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7144. }
  7145. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7146. /* For non-function blocks, inherit the use status from parent block */
  7147. BranchBlock *parent_csp = current_csp - 1;
  7148. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7149. bh_assert(parent_csp->local_use_mask);
  7150. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7151. parent_csp->local_use_mask, local_mask_size);
  7152. }
  7153. return true;
  7154. }
  7155. static void
  7156. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7157. {
  7158. BranchBlock *current_csp = ctx->frame_csp - 1;
  7159. bh_assert(current_csp->local_use_mask
  7160. || current_csp->local_use_mask_size == 0);
  7161. if (current_csp->local_use_mask) {
  7162. wasm_runtime_free(current_csp->local_use_mask);
  7163. }
  7164. current_csp->local_use_mask = NULL;
  7165. current_csp->local_use_mask_size = 0;
  7166. }
  7167. static void
  7168. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7169. {
  7170. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7171. uint32 i;
  7172. for (i = 0; i < ctx->csp_num; i++) {
  7173. if (tmp_csp->local_use_mask) {
  7174. wasm_runtime_free(tmp_csp->local_use_mask);
  7175. tmp_csp->local_use_mask = NULL;
  7176. tmp_csp->local_use_mask_size = 0;
  7177. }
  7178. tmp_csp++;
  7179. }
  7180. }
  7181. static void
  7182. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7183. {
  7184. BranchBlock *current_csp = ctx->frame_csp - 1;
  7185. uint32 byte_offset = index / sizeof(uint8);
  7186. uint32 bit_offset = index % sizeof(uint8);
  7187. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7188. bh_assert(current_csp->local_use_mask);
  7189. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7190. }
  7191. static bool
  7192. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7193. {
  7194. BranchBlock *current_csp = ctx->frame_csp - 1;
  7195. uint32 byte_offset = index / sizeof(uint8);
  7196. uint32 bit_offset = index % sizeof(uint8);
  7197. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7198. bh_assert(current_csp->local_use_mask);
  7199. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7200. ? true
  7201. : false;
  7202. }
  7203. #endif /* end of WASM_ENABLE_GC != 0 */
  7204. static void
  7205. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7206. {
  7207. if (ctx) {
  7208. if (ctx->frame_ref_bottom)
  7209. wasm_runtime_free(ctx->frame_ref_bottom);
  7210. #if WASM_ENABLE_GC != 0
  7211. if (ctx->frame_reftype_map_bottom)
  7212. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7213. #endif
  7214. if (ctx->frame_csp_bottom) {
  7215. #if WASM_ENABLE_FAST_INTERP != 0
  7216. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7217. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7218. ctx->csp_num);
  7219. #endif
  7220. #if WASM_ENABLE_GC != 0
  7221. wasm_loader_clean_all_local_use_masks(ctx);
  7222. #endif
  7223. wasm_runtime_free(ctx->frame_csp_bottom);
  7224. }
  7225. #if WASM_ENABLE_FAST_INTERP != 0
  7226. if (ctx->frame_offset_bottom)
  7227. wasm_runtime_free(ctx->frame_offset_bottom);
  7228. if (ctx->const_buf)
  7229. wasm_runtime_free(ctx->const_buf);
  7230. #endif
  7231. wasm_runtime_free(ctx);
  7232. }
  7233. }
  7234. static WASMLoaderContext *
  7235. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7236. {
  7237. WASMLoaderContext *loader_ctx =
  7238. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7239. if (!loader_ctx)
  7240. return NULL;
  7241. loader_ctx->frame_ref_size = 32;
  7242. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7243. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7244. goto fail;
  7245. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7246. #if WASM_ENABLE_GC != 0
  7247. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7248. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7249. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7250. error_buf_size)))
  7251. goto fail;
  7252. loader_ctx->frame_reftype_map_boundary =
  7253. loader_ctx->frame_reftype_map_bottom + 16;
  7254. #endif
  7255. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7256. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7257. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7258. goto fail;
  7259. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7260. #if WASM_ENABLE_EXCE_HANDLING != 0
  7261. func->exception_handler_count = 0;
  7262. #endif
  7263. #if WASM_ENABLE_FAST_INTERP != 0
  7264. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7265. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7266. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7267. error_buf_size)))
  7268. goto fail;
  7269. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7270. loader_ctx->num_const = 0;
  7271. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7272. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7273. error_buf, error_buf_size)))
  7274. goto fail;
  7275. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7276. set_error_buf(error_buf, error_buf_size,
  7277. "fast interpreter offset overflow");
  7278. goto fail;
  7279. }
  7280. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7281. loader_ctx->max_dynamic_offset =
  7282. func->param_cell_num + func->local_cell_num;
  7283. #endif
  7284. return loader_ctx;
  7285. fail:
  7286. wasm_loader_ctx_destroy(loader_ctx);
  7287. return NULL;
  7288. }
  7289. static bool
  7290. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7291. uint32 error_buf_size)
  7292. {
  7293. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7294. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7295. /* Increase the frame ref stack */
  7296. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7297. ctx->frame_ref_size + 16);
  7298. ctx->frame_ref_size += 16;
  7299. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7300. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7301. }
  7302. #if WASM_ENABLE_GC != 0
  7303. if (wasm_is_type_multi_byte_type(type)
  7304. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7305. /* Increase the frame reftype map stack */
  7306. bh_assert(
  7307. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7308. * sizeof(WASMRefTypeMap))
  7309. == ctx->frame_reftype_map_size);
  7310. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7311. ctx->frame_reftype_map_size
  7312. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7313. ctx->frame_reftype_map =
  7314. ctx->frame_reftype_map_bottom
  7315. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7316. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7317. ctx->frame_reftype_map_boundary =
  7318. ctx->frame_reftype_map_bottom
  7319. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7320. }
  7321. #endif
  7322. return true;
  7323. fail:
  7324. return false;
  7325. }
  7326. static bool
  7327. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7328. uint32 error_buf_size)
  7329. {
  7330. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7331. uint32 i;
  7332. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7333. return false;
  7334. #if WASM_ENABLE_GC != 0
  7335. if (wasm_is_type_multi_byte_type(type)) {
  7336. WASMRefType *ref_type;
  7337. if (!(ref_type =
  7338. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7339. error_buf, error_buf_size))) {
  7340. return false;
  7341. }
  7342. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7343. /* Increase the frame reftype map stack */
  7344. bh_assert((uint32)((ctx->frame_reftype_map
  7345. - ctx->frame_reftype_map_bottom)
  7346. * sizeof(WASMRefTypeMap))
  7347. == ctx->frame_reftype_map_size);
  7348. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7349. ctx->frame_reftype_map_size,
  7350. ctx->frame_reftype_map_size
  7351. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7352. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7353. + ctx->frame_reftype_map_size
  7354. / ((uint32)sizeof(WASMRefTypeMap));
  7355. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7356. ctx->frame_reftype_map_boundary =
  7357. ctx->frame_reftype_map_bottom
  7358. + ctx->frame_reftype_map_size
  7359. / ((uint32)sizeof(WASMRefTypeMap));
  7360. }
  7361. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7362. ctx->frame_reftype_map->ref_type = ref_type;
  7363. ctx->frame_reftype_map++;
  7364. ctx->reftype_map_num++;
  7365. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7366. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7367. }
  7368. #endif
  7369. for (i = 0; i < type_cell_num; i++)
  7370. *ctx->frame_ref++ = type;
  7371. ctx->stack_cell_num += type_cell_num;
  7372. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7373. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7374. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7375. set_error_buf(error_buf, error_buf_size,
  7376. "operand stack depth limit exceeded");
  7377. return false;
  7378. }
  7379. }
  7380. return true;
  7381. #if WASM_ENABLE_GC != 0
  7382. fail:
  7383. return false;
  7384. #endif
  7385. }
  7386. static bool
  7387. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7388. int32 stack_cell_num,
  7389. #if WASM_ENABLE_GC != 0
  7390. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7391. #endif
  7392. uint8 type,
  7393. #if WASM_ENABLE_GC != 0
  7394. WASMRefType *ref_type,
  7395. #endif
  7396. char *error_buf, uint32 error_buf_size)
  7397. {
  7398. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7399. #if WASM_ENABLE_GC != 0
  7400. WASMRefType *frame_reftype = NULL;
  7401. #endif
  7402. if (stack_cell_num < type_cell_num) {
  7403. set_error_buf(error_buf, error_buf_size,
  7404. "type mismatch: expect data but stack was empty");
  7405. return false;
  7406. }
  7407. #if WASM_ENABLE_GC == 0
  7408. for (i = 0; i < type_cell_num; i++) {
  7409. if (*(frame_ref - 1 - i) != type) {
  7410. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7411. "type mismatch: expect ", type2str(type),
  7412. " but got other");
  7413. return false;
  7414. }
  7415. }
  7416. #else
  7417. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7418. bh_assert(reftype_map_num > 0);
  7419. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7420. }
  7421. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7422. ref_type, ctx->module->types,
  7423. ctx->module->type_count)) {
  7424. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7425. "type mismatch: expect ", type2str(type),
  7426. " but got other");
  7427. return false;
  7428. }
  7429. for (i = 0; i < type_cell_num - 1; i++) {
  7430. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7431. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7432. "type mismatch: expect ", type2str(type),
  7433. " but got other");
  7434. return false;
  7435. }
  7436. }
  7437. #endif
  7438. return true;
  7439. }
  7440. static bool
  7441. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7442. uint32 error_buf_size)
  7443. {
  7444. int32 block_stack_cell_num =
  7445. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7446. #if WASM_ENABLE_GC != 0
  7447. int32 reftype_map_num =
  7448. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7449. #endif
  7450. if (block_stack_cell_num > 0) {
  7451. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7452. /* the stack top is a value of any type, return success */
  7453. return true;
  7454. }
  7455. #if WASM_ENABLE_GC != 0
  7456. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7457. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7458. WASMRefType *stack_top_ref_type = NULL;
  7459. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7460. bh_assert(reftype_map_num > 0);
  7461. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7462. }
  7463. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7464. ctx->ref_type_tmp, ctx->module->types,
  7465. ctx->module->type_count)) {
  7466. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7467. uint32 ref_type_struct_size =
  7468. wasm_reftype_struct_size(stack_top_ref_type);
  7469. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7470. stack_top_ref_type, ref_type_struct_size);
  7471. }
  7472. return true;
  7473. }
  7474. }
  7475. #endif
  7476. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7477. #if WASM_ENABLE_GC != 0
  7478. ctx->frame_reftype_map, reftype_map_num,
  7479. #endif
  7480. type,
  7481. #if WASM_ENABLE_GC != 0
  7482. ctx->ref_type_tmp,
  7483. #endif
  7484. error_buf, error_buf_size)) {
  7485. return false;
  7486. }
  7487. return true;
  7488. }
  7489. static bool
  7490. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7491. uint32 error_buf_size)
  7492. {
  7493. BranchBlock *cur_block = ctx->frame_csp - 1;
  7494. int32 available_stack_cell =
  7495. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7496. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7497. /* Directly return success if current block is in stack
  7498. polymorphic state while stack is empty. */
  7499. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7500. return true;
  7501. if (type == VALUE_TYPE_VOID)
  7502. return true;
  7503. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7504. return false;
  7505. bh_assert(available_stack_cell > 0);
  7506. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7507. type = VALUE_TYPE_ANY;
  7508. cell_num_to_pop = 1;
  7509. }
  7510. ctx->frame_ref -= cell_num_to_pop;
  7511. ctx->stack_cell_num -= cell_num_to_pop;
  7512. #if WASM_ENABLE_GC != 0
  7513. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7514. ctx->frame_reftype_map--;
  7515. ctx->reftype_map_num--;
  7516. }
  7517. #endif
  7518. return true;
  7519. }
  7520. #if WASM_ENABLE_GC != 0
  7521. /* Get the stack top element of current block */
  7522. static bool
  7523. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7524. WASMRefType **p_ref_type, char *error_buf,
  7525. uint32 error_buf_size)
  7526. {
  7527. BranchBlock *cur_block = ctx->frame_csp - 1;
  7528. int32 available_stack_cell =
  7529. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7530. if (available_stack_cell <= 0) {
  7531. /* Directly return success if current block is in stack
  7532. polymorphic state while stack is empty. */
  7533. if (cur_block->is_stack_polymorphic) {
  7534. *p_type = VALUE_TYPE_ANY;
  7535. return true;
  7536. }
  7537. else {
  7538. set_error_buf(
  7539. error_buf, error_buf_size,
  7540. "type mismatch: expect data but block stack was empty");
  7541. return false;
  7542. }
  7543. }
  7544. *p_type = *(ctx->frame_ref - 1);
  7545. if (wasm_is_type_multi_byte_type(*p_type)) {
  7546. int32 available_reftype_map =
  7547. (int32)(ctx->reftype_map_num
  7548. - (ctx->frame_csp - 1)->reftype_map_num);
  7549. bh_assert(available_reftype_map > 0);
  7550. (void)available_reftype_map;
  7551. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7552. }
  7553. return true;
  7554. }
  7555. #if WASM_ENABLE_FAST_INTERP != 0
  7556. static bool
  7557. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7558. char *error_buf, uint32 error_buf_size);
  7559. #endif
  7560. /* Check whether the stack top elem is a heap object, and if yes,
  7561. pop and return it */
  7562. static bool
  7563. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7564. WASMRefType *ref_ht_ret, char *error_buf,
  7565. uint32 error_buf_size)
  7566. {
  7567. uint8 type = 0;
  7568. WASMRefType *ref_type = NULL;
  7569. /* Get stack top element */
  7570. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7571. error_buf_size)) {
  7572. return false;
  7573. }
  7574. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7575. /* stack top isn't a ref type */
  7576. && !wasm_is_type_reftype(type)) {
  7577. set_error_buf(error_buf, error_buf_size,
  7578. "type mismatch: expect heap object but got others");
  7579. return false;
  7580. }
  7581. /* POP stack top */
  7582. if (wasm_is_type_multi_byte_type(type)) {
  7583. bh_assert(ref_type);
  7584. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7585. wasm_reftype_struct_size(ref_type));
  7586. }
  7587. #if WASM_ENABLE_FAST_INTERP != 0
  7588. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7589. error_buf_size)) {
  7590. return false;
  7591. }
  7592. #else
  7593. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7594. return false;
  7595. }
  7596. #endif
  7597. if (p_type)
  7598. *p_type = type;
  7599. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7600. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7601. wasm_reftype_struct_size(ref_type));
  7602. }
  7603. return true;
  7604. }
  7605. /* Check whether the stack top elem is subtype of (ref null ht),
  7606. and if yes, pop it and return the converted (ref ht) */
  7607. static bool
  7608. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7609. WASMRefType *ref_ht_ret, char *error_buf,
  7610. uint32 error_buf_size)
  7611. {
  7612. uint8 type = 0;
  7613. WASMRefType ref_type = { 0 };
  7614. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7615. error_buf_size)) {
  7616. return false;
  7617. }
  7618. /* Convert to related (ref ht) and return */
  7619. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7620. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7621. */
  7622. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7623. HEAP_TYPE_ARRAY
  7624. + (type - REF_TYPE_ARRAYREF));
  7625. type = ref_ht_ret->ref_type;
  7626. }
  7627. else if (wasm_is_reftype_htref_nullable(type)
  7628. || wasm_is_reftype_htref_non_nullable(type)) {
  7629. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7630. wasm_reftype_struct_size(&ref_type));
  7631. /* Convert to (ref ht) */
  7632. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7633. ref_ht_ret->ref_ht_common.nullable = false;
  7634. type = ref_ht_ret->ref_type;
  7635. }
  7636. *p_type = type;
  7637. return true;
  7638. }
  7639. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7640. and if yes, pop it and return the type_idx */
  7641. static bool
  7642. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7643. uint32 *p_type_idx, char *error_buf,
  7644. uint32 error_buf_size)
  7645. {
  7646. uint8 type = 0;
  7647. int32 type_idx = -1;
  7648. WASMRefType *ref_type = NULL;
  7649. /* Get stack top element */
  7650. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7651. error_buf_size)) {
  7652. return false;
  7653. }
  7654. if (type != VALUE_TYPE_ANY) {
  7655. /* stack top isn't (ref null $t) */
  7656. if (!((wasm_is_reftype_htref_nullable(type)
  7657. || wasm_is_reftype_htref_non_nullable(type))
  7658. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7659. set_error_buf(error_buf, error_buf_size,
  7660. "type mismatch: expect (ref null $t) but got others");
  7661. return false;
  7662. }
  7663. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7664. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7665. wasm_reftype_struct_size(ref_type));
  7666. }
  7667. /* POP stack top */
  7668. #if WASM_ENABLE_FAST_INTERP != 0
  7669. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7670. error_buf_size)) {
  7671. return false;
  7672. }
  7673. #else
  7674. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7675. return false;
  7676. }
  7677. #endif
  7678. /* Convert to type_idx and return */
  7679. *p_type = type;
  7680. if (type != VALUE_TYPE_ANY)
  7681. *p_type_idx = (uint32)type_idx;
  7682. return true;
  7683. }
  7684. #endif /* WASM_ENABLE_GC != 0 */
  7685. #if WASM_ENABLE_FAST_INTERP == 0
  7686. static bool
  7687. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7688. uint8 type_push, uint8 type_pop, char *error_buf,
  7689. uint32 error_buf_size)
  7690. {
  7691. for (int i = 0; i < pop_cnt; i++) {
  7692. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7693. error_buf_size))
  7694. return false;
  7695. }
  7696. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7697. return false;
  7698. return true;
  7699. }
  7700. #endif
  7701. static bool
  7702. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7703. BlockType block_type, uint8 *start_addr,
  7704. char *error_buf, uint32 error_buf_size)
  7705. {
  7706. CHECK_CSP_PUSH();
  7707. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7708. ctx->frame_csp->label_type = label_type;
  7709. ctx->frame_csp->block_type = block_type;
  7710. ctx->frame_csp->start_addr = start_addr;
  7711. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7712. #if WASM_ENABLE_GC != 0
  7713. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7714. #endif
  7715. #if WASM_ENABLE_FAST_INTERP != 0
  7716. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7717. ctx->frame_csp->patch_list = NULL;
  7718. #endif
  7719. ctx->frame_csp++;
  7720. ctx->csp_num++;
  7721. if (ctx->csp_num > ctx->max_csp_num) {
  7722. ctx->max_csp_num = ctx->csp_num;
  7723. if (ctx->max_csp_num > UINT16_MAX) {
  7724. set_error_buf(error_buf, error_buf_size,
  7725. "label stack depth limit exceeded");
  7726. return false;
  7727. }
  7728. }
  7729. return true;
  7730. fail:
  7731. return false;
  7732. }
  7733. static bool
  7734. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7735. uint32 error_buf_size)
  7736. {
  7737. CHECK_CSP_POP();
  7738. #if WASM_ENABLE_FAST_INTERP != 0
  7739. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7740. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7741. #endif
  7742. ctx->frame_csp--;
  7743. ctx->csp_num--;
  7744. return true;
  7745. fail:
  7746. return false;
  7747. }
  7748. #if WASM_ENABLE_FAST_INTERP != 0
  7749. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7750. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7751. #define emit_label(opcode) \
  7752. do { \
  7753. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7754. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7755. } while (0)
  7756. #define skip_label() \
  7757. do { \
  7758. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7759. LOG_OP("\ndelete last op\n"); \
  7760. } while (0)
  7761. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7762. #if UINTPTR_MAX == UINT64_MAX
  7763. #define emit_label(opcode) \
  7764. do { \
  7765. int32 offset = \
  7766. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7767. /* emit int32 relative offset in 64-bit target */ \
  7768. wasm_loader_emit_uint32(loader_ctx, offset); \
  7769. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7770. } while (0)
  7771. #else
  7772. #define emit_label(opcode) \
  7773. do { \
  7774. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7775. /* emit uint32 label address in 32-bit target */ \
  7776. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7777. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7778. } while (0)
  7779. #endif
  7780. #define skip_label() \
  7781. do { \
  7782. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7783. LOG_OP("\ndelete last op\n"); \
  7784. } while (0)
  7785. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7786. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7787. #define emit_label(opcode) \
  7788. do { \
  7789. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7790. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7791. } while (0)
  7792. #define skip_label() \
  7793. do { \
  7794. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7795. LOG_OP("\ndelete last op\n"); \
  7796. } while (0)
  7797. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7798. #define emit_empty_label_addr_and_frame_ip(type) \
  7799. do { \
  7800. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7801. loader_ctx->p_code_compiled, error_buf, \
  7802. error_buf_size)) \
  7803. goto fail; \
  7804. /* label address, to be patched */ \
  7805. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7806. } while (0)
  7807. #define emit_br_info(frame_csp, is_br) \
  7808. do { \
  7809. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7810. error_buf_size)) \
  7811. goto fail; \
  7812. } while (0)
  7813. #define LAST_OP_OUTPUT_I32() \
  7814. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7815. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7816. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7817. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7818. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7819. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7820. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7821. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7822. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7823. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7824. || (last_op == EXT_OP_COPY_STACK_TOP)
  7825. #define LAST_OP_OUTPUT_I64() \
  7826. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7827. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7828. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7829. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7830. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7831. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7832. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7833. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7834. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7835. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7836. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7837. #define GET_CONST_OFFSET(type, val) \
  7838. do { \
  7839. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7840. &operand_offset, error_buf, \
  7841. error_buf_size))) \
  7842. goto fail; \
  7843. } while (0)
  7844. #define GET_CONST_F32_OFFSET(type, fval) \
  7845. do { \
  7846. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7847. &operand_offset, error_buf, \
  7848. error_buf_size))) \
  7849. goto fail; \
  7850. } while (0)
  7851. #define GET_CONST_F64_OFFSET(type, fval) \
  7852. do { \
  7853. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7854. &operand_offset, error_buf, \
  7855. error_buf_size))) \
  7856. goto fail; \
  7857. } while (0)
  7858. #define emit_operand(ctx, offset) \
  7859. do { \
  7860. wasm_loader_emit_int16(ctx, offset); \
  7861. LOG_OP("%d\t", offset); \
  7862. } while (0)
  7863. #define emit_byte(ctx, byte) \
  7864. do { \
  7865. wasm_loader_emit_uint8(ctx, byte); \
  7866. LOG_OP("%d\t", byte); \
  7867. } while (0)
  7868. #define emit_uint32(ctx, value) \
  7869. do { \
  7870. wasm_loader_emit_uint32(ctx, value); \
  7871. LOG_OP("%d\t", value); \
  7872. } while (0)
  7873. #define emit_uint64(ctx, value) \
  7874. do { \
  7875. wasm_loader_emit_const(ctx, &value, false); \
  7876. LOG_OP("%lld\t", value); \
  7877. } while (0)
  7878. #define emit_float32(ctx, value) \
  7879. do { \
  7880. wasm_loader_emit_const(ctx, &value, true); \
  7881. LOG_OP("%f\t", value); \
  7882. } while (0)
  7883. #define emit_float64(ctx, value) \
  7884. do { \
  7885. wasm_loader_emit_const(ctx, &value, false); \
  7886. LOG_OP("%f\t", value); \
  7887. } while (0)
  7888. static bool
  7889. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7890. {
  7891. if (!(ctx->p_code_compiled =
  7892. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7893. return false;
  7894. ctx->p_code_compiled_end =
  7895. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  7896. /* clean up frame ref */
  7897. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  7898. ctx->frame_ref = ctx->frame_ref_bottom;
  7899. ctx->stack_cell_num = 0;
  7900. #if WASM_ENABLE_GC != 0
  7901. /* clean up reftype map */
  7902. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  7903. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  7904. ctx->reftype_map_num = 0;
  7905. #endif
  7906. /* clean up frame csp */
  7907. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  7908. ctx->frame_csp = ctx->frame_csp_bottom;
  7909. ctx->csp_num = 0;
  7910. ctx->max_csp_num = 0;
  7911. /* clean up frame offset */
  7912. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  7913. ctx->frame_offset = ctx->frame_offset_bottom;
  7914. ctx->dynamic_offset = ctx->start_dynamic_offset;
  7915. /* init preserved local offsets */
  7916. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  7917. /* const buf is reserved */
  7918. return true;
  7919. }
  7920. static void
  7921. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  7922. {
  7923. ctx->code_compiled_size += size;
  7924. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  7925. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  7926. }
  7927. }
  7928. static void
  7929. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  7930. {
  7931. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  7932. if (ctx->p_code_compiled) {
  7933. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7934. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7935. #endif
  7936. bh_memcpy_s(ctx->p_code_compiled,
  7937. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  7938. value, size);
  7939. ctx->p_code_compiled += size;
  7940. }
  7941. else {
  7942. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7943. bh_assert((ctx->code_compiled_size & 1) == 0);
  7944. #endif
  7945. increase_compiled_code_space(ctx, size);
  7946. }
  7947. }
  7948. static void
  7949. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  7950. {
  7951. if (ctx->p_code_compiled) {
  7952. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7953. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7954. #endif
  7955. STORE_U32(ctx->p_code_compiled, value);
  7956. ctx->p_code_compiled += sizeof(uint32);
  7957. }
  7958. else {
  7959. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7960. bh_assert((ctx->code_compiled_size & 1) == 0);
  7961. #endif
  7962. increase_compiled_code_space(ctx, sizeof(uint32));
  7963. }
  7964. }
  7965. static void
  7966. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  7967. {
  7968. if (ctx->p_code_compiled) {
  7969. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7970. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7971. #endif
  7972. STORE_U16(ctx->p_code_compiled, (uint16)value);
  7973. ctx->p_code_compiled += sizeof(int16);
  7974. }
  7975. else {
  7976. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7977. bh_assert((ctx->code_compiled_size & 1) == 0);
  7978. #endif
  7979. increase_compiled_code_space(ctx, sizeof(uint16));
  7980. }
  7981. }
  7982. static void
  7983. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  7984. {
  7985. if (ctx->p_code_compiled) {
  7986. *(ctx->p_code_compiled) = value;
  7987. ctx->p_code_compiled += sizeof(uint8);
  7988. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7989. ctx->p_code_compiled++;
  7990. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7991. #endif
  7992. }
  7993. else {
  7994. increase_compiled_code_space(ctx, sizeof(uint8));
  7995. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7996. increase_compiled_code_space(ctx, sizeof(uint8));
  7997. bh_assert((ctx->code_compiled_size & 1) == 0);
  7998. #endif
  7999. }
  8000. }
  8001. static void
  8002. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8003. {
  8004. if (ctx->p_code_compiled) {
  8005. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8006. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8007. #endif
  8008. STORE_PTR(ctx->p_code_compiled, value);
  8009. ctx->p_code_compiled += sizeof(void *);
  8010. }
  8011. else {
  8012. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8013. bh_assert((ctx->code_compiled_size & 1) == 0);
  8014. #endif
  8015. increase_compiled_code_space(ctx, sizeof(void *));
  8016. }
  8017. }
  8018. static void
  8019. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8020. {
  8021. if (ctx->p_code_compiled) {
  8022. ctx->p_code_compiled -= size;
  8023. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8024. if (size == sizeof(uint8)) {
  8025. ctx->p_code_compiled--;
  8026. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8027. }
  8028. #endif
  8029. }
  8030. else {
  8031. ctx->code_compiled_size -= size;
  8032. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8033. if (size == sizeof(uint8)) {
  8034. ctx->code_compiled_size--;
  8035. bh_assert((ctx->code_compiled_size & 1) == 0);
  8036. }
  8037. #endif
  8038. }
  8039. }
  8040. static bool
  8041. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8042. uint32 local_index, uint32 local_type,
  8043. bool *preserved, char *error_buf,
  8044. uint32 error_buf_size)
  8045. {
  8046. uint32 i = 0;
  8047. int16 preserved_offset = (int16)local_index;
  8048. *preserved = false;
  8049. while (i < loader_ctx->stack_cell_num) {
  8050. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8051. /* move previous local into dynamic space before a set/tee_local opcode
  8052. */
  8053. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8054. if (!(*preserved)) {
  8055. *preserved = true;
  8056. skip_label();
  8057. preserved_offset = loader_ctx->preserved_local_offset;
  8058. if (loader_ctx->p_code_compiled) {
  8059. bh_assert(preserved_offset != (int16)local_index);
  8060. }
  8061. if (is_32bit_type(local_type)) {
  8062. /* Only increase preserve offset in the second traversal */
  8063. if (loader_ctx->p_code_compiled)
  8064. loader_ctx->preserved_local_offset++;
  8065. emit_label(EXT_OP_COPY_STACK_TOP);
  8066. }
  8067. else {
  8068. if (loader_ctx->p_code_compiled)
  8069. loader_ctx->preserved_local_offset += 2;
  8070. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8071. }
  8072. emit_operand(loader_ctx, local_index);
  8073. emit_operand(loader_ctx, preserved_offset);
  8074. emit_label(opcode);
  8075. }
  8076. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8077. }
  8078. if (is_32bit_type(cur_type))
  8079. i++;
  8080. else
  8081. i += 2;
  8082. }
  8083. (void)error_buf;
  8084. (void)error_buf_size;
  8085. return true;
  8086. }
  8087. static bool
  8088. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8089. char *error_buf, uint32 error_buf_size)
  8090. {
  8091. uint32 i = 0;
  8092. bool preserve_local;
  8093. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8094. blocks will not influence the value of these locals */
  8095. while (i < loader_ctx->stack_cell_num) {
  8096. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8097. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8098. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8099. && (cur_offset >= 0)) {
  8100. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8101. cur_type, &preserve_local,
  8102. error_buf, error_buf_size)))
  8103. return false;
  8104. }
  8105. if (is_32bit_type(cur_type)) {
  8106. i++;
  8107. }
  8108. else {
  8109. i += 2;
  8110. }
  8111. }
  8112. return true;
  8113. }
  8114. static bool
  8115. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8116. uint8 *p_code_compiled, char *error_buf,
  8117. uint32 error_buf_size)
  8118. {
  8119. BranchBlockPatch *patch =
  8120. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8121. if (!patch) {
  8122. return false;
  8123. }
  8124. patch->patch_type = patch_type;
  8125. patch->code_compiled = p_code_compiled;
  8126. if (!frame_csp->patch_list) {
  8127. frame_csp->patch_list = patch;
  8128. patch->next = NULL;
  8129. }
  8130. else {
  8131. patch->next = frame_csp->patch_list;
  8132. frame_csp->patch_list = patch;
  8133. }
  8134. return true;
  8135. }
  8136. static void
  8137. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8138. {
  8139. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8140. BranchBlockPatch *node = frame_csp->patch_list;
  8141. BranchBlockPatch *node_prev = NULL, *node_next;
  8142. if (!ctx->p_code_compiled)
  8143. return;
  8144. while (node) {
  8145. node_next = node->next;
  8146. if (node->patch_type == patch_type) {
  8147. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8148. if (node_prev == NULL) {
  8149. frame_csp->patch_list = node_next;
  8150. }
  8151. else {
  8152. node_prev->next = node_next;
  8153. }
  8154. wasm_runtime_free(node);
  8155. }
  8156. else {
  8157. node_prev = node;
  8158. }
  8159. node = node_next;
  8160. }
  8161. }
  8162. static bool
  8163. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8164. bool is_br, char *error_buf, uint32 error_buf_size)
  8165. {
  8166. /* br info layout:
  8167. * a) arity of target block
  8168. * b) total cell num of arity values
  8169. * c) each arity value's cell num
  8170. * d) each arity value's src frame offset
  8171. * e) each arity values's dst dynamic offset
  8172. * f) branch target address
  8173. *
  8174. * Note: b-e are omitted when arity is 0 so that
  8175. * interpreter can recover the br info quickly.
  8176. */
  8177. BlockType *block_type = &frame_csp->block_type;
  8178. uint8 *types = NULL, cell;
  8179. #if WASM_ENABLE_GC != 0
  8180. WASMRefTypeMap *reftype_maps;
  8181. uint32 reftype_map_count;
  8182. #endif
  8183. uint32 arity = 0;
  8184. int32 i;
  8185. int16 *frame_offset = ctx->frame_offset;
  8186. uint16 dynamic_offset;
  8187. /* Note: loop's arity is different from if and block. loop's arity is
  8188. * its parameter count while if and block arity is result count.
  8189. */
  8190. #if WASM_ENABLE_GC == 0
  8191. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8192. arity = block_type_get_param_types(block_type, &types);
  8193. else
  8194. arity = block_type_get_result_types(block_type, &types);
  8195. #else
  8196. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8197. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8198. &reftype_map_count);
  8199. else
  8200. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8201. &reftype_map_count);
  8202. #endif
  8203. /* Part a */
  8204. emit_uint32(ctx, arity);
  8205. if (arity) {
  8206. /* Part b */
  8207. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8208. /* Part c */
  8209. for (i = (int32)arity - 1; i >= 0; i--) {
  8210. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8211. emit_byte(ctx, cell);
  8212. }
  8213. /* Part d */
  8214. for (i = (int32)arity - 1; i >= 0; i--) {
  8215. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8216. frame_offset -= cell;
  8217. emit_operand(ctx, *(int16 *)(frame_offset));
  8218. }
  8219. /* Part e */
  8220. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8221. /* Use start_dynamic_offset which was set in
  8222. copy_params_to_dynamic_space */
  8223. dynamic_offset = frame_csp->start_dynamic_offset
  8224. + wasm_get_cell_num(types, arity);
  8225. else
  8226. dynamic_offset =
  8227. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8228. if (is_br)
  8229. ctx->dynamic_offset = dynamic_offset;
  8230. for (i = (int32)arity - 1; i >= 0; i--) {
  8231. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8232. dynamic_offset -= cell;
  8233. emit_operand(ctx, dynamic_offset);
  8234. }
  8235. }
  8236. /* Part f */
  8237. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8238. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8239. }
  8240. else {
  8241. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8242. error_buf, error_buf_size))
  8243. return false;
  8244. /* label address, to be patched */
  8245. wasm_loader_emit_ptr(ctx, NULL);
  8246. }
  8247. return true;
  8248. }
  8249. static bool
  8250. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8251. bool disable_emit, int16 operand_offset,
  8252. char *error_buf, uint32 error_buf_size)
  8253. {
  8254. uint32 cell_num_to_push, i;
  8255. if (type == VALUE_TYPE_VOID)
  8256. return true;
  8257. /* only check memory overflow in first traverse */
  8258. if (ctx->p_code_compiled == NULL) {
  8259. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8260. return false;
  8261. }
  8262. if (disable_emit)
  8263. *(ctx->frame_offset)++ = operand_offset;
  8264. else {
  8265. emit_operand(ctx, ctx->dynamic_offset);
  8266. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8267. ctx->dynamic_offset++;
  8268. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8269. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8270. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8271. goto fail;
  8272. }
  8273. }
  8274. }
  8275. if (is_32bit_type(type))
  8276. return true;
  8277. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8278. for (i = 0; i < cell_num_to_push; i++) {
  8279. if (ctx->p_code_compiled == NULL) {
  8280. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8281. return false;
  8282. }
  8283. ctx->frame_offset++;
  8284. if (!disable_emit) {
  8285. ctx->dynamic_offset++;
  8286. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8287. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8288. if (ctx->max_dynamic_offset >= INT16_MAX)
  8289. goto fail;
  8290. }
  8291. }
  8292. }
  8293. return true;
  8294. fail:
  8295. set_error_buf(error_buf, error_buf_size,
  8296. "fast interpreter offset overflow");
  8297. return false;
  8298. }
  8299. /* This function should be in front of wasm_loader_pop_frame_ref
  8300. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8301. will be modified by wasm_loader_pop_frame_ref */
  8302. static bool
  8303. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8304. char *error_buf, uint32 error_buf_size)
  8305. {
  8306. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8307. then current block is the function block */
  8308. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8309. BranchBlock *cur_block = ctx->frame_csp - depth;
  8310. int32 available_stack_cell =
  8311. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8312. uint32 cell_num_to_pop;
  8313. /* Directly return success if current block is in stack
  8314. polymorphic state while stack is empty. */
  8315. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8316. return true;
  8317. if (type == VALUE_TYPE_VOID)
  8318. return true;
  8319. /* Change type to ANY when the stack top is ANY, so as to avoid
  8320. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8321. two offsets */
  8322. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8323. type = VALUE_TYPE_ANY;
  8324. cell_num_to_pop = wasm_value_type_cell_num(type);
  8325. /* Check the offset stack bottom to ensure the frame offset
  8326. stack will not go underflow. But we don't thrown error
  8327. and return true here, because the error msg should be
  8328. given in wasm_loader_pop_frame_ref */
  8329. if (!check_offset_pop(ctx, cell_num_to_pop))
  8330. return true;
  8331. ctx->frame_offset -= cell_num_to_pop;
  8332. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8333. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8334. ctx->dynamic_offset -= cell_num_to_pop;
  8335. emit_operand(ctx, *(ctx->frame_offset));
  8336. (void)error_buf;
  8337. (void)error_buf_size;
  8338. return true;
  8339. }
  8340. static bool
  8341. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8342. bool disable_emit, int16 operand_offset,
  8343. char *error_buf, uint32 error_buf_size)
  8344. {
  8345. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8346. error_buf, error_buf_size)))
  8347. return false;
  8348. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8349. return false;
  8350. return true;
  8351. }
  8352. static bool
  8353. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8354. char *error_buf, uint32 error_buf_size)
  8355. {
  8356. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8357. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8358. return false;
  8359. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8360. return false;
  8361. return true;
  8362. }
  8363. static bool
  8364. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8365. uint8 type_push, uint8 type_pop,
  8366. bool disable_emit, int16 operand_offset,
  8367. char *error_buf, uint32 error_buf_size)
  8368. {
  8369. uint8 i;
  8370. for (i = 0; i < pop_cnt; i++) {
  8371. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8372. error_buf_size))
  8373. return false;
  8374. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8375. error_buf_size))
  8376. return false;
  8377. }
  8378. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8379. operand_offset, error_buf,
  8380. error_buf_size))
  8381. return false;
  8382. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8383. return false;
  8384. return true;
  8385. }
  8386. static bool
  8387. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8388. int16 *offset, char *error_buf,
  8389. uint32 error_buf_size)
  8390. {
  8391. int8 bytes_to_increase;
  8392. int16 operand_offset = 0;
  8393. Const *c;
  8394. /* Search existing constant */
  8395. for (c = (Const *)ctx->const_buf;
  8396. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8397. /* TODO: handle v128 type? */
  8398. if ((type == c->value_type)
  8399. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8400. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8401. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8402. || (type == VALUE_TYPE_FUNCREF
  8403. && *(int32 *)value == c->value.i32)
  8404. || (type == VALUE_TYPE_EXTERNREF
  8405. && *(int32 *)value == c->value.i32)
  8406. #endif
  8407. || (type == VALUE_TYPE_F64
  8408. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8409. || (type == VALUE_TYPE_F32
  8410. && (0
  8411. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8412. operand_offset = c->slot_index;
  8413. break;
  8414. }
  8415. if (is_32bit_type(c->value_type))
  8416. operand_offset += 1;
  8417. else
  8418. operand_offset += 2;
  8419. }
  8420. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8421. /* New constant, append to the const buffer */
  8422. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8423. bytes_to_increase = 2;
  8424. }
  8425. else {
  8426. bytes_to_increase = 1;
  8427. }
  8428. /* The max cell num of const buffer is 32768 since the valid index range
  8429. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8430. * full */
  8431. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8432. *offset = 0;
  8433. return true;
  8434. }
  8435. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8436. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8437. ctx->const_buf_size + 4 * sizeof(Const));
  8438. ctx->const_buf_size += 4 * sizeof(Const);
  8439. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8440. }
  8441. c->value_type = type;
  8442. switch (type) {
  8443. case VALUE_TYPE_F64:
  8444. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8445. sizeof(float64));
  8446. ctx->const_cell_num += 2;
  8447. /* The const buf will be reversed, we use the second cell */
  8448. /* of the i64/f64 const so the final offset is correct */
  8449. operand_offset++;
  8450. break;
  8451. case VALUE_TYPE_I64:
  8452. c->value.i64 = *(int64 *)value;
  8453. ctx->const_cell_num += 2;
  8454. operand_offset++;
  8455. break;
  8456. case VALUE_TYPE_F32:
  8457. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8458. sizeof(float32));
  8459. ctx->const_cell_num++;
  8460. break;
  8461. case VALUE_TYPE_I32:
  8462. c->value.i32 = *(int32 *)value;
  8463. ctx->const_cell_num++;
  8464. break;
  8465. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8466. case VALUE_TYPE_EXTERNREF:
  8467. case VALUE_TYPE_FUNCREF:
  8468. c->value.i32 = *(int32 *)value;
  8469. ctx->const_cell_num++;
  8470. break;
  8471. #endif
  8472. default:
  8473. break;
  8474. }
  8475. c->slot_index = operand_offset;
  8476. ctx->num_const++;
  8477. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8478. (int64)c->value.i64);
  8479. }
  8480. /* use negative index for const */
  8481. operand_offset = -(operand_offset + 1);
  8482. *offset = operand_offset;
  8483. return true;
  8484. fail:
  8485. return false;
  8486. }
  8487. /*
  8488. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8489. -- Mostly used for the binary / compare operation
  8490. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8491. -- Mostly used in block / control instructions
  8492. The POP will always emit the offset on the top of the frame_offset stack
  8493. PUSH can be used in two ways:
  8494. 1. directly PUSH:
  8495. PUSH_XXX();
  8496. will allocate a dynamic space and emit
  8497. 2. silent PUSH:
  8498. operand_offset = xxx; disable_emit = true;
  8499. PUSH_XXX();
  8500. only push the frame_offset stack, no emit
  8501. */
  8502. #define TEMPLATE_PUSH(Type) \
  8503. do { \
  8504. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8505. disable_emit, operand_offset, \
  8506. error_buf, error_buf_size)) \
  8507. goto fail; \
  8508. } while (0)
  8509. #define TEMPLATE_PUSH_REF(Type) \
  8510. do { \
  8511. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8512. operand_offset, error_buf, \
  8513. error_buf_size)) \
  8514. goto fail; \
  8515. } while (0)
  8516. #define TEMPLATE_POP(Type) \
  8517. do { \
  8518. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8519. error_buf, error_buf_size)) \
  8520. goto fail; \
  8521. } while (0)
  8522. #define TEMPLATE_POP_REF(Type) \
  8523. do { \
  8524. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8525. error_buf_size)) \
  8526. goto fail; \
  8527. } while (0)
  8528. #define PUSH_OFFSET_TYPE(type) \
  8529. do { \
  8530. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8531. operand_offset, error_buf, \
  8532. error_buf_size))) \
  8533. goto fail; \
  8534. } while (0)
  8535. #define POP_OFFSET_TYPE(type) \
  8536. do { \
  8537. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8538. error_buf_size))) \
  8539. goto fail; \
  8540. } while (0)
  8541. #define POP_AND_PUSH(type_pop, type_push) \
  8542. do { \
  8543. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8544. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8545. operand_offset, error_buf, error_buf_size))) \
  8546. goto fail; \
  8547. } while (0)
  8548. /* type of POPs should be the same */
  8549. #define POP2_AND_PUSH(type_pop, type_push) \
  8550. do { \
  8551. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8552. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8553. operand_offset, error_buf, error_buf_size))) \
  8554. goto fail; \
  8555. } while (0)
  8556. #else /* WASM_ENABLE_FAST_INTERP */
  8557. #define TEMPLATE_PUSH(Type) \
  8558. do { \
  8559. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8560. error_buf, error_buf_size))) \
  8561. goto fail; \
  8562. } while (0)
  8563. #define TEMPLATE_PUSH_REF(Type) \
  8564. do { \
  8565. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8566. error_buf_size))) \
  8567. goto fail; \
  8568. } while (0)
  8569. #define TEMPLATE_POP(Type) \
  8570. do { \
  8571. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8572. error_buf, error_buf_size))) \
  8573. goto fail; \
  8574. } while (0)
  8575. #define TEMPLATE_POP_REF(Type) \
  8576. do { \
  8577. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8578. error_buf_size))) \
  8579. goto fail; \
  8580. } while (0)
  8581. #define POP_AND_PUSH(type_pop, type_push) \
  8582. do { \
  8583. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8584. type_pop, error_buf, \
  8585. error_buf_size))) \
  8586. goto fail; \
  8587. } while (0)
  8588. /* type of POPs should be the same */
  8589. #define POP2_AND_PUSH(type_pop, type_push) \
  8590. do { \
  8591. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8592. type_pop, error_buf, \
  8593. error_buf_size))) \
  8594. goto fail; \
  8595. } while (0)
  8596. #endif /* WASM_ENABLE_FAST_INTERP */
  8597. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8598. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8599. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8600. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8601. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8602. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8603. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8604. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8605. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8606. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8607. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8608. #define POP_I32() TEMPLATE_POP(I32)
  8609. #define POP_F32() TEMPLATE_POP(F32)
  8610. #define POP_I64() TEMPLATE_POP(I64)
  8611. #define POP_F64() TEMPLATE_POP(F64)
  8612. #define POP_V128() TEMPLATE_POP(V128)
  8613. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8614. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8615. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8616. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8617. #if WASM_ENABLE_FAST_INTERP != 0
  8618. static bool
  8619. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8620. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8621. {
  8622. int16 operand_offset = 0;
  8623. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8624. : loader_ctx->frame_csp;
  8625. BlockType *block_type = &block->block_type;
  8626. uint8 *return_types = NULL;
  8627. #if WASM_ENABLE_GC != 0
  8628. WASMRefTypeMap *reftype_maps = NULL;
  8629. uint32 reftype_map_count;
  8630. #endif
  8631. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8632. int32 i = 0;
  8633. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8634. *frame_offset_org = NULL;
  8635. #if WASM_ENABLE_GC == 0
  8636. return_count = block_type_get_result_types(block_type, &return_types);
  8637. #else
  8638. return_count = block_type_get_result_types(
  8639. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8640. #endif
  8641. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8642. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8643. if (return_count == 1) {
  8644. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8645. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8646. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8647. /* insert op_copy before else opcode */
  8648. if (opcode == WASM_OP_ELSE)
  8649. skip_label();
  8650. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8651. : EXT_OP_COPY_STACK_TOP_I64);
  8652. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8653. emit_operand(loader_ctx, block->dynamic_offset);
  8654. if (opcode == WASM_OP_ELSE) {
  8655. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8656. }
  8657. else {
  8658. loader_ctx->frame_offset -= cell;
  8659. loader_ctx->dynamic_offset = block->dynamic_offset;
  8660. PUSH_OFFSET_TYPE(return_types[0]);
  8661. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8662. }
  8663. if (opcode == WASM_OP_ELSE)
  8664. emit_label(opcode);
  8665. }
  8666. return true;
  8667. }
  8668. /* Copy stack top values to block's results which are in dynamic space.
  8669. * The instruction format:
  8670. * Part a: values count
  8671. * Part b: all values total cell num
  8672. * Part c: each value's cell_num, src offset and dst offset
  8673. * Part d: each value's src offset and dst offset
  8674. * Part e: each value's dst offset
  8675. */
  8676. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8677. dynamic_offset = dynamic_offset_org =
  8678. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8679. /* First traversal to get the count of values needed to be copied. */
  8680. for (i = (int32)return_count - 1; i >= 0; i--) {
  8681. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8682. frame_offset -= cells;
  8683. dynamic_offset -= cells;
  8684. if (dynamic_offset != *frame_offset) {
  8685. value_count++;
  8686. total_cel_num += cells;
  8687. }
  8688. }
  8689. if (value_count) {
  8690. uint32 j = 0;
  8691. uint8 *emit_data = NULL, *cells = NULL;
  8692. int16 *src_offsets = NULL;
  8693. uint16 *dst_offsets = NULL;
  8694. uint64 size =
  8695. (uint64)value_count
  8696. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8697. /* Allocate memory for the emit data */
  8698. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8699. return false;
  8700. cells = emit_data;
  8701. src_offsets = (int16 *)(cells + value_count);
  8702. dst_offsets = (uint16 *)(src_offsets + value_count);
  8703. /* insert op_copy before else opcode */
  8704. if (opcode == WASM_OP_ELSE)
  8705. skip_label();
  8706. emit_label(EXT_OP_COPY_STACK_VALUES);
  8707. /* Part a) */
  8708. emit_uint32(loader_ctx, value_count);
  8709. /* Part b) */
  8710. emit_uint32(loader_ctx, total_cel_num);
  8711. /* Second traversal to get each value's cell num, src offset and dst
  8712. * offset. */
  8713. frame_offset = frame_offset_org;
  8714. dynamic_offset = dynamic_offset_org;
  8715. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8716. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8717. frame_offset -= cell;
  8718. dynamic_offset -= cell;
  8719. if (dynamic_offset != *frame_offset) {
  8720. /* cell num */
  8721. cells[j] = cell;
  8722. /* src offset */
  8723. src_offsets[j] = *frame_offset;
  8724. /* dst offset */
  8725. dst_offsets[j] = dynamic_offset;
  8726. j++;
  8727. }
  8728. if (opcode == WASM_OP_ELSE) {
  8729. *frame_offset = dynamic_offset;
  8730. }
  8731. else {
  8732. loader_ctx->frame_offset = frame_offset;
  8733. loader_ctx->dynamic_offset = dynamic_offset;
  8734. PUSH_OFFSET_TYPE(return_types[i]);
  8735. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8736. loader_ctx->frame_offset = frame_offset_org;
  8737. loader_ctx->dynamic_offset = dynamic_offset_org;
  8738. }
  8739. }
  8740. bh_assert(j == value_count);
  8741. /* Emit the cells, src_offsets and dst_offsets */
  8742. for (j = 0; j < value_count; j++)
  8743. emit_byte(loader_ctx, cells[j]);
  8744. for (j = 0; j < value_count; j++)
  8745. emit_operand(loader_ctx, src_offsets[j]);
  8746. for (j = 0; j < value_count; j++)
  8747. emit_operand(loader_ctx, dst_offsets[j]);
  8748. if (opcode == WASM_OP_ELSE)
  8749. emit_label(opcode);
  8750. wasm_runtime_free(emit_data);
  8751. }
  8752. return true;
  8753. fail:
  8754. return false;
  8755. }
  8756. #endif /* WASM_ENABLE_FAST_INTERP */
  8757. #define RESERVE_BLOCK_RET() \
  8758. do { \
  8759. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  8760. error_buf_size)) \
  8761. goto fail; \
  8762. } while (0)
  8763. #define PUSH_TYPE(type) \
  8764. do { \
  8765. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8766. error_buf_size))) \
  8767. goto fail; \
  8768. } while (0)
  8769. #define POP_TYPE(type) \
  8770. do { \
  8771. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8772. error_buf_size))) \
  8773. goto fail; \
  8774. } while (0)
  8775. #if WASM_ENABLE_GC == 0
  8776. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8777. do { \
  8778. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8779. _start_addr, error_buf, \
  8780. error_buf_size)) \
  8781. goto fail; \
  8782. } while (0)
  8783. #define POP_CSP() \
  8784. do { \
  8785. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8786. goto fail; \
  8787. } while (0)
  8788. #else
  8789. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8790. do { \
  8791. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8792. _start_addr, error_buf, \
  8793. error_buf_size)) \
  8794. goto fail; \
  8795. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8796. error_buf, error_buf_size)) { \
  8797. goto fail; \
  8798. } \
  8799. } while (0)
  8800. #define POP_CSP() \
  8801. do { \
  8802. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8803. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8804. goto fail; \
  8805. } while (0)
  8806. #endif /* end of WASM_ENABLE_GC == 0 */
  8807. #if WASM_ENABLE_GC == 0
  8808. #define GET_LOCAL_REFTYPE() (void)0
  8809. #else
  8810. #define GET_LOCAL_REFTYPE() \
  8811. do { \
  8812. if (wasm_is_type_multi_byte_type(local_type)) { \
  8813. WASMRefType *_ref_type; \
  8814. if (local_idx < param_count) \
  8815. _ref_type = wasm_reftype_map_find( \
  8816. param_reftype_maps, param_reftype_map_count, local_idx); \
  8817. else \
  8818. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8819. local_reftype_map_count, \
  8820. local_idx - param_count); \
  8821. bh_assert(_ref_type); \
  8822. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8823. wasm_reftype_struct_size(_ref_type)); \
  8824. } \
  8825. } while (0)
  8826. #endif /* end of WASM_ENABLE_GC == 0 */
  8827. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8828. do { \
  8829. read_leb_uint32(p, p_end, local_idx); \
  8830. if (local_idx >= param_count + local_count) { \
  8831. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8832. goto fail; \
  8833. } \
  8834. local_type = local_idx < param_count \
  8835. ? param_types[local_idx] \
  8836. : local_types[local_idx - param_count]; \
  8837. local_offset = local_offsets[local_idx]; \
  8838. GET_LOCAL_REFTYPE(); \
  8839. } while (0)
  8840. static bool
  8841. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8842. {
  8843. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8844. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8845. return false;
  8846. }
  8847. return true;
  8848. }
  8849. #define CHECK_MEMORY() \
  8850. do { \
  8851. if (!check_memory(module, error_buf, error_buf_size)) \
  8852. goto fail; \
  8853. } while (0)
  8854. static bool
  8855. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8856. uint32 error_buf_size)
  8857. {
  8858. uint8 mem_access_aligns[] = {
  8859. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8860. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8861. };
  8862. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8863. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8864. set_error_buf(error_buf, error_buf_size,
  8865. "alignment must not be larger than natural");
  8866. return false;
  8867. }
  8868. return true;
  8869. }
  8870. #if WASM_ENABLE_SIMD != 0
  8871. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8872. static bool
  8873. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8874. uint32 error_buf_size)
  8875. {
  8876. uint8 mem_access_aligns[] = {
  8877. 4, /* load */
  8878. 3, 3, 3, 3, 3, 3, /* load and extend */
  8879. 0, 1, 2, 3, /* load and splat */
  8880. 4, /* store */
  8881. };
  8882. uint8 mem_access_aligns_load_lane[] = {
  8883. 0, 1, 2, 3, /* load lane */
  8884. 0, 1, 2, 3, /* store lane */
  8885. 2, 3 /* store zero */
  8886. };
  8887. if (!((opcode <= SIMD_v128_store)
  8888. || (SIMD_v128_load8_lane <= opcode
  8889. && opcode <= SIMD_v128_load64_zero))) {
  8890. set_error_buf(error_buf, error_buf_size,
  8891. "the opcode doesn't include memarg");
  8892. return false;
  8893. }
  8894. if ((opcode <= SIMD_v128_store
  8895. && align > mem_access_aligns[opcode - SIMD_v128_load])
  8896. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  8897. && align > mem_access_aligns_load_lane[opcode
  8898. - SIMD_v128_load8_lane])) {
  8899. set_error_buf(error_buf, error_buf_size,
  8900. "alignment must not be larger than natural");
  8901. return false;
  8902. }
  8903. return true;
  8904. }
  8905. static bool
  8906. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  8907. uint32 error_buf_size)
  8908. {
  8909. switch (opcode) {
  8910. case SIMD_i8x16_extract_lane_s:
  8911. case SIMD_i8x16_extract_lane_u:
  8912. case SIMD_i8x16_replace_lane:
  8913. if (lane >= 16) {
  8914. goto fail;
  8915. }
  8916. break;
  8917. case SIMD_i16x8_extract_lane_s:
  8918. case SIMD_i16x8_extract_lane_u:
  8919. case SIMD_i16x8_replace_lane:
  8920. if (lane >= 8) {
  8921. goto fail;
  8922. }
  8923. break;
  8924. case SIMD_i32x4_extract_lane:
  8925. case SIMD_i32x4_replace_lane:
  8926. case SIMD_f32x4_extract_lane:
  8927. case SIMD_f32x4_replace_lane:
  8928. if (lane >= 4) {
  8929. goto fail;
  8930. }
  8931. break;
  8932. case SIMD_i64x2_extract_lane:
  8933. case SIMD_i64x2_replace_lane:
  8934. case SIMD_f64x2_extract_lane:
  8935. case SIMD_f64x2_replace_lane:
  8936. if (lane >= 2) {
  8937. goto fail;
  8938. }
  8939. break;
  8940. case SIMD_v128_load8_lane:
  8941. case SIMD_v128_load16_lane:
  8942. case SIMD_v128_load32_lane:
  8943. case SIMD_v128_load64_lane:
  8944. case SIMD_v128_store8_lane:
  8945. case SIMD_v128_store16_lane:
  8946. case SIMD_v128_store32_lane:
  8947. case SIMD_v128_store64_lane:
  8948. case SIMD_v128_load32_zero:
  8949. case SIMD_v128_load64_zero:
  8950. {
  8951. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  8952. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  8953. goto fail;
  8954. }
  8955. break;
  8956. }
  8957. default:
  8958. goto fail;
  8959. }
  8960. return true;
  8961. fail:
  8962. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8963. return false;
  8964. }
  8965. static bool
  8966. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  8967. {
  8968. uint8 i;
  8969. for (i = 0; i != 16; ++i) {
  8970. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  8971. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8972. return false;
  8973. }
  8974. }
  8975. return true;
  8976. }
  8977. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  8978. #endif /* end of WASM_ENABLE_SIMD */
  8979. #if WASM_ENABLE_SHARED_MEMORY != 0
  8980. static bool
  8981. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  8982. uint32 error_buf_size)
  8983. {
  8984. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  8985. uint8 mem_access_aligns[] = {
  8986. 2, 3, 0, 1, 0, 1, 2,
  8987. };
  8988. uint8 expect;
  8989. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  8990. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  8991. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  8992. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  8993. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  8994. }
  8995. else {
  8996. /* 7 opcodes in every group */
  8997. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  8998. }
  8999. if (align != expect) {
  9000. set_error_buf(error_buf, error_buf_size,
  9001. "alignment isn't equal to natural");
  9002. return false;
  9003. }
  9004. return true;
  9005. }
  9006. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9007. static bool
  9008. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9009. char *error_buf, uint32 error_buf_size)
  9010. {
  9011. BranchBlock *target_block, *cur_block;
  9012. BlockType *target_block_type;
  9013. uint8 type, *types = NULL, *frame_ref;
  9014. uint32 arity = 0;
  9015. int32 i, available_stack_cell;
  9016. uint16 cell_num;
  9017. #if WASM_ENABLE_GC != 0
  9018. WASMRefTypeMap *frame_reftype_map;
  9019. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9020. WASMRefType *ref_type;
  9021. uint32 reftype_map_count = 0;
  9022. int32 available_reftype_map;
  9023. bool is_type_multi_byte;
  9024. #endif
  9025. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9026. uint8 *frame_ref_after_popped = NULL;
  9027. uint8 frame_ref_tmp[4] = { 0 };
  9028. uint8 *frame_ref_buf = frame_ref_tmp;
  9029. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9030. #if WASM_ENABLE_GC != 0
  9031. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9032. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9033. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9034. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9035. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9036. #endif
  9037. #if WASM_ENABLE_FAST_INTERP != 0
  9038. int16 *frame_offset_old = loader_ctx->frame_offset;
  9039. int16 *frame_offset_after_popped = NULL;
  9040. int16 frame_offset_tmp[4] = { 0 };
  9041. int16 *frame_offset_buf = frame_offset_tmp;
  9042. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9043. #endif
  9044. bool ret = false;
  9045. bh_assert(loader_ctx->csp_num > 0);
  9046. if (loader_ctx->csp_num - 1 < depth) {
  9047. set_error_buf(error_buf, error_buf_size,
  9048. "unknown label, "
  9049. "unexpected end of section or function");
  9050. return false;
  9051. }
  9052. cur_block = loader_ctx->frame_csp - 1;
  9053. target_block = loader_ctx->frame_csp - (depth + 1);
  9054. target_block_type = &target_block->block_type;
  9055. frame_ref = loader_ctx->frame_ref;
  9056. #if WASM_ENABLE_GC != 0
  9057. frame_reftype_map = loader_ctx->frame_reftype_map;
  9058. #endif
  9059. /* Note: loop's arity is different from if and block. loop's arity is
  9060. * its parameter count while if and block arity is result count.
  9061. */
  9062. #if WASM_ENABLE_GC == 0
  9063. if (target_block->label_type == LABEL_TYPE_LOOP)
  9064. arity = block_type_get_param_types(target_block_type, &types);
  9065. else
  9066. arity = block_type_get_result_types(target_block_type, &types);
  9067. #else
  9068. if (target_block->label_type == LABEL_TYPE_LOOP)
  9069. arity = block_type_get_param_types(target_block_type, &types,
  9070. &reftype_maps, &reftype_map_count);
  9071. else
  9072. arity = block_type_get_result_types(target_block_type, &types,
  9073. &reftype_maps, &reftype_map_count);
  9074. #endif
  9075. /* If the stack is in polymorphic state, just clear the stack
  9076. * and then re-push the values to make the stack top values
  9077. * match block type. */
  9078. if (cur_block->is_stack_polymorphic) {
  9079. #if WASM_ENABLE_GC != 0
  9080. int32 j = reftype_map_count - 1;
  9081. #endif
  9082. for (i = (int32)arity - 1; i >= 0; i--) {
  9083. #if WASM_ENABLE_GC != 0
  9084. if (wasm_is_type_multi_byte_type(types[i])) {
  9085. bh_assert(reftype_maps[j].index == i);
  9086. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9087. reftype_maps[j].ref_type,
  9088. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9089. j--;
  9090. }
  9091. #endif
  9092. #if WASM_ENABLE_FAST_INTERP != 0
  9093. POP_OFFSET_TYPE(types[i]);
  9094. #endif
  9095. POP_TYPE(types[i]);
  9096. }
  9097. /* Backup stack data since it may be changed in the below
  9098. push operations, and the stack data may be used when
  9099. checking other target blocks of opcode br_table */
  9100. if (opcode == WASM_OP_BR_TABLE) {
  9101. uint64 total_size;
  9102. frame_ref_after_popped = loader_ctx->frame_ref;
  9103. total_size = (uint64)sizeof(uint8)
  9104. * (frame_ref_old - frame_ref_after_popped);
  9105. if (total_size > sizeof(frame_ref_tmp)
  9106. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9107. error_buf_size))) {
  9108. goto fail;
  9109. }
  9110. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9111. frame_ref_after_popped, (uint32)total_size);
  9112. #if WASM_ENABLE_GC != 0
  9113. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9114. total_size =
  9115. (uint64)sizeof(WASMRefTypeMap)
  9116. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9117. if (total_size > sizeof(frame_reftype_map_tmp)
  9118. && !(frame_reftype_map_buf = loader_malloc(
  9119. total_size, error_buf, error_buf_size))) {
  9120. goto fail;
  9121. }
  9122. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9123. frame_reftype_map_after_popped, (uint32)total_size);
  9124. #endif
  9125. #if WASM_ENABLE_FAST_INTERP != 0
  9126. frame_offset_after_popped = loader_ctx->frame_offset;
  9127. total_size = (uint64)sizeof(int16)
  9128. * (frame_offset_old - frame_offset_after_popped);
  9129. if (total_size > sizeof(frame_offset_tmp)
  9130. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9131. error_buf_size))) {
  9132. goto fail;
  9133. }
  9134. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9135. frame_offset_after_popped, (uint32)total_size);
  9136. #endif
  9137. }
  9138. #if WASM_ENABLE_GC != 0
  9139. j = 0;
  9140. #endif
  9141. for (i = 0; i < (int32)arity; i++) {
  9142. #if WASM_ENABLE_GC != 0
  9143. if (wasm_is_type_multi_byte_type(types[i])) {
  9144. bh_assert(reftype_maps[j].index == i);
  9145. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9146. reftype_maps[j].ref_type,
  9147. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9148. j++;
  9149. }
  9150. #endif
  9151. #if WASM_ENABLE_FAST_INTERP != 0
  9152. bool disable_emit = true;
  9153. int16 operand_offset = 0;
  9154. PUSH_OFFSET_TYPE(types[i]);
  9155. #endif
  9156. PUSH_TYPE(types[i]);
  9157. }
  9158. #if WASM_ENABLE_FAST_INTERP != 0
  9159. emit_br_info(target_block, opcode == WASM_OP_BR);
  9160. #endif
  9161. /* Restore the stack data, note that frame_ref_bottom,
  9162. frame_reftype_map_bottom, frame_offset_bottom may be
  9163. re-allocated in the above push operations */
  9164. if (opcode == WASM_OP_BR_TABLE) {
  9165. uint32 total_size;
  9166. /* The stack operand num should not be smaller than before
  9167. after pop and push operations */
  9168. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9169. loader_ctx->stack_cell_num = stack_cell_num_old;
  9170. loader_ctx->frame_ref =
  9171. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9172. total_size = (uint32)(sizeof(uint8)
  9173. * (frame_ref_old - frame_ref_after_popped));
  9174. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9175. frame_ref_buf, total_size);
  9176. #if WASM_ENABLE_GC != 0
  9177. /* The stack operand num should not be smaller than before
  9178. after pop and push operations */
  9179. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9180. loader_ctx->reftype_map_num = reftype_map_num_old;
  9181. loader_ctx->frame_reftype_map =
  9182. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9183. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9184. * (frame_reftype_map_old
  9185. - frame_reftype_map_after_popped));
  9186. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9187. total_size, frame_reftype_map_buf, total_size);
  9188. #endif
  9189. #if WASM_ENABLE_FAST_INTERP != 0
  9190. loader_ctx->frame_offset =
  9191. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9192. total_size =
  9193. (uint32)(sizeof(int16)
  9194. * (frame_offset_old - frame_offset_after_popped));
  9195. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9196. total_size, frame_offset_buf, total_size);
  9197. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9198. #endif
  9199. }
  9200. ret = true;
  9201. goto cleanup_and_return;
  9202. }
  9203. available_stack_cell =
  9204. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9205. #if WASM_ENABLE_GC != 0
  9206. available_reftype_map =
  9207. (int32)(loader_ctx->reftype_map_num
  9208. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9209. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9210. #endif
  9211. /* Check stack top values match target block type */
  9212. for (i = (int32)arity - 1; i >= 0; i--) {
  9213. type = types[i];
  9214. #if WASM_ENABLE_GC != 0
  9215. ref_type = NULL;
  9216. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9217. if (is_type_multi_byte) {
  9218. bh_assert(reftype_map);
  9219. ref_type = reftype_map->ref_type;
  9220. }
  9221. #endif
  9222. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9223. break;
  9224. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9225. #if WASM_ENABLE_GC != 0
  9226. frame_reftype_map, available_reftype_map,
  9227. #endif
  9228. type,
  9229. #if WASM_ENABLE_GC != 0
  9230. ref_type,
  9231. #endif
  9232. error_buf, error_buf_size)) {
  9233. goto fail;
  9234. }
  9235. cell_num = wasm_value_type_cell_num(types[i]);
  9236. frame_ref -= cell_num;
  9237. available_stack_cell -= cell_num;
  9238. #if WASM_ENABLE_GC != 0
  9239. if (is_type_multi_byte) {
  9240. frame_reftype_map--;
  9241. available_reftype_map--;
  9242. reftype_map--;
  9243. }
  9244. #endif
  9245. }
  9246. #if WASM_ENABLE_FAST_INTERP != 0
  9247. emit_br_info(target_block, opcode == WASM_OP_BR);
  9248. #endif
  9249. ret = true;
  9250. cleanup_and_return:
  9251. fail:
  9252. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9253. wasm_runtime_free(frame_ref_buf);
  9254. #if WASM_ENABLE_GC != 0
  9255. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9256. wasm_runtime_free(frame_reftype_map_buf);
  9257. #endif
  9258. #if WASM_ENABLE_FAST_INTERP != 0
  9259. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9260. wasm_runtime_free(frame_offset_buf);
  9261. #endif
  9262. return ret;
  9263. }
  9264. static BranchBlock *
  9265. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9266. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9267. {
  9268. uint8 *p = *p_buf, *p_end = buf_end;
  9269. BranchBlock *frame_csp_tmp;
  9270. uint32 depth;
  9271. read_leb_uint32(p, p_end, depth);
  9272. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9273. error_buf_size)) {
  9274. goto fail;
  9275. }
  9276. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9277. *p_buf = p;
  9278. return frame_csp_tmp;
  9279. fail:
  9280. return NULL;
  9281. }
  9282. #if WASM_ENABLE_EXCE_HANDLING != 0
  9283. static BranchBlock *
  9284. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9285. uint8 *buf_end, char *error_buf,
  9286. uint32 error_buf_size)
  9287. {
  9288. uint8 *p = *p_buf, *p_end = buf_end;
  9289. BranchBlock *frame_csp_tmp;
  9290. uint32 depth;
  9291. read_leb_uint32(p, p_end, depth);
  9292. /*
  9293. * Note: "delegate 0" means the surrounding block, not the
  9294. * try-delegate block itself.
  9295. *
  9296. * Note: the caller hasn't popped the try-delegate frame yet.
  9297. */
  9298. bh_assert(loader_ctx->csp_num > 0);
  9299. if (loader_ctx->csp_num - 1 <= depth) {
  9300. #if WASM_ENABLE_SPEC_TEST == 0
  9301. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9302. #else
  9303. set_error_buf(error_buf, error_buf_size, "unknown label");
  9304. #endif
  9305. goto fail;
  9306. }
  9307. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9308. #if WASM_ENABLE_FAST_INTERP != 0
  9309. emit_br_info(frame_csp_tmp, false);
  9310. #endif
  9311. *p_buf = p;
  9312. return frame_csp_tmp;
  9313. fail:
  9314. return NULL;
  9315. }
  9316. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9317. static bool
  9318. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9319. char *error_buf, uint32 error_buf_size)
  9320. {
  9321. BlockType *block_type = &block->block_type;
  9322. uint8 *return_types = NULL;
  9323. uint32 return_count = 0;
  9324. int32 available_stack_cell, return_cell_num, i;
  9325. uint8 *frame_ref = NULL;
  9326. #if WASM_ENABLE_GC != 0
  9327. WASMRefTypeMap *frame_reftype_map;
  9328. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9329. WASMRefType *ref_type;
  9330. uint32 param_count, return_reftype_map_count = 0;
  9331. int32 available_reftype_map =
  9332. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9333. #endif
  9334. available_stack_cell =
  9335. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9336. #if WASM_ENABLE_GC == 0
  9337. return_count = block_type_get_result_types(block_type, &return_types);
  9338. #else
  9339. return_count = block_type_get_result_types(block_type, &return_types,
  9340. &return_reftype_maps,
  9341. &return_reftype_map_count);
  9342. param_count =
  9343. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9344. (void)param_count;
  9345. #endif
  9346. return_cell_num =
  9347. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9348. /* If the stack is in polymorphic state, just clear the stack
  9349. * and then re-push the values to make the stack top values
  9350. * match block type. */
  9351. if (block->is_stack_polymorphic) {
  9352. #if WASM_ENABLE_GC != 0
  9353. int32 j = return_reftype_map_count - 1;
  9354. #endif
  9355. for (i = (int32)return_count - 1; i >= 0; i--) {
  9356. #if WASM_ENABLE_GC != 0
  9357. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9358. bh_assert(return_reftype_maps[j].index == i + param_count);
  9359. bh_memcpy_s(
  9360. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9361. return_reftype_maps[j].ref_type,
  9362. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9363. j--;
  9364. }
  9365. #endif
  9366. #if WASM_ENABLE_FAST_INTERP != 0
  9367. POP_OFFSET_TYPE(return_types[i]);
  9368. #endif
  9369. POP_TYPE(return_types[i]);
  9370. }
  9371. /* Check stack is empty */
  9372. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9373. set_error_buf(
  9374. error_buf, error_buf_size,
  9375. "type mismatch: stack size does not match block type");
  9376. goto fail;
  9377. }
  9378. #if WASM_ENABLE_GC != 0
  9379. j = 0;
  9380. #endif
  9381. for (i = 0; i < (int32)return_count; i++) {
  9382. #if WASM_ENABLE_GC != 0
  9383. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9384. bh_assert(return_reftype_maps[j].index == i + param_count);
  9385. bh_memcpy_s(
  9386. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9387. return_reftype_maps[j].ref_type,
  9388. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9389. j++;
  9390. }
  9391. #endif
  9392. #if WASM_ENABLE_FAST_INTERP != 0
  9393. bool disable_emit = true;
  9394. int16 operand_offset = 0;
  9395. PUSH_OFFSET_TYPE(return_types[i]);
  9396. #endif
  9397. PUSH_TYPE(return_types[i]);
  9398. }
  9399. return true;
  9400. }
  9401. if (available_stack_cell != return_cell_num) {
  9402. #if WASM_ENABLE_EXCE_HANDLING != 0
  9403. /* testspec: this error message format is expected by try_catch.wast */
  9404. snprintf(
  9405. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9406. block->label_type == LABEL_TYPE_TRY
  9407. || (block->label_type == LABEL_TYPE_CATCH
  9408. && return_cell_num > 0)
  9409. ? "instruction"
  9410. : "block",
  9411. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9412. " but stack has ",
  9413. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9414. : "");
  9415. goto fail;
  9416. #else
  9417. set_error_buf(error_buf, error_buf_size,
  9418. "type mismatch: stack size does not match block type");
  9419. goto fail;
  9420. #endif
  9421. }
  9422. /* Check stack values match return types */
  9423. frame_ref = loader_ctx->frame_ref;
  9424. #if WASM_ENABLE_GC != 0
  9425. frame_reftype_map = loader_ctx->frame_reftype_map;
  9426. return_reftype_map =
  9427. return_reftype_map_count
  9428. ? return_reftype_maps + return_reftype_map_count - 1
  9429. : NULL;
  9430. #endif
  9431. for (i = (int32)return_count - 1; i >= 0; i--) {
  9432. uint8 type = return_types[i];
  9433. #if WASM_ENABLE_GC != 0
  9434. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9435. ref_type = NULL;
  9436. if (is_type_multi_byte) {
  9437. bh_assert(return_reftype_map);
  9438. ref_type = return_reftype_map->ref_type;
  9439. }
  9440. #endif
  9441. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9442. #if WASM_ENABLE_GC != 0
  9443. frame_reftype_map, available_reftype_map,
  9444. #endif
  9445. type,
  9446. #if WASM_ENABLE_GC != 0
  9447. ref_type,
  9448. #endif
  9449. error_buf, error_buf_size))
  9450. return false;
  9451. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9452. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9453. #if WASM_ENABLE_GC != 0
  9454. if (is_type_multi_byte) {
  9455. frame_reftype_map--;
  9456. available_reftype_map--;
  9457. return_reftype_map--;
  9458. }
  9459. #endif
  9460. }
  9461. return true;
  9462. fail:
  9463. return false;
  9464. }
  9465. #if WASM_ENABLE_FAST_INTERP != 0
  9466. /* Copy parameters to dynamic space.
  9467. * 1) POP original parameter out;
  9468. * 2) Push and copy original values to dynamic space.
  9469. * The copy instruction format:
  9470. * Part a: param count
  9471. * Part b: all param total cell num
  9472. * Part c: each param's cell_num, src offset and dst offset
  9473. * Part d: each param's src offset
  9474. * Part e: each param's dst offset
  9475. */
  9476. static bool
  9477. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9478. uint32 error_buf_size)
  9479. {
  9480. bool ret = false;
  9481. int16 *frame_offset = NULL;
  9482. uint8 *cells = NULL, cell;
  9483. int16 *src_offsets = NULL;
  9484. uint8 *emit_data = NULL;
  9485. uint32 i;
  9486. BranchBlock *block = loader_ctx->frame_csp - 1;
  9487. BlockType *block_type = &block->block_type;
  9488. WASMFuncType *wasm_type = block_type->u.type;
  9489. uint32 param_count = block_type->u.type->param_count;
  9490. int16 condition_offset = 0;
  9491. bool disable_emit = false;
  9492. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9493. int16 operand_offset = 0;
  9494. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9495. bh_assert(size > 0);
  9496. /* For if block, we also need copy the condition operand offset. */
  9497. if (is_if_block)
  9498. size += sizeof(*cells) + sizeof(*src_offsets);
  9499. /* Allocate memory for the emit data */
  9500. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9501. return false;
  9502. cells = emit_data;
  9503. src_offsets = (int16 *)(cells + param_count);
  9504. if (is_if_block)
  9505. condition_offset = *loader_ctx->frame_offset;
  9506. /* POP original parameter out */
  9507. for (i = 0; i < param_count; i++) {
  9508. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9509. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9510. }
  9511. frame_offset = loader_ctx->frame_offset;
  9512. /* Get each param's cell num and src offset */
  9513. for (i = 0; i < param_count; i++) {
  9514. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9515. cells[i] = cell;
  9516. src_offsets[i] = *frame_offset;
  9517. frame_offset += cell;
  9518. }
  9519. /* emit copy instruction */
  9520. emit_label(EXT_OP_COPY_STACK_VALUES);
  9521. /* Part a) */
  9522. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9523. /* Part b) */
  9524. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9525. : wasm_type->param_cell_num);
  9526. /* Part c) */
  9527. for (i = 0; i < param_count; i++)
  9528. emit_byte(loader_ctx, cells[i]);
  9529. if (is_if_block)
  9530. emit_byte(loader_ctx, 1);
  9531. /* Part d) */
  9532. for (i = 0; i < param_count; i++)
  9533. emit_operand(loader_ctx, src_offsets[i]);
  9534. if (is_if_block)
  9535. emit_operand(loader_ctx, condition_offset);
  9536. /* Since the start offset to save the block's params and
  9537. * the start offset to save the block's results may be
  9538. * different, we remember the dynamic offset for loop block
  9539. * so that we can use it to copy the stack operands to the
  9540. * loop block's params in wasm_loader_emit_br_info. */
  9541. if (block->label_type == LABEL_TYPE_LOOP)
  9542. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9543. /* Part e) */
  9544. /* Push to dynamic space. The push will emit the dst offset. */
  9545. for (i = 0; i < param_count; i++)
  9546. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9547. if (is_if_block)
  9548. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9549. ret = true;
  9550. fail:
  9551. /* Free the emit data */
  9552. wasm_runtime_free(emit_data);
  9553. return ret;
  9554. }
  9555. #endif
  9556. #if WASM_ENABLE_GC == 0
  9557. #define RESET_REFTYPE_MAP_STACK() (void)0
  9558. #else
  9559. #define RESET_REFTYPE_MAP_STACK() \
  9560. do { \
  9561. loader_ctx->reftype_map_num = \
  9562. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9563. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9564. + loader_ctx->reftype_map_num; \
  9565. } while (0)
  9566. #endif
  9567. /* reset the stack to the state of before entering the last block */
  9568. #if WASM_ENABLE_FAST_INTERP != 0
  9569. #define RESET_STACK() \
  9570. do { \
  9571. loader_ctx->stack_cell_num = \
  9572. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9573. loader_ctx->frame_ref = \
  9574. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9575. loader_ctx->frame_offset = \
  9576. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9577. RESET_REFTYPE_MAP_STACK(); \
  9578. } while (0)
  9579. #else
  9580. #define RESET_STACK() \
  9581. do { \
  9582. loader_ctx->stack_cell_num = \
  9583. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9584. loader_ctx->frame_ref = \
  9585. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9586. RESET_REFTYPE_MAP_STACK(); \
  9587. } while (0)
  9588. #endif
  9589. /* set current block's stack polymorphic state */
  9590. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9591. do { \
  9592. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9593. _cur_block->is_stack_polymorphic = flag; \
  9594. } while (0)
  9595. #define BLOCK_HAS_PARAM(block_type) \
  9596. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9597. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9598. do { \
  9599. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9600. error_buf_size))) { \
  9601. goto fail; \
  9602. } \
  9603. } while (0)
  9604. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9605. static bool
  9606. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9607. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9608. uint32 error_buf_size)
  9609. {
  9610. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9611. return false;
  9612. }
  9613. if (table_idx < module->import_table_count) {
  9614. if (p_elem_type)
  9615. *p_elem_type =
  9616. module->import_tables[table_idx].u.table.table_type.elem_type;
  9617. #if WASM_ENABLE_GC != 0
  9618. if (p_ref_type)
  9619. *((WASMRefType **)p_ref_type) =
  9620. module->import_tables[table_idx]
  9621. .u.table.table_type.elem_ref_type;
  9622. #endif
  9623. }
  9624. else {
  9625. if (p_elem_type)
  9626. *p_elem_type =
  9627. module->tables[module->import_table_count + table_idx]
  9628. .table_type.elem_type;
  9629. #if WASM_ENABLE_GC != 0
  9630. if (p_ref_type)
  9631. *((WASMRefType **)p_ref_type) =
  9632. module->tables[module->import_table_count + table_idx]
  9633. .table_type.elem_ref_type;
  9634. #endif
  9635. }
  9636. return true;
  9637. }
  9638. static bool
  9639. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9640. uint8 *p_elem_type, void **p_elem_ref_type,
  9641. char *error_buf, uint32 error_buf_size)
  9642. {
  9643. if (table_seg_idx >= module->table_seg_count) {
  9644. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9645. table_seg_idx);
  9646. return false;
  9647. }
  9648. if (p_elem_type) {
  9649. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9650. }
  9651. #if WASM_ENABLE_GC != 0
  9652. if (p_elem_ref_type)
  9653. *((WASMRefType **)p_elem_ref_type) =
  9654. module->table_segments[table_seg_idx].elem_ref_type;
  9655. #endif
  9656. return true;
  9657. }
  9658. #endif
  9659. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9660. const uint8 *
  9661. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9662. uint32 *len)
  9663. {
  9664. WASMCustomSection *section = module->custom_section_list;
  9665. while (section) {
  9666. if ((section->name_len == strlen(name))
  9667. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9668. if (len) {
  9669. *len = section->content_len;
  9670. }
  9671. return section->content_addr;
  9672. }
  9673. section = section->next;
  9674. }
  9675. return NULL;
  9676. }
  9677. #endif
  9678. #if 0
  9679. #define HANDLE_OPCODE(opcode) #opcode
  9680. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  9681. #undef HANDLE_OPCODE
  9682. #endif
  9683. static bool
  9684. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9685. uint32 cur_func_idx, char *error_buf,
  9686. uint32 error_buf_size)
  9687. {
  9688. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9689. uint32 param_count, local_count, global_count;
  9690. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type;
  9691. BlockType func_block_type;
  9692. uint16 *local_offsets, local_offset;
  9693. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9694. uint32 table_seg_idx, data_seg_idx, count, align, i;
  9695. mem_offset_t mem_offset;
  9696. int32 i32_const = 0;
  9697. int64 i64_const;
  9698. uint8 opcode;
  9699. bool return_value = false;
  9700. WASMLoaderContext *loader_ctx;
  9701. BranchBlock *frame_csp_tmp;
  9702. #if WASM_ENABLE_GC != 0
  9703. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9704. uint32 param_reftype_map_count, local_reftype_map_count;
  9705. int32 heap_type;
  9706. WASMRefType wasm_ref_type = { 0 };
  9707. bool need_ref_type_map;
  9708. #endif
  9709. #if WASM_ENABLE_FAST_INTERP != 0
  9710. uint8 *func_const_end, *func_const = NULL;
  9711. int16 operand_offset = 0;
  9712. uint8 last_op = 0;
  9713. bool disable_emit, preserve_local = false, if_condition_available = true;
  9714. float32 f32_const;
  9715. float64 f64_const;
  9716. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9717. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9718. #endif
  9719. #if WASM_ENABLE_MEMORY64 != 0
  9720. bool is_memory64 = has_module_memory64(module);
  9721. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  9722. #else
  9723. mem_offset_type = VALUE_TYPE_I32;
  9724. #endif
  9725. uint32 memidx;
  9726. global_count = module->import_global_count + module->global_count;
  9727. param_count = func->func_type->param_count;
  9728. param_types = func->func_type->types;
  9729. func_block_type.is_value_type = false;
  9730. func_block_type.u.type = func->func_type;
  9731. local_count = func->local_count;
  9732. local_types = func->local_types;
  9733. local_offsets = func->local_offsets;
  9734. #if WASM_ENABLE_GC != 0
  9735. param_reftype_maps = func->func_type->ref_type_maps;
  9736. param_reftype_map_count = func->func_type->ref_type_map_count;
  9737. local_reftype_maps = func->local_ref_type_maps;
  9738. local_reftype_map_count = func->local_ref_type_map_count;
  9739. #endif
  9740. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9741. goto fail;
  9742. }
  9743. #if WASM_ENABLE_GC != 0
  9744. loader_ctx->module = module;
  9745. loader_ctx->ref_type_set = module->ref_type_set;
  9746. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9747. #endif
  9748. #if WASM_ENABLE_FAST_INTERP != 0
  9749. /* For the first traverse, the initial value of preserved_local_offset has
  9750. * not been determined, we use the INT16_MAX to represent that a slot has
  9751. * been copied to preserve space. For second traverse, this field will be
  9752. * set to the appropriate value in wasm_loader_ctx_reinit.
  9753. * This is for Issue #1230,
  9754. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9755. * drop opcodes need to know which slots are preserved, so those slots will
  9756. * not be treated as dynamically allocated slots */
  9757. loader_ctx->preserved_local_offset = INT16_MAX;
  9758. re_scan:
  9759. if (loader_ctx->code_compiled_size > 0) {
  9760. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9761. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9762. goto fail;
  9763. }
  9764. p = func->code;
  9765. func->code_compiled = loader_ctx->p_code_compiled;
  9766. func->code_compiled_size = loader_ctx->code_compiled_size;
  9767. }
  9768. #endif
  9769. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9770. while (p < p_end) {
  9771. opcode = *p++;
  9772. #if WASM_ENABLE_FAST_INTERP != 0
  9773. p_org = p;
  9774. disable_emit = false;
  9775. emit_label(opcode);
  9776. #endif
  9777. switch (opcode) {
  9778. case WASM_OP_UNREACHABLE:
  9779. RESET_STACK();
  9780. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9781. break;
  9782. case WASM_OP_NOP:
  9783. #if WASM_ENABLE_FAST_INTERP != 0
  9784. skip_label();
  9785. #endif
  9786. break;
  9787. case WASM_OP_IF:
  9788. {
  9789. #if WASM_ENABLE_FAST_INTERP != 0
  9790. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9791. int32 available_stack_cell =
  9792. (int32)(loader_ctx->stack_cell_num
  9793. - parent_block->stack_cell_num);
  9794. if (available_stack_cell <= 0
  9795. && parent_block->is_stack_polymorphic)
  9796. if_condition_available = false;
  9797. else
  9798. if_condition_available = true;
  9799. PRESERVE_LOCAL_FOR_BLOCK();
  9800. #endif
  9801. #if WASM_ENABLE_GC == 0
  9802. POP_I32();
  9803. #endif
  9804. goto handle_op_block_and_loop;
  9805. }
  9806. case WASM_OP_BLOCK:
  9807. case WASM_OP_LOOP:
  9808. #if WASM_ENABLE_EXCE_HANDLING != 0
  9809. case WASM_OP_TRY:
  9810. if (opcode == WASM_OP_TRY) {
  9811. /*
  9812. * keep track of exception handlers to account for
  9813. * memory allocation
  9814. */
  9815. func->exception_handler_count++;
  9816. /*
  9817. * try is a block
  9818. * do nothing special, but execution continues to
  9819. * to handle_op_block_and_loop,
  9820. * and that be pushes the csp
  9821. */
  9822. }
  9823. #endif
  9824. #if WASM_ENABLE_FAST_INTERP != 0
  9825. PRESERVE_LOCAL_FOR_BLOCK();
  9826. #endif
  9827. handle_op_block_and_loop:
  9828. {
  9829. uint8 value_type;
  9830. BlockType block_type;
  9831. #if WASM_ENABLE_FAST_INTERP != 0
  9832. uint32 available_params = 0;
  9833. #endif
  9834. CHECK_BUF(p, p_end, 1);
  9835. value_type = read_uint8(p);
  9836. if (is_byte_a_type(value_type)) {
  9837. /* If the first byte is one of these special values:
  9838. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9839. * the single return value. */
  9840. block_type.is_value_type = true;
  9841. block_type.u.value_type.type = value_type;
  9842. #if WASM_ENABLE_WAMR_COMPILER != 0
  9843. if (value_type == VALUE_TYPE_V128)
  9844. module->is_simd_used = true;
  9845. else if (value_type == VALUE_TYPE_FUNCREF
  9846. || value_type == VALUE_TYPE_EXTERNREF)
  9847. module->is_ref_types_used = true;
  9848. #endif
  9849. #if WASM_ENABLE_GC != 0
  9850. if (value_type != VALUE_TYPE_VOID) {
  9851. p_org = p;
  9852. p--;
  9853. if (!resolve_value_type((const uint8 **)&p, p_end,
  9854. module, module->type_count,
  9855. &need_ref_type_map,
  9856. &wasm_ref_type, false,
  9857. error_buf, error_buf_size)) {
  9858. goto fail;
  9859. }
  9860. if (need_ref_type_map) {
  9861. block_type.u.value_type.ref_type_map.index = 0;
  9862. if (!(block_type.u.value_type.ref_type_map
  9863. .ref_type = reftype_set_insert(
  9864. module->ref_type_set, &wasm_ref_type,
  9865. error_buf, error_buf_size))) {
  9866. goto fail;
  9867. }
  9868. }
  9869. /* Set again as the type might be changed, e.g.
  9870. (ref null any) to anyref */
  9871. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9872. #if WASM_ENABLE_FAST_INTERP == 0
  9873. while (p_org < p) {
  9874. #if WASM_ENABLE_DEBUG_INTERP != 0
  9875. if (!record_fast_op(module, p_org, *p_org,
  9876. error_buf, error_buf_size)) {
  9877. goto fail;
  9878. }
  9879. #endif
  9880. /* Ignore extra bytes for interpreter */
  9881. *p_org++ = WASM_OP_NOP;
  9882. }
  9883. #endif
  9884. }
  9885. #endif /* end of WASM_ENABLE_GC != 0 */
  9886. }
  9887. else {
  9888. int32 type_index;
  9889. /* Resolve the leb128 encoded type index as block type */
  9890. p--;
  9891. p_org = p - 1;
  9892. read_leb_int32(p, p_end, type_index);
  9893. if ((uint32)type_index >= module->type_count) {
  9894. set_error_buf(error_buf, error_buf_size,
  9895. "unknown type");
  9896. goto fail;
  9897. }
  9898. block_type.is_value_type = false;
  9899. block_type.u.type =
  9900. (WASMFuncType *)module->types[type_index];
  9901. #if WASM_ENABLE_FAST_INTERP == 0
  9902. /* If block use type index as block type, change the opcode
  9903. * to new extended opcode so that interpreter can resolve
  9904. * the block quickly.
  9905. */
  9906. #if WASM_ENABLE_DEBUG_INTERP != 0
  9907. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9908. error_buf_size)) {
  9909. goto fail;
  9910. }
  9911. #endif
  9912. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  9913. #endif
  9914. }
  9915. #if WASM_ENABLE_GC != 0
  9916. if (opcode == WASM_OP_IF) {
  9917. POP_I32();
  9918. }
  9919. #endif
  9920. /* Pop block parameters from stack */
  9921. if (BLOCK_HAS_PARAM(block_type)) {
  9922. WASMFuncType *wasm_type = block_type.u.type;
  9923. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9924. #if WASM_ENABLE_FAST_INTERP != 0
  9925. uint32 cell_num;
  9926. available_params = block_type.u.type->param_count;
  9927. #endif
  9928. for (i = 0; i < block_type.u.type->param_count; i++) {
  9929. int32 available_stack_cell =
  9930. (int32)(loader_ctx->stack_cell_num
  9931. - cur_block->stack_cell_num);
  9932. if (available_stack_cell <= 0
  9933. && cur_block->is_stack_polymorphic) {
  9934. #if WASM_ENABLE_FAST_INTERP != 0
  9935. available_params = i;
  9936. #endif
  9937. break;
  9938. }
  9939. POP_TYPE(
  9940. wasm_type->types[wasm_type->param_count - i - 1]);
  9941. #if WASM_ENABLE_FAST_INTERP != 0
  9942. /* decrease the frame_offset pointer accordingly to keep
  9943. * consistent with frame_ref stack */
  9944. cell_num = wasm_value_type_cell_num(
  9945. wasm_type->types[wasm_type->param_count - i - 1]);
  9946. loader_ctx->frame_offset -= cell_num;
  9947. #endif
  9948. }
  9949. }
  9950. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  9951. block_type, p);
  9952. /* Pass parameters to block */
  9953. if (BLOCK_HAS_PARAM(block_type)) {
  9954. WASMFuncType *func_type = block_type.u.type;
  9955. #if WASM_ENABLE_GC != 0
  9956. WASMRefType *ref_type;
  9957. uint32 j = 0;
  9958. #endif
  9959. for (i = 0; i < func_type->param_count; i++) {
  9960. #if WASM_ENABLE_FAST_INTERP != 0
  9961. uint32 cell_num =
  9962. wasm_value_type_cell_num(func_type->types[i]);
  9963. if (i >= available_params) {
  9964. /* If there isn't enough data on stack, push a dummy
  9965. * offset to keep the stack consistent with
  9966. * frame_ref.
  9967. * Since the stack is already in polymorphic state,
  9968. * the opcode will not be executed, so the dummy
  9969. * offset won't cause any error */
  9970. uint32 n;
  9971. for (n = 0; n < cell_num; n++) {
  9972. if (loader_ctx->p_code_compiled == NULL) {
  9973. if (!check_offset_push(loader_ctx,
  9974. error_buf,
  9975. error_buf_size))
  9976. goto fail;
  9977. }
  9978. *loader_ctx->frame_offset++ = 0;
  9979. }
  9980. }
  9981. else {
  9982. loader_ctx->frame_offset += cell_num;
  9983. }
  9984. #endif
  9985. #if WASM_ENABLE_GC != 0
  9986. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  9987. bh_assert(func_type->ref_type_maps[j].index == i);
  9988. ref_type = func_type->ref_type_maps[j].ref_type;
  9989. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  9990. ref_type,
  9991. wasm_reftype_struct_size(ref_type));
  9992. j++;
  9993. }
  9994. #endif
  9995. PUSH_TYPE(func_type->types[i]);
  9996. }
  9997. }
  9998. #if WASM_ENABLE_FAST_INTERP != 0
  9999. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10000. skip_label();
  10001. if (BLOCK_HAS_PARAM(block_type)) {
  10002. /* Make sure params are in dynamic space */
  10003. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10004. error_buf_size))
  10005. goto fail;
  10006. }
  10007. if (opcode == WASM_OP_LOOP) {
  10008. (loader_ctx->frame_csp - 1)->code_compiled =
  10009. loader_ctx->p_code_compiled;
  10010. }
  10011. }
  10012. #if WASM_ENABLE_EXCE_HANDLING != 0
  10013. else if (opcode == WASM_OP_TRY) {
  10014. skip_label();
  10015. }
  10016. #endif
  10017. else if (opcode == WASM_OP_IF) {
  10018. BranchBlock *block = loader_ctx->frame_csp - 1;
  10019. /* If block has parameters, we should make sure they are in
  10020. * dynamic space. Otherwise, when else branch is missing,
  10021. * the later opcode may consume incorrect operand offset.
  10022. * Spec case:
  10023. * (func (export "params-id") (param i32) (result i32)
  10024. * (i32.const 1)
  10025. * (i32.const 2)
  10026. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10027. * (then)) (i32.add)
  10028. * )
  10029. *
  10030. * So we should emit a copy instruction before the if.
  10031. *
  10032. * And we also need to save the parameter offsets and
  10033. * recover them before entering else branch.
  10034. *
  10035. */
  10036. if (BLOCK_HAS_PARAM(block_type)) {
  10037. uint64 size;
  10038. /* In polymorphic state, there may be no if condition on
  10039. * the stack, so the offset may not emitted */
  10040. if (if_condition_available) {
  10041. /* skip the if condition operand offset */
  10042. wasm_loader_emit_backspace(loader_ctx,
  10043. sizeof(int16));
  10044. }
  10045. /* skip the if label */
  10046. skip_label();
  10047. /* Emit a copy instruction */
  10048. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10049. error_buf_size))
  10050. goto fail;
  10051. /* Emit the if instruction */
  10052. emit_label(opcode);
  10053. /* Emit the new condition operand offset */
  10054. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10055. /* Save top param_count values of frame_offset stack, so
  10056. * that we can recover it before executing else branch
  10057. */
  10058. size = sizeof(int16)
  10059. * (uint64)block_type.u.type->param_cell_num;
  10060. if (!(block->param_frame_offsets = loader_malloc(
  10061. size, error_buf, error_buf_size)))
  10062. goto fail;
  10063. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10064. loader_ctx->frame_offset
  10065. - size / sizeof(int16),
  10066. (uint32)size);
  10067. }
  10068. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10069. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10070. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10071. }
  10072. #endif
  10073. break;
  10074. }
  10075. #if WASM_ENABLE_EXCE_HANDLING != 0
  10076. case WASM_OP_THROW:
  10077. {
  10078. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10079. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10080. uint8 label_type = cur_block->label_type;
  10081. uint32 tag_index = 0;
  10082. read_leb_int32(p, p_end, tag_index);
  10083. /* check validity of tag_index against module->tag_count */
  10084. /* check tag index is within the tag index space */
  10085. if (tag_index >= module->import_tag_count + module->tag_count) {
  10086. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10087. tag_index);
  10088. goto fail;
  10089. }
  10090. /* the tag_type is stored in either the WASMTag (section tags)
  10091. * or WASMTagImport (import tag) */
  10092. WASMFuncType *tag_type = NULL;
  10093. if (tag_index < module->import_tag_count) {
  10094. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10095. }
  10096. else {
  10097. tag_type =
  10098. module->tags[tag_index - module->import_tag_count]
  10099. ->tag_type;
  10100. }
  10101. if (tag_type->result_count != 0) {
  10102. set_error_buf(error_buf, error_buf_size,
  10103. "tag type signature does not return void");
  10104. goto fail;
  10105. }
  10106. int32 available_stack_cell =
  10107. (int32)(loader_ctx->stack_cell_num
  10108. - cur_block->stack_cell_num);
  10109. int32 tti;
  10110. /* Check stack values match return types by comparing tag param
  10111. * types with stack cells */
  10112. uint8 *frame_ref = loader_ctx->frame_ref;
  10113. #if WASM_ENABLE_GC != 0
  10114. WASMRefTypeMap *frame_reftype_map =
  10115. loader_ctx->frame_reftype_map;
  10116. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10117. /* Temporarily set these values since they may be used in
  10118. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10119. param_reftype_maps = tag_type->ref_type_maps;
  10120. /* For tag_type function, it shouldn't have result_count = 0 */
  10121. param_reftype_map_count = tag_type->ref_type_map_count;
  10122. param_count = tag_type->param_count;
  10123. #endif
  10124. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10125. #if WASM_ENABLE_GC != 0
  10126. local_type = tag_type->types[tti];
  10127. local_idx = tti;
  10128. /* Get the wasm_ref_type if the local_type is multibyte
  10129. type */
  10130. GET_LOCAL_REFTYPE();
  10131. #endif
  10132. if (!check_stack_top_values(
  10133. loader_ctx, frame_ref, available_stack_cell,
  10134. #if WASM_ENABLE_GC != 0
  10135. frame_reftype_map, frame_reftype_map_num,
  10136. #endif
  10137. tag_type->types[tti],
  10138. #if WASM_ENABLE_GC != 0
  10139. &wasm_ref_type,
  10140. #endif
  10141. error_buf, error_buf_size)) {
  10142. snprintf(error_buf, error_buf_size,
  10143. "type mismatch: instruction requires [%s] but "
  10144. "stack has [%s]",
  10145. tag_type->param_count > 0
  10146. ? type2str(tag_type->types[tti])
  10147. : "",
  10148. available_stack_cell > 0
  10149. ? type2str(*(loader_ctx->frame_ref - 1))
  10150. : "");
  10151. goto fail;
  10152. }
  10153. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10154. available_stack_cell -=
  10155. wasm_value_type_cell_num(tag_type->types[tti]);
  10156. }
  10157. #if WASM_ENABLE_GC != 0
  10158. /* Restore the values */
  10159. param_reftype_maps = func->func_type->ref_type_maps;
  10160. param_reftype_map_count = func->func_type->ref_type_map_count;
  10161. param_count = func->func_type->param_count;
  10162. #endif
  10163. /* throw is stack polymorphic */
  10164. (void)label_type;
  10165. RESET_STACK();
  10166. break;
  10167. }
  10168. case WASM_OP_RETHROW:
  10169. {
  10170. /* must be done before checking branch block */
  10171. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10172. /* check the target catching block: LABEL_TYPE_CATCH */
  10173. if (!(frame_csp_tmp =
  10174. check_branch_block(loader_ctx, &p, p_end, opcode,
  10175. error_buf, error_buf_size)))
  10176. goto fail;
  10177. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10178. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10179. /* trap according to spectest (rethrow.wast) */
  10180. set_error_buf(error_buf, error_buf_size,
  10181. "invalid rethrow label");
  10182. goto fail;
  10183. }
  10184. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10185. uint8 label_type = cur_block->label_type;
  10186. (void)label_type;
  10187. /* rethrow is stack polymorphic */
  10188. RESET_STACK();
  10189. break;
  10190. }
  10191. case WASM_OP_DELEGATE:
  10192. {
  10193. /* check target block is valid */
  10194. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10195. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10196. goto fail;
  10197. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10198. uint8 label_type = cur_block->label_type;
  10199. (void)label_type;
  10200. /* DELEGATE ends the block */
  10201. POP_CSP();
  10202. break;
  10203. }
  10204. case WASM_OP_CATCH:
  10205. {
  10206. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10207. uint8 label_type = cur_block->label_type;
  10208. uint32 tag_index = 0;
  10209. read_leb_int32(p, p_end, tag_index);
  10210. /* check validity of tag_index against module->tag_count */
  10211. /* check tag index is within the tag index space */
  10212. if (tag_index >= module->import_tag_count + module->tag_count) {
  10213. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10214. __FUNCTION__);
  10215. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10216. goto fail;
  10217. }
  10218. /* the tag_type is stored in either the WASMTag (section tags)
  10219. * or WASMTagImport (import tag) */
  10220. WASMFuncType *func_type = NULL;
  10221. if (tag_index < module->import_tag_count) {
  10222. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10223. }
  10224. else {
  10225. func_type =
  10226. module->tags[tag_index - module->import_tag_count]
  10227. ->tag_type;
  10228. }
  10229. if (func_type->result_count != 0) {
  10230. set_error_buf(error_buf, error_buf_size,
  10231. "tag type signature does not return void");
  10232. goto fail;
  10233. }
  10234. /* check validity of current label (expect LABEL_TYPE_TRY or
  10235. * LABEL_TYPE_CATCH) */
  10236. if ((LABEL_TYPE_CATCH != label_type)
  10237. && (LABEL_TYPE_TRY != label_type)) {
  10238. set_error_buf(error_buf, error_buf_size,
  10239. "Unexpected block sequence encountered.");
  10240. goto fail;
  10241. }
  10242. /*
  10243. * replace frame_csp by LABEL_TYPE_CATCH
  10244. */
  10245. cur_block->label_type = LABEL_TYPE_CATCH;
  10246. /* RESET_STACK removes the values pushed in TRY or pervious
  10247. * CATCH Blocks */
  10248. RESET_STACK();
  10249. #if WASM_ENABLE_GC != 0
  10250. WASMRefType *ref_type;
  10251. uint32 j = 0;
  10252. #endif
  10253. /* push types on the stack according to caught type */
  10254. for (i = 0; i < func_type->param_count; i++) {
  10255. #if WASM_ENABLE_GC != 0
  10256. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10257. bh_assert(func_type->ref_type_maps[j].index == i);
  10258. ref_type = func_type->ref_type_maps[j].ref_type;
  10259. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10260. ref_type,
  10261. wasm_reftype_struct_size(ref_type));
  10262. j++;
  10263. }
  10264. #endif
  10265. PUSH_TYPE(func_type->types[i]);
  10266. }
  10267. break;
  10268. }
  10269. case WASM_OP_CATCH_ALL:
  10270. {
  10271. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10272. /* expecting a TRY or CATCH, anything else will be considered an
  10273. * error */
  10274. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10275. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10276. set_error_buf(error_buf, error_buf_size,
  10277. "Unexpected block sequence encountered.");
  10278. goto fail;
  10279. }
  10280. /* no immediates */
  10281. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10282. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10283. /* RESET_STACK removes the values pushed in TRY or pervious
  10284. * CATCH Blocks */
  10285. RESET_STACK();
  10286. /* catch_all has no tagtype and therefore no parameters */
  10287. break;
  10288. }
  10289. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10290. case WASM_OP_ELSE:
  10291. handle_op_else:
  10292. {
  10293. BranchBlock *block = NULL;
  10294. BlockType block_type;
  10295. if (loader_ctx->csp_num < 2
  10296. /* the matched if isn't found */
  10297. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10298. /* duplicated else is found */
  10299. || (loader_ctx->frame_csp - 1)->else_addr) {
  10300. set_error_buf(
  10301. error_buf, error_buf_size,
  10302. "opcode else found without matched opcode if");
  10303. goto fail;
  10304. }
  10305. block = loader_ctx->frame_csp - 1;
  10306. /* check whether if branch's stack matches its result type */
  10307. if (!check_block_stack(loader_ctx, block, error_buf,
  10308. error_buf_size))
  10309. goto fail;
  10310. block->else_addr = p - 1;
  10311. block_type = block->block_type;
  10312. #if WASM_ENABLE_GC != 0
  10313. if (!wasm_loader_init_local_use_masks(
  10314. loader_ctx, local_count, error_buf, error_buf_size)) {
  10315. goto fail;
  10316. }
  10317. #endif
  10318. #if WASM_ENABLE_FAST_INTERP != 0
  10319. /* if the result of if branch is in local or const area, add a
  10320. * copy op */
  10321. RESERVE_BLOCK_RET();
  10322. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10323. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10324. #endif
  10325. RESET_STACK();
  10326. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10327. /* Pass parameters to if-false branch */
  10328. if (BLOCK_HAS_PARAM(block_type)) {
  10329. for (i = 0; i < block_type.u.type->param_count; i++)
  10330. PUSH_TYPE(block_type.u.type->types[i]);
  10331. }
  10332. #if WASM_ENABLE_FAST_INTERP != 0
  10333. /* Recover top param_count values of frame_offset stack */
  10334. if (BLOCK_HAS_PARAM((block_type))) {
  10335. uint32 size;
  10336. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10337. bh_memcpy_s(loader_ctx->frame_offset, size,
  10338. block->param_frame_offsets, size);
  10339. loader_ctx->frame_offset += (size / sizeof(int16));
  10340. }
  10341. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10342. #endif
  10343. break;
  10344. }
  10345. case WASM_OP_END:
  10346. {
  10347. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10348. /* check whether block stack matches its result type */
  10349. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10350. error_buf_size))
  10351. goto fail;
  10352. /* if there is no else branch, make a virtual else opcode for
  10353. easier integrity check and to copy the correct results to
  10354. the block return address for fast-interp mode:
  10355. change if block from `if ... end` to `if ... else end` */
  10356. if (cur_block->label_type == LABEL_TYPE_IF
  10357. && !cur_block->else_addr) {
  10358. opcode = WASM_OP_ELSE;
  10359. p--;
  10360. #if WASM_ENABLE_FAST_INTERP != 0
  10361. p_org = p;
  10362. skip_label();
  10363. disable_emit = false;
  10364. emit_label(opcode);
  10365. #endif
  10366. goto handle_op_else;
  10367. }
  10368. POP_CSP();
  10369. #if WASM_ENABLE_FAST_INTERP != 0
  10370. skip_label();
  10371. /* copy the result to the block return address */
  10372. RESERVE_BLOCK_RET();
  10373. apply_label_patch(loader_ctx, 0, PATCH_END);
  10374. free_label_patch_list(loader_ctx->frame_csp);
  10375. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10376. int32 idx;
  10377. uint8 ret_type;
  10378. emit_label(WASM_OP_RETURN);
  10379. for (idx = (int32)func->func_type->result_count - 1;
  10380. idx >= 0; idx--) {
  10381. ret_type = *(func->func_type->types
  10382. + func->func_type->param_count + idx);
  10383. POP_OFFSET_TYPE(ret_type);
  10384. }
  10385. }
  10386. #endif
  10387. if (loader_ctx->csp_num > 0) {
  10388. loader_ctx->frame_csp->end_addr = p - 1;
  10389. }
  10390. else {
  10391. /* end of function block, function will return */
  10392. if (p < p_end) {
  10393. set_error_buf(error_buf, error_buf_size,
  10394. "section size mismatch");
  10395. goto fail;
  10396. }
  10397. }
  10398. break;
  10399. }
  10400. case WASM_OP_BR:
  10401. {
  10402. if (!(frame_csp_tmp =
  10403. check_branch_block(loader_ctx, &p, p_end, opcode,
  10404. error_buf, error_buf_size)))
  10405. goto fail;
  10406. RESET_STACK();
  10407. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10408. break;
  10409. }
  10410. case WASM_OP_BR_IF:
  10411. {
  10412. POP_I32();
  10413. if (!(frame_csp_tmp =
  10414. check_branch_block(loader_ctx, &p, p_end, opcode,
  10415. error_buf, error_buf_size)))
  10416. goto fail;
  10417. break;
  10418. }
  10419. case WASM_OP_BR_TABLE:
  10420. {
  10421. uint32 depth = 0, default_arity, arity = 0;
  10422. BranchBlock *target_block;
  10423. BlockType *target_block_type;
  10424. #if WASM_ENABLE_FAST_INTERP == 0
  10425. BrTableCache *br_table_cache = NULL;
  10426. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10427. uint32 j;
  10428. #endif
  10429. read_leb_uint32(p, p_end, count);
  10430. #if WASM_ENABLE_FAST_INTERP != 0
  10431. emit_uint32(loader_ctx, count);
  10432. #endif
  10433. POP_I32();
  10434. /* Get each depth and check it */
  10435. p_org = p;
  10436. for (i = 0; i <= count; i++) {
  10437. read_leb_uint32(p, p_end, depth);
  10438. bh_assert(loader_ctx->csp_num > 0);
  10439. if (loader_ctx->csp_num - 1 < depth) {
  10440. set_error_buf(error_buf, error_buf_size,
  10441. "unknown label, "
  10442. "unexpected end of section or function");
  10443. goto fail;
  10444. }
  10445. }
  10446. p = p_org;
  10447. /* Get the default block's arity */
  10448. target_block = loader_ctx->frame_csp - (depth + 1);
  10449. target_block_type = &target_block->block_type;
  10450. default_arity = block_type_get_arity(target_block_type,
  10451. target_block->label_type);
  10452. #if WASM_ENABLE_FAST_INTERP == 0
  10453. p_depth_begin = p_depth = p;
  10454. #endif
  10455. for (i = 0; i <= count; i++) {
  10456. p_org = p;
  10457. read_leb_uint32(p, p_end, depth);
  10458. p = p_org;
  10459. /* Get the target block's arity and check it */
  10460. target_block = loader_ctx->frame_csp - (depth + 1);
  10461. target_block_type = &target_block->block_type;
  10462. arity = block_type_get_arity(target_block_type,
  10463. target_block->label_type);
  10464. if (arity != default_arity) {
  10465. set_error_buf(error_buf, error_buf_size,
  10466. "type mismatch: br_table targets must "
  10467. "all use same result type");
  10468. goto fail;
  10469. }
  10470. if (!(frame_csp_tmp =
  10471. check_branch_block(loader_ctx, &p, p_end, opcode,
  10472. error_buf, error_buf_size))) {
  10473. goto fail;
  10474. }
  10475. #if WASM_ENABLE_FAST_INTERP == 0
  10476. if (br_table_cache) {
  10477. br_table_cache->br_depths[i] = depth;
  10478. }
  10479. else {
  10480. if (depth > 255) {
  10481. /* The depth cannot be stored in one byte,
  10482. create br_table cache to store each depth */
  10483. #if WASM_ENABLE_DEBUG_INTERP != 0
  10484. if (!record_fast_op(module, p_opcode, *p_opcode,
  10485. error_buf, error_buf_size)) {
  10486. goto fail;
  10487. }
  10488. #endif
  10489. if (!(br_table_cache = loader_malloc(
  10490. offsetof(BrTableCache, br_depths)
  10491. + sizeof(uint32)
  10492. * (uint64)(count + 1),
  10493. error_buf, error_buf_size))) {
  10494. goto fail;
  10495. }
  10496. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10497. br_table_cache->br_table_op_addr = p_opcode;
  10498. br_table_cache->br_count = count;
  10499. /* Copy previous depths which are one byte */
  10500. for (j = 0; j < i; j++) {
  10501. br_table_cache->br_depths[j] = p_depth_begin[j];
  10502. }
  10503. br_table_cache->br_depths[i] = depth;
  10504. bh_list_insert(module->br_table_cache_list,
  10505. br_table_cache);
  10506. }
  10507. else {
  10508. /* The depth can be stored in one byte, use the
  10509. byte of the leb to store it */
  10510. *p_depth++ = (uint8)depth;
  10511. }
  10512. }
  10513. #endif
  10514. }
  10515. #if WASM_ENABLE_FAST_INTERP == 0
  10516. /* Set the tailing bytes to nop */
  10517. if (br_table_cache)
  10518. p_depth = p_depth_begin;
  10519. while (p_depth < p)
  10520. *p_depth++ = WASM_OP_NOP;
  10521. #endif
  10522. RESET_STACK();
  10523. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10524. break;
  10525. }
  10526. case WASM_OP_RETURN:
  10527. {
  10528. WASMFuncType *func_type = func->func_type;
  10529. int32 idx;
  10530. uint8 ret_type;
  10531. #if WASM_ENABLE_GC != 0
  10532. uint32 j = func_type->ref_type_map_count - 1;
  10533. #endif
  10534. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  10535. idx--) {
  10536. ret_type =
  10537. *(func_type->types + func_type->param_count + idx);
  10538. #if WASM_ENABLE_GC != 0
  10539. if (wasm_is_type_multi_byte_type(ret_type)) {
  10540. WASMRefType *ref_type =
  10541. func_type->ref_type_maps[j].ref_type;
  10542. bh_assert(func_type->ref_type_maps[j].index
  10543. == func_type->param_count + idx);
  10544. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10545. ref_type,
  10546. wasm_reftype_struct_size(ref_type));
  10547. j--;
  10548. }
  10549. #endif
  10550. #if WASM_ENABLE_FAST_INTERP != 0
  10551. /* emit the offset after return opcode */
  10552. POP_OFFSET_TYPE(ret_type);
  10553. #endif
  10554. POP_TYPE(ret_type);
  10555. }
  10556. RESET_STACK();
  10557. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10558. break;
  10559. }
  10560. case WASM_OP_CALL:
  10561. #if WASM_ENABLE_TAIL_CALL != 0
  10562. case WASM_OP_RETURN_CALL:
  10563. #endif
  10564. #if WASM_ENABLE_GC != 0
  10565. case WASM_OP_CALL_REF:
  10566. case WASM_OP_RETURN_CALL_REF:
  10567. #endif
  10568. {
  10569. WASMFuncType *func_type;
  10570. uint8 type;
  10571. int32 idx;
  10572. #if WASM_ENABLE_GC != 0
  10573. WASMRefType *ref_type;
  10574. uint32 type_idx1;
  10575. int32 j;
  10576. #endif
  10577. #if WASM_ENABLE_GC != 0
  10578. if (opcode == WASM_OP_CALL_REF
  10579. || opcode == WASM_OP_RETURN_CALL_REF) {
  10580. read_leb_uint32(p, p_end, type_idx1);
  10581. if (!check_type_index(module, module->type_count, type_idx1,
  10582. error_buf, error_buf_size)) {
  10583. goto fail;
  10584. }
  10585. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  10586. set_error_buf(error_buf, error_buf_size,
  10587. "unkown function type");
  10588. goto fail;
  10589. }
  10590. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10591. &type_idx, error_buf,
  10592. error_buf_size)) {
  10593. goto fail;
  10594. }
  10595. if (type == VALUE_TYPE_ANY) {
  10596. type_idx = type_idx1;
  10597. }
  10598. if (!check_type_index(module, module->type_count, type_idx,
  10599. error_buf, error_buf_size)) {
  10600. goto fail;
  10601. }
  10602. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10603. set_error_buf(error_buf, error_buf_size,
  10604. "unkown function type");
  10605. goto fail;
  10606. }
  10607. if (!wasm_func_type_is_super_of(
  10608. (WASMFuncType *)module->types[type_idx1],
  10609. (WASMFuncType *)module->types[type_idx])) {
  10610. set_error_buf(error_buf, error_buf_size,
  10611. "function type mismatch");
  10612. goto fail;
  10613. }
  10614. func_type = (WASMFuncType *)module->types[type_idx];
  10615. }
  10616. else
  10617. #endif
  10618. {
  10619. read_leb_uint32(p, p_end, func_idx);
  10620. #if WASM_ENABLE_FAST_INTERP != 0
  10621. /* we need to emit func_idx before arguments */
  10622. emit_uint32(loader_ctx, func_idx);
  10623. #endif
  10624. if (!check_function_index(module, func_idx, error_buf,
  10625. error_buf_size)) {
  10626. goto fail;
  10627. }
  10628. if (func_idx < module->import_function_count)
  10629. func_type = module->import_functions[func_idx]
  10630. .u.function.func_type;
  10631. else
  10632. func_type =
  10633. module
  10634. ->functions[func_idx
  10635. - module->import_function_count]
  10636. ->func_type;
  10637. }
  10638. if (func_type->param_count > 0) {
  10639. #if WASM_ENABLE_GC != 0
  10640. j = (int32)(func_type->result_ref_type_maps
  10641. - func_type->ref_type_maps - 1);
  10642. #endif
  10643. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10644. idx--) {
  10645. #if WASM_ENABLE_GC != 0
  10646. if (wasm_is_type_multi_byte_type(
  10647. func_type->types[idx])) {
  10648. ref_type = func_type->ref_type_maps[j].ref_type;
  10649. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10650. ref_type,
  10651. wasm_reftype_struct_size(ref_type));
  10652. j--;
  10653. }
  10654. #endif
  10655. #if WASM_ENABLE_FAST_INTERP != 0
  10656. POP_OFFSET_TYPE(func_type->types[idx]);
  10657. #endif
  10658. POP_TYPE(func_type->types[idx]);
  10659. }
  10660. }
  10661. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10662. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10663. #endif
  10664. #if WASM_ENABLE_GC != 0
  10665. j = (int32)(func_type->result_ref_type_maps
  10666. - func_type->ref_type_maps);
  10667. #endif
  10668. for (i = 0; i < func_type->result_count; i++) {
  10669. #if WASM_ENABLE_GC != 0
  10670. if (wasm_is_type_multi_byte_type(
  10671. func_type->types[func_type->param_count + i])) {
  10672. ref_type = func_type->ref_type_maps[j].ref_type;
  10673. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10674. ref_type,
  10675. wasm_reftype_struct_size(ref_type));
  10676. j++;
  10677. }
  10678. #endif
  10679. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10680. #if WASM_ENABLE_FAST_INTERP != 0
  10681. /* Here we emit each return value's dynamic_offset. But
  10682. * in fact these offsets are continuous, so interpreter
  10683. * only need to get the first return value's offset.
  10684. */
  10685. PUSH_OFFSET_TYPE(
  10686. func_type->types[func_type->param_count + i]);
  10687. #endif
  10688. }
  10689. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10690. }
  10691. else {
  10692. #if WASM_ENABLE_GC == 0
  10693. if (func_type->result_count
  10694. != func->func_type->result_count) {
  10695. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10696. "type mismatch: expect ",
  10697. func->func_type->result_count,
  10698. " return values but got other");
  10699. goto fail;
  10700. }
  10701. for (i = 0; i < func_type->result_count; i++) {
  10702. type = func->func_type
  10703. ->types[func->func_type->param_count + i];
  10704. if (func_type->types[func_type->param_count + i]
  10705. != type) {
  10706. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10707. "type mismatch: expect ",
  10708. type2str(type), " but got other");
  10709. goto fail;
  10710. }
  10711. }
  10712. #else
  10713. if (!wasm_func_type_result_is_subtype_of(
  10714. func_type, func->func_type, module->types,
  10715. module->type_count)) {
  10716. set_error_buf(
  10717. error_buf, error_buf_size,
  10718. "type mismatch: invalid func result types");
  10719. goto fail;
  10720. }
  10721. #endif
  10722. RESET_STACK();
  10723. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10724. }
  10725. #endif
  10726. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10727. || WASM_ENABLE_WAMR_COMPILER != 0
  10728. func->has_op_func_call = true;
  10729. #endif
  10730. (void)type;
  10731. break;
  10732. }
  10733. /*
  10734. * if disable reference type: call_indirect typeidx, 0x00
  10735. * if enable reference type: call_indirect typeidx, tableidx
  10736. */
  10737. case WASM_OP_CALL_INDIRECT:
  10738. #if WASM_ENABLE_TAIL_CALL != 0
  10739. case WASM_OP_RETURN_CALL_INDIRECT:
  10740. #endif
  10741. {
  10742. int32 idx;
  10743. WASMFuncType *func_type;
  10744. read_leb_uint32(p, p_end, type_idx);
  10745. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10746. #if WASM_ENABLE_WAMR_COMPILER != 0
  10747. if (p + 1 < p_end && *p != 0x00) {
  10748. /*
  10749. * Any non-0x00 byte requires the ref types proposal.
  10750. * This is different from checking the table_idx value
  10751. * since `0x80 0x00` etc. are all valid encodings of zero.
  10752. */
  10753. module->is_ref_types_used = true;
  10754. }
  10755. #endif
  10756. read_leb_uint32(p, p_end, table_idx);
  10757. #else
  10758. CHECK_BUF(p, p_end, 1);
  10759. table_idx = read_uint8(p);
  10760. #endif
  10761. if (!check_table_index(module, table_idx, error_buf,
  10762. error_buf_size)) {
  10763. goto fail;
  10764. }
  10765. #if WASM_ENABLE_FAST_INTERP != 0
  10766. /* we need to emit before arguments */
  10767. #if WASM_ENABLE_TAIL_CALL != 0
  10768. emit_byte(loader_ctx, opcode);
  10769. #endif
  10770. emit_uint32(loader_ctx, type_idx);
  10771. emit_uint32(loader_ctx, table_idx);
  10772. #endif
  10773. /* skip elem idx */
  10774. POP_I32();
  10775. if (type_idx >= module->type_count) {
  10776. set_error_buf(error_buf, error_buf_size, "unknown type");
  10777. goto fail;
  10778. }
  10779. func_type = (WASMFuncType *)module->types[type_idx];
  10780. if (func_type->param_count > 0) {
  10781. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10782. idx--) {
  10783. #if WASM_ENABLE_FAST_INTERP != 0
  10784. POP_OFFSET_TYPE(func_type->types[idx]);
  10785. #endif
  10786. POP_TYPE(func_type->types[idx]);
  10787. }
  10788. }
  10789. #if WASM_ENABLE_TAIL_CALL != 0
  10790. if (opcode == WASM_OP_CALL_INDIRECT) {
  10791. #endif
  10792. for (i = 0; i < func_type->result_count; i++) {
  10793. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10794. #if WASM_ENABLE_FAST_INTERP != 0
  10795. PUSH_OFFSET_TYPE(
  10796. func_type->types[func_type->param_count + i]);
  10797. #endif
  10798. }
  10799. #if WASM_ENABLE_TAIL_CALL != 0
  10800. }
  10801. else {
  10802. uint8 type;
  10803. if (func_type->result_count
  10804. != func->func_type->result_count) {
  10805. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10806. "type mismatch: expect ",
  10807. func->func_type->result_count,
  10808. " return values but got other");
  10809. goto fail;
  10810. }
  10811. for (i = 0; i < func_type->result_count; i++) {
  10812. type = func->func_type
  10813. ->types[func->func_type->param_count + i];
  10814. if (func_type->types[func_type->param_count + i]
  10815. != type) {
  10816. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10817. "type mismatch: expect ",
  10818. type2str(type), " but got other");
  10819. goto fail;
  10820. }
  10821. }
  10822. RESET_STACK();
  10823. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10824. }
  10825. #endif
  10826. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10827. || WASM_ENABLE_WAMR_COMPILER != 0
  10828. func->has_op_func_call = true;
  10829. #endif
  10830. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10831. func->has_op_call_indirect = true;
  10832. #endif
  10833. break;
  10834. }
  10835. case WASM_OP_DROP:
  10836. {
  10837. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10838. int32 available_stack_cell =
  10839. (int32)(loader_ctx->stack_cell_num
  10840. - cur_block->stack_cell_num);
  10841. if (available_stack_cell <= 0
  10842. && !cur_block->is_stack_polymorphic) {
  10843. set_error_buf(error_buf, error_buf_size,
  10844. "type mismatch, opcode drop was found "
  10845. "but stack was empty");
  10846. goto fail;
  10847. }
  10848. if (available_stack_cell > 0) {
  10849. #if WASM_ENABLE_GC != 0
  10850. if (wasm_is_type_multi_byte_type(
  10851. *(loader_ctx->frame_ref - 1))) {
  10852. bh_assert((int32)(loader_ctx->reftype_map_num
  10853. - cur_block->reftype_map_num)
  10854. > 0);
  10855. loader_ctx->frame_reftype_map--;
  10856. loader_ctx->reftype_map_num--;
  10857. }
  10858. #endif
  10859. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  10860. loader_ctx->frame_ref--;
  10861. loader_ctx->stack_cell_num--;
  10862. #if WASM_ENABLE_FAST_INTERP != 0
  10863. skip_label();
  10864. loader_ctx->frame_offset--;
  10865. if ((*(loader_ctx->frame_offset)
  10866. > loader_ctx->start_dynamic_offset)
  10867. && (*(loader_ctx->frame_offset)
  10868. < loader_ctx->max_dynamic_offset))
  10869. loader_ctx->dynamic_offset--;
  10870. #endif
  10871. }
  10872. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10873. loader_ctx->frame_ref -= 2;
  10874. loader_ctx->stack_cell_num -= 2;
  10875. #if WASM_ENABLE_FAST_INTERP == 0
  10876. *(p - 1) = WASM_OP_DROP_64;
  10877. #endif
  10878. #if WASM_ENABLE_FAST_INTERP != 0
  10879. skip_label();
  10880. loader_ctx->frame_offset -= 2;
  10881. if ((*(loader_ctx->frame_offset)
  10882. > loader_ctx->start_dynamic_offset)
  10883. && (*(loader_ctx->frame_offset)
  10884. < loader_ctx->max_dynamic_offset))
  10885. loader_ctx->dynamic_offset -= 2;
  10886. #endif
  10887. }
  10888. #if WASM_ENABLE_SIMD != 0
  10889. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10890. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10891. loader_ctx->frame_ref -= 4;
  10892. loader_ctx->stack_cell_num -= 4;
  10893. }
  10894. #endif
  10895. #endif
  10896. else {
  10897. set_error_buf(error_buf, error_buf_size,
  10898. "type mismatch");
  10899. goto fail;
  10900. }
  10901. }
  10902. else {
  10903. #if WASM_ENABLE_FAST_INTERP != 0
  10904. skip_label();
  10905. #endif
  10906. }
  10907. break;
  10908. }
  10909. case WASM_OP_SELECT:
  10910. {
  10911. uint8 ref_type;
  10912. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10913. int32 available_stack_cell;
  10914. #if WASM_ENABLE_FAST_INTERP != 0
  10915. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10916. #endif
  10917. POP_I32();
  10918. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  10919. - cur_block->stack_cell_num);
  10920. if (available_stack_cell <= 0
  10921. && !cur_block->is_stack_polymorphic) {
  10922. set_error_buf(error_buf, error_buf_size,
  10923. "type mismatch or invalid result arity, "
  10924. "opcode select was found "
  10925. "but stack was empty");
  10926. goto fail;
  10927. }
  10928. if (available_stack_cell > 0) {
  10929. switch (*(loader_ctx->frame_ref - 1)) {
  10930. case VALUE_TYPE_I32:
  10931. case VALUE_TYPE_F32:
  10932. case VALUE_TYPE_ANY:
  10933. break;
  10934. case VALUE_TYPE_I64:
  10935. case VALUE_TYPE_F64:
  10936. #if WASM_ENABLE_FAST_INTERP == 0
  10937. *(p - 1) = WASM_OP_SELECT_64;
  10938. #endif
  10939. #if WASM_ENABLE_FAST_INTERP != 0
  10940. if (loader_ctx->p_code_compiled) {
  10941. uint8 opcode_tmp = WASM_OP_SELECT_64;
  10942. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10943. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10944. *(void **)(p_code_compiled_tmp
  10945. - sizeof(void *)) =
  10946. handle_table[opcode_tmp];
  10947. #else
  10948. #if UINTPTR_MAX == UINT64_MAX
  10949. /* emit int32 relative offset in 64-bit target
  10950. */
  10951. int32 offset =
  10952. (int32)((uint8 *)handle_table[opcode_tmp]
  10953. - (uint8 *)handle_table[0]);
  10954. *(int32 *)(p_code_compiled_tmp
  10955. - sizeof(int32)) = offset;
  10956. #else
  10957. /* emit uint32 label address in 32-bit target */
  10958. *(uint32 *)(p_code_compiled_tmp
  10959. - sizeof(uint32)) =
  10960. (uint32)(uintptr_t)handle_table[opcode_tmp];
  10961. #endif
  10962. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10963. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  10964. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10965. *(p_code_compiled_tmp - 1) = opcode_tmp;
  10966. #else
  10967. *(p_code_compiled_tmp - 2) = opcode_tmp;
  10968. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10969. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  10970. }
  10971. #endif /* end of WASM_ENABLE_FAST_INTERP */
  10972. break;
  10973. #if WASM_ENABLE_SIMD != 0
  10974. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10975. case VALUE_TYPE_V128:
  10976. break;
  10977. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  10978. #endif /* WASM_ENABLE_SIMD != 0 */
  10979. default:
  10980. {
  10981. set_error_buf(error_buf, error_buf_size,
  10982. "type mismatch");
  10983. goto fail;
  10984. }
  10985. }
  10986. ref_type = *(loader_ctx->frame_ref - 1);
  10987. #if WASM_ENABLE_FAST_INTERP != 0
  10988. POP_OFFSET_TYPE(ref_type);
  10989. POP_TYPE(ref_type);
  10990. POP_OFFSET_TYPE(ref_type);
  10991. POP_TYPE(ref_type);
  10992. PUSH_OFFSET_TYPE(ref_type);
  10993. PUSH_TYPE(ref_type);
  10994. #else
  10995. POP2_AND_PUSH(ref_type, ref_type);
  10996. #endif
  10997. }
  10998. else {
  10999. #if WASM_ENABLE_FAST_INTERP != 0
  11000. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11001. #endif
  11002. PUSH_TYPE(VALUE_TYPE_ANY);
  11003. }
  11004. break;
  11005. }
  11006. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11007. case WASM_OP_SELECT_T:
  11008. {
  11009. uint8 vec_len, type;
  11010. #if WASM_ENABLE_GC != 0
  11011. WASMRefType *ref_type = NULL;
  11012. #endif
  11013. #if WASM_ENABLE_FAST_INTERP != 0
  11014. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11015. #endif
  11016. read_leb_uint32(p, p_end, vec_len);
  11017. if (vec_len != 1) {
  11018. /* typed select must have exactly one result */
  11019. set_error_buf(error_buf, error_buf_size,
  11020. "invalid result arity");
  11021. goto fail;
  11022. }
  11023. #if WASM_ENABLE_GC == 0
  11024. CHECK_BUF(p, p_end, 1);
  11025. type = read_uint8(p);
  11026. if (!is_valid_value_type_for_interpreter(type)) {
  11027. set_error_buf(error_buf, error_buf_size,
  11028. "unknown value type");
  11029. goto fail;
  11030. }
  11031. #else
  11032. p_org = p + 1;
  11033. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11034. module->type_count, &need_ref_type_map,
  11035. &wasm_ref_type, false, error_buf,
  11036. error_buf_size)) {
  11037. goto fail;
  11038. }
  11039. type = wasm_ref_type.ref_type;
  11040. if (need_ref_type_map) {
  11041. if (!(ref_type = reftype_set_insert(
  11042. module->ref_type_set, &wasm_ref_type, error_buf,
  11043. error_buf_size))) {
  11044. goto fail;
  11045. }
  11046. }
  11047. #if WASM_ENABLE_FAST_INTERP == 0
  11048. while (p_org < p) {
  11049. #if WASM_ENABLE_DEBUG_INTERP != 0
  11050. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11051. error_buf_size)) {
  11052. goto fail;
  11053. }
  11054. #endif
  11055. /* Ignore extra bytes for interpreter */
  11056. *p_org++ = WASM_OP_NOP;
  11057. }
  11058. #endif
  11059. #endif /* end of WASM_ENABLE_GC == 0 */
  11060. POP_I32();
  11061. #if WASM_ENABLE_FAST_INTERP != 0
  11062. if (loader_ctx->p_code_compiled) {
  11063. uint8 opcode_tmp = WASM_OP_SELECT;
  11064. if (type == VALUE_TYPE_V128) {
  11065. #if (WASM_ENABLE_SIMD == 0) \
  11066. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  11067. set_error_buf(error_buf, error_buf_size,
  11068. "SIMD v128 type isn't supported");
  11069. goto fail;
  11070. #endif
  11071. }
  11072. else {
  11073. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11074. opcode_tmp = WASM_OP_SELECT_64;
  11075. #if WASM_ENABLE_GC != 0
  11076. if (wasm_is_type_reftype(type))
  11077. opcode_tmp = WASM_OP_SELECT_T;
  11078. #endif
  11079. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11080. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11081. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11082. handle_table[opcode_tmp];
  11083. #else
  11084. #if UINTPTR_MAX == UINT64_MAX
  11085. /* emit int32 relative offset in 64-bit target */
  11086. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11087. - (uint8 *)handle_table[0]);
  11088. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11089. offset;
  11090. #else
  11091. /* emit uint32 label address in 32-bit target */
  11092. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11093. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11094. #endif
  11095. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11096. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11097. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11098. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11099. #else
  11100. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11101. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11102. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11103. }
  11104. }
  11105. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11106. POP_REF(type);
  11107. #if WASM_ENABLE_GC != 0
  11108. if (need_ref_type_map) {
  11109. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11110. wasm_reftype_struct_size(ref_type));
  11111. }
  11112. #endif
  11113. POP_REF(type);
  11114. #if WASM_ENABLE_GC != 0
  11115. if (need_ref_type_map) {
  11116. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11117. wasm_reftype_struct_size(ref_type));
  11118. }
  11119. #endif
  11120. PUSH_REF(type);
  11121. #if WASM_ENABLE_WAMR_COMPILER != 0
  11122. module->is_ref_types_used = true;
  11123. #endif
  11124. (void)vec_len;
  11125. break;
  11126. }
  11127. /* table.get x. tables[x]. [i32] -> [t] */
  11128. /* table.set x. tables[x]. [i32 t] -> [] */
  11129. case WASM_OP_TABLE_GET:
  11130. case WASM_OP_TABLE_SET:
  11131. {
  11132. uint8 decl_ref_type;
  11133. #if WASM_ENABLE_GC != 0
  11134. WASMRefType *ref_type;
  11135. #endif
  11136. read_leb_uint32(p, p_end, table_idx);
  11137. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11138. #if WASM_ENABLE_GC != 0
  11139. (void **)&ref_type,
  11140. #else
  11141. NULL,
  11142. #endif
  11143. error_buf, error_buf_size))
  11144. goto fail;
  11145. #if WASM_ENABLE_GC != 0
  11146. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11147. bh_assert(ref_type);
  11148. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11149. ref_type, wasm_reftype_struct_size(ref_type));
  11150. }
  11151. #endif
  11152. #if WASM_ENABLE_FAST_INTERP != 0
  11153. emit_uint32(loader_ctx, table_idx);
  11154. #endif
  11155. if (opcode == WASM_OP_TABLE_GET) {
  11156. POP_I32();
  11157. #if WASM_ENABLE_FAST_INTERP != 0
  11158. PUSH_OFFSET_TYPE(decl_ref_type);
  11159. #endif
  11160. PUSH_TYPE(decl_ref_type);
  11161. }
  11162. else {
  11163. #if WASM_ENABLE_FAST_INTERP != 0
  11164. POP_OFFSET_TYPE(decl_ref_type);
  11165. #endif
  11166. POP_TYPE(decl_ref_type);
  11167. POP_I32();
  11168. }
  11169. #if WASM_ENABLE_WAMR_COMPILER != 0
  11170. module->is_ref_types_used = true;
  11171. #endif
  11172. break;
  11173. }
  11174. case WASM_OP_REF_NULL:
  11175. {
  11176. uint8 ref_type;
  11177. #if WASM_ENABLE_GC == 0
  11178. CHECK_BUF(p, p_end, 1);
  11179. ref_type = read_uint8(p);
  11180. if (ref_type != VALUE_TYPE_FUNCREF
  11181. && ref_type != VALUE_TYPE_EXTERNREF) {
  11182. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11183. goto fail;
  11184. }
  11185. #else
  11186. read_leb_int32(p, p_end, heap_type);
  11187. if (heap_type >= 0) {
  11188. if (!check_type_index(module, module->type_count, heap_type,
  11189. error_buf, error_buf_size)) {
  11190. goto fail;
  11191. }
  11192. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11193. true, heap_type);
  11194. ref_type = wasm_ref_type.ref_type;
  11195. }
  11196. else {
  11197. if (!wasm_is_valid_heap_type(heap_type)) {
  11198. set_error_buf(error_buf, error_buf_size,
  11199. "unknown type");
  11200. goto fail;
  11201. }
  11202. ref_type = (uint8)((int32)0x80 + heap_type);
  11203. }
  11204. #endif /* end of WASM_ENABLE_GC == 0 */
  11205. #if WASM_ENABLE_FAST_INTERP != 0
  11206. PUSH_OFFSET_TYPE(ref_type);
  11207. #endif
  11208. PUSH_TYPE(ref_type);
  11209. #if WASM_ENABLE_WAMR_COMPILER != 0
  11210. module->is_ref_types_used = true;
  11211. #endif
  11212. break;
  11213. }
  11214. case WASM_OP_REF_IS_NULL:
  11215. {
  11216. #if WASM_ENABLE_GC == 0
  11217. #if WASM_ENABLE_FAST_INTERP != 0
  11218. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11219. int32 block_stack_cell_num =
  11220. (int32)(loader_ctx->stack_cell_num
  11221. - cur_block->stack_cell_num);
  11222. if (block_stack_cell_num <= 0) {
  11223. if (!cur_block->is_stack_polymorphic) {
  11224. set_error_buf(
  11225. error_buf, error_buf_size,
  11226. "type mismatch: expect data but stack was empty");
  11227. goto fail;
  11228. }
  11229. }
  11230. else {
  11231. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11232. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11233. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11234. if (!wasm_loader_pop_frame_ref_offset(
  11235. loader_ctx, *(loader_ctx->frame_ref - 1),
  11236. error_buf, error_buf_size)) {
  11237. goto fail;
  11238. }
  11239. }
  11240. else {
  11241. set_error_buf(error_buf, error_buf_size,
  11242. "type mismatch");
  11243. goto fail;
  11244. }
  11245. }
  11246. #else
  11247. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11248. error_buf, error_buf_size)
  11249. && !wasm_loader_pop_frame_ref(loader_ctx,
  11250. VALUE_TYPE_EXTERNREF,
  11251. error_buf, error_buf_size)) {
  11252. goto fail;
  11253. }
  11254. #endif
  11255. #else /* else of WASM_ENABLE_GC == 0 */
  11256. uint8 type;
  11257. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11258. error_buf, error_buf_size)) {
  11259. goto fail;
  11260. }
  11261. #endif
  11262. PUSH_I32();
  11263. #if WASM_ENABLE_WAMR_COMPILER != 0
  11264. module->is_ref_types_used = true;
  11265. #endif
  11266. break;
  11267. }
  11268. case WASM_OP_REF_FUNC:
  11269. {
  11270. read_leb_uint32(p, p_end, func_idx);
  11271. if (!check_function_index(module, func_idx, error_buf,
  11272. error_buf_size)) {
  11273. goto fail;
  11274. }
  11275. /* Refer to a forward-declared function:
  11276. the function must be an import, exported, or present in
  11277. a table elem segment or global initializer to be used as
  11278. the operand to ref.func */
  11279. if (func_idx >= module->import_function_count) {
  11280. WASMTableSeg *table_seg = module->table_segments;
  11281. bool func_declared = false;
  11282. uint32 j;
  11283. for (i = 0; i < module->global_count; i++) {
  11284. if (module->globals[i].type.val_type
  11285. == VALUE_TYPE_FUNCREF
  11286. && module->globals[i].init_expr.init_expr_type
  11287. == INIT_EXPR_TYPE_FUNCREF_CONST
  11288. && module->globals[i].init_expr.u.u32 == func_idx) {
  11289. func_declared = true;
  11290. break;
  11291. }
  11292. }
  11293. if (!func_declared) {
  11294. /* Check whether the function is declared in table segs,
  11295. note that it doesn't matter whether the table seg's
  11296. mode is passive, active or declarative. */
  11297. for (i = 0; i < module->table_seg_count;
  11298. i++, table_seg++) {
  11299. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11300. #if WASM_ENABLE_GC != 0
  11301. /* elem type is (ref null? func) or
  11302. (ref null? $t) */
  11303. || ((table_seg->elem_type
  11304. == REF_TYPE_HT_NON_NULLABLE
  11305. || table_seg->elem_type
  11306. == REF_TYPE_HT_NULLABLE)
  11307. && (table_seg->elem_ref_type->ref_ht_common
  11308. .heap_type
  11309. == HEAP_TYPE_FUNC
  11310. || table_seg->elem_ref_type
  11311. ->ref_ht_common.heap_type
  11312. > 0))
  11313. #endif
  11314. ) {
  11315. for (j = 0; j < table_seg->value_count; j++) {
  11316. if (table_seg->init_values[j].u.ref_index
  11317. == func_idx) {
  11318. func_declared = true;
  11319. break;
  11320. }
  11321. }
  11322. }
  11323. }
  11324. }
  11325. if (!func_declared) {
  11326. /* Check whether the function is exported */
  11327. for (i = 0; i < module->export_count; i++) {
  11328. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11329. && module->exports[i].index == func_idx) {
  11330. func_declared = true;
  11331. break;
  11332. }
  11333. }
  11334. }
  11335. if (!func_declared) {
  11336. set_error_buf(error_buf, error_buf_size,
  11337. "undeclared function reference");
  11338. goto fail;
  11339. }
  11340. }
  11341. #if WASM_ENABLE_FAST_INTERP != 0
  11342. emit_uint32(loader_ctx, func_idx);
  11343. #endif
  11344. #if WASM_ENABLE_GC == 0
  11345. PUSH_FUNCREF();
  11346. #else
  11347. if (func_idx < module->import_function_count)
  11348. type_idx =
  11349. module->import_functions[func_idx].u.function.type_idx;
  11350. else
  11351. type_idx = module
  11352. ->functions[func_idx
  11353. - module->import_function_count]
  11354. ->type_idx;
  11355. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11356. false, type_idx);
  11357. PUSH_REF(wasm_ref_type.ref_type);
  11358. #endif
  11359. #if WASM_ENABLE_WAMR_COMPILER != 0
  11360. module->is_ref_types_used = true;
  11361. #endif
  11362. break;
  11363. }
  11364. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11365. #if WASM_ENABLE_GC != 0
  11366. case WASM_OP_REF_AS_NON_NULL:
  11367. case WASM_OP_BR_ON_NULL:
  11368. {
  11369. uint8 type;
  11370. WASMRefType ref_type;
  11371. /* POP (ref null ht) and get the converted (ref ht) */
  11372. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11373. error_buf, error_buf_size)) {
  11374. goto fail;
  11375. }
  11376. if (opcode == WASM_OP_BR_ON_NULL) {
  11377. if (!(frame_csp_tmp =
  11378. check_branch_block(loader_ctx, &p, p_end, opcode,
  11379. error_buf, error_buf_size))) {
  11380. goto fail;
  11381. }
  11382. #if WASM_ENABLE_FAST_INTERP != 0
  11383. disable_emit = true;
  11384. #endif
  11385. }
  11386. /* PUSH the converted (ref ht) */
  11387. if (type != VALUE_TYPE_ANY) {
  11388. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11389. sizeof(WASMRefType));
  11390. }
  11391. PUSH_REF(type);
  11392. break;
  11393. }
  11394. case WASM_OP_BR_ON_NON_NULL:
  11395. {
  11396. uint8 type;
  11397. WASMRefType ref_type;
  11398. uint32 available_stack_cell =
  11399. loader_ctx->stack_cell_num
  11400. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11401. /* POP (ref null ht) and get the converted (ref ht) */
  11402. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11403. error_buf, error_buf_size)) {
  11404. goto fail;
  11405. }
  11406. #if WASM_ENABLE_FAST_INTERP != 0
  11407. disable_emit = true;
  11408. #endif
  11409. /* Temporarily PUSH back (ref ht), check brach block and
  11410. then POP it */
  11411. if (available_stack_cell
  11412. > 0) { /* stack isn't in polymorphic state */
  11413. if (type != VALUE_TYPE_ANY) {
  11414. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11415. &ref_type, sizeof(WASMRefType));
  11416. }
  11417. PUSH_REF(type);
  11418. }
  11419. if (!(frame_csp_tmp =
  11420. check_branch_block(loader_ctx, &p, p_end, opcode,
  11421. error_buf, error_buf_size))) {
  11422. goto fail;
  11423. }
  11424. if (available_stack_cell
  11425. > 0) { /* stack isn't in polymorphic state */
  11426. POP_REF(type);
  11427. #if WASM_ENABLE_FAST_INTERP != 0
  11428. /* Erase the opnd offset emitted by POP_REF() */
  11429. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11430. #endif
  11431. }
  11432. break;
  11433. }
  11434. case WASM_OP_REF_EQ:
  11435. POP_REF(REF_TYPE_EQREF);
  11436. POP_REF(REF_TYPE_EQREF);
  11437. PUSH_I32();
  11438. break;
  11439. #endif /* end of WASM_ENABLE_GC != 0 */
  11440. case WASM_OP_GET_LOCAL:
  11441. {
  11442. p_org = p - 1;
  11443. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11444. PUSH_TYPE(local_type);
  11445. #if WASM_ENABLE_GC != 0
  11446. /* Cannot get a non-nullable and unset local */
  11447. if (local_idx >= param_count
  11448. && wasm_is_reftype_htref_non_nullable(local_type)
  11449. && !wasm_loader_get_local_status(loader_ctx,
  11450. local_idx - param_count)) {
  11451. set_error_buf(error_buf, error_buf_size,
  11452. "uninitialized local");
  11453. goto fail;
  11454. }
  11455. #endif
  11456. #if WASM_ENABLE_FAST_INTERP != 0
  11457. /* Get Local is optimized out */
  11458. skip_label();
  11459. disable_emit = true;
  11460. operand_offset = local_offset;
  11461. PUSH_OFFSET_TYPE(local_type);
  11462. #else
  11463. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11464. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11465. if (local_offset < 0x80
  11466. #if WASM_ENABLE_GC != 0
  11467. && !wasm_is_type_reftype(local_type)
  11468. #endif
  11469. ) {
  11470. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11471. if (is_32bit_type(local_type)) {
  11472. *p_org++ = (uint8)local_offset;
  11473. }
  11474. else {
  11475. *p_org++ = (uint8)(local_offset | 0x80);
  11476. }
  11477. while (p_org < p) {
  11478. *p_org++ = WASM_OP_NOP;
  11479. }
  11480. }
  11481. #endif
  11482. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11483. break;
  11484. }
  11485. case WASM_OP_SET_LOCAL:
  11486. {
  11487. p_org = p - 1;
  11488. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11489. #if WASM_ENABLE_FAST_INTERP != 0
  11490. if (!(preserve_referenced_local(
  11491. loader_ctx, opcode, local_offset, local_type,
  11492. &preserve_local, error_buf, error_buf_size)))
  11493. goto fail;
  11494. if (local_offset < 256
  11495. #if WASM_ENABLE_GC != 0
  11496. && !wasm_is_type_reftype(local_type)
  11497. #endif
  11498. ) {
  11499. skip_label();
  11500. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11501. if (loader_ctx->p_code_compiled)
  11502. STORE_U16(loader_ctx->p_code_compiled - 2,
  11503. local_offset);
  11504. loader_ctx->frame_offset--;
  11505. loader_ctx->dynamic_offset--;
  11506. }
  11507. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11508. if (loader_ctx->p_code_compiled)
  11509. STORE_U16(loader_ctx->p_code_compiled - 2,
  11510. local_offset);
  11511. loader_ctx->frame_offset -= 2;
  11512. loader_ctx->dynamic_offset -= 2;
  11513. }
  11514. else {
  11515. if (is_32bit_type(local_type)) {
  11516. emit_label(EXT_OP_SET_LOCAL_FAST);
  11517. emit_byte(loader_ctx, (uint8)local_offset);
  11518. }
  11519. else {
  11520. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11521. emit_byte(loader_ctx, (uint8)local_offset);
  11522. }
  11523. POP_OFFSET_TYPE(local_type);
  11524. }
  11525. }
  11526. else { /* local index larger than 255, reserve leb */
  11527. emit_uint32(loader_ctx, local_idx);
  11528. POP_OFFSET_TYPE(local_type);
  11529. }
  11530. #else
  11531. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11532. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11533. if (local_offset < 0x80
  11534. #if WASM_ENABLE_GC != 0
  11535. && !wasm_is_type_reftype(local_type)
  11536. #endif
  11537. ) {
  11538. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11539. if (is_32bit_type(local_type)) {
  11540. *p_org++ = (uint8)local_offset;
  11541. }
  11542. else {
  11543. *p_org++ = (uint8)(local_offset | 0x80);
  11544. }
  11545. while (p_org < p) {
  11546. *p_org++ = WASM_OP_NOP;
  11547. }
  11548. }
  11549. #endif
  11550. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11551. #if WASM_ENABLE_GC != 0
  11552. if (local_idx >= param_count) {
  11553. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11554. }
  11555. #endif
  11556. POP_TYPE(local_type);
  11557. break;
  11558. }
  11559. case WASM_OP_TEE_LOCAL:
  11560. {
  11561. p_org = p - 1;
  11562. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11563. #if WASM_ENABLE_FAST_INTERP != 0
  11564. /* If the stack is in polymorphic state, do fake pop and push on
  11565. offset stack to keep the depth of offset stack to be the
  11566. same with ref stack */
  11567. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11568. if (cur_block->is_stack_polymorphic) {
  11569. POP_OFFSET_TYPE(local_type);
  11570. PUSH_OFFSET_TYPE(local_type);
  11571. }
  11572. #endif
  11573. POP_TYPE(local_type);
  11574. PUSH_TYPE(local_type);
  11575. #if WASM_ENABLE_FAST_INTERP != 0
  11576. if (!(preserve_referenced_local(
  11577. loader_ctx, opcode, local_offset, local_type,
  11578. &preserve_local, error_buf, error_buf_size)))
  11579. goto fail;
  11580. if (local_offset < 256
  11581. #if WASM_ENABLE_GC != 0
  11582. && !wasm_is_type_reftype(local_type)
  11583. #endif
  11584. ) {
  11585. skip_label();
  11586. if (is_32bit_type(local_type)) {
  11587. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11588. emit_byte(loader_ctx, (uint8)local_offset);
  11589. }
  11590. else {
  11591. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11592. emit_byte(loader_ctx, (uint8)local_offset);
  11593. }
  11594. }
  11595. else { /* local index larger than 255, reserve leb */
  11596. emit_uint32(loader_ctx, local_idx);
  11597. }
  11598. emit_operand(loader_ctx,
  11599. *(loader_ctx->frame_offset
  11600. - wasm_value_type_cell_num(local_type)));
  11601. #else
  11602. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11603. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11604. if (local_offset < 0x80
  11605. #if WASM_ENABLE_GC != 0
  11606. && !wasm_is_type_reftype(local_type)
  11607. #endif
  11608. ) {
  11609. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11610. if (is_32bit_type(local_type)) {
  11611. *p_org++ = (uint8)local_offset;
  11612. }
  11613. else {
  11614. *p_org++ = (uint8)(local_offset | 0x80);
  11615. }
  11616. while (p_org < p) {
  11617. *p_org++ = WASM_OP_NOP;
  11618. }
  11619. }
  11620. #endif
  11621. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11622. #if WASM_ENABLE_GC != 0
  11623. if (local_idx >= param_count) {
  11624. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11625. }
  11626. #endif
  11627. break;
  11628. }
  11629. case WASM_OP_GET_GLOBAL:
  11630. {
  11631. #if WASM_ENABLE_GC != 0
  11632. WASMRefType *ref_type;
  11633. #endif
  11634. p_org = p - 1;
  11635. read_leb_uint32(p, p_end, global_idx);
  11636. if (global_idx >= global_count) {
  11637. set_error_buf(error_buf, error_buf_size, "unknown global");
  11638. goto fail;
  11639. }
  11640. global_type = global_idx < module->import_global_count
  11641. ? module->import_globals[global_idx]
  11642. .u.global.type.val_type
  11643. : module
  11644. ->globals[global_idx
  11645. - module->import_global_count]
  11646. .type.val_type;
  11647. #if WASM_ENABLE_GC != 0
  11648. ref_type =
  11649. global_idx < module->import_global_count
  11650. ? module->import_globals[global_idx].u.global.ref_type
  11651. : module
  11652. ->globals[global_idx
  11653. - module->import_global_count]
  11654. .ref_type;
  11655. if (wasm_is_type_multi_byte_type(global_type)) {
  11656. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11657. wasm_reftype_struct_size(ref_type));
  11658. }
  11659. #endif
  11660. PUSH_TYPE(global_type);
  11661. #if WASM_ENABLE_FAST_INTERP == 0
  11662. if (global_type == VALUE_TYPE_I64
  11663. || global_type == VALUE_TYPE_F64) {
  11664. #if WASM_ENABLE_DEBUG_INTERP != 0
  11665. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11666. error_buf_size)) {
  11667. goto fail;
  11668. }
  11669. #endif
  11670. *p_org = WASM_OP_GET_GLOBAL_64;
  11671. }
  11672. #else /* else of WASM_ENABLE_FAST_INTERP */
  11673. if (global_type == VALUE_TYPE_I64
  11674. || global_type == VALUE_TYPE_F64) {
  11675. skip_label();
  11676. emit_label(WASM_OP_GET_GLOBAL_64);
  11677. }
  11678. emit_uint32(loader_ctx, global_idx);
  11679. PUSH_OFFSET_TYPE(global_type);
  11680. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11681. break;
  11682. }
  11683. case WASM_OP_SET_GLOBAL:
  11684. {
  11685. bool is_mutable = false;
  11686. #if WASM_ENABLE_GC != 0
  11687. WASMRefType *ref_type;
  11688. #endif
  11689. p_org = p - 1;
  11690. read_leb_uint32(p, p_end, global_idx);
  11691. if (global_idx >= global_count) {
  11692. set_error_buf(error_buf, error_buf_size, "unknown global");
  11693. goto fail;
  11694. }
  11695. is_mutable = global_idx < module->import_global_count
  11696. ? module->import_globals[global_idx]
  11697. .u.global.type.is_mutable
  11698. : module
  11699. ->globals[global_idx
  11700. - module->import_global_count]
  11701. .type.is_mutable;
  11702. if (!is_mutable) {
  11703. #if WASM_ENABLE_GC == 0
  11704. set_error_buf(error_buf, error_buf_size,
  11705. "global is immutable");
  11706. #else
  11707. set_error_buf(error_buf, error_buf_size,
  11708. "immutable global");
  11709. #endif
  11710. goto fail;
  11711. }
  11712. global_type = global_idx < module->import_global_count
  11713. ? module->import_globals[global_idx]
  11714. .u.global.type.val_type
  11715. : module
  11716. ->globals[global_idx
  11717. - module->import_global_count]
  11718. .type.val_type;
  11719. #if WASM_ENABLE_GC != 0
  11720. ref_type =
  11721. global_idx < module->import_global_count
  11722. ? module->import_globals[global_idx].u.global.ref_type
  11723. : module
  11724. ->globals[global_idx
  11725. - module->import_global_count]
  11726. .ref_type;
  11727. if (wasm_is_type_multi_byte_type(global_type)) {
  11728. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11729. wasm_reftype_struct_size(ref_type));
  11730. }
  11731. #endif
  11732. #if WASM_ENABLE_FAST_INTERP == 0
  11733. if (global_type == VALUE_TYPE_I64
  11734. || global_type == VALUE_TYPE_F64) {
  11735. #if WASM_ENABLE_DEBUG_INTERP != 0
  11736. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11737. error_buf_size)) {
  11738. goto fail;
  11739. }
  11740. #endif
  11741. *p_org = WASM_OP_SET_GLOBAL_64;
  11742. }
  11743. else if (module->aux_stack_size > 0
  11744. && global_idx == module->aux_stack_top_global_index) {
  11745. #if WASM_ENABLE_DEBUG_INTERP != 0
  11746. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11747. error_buf_size)) {
  11748. goto fail;
  11749. }
  11750. #endif
  11751. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11752. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11753. func->has_op_set_global_aux_stack = true;
  11754. #endif
  11755. }
  11756. #else /* else of WASM_ENABLE_FAST_INTERP */
  11757. if (global_type == VALUE_TYPE_I64
  11758. || global_type == VALUE_TYPE_F64) {
  11759. skip_label();
  11760. emit_label(WASM_OP_SET_GLOBAL_64);
  11761. }
  11762. else if (module->aux_stack_size > 0
  11763. && global_idx == module->aux_stack_top_global_index) {
  11764. skip_label();
  11765. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11766. }
  11767. emit_uint32(loader_ctx, global_idx);
  11768. POP_OFFSET_TYPE(global_type);
  11769. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11770. POP_TYPE(global_type);
  11771. break;
  11772. }
  11773. /* load */
  11774. case WASM_OP_I32_LOAD:
  11775. case WASM_OP_I32_LOAD8_S:
  11776. case WASM_OP_I32_LOAD8_U:
  11777. case WASM_OP_I32_LOAD16_S:
  11778. case WASM_OP_I32_LOAD16_U:
  11779. case WASM_OP_I64_LOAD:
  11780. case WASM_OP_I64_LOAD8_S:
  11781. case WASM_OP_I64_LOAD8_U:
  11782. case WASM_OP_I64_LOAD16_S:
  11783. case WASM_OP_I64_LOAD16_U:
  11784. case WASM_OP_I64_LOAD32_S:
  11785. case WASM_OP_I64_LOAD32_U:
  11786. case WASM_OP_F32_LOAD:
  11787. case WASM_OP_F64_LOAD:
  11788. /* store */
  11789. case WASM_OP_I32_STORE:
  11790. case WASM_OP_I32_STORE8:
  11791. case WASM_OP_I32_STORE16:
  11792. case WASM_OP_I64_STORE:
  11793. case WASM_OP_I64_STORE8:
  11794. case WASM_OP_I64_STORE16:
  11795. case WASM_OP_I64_STORE32:
  11796. case WASM_OP_F32_STORE:
  11797. case WASM_OP_F64_STORE:
  11798. {
  11799. #if WASM_ENABLE_FAST_INTERP != 0
  11800. /* change F32/F64 into I32/I64 */
  11801. if (opcode == WASM_OP_F32_LOAD) {
  11802. skip_label();
  11803. emit_label(WASM_OP_I32_LOAD);
  11804. }
  11805. else if (opcode == WASM_OP_F64_LOAD) {
  11806. skip_label();
  11807. emit_label(WASM_OP_I64_LOAD);
  11808. }
  11809. else if (opcode == WASM_OP_F32_STORE) {
  11810. skip_label();
  11811. emit_label(WASM_OP_I32_STORE);
  11812. }
  11813. else if (opcode == WASM_OP_F64_STORE) {
  11814. skip_label();
  11815. emit_label(WASM_OP_I64_STORE);
  11816. }
  11817. #endif
  11818. CHECK_MEMORY();
  11819. read_leb_memarg(p, p_end, align); /* align */
  11820. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  11821. if (!check_memory_access_align(opcode, align, error_buf,
  11822. error_buf_size)) {
  11823. goto fail;
  11824. }
  11825. #if WASM_ENABLE_FAST_INTERP != 0
  11826. emit_uint32(loader_ctx, mem_offset);
  11827. #endif
  11828. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11829. func->has_memory_operations = true;
  11830. #endif
  11831. switch (opcode) {
  11832. /* load */
  11833. case WASM_OP_I32_LOAD:
  11834. case WASM_OP_I32_LOAD8_S:
  11835. case WASM_OP_I32_LOAD8_U:
  11836. case WASM_OP_I32_LOAD16_S:
  11837. case WASM_OP_I32_LOAD16_U:
  11838. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  11839. break;
  11840. case WASM_OP_I64_LOAD:
  11841. case WASM_OP_I64_LOAD8_S:
  11842. case WASM_OP_I64_LOAD8_U:
  11843. case WASM_OP_I64_LOAD16_S:
  11844. case WASM_OP_I64_LOAD16_U:
  11845. case WASM_OP_I64_LOAD32_S:
  11846. case WASM_OP_I64_LOAD32_U:
  11847. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  11848. break;
  11849. case WASM_OP_F32_LOAD:
  11850. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  11851. break;
  11852. case WASM_OP_F64_LOAD:
  11853. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  11854. break;
  11855. /* store */
  11856. case WASM_OP_I32_STORE:
  11857. case WASM_OP_I32_STORE8:
  11858. case WASM_OP_I32_STORE16:
  11859. POP_I32();
  11860. POP_MEM_OFFSET();
  11861. break;
  11862. case WASM_OP_I64_STORE:
  11863. case WASM_OP_I64_STORE8:
  11864. case WASM_OP_I64_STORE16:
  11865. case WASM_OP_I64_STORE32:
  11866. POP_I64();
  11867. POP_MEM_OFFSET();
  11868. break;
  11869. case WASM_OP_F32_STORE:
  11870. POP_F32();
  11871. POP_MEM_OFFSET();
  11872. break;
  11873. case WASM_OP_F64_STORE:
  11874. POP_F64();
  11875. POP_MEM_OFFSET();
  11876. break;
  11877. default:
  11878. break;
  11879. }
  11880. break;
  11881. }
  11882. case WASM_OP_MEMORY_SIZE:
  11883. CHECK_MEMORY();
  11884. read_leb_uint32(p, p_end, memidx);
  11885. check_memidx(module, memidx);
  11886. PUSH_PAGE_COUNT();
  11887. module->possible_memory_grow = true;
  11888. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11889. func->has_memory_operations = true;
  11890. #endif
  11891. break;
  11892. case WASM_OP_MEMORY_GROW:
  11893. CHECK_MEMORY();
  11894. read_leb_uint32(p, p_end, memidx);
  11895. check_memidx(module, memidx);
  11896. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  11897. module->possible_memory_grow = true;
  11898. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11899. || WASM_ENABLE_WAMR_COMPILER != 0
  11900. func->has_op_memory_grow = true;
  11901. #endif
  11902. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11903. func->has_memory_operations = true;
  11904. #endif
  11905. break;
  11906. case WASM_OP_I32_CONST:
  11907. read_leb_int32(p, p_end, i32_const);
  11908. #if WASM_ENABLE_FAST_INTERP != 0
  11909. skip_label();
  11910. disable_emit = true;
  11911. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  11912. if (operand_offset == 0) {
  11913. disable_emit = false;
  11914. emit_label(WASM_OP_I32_CONST);
  11915. emit_uint32(loader_ctx, i32_const);
  11916. }
  11917. #else
  11918. (void)i32_const;
  11919. #endif
  11920. PUSH_I32();
  11921. break;
  11922. case WASM_OP_I64_CONST:
  11923. read_leb_int64(p, p_end, i64_const);
  11924. #if WASM_ENABLE_FAST_INTERP != 0
  11925. skip_label();
  11926. disable_emit = true;
  11927. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  11928. if (operand_offset == 0) {
  11929. disable_emit = false;
  11930. emit_label(WASM_OP_I64_CONST);
  11931. emit_uint64(loader_ctx, i64_const);
  11932. }
  11933. #endif
  11934. PUSH_I64();
  11935. break;
  11936. case WASM_OP_F32_CONST:
  11937. CHECK_BUF(p, p_end, sizeof(float32));
  11938. p += sizeof(float32);
  11939. #if WASM_ENABLE_FAST_INTERP != 0
  11940. skip_label();
  11941. disable_emit = true;
  11942. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  11943. sizeof(float32));
  11944. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  11945. if (operand_offset == 0) {
  11946. disable_emit = false;
  11947. emit_label(WASM_OP_F32_CONST);
  11948. emit_float32(loader_ctx, f32_const);
  11949. }
  11950. #endif
  11951. PUSH_F32();
  11952. break;
  11953. case WASM_OP_F64_CONST:
  11954. CHECK_BUF(p, p_end, sizeof(float64));
  11955. p += sizeof(float64);
  11956. #if WASM_ENABLE_FAST_INTERP != 0
  11957. skip_label();
  11958. disable_emit = true;
  11959. /* Some MCU may require 8-byte align */
  11960. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  11961. sizeof(float64));
  11962. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  11963. if (operand_offset == 0) {
  11964. disable_emit = false;
  11965. emit_label(WASM_OP_F64_CONST);
  11966. emit_float64(loader_ctx, f64_const);
  11967. }
  11968. #endif
  11969. PUSH_F64();
  11970. break;
  11971. case WASM_OP_I32_EQZ:
  11972. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11973. break;
  11974. case WASM_OP_I32_EQ:
  11975. case WASM_OP_I32_NE:
  11976. case WASM_OP_I32_LT_S:
  11977. case WASM_OP_I32_LT_U:
  11978. case WASM_OP_I32_GT_S:
  11979. case WASM_OP_I32_GT_U:
  11980. case WASM_OP_I32_LE_S:
  11981. case WASM_OP_I32_LE_U:
  11982. case WASM_OP_I32_GE_S:
  11983. case WASM_OP_I32_GE_U:
  11984. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11985. break;
  11986. case WASM_OP_I64_EQZ:
  11987. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11988. break;
  11989. case WASM_OP_I64_EQ:
  11990. case WASM_OP_I64_NE:
  11991. case WASM_OP_I64_LT_S:
  11992. case WASM_OP_I64_LT_U:
  11993. case WASM_OP_I64_GT_S:
  11994. case WASM_OP_I64_GT_U:
  11995. case WASM_OP_I64_LE_S:
  11996. case WASM_OP_I64_LE_U:
  11997. case WASM_OP_I64_GE_S:
  11998. case WASM_OP_I64_GE_U:
  11999. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12000. break;
  12001. case WASM_OP_F32_EQ:
  12002. case WASM_OP_F32_NE:
  12003. case WASM_OP_F32_LT:
  12004. case WASM_OP_F32_GT:
  12005. case WASM_OP_F32_LE:
  12006. case WASM_OP_F32_GE:
  12007. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12008. break;
  12009. case WASM_OP_F64_EQ:
  12010. case WASM_OP_F64_NE:
  12011. case WASM_OP_F64_LT:
  12012. case WASM_OP_F64_GT:
  12013. case WASM_OP_F64_LE:
  12014. case WASM_OP_F64_GE:
  12015. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12016. break;
  12017. case WASM_OP_I32_CLZ:
  12018. case WASM_OP_I32_CTZ:
  12019. case WASM_OP_I32_POPCNT:
  12020. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12021. break;
  12022. case WASM_OP_I32_ADD:
  12023. case WASM_OP_I32_SUB:
  12024. case WASM_OP_I32_MUL:
  12025. case WASM_OP_I32_DIV_S:
  12026. case WASM_OP_I32_DIV_U:
  12027. case WASM_OP_I32_REM_S:
  12028. case WASM_OP_I32_REM_U:
  12029. case WASM_OP_I32_AND:
  12030. case WASM_OP_I32_OR:
  12031. case WASM_OP_I32_XOR:
  12032. case WASM_OP_I32_SHL:
  12033. case WASM_OP_I32_SHR_S:
  12034. case WASM_OP_I32_SHR_U:
  12035. case WASM_OP_I32_ROTL:
  12036. case WASM_OP_I32_ROTR:
  12037. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12038. break;
  12039. case WASM_OP_I64_CLZ:
  12040. case WASM_OP_I64_CTZ:
  12041. case WASM_OP_I64_POPCNT:
  12042. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12043. break;
  12044. case WASM_OP_I64_ADD:
  12045. case WASM_OP_I64_SUB:
  12046. case WASM_OP_I64_MUL:
  12047. case WASM_OP_I64_DIV_S:
  12048. case WASM_OP_I64_DIV_U:
  12049. case WASM_OP_I64_REM_S:
  12050. case WASM_OP_I64_REM_U:
  12051. case WASM_OP_I64_AND:
  12052. case WASM_OP_I64_OR:
  12053. case WASM_OP_I64_XOR:
  12054. case WASM_OP_I64_SHL:
  12055. case WASM_OP_I64_SHR_S:
  12056. case WASM_OP_I64_SHR_U:
  12057. case WASM_OP_I64_ROTL:
  12058. case WASM_OP_I64_ROTR:
  12059. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12060. break;
  12061. case WASM_OP_F32_ABS:
  12062. case WASM_OP_F32_NEG:
  12063. case WASM_OP_F32_CEIL:
  12064. case WASM_OP_F32_FLOOR:
  12065. case WASM_OP_F32_TRUNC:
  12066. case WASM_OP_F32_NEAREST:
  12067. case WASM_OP_F32_SQRT:
  12068. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12069. break;
  12070. case WASM_OP_F32_ADD:
  12071. case WASM_OP_F32_SUB:
  12072. case WASM_OP_F32_MUL:
  12073. case WASM_OP_F32_DIV:
  12074. case WASM_OP_F32_MIN:
  12075. case WASM_OP_F32_MAX:
  12076. case WASM_OP_F32_COPYSIGN:
  12077. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12078. break;
  12079. case WASM_OP_F64_ABS:
  12080. case WASM_OP_F64_NEG:
  12081. case WASM_OP_F64_CEIL:
  12082. case WASM_OP_F64_FLOOR:
  12083. case WASM_OP_F64_TRUNC:
  12084. case WASM_OP_F64_NEAREST:
  12085. case WASM_OP_F64_SQRT:
  12086. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12087. break;
  12088. case WASM_OP_F64_ADD:
  12089. case WASM_OP_F64_SUB:
  12090. case WASM_OP_F64_MUL:
  12091. case WASM_OP_F64_DIV:
  12092. case WASM_OP_F64_MIN:
  12093. case WASM_OP_F64_MAX:
  12094. case WASM_OP_F64_COPYSIGN:
  12095. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12096. break;
  12097. case WASM_OP_I32_WRAP_I64:
  12098. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12099. break;
  12100. case WASM_OP_I32_TRUNC_S_F32:
  12101. case WASM_OP_I32_TRUNC_U_F32:
  12102. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12103. break;
  12104. case WASM_OP_I32_TRUNC_S_F64:
  12105. case WASM_OP_I32_TRUNC_U_F64:
  12106. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12107. break;
  12108. case WASM_OP_I64_EXTEND_S_I32:
  12109. case WASM_OP_I64_EXTEND_U_I32:
  12110. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12111. break;
  12112. case WASM_OP_I64_TRUNC_S_F32:
  12113. case WASM_OP_I64_TRUNC_U_F32:
  12114. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12115. break;
  12116. case WASM_OP_I64_TRUNC_S_F64:
  12117. case WASM_OP_I64_TRUNC_U_F64:
  12118. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12119. break;
  12120. case WASM_OP_F32_CONVERT_S_I32:
  12121. case WASM_OP_F32_CONVERT_U_I32:
  12122. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12123. break;
  12124. case WASM_OP_F32_CONVERT_S_I64:
  12125. case WASM_OP_F32_CONVERT_U_I64:
  12126. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12127. break;
  12128. case WASM_OP_F32_DEMOTE_F64:
  12129. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12130. break;
  12131. case WASM_OP_F64_CONVERT_S_I32:
  12132. case WASM_OP_F64_CONVERT_U_I32:
  12133. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12134. break;
  12135. case WASM_OP_F64_CONVERT_S_I64:
  12136. case WASM_OP_F64_CONVERT_U_I64:
  12137. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12138. break;
  12139. case WASM_OP_F64_PROMOTE_F32:
  12140. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12141. break;
  12142. case WASM_OP_I32_REINTERPRET_F32:
  12143. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12144. break;
  12145. case WASM_OP_I64_REINTERPRET_F64:
  12146. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12147. break;
  12148. case WASM_OP_F32_REINTERPRET_I32:
  12149. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12150. break;
  12151. case WASM_OP_F64_REINTERPRET_I64:
  12152. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12153. break;
  12154. case WASM_OP_I32_EXTEND8_S:
  12155. case WASM_OP_I32_EXTEND16_S:
  12156. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12157. break;
  12158. case WASM_OP_I64_EXTEND8_S:
  12159. case WASM_OP_I64_EXTEND16_S:
  12160. case WASM_OP_I64_EXTEND32_S:
  12161. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12162. break;
  12163. #if WASM_ENABLE_GC != 0
  12164. case WASM_OP_GC_PREFIX:
  12165. {
  12166. uint32 opcode1;
  12167. read_leb_uint32(p, p_end, opcode1);
  12168. #if WASM_ENABLE_FAST_INTERP != 0
  12169. emit_byte(loader_ctx, ((uint8)opcode1));
  12170. #endif
  12171. switch (opcode1) {
  12172. case WASM_OP_STRUCT_NEW:
  12173. case WASM_OP_STRUCT_NEW_DEFAULT:
  12174. {
  12175. read_leb_uint32(p, p_end, type_idx);
  12176. #if WASM_ENABLE_FAST_INTERP != 0
  12177. emit_uint32(loader_ctx, type_idx);
  12178. #endif
  12179. if (!check_type_index(module, module->type_count,
  12180. type_idx, error_buf,
  12181. error_buf_size)) {
  12182. goto fail;
  12183. }
  12184. if (module->types[type_idx]->type_flag
  12185. != WASM_TYPE_STRUCT) {
  12186. set_error_buf(error_buf, error_buf_size,
  12187. "unkown struct type");
  12188. goto fail;
  12189. }
  12190. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12191. int32 j, k;
  12192. uint8 value_type;
  12193. uint32 ref_type_struct_size;
  12194. WASMStructType *struct_type =
  12195. (WASMStructType *)module->types[type_idx];
  12196. k = struct_type->ref_type_map_count - 1;
  12197. for (j = struct_type->field_count - 1; j >= 0;
  12198. j--) {
  12199. value_type = struct_type->fields[j].field_type;
  12200. if (wasm_is_type_reftype(value_type)) {
  12201. if (wasm_is_type_multi_byte_type(
  12202. value_type)) {
  12203. ref_type_struct_size =
  12204. wasm_reftype_struct_size(
  12205. struct_type->ref_type_maps[k]
  12206. .ref_type);
  12207. bh_memcpy_s(
  12208. &wasm_ref_type,
  12209. (uint32)sizeof(WASMRefType),
  12210. struct_type->ref_type_maps[k]
  12211. .ref_type,
  12212. ref_type_struct_size);
  12213. k--;
  12214. }
  12215. POP_REF(value_type);
  12216. }
  12217. else {
  12218. switch (value_type) {
  12219. case VALUE_TYPE_I32:
  12220. case PACKED_TYPE_I8:
  12221. case PACKED_TYPE_I16:
  12222. POP_I32();
  12223. break;
  12224. case VALUE_TYPE_I64:
  12225. POP_I64();
  12226. break;
  12227. case VALUE_TYPE_F32:
  12228. POP_F32();
  12229. break;
  12230. case VALUE_TYPE_F64:
  12231. POP_F64();
  12232. break;
  12233. default:
  12234. set_error_buf(error_buf,
  12235. error_buf_size,
  12236. "unknown type");
  12237. goto fail;
  12238. }
  12239. }
  12240. }
  12241. }
  12242. /* PUSH struct obj, (ref $t) */
  12243. wasm_set_refheaptype_typeidx(
  12244. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12245. PUSH_REF(wasm_ref_type.ref_type);
  12246. break;
  12247. }
  12248. case WASM_OP_STRUCT_GET:
  12249. case WASM_OP_STRUCT_GET_S:
  12250. case WASM_OP_STRUCT_GET_U:
  12251. case WASM_OP_STRUCT_SET:
  12252. {
  12253. WASMStructType *struct_type;
  12254. WASMRefType *ref_type = NULL;
  12255. uint32 field_idx;
  12256. uint8 field_type;
  12257. read_leb_uint32(p, p_end, type_idx);
  12258. #if WASM_ENABLE_FAST_INTERP != 0
  12259. emit_uint32(loader_ctx, type_idx);
  12260. #endif
  12261. if (!check_type_index(module, module->type_count,
  12262. type_idx, error_buf,
  12263. error_buf_size)) {
  12264. goto fail;
  12265. }
  12266. if (module->types[type_idx]->type_flag
  12267. != WASM_TYPE_STRUCT) {
  12268. set_error_buf(error_buf, error_buf_size,
  12269. "unknown struct type");
  12270. goto fail;
  12271. }
  12272. struct_type = (WASMStructType *)module->types[type_idx];
  12273. read_leb_uint32(p, p_end, field_idx);
  12274. #if WASM_ENABLE_FAST_INTERP != 0
  12275. emit_uint32(loader_ctx, field_idx);
  12276. #endif
  12277. if (field_idx >= struct_type->field_count) {
  12278. set_error_buf(error_buf, error_buf_size,
  12279. "unknown struct field");
  12280. goto fail;
  12281. }
  12282. if (opcode1 == WASM_OP_STRUCT_SET
  12283. && !(struct_type->fields[field_idx].field_flags
  12284. & 1)) {
  12285. set_error_buf(error_buf, error_buf_size,
  12286. "field is immutable");
  12287. goto fail;
  12288. }
  12289. field_type = struct_type->fields[field_idx].field_type;
  12290. if (is_packed_type(field_type)) {
  12291. if (opcode1 == WASM_OP_STRUCT_GET) {
  12292. set_error_buf(error_buf, error_buf_size,
  12293. "type mismatch");
  12294. goto fail;
  12295. }
  12296. else {
  12297. field_type = VALUE_TYPE_I32;
  12298. }
  12299. }
  12300. if (wasm_is_type_multi_byte_type(field_type)) {
  12301. ref_type = wasm_reftype_map_find(
  12302. struct_type->ref_type_maps,
  12303. struct_type->ref_type_map_count, field_idx);
  12304. bh_assert(ref_type);
  12305. }
  12306. if (opcode1 == WASM_OP_STRUCT_SET) {
  12307. /* POP field */
  12308. if (wasm_is_type_multi_byte_type(field_type)) {
  12309. bh_memcpy_s(&wasm_ref_type,
  12310. (uint32)sizeof(WASMRefType),
  12311. ref_type,
  12312. wasm_reftype_struct_size(ref_type));
  12313. }
  12314. POP_REF(field_type);
  12315. /* POP struct obj, (ref null $t) */
  12316. wasm_set_refheaptype_typeidx(
  12317. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12318. POP_REF(wasm_ref_type.ref_type);
  12319. }
  12320. else {
  12321. /* POP struct obj, (ref null $t) */
  12322. wasm_set_refheaptype_typeidx(
  12323. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12324. POP_REF(wasm_ref_type.ref_type);
  12325. /* PUSH field */
  12326. if (wasm_is_type_multi_byte_type(field_type)) {
  12327. bh_memcpy_s(&wasm_ref_type,
  12328. (uint32)sizeof(WASMRefType),
  12329. ref_type,
  12330. wasm_reftype_struct_size(ref_type));
  12331. }
  12332. PUSH_REF(field_type);
  12333. }
  12334. break;
  12335. }
  12336. case WASM_OP_ARRAY_NEW:
  12337. case WASM_OP_ARRAY_NEW_DEFAULT:
  12338. case WASM_OP_ARRAY_NEW_FIXED:
  12339. case WASM_OP_ARRAY_NEW_DATA:
  12340. case WASM_OP_ARRAY_NEW_ELEM:
  12341. {
  12342. WASMArrayType *array_type;
  12343. uint8 elem_type;
  12344. uint32 u32 = 0;
  12345. read_leb_uint32(p, p_end, type_idx);
  12346. #if WASM_ENABLE_FAST_INTERP != 0
  12347. emit_uint32(loader_ctx, type_idx);
  12348. #endif
  12349. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12350. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12351. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12352. read_leb_uint32(p, p_end, u32);
  12353. #if WASM_ENABLE_FAST_INTERP != 0
  12354. emit_uint32(loader_ctx, u32);
  12355. #endif
  12356. }
  12357. if (!check_array_type(module, type_idx, error_buf,
  12358. error_buf_size)) {
  12359. goto fail;
  12360. }
  12361. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12362. /* length */
  12363. POP_I32();
  12364. }
  12365. array_type = (WASMArrayType *)module->types[type_idx];
  12366. elem_type = array_type->elem_type;
  12367. if (opcode1 == WASM_OP_ARRAY_NEW
  12368. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12369. if (wasm_is_type_multi_byte_type(elem_type)) {
  12370. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12371. array_type->elem_ref_type,
  12372. wasm_reftype_struct_size(
  12373. array_type->elem_ref_type));
  12374. }
  12375. if (is_packed_type(elem_type)) {
  12376. elem_type = VALUE_TYPE_I32;
  12377. }
  12378. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12379. uint32 N = u32;
  12380. for (i = 0; i < N; i++) {
  12381. if (wasm_is_type_multi_byte_type(
  12382. elem_type)) {
  12383. bh_memcpy_s(
  12384. &wasm_ref_type, sizeof(WASMRefType),
  12385. array_type->elem_ref_type,
  12386. wasm_reftype_struct_size(
  12387. array_type->elem_ref_type));
  12388. }
  12389. POP_REF(elem_type);
  12390. }
  12391. }
  12392. else
  12393. POP_REF(elem_type);
  12394. }
  12395. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12396. /* offset of data segment */
  12397. POP_I32();
  12398. if (u32 >= module->data_seg_count) {
  12399. set_error_buf(error_buf, error_buf_size,
  12400. "unknown data segment");
  12401. goto fail;
  12402. }
  12403. if (wasm_is_type_reftype(elem_type)) {
  12404. set_error_buf(error_buf, error_buf_size,
  12405. "array elem type mismatch");
  12406. goto fail;
  12407. }
  12408. }
  12409. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12410. WASMTableSeg *table_seg =
  12411. module->table_segments + u32;
  12412. /* offset of element segment */
  12413. POP_I32();
  12414. if (u32 >= module->table_seg_count) {
  12415. set_error_buf(error_buf, error_buf_size,
  12416. "unknown element segment");
  12417. goto fail;
  12418. }
  12419. if (!wasm_reftype_is_subtype_of(
  12420. table_seg->elem_type,
  12421. table_seg->elem_ref_type, elem_type,
  12422. array_type->elem_ref_type, module->types,
  12423. module->type_count)) {
  12424. set_error_buf(error_buf, error_buf_size,
  12425. "array elem type mismatch");
  12426. goto fail;
  12427. }
  12428. }
  12429. /* PUSH array obj, (ref $t) */
  12430. wasm_set_refheaptype_typeidx(
  12431. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12432. PUSH_REF(wasm_ref_type.ref_type);
  12433. break;
  12434. }
  12435. case WASM_OP_ARRAY_GET:
  12436. case WASM_OP_ARRAY_GET_S:
  12437. case WASM_OP_ARRAY_GET_U:
  12438. case WASM_OP_ARRAY_SET:
  12439. {
  12440. uint8 elem_type;
  12441. WASMArrayType *array_type;
  12442. WASMRefType *ref_type = NULL;
  12443. read_leb_uint32(p, p_end, type_idx);
  12444. #if WASM_ENABLE_FAST_INTERP != 0
  12445. emit_uint32(loader_ctx, type_idx);
  12446. #endif
  12447. if (!check_array_type(module, type_idx, error_buf,
  12448. error_buf_size)) {
  12449. goto fail;
  12450. }
  12451. array_type = (WASMArrayType *)module->types[type_idx];
  12452. if (opcode1 == WASM_OP_ARRAY_SET
  12453. && !(array_type->elem_flags & 1)) {
  12454. set_error_buf(error_buf, error_buf_size,
  12455. "array is immutable");
  12456. goto fail;
  12457. }
  12458. elem_type = array_type->elem_type;
  12459. if (is_packed_type(elem_type)) {
  12460. if (opcode1 != WASM_OP_ARRAY_GET_S
  12461. && opcode1 != WASM_OP_ARRAY_GET_U
  12462. && opcode1 != WASM_OP_ARRAY_SET) {
  12463. set_error_buf(error_buf, error_buf_size,
  12464. "type mismatch");
  12465. goto fail;
  12466. }
  12467. else {
  12468. elem_type = VALUE_TYPE_I32;
  12469. }
  12470. }
  12471. ref_type = array_type->elem_ref_type;
  12472. if (opcode1 == WASM_OP_ARRAY_SET) {
  12473. /* POP elem to set */
  12474. if (wasm_is_type_multi_byte_type(elem_type)) {
  12475. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12476. ref_type,
  12477. wasm_reftype_struct_size(ref_type));
  12478. }
  12479. POP_REF(elem_type);
  12480. }
  12481. /* elem idx */
  12482. POP_I32();
  12483. /* POP array obj, (ref null $t) */
  12484. wasm_set_refheaptype_typeidx(
  12485. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12486. POP_REF(wasm_ref_type.ref_type);
  12487. if (opcode1 != WASM_OP_ARRAY_SET) {
  12488. /* PUSH elem */
  12489. if (wasm_is_type_multi_byte_type(elem_type)) {
  12490. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12491. ref_type,
  12492. wasm_reftype_struct_size(ref_type));
  12493. }
  12494. PUSH_REF(elem_type);
  12495. }
  12496. break;
  12497. }
  12498. case WASM_OP_ARRAY_LEN:
  12499. {
  12500. POP_REF(REF_TYPE_ARRAYREF);
  12501. /* length */
  12502. PUSH_I32();
  12503. break;
  12504. }
  12505. case WASM_OP_ARRAY_FILL:
  12506. {
  12507. WASMArrayType *array_type;
  12508. uint8 elem_type;
  12509. /* typeidx */
  12510. read_leb_uint32(p, p_end, type_idx);
  12511. #if WASM_ENABLE_FAST_INTERP != 0
  12512. emit_uint32(loader_ctx, type_idx);
  12513. #endif
  12514. if (!check_array_type(module, type_idx, error_buf,
  12515. error_buf_size)) {
  12516. goto fail;
  12517. }
  12518. array_type = (WASMArrayType *)module->types[type_idx];
  12519. if (!(array_type->elem_flags & 1)) {
  12520. set_error_buf(error_buf, error_buf_size,
  12521. "array is immutable");
  12522. goto fail;
  12523. }
  12524. elem_type = array_type->elem_type;
  12525. if (is_packed_type(elem_type)) {
  12526. elem_type = VALUE_TYPE_I32;
  12527. }
  12528. POP_I32(); /* length */
  12529. #if WASM_ENABLE_FAST_INTERP != 0
  12530. POP_OFFSET_TYPE(elem_type);
  12531. #endif
  12532. POP_TYPE(elem_type);
  12533. POP_I32(); /* start */
  12534. /* POP array obj, (ref null $t) */
  12535. wasm_set_refheaptype_typeidx(
  12536. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12537. POP_REF(wasm_ref_type.ref_type);
  12538. break;
  12539. }
  12540. case WASM_OP_ARRAY_COPY:
  12541. {
  12542. uint32 src_type_idx;
  12543. uint8 src_elem_type, dst_elem_type;
  12544. WASMRefType src_ref_type = { 0 },
  12545. *src_elem_ref_type = NULL;
  12546. WASMRefType dst_ref_type = { 0 },
  12547. *dst_elem_ref_type = NULL;
  12548. WASMArrayType *array_type;
  12549. /* typeidx1 */
  12550. read_leb_uint32(p, p_end, type_idx);
  12551. #if WASM_ENABLE_FAST_INTERP != 0
  12552. emit_uint32(loader_ctx, type_idx);
  12553. #endif
  12554. /* typeidx2 */
  12555. read_leb_uint32(p, p_end, src_type_idx);
  12556. #if WASM_ENABLE_FAST_INTERP != 0
  12557. emit_uint32(loader_ctx, src_type_idx);
  12558. #endif
  12559. if (!check_array_type(module, type_idx, error_buf,
  12560. error_buf_size)) {
  12561. goto fail;
  12562. }
  12563. if (!check_array_type(module, src_type_idx, error_buf,
  12564. error_buf_size)) {
  12565. goto fail;
  12566. }
  12567. POP_I32();
  12568. POP_I32();
  12569. /* POP array obj, (ref null $t) */
  12570. wasm_set_refheaptype_typeidx(
  12571. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12572. POP_REF(wasm_ref_type.ref_type);
  12573. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12574. &wasm_ref_type,
  12575. wasm_reftype_struct_size(&wasm_ref_type));
  12576. POP_I32();
  12577. /* POP array obj, (ref null $t) */
  12578. wasm_set_refheaptype_typeidx(
  12579. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12580. POP_REF(wasm_ref_type.ref_type);
  12581. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12582. &wasm_ref_type,
  12583. wasm_reftype_struct_size(&wasm_ref_type));
  12584. array_type = (WASMArrayType *)module->types[type_idx];
  12585. if (!(array_type->elem_flags & 1)) {
  12586. set_error_buf(error_buf, error_buf_size,
  12587. "destination array is immutable");
  12588. goto fail;
  12589. }
  12590. dst_elem_type = array_type->elem_type;
  12591. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12592. dst_elem_ref_type = array_type->elem_ref_type;
  12593. }
  12594. array_type =
  12595. (WASMArrayType *)module->types[src_type_idx];
  12596. src_elem_type = array_type->elem_type;
  12597. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12598. src_elem_ref_type = array_type->elem_ref_type;
  12599. }
  12600. if (!wasm_reftype_is_subtype_of(
  12601. src_elem_type, src_elem_ref_type, dst_elem_type,
  12602. dst_elem_ref_type, module->types,
  12603. module->type_count)) {
  12604. set_error_buf(error_buf, error_buf_size,
  12605. "array types do not match");
  12606. goto fail;
  12607. }
  12608. break;
  12609. }
  12610. case WASM_OP_REF_I31:
  12611. {
  12612. POP_I32();
  12613. wasm_set_refheaptype_common(
  12614. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12615. PUSH_REF(wasm_ref_type.ref_type);
  12616. break;
  12617. }
  12618. case WASM_OP_I31_GET_S:
  12619. case WASM_OP_I31_GET_U:
  12620. {
  12621. POP_REF(REF_TYPE_I31REF);
  12622. PUSH_I32();
  12623. break;
  12624. }
  12625. case WASM_OP_REF_TEST:
  12626. case WASM_OP_REF_CAST:
  12627. case WASM_OP_REF_TEST_NULLABLE:
  12628. case WASM_OP_REF_CAST_NULLABLE:
  12629. {
  12630. uint8 type;
  12631. read_leb_int32(p, p_end, heap_type);
  12632. #if WASM_ENABLE_FAST_INTERP != 0
  12633. emit_uint32(loader_ctx, (uint32)heap_type);
  12634. #endif
  12635. if (heap_type >= 0) {
  12636. if (!check_type_index(module, module->type_count,
  12637. heap_type, error_buf,
  12638. error_buf_size)) {
  12639. goto fail;
  12640. }
  12641. }
  12642. else {
  12643. if (!wasm_is_valid_heap_type(heap_type)) {
  12644. set_error_buf(error_buf, error_buf_size,
  12645. "unknown type");
  12646. goto fail;
  12647. }
  12648. }
  12649. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12650. &wasm_ref_type, error_buf,
  12651. error_buf_size)) {
  12652. goto fail;
  12653. }
  12654. if (opcode1 == WASM_OP_REF_TEST
  12655. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12656. PUSH_I32();
  12657. else {
  12658. bool nullable =
  12659. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12660. : false;
  12661. if (heap_type >= 0 || !nullable) {
  12662. wasm_set_refheaptype_typeidx(
  12663. &wasm_ref_type.ref_ht_typeidx, nullable,
  12664. heap_type);
  12665. PUSH_REF(wasm_ref_type.ref_type);
  12666. }
  12667. else {
  12668. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12669. }
  12670. }
  12671. break;
  12672. }
  12673. case WASM_OP_BR_ON_CAST:
  12674. case WASM_OP_BR_ON_CAST_FAIL:
  12675. {
  12676. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12677. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12678. uint8 type_tmp, castflags;
  12679. uint32 depth;
  12680. int32 heap_type_dst;
  12681. bool src_nullable, dst_nullable;
  12682. CHECK_BUF(p, p_end, 1);
  12683. castflags = read_uint8(p);
  12684. #if WASM_ENABLE_FAST_INTERP != 0
  12685. /* Emit heap_type firstly */
  12686. emit_byte(loader_ctx, castflags);
  12687. #endif
  12688. p_org = p;
  12689. read_leb_uint32(p, p_end, depth);
  12690. read_leb_int32(p, p_end, heap_type);
  12691. #if WASM_ENABLE_FAST_INTERP != 0
  12692. /* Emit heap_type firstly */
  12693. emit_uint32(loader_ctx, (uint32)heap_type);
  12694. #endif
  12695. read_leb_int32(p, p_end, heap_type_dst);
  12696. #if WASM_ENABLE_FAST_INTERP != 0
  12697. /* Emit heap_type firstly */
  12698. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12699. #endif
  12700. (void)depth;
  12701. /*
  12702. * castflags should be 0~3:
  12703. * 0: (non-null, non-null)
  12704. * 1: (null, non-null)
  12705. * 2: (non-null, null)
  12706. * 3: (null, null)
  12707. */
  12708. if (castflags > 3) {
  12709. set_error_buf(error_buf, error_buf_size,
  12710. "invalid castflags");
  12711. break;
  12712. }
  12713. src_nullable =
  12714. (castflags == 1) || (castflags == 3) ? true : false;
  12715. dst_nullable =
  12716. (castflags == 2) || (castflags == 3) ? true : false;
  12717. /* Pop and backup the stack top's ref type */
  12718. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12719. &ref_type_tmp, error_buf,
  12720. error_buf_size)) {
  12721. goto fail;
  12722. }
  12723. /* The reference type rt1 must be valid */
  12724. if (!init_ref_type(module, &ref_type1, src_nullable,
  12725. heap_type, error_buf,
  12726. error_buf_size)) {
  12727. goto fail;
  12728. }
  12729. /* The reference type rt2 must be valid. */
  12730. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12731. heap_type_dst, error_buf,
  12732. error_buf_size)) {
  12733. goto fail;
  12734. }
  12735. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12736. &ref_type2);
  12737. /* The reference type rt2 must match rt1. */
  12738. if (!wasm_reftype_is_subtype_of(
  12739. ref_type2.ref_type, &ref_type2,
  12740. ref_type1.ref_type, &ref_type1, module->types,
  12741. module->type_count)) {
  12742. set_error_buf(error_buf, error_buf_size,
  12743. "type mismatch");
  12744. goto fail;
  12745. }
  12746. p = p_org;
  12747. /* Push ref type casted for branch block check */
  12748. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12749. /* The reference type rt2 must match rt′. */
  12750. type_tmp = ref_type2.ref_type;
  12751. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12752. bh_memcpy_s(
  12753. &wasm_ref_type,
  12754. wasm_reftype_struct_size(&ref_type2),
  12755. &ref_type2,
  12756. wasm_reftype_struct_size(&ref_type2));
  12757. }
  12758. }
  12759. else {
  12760. /* The reference type rt′1 must match rt′. */
  12761. type_tmp = ref_type_diff.ref_type;
  12762. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12763. bh_memcpy_s(
  12764. &wasm_ref_type,
  12765. wasm_reftype_struct_size(&ref_type_diff),
  12766. &ref_type_diff,
  12767. wasm_reftype_struct_size(&ref_type_diff));
  12768. }
  12769. }
  12770. PUSH_REF(type_tmp);
  12771. if (!(frame_csp_tmp = check_branch_block(
  12772. loader_ctx, &p, p_end, opcode, error_buf,
  12773. error_buf_size))) {
  12774. goto fail;
  12775. }
  12776. /* Ignore heap_types */
  12777. skip_leb_uint32(p, p_end);
  12778. skip_leb_uint32(p, p_end);
  12779. /* Restore the original stack top's ref type */
  12780. POP_REF(type_tmp);
  12781. #if WASM_ENABLE_FAST_INTERP != 0
  12782. /* Erase the opnd offset emitted by POP_REF() */
  12783. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12784. #endif
  12785. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12786. type_tmp = ref_type_diff.ref_type;
  12787. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12788. bh_memcpy_s(
  12789. &wasm_ref_type,
  12790. wasm_reftype_struct_size(&ref_type_diff),
  12791. &ref_type_diff,
  12792. wasm_reftype_struct_size(&ref_type_diff));
  12793. }
  12794. }
  12795. else {
  12796. type_tmp = ref_type2.ref_type;
  12797. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12798. bh_memcpy_s(
  12799. &wasm_ref_type,
  12800. wasm_reftype_struct_size(&ref_type2),
  12801. &ref_type2,
  12802. wasm_reftype_struct_size(&ref_type2));
  12803. }
  12804. }
  12805. PUSH_REF(type_tmp);
  12806. #if WASM_ENABLE_FAST_INTERP != 0
  12807. /* Erase the opnd offset emitted by PUSH_REF() */
  12808. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12809. #endif
  12810. break;
  12811. }
  12812. case WASM_OP_ANY_CONVERT_EXTERN:
  12813. {
  12814. uint8 type;
  12815. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12816. &wasm_ref_type, error_buf,
  12817. error_buf_size)) {
  12818. goto fail;
  12819. }
  12820. if (!(type == REF_TYPE_EXTERNREF
  12821. || (type == REF_TYPE_HT_NON_NULLABLE
  12822. && wasm_ref_type.ref_ht_common.heap_type
  12823. == HEAP_TYPE_EXTERN)
  12824. || type == VALUE_TYPE_ANY)) {
  12825. set_error_buf(error_buf, error_buf_size,
  12826. "type mismatch");
  12827. goto fail;
  12828. }
  12829. if (type == REF_TYPE_EXTERNREF)
  12830. type = REF_TYPE_ANYREF;
  12831. else {
  12832. wasm_ref_type.ref_ht_common.heap_type =
  12833. HEAP_TYPE_ANY;
  12834. }
  12835. PUSH_REF(type);
  12836. break;
  12837. }
  12838. case WASM_OP_EXTERN_CONVERT_ANY:
  12839. {
  12840. uint8 type;
  12841. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12842. &wasm_ref_type, error_buf,
  12843. error_buf_size)) {
  12844. goto fail;
  12845. }
  12846. if (type == REF_TYPE_EXTERNREF
  12847. || ((type == REF_TYPE_HT_NULLABLE
  12848. || type == REF_TYPE_HT_NON_NULLABLE)
  12849. && wasm_ref_type.ref_ht_common.heap_type
  12850. == HEAP_TYPE_EXTERN)) {
  12851. set_error_buf(error_buf, error_buf_size,
  12852. "type mismatch");
  12853. goto fail;
  12854. }
  12855. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12856. /* push (ref null extern) */
  12857. type = REF_TYPE_EXTERNREF;
  12858. }
  12859. else {
  12860. /* push (ref extern) */
  12861. type = REF_TYPE_HT_NON_NULLABLE;
  12862. wasm_set_refheaptype_common(
  12863. &wasm_ref_type.ref_ht_common, false,
  12864. HEAP_TYPE_EXTERN);
  12865. }
  12866. PUSH_REF(type);
  12867. break;
  12868. }
  12869. #if WASM_ENABLE_STRINGREF != 0
  12870. case WASM_OP_STRING_NEW_UTF8:
  12871. case WASM_OP_STRING_NEW_WTF16:
  12872. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12873. case WASM_OP_STRING_NEW_WTF8:
  12874. {
  12875. uint32 memidx;
  12876. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12877. func->has_memory_operations = true;
  12878. #endif
  12879. read_leb_uint32(p, p_end, memidx);
  12880. #if WASM_ENABLE_FAST_INTERP != 0
  12881. emit_uint32(loader_ctx, (uint32)memidx);
  12882. #endif
  12883. POP_I32();
  12884. POP_I32();
  12885. PUSH_REF(REF_TYPE_STRINGREF);
  12886. (void)memidx;
  12887. break;
  12888. }
  12889. case WASM_OP_STRING_CONST:
  12890. {
  12891. uint32 contents;
  12892. read_leb_uint32(p, p_end, contents);
  12893. #if WASM_ENABLE_FAST_INTERP != 0
  12894. emit_uint32(loader_ctx, (uint32)contents);
  12895. #endif
  12896. PUSH_REF(REF_TYPE_STRINGREF);
  12897. (void)contents;
  12898. break;
  12899. }
  12900. case WASM_OP_STRING_MEASURE_UTF8:
  12901. case WASM_OP_STRING_MEASURE_WTF8:
  12902. case WASM_OP_STRING_MEASURE_WTF16:
  12903. {
  12904. POP_STRINGREF();
  12905. PUSH_I32();
  12906. break;
  12907. }
  12908. case WASM_OP_STRING_ENCODE_UTF8:
  12909. case WASM_OP_STRING_ENCODE_WTF16:
  12910. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  12911. case WASM_OP_STRING_ENCODE_WTF8:
  12912. {
  12913. uint32 memidx;
  12914. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12915. func->has_memory_operations = true;
  12916. #endif
  12917. read_leb_uint32(p, p_end, memidx);
  12918. #if WASM_ENABLE_FAST_INTERP != 0
  12919. emit_uint32(loader_ctx, (uint32)memidx);
  12920. #endif
  12921. POP_I32();
  12922. POP_STRINGREF();
  12923. PUSH_I32();
  12924. (void)memidx;
  12925. break;
  12926. }
  12927. case WASM_OP_STRING_CONCAT:
  12928. {
  12929. POP_STRINGREF();
  12930. POP_STRINGREF();
  12931. PUSH_REF(REF_TYPE_STRINGREF);
  12932. break;
  12933. }
  12934. case WASM_OP_STRING_EQ:
  12935. {
  12936. POP_STRINGREF();
  12937. POP_STRINGREF();
  12938. PUSH_I32();
  12939. break;
  12940. }
  12941. case WASM_OP_STRING_IS_USV_SEQUENCE:
  12942. {
  12943. POP_STRINGREF();
  12944. PUSH_I32();
  12945. break;
  12946. }
  12947. case WASM_OP_STRING_AS_WTF8:
  12948. {
  12949. POP_STRINGREF();
  12950. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  12951. break;
  12952. }
  12953. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  12954. {
  12955. POP_I32();
  12956. POP_I32();
  12957. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12958. PUSH_I32();
  12959. break;
  12960. }
  12961. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  12962. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  12963. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  12964. {
  12965. uint32 memidx;
  12966. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12967. func->has_memory_operations = true;
  12968. #endif
  12969. read_leb_uint32(p, p_end, memidx);
  12970. #if WASM_ENABLE_FAST_INTERP != 0
  12971. emit_uint32(loader_ctx, (uint32)memidx);
  12972. #endif
  12973. POP_I32();
  12974. POP_I32();
  12975. POP_I32();
  12976. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12977. PUSH_I32();
  12978. PUSH_I32();
  12979. (void)memidx;
  12980. break;
  12981. }
  12982. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  12983. {
  12984. POP_I32();
  12985. POP_I32();
  12986. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12987. PUSH_REF(REF_TYPE_STRINGREF);
  12988. break;
  12989. }
  12990. case WASM_OP_STRING_AS_WTF16:
  12991. {
  12992. POP_STRINGREF();
  12993. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  12994. break;
  12995. }
  12996. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  12997. {
  12998. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12999. PUSH_I32();
  13000. break;
  13001. }
  13002. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13003. {
  13004. POP_I32();
  13005. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13006. PUSH_I32();
  13007. break;
  13008. }
  13009. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13010. {
  13011. uint32 memidx;
  13012. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13013. func->has_memory_operations = true;
  13014. #endif
  13015. read_leb_uint32(p, p_end, memidx);
  13016. #if WASM_ENABLE_FAST_INTERP != 0
  13017. emit_uint32(loader_ctx, (uint32)memidx);
  13018. #endif
  13019. POP_I32();
  13020. POP_I32();
  13021. POP_I32();
  13022. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13023. PUSH_I32();
  13024. (void)memidx;
  13025. break;
  13026. }
  13027. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13028. {
  13029. POP_I32();
  13030. POP_I32();
  13031. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13032. PUSH_REF(REF_TYPE_STRINGREF);
  13033. break;
  13034. }
  13035. case WASM_OP_STRING_AS_ITER:
  13036. {
  13037. POP_STRINGREF();
  13038. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13039. break;
  13040. }
  13041. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13042. {
  13043. POP_REF(REF_TYPE_STRINGVIEWITER);
  13044. PUSH_I32();
  13045. break;
  13046. }
  13047. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13048. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13049. {
  13050. POP_I32();
  13051. POP_REF(REF_TYPE_STRINGVIEWITER);
  13052. PUSH_I32();
  13053. break;
  13054. }
  13055. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13056. {
  13057. POP_I32();
  13058. POP_REF(REF_TYPE_STRINGVIEWITER);
  13059. PUSH_REF(REF_TYPE_STRINGREF);
  13060. break;
  13061. }
  13062. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13063. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13064. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13065. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13066. {
  13067. POP_I32();
  13068. POP_I32();
  13069. POP_REF(REF_TYPE_ARRAYREF);
  13070. PUSH_REF(REF_TYPE_STRINGREF);
  13071. break;
  13072. }
  13073. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13074. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13075. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13076. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13077. {
  13078. POP_I32();
  13079. POP_REF(REF_TYPE_ARRAYREF);
  13080. POP_STRINGREF();
  13081. PUSH_I32();
  13082. break;
  13083. }
  13084. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13085. default:
  13086. set_error_buf_v(error_buf, error_buf_size,
  13087. "%s %02x %02x", "unsupported opcode",
  13088. 0xfb, opcode1);
  13089. goto fail;
  13090. }
  13091. break;
  13092. }
  13093. #endif /* end of WASM_ENABLE_GC != 0 */
  13094. case WASM_OP_MISC_PREFIX:
  13095. {
  13096. uint32 opcode1;
  13097. read_leb_uint32(p, p_end, opcode1);
  13098. #if WASM_ENABLE_FAST_INTERP != 0
  13099. emit_byte(loader_ctx, ((uint8)opcode1));
  13100. #endif
  13101. switch (opcode1) {
  13102. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13103. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13104. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13105. break;
  13106. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13107. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13108. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13109. break;
  13110. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13111. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13112. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13113. break;
  13114. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13115. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13116. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13117. break;
  13118. #if WASM_ENABLE_BULK_MEMORY != 0
  13119. case WASM_OP_MEMORY_INIT:
  13120. {
  13121. read_leb_uint32(p, p_end, data_seg_idx);
  13122. #if WASM_ENABLE_FAST_INTERP != 0
  13123. emit_uint32(loader_ctx, data_seg_idx);
  13124. #endif
  13125. if (module->import_memory_count == 0
  13126. && module->memory_count == 0)
  13127. goto fail_unknown_memory;
  13128. read_leb_uint32(p, p_end, memidx);
  13129. check_memidx(module, memidx);
  13130. if (data_seg_idx >= module->data_seg_count) {
  13131. set_error_buf_v(error_buf, error_buf_size,
  13132. "unknown data segment %d",
  13133. data_seg_idx);
  13134. goto fail;
  13135. }
  13136. if (module->data_seg_count1 == 0)
  13137. goto fail_data_cnt_sec_require;
  13138. POP_I32();
  13139. POP_I32();
  13140. POP_MEM_OFFSET();
  13141. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13142. func->has_memory_operations = true;
  13143. #endif
  13144. #if WASM_ENABLE_WAMR_COMPILER != 0
  13145. module->is_bulk_memory_used = true;
  13146. #endif
  13147. break;
  13148. }
  13149. case WASM_OP_DATA_DROP:
  13150. {
  13151. read_leb_uint32(p, p_end, data_seg_idx);
  13152. #if WASM_ENABLE_FAST_INTERP != 0
  13153. emit_uint32(loader_ctx, data_seg_idx);
  13154. #endif
  13155. if (data_seg_idx >= module->data_seg_count) {
  13156. set_error_buf(error_buf, error_buf_size,
  13157. "unknown data segment");
  13158. goto fail;
  13159. }
  13160. if (module->data_seg_count1 == 0)
  13161. goto fail_data_cnt_sec_require;
  13162. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13163. func->has_memory_operations = true;
  13164. #endif
  13165. #if WASM_ENABLE_WAMR_COMPILER != 0
  13166. module->is_bulk_memory_used = true;
  13167. #endif
  13168. break;
  13169. }
  13170. case WASM_OP_MEMORY_COPY:
  13171. {
  13172. CHECK_BUF(p, p_end, sizeof(int16));
  13173. /* check both src and dst memory index */
  13174. read_leb_uint32(p, p_end, memidx);
  13175. check_memidx(module, memidx);
  13176. read_leb_uint32(p, p_end, memidx);
  13177. check_memidx(module, memidx);
  13178. if (module->import_memory_count == 0
  13179. && module->memory_count == 0)
  13180. goto fail_unknown_memory;
  13181. POP_MEM_OFFSET();
  13182. POP_MEM_OFFSET();
  13183. POP_MEM_OFFSET();
  13184. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13185. func->has_memory_operations = true;
  13186. #endif
  13187. #if WASM_ENABLE_WAMR_COMPILER != 0
  13188. module->is_bulk_memory_used = true;
  13189. #endif
  13190. break;
  13191. }
  13192. case WASM_OP_MEMORY_FILL:
  13193. {
  13194. read_leb_uint32(p, p_end, memidx);
  13195. check_memidx(module, memidx);
  13196. if (module->import_memory_count == 0
  13197. && module->memory_count == 0) {
  13198. goto fail_unknown_memory;
  13199. }
  13200. POP_MEM_OFFSET();
  13201. POP_I32();
  13202. POP_MEM_OFFSET();
  13203. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13204. func->has_memory_operations = true;
  13205. #endif
  13206. #if WASM_ENABLE_WAMR_COMPILER != 0
  13207. module->is_bulk_memory_used = true;
  13208. #endif
  13209. break;
  13210. }
  13211. fail_unknown_memory:
  13212. set_error_buf(error_buf, error_buf_size,
  13213. "unknown memory 0");
  13214. goto fail;
  13215. fail_data_cnt_sec_require:
  13216. set_error_buf(error_buf, error_buf_size,
  13217. "data count section required");
  13218. goto fail;
  13219. #endif /* WASM_ENABLE_BULK_MEMORY */
  13220. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13221. case WASM_OP_TABLE_INIT:
  13222. {
  13223. uint8 seg_type = 0, tbl_type = 0;
  13224. #if WASM_ENABLE_GC != 0
  13225. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13226. #endif
  13227. read_leb_uint32(p, p_end, table_seg_idx);
  13228. read_leb_uint32(p, p_end, table_idx);
  13229. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13230. #if WASM_ENABLE_GC != 0
  13231. (void **)&tbl_ref_type,
  13232. #else
  13233. NULL,
  13234. #endif
  13235. error_buf, error_buf_size))
  13236. goto fail;
  13237. if (!get_table_seg_elem_type(module, table_seg_idx,
  13238. &seg_type,
  13239. #if WASM_ENABLE_GC != 0
  13240. (void **)&seg_ref_type,
  13241. #else
  13242. NULL,
  13243. #endif
  13244. error_buf, error_buf_size))
  13245. goto fail;
  13246. #if WASM_ENABLE_GC == 0
  13247. if (seg_type != tbl_type) {
  13248. set_error_buf(error_buf, error_buf_size,
  13249. "type mismatch");
  13250. goto fail;
  13251. }
  13252. #else
  13253. if (!wasm_reftype_is_subtype_of(
  13254. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13255. module->types, module->type_count)) {
  13256. set_error_buf(error_buf, error_buf_size,
  13257. "type mismatch");
  13258. goto fail;
  13259. }
  13260. #endif
  13261. #if WASM_ENABLE_FAST_INTERP != 0
  13262. emit_uint32(loader_ctx, table_seg_idx);
  13263. emit_uint32(loader_ctx, table_idx);
  13264. #endif
  13265. POP_I32();
  13266. POP_I32();
  13267. POP_I32();
  13268. #if WASM_ENABLE_WAMR_COMPILER != 0
  13269. module->is_ref_types_used = true;
  13270. #endif
  13271. break;
  13272. }
  13273. case WASM_OP_ELEM_DROP:
  13274. {
  13275. read_leb_uint32(p, p_end, table_seg_idx);
  13276. if (!get_table_seg_elem_type(module, table_seg_idx,
  13277. NULL, NULL, error_buf,
  13278. error_buf_size))
  13279. goto fail;
  13280. #if WASM_ENABLE_FAST_INTERP != 0
  13281. emit_uint32(loader_ctx, table_seg_idx);
  13282. #endif
  13283. #if WASM_ENABLE_WAMR_COMPILER != 0
  13284. module->is_ref_types_used = true;
  13285. #endif
  13286. break;
  13287. }
  13288. case WASM_OP_TABLE_COPY:
  13289. {
  13290. uint8 src_type, dst_type;
  13291. #if WASM_ENABLE_GC != 0
  13292. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13293. #endif
  13294. uint32 src_tbl_idx, dst_tbl_idx;
  13295. read_leb_uint32(p, p_end, dst_tbl_idx);
  13296. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13297. #if WASM_ENABLE_GC != 0
  13298. (void **)&dst_ref_type,
  13299. #else
  13300. NULL,
  13301. #endif
  13302. error_buf, error_buf_size))
  13303. goto fail;
  13304. read_leb_uint32(p, p_end, src_tbl_idx);
  13305. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13306. #if WASM_ENABLE_GC != 0
  13307. (void **)&src_ref_type,
  13308. #else
  13309. NULL,
  13310. #endif
  13311. error_buf, error_buf_size))
  13312. goto fail;
  13313. #if WASM_ENABLE_GC == 0
  13314. if (src_type != dst_type) {
  13315. set_error_buf(error_buf, error_buf_size,
  13316. "type mismatch");
  13317. goto fail;
  13318. }
  13319. #else
  13320. if (!wasm_reftype_is_subtype_of(
  13321. src_type, src_ref_type, dst_type, dst_ref_type,
  13322. module->types, module->type_count)) {
  13323. set_error_buf(error_buf, error_buf_size,
  13324. "type mismatch");
  13325. goto fail;
  13326. }
  13327. #endif
  13328. #if WASM_ENABLE_FAST_INTERP != 0
  13329. emit_uint32(loader_ctx, dst_tbl_idx);
  13330. emit_uint32(loader_ctx, src_tbl_idx);
  13331. #endif
  13332. POP_I32();
  13333. POP_I32();
  13334. POP_I32();
  13335. #if WASM_ENABLE_WAMR_COMPILER != 0
  13336. module->is_ref_types_used = true;
  13337. #endif
  13338. break;
  13339. }
  13340. case WASM_OP_TABLE_SIZE:
  13341. {
  13342. read_leb_uint32(p, p_end, table_idx);
  13343. /* TODO: shall we create a new function to check
  13344. table idx instead of using below function? */
  13345. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13346. error_buf, error_buf_size))
  13347. goto fail;
  13348. #if WASM_ENABLE_FAST_INTERP != 0
  13349. emit_uint32(loader_ctx, table_idx);
  13350. #endif
  13351. PUSH_I32();
  13352. #if WASM_ENABLE_WAMR_COMPILER != 0
  13353. module->is_ref_types_used = true;
  13354. #endif
  13355. break;
  13356. }
  13357. case WASM_OP_TABLE_GROW:
  13358. case WASM_OP_TABLE_FILL:
  13359. {
  13360. uint8 decl_type;
  13361. #if WASM_ENABLE_GC != 0
  13362. WASMRefType *ref_type = NULL;
  13363. #endif
  13364. read_leb_uint32(p, p_end, table_idx);
  13365. if (!get_table_elem_type(module, table_idx, &decl_type,
  13366. #if WASM_ENABLE_GC != 0
  13367. (void **)&ref_type,
  13368. #else
  13369. NULL,
  13370. #endif
  13371. error_buf, error_buf_size))
  13372. goto fail;
  13373. #if WASM_ENABLE_GC != 0
  13374. if (wasm_is_type_multi_byte_type(decl_type)) {
  13375. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13376. ref_type,
  13377. wasm_reftype_struct_size(ref_type));
  13378. }
  13379. #endif
  13380. if (opcode1 == WASM_OP_TABLE_GROW) {
  13381. if (table_idx < module->import_table_count) {
  13382. module->import_tables[table_idx]
  13383. .u.table.table_type.possible_grow = true;
  13384. }
  13385. else {
  13386. module
  13387. ->tables[table_idx
  13388. - module->import_table_count]
  13389. .table_type.possible_grow = true;
  13390. }
  13391. }
  13392. #if WASM_ENABLE_FAST_INTERP != 0
  13393. emit_uint32(loader_ctx, table_idx);
  13394. #endif
  13395. POP_I32();
  13396. #if WASM_ENABLE_FAST_INTERP != 0
  13397. POP_OFFSET_TYPE(decl_type);
  13398. #endif
  13399. POP_TYPE(decl_type);
  13400. if (opcode1 == WASM_OP_TABLE_GROW)
  13401. PUSH_I32();
  13402. else
  13403. POP_I32();
  13404. #if WASM_ENABLE_WAMR_COMPILER != 0
  13405. module->is_ref_types_used = true;
  13406. #endif
  13407. break;
  13408. }
  13409. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13410. default:
  13411. set_error_buf_v(error_buf, error_buf_size,
  13412. "%s %02x %02x", "unsupported opcode",
  13413. 0xfc, opcode1);
  13414. goto fail;
  13415. }
  13416. break;
  13417. }
  13418. #if WASM_ENABLE_SIMD != 0
  13419. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13420. case WASM_OP_SIMD_PREFIX:
  13421. {
  13422. uint32 opcode1;
  13423. #if WASM_ENABLE_WAMR_COMPILER != 0
  13424. /* Mark the SIMD instruction is used in this module */
  13425. module->is_simd_used = true;
  13426. #endif
  13427. read_leb_uint32(p, p_end, opcode1);
  13428. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13429. */
  13430. switch (opcode1) {
  13431. /* memory instruction */
  13432. case SIMD_v128_load:
  13433. case SIMD_v128_load8x8_s:
  13434. case SIMD_v128_load8x8_u:
  13435. case SIMD_v128_load16x4_s:
  13436. case SIMD_v128_load16x4_u:
  13437. case SIMD_v128_load32x2_s:
  13438. case SIMD_v128_load32x2_u:
  13439. case SIMD_v128_load8_splat:
  13440. case SIMD_v128_load16_splat:
  13441. case SIMD_v128_load32_splat:
  13442. case SIMD_v128_load64_splat:
  13443. {
  13444. CHECK_MEMORY();
  13445. read_leb_uint32(p, p_end, align); /* align */
  13446. if (!check_simd_memory_access_align(
  13447. opcode1, align, error_buf, error_buf_size)) {
  13448. goto fail;
  13449. }
  13450. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13451. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13452. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13453. func->has_memory_operations = true;
  13454. #endif
  13455. break;
  13456. }
  13457. case SIMD_v128_store:
  13458. {
  13459. CHECK_MEMORY();
  13460. read_leb_uint32(p, p_end, align); /* align */
  13461. if (!check_simd_memory_access_align(
  13462. opcode1, align, error_buf, error_buf_size)) {
  13463. goto fail;
  13464. }
  13465. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13466. POP_V128();
  13467. POP_MEM_OFFSET();
  13468. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13469. func->has_memory_operations = true;
  13470. #endif
  13471. break;
  13472. }
  13473. /* basic operation */
  13474. case SIMD_v128_const:
  13475. {
  13476. CHECK_BUF1(p, p_end, 16);
  13477. p += 16;
  13478. PUSH_V128();
  13479. break;
  13480. }
  13481. case SIMD_v8x16_shuffle:
  13482. {
  13483. V128 mask;
  13484. CHECK_BUF1(p, p_end, 16);
  13485. mask = read_i8x16(p, error_buf, error_buf_size);
  13486. p += 16;
  13487. if (!check_simd_shuffle_mask(mask, error_buf,
  13488. error_buf_size)) {
  13489. goto fail;
  13490. }
  13491. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13492. break;
  13493. }
  13494. case SIMD_v8x16_swizzle:
  13495. {
  13496. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13497. break;
  13498. }
  13499. /* splat operation */
  13500. case SIMD_i8x16_splat:
  13501. case SIMD_i16x8_splat:
  13502. case SIMD_i32x4_splat:
  13503. case SIMD_i64x2_splat:
  13504. case SIMD_f32x4_splat:
  13505. case SIMD_f64x2_splat:
  13506. {
  13507. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13508. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13509. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13510. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13511. VALUE_TYPE_V128);
  13512. break;
  13513. }
  13514. /* lane operation */
  13515. case SIMD_i8x16_extract_lane_s:
  13516. case SIMD_i8x16_extract_lane_u:
  13517. case SIMD_i8x16_replace_lane:
  13518. case SIMD_i16x8_extract_lane_s:
  13519. case SIMD_i16x8_extract_lane_u:
  13520. case SIMD_i16x8_replace_lane:
  13521. case SIMD_i32x4_extract_lane:
  13522. case SIMD_i32x4_replace_lane:
  13523. case SIMD_i64x2_extract_lane:
  13524. case SIMD_i64x2_replace_lane:
  13525. case SIMD_f32x4_extract_lane:
  13526. case SIMD_f32x4_replace_lane:
  13527. case SIMD_f64x2_extract_lane:
  13528. case SIMD_f64x2_replace_lane:
  13529. {
  13530. uint8 lane;
  13531. /* clang-format off */
  13532. uint8 replace[] = {
  13533. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13534. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13535. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13536. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13537. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13538. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13539. };
  13540. uint8 push_type[] = {
  13541. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13542. VALUE_TYPE_V128,
  13543. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13544. VALUE_TYPE_V128,
  13545. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13546. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13547. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13548. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13549. };
  13550. /* clang-format on */
  13551. CHECK_BUF(p, p_end, 1);
  13552. lane = read_uint8(p);
  13553. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13554. error_buf_size)) {
  13555. goto fail;
  13556. }
  13557. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13558. if (!(wasm_loader_pop_frame_ref(
  13559. loader_ctx,
  13560. replace[opcode1
  13561. - SIMD_i8x16_extract_lane_s],
  13562. error_buf, error_buf_size)))
  13563. goto fail;
  13564. }
  13565. POP_AND_PUSH(
  13566. VALUE_TYPE_V128,
  13567. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13568. break;
  13569. }
  13570. /* i8x16 compare operation */
  13571. case SIMD_i8x16_eq:
  13572. case SIMD_i8x16_ne:
  13573. case SIMD_i8x16_lt_s:
  13574. case SIMD_i8x16_lt_u:
  13575. case SIMD_i8x16_gt_s:
  13576. case SIMD_i8x16_gt_u:
  13577. case SIMD_i8x16_le_s:
  13578. case SIMD_i8x16_le_u:
  13579. case SIMD_i8x16_ge_s:
  13580. case SIMD_i8x16_ge_u:
  13581. /* i16x8 compare operation */
  13582. case SIMD_i16x8_eq:
  13583. case SIMD_i16x8_ne:
  13584. case SIMD_i16x8_lt_s:
  13585. case SIMD_i16x8_lt_u:
  13586. case SIMD_i16x8_gt_s:
  13587. case SIMD_i16x8_gt_u:
  13588. case SIMD_i16x8_le_s:
  13589. case SIMD_i16x8_le_u:
  13590. case SIMD_i16x8_ge_s:
  13591. case SIMD_i16x8_ge_u:
  13592. /* i32x4 compare operation */
  13593. case SIMD_i32x4_eq:
  13594. case SIMD_i32x4_ne:
  13595. case SIMD_i32x4_lt_s:
  13596. case SIMD_i32x4_lt_u:
  13597. case SIMD_i32x4_gt_s:
  13598. case SIMD_i32x4_gt_u:
  13599. case SIMD_i32x4_le_s:
  13600. case SIMD_i32x4_le_u:
  13601. case SIMD_i32x4_ge_s:
  13602. case SIMD_i32x4_ge_u:
  13603. /* f32x4 compare operation */
  13604. case SIMD_f32x4_eq:
  13605. case SIMD_f32x4_ne:
  13606. case SIMD_f32x4_lt:
  13607. case SIMD_f32x4_gt:
  13608. case SIMD_f32x4_le:
  13609. case SIMD_f32x4_ge:
  13610. /* f64x2 compare operation */
  13611. case SIMD_f64x2_eq:
  13612. case SIMD_f64x2_ne:
  13613. case SIMD_f64x2_lt:
  13614. case SIMD_f64x2_gt:
  13615. case SIMD_f64x2_le:
  13616. case SIMD_f64x2_ge:
  13617. {
  13618. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13619. break;
  13620. }
  13621. /* v128 operation */
  13622. case SIMD_v128_not:
  13623. {
  13624. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13625. break;
  13626. }
  13627. case SIMD_v128_and:
  13628. case SIMD_v128_andnot:
  13629. case SIMD_v128_or:
  13630. case SIMD_v128_xor:
  13631. {
  13632. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13633. break;
  13634. }
  13635. case SIMD_v128_bitselect:
  13636. {
  13637. POP_V128();
  13638. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13639. break;
  13640. }
  13641. case SIMD_v128_any_true:
  13642. {
  13643. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13644. break;
  13645. }
  13646. /* Load Lane Operation */
  13647. case SIMD_v128_load8_lane:
  13648. case SIMD_v128_load16_lane:
  13649. case SIMD_v128_load32_lane:
  13650. case SIMD_v128_load64_lane:
  13651. case SIMD_v128_store8_lane:
  13652. case SIMD_v128_store16_lane:
  13653. case SIMD_v128_store32_lane:
  13654. case SIMD_v128_store64_lane:
  13655. {
  13656. uint8 lane;
  13657. CHECK_MEMORY();
  13658. read_leb_uint32(p, p_end, align); /* align */
  13659. if (!check_simd_memory_access_align(
  13660. opcode1, align, error_buf, error_buf_size)) {
  13661. goto fail;
  13662. }
  13663. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13664. CHECK_BUF(p, p_end, 1);
  13665. lane = read_uint8(p);
  13666. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13667. error_buf_size)) {
  13668. goto fail;
  13669. }
  13670. POP_V128();
  13671. POP_MEM_OFFSET();
  13672. if (opcode1 < SIMD_v128_store8_lane) {
  13673. PUSH_V128();
  13674. }
  13675. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13676. func->has_memory_operations = true;
  13677. #endif
  13678. break;
  13679. }
  13680. case SIMD_v128_load32_zero:
  13681. case SIMD_v128_load64_zero:
  13682. {
  13683. CHECK_MEMORY();
  13684. read_leb_uint32(p, p_end, align); /* align */
  13685. if (!check_simd_memory_access_align(
  13686. opcode1, align, error_buf, error_buf_size)) {
  13687. goto fail;
  13688. }
  13689. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13690. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13691. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13692. func->has_memory_operations = true;
  13693. #endif
  13694. break;
  13695. }
  13696. /* Float conversion */
  13697. case SIMD_f32x4_demote_f64x2_zero:
  13698. case SIMD_f64x2_promote_low_f32x4_zero:
  13699. {
  13700. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13701. break;
  13702. }
  13703. /* i8x16 Operation */
  13704. case SIMD_i8x16_abs:
  13705. case SIMD_i8x16_neg:
  13706. case SIMD_i8x16_popcnt:
  13707. {
  13708. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13709. break;
  13710. }
  13711. case SIMD_i8x16_all_true:
  13712. case SIMD_i8x16_bitmask:
  13713. {
  13714. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13715. break;
  13716. }
  13717. case SIMD_i8x16_narrow_i16x8_s:
  13718. case SIMD_i8x16_narrow_i16x8_u:
  13719. {
  13720. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13721. break;
  13722. }
  13723. case SIMD_f32x4_ceil:
  13724. case SIMD_f32x4_floor:
  13725. case SIMD_f32x4_trunc:
  13726. case SIMD_f32x4_nearest:
  13727. {
  13728. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13729. break;
  13730. }
  13731. case SIMD_i8x16_shl:
  13732. case SIMD_i8x16_shr_s:
  13733. case SIMD_i8x16_shr_u:
  13734. {
  13735. POP_I32();
  13736. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13737. break;
  13738. }
  13739. case SIMD_i8x16_add:
  13740. case SIMD_i8x16_add_sat_s:
  13741. case SIMD_i8x16_add_sat_u:
  13742. case SIMD_i8x16_sub:
  13743. case SIMD_i8x16_sub_sat_s:
  13744. case SIMD_i8x16_sub_sat_u:
  13745. {
  13746. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13747. break;
  13748. }
  13749. case SIMD_f64x2_ceil:
  13750. case SIMD_f64x2_floor:
  13751. {
  13752. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13753. break;
  13754. }
  13755. case SIMD_i8x16_min_s:
  13756. case SIMD_i8x16_min_u:
  13757. case SIMD_i8x16_max_s:
  13758. case SIMD_i8x16_max_u:
  13759. {
  13760. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13761. break;
  13762. }
  13763. case SIMD_f64x2_trunc:
  13764. {
  13765. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13766. break;
  13767. }
  13768. case SIMD_i8x16_avgr_u:
  13769. {
  13770. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13771. break;
  13772. }
  13773. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13774. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13775. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13776. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13777. /* i16x8 operation */
  13778. case SIMD_i16x8_abs:
  13779. case SIMD_i16x8_neg:
  13780. {
  13781. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13782. break;
  13783. }
  13784. case SIMD_i16x8_q15mulr_sat_s:
  13785. {
  13786. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13787. break;
  13788. }
  13789. case SIMD_i16x8_all_true:
  13790. case SIMD_i16x8_bitmask:
  13791. {
  13792. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13793. break;
  13794. }
  13795. case SIMD_i16x8_narrow_i32x4_s:
  13796. case SIMD_i16x8_narrow_i32x4_u:
  13797. {
  13798. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13799. break;
  13800. }
  13801. case SIMD_i16x8_extend_low_i8x16_s:
  13802. case SIMD_i16x8_extend_high_i8x16_s:
  13803. case SIMD_i16x8_extend_low_i8x16_u:
  13804. case SIMD_i16x8_extend_high_i8x16_u:
  13805. {
  13806. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13807. break;
  13808. }
  13809. case SIMD_i16x8_shl:
  13810. case SIMD_i16x8_shr_s:
  13811. case SIMD_i16x8_shr_u:
  13812. {
  13813. POP_I32();
  13814. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13815. break;
  13816. }
  13817. case SIMD_i16x8_add:
  13818. case SIMD_i16x8_add_sat_s:
  13819. case SIMD_i16x8_add_sat_u:
  13820. case SIMD_i16x8_sub:
  13821. case SIMD_i16x8_sub_sat_s:
  13822. case SIMD_i16x8_sub_sat_u:
  13823. {
  13824. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13825. break;
  13826. }
  13827. case SIMD_f64x2_nearest:
  13828. {
  13829. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13830. break;
  13831. }
  13832. case SIMD_i16x8_mul:
  13833. case SIMD_i16x8_min_s:
  13834. case SIMD_i16x8_min_u:
  13835. case SIMD_i16x8_max_s:
  13836. case SIMD_i16x8_max_u:
  13837. case SIMD_i16x8_avgr_u:
  13838. case SIMD_i16x8_extmul_low_i8x16_s:
  13839. case SIMD_i16x8_extmul_high_i8x16_s:
  13840. case SIMD_i16x8_extmul_low_i8x16_u:
  13841. case SIMD_i16x8_extmul_high_i8x16_u:
  13842. {
  13843. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13844. break;
  13845. }
  13846. /* i32x4 operation */
  13847. case SIMD_i32x4_abs:
  13848. case SIMD_i32x4_neg:
  13849. {
  13850. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13851. break;
  13852. }
  13853. case SIMD_i32x4_all_true:
  13854. case SIMD_i32x4_bitmask:
  13855. {
  13856. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13857. break;
  13858. }
  13859. case SIMD_i32x4_extend_low_i16x8_s:
  13860. case SIMD_i32x4_extend_high_i16x8_s:
  13861. case SIMD_i32x4_extend_low_i16x8_u:
  13862. case SIMD_i32x4_extend_high_i16x8_u:
  13863. {
  13864. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13865. break;
  13866. }
  13867. case SIMD_i32x4_shl:
  13868. case SIMD_i32x4_shr_s:
  13869. case SIMD_i32x4_shr_u:
  13870. {
  13871. POP_I32();
  13872. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13873. break;
  13874. }
  13875. case SIMD_i32x4_add:
  13876. case SIMD_i32x4_sub:
  13877. case SIMD_i32x4_mul:
  13878. case SIMD_i32x4_min_s:
  13879. case SIMD_i32x4_min_u:
  13880. case SIMD_i32x4_max_s:
  13881. case SIMD_i32x4_max_u:
  13882. case SIMD_i32x4_dot_i16x8_s:
  13883. case SIMD_i32x4_extmul_low_i16x8_s:
  13884. case SIMD_i32x4_extmul_high_i16x8_s:
  13885. case SIMD_i32x4_extmul_low_i16x8_u:
  13886. case SIMD_i32x4_extmul_high_i16x8_u:
  13887. {
  13888. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13889. break;
  13890. }
  13891. /* i64x2 operation */
  13892. case SIMD_i64x2_abs:
  13893. case SIMD_i64x2_neg:
  13894. {
  13895. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13896. break;
  13897. }
  13898. case SIMD_i64x2_all_true:
  13899. case SIMD_i64x2_bitmask:
  13900. {
  13901. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13902. break;
  13903. }
  13904. case SIMD_i64x2_extend_low_i32x4_s:
  13905. case SIMD_i64x2_extend_high_i32x4_s:
  13906. case SIMD_i64x2_extend_low_i32x4_u:
  13907. case SIMD_i64x2_extend_high_i32x4_u:
  13908. {
  13909. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13910. break;
  13911. }
  13912. case SIMD_i64x2_shl:
  13913. case SIMD_i64x2_shr_s:
  13914. case SIMD_i64x2_shr_u:
  13915. {
  13916. POP_I32();
  13917. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13918. break;
  13919. }
  13920. case SIMD_i64x2_add:
  13921. case SIMD_i64x2_sub:
  13922. case SIMD_i64x2_mul:
  13923. case SIMD_i64x2_eq:
  13924. case SIMD_i64x2_ne:
  13925. case SIMD_i64x2_lt_s:
  13926. case SIMD_i64x2_gt_s:
  13927. case SIMD_i64x2_le_s:
  13928. case SIMD_i64x2_ge_s:
  13929. case SIMD_i64x2_extmul_low_i32x4_s:
  13930. case SIMD_i64x2_extmul_high_i32x4_s:
  13931. case SIMD_i64x2_extmul_low_i32x4_u:
  13932. case SIMD_i64x2_extmul_high_i32x4_u:
  13933. {
  13934. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13935. break;
  13936. }
  13937. /* f32x4 operation */
  13938. case SIMD_f32x4_abs:
  13939. case SIMD_f32x4_neg:
  13940. case SIMD_f32x4_sqrt:
  13941. {
  13942. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13943. break;
  13944. }
  13945. case SIMD_f32x4_add:
  13946. case SIMD_f32x4_sub:
  13947. case SIMD_f32x4_mul:
  13948. case SIMD_f32x4_div:
  13949. case SIMD_f32x4_min:
  13950. case SIMD_f32x4_max:
  13951. case SIMD_f32x4_pmin:
  13952. case SIMD_f32x4_pmax:
  13953. {
  13954. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13955. break;
  13956. }
  13957. /* f64x2 operation */
  13958. case SIMD_f64x2_abs:
  13959. case SIMD_f64x2_neg:
  13960. case SIMD_f64x2_sqrt:
  13961. {
  13962. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13963. break;
  13964. }
  13965. case SIMD_f64x2_add:
  13966. case SIMD_f64x2_sub:
  13967. case SIMD_f64x2_mul:
  13968. case SIMD_f64x2_div:
  13969. case SIMD_f64x2_min:
  13970. case SIMD_f64x2_max:
  13971. case SIMD_f64x2_pmin:
  13972. case SIMD_f64x2_pmax:
  13973. {
  13974. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13975. break;
  13976. }
  13977. case SIMD_i32x4_trunc_sat_f32x4_s:
  13978. case SIMD_i32x4_trunc_sat_f32x4_u:
  13979. case SIMD_f32x4_convert_i32x4_s:
  13980. case SIMD_f32x4_convert_i32x4_u:
  13981. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  13982. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  13983. case SIMD_f64x2_convert_low_i32x4_s:
  13984. case SIMD_f64x2_convert_low_i32x4_u:
  13985. {
  13986. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13987. break;
  13988. }
  13989. default:
  13990. {
  13991. if (error_buf != NULL) {
  13992. snprintf(error_buf, error_buf_size,
  13993. "WASM module load failed: "
  13994. "invalid opcode 0xfd %02x.",
  13995. opcode1);
  13996. }
  13997. goto fail;
  13998. }
  13999. }
  14000. break;
  14001. }
  14002. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  14003. #endif /* end of WASM_ENABLE_SIMD */
  14004. #if WASM_ENABLE_SHARED_MEMORY != 0
  14005. case WASM_OP_ATOMIC_PREFIX:
  14006. {
  14007. uint32 opcode1;
  14008. read_leb_uint32(p, p_end, opcode1);
  14009. #if WASM_ENABLE_FAST_INTERP != 0
  14010. emit_byte(loader_ctx, opcode1);
  14011. #endif
  14012. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14013. CHECK_MEMORY();
  14014. read_leb_uint32(p, p_end, align); /* align */
  14015. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14016. if (!check_memory_align_equal(opcode1, align, error_buf,
  14017. error_buf_size)) {
  14018. goto fail;
  14019. }
  14020. #if WASM_ENABLE_FAST_INTERP != 0
  14021. emit_uint32(loader_ctx, mem_offset);
  14022. #endif
  14023. }
  14024. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14025. func->has_memory_operations = true;
  14026. #endif
  14027. switch (opcode1) {
  14028. case WASM_OP_ATOMIC_NOTIFY:
  14029. POP_I32();
  14030. POP_MEM_OFFSET();
  14031. PUSH_I32();
  14032. break;
  14033. case WASM_OP_ATOMIC_WAIT32:
  14034. POP_I64();
  14035. POP_I32();
  14036. POP_MEM_OFFSET();
  14037. PUSH_I32();
  14038. break;
  14039. case WASM_OP_ATOMIC_WAIT64:
  14040. POP_I64();
  14041. POP_I64();
  14042. POP_MEM_OFFSET();
  14043. PUSH_I32();
  14044. break;
  14045. case WASM_OP_ATOMIC_FENCE:
  14046. /* reserved byte 0x00 */
  14047. if (*p++ != 0x00) {
  14048. set_error_buf(error_buf, error_buf_size,
  14049. "zero byte expected");
  14050. goto fail;
  14051. }
  14052. break;
  14053. case WASM_OP_ATOMIC_I32_LOAD:
  14054. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14055. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14056. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14057. break;
  14058. case WASM_OP_ATOMIC_I32_STORE:
  14059. case WASM_OP_ATOMIC_I32_STORE8:
  14060. case WASM_OP_ATOMIC_I32_STORE16:
  14061. POP_I32();
  14062. POP_MEM_OFFSET();
  14063. break;
  14064. case WASM_OP_ATOMIC_I64_LOAD:
  14065. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14066. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14067. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14068. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14069. break;
  14070. case WASM_OP_ATOMIC_I64_STORE:
  14071. case WASM_OP_ATOMIC_I64_STORE8:
  14072. case WASM_OP_ATOMIC_I64_STORE16:
  14073. case WASM_OP_ATOMIC_I64_STORE32:
  14074. POP_I64();
  14075. POP_MEM_OFFSET();
  14076. break;
  14077. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14078. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14079. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14080. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14081. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14082. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14083. case WASM_OP_ATOMIC_RMW_I32_AND:
  14084. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14085. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14086. case WASM_OP_ATOMIC_RMW_I32_OR:
  14087. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14088. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14089. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14090. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14091. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14092. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14093. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14094. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14095. POP_I32();
  14096. POP_MEM_OFFSET();
  14097. PUSH_I32();
  14098. break;
  14099. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14100. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14101. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14102. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14103. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14104. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14105. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14106. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14107. case WASM_OP_ATOMIC_RMW_I64_AND:
  14108. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14109. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14110. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14111. case WASM_OP_ATOMIC_RMW_I64_OR:
  14112. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14113. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14114. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14115. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14116. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14117. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14118. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14119. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14120. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14121. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14122. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14123. POP_I64();
  14124. POP_MEM_OFFSET();
  14125. PUSH_I64();
  14126. break;
  14127. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14128. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14129. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14130. POP_I32();
  14131. POP_I32();
  14132. POP_MEM_OFFSET();
  14133. PUSH_I32();
  14134. break;
  14135. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14136. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14137. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14138. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14139. POP_I64();
  14140. POP_I64();
  14141. POP_MEM_OFFSET();
  14142. PUSH_I64();
  14143. break;
  14144. default:
  14145. set_error_buf_v(error_buf, error_buf_size,
  14146. "%s %02x %02x", "unsupported opcode",
  14147. 0xfe, opcode1);
  14148. goto fail;
  14149. }
  14150. break;
  14151. }
  14152. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14153. default:
  14154. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14155. "unsupported opcode", opcode);
  14156. goto fail;
  14157. }
  14158. #if WASM_ENABLE_FAST_INTERP != 0
  14159. last_op = opcode;
  14160. #endif
  14161. }
  14162. if (loader_ctx->csp_num > 0) {
  14163. if (cur_func_idx < module->function_count - 1)
  14164. /* Function with missing end marker (between two functions) */
  14165. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  14166. else
  14167. /* Function with missing end marker
  14168. (at EOF or end of code sections) */
  14169. set_error_buf(error_buf, error_buf_size,
  14170. "unexpected end of section or function, "
  14171. "or section size mismatch");
  14172. goto fail;
  14173. }
  14174. #if WASM_ENABLE_FAST_INTERP != 0
  14175. if (loader_ctx->p_code_compiled == NULL)
  14176. goto re_scan;
  14177. func->const_cell_num = loader_ctx->const_cell_num;
  14178. if (func->const_cell_num > 0) {
  14179. int32 j;
  14180. if (!(func->consts = func_const = loader_malloc(
  14181. func->const_cell_num * 4, error_buf, error_buf_size)))
  14182. goto fail;
  14183. func_const_end = func->consts + func->const_cell_num * 4;
  14184. /* reverse the const buf */
  14185. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  14186. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  14187. if (c->value_type == VALUE_TYPE_F64
  14188. || c->value_type == VALUE_TYPE_I64) {
  14189. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14190. &(c->value.f64), (uint32)sizeof(int64));
  14191. func_const += sizeof(int64);
  14192. }
  14193. else {
  14194. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14195. &(c->value.f32), (uint32)sizeof(int32));
  14196. func_const += sizeof(int32);
  14197. }
  14198. }
  14199. }
  14200. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14201. - loader_ctx->start_dynamic_offset + 1;
  14202. #else
  14203. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14204. #endif
  14205. func->max_block_num = loader_ctx->max_csp_num;
  14206. return_value = true;
  14207. fail:
  14208. wasm_loader_ctx_destroy(loader_ctx);
  14209. (void)table_idx;
  14210. (void)table_seg_idx;
  14211. (void)data_seg_idx;
  14212. (void)i64_const;
  14213. (void)local_offset;
  14214. (void)p_org;
  14215. (void)mem_offset;
  14216. (void)align;
  14217. return return_value;
  14218. }