wasm_loader.c 585 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "wasm.h"
  9. #include "wasm_opcode.h"
  10. #include "wasm_runtime.h"
  11. #include "wasm_loader_common.h"
  12. #include "../common/wasm_native.h"
  13. #include "../common/wasm_memory.h"
  14. #if WASM_ENABLE_GC != 0
  15. #include "../common/gc/gc_type.h"
  16. #include "../common/gc/gc_object.h"
  17. #endif
  18. #if WASM_ENABLE_DEBUG_INTERP != 0
  19. #include "../libraries/debug-engine/debug_engine.h"
  20. #endif
  21. #if WASM_ENABLE_FAST_JIT != 0
  22. #include "../fast-jit/jit_compiler.h"
  23. #include "../fast-jit/jit_codecache.h"
  24. #endif
  25. #if WASM_ENABLE_JIT != 0
  26. #include "../compilation/aot_llvm.h"
  27. #endif
  28. #ifndef TRACE_WASM_LOADER
  29. #define TRACE_WASM_LOADER 0
  30. #endif
  31. /* Read a value of given type from the address pointed to by the given
  32. pointer and increase the pointer to the position just after the
  33. value being read. */
  34. #define TEMPLATE_READ_VALUE(Type, p) \
  35. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  36. #if WASM_ENABLE_MEMORY64 != 0
  37. static bool
  38. has_module_memory64(WASMModule *module)
  39. {
  40. /* TODO: multi-memories for now assuming the memory idx type is consistent
  41. * across multi-memories */
  42. if (module->import_memory_count > 0)
  43. return !!(module->import_memories[0].u.memory.mem_type.flags
  44. & MEMORY64_FLAG);
  45. else if (module->memory_count > 0)
  46. return !!(module->memories[0].flags & MEMORY64_FLAG);
  47. return false;
  48. }
  49. #endif
  50. static void
  51. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  52. {
  53. wasm_loader_set_error_buf(error_buf, error_buf_size, string, false);
  54. }
  55. #if WASM_ENABLE_MEMORY64 != 0
  56. static void
  57. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  58. {
  59. if (error_buf != NULL) {
  60. snprintf(error_buf, error_buf_size, "offset out of range");
  61. }
  62. }
  63. #endif
  64. static void
  65. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  66. {
  67. va_list args;
  68. char buf[128];
  69. if (error_buf != NULL) {
  70. va_start(args, format);
  71. vsnprintf(buf, sizeof(buf), format, args);
  72. va_end(args);
  73. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  74. }
  75. }
  76. static bool
  77. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  78. char *error_buf, uint32 error_buf_size)
  79. {
  80. if ((uintptr_t)buf + length < (uintptr_t)buf
  81. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  82. set_error_buf(error_buf, error_buf_size,
  83. "unexpected end of section or function");
  84. return false;
  85. }
  86. return true;
  87. }
  88. static bool
  89. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  90. char *error_buf, uint32 error_buf_size)
  91. {
  92. if ((uintptr_t)buf + length < (uintptr_t)buf
  93. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  94. set_error_buf(error_buf, error_buf_size, "unexpected end");
  95. return false;
  96. }
  97. return true;
  98. }
  99. #define CHECK_BUF(buf, buf_end, length) \
  100. do { \
  101. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  102. goto fail; \
  103. } \
  104. } while (0)
  105. #define CHECK_BUF1(buf, buf_end, length) \
  106. do { \
  107. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  108. goto fail; \
  109. } \
  110. } while (0)
  111. #define skip_leb(p) while (*p++ & 0x80)
  112. #define skip_leb_int64(p, p_end) skip_leb(p)
  113. #define skip_leb_uint32(p, p_end) skip_leb(p)
  114. #define skip_leb_int32(p, p_end) skip_leb(p)
  115. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  116. #define skip_leb_memidx(p, p_end) skip_leb(p)
  117. #if WASM_ENABLE_MULTI_MEMORY == 0
  118. #define skip_leb_align(p, p_end) skip_leb(p)
  119. #else
  120. /* Skip the following memidx if applicable */
  121. #define skip_leb_align(p, p_end) \
  122. do { \
  123. if (*p++ & OPT_MEMIDX_FLAG) \
  124. skip_leb_uint32(p, p_end); \
  125. } while (0)
  126. #endif
  127. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  128. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  129. #define read_leb_int64(p, p_end, res) \
  130. do { \
  131. uint64 res64; \
  132. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  133. error_buf_size)) \
  134. goto fail; \
  135. res = (int64)res64; \
  136. } while (0)
  137. #if WASM_ENABLE_MEMORY64 != 0
  138. #define read_leb_mem_offset(p, p_end, res) \
  139. do { \
  140. uint64 res64; \
  141. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  142. &res64, error_buf, error_buf_size)) { \
  143. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  144. goto fail; \
  145. } \
  146. res = (mem_offset_t)res64; \
  147. } while (0)
  148. #else
  149. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  150. #endif
  151. #define read_leb_uint32(p, p_end, res) \
  152. do { \
  153. uint64 res64; \
  154. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  155. error_buf_size)) \
  156. goto fail; \
  157. res = (uint32)res64; \
  158. } while (0)
  159. #define read_leb_int32(p, p_end, res) \
  160. do { \
  161. uint64 res64; \
  162. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  163. error_buf_size)) \
  164. goto fail; \
  165. res = (int32)res64; \
  166. } while (0)
  167. #define read_leb_memidx(p, p_end, res) read_leb_uint32(p, p_end, res)
  168. #if WASM_ENABLE_MULTI_MEMORY != 0
  169. #define check_memidx(module, memidx) \
  170. do { \
  171. if (memidx >= module->import_memory_count + module->memory_count) { \
  172. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d", \
  173. memidx); \
  174. goto fail; \
  175. } \
  176. } while (0)
  177. /* Bit 6(0x40) indicating the optional memidx, and reset bit 6 for
  178. * alignment check */
  179. #define read_leb_memarg(p, p_end, res) \
  180. do { \
  181. read_leb_uint32(p, p_end, res); \
  182. if (res & OPT_MEMIDX_FLAG) { \
  183. res &= ~OPT_MEMIDX_FLAG; \
  184. read_leb_uint32(p, p_end, memidx); /* memidx */ \
  185. check_memidx(module, memidx); \
  186. } \
  187. } while (0)
  188. #else
  189. /* reserved byte 0x00 */
  190. #define check_memidx(module, memidx) \
  191. do { \
  192. (void)module; \
  193. if (memidx != 0) { \
  194. set_error_buf(error_buf, error_buf_size, "zero byte expected"); \
  195. goto fail; \
  196. } \
  197. } while (0)
  198. #define read_leb_memarg(p, p_end, res) read_leb_uint32(p, p_end, res)
  199. #endif
  200. static char *
  201. type2str(uint8 type)
  202. {
  203. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  204. #if WASM_ENABLE_GC != 0
  205. char *type_str_ref[] = { "stringview_iter",
  206. "stringview_wtf16",
  207. "(ref null ht)",
  208. "(ref ht)",
  209. "", /* reserved */
  210. "stringview_wtf8",
  211. "stringref",
  212. "", /* reserved */
  213. "", /* reserved */
  214. "arrayref",
  215. "structref",
  216. "i32ref",
  217. "eqref",
  218. "anyref",
  219. "externref",
  220. "funcref",
  221. "nullref",
  222. "nullexternref",
  223. "nullfuncref" };
  224. #endif
  225. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  226. return type_str[type - VALUE_TYPE_V128];
  227. #if WASM_ENABLE_GC != 0
  228. else if (wasm_is_type_reftype(type))
  229. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  230. #endif
  231. else if (type == VALUE_TYPE_FUNCREF)
  232. return "funcref";
  233. else if (type == VALUE_TYPE_EXTERNREF)
  234. return "externref";
  235. else
  236. return "unknown type";
  237. }
  238. static bool
  239. is_32bit_type(uint8 type)
  240. {
  241. if (type == VALUE_TYPE_I32
  242. || type == VALUE_TYPE_F32
  243. /* the operand stack is in polymorphic state */
  244. || type == VALUE_TYPE_ANY
  245. #if WASM_ENABLE_GC != 0
  246. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  247. #elif WASM_ENABLE_REF_TYPES != 0
  248. /* For reference types, we use uint32 index to represent
  249. the funcref and externref */
  250. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  251. #endif
  252. )
  253. return true;
  254. return false;
  255. }
  256. static bool
  257. is_64bit_type(uint8 type)
  258. {
  259. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  260. #if WASM_ENABLE_GC != 0
  261. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  262. #endif
  263. )
  264. return true;
  265. return false;
  266. }
  267. #if WASM_ENABLE_GC != 0
  268. static bool
  269. is_packed_type(uint8 type)
  270. {
  271. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  272. }
  273. #endif
  274. static bool
  275. is_byte_a_type(uint8 type)
  276. {
  277. return (is_valid_value_type_for_interpreter(type)
  278. || (type == VALUE_TYPE_VOID))
  279. ? true
  280. : false;
  281. }
  282. #if WASM_ENABLE_SIMD != 0
  283. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  284. static V128
  285. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  286. {
  287. V128 result;
  288. uint8 i;
  289. for (i = 0; i != 16; ++i) {
  290. result.i8x16[i] = read_uint8(p_buf);
  291. }
  292. return result;
  293. }
  294. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  295. #endif /* end of WASM_ENABLE_SIMD */
  296. static void *
  297. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  298. {
  299. void *mem;
  300. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  301. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  302. return NULL;
  303. }
  304. memset(mem, 0, (uint32)size);
  305. return mem;
  306. }
  307. static void *
  308. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  309. uint32 error_buf_size)
  310. {
  311. uint8 *mem_new;
  312. bh_assert(size_new > size_old);
  313. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  314. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  315. memset(mem_new + size_old, 0, size_new - size_old);
  316. wasm_runtime_free(mem_old);
  317. }
  318. return mem_new;
  319. }
  320. #define MEM_REALLOC(mem, size_old, size_new) \
  321. do { \
  322. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  323. error_buf_size); \
  324. if (!mem_new) \
  325. goto fail; \
  326. mem = mem_new; \
  327. } while (0)
  328. #if WASM_ENABLE_GC != 0
  329. static bool
  330. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  331. char *error_buf, uint32 error_buf_size)
  332. {
  333. if (type_index >= type_count) {
  334. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  335. type_index);
  336. return false;
  337. }
  338. return true;
  339. }
  340. static bool
  341. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  342. uint32 error_buf_size)
  343. {
  344. if (!check_type_index(module, module->type_count, type_index, error_buf,
  345. error_buf_size)) {
  346. return false;
  347. }
  348. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  349. set_error_buf(error_buf, error_buf_size, "unkown array type");
  350. return false;
  351. }
  352. return true;
  353. }
  354. #endif
  355. static bool
  356. check_function_index(const WASMModule *module, uint32 function_index,
  357. char *error_buf, uint32 error_buf_size)
  358. {
  359. if (function_index
  360. >= module->import_function_count + module->function_count) {
  361. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  362. function_index);
  363. return false;
  364. }
  365. return true;
  366. }
  367. typedef struct InitValue {
  368. uint8 type;
  369. uint8 flag;
  370. #if WASM_ENABLE_GC != 0
  371. uint8 gc_opcode;
  372. WASMRefType ref_type;
  373. #endif
  374. WASMValue value;
  375. } InitValue;
  376. typedef struct ConstExprContext {
  377. uint32 sp;
  378. uint32 size;
  379. WASMModule *module;
  380. InitValue *stack;
  381. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  382. } ConstExprContext;
  383. static void
  384. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  385. {
  386. ctx->sp = 0;
  387. ctx->module = module;
  388. ctx->stack = ctx->data;
  389. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  390. }
  391. static bool
  392. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  393. #if WASM_ENABLE_GC != 0
  394. WASMRefType *ref_type, uint8 gc_opcode,
  395. #endif
  396. WASMValue *value, char *error_buf, uint32 error_buf_size)
  397. {
  398. InitValue *cur_value;
  399. if (ctx->sp >= ctx->size) {
  400. if (ctx->stack != ctx->data) {
  401. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  402. (ctx->size + 4) * sizeof(InitValue));
  403. }
  404. else {
  405. if (!(ctx->stack =
  406. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  407. error_buf, error_buf_size))) {
  408. goto fail;
  409. }
  410. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  411. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  412. }
  413. ctx->size += 4;
  414. }
  415. cur_value = &ctx->stack[ctx->sp++];
  416. cur_value->type = type;
  417. cur_value->flag = flag;
  418. cur_value->value = *value;
  419. #if WASM_ENABLE_GC != 0
  420. cur_value->gc_opcode = gc_opcode;
  421. if (wasm_is_type_multi_byte_type(type)) {
  422. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  423. ref_type, wasm_reftype_struct_size(ref_type));
  424. }
  425. #endif
  426. return true;
  427. fail:
  428. return false;
  429. }
  430. #if WASM_ENABLE_GC != 0
  431. static void
  432. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  433. {
  434. WASMStructNewInitValues *struct_init_values =
  435. (WASMStructNewInitValues *)data;
  436. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  437. WASMType *wasm_type;
  438. uint32 i;
  439. if (!data)
  440. return;
  441. wasm_type = module->types[struct_init_values->type_idx];
  442. /* The data can only be type of `WASMStructNewInitValues *`
  443. or `WASMArrayNewInitValues *` */
  444. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  445. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  446. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  447. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  448. WASMRefTypeMap *ref_type_map = struct_type->ref_type_maps;
  449. WASMRefType *ref_type;
  450. uint8 field_type;
  451. for (i = 0; i < struct_init_values->count; i++) {
  452. field_type = struct_type->fields[i].field_type;
  453. if (wasm_is_type_multi_byte_type(field_type))
  454. ref_type = ref_type_map->ref_type;
  455. else
  456. ref_type = NULL;
  457. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  458. REF_TYPE_STRUCTREF, NULL,
  459. module->types, module->type_count)
  460. || wasm_reftype_is_subtype_of(
  461. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  462. module->types, module->type_count)) {
  463. destroy_init_expr_data_recursive(
  464. module, struct_init_values->fields[i].data);
  465. }
  466. }
  467. }
  468. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  469. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  470. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  471. uint8 elem_type = array_type->elem_type;
  472. for (i = 0; i < array_init_values->length; i++) {
  473. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  474. REF_TYPE_STRUCTREF, NULL,
  475. module->types, module->type_count)
  476. || wasm_reftype_is_subtype_of(
  477. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  478. module->types, module->type_count)) {
  479. destroy_init_expr_data_recursive(
  480. module, array_init_values->elem_data[i].data);
  481. }
  482. }
  483. }
  484. wasm_runtime_free(data);
  485. }
  486. #endif
  487. static bool
  488. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  489. #if WASM_ENABLE_GC != 0
  490. WASMRefType *ref_type, uint8 *p_gc_opcode,
  491. #endif
  492. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  493. {
  494. InitValue *cur_value;
  495. if (ctx->sp == 0) {
  496. set_error_buf(error_buf, error_buf_size,
  497. "type mismatch: const expr stack underflow");
  498. return false;
  499. }
  500. cur_value = &ctx->stack[--ctx->sp];
  501. #if WASM_ENABLE_GC == 0
  502. if (cur_value->type != type) {
  503. set_error_buf(error_buf, error_buf_size, "type mismatch");
  504. return false;
  505. }
  506. #else
  507. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  508. ref_type, ctx->module->types,
  509. ctx->module->type_count)) {
  510. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  511. "type mismatch: expect ", type2str(type),
  512. " but got other");
  513. goto fail;
  514. }
  515. #endif
  516. if (p_flag)
  517. *p_flag = cur_value->flag;
  518. if (p_value)
  519. *p_value = cur_value->value;
  520. #if WASM_ENABLE_GC != 0
  521. if (p_gc_opcode)
  522. *p_gc_opcode = cur_value->gc_opcode;
  523. #endif
  524. return true;
  525. #if WASM_ENABLE_GC != 0
  526. fail:
  527. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  528. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  529. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  530. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  531. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  532. }
  533. return false;
  534. #endif
  535. }
  536. static void
  537. destroy_const_expr_stack(ConstExprContext *ctx)
  538. {
  539. #if WASM_ENABLE_GC != 0
  540. uint32 i;
  541. for (i = 0; i < ctx->sp; i++) {
  542. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  543. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  544. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  545. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  546. destroy_init_expr_data_recursive(ctx->module,
  547. ctx->stack[i].value.data);
  548. }
  549. }
  550. #endif
  551. if (ctx->stack != ctx->data) {
  552. wasm_runtime_free(ctx->stack);
  553. }
  554. }
  555. #if WASM_ENABLE_GC != 0
  556. static void
  557. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  558. {
  559. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  560. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  561. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  562. destroy_init_expr_data_recursive(module, expr->u.data);
  563. }
  564. }
  565. #endif /* end of WASM_ENABLE_GC != 0 */
  566. static bool
  567. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  568. InitializerExpression *init_expr, uint8 type, void *ref_type,
  569. char *error_buf, uint32 error_buf_size)
  570. {
  571. const uint8 *p = *p_buf, *p_end = buf_end;
  572. uint8 flag, *p_float;
  573. uint32 i;
  574. ConstExprContext const_expr_ctx = { 0 };
  575. WASMValue cur_value;
  576. #if WASM_ENABLE_GC != 0
  577. uint32 opcode1, type_idx;
  578. uint8 opcode;
  579. WASMRefType cur_ref_type = { 0 };
  580. #endif
  581. init_const_expr_stack(&const_expr_ctx, module);
  582. CHECK_BUF(p, p_end, 1);
  583. flag = read_uint8(p);
  584. while (flag != WASM_OP_END) {
  585. switch (flag) {
  586. /* i32.const */
  587. case INIT_EXPR_TYPE_I32_CONST:
  588. read_leb_int32(p, p_end, cur_value.i32);
  589. if (!push_const_expr_stack(
  590. &const_expr_ctx, flag, VALUE_TYPE_I32,
  591. #if WASM_ENABLE_GC != 0
  592. NULL, 0,
  593. #endif
  594. &cur_value, error_buf, error_buf_size))
  595. goto fail;
  596. break;
  597. /* i64.const */
  598. case INIT_EXPR_TYPE_I64_CONST:
  599. read_leb_int64(p, p_end, cur_value.i64);
  600. if (!push_const_expr_stack(
  601. &const_expr_ctx, flag, VALUE_TYPE_I64,
  602. #if WASM_ENABLE_GC != 0
  603. NULL, 0,
  604. #endif
  605. &cur_value, error_buf, error_buf_size))
  606. goto fail;
  607. break;
  608. /* f32.const */
  609. case INIT_EXPR_TYPE_F32_CONST:
  610. CHECK_BUF(p, p_end, 4);
  611. p_float = (uint8 *)&cur_value.f32;
  612. for (i = 0; i < sizeof(float32); i++)
  613. *p_float++ = *p++;
  614. if (!push_const_expr_stack(
  615. &const_expr_ctx, flag, VALUE_TYPE_F32,
  616. #if WASM_ENABLE_GC != 0
  617. NULL, 0,
  618. #endif
  619. &cur_value, error_buf, error_buf_size))
  620. goto fail;
  621. break;
  622. /* f64.const */
  623. case INIT_EXPR_TYPE_F64_CONST:
  624. CHECK_BUF(p, p_end, 8);
  625. p_float = (uint8 *)&cur_value.f64;
  626. for (i = 0; i < sizeof(float64); i++)
  627. *p_float++ = *p++;
  628. if (!push_const_expr_stack(
  629. &const_expr_ctx, flag, VALUE_TYPE_F64,
  630. #if WASM_ENABLE_GC != 0
  631. NULL, 0,
  632. #endif
  633. &cur_value, error_buf, error_buf_size))
  634. goto fail;
  635. break;
  636. #if WASM_ENABLE_SIMD != 0
  637. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  638. /* v128.const */
  639. case INIT_EXPR_TYPE_V128_CONST:
  640. {
  641. uint64 high, low;
  642. CHECK_BUF(p, p_end, 1);
  643. (void)read_uint8(p);
  644. CHECK_BUF(p, p_end, 16);
  645. wasm_runtime_read_v128(p, &high, &low);
  646. p += 16;
  647. cur_value.v128.i64x2[0] = high;
  648. cur_value.v128.i64x2[1] = low;
  649. if (!push_const_expr_stack(
  650. &const_expr_ctx, flag, VALUE_TYPE_V128,
  651. #if WASM_ENABLE_GC != 0
  652. NULL, 0,
  653. #endif
  654. &cur_value, error_buf, error_buf_size))
  655. goto fail;
  656. #if WASM_ENABLE_WAMR_COMPILER != 0
  657. /* If any init_expr is v128.const, mark SIMD used */
  658. module->is_simd_used = true;
  659. #endif
  660. break;
  661. }
  662. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  663. #endif /* end of WASM_ENABLE_SIMD */
  664. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  665. /* ref.func */
  666. case INIT_EXPR_TYPE_FUNCREF_CONST:
  667. {
  668. uint32 func_idx;
  669. read_leb_uint32(p, p_end, func_idx);
  670. cur_value.ref_index = func_idx;
  671. if (!check_function_index(module, func_idx, error_buf,
  672. error_buf_size)) {
  673. goto fail;
  674. }
  675. #if WASM_ENABLE_GC == 0
  676. if (!push_const_expr_stack(&const_expr_ctx, flag,
  677. VALUE_TYPE_FUNCREF, &cur_value,
  678. error_buf, error_buf_size))
  679. goto fail;
  680. #else
  681. if (func_idx < module->import_function_count) {
  682. type_idx =
  683. module->import_functions[func_idx].u.function.type_idx;
  684. }
  685. else {
  686. type_idx = module
  687. ->functions[func_idx
  688. - module->import_function_count]
  689. ->type_idx;
  690. }
  691. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  692. false, type_idx);
  693. if (!push_const_expr_stack(&const_expr_ctx, flag,
  694. cur_ref_type.ref_type, &cur_ref_type,
  695. 0, &cur_value, error_buf,
  696. error_buf_size))
  697. goto fail;
  698. #endif
  699. #if WASM_ENABLE_WAMR_COMPILER != 0
  700. module->is_ref_types_used = true;
  701. #endif
  702. break;
  703. }
  704. /* ref.null */
  705. case INIT_EXPR_TYPE_REFNULL_CONST:
  706. {
  707. uint8 type1;
  708. CHECK_BUF(p, p_end, 1);
  709. type1 = read_uint8(p);
  710. #if WASM_ENABLE_GC == 0
  711. cur_value.ref_index = NULL_REF;
  712. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  713. &cur_value, error_buf,
  714. error_buf_size))
  715. goto fail;
  716. #else
  717. cur_value.gc_obj = NULL_REF;
  718. if (!is_byte_a_type(type1)) {
  719. p--;
  720. read_leb_uint32(p, p_end, type_idx);
  721. if (!check_type_index(module, module->type_count, type_idx,
  722. error_buf, error_buf_size))
  723. goto fail;
  724. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  725. true, type_idx);
  726. if (!push_const_expr_stack(&const_expr_ctx, flag,
  727. cur_ref_type.ref_type,
  728. &cur_ref_type, 0, &cur_value,
  729. error_buf, error_buf_size))
  730. goto fail;
  731. }
  732. else {
  733. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  734. NULL, 0, &cur_value, error_buf,
  735. error_buf_size))
  736. goto fail;
  737. }
  738. #endif
  739. #if WASM_ENABLE_WAMR_COMPILER != 0
  740. module->is_ref_types_used = true;
  741. #endif
  742. break;
  743. }
  744. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  745. /* get_global */
  746. case INIT_EXPR_TYPE_GET_GLOBAL:
  747. {
  748. uint32 global_idx;
  749. uint8 global_type;
  750. read_leb_uint32(p, p_end, cur_value.global_index);
  751. global_idx = cur_value.global_index;
  752. /*
  753. * Currently, constant expressions occurring as initializers
  754. * of globals are further constrained in that contained
  755. * global.get instructions are
  756. * only allowed to refer to imported globals.
  757. *
  758. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  759. */
  760. if (global_idx >= module->import_global_count
  761. /* make spec test happy */
  762. #if WASM_ENABLE_GC != 0
  763. + module->global_count
  764. #endif
  765. ) {
  766. set_error_buf_v(error_buf, error_buf_size,
  767. "unknown global %u", global_idx);
  768. goto fail;
  769. }
  770. if (
  771. /* make spec test happy */
  772. #if WASM_ENABLE_GC != 0
  773. global_idx < module->import_global_count &&
  774. #endif
  775. module->import_globals[global_idx]
  776. .u.global.type.is_mutable) {
  777. set_error_buf_v(error_buf, error_buf_size,
  778. "constant expression required");
  779. goto fail;
  780. }
  781. if (global_idx < module->import_global_count) {
  782. global_type = module->import_globals[global_idx]
  783. .u.global.type.val_type;
  784. #if WASM_ENABLE_GC != 0
  785. if (wasm_is_type_multi_byte_type(global_type)) {
  786. WASMRefType *global_ref_type =
  787. module->import_globals[global_idx]
  788. .u.global.ref_type;
  789. bh_memcpy_s(&cur_ref_type,
  790. wasm_reftype_struct_size(global_ref_type),
  791. global_ref_type,
  792. wasm_reftype_struct_size(global_ref_type));
  793. }
  794. #endif
  795. }
  796. else {
  797. global_type =
  798. module
  799. ->globals[global_idx - module->import_global_count]
  800. .type.val_type;
  801. #if WASM_ENABLE_GC != 0
  802. if (wasm_is_type_multi_byte_type(global_type)) {
  803. WASMRefType *global_ref_type =
  804. module
  805. ->globals[global_idx
  806. - module->import_global_count]
  807. .ref_type;
  808. bh_memcpy_s(&cur_ref_type,
  809. wasm_reftype_struct_size(global_ref_type),
  810. global_ref_type,
  811. wasm_reftype_struct_size(global_ref_type));
  812. }
  813. #endif
  814. }
  815. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  816. #if WASM_ENABLE_GC != 0
  817. &cur_ref_type, 0,
  818. #endif
  819. &cur_value, error_buf,
  820. error_buf_size))
  821. goto fail;
  822. break;
  823. }
  824. #if WASM_ENABLE_GC != 0
  825. /* struct.new and array.new */
  826. case WASM_OP_GC_PREFIX:
  827. {
  828. read_leb_uint32(p, p_end, opcode1);
  829. switch (opcode1) {
  830. case WASM_OP_STRUCT_NEW:
  831. {
  832. WASMStructType *struct_type;
  833. WASMStructNewInitValues *struct_init_values = NULL;
  834. uint32 field_count;
  835. read_leb_uint32(p, p_end, type_idx);
  836. if (!check_type_index(module, module->type_count,
  837. type_idx, error_buf,
  838. error_buf_size)) {
  839. goto fail;
  840. }
  841. struct_type = (WASMStructType *)module->types[type_idx];
  842. if (struct_type->base_type.type_flag
  843. != WASM_TYPE_STRUCT) {
  844. set_error_buf(error_buf, error_buf_size,
  845. "unkown struct type");
  846. goto fail;
  847. }
  848. field_count = struct_type->field_count;
  849. if (!(struct_init_values = loader_malloc(
  850. offsetof(WASMStructNewInitValues, fields)
  851. + (uint64)field_count * sizeof(WASMValue),
  852. error_buf, error_buf_size))) {
  853. goto fail;
  854. }
  855. struct_init_values->type_idx = type_idx;
  856. struct_init_values->count = field_count;
  857. for (i = field_count; i > 0; i--) {
  858. WASMRefType *field_ref_type = NULL;
  859. uint32 field_idx = i - 1;
  860. uint8 field_type =
  861. struct_type->fields[field_idx].field_type;
  862. if (wasm_is_type_multi_byte_type(field_type)) {
  863. field_ref_type = wasm_reftype_map_find(
  864. struct_type->ref_type_maps,
  865. struct_type->ref_type_map_count, field_idx);
  866. }
  867. if (is_packed_type(field_type)) {
  868. field_type = VALUE_TYPE_I32;
  869. }
  870. if (!pop_const_expr_stack(
  871. &const_expr_ctx, NULL, field_type,
  872. field_ref_type, NULL,
  873. &struct_init_values->fields[field_idx],
  874. error_buf, error_buf_size)) {
  875. destroy_init_expr_data_recursive(
  876. module, struct_init_values);
  877. goto fail;
  878. }
  879. }
  880. cur_value.data = struct_init_values;
  881. wasm_set_refheaptype_typeidx(
  882. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  883. if (!push_const_expr_stack(
  884. &const_expr_ctx, flag, cur_ref_type.ref_type,
  885. &cur_ref_type, (uint8)opcode1, &cur_value,
  886. error_buf, error_buf_size)) {
  887. destroy_init_expr_data_recursive(
  888. module, struct_init_values);
  889. goto fail;
  890. }
  891. break;
  892. }
  893. case WASM_OP_STRUCT_NEW_DEFAULT:
  894. {
  895. read_leb_uint32(p, p_end, cur_value.type_index);
  896. type_idx = cur_value.type_index;
  897. if (!check_type_index(module, module->type_count,
  898. type_idx, error_buf,
  899. error_buf_size)) {
  900. goto fail;
  901. }
  902. if (module->types[type_idx]->type_flag
  903. != WASM_TYPE_STRUCT) {
  904. set_error_buf(error_buf, error_buf_size,
  905. "unkown struct type");
  906. goto fail;
  907. }
  908. cur_value.type_index = type_idx;
  909. cur_value.data = NULL;
  910. wasm_set_refheaptype_typeidx(
  911. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  912. if (!push_const_expr_stack(
  913. &const_expr_ctx, flag, cur_ref_type.ref_type,
  914. &cur_ref_type, (uint8)opcode1, &cur_value,
  915. error_buf, error_buf_size)) {
  916. goto fail;
  917. }
  918. break;
  919. }
  920. case WASM_OP_ARRAY_NEW:
  921. case WASM_OP_ARRAY_NEW_DEFAULT:
  922. case WASM_OP_ARRAY_NEW_FIXED:
  923. {
  924. WASMArrayNewInitValues *array_init_values = NULL;
  925. WASMArrayType *array_type = NULL;
  926. WASMRefType *elem_ref_type = NULL;
  927. uint64 total_size;
  928. uint8 elem_type;
  929. read_leb_uint32(p, p_end, cur_value.type_index);
  930. type_idx = cur_value.type_index;
  931. if (!check_type_index(module, module->type_count,
  932. type_idx, error_buf,
  933. error_buf_size)) {
  934. goto fail;
  935. }
  936. array_type = (WASMArrayType *)module->types[type_idx];
  937. if (array_type->base_type.type_flag
  938. != WASM_TYPE_ARRAY) {
  939. set_error_buf(error_buf, error_buf_size,
  940. "unkown array type");
  941. goto fail;
  942. }
  943. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  944. elem_type = array_type->elem_type;
  945. if (wasm_is_type_multi_byte_type(elem_type)) {
  946. elem_ref_type = array_type->elem_ref_type;
  947. }
  948. if (is_packed_type(elem_type)) {
  949. elem_type = VALUE_TYPE_I32;
  950. }
  951. if (opcode1 == WASM_OP_ARRAY_NEW) {
  952. WASMValue len_val;
  953. if (!(array_init_values = loader_malloc(
  954. sizeof(WASMArrayNewInitValues),
  955. error_buf, error_buf_size))) {
  956. goto fail;
  957. }
  958. array_init_values->type_idx = type_idx;
  959. if (!pop_const_expr_stack(
  960. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  961. NULL, NULL, &len_val, error_buf,
  962. error_buf_size)) {
  963. destroy_init_expr_data_recursive(
  964. module, array_init_values);
  965. goto fail;
  966. }
  967. array_init_values->length = len_val.i32;
  968. if (!pop_const_expr_stack(
  969. &const_expr_ctx, NULL, elem_type,
  970. elem_ref_type, NULL,
  971. &array_init_values->elem_data[0],
  972. error_buf, error_buf_size)) {
  973. destroy_init_expr_data_recursive(
  974. module, array_init_values);
  975. goto fail;
  976. }
  977. cur_value.data = array_init_values;
  978. }
  979. else {
  980. /* WASM_OP_ARRAY_NEW_FIXED */
  981. uint32 len;
  982. read_leb_uint32(p, p_end, len);
  983. total_size =
  984. (uint64)offsetof(WASMArrayNewInitValues,
  985. elem_data)
  986. + (uint64)sizeof(WASMValue) * len;
  987. if (!(array_init_values =
  988. loader_malloc(total_size, error_buf,
  989. error_buf_size))) {
  990. goto fail;
  991. }
  992. array_init_values->type_idx = type_idx;
  993. array_init_values->length = len;
  994. for (i = len; i > 0; i--) {
  995. if (!pop_const_expr_stack(
  996. &const_expr_ctx, NULL, elem_type,
  997. elem_ref_type, NULL,
  998. &array_init_values
  999. ->elem_data[i - 1],
  1000. error_buf, error_buf_size)) {
  1001. destroy_init_expr_data_recursive(
  1002. module, array_init_values);
  1003. goto fail;
  1004. }
  1005. }
  1006. cur_value.data = array_init_values;
  1007. }
  1008. }
  1009. else {
  1010. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1011. WASMValue len_val;
  1012. uint32 len;
  1013. /* POP(i32) */
  1014. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1015. VALUE_TYPE_I32, NULL,
  1016. NULL, &len_val, error_buf,
  1017. error_buf_size)) {
  1018. goto fail;
  1019. }
  1020. len = len_val.i32;
  1021. cur_value.array_new_default.type_index = type_idx;
  1022. cur_value.array_new_default.length = len;
  1023. }
  1024. wasm_set_refheaptype_typeidx(
  1025. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1026. if (!push_const_expr_stack(
  1027. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1028. &cur_ref_type, (uint8)opcode1, &cur_value,
  1029. error_buf, error_buf_size)) {
  1030. if (array_init_values) {
  1031. destroy_init_expr_data_recursive(
  1032. module, array_init_values);
  1033. }
  1034. goto fail;
  1035. }
  1036. break;
  1037. }
  1038. case WASM_OP_ANY_CONVERT_EXTERN:
  1039. {
  1040. set_error_buf(error_buf, error_buf_size,
  1041. "unsupported constant expression of "
  1042. "extern.internalize");
  1043. goto fail;
  1044. }
  1045. case WASM_OP_EXTERN_CONVERT_ANY:
  1046. {
  1047. set_error_buf(error_buf, error_buf_size,
  1048. "unsupported constant expression of "
  1049. "extern.externalize");
  1050. goto fail;
  1051. }
  1052. case WASM_OP_REF_I31:
  1053. {
  1054. /* POP(i32) */
  1055. if (!pop_const_expr_stack(
  1056. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1057. NULL, &cur_value, error_buf, error_buf_size)) {
  1058. goto fail;
  1059. }
  1060. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1061. false, HEAP_TYPE_I31);
  1062. if (!push_const_expr_stack(
  1063. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1064. &cur_ref_type, (uint8)opcode1, &cur_value,
  1065. error_buf, error_buf_size)) {
  1066. goto fail;
  1067. }
  1068. break;
  1069. }
  1070. default:
  1071. set_error_buf(
  1072. error_buf, error_buf_size,
  1073. "type mismatch or constant expression required");
  1074. goto fail;
  1075. }
  1076. break;
  1077. }
  1078. #endif /* end of WASM_ENABLE_GC != 0 */
  1079. default:
  1080. {
  1081. set_error_buf(error_buf, error_buf_size,
  1082. "illegal opcode "
  1083. "or constant expression required "
  1084. "or type mismatch");
  1085. goto fail;
  1086. }
  1087. }
  1088. CHECK_BUF(p, p_end, 1);
  1089. flag = read_uint8(p);
  1090. }
  1091. /* There should be only one value left on the init value stack */
  1092. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1093. #if WASM_ENABLE_GC != 0
  1094. ref_type, &opcode,
  1095. #endif
  1096. &cur_value, error_buf, error_buf_size)) {
  1097. goto fail;
  1098. }
  1099. if (const_expr_ctx.sp != 0) {
  1100. set_error_buf(error_buf, error_buf_size,
  1101. "type mismatch: illegal constant opcode sequence");
  1102. goto fail;
  1103. }
  1104. init_expr->init_expr_type = flag;
  1105. init_expr->u = cur_value;
  1106. #if WASM_ENABLE_GC != 0
  1107. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1108. switch (opcode) {
  1109. case WASM_OP_STRUCT_NEW:
  1110. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1111. break;
  1112. case WASM_OP_STRUCT_NEW_DEFAULT:
  1113. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1114. break;
  1115. case WASM_OP_ARRAY_NEW:
  1116. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1117. break;
  1118. case WASM_OP_ARRAY_NEW_DEFAULT:
  1119. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1120. break;
  1121. case WASM_OP_ARRAY_NEW_FIXED:
  1122. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1123. break;
  1124. case WASM_OP_REF_I31:
  1125. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1126. break;
  1127. default:
  1128. bh_assert(0);
  1129. break;
  1130. }
  1131. }
  1132. #endif /* end of WASM_ENABLE_GC != 0 */
  1133. *p_buf = p;
  1134. destroy_const_expr_stack(&const_expr_ctx);
  1135. return true;
  1136. fail:
  1137. destroy_const_expr_stack(&const_expr_ctx);
  1138. return false;
  1139. }
  1140. static bool
  1141. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1142. {
  1143. if (mutable >= 2) {
  1144. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1145. return false;
  1146. }
  1147. return true;
  1148. }
  1149. #if WASM_ENABLE_GC != 0
  1150. static void
  1151. destroy_func_type(WASMFuncType *type)
  1152. {
  1153. /* Destroy the reference type hash set */
  1154. if (type->ref_type_maps)
  1155. wasm_runtime_free(type->ref_type_maps);
  1156. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1157. && WASM_ENABLE_LAZY_JIT != 0
  1158. if (type->call_to_llvm_jit_from_fast_jit)
  1159. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1160. #endif
  1161. /* Free the type */
  1162. wasm_runtime_free(type);
  1163. }
  1164. static void
  1165. destroy_struct_type(WASMStructType *type)
  1166. {
  1167. if (type->ref_type_maps)
  1168. wasm_runtime_free(type->ref_type_maps);
  1169. wasm_runtime_free(type);
  1170. }
  1171. static void
  1172. destroy_array_type(WASMArrayType *type)
  1173. {
  1174. wasm_runtime_free(type);
  1175. }
  1176. static void
  1177. destroy_wasm_type(WASMType *type)
  1178. {
  1179. if (type->ref_count > 1) {
  1180. /* The type is referenced by other types
  1181. of current wasm module */
  1182. type->ref_count--;
  1183. return;
  1184. }
  1185. if (type->type_flag == WASM_TYPE_FUNC)
  1186. destroy_func_type((WASMFuncType *)type);
  1187. else if (type->type_flag == WASM_TYPE_STRUCT)
  1188. destroy_struct_type((WASMStructType *)type);
  1189. else if (type->type_flag == WASM_TYPE_ARRAY)
  1190. destroy_array_type((WASMArrayType *)type);
  1191. else {
  1192. bh_assert(0);
  1193. }
  1194. }
  1195. /* Resolve (ref null ht) or (ref ht) */
  1196. static bool
  1197. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1198. WASMModule *module, uint32 type_count, bool nullable,
  1199. WASMRefType *ref_type, char *error_buf,
  1200. uint32 error_buf_size)
  1201. {
  1202. const uint8 *p = *p_buf, *p_end = buf_end;
  1203. ref_type->ref_type =
  1204. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1205. ref_type->ref_ht_common.nullable = nullable;
  1206. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1207. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1208. /* heap type is (type i), i : typeidx, >= 0 */
  1209. if (!check_type_index(module, type_count,
  1210. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1211. error_buf_size)) {
  1212. return false;
  1213. }
  1214. }
  1215. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1216. /* heap type is func, extern, any, eq, i31 or data */
  1217. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1218. return false;
  1219. }
  1220. *p_buf = p;
  1221. return true;
  1222. fail:
  1223. return false;
  1224. }
  1225. static bool
  1226. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1227. WASMModule *module, uint32 type_count,
  1228. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1229. bool allow_packed_type, char *error_buf,
  1230. uint32 error_buf_size)
  1231. {
  1232. const uint8 *p = *p_buf, *p_end = buf_end;
  1233. uint8 type;
  1234. memset(ref_type, 0, sizeof(WASMRefType));
  1235. CHECK_BUF(p, p_end, 1);
  1236. type = read_uint8(p);
  1237. if (wasm_is_reftype_htref_nullable(type)) {
  1238. /* (ref null ht) */
  1239. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1240. ref_type, error_buf, error_buf_size))
  1241. return false;
  1242. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1243. *p_need_ref_type_map = true;
  1244. else {
  1245. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1246. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1247. multi-byte type to one-byte type to reduce the footprint and
  1248. the complexity of type equal/subtype checking */
  1249. ref_type->ref_type =
  1250. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1251. *p_need_ref_type_map = false;
  1252. }
  1253. }
  1254. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1255. /* (ref ht) */
  1256. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1257. ref_type, error_buf, error_buf_size))
  1258. return false;
  1259. *p_need_ref_type_map = true;
  1260. #if WASM_ENABLE_STRINGREF != 0
  1261. /* covert (ref string) to stringref */
  1262. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1263. ref_type->ref_type =
  1264. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1265. *p_need_ref_type_map = false;
  1266. }
  1267. #endif
  1268. }
  1269. else {
  1270. /* type which can be represented by one byte */
  1271. if (!is_valid_value_type_for_interpreter(type)
  1272. && !(allow_packed_type && is_packed_type(type))) {
  1273. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1274. return false;
  1275. }
  1276. ref_type->ref_type = type;
  1277. *p_need_ref_type_map = false;
  1278. #if WASM_ENABLE_WAMR_COMPILER != 0
  1279. /* If any value's type is v128, mark the module as SIMD used */
  1280. if (type == VALUE_TYPE_V128)
  1281. module->is_simd_used = true;
  1282. #endif
  1283. }
  1284. *p_buf = p;
  1285. return true;
  1286. fail:
  1287. return false;
  1288. }
  1289. static WASMRefType *
  1290. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1291. char *error_buf, uint32 error_buf_size)
  1292. {
  1293. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1294. if (!ret) {
  1295. set_error_buf(error_buf, error_buf_size,
  1296. "insert ref type to hash set failed");
  1297. }
  1298. return ret;
  1299. }
  1300. static bool
  1301. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1302. uint32 type_count, uint32 type_idx, char *error_buf,
  1303. uint32 error_buf_size)
  1304. {
  1305. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1306. uint32 param_count, result_count, i, j = 0;
  1307. uint32 param_cell_num, ret_cell_num;
  1308. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1309. uint64 total_size;
  1310. bool need_ref_type_map;
  1311. WASMRefType ref_type;
  1312. WASMFuncType *type = NULL;
  1313. /* Parse first time to resolve param count, result count and
  1314. ref type map count */
  1315. read_leb_uint32(p, p_end, param_count);
  1316. p_org = p;
  1317. for (i = 0; i < param_count; i++) {
  1318. if (!resolve_value_type(&p, p_end, module, type_count,
  1319. &need_ref_type_map, &ref_type, false, error_buf,
  1320. error_buf_size)) {
  1321. return false;
  1322. }
  1323. if (need_ref_type_map)
  1324. ref_type_map_count++;
  1325. }
  1326. read_leb_uint32(p, p_end, result_count);
  1327. for (i = 0; i < result_count; i++) {
  1328. if (!resolve_value_type(&p, p_end, module, type_count,
  1329. &need_ref_type_map, &ref_type, false, error_buf,
  1330. error_buf_size)) {
  1331. return false;
  1332. }
  1333. if (need_ref_type_map) {
  1334. ref_type_map_count++;
  1335. result_ref_type_map_count++;
  1336. }
  1337. }
  1338. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1339. "ref type map count: %d",
  1340. type_idx, param_count, result_count, ref_type_map_count);
  1341. /* Parse second time to resolve param types, result types and
  1342. ref type map info */
  1343. p = p_org;
  1344. total_size = offsetof(WASMFuncType, types)
  1345. + sizeof(uint8) * (uint64)(param_count + result_count);
  1346. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1347. return false;
  1348. }
  1349. if (ref_type_map_count > 0) {
  1350. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1351. if (!(type->ref_type_maps =
  1352. loader_malloc(total_size, error_buf, error_buf_size))) {
  1353. goto fail;
  1354. }
  1355. }
  1356. type->base_type.type_flag = WASM_TYPE_FUNC;
  1357. type->param_count = param_count;
  1358. type->result_count = result_count;
  1359. type->ref_type_map_count = ref_type_map_count;
  1360. if (ref_type_map_count > 0) {
  1361. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1362. - result_ref_type_map_count;
  1363. }
  1364. for (i = 0; i < param_count; i++) {
  1365. if (!resolve_value_type(&p, p_end, module, type_count,
  1366. &need_ref_type_map, &ref_type, false, error_buf,
  1367. error_buf_size)) {
  1368. goto fail;
  1369. }
  1370. type->types[i] = ref_type.ref_type;
  1371. if (need_ref_type_map) {
  1372. type->ref_type_maps[j].index = i;
  1373. if (!(type->ref_type_maps[j++].ref_type =
  1374. reftype_set_insert(module->ref_type_set, &ref_type,
  1375. error_buf, error_buf_size))) {
  1376. goto fail;
  1377. }
  1378. }
  1379. }
  1380. read_leb_uint32(p, p_end, result_count);
  1381. for (i = 0; i < result_count; i++) {
  1382. if (!resolve_value_type(&p, p_end, module, type_count,
  1383. &need_ref_type_map, &ref_type, false, error_buf,
  1384. error_buf_size)) {
  1385. goto fail;
  1386. }
  1387. type->types[param_count + i] = ref_type.ref_type;
  1388. if (need_ref_type_map) {
  1389. type->ref_type_maps[j].index = param_count + i;
  1390. if (!(type->ref_type_maps[j++].ref_type =
  1391. reftype_set_insert(module->ref_type_set, &ref_type,
  1392. error_buf, error_buf_size))) {
  1393. goto fail;
  1394. }
  1395. }
  1396. }
  1397. bh_assert(j == type->ref_type_map_count);
  1398. #if TRACE_WASM_LOADER != 0
  1399. os_printf("type %d = ", type_idx);
  1400. wasm_dump_func_type(type);
  1401. #endif
  1402. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1403. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1404. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1405. set_error_buf(error_buf, error_buf_size,
  1406. "param count or result count too large");
  1407. goto fail;
  1408. }
  1409. type->param_cell_num = (uint16)param_cell_num;
  1410. type->ret_cell_num = (uint16)ret_cell_num;
  1411. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1412. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1413. #endif
  1414. #if WASM_ENABLE_WAMR_COMPILER != 0
  1415. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1416. if (type->types[i] == VALUE_TYPE_V128)
  1417. module->is_simd_used = true;
  1418. }
  1419. #endif
  1420. *p_buf = p;
  1421. module->types[type_idx] = (WASMType *)type;
  1422. return true;
  1423. fail:
  1424. if (type)
  1425. destroy_func_type(type);
  1426. return false;
  1427. }
  1428. static bool
  1429. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1430. WASMModule *module, uint32 type_count, uint32 type_idx,
  1431. char *error_buf, uint32 error_buf_size)
  1432. {
  1433. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1434. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1435. uint32 i, j = 0, offset;
  1436. uint16 *reference_table;
  1437. uint64 total_size;
  1438. uint8 mutable;
  1439. bool need_ref_type_map;
  1440. WASMRefType ref_type;
  1441. WASMStructType *type = NULL;
  1442. /* Parse first time to resolve field count and ref type map count */
  1443. read_leb_uint32(p, p_end, field_count);
  1444. p_org = p;
  1445. for (i = 0; i < field_count; i++) {
  1446. if (!resolve_value_type(&p, p_end, module, type_count,
  1447. &need_ref_type_map, &ref_type, true, error_buf,
  1448. error_buf_size)) {
  1449. return false;
  1450. }
  1451. if (need_ref_type_map)
  1452. ref_type_map_count++;
  1453. if (wasm_is_type_reftype(ref_type.ref_type))
  1454. ref_field_count++;
  1455. CHECK_BUF(p, p_end, 1);
  1456. mutable = read_uint8(p);
  1457. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1458. return false;
  1459. }
  1460. }
  1461. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1462. type_idx, field_count, ref_type_map_count);
  1463. /* Parse second time to resolve field types and ref type map info */
  1464. p = p_org;
  1465. total_size = offsetof(WASMStructType, fields)
  1466. + sizeof(WASMStructFieldType) * (uint64)field_count
  1467. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1468. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1469. return false;
  1470. }
  1471. if (ref_type_map_count > 0) {
  1472. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1473. if (!(type->ref_type_maps =
  1474. loader_malloc(total_size, error_buf, error_buf_size))) {
  1475. goto fail;
  1476. }
  1477. }
  1478. type->reference_table = reference_table =
  1479. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1480. + sizeof(WASMStructFieldType) * field_count);
  1481. *reference_table++ = ref_field_count;
  1482. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1483. type->field_count = field_count;
  1484. type->ref_type_map_count = ref_type_map_count;
  1485. offset = (uint32)offsetof(WASMStructObject, field_data);
  1486. for (i = 0; i < field_count; i++) {
  1487. if (!resolve_value_type(&p, p_end, module, type_count,
  1488. &need_ref_type_map, &ref_type, true, error_buf,
  1489. error_buf_size)) {
  1490. goto fail;
  1491. }
  1492. type->fields[i].field_type = ref_type.ref_type;
  1493. if (need_ref_type_map) {
  1494. type->ref_type_maps[j].index = i;
  1495. if (!(type->ref_type_maps[j++].ref_type =
  1496. reftype_set_insert(module->ref_type_set, &ref_type,
  1497. error_buf, error_buf_size))) {
  1498. goto fail;
  1499. }
  1500. }
  1501. CHECK_BUF(p, p_end, 1);
  1502. type->fields[i].field_flags = read_uint8(p);
  1503. type->fields[i].field_size =
  1504. (uint8)wasm_reftype_size(ref_type.ref_type);
  1505. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1506. || defined(BUILD_TARGET_X86_32))
  1507. if (type->fields[i].field_size == 2)
  1508. offset = align_uint(offset, 2);
  1509. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1510. offset = align_uint(offset, 4);
  1511. #endif
  1512. type->fields[i].field_offset = offset;
  1513. if (wasm_is_type_reftype(ref_type.ref_type))
  1514. *reference_table++ = offset;
  1515. offset += type->fields[i].field_size;
  1516. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1517. type->fields[i].field_flags, type->fields[i].field_type);
  1518. }
  1519. type->total_size = offset;
  1520. bh_assert(j == type->ref_type_map_count);
  1521. #if TRACE_WASM_LOADER != 0
  1522. os_printf("type %d = ", type_idx);
  1523. wasm_dump_struct_type(type);
  1524. #endif
  1525. *p_buf = p;
  1526. module->types[type_idx] = (WASMType *)type;
  1527. return true;
  1528. fail:
  1529. if (type)
  1530. destroy_struct_type(type);
  1531. return false;
  1532. }
  1533. static bool
  1534. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1535. WASMModule *module, uint32 type_count, uint32 type_idx,
  1536. char *error_buf, uint32 error_buf_size)
  1537. {
  1538. const uint8 *p = *p_buf, *p_end = buf_end;
  1539. uint8 mutable;
  1540. bool need_ref_type_map;
  1541. WASMRefType ref_type;
  1542. WASMArrayType *type = NULL;
  1543. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1544. &ref_type, true, error_buf, error_buf_size)) {
  1545. return false;
  1546. }
  1547. CHECK_BUF(p, p_end, 1);
  1548. mutable = read_uint8(p);
  1549. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1550. return false;
  1551. }
  1552. LOG_VERBOSE("type %u: array", type_idx);
  1553. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1554. error_buf_size))) {
  1555. return false;
  1556. }
  1557. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1558. type->elem_flags = mutable;
  1559. type->elem_type = ref_type.ref_type;
  1560. if (need_ref_type_map) {
  1561. if (!(type->elem_ref_type =
  1562. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1563. error_buf_size))) {
  1564. goto fail;
  1565. }
  1566. }
  1567. #if TRACE_WASM_LOADER != 0
  1568. os_printf("type %d = ", type_idx);
  1569. wasm_dump_array_type(type);
  1570. #endif
  1571. *p_buf = p;
  1572. module->types[type_idx] = (WASMType *)type;
  1573. return true;
  1574. fail:
  1575. if (type)
  1576. destroy_array_type(type);
  1577. return false;
  1578. }
  1579. static bool
  1580. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1581. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1582. {
  1583. if (heap_type >= 0) {
  1584. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1585. error_buf_size)) {
  1586. return false;
  1587. }
  1588. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1589. heap_type);
  1590. }
  1591. else {
  1592. if (!wasm_is_valid_heap_type(heap_type)) {
  1593. set_error_buf(error_buf, error_buf_size, "unknown type");
  1594. return false;
  1595. }
  1596. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1597. heap_type);
  1598. if (nullable) {
  1599. /* For (ref null func/extern/any/eq/i31/data),
  1600. they are same as
  1601. funcref/externref/anyref/eqref/i31ref/dataref,
  1602. we convert the multi-byte type to one-byte
  1603. type to reduce the footprint and the
  1604. complexity of type equal/subtype checking */
  1605. ref_type->ref_type =
  1606. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1607. }
  1608. }
  1609. return true;
  1610. }
  1611. static void
  1612. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1613. WASMRefType *ref_type2)
  1614. {
  1615. /**
  1616. * The difference rt1 ∖ rt2 between two reference types is defined as
  1617. * follows:
  1618. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1619. * (ref ht2) = (ref null?1 ht1)
  1620. */
  1621. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1622. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1623. ref_type1, wasm_reftype_struct_size(ref_type1));
  1624. }
  1625. else {
  1626. ref_type_diff->ref_type = ref_type1->ref_type;
  1627. }
  1628. if (ref_type2->ref_ht_common.nullable) {
  1629. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1630. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1631. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1632. (int32)ref_type_diff->ref_type - 0x80);
  1633. }
  1634. else {
  1635. ref_type_diff->ref_ht_typeidx.nullable = false;
  1636. }
  1637. }
  1638. }
  1639. #else /* else of WASM_ENABLE_GC != 0 */
  1640. static void
  1641. destroy_wasm_type(WASMType *type)
  1642. {
  1643. if (type->ref_count > 1) {
  1644. /* The type is referenced by other types
  1645. of current wasm module */
  1646. type->ref_count--;
  1647. return;
  1648. }
  1649. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1650. && WASM_ENABLE_LAZY_JIT != 0
  1651. if (type->call_to_llvm_jit_from_fast_jit)
  1652. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1653. #endif
  1654. wasm_runtime_free(type);
  1655. }
  1656. #endif /* end of WASM_ENABLE_GC != 0 */
  1657. static bool
  1658. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1659. char *error_buf, uint32 error_buf_size)
  1660. {
  1661. const uint8 *p = buf, *p_end = buf_end;
  1662. uint32 type_count, i;
  1663. uint64 total_size;
  1664. uint8 flag;
  1665. #if WASM_ENABLE_GC != 0
  1666. uint32 processed_type_count = 0;
  1667. #endif
  1668. read_leb_uint32(p, p_end, type_count);
  1669. if (type_count) {
  1670. module->type_count = type_count;
  1671. total_size = sizeof(WASMType *) * (uint64)type_count;
  1672. if (!(module->types =
  1673. loader_malloc(total_size, error_buf, error_buf_size))) {
  1674. return false;
  1675. }
  1676. #if WASM_ENABLE_GC == 0
  1677. for (i = 0; i < type_count; i++) {
  1678. WASMFuncType *type;
  1679. const uint8 *p_org;
  1680. uint32 param_count, result_count, j;
  1681. uint32 param_cell_num, ret_cell_num;
  1682. CHECK_BUF(p, p_end, 1);
  1683. flag = read_uint8(p);
  1684. if (flag != 0x60) {
  1685. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1686. return false;
  1687. }
  1688. read_leb_uint32(p, p_end, param_count);
  1689. /* Resolve param count and result count firstly */
  1690. p_org = p;
  1691. CHECK_BUF(p, p_end, param_count);
  1692. p += param_count;
  1693. read_leb_uint32(p, p_end, result_count);
  1694. CHECK_BUF(p, p_end, result_count);
  1695. p = p_org;
  1696. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1697. set_error_buf(error_buf, error_buf_size,
  1698. "param count or result count too large");
  1699. return false;
  1700. }
  1701. total_size = offsetof(WASMFuncType, types)
  1702. + sizeof(uint8) * (uint64)(param_count + result_count);
  1703. if (!(type = module->types[i] =
  1704. loader_malloc(total_size, error_buf, error_buf_size))) {
  1705. return false;
  1706. }
  1707. /* Resolve param types and result types */
  1708. type->ref_count = 1;
  1709. type->param_count = (uint16)param_count;
  1710. type->result_count = (uint16)result_count;
  1711. for (j = 0; j < param_count; j++) {
  1712. CHECK_BUF(p, p_end, 1);
  1713. type->types[j] = read_uint8(p);
  1714. }
  1715. read_leb_uint32(p, p_end, result_count);
  1716. for (j = 0; j < result_count; j++) {
  1717. CHECK_BUF(p, p_end, 1);
  1718. type->types[param_count + j] = read_uint8(p);
  1719. }
  1720. for (j = 0; j < param_count + result_count; j++) {
  1721. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  1722. set_error_buf(error_buf, error_buf_size,
  1723. "unknown value type");
  1724. return false;
  1725. }
  1726. }
  1727. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1728. ret_cell_num =
  1729. wasm_get_cell_num(type->types + param_count, result_count);
  1730. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1731. set_error_buf(error_buf, error_buf_size,
  1732. "param count or result count too large");
  1733. return false;
  1734. }
  1735. type->param_cell_num = (uint16)param_cell_num;
  1736. type->ret_cell_num = (uint16)ret_cell_num;
  1737. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1738. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1739. #endif
  1740. #if WASM_ENABLE_WAMR_COMPILER != 0
  1741. for (j = 0; j < type->param_count + type->result_count; j++) {
  1742. if (type->types[j] == VALUE_TYPE_V128)
  1743. module->is_simd_used = true;
  1744. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1745. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1746. module->is_ref_types_used = true;
  1747. }
  1748. #endif
  1749. /* If there is already a same type created, use it instead */
  1750. for (j = 0; j < i; j++) {
  1751. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1752. if (module->types[j]->ref_count == UINT16_MAX) {
  1753. set_error_buf(error_buf, error_buf_size,
  1754. "wasm type's ref count too large");
  1755. return false;
  1756. }
  1757. destroy_wasm_type(type);
  1758. module->types[i] = module->types[j];
  1759. module->types[j]->ref_count++;
  1760. break;
  1761. }
  1762. }
  1763. }
  1764. #else /* else of WASM_ENABLE_GC == 0 */
  1765. for (i = 0; i < type_count; i++) {
  1766. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1767. uint32 rec_count = 1, j;
  1768. bool is_sub_final = true;
  1769. CHECK_BUF(p, p_end, 1);
  1770. flag = read_uint8(p);
  1771. if (flag == DEFINED_TYPE_REC) {
  1772. read_leb_uint32(p, p_end, rec_count);
  1773. if (rec_count > 1) {
  1774. uint64 new_total_size;
  1775. /* integer overflow */
  1776. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1777. set_error_buf(error_buf, error_buf_size,
  1778. "recursive type count too large");
  1779. return false;
  1780. }
  1781. module->type_count += rec_count - 1;
  1782. new_total_size =
  1783. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1784. if (new_total_size > UINT32_MAX) {
  1785. set_error_buf(error_buf, error_buf_size,
  1786. "allocate memory failed");
  1787. return false;
  1788. }
  1789. MEM_REALLOC(module->types, (uint32)total_size,
  1790. (uint32)new_total_size);
  1791. total_size = new_total_size;
  1792. }
  1793. LOG_VERBOSE("Processing rec group [%d-%d]",
  1794. processed_type_count,
  1795. processed_type_count + rec_count - 1);
  1796. }
  1797. else {
  1798. p--;
  1799. }
  1800. for (j = 0; j < rec_count; j++) {
  1801. WASMType *cur_type = NULL;
  1802. CHECK_BUF(p, p_end, 1);
  1803. flag = read_uint8(p);
  1804. parent_type_idx = -1;
  1805. if (flag == DEFINED_TYPE_SUB
  1806. || flag == DEFINED_TYPE_SUB_FINAL) {
  1807. read_leb_uint32(p, p_end, super_type_count);
  1808. if (super_type_count > 1) {
  1809. set_error_buf(error_buf, error_buf_size,
  1810. "super type count too large");
  1811. return false;
  1812. }
  1813. if (super_type_count > 0) {
  1814. read_leb_uint32(p, p_end, parent_type_idx);
  1815. if (parent_type_idx >= processed_type_count + j) {
  1816. set_error_buf_v(error_buf, error_buf_size,
  1817. "unknown type %d", parent_type_idx);
  1818. return false;
  1819. }
  1820. if (module->types[parent_type_idx]->is_sub_final) {
  1821. set_error_buf(error_buf, error_buf_size,
  1822. "sub type can not inherit from "
  1823. "a final super type");
  1824. return false;
  1825. }
  1826. }
  1827. if (flag == DEFINED_TYPE_SUB)
  1828. is_sub_final = false;
  1829. CHECK_BUF(p, p_end, 1);
  1830. flag = read_uint8(p);
  1831. }
  1832. if (flag == DEFINED_TYPE_FUNC) {
  1833. if (!resolve_func_type(&p, buf_end, module,
  1834. processed_type_count + rec_count,
  1835. processed_type_count + j, error_buf,
  1836. error_buf_size)) {
  1837. return false;
  1838. }
  1839. }
  1840. else if (flag == DEFINED_TYPE_STRUCT) {
  1841. if (!resolve_struct_type(&p, buf_end, module,
  1842. processed_type_count + rec_count,
  1843. processed_type_count + j,
  1844. error_buf, error_buf_size)) {
  1845. return false;
  1846. }
  1847. }
  1848. else if (flag == DEFINED_TYPE_ARRAY) {
  1849. if (!resolve_array_type(&p, buf_end, module,
  1850. processed_type_count + rec_count,
  1851. processed_type_count + j, error_buf,
  1852. error_buf_size)) {
  1853. return false;
  1854. }
  1855. }
  1856. else {
  1857. set_error_buf(error_buf, error_buf_size,
  1858. "invalid type flag");
  1859. return false;
  1860. }
  1861. cur_type = module->types[processed_type_count + j];
  1862. cur_type->ref_count = 1;
  1863. cur_type->parent_type_idx = parent_type_idx;
  1864. cur_type->is_sub_final = is_sub_final;
  1865. cur_type->rec_count = rec_count;
  1866. cur_type->rec_idx = j;
  1867. cur_type->rec_begin_type_idx = processed_type_count;
  1868. }
  1869. /* resolve subtyping relationship in current rec group */
  1870. for (j = 0; j < rec_count; j++) {
  1871. WASMType *cur_type = module->types[processed_type_count + j];
  1872. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1873. WASMType *parent_type =
  1874. module->types[cur_type->parent_type_idx];
  1875. cur_type->parent_type = parent_type;
  1876. cur_type->root_type = parent_type->root_type;
  1877. if (parent_type->inherit_depth == UINT16_MAX) {
  1878. set_error_buf(error_buf, error_buf_size,
  1879. "parent type's inherit depth too large");
  1880. return false;
  1881. }
  1882. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1883. }
  1884. else {
  1885. cur_type->parent_type = NULL;
  1886. cur_type->root_type = cur_type;
  1887. cur_type->inherit_depth = 0;
  1888. }
  1889. }
  1890. for (j = 0; j < rec_count; j++) {
  1891. WASMType *cur_type = module->types[processed_type_count + j];
  1892. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1893. WASMType *parent_type =
  1894. module->types[cur_type->parent_type_idx];
  1895. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1896. module->types,
  1897. module->type_count)) {
  1898. set_error_buf(error_buf, error_buf_size,
  1899. "sub type does not match super type");
  1900. return false;
  1901. }
  1902. }
  1903. }
  1904. /* If there is already an equivalence type or a group of equivalence
  1905. recursive types created, use it or them instead */
  1906. for (j = 0; j < processed_type_count;) {
  1907. WASMType *src_type = module->types[j];
  1908. WASMType *cur_type = module->types[processed_type_count];
  1909. uint32 k, src_rec_count;
  1910. src_rec_count = src_type->rec_count;
  1911. if (src_rec_count != rec_count) {
  1912. /* no type equivalence */
  1913. j += src_rec_count;
  1914. continue;
  1915. }
  1916. for (k = 0; k < rec_count; k++) {
  1917. src_type = module->types[j + k];
  1918. cur_type = module->types[processed_type_count + k];
  1919. if (!wasm_type_equal(src_type, cur_type, module->types,
  1920. module->type_count)) {
  1921. break;
  1922. }
  1923. }
  1924. if (k < rec_count) {
  1925. /* no type equivalence */
  1926. j += src_rec_count;
  1927. continue;
  1928. }
  1929. /* type equivalence */
  1930. for (k = 0; k < rec_count; k++) {
  1931. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1932. set_error_buf(error_buf, error_buf_size,
  1933. "wasm type's ref count too large");
  1934. return false;
  1935. }
  1936. destroy_wasm_type(module->types[processed_type_count + k]);
  1937. module->types[processed_type_count + k] =
  1938. module->types[j + k];
  1939. module->types[j + k]->ref_count++;
  1940. }
  1941. break;
  1942. }
  1943. if (rec_count > 1) {
  1944. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1945. processed_type_count,
  1946. processed_type_count + rec_count - 1);
  1947. }
  1948. processed_type_count += rec_count;
  1949. }
  1950. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1951. * module->type_count,
  1952. error_buf, error_buf_size))) {
  1953. return false;
  1954. }
  1955. #endif /* end of WASM_ENABLE_GC == 0 */
  1956. }
  1957. if (p != p_end) {
  1958. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1959. return false;
  1960. }
  1961. LOG_VERBOSE("Load type section success.\n");
  1962. return true;
  1963. fail:
  1964. return false;
  1965. }
  1966. static void
  1967. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1968. {
  1969. uint32 default_max_size;
  1970. if (UINT32_MAX / 2 > init_size)
  1971. default_max_size = init_size * 2;
  1972. else
  1973. default_max_size = UINT32_MAX;
  1974. if (default_max_size < WASM_TABLE_MAX_SIZE)
  1975. default_max_size = WASM_TABLE_MAX_SIZE;
  1976. if (max_size_flag) {
  1977. /* module defines the table limitation */
  1978. bh_assert(init_size <= *max_size);
  1979. if (init_size < *max_size) {
  1980. *max_size =
  1981. *max_size < default_max_size ? *max_size : default_max_size;
  1982. }
  1983. }
  1984. else {
  1985. /* partial defined table limitation, gives a default value */
  1986. *max_size = default_max_size;
  1987. }
  1988. }
  1989. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  1990. /**
  1991. * Find export item of a module with export info:
  1992. * module name, field name and export kind
  1993. */
  1994. static WASMExport *
  1995. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  1996. const char *field_name, uint8 export_kind,
  1997. char *error_buf, uint32 error_buf_size)
  1998. {
  1999. WASMExport *export =
  2000. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2001. export_kind, error_buf, error_buf_size);
  2002. return export;
  2003. }
  2004. #endif
  2005. #if WASM_ENABLE_MULTI_MODULE != 0
  2006. static WASMFunction *
  2007. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  2008. const WASMFuncType *expected_function_type,
  2009. char *error_buf, uint32 error_buf_size)
  2010. {
  2011. WASMModuleCommon *module_reg;
  2012. WASMFunction *function = NULL;
  2013. WASMExport *export = NULL;
  2014. WASMModule *module = NULL;
  2015. WASMFuncType *target_function_type = NULL;
  2016. module_reg = wasm_runtime_find_module_registered(module_name);
  2017. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2018. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  2019. function_name);
  2020. set_error_buf(error_buf, error_buf_size, "unknown import");
  2021. return NULL;
  2022. }
  2023. module = (WASMModule *)module_reg;
  2024. export =
  2025. wasm_loader_find_export(module, module_name, function_name,
  2026. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  2027. if (!export) {
  2028. return NULL;
  2029. }
  2030. /* resolve function type and function */
  2031. if (export->index < module->import_function_count) {
  2032. target_function_type =
  2033. module->import_functions[export->index].u.function.func_type;
  2034. function = module->import_functions[export->index]
  2035. .u.function.import_func_linked;
  2036. }
  2037. else {
  2038. target_function_type =
  2039. module->functions[export->index - module->import_function_count]
  2040. ->func_type;
  2041. function =
  2042. module->functions[export->index - module->import_function_count];
  2043. }
  2044. /* check function type */
  2045. if (!wasm_type_equal((WASMType *)expected_function_type,
  2046. (WASMType *)target_function_type, module->types,
  2047. module->type_count)) {
  2048. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  2049. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2050. return NULL;
  2051. }
  2052. return function;
  2053. }
  2054. static WASMTable *
  2055. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2056. uint32 init_size, uint32 max_size, char *error_buf,
  2057. uint32 error_buf_size)
  2058. {
  2059. WASMModuleCommon *module_reg;
  2060. WASMTable *table = NULL;
  2061. WASMExport *export = NULL;
  2062. WASMModule *module = NULL;
  2063. module_reg = wasm_runtime_find_module_registered(module_name);
  2064. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2065. LOG_DEBUG("can not find a module named %s for table", module_name);
  2066. set_error_buf(error_buf, error_buf_size, "unknown import");
  2067. return NULL;
  2068. }
  2069. module = (WASMModule *)module_reg;
  2070. export =
  2071. wasm_loader_find_export(module, module_name, table_name,
  2072. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2073. if (!export) {
  2074. return NULL;
  2075. }
  2076. /* resolve table and check the init/max size */
  2077. if (export->index < module->import_table_count) {
  2078. table =
  2079. module->import_tables[export->index].u.table.import_table_linked;
  2080. }
  2081. else {
  2082. table = &(module->tables[export->index - module->import_table_count]);
  2083. }
  2084. if (table->table_type.init_size < init_size
  2085. || table->table_type.max_size > max_size) {
  2086. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2087. module_name, table_name, table->table_type.init_size,
  2088. table->table_type.max_size, init_size, max_size);
  2089. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2090. return NULL;
  2091. }
  2092. return table;
  2093. }
  2094. static WASMMemory *
  2095. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2096. uint32 init_page_count, uint32 max_page_count,
  2097. char *error_buf, uint32 error_buf_size)
  2098. {
  2099. WASMModuleCommon *module_reg;
  2100. WASMMemory *memory = NULL;
  2101. WASMExport *export = NULL;
  2102. WASMModule *module = NULL;
  2103. module_reg = wasm_runtime_find_module_registered(module_name);
  2104. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2105. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2106. set_error_buf(error_buf, error_buf_size, "unknown import");
  2107. return NULL;
  2108. }
  2109. module = (WASMModule *)module_reg;
  2110. export =
  2111. wasm_loader_find_export(module, module_name, memory_name,
  2112. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2113. if (!export) {
  2114. return NULL;
  2115. }
  2116. /* resolve memory and check the init/max page count */
  2117. if (export->index < module->import_memory_count) {
  2118. memory = module->import_memories[export->index]
  2119. .u.memory.import_memory_linked;
  2120. }
  2121. else {
  2122. memory =
  2123. &(module->memories[export->index - module->import_memory_count]);
  2124. }
  2125. if (memory->init_page_count < init_page_count
  2126. || memory->max_page_count > max_page_count) {
  2127. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2128. module_name, memory_name, memory->init_page_count,
  2129. memory->max_page_count, init_page_count, max_page_count);
  2130. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2131. return NULL;
  2132. }
  2133. return memory;
  2134. }
  2135. static WASMGlobal *
  2136. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2137. uint8 type, bool is_mutable, char *error_buf,
  2138. uint32 error_buf_size)
  2139. {
  2140. WASMModuleCommon *module_reg;
  2141. WASMGlobal *global = NULL;
  2142. WASMExport *export = NULL;
  2143. WASMModule *module = NULL;
  2144. module_reg = wasm_runtime_find_module_registered(module_name);
  2145. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2146. LOG_DEBUG("can not find a module named %s for global", module_name);
  2147. set_error_buf(error_buf, error_buf_size, "unknown import");
  2148. return NULL;
  2149. }
  2150. module = (WASMModule *)module_reg;
  2151. export =
  2152. wasm_loader_find_export(module, module_name, global_name,
  2153. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2154. if (!export) {
  2155. return NULL;
  2156. }
  2157. /* resolve and check the global */
  2158. if (export->index < module->import_global_count) {
  2159. global =
  2160. module->import_globals[export->index].u.global.import_global_linked;
  2161. }
  2162. else {
  2163. global =
  2164. &(module->globals[export->index - module->import_global_count]);
  2165. }
  2166. if (global->type.val_type != type
  2167. || global->type.is_mutable != is_mutable) {
  2168. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2169. module_name, global_name, global->type.val_type,
  2170. global->type.is_mutable, type, is_mutable);
  2171. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2172. return NULL;
  2173. }
  2174. return global;
  2175. }
  2176. #if WASM_ENABLE_TAGS != 0
  2177. static WASMTag *
  2178. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2179. const WASMType *expected_tag_type,
  2180. uint32 *linked_tag_index, char *error_buf,
  2181. uint32 error_buf_size)
  2182. {
  2183. WASMModuleCommon *module_reg;
  2184. WASMTag *tag = NULL;
  2185. WASMExport *export = NULL;
  2186. WASMModule *module = NULL;
  2187. module_reg = wasm_runtime_find_module_registered(module_name);
  2188. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2189. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2190. tag_name);
  2191. set_error_buf(error_buf, error_buf_size, "unknown import");
  2192. return NULL;
  2193. }
  2194. module = (WASMModule *)module_reg;
  2195. export =
  2196. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2197. error_buf, error_buf_size);
  2198. if (!export) {
  2199. return NULL;
  2200. }
  2201. /* resolve tag type and tag */
  2202. if (export->index < module->import_tag_count) {
  2203. /* importing an imported tag from the submodule */
  2204. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2205. }
  2206. else {
  2207. /* importing an section tag from the submodule */
  2208. tag = module->tags[export->index - module->import_tag_count];
  2209. }
  2210. /* check function type */
  2211. if (!wasm_type_equal(expected_tag_type, tag->tag_type)) {
  2212. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2213. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2214. return NULL;
  2215. }
  2216. if (linked_tag_index != NULL) {
  2217. *linked_tag_index = export->index;
  2218. }
  2219. return tag;
  2220. }
  2221. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2222. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2223. static bool
  2224. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2225. const WASMModule *parent_module,
  2226. const char *sub_module_name, const char *function_name,
  2227. WASMFunctionImport *function, char *error_buf,
  2228. uint32 error_buf_size)
  2229. {
  2230. const uint8 *p = *p_buf, *p_end = buf_end;
  2231. uint32 declare_type_index = 0;
  2232. WASMFuncType *declare_func_type = NULL;
  2233. WASMFunction *linked_func = NULL;
  2234. #if WASM_ENABLE_MULTI_MODULE != 0
  2235. WASMModule *sub_module = NULL;
  2236. bool is_built_in_module = false;
  2237. #endif
  2238. const char *linked_signature = NULL;
  2239. void *linked_attachment = NULL;
  2240. bool linked_call_conv_raw = false;
  2241. bool is_native_symbol = false;
  2242. read_leb_uint32(p, p_end, declare_type_index);
  2243. *p_buf = p;
  2244. if (declare_type_index >= parent_module->type_count) {
  2245. set_error_buf(error_buf, error_buf_size, "unknown type");
  2246. return false;
  2247. }
  2248. #if WASM_ENABLE_GC != 0
  2249. function->type_idx = declare_type_index;
  2250. #endif
  2251. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2252. declare_type_index = wasm_get_smallest_type_idx(
  2253. parent_module->types, parent_module->type_count, declare_type_index);
  2254. #endif
  2255. declare_func_type =
  2256. (WASMFuncType *)parent_module->types[declare_type_index];
  2257. /* lookup registered native symbols first */
  2258. linked_func = wasm_native_resolve_symbol(
  2259. sub_module_name, function_name, declare_func_type, &linked_signature,
  2260. &linked_attachment, &linked_call_conv_raw);
  2261. if (linked_func) {
  2262. is_native_symbol = true;
  2263. }
  2264. #if WASM_ENABLE_MULTI_MODULE != 0
  2265. else {
  2266. if (!(is_built_in_module =
  2267. wasm_runtime_is_built_in_module(sub_module_name))) {
  2268. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2269. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2270. error_buf_size);
  2271. }
  2272. if (is_built_in_module || sub_module)
  2273. linked_func = wasm_loader_resolve_function(
  2274. sub_module_name, function_name, declare_func_type, error_buf,
  2275. error_buf_size);
  2276. }
  2277. #endif
  2278. function->module_name = (char *)sub_module_name;
  2279. function->field_name = (char *)function_name;
  2280. function->func_type = declare_func_type;
  2281. /* func_ptr_linked is for native registered symbol */
  2282. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  2283. function->signature = linked_signature;
  2284. function->attachment = linked_attachment;
  2285. function->call_conv_raw = linked_call_conv_raw;
  2286. #if WASM_ENABLE_MULTI_MODULE != 0
  2287. function->import_module = is_native_symbol ? NULL : sub_module;
  2288. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  2289. #endif
  2290. return true;
  2291. fail:
  2292. return false;
  2293. }
  2294. static bool
  2295. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2296. uint32 error_buf_size)
  2297. {
  2298. if (max_size < init_size) {
  2299. set_error_buf(error_buf, error_buf_size,
  2300. "size minimum must not be greater than maximum");
  2301. return false;
  2302. }
  2303. return true;
  2304. }
  2305. static bool
  2306. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2307. WASMModule *parent_module, const char *sub_module_name,
  2308. const char *table_name, WASMTableImport *table,
  2309. char *error_buf, uint32 error_buf_size)
  2310. {
  2311. const uint8 *p = *p_buf, *p_end = buf_end;
  2312. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  2313. declare_init_size = 0, declare_max_size = 0;
  2314. #if WASM_ENABLE_MULTI_MODULE != 0
  2315. WASMModule *sub_module = NULL;
  2316. WASMTable *linked_table = NULL;
  2317. #endif
  2318. #if WASM_ENABLE_GC != 0
  2319. WASMRefType ref_type;
  2320. bool need_ref_type_map;
  2321. #endif
  2322. #if WASM_ENABLE_GC == 0
  2323. CHECK_BUF(p, p_end, 1);
  2324. /* 0x70 or 0x6F */
  2325. declare_elem_type = read_uint8(p);
  2326. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2327. #if WASM_ENABLE_REF_TYPES != 0
  2328. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2329. #endif
  2330. ) {
  2331. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2332. return false;
  2333. }
  2334. #else /* else of WASM_ENABLE_GC == 0 */
  2335. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2336. &need_ref_type_map, &ref_type, false, error_buf,
  2337. error_buf_size)) {
  2338. return false;
  2339. }
  2340. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2341. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2342. return false;
  2343. }
  2344. declare_elem_type = ref_type.ref_type;
  2345. if (need_ref_type_map) {
  2346. if (!(table->table_type.elem_ref_type =
  2347. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2348. error_buf, error_buf_size))) {
  2349. return false;
  2350. }
  2351. }
  2352. #if TRACE_WASM_LOADER != 0
  2353. os_printf("import table type: ");
  2354. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2355. os_printf("\n");
  2356. #endif
  2357. #endif /* end of WASM_ENABLE_GC == 0 */
  2358. read_leb_uint32(p, p_end, declare_max_size_flag);
  2359. if (declare_max_size_flag > 1) {
  2360. set_error_buf(error_buf, error_buf_size, "integer too large");
  2361. return false;
  2362. }
  2363. read_leb_uint32(p, p_end, declare_init_size);
  2364. if (declare_max_size_flag) {
  2365. read_leb_uint32(p, p_end, declare_max_size);
  2366. if (!check_table_max_size(declare_init_size, declare_max_size,
  2367. error_buf, error_buf_size))
  2368. return false;
  2369. }
  2370. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  2371. &declare_max_size);
  2372. *p_buf = p;
  2373. #if WASM_ENABLE_MULTI_MODULE != 0
  2374. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2375. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2376. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2377. error_buf_size);
  2378. if (sub_module) {
  2379. linked_table = wasm_loader_resolve_table(
  2380. sub_module_name, table_name, declare_init_size,
  2381. declare_max_size, error_buf, error_buf_size);
  2382. if (linked_table) {
  2383. /* reset with linked table limit */
  2384. declare_elem_type = linked_table->table_type.elem_type;
  2385. declare_init_size = linked_table->table_type.init_size;
  2386. declare_max_size = linked_table->table_type.max_size;
  2387. declare_max_size_flag = linked_table->table_type.flags;
  2388. table->import_table_linked = linked_table;
  2389. table->import_module = sub_module;
  2390. }
  2391. }
  2392. }
  2393. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2394. /* (table (export "table") 10 20 funcref) */
  2395. /* we need this section working in wamrc */
  2396. if (!strcmp("spectest", sub_module_name)) {
  2397. const uint32 spectest_table_init_size = 10;
  2398. const uint32 spectest_table_max_size = 20;
  2399. if (strcmp("table", table_name)) {
  2400. set_error_buf(error_buf, error_buf_size,
  2401. "incompatible import type or unknown import");
  2402. return false;
  2403. }
  2404. if (declare_init_size > spectest_table_init_size
  2405. || declare_max_size < spectest_table_max_size) {
  2406. set_error_buf(error_buf, error_buf_size,
  2407. "incompatible import type");
  2408. return false;
  2409. }
  2410. declare_init_size = spectest_table_init_size;
  2411. declare_max_size = spectest_table_max_size;
  2412. }
  2413. /* now we believe all declaration are ok */
  2414. table->table_type.elem_type = declare_elem_type;
  2415. table->table_type.init_size = declare_init_size;
  2416. table->table_type.flags = declare_max_size_flag;
  2417. table->table_type.max_size = declare_max_size;
  2418. #if WASM_ENABLE_WAMR_COMPILER != 0
  2419. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2420. parent_module->is_ref_types_used = true;
  2421. #endif
  2422. (void)parent_module;
  2423. return true;
  2424. fail:
  2425. return false;
  2426. }
  2427. static bool
  2428. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2429. uint32 error_buf_size)
  2430. {
  2431. uint32 default_max_size =
  2432. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2433. if (!is_memory64 && init_size > default_max_size) {
  2434. set_error_buf(error_buf, error_buf_size,
  2435. "memory size must be at most 65536 pages (4GiB)");
  2436. return false;
  2437. }
  2438. #if WASM_ENABLE_MEMORY64 != 0
  2439. else if (is_memory64 && init_size > default_max_size) {
  2440. set_error_buf(
  2441. error_buf, error_buf_size,
  2442. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2443. return false;
  2444. }
  2445. #endif
  2446. return true;
  2447. }
  2448. static bool
  2449. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2450. char *error_buf, uint32 error_buf_size)
  2451. {
  2452. uint32 default_max_size =
  2453. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2454. if (max_size < init_size) {
  2455. set_error_buf(error_buf, error_buf_size,
  2456. "size minimum must not be greater than maximum");
  2457. return false;
  2458. }
  2459. if (!is_memory64 && max_size > default_max_size) {
  2460. set_error_buf(error_buf, error_buf_size,
  2461. "memory size must be at most 65536 pages (4GiB)");
  2462. return false;
  2463. }
  2464. #if WASM_ENABLE_MEMORY64 != 0
  2465. else if (is_memory64 && max_size > default_max_size) {
  2466. set_error_buf(
  2467. error_buf, error_buf_size,
  2468. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2469. return false;
  2470. }
  2471. #endif
  2472. return true;
  2473. }
  2474. static bool
  2475. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2476. WASMModule *parent_module, const char *sub_module_name,
  2477. const char *memory_name, WASMMemoryImport *memory,
  2478. char *error_buf, uint32 error_buf_size)
  2479. {
  2480. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2481. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2482. uint32 pool_size = wasm_runtime_memory_pool_size();
  2483. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2484. / DEFAULT_NUM_BYTES_PER_PAGE;
  2485. #else
  2486. uint32 max_page_count;
  2487. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2488. uint32 mem_flag = 0;
  2489. bool is_memory64 = false;
  2490. uint32 declare_init_page_count = 0;
  2491. uint32 declare_max_page_count = 0;
  2492. #if WASM_ENABLE_MULTI_MODULE != 0
  2493. WASMModule *sub_module = NULL;
  2494. WASMMemory *linked_memory = NULL;
  2495. #endif
  2496. p_org = p;
  2497. read_leb_uint32(p, p_end, mem_flag);
  2498. is_memory64 = mem_flag & MEMORY64_FLAG;
  2499. if (p - p_org > 1) {
  2500. LOG_VERBOSE("integer representation too long");
  2501. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2502. return false;
  2503. }
  2504. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2505. return false;
  2506. }
  2507. read_leb_uint32(p, p_end, declare_init_page_count);
  2508. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2509. error_buf_size)) {
  2510. return false;
  2511. }
  2512. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2513. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2514. #endif
  2515. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2516. read_leb_uint32(p, p_end, declare_max_page_count);
  2517. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2518. declare_max_page_count, error_buf,
  2519. error_buf_size)) {
  2520. return false;
  2521. }
  2522. if (declare_max_page_count > max_page_count) {
  2523. declare_max_page_count = max_page_count;
  2524. }
  2525. }
  2526. else {
  2527. /* Limit the maximum memory size to max_page_count */
  2528. declare_max_page_count = max_page_count;
  2529. }
  2530. #if WASM_ENABLE_MULTI_MODULE != 0
  2531. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2532. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2533. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2534. error_buf_size);
  2535. if (sub_module) {
  2536. linked_memory = wasm_loader_resolve_memory(
  2537. sub_module_name, memory_name, declare_init_page_count,
  2538. declare_max_page_count, error_buf, error_buf_size);
  2539. if (linked_memory) {
  2540. /**
  2541. * reset with linked memory limit
  2542. */
  2543. memory->import_module = sub_module;
  2544. memory->import_memory_linked = linked_memory;
  2545. declare_init_page_count = linked_memory->init_page_count;
  2546. declare_max_page_count = linked_memory->max_page_count;
  2547. }
  2548. }
  2549. }
  2550. #endif
  2551. /* (memory (export "memory") 1 2) */
  2552. if (!strcmp("spectest", sub_module_name)) {
  2553. uint32 spectest_memory_init_page = 1;
  2554. uint32 spectest_memory_max_page = 2;
  2555. if (strcmp("memory", memory_name)) {
  2556. set_error_buf(error_buf, error_buf_size,
  2557. "incompatible import type or unknown import");
  2558. return false;
  2559. }
  2560. if (declare_init_page_count > spectest_memory_init_page
  2561. || declare_max_page_count < spectest_memory_max_page) {
  2562. set_error_buf(error_buf, error_buf_size,
  2563. "incompatible import type");
  2564. return false;
  2565. }
  2566. declare_init_page_count = spectest_memory_init_page;
  2567. declare_max_page_count = spectest_memory_max_page;
  2568. }
  2569. #if WASM_ENABLE_WASI_TEST != 0
  2570. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2571. else if (!strcmp("foo", sub_module_name)) {
  2572. uint32 spectest_memory_init_page = 1;
  2573. uint32 spectest_memory_max_page = 1;
  2574. if (strcmp("bar", memory_name)) {
  2575. set_error_buf(error_buf, error_buf_size,
  2576. "incompatible import type or unknown import");
  2577. return false;
  2578. }
  2579. if (declare_init_page_count > spectest_memory_init_page
  2580. || declare_max_page_count < spectest_memory_max_page) {
  2581. set_error_buf(error_buf, error_buf_size,
  2582. "incompatible import type");
  2583. return false;
  2584. }
  2585. declare_init_page_count = spectest_memory_init_page;
  2586. declare_max_page_count = spectest_memory_max_page;
  2587. }
  2588. #endif
  2589. /* now we believe all declaration are ok */
  2590. memory->mem_type.flags = mem_flag;
  2591. memory->mem_type.init_page_count = declare_init_page_count;
  2592. memory->mem_type.max_page_count = declare_max_page_count;
  2593. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2594. *p_buf = p;
  2595. (void)parent_module;
  2596. return true;
  2597. fail:
  2598. return false;
  2599. }
  2600. #if WASM_ENABLE_TAGS != 0
  2601. static bool
  2602. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2603. const WASMModule *parent_module, /* this module ! */
  2604. const char *sub_module_name, const char *tag_name,
  2605. WASMTagImport *tag, /* structure to fill */
  2606. char *error_buf, uint32 error_buf_size)
  2607. {
  2608. /* attribute and type of the import statement */
  2609. uint8 declare_tag_attribute;
  2610. uint32 declare_type_index;
  2611. const uint8 *p = *p_buf, *p_end = buf_end;
  2612. #if WASM_ENABLE_MULTI_MODULE != 0
  2613. WASMModule *sub_module = NULL;
  2614. #endif
  2615. /* get the one byte attribute */
  2616. CHECK_BUF(p, p_end, 1);
  2617. declare_tag_attribute = read_uint8(p);
  2618. if (declare_tag_attribute != 0) {
  2619. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2620. goto fail;
  2621. }
  2622. /* get type */
  2623. read_leb_uint32(p, p_end, declare_type_index);
  2624. /* compare against module->types */
  2625. if (declare_type_index >= parent_module->type_count) {
  2626. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2627. goto fail;
  2628. }
  2629. WASMFuncType *declare_tag_type =
  2630. (WASMFuncType *)parent_module->types[declare_type_index];
  2631. /* check, that the type of the declared tag returns void */
  2632. if (declare_tag_type->result_count != 0) {
  2633. set_error_buf(error_buf, error_buf_size,
  2634. "tag type signature does not return void");
  2635. goto fail;
  2636. }
  2637. #if WASM_ENABLE_MULTI_MODULE != 0
  2638. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2639. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2640. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2641. error_buf_size);
  2642. if (sub_module) {
  2643. /* wasm_loader_resolve_tag checks, that the imported tag
  2644. * and the declared tag have the same type
  2645. */
  2646. uint32 linked_tag_index = 0;
  2647. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2648. sub_module_name, tag_name, declare_tag_type,
  2649. &linked_tag_index /* out */, error_buf, error_buf_size);
  2650. if (linked_tag) {
  2651. tag->import_module = sub_module;
  2652. tag->import_tag_linked = linked_tag;
  2653. tag->import_tag_index_linked = linked_tag_index;
  2654. }
  2655. }
  2656. }
  2657. #endif
  2658. /* store to module tag declarations */
  2659. tag->attribute = declare_tag_attribute;
  2660. tag->type = declare_type_index;
  2661. tag->module_name = (char *)sub_module_name;
  2662. tag->field_name = (char *)tag_name;
  2663. tag->tag_type = declare_tag_type;
  2664. *p_buf = p;
  2665. (void)parent_module;
  2666. LOG_VERBOSE("Load tag import success\n");
  2667. return true;
  2668. fail:
  2669. return false;
  2670. }
  2671. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2672. static bool
  2673. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2674. WASMModule *parent_module, char *sub_module_name,
  2675. char *global_name, WASMGlobalImport *global, char *error_buf,
  2676. uint32 error_buf_size)
  2677. {
  2678. const uint8 *p = *p_buf, *p_end = buf_end;
  2679. uint8 declare_type = 0;
  2680. uint8 declare_mutable = 0;
  2681. #if WASM_ENABLE_MULTI_MODULE != 0
  2682. WASMModule *sub_module = NULL;
  2683. WASMGlobal *linked_global = NULL;
  2684. #endif
  2685. #if WASM_ENABLE_GC != 0
  2686. WASMRefType ref_type;
  2687. bool need_ref_type_map;
  2688. #endif
  2689. bool ret = false;
  2690. #if WASM_ENABLE_GC == 0
  2691. CHECK_BUF(p, p_end, 2);
  2692. /* global type */
  2693. declare_type = read_uint8(p);
  2694. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2695. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2696. return false;
  2697. }
  2698. declare_mutable = read_uint8(p);
  2699. #else
  2700. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2701. &need_ref_type_map, &ref_type, false, error_buf,
  2702. error_buf_size)) {
  2703. return false;
  2704. }
  2705. declare_type = ref_type.ref_type;
  2706. if (need_ref_type_map) {
  2707. if (!(global->ref_type =
  2708. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2709. error_buf, error_buf_size))) {
  2710. return false;
  2711. }
  2712. }
  2713. #if TRACE_WASM_LOADER != 0
  2714. os_printf("import global type: ");
  2715. wasm_dump_value_type(declare_type, global->ref_type);
  2716. os_printf("\n");
  2717. #endif
  2718. CHECK_BUF(p, p_end, 1);
  2719. declare_mutable = read_uint8(p);
  2720. #endif /* end of WASM_ENABLE_GC == 0 */
  2721. *p_buf = p;
  2722. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2723. return false;
  2724. }
  2725. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2726. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2727. global);
  2728. if (ret) {
  2729. if (global->type.val_type != declare_type
  2730. || global->type.is_mutable != declare_mutable) {
  2731. set_error_buf(error_buf, error_buf_size,
  2732. "incompatible import type");
  2733. return false;
  2734. }
  2735. global->is_linked = true;
  2736. }
  2737. #endif
  2738. #if WASM_ENABLE_MULTI_MODULE != 0
  2739. if (!global->is_linked
  2740. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2741. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2742. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2743. error_buf_size);
  2744. if (sub_module) {
  2745. /* check sub modules */
  2746. linked_global = wasm_loader_resolve_global(
  2747. sub_module_name, global_name, declare_type, declare_mutable,
  2748. error_buf, error_buf_size);
  2749. if (linked_global) {
  2750. global->import_module = sub_module;
  2751. global->import_global_linked = linked_global;
  2752. global->is_linked = true;
  2753. }
  2754. }
  2755. }
  2756. #endif
  2757. global->module_name = sub_module_name;
  2758. global->field_name = global_name;
  2759. global->type.val_type = declare_type;
  2760. global->type.is_mutable = (declare_mutable == 1);
  2761. #if WASM_ENABLE_WAMR_COMPILER != 0
  2762. if (global->type.val_type == VALUE_TYPE_V128)
  2763. parent_module->is_simd_used = true;
  2764. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2765. parent_module->is_ref_types_used = true;
  2766. #endif
  2767. (void)parent_module;
  2768. (void)ret;
  2769. return true;
  2770. fail:
  2771. return false;
  2772. }
  2773. static bool
  2774. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2775. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2776. {
  2777. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2778. #if WASM_ENABLE_GC != 0
  2779. WASMRefType ref_type;
  2780. bool need_ref_type_map;
  2781. #endif
  2782. #if WASM_ENABLE_GC == 0
  2783. CHECK_BUF(p, p_end, 1);
  2784. /* 0x70 or 0x6F */
  2785. table->table_type.elem_type = read_uint8(p);
  2786. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  2787. #if WASM_ENABLE_REF_TYPES != 0
  2788. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  2789. #endif
  2790. ) {
  2791. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2792. return false;
  2793. }
  2794. #else /* else of WASM_ENABLE_GC == 0 */
  2795. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2796. &need_ref_type_map, &ref_type, false, error_buf,
  2797. error_buf_size)) {
  2798. return false;
  2799. }
  2800. table->table_type.elem_type = ref_type.ref_type;
  2801. if (need_ref_type_map) {
  2802. if (!(table->table_type.elem_ref_type =
  2803. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2804. error_buf_size))) {
  2805. return false;
  2806. }
  2807. }
  2808. #if TRACE_WASM_LOADER != 0
  2809. os_printf("table type: ");
  2810. wasm_dump_value_type(table->table_type.elem_type,
  2811. table->table_type.elem_ref_type);
  2812. os_printf("\n");
  2813. #endif
  2814. #endif /* end of WASM_ENABLE_GC == 0 */
  2815. p_org = p;
  2816. read_leb_uint32(p, p_end, table->table_type.flags);
  2817. #if WASM_ENABLE_SHARED_MEMORY == 0
  2818. if (p - p_org > 1) {
  2819. set_error_buf(error_buf, error_buf_size,
  2820. "integer representation too long");
  2821. return false;
  2822. }
  2823. if (table->table_type.flags > 1) {
  2824. set_error_buf(error_buf, error_buf_size, "integer too large");
  2825. return false;
  2826. }
  2827. #else
  2828. if (p - p_org > 1) {
  2829. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2830. return false;
  2831. }
  2832. if (table->table_type.flags == 2) {
  2833. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2834. return false;
  2835. }
  2836. if (table->table_type.flags > 1) {
  2837. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2838. return false;
  2839. }
  2840. #endif
  2841. read_leb_uint32(p, p_end, table->table_type.init_size);
  2842. if (table->table_type.flags) {
  2843. read_leb_uint32(p, p_end, table->table_type.max_size);
  2844. if (!check_table_max_size(table->table_type.init_size,
  2845. table->table_type.max_size, error_buf,
  2846. error_buf_size))
  2847. return false;
  2848. }
  2849. adjust_table_max_size(table->table_type.init_size, table->table_type.flags,
  2850. &table->table_type.max_size);
  2851. #if WASM_ENABLE_WAMR_COMPILER != 0
  2852. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2853. module->is_ref_types_used = true;
  2854. #endif
  2855. *p_buf = p;
  2856. return true;
  2857. fail:
  2858. return false;
  2859. }
  2860. static bool
  2861. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2862. char *error_buf, uint32 error_buf_size)
  2863. {
  2864. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2865. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2866. uint32 pool_size = wasm_runtime_memory_pool_size();
  2867. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2868. / DEFAULT_NUM_BYTES_PER_PAGE;
  2869. #else
  2870. uint32 max_page_count;
  2871. #endif
  2872. bool is_memory64 = false;
  2873. p_org = p;
  2874. read_leb_uint32(p, p_end, memory->flags);
  2875. is_memory64 = memory->flags & MEMORY64_FLAG;
  2876. if (p - p_org > 1) {
  2877. LOG_VERBOSE("integer representation too long");
  2878. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2879. return false;
  2880. }
  2881. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2882. false)) {
  2883. return false;
  2884. }
  2885. read_leb_uint32(p, p_end, memory->init_page_count);
  2886. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2887. error_buf_size))
  2888. return false;
  2889. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2890. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2891. #endif
  2892. if (memory->flags & 1) {
  2893. read_leb_uint32(p, p_end, memory->max_page_count);
  2894. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2895. memory->max_page_count, error_buf,
  2896. error_buf_size))
  2897. return false;
  2898. if (memory->max_page_count > max_page_count)
  2899. memory->max_page_count = max_page_count;
  2900. }
  2901. else {
  2902. /* Limit the maximum memory size to max_page_count */
  2903. memory->max_page_count = max_page_count;
  2904. }
  2905. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2906. *p_buf = p;
  2907. return true;
  2908. fail:
  2909. return false;
  2910. }
  2911. static bool
  2912. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2913. bool is_load_from_file_buf, char *error_buf,
  2914. uint32 error_buf_size)
  2915. {
  2916. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2917. uint32 import_count, name_len, type_index, i, u32, flags;
  2918. uint64 total_size;
  2919. WASMImport *import;
  2920. WASMImport *import_functions = NULL, *import_tables = NULL;
  2921. WASMImport *import_memories = NULL, *import_globals = NULL;
  2922. #if WASM_ENABLE_TAGS != 0
  2923. WASMImport *import_tags = NULL;
  2924. #endif
  2925. char *sub_module_name, *field_name;
  2926. uint8 u8, kind, global_type;
  2927. read_leb_uint32(p, p_end, import_count);
  2928. if (import_count) {
  2929. module->import_count = import_count;
  2930. total_size = sizeof(WASMImport) * (uint64)import_count;
  2931. if (!(module->imports =
  2932. loader_malloc(total_size, error_buf, error_buf_size))) {
  2933. return false;
  2934. }
  2935. p_old = p;
  2936. /* Scan firstly to get import count of each type */
  2937. for (i = 0; i < import_count; i++) {
  2938. /* module name */
  2939. read_leb_uint32(p, p_end, name_len);
  2940. CHECK_BUF(p, p_end, name_len);
  2941. p += name_len;
  2942. /* field name */
  2943. read_leb_uint32(p, p_end, name_len);
  2944. CHECK_BUF(p, p_end, name_len);
  2945. p += name_len;
  2946. CHECK_BUF(p, p_end, 1);
  2947. /* 0x00/0x01/0x02/0x03/0x04 */
  2948. kind = read_uint8(p);
  2949. switch (kind) {
  2950. case IMPORT_KIND_FUNC: /* import function */
  2951. read_leb_uint32(p, p_end, type_index);
  2952. module->import_function_count++;
  2953. break;
  2954. case IMPORT_KIND_TABLE: /* import table */
  2955. CHECK_BUF(p, p_end, 1);
  2956. /* 0x70 */
  2957. u8 = read_uint8(p);
  2958. read_leb_uint32(p, p_end, flags);
  2959. read_leb_uint32(p, p_end, u32);
  2960. if (flags & 1)
  2961. read_leb_uint32(p, p_end, u32);
  2962. module->import_table_count++;
  2963. if (module->import_table_count > 1) {
  2964. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2965. set_error_buf(error_buf, error_buf_size,
  2966. "multiple tables");
  2967. return false;
  2968. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2969. module->is_ref_types_used = true;
  2970. #endif
  2971. }
  2972. break;
  2973. case IMPORT_KIND_MEMORY: /* import memory */
  2974. read_leb_uint32(p, p_end, flags);
  2975. read_leb_uint32(p, p_end, u32);
  2976. if (flags & 1)
  2977. read_leb_uint32(p, p_end, u32);
  2978. module->import_memory_count++;
  2979. #if WASM_ENABLE_MULTI_MEMORY == 0
  2980. if (module->import_memory_count > 1) {
  2981. set_error_buf(error_buf, error_buf_size,
  2982. "multiple memories");
  2983. return false;
  2984. }
  2985. #endif
  2986. break;
  2987. #if WASM_ENABLE_TAGS != 0
  2988. case IMPORT_KIND_TAG: /* import tags */
  2989. /* it only counts the number of tags to import */
  2990. module->import_tag_count++;
  2991. CHECK_BUF(p, p_end, 1);
  2992. u8 = read_uint8(p);
  2993. read_leb_uint32(p, p_end, type_index);
  2994. break;
  2995. #endif
  2996. case IMPORT_KIND_GLOBAL: /* import global */
  2997. #if WASM_ENABLE_GC != 0
  2998. /* valtype */
  2999. CHECK_BUF(p, p_end, 1);
  3000. global_type = read_uint8(p);
  3001. if (wasm_is_type_multi_byte_type(global_type)) {
  3002. int32 heap_type;
  3003. read_leb_int32(p, p_end, heap_type);
  3004. (void)heap_type;
  3005. }
  3006. /* mutability */
  3007. CHECK_BUF(p, p_end, 1);
  3008. p += 1;
  3009. #else
  3010. CHECK_BUF(p, p_end, 2);
  3011. p += 2;
  3012. #endif
  3013. (void)global_type;
  3014. module->import_global_count++;
  3015. break;
  3016. default:
  3017. set_error_buf(error_buf, error_buf_size,
  3018. "invalid import kind");
  3019. return false;
  3020. }
  3021. }
  3022. if (module->import_function_count)
  3023. import_functions = module->import_functions = module->imports;
  3024. if (module->import_table_count)
  3025. import_tables = module->import_tables =
  3026. module->imports + module->import_function_count;
  3027. if (module->import_memory_count)
  3028. import_memories = module->import_memories =
  3029. module->imports + module->import_function_count
  3030. + module->import_table_count;
  3031. #if WASM_ENABLE_TAGS != 0
  3032. if (module->import_tag_count)
  3033. import_tags = module->import_tags =
  3034. module->imports + module->import_function_count
  3035. + module->import_table_count + module->import_memory_count;
  3036. if (module->import_global_count)
  3037. import_globals = module->import_globals =
  3038. module->imports + module->import_function_count
  3039. + module->import_table_count + module->import_memory_count
  3040. + module->import_tag_count;
  3041. #else
  3042. if (module->import_global_count)
  3043. import_globals = module->import_globals =
  3044. module->imports + module->import_function_count
  3045. + module->import_table_count + module->import_memory_count;
  3046. #endif
  3047. p = p_old;
  3048. /* Scan again to resolve the data */
  3049. for (i = 0; i < import_count; i++) {
  3050. /* load module name */
  3051. read_leb_uint32(p, p_end, name_len);
  3052. CHECK_BUF(p, p_end, name_len);
  3053. if (!(sub_module_name = wasm_const_str_list_insert(
  3054. p, name_len, module, is_load_from_file_buf, error_buf,
  3055. error_buf_size))) {
  3056. return false;
  3057. }
  3058. p += name_len;
  3059. /* load field name */
  3060. read_leb_uint32(p, p_end, name_len);
  3061. CHECK_BUF(p, p_end, name_len);
  3062. if (!(field_name = wasm_const_str_list_insert(
  3063. p, name_len, module, is_load_from_file_buf, error_buf,
  3064. error_buf_size))) {
  3065. return false;
  3066. }
  3067. p += name_len;
  3068. CHECK_BUF(p, p_end, 1);
  3069. /* 0x00/0x01/0x02/0x03/0x4 */
  3070. kind = read_uint8(p);
  3071. switch (kind) {
  3072. case IMPORT_KIND_FUNC: /* import function */
  3073. bh_assert(import_functions);
  3074. import = import_functions++;
  3075. if (!load_function_import(
  3076. &p, p_end, module, sub_module_name, field_name,
  3077. &import->u.function, error_buf, error_buf_size)) {
  3078. return false;
  3079. }
  3080. break;
  3081. case IMPORT_KIND_TABLE: /* import table */
  3082. bh_assert(import_tables);
  3083. import = import_tables++;
  3084. if (!load_table_import(&p, p_end, module, sub_module_name,
  3085. field_name, &import->u.table,
  3086. error_buf, error_buf_size)) {
  3087. LOG_DEBUG("can not import such a table (%s,%s)",
  3088. sub_module_name, field_name);
  3089. return false;
  3090. }
  3091. break;
  3092. case IMPORT_KIND_MEMORY: /* import memory */
  3093. bh_assert(import_memories);
  3094. import = import_memories++;
  3095. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3096. field_name, &import->u.memory,
  3097. error_buf, error_buf_size)) {
  3098. return false;
  3099. }
  3100. break;
  3101. #if WASM_ENABLE_TAGS != 0
  3102. case IMPORT_KIND_TAG:
  3103. bh_assert(import_tags);
  3104. import = import_tags++;
  3105. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3106. field_name, &import->u.tag, error_buf,
  3107. error_buf_size)) {
  3108. return false;
  3109. }
  3110. break;
  3111. #endif
  3112. case IMPORT_KIND_GLOBAL: /* import global */
  3113. bh_assert(import_globals);
  3114. import = import_globals++;
  3115. if (!load_global_import(&p, p_end, module, sub_module_name,
  3116. field_name, &import->u.global,
  3117. error_buf, error_buf_size)) {
  3118. return false;
  3119. }
  3120. break;
  3121. default:
  3122. set_error_buf(error_buf, error_buf_size,
  3123. "invalid import kind");
  3124. return false;
  3125. }
  3126. import->kind = kind;
  3127. import->u.names.module_name = sub_module_name;
  3128. import->u.names.field_name = field_name;
  3129. }
  3130. #if WASM_ENABLE_LIBC_WASI != 0
  3131. import = module->import_functions;
  3132. for (i = 0; i < module->import_function_count; i++, import++) {
  3133. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3134. || !strcmp(import->u.names.module_name,
  3135. "wasi_snapshot_preview1")) {
  3136. module->import_wasi_api = true;
  3137. break;
  3138. }
  3139. }
  3140. #endif
  3141. }
  3142. if (p != p_end) {
  3143. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3144. return false;
  3145. }
  3146. LOG_VERBOSE("Load import section success.\n");
  3147. (void)u8;
  3148. (void)u32;
  3149. (void)type_index;
  3150. return true;
  3151. fail:
  3152. return false;
  3153. }
  3154. static bool
  3155. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3156. uint32 error_buf_size)
  3157. {
  3158. WASMFuncType *param_type = func->func_type;
  3159. uint32 param_count = param_type->param_count;
  3160. uint8 *param_types = param_type->types;
  3161. uint32 local_count = func->local_count;
  3162. uint8 *local_types = func->local_types;
  3163. uint32 i, local_offset = 0;
  3164. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3165. /*
  3166. * Only allocate memory when total_size is not 0,
  3167. * or the return value of malloc(0) might be NULL on some platforms,
  3168. * which causes wasm loader return false.
  3169. */
  3170. if (total_size > 0
  3171. && !(func->local_offsets =
  3172. loader_malloc(total_size, error_buf, error_buf_size))) {
  3173. return false;
  3174. }
  3175. for (i = 0; i < param_count; i++) {
  3176. func->local_offsets[i] = (uint16)local_offset;
  3177. local_offset += wasm_value_type_cell_num(param_types[i]);
  3178. }
  3179. for (i = 0; i < local_count; i++) {
  3180. func->local_offsets[param_count + i] = (uint16)local_offset;
  3181. local_offset += wasm_value_type_cell_num(local_types[i]);
  3182. }
  3183. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3184. return true;
  3185. }
  3186. static bool
  3187. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3188. const uint8 *buf_code, const uint8 *buf_code_end,
  3189. WASMModule *module, char *error_buf,
  3190. uint32 error_buf_size)
  3191. {
  3192. const uint8 *p = buf, *p_end = buf_end;
  3193. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3194. uint32 func_count;
  3195. uint64 total_size;
  3196. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3197. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3198. uint8 type;
  3199. WASMFunction *func;
  3200. #if WASM_ENABLE_GC != 0
  3201. bool need_ref_type_map;
  3202. WASMRefType ref_type;
  3203. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3204. #endif
  3205. read_leb_uint32(p, p_end, func_count);
  3206. if (buf_code)
  3207. read_leb_uint32(p_code, buf_code_end, code_count);
  3208. if (func_count != code_count) {
  3209. set_error_buf(error_buf, error_buf_size,
  3210. "function and code section have inconsistent lengths or "
  3211. "unexpected end");
  3212. return false;
  3213. }
  3214. if (is_indices_overflow(module->import_function_count, func_count,
  3215. error_buf, error_buf_size))
  3216. return false;
  3217. if (func_count) {
  3218. module->function_count = func_count;
  3219. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3220. if (!(module->functions =
  3221. loader_malloc(total_size, error_buf, error_buf_size))) {
  3222. return false;
  3223. }
  3224. for (i = 0; i < func_count; i++) {
  3225. /* Resolve function type */
  3226. read_leb_uint32(p, p_end, type_index);
  3227. if (type_index >= module->type_count) {
  3228. set_error_buf(error_buf, error_buf_size, "unknown type");
  3229. return false;
  3230. }
  3231. #if WASM_ENABLE_GC != 0
  3232. type_index_org = type_index;
  3233. #endif
  3234. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3235. && WASM_ENABLE_GC == 0
  3236. type_index = wasm_get_smallest_type_idx(
  3237. module->types, module->type_count, type_index);
  3238. #endif
  3239. read_leb_uint32(p_code, buf_code_end, code_size);
  3240. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3241. set_error_buf(error_buf, error_buf_size,
  3242. "invalid function code size");
  3243. return false;
  3244. }
  3245. /* Resolve local set count */
  3246. p_code_end = p_code + code_size;
  3247. local_count = 0;
  3248. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3249. p_code_save = p_code;
  3250. #if WASM_ENABLE_GC != 0
  3251. ref_type_map_count = 0;
  3252. #endif
  3253. /* Calculate total local count */
  3254. for (j = 0; j < local_set_count; j++) {
  3255. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3256. if (sub_local_count > UINT32_MAX - local_count) {
  3257. set_error_buf(error_buf, error_buf_size, "too many locals");
  3258. return false;
  3259. }
  3260. #if WASM_ENABLE_GC == 0
  3261. CHECK_BUF(p_code, buf_code_end, 1);
  3262. /* 0x7F/0x7E/0x7D/0x7C */
  3263. type = read_uint8(p_code);
  3264. local_count += sub_local_count;
  3265. #if WASM_ENABLE_WAMR_COMPILER != 0
  3266. /* If any value's type is v128, mark the module as SIMD used */
  3267. if (type == VALUE_TYPE_V128)
  3268. module->is_simd_used = true;
  3269. #endif
  3270. #else
  3271. if (!resolve_value_type(&p_code, buf_code_end, module,
  3272. module->type_count, &need_ref_type_map,
  3273. &ref_type, false, error_buf,
  3274. error_buf_size)) {
  3275. return false;
  3276. }
  3277. local_count += sub_local_count;
  3278. if (need_ref_type_map)
  3279. ref_type_map_count += sub_local_count;
  3280. #endif
  3281. }
  3282. /* Alloc memory, layout: function structure + local types */
  3283. code_size = (uint32)(p_code_end - p_code);
  3284. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3285. if (!(func = module->functions[i] =
  3286. loader_malloc(total_size, error_buf, error_buf_size))) {
  3287. return false;
  3288. }
  3289. #if WASM_ENABLE_GC != 0
  3290. if (ref_type_map_count > 0) {
  3291. total_size =
  3292. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3293. if (!(func->local_ref_type_maps = loader_malloc(
  3294. total_size, error_buf, error_buf_size))) {
  3295. return false;
  3296. }
  3297. func->local_ref_type_map_count = ref_type_map_count;
  3298. }
  3299. #endif
  3300. /* Set function type, local count, code size and code body */
  3301. func->func_type = (WASMFuncType *)module->types[type_index];
  3302. func->local_count = local_count;
  3303. if (local_count > 0)
  3304. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3305. func->code_size = code_size;
  3306. /*
  3307. * we shall make a copy of code body [p_code, p_code + code_size]
  3308. * when we are worrying about inappropriate releasing behaviour.
  3309. * all code bodies are actually in a buffer which user allocates in
  3310. * his embedding environment and we don't have power on them.
  3311. * it will be like:
  3312. * code_body_cp = malloc(code_size);
  3313. * memcpy(code_body_cp, p_code, code_size);
  3314. * func->code = code_body_cp;
  3315. */
  3316. func->code = (uint8 *)p_code;
  3317. #if WASM_ENABLE_GC != 0
  3318. func->type_idx = type_index_org;
  3319. #endif
  3320. #if WASM_ENABLE_GC != 0
  3321. t = 0;
  3322. #endif
  3323. /* Load each local type */
  3324. p_code = p_code_save;
  3325. local_type_index = 0;
  3326. for (j = 0; j < local_set_count; j++) {
  3327. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3328. /* Note: sub_local_count is allowed to be 0 */
  3329. if (local_type_index > UINT32_MAX - sub_local_count
  3330. || local_type_index + sub_local_count > local_count) {
  3331. set_error_buf(error_buf, error_buf_size,
  3332. "invalid local count");
  3333. return false;
  3334. }
  3335. #if WASM_ENABLE_GC == 0
  3336. CHECK_BUF(p_code, buf_code_end, 1);
  3337. /* 0x7F/0x7E/0x7D/0x7C */
  3338. type = read_uint8(p_code);
  3339. if (!is_valid_value_type_for_interpreter(type)) {
  3340. if (type == VALUE_TYPE_V128)
  3341. set_error_buf(error_buf, error_buf_size,
  3342. "v128 value type requires simd feature");
  3343. else if (type == VALUE_TYPE_FUNCREF
  3344. || type == VALUE_TYPE_EXTERNREF)
  3345. set_error_buf(error_buf, error_buf_size,
  3346. "ref value type requires "
  3347. "reference types feature");
  3348. else
  3349. set_error_buf_v(error_buf, error_buf_size,
  3350. "invalid local type 0x%02X", type);
  3351. return false;
  3352. }
  3353. #else
  3354. if (!resolve_value_type(&p_code, buf_code_end, module,
  3355. module->type_count, &need_ref_type_map,
  3356. &ref_type, false, error_buf,
  3357. error_buf_size)) {
  3358. return false;
  3359. }
  3360. if (need_ref_type_map) {
  3361. WASMRefType *ref_type_tmp;
  3362. if (!(ref_type_tmp = reftype_set_insert(
  3363. module->ref_type_set, &ref_type, error_buf,
  3364. error_buf_size))) {
  3365. return false;
  3366. }
  3367. for (k = 0; k < sub_local_count; k++) {
  3368. func->local_ref_type_maps[t + k].ref_type =
  3369. ref_type_tmp;
  3370. func->local_ref_type_maps[t + k].index =
  3371. local_type_index + k;
  3372. }
  3373. t += sub_local_count;
  3374. }
  3375. type = ref_type.ref_type;
  3376. #endif
  3377. for (k = 0; k < sub_local_count; k++) {
  3378. func->local_types[local_type_index++] = type;
  3379. }
  3380. #if WASM_ENABLE_WAMR_COMPILER != 0
  3381. if (type == VALUE_TYPE_V128)
  3382. module->is_simd_used = true;
  3383. else if (type == VALUE_TYPE_FUNCREF
  3384. || type == VALUE_TYPE_EXTERNREF)
  3385. module->is_ref_types_used = true;
  3386. #endif
  3387. }
  3388. bh_assert(local_type_index == func->local_count);
  3389. #if WASM_ENABLE_GC != 0
  3390. bh_assert(t == func->local_ref_type_map_count);
  3391. #if TRACE_WASM_LOADER != 0
  3392. os_printf("func %u, local types: [", i);
  3393. k = 0;
  3394. for (j = 0; j < func->local_count; j++) {
  3395. WASMRefType *ref_type_tmp = NULL;
  3396. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3397. bh_assert(j == func->local_ref_type_maps[k].index);
  3398. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3399. }
  3400. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3401. if (j < func->local_count - 1)
  3402. os_printf(" ");
  3403. }
  3404. os_printf("]\n");
  3405. #endif
  3406. #endif
  3407. func->param_cell_num = func->func_type->param_cell_num;
  3408. func->ret_cell_num = func->func_type->ret_cell_num;
  3409. local_cell_num =
  3410. wasm_get_cell_num(func->local_types, func->local_count);
  3411. if (local_cell_num > UINT16_MAX) {
  3412. set_error_buf(error_buf, error_buf_size,
  3413. "local count too large");
  3414. return false;
  3415. }
  3416. func->local_cell_num = (uint16)local_cell_num;
  3417. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3418. return false;
  3419. p_code = p_code_end;
  3420. }
  3421. }
  3422. if (p != p_end) {
  3423. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3424. return false;
  3425. }
  3426. LOG_VERBOSE("Load function section success.\n");
  3427. return true;
  3428. fail:
  3429. return false;
  3430. }
  3431. static bool
  3432. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3433. char *error_buf, uint32 error_buf_size)
  3434. {
  3435. const uint8 *p = buf, *p_end = buf_end;
  3436. uint32 table_count, i;
  3437. uint64 total_size;
  3438. WASMTable *table;
  3439. read_leb_uint32(p, p_end, table_count);
  3440. if (module->import_table_count + table_count > 1) {
  3441. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3442. /* a total of one table is allowed */
  3443. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3444. return false;
  3445. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3446. module->is_ref_types_used = true;
  3447. #endif
  3448. }
  3449. if (table_count) {
  3450. module->table_count = table_count;
  3451. total_size = sizeof(WASMTable) * (uint64)table_count;
  3452. if (!(module->tables =
  3453. loader_malloc(total_size, error_buf, error_buf_size))) {
  3454. return false;
  3455. }
  3456. /* load each table */
  3457. table = module->tables;
  3458. for (i = 0; i < table_count; i++, table++) {
  3459. #if WASM_ENABLE_GC != 0
  3460. uint8 flag;
  3461. bool has_init = false;
  3462. CHECK_BUF(buf, buf_end, 1);
  3463. flag = read_uint8(p);
  3464. if (flag == TABLE_INIT_EXPR_FLAG) {
  3465. CHECK_BUF(buf, buf_end, 1);
  3466. flag = read_uint8(p);
  3467. if (flag != 0x00) {
  3468. set_error_buf(error_buf, error_buf_size,
  3469. "invalid leading bytes for table");
  3470. return false;
  3471. }
  3472. has_init = true;
  3473. }
  3474. else {
  3475. p--;
  3476. }
  3477. #endif /* end of WASM_ENABLE_GC != 0 */
  3478. if (!load_table(&p, p_end, module, table, error_buf,
  3479. error_buf_size))
  3480. return false;
  3481. #if WASM_ENABLE_GC != 0
  3482. if (has_init) {
  3483. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3484. table->table_type.elem_type,
  3485. table->table_type.elem_ref_type, error_buf,
  3486. error_buf_size))
  3487. return false;
  3488. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3489. && table->init_expr.init_expr_type
  3490. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3491. set_error_buf(
  3492. error_buf, error_buf_size,
  3493. "unsupported initializer expression for table");
  3494. return false;
  3495. }
  3496. }
  3497. else {
  3498. if (wasm_is_reftype_htref_non_nullable(
  3499. table->table_type.elem_type)) {
  3500. set_error_buf(
  3501. error_buf, error_buf_size,
  3502. "type mismatch: non-nullable table without init expr");
  3503. return false;
  3504. }
  3505. }
  3506. #endif /* end of WASM_ENABLE_GC != 0 */
  3507. #if WASM_ENABLE_WAMR_COMPILER != 0
  3508. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3509. module->is_ref_types_used = true;
  3510. #endif
  3511. }
  3512. }
  3513. if (p != p_end) {
  3514. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3515. return false;
  3516. }
  3517. LOG_VERBOSE("Load table section success.\n");
  3518. return true;
  3519. fail:
  3520. return false;
  3521. }
  3522. static bool
  3523. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3524. char *error_buf, uint32 error_buf_size)
  3525. {
  3526. const uint8 *p = buf, *p_end = buf_end;
  3527. uint32 memory_count, i;
  3528. uint64 total_size;
  3529. WASMMemory *memory;
  3530. read_leb_uint32(p, p_end, memory_count);
  3531. #if WASM_ENABLE_MULTI_MEMORY == 0
  3532. /* a total of one memory is allowed */
  3533. if (module->import_memory_count + memory_count > 1) {
  3534. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3535. return false;
  3536. }
  3537. #endif
  3538. if (memory_count) {
  3539. module->memory_count = memory_count;
  3540. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3541. if (!(module->memories =
  3542. loader_malloc(total_size, error_buf, error_buf_size))) {
  3543. return false;
  3544. }
  3545. /* load each memory */
  3546. memory = module->memories;
  3547. for (i = 0; i < memory_count; i++, memory++)
  3548. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3549. return false;
  3550. }
  3551. if (p != p_end) {
  3552. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3553. return false;
  3554. }
  3555. LOG_VERBOSE("Load memory section success.\n");
  3556. return true;
  3557. fail:
  3558. return false;
  3559. }
  3560. static bool
  3561. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3562. char *error_buf, uint32 error_buf_size)
  3563. {
  3564. const uint8 *p = buf, *p_end = buf_end;
  3565. uint32 global_count, i;
  3566. uint64 total_size;
  3567. WASMGlobal *global;
  3568. uint8 mutable;
  3569. #if WASM_ENABLE_GC != 0
  3570. bool need_ref_type_map;
  3571. WASMRefType ref_type;
  3572. #endif
  3573. read_leb_uint32(p, p_end, global_count);
  3574. if (is_indices_overflow(module->import_global_count, global_count,
  3575. error_buf, error_buf_size))
  3576. return false;
  3577. module->global_count = 0;
  3578. if (global_count) {
  3579. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3580. if (!(module->globals =
  3581. loader_malloc(total_size, error_buf, error_buf_size))) {
  3582. return false;
  3583. }
  3584. global = module->globals;
  3585. for (i = 0; i < global_count; i++, global++) {
  3586. #if WASM_ENABLE_GC == 0
  3587. CHECK_BUF(p, p_end, 2);
  3588. /* global type */
  3589. global->type.val_type = read_uint8(p);
  3590. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3591. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3592. return false;
  3593. }
  3594. mutable = read_uint8(p);
  3595. #else
  3596. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3597. &need_ref_type_map, &ref_type, false,
  3598. error_buf, error_buf_size)) {
  3599. return false;
  3600. }
  3601. global->type.val_type = ref_type.ref_type;
  3602. CHECK_BUF(p, p_end, 1);
  3603. mutable = read_uint8(p);
  3604. #endif /* end of WASM_ENABLE_GC */
  3605. #if WASM_ENABLE_WAMR_COMPILER != 0
  3606. if (global->type.val_type == VALUE_TYPE_V128)
  3607. module->is_simd_used = true;
  3608. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3609. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3610. module->is_ref_types_used = true;
  3611. #endif
  3612. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3613. return false;
  3614. }
  3615. global->type.is_mutable = mutable ? true : false;
  3616. /* initialize expression */
  3617. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3618. global->type.val_type,
  3619. #if WASM_ENABLE_GC == 0
  3620. NULL,
  3621. #else
  3622. &ref_type,
  3623. #endif
  3624. error_buf, error_buf_size))
  3625. return false;
  3626. #if WASM_ENABLE_GC != 0
  3627. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3628. uint8 global_type;
  3629. WASMRefType *global_ref_type;
  3630. uint32 global_idx = global->init_expr.u.global_index;
  3631. if (global->init_expr.u.global_index
  3632. >= module->import_global_count + i) {
  3633. set_error_buf(error_buf, error_buf_size, "unknown global");
  3634. return false;
  3635. }
  3636. if (global_idx < module->import_global_count) {
  3637. global_type = module->import_globals[global_idx]
  3638. .u.global.type.val_type;
  3639. global_ref_type =
  3640. module->import_globals[global_idx].u.global.ref_type;
  3641. }
  3642. else {
  3643. global_type =
  3644. module
  3645. ->globals[global_idx - module->import_global_count]
  3646. .type.val_type;
  3647. global_ref_type =
  3648. module
  3649. ->globals[global_idx - module->import_global_count]
  3650. .ref_type;
  3651. }
  3652. if (!wasm_reftype_is_subtype_of(
  3653. global_type, global_ref_type, global->type.val_type,
  3654. global->ref_type, module->types, module->type_count)) {
  3655. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3656. return false;
  3657. }
  3658. }
  3659. if (need_ref_type_map) {
  3660. if (!(global->ref_type =
  3661. reftype_set_insert(module->ref_type_set, &ref_type,
  3662. error_buf, error_buf_size))) {
  3663. return false;
  3664. }
  3665. }
  3666. #if TRACE_WASM_LOADER != 0
  3667. os_printf("global type: ");
  3668. wasm_dump_value_type(global->type, global->ref_type);
  3669. os_printf("\n");
  3670. #endif
  3671. #endif
  3672. module->global_count++;
  3673. }
  3674. bh_assert(module->global_count == global_count);
  3675. }
  3676. if (p != p_end) {
  3677. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3678. return false;
  3679. }
  3680. LOG_VERBOSE("Load global section success.\n");
  3681. return true;
  3682. fail:
  3683. return false;
  3684. }
  3685. static bool
  3686. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3687. bool is_load_from_file_buf, char *error_buf,
  3688. uint32 error_buf_size)
  3689. {
  3690. const uint8 *p = buf, *p_end = buf_end;
  3691. uint32 export_count, i, j, index;
  3692. uint64 total_size;
  3693. uint32 str_len;
  3694. WASMExport *export;
  3695. const char *name;
  3696. read_leb_uint32(p, p_end, export_count);
  3697. if (export_count) {
  3698. module->export_count = export_count;
  3699. total_size = sizeof(WASMExport) * (uint64)export_count;
  3700. if (!(module->exports =
  3701. loader_malloc(total_size, error_buf, error_buf_size))) {
  3702. return false;
  3703. }
  3704. export = module->exports;
  3705. for (i = 0; i < export_count; i++, export ++) {
  3706. #if WASM_ENABLE_THREAD_MGR == 0
  3707. if (p == p_end) {
  3708. /* export section with inconsistent count:
  3709. n export declared, but less than n given */
  3710. set_error_buf(error_buf, error_buf_size,
  3711. "length out of bounds");
  3712. return false;
  3713. }
  3714. #endif
  3715. read_leb_uint32(p, p_end, str_len);
  3716. CHECK_BUF(p, p_end, str_len);
  3717. for (j = 0; j < i; j++) {
  3718. name = module->exports[j].name;
  3719. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  3720. set_error_buf(error_buf, error_buf_size,
  3721. "duplicate export name");
  3722. return false;
  3723. }
  3724. }
  3725. if (!(export->name = wasm_const_str_list_insert(
  3726. p, str_len, module, is_load_from_file_buf, error_buf,
  3727. error_buf_size))) {
  3728. return false;
  3729. }
  3730. p += str_len;
  3731. CHECK_BUF(p, p_end, 1);
  3732. export->kind = read_uint8(p);
  3733. read_leb_uint32(p, p_end, index);
  3734. export->index = index;
  3735. switch (export->kind) {
  3736. /* function index */
  3737. case EXPORT_KIND_FUNC:
  3738. if (index >= module->function_count
  3739. + module->import_function_count) {
  3740. set_error_buf(error_buf, error_buf_size,
  3741. "unknown function");
  3742. return false;
  3743. }
  3744. #if WASM_ENABLE_SIMD != 0
  3745. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3746. /* TODO: check func type, if it has v128 param or result,
  3747. report error */
  3748. #endif
  3749. #endif
  3750. break;
  3751. /* table index */
  3752. case EXPORT_KIND_TABLE:
  3753. if (index
  3754. >= module->table_count + module->import_table_count) {
  3755. set_error_buf(error_buf, error_buf_size,
  3756. "unknown table");
  3757. return false;
  3758. }
  3759. break;
  3760. /* memory index */
  3761. case EXPORT_KIND_MEMORY:
  3762. if (index
  3763. >= module->memory_count + module->import_memory_count) {
  3764. set_error_buf(error_buf, error_buf_size,
  3765. "unknown memory");
  3766. return false;
  3767. }
  3768. break;
  3769. #if WASM_ENABLE_TAGS != 0
  3770. /* export tag */
  3771. case EXPORT_KIND_TAG:
  3772. if (index >= module->tag_count + module->import_tag_count) {
  3773. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3774. return false;
  3775. }
  3776. break;
  3777. #endif
  3778. /* global index */
  3779. case EXPORT_KIND_GLOBAL:
  3780. if (index
  3781. >= module->global_count + module->import_global_count) {
  3782. set_error_buf(error_buf, error_buf_size,
  3783. "unknown global");
  3784. return false;
  3785. }
  3786. break;
  3787. default:
  3788. set_error_buf(error_buf, error_buf_size,
  3789. "invalid export kind");
  3790. return false;
  3791. }
  3792. }
  3793. }
  3794. if (p != p_end) {
  3795. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3796. return false;
  3797. }
  3798. LOG_VERBOSE("Load export section success.\n");
  3799. return true;
  3800. fail:
  3801. return false;
  3802. }
  3803. static bool
  3804. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3805. uint32 error_buf_size)
  3806. {
  3807. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3808. if (table_index != 0) {
  3809. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3810. return false;
  3811. }
  3812. #endif
  3813. if (table_index >= module->import_table_count + module->table_count) {
  3814. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3815. table_index);
  3816. return false;
  3817. }
  3818. return true;
  3819. }
  3820. static bool
  3821. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3822. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3823. {
  3824. const uint8 *p = *p_buf, *p_end = buf_end;
  3825. uint32 table_index;
  3826. read_leb_uint32(p, p_end, table_index);
  3827. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3828. return false;
  3829. }
  3830. *p_table_index = table_index;
  3831. *p_buf = p;
  3832. return true;
  3833. fail:
  3834. return false;
  3835. }
  3836. /* Element segments must match element type of table */
  3837. static bool
  3838. check_table_elem_type(WASMModule *module, uint32 table_index,
  3839. uint32 type_from_elem_seg, char *error_buf,
  3840. uint32 error_buf_size)
  3841. {
  3842. uint32 table_declared_elem_type;
  3843. if (table_index < module->import_table_count)
  3844. table_declared_elem_type =
  3845. module->import_tables[table_index].u.table.table_type.elem_type;
  3846. else
  3847. table_declared_elem_type =
  3848. (module->tables + table_index)->table_type.elem_type;
  3849. if (table_declared_elem_type == type_from_elem_seg)
  3850. return true;
  3851. #if WASM_ENABLE_GC != 0
  3852. /*
  3853. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3854. */
  3855. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3856. return true;
  3857. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3858. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3859. return true;
  3860. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3861. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3862. return true;
  3863. #endif
  3864. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3865. return false;
  3866. }
  3867. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3868. static bool
  3869. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3870. uint32 *p_elem_type,
  3871. #if WASM_ENABLE_GC != 0
  3872. WASMRefType **p_elem_ref_type,
  3873. #endif
  3874. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3875. {
  3876. const uint8 *p = *p_buf, *p_end = buf_end;
  3877. uint8 elem_type;
  3878. #if WASM_ENABLE_GC != 0
  3879. WASMRefType elem_ref_type;
  3880. bool need_ref_type_map;
  3881. #endif
  3882. CHECK_BUF(p, p_end, 1);
  3883. elem_type = read_uint8(p);
  3884. if (elemkind_zero) {
  3885. if (elem_type != 0) {
  3886. set_error_buf(error_buf, error_buf_size,
  3887. "invalid reference type or unknown type");
  3888. return false;
  3889. }
  3890. else {
  3891. *p_elem_type = VALUE_TYPE_FUNCREF;
  3892. *p_buf = p;
  3893. return true;
  3894. }
  3895. }
  3896. #if WASM_ENABLE_GC == 0
  3897. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3898. set_error_buf(error_buf, error_buf_size,
  3899. "invalid reference type or unknown type");
  3900. return false;
  3901. }
  3902. *p_elem_type = elem_type;
  3903. #else
  3904. p--;
  3905. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3906. module->type_count, &need_ref_type_map,
  3907. &elem_ref_type, false, error_buf, error_buf_size)) {
  3908. return false;
  3909. }
  3910. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3911. set_error_buf(error_buf, error_buf_size,
  3912. "invalid reference type or unknown type");
  3913. return false;
  3914. }
  3915. *p_elem_type = elem_ref_type.ref_type;
  3916. if (need_ref_type_map) {
  3917. if (!(*p_elem_ref_type =
  3918. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3919. error_buf, error_buf_size))) {
  3920. return false;
  3921. }
  3922. }
  3923. #endif
  3924. *p_buf = p;
  3925. return true;
  3926. fail:
  3927. return false;
  3928. }
  3929. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3930. static bool
  3931. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3932. WASMModule *module, WASMTableSeg *table_segment,
  3933. char *error_buf, uint32 error_buf_size)
  3934. {
  3935. const uint8 *p = *p_buf, *p_end = buf_end;
  3936. uint32 function_count, function_index = 0, i;
  3937. uint64 total_size;
  3938. read_leb_uint32(p, p_end, function_count);
  3939. table_segment->value_count = function_count;
  3940. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3941. if (total_size > 0
  3942. && !(table_segment->init_values =
  3943. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3944. error_buf_size))) {
  3945. return false;
  3946. }
  3947. for (i = 0; i < function_count; i++) {
  3948. InitializerExpression *init_expr = &table_segment->init_values[i];
  3949. read_leb_uint32(p, p_end, function_index);
  3950. if (!check_function_index(module, function_index, error_buf,
  3951. error_buf_size)) {
  3952. return false;
  3953. }
  3954. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3955. init_expr->u.ref_index = function_index;
  3956. }
  3957. *p_buf = p;
  3958. return true;
  3959. fail:
  3960. return false;
  3961. }
  3962. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3963. static bool
  3964. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3965. WASMModule *module, WASMTableSeg *table_segment,
  3966. char *error_buf, uint32 error_buf_size)
  3967. {
  3968. const uint8 *p = *p_buf, *p_end = buf_end;
  3969. uint32 ref_count, i;
  3970. uint64 total_size;
  3971. read_leb_uint32(p, p_end, ref_count);
  3972. table_segment->value_count = ref_count;
  3973. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3974. if (total_size > 0
  3975. && !(table_segment->init_values =
  3976. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3977. error_buf_size))) {
  3978. return false;
  3979. }
  3980. for (i = 0; i < ref_count; i++) {
  3981. InitializerExpression *init_expr = &table_segment->init_values[i];
  3982. if (!load_init_expr(module, &p, p_end, init_expr,
  3983. table_segment->elem_type,
  3984. #if WASM_ENABLE_GC == 0
  3985. NULL,
  3986. #else
  3987. table_segment->elem_ref_type,
  3988. #endif
  3989. error_buf, error_buf_size))
  3990. return false;
  3991. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3992. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3993. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3994. && init_expr->init_expr_type
  3995. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3996. }
  3997. *p_buf = p;
  3998. return true;
  3999. fail:
  4000. return false;
  4001. }
  4002. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  4003. static bool
  4004. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  4005. WASMModule *module, char *error_buf,
  4006. uint32 error_buf_size)
  4007. {
  4008. const uint8 *p = buf, *p_end = buf_end;
  4009. uint32 table_segment_count, i;
  4010. uint64 total_size;
  4011. WASMTableSeg *table_segment;
  4012. read_leb_uint32(p, p_end, table_segment_count);
  4013. if (table_segment_count) {
  4014. module->table_seg_count = table_segment_count;
  4015. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  4016. if (!(module->table_segments =
  4017. loader_malloc(total_size, error_buf, error_buf_size))) {
  4018. return false;
  4019. }
  4020. table_segment = module->table_segments;
  4021. for (i = 0; i < table_segment_count; i++, table_segment++) {
  4022. if (p >= p_end) {
  4023. set_error_buf(error_buf, error_buf_size,
  4024. "invalid value type or "
  4025. "invalid elements segment kind");
  4026. return false;
  4027. }
  4028. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4029. read_leb_uint32(p, p_end, table_segment->mode);
  4030. /* last three bits */
  4031. table_segment->mode = table_segment->mode & 0x07;
  4032. switch (table_segment->mode) {
  4033. /* elemkind/elemtype + active */
  4034. case 0:
  4035. case 4:
  4036. {
  4037. #if WASM_ENABLE_GC != 0
  4038. if (table_segment->mode == 0) {
  4039. /* vec(funcidx), set elem type to (ref func) */
  4040. WASMRefType elem_ref_type = { 0 };
  4041. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4042. wasm_set_refheaptype_common(
  4043. &elem_ref_type.ref_ht_common, false,
  4044. HEAP_TYPE_FUNC);
  4045. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4046. module->ref_type_set, &elem_ref_type,
  4047. error_buf, error_buf_size)))
  4048. return false;
  4049. }
  4050. else {
  4051. /* vec(expr), set elem type to funcref */
  4052. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4053. }
  4054. #else
  4055. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4056. #endif
  4057. table_segment->table_index = 0;
  4058. if (!check_table_index(module, table_segment->table_index,
  4059. error_buf, error_buf_size))
  4060. return false;
  4061. if (!load_init_expr(
  4062. module, &p, p_end, &table_segment->base_offset,
  4063. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4064. return false;
  4065. if (table_segment->mode == 0) {
  4066. /* vec(funcidx) */
  4067. if (!load_func_index_vec(&p, p_end, module,
  4068. table_segment, error_buf,
  4069. error_buf_size))
  4070. return false;
  4071. }
  4072. else {
  4073. /* vec(expr) */
  4074. if (!load_init_expr_vec(&p, p_end, module,
  4075. table_segment, error_buf,
  4076. error_buf_size))
  4077. return false;
  4078. }
  4079. if (!check_table_elem_type(module,
  4080. table_segment->table_index,
  4081. table_segment->elem_type,
  4082. error_buf, error_buf_size))
  4083. return false;
  4084. break;
  4085. }
  4086. /* elemkind + passive/declarative */
  4087. case 1:
  4088. case 3:
  4089. if (!load_elem_type(module, &p, p_end,
  4090. &table_segment->elem_type,
  4091. #if WASM_ENABLE_GC != 0
  4092. &table_segment->elem_ref_type,
  4093. #endif
  4094. true, error_buf, error_buf_size))
  4095. return false;
  4096. /* vec(funcidx) */
  4097. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4098. error_buf, error_buf_size))
  4099. return false;
  4100. break;
  4101. /* elemkind/elemtype + table_idx + active */
  4102. case 2:
  4103. case 6:
  4104. if (!load_table_index(&p, p_end, module,
  4105. &table_segment->table_index,
  4106. error_buf, error_buf_size))
  4107. return false;
  4108. if (!load_init_expr(
  4109. module, &p, p_end, &table_segment->base_offset,
  4110. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4111. return false;
  4112. if (!load_elem_type(module, &p, p_end,
  4113. &table_segment->elem_type,
  4114. #if WASM_ENABLE_GC != 0
  4115. &table_segment->elem_ref_type,
  4116. #endif
  4117. table_segment->mode == 2 ? true : false,
  4118. error_buf, error_buf_size))
  4119. return false;
  4120. if (table_segment->mode == 2) {
  4121. /* vec(funcidx) */
  4122. if (!load_func_index_vec(&p, p_end, module,
  4123. table_segment, error_buf,
  4124. error_buf_size))
  4125. return false;
  4126. }
  4127. else {
  4128. /* vec(expr) */
  4129. if (!load_init_expr_vec(&p, p_end, module,
  4130. table_segment, error_buf,
  4131. error_buf_size))
  4132. return false;
  4133. }
  4134. if (!check_table_elem_type(module,
  4135. table_segment->table_index,
  4136. table_segment->elem_type,
  4137. error_buf, error_buf_size))
  4138. return false;
  4139. break;
  4140. case 5:
  4141. case 7:
  4142. if (!load_elem_type(module, &p, p_end,
  4143. &table_segment->elem_type,
  4144. #if WASM_ENABLE_GC != 0
  4145. &table_segment->elem_ref_type,
  4146. #endif
  4147. false, error_buf, error_buf_size))
  4148. return false;
  4149. /* vec(expr) */
  4150. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4151. error_buf, error_buf_size))
  4152. return false;
  4153. break;
  4154. default:
  4155. set_error_buf(error_buf, error_buf_size,
  4156. "unknown element segment kind");
  4157. return false;
  4158. }
  4159. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4160. /*
  4161. * like: 00 41 05 0b 04 00 01 00 01
  4162. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4163. */
  4164. if (!load_table_index(&p, p_end, module,
  4165. &table_segment->table_index, error_buf,
  4166. error_buf_size))
  4167. return false;
  4168. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4169. VALUE_TYPE_I32, NULL, error_buf,
  4170. error_buf_size))
  4171. return false;
  4172. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4173. error_buf, error_buf_size))
  4174. return false;
  4175. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4176. if (!check_table_elem_type(module, table_segment->table_index,
  4177. table_segment->elem_type, error_buf,
  4178. error_buf_size))
  4179. return false;
  4180. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4181. #if WASM_ENABLE_WAMR_COMPILER != 0
  4182. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4183. module->is_ref_types_used = true;
  4184. #endif
  4185. }
  4186. }
  4187. if (p != p_end) {
  4188. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4189. return false;
  4190. }
  4191. LOG_VERBOSE("Load table segment section success.\n");
  4192. return true;
  4193. fail:
  4194. return false;
  4195. }
  4196. static bool
  4197. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4198. WASMModule *module,
  4199. #if WASM_ENABLE_BULK_MEMORY != 0
  4200. bool has_datacount_section,
  4201. #endif
  4202. bool clone_data_seg, char *error_buf,
  4203. uint32 error_buf_size)
  4204. {
  4205. const uint8 *p = buf, *p_end = buf_end;
  4206. uint32 data_seg_count, i, mem_index, data_seg_len;
  4207. uint64 total_size;
  4208. WASMDataSeg *dataseg;
  4209. InitializerExpression init_expr;
  4210. #if WASM_ENABLE_BULK_MEMORY != 0
  4211. bool is_passive = false;
  4212. uint32 mem_flag;
  4213. #endif
  4214. uint8 mem_offset_type = VALUE_TYPE_I32;
  4215. read_leb_uint32(p, p_end, data_seg_count);
  4216. #if WASM_ENABLE_BULK_MEMORY != 0
  4217. if (has_datacount_section && data_seg_count != module->data_seg_count1) {
  4218. set_error_buf(error_buf, error_buf_size,
  4219. "data count and data section have inconsistent lengths");
  4220. return false;
  4221. }
  4222. #endif
  4223. if (data_seg_count) {
  4224. module->data_seg_count = data_seg_count;
  4225. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4226. if (!(module->data_segments =
  4227. loader_malloc(total_size, error_buf, error_buf_size))) {
  4228. return false;
  4229. }
  4230. for (i = 0; i < data_seg_count; i++) {
  4231. read_leb_uint32(p, p_end, mem_index);
  4232. #if WASM_ENABLE_BULK_MEMORY != 0
  4233. is_passive = false;
  4234. mem_flag = mem_index & 0x03;
  4235. switch (mem_flag) {
  4236. case 0x01:
  4237. is_passive = true;
  4238. #if WASM_ENABLE_WAMR_COMPILER != 0
  4239. module->is_bulk_memory_used = true;
  4240. #endif
  4241. break;
  4242. case 0x00:
  4243. /* no memory index, treat index as 0 */
  4244. mem_index = 0;
  4245. goto check_mem_index;
  4246. case 0x02:
  4247. /* read following memory index */
  4248. read_leb_uint32(p, p_end, mem_index);
  4249. #if WASM_ENABLE_WAMR_COMPILER != 0
  4250. module->is_bulk_memory_used = true;
  4251. #endif
  4252. check_mem_index:
  4253. if (mem_index
  4254. >= module->import_memory_count + module->memory_count) {
  4255. set_error_buf_v(error_buf, error_buf_size,
  4256. "unknown memory %d", mem_index);
  4257. return false;
  4258. }
  4259. break;
  4260. case 0x03:
  4261. default:
  4262. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4263. return false;
  4264. break;
  4265. }
  4266. #else
  4267. if (mem_index
  4268. >= module->import_memory_count + module->memory_count) {
  4269. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4270. mem_index);
  4271. return false;
  4272. }
  4273. #endif /* WASM_ENABLE_BULK_MEMORY */
  4274. #if WASM_ENABLE_BULK_MEMORY != 0
  4275. if (!is_passive)
  4276. #endif
  4277. {
  4278. #if WASM_ENABLE_MEMORY64 != 0
  4279. /* This memory_flag is from memory instead of data segment */
  4280. uint8 memory_flag;
  4281. if (module->import_memory_count > 0) {
  4282. memory_flag = module->import_memories[mem_index]
  4283. .u.memory.mem_type.flags;
  4284. }
  4285. else {
  4286. memory_flag =
  4287. module
  4288. ->memories[mem_index - module->import_memory_count]
  4289. .flags;
  4290. }
  4291. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4292. : VALUE_TYPE_I32;
  4293. #else
  4294. mem_offset_type = VALUE_TYPE_I32;
  4295. #endif
  4296. }
  4297. #if WASM_ENABLE_BULK_MEMORY != 0
  4298. if (!is_passive)
  4299. #endif
  4300. if (!load_init_expr(module, &p, p_end, &init_expr,
  4301. mem_offset_type, NULL, error_buf,
  4302. error_buf_size))
  4303. return false;
  4304. read_leb_uint32(p, p_end, data_seg_len);
  4305. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4306. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4307. return false;
  4308. }
  4309. #if WASM_ENABLE_BULK_MEMORY != 0
  4310. dataseg->is_passive = is_passive;
  4311. if (!is_passive)
  4312. #endif
  4313. {
  4314. bh_memcpy_s(&dataseg->base_offset,
  4315. sizeof(InitializerExpression), &init_expr,
  4316. sizeof(InitializerExpression));
  4317. dataseg->memory_index = mem_index;
  4318. }
  4319. dataseg->data_length = data_seg_len;
  4320. CHECK_BUF(p, p_end, data_seg_len);
  4321. if (clone_data_seg) {
  4322. if (!(dataseg->data = loader_malloc(
  4323. dataseg->data_length, error_buf, error_buf_size))) {
  4324. return false;
  4325. }
  4326. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4327. data_seg_len);
  4328. }
  4329. else {
  4330. dataseg->data = (uint8 *)p;
  4331. }
  4332. dataseg->is_data_cloned = clone_data_seg;
  4333. p += data_seg_len;
  4334. }
  4335. }
  4336. if (p != p_end) {
  4337. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4338. return false;
  4339. }
  4340. LOG_VERBOSE("Load data segment section success.\n");
  4341. return true;
  4342. fail:
  4343. return false;
  4344. }
  4345. #if WASM_ENABLE_BULK_MEMORY != 0
  4346. static bool
  4347. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4348. WASMModule *module, char *error_buf,
  4349. uint32 error_buf_size)
  4350. {
  4351. const uint8 *p = buf, *p_end = buf_end;
  4352. uint32 data_seg_count1 = 0;
  4353. read_leb_uint32(p, p_end, data_seg_count1);
  4354. module->data_seg_count1 = data_seg_count1;
  4355. if (p != p_end) {
  4356. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4357. return false;
  4358. }
  4359. #if WASM_ENABLE_WAMR_COMPILER != 0
  4360. module->is_bulk_memory_used = true;
  4361. #endif
  4362. LOG_VERBOSE("Load datacount section success.\n");
  4363. return true;
  4364. fail:
  4365. return false;
  4366. }
  4367. #endif
  4368. #if WASM_ENABLE_TAGS != 0
  4369. static bool
  4370. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4371. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4372. uint32 error_buf_size)
  4373. {
  4374. (void)buf_code;
  4375. (void)buf_code_end;
  4376. const uint8 *p = buf, *p_end = buf_end;
  4377. size_t total_size = 0;
  4378. /* number of tags defined in the section */
  4379. uint32 section_tag_count = 0;
  4380. uint8 tag_attribute;
  4381. uint32 tag_type;
  4382. WASMTag *tag = NULL;
  4383. /* get tag count */
  4384. read_leb_uint32(p, p_end, section_tag_count);
  4385. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4386. error_buf, error_buf_size))
  4387. return false;
  4388. module->tag_count = section_tag_count;
  4389. if (section_tag_count) {
  4390. total_size = sizeof(WASMTag *) * module->tag_count;
  4391. if (!(module->tags =
  4392. loader_malloc(total_size, error_buf, error_buf_size))) {
  4393. return false;
  4394. }
  4395. /* load each tag, imported tags precede the tags */
  4396. uint32 tag_index;
  4397. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4398. /* get the one byte attribute */
  4399. CHECK_BUF(p, p_end, 1);
  4400. tag_attribute = read_uint8(p);
  4401. /* get type */
  4402. read_leb_uint32(p, p_end, tag_type);
  4403. /* compare against module->types */
  4404. if (tag_type >= module->type_count) {
  4405. set_error_buf(error_buf, error_buf_size, "unknown type");
  4406. return false;
  4407. }
  4408. /* get return type (must be 0) */
  4409. /* check, that the type of the referred tag returns void */
  4410. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4411. if (func_type->result_count != 0) {
  4412. set_error_buf(error_buf, error_buf_size,
  4413. "non-empty tag result type");
  4414. goto fail;
  4415. }
  4416. if (!(tag = module->tags[tag_index] = loader_malloc(
  4417. sizeof(WASMTag), error_buf, error_buf_size))) {
  4418. return false;
  4419. }
  4420. /* store to module tag declarations */
  4421. tag->attribute = tag_attribute;
  4422. tag->type = tag_type;
  4423. tag->tag_type = func_type;
  4424. }
  4425. }
  4426. if (p != p_end) {
  4427. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4428. return false;
  4429. }
  4430. LOG_VERBOSE("Load tag section success.\n");
  4431. return true;
  4432. fail:
  4433. return false;
  4434. }
  4435. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4436. static bool
  4437. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4438. const uint8 *buf_func_end, WASMModule *module,
  4439. char *error_buf, uint32 error_buf_size)
  4440. {
  4441. const uint8 *p = buf, *p_end = buf_end;
  4442. const uint8 *p_func = buf_func;
  4443. uint32 func_count = 0, code_count;
  4444. /* code has been loaded in function section, so pass it here, just check
  4445. * whether function and code section have inconsistent lengths */
  4446. read_leb_uint32(p, p_end, code_count);
  4447. if (buf_func)
  4448. read_leb_uint32(p_func, buf_func_end, func_count);
  4449. if (func_count != code_count) {
  4450. set_error_buf(error_buf, error_buf_size,
  4451. "function and code section have inconsistent lengths");
  4452. return false;
  4453. }
  4454. LOG_VERBOSE("Load code segment section success.\n");
  4455. (void)module;
  4456. return true;
  4457. fail:
  4458. return false;
  4459. }
  4460. static bool
  4461. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4462. char *error_buf, uint32 error_buf_size)
  4463. {
  4464. const uint8 *p = buf, *p_end = buf_end;
  4465. WASMFuncType *type;
  4466. uint32 start_function;
  4467. read_leb_uint32(p, p_end, start_function);
  4468. if (start_function
  4469. >= module->function_count + module->import_function_count) {
  4470. set_error_buf(error_buf, error_buf_size, "unknown function");
  4471. return false;
  4472. }
  4473. if (start_function < module->import_function_count)
  4474. type = module->import_functions[start_function].u.function.func_type;
  4475. else
  4476. type = module->functions[start_function - module->import_function_count]
  4477. ->func_type;
  4478. if (type->param_count != 0 || type->result_count != 0) {
  4479. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4480. return false;
  4481. }
  4482. module->start_function = start_function;
  4483. if (p != p_end) {
  4484. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4485. return false;
  4486. }
  4487. LOG_VERBOSE("Load start section success.\n");
  4488. return true;
  4489. fail:
  4490. return false;
  4491. }
  4492. #if WASM_ENABLE_STRINGREF != 0
  4493. static bool
  4494. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4495. WASMModule *module, bool is_load_from_file_buf,
  4496. char *error_buf, uint32 error_buf_size)
  4497. {
  4498. const uint8 *p = buf, *p_end = buf_end;
  4499. int32 deferred_count, immediate_count, string_length, i;
  4500. uint64 total_size;
  4501. read_leb_uint32(p, p_end, deferred_count);
  4502. read_leb_uint32(p, p_end, immediate_count);
  4503. /* proposal set deferred_count for future extension */
  4504. if (deferred_count != 0) {
  4505. goto fail;
  4506. }
  4507. if (immediate_count > 0) {
  4508. total_size = sizeof(char *) * (uint64)immediate_count;
  4509. if (!(module->string_literal_ptrs =
  4510. loader_malloc(total_size, error_buf, error_buf_size))) {
  4511. goto fail;
  4512. }
  4513. module->string_literal_count = immediate_count;
  4514. total_size = sizeof(uint32) * (uint64)immediate_count;
  4515. if (!(module->string_literal_lengths =
  4516. loader_malloc(total_size, error_buf, error_buf_size))) {
  4517. goto fail;
  4518. }
  4519. for (i = 0; i < immediate_count; i++) {
  4520. read_leb_uint32(p, p_end, string_length);
  4521. CHECK_BUF(p, p_end, string_length);
  4522. module->string_literal_ptrs[i] = p;
  4523. module->string_literal_lengths[i] = string_length;
  4524. p += string_length;
  4525. }
  4526. }
  4527. if (p != p_end) {
  4528. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4529. goto fail;
  4530. }
  4531. LOG_VERBOSE("Load stringref section success.\n");
  4532. return true;
  4533. fail:
  4534. return false;
  4535. }
  4536. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4537. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4538. static bool
  4539. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4540. bool is_load_from_file_buf, char *error_buf,
  4541. uint32 error_buf_size)
  4542. {
  4543. const uint8 *p = buf, *p_end = buf_end;
  4544. uint32 name_type, subsection_size;
  4545. uint32 previous_name_type = 0;
  4546. uint32 num_func_name;
  4547. uint32 func_index;
  4548. uint32 previous_func_index = ~0U;
  4549. uint32 func_name_len;
  4550. uint32 name_index;
  4551. int i = 0;
  4552. if (p >= p_end) {
  4553. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4554. return false;
  4555. }
  4556. while (p < p_end) {
  4557. read_leb_uint32(p, p_end, name_type);
  4558. if (i != 0) {
  4559. if (name_type == previous_name_type) {
  4560. set_error_buf(error_buf, error_buf_size,
  4561. "duplicate sub-section");
  4562. return false;
  4563. }
  4564. if (name_type < previous_name_type) {
  4565. set_error_buf(error_buf, error_buf_size,
  4566. "out-of-order sub-section");
  4567. return false;
  4568. }
  4569. }
  4570. previous_name_type = name_type;
  4571. read_leb_uint32(p, p_end, subsection_size);
  4572. CHECK_BUF(p, p_end, subsection_size);
  4573. switch (name_type) {
  4574. case SUB_SECTION_TYPE_FUNC:
  4575. if (subsection_size) {
  4576. read_leb_uint32(p, p_end, num_func_name);
  4577. for (name_index = 0; name_index < num_func_name;
  4578. name_index++) {
  4579. read_leb_uint32(p, p_end, func_index);
  4580. if (func_index == previous_func_index) {
  4581. set_error_buf(error_buf, error_buf_size,
  4582. "duplicate function name");
  4583. return false;
  4584. }
  4585. if (func_index < previous_func_index
  4586. && previous_func_index != ~0U) {
  4587. set_error_buf(error_buf, error_buf_size,
  4588. "out-of-order function index ");
  4589. return false;
  4590. }
  4591. previous_func_index = func_index;
  4592. read_leb_uint32(p, p_end, func_name_len);
  4593. CHECK_BUF(p, p_end, func_name_len);
  4594. /* Skip the import functions */
  4595. if (func_index >= module->import_function_count) {
  4596. func_index -= module->import_function_count;
  4597. if (func_index >= module->function_count) {
  4598. set_error_buf(error_buf, error_buf_size,
  4599. "out-of-range function index");
  4600. return false;
  4601. }
  4602. if (!(module->functions[func_index]->field_name =
  4603. wasm_const_str_list_insert(
  4604. p, func_name_len, module,
  4605. #if WASM_ENABLE_WAMR_COMPILER != 0
  4606. false,
  4607. #else
  4608. is_load_from_file_buf,
  4609. #endif
  4610. error_buf, error_buf_size))) {
  4611. return false;
  4612. }
  4613. }
  4614. p += func_name_len;
  4615. }
  4616. }
  4617. break;
  4618. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4619. */
  4620. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4621. default:
  4622. p = p + subsection_size;
  4623. break;
  4624. }
  4625. i++;
  4626. }
  4627. return true;
  4628. fail:
  4629. return false;
  4630. }
  4631. #endif
  4632. static bool
  4633. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4634. bool is_load_from_file_buf, char *error_buf,
  4635. uint32 error_buf_size)
  4636. {
  4637. const uint8 *p = buf, *p_end = buf_end;
  4638. char section_name[32];
  4639. uint32 name_len, buffer_len;
  4640. if (p >= p_end) {
  4641. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4642. return false;
  4643. }
  4644. read_leb_uint32(p, p_end, name_len);
  4645. if (p + name_len > p_end) {
  4646. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4647. return false;
  4648. }
  4649. if (!wasm_check_utf8_str(p, name_len)) {
  4650. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4651. return false;
  4652. }
  4653. buffer_len = sizeof(section_name);
  4654. memset(section_name, 0, buffer_len);
  4655. if (name_len < buffer_len) {
  4656. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4657. }
  4658. else {
  4659. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4660. memset(section_name + buffer_len - 4, '.', 3);
  4661. }
  4662. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4663. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4664. module->name_section_buf = buf;
  4665. module->name_section_buf_end = buf_end;
  4666. p += name_len;
  4667. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4668. error_buf, error_buf_size)) {
  4669. return false;
  4670. }
  4671. LOG_VERBOSE("Load custom name section success.");
  4672. }
  4673. #endif
  4674. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4675. {
  4676. WASMCustomSection *section =
  4677. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4678. if (!section) {
  4679. return false;
  4680. }
  4681. section->name_addr = (char *)p;
  4682. section->name_len = name_len;
  4683. section->content_addr = (uint8 *)(p + name_len);
  4684. section->content_len = (uint32)(p_end - p - name_len);
  4685. section->next = module->custom_section_list;
  4686. module->custom_section_list = section;
  4687. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4688. return true;
  4689. }
  4690. #endif
  4691. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4692. (void)is_load_from_file_buf;
  4693. (void)module;
  4694. return true;
  4695. fail:
  4696. return false;
  4697. }
  4698. static void
  4699. calculate_global_data_offset(WASMModule *module)
  4700. {
  4701. uint32 i, data_offset;
  4702. data_offset = 0;
  4703. for (i = 0; i < module->import_global_count; i++) {
  4704. WASMGlobalImport *import_global =
  4705. &((module->import_globals + i)->u.global);
  4706. #if WASM_ENABLE_FAST_JIT != 0
  4707. import_global->data_offset = data_offset;
  4708. #endif
  4709. data_offset += wasm_value_type_size(import_global->type.val_type);
  4710. }
  4711. for (i = 0; i < module->global_count; i++) {
  4712. WASMGlobal *global = module->globals + i;
  4713. #if WASM_ENABLE_FAST_JIT != 0
  4714. global->data_offset = data_offset;
  4715. #endif
  4716. data_offset += wasm_value_type_size(global->type.val_type);
  4717. }
  4718. module->global_data_size = data_offset;
  4719. }
  4720. #if WASM_ENABLE_FAST_JIT != 0
  4721. static bool
  4722. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4723. uint32 error_buf_size)
  4724. {
  4725. #if WASM_ENABLE_LAZY_JIT != 0
  4726. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4727. #endif
  4728. uint32 i;
  4729. if (!module->function_count)
  4730. return true;
  4731. if (!(module->fast_jit_func_ptrs =
  4732. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4733. error_buf_size))) {
  4734. return false;
  4735. }
  4736. #if WASM_ENABLE_LAZY_JIT != 0
  4737. for (i = 0; i < module->function_count; i++) {
  4738. module->fast_jit_func_ptrs[i] =
  4739. jit_globals->compile_fast_jit_and_then_call;
  4740. }
  4741. #endif
  4742. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4743. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4744. set_error_buf(error_buf, error_buf_size,
  4745. "init fast jit thread lock failed");
  4746. return false;
  4747. }
  4748. module->fast_jit_thread_locks_inited[i] = true;
  4749. }
  4750. return true;
  4751. }
  4752. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4753. #if WASM_ENABLE_JIT != 0
  4754. static bool
  4755. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4756. uint32 error_buf_size)
  4757. {
  4758. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4759. AOTCompOption option = { 0 };
  4760. char *aot_last_error;
  4761. uint64 size;
  4762. #if WASM_ENABLE_GC != 0
  4763. bool gc_enabled = true;
  4764. #else
  4765. bool gc_enabled = false;
  4766. #endif
  4767. if (module->function_count == 0)
  4768. return true;
  4769. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4770. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4771. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4772. return false;
  4773. }
  4774. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4775. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4776. os_mutex_destroy(&module->tierup_wait_lock);
  4777. return false;
  4778. }
  4779. module->tierup_wait_lock_inited = true;
  4780. #endif
  4781. size = sizeof(void *) * (uint64)module->function_count
  4782. + sizeof(bool) * (uint64)module->function_count;
  4783. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4784. return false;
  4785. }
  4786. module->func_ptrs_compiled =
  4787. (bool *)((uint8 *)module->func_ptrs
  4788. + sizeof(void *) * module->function_count);
  4789. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4790. if (!module->comp_data) {
  4791. aot_last_error = aot_get_last_error();
  4792. bh_assert(aot_last_error != NULL);
  4793. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4794. return false;
  4795. }
  4796. option.is_jit_mode = true;
  4797. option.opt_level = llvm_jit_options->opt_level;
  4798. option.size_level = llvm_jit_options->size_level;
  4799. option.segue_flags = llvm_jit_options->segue_flags;
  4800. option.quick_invoke_c_api_import =
  4801. llvm_jit_options->quick_invoke_c_api_import;
  4802. #if WASM_ENABLE_BULK_MEMORY != 0
  4803. option.enable_bulk_memory = true;
  4804. #endif
  4805. #if WASM_ENABLE_THREAD_MGR != 0
  4806. option.enable_thread_mgr = true;
  4807. #endif
  4808. #if WASM_ENABLE_TAIL_CALL != 0
  4809. option.enable_tail_call = true;
  4810. #endif
  4811. #if WASM_ENABLE_SIMD != 0
  4812. option.enable_simd = true;
  4813. #endif
  4814. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4815. option.enable_ref_types = true;
  4816. #elif WASM_ENABLE_GC != 0
  4817. option.enable_gc = true;
  4818. #endif
  4819. option.enable_aux_stack_check = true;
  4820. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4821. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4822. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  4823. aot_call_stack_features_init_default(&option.call_stack_features);
  4824. #endif
  4825. #if WASM_ENABLE_PERF_PROFILING != 0
  4826. option.enable_perf_profiling = true;
  4827. #endif
  4828. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4829. option.enable_memory_profiling = true;
  4830. option.enable_stack_estimation = true;
  4831. #endif
  4832. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4833. if (!module->comp_ctx) {
  4834. aot_last_error = aot_get_last_error();
  4835. bh_assert(aot_last_error != NULL);
  4836. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4837. return false;
  4838. }
  4839. return true;
  4840. }
  4841. static bool
  4842. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4843. uint32 error_buf_size)
  4844. {
  4845. char *aot_last_error;
  4846. uint32 i;
  4847. if (module->function_count == 0)
  4848. return true;
  4849. if (!aot_compile_wasm(module->comp_ctx)) {
  4850. aot_last_error = aot_get_last_error();
  4851. bh_assert(aot_last_error != NULL);
  4852. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4853. return false;
  4854. }
  4855. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4856. if (module->orcjit_stop_compiling)
  4857. return false;
  4858. #endif
  4859. bh_print_time("Begin to lookup llvm jit functions");
  4860. for (i = 0; i < module->function_count; i++) {
  4861. LLVMOrcJITTargetAddress func_addr = 0;
  4862. LLVMErrorRef error;
  4863. char func_name[48];
  4864. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4865. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4866. func_name);
  4867. if (error != LLVMErrorSuccess) {
  4868. char *err_msg = LLVMGetErrorMessage(error);
  4869. set_error_buf_v(error_buf, error_buf_size,
  4870. "failed to compile llvm jit function: %s", err_msg);
  4871. LLVMDisposeErrorMessage(err_msg);
  4872. return false;
  4873. }
  4874. /**
  4875. * No need to lock the func_ptr[func_idx] here as it is basic
  4876. * data type, the load/store for it can be finished by one cpu
  4877. * instruction, and there can be only one cpu instruction
  4878. * loading/storing at the same time.
  4879. */
  4880. module->func_ptrs[i] = (void *)func_addr;
  4881. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4882. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4883. if (module->orcjit_stop_compiling)
  4884. return false;
  4885. #endif
  4886. }
  4887. bh_print_time("End lookup llvm jit functions");
  4888. return true;
  4889. }
  4890. #endif /* end of WASM_ENABLE_JIT != 0 */
  4891. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4892. && WASM_ENABLE_LAZY_JIT != 0
  4893. static void *
  4894. init_llvm_jit_functions_stage2_callback(void *arg)
  4895. {
  4896. WASMModule *module = (WASMModule *)arg;
  4897. char error_buf[128];
  4898. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4899. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4900. module->orcjit_stop_compiling = true;
  4901. return NULL;
  4902. }
  4903. os_mutex_lock(&module->tierup_wait_lock);
  4904. module->llvm_jit_inited = true;
  4905. os_cond_broadcast(&module->tierup_wait_cond);
  4906. os_mutex_unlock(&module->tierup_wait_lock);
  4907. return NULL;
  4908. }
  4909. #endif
  4910. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4911. /* The callback function to compile jit functions */
  4912. static void *
  4913. orcjit_thread_callback(void *arg)
  4914. {
  4915. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4916. #if WASM_ENABLE_JIT != 0
  4917. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4918. #endif
  4919. WASMModule *module = thread_arg->module;
  4920. uint32 group_idx = thread_arg->group_idx;
  4921. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4922. uint32 func_count = module->function_count;
  4923. uint32 i;
  4924. #if WASM_ENABLE_FAST_JIT != 0
  4925. /* Compile fast jit functions of this group */
  4926. for (i = group_idx; i < func_count; i += group_stride) {
  4927. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4928. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4929. break;
  4930. }
  4931. if (module->orcjit_stop_compiling) {
  4932. return NULL;
  4933. }
  4934. }
  4935. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4936. os_mutex_lock(&module->tierup_wait_lock);
  4937. module->fast_jit_ready_groups++;
  4938. os_mutex_unlock(&module->tierup_wait_lock);
  4939. #endif
  4940. #endif
  4941. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4942. && WASM_ENABLE_LAZY_JIT != 0
  4943. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4944. for (i = group_idx; i < func_count;
  4945. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4946. uint32 j;
  4947. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4948. if (i + j * group_stride < func_count) {
  4949. if (!jit_compiler_set_call_to_fast_jit(
  4950. module,
  4951. i + j * group_stride + module->import_function_count)) {
  4952. LOG_ERROR(
  4953. "failed to compile call_to_fast_jit for func %u\n",
  4954. i + j * group_stride + module->import_function_count);
  4955. module->orcjit_stop_compiling = true;
  4956. return NULL;
  4957. }
  4958. }
  4959. if (module->orcjit_stop_compiling) {
  4960. return NULL;
  4961. }
  4962. }
  4963. }
  4964. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4965. fast jit functions are compiled */
  4966. os_mutex_lock(&module->tierup_wait_lock);
  4967. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4968. && module->fast_jit_ready_groups >= group_stride)) {
  4969. os_cond_reltimedwait(&module->tierup_wait_cond,
  4970. &module->tierup_wait_lock, 10000);
  4971. if (module->orcjit_stop_compiling) {
  4972. /* init_llvm_jit_functions_stage2 failed */
  4973. os_mutex_unlock(&module->tierup_wait_lock);
  4974. return NULL;
  4975. }
  4976. }
  4977. os_mutex_unlock(&module->tierup_wait_lock);
  4978. #endif
  4979. #if WASM_ENABLE_JIT != 0
  4980. /* Compile llvm jit functions of this group */
  4981. for (i = group_idx; i < func_count;
  4982. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4983. LLVMOrcJITTargetAddress func_addr = 0;
  4984. LLVMErrorRef error;
  4985. char func_name[48];
  4986. typedef void (*F)(void);
  4987. union {
  4988. F f;
  4989. void *v;
  4990. } u;
  4991. uint32 j;
  4992. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  4993. "_wrapper");
  4994. LOG_DEBUG("compile llvm jit func %s", func_name);
  4995. error =
  4996. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  4997. if (error != LLVMErrorSuccess) {
  4998. char *err_msg = LLVMGetErrorMessage(error);
  4999. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  5000. LLVMDisposeErrorMessage(err_msg);
  5001. break;
  5002. }
  5003. /* Call the jit wrapper function to trigger its compilation, so as
  5004. to compile the actual jit functions, since we add the latter to
  5005. function list in the PartitionFunction callback */
  5006. u.v = (void *)func_addr;
  5007. u.f();
  5008. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5009. if (i + j * group_stride < func_count) {
  5010. module->func_ptrs_compiled[i + j * group_stride] = true;
  5011. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5012. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5013. i + j * group_stride);
  5014. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5015. func_name);
  5016. if (error != LLVMErrorSuccess) {
  5017. char *err_msg = LLVMGetErrorMessage(error);
  5018. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5019. err_msg);
  5020. LLVMDisposeErrorMessage(err_msg);
  5021. /* Ignore current llvm jit func, as its func ptr is
  5022. previous set to call_to_fast_jit, which also works */
  5023. continue;
  5024. }
  5025. jit_compiler_set_llvm_jit_func_ptr(
  5026. module,
  5027. i + j * group_stride + module->import_function_count,
  5028. (void *)func_addr);
  5029. /* Try to switch to call this llvm jit function instead of
  5030. fast jit function from fast jit jitted code */
  5031. jit_compiler_set_call_to_llvm_jit(
  5032. module,
  5033. i + j * group_stride + module->import_function_count);
  5034. #endif
  5035. }
  5036. }
  5037. if (module->orcjit_stop_compiling) {
  5038. break;
  5039. }
  5040. }
  5041. #endif
  5042. return NULL;
  5043. }
  5044. static void
  5045. orcjit_stop_compile_threads(WASMModule *module)
  5046. {
  5047. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5048. / sizeof(OrcJitThreadArg));
  5049. module->orcjit_stop_compiling = true;
  5050. for (i = 0; i < thread_num; i++) {
  5051. if (module->orcjit_threads[i])
  5052. os_thread_join(module->orcjit_threads[i], NULL);
  5053. }
  5054. }
  5055. static bool
  5056. compile_jit_functions(WASMModule *module, char *error_buf,
  5057. uint32 error_buf_size)
  5058. {
  5059. uint32 thread_num =
  5060. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5061. uint32 i, j;
  5062. bh_print_time("Begin to compile jit functions");
  5063. /* Create threads to compile the jit functions */
  5064. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5065. #if WASM_ENABLE_JIT != 0
  5066. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5067. #endif
  5068. module->orcjit_thread_args[i].module = module;
  5069. module->orcjit_thread_args[i].group_idx = i;
  5070. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5071. (void *)&module->orcjit_thread_args[i],
  5072. APP_THREAD_STACK_SIZE_DEFAULT)
  5073. != 0) {
  5074. set_error_buf(error_buf, error_buf_size,
  5075. "create orcjit compile thread failed");
  5076. /* Terminate the threads created */
  5077. module->orcjit_stop_compiling = true;
  5078. for (j = 0; j < i; j++) {
  5079. os_thread_join(module->orcjit_threads[j], NULL);
  5080. }
  5081. return false;
  5082. }
  5083. }
  5084. #if WASM_ENABLE_LAZY_JIT == 0
  5085. /* Wait until all jit functions are compiled for eager mode */
  5086. for (i = 0; i < thread_num; i++) {
  5087. if (module->orcjit_threads[i])
  5088. os_thread_join(module->orcjit_threads[i], NULL);
  5089. }
  5090. #if WASM_ENABLE_FAST_JIT != 0
  5091. /* Ensure all the fast-jit functions are compiled */
  5092. for (i = 0; i < module->function_count; i++) {
  5093. if (!jit_compiler_is_compiled(module,
  5094. i + module->import_function_count)) {
  5095. set_error_buf(error_buf, error_buf_size,
  5096. "failed to compile fast jit function");
  5097. return false;
  5098. }
  5099. }
  5100. #endif
  5101. #if WASM_ENABLE_JIT != 0
  5102. /* Ensure all the llvm-jit functions are compiled */
  5103. for (i = 0; i < module->function_count; i++) {
  5104. if (!module->func_ptrs_compiled[i]) {
  5105. set_error_buf(error_buf, error_buf_size,
  5106. "failed to compile llvm jit function");
  5107. return false;
  5108. }
  5109. }
  5110. #endif
  5111. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5112. bh_print_time("End compile jit functions");
  5113. return true;
  5114. }
  5115. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5116. static bool
  5117. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5118. uint32 cur_func_idx, char *error_buf,
  5119. uint32 error_buf_size);
  5120. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5121. void **
  5122. wasm_interp_get_handle_table(void);
  5123. static void **handle_table;
  5124. #endif
  5125. static bool
  5126. load_from_sections(WASMModule *module, WASMSection *sections,
  5127. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5128. char *error_buf, uint32 error_buf_size)
  5129. {
  5130. WASMExport *export;
  5131. WASMSection *section = sections;
  5132. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5133. *buf_func = NULL, *buf_func_end = NULL;
  5134. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5135. WASMGlobal *aux_stack_top_global = NULL, *global;
  5136. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5137. aux_stack_top = (uint64)-1LL;
  5138. uint32 global_index, func_index, i;
  5139. uint32 aux_data_end_global_index = (uint32)-1;
  5140. uint32 aux_heap_base_global_index = (uint32)-1;
  5141. WASMFuncType *func_type;
  5142. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5143. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5144. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5145. #if WASM_ENABLE_BULK_MEMORY != 0
  5146. bool has_datacount_section = false;
  5147. #endif
  5148. /* Find code and function sections if have */
  5149. while (section) {
  5150. if (section->section_type == SECTION_TYPE_CODE) {
  5151. buf_code = section->section_body;
  5152. buf_code_end = buf_code + section->section_body_size;
  5153. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5154. module->buf_code = (uint8 *)buf_code;
  5155. module->buf_code_size = section->section_body_size;
  5156. #endif
  5157. }
  5158. else if (section->section_type == SECTION_TYPE_FUNC) {
  5159. buf_func = section->section_body;
  5160. buf_func_end = buf_func + section->section_body_size;
  5161. }
  5162. section = section->next;
  5163. }
  5164. section = sections;
  5165. while (section) {
  5166. buf = section->section_body;
  5167. buf_end = buf + section->section_body_size;
  5168. switch (section->section_type) {
  5169. case SECTION_TYPE_USER:
  5170. /* unsupported user section, ignore it. */
  5171. if (!load_user_section(buf, buf_end, module,
  5172. reuse_const_strings, error_buf,
  5173. error_buf_size))
  5174. return false;
  5175. break;
  5176. case SECTION_TYPE_TYPE:
  5177. if (!load_type_section(buf, buf_end, module, error_buf,
  5178. error_buf_size))
  5179. return false;
  5180. break;
  5181. case SECTION_TYPE_IMPORT:
  5182. if (!load_import_section(buf, buf_end, module,
  5183. reuse_const_strings, error_buf,
  5184. error_buf_size))
  5185. return false;
  5186. break;
  5187. case SECTION_TYPE_FUNC:
  5188. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5189. module, error_buf, error_buf_size))
  5190. return false;
  5191. break;
  5192. case SECTION_TYPE_TABLE:
  5193. if (!load_table_section(buf, buf_end, module, error_buf,
  5194. error_buf_size))
  5195. return false;
  5196. break;
  5197. case SECTION_TYPE_MEMORY:
  5198. if (!load_memory_section(buf, buf_end, module, error_buf,
  5199. error_buf_size))
  5200. return false;
  5201. break;
  5202. #if WASM_ENABLE_TAGS != 0
  5203. case SECTION_TYPE_TAG:
  5204. /* load tag declaration section */
  5205. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5206. module, error_buf, error_buf_size))
  5207. return false;
  5208. break;
  5209. #endif
  5210. case SECTION_TYPE_GLOBAL:
  5211. if (!load_global_section(buf, buf_end, module, error_buf,
  5212. error_buf_size))
  5213. return false;
  5214. break;
  5215. case SECTION_TYPE_EXPORT:
  5216. if (!load_export_section(buf, buf_end, module,
  5217. reuse_const_strings, error_buf,
  5218. error_buf_size))
  5219. return false;
  5220. break;
  5221. case SECTION_TYPE_START:
  5222. if (!load_start_section(buf, buf_end, module, error_buf,
  5223. error_buf_size))
  5224. return false;
  5225. break;
  5226. case SECTION_TYPE_ELEM:
  5227. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5228. error_buf_size))
  5229. return false;
  5230. break;
  5231. case SECTION_TYPE_CODE:
  5232. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5233. module, error_buf, error_buf_size))
  5234. return false;
  5235. break;
  5236. case SECTION_TYPE_DATA:
  5237. if (!load_data_segment_section(buf, buf_end, module,
  5238. #if WASM_ENABLE_BULK_MEMORY != 0
  5239. has_datacount_section,
  5240. #endif
  5241. clone_data_seg, error_buf,
  5242. error_buf_size))
  5243. return false;
  5244. break;
  5245. #if WASM_ENABLE_BULK_MEMORY != 0
  5246. case SECTION_TYPE_DATACOUNT:
  5247. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5248. error_buf_size))
  5249. return false;
  5250. has_datacount_section = true;
  5251. break;
  5252. #endif
  5253. #if WASM_ENABLE_STRINGREF != 0
  5254. case SECTION_TYPE_STRINGREF:
  5255. if (!load_stringref_section(buf, buf_end, module,
  5256. reuse_const_strings, error_buf,
  5257. error_buf_size))
  5258. return false;
  5259. break;
  5260. #endif
  5261. default:
  5262. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5263. return false;
  5264. }
  5265. section = section->next;
  5266. }
  5267. module->aux_data_end_global_index = (uint32)-1;
  5268. module->aux_heap_base_global_index = (uint32)-1;
  5269. module->aux_stack_top_global_index = (uint32)-1;
  5270. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5271. export = module->exports;
  5272. for (i = 0; i < module->export_count; i++, export ++) {
  5273. if (export->kind == EXPORT_KIND_GLOBAL) {
  5274. if (!strcmp(export->name, "__heap_base")) {
  5275. global_index = export->index - module->import_global_count;
  5276. global = module->globals + global_index;
  5277. if (global->type.val_type == VALUE_TYPE_I32
  5278. && !global->type.is_mutable
  5279. && global->init_expr.init_expr_type
  5280. == INIT_EXPR_TYPE_I32_CONST) {
  5281. aux_heap_base_global = global;
  5282. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5283. aux_heap_base_global_index = export->index;
  5284. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5285. aux_heap_base);
  5286. }
  5287. }
  5288. else if (!strcmp(export->name, "__data_end")) {
  5289. global_index = export->index - module->import_global_count;
  5290. global = module->globals + global_index;
  5291. if (global->type.val_type == VALUE_TYPE_I32
  5292. && !global->type.is_mutable
  5293. && global->init_expr.init_expr_type
  5294. == INIT_EXPR_TYPE_I32_CONST) {
  5295. aux_data_end_global = global;
  5296. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5297. aux_data_end_global_index = export->index;
  5298. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5299. aux_data_end);
  5300. aux_data_end = align_uint64(aux_data_end, 16);
  5301. }
  5302. }
  5303. /* For module compiled with -pthread option, the global is:
  5304. [0] stack_top <-- 0
  5305. [1] tls_pointer
  5306. [2] tls_size
  5307. [3] data_end <-- 3
  5308. [4] global_base
  5309. [5] heap_base <-- 5
  5310. [6] dso_handle
  5311. For module compiled without -pthread option:
  5312. [0] stack_top <-- 0
  5313. [1] data_end <-- 1
  5314. [2] global_base
  5315. [3] heap_base <-- 3
  5316. [4] dso_handle
  5317. */
  5318. if (aux_data_end_global && aux_heap_base_global
  5319. && aux_data_end <= aux_heap_base) {
  5320. module->aux_data_end_global_index = aux_data_end_global_index;
  5321. module->aux_data_end = aux_data_end;
  5322. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5323. module->aux_heap_base = aux_heap_base;
  5324. /* Resolve aux stack top global */
  5325. for (global_index = 0; global_index < module->global_count;
  5326. global_index++) {
  5327. global = module->globals + global_index;
  5328. if (global->type.is_mutable /* heap_base and data_end is
  5329. not mutable */
  5330. && global->type.val_type == VALUE_TYPE_I32
  5331. && global->init_expr.init_expr_type
  5332. == INIT_EXPR_TYPE_I32_CONST
  5333. && (uint64)(uint32)global->init_expr.u.i32
  5334. <= aux_heap_base) {
  5335. aux_stack_top_global = global;
  5336. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5337. module->aux_stack_top_global_index =
  5338. module->import_global_count + global_index;
  5339. module->aux_stack_bottom = aux_stack_top;
  5340. module->aux_stack_size =
  5341. aux_stack_top > aux_data_end
  5342. ? (uint32)(aux_stack_top - aux_data_end)
  5343. : (uint32)aux_stack_top;
  5344. LOG_VERBOSE(
  5345. "Found aux stack top global, value: %" PRIu64 ", "
  5346. "global index: %d, stack size: %d",
  5347. aux_stack_top, global_index,
  5348. module->aux_stack_size);
  5349. break;
  5350. }
  5351. }
  5352. if (!aux_stack_top_global) {
  5353. /* Auxiliary stack global isn't found, it must be unused
  5354. in the wasm app, as if it is used, the global must be
  5355. defined. Here we set it to __heap_base global and set
  5356. its size to 0. */
  5357. aux_stack_top_global = aux_heap_base_global;
  5358. aux_stack_top = aux_heap_base;
  5359. module->aux_stack_top_global_index =
  5360. module->aux_heap_base_global_index;
  5361. module->aux_stack_bottom = aux_stack_top;
  5362. module->aux_stack_size = 0;
  5363. }
  5364. break;
  5365. }
  5366. }
  5367. }
  5368. module->malloc_function = (uint32)-1;
  5369. module->free_function = (uint32)-1;
  5370. module->retain_function = (uint32)-1;
  5371. /* Resolve malloc/free function exported by wasm module */
  5372. #if WASM_ENABLE_MEMORY64 != 0
  5373. if (has_module_memory64(module))
  5374. malloc_free_io_type = VALUE_TYPE_I64;
  5375. #endif
  5376. export = module->exports;
  5377. for (i = 0; i < module->export_count; i++, export ++) {
  5378. if (export->kind == EXPORT_KIND_FUNC) {
  5379. if (!strcmp(export->name, "malloc")
  5380. && export->index >= module->import_function_count) {
  5381. func_index = export->index - module->import_function_count;
  5382. func_type = module->functions[func_index]->func_type;
  5383. if (func_type->param_count == 1 && func_type->result_count == 1
  5384. && func_type->types[0] == malloc_free_io_type
  5385. && func_type->types[1] == malloc_free_io_type) {
  5386. bh_assert(module->malloc_function == (uint32)-1);
  5387. module->malloc_function = export->index;
  5388. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5389. export->name, export->index);
  5390. }
  5391. }
  5392. else if (!strcmp(export->name, "__new")
  5393. && export->index >= module->import_function_count) {
  5394. /* __new && __pin for AssemblyScript */
  5395. func_index = export->index - module->import_function_count;
  5396. func_type = module->functions[func_index]->func_type;
  5397. if (func_type->param_count == 2 && func_type->result_count == 1
  5398. && func_type->types[0] == malloc_free_io_type
  5399. && func_type->types[1] == VALUE_TYPE_I32
  5400. && func_type->types[2] == malloc_free_io_type) {
  5401. uint32 j;
  5402. WASMExport *export_tmp;
  5403. bh_assert(module->malloc_function == (uint32)-1);
  5404. module->malloc_function = export->index;
  5405. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5406. export->name, export->index);
  5407. /* resolve retain function.
  5408. If not found, reset malloc function index */
  5409. export_tmp = module->exports;
  5410. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5411. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5412. && (!strcmp(export_tmp->name, "__retain")
  5413. || (!strcmp(export_tmp->name, "__pin")))
  5414. && (export_tmp->index
  5415. >= module->import_function_count)) {
  5416. func_index = export_tmp->index
  5417. - module->import_function_count;
  5418. func_type =
  5419. module->functions[func_index]->func_type;
  5420. if (func_type->param_count == 1
  5421. && func_type->result_count == 1
  5422. && func_type->types[0] == malloc_free_io_type
  5423. && func_type->types[1] == malloc_free_io_type) {
  5424. bh_assert(module->retain_function
  5425. == (uint32)-1);
  5426. module->retain_function = export_tmp->index;
  5427. LOG_VERBOSE("Found retain function, name: %s, "
  5428. "index: %u",
  5429. export_tmp->name,
  5430. export_tmp->index);
  5431. break;
  5432. }
  5433. }
  5434. }
  5435. if (j == module->export_count) {
  5436. module->malloc_function = (uint32)-1;
  5437. LOG_VERBOSE("Can't find retain function,"
  5438. "reset malloc function index to -1");
  5439. }
  5440. }
  5441. }
  5442. else if (((!strcmp(export->name, "free"))
  5443. || (!strcmp(export->name, "__release"))
  5444. || (!strcmp(export->name, "__unpin")))
  5445. && export->index >= module->import_function_count) {
  5446. func_index = export->index - module->import_function_count;
  5447. func_type = module->functions[func_index]->func_type;
  5448. if (func_type->param_count == 1 && func_type->result_count == 0
  5449. && func_type->types[0] == malloc_free_io_type) {
  5450. bh_assert(module->free_function == (uint32)-1);
  5451. module->free_function = export->index;
  5452. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5453. export->name, export->index);
  5454. }
  5455. }
  5456. }
  5457. }
  5458. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5459. handle_table = wasm_interp_get_handle_table();
  5460. #endif
  5461. for (i = 0; i < module->function_count; i++) {
  5462. WASMFunction *func = module->functions[i];
  5463. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5464. error_buf_size)) {
  5465. return false;
  5466. }
  5467. if (i == module->function_count - 1
  5468. && func->code + func->code_size != buf_code_end) {
  5469. set_error_buf(error_buf, error_buf_size,
  5470. "code section size mismatch");
  5471. return false;
  5472. }
  5473. }
  5474. if (!module->possible_memory_grow) {
  5475. WASMMemoryImport *memory_import;
  5476. WASMMemory *memory;
  5477. if (aux_data_end_global && aux_heap_base_global
  5478. && aux_stack_top_global) {
  5479. uint64 init_memory_size;
  5480. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5481. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5482. * valid range of uint32 */
  5483. if (shrunk_memory_size <= UINT32_MAX) {
  5484. if (module->import_memory_count) {
  5485. memory_import = &module->import_memories[0].u.memory;
  5486. init_memory_size =
  5487. (uint64)memory_import->mem_type.num_bytes_per_page
  5488. * memory_import->mem_type.init_page_count;
  5489. if (shrunk_memory_size <= init_memory_size) {
  5490. /* Reset memory info to decrease memory usage */
  5491. memory_import->mem_type.num_bytes_per_page =
  5492. (uint32)shrunk_memory_size;
  5493. memory_import->mem_type.init_page_count = 1;
  5494. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5495. shrunk_memory_size);
  5496. }
  5497. }
  5498. if (module->memory_count) {
  5499. memory = &module->memories[0];
  5500. init_memory_size = (uint64)memory->num_bytes_per_page
  5501. * memory->init_page_count;
  5502. if (shrunk_memory_size <= init_memory_size) {
  5503. /* Reset memory info to decrease memory usage */
  5504. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5505. memory->init_page_count = 1;
  5506. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5507. shrunk_memory_size);
  5508. }
  5509. }
  5510. }
  5511. }
  5512. #if WASM_ENABLE_MULTI_MODULE == 0
  5513. if (module->import_memory_count) {
  5514. memory_import = &module->import_memories[0].u.memory;
  5515. /* Only resize the memory to one big page if num_bytes_per_page is
  5516. * in valid range of uint32 */
  5517. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5518. memory_import->mem_type.num_bytes_per_page *=
  5519. memory_import->mem_type.init_page_count;
  5520. if (memory_import->mem_type.init_page_count > 0)
  5521. memory_import->mem_type.init_page_count =
  5522. memory_import->mem_type.max_page_count = 1;
  5523. else
  5524. memory_import->mem_type.init_page_count =
  5525. memory_import->mem_type.max_page_count = 0;
  5526. }
  5527. }
  5528. if (module->memory_count) {
  5529. memory = &module->memories[0];
  5530. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5531. * valid range of uint32 */
  5532. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5533. memory->num_bytes_per_page *= memory->init_page_count;
  5534. if (memory->init_page_count > 0)
  5535. memory->init_page_count = memory->max_page_count = 1;
  5536. else
  5537. memory->init_page_count = memory->max_page_count = 0;
  5538. }
  5539. }
  5540. #endif
  5541. }
  5542. calculate_global_data_offset(module);
  5543. #if WASM_ENABLE_FAST_JIT != 0
  5544. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5545. return false;
  5546. }
  5547. #endif
  5548. #if WASM_ENABLE_JIT != 0
  5549. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5550. return false;
  5551. }
  5552. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5553. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5554. return false;
  5555. }
  5556. #else
  5557. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5558. thread fast jit execution, since applying llvm optimizations in
  5559. aot_compile_wasm may cost a lot of time.
  5560. Create thread with enough native stack to apply llvm optimizations */
  5561. if (os_thread_create(&module->llvm_jit_init_thread,
  5562. init_llvm_jit_functions_stage2_callback,
  5563. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5564. != 0) {
  5565. set_error_buf(error_buf, error_buf_size,
  5566. "create orcjit compile thread failed");
  5567. return false;
  5568. }
  5569. #endif
  5570. #endif
  5571. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5572. /* Create threads to compile the jit functions */
  5573. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5574. return false;
  5575. }
  5576. #endif
  5577. #if WASM_ENABLE_MEMORY_TRACING != 0
  5578. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5579. #endif
  5580. return true;
  5581. }
  5582. static WASMModule *
  5583. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5584. {
  5585. WASMModule *module =
  5586. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5587. bh_list_status ret;
  5588. if (!module) {
  5589. return NULL;
  5590. }
  5591. module->module_type = Wasm_Module_Bytecode;
  5592. /* Set start_function to -1, means no start function */
  5593. module->start_function = (uint32)-1;
  5594. module->name = name;
  5595. module->is_binary_freeable = false;
  5596. #if WASM_ENABLE_FAST_INTERP == 0
  5597. module->br_table_cache_list = &module->br_table_cache_list_head;
  5598. ret = bh_list_init(module->br_table_cache_list);
  5599. bh_assert(ret == BH_LIST_SUCCESS);
  5600. #endif
  5601. #if WASM_ENABLE_MULTI_MODULE != 0
  5602. module->import_module_list = &module->import_module_list_head;
  5603. ret = bh_list_init(module->import_module_list);
  5604. bh_assert(ret == BH_LIST_SUCCESS);
  5605. #endif
  5606. #if WASM_ENABLE_DEBUG_INTERP != 0
  5607. ret = bh_list_init(&module->fast_opcode_list);
  5608. bh_assert(ret == BH_LIST_SUCCESS);
  5609. #endif
  5610. #if WASM_ENABLE_GC != 0
  5611. if (!(module->ref_type_set =
  5612. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5613. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5614. goto fail1;
  5615. }
  5616. if (os_mutex_init(&module->rtt_type_lock)) {
  5617. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5618. goto fail2;
  5619. }
  5620. #endif
  5621. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5622. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5623. && WASM_ENABLE_LAZY_JIT != 0)
  5624. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5625. set_error_buf(error_buf, error_buf_size,
  5626. "init instance list lock failed");
  5627. goto fail3;
  5628. }
  5629. #endif
  5630. (void)ret;
  5631. return module;
  5632. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5633. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5634. && WASM_ENABLE_LAZY_JIT != 0)
  5635. fail3:
  5636. #endif
  5637. #if WASM_ENABLE_GC != 0
  5638. os_mutex_destroy(&module->rtt_type_lock);
  5639. fail2:
  5640. bh_hash_map_destroy(module->ref_type_set);
  5641. fail1:
  5642. #endif
  5643. wasm_runtime_free(module);
  5644. return NULL;
  5645. }
  5646. #if WASM_ENABLE_DEBUG_INTERP != 0
  5647. static bool
  5648. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5649. uint32 error_buf_size)
  5650. {
  5651. WASMFastOPCodeNode *fast_op =
  5652. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5653. if (fast_op) {
  5654. fast_op->offset = pos - module->load_addr;
  5655. fast_op->orig_op = orig_op;
  5656. bh_list_insert(&module->fast_opcode_list, fast_op);
  5657. }
  5658. return fast_op ? true : false;
  5659. }
  5660. #endif
  5661. WASMModule *
  5662. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5663. uint32 error_buf_size)
  5664. {
  5665. WASMModule *module = create_module("", error_buf, error_buf_size);
  5666. if (!module)
  5667. return NULL;
  5668. if (!load_from_sections(module, section_list, false, true, error_buf,
  5669. error_buf_size)) {
  5670. wasm_loader_unload(module);
  5671. return NULL;
  5672. }
  5673. LOG_VERBOSE("Load module from sections success.\n");
  5674. return module;
  5675. }
  5676. static void
  5677. destroy_sections(WASMSection *section_list)
  5678. {
  5679. WASMSection *section = section_list, *next;
  5680. while (section) {
  5681. next = section->next;
  5682. wasm_runtime_free(section);
  5683. section = next;
  5684. }
  5685. }
  5686. /* clang-format off */
  5687. static uint8 section_ids[] = {
  5688. SECTION_TYPE_USER,
  5689. SECTION_TYPE_TYPE,
  5690. SECTION_TYPE_IMPORT,
  5691. SECTION_TYPE_FUNC,
  5692. SECTION_TYPE_TABLE,
  5693. SECTION_TYPE_MEMORY,
  5694. #if WASM_ENABLE_TAGS != 0
  5695. SECTION_TYPE_TAG,
  5696. #endif
  5697. #if WASM_ENABLE_STRINGREF != 0
  5698. /* must immediately precede the global section,
  5699. or where the global section would be */
  5700. SECTION_TYPE_STRINGREF,
  5701. #endif
  5702. SECTION_TYPE_GLOBAL,
  5703. SECTION_TYPE_EXPORT,
  5704. SECTION_TYPE_START,
  5705. SECTION_TYPE_ELEM,
  5706. #if WASM_ENABLE_BULK_MEMORY != 0
  5707. SECTION_TYPE_DATACOUNT,
  5708. #endif
  5709. SECTION_TYPE_CODE,
  5710. SECTION_TYPE_DATA
  5711. };
  5712. /* clang-format on */
  5713. static uint8
  5714. get_section_index(uint8 section_type)
  5715. {
  5716. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5717. for (uint8 i = 0; i < max_id; i++) {
  5718. if (section_type == section_ids[i])
  5719. return i;
  5720. }
  5721. return (uint8)-1;
  5722. }
  5723. static bool
  5724. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5725. char *error_buf, uint32 error_buf_size)
  5726. {
  5727. WASMSection *section_list_end = NULL, *section;
  5728. const uint8 *p = buf, *p_end = buf + size;
  5729. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5730. uint32 section_size;
  5731. bh_assert(!*p_section_list);
  5732. p += 8;
  5733. while (p < p_end) {
  5734. CHECK_BUF(p, p_end, 1);
  5735. section_type = read_uint8(p);
  5736. section_index = get_section_index(section_type);
  5737. if (section_index != (uint8)-1) {
  5738. if (section_type != SECTION_TYPE_USER) {
  5739. /* Custom sections may be inserted at any place,
  5740. while other sections must occur at most once
  5741. and in prescribed order. */
  5742. if (last_section_index != (uint8)-1
  5743. && (section_index <= last_section_index)) {
  5744. set_error_buf(error_buf, error_buf_size,
  5745. "unexpected content after last section or "
  5746. "junk after last section");
  5747. return false;
  5748. }
  5749. last_section_index = section_index;
  5750. }
  5751. read_leb_uint32(p, p_end, section_size);
  5752. CHECK_BUF1(p, p_end, section_size);
  5753. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5754. error_buf_size))) {
  5755. return false;
  5756. }
  5757. section->section_type = section_type;
  5758. section->section_body = (uint8 *)p;
  5759. section->section_body_size = section_size;
  5760. if (!section_list_end)
  5761. *p_section_list = section_list_end = section;
  5762. else {
  5763. section_list_end->next = section;
  5764. section_list_end = section;
  5765. }
  5766. p += section_size;
  5767. }
  5768. else {
  5769. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5770. return false;
  5771. }
  5772. }
  5773. return true;
  5774. fail:
  5775. return false;
  5776. }
  5777. static void
  5778. exchange32(uint8 *p_data)
  5779. {
  5780. uint8 value = *p_data;
  5781. *p_data = *(p_data + 3);
  5782. *(p_data + 3) = value;
  5783. value = *(p_data + 1);
  5784. *(p_data + 1) = *(p_data + 2);
  5785. *(p_data + 2) = value;
  5786. }
  5787. static union {
  5788. int a;
  5789. char b;
  5790. } __ue = { .a = 1 };
  5791. #define is_little_endian() (__ue.b == 1)
  5792. static bool
  5793. load(const uint8 *buf, uint32 size, WASMModule *module,
  5794. bool wasm_binary_freeable, char *error_buf, uint32 error_buf_size)
  5795. {
  5796. const uint8 *buf_end = buf + size;
  5797. const uint8 *p = buf, *p_end = buf_end;
  5798. uint32 magic_number, version;
  5799. WASMSection *section_list = NULL;
  5800. CHECK_BUF1(p, p_end, sizeof(uint32));
  5801. magic_number = read_uint32(p);
  5802. if (!is_little_endian())
  5803. exchange32((uint8 *)&magic_number);
  5804. if (magic_number != WASM_MAGIC_NUMBER) {
  5805. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5806. return false;
  5807. }
  5808. CHECK_BUF1(p, p_end, sizeof(uint32));
  5809. version = read_uint32(p);
  5810. if (!is_little_endian())
  5811. exchange32((uint8 *)&version);
  5812. if (version != WASM_CURRENT_VERSION) {
  5813. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5814. return false;
  5815. }
  5816. module->package_version = version;
  5817. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5818. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  5819. error_buf, error_buf_size)) {
  5820. destroy_sections(section_list);
  5821. return false;
  5822. }
  5823. destroy_sections(section_list);
  5824. return true;
  5825. fail:
  5826. return false;
  5827. }
  5828. #if WASM_ENABLE_LIBC_WASI != 0
  5829. /**
  5830. * refer to
  5831. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5832. */
  5833. static bool
  5834. check_wasi_abi_compatibility(const WASMModule *module,
  5835. #if WASM_ENABLE_MULTI_MODULE != 0
  5836. bool main_module,
  5837. #endif
  5838. char *error_buf, uint32 error_buf_size)
  5839. {
  5840. /**
  5841. * be careful with:
  5842. * wasi compatible modules(command/reactor) which don't import any wasi
  5843. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5844. * reactor can depend on nothing. At the same time, each has its own entry
  5845. * point.
  5846. *
  5847. * observations:
  5848. * - clang always injects `_start` into a command
  5849. * - clang always injects `_initialize` into a reactor
  5850. * - `iwasm -f` allows to run a function in the reactor
  5851. *
  5852. * strong assumptions:
  5853. * - no one will define either `_start` or `_initialize` on purpose
  5854. * - `_start` should always be `void _start(void)`
  5855. * - `_initialize` should always be `void _initialize(void)`
  5856. *
  5857. */
  5858. /* clang-format off */
  5859. /**
  5860. *
  5861. * | | import_wasi_api True | | import_wasi_api False | |
  5862. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5863. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5864. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5865. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5866. */
  5867. /* clang-format on */
  5868. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5869. uint32 import_function_count = module->import_function_count;
  5870. WASMFuncType *func_type;
  5871. /* (func (export "_start") (...) */
  5872. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5873. error_buf, error_buf_size);
  5874. if (start) {
  5875. if (start->index < import_function_count) {
  5876. set_error_buf(
  5877. error_buf, error_buf_size,
  5878. "the builtin _start function can not be an import function");
  5879. return false;
  5880. }
  5881. func_type =
  5882. module->functions[start->index - import_function_count]->func_type;
  5883. if (func_type->param_count || func_type->result_count) {
  5884. set_error_buf(error_buf, error_buf_size,
  5885. "the signature of builtin _start function is wrong");
  5886. return false;
  5887. }
  5888. }
  5889. else {
  5890. /* (func (export "_initialize") (...) */
  5891. initialize =
  5892. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5893. error_buf, error_buf_size);
  5894. if (initialize) {
  5895. if (initialize->index < import_function_count) {
  5896. set_error_buf(error_buf, error_buf_size,
  5897. "the builtin _initialize function can not be an "
  5898. "import function");
  5899. return false;
  5900. }
  5901. func_type =
  5902. module->functions[initialize->index - import_function_count]
  5903. ->func_type;
  5904. if (func_type->param_count || func_type->result_count) {
  5905. set_error_buf(
  5906. error_buf, error_buf_size,
  5907. "the signature of builtin _initialize function is wrong");
  5908. return false;
  5909. }
  5910. }
  5911. }
  5912. /* filter out non-wasi compatible modules */
  5913. if (!module->import_wasi_api && !start && !initialize) {
  5914. return true;
  5915. }
  5916. /* should have one at least */
  5917. if (module->import_wasi_api && !start && !initialize) {
  5918. LOG_WARNING("warning: a module with WASI apis should be either "
  5919. "a command or a reactor");
  5920. }
  5921. /*
  5922. * there is at least one of `_start` and `_initialize` in below cases.
  5923. * according to the assumption, they should be all wasi compatible
  5924. */
  5925. #if WASM_ENABLE_MULTI_MODULE != 0
  5926. /* filter out commands (with `_start`) cases */
  5927. if (start && !main_module) {
  5928. set_error_buf(
  5929. error_buf, error_buf_size,
  5930. "a command (with _start function) can not be a sub-module");
  5931. return false;
  5932. }
  5933. #endif
  5934. /*
  5935. * it is ok a reactor acts as a main module,
  5936. * so skip the check about (with `_initialize`)
  5937. */
  5938. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5939. error_buf, error_buf_size);
  5940. if (!memory
  5941. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5942. /*
  5943. * with wasi-threads, it's still an open question if a memory
  5944. * should be exported.
  5945. *
  5946. * https://github.com/WebAssembly/wasi-threads/issues/22
  5947. * https://github.com/WebAssembly/WASI/issues/502
  5948. *
  5949. * Note: this code assumes the number of memories is at most 1.
  5950. */
  5951. && module->import_memory_count == 0
  5952. #endif
  5953. ) {
  5954. set_error_buf(error_buf, error_buf_size,
  5955. "a module with WASI apis must export memory by default");
  5956. return false;
  5957. }
  5958. return true;
  5959. }
  5960. #endif
  5961. WASMModule *
  5962. wasm_loader_load(uint8 *buf, uint32 size,
  5963. #if WASM_ENABLE_MULTI_MODULE != 0
  5964. bool main_module,
  5965. #endif
  5966. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  5967. {
  5968. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  5969. if (!module) {
  5970. return NULL;
  5971. }
  5972. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5973. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5974. module->load_addr = (uint8 *)buf;
  5975. module->load_size = size;
  5976. #endif
  5977. if (!load(buf, size, module, args->wasm_binary_freeable, error_buf,
  5978. error_buf_size)) {
  5979. goto fail;
  5980. }
  5981. #if WASM_ENABLE_LIBC_WASI != 0
  5982. /* Check the WASI application ABI */
  5983. if (!check_wasi_abi_compatibility(module,
  5984. #if WASM_ENABLE_MULTI_MODULE != 0
  5985. main_module,
  5986. #endif
  5987. error_buf, error_buf_size)) {
  5988. goto fail;
  5989. }
  5990. #endif
  5991. LOG_VERBOSE("Load module success.\n");
  5992. return module;
  5993. fail:
  5994. wasm_loader_unload(module);
  5995. return NULL;
  5996. }
  5997. void
  5998. wasm_loader_unload(WASMModule *module)
  5999. {
  6000. uint32 i;
  6001. if (!module)
  6002. return;
  6003. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6004. && WASM_ENABLE_LAZY_JIT != 0
  6005. module->orcjit_stop_compiling = true;
  6006. if (module->llvm_jit_init_thread)
  6007. os_thread_join(module->llvm_jit_init_thread, NULL);
  6008. #endif
  6009. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6010. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6011. module internal data after they were freed */
  6012. orcjit_stop_compile_threads(module);
  6013. #endif
  6014. #if WASM_ENABLE_JIT != 0
  6015. if (module->func_ptrs)
  6016. wasm_runtime_free(module->func_ptrs);
  6017. if (module->comp_ctx)
  6018. aot_destroy_comp_context(module->comp_ctx);
  6019. if (module->comp_data)
  6020. aot_destroy_comp_data(module->comp_data);
  6021. #endif
  6022. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6023. && WASM_ENABLE_LAZY_JIT != 0
  6024. if (module->tierup_wait_lock_inited) {
  6025. os_mutex_destroy(&module->tierup_wait_lock);
  6026. os_cond_destroy(&module->tierup_wait_cond);
  6027. }
  6028. #endif
  6029. if (module->imports)
  6030. wasm_runtime_free(module->imports);
  6031. if (module->functions) {
  6032. for (i = 0; i < module->function_count; i++) {
  6033. if (module->functions[i]) {
  6034. if (module->functions[i]->local_offsets)
  6035. wasm_runtime_free(module->functions[i]->local_offsets);
  6036. #if WASM_ENABLE_FAST_INTERP != 0
  6037. if (module->functions[i]->code_compiled)
  6038. wasm_runtime_free(module->functions[i]->code_compiled);
  6039. if (module->functions[i]->consts)
  6040. wasm_runtime_free(module->functions[i]->consts);
  6041. #endif
  6042. #if WASM_ENABLE_FAST_JIT != 0
  6043. if (module->functions[i]->fast_jit_jitted_code) {
  6044. jit_code_cache_free(
  6045. module->functions[i]->fast_jit_jitted_code);
  6046. }
  6047. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6048. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6049. jit_code_cache_free(
  6050. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6051. }
  6052. #endif
  6053. #endif
  6054. #if WASM_ENABLE_GC != 0
  6055. if (module->functions[i]->local_ref_type_maps) {
  6056. wasm_runtime_free(
  6057. module->functions[i]->local_ref_type_maps);
  6058. }
  6059. #endif
  6060. wasm_runtime_free(module->functions[i]);
  6061. }
  6062. }
  6063. wasm_runtime_free(module->functions);
  6064. }
  6065. if (module->tables) {
  6066. #if WASM_ENABLE_GC != 0
  6067. for (i = 0; i < module->table_count; i++) {
  6068. destroy_init_expr(module, &module->tables[i].init_expr);
  6069. }
  6070. #endif
  6071. wasm_runtime_free(module->tables);
  6072. }
  6073. if (module->memories)
  6074. wasm_runtime_free(module->memories);
  6075. if (module->globals) {
  6076. #if WASM_ENABLE_GC != 0
  6077. for (i = 0; i < module->global_count; i++) {
  6078. destroy_init_expr(module, &module->globals[i].init_expr);
  6079. }
  6080. #endif
  6081. wasm_runtime_free(module->globals);
  6082. }
  6083. #if WASM_ENABLE_TAGS != 0
  6084. if (module->tags) {
  6085. for (i = 0; i < module->tag_count; i++) {
  6086. if (module->tags[i])
  6087. wasm_runtime_free(module->tags[i]);
  6088. }
  6089. wasm_runtime_free(module->tags);
  6090. }
  6091. #endif
  6092. if (module->exports)
  6093. wasm_runtime_free(module->exports);
  6094. if (module->table_segments) {
  6095. for (i = 0; i < module->table_seg_count; i++) {
  6096. if (module->table_segments[i].init_values) {
  6097. #if WASM_ENABLE_GC != 0
  6098. uint32 j;
  6099. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6100. destroy_init_expr(
  6101. module, &module->table_segments[i].init_values[j]);
  6102. }
  6103. #endif
  6104. wasm_runtime_free(module->table_segments[i].init_values);
  6105. }
  6106. }
  6107. wasm_runtime_free(module->table_segments);
  6108. }
  6109. if (module->data_segments) {
  6110. for (i = 0; i < module->data_seg_count; i++) {
  6111. if (module->data_segments[i]) {
  6112. if (module->data_segments[i]->is_data_cloned)
  6113. wasm_runtime_free(module->data_segments[i]->data);
  6114. wasm_runtime_free(module->data_segments[i]);
  6115. }
  6116. }
  6117. wasm_runtime_free(module->data_segments);
  6118. }
  6119. if (module->types) {
  6120. for (i = 0; i < module->type_count; i++) {
  6121. if (module->types[i])
  6122. destroy_wasm_type(module->types[i]);
  6123. }
  6124. wasm_runtime_free(module->types);
  6125. }
  6126. if (module->const_str_list) {
  6127. StringNode *node = module->const_str_list, *node_next;
  6128. while (node) {
  6129. node_next = node->next;
  6130. wasm_runtime_free(node);
  6131. node = node_next;
  6132. }
  6133. }
  6134. #if WASM_ENABLE_STRINGREF != 0
  6135. if (module->string_literal_ptrs) {
  6136. wasm_runtime_free((void *)module->string_literal_ptrs);
  6137. }
  6138. if (module->string_literal_lengths) {
  6139. wasm_runtime_free(module->string_literal_lengths);
  6140. }
  6141. #endif
  6142. #if WASM_ENABLE_FAST_INTERP == 0
  6143. if (module->br_table_cache_list) {
  6144. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6145. BrTableCache *node_next;
  6146. while (node) {
  6147. node_next = bh_list_elem_next(node);
  6148. wasm_runtime_free(node);
  6149. node = node_next;
  6150. }
  6151. }
  6152. #endif
  6153. #if WASM_ENABLE_MULTI_MODULE != 0
  6154. /* just release the sub module list */
  6155. if (module->import_module_list) {
  6156. WASMRegisteredModule *node =
  6157. bh_list_first_elem(module->import_module_list);
  6158. while (node) {
  6159. WASMRegisteredModule *next = bh_list_elem_next(node);
  6160. bh_list_remove(module->import_module_list, node);
  6161. /*
  6162. * unload(sub_module) will be triggered during runtime_destroy().
  6163. * every module in the global module list will be unloaded one by
  6164. * one. so don't worry.
  6165. */
  6166. wasm_runtime_free(node);
  6167. /*
  6168. * the module file reading buffer will be released
  6169. * in runtime_destroy()
  6170. */
  6171. node = next;
  6172. }
  6173. }
  6174. #endif
  6175. #if WASM_ENABLE_DEBUG_INTERP != 0
  6176. WASMFastOPCodeNode *fast_opcode =
  6177. bh_list_first_elem(&module->fast_opcode_list);
  6178. while (fast_opcode) {
  6179. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6180. wasm_runtime_free(fast_opcode);
  6181. fast_opcode = next;
  6182. }
  6183. #endif
  6184. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6185. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6186. && WASM_ENABLE_LAZY_JIT != 0)
  6187. os_mutex_destroy(&module->instance_list_lock);
  6188. #endif
  6189. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6190. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6191. #endif
  6192. #if WASM_ENABLE_FAST_JIT != 0
  6193. if (module->fast_jit_func_ptrs) {
  6194. wasm_runtime_free(module->fast_jit_func_ptrs);
  6195. }
  6196. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6197. if (module->fast_jit_thread_locks_inited[i]) {
  6198. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6199. }
  6200. }
  6201. #endif
  6202. #if WASM_ENABLE_GC != 0
  6203. os_mutex_destroy(&module->rtt_type_lock);
  6204. bh_hash_map_destroy(module->ref_type_set);
  6205. if (module->rtt_types) {
  6206. for (i = 0; i < module->type_count; i++) {
  6207. if (module->rtt_types[i])
  6208. wasm_runtime_free(module->rtt_types[i]);
  6209. }
  6210. wasm_runtime_free(module->rtt_types);
  6211. }
  6212. #if WASM_ENABLE_STRINGREF != 0
  6213. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6214. if (module->stringref_rtts[i])
  6215. wasm_runtime_free(module->stringref_rtts[i]);
  6216. }
  6217. #endif
  6218. #endif
  6219. wasm_runtime_free(module);
  6220. }
  6221. bool
  6222. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6223. const uint8 *start_addr, const uint8 *code_end_addr,
  6224. uint8 label_type, uint8 **p_else_addr,
  6225. uint8 **p_end_addr)
  6226. {
  6227. const uint8 *p = start_addr, *p_end = code_end_addr;
  6228. uint8 *else_addr = NULL;
  6229. char error_buf[128];
  6230. uint32 block_nested_depth = 1, count, i, j, t;
  6231. uint32 error_buf_size = sizeof(error_buf);
  6232. uint8 opcode, u8;
  6233. BlockAddr block_stack[16] = { { 0 } }, *block;
  6234. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6235. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6236. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6237. if (block[j].start_addr == start_addr) {
  6238. /* Cache hit */
  6239. *p_else_addr = block[j].else_addr;
  6240. *p_end_addr = block[j].end_addr;
  6241. return true;
  6242. }
  6243. }
  6244. /* Cache unhit */
  6245. block_stack[0].start_addr = start_addr;
  6246. while (p < code_end_addr) {
  6247. opcode = *p++;
  6248. #if WASM_ENABLE_DEBUG_INTERP != 0
  6249. op_break_retry:
  6250. #endif
  6251. switch (opcode) {
  6252. case WASM_OP_UNREACHABLE:
  6253. case WASM_OP_NOP:
  6254. break;
  6255. #if WASM_ENABLE_EXCE_HANDLING != 0
  6256. case WASM_OP_TRY:
  6257. u8 = read_uint8(p);
  6258. if (block_nested_depth
  6259. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6260. block_stack[block_nested_depth].start_addr = p;
  6261. block_stack[block_nested_depth].else_addr = NULL;
  6262. }
  6263. block_nested_depth++;
  6264. break;
  6265. case EXT_OP_TRY:
  6266. skip_leb_uint32(p, p_end);
  6267. if (block_nested_depth
  6268. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6269. block_stack[block_nested_depth].start_addr = p;
  6270. block_stack[block_nested_depth].else_addr = NULL;
  6271. }
  6272. block_nested_depth++;
  6273. break;
  6274. case WASM_OP_CATCH:
  6275. if (block_nested_depth == 1) {
  6276. *p_end_addr = (uint8 *)(p - 1);
  6277. /* stop search and return the address of the catch block */
  6278. return true;
  6279. }
  6280. break;
  6281. case WASM_OP_CATCH_ALL:
  6282. if (block_nested_depth == 1) {
  6283. *p_end_addr = (uint8 *)(p - 1);
  6284. /* stop search and return the address of the catch_all block
  6285. */
  6286. return true;
  6287. }
  6288. break;
  6289. case WASM_OP_THROW:
  6290. /* skip tag_index */
  6291. skip_leb(p);
  6292. break;
  6293. case WASM_OP_RETHROW:
  6294. /* skip depth */
  6295. skip_leb(p);
  6296. break;
  6297. case WASM_OP_DELEGATE:
  6298. if (block_nested_depth == 1) {
  6299. *p_end_addr = (uint8 *)(p - 1);
  6300. return true;
  6301. }
  6302. else {
  6303. skip_leb(p);
  6304. /* the DELEGATE opcode ends the tryblock, */
  6305. block_nested_depth--;
  6306. if (block_nested_depth
  6307. < sizeof(block_stack) / sizeof(BlockAddr))
  6308. block_stack[block_nested_depth].end_addr =
  6309. (uint8 *)(p - 1);
  6310. }
  6311. break;
  6312. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6313. case WASM_OP_BLOCK:
  6314. case WASM_OP_LOOP:
  6315. case WASM_OP_IF:
  6316. {
  6317. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6318. u8 = read_uint8(p);
  6319. if (is_byte_a_type(u8)) {
  6320. #if WASM_ENABLE_GC != 0
  6321. if (wasm_is_type_multi_byte_type(u8)) {
  6322. /* the possible extra bytes of GC ref type have been
  6323. modified to OP_NOP, no need to resolve them again */
  6324. }
  6325. #endif
  6326. }
  6327. else {
  6328. p--;
  6329. /* block type */
  6330. skip_leb_int32(p, p_end);
  6331. }
  6332. if (block_nested_depth
  6333. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6334. block_stack[block_nested_depth].start_addr = p;
  6335. block_stack[block_nested_depth].else_addr = NULL;
  6336. }
  6337. block_nested_depth++;
  6338. break;
  6339. }
  6340. case EXT_OP_BLOCK:
  6341. case EXT_OP_LOOP:
  6342. case EXT_OP_IF:
  6343. /* block type */
  6344. skip_leb_int32(p, p_end);
  6345. if (block_nested_depth
  6346. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6347. block_stack[block_nested_depth].start_addr = p;
  6348. block_stack[block_nested_depth].else_addr = NULL;
  6349. }
  6350. block_nested_depth++;
  6351. break;
  6352. case WASM_OP_ELSE:
  6353. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6354. else_addr = (uint8 *)(p - 1);
  6355. if (block_nested_depth - 1
  6356. < sizeof(block_stack) / sizeof(BlockAddr))
  6357. block_stack[block_nested_depth - 1].else_addr =
  6358. (uint8 *)(p - 1);
  6359. break;
  6360. case WASM_OP_END:
  6361. if (block_nested_depth == 1) {
  6362. if (label_type == LABEL_TYPE_IF)
  6363. *p_else_addr = else_addr;
  6364. *p_end_addr = (uint8 *)(p - 1);
  6365. block_stack[0].end_addr = (uint8 *)(p - 1);
  6366. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6367. t++) {
  6368. start_addr = block_stack[t].start_addr;
  6369. if (start_addr) {
  6370. i = ((uintptr_t)start_addr)
  6371. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6372. block =
  6373. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6374. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6375. if (!block[j].start_addr)
  6376. break;
  6377. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6378. memmove(block + 1, block,
  6379. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6380. * sizeof(BlockAddr));
  6381. j = 0;
  6382. }
  6383. block[j].start_addr = block_stack[t].start_addr;
  6384. block[j].else_addr = block_stack[t].else_addr;
  6385. block[j].end_addr = block_stack[t].end_addr;
  6386. }
  6387. else
  6388. break;
  6389. }
  6390. return true;
  6391. }
  6392. else {
  6393. block_nested_depth--;
  6394. if (block_nested_depth
  6395. < sizeof(block_stack) / sizeof(BlockAddr))
  6396. block_stack[block_nested_depth].end_addr =
  6397. (uint8 *)(p - 1);
  6398. }
  6399. break;
  6400. case WASM_OP_BR:
  6401. case WASM_OP_BR_IF:
  6402. skip_leb_uint32(p, p_end); /* labelidx */
  6403. break;
  6404. case WASM_OP_BR_TABLE:
  6405. read_leb_uint32(p, p_end, count); /* label num */
  6406. #if WASM_ENABLE_FAST_INTERP != 0
  6407. for (i = 0; i <= count; i++) /* labelidxs */
  6408. skip_leb_uint32(p, p_end);
  6409. #else
  6410. p += count + 1;
  6411. while (*p == WASM_OP_NOP)
  6412. p++;
  6413. #endif
  6414. break;
  6415. #if WASM_ENABLE_FAST_INTERP == 0
  6416. case EXT_OP_BR_TABLE_CACHE:
  6417. read_leb_uint32(p, p_end, count); /* label num */
  6418. while (*p == WASM_OP_NOP)
  6419. p++;
  6420. break;
  6421. #endif
  6422. case WASM_OP_RETURN:
  6423. break;
  6424. case WASM_OP_CALL:
  6425. #if WASM_ENABLE_TAIL_CALL != 0
  6426. case WASM_OP_RETURN_CALL:
  6427. #endif
  6428. skip_leb_uint32(p, p_end); /* funcidx */
  6429. break;
  6430. case WASM_OP_CALL_INDIRECT:
  6431. #if WASM_ENABLE_TAIL_CALL != 0
  6432. case WASM_OP_RETURN_CALL_INDIRECT:
  6433. #endif
  6434. skip_leb_uint32(p, p_end); /* typeidx */
  6435. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6436. skip_leb_uint32(p, p_end); /* tableidx */
  6437. #else
  6438. u8 = read_uint8(p); /* 0x00 */
  6439. #endif
  6440. break;
  6441. #if WASM_ENABLE_GC != 0
  6442. case WASM_OP_CALL_REF:
  6443. case WASM_OP_RETURN_CALL_REF:
  6444. skip_leb_uint32(p, p_end); /* typeidx */
  6445. break;
  6446. #endif
  6447. case WASM_OP_DROP:
  6448. case WASM_OP_SELECT:
  6449. case WASM_OP_DROP_64:
  6450. case WASM_OP_SELECT_64:
  6451. break;
  6452. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6453. case WASM_OP_SELECT_T:
  6454. {
  6455. skip_leb_uint32(p, p_end); /* vec length */
  6456. u8 = read_uint8(p); /* typeidx */
  6457. /* the possible extra bytes of GC ref type have been
  6458. modified to OP_NOP, no need to resolve them again */
  6459. break;
  6460. }
  6461. case WASM_OP_TABLE_GET:
  6462. case WASM_OP_TABLE_SET:
  6463. skip_leb_uint32(p, p_end); /* table index */
  6464. break;
  6465. case WASM_OP_REF_NULL:
  6466. {
  6467. u8 = read_uint8(p); /* type */
  6468. if (is_byte_a_type(u8)) {
  6469. #if WASM_ENABLE_GC != 0
  6470. if (wasm_is_type_multi_byte_type(u8)) {
  6471. /* the possible extra bytes of GC ref type have been
  6472. modified to OP_NOP, no need to resolve them again */
  6473. }
  6474. #endif
  6475. }
  6476. else {
  6477. p--;
  6478. skip_leb_uint32(p, p_end);
  6479. }
  6480. break;
  6481. }
  6482. case WASM_OP_REF_IS_NULL:
  6483. break;
  6484. case WASM_OP_REF_FUNC:
  6485. skip_leb_uint32(p, p_end); /* func index */
  6486. break;
  6487. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6488. #if WASM_ENABLE_GC != 0
  6489. case WASM_OP_REF_AS_NON_NULL:
  6490. case WASM_OP_REF_EQ:
  6491. break;
  6492. case WASM_OP_BR_ON_NULL:
  6493. case WASM_OP_BR_ON_NON_NULL:
  6494. skip_leb_uint32(p, p_end); /* label index */
  6495. break;
  6496. #endif /* end of WASM_ENABLE_GC != 0 */
  6497. case WASM_OP_GET_LOCAL:
  6498. case WASM_OP_SET_LOCAL:
  6499. case WASM_OP_TEE_LOCAL:
  6500. case WASM_OP_GET_GLOBAL:
  6501. case WASM_OP_SET_GLOBAL:
  6502. case WASM_OP_GET_GLOBAL_64:
  6503. case WASM_OP_SET_GLOBAL_64:
  6504. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6505. skip_leb_uint32(p, p_end); /* local index */
  6506. break;
  6507. case EXT_OP_GET_LOCAL_FAST:
  6508. case EXT_OP_SET_LOCAL_FAST:
  6509. case EXT_OP_TEE_LOCAL_FAST:
  6510. CHECK_BUF(p, p_end, 1);
  6511. p++;
  6512. break;
  6513. case WASM_OP_I32_LOAD:
  6514. case WASM_OP_I64_LOAD:
  6515. case WASM_OP_F32_LOAD:
  6516. case WASM_OP_F64_LOAD:
  6517. case WASM_OP_I32_LOAD8_S:
  6518. case WASM_OP_I32_LOAD8_U:
  6519. case WASM_OP_I32_LOAD16_S:
  6520. case WASM_OP_I32_LOAD16_U:
  6521. case WASM_OP_I64_LOAD8_S:
  6522. case WASM_OP_I64_LOAD8_U:
  6523. case WASM_OP_I64_LOAD16_S:
  6524. case WASM_OP_I64_LOAD16_U:
  6525. case WASM_OP_I64_LOAD32_S:
  6526. case WASM_OP_I64_LOAD32_U:
  6527. case WASM_OP_I32_STORE:
  6528. case WASM_OP_I64_STORE:
  6529. case WASM_OP_F32_STORE:
  6530. case WASM_OP_F64_STORE:
  6531. case WASM_OP_I32_STORE8:
  6532. case WASM_OP_I32_STORE16:
  6533. case WASM_OP_I64_STORE8:
  6534. case WASM_OP_I64_STORE16:
  6535. case WASM_OP_I64_STORE32:
  6536. skip_leb_align(p, p_end); /* align */
  6537. skip_leb_mem_offset(p, p_end); /* offset */
  6538. break;
  6539. case WASM_OP_MEMORY_SIZE:
  6540. case WASM_OP_MEMORY_GROW:
  6541. skip_leb_memidx(p, p_end); /* memidx */
  6542. break;
  6543. case WASM_OP_I32_CONST:
  6544. skip_leb_int32(p, p_end);
  6545. break;
  6546. case WASM_OP_I64_CONST:
  6547. skip_leb_int64(p, p_end);
  6548. break;
  6549. case WASM_OP_F32_CONST:
  6550. p += sizeof(float32);
  6551. break;
  6552. case WASM_OP_F64_CONST:
  6553. p += sizeof(float64);
  6554. break;
  6555. case WASM_OP_I32_EQZ:
  6556. case WASM_OP_I32_EQ:
  6557. case WASM_OP_I32_NE:
  6558. case WASM_OP_I32_LT_S:
  6559. case WASM_OP_I32_LT_U:
  6560. case WASM_OP_I32_GT_S:
  6561. case WASM_OP_I32_GT_U:
  6562. case WASM_OP_I32_LE_S:
  6563. case WASM_OP_I32_LE_U:
  6564. case WASM_OP_I32_GE_S:
  6565. case WASM_OP_I32_GE_U:
  6566. case WASM_OP_I64_EQZ:
  6567. case WASM_OP_I64_EQ:
  6568. case WASM_OP_I64_NE:
  6569. case WASM_OP_I64_LT_S:
  6570. case WASM_OP_I64_LT_U:
  6571. case WASM_OP_I64_GT_S:
  6572. case WASM_OP_I64_GT_U:
  6573. case WASM_OP_I64_LE_S:
  6574. case WASM_OP_I64_LE_U:
  6575. case WASM_OP_I64_GE_S:
  6576. case WASM_OP_I64_GE_U:
  6577. case WASM_OP_F32_EQ:
  6578. case WASM_OP_F32_NE:
  6579. case WASM_OP_F32_LT:
  6580. case WASM_OP_F32_GT:
  6581. case WASM_OP_F32_LE:
  6582. case WASM_OP_F32_GE:
  6583. case WASM_OP_F64_EQ:
  6584. case WASM_OP_F64_NE:
  6585. case WASM_OP_F64_LT:
  6586. case WASM_OP_F64_GT:
  6587. case WASM_OP_F64_LE:
  6588. case WASM_OP_F64_GE:
  6589. case WASM_OP_I32_CLZ:
  6590. case WASM_OP_I32_CTZ:
  6591. case WASM_OP_I32_POPCNT:
  6592. case WASM_OP_I32_ADD:
  6593. case WASM_OP_I32_SUB:
  6594. case WASM_OP_I32_MUL:
  6595. case WASM_OP_I32_DIV_S:
  6596. case WASM_OP_I32_DIV_U:
  6597. case WASM_OP_I32_REM_S:
  6598. case WASM_OP_I32_REM_U:
  6599. case WASM_OP_I32_AND:
  6600. case WASM_OP_I32_OR:
  6601. case WASM_OP_I32_XOR:
  6602. case WASM_OP_I32_SHL:
  6603. case WASM_OP_I32_SHR_S:
  6604. case WASM_OP_I32_SHR_U:
  6605. case WASM_OP_I32_ROTL:
  6606. case WASM_OP_I32_ROTR:
  6607. case WASM_OP_I64_CLZ:
  6608. case WASM_OP_I64_CTZ:
  6609. case WASM_OP_I64_POPCNT:
  6610. case WASM_OP_I64_ADD:
  6611. case WASM_OP_I64_SUB:
  6612. case WASM_OP_I64_MUL:
  6613. case WASM_OP_I64_DIV_S:
  6614. case WASM_OP_I64_DIV_U:
  6615. case WASM_OP_I64_REM_S:
  6616. case WASM_OP_I64_REM_U:
  6617. case WASM_OP_I64_AND:
  6618. case WASM_OP_I64_OR:
  6619. case WASM_OP_I64_XOR:
  6620. case WASM_OP_I64_SHL:
  6621. case WASM_OP_I64_SHR_S:
  6622. case WASM_OP_I64_SHR_U:
  6623. case WASM_OP_I64_ROTL:
  6624. case WASM_OP_I64_ROTR:
  6625. case WASM_OP_F32_ABS:
  6626. case WASM_OP_F32_NEG:
  6627. case WASM_OP_F32_CEIL:
  6628. case WASM_OP_F32_FLOOR:
  6629. case WASM_OP_F32_TRUNC:
  6630. case WASM_OP_F32_NEAREST:
  6631. case WASM_OP_F32_SQRT:
  6632. case WASM_OP_F32_ADD:
  6633. case WASM_OP_F32_SUB:
  6634. case WASM_OP_F32_MUL:
  6635. case WASM_OP_F32_DIV:
  6636. case WASM_OP_F32_MIN:
  6637. case WASM_OP_F32_MAX:
  6638. case WASM_OP_F32_COPYSIGN:
  6639. case WASM_OP_F64_ABS:
  6640. case WASM_OP_F64_NEG:
  6641. case WASM_OP_F64_CEIL:
  6642. case WASM_OP_F64_FLOOR:
  6643. case WASM_OP_F64_TRUNC:
  6644. case WASM_OP_F64_NEAREST:
  6645. case WASM_OP_F64_SQRT:
  6646. case WASM_OP_F64_ADD:
  6647. case WASM_OP_F64_SUB:
  6648. case WASM_OP_F64_MUL:
  6649. case WASM_OP_F64_DIV:
  6650. case WASM_OP_F64_MIN:
  6651. case WASM_OP_F64_MAX:
  6652. case WASM_OP_F64_COPYSIGN:
  6653. case WASM_OP_I32_WRAP_I64:
  6654. case WASM_OP_I32_TRUNC_S_F32:
  6655. case WASM_OP_I32_TRUNC_U_F32:
  6656. case WASM_OP_I32_TRUNC_S_F64:
  6657. case WASM_OP_I32_TRUNC_U_F64:
  6658. case WASM_OP_I64_EXTEND_S_I32:
  6659. case WASM_OP_I64_EXTEND_U_I32:
  6660. case WASM_OP_I64_TRUNC_S_F32:
  6661. case WASM_OP_I64_TRUNC_U_F32:
  6662. case WASM_OP_I64_TRUNC_S_F64:
  6663. case WASM_OP_I64_TRUNC_U_F64:
  6664. case WASM_OP_F32_CONVERT_S_I32:
  6665. case WASM_OP_F32_CONVERT_U_I32:
  6666. case WASM_OP_F32_CONVERT_S_I64:
  6667. case WASM_OP_F32_CONVERT_U_I64:
  6668. case WASM_OP_F32_DEMOTE_F64:
  6669. case WASM_OP_F64_CONVERT_S_I32:
  6670. case WASM_OP_F64_CONVERT_U_I32:
  6671. case WASM_OP_F64_CONVERT_S_I64:
  6672. case WASM_OP_F64_CONVERT_U_I64:
  6673. case WASM_OP_F64_PROMOTE_F32:
  6674. case WASM_OP_I32_REINTERPRET_F32:
  6675. case WASM_OP_I64_REINTERPRET_F64:
  6676. case WASM_OP_F32_REINTERPRET_I32:
  6677. case WASM_OP_F64_REINTERPRET_I64:
  6678. case WASM_OP_I32_EXTEND8_S:
  6679. case WASM_OP_I32_EXTEND16_S:
  6680. case WASM_OP_I64_EXTEND8_S:
  6681. case WASM_OP_I64_EXTEND16_S:
  6682. case WASM_OP_I64_EXTEND32_S:
  6683. break;
  6684. #if WASM_ENABLE_GC != 0
  6685. case WASM_OP_GC_PREFIX:
  6686. {
  6687. uint32 opcode1;
  6688. read_leb_uint32(p, p_end, opcode1);
  6689. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6690. is no larger than UINT8_MAX */
  6691. opcode = (uint8)opcode1;
  6692. switch (opcode) {
  6693. case WASM_OP_STRUCT_NEW:
  6694. case WASM_OP_STRUCT_NEW_DEFAULT:
  6695. skip_leb_uint32(p, p_end); /* typeidx */
  6696. break;
  6697. case WASM_OP_STRUCT_GET:
  6698. case WASM_OP_STRUCT_GET_S:
  6699. case WASM_OP_STRUCT_GET_U:
  6700. case WASM_OP_STRUCT_SET:
  6701. skip_leb_uint32(p, p_end); /* typeidx */
  6702. skip_leb_uint32(p, p_end); /* fieldidx */
  6703. break;
  6704. case WASM_OP_ARRAY_NEW:
  6705. case WASM_OP_ARRAY_NEW_DEFAULT:
  6706. case WASM_OP_ARRAY_GET:
  6707. case WASM_OP_ARRAY_GET_S:
  6708. case WASM_OP_ARRAY_GET_U:
  6709. case WASM_OP_ARRAY_SET:
  6710. case WASM_OP_ARRAY_FILL:
  6711. skip_leb_uint32(p, p_end); /* typeidx */
  6712. break;
  6713. case WASM_OP_ARRAY_COPY:
  6714. skip_leb_uint32(p, p_end); /* typeidx1 */
  6715. skip_leb_uint32(p, p_end); /* typeidx2 */
  6716. break;
  6717. case WASM_OP_ARRAY_LEN:
  6718. break;
  6719. case WASM_OP_ARRAY_NEW_FIXED:
  6720. case WASM_OP_ARRAY_NEW_DATA:
  6721. case WASM_OP_ARRAY_NEW_ELEM:
  6722. skip_leb_uint32(p, p_end); /* typeidx */
  6723. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6724. break;
  6725. case WASM_OP_REF_I31:
  6726. case WASM_OP_I31_GET_S:
  6727. case WASM_OP_I31_GET_U:
  6728. break;
  6729. case WASM_OP_REF_TEST:
  6730. case WASM_OP_REF_CAST:
  6731. case WASM_OP_REF_TEST_NULLABLE:
  6732. case WASM_OP_REF_CAST_NULLABLE:
  6733. skip_leb_int32(p, p_end); /* heaptype */
  6734. break;
  6735. case WASM_OP_BR_ON_CAST:
  6736. case WASM_OP_BR_ON_CAST_FAIL:
  6737. p += sizeof(uint8); /* castflag */
  6738. skip_leb_uint32(p, p_end); /* labelidx */
  6739. skip_leb_int32(p, p_end); /* heaptype */
  6740. skip_leb_int32(p, p_end); /* heaptype2 */
  6741. break;
  6742. case WASM_OP_ANY_CONVERT_EXTERN:
  6743. case WASM_OP_EXTERN_CONVERT_ANY:
  6744. break;
  6745. #if WASM_ENABLE_STRINGREF != 0
  6746. case WASM_OP_STRING_NEW_UTF8:
  6747. case WASM_OP_STRING_NEW_WTF16:
  6748. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6749. case WASM_OP_STRING_NEW_WTF8:
  6750. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6751. break;
  6752. case WASM_OP_STRING_CONST:
  6753. skip_leb_int32(p, p_end); /* contents */
  6754. break;
  6755. case WASM_OP_STRING_MEASURE_UTF8:
  6756. case WASM_OP_STRING_MEASURE_WTF8:
  6757. case WASM_OP_STRING_MEASURE_WTF16:
  6758. break;
  6759. case WASM_OP_STRING_ENCODE_UTF8:
  6760. case WASM_OP_STRING_ENCODE_WTF16:
  6761. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6762. case WASM_OP_STRING_ENCODE_WTF8:
  6763. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6764. break;
  6765. case WASM_OP_STRING_CONCAT:
  6766. case WASM_OP_STRING_EQ:
  6767. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6768. case WASM_OP_STRING_AS_WTF8:
  6769. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6770. break;
  6771. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6772. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6773. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6774. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6775. break;
  6776. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6777. case WASM_OP_STRING_AS_WTF16:
  6778. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6779. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6780. break;
  6781. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6782. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6783. break;
  6784. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6785. case WASM_OP_STRING_AS_ITER:
  6786. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6787. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6788. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6789. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6790. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6791. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6792. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6793. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6794. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6795. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6796. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6797. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6798. break;
  6799. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6800. default:
  6801. return false;
  6802. }
  6803. break;
  6804. }
  6805. #endif /* end of WASM_ENABLE_GC != 0 */
  6806. case WASM_OP_MISC_PREFIX:
  6807. {
  6808. uint32 opcode1;
  6809. read_leb_uint32(p, p_end, opcode1);
  6810. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6811. is no larger than UINT8_MAX */
  6812. opcode = (uint8)opcode1;
  6813. switch (opcode) {
  6814. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6815. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6816. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6817. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6818. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6819. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6820. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6821. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6822. break;
  6823. #if WASM_ENABLE_BULK_MEMORY != 0
  6824. case WASM_OP_MEMORY_INIT:
  6825. skip_leb_uint32(p, p_end);
  6826. skip_leb_memidx(p, p_end);
  6827. break;
  6828. case WASM_OP_DATA_DROP:
  6829. skip_leb_uint32(p, p_end);
  6830. break;
  6831. case WASM_OP_MEMORY_COPY:
  6832. skip_leb_memidx(p, p_end);
  6833. skip_leb_memidx(p, p_end);
  6834. break;
  6835. case WASM_OP_MEMORY_FILL:
  6836. skip_leb_memidx(p, p_end);
  6837. break;
  6838. #endif /* WASM_ENABLE_BULK_MEMORY */
  6839. #if WASM_ENABLE_REF_TYPES != 0
  6840. case WASM_OP_TABLE_INIT:
  6841. case WASM_OP_TABLE_COPY:
  6842. /* tableidx */
  6843. skip_leb_uint32(p, p_end);
  6844. /* elemidx */
  6845. skip_leb_uint32(p, p_end);
  6846. break;
  6847. case WASM_OP_ELEM_DROP:
  6848. /* elemidx */
  6849. skip_leb_uint32(p, p_end);
  6850. break;
  6851. case WASM_OP_TABLE_SIZE:
  6852. case WASM_OP_TABLE_GROW:
  6853. case WASM_OP_TABLE_FILL:
  6854. skip_leb_uint32(p, p_end); /* table idx */
  6855. break;
  6856. #endif /* WASM_ENABLE_REF_TYPES */
  6857. default:
  6858. return false;
  6859. }
  6860. break;
  6861. }
  6862. #if WASM_ENABLE_SIMD != 0
  6863. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6864. case WASM_OP_SIMD_PREFIX:
  6865. {
  6866. uint32 opcode1;
  6867. read_leb_uint32(p, p_end, opcode1);
  6868. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6869. is no larger than UINT8_MAX */
  6870. opcode = (uint8)opcode1;
  6871. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6872. */
  6873. switch (opcode) {
  6874. case SIMD_v128_load:
  6875. case SIMD_v128_load8x8_s:
  6876. case SIMD_v128_load8x8_u:
  6877. case SIMD_v128_load16x4_s:
  6878. case SIMD_v128_load16x4_u:
  6879. case SIMD_v128_load32x2_s:
  6880. case SIMD_v128_load32x2_u:
  6881. case SIMD_v128_load8_splat:
  6882. case SIMD_v128_load16_splat:
  6883. case SIMD_v128_load32_splat:
  6884. case SIMD_v128_load64_splat:
  6885. case SIMD_v128_store:
  6886. /* memarg align */
  6887. skip_leb_uint32(p, p_end);
  6888. /* memarg offset */
  6889. skip_leb_mem_offset(p, p_end);
  6890. break;
  6891. case SIMD_v128_const:
  6892. case SIMD_v8x16_shuffle:
  6893. /* immByte[16] immLaneId[16] */
  6894. CHECK_BUF1(p, p_end, 16);
  6895. p += 16;
  6896. break;
  6897. case SIMD_i8x16_extract_lane_s:
  6898. case SIMD_i8x16_extract_lane_u:
  6899. case SIMD_i8x16_replace_lane:
  6900. case SIMD_i16x8_extract_lane_s:
  6901. case SIMD_i16x8_extract_lane_u:
  6902. case SIMD_i16x8_replace_lane:
  6903. case SIMD_i32x4_extract_lane:
  6904. case SIMD_i32x4_replace_lane:
  6905. case SIMD_i64x2_extract_lane:
  6906. case SIMD_i64x2_replace_lane:
  6907. case SIMD_f32x4_extract_lane:
  6908. case SIMD_f32x4_replace_lane:
  6909. case SIMD_f64x2_extract_lane:
  6910. case SIMD_f64x2_replace_lane:
  6911. /* ImmLaneId */
  6912. CHECK_BUF(p, p_end, 1);
  6913. p++;
  6914. break;
  6915. case SIMD_v128_load8_lane:
  6916. case SIMD_v128_load16_lane:
  6917. case SIMD_v128_load32_lane:
  6918. case SIMD_v128_load64_lane:
  6919. case SIMD_v128_store8_lane:
  6920. case SIMD_v128_store16_lane:
  6921. case SIMD_v128_store32_lane:
  6922. case SIMD_v128_store64_lane:
  6923. /* memarg align */
  6924. skip_leb_uint32(p, p_end);
  6925. /* memarg offset */
  6926. skip_leb_mem_offset(p, p_end);
  6927. /* ImmLaneId */
  6928. CHECK_BUF(p, p_end, 1);
  6929. p++;
  6930. break;
  6931. case SIMD_v128_load32_zero:
  6932. case SIMD_v128_load64_zero:
  6933. /* memarg align */
  6934. skip_leb_uint32(p, p_end);
  6935. /* memarg offset */
  6936. skip_leb_mem_offset(p, p_end);
  6937. break;
  6938. default:
  6939. /*
  6940. * since latest SIMD specific used almost every value
  6941. * from 0x00 to 0xff, the default branch will present
  6942. * all opcodes without imm
  6943. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6944. */
  6945. break;
  6946. }
  6947. break;
  6948. }
  6949. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6950. #endif /* end of WASM_ENABLE_SIMD */
  6951. #if WASM_ENABLE_SHARED_MEMORY != 0
  6952. case WASM_OP_ATOMIC_PREFIX:
  6953. {
  6954. uint32 opcode1;
  6955. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6956. read_leb_uint32(p, p_end, opcode1);
  6957. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6958. is no larger than UINT8_MAX */
  6959. opcode = (uint8)opcode1;
  6960. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6961. skip_leb_uint32(p, p_end); /* align */
  6962. skip_leb_mem_offset(p, p_end); /* offset */
  6963. }
  6964. else {
  6965. /* atomic.fence doesn't have memarg */
  6966. p++;
  6967. }
  6968. break;
  6969. }
  6970. #endif
  6971. #if WASM_ENABLE_DEBUG_INTERP != 0
  6972. case DEBUG_OP_BREAK:
  6973. {
  6974. WASMDebugInstance *debug_instance =
  6975. wasm_exec_env_get_instance(exec_env);
  6976. char original_opcode[1];
  6977. uint64 size = 1;
  6978. WASMModuleInstance *module_inst =
  6979. (WASMModuleInstance *)exec_env->module_inst;
  6980. uint64 offset = (p - 1) >= module_inst->module->load_addr
  6981. ? (p - 1) - module_inst->module->load_addr
  6982. : ~0;
  6983. if (debug_instance) {
  6984. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  6985. original_opcode, &size)
  6986. && size == 1) {
  6987. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  6988. "with %02x: ",
  6989. original_opcode[0]);
  6990. opcode = original_opcode[0];
  6991. goto op_break_retry;
  6992. }
  6993. }
  6994. break;
  6995. }
  6996. #endif
  6997. default:
  6998. return false;
  6999. }
  7000. }
  7001. (void)u8;
  7002. (void)exec_env;
  7003. return false;
  7004. fail:
  7005. return false;
  7006. }
  7007. #if WASM_ENABLE_FAST_INTERP != 0
  7008. #if WASM_DEBUG_PREPROCESSOR != 0
  7009. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7010. #else
  7011. #define LOG_OP(...) (void)0
  7012. #endif
  7013. #define PATCH_ELSE 0
  7014. #define PATCH_END 1
  7015. typedef struct BranchBlockPatch {
  7016. struct BranchBlockPatch *next;
  7017. uint8 patch_type;
  7018. uint8 *code_compiled;
  7019. } BranchBlockPatch;
  7020. #endif
  7021. typedef struct BranchBlock {
  7022. uint8 label_type;
  7023. BlockType block_type;
  7024. uint8 *start_addr;
  7025. uint8 *else_addr;
  7026. uint8 *end_addr;
  7027. uint32 stack_cell_num;
  7028. #if WASM_ENABLE_GC != 0
  7029. uint32 reftype_map_num;
  7030. /* Indicate which local is used inside current block, used to validate
  7031. * local.get with non-nullable ref types */
  7032. uint8 *local_use_mask;
  7033. uint32 local_use_mask_size;
  7034. #endif
  7035. #if WASM_ENABLE_FAST_INTERP != 0
  7036. uint16 dynamic_offset;
  7037. uint8 *code_compiled;
  7038. BranchBlockPatch *patch_list;
  7039. /* This is used to save params frame_offset of of if block */
  7040. int16 *param_frame_offsets;
  7041. /* This is used to recover the dynamic offset for else branch,
  7042. * and also to remember the start offset of dynamic space which
  7043. * stores the block arguments for loop block, so we can use it
  7044. * to copy the stack operands to the loop block's arguments in
  7045. * wasm_loader_emit_br_info for opcode br. */
  7046. uint16 start_dynamic_offset;
  7047. #endif
  7048. /* Indicate the operand stack is in polymorphic state.
  7049. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7050. * to polymorphic state until the block's 'end' opcode is processed.
  7051. * If stack is in polymorphic state and stack is empty, instruction can
  7052. * pop any type of value directly without decreasing stack top pointer
  7053. * and stack cell num. */
  7054. bool is_stack_polymorphic;
  7055. } BranchBlock;
  7056. typedef struct WASMLoaderContext {
  7057. /* frame ref stack */
  7058. uint8 *frame_ref;
  7059. uint8 *frame_ref_bottom;
  7060. uint8 *frame_ref_boundary;
  7061. uint32 frame_ref_size;
  7062. uint32 stack_cell_num;
  7063. uint32 max_stack_cell_num;
  7064. #if WASM_ENABLE_GC != 0
  7065. /* frame reftype map stack */
  7066. WASMRefTypeMap *frame_reftype_map;
  7067. WASMRefTypeMap *frame_reftype_map_bottom;
  7068. WASMRefTypeMap *frame_reftype_map_boundary;
  7069. uint32 frame_reftype_map_size;
  7070. uint32 reftype_map_num;
  7071. uint32 max_reftype_map_num;
  7072. /* Current module */
  7073. WASMModule *module;
  7074. /* Current module's ref_type_set */
  7075. HashMap *ref_type_set;
  7076. /* Always point to local variable ref_type of
  7077. wasm_loader_prepare_bytecode */
  7078. WASMRefType *ref_type_tmp;
  7079. #endif
  7080. /* frame csp stack */
  7081. BranchBlock *frame_csp;
  7082. BranchBlock *frame_csp_bottom;
  7083. BranchBlock *frame_csp_boundary;
  7084. uint32 frame_csp_size;
  7085. uint32 csp_num;
  7086. uint32 max_csp_num;
  7087. #if WASM_ENABLE_FAST_INTERP != 0
  7088. /* frame offset stack */
  7089. int16 *frame_offset;
  7090. int16 *frame_offset_bottom;
  7091. int16 *frame_offset_boundary;
  7092. uint32 frame_offset_size;
  7093. int16 dynamic_offset;
  7094. int16 start_dynamic_offset;
  7095. int16 max_dynamic_offset;
  7096. /* preserved local offset */
  7097. int16 preserved_local_offset;
  7098. /* const buffer */
  7099. uint8 *const_buf;
  7100. uint16 num_const;
  7101. uint16 const_cell_num;
  7102. uint32 const_buf_size;
  7103. /* processed code */
  7104. uint8 *p_code_compiled;
  7105. uint8 *p_code_compiled_end;
  7106. uint32 code_compiled_size;
  7107. /* If the last opcode will be dropped, the peak memory usage will be larger
  7108. * than the final code_compiled_size, we record the peak size to ensure
  7109. * there will not be invalid memory access during second traverse */
  7110. uint32 code_compiled_peak_size;
  7111. #endif
  7112. } WASMLoaderContext;
  7113. typedef struct Const {
  7114. WASMValue value;
  7115. uint16 slot_index;
  7116. uint8 value_type;
  7117. } Const;
  7118. #define CHECK_CSP_PUSH() \
  7119. do { \
  7120. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7121. MEM_REALLOC( \
  7122. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7123. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7124. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7125. ctx->frame_csp_boundary = \
  7126. ctx->frame_csp_bottom \
  7127. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7128. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7129. } \
  7130. } while (0)
  7131. #define CHECK_CSP_POP() \
  7132. do { \
  7133. if (ctx->csp_num < 1) { \
  7134. set_error_buf(error_buf, error_buf_size, \
  7135. "type mismatch: " \
  7136. "expect data but block stack was empty"); \
  7137. goto fail; \
  7138. } \
  7139. } while (0)
  7140. #if WASM_ENABLE_FAST_INTERP != 0
  7141. static bool
  7142. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7143. uint32 error_buf_size)
  7144. {
  7145. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7146. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7147. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7148. ctx->frame_offset_size + 16);
  7149. ctx->frame_offset_size += 16;
  7150. ctx->frame_offset_boundary =
  7151. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7152. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7153. }
  7154. return true;
  7155. fail:
  7156. return false;
  7157. }
  7158. static bool
  7159. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7160. {
  7161. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7162. return false;
  7163. return true;
  7164. }
  7165. static void
  7166. free_label_patch_list(BranchBlock *frame_csp)
  7167. {
  7168. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7169. BranchBlockPatch *next;
  7170. while (label_patch != NULL) {
  7171. next = label_patch->next;
  7172. wasm_runtime_free(label_patch);
  7173. label_patch = next;
  7174. }
  7175. frame_csp->patch_list = NULL;
  7176. }
  7177. static void
  7178. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7179. {
  7180. BranchBlock *tmp_csp = frame_csp;
  7181. uint32 i;
  7182. for (i = 0; i < csp_num; i++) {
  7183. free_label_patch_list(tmp_csp);
  7184. tmp_csp++;
  7185. }
  7186. }
  7187. static void
  7188. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7189. {
  7190. BranchBlock *tmp_csp = frame_csp;
  7191. uint32 i;
  7192. for (i = 0; i < csp_num; i++) {
  7193. if (tmp_csp->param_frame_offsets)
  7194. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7195. tmp_csp++;
  7196. }
  7197. }
  7198. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7199. #if WASM_ENABLE_GC != 0
  7200. static bool
  7201. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7202. char *error_buf, uint32 error_buf_size)
  7203. {
  7204. BranchBlock *current_csp = ctx->frame_csp - 1;
  7205. uint32 local_mask_size;
  7206. if (local_count == 0) {
  7207. current_csp->local_use_mask_size = 0;
  7208. return true;
  7209. }
  7210. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7211. * else branch, we don't need to allocate memory again */
  7212. if (!current_csp->local_use_mask) {
  7213. local_mask_size = (local_count + 7) / sizeof(uint8);
  7214. if (!(current_csp->local_use_mask =
  7215. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7216. return false;
  7217. }
  7218. current_csp->local_use_mask_size = local_mask_size;
  7219. }
  7220. else {
  7221. local_mask_size = current_csp->local_use_mask_size;
  7222. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7223. }
  7224. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7225. /* For non-function blocks, inherit the use status from parent block */
  7226. BranchBlock *parent_csp = current_csp - 1;
  7227. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7228. bh_assert(parent_csp->local_use_mask);
  7229. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7230. parent_csp->local_use_mask, local_mask_size);
  7231. }
  7232. return true;
  7233. }
  7234. static void
  7235. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7236. {
  7237. BranchBlock *current_csp = ctx->frame_csp - 1;
  7238. bh_assert(current_csp->local_use_mask
  7239. || current_csp->local_use_mask_size == 0);
  7240. if (current_csp->local_use_mask) {
  7241. wasm_runtime_free(current_csp->local_use_mask);
  7242. }
  7243. current_csp->local_use_mask = NULL;
  7244. current_csp->local_use_mask_size = 0;
  7245. }
  7246. static void
  7247. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7248. {
  7249. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7250. uint32 i;
  7251. for (i = 0; i < ctx->csp_num; i++) {
  7252. if (tmp_csp->local_use_mask) {
  7253. wasm_runtime_free(tmp_csp->local_use_mask);
  7254. tmp_csp->local_use_mask = NULL;
  7255. tmp_csp->local_use_mask_size = 0;
  7256. }
  7257. tmp_csp++;
  7258. }
  7259. }
  7260. static void
  7261. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7262. {
  7263. BranchBlock *current_csp = ctx->frame_csp - 1;
  7264. uint32 byte_offset = index / sizeof(uint8);
  7265. uint32 bit_offset = index % sizeof(uint8);
  7266. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7267. bh_assert(current_csp->local_use_mask);
  7268. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7269. }
  7270. static bool
  7271. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7272. {
  7273. BranchBlock *current_csp = ctx->frame_csp - 1;
  7274. uint32 byte_offset = index / sizeof(uint8);
  7275. uint32 bit_offset = index % sizeof(uint8);
  7276. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7277. bh_assert(current_csp->local_use_mask);
  7278. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7279. ? true
  7280. : false;
  7281. }
  7282. #endif /* end of WASM_ENABLE_GC != 0 */
  7283. static void
  7284. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7285. {
  7286. if (ctx) {
  7287. if (ctx->frame_ref_bottom)
  7288. wasm_runtime_free(ctx->frame_ref_bottom);
  7289. #if WASM_ENABLE_GC != 0
  7290. if (ctx->frame_reftype_map_bottom)
  7291. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7292. #endif
  7293. if (ctx->frame_csp_bottom) {
  7294. #if WASM_ENABLE_FAST_INTERP != 0
  7295. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7296. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7297. ctx->csp_num);
  7298. #endif
  7299. #if WASM_ENABLE_GC != 0
  7300. wasm_loader_clean_all_local_use_masks(ctx);
  7301. #endif
  7302. wasm_runtime_free(ctx->frame_csp_bottom);
  7303. }
  7304. #if WASM_ENABLE_FAST_INTERP != 0
  7305. if (ctx->frame_offset_bottom)
  7306. wasm_runtime_free(ctx->frame_offset_bottom);
  7307. if (ctx->const_buf)
  7308. wasm_runtime_free(ctx->const_buf);
  7309. #endif
  7310. wasm_runtime_free(ctx);
  7311. }
  7312. }
  7313. static WASMLoaderContext *
  7314. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7315. {
  7316. WASMLoaderContext *loader_ctx =
  7317. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7318. if (!loader_ctx)
  7319. return NULL;
  7320. loader_ctx->frame_ref_size = 32;
  7321. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7322. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7323. goto fail;
  7324. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7325. #if WASM_ENABLE_GC != 0
  7326. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7327. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7328. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7329. error_buf_size)))
  7330. goto fail;
  7331. loader_ctx->frame_reftype_map_boundary =
  7332. loader_ctx->frame_reftype_map_bottom + 16;
  7333. #endif
  7334. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7335. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7336. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7337. goto fail;
  7338. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7339. #if WASM_ENABLE_EXCE_HANDLING != 0
  7340. func->exception_handler_count = 0;
  7341. #endif
  7342. #if WASM_ENABLE_FAST_INTERP != 0
  7343. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7344. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7345. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7346. error_buf_size)))
  7347. goto fail;
  7348. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7349. loader_ctx->num_const = 0;
  7350. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7351. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7352. error_buf, error_buf_size)))
  7353. goto fail;
  7354. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7355. set_error_buf(error_buf, error_buf_size,
  7356. "fast interpreter offset overflow");
  7357. goto fail;
  7358. }
  7359. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7360. loader_ctx->max_dynamic_offset =
  7361. func->param_cell_num + func->local_cell_num;
  7362. #endif
  7363. return loader_ctx;
  7364. fail:
  7365. wasm_loader_ctx_destroy(loader_ctx);
  7366. return NULL;
  7367. }
  7368. static bool
  7369. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7370. uint32 error_buf_size)
  7371. {
  7372. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7373. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7374. /* Increase the frame ref stack */
  7375. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7376. ctx->frame_ref_size + 16);
  7377. ctx->frame_ref_size += 16;
  7378. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7379. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7380. }
  7381. #if WASM_ENABLE_GC != 0
  7382. if (wasm_is_type_multi_byte_type(type)
  7383. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7384. /* Increase the frame reftype map stack */
  7385. bh_assert(
  7386. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7387. * sizeof(WASMRefTypeMap))
  7388. == ctx->frame_reftype_map_size);
  7389. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7390. ctx->frame_reftype_map_size
  7391. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7392. ctx->frame_reftype_map =
  7393. ctx->frame_reftype_map_bottom
  7394. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7395. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7396. ctx->frame_reftype_map_boundary =
  7397. ctx->frame_reftype_map_bottom
  7398. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7399. }
  7400. #endif
  7401. return true;
  7402. fail:
  7403. return false;
  7404. }
  7405. static bool
  7406. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7407. uint32 error_buf_size)
  7408. {
  7409. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7410. uint32 i;
  7411. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7412. return false;
  7413. #if WASM_ENABLE_GC != 0
  7414. if (wasm_is_type_multi_byte_type(type)) {
  7415. WASMRefType *ref_type;
  7416. if (!(ref_type =
  7417. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7418. error_buf, error_buf_size))) {
  7419. return false;
  7420. }
  7421. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7422. /* Increase the frame reftype map stack */
  7423. bh_assert((uint32)((ctx->frame_reftype_map
  7424. - ctx->frame_reftype_map_bottom)
  7425. * sizeof(WASMRefTypeMap))
  7426. == ctx->frame_reftype_map_size);
  7427. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7428. ctx->frame_reftype_map_size,
  7429. ctx->frame_reftype_map_size
  7430. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7431. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7432. + ctx->frame_reftype_map_size
  7433. / ((uint32)sizeof(WASMRefTypeMap));
  7434. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7435. ctx->frame_reftype_map_boundary =
  7436. ctx->frame_reftype_map_bottom
  7437. + ctx->frame_reftype_map_size
  7438. / ((uint32)sizeof(WASMRefTypeMap));
  7439. }
  7440. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7441. ctx->frame_reftype_map->ref_type = ref_type;
  7442. ctx->frame_reftype_map++;
  7443. ctx->reftype_map_num++;
  7444. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7445. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7446. }
  7447. #endif
  7448. for (i = 0; i < type_cell_num; i++)
  7449. *ctx->frame_ref++ = type;
  7450. ctx->stack_cell_num += type_cell_num;
  7451. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7452. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7453. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7454. set_error_buf(error_buf, error_buf_size,
  7455. "operand stack depth limit exceeded");
  7456. return false;
  7457. }
  7458. }
  7459. return true;
  7460. #if WASM_ENABLE_GC != 0
  7461. fail:
  7462. return false;
  7463. #endif
  7464. }
  7465. static bool
  7466. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7467. int32 stack_cell_num,
  7468. #if WASM_ENABLE_GC != 0
  7469. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7470. #endif
  7471. uint8 type,
  7472. #if WASM_ENABLE_GC != 0
  7473. WASMRefType *ref_type,
  7474. #endif
  7475. char *error_buf, uint32 error_buf_size)
  7476. {
  7477. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7478. #if WASM_ENABLE_GC != 0
  7479. WASMRefType *frame_reftype = NULL;
  7480. #endif
  7481. if (stack_cell_num < type_cell_num) {
  7482. set_error_buf(error_buf, error_buf_size,
  7483. "type mismatch: expect data but stack was empty");
  7484. return false;
  7485. }
  7486. #if WASM_ENABLE_GC == 0
  7487. for (i = 0; i < type_cell_num; i++) {
  7488. if (*(frame_ref - 1 - i) != type) {
  7489. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7490. "type mismatch: expect ", type2str(type),
  7491. " but got other");
  7492. return false;
  7493. }
  7494. }
  7495. #else
  7496. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7497. bh_assert(reftype_map_num > 0);
  7498. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7499. }
  7500. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7501. ref_type, ctx->module->types,
  7502. ctx->module->type_count)) {
  7503. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7504. "type mismatch: expect ", type2str(type),
  7505. " but got other");
  7506. return false;
  7507. }
  7508. for (i = 0; i < type_cell_num - 1; i++) {
  7509. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7510. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7511. "type mismatch: expect ", type2str(type),
  7512. " but got other");
  7513. return false;
  7514. }
  7515. }
  7516. #endif
  7517. return true;
  7518. }
  7519. static bool
  7520. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7521. uint32 error_buf_size)
  7522. {
  7523. int32 block_stack_cell_num =
  7524. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7525. #if WASM_ENABLE_GC != 0
  7526. int32 reftype_map_num =
  7527. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7528. #endif
  7529. if (block_stack_cell_num > 0) {
  7530. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7531. /* the stack top is a value of any type, return success */
  7532. return true;
  7533. }
  7534. #if WASM_ENABLE_GC != 0
  7535. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7536. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7537. WASMRefType *stack_top_ref_type = NULL;
  7538. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7539. bh_assert(reftype_map_num > 0);
  7540. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7541. }
  7542. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7543. ctx->ref_type_tmp, ctx->module->types,
  7544. ctx->module->type_count)) {
  7545. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7546. uint32 ref_type_struct_size =
  7547. wasm_reftype_struct_size(stack_top_ref_type);
  7548. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7549. stack_top_ref_type, ref_type_struct_size);
  7550. }
  7551. return true;
  7552. }
  7553. }
  7554. #endif
  7555. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7556. #if WASM_ENABLE_GC != 0
  7557. ctx->frame_reftype_map, reftype_map_num,
  7558. #endif
  7559. type,
  7560. #if WASM_ENABLE_GC != 0
  7561. ctx->ref_type_tmp,
  7562. #endif
  7563. error_buf, error_buf_size)) {
  7564. return false;
  7565. }
  7566. return true;
  7567. }
  7568. static bool
  7569. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7570. uint32 error_buf_size)
  7571. {
  7572. BranchBlock *cur_block = ctx->frame_csp - 1;
  7573. int32 available_stack_cell =
  7574. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7575. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7576. /* Directly return success if current block is in stack
  7577. polymorphic state while stack is empty. */
  7578. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7579. return true;
  7580. if (type == VALUE_TYPE_VOID)
  7581. return true;
  7582. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7583. return false;
  7584. bh_assert(available_stack_cell > 0);
  7585. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7586. type = VALUE_TYPE_ANY;
  7587. cell_num_to_pop = 1;
  7588. }
  7589. ctx->frame_ref -= cell_num_to_pop;
  7590. ctx->stack_cell_num -= cell_num_to_pop;
  7591. #if WASM_ENABLE_GC != 0
  7592. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7593. ctx->frame_reftype_map--;
  7594. ctx->reftype_map_num--;
  7595. }
  7596. #endif
  7597. return true;
  7598. }
  7599. #if WASM_ENABLE_GC != 0
  7600. /* Get the stack top element of current block */
  7601. static bool
  7602. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7603. WASMRefType **p_ref_type, char *error_buf,
  7604. uint32 error_buf_size)
  7605. {
  7606. BranchBlock *cur_block = ctx->frame_csp - 1;
  7607. int32 available_stack_cell =
  7608. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7609. if (available_stack_cell <= 0) {
  7610. /* Directly return success if current block is in stack
  7611. polymorphic state while stack is empty. */
  7612. if (cur_block->is_stack_polymorphic) {
  7613. *p_type = VALUE_TYPE_ANY;
  7614. return true;
  7615. }
  7616. else {
  7617. set_error_buf(
  7618. error_buf, error_buf_size,
  7619. "type mismatch: expect data but block stack was empty");
  7620. return false;
  7621. }
  7622. }
  7623. *p_type = *(ctx->frame_ref - 1);
  7624. if (wasm_is_type_multi_byte_type(*p_type)) {
  7625. int32 available_reftype_map =
  7626. (int32)(ctx->reftype_map_num
  7627. - (ctx->frame_csp - 1)->reftype_map_num);
  7628. bh_assert(available_reftype_map > 0);
  7629. (void)available_reftype_map;
  7630. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7631. }
  7632. return true;
  7633. }
  7634. #if WASM_ENABLE_FAST_INTERP != 0
  7635. static bool
  7636. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7637. char *error_buf, uint32 error_buf_size);
  7638. #endif
  7639. /* Check whether the stack top elem is a heap object, and if yes,
  7640. pop and return it */
  7641. static bool
  7642. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7643. WASMRefType *ref_ht_ret, char *error_buf,
  7644. uint32 error_buf_size)
  7645. {
  7646. uint8 type = 0;
  7647. WASMRefType *ref_type = NULL;
  7648. /* Get stack top element */
  7649. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7650. error_buf_size)) {
  7651. return false;
  7652. }
  7653. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7654. /* stack top isn't a ref type */
  7655. && !wasm_is_type_reftype(type)) {
  7656. set_error_buf(error_buf, error_buf_size,
  7657. "type mismatch: expect heap object but got others");
  7658. return false;
  7659. }
  7660. /* POP stack top */
  7661. if (wasm_is_type_multi_byte_type(type)) {
  7662. bh_assert(ref_type);
  7663. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7664. wasm_reftype_struct_size(ref_type));
  7665. }
  7666. #if WASM_ENABLE_FAST_INTERP != 0
  7667. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7668. error_buf_size)) {
  7669. return false;
  7670. }
  7671. #else
  7672. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7673. return false;
  7674. }
  7675. #endif
  7676. if (p_type)
  7677. *p_type = type;
  7678. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7679. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7680. wasm_reftype_struct_size(ref_type));
  7681. }
  7682. return true;
  7683. }
  7684. /* Check whether the stack top elem is subtype of (ref null ht),
  7685. and if yes, pop it and return the converted (ref ht) */
  7686. static bool
  7687. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7688. WASMRefType *ref_ht_ret, char *error_buf,
  7689. uint32 error_buf_size)
  7690. {
  7691. uint8 type = 0;
  7692. WASMRefType ref_type = { 0 };
  7693. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7694. error_buf_size)) {
  7695. return false;
  7696. }
  7697. /* Convert to related (ref ht) and return */
  7698. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7699. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7700. */
  7701. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7702. HEAP_TYPE_ARRAY
  7703. + (type - REF_TYPE_ARRAYREF));
  7704. type = ref_ht_ret->ref_type;
  7705. }
  7706. else if (wasm_is_reftype_htref_nullable(type)
  7707. || wasm_is_reftype_htref_non_nullable(type)) {
  7708. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7709. wasm_reftype_struct_size(&ref_type));
  7710. /* Convert to (ref ht) */
  7711. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7712. ref_ht_ret->ref_ht_common.nullable = false;
  7713. type = ref_ht_ret->ref_type;
  7714. }
  7715. *p_type = type;
  7716. return true;
  7717. }
  7718. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7719. and if yes, pop it and return the type_idx */
  7720. static bool
  7721. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7722. uint32 *p_type_idx, char *error_buf,
  7723. uint32 error_buf_size)
  7724. {
  7725. uint8 type = 0;
  7726. int32 type_idx = -1;
  7727. WASMRefType *ref_type = NULL;
  7728. /* Get stack top element */
  7729. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7730. error_buf_size)) {
  7731. return false;
  7732. }
  7733. if (type != VALUE_TYPE_ANY) {
  7734. /* stack top isn't (ref null $t) */
  7735. if (!((wasm_is_reftype_htref_nullable(type)
  7736. || wasm_is_reftype_htref_non_nullable(type))
  7737. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7738. set_error_buf(error_buf, error_buf_size,
  7739. "type mismatch: expect (ref null $t) but got others");
  7740. return false;
  7741. }
  7742. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7743. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7744. wasm_reftype_struct_size(ref_type));
  7745. }
  7746. /* POP stack top */
  7747. #if WASM_ENABLE_FAST_INTERP != 0
  7748. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7749. error_buf_size)) {
  7750. return false;
  7751. }
  7752. #else
  7753. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7754. return false;
  7755. }
  7756. #endif
  7757. /* Convert to type_idx and return */
  7758. *p_type = type;
  7759. if (type != VALUE_TYPE_ANY)
  7760. *p_type_idx = (uint32)type_idx;
  7761. return true;
  7762. }
  7763. #endif /* WASM_ENABLE_GC != 0 */
  7764. #if WASM_ENABLE_FAST_INTERP == 0
  7765. static bool
  7766. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7767. uint8 type_push, uint8 type_pop, char *error_buf,
  7768. uint32 error_buf_size)
  7769. {
  7770. for (int i = 0; i < pop_cnt; i++) {
  7771. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7772. error_buf_size))
  7773. return false;
  7774. }
  7775. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7776. return false;
  7777. return true;
  7778. }
  7779. #endif
  7780. static bool
  7781. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7782. BlockType block_type, uint8 *start_addr,
  7783. char *error_buf, uint32 error_buf_size)
  7784. {
  7785. CHECK_CSP_PUSH();
  7786. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7787. ctx->frame_csp->label_type = label_type;
  7788. ctx->frame_csp->block_type = block_type;
  7789. ctx->frame_csp->start_addr = start_addr;
  7790. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7791. #if WASM_ENABLE_GC != 0
  7792. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7793. #endif
  7794. #if WASM_ENABLE_FAST_INTERP != 0
  7795. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7796. ctx->frame_csp->patch_list = NULL;
  7797. #endif
  7798. ctx->frame_csp++;
  7799. ctx->csp_num++;
  7800. if (ctx->csp_num > ctx->max_csp_num) {
  7801. ctx->max_csp_num = ctx->csp_num;
  7802. if (ctx->max_csp_num > UINT16_MAX) {
  7803. set_error_buf(error_buf, error_buf_size,
  7804. "label stack depth limit exceeded");
  7805. return false;
  7806. }
  7807. }
  7808. return true;
  7809. fail:
  7810. return false;
  7811. }
  7812. static bool
  7813. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7814. uint32 error_buf_size)
  7815. {
  7816. CHECK_CSP_POP();
  7817. #if WASM_ENABLE_FAST_INTERP != 0
  7818. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7819. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7820. #endif
  7821. ctx->frame_csp--;
  7822. ctx->csp_num--;
  7823. return true;
  7824. fail:
  7825. return false;
  7826. }
  7827. #if WASM_ENABLE_FAST_INTERP != 0
  7828. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7829. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7830. #define emit_label(opcode) \
  7831. do { \
  7832. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7833. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7834. } while (0)
  7835. #define skip_label() \
  7836. do { \
  7837. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7838. LOG_OP("\ndelete last op\n"); \
  7839. } while (0)
  7840. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7841. #if UINTPTR_MAX == UINT64_MAX
  7842. #define emit_label(opcode) \
  7843. do { \
  7844. int32 offset = \
  7845. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7846. /* emit int32 relative offset in 64-bit target */ \
  7847. wasm_loader_emit_uint32(loader_ctx, offset); \
  7848. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7849. } while (0)
  7850. #else
  7851. #define emit_label(opcode) \
  7852. do { \
  7853. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7854. /* emit uint32 label address in 32-bit target */ \
  7855. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7856. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7857. } while (0)
  7858. #endif
  7859. #define skip_label() \
  7860. do { \
  7861. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7862. LOG_OP("\ndelete last op\n"); \
  7863. } while (0)
  7864. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7865. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7866. #define emit_label(opcode) \
  7867. do { \
  7868. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7869. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7870. } while (0)
  7871. #define skip_label() \
  7872. do { \
  7873. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7874. LOG_OP("\ndelete last op\n"); \
  7875. } while (0)
  7876. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7877. #define emit_empty_label_addr_and_frame_ip(type) \
  7878. do { \
  7879. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7880. loader_ctx->p_code_compiled, error_buf, \
  7881. error_buf_size)) \
  7882. goto fail; \
  7883. /* label address, to be patched */ \
  7884. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7885. } while (0)
  7886. #define emit_br_info(frame_csp, is_br) \
  7887. do { \
  7888. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7889. error_buf_size)) \
  7890. goto fail; \
  7891. } while (0)
  7892. #define LAST_OP_OUTPUT_I32() \
  7893. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7894. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7895. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7896. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7897. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7898. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7899. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7900. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7901. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7902. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7903. || (last_op == EXT_OP_COPY_STACK_TOP)
  7904. #define LAST_OP_OUTPUT_I64() \
  7905. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7906. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7907. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7908. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7909. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7910. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7911. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7912. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7913. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7914. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7915. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7916. #define GET_CONST_OFFSET(type, val) \
  7917. do { \
  7918. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7919. &operand_offset, error_buf, \
  7920. error_buf_size))) \
  7921. goto fail; \
  7922. } while (0)
  7923. #define GET_CONST_F32_OFFSET(type, fval) \
  7924. do { \
  7925. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7926. &operand_offset, error_buf, \
  7927. error_buf_size))) \
  7928. goto fail; \
  7929. } while (0)
  7930. #define GET_CONST_F64_OFFSET(type, fval) \
  7931. do { \
  7932. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7933. &operand_offset, error_buf, \
  7934. error_buf_size))) \
  7935. goto fail; \
  7936. } while (0)
  7937. #define emit_operand(ctx, offset) \
  7938. do { \
  7939. wasm_loader_emit_int16(ctx, offset); \
  7940. LOG_OP("%d\t", offset); \
  7941. } while (0)
  7942. #define emit_byte(ctx, byte) \
  7943. do { \
  7944. wasm_loader_emit_uint8(ctx, byte); \
  7945. LOG_OP("%d\t", byte); \
  7946. } while (0)
  7947. #define emit_uint32(ctx, value) \
  7948. do { \
  7949. wasm_loader_emit_uint32(ctx, value); \
  7950. LOG_OP("%d\t", value); \
  7951. } while (0)
  7952. #define emit_uint64(ctx, value) \
  7953. do { \
  7954. wasm_loader_emit_const(ctx, &value, false); \
  7955. LOG_OP("%lld\t", value); \
  7956. } while (0)
  7957. #define emit_float32(ctx, value) \
  7958. do { \
  7959. wasm_loader_emit_const(ctx, &value, true); \
  7960. LOG_OP("%f\t", value); \
  7961. } while (0)
  7962. #define emit_float64(ctx, value) \
  7963. do { \
  7964. wasm_loader_emit_const(ctx, &value, false); \
  7965. LOG_OP("%f\t", value); \
  7966. } while (0)
  7967. static bool
  7968. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7969. {
  7970. if (!(ctx->p_code_compiled =
  7971. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7972. return false;
  7973. ctx->p_code_compiled_end =
  7974. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  7975. /* clean up frame ref */
  7976. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  7977. ctx->frame_ref = ctx->frame_ref_bottom;
  7978. ctx->stack_cell_num = 0;
  7979. #if WASM_ENABLE_GC != 0
  7980. /* clean up reftype map */
  7981. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  7982. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  7983. ctx->reftype_map_num = 0;
  7984. #endif
  7985. /* clean up frame csp */
  7986. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  7987. ctx->frame_csp = ctx->frame_csp_bottom;
  7988. ctx->csp_num = 0;
  7989. ctx->max_csp_num = 0;
  7990. /* clean up frame offset */
  7991. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  7992. ctx->frame_offset = ctx->frame_offset_bottom;
  7993. ctx->dynamic_offset = ctx->start_dynamic_offset;
  7994. /* init preserved local offsets */
  7995. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  7996. /* const buf is reserved */
  7997. return true;
  7998. }
  7999. static void
  8000. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8001. {
  8002. ctx->code_compiled_size += size;
  8003. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8004. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8005. }
  8006. }
  8007. static void
  8008. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8009. {
  8010. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8011. if (ctx->p_code_compiled) {
  8012. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8013. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8014. #endif
  8015. bh_memcpy_s(ctx->p_code_compiled,
  8016. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8017. value, size);
  8018. ctx->p_code_compiled += size;
  8019. }
  8020. else {
  8021. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8022. bh_assert((ctx->code_compiled_size & 1) == 0);
  8023. #endif
  8024. increase_compiled_code_space(ctx, size);
  8025. }
  8026. }
  8027. static void
  8028. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8029. {
  8030. if (ctx->p_code_compiled) {
  8031. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8032. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8033. #endif
  8034. STORE_U32(ctx->p_code_compiled, value);
  8035. ctx->p_code_compiled += sizeof(uint32);
  8036. }
  8037. else {
  8038. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8039. bh_assert((ctx->code_compiled_size & 1) == 0);
  8040. #endif
  8041. increase_compiled_code_space(ctx, sizeof(uint32));
  8042. }
  8043. }
  8044. static void
  8045. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8046. {
  8047. if (ctx->p_code_compiled) {
  8048. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8049. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8050. #endif
  8051. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8052. ctx->p_code_compiled += sizeof(int16);
  8053. }
  8054. else {
  8055. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8056. bh_assert((ctx->code_compiled_size & 1) == 0);
  8057. #endif
  8058. increase_compiled_code_space(ctx, sizeof(uint16));
  8059. }
  8060. }
  8061. static void
  8062. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8063. {
  8064. if (ctx->p_code_compiled) {
  8065. *(ctx->p_code_compiled) = value;
  8066. ctx->p_code_compiled += sizeof(uint8);
  8067. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8068. ctx->p_code_compiled++;
  8069. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8070. #endif
  8071. }
  8072. else {
  8073. increase_compiled_code_space(ctx, sizeof(uint8));
  8074. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8075. increase_compiled_code_space(ctx, sizeof(uint8));
  8076. bh_assert((ctx->code_compiled_size & 1) == 0);
  8077. #endif
  8078. }
  8079. }
  8080. static void
  8081. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8082. {
  8083. if (ctx->p_code_compiled) {
  8084. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8085. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8086. #endif
  8087. STORE_PTR(ctx->p_code_compiled, value);
  8088. ctx->p_code_compiled += sizeof(void *);
  8089. }
  8090. else {
  8091. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8092. bh_assert((ctx->code_compiled_size & 1) == 0);
  8093. #endif
  8094. increase_compiled_code_space(ctx, sizeof(void *));
  8095. }
  8096. }
  8097. static void
  8098. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8099. {
  8100. if (ctx->p_code_compiled) {
  8101. ctx->p_code_compiled -= size;
  8102. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8103. if (size == sizeof(uint8)) {
  8104. ctx->p_code_compiled--;
  8105. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8106. }
  8107. #endif
  8108. }
  8109. else {
  8110. ctx->code_compiled_size -= size;
  8111. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8112. if (size == sizeof(uint8)) {
  8113. ctx->code_compiled_size--;
  8114. bh_assert((ctx->code_compiled_size & 1) == 0);
  8115. }
  8116. #endif
  8117. }
  8118. }
  8119. static bool
  8120. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8121. uint32 local_index, uint32 local_type,
  8122. bool *preserved, char *error_buf,
  8123. uint32 error_buf_size)
  8124. {
  8125. uint32 i = 0;
  8126. int16 preserved_offset = (int16)local_index;
  8127. *preserved = false;
  8128. while (i < loader_ctx->stack_cell_num) {
  8129. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8130. /* move previous local into dynamic space before a set/tee_local opcode
  8131. */
  8132. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8133. if (!(*preserved)) {
  8134. *preserved = true;
  8135. skip_label();
  8136. preserved_offset = loader_ctx->preserved_local_offset;
  8137. if (loader_ctx->p_code_compiled) {
  8138. bh_assert(preserved_offset != (int16)local_index);
  8139. }
  8140. if (is_32bit_type(local_type)) {
  8141. /* Only increase preserve offset in the second traversal */
  8142. if (loader_ctx->p_code_compiled)
  8143. loader_ctx->preserved_local_offset++;
  8144. emit_label(EXT_OP_COPY_STACK_TOP);
  8145. }
  8146. else {
  8147. if (loader_ctx->p_code_compiled)
  8148. loader_ctx->preserved_local_offset += 2;
  8149. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8150. }
  8151. emit_operand(loader_ctx, local_index);
  8152. emit_operand(loader_ctx, preserved_offset);
  8153. emit_label(opcode);
  8154. }
  8155. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8156. }
  8157. if (is_32bit_type(cur_type))
  8158. i++;
  8159. else
  8160. i += 2;
  8161. }
  8162. (void)error_buf;
  8163. (void)error_buf_size;
  8164. return true;
  8165. }
  8166. static bool
  8167. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8168. char *error_buf, uint32 error_buf_size)
  8169. {
  8170. uint32 i = 0;
  8171. bool preserve_local;
  8172. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8173. blocks will not influence the value of these locals */
  8174. while (i < loader_ctx->stack_cell_num) {
  8175. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8176. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8177. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8178. && (cur_offset >= 0)) {
  8179. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8180. cur_type, &preserve_local,
  8181. error_buf, error_buf_size)))
  8182. return false;
  8183. }
  8184. if (is_32bit_type(cur_type)) {
  8185. i++;
  8186. }
  8187. else {
  8188. i += 2;
  8189. }
  8190. }
  8191. return true;
  8192. }
  8193. static bool
  8194. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8195. uint8 *p_code_compiled, char *error_buf,
  8196. uint32 error_buf_size)
  8197. {
  8198. BranchBlockPatch *patch =
  8199. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8200. if (!patch) {
  8201. return false;
  8202. }
  8203. patch->patch_type = patch_type;
  8204. patch->code_compiled = p_code_compiled;
  8205. if (!frame_csp->patch_list) {
  8206. frame_csp->patch_list = patch;
  8207. patch->next = NULL;
  8208. }
  8209. else {
  8210. patch->next = frame_csp->patch_list;
  8211. frame_csp->patch_list = patch;
  8212. }
  8213. return true;
  8214. }
  8215. static void
  8216. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8217. {
  8218. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8219. BranchBlockPatch *node = frame_csp->patch_list;
  8220. BranchBlockPatch *node_prev = NULL, *node_next;
  8221. if (!ctx->p_code_compiled)
  8222. return;
  8223. while (node) {
  8224. node_next = node->next;
  8225. if (node->patch_type == patch_type) {
  8226. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8227. if (node_prev == NULL) {
  8228. frame_csp->patch_list = node_next;
  8229. }
  8230. else {
  8231. node_prev->next = node_next;
  8232. }
  8233. wasm_runtime_free(node);
  8234. }
  8235. else {
  8236. node_prev = node;
  8237. }
  8238. node = node_next;
  8239. }
  8240. }
  8241. static bool
  8242. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8243. bool is_br, char *error_buf, uint32 error_buf_size)
  8244. {
  8245. /* br info layout:
  8246. * a) arity of target block
  8247. * b) total cell num of arity values
  8248. * c) each arity value's cell num
  8249. * d) each arity value's src frame offset
  8250. * e) each arity values's dst dynamic offset
  8251. * f) branch target address
  8252. *
  8253. * Note: b-e are omitted when arity is 0 so that
  8254. * interpreter can recover the br info quickly.
  8255. */
  8256. BlockType *block_type = &frame_csp->block_type;
  8257. uint8 *types = NULL, cell;
  8258. #if WASM_ENABLE_GC != 0
  8259. WASMRefTypeMap *reftype_maps;
  8260. uint32 reftype_map_count;
  8261. #endif
  8262. uint32 arity = 0;
  8263. int32 i;
  8264. int16 *frame_offset = ctx->frame_offset;
  8265. uint16 dynamic_offset;
  8266. /* Note: loop's arity is different from if and block. loop's arity is
  8267. * its parameter count while if and block arity is result count.
  8268. */
  8269. #if WASM_ENABLE_GC == 0
  8270. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8271. arity = block_type_get_param_types(block_type, &types);
  8272. else
  8273. arity = block_type_get_result_types(block_type, &types);
  8274. #else
  8275. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8276. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8277. &reftype_map_count);
  8278. else
  8279. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8280. &reftype_map_count);
  8281. #endif
  8282. /* Part a */
  8283. emit_uint32(ctx, arity);
  8284. if (arity) {
  8285. /* Part b */
  8286. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8287. /* Part c */
  8288. for (i = (int32)arity - 1; i >= 0; i--) {
  8289. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8290. emit_byte(ctx, cell);
  8291. }
  8292. /* Part d */
  8293. for (i = (int32)arity - 1; i >= 0; i--) {
  8294. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8295. frame_offset -= cell;
  8296. emit_operand(ctx, *(int16 *)(frame_offset));
  8297. }
  8298. /* Part e */
  8299. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8300. /* Use start_dynamic_offset which was set in
  8301. copy_params_to_dynamic_space */
  8302. dynamic_offset = frame_csp->start_dynamic_offset
  8303. + wasm_get_cell_num(types, arity);
  8304. else
  8305. dynamic_offset =
  8306. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8307. if (is_br)
  8308. ctx->dynamic_offset = dynamic_offset;
  8309. for (i = (int32)arity - 1; i >= 0; i--) {
  8310. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8311. dynamic_offset -= cell;
  8312. emit_operand(ctx, dynamic_offset);
  8313. }
  8314. }
  8315. /* Part f */
  8316. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8317. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8318. }
  8319. else {
  8320. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8321. error_buf, error_buf_size))
  8322. return false;
  8323. /* label address, to be patched */
  8324. wasm_loader_emit_ptr(ctx, NULL);
  8325. }
  8326. return true;
  8327. }
  8328. static bool
  8329. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8330. bool disable_emit, int16 operand_offset,
  8331. char *error_buf, uint32 error_buf_size)
  8332. {
  8333. uint32 cell_num_to_push, i;
  8334. if (type == VALUE_TYPE_VOID)
  8335. return true;
  8336. /* only check memory overflow in first traverse */
  8337. if (ctx->p_code_compiled == NULL) {
  8338. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8339. return false;
  8340. }
  8341. if (disable_emit)
  8342. *(ctx->frame_offset)++ = operand_offset;
  8343. else {
  8344. emit_operand(ctx, ctx->dynamic_offset);
  8345. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8346. ctx->dynamic_offset++;
  8347. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8348. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8349. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8350. goto fail;
  8351. }
  8352. }
  8353. }
  8354. if (is_32bit_type(type))
  8355. return true;
  8356. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8357. for (i = 0; i < cell_num_to_push; i++) {
  8358. if (ctx->p_code_compiled == NULL) {
  8359. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8360. return false;
  8361. }
  8362. ctx->frame_offset++;
  8363. if (!disable_emit) {
  8364. ctx->dynamic_offset++;
  8365. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8366. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8367. if (ctx->max_dynamic_offset >= INT16_MAX)
  8368. goto fail;
  8369. }
  8370. }
  8371. }
  8372. return true;
  8373. fail:
  8374. set_error_buf(error_buf, error_buf_size,
  8375. "fast interpreter offset overflow");
  8376. return false;
  8377. }
  8378. /* This function should be in front of wasm_loader_pop_frame_ref
  8379. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8380. will be modified by wasm_loader_pop_frame_ref */
  8381. static bool
  8382. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8383. char *error_buf, uint32 error_buf_size)
  8384. {
  8385. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8386. then current block is the function block */
  8387. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8388. BranchBlock *cur_block = ctx->frame_csp - depth;
  8389. int32 available_stack_cell =
  8390. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8391. uint32 cell_num_to_pop;
  8392. /* Directly return success if current block is in stack
  8393. polymorphic state while stack is empty. */
  8394. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8395. return true;
  8396. if (type == VALUE_TYPE_VOID)
  8397. return true;
  8398. /* Change type to ANY when the stack top is ANY, so as to avoid
  8399. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8400. two offsets */
  8401. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8402. type = VALUE_TYPE_ANY;
  8403. cell_num_to_pop = wasm_value_type_cell_num(type);
  8404. /* Check the offset stack bottom to ensure the frame offset
  8405. stack will not go underflow. But we don't thrown error
  8406. and return true here, because the error msg should be
  8407. given in wasm_loader_pop_frame_ref */
  8408. if (!check_offset_pop(ctx, cell_num_to_pop))
  8409. return true;
  8410. ctx->frame_offset -= cell_num_to_pop;
  8411. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8412. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8413. ctx->dynamic_offset -= cell_num_to_pop;
  8414. emit_operand(ctx, *(ctx->frame_offset));
  8415. (void)error_buf;
  8416. (void)error_buf_size;
  8417. return true;
  8418. }
  8419. static bool
  8420. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8421. bool disable_emit, int16 operand_offset,
  8422. char *error_buf, uint32 error_buf_size)
  8423. {
  8424. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8425. error_buf, error_buf_size)))
  8426. return false;
  8427. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8428. return false;
  8429. return true;
  8430. }
  8431. static bool
  8432. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8433. char *error_buf, uint32 error_buf_size)
  8434. {
  8435. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8436. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8437. return false;
  8438. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8439. return false;
  8440. return true;
  8441. }
  8442. static bool
  8443. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8444. uint8 type_push, uint8 type_pop,
  8445. bool disable_emit, int16 operand_offset,
  8446. char *error_buf, uint32 error_buf_size)
  8447. {
  8448. uint8 i;
  8449. for (i = 0; i < pop_cnt; i++) {
  8450. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8451. error_buf_size))
  8452. return false;
  8453. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8454. error_buf_size))
  8455. return false;
  8456. }
  8457. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8458. operand_offset, error_buf,
  8459. error_buf_size))
  8460. return false;
  8461. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8462. return false;
  8463. return true;
  8464. }
  8465. static bool
  8466. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8467. int16 *offset, char *error_buf,
  8468. uint32 error_buf_size)
  8469. {
  8470. int8 bytes_to_increase;
  8471. int16 operand_offset = 0;
  8472. Const *c;
  8473. /* Search existing constant */
  8474. for (c = (Const *)ctx->const_buf;
  8475. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8476. /* TODO: handle v128 type? */
  8477. if ((type == c->value_type)
  8478. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8479. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8480. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8481. || (type == VALUE_TYPE_FUNCREF
  8482. && *(int32 *)value == c->value.i32)
  8483. || (type == VALUE_TYPE_EXTERNREF
  8484. && *(int32 *)value == c->value.i32)
  8485. #endif
  8486. || (type == VALUE_TYPE_F64
  8487. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8488. || (type == VALUE_TYPE_F32
  8489. && (0
  8490. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8491. operand_offset = c->slot_index;
  8492. break;
  8493. }
  8494. if (is_32bit_type(c->value_type))
  8495. operand_offset += 1;
  8496. else
  8497. operand_offset += 2;
  8498. }
  8499. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8500. /* New constant, append to the const buffer */
  8501. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8502. bytes_to_increase = 2;
  8503. }
  8504. else {
  8505. bytes_to_increase = 1;
  8506. }
  8507. /* The max cell num of const buffer is 32768 since the valid index range
  8508. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8509. * full */
  8510. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8511. *offset = 0;
  8512. return true;
  8513. }
  8514. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8515. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8516. ctx->const_buf_size + 4 * sizeof(Const));
  8517. ctx->const_buf_size += 4 * sizeof(Const);
  8518. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8519. }
  8520. c->value_type = type;
  8521. switch (type) {
  8522. case VALUE_TYPE_F64:
  8523. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8524. sizeof(float64));
  8525. ctx->const_cell_num += 2;
  8526. /* The const buf will be reversed, we use the second cell */
  8527. /* of the i64/f64 const so the final offset is correct */
  8528. operand_offset++;
  8529. break;
  8530. case VALUE_TYPE_I64:
  8531. c->value.i64 = *(int64 *)value;
  8532. ctx->const_cell_num += 2;
  8533. operand_offset++;
  8534. break;
  8535. case VALUE_TYPE_F32:
  8536. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8537. sizeof(float32));
  8538. ctx->const_cell_num++;
  8539. break;
  8540. case VALUE_TYPE_I32:
  8541. c->value.i32 = *(int32 *)value;
  8542. ctx->const_cell_num++;
  8543. break;
  8544. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8545. case VALUE_TYPE_EXTERNREF:
  8546. case VALUE_TYPE_FUNCREF:
  8547. c->value.i32 = *(int32 *)value;
  8548. ctx->const_cell_num++;
  8549. break;
  8550. #endif
  8551. default:
  8552. break;
  8553. }
  8554. c->slot_index = operand_offset;
  8555. ctx->num_const++;
  8556. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8557. (int64)c->value.i64);
  8558. }
  8559. /* use negative index for const */
  8560. operand_offset = -(operand_offset + 1);
  8561. *offset = operand_offset;
  8562. return true;
  8563. fail:
  8564. return false;
  8565. }
  8566. /*
  8567. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8568. -- Mostly used for the binary / compare operation
  8569. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8570. -- Mostly used in block / control instructions
  8571. The POP will always emit the offset on the top of the frame_offset stack
  8572. PUSH can be used in two ways:
  8573. 1. directly PUSH:
  8574. PUSH_XXX();
  8575. will allocate a dynamic space and emit
  8576. 2. silent PUSH:
  8577. operand_offset = xxx; disable_emit = true;
  8578. PUSH_XXX();
  8579. only push the frame_offset stack, no emit
  8580. */
  8581. #define TEMPLATE_PUSH(Type) \
  8582. do { \
  8583. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8584. disable_emit, operand_offset, \
  8585. error_buf, error_buf_size)) \
  8586. goto fail; \
  8587. } while (0)
  8588. #define TEMPLATE_PUSH_REF(Type) \
  8589. do { \
  8590. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8591. operand_offset, error_buf, \
  8592. error_buf_size)) \
  8593. goto fail; \
  8594. } while (0)
  8595. #define TEMPLATE_POP(Type) \
  8596. do { \
  8597. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8598. error_buf, error_buf_size)) \
  8599. goto fail; \
  8600. } while (0)
  8601. #define TEMPLATE_POP_REF(Type) \
  8602. do { \
  8603. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8604. error_buf_size)) \
  8605. goto fail; \
  8606. } while (0)
  8607. #define PUSH_OFFSET_TYPE(type) \
  8608. do { \
  8609. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8610. operand_offset, error_buf, \
  8611. error_buf_size))) \
  8612. goto fail; \
  8613. } while (0)
  8614. #define POP_OFFSET_TYPE(type) \
  8615. do { \
  8616. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8617. error_buf_size))) \
  8618. goto fail; \
  8619. } while (0)
  8620. #define POP_AND_PUSH(type_pop, type_push) \
  8621. do { \
  8622. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8623. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8624. operand_offset, error_buf, error_buf_size))) \
  8625. goto fail; \
  8626. } while (0)
  8627. /* type of POPs should be the same */
  8628. #define POP2_AND_PUSH(type_pop, type_push) \
  8629. do { \
  8630. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8631. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8632. operand_offset, error_buf, error_buf_size))) \
  8633. goto fail; \
  8634. } while (0)
  8635. #else /* WASM_ENABLE_FAST_INTERP */
  8636. #define TEMPLATE_PUSH(Type) \
  8637. do { \
  8638. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8639. error_buf, error_buf_size))) \
  8640. goto fail; \
  8641. } while (0)
  8642. #define TEMPLATE_PUSH_REF(Type) \
  8643. do { \
  8644. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8645. error_buf_size))) \
  8646. goto fail; \
  8647. } while (0)
  8648. #define TEMPLATE_POP(Type) \
  8649. do { \
  8650. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8651. error_buf, error_buf_size))) \
  8652. goto fail; \
  8653. } while (0)
  8654. #define TEMPLATE_POP_REF(Type) \
  8655. do { \
  8656. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8657. error_buf_size))) \
  8658. goto fail; \
  8659. } while (0)
  8660. #define POP_AND_PUSH(type_pop, type_push) \
  8661. do { \
  8662. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8663. type_pop, error_buf, \
  8664. error_buf_size))) \
  8665. goto fail; \
  8666. } while (0)
  8667. /* type of POPs should be the same */
  8668. #define POP2_AND_PUSH(type_pop, type_push) \
  8669. do { \
  8670. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8671. type_pop, error_buf, \
  8672. error_buf_size))) \
  8673. goto fail; \
  8674. } while (0)
  8675. #endif /* WASM_ENABLE_FAST_INTERP */
  8676. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8677. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8678. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8679. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8680. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8681. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8682. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8683. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8684. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8685. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8686. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8687. #define POP_I32() TEMPLATE_POP(I32)
  8688. #define POP_F32() TEMPLATE_POP(F32)
  8689. #define POP_I64() TEMPLATE_POP(I64)
  8690. #define POP_F64() TEMPLATE_POP(F64)
  8691. #define POP_V128() TEMPLATE_POP(V128)
  8692. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8693. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8694. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8695. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8696. #if WASM_ENABLE_FAST_INTERP != 0
  8697. static bool
  8698. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8699. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8700. {
  8701. int16 operand_offset = 0;
  8702. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8703. : loader_ctx->frame_csp;
  8704. BlockType *block_type = &block->block_type;
  8705. uint8 *return_types = NULL;
  8706. #if WASM_ENABLE_GC != 0
  8707. WASMRefTypeMap *reftype_maps = NULL;
  8708. uint32 reftype_map_count;
  8709. #endif
  8710. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8711. int32 i = 0;
  8712. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8713. *frame_offset_org = NULL;
  8714. #if WASM_ENABLE_GC == 0
  8715. return_count = block_type_get_result_types(block_type, &return_types);
  8716. #else
  8717. return_count = block_type_get_result_types(
  8718. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8719. #endif
  8720. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8721. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8722. if (return_count == 1) {
  8723. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8724. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8725. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8726. /* insert op_copy before else opcode */
  8727. if (opcode == WASM_OP_ELSE)
  8728. skip_label();
  8729. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8730. : EXT_OP_COPY_STACK_TOP_I64);
  8731. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8732. emit_operand(loader_ctx, block->dynamic_offset);
  8733. if (opcode == WASM_OP_ELSE) {
  8734. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8735. }
  8736. else {
  8737. loader_ctx->frame_offset -= cell;
  8738. loader_ctx->dynamic_offset = block->dynamic_offset;
  8739. PUSH_OFFSET_TYPE(return_types[0]);
  8740. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8741. }
  8742. if (opcode == WASM_OP_ELSE)
  8743. emit_label(opcode);
  8744. }
  8745. return true;
  8746. }
  8747. /* Copy stack top values to block's results which are in dynamic space.
  8748. * The instruction format:
  8749. * Part a: values count
  8750. * Part b: all values total cell num
  8751. * Part c: each value's cell_num, src offset and dst offset
  8752. * Part d: each value's src offset and dst offset
  8753. * Part e: each value's dst offset
  8754. */
  8755. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8756. dynamic_offset = dynamic_offset_org =
  8757. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8758. /* First traversal to get the count of values needed to be copied. */
  8759. for (i = (int32)return_count - 1; i >= 0; i--) {
  8760. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8761. frame_offset -= cells;
  8762. dynamic_offset -= cells;
  8763. if (dynamic_offset != *frame_offset) {
  8764. value_count++;
  8765. total_cel_num += cells;
  8766. }
  8767. }
  8768. if (value_count) {
  8769. uint32 j = 0;
  8770. uint8 *emit_data = NULL, *cells = NULL;
  8771. int16 *src_offsets = NULL;
  8772. uint16 *dst_offsets = NULL;
  8773. uint64 size =
  8774. (uint64)value_count
  8775. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8776. /* Allocate memory for the emit data */
  8777. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8778. return false;
  8779. cells = emit_data;
  8780. src_offsets = (int16 *)(cells + value_count);
  8781. dst_offsets = (uint16 *)(src_offsets + value_count);
  8782. /* insert op_copy before else opcode */
  8783. if (opcode == WASM_OP_ELSE)
  8784. skip_label();
  8785. emit_label(EXT_OP_COPY_STACK_VALUES);
  8786. /* Part a) */
  8787. emit_uint32(loader_ctx, value_count);
  8788. /* Part b) */
  8789. emit_uint32(loader_ctx, total_cel_num);
  8790. /* Second traversal to get each value's cell num, src offset and dst
  8791. * offset. */
  8792. frame_offset = frame_offset_org;
  8793. dynamic_offset = dynamic_offset_org;
  8794. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8795. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8796. frame_offset -= cell;
  8797. dynamic_offset -= cell;
  8798. if (dynamic_offset != *frame_offset) {
  8799. /* cell num */
  8800. cells[j] = cell;
  8801. /* src offset */
  8802. src_offsets[j] = *frame_offset;
  8803. /* dst offset */
  8804. dst_offsets[j] = dynamic_offset;
  8805. j++;
  8806. }
  8807. if (opcode == WASM_OP_ELSE) {
  8808. *frame_offset = dynamic_offset;
  8809. }
  8810. else {
  8811. loader_ctx->frame_offset = frame_offset;
  8812. loader_ctx->dynamic_offset = dynamic_offset;
  8813. PUSH_OFFSET_TYPE(return_types[i]);
  8814. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8815. loader_ctx->frame_offset = frame_offset_org;
  8816. loader_ctx->dynamic_offset = dynamic_offset_org;
  8817. }
  8818. }
  8819. bh_assert(j == value_count);
  8820. /* Emit the cells, src_offsets and dst_offsets */
  8821. for (j = 0; j < value_count; j++)
  8822. emit_byte(loader_ctx, cells[j]);
  8823. for (j = 0; j < value_count; j++)
  8824. emit_operand(loader_ctx, src_offsets[j]);
  8825. for (j = 0; j < value_count; j++)
  8826. emit_operand(loader_ctx, dst_offsets[j]);
  8827. if (opcode == WASM_OP_ELSE)
  8828. emit_label(opcode);
  8829. wasm_runtime_free(emit_data);
  8830. }
  8831. return true;
  8832. fail:
  8833. return false;
  8834. }
  8835. #endif /* WASM_ENABLE_FAST_INTERP */
  8836. #define RESERVE_BLOCK_RET() \
  8837. do { \
  8838. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  8839. error_buf_size)) \
  8840. goto fail; \
  8841. } while (0)
  8842. #define PUSH_TYPE(type) \
  8843. do { \
  8844. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8845. error_buf_size))) \
  8846. goto fail; \
  8847. } while (0)
  8848. #define POP_TYPE(type) \
  8849. do { \
  8850. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8851. error_buf_size))) \
  8852. goto fail; \
  8853. } while (0)
  8854. #if WASM_ENABLE_GC == 0
  8855. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8856. do { \
  8857. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8858. _start_addr, error_buf, \
  8859. error_buf_size)) \
  8860. goto fail; \
  8861. } while (0)
  8862. #define POP_CSP() \
  8863. do { \
  8864. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8865. goto fail; \
  8866. } while (0)
  8867. #else
  8868. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8869. do { \
  8870. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8871. _start_addr, error_buf, \
  8872. error_buf_size)) \
  8873. goto fail; \
  8874. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8875. error_buf, error_buf_size)) { \
  8876. goto fail; \
  8877. } \
  8878. } while (0)
  8879. #define POP_CSP() \
  8880. do { \
  8881. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8882. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8883. goto fail; \
  8884. } while (0)
  8885. #endif /* end of WASM_ENABLE_GC == 0 */
  8886. #if WASM_ENABLE_GC == 0
  8887. #define GET_LOCAL_REFTYPE() (void)0
  8888. #else
  8889. #define GET_LOCAL_REFTYPE() \
  8890. do { \
  8891. if (wasm_is_type_multi_byte_type(local_type)) { \
  8892. WASMRefType *_ref_type; \
  8893. if (local_idx < param_count) \
  8894. _ref_type = wasm_reftype_map_find( \
  8895. param_reftype_maps, param_reftype_map_count, local_idx); \
  8896. else \
  8897. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8898. local_reftype_map_count, \
  8899. local_idx - param_count); \
  8900. bh_assert(_ref_type); \
  8901. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8902. wasm_reftype_struct_size(_ref_type)); \
  8903. } \
  8904. } while (0)
  8905. #endif /* end of WASM_ENABLE_GC == 0 */
  8906. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8907. do { \
  8908. read_leb_uint32(p, p_end, local_idx); \
  8909. if (local_idx >= param_count + local_count) { \
  8910. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8911. goto fail; \
  8912. } \
  8913. local_type = local_idx < param_count \
  8914. ? param_types[local_idx] \
  8915. : local_types[local_idx - param_count]; \
  8916. local_offset = local_offsets[local_idx]; \
  8917. GET_LOCAL_REFTYPE(); \
  8918. } while (0)
  8919. static bool
  8920. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8921. {
  8922. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8923. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8924. return false;
  8925. }
  8926. return true;
  8927. }
  8928. #define CHECK_MEMORY() \
  8929. do { \
  8930. if (!check_memory(module, error_buf, error_buf_size)) \
  8931. goto fail; \
  8932. } while (0)
  8933. static bool
  8934. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8935. uint32 error_buf_size)
  8936. {
  8937. uint8 mem_access_aligns[] = {
  8938. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8939. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8940. };
  8941. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8942. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8943. set_error_buf(error_buf, error_buf_size,
  8944. "alignment must not be larger than natural");
  8945. return false;
  8946. }
  8947. return true;
  8948. }
  8949. #if WASM_ENABLE_SIMD != 0
  8950. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8951. static bool
  8952. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8953. uint32 error_buf_size)
  8954. {
  8955. uint8 mem_access_aligns[] = {
  8956. 4, /* load */
  8957. 3, 3, 3, 3, 3, 3, /* load and extend */
  8958. 0, 1, 2, 3, /* load and splat */
  8959. 4, /* store */
  8960. };
  8961. uint8 mem_access_aligns_load_lane[] = {
  8962. 0, 1, 2, 3, /* load lane */
  8963. 0, 1, 2, 3, /* store lane */
  8964. 2, 3 /* store zero */
  8965. };
  8966. if (!((opcode <= SIMD_v128_store)
  8967. || (SIMD_v128_load8_lane <= opcode
  8968. && opcode <= SIMD_v128_load64_zero))) {
  8969. set_error_buf(error_buf, error_buf_size,
  8970. "the opcode doesn't include memarg");
  8971. return false;
  8972. }
  8973. if ((opcode <= SIMD_v128_store
  8974. && align > mem_access_aligns[opcode - SIMD_v128_load])
  8975. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  8976. && align > mem_access_aligns_load_lane[opcode
  8977. - SIMD_v128_load8_lane])) {
  8978. set_error_buf(error_buf, error_buf_size,
  8979. "alignment must not be larger than natural");
  8980. return false;
  8981. }
  8982. return true;
  8983. }
  8984. static bool
  8985. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  8986. uint32 error_buf_size)
  8987. {
  8988. switch (opcode) {
  8989. case SIMD_i8x16_extract_lane_s:
  8990. case SIMD_i8x16_extract_lane_u:
  8991. case SIMD_i8x16_replace_lane:
  8992. if (lane >= 16) {
  8993. goto fail;
  8994. }
  8995. break;
  8996. case SIMD_i16x8_extract_lane_s:
  8997. case SIMD_i16x8_extract_lane_u:
  8998. case SIMD_i16x8_replace_lane:
  8999. if (lane >= 8) {
  9000. goto fail;
  9001. }
  9002. break;
  9003. case SIMD_i32x4_extract_lane:
  9004. case SIMD_i32x4_replace_lane:
  9005. case SIMD_f32x4_extract_lane:
  9006. case SIMD_f32x4_replace_lane:
  9007. if (lane >= 4) {
  9008. goto fail;
  9009. }
  9010. break;
  9011. case SIMD_i64x2_extract_lane:
  9012. case SIMD_i64x2_replace_lane:
  9013. case SIMD_f64x2_extract_lane:
  9014. case SIMD_f64x2_replace_lane:
  9015. if (lane >= 2) {
  9016. goto fail;
  9017. }
  9018. break;
  9019. case SIMD_v128_load8_lane:
  9020. case SIMD_v128_load16_lane:
  9021. case SIMD_v128_load32_lane:
  9022. case SIMD_v128_load64_lane:
  9023. case SIMD_v128_store8_lane:
  9024. case SIMD_v128_store16_lane:
  9025. case SIMD_v128_store32_lane:
  9026. case SIMD_v128_store64_lane:
  9027. case SIMD_v128_load32_zero:
  9028. case SIMD_v128_load64_zero:
  9029. {
  9030. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9031. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9032. goto fail;
  9033. }
  9034. break;
  9035. }
  9036. default:
  9037. goto fail;
  9038. }
  9039. return true;
  9040. fail:
  9041. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9042. return false;
  9043. }
  9044. static bool
  9045. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9046. {
  9047. uint8 i;
  9048. for (i = 0; i != 16; ++i) {
  9049. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9050. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9051. return false;
  9052. }
  9053. }
  9054. return true;
  9055. }
  9056. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9057. #endif /* end of WASM_ENABLE_SIMD */
  9058. #if WASM_ENABLE_SHARED_MEMORY != 0
  9059. static bool
  9060. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9061. uint32 error_buf_size)
  9062. {
  9063. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9064. uint8 mem_access_aligns[] = {
  9065. 2, 3, 0, 1, 0, 1, 2,
  9066. };
  9067. uint8 expect;
  9068. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9069. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9070. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9071. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9072. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9073. }
  9074. else {
  9075. /* 7 opcodes in every group */
  9076. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9077. }
  9078. if (align != expect) {
  9079. set_error_buf(error_buf, error_buf_size,
  9080. "alignment isn't equal to natural");
  9081. return false;
  9082. }
  9083. return true;
  9084. }
  9085. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9086. static bool
  9087. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9088. char *error_buf, uint32 error_buf_size)
  9089. {
  9090. BranchBlock *target_block, *cur_block;
  9091. BlockType *target_block_type;
  9092. uint8 type, *types = NULL, *frame_ref;
  9093. uint32 arity = 0;
  9094. int32 i, available_stack_cell;
  9095. uint16 cell_num;
  9096. #if WASM_ENABLE_GC != 0
  9097. WASMRefTypeMap *frame_reftype_map;
  9098. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9099. WASMRefType *ref_type;
  9100. uint32 reftype_map_count = 0;
  9101. int32 available_reftype_map;
  9102. bool is_type_multi_byte;
  9103. #endif
  9104. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9105. uint8 *frame_ref_after_popped = NULL;
  9106. uint8 frame_ref_tmp[4] = { 0 };
  9107. uint8 *frame_ref_buf = frame_ref_tmp;
  9108. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9109. #if WASM_ENABLE_GC != 0
  9110. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9111. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9112. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9113. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9114. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9115. #endif
  9116. #if WASM_ENABLE_FAST_INTERP != 0
  9117. int16 *frame_offset_old = loader_ctx->frame_offset;
  9118. int16 *frame_offset_after_popped = NULL;
  9119. int16 frame_offset_tmp[4] = { 0 };
  9120. int16 *frame_offset_buf = frame_offset_tmp;
  9121. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9122. #endif
  9123. bool ret = false;
  9124. bh_assert(loader_ctx->csp_num > 0);
  9125. if (loader_ctx->csp_num - 1 < depth) {
  9126. set_error_buf(error_buf, error_buf_size,
  9127. "unknown label, "
  9128. "unexpected end of section or function");
  9129. return false;
  9130. }
  9131. cur_block = loader_ctx->frame_csp - 1;
  9132. target_block = loader_ctx->frame_csp - (depth + 1);
  9133. target_block_type = &target_block->block_type;
  9134. frame_ref = loader_ctx->frame_ref;
  9135. #if WASM_ENABLE_GC != 0
  9136. frame_reftype_map = loader_ctx->frame_reftype_map;
  9137. #endif
  9138. /* Note: loop's arity is different from if and block. loop's arity is
  9139. * its parameter count while if and block arity is result count.
  9140. */
  9141. #if WASM_ENABLE_GC == 0
  9142. if (target_block->label_type == LABEL_TYPE_LOOP)
  9143. arity = block_type_get_param_types(target_block_type, &types);
  9144. else
  9145. arity = block_type_get_result_types(target_block_type, &types);
  9146. #else
  9147. if (target_block->label_type == LABEL_TYPE_LOOP)
  9148. arity = block_type_get_param_types(target_block_type, &types,
  9149. &reftype_maps, &reftype_map_count);
  9150. else
  9151. arity = block_type_get_result_types(target_block_type, &types,
  9152. &reftype_maps, &reftype_map_count);
  9153. #endif
  9154. /* If the stack is in polymorphic state, just clear the stack
  9155. * and then re-push the values to make the stack top values
  9156. * match block type. */
  9157. if (cur_block->is_stack_polymorphic) {
  9158. #if WASM_ENABLE_GC != 0
  9159. int32 j = reftype_map_count - 1;
  9160. #endif
  9161. for (i = (int32)arity - 1; i >= 0; i--) {
  9162. #if WASM_ENABLE_GC != 0
  9163. if (wasm_is_type_multi_byte_type(types[i])) {
  9164. bh_assert(reftype_maps[j].index == i);
  9165. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9166. reftype_maps[j].ref_type,
  9167. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9168. j--;
  9169. }
  9170. #endif
  9171. #if WASM_ENABLE_FAST_INTERP != 0
  9172. POP_OFFSET_TYPE(types[i]);
  9173. #endif
  9174. POP_TYPE(types[i]);
  9175. }
  9176. /* Backup stack data since it may be changed in the below
  9177. push operations, and the stack data may be used when
  9178. checking other target blocks of opcode br_table */
  9179. if (opcode == WASM_OP_BR_TABLE) {
  9180. uint64 total_size;
  9181. frame_ref_after_popped = loader_ctx->frame_ref;
  9182. total_size = (uint64)sizeof(uint8)
  9183. * (frame_ref_old - frame_ref_after_popped);
  9184. if (total_size > sizeof(frame_ref_tmp)
  9185. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9186. error_buf_size))) {
  9187. goto fail;
  9188. }
  9189. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9190. frame_ref_after_popped, (uint32)total_size);
  9191. #if WASM_ENABLE_GC != 0
  9192. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9193. total_size =
  9194. (uint64)sizeof(WASMRefTypeMap)
  9195. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9196. if (total_size > sizeof(frame_reftype_map_tmp)
  9197. && !(frame_reftype_map_buf = loader_malloc(
  9198. total_size, error_buf, error_buf_size))) {
  9199. goto fail;
  9200. }
  9201. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9202. frame_reftype_map_after_popped, (uint32)total_size);
  9203. #endif
  9204. #if WASM_ENABLE_FAST_INTERP != 0
  9205. frame_offset_after_popped = loader_ctx->frame_offset;
  9206. total_size = (uint64)sizeof(int16)
  9207. * (frame_offset_old - frame_offset_after_popped);
  9208. if (total_size > sizeof(frame_offset_tmp)
  9209. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9210. error_buf_size))) {
  9211. goto fail;
  9212. }
  9213. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9214. frame_offset_after_popped, (uint32)total_size);
  9215. #endif
  9216. }
  9217. #if WASM_ENABLE_GC != 0
  9218. j = 0;
  9219. #endif
  9220. for (i = 0; i < (int32)arity; i++) {
  9221. #if WASM_ENABLE_GC != 0
  9222. if (wasm_is_type_multi_byte_type(types[i])) {
  9223. bh_assert(reftype_maps[j].index == i);
  9224. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9225. reftype_maps[j].ref_type,
  9226. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9227. j++;
  9228. }
  9229. #endif
  9230. #if WASM_ENABLE_FAST_INTERP != 0
  9231. bool disable_emit = true;
  9232. int16 operand_offset = 0;
  9233. PUSH_OFFSET_TYPE(types[i]);
  9234. #endif
  9235. PUSH_TYPE(types[i]);
  9236. }
  9237. #if WASM_ENABLE_FAST_INTERP != 0
  9238. emit_br_info(target_block, opcode == WASM_OP_BR);
  9239. #endif
  9240. /* Restore the stack data, note that frame_ref_bottom,
  9241. frame_reftype_map_bottom, frame_offset_bottom may be
  9242. re-allocated in the above push operations */
  9243. if (opcode == WASM_OP_BR_TABLE) {
  9244. uint32 total_size;
  9245. /* The stack operand num should not be smaller than before
  9246. after pop and push operations */
  9247. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9248. loader_ctx->stack_cell_num = stack_cell_num_old;
  9249. loader_ctx->frame_ref =
  9250. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9251. total_size = (uint32)(sizeof(uint8)
  9252. * (frame_ref_old - frame_ref_after_popped));
  9253. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9254. frame_ref_buf, total_size);
  9255. #if WASM_ENABLE_GC != 0
  9256. /* The stack operand num should not be smaller than before
  9257. after pop and push operations */
  9258. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9259. loader_ctx->reftype_map_num = reftype_map_num_old;
  9260. loader_ctx->frame_reftype_map =
  9261. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9262. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9263. * (frame_reftype_map_old
  9264. - frame_reftype_map_after_popped));
  9265. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9266. total_size, frame_reftype_map_buf, total_size);
  9267. #endif
  9268. #if WASM_ENABLE_FAST_INTERP != 0
  9269. loader_ctx->frame_offset =
  9270. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9271. total_size =
  9272. (uint32)(sizeof(int16)
  9273. * (frame_offset_old - frame_offset_after_popped));
  9274. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9275. total_size, frame_offset_buf, total_size);
  9276. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9277. #endif
  9278. }
  9279. ret = true;
  9280. goto cleanup_and_return;
  9281. }
  9282. available_stack_cell =
  9283. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9284. #if WASM_ENABLE_GC != 0
  9285. available_reftype_map =
  9286. (int32)(loader_ctx->reftype_map_num
  9287. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9288. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9289. #endif
  9290. /* Check stack top values match target block type */
  9291. for (i = (int32)arity - 1; i >= 0; i--) {
  9292. type = types[i];
  9293. #if WASM_ENABLE_GC != 0
  9294. ref_type = NULL;
  9295. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9296. if (is_type_multi_byte) {
  9297. bh_assert(reftype_map);
  9298. ref_type = reftype_map->ref_type;
  9299. }
  9300. #endif
  9301. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9302. break;
  9303. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9304. #if WASM_ENABLE_GC != 0
  9305. frame_reftype_map, available_reftype_map,
  9306. #endif
  9307. type,
  9308. #if WASM_ENABLE_GC != 0
  9309. ref_type,
  9310. #endif
  9311. error_buf, error_buf_size)) {
  9312. goto fail;
  9313. }
  9314. cell_num = wasm_value_type_cell_num(types[i]);
  9315. frame_ref -= cell_num;
  9316. available_stack_cell -= cell_num;
  9317. #if WASM_ENABLE_GC != 0
  9318. if (is_type_multi_byte) {
  9319. frame_reftype_map--;
  9320. available_reftype_map--;
  9321. reftype_map--;
  9322. }
  9323. #endif
  9324. }
  9325. #if WASM_ENABLE_FAST_INTERP != 0
  9326. emit_br_info(target_block, opcode == WASM_OP_BR);
  9327. #endif
  9328. ret = true;
  9329. cleanup_and_return:
  9330. fail:
  9331. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9332. wasm_runtime_free(frame_ref_buf);
  9333. #if WASM_ENABLE_GC != 0
  9334. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9335. wasm_runtime_free(frame_reftype_map_buf);
  9336. #endif
  9337. #if WASM_ENABLE_FAST_INTERP != 0
  9338. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9339. wasm_runtime_free(frame_offset_buf);
  9340. #endif
  9341. return ret;
  9342. }
  9343. static BranchBlock *
  9344. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9345. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9346. {
  9347. uint8 *p = *p_buf, *p_end = buf_end;
  9348. BranchBlock *frame_csp_tmp;
  9349. uint32 depth;
  9350. read_leb_uint32(p, p_end, depth);
  9351. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9352. error_buf_size)) {
  9353. goto fail;
  9354. }
  9355. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9356. *p_buf = p;
  9357. return frame_csp_tmp;
  9358. fail:
  9359. return NULL;
  9360. }
  9361. #if WASM_ENABLE_EXCE_HANDLING != 0
  9362. static BranchBlock *
  9363. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9364. uint8 *buf_end, char *error_buf,
  9365. uint32 error_buf_size)
  9366. {
  9367. uint8 *p = *p_buf, *p_end = buf_end;
  9368. BranchBlock *frame_csp_tmp;
  9369. uint32 depth;
  9370. read_leb_uint32(p, p_end, depth);
  9371. /*
  9372. * Note: "delegate 0" means the surrounding block, not the
  9373. * try-delegate block itself.
  9374. *
  9375. * Note: the caller hasn't popped the try-delegate frame yet.
  9376. */
  9377. bh_assert(loader_ctx->csp_num > 0);
  9378. if (loader_ctx->csp_num - 1 <= depth) {
  9379. #if WASM_ENABLE_SPEC_TEST == 0
  9380. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9381. #else
  9382. set_error_buf(error_buf, error_buf_size, "unknown label");
  9383. #endif
  9384. goto fail;
  9385. }
  9386. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9387. #if WASM_ENABLE_FAST_INTERP != 0
  9388. emit_br_info(frame_csp_tmp, false);
  9389. #endif
  9390. *p_buf = p;
  9391. return frame_csp_tmp;
  9392. fail:
  9393. return NULL;
  9394. }
  9395. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9396. static bool
  9397. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9398. char *error_buf, uint32 error_buf_size)
  9399. {
  9400. BlockType *block_type = &block->block_type;
  9401. uint8 *return_types = NULL;
  9402. uint32 return_count = 0;
  9403. int32 available_stack_cell, return_cell_num, i;
  9404. uint8 *frame_ref = NULL;
  9405. #if WASM_ENABLE_GC != 0
  9406. WASMRefTypeMap *frame_reftype_map;
  9407. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9408. WASMRefType *ref_type;
  9409. uint32 param_count, return_reftype_map_count = 0;
  9410. int32 available_reftype_map =
  9411. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9412. #endif
  9413. available_stack_cell =
  9414. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9415. #if WASM_ENABLE_GC == 0
  9416. return_count = block_type_get_result_types(block_type, &return_types);
  9417. #else
  9418. return_count = block_type_get_result_types(block_type, &return_types,
  9419. &return_reftype_maps,
  9420. &return_reftype_map_count);
  9421. param_count =
  9422. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9423. (void)param_count;
  9424. #endif
  9425. return_cell_num =
  9426. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9427. /* If the stack is in polymorphic state, just clear the stack
  9428. * and then re-push the values to make the stack top values
  9429. * match block type. */
  9430. if (block->is_stack_polymorphic) {
  9431. #if WASM_ENABLE_GC != 0
  9432. int32 j = return_reftype_map_count - 1;
  9433. #endif
  9434. for (i = (int32)return_count - 1; i >= 0; i--) {
  9435. #if WASM_ENABLE_GC != 0
  9436. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9437. bh_assert(return_reftype_maps[j].index == i + param_count);
  9438. bh_memcpy_s(
  9439. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9440. return_reftype_maps[j].ref_type,
  9441. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9442. j--;
  9443. }
  9444. #endif
  9445. #if WASM_ENABLE_FAST_INTERP != 0
  9446. POP_OFFSET_TYPE(return_types[i]);
  9447. #endif
  9448. POP_TYPE(return_types[i]);
  9449. }
  9450. /* Check stack is empty */
  9451. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9452. set_error_buf(
  9453. error_buf, error_buf_size,
  9454. "type mismatch: stack size does not match block type");
  9455. goto fail;
  9456. }
  9457. #if WASM_ENABLE_GC != 0
  9458. j = 0;
  9459. #endif
  9460. for (i = 0; i < (int32)return_count; i++) {
  9461. #if WASM_ENABLE_GC != 0
  9462. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9463. bh_assert(return_reftype_maps[j].index == i + param_count);
  9464. bh_memcpy_s(
  9465. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9466. return_reftype_maps[j].ref_type,
  9467. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9468. j++;
  9469. }
  9470. #endif
  9471. #if WASM_ENABLE_FAST_INTERP != 0
  9472. bool disable_emit = true;
  9473. int16 operand_offset = 0;
  9474. PUSH_OFFSET_TYPE(return_types[i]);
  9475. #endif
  9476. PUSH_TYPE(return_types[i]);
  9477. }
  9478. return true;
  9479. }
  9480. if (available_stack_cell != return_cell_num) {
  9481. #if WASM_ENABLE_EXCE_HANDLING != 0
  9482. /* testspec: this error message format is expected by try_catch.wast */
  9483. snprintf(
  9484. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9485. block->label_type == LABEL_TYPE_TRY
  9486. || (block->label_type == LABEL_TYPE_CATCH
  9487. && return_cell_num > 0)
  9488. ? "instruction"
  9489. : "block",
  9490. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9491. " but stack has ",
  9492. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9493. : "");
  9494. goto fail;
  9495. #else
  9496. set_error_buf(error_buf, error_buf_size,
  9497. "type mismatch: stack size does not match block type");
  9498. goto fail;
  9499. #endif
  9500. }
  9501. /* Check stack values match return types */
  9502. frame_ref = loader_ctx->frame_ref;
  9503. #if WASM_ENABLE_GC != 0
  9504. frame_reftype_map = loader_ctx->frame_reftype_map;
  9505. return_reftype_map =
  9506. return_reftype_map_count
  9507. ? return_reftype_maps + return_reftype_map_count - 1
  9508. : NULL;
  9509. #endif
  9510. for (i = (int32)return_count - 1; i >= 0; i--) {
  9511. uint8 type = return_types[i];
  9512. #if WASM_ENABLE_GC != 0
  9513. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9514. ref_type = NULL;
  9515. if (is_type_multi_byte) {
  9516. bh_assert(return_reftype_map);
  9517. ref_type = return_reftype_map->ref_type;
  9518. }
  9519. #endif
  9520. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9521. #if WASM_ENABLE_GC != 0
  9522. frame_reftype_map, available_reftype_map,
  9523. #endif
  9524. type,
  9525. #if WASM_ENABLE_GC != 0
  9526. ref_type,
  9527. #endif
  9528. error_buf, error_buf_size))
  9529. return false;
  9530. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9531. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9532. #if WASM_ENABLE_GC != 0
  9533. if (is_type_multi_byte) {
  9534. frame_reftype_map--;
  9535. available_reftype_map--;
  9536. return_reftype_map--;
  9537. }
  9538. #endif
  9539. }
  9540. return true;
  9541. fail:
  9542. return false;
  9543. }
  9544. #if WASM_ENABLE_FAST_INTERP != 0
  9545. /* Copy parameters to dynamic space.
  9546. * 1) POP original parameter out;
  9547. * 2) Push and copy original values to dynamic space.
  9548. * The copy instruction format:
  9549. * Part a: param count
  9550. * Part b: all param total cell num
  9551. * Part c: each param's cell_num, src offset and dst offset
  9552. * Part d: each param's src offset
  9553. * Part e: each param's dst offset
  9554. */
  9555. static bool
  9556. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9557. uint32 error_buf_size)
  9558. {
  9559. bool ret = false;
  9560. int16 *frame_offset = NULL;
  9561. uint8 *cells = NULL, cell;
  9562. int16 *src_offsets = NULL;
  9563. uint8 *emit_data = NULL;
  9564. uint32 i;
  9565. BranchBlock *block = loader_ctx->frame_csp - 1;
  9566. BlockType *block_type = &block->block_type;
  9567. WASMFuncType *wasm_type = block_type->u.type;
  9568. uint32 param_count = block_type->u.type->param_count;
  9569. int16 condition_offset = 0;
  9570. bool disable_emit = false;
  9571. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9572. int16 operand_offset = 0;
  9573. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9574. bh_assert(size > 0);
  9575. /* For if block, we also need copy the condition operand offset. */
  9576. if (is_if_block)
  9577. size += sizeof(*cells) + sizeof(*src_offsets);
  9578. /* Allocate memory for the emit data */
  9579. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9580. return false;
  9581. cells = emit_data;
  9582. src_offsets = (int16 *)(cells + param_count);
  9583. if (is_if_block)
  9584. condition_offset = *loader_ctx->frame_offset;
  9585. /* POP original parameter out */
  9586. for (i = 0; i < param_count; i++) {
  9587. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9588. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9589. }
  9590. frame_offset = loader_ctx->frame_offset;
  9591. /* Get each param's cell num and src offset */
  9592. for (i = 0; i < param_count; i++) {
  9593. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9594. cells[i] = cell;
  9595. src_offsets[i] = *frame_offset;
  9596. frame_offset += cell;
  9597. }
  9598. /* emit copy instruction */
  9599. emit_label(EXT_OP_COPY_STACK_VALUES);
  9600. /* Part a) */
  9601. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9602. /* Part b) */
  9603. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9604. : wasm_type->param_cell_num);
  9605. /* Part c) */
  9606. for (i = 0; i < param_count; i++)
  9607. emit_byte(loader_ctx, cells[i]);
  9608. if (is_if_block)
  9609. emit_byte(loader_ctx, 1);
  9610. /* Part d) */
  9611. for (i = 0; i < param_count; i++)
  9612. emit_operand(loader_ctx, src_offsets[i]);
  9613. if (is_if_block)
  9614. emit_operand(loader_ctx, condition_offset);
  9615. /* Since the start offset to save the block's params and
  9616. * the start offset to save the block's results may be
  9617. * different, we remember the dynamic offset for loop block
  9618. * so that we can use it to copy the stack operands to the
  9619. * loop block's params in wasm_loader_emit_br_info. */
  9620. if (block->label_type == LABEL_TYPE_LOOP)
  9621. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9622. /* Part e) */
  9623. /* Push to dynamic space. The push will emit the dst offset. */
  9624. for (i = 0; i < param_count; i++)
  9625. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9626. if (is_if_block)
  9627. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9628. ret = true;
  9629. fail:
  9630. /* Free the emit data */
  9631. wasm_runtime_free(emit_data);
  9632. return ret;
  9633. }
  9634. #endif
  9635. #if WASM_ENABLE_GC == 0
  9636. #define RESET_REFTYPE_MAP_STACK() (void)0
  9637. #else
  9638. #define RESET_REFTYPE_MAP_STACK() \
  9639. do { \
  9640. loader_ctx->reftype_map_num = \
  9641. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9642. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9643. + loader_ctx->reftype_map_num; \
  9644. } while (0)
  9645. #endif
  9646. /* reset the stack to the state of before entering the last block */
  9647. #if WASM_ENABLE_FAST_INTERP != 0
  9648. #define RESET_STACK() \
  9649. do { \
  9650. loader_ctx->stack_cell_num = \
  9651. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9652. loader_ctx->frame_ref = \
  9653. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9654. loader_ctx->frame_offset = \
  9655. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9656. RESET_REFTYPE_MAP_STACK(); \
  9657. } while (0)
  9658. #else
  9659. #define RESET_STACK() \
  9660. do { \
  9661. loader_ctx->stack_cell_num = \
  9662. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9663. loader_ctx->frame_ref = \
  9664. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9665. RESET_REFTYPE_MAP_STACK(); \
  9666. } while (0)
  9667. #endif
  9668. /* set current block's stack polymorphic state */
  9669. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9670. do { \
  9671. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9672. _cur_block->is_stack_polymorphic = flag; \
  9673. } while (0)
  9674. #define BLOCK_HAS_PARAM(block_type) \
  9675. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9676. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9677. do { \
  9678. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9679. error_buf_size))) { \
  9680. goto fail; \
  9681. } \
  9682. } while (0)
  9683. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9684. static bool
  9685. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9686. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9687. uint32 error_buf_size)
  9688. {
  9689. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9690. return false;
  9691. }
  9692. if (table_idx < module->import_table_count) {
  9693. if (p_elem_type)
  9694. *p_elem_type =
  9695. module->import_tables[table_idx].u.table.table_type.elem_type;
  9696. #if WASM_ENABLE_GC != 0
  9697. if (p_ref_type)
  9698. *((WASMRefType **)p_ref_type) =
  9699. module->import_tables[table_idx]
  9700. .u.table.table_type.elem_ref_type;
  9701. #endif
  9702. }
  9703. else {
  9704. if (p_elem_type)
  9705. *p_elem_type =
  9706. module->tables[module->import_table_count + table_idx]
  9707. .table_type.elem_type;
  9708. #if WASM_ENABLE_GC != 0
  9709. if (p_ref_type)
  9710. *((WASMRefType **)p_ref_type) =
  9711. module->tables[module->import_table_count + table_idx]
  9712. .table_type.elem_ref_type;
  9713. #endif
  9714. }
  9715. return true;
  9716. }
  9717. static bool
  9718. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9719. uint8 *p_elem_type, void **p_elem_ref_type,
  9720. char *error_buf, uint32 error_buf_size)
  9721. {
  9722. if (table_seg_idx >= module->table_seg_count) {
  9723. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9724. table_seg_idx);
  9725. return false;
  9726. }
  9727. if (p_elem_type) {
  9728. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9729. }
  9730. #if WASM_ENABLE_GC != 0
  9731. if (p_elem_ref_type)
  9732. *((WASMRefType **)p_elem_ref_type) =
  9733. module->table_segments[table_seg_idx].elem_ref_type;
  9734. #endif
  9735. return true;
  9736. }
  9737. #endif
  9738. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9739. const uint8 *
  9740. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9741. uint32 *len)
  9742. {
  9743. WASMCustomSection *section = module->custom_section_list;
  9744. while (section) {
  9745. if ((section->name_len == strlen(name))
  9746. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9747. if (len) {
  9748. *len = section->content_len;
  9749. }
  9750. return section->content_addr;
  9751. }
  9752. section = section->next;
  9753. }
  9754. return NULL;
  9755. }
  9756. #endif
  9757. #if 0
  9758. #define HANDLE_OPCODE(opcode) #opcode
  9759. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  9760. #undef HANDLE_OPCODE
  9761. #endif
  9762. static bool
  9763. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9764. uint32 cur_func_idx, char *error_buf,
  9765. uint32 error_buf_size)
  9766. {
  9767. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9768. uint32 param_count, local_count, global_count;
  9769. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type;
  9770. BlockType func_block_type;
  9771. uint16 *local_offsets, local_offset;
  9772. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9773. uint32 table_seg_idx, data_seg_idx, count, align, i;
  9774. mem_offset_t mem_offset;
  9775. int32 i32_const = 0;
  9776. int64 i64_const;
  9777. uint8 opcode;
  9778. bool return_value = false;
  9779. WASMLoaderContext *loader_ctx;
  9780. BranchBlock *frame_csp_tmp;
  9781. #if WASM_ENABLE_GC != 0
  9782. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9783. uint32 param_reftype_map_count, local_reftype_map_count;
  9784. int32 heap_type;
  9785. WASMRefType wasm_ref_type = { 0 };
  9786. bool need_ref_type_map;
  9787. #endif
  9788. #if WASM_ENABLE_FAST_INTERP != 0
  9789. uint8 *func_const_end, *func_const = NULL;
  9790. int16 operand_offset = 0;
  9791. uint8 last_op = 0;
  9792. bool disable_emit, preserve_local = false, if_condition_available = true;
  9793. float32 f32_const;
  9794. float64 f64_const;
  9795. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9796. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9797. #endif
  9798. #if WASM_ENABLE_MEMORY64 != 0
  9799. bool is_memory64 = has_module_memory64(module);
  9800. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  9801. #else
  9802. mem_offset_type = VALUE_TYPE_I32;
  9803. #endif
  9804. uint32 memidx;
  9805. global_count = module->import_global_count + module->global_count;
  9806. param_count = func->func_type->param_count;
  9807. param_types = func->func_type->types;
  9808. func_block_type.is_value_type = false;
  9809. func_block_type.u.type = func->func_type;
  9810. local_count = func->local_count;
  9811. local_types = func->local_types;
  9812. local_offsets = func->local_offsets;
  9813. #if WASM_ENABLE_GC != 0
  9814. param_reftype_maps = func->func_type->ref_type_maps;
  9815. param_reftype_map_count = func->func_type->ref_type_map_count;
  9816. local_reftype_maps = func->local_ref_type_maps;
  9817. local_reftype_map_count = func->local_ref_type_map_count;
  9818. #endif
  9819. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9820. goto fail;
  9821. }
  9822. #if WASM_ENABLE_GC != 0
  9823. loader_ctx->module = module;
  9824. loader_ctx->ref_type_set = module->ref_type_set;
  9825. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9826. #endif
  9827. #if WASM_ENABLE_FAST_INTERP != 0
  9828. /* For the first traverse, the initial value of preserved_local_offset has
  9829. * not been determined, we use the INT16_MAX to represent that a slot has
  9830. * been copied to preserve space. For second traverse, this field will be
  9831. * set to the appropriate value in wasm_loader_ctx_reinit.
  9832. * This is for Issue #1230,
  9833. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9834. * drop opcodes need to know which slots are preserved, so those slots will
  9835. * not be treated as dynamically allocated slots */
  9836. loader_ctx->preserved_local_offset = INT16_MAX;
  9837. re_scan:
  9838. if (loader_ctx->code_compiled_size > 0) {
  9839. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9840. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9841. goto fail;
  9842. }
  9843. p = func->code;
  9844. func->code_compiled = loader_ctx->p_code_compiled;
  9845. func->code_compiled_size = loader_ctx->code_compiled_size;
  9846. }
  9847. #endif
  9848. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9849. while (p < p_end) {
  9850. opcode = *p++;
  9851. #if WASM_ENABLE_FAST_INTERP != 0
  9852. p_org = p;
  9853. disable_emit = false;
  9854. emit_label(opcode);
  9855. #endif
  9856. switch (opcode) {
  9857. case WASM_OP_UNREACHABLE:
  9858. RESET_STACK();
  9859. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9860. break;
  9861. case WASM_OP_NOP:
  9862. #if WASM_ENABLE_FAST_INTERP != 0
  9863. skip_label();
  9864. #endif
  9865. break;
  9866. case WASM_OP_IF:
  9867. {
  9868. #if WASM_ENABLE_FAST_INTERP != 0
  9869. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9870. int32 available_stack_cell =
  9871. (int32)(loader_ctx->stack_cell_num
  9872. - parent_block->stack_cell_num);
  9873. if (available_stack_cell <= 0
  9874. && parent_block->is_stack_polymorphic)
  9875. if_condition_available = false;
  9876. else
  9877. if_condition_available = true;
  9878. PRESERVE_LOCAL_FOR_BLOCK();
  9879. #endif
  9880. #if WASM_ENABLE_GC == 0
  9881. POP_I32();
  9882. #endif
  9883. goto handle_op_block_and_loop;
  9884. }
  9885. case WASM_OP_BLOCK:
  9886. case WASM_OP_LOOP:
  9887. #if WASM_ENABLE_EXCE_HANDLING != 0
  9888. case WASM_OP_TRY:
  9889. if (opcode == WASM_OP_TRY) {
  9890. /*
  9891. * keep track of exception handlers to account for
  9892. * memory allocation
  9893. */
  9894. func->exception_handler_count++;
  9895. /*
  9896. * try is a block
  9897. * do nothing special, but execution continues to
  9898. * to handle_op_block_and_loop,
  9899. * and that be pushes the csp
  9900. */
  9901. }
  9902. #endif
  9903. #if WASM_ENABLE_FAST_INTERP != 0
  9904. PRESERVE_LOCAL_FOR_BLOCK();
  9905. #endif
  9906. handle_op_block_and_loop:
  9907. {
  9908. uint8 value_type;
  9909. BlockType block_type;
  9910. #if WASM_ENABLE_FAST_INTERP != 0
  9911. uint32 available_params = 0;
  9912. #endif
  9913. CHECK_BUF(p, p_end, 1);
  9914. value_type = read_uint8(p);
  9915. if (is_byte_a_type(value_type)) {
  9916. /* If the first byte is one of these special values:
  9917. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9918. * the single return value. */
  9919. block_type.is_value_type = true;
  9920. block_type.u.value_type.type = value_type;
  9921. #if WASM_ENABLE_WAMR_COMPILER != 0
  9922. if (value_type == VALUE_TYPE_V128)
  9923. module->is_simd_used = true;
  9924. else if (value_type == VALUE_TYPE_FUNCREF
  9925. || value_type == VALUE_TYPE_EXTERNREF)
  9926. module->is_ref_types_used = true;
  9927. #endif
  9928. #if WASM_ENABLE_GC != 0
  9929. if (value_type != VALUE_TYPE_VOID) {
  9930. p_org = p;
  9931. p--;
  9932. if (!resolve_value_type((const uint8 **)&p, p_end,
  9933. module, module->type_count,
  9934. &need_ref_type_map,
  9935. &wasm_ref_type, false,
  9936. error_buf, error_buf_size)) {
  9937. goto fail;
  9938. }
  9939. if (need_ref_type_map) {
  9940. block_type.u.value_type.ref_type_map.index = 0;
  9941. if (!(block_type.u.value_type.ref_type_map
  9942. .ref_type = reftype_set_insert(
  9943. module->ref_type_set, &wasm_ref_type,
  9944. error_buf, error_buf_size))) {
  9945. goto fail;
  9946. }
  9947. }
  9948. /* Set again as the type might be changed, e.g.
  9949. (ref null any) to anyref */
  9950. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9951. #if WASM_ENABLE_FAST_INTERP == 0
  9952. while (p_org < p) {
  9953. #if WASM_ENABLE_DEBUG_INTERP != 0
  9954. if (!record_fast_op(module, p_org, *p_org,
  9955. error_buf, error_buf_size)) {
  9956. goto fail;
  9957. }
  9958. #endif
  9959. /* Ignore extra bytes for interpreter */
  9960. *p_org++ = WASM_OP_NOP;
  9961. }
  9962. #endif
  9963. }
  9964. #endif /* end of WASM_ENABLE_GC != 0 */
  9965. }
  9966. else {
  9967. int32 type_index;
  9968. /* Resolve the leb128 encoded type index as block type */
  9969. p--;
  9970. p_org = p - 1;
  9971. read_leb_int32(p, p_end, type_index);
  9972. if ((uint32)type_index >= module->type_count) {
  9973. set_error_buf(error_buf, error_buf_size,
  9974. "unknown type");
  9975. goto fail;
  9976. }
  9977. block_type.is_value_type = false;
  9978. block_type.u.type =
  9979. (WASMFuncType *)module->types[type_index];
  9980. #if WASM_ENABLE_FAST_INTERP == 0
  9981. /* If block use type index as block type, change the opcode
  9982. * to new extended opcode so that interpreter can resolve
  9983. * the block quickly.
  9984. */
  9985. #if WASM_ENABLE_DEBUG_INTERP != 0
  9986. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9987. error_buf_size)) {
  9988. goto fail;
  9989. }
  9990. #endif
  9991. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  9992. #endif
  9993. }
  9994. #if WASM_ENABLE_GC != 0
  9995. if (opcode == WASM_OP_IF) {
  9996. POP_I32();
  9997. }
  9998. #endif
  9999. /* Pop block parameters from stack */
  10000. if (BLOCK_HAS_PARAM(block_type)) {
  10001. WASMFuncType *wasm_type = block_type.u.type;
  10002. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10003. #if WASM_ENABLE_FAST_INTERP != 0
  10004. uint32 cell_num;
  10005. available_params = block_type.u.type->param_count;
  10006. #endif
  10007. for (i = 0; i < block_type.u.type->param_count; i++) {
  10008. int32 available_stack_cell =
  10009. (int32)(loader_ctx->stack_cell_num
  10010. - cur_block->stack_cell_num);
  10011. if (available_stack_cell <= 0
  10012. && cur_block->is_stack_polymorphic) {
  10013. #if WASM_ENABLE_FAST_INTERP != 0
  10014. available_params = i;
  10015. #endif
  10016. break;
  10017. }
  10018. POP_TYPE(
  10019. wasm_type->types[wasm_type->param_count - i - 1]);
  10020. #if WASM_ENABLE_FAST_INTERP != 0
  10021. /* decrease the frame_offset pointer accordingly to keep
  10022. * consistent with frame_ref stack */
  10023. cell_num = wasm_value_type_cell_num(
  10024. wasm_type->types[wasm_type->param_count - i - 1]);
  10025. loader_ctx->frame_offset -= cell_num;
  10026. #endif
  10027. }
  10028. }
  10029. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10030. block_type, p);
  10031. /* Pass parameters to block */
  10032. if (BLOCK_HAS_PARAM(block_type)) {
  10033. WASMFuncType *func_type = block_type.u.type;
  10034. #if WASM_ENABLE_GC != 0
  10035. WASMRefType *ref_type;
  10036. uint32 j = 0;
  10037. #endif
  10038. for (i = 0; i < func_type->param_count; i++) {
  10039. #if WASM_ENABLE_FAST_INTERP != 0
  10040. uint32 cell_num =
  10041. wasm_value_type_cell_num(func_type->types[i]);
  10042. if (i >= available_params) {
  10043. /* If there isn't enough data on stack, push a dummy
  10044. * offset to keep the stack consistent with
  10045. * frame_ref.
  10046. * Since the stack is already in polymorphic state,
  10047. * the opcode will not be executed, so the dummy
  10048. * offset won't cause any error */
  10049. uint32 n;
  10050. for (n = 0; n < cell_num; n++) {
  10051. if (loader_ctx->p_code_compiled == NULL) {
  10052. if (!check_offset_push(loader_ctx,
  10053. error_buf,
  10054. error_buf_size))
  10055. goto fail;
  10056. }
  10057. *loader_ctx->frame_offset++ = 0;
  10058. }
  10059. }
  10060. else {
  10061. loader_ctx->frame_offset += cell_num;
  10062. }
  10063. #endif
  10064. #if WASM_ENABLE_GC != 0
  10065. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10066. bh_assert(func_type->ref_type_maps[j].index == i);
  10067. ref_type = func_type->ref_type_maps[j].ref_type;
  10068. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10069. ref_type,
  10070. wasm_reftype_struct_size(ref_type));
  10071. j++;
  10072. }
  10073. #endif
  10074. PUSH_TYPE(func_type->types[i]);
  10075. }
  10076. }
  10077. #if WASM_ENABLE_FAST_INTERP != 0
  10078. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10079. skip_label();
  10080. if (BLOCK_HAS_PARAM(block_type)) {
  10081. /* Make sure params are in dynamic space */
  10082. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10083. error_buf_size))
  10084. goto fail;
  10085. }
  10086. if (opcode == WASM_OP_LOOP) {
  10087. (loader_ctx->frame_csp - 1)->code_compiled =
  10088. loader_ctx->p_code_compiled;
  10089. }
  10090. }
  10091. #if WASM_ENABLE_EXCE_HANDLING != 0
  10092. else if (opcode == WASM_OP_TRY) {
  10093. skip_label();
  10094. }
  10095. #endif
  10096. else if (opcode == WASM_OP_IF) {
  10097. BranchBlock *block = loader_ctx->frame_csp - 1;
  10098. /* If block has parameters, we should make sure they are in
  10099. * dynamic space. Otherwise, when else branch is missing,
  10100. * the later opcode may consume incorrect operand offset.
  10101. * Spec case:
  10102. * (func (export "params-id") (param i32) (result i32)
  10103. * (i32.const 1)
  10104. * (i32.const 2)
  10105. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10106. * (then)) (i32.add)
  10107. * )
  10108. *
  10109. * So we should emit a copy instruction before the if.
  10110. *
  10111. * And we also need to save the parameter offsets and
  10112. * recover them before entering else branch.
  10113. *
  10114. */
  10115. if (BLOCK_HAS_PARAM(block_type)) {
  10116. uint64 size;
  10117. /* In polymorphic state, there may be no if condition on
  10118. * the stack, so the offset may not emitted */
  10119. if (if_condition_available) {
  10120. /* skip the if condition operand offset */
  10121. wasm_loader_emit_backspace(loader_ctx,
  10122. sizeof(int16));
  10123. }
  10124. /* skip the if label */
  10125. skip_label();
  10126. /* Emit a copy instruction */
  10127. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10128. error_buf_size))
  10129. goto fail;
  10130. /* Emit the if instruction */
  10131. emit_label(opcode);
  10132. /* Emit the new condition operand offset */
  10133. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10134. /* Save top param_count values of frame_offset stack, so
  10135. * that we can recover it before executing else branch
  10136. */
  10137. size = sizeof(int16)
  10138. * (uint64)block_type.u.type->param_cell_num;
  10139. if (!(block->param_frame_offsets = loader_malloc(
  10140. size, error_buf, error_buf_size)))
  10141. goto fail;
  10142. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10143. loader_ctx->frame_offset
  10144. - size / sizeof(int16),
  10145. (uint32)size);
  10146. }
  10147. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10148. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10149. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10150. }
  10151. #endif
  10152. break;
  10153. }
  10154. #if WASM_ENABLE_EXCE_HANDLING != 0
  10155. case WASM_OP_THROW:
  10156. {
  10157. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10158. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10159. uint8 label_type = cur_block->label_type;
  10160. uint32 tag_index = 0;
  10161. read_leb_int32(p, p_end, tag_index);
  10162. /* check validity of tag_index against module->tag_count */
  10163. /* check tag index is within the tag index space */
  10164. if (tag_index >= module->import_tag_count + module->tag_count) {
  10165. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10166. tag_index);
  10167. goto fail;
  10168. }
  10169. /* the tag_type is stored in either the WASMTag (section tags)
  10170. * or WASMTagImport (import tag) */
  10171. WASMFuncType *tag_type = NULL;
  10172. if (tag_index < module->import_tag_count) {
  10173. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10174. }
  10175. else {
  10176. tag_type =
  10177. module->tags[tag_index - module->import_tag_count]
  10178. ->tag_type;
  10179. }
  10180. if (tag_type->result_count != 0) {
  10181. set_error_buf(error_buf, error_buf_size,
  10182. "tag type signature does not return void");
  10183. goto fail;
  10184. }
  10185. int32 available_stack_cell =
  10186. (int32)(loader_ctx->stack_cell_num
  10187. - cur_block->stack_cell_num);
  10188. int32 tti;
  10189. /* Check stack values match return types by comparing tag param
  10190. * types with stack cells */
  10191. uint8 *frame_ref = loader_ctx->frame_ref;
  10192. #if WASM_ENABLE_GC != 0
  10193. WASMRefTypeMap *frame_reftype_map =
  10194. loader_ctx->frame_reftype_map;
  10195. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10196. /* Temporarily set these values since they may be used in
  10197. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10198. param_reftype_maps = tag_type->ref_type_maps;
  10199. /* For tag_type function, it shouldn't have result_count = 0 */
  10200. param_reftype_map_count = tag_type->ref_type_map_count;
  10201. param_count = tag_type->param_count;
  10202. #endif
  10203. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10204. #if WASM_ENABLE_GC != 0
  10205. local_type = tag_type->types[tti];
  10206. local_idx = tti;
  10207. /* Get the wasm_ref_type if the local_type is multibyte
  10208. type */
  10209. GET_LOCAL_REFTYPE();
  10210. #endif
  10211. if (!check_stack_top_values(
  10212. loader_ctx, frame_ref, available_stack_cell,
  10213. #if WASM_ENABLE_GC != 0
  10214. frame_reftype_map, frame_reftype_map_num,
  10215. #endif
  10216. tag_type->types[tti],
  10217. #if WASM_ENABLE_GC != 0
  10218. &wasm_ref_type,
  10219. #endif
  10220. error_buf, error_buf_size)) {
  10221. snprintf(error_buf, error_buf_size,
  10222. "type mismatch: instruction requires [%s] but "
  10223. "stack has [%s]",
  10224. tag_type->param_count > 0
  10225. ? type2str(tag_type->types[tti])
  10226. : "",
  10227. available_stack_cell > 0
  10228. ? type2str(*(loader_ctx->frame_ref - 1))
  10229. : "");
  10230. goto fail;
  10231. }
  10232. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10233. available_stack_cell -=
  10234. wasm_value_type_cell_num(tag_type->types[tti]);
  10235. }
  10236. #if WASM_ENABLE_GC != 0
  10237. /* Restore the values */
  10238. param_reftype_maps = func->func_type->ref_type_maps;
  10239. param_reftype_map_count = func->func_type->ref_type_map_count;
  10240. param_count = func->func_type->param_count;
  10241. #endif
  10242. /* throw is stack polymorphic */
  10243. (void)label_type;
  10244. RESET_STACK();
  10245. break;
  10246. }
  10247. case WASM_OP_RETHROW:
  10248. {
  10249. /* must be done before checking branch block */
  10250. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10251. /* check the target catching block: LABEL_TYPE_CATCH */
  10252. if (!(frame_csp_tmp =
  10253. check_branch_block(loader_ctx, &p, p_end, opcode,
  10254. error_buf, error_buf_size)))
  10255. goto fail;
  10256. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10257. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10258. /* trap according to spectest (rethrow.wast) */
  10259. set_error_buf(error_buf, error_buf_size,
  10260. "invalid rethrow label");
  10261. goto fail;
  10262. }
  10263. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10264. uint8 label_type = cur_block->label_type;
  10265. (void)label_type;
  10266. /* rethrow is stack polymorphic */
  10267. RESET_STACK();
  10268. break;
  10269. }
  10270. case WASM_OP_DELEGATE:
  10271. {
  10272. /* check target block is valid */
  10273. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10274. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10275. goto fail;
  10276. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10277. uint8 label_type = cur_block->label_type;
  10278. (void)label_type;
  10279. /* DELEGATE ends the block */
  10280. POP_CSP();
  10281. break;
  10282. }
  10283. case WASM_OP_CATCH:
  10284. {
  10285. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10286. uint8 label_type = cur_block->label_type;
  10287. uint32 tag_index = 0;
  10288. read_leb_int32(p, p_end, tag_index);
  10289. /* check validity of tag_index against module->tag_count */
  10290. /* check tag index is within the tag index space */
  10291. if (tag_index >= module->import_tag_count + module->tag_count) {
  10292. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10293. __FUNCTION__);
  10294. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10295. goto fail;
  10296. }
  10297. /* the tag_type is stored in either the WASMTag (section tags)
  10298. * or WASMTagImport (import tag) */
  10299. WASMFuncType *func_type = NULL;
  10300. if (tag_index < module->import_tag_count) {
  10301. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10302. }
  10303. else {
  10304. func_type =
  10305. module->tags[tag_index - module->import_tag_count]
  10306. ->tag_type;
  10307. }
  10308. if (func_type->result_count != 0) {
  10309. set_error_buf(error_buf, error_buf_size,
  10310. "tag type signature does not return void");
  10311. goto fail;
  10312. }
  10313. /* check validity of current label (expect LABEL_TYPE_TRY or
  10314. * LABEL_TYPE_CATCH) */
  10315. if ((LABEL_TYPE_CATCH != label_type)
  10316. && (LABEL_TYPE_TRY != label_type)) {
  10317. set_error_buf(error_buf, error_buf_size,
  10318. "Unexpected block sequence encountered.");
  10319. goto fail;
  10320. }
  10321. /*
  10322. * replace frame_csp by LABEL_TYPE_CATCH
  10323. */
  10324. cur_block->label_type = LABEL_TYPE_CATCH;
  10325. /* RESET_STACK removes the values pushed in TRY or pervious
  10326. * CATCH Blocks */
  10327. RESET_STACK();
  10328. #if WASM_ENABLE_GC != 0
  10329. WASMRefType *ref_type;
  10330. uint32 j = 0;
  10331. #endif
  10332. /* push types on the stack according to caught type */
  10333. for (i = 0; i < func_type->param_count; i++) {
  10334. #if WASM_ENABLE_GC != 0
  10335. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10336. bh_assert(func_type->ref_type_maps[j].index == i);
  10337. ref_type = func_type->ref_type_maps[j].ref_type;
  10338. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10339. ref_type,
  10340. wasm_reftype_struct_size(ref_type));
  10341. j++;
  10342. }
  10343. #endif
  10344. PUSH_TYPE(func_type->types[i]);
  10345. }
  10346. break;
  10347. }
  10348. case WASM_OP_CATCH_ALL:
  10349. {
  10350. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10351. /* expecting a TRY or CATCH, anything else will be considered an
  10352. * error */
  10353. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10354. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10355. set_error_buf(error_buf, error_buf_size,
  10356. "Unexpected block sequence encountered.");
  10357. goto fail;
  10358. }
  10359. /* no immediates */
  10360. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10361. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10362. /* RESET_STACK removes the values pushed in TRY or pervious
  10363. * CATCH Blocks */
  10364. RESET_STACK();
  10365. /* catch_all has no tagtype and therefore no parameters */
  10366. break;
  10367. }
  10368. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10369. case WASM_OP_ELSE:
  10370. handle_op_else:
  10371. {
  10372. BranchBlock *block = NULL;
  10373. BlockType block_type;
  10374. if (loader_ctx->csp_num < 2
  10375. /* the matched if isn't found */
  10376. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10377. /* duplicated else is found */
  10378. || (loader_ctx->frame_csp - 1)->else_addr) {
  10379. set_error_buf(
  10380. error_buf, error_buf_size,
  10381. "opcode else found without matched opcode if");
  10382. goto fail;
  10383. }
  10384. block = loader_ctx->frame_csp - 1;
  10385. /* check whether if branch's stack matches its result type */
  10386. if (!check_block_stack(loader_ctx, block, error_buf,
  10387. error_buf_size))
  10388. goto fail;
  10389. block->else_addr = p - 1;
  10390. block_type = block->block_type;
  10391. #if WASM_ENABLE_GC != 0
  10392. if (!wasm_loader_init_local_use_masks(
  10393. loader_ctx, local_count, error_buf, error_buf_size)) {
  10394. goto fail;
  10395. }
  10396. #endif
  10397. #if WASM_ENABLE_FAST_INTERP != 0
  10398. /* if the result of if branch is in local or const area, add a
  10399. * copy op */
  10400. RESERVE_BLOCK_RET();
  10401. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10402. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10403. #endif
  10404. RESET_STACK();
  10405. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10406. /* Pass parameters to if-false branch */
  10407. if (BLOCK_HAS_PARAM(block_type)) {
  10408. for (i = 0; i < block_type.u.type->param_count; i++)
  10409. PUSH_TYPE(block_type.u.type->types[i]);
  10410. }
  10411. #if WASM_ENABLE_FAST_INTERP != 0
  10412. /* Recover top param_count values of frame_offset stack */
  10413. if (BLOCK_HAS_PARAM((block_type))) {
  10414. uint32 size;
  10415. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10416. bh_memcpy_s(loader_ctx->frame_offset, size,
  10417. block->param_frame_offsets, size);
  10418. loader_ctx->frame_offset += (size / sizeof(int16));
  10419. }
  10420. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10421. #endif
  10422. break;
  10423. }
  10424. case WASM_OP_END:
  10425. {
  10426. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10427. /* check whether block stack matches its result type */
  10428. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10429. error_buf_size))
  10430. goto fail;
  10431. /* if there is no else branch, make a virtual else opcode for
  10432. easier integrity check and to copy the correct results to
  10433. the block return address for fast-interp mode:
  10434. change if block from `if ... end` to `if ... else end` */
  10435. if (cur_block->label_type == LABEL_TYPE_IF
  10436. && !cur_block->else_addr) {
  10437. opcode = WASM_OP_ELSE;
  10438. p--;
  10439. #if WASM_ENABLE_FAST_INTERP != 0
  10440. p_org = p;
  10441. skip_label();
  10442. disable_emit = false;
  10443. emit_label(opcode);
  10444. #endif
  10445. goto handle_op_else;
  10446. }
  10447. POP_CSP();
  10448. #if WASM_ENABLE_FAST_INTERP != 0
  10449. skip_label();
  10450. /* copy the result to the block return address */
  10451. RESERVE_BLOCK_RET();
  10452. apply_label_patch(loader_ctx, 0, PATCH_END);
  10453. free_label_patch_list(loader_ctx->frame_csp);
  10454. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10455. int32 idx;
  10456. uint8 ret_type;
  10457. emit_label(WASM_OP_RETURN);
  10458. for (idx = (int32)func->func_type->result_count - 1;
  10459. idx >= 0; idx--) {
  10460. ret_type = *(func->func_type->types
  10461. + func->func_type->param_count + idx);
  10462. POP_OFFSET_TYPE(ret_type);
  10463. }
  10464. }
  10465. #endif
  10466. if (loader_ctx->csp_num > 0) {
  10467. loader_ctx->frame_csp->end_addr = p - 1;
  10468. }
  10469. else {
  10470. /* end of function block, function will return */
  10471. if (p < p_end) {
  10472. set_error_buf(error_buf, error_buf_size,
  10473. "section size mismatch");
  10474. goto fail;
  10475. }
  10476. }
  10477. break;
  10478. }
  10479. case WASM_OP_BR:
  10480. {
  10481. if (!(frame_csp_tmp =
  10482. check_branch_block(loader_ctx, &p, p_end, opcode,
  10483. error_buf, error_buf_size)))
  10484. goto fail;
  10485. RESET_STACK();
  10486. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10487. break;
  10488. }
  10489. case WASM_OP_BR_IF:
  10490. {
  10491. POP_I32();
  10492. if (!(frame_csp_tmp =
  10493. check_branch_block(loader_ctx, &p, p_end, opcode,
  10494. error_buf, error_buf_size)))
  10495. goto fail;
  10496. break;
  10497. }
  10498. case WASM_OP_BR_TABLE:
  10499. {
  10500. uint32 depth = 0, default_arity, arity = 0;
  10501. BranchBlock *target_block;
  10502. BlockType *target_block_type;
  10503. #if WASM_ENABLE_FAST_INTERP == 0
  10504. BrTableCache *br_table_cache = NULL;
  10505. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10506. uint32 j;
  10507. #endif
  10508. read_leb_uint32(p, p_end, count);
  10509. #if WASM_ENABLE_FAST_INTERP != 0
  10510. emit_uint32(loader_ctx, count);
  10511. #endif
  10512. POP_I32();
  10513. /* Get each depth and check it */
  10514. p_org = p;
  10515. for (i = 0; i <= count; i++) {
  10516. read_leb_uint32(p, p_end, depth);
  10517. bh_assert(loader_ctx->csp_num > 0);
  10518. if (loader_ctx->csp_num - 1 < depth) {
  10519. set_error_buf(error_buf, error_buf_size,
  10520. "unknown label, "
  10521. "unexpected end of section or function");
  10522. goto fail;
  10523. }
  10524. }
  10525. p = p_org;
  10526. /* Get the default block's arity */
  10527. target_block = loader_ctx->frame_csp - (depth + 1);
  10528. target_block_type = &target_block->block_type;
  10529. default_arity = block_type_get_arity(target_block_type,
  10530. target_block->label_type);
  10531. #if WASM_ENABLE_FAST_INTERP == 0
  10532. p_depth_begin = p_depth = p;
  10533. #endif
  10534. for (i = 0; i <= count; i++) {
  10535. p_org = p;
  10536. read_leb_uint32(p, p_end, depth);
  10537. p = p_org;
  10538. /* Get the target block's arity and check it */
  10539. target_block = loader_ctx->frame_csp - (depth + 1);
  10540. target_block_type = &target_block->block_type;
  10541. arity = block_type_get_arity(target_block_type,
  10542. target_block->label_type);
  10543. if (arity != default_arity) {
  10544. set_error_buf(error_buf, error_buf_size,
  10545. "type mismatch: br_table targets must "
  10546. "all use same result type");
  10547. goto fail;
  10548. }
  10549. if (!(frame_csp_tmp =
  10550. check_branch_block(loader_ctx, &p, p_end, opcode,
  10551. error_buf, error_buf_size))) {
  10552. goto fail;
  10553. }
  10554. #if WASM_ENABLE_FAST_INTERP == 0
  10555. if (br_table_cache) {
  10556. br_table_cache->br_depths[i] = depth;
  10557. }
  10558. else {
  10559. if (depth > 255) {
  10560. /* The depth cannot be stored in one byte,
  10561. create br_table cache to store each depth */
  10562. #if WASM_ENABLE_DEBUG_INTERP != 0
  10563. if (!record_fast_op(module, p_opcode, *p_opcode,
  10564. error_buf, error_buf_size)) {
  10565. goto fail;
  10566. }
  10567. #endif
  10568. if (!(br_table_cache = loader_malloc(
  10569. offsetof(BrTableCache, br_depths)
  10570. + sizeof(uint32)
  10571. * (uint64)(count + 1),
  10572. error_buf, error_buf_size))) {
  10573. goto fail;
  10574. }
  10575. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10576. br_table_cache->br_table_op_addr = p_opcode;
  10577. br_table_cache->br_count = count;
  10578. /* Copy previous depths which are one byte */
  10579. for (j = 0; j < i; j++) {
  10580. br_table_cache->br_depths[j] = p_depth_begin[j];
  10581. }
  10582. br_table_cache->br_depths[i] = depth;
  10583. bh_list_insert(module->br_table_cache_list,
  10584. br_table_cache);
  10585. }
  10586. else {
  10587. /* The depth can be stored in one byte, use the
  10588. byte of the leb to store it */
  10589. *p_depth++ = (uint8)depth;
  10590. }
  10591. }
  10592. #endif
  10593. }
  10594. #if WASM_ENABLE_FAST_INTERP == 0
  10595. /* Set the tailing bytes to nop */
  10596. if (br_table_cache)
  10597. p_depth = p_depth_begin;
  10598. while (p_depth < p)
  10599. *p_depth++ = WASM_OP_NOP;
  10600. #endif
  10601. RESET_STACK();
  10602. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10603. break;
  10604. }
  10605. case WASM_OP_RETURN:
  10606. {
  10607. WASMFuncType *func_type = func->func_type;
  10608. int32 idx;
  10609. uint8 ret_type;
  10610. #if WASM_ENABLE_GC != 0
  10611. uint32 j = func_type->ref_type_map_count - 1;
  10612. #endif
  10613. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  10614. idx--) {
  10615. ret_type =
  10616. *(func_type->types + func_type->param_count + idx);
  10617. #if WASM_ENABLE_GC != 0
  10618. if (wasm_is_type_multi_byte_type(ret_type)) {
  10619. WASMRefType *ref_type =
  10620. func_type->ref_type_maps[j].ref_type;
  10621. bh_assert(func_type->ref_type_maps[j].index
  10622. == func_type->param_count + idx);
  10623. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10624. ref_type,
  10625. wasm_reftype_struct_size(ref_type));
  10626. j--;
  10627. }
  10628. #endif
  10629. #if WASM_ENABLE_FAST_INTERP != 0
  10630. /* emit the offset after return opcode */
  10631. POP_OFFSET_TYPE(ret_type);
  10632. #endif
  10633. POP_TYPE(ret_type);
  10634. }
  10635. RESET_STACK();
  10636. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10637. break;
  10638. }
  10639. case WASM_OP_CALL:
  10640. #if WASM_ENABLE_TAIL_CALL != 0
  10641. case WASM_OP_RETURN_CALL:
  10642. #endif
  10643. #if WASM_ENABLE_GC != 0
  10644. case WASM_OP_CALL_REF:
  10645. case WASM_OP_RETURN_CALL_REF:
  10646. #endif
  10647. {
  10648. WASMFuncType *func_type;
  10649. uint8 type;
  10650. int32 idx;
  10651. #if WASM_ENABLE_GC != 0
  10652. WASMRefType *ref_type;
  10653. uint32 type_idx1;
  10654. int32 j;
  10655. #endif
  10656. #if WASM_ENABLE_GC != 0
  10657. if (opcode == WASM_OP_CALL_REF
  10658. || opcode == WASM_OP_RETURN_CALL_REF) {
  10659. read_leb_uint32(p, p_end, type_idx1);
  10660. if (!check_type_index(module, module->type_count, type_idx1,
  10661. error_buf, error_buf_size)) {
  10662. goto fail;
  10663. }
  10664. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  10665. set_error_buf(error_buf, error_buf_size,
  10666. "unkown function type");
  10667. goto fail;
  10668. }
  10669. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10670. &type_idx, error_buf,
  10671. error_buf_size)) {
  10672. goto fail;
  10673. }
  10674. if (type == VALUE_TYPE_ANY) {
  10675. type_idx = type_idx1;
  10676. }
  10677. if (!check_type_index(module, module->type_count, type_idx,
  10678. error_buf, error_buf_size)) {
  10679. goto fail;
  10680. }
  10681. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10682. set_error_buf(error_buf, error_buf_size,
  10683. "unkown function type");
  10684. goto fail;
  10685. }
  10686. if (!wasm_func_type_is_super_of(
  10687. (WASMFuncType *)module->types[type_idx1],
  10688. (WASMFuncType *)module->types[type_idx])) {
  10689. set_error_buf(error_buf, error_buf_size,
  10690. "function type mismatch");
  10691. goto fail;
  10692. }
  10693. func_type = (WASMFuncType *)module->types[type_idx];
  10694. }
  10695. else
  10696. #endif
  10697. {
  10698. read_leb_uint32(p, p_end, func_idx);
  10699. #if WASM_ENABLE_FAST_INTERP != 0
  10700. /* we need to emit func_idx before arguments */
  10701. emit_uint32(loader_ctx, func_idx);
  10702. #endif
  10703. if (!check_function_index(module, func_idx, error_buf,
  10704. error_buf_size)) {
  10705. goto fail;
  10706. }
  10707. if (func_idx < module->import_function_count)
  10708. func_type = module->import_functions[func_idx]
  10709. .u.function.func_type;
  10710. else
  10711. func_type =
  10712. module
  10713. ->functions[func_idx
  10714. - module->import_function_count]
  10715. ->func_type;
  10716. }
  10717. if (func_type->param_count > 0) {
  10718. #if WASM_ENABLE_GC != 0
  10719. j = (int32)(func_type->result_ref_type_maps
  10720. - func_type->ref_type_maps - 1);
  10721. #endif
  10722. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10723. idx--) {
  10724. #if WASM_ENABLE_GC != 0
  10725. if (wasm_is_type_multi_byte_type(
  10726. func_type->types[idx])) {
  10727. ref_type = func_type->ref_type_maps[j].ref_type;
  10728. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10729. ref_type,
  10730. wasm_reftype_struct_size(ref_type));
  10731. j--;
  10732. }
  10733. #endif
  10734. #if WASM_ENABLE_FAST_INTERP != 0
  10735. POP_OFFSET_TYPE(func_type->types[idx]);
  10736. #endif
  10737. POP_TYPE(func_type->types[idx]);
  10738. }
  10739. }
  10740. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10741. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10742. #endif
  10743. #if WASM_ENABLE_GC != 0
  10744. j = (int32)(func_type->result_ref_type_maps
  10745. - func_type->ref_type_maps);
  10746. #endif
  10747. for (i = 0; i < func_type->result_count; i++) {
  10748. #if WASM_ENABLE_GC != 0
  10749. if (wasm_is_type_multi_byte_type(
  10750. func_type->types[func_type->param_count + i])) {
  10751. ref_type = func_type->ref_type_maps[j].ref_type;
  10752. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10753. ref_type,
  10754. wasm_reftype_struct_size(ref_type));
  10755. j++;
  10756. }
  10757. #endif
  10758. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10759. #if WASM_ENABLE_FAST_INTERP != 0
  10760. /* Here we emit each return value's dynamic_offset. But
  10761. * in fact these offsets are continuous, so interpreter
  10762. * only need to get the first return value's offset.
  10763. */
  10764. PUSH_OFFSET_TYPE(
  10765. func_type->types[func_type->param_count + i]);
  10766. #endif
  10767. }
  10768. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10769. }
  10770. else {
  10771. #if WASM_ENABLE_GC == 0
  10772. if (func_type->result_count
  10773. != func->func_type->result_count) {
  10774. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10775. "type mismatch: expect ",
  10776. func->func_type->result_count,
  10777. " return values but got other");
  10778. goto fail;
  10779. }
  10780. for (i = 0; i < func_type->result_count; i++) {
  10781. type = func->func_type
  10782. ->types[func->func_type->param_count + i];
  10783. if (func_type->types[func_type->param_count + i]
  10784. != type) {
  10785. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10786. "type mismatch: expect ",
  10787. type2str(type), " but got other");
  10788. goto fail;
  10789. }
  10790. }
  10791. #else
  10792. if (!wasm_func_type_result_is_subtype_of(
  10793. func_type, func->func_type, module->types,
  10794. module->type_count)) {
  10795. set_error_buf(
  10796. error_buf, error_buf_size,
  10797. "type mismatch: invalid func result types");
  10798. goto fail;
  10799. }
  10800. #endif
  10801. RESET_STACK();
  10802. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10803. }
  10804. #endif
  10805. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10806. || WASM_ENABLE_WAMR_COMPILER != 0
  10807. func->has_op_func_call = true;
  10808. #endif
  10809. (void)type;
  10810. break;
  10811. }
  10812. /*
  10813. * if disable reference type: call_indirect typeidx, 0x00
  10814. * if enable reference type: call_indirect typeidx, tableidx
  10815. */
  10816. case WASM_OP_CALL_INDIRECT:
  10817. #if WASM_ENABLE_TAIL_CALL != 0
  10818. case WASM_OP_RETURN_CALL_INDIRECT:
  10819. #endif
  10820. {
  10821. int32 idx;
  10822. WASMFuncType *func_type;
  10823. read_leb_uint32(p, p_end, type_idx);
  10824. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10825. #if WASM_ENABLE_WAMR_COMPILER != 0
  10826. if (p + 1 < p_end && *p != 0x00) {
  10827. /*
  10828. * Any non-0x00 byte requires the ref types proposal.
  10829. * This is different from checking the table_idx value
  10830. * since `0x80 0x00` etc. are all valid encodings of zero.
  10831. */
  10832. module->is_ref_types_used = true;
  10833. }
  10834. #endif
  10835. read_leb_uint32(p, p_end, table_idx);
  10836. #else
  10837. CHECK_BUF(p, p_end, 1);
  10838. table_idx = read_uint8(p);
  10839. #endif
  10840. if (!check_table_index(module, table_idx, error_buf,
  10841. error_buf_size)) {
  10842. goto fail;
  10843. }
  10844. #if WASM_ENABLE_FAST_INTERP != 0
  10845. /* we need to emit before arguments */
  10846. #if WASM_ENABLE_TAIL_CALL != 0
  10847. emit_byte(loader_ctx, opcode);
  10848. #endif
  10849. emit_uint32(loader_ctx, type_idx);
  10850. emit_uint32(loader_ctx, table_idx);
  10851. #endif
  10852. /* skip elem idx */
  10853. POP_I32();
  10854. if (type_idx >= module->type_count) {
  10855. set_error_buf(error_buf, error_buf_size, "unknown type");
  10856. goto fail;
  10857. }
  10858. func_type = (WASMFuncType *)module->types[type_idx];
  10859. if (func_type->param_count > 0) {
  10860. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10861. idx--) {
  10862. #if WASM_ENABLE_FAST_INTERP != 0
  10863. POP_OFFSET_TYPE(func_type->types[idx]);
  10864. #endif
  10865. POP_TYPE(func_type->types[idx]);
  10866. }
  10867. }
  10868. #if WASM_ENABLE_TAIL_CALL != 0
  10869. if (opcode == WASM_OP_CALL_INDIRECT) {
  10870. #endif
  10871. for (i = 0; i < func_type->result_count; i++) {
  10872. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10873. #if WASM_ENABLE_FAST_INTERP != 0
  10874. PUSH_OFFSET_TYPE(
  10875. func_type->types[func_type->param_count + i]);
  10876. #endif
  10877. }
  10878. #if WASM_ENABLE_TAIL_CALL != 0
  10879. }
  10880. else {
  10881. uint8 type;
  10882. if (func_type->result_count
  10883. != func->func_type->result_count) {
  10884. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10885. "type mismatch: expect ",
  10886. func->func_type->result_count,
  10887. " return values but got other");
  10888. goto fail;
  10889. }
  10890. for (i = 0; i < func_type->result_count; i++) {
  10891. type = func->func_type
  10892. ->types[func->func_type->param_count + i];
  10893. if (func_type->types[func_type->param_count + i]
  10894. != type) {
  10895. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10896. "type mismatch: expect ",
  10897. type2str(type), " but got other");
  10898. goto fail;
  10899. }
  10900. }
  10901. RESET_STACK();
  10902. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10903. }
  10904. #endif
  10905. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10906. || WASM_ENABLE_WAMR_COMPILER != 0
  10907. func->has_op_func_call = true;
  10908. #endif
  10909. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10910. func->has_op_call_indirect = true;
  10911. #endif
  10912. break;
  10913. }
  10914. case WASM_OP_DROP:
  10915. {
  10916. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10917. int32 available_stack_cell =
  10918. (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, opcode drop was found "
  10924. "but stack was empty");
  10925. goto fail;
  10926. }
  10927. if (available_stack_cell > 0) {
  10928. #if WASM_ENABLE_GC != 0
  10929. if (wasm_is_type_multi_byte_type(
  10930. *(loader_ctx->frame_ref - 1))) {
  10931. bh_assert((int32)(loader_ctx->reftype_map_num
  10932. - cur_block->reftype_map_num)
  10933. > 0);
  10934. loader_ctx->frame_reftype_map--;
  10935. loader_ctx->reftype_map_num--;
  10936. }
  10937. #endif
  10938. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  10939. loader_ctx->frame_ref--;
  10940. loader_ctx->stack_cell_num--;
  10941. #if WASM_ENABLE_FAST_INTERP != 0
  10942. skip_label();
  10943. loader_ctx->frame_offset--;
  10944. if ((*(loader_ctx->frame_offset)
  10945. > loader_ctx->start_dynamic_offset)
  10946. && (*(loader_ctx->frame_offset)
  10947. < loader_ctx->max_dynamic_offset))
  10948. loader_ctx->dynamic_offset--;
  10949. #endif
  10950. }
  10951. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10952. loader_ctx->frame_ref -= 2;
  10953. loader_ctx->stack_cell_num -= 2;
  10954. #if WASM_ENABLE_FAST_INTERP == 0
  10955. *(p - 1) = WASM_OP_DROP_64;
  10956. #endif
  10957. #if WASM_ENABLE_FAST_INTERP != 0
  10958. skip_label();
  10959. loader_ctx->frame_offset -= 2;
  10960. if ((*(loader_ctx->frame_offset)
  10961. > loader_ctx->start_dynamic_offset)
  10962. && (*(loader_ctx->frame_offset)
  10963. < loader_ctx->max_dynamic_offset))
  10964. loader_ctx->dynamic_offset -= 2;
  10965. #endif
  10966. }
  10967. #if WASM_ENABLE_SIMD != 0
  10968. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10969. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10970. loader_ctx->frame_ref -= 4;
  10971. loader_ctx->stack_cell_num -= 4;
  10972. }
  10973. #endif
  10974. #endif
  10975. else {
  10976. set_error_buf(error_buf, error_buf_size,
  10977. "type mismatch");
  10978. goto fail;
  10979. }
  10980. }
  10981. else {
  10982. #if WASM_ENABLE_FAST_INTERP != 0
  10983. skip_label();
  10984. #endif
  10985. }
  10986. break;
  10987. }
  10988. case WASM_OP_SELECT:
  10989. {
  10990. uint8 ref_type;
  10991. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10992. int32 available_stack_cell;
  10993. #if WASM_ENABLE_FAST_INTERP != 0
  10994. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10995. #endif
  10996. POP_I32();
  10997. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  10998. - cur_block->stack_cell_num);
  10999. if (available_stack_cell <= 0
  11000. && !cur_block->is_stack_polymorphic) {
  11001. set_error_buf(error_buf, error_buf_size,
  11002. "type mismatch or invalid result arity, "
  11003. "opcode select was found "
  11004. "but stack was empty");
  11005. goto fail;
  11006. }
  11007. if (available_stack_cell > 0) {
  11008. switch (*(loader_ctx->frame_ref - 1)) {
  11009. case VALUE_TYPE_I32:
  11010. case VALUE_TYPE_F32:
  11011. case VALUE_TYPE_ANY:
  11012. break;
  11013. case VALUE_TYPE_I64:
  11014. case VALUE_TYPE_F64:
  11015. #if WASM_ENABLE_FAST_INTERP == 0
  11016. *(p - 1) = WASM_OP_SELECT_64;
  11017. #endif
  11018. #if WASM_ENABLE_FAST_INTERP != 0
  11019. if (loader_ctx->p_code_compiled) {
  11020. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11021. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11022. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11023. *(void **)(p_code_compiled_tmp
  11024. - sizeof(void *)) =
  11025. handle_table[opcode_tmp];
  11026. #else
  11027. #if UINTPTR_MAX == UINT64_MAX
  11028. /* emit int32 relative offset in 64-bit target
  11029. */
  11030. int32 offset =
  11031. (int32)((uint8 *)handle_table[opcode_tmp]
  11032. - (uint8 *)handle_table[0]);
  11033. *(int32 *)(p_code_compiled_tmp
  11034. - sizeof(int32)) = offset;
  11035. #else
  11036. /* emit uint32 label address in 32-bit target */
  11037. *(uint32 *)(p_code_compiled_tmp
  11038. - sizeof(uint32)) =
  11039. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11040. #endif
  11041. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11042. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11043. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11044. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11045. #else
  11046. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11047. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11048. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11049. }
  11050. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11051. break;
  11052. #if WASM_ENABLE_SIMD != 0
  11053. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  11054. case VALUE_TYPE_V128:
  11055. break;
  11056. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  11057. #endif /* WASM_ENABLE_SIMD != 0 */
  11058. default:
  11059. {
  11060. set_error_buf(error_buf, error_buf_size,
  11061. "type mismatch");
  11062. goto fail;
  11063. }
  11064. }
  11065. ref_type = *(loader_ctx->frame_ref - 1);
  11066. #if WASM_ENABLE_FAST_INTERP != 0
  11067. POP_OFFSET_TYPE(ref_type);
  11068. POP_TYPE(ref_type);
  11069. POP_OFFSET_TYPE(ref_type);
  11070. POP_TYPE(ref_type);
  11071. PUSH_OFFSET_TYPE(ref_type);
  11072. PUSH_TYPE(ref_type);
  11073. #else
  11074. POP2_AND_PUSH(ref_type, ref_type);
  11075. #endif
  11076. }
  11077. else {
  11078. #if WASM_ENABLE_FAST_INTERP != 0
  11079. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11080. #endif
  11081. PUSH_TYPE(VALUE_TYPE_ANY);
  11082. }
  11083. break;
  11084. }
  11085. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11086. case WASM_OP_SELECT_T:
  11087. {
  11088. uint8 vec_len, type;
  11089. #if WASM_ENABLE_GC != 0
  11090. WASMRefType *ref_type = NULL;
  11091. #endif
  11092. #if WASM_ENABLE_FAST_INTERP != 0
  11093. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11094. #endif
  11095. read_leb_uint32(p, p_end, vec_len);
  11096. if (vec_len != 1) {
  11097. /* typed select must have exactly one result */
  11098. set_error_buf(error_buf, error_buf_size,
  11099. "invalid result arity");
  11100. goto fail;
  11101. }
  11102. #if WASM_ENABLE_GC == 0
  11103. CHECK_BUF(p, p_end, 1);
  11104. type = read_uint8(p);
  11105. if (!is_valid_value_type_for_interpreter(type)) {
  11106. set_error_buf(error_buf, error_buf_size,
  11107. "unknown value type");
  11108. goto fail;
  11109. }
  11110. #else
  11111. p_org = p + 1;
  11112. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11113. module->type_count, &need_ref_type_map,
  11114. &wasm_ref_type, false, error_buf,
  11115. error_buf_size)) {
  11116. goto fail;
  11117. }
  11118. type = wasm_ref_type.ref_type;
  11119. if (need_ref_type_map) {
  11120. if (!(ref_type = reftype_set_insert(
  11121. module->ref_type_set, &wasm_ref_type, error_buf,
  11122. error_buf_size))) {
  11123. goto fail;
  11124. }
  11125. }
  11126. #if WASM_ENABLE_FAST_INTERP == 0
  11127. while (p_org < p) {
  11128. #if WASM_ENABLE_DEBUG_INTERP != 0
  11129. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11130. error_buf_size)) {
  11131. goto fail;
  11132. }
  11133. #endif
  11134. /* Ignore extra bytes for interpreter */
  11135. *p_org++ = WASM_OP_NOP;
  11136. }
  11137. #endif
  11138. #endif /* end of WASM_ENABLE_GC == 0 */
  11139. POP_I32();
  11140. #if WASM_ENABLE_FAST_INTERP != 0
  11141. if (loader_ctx->p_code_compiled) {
  11142. uint8 opcode_tmp = WASM_OP_SELECT;
  11143. if (type == VALUE_TYPE_V128) {
  11144. #if (WASM_ENABLE_SIMD == 0) \
  11145. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  11146. set_error_buf(error_buf, error_buf_size,
  11147. "SIMD v128 type isn't supported");
  11148. goto fail;
  11149. #endif
  11150. }
  11151. else {
  11152. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11153. opcode_tmp = WASM_OP_SELECT_64;
  11154. #if WASM_ENABLE_GC != 0
  11155. if (wasm_is_type_reftype(type))
  11156. opcode_tmp = WASM_OP_SELECT_T;
  11157. #endif
  11158. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11159. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11160. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11161. handle_table[opcode_tmp];
  11162. #else
  11163. #if UINTPTR_MAX == UINT64_MAX
  11164. /* emit int32 relative offset in 64-bit target */
  11165. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11166. - (uint8 *)handle_table[0]);
  11167. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11168. offset;
  11169. #else
  11170. /* emit uint32 label address in 32-bit target */
  11171. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11172. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11173. #endif
  11174. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11175. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11176. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11177. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11178. #else
  11179. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11180. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11181. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11182. }
  11183. }
  11184. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11185. POP_REF(type);
  11186. #if WASM_ENABLE_GC != 0
  11187. if (need_ref_type_map) {
  11188. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11189. wasm_reftype_struct_size(ref_type));
  11190. }
  11191. #endif
  11192. POP_REF(type);
  11193. #if WASM_ENABLE_GC != 0
  11194. if (need_ref_type_map) {
  11195. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11196. wasm_reftype_struct_size(ref_type));
  11197. }
  11198. #endif
  11199. PUSH_REF(type);
  11200. #if WASM_ENABLE_WAMR_COMPILER != 0
  11201. module->is_ref_types_used = true;
  11202. #endif
  11203. (void)vec_len;
  11204. break;
  11205. }
  11206. /* table.get x. tables[x]. [i32] -> [t] */
  11207. /* table.set x. tables[x]. [i32 t] -> [] */
  11208. case WASM_OP_TABLE_GET:
  11209. case WASM_OP_TABLE_SET:
  11210. {
  11211. uint8 decl_ref_type;
  11212. #if WASM_ENABLE_GC != 0
  11213. WASMRefType *ref_type;
  11214. #endif
  11215. read_leb_uint32(p, p_end, table_idx);
  11216. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11217. #if WASM_ENABLE_GC != 0
  11218. (void **)&ref_type,
  11219. #else
  11220. NULL,
  11221. #endif
  11222. error_buf, error_buf_size))
  11223. goto fail;
  11224. #if WASM_ENABLE_GC != 0
  11225. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11226. bh_assert(ref_type);
  11227. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11228. ref_type, wasm_reftype_struct_size(ref_type));
  11229. }
  11230. #endif
  11231. #if WASM_ENABLE_FAST_INTERP != 0
  11232. emit_uint32(loader_ctx, table_idx);
  11233. #endif
  11234. if (opcode == WASM_OP_TABLE_GET) {
  11235. POP_I32();
  11236. #if WASM_ENABLE_FAST_INTERP != 0
  11237. PUSH_OFFSET_TYPE(decl_ref_type);
  11238. #endif
  11239. PUSH_TYPE(decl_ref_type);
  11240. }
  11241. else {
  11242. #if WASM_ENABLE_FAST_INTERP != 0
  11243. POP_OFFSET_TYPE(decl_ref_type);
  11244. #endif
  11245. POP_TYPE(decl_ref_type);
  11246. POP_I32();
  11247. }
  11248. #if WASM_ENABLE_WAMR_COMPILER != 0
  11249. module->is_ref_types_used = true;
  11250. #endif
  11251. break;
  11252. }
  11253. case WASM_OP_REF_NULL:
  11254. {
  11255. uint8 ref_type;
  11256. #if WASM_ENABLE_GC == 0
  11257. CHECK_BUF(p, p_end, 1);
  11258. ref_type = read_uint8(p);
  11259. if (ref_type != VALUE_TYPE_FUNCREF
  11260. && ref_type != VALUE_TYPE_EXTERNREF) {
  11261. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11262. goto fail;
  11263. }
  11264. #else
  11265. read_leb_int32(p, p_end, heap_type);
  11266. if (heap_type >= 0) {
  11267. if (!check_type_index(module, module->type_count, heap_type,
  11268. error_buf, error_buf_size)) {
  11269. goto fail;
  11270. }
  11271. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11272. true, heap_type);
  11273. ref_type = wasm_ref_type.ref_type;
  11274. }
  11275. else {
  11276. if (!wasm_is_valid_heap_type(heap_type)) {
  11277. set_error_buf(error_buf, error_buf_size,
  11278. "unknown type");
  11279. goto fail;
  11280. }
  11281. ref_type = (uint8)((int32)0x80 + heap_type);
  11282. }
  11283. #endif /* end of WASM_ENABLE_GC == 0 */
  11284. #if WASM_ENABLE_FAST_INTERP != 0
  11285. PUSH_OFFSET_TYPE(ref_type);
  11286. #endif
  11287. PUSH_TYPE(ref_type);
  11288. #if WASM_ENABLE_WAMR_COMPILER != 0
  11289. module->is_ref_types_used = true;
  11290. #endif
  11291. break;
  11292. }
  11293. case WASM_OP_REF_IS_NULL:
  11294. {
  11295. #if WASM_ENABLE_GC == 0
  11296. #if WASM_ENABLE_FAST_INTERP != 0
  11297. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11298. int32 block_stack_cell_num =
  11299. (int32)(loader_ctx->stack_cell_num
  11300. - cur_block->stack_cell_num);
  11301. if (block_stack_cell_num <= 0) {
  11302. if (!cur_block->is_stack_polymorphic) {
  11303. set_error_buf(
  11304. error_buf, error_buf_size,
  11305. "type mismatch: expect data but stack was empty");
  11306. goto fail;
  11307. }
  11308. }
  11309. else {
  11310. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11311. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11312. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11313. if (!wasm_loader_pop_frame_ref_offset(
  11314. loader_ctx, *(loader_ctx->frame_ref - 1),
  11315. error_buf, error_buf_size)) {
  11316. goto fail;
  11317. }
  11318. }
  11319. else {
  11320. set_error_buf(error_buf, error_buf_size,
  11321. "type mismatch");
  11322. goto fail;
  11323. }
  11324. }
  11325. #else
  11326. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11327. error_buf, error_buf_size)
  11328. && !wasm_loader_pop_frame_ref(loader_ctx,
  11329. VALUE_TYPE_EXTERNREF,
  11330. error_buf, error_buf_size)) {
  11331. goto fail;
  11332. }
  11333. #endif
  11334. #else /* else of WASM_ENABLE_GC == 0 */
  11335. uint8 type;
  11336. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11337. error_buf, error_buf_size)) {
  11338. goto fail;
  11339. }
  11340. #endif
  11341. PUSH_I32();
  11342. #if WASM_ENABLE_WAMR_COMPILER != 0
  11343. module->is_ref_types_used = true;
  11344. #endif
  11345. break;
  11346. }
  11347. case WASM_OP_REF_FUNC:
  11348. {
  11349. read_leb_uint32(p, p_end, func_idx);
  11350. if (!check_function_index(module, func_idx, error_buf,
  11351. error_buf_size)) {
  11352. goto fail;
  11353. }
  11354. /* Refer to a forward-declared function:
  11355. the function must be an import, exported, or present in
  11356. a table elem segment or global initializer to be used as
  11357. the operand to ref.func */
  11358. if (func_idx >= module->import_function_count) {
  11359. WASMTableSeg *table_seg = module->table_segments;
  11360. bool func_declared = false;
  11361. uint32 j;
  11362. for (i = 0; i < module->global_count; i++) {
  11363. if (module->globals[i].type.val_type
  11364. == VALUE_TYPE_FUNCREF
  11365. && module->globals[i].init_expr.init_expr_type
  11366. == INIT_EXPR_TYPE_FUNCREF_CONST
  11367. && module->globals[i].init_expr.u.u32 == func_idx) {
  11368. func_declared = true;
  11369. break;
  11370. }
  11371. }
  11372. if (!func_declared) {
  11373. /* Check whether the function is declared in table segs,
  11374. note that it doesn't matter whether the table seg's
  11375. mode is passive, active or declarative. */
  11376. for (i = 0; i < module->table_seg_count;
  11377. i++, table_seg++) {
  11378. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11379. #if WASM_ENABLE_GC != 0
  11380. /* elem type is (ref null? func) or
  11381. (ref null? $t) */
  11382. || ((table_seg->elem_type
  11383. == REF_TYPE_HT_NON_NULLABLE
  11384. || table_seg->elem_type
  11385. == REF_TYPE_HT_NULLABLE)
  11386. && (table_seg->elem_ref_type->ref_ht_common
  11387. .heap_type
  11388. == HEAP_TYPE_FUNC
  11389. || table_seg->elem_ref_type
  11390. ->ref_ht_common.heap_type
  11391. > 0))
  11392. #endif
  11393. ) {
  11394. for (j = 0; j < table_seg->value_count; j++) {
  11395. if (table_seg->init_values[j].u.ref_index
  11396. == func_idx) {
  11397. func_declared = true;
  11398. break;
  11399. }
  11400. }
  11401. }
  11402. }
  11403. }
  11404. if (!func_declared) {
  11405. /* Check whether the function is exported */
  11406. for (i = 0; i < module->export_count; i++) {
  11407. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11408. && module->exports[i].index == func_idx) {
  11409. func_declared = true;
  11410. break;
  11411. }
  11412. }
  11413. }
  11414. if (!func_declared) {
  11415. set_error_buf(error_buf, error_buf_size,
  11416. "undeclared function reference");
  11417. goto fail;
  11418. }
  11419. }
  11420. #if WASM_ENABLE_FAST_INTERP != 0
  11421. emit_uint32(loader_ctx, func_idx);
  11422. #endif
  11423. #if WASM_ENABLE_GC == 0
  11424. PUSH_FUNCREF();
  11425. #else
  11426. if (func_idx < module->import_function_count)
  11427. type_idx =
  11428. module->import_functions[func_idx].u.function.type_idx;
  11429. else
  11430. type_idx = module
  11431. ->functions[func_idx
  11432. - module->import_function_count]
  11433. ->type_idx;
  11434. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11435. false, type_idx);
  11436. PUSH_REF(wasm_ref_type.ref_type);
  11437. #endif
  11438. #if WASM_ENABLE_WAMR_COMPILER != 0
  11439. module->is_ref_types_used = true;
  11440. #endif
  11441. break;
  11442. }
  11443. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11444. #if WASM_ENABLE_GC != 0
  11445. case WASM_OP_REF_AS_NON_NULL:
  11446. case WASM_OP_BR_ON_NULL:
  11447. {
  11448. uint8 type;
  11449. WASMRefType ref_type;
  11450. /* POP (ref null ht) and get the converted (ref ht) */
  11451. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11452. error_buf, error_buf_size)) {
  11453. goto fail;
  11454. }
  11455. if (opcode == WASM_OP_BR_ON_NULL) {
  11456. if (!(frame_csp_tmp =
  11457. check_branch_block(loader_ctx, &p, p_end, opcode,
  11458. error_buf, error_buf_size))) {
  11459. goto fail;
  11460. }
  11461. #if WASM_ENABLE_FAST_INTERP != 0
  11462. disable_emit = true;
  11463. #endif
  11464. }
  11465. /* PUSH the converted (ref ht) */
  11466. if (type != VALUE_TYPE_ANY) {
  11467. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11468. sizeof(WASMRefType));
  11469. }
  11470. PUSH_REF(type);
  11471. break;
  11472. }
  11473. case WASM_OP_BR_ON_NON_NULL:
  11474. {
  11475. uint8 type;
  11476. WASMRefType ref_type;
  11477. uint32 available_stack_cell =
  11478. loader_ctx->stack_cell_num
  11479. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11480. /* POP (ref null ht) and get the converted (ref ht) */
  11481. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11482. error_buf, error_buf_size)) {
  11483. goto fail;
  11484. }
  11485. #if WASM_ENABLE_FAST_INTERP != 0
  11486. disable_emit = true;
  11487. #endif
  11488. /* Temporarily PUSH back (ref ht), check brach block and
  11489. then POP it */
  11490. if (available_stack_cell
  11491. > 0) { /* stack isn't in polymorphic state */
  11492. if (type != VALUE_TYPE_ANY) {
  11493. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11494. &ref_type, sizeof(WASMRefType));
  11495. }
  11496. PUSH_REF(type);
  11497. }
  11498. if (!(frame_csp_tmp =
  11499. check_branch_block(loader_ctx, &p, p_end, opcode,
  11500. error_buf, error_buf_size))) {
  11501. goto fail;
  11502. }
  11503. if (available_stack_cell
  11504. > 0) { /* stack isn't in polymorphic state */
  11505. POP_REF(type);
  11506. #if WASM_ENABLE_FAST_INTERP != 0
  11507. /* Erase the opnd offset emitted by POP_REF() */
  11508. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11509. #endif
  11510. }
  11511. break;
  11512. }
  11513. case WASM_OP_REF_EQ:
  11514. POP_REF(REF_TYPE_EQREF);
  11515. POP_REF(REF_TYPE_EQREF);
  11516. PUSH_I32();
  11517. break;
  11518. #endif /* end of WASM_ENABLE_GC != 0 */
  11519. case WASM_OP_GET_LOCAL:
  11520. {
  11521. p_org = p - 1;
  11522. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11523. PUSH_TYPE(local_type);
  11524. #if WASM_ENABLE_GC != 0
  11525. /* Cannot get a non-nullable and unset local */
  11526. if (local_idx >= param_count
  11527. && wasm_is_reftype_htref_non_nullable(local_type)
  11528. && !wasm_loader_get_local_status(loader_ctx,
  11529. local_idx - param_count)) {
  11530. set_error_buf(error_buf, error_buf_size,
  11531. "uninitialized local");
  11532. goto fail;
  11533. }
  11534. #endif
  11535. #if WASM_ENABLE_FAST_INTERP != 0
  11536. /* Get Local is optimized out */
  11537. skip_label();
  11538. disable_emit = true;
  11539. operand_offset = local_offset;
  11540. PUSH_OFFSET_TYPE(local_type);
  11541. #else
  11542. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11543. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11544. if (local_offset < 0x80
  11545. #if WASM_ENABLE_GC != 0
  11546. && !wasm_is_type_reftype(local_type)
  11547. #endif
  11548. ) {
  11549. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11550. if (is_32bit_type(local_type)) {
  11551. *p_org++ = (uint8)local_offset;
  11552. }
  11553. else {
  11554. *p_org++ = (uint8)(local_offset | 0x80);
  11555. }
  11556. while (p_org < p) {
  11557. *p_org++ = WASM_OP_NOP;
  11558. }
  11559. }
  11560. #endif
  11561. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11562. break;
  11563. }
  11564. case WASM_OP_SET_LOCAL:
  11565. {
  11566. p_org = p - 1;
  11567. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11568. #if WASM_ENABLE_FAST_INTERP != 0
  11569. if (!(preserve_referenced_local(
  11570. loader_ctx, opcode, local_offset, local_type,
  11571. &preserve_local, error_buf, error_buf_size)))
  11572. goto fail;
  11573. if (local_offset < 256
  11574. #if WASM_ENABLE_GC != 0
  11575. && !wasm_is_type_reftype(local_type)
  11576. #endif
  11577. ) {
  11578. skip_label();
  11579. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11580. if (loader_ctx->p_code_compiled)
  11581. STORE_U16(loader_ctx->p_code_compiled - 2,
  11582. local_offset);
  11583. loader_ctx->frame_offset--;
  11584. loader_ctx->dynamic_offset--;
  11585. }
  11586. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11587. if (loader_ctx->p_code_compiled)
  11588. STORE_U16(loader_ctx->p_code_compiled - 2,
  11589. local_offset);
  11590. loader_ctx->frame_offset -= 2;
  11591. loader_ctx->dynamic_offset -= 2;
  11592. }
  11593. else {
  11594. if (is_32bit_type(local_type)) {
  11595. emit_label(EXT_OP_SET_LOCAL_FAST);
  11596. emit_byte(loader_ctx, (uint8)local_offset);
  11597. }
  11598. else {
  11599. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11600. emit_byte(loader_ctx, (uint8)local_offset);
  11601. }
  11602. POP_OFFSET_TYPE(local_type);
  11603. }
  11604. }
  11605. else { /* local index larger than 255, reserve leb */
  11606. emit_uint32(loader_ctx, local_idx);
  11607. POP_OFFSET_TYPE(local_type);
  11608. }
  11609. #else
  11610. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11611. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11612. if (local_offset < 0x80
  11613. #if WASM_ENABLE_GC != 0
  11614. && !wasm_is_type_reftype(local_type)
  11615. #endif
  11616. ) {
  11617. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11618. if (is_32bit_type(local_type)) {
  11619. *p_org++ = (uint8)local_offset;
  11620. }
  11621. else {
  11622. *p_org++ = (uint8)(local_offset | 0x80);
  11623. }
  11624. while (p_org < p) {
  11625. *p_org++ = WASM_OP_NOP;
  11626. }
  11627. }
  11628. #endif
  11629. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11630. #if WASM_ENABLE_GC != 0
  11631. if (local_idx >= param_count) {
  11632. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11633. }
  11634. #endif
  11635. POP_TYPE(local_type);
  11636. break;
  11637. }
  11638. case WASM_OP_TEE_LOCAL:
  11639. {
  11640. p_org = p - 1;
  11641. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11642. #if WASM_ENABLE_FAST_INTERP != 0
  11643. /* If the stack is in polymorphic state, do fake pop and push on
  11644. offset stack to keep the depth of offset stack to be the
  11645. same with ref stack */
  11646. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11647. if (cur_block->is_stack_polymorphic) {
  11648. POP_OFFSET_TYPE(local_type);
  11649. PUSH_OFFSET_TYPE(local_type);
  11650. }
  11651. #endif
  11652. POP_TYPE(local_type);
  11653. PUSH_TYPE(local_type);
  11654. #if WASM_ENABLE_FAST_INTERP != 0
  11655. if (!(preserve_referenced_local(
  11656. loader_ctx, opcode, local_offset, local_type,
  11657. &preserve_local, error_buf, error_buf_size)))
  11658. goto fail;
  11659. if (local_offset < 256
  11660. #if WASM_ENABLE_GC != 0
  11661. && !wasm_is_type_reftype(local_type)
  11662. #endif
  11663. ) {
  11664. skip_label();
  11665. if (is_32bit_type(local_type)) {
  11666. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11667. emit_byte(loader_ctx, (uint8)local_offset);
  11668. }
  11669. else {
  11670. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11671. emit_byte(loader_ctx, (uint8)local_offset);
  11672. }
  11673. }
  11674. else { /* local index larger than 255, reserve leb */
  11675. emit_uint32(loader_ctx, local_idx);
  11676. }
  11677. emit_operand(loader_ctx,
  11678. *(loader_ctx->frame_offset
  11679. - wasm_value_type_cell_num(local_type)));
  11680. #else
  11681. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11682. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11683. if (local_offset < 0x80
  11684. #if WASM_ENABLE_GC != 0
  11685. && !wasm_is_type_reftype(local_type)
  11686. #endif
  11687. ) {
  11688. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11689. if (is_32bit_type(local_type)) {
  11690. *p_org++ = (uint8)local_offset;
  11691. }
  11692. else {
  11693. *p_org++ = (uint8)(local_offset | 0x80);
  11694. }
  11695. while (p_org < p) {
  11696. *p_org++ = WASM_OP_NOP;
  11697. }
  11698. }
  11699. #endif
  11700. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11701. #if WASM_ENABLE_GC != 0
  11702. if (local_idx >= param_count) {
  11703. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11704. }
  11705. #endif
  11706. break;
  11707. }
  11708. case WASM_OP_GET_GLOBAL:
  11709. {
  11710. #if WASM_ENABLE_GC != 0
  11711. WASMRefType *ref_type;
  11712. #endif
  11713. p_org = p - 1;
  11714. read_leb_uint32(p, p_end, global_idx);
  11715. if (global_idx >= global_count) {
  11716. set_error_buf(error_buf, error_buf_size, "unknown global");
  11717. goto fail;
  11718. }
  11719. global_type = global_idx < module->import_global_count
  11720. ? module->import_globals[global_idx]
  11721. .u.global.type.val_type
  11722. : module
  11723. ->globals[global_idx
  11724. - module->import_global_count]
  11725. .type.val_type;
  11726. #if WASM_ENABLE_GC != 0
  11727. ref_type =
  11728. global_idx < module->import_global_count
  11729. ? module->import_globals[global_idx].u.global.ref_type
  11730. : module
  11731. ->globals[global_idx
  11732. - module->import_global_count]
  11733. .ref_type;
  11734. if (wasm_is_type_multi_byte_type(global_type)) {
  11735. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11736. wasm_reftype_struct_size(ref_type));
  11737. }
  11738. #endif
  11739. PUSH_TYPE(global_type);
  11740. #if WASM_ENABLE_FAST_INTERP == 0
  11741. if (global_type == VALUE_TYPE_I64
  11742. || global_type == VALUE_TYPE_F64) {
  11743. #if WASM_ENABLE_DEBUG_INTERP != 0
  11744. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11745. error_buf_size)) {
  11746. goto fail;
  11747. }
  11748. #endif
  11749. *p_org = WASM_OP_GET_GLOBAL_64;
  11750. }
  11751. #else /* else of WASM_ENABLE_FAST_INTERP */
  11752. if (global_type == VALUE_TYPE_I64
  11753. || global_type == VALUE_TYPE_F64) {
  11754. skip_label();
  11755. emit_label(WASM_OP_GET_GLOBAL_64);
  11756. }
  11757. emit_uint32(loader_ctx, global_idx);
  11758. PUSH_OFFSET_TYPE(global_type);
  11759. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11760. break;
  11761. }
  11762. case WASM_OP_SET_GLOBAL:
  11763. {
  11764. bool is_mutable = false;
  11765. #if WASM_ENABLE_GC != 0
  11766. WASMRefType *ref_type;
  11767. #endif
  11768. p_org = p - 1;
  11769. read_leb_uint32(p, p_end, global_idx);
  11770. if (global_idx >= global_count) {
  11771. set_error_buf(error_buf, error_buf_size, "unknown global");
  11772. goto fail;
  11773. }
  11774. is_mutable = global_idx < module->import_global_count
  11775. ? module->import_globals[global_idx]
  11776. .u.global.type.is_mutable
  11777. : module
  11778. ->globals[global_idx
  11779. - module->import_global_count]
  11780. .type.is_mutable;
  11781. if (!is_mutable) {
  11782. #if WASM_ENABLE_GC == 0
  11783. set_error_buf(error_buf, error_buf_size,
  11784. "global is immutable");
  11785. #else
  11786. set_error_buf(error_buf, error_buf_size,
  11787. "immutable global");
  11788. #endif
  11789. goto fail;
  11790. }
  11791. global_type = global_idx < module->import_global_count
  11792. ? module->import_globals[global_idx]
  11793. .u.global.type.val_type
  11794. : module
  11795. ->globals[global_idx
  11796. - module->import_global_count]
  11797. .type.val_type;
  11798. #if WASM_ENABLE_GC != 0
  11799. ref_type =
  11800. global_idx < module->import_global_count
  11801. ? module->import_globals[global_idx].u.global.ref_type
  11802. : module
  11803. ->globals[global_idx
  11804. - module->import_global_count]
  11805. .ref_type;
  11806. if (wasm_is_type_multi_byte_type(global_type)) {
  11807. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11808. wasm_reftype_struct_size(ref_type));
  11809. }
  11810. #endif
  11811. #if WASM_ENABLE_FAST_INTERP == 0
  11812. if (global_type == VALUE_TYPE_I64
  11813. || global_type == VALUE_TYPE_F64) {
  11814. #if WASM_ENABLE_DEBUG_INTERP != 0
  11815. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11816. error_buf_size)) {
  11817. goto fail;
  11818. }
  11819. #endif
  11820. *p_org = WASM_OP_SET_GLOBAL_64;
  11821. }
  11822. else if (module->aux_stack_size > 0
  11823. && global_idx == module->aux_stack_top_global_index) {
  11824. #if WASM_ENABLE_DEBUG_INTERP != 0
  11825. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11826. error_buf_size)) {
  11827. goto fail;
  11828. }
  11829. #endif
  11830. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11831. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11832. func->has_op_set_global_aux_stack = true;
  11833. #endif
  11834. }
  11835. #else /* else of WASM_ENABLE_FAST_INTERP */
  11836. if (global_type == VALUE_TYPE_I64
  11837. || global_type == VALUE_TYPE_F64) {
  11838. skip_label();
  11839. emit_label(WASM_OP_SET_GLOBAL_64);
  11840. }
  11841. else if (module->aux_stack_size > 0
  11842. && global_idx == module->aux_stack_top_global_index) {
  11843. skip_label();
  11844. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11845. }
  11846. emit_uint32(loader_ctx, global_idx);
  11847. POP_OFFSET_TYPE(global_type);
  11848. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11849. POP_TYPE(global_type);
  11850. break;
  11851. }
  11852. /* load */
  11853. case WASM_OP_I32_LOAD:
  11854. case WASM_OP_I32_LOAD8_S:
  11855. case WASM_OP_I32_LOAD8_U:
  11856. case WASM_OP_I32_LOAD16_S:
  11857. case WASM_OP_I32_LOAD16_U:
  11858. case WASM_OP_I64_LOAD:
  11859. case WASM_OP_I64_LOAD8_S:
  11860. case WASM_OP_I64_LOAD8_U:
  11861. case WASM_OP_I64_LOAD16_S:
  11862. case WASM_OP_I64_LOAD16_U:
  11863. case WASM_OP_I64_LOAD32_S:
  11864. case WASM_OP_I64_LOAD32_U:
  11865. case WASM_OP_F32_LOAD:
  11866. case WASM_OP_F64_LOAD:
  11867. /* store */
  11868. case WASM_OP_I32_STORE:
  11869. case WASM_OP_I32_STORE8:
  11870. case WASM_OP_I32_STORE16:
  11871. case WASM_OP_I64_STORE:
  11872. case WASM_OP_I64_STORE8:
  11873. case WASM_OP_I64_STORE16:
  11874. case WASM_OP_I64_STORE32:
  11875. case WASM_OP_F32_STORE:
  11876. case WASM_OP_F64_STORE:
  11877. {
  11878. #if WASM_ENABLE_FAST_INTERP != 0
  11879. /* change F32/F64 into I32/I64 */
  11880. if (opcode == WASM_OP_F32_LOAD) {
  11881. skip_label();
  11882. emit_label(WASM_OP_I32_LOAD);
  11883. }
  11884. else if (opcode == WASM_OP_F64_LOAD) {
  11885. skip_label();
  11886. emit_label(WASM_OP_I64_LOAD);
  11887. }
  11888. else if (opcode == WASM_OP_F32_STORE) {
  11889. skip_label();
  11890. emit_label(WASM_OP_I32_STORE);
  11891. }
  11892. else if (opcode == WASM_OP_F64_STORE) {
  11893. skip_label();
  11894. emit_label(WASM_OP_I64_STORE);
  11895. }
  11896. #endif
  11897. CHECK_MEMORY();
  11898. read_leb_memarg(p, p_end, align); /* align */
  11899. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  11900. if (!check_memory_access_align(opcode, align, error_buf,
  11901. error_buf_size)) {
  11902. goto fail;
  11903. }
  11904. #if WASM_ENABLE_FAST_INTERP != 0
  11905. emit_uint32(loader_ctx, mem_offset);
  11906. #endif
  11907. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11908. func->has_memory_operations = true;
  11909. #endif
  11910. switch (opcode) {
  11911. /* load */
  11912. case WASM_OP_I32_LOAD:
  11913. case WASM_OP_I32_LOAD8_S:
  11914. case WASM_OP_I32_LOAD8_U:
  11915. case WASM_OP_I32_LOAD16_S:
  11916. case WASM_OP_I32_LOAD16_U:
  11917. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  11918. break;
  11919. case WASM_OP_I64_LOAD:
  11920. case WASM_OP_I64_LOAD8_S:
  11921. case WASM_OP_I64_LOAD8_U:
  11922. case WASM_OP_I64_LOAD16_S:
  11923. case WASM_OP_I64_LOAD16_U:
  11924. case WASM_OP_I64_LOAD32_S:
  11925. case WASM_OP_I64_LOAD32_U:
  11926. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  11927. break;
  11928. case WASM_OP_F32_LOAD:
  11929. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  11930. break;
  11931. case WASM_OP_F64_LOAD:
  11932. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  11933. break;
  11934. /* store */
  11935. case WASM_OP_I32_STORE:
  11936. case WASM_OP_I32_STORE8:
  11937. case WASM_OP_I32_STORE16:
  11938. POP_I32();
  11939. POP_MEM_OFFSET();
  11940. break;
  11941. case WASM_OP_I64_STORE:
  11942. case WASM_OP_I64_STORE8:
  11943. case WASM_OP_I64_STORE16:
  11944. case WASM_OP_I64_STORE32:
  11945. POP_I64();
  11946. POP_MEM_OFFSET();
  11947. break;
  11948. case WASM_OP_F32_STORE:
  11949. POP_F32();
  11950. POP_MEM_OFFSET();
  11951. break;
  11952. case WASM_OP_F64_STORE:
  11953. POP_F64();
  11954. POP_MEM_OFFSET();
  11955. break;
  11956. default:
  11957. break;
  11958. }
  11959. break;
  11960. }
  11961. case WASM_OP_MEMORY_SIZE:
  11962. CHECK_MEMORY();
  11963. read_leb_uint32(p, p_end, memidx);
  11964. check_memidx(module, memidx);
  11965. PUSH_PAGE_COUNT();
  11966. module->possible_memory_grow = true;
  11967. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11968. func->has_memory_operations = true;
  11969. #endif
  11970. break;
  11971. case WASM_OP_MEMORY_GROW:
  11972. CHECK_MEMORY();
  11973. read_leb_uint32(p, p_end, memidx);
  11974. check_memidx(module, memidx);
  11975. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  11976. module->possible_memory_grow = true;
  11977. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11978. || WASM_ENABLE_WAMR_COMPILER != 0
  11979. func->has_op_memory_grow = true;
  11980. #endif
  11981. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11982. func->has_memory_operations = true;
  11983. #endif
  11984. break;
  11985. case WASM_OP_I32_CONST:
  11986. read_leb_int32(p, p_end, i32_const);
  11987. #if WASM_ENABLE_FAST_INTERP != 0
  11988. skip_label();
  11989. disable_emit = true;
  11990. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  11991. if (operand_offset == 0) {
  11992. disable_emit = false;
  11993. emit_label(WASM_OP_I32_CONST);
  11994. emit_uint32(loader_ctx, i32_const);
  11995. }
  11996. #else
  11997. (void)i32_const;
  11998. #endif
  11999. PUSH_I32();
  12000. break;
  12001. case WASM_OP_I64_CONST:
  12002. read_leb_int64(p, p_end, i64_const);
  12003. #if WASM_ENABLE_FAST_INTERP != 0
  12004. skip_label();
  12005. disable_emit = true;
  12006. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  12007. if (operand_offset == 0) {
  12008. disable_emit = false;
  12009. emit_label(WASM_OP_I64_CONST);
  12010. emit_uint64(loader_ctx, i64_const);
  12011. }
  12012. #endif
  12013. PUSH_I64();
  12014. break;
  12015. case WASM_OP_F32_CONST:
  12016. CHECK_BUF(p, p_end, sizeof(float32));
  12017. p += sizeof(float32);
  12018. #if WASM_ENABLE_FAST_INTERP != 0
  12019. skip_label();
  12020. disable_emit = true;
  12021. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12022. sizeof(float32));
  12023. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12024. if (operand_offset == 0) {
  12025. disable_emit = false;
  12026. emit_label(WASM_OP_F32_CONST);
  12027. emit_float32(loader_ctx, f32_const);
  12028. }
  12029. #endif
  12030. PUSH_F32();
  12031. break;
  12032. case WASM_OP_F64_CONST:
  12033. CHECK_BUF(p, p_end, sizeof(float64));
  12034. p += sizeof(float64);
  12035. #if WASM_ENABLE_FAST_INTERP != 0
  12036. skip_label();
  12037. disable_emit = true;
  12038. /* Some MCU may require 8-byte align */
  12039. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12040. sizeof(float64));
  12041. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12042. if (operand_offset == 0) {
  12043. disable_emit = false;
  12044. emit_label(WASM_OP_F64_CONST);
  12045. emit_float64(loader_ctx, f64_const);
  12046. }
  12047. #endif
  12048. PUSH_F64();
  12049. break;
  12050. case WASM_OP_I32_EQZ:
  12051. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12052. break;
  12053. case WASM_OP_I32_EQ:
  12054. case WASM_OP_I32_NE:
  12055. case WASM_OP_I32_LT_S:
  12056. case WASM_OP_I32_LT_U:
  12057. case WASM_OP_I32_GT_S:
  12058. case WASM_OP_I32_GT_U:
  12059. case WASM_OP_I32_LE_S:
  12060. case WASM_OP_I32_LE_U:
  12061. case WASM_OP_I32_GE_S:
  12062. case WASM_OP_I32_GE_U:
  12063. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12064. break;
  12065. case WASM_OP_I64_EQZ:
  12066. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12067. break;
  12068. case WASM_OP_I64_EQ:
  12069. case WASM_OP_I64_NE:
  12070. case WASM_OP_I64_LT_S:
  12071. case WASM_OP_I64_LT_U:
  12072. case WASM_OP_I64_GT_S:
  12073. case WASM_OP_I64_GT_U:
  12074. case WASM_OP_I64_LE_S:
  12075. case WASM_OP_I64_LE_U:
  12076. case WASM_OP_I64_GE_S:
  12077. case WASM_OP_I64_GE_U:
  12078. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12079. break;
  12080. case WASM_OP_F32_EQ:
  12081. case WASM_OP_F32_NE:
  12082. case WASM_OP_F32_LT:
  12083. case WASM_OP_F32_GT:
  12084. case WASM_OP_F32_LE:
  12085. case WASM_OP_F32_GE:
  12086. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12087. break;
  12088. case WASM_OP_F64_EQ:
  12089. case WASM_OP_F64_NE:
  12090. case WASM_OP_F64_LT:
  12091. case WASM_OP_F64_GT:
  12092. case WASM_OP_F64_LE:
  12093. case WASM_OP_F64_GE:
  12094. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12095. break;
  12096. case WASM_OP_I32_CLZ:
  12097. case WASM_OP_I32_CTZ:
  12098. case WASM_OP_I32_POPCNT:
  12099. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12100. break;
  12101. case WASM_OP_I32_ADD:
  12102. case WASM_OP_I32_SUB:
  12103. case WASM_OP_I32_MUL:
  12104. case WASM_OP_I32_DIV_S:
  12105. case WASM_OP_I32_DIV_U:
  12106. case WASM_OP_I32_REM_S:
  12107. case WASM_OP_I32_REM_U:
  12108. case WASM_OP_I32_AND:
  12109. case WASM_OP_I32_OR:
  12110. case WASM_OP_I32_XOR:
  12111. case WASM_OP_I32_SHL:
  12112. case WASM_OP_I32_SHR_S:
  12113. case WASM_OP_I32_SHR_U:
  12114. case WASM_OP_I32_ROTL:
  12115. case WASM_OP_I32_ROTR:
  12116. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12117. break;
  12118. case WASM_OP_I64_CLZ:
  12119. case WASM_OP_I64_CTZ:
  12120. case WASM_OP_I64_POPCNT:
  12121. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12122. break;
  12123. case WASM_OP_I64_ADD:
  12124. case WASM_OP_I64_SUB:
  12125. case WASM_OP_I64_MUL:
  12126. case WASM_OP_I64_DIV_S:
  12127. case WASM_OP_I64_DIV_U:
  12128. case WASM_OP_I64_REM_S:
  12129. case WASM_OP_I64_REM_U:
  12130. case WASM_OP_I64_AND:
  12131. case WASM_OP_I64_OR:
  12132. case WASM_OP_I64_XOR:
  12133. case WASM_OP_I64_SHL:
  12134. case WASM_OP_I64_SHR_S:
  12135. case WASM_OP_I64_SHR_U:
  12136. case WASM_OP_I64_ROTL:
  12137. case WASM_OP_I64_ROTR:
  12138. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12139. break;
  12140. case WASM_OP_F32_ABS:
  12141. case WASM_OP_F32_NEG:
  12142. case WASM_OP_F32_CEIL:
  12143. case WASM_OP_F32_FLOOR:
  12144. case WASM_OP_F32_TRUNC:
  12145. case WASM_OP_F32_NEAREST:
  12146. case WASM_OP_F32_SQRT:
  12147. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12148. break;
  12149. case WASM_OP_F32_ADD:
  12150. case WASM_OP_F32_SUB:
  12151. case WASM_OP_F32_MUL:
  12152. case WASM_OP_F32_DIV:
  12153. case WASM_OP_F32_MIN:
  12154. case WASM_OP_F32_MAX:
  12155. case WASM_OP_F32_COPYSIGN:
  12156. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12157. break;
  12158. case WASM_OP_F64_ABS:
  12159. case WASM_OP_F64_NEG:
  12160. case WASM_OP_F64_CEIL:
  12161. case WASM_OP_F64_FLOOR:
  12162. case WASM_OP_F64_TRUNC:
  12163. case WASM_OP_F64_NEAREST:
  12164. case WASM_OP_F64_SQRT:
  12165. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12166. break;
  12167. case WASM_OP_F64_ADD:
  12168. case WASM_OP_F64_SUB:
  12169. case WASM_OP_F64_MUL:
  12170. case WASM_OP_F64_DIV:
  12171. case WASM_OP_F64_MIN:
  12172. case WASM_OP_F64_MAX:
  12173. case WASM_OP_F64_COPYSIGN:
  12174. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12175. break;
  12176. case WASM_OP_I32_WRAP_I64:
  12177. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12178. break;
  12179. case WASM_OP_I32_TRUNC_S_F32:
  12180. case WASM_OP_I32_TRUNC_U_F32:
  12181. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12182. break;
  12183. case WASM_OP_I32_TRUNC_S_F64:
  12184. case WASM_OP_I32_TRUNC_U_F64:
  12185. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12186. break;
  12187. case WASM_OP_I64_EXTEND_S_I32:
  12188. case WASM_OP_I64_EXTEND_U_I32:
  12189. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12190. break;
  12191. case WASM_OP_I64_TRUNC_S_F32:
  12192. case WASM_OP_I64_TRUNC_U_F32:
  12193. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12194. break;
  12195. case WASM_OP_I64_TRUNC_S_F64:
  12196. case WASM_OP_I64_TRUNC_U_F64:
  12197. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12198. break;
  12199. case WASM_OP_F32_CONVERT_S_I32:
  12200. case WASM_OP_F32_CONVERT_U_I32:
  12201. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12202. break;
  12203. case WASM_OP_F32_CONVERT_S_I64:
  12204. case WASM_OP_F32_CONVERT_U_I64:
  12205. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12206. break;
  12207. case WASM_OP_F32_DEMOTE_F64:
  12208. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12209. break;
  12210. case WASM_OP_F64_CONVERT_S_I32:
  12211. case WASM_OP_F64_CONVERT_U_I32:
  12212. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12213. break;
  12214. case WASM_OP_F64_CONVERT_S_I64:
  12215. case WASM_OP_F64_CONVERT_U_I64:
  12216. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12217. break;
  12218. case WASM_OP_F64_PROMOTE_F32:
  12219. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12220. break;
  12221. case WASM_OP_I32_REINTERPRET_F32:
  12222. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12223. break;
  12224. case WASM_OP_I64_REINTERPRET_F64:
  12225. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12226. break;
  12227. case WASM_OP_F32_REINTERPRET_I32:
  12228. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12229. break;
  12230. case WASM_OP_F64_REINTERPRET_I64:
  12231. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12232. break;
  12233. case WASM_OP_I32_EXTEND8_S:
  12234. case WASM_OP_I32_EXTEND16_S:
  12235. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12236. break;
  12237. case WASM_OP_I64_EXTEND8_S:
  12238. case WASM_OP_I64_EXTEND16_S:
  12239. case WASM_OP_I64_EXTEND32_S:
  12240. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12241. break;
  12242. #if WASM_ENABLE_GC != 0
  12243. case WASM_OP_GC_PREFIX:
  12244. {
  12245. uint32 opcode1;
  12246. read_leb_uint32(p, p_end, opcode1);
  12247. #if WASM_ENABLE_FAST_INTERP != 0
  12248. emit_byte(loader_ctx, ((uint8)opcode1));
  12249. #endif
  12250. switch (opcode1) {
  12251. case WASM_OP_STRUCT_NEW:
  12252. case WASM_OP_STRUCT_NEW_DEFAULT:
  12253. {
  12254. read_leb_uint32(p, p_end, type_idx);
  12255. #if WASM_ENABLE_FAST_INTERP != 0
  12256. emit_uint32(loader_ctx, type_idx);
  12257. #endif
  12258. if (!check_type_index(module, module->type_count,
  12259. type_idx, error_buf,
  12260. error_buf_size)) {
  12261. goto fail;
  12262. }
  12263. if (module->types[type_idx]->type_flag
  12264. != WASM_TYPE_STRUCT) {
  12265. set_error_buf(error_buf, error_buf_size,
  12266. "unkown struct type");
  12267. goto fail;
  12268. }
  12269. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12270. int32 j, k;
  12271. uint8 value_type;
  12272. uint32 ref_type_struct_size;
  12273. WASMStructType *struct_type =
  12274. (WASMStructType *)module->types[type_idx];
  12275. k = struct_type->ref_type_map_count - 1;
  12276. for (j = struct_type->field_count - 1; j >= 0;
  12277. j--) {
  12278. value_type = struct_type->fields[j].field_type;
  12279. if (wasm_is_type_reftype(value_type)) {
  12280. if (wasm_is_type_multi_byte_type(
  12281. value_type)) {
  12282. ref_type_struct_size =
  12283. wasm_reftype_struct_size(
  12284. struct_type->ref_type_maps[k]
  12285. .ref_type);
  12286. bh_memcpy_s(
  12287. &wasm_ref_type,
  12288. (uint32)sizeof(WASMRefType),
  12289. struct_type->ref_type_maps[k]
  12290. .ref_type,
  12291. ref_type_struct_size);
  12292. k--;
  12293. }
  12294. POP_REF(value_type);
  12295. }
  12296. else {
  12297. switch (value_type) {
  12298. case VALUE_TYPE_I32:
  12299. case PACKED_TYPE_I8:
  12300. case PACKED_TYPE_I16:
  12301. POP_I32();
  12302. break;
  12303. case VALUE_TYPE_I64:
  12304. POP_I64();
  12305. break;
  12306. case VALUE_TYPE_F32:
  12307. POP_F32();
  12308. break;
  12309. case VALUE_TYPE_F64:
  12310. POP_F64();
  12311. break;
  12312. default:
  12313. set_error_buf(error_buf,
  12314. error_buf_size,
  12315. "unknown type");
  12316. goto fail;
  12317. }
  12318. }
  12319. }
  12320. }
  12321. /* PUSH struct obj, (ref $t) */
  12322. wasm_set_refheaptype_typeidx(
  12323. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12324. PUSH_REF(wasm_ref_type.ref_type);
  12325. break;
  12326. }
  12327. case WASM_OP_STRUCT_GET:
  12328. case WASM_OP_STRUCT_GET_S:
  12329. case WASM_OP_STRUCT_GET_U:
  12330. case WASM_OP_STRUCT_SET:
  12331. {
  12332. WASMStructType *struct_type;
  12333. WASMRefType *ref_type = NULL;
  12334. uint32 field_idx;
  12335. uint8 field_type;
  12336. read_leb_uint32(p, p_end, type_idx);
  12337. #if WASM_ENABLE_FAST_INTERP != 0
  12338. emit_uint32(loader_ctx, type_idx);
  12339. #endif
  12340. if (!check_type_index(module, module->type_count,
  12341. type_idx, error_buf,
  12342. error_buf_size)) {
  12343. goto fail;
  12344. }
  12345. if (module->types[type_idx]->type_flag
  12346. != WASM_TYPE_STRUCT) {
  12347. set_error_buf(error_buf, error_buf_size,
  12348. "unknown struct type");
  12349. goto fail;
  12350. }
  12351. struct_type = (WASMStructType *)module->types[type_idx];
  12352. read_leb_uint32(p, p_end, field_idx);
  12353. #if WASM_ENABLE_FAST_INTERP != 0
  12354. emit_uint32(loader_ctx, field_idx);
  12355. #endif
  12356. if (field_idx >= struct_type->field_count) {
  12357. set_error_buf(error_buf, error_buf_size,
  12358. "unknown struct field");
  12359. goto fail;
  12360. }
  12361. if (opcode1 == WASM_OP_STRUCT_SET
  12362. && !(struct_type->fields[field_idx].field_flags
  12363. & 1)) {
  12364. set_error_buf(error_buf, error_buf_size,
  12365. "field is immutable");
  12366. goto fail;
  12367. }
  12368. field_type = struct_type->fields[field_idx].field_type;
  12369. if (is_packed_type(field_type)) {
  12370. if (opcode1 == WASM_OP_STRUCT_GET) {
  12371. set_error_buf(error_buf, error_buf_size,
  12372. "type mismatch");
  12373. goto fail;
  12374. }
  12375. else {
  12376. field_type = VALUE_TYPE_I32;
  12377. }
  12378. }
  12379. if (wasm_is_type_multi_byte_type(field_type)) {
  12380. ref_type = wasm_reftype_map_find(
  12381. struct_type->ref_type_maps,
  12382. struct_type->ref_type_map_count, field_idx);
  12383. bh_assert(ref_type);
  12384. }
  12385. if (opcode1 == WASM_OP_STRUCT_SET) {
  12386. /* POP field */
  12387. if (wasm_is_type_multi_byte_type(field_type)) {
  12388. bh_memcpy_s(&wasm_ref_type,
  12389. (uint32)sizeof(WASMRefType),
  12390. ref_type,
  12391. wasm_reftype_struct_size(ref_type));
  12392. }
  12393. POP_REF(field_type);
  12394. /* POP struct obj, (ref null $t) */
  12395. wasm_set_refheaptype_typeidx(
  12396. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12397. POP_REF(wasm_ref_type.ref_type);
  12398. }
  12399. else {
  12400. /* POP struct obj, (ref null $t) */
  12401. wasm_set_refheaptype_typeidx(
  12402. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12403. POP_REF(wasm_ref_type.ref_type);
  12404. /* PUSH field */
  12405. if (wasm_is_type_multi_byte_type(field_type)) {
  12406. bh_memcpy_s(&wasm_ref_type,
  12407. (uint32)sizeof(WASMRefType),
  12408. ref_type,
  12409. wasm_reftype_struct_size(ref_type));
  12410. }
  12411. PUSH_REF(field_type);
  12412. }
  12413. break;
  12414. }
  12415. case WASM_OP_ARRAY_NEW:
  12416. case WASM_OP_ARRAY_NEW_DEFAULT:
  12417. case WASM_OP_ARRAY_NEW_FIXED:
  12418. case WASM_OP_ARRAY_NEW_DATA:
  12419. case WASM_OP_ARRAY_NEW_ELEM:
  12420. {
  12421. WASMArrayType *array_type;
  12422. uint8 elem_type;
  12423. uint32 u32 = 0;
  12424. read_leb_uint32(p, p_end, type_idx);
  12425. #if WASM_ENABLE_FAST_INTERP != 0
  12426. emit_uint32(loader_ctx, type_idx);
  12427. #endif
  12428. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12429. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12430. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12431. read_leb_uint32(p, p_end, u32);
  12432. #if WASM_ENABLE_FAST_INTERP != 0
  12433. emit_uint32(loader_ctx, u32);
  12434. #endif
  12435. }
  12436. if (!check_array_type(module, type_idx, error_buf,
  12437. error_buf_size)) {
  12438. goto fail;
  12439. }
  12440. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12441. /* length */
  12442. POP_I32();
  12443. }
  12444. array_type = (WASMArrayType *)module->types[type_idx];
  12445. elem_type = array_type->elem_type;
  12446. if (opcode1 == WASM_OP_ARRAY_NEW
  12447. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12448. if (wasm_is_type_multi_byte_type(elem_type)) {
  12449. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12450. array_type->elem_ref_type,
  12451. wasm_reftype_struct_size(
  12452. array_type->elem_ref_type));
  12453. }
  12454. if (is_packed_type(elem_type)) {
  12455. elem_type = VALUE_TYPE_I32;
  12456. }
  12457. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12458. uint32 N = u32;
  12459. for (i = 0; i < N; i++) {
  12460. if (wasm_is_type_multi_byte_type(
  12461. elem_type)) {
  12462. bh_memcpy_s(
  12463. &wasm_ref_type, sizeof(WASMRefType),
  12464. array_type->elem_ref_type,
  12465. wasm_reftype_struct_size(
  12466. array_type->elem_ref_type));
  12467. }
  12468. POP_REF(elem_type);
  12469. }
  12470. }
  12471. else
  12472. POP_REF(elem_type);
  12473. }
  12474. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12475. /* offset of data segment */
  12476. POP_I32();
  12477. if (u32 >= module->data_seg_count) {
  12478. set_error_buf(error_buf, error_buf_size,
  12479. "unknown data segment");
  12480. goto fail;
  12481. }
  12482. if (wasm_is_type_reftype(elem_type)) {
  12483. set_error_buf(error_buf, error_buf_size,
  12484. "array elem type mismatch");
  12485. goto fail;
  12486. }
  12487. }
  12488. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12489. WASMTableSeg *table_seg =
  12490. module->table_segments + u32;
  12491. /* offset of element segment */
  12492. POP_I32();
  12493. if (u32 >= module->table_seg_count) {
  12494. set_error_buf(error_buf, error_buf_size,
  12495. "unknown element segment");
  12496. goto fail;
  12497. }
  12498. if (!wasm_reftype_is_subtype_of(
  12499. table_seg->elem_type,
  12500. table_seg->elem_ref_type, elem_type,
  12501. array_type->elem_ref_type, module->types,
  12502. module->type_count)) {
  12503. set_error_buf(error_buf, error_buf_size,
  12504. "array elem type mismatch");
  12505. goto fail;
  12506. }
  12507. }
  12508. /* PUSH array obj, (ref $t) */
  12509. wasm_set_refheaptype_typeidx(
  12510. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12511. PUSH_REF(wasm_ref_type.ref_type);
  12512. break;
  12513. }
  12514. case WASM_OP_ARRAY_GET:
  12515. case WASM_OP_ARRAY_GET_S:
  12516. case WASM_OP_ARRAY_GET_U:
  12517. case WASM_OP_ARRAY_SET:
  12518. {
  12519. uint8 elem_type;
  12520. WASMArrayType *array_type;
  12521. WASMRefType *ref_type = NULL;
  12522. read_leb_uint32(p, p_end, type_idx);
  12523. #if WASM_ENABLE_FAST_INTERP != 0
  12524. emit_uint32(loader_ctx, type_idx);
  12525. #endif
  12526. if (!check_array_type(module, type_idx, error_buf,
  12527. error_buf_size)) {
  12528. goto fail;
  12529. }
  12530. array_type = (WASMArrayType *)module->types[type_idx];
  12531. if (opcode1 == WASM_OP_ARRAY_SET
  12532. && !(array_type->elem_flags & 1)) {
  12533. set_error_buf(error_buf, error_buf_size,
  12534. "array is immutable");
  12535. goto fail;
  12536. }
  12537. elem_type = array_type->elem_type;
  12538. if (is_packed_type(elem_type)) {
  12539. if (opcode1 != WASM_OP_ARRAY_GET_S
  12540. && opcode1 != WASM_OP_ARRAY_GET_U
  12541. && opcode1 != WASM_OP_ARRAY_SET) {
  12542. set_error_buf(error_buf, error_buf_size,
  12543. "type mismatch");
  12544. goto fail;
  12545. }
  12546. else {
  12547. elem_type = VALUE_TYPE_I32;
  12548. }
  12549. }
  12550. ref_type = array_type->elem_ref_type;
  12551. if (opcode1 == WASM_OP_ARRAY_SET) {
  12552. /* POP elem to set */
  12553. if (wasm_is_type_multi_byte_type(elem_type)) {
  12554. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12555. ref_type,
  12556. wasm_reftype_struct_size(ref_type));
  12557. }
  12558. POP_REF(elem_type);
  12559. }
  12560. /* elem idx */
  12561. POP_I32();
  12562. /* POP array obj, (ref null $t) */
  12563. wasm_set_refheaptype_typeidx(
  12564. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12565. POP_REF(wasm_ref_type.ref_type);
  12566. if (opcode1 != WASM_OP_ARRAY_SET) {
  12567. /* PUSH elem */
  12568. if (wasm_is_type_multi_byte_type(elem_type)) {
  12569. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12570. ref_type,
  12571. wasm_reftype_struct_size(ref_type));
  12572. }
  12573. PUSH_REF(elem_type);
  12574. }
  12575. break;
  12576. }
  12577. case WASM_OP_ARRAY_LEN:
  12578. {
  12579. POP_REF(REF_TYPE_ARRAYREF);
  12580. /* length */
  12581. PUSH_I32();
  12582. break;
  12583. }
  12584. case WASM_OP_ARRAY_FILL:
  12585. {
  12586. WASMArrayType *array_type;
  12587. uint8 elem_type;
  12588. /* typeidx */
  12589. read_leb_uint32(p, p_end, type_idx);
  12590. #if WASM_ENABLE_FAST_INTERP != 0
  12591. emit_uint32(loader_ctx, type_idx);
  12592. #endif
  12593. if (!check_array_type(module, type_idx, error_buf,
  12594. error_buf_size)) {
  12595. goto fail;
  12596. }
  12597. array_type = (WASMArrayType *)module->types[type_idx];
  12598. if (!(array_type->elem_flags & 1)) {
  12599. set_error_buf(error_buf, error_buf_size,
  12600. "array is immutable");
  12601. goto fail;
  12602. }
  12603. elem_type = array_type->elem_type;
  12604. if (is_packed_type(elem_type)) {
  12605. elem_type = VALUE_TYPE_I32;
  12606. }
  12607. POP_I32(); /* length */
  12608. #if WASM_ENABLE_FAST_INTERP != 0
  12609. POP_OFFSET_TYPE(elem_type);
  12610. #endif
  12611. POP_TYPE(elem_type);
  12612. POP_I32(); /* start */
  12613. /* POP array obj, (ref null $t) */
  12614. wasm_set_refheaptype_typeidx(
  12615. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12616. POP_REF(wasm_ref_type.ref_type);
  12617. break;
  12618. }
  12619. case WASM_OP_ARRAY_COPY:
  12620. {
  12621. uint32 src_type_idx;
  12622. uint8 src_elem_type, dst_elem_type;
  12623. WASMRefType src_ref_type = { 0 },
  12624. *src_elem_ref_type = NULL;
  12625. WASMRefType dst_ref_type = { 0 },
  12626. *dst_elem_ref_type = NULL;
  12627. WASMArrayType *array_type;
  12628. /* typeidx1 */
  12629. read_leb_uint32(p, p_end, type_idx);
  12630. #if WASM_ENABLE_FAST_INTERP != 0
  12631. emit_uint32(loader_ctx, type_idx);
  12632. #endif
  12633. /* typeidx2 */
  12634. read_leb_uint32(p, p_end, src_type_idx);
  12635. #if WASM_ENABLE_FAST_INTERP != 0
  12636. emit_uint32(loader_ctx, src_type_idx);
  12637. #endif
  12638. if (!check_array_type(module, type_idx, error_buf,
  12639. error_buf_size)) {
  12640. goto fail;
  12641. }
  12642. if (!check_array_type(module, src_type_idx, error_buf,
  12643. error_buf_size)) {
  12644. goto fail;
  12645. }
  12646. POP_I32();
  12647. POP_I32();
  12648. /* POP array obj, (ref null $t) */
  12649. wasm_set_refheaptype_typeidx(
  12650. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12651. POP_REF(wasm_ref_type.ref_type);
  12652. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12653. &wasm_ref_type,
  12654. wasm_reftype_struct_size(&wasm_ref_type));
  12655. POP_I32();
  12656. /* POP array obj, (ref null $t) */
  12657. wasm_set_refheaptype_typeidx(
  12658. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12659. POP_REF(wasm_ref_type.ref_type);
  12660. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12661. &wasm_ref_type,
  12662. wasm_reftype_struct_size(&wasm_ref_type));
  12663. array_type = (WASMArrayType *)module->types[type_idx];
  12664. if (!(array_type->elem_flags & 1)) {
  12665. set_error_buf(error_buf, error_buf_size,
  12666. "destination array is immutable");
  12667. goto fail;
  12668. }
  12669. dst_elem_type = array_type->elem_type;
  12670. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12671. dst_elem_ref_type = array_type->elem_ref_type;
  12672. }
  12673. array_type =
  12674. (WASMArrayType *)module->types[src_type_idx];
  12675. src_elem_type = array_type->elem_type;
  12676. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12677. src_elem_ref_type = array_type->elem_ref_type;
  12678. }
  12679. if (!wasm_reftype_is_subtype_of(
  12680. src_elem_type, src_elem_ref_type, dst_elem_type,
  12681. dst_elem_ref_type, module->types,
  12682. module->type_count)) {
  12683. set_error_buf(error_buf, error_buf_size,
  12684. "array types do not match");
  12685. goto fail;
  12686. }
  12687. break;
  12688. }
  12689. case WASM_OP_REF_I31:
  12690. {
  12691. POP_I32();
  12692. wasm_set_refheaptype_common(
  12693. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12694. PUSH_REF(wasm_ref_type.ref_type);
  12695. break;
  12696. }
  12697. case WASM_OP_I31_GET_S:
  12698. case WASM_OP_I31_GET_U:
  12699. {
  12700. POP_REF(REF_TYPE_I31REF);
  12701. PUSH_I32();
  12702. break;
  12703. }
  12704. case WASM_OP_REF_TEST:
  12705. case WASM_OP_REF_CAST:
  12706. case WASM_OP_REF_TEST_NULLABLE:
  12707. case WASM_OP_REF_CAST_NULLABLE:
  12708. {
  12709. uint8 type;
  12710. read_leb_int32(p, p_end, heap_type);
  12711. #if WASM_ENABLE_FAST_INTERP != 0
  12712. emit_uint32(loader_ctx, (uint32)heap_type);
  12713. #endif
  12714. if (heap_type >= 0) {
  12715. if (!check_type_index(module, module->type_count,
  12716. heap_type, error_buf,
  12717. error_buf_size)) {
  12718. goto fail;
  12719. }
  12720. }
  12721. else {
  12722. if (!wasm_is_valid_heap_type(heap_type)) {
  12723. set_error_buf(error_buf, error_buf_size,
  12724. "unknown type");
  12725. goto fail;
  12726. }
  12727. }
  12728. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12729. &wasm_ref_type, error_buf,
  12730. error_buf_size)) {
  12731. goto fail;
  12732. }
  12733. if (opcode1 == WASM_OP_REF_TEST
  12734. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12735. PUSH_I32();
  12736. else {
  12737. bool nullable =
  12738. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12739. : false;
  12740. if (heap_type >= 0 || !nullable) {
  12741. wasm_set_refheaptype_typeidx(
  12742. &wasm_ref_type.ref_ht_typeidx, nullable,
  12743. heap_type);
  12744. PUSH_REF(wasm_ref_type.ref_type);
  12745. }
  12746. else {
  12747. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12748. }
  12749. }
  12750. break;
  12751. }
  12752. case WASM_OP_BR_ON_CAST:
  12753. case WASM_OP_BR_ON_CAST_FAIL:
  12754. {
  12755. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12756. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12757. uint8 type_tmp, castflags;
  12758. uint32 depth;
  12759. int32 heap_type_dst;
  12760. bool src_nullable, dst_nullable;
  12761. CHECK_BUF(p, p_end, 1);
  12762. castflags = read_uint8(p);
  12763. #if WASM_ENABLE_FAST_INTERP != 0
  12764. /* Emit heap_type firstly */
  12765. emit_byte(loader_ctx, castflags);
  12766. #endif
  12767. p_org = p;
  12768. read_leb_uint32(p, p_end, depth);
  12769. read_leb_int32(p, p_end, heap_type);
  12770. #if WASM_ENABLE_FAST_INTERP != 0
  12771. /* Emit heap_type firstly */
  12772. emit_uint32(loader_ctx, (uint32)heap_type);
  12773. #endif
  12774. read_leb_int32(p, p_end, heap_type_dst);
  12775. #if WASM_ENABLE_FAST_INTERP != 0
  12776. /* Emit heap_type firstly */
  12777. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12778. #endif
  12779. (void)depth;
  12780. /*
  12781. * castflags should be 0~3:
  12782. * 0: (non-null, non-null)
  12783. * 1: (null, non-null)
  12784. * 2: (non-null, null)
  12785. * 3: (null, null)
  12786. */
  12787. if (castflags > 3) {
  12788. set_error_buf(error_buf, error_buf_size,
  12789. "invalid castflags");
  12790. break;
  12791. }
  12792. src_nullable =
  12793. (castflags == 1) || (castflags == 3) ? true : false;
  12794. dst_nullable =
  12795. (castflags == 2) || (castflags == 3) ? true : false;
  12796. /* Pop and backup the stack top's ref type */
  12797. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12798. &ref_type_tmp, error_buf,
  12799. error_buf_size)) {
  12800. goto fail;
  12801. }
  12802. /* The reference type rt1 must be valid */
  12803. if (!init_ref_type(module, &ref_type1, src_nullable,
  12804. heap_type, error_buf,
  12805. error_buf_size)) {
  12806. goto fail;
  12807. }
  12808. /* The reference type rt2 must be valid. */
  12809. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12810. heap_type_dst, error_buf,
  12811. error_buf_size)) {
  12812. goto fail;
  12813. }
  12814. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12815. &ref_type2);
  12816. /* The reference type rt2 must match rt1. */
  12817. if (!wasm_reftype_is_subtype_of(
  12818. ref_type2.ref_type, &ref_type2,
  12819. ref_type1.ref_type, &ref_type1, module->types,
  12820. module->type_count)) {
  12821. set_error_buf(error_buf, error_buf_size,
  12822. "type mismatch");
  12823. goto fail;
  12824. }
  12825. p = p_org;
  12826. /* Push ref type casted for branch block check */
  12827. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12828. /* The reference type rt2 must match rt′. */
  12829. type_tmp = ref_type2.ref_type;
  12830. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12831. bh_memcpy_s(
  12832. &wasm_ref_type,
  12833. wasm_reftype_struct_size(&ref_type2),
  12834. &ref_type2,
  12835. wasm_reftype_struct_size(&ref_type2));
  12836. }
  12837. }
  12838. else {
  12839. /* The reference type rt′1 must match rt′. */
  12840. type_tmp = ref_type_diff.ref_type;
  12841. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12842. bh_memcpy_s(
  12843. &wasm_ref_type,
  12844. wasm_reftype_struct_size(&ref_type_diff),
  12845. &ref_type_diff,
  12846. wasm_reftype_struct_size(&ref_type_diff));
  12847. }
  12848. }
  12849. PUSH_REF(type_tmp);
  12850. if (!(frame_csp_tmp = check_branch_block(
  12851. loader_ctx, &p, p_end, opcode, error_buf,
  12852. error_buf_size))) {
  12853. goto fail;
  12854. }
  12855. /* Ignore heap_types */
  12856. skip_leb_uint32(p, p_end);
  12857. skip_leb_uint32(p, p_end);
  12858. /* Restore the original stack top's ref type */
  12859. POP_REF(type_tmp);
  12860. #if WASM_ENABLE_FAST_INTERP != 0
  12861. /* Erase the opnd offset emitted by POP_REF() */
  12862. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12863. #endif
  12864. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12865. type_tmp = ref_type_diff.ref_type;
  12866. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12867. bh_memcpy_s(
  12868. &wasm_ref_type,
  12869. wasm_reftype_struct_size(&ref_type_diff),
  12870. &ref_type_diff,
  12871. wasm_reftype_struct_size(&ref_type_diff));
  12872. }
  12873. }
  12874. else {
  12875. type_tmp = ref_type2.ref_type;
  12876. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12877. bh_memcpy_s(
  12878. &wasm_ref_type,
  12879. wasm_reftype_struct_size(&ref_type2),
  12880. &ref_type2,
  12881. wasm_reftype_struct_size(&ref_type2));
  12882. }
  12883. }
  12884. PUSH_REF(type_tmp);
  12885. #if WASM_ENABLE_FAST_INTERP != 0
  12886. /* Erase the opnd offset emitted by PUSH_REF() */
  12887. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12888. #endif
  12889. break;
  12890. }
  12891. case WASM_OP_ANY_CONVERT_EXTERN:
  12892. {
  12893. uint8 type;
  12894. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12895. &wasm_ref_type, error_buf,
  12896. error_buf_size)) {
  12897. goto fail;
  12898. }
  12899. if (!(type == REF_TYPE_EXTERNREF
  12900. || (type == REF_TYPE_HT_NON_NULLABLE
  12901. && wasm_ref_type.ref_ht_common.heap_type
  12902. == HEAP_TYPE_EXTERN)
  12903. || type == VALUE_TYPE_ANY)) {
  12904. set_error_buf(error_buf, error_buf_size,
  12905. "type mismatch");
  12906. goto fail;
  12907. }
  12908. if (type == REF_TYPE_EXTERNREF)
  12909. type = REF_TYPE_ANYREF;
  12910. else {
  12911. wasm_ref_type.ref_ht_common.heap_type =
  12912. HEAP_TYPE_ANY;
  12913. }
  12914. PUSH_REF(type);
  12915. break;
  12916. }
  12917. case WASM_OP_EXTERN_CONVERT_ANY:
  12918. {
  12919. uint8 type;
  12920. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12921. &wasm_ref_type, error_buf,
  12922. error_buf_size)) {
  12923. goto fail;
  12924. }
  12925. if (type == REF_TYPE_EXTERNREF
  12926. || ((type == REF_TYPE_HT_NULLABLE
  12927. || type == REF_TYPE_HT_NON_NULLABLE)
  12928. && wasm_ref_type.ref_ht_common.heap_type
  12929. == HEAP_TYPE_EXTERN)) {
  12930. set_error_buf(error_buf, error_buf_size,
  12931. "type mismatch");
  12932. goto fail;
  12933. }
  12934. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12935. /* push (ref null extern) */
  12936. type = REF_TYPE_EXTERNREF;
  12937. }
  12938. else {
  12939. /* push (ref extern) */
  12940. type = REF_TYPE_HT_NON_NULLABLE;
  12941. wasm_set_refheaptype_common(
  12942. &wasm_ref_type.ref_ht_common, false,
  12943. HEAP_TYPE_EXTERN);
  12944. }
  12945. PUSH_REF(type);
  12946. break;
  12947. }
  12948. #if WASM_ENABLE_STRINGREF != 0
  12949. case WASM_OP_STRING_NEW_UTF8:
  12950. case WASM_OP_STRING_NEW_WTF16:
  12951. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12952. case WASM_OP_STRING_NEW_WTF8:
  12953. {
  12954. uint32 memidx;
  12955. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12956. func->has_memory_operations = true;
  12957. #endif
  12958. read_leb_uint32(p, p_end, memidx);
  12959. #if WASM_ENABLE_FAST_INTERP != 0
  12960. emit_uint32(loader_ctx, (uint32)memidx);
  12961. #endif
  12962. POP_I32();
  12963. POP_I32();
  12964. PUSH_REF(REF_TYPE_STRINGREF);
  12965. (void)memidx;
  12966. break;
  12967. }
  12968. case WASM_OP_STRING_CONST:
  12969. {
  12970. uint32 contents;
  12971. read_leb_uint32(p, p_end, contents);
  12972. #if WASM_ENABLE_FAST_INTERP != 0
  12973. emit_uint32(loader_ctx, (uint32)contents);
  12974. #endif
  12975. PUSH_REF(REF_TYPE_STRINGREF);
  12976. (void)contents;
  12977. break;
  12978. }
  12979. case WASM_OP_STRING_MEASURE_UTF8:
  12980. case WASM_OP_STRING_MEASURE_WTF8:
  12981. case WASM_OP_STRING_MEASURE_WTF16:
  12982. {
  12983. POP_STRINGREF();
  12984. PUSH_I32();
  12985. break;
  12986. }
  12987. case WASM_OP_STRING_ENCODE_UTF8:
  12988. case WASM_OP_STRING_ENCODE_WTF16:
  12989. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  12990. case WASM_OP_STRING_ENCODE_WTF8:
  12991. {
  12992. uint32 memidx;
  12993. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12994. func->has_memory_operations = true;
  12995. #endif
  12996. read_leb_uint32(p, p_end, memidx);
  12997. #if WASM_ENABLE_FAST_INTERP != 0
  12998. emit_uint32(loader_ctx, (uint32)memidx);
  12999. #endif
  13000. POP_I32();
  13001. POP_STRINGREF();
  13002. PUSH_I32();
  13003. (void)memidx;
  13004. break;
  13005. }
  13006. case WASM_OP_STRING_CONCAT:
  13007. {
  13008. POP_STRINGREF();
  13009. POP_STRINGREF();
  13010. PUSH_REF(REF_TYPE_STRINGREF);
  13011. break;
  13012. }
  13013. case WASM_OP_STRING_EQ:
  13014. {
  13015. POP_STRINGREF();
  13016. POP_STRINGREF();
  13017. PUSH_I32();
  13018. break;
  13019. }
  13020. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13021. {
  13022. POP_STRINGREF();
  13023. PUSH_I32();
  13024. break;
  13025. }
  13026. case WASM_OP_STRING_AS_WTF8:
  13027. {
  13028. POP_STRINGREF();
  13029. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13030. break;
  13031. }
  13032. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13033. {
  13034. POP_I32();
  13035. POP_I32();
  13036. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13037. PUSH_I32();
  13038. break;
  13039. }
  13040. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13041. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13042. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13043. {
  13044. uint32 memidx;
  13045. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13046. func->has_memory_operations = true;
  13047. #endif
  13048. read_leb_uint32(p, p_end, memidx);
  13049. #if WASM_ENABLE_FAST_INTERP != 0
  13050. emit_uint32(loader_ctx, (uint32)memidx);
  13051. #endif
  13052. POP_I32();
  13053. POP_I32();
  13054. POP_I32();
  13055. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13056. PUSH_I32();
  13057. PUSH_I32();
  13058. (void)memidx;
  13059. break;
  13060. }
  13061. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13062. {
  13063. POP_I32();
  13064. POP_I32();
  13065. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13066. PUSH_REF(REF_TYPE_STRINGREF);
  13067. break;
  13068. }
  13069. case WASM_OP_STRING_AS_WTF16:
  13070. {
  13071. POP_STRINGREF();
  13072. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13073. break;
  13074. }
  13075. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13076. {
  13077. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13078. PUSH_I32();
  13079. break;
  13080. }
  13081. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13082. {
  13083. POP_I32();
  13084. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13085. PUSH_I32();
  13086. break;
  13087. }
  13088. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13089. {
  13090. uint32 memidx;
  13091. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13092. func->has_memory_operations = true;
  13093. #endif
  13094. read_leb_uint32(p, p_end, memidx);
  13095. #if WASM_ENABLE_FAST_INTERP != 0
  13096. emit_uint32(loader_ctx, (uint32)memidx);
  13097. #endif
  13098. POP_I32();
  13099. POP_I32();
  13100. POP_I32();
  13101. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13102. PUSH_I32();
  13103. (void)memidx;
  13104. break;
  13105. }
  13106. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13107. {
  13108. POP_I32();
  13109. POP_I32();
  13110. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13111. PUSH_REF(REF_TYPE_STRINGREF);
  13112. break;
  13113. }
  13114. case WASM_OP_STRING_AS_ITER:
  13115. {
  13116. POP_STRINGREF();
  13117. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13118. break;
  13119. }
  13120. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13121. {
  13122. POP_REF(REF_TYPE_STRINGVIEWITER);
  13123. PUSH_I32();
  13124. break;
  13125. }
  13126. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13127. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13128. {
  13129. POP_I32();
  13130. POP_REF(REF_TYPE_STRINGVIEWITER);
  13131. PUSH_I32();
  13132. break;
  13133. }
  13134. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13135. {
  13136. POP_I32();
  13137. POP_REF(REF_TYPE_STRINGVIEWITER);
  13138. PUSH_REF(REF_TYPE_STRINGREF);
  13139. break;
  13140. }
  13141. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13142. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13143. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13144. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13145. {
  13146. POP_I32();
  13147. POP_I32();
  13148. POP_REF(REF_TYPE_ARRAYREF);
  13149. PUSH_REF(REF_TYPE_STRINGREF);
  13150. break;
  13151. }
  13152. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13153. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13154. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13155. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13156. {
  13157. POP_I32();
  13158. POP_REF(REF_TYPE_ARRAYREF);
  13159. POP_STRINGREF();
  13160. PUSH_I32();
  13161. break;
  13162. }
  13163. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13164. default:
  13165. set_error_buf_v(error_buf, error_buf_size,
  13166. "%s %02x %02x", "unsupported opcode",
  13167. 0xfb, opcode1);
  13168. goto fail;
  13169. }
  13170. break;
  13171. }
  13172. #endif /* end of WASM_ENABLE_GC != 0 */
  13173. case WASM_OP_MISC_PREFIX:
  13174. {
  13175. uint32 opcode1;
  13176. read_leb_uint32(p, p_end, opcode1);
  13177. #if WASM_ENABLE_FAST_INTERP != 0
  13178. emit_byte(loader_ctx, ((uint8)opcode1));
  13179. #endif
  13180. switch (opcode1) {
  13181. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13182. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13183. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13184. break;
  13185. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13186. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13187. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13188. break;
  13189. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13190. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13191. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13192. break;
  13193. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13194. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13195. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13196. break;
  13197. #if WASM_ENABLE_BULK_MEMORY != 0
  13198. case WASM_OP_MEMORY_INIT:
  13199. {
  13200. read_leb_uint32(p, p_end, data_seg_idx);
  13201. #if WASM_ENABLE_FAST_INTERP != 0
  13202. emit_uint32(loader_ctx, data_seg_idx);
  13203. #endif
  13204. if (module->import_memory_count == 0
  13205. && module->memory_count == 0)
  13206. goto fail_unknown_memory;
  13207. read_leb_uint32(p, p_end, memidx);
  13208. check_memidx(module, memidx);
  13209. if (data_seg_idx >= module->data_seg_count) {
  13210. set_error_buf_v(error_buf, error_buf_size,
  13211. "unknown data segment %d",
  13212. data_seg_idx);
  13213. goto fail;
  13214. }
  13215. if (module->data_seg_count1 == 0)
  13216. goto fail_data_cnt_sec_require;
  13217. POP_I32();
  13218. POP_I32();
  13219. POP_MEM_OFFSET();
  13220. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13221. func->has_memory_operations = true;
  13222. #endif
  13223. #if WASM_ENABLE_WAMR_COMPILER != 0
  13224. module->is_bulk_memory_used = true;
  13225. #endif
  13226. break;
  13227. }
  13228. case WASM_OP_DATA_DROP:
  13229. {
  13230. read_leb_uint32(p, p_end, data_seg_idx);
  13231. #if WASM_ENABLE_FAST_INTERP != 0
  13232. emit_uint32(loader_ctx, data_seg_idx);
  13233. #endif
  13234. if (data_seg_idx >= module->data_seg_count) {
  13235. set_error_buf(error_buf, error_buf_size,
  13236. "unknown data segment");
  13237. goto fail;
  13238. }
  13239. if (module->data_seg_count1 == 0)
  13240. goto fail_data_cnt_sec_require;
  13241. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13242. func->has_memory_operations = true;
  13243. #endif
  13244. #if WASM_ENABLE_WAMR_COMPILER != 0
  13245. module->is_bulk_memory_used = true;
  13246. #endif
  13247. break;
  13248. }
  13249. case WASM_OP_MEMORY_COPY:
  13250. {
  13251. CHECK_BUF(p, p_end, sizeof(int16));
  13252. /* check both src and dst memory index */
  13253. read_leb_uint32(p, p_end, memidx);
  13254. check_memidx(module, memidx);
  13255. read_leb_uint32(p, p_end, memidx);
  13256. check_memidx(module, memidx);
  13257. if (module->import_memory_count == 0
  13258. && module->memory_count == 0)
  13259. goto fail_unknown_memory;
  13260. POP_MEM_OFFSET();
  13261. POP_MEM_OFFSET();
  13262. POP_MEM_OFFSET();
  13263. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13264. func->has_memory_operations = true;
  13265. #endif
  13266. #if WASM_ENABLE_WAMR_COMPILER != 0
  13267. module->is_bulk_memory_used = true;
  13268. #endif
  13269. break;
  13270. }
  13271. case WASM_OP_MEMORY_FILL:
  13272. {
  13273. read_leb_uint32(p, p_end, memidx);
  13274. check_memidx(module, memidx);
  13275. if (module->import_memory_count == 0
  13276. && module->memory_count == 0) {
  13277. goto fail_unknown_memory;
  13278. }
  13279. POP_MEM_OFFSET();
  13280. POP_I32();
  13281. POP_MEM_OFFSET();
  13282. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13283. func->has_memory_operations = true;
  13284. #endif
  13285. #if WASM_ENABLE_WAMR_COMPILER != 0
  13286. module->is_bulk_memory_used = true;
  13287. #endif
  13288. break;
  13289. }
  13290. fail_unknown_memory:
  13291. set_error_buf(error_buf, error_buf_size,
  13292. "unknown memory 0");
  13293. goto fail;
  13294. fail_data_cnt_sec_require:
  13295. set_error_buf(error_buf, error_buf_size,
  13296. "data count section required");
  13297. goto fail;
  13298. #endif /* WASM_ENABLE_BULK_MEMORY */
  13299. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13300. case WASM_OP_TABLE_INIT:
  13301. {
  13302. uint8 seg_type = 0, tbl_type = 0;
  13303. #if WASM_ENABLE_GC != 0
  13304. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13305. #endif
  13306. read_leb_uint32(p, p_end, table_seg_idx);
  13307. read_leb_uint32(p, p_end, table_idx);
  13308. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13309. #if WASM_ENABLE_GC != 0
  13310. (void **)&tbl_ref_type,
  13311. #else
  13312. NULL,
  13313. #endif
  13314. error_buf, error_buf_size))
  13315. goto fail;
  13316. if (!get_table_seg_elem_type(module, table_seg_idx,
  13317. &seg_type,
  13318. #if WASM_ENABLE_GC != 0
  13319. (void **)&seg_ref_type,
  13320. #else
  13321. NULL,
  13322. #endif
  13323. error_buf, error_buf_size))
  13324. goto fail;
  13325. #if WASM_ENABLE_GC == 0
  13326. if (seg_type != tbl_type) {
  13327. set_error_buf(error_buf, error_buf_size,
  13328. "type mismatch");
  13329. goto fail;
  13330. }
  13331. #else
  13332. if (!wasm_reftype_is_subtype_of(
  13333. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13334. module->types, module->type_count)) {
  13335. set_error_buf(error_buf, error_buf_size,
  13336. "type mismatch");
  13337. goto fail;
  13338. }
  13339. #endif
  13340. #if WASM_ENABLE_FAST_INTERP != 0
  13341. emit_uint32(loader_ctx, table_seg_idx);
  13342. emit_uint32(loader_ctx, table_idx);
  13343. #endif
  13344. POP_I32();
  13345. POP_I32();
  13346. POP_I32();
  13347. #if WASM_ENABLE_WAMR_COMPILER != 0
  13348. module->is_ref_types_used = true;
  13349. #endif
  13350. break;
  13351. }
  13352. case WASM_OP_ELEM_DROP:
  13353. {
  13354. read_leb_uint32(p, p_end, table_seg_idx);
  13355. if (!get_table_seg_elem_type(module, table_seg_idx,
  13356. NULL, NULL, error_buf,
  13357. error_buf_size))
  13358. goto fail;
  13359. #if WASM_ENABLE_FAST_INTERP != 0
  13360. emit_uint32(loader_ctx, table_seg_idx);
  13361. #endif
  13362. #if WASM_ENABLE_WAMR_COMPILER != 0
  13363. module->is_ref_types_used = true;
  13364. #endif
  13365. break;
  13366. }
  13367. case WASM_OP_TABLE_COPY:
  13368. {
  13369. uint8 src_type, dst_type;
  13370. #if WASM_ENABLE_GC != 0
  13371. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13372. #endif
  13373. uint32 src_tbl_idx, dst_tbl_idx;
  13374. read_leb_uint32(p, p_end, dst_tbl_idx);
  13375. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13376. #if WASM_ENABLE_GC != 0
  13377. (void **)&dst_ref_type,
  13378. #else
  13379. NULL,
  13380. #endif
  13381. error_buf, error_buf_size))
  13382. goto fail;
  13383. read_leb_uint32(p, p_end, src_tbl_idx);
  13384. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13385. #if WASM_ENABLE_GC != 0
  13386. (void **)&src_ref_type,
  13387. #else
  13388. NULL,
  13389. #endif
  13390. error_buf, error_buf_size))
  13391. goto fail;
  13392. #if WASM_ENABLE_GC == 0
  13393. if (src_type != dst_type) {
  13394. set_error_buf(error_buf, error_buf_size,
  13395. "type mismatch");
  13396. goto fail;
  13397. }
  13398. #else
  13399. if (!wasm_reftype_is_subtype_of(
  13400. src_type, src_ref_type, dst_type, dst_ref_type,
  13401. module->types, module->type_count)) {
  13402. set_error_buf(error_buf, error_buf_size,
  13403. "type mismatch");
  13404. goto fail;
  13405. }
  13406. #endif
  13407. #if WASM_ENABLE_FAST_INTERP != 0
  13408. emit_uint32(loader_ctx, dst_tbl_idx);
  13409. emit_uint32(loader_ctx, src_tbl_idx);
  13410. #endif
  13411. POP_I32();
  13412. POP_I32();
  13413. POP_I32();
  13414. #if WASM_ENABLE_WAMR_COMPILER != 0
  13415. module->is_ref_types_used = true;
  13416. #endif
  13417. break;
  13418. }
  13419. case WASM_OP_TABLE_SIZE:
  13420. {
  13421. read_leb_uint32(p, p_end, table_idx);
  13422. /* TODO: shall we create a new function to check
  13423. table idx instead of using below function? */
  13424. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13425. error_buf, error_buf_size))
  13426. goto fail;
  13427. #if WASM_ENABLE_FAST_INTERP != 0
  13428. emit_uint32(loader_ctx, table_idx);
  13429. #endif
  13430. PUSH_I32();
  13431. #if WASM_ENABLE_WAMR_COMPILER != 0
  13432. module->is_ref_types_used = true;
  13433. #endif
  13434. break;
  13435. }
  13436. case WASM_OP_TABLE_GROW:
  13437. case WASM_OP_TABLE_FILL:
  13438. {
  13439. uint8 decl_type;
  13440. #if WASM_ENABLE_GC != 0
  13441. WASMRefType *ref_type = NULL;
  13442. #endif
  13443. read_leb_uint32(p, p_end, table_idx);
  13444. if (!get_table_elem_type(module, table_idx, &decl_type,
  13445. #if WASM_ENABLE_GC != 0
  13446. (void **)&ref_type,
  13447. #else
  13448. NULL,
  13449. #endif
  13450. error_buf, error_buf_size))
  13451. goto fail;
  13452. #if WASM_ENABLE_GC != 0
  13453. if (wasm_is_type_multi_byte_type(decl_type)) {
  13454. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13455. ref_type,
  13456. wasm_reftype_struct_size(ref_type));
  13457. }
  13458. #endif
  13459. if (opcode1 == WASM_OP_TABLE_GROW) {
  13460. if (table_idx < module->import_table_count) {
  13461. module->import_tables[table_idx]
  13462. .u.table.table_type.possible_grow = true;
  13463. }
  13464. else {
  13465. module
  13466. ->tables[table_idx
  13467. - module->import_table_count]
  13468. .table_type.possible_grow = true;
  13469. }
  13470. }
  13471. #if WASM_ENABLE_FAST_INTERP != 0
  13472. emit_uint32(loader_ctx, table_idx);
  13473. #endif
  13474. POP_I32();
  13475. #if WASM_ENABLE_FAST_INTERP != 0
  13476. POP_OFFSET_TYPE(decl_type);
  13477. #endif
  13478. POP_TYPE(decl_type);
  13479. if (opcode1 == WASM_OP_TABLE_GROW)
  13480. PUSH_I32();
  13481. else
  13482. POP_I32();
  13483. #if WASM_ENABLE_WAMR_COMPILER != 0
  13484. module->is_ref_types_used = true;
  13485. #endif
  13486. break;
  13487. }
  13488. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13489. default:
  13490. set_error_buf_v(error_buf, error_buf_size,
  13491. "%s %02x %02x", "unsupported opcode",
  13492. 0xfc, opcode1);
  13493. goto fail;
  13494. }
  13495. break;
  13496. }
  13497. #if WASM_ENABLE_SIMD != 0
  13498. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13499. case WASM_OP_SIMD_PREFIX:
  13500. {
  13501. uint32 opcode1;
  13502. #if WASM_ENABLE_WAMR_COMPILER != 0
  13503. /* Mark the SIMD instruction is used in this module */
  13504. module->is_simd_used = true;
  13505. #endif
  13506. read_leb_uint32(p, p_end, opcode1);
  13507. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13508. */
  13509. switch (opcode1) {
  13510. /* memory instruction */
  13511. case SIMD_v128_load:
  13512. case SIMD_v128_load8x8_s:
  13513. case SIMD_v128_load8x8_u:
  13514. case SIMD_v128_load16x4_s:
  13515. case SIMD_v128_load16x4_u:
  13516. case SIMD_v128_load32x2_s:
  13517. case SIMD_v128_load32x2_u:
  13518. case SIMD_v128_load8_splat:
  13519. case SIMD_v128_load16_splat:
  13520. case SIMD_v128_load32_splat:
  13521. case SIMD_v128_load64_splat:
  13522. {
  13523. CHECK_MEMORY();
  13524. read_leb_uint32(p, p_end, align); /* align */
  13525. if (!check_simd_memory_access_align(
  13526. opcode1, align, error_buf, error_buf_size)) {
  13527. goto fail;
  13528. }
  13529. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13530. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13531. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13532. func->has_memory_operations = true;
  13533. #endif
  13534. break;
  13535. }
  13536. case SIMD_v128_store:
  13537. {
  13538. CHECK_MEMORY();
  13539. read_leb_uint32(p, p_end, align); /* align */
  13540. if (!check_simd_memory_access_align(
  13541. opcode1, align, error_buf, error_buf_size)) {
  13542. goto fail;
  13543. }
  13544. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13545. POP_V128();
  13546. POP_MEM_OFFSET();
  13547. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13548. func->has_memory_operations = true;
  13549. #endif
  13550. break;
  13551. }
  13552. /* basic operation */
  13553. case SIMD_v128_const:
  13554. {
  13555. CHECK_BUF1(p, p_end, 16);
  13556. p += 16;
  13557. PUSH_V128();
  13558. break;
  13559. }
  13560. case SIMD_v8x16_shuffle:
  13561. {
  13562. V128 mask;
  13563. CHECK_BUF1(p, p_end, 16);
  13564. mask = read_i8x16(p, error_buf, error_buf_size);
  13565. p += 16;
  13566. if (!check_simd_shuffle_mask(mask, error_buf,
  13567. error_buf_size)) {
  13568. goto fail;
  13569. }
  13570. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13571. break;
  13572. }
  13573. case SIMD_v8x16_swizzle:
  13574. {
  13575. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13576. break;
  13577. }
  13578. /* splat operation */
  13579. case SIMD_i8x16_splat:
  13580. case SIMD_i16x8_splat:
  13581. case SIMD_i32x4_splat:
  13582. case SIMD_i64x2_splat:
  13583. case SIMD_f32x4_splat:
  13584. case SIMD_f64x2_splat:
  13585. {
  13586. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13587. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13588. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13589. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13590. VALUE_TYPE_V128);
  13591. break;
  13592. }
  13593. /* lane operation */
  13594. case SIMD_i8x16_extract_lane_s:
  13595. case SIMD_i8x16_extract_lane_u:
  13596. case SIMD_i8x16_replace_lane:
  13597. case SIMD_i16x8_extract_lane_s:
  13598. case SIMD_i16x8_extract_lane_u:
  13599. case SIMD_i16x8_replace_lane:
  13600. case SIMD_i32x4_extract_lane:
  13601. case SIMD_i32x4_replace_lane:
  13602. case SIMD_i64x2_extract_lane:
  13603. case SIMD_i64x2_replace_lane:
  13604. case SIMD_f32x4_extract_lane:
  13605. case SIMD_f32x4_replace_lane:
  13606. case SIMD_f64x2_extract_lane:
  13607. case SIMD_f64x2_replace_lane:
  13608. {
  13609. uint8 lane;
  13610. /* clang-format off */
  13611. uint8 replace[] = {
  13612. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13613. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13614. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13615. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13616. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13617. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13618. };
  13619. uint8 push_type[] = {
  13620. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13621. VALUE_TYPE_V128,
  13622. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13623. VALUE_TYPE_V128,
  13624. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13625. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13626. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13627. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13628. };
  13629. /* clang-format on */
  13630. CHECK_BUF(p, p_end, 1);
  13631. lane = read_uint8(p);
  13632. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13633. error_buf_size)) {
  13634. goto fail;
  13635. }
  13636. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13637. if (!(wasm_loader_pop_frame_ref(
  13638. loader_ctx,
  13639. replace[opcode1
  13640. - SIMD_i8x16_extract_lane_s],
  13641. error_buf, error_buf_size)))
  13642. goto fail;
  13643. }
  13644. POP_AND_PUSH(
  13645. VALUE_TYPE_V128,
  13646. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13647. break;
  13648. }
  13649. /* i8x16 compare operation */
  13650. case SIMD_i8x16_eq:
  13651. case SIMD_i8x16_ne:
  13652. case SIMD_i8x16_lt_s:
  13653. case SIMD_i8x16_lt_u:
  13654. case SIMD_i8x16_gt_s:
  13655. case SIMD_i8x16_gt_u:
  13656. case SIMD_i8x16_le_s:
  13657. case SIMD_i8x16_le_u:
  13658. case SIMD_i8x16_ge_s:
  13659. case SIMD_i8x16_ge_u:
  13660. /* i16x8 compare operation */
  13661. case SIMD_i16x8_eq:
  13662. case SIMD_i16x8_ne:
  13663. case SIMD_i16x8_lt_s:
  13664. case SIMD_i16x8_lt_u:
  13665. case SIMD_i16x8_gt_s:
  13666. case SIMD_i16x8_gt_u:
  13667. case SIMD_i16x8_le_s:
  13668. case SIMD_i16x8_le_u:
  13669. case SIMD_i16x8_ge_s:
  13670. case SIMD_i16x8_ge_u:
  13671. /* i32x4 compare operation */
  13672. case SIMD_i32x4_eq:
  13673. case SIMD_i32x4_ne:
  13674. case SIMD_i32x4_lt_s:
  13675. case SIMD_i32x4_lt_u:
  13676. case SIMD_i32x4_gt_s:
  13677. case SIMD_i32x4_gt_u:
  13678. case SIMD_i32x4_le_s:
  13679. case SIMD_i32x4_le_u:
  13680. case SIMD_i32x4_ge_s:
  13681. case SIMD_i32x4_ge_u:
  13682. /* f32x4 compare operation */
  13683. case SIMD_f32x4_eq:
  13684. case SIMD_f32x4_ne:
  13685. case SIMD_f32x4_lt:
  13686. case SIMD_f32x4_gt:
  13687. case SIMD_f32x4_le:
  13688. case SIMD_f32x4_ge:
  13689. /* f64x2 compare operation */
  13690. case SIMD_f64x2_eq:
  13691. case SIMD_f64x2_ne:
  13692. case SIMD_f64x2_lt:
  13693. case SIMD_f64x2_gt:
  13694. case SIMD_f64x2_le:
  13695. case SIMD_f64x2_ge:
  13696. {
  13697. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13698. break;
  13699. }
  13700. /* v128 operation */
  13701. case SIMD_v128_not:
  13702. {
  13703. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13704. break;
  13705. }
  13706. case SIMD_v128_and:
  13707. case SIMD_v128_andnot:
  13708. case SIMD_v128_or:
  13709. case SIMD_v128_xor:
  13710. {
  13711. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13712. break;
  13713. }
  13714. case SIMD_v128_bitselect:
  13715. {
  13716. POP_V128();
  13717. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13718. break;
  13719. }
  13720. case SIMD_v128_any_true:
  13721. {
  13722. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13723. break;
  13724. }
  13725. /* Load Lane Operation */
  13726. case SIMD_v128_load8_lane:
  13727. case SIMD_v128_load16_lane:
  13728. case SIMD_v128_load32_lane:
  13729. case SIMD_v128_load64_lane:
  13730. case SIMD_v128_store8_lane:
  13731. case SIMD_v128_store16_lane:
  13732. case SIMD_v128_store32_lane:
  13733. case SIMD_v128_store64_lane:
  13734. {
  13735. uint8 lane;
  13736. CHECK_MEMORY();
  13737. read_leb_uint32(p, p_end, align); /* align */
  13738. if (!check_simd_memory_access_align(
  13739. opcode1, align, error_buf, error_buf_size)) {
  13740. goto fail;
  13741. }
  13742. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13743. CHECK_BUF(p, p_end, 1);
  13744. lane = read_uint8(p);
  13745. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13746. error_buf_size)) {
  13747. goto fail;
  13748. }
  13749. POP_V128();
  13750. POP_MEM_OFFSET();
  13751. if (opcode1 < SIMD_v128_store8_lane) {
  13752. PUSH_V128();
  13753. }
  13754. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13755. func->has_memory_operations = true;
  13756. #endif
  13757. break;
  13758. }
  13759. case SIMD_v128_load32_zero:
  13760. case SIMD_v128_load64_zero:
  13761. {
  13762. CHECK_MEMORY();
  13763. read_leb_uint32(p, p_end, align); /* align */
  13764. if (!check_simd_memory_access_align(
  13765. opcode1, align, error_buf, error_buf_size)) {
  13766. goto fail;
  13767. }
  13768. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13769. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13770. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13771. func->has_memory_operations = true;
  13772. #endif
  13773. break;
  13774. }
  13775. /* Float conversion */
  13776. case SIMD_f32x4_demote_f64x2_zero:
  13777. case SIMD_f64x2_promote_low_f32x4_zero:
  13778. {
  13779. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13780. break;
  13781. }
  13782. /* i8x16 Operation */
  13783. case SIMD_i8x16_abs:
  13784. case SIMD_i8x16_neg:
  13785. case SIMD_i8x16_popcnt:
  13786. {
  13787. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13788. break;
  13789. }
  13790. case SIMD_i8x16_all_true:
  13791. case SIMD_i8x16_bitmask:
  13792. {
  13793. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13794. break;
  13795. }
  13796. case SIMD_i8x16_narrow_i16x8_s:
  13797. case SIMD_i8x16_narrow_i16x8_u:
  13798. {
  13799. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13800. break;
  13801. }
  13802. case SIMD_f32x4_ceil:
  13803. case SIMD_f32x4_floor:
  13804. case SIMD_f32x4_trunc:
  13805. case SIMD_f32x4_nearest:
  13806. {
  13807. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13808. break;
  13809. }
  13810. case SIMD_i8x16_shl:
  13811. case SIMD_i8x16_shr_s:
  13812. case SIMD_i8x16_shr_u:
  13813. {
  13814. POP_I32();
  13815. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13816. break;
  13817. }
  13818. case SIMD_i8x16_add:
  13819. case SIMD_i8x16_add_sat_s:
  13820. case SIMD_i8x16_add_sat_u:
  13821. case SIMD_i8x16_sub:
  13822. case SIMD_i8x16_sub_sat_s:
  13823. case SIMD_i8x16_sub_sat_u:
  13824. {
  13825. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13826. break;
  13827. }
  13828. case SIMD_f64x2_ceil:
  13829. case SIMD_f64x2_floor:
  13830. {
  13831. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13832. break;
  13833. }
  13834. case SIMD_i8x16_min_s:
  13835. case SIMD_i8x16_min_u:
  13836. case SIMD_i8x16_max_s:
  13837. case SIMD_i8x16_max_u:
  13838. {
  13839. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13840. break;
  13841. }
  13842. case SIMD_f64x2_trunc:
  13843. {
  13844. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13845. break;
  13846. }
  13847. case SIMD_i8x16_avgr_u:
  13848. {
  13849. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13850. break;
  13851. }
  13852. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13853. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13854. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13855. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13856. /* i16x8 operation */
  13857. case SIMD_i16x8_abs:
  13858. case SIMD_i16x8_neg:
  13859. {
  13860. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13861. break;
  13862. }
  13863. case SIMD_i16x8_q15mulr_sat_s:
  13864. {
  13865. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13866. break;
  13867. }
  13868. case SIMD_i16x8_all_true:
  13869. case SIMD_i16x8_bitmask:
  13870. {
  13871. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13872. break;
  13873. }
  13874. case SIMD_i16x8_narrow_i32x4_s:
  13875. case SIMD_i16x8_narrow_i32x4_u:
  13876. {
  13877. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13878. break;
  13879. }
  13880. case SIMD_i16x8_extend_low_i8x16_s:
  13881. case SIMD_i16x8_extend_high_i8x16_s:
  13882. case SIMD_i16x8_extend_low_i8x16_u:
  13883. case SIMD_i16x8_extend_high_i8x16_u:
  13884. {
  13885. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13886. break;
  13887. }
  13888. case SIMD_i16x8_shl:
  13889. case SIMD_i16x8_shr_s:
  13890. case SIMD_i16x8_shr_u:
  13891. {
  13892. POP_I32();
  13893. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13894. break;
  13895. }
  13896. case SIMD_i16x8_add:
  13897. case SIMD_i16x8_add_sat_s:
  13898. case SIMD_i16x8_add_sat_u:
  13899. case SIMD_i16x8_sub:
  13900. case SIMD_i16x8_sub_sat_s:
  13901. case SIMD_i16x8_sub_sat_u:
  13902. {
  13903. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13904. break;
  13905. }
  13906. case SIMD_f64x2_nearest:
  13907. {
  13908. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13909. break;
  13910. }
  13911. case SIMD_i16x8_mul:
  13912. case SIMD_i16x8_min_s:
  13913. case SIMD_i16x8_min_u:
  13914. case SIMD_i16x8_max_s:
  13915. case SIMD_i16x8_max_u:
  13916. case SIMD_i16x8_avgr_u:
  13917. case SIMD_i16x8_extmul_low_i8x16_s:
  13918. case SIMD_i16x8_extmul_high_i8x16_s:
  13919. case SIMD_i16x8_extmul_low_i8x16_u:
  13920. case SIMD_i16x8_extmul_high_i8x16_u:
  13921. {
  13922. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13923. break;
  13924. }
  13925. /* i32x4 operation */
  13926. case SIMD_i32x4_abs:
  13927. case SIMD_i32x4_neg:
  13928. {
  13929. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13930. break;
  13931. }
  13932. case SIMD_i32x4_all_true:
  13933. case SIMD_i32x4_bitmask:
  13934. {
  13935. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13936. break;
  13937. }
  13938. case SIMD_i32x4_extend_low_i16x8_s:
  13939. case SIMD_i32x4_extend_high_i16x8_s:
  13940. case SIMD_i32x4_extend_low_i16x8_u:
  13941. case SIMD_i32x4_extend_high_i16x8_u:
  13942. {
  13943. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13944. break;
  13945. }
  13946. case SIMD_i32x4_shl:
  13947. case SIMD_i32x4_shr_s:
  13948. case SIMD_i32x4_shr_u:
  13949. {
  13950. POP_I32();
  13951. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13952. break;
  13953. }
  13954. case SIMD_i32x4_add:
  13955. case SIMD_i32x4_sub:
  13956. case SIMD_i32x4_mul:
  13957. case SIMD_i32x4_min_s:
  13958. case SIMD_i32x4_min_u:
  13959. case SIMD_i32x4_max_s:
  13960. case SIMD_i32x4_max_u:
  13961. case SIMD_i32x4_dot_i16x8_s:
  13962. case SIMD_i32x4_extmul_low_i16x8_s:
  13963. case SIMD_i32x4_extmul_high_i16x8_s:
  13964. case SIMD_i32x4_extmul_low_i16x8_u:
  13965. case SIMD_i32x4_extmul_high_i16x8_u:
  13966. {
  13967. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13968. break;
  13969. }
  13970. /* i64x2 operation */
  13971. case SIMD_i64x2_abs:
  13972. case SIMD_i64x2_neg:
  13973. {
  13974. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13975. break;
  13976. }
  13977. case SIMD_i64x2_all_true:
  13978. case SIMD_i64x2_bitmask:
  13979. {
  13980. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13981. break;
  13982. }
  13983. case SIMD_i64x2_extend_low_i32x4_s:
  13984. case SIMD_i64x2_extend_high_i32x4_s:
  13985. case SIMD_i64x2_extend_low_i32x4_u:
  13986. case SIMD_i64x2_extend_high_i32x4_u:
  13987. {
  13988. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13989. break;
  13990. }
  13991. case SIMD_i64x2_shl:
  13992. case SIMD_i64x2_shr_s:
  13993. case SIMD_i64x2_shr_u:
  13994. {
  13995. POP_I32();
  13996. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13997. break;
  13998. }
  13999. case SIMD_i64x2_add:
  14000. case SIMD_i64x2_sub:
  14001. case SIMD_i64x2_mul:
  14002. case SIMD_i64x2_eq:
  14003. case SIMD_i64x2_ne:
  14004. case SIMD_i64x2_lt_s:
  14005. case SIMD_i64x2_gt_s:
  14006. case SIMD_i64x2_le_s:
  14007. case SIMD_i64x2_ge_s:
  14008. case SIMD_i64x2_extmul_low_i32x4_s:
  14009. case SIMD_i64x2_extmul_high_i32x4_s:
  14010. case SIMD_i64x2_extmul_low_i32x4_u:
  14011. case SIMD_i64x2_extmul_high_i32x4_u:
  14012. {
  14013. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14014. break;
  14015. }
  14016. /* f32x4 operation */
  14017. case SIMD_f32x4_abs:
  14018. case SIMD_f32x4_neg:
  14019. case SIMD_f32x4_sqrt:
  14020. {
  14021. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14022. break;
  14023. }
  14024. case SIMD_f32x4_add:
  14025. case SIMD_f32x4_sub:
  14026. case SIMD_f32x4_mul:
  14027. case SIMD_f32x4_div:
  14028. case SIMD_f32x4_min:
  14029. case SIMD_f32x4_max:
  14030. case SIMD_f32x4_pmin:
  14031. case SIMD_f32x4_pmax:
  14032. {
  14033. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14034. break;
  14035. }
  14036. /* f64x2 operation */
  14037. case SIMD_f64x2_abs:
  14038. case SIMD_f64x2_neg:
  14039. case SIMD_f64x2_sqrt:
  14040. {
  14041. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14042. break;
  14043. }
  14044. case SIMD_f64x2_add:
  14045. case SIMD_f64x2_sub:
  14046. case SIMD_f64x2_mul:
  14047. case SIMD_f64x2_div:
  14048. case SIMD_f64x2_min:
  14049. case SIMD_f64x2_max:
  14050. case SIMD_f64x2_pmin:
  14051. case SIMD_f64x2_pmax:
  14052. {
  14053. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14054. break;
  14055. }
  14056. case SIMD_i32x4_trunc_sat_f32x4_s:
  14057. case SIMD_i32x4_trunc_sat_f32x4_u:
  14058. case SIMD_f32x4_convert_i32x4_s:
  14059. case SIMD_f32x4_convert_i32x4_u:
  14060. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14061. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14062. case SIMD_f64x2_convert_low_i32x4_s:
  14063. case SIMD_f64x2_convert_low_i32x4_u:
  14064. {
  14065. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14066. break;
  14067. }
  14068. default:
  14069. {
  14070. if (error_buf != NULL) {
  14071. snprintf(error_buf, error_buf_size,
  14072. "WASM module load failed: "
  14073. "invalid opcode 0xfd %02x.",
  14074. opcode1);
  14075. }
  14076. goto fail;
  14077. }
  14078. }
  14079. break;
  14080. }
  14081. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  14082. #endif /* end of WASM_ENABLE_SIMD */
  14083. #if WASM_ENABLE_SHARED_MEMORY != 0
  14084. case WASM_OP_ATOMIC_PREFIX:
  14085. {
  14086. uint32 opcode1;
  14087. read_leb_uint32(p, p_end, opcode1);
  14088. #if WASM_ENABLE_FAST_INTERP != 0
  14089. emit_byte(loader_ctx, opcode1);
  14090. #endif
  14091. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14092. CHECK_MEMORY();
  14093. read_leb_uint32(p, p_end, align); /* align */
  14094. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14095. if (!check_memory_align_equal(opcode1, align, error_buf,
  14096. error_buf_size)) {
  14097. goto fail;
  14098. }
  14099. #if WASM_ENABLE_FAST_INTERP != 0
  14100. emit_uint32(loader_ctx, mem_offset);
  14101. #endif
  14102. }
  14103. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14104. func->has_memory_operations = true;
  14105. #endif
  14106. switch (opcode1) {
  14107. case WASM_OP_ATOMIC_NOTIFY:
  14108. POP_I32();
  14109. POP_MEM_OFFSET();
  14110. PUSH_I32();
  14111. break;
  14112. case WASM_OP_ATOMIC_WAIT32:
  14113. POP_I64();
  14114. POP_I32();
  14115. POP_MEM_OFFSET();
  14116. PUSH_I32();
  14117. break;
  14118. case WASM_OP_ATOMIC_WAIT64:
  14119. POP_I64();
  14120. POP_I64();
  14121. POP_MEM_OFFSET();
  14122. PUSH_I32();
  14123. break;
  14124. case WASM_OP_ATOMIC_FENCE:
  14125. /* reserved byte 0x00 */
  14126. if (*p++ != 0x00) {
  14127. set_error_buf(error_buf, error_buf_size,
  14128. "zero byte expected");
  14129. goto fail;
  14130. }
  14131. break;
  14132. case WASM_OP_ATOMIC_I32_LOAD:
  14133. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14134. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14135. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14136. break;
  14137. case WASM_OP_ATOMIC_I32_STORE:
  14138. case WASM_OP_ATOMIC_I32_STORE8:
  14139. case WASM_OP_ATOMIC_I32_STORE16:
  14140. POP_I32();
  14141. POP_MEM_OFFSET();
  14142. break;
  14143. case WASM_OP_ATOMIC_I64_LOAD:
  14144. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14145. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14146. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14147. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14148. break;
  14149. case WASM_OP_ATOMIC_I64_STORE:
  14150. case WASM_OP_ATOMIC_I64_STORE8:
  14151. case WASM_OP_ATOMIC_I64_STORE16:
  14152. case WASM_OP_ATOMIC_I64_STORE32:
  14153. POP_I64();
  14154. POP_MEM_OFFSET();
  14155. break;
  14156. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14157. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14158. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14159. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14160. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14161. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14162. case WASM_OP_ATOMIC_RMW_I32_AND:
  14163. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14164. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14165. case WASM_OP_ATOMIC_RMW_I32_OR:
  14166. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14167. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14168. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14169. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14170. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14171. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14172. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14173. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14174. POP_I32();
  14175. POP_MEM_OFFSET();
  14176. PUSH_I32();
  14177. break;
  14178. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14179. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14180. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14181. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14182. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14183. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14184. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14185. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14186. case WASM_OP_ATOMIC_RMW_I64_AND:
  14187. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14188. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14189. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14190. case WASM_OP_ATOMIC_RMW_I64_OR:
  14191. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14192. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14193. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14194. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14195. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14196. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14197. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14198. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14199. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14200. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14201. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14202. POP_I64();
  14203. POP_MEM_OFFSET();
  14204. PUSH_I64();
  14205. break;
  14206. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14207. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14208. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14209. POP_I32();
  14210. POP_I32();
  14211. POP_MEM_OFFSET();
  14212. PUSH_I32();
  14213. break;
  14214. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14215. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14216. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14217. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14218. POP_I64();
  14219. POP_I64();
  14220. POP_MEM_OFFSET();
  14221. PUSH_I64();
  14222. break;
  14223. default:
  14224. set_error_buf_v(error_buf, error_buf_size,
  14225. "%s %02x %02x", "unsupported opcode",
  14226. 0xfe, opcode1);
  14227. goto fail;
  14228. }
  14229. break;
  14230. }
  14231. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14232. default:
  14233. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14234. "unsupported opcode", opcode);
  14235. goto fail;
  14236. }
  14237. #if WASM_ENABLE_FAST_INTERP != 0
  14238. last_op = opcode;
  14239. #endif
  14240. }
  14241. if (loader_ctx->csp_num > 0) {
  14242. if (cur_func_idx < module->function_count - 1)
  14243. /* Function with missing end marker (between two functions) */
  14244. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  14245. else
  14246. /* Function with missing end marker
  14247. (at EOF or end of code sections) */
  14248. set_error_buf(error_buf, error_buf_size,
  14249. "unexpected end of section or function, "
  14250. "or section size mismatch");
  14251. goto fail;
  14252. }
  14253. #if WASM_ENABLE_FAST_INTERP != 0
  14254. if (loader_ctx->p_code_compiled == NULL)
  14255. goto re_scan;
  14256. func->const_cell_num = loader_ctx->const_cell_num;
  14257. if (func->const_cell_num > 0) {
  14258. int32 j;
  14259. if (!(func->consts = func_const = loader_malloc(
  14260. func->const_cell_num * 4, error_buf, error_buf_size)))
  14261. goto fail;
  14262. func_const_end = func->consts + func->const_cell_num * 4;
  14263. /* reverse the const buf */
  14264. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  14265. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  14266. if (c->value_type == VALUE_TYPE_F64
  14267. || c->value_type == VALUE_TYPE_I64) {
  14268. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14269. &(c->value.f64), (uint32)sizeof(int64));
  14270. func_const += sizeof(int64);
  14271. }
  14272. else {
  14273. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14274. &(c->value.f32), (uint32)sizeof(int32));
  14275. func_const += sizeof(int32);
  14276. }
  14277. }
  14278. }
  14279. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14280. - loader_ctx->start_dynamic_offset + 1;
  14281. #else
  14282. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14283. #endif
  14284. func->max_block_num = loader_ctx->max_csp_num;
  14285. return_value = true;
  14286. fail:
  14287. wasm_loader_ctx_destroy(loader_ctx);
  14288. (void)table_idx;
  14289. (void)table_seg_idx;
  14290. (void)data_seg_idx;
  14291. (void)i64_const;
  14292. (void)local_offset;
  14293. (void)p_org;
  14294. (void)mem_offset;
  14295. (void)align;
  14296. return return_value;
  14297. }