wasm_loader.c 587 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_platform.h"
  7. #include "wasm.h"
  8. #include "wasm_opcode.h"
  9. #include "wasm_runtime.h"
  10. #include "wasm_loader_common.h"
  11. #include "../common/wasm_native.h"
  12. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_GC != 0
  14. #include "../common/gc/gc_type.h"
  15. #include "../common/gc/gc_object.h"
  16. #endif
  17. #if WASM_ENABLE_DEBUG_INTERP != 0
  18. #include "../libraries/debug-engine/debug_engine.h"
  19. #endif
  20. #if WASM_ENABLE_FAST_JIT != 0
  21. #include "../fast-jit/jit_compiler.h"
  22. #include "../fast-jit/jit_codecache.h"
  23. #endif
  24. #if WASM_ENABLE_JIT != 0
  25. #include "../compilation/aot_llvm.h"
  26. #endif
  27. #ifndef TRACE_WASM_LOADER
  28. #define TRACE_WASM_LOADER 0
  29. #endif
  30. /* Read a value of given type from the address pointed to by the given
  31. pointer and increase the pointer to the position just after the
  32. value being read. */
  33. #define TEMPLATE_READ_VALUE(Type, p) \
  34. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  35. #if WASM_ENABLE_MEMORY64 != 0
  36. static bool
  37. has_module_memory64(WASMModule *module)
  38. {
  39. /* TODO: multi-memories for now assuming the memory idx type is consistent
  40. * across multi-memories */
  41. if (module->import_memory_count > 0)
  42. return !!(module->import_memories[0].u.memory.mem_type.flags
  43. & MEMORY64_FLAG);
  44. else if (module->memory_count > 0)
  45. return !!(module->memories[0].flags & MEMORY64_FLAG);
  46. return false;
  47. }
  48. static bool
  49. is_table_64bit(WASMModule *module, uint32 table_idx)
  50. {
  51. if (table_idx < module->import_table_count)
  52. return !!(module->import_tables[table_idx].u.table.table_type.flags
  53. & TABLE64_FLAG);
  54. else
  55. return !!(module->tables[table_idx].table_type.flags & TABLE64_FLAG);
  56. return false;
  57. }
  58. #endif
  59. static void
  60. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  61. {
  62. wasm_loader_set_error_buf(error_buf, error_buf_size, string, false);
  63. }
  64. #if WASM_ENABLE_MEMORY64 != 0
  65. static void
  66. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  67. {
  68. if (error_buf != NULL) {
  69. snprintf(error_buf, error_buf_size, "offset out of range");
  70. }
  71. }
  72. #endif
  73. static void
  74. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  75. {
  76. va_list args;
  77. char buf[128];
  78. if (error_buf != NULL) {
  79. va_start(args, format);
  80. vsnprintf(buf, sizeof(buf), format, args);
  81. va_end(args);
  82. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  83. }
  84. }
  85. static bool
  86. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  87. char *error_buf, uint32 error_buf_size)
  88. {
  89. if ((uintptr_t)buf + length < (uintptr_t)buf
  90. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  91. set_error_buf(error_buf, error_buf_size,
  92. "unexpected end of section or function");
  93. return false;
  94. }
  95. return true;
  96. }
  97. static bool
  98. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  99. char *error_buf, uint32 error_buf_size)
  100. {
  101. if ((uintptr_t)buf + length < (uintptr_t)buf
  102. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  103. set_error_buf(error_buf, error_buf_size, "unexpected end");
  104. return false;
  105. }
  106. return true;
  107. }
  108. #define CHECK_BUF(buf, buf_end, length) \
  109. do { \
  110. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  111. goto fail; \
  112. } \
  113. } while (0)
  114. #define CHECK_BUF1(buf, buf_end, length) \
  115. do { \
  116. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  117. goto fail; \
  118. } \
  119. } while (0)
  120. #define skip_leb(p) while (*p++ & 0x80)
  121. #define skip_leb_int64(p, p_end) skip_leb(p)
  122. #define skip_leb_uint32(p, p_end) skip_leb(p)
  123. #define skip_leb_int32(p, p_end) skip_leb(p)
  124. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  125. #define skip_leb_memidx(p, p_end) skip_leb(p)
  126. #if WASM_ENABLE_MULTI_MEMORY == 0
  127. #define skip_leb_align(p, p_end) skip_leb(p)
  128. #else
  129. /* Skip the following memidx if applicable */
  130. #define skip_leb_align(p, p_end) \
  131. do { \
  132. if (*p++ & OPT_MEMIDX_FLAG) \
  133. skip_leb_uint32(p, p_end); \
  134. } while (0)
  135. #endif
  136. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  137. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  138. #define read_leb_int64(p, p_end, res) \
  139. do { \
  140. uint64 res64; \
  141. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  142. error_buf_size)) \
  143. goto fail; \
  144. res = (int64)res64; \
  145. } while (0)
  146. #if WASM_ENABLE_MEMORY64 != 0
  147. #define read_leb_mem_offset(p, p_end, res) \
  148. do { \
  149. uint64 res64; \
  150. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  151. &res64, error_buf, error_buf_size)) { \
  152. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  153. goto fail; \
  154. } \
  155. res = (mem_offset_t)res64; \
  156. } while (0)
  157. #else
  158. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  159. #endif
  160. #define read_leb_uint32(p, p_end, res) \
  161. do { \
  162. uint64 res64; \
  163. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  164. error_buf_size)) \
  165. goto fail; \
  166. res = (uint32)res64; \
  167. } while (0)
  168. #define read_leb_int32(p, p_end, res) \
  169. do { \
  170. uint64 res64; \
  171. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  172. error_buf_size)) \
  173. goto fail; \
  174. res = (int32)res64; \
  175. } while (0)
  176. #define read_leb_memidx(p, p_end, res) read_leb_uint32(p, p_end, res)
  177. #if WASM_ENABLE_MULTI_MEMORY != 0
  178. #define check_memidx(module, memidx) \
  179. do { \
  180. if (memidx >= module->import_memory_count + module->memory_count) { \
  181. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d", \
  182. memidx); \
  183. goto fail; \
  184. } \
  185. } while (0)
  186. /* Bit 6(0x40) indicating the optional memidx, and reset bit 6 for
  187. * alignment check */
  188. #define read_leb_memarg(p, p_end, res) \
  189. do { \
  190. read_leb_uint32(p, p_end, res); \
  191. if (res & OPT_MEMIDX_FLAG) { \
  192. res &= ~OPT_MEMIDX_FLAG; \
  193. read_leb_uint32(p, p_end, memidx); /* memidx */ \
  194. check_memidx(module, memidx); \
  195. } \
  196. } while (0)
  197. #else
  198. /* reserved byte 0x00 */
  199. #define check_memidx(module, memidx) \
  200. do { \
  201. (void)module; \
  202. if (memidx != 0) { \
  203. set_error_buf(error_buf, error_buf_size, "zero byte expected"); \
  204. goto fail; \
  205. } \
  206. } while (0)
  207. #define read_leb_memarg(p, p_end, res) read_leb_uint32(p, p_end, res)
  208. #endif
  209. static char *
  210. type2str(uint8 type)
  211. {
  212. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  213. #if WASM_ENABLE_GC != 0
  214. char *type_str_ref[] = { "stringview_iter",
  215. "stringview_wtf16",
  216. "(ref null ht)",
  217. "(ref ht)",
  218. "", /* reserved */
  219. "stringview_wtf8",
  220. "stringref",
  221. "", /* reserved */
  222. "", /* reserved */
  223. "arrayref",
  224. "structref",
  225. "i32ref",
  226. "eqref",
  227. "anyref",
  228. "externref",
  229. "funcref",
  230. "nullref",
  231. "nullexternref",
  232. "nullfuncref" };
  233. #endif
  234. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  235. return type_str[type - VALUE_TYPE_V128];
  236. #if WASM_ENABLE_GC != 0
  237. else if (wasm_is_type_reftype(type))
  238. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  239. #endif
  240. else if (type == VALUE_TYPE_FUNCREF)
  241. return "funcref";
  242. else if (type == VALUE_TYPE_EXTERNREF)
  243. return "externref";
  244. else
  245. return "unknown type";
  246. }
  247. static bool
  248. is_32bit_type(uint8 type)
  249. {
  250. if (type == VALUE_TYPE_I32
  251. || type == VALUE_TYPE_F32
  252. /* the operand stack is in polymorphic state */
  253. || type == VALUE_TYPE_ANY
  254. #if WASM_ENABLE_GC != 0
  255. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  256. #elif WASM_ENABLE_REF_TYPES != 0
  257. /* For reference types, we use uint32 index to represent
  258. the funcref and externref */
  259. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  260. #endif
  261. )
  262. return true;
  263. return false;
  264. }
  265. static bool
  266. is_64bit_type(uint8 type)
  267. {
  268. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  269. #if WASM_ENABLE_GC != 0
  270. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  271. #endif
  272. )
  273. return true;
  274. return false;
  275. }
  276. #if WASM_ENABLE_GC != 0
  277. static bool
  278. is_packed_type(uint8 type)
  279. {
  280. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  281. }
  282. #endif
  283. static bool
  284. is_byte_a_type(uint8 type)
  285. {
  286. return (is_valid_value_type_for_interpreter(type)
  287. || (type == VALUE_TYPE_VOID))
  288. ? true
  289. : false;
  290. }
  291. #if WASM_ENABLE_SIMD != 0
  292. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  293. static V128
  294. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  295. {
  296. V128 result;
  297. uint8 i;
  298. for (i = 0; i != 16; ++i) {
  299. result.i8x16[i] = read_uint8(p_buf);
  300. }
  301. return result;
  302. }
  303. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  304. #endif /* end of WASM_ENABLE_SIMD */
  305. static void *
  306. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  307. {
  308. void *mem;
  309. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  310. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  311. return NULL;
  312. }
  313. memset(mem, 0, (uint32)size);
  314. return mem;
  315. }
  316. static void *
  317. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  318. uint32 error_buf_size)
  319. {
  320. uint8 *mem_new;
  321. bh_assert(size_new > size_old);
  322. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  323. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  324. memset(mem_new + size_old, 0, size_new - size_old);
  325. wasm_runtime_free(mem_old);
  326. }
  327. return mem_new;
  328. }
  329. #define MEM_REALLOC(mem, size_old, size_new) \
  330. do { \
  331. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  332. error_buf_size); \
  333. if (!mem_new) \
  334. goto fail; \
  335. mem = mem_new; \
  336. } while (0)
  337. #if WASM_ENABLE_GC != 0
  338. static bool
  339. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  340. char *error_buf, uint32 error_buf_size)
  341. {
  342. if (type_index >= type_count) {
  343. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  344. type_index);
  345. return false;
  346. }
  347. return true;
  348. }
  349. static bool
  350. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  351. uint32 error_buf_size)
  352. {
  353. if (!check_type_index(module, module->type_count, type_index, error_buf,
  354. error_buf_size)) {
  355. return false;
  356. }
  357. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  358. set_error_buf(error_buf, error_buf_size, "unkown array type");
  359. return false;
  360. }
  361. return true;
  362. }
  363. #endif
  364. static bool
  365. check_function_index(const WASMModule *module, uint32 function_index,
  366. char *error_buf, uint32 error_buf_size)
  367. {
  368. if (function_index
  369. >= module->import_function_count + module->function_count) {
  370. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  371. function_index);
  372. return false;
  373. }
  374. return true;
  375. }
  376. typedef struct InitValue {
  377. uint8 type;
  378. uint8 flag;
  379. #if WASM_ENABLE_GC != 0
  380. uint8 gc_opcode;
  381. WASMRefType ref_type;
  382. #endif
  383. WASMValue value;
  384. } InitValue;
  385. typedef struct ConstExprContext {
  386. uint32 sp;
  387. uint32 size;
  388. WASMModule *module;
  389. InitValue *stack;
  390. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  391. } ConstExprContext;
  392. static void
  393. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  394. {
  395. ctx->sp = 0;
  396. ctx->module = module;
  397. ctx->stack = ctx->data;
  398. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  399. }
  400. static bool
  401. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  402. #if WASM_ENABLE_GC != 0
  403. WASMRefType *ref_type, uint8 gc_opcode,
  404. #endif
  405. WASMValue *value, char *error_buf, uint32 error_buf_size)
  406. {
  407. InitValue *cur_value;
  408. if (ctx->sp >= ctx->size) {
  409. if (ctx->stack != ctx->data) {
  410. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  411. (ctx->size + 4) * sizeof(InitValue));
  412. }
  413. else {
  414. if (!(ctx->stack =
  415. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  416. error_buf, error_buf_size))) {
  417. goto fail;
  418. }
  419. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  420. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  421. }
  422. ctx->size += 4;
  423. }
  424. cur_value = &ctx->stack[ctx->sp++];
  425. cur_value->type = type;
  426. cur_value->flag = flag;
  427. cur_value->value = *value;
  428. #if WASM_ENABLE_GC != 0
  429. cur_value->gc_opcode = gc_opcode;
  430. if (wasm_is_type_multi_byte_type(type)) {
  431. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  432. ref_type, wasm_reftype_struct_size(ref_type));
  433. }
  434. #endif
  435. return true;
  436. fail:
  437. return false;
  438. }
  439. #if WASM_ENABLE_GC != 0
  440. static void
  441. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  442. {
  443. WASMStructNewInitValues *struct_init_values =
  444. (WASMStructNewInitValues *)data;
  445. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  446. WASMType *wasm_type;
  447. uint32 i;
  448. if (!data)
  449. return;
  450. wasm_type = module->types[struct_init_values->type_idx];
  451. /* The data can only be type of `WASMStructNewInitValues *`
  452. or `WASMArrayNewInitValues *` */
  453. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  454. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  455. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  456. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  457. WASMRefTypeMap *ref_type_map = struct_type->ref_type_maps;
  458. WASMRefType *ref_type;
  459. uint8 field_type;
  460. for (i = 0; i < struct_init_values->count; i++) {
  461. field_type = struct_type->fields[i].field_type;
  462. if (wasm_is_type_multi_byte_type(field_type))
  463. ref_type = ref_type_map->ref_type;
  464. else
  465. ref_type = NULL;
  466. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  467. REF_TYPE_STRUCTREF, NULL,
  468. module->types, module->type_count)
  469. || wasm_reftype_is_subtype_of(
  470. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  471. module->types, module->type_count)) {
  472. destroy_init_expr_data_recursive(
  473. module, struct_init_values->fields[i].data);
  474. }
  475. }
  476. }
  477. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  478. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  479. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  480. uint8 elem_type = array_type->elem_type;
  481. for (i = 0; i < array_init_values->length; i++) {
  482. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  483. REF_TYPE_STRUCTREF, NULL,
  484. module->types, module->type_count)
  485. || wasm_reftype_is_subtype_of(
  486. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  487. module->types, module->type_count)) {
  488. destroy_init_expr_data_recursive(
  489. module, array_init_values->elem_data[i].data);
  490. }
  491. }
  492. }
  493. wasm_runtime_free(data);
  494. }
  495. #endif
  496. static bool
  497. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  498. #if WASM_ENABLE_GC != 0
  499. WASMRefType *ref_type, uint8 *p_gc_opcode,
  500. #endif
  501. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  502. {
  503. InitValue *cur_value;
  504. if (ctx->sp == 0) {
  505. set_error_buf(error_buf, error_buf_size,
  506. "type mismatch: const expr stack underflow");
  507. return false;
  508. }
  509. cur_value = &ctx->stack[--ctx->sp];
  510. #if WASM_ENABLE_GC == 0
  511. if (cur_value->type != type) {
  512. set_error_buf(error_buf, error_buf_size, "type mismatch");
  513. return false;
  514. }
  515. #else
  516. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  517. ref_type, ctx->module->types,
  518. ctx->module->type_count)) {
  519. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  520. "type mismatch: expect ", type2str(type),
  521. " but got other");
  522. goto fail;
  523. }
  524. #endif
  525. if (p_flag)
  526. *p_flag = cur_value->flag;
  527. if (p_value)
  528. *p_value = cur_value->value;
  529. #if WASM_ENABLE_GC != 0
  530. if (p_gc_opcode)
  531. *p_gc_opcode = cur_value->gc_opcode;
  532. #endif
  533. return true;
  534. #if WASM_ENABLE_GC != 0
  535. fail:
  536. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  537. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  538. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  539. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  540. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  541. }
  542. return false;
  543. #endif
  544. }
  545. static void
  546. destroy_const_expr_stack(ConstExprContext *ctx)
  547. {
  548. #if WASM_ENABLE_GC != 0
  549. uint32 i;
  550. for (i = 0; i < ctx->sp; i++) {
  551. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  552. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  553. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  554. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  555. destroy_init_expr_data_recursive(ctx->module,
  556. ctx->stack[i].value.data);
  557. }
  558. }
  559. #endif
  560. if (ctx->stack != ctx->data) {
  561. wasm_runtime_free(ctx->stack);
  562. }
  563. }
  564. #if WASM_ENABLE_GC != 0
  565. static void
  566. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  567. {
  568. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  569. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  570. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  571. destroy_init_expr_data_recursive(module, expr->u.data);
  572. }
  573. }
  574. #endif /* end of WASM_ENABLE_GC != 0 */
  575. static bool
  576. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  577. InitializerExpression *init_expr, uint8 type, void *ref_type,
  578. char *error_buf, uint32 error_buf_size)
  579. {
  580. const uint8 *p = *p_buf, *p_end = buf_end;
  581. uint8 flag, *p_float;
  582. uint32 i;
  583. ConstExprContext const_expr_ctx = { 0 };
  584. WASMValue cur_value;
  585. #if WASM_ENABLE_GC != 0
  586. uint32 opcode1, type_idx;
  587. uint8 opcode;
  588. WASMRefType cur_ref_type = { 0 };
  589. #endif
  590. init_const_expr_stack(&const_expr_ctx, module);
  591. CHECK_BUF(p, p_end, 1);
  592. flag = read_uint8(p);
  593. while (flag != WASM_OP_END) {
  594. switch (flag) {
  595. /* i32.const */
  596. case INIT_EXPR_TYPE_I32_CONST:
  597. read_leb_int32(p, p_end, cur_value.i32);
  598. if (!push_const_expr_stack(
  599. &const_expr_ctx, flag, VALUE_TYPE_I32,
  600. #if WASM_ENABLE_GC != 0
  601. NULL, 0,
  602. #endif
  603. &cur_value, error_buf, error_buf_size))
  604. goto fail;
  605. break;
  606. /* i64.const */
  607. case INIT_EXPR_TYPE_I64_CONST:
  608. read_leb_int64(p, p_end, cur_value.i64);
  609. if (!push_const_expr_stack(
  610. &const_expr_ctx, flag, VALUE_TYPE_I64,
  611. #if WASM_ENABLE_GC != 0
  612. NULL, 0,
  613. #endif
  614. &cur_value, error_buf, error_buf_size))
  615. goto fail;
  616. break;
  617. /* f32.const */
  618. case INIT_EXPR_TYPE_F32_CONST:
  619. CHECK_BUF(p, p_end, 4);
  620. p_float = (uint8 *)&cur_value.f32;
  621. for (i = 0; i < sizeof(float32); i++)
  622. *p_float++ = *p++;
  623. if (!push_const_expr_stack(
  624. &const_expr_ctx, flag, VALUE_TYPE_F32,
  625. #if WASM_ENABLE_GC != 0
  626. NULL, 0,
  627. #endif
  628. &cur_value, error_buf, error_buf_size))
  629. goto fail;
  630. break;
  631. /* f64.const */
  632. case INIT_EXPR_TYPE_F64_CONST:
  633. CHECK_BUF(p, p_end, 8);
  634. p_float = (uint8 *)&cur_value.f64;
  635. for (i = 0; i < sizeof(float64); i++)
  636. *p_float++ = *p++;
  637. if (!push_const_expr_stack(
  638. &const_expr_ctx, flag, VALUE_TYPE_F64,
  639. #if WASM_ENABLE_GC != 0
  640. NULL, 0,
  641. #endif
  642. &cur_value, error_buf, error_buf_size))
  643. goto fail;
  644. break;
  645. #if WASM_ENABLE_SIMD != 0
  646. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  647. /* v128.const */
  648. case INIT_EXPR_TYPE_V128_CONST:
  649. {
  650. uint64 high, low;
  651. CHECK_BUF(p, p_end, 1);
  652. (void)read_uint8(p);
  653. CHECK_BUF(p, p_end, 16);
  654. wasm_runtime_read_v128(p, &high, &low);
  655. p += 16;
  656. cur_value.v128.i64x2[0] = high;
  657. cur_value.v128.i64x2[1] = low;
  658. if (!push_const_expr_stack(
  659. &const_expr_ctx, flag, VALUE_TYPE_V128,
  660. #if WASM_ENABLE_GC != 0
  661. NULL, 0,
  662. #endif
  663. &cur_value, error_buf, error_buf_size))
  664. goto fail;
  665. #if WASM_ENABLE_WAMR_COMPILER != 0
  666. /* If any init_expr is v128.const, mark SIMD used */
  667. module->is_simd_used = true;
  668. #endif
  669. break;
  670. }
  671. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  672. #endif /* end of WASM_ENABLE_SIMD */
  673. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  674. /* ref.func */
  675. case INIT_EXPR_TYPE_FUNCREF_CONST:
  676. {
  677. uint32 func_idx;
  678. read_leb_uint32(p, p_end, func_idx);
  679. cur_value.ref_index = func_idx;
  680. if (!check_function_index(module, func_idx, error_buf,
  681. error_buf_size)) {
  682. goto fail;
  683. }
  684. #if WASM_ENABLE_GC == 0
  685. if (!push_const_expr_stack(&const_expr_ctx, flag,
  686. VALUE_TYPE_FUNCREF, &cur_value,
  687. error_buf, error_buf_size))
  688. goto fail;
  689. #else
  690. if (func_idx < module->import_function_count) {
  691. type_idx =
  692. module->import_functions[func_idx].u.function.type_idx;
  693. }
  694. else {
  695. type_idx = module
  696. ->functions[func_idx
  697. - module->import_function_count]
  698. ->type_idx;
  699. }
  700. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  701. false, type_idx);
  702. if (!push_const_expr_stack(&const_expr_ctx, flag,
  703. cur_ref_type.ref_type, &cur_ref_type,
  704. 0, &cur_value, error_buf,
  705. error_buf_size))
  706. goto fail;
  707. #endif
  708. #if WASM_ENABLE_WAMR_COMPILER != 0
  709. module->is_ref_types_used = true;
  710. #endif
  711. break;
  712. }
  713. /* ref.null */
  714. case INIT_EXPR_TYPE_REFNULL_CONST:
  715. {
  716. uint8 type1;
  717. CHECK_BUF(p, p_end, 1);
  718. type1 = read_uint8(p);
  719. #if WASM_ENABLE_GC == 0
  720. cur_value.ref_index = NULL_REF;
  721. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  722. &cur_value, error_buf,
  723. error_buf_size))
  724. goto fail;
  725. #else
  726. cur_value.gc_obj = NULL_REF;
  727. if (!is_byte_a_type(type1)) {
  728. p--;
  729. read_leb_uint32(p, p_end, type_idx);
  730. if (!check_type_index(module, module->type_count, type_idx,
  731. error_buf, error_buf_size))
  732. goto fail;
  733. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  734. true, type_idx);
  735. if (!push_const_expr_stack(&const_expr_ctx, flag,
  736. cur_ref_type.ref_type,
  737. &cur_ref_type, 0, &cur_value,
  738. error_buf, error_buf_size))
  739. goto fail;
  740. }
  741. else {
  742. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  743. NULL, 0, &cur_value, error_buf,
  744. error_buf_size))
  745. goto fail;
  746. }
  747. #endif
  748. #if WASM_ENABLE_WAMR_COMPILER != 0
  749. module->is_ref_types_used = true;
  750. #endif
  751. break;
  752. }
  753. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  754. /* get_global */
  755. case INIT_EXPR_TYPE_GET_GLOBAL:
  756. {
  757. uint32 global_idx;
  758. uint8 global_type;
  759. read_leb_uint32(p, p_end, cur_value.global_index);
  760. global_idx = cur_value.global_index;
  761. /*
  762. * Currently, constant expressions occurring as initializers
  763. * of globals are further constrained in that contained
  764. * global.get instructions are
  765. * only allowed to refer to imported globals.
  766. *
  767. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  768. */
  769. if (global_idx >= module->import_global_count
  770. /* make spec test happy */
  771. #if WASM_ENABLE_GC != 0
  772. + module->global_count
  773. #endif
  774. ) {
  775. set_error_buf_v(error_buf, error_buf_size,
  776. "unknown global %u", global_idx);
  777. goto fail;
  778. }
  779. if (
  780. /* make spec test happy */
  781. #if WASM_ENABLE_GC != 0
  782. global_idx < module->import_global_count &&
  783. #endif
  784. module->import_globals[global_idx]
  785. .u.global.type.is_mutable) {
  786. set_error_buf_v(error_buf, error_buf_size,
  787. "constant expression required");
  788. goto fail;
  789. }
  790. if (global_idx < module->import_global_count) {
  791. global_type = module->import_globals[global_idx]
  792. .u.global.type.val_type;
  793. #if WASM_ENABLE_GC != 0
  794. if (wasm_is_type_multi_byte_type(global_type)) {
  795. WASMRefType *global_ref_type =
  796. module->import_globals[global_idx]
  797. .u.global.ref_type;
  798. bh_memcpy_s(&cur_ref_type,
  799. wasm_reftype_struct_size(global_ref_type),
  800. global_ref_type,
  801. wasm_reftype_struct_size(global_ref_type));
  802. }
  803. #endif
  804. }
  805. else {
  806. global_type =
  807. module
  808. ->globals[global_idx - module->import_global_count]
  809. .type.val_type;
  810. #if WASM_ENABLE_GC != 0
  811. if (wasm_is_type_multi_byte_type(global_type)) {
  812. WASMRefType *global_ref_type =
  813. module
  814. ->globals[global_idx
  815. - module->import_global_count]
  816. .ref_type;
  817. bh_memcpy_s(&cur_ref_type,
  818. wasm_reftype_struct_size(global_ref_type),
  819. global_ref_type,
  820. wasm_reftype_struct_size(global_ref_type));
  821. }
  822. #endif
  823. }
  824. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  825. #if WASM_ENABLE_GC != 0
  826. &cur_ref_type, 0,
  827. #endif
  828. &cur_value, error_buf,
  829. error_buf_size))
  830. goto fail;
  831. break;
  832. }
  833. #if WASM_ENABLE_GC != 0
  834. /* struct.new and array.new */
  835. case WASM_OP_GC_PREFIX:
  836. {
  837. read_leb_uint32(p, p_end, opcode1);
  838. switch (opcode1) {
  839. case WASM_OP_STRUCT_NEW:
  840. {
  841. WASMStructType *struct_type;
  842. WASMStructNewInitValues *struct_init_values = NULL;
  843. uint32 field_count;
  844. read_leb_uint32(p, p_end, type_idx);
  845. if (!check_type_index(module, module->type_count,
  846. type_idx, error_buf,
  847. error_buf_size)) {
  848. goto fail;
  849. }
  850. struct_type = (WASMStructType *)module->types[type_idx];
  851. if (struct_type->base_type.type_flag
  852. != WASM_TYPE_STRUCT) {
  853. set_error_buf(error_buf, error_buf_size,
  854. "unkown struct type");
  855. goto fail;
  856. }
  857. field_count = struct_type->field_count;
  858. if (!(struct_init_values = loader_malloc(
  859. offsetof(WASMStructNewInitValues, fields)
  860. + (uint64)field_count * sizeof(WASMValue),
  861. error_buf, error_buf_size))) {
  862. goto fail;
  863. }
  864. struct_init_values->type_idx = type_idx;
  865. struct_init_values->count = field_count;
  866. for (i = field_count; i > 0; i--) {
  867. WASMRefType *field_ref_type = NULL;
  868. uint32 field_idx = i - 1;
  869. uint8 field_type =
  870. struct_type->fields[field_idx].field_type;
  871. if (wasm_is_type_multi_byte_type(field_type)) {
  872. field_ref_type = wasm_reftype_map_find(
  873. struct_type->ref_type_maps,
  874. struct_type->ref_type_map_count, field_idx);
  875. }
  876. if (is_packed_type(field_type)) {
  877. field_type = VALUE_TYPE_I32;
  878. }
  879. if (!pop_const_expr_stack(
  880. &const_expr_ctx, NULL, field_type,
  881. field_ref_type, NULL,
  882. &struct_init_values->fields[field_idx],
  883. error_buf, error_buf_size)) {
  884. destroy_init_expr_data_recursive(
  885. module, struct_init_values);
  886. goto fail;
  887. }
  888. }
  889. cur_value.data = struct_init_values;
  890. wasm_set_refheaptype_typeidx(
  891. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  892. if (!push_const_expr_stack(
  893. &const_expr_ctx, flag, cur_ref_type.ref_type,
  894. &cur_ref_type, (uint8)opcode1, &cur_value,
  895. error_buf, error_buf_size)) {
  896. destroy_init_expr_data_recursive(
  897. module, struct_init_values);
  898. goto fail;
  899. }
  900. break;
  901. }
  902. case WASM_OP_STRUCT_NEW_DEFAULT:
  903. {
  904. read_leb_uint32(p, p_end, cur_value.type_index);
  905. type_idx = cur_value.type_index;
  906. if (!check_type_index(module, module->type_count,
  907. type_idx, error_buf,
  908. error_buf_size)) {
  909. goto fail;
  910. }
  911. if (module->types[type_idx]->type_flag
  912. != WASM_TYPE_STRUCT) {
  913. set_error_buf(error_buf, error_buf_size,
  914. "unkown struct type");
  915. goto fail;
  916. }
  917. cur_value.type_index = type_idx;
  918. cur_value.data = NULL;
  919. wasm_set_refheaptype_typeidx(
  920. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  921. if (!push_const_expr_stack(
  922. &const_expr_ctx, flag, cur_ref_type.ref_type,
  923. &cur_ref_type, (uint8)opcode1, &cur_value,
  924. error_buf, error_buf_size)) {
  925. goto fail;
  926. }
  927. break;
  928. }
  929. case WASM_OP_ARRAY_NEW:
  930. case WASM_OP_ARRAY_NEW_DEFAULT:
  931. case WASM_OP_ARRAY_NEW_FIXED:
  932. {
  933. WASMArrayNewInitValues *array_init_values = NULL;
  934. WASMArrayType *array_type = NULL;
  935. WASMRefType *elem_ref_type = NULL;
  936. uint64 total_size;
  937. uint8 elem_type;
  938. read_leb_uint32(p, p_end, cur_value.type_index);
  939. type_idx = cur_value.type_index;
  940. if (!check_type_index(module, module->type_count,
  941. type_idx, error_buf,
  942. error_buf_size)) {
  943. goto fail;
  944. }
  945. array_type = (WASMArrayType *)module->types[type_idx];
  946. if (array_type->base_type.type_flag
  947. != WASM_TYPE_ARRAY) {
  948. set_error_buf(error_buf, error_buf_size,
  949. "unkown array type");
  950. goto fail;
  951. }
  952. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  953. elem_type = array_type->elem_type;
  954. if (wasm_is_type_multi_byte_type(elem_type)) {
  955. elem_ref_type = array_type->elem_ref_type;
  956. }
  957. if (is_packed_type(elem_type)) {
  958. elem_type = VALUE_TYPE_I32;
  959. }
  960. if (opcode1 == WASM_OP_ARRAY_NEW) {
  961. WASMValue len_val;
  962. if (!(array_init_values = loader_malloc(
  963. sizeof(WASMArrayNewInitValues),
  964. error_buf, error_buf_size))) {
  965. goto fail;
  966. }
  967. array_init_values->type_idx = type_idx;
  968. if (!pop_const_expr_stack(
  969. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  970. NULL, NULL, &len_val, error_buf,
  971. error_buf_size)) {
  972. destroy_init_expr_data_recursive(
  973. module, array_init_values);
  974. goto fail;
  975. }
  976. array_init_values->length = len_val.i32;
  977. if (!pop_const_expr_stack(
  978. &const_expr_ctx, NULL, elem_type,
  979. elem_ref_type, NULL,
  980. &array_init_values->elem_data[0],
  981. error_buf, error_buf_size)) {
  982. destroy_init_expr_data_recursive(
  983. module, array_init_values);
  984. goto fail;
  985. }
  986. cur_value.data = array_init_values;
  987. }
  988. else {
  989. /* WASM_OP_ARRAY_NEW_FIXED */
  990. uint32 len;
  991. read_leb_uint32(p, p_end, len);
  992. total_size =
  993. (uint64)offsetof(WASMArrayNewInitValues,
  994. elem_data)
  995. + (uint64)sizeof(WASMValue) * len;
  996. if (!(array_init_values =
  997. loader_malloc(total_size, error_buf,
  998. error_buf_size))) {
  999. goto fail;
  1000. }
  1001. array_init_values->type_idx = type_idx;
  1002. array_init_values->length = len;
  1003. for (i = len; i > 0; i--) {
  1004. if (!pop_const_expr_stack(
  1005. &const_expr_ctx, NULL, elem_type,
  1006. elem_ref_type, NULL,
  1007. &array_init_values
  1008. ->elem_data[i - 1],
  1009. error_buf, error_buf_size)) {
  1010. destroy_init_expr_data_recursive(
  1011. module, array_init_values);
  1012. goto fail;
  1013. }
  1014. }
  1015. cur_value.data = array_init_values;
  1016. }
  1017. }
  1018. else {
  1019. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1020. WASMValue len_val;
  1021. uint32 len;
  1022. /* POP(i32) */
  1023. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1024. VALUE_TYPE_I32, NULL,
  1025. NULL, &len_val, error_buf,
  1026. error_buf_size)) {
  1027. goto fail;
  1028. }
  1029. len = len_val.i32;
  1030. cur_value.array_new_default.type_index = type_idx;
  1031. cur_value.array_new_default.length = len;
  1032. }
  1033. wasm_set_refheaptype_typeidx(
  1034. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1035. if (!push_const_expr_stack(
  1036. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1037. &cur_ref_type, (uint8)opcode1, &cur_value,
  1038. error_buf, error_buf_size)) {
  1039. if (array_init_values) {
  1040. destroy_init_expr_data_recursive(
  1041. module, array_init_values);
  1042. }
  1043. goto fail;
  1044. }
  1045. break;
  1046. }
  1047. case WASM_OP_ANY_CONVERT_EXTERN:
  1048. {
  1049. set_error_buf(error_buf, error_buf_size,
  1050. "unsupported constant expression of "
  1051. "extern.internalize");
  1052. goto fail;
  1053. }
  1054. case WASM_OP_EXTERN_CONVERT_ANY:
  1055. {
  1056. set_error_buf(error_buf, error_buf_size,
  1057. "unsupported constant expression of "
  1058. "extern.externalize");
  1059. goto fail;
  1060. }
  1061. case WASM_OP_REF_I31:
  1062. {
  1063. /* POP(i32) */
  1064. if (!pop_const_expr_stack(
  1065. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1066. NULL, &cur_value, error_buf, error_buf_size)) {
  1067. goto fail;
  1068. }
  1069. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1070. false, HEAP_TYPE_I31);
  1071. if (!push_const_expr_stack(
  1072. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1073. &cur_ref_type, (uint8)opcode1, &cur_value,
  1074. error_buf, error_buf_size)) {
  1075. goto fail;
  1076. }
  1077. break;
  1078. }
  1079. default:
  1080. set_error_buf(
  1081. error_buf, error_buf_size,
  1082. "type mismatch or constant expression required");
  1083. goto fail;
  1084. }
  1085. break;
  1086. }
  1087. #endif /* end of WASM_ENABLE_GC != 0 */
  1088. default:
  1089. {
  1090. set_error_buf(error_buf, error_buf_size,
  1091. "illegal opcode "
  1092. "or constant expression required "
  1093. "or type mismatch");
  1094. goto fail;
  1095. }
  1096. }
  1097. CHECK_BUF(p, p_end, 1);
  1098. flag = read_uint8(p);
  1099. }
  1100. /* There should be only one value left on the init value stack */
  1101. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1102. #if WASM_ENABLE_GC != 0
  1103. ref_type, &opcode,
  1104. #endif
  1105. &cur_value, error_buf, error_buf_size)) {
  1106. goto fail;
  1107. }
  1108. if (const_expr_ctx.sp != 0) {
  1109. set_error_buf(error_buf, error_buf_size,
  1110. "type mismatch: illegal constant opcode sequence");
  1111. goto fail;
  1112. }
  1113. init_expr->init_expr_type = flag;
  1114. init_expr->u = cur_value;
  1115. #if WASM_ENABLE_GC != 0
  1116. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1117. switch (opcode) {
  1118. case WASM_OP_STRUCT_NEW:
  1119. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1120. break;
  1121. case WASM_OP_STRUCT_NEW_DEFAULT:
  1122. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1123. break;
  1124. case WASM_OP_ARRAY_NEW:
  1125. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1126. break;
  1127. case WASM_OP_ARRAY_NEW_DEFAULT:
  1128. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1129. break;
  1130. case WASM_OP_ARRAY_NEW_FIXED:
  1131. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1132. break;
  1133. case WASM_OP_REF_I31:
  1134. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1135. break;
  1136. default:
  1137. bh_assert(0);
  1138. break;
  1139. }
  1140. }
  1141. #endif /* end of WASM_ENABLE_GC != 0 */
  1142. *p_buf = p;
  1143. destroy_const_expr_stack(&const_expr_ctx);
  1144. return true;
  1145. fail:
  1146. destroy_const_expr_stack(&const_expr_ctx);
  1147. return false;
  1148. }
  1149. static bool
  1150. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1151. {
  1152. if (mutable >= 2) {
  1153. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1154. return false;
  1155. }
  1156. return true;
  1157. }
  1158. #if WASM_ENABLE_GC != 0
  1159. static void
  1160. destroy_func_type(WASMFuncType *type)
  1161. {
  1162. /* Destroy the reference type hash set */
  1163. if (type->ref_type_maps)
  1164. wasm_runtime_free(type->ref_type_maps);
  1165. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1166. && WASM_ENABLE_LAZY_JIT != 0
  1167. if (type->call_to_llvm_jit_from_fast_jit)
  1168. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1169. #endif
  1170. /* Free the type */
  1171. wasm_runtime_free(type);
  1172. }
  1173. static void
  1174. destroy_struct_type(WASMStructType *type)
  1175. {
  1176. if (type->ref_type_maps)
  1177. wasm_runtime_free(type->ref_type_maps);
  1178. wasm_runtime_free(type);
  1179. }
  1180. static void
  1181. destroy_array_type(WASMArrayType *type)
  1182. {
  1183. wasm_runtime_free(type);
  1184. }
  1185. static void
  1186. destroy_wasm_type(WASMType *type)
  1187. {
  1188. if (type->ref_count > 1) {
  1189. /* The type is referenced by other types
  1190. of current wasm module */
  1191. type->ref_count--;
  1192. return;
  1193. }
  1194. if (type->type_flag == WASM_TYPE_FUNC)
  1195. destroy_func_type((WASMFuncType *)type);
  1196. else if (type->type_flag == WASM_TYPE_STRUCT)
  1197. destroy_struct_type((WASMStructType *)type);
  1198. else if (type->type_flag == WASM_TYPE_ARRAY)
  1199. destroy_array_type((WASMArrayType *)type);
  1200. else {
  1201. bh_assert(0);
  1202. }
  1203. }
  1204. /* Resolve (ref null ht) or (ref ht) */
  1205. static bool
  1206. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1207. WASMModule *module, uint32 type_count, bool nullable,
  1208. WASMRefType *ref_type, char *error_buf,
  1209. uint32 error_buf_size)
  1210. {
  1211. const uint8 *p = *p_buf, *p_end = buf_end;
  1212. ref_type->ref_type =
  1213. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1214. ref_type->ref_ht_common.nullable = nullable;
  1215. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1216. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1217. /* heap type is (type i), i : typeidx, >= 0 */
  1218. if (!check_type_index(module, type_count,
  1219. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1220. error_buf_size)) {
  1221. return false;
  1222. }
  1223. }
  1224. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1225. /* heap type is func, extern, any, eq, i31 or data */
  1226. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1227. return false;
  1228. }
  1229. *p_buf = p;
  1230. return true;
  1231. fail:
  1232. return false;
  1233. }
  1234. static bool
  1235. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1236. WASMModule *module, uint32 type_count,
  1237. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1238. bool allow_packed_type, char *error_buf,
  1239. uint32 error_buf_size)
  1240. {
  1241. const uint8 *p = *p_buf, *p_end = buf_end;
  1242. uint8 type;
  1243. memset(ref_type, 0, sizeof(WASMRefType));
  1244. CHECK_BUF(p, p_end, 1);
  1245. type = read_uint8(p);
  1246. if (wasm_is_reftype_htref_nullable(type)) {
  1247. /* (ref null ht) */
  1248. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1249. ref_type, error_buf, error_buf_size))
  1250. return false;
  1251. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1252. *p_need_ref_type_map = true;
  1253. else {
  1254. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1255. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1256. multi-byte type to one-byte type to reduce the footprint and
  1257. the complexity of type equal/subtype checking */
  1258. ref_type->ref_type =
  1259. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1260. *p_need_ref_type_map = false;
  1261. }
  1262. }
  1263. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1264. /* (ref ht) */
  1265. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1266. ref_type, error_buf, error_buf_size))
  1267. return false;
  1268. *p_need_ref_type_map = true;
  1269. #if WASM_ENABLE_STRINGREF != 0
  1270. /* covert (ref string) to stringref */
  1271. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1272. ref_type->ref_type =
  1273. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1274. *p_need_ref_type_map = false;
  1275. }
  1276. #endif
  1277. }
  1278. else {
  1279. /* type which can be represented by one byte */
  1280. if (!is_valid_value_type_for_interpreter(type)
  1281. && !(allow_packed_type && is_packed_type(type))) {
  1282. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1283. return false;
  1284. }
  1285. ref_type->ref_type = type;
  1286. *p_need_ref_type_map = false;
  1287. #if WASM_ENABLE_WAMR_COMPILER != 0
  1288. /* If any value's type is v128, mark the module as SIMD used */
  1289. if (type == VALUE_TYPE_V128)
  1290. module->is_simd_used = true;
  1291. #endif
  1292. }
  1293. *p_buf = p;
  1294. return true;
  1295. fail:
  1296. return false;
  1297. }
  1298. static WASMRefType *
  1299. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1300. char *error_buf, uint32 error_buf_size)
  1301. {
  1302. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1303. if (!ret) {
  1304. set_error_buf(error_buf, error_buf_size,
  1305. "insert ref type to hash set failed");
  1306. }
  1307. return ret;
  1308. }
  1309. static bool
  1310. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1311. uint32 type_count, uint32 type_idx, char *error_buf,
  1312. uint32 error_buf_size)
  1313. {
  1314. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1315. uint32 param_count, result_count, i, j = 0;
  1316. uint32 param_cell_num, ret_cell_num;
  1317. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1318. uint64 total_size;
  1319. bool need_ref_type_map;
  1320. WASMRefType ref_type;
  1321. WASMFuncType *type = NULL;
  1322. /* Parse first time to resolve param count, result count and
  1323. ref type map count */
  1324. read_leb_uint32(p, p_end, param_count);
  1325. p_org = p;
  1326. for (i = 0; i < param_count; i++) {
  1327. if (!resolve_value_type(&p, p_end, module, type_count,
  1328. &need_ref_type_map, &ref_type, false, error_buf,
  1329. error_buf_size)) {
  1330. return false;
  1331. }
  1332. if (need_ref_type_map)
  1333. ref_type_map_count++;
  1334. }
  1335. read_leb_uint32(p, p_end, result_count);
  1336. for (i = 0; i < result_count; i++) {
  1337. if (!resolve_value_type(&p, p_end, module, type_count,
  1338. &need_ref_type_map, &ref_type, false, error_buf,
  1339. error_buf_size)) {
  1340. return false;
  1341. }
  1342. if (need_ref_type_map) {
  1343. ref_type_map_count++;
  1344. result_ref_type_map_count++;
  1345. }
  1346. }
  1347. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1348. "ref type map count: %d",
  1349. type_idx, param_count, result_count, ref_type_map_count);
  1350. /* Parse second time to resolve param types, result types and
  1351. ref type map info */
  1352. p = p_org;
  1353. total_size = offsetof(WASMFuncType, types)
  1354. + sizeof(uint8) * (uint64)(param_count + result_count);
  1355. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1356. return false;
  1357. }
  1358. if (ref_type_map_count > 0) {
  1359. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1360. if (!(type->ref_type_maps =
  1361. loader_malloc(total_size, error_buf, error_buf_size))) {
  1362. goto fail;
  1363. }
  1364. }
  1365. type->base_type.type_flag = WASM_TYPE_FUNC;
  1366. type->param_count = param_count;
  1367. type->result_count = result_count;
  1368. type->ref_type_map_count = ref_type_map_count;
  1369. if (ref_type_map_count > 0) {
  1370. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1371. - result_ref_type_map_count;
  1372. }
  1373. for (i = 0; i < param_count; i++) {
  1374. if (!resolve_value_type(&p, p_end, module, type_count,
  1375. &need_ref_type_map, &ref_type, false, error_buf,
  1376. error_buf_size)) {
  1377. goto fail;
  1378. }
  1379. type->types[i] = ref_type.ref_type;
  1380. if (need_ref_type_map) {
  1381. type->ref_type_maps[j].index = i;
  1382. if (!(type->ref_type_maps[j++].ref_type =
  1383. reftype_set_insert(module->ref_type_set, &ref_type,
  1384. error_buf, error_buf_size))) {
  1385. goto fail;
  1386. }
  1387. }
  1388. }
  1389. read_leb_uint32(p, p_end, result_count);
  1390. for (i = 0; i < result_count; i++) {
  1391. if (!resolve_value_type(&p, p_end, module, type_count,
  1392. &need_ref_type_map, &ref_type, false, error_buf,
  1393. error_buf_size)) {
  1394. goto fail;
  1395. }
  1396. type->types[param_count + i] = ref_type.ref_type;
  1397. if (need_ref_type_map) {
  1398. type->ref_type_maps[j].index = param_count + i;
  1399. if (!(type->ref_type_maps[j++].ref_type =
  1400. reftype_set_insert(module->ref_type_set, &ref_type,
  1401. error_buf, error_buf_size))) {
  1402. goto fail;
  1403. }
  1404. }
  1405. }
  1406. bh_assert(j == type->ref_type_map_count);
  1407. #if TRACE_WASM_LOADER != 0
  1408. os_printf("type %d = ", type_idx);
  1409. wasm_dump_func_type(type);
  1410. #endif
  1411. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1412. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1413. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1414. set_error_buf(error_buf, error_buf_size,
  1415. "param count or result count too large");
  1416. goto fail;
  1417. }
  1418. type->param_cell_num = (uint16)param_cell_num;
  1419. type->ret_cell_num = (uint16)ret_cell_num;
  1420. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1421. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1422. #endif
  1423. #if WASM_ENABLE_WAMR_COMPILER != 0
  1424. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1425. if (type->types[i] == VALUE_TYPE_V128)
  1426. module->is_simd_used = true;
  1427. }
  1428. #endif
  1429. *p_buf = p;
  1430. module->types[type_idx] = (WASMType *)type;
  1431. return true;
  1432. fail:
  1433. if (type)
  1434. destroy_func_type(type);
  1435. return false;
  1436. }
  1437. static bool
  1438. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1439. WASMModule *module, uint32 type_count, uint32 type_idx,
  1440. char *error_buf, uint32 error_buf_size)
  1441. {
  1442. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1443. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1444. uint32 i, j = 0, offset;
  1445. uint16 *reference_table;
  1446. uint64 total_size;
  1447. uint8 mutable;
  1448. bool need_ref_type_map;
  1449. WASMRefType ref_type;
  1450. WASMStructType *type = NULL;
  1451. /* Parse first time to resolve field count and ref type map count */
  1452. read_leb_uint32(p, p_end, field_count);
  1453. p_org = p;
  1454. for (i = 0; i < field_count; i++) {
  1455. if (!resolve_value_type(&p, p_end, module, type_count,
  1456. &need_ref_type_map, &ref_type, true, error_buf,
  1457. error_buf_size)) {
  1458. return false;
  1459. }
  1460. if (need_ref_type_map)
  1461. ref_type_map_count++;
  1462. if (wasm_is_type_reftype(ref_type.ref_type))
  1463. ref_field_count++;
  1464. CHECK_BUF(p, p_end, 1);
  1465. mutable = read_uint8(p);
  1466. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1467. return false;
  1468. }
  1469. }
  1470. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1471. type_idx, field_count, ref_type_map_count);
  1472. /* Parse second time to resolve field types and ref type map info */
  1473. p = p_org;
  1474. total_size = offsetof(WASMStructType, fields)
  1475. + sizeof(WASMStructFieldType) * (uint64)field_count
  1476. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1477. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1478. return false;
  1479. }
  1480. if (ref_type_map_count > 0) {
  1481. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1482. if (!(type->ref_type_maps =
  1483. loader_malloc(total_size, error_buf, error_buf_size))) {
  1484. goto fail;
  1485. }
  1486. }
  1487. type->reference_table = reference_table =
  1488. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1489. + sizeof(WASMStructFieldType) * field_count);
  1490. *reference_table++ = ref_field_count;
  1491. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1492. type->field_count = field_count;
  1493. type->ref_type_map_count = ref_type_map_count;
  1494. offset = (uint32)offsetof(WASMStructObject, field_data);
  1495. for (i = 0; i < field_count; i++) {
  1496. if (!resolve_value_type(&p, p_end, module, type_count,
  1497. &need_ref_type_map, &ref_type, true, error_buf,
  1498. error_buf_size)) {
  1499. goto fail;
  1500. }
  1501. type->fields[i].field_type = ref_type.ref_type;
  1502. if (need_ref_type_map) {
  1503. type->ref_type_maps[j].index = i;
  1504. if (!(type->ref_type_maps[j++].ref_type =
  1505. reftype_set_insert(module->ref_type_set, &ref_type,
  1506. error_buf, error_buf_size))) {
  1507. goto fail;
  1508. }
  1509. }
  1510. CHECK_BUF(p, p_end, 1);
  1511. type->fields[i].field_flags = read_uint8(p);
  1512. type->fields[i].field_size =
  1513. (uint8)wasm_reftype_size(ref_type.ref_type);
  1514. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1515. || defined(BUILD_TARGET_X86_32))
  1516. if (type->fields[i].field_size == 2)
  1517. offset = align_uint(offset, 2);
  1518. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1519. offset = align_uint(offset, 4);
  1520. #endif
  1521. type->fields[i].field_offset = offset;
  1522. if (wasm_is_type_reftype(ref_type.ref_type))
  1523. *reference_table++ = offset;
  1524. offset += type->fields[i].field_size;
  1525. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1526. type->fields[i].field_flags, type->fields[i].field_type);
  1527. }
  1528. type->total_size = offset;
  1529. bh_assert(j == type->ref_type_map_count);
  1530. #if TRACE_WASM_LOADER != 0
  1531. os_printf("type %d = ", type_idx);
  1532. wasm_dump_struct_type(type);
  1533. #endif
  1534. *p_buf = p;
  1535. module->types[type_idx] = (WASMType *)type;
  1536. return true;
  1537. fail:
  1538. if (type)
  1539. destroy_struct_type(type);
  1540. return false;
  1541. }
  1542. static bool
  1543. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1544. WASMModule *module, uint32 type_count, uint32 type_idx,
  1545. char *error_buf, uint32 error_buf_size)
  1546. {
  1547. const uint8 *p = *p_buf, *p_end = buf_end;
  1548. uint8 mutable;
  1549. bool need_ref_type_map;
  1550. WASMRefType ref_type;
  1551. WASMArrayType *type = NULL;
  1552. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1553. &ref_type, true, error_buf, error_buf_size)) {
  1554. return false;
  1555. }
  1556. CHECK_BUF(p, p_end, 1);
  1557. mutable = read_uint8(p);
  1558. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1559. return false;
  1560. }
  1561. LOG_VERBOSE("type %u: array", type_idx);
  1562. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1563. error_buf_size))) {
  1564. return false;
  1565. }
  1566. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1567. type->elem_flags = mutable;
  1568. type->elem_type = ref_type.ref_type;
  1569. if (need_ref_type_map) {
  1570. if (!(type->elem_ref_type =
  1571. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1572. error_buf_size))) {
  1573. goto fail;
  1574. }
  1575. }
  1576. #if TRACE_WASM_LOADER != 0
  1577. os_printf("type %d = ", type_idx);
  1578. wasm_dump_array_type(type);
  1579. #endif
  1580. *p_buf = p;
  1581. module->types[type_idx] = (WASMType *)type;
  1582. return true;
  1583. fail:
  1584. if (type)
  1585. destroy_array_type(type);
  1586. return false;
  1587. }
  1588. static bool
  1589. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1590. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1591. {
  1592. if (heap_type >= 0) {
  1593. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1594. error_buf_size)) {
  1595. return false;
  1596. }
  1597. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1598. heap_type);
  1599. }
  1600. else {
  1601. if (!wasm_is_valid_heap_type(heap_type)) {
  1602. set_error_buf(error_buf, error_buf_size, "unknown type");
  1603. return false;
  1604. }
  1605. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1606. heap_type);
  1607. if (nullable) {
  1608. /* For (ref null func/extern/any/eq/i31/data),
  1609. they are same as
  1610. funcref/externref/anyref/eqref/i31ref/dataref,
  1611. we convert the multi-byte type to one-byte
  1612. type to reduce the footprint and the
  1613. complexity of type equal/subtype checking */
  1614. ref_type->ref_type =
  1615. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1616. }
  1617. }
  1618. return true;
  1619. }
  1620. static void
  1621. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1622. WASMRefType *ref_type2)
  1623. {
  1624. /**
  1625. * The difference rt1 ∖ rt2 between two reference types is defined as
  1626. * follows:
  1627. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1628. * (ref ht2) = (ref null?1 ht1)
  1629. */
  1630. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1631. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1632. ref_type1, wasm_reftype_struct_size(ref_type1));
  1633. }
  1634. else {
  1635. ref_type_diff->ref_type = ref_type1->ref_type;
  1636. }
  1637. if (ref_type2->ref_ht_common.nullable) {
  1638. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1639. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1640. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1641. (int32)ref_type_diff->ref_type - 0x80);
  1642. }
  1643. else {
  1644. ref_type_diff->ref_ht_typeidx.nullable = false;
  1645. }
  1646. }
  1647. }
  1648. #else /* else of WASM_ENABLE_GC != 0 */
  1649. static void
  1650. destroy_wasm_type(WASMType *type)
  1651. {
  1652. if (type->ref_count > 1) {
  1653. /* The type is referenced by other types
  1654. of current wasm module */
  1655. type->ref_count--;
  1656. return;
  1657. }
  1658. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1659. && WASM_ENABLE_LAZY_JIT != 0
  1660. if (type->call_to_llvm_jit_from_fast_jit)
  1661. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1662. #endif
  1663. wasm_runtime_free(type);
  1664. }
  1665. #endif /* end of WASM_ENABLE_GC != 0 */
  1666. static bool
  1667. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1668. char *error_buf, uint32 error_buf_size)
  1669. {
  1670. const uint8 *p = buf, *p_end = buf_end;
  1671. uint32 type_count, i;
  1672. uint64 total_size;
  1673. uint8 flag;
  1674. #if WASM_ENABLE_GC != 0
  1675. uint32 processed_type_count = 0;
  1676. #endif
  1677. read_leb_uint32(p, p_end, type_count);
  1678. if (type_count) {
  1679. module->type_count = type_count;
  1680. total_size = sizeof(WASMType *) * (uint64)type_count;
  1681. if (!(module->types =
  1682. loader_malloc(total_size, error_buf, error_buf_size))) {
  1683. return false;
  1684. }
  1685. #if WASM_ENABLE_GC == 0
  1686. for (i = 0; i < type_count; i++) {
  1687. WASMFuncType *type;
  1688. const uint8 *p_org;
  1689. uint32 param_count, result_count, j;
  1690. uint32 param_cell_num, ret_cell_num;
  1691. CHECK_BUF(p, p_end, 1);
  1692. flag = read_uint8(p);
  1693. if (flag != 0x60) {
  1694. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1695. return false;
  1696. }
  1697. read_leb_uint32(p, p_end, param_count);
  1698. /* Resolve param count and result count firstly */
  1699. p_org = p;
  1700. CHECK_BUF(p, p_end, param_count);
  1701. p += param_count;
  1702. read_leb_uint32(p, p_end, result_count);
  1703. CHECK_BUF(p, p_end, result_count);
  1704. p = p_org;
  1705. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1706. set_error_buf(error_buf, error_buf_size,
  1707. "param count or result count too large");
  1708. return false;
  1709. }
  1710. total_size = offsetof(WASMFuncType, types)
  1711. + sizeof(uint8) * (uint64)(param_count + result_count);
  1712. if (!(type = module->types[i] =
  1713. loader_malloc(total_size, error_buf, error_buf_size))) {
  1714. return false;
  1715. }
  1716. /* Resolve param types and result types */
  1717. type->ref_count = 1;
  1718. type->param_count = (uint16)param_count;
  1719. type->result_count = (uint16)result_count;
  1720. for (j = 0; j < param_count; j++) {
  1721. CHECK_BUF(p, p_end, 1);
  1722. type->types[j] = read_uint8(p);
  1723. }
  1724. read_leb_uint32(p, p_end, result_count);
  1725. for (j = 0; j < result_count; j++) {
  1726. CHECK_BUF(p, p_end, 1);
  1727. type->types[param_count + j] = read_uint8(p);
  1728. }
  1729. for (j = 0; j < param_count + result_count; j++) {
  1730. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  1731. set_error_buf(error_buf, error_buf_size,
  1732. "unknown value type");
  1733. return false;
  1734. }
  1735. }
  1736. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1737. ret_cell_num =
  1738. wasm_get_cell_num(type->types + param_count, result_count);
  1739. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1740. set_error_buf(error_buf, error_buf_size,
  1741. "param count or result count too large");
  1742. return false;
  1743. }
  1744. type->param_cell_num = (uint16)param_cell_num;
  1745. type->ret_cell_num = (uint16)ret_cell_num;
  1746. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1747. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1748. #endif
  1749. #if WASM_ENABLE_WAMR_COMPILER != 0
  1750. for (j = 0; j < type->param_count + type->result_count; j++) {
  1751. if (type->types[j] == VALUE_TYPE_V128)
  1752. module->is_simd_used = true;
  1753. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1754. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1755. module->is_ref_types_used = true;
  1756. }
  1757. #endif
  1758. /* If there is already a same type created, use it instead */
  1759. for (j = 0; j < i; j++) {
  1760. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1761. if (module->types[j]->ref_count == UINT16_MAX) {
  1762. set_error_buf(error_buf, error_buf_size,
  1763. "wasm type's ref count too large");
  1764. return false;
  1765. }
  1766. destroy_wasm_type(type);
  1767. module->types[i] = module->types[j];
  1768. module->types[j]->ref_count++;
  1769. break;
  1770. }
  1771. }
  1772. }
  1773. #else /* else of WASM_ENABLE_GC == 0 */
  1774. for (i = 0; i < type_count; i++) {
  1775. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1776. uint32 rec_count = 1, j;
  1777. bool is_sub_final = true;
  1778. CHECK_BUF(p, p_end, 1);
  1779. flag = read_uint8(p);
  1780. if (flag == DEFINED_TYPE_REC) {
  1781. read_leb_uint32(p, p_end, rec_count);
  1782. if (rec_count > 1) {
  1783. uint64 new_total_size;
  1784. /* integer overflow */
  1785. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1786. set_error_buf(error_buf, error_buf_size,
  1787. "recursive type count too large");
  1788. return false;
  1789. }
  1790. module->type_count += rec_count - 1;
  1791. new_total_size =
  1792. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1793. if (new_total_size > UINT32_MAX) {
  1794. set_error_buf(error_buf, error_buf_size,
  1795. "allocate memory failed");
  1796. return false;
  1797. }
  1798. MEM_REALLOC(module->types, (uint32)total_size,
  1799. (uint32)new_total_size);
  1800. total_size = new_total_size;
  1801. }
  1802. LOG_VERBOSE("Processing rec group [%d-%d]",
  1803. processed_type_count,
  1804. processed_type_count + rec_count - 1);
  1805. }
  1806. else {
  1807. p--;
  1808. }
  1809. for (j = 0; j < rec_count; j++) {
  1810. WASMType *cur_type = NULL;
  1811. CHECK_BUF(p, p_end, 1);
  1812. flag = read_uint8(p);
  1813. parent_type_idx = -1;
  1814. if (flag == DEFINED_TYPE_SUB
  1815. || flag == DEFINED_TYPE_SUB_FINAL) {
  1816. read_leb_uint32(p, p_end, super_type_count);
  1817. if (super_type_count > 1) {
  1818. set_error_buf(error_buf, error_buf_size,
  1819. "super type count too large");
  1820. return false;
  1821. }
  1822. if (super_type_count > 0) {
  1823. read_leb_uint32(p, p_end, parent_type_idx);
  1824. if (parent_type_idx >= processed_type_count + j) {
  1825. set_error_buf_v(error_buf, error_buf_size,
  1826. "unknown type %d", parent_type_idx);
  1827. return false;
  1828. }
  1829. if (module->types[parent_type_idx]->is_sub_final) {
  1830. set_error_buf(error_buf, error_buf_size,
  1831. "sub type can not inherit from "
  1832. "a final super type");
  1833. return false;
  1834. }
  1835. }
  1836. if (flag == DEFINED_TYPE_SUB)
  1837. is_sub_final = false;
  1838. CHECK_BUF(p, p_end, 1);
  1839. flag = read_uint8(p);
  1840. }
  1841. if (flag == DEFINED_TYPE_FUNC) {
  1842. if (!resolve_func_type(&p, buf_end, module,
  1843. processed_type_count + rec_count,
  1844. processed_type_count + j, error_buf,
  1845. error_buf_size)) {
  1846. return false;
  1847. }
  1848. }
  1849. else if (flag == DEFINED_TYPE_STRUCT) {
  1850. if (!resolve_struct_type(&p, buf_end, module,
  1851. processed_type_count + rec_count,
  1852. processed_type_count + j,
  1853. error_buf, error_buf_size)) {
  1854. return false;
  1855. }
  1856. }
  1857. else if (flag == DEFINED_TYPE_ARRAY) {
  1858. if (!resolve_array_type(&p, buf_end, module,
  1859. processed_type_count + rec_count,
  1860. processed_type_count + j, error_buf,
  1861. error_buf_size)) {
  1862. return false;
  1863. }
  1864. }
  1865. else {
  1866. set_error_buf(error_buf, error_buf_size,
  1867. "invalid type flag");
  1868. return false;
  1869. }
  1870. cur_type = module->types[processed_type_count + j];
  1871. cur_type->ref_count = 1;
  1872. cur_type->parent_type_idx = parent_type_idx;
  1873. cur_type->is_sub_final = is_sub_final;
  1874. cur_type->rec_count = rec_count;
  1875. cur_type->rec_idx = j;
  1876. cur_type->rec_begin_type_idx = processed_type_count;
  1877. }
  1878. /* resolve subtyping relationship in current rec group */
  1879. for (j = 0; j < rec_count; j++) {
  1880. WASMType *cur_type = module->types[processed_type_count + j];
  1881. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1882. WASMType *parent_type =
  1883. module->types[cur_type->parent_type_idx];
  1884. cur_type->parent_type = parent_type;
  1885. cur_type->root_type = parent_type->root_type;
  1886. if (parent_type->inherit_depth == UINT16_MAX) {
  1887. set_error_buf(error_buf, error_buf_size,
  1888. "parent type's inherit depth too large");
  1889. return false;
  1890. }
  1891. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1892. }
  1893. else {
  1894. cur_type->parent_type = NULL;
  1895. cur_type->root_type = cur_type;
  1896. cur_type->inherit_depth = 0;
  1897. }
  1898. }
  1899. for (j = 0; j < rec_count; j++) {
  1900. WASMType *cur_type = module->types[processed_type_count + j];
  1901. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1902. WASMType *parent_type =
  1903. module->types[cur_type->parent_type_idx];
  1904. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1905. module->types,
  1906. module->type_count)) {
  1907. set_error_buf(error_buf, error_buf_size,
  1908. "sub type does not match super type");
  1909. return false;
  1910. }
  1911. }
  1912. }
  1913. /* If there is already an equivalence type or a group of equivalence
  1914. recursive types created, use it or them instead */
  1915. for (j = 0; j < processed_type_count;) {
  1916. WASMType *src_type = module->types[j];
  1917. WASMType *cur_type = module->types[processed_type_count];
  1918. uint32 k, src_rec_count;
  1919. src_rec_count = src_type->rec_count;
  1920. if (src_rec_count != rec_count) {
  1921. /* no type equivalence */
  1922. j += src_rec_count;
  1923. continue;
  1924. }
  1925. for (k = 0; k < rec_count; k++) {
  1926. src_type = module->types[j + k];
  1927. cur_type = module->types[processed_type_count + k];
  1928. if (!wasm_type_equal(src_type, cur_type, module->types,
  1929. module->type_count)) {
  1930. break;
  1931. }
  1932. }
  1933. if (k < rec_count) {
  1934. /* no type equivalence */
  1935. j += src_rec_count;
  1936. continue;
  1937. }
  1938. /* type equivalence */
  1939. for (k = 0; k < rec_count; k++) {
  1940. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1941. set_error_buf(error_buf, error_buf_size,
  1942. "wasm type's ref count too large");
  1943. return false;
  1944. }
  1945. destroy_wasm_type(module->types[processed_type_count + k]);
  1946. module->types[processed_type_count + k] =
  1947. module->types[j + k];
  1948. module->types[j + k]->ref_count++;
  1949. }
  1950. break;
  1951. }
  1952. if (rec_count > 1) {
  1953. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1954. processed_type_count,
  1955. processed_type_count + rec_count - 1);
  1956. }
  1957. processed_type_count += rec_count;
  1958. }
  1959. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1960. * module->type_count,
  1961. error_buf, error_buf_size))) {
  1962. return false;
  1963. }
  1964. #endif /* end of WASM_ENABLE_GC == 0 */
  1965. }
  1966. if (p != p_end) {
  1967. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1968. return false;
  1969. }
  1970. LOG_VERBOSE("Load type section success.\n");
  1971. return true;
  1972. fail:
  1973. return false;
  1974. }
  1975. static void
  1976. adjust_table_max_size(bool is_table64, uint32 init_size, uint32 max_size_flag,
  1977. uint32 *max_size)
  1978. {
  1979. uint32 default_max_size;
  1980. /* TODO: current still use UINT32_MAX as upper limit for table size to keep
  1981. * ABI unchanged */
  1982. (void)is_table64;
  1983. if (UINT32_MAX / 2 > init_size)
  1984. default_max_size = init_size * 2;
  1985. else
  1986. default_max_size = UINT32_MAX;
  1987. if (default_max_size < WASM_TABLE_MAX_SIZE)
  1988. default_max_size = WASM_TABLE_MAX_SIZE;
  1989. if (max_size_flag) {
  1990. /* module defines the table limitation */
  1991. bh_assert(init_size <= *max_size);
  1992. if (init_size < *max_size) {
  1993. *max_size =
  1994. *max_size < default_max_size ? *max_size : default_max_size;
  1995. }
  1996. }
  1997. else {
  1998. /* partial defined table limitation, gives a default value */
  1999. *max_size = default_max_size;
  2000. }
  2001. }
  2002. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  2003. /**
  2004. * Find export item of a module with export info:
  2005. * module name, field name and export kind
  2006. */
  2007. static WASMExport *
  2008. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  2009. const char *field_name, uint8 export_kind,
  2010. char *error_buf, uint32 error_buf_size)
  2011. {
  2012. WASMExport *export =
  2013. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2014. export_kind, error_buf, error_buf_size);
  2015. return export;
  2016. }
  2017. #endif
  2018. #if WASM_ENABLE_MULTI_MODULE != 0
  2019. static WASMTable *
  2020. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2021. uint32 init_size, uint32 max_size, char *error_buf,
  2022. uint32 error_buf_size)
  2023. {
  2024. WASMModuleCommon *module_reg;
  2025. WASMTable *table = NULL;
  2026. WASMExport *export = NULL;
  2027. WASMModule *module = NULL;
  2028. module_reg = wasm_runtime_find_module_registered(module_name);
  2029. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2030. LOG_DEBUG("can not find a module named %s for table", module_name);
  2031. set_error_buf(error_buf, error_buf_size, "unknown import");
  2032. return NULL;
  2033. }
  2034. module = (WASMModule *)module_reg;
  2035. export =
  2036. wasm_loader_find_export(module, module_name, table_name,
  2037. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2038. if (!export) {
  2039. return NULL;
  2040. }
  2041. /* resolve table and check the init/max size */
  2042. if (export->index < module->import_table_count) {
  2043. table =
  2044. module->import_tables[export->index].u.table.import_table_linked;
  2045. }
  2046. else {
  2047. table = &(module->tables[export->index - module->import_table_count]);
  2048. }
  2049. if (table->table_type.init_size < init_size
  2050. || table->table_type.max_size > max_size) {
  2051. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2052. module_name, table_name, table->table_type.init_size,
  2053. table->table_type.max_size, init_size, max_size);
  2054. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2055. return NULL;
  2056. }
  2057. return table;
  2058. }
  2059. static WASMMemory *
  2060. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2061. uint32 init_page_count, uint32 max_page_count,
  2062. char *error_buf, uint32 error_buf_size)
  2063. {
  2064. WASMModuleCommon *module_reg;
  2065. WASMMemory *memory = NULL;
  2066. WASMExport *export = NULL;
  2067. WASMModule *module = NULL;
  2068. module_reg = wasm_runtime_find_module_registered(module_name);
  2069. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2070. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2071. set_error_buf(error_buf, error_buf_size, "unknown import");
  2072. return NULL;
  2073. }
  2074. module = (WASMModule *)module_reg;
  2075. export =
  2076. wasm_loader_find_export(module, module_name, memory_name,
  2077. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2078. if (!export) {
  2079. return NULL;
  2080. }
  2081. /* resolve memory and check the init/max page count */
  2082. if (export->index < module->import_memory_count) {
  2083. memory = module->import_memories[export->index]
  2084. .u.memory.import_memory_linked;
  2085. }
  2086. else {
  2087. memory =
  2088. &(module->memories[export->index - module->import_memory_count]);
  2089. }
  2090. if (memory->init_page_count < init_page_count
  2091. || memory->max_page_count > max_page_count) {
  2092. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2093. module_name, memory_name, memory->init_page_count,
  2094. memory->max_page_count, init_page_count, max_page_count);
  2095. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2096. return NULL;
  2097. }
  2098. return memory;
  2099. }
  2100. static WASMGlobal *
  2101. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2102. uint8 type, bool is_mutable, char *error_buf,
  2103. uint32 error_buf_size)
  2104. {
  2105. WASMModuleCommon *module_reg;
  2106. WASMGlobal *global = NULL;
  2107. WASMExport *export = NULL;
  2108. WASMModule *module = NULL;
  2109. module_reg = wasm_runtime_find_module_registered(module_name);
  2110. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2111. LOG_DEBUG("can not find a module named %s for global", module_name);
  2112. set_error_buf(error_buf, error_buf_size, "unknown import");
  2113. return NULL;
  2114. }
  2115. module = (WASMModule *)module_reg;
  2116. export =
  2117. wasm_loader_find_export(module, module_name, global_name,
  2118. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2119. if (!export) {
  2120. return NULL;
  2121. }
  2122. /* resolve and check the global */
  2123. if (export->index < module->import_global_count) {
  2124. global =
  2125. module->import_globals[export->index].u.global.import_global_linked;
  2126. }
  2127. else {
  2128. global =
  2129. &(module->globals[export->index - module->import_global_count]);
  2130. }
  2131. if (global->type.val_type != type
  2132. || global->type.is_mutable != is_mutable) {
  2133. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2134. module_name, global_name, global->type.val_type,
  2135. global->type.is_mutable, type, is_mutable);
  2136. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2137. return NULL;
  2138. }
  2139. return global;
  2140. }
  2141. #if WASM_ENABLE_TAGS != 0
  2142. static WASMTag *
  2143. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2144. const WASMType *expected_tag_type,
  2145. uint32 *linked_tag_index, char *error_buf,
  2146. uint32 error_buf_size)
  2147. {
  2148. WASMModuleCommon *module_reg;
  2149. WASMTag *tag = NULL;
  2150. WASMExport *export = NULL;
  2151. WASMModule *module = NULL;
  2152. module_reg = wasm_runtime_find_module_registered(module_name);
  2153. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2154. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2155. tag_name);
  2156. set_error_buf(error_buf, error_buf_size, "unknown import");
  2157. return NULL;
  2158. }
  2159. module = (WASMModule *)module_reg;
  2160. export =
  2161. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2162. error_buf, error_buf_size);
  2163. if (!export) {
  2164. return NULL;
  2165. }
  2166. /* resolve tag type and tag */
  2167. if (export->index < module->import_tag_count) {
  2168. /* importing an imported tag from the submodule */
  2169. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2170. }
  2171. else {
  2172. /* importing an section tag from the submodule */
  2173. tag = module->tags[export->index - module->import_tag_count];
  2174. }
  2175. /* check function type */
  2176. if (!wasm_type_equal(expected_tag_type, tag->tag_type, module->types,
  2177. module->type_count)) {
  2178. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2179. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2180. return NULL;
  2181. }
  2182. if (linked_tag_index != NULL) {
  2183. *linked_tag_index = export->index;
  2184. }
  2185. return tag;
  2186. }
  2187. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2188. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2189. static bool
  2190. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2191. const WASMModule *parent_module,
  2192. const char *sub_module_name, const char *function_name,
  2193. WASMFunctionImport *function, bool no_resolve,
  2194. char *error_buf, uint32 error_buf_size)
  2195. {
  2196. const uint8 *p = *p_buf, *p_end = buf_end;
  2197. uint32 declare_type_index = 0;
  2198. read_leb_uint32(p, p_end, declare_type_index);
  2199. *p_buf = p;
  2200. if (declare_type_index >= parent_module->type_count) {
  2201. set_error_buf(error_buf, error_buf_size, "unknown type");
  2202. return false;
  2203. }
  2204. #if WASM_ENABLE_GC != 0
  2205. function->type_idx = declare_type_index;
  2206. #endif
  2207. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2208. declare_type_index = wasm_get_smallest_type_idx(
  2209. parent_module->types, parent_module->type_count, declare_type_index);
  2210. #endif
  2211. function->func_type =
  2212. (WASMFuncType *)parent_module->types[declare_type_index];
  2213. function->module_name = (char *)sub_module_name;
  2214. function->field_name = (char *)function_name;
  2215. function->attachment = NULL;
  2216. function->signature = NULL;
  2217. function->call_conv_raw = false;
  2218. /* lookup registered native symbols first */
  2219. if (!no_resolve) {
  2220. wasm_resolve_import_func(parent_module, function);
  2221. }
  2222. return true;
  2223. fail:
  2224. return false;
  2225. }
  2226. static bool
  2227. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2228. uint32 error_buf_size)
  2229. {
  2230. if (max_size < init_size) {
  2231. set_error_buf(error_buf, error_buf_size,
  2232. "size minimum must not be greater than maximum");
  2233. return false;
  2234. }
  2235. return true;
  2236. }
  2237. static bool
  2238. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2239. WASMModule *parent_module, const char *sub_module_name,
  2240. const char *table_name, WASMTableImport *table,
  2241. char *error_buf, uint32 error_buf_size)
  2242. {
  2243. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2244. uint32 declare_elem_type = 0, table_flag = 0, declare_init_size = 0,
  2245. declare_max_size = 0;
  2246. #if WASM_ENABLE_MULTI_MODULE != 0
  2247. WASMModule *sub_module = NULL;
  2248. WASMTable *linked_table = NULL;
  2249. #endif
  2250. #if WASM_ENABLE_GC != 0
  2251. WASMRefType ref_type;
  2252. bool need_ref_type_map;
  2253. #endif
  2254. bool is_table64 = false;
  2255. #if WASM_ENABLE_GC == 0
  2256. CHECK_BUF(p, p_end, 1);
  2257. /* 0x70 or 0x6F */
  2258. declare_elem_type = read_uint8(p);
  2259. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2260. #if WASM_ENABLE_REF_TYPES != 0
  2261. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2262. #endif
  2263. ) {
  2264. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2265. return false;
  2266. }
  2267. #else /* else of WASM_ENABLE_GC == 0 */
  2268. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2269. &need_ref_type_map, &ref_type, false, error_buf,
  2270. error_buf_size)) {
  2271. return false;
  2272. }
  2273. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2274. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2275. return false;
  2276. }
  2277. declare_elem_type = ref_type.ref_type;
  2278. if (need_ref_type_map) {
  2279. if (!(table->table_type.elem_ref_type =
  2280. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2281. error_buf, error_buf_size))) {
  2282. return false;
  2283. }
  2284. }
  2285. #if TRACE_WASM_LOADER != 0
  2286. os_printf("import table type: ");
  2287. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2288. os_printf("\n");
  2289. #endif
  2290. #endif /* end of WASM_ENABLE_GC == 0 */
  2291. p_org = p;
  2292. read_leb_uint32(p, p_end, table_flag);
  2293. is_table64 = table_flag & TABLE64_FLAG;
  2294. if (p - p_org > 1) {
  2295. LOG_VERBOSE("integer representation too long(import table)");
  2296. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2297. return false;
  2298. }
  2299. if (!wasm_table_check_flags(table_flag, error_buf, error_buf_size, false)) {
  2300. return false;
  2301. }
  2302. read_leb_uint32(p, p_end, declare_init_size);
  2303. if (table_flag & MAX_TABLE_SIZE_FLAG) {
  2304. read_leb_uint32(p, p_end, declare_max_size);
  2305. if (!check_table_max_size(declare_init_size, declare_max_size,
  2306. error_buf, error_buf_size))
  2307. return false;
  2308. }
  2309. adjust_table_max_size(is_table64, declare_init_size,
  2310. table_flag & MAX_TABLE_SIZE_FLAG, &declare_max_size);
  2311. *p_buf = p;
  2312. #if WASM_ENABLE_MULTI_MODULE != 0
  2313. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2314. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2315. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2316. error_buf_size);
  2317. if (sub_module) {
  2318. linked_table = wasm_loader_resolve_table(
  2319. sub_module_name, table_name, declare_init_size,
  2320. declare_max_size, error_buf, error_buf_size);
  2321. if (linked_table) {
  2322. /* reset with linked table limit */
  2323. declare_elem_type = linked_table->table_type.elem_type;
  2324. declare_init_size = linked_table->table_type.init_size;
  2325. declare_max_size = linked_table->table_type.max_size;
  2326. table_flag = linked_table->table_type.flags;
  2327. table->import_table_linked = linked_table;
  2328. table->import_module = sub_module;
  2329. }
  2330. }
  2331. }
  2332. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2333. /* (table (export "table") 10 20 funcref) */
  2334. /* (table (export "table64") 10 20 funcref) */
  2335. /* we need this section working in wamrc */
  2336. if (!strcmp("spectest", sub_module_name)) {
  2337. const uint32 spectest_table_init_size = 10;
  2338. const uint32 spectest_table_max_size = 20;
  2339. if (strcmp("table", table_name)
  2340. #if WASM_ENABLE_MEMORY64 != 0
  2341. && strcmp("table64", table_name)
  2342. #endif
  2343. ) {
  2344. set_error_buf(error_buf, error_buf_size,
  2345. "incompatible import type or unknown import");
  2346. return false;
  2347. }
  2348. if (declare_init_size > spectest_table_init_size
  2349. || declare_max_size < spectest_table_max_size) {
  2350. set_error_buf(error_buf, error_buf_size,
  2351. "incompatible import type");
  2352. return false;
  2353. }
  2354. declare_init_size = spectest_table_init_size;
  2355. declare_max_size = spectest_table_max_size;
  2356. }
  2357. /* now we believe all declaration are ok */
  2358. table->table_type.elem_type = declare_elem_type;
  2359. table->table_type.init_size = declare_init_size;
  2360. table->table_type.flags = table_flag;
  2361. table->table_type.max_size = declare_max_size;
  2362. #if WASM_ENABLE_WAMR_COMPILER != 0
  2363. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2364. parent_module->is_ref_types_used = true;
  2365. #endif
  2366. (void)parent_module;
  2367. return true;
  2368. fail:
  2369. return false;
  2370. }
  2371. static bool
  2372. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2373. uint32 error_buf_size)
  2374. {
  2375. uint32 default_max_size =
  2376. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2377. if (!is_memory64 && init_size > default_max_size) {
  2378. set_error_buf(error_buf, error_buf_size,
  2379. "memory size must be at most 65536 pages (4GiB)");
  2380. return false;
  2381. }
  2382. #if WASM_ENABLE_MEMORY64 != 0
  2383. else if (is_memory64 && init_size > default_max_size) {
  2384. set_error_buf(
  2385. error_buf, error_buf_size,
  2386. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2387. return false;
  2388. }
  2389. #endif
  2390. return true;
  2391. }
  2392. static bool
  2393. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2394. char *error_buf, uint32 error_buf_size)
  2395. {
  2396. uint32 default_max_size =
  2397. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2398. if (max_size < init_size) {
  2399. set_error_buf(error_buf, error_buf_size,
  2400. "size minimum must not be greater than maximum");
  2401. return false;
  2402. }
  2403. if (!is_memory64 && max_size > default_max_size) {
  2404. set_error_buf(error_buf, error_buf_size,
  2405. "memory size must be at most 65536 pages (4GiB)");
  2406. return false;
  2407. }
  2408. #if WASM_ENABLE_MEMORY64 != 0
  2409. else if (is_memory64 && max_size > default_max_size) {
  2410. set_error_buf(
  2411. error_buf, error_buf_size,
  2412. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2413. return false;
  2414. }
  2415. #endif
  2416. return true;
  2417. }
  2418. static bool
  2419. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2420. WASMModule *parent_module, const char *sub_module_name,
  2421. const char *memory_name, WASMMemoryImport *memory,
  2422. char *error_buf, uint32 error_buf_size)
  2423. {
  2424. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2425. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2426. uint32 pool_size = wasm_runtime_memory_pool_size();
  2427. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2428. / DEFAULT_NUM_BYTES_PER_PAGE;
  2429. #else
  2430. uint32 max_page_count;
  2431. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2432. uint32 mem_flag = 0;
  2433. bool is_memory64 = false;
  2434. uint32 declare_init_page_count = 0;
  2435. uint32 declare_max_page_count = 0;
  2436. #if WASM_ENABLE_MULTI_MODULE != 0
  2437. WASMModule *sub_module = NULL;
  2438. WASMMemory *linked_memory = NULL;
  2439. #endif
  2440. p_org = p;
  2441. read_leb_uint32(p, p_end, mem_flag);
  2442. is_memory64 = mem_flag & MEMORY64_FLAG;
  2443. if (p - p_org > 1) {
  2444. LOG_VERBOSE("integer representation too long(import memory)");
  2445. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2446. return false;
  2447. }
  2448. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2449. return false;
  2450. }
  2451. read_leb_uint32(p, p_end, declare_init_page_count);
  2452. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2453. error_buf_size)) {
  2454. return false;
  2455. }
  2456. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2457. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2458. #endif
  2459. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2460. read_leb_uint32(p, p_end, declare_max_page_count);
  2461. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2462. declare_max_page_count, error_buf,
  2463. error_buf_size)) {
  2464. return false;
  2465. }
  2466. if (declare_max_page_count > max_page_count) {
  2467. declare_max_page_count = max_page_count;
  2468. }
  2469. }
  2470. else {
  2471. /* Limit the maximum memory size to max_page_count */
  2472. declare_max_page_count = max_page_count;
  2473. }
  2474. #if WASM_ENABLE_MULTI_MODULE != 0
  2475. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2476. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2477. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2478. error_buf_size);
  2479. if (sub_module) {
  2480. linked_memory = wasm_loader_resolve_memory(
  2481. sub_module_name, memory_name, declare_init_page_count,
  2482. declare_max_page_count, error_buf, error_buf_size);
  2483. if (linked_memory) {
  2484. /**
  2485. * reset with linked memory limit
  2486. */
  2487. memory->import_module = sub_module;
  2488. memory->import_memory_linked = linked_memory;
  2489. declare_init_page_count = linked_memory->init_page_count;
  2490. declare_max_page_count = linked_memory->max_page_count;
  2491. }
  2492. }
  2493. }
  2494. #endif
  2495. /* (memory (export "memory") 1 2) */
  2496. if (!strcmp("spectest", sub_module_name)) {
  2497. uint32 spectest_memory_init_page = 1;
  2498. uint32 spectest_memory_max_page = 2;
  2499. if (strcmp("memory", memory_name)) {
  2500. set_error_buf(error_buf, error_buf_size,
  2501. "incompatible import type or unknown import");
  2502. return false;
  2503. }
  2504. if (declare_init_page_count > spectest_memory_init_page
  2505. || declare_max_page_count < spectest_memory_max_page) {
  2506. set_error_buf(error_buf, error_buf_size,
  2507. "incompatible import type");
  2508. return false;
  2509. }
  2510. declare_init_page_count = spectest_memory_init_page;
  2511. declare_max_page_count = spectest_memory_max_page;
  2512. }
  2513. #if WASM_ENABLE_WASI_TEST != 0
  2514. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2515. else if (!strcmp("foo", sub_module_name)) {
  2516. uint32 spectest_memory_init_page = 1;
  2517. uint32 spectest_memory_max_page = 1;
  2518. if (strcmp("bar", memory_name)) {
  2519. set_error_buf(error_buf, error_buf_size,
  2520. "incompatible import type or unknown import");
  2521. return false;
  2522. }
  2523. if (declare_init_page_count > spectest_memory_init_page
  2524. || declare_max_page_count < spectest_memory_max_page) {
  2525. set_error_buf(error_buf, error_buf_size,
  2526. "incompatible import type");
  2527. return false;
  2528. }
  2529. declare_init_page_count = spectest_memory_init_page;
  2530. declare_max_page_count = spectest_memory_max_page;
  2531. }
  2532. #endif
  2533. /* now we believe all declaration are ok */
  2534. memory->mem_type.flags = mem_flag;
  2535. memory->mem_type.init_page_count = declare_init_page_count;
  2536. memory->mem_type.max_page_count = declare_max_page_count;
  2537. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2538. *p_buf = p;
  2539. (void)parent_module;
  2540. return true;
  2541. fail:
  2542. return false;
  2543. }
  2544. #if WASM_ENABLE_TAGS != 0
  2545. static bool
  2546. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2547. const WASMModule *parent_module, /* this module ! */
  2548. const char *sub_module_name, const char *tag_name,
  2549. WASMTagImport *tag, /* structure to fill */
  2550. char *error_buf, uint32 error_buf_size)
  2551. {
  2552. /* attribute and type of the import statement */
  2553. uint8 declare_tag_attribute;
  2554. uint32 declare_type_index;
  2555. const uint8 *p = *p_buf, *p_end = buf_end;
  2556. #if WASM_ENABLE_MULTI_MODULE != 0
  2557. WASMModule *sub_module = NULL;
  2558. #endif
  2559. /* get the one byte attribute */
  2560. CHECK_BUF(p, p_end, 1);
  2561. declare_tag_attribute = read_uint8(p);
  2562. if (declare_tag_attribute != 0) {
  2563. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2564. goto fail;
  2565. }
  2566. /* get type */
  2567. read_leb_uint32(p, p_end, declare_type_index);
  2568. /* compare against module->types */
  2569. if (declare_type_index >= parent_module->type_count) {
  2570. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2571. goto fail;
  2572. }
  2573. WASMFuncType *declare_tag_type =
  2574. (WASMFuncType *)parent_module->types[declare_type_index];
  2575. /* check, that the type of the declared tag returns void */
  2576. if (declare_tag_type->result_count != 0) {
  2577. set_error_buf(error_buf, error_buf_size,
  2578. "tag type signature does not return void");
  2579. goto fail;
  2580. }
  2581. #if WASM_ENABLE_MULTI_MODULE != 0
  2582. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2583. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2584. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2585. error_buf_size);
  2586. if (sub_module) {
  2587. /* wasm_loader_resolve_tag checks, that the imported tag
  2588. * and the declared tag have the same type
  2589. */
  2590. uint32 linked_tag_index = 0;
  2591. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2592. sub_module_name, tag_name, declare_tag_type,
  2593. &linked_tag_index /* out */, error_buf, error_buf_size);
  2594. if (linked_tag) {
  2595. tag->import_module = sub_module;
  2596. tag->import_tag_linked = linked_tag;
  2597. tag->import_tag_index_linked = linked_tag_index;
  2598. }
  2599. }
  2600. }
  2601. #endif
  2602. /* store to module tag declarations */
  2603. tag->attribute = declare_tag_attribute;
  2604. tag->type = declare_type_index;
  2605. tag->module_name = (char *)sub_module_name;
  2606. tag->field_name = (char *)tag_name;
  2607. tag->tag_type = declare_tag_type;
  2608. *p_buf = p;
  2609. (void)parent_module;
  2610. LOG_VERBOSE("Load tag import success\n");
  2611. return true;
  2612. fail:
  2613. return false;
  2614. }
  2615. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2616. static bool
  2617. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2618. WASMModule *parent_module, char *sub_module_name,
  2619. char *global_name, WASMGlobalImport *global, char *error_buf,
  2620. uint32 error_buf_size)
  2621. {
  2622. const uint8 *p = *p_buf, *p_end = buf_end;
  2623. uint8 declare_type = 0;
  2624. uint8 declare_mutable = 0;
  2625. #if WASM_ENABLE_MULTI_MODULE != 0
  2626. WASMModule *sub_module = NULL;
  2627. WASMGlobal *linked_global = NULL;
  2628. #endif
  2629. #if WASM_ENABLE_GC != 0
  2630. WASMRefType ref_type;
  2631. bool need_ref_type_map;
  2632. #endif
  2633. bool ret = false;
  2634. #if WASM_ENABLE_GC == 0
  2635. CHECK_BUF(p, p_end, 2);
  2636. /* global type */
  2637. declare_type = read_uint8(p);
  2638. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2639. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2640. return false;
  2641. }
  2642. declare_mutable = read_uint8(p);
  2643. #else
  2644. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2645. &need_ref_type_map, &ref_type, false, error_buf,
  2646. error_buf_size)) {
  2647. return false;
  2648. }
  2649. declare_type = ref_type.ref_type;
  2650. if (need_ref_type_map) {
  2651. if (!(global->ref_type =
  2652. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2653. error_buf, error_buf_size))) {
  2654. return false;
  2655. }
  2656. }
  2657. #if TRACE_WASM_LOADER != 0
  2658. os_printf("import global type: ");
  2659. wasm_dump_value_type(declare_type, global->ref_type);
  2660. os_printf("\n");
  2661. #endif
  2662. CHECK_BUF(p, p_end, 1);
  2663. declare_mutable = read_uint8(p);
  2664. #endif /* end of WASM_ENABLE_GC == 0 */
  2665. *p_buf = p;
  2666. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2667. return false;
  2668. }
  2669. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2670. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2671. global);
  2672. if (ret) {
  2673. if (global->type.val_type != declare_type
  2674. || global->type.is_mutable != declare_mutable) {
  2675. set_error_buf(error_buf, error_buf_size,
  2676. "incompatible import type");
  2677. return false;
  2678. }
  2679. global->is_linked = true;
  2680. }
  2681. #endif
  2682. #if WASM_ENABLE_MULTI_MODULE != 0
  2683. if (!global->is_linked
  2684. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2685. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2686. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2687. error_buf_size);
  2688. if (sub_module) {
  2689. /* check sub modules */
  2690. linked_global = wasm_loader_resolve_global(
  2691. sub_module_name, global_name, declare_type, declare_mutable,
  2692. error_buf, error_buf_size);
  2693. if (linked_global) {
  2694. global->import_module = sub_module;
  2695. global->import_global_linked = linked_global;
  2696. global->is_linked = true;
  2697. }
  2698. }
  2699. }
  2700. #endif
  2701. global->module_name = sub_module_name;
  2702. global->field_name = global_name;
  2703. global->type.val_type = declare_type;
  2704. global->type.is_mutable = (declare_mutable == 1);
  2705. #if WASM_ENABLE_WAMR_COMPILER != 0
  2706. if (global->type.val_type == VALUE_TYPE_V128)
  2707. parent_module->is_simd_used = true;
  2708. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2709. parent_module->is_ref_types_used = true;
  2710. #endif
  2711. (void)parent_module;
  2712. (void)ret;
  2713. return true;
  2714. fail:
  2715. return false;
  2716. }
  2717. static bool
  2718. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2719. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2720. {
  2721. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2722. #if WASM_ENABLE_GC != 0
  2723. WASMRefType ref_type;
  2724. bool need_ref_type_map;
  2725. #endif
  2726. bool is_table64 = false;
  2727. #if WASM_ENABLE_GC == 0
  2728. CHECK_BUF(p, p_end, 1);
  2729. /* 0x70 or 0x6F */
  2730. table->table_type.elem_type = read_uint8(p);
  2731. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  2732. #if WASM_ENABLE_REF_TYPES != 0
  2733. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  2734. #endif
  2735. ) {
  2736. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2737. return false;
  2738. }
  2739. #else /* else of WASM_ENABLE_GC == 0 */
  2740. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2741. &need_ref_type_map, &ref_type, false, error_buf,
  2742. error_buf_size)) {
  2743. return false;
  2744. }
  2745. table->table_type.elem_type = ref_type.ref_type;
  2746. if (need_ref_type_map) {
  2747. if (!(table->table_type.elem_ref_type =
  2748. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2749. error_buf_size))) {
  2750. return false;
  2751. }
  2752. }
  2753. #if TRACE_WASM_LOADER != 0
  2754. os_printf("table type: ");
  2755. wasm_dump_value_type(table->table_type.elem_type,
  2756. table->table_type.elem_ref_type);
  2757. os_printf("\n");
  2758. #endif
  2759. #endif /* end of WASM_ENABLE_GC == 0 */
  2760. p_org = p;
  2761. read_leb_uint32(p, p_end, table->table_type.flags);
  2762. is_table64 = table->table_type.flags & TABLE64_FLAG;
  2763. if (p - p_org > 1) {
  2764. LOG_VERBOSE("integer representation too long(table)");
  2765. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2766. return false;
  2767. }
  2768. if (!wasm_table_check_flags(table->table_type.flags, error_buf,
  2769. error_buf_size, false)) {
  2770. return false;
  2771. }
  2772. read_leb_uint32(p, p_end, table->table_type.init_size);
  2773. if (table->table_type.flags & MAX_TABLE_SIZE_FLAG) {
  2774. read_leb_uint32(p, p_end, table->table_type.max_size);
  2775. if (!check_table_max_size(table->table_type.init_size,
  2776. table->table_type.max_size, error_buf,
  2777. error_buf_size))
  2778. return false;
  2779. }
  2780. adjust_table_max_size(is_table64, table->table_type.init_size,
  2781. table->table_type.flags & MAX_TABLE_SIZE_FLAG,
  2782. &table->table_type.max_size);
  2783. #if WASM_ENABLE_WAMR_COMPILER != 0
  2784. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2785. module->is_ref_types_used = true;
  2786. #endif
  2787. *p_buf = p;
  2788. return true;
  2789. fail:
  2790. return false;
  2791. }
  2792. static bool
  2793. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2794. char *error_buf, uint32 error_buf_size)
  2795. {
  2796. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2797. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2798. uint32 pool_size = wasm_runtime_memory_pool_size();
  2799. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2800. / DEFAULT_NUM_BYTES_PER_PAGE;
  2801. #else
  2802. uint32 max_page_count;
  2803. #endif
  2804. bool is_memory64 = false;
  2805. p_org = p;
  2806. read_leb_uint32(p, p_end, memory->flags);
  2807. is_memory64 = memory->flags & MEMORY64_FLAG;
  2808. if (p - p_org > 1) {
  2809. LOG_VERBOSE("integer representation too long(memory)");
  2810. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2811. return false;
  2812. }
  2813. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2814. false)) {
  2815. return false;
  2816. }
  2817. read_leb_uint32(p, p_end, memory->init_page_count);
  2818. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2819. error_buf_size))
  2820. return false;
  2821. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2822. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2823. #endif
  2824. if (memory->flags & 1) {
  2825. read_leb_uint32(p, p_end, memory->max_page_count);
  2826. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2827. memory->max_page_count, error_buf,
  2828. error_buf_size))
  2829. return false;
  2830. if (memory->max_page_count > max_page_count)
  2831. memory->max_page_count = max_page_count;
  2832. }
  2833. else {
  2834. /* Limit the maximum memory size to max_page_count */
  2835. memory->max_page_count = max_page_count;
  2836. }
  2837. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2838. *p_buf = p;
  2839. return true;
  2840. fail:
  2841. return false;
  2842. }
  2843. static int
  2844. cmp_export_name(const void *a, const void *b)
  2845. {
  2846. return strcmp(*(char **)a, *(char **)b);
  2847. }
  2848. static bool
  2849. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2850. bool is_load_from_file_buf, bool no_resolve,
  2851. char *error_buf, uint32 error_buf_size)
  2852. {
  2853. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2854. uint32 import_count, name_len, type_index, i, u32, flags;
  2855. uint64 total_size;
  2856. WASMImport *import;
  2857. WASMImport *import_functions = NULL, *import_tables = NULL;
  2858. WASMImport *import_memories = NULL, *import_globals = NULL;
  2859. #if WASM_ENABLE_TAGS != 0
  2860. WASMImport *import_tags = NULL;
  2861. #endif
  2862. char *sub_module_name, *field_name;
  2863. uint8 u8, kind, global_type;
  2864. read_leb_uint32(p, p_end, import_count);
  2865. if (import_count) {
  2866. module->import_count = import_count;
  2867. total_size = sizeof(WASMImport) * (uint64)import_count;
  2868. if (!(module->imports =
  2869. loader_malloc(total_size, error_buf, error_buf_size))) {
  2870. return false;
  2871. }
  2872. p_old = p;
  2873. /* Scan firstly to get import count of each type */
  2874. for (i = 0; i < import_count; i++) {
  2875. /* module name */
  2876. read_leb_uint32(p, p_end, name_len);
  2877. CHECK_BUF(p, p_end, name_len);
  2878. p += name_len;
  2879. /* field name */
  2880. read_leb_uint32(p, p_end, name_len);
  2881. CHECK_BUF(p, p_end, name_len);
  2882. p += name_len;
  2883. CHECK_BUF(p, p_end, 1);
  2884. /* 0x00/0x01/0x02/0x03/0x04 */
  2885. kind = read_uint8(p);
  2886. switch (kind) {
  2887. case IMPORT_KIND_FUNC: /* import function */
  2888. read_leb_uint32(p, p_end, type_index);
  2889. module->import_function_count++;
  2890. break;
  2891. case IMPORT_KIND_TABLE: /* import table */
  2892. CHECK_BUF(p, p_end, 1);
  2893. /* 0x70 */
  2894. u8 = read_uint8(p);
  2895. read_leb_uint32(p, p_end, flags);
  2896. read_leb_uint32(p, p_end, u32);
  2897. if (flags & 1)
  2898. read_leb_uint32(p, p_end, u32);
  2899. module->import_table_count++;
  2900. if (module->import_table_count > 1) {
  2901. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2902. set_error_buf(error_buf, error_buf_size,
  2903. "multiple tables");
  2904. return false;
  2905. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2906. module->is_ref_types_used = true;
  2907. #endif
  2908. }
  2909. break;
  2910. case IMPORT_KIND_MEMORY: /* import memory */
  2911. read_leb_uint32(p, p_end, flags);
  2912. read_leb_uint32(p, p_end, u32);
  2913. if (flags & 1)
  2914. read_leb_uint32(p, p_end, u32);
  2915. module->import_memory_count++;
  2916. #if WASM_ENABLE_MULTI_MEMORY == 0
  2917. if (module->import_memory_count > 1) {
  2918. set_error_buf(error_buf, error_buf_size,
  2919. "multiple memories");
  2920. return false;
  2921. }
  2922. #endif
  2923. break;
  2924. #if WASM_ENABLE_TAGS != 0
  2925. case IMPORT_KIND_TAG: /* import tags */
  2926. /* it only counts the number of tags to import */
  2927. module->import_tag_count++;
  2928. CHECK_BUF(p, p_end, 1);
  2929. u8 = read_uint8(p);
  2930. read_leb_uint32(p, p_end, type_index);
  2931. break;
  2932. #endif
  2933. case IMPORT_KIND_GLOBAL: /* import global */
  2934. #if WASM_ENABLE_GC != 0
  2935. /* valtype */
  2936. CHECK_BUF(p, p_end, 1);
  2937. global_type = read_uint8(p);
  2938. if (wasm_is_type_multi_byte_type(global_type)) {
  2939. int32 heap_type;
  2940. read_leb_int32(p, p_end, heap_type);
  2941. (void)heap_type;
  2942. }
  2943. /* mutability */
  2944. CHECK_BUF(p, p_end, 1);
  2945. p += 1;
  2946. #else
  2947. CHECK_BUF(p, p_end, 2);
  2948. p += 2;
  2949. #endif
  2950. (void)global_type;
  2951. module->import_global_count++;
  2952. break;
  2953. default:
  2954. set_error_buf(error_buf, error_buf_size,
  2955. "invalid import kind");
  2956. return false;
  2957. }
  2958. }
  2959. if (module->import_function_count)
  2960. import_functions = module->import_functions = module->imports;
  2961. if (module->import_table_count)
  2962. import_tables = module->import_tables =
  2963. module->imports + module->import_function_count;
  2964. if (module->import_memory_count)
  2965. import_memories = module->import_memories =
  2966. module->imports + module->import_function_count
  2967. + module->import_table_count;
  2968. #if WASM_ENABLE_TAGS != 0
  2969. if (module->import_tag_count)
  2970. import_tags = module->import_tags =
  2971. module->imports + module->import_function_count
  2972. + module->import_table_count + module->import_memory_count;
  2973. if (module->import_global_count)
  2974. import_globals = module->import_globals =
  2975. module->imports + module->import_function_count
  2976. + module->import_table_count + module->import_memory_count
  2977. + module->import_tag_count;
  2978. #else
  2979. if (module->import_global_count)
  2980. import_globals = module->import_globals =
  2981. module->imports + module->import_function_count
  2982. + module->import_table_count + module->import_memory_count;
  2983. #endif
  2984. p = p_old;
  2985. /* Scan again to resolve the data */
  2986. for (i = 0; i < import_count; i++) {
  2987. /* load module name */
  2988. read_leb_uint32(p, p_end, name_len);
  2989. CHECK_BUF(p, p_end, name_len);
  2990. if (!(sub_module_name = wasm_const_str_list_insert(
  2991. p, name_len, module, is_load_from_file_buf, error_buf,
  2992. error_buf_size))) {
  2993. return false;
  2994. }
  2995. p += name_len;
  2996. /* load field name */
  2997. read_leb_uint32(p, p_end, name_len);
  2998. CHECK_BUF(p, p_end, name_len);
  2999. if (!(field_name = wasm_const_str_list_insert(
  3000. p, name_len, module, is_load_from_file_buf, error_buf,
  3001. error_buf_size))) {
  3002. return false;
  3003. }
  3004. p += name_len;
  3005. CHECK_BUF(p, p_end, 1);
  3006. /* 0x00/0x01/0x02/0x03/0x4 */
  3007. kind = read_uint8(p);
  3008. switch (kind) {
  3009. case IMPORT_KIND_FUNC: /* import function */
  3010. bh_assert(import_functions);
  3011. import = import_functions++;
  3012. if (!load_function_import(&p, p_end, module,
  3013. sub_module_name, field_name,
  3014. &import->u.function, no_resolve,
  3015. error_buf, error_buf_size)) {
  3016. return false;
  3017. }
  3018. break;
  3019. case IMPORT_KIND_TABLE: /* import table */
  3020. bh_assert(import_tables);
  3021. import = import_tables++;
  3022. if (!load_table_import(&p, p_end, module, sub_module_name,
  3023. field_name, &import->u.table,
  3024. error_buf, error_buf_size)) {
  3025. LOG_DEBUG("can not import such a table (%s,%s)",
  3026. sub_module_name, field_name);
  3027. return false;
  3028. }
  3029. break;
  3030. case IMPORT_KIND_MEMORY: /* import memory */
  3031. bh_assert(import_memories);
  3032. import = import_memories++;
  3033. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3034. field_name, &import->u.memory,
  3035. error_buf, error_buf_size)) {
  3036. return false;
  3037. }
  3038. break;
  3039. #if WASM_ENABLE_TAGS != 0
  3040. case IMPORT_KIND_TAG:
  3041. bh_assert(import_tags);
  3042. import = import_tags++;
  3043. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3044. field_name, &import->u.tag, error_buf,
  3045. error_buf_size)) {
  3046. return false;
  3047. }
  3048. break;
  3049. #endif
  3050. case IMPORT_KIND_GLOBAL: /* import global */
  3051. bh_assert(import_globals);
  3052. import = import_globals++;
  3053. if (!load_global_import(&p, p_end, module, sub_module_name,
  3054. field_name, &import->u.global,
  3055. error_buf, error_buf_size)) {
  3056. return false;
  3057. }
  3058. break;
  3059. default:
  3060. set_error_buf(error_buf, error_buf_size,
  3061. "invalid import kind");
  3062. return false;
  3063. }
  3064. import->kind = kind;
  3065. import->u.names.module_name = sub_module_name;
  3066. import->u.names.field_name = field_name;
  3067. }
  3068. #if WASM_ENABLE_LIBC_WASI != 0
  3069. import = module->import_functions;
  3070. for (i = 0; i < module->import_function_count; i++, import++) {
  3071. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3072. || !strcmp(import->u.names.module_name,
  3073. "wasi_snapshot_preview1")) {
  3074. module->import_wasi_api = true;
  3075. break;
  3076. }
  3077. }
  3078. #endif
  3079. }
  3080. if (p != p_end) {
  3081. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3082. return false;
  3083. }
  3084. LOG_VERBOSE("Load import section success.\n");
  3085. (void)u8;
  3086. (void)u32;
  3087. (void)type_index;
  3088. return true;
  3089. fail:
  3090. return false;
  3091. }
  3092. static bool
  3093. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3094. uint32 error_buf_size)
  3095. {
  3096. WASMFuncType *param_type = func->func_type;
  3097. uint32 param_count = param_type->param_count;
  3098. uint8 *param_types = param_type->types;
  3099. uint32 local_count = func->local_count;
  3100. uint8 *local_types = func->local_types;
  3101. uint32 i, local_offset = 0;
  3102. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3103. /*
  3104. * Only allocate memory when total_size is not 0,
  3105. * or the return value of malloc(0) might be NULL on some platforms,
  3106. * which causes wasm loader return false.
  3107. */
  3108. if (total_size > 0
  3109. && !(func->local_offsets =
  3110. loader_malloc(total_size, error_buf, error_buf_size))) {
  3111. return false;
  3112. }
  3113. for (i = 0; i < param_count; i++) {
  3114. func->local_offsets[i] = (uint16)local_offset;
  3115. local_offset += wasm_value_type_cell_num(param_types[i]);
  3116. }
  3117. for (i = 0; i < local_count; i++) {
  3118. func->local_offsets[param_count + i] = (uint16)local_offset;
  3119. local_offset += wasm_value_type_cell_num(local_types[i]);
  3120. }
  3121. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3122. return true;
  3123. }
  3124. static bool
  3125. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3126. const uint8 *buf_code, const uint8 *buf_code_end,
  3127. WASMModule *module, char *error_buf,
  3128. uint32 error_buf_size)
  3129. {
  3130. const uint8 *p = buf, *p_end = buf_end;
  3131. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3132. uint32 func_count;
  3133. uint64 total_size;
  3134. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3135. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3136. uint8 type;
  3137. WASMFunction *func;
  3138. #if WASM_ENABLE_GC != 0
  3139. bool need_ref_type_map;
  3140. WASMRefType ref_type;
  3141. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3142. #endif
  3143. read_leb_uint32(p, p_end, func_count);
  3144. if (buf_code)
  3145. read_leb_uint32(p_code, buf_code_end, code_count);
  3146. if (func_count != code_count) {
  3147. set_error_buf(error_buf, error_buf_size,
  3148. "function and code section have inconsistent lengths or "
  3149. "unexpected end");
  3150. return false;
  3151. }
  3152. if (is_indices_overflow(module->import_function_count, func_count,
  3153. error_buf, error_buf_size))
  3154. return false;
  3155. if (func_count) {
  3156. module->function_count = func_count;
  3157. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3158. if (!(module->functions =
  3159. loader_malloc(total_size, error_buf, error_buf_size))) {
  3160. return false;
  3161. }
  3162. for (i = 0; i < func_count; i++) {
  3163. /* Resolve function type */
  3164. read_leb_uint32(p, p_end, type_index);
  3165. if (type_index >= module->type_count) {
  3166. set_error_buf(error_buf, error_buf_size, "unknown type");
  3167. return false;
  3168. }
  3169. #if WASM_ENABLE_GC != 0
  3170. type_index_org = type_index;
  3171. #endif
  3172. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3173. && WASM_ENABLE_GC == 0
  3174. type_index = wasm_get_smallest_type_idx(
  3175. module->types, module->type_count, type_index);
  3176. #endif
  3177. read_leb_uint32(p_code, buf_code_end, code_size);
  3178. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3179. set_error_buf(error_buf, error_buf_size,
  3180. "invalid function code size");
  3181. return false;
  3182. }
  3183. /* Resolve local set count */
  3184. p_code_end = p_code + code_size;
  3185. local_count = 0;
  3186. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3187. p_code_save = p_code;
  3188. #if WASM_ENABLE_GC != 0
  3189. ref_type_map_count = 0;
  3190. #endif
  3191. /* Calculate total local count */
  3192. for (j = 0; j < local_set_count; j++) {
  3193. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3194. if (sub_local_count > UINT32_MAX - local_count) {
  3195. set_error_buf(error_buf, error_buf_size, "too many locals");
  3196. return false;
  3197. }
  3198. #if WASM_ENABLE_GC == 0
  3199. CHECK_BUF(p_code, buf_code_end, 1);
  3200. /* 0x7F/0x7E/0x7D/0x7C */
  3201. type = read_uint8(p_code);
  3202. local_count += sub_local_count;
  3203. #if WASM_ENABLE_WAMR_COMPILER != 0
  3204. /* If any value's type is v128, mark the module as SIMD used */
  3205. if (type == VALUE_TYPE_V128)
  3206. module->is_simd_used = true;
  3207. #endif
  3208. #else
  3209. if (!resolve_value_type(&p_code, buf_code_end, module,
  3210. module->type_count, &need_ref_type_map,
  3211. &ref_type, false, error_buf,
  3212. error_buf_size)) {
  3213. return false;
  3214. }
  3215. local_count += sub_local_count;
  3216. if (need_ref_type_map)
  3217. ref_type_map_count += sub_local_count;
  3218. #endif
  3219. }
  3220. /* Alloc memory, layout: function structure + local types */
  3221. code_size = (uint32)(p_code_end - p_code);
  3222. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3223. if (!(func = module->functions[i] =
  3224. loader_malloc(total_size, error_buf, error_buf_size))) {
  3225. return false;
  3226. }
  3227. #if WASM_ENABLE_GC != 0
  3228. if (ref_type_map_count > 0) {
  3229. total_size =
  3230. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3231. if (!(func->local_ref_type_maps = loader_malloc(
  3232. total_size, error_buf, error_buf_size))) {
  3233. return false;
  3234. }
  3235. func->local_ref_type_map_count = ref_type_map_count;
  3236. }
  3237. #endif
  3238. /* Set function type, local count, code size and code body */
  3239. func->func_type = (WASMFuncType *)module->types[type_index];
  3240. func->local_count = local_count;
  3241. if (local_count > 0)
  3242. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3243. func->code_size = code_size;
  3244. /*
  3245. * we shall make a copy of code body [p_code, p_code + code_size]
  3246. * when we are worrying about inappropriate releasing behaviour.
  3247. * all code bodies are actually in a buffer which user allocates in
  3248. * his embedding environment and we don't have power on them.
  3249. * it will be like:
  3250. * code_body_cp = malloc(code_size);
  3251. * memcpy(code_body_cp, p_code, code_size);
  3252. * func->code = code_body_cp;
  3253. */
  3254. func->code = (uint8 *)p_code;
  3255. #if WASM_ENABLE_GC != 0
  3256. func->type_idx = type_index_org;
  3257. #endif
  3258. #if WASM_ENABLE_GC != 0
  3259. t = 0;
  3260. #endif
  3261. /* Load each local type */
  3262. p_code = p_code_save;
  3263. local_type_index = 0;
  3264. for (j = 0; j < local_set_count; j++) {
  3265. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3266. /* Note: sub_local_count is allowed to be 0 */
  3267. if (local_type_index > UINT32_MAX - sub_local_count
  3268. || local_type_index + sub_local_count > local_count) {
  3269. set_error_buf(error_buf, error_buf_size,
  3270. "invalid local count");
  3271. return false;
  3272. }
  3273. #if WASM_ENABLE_GC == 0
  3274. CHECK_BUF(p_code, buf_code_end, 1);
  3275. /* 0x7F/0x7E/0x7D/0x7C */
  3276. type = read_uint8(p_code);
  3277. if (!is_valid_value_type_for_interpreter(type)) {
  3278. if (type == VALUE_TYPE_V128)
  3279. set_error_buf(error_buf, error_buf_size,
  3280. "v128 value type requires simd feature");
  3281. else if (type == VALUE_TYPE_FUNCREF
  3282. || type == VALUE_TYPE_EXTERNREF)
  3283. set_error_buf(error_buf, error_buf_size,
  3284. "ref value type requires "
  3285. "reference types feature");
  3286. else
  3287. set_error_buf_v(error_buf, error_buf_size,
  3288. "invalid local type 0x%02X", type);
  3289. return false;
  3290. }
  3291. #else
  3292. if (!resolve_value_type(&p_code, buf_code_end, module,
  3293. module->type_count, &need_ref_type_map,
  3294. &ref_type, false, error_buf,
  3295. error_buf_size)) {
  3296. return false;
  3297. }
  3298. if (need_ref_type_map) {
  3299. WASMRefType *ref_type_tmp;
  3300. if (!(ref_type_tmp = reftype_set_insert(
  3301. module->ref_type_set, &ref_type, error_buf,
  3302. error_buf_size))) {
  3303. return false;
  3304. }
  3305. for (k = 0; k < sub_local_count; k++) {
  3306. func->local_ref_type_maps[t + k].ref_type =
  3307. ref_type_tmp;
  3308. func->local_ref_type_maps[t + k].index =
  3309. local_type_index + k;
  3310. }
  3311. t += sub_local_count;
  3312. }
  3313. type = ref_type.ref_type;
  3314. #endif
  3315. for (k = 0; k < sub_local_count; k++) {
  3316. func->local_types[local_type_index++] = type;
  3317. }
  3318. #if WASM_ENABLE_WAMR_COMPILER != 0
  3319. if (type == VALUE_TYPE_V128)
  3320. module->is_simd_used = true;
  3321. else if (type == VALUE_TYPE_FUNCREF
  3322. || type == VALUE_TYPE_EXTERNREF)
  3323. module->is_ref_types_used = true;
  3324. #endif
  3325. }
  3326. bh_assert(local_type_index == func->local_count);
  3327. #if WASM_ENABLE_GC != 0
  3328. bh_assert(t == func->local_ref_type_map_count);
  3329. #if TRACE_WASM_LOADER != 0
  3330. os_printf("func %u, local types: [", i);
  3331. k = 0;
  3332. for (j = 0; j < func->local_count; j++) {
  3333. WASMRefType *ref_type_tmp = NULL;
  3334. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3335. bh_assert(j == func->local_ref_type_maps[k].index);
  3336. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3337. }
  3338. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3339. if (j < func->local_count - 1)
  3340. os_printf(" ");
  3341. }
  3342. os_printf("]\n");
  3343. #endif
  3344. #endif
  3345. func->param_cell_num = func->func_type->param_cell_num;
  3346. func->ret_cell_num = func->func_type->ret_cell_num;
  3347. local_cell_num =
  3348. wasm_get_cell_num(func->local_types, func->local_count);
  3349. if (local_cell_num > UINT16_MAX) {
  3350. set_error_buf(error_buf, error_buf_size,
  3351. "local count too large");
  3352. return false;
  3353. }
  3354. func->local_cell_num = (uint16)local_cell_num;
  3355. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3356. return false;
  3357. p_code = p_code_end;
  3358. }
  3359. }
  3360. if (p != p_end) {
  3361. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3362. return false;
  3363. }
  3364. LOG_VERBOSE("Load function section success.\n");
  3365. return true;
  3366. fail:
  3367. return false;
  3368. }
  3369. static bool
  3370. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3371. char *error_buf, uint32 error_buf_size)
  3372. {
  3373. const uint8 *p = buf, *p_end = buf_end;
  3374. uint32 table_count, i;
  3375. uint64 total_size;
  3376. WASMTable *table;
  3377. read_leb_uint32(p, p_end, table_count);
  3378. if (module->import_table_count + table_count > 1) {
  3379. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3380. /* a total of one table is allowed */
  3381. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3382. return false;
  3383. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3384. module->is_ref_types_used = true;
  3385. #endif
  3386. }
  3387. if (table_count) {
  3388. module->table_count = table_count;
  3389. total_size = sizeof(WASMTable) * (uint64)table_count;
  3390. if (!(module->tables =
  3391. loader_malloc(total_size, error_buf, error_buf_size))) {
  3392. return false;
  3393. }
  3394. /* load each table */
  3395. table = module->tables;
  3396. for (i = 0; i < table_count; i++, table++) {
  3397. #if WASM_ENABLE_GC != 0
  3398. uint8 flag;
  3399. bool has_init = false;
  3400. CHECK_BUF(buf, buf_end, 1);
  3401. flag = read_uint8(p);
  3402. if (flag == TABLE_INIT_EXPR_FLAG) {
  3403. CHECK_BUF(buf, buf_end, 1);
  3404. flag = read_uint8(p);
  3405. if (flag != 0x00) {
  3406. set_error_buf(error_buf, error_buf_size,
  3407. "invalid leading bytes for table");
  3408. return false;
  3409. }
  3410. has_init = true;
  3411. }
  3412. else {
  3413. p--;
  3414. }
  3415. #endif /* end of WASM_ENABLE_GC != 0 */
  3416. if (!load_table(&p, p_end, module, table, error_buf,
  3417. error_buf_size))
  3418. return false;
  3419. #if WASM_ENABLE_GC != 0
  3420. if (has_init) {
  3421. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3422. table->table_type.elem_type,
  3423. table->table_type.elem_ref_type, error_buf,
  3424. error_buf_size))
  3425. return false;
  3426. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3427. && table->init_expr.init_expr_type
  3428. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3429. set_error_buf(
  3430. error_buf, error_buf_size,
  3431. "unsupported initializer expression for table");
  3432. return false;
  3433. }
  3434. }
  3435. else {
  3436. if (wasm_is_reftype_htref_non_nullable(
  3437. table->table_type.elem_type)) {
  3438. set_error_buf(
  3439. error_buf, error_buf_size,
  3440. "type mismatch: non-nullable table without init expr");
  3441. return false;
  3442. }
  3443. }
  3444. #endif /* end of WASM_ENABLE_GC != 0 */
  3445. #if WASM_ENABLE_WAMR_COMPILER != 0
  3446. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3447. module->is_ref_types_used = true;
  3448. #endif
  3449. }
  3450. }
  3451. if (p != p_end) {
  3452. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3453. return false;
  3454. }
  3455. LOG_VERBOSE("Load table section success.\n");
  3456. return true;
  3457. fail:
  3458. return false;
  3459. }
  3460. static bool
  3461. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3462. char *error_buf, uint32 error_buf_size)
  3463. {
  3464. const uint8 *p = buf, *p_end = buf_end;
  3465. uint32 memory_count, i;
  3466. uint64 total_size;
  3467. WASMMemory *memory;
  3468. read_leb_uint32(p, p_end, memory_count);
  3469. #if WASM_ENABLE_MULTI_MEMORY == 0
  3470. /* a total of one memory is allowed */
  3471. if (module->import_memory_count + memory_count > 1) {
  3472. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3473. return false;
  3474. }
  3475. #endif
  3476. if (memory_count) {
  3477. module->memory_count = memory_count;
  3478. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3479. if (!(module->memories =
  3480. loader_malloc(total_size, error_buf, error_buf_size))) {
  3481. return false;
  3482. }
  3483. /* load each memory */
  3484. memory = module->memories;
  3485. for (i = 0; i < memory_count; i++, memory++)
  3486. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3487. return false;
  3488. }
  3489. if (p != p_end) {
  3490. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3491. return false;
  3492. }
  3493. LOG_VERBOSE("Load memory section success.\n");
  3494. return true;
  3495. fail:
  3496. return false;
  3497. }
  3498. static bool
  3499. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3500. char *error_buf, uint32 error_buf_size)
  3501. {
  3502. const uint8 *p = buf, *p_end = buf_end;
  3503. uint32 global_count, i;
  3504. uint64 total_size;
  3505. WASMGlobal *global;
  3506. uint8 mutable;
  3507. #if WASM_ENABLE_GC != 0
  3508. bool need_ref_type_map;
  3509. WASMRefType ref_type;
  3510. #endif
  3511. read_leb_uint32(p, p_end, global_count);
  3512. if (is_indices_overflow(module->import_global_count, global_count,
  3513. error_buf, error_buf_size))
  3514. return false;
  3515. module->global_count = 0;
  3516. if (global_count) {
  3517. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3518. if (!(module->globals =
  3519. loader_malloc(total_size, error_buf, error_buf_size))) {
  3520. return false;
  3521. }
  3522. global = module->globals;
  3523. for (i = 0; i < global_count; i++, global++) {
  3524. #if WASM_ENABLE_GC == 0
  3525. CHECK_BUF(p, p_end, 2);
  3526. /* global type */
  3527. global->type.val_type = read_uint8(p);
  3528. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3529. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3530. return false;
  3531. }
  3532. mutable = read_uint8(p);
  3533. #else
  3534. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3535. &need_ref_type_map, &ref_type, false,
  3536. error_buf, error_buf_size)) {
  3537. return false;
  3538. }
  3539. global->type.val_type = ref_type.ref_type;
  3540. CHECK_BUF(p, p_end, 1);
  3541. mutable = read_uint8(p);
  3542. #endif /* end of WASM_ENABLE_GC */
  3543. #if WASM_ENABLE_WAMR_COMPILER != 0
  3544. if (global->type.val_type == VALUE_TYPE_V128)
  3545. module->is_simd_used = true;
  3546. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3547. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3548. module->is_ref_types_used = true;
  3549. #endif
  3550. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3551. return false;
  3552. }
  3553. global->type.is_mutable = mutable ? true : false;
  3554. /* initialize expression */
  3555. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3556. global->type.val_type,
  3557. #if WASM_ENABLE_GC == 0
  3558. NULL,
  3559. #else
  3560. &ref_type,
  3561. #endif
  3562. error_buf, error_buf_size))
  3563. return false;
  3564. #if WASM_ENABLE_GC != 0
  3565. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3566. uint8 global_type;
  3567. WASMRefType *global_ref_type;
  3568. uint32 global_idx = global->init_expr.u.global_index;
  3569. if (global->init_expr.u.global_index
  3570. >= module->import_global_count + i) {
  3571. set_error_buf(error_buf, error_buf_size, "unknown global");
  3572. return false;
  3573. }
  3574. if (global_idx < module->import_global_count) {
  3575. global_type = module->import_globals[global_idx]
  3576. .u.global.type.val_type;
  3577. global_ref_type =
  3578. module->import_globals[global_idx].u.global.ref_type;
  3579. }
  3580. else {
  3581. global_type =
  3582. module
  3583. ->globals[global_idx - module->import_global_count]
  3584. .type.val_type;
  3585. global_ref_type =
  3586. module
  3587. ->globals[global_idx - module->import_global_count]
  3588. .ref_type;
  3589. }
  3590. if (!wasm_reftype_is_subtype_of(
  3591. global_type, global_ref_type, global->type.val_type,
  3592. global->ref_type, module->types, module->type_count)) {
  3593. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3594. return false;
  3595. }
  3596. }
  3597. if (need_ref_type_map) {
  3598. if (!(global->ref_type =
  3599. reftype_set_insert(module->ref_type_set, &ref_type,
  3600. error_buf, error_buf_size))) {
  3601. return false;
  3602. }
  3603. }
  3604. #if TRACE_WASM_LOADER != 0
  3605. os_printf("global type: ");
  3606. wasm_dump_value_type(global->type, global->ref_type);
  3607. os_printf("\n");
  3608. #endif
  3609. #endif
  3610. module->global_count++;
  3611. }
  3612. bh_assert(module->global_count == global_count);
  3613. }
  3614. if (p != p_end) {
  3615. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3616. return false;
  3617. }
  3618. LOG_VERBOSE("Load global section success.\n");
  3619. return true;
  3620. fail:
  3621. return false;
  3622. }
  3623. static bool
  3624. check_duplicate_exports(WASMModule *module, char *error_buf,
  3625. uint32 error_buf_size)
  3626. {
  3627. uint32 i;
  3628. bool result = false;
  3629. char *names_buf[32], **names = names_buf;
  3630. if (module->export_count > 32) {
  3631. names = loader_malloc(module->export_count * sizeof(char *), error_buf,
  3632. error_buf_size);
  3633. if (!names) {
  3634. return result;
  3635. }
  3636. }
  3637. for (i = 0; i < module->export_count; i++) {
  3638. names[i] = module->exports[i].name;
  3639. }
  3640. qsort(names, module->export_count, sizeof(char *), cmp_export_name);
  3641. for (i = 1; i < module->export_count; i++) {
  3642. if (!strcmp(names[i], names[i - 1])) {
  3643. set_error_buf(error_buf, error_buf_size, "duplicate export name");
  3644. goto cleanup;
  3645. }
  3646. }
  3647. result = true;
  3648. cleanup:
  3649. if (module->export_count > 32) {
  3650. wasm_runtime_free(names);
  3651. }
  3652. return result;
  3653. }
  3654. static bool
  3655. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3656. bool is_load_from_file_buf, char *error_buf,
  3657. uint32 error_buf_size)
  3658. {
  3659. const uint8 *p = buf, *p_end = buf_end;
  3660. uint32 export_count, i, index;
  3661. uint64 total_size;
  3662. uint32 str_len;
  3663. WASMExport *export;
  3664. read_leb_uint32(p, p_end, export_count);
  3665. if (export_count) {
  3666. module->export_count = export_count;
  3667. total_size = sizeof(WASMExport) * (uint64)export_count;
  3668. if (!(module->exports =
  3669. loader_malloc(total_size, error_buf, error_buf_size))) {
  3670. return false;
  3671. }
  3672. export = module->exports;
  3673. for (i = 0; i < export_count; i++, export ++) {
  3674. #if WASM_ENABLE_THREAD_MGR == 0
  3675. if (p == p_end) {
  3676. /* export section with inconsistent count:
  3677. n export declared, but less than n given */
  3678. set_error_buf(error_buf, error_buf_size,
  3679. "length out of bounds");
  3680. return false;
  3681. }
  3682. #endif
  3683. read_leb_uint32(p, p_end, str_len);
  3684. CHECK_BUF(p, p_end, str_len);
  3685. if (!(export->name = wasm_const_str_list_insert(
  3686. p, str_len, module, is_load_from_file_buf, error_buf,
  3687. error_buf_size))) {
  3688. return false;
  3689. }
  3690. p += str_len;
  3691. CHECK_BUF(p, p_end, 1);
  3692. export->kind = read_uint8(p);
  3693. read_leb_uint32(p, p_end, index);
  3694. export->index = index;
  3695. switch (export->kind) {
  3696. /* function index */
  3697. case EXPORT_KIND_FUNC:
  3698. if (index >= module->function_count
  3699. + module->import_function_count) {
  3700. set_error_buf(error_buf, error_buf_size,
  3701. "unknown function");
  3702. return false;
  3703. }
  3704. #if WASM_ENABLE_SIMD != 0
  3705. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3706. /* TODO: check func type, if it has v128 param or result,
  3707. report error */
  3708. #endif
  3709. #endif
  3710. break;
  3711. /* table index */
  3712. case EXPORT_KIND_TABLE:
  3713. if (index
  3714. >= module->table_count + module->import_table_count) {
  3715. set_error_buf(error_buf, error_buf_size,
  3716. "unknown table");
  3717. return false;
  3718. }
  3719. break;
  3720. /* memory index */
  3721. case EXPORT_KIND_MEMORY:
  3722. if (index
  3723. >= module->memory_count + module->import_memory_count) {
  3724. set_error_buf(error_buf, error_buf_size,
  3725. "unknown memory");
  3726. return false;
  3727. }
  3728. break;
  3729. #if WASM_ENABLE_TAGS != 0
  3730. /* export tag */
  3731. case EXPORT_KIND_TAG:
  3732. if (index >= module->tag_count + module->import_tag_count) {
  3733. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3734. return false;
  3735. }
  3736. break;
  3737. #endif
  3738. /* global index */
  3739. case EXPORT_KIND_GLOBAL:
  3740. if (index
  3741. >= module->global_count + module->import_global_count) {
  3742. set_error_buf(error_buf, error_buf_size,
  3743. "unknown global");
  3744. return false;
  3745. }
  3746. break;
  3747. default:
  3748. set_error_buf(error_buf, error_buf_size,
  3749. "invalid export kind");
  3750. return false;
  3751. }
  3752. }
  3753. if (!check_duplicate_exports(module, error_buf, error_buf_size)) {
  3754. return false;
  3755. }
  3756. }
  3757. if (p != p_end) {
  3758. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3759. return false;
  3760. }
  3761. LOG_VERBOSE("Load export section success.\n");
  3762. return true;
  3763. fail:
  3764. return false;
  3765. }
  3766. static bool
  3767. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3768. uint32 error_buf_size)
  3769. {
  3770. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3771. if (table_index != 0) {
  3772. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3773. return false;
  3774. }
  3775. #endif
  3776. if (table_index >= module->import_table_count + module->table_count) {
  3777. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3778. table_index);
  3779. return false;
  3780. }
  3781. return true;
  3782. }
  3783. static bool
  3784. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3785. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3786. {
  3787. const uint8 *p = *p_buf, *p_end = buf_end;
  3788. uint32 table_index;
  3789. read_leb_uint32(p, p_end, table_index);
  3790. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3791. return false;
  3792. }
  3793. *p_table_index = table_index;
  3794. *p_buf = p;
  3795. return true;
  3796. fail:
  3797. return false;
  3798. }
  3799. /* Element segments must match element type of table */
  3800. static bool
  3801. check_table_elem_type(WASMModule *module, uint32 table_index,
  3802. uint32 type_from_elem_seg, char *error_buf,
  3803. uint32 error_buf_size)
  3804. {
  3805. uint32 table_declared_elem_type;
  3806. if (table_index < module->import_table_count)
  3807. table_declared_elem_type =
  3808. module->import_tables[table_index].u.table.table_type.elem_type;
  3809. else
  3810. table_declared_elem_type =
  3811. (module->tables + table_index)->table_type.elem_type;
  3812. if (table_declared_elem_type == type_from_elem_seg)
  3813. return true;
  3814. #if WASM_ENABLE_GC != 0
  3815. /*
  3816. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3817. */
  3818. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3819. return true;
  3820. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3821. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3822. return true;
  3823. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3824. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3825. return true;
  3826. #endif
  3827. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3828. return false;
  3829. }
  3830. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3831. static bool
  3832. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3833. uint32 *p_elem_type,
  3834. #if WASM_ENABLE_GC != 0
  3835. WASMRefType **p_elem_ref_type,
  3836. #endif
  3837. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3838. {
  3839. const uint8 *p = *p_buf, *p_end = buf_end;
  3840. uint8 elem_type;
  3841. #if WASM_ENABLE_GC != 0
  3842. WASMRefType elem_ref_type;
  3843. bool need_ref_type_map;
  3844. #endif
  3845. CHECK_BUF(p, p_end, 1);
  3846. elem_type = read_uint8(p);
  3847. if (elemkind_zero) {
  3848. if (elem_type != 0) {
  3849. set_error_buf(error_buf, error_buf_size,
  3850. "invalid reference type or unknown type");
  3851. return false;
  3852. }
  3853. else {
  3854. *p_elem_type = VALUE_TYPE_FUNCREF;
  3855. *p_buf = p;
  3856. return true;
  3857. }
  3858. }
  3859. #if WASM_ENABLE_GC == 0
  3860. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3861. set_error_buf(error_buf, error_buf_size,
  3862. "invalid reference type or unknown type");
  3863. return false;
  3864. }
  3865. *p_elem_type = elem_type;
  3866. #else
  3867. p--;
  3868. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3869. module->type_count, &need_ref_type_map,
  3870. &elem_ref_type, false, error_buf, error_buf_size)) {
  3871. return false;
  3872. }
  3873. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3874. set_error_buf(error_buf, error_buf_size,
  3875. "invalid reference type or unknown type");
  3876. return false;
  3877. }
  3878. *p_elem_type = elem_ref_type.ref_type;
  3879. if (need_ref_type_map) {
  3880. if (!(*p_elem_ref_type =
  3881. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3882. error_buf, error_buf_size))) {
  3883. return false;
  3884. }
  3885. }
  3886. #endif
  3887. *p_buf = p;
  3888. return true;
  3889. fail:
  3890. return false;
  3891. }
  3892. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3893. static bool
  3894. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3895. WASMModule *module, WASMTableSeg *table_segment,
  3896. char *error_buf, uint32 error_buf_size)
  3897. {
  3898. const uint8 *p = *p_buf, *p_end = buf_end;
  3899. uint32 function_count, function_index = 0, i;
  3900. uint64 total_size;
  3901. read_leb_uint32(p, p_end, function_count);
  3902. table_segment->value_count = function_count;
  3903. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3904. if (total_size > 0
  3905. && !(table_segment->init_values =
  3906. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3907. error_buf_size))) {
  3908. return false;
  3909. }
  3910. for (i = 0; i < function_count; i++) {
  3911. InitializerExpression *init_expr = &table_segment->init_values[i];
  3912. read_leb_uint32(p, p_end, function_index);
  3913. if (!check_function_index(module, function_index, error_buf,
  3914. error_buf_size)) {
  3915. return false;
  3916. }
  3917. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3918. init_expr->u.ref_index = function_index;
  3919. }
  3920. *p_buf = p;
  3921. return true;
  3922. fail:
  3923. return false;
  3924. }
  3925. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3926. static bool
  3927. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3928. WASMModule *module, WASMTableSeg *table_segment,
  3929. char *error_buf, uint32 error_buf_size)
  3930. {
  3931. const uint8 *p = *p_buf, *p_end = buf_end;
  3932. uint32 ref_count, i;
  3933. uint64 total_size;
  3934. read_leb_uint32(p, p_end, ref_count);
  3935. table_segment->value_count = ref_count;
  3936. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3937. if (total_size > 0
  3938. && !(table_segment->init_values =
  3939. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3940. error_buf_size))) {
  3941. return false;
  3942. }
  3943. for (i = 0; i < ref_count; i++) {
  3944. InitializerExpression *init_expr = &table_segment->init_values[i];
  3945. if (!load_init_expr(module, &p, p_end, init_expr,
  3946. table_segment->elem_type,
  3947. #if WASM_ENABLE_GC == 0
  3948. NULL,
  3949. #else
  3950. table_segment->elem_ref_type,
  3951. #endif
  3952. error_buf, error_buf_size))
  3953. return false;
  3954. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3955. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3956. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3957. && init_expr->init_expr_type
  3958. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3959. }
  3960. *p_buf = p;
  3961. return true;
  3962. fail:
  3963. return false;
  3964. }
  3965. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  3966. static bool
  3967. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3968. WASMModule *module, char *error_buf,
  3969. uint32 error_buf_size)
  3970. {
  3971. const uint8 *p = buf, *p_end = buf_end;
  3972. uint8 table_elem_idx_type;
  3973. uint32 table_segment_count, i;
  3974. uint64 total_size;
  3975. WASMTableSeg *table_segment;
  3976. read_leb_uint32(p, p_end, table_segment_count);
  3977. if (table_segment_count) {
  3978. module->table_seg_count = table_segment_count;
  3979. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  3980. if (!(module->table_segments =
  3981. loader_malloc(total_size, error_buf, error_buf_size))) {
  3982. return false;
  3983. }
  3984. table_segment = module->table_segments;
  3985. for (i = 0; i < table_segment_count; i++, table_segment++) {
  3986. if (p >= p_end) {
  3987. set_error_buf(error_buf, error_buf_size,
  3988. "invalid value type or "
  3989. "invalid elements segment kind");
  3990. return false;
  3991. }
  3992. table_elem_idx_type = VALUE_TYPE_I32;
  3993. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3994. read_leb_uint32(p, p_end, table_segment->mode);
  3995. /* last three bits */
  3996. table_segment->mode = table_segment->mode & 0x07;
  3997. switch (table_segment->mode) {
  3998. /* elemkind/elemtype + active */
  3999. case 0:
  4000. case 4:
  4001. {
  4002. #if WASM_ENABLE_GC != 0
  4003. if (table_segment->mode == 0) {
  4004. /* vec(funcidx), set elem type to (ref func) */
  4005. WASMRefType elem_ref_type = { 0 };
  4006. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4007. wasm_set_refheaptype_common(
  4008. &elem_ref_type.ref_ht_common, false,
  4009. HEAP_TYPE_FUNC);
  4010. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4011. module->ref_type_set, &elem_ref_type,
  4012. error_buf, error_buf_size)))
  4013. return false;
  4014. }
  4015. else {
  4016. /* vec(expr), set elem type to funcref */
  4017. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4018. }
  4019. #else
  4020. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4021. #endif
  4022. table_segment->table_index = 0;
  4023. if (!check_table_index(module, table_segment->table_index,
  4024. error_buf, error_buf_size))
  4025. return false;
  4026. #if WASM_ENABLE_MEMORY64 != 0
  4027. table_elem_idx_type =
  4028. is_table_64bit(module, table_segment->table_index)
  4029. ? VALUE_TYPE_I64
  4030. : VALUE_TYPE_I32;
  4031. #endif
  4032. if (!load_init_expr(module, &p, p_end,
  4033. &table_segment->base_offset,
  4034. table_elem_idx_type, NULL, error_buf,
  4035. error_buf_size))
  4036. return false;
  4037. if (table_segment->mode == 0) {
  4038. /* vec(funcidx) */
  4039. if (!load_func_index_vec(&p, p_end, module,
  4040. table_segment, error_buf,
  4041. error_buf_size))
  4042. return false;
  4043. }
  4044. else {
  4045. /* vec(expr) */
  4046. if (!load_init_expr_vec(&p, p_end, module,
  4047. table_segment, error_buf,
  4048. error_buf_size))
  4049. return false;
  4050. }
  4051. if (!check_table_elem_type(module,
  4052. table_segment->table_index,
  4053. table_segment->elem_type,
  4054. error_buf, error_buf_size))
  4055. return false;
  4056. break;
  4057. }
  4058. /* elemkind + passive/declarative */
  4059. case 1:
  4060. case 3:
  4061. if (!load_elem_type(module, &p, p_end,
  4062. &table_segment->elem_type,
  4063. #if WASM_ENABLE_GC != 0
  4064. &table_segment->elem_ref_type,
  4065. #endif
  4066. true, error_buf, error_buf_size))
  4067. return false;
  4068. /* vec(funcidx) */
  4069. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4070. error_buf, error_buf_size))
  4071. return false;
  4072. break;
  4073. /* elemkind/elemtype + table_idx + active */
  4074. case 2:
  4075. case 6:
  4076. if (!load_table_index(&p, p_end, module,
  4077. &table_segment->table_index,
  4078. error_buf, error_buf_size))
  4079. return false;
  4080. #if WASM_ENABLE_MEMORY64 != 0
  4081. table_elem_idx_type =
  4082. is_table_64bit(module, table_segment->table_index)
  4083. ? VALUE_TYPE_I64
  4084. : VALUE_TYPE_I32;
  4085. #endif
  4086. if (!load_init_expr(module, &p, p_end,
  4087. &table_segment->base_offset,
  4088. table_elem_idx_type, NULL, error_buf,
  4089. error_buf_size))
  4090. return false;
  4091. if (!load_elem_type(module, &p, p_end,
  4092. &table_segment->elem_type,
  4093. #if WASM_ENABLE_GC != 0
  4094. &table_segment->elem_ref_type,
  4095. #endif
  4096. table_segment->mode == 2 ? true : false,
  4097. error_buf, error_buf_size))
  4098. return false;
  4099. if (table_segment->mode == 2) {
  4100. /* vec(funcidx) */
  4101. if (!load_func_index_vec(&p, p_end, module,
  4102. table_segment, error_buf,
  4103. error_buf_size))
  4104. return false;
  4105. }
  4106. else {
  4107. /* vec(expr) */
  4108. if (!load_init_expr_vec(&p, p_end, module,
  4109. table_segment, error_buf,
  4110. error_buf_size))
  4111. return false;
  4112. }
  4113. if (!check_table_elem_type(module,
  4114. table_segment->table_index,
  4115. table_segment->elem_type,
  4116. error_buf, error_buf_size))
  4117. return false;
  4118. break;
  4119. case 5:
  4120. case 7:
  4121. if (!load_elem_type(module, &p, p_end,
  4122. &table_segment->elem_type,
  4123. #if WASM_ENABLE_GC != 0
  4124. &table_segment->elem_ref_type,
  4125. #endif
  4126. false, error_buf, error_buf_size))
  4127. return false;
  4128. /* vec(expr) */
  4129. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4130. error_buf, error_buf_size))
  4131. return false;
  4132. break;
  4133. default:
  4134. set_error_buf(error_buf, error_buf_size,
  4135. "unknown element segment kind");
  4136. return false;
  4137. }
  4138. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4139. /*
  4140. * like: 00 41 05 0b 04 00 01 00 01
  4141. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4142. */
  4143. if (!load_table_index(&p, p_end, module,
  4144. &table_segment->table_index, error_buf,
  4145. error_buf_size))
  4146. return false;
  4147. #if WASM_ENABLE_MEMORY64 != 0
  4148. table_elem_idx_type =
  4149. is_table_64bit(module, table_segment->table_index)
  4150. ? VALUE_TYPE_I64
  4151. : VALUE_TYPE_I32;
  4152. #endif
  4153. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4154. table_elem_idx_type, NULL, error_buf,
  4155. error_buf_size))
  4156. return false;
  4157. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4158. error_buf, error_buf_size))
  4159. return false;
  4160. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4161. if (!check_table_elem_type(module, table_segment->table_index,
  4162. table_segment->elem_type, error_buf,
  4163. error_buf_size))
  4164. return false;
  4165. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4166. #if WASM_ENABLE_MEMORY64 != 0
  4167. if (table_elem_idx_type == VALUE_TYPE_I64
  4168. && table_segment->base_offset.u.u64 > UINT32_MAX) {
  4169. set_error_buf(error_buf, error_buf_size,
  4170. "In table64, table base offset can't be "
  4171. "larger than UINT32_MAX");
  4172. return false;
  4173. }
  4174. #endif
  4175. #if WASM_ENABLE_WAMR_COMPILER != 0
  4176. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4177. module->is_ref_types_used = true;
  4178. #endif
  4179. }
  4180. }
  4181. if (p != p_end) {
  4182. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4183. return false;
  4184. }
  4185. LOG_VERBOSE("Load table segment section success.\n");
  4186. return true;
  4187. fail:
  4188. return false;
  4189. }
  4190. static bool
  4191. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4192. WASMModule *module,
  4193. #if WASM_ENABLE_BULK_MEMORY != 0
  4194. bool has_datacount_section,
  4195. #endif
  4196. bool clone_data_seg, char *error_buf,
  4197. uint32 error_buf_size)
  4198. {
  4199. const uint8 *p = buf, *p_end = buf_end;
  4200. uint32 data_seg_count, i, mem_index, data_seg_len;
  4201. uint64 total_size;
  4202. WASMDataSeg *dataseg;
  4203. InitializerExpression init_expr;
  4204. #if WASM_ENABLE_BULK_MEMORY != 0
  4205. bool is_passive = false;
  4206. uint32 mem_flag;
  4207. #endif
  4208. uint8 mem_offset_type = VALUE_TYPE_I32;
  4209. read_leb_uint32(p, p_end, data_seg_count);
  4210. #if WASM_ENABLE_BULK_MEMORY != 0
  4211. if (has_datacount_section && data_seg_count != module->data_seg_count1) {
  4212. set_error_buf(error_buf, error_buf_size,
  4213. "data count and data section have inconsistent lengths");
  4214. return false;
  4215. }
  4216. #endif
  4217. if (data_seg_count) {
  4218. module->data_seg_count = data_seg_count;
  4219. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4220. if (!(module->data_segments =
  4221. loader_malloc(total_size, error_buf, error_buf_size))) {
  4222. return false;
  4223. }
  4224. for (i = 0; i < data_seg_count; i++) {
  4225. read_leb_uint32(p, p_end, mem_index);
  4226. #if WASM_ENABLE_BULK_MEMORY != 0
  4227. is_passive = false;
  4228. mem_flag = mem_index & 0x03;
  4229. switch (mem_flag) {
  4230. case 0x01:
  4231. is_passive = true;
  4232. #if WASM_ENABLE_WAMR_COMPILER != 0
  4233. module->is_bulk_memory_used = true;
  4234. #endif
  4235. break;
  4236. case 0x00:
  4237. /* no memory index, treat index as 0 */
  4238. mem_index = 0;
  4239. goto check_mem_index;
  4240. case 0x02:
  4241. /* read following memory index */
  4242. read_leb_uint32(p, p_end, mem_index);
  4243. #if WASM_ENABLE_WAMR_COMPILER != 0
  4244. module->is_bulk_memory_used = true;
  4245. #endif
  4246. check_mem_index:
  4247. if (mem_index
  4248. >= module->import_memory_count + module->memory_count) {
  4249. set_error_buf_v(error_buf, error_buf_size,
  4250. "unknown memory %d", mem_index);
  4251. return false;
  4252. }
  4253. break;
  4254. case 0x03:
  4255. default:
  4256. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4257. return false;
  4258. break;
  4259. }
  4260. #else
  4261. if (mem_index
  4262. >= module->import_memory_count + module->memory_count) {
  4263. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4264. mem_index);
  4265. return false;
  4266. }
  4267. #endif /* WASM_ENABLE_BULK_MEMORY */
  4268. #if WASM_ENABLE_BULK_MEMORY != 0
  4269. if (!is_passive)
  4270. #endif
  4271. {
  4272. #if WASM_ENABLE_MEMORY64 != 0
  4273. /* This memory_flag is from memory instead of data segment */
  4274. uint8 memory_flag;
  4275. if (module->import_memory_count > 0) {
  4276. memory_flag = module->import_memories[mem_index]
  4277. .u.memory.mem_type.flags;
  4278. }
  4279. else {
  4280. memory_flag =
  4281. module
  4282. ->memories[mem_index - module->import_memory_count]
  4283. .flags;
  4284. }
  4285. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4286. : VALUE_TYPE_I32;
  4287. #else
  4288. mem_offset_type = VALUE_TYPE_I32;
  4289. #endif
  4290. }
  4291. #if WASM_ENABLE_BULK_MEMORY != 0
  4292. if (!is_passive)
  4293. #endif
  4294. if (!load_init_expr(module, &p, p_end, &init_expr,
  4295. mem_offset_type, NULL, error_buf,
  4296. error_buf_size))
  4297. return false;
  4298. read_leb_uint32(p, p_end, data_seg_len);
  4299. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4300. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4301. return false;
  4302. }
  4303. #if WASM_ENABLE_BULK_MEMORY != 0
  4304. dataseg->is_passive = is_passive;
  4305. if (!is_passive)
  4306. #endif
  4307. {
  4308. bh_memcpy_s(&dataseg->base_offset,
  4309. sizeof(InitializerExpression), &init_expr,
  4310. sizeof(InitializerExpression));
  4311. dataseg->memory_index = mem_index;
  4312. }
  4313. dataseg->data_length = data_seg_len;
  4314. CHECK_BUF(p, p_end, data_seg_len);
  4315. if (clone_data_seg) {
  4316. if (!(dataseg->data = loader_malloc(
  4317. dataseg->data_length, error_buf, error_buf_size))) {
  4318. return false;
  4319. }
  4320. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4321. data_seg_len);
  4322. }
  4323. else {
  4324. dataseg->data = (uint8 *)p;
  4325. }
  4326. dataseg->is_data_cloned = clone_data_seg;
  4327. p += data_seg_len;
  4328. }
  4329. }
  4330. if (p != p_end) {
  4331. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4332. return false;
  4333. }
  4334. LOG_VERBOSE("Load data segment section success.\n");
  4335. return true;
  4336. fail:
  4337. return false;
  4338. }
  4339. #if WASM_ENABLE_BULK_MEMORY != 0
  4340. static bool
  4341. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4342. WASMModule *module, char *error_buf,
  4343. uint32 error_buf_size)
  4344. {
  4345. const uint8 *p = buf, *p_end = buf_end;
  4346. uint32 data_seg_count1 = 0;
  4347. read_leb_uint32(p, p_end, data_seg_count1);
  4348. module->data_seg_count1 = data_seg_count1;
  4349. if (p != p_end) {
  4350. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4351. return false;
  4352. }
  4353. #if WASM_ENABLE_WAMR_COMPILER != 0
  4354. module->is_bulk_memory_used = true;
  4355. #endif
  4356. LOG_VERBOSE("Load datacount section success.\n");
  4357. return true;
  4358. fail:
  4359. return false;
  4360. }
  4361. #endif
  4362. #if WASM_ENABLE_TAGS != 0
  4363. static bool
  4364. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4365. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4366. uint32 error_buf_size)
  4367. {
  4368. (void)buf_code;
  4369. (void)buf_code_end;
  4370. const uint8 *p = buf, *p_end = buf_end;
  4371. size_t total_size = 0;
  4372. /* number of tags defined in the section */
  4373. uint32 section_tag_count = 0;
  4374. uint8 tag_attribute;
  4375. uint32 tag_type;
  4376. WASMTag *tag = NULL;
  4377. /* get tag count */
  4378. read_leb_uint32(p, p_end, section_tag_count);
  4379. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4380. error_buf, error_buf_size))
  4381. return false;
  4382. module->tag_count = section_tag_count;
  4383. if (section_tag_count) {
  4384. total_size = sizeof(WASMTag *) * module->tag_count;
  4385. if (!(module->tags =
  4386. loader_malloc(total_size, error_buf, error_buf_size))) {
  4387. return false;
  4388. }
  4389. /* load each tag, imported tags precede the tags */
  4390. uint32 tag_index;
  4391. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4392. /* get the one byte attribute */
  4393. CHECK_BUF(p, p_end, 1);
  4394. tag_attribute = read_uint8(p);
  4395. /* get type */
  4396. read_leb_uint32(p, p_end, tag_type);
  4397. /* compare against module->types */
  4398. if (tag_type >= module->type_count) {
  4399. set_error_buf(error_buf, error_buf_size, "unknown type");
  4400. return false;
  4401. }
  4402. /* get return type (must be 0) */
  4403. /* check, that the type of the referred tag returns void */
  4404. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4405. if (func_type->result_count != 0) {
  4406. set_error_buf(error_buf, error_buf_size,
  4407. "non-empty tag result type");
  4408. goto fail;
  4409. }
  4410. if (!(tag = module->tags[tag_index] = loader_malloc(
  4411. sizeof(WASMTag), error_buf, error_buf_size))) {
  4412. return false;
  4413. }
  4414. /* store to module tag declarations */
  4415. tag->attribute = tag_attribute;
  4416. tag->type = tag_type;
  4417. tag->tag_type = func_type;
  4418. }
  4419. }
  4420. if (p != p_end) {
  4421. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4422. return false;
  4423. }
  4424. LOG_VERBOSE("Load tag section success.\n");
  4425. return true;
  4426. fail:
  4427. return false;
  4428. }
  4429. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4430. static bool
  4431. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4432. const uint8 *buf_func_end, WASMModule *module,
  4433. char *error_buf, uint32 error_buf_size)
  4434. {
  4435. const uint8 *p = buf, *p_end = buf_end;
  4436. const uint8 *p_func = buf_func;
  4437. uint32 func_count = 0, code_count;
  4438. /* code has been loaded in function section, so pass it here, just check
  4439. * whether function and code section have inconsistent lengths */
  4440. read_leb_uint32(p, p_end, code_count);
  4441. if (buf_func)
  4442. read_leb_uint32(p_func, buf_func_end, func_count);
  4443. if (func_count != code_count) {
  4444. set_error_buf(error_buf, error_buf_size,
  4445. "function and code section have inconsistent lengths");
  4446. return false;
  4447. }
  4448. LOG_VERBOSE("Load code segment section success.\n");
  4449. (void)module;
  4450. return true;
  4451. fail:
  4452. return false;
  4453. }
  4454. static bool
  4455. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4456. char *error_buf, uint32 error_buf_size)
  4457. {
  4458. const uint8 *p = buf, *p_end = buf_end;
  4459. WASMFuncType *type;
  4460. uint32 start_function;
  4461. read_leb_uint32(p, p_end, start_function);
  4462. if (start_function
  4463. >= module->function_count + module->import_function_count) {
  4464. set_error_buf(error_buf, error_buf_size, "unknown function");
  4465. return false;
  4466. }
  4467. if (start_function < module->import_function_count)
  4468. type = module->import_functions[start_function].u.function.func_type;
  4469. else
  4470. type = module->functions[start_function - module->import_function_count]
  4471. ->func_type;
  4472. if (type->param_count != 0 || type->result_count != 0) {
  4473. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4474. return false;
  4475. }
  4476. module->start_function = start_function;
  4477. if (p != p_end) {
  4478. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4479. return false;
  4480. }
  4481. LOG_VERBOSE("Load start section success.\n");
  4482. return true;
  4483. fail:
  4484. return false;
  4485. }
  4486. #if WASM_ENABLE_STRINGREF != 0
  4487. static bool
  4488. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4489. WASMModule *module, bool is_load_from_file_buf,
  4490. char *error_buf, uint32 error_buf_size)
  4491. {
  4492. const uint8 *p = buf, *p_end = buf_end;
  4493. int32 deferred_count, immediate_count, string_length, i;
  4494. uint64 total_size;
  4495. read_leb_uint32(p, p_end, deferred_count);
  4496. read_leb_uint32(p, p_end, immediate_count);
  4497. /* proposal set deferred_count for future extension */
  4498. if (deferred_count != 0) {
  4499. goto fail;
  4500. }
  4501. if (immediate_count > 0) {
  4502. total_size = sizeof(char *) * (uint64)immediate_count;
  4503. if (!(module->string_literal_ptrs =
  4504. loader_malloc(total_size, error_buf, error_buf_size))) {
  4505. goto fail;
  4506. }
  4507. module->string_literal_count = immediate_count;
  4508. total_size = sizeof(uint32) * (uint64)immediate_count;
  4509. if (!(module->string_literal_lengths =
  4510. loader_malloc(total_size, error_buf, error_buf_size))) {
  4511. goto fail;
  4512. }
  4513. for (i = 0; i < immediate_count; i++) {
  4514. read_leb_uint32(p, p_end, string_length);
  4515. CHECK_BUF(p, p_end, string_length);
  4516. module->string_literal_ptrs[i] = p;
  4517. module->string_literal_lengths[i] = string_length;
  4518. p += string_length;
  4519. }
  4520. }
  4521. if (p != p_end) {
  4522. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4523. goto fail;
  4524. }
  4525. LOG_VERBOSE("Load stringref section success.\n");
  4526. return true;
  4527. fail:
  4528. return false;
  4529. }
  4530. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4531. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4532. static bool
  4533. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4534. bool is_load_from_file_buf, char *error_buf,
  4535. uint32 error_buf_size)
  4536. {
  4537. const uint8 *p = buf, *p_end = buf_end;
  4538. uint32 name_type, subsection_size;
  4539. uint32 previous_name_type = 0;
  4540. uint32 num_func_name;
  4541. uint32 func_index;
  4542. uint32 previous_func_index = ~0U;
  4543. uint32 func_name_len;
  4544. uint32 name_index;
  4545. int i = 0;
  4546. if (p >= p_end) {
  4547. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4548. return false;
  4549. }
  4550. while (p < p_end) {
  4551. read_leb_uint32(p, p_end, name_type);
  4552. if (i != 0) {
  4553. if (name_type == previous_name_type) {
  4554. set_error_buf(error_buf, error_buf_size,
  4555. "duplicate sub-section");
  4556. return false;
  4557. }
  4558. if (name_type < previous_name_type) {
  4559. set_error_buf(error_buf, error_buf_size,
  4560. "out-of-order sub-section");
  4561. return false;
  4562. }
  4563. }
  4564. previous_name_type = name_type;
  4565. read_leb_uint32(p, p_end, subsection_size);
  4566. CHECK_BUF(p, p_end, subsection_size);
  4567. switch (name_type) {
  4568. case SUB_SECTION_TYPE_FUNC:
  4569. if (subsection_size) {
  4570. read_leb_uint32(p, p_end, num_func_name);
  4571. for (name_index = 0; name_index < num_func_name;
  4572. name_index++) {
  4573. read_leb_uint32(p, p_end, func_index);
  4574. if (func_index == previous_func_index) {
  4575. set_error_buf(error_buf, error_buf_size,
  4576. "duplicate function name");
  4577. return false;
  4578. }
  4579. if (func_index < previous_func_index
  4580. && previous_func_index != ~0U) {
  4581. set_error_buf(error_buf, error_buf_size,
  4582. "out-of-order function index ");
  4583. return false;
  4584. }
  4585. previous_func_index = func_index;
  4586. read_leb_uint32(p, p_end, func_name_len);
  4587. CHECK_BUF(p, p_end, func_name_len);
  4588. /* Skip the import functions */
  4589. if (func_index >= module->import_function_count) {
  4590. func_index -= module->import_function_count;
  4591. if (func_index >= module->function_count) {
  4592. set_error_buf(error_buf, error_buf_size,
  4593. "out-of-range function index");
  4594. return false;
  4595. }
  4596. if (!(module->functions[func_index]->field_name =
  4597. wasm_const_str_list_insert(
  4598. p, func_name_len, module,
  4599. #if WASM_ENABLE_WAMR_COMPILER != 0
  4600. false,
  4601. #else
  4602. is_load_from_file_buf,
  4603. #endif
  4604. error_buf, error_buf_size))) {
  4605. return false;
  4606. }
  4607. }
  4608. p += func_name_len;
  4609. }
  4610. }
  4611. break;
  4612. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4613. */
  4614. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4615. default:
  4616. p = p + subsection_size;
  4617. break;
  4618. }
  4619. i++;
  4620. }
  4621. return true;
  4622. fail:
  4623. return false;
  4624. }
  4625. #endif
  4626. static bool
  4627. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4628. bool is_load_from_file_buf, char *error_buf,
  4629. uint32 error_buf_size)
  4630. {
  4631. const uint8 *p = buf, *p_end = buf_end;
  4632. char section_name[32];
  4633. uint32 name_len, buffer_len;
  4634. if (p >= p_end) {
  4635. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4636. return false;
  4637. }
  4638. read_leb_uint32(p, p_end, name_len);
  4639. if (p + name_len > p_end) {
  4640. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4641. return false;
  4642. }
  4643. if (!wasm_check_utf8_str(p, name_len)) {
  4644. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4645. return false;
  4646. }
  4647. buffer_len = sizeof(section_name);
  4648. memset(section_name, 0, buffer_len);
  4649. if (name_len < buffer_len) {
  4650. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4651. }
  4652. else {
  4653. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4654. memset(section_name + buffer_len - 4, '.', 3);
  4655. }
  4656. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4657. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4658. module->name_section_buf = buf;
  4659. module->name_section_buf_end = buf_end;
  4660. p += name_len;
  4661. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4662. error_buf, error_buf_size)) {
  4663. return false;
  4664. }
  4665. LOG_VERBOSE("Load custom name section success.");
  4666. }
  4667. #endif
  4668. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4669. {
  4670. WASMCustomSection *section =
  4671. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4672. if (!section) {
  4673. return false;
  4674. }
  4675. section->name_addr = (char *)p;
  4676. section->name_len = name_len;
  4677. section->content_addr = (uint8 *)(p + name_len);
  4678. section->content_len = (uint32)(p_end - p - name_len);
  4679. section->next = module->custom_section_list;
  4680. module->custom_section_list = section;
  4681. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4682. return true;
  4683. }
  4684. #endif
  4685. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4686. (void)is_load_from_file_buf;
  4687. (void)module;
  4688. return true;
  4689. fail:
  4690. return false;
  4691. }
  4692. static void
  4693. calculate_global_data_offset(WASMModule *module)
  4694. {
  4695. uint32 i, data_offset;
  4696. data_offset = 0;
  4697. for (i = 0; i < module->import_global_count; i++) {
  4698. WASMGlobalImport *import_global =
  4699. &((module->import_globals + i)->u.global);
  4700. #if WASM_ENABLE_FAST_JIT != 0
  4701. import_global->data_offset = data_offset;
  4702. #endif
  4703. data_offset += wasm_value_type_size(import_global->type.val_type);
  4704. }
  4705. for (i = 0; i < module->global_count; i++) {
  4706. WASMGlobal *global = module->globals + i;
  4707. #if WASM_ENABLE_FAST_JIT != 0
  4708. global->data_offset = data_offset;
  4709. #endif
  4710. data_offset += wasm_value_type_size(global->type.val_type);
  4711. }
  4712. module->global_data_size = data_offset;
  4713. }
  4714. #if WASM_ENABLE_FAST_JIT != 0
  4715. static bool
  4716. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4717. uint32 error_buf_size)
  4718. {
  4719. #if WASM_ENABLE_LAZY_JIT != 0
  4720. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4721. #endif
  4722. uint32 i;
  4723. if (!module->function_count)
  4724. return true;
  4725. if (!(module->fast_jit_func_ptrs =
  4726. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4727. error_buf_size))) {
  4728. return false;
  4729. }
  4730. #if WASM_ENABLE_LAZY_JIT != 0
  4731. for (i = 0; i < module->function_count; i++) {
  4732. module->fast_jit_func_ptrs[i] =
  4733. jit_globals->compile_fast_jit_and_then_call;
  4734. }
  4735. #endif
  4736. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4737. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4738. set_error_buf(error_buf, error_buf_size,
  4739. "init fast jit thread lock failed");
  4740. return false;
  4741. }
  4742. module->fast_jit_thread_locks_inited[i] = true;
  4743. }
  4744. return true;
  4745. }
  4746. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4747. #if WASM_ENABLE_JIT != 0
  4748. static bool
  4749. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4750. uint32 error_buf_size)
  4751. {
  4752. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4753. AOTCompOption option = { 0 };
  4754. char *aot_last_error;
  4755. uint64 size;
  4756. #if WASM_ENABLE_GC != 0
  4757. bool gc_enabled = true;
  4758. #else
  4759. bool gc_enabled = false;
  4760. #endif
  4761. if (module->function_count == 0)
  4762. return true;
  4763. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4764. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4765. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4766. return false;
  4767. }
  4768. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4769. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4770. os_mutex_destroy(&module->tierup_wait_lock);
  4771. return false;
  4772. }
  4773. module->tierup_wait_lock_inited = true;
  4774. #endif
  4775. size = sizeof(void *) * (uint64)module->function_count
  4776. + sizeof(bool) * (uint64)module->function_count;
  4777. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4778. return false;
  4779. }
  4780. module->func_ptrs_compiled =
  4781. (bool *)((uint8 *)module->func_ptrs
  4782. + sizeof(void *) * module->function_count);
  4783. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4784. if (!module->comp_data) {
  4785. aot_last_error = aot_get_last_error();
  4786. bh_assert(aot_last_error != NULL);
  4787. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4788. return false;
  4789. }
  4790. option.is_jit_mode = true;
  4791. option.opt_level = llvm_jit_options->opt_level;
  4792. option.size_level = llvm_jit_options->size_level;
  4793. option.segue_flags = llvm_jit_options->segue_flags;
  4794. option.quick_invoke_c_api_import =
  4795. llvm_jit_options->quick_invoke_c_api_import;
  4796. #if WASM_ENABLE_BULK_MEMORY != 0
  4797. option.enable_bulk_memory = true;
  4798. #endif
  4799. #if WASM_ENABLE_THREAD_MGR != 0
  4800. option.enable_thread_mgr = true;
  4801. #endif
  4802. #if WASM_ENABLE_TAIL_CALL != 0
  4803. option.enable_tail_call = true;
  4804. #endif
  4805. #if WASM_ENABLE_SIMD != 0
  4806. option.enable_simd = true;
  4807. #endif
  4808. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4809. option.enable_ref_types = true;
  4810. #elif WASM_ENABLE_GC != 0
  4811. option.enable_gc = true;
  4812. #endif
  4813. option.enable_aux_stack_check = true;
  4814. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4815. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4816. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  4817. aot_call_stack_features_init_default(&option.call_stack_features);
  4818. #endif
  4819. #if WASM_ENABLE_PERF_PROFILING != 0
  4820. option.enable_perf_profiling = true;
  4821. #endif
  4822. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4823. option.enable_memory_profiling = true;
  4824. option.enable_stack_estimation = true;
  4825. #endif
  4826. #if WASM_ENABLE_SHARED_HEAP != 0
  4827. option.enable_shared_heap = true;
  4828. #endif
  4829. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4830. if (!module->comp_ctx) {
  4831. aot_last_error = aot_get_last_error();
  4832. bh_assert(aot_last_error != NULL);
  4833. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4834. return false;
  4835. }
  4836. return true;
  4837. }
  4838. static bool
  4839. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4840. uint32 error_buf_size)
  4841. {
  4842. char *aot_last_error;
  4843. uint32 i;
  4844. if (module->function_count == 0)
  4845. return true;
  4846. if (!aot_compile_wasm(module->comp_ctx)) {
  4847. aot_last_error = aot_get_last_error();
  4848. bh_assert(aot_last_error != NULL);
  4849. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4850. return false;
  4851. }
  4852. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4853. if (module->orcjit_stop_compiling)
  4854. return false;
  4855. #endif
  4856. bh_print_time("Begin to lookup llvm jit functions");
  4857. for (i = 0; i < module->function_count; i++) {
  4858. LLVMOrcJITTargetAddress func_addr = 0;
  4859. LLVMErrorRef error;
  4860. char func_name[48];
  4861. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4862. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4863. func_name);
  4864. if (error != LLVMErrorSuccess) {
  4865. char *err_msg = LLVMGetErrorMessage(error);
  4866. set_error_buf_v(error_buf, error_buf_size,
  4867. "failed to compile llvm jit function: %s", err_msg);
  4868. LLVMDisposeErrorMessage(err_msg);
  4869. return false;
  4870. }
  4871. /**
  4872. * No need to lock the func_ptr[func_idx] here as it is basic
  4873. * data type, the load/store for it can be finished by one cpu
  4874. * instruction, and there can be only one cpu instruction
  4875. * loading/storing at the same time.
  4876. */
  4877. module->func_ptrs[i] = (void *)func_addr;
  4878. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4879. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4880. if (module->orcjit_stop_compiling)
  4881. return false;
  4882. #endif
  4883. }
  4884. bh_print_time("End lookup llvm jit functions");
  4885. return true;
  4886. }
  4887. #endif /* end of WASM_ENABLE_JIT != 0 */
  4888. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4889. && WASM_ENABLE_LAZY_JIT != 0
  4890. static void *
  4891. init_llvm_jit_functions_stage2_callback(void *arg)
  4892. {
  4893. WASMModule *module = (WASMModule *)arg;
  4894. char error_buf[128];
  4895. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4896. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4897. module->orcjit_stop_compiling = true;
  4898. return NULL;
  4899. }
  4900. os_mutex_lock(&module->tierup_wait_lock);
  4901. module->llvm_jit_inited = true;
  4902. os_cond_broadcast(&module->tierup_wait_cond);
  4903. os_mutex_unlock(&module->tierup_wait_lock);
  4904. return NULL;
  4905. }
  4906. #endif
  4907. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4908. /* The callback function to compile jit functions */
  4909. static void *
  4910. orcjit_thread_callback(void *arg)
  4911. {
  4912. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4913. #if WASM_ENABLE_JIT != 0
  4914. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4915. #endif
  4916. WASMModule *module = thread_arg->module;
  4917. uint32 group_idx = thread_arg->group_idx;
  4918. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4919. uint32 func_count = module->function_count;
  4920. uint32 i;
  4921. #if WASM_ENABLE_FAST_JIT != 0
  4922. /* Compile fast jit functions of this group */
  4923. for (i = group_idx; i < func_count; i += group_stride) {
  4924. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4925. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4926. break;
  4927. }
  4928. if (module->orcjit_stop_compiling) {
  4929. return NULL;
  4930. }
  4931. }
  4932. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4933. os_mutex_lock(&module->tierup_wait_lock);
  4934. module->fast_jit_ready_groups++;
  4935. os_mutex_unlock(&module->tierup_wait_lock);
  4936. #endif
  4937. #endif
  4938. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4939. && WASM_ENABLE_LAZY_JIT != 0
  4940. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4941. for (i = group_idx; i < func_count;
  4942. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4943. uint32 j;
  4944. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4945. if (i + j * group_stride < func_count) {
  4946. if (!jit_compiler_set_call_to_fast_jit(
  4947. module,
  4948. i + j * group_stride + module->import_function_count)) {
  4949. LOG_ERROR(
  4950. "failed to compile call_to_fast_jit for func %u\n",
  4951. i + j * group_stride + module->import_function_count);
  4952. module->orcjit_stop_compiling = true;
  4953. return NULL;
  4954. }
  4955. }
  4956. if (module->orcjit_stop_compiling) {
  4957. return NULL;
  4958. }
  4959. }
  4960. }
  4961. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4962. fast jit functions are compiled */
  4963. os_mutex_lock(&module->tierup_wait_lock);
  4964. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4965. && module->fast_jit_ready_groups >= group_stride)) {
  4966. os_cond_reltimedwait(&module->tierup_wait_cond,
  4967. &module->tierup_wait_lock, 10000);
  4968. if (module->orcjit_stop_compiling) {
  4969. /* init_llvm_jit_functions_stage2 failed */
  4970. os_mutex_unlock(&module->tierup_wait_lock);
  4971. return NULL;
  4972. }
  4973. }
  4974. os_mutex_unlock(&module->tierup_wait_lock);
  4975. #endif
  4976. #if WASM_ENABLE_JIT != 0
  4977. /* Compile llvm jit functions of this group */
  4978. for (i = group_idx; i < func_count;
  4979. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4980. LLVMOrcJITTargetAddress func_addr = 0;
  4981. LLVMErrorRef error;
  4982. char func_name[48];
  4983. typedef void (*F)(void);
  4984. union {
  4985. F f;
  4986. void *v;
  4987. } u;
  4988. uint32 j;
  4989. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  4990. "_wrapper");
  4991. LOG_DEBUG("compile llvm jit func %s", func_name);
  4992. error =
  4993. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  4994. if (error != LLVMErrorSuccess) {
  4995. char *err_msg = LLVMGetErrorMessage(error);
  4996. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  4997. LLVMDisposeErrorMessage(err_msg);
  4998. break;
  4999. }
  5000. /* Call the jit wrapper function to trigger its compilation, so as
  5001. to compile the actual jit functions, since we add the latter to
  5002. function list in the PartitionFunction callback */
  5003. u.v = (void *)func_addr;
  5004. u.f();
  5005. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5006. if (i + j * group_stride < func_count) {
  5007. module->func_ptrs_compiled[i + j * group_stride] = true;
  5008. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5009. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5010. i + j * group_stride);
  5011. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5012. func_name);
  5013. if (error != LLVMErrorSuccess) {
  5014. char *err_msg = LLVMGetErrorMessage(error);
  5015. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5016. err_msg);
  5017. LLVMDisposeErrorMessage(err_msg);
  5018. /* Ignore current llvm jit func, as its func ptr is
  5019. previous set to call_to_fast_jit, which also works */
  5020. continue;
  5021. }
  5022. jit_compiler_set_llvm_jit_func_ptr(
  5023. module,
  5024. i + j * group_stride + module->import_function_count,
  5025. (void *)func_addr);
  5026. /* Try to switch to call this llvm jit function instead of
  5027. fast jit function from fast jit jitted code */
  5028. jit_compiler_set_call_to_llvm_jit(
  5029. module,
  5030. i + j * group_stride + module->import_function_count);
  5031. #endif
  5032. }
  5033. }
  5034. if (module->orcjit_stop_compiling) {
  5035. break;
  5036. }
  5037. }
  5038. #endif
  5039. return NULL;
  5040. }
  5041. static void
  5042. orcjit_stop_compile_threads(WASMModule *module)
  5043. {
  5044. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5045. / sizeof(OrcJitThreadArg));
  5046. module->orcjit_stop_compiling = true;
  5047. for (i = 0; i < thread_num; i++) {
  5048. if (module->orcjit_threads[i])
  5049. os_thread_join(module->orcjit_threads[i], NULL);
  5050. }
  5051. }
  5052. static bool
  5053. compile_jit_functions(WASMModule *module, char *error_buf,
  5054. uint32 error_buf_size)
  5055. {
  5056. uint32 thread_num =
  5057. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5058. uint32 i, j;
  5059. bh_print_time("Begin to compile jit functions");
  5060. /* Create threads to compile the jit functions */
  5061. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5062. #if WASM_ENABLE_JIT != 0
  5063. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5064. #endif
  5065. module->orcjit_thread_args[i].module = module;
  5066. module->orcjit_thread_args[i].group_idx = i;
  5067. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5068. (void *)&module->orcjit_thread_args[i],
  5069. APP_THREAD_STACK_SIZE_DEFAULT)
  5070. != 0) {
  5071. set_error_buf(error_buf, error_buf_size,
  5072. "create orcjit compile thread failed");
  5073. /* Terminate the threads created */
  5074. module->orcjit_stop_compiling = true;
  5075. for (j = 0; j < i; j++) {
  5076. os_thread_join(module->orcjit_threads[j], NULL);
  5077. }
  5078. return false;
  5079. }
  5080. }
  5081. #if WASM_ENABLE_LAZY_JIT == 0
  5082. /* Wait until all jit functions are compiled for eager mode */
  5083. for (i = 0; i < thread_num; i++) {
  5084. if (module->orcjit_threads[i])
  5085. os_thread_join(module->orcjit_threads[i], NULL);
  5086. }
  5087. #if WASM_ENABLE_FAST_JIT != 0
  5088. /* Ensure all the fast-jit functions are compiled */
  5089. for (i = 0; i < module->function_count; i++) {
  5090. if (!jit_compiler_is_compiled(module,
  5091. i + module->import_function_count)) {
  5092. set_error_buf(error_buf, error_buf_size,
  5093. "failed to compile fast jit function");
  5094. return false;
  5095. }
  5096. }
  5097. #endif
  5098. #if WASM_ENABLE_JIT != 0
  5099. /* Ensure all the llvm-jit functions are compiled */
  5100. for (i = 0; i < module->function_count; i++) {
  5101. if (!module->func_ptrs_compiled[i]) {
  5102. set_error_buf(error_buf, error_buf_size,
  5103. "failed to compile llvm jit function");
  5104. return false;
  5105. }
  5106. }
  5107. #endif
  5108. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5109. bh_print_time("End compile jit functions");
  5110. return true;
  5111. }
  5112. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5113. static bool
  5114. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5115. uint32 cur_func_idx, char *error_buf,
  5116. uint32 error_buf_size);
  5117. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5118. void **
  5119. wasm_interp_get_handle_table(void);
  5120. static void **handle_table;
  5121. #endif
  5122. static bool
  5123. load_from_sections(WASMModule *module, WASMSection *sections,
  5124. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5125. bool no_resolve, char *error_buf, uint32 error_buf_size)
  5126. {
  5127. WASMExport *export;
  5128. WASMSection *section = sections;
  5129. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5130. *buf_func = NULL, *buf_func_end = NULL;
  5131. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5132. WASMGlobal *aux_stack_top_global = NULL, *global;
  5133. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5134. aux_stack_top = (uint64)-1LL;
  5135. uint32 global_index, func_index, i;
  5136. uint32 aux_data_end_global_index = (uint32)-1;
  5137. uint32 aux_heap_base_global_index = (uint32)-1;
  5138. WASMFuncType *func_type;
  5139. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5140. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5141. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5142. #if WASM_ENABLE_BULK_MEMORY != 0
  5143. bool has_datacount_section = false;
  5144. #endif
  5145. /* Find code and function sections if have */
  5146. while (section) {
  5147. if (section->section_type == SECTION_TYPE_CODE) {
  5148. buf_code = section->section_body;
  5149. buf_code_end = buf_code + section->section_body_size;
  5150. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5151. module->buf_code = (uint8 *)buf_code;
  5152. module->buf_code_size = section->section_body_size;
  5153. #endif
  5154. }
  5155. else if (section->section_type == SECTION_TYPE_FUNC) {
  5156. buf_func = section->section_body;
  5157. buf_func_end = buf_func + section->section_body_size;
  5158. }
  5159. section = section->next;
  5160. }
  5161. section = sections;
  5162. while (section) {
  5163. buf = section->section_body;
  5164. buf_end = buf + section->section_body_size;
  5165. switch (section->section_type) {
  5166. case SECTION_TYPE_USER:
  5167. /* unsupported user section, ignore it. */
  5168. if (!load_user_section(buf, buf_end, module,
  5169. reuse_const_strings, error_buf,
  5170. error_buf_size))
  5171. return false;
  5172. break;
  5173. case SECTION_TYPE_TYPE:
  5174. if (!load_type_section(buf, buf_end, module, error_buf,
  5175. error_buf_size))
  5176. return false;
  5177. break;
  5178. case SECTION_TYPE_IMPORT:
  5179. if (!load_import_section(buf, buf_end, module,
  5180. reuse_const_strings, no_resolve,
  5181. error_buf, error_buf_size))
  5182. return false;
  5183. break;
  5184. case SECTION_TYPE_FUNC:
  5185. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5186. module, error_buf, error_buf_size))
  5187. return false;
  5188. break;
  5189. case SECTION_TYPE_TABLE:
  5190. if (!load_table_section(buf, buf_end, module, error_buf,
  5191. error_buf_size))
  5192. return false;
  5193. break;
  5194. case SECTION_TYPE_MEMORY:
  5195. if (!load_memory_section(buf, buf_end, module, error_buf,
  5196. error_buf_size))
  5197. return false;
  5198. break;
  5199. #if WASM_ENABLE_TAGS != 0
  5200. case SECTION_TYPE_TAG:
  5201. /* load tag declaration section */
  5202. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5203. module, error_buf, error_buf_size))
  5204. return false;
  5205. break;
  5206. #endif
  5207. case SECTION_TYPE_GLOBAL:
  5208. if (!load_global_section(buf, buf_end, module, error_buf,
  5209. error_buf_size))
  5210. return false;
  5211. break;
  5212. case SECTION_TYPE_EXPORT:
  5213. if (!load_export_section(buf, buf_end, module,
  5214. reuse_const_strings, error_buf,
  5215. error_buf_size))
  5216. return false;
  5217. break;
  5218. case SECTION_TYPE_START:
  5219. if (!load_start_section(buf, buf_end, module, error_buf,
  5220. error_buf_size))
  5221. return false;
  5222. break;
  5223. case SECTION_TYPE_ELEM:
  5224. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5225. error_buf_size))
  5226. return false;
  5227. break;
  5228. case SECTION_TYPE_CODE:
  5229. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5230. module, error_buf, error_buf_size))
  5231. return false;
  5232. break;
  5233. case SECTION_TYPE_DATA:
  5234. if (!load_data_segment_section(buf, buf_end, module,
  5235. #if WASM_ENABLE_BULK_MEMORY != 0
  5236. has_datacount_section,
  5237. #endif
  5238. clone_data_seg, error_buf,
  5239. error_buf_size))
  5240. return false;
  5241. break;
  5242. #if WASM_ENABLE_BULK_MEMORY != 0
  5243. case SECTION_TYPE_DATACOUNT:
  5244. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5245. error_buf_size))
  5246. return false;
  5247. has_datacount_section = true;
  5248. break;
  5249. #endif
  5250. #if WASM_ENABLE_STRINGREF != 0
  5251. case SECTION_TYPE_STRINGREF:
  5252. if (!load_stringref_section(buf, buf_end, module,
  5253. reuse_const_strings, error_buf,
  5254. error_buf_size))
  5255. return false;
  5256. break;
  5257. #endif
  5258. default:
  5259. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5260. return false;
  5261. }
  5262. section = section->next;
  5263. }
  5264. module->aux_data_end_global_index = (uint32)-1;
  5265. module->aux_heap_base_global_index = (uint32)-1;
  5266. module->aux_stack_top_global_index = (uint32)-1;
  5267. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5268. export = module->exports;
  5269. for (i = 0; i < module->export_count; i++, export ++) {
  5270. if (export->kind == EXPORT_KIND_GLOBAL) {
  5271. if (!strcmp(export->name, "__heap_base")) {
  5272. global_index = export->index - module->import_global_count;
  5273. global = module->globals + global_index;
  5274. if (global->type.val_type == VALUE_TYPE_I32
  5275. && !global->type.is_mutable
  5276. && global->init_expr.init_expr_type
  5277. == INIT_EXPR_TYPE_I32_CONST) {
  5278. aux_heap_base_global = global;
  5279. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5280. aux_heap_base_global_index = export->index;
  5281. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5282. aux_heap_base);
  5283. }
  5284. }
  5285. else if (!strcmp(export->name, "__data_end")) {
  5286. global_index = export->index - module->import_global_count;
  5287. global = module->globals + global_index;
  5288. if (global->type.val_type == VALUE_TYPE_I32
  5289. && !global->type.is_mutable
  5290. && global->init_expr.init_expr_type
  5291. == INIT_EXPR_TYPE_I32_CONST) {
  5292. aux_data_end_global = global;
  5293. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5294. aux_data_end_global_index = export->index;
  5295. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5296. aux_data_end);
  5297. aux_data_end = align_uint64(aux_data_end, 16);
  5298. }
  5299. }
  5300. /* For module compiled with -pthread option, the global is:
  5301. [0] stack_top <-- 0
  5302. [1] tls_pointer
  5303. [2] tls_size
  5304. [3] data_end <-- 3
  5305. [4] global_base
  5306. [5] heap_base <-- 5
  5307. [6] dso_handle
  5308. For module compiled without -pthread option:
  5309. [0] stack_top <-- 0
  5310. [1] data_end <-- 1
  5311. [2] global_base
  5312. [3] heap_base <-- 3
  5313. [4] dso_handle
  5314. */
  5315. if (aux_data_end_global && aux_heap_base_global
  5316. && aux_data_end <= aux_heap_base) {
  5317. module->aux_data_end_global_index = aux_data_end_global_index;
  5318. module->aux_data_end = aux_data_end;
  5319. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5320. module->aux_heap_base = aux_heap_base;
  5321. /* Resolve aux stack top global */
  5322. for (global_index = 0; global_index < module->global_count;
  5323. global_index++) {
  5324. global = module->globals + global_index;
  5325. if (global->type.is_mutable /* heap_base and data_end is
  5326. not mutable */
  5327. && global->type.val_type == VALUE_TYPE_I32
  5328. && global->init_expr.init_expr_type
  5329. == INIT_EXPR_TYPE_I32_CONST
  5330. && (uint64)(uint32)global->init_expr.u.i32
  5331. <= aux_heap_base) {
  5332. aux_stack_top_global = global;
  5333. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5334. module->aux_stack_top_global_index =
  5335. module->import_global_count + global_index;
  5336. module->aux_stack_bottom = aux_stack_top;
  5337. module->aux_stack_size =
  5338. aux_stack_top > aux_data_end
  5339. ? (uint32)(aux_stack_top - aux_data_end)
  5340. : (uint32)aux_stack_top;
  5341. LOG_VERBOSE(
  5342. "Found aux stack top global, value: %" PRIu64 ", "
  5343. "global index: %d, stack size: %d",
  5344. aux_stack_top, global_index,
  5345. module->aux_stack_size);
  5346. break;
  5347. }
  5348. }
  5349. if (!aux_stack_top_global) {
  5350. /* Auxiliary stack global isn't found, it must be unused
  5351. in the wasm app, as if it is used, the global must be
  5352. defined. Here we set it to __heap_base global and set
  5353. its size to 0. */
  5354. aux_stack_top_global = aux_heap_base_global;
  5355. aux_stack_top = aux_heap_base;
  5356. module->aux_stack_top_global_index =
  5357. module->aux_heap_base_global_index;
  5358. module->aux_stack_bottom = aux_stack_top;
  5359. module->aux_stack_size = 0;
  5360. }
  5361. break;
  5362. }
  5363. }
  5364. }
  5365. module->malloc_function = (uint32)-1;
  5366. module->free_function = (uint32)-1;
  5367. module->retain_function = (uint32)-1;
  5368. /* Resolve malloc/free function exported by wasm module */
  5369. #if WASM_ENABLE_MEMORY64 != 0
  5370. if (has_module_memory64(module))
  5371. malloc_free_io_type = VALUE_TYPE_I64;
  5372. #endif
  5373. export = module->exports;
  5374. for (i = 0; i < module->export_count; i++, export ++) {
  5375. if (export->kind == EXPORT_KIND_FUNC) {
  5376. if (!strcmp(export->name, "malloc")
  5377. && export->index >= module->import_function_count) {
  5378. func_index = export->index - module->import_function_count;
  5379. func_type = module->functions[func_index]->func_type;
  5380. if (func_type->param_count == 1 && func_type->result_count == 1
  5381. && func_type->types[0] == malloc_free_io_type
  5382. && func_type->types[1] == malloc_free_io_type) {
  5383. bh_assert(module->malloc_function == (uint32)-1);
  5384. module->malloc_function = export->index;
  5385. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5386. export->name, export->index);
  5387. }
  5388. }
  5389. else if (!strcmp(export->name, "__new")
  5390. && export->index >= module->import_function_count) {
  5391. /* __new && __pin for AssemblyScript */
  5392. func_index = export->index - module->import_function_count;
  5393. func_type = module->functions[func_index]->func_type;
  5394. if (func_type->param_count == 2 && func_type->result_count == 1
  5395. && func_type->types[0] == malloc_free_io_type
  5396. && func_type->types[1] == VALUE_TYPE_I32
  5397. && func_type->types[2] == malloc_free_io_type) {
  5398. uint32 j;
  5399. WASMExport *export_tmp;
  5400. bh_assert(module->malloc_function == (uint32)-1);
  5401. module->malloc_function = export->index;
  5402. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5403. export->name, export->index);
  5404. /* resolve retain function.
  5405. If not found, reset malloc function index */
  5406. export_tmp = module->exports;
  5407. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5408. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5409. && (!strcmp(export_tmp->name, "__retain")
  5410. || (!strcmp(export_tmp->name, "__pin")))
  5411. && (export_tmp->index
  5412. >= module->import_function_count)) {
  5413. func_index = export_tmp->index
  5414. - module->import_function_count;
  5415. func_type =
  5416. module->functions[func_index]->func_type;
  5417. if (func_type->param_count == 1
  5418. && func_type->result_count == 1
  5419. && func_type->types[0] == malloc_free_io_type
  5420. && func_type->types[1] == malloc_free_io_type) {
  5421. bh_assert(module->retain_function
  5422. == (uint32)-1);
  5423. module->retain_function = export_tmp->index;
  5424. LOG_VERBOSE("Found retain function, name: %s, "
  5425. "index: %u",
  5426. export_tmp->name,
  5427. export_tmp->index);
  5428. break;
  5429. }
  5430. }
  5431. }
  5432. if (j == module->export_count) {
  5433. module->malloc_function = (uint32)-1;
  5434. LOG_VERBOSE("Can't find retain function,"
  5435. "reset malloc function index to -1");
  5436. }
  5437. }
  5438. }
  5439. else if (((!strcmp(export->name, "free"))
  5440. || (!strcmp(export->name, "__release"))
  5441. || (!strcmp(export->name, "__unpin")))
  5442. && export->index >= module->import_function_count) {
  5443. func_index = export->index - module->import_function_count;
  5444. func_type = module->functions[func_index]->func_type;
  5445. if (func_type->param_count == 1 && func_type->result_count == 0
  5446. && func_type->types[0] == malloc_free_io_type) {
  5447. bh_assert(module->free_function == (uint32)-1);
  5448. module->free_function = export->index;
  5449. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5450. export->name, export->index);
  5451. }
  5452. }
  5453. }
  5454. }
  5455. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5456. handle_table = wasm_interp_get_handle_table();
  5457. #endif
  5458. for (i = 0; i < module->function_count; i++) {
  5459. WASMFunction *func = module->functions[i];
  5460. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5461. error_buf_size)) {
  5462. return false;
  5463. }
  5464. if (i == module->function_count - 1
  5465. && func->code + func->code_size != buf_code_end) {
  5466. set_error_buf(error_buf, error_buf_size,
  5467. "code section size mismatch");
  5468. return false;
  5469. }
  5470. }
  5471. if (!module->possible_memory_grow) {
  5472. WASMMemoryImport *memory_import;
  5473. WASMMemory *memory;
  5474. if (aux_data_end_global && aux_heap_base_global
  5475. && aux_stack_top_global) {
  5476. uint64 init_memory_size;
  5477. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5478. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5479. * valid range of uint32 */
  5480. if (shrunk_memory_size <= UINT32_MAX) {
  5481. if (module->import_memory_count) {
  5482. memory_import = &module->import_memories[0].u.memory;
  5483. init_memory_size =
  5484. (uint64)memory_import->mem_type.num_bytes_per_page
  5485. * memory_import->mem_type.init_page_count;
  5486. if (shrunk_memory_size <= init_memory_size) {
  5487. /* Reset memory info to decrease memory usage */
  5488. memory_import->mem_type.num_bytes_per_page =
  5489. (uint32)shrunk_memory_size;
  5490. memory_import->mem_type.init_page_count = 1;
  5491. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5492. shrunk_memory_size);
  5493. }
  5494. }
  5495. if (module->memory_count) {
  5496. memory = &module->memories[0];
  5497. init_memory_size = (uint64)memory->num_bytes_per_page
  5498. * memory->init_page_count;
  5499. if (shrunk_memory_size <= init_memory_size) {
  5500. /* Reset memory info to decrease memory usage */
  5501. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5502. memory->init_page_count = 1;
  5503. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5504. shrunk_memory_size);
  5505. }
  5506. }
  5507. }
  5508. }
  5509. #if WASM_ENABLE_MULTI_MODULE == 0
  5510. if (module->import_memory_count) {
  5511. memory_import = &module->import_memories[0].u.memory;
  5512. /* Only resize the memory to one big page if num_bytes_per_page is
  5513. * in valid range of uint32 */
  5514. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5515. memory_import->mem_type.num_bytes_per_page *=
  5516. memory_import->mem_type.init_page_count;
  5517. if (memory_import->mem_type.init_page_count > 0)
  5518. memory_import->mem_type.init_page_count =
  5519. memory_import->mem_type.max_page_count = 1;
  5520. else
  5521. memory_import->mem_type.init_page_count =
  5522. memory_import->mem_type.max_page_count = 0;
  5523. }
  5524. }
  5525. if (module->memory_count) {
  5526. memory = &module->memories[0];
  5527. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5528. * valid range of uint32 */
  5529. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5530. memory->num_bytes_per_page *= memory->init_page_count;
  5531. if (memory->init_page_count > 0)
  5532. memory->init_page_count = memory->max_page_count = 1;
  5533. else
  5534. memory->init_page_count = memory->max_page_count = 0;
  5535. }
  5536. }
  5537. #endif
  5538. }
  5539. #if WASM_ENABLE_MEMORY64 != 0
  5540. if (!check_memory64_flags_consistency(module, error_buf, error_buf_size,
  5541. false))
  5542. return false;
  5543. #endif
  5544. calculate_global_data_offset(module);
  5545. #if WASM_ENABLE_FAST_JIT != 0
  5546. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5547. return false;
  5548. }
  5549. #endif
  5550. #if WASM_ENABLE_JIT != 0
  5551. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5552. return false;
  5553. }
  5554. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5555. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5556. return false;
  5557. }
  5558. #else
  5559. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5560. thread fast jit execution, since applying llvm optimizations in
  5561. aot_compile_wasm may cost a lot of time.
  5562. Create thread with enough native stack to apply llvm optimizations */
  5563. if (os_thread_create(&module->llvm_jit_init_thread,
  5564. init_llvm_jit_functions_stage2_callback,
  5565. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5566. != 0) {
  5567. set_error_buf(error_buf, error_buf_size,
  5568. "create orcjit compile thread failed");
  5569. return false;
  5570. }
  5571. #endif
  5572. #endif
  5573. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5574. /* Create threads to compile the jit functions */
  5575. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5576. return false;
  5577. }
  5578. #endif
  5579. #if WASM_ENABLE_MEMORY_TRACING != 0
  5580. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5581. #endif
  5582. return true;
  5583. }
  5584. static WASMModule *
  5585. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5586. {
  5587. WASMModule *module =
  5588. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5589. bh_list_status ret;
  5590. if (!module) {
  5591. return NULL;
  5592. }
  5593. module->module_type = Wasm_Module_Bytecode;
  5594. /* Set start_function to -1, means no start function */
  5595. module->start_function = (uint32)-1;
  5596. module->name = name;
  5597. module->is_binary_freeable = false;
  5598. #if WASM_ENABLE_FAST_INTERP == 0
  5599. module->br_table_cache_list = &module->br_table_cache_list_head;
  5600. ret = bh_list_init(module->br_table_cache_list);
  5601. bh_assert(ret == BH_LIST_SUCCESS);
  5602. #endif
  5603. #if WASM_ENABLE_MULTI_MODULE != 0
  5604. module->import_module_list = &module->import_module_list_head;
  5605. ret = bh_list_init(module->import_module_list);
  5606. bh_assert(ret == BH_LIST_SUCCESS);
  5607. #endif
  5608. #if WASM_ENABLE_DEBUG_INTERP != 0
  5609. ret = bh_list_init(&module->fast_opcode_list);
  5610. bh_assert(ret == BH_LIST_SUCCESS);
  5611. #endif
  5612. #if WASM_ENABLE_GC != 0
  5613. if (!(module->ref_type_set =
  5614. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5615. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5616. goto fail1;
  5617. }
  5618. if (os_mutex_init(&module->rtt_type_lock)) {
  5619. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5620. goto fail2;
  5621. }
  5622. #endif
  5623. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5624. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5625. && WASM_ENABLE_LAZY_JIT != 0)
  5626. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5627. set_error_buf(error_buf, error_buf_size,
  5628. "init instance list lock failed");
  5629. goto fail3;
  5630. }
  5631. #endif
  5632. (void)ret;
  5633. return module;
  5634. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5635. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5636. && WASM_ENABLE_LAZY_JIT != 0)
  5637. fail3:
  5638. #endif
  5639. #if WASM_ENABLE_GC != 0
  5640. os_mutex_destroy(&module->rtt_type_lock);
  5641. fail2:
  5642. bh_hash_map_destroy(module->ref_type_set);
  5643. fail1:
  5644. #endif
  5645. wasm_runtime_free(module);
  5646. return NULL;
  5647. }
  5648. #if WASM_ENABLE_DEBUG_INTERP != 0
  5649. static bool
  5650. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5651. uint32 error_buf_size)
  5652. {
  5653. WASMFastOPCodeNode *fast_op =
  5654. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5655. if (fast_op) {
  5656. fast_op->offset = pos - module->load_addr;
  5657. fast_op->orig_op = orig_op;
  5658. bh_list_insert(&module->fast_opcode_list, fast_op);
  5659. }
  5660. return fast_op ? true : false;
  5661. }
  5662. #endif
  5663. WASMModule *
  5664. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5665. uint32 error_buf_size)
  5666. {
  5667. WASMModule *module = create_module("", error_buf, error_buf_size);
  5668. if (!module)
  5669. return NULL;
  5670. if (!load_from_sections(module, section_list, false, true, false, error_buf,
  5671. error_buf_size)) {
  5672. wasm_loader_unload(module);
  5673. return NULL;
  5674. }
  5675. LOG_VERBOSE("Load module from sections success.\n");
  5676. return module;
  5677. }
  5678. static void
  5679. destroy_sections(WASMSection *section_list)
  5680. {
  5681. WASMSection *section = section_list, *next;
  5682. while (section) {
  5683. next = section->next;
  5684. wasm_runtime_free(section);
  5685. section = next;
  5686. }
  5687. }
  5688. /* clang-format off */
  5689. static uint8 section_ids[] = {
  5690. SECTION_TYPE_USER,
  5691. SECTION_TYPE_TYPE,
  5692. SECTION_TYPE_IMPORT,
  5693. SECTION_TYPE_FUNC,
  5694. SECTION_TYPE_TABLE,
  5695. SECTION_TYPE_MEMORY,
  5696. #if WASM_ENABLE_TAGS != 0
  5697. SECTION_TYPE_TAG,
  5698. #endif
  5699. #if WASM_ENABLE_STRINGREF != 0
  5700. /* must immediately precede the global section,
  5701. or where the global section would be */
  5702. SECTION_TYPE_STRINGREF,
  5703. #endif
  5704. SECTION_TYPE_GLOBAL,
  5705. SECTION_TYPE_EXPORT,
  5706. SECTION_TYPE_START,
  5707. SECTION_TYPE_ELEM,
  5708. #if WASM_ENABLE_BULK_MEMORY != 0
  5709. SECTION_TYPE_DATACOUNT,
  5710. #endif
  5711. SECTION_TYPE_CODE,
  5712. SECTION_TYPE_DATA
  5713. };
  5714. /* clang-format on */
  5715. static uint8
  5716. get_section_index(uint8 section_type)
  5717. {
  5718. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5719. for (uint8 i = 0; i < max_id; i++) {
  5720. if (section_type == section_ids[i])
  5721. return i;
  5722. }
  5723. return (uint8)-1;
  5724. }
  5725. static bool
  5726. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5727. char *error_buf, uint32 error_buf_size)
  5728. {
  5729. WASMSection *section_list_end = NULL, *section;
  5730. const uint8 *p = buf, *p_end = buf + size;
  5731. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5732. uint32 section_size;
  5733. bh_assert(!*p_section_list);
  5734. p += 8;
  5735. while (p < p_end) {
  5736. CHECK_BUF(p, p_end, 1);
  5737. section_type = read_uint8(p);
  5738. section_index = get_section_index(section_type);
  5739. if (section_index != (uint8)-1) {
  5740. if (section_type != SECTION_TYPE_USER) {
  5741. /* Custom sections may be inserted at any place,
  5742. while other sections must occur at most once
  5743. and in prescribed order. */
  5744. if (last_section_index != (uint8)-1
  5745. && (section_index <= last_section_index)) {
  5746. set_error_buf(error_buf, error_buf_size,
  5747. "unexpected content after last section or "
  5748. "junk after last section");
  5749. return false;
  5750. }
  5751. last_section_index = section_index;
  5752. }
  5753. read_leb_uint32(p, p_end, section_size);
  5754. CHECK_BUF1(p, p_end, section_size);
  5755. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5756. error_buf_size))) {
  5757. return false;
  5758. }
  5759. section->section_type = section_type;
  5760. section->section_body = (uint8 *)p;
  5761. section->section_body_size = section_size;
  5762. if (!section_list_end)
  5763. *p_section_list = section_list_end = section;
  5764. else {
  5765. section_list_end->next = section;
  5766. section_list_end = section;
  5767. }
  5768. p += section_size;
  5769. }
  5770. else {
  5771. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5772. return false;
  5773. }
  5774. }
  5775. return true;
  5776. fail:
  5777. return false;
  5778. }
  5779. static void
  5780. exchange32(uint8 *p_data)
  5781. {
  5782. uint8 value = *p_data;
  5783. *p_data = *(p_data + 3);
  5784. *(p_data + 3) = value;
  5785. value = *(p_data + 1);
  5786. *(p_data + 1) = *(p_data + 2);
  5787. *(p_data + 2) = value;
  5788. }
  5789. static union {
  5790. int a;
  5791. char b;
  5792. } __ue = { .a = 1 };
  5793. #define is_little_endian() (__ue.b == 1)
  5794. static bool
  5795. load(const uint8 *buf, uint32 size, WASMModule *module,
  5796. bool wasm_binary_freeable, bool no_resolve, char *error_buf,
  5797. uint32 error_buf_size)
  5798. {
  5799. const uint8 *buf_end = buf + size;
  5800. const uint8 *p = buf, *p_end = buf_end;
  5801. uint32 magic_number, version;
  5802. WASMSection *section_list = NULL;
  5803. CHECK_BUF1(p, p_end, sizeof(uint32));
  5804. magic_number = read_uint32(p);
  5805. if (!is_little_endian())
  5806. exchange32((uint8 *)&magic_number);
  5807. if (magic_number != WASM_MAGIC_NUMBER) {
  5808. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5809. return false;
  5810. }
  5811. CHECK_BUF1(p, p_end, sizeof(uint32));
  5812. version = read_uint32(p);
  5813. if (!is_little_endian())
  5814. exchange32((uint8 *)&version);
  5815. if (version != WASM_CURRENT_VERSION) {
  5816. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5817. return false;
  5818. }
  5819. module->package_version = version;
  5820. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5821. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  5822. no_resolve, error_buf, error_buf_size)) {
  5823. destroy_sections(section_list);
  5824. return false;
  5825. }
  5826. destroy_sections(section_list);
  5827. return true;
  5828. fail:
  5829. return false;
  5830. }
  5831. #if WASM_ENABLE_LIBC_WASI != 0
  5832. /**
  5833. * refer to
  5834. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5835. */
  5836. static bool
  5837. check_wasi_abi_compatibility(const WASMModule *module,
  5838. #if WASM_ENABLE_MULTI_MODULE != 0
  5839. bool main_module,
  5840. #endif
  5841. char *error_buf, uint32 error_buf_size)
  5842. {
  5843. /**
  5844. * be careful with:
  5845. * wasi compatible modules(command/reactor) which don't import any wasi
  5846. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5847. * reactor can depend on nothing. At the same time, each has its own entry
  5848. * point.
  5849. *
  5850. * observations:
  5851. * - clang always injects `_start` into a command
  5852. * - clang always injects `_initialize` into a reactor
  5853. * - `iwasm -f` allows to run a function in the reactor
  5854. *
  5855. * strong assumptions:
  5856. * - no one will define either `_start` or `_initialize` on purpose
  5857. * - `_start` should always be `void _start(void)`
  5858. * - `_initialize` should always be `void _initialize(void)`
  5859. *
  5860. */
  5861. /* clang-format off */
  5862. /**
  5863. *
  5864. * | | import_wasi_api True | | import_wasi_api False | |
  5865. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5866. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5867. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5868. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5869. */
  5870. /* clang-format on */
  5871. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5872. uint32 import_function_count = module->import_function_count;
  5873. WASMFuncType *func_type;
  5874. /* (func (export "_start") (...) */
  5875. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5876. error_buf, error_buf_size);
  5877. if (start) {
  5878. if (start->index < import_function_count) {
  5879. set_error_buf(
  5880. error_buf, error_buf_size,
  5881. "the builtin _start function can not be an import function");
  5882. return false;
  5883. }
  5884. func_type =
  5885. module->functions[start->index - import_function_count]->func_type;
  5886. if (func_type->param_count || func_type->result_count) {
  5887. set_error_buf(error_buf, error_buf_size,
  5888. "the signature of builtin _start function is wrong");
  5889. return false;
  5890. }
  5891. }
  5892. else {
  5893. /* (func (export "_initialize") (...) */
  5894. initialize =
  5895. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5896. error_buf, error_buf_size);
  5897. if (initialize) {
  5898. if (initialize->index < import_function_count) {
  5899. set_error_buf(error_buf, error_buf_size,
  5900. "the builtin _initialize function can not be an "
  5901. "import function");
  5902. return false;
  5903. }
  5904. func_type =
  5905. module->functions[initialize->index - import_function_count]
  5906. ->func_type;
  5907. if (func_type->param_count || func_type->result_count) {
  5908. set_error_buf(
  5909. error_buf, error_buf_size,
  5910. "the signature of builtin _initialize function is wrong");
  5911. return false;
  5912. }
  5913. }
  5914. }
  5915. /* filter out non-wasi compatible modules */
  5916. if (!module->import_wasi_api && !start && !initialize) {
  5917. return true;
  5918. }
  5919. /* should have one at least */
  5920. if (module->import_wasi_api && !start && !initialize) {
  5921. LOG_WARNING("warning: a module with WASI apis should be either "
  5922. "a command or a reactor");
  5923. }
  5924. /*
  5925. * there is at least one of `_start` and `_initialize` in below cases.
  5926. * according to the assumption, they should be all wasi compatible
  5927. */
  5928. #if WASM_ENABLE_MULTI_MODULE != 0
  5929. /* filter out commands (with `_start`) cases */
  5930. if (start && !main_module) {
  5931. set_error_buf(
  5932. error_buf, error_buf_size,
  5933. "a command (with _start function) can not be a sub-module");
  5934. return false;
  5935. }
  5936. #endif
  5937. /*
  5938. * it is ok a reactor acts as a main module,
  5939. * so skip the check about (with `_initialize`)
  5940. */
  5941. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5942. error_buf, error_buf_size);
  5943. if (!memory
  5944. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5945. /*
  5946. * with wasi-threads, it's still an open question if a memory
  5947. * should be exported.
  5948. *
  5949. * https://github.com/WebAssembly/wasi-threads/issues/22
  5950. * https://github.com/WebAssembly/WASI/issues/502
  5951. *
  5952. * Note: this code assumes the number of memories is at most 1.
  5953. */
  5954. && module->import_memory_count == 0
  5955. #endif
  5956. ) {
  5957. set_error_buf(error_buf, error_buf_size,
  5958. "a module with WASI apis must export memory by default");
  5959. return false;
  5960. }
  5961. return true;
  5962. }
  5963. #endif
  5964. WASMModule *
  5965. wasm_loader_load(uint8 *buf, uint32 size,
  5966. #if WASM_ENABLE_MULTI_MODULE != 0
  5967. bool main_module,
  5968. #endif
  5969. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  5970. {
  5971. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  5972. if (!module) {
  5973. return NULL;
  5974. }
  5975. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5976. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5977. module->load_addr = (uint8 *)buf;
  5978. module->load_size = size;
  5979. #endif
  5980. if (!load(buf, size, module, args->wasm_binary_freeable, args->no_resolve,
  5981. error_buf, error_buf_size)) {
  5982. goto fail;
  5983. }
  5984. #if WASM_ENABLE_LIBC_WASI != 0
  5985. /* Check the WASI application ABI */
  5986. if (!check_wasi_abi_compatibility(module,
  5987. #if WASM_ENABLE_MULTI_MODULE != 0
  5988. main_module,
  5989. #endif
  5990. error_buf, error_buf_size)) {
  5991. goto fail;
  5992. }
  5993. #endif
  5994. LOG_VERBOSE("Load module success.\n");
  5995. return module;
  5996. fail:
  5997. wasm_loader_unload(module);
  5998. return NULL;
  5999. }
  6000. void
  6001. wasm_loader_unload(WASMModule *module)
  6002. {
  6003. uint32 i;
  6004. if (!module)
  6005. return;
  6006. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6007. && WASM_ENABLE_LAZY_JIT != 0
  6008. module->orcjit_stop_compiling = true;
  6009. if (module->llvm_jit_init_thread)
  6010. os_thread_join(module->llvm_jit_init_thread, NULL);
  6011. #endif
  6012. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6013. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6014. module internal data after they were freed */
  6015. orcjit_stop_compile_threads(module);
  6016. #endif
  6017. #if WASM_ENABLE_JIT != 0
  6018. if (module->func_ptrs)
  6019. wasm_runtime_free(module->func_ptrs);
  6020. if (module->comp_ctx)
  6021. aot_destroy_comp_context(module->comp_ctx);
  6022. if (module->comp_data)
  6023. aot_destroy_comp_data(module->comp_data);
  6024. #endif
  6025. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6026. && WASM_ENABLE_LAZY_JIT != 0
  6027. if (module->tierup_wait_lock_inited) {
  6028. os_mutex_destroy(&module->tierup_wait_lock);
  6029. os_cond_destroy(&module->tierup_wait_cond);
  6030. }
  6031. #endif
  6032. if (module->imports)
  6033. wasm_runtime_free(module->imports);
  6034. if (module->functions) {
  6035. for (i = 0; i < module->function_count; i++) {
  6036. if (module->functions[i]) {
  6037. if (module->functions[i]->local_offsets)
  6038. wasm_runtime_free(module->functions[i]->local_offsets);
  6039. #if WASM_ENABLE_FAST_INTERP != 0
  6040. if (module->functions[i]->code_compiled)
  6041. wasm_runtime_free(module->functions[i]->code_compiled);
  6042. if (module->functions[i]->consts)
  6043. wasm_runtime_free(module->functions[i]->consts);
  6044. #endif
  6045. #if WASM_ENABLE_FAST_JIT != 0
  6046. if (module->functions[i]->fast_jit_jitted_code) {
  6047. jit_code_cache_free(
  6048. module->functions[i]->fast_jit_jitted_code);
  6049. }
  6050. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6051. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6052. jit_code_cache_free(
  6053. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6054. }
  6055. #endif
  6056. #endif
  6057. #if WASM_ENABLE_GC != 0
  6058. if (module->functions[i]->local_ref_type_maps) {
  6059. wasm_runtime_free(
  6060. module->functions[i]->local_ref_type_maps);
  6061. }
  6062. #endif
  6063. wasm_runtime_free(module->functions[i]);
  6064. }
  6065. }
  6066. wasm_runtime_free(module->functions);
  6067. }
  6068. if (module->tables) {
  6069. #if WASM_ENABLE_GC != 0
  6070. for (i = 0; i < module->table_count; i++) {
  6071. destroy_init_expr(module, &module->tables[i].init_expr);
  6072. }
  6073. #endif
  6074. wasm_runtime_free(module->tables);
  6075. }
  6076. if (module->memories)
  6077. wasm_runtime_free(module->memories);
  6078. if (module->globals) {
  6079. #if WASM_ENABLE_GC != 0
  6080. for (i = 0; i < module->global_count; i++) {
  6081. destroy_init_expr(module, &module->globals[i].init_expr);
  6082. }
  6083. #endif
  6084. wasm_runtime_free(module->globals);
  6085. }
  6086. #if WASM_ENABLE_TAGS != 0
  6087. if (module->tags) {
  6088. for (i = 0; i < module->tag_count; i++) {
  6089. if (module->tags[i])
  6090. wasm_runtime_free(module->tags[i]);
  6091. }
  6092. wasm_runtime_free(module->tags);
  6093. }
  6094. #endif
  6095. if (module->exports)
  6096. wasm_runtime_free(module->exports);
  6097. if (module->table_segments) {
  6098. for (i = 0; i < module->table_seg_count; i++) {
  6099. if (module->table_segments[i].init_values) {
  6100. #if WASM_ENABLE_GC != 0
  6101. uint32 j;
  6102. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6103. destroy_init_expr(
  6104. module, &module->table_segments[i].init_values[j]);
  6105. }
  6106. #endif
  6107. wasm_runtime_free(module->table_segments[i].init_values);
  6108. }
  6109. }
  6110. wasm_runtime_free(module->table_segments);
  6111. }
  6112. if (module->data_segments) {
  6113. for (i = 0; i < module->data_seg_count; i++) {
  6114. if (module->data_segments[i]) {
  6115. if (module->data_segments[i]->is_data_cloned)
  6116. wasm_runtime_free(module->data_segments[i]->data);
  6117. wasm_runtime_free(module->data_segments[i]);
  6118. }
  6119. }
  6120. wasm_runtime_free(module->data_segments);
  6121. }
  6122. if (module->types) {
  6123. for (i = 0; i < module->type_count; i++) {
  6124. if (module->types[i])
  6125. destroy_wasm_type(module->types[i]);
  6126. }
  6127. wasm_runtime_free(module->types);
  6128. }
  6129. if (module->const_str_list) {
  6130. StringNode *node = module->const_str_list, *node_next;
  6131. while (node) {
  6132. node_next = node->next;
  6133. wasm_runtime_free(node);
  6134. node = node_next;
  6135. }
  6136. }
  6137. #if WASM_ENABLE_STRINGREF != 0
  6138. if (module->string_literal_ptrs) {
  6139. wasm_runtime_free((void *)module->string_literal_ptrs);
  6140. }
  6141. if (module->string_literal_lengths) {
  6142. wasm_runtime_free(module->string_literal_lengths);
  6143. }
  6144. #endif
  6145. #if WASM_ENABLE_FAST_INTERP == 0
  6146. if (module->br_table_cache_list) {
  6147. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6148. BrTableCache *node_next;
  6149. while (node) {
  6150. node_next = bh_list_elem_next(node);
  6151. wasm_runtime_free(node);
  6152. node = node_next;
  6153. }
  6154. }
  6155. #endif
  6156. #if WASM_ENABLE_MULTI_MODULE != 0
  6157. /* just release the sub module list */
  6158. if (module->import_module_list) {
  6159. WASMRegisteredModule *node =
  6160. bh_list_first_elem(module->import_module_list);
  6161. while (node) {
  6162. WASMRegisteredModule *next = bh_list_elem_next(node);
  6163. bh_list_remove(module->import_module_list, node);
  6164. /*
  6165. * unload(sub_module) will be triggered during runtime_destroy().
  6166. * every module in the global module list will be unloaded one by
  6167. * one. so don't worry.
  6168. */
  6169. wasm_runtime_free(node);
  6170. /*
  6171. * the module file reading buffer will be released
  6172. * in runtime_destroy()
  6173. */
  6174. node = next;
  6175. }
  6176. }
  6177. #endif
  6178. #if WASM_ENABLE_DEBUG_INTERP != 0
  6179. WASMFastOPCodeNode *fast_opcode =
  6180. bh_list_first_elem(&module->fast_opcode_list);
  6181. while (fast_opcode) {
  6182. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6183. wasm_runtime_free(fast_opcode);
  6184. fast_opcode = next;
  6185. }
  6186. #endif
  6187. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6188. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6189. && WASM_ENABLE_LAZY_JIT != 0)
  6190. os_mutex_destroy(&module->instance_list_lock);
  6191. #endif
  6192. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6193. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6194. #endif
  6195. #if WASM_ENABLE_FAST_JIT != 0
  6196. if (module->fast_jit_func_ptrs) {
  6197. wasm_runtime_free(module->fast_jit_func_ptrs);
  6198. }
  6199. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6200. if (module->fast_jit_thread_locks_inited[i]) {
  6201. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6202. }
  6203. }
  6204. #endif
  6205. #if WASM_ENABLE_GC != 0
  6206. os_mutex_destroy(&module->rtt_type_lock);
  6207. bh_hash_map_destroy(module->ref_type_set);
  6208. if (module->rtt_types) {
  6209. for (i = 0; i < module->type_count; i++) {
  6210. if (module->rtt_types[i])
  6211. wasm_runtime_free(module->rtt_types[i]);
  6212. }
  6213. wasm_runtime_free(module->rtt_types);
  6214. }
  6215. #if WASM_ENABLE_STRINGREF != 0
  6216. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6217. if (module->stringref_rtts[i])
  6218. wasm_runtime_free(module->stringref_rtts[i]);
  6219. }
  6220. #endif
  6221. #endif
  6222. wasm_runtime_free(module);
  6223. }
  6224. bool
  6225. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6226. const uint8 *start_addr, const uint8 *code_end_addr,
  6227. uint8 label_type, uint8 **p_else_addr,
  6228. uint8 **p_end_addr)
  6229. {
  6230. const uint8 *p = start_addr, *p_end = code_end_addr;
  6231. uint8 *else_addr = NULL;
  6232. char error_buf[128];
  6233. uint32 block_nested_depth = 1, count, i, j, t;
  6234. uint32 error_buf_size = sizeof(error_buf);
  6235. uint8 opcode, u8;
  6236. BlockAddr block_stack[16] = { { 0 } }, *block;
  6237. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6238. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6239. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6240. if (block[j].start_addr == start_addr) {
  6241. /* Cache hit */
  6242. *p_else_addr = block[j].else_addr;
  6243. *p_end_addr = block[j].end_addr;
  6244. return true;
  6245. }
  6246. }
  6247. /* Cache unhit */
  6248. block_stack[0].start_addr = start_addr;
  6249. while (p < code_end_addr) {
  6250. opcode = *p++;
  6251. #if WASM_ENABLE_DEBUG_INTERP != 0
  6252. op_break_retry:
  6253. #endif
  6254. switch (opcode) {
  6255. case WASM_OP_UNREACHABLE:
  6256. case WASM_OP_NOP:
  6257. break;
  6258. #if WASM_ENABLE_EXCE_HANDLING != 0
  6259. case WASM_OP_TRY:
  6260. u8 = read_uint8(p);
  6261. if (block_nested_depth
  6262. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6263. block_stack[block_nested_depth].start_addr = p;
  6264. block_stack[block_nested_depth].else_addr = NULL;
  6265. }
  6266. block_nested_depth++;
  6267. break;
  6268. case EXT_OP_TRY:
  6269. skip_leb_uint32(p, p_end);
  6270. if (block_nested_depth
  6271. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6272. block_stack[block_nested_depth].start_addr = p;
  6273. block_stack[block_nested_depth].else_addr = NULL;
  6274. }
  6275. block_nested_depth++;
  6276. break;
  6277. case WASM_OP_CATCH:
  6278. if (block_nested_depth == 1) {
  6279. *p_end_addr = (uint8 *)(p - 1);
  6280. /* stop search and return the address of the catch block */
  6281. return true;
  6282. }
  6283. break;
  6284. case WASM_OP_CATCH_ALL:
  6285. if (block_nested_depth == 1) {
  6286. *p_end_addr = (uint8 *)(p - 1);
  6287. /* stop search and return the address of the catch_all block
  6288. */
  6289. return true;
  6290. }
  6291. break;
  6292. case WASM_OP_THROW:
  6293. /* skip tag_index */
  6294. skip_leb(p);
  6295. break;
  6296. case WASM_OP_RETHROW:
  6297. /* skip depth */
  6298. skip_leb(p);
  6299. break;
  6300. case WASM_OP_DELEGATE:
  6301. if (block_nested_depth == 1) {
  6302. *p_end_addr = (uint8 *)(p - 1);
  6303. return true;
  6304. }
  6305. else {
  6306. skip_leb(p);
  6307. /* the DELEGATE opcode ends the tryblock, */
  6308. block_nested_depth--;
  6309. if (block_nested_depth
  6310. < sizeof(block_stack) / sizeof(BlockAddr))
  6311. block_stack[block_nested_depth].end_addr =
  6312. (uint8 *)(p - 1);
  6313. }
  6314. break;
  6315. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6316. case WASM_OP_BLOCK:
  6317. case WASM_OP_LOOP:
  6318. case WASM_OP_IF:
  6319. {
  6320. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6321. u8 = read_uint8(p);
  6322. if (is_byte_a_type(u8)) {
  6323. #if WASM_ENABLE_GC != 0
  6324. if (wasm_is_type_multi_byte_type(u8)) {
  6325. /* the possible extra bytes of GC ref type have been
  6326. modified to OP_NOP, no need to resolve them again */
  6327. }
  6328. #endif
  6329. }
  6330. else {
  6331. p--;
  6332. /* block type */
  6333. skip_leb_int32(p, p_end);
  6334. }
  6335. if (block_nested_depth
  6336. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6337. block_stack[block_nested_depth].start_addr = p;
  6338. block_stack[block_nested_depth].else_addr = NULL;
  6339. }
  6340. block_nested_depth++;
  6341. break;
  6342. }
  6343. case EXT_OP_BLOCK:
  6344. case EXT_OP_LOOP:
  6345. case EXT_OP_IF:
  6346. /* block type */
  6347. skip_leb_int32(p, p_end);
  6348. if (block_nested_depth
  6349. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6350. block_stack[block_nested_depth].start_addr = p;
  6351. block_stack[block_nested_depth].else_addr = NULL;
  6352. }
  6353. block_nested_depth++;
  6354. break;
  6355. case WASM_OP_ELSE:
  6356. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6357. else_addr = (uint8 *)(p - 1);
  6358. if (block_nested_depth - 1
  6359. < sizeof(block_stack) / sizeof(BlockAddr))
  6360. block_stack[block_nested_depth - 1].else_addr =
  6361. (uint8 *)(p - 1);
  6362. break;
  6363. case WASM_OP_END:
  6364. if (block_nested_depth == 1) {
  6365. if (label_type == LABEL_TYPE_IF)
  6366. *p_else_addr = else_addr;
  6367. *p_end_addr = (uint8 *)(p - 1);
  6368. block_stack[0].end_addr = (uint8 *)(p - 1);
  6369. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6370. t++) {
  6371. start_addr = block_stack[t].start_addr;
  6372. if (start_addr) {
  6373. i = ((uintptr_t)start_addr)
  6374. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6375. block =
  6376. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6377. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6378. if (!block[j].start_addr)
  6379. break;
  6380. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6381. memmove(block + 1, block,
  6382. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6383. * sizeof(BlockAddr));
  6384. j = 0;
  6385. }
  6386. block[j].start_addr = block_stack[t].start_addr;
  6387. block[j].else_addr = block_stack[t].else_addr;
  6388. block[j].end_addr = block_stack[t].end_addr;
  6389. }
  6390. else
  6391. break;
  6392. }
  6393. return true;
  6394. }
  6395. else {
  6396. block_nested_depth--;
  6397. if (block_nested_depth
  6398. < sizeof(block_stack) / sizeof(BlockAddr))
  6399. block_stack[block_nested_depth].end_addr =
  6400. (uint8 *)(p - 1);
  6401. }
  6402. break;
  6403. case WASM_OP_BR:
  6404. case WASM_OP_BR_IF:
  6405. skip_leb_uint32(p, p_end); /* labelidx */
  6406. break;
  6407. case WASM_OP_BR_TABLE:
  6408. read_leb_uint32(p, p_end, count); /* label num */
  6409. #if WASM_ENABLE_FAST_INTERP != 0
  6410. for (i = 0; i <= count; i++) /* labelidxs */
  6411. skip_leb_uint32(p, p_end);
  6412. #else
  6413. p += count + 1;
  6414. while (*p == WASM_OP_NOP)
  6415. p++;
  6416. #endif
  6417. break;
  6418. #if WASM_ENABLE_FAST_INTERP == 0
  6419. case EXT_OP_BR_TABLE_CACHE:
  6420. read_leb_uint32(p, p_end, count); /* label num */
  6421. while (*p == WASM_OP_NOP)
  6422. p++;
  6423. break;
  6424. #endif
  6425. case WASM_OP_RETURN:
  6426. break;
  6427. case WASM_OP_CALL:
  6428. #if WASM_ENABLE_TAIL_CALL != 0
  6429. case WASM_OP_RETURN_CALL:
  6430. #endif
  6431. skip_leb_uint32(p, p_end); /* funcidx */
  6432. break;
  6433. case WASM_OP_CALL_INDIRECT:
  6434. #if WASM_ENABLE_TAIL_CALL != 0
  6435. case WASM_OP_RETURN_CALL_INDIRECT:
  6436. #endif
  6437. skip_leb_uint32(p, p_end); /* typeidx */
  6438. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6439. skip_leb_uint32(p, p_end); /* tableidx */
  6440. #else
  6441. u8 = read_uint8(p); /* 0x00 */
  6442. #endif
  6443. break;
  6444. #if WASM_ENABLE_GC != 0
  6445. case WASM_OP_CALL_REF:
  6446. case WASM_OP_RETURN_CALL_REF:
  6447. skip_leb_uint32(p, p_end); /* typeidx */
  6448. break;
  6449. #endif
  6450. case WASM_OP_DROP:
  6451. case WASM_OP_SELECT:
  6452. case WASM_OP_DROP_64:
  6453. case WASM_OP_SELECT_64:
  6454. break;
  6455. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6456. case WASM_OP_SELECT_T:
  6457. {
  6458. skip_leb_uint32(p, p_end); /* vec length */
  6459. u8 = read_uint8(p); /* typeidx */
  6460. /* the possible extra bytes of GC ref type have been
  6461. modified to OP_NOP, no need to resolve them again */
  6462. break;
  6463. }
  6464. case WASM_OP_TABLE_GET:
  6465. case WASM_OP_TABLE_SET:
  6466. skip_leb_uint32(p, p_end); /* table index */
  6467. break;
  6468. case WASM_OP_REF_NULL:
  6469. {
  6470. u8 = read_uint8(p); /* type */
  6471. if (is_byte_a_type(u8)) {
  6472. #if WASM_ENABLE_GC != 0
  6473. if (wasm_is_type_multi_byte_type(u8)) {
  6474. /* the possible extra bytes of GC ref type have been
  6475. modified to OP_NOP, no need to resolve them again */
  6476. }
  6477. #endif
  6478. }
  6479. else {
  6480. p--;
  6481. skip_leb_uint32(p, p_end);
  6482. }
  6483. break;
  6484. }
  6485. case WASM_OP_REF_IS_NULL:
  6486. break;
  6487. case WASM_OP_REF_FUNC:
  6488. skip_leb_uint32(p, p_end); /* func index */
  6489. break;
  6490. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6491. #if WASM_ENABLE_GC != 0
  6492. case WASM_OP_REF_AS_NON_NULL:
  6493. case WASM_OP_REF_EQ:
  6494. break;
  6495. case WASM_OP_BR_ON_NULL:
  6496. case WASM_OP_BR_ON_NON_NULL:
  6497. skip_leb_uint32(p, p_end); /* label index */
  6498. break;
  6499. #endif /* end of WASM_ENABLE_GC != 0 */
  6500. case WASM_OP_GET_LOCAL:
  6501. case WASM_OP_SET_LOCAL:
  6502. case WASM_OP_TEE_LOCAL:
  6503. case WASM_OP_GET_GLOBAL:
  6504. case WASM_OP_SET_GLOBAL:
  6505. case WASM_OP_GET_GLOBAL_64:
  6506. case WASM_OP_SET_GLOBAL_64:
  6507. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6508. skip_leb_uint32(p, p_end); /* local index */
  6509. break;
  6510. case EXT_OP_GET_LOCAL_FAST:
  6511. case EXT_OP_SET_LOCAL_FAST:
  6512. case EXT_OP_TEE_LOCAL_FAST:
  6513. CHECK_BUF(p, p_end, 1);
  6514. p++;
  6515. break;
  6516. case WASM_OP_I32_LOAD:
  6517. case WASM_OP_I64_LOAD:
  6518. case WASM_OP_F32_LOAD:
  6519. case WASM_OP_F64_LOAD:
  6520. case WASM_OP_I32_LOAD8_S:
  6521. case WASM_OP_I32_LOAD8_U:
  6522. case WASM_OP_I32_LOAD16_S:
  6523. case WASM_OP_I32_LOAD16_U:
  6524. case WASM_OP_I64_LOAD8_S:
  6525. case WASM_OP_I64_LOAD8_U:
  6526. case WASM_OP_I64_LOAD16_S:
  6527. case WASM_OP_I64_LOAD16_U:
  6528. case WASM_OP_I64_LOAD32_S:
  6529. case WASM_OP_I64_LOAD32_U:
  6530. case WASM_OP_I32_STORE:
  6531. case WASM_OP_I64_STORE:
  6532. case WASM_OP_F32_STORE:
  6533. case WASM_OP_F64_STORE:
  6534. case WASM_OP_I32_STORE8:
  6535. case WASM_OP_I32_STORE16:
  6536. case WASM_OP_I64_STORE8:
  6537. case WASM_OP_I64_STORE16:
  6538. case WASM_OP_I64_STORE32:
  6539. skip_leb_align(p, p_end); /* align */
  6540. skip_leb_mem_offset(p, p_end); /* offset */
  6541. break;
  6542. case WASM_OP_MEMORY_SIZE:
  6543. case WASM_OP_MEMORY_GROW:
  6544. skip_leb_memidx(p, p_end); /* memidx */
  6545. break;
  6546. case WASM_OP_I32_CONST:
  6547. skip_leb_int32(p, p_end);
  6548. break;
  6549. case WASM_OP_I64_CONST:
  6550. skip_leb_int64(p, p_end);
  6551. break;
  6552. case WASM_OP_F32_CONST:
  6553. p += sizeof(float32);
  6554. break;
  6555. case WASM_OP_F64_CONST:
  6556. p += sizeof(float64);
  6557. break;
  6558. case WASM_OP_I32_EQZ:
  6559. case WASM_OP_I32_EQ:
  6560. case WASM_OP_I32_NE:
  6561. case WASM_OP_I32_LT_S:
  6562. case WASM_OP_I32_LT_U:
  6563. case WASM_OP_I32_GT_S:
  6564. case WASM_OP_I32_GT_U:
  6565. case WASM_OP_I32_LE_S:
  6566. case WASM_OP_I32_LE_U:
  6567. case WASM_OP_I32_GE_S:
  6568. case WASM_OP_I32_GE_U:
  6569. case WASM_OP_I64_EQZ:
  6570. case WASM_OP_I64_EQ:
  6571. case WASM_OP_I64_NE:
  6572. case WASM_OP_I64_LT_S:
  6573. case WASM_OP_I64_LT_U:
  6574. case WASM_OP_I64_GT_S:
  6575. case WASM_OP_I64_GT_U:
  6576. case WASM_OP_I64_LE_S:
  6577. case WASM_OP_I64_LE_U:
  6578. case WASM_OP_I64_GE_S:
  6579. case WASM_OP_I64_GE_U:
  6580. case WASM_OP_F32_EQ:
  6581. case WASM_OP_F32_NE:
  6582. case WASM_OP_F32_LT:
  6583. case WASM_OP_F32_GT:
  6584. case WASM_OP_F32_LE:
  6585. case WASM_OP_F32_GE:
  6586. case WASM_OP_F64_EQ:
  6587. case WASM_OP_F64_NE:
  6588. case WASM_OP_F64_LT:
  6589. case WASM_OP_F64_GT:
  6590. case WASM_OP_F64_LE:
  6591. case WASM_OP_F64_GE:
  6592. case WASM_OP_I32_CLZ:
  6593. case WASM_OP_I32_CTZ:
  6594. case WASM_OP_I32_POPCNT:
  6595. case WASM_OP_I32_ADD:
  6596. case WASM_OP_I32_SUB:
  6597. case WASM_OP_I32_MUL:
  6598. case WASM_OP_I32_DIV_S:
  6599. case WASM_OP_I32_DIV_U:
  6600. case WASM_OP_I32_REM_S:
  6601. case WASM_OP_I32_REM_U:
  6602. case WASM_OP_I32_AND:
  6603. case WASM_OP_I32_OR:
  6604. case WASM_OP_I32_XOR:
  6605. case WASM_OP_I32_SHL:
  6606. case WASM_OP_I32_SHR_S:
  6607. case WASM_OP_I32_SHR_U:
  6608. case WASM_OP_I32_ROTL:
  6609. case WASM_OP_I32_ROTR:
  6610. case WASM_OP_I64_CLZ:
  6611. case WASM_OP_I64_CTZ:
  6612. case WASM_OP_I64_POPCNT:
  6613. case WASM_OP_I64_ADD:
  6614. case WASM_OP_I64_SUB:
  6615. case WASM_OP_I64_MUL:
  6616. case WASM_OP_I64_DIV_S:
  6617. case WASM_OP_I64_DIV_U:
  6618. case WASM_OP_I64_REM_S:
  6619. case WASM_OP_I64_REM_U:
  6620. case WASM_OP_I64_AND:
  6621. case WASM_OP_I64_OR:
  6622. case WASM_OP_I64_XOR:
  6623. case WASM_OP_I64_SHL:
  6624. case WASM_OP_I64_SHR_S:
  6625. case WASM_OP_I64_SHR_U:
  6626. case WASM_OP_I64_ROTL:
  6627. case WASM_OP_I64_ROTR:
  6628. case WASM_OP_F32_ABS:
  6629. case WASM_OP_F32_NEG:
  6630. case WASM_OP_F32_CEIL:
  6631. case WASM_OP_F32_FLOOR:
  6632. case WASM_OP_F32_TRUNC:
  6633. case WASM_OP_F32_NEAREST:
  6634. case WASM_OP_F32_SQRT:
  6635. case WASM_OP_F32_ADD:
  6636. case WASM_OP_F32_SUB:
  6637. case WASM_OP_F32_MUL:
  6638. case WASM_OP_F32_DIV:
  6639. case WASM_OP_F32_MIN:
  6640. case WASM_OP_F32_MAX:
  6641. case WASM_OP_F32_COPYSIGN:
  6642. case WASM_OP_F64_ABS:
  6643. case WASM_OP_F64_NEG:
  6644. case WASM_OP_F64_CEIL:
  6645. case WASM_OP_F64_FLOOR:
  6646. case WASM_OP_F64_TRUNC:
  6647. case WASM_OP_F64_NEAREST:
  6648. case WASM_OP_F64_SQRT:
  6649. case WASM_OP_F64_ADD:
  6650. case WASM_OP_F64_SUB:
  6651. case WASM_OP_F64_MUL:
  6652. case WASM_OP_F64_DIV:
  6653. case WASM_OP_F64_MIN:
  6654. case WASM_OP_F64_MAX:
  6655. case WASM_OP_F64_COPYSIGN:
  6656. case WASM_OP_I32_WRAP_I64:
  6657. case WASM_OP_I32_TRUNC_S_F32:
  6658. case WASM_OP_I32_TRUNC_U_F32:
  6659. case WASM_OP_I32_TRUNC_S_F64:
  6660. case WASM_OP_I32_TRUNC_U_F64:
  6661. case WASM_OP_I64_EXTEND_S_I32:
  6662. case WASM_OP_I64_EXTEND_U_I32:
  6663. case WASM_OP_I64_TRUNC_S_F32:
  6664. case WASM_OP_I64_TRUNC_U_F32:
  6665. case WASM_OP_I64_TRUNC_S_F64:
  6666. case WASM_OP_I64_TRUNC_U_F64:
  6667. case WASM_OP_F32_CONVERT_S_I32:
  6668. case WASM_OP_F32_CONVERT_U_I32:
  6669. case WASM_OP_F32_CONVERT_S_I64:
  6670. case WASM_OP_F32_CONVERT_U_I64:
  6671. case WASM_OP_F32_DEMOTE_F64:
  6672. case WASM_OP_F64_CONVERT_S_I32:
  6673. case WASM_OP_F64_CONVERT_U_I32:
  6674. case WASM_OP_F64_CONVERT_S_I64:
  6675. case WASM_OP_F64_CONVERT_U_I64:
  6676. case WASM_OP_F64_PROMOTE_F32:
  6677. case WASM_OP_I32_REINTERPRET_F32:
  6678. case WASM_OP_I64_REINTERPRET_F64:
  6679. case WASM_OP_F32_REINTERPRET_I32:
  6680. case WASM_OP_F64_REINTERPRET_I64:
  6681. case WASM_OP_I32_EXTEND8_S:
  6682. case WASM_OP_I32_EXTEND16_S:
  6683. case WASM_OP_I64_EXTEND8_S:
  6684. case WASM_OP_I64_EXTEND16_S:
  6685. case WASM_OP_I64_EXTEND32_S:
  6686. break;
  6687. #if WASM_ENABLE_GC != 0
  6688. case WASM_OP_GC_PREFIX:
  6689. {
  6690. uint32 opcode1;
  6691. read_leb_uint32(p, p_end, opcode1);
  6692. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6693. is no larger than UINT8_MAX */
  6694. opcode = (uint8)opcode1;
  6695. switch (opcode) {
  6696. case WASM_OP_STRUCT_NEW:
  6697. case WASM_OP_STRUCT_NEW_DEFAULT:
  6698. skip_leb_uint32(p, p_end); /* typeidx */
  6699. break;
  6700. case WASM_OP_STRUCT_GET:
  6701. case WASM_OP_STRUCT_GET_S:
  6702. case WASM_OP_STRUCT_GET_U:
  6703. case WASM_OP_STRUCT_SET:
  6704. skip_leb_uint32(p, p_end); /* typeidx */
  6705. skip_leb_uint32(p, p_end); /* fieldidx */
  6706. break;
  6707. case WASM_OP_ARRAY_NEW:
  6708. case WASM_OP_ARRAY_NEW_DEFAULT:
  6709. case WASM_OP_ARRAY_GET:
  6710. case WASM_OP_ARRAY_GET_S:
  6711. case WASM_OP_ARRAY_GET_U:
  6712. case WASM_OP_ARRAY_SET:
  6713. case WASM_OP_ARRAY_FILL:
  6714. skip_leb_uint32(p, p_end); /* typeidx */
  6715. break;
  6716. case WASM_OP_ARRAY_COPY:
  6717. skip_leb_uint32(p, p_end); /* typeidx1 */
  6718. skip_leb_uint32(p, p_end); /* typeidx2 */
  6719. break;
  6720. case WASM_OP_ARRAY_LEN:
  6721. break;
  6722. case WASM_OP_ARRAY_NEW_FIXED:
  6723. case WASM_OP_ARRAY_NEW_DATA:
  6724. case WASM_OP_ARRAY_NEW_ELEM:
  6725. skip_leb_uint32(p, p_end); /* typeidx */
  6726. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6727. break;
  6728. case WASM_OP_REF_I31:
  6729. case WASM_OP_I31_GET_S:
  6730. case WASM_OP_I31_GET_U:
  6731. break;
  6732. case WASM_OP_REF_TEST:
  6733. case WASM_OP_REF_CAST:
  6734. case WASM_OP_REF_TEST_NULLABLE:
  6735. case WASM_OP_REF_CAST_NULLABLE:
  6736. skip_leb_int32(p, p_end); /* heaptype */
  6737. break;
  6738. case WASM_OP_BR_ON_CAST:
  6739. case WASM_OP_BR_ON_CAST_FAIL:
  6740. p += sizeof(uint8); /* castflag */
  6741. skip_leb_uint32(p, p_end); /* labelidx */
  6742. skip_leb_int32(p, p_end); /* heaptype */
  6743. skip_leb_int32(p, p_end); /* heaptype2 */
  6744. break;
  6745. case WASM_OP_ANY_CONVERT_EXTERN:
  6746. case WASM_OP_EXTERN_CONVERT_ANY:
  6747. break;
  6748. #if WASM_ENABLE_STRINGREF != 0
  6749. case WASM_OP_STRING_NEW_UTF8:
  6750. case WASM_OP_STRING_NEW_WTF16:
  6751. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6752. case WASM_OP_STRING_NEW_WTF8:
  6753. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6754. break;
  6755. case WASM_OP_STRING_CONST:
  6756. skip_leb_int32(p, p_end); /* contents */
  6757. break;
  6758. case WASM_OP_STRING_MEASURE_UTF8:
  6759. case WASM_OP_STRING_MEASURE_WTF8:
  6760. case WASM_OP_STRING_MEASURE_WTF16:
  6761. break;
  6762. case WASM_OP_STRING_ENCODE_UTF8:
  6763. case WASM_OP_STRING_ENCODE_WTF16:
  6764. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6765. case WASM_OP_STRING_ENCODE_WTF8:
  6766. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6767. break;
  6768. case WASM_OP_STRING_CONCAT:
  6769. case WASM_OP_STRING_EQ:
  6770. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6771. case WASM_OP_STRING_AS_WTF8:
  6772. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6773. break;
  6774. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6775. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6776. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6777. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6778. break;
  6779. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6780. case WASM_OP_STRING_AS_WTF16:
  6781. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6782. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6783. break;
  6784. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6785. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6786. break;
  6787. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6788. case WASM_OP_STRING_AS_ITER:
  6789. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6790. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6791. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6792. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6793. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6794. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6795. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6796. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6797. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6798. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6799. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6800. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6801. break;
  6802. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6803. default:
  6804. return false;
  6805. }
  6806. break;
  6807. }
  6808. #endif /* end of WASM_ENABLE_GC != 0 */
  6809. case WASM_OP_MISC_PREFIX:
  6810. {
  6811. uint32 opcode1;
  6812. read_leb_uint32(p, p_end, opcode1);
  6813. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6814. is no larger than UINT8_MAX */
  6815. opcode = (uint8)opcode1;
  6816. switch (opcode) {
  6817. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6818. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6819. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6820. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6821. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6822. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6823. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6824. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6825. break;
  6826. #if WASM_ENABLE_BULK_MEMORY != 0
  6827. case WASM_OP_MEMORY_INIT:
  6828. skip_leb_uint32(p, p_end);
  6829. skip_leb_memidx(p, p_end);
  6830. break;
  6831. case WASM_OP_DATA_DROP:
  6832. skip_leb_uint32(p, p_end);
  6833. break;
  6834. case WASM_OP_MEMORY_COPY:
  6835. skip_leb_memidx(p, p_end);
  6836. skip_leb_memidx(p, p_end);
  6837. break;
  6838. case WASM_OP_MEMORY_FILL:
  6839. skip_leb_memidx(p, p_end);
  6840. break;
  6841. #endif /* WASM_ENABLE_BULK_MEMORY */
  6842. #if WASM_ENABLE_REF_TYPES != 0
  6843. case WASM_OP_TABLE_INIT:
  6844. case WASM_OP_TABLE_COPY:
  6845. /* tableidx */
  6846. skip_leb_uint32(p, p_end);
  6847. /* elemidx */
  6848. skip_leb_uint32(p, p_end);
  6849. break;
  6850. case WASM_OP_ELEM_DROP:
  6851. /* elemidx */
  6852. skip_leb_uint32(p, p_end);
  6853. break;
  6854. case WASM_OP_TABLE_SIZE:
  6855. case WASM_OP_TABLE_GROW:
  6856. case WASM_OP_TABLE_FILL:
  6857. skip_leb_uint32(p, p_end); /* table idx */
  6858. break;
  6859. #endif /* WASM_ENABLE_REF_TYPES */
  6860. default:
  6861. return false;
  6862. }
  6863. break;
  6864. }
  6865. #if WASM_ENABLE_SIMD != 0
  6866. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6867. case WASM_OP_SIMD_PREFIX:
  6868. {
  6869. uint32 opcode1;
  6870. read_leb_uint32(p, p_end, opcode1);
  6871. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6872. is no larger than UINT8_MAX */
  6873. opcode = (uint8)opcode1;
  6874. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6875. */
  6876. switch (opcode) {
  6877. case SIMD_v128_load:
  6878. case SIMD_v128_load8x8_s:
  6879. case SIMD_v128_load8x8_u:
  6880. case SIMD_v128_load16x4_s:
  6881. case SIMD_v128_load16x4_u:
  6882. case SIMD_v128_load32x2_s:
  6883. case SIMD_v128_load32x2_u:
  6884. case SIMD_v128_load8_splat:
  6885. case SIMD_v128_load16_splat:
  6886. case SIMD_v128_load32_splat:
  6887. case SIMD_v128_load64_splat:
  6888. case SIMD_v128_store:
  6889. /* memarg align */
  6890. skip_leb_uint32(p, p_end);
  6891. /* memarg offset */
  6892. skip_leb_mem_offset(p, p_end);
  6893. break;
  6894. case SIMD_v128_const:
  6895. case SIMD_v8x16_shuffle:
  6896. /* immByte[16] immLaneId[16] */
  6897. CHECK_BUF1(p, p_end, 16);
  6898. p += 16;
  6899. break;
  6900. case SIMD_i8x16_extract_lane_s:
  6901. case SIMD_i8x16_extract_lane_u:
  6902. case SIMD_i8x16_replace_lane:
  6903. case SIMD_i16x8_extract_lane_s:
  6904. case SIMD_i16x8_extract_lane_u:
  6905. case SIMD_i16x8_replace_lane:
  6906. case SIMD_i32x4_extract_lane:
  6907. case SIMD_i32x4_replace_lane:
  6908. case SIMD_i64x2_extract_lane:
  6909. case SIMD_i64x2_replace_lane:
  6910. case SIMD_f32x4_extract_lane:
  6911. case SIMD_f32x4_replace_lane:
  6912. case SIMD_f64x2_extract_lane:
  6913. case SIMD_f64x2_replace_lane:
  6914. /* ImmLaneId */
  6915. CHECK_BUF(p, p_end, 1);
  6916. p++;
  6917. break;
  6918. case SIMD_v128_load8_lane:
  6919. case SIMD_v128_load16_lane:
  6920. case SIMD_v128_load32_lane:
  6921. case SIMD_v128_load64_lane:
  6922. case SIMD_v128_store8_lane:
  6923. case SIMD_v128_store16_lane:
  6924. case SIMD_v128_store32_lane:
  6925. case SIMD_v128_store64_lane:
  6926. /* memarg align */
  6927. skip_leb_uint32(p, p_end);
  6928. /* memarg offset */
  6929. skip_leb_mem_offset(p, p_end);
  6930. /* ImmLaneId */
  6931. CHECK_BUF(p, p_end, 1);
  6932. p++;
  6933. break;
  6934. case SIMD_v128_load32_zero:
  6935. case SIMD_v128_load64_zero:
  6936. /* memarg align */
  6937. skip_leb_uint32(p, p_end);
  6938. /* memarg offset */
  6939. skip_leb_mem_offset(p, p_end);
  6940. break;
  6941. default:
  6942. /*
  6943. * since latest SIMD specific used almost every value
  6944. * from 0x00 to 0xff, the default branch will present
  6945. * all opcodes without imm
  6946. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6947. */
  6948. break;
  6949. }
  6950. break;
  6951. }
  6952. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6953. #endif /* end of WASM_ENABLE_SIMD */
  6954. #if WASM_ENABLE_SHARED_MEMORY != 0
  6955. case WASM_OP_ATOMIC_PREFIX:
  6956. {
  6957. uint32 opcode1;
  6958. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6959. read_leb_uint32(p, p_end, opcode1);
  6960. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6961. is no larger than UINT8_MAX */
  6962. opcode = (uint8)opcode1;
  6963. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6964. skip_leb_uint32(p, p_end); /* align */
  6965. skip_leb_mem_offset(p, p_end); /* offset */
  6966. }
  6967. else {
  6968. /* atomic.fence doesn't have memarg */
  6969. p++;
  6970. }
  6971. break;
  6972. }
  6973. #endif
  6974. #if WASM_ENABLE_DEBUG_INTERP != 0
  6975. case DEBUG_OP_BREAK:
  6976. {
  6977. WASMDebugInstance *debug_instance =
  6978. wasm_exec_env_get_instance(exec_env);
  6979. char original_opcode[1];
  6980. uint64 size = 1;
  6981. WASMModuleInstance *module_inst =
  6982. (WASMModuleInstance *)exec_env->module_inst;
  6983. uint64 offset = (p - 1) >= module_inst->module->load_addr
  6984. ? (p - 1) - module_inst->module->load_addr
  6985. : ~0;
  6986. if (debug_instance) {
  6987. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  6988. original_opcode, &size)
  6989. && size == 1) {
  6990. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  6991. "with %02x: ",
  6992. original_opcode[0]);
  6993. opcode = original_opcode[0];
  6994. goto op_break_retry;
  6995. }
  6996. }
  6997. break;
  6998. }
  6999. #endif
  7000. default:
  7001. return false;
  7002. }
  7003. }
  7004. (void)u8;
  7005. (void)exec_env;
  7006. return false;
  7007. fail:
  7008. return false;
  7009. }
  7010. #if WASM_ENABLE_FAST_INTERP != 0
  7011. #if WASM_DEBUG_PREPROCESSOR != 0
  7012. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7013. #else
  7014. #define LOG_OP(...) (void)0
  7015. #endif
  7016. #define PATCH_ELSE 0
  7017. #define PATCH_END 1
  7018. typedef struct BranchBlockPatch {
  7019. struct BranchBlockPatch *next;
  7020. uint8 patch_type;
  7021. uint8 *code_compiled;
  7022. } BranchBlockPatch;
  7023. #endif
  7024. typedef struct BranchBlock {
  7025. uint8 label_type;
  7026. BlockType block_type;
  7027. uint8 *start_addr;
  7028. uint8 *else_addr;
  7029. uint8 *end_addr;
  7030. uint32 stack_cell_num;
  7031. #if WASM_ENABLE_GC != 0
  7032. uint32 reftype_map_num;
  7033. /* Indicate which local is used inside current block, used to validate
  7034. * local.get with non-nullable ref types */
  7035. uint8 *local_use_mask;
  7036. uint32 local_use_mask_size;
  7037. #endif
  7038. #if WASM_ENABLE_FAST_INTERP != 0
  7039. uint16 dynamic_offset;
  7040. uint8 *code_compiled;
  7041. BranchBlockPatch *patch_list;
  7042. /* This is used to save params frame_offset of of if block */
  7043. int16 *param_frame_offsets;
  7044. /* This is used to recover the dynamic offset for else branch,
  7045. * and also to remember the start offset of dynamic space which
  7046. * stores the block arguments for loop block, so we can use it
  7047. * to copy the stack operands to the loop block's arguments in
  7048. * wasm_loader_emit_br_info for opcode br. */
  7049. uint16 start_dynamic_offset;
  7050. #endif
  7051. /* Indicate the operand stack is in polymorphic state.
  7052. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7053. * to polymorphic state until the block's 'end' opcode is processed.
  7054. * If stack is in polymorphic state and stack is empty, instruction can
  7055. * pop any type of value directly without decreasing stack top pointer
  7056. * and stack cell num. */
  7057. bool is_stack_polymorphic;
  7058. } BranchBlock;
  7059. typedef struct WASMLoaderContext {
  7060. /* frame ref stack */
  7061. uint8 *frame_ref;
  7062. uint8 *frame_ref_bottom;
  7063. uint8 *frame_ref_boundary;
  7064. uint32 frame_ref_size;
  7065. uint32 stack_cell_num;
  7066. uint32 max_stack_cell_num;
  7067. #if WASM_ENABLE_GC != 0
  7068. /* frame reftype map stack */
  7069. WASMRefTypeMap *frame_reftype_map;
  7070. WASMRefTypeMap *frame_reftype_map_bottom;
  7071. WASMRefTypeMap *frame_reftype_map_boundary;
  7072. uint32 frame_reftype_map_size;
  7073. uint32 reftype_map_num;
  7074. uint32 max_reftype_map_num;
  7075. /* Current module */
  7076. WASMModule *module;
  7077. /* Current module's ref_type_set */
  7078. HashMap *ref_type_set;
  7079. /* Always point to local variable ref_type of
  7080. wasm_loader_prepare_bytecode */
  7081. WASMRefType *ref_type_tmp;
  7082. #endif
  7083. /* frame csp stack */
  7084. BranchBlock *frame_csp;
  7085. BranchBlock *frame_csp_bottom;
  7086. BranchBlock *frame_csp_boundary;
  7087. uint32 frame_csp_size;
  7088. uint32 csp_num;
  7089. uint32 max_csp_num;
  7090. #if WASM_ENABLE_FAST_INTERP != 0
  7091. /* frame offset stack */
  7092. int16 *frame_offset;
  7093. int16 *frame_offset_bottom;
  7094. int16 *frame_offset_boundary;
  7095. uint32 frame_offset_size;
  7096. int16 dynamic_offset;
  7097. int16 start_dynamic_offset;
  7098. int16 max_dynamic_offset;
  7099. /* preserved local offset */
  7100. int16 preserved_local_offset;
  7101. /* const buffer */
  7102. uint8 *const_buf;
  7103. uint16 num_const;
  7104. uint16 const_cell_num;
  7105. uint32 const_buf_size;
  7106. /* processed code */
  7107. uint8 *p_code_compiled;
  7108. uint8 *p_code_compiled_end;
  7109. uint32 code_compiled_size;
  7110. /* If the last opcode will be dropped, the peak memory usage will be larger
  7111. * than the final code_compiled_size, we record the peak size to ensure
  7112. * there will not be invalid memory access during second traverse */
  7113. uint32 code_compiled_peak_size;
  7114. #endif
  7115. } WASMLoaderContext;
  7116. typedef struct Const {
  7117. WASMValue value;
  7118. uint16 slot_index;
  7119. uint8 value_type;
  7120. } Const;
  7121. #define CHECK_CSP_PUSH() \
  7122. do { \
  7123. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7124. MEM_REALLOC( \
  7125. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7126. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7127. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7128. ctx->frame_csp_boundary = \
  7129. ctx->frame_csp_bottom \
  7130. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7131. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7132. } \
  7133. } while (0)
  7134. #define CHECK_CSP_POP() \
  7135. do { \
  7136. if (ctx->csp_num < 1) { \
  7137. set_error_buf(error_buf, error_buf_size, \
  7138. "type mismatch: " \
  7139. "expect data but block stack was empty"); \
  7140. goto fail; \
  7141. } \
  7142. } while (0)
  7143. #if WASM_ENABLE_FAST_INTERP != 0
  7144. static bool
  7145. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7146. uint32 error_buf_size)
  7147. {
  7148. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7149. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7150. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7151. ctx->frame_offset_size + 16);
  7152. ctx->frame_offset_size += 16;
  7153. ctx->frame_offset_boundary =
  7154. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7155. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7156. }
  7157. return true;
  7158. fail:
  7159. return false;
  7160. }
  7161. static bool
  7162. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7163. {
  7164. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7165. return false;
  7166. return true;
  7167. }
  7168. static void
  7169. free_label_patch_list(BranchBlock *frame_csp)
  7170. {
  7171. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7172. BranchBlockPatch *next;
  7173. while (label_patch != NULL) {
  7174. next = label_patch->next;
  7175. wasm_runtime_free(label_patch);
  7176. label_patch = next;
  7177. }
  7178. frame_csp->patch_list = NULL;
  7179. }
  7180. static void
  7181. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7182. {
  7183. BranchBlock *tmp_csp = frame_csp;
  7184. uint32 i;
  7185. for (i = 0; i < csp_num; i++) {
  7186. free_label_patch_list(tmp_csp);
  7187. tmp_csp++;
  7188. }
  7189. }
  7190. static void
  7191. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7192. {
  7193. BranchBlock *tmp_csp = frame_csp;
  7194. uint32 i;
  7195. for (i = 0; i < csp_num; i++) {
  7196. if (tmp_csp->param_frame_offsets)
  7197. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7198. tmp_csp++;
  7199. }
  7200. }
  7201. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7202. #if WASM_ENABLE_GC != 0
  7203. static bool
  7204. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7205. char *error_buf, uint32 error_buf_size)
  7206. {
  7207. BranchBlock *current_csp = ctx->frame_csp - 1;
  7208. uint32 local_mask_size;
  7209. if (local_count == 0) {
  7210. current_csp->local_use_mask_size = 0;
  7211. return true;
  7212. }
  7213. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7214. * else branch, we don't need to allocate memory again */
  7215. if (!current_csp->local_use_mask) {
  7216. local_mask_size = (local_count + 7) / sizeof(uint8);
  7217. if (!(current_csp->local_use_mask =
  7218. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7219. return false;
  7220. }
  7221. current_csp->local_use_mask_size = local_mask_size;
  7222. }
  7223. else {
  7224. local_mask_size = current_csp->local_use_mask_size;
  7225. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7226. }
  7227. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7228. /* For non-function blocks, inherit the use status from parent block */
  7229. BranchBlock *parent_csp = current_csp - 1;
  7230. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7231. bh_assert(parent_csp->local_use_mask);
  7232. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7233. parent_csp->local_use_mask, local_mask_size);
  7234. }
  7235. return true;
  7236. }
  7237. static void
  7238. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7239. {
  7240. BranchBlock *current_csp = ctx->frame_csp - 1;
  7241. bh_assert(current_csp->local_use_mask
  7242. || current_csp->local_use_mask_size == 0);
  7243. if (current_csp->local_use_mask) {
  7244. wasm_runtime_free(current_csp->local_use_mask);
  7245. }
  7246. current_csp->local_use_mask = NULL;
  7247. current_csp->local_use_mask_size = 0;
  7248. }
  7249. static void
  7250. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7251. {
  7252. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7253. uint32 i;
  7254. for (i = 0; i < ctx->csp_num; i++) {
  7255. if (tmp_csp->local_use_mask) {
  7256. wasm_runtime_free(tmp_csp->local_use_mask);
  7257. tmp_csp->local_use_mask = NULL;
  7258. tmp_csp->local_use_mask_size = 0;
  7259. }
  7260. tmp_csp++;
  7261. }
  7262. }
  7263. static void
  7264. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7265. {
  7266. BranchBlock *current_csp = ctx->frame_csp - 1;
  7267. uint32 byte_offset = index / sizeof(uint8);
  7268. uint32 bit_offset = index % sizeof(uint8);
  7269. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7270. bh_assert(current_csp->local_use_mask);
  7271. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7272. }
  7273. static bool
  7274. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7275. {
  7276. BranchBlock *current_csp = ctx->frame_csp - 1;
  7277. uint32 byte_offset = index / sizeof(uint8);
  7278. uint32 bit_offset = index % sizeof(uint8);
  7279. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7280. bh_assert(current_csp->local_use_mask);
  7281. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7282. ? true
  7283. : false;
  7284. }
  7285. #endif /* end of WASM_ENABLE_GC != 0 */
  7286. static void
  7287. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7288. {
  7289. if (ctx) {
  7290. if (ctx->frame_ref_bottom)
  7291. wasm_runtime_free(ctx->frame_ref_bottom);
  7292. #if WASM_ENABLE_GC != 0
  7293. if (ctx->frame_reftype_map_bottom)
  7294. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7295. #endif
  7296. if (ctx->frame_csp_bottom) {
  7297. #if WASM_ENABLE_FAST_INTERP != 0
  7298. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7299. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7300. ctx->csp_num);
  7301. #endif
  7302. #if WASM_ENABLE_GC != 0
  7303. wasm_loader_clean_all_local_use_masks(ctx);
  7304. #endif
  7305. wasm_runtime_free(ctx->frame_csp_bottom);
  7306. }
  7307. #if WASM_ENABLE_FAST_INTERP != 0
  7308. if (ctx->frame_offset_bottom)
  7309. wasm_runtime_free(ctx->frame_offset_bottom);
  7310. if (ctx->const_buf)
  7311. wasm_runtime_free(ctx->const_buf);
  7312. #endif
  7313. wasm_runtime_free(ctx);
  7314. }
  7315. }
  7316. static WASMLoaderContext *
  7317. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7318. {
  7319. WASMLoaderContext *loader_ctx =
  7320. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7321. if (!loader_ctx)
  7322. return NULL;
  7323. loader_ctx->frame_ref_size = 32;
  7324. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7325. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7326. goto fail;
  7327. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7328. #if WASM_ENABLE_GC != 0
  7329. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7330. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7331. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7332. error_buf_size)))
  7333. goto fail;
  7334. loader_ctx->frame_reftype_map_boundary =
  7335. loader_ctx->frame_reftype_map_bottom + 16;
  7336. #endif
  7337. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7338. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7339. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7340. goto fail;
  7341. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7342. #if WASM_ENABLE_EXCE_HANDLING != 0
  7343. func->exception_handler_count = 0;
  7344. #endif
  7345. #if WASM_ENABLE_FAST_INTERP != 0
  7346. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7347. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7348. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7349. error_buf_size)))
  7350. goto fail;
  7351. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7352. loader_ctx->num_const = 0;
  7353. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7354. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7355. error_buf, error_buf_size)))
  7356. goto fail;
  7357. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7358. set_error_buf(error_buf, error_buf_size,
  7359. "fast interpreter offset overflow");
  7360. goto fail;
  7361. }
  7362. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7363. loader_ctx->max_dynamic_offset =
  7364. func->param_cell_num + func->local_cell_num;
  7365. #endif
  7366. return loader_ctx;
  7367. fail:
  7368. wasm_loader_ctx_destroy(loader_ctx);
  7369. return NULL;
  7370. }
  7371. static bool
  7372. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7373. uint32 error_buf_size)
  7374. {
  7375. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7376. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7377. /* Increase the frame ref stack */
  7378. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7379. ctx->frame_ref_size + 16);
  7380. ctx->frame_ref_size += 16;
  7381. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7382. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7383. }
  7384. #if WASM_ENABLE_GC != 0
  7385. if (wasm_is_type_multi_byte_type(type)
  7386. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7387. /* Increase the frame reftype map stack */
  7388. bh_assert(
  7389. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7390. * sizeof(WASMRefTypeMap))
  7391. == ctx->frame_reftype_map_size);
  7392. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7393. ctx->frame_reftype_map_size
  7394. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7395. ctx->frame_reftype_map =
  7396. ctx->frame_reftype_map_bottom
  7397. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7398. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7399. ctx->frame_reftype_map_boundary =
  7400. ctx->frame_reftype_map_bottom
  7401. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7402. }
  7403. #endif
  7404. return true;
  7405. fail:
  7406. return false;
  7407. }
  7408. static bool
  7409. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7410. uint32 error_buf_size)
  7411. {
  7412. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7413. uint32 i;
  7414. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7415. return false;
  7416. #if WASM_ENABLE_GC != 0
  7417. if (wasm_is_type_multi_byte_type(type)) {
  7418. WASMRefType *ref_type;
  7419. if (!(ref_type =
  7420. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7421. error_buf, error_buf_size))) {
  7422. return false;
  7423. }
  7424. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7425. /* Increase the frame reftype map stack */
  7426. bh_assert((uint32)((ctx->frame_reftype_map
  7427. - ctx->frame_reftype_map_bottom)
  7428. * sizeof(WASMRefTypeMap))
  7429. == ctx->frame_reftype_map_size);
  7430. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7431. ctx->frame_reftype_map_size,
  7432. ctx->frame_reftype_map_size
  7433. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7434. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7435. + ctx->frame_reftype_map_size
  7436. / ((uint32)sizeof(WASMRefTypeMap));
  7437. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7438. ctx->frame_reftype_map_boundary =
  7439. ctx->frame_reftype_map_bottom
  7440. + ctx->frame_reftype_map_size
  7441. / ((uint32)sizeof(WASMRefTypeMap));
  7442. }
  7443. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7444. ctx->frame_reftype_map->ref_type = ref_type;
  7445. ctx->frame_reftype_map++;
  7446. ctx->reftype_map_num++;
  7447. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7448. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7449. }
  7450. #endif
  7451. for (i = 0; i < type_cell_num; i++)
  7452. *ctx->frame_ref++ = type;
  7453. ctx->stack_cell_num += type_cell_num;
  7454. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7455. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7456. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7457. set_error_buf(error_buf, error_buf_size,
  7458. "operand stack depth limit exceeded");
  7459. return false;
  7460. }
  7461. }
  7462. return true;
  7463. #if WASM_ENABLE_GC != 0
  7464. fail:
  7465. return false;
  7466. #endif
  7467. }
  7468. static bool
  7469. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7470. int32 stack_cell_num,
  7471. #if WASM_ENABLE_GC != 0
  7472. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7473. #endif
  7474. uint8 type,
  7475. #if WASM_ENABLE_GC != 0
  7476. WASMRefType *ref_type,
  7477. #endif
  7478. char *error_buf, uint32 error_buf_size)
  7479. {
  7480. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7481. #if WASM_ENABLE_GC != 0
  7482. WASMRefType *frame_reftype = NULL;
  7483. #endif
  7484. if (stack_cell_num < type_cell_num) {
  7485. set_error_buf(error_buf, error_buf_size,
  7486. "type mismatch: expect data but stack was empty");
  7487. return false;
  7488. }
  7489. #if WASM_ENABLE_GC == 0
  7490. for (i = 0; i < type_cell_num; i++) {
  7491. if (*(frame_ref - 1 - i) != type) {
  7492. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7493. "type mismatch: expect ", type2str(type),
  7494. " but got other");
  7495. return false;
  7496. }
  7497. }
  7498. #else
  7499. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7500. bh_assert(reftype_map_num > 0);
  7501. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7502. }
  7503. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7504. ref_type, ctx->module->types,
  7505. ctx->module->type_count)) {
  7506. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7507. "type mismatch: expect ", type2str(type),
  7508. " but got other");
  7509. return false;
  7510. }
  7511. for (i = 0; i < type_cell_num - 1; i++) {
  7512. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7513. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7514. "type mismatch: expect ", type2str(type),
  7515. " but got other");
  7516. return false;
  7517. }
  7518. }
  7519. #endif
  7520. return true;
  7521. }
  7522. static bool
  7523. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7524. uint32 error_buf_size)
  7525. {
  7526. int32 block_stack_cell_num =
  7527. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7528. #if WASM_ENABLE_GC != 0
  7529. int32 reftype_map_num =
  7530. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7531. #endif
  7532. if (block_stack_cell_num > 0) {
  7533. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7534. /* the stack top is a value of any type, return success */
  7535. return true;
  7536. }
  7537. #if WASM_ENABLE_GC != 0
  7538. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7539. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7540. WASMRefType *stack_top_ref_type = NULL;
  7541. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7542. bh_assert(reftype_map_num > 0);
  7543. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7544. }
  7545. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7546. ctx->ref_type_tmp, ctx->module->types,
  7547. ctx->module->type_count)) {
  7548. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7549. uint32 ref_type_struct_size =
  7550. wasm_reftype_struct_size(stack_top_ref_type);
  7551. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7552. stack_top_ref_type, ref_type_struct_size);
  7553. }
  7554. return true;
  7555. }
  7556. }
  7557. #endif
  7558. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7559. #if WASM_ENABLE_GC != 0
  7560. ctx->frame_reftype_map, reftype_map_num,
  7561. #endif
  7562. type,
  7563. #if WASM_ENABLE_GC != 0
  7564. ctx->ref_type_tmp,
  7565. #endif
  7566. error_buf, error_buf_size)) {
  7567. return false;
  7568. }
  7569. return true;
  7570. }
  7571. static bool
  7572. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7573. uint32 error_buf_size)
  7574. {
  7575. BranchBlock *cur_block = ctx->frame_csp - 1;
  7576. int32 available_stack_cell =
  7577. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7578. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7579. /* Directly return success if current block is in stack
  7580. polymorphic state while stack is empty. */
  7581. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7582. return true;
  7583. if (type == VALUE_TYPE_VOID)
  7584. return true;
  7585. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7586. return false;
  7587. bh_assert(available_stack_cell > 0);
  7588. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7589. type = VALUE_TYPE_ANY;
  7590. cell_num_to_pop = 1;
  7591. }
  7592. ctx->frame_ref -= cell_num_to_pop;
  7593. ctx->stack_cell_num -= cell_num_to_pop;
  7594. #if WASM_ENABLE_GC != 0
  7595. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7596. ctx->frame_reftype_map--;
  7597. ctx->reftype_map_num--;
  7598. }
  7599. #endif
  7600. return true;
  7601. }
  7602. #if WASM_ENABLE_GC != 0
  7603. /* Get the stack top element of current block */
  7604. static bool
  7605. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7606. WASMRefType **p_ref_type, char *error_buf,
  7607. uint32 error_buf_size)
  7608. {
  7609. BranchBlock *cur_block = ctx->frame_csp - 1;
  7610. int32 available_stack_cell =
  7611. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7612. if (available_stack_cell <= 0) {
  7613. /* Directly return success if current block is in stack
  7614. polymorphic state while stack is empty. */
  7615. if (cur_block->is_stack_polymorphic) {
  7616. *p_type = VALUE_TYPE_ANY;
  7617. return true;
  7618. }
  7619. else {
  7620. set_error_buf(
  7621. error_buf, error_buf_size,
  7622. "type mismatch: expect data but block stack was empty");
  7623. return false;
  7624. }
  7625. }
  7626. *p_type = *(ctx->frame_ref - 1);
  7627. if (wasm_is_type_multi_byte_type(*p_type)) {
  7628. int32 available_reftype_map =
  7629. (int32)(ctx->reftype_map_num
  7630. - (ctx->frame_csp - 1)->reftype_map_num);
  7631. bh_assert(available_reftype_map > 0);
  7632. (void)available_reftype_map;
  7633. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7634. }
  7635. return true;
  7636. }
  7637. #if WASM_ENABLE_FAST_INTERP != 0
  7638. static bool
  7639. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7640. char *error_buf, uint32 error_buf_size);
  7641. #endif
  7642. /* Check whether the stack top elem is a heap object, and if yes,
  7643. pop and return it */
  7644. static bool
  7645. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7646. WASMRefType *ref_ht_ret, char *error_buf,
  7647. uint32 error_buf_size)
  7648. {
  7649. uint8 type = 0;
  7650. WASMRefType *ref_type = NULL;
  7651. /* Get stack top element */
  7652. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7653. error_buf_size)) {
  7654. return false;
  7655. }
  7656. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7657. /* stack top isn't a ref type */
  7658. && !wasm_is_type_reftype(type)) {
  7659. set_error_buf(error_buf, error_buf_size,
  7660. "type mismatch: expect heap object but got others");
  7661. return false;
  7662. }
  7663. /* POP stack top */
  7664. if (wasm_is_type_multi_byte_type(type)) {
  7665. bh_assert(ref_type);
  7666. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7667. wasm_reftype_struct_size(ref_type));
  7668. }
  7669. #if WASM_ENABLE_FAST_INTERP != 0
  7670. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7671. error_buf_size)) {
  7672. return false;
  7673. }
  7674. #else
  7675. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7676. return false;
  7677. }
  7678. #endif
  7679. if (p_type)
  7680. *p_type = type;
  7681. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7682. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7683. wasm_reftype_struct_size(ref_type));
  7684. }
  7685. return true;
  7686. }
  7687. /* Check whether the stack top elem is subtype of (ref null ht),
  7688. and if yes, pop it and return the converted (ref ht) */
  7689. static bool
  7690. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7691. WASMRefType *ref_ht_ret, char *error_buf,
  7692. uint32 error_buf_size)
  7693. {
  7694. uint8 type = 0;
  7695. WASMRefType ref_type = { 0 };
  7696. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7697. error_buf_size)) {
  7698. return false;
  7699. }
  7700. /* Convert to related (ref ht) and return */
  7701. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7702. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7703. */
  7704. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7705. HEAP_TYPE_ARRAY
  7706. + (type - REF_TYPE_ARRAYREF));
  7707. type = ref_ht_ret->ref_type;
  7708. }
  7709. else if (wasm_is_reftype_htref_nullable(type)
  7710. || wasm_is_reftype_htref_non_nullable(type)) {
  7711. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7712. wasm_reftype_struct_size(&ref_type));
  7713. /* Convert to (ref ht) */
  7714. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7715. ref_ht_ret->ref_ht_common.nullable = false;
  7716. type = ref_ht_ret->ref_type;
  7717. }
  7718. *p_type = type;
  7719. return true;
  7720. }
  7721. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7722. and if yes, pop it and return the type_idx */
  7723. static bool
  7724. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7725. uint32 *p_type_idx, char *error_buf,
  7726. uint32 error_buf_size)
  7727. {
  7728. uint8 type = 0;
  7729. int32 type_idx = -1;
  7730. WASMRefType *ref_type = NULL;
  7731. /* Get stack top element */
  7732. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7733. error_buf_size)) {
  7734. return false;
  7735. }
  7736. if (type != VALUE_TYPE_ANY) {
  7737. /* stack top isn't (ref null $t) */
  7738. if (!((wasm_is_reftype_htref_nullable(type)
  7739. || wasm_is_reftype_htref_non_nullable(type))
  7740. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7741. set_error_buf(error_buf, error_buf_size,
  7742. "type mismatch: expect (ref null $t) but got others");
  7743. return false;
  7744. }
  7745. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7746. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7747. wasm_reftype_struct_size(ref_type));
  7748. }
  7749. /* POP stack top */
  7750. #if WASM_ENABLE_FAST_INTERP != 0
  7751. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7752. error_buf_size)) {
  7753. return false;
  7754. }
  7755. #else
  7756. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7757. return false;
  7758. }
  7759. #endif
  7760. /* Convert to type_idx and return */
  7761. *p_type = type;
  7762. if (type != VALUE_TYPE_ANY)
  7763. *p_type_idx = (uint32)type_idx;
  7764. return true;
  7765. }
  7766. #endif /* WASM_ENABLE_GC != 0 */
  7767. #if WASM_ENABLE_FAST_INTERP == 0
  7768. static bool
  7769. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7770. uint8 type_push, uint8 type_pop, char *error_buf,
  7771. uint32 error_buf_size)
  7772. {
  7773. for (int i = 0; i < pop_cnt; i++) {
  7774. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7775. error_buf_size))
  7776. return false;
  7777. }
  7778. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7779. return false;
  7780. return true;
  7781. }
  7782. #endif
  7783. static bool
  7784. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7785. BlockType block_type, uint8 *start_addr,
  7786. char *error_buf, uint32 error_buf_size)
  7787. {
  7788. CHECK_CSP_PUSH();
  7789. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7790. ctx->frame_csp->label_type = label_type;
  7791. ctx->frame_csp->block_type = block_type;
  7792. ctx->frame_csp->start_addr = start_addr;
  7793. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7794. #if WASM_ENABLE_GC != 0
  7795. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7796. #endif
  7797. #if WASM_ENABLE_FAST_INTERP != 0
  7798. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7799. ctx->frame_csp->patch_list = NULL;
  7800. #endif
  7801. ctx->frame_csp++;
  7802. ctx->csp_num++;
  7803. if (ctx->csp_num > ctx->max_csp_num) {
  7804. ctx->max_csp_num = ctx->csp_num;
  7805. if (ctx->max_csp_num > UINT16_MAX) {
  7806. set_error_buf(error_buf, error_buf_size,
  7807. "label stack depth limit exceeded");
  7808. return false;
  7809. }
  7810. }
  7811. return true;
  7812. fail:
  7813. return false;
  7814. }
  7815. static bool
  7816. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7817. uint32 error_buf_size)
  7818. {
  7819. CHECK_CSP_POP();
  7820. #if WASM_ENABLE_FAST_INTERP != 0
  7821. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7822. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7823. #endif
  7824. ctx->frame_csp--;
  7825. ctx->csp_num--;
  7826. return true;
  7827. fail:
  7828. return false;
  7829. }
  7830. #if WASM_ENABLE_FAST_INTERP != 0
  7831. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7832. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7833. #define emit_label(opcode) \
  7834. do { \
  7835. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7836. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7837. } while (0)
  7838. #define skip_label() \
  7839. do { \
  7840. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7841. LOG_OP("\ndelete last op\n"); \
  7842. } while (0)
  7843. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7844. #if UINTPTR_MAX == UINT64_MAX
  7845. #define emit_label(opcode) \
  7846. do { \
  7847. int32 offset = \
  7848. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7849. /* emit int32 relative offset in 64-bit target */ \
  7850. wasm_loader_emit_uint32(loader_ctx, offset); \
  7851. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7852. } while (0)
  7853. #else
  7854. #define emit_label(opcode) \
  7855. do { \
  7856. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7857. /* emit uint32 label address in 32-bit target */ \
  7858. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7859. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7860. } while (0)
  7861. #endif
  7862. #define skip_label() \
  7863. do { \
  7864. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7865. LOG_OP("\ndelete last op\n"); \
  7866. } while (0)
  7867. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7868. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7869. #define emit_label(opcode) \
  7870. do { \
  7871. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7872. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7873. } while (0)
  7874. #define skip_label() \
  7875. do { \
  7876. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7877. LOG_OP("\ndelete last op\n"); \
  7878. } while (0)
  7879. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7880. #define emit_empty_label_addr_and_frame_ip(type) \
  7881. do { \
  7882. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7883. loader_ctx->p_code_compiled, error_buf, \
  7884. error_buf_size)) \
  7885. goto fail; \
  7886. /* label address, to be patched */ \
  7887. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7888. } while (0)
  7889. #define emit_br_info(frame_csp, is_br) \
  7890. do { \
  7891. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7892. error_buf_size)) \
  7893. goto fail; \
  7894. } while (0)
  7895. #define LAST_OP_OUTPUT_I32() \
  7896. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7897. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7898. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7899. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7900. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7901. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7902. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7903. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7904. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7905. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7906. || (last_op == EXT_OP_COPY_STACK_TOP)
  7907. #define LAST_OP_OUTPUT_I64() \
  7908. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7909. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7910. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7911. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7912. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7913. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7914. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7915. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7916. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7917. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7918. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7919. #define GET_CONST_OFFSET(type, val) \
  7920. do { \
  7921. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7922. &operand_offset, error_buf, \
  7923. error_buf_size))) \
  7924. goto fail; \
  7925. } while (0)
  7926. #define GET_CONST_F32_OFFSET(type, fval) \
  7927. do { \
  7928. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7929. &operand_offset, error_buf, \
  7930. error_buf_size))) \
  7931. goto fail; \
  7932. } while (0)
  7933. #define GET_CONST_F64_OFFSET(type, fval) \
  7934. do { \
  7935. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7936. &operand_offset, error_buf, \
  7937. error_buf_size))) \
  7938. goto fail; \
  7939. } while (0)
  7940. #define emit_operand(ctx, offset) \
  7941. do { \
  7942. wasm_loader_emit_int16(ctx, offset); \
  7943. LOG_OP("%d\t", offset); \
  7944. } while (0)
  7945. #define emit_byte(ctx, byte) \
  7946. do { \
  7947. wasm_loader_emit_uint8(ctx, byte); \
  7948. LOG_OP("%d\t", byte); \
  7949. } while (0)
  7950. #define emit_uint32(ctx, value) \
  7951. do { \
  7952. wasm_loader_emit_uint32(ctx, value); \
  7953. LOG_OP("%d\t", value); \
  7954. } while (0)
  7955. #define emit_uint64(ctx, value) \
  7956. do { \
  7957. wasm_loader_emit_const(ctx, &value, false); \
  7958. LOG_OP("%lld\t", value); \
  7959. } while (0)
  7960. #define emit_float32(ctx, value) \
  7961. do { \
  7962. wasm_loader_emit_const(ctx, &value, true); \
  7963. LOG_OP("%f\t", value); \
  7964. } while (0)
  7965. #define emit_float64(ctx, value) \
  7966. do { \
  7967. wasm_loader_emit_const(ctx, &value, false); \
  7968. LOG_OP("%f\t", value); \
  7969. } while (0)
  7970. static bool
  7971. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7972. {
  7973. if (!(ctx->p_code_compiled =
  7974. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7975. return false;
  7976. ctx->p_code_compiled_end =
  7977. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  7978. /* clean up frame ref */
  7979. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  7980. ctx->frame_ref = ctx->frame_ref_bottom;
  7981. ctx->stack_cell_num = 0;
  7982. #if WASM_ENABLE_GC != 0
  7983. /* clean up reftype map */
  7984. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  7985. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  7986. ctx->reftype_map_num = 0;
  7987. #endif
  7988. /* clean up frame csp */
  7989. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  7990. ctx->frame_csp = ctx->frame_csp_bottom;
  7991. ctx->csp_num = 0;
  7992. ctx->max_csp_num = 0;
  7993. /* clean up frame offset */
  7994. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  7995. ctx->frame_offset = ctx->frame_offset_bottom;
  7996. ctx->dynamic_offset = ctx->start_dynamic_offset;
  7997. /* init preserved local offsets */
  7998. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  7999. /* const buf is reserved */
  8000. return true;
  8001. }
  8002. static void
  8003. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8004. {
  8005. ctx->code_compiled_size += size;
  8006. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8007. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8008. }
  8009. }
  8010. static void
  8011. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8012. {
  8013. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8014. if (ctx->p_code_compiled) {
  8015. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8016. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8017. #endif
  8018. bh_memcpy_s(ctx->p_code_compiled,
  8019. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8020. value, size);
  8021. ctx->p_code_compiled += size;
  8022. }
  8023. else {
  8024. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8025. bh_assert((ctx->code_compiled_size & 1) == 0);
  8026. #endif
  8027. increase_compiled_code_space(ctx, size);
  8028. }
  8029. }
  8030. static void
  8031. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8032. {
  8033. if (ctx->p_code_compiled) {
  8034. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8035. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8036. #endif
  8037. STORE_U32(ctx->p_code_compiled, value);
  8038. ctx->p_code_compiled += sizeof(uint32);
  8039. }
  8040. else {
  8041. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8042. bh_assert((ctx->code_compiled_size & 1) == 0);
  8043. #endif
  8044. increase_compiled_code_space(ctx, sizeof(uint32));
  8045. }
  8046. }
  8047. static void
  8048. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8049. {
  8050. if (ctx->p_code_compiled) {
  8051. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8052. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8053. #endif
  8054. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8055. ctx->p_code_compiled += sizeof(int16);
  8056. }
  8057. else {
  8058. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8059. bh_assert((ctx->code_compiled_size & 1) == 0);
  8060. #endif
  8061. increase_compiled_code_space(ctx, sizeof(uint16));
  8062. }
  8063. }
  8064. static void
  8065. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8066. {
  8067. if (ctx->p_code_compiled) {
  8068. *(ctx->p_code_compiled) = value;
  8069. ctx->p_code_compiled += sizeof(uint8);
  8070. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8071. ctx->p_code_compiled++;
  8072. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8073. #endif
  8074. }
  8075. else {
  8076. increase_compiled_code_space(ctx, sizeof(uint8));
  8077. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8078. increase_compiled_code_space(ctx, sizeof(uint8));
  8079. bh_assert((ctx->code_compiled_size & 1) == 0);
  8080. #endif
  8081. }
  8082. }
  8083. static void
  8084. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8085. {
  8086. if (ctx->p_code_compiled) {
  8087. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8088. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8089. #endif
  8090. STORE_PTR(ctx->p_code_compiled, value);
  8091. ctx->p_code_compiled += sizeof(void *);
  8092. }
  8093. else {
  8094. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8095. bh_assert((ctx->code_compiled_size & 1) == 0);
  8096. #endif
  8097. increase_compiled_code_space(ctx, sizeof(void *));
  8098. }
  8099. }
  8100. static void
  8101. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8102. {
  8103. if (ctx->p_code_compiled) {
  8104. ctx->p_code_compiled -= size;
  8105. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8106. if (size == sizeof(uint8)) {
  8107. ctx->p_code_compiled--;
  8108. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8109. }
  8110. #endif
  8111. }
  8112. else {
  8113. ctx->code_compiled_size -= size;
  8114. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8115. if (size == sizeof(uint8)) {
  8116. ctx->code_compiled_size--;
  8117. bh_assert((ctx->code_compiled_size & 1) == 0);
  8118. }
  8119. #endif
  8120. }
  8121. }
  8122. static bool
  8123. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8124. uint32 local_index, uint32 local_type,
  8125. bool *preserved, char *error_buf,
  8126. uint32 error_buf_size)
  8127. {
  8128. uint32 i = 0;
  8129. int16 preserved_offset = (int16)local_index;
  8130. *preserved = false;
  8131. while (i < loader_ctx->stack_cell_num) {
  8132. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8133. /* move previous local into dynamic space before a set/tee_local opcode
  8134. */
  8135. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8136. if (!(*preserved)) {
  8137. *preserved = true;
  8138. skip_label();
  8139. preserved_offset = loader_ctx->preserved_local_offset;
  8140. if (loader_ctx->p_code_compiled) {
  8141. bh_assert(preserved_offset != (int16)local_index);
  8142. }
  8143. if (is_32bit_type(local_type)) {
  8144. /* Only increase preserve offset in the second traversal */
  8145. if (loader_ctx->p_code_compiled)
  8146. loader_ctx->preserved_local_offset++;
  8147. emit_label(EXT_OP_COPY_STACK_TOP);
  8148. }
  8149. else {
  8150. if (loader_ctx->p_code_compiled)
  8151. loader_ctx->preserved_local_offset += 2;
  8152. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8153. }
  8154. emit_operand(loader_ctx, local_index);
  8155. emit_operand(loader_ctx, preserved_offset);
  8156. emit_label(opcode);
  8157. }
  8158. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8159. }
  8160. if (is_32bit_type(cur_type))
  8161. i++;
  8162. else
  8163. i += 2;
  8164. }
  8165. (void)error_buf;
  8166. (void)error_buf_size;
  8167. return true;
  8168. }
  8169. static bool
  8170. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8171. char *error_buf, uint32 error_buf_size)
  8172. {
  8173. uint32 i = 0;
  8174. bool preserve_local;
  8175. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8176. blocks will not influence the value of these locals */
  8177. while (i < loader_ctx->stack_cell_num) {
  8178. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8179. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8180. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8181. && (cur_offset >= 0)) {
  8182. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8183. cur_type, &preserve_local,
  8184. error_buf, error_buf_size)))
  8185. return false;
  8186. }
  8187. if (is_32bit_type(cur_type)) {
  8188. i++;
  8189. }
  8190. else {
  8191. i += 2;
  8192. }
  8193. }
  8194. return true;
  8195. }
  8196. static bool
  8197. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8198. uint8 *p_code_compiled, char *error_buf,
  8199. uint32 error_buf_size)
  8200. {
  8201. BranchBlockPatch *patch =
  8202. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8203. if (!patch) {
  8204. return false;
  8205. }
  8206. patch->patch_type = patch_type;
  8207. patch->code_compiled = p_code_compiled;
  8208. if (!frame_csp->patch_list) {
  8209. frame_csp->patch_list = patch;
  8210. patch->next = NULL;
  8211. }
  8212. else {
  8213. patch->next = frame_csp->patch_list;
  8214. frame_csp->patch_list = patch;
  8215. }
  8216. return true;
  8217. }
  8218. static void
  8219. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8220. {
  8221. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8222. BranchBlockPatch *node = frame_csp->patch_list;
  8223. BranchBlockPatch *node_prev = NULL, *node_next;
  8224. if (!ctx->p_code_compiled)
  8225. return;
  8226. while (node) {
  8227. node_next = node->next;
  8228. if (node->patch_type == patch_type) {
  8229. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8230. if (node_prev == NULL) {
  8231. frame_csp->patch_list = node_next;
  8232. }
  8233. else {
  8234. node_prev->next = node_next;
  8235. }
  8236. wasm_runtime_free(node);
  8237. }
  8238. else {
  8239. node_prev = node;
  8240. }
  8241. node = node_next;
  8242. }
  8243. }
  8244. static bool
  8245. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8246. bool is_br, char *error_buf, uint32 error_buf_size)
  8247. {
  8248. /* br info layout:
  8249. * a) arity of target block
  8250. * b) total cell num of arity values
  8251. * c) each arity value's cell num
  8252. * d) each arity value's src frame offset
  8253. * e) each arity values's dst dynamic offset
  8254. * f) branch target address
  8255. *
  8256. * Note: b-e are omitted when arity is 0 so that
  8257. * interpreter can recover the br info quickly.
  8258. */
  8259. BlockType *block_type = &frame_csp->block_type;
  8260. uint8 *types = NULL, cell;
  8261. #if WASM_ENABLE_GC != 0
  8262. WASMRefTypeMap *reftype_maps;
  8263. uint32 reftype_map_count;
  8264. #endif
  8265. uint32 arity = 0;
  8266. int32 i;
  8267. int16 *frame_offset = ctx->frame_offset;
  8268. uint16 dynamic_offset;
  8269. /* Note: loop's arity is different from if and block. loop's arity is
  8270. * its parameter count while if and block arity is result count.
  8271. */
  8272. #if WASM_ENABLE_GC == 0
  8273. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8274. arity = block_type_get_param_types(block_type, &types);
  8275. else
  8276. arity = block_type_get_result_types(block_type, &types);
  8277. #else
  8278. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8279. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8280. &reftype_map_count);
  8281. else
  8282. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8283. &reftype_map_count);
  8284. #endif
  8285. /* Part a */
  8286. emit_uint32(ctx, arity);
  8287. if (arity) {
  8288. /* Part b */
  8289. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8290. /* Part c */
  8291. for (i = (int32)arity - 1; i >= 0; i--) {
  8292. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8293. emit_byte(ctx, cell);
  8294. }
  8295. /* Part d */
  8296. for (i = (int32)arity - 1; i >= 0; i--) {
  8297. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8298. frame_offset -= cell;
  8299. emit_operand(ctx, *(int16 *)(frame_offset));
  8300. }
  8301. /* Part e */
  8302. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8303. /* Use start_dynamic_offset which was set in
  8304. copy_params_to_dynamic_space */
  8305. dynamic_offset = frame_csp->start_dynamic_offset
  8306. + wasm_get_cell_num(types, arity);
  8307. else
  8308. dynamic_offset =
  8309. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8310. if (is_br)
  8311. ctx->dynamic_offset = dynamic_offset;
  8312. for (i = (int32)arity - 1; i >= 0; i--) {
  8313. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8314. dynamic_offset -= cell;
  8315. emit_operand(ctx, dynamic_offset);
  8316. }
  8317. }
  8318. /* Part f */
  8319. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8320. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8321. }
  8322. else {
  8323. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8324. error_buf, error_buf_size))
  8325. return false;
  8326. /* label address, to be patched */
  8327. wasm_loader_emit_ptr(ctx, NULL);
  8328. }
  8329. return true;
  8330. }
  8331. static bool
  8332. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8333. bool disable_emit, int16 operand_offset,
  8334. char *error_buf, uint32 error_buf_size)
  8335. {
  8336. uint32 cell_num_to_push, i;
  8337. if (type == VALUE_TYPE_VOID)
  8338. return true;
  8339. /* only check memory overflow in first traverse */
  8340. if (ctx->p_code_compiled == NULL) {
  8341. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8342. return false;
  8343. }
  8344. if (disable_emit)
  8345. *(ctx->frame_offset)++ = operand_offset;
  8346. else {
  8347. emit_operand(ctx, ctx->dynamic_offset);
  8348. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8349. ctx->dynamic_offset++;
  8350. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8351. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8352. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8353. goto fail;
  8354. }
  8355. }
  8356. }
  8357. if (is_32bit_type(type))
  8358. return true;
  8359. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8360. for (i = 0; i < cell_num_to_push; i++) {
  8361. if (ctx->p_code_compiled == NULL) {
  8362. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8363. return false;
  8364. }
  8365. ctx->frame_offset++;
  8366. if (!disable_emit) {
  8367. ctx->dynamic_offset++;
  8368. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8369. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8370. if (ctx->max_dynamic_offset >= INT16_MAX)
  8371. goto fail;
  8372. }
  8373. }
  8374. }
  8375. return true;
  8376. fail:
  8377. set_error_buf(error_buf, error_buf_size,
  8378. "fast interpreter offset overflow");
  8379. return false;
  8380. }
  8381. /* This function should be in front of wasm_loader_pop_frame_ref
  8382. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8383. will be modified by wasm_loader_pop_frame_ref */
  8384. static bool
  8385. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8386. char *error_buf, uint32 error_buf_size)
  8387. {
  8388. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8389. then current block is the function block */
  8390. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8391. BranchBlock *cur_block = ctx->frame_csp - depth;
  8392. int32 available_stack_cell =
  8393. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8394. uint32 cell_num_to_pop;
  8395. /* Directly return success if current block is in stack
  8396. polymorphic state while stack is empty. */
  8397. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8398. return true;
  8399. if (type == VALUE_TYPE_VOID)
  8400. return true;
  8401. /* Change type to ANY when the stack top is ANY, so as to avoid
  8402. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8403. two offsets */
  8404. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8405. type = VALUE_TYPE_ANY;
  8406. cell_num_to_pop = wasm_value_type_cell_num(type);
  8407. /* Check the offset stack bottom to ensure the frame offset
  8408. stack will not go underflow. But we don't thrown error
  8409. and return true here, because the error msg should be
  8410. given in wasm_loader_pop_frame_ref */
  8411. if (!check_offset_pop(ctx, cell_num_to_pop))
  8412. return true;
  8413. ctx->frame_offset -= cell_num_to_pop;
  8414. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8415. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8416. ctx->dynamic_offset -= cell_num_to_pop;
  8417. emit_operand(ctx, *(ctx->frame_offset));
  8418. (void)error_buf;
  8419. (void)error_buf_size;
  8420. return true;
  8421. }
  8422. static bool
  8423. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8424. bool disable_emit, int16 operand_offset,
  8425. char *error_buf, uint32 error_buf_size)
  8426. {
  8427. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8428. error_buf, error_buf_size)))
  8429. return false;
  8430. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8431. return false;
  8432. return true;
  8433. }
  8434. static bool
  8435. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8436. char *error_buf, uint32 error_buf_size)
  8437. {
  8438. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8439. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8440. return false;
  8441. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8442. return false;
  8443. return true;
  8444. }
  8445. static bool
  8446. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8447. uint8 type_push, uint8 type_pop,
  8448. bool disable_emit, int16 operand_offset,
  8449. char *error_buf, uint32 error_buf_size)
  8450. {
  8451. uint8 i;
  8452. for (i = 0; i < pop_cnt; i++) {
  8453. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8454. error_buf_size))
  8455. return false;
  8456. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8457. error_buf_size))
  8458. return false;
  8459. }
  8460. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8461. operand_offset, error_buf,
  8462. error_buf_size))
  8463. return false;
  8464. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8465. return false;
  8466. return true;
  8467. }
  8468. static bool
  8469. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8470. int16 *offset, char *error_buf,
  8471. uint32 error_buf_size)
  8472. {
  8473. int8 bytes_to_increase;
  8474. int16 operand_offset = 0;
  8475. Const *c;
  8476. /* Search existing constant */
  8477. for (c = (Const *)ctx->const_buf;
  8478. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8479. /* TODO: handle v128 type? */
  8480. if ((type == c->value_type)
  8481. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8482. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8483. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8484. || (type == VALUE_TYPE_FUNCREF
  8485. && *(int32 *)value == c->value.i32)
  8486. || (type == VALUE_TYPE_EXTERNREF
  8487. && *(int32 *)value == c->value.i32)
  8488. #endif
  8489. || (type == VALUE_TYPE_F64
  8490. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8491. || (type == VALUE_TYPE_F32
  8492. && (0
  8493. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8494. operand_offset = c->slot_index;
  8495. break;
  8496. }
  8497. if (is_32bit_type(c->value_type))
  8498. operand_offset += 1;
  8499. else
  8500. operand_offset += 2;
  8501. }
  8502. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8503. /* New constant, append to the const buffer */
  8504. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8505. bytes_to_increase = 2;
  8506. }
  8507. else {
  8508. bytes_to_increase = 1;
  8509. }
  8510. /* The max cell num of const buffer is 32768 since the valid index range
  8511. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8512. * full */
  8513. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8514. *offset = 0;
  8515. return true;
  8516. }
  8517. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8518. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8519. ctx->const_buf_size + 4 * sizeof(Const));
  8520. ctx->const_buf_size += 4 * sizeof(Const);
  8521. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8522. }
  8523. c->value_type = type;
  8524. switch (type) {
  8525. case VALUE_TYPE_F64:
  8526. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8527. sizeof(float64));
  8528. ctx->const_cell_num += 2;
  8529. /* The const buf will be reversed, we use the second cell */
  8530. /* of the i64/f64 const so the final offset is correct */
  8531. operand_offset++;
  8532. break;
  8533. case VALUE_TYPE_I64:
  8534. c->value.i64 = *(int64 *)value;
  8535. ctx->const_cell_num += 2;
  8536. operand_offset++;
  8537. break;
  8538. case VALUE_TYPE_F32:
  8539. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8540. sizeof(float32));
  8541. ctx->const_cell_num++;
  8542. break;
  8543. case VALUE_TYPE_I32:
  8544. c->value.i32 = *(int32 *)value;
  8545. ctx->const_cell_num++;
  8546. break;
  8547. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8548. case VALUE_TYPE_EXTERNREF:
  8549. case VALUE_TYPE_FUNCREF:
  8550. c->value.i32 = *(int32 *)value;
  8551. ctx->const_cell_num++;
  8552. break;
  8553. #endif
  8554. default:
  8555. break;
  8556. }
  8557. c->slot_index = operand_offset;
  8558. ctx->num_const++;
  8559. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8560. (int64)c->value.i64);
  8561. }
  8562. /* use negative index for const */
  8563. operand_offset = -(operand_offset + 1);
  8564. *offset = operand_offset;
  8565. return true;
  8566. fail:
  8567. return false;
  8568. }
  8569. /*
  8570. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8571. -- Mostly used for the binary / compare operation
  8572. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8573. -- Mostly used in block / control instructions
  8574. The POP will always emit the offset on the top of the frame_offset stack
  8575. PUSH can be used in two ways:
  8576. 1. directly PUSH:
  8577. PUSH_XXX();
  8578. will allocate a dynamic space and emit
  8579. 2. silent PUSH:
  8580. operand_offset = xxx; disable_emit = true;
  8581. PUSH_XXX();
  8582. only push the frame_offset stack, no emit
  8583. */
  8584. #define TEMPLATE_PUSH(Type) \
  8585. do { \
  8586. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8587. disable_emit, operand_offset, \
  8588. error_buf, error_buf_size)) \
  8589. goto fail; \
  8590. } while (0)
  8591. #define TEMPLATE_PUSH_REF(Type) \
  8592. do { \
  8593. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8594. operand_offset, error_buf, \
  8595. error_buf_size)) \
  8596. goto fail; \
  8597. } while (0)
  8598. #define TEMPLATE_POP(Type) \
  8599. do { \
  8600. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8601. error_buf, error_buf_size)) \
  8602. goto fail; \
  8603. } while (0)
  8604. #define TEMPLATE_POP_REF(Type) \
  8605. do { \
  8606. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8607. error_buf_size)) \
  8608. goto fail; \
  8609. } while (0)
  8610. #define PUSH_OFFSET_TYPE(type) \
  8611. do { \
  8612. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8613. operand_offset, error_buf, \
  8614. error_buf_size))) \
  8615. goto fail; \
  8616. } while (0)
  8617. #define POP_OFFSET_TYPE(type) \
  8618. do { \
  8619. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8620. error_buf_size))) \
  8621. goto fail; \
  8622. } while (0)
  8623. #define POP_AND_PUSH(type_pop, type_push) \
  8624. do { \
  8625. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8626. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8627. operand_offset, error_buf, error_buf_size))) \
  8628. goto fail; \
  8629. } while (0)
  8630. /* type of POPs should be the same */
  8631. #define POP2_AND_PUSH(type_pop, type_push) \
  8632. do { \
  8633. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8634. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8635. operand_offset, error_buf, error_buf_size))) \
  8636. goto fail; \
  8637. } while (0)
  8638. #else /* WASM_ENABLE_FAST_INTERP */
  8639. #define TEMPLATE_PUSH(Type) \
  8640. do { \
  8641. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8642. error_buf, error_buf_size))) \
  8643. goto fail; \
  8644. } while (0)
  8645. #define TEMPLATE_PUSH_REF(Type) \
  8646. do { \
  8647. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8648. error_buf_size))) \
  8649. goto fail; \
  8650. } while (0)
  8651. #define TEMPLATE_POP(Type) \
  8652. do { \
  8653. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8654. error_buf, error_buf_size))) \
  8655. goto fail; \
  8656. } while (0)
  8657. #define TEMPLATE_POP_REF(Type) \
  8658. do { \
  8659. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8660. error_buf_size))) \
  8661. goto fail; \
  8662. } while (0)
  8663. #define POP_AND_PUSH(type_pop, type_push) \
  8664. do { \
  8665. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8666. type_pop, error_buf, \
  8667. error_buf_size))) \
  8668. goto fail; \
  8669. } while (0)
  8670. /* type of POPs should be the same */
  8671. #define POP2_AND_PUSH(type_pop, type_push) \
  8672. do { \
  8673. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8674. type_pop, error_buf, \
  8675. error_buf_size))) \
  8676. goto fail; \
  8677. } while (0)
  8678. #endif /* WASM_ENABLE_FAST_INTERP */
  8679. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8680. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8681. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8682. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8683. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8684. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8685. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8686. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8687. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8688. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8689. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8690. #define PUSH_TBL_ELEM_IDX() TEMPLATE_PUSH_REF(table_elem_idx_type)
  8691. #define POP_I32() TEMPLATE_POP(I32)
  8692. #define POP_F32() TEMPLATE_POP(F32)
  8693. #define POP_I64() TEMPLATE_POP(I64)
  8694. #define POP_F64() TEMPLATE_POP(F64)
  8695. #define POP_V128() TEMPLATE_POP(V128)
  8696. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8697. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8698. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8699. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8700. #define POP_TBL_ELEM_IDX() TEMPLATE_POP_REF(table_elem_idx_type)
  8701. #if WASM_ENABLE_FAST_INTERP != 0
  8702. static bool
  8703. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8704. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8705. {
  8706. int16 operand_offset = 0;
  8707. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8708. : loader_ctx->frame_csp;
  8709. BlockType *block_type = &block->block_type;
  8710. uint8 *return_types = NULL;
  8711. #if WASM_ENABLE_GC != 0
  8712. WASMRefTypeMap *reftype_maps = NULL;
  8713. uint32 reftype_map_count;
  8714. #endif
  8715. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8716. int32 i = 0;
  8717. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8718. *frame_offset_org = NULL;
  8719. #if WASM_ENABLE_GC == 0
  8720. return_count = block_type_get_result_types(block_type, &return_types);
  8721. #else
  8722. return_count = block_type_get_result_types(
  8723. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8724. #endif
  8725. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8726. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8727. if (return_count == 1) {
  8728. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8729. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8730. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8731. /* insert op_copy before else opcode */
  8732. if (opcode == WASM_OP_ELSE)
  8733. skip_label();
  8734. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8735. : EXT_OP_COPY_STACK_TOP_I64);
  8736. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8737. emit_operand(loader_ctx, block->dynamic_offset);
  8738. if (opcode == WASM_OP_ELSE) {
  8739. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8740. }
  8741. else {
  8742. loader_ctx->frame_offset -= cell;
  8743. loader_ctx->dynamic_offset = block->dynamic_offset;
  8744. PUSH_OFFSET_TYPE(return_types[0]);
  8745. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8746. }
  8747. if (opcode == WASM_OP_ELSE)
  8748. emit_label(opcode);
  8749. }
  8750. return true;
  8751. }
  8752. /* Copy stack top values to block's results which are in dynamic space.
  8753. * The instruction format:
  8754. * Part a: values count
  8755. * Part b: all values total cell num
  8756. * Part c: each value's cell_num, src offset and dst offset
  8757. * Part d: each value's src offset and dst offset
  8758. * Part e: each value's dst offset
  8759. */
  8760. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8761. dynamic_offset = dynamic_offset_org =
  8762. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8763. /* First traversal to get the count of values needed to be copied. */
  8764. for (i = (int32)return_count - 1; i >= 0; i--) {
  8765. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8766. frame_offset -= cells;
  8767. dynamic_offset -= cells;
  8768. if (dynamic_offset != *frame_offset) {
  8769. value_count++;
  8770. total_cel_num += cells;
  8771. }
  8772. }
  8773. if (value_count) {
  8774. uint32 j = 0;
  8775. uint8 *emit_data = NULL, *cells = NULL;
  8776. int16 *src_offsets = NULL;
  8777. uint16 *dst_offsets = NULL;
  8778. uint64 size =
  8779. (uint64)value_count
  8780. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8781. /* Allocate memory for the emit data */
  8782. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8783. return false;
  8784. cells = emit_data;
  8785. src_offsets = (int16 *)(cells + value_count);
  8786. dst_offsets = (uint16 *)(src_offsets + value_count);
  8787. /* insert op_copy before else opcode */
  8788. if (opcode == WASM_OP_ELSE)
  8789. skip_label();
  8790. emit_label(EXT_OP_COPY_STACK_VALUES);
  8791. /* Part a) */
  8792. emit_uint32(loader_ctx, value_count);
  8793. /* Part b) */
  8794. emit_uint32(loader_ctx, total_cel_num);
  8795. /* Second traversal to get each value's cell num, src offset and dst
  8796. * offset. */
  8797. frame_offset = frame_offset_org;
  8798. dynamic_offset = dynamic_offset_org;
  8799. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8800. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8801. frame_offset -= cell;
  8802. dynamic_offset -= cell;
  8803. if (dynamic_offset != *frame_offset) {
  8804. /* cell num */
  8805. cells[j] = cell;
  8806. /* src offset */
  8807. src_offsets[j] = *frame_offset;
  8808. /* dst offset */
  8809. dst_offsets[j] = dynamic_offset;
  8810. j++;
  8811. }
  8812. if (opcode == WASM_OP_ELSE) {
  8813. *frame_offset = dynamic_offset;
  8814. }
  8815. else {
  8816. loader_ctx->frame_offset = frame_offset;
  8817. loader_ctx->dynamic_offset = dynamic_offset;
  8818. PUSH_OFFSET_TYPE(return_types[i]);
  8819. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8820. loader_ctx->frame_offset = frame_offset_org;
  8821. loader_ctx->dynamic_offset = dynamic_offset_org;
  8822. }
  8823. }
  8824. bh_assert(j == value_count);
  8825. /* Emit the cells, src_offsets and dst_offsets */
  8826. for (j = 0; j < value_count; j++)
  8827. emit_byte(loader_ctx, cells[j]);
  8828. for (j = 0; j < value_count; j++)
  8829. emit_operand(loader_ctx, src_offsets[j]);
  8830. for (j = 0; j < value_count; j++)
  8831. emit_operand(loader_ctx, dst_offsets[j]);
  8832. if (opcode == WASM_OP_ELSE)
  8833. emit_label(opcode);
  8834. wasm_runtime_free(emit_data);
  8835. }
  8836. return true;
  8837. fail:
  8838. return false;
  8839. }
  8840. #endif /* WASM_ENABLE_FAST_INTERP */
  8841. #define RESERVE_BLOCK_RET() \
  8842. do { \
  8843. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  8844. error_buf_size)) \
  8845. goto fail; \
  8846. } while (0)
  8847. #define PUSH_TYPE(type) \
  8848. do { \
  8849. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8850. error_buf_size))) \
  8851. goto fail; \
  8852. } while (0)
  8853. #define POP_TYPE(type) \
  8854. do { \
  8855. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8856. error_buf_size))) \
  8857. goto fail; \
  8858. } while (0)
  8859. #if WASM_ENABLE_GC == 0
  8860. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8861. do { \
  8862. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8863. _start_addr, error_buf, \
  8864. error_buf_size)) \
  8865. goto fail; \
  8866. } while (0)
  8867. #define POP_CSP() \
  8868. do { \
  8869. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8870. goto fail; \
  8871. } while (0)
  8872. #else
  8873. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8874. do { \
  8875. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8876. _start_addr, error_buf, \
  8877. error_buf_size)) \
  8878. goto fail; \
  8879. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8880. error_buf, error_buf_size)) { \
  8881. goto fail; \
  8882. } \
  8883. } while (0)
  8884. #define POP_CSP() \
  8885. do { \
  8886. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8887. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8888. goto fail; \
  8889. } while (0)
  8890. #endif /* end of WASM_ENABLE_GC == 0 */
  8891. #if WASM_ENABLE_GC == 0
  8892. #define GET_LOCAL_REFTYPE() (void)0
  8893. #else
  8894. #define GET_LOCAL_REFTYPE() \
  8895. do { \
  8896. if (wasm_is_type_multi_byte_type(local_type)) { \
  8897. WASMRefType *_ref_type; \
  8898. if (local_idx < param_count) \
  8899. _ref_type = wasm_reftype_map_find( \
  8900. param_reftype_maps, param_reftype_map_count, local_idx); \
  8901. else \
  8902. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8903. local_reftype_map_count, \
  8904. local_idx - param_count); \
  8905. bh_assert(_ref_type); \
  8906. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8907. wasm_reftype_struct_size(_ref_type)); \
  8908. } \
  8909. } while (0)
  8910. #endif /* end of WASM_ENABLE_GC == 0 */
  8911. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8912. do { \
  8913. read_leb_uint32(p, p_end, local_idx); \
  8914. if (local_idx >= param_count + local_count) { \
  8915. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8916. goto fail; \
  8917. } \
  8918. local_type = local_idx < param_count \
  8919. ? param_types[local_idx] \
  8920. : local_types[local_idx - param_count]; \
  8921. local_offset = local_offsets[local_idx]; \
  8922. GET_LOCAL_REFTYPE(); \
  8923. } while (0)
  8924. static bool
  8925. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8926. {
  8927. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8928. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8929. return false;
  8930. }
  8931. return true;
  8932. }
  8933. #define CHECK_MEMORY() \
  8934. do { \
  8935. if (!check_memory(module, error_buf, error_buf_size)) \
  8936. goto fail; \
  8937. } while (0)
  8938. static bool
  8939. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8940. uint32 error_buf_size)
  8941. {
  8942. uint8 mem_access_aligns[] = {
  8943. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8944. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8945. };
  8946. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8947. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8948. set_error_buf(error_buf, error_buf_size,
  8949. "alignment must not be larger than natural");
  8950. return false;
  8951. }
  8952. return true;
  8953. }
  8954. #if WASM_ENABLE_SIMD != 0
  8955. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8956. static bool
  8957. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8958. uint32 error_buf_size)
  8959. {
  8960. uint8 mem_access_aligns[] = {
  8961. 4, /* load */
  8962. 3, 3, 3, 3, 3, 3, /* load and extend */
  8963. 0, 1, 2, 3, /* load and splat */
  8964. 4, /* store */
  8965. };
  8966. uint8 mem_access_aligns_load_lane[] = {
  8967. 0, 1, 2, 3, /* load lane */
  8968. 0, 1, 2, 3, /* store lane */
  8969. 2, 3 /* store zero */
  8970. };
  8971. if (!((opcode <= SIMD_v128_store)
  8972. || (SIMD_v128_load8_lane <= opcode
  8973. && opcode <= SIMD_v128_load64_zero))) {
  8974. set_error_buf(error_buf, error_buf_size,
  8975. "the opcode doesn't include memarg");
  8976. return false;
  8977. }
  8978. if ((opcode <= SIMD_v128_store
  8979. && align > mem_access_aligns[opcode - SIMD_v128_load])
  8980. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  8981. && align > mem_access_aligns_load_lane[opcode
  8982. - SIMD_v128_load8_lane])) {
  8983. set_error_buf(error_buf, error_buf_size,
  8984. "alignment must not be larger than natural");
  8985. return false;
  8986. }
  8987. return true;
  8988. }
  8989. static bool
  8990. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  8991. uint32 error_buf_size)
  8992. {
  8993. switch (opcode) {
  8994. case SIMD_i8x16_extract_lane_s:
  8995. case SIMD_i8x16_extract_lane_u:
  8996. case SIMD_i8x16_replace_lane:
  8997. if (lane >= 16) {
  8998. goto fail;
  8999. }
  9000. break;
  9001. case SIMD_i16x8_extract_lane_s:
  9002. case SIMD_i16x8_extract_lane_u:
  9003. case SIMD_i16x8_replace_lane:
  9004. if (lane >= 8) {
  9005. goto fail;
  9006. }
  9007. break;
  9008. case SIMD_i32x4_extract_lane:
  9009. case SIMD_i32x4_replace_lane:
  9010. case SIMD_f32x4_extract_lane:
  9011. case SIMD_f32x4_replace_lane:
  9012. if (lane >= 4) {
  9013. goto fail;
  9014. }
  9015. break;
  9016. case SIMD_i64x2_extract_lane:
  9017. case SIMD_i64x2_replace_lane:
  9018. case SIMD_f64x2_extract_lane:
  9019. case SIMD_f64x2_replace_lane:
  9020. if (lane >= 2) {
  9021. goto fail;
  9022. }
  9023. break;
  9024. case SIMD_v128_load8_lane:
  9025. case SIMD_v128_load16_lane:
  9026. case SIMD_v128_load32_lane:
  9027. case SIMD_v128_load64_lane:
  9028. case SIMD_v128_store8_lane:
  9029. case SIMD_v128_store16_lane:
  9030. case SIMD_v128_store32_lane:
  9031. case SIMD_v128_store64_lane:
  9032. case SIMD_v128_load32_zero:
  9033. case SIMD_v128_load64_zero:
  9034. {
  9035. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9036. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9037. goto fail;
  9038. }
  9039. break;
  9040. }
  9041. default:
  9042. goto fail;
  9043. }
  9044. return true;
  9045. fail:
  9046. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9047. return false;
  9048. }
  9049. static bool
  9050. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9051. {
  9052. uint8 i;
  9053. for (i = 0; i != 16; ++i) {
  9054. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9055. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9056. return false;
  9057. }
  9058. }
  9059. return true;
  9060. }
  9061. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9062. #endif /* end of WASM_ENABLE_SIMD */
  9063. #if WASM_ENABLE_SHARED_MEMORY != 0
  9064. static bool
  9065. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9066. uint32 error_buf_size)
  9067. {
  9068. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9069. uint8 mem_access_aligns[] = {
  9070. 2, 3, 0, 1, 0, 1, 2,
  9071. };
  9072. uint8 expect;
  9073. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9074. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9075. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9076. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9077. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9078. }
  9079. else {
  9080. /* 7 opcodes in every group */
  9081. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9082. }
  9083. if (align != expect) {
  9084. set_error_buf(error_buf, error_buf_size,
  9085. "alignment isn't equal to natural");
  9086. return false;
  9087. }
  9088. return true;
  9089. }
  9090. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9091. static bool
  9092. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9093. char *error_buf, uint32 error_buf_size)
  9094. {
  9095. BranchBlock *target_block, *cur_block;
  9096. BlockType *target_block_type;
  9097. uint8 type, *types = NULL, *frame_ref;
  9098. uint32 arity = 0;
  9099. int32 i, available_stack_cell;
  9100. uint16 cell_num;
  9101. #if WASM_ENABLE_GC != 0
  9102. WASMRefTypeMap *frame_reftype_map;
  9103. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9104. WASMRefType *ref_type;
  9105. uint32 reftype_map_count = 0;
  9106. int32 available_reftype_map;
  9107. bool is_type_multi_byte;
  9108. #endif
  9109. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9110. uint8 *frame_ref_after_popped = NULL;
  9111. uint8 frame_ref_tmp[4] = { 0 };
  9112. uint8 *frame_ref_buf = frame_ref_tmp;
  9113. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9114. #if WASM_ENABLE_GC != 0
  9115. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9116. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9117. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9118. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9119. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9120. #endif
  9121. #if WASM_ENABLE_FAST_INTERP != 0
  9122. int16 *frame_offset_old = loader_ctx->frame_offset;
  9123. int16 *frame_offset_after_popped = NULL;
  9124. int16 frame_offset_tmp[4] = { 0 };
  9125. int16 *frame_offset_buf = frame_offset_tmp;
  9126. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9127. #endif
  9128. bool ret = false;
  9129. bh_assert(loader_ctx->csp_num > 0);
  9130. if (loader_ctx->csp_num - 1 < depth) {
  9131. set_error_buf(error_buf, error_buf_size,
  9132. "unknown label, "
  9133. "unexpected end of section or function");
  9134. return false;
  9135. }
  9136. cur_block = loader_ctx->frame_csp - 1;
  9137. target_block = loader_ctx->frame_csp - (depth + 1);
  9138. target_block_type = &target_block->block_type;
  9139. frame_ref = loader_ctx->frame_ref;
  9140. #if WASM_ENABLE_GC != 0
  9141. frame_reftype_map = loader_ctx->frame_reftype_map;
  9142. #endif
  9143. /* Note: loop's arity is different from if and block. loop's arity is
  9144. * its parameter count while if and block arity is result count.
  9145. */
  9146. #if WASM_ENABLE_GC == 0
  9147. if (target_block->label_type == LABEL_TYPE_LOOP)
  9148. arity = block_type_get_param_types(target_block_type, &types);
  9149. else
  9150. arity = block_type_get_result_types(target_block_type, &types);
  9151. #else
  9152. if (target_block->label_type == LABEL_TYPE_LOOP)
  9153. arity = block_type_get_param_types(target_block_type, &types,
  9154. &reftype_maps, &reftype_map_count);
  9155. else
  9156. arity = block_type_get_result_types(target_block_type, &types,
  9157. &reftype_maps, &reftype_map_count);
  9158. #endif
  9159. /* If the stack is in polymorphic state, just clear the stack
  9160. * and then re-push the values to make the stack top values
  9161. * match block type. */
  9162. if (cur_block->is_stack_polymorphic) {
  9163. #if WASM_ENABLE_GC != 0
  9164. int32 j = reftype_map_count - 1;
  9165. #endif
  9166. for (i = (int32)arity - 1; i >= 0; i--) {
  9167. #if WASM_ENABLE_GC != 0
  9168. if (wasm_is_type_multi_byte_type(types[i])) {
  9169. bh_assert(reftype_maps[j].index == i);
  9170. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9171. reftype_maps[j].ref_type,
  9172. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9173. j--;
  9174. }
  9175. #endif
  9176. #if WASM_ENABLE_FAST_INTERP != 0
  9177. POP_OFFSET_TYPE(types[i]);
  9178. #endif
  9179. POP_TYPE(types[i]);
  9180. }
  9181. /* Backup stack data since it may be changed in the below
  9182. push operations, and the stack data may be used when
  9183. checking other target blocks of opcode br_table */
  9184. if (opcode == WASM_OP_BR_TABLE) {
  9185. uint64 total_size;
  9186. frame_ref_after_popped = loader_ctx->frame_ref;
  9187. total_size = (uint64)sizeof(uint8)
  9188. * (frame_ref_old - frame_ref_after_popped);
  9189. if (total_size > sizeof(frame_ref_tmp)
  9190. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9191. error_buf_size))) {
  9192. goto fail;
  9193. }
  9194. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9195. frame_ref_after_popped, (uint32)total_size);
  9196. #if WASM_ENABLE_GC != 0
  9197. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9198. total_size =
  9199. (uint64)sizeof(WASMRefTypeMap)
  9200. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9201. if (total_size > sizeof(frame_reftype_map_tmp)
  9202. && !(frame_reftype_map_buf = loader_malloc(
  9203. total_size, error_buf, error_buf_size))) {
  9204. goto fail;
  9205. }
  9206. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9207. frame_reftype_map_after_popped, (uint32)total_size);
  9208. #endif
  9209. #if WASM_ENABLE_FAST_INTERP != 0
  9210. frame_offset_after_popped = loader_ctx->frame_offset;
  9211. total_size = (uint64)sizeof(int16)
  9212. * (frame_offset_old - frame_offset_after_popped);
  9213. if (total_size > sizeof(frame_offset_tmp)
  9214. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9215. error_buf_size))) {
  9216. goto fail;
  9217. }
  9218. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9219. frame_offset_after_popped, (uint32)total_size);
  9220. #endif
  9221. }
  9222. #if WASM_ENABLE_GC != 0
  9223. j = 0;
  9224. #endif
  9225. for (i = 0; i < (int32)arity; i++) {
  9226. #if WASM_ENABLE_GC != 0
  9227. if (wasm_is_type_multi_byte_type(types[i])) {
  9228. bh_assert(reftype_maps[j].index == i);
  9229. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9230. reftype_maps[j].ref_type,
  9231. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9232. j++;
  9233. }
  9234. #endif
  9235. #if WASM_ENABLE_FAST_INTERP != 0
  9236. bool disable_emit = true;
  9237. int16 operand_offset = 0;
  9238. PUSH_OFFSET_TYPE(types[i]);
  9239. #endif
  9240. PUSH_TYPE(types[i]);
  9241. }
  9242. #if WASM_ENABLE_FAST_INTERP != 0
  9243. emit_br_info(target_block, opcode == WASM_OP_BR);
  9244. #endif
  9245. /* Restore the stack data, note that frame_ref_bottom,
  9246. frame_reftype_map_bottom, frame_offset_bottom may be
  9247. re-allocated in the above push operations */
  9248. if (opcode == WASM_OP_BR_TABLE) {
  9249. uint32 total_size;
  9250. /* The stack operand num should not be smaller than before
  9251. after pop and push operations */
  9252. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9253. loader_ctx->stack_cell_num = stack_cell_num_old;
  9254. loader_ctx->frame_ref =
  9255. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9256. total_size = (uint32)(sizeof(uint8)
  9257. * (frame_ref_old - frame_ref_after_popped));
  9258. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9259. frame_ref_buf, total_size);
  9260. #if WASM_ENABLE_GC != 0
  9261. /* The stack operand num should not be smaller than before
  9262. after pop and push operations */
  9263. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9264. loader_ctx->reftype_map_num = reftype_map_num_old;
  9265. loader_ctx->frame_reftype_map =
  9266. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9267. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9268. * (frame_reftype_map_old
  9269. - frame_reftype_map_after_popped));
  9270. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9271. total_size, frame_reftype_map_buf, total_size);
  9272. #endif
  9273. #if WASM_ENABLE_FAST_INTERP != 0
  9274. loader_ctx->frame_offset =
  9275. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9276. total_size =
  9277. (uint32)(sizeof(int16)
  9278. * (frame_offset_old - frame_offset_after_popped));
  9279. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9280. total_size, frame_offset_buf, total_size);
  9281. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9282. #endif
  9283. }
  9284. ret = true;
  9285. goto cleanup_and_return;
  9286. }
  9287. available_stack_cell =
  9288. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9289. #if WASM_ENABLE_GC != 0
  9290. available_reftype_map =
  9291. (int32)(loader_ctx->reftype_map_num
  9292. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9293. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9294. #endif
  9295. /* Check stack top values match target block type */
  9296. for (i = (int32)arity - 1; i >= 0; i--) {
  9297. type = types[i];
  9298. #if WASM_ENABLE_GC != 0
  9299. ref_type = NULL;
  9300. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9301. if (is_type_multi_byte) {
  9302. bh_assert(reftype_map);
  9303. ref_type = reftype_map->ref_type;
  9304. }
  9305. #endif
  9306. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9307. break;
  9308. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9309. #if WASM_ENABLE_GC != 0
  9310. frame_reftype_map, available_reftype_map,
  9311. #endif
  9312. type,
  9313. #if WASM_ENABLE_GC != 0
  9314. ref_type,
  9315. #endif
  9316. error_buf, error_buf_size)) {
  9317. goto fail;
  9318. }
  9319. cell_num = wasm_value_type_cell_num(types[i]);
  9320. frame_ref -= cell_num;
  9321. available_stack_cell -= cell_num;
  9322. #if WASM_ENABLE_GC != 0
  9323. if (is_type_multi_byte) {
  9324. frame_reftype_map--;
  9325. available_reftype_map--;
  9326. reftype_map--;
  9327. }
  9328. #endif
  9329. }
  9330. #if WASM_ENABLE_FAST_INTERP != 0
  9331. emit_br_info(target_block, opcode == WASM_OP_BR);
  9332. #endif
  9333. ret = true;
  9334. cleanup_and_return:
  9335. fail:
  9336. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9337. wasm_runtime_free(frame_ref_buf);
  9338. #if WASM_ENABLE_GC != 0
  9339. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9340. wasm_runtime_free(frame_reftype_map_buf);
  9341. #endif
  9342. #if WASM_ENABLE_FAST_INTERP != 0
  9343. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9344. wasm_runtime_free(frame_offset_buf);
  9345. #endif
  9346. return ret;
  9347. }
  9348. static BranchBlock *
  9349. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9350. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9351. {
  9352. uint8 *p = *p_buf, *p_end = buf_end;
  9353. BranchBlock *frame_csp_tmp;
  9354. uint32 depth;
  9355. read_leb_uint32(p, p_end, depth);
  9356. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9357. error_buf_size)) {
  9358. goto fail;
  9359. }
  9360. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9361. *p_buf = p;
  9362. return frame_csp_tmp;
  9363. fail:
  9364. return NULL;
  9365. }
  9366. #if WASM_ENABLE_EXCE_HANDLING != 0
  9367. static BranchBlock *
  9368. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9369. uint8 *buf_end, char *error_buf,
  9370. uint32 error_buf_size)
  9371. {
  9372. uint8 *p = *p_buf, *p_end = buf_end;
  9373. BranchBlock *frame_csp_tmp;
  9374. uint32 depth;
  9375. read_leb_uint32(p, p_end, depth);
  9376. /*
  9377. * Note: "delegate 0" means the surrounding block, not the
  9378. * try-delegate block itself.
  9379. *
  9380. * Note: the caller hasn't popped the try-delegate frame yet.
  9381. */
  9382. bh_assert(loader_ctx->csp_num > 0);
  9383. if (loader_ctx->csp_num - 1 <= depth) {
  9384. #if WASM_ENABLE_SPEC_TEST == 0
  9385. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9386. #else
  9387. set_error_buf(error_buf, error_buf_size, "unknown label");
  9388. #endif
  9389. goto fail;
  9390. }
  9391. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9392. #if WASM_ENABLE_FAST_INTERP != 0
  9393. emit_br_info(frame_csp_tmp, false);
  9394. #endif
  9395. *p_buf = p;
  9396. return frame_csp_tmp;
  9397. fail:
  9398. return NULL;
  9399. }
  9400. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9401. static bool
  9402. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9403. char *error_buf, uint32 error_buf_size)
  9404. {
  9405. BlockType *block_type = &block->block_type;
  9406. uint8 *return_types = NULL;
  9407. uint32 return_count = 0;
  9408. int32 available_stack_cell, return_cell_num, i;
  9409. uint8 *frame_ref = NULL;
  9410. #if WASM_ENABLE_GC != 0
  9411. WASMRefTypeMap *frame_reftype_map;
  9412. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9413. WASMRefType *ref_type;
  9414. uint32 param_count, return_reftype_map_count = 0;
  9415. int32 available_reftype_map =
  9416. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9417. #endif
  9418. available_stack_cell =
  9419. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9420. #if WASM_ENABLE_GC == 0
  9421. return_count = block_type_get_result_types(block_type, &return_types);
  9422. #else
  9423. return_count = block_type_get_result_types(block_type, &return_types,
  9424. &return_reftype_maps,
  9425. &return_reftype_map_count);
  9426. param_count =
  9427. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9428. (void)param_count;
  9429. #endif
  9430. return_cell_num =
  9431. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9432. /* If the stack is in polymorphic state, just clear the stack
  9433. * and then re-push the values to make the stack top values
  9434. * match block type. */
  9435. if (block->is_stack_polymorphic) {
  9436. #if WASM_ENABLE_GC != 0
  9437. int32 j = return_reftype_map_count - 1;
  9438. #endif
  9439. for (i = (int32)return_count - 1; i >= 0; i--) {
  9440. #if WASM_ENABLE_GC != 0
  9441. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9442. bh_assert(return_reftype_maps[j].index == i + param_count);
  9443. bh_memcpy_s(
  9444. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9445. return_reftype_maps[j].ref_type,
  9446. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9447. j--;
  9448. }
  9449. #endif
  9450. #if WASM_ENABLE_FAST_INTERP != 0
  9451. POP_OFFSET_TYPE(return_types[i]);
  9452. #endif
  9453. POP_TYPE(return_types[i]);
  9454. }
  9455. /* Check stack is empty */
  9456. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9457. set_error_buf(
  9458. error_buf, error_buf_size,
  9459. "type mismatch: stack size does not match block type");
  9460. goto fail;
  9461. }
  9462. #if WASM_ENABLE_GC != 0
  9463. j = 0;
  9464. #endif
  9465. for (i = 0; i < (int32)return_count; i++) {
  9466. #if WASM_ENABLE_GC != 0
  9467. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9468. bh_assert(return_reftype_maps[j].index == i + param_count);
  9469. bh_memcpy_s(
  9470. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9471. return_reftype_maps[j].ref_type,
  9472. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9473. j++;
  9474. }
  9475. #endif
  9476. #if WASM_ENABLE_FAST_INTERP != 0
  9477. bool disable_emit = true;
  9478. int16 operand_offset = 0;
  9479. PUSH_OFFSET_TYPE(return_types[i]);
  9480. #endif
  9481. PUSH_TYPE(return_types[i]);
  9482. }
  9483. return true;
  9484. }
  9485. if (available_stack_cell != return_cell_num) {
  9486. #if WASM_ENABLE_EXCE_HANDLING != 0
  9487. /* testspec: this error message format is expected by try_catch.wast */
  9488. snprintf(
  9489. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9490. block->label_type == LABEL_TYPE_TRY
  9491. || (block->label_type == LABEL_TYPE_CATCH
  9492. && return_cell_num > 0)
  9493. ? "instruction"
  9494. : "block",
  9495. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9496. " but stack has ",
  9497. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9498. : "");
  9499. goto fail;
  9500. #else
  9501. set_error_buf(error_buf, error_buf_size,
  9502. "type mismatch: stack size does not match block type");
  9503. goto fail;
  9504. #endif
  9505. }
  9506. /* Check stack values match return types */
  9507. frame_ref = loader_ctx->frame_ref;
  9508. #if WASM_ENABLE_GC != 0
  9509. frame_reftype_map = loader_ctx->frame_reftype_map;
  9510. return_reftype_map =
  9511. return_reftype_map_count
  9512. ? return_reftype_maps + return_reftype_map_count - 1
  9513. : NULL;
  9514. #endif
  9515. for (i = (int32)return_count - 1; i >= 0; i--) {
  9516. uint8 type = return_types[i];
  9517. #if WASM_ENABLE_GC != 0
  9518. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9519. ref_type = NULL;
  9520. if (is_type_multi_byte) {
  9521. bh_assert(return_reftype_map);
  9522. ref_type = return_reftype_map->ref_type;
  9523. }
  9524. #endif
  9525. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9526. #if WASM_ENABLE_GC != 0
  9527. frame_reftype_map, available_reftype_map,
  9528. #endif
  9529. type,
  9530. #if WASM_ENABLE_GC != 0
  9531. ref_type,
  9532. #endif
  9533. error_buf, error_buf_size))
  9534. return false;
  9535. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9536. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9537. #if WASM_ENABLE_GC != 0
  9538. if (is_type_multi_byte) {
  9539. frame_reftype_map--;
  9540. available_reftype_map--;
  9541. return_reftype_map--;
  9542. }
  9543. #endif
  9544. }
  9545. return true;
  9546. fail:
  9547. return false;
  9548. }
  9549. #if WASM_ENABLE_FAST_INTERP != 0
  9550. /* Copy parameters to dynamic space.
  9551. * 1) POP original parameter out;
  9552. * 2) Push and copy original values to dynamic space.
  9553. * The copy instruction format:
  9554. * Part a: param count
  9555. * Part b: all param total cell num
  9556. * Part c: each param's cell_num, src offset and dst offset
  9557. * Part d: each param's src offset
  9558. * Part e: each param's dst offset
  9559. */
  9560. static bool
  9561. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9562. uint32 error_buf_size)
  9563. {
  9564. bool ret = false;
  9565. int16 *frame_offset = NULL;
  9566. uint8 *cells = NULL, cell;
  9567. int16 *src_offsets = NULL;
  9568. uint8 *emit_data = NULL;
  9569. uint32 i;
  9570. BranchBlock *block = loader_ctx->frame_csp - 1;
  9571. BlockType *block_type = &block->block_type;
  9572. WASMFuncType *wasm_type = block_type->u.type;
  9573. uint32 param_count = block_type->u.type->param_count;
  9574. int16 condition_offset = 0;
  9575. bool disable_emit = false;
  9576. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9577. int16 operand_offset = 0;
  9578. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9579. bh_assert(size > 0);
  9580. /* For if block, we also need copy the condition operand offset. */
  9581. if (is_if_block)
  9582. size += sizeof(*cells) + sizeof(*src_offsets);
  9583. /* Allocate memory for the emit data */
  9584. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9585. return false;
  9586. cells = emit_data;
  9587. src_offsets = (int16 *)(cells + param_count);
  9588. if (is_if_block)
  9589. condition_offset = *loader_ctx->frame_offset;
  9590. /* POP original parameter out */
  9591. for (i = 0; i < param_count; i++) {
  9592. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9593. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9594. }
  9595. frame_offset = loader_ctx->frame_offset;
  9596. /* Get each param's cell num and src offset */
  9597. for (i = 0; i < param_count; i++) {
  9598. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9599. cells[i] = cell;
  9600. src_offsets[i] = *frame_offset;
  9601. frame_offset += cell;
  9602. }
  9603. /* emit copy instruction */
  9604. emit_label(EXT_OP_COPY_STACK_VALUES);
  9605. /* Part a) */
  9606. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9607. /* Part b) */
  9608. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9609. : wasm_type->param_cell_num);
  9610. /* Part c) */
  9611. for (i = 0; i < param_count; i++)
  9612. emit_byte(loader_ctx, cells[i]);
  9613. if (is_if_block)
  9614. emit_byte(loader_ctx, 1);
  9615. /* Part d) */
  9616. for (i = 0; i < param_count; i++)
  9617. emit_operand(loader_ctx, src_offsets[i]);
  9618. if (is_if_block)
  9619. emit_operand(loader_ctx, condition_offset);
  9620. /* Since the start offset to save the block's params and
  9621. * the start offset to save the block's results may be
  9622. * different, we remember the dynamic offset for loop block
  9623. * so that we can use it to copy the stack operands to the
  9624. * loop block's params in wasm_loader_emit_br_info. */
  9625. if (block->label_type == LABEL_TYPE_LOOP)
  9626. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9627. /* Part e) */
  9628. /* Push to dynamic space. The push will emit the dst offset. */
  9629. for (i = 0; i < param_count; i++)
  9630. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9631. if (is_if_block)
  9632. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9633. ret = true;
  9634. fail:
  9635. /* Free the emit data */
  9636. wasm_runtime_free(emit_data);
  9637. return ret;
  9638. }
  9639. #endif
  9640. #if WASM_ENABLE_GC == 0
  9641. #define RESET_REFTYPE_MAP_STACK() (void)0
  9642. #else
  9643. #define RESET_REFTYPE_MAP_STACK() \
  9644. do { \
  9645. loader_ctx->reftype_map_num = \
  9646. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9647. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9648. + loader_ctx->reftype_map_num; \
  9649. } while (0)
  9650. #endif
  9651. /* reset the stack to the state of before entering the last block */
  9652. #if WASM_ENABLE_FAST_INTERP != 0
  9653. #define RESET_STACK() \
  9654. do { \
  9655. loader_ctx->stack_cell_num = \
  9656. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9657. loader_ctx->frame_ref = \
  9658. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9659. loader_ctx->frame_offset = \
  9660. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9661. RESET_REFTYPE_MAP_STACK(); \
  9662. } while (0)
  9663. #else
  9664. #define RESET_STACK() \
  9665. do { \
  9666. loader_ctx->stack_cell_num = \
  9667. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9668. loader_ctx->frame_ref = \
  9669. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9670. RESET_REFTYPE_MAP_STACK(); \
  9671. } while (0)
  9672. #endif
  9673. /* set current block's stack polymorphic state */
  9674. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9675. do { \
  9676. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9677. _cur_block->is_stack_polymorphic = flag; \
  9678. } while (0)
  9679. #define BLOCK_HAS_PARAM(block_type) \
  9680. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9681. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9682. do { \
  9683. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9684. error_buf_size))) { \
  9685. goto fail; \
  9686. } \
  9687. } while (0)
  9688. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9689. static bool
  9690. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9691. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9692. uint32 error_buf_size)
  9693. {
  9694. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9695. return false;
  9696. }
  9697. if (table_idx < module->import_table_count) {
  9698. if (p_elem_type)
  9699. *p_elem_type =
  9700. module->import_tables[table_idx].u.table.table_type.elem_type;
  9701. #if WASM_ENABLE_GC != 0
  9702. if (p_ref_type)
  9703. *((WASMRefType **)p_ref_type) =
  9704. module->import_tables[table_idx]
  9705. .u.table.table_type.elem_ref_type;
  9706. #endif
  9707. }
  9708. else {
  9709. if (p_elem_type)
  9710. *p_elem_type =
  9711. module->tables[module->import_table_count + table_idx]
  9712. .table_type.elem_type;
  9713. #if WASM_ENABLE_GC != 0
  9714. if (p_ref_type)
  9715. *((WASMRefType **)p_ref_type) =
  9716. module->tables[module->import_table_count + table_idx]
  9717. .table_type.elem_ref_type;
  9718. #endif
  9719. }
  9720. return true;
  9721. }
  9722. static bool
  9723. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9724. uint8 *p_elem_type, void **p_elem_ref_type,
  9725. char *error_buf, uint32 error_buf_size)
  9726. {
  9727. if (table_seg_idx >= module->table_seg_count) {
  9728. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9729. table_seg_idx);
  9730. return false;
  9731. }
  9732. if (p_elem_type) {
  9733. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9734. }
  9735. #if WASM_ENABLE_GC != 0
  9736. if (p_elem_ref_type)
  9737. *((WASMRefType **)p_elem_ref_type) =
  9738. module->table_segments[table_seg_idx].elem_ref_type;
  9739. #endif
  9740. return true;
  9741. }
  9742. #endif
  9743. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9744. const uint8 *
  9745. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9746. uint32 *len)
  9747. {
  9748. WASMCustomSection *section = module->custom_section_list;
  9749. while (section) {
  9750. if ((section->name_len == strlen(name))
  9751. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9752. if (len) {
  9753. *len = section->content_len;
  9754. }
  9755. return section->content_addr;
  9756. }
  9757. section = section->next;
  9758. }
  9759. return NULL;
  9760. }
  9761. #endif
  9762. #if 0
  9763. #define HANDLE_OPCODE(opcode) #opcode
  9764. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  9765. #undef HANDLE_OPCODE
  9766. #endif
  9767. static bool
  9768. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9769. uint32 cur_func_idx, char *error_buf,
  9770. uint32 error_buf_size)
  9771. {
  9772. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9773. uint32 param_count, local_count, global_count;
  9774. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type,
  9775. table_elem_idx_type;
  9776. BlockType func_block_type;
  9777. uint16 *local_offsets, local_offset;
  9778. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9779. uint32 table_seg_idx, data_seg_idx, count, align, i;
  9780. mem_offset_t mem_offset;
  9781. int32 i32_const = 0;
  9782. int64 i64_const;
  9783. uint8 opcode;
  9784. bool return_value = false;
  9785. WASMLoaderContext *loader_ctx;
  9786. BranchBlock *frame_csp_tmp;
  9787. #if WASM_ENABLE_GC != 0
  9788. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9789. uint32 param_reftype_map_count, local_reftype_map_count;
  9790. int32 heap_type;
  9791. WASMRefType wasm_ref_type = { 0 };
  9792. bool need_ref_type_map;
  9793. #endif
  9794. #if WASM_ENABLE_FAST_INTERP != 0
  9795. uint8 *func_const_end, *func_const = NULL;
  9796. int16 operand_offset = 0;
  9797. uint8 last_op = 0;
  9798. bool disable_emit, preserve_local = false, if_condition_available = true;
  9799. float32 f32_const;
  9800. float64 f64_const;
  9801. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9802. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9803. #endif
  9804. #if WASM_ENABLE_MEMORY64 != 0
  9805. bool is_memory64 = has_module_memory64(module);
  9806. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  9807. #else
  9808. mem_offset_type = VALUE_TYPE_I32;
  9809. table_elem_idx_type = VALUE_TYPE_I32;
  9810. #endif
  9811. uint32 memidx;
  9812. global_count = module->import_global_count + module->global_count;
  9813. param_count = func->func_type->param_count;
  9814. param_types = func->func_type->types;
  9815. func_block_type.is_value_type = false;
  9816. func_block_type.u.type = func->func_type;
  9817. local_count = func->local_count;
  9818. local_types = func->local_types;
  9819. local_offsets = func->local_offsets;
  9820. #if WASM_ENABLE_GC != 0
  9821. param_reftype_maps = func->func_type->ref_type_maps;
  9822. param_reftype_map_count = func->func_type->ref_type_map_count;
  9823. local_reftype_maps = func->local_ref_type_maps;
  9824. local_reftype_map_count = func->local_ref_type_map_count;
  9825. #endif
  9826. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9827. goto fail;
  9828. }
  9829. #if WASM_ENABLE_GC != 0
  9830. loader_ctx->module = module;
  9831. loader_ctx->ref_type_set = module->ref_type_set;
  9832. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9833. #endif
  9834. #if WASM_ENABLE_FAST_INTERP != 0
  9835. /* For the first traverse, the initial value of preserved_local_offset has
  9836. * not been determined, we use the INT16_MAX to represent that a slot has
  9837. * been copied to preserve space. For second traverse, this field will be
  9838. * set to the appropriate value in wasm_loader_ctx_reinit.
  9839. * This is for Issue #1230,
  9840. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9841. * drop opcodes need to know which slots are preserved, so those slots will
  9842. * not be treated as dynamically allocated slots */
  9843. loader_ctx->preserved_local_offset = INT16_MAX;
  9844. re_scan:
  9845. if (loader_ctx->code_compiled_size > 0) {
  9846. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9847. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9848. goto fail;
  9849. }
  9850. p = func->code;
  9851. func->code_compiled = loader_ctx->p_code_compiled;
  9852. func->code_compiled_size = loader_ctx->code_compiled_size;
  9853. }
  9854. #endif
  9855. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9856. while (p < p_end) {
  9857. opcode = *p++;
  9858. #if WASM_ENABLE_FAST_INTERP != 0
  9859. p_org = p;
  9860. disable_emit = false;
  9861. emit_label(opcode);
  9862. #endif
  9863. switch (opcode) {
  9864. case WASM_OP_UNREACHABLE:
  9865. RESET_STACK();
  9866. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9867. break;
  9868. case WASM_OP_NOP:
  9869. #if WASM_ENABLE_FAST_INTERP != 0
  9870. skip_label();
  9871. #endif
  9872. break;
  9873. case WASM_OP_IF:
  9874. {
  9875. #if WASM_ENABLE_FAST_INTERP != 0
  9876. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9877. int32 available_stack_cell =
  9878. (int32)(loader_ctx->stack_cell_num
  9879. - parent_block->stack_cell_num);
  9880. if (available_stack_cell <= 0
  9881. && parent_block->is_stack_polymorphic)
  9882. if_condition_available = false;
  9883. else
  9884. if_condition_available = true;
  9885. PRESERVE_LOCAL_FOR_BLOCK();
  9886. #endif
  9887. #if WASM_ENABLE_GC == 0
  9888. POP_I32();
  9889. #endif
  9890. goto handle_op_block_and_loop;
  9891. }
  9892. case WASM_OP_BLOCK:
  9893. case WASM_OP_LOOP:
  9894. #if WASM_ENABLE_EXCE_HANDLING != 0
  9895. case WASM_OP_TRY:
  9896. if (opcode == WASM_OP_TRY) {
  9897. /*
  9898. * keep track of exception handlers to account for
  9899. * memory allocation
  9900. */
  9901. func->exception_handler_count++;
  9902. /*
  9903. * try is a block
  9904. * do nothing special, but execution continues to
  9905. * to handle_op_block_and_loop,
  9906. * and that be pushes the csp
  9907. */
  9908. }
  9909. #endif
  9910. #if WASM_ENABLE_FAST_INTERP != 0
  9911. PRESERVE_LOCAL_FOR_BLOCK();
  9912. #endif
  9913. handle_op_block_and_loop:
  9914. {
  9915. uint8 value_type;
  9916. BlockType block_type;
  9917. #if WASM_ENABLE_FAST_INTERP != 0
  9918. uint32 available_params = 0;
  9919. #endif
  9920. CHECK_BUF(p, p_end, 1);
  9921. value_type = read_uint8(p);
  9922. if (is_byte_a_type(value_type)) {
  9923. /* If the first byte is one of these special values:
  9924. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9925. * the single return value. */
  9926. block_type.is_value_type = true;
  9927. block_type.u.value_type.type = value_type;
  9928. #if WASM_ENABLE_WAMR_COMPILER != 0
  9929. if (value_type == VALUE_TYPE_V128)
  9930. module->is_simd_used = true;
  9931. else if (value_type == VALUE_TYPE_FUNCREF
  9932. || value_type == VALUE_TYPE_EXTERNREF)
  9933. module->is_ref_types_used = true;
  9934. #endif
  9935. #if WASM_ENABLE_GC != 0
  9936. if (value_type != VALUE_TYPE_VOID) {
  9937. p_org = p;
  9938. p--;
  9939. if (!resolve_value_type((const uint8 **)&p, p_end,
  9940. module, module->type_count,
  9941. &need_ref_type_map,
  9942. &wasm_ref_type, false,
  9943. error_buf, error_buf_size)) {
  9944. goto fail;
  9945. }
  9946. if (need_ref_type_map) {
  9947. block_type.u.value_type.ref_type_map.index = 0;
  9948. if (!(block_type.u.value_type.ref_type_map
  9949. .ref_type = reftype_set_insert(
  9950. module->ref_type_set, &wasm_ref_type,
  9951. error_buf, error_buf_size))) {
  9952. goto fail;
  9953. }
  9954. }
  9955. /* Set again as the type might be changed, e.g.
  9956. (ref null any) to anyref */
  9957. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9958. #if WASM_ENABLE_FAST_INTERP == 0
  9959. while (p_org < p) {
  9960. #if WASM_ENABLE_DEBUG_INTERP != 0
  9961. if (!record_fast_op(module, p_org, *p_org,
  9962. error_buf, error_buf_size)) {
  9963. goto fail;
  9964. }
  9965. #endif
  9966. /* Ignore extra bytes for interpreter */
  9967. *p_org++ = WASM_OP_NOP;
  9968. }
  9969. #endif
  9970. }
  9971. #endif /* end of WASM_ENABLE_GC != 0 */
  9972. }
  9973. else {
  9974. int32 type_index;
  9975. /* Resolve the leb128 encoded type index as block type */
  9976. p--;
  9977. p_org = p - 1;
  9978. read_leb_int32(p, p_end, type_index);
  9979. if ((uint32)type_index >= module->type_count) {
  9980. set_error_buf(error_buf, error_buf_size,
  9981. "unknown type");
  9982. goto fail;
  9983. }
  9984. block_type.is_value_type = false;
  9985. block_type.u.type =
  9986. (WASMFuncType *)module->types[type_index];
  9987. #if WASM_ENABLE_FAST_INTERP == 0
  9988. /* If block use type index as block type, change the opcode
  9989. * to new extended opcode so that interpreter can resolve
  9990. * the block quickly.
  9991. */
  9992. #if WASM_ENABLE_DEBUG_INTERP != 0
  9993. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9994. error_buf_size)) {
  9995. goto fail;
  9996. }
  9997. #endif
  9998. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  9999. #endif
  10000. }
  10001. #if WASM_ENABLE_GC != 0
  10002. if (opcode == WASM_OP_IF) {
  10003. POP_I32();
  10004. }
  10005. #endif
  10006. /* Pop block parameters from stack */
  10007. if (BLOCK_HAS_PARAM(block_type)) {
  10008. WASMFuncType *wasm_type = block_type.u.type;
  10009. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10010. #if WASM_ENABLE_FAST_INTERP != 0
  10011. uint32 cell_num;
  10012. available_params = block_type.u.type->param_count;
  10013. #endif
  10014. for (i = 0; i < block_type.u.type->param_count; i++) {
  10015. int32 available_stack_cell =
  10016. (int32)(loader_ctx->stack_cell_num
  10017. - cur_block->stack_cell_num);
  10018. if (available_stack_cell <= 0
  10019. && cur_block->is_stack_polymorphic) {
  10020. #if WASM_ENABLE_FAST_INTERP != 0
  10021. available_params = i;
  10022. #endif
  10023. break;
  10024. }
  10025. POP_TYPE(
  10026. wasm_type->types[wasm_type->param_count - i - 1]);
  10027. #if WASM_ENABLE_FAST_INTERP != 0
  10028. /* decrease the frame_offset pointer accordingly to keep
  10029. * consistent with frame_ref stack */
  10030. cell_num = wasm_value_type_cell_num(
  10031. wasm_type->types[wasm_type->param_count - i - 1]);
  10032. loader_ctx->frame_offset -= cell_num;
  10033. #endif
  10034. }
  10035. }
  10036. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10037. block_type, p);
  10038. /* Pass parameters to block */
  10039. if (BLOCK_HAS_PARAM(block_type)) {
  10040. WASMFuncType *func_type = block_type.u.type;
  10041. #if WASM_ENABLE_GC != 0
  10042. WASMRefType *ref_type;
  10043. uint32 j = 0;
  10044. #endif
  10045. for (i = 0; i < func_type->param_count; i++) {
  10046. #if WASM_ENABLE_FAST_INTERP != 0
  10047. uint32 cell_num =
  10048. wasm_value_type_cell_num(func_type->types[i]);
  10049. if (i >= available_params) {
  10050. /* If there isn't enough data on stack, push a dummy
  10051. * offset to keep the stack consistent with
  10052. * frame_ref.
  10053. * Since the stack is already in polymorphic state,
  10054. * the opcode will not be executed, so the dummy
  10055. * offset won't cause any error */
  10056. uint32 n;
  10057. for (n = 0; n < cell_num; n++) {
  10058. if (loader_ctx->p_code_compiled == NULL) {
  10059. if (!check_offset_push(loader_ctx,
  10060. error_buf,
  10061. error_buf_size))
  10062. goto fail;
  10063. }
  10064. *loader_ctx->frame_offset++ = 0;
  10065. }
  10066. }
  10067. else {
  10068. loader_ctx->frame_offset += cell_num;
  10069. }
  10070. #endif
  10071. #if WASM_ENABLE_GC != 0
  10072. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10073. bh_assert(func_type->ref_type_maps[j].index == i);
  10074. ref_type = func_type->ref_type_maps[j].ref_type;
  10075. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10076. ref_type,
  10077. wasm_reftype_struct_size(ref_type));
  10078. j++;
  10079. }
  10080. #endif
  10081. PUSH_TYPE(func_type->types[i]);
  10082. }
  10083. }
  10084. #if WASM_ENABLE_FAST_INTERP != 0
  10085. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10086. skip_label();
  10087. if (BLOCK_HAS_PARAM(block_type)) {
  10088. /* Make sure params are in dynamic space */
  10089. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10090. error_buf_size))
  10091. goto fail;
  10092. }
  10093. if (opcode == WASM_OP_LOOP) {
  10094. (loader_ctx->frame_csp - 1)->code_compiled =
  10095. loader_ctx->p_code_compiled;
  10096. }
  10097. }
  10098. #if WASM_ENABLE_EXCE_HANDLING != 0
  10099. else if (opcode == WASM_OP_TRY) {
  10100. skip_label();
  10101. }
  10102. #endif
  10103. else if (opcode == WASM_OP_IF) {
  10104. BranchBlock *block = loader_ctx->frame_csp - 1;
  10105. /* If block has parameters, we should make sure they are in
  10106. * dynamic space. Otherwise, when else branch is missing,
  10107. * the later opcode may consume incorrect operand offset.
  10108. * Spec case:
  10109. * (func (export "params-id") (param i32) (result i32)
  10110. * (i32.const 1)
  10111. * (i32.const 2)
  10112. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10113. * (then)) (i32.add)
  10114. * )
  10115. *
  10116. * So we should emit a copy instruction before the if.
  10117. *
  10118. * And we also need to save the parameter offsets and
  10119. * recover them before entering else branch.
  10120. *
  10121. */
  10122. if (BLOCK_HAS_PARAM(block_type)) {
  10123. uint64 size;
  10124. /* In polymorphic state, there may be no if condition on
  10125. * the stack, so the offset may not emitted */
  10126. if (if_condition_available) {
  10127. /* skip the if condition operand offset */
  10128. wasm_loader_emit_backspace(loader_ctx,
  10129. sizeof(int16));
  10130. }
  10131. /* skip the if label */
  10132. skip_label();
  10133. /* Emit a copy instruction */
  10134. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10135. error_buf_size))
  10136. goto fail;
  10137. /* Emit the if instruction */
  10138. emit_label(opcode);
  10139. /* Emit the new condition operand offset */
  10140. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10141. /* Save top param_count values of frame_offset stack, so
  10142. * that we can recover it before executing else branch
  10143. */
  10144. size = sizeof(int16)
  10145. * (uint64)block_type.u.type->param_cell_num;
  10146. if (!(block->param_frame_offsets = loader_malloc(
  10147. size, error_buf, error_buf_size)))
  10148. goto fail;
  10149. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10150. loader_ctx->frame_offset
  10151. - size / sizeof(int16),
  10152. (uint32)size);
  10153. }
  10154. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10155. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10156. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10157. }
  10158. #endif
  10159. break;
  10160. }
  10161. #if WASM_ENABLE_EXCE_HANDLING != 0
  10162. case WASM_OP_THROW:
  10163. {
  10164. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10165. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10166. uint8 label_type = cur_block->label_type;
  10167. uint32 tag_index = 0;
  10168. read_leb_int32(p, p_end, tag_index);
  10169. /* check validity of tag_index against module->tag_count */
  10170. /* check tag index is within the tag index space */
  10171. if (tag_index >= module->import_tag_count + module->tag_count) {
  10172. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10173. tag_index);
  10174. goto fail;
  10175. }
  10176. /* the tag_type is stored in either the WASMTag (section tags)
  10177. * or WASMTagImport (import tag) */
  10178. WASMFuncType *tag_type = NULL;
  10179. if (tag_index < module->import_tag_count) {
  10180. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10181. }
  10182. else {
  10183. tag_type =
  10184. module->tags[tag_index - module->import_tag_count]
  10185. ->tag_type;
  10186. }
  10187. if (tag_type->result_count != 0) {
  10188. set_error_buf(error_buf, error_buf_size,
  10189. "tag type signature does not return void");
  10190. goto fail;
  10191. }
  10192. int32 available_stack_cell =
  10193. (int32)(loader_ctx->stack_cell_num
  10194. - cur_block->stack_cell_num);
  10195. int32 tti;
  10196. /* Check stack values match return types by comparing tag param
  10197. * types with stack cells */
  10198. uint8 *frame_ref = loader_ctx->frame_ref;
  10199. #if WASM_ENABLE_GC != 0
  10200. WASMRefTypeMap *frame_reftype_map =
  10201. loader_ctx->frame_reftype_map;
  10202. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10203. /* Temporarily set these values since they may be used in
  10204. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10205. param_reftype_maps = tag_type->ref_type_maps;
  10206. /* For tag_type function, it shouldn't have result_count = 0 */
  10207. param_reftype_map_count = tag_type->ref_type_map_count;
  10208. param_count = tag_type->param_count;
  10209. #endif
  10210. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10211. #if WASM_ENABLE_GC != 0
  10212. local_type = tag_type->types[tti];
  10213. local_idx = tti;
  10214. /* Get the wasm_ref_type if the local_type is multibyte
  10215. type */
  10216. GET_LOCAL_REFTYPE();
  10217. #endif
  10218. if (!check_stack_top_values(
  10219. loader_ctx, frame_ref, available_stack_cell,
  10220. #if WASM_ENABLE_GC != 0
  10221. frame_reftype_map, frame_reftype_map_num,
  10222. #endif
  10223. tag_type->types[tti],
  10224. #if WASM_ENABLE_GC != 0
  10225. &wasm_ref_type,
  10226. #endif
  10227. error_buf, error_buf_size)) {
  10228. snprintf(error_buf, error_buf_size,
  10229. "type mismatch: instruction requires [%s] but "
  10230. "stack has [%s]",
  10231. tag_type->param_count > 0
  10232. ? type2str(tag_type->types[tti])
  10233. : "",
  10234. available_stack_cell > 0
  10235. ? type2str(*(loader_ctx->frame_ref - 1))
  10236. : "");
  10237. goto fail;
  10238. }
  10239. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10240. available_stack_cell -=
  10241. wasm_value_type_cell_num(tag_type->types[tti]);
  10242. }
  10243. #if WASM_ENABLE_GC != 0
  10244. /* Restore the values */
  10245. param_reftype_maps = func->func_type->ref_type_maps;
  10246. param_reftype_map_count = func->func_type->ref_type_map_count;
  10247. param_count = func->func_type->param_count;
  10248. #endif
  10249. /* throw is stack polymorphic */
  10250. (void)label_type;
  10251. RESET_STACK();
  10252. break;
  10253. }
  10254. case WASM_OP_RETHROW:
  10255. {
  10256. /* must be done before checking branch block */
  10257. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10258. /* check the target catching block: LABEL_TYPE_CATCH */
  10259. if (!(frame_csp_tmp =
  10260. check_branch_block(loader_ctx, &p, p_end, opcode,
  10261. error_buf, error_buf_size)))
  10262. goto fail;
  10263. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10264. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10265. /* trap according to spectest (rethrow.wast) */
  10266. set_error_buf(error_buf, error_buf_size,
  10267. "invalid rethrow label");
  10268. goto fail;
  10269. }
  10270. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10271. uint8 label_type = cur_block->label_type;
  10272. (void)label_type;
  10273. /* rethrow is stack polymorphic */
  10274. RESET_STACK();
  10275. break;
  10276. }
  10277. case WASM_OP_DELEGATE:
  10278. {
  10279. /* check target block is valid */
  10280. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10281. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10282. goto fail;
  10283. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10284. uint8 label_type = cur_block->label_type;
  10285. (void)label_type;
  10286. /* DELEGATE ends the block */
  10287. POP_CSP();
  10288. break;
  10289. }
  10290. case WASM_OP_CATCH:
  10291. {
  10292. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10293. uint8 label_type = cur_block->label_type;
  10294. uint32 tag_index = 0;
  10295. read_leb_int32(p, p_end, tag_index);
  10296. /* check validity of tag_index against module->tag_count */
  10297. /* check tag index is within the tag index space */
  10298. if (tag_index >= module->import_tag_count + module->tag_count) {
  10299. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10300. __FUNCTION__);
  10301. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10302. goto fail;
  10303. }
  10304. /* the tag_type is stored in either the WASMTag (section tags)
  10305. * or WASMTagImport (import tag) */
  10306. WASMFuncType *func_type = NULL;
  10307. if (tag_index < module->import_tag_count) {
  10308. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10309. }
  10310. else {
  10311. func_type =
  10312. module->tags[tag_index - module->import_tag_count]
  10313. ->tag_type;
  10314. }
  10315. if (func_type->result_count != 0) {
  10316. set_error_buf(error_buf, error_buf_size,
  10317. "tag type signature does not return void");
  10318. goto fail;
  10319. }
  10320. /* check validity of current label (expect LABEL_TYPE_TRY or
  10321. * LABEL_TYPE_CATCH) */
  10322. if ((LABEL_TYPE_CATCH != label_type)
  10323. && (LABEL_TYPE_TRY != label_type)) {
  10324. set_error_buf(error_buf, error_buf_size,
  10325. "Unexpected block sequence encountered.");
  10326. goto fail;
  10327. }
  10328. /*
  10329. * replace frame_csp by LABEL_TYPE_CATCH
  10330. */
  10331. cur_block->label_type = LABEL_TYPE_CATCH;
  10332. /* RESET_STACK removes the values pushed in TRY or pervious
  10333. * CATCH Blocks */
  10334. RESET_STACK();
  10335. #if WASM_ENABLE_GC != 0
  10336. WASMRefType *ref_type;
  10337. uint32 j = 0;
  10338. #endif
  10339. /* push types on the stack according to caught type */
  10340. for (i = 0; i < func_type->param_count; i++) {
  10341. #if WASM_ENABLE_GC != 0
  10342. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10343. bh_assert(func_type->ref_type_maps[j].index == i);
  10344. ref_type = func_type->ref_type_maps[j].ref_type;
  10345. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10346. ref_type,
  10347. wasm_reftype_struct_size(ref_type));
  10348. j++;
  10349. }
  10350. #endif
  10351. PUSH_TYPE(func_type->types[i]);
  10352. }
  10353. break;
  10354. }
  10355. case WASM_OP_CATCH_ALL:
  10356. {
  10357. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10358. /* expecting a TRY or CATCH, anything else will be considered an
  10359. * error */
  10360. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10361. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10362. set_error_buf(error_buf, error_buf_size,
  10363. "Unexpected block sequence encountered.");
  10364. goto fail;
  10365. }
  10366. /* no immediates */
  10367. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10368. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10369. /* RESET_STACK removes the values pushed in TRY or pervious
  10370. * CATCH Blocks */
  10371. RESET_STACK();
  10372. /* catch_all has no tagtype and therefore no parameters */
  10373. break;
  10374. }
  10375. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10376. case WASM_OP_ELSE:
  10377. handle_op_else:
  10378. {
  10379. BranchBlock *block = NULL;
  10380. BlockType block_type;
  10381. if (loader_ctx->csp_num < 2
  10382. /* the matched if isn't found */
  10383. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10384. /* duplicated else is found */
  10385. || (loader_ctx->frame_csp - 1)->else_addr) {
  10386. set_error_buf(
  10387. error_buf, error_buf_size,
  10388. "opcode else found without matched opcode if");
  10389. goto fail;
  10390. }
  10391. block = loader_ctx->frame_csp - 1;
  10392. /* check whether if branch's stack matches its result type */
  10393. if (!check_block_stack(loader_ctx, block, error_buf,
  10394. error_buf_size))
  10395. goto fail;
  10396. block->else_addr = p - 1;
  10397. block_type = block->block_type;
  10398. #if WASM_ENABLE_GC != 0
  10399. if (!wasm_loader_init_local_use_masks(
  10400. loader_ctx, local_count, error_buf, error_buf_size)) {
  10401. goto fail;
  10402. }
  10403. #endif
  10404. #if WASM_ENABLE_FAST_INTERP != 0
  10405. /* if the result of if branch is in local or const area, add a
  10406. * copy op */
  10407. RESERVE_BLOCK_RET();
  10408. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10409. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10410. #endif
  10411. RESET_STACK();
  10412. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10413. /* Pass parameters to if-false branch */
  10414. if (BLOCK_HAS_PARAM(block_type)) {
  10415. for (i = 0; i < block_type.u.type->param_count; i++)
  10416. PUSH_TYPE(block_type.u.type->types[i]);
  10417. }
  10418. #if WASM_ENABLE_FAST_INTERP != 0
  10419. /* Recover top param_count values of frame_offset stack */
  10420. if (BLOCK_HAS_PARAM((block_type))) {
  10421. uint32 size;
  10422. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10423. bh_memcpy_s(loader_ctx->frame_offset, size,
  10424. block->param_frame_offsets, size);
  10425. loader_ctx->frame_offset += (size / sizeof(int16));
  10426. }
  10427. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10428. #endif
  10429. break;
  10430. }
  10431. case WASM_OP_END:
  10432. {
  10433. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10434. /* check whether block stack matches its result type */
  10435. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10436. error_buf_size))
  10437. goto fail;
  10438. /* if there is no else branch, make a virtual else opcode for
  10439. easier integrity check and to copy the correct results to
  10440. the block return address for fast-interp mode:
  10441. change if block from `if ... end` to `if ... else end` */
  10442. if (cur_block->label_type == LABEL_TYPE_IF
  10443. && !cur_block->else_addr) {
  10444. opcode = WASM_OP_ELSE;
  10445. p--;
  10446. #if WASM_ENABLE_FAST_INTERP != 0
  10447. p_org = p;
  10448. skip_label();
  10449. disable_emit = false;
  10450. emit_label(opcode);
  10451. #endif
  10452. goto handle_op_else;
  10453. }
  10454. POP_CSP();
  10455. #if WASM_ENABLE_FAST_INTERP != 0
  10456. skip_label();
  10457. /* copy the result to the block return address */
  10458. RESERVE_BLOCK_RET();
  10459. apply_label_patch(loader_ctx, 0, PATCH_END);
  10460. free_label_patch_list(loader_ctx->frame_csp);
  10461. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10462. int32 idx;
  10463. uint8 ret_type;
  10464. emit_label(WASM_OP_RETURN);
  10465. for (idx = (int32)func->func_type->result_count - 1;
  10466. idx >= 0; idx--) {
  10467. ret_type = *(func->func_type->types
  10468. + func->func_type->param_count + idx);
  10469. POP_OFFSET_TYPE(ret_type);
  10470. }
  10471. }
  10472. #endif
  10473. if (loader_ctx->csp_num > 0) {
  10474. loader_ctx->frame_csp->end_addr = p - 1;
  10475. }
  10476. else {
  10477. /* end of function block, function will return */
  10478. if (p < p_end) {
  10479. set_error_buf(error_buf, error_buf_size,
  10480. "section size mismatch");
  10481. goto fail;
  10482. }
  10483. }
  10484. break;
  10485. }
  10486. case WASM_OP_BR:
  10487. {
  10488. if (!(frame_csp_tmp =
  10489. check_branch_block(loader_ctx, &p, p_end, opcode,
  10490. error_buf, error_buf_size)))
  10491. goto fail;
  10492. RESET_STACK();
  10493. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10494. break;
  10495. }
  10496. case WASM_OP_BR_IF:
  10497. {
  10498. POP_I32();
  10499. if (!(frame_csp_tmp =
  10500. check_branch_block(loader_ctx, &p, p_end, opcode,
  10501. error_buf, error_buf_size)))
  10502. goto fail;
  10503. break;
  10504. }
  10505. case WASM_OP_BR_TABLE:
  10506. {
  10507. uint32 depth = 0, default_arity, arity = 0;
  10508. BranchBlock *target_block;
  10509. BlockType *target_block_type;
  10510. #if WASM_ENABLE_FAST_INTERP == 0
  10511. BrTableCache *br_table_cache = NULL;
  10512. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10513. uint32 j;
  10514. #endif
  10515. read_leb_uint32(p, p_end, count);
  10516. #if WASM_ENABLE_FAST_INTERP != 0
  10517. emit_uint32(loader_ctx, count);
  10518. #endif
  10519. POP_I32();
  10520. /* Get each depth and check it */
  10521. p_org = p;
  10522. for (i = 0; i <= count; i++) {
  10523. read_leb_uint32(p, p_end, depth);
  10524. bh_assert(loader_ctx->csp_num > 0);
  10525. if (loader_ctx->csp_num - 1 < depth) {
  10526. set_error_buf(error_buf, error_buf_size,
  10527. "unknown label, "
  10528. "unexpected end of section or function");
  10529. goto fail;
  10530. }
  10531. }
  10532. p = p_org;
  10533. /* Get the default block's arity */
  10534. target_block = loader_ctx->frame_csp - (depth + 1);
  10535. target_block_type = &target_block->block_type;
  10536. default_arity = block_type_get_arity(target_block_type,
  10537. target_block->label_type);
  10538. #if WASM_ENABLE_FAST_INTERP == 0
  10539. p_depth_begin = p_depth = p;
  10540. #endif
  10541. for (i = 0; i <= count; i++) {
  10542. p_org = p;
  10543. read_leb_uint32(p, p_end, depth);
  10544. p = p_org;
  10545. /* Get the target block's arity and check it */
  10546. target_block = loader_ctx->frame_csp - (depth + 1);
  10547. target_block_type = &target_block->block_type;
  10548. arity = block_type_get_arity(target_block_type,
  10549. target_block->label_type);
  10550. if (arity != default_arity) {
  10551. set_error_buf(error_buf, error_buf_size,
  10552. "type mismatch: br_table targets must "
  10553. "all use same result type");
  10554. goto fail;
  10555. }
  10556. if (!(frame_csp_tmp =
  10557. check_branch_block(loader_ctx, &p, p_end, opcode,
  10558. error_buf, error_buf_size))) {
  10559. goto fail;
  10560. }
  10561. #if WASM_ENABLE_FAST_INTERP == 0
  10562. if (br_table_cache) {
  10563. br_table_cache->br_depths[i] = depth;
  10564. }
  10565. else {
  10566. if (depth > 255) {
  10567. /* The depth cannot be stored in one byte,
  10568. create br_table cache to store each depth */
  10569. #if WASM_ENABLE_DEBUG_INTERP != 0
  10570. if (!record_fast_op(module, p_opcode, *p_opcode,
  10571. error_buf, error_buf_size)) {
  10572. goto fail;
  10573. }
  10574. #endif
  10575. if (!(br_table_cache = loader_malloc(
  10576. offsetof(BrTableCache, br_depths)
  10577. + sizeof(uint32)
  10578. * (uint64)(count + 1),
  10579. error_buf, error_buf_size))) {
  10580. goto fail;
  10581. }
  10582. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10583. br_table_cache->br_table_op_addr = p_opcode;
  10584. br_table_cache->br_count = count;
  10585. /* Copy previous depths which are one byte */
  10586. for (j = 0; j < i; j++) {
  10587. br_table_cache->br_depths[j] = p_depth_begin[j];
  10588. }
  10589. br_table_cache->br_depths[i] = depth;
  10590. bh_list_insert(module->br_table_cache_list,
  10591. br_table_cache);
  10592. }
  10593. else {
  10594. /* The depth can be stored in one byte, use the
  10595. byte of the leb to store it */
  10596. *p_depth++ = (uint8)depth;
  10597. }
  10598. }
  10599. #endif
  10600. }
  10601. #if WASM_ENABLE_FAST_INTERP == 0
  10602. /* Set the tailing bytes to nop */
  10603. if (br_table_cache)
  10604. p_depth = p_depth_begin;
  10605. while (p_depth < p)
  10606. *p_depth++ = WASM_OP_NOP;
  10607. #endif
  10608. RESET_STACK();
  10609. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10610. break;
  10611. }
  10612. case WASM_OP_RETURN:
  10613. {
  10614. WASMFuncType *func_type = func->func_type;
  10615. int32 idx;
  10616. uint8 ret_type;
  10617. #if WASM_ENABLE_GC != 0
  10618. uint32 j = func_type->ref_type_map_count - 1;
  10619. #endif
  10620. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  10621. idx--) {
  10622. ret_type =
  10623. *(func_type->types + func_type->param_count + idx);
  10624. #if WASM_ENABLE_GC != 0
  10625. if (wasm_is_type_multi_byte_type(ret_type)) {
  10626. WASMRefType *ref_type =
  10627. func_type->ref_type_maps[j].ref_type;
  10628. bh_assert(func_type->ref_type_maps[j].index
  10629. == func_type->param_count + idx);
  10630. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10631. ref_type,
  10632. wasm_reftype_struct_size(ref_type));
  10633. j--;
  10634. }
  10635. #endif
  10636. #if WASM_ENABLE_FAST_INTERP != 0
  10637. /* emit the offset after return opcode */
  10638. POP_OFFSET_TYPE(ret_type);
  10639. #endif
  10640. POP_TYPE(ret_type);
  10641. }
  10642. RESET_STACK();
  10643. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10644. break;
  10645. }
  10646. case WASM_OP_CALL:
  10647. #if WASM_ENABLE_TAIL_CALL != 0
  10648. case WASM_OP_RETURN_CALL:
  10649. #endif
  10650. #if WASM_ENABLE_GC != 0
  10651. case WASM_OP_CALL_REF:
  10652. case WASM_OP_RETURN_CALL_REF:
  10653. #endif
  10654. {
  10655. WASMFuncType *func_type;
  10656. uint8 type;
  10657. int32 idx;
  10658. #if WASM_ENABLE_GC != 0
  10659. WASMRefType *ref_type;
  10660. uint32 type_idx1;
  10661. int32 j;
  10662. #endif
  10663. #if WASM_ENABLE_GC != 0
  10664. if (opcode == WASM_OP_CALL_REF
  10665. || opcode == WASM_OP_RETURN_CALL_REF) {
  10666. read_leb_uint32(p, p_end, type_idx1);
  10667. if (!check_type_index(module, module->type_count, type_idx1,
  10668. error_buf, error_buf_size)) {
  10669. goto fail;
  10670. }
  10671. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  10672. set_error_buf(error_buf, error_buf_size,
  10673. "unkown function type");
  10674. goto fail;
  10675. }
  10676. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10677. &type_idx, error_buf,
  10678. error_buf_size)) {
  10679. goto fail;
  10680. }
  10681. if (type == VALUE_TYPE_ANY) {
  10682. type_idx = type_idx1;
  10683. }
  10684. if (!check_type_index(module, module->type_count, type_idx,
  10685. error_buf, error_buf_size)) {
  10686. goto fail;
  10687. }
  10688. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10689. set_error_buf(error_buf, error_buf_size,
  10690. "unkown function type");
  10691. goto fail;
  10692. }
  10693. if (!wasm_func_type_is_super_of(
  10694. (WASMFuncType *)module->types[type_idx1],
  10695. (WASMFuncType *)module->types[type_idx])) {
  10696. set_error_buf(error_buf, error_buf_size,
  10697. "function type mismatch");
  10698. goto fail;
  10699. }
  10700. func_type = (WASMFuncType *)module->types[type_idx];
  10701. }
  10702. else
  10703. #endif
  10704. {
  10705. read_leb_uint32(p, p_end, func_idx);
  10706. #if WASM_ENABLE_FAST_INTERP != 0
  10707. /* we need to emit func_idx before arguments */
  10708. emit_uint32(loader_ctx, func_idx);
  10709. #endif
  10710. if (!check_function_index(module, func_idx, error_buf,
  10711. error_buf_size)) {
  10712. goto fail;
  10713. }
  10714. if (func_idx < module->import_function_count)
  10715. func_type = module->import_functions[func_idx]
  10716. .u.function.func_type;
  10717. else
  10718. func_type =
  10719. module
  10720. ->functions[func_idx
  10721. - module->import_function_count]
  10722. ->func_type;
  10723. }
  10724. if (func_type->param_count > 0) {
  10725. #if WASM_ENABLE_GC != 0
  10726. j = (int32)(func_type->result_ref_type_maps
  10727. - func_type->ref_type_maps - 1);
  10728. #endif
  10729. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10730. idx--) {
  10731. #if WASM_ENABLE_GC != 0
  10732. if (wasm_is_type_multi_byte_type(
  10733. func_type->types[idx])) {
  10734. ref_type = func_type->ref_type_maps[j].ref_type;
  10735. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10736. ref_type,
  10737. wasm_reftype_struct_size(ref_type));
  10738. j--;
  10739. }
  10740. #endif
  10741. #if WASM_ENABLE_FAST_INTERP != 0
  10742. POP_OFFSET_TYPE(func_type->types[idx]);
  10743. #endif
  10744. POP_TYPE(func_type->types[idx]);
  10745. }
  10746. }
  10747. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10748. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10749. #endif
  10750. #if WASM_ENABLE_GC != 0
  10751. j = (int32)(func_type->result_ref_type_maps
  10752. - func_type->ref_type_maps);
  10753. #endif
  10754. for (i = 0; i < func_type->result_count; i++) {
  10755. #if WASM_ENABLE_GC != 0
  10756. if (wasm_is_type_multi_byte_type(
  10757. func_type->types[func_type->param_count + i])) {
  10758. ref_type = func_type->ref_type_maps[j].ref_type;
  10759. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10760. ref_type,
  10761. wasm_reftype_struct_size(ref_type));
  10762. j++;
  10763. }
  10764. #endif
  10765. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10766. #if WASM_ENABLE_FAST_INTERP != 0
  10767. /* Here we emit each return value's dynamic_offset. But
  10768. * in fact these offsets are continuous, so interpreter
  10769. * only need to get the first return value's offset.
  10770. */
  10771. PUSH_OFFSET_TYPE(
  10772. func_type->types[func_type->param_count + i]);
  10773. #endif
  10774. }
  10775. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10776. }
  10777. else {
  10778. #if WASM_ENABLE_GC == 0
  10779. if (func_type->result_count
  10780. != func->func_type->result_count) {
  10781. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10782. "type mismatch: expect ",
  10783. func->func_type->result_count,
  10784. " return values but got other");
  10785. goto fail;
  10786. }
  10787. for (i = 0; i < func_type->result_count; i++) {
  10788. type = func->func_type
  10789. ->types[func->func_type->param_count + i];
  10790. if (func_type->types[func_type->param_count + i]
  10791. != type) {
  10792. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10793. "type mismatch: expect ",
  10794. type2str(type), " but got other");
  10795. goto fail;
  10796. }
  10797. }
  10798. #else
  10799. if (!wasm_func_type_result_is_subtype_of(
  10800. func_type, func->func_type, module->types,
  10801. module->type_count)) {
  10802. set_error_buf(
  10803. error_buf, error_buf_size,
  10804. "type mismatch: invalid func result types");
  10805. goto fail;
  10806. }
  10807. #endif
  10808. RESET_STACK();
  10809. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10810. }
  10811. #endif
  10812. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10813. || WASM_ENABLE_WAMR_COMPILER != 0
  10814. func->has_op_func_call = true;
  10815. #endif
  10816. (void)type;
  10817. break;
  10818. }
  10819. /*
  10820. * if disable reference type: call_indirect typeidx, 0x00
  10821. * if enable reference type: call_indirect typeidx, tableidx
  10822. */
  10823. case WASM_OP_CALL_INDIRECT:
  10824. #if WASM_ENABLE_TAIL_CALL != 0
  10825. case WASM_OP_RETURN_CALL_INDIRECT:
  10826. #endif
  10827. {
  10828. int32 idx;
  10829. WASMFuncType *func_type;
  10830. read_leb_uint32(p, p_end, type_idx);
  10831. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10832. #if WASM_ENABLE_WAMR_COMPILER != 0
  10833. if (p + 1 < p_end && *p != 0x00) {
  10834. /*
  10835. * Any non-0x00 byte requires the ref types proposal.
  10836. * This is different from checking the table_idx value
  10837. * since `0x80 0x00` etc. are all valid encodings of zero.
  10838. */
  10839. module->is_ref_types_used = true;
  10840. }
  10841. #endif
  10842. read_leb_uint32(p, p_end, table_idx);
  10843. #else
  10844. CHECK_BUF(p, p_end, 1);
  10845. table_idx = read_uint8(p);
  10846. #endif
  10847. if (!check_table_index(module, table_idx, error_buf,
  10848. error_buf_size)) {
  10849. goto fail;
  10850. }
  10851. #if WASM_ENABLE_FAST_INTERP != 0
  10852. /* we need to emit before arguments */
  10853. #if WASM_ENABLE_TAIL_CALL != 0
  10854. emit_byte(loader_ctx, opcode);
  10855. #endif
  10856. emit_uint32(loader_ctx, type_idx);
  10857. emit_uint32(loader_ctx, table_idx);
  10858. #endif
  10859. #if WASM_ENABLE_MEMORY64 != 0
  10860. table_elem_idx_type = is_table_64bit(module, table_idx)
  10861. ? VALUE_TYPE_I64
  10862. : VALUE_TYPE_I32;
  10863. #endif
  10864. /* skip elem idx */
  10865. POP_TBL_ELEM_IDX();
  10866. if (type_idx >= module->type_count) {
  10867. set_error_buf(error_buf, error_buf_size, "unknown type");
  10868. goto fail;
  10869. }
  10870. func_type = (WASMFuncType *)module->types[type_idx];
  10871. if (func_type->param_count > 0) {
  10872. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10873. idx--) {
  10874. #if WASM_ENABLE_FAST_INTERP != 0
  10875. POP_OFFSET_TYPE(func_type->types[idx]);
  10876. #endif
  10877. POP_TYPE(func_type->types[idx]);
  10878. }
  10879. }
  10880. #if WASM_ENABLE_TAIL_CALL != 0
  10881. if (opcode == WASM_OP_CALL_INDIRECT) {
  10882. #endif
  10883. for (i = 0; i < func_type->result_count; i++) {
  10884. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10885. #if WASM_ENABLE_FAST_INTERP != 0
  10886. PUSH_OFFSET_TYPE(
  10887. func_type->types[func_type->param_count + i]);
  10888. #endif
  10889. }
  10890. #if WASM_ENABLE_TAIL_CALL != 0
  10891. }
  10892. else {
  10893. uint8 type;
  10894. if (func_type->result_count
  10895. != func->func_type->result_count) {
  10896. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10897. "type mismatch: expect ",
  10898. func->func_type->result_count,
  10899. " return values but got other");
  10900. goto fail;
  10901. }
  10902. for (i = 0; i < func_type->result_count; i++) {
  10903. type = func->func_type
  10904. ->types[func->func_type->param_count + i];
  10905. if (func_type->types[func_type->param_count + i]
  10906. != type) {
  10907. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10908. "type mismatch: expect ",
  10909. type2str(type), " but got other");
  10910. goto fail;
  10911. }
  10912. }
  10913. RESET_STACK();
  10914. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10915. }
  10916. #endif
  10917. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10918. || WASM_ENABLE_WAMR_COMPILER != 0
  10919. func->has_op_func_call = true;
  10920. #endif
  10921. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10922. func->has_op_call_indirect = true;
  10923. #endif
  10924. break;
  10925. }
  10926. case WASM_OP_DROP:
  10927. {
  10928. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10929. int32 available_stack_cell =
  10930. (int32)(loader_ctx->stack_cell_num
  10931. - cur_block->stack_cell_num);
  10932. if (available_stack_cell <= 0
  10933. && !cur_block->is_stack_polymorphic) {
  10934. set_error_buf(error_buf, error_buf_size,
  10935. "type mismatch, opcode drop was found "
  10936. "but stack was empty");
  10937. goto fail;
  10938. }
  10939. if (available_stack_cell > 0) {
  10940. #if WASM_ENABLE_GC != 0
  10941. if (wasm_is_type_multi_byte_type(
  10942. *(loader_ctx->frame_ref - 1))) {
  10943. bh_assert((int32)(loader_ctx->reftype_map_num
  10944. - cur_block->reftype_map_num)
  10945. > 0);
  10946. loader_ctx->frame_reftype_map--;
  10947. loader_ctx->reftype_map_num--;
  10948. }
  10949. #endif
  10950. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  10951. loader_ctx->frame_ref--;
  10952. loader_ctx->stack_cell_num--;
  10953. #if WASM_ENABLE_FAST_INTERP != 0
  10954. skip_label();
  10955. loader_ctx->frame_offset--;
  10956. if ((*(loader_ctx->frame_offset)
  10957. > loader_ctx->start_dynamic_offset)
  10958. && (*(loader_ctx->frame_offset)
  10959. < loader_ctx->max_dynamic_offset))
  10960. loader_ctx->dynamic_offset--;
  10961. #endif
  10962. }
  10963. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10964. loader_ctx->frame_ref -= 2;
  10965. loader_ctx->stack_cell_num -= 2;
  10966. #if WASM_ENABLE_FAST_INTERP == 0
  10967. *(p - 1) = WASM_OP_DROP_64;
  10968. #endif
  10969. #if WASM_ENABLE_FAST_INTERP != 0
  10970. skip_label();
  10971. loader_ctx->frame_offset -= 2;
  10972. if ((*(loader_ctx->frame_offset)
  10973. > loader_ctx->start_dynamic_offset)
  10974. && (*(loader_ctx->frame_offset)
  10975. < loader_ctx->max_dynamic_offset))
  10976. loader_ctx->dynamic_offset -= 2;
  10977. #endif
  10978. }
  10979. #if WASM_ENABLE_SIMD != 0
  10980. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10981. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10982. loader_ctx->frame_ref -= 4;
  10983. loader_ctx->stack_cell_num -= 4;
  10984. }
  10985. #endif
  10986. #endif
  10987. else {
  10988. set_error_buf(error_buf, error_buf_size,
  10989. "type mismatch");
  10990. goto fail;
  10991. }
  10992. }
  10993. else {
  10994. #if WASM_ENABLE_FAST_INTERP != 0
  10995. skip_label();
  10996. #endif
  10997. }
  10998. break;
  10999. }
  11000. case WASM_OP_SELECT:
  11001. {
  11002. uint8 ref_type;
  11003. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11004. int32 available_stack_cell;
  11005. #if WASM_ENABLE_FAST_INTERP != 0
  11006. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11007. #endif
  11008. POP_I32();
  11009. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  11010. - cur_block->stack_cell_num);
  11011. if (available_stack_cell <= 0
  11012. && !cur_block->is_stack_polymorphic) {
  11013. set_error_buf(error_buf, error_buf_size,
  11014. "type mismatch or invalid result arity, "
  11015. "opcode select was found "
  11016. "but stack was empty");
  11017. goto fail;
  11018. }
  11019. if (available_stack_cell > 0) {
  11020. switch (*(loader_ctx->frame_ref - 1)) {
  11021. case VALUE_TYPE_I32:
  11022. case VALUE_TYPE_F32:
  11023. case VALUE_TYPE_ANY:
  11024. break;
  11025. case VALUE_TYPE_I64:
  11026. case VALUE_TYPE_F64:
  11027. #if WASM_ENABLE_FAST_INTERP == 0
  11028. *(p - 1) = WASM_OP_SELECT_64;
  11029. #endif
  11030. #if WASM_ENABLE_FAST_INTERP != 0
  11031. if (loader_ctx->p_code_compiled) {
  11032. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11033. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11034. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11035. *(void **)(p_code_compiled_tmp
  11036. - sizeof(void *)) =
  11037. handle_table[opcode_tmp];
  11038. #else
  11039. #if UINTPTR_MAX == UINT64_MAX
  11040. /* emit int32 relative offset in 64-bit target
  11041. */
  11042. int32 offset =
  11043. (int32)((uint8 *)handle_table[opcode_tmp]
  11044. - (uint8 *)handle_table[0]);
  11045. *(int32 *)(p_code_compiled_tmp
  11046. - sizeof(int32)) = offset;
  11047. #else
  11048. /* emit uint32 label address in 32-bit target */
  11049. *(uint32 *)(p_code_compiled_tmp
  11050. - sizeof(uint32)) =
  11051. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11052. #endif
  11053. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11054. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11055. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11056. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11057. #else
  11058. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11059. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11060. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11061. }
  11062. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11063. break;
  11064. #if WASM_ENABLE_SIMD != 0
  11065. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  11066. case VALUE_TYPE_V128:
  11067. break;
  11068. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  11069. #endif /* WASM_ENABLE_SIMD != 0 */
  11070. default:
  11071. {
  11072. set_error_buf(error_buf, error_buf_size,
  11073. "type mismatch");
  11074. goto fail;
  11075. }
  11076. }
  11077. ref_type = *(loader_ctx->frame_ref - 1);
  11078. #if WASM_ENABLE_FAST_INTERP != 0
  11079. POP_OFFSET_TYPE(ref_type);
  11080. POP_TYPE(ref_type);
  11081. POP_OFFSET_TYPE(ref_type);
  11082. POP_TYPE(ref_type);
  11083. PUSH_OFFSET_TYPE(ref_type);
  11084. PUSH_TYPE(ref_type);
  11085. #else
  11086. POP2_AND_PUSH(ref_type, ref_type);
  11087. #endif
  11088. }
  11089. else {
  11090. #if WASM_ENABLE_FAST_INTERP != 0
  11091. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11092. #endif
  11093. PUSH_TYPE(VALUE_TYPE_ANY);
  11094. }
  11095. break;
  11096. }
  11097. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11098. case WASM_OP_SELECT_T:
  11099. {
  11100. uint8 vec_len, type;
  11101. #if WASM_ENABLE_GC != 0
  11102. WASMRefType *ref_type = NULL;
  11103. #endif
  11104. #if WASM_ENABLE_FAST_INTERP != 0
  11105. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11106. #endif
  11107. read_leb_uint32(p, p_end, vec_len);
  11108. if (vec_len != 1) {
  11109. /* typed select must have exactly one result */
  11110. set_error_buf(error_buf, error_buf_size,
  11111. "invalid result arity");
  11112. goto fail;
  11113. }
  11114. #if WASM_ENABLE_GC == 0
  11115. CHECK_BUF(p, p_end, 1);
  11116. type = read_uint8(p);
  11117. if (!is_valid_value_type_for_interpreter(type)) {
  11118. set_error_buf(error_buf, error_buf_size,
  11119. "unknown value type");
  11120. goto fail;
  11121. }
  11122. #else
  11123. p_org = p + 1;
  11124. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11125. module->type_count, &need_ref_type_map,
  11126. &wasm_ref_type, false, error_buf,
  11127. error_buf_size)) {
  11128. goto fail;
  11129. }
  11130. type = wasm_ref_type.ref_type;
  11131. if (need_ref_type_map) {
  11132. if (!(ref_type = reftype_set_insert(
  11133. module->ref_type_set, &wasm_ref_type, error_buf,
  11134. error_buf_size))) {
  11135. goto fail;
  11136. }
  11137. }
  11138. #if WASM_ENABLE_FAST_INTERP == 0
  11139. while (p_org < p) {
  11140. #if WASM_ENABLE_DEBUG_INTERP != 0
  11141. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11142. error_buf_size)) {
  11143. goto fail;
  11144. }
  11145. #endif
  11146. /* Ignore extra bytes for interpreter */
  11147. *p_org++ = WASM_OP_NOP;
  11148. }
  11149. #endif
  11150. #endif /* end of WASM_ENABLE_GC == 0 */
  11151. POP_I32();
  11152. #if WASM_ENABLE_FAST_INTERP != 0
  11153. if (loader_ctx->p_code_compiled) {
  11154. uint8 opcode_tmp = WASM_OP_SELECT;
  11155. if (type == VALUE_TYPE_V128) {
  11156. #if (WASM_ENABLE_SIMD == 0) \
  11157. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  11158. set_error_buf(error_buf, error_buf_size,
  11159. "SIMD v128 type isn't supported");
  11160. goto fail;
  11161. #endif
  11162. }
  11163. else {
  11164. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11165. opcode_tmp = WASM_OP_SELECT_64;
  11166. #if WASM_ENABLE_GC != 0
  11167. if (wasm_is_type_reftype(type))
  11168. opcode_tmp = WASM_OP_SELECT_T;
  11169. #endif
  11170. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11171. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11172. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11173. handle_table[opcode_tmp];
  11174. #else
  11175. #if UINTPTR_MAX == UINT64_MAX
  11176. /* emit int32 relative offset in 64-bit target */
  11177. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11178. - (uint8 *)handle_table[0]);
  11179. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11180. offset;
  11181. #else
  11182. /* emit uint32 label address in 32-bit target */
  11183. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11184. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11185. #endif
  11186. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11187. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11188. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11189. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11190. #else
  11191. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11192. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11193. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11194. }
  11195. }
  11196. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11197. POP_REF(type);
  11198. #if WASM_ENABLE_GC != 0
  11199. if (need_ref_type_map) {
  11200. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11201. wasm_reftype_struct_size(ref_type));
  11202. }
  11203. #endif
  11204. POP_REF(type);
  11205. #if WASM_ENABLE_GC != 0
  11206. if (need_ref_type_map) {
  11207. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11208. wasm_reftype_struct_size(ref_type));
  11209. }
  11210. #endif
  11211. PUSH_REF(type);
  11212. #if WASM_ENABLE_WAMR_COMPILER != 0
  11213. module->is_ref_types_used = true;
  11214. #endif
  11215. (void)vec_len;
  11216. break;
  11217. }
  11218. /* table.get x. tables[x]. [it] -> [t] */
  11219. /* table.set x. tables[x]. [it t] -> [] */
  11220. case WASM_OP_TABLE_GET:
  11221. case WASM_OP_TABLE_SET:
  11222. {
  11223. uint8 decl_ref_type;
  11224. #if WASM_ENABLE_GC != 0
  11225. WASMRefType *ref_type;
  11226. #endif
  11227. read_leb_uint32(p, p_end, table_idx);
  11228. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11229. #if WASM_ENABLE_GC != 0
  11230. (void **)&ref_type,
  11231. #else
  11232. NULL,
  11233. #endif
  11234. error_buf, error_buf_size))
  11235. goto fail;
  11236. #if WASM_ENABLE_GC != 0
  11237. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11238. bh_assert(ref_type);
  11239. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11240. ref_type, wasm_reftype_struct_size(ref_type));
  11241. }
  11242. #endif
  11243. #if WASM_ENABLE_FAST_INTERP != 0
  11244. emit_uint32(loader_ctx, table_idx);
  11245. #endif
  11246. #if WASM_ENABLE_MEMORY64 != 0
  11247. table_elem_idx_type = is_table_64bit(module, table_idx)
  11248. ? VALUE_TYPE_I64
  11249. : VALUE_TYPE_I32;
  11250. #endif
  11251. if (opcode == WASM_OP_TABLE_GET) {
  11252. POP_TBL_ELEM_IDX();
  11253. #if WASM_ENABLE_FAST_INTERP != 0
  11254. PUSH_OFFSET_TYPE(decl_ref_type);
  11255. #endif
  11256. PUSH_TYPE(decl_ref_type);
  11257. }
  11258. else {
  11259. #if WASM_ENABLE_FAST_INTERP != 0
  11260. POP_OFFSET_TYPE(decl_ref_type);
  11261. #endif
  11262. POP_TYPE(decl_ref_type);
  11263. POP_TBL_ELEM_IDX();
  11264. }
  11265. #if WASM_ENABLE_WAMR_COMPILER != 0
  11266. module->is_ref_types_used = true;
  11267. #endif
  11268. break;
  11269. }
  11270. case WASM_OP_REF_NULL:
  11271. {
  11272. uint8 ref_type;
  11273. #if WASM_ENABLE_GC == 0
  11274. CHECK_BUF(p, p_end, 1);
  11275. ref_type = read_uint8(p);
  11276. if (ref_type != VALUE_TYPE_FUNCREF
  11277. && ref_type != VALUE_TYPE_EXTERNREF) {
  11278. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11279. goto fail;
  11280. }
  11281. #else
  11282. read_leb_int32(p, p_end, heap_type);
  11283. if (heap_type >= 0) {
  11284. if (!check_type_index(module, module->type_count, heap_type,
  11285. error_buf, error_buf_size)) {
  11286. goto fail;
  11287. }
  11288. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11289. true, heap_type);
  11290. ref_type = wasm_ref_type.ref_type;
  11291. }
  11292. else {
  11293. if (!wasm_is_valid_heap_type(heap_type)) {
  11294. set_error_buf(error_buf, error_buf_size,
  11295. "unknown type");
  11296. goto fail;
  11297. }
  11298. ref_type = (uint8)((int32)0x80 + heap_type);
  11299. }
  11300. #endif /* end of WASM_ENABLE_GC == 0 */
  11301. #if WASM_ENABLE_FAST_INTERP != 0
  11302. PUSH_OFFSET_TYPE(ref_type);
  11303. #endif
  11304. PUSH_TYPE(ref_type);
  11305. #if WASM_ENABLE_WAMR_COMPILER != 0
  11306. module->is_ref_types_used = true;
  11307. #endif
  11308. break;
  11309. }
  11310. case WASM_OP_REF_IS_NULL:
  11311. {
  11312. #if WASM_ENABLE_GC == 0
  11313. #if WASM_ENABLE_FAST_INTERP != 0
  11314. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11315. int32 block_stack_cell_num =
  11316. (int32)(loader_ctx->stack_cell_num
  11317. - cur_block->stack_cell_num);
  11318. if (block_stack_cell_num <= 0) {
  11319. if (!cur_block->is_stack_polymorphic) {
  11320. set_error_buf(
  11321. error_buf, error_buf_size,
  11322. "type mismatch: expect data but stack was empty");
  11323. goto fail;
  11324. }
  11325. }
  11326. else {
  11327. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11328. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11329. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11330. if (!wasm_loader_pop_frame_ref_offset(
  11331. loader_ctx, *(loader_ctx->frame_ref - 1),
  11332. error_buf, error_buf_size)) {
  11333. goto fail;
  11334. }
  11335. }
  11336. else {
  11337. set_error_buf(error_buf, error_buf_size,
  11338. "type mismatch");
  11339. goto fail;
  11340. }
  11341. }
  11342. #else
  11343. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11344. error_buf, error_buf_size)
  11345. && !wasm_loader_pop_frame_ref(loader_ctx,
  11346. VALUE_TYPE_EXTERNREF,
  11347. error_buf, error_buf_size)) {
  11348. goto fail;
  11349. }
  11350. #endif
  11351. #else /* else of WASM_ENABLE_GC == 0 */
  11352. uint8 type;
  11353. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11354. error_buf, error_buf_size)) {
  11355. goto fail;
  11356. }
  11357. #endif
  11358. PUSH_I32();
  11359. #if WASM_ENABLE_WAMR_COMPILER != 0
  11360. module->is_ref_types_used = true;
  11361. #endif
  11362. break;
  11363. }
  11364. case WASM_OP_REF_FUNC:
  11365. {
  11366. read_leb_uint32(p, p_end, func_idx);
  11367. if (!check_function_index(module, func_idx, error_buf,
  11368. error_buf_size)) {
  11369. goto fail;
  11370. }
  11371. /* Refer to a forward-declared function:
  11372. the function must be an import, exported, or present in
  11373. a table elem segment or global initializer to be used as
  11374. the operand to ref.func */
  11375. if (func_idx >= module->import_function_count) {
  11376. WASMTableSeg *table_seg = module->table_segments;
  11377. bool func_declared = false;
  11378. uint32 j;
  11379. for (i = 0; i < module->global_count; i++) {
  11380. if (module->globals[i].type.val_type
  11381. == VALUE_TYPE_FUNCREF
  11382. && module->globals[i].init_expr.init_expr_type
  11383. == INIT_EXPR_TYPE_FUNCREF_CONST
  11384. && module->globals[i].init_expr.u.u32 == func_idx) {
  11385. func_declared = true;
  11386. break;
  11387. }
  11388. }
  11389. if (!func_declared) {
  11390. /* Check whether the function is declared in table segs,
  11391. note that it doesn't matter whether the table seg's
  11392. mode is passive, active or declarative. */
  11393. for (i = 0; i < module->table_seg_count;
  11394. i++, table_seg++) {
  11395. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11396. #if WASM_ENABLE_GC != 0
  11397. /* elem type is (ref null? func) or
  11398. (ref null? $t) */
  11399. || ((table_seg->elem_type
  11400. == REF_TYPE_HT_NON_NULLABLE
  11401. || table_seg->elem_type
  11402. == REF_TYPE_HT_NULLABLE)
  11403. && (table_seg->elem_ref_type->ref_ht_common
  11404. .heap_type
  11405. == HEAP_TYPE_FUNC
  11406. || table_seg->elem_ref_type
  11407. ->ref_ht_common.heap_type
  11408. > 0))
  11409. #endif
  11410. ) {
  11411. for (j = 0; j < table_seg->value_count; j++) {
  11412. if (table_seg->init_values[j].u.ref_index
  11413. == func_idx) {
  11414. func_declared = true;
  11415. break;
  11416. }
  11417. }
  11418. }
  11419. }
  11420. }
  11421. if (!func_declared) {
  11422. /* Check whether the function is exported */
  11423. for (i = 0; i < module->export_count; i++) {
  11424. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11425. && module->exports[i].index == func_idx) {
  11426. func_declared = true;
  11427. break;
  11428. }
  11429. }
  11430. }
  11431. if (!func_declared) {
  11432. set_error_buf(error_buf, error_buf_size,
  11433. "undeclared function reference");
  11434. goto fail;
  11435. }
  11436. }
  11437. #if WASM_ENABLE_FAST_INTERP != 0
  11438. emit_uint32(loader_ctx, func_idx);
  11439. #endif
  11440. #if WASM_ENABLE_GC == 0
  11441. PUSH_FUNCREF();
  11442. #else
  11443. if (func_idx < module->import_function_count)
  11444. type_idx =
  11445. module->import_functions[func_idx].u.function.type_idx;
  11446. else
  11447. type_idx = module
  11448. ->functions[func_idx
  11449. - module->import_function_count]
  11450. ->type_idx;
  11451. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11452. false, type_idx);
  11453. PUSH_REF(wasm_ref_type.ref_type);
  11454. #endif
  11455. #if WASM_ENABLE_WAMR_COMPILER != 0
  11456. module->is_ref_types_used = true;
  11457. #endif
  11458. break;
  11459. }
  11460. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11461. #if WASM_ENABLE_GC != 0
  11462. case WASM_OP_REF_AS_NON_NULL:
  11463. case WASM_OP_BR_ON_NULL:
  11464. {
  11465. uint8 type;
  11466. WASMRefType ref_type;
  11467. /* POP (ref null ht) and get the converted (ref ht) */
  11468. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11469. error_buf, error_buf_size)) {
  11470. goto fail;
  11471. }
  11472. if (opcode == WASM_OP_BR_ON_NULL) {
  11473. if (!(frame_csp_tmp =
  11474. check_branch_block(loader_ctx, &p, p_end, opcode,
  11475. error_buf, error_buf_size))) {
  11476. goto fail;
  11477. }
  11478. #if WASM_ENABLE_FAST_INTERP != 0
  11479. disable_emit = true;
  11480. #endif
  11481. }
  11482. /* PUSH the converted (ref ht) */
  11483. if (type != VALUE_TYPE_ANY) {
  11484. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11485. sizeof(WASMRefType));
  11486. }
  11487. PUSH_REF(type);
  11488. break;
  11489. }
  11490. case WASM_OP_BR_ON_NON_NULL:
  11491. {
  11492. uint8 type;
  11493. WASMRefType ref_type;
  11494. uint32 available_stack_cell =
  11495. loader_ctx->stack_cell_num
  11496. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11497. /* POP (ref null ht) and get the converted (ref ht) */
  11498. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11499. error_buf, error_buf_size)) {
  11500. goto fail;
  11501. }
  11502. #if WASM_ENABLE_FAST_INTERP != 0
  11503. disable_emit = true;
  11504. #endif
  11505. /* Temporarily PUSH back (ref ht), check brach block and
  11506. then POP it */
  11507. if (available_stack_cell
  11508. > 0) { /* stack isn't in polymorphic state */
  11509. if (type != VALUE_TYPE_ANY) {
  11510. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11511. &ref_type, sizeof(WASMRefType));
  11512. }
  11513. PUSH_REF(type);
  11514. }
  11515. if (!(frame_csp_tmp =
  11516. check_branch_block(loader_ctx, &p, p_end, opcode,
  11517. error_buf, error_buf_size))) {
  11518. goto fail;
  11519. }
  11520. if (available_stack_cell
  11521. > 0) { /* stack isn't in polymorphic state */
  11522. POP_REF(type);
  11523. #if WASM_ENABLE_FAST_INTERP != 0
  11524. /* Erase the opnd offset emitted by POP_REF() */
  11525. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11526. #endif
  11527. }
  11528. break;
  11529. }
  11530. case WASM_OP_REF_EQ:
  11531. POP_REF(REF_TYPE_EQREF);
  11532. POP_REF(REF_TYPE_EQREF);
  11533. PUSH_I32();
  11534. break;
  11535. #endif /* end of WASM_ENABLE_GC != 0 */
  11536. case WASM_OP_GET_LOCAL:
  11537. {
  11538. p_org = p - 1;
  11539. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11540. PUSH_TYPE(local_type);
  11541. #if WASM_ENABLE_GC != 0
  11542. /* Cannot get a non-nullable and unset local */
  11543. if (local_idx >= param_count
  11544. && wasm_is_reftype_htref_non_nullable(local_type)
  11545. && !wasm_loader_get_local_status(loader_ctx,
  11546. local_idx - param_count)) {
  11547. set_error_buf(error_buf, error_buf_size,
  11548. "uninitialized local");
  11549. goto fail;
  11550. }
  11551. #endif
  11552. #if WASM_ENABLE_FAST_INTERP != 0
  11553. /* Get Local is optimized out */
  11554. skip_label();
  11555. disable_emit = true;
  11556. operand_offset = local_offset;
  11557. PUSH_OFFSET_TYPE(local_type);
  11558. #else
  11559. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11560. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11561. if (local_offset < 0x80
  11562. #if WASM_ENABLE_GC != 0
  11563. && !wasm_is_type_reftype(local_type)
  11564. #endif
  11565. ) {
  11566. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11567. if (is_32bit_type(local_type)) {
  11568. *p_org++ = (uint8)local_offset;
  11569. }
  11570. else {
  11571. *p_org++ = (uint8)(local_offset | 0x80);
  11572. }
  11573. while (p_org < p) {
  11574. *p_org++ = WASM_OP_NOP;
  11575. }
  11576. }
  11577. #endif
  11578. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11579. break;
  11580. }
  11581. case WASM_OP_SET_LOCAL:
  11582. {
  11583. p_org = p - 1;
  11584. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11585. #if WASM_ENABLE_FAST_INTERP != 0
  11586. if (!(preserve_referenced_local(
  11587. loader_ctx, opcode, local_offset, local_type,
  11588. &preserve_local, error_buf, error_buf_size)))
  11589. goto fail;
  11590. if (local_offset < 256
  11591. #if WASM_ENABLE_GC != 0
  11592. && !wasm_is_type_reftype(local_type)
  11593. #endif
  11594. ) {
  11595. skip_label();
  11596. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11597. if (loader_ctx->p_code_compiled)
  11598. STORE_U16(loader_ctx->p_code_compiled - 2,
  11599. local_offset);
  11600. loader_ctx->frame_offset--;
  11601. loader_ctx->dynamic_offset--;
  11602. }
  11603. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11604. if (loader_ctx->p_code_compiled)
  11605. STORE_U16(loader_ctx->p_code_compiled - 2,
  11606. local_offset);
  11607. loader_ctx->frame_offset -= 2;
  11608. loader_ctx->dynamic_offset -= 2;
  11609. }
  11610. else {
  11611. if (is_32bit_type(local_type)) {
  11612. emit_label(EXT_OP_SET_LOCAL_FAST);
  11613. emit_byte(loader_ctx, (uint8)local_offset);
  11614. }
  11615. else {
  11616. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11617. emit_byte(loader_ctx, (uint8)local_offset);
  11618. }
  11619. POP_OFFSET_TYPE(local_type);
  11620. }
  11621. }
  11622. else { /* local index larger than 255, reserve leb */
  11623. emit_uint32(loader_ctx, local_idx);
  11624. POP_OFFSET_TYPE(local_type);
  11625. }
  11626. #else
  11627. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11628. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11629. if (local_offset < 0x80
  11630. #if WASM_ENABLE_GC != 0
  11631. && !wasm_is_type_reftype(local_type)
  11632. #endif
  11633. ) {
  11634. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11635. if (is_32bit_type(local_type)) {
  11636. *p_org++ = (uint8)local_offset;
  11637. }
  11638. else {
  11639. *p_org++ = (uint8)(local_offset | 0x80);
  11640. }
  11641. while (p_org < p) {
  11642. *p_org++ = WASM_OP_NOP;
  11643. }
  11644. }
  11645. #endif
  11646. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11647. #if WASM_ENABLE_GC != 0
  11648. if (local_idx >= param_count) {
  11649. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11650. }
  11651. #endif
  11652. POP_TYPE(local_type);
  11653. break;
  11654. }
  11655. case WASM_OP_TEE_LOCAL:
  11656. {
  11657. p_org = p - 1;
  11658. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11659. #if WASM_ENABLE_FAST_INTERP != 0
  11660. /* If the stack is in polymorphic state, do fake pop and push on
  11661. offset stack to keep the depth of offset stack to be the
  11662. same with ref stack */
  11663. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11664. if (cur_block->is_stack_polymorphic) {
  11665. POP_OFFSET_TYPE(local_type);
  11666. PUSH_OFFSET_TYPE(local_type);
  11667. }
  11668. #endif
  11669. POP_TYPE(local_type);
  11670. PUSH_TYPE(local_type);
  11671. #if WASM_ENABLE_FAST_INTERP != 0
  11672. if (!(preserve_referenced_local(
  11673. loader_ctx, opcode, local_offset, local_type,
  11674. &preserve_local, error_buf, error_buf_size)))
  11675. goto fail;
  11676. if (local_offset < 256
  11677. #if WASM_ENABLE_GC != 0
  11678. && !wasm_is_type_reftype(local_type)
  11679. #endif
  11680. ) {
  11681. skip_label();
  11682. if (is_32bit_type(local_type)) {
  11683. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11684. emit_byte(loader_ctx, (uint8)local_offset);
  11685. }
  11686. else {
  11687. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11688. emit_byte(loader_ctx, (uint8)local_offset);
  11689. }
  11690. }
  11691. else { /* local index larger than 255, reserve leb */
  11692. emit_uint32(loader_ctx, local_idx);
  11693. }
  11694. emit_operand(loader_ctx,
  11695. *(loader_ctx->frame_offset
  11696. - wasm_value_type_cell_num(local_type)));
  11697. #else
  11698. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11699. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11700. if (local_offset < 0x80
  11701. #if WASM_ENABLE_GC != 0
  11702. && !wasm_is_type_reftype(local_type)
  11703. #endif
  11704. ) {
  11705. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11706. if (is_32bit_type(local_type)) {
  11707. *p_org++ = (uint8)local_offset;
  11708. }
  11709. else {
  11710. *p_org++ = (uint8)(local_offset | 0x80);
  11711. }
  11712. while (p_org < p) {
  11713. *p_org++ = WASM_OP_NOP;
  11714. }
  11715. }
  11716. #endif
  11717. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11718. #if WASM_ENABLE_GC != 0
  11719. if (local_idx >= param_count) {
  11720. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11721. }
  11722. #endif
  11723. break;
  11724. }
  11725. case WASM_OP_GET_GLOBAL:
  11726. {
  11727. #if WASM_ENABLE_GC != 0
  11728. WASMRefType *ref_type;
  11729. #endif
  11730. p_org = p - 1;
  11731. read_leb_uint32(p, p_end, global_idx);
  11732. if (global_idx >= global_count) {
  11733. set_error_buf(error_buf, error_buf_size, "unknown global");
  11734. goto fail;
  11735. }
  11736. global_type = global_idx < module->import_global_count
  11737. ? module->import_globals[global_idx]
  11738. .u.global.type.val_type
  11739. : module
  11740. ->globals[global_idx
  11741. - module->import_global_count]
  11742. .type.val_type;
  11743. #if WASM_ENABLE_GC != 0
  11744. ref_type =
  11745. global_idx < module->import_global_count
  11746. ? module->import_globals[global_idx].u.global.ref_type
  11747. : module
  11748. ->globals[global_idx
  11749. - module->import_global_count]
  11750. .ref_type;
  11751. if (wasm_is_type_multi_byte_type(global_type)) {
  11752. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11753. wasm_reftype_struct_size(ref_type));
  11754. }
  11755. #endif
  11756. PUSH_TYPE(global_type);
  11757. #if WASM_ENABLE_FAST_INTERP == 0
  11758. if (global_type == VALUE_TYPE_I64
  11759. || global_type == VALUE_TYPE_F64) {
  11760. #if WASM_ENABLE_DEBUG_INTERP != 0
  11761. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11762. error_buf_size)) {
  11763. goto fail;
  11764. }
  11765. #endif
  11766. *p_org = WASM_OP_GET_GLOBAL_64;
  11767. }
  11768. #else /* else of WASM_ENABLE_FAST_INTERP */
  11769. if (global_type == VALUE_TYPE_I64
  11770. || global_type == VALUE_TYPE_F64) {
  11771. skip_label();
  11772. emit_label(WASM_OP_GET_GLOBAL_64);
  11773. }
  11774. emit_uint32(loader_ctx, global_idx);
  11775. PUSH_OFFSET_TYPE(global_type);
  11776. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11777. break;
  11778. }
  11779. case WASM_OP_SET_GLOBAL:
  11780. {
  11781. bool is_mutable = false;
  11782. #if WASM_ENABLE_GC != 0
  11783. WASMRefType *ref_type;
  11784. #endif
  11785. p_org = p - 1;
  11786. read_leb_uint32(p, p_end, global_idx);
  11787. if (global_idx >= global_count) {
  11788. set_error_buf(error_buf, error_buf_size, "unknown global");
  11789. goto fail;
  11790. }
  11791. is_mutable = global_idx < module->import_global_count
  11792. ? module->import_globals[global_idx]
  11793. .u.global.type.is_mutable
  11794. : module
  11795. ->globals[global_idx
  11796. - module->import_global_count]
  11797. .type.is_mutable;
  11798. if (!is_mutable) {
  11799. #if WASM_ENABLE_GC == 0
  11800. set_error_buf(error_buf, error_buf_size,
  11801. "global is immutable");
  11802. #else
  11803. set_error_buf(error_buf, error_buf_size,
  11804. "immutable global");
  11805. #endif
  11806. goto fail;
  11807. }
  11808. global_type = global_idx < module->import_global_count
  11809. ? module->import_globals[global_idx]
  11810. .u.global.type.val_type
  11811. : module
  11812. ->globals[global_idx
  11813. - module->import_global_count]
  11814. .type.val_type;
  11815. #if WASM_ENABLE_GC != 0
  11816. ref_type =
  11817. global_idx < module->import_global_count
  11818. ? module->import_globals[global_idx].u.global.ref_type
  11819. : module
  11820. ->globals[global_idx
  11821. - module->import_global_count]
  11822. .ref_type;
  11823. if (wasm_is_type_multi_byte_type(global_type)) {
  11824. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11825. wasm_reftype_struct_size(ref_type));
  11826. }
  11827. #endif
  11828. #if WASM_ENABLE_FAST_INTERP == 0
  11829. if (global_type == VALUE_TYPE_I64
  11830. || global_type == VALUE_TYPE_F64) {
  11831. #if WASM_ENABLE_DEBUG_INTERP != 0
  11832. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11833. error_buf_size)) {
  11834. goto fail;
  11835. }
  11836. #endif
  11837. *p_org = WASM_OP_SET_GLOBAL_64;
  11838. }
  11839. else if (module->aux_stack_size > 0
  11840. && global_idx == module->aux_stack_top_global_index) {
  11841. #if WASM_ENABLE_DEBUG_INTERP != 0
  11842. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11843. error_buf_size)) {
  11844. goto fail;
  11845. }
  11846. #endif
  11847. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11848. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11849. func->has_op_set_global_aux_stack = true;
  11850. #endif
  11851. }
  11852. #else /* else of WASM_ENABLE_FAST_INTERP */
  11853. if (global_type == VALUE_TYPE_I64
  11854. || global_type == VALUE_TYPE_F64) {
  11855. skip_label();
  11856. emit_label(WASM_OP_SET_GLOBAL_64);
  11857. }
  11858. else if (module->aux_stack_size > 0
  11859. && global_idx == module->aux_stack_top_global_index) {
  11860. skip_label();
  11861. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11862. }
  11863. emit_uint32(loader_ctx, global_idx);
  11864. POP_OFFSET_TYPE(global_type);
  11865. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11866. POP_TYPE(global_type);
  11867. break;
  11868. }
  11869. /* load */
  11870. case WASM_OP_I32_LOAD:
  11871. case WASM_OP_I32_LOAD8_S:
  11872. case WASM_OP_I32_LOAD8_U:
  11873. case WASM_OP_I32_LOAD16_S:
  11874. case WASM_OP_I32_LOAD16_U:
  11875. case WASM_OP_I64_LOAD:
  11876. case WASM_OP_I64_LOAD8_S:
  11877. case WASM_OP_I64_LOAD8_U:
  11878. case WASM_OP_I64_LOAD16_S:
  11879. case WASM_OP_I64_LOAD16_U:
  11880. case WASM_OP_I64_LOAD32_S:
  11881. case WASM_OP_I64_LOAD32_U:
  11882. case WASM_OP_F32_LOAD:
  11883. case WASM_OP_F64_LOAD:
  11884. /* store */
  11885. case WASM_OP_I32_STORE:
  11886. case WASM_OP_I32_STORE8:
  11887. case WASM_OP_I32_STORE16:
  11888. case WASM_OP_I64_STORE:
  11889. case WASM_OP_I64_STORE8:
  11890. case WASM_OP_I64_STORE16:
  11891. case WASM_OP_I64_STORE32:
  11892. case WASM_OP_F32_STORE:
  11893. case WASM_OP_F64_STORE:
  11894. {
  11895. #if WASM_ENABLE_FAST_INTERP != 0
  11896. /* change F32/F64 into I32/I64 */
  11897. if (opcode == WASM_OP_F32_LOAD) {
  11898. skip_label();
  11899. emit_label(WASM_OP_I32_LOAD);
  11900. }
  11901. else if (opcode == WASM_OP_F64_LOAD) {
  11902. skip_label();
  11903. emit_label(WASM_OP_I64_LOAD);
  11904. }
  11905. else if (opcode == WASM_OP_F32_STORE) {
  11906. skip_label();
  11907. emit_label(WASM_OP_I32_STORE);
  11908. }
  11909. else if (opcode == WASM_OP_F64_STORE) {
  11910. skip_label();
  11911. emit_label(WASM_OP_I64_STORE);
  11912. }
  11913. #endif
  11914. CHECK_MEMORY();
  11915. read_leb_memarg(p, p_end, align); /* align */
  11916. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  11917. if (!check_memory_access_align(opcode, align, error_buf,
  11918. error_buf_size)) {
  11919. goto fail;
  11920. }
  11921. #if WASM_ENABLE_FAST_INTERP != 0
  11922. emit_uint32(loader_ctx, mem_offset);
  11923. #endif
  11924. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11925. func->has_memory_operations = true;
  11926. #endif
  11927. switch (opcode) {
  11928. /* load */
  11929. case WASM_OP_I32_LOAD:
  11930. case WASM_OP_I32_LOAD8_S:
  11931. case WASM_OP_I32_LOAD8_U:
  11932. case WASM_OP_I32_LOAD16_S:
  11933. case WASM_OP_I32_LOAD16_U:
  11934. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  11935. break;
  11936. case WASM_OP_I64_LOAD:
  11937. case WASM_OP_I64_LOAD8_S:
  11938. case WASM_OP_I64_LOAD8_U:
  11939. case WASM_OP_I64_LOAD16_S:
  11940. case WASM_OP_I64_LOAD16_U:
  11941. case WASM_OP_I64_LOAD32_S:
  11942. case WASM_OP_I64_LOAD32_U:
  11943. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  11944. break;
  11945. case WASM_OP_F32_LOAD:
  11946. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  11947. break;
  11948. case WASM_OP_F64_LOAD:
  11949. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  11950. break;
  11951. /* store */
  11952. case WASM_OP_I32_STORE:
  11953. case WASM_OP_I32_STORE8:
  11954. case WASM_OP_I32_STORE16:
  11955. POP_I32();
  11956. POP_MEM_OFFSET();
  11957. break;
  11958. case WASM_OP_I64_STORE:
  11959. case WASM_OP_I64_STORE8:
  11960. case WASM_OP_I64_STORE16:
  11961. case WASM_OP_I64_STORE32:
  11962. POP_I64();
  11963. POP_MEM_OFFSET();
  11964. break;
  11965. case WASM_OP_F32_STORE:
  11966. POP_F32();
  11967. POP_MEM_OFFSET();
  11968. break;
  11969. case WASM_OP_F64_STORE:
  11970. POP_F64();
  11971. POP_MEM_OFFSET();
  11972. break;
  11973. default:
  11974. break;
  11975. }
  11976. break;
  11977. }
  11978. case WASM_OP_MEMORY_SIZE:
  11979. CHECK_MEMORY();
  11980. read_leb_uint32(p, p_end, memidx);
  11981. check_memidx(module, memidx);
  11982. PUSH_PAGE_COUNT();
  11983. module->possible_memory_grow = true;
  11984. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11985. func->has_memory_operations = true;
  11986. #endif
  11987. break;
  11988. case WASM_OP_MEMORY_GROW:
  11989. CHECK_MEMORY();
  11990. read_leb_uint32(p, p_end, memidx);
  11991. check_memidx(module, memidx);
  11992. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  11993. module->possible_memory_grow = true;
  11994. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11995. || WASM_ENABLE_WAMR_COMPILER != 0
  11996. func->has_op_memory_grow = true;
  11997. #endif
  11998. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11999. func->has_memory_operations = true;
  12000. #endif
  12001. break;
  12002. case WASM_OP_I32_CONST:
  12003. read_leb_int32(p, p_end, i32_const);
  12004. #if WASM_ENABLE_FAST_INTERP != 0
  12005. skip_label();
  12006. disable_emit = true;
  12007. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  12008. if (operand_offset == 0) {
  12009. disable_emit = false;
  12010. emit_label(WASM_OP_I32_CONST);
  12011. emit_uint32(loader_ctx, i32_const);
  12012. }
  12013. #else
  12014. (void)i32_const;
  12015. #endif
  12016. PUSH_I32();
  12017. break;
  12018. case WASM_OP_I64_CONST:
  12019. read_leb_int64(p, p_end, i64_const);
  12020. #if WASM_ENABLE_FAST_INTERP != 0
  12021. skip_label();
  12022. disable_emit = true;
  12023. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  12024. if (operand_offset == 0) {
  12025. disable_emit = false;
  12026. emit_label(WASM_OP_I64_CONST);
  12027. emit_uint64(loader_ctx, i64_const);
  12028. }
  12029. #endif
  12030. PUSH_I64();
  12031. break;
  12032. case WASM_OP_F32_CONST:
  12033. CHECK_BUF(p, p_end, sizeof(float32));
  12034. p += sizeof(float32);
  12035. #if WASM_ENABLE_FAST_INTERP != 0
  12036. skip_label();
  12037. disable_emit = true;
  12038. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12039. sizeof(float32));
  12040. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12041. if (operand_offset == 0) {
  12042. disable_emit = false;
  12043. emit_label(WASM_OP_F32_CONST);
  12044. emit_float32(loader_ctx, f32_const);
  12045. }
  12046. #endif
  12047. PUSH_F32();
  12048. break;
  12049. case WASM_OP_F64_CONST:
  12050. CHECK_BUF(p, p_end, sizeof(float64));
  12051. p += sizeof(float64);
  12052. #if WASM_ENABLE_FAST_INTERP != 0
  12053. skip_label();
  12054. disable_emit = true;
  12055. /* Some MCU may require 8-byte align */
  12056. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12057. sizeof(float64));
  12058. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12059. if (operand_offset == 0) {
  12060. disable_emit = false;
  12061. emit_label(WASM_OP_F64_CONST);
  12062. emit_float64(loader_ctx, f64_const);
  12063. }
  12064. #endif
  12065. PUSH_F64();
  12066. break;
  12067. case WASM_OP_I32_EQZ:
  12068. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12069. break;
  12070. case WASM_OP_I32_EQ:
  12071. case WASM_OP_I32_NE:
  12072. case WASM_OP_I32_LT_S:
  12073. case WASM_OP_I32_LT_U:
  12074. case WASM_OP_I32_GT_S:
  12075. case WASM_OP_I32_GT_U:
  12076. case WASM_OP_I32_LE_S:
  12077. case WASM_OP_I32_LE_U:
  12078. case WASM_OP_I32_GE_S:
  12079. case WASM_OP_I32_GE_U:
  12080. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12081. break;
  12082. case WASM_OP_I64_EQZ:
  12083. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12084. break;
  12085. case WASM_OP_I64_EQ:
  12086. case WASM_OP_I64_NE:
  12087. case WASM_OP_I64_LT_S:
  12088. case WASM_OP_I64_LT_U:
  12089. case WASM_OP_I64_GT_S:
  12090. case WASM_OP_I64_GT_U:
  12091. case WASM_OP_I64_LE_S:
  12092. case WASM_OP_I64_LE_U:
  12093. case WASM_OP_I64_GE_S:
  12094. case WASM_OP_I64_GE_U:
  12095. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12096. break;
  12097. case WASM_OP_F32_EQ:
  12098. case WASM_OP_F32_NE:
  12099. case WASM_OP_F32_LT:
  12100. case WASM_OP_F32_GT:
  12101. case WASM_OP_F32_LE:
  12102. case WASM_OP_F32_GE:
  12103. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12104. break;
  12105. case WASM_OP_F64_EQ:
  12106. case WASM_OP_F64_NE:
  12107. case WASM_OP_F64_LT:
  12108. case WASM_OP_F64_GT:
  12109. case WASM_OP_F64_LE:
  12110. case WASM_OP_F64_GE:
  12111. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12112. break;
  12113. case WASM_OP_I32_CLZ:
  12114. case WASM_OP_I32_CTZ:
  12115. case WASM_OP_I32_POPCNT:
  12116. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12117. break;
  12118. case WASM_OP_I32_ADD:
  12119. case WASM_OP_I32_SUB:
  12120. case WASM_OP_I32_MUL:
  12121. case WASM_OP_I32_DIV_S:
  12122. case WASM_OP_I32_DIV_U:
  12123. case WASM_OP_I32_REM_S:
  12124. case WASM_OP_I32_REM_U:
  12125. case WASM_OP_I32_AND:
  12126. case WASM_OP_I32_OR:
  12127. case WASM_OP_I32_XOR:
  12128. case WASM_OP_I32_SHL:
  12129. case WASM_OP_I32_SHR_S:
  12130. case WASM_OP_I32_SHR_U:
  12131. case WASM_OP_I32_ROTL:
  12132. case WASM_OP_I32_ROTR:
  12133. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12134. break;
  12135. case WASM_OP_I64_CLZ:
  12136. case WASM_OP_I64_CTZ:
  12137. case WASM_OP_I64_POPCNT:
  12138. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12139. break;
  12140. case WASM_OP_I64_ADD:
  12141. case WASM_OP_I64_SUB:
  12142. case WASM_OP_I64_MUL:
  12143. case WASM_OP_I64_DIV_S:
  12144. case WASM_OP_I64_DIV_U:
  12145. case WASM_OP_I64_REM_S:
  12146. case WASM_OP_I64_REM_U:
  12147. case WASM_OP_I64_AND:
  12148. case WASM_OP_I64_OR:
  12149. case WASM_OP_I64_XOR:
  12150. case WASM_OP_I64_SHL:
  12151. case WASM_OP_I64_SHR_S:
  12152. case WASM_OP_I64_SHR_U:
  12153. case WASM_OP_I64_ROTL:
  12154. case WASM_OP_I64_ROTR:
  12155. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12156. break;
  12157. case WASM_OP_F32_ABS:
  12158. case WASM_OP_F32_NEG:
  12159. case WASM_OP_F32_CEIL:
  12160. case WASM_OP_F32_FLOOR:
  12161. case WASM_OP_F32_TRUNC:
  12162. case WASM_OP_F32_NEAREST:
  12163. case WASM_OP_F32_SQRT:
  12164. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12165. break;
  12166. case WASM_OP_F32_ADD:
  12167. case WASM_OP_F32_SUB:
  12168. case WASM_OP_F32_MUL:
  12169. case WASM_OP_F32_DIV:
  12170. case WASM_OP_F32_MIN:
  12171. case WASM_OP_F32_MAX:
  12172. case WASM_OP_F32_COPYSIGN:
  12173. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12174. break;
  12175. case WASM_OP_F64_ABS:
  12176. case WASM_OP_F64_NEG:
  12177. case WASM_OP_F64_CEIL:
  12178. case WASM_OP_F64_FLOOR:
  12179. case WASM_OP_F64_TRUNC:
  12180. case WASM_OP_F64_NEAREST:
  12181. case WASM_OP_F64_SQRT:
  12182. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12183. break;
  12184. case WASM_OP_F64_ADD:
  12185. case WASM_OP_F64_SUB:
  12186. case WASM_OP_F64_MUL:
  12187. case WASM_OP_F64_DIV:
  12188. case WASM_OP_F64_MIN:
  12189. case WASM_OP_F64_MAX:
  12190. case WASM_OP_F64_COPYSIGN:
  12191. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12192. break;
  12193. case WASM_OP_I32_WRAP_I64:
  12194. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12195. break;
  12196. case WASM_OP_I32_TRUNC_S_F32:
  12197. case WASM_OP_I32_TRUNC_U_F32:
  12198. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12199. break;
  12200. case WASM_OP_I32_TRUNC_S_F64:
  12201. case WASM_OP_I32_TRUNC_U_F64:
  12202. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12203. break;
  12204. case WASM_OP_I64_EXTEND_S_I32:
  12205. case WASM_OP_I64_EXTEND_U_I32:
  12206. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12207. break;
  12208. case WASM_OP_I64_TRUNC_S_F32:
  12209. case WASM_OP_I64_TRUNC_U_F32:
  12210. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12211. break;
  12212. case WASM_OP_I64_TRUNC_S_F64:
  12213. case WASM_OP_I64_TRUNC_U_F64:
  12214. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12215. break;
  12216. case WASM_OP_F32_CONVERT_S_I32:
  12217. case WASM_OP_F32_CONVERT_U_I32:
  12218. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12219. break;
  12220. case WASM_OP_F32_CONVERT_S_I64:
  12221. case WASM_OP_F32_CONVERT_U_I64:
  12222. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12223. break;
  12224. case WASM_OP_F32_DEMOTE_F64:
  12225. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12226. break;
  12227. case WASM_OP_F64_CONVERT_S_I32:
  12228. case WASM_OP_F64_CONVERT_U_I32:
  12229. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12230. break;
  12231. case WASM_OP_F64_CONVERT_S_I64:
  12232. case WASM_OP_F64_CONVERT_U_I64:
  12233. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12234. break;
  12235. case WASM_OP_F64_PROMOTE_F32:
  12236. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12237. break;
  12238. case WASM_OP_I32_REINTERPRET_F32:
  12239. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12240. break;
  12241. case WASM_OP_I64_REINTERPRET_F64:
  12242. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12243. break;
  12244. case WASM_OP_F32_REINTERPRET_I32:
  12245. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12246. break;
  12247. case WASM_OP_F64_REINTERPRET_I64:
  12248. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12249. break;
  12250. case WASM_OP_I32_EXTEND8_S:
  12251. case WASM_OP_I32_EXTEND16_S:
  12252. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12253. break;
  12254. case WASM_OP_I64_EXTEND8_S:
  12255. case WASM_OP_I64_EXTEND16_S:
  12256. case WASM_OP_I64_EXTEND32_S:
  12257. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12258. break;
  12259. #if WASM_ENABLE_GC != 0
  12260. case WASM_OP_GC_PREFIX:
  12261. {
  12262. uint32 opcode1;
  12263. read_leb_uint32(p, p_end, opcode1);
  12264. #if WASM_ENABLE_FAST_INTERP != 0
  12265. emit_byte(loader_ctx, ((uint8)opcode1));
  12266. #endif
  12267. switch (opcode1) {
  12268. case WASM_OP_STRUCT_NEW:
  12269. case WASM_OP_STRUCT_NEW_DEFAULT:
  12270. {
  12271. read_leb_uint32(p, p_end, type_idx);
  12272. #if WASM_ENABLE_FAST_INTERP != 0
  12273. emit_uint32(loader_ctx, type_idx);
  12274. #endif
  12275. if (!check_type_index(module, module->type_count,
  12276. type_idx, error_buf,
  12277. error_buf_size)) {
  12278. goto fail;
  12279. }
  12280. if (module->types[type_idx]->type_flag
  12281. != WASM_TYPE_STRUCT) {
  12282. set_error_buf(error_buf, error_buf_size,
  12283. "unkown struct type");
  12284. goto fail;
  12285. }
  12286. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12287. int32 j, k;
  12288. uint8 value_type;
  12289. uint32 ref_type_struct_size;
  12290. WASMStructType *struct_type =
  12291. (WASMStructType *)module->types[type_idx];
  12292. k = struct_type->ref_type_map_count - 1;
  12293. for (j = struct_type->field_count - 1; j >= 0;
  12294. j--) {
  12295. value_type = struct_type->fields[j].field_type;
  12296. if (wasm_is_type_reftype(value_type)) {
  12297. if (wasm_is_type_multi_byte_type(
  12298. value_type)) {
  12299. ref_type_struct_size =
  12300. wasm_reftype_struct_size(
  12301. struct_type->ref_type_maps[k]
  12302. .ref_type);
  12303. bh_memcpy_s(
  12304. &wasm_ref_type,
  12305. (uint32)sizeof(WASMRefType),
  12306. struct_type->ref_type_maps[k]
  12307. .ref_type,
  12308. ref_type_struct_size);
  12309. k--;
  12310. }
  12311. POP_REF(value_type);
  12312. }
  12313. else {
  12314. switch (value_type) {
  12315. case VALUE_TYPE_I32:
  12316. case PACKED_TYPE_I8:
  12317. case PACKED_TYPE_I16:
  12318. POP_I32();
  12319. break;
  12320. case VALUE_TYPE_I64:
  12321. POP_I64();
  12322. break;
  12323. case VALUE_TYPE_F32:
  12324. POP_F32();
  12325. break;
  12326. case VALUE_TYPE_F64:
  12327. POP_F64();
  12328. break;
  12329. default:
  12330. set_error_buf(error_buf,
  12331. error_buf_size,
  12332. "unknown type");
  12333. goto fail;
  12334. }
  12335. }
  12336. }
  12337. }
  12338. /* PUSH struct obj, (ref $t) */
  12339. wasm_set_refheaptype_typeidx(
  12340. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12341. PUSH_REF(wasm_ref_type.ref_type);
  12342. break;
  12343. }
  12344. case WASM_OP_STRUCT_GET:
  12345. case WASM_OP_STRUCT_GET_S:
  12346. case WASM_OP_STRUCT_GET_U:
  12347. case WASM_OP_STRUCT_SET:
  12348. {
  12349. WASMStructType *struct_type;
  12350. WASMRefType *ref_type = NULL;
  12351. uint32 field_idx;
  12352. uint8 field_type;
  12353. read_leb_uint32(p, p_end, type_idx);
  12354. #if WASM_ENABLE_FAST_INTERP != 0
  12355. emit_uint32(loader_ctx, type_idx);
  12356. #endif
  12357. if (!check_type_index(module, module->type_count,
  12358. type_idx, error_buf,
  12359. error_buf_size)) {
  12360. goto fail;
  12361. }
  12362. if (module->types[type_idx]->type_flag
  12363. != WASM_TYPE_STRUCT) {
  12364. set_error_buf(error_buf, error_buf_size,
  12365. "unknown struct type");
  12366. goto fail;
  12367. }
  12368. struct_type = (WASMStructType *)module->types[type_idx];
  12369. read_leb_uint32(p, p_end, field_idx);
  12370. #if WASM_ENABLE_FAST_INTERP != 0
  12371. emit_uint32(loader_ctx, field_idx);
  12372. #endif
  12373. if (field_idx >= struct_type->field_count) {
  12374. set_error_buf(error_buf, error_buf_size,
  12375. "unknown struct field");
  12376. goto fail;
  12377. }
  12378. if (opcode1 == WASM_OP_STRUCT_SET
  12379. && !(struct_type->fields[field_idx].field_flags
  12380. & 1)) {
  12381. set_error_buf(error_buf, error_buf_size,
  12382. "field is immutable");
  12383. goto fail;
  12384. }
  12385. field_type = struct_type->fields[field_idx].field_type;
  12386. if (is_packed_type(field_type)) {
  12387. if (opcode1 == WASM_OP_STRUCT_GET) {
  12388. set_error_buf(error_buf, error_buf_size,
  12389. "type mismatch");
  12390. goto fail;
  12391. }
  12392. else {
  12393. field_type = VALUE_TYPE_I32;
  12394. }
  12395. }
  12396. if (wasm_is_type_multi_byte_type(field_type)) {
  12397. ref_type = wasm_reftype_map_find(
  12398. struct_type->ref_type_maps,
  12399. struct_type->ref_type_map_count, field_idx);
  12400. bh_assert(ref_type);
  12401. }
  12402. if (opcode1 == WASM_OP_STRUCT_SET) {
  12403. /* POP field */
  12404. if (wasm_is_type_multi_byte_type(field_type)) {
  12405. bh_memcpy_s(&wasm_ref_type,
  12406. (uint32)sizeof(WASMRefType),
  12407. ref_type,
  12408. wasm_reftype_struct_size(ref_type));
  12409. }
  12410. POP_REF(field_type);
  12411. /* POP struct obj, (ref null $t) */
  12412. wasm_set_refheaptype_typeidx(
  12413. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12414. POP_REF(wasm_ref_type.ref_type);
  12415. }
  12416. else {
  12417. /* POP struct obj, (ref null $t) */
  12418. wasm_set_refheaptype_typeidx(
  12419. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12420. POP_REF(wasm_ref_type.ref_type);
  12421. /* PUSH field */
  12422. if (wasm_is_type_multi_byte_type(field_type)) {
  12423. bh_memcpy_s(&wasm_ref_type,
  12424. (uint32)sizeof(WASMRefType),
  12425. ref_type,
  12426. wasm_reftype_struct_size(ref_type));
  12427. }
  12428. PUSH_REF(field_type);
  12429. }
  12430. break;
  12431. }
  12432. case WASM_OP_ARRAY_NEW:
  12433. case WASM_OP_ARRAY_NEW_DEFAULT:
  12434. case WASM_OP_ARRAY_NEW_FIXED:
  12435. case WASM_OP_ARRAY_NEW_DATA:
  12436. case WASM_OP_ARRAY_NEW_ELEM:
  12437. {
  12438. WASMArrayType *array_type;
  12439. uint8 elem_type;
  12440. uint32 u32 = 0;
  12441. read_leb_uint32(p, p_end, type_idx);
  12442. #if WASM_ENABLE_FAST_INTERP != 0
  12443. emit_uint32(loader_ctx, type_idx);
  12444. #endif
  12445. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12446. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12447. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12448. read_leb_uint32(p, p_end, u32);
  12449. #if WASM_ENABLE_FAST_INTERP != 0
  12450. emit_uint32(loader_ctx, u32);
  12451. #endif
  12452. }
  12453. if (!check_array_type(module, type_idx, error_buf,
  12454. error_buf_size)) {
  12455. goto fail;
  12456. }
  12457. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12458. /* length */
  12459. POP_I32();
  12460. }
  12461. array_type = (WASMArrayType *)module->types[type_idx];
  12462. elem_type = array_type->elem_type;
  12463. if (opcode1 == WASM_OP_ARRAY_NEW
  12464. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12465. if (wasm_is_type_multi_byte_type(elem_type)) {
  12466. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12467. array_type->elem_ref_type,
  12468. wasm_reftype_struct_size(
  12469. array_type->elem_ref_type));
  12470. }
  12471. if (is_packed_type(elem_type)) {
  12472. elem_type = VALUE_TYPE_I32;
  12473. }
  12474. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12475. uint32 N = u32;
  12476. for (i = 0; i < N; i++) {
  12477. if (wasm_is_type_multi_byte_type(
  12478. elem_type)) {
  12479. bh_memcpy_s(
  12480. &wasm_ref_type, sizeof(WASMRefType),
  12481. array_type->elem_ref_type,
  12482. wasm_reftype_struct_size(
  12483. array_type->elem_ref_type));
  12484. }
  12485. POP_REF(elem_type);
  12486. }
  12487. }
  12488. else
  12489. POP_REF(elem_type);
  12490. }
  12491. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12492. /* offset of data segment */
  12493. POP_I32();
  12494. if (u32 >= module->data_seg_count) {
  12495. set_error_buf(error_buf, error_buf_size,
  12496. "unknown data segment");
  12497. goto fail;
  12498. }
  12499. if (wasm_is_type_reftype(elem_type)) {
  12500. set_error_buf(error_buf, error_buf_size,
  12501. "array elem type mismatch");
  12502. goto fail;
  12503. }
  12504. }
  12505. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12506. WASMTableSeg *table_seg =
  12507. module->table_segments + u32;
  12508. /* offset of element segment */
  12509. POP_I32();
  12510. if (u32 >= module->table_seg_count) {
  12511. set_error_buf(error_buf, error_buf_size,
  12512. "unknown element segment");
  12513. goto fail;
  12514. }
  12515. if (!wasm_reftype_is_subtype_of(
  12516. table_seg->elem_type,
  12517. table_seg->elem_ref_type, elem_type,
  12518. array_type->elem_ref_type, module->types,
  12519. module->type_count)) {
  12520. set_error_buf(error_buf, error_buf_size,
  12521. "array elem type mismatch");
  12522. goto fail;
  12523. }
  12524. }
  12525. /* PUSH array obj, (ref $t) */
  12526. wasm_set_refheaptype_typeidx(
  12527. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12528. PUSH_REF(wasm_ref_type.ref_type);
  12529. break;
  12530. }
  12531. case WASM_OP_ARRAY_GET:
  12532. case WASM_OP_ARRAY_GET_S:
  12533. case WASM_OP_ARRAY_GET_U:
  12534. case WASM_OP_ARRAY_SET:
  12535. {
  12536. uint8 elem_type;
  12537. WASMArrayType *array_type;
  12538. WASMRefType *ref_type = NULL;
  12539. read_leb_uint32(p, p_end, type_idx);
  12540. #if WASM_ENABLE_FAST_INTERP != 0
  12541. emit_uint32(loader_ctx, type_idx);
  12542. #endif
  12543. if (!check_array_type(module, type_idx, error_buf,
  12544. error_buf_size)) {
  12545. goto fail;
  12546. }
  12547. array_type = (WASMArrayType *)module->types[type_idx];
  12548. if (opcode1 == WASM_OP_ARRAY_SET
  12549. && !(array_type->elem_flags & 1)) {
  12550. set_error_buf(error_buf, error_buf_size,
  12551. "array is immutable");
  12552. goto fail;
  12553. }
  12554. elem_type = array_type->elem_type;
  12555. if (is_packed_type(elem_type)) {
  12556. if (opcode1 != WASM_OP_ARRAY_GET_S
  12557. && opcode1 != WASM_OP_ARRAY_GET_U
  12558. && opcode1 != WASM_OP_ARRAY_SET) {
  12559. set_error_buf(error_buf, error_buf_size,
  12560. "type mismatch");
  12561. goto fail;
  12562. }
  12563. else {
  12564. elem_type = VALUE_TYPE_I32;
  12565. }
  12566. }
  12567. ref_type = array_type->elem_ref_type;
  12568. if (opcode1 == WASM_OP_ARRAY_SET) {
  12569. /* POP elem to set */
  12570. if (wasm_is_type_multi_byte_type(elem_type)) {
  12571. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12572. ref_type,
  12573. wasm_reftype_struct_size(ref_type));
  12574. }
  12575. POP_REF(elem_type);
  12576. }
  12577. /* elem idx */
  12578. POP_I32();
  12579. /* POP array obj, (ref null $t) */
  12580. wasm_set_refheaptype_typeidx(
  12581. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12582. POP_REF(wasm_ref_type.ref_type);
  12583. if (opcode1 != WASM_OP_ARRAY_SET) {
  12584. /* PUSH elem */
  12585. if (wasm_is_type_multi_byte_type(elem_type)) {
  12586. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12587. ref_type,
  12588. wasm_reftype_struct_size(ref_type));
  12589. }
  12590. PUSH_REF(elem_type);
  12591. }
  12592. break;
  12593. }
  12594. case WASM_OP_ARRAY_LEN:
  12595. {
  12596. POP_REF(REF_TYPE_ARRAYREF);
  12597. /* length */
  12598. PUSH_I32();
  12599. break;
  12600. }
  12601. case WASM_OP_ARRAY_FILL:
  12602. {
  12603. WASMArrayType *array_type;
  12604. uint8 elem_type;
  12605. /* typeidx */
  12606. read_leb_uint32(p, p_end, type_idx);
  12607. #if WASM_ENABLE_FAST_INTERP != 0
  12608. emit_uint32(loader_ctx, type_idx);
  12609. #endif
  12610. if (!check_array_type(module, type_idx, error_buf,
  12611. error_buf_size)) {
  12612. goto fail;
  12613. }
  12614. array_type = (WASMArrayType *)module->types[type_idx];
  12615. if (!(array_type->elem_flags & 1)) {
  12616. set_error_buf(error_buf, error_buf_size,
  12617. "array is immutable");
  12618. goto fail;
  12619. }
  12620. elem_type = array_type->elem_type;
  12621. if (is_packed_type(elem_type)) {
  12622. elem_type = VALUE_TYPE_I32;
  12623. }
  12624. POP_I32(); /* length */
  12625. #if WASM_ENABLE_FAST_INTERP != 0
  12626. POP_OFFSET_TYPE(elem_type);
  12627. #endif
  12628. POP_TYPE(elem_type);
  12629. POP_I32(); /* start */
  12630. /* POP array obj, (ref null $t) */
  12631. wasm_set_refheaptype_typeidx(
  12632. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12633. POP_REF(wasm_ref_type.ref_type);
  12634. break;
  12635. }
  12636. case WASM_OP_ARRAY_COPY:
  12637. {
  12638. uint32 src_type_idx;
  12639. uint8 src_elem_type, dst_elem_type;
  12640. WASMRefType src_ref_type = { 0 },
  12641. *src_elem_ref_type = NULL;
  12642. WASMRefType dst_ref_type = { 0 },
  12643. *dst_elem_ref_type = NULL;
  12644. WASMArrayType *array_type;
  12645. /* typeidx1 */
  12646. read_leb_uint32(p, p_end, type_idx);
  12647. #if WASM_ENABLE_FAST_INTERP != 0
  12648. emit_uint32(loader_ctx, type_idx);
  12649. #endif
  12650. /* typeidx2 */
  12651. read_leb_uint32(p, p_end, src_type_idx);
  12652. #if WASM_ENABLE_FAST_INTERP != 0
  12653. emit_uint32(loader_ctx, src_type_idx);
  12654. #endif
  12655. if (!check_array_type(module, type_idx, error_buf,
  12656. error_buf_size)) {
  12657. goto fail;
  12658. }
  12659. if (!check_array_type(module, src_type_idx, error_buf,
  12660. error_buf_size)) {
  12661. goto fail;
  12662. }
  12663. POP_I32();
  12664. POP_I32();
  12665. /* POP array obj, (ref null $t) */
  12666. wasm_set_refheaptype_typeidx(
  12667. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12668. POP_REF(wasm_ref_type.ref_type);
  12669. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12670. &wasm_ref_type,
  12671. wasm_reftype_struct_size(&wasm_ref_type));
  12672. POP_I32();
  12673. /* POP array obj, (ref null $t) */
  12674. wasm_set_refheaptype_typeidx(
  12675. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12676. POP_REF(wasm_ref_type.ref_type);
  12677. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12678. &wasm_ref_type,
  12679. wasm_reftype_struct_size(&wasm_ref_type));
  12680. array_type = (WASMArrayType *)module->types[type_idx];
  12681. if (!(array_type->elem_flags & 1)) {
  12682. set_error_buf(error_buf, error_buf_size,
  12683. "destination array is immutable");
  12684. goto fail;
  12685. }
  12686. dst_elem_type = array_type->elem_type;
  12687. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12688. dst_elem_ref_type = array_type->elem_ref_type;
  12689. }
  12690. array_type =
  12691. (WASMArrayType *)module->types[src_type_idx];
  12692. src_elem_type = array_type->elem_type;
  12693. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12694. src_elem_ref_type = array_type->elem_ref_type;
  12695. }
  12696. if (!wasm_reftype_is_subtype_of(
  12697. src_elem_type, src_elem_ref_type, dst_elem_type,
  12698. dst_elem_ref_type, module->types,
  12699. module->type_count)) {
  12700. set_error_buf(error_buf, error_buf_size,
  12701. "array types do not match");
  12702. goto fail;
  12703. }
  12704. break;
  12705. }
  12706. case WASM_OP_REF_I31:
  12707. {
  12708. POP_I32();
  12709. wasm_set_refheaptype_common(
  12710. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12711. PUSH_REF(wasm_ref_type.ref_type);
  12712. break;
  12713. }
  12714. case WASM_OP_I31_GET_S:
  12715. case WASM_OP_I31_GET_U:
  12716. {
  12717. POP_REF(REF_TYPE_I31REF);
  12718. PUSH_I32();
  12719. break;
  12720. }
  12721. case WASM_OP_REF_TEST:
  12722. case WASM_OP_REF_CAST:
  12723. case WASM_OP_REF_TEST_NULLABLE:
  12724. case WASM_OP_REF_CAST_NULLABLE:
  12725. {
  12726. uint8 type;
  12727. read_leb_int32(p, p_end, heap_type);
  12728. #if WASM_ENABLE_FAST_INTERP != 0
  12729. emit_uint32(loader_ctx, (uint32)heap_type);
  12730. #endif
  12731. if (heap_type >= 0) {
  12732. if (!check_type_index(module, module->type_count,
  12733. heap_type, error_buf,
  12734. error_buf_size)) {
  12735. goto fail;
  12736. }
  12737. }
  12738. else {
  12739. if (!wasm_is_valid_heap_type(heap_type)) {
  12740. set_error_buf(error_buf, error_buf_size,
  12741. "unknown type");
  12742. goto fail;
  12743. }
  12744. }
  12745. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12746. &wasm_ref_type, error_buf,
  12747. error_buf_size)) {
  12748. goto fail;
  12749. }
  12750. if (opcode1 == WASM_OP_REF_TEST
  12751. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12752. PUSH_I32();
  12753. else {
  12754. bool nullable =
  12755. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12756. : false;
  12757. if (heap_type >= 0 || !nullable) {
  12758. wasm_set_refheaptype_typeidx(
  12759. &wasm_ref_type.ref_ht_typeidx, nullable,
  12760. heap_type);
  12761. PUSH_REF(wasm_ref_type.ref_type);
  12762. }
  12763. else {
  12764. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12765. }
  12766. }
  12767. break;
  12768. }
  12769. case WASM_OP_BR_ON_CAST:
  12770. case WASM_OP_BR_ON_CAST_FAIL:
  12771. {
  12772. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12773. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12774. uint8 type_tmp, castflags;
  12775. uint32 depth;
  12776. int32 heap_type_dst;
  12777. bool src_nullable, dst_nullable;
  12778. CHECK_BUF(p, p_end, 1);
  12779. castflags = read_uint8(p);
  12780. #if WASM_ENABLE_FAST_INTERP != 0
  12781. /* Emit heap_type firstly */
  12782. emit_byte(loader_ctx, castflags);
  12783. #endif
  12784. p_org = p;
  12785. read_leb_uint32(p, p_end, depth);
  12786. read_leb_int32(p, p_end, heap_type);
  12787. #if WASM_ENABLE_FAST_INTERP != 0
  12788. /* Emit heap_type firstly */
  12789. emit_uint32(loader_ctx, (uint32)heap_type);
  12790. #endif
  12791. read_leb_int32(p, p_end, heap_type_dst);
  12792. #if WASM_ENABLE_FAST_INTERP != 0
  12793. /* Emit heap_type firstly */
  12794. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12795. #endif
  12796. (void)depth;
  12797. /*
  12798. * castflags should be 0~3:
  12799. * 0: (non-null, non-null)
  12800. * 1: (null, non-null)
  12801. * 2: (non-null, null)
  12802. * 3: (null, null)
  12803. */
  12804. if (castflags > 3) {
  12805. set_error_buf(error_buf, error_buf_size,
  12806. "invalid castflags");
  12807. break;
  12808. }
  12809. src_nullable =
  12810. (castflags == 1) || (castflags == 3) ? true : false;
  12811. dst_nullable =
  12812. (castflags == 2) || (castflags == 3) ? true : false;
  12813. /* Pop and backup the stack top's ref type */
  12814. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12815. &ref_type_tmp, error_buf,
  12816. error_buf_size)) {
  12817. goto fail;
  12818. }
  12819. /* The reference type rt1 must be valid */
  12820. if (!init_ref_type(module, &ref_type1, src_nullable,
  12821. heap_type, error_buf,
  12822. error_buf_size)) {
  12823. goto fail;
  12824. }
  12825. /* The reference type rt2 must be valid. */
  12826. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12827. heap_type_dst, error_buf,
  12828. error_buf_size)) {
  12829. goto fail;
  12830. }
  12831. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12832. &ref_type2);
  12833. /* The reference type rt2 must match rt1. */
  12834. if (!wasm_reftype_is_subtype_of(
  12835. ref_type2.ref_type, &ref_type2,
  12836. ref_type1.ref_type, &ref_type1, module->types,
  12837. module->type_count)) {
  12838. set_error_buf(error_buf, error_buf_size,
  12839. "type mismatch");
  12840. goto fail;
  12841. }
  12842. p = p_org;
  12843. /* Push ref type casted for branch block check */
  12844. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12845. /* The reference type rt2 must match rt′. */
  12846. type_tmp = ref_type2.ref_type;
  12847. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12848. bh_memcpy_s(
  12849. &wasm_ref_type,
  12850. wasm_reftype_struct_size(&ref_type2),
  12851. &ref_type2,
  12852. wasm_reftype_struct_size(&ref_type2));
  12853. }
  12854. }
  12855. else {
  12856. /* The reference type rt′1 must match rt′. */
  12857. type_tmp = ref_type_diff.ref_type;
  12858. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12859. bh_memcpy_s(
  12860. &wasm_ref_type,
  12861. wasm_reftype_struct_size(&ref_type_diff),
  12862. &ref_type_diff,
  12863. wasm_reftype_struct_size(&ref_type_diff));
  12864. }
  12865. }
  12866. PUSH_REF(type_tmp);
  12867. if (!(frame_csp_tmp = check_branch_block(
  12868. loader_ctx, &p, p_end, opcode, error_buf,
  12869. error_buf_size))) {
  12870. goto fail;
  12871. }
  12872. /* Ignore heap_types */
  12873. skip_leb_uint32(p, p_end);
  12874. skip_leb_uint32(p, p_end);
  12875. /* Restore the original stack top's ref type */
  12876. POP_REF(type_tmp);
  12877. #if WASM_ENABLE_FAST_INTERP != 0
  12878. /* Erase the opnd offset emitted by POP_REF() */
  12879. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12880. #endif
  12881. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12882. type_tmp = ref_type_diff.ref_type;
  12883. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12884. bh_memcpy_s(
  12885. &wasm_ref_type,
  12886. wasm_reftype_struct_size(&ref_type_diff),
  12887. &ref_type_diff,
  12888. wasm_reftype_struct_size(&ref_type_diff));
  12889. }
  12890. }
  12891. else {
  12892. type_tmp = ref_type2.ref_type;
  12893. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12894. bh_memcpy_s(
  12895. &wasm_ref_type,
  12896. wasm_reftype_struct_size(&ref_type2),
  12897. &ref_type2,
  12898. wasm_reftype_struct_size(&ref_type2));
  12899. }
  12900. }
  12901. PUSH_REF(type_tmp);
  12902. #if WASM_ENABLE_FAST_INTERP != 0
  12903. /* Erase the opnd offset emitted by PUSH_REF() */
  12904. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12905. #endif
  12906. break;
  12907. }
  12908. case WASM_OP_ANY_CONVERT_EXTERN:
  12909. {
  12910. uint8 type;
  12911. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12912. &wasm_ref_type, error_buf,
  12913. error_buf_size)) {
  12914. goto fail;
  12915. }
  12916. if (!(type == REF_TYPE_EXTERNREF
  12917. || (type == REF_TYPE_HT_NON_NULLABLE
  12918. && wasm_ref_type.ref_ht_common.heap_type
  12919. == HEAP_TYPE_EXTERN)
  12920. || type == VALUE_TYPE_ANY)) {
  12921. set_error_buf(error_buf, error_buf_size,
  12922. "type mismatch");
  12923. goto fail;
  12924. }
  12925. if (type == REF_TYPE_EXTERNREF)
  12926. type = REF_TYPE_ANYREF;
  12927. else {
  12928. wasm_ref_type.ref_ht_common.heap_type =
  12929. HEAP_TYPE_ANY;
  12930. }
  12931. PUSH_REF(type);
  12932. break;
  12933. }
  12934. case WASM_OP_EXTERN_CONVERT_ANY:
  12935. {
  12936. uint8 type;
  12937. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12938. &wasm_ref_type, error_buf,
  12939. error_buf_size)) {
  12940. goto fail;
  12941. }
  12942. if (type == REF_TYPE_EXTERNREF
  12943. || ((type == REF_TYPE_HT_NULLABLE
  12944. || type == REF_TYPE_HT_NON_NULLABLE)
  12945. && wasm_ref_type.ref_ht_common.heap_type
  12946. == HEAP_TYPE_EXTERN)) {
  12947. set_error_buf(error_buf, error_buf_size,
  12948. "type mismatch");
  12949. goto fail;
  12950. }
  12951. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12952. /* push (ref null extern) */
  12953. type = REF_TYPE_EXTERNREF;
  12954. }
  12955. else {
  12956. /* push (ref extern) */
  12957. type = REF_TYPE_HT_NON_NULLABLE;
  12958. wasm_set_refheaptype_common(
  12959. &wasm_ref_type.ref_ht_common, false,
  12960. HEAP_TYPE_EXTERN);
  12961. }
  12962. PUSH_REF(type);
  12963. break;
  12964. }
  12965. #if WASM_ENABLE_STRINGREF != 0
  12966. case WASM_OP_STRING_NEW_UTF8:
  12967. case WASM_OP_STRING_NEW_WTF16:
  12968. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12969. case WASM_OP_STRING_NEW_WTF8:
  12970. {
  12971. uint32 memidx;
  12972. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12973. func->has_memory_operations = true;
  12974. #endif
  12975. read_leb_uint32(p, p_end, memidx);
  12976. #if WASM_ENABLE_FAST_INTERP != 0
  12977. emit_uint32(loader_ctx, (uint32)memidx);
  12978. #endif
  12979. POP_I32();
  12980. POP_I32();
  12981. PUSH_REF(REF_TYPE_STRINGREF);
  12982. (void)memidx;
  12983. break;
  12984. }
  12985. case WASM_OP_STRING_CONST:
  12986. {
  12987. uint32 contents;
  12988. read_leb_uint32(p, p_end, contents);
  12989. #if WASM_ENABLE_FAST_INTERP != 0
  12990. emit_uint32(loader_ctx, (uint32)contents);
  12991. #endif
  12992. PUSH_REF(REF_TYPE_STRINGREF);
  12993. (void)contents;
  12994. break;
  12995. }
  12996. case WASM_OP_STRING_MEASURE_UTF8:
  12997. case WASM_OP_STRING_MEASURE_WTF8:
  12998. case WASM_OP_STRING_MEASURE_WTF16:
  12999. {
  13000. POP_STRINGREF();
  13001. PUSH_I32();
  13002. break;
  13003. }
  13004. case WASM_OP_STRING_ENCODE_UTF8:
  13005. case WASM_OP_STRING_ENCODE_WTF16:
  13006. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  13007. case WASM_OP_STRING_ENCODE_WTF8:
  13008. {
  13009. uint32 memidx;
  13010. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13011. func->has_memory_operations = true;
  13012. #endif
  13013. read_leb_uint32(p, p_end, memidx);
  13014. #if WASM_ENABLE_FAST_INTERP != 0
  13015. emit_uint32(loader_ctx, (uint32)memidx);
  13016. #endif
  13017. POP_I32();
  13018. POP_STRINGREF();
  13019. PUSH_I32();
  13020. (void)memidx;
  13021. break;
  13022. }
  13023. case WASM_OP_STRING_CONCAT:
  13024. {
  13025. POP_STRINGREF();
  13026. POP_STRINGREF();
  13027. PUSH_REF(REF_TYPE_STRINGREF);
  13028. break;
  13029. }
  13030. case WASM_OP_STRING_EQ:
  13031. {
  13032. POP_STRINGREF();
  13033. POP_STRINGREF();
  13034. PUSH_I32();
  13035. break;
  13036. }
  13037. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13038. {
  13039. POP_STRINGREF();
  13040. PUSH_I32();
  13041. break;
  13042. }
  13043. case WASM_OP_STRING_AS_WTF8:
  13044. {
  13045. POP_STRINGREF();
  13046. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13047. break;
  13048. }
  13049. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13050. {
  13051. POP_I32();
  13052. POP_I32();
  13053. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13054. PUSH_I32();
  13055. break;
  13056. }
  13057. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13058. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13059. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13060. {
  13061. uint32 memidx;
  13062. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13063. func->has_memory_operations = true;
  13064. #endif
  13065. read_leb_uint32(p, p_end, memidx);
  13066. #if WASM_ENABLE_FAST_INTERP != 0
  13067. emit_uint32(loader_ctx, (uint32)memidx);
  13068. #endif
  13069. POP_I32();
  13070. POP_I32();
  13071. POP_I32();
  13072. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13073. PUSH_I32();
  13074. PUSH_I32();
  13075. (void)memidx;
  13076. break;
  13077. }
  13078. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13079. {
  13080. POP_I32();
  13081. POP_I32();
  13082. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13083. PUSH_REF(REF_TYPE_STRINGREF);
  13084. break;
  13085. }
  13086. case WASM_OP_STRING_AS_WTF16:
  13087. {
  13088. POP_STRINGREF();
  13089. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13090. break;
  13091. }
  13092. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13093. {
  13094. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13095. PUSH_I32();
  13096. break;
  13097. }
  13098. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13099. {
  13100. POP_I32();
  13101. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13102. PUSH_I32();
  13103. break;
  13104. }
  13105. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13106. {
  13107. uint32 memidx;
  13108. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13109. func->has_memory_operations = true;
  13110. #endif
  13111. read_leb_uint32(p, p_end, memidx);
  13112. #if WASM_ENABLE_FAST_INTERP != 0
  13113. emit_uint32(loader_ctx, (uint32)memidx);
  13114. #endif
  13115. POP_I32();
  13116. POP_I32();
  13117. POP_I32();
  13118. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13119. PUSH_I32();
  13120. (void)memidx;
  13121. break;
  13122. }
  13123. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13124. {
  13125. POP_I32();
  13126. POP_I32();
  13127. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13128. PUSH_REF(REF_TYPE_STRINGREF);
  13129. break;
  13130. }
  13131. case WASM_OP_STRING_AS_ITER:
  13132. {
  13133. POP_STRINGREF();
  13134. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13135. break;
  13136. }
  13137. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13138. {
  13139. POP_REF(REF_TYPE_STRINGVIEWITER);
  13140. PUSH_I32();
  13141. break;
  13142. }
  13143. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13144. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13145. {
  13146. POP_I32();
  13147. POP_REF(REF_TYPE_STRINGVIEWITER);
  13148. PUSH_I32();
  13149. break;
  13150. }
  13151. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13152. {
  13153. POP_I32();
  13154. POP_REF(REF_TYPE_STRINGVIEWITER);
  13155. PUSH_REF(REF_TYPE_STRINGREF);
  13156. break;
  13157. }
  13158. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13159. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13160. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13161. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13162. {
  13163. POP_I32();
  13164. POP_I32();
  13165. POP_REF(REF_TYPE_ARRAYREF);
  13166. PUSH_REF(REF_TYPE_STRINGREF);
  13167. break;
  13168. }
  13169. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13170. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13171. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13172. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13173. {
  13174. POP_I32();
  13175. POP_REF(REF_TYPE_ARRAYREF);
  13176. POP_STRINGREF();
  13177. PUSH_I32();
  13178. break;
  13179. }
  13180. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13181. default:
  13182. set_error_buf_v(error_buf, error_buf_size,
  13183. "%s %02x %02x", "unsupported opcode",
  13184. 0xfb, opcode1);
  13185. goto fail;
  13186. }
  13187. break;
  13188. }
  13189. #endif /* end of WASM_ENABLE_GC != 0 */
  13190. case WASM_OP_MISC_PREFIX:
  13191. {
  13192. uint32 opcode1;
  13193. read_leb_uint32(p, p_end, opcode1);
  13194. #if WASM_ENABLE_FAST_INTERP != 0
  13195. emit_byte(loader_ctx, ((uint8)opcode1));
  13196. #endif
  13197. switch (opcode1) {
  13198. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13199. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13200. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13201. break;
  13202. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13203. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13204. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13205. break;
  13206. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13207. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13208. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13209. break;
  13210. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13211. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13212. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13213. break;
  13214. #if WASM_ENABLE_BULK_MEMORY != 0
  13215. case WASM_OP_MEMORY_INIT:
  13216. {
  13217. read_leb_uint32(p, p_end, data_seg_idx);
  13218. #if WASM_ENABLE_FAST_INTERP != 0
  13219. emit_uint32(loader_ctx, data_seg_idx);
  13220. #endif
  13221. if (module->import_memory_count == 0
  13222. && module->memory_count == 0)
  13223. goto fail_unknown_memory;
  13224. read_leb_uint32(p, p_end, memidx);
  13225. check_memidx(module, memidx);
  13226. if (data_seg_idx >= module->data_seg_count) {
  13227. set_error_buf_v(error_buf, error_buf_size,
  13228. "unknown data segment %d",
  13229. data_seg_idx);
  13230. goto fail;
  13231. }
  13232. if (module->data_seg_count1 == 0)
  13233. goto fail_data_cnt_sec_require;
  13234. POP_I32();
  13235. POP_I32();
  13236. POP_MEM_OFFSET();
  13237. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13238. func->has_memory_operations = true;
  13239. #endif
  13240. #if WASM_ENABLE_WAMR_COMPILER != 0
  13241. module->is_bulk_memory_used = true;
  13242. #endif
  13243. break;
  13244. }
  13245. case WASM_OP_DATA_DROP:
  13246. {
  13247. read_leb_uint32(p, p_end, data_seg_idx);
  13248. #if WASM_ENABLE_FAST_INTERP != 0
  13249. emit_uint32(loader_ctx, data_seg_idx);
  13250. #endif
  13251. if (data_seg_idx >= module->data_seg_count) {
  13252. set_error_buf(error_buf, error_buf_size,
  13253. "unknown data segment");
  13254. goto fail;
  13255. }
  13256. if (module->data_seg_count1 == 0)
  13257. goto fail_data_cnt_sec_require;
  13258. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13259. func->has_memory_operations = true;
  13260. #endif
  13261. #if WASM_ENABLE_WAMR_COMPILER != 0
  13262. module->is_bulk_memory_used = true;
  13263. #endif
  13264. break;
  13265. }
  13266. case WASM_OP_MEMORY_COPY:
  13267. {
  13268. CHECK_BUF(p, p_end, sizeof(int16));
  13269. /* check both src and dst memory index */
  13270. read_leb_uint32(p, p_end, memidx);
  13271. check_memidx(module, memidx);
  13272. read_leb_uint32(p, p_end, memidx);
  13273. check_memidx(module, memidx);
  13274. if (module->import_memory_count == 0
  13275. && module->memory_count == 0)
  13276. goto fail_unknown_memory;
  13277. POP_MEM_OFFSET();
  13278. POP_MEM_OFFSET();
  13279. POP_MEM_OFFSET();
  13280. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13281. func->has_memory_operations = true;
  13282. #endif
  13283. #if WASM_ENABLE_WAMR_COMPILER != 0
  13284. module->is_bulk_memory_used = true;
  13285. #endif
  13286. break;
  13287. }
  13288. case WASM_OP_MEMORY_FILL:
  13289. {
  13290. read_leb_uint32(p, p_end, memidx);
  13291. check_memidx(module, memidx);
  13292. if (module->import_memory_count == 0
  13293. && module->memory_count == 0) {
  13294. goto fail_unknown_memory;
  13295. }
  13296. POP_MEM_OFFSET();
  13297. POP_I32();
  13298. POP_MEM_OFFSET();
  13299. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13300. func->has_memory_operations = true;
  13301. #endif
  13302. #if WASM_ENABLE_WAMR_COMPILER != 0
  13303. module->is_bulk_memory_used = true;
  13304. #endif
  13305. break;
  13306. }
  13307. fail_unknown_memory:
  13308. set_error_buf(error_buf, error_buf_size,
  13309. "unknown memory 0");
  13310. goto fail;
  13311. fail_data_cnt_sec_require:
  13312. set_error_buf(error_buf, error_buf_size,
  13313. "data count section required");
  13314. goto fail;
  13315. #endif /* WASM_ENABLE_BULK_MEMORY */
  13316. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13317. case WASM_OP_TABLE_INIT:
  13318. {
  13319. uint8 seg_type = 0, tbl_type = 0;
  13320. #if WASM_ENABLE_GC != 0
  13321. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13322. #endif
  13323. read_leb_uint32(p, p_end, table_seg_idx);
  13324. read_leb_uint32(p, p_end, table_idx);
  13325. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13326. #if WASM_ENABLE_GC != 0
  13327. (void **)&tbl_ref_type,
  13328. #else
  13329. NULL,
  13330. #endif
  13331. error_buf, error_buf_size))
  13332. goto fail;
  13333. if (!get_table_seg_elem_type(module, table_seg_idx,
  13334. &seg_type,
  13335. #if WASM_ENABLE_GC != 0
  13336. (void **)&seg_ref_type,
  13337. #else
  13338. NULL,
  13339. #endif
  13340. error_buf, error_buf_size))
  13341. goto fail;
  13342. #if WASM_ENABLE_GC == 0
  13343. if (seg_type != tbl_type) {
  13344. set_error_buf(error_buf, error_buf_size,
  13345. "type mismatch");
  13346. goto fail;
  13347. }
  13348. #else
  13349. if (!wasm_reftype_is_subtype_of(
  13350. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13351. module->types, module->type_count)) {
  13352. set_error_buf(error_buf, error_buf_size,
  13353. "type mismatch");
  13354. goto fail;
  13355. }
  13356. #endif
  13357. #if WASM_ENABLE_FAST_INTERP != 0
  13358. emit_uint32(loader_ctx, table_seg_idx);
  13359. emit_uint32(loader_ctx, table_idx);
  13360. #endif
  13361. POP_I32();
  13362. POP_I32();
  13363. #if WASM_ENABLE_MEMORY64 != 0
  13364. table_elem_idx_type = is_table_64bit(module, table_idx)
  13365. ? VALUE_TYPE_I64
  13366. : VALUE_TYPE_I32;
  13367. #endif
  13368. POP_TBL_ELEM_IDX();
  13369. #if WASM_ENABLE_WAMR_COMPILER != 0
  13370. module->is_ref_types_used = true;
  13371. #endif
  13372. break;
  13373. }
  13374. case WASM_OP_ELEM_DROP:
  13375. {
  13376. read_leb_uint32(p, p_end, table_seg_idx);
  13377. if (!get_table_seg_elem_type(module, table_seg_idx,
  13378. NULL, NULL, error_buf,
  13379. error_buf_size))
  13380. goto fail;
  13381. #if WASM_ENABLE_FAST_INTERP != 0
  13382. emit_uint32(loader_ctx, table_seg_idx);
  13383. #endif
  13384. #if WASM_ENABLE_WAMR_COMPILER != 0
  13385. module->is_ref_types_used = true;
  13386. #endif
  13387. break;
  13388. }
  13389. case WASM_OP_TABLE_COPY:
  13390. {
  13391. uint8 src_type, dst_type, src_tbl_idx_type,
  13392. dst_tbl_idx_type, min_tbl_idx_type;
  13393. #if WASM_ENABLE_GC != 0
  13394. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13395. #endif
  13396. uint32 src_tbl_idx, dst_tbl_idx;
  13397. read_leb_uint32(p, p_end, dst_tbl_idx);
  13398. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13399. #if WASM_ENABLE_GC != 0
  13400. (void **)&dst_ref_type,
  13401. #else
  13402. NULL,
  13403. #endif
  13404. error_buf, error_buf_size))
  13405. goto fail;
  13406. read_leb_uint32(p, p_end, src_tbl_idx);
  13407. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13408. #if WASM_ENABLE_GC != 0
  13409. (void **)&src_ref_type,
  13410. #else
  13411. NULL,
  13412. #endif
  13413. error_buf, error_buf_size))
  13414. goto fail;
  13415. #if WASM_ENABLE_GC == 0
  13416. if (src_type != dst_type) {
  13417. set_error_buf(error_buf, error_buf_size,
  13418. "type mismatch");
  13419. goto fail;
  13420. }
  13421. #else
  13422. if (!wasm_reftype_is_subtype_of(
  13423. src_type, src_ref_type, dst_type, dst_ref_type,
  13424. module->types, module->type_count)) {
  13425. set_error_buf(error_buf, error_buf_size,
  13426. "type mismatch");
  13427. goto fail;
  13428. }
  13429. #endif
  13430. #if WASM_ENABLE_FAST_INTERP != 0
  13431. emit_uint32(loader_ctx, dst_tbl_idx);
  13432. emit_uint32(loader_ctx, src_tbl_idx);
  13433. #endif
  13434. #if WASM_ENABLE_MEMORY64 != 0
  13435. src_tbl_idx_type = is_table_64bit(module, src_tbl_idx)
  13436. ? VALUE_TYPE_I64
  13437. : VALUE_TYPE_I32;
  13438. dst_tbl_idx_type = is_table_64bit(module, dst_tbl_idx)
  13439. ? VALUE_TYPE_I64
  13440. : VALUE_TYPE_I32;
  13441. min_tbl_idx_type =
  13442. (src_tbl_idx_type == VALUE_TYPE_I32
  13443. || dst_tbl_idx_type == VALUE_TYPE_I32)
  13444. ? VALUE_TYPE_I32
  13445. : VALUE_TYPE_I64;
  13446. #else
  13447. src_tbl_idx_type = VALUE_TYPE_I32;
  13448. dst_tbl_idx_type = VALUE_TYPE_I32;
  13449. min_tbl_idx_type = VALUE_TYPE_I32;
  13450. #endif
  13451. table_elem_idx_type = min_tbl_idx_type;
  13452. POP_TBL_ELEM_IDX();
  13453. table_elem_idx_type = src_tbl_idx_type;
  13454. POP_TBL_ELEM_IDX();
  13455. table_elem_idx_type = dst_tbl_idx_type;
  13456. POP_TBL_ELEM_IDX();
  13457. #if WASM_ENABLE_WAMR_COMPILER != 0
  13458. module->is_ref_types_used = true;
  13459. #endif
  13460. break;
  13461. }
  13462. case WASM_OP_TABLE_SIZE:
  13463. {
  13464. read_leb_uint32(p, p_end, table_idx);
  13465. /* TODO: shall we create a new function to check
  13466. table idx instead of using below function? */
  13467. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13468. error_buf, error_buf_size))
  13469. goto fail;
  13470. #if WASM_ENABLE_FAST_INTERP != 0
  13471. emit_uint32(loader_ctx, table_idx);
  13472. #endif
  13473. #if WASM_ENABLE_MEMORY64 != 0
  13474. table_elem_idx_type = is_table_64bit(module, table_idx)
  13475. ? VALUE_TYPE_I64
  13476. : VALUE_TYPE_I32;
  13477. #endif
  13478. PUSH_TBL_ELEM_IDX();
  13479. #if WASM_ENABLE_WAMR_COMPILER != 0
  13480. module->is_ref_types_used = true;
  13481. #endif
  13482. break;
  13483. }
  13484. case WASM_OP_TABLE_GROW:
  13485. case WASM_OP_TABLE_FILL:
  13486. {
  13487. uint8 decl_type;
  13488. #if WASM_ENABLE_GC != 0
  13489. WASMRefType *ref_type = NULL;
  13490. #endif
  13491. read_leb_uint32(p, p_end, table_idx);
  13492. if (!get_table_elem_type(module, table_idx, &decl_type,
  13493. #if WASM_ENABLE_GC != 0
  13494. (void **)&ref_type,
  13495. #else
  13496. NULL,
  13497. #endif
  13498. error_buf, error_buf_size))
  13499. goto fail;
  13500. #if WASM_ENABLE_GC != 0
  13501. if (wasm_is_type_multi_byte_type(decl_type)) {
  13502. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13503. ref_type,
  13504. wasm_reftype_struct_size(ref_type));
  13505. }
  13506. #endif
  13507. if (opcode1 == WASM_OP_TABLE_GROW) {
  13508. if (table_idx < module->import_table_count) {
  13509. module->import_tables[table_idx]
  13510. .u.table.table_type.possible_grow = true;
  13511. }
  13512. else {
  13513. module
  13514. ->tables[table_idx
  13515. - module->import_table_count]
  13516. .table_type.possible_grow = true;
  13517. }
  13518. }
  13519. #if WASM_ENABLE_FAST_INTERP != 0
  13520. emit_uint32(loader_ctx, table_idx);
  13521. #endif
  13522. #if WASM_ENABLE_MEMORY64 != 0
  13523. table_elem_idx_type = is_table_64bit(module, table_idx)
  13524. ? VALUE_TYPE_I64
  13525. : VALUE_TYPE_I32;
  13526. #endif
  13527. POP_TBL_ELEM_IDX();
  13528. #if WASM_ENABLE_FAST_INTERP != 0
  13529. POP_OFFSET_TYPE(decl_type);
  13530. #endif
  13531. POP_TYPE(decl_type);
  13532. if (opcode1 == WASM_OP_TABLE_GROW)
  13533. PUSH_TBL_ELEM_IDX();
  13534. else
  13535. POP_TBL_ELEM_IDX();
  13536. #if WASM_ENABLE_WAMR_COMPILER != 0
  13537. module->is_ref_types_used = true;
  13538. #endif
  13539. break;
  13540. }
  13541. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13542. default:
  13543. set_error_buf_v(error_buf, error_buf_size,
  13544. "%s %02x %02x", "unsupported opcode",
  13545. 0xfc, opcode1);
  13546. goto fail;
  13547. }
  13548. break;
  13549. }
  13550. #if WASM_ENABLE_SIMD != 0
  13551. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13552. case WASM_OP_SIMD_PREFIX:
  13553. {
  13554. uint32 opcode1;
  13555. #if WASM_ENABLE_WAMR_COMPILER != 0
  13556. /* Mark the SIMD instruction is used in this module */
  13557. module->is_simd_used = true;
  13558. #endif
  13559. read_leb_uint32(p, p_end, opcode1);
  13560. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13561. */
  13562. switch (opcode1) {
  13563. /* memory instruction */
  13564. case SIMD_v128_load:
  13565. case SIMD_v128_load8x8_s:
  13566. case SIMD_v128_load8x8_u:
  13567. case SIMD_v128_load16x4_s:
  13568. case SIMD_v128_load16x4_u:
  13569. case SIMD_v128_load32x2_s:
  13570. case SIMD_v128_load32x2_u:
  13571. case SIMD_v128_load8_splat:
  13572. case SIMD_v128_load16_splat:
  13573. case SIMD_v128_load32_splat:
  13574. case SIMD_v128_load64_splat:
  13575. {
  13576. CHECK_MEMORY();
  13577. read_leb_uint32(p, p_end, align); /* align */
  13578. if (!check_simd_memory_access_align(
  13579. opcode1, align, error_buf, error_buf_size)) {
  13580. goto fail;
  13581. }
  13582. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13583. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13584. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13585. func->has_memory_operations = true;
  13586. #endif
  13587. break;
  13588. }
  13589. case SIMD_v128_store:
  13590. {
  13591. CHECK_MEMORY();
  13592. read_leb_uint32(p, p_end, align); /* align */
  13593. if (!check_simd_memory_access_align(
  13594. opcode1, align, error_buf, error_buf_size)) {
  13595. goto fail;
  13596. }
  13597. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13598. POP_V128();
  13599. POP_MEM_OFFSET();
  13600. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13601. func->has_memory_operations = true;
  13602. #endif
  13603. break;
  13604. }
  13605. /* basic operation */
  13606. case SIMD_v128_const:
  13607. {
  13608. CHECK_BUF1(p, p_end, 16);
  13609. p += 16;
  13610. PUSH_V128();
  13611. break;
  13612. }
  13613. case SIMD_v8x16_shuffle:
  13614. {
  13615. V128 mask;
  13616. CHECK_BUF1(p, p_end, 16);
  13617. mask = read_i8x16(p, error_buf, error_buf_size);
  13618. p += 16;
  13619. if (!check_simd_shuffle_mask(mask, error_buf,
  13620. error_buf_size)) {
  13621. goto fail;
  13622. }
  13623. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13624. break;
  13625. }
  13626. case SIMD_v8x16_swizzle:
  13627. {
  13628. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13629. break;
  13630. }
  13631. /* splat operation */
  13632. case SIMD_i8x16_splat:
  13633. case SIMD_i16x8_splat:
  13634. case SIMD_i32x4_splat:
  13635. case SIMD_i64x2_splat:
  13636. case SIMD_f32x4_splat:
  13637. case SIMD_f64x2_splat:
  13638. {
  13639. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13640. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13641. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13642. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13643. VALUE_TYPE_V128);
  13644. break;
  13645. }
  13646. /* lane operation */
  13647. case SIMD_i8x16_extract_lane_s:
  13648. case SIMD_i8x16_extract_lane_u:
  13649. case SIMD_i8x16_replace_lane:
  13650. case SIMD_i16x8_extract_lane_s:
  13651. case SIMD_i16x8_extract_lane_u:
  13652. case SIMD_i16x8_replace_lane:
  13653. case SIMD_i32x4_extract_lane:
  13654. case SIMD_i32x4_replace_lane:
  13655. case SIMD_i64x2_extract_lane:
  13656. case SIMD_i64x2_replace_lane:
  13657. case SIMD_f32x4_extract_lane:
  13658. case SIMD_f32x4_replace_lane:
  13659. case SIMD_f64x2_extract_lane:
  13660. case SIMD_f64x2_replace_lane:
  13661. {
  13662. uint8 lane;
  13663. /* clang-format off */
  13664. uint8 replace[] = {
  13665. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13666. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13667. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13668. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13669. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13670. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13671. };
  13672. uint8 push_type[] = {
  13673. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13674. VALUE_TYPE_V128,
  13675. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13676. VALUE_TYPE_V128,
  13677. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13678. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13679. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13680. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13681. };
  13682. /* clang-format on */
  13683. CHECK_BUF(p, p_end, 1);
  13684. lane = read_uint8(p);
  13685. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13686. error_buf_size)) {
  13687. goto fail;
  13688. }
  13689. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13690. if (!(wasm_loader_pop_frame_ref(
  13691. loader_ctx,
  13692. replace[opcode1
  13693. - SIMD_i8x16_extract_lane_s],
  13694. error_buf, error_buf_size)))
  13695. goto fail;
  13696. }
  13697. POP_AND_PUSH(
  13698. VALUE_TYPE_V128,
  13699. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13700. break;
  13701. }
  13702. /* i8x16 compare operation */
  13703. case SIMD_i8x16_eq:
  13704. case SIMD_i8x16_ne:
  13705. case SIMD_i8x16_lt_s:
  13706. case SIMD_i8x16_lt_u:
  13707. case SIMD_i8x16_gt_s:
  13708. case SIMD_i8x16_gt_u:
  13709. case SIMD_i8x16_le_s:
  13710. case SIMD_i8x16_le_u:
  13711. case SIMD_i8x16_ge_s:
  13712. case SIMD_i8x16_ge_u:
  13713. /* i16x8 compare operation */
  13714. case SIMD_i16x8_eq:
  13715. case SIMD_i16x8_ne:
  13716. case SIMD_i16x8_lt_s:
  13717. case SIMD_i16x8_lt_u:
  13718. case SIMD_i16x8_gt_s:
  13719. case SIMD_i16x8_gt_u:
  13720. case SIMD_i16x8_le_s:
  13721. case SIMD_i16x8_le_u:
  13722. case SIMD_i16x8_ge_s:
  13723. case SIMD_i16x8_ge_u:
  13724. /* i32x4 compare operation */
  13725. case SIMD_i32x4_eq:
  13726. case SIMD_i32x4_ne:
  13727. case SIMD_i32x4_lt_s:
  13728. case SIMD_i32x4_lt_u:
  13729. case SIMD_i32x4_gt_s:
  13730. case SIMD_i32x4_gt_u:
  13731. case SIMD_i32x4_le_s:
  13732. case SIMD_i32x4_le_u:
  13733. case SIMD_i32x4_ge_s:
  13734. case SIMD_i32x4_ge_u:
  13735. /* f32x4 compare operation */
  13736. case SIMD_f32x4_eq:
  13737. case SIMD_f32x4_ne:
  13738. case SIMD_f32x4_lt:
  13739. case SIMD_f32x4_gt:
  13740. case SIMD_f32x4_le:
  13741. case SIMD_f32x4_ge:
  13742. /* f64x2 compare operation */
  13743. case SIMD_f64x2_eq:
  13744. case SIMD_f64x2_ne:
  13745. case SIMD_f64x2_lt:
  13746. case SIMD_f64x2_gt:
  13747. case SIMD_f64x2_le:
  13748. case SIMD_f64x2_ge:
  13749. {
  13750. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13751. break;
  13752. }
  13753. /* v128 operation */
  13754. case SIMD_v128_not:
  13755. {
  13756. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13757. break;
  13758. }
  13759. case SIMD_v128_and:
  13760. case SIMD_v128_andnot:
  13761. case SIMD_v128_or:
  13762. case SIMD_v128_xor:
  13763. {
  13764. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13765. break;
  13766. }
  13767. case SIMD_v128_bitselect:
  13768. {
  13769. POP_V128();
  13770. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13771. break;
  13772. }
  13773. case SIMD_v128_any_true:
  13774. {
  13775. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13776. break;
  13777. }
  13778. /* Load Lane Operation */
  13779. case SIMD_v128_load8_lane:
  13780. case SIMD_v128_load16_lane:
  13781. case SIMD_v128_load32_lane:
  13782. case SIMD_v128_load64_lane:
  13783. case SIMD_v128_store8_lane:
  13784. case SIMD_v128_store16_lane:
  13785. case SIMD_v128_store32_lane:
  13786. case SIMD_v128_store64_lane:
  13787. {
  13788. uint8 lane;
  13789. CHECK_MEMORY();
  13790. read_leb_uint32(p, p_end, align); /* align */
  13791. if (!check_simd_memory_access_align(
  13792. opcode1, align, error_buf, error_buf_size)) {
  13793. goto fail;
  13794. }
  13795. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13796. CHECK_BUF(p, p_end, 1);
  13797. lane = read_uint8(p);
  13798. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13799. error_buf_size)) {
  13800. goto fail;
  13801. }
  13802. POP_V128();
  13803. POP_MEM_OFFSET();
  13804. if (opcode1 < SIMD_v128_store8_lane) {
  13805. PUSH_V128();
  13806. }
  13807. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13808. func->has_memory_operations = true;
  13809. #endif
  13810. break;
  13811. }
  13812. case SIMD_v128_load32_zero:
  13813. case SIMD_v128_load64_zero:
  13814. {
  13815. CHECK_MEMORY();
  13816. read_leb_uint32(p, p_end, align); /* align */
  13817. if (!check_simd_memory_access_align(
  13818. opcode1, align, error_buf, error_buf_size)) {
  13819. goto fail;
  13820. }
  13821. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13822. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13823. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13824. func->has_memory_operations = true;
  13825. #endif
  13826. break;
  13827. }
  13828. /* Float conversion */
  13829. case SIMD_f32x4_demote_f64x2_zero:
  13830. case SIMD_f64x2_promote_low_f32x4_zero:
  13831. {
  13832. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13833. break;
  13834. }
  13835. /* i8x16 Operation */
  13836. case SIMD_i8x16_abs:
  13837. case SIMD_i8x16_neg:
  13838. case SIMD_i8x16_popcnt:
  13839. {
  13840. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13841. break;
  13842. }
  13843. case SIMD_i8x16_all_true:
  13844. case SIMD_i8x16_bitmask:
  13845. {
  13846. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13847. break;
  13848. }
  13849. case SIMD_i8x16_narrow_i16x8_s:
  13850. case SIMD_i8x16_narrow_i16x8_u:
  13851. {
  13852. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13853. break;
  13854. }
  13855. case SIMD_f32x4_ceil:
  13856. case SIMD_f32x4_floor:
  13857. case SIMD_f32x4_trunc:
  13858. case SIMD_f32x4_nearest:
  13859. {
  13860. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13861. break;
  13862. }
  13863. case SIMD_i8x16_shl:
  13864. case SIMD_i8x16_shr_s:
  13865. case SIMD_i8x16_shr_u:
  13866. {
  13867. POP_I32();
  13868. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13869. break;
  13870. }
  13871. case SIMD_i8x16_add:
  13872. case SIMD_i8x16_add_sat_s:
  13873. case SIMD_i8x16_add_sat_u:
  13874. case SIMD_i8x16_sub:
  13875. case SIMD_i8x16_sub_sat_s:
  13876. case SIMD_i8x16_sub_sat_u:
  13877. {
  13878. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13879. break;
  13880. }
  13881. case SIMD_f64x2_ceil:
  13882. case SIMD_f64x2_floor:
  13883. {
  13884. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13885. break;
  13886. }
  13887. case SIMD_i8x16_min_s:
  13888. case SIMD_i8x16_min_u:
  13889. case SIMD_i8x16_max_s:
  13890. case SIMD_i8x16_max_u:
  13891. {
  13892. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13893. break;
  13894. }
  13895. case SIMD_f64x2_trunc:
  13896. {
  13897. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13898. break;
  13899. }
  13900. case SIMD_i8x16_avgr_u:
  13901. {
  13902. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13903. break;
  13904. }
  13905. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13906. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13907. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13908. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13909. /* i16x8 operation */
  13910. case SIMD_i16x8_abs:
  13911. case SIMD_i16x8_neg:
  13912. {
  13913. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13914. break;
  13915. }
  13916. case SIMD_i16x8_q15mulr_sat_s:
  13917. {
  13918. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13919. break;
  13920. }
  13921. case SIMD_i16x8_all_true:
  13922. case SIMD_i16x8_bitmask:
  13923. {
  13924. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13925. break;
  13926. }
  13927. case SIMD_i16x8_narrow_i32x4_s:
  13928. case SIMD_i16x8_narrow_i32x4_u:
  13929. {
  13930. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13931. break;
  13932. }
  13933. case SIMD_i16x8_extend_low_i8x16_s:
  13934. case SIMD_i16x8_extend_high_i8x16_s:
  13935. case SIMD_i16x8_extend_low_i8x16_u:
  13936. case SIMD_i16x8_extend_high_i8x16_u:
  13937. {
  13938. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13939. break;
  13940. }
  13941. case SIMD_i16x8_shl:
  13942. case SIMD_i16x8_shr_s:
  13943. case SIMD_i16x8_shr_u:
  13944. {
  13945. POP_I32();
  13946. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13947. break;
  13948. }
  13949. case SIMD_i16x8_add:
  13950. case SIMD_i16x8_add_sat_s:
  13951. case SIMD_i16x8_add_sat_u:
  13952. case SIMD_i16x8_sub:
  13953. case SIMD_i16x8_sub_sat_s:
  13954. case SIMD_i16x8_sub_sat_u:
  13955. {
  13956. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13957. break;
  13958. }
  13959. case SIMD_f64x2_nearest:
  13960. {
  13961. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13962. break;
  13963. }
  13964. case SIMD_i16x8_mul:
  13965. case SIMD_i16x8_min_s:
  13966. case SIMD_i16x8_min_u:
  13967. case SIMD_i16x8_max_s:
  13968. case SIMD_i16x8_max_u:
  13969. case SIMD_i16x8_avgr_u:
  13970. case SIMD_i16x8_extmul_low_i8x16_s:
  13971. case SIMD_i16x8_extmul_high_i8x16_s:
  13972. case SIMD_i16x8_extmul_low_i8x16_u:
  13973. case SIMD_i16x8_extmul_high_i8x16_u:
  13974. {
  13975. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13976. break;
  13977. }
  13978. /* i32x4 operation */
  13979. case SIMD_i32x4_abs:
  13980. case SIMD_i32x4_neg:
  13981. {
  13982. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13983. break;
  13984. }
  13985. case SIMD_i32x4_all_true:
  13986. case SIMD_i32x4_bitmask:
  13987. {
  13988. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13989. break;
  13990. }
  13991. case SIMD_i32x4_extend_low_i16x8_s:
  13992. case SIMD_i32x4_extend_high_i16x8_s:
  13993. case SIMD_i32x4_extend_low_i16x8_u:
  13994. case SIMD_i32x4_extend_high_i16x8_u:
  13995. {
  13996. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13997. break;
  13998. }
  13999. case SIMD_i32x4_shl:
  14000. case SIMD_i32x4_shr_s:
  14001. case SIMD_i32x4_shr_u:
  14002. {
  14003. POP_I32();
  14004. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14005. break;
  14006. }
  14007. case SIMD_i32x4_add:
  14008. case SIMD_i32x4_sub:
  14009. case SIMD_i32x4_mul:
  14010. case SIMD_i32x4_min_s:
  14011. case SIMD_i32x4_min_u:
  14012. case SIMD_i32x4_max_s:
  14013. case SIMD_i32x4_max_u:
  14014. case SIMD_i32x4_dot_i16x8_s:
  14015. case SIMD_i32x4_extmul_low_i16x8_s:
  14016. case SIMD_i32x4_extmul_high_i16x8_s:
  14017. case SIMD_i32x4_extmul_low_i16x8_u:
  14018. case SIMD_i32x4_extmul_high_i16x8_u:
  14019. {
  14020. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14021. break;
  14022. }
  14023. /* i64x2 operation */
  14024. case SIMD_i64x2_abs:
  14025. case SIMD_i64x2_neg:
  14026. {
  14027. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14028. break;
  14029. }
  14030. case SIMD_i64x2_all_true:
  14031. case SIMD_i64x2_bitmask:
  14032. {
  14033. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14034. break;
  14035. }
  14036. case SIMD_i64x2_extend_low_i32x4_s:
  14037. case SIMD_i64x2_extend_high_i32x4_s:
  14038. case SIMD_i64x2_extend_low_i32x4_u:
  14039. case SIMD_i64x2_extend_high_i32x4_u:
  14040. {
  14041. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14042. break;
  14043. }
  14044. case SIMD_i64x2_shl:
  14045. case SIMD_i64x2_shr_s:
  14046. case SIMD_i64x2_shr_u:
  14047. {
  14048. POP_I32();
  14049. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14050. break;
  14051. }
  14052. case SIMD_i64x2_add:
  14053. case SIMD_i64x2_sub:
  14054. case SIMD_i64x2_mul:
  14055. case SIMD_i64x2_eq:
  14056. case SIMD_i64x2_ne:
  14057. case SIMD_i64x2_lt_s:
  14058. case SIMD_i64x2_gt_s:
  14059. case SIMD_i64x2_le_s:
  14060. case SIMD_i64x2_ge_s:
  14061. case SIMD_i64x2_extmul_low_i32x4_s:
  14062. case SIMD_i64x2_extmul_high_i32x4_s:
  14063. case SIMD_i64x2_extmul_low_i32x4_u:
  14064. case SIMD_i64x2_extmul_high_i32x4_u:
  14065. {
  14066. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14067. break;
  14068. }
  14069. /* f32x4 operation */
  14070. case SIMD_f32x4_abs:
  14071. case SIMD_f32x4_neg:
  14072. case SIMD_f32x4_sqrt:
  14073. {
  14074. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14075. break;
  14076. }
  14077. case SIMD_f32x4_add:
  14078. case SIMD_f32x4_sub:
  14079. case SIMD_f32x4_mul:
  14080. case SIMD_f32x4_div:
  14081. case SIMD_f32x4_min:
  14082. case SIMD_f32x4_max:
  14083. case SIMD_f32x4_pmin:
  14084. case SIMD_f32x4_pmax:
  14085. {
  14086. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14087. break;
  14088. }
  14089. /* f64x2 operation */
  14090. case SIMD_f64x2_abs:
  14091. case SIMD_f64x2_neg:
  14092. case SIMD_f64x2_sqrt:
  14093. {
  14094. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14095. break;
  14096. }
  14097. case SIMD_f64x2_add:
  14098. case SIMD_f64x2_sub:
  14099. case SIMD_f64x2_mul:
  14100. case SIMD_f64x2_div:
  14101. case SIMD_f64x2_min:
  14102. case SIMD_f64x2_max:
  14103. case SIMD_f64x2_pmin:
  14104. case SIMD_f64x2_pmax:
  14105. {
  14106. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14107. break;
  14108. }
  14109. case SIMD_i32x4_trunc_sat_f32x4_s:
  14110. case SIMD_i32x4_trunc_sat_f32x4_u:
  14111. case SIMD_f32x4_convert_i32x4_s:
  14112. case SIMD_f32x4_convert_i32x4_u:
  14113. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14114. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14115. case SIMD_f64x2_convert_low_i32x4_s:
  14116. case SIMD_f64x2_convert_low_i32x4_u:
  14117. {
  14118. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14119. break;
  14120. }
  14121. default:
  14122. {
  14123. if (error_buf != NULL) {
  14124. snprintf(error_buf, error_buf_size,
  14125. "WASM module load failed: "
  14126. "invalid opcode 0xfd %02x.",
  14127. opcode1);
  14128. }
  14129. goto fail;
  14130. }
  14131. }
  14132. break;
  14133. }
  14134. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  14135. #endif /* end of WASM_ENABLE_SIMD */
  14136. #if WASM_ENABLE_SHARED_MEMORY != 0
  14137. case WASM_OP_ATOMIC_PREFIX:
  14138. {
  14139. uint32 opcode1;
  14140. read_leb_uint32(p, p_end, opcode1);
  14141. #if WASM_ENABLE_FAST_INTERP != 0
  14142. emit_byte(loader_ctx, opcode1);
  14143. #endif
  14144. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14145. CHECK_MEMORY();
  14146. read_leb_uint32(p, p_end, align); /* align */
  14147. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14148. if (!check_memory_align_equal(opcode1, align, error_buf,
  14149. error_buf_size)) {
  14150. goto fail;
  14151. }
  14152. #if WASM_ENABLE_FAST_INTERP != 0
  14153. emit_uint32(loader_ctx, mem_offset);
  14154. #endif
  14155. }
  14156. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14157. func->has_memory_operations = true;
  14158. #endif
  14159. switch (opcode1) {
  14160. case WASM_OP_ATOMIC_NOTIFY:
  14161. POP_I32();
  14162. POP_MEM_OFFSET();
  14163. PUSH_I32();
  14164. break;
  14165. case WASM_OP_ATOMIC_WAIT32:
  14166. POP_I64();
  14167. POP_I32();
  14168. POP_MEM_OFFSET();
  14169. PUSH_I32();
  14170. break;
  14171. case WASM_OP_ATOMIC_WAIT64:
  14172. POP_I64();
  14173. POP_I64();
  14174. POP_MEM_OFFSET();
  14175. PUSH_I32();
  14176. break;
  14177. case WASM_OP_ATOMIC_FENCE:
  14178. /* reserved byte 0x00 */
  14179. if (*p++ != 0x00) {
  14180. set_error_buf(error_buf, error_buf_size,
  14181. "zero byte expected");
  14182. goto fail;
  14183. }
  14184. break;
  14185. case WASM_OP_ATOMIC_I32_LOAD:
  14186. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14187. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14188. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14189. break;
  14190. case WASM_OP_ATOMIC_I32_STORE:
  14191. case WASM_OP_ATOMIC_I32_STORE8:
  14192. case WASM_OP_ATOMIC_I32_STORE16:
  14193. POP_I32();
  14194. POP_MEM_OFFSET();
  14195. break;
  14196. case WASM_OP_ATOMIC_I64_LOAD:
  14197. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14198. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14199. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14200. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14201. break;
  14202. case WASM_OP_ATOMIC_I64_STORE:
  14203. case WASM_OP_ATOMIC_I64_STORE8:
  14204. case WASM_OP_ATOMIC_I64_STORE16:
  14205. case WASM_OP_ATOMIC_I64_STORE32:
  14206. POP_I64();
  14207. POP_MEM_OFFSET();
  14208. break;
  14209. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14210. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14211. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14212. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14213. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14214. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14215. case WASM_OP_ATOMIC_RMW_I32_AND:
  14216. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14217. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14218. case WASM_OP_ATOMIC_RMW_I32_OR:
  14219. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14220. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14221. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14222. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14223. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14224. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14225. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14226. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14227. POP_I32();
  14228. POP_MEM_OFFSET();
  14229. PUSH_I32();
  14230. break;
  14231. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14232. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14233. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14234. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14235. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14236. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14237. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14238. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14239. case WASM_OP_ATOMIC_RMW_I64_AND:
  14240. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14241. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14242. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14243. case WASM_OP_ATOMIC_RMW_I64_OR:
  14244. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14245. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14246. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14247. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14248. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14249. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14250. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14251. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14252. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14253. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14254. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14255. POP_I64();
  14256. POP_MEM_OFFSET();
  14257. PUSH_I64();
  14258. break;
  14259. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14260. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14261. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14262. POP_I32();
  14263. POP_I32();
  14264. POP_MEM_OFFSET();
  14265. PUSH_I32();
  14266. break;
  14267. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14268. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14269. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14270. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14271. POP_I64();
  14272. POP_I64();
  14273. POP_MEM_OFFSET();
  14274. PUSH_I64();
  14275. break;
  14276. default:
  14277. set_error_buf_v(error_buf, error_buf_size,
  14278. "%s %02x %02x", "unsupported opcode",
  14279. 0xfe, opcode1);
  14280. goto fail;
  14281. }
  14282. break;
  14283. }
  14284. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14285. default:
  14286. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14287. "unsupported opcode", opcode);
  14288. goto fail;
  14289. }
  14290. #if WASM_ENABLE_FAST_INTERP != 0
  14291. last_op = opcode;
  14292. #endif
  14293. }
  14294. if (loader_ctx->csp_num > 0) {
  14295. if (cur_func_idx < module->function_count - 1)
  14296. /* Function with missing end marker (between two functions) */
  14297. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  14298. else
  14299. /* Function with missing end marker
  14300. (at EOF or end of code sections) */
  14301. set_error_buf(error_buf, error_buf_size,
  14302. "unexpected end of section or function, "
  14303. "or section size mismatch");
  14304. goto fail;
  14305. }
  14306. #if WASM_ENABLE_FAST_INTERP != 0
  14307. if (loader_ctx->p_code_compiled == NULL)
  14308. goto re_scan;
  14309. func->const_cell_num = loader_ctx->const_cell_num;
  14310. if (func->const_cell_num > 0) {
  14311. int32 j;
  14312. if (!(func->consts = func_const = loader_malloc(
  14313. func->const_cell_num * 4, error_buf, error_buf_size)))
  14314. goto fail;
  14315. func_const_end = func->consts + func->const_cell_num * 4;
  14316. /* reverse the const buf */
  14317. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  14318. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  14319. if (c->value_type == VALUE_TYPE_F64
  14320. || c->value_type == VALUE_TYPE_I64) {
  14321. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14322. &(c->value.f64), (uint32)sizeof(int64));
  14323. func_const += sizeof(int64);
  14324. }
  14325. else {
  14326. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14327. &(c->value.f32), (uint32)sizeof(int32));
  14328. func_const += sizeof(int32);
  14329. }
  14330. }
  14331. }
  14332. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14333. - loader_ctx->start_dynamic_offset + 1;
  14334. #else
  14335. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14336. #endif
  14337. func->max_block_num = loader_ctx->max_csp_num;
  14338. return_value = true;
  14339. fail:
  14340. wasm_loader_ctx_destroy(loader_ctx);
  14341. (void)table_idx;
  14342. (void)table_seg_idx;
  14343. (void)data_seg_idx;
  14344. (void)i64_const;
  14345. (void)local_offset;
  14346. (void)p_org;
  14347. (void)mem_offset;
  14348. (void)align;
  14349. return return_value;
  14350. }