wasm_loader.c 585 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "wasm.h"
  9. #include "wasm_opcode.h"
  10. #include "wasm_runtime.h"
  11. #include "wasm_loader_common.h"
  12. #include "../common/wasm_native.h"
  13. #include "../common/wasm_memory.h"
  14. #if WASM_ENABLE_GC != 0
  15. #include "../common/gc/gc_type.h"
  16. #include "../common/gc/gc_object.h"
  17. #endif
  18. #if WASM_ENABLE_DEBUG_INTERP != 0
  19. #include "../libraries/debug-engine/debug_engine.h"
  20. #endif
  21. #if WASM_ENABLE_FAST_JIT != 0
  22. #include "../fast-jit/jit_compiler.h"
  23. #include "../fast-jit/jit_codecache.h"
  24. #endif
  25. #if WASM_ENABLE_JIT != 0
  26. #include "../compilation/aot_llvm.h"
  27. #endif
  28. #ifndef TRACE_WASM_LOADER
  29. #define TRACE_WASM_LOADER 0
  30. #endif
  31. /* Read a value of given type from the address pointed to by the given
  32. pointer and increase the pointer to the position just after the
  33. value being read. */
  34. #define TEMPLATE_READ_VALUE(Type, p) \
  35. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  36. #if WASM_ENABLE_MEMORY64 != 0
  37. static bool
  38. has_module_memory64(WASMModule *module)
  39. {
  40. /* TODO: multi-memories for now assuming the memory idx type is consistent
  41. * across multi-memories */
  42. if (module->import_memory_count > 0)
  43. return !!(module->import_memories[0].u.memory.mem_type.flags
  44. & MEMORY64_FLAG);
  45. else if (module->memory_count > 0)
  46. return !!(module->memories[0].flags & MEMORY64_FLAG);
  47. return false;
  48. }
  49. #endif
  50. static void
  51. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  52. {
  53. wasm_loader_set_error_buf(error_buf, error_buf_size, string, false);
  54. }
  55. #if WASM_ENABLE_MEMORY64 != 0
  56. static void
  57. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  58. {
  59. if (error_buf != NULL) {
  60. snprintf(error_buf, error_buf_size, "offset out of range");
  61. }
  62. }
  63. #endif
  64. static void
  65. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  66. {
  67. va_list args;
  68. char buf[128];
  69. if (error_buf != NULL) {
  70. va_start(args, format);
  71. vsnprintf(buf, sizeof(buf), format, args);
  72. va_end(args);
  73. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  74. }
  75. }
  76. static bool
  77. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  78. char *error_buf, uint32 error_buf_size)
  79. {
  80. if ((uintptr_t)buf + length < (uintptr_t)buf
  81. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  82. set_error_buf(error_buf, error_buf_size,
  83. "unexpected end of section or function");
  84. return false;
  85. }
  86. return true;
  87. }
  88. static bool
  89. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  90. char *error_buf, uint32 error_buf_size)
  91. {
  92. if ((uintptr_t)buf + length < (uintptr_t)buf
  93. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  94. set_error_buf(error_buf, error_buf_size, "unexpected end");
  95. return false;
  96. }
  97. return true;
  98. }
  99. #define CHECK_BUF(buf, buf_end, length) \
  100. do { \
  101. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  102. goto fail; \
  103. } \
  104. } while (0)
  105. #define CHECK_BUF1(buf, buf_end, length) \
  106. do { \
  107. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  108. goto fail; \
  109. } \
  110. } while (0)
  111. #define skip_leb(p) while (*p++ & 0x80)
  112. #define skip_leb_int64(p, p_end) skip_leb(p)
  113. #define skip_leb_uint32(p, p_end) skip_leb(p)
  114. #define skip_leb_int32(p, p_end) skip_leb(p)
  115. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  116. #define skip_leb_memidx(p, p_end) skip_leb(p)
  117. #if WASM_ENABLE_MULTI_MEMORY == 0
  118. #define skip_leb_align(p, p_end) skip_leb(p)
  119. #else
  120. /* Skip the following memidx if applicable */
  121. #define skip_leb_align(p, p_end) \
  122. do { \
  123. if (*p++ & OPT_MEMIDX_FLAG) \
  124. skip_leb_uint32(p, p_end); \
  125. } while (0)
  126. #endif
  127. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  128. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  129. #define read_leb_int64(p, p_end, res) \
  130. do { \
  131. uint64 res64; \
  132. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  133. error_buf_size)) \
  134. goto fail; \
  135. res = (int64)res64; \
  136. } while (0)
  137. #if WASM_ENABLE_MEMORY64 != 0
  138. #define read_leb_mem_offset(p, p_end, res) \
  139. do { \
  140. uint64 res64; \
  141. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  142. &res64, error_buf, error_buf_size)) { \
  143. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  144. goto fail; \
  145. } \
  146. res = (mem_offset_t)res64; \
  147. } while (0)
  148. #else
  149. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  150. #endif
  151. #define read_leb_uint32(p, p_end, res) \
  152. do { \
  153. uint64 res64; \
  154. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  155. error_buf_size)) \
  156. goto fail; \
  157. res = (uint32)res64; \
  158. } while (0)
  159. #define read_leb_int32(p, p_end, res) \
  160. do { \
  161. uint64 res64; \
  162. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  163. error_buf_size)) \
  164. goto fail; \
  165. res = (int32)res64; \
  166. } while (0)
  167. #define read_leb_memidx(p, p_end, res) read_leb_uint32(p, p_end, res)
  168. #if WASM_ENABLE_MULTI_MEMORY != 0
  169. #define check_memidx(module, memidx) \
  170. do { \
  171. if (memidx >= module->import_memory_count + module->memory_count) { \
  172. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d", \
  173. memidx); \
  174. goto fail; \
  175. } \
  176. } while (0)
  177. /* Bit 6(0x40) indicating the optional memidx, and reset bit 6 for
  178. * alignment check */
  179. #define read_leb_memarg(p, p_end, res) \
  180. do { \
  181. read_leb_uint32(p, p_end, res); \
  182. if (res & OPT_MEMIDX_FLAG) { \
  183. res &= ~OPT_MEMIDX_FLAG; \
  184. read_leb_uint32(p, p_end, memidx); /* memidx */ \
  185. check_memidx(module, memidx); \
  186. } \
  187. } while (0)
  188. #else
  189. /* reserved byte 0x00 */
  190. #define check_memidx(module, memidx) \
  191. do { \
  192. (void)module; \
  193. if (memidx != 0) { \
  194. set_error_buf(error_buf, error_buf_size, "zero byte expected"); \
  195. goto fail; \
  196. } \
  197. } while (0)
  198. #define read_leb_memarg(p, p_end, res) read_leb_uint32(p, p_end, res)
  199. #endif
  200. static char *
  201. type2str(uint8 type)
  202. {
  203. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  204. #if WASM_ENABLE_GC != 0
  205. char *type_str_ref[] = { "stringview_iter",
  206. "stringview_wtf16",
  207. "(ref null ht)",
  208. "(ref ht)",
  209. "", /* reserved */
  210. "stringview_wtf8",
  211. "stringref",
  212. "", /* reserved */
  213. "", /* reserved */
  214. "arrayref",
  215. "structref",
  216. "i32ref",
  217. "eqref",
  218. "anyref",
  219. "externref",
  220. "funcref",
  221. "nullref",
  222. "nullexternref",
  223. "nullfuncref" };
  224. #endif
  225. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  226. return type_str[type - VALUE_TYPE_V128];
  227. #if WASM_ENABLE_GC != 0
  228. else if (wasm_is_type_reftype(type))
  229. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  230. #endif
  231. else if (type == VALUE_TYPE_FUNCREF)
  232. return "funcref";
  233. else if (type == VALUE_TYPE_EXTERNREF)
  234. return "externref";
  235. else
  236. return "unknown type";
  237. }
  238. static bool
  239. is_32bit_type(uint8 type)
  240. {
  241. if (type == VALUE_TYPE_I32
  242. || type == VALUE_TYPE_F32
  243. /* the operand stack is in polymorphic state */
  244. || type == VALUE_TYPE_ANY
  245. #if WASM_ENABLE_GC != 0
  246. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  247. #elif WASM_ENABLE_REF_TYPES != 0
  248. /* For reference types, we use uint32 index to represent
  249. the funcref and externref */
  250. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  251. #endif
  252. )
  253. return true;
  254. return false;
  255. }
  256. static bool
  257. is_64bit_type(uint8 type)
  258. {
  259. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  260. #if WASM_ENABLE_GC != 0
  261. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  262. #endif
  263. )
  264. return true;
  265. return false;
  266. }
  267. #if WASM_ENABLE_GC != 0
  268. static bool
  269. is_packed_type(uint8 type)
  270. {
  271. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  272. }
  273. #endif
  274. static bool
  275. is_byte_a_type(uint8 type)
  276. {
  277. return (is_valid_value_type_for_interpreter(type)
  278. || (type == VALUE_TYPE_VOID))
  279. ? true
  280. : false;
  281. }
  282. #if WASM_ENABLE_SIMD != 0
  283. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  284. static V128
  285. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  286. {
  287. V128 result;
  288. uint8 i;
  289. for (i = 0; i != 16; ++i) {
  290. result.i8x16[i] = read_uint8(p_buf);
  291. }
  292. return result;
  293. }
  294. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  295. #endif /* end of WASM_ENABLE_SIMD */
  296. static void *
  297. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  298. {
  299. void *mem;
  300. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  301. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  302. return NULL;
  303. }
  304. memset(mem, 0, (uint32)size);
  305. return mem;
  306. }
  307. static void *
  308. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  309. uint32 error_buf_size)
  310. {
  311. uint8 *mem_new;
  312. bh_assert(size_new > size_old);
  313. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  314. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  315. memset(mem_new + size_old, 0, size_new - size_old);
  316. wasm_runtime_free(mem_old);
  317. }
  318. return mem_new;
  319. }
  320. #define MEM_REALLOC(mem, size_old, size_new) \
  321. do { \
  322. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  323. error_buf_size); \
  324. if (!mem_new) \
  325. goto fail; \
  326. mem = mem_new; \
  327. } while (0)
  328. #if WASM_ENABLE_GC != 0
  329. static bool
  330. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  331. char *error_buf, uint32 error_buf_size)
  332. {
  333. if (type_index >= type_count) {
  334. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  335. type_index);
  336. return false;
  337. }
  338. return true;
  339. }
  340. static bool
  341. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  342. uint32 error_buf_size)
  343. {
  344. if (!check_type_index(module, module->type_count, type_index, error_buf,
  345. error_buf_size)) {
  346. return false;
  347. }
  348. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  349. set_error_buf(error_buf, error_buf_size, "unkown array type");
  350. return false;
  351. }
  352. return true;
  353. }
  354. #endif
  355. static bool
  356. check_function_index(const WASMModule *module, uint32 function_index,
  357. char *error_buf, uint32 error_buf_size)
  358. {
  359. if (function_index
  360. >= module->import_function_count + module->function_count) {
  361. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  362. function_index);
  363. return false;
  364. }
  365. return true;
  366. }
  367. typedef struct InitValue {
  368. uint8 type;
  369. uint8 flag;
  370. #if WASM_ENABLE_GC != 0
  371. uint8 gc_opcode;
  372. WASMRefType ref_type;
  373. #endif
  374. WASMValue value;
  375. } InitValue;
  376. typedef struct ConstExprContext {
  377. uint32 sp;
  378. uint32 size;
  379. WASMModule *module;
  380. InitValue *stack;
  381. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  382. } ConstExprContext;
  383. static void
  384. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  385. {
  386. ctx->sp = 0;
  387. ctx->module = module;
  388. ctx->stack = ctx->data;
  389. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  390. }
  391. static bool
  392. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  393. #if WASM_ENABLE_GC != 0
  394. WASMRefType *ref_type, uint8 gc_opcode,
  395. #endif
  396. WASMValue *value, char *error_buf, uint32 error_buf_size)
  397. {
  398. InitValue *cur_value;
  399. if (ctx->sp >= ctx->size) {
  400. if (ctx->stack != ctx->data) {
  401. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  402. (ctx->size + 4) * sizeof(InitValue));
  403. }
  404. else {
  405. if (!(ctx->stack =
  406. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  407. error_buf, error_buf_size))) {
  408. goto fail;
  409. }
  410. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  411. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  412. }
  413. ctx->size += 4;
  414. }
  415. cur_value = &ctx->stack[ctx->sp++];
  416. cur_value->type = type;
  417. cur_value->flag = flag;
  418. cur_value->value = *value;
  419. #if WASM_ENABLE_GC != 0
  420. cur_value->gc_opcode = gc_opcode;
  421. if (wasm_is_type_multi_byte_type(type)) {
  422. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  423. ref_type, wasm_reftype_struct_size(ref_type));
  424. }
  425. #endif
  426. return true;
  427. fail:
  428. return false;
  429. }
  430. #if WASM_ENABLE_GC != 0
  431. static void
  432. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  433. {
  434. WASMStructNewInitValues *struct_init_values =
  435. (WASMStructNewInitValues *)data;
  436. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  437. WASMType *wasm_type;
  438. uint32 i;
  439. if (!data)
  440. return;
  441. wasm_type = module->types[struct_init_values->type_idx];
  442. /* The data can only be type of `WASMStructNewInitValues *`
  443. or `WASMArrayNewInitValues *` */
  444. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  445. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  446. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  447. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  448. WASMRefTypeMap *ref_type_map = struct_type->ref_type_maps;
  449. WASMRefType *ref_type;
  450. uint8 field_type;
  451. for (i = 0; i < struct_init_values->count; i++) {
  452. field_type = struct_type->fields[i].field_type;
  453. if (wasm_is_type_multi_byte_type(field_type))
  454. ref_type = ref_type_map->ref_type;
  455. else
  456. ref_type = NULL;
  457. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  458. REF_TYPE_STRUCTREF, NULL,
  459. module->types, module->type_count)
  460. || wasm_reftype_is_subtype_of(
  461. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  462. module->types, module->type_count)) {
  463. destroy_init_expr_data_recursive(
  464. module, struct_init_values->fields[i].data);
  465. }
  466. }
  467. }
  468. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  469. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  470. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  471. uint8 elem_type = array_type->elem_type;
  472. for (i = 0; i < array_init_values->length; i++) {
  473. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  474. REF_TYPE_STRUCTREF, NULL,
  475. module->types, module->type_count)
  476. || wasm_reftype_is_subtype_of(
  477. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  478. module->types, module->type_count)) {
  479. destroy_init_expr_data_recursive(
  480. module, array_init_values->elem_data[i].data);
  481. }
  482. }
  483. }
  484. wasm_runtime_free(data);
  485. }
  486. #endif
  487. static bool
  488. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  489. #if WASM_ENABLE_GC != 0
  490. WASMRefType *ref_type, uint8 *p_gc_opcode,
  491. #endif
  492. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  493. {
  494. InitValue *cur_value;
  495. if (ctx->sp == 0) {
  496. set_error_buf(error_buf, error_buf_size,
  497. "type mismatch: const expr stack underflow");
  498. return false;
  499. }
  500. cur_value = &ctx->stack[--ctx->sp];
  501. #if WASM_ENABLE_GC == 0
  502. if (cur_value->type != type) {
  503. set_error_buf(error_buf, error_buf_size, "type mismatch");
  504. return false;
  505. }
  506. #else
  507. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  508. ref_type, ctx->module->types,
  509. ctx->module->type_count)) {
  510. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  511. "type mismatch: expect ", type2str(type),
  512. " but got other");
  513. goto fail;
  514. }
  515. #endif
  516. if (p_flag)
  517. *p_flag = cur_value->flag;
  518. if (p_value)
  519. *p_value = cur_value->value;
  520. #if WASM_ENABLE_GC != 0
  521. if (p_gc_opcode)
  522. *p_gc_opcode = cur_value->gc_opcode;
  523. #endif
  524. return true;
  525. #if WASM_ENABLE_GC != 0
  526. fail:
  527. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  528. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  529. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  530. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  531. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  532. }
  533. return false;
  534. #endif
  535. }
  536. static void
  537. destroy_const_expr_stack(ConstExprContext *ctx)
  538. {
  539. #if WASM_ENABLE_GC != 0
  540. uint32 i;
  541. for (i = 0; i < ctx->sp; i++) {
  542. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  543. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  544. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  545. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  546. destroy_init_expr_data_recursive(ctx->module,
  547. ctx->stack[i].value.data);
  548. }
  549. }
  550. #endif
  551. if (ctx->stack != ctx->data) {
  552. wasm_runtime_free(ctx->stack);
  553. }
  554. }
  555. #if WASM_ENABLE_GC != 0
  556. static void
  557. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  558. {
  559. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  560. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  561. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  562. destroy_init_expr_data_recursive(module, expr->u.data);
  563. }
  564. }
  565. #endif /* end of WASM_ENABLE_GC != 0 */
  566. static bool
  567. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  568. InitializerExpression *init_expr, uint8 type, void *ref_type,
  569. char *error_buf, uint32 error_buf_size)
  570. {
  571. const uint8 *p = *p_buf, *p_end = buf_end;
  572. uint8 flag, *p_float;
  573. uint32 i;
  574. ConstExprContext const_expr_ctx = { 0 };
  575. WASMValue cur_value;
  576. #if WASM_ENABLE_GC != 0
  577. uint32 opcode1, type_idx;
  578. uint8 opcode;
  579. WASMRefType cur_ref_type = { 0 };
  580. #endif
  581. init_const_expr_stack(&const_expr_ctx, module);
  582. CHECK_BUF(p, p_end, 1);
  583. flag = read_uint8(p);
  584. while (flag != WASM_OP_END) {
  585. switch (flag) {
  586. /* i32.const */
  587. case INIT_EXPR_TYPE_I32_CONST:
  588. read_leb_int32(p, p_end, cur_value.i32);
  589. if (!push_const_expr_stack(
  590. &const_expr_ctx, flag, VALUE_TYPE_I32,
  591. #if WASM_ENABLE_GC != 0
  592. NULL, 0,
  593. #endif
  594. &cur_value, error_buf, error_buf_size))
  595. goto fail;
  596. break;
  597. /* i64.const */
  598. case INIT_EXPR_TYPE_I64_CONST:
  599. read_leb_int64(p, p_end, cur_value.i64);
  600. if (!push_const_expr_stack(
  601. &const_expr_ctx, flag, VALUE_TYPE_I64,
  602. #if WASM_ENABLE_GC != 0
  603. NULL, 0,
  604. #endif
  605. &cur_value, error_buf, error_buf_size))
  606. goto fail;
  607. break;
  608. /* f32.const */
  609. case INIT_EXPR_TYPE_F32_CONST:
  610. CHECK_BUF(p, p_end, 4);
  611. p_float = (uint8 *)&cur_value.f32;
  612. for (i = 0; i < sizeof(float32); i++)
  613. *p_float++ = *p++;
  614. if (!push_const_expr_stack(
  615. &const_expr_ctx, flag, VALUE_TYPE_F32,
  616. #if WASM_ENABLE_GC != 0
  617. NULL, 0,
  618. #endif
  619. &cur_value, error_buf, error_buf_size))
  620. goto fail;
  621. break;
  622. /* f64.const */
  623. case INIT_EXPR_TYPE_F64_CONST:
  624. CHECK_BUF(p, p_end, 8);
  625. p_float = (uint8 *)&cur_value.f64;
  626. for (i = 0; i < sizeof(float64); i++)
  627. *p_float++ = *p++;
  628. if (!push_const_expr_stack(
  629. &const_expr_ctx, flag, VALUE_TYPE_F64,
  630. #if WASM_ENABLE_GC != 0
  631. NULL, 0,
  632. #endif
  633. &cur_value, error_buf, error_buf_size))
  634. goto fail;
  635. break;
  636. #if WASM_ENABLE_SIMD != 0
  637. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  638. /* v128.const */
  639. case INIT_EXPR_TYPE_V128_CONST:
  640. {
  641. uint64 high, low;
  642. CHECK_BUF(p, p_end, 1);
  643. (void)read_uint8(p);
  644. CHECK_BUF(p, p_end, 16);
  645. wasm_runtime_read_v128(p, &high, &low);
  646. p += 16;
  647. cur_value.v128.i64x2[0] = high;
  648. cur_value.v128.i64x2[1] = low;
  649. if (!push_const_expr_stack(
  650. &const_expr_ctx, flag, VALUE_TYPE_V128,
  651. #if WASM_ENABLE_GC != 0
  652. NULL, 0,
  653. #endif
  654. &cur_value, error_buf, error_buf_size))
  655. goto fail;
  656. #if WASM_ENABLE_WAMR_COMPILER != 0
  657. /* If any init_expr is v128.const, mark SIMD used */
  658. module->is_simd_used = true;
  659. #endif
  660. break;
  661. }
  662. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  663. #endif /* end of WASM_ENABLE_SIMD */
  664. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  665. /* ref.func */
  666. case INIT_EXPR_TYPE_FUNCREF_CONST:
  667. {
  668. uint32 func_idx;
  669. read_leb_uint32(p, p_end, func_idx);
  670. cur_value.ref_index = func_idx;
  671. if (!check_function_index(module, func_idx, error_buf,
  672. error_buf_size)) {
  673. goto fail;
  674. }
  675. #if WASM_ENABLE_GC == 0
  676. if (!push_const_expr_stack(&const_expr_ctx, flag,
  677. VALUE_TYPE_FUNCREF, &cur_value,
  678. error_buf, error_buf_size))
  679. goto fail;
  680. #else
  681. if (func_idx < module->import_function_count) {
  682. type_idx =
  683. module->import_functions[func_idx].u.function.type_idx;
  684. }
  685. else {
  686. type_idx = module
  687. ->functions[func_idx
  688. - module->import_function_count]
  689. ->type_idx;
  690. }
  691. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  692. false, type_idx);
  693. if (!push_const_expr_stack(&const_expr_ctx, flag,
  694. cur_ref_type.ref_type, &cur_ref_type,
  695. 0, &cur_value, error_buf,
  696. error_buf_size))
  697. goto fail;
  698. #endif
  699. #if WASM_ENABLE_WAMR_COMPILER != 0
  700. module->is_ref_types_used = true;
  701. #endif
  702. break;
  703. }
  704. /* ref.null */
  705. case INIT_EXPR_TYPE_REFNULL_CONST:
  706. {
  707. uint8 type1;
  708. CHECK_BUF(p, p_end, 1);
  709. type1 = read_uint8(p);
  710. #if WASM_ENABLE_GC == 0
  711. cur_value.ref_index = NULL_REF;
  712. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  713. &cur_value, error_buf,
  714. error_buf_size))
  715. goto fail;
  716. #else
  717. cur_value.gc_obj = NULL_REF;
  718. if (!is_byte_a_type(type1)) {
  719. p--;
  720. read_leb_uint32(p, p_end, type_idx);
  721. if (!check_type_index(module, module->type_count, type_idx,
  722. error_buf, error_buf_size))
  723. goto fail;
  724. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  725. true, type_idx);
  726. if (!push_const_expr_stack(&const_expr_ctx, flag,
  727. cur_ref_type.ref_type,
  728. &cur_ref_type, 0, &cur_value,
  729. error_buf, error_buf_size))
  730. goto fail;
  731. }
  732. else {
  733. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  734. NULL, 0, &cur_value, error_buf,
  735. error_buf_size))
  736. goto fail;
  737. }
  738. #endif
  739. #if WASM_ENABLE_WAMR_COMPILER != 0
  740. module->is_ref_types_used = true;
  741. #endif
  742. break;
  743. }
  744. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  745. /* get_global */
  746. case INIT_EXPR_TYPE_GET_GLOBAL:
  747. {
  748. uint32 global_idx;
  749. uint8 global_type;
  750. read_leb_uint32(p, p_end, cur_value.global_index);
  751. global_idx = cur_value.global_index;
  752. /*
  753. * Currently, constant expressions occurring as initializers
  754. * of globals are further constrained in that contained
  755. * global.get instructions are
  756. * only allowed to refer to imported globals.
  757. *
  758. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  759. */
  760. if (global_idx >= module->import_global_count
  761. /* make spec test happy */
  762. #if WASM_ENABLE_GC != 0
  763. + module->global_count
  764. #endif
  765. ) {
  766. set_error_buf_v(error_buf, error_buf_size,
  767. "unknown global %u", global_idx);
  768. goto fail;
  769. }
  770. if (
  771. /* make spec test happy */
  772. #if WASM_ENABLE_GC != 0
  773. global_idx < module->import_global_count &&
  774. #endif
  775. module->import_globals[global_idx]
  776. .u.global.type.is_mutable) {
  777. set_error_buf_v(error_buf, error_buf_size,
  778. "constant expression required");
  779. goto fail;
  780. }
  781. if (global_idx < module->import_global_count) {
  782. global_type = module->import_globals[global_idx]
  783. .u.global.type.val_type;
  784. #if WASM_ENABLE_GC != 0
  785. if (wasm_is_type_multi_byte_type(global_type)) {
  786. WASMRefType *global_ref_type =
  787. module->import_globals[global_idx]
  788. .u.global.ref_type;
  789. bh_memcpy_s(&cur_ref_type,
  790. wasm_reftype_struct_size(global_ref_type),
  791. global_ref_type,
  792. wasm_reftype_struct_size(global_ref_type));
  793. }
  794. #endif
  795. }
  796. else {
  797. global_type =
  798. module
  799. ->globals[global_idx - module->import_global_count]
  800. .type.val_type;
  801. #if WASM_ENABLE_GC != 0
  802. if (wasm_is_type_multi_byte_type(global_type)) {
  803. WASMRefType *global_ref_type =
  804. module
  805. ->globals[global_idx
  806. - module->import_global_count]
  807. .ref_type;
  808. bh_memcpy_s(&cur_ref_type,
  809. wasm_reftype_struct_size(global_ref_type),
  810. global_ref_type,
  811. wasm_reftype_struct_size(global_ref_type));
  812. }
  813. #endif
  814. }
  815. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  816. #if WASM_ENABLE_GC != 0
  817. &cur_ref_type, 0,
  818. #endif
  819. &cur_value, error_buf,
  820. error_buf_size))
  821. goto fail;
  822. break;
  823. }
  824. #if WASM_ENABLE_GC != 0
  825. /* struct.new and array.new */
  826. case WASM_OP_GC_PREFIX:
  827. {
  828. read_leb_uint32(p, p_end, opcode1);
  829. switch (opcode1) {
  830. case WASM_OP_STRUCT_NEW:
  831. {
  832. WASMStructType *struct_type;
  833. WASMStructNewInitValues *struct_init_values = NULL;
  834. uint32 field_count;
  835. read_leb_uint32(p, p_end, type_idx);
  836. if (!check_type_index(module, module->type_count,
  837. type_idx, error_buf,
  838. error_buf_size)) {
  839. goto fail;
  840. }
  841. struct_type = (WASMStructType *)module->types[type_idx];
  842. if (struct_type->base_type.type_flag
  843. != WASM_TYPE_STRUCT) {
  844. set_error_buf(error_buf, error_buf_size,
  845. "unkown struct type");
  846. goto fail;
  847. }
  848. field_count = struct_type->field_count;
  849. if (!(struct_init_values = loader_malloc(
  850. offsetof(WASMStructNewInitValues, fields)
  851. + (uint64)field_count * sizeof(WASMValue),
  852. error_buf, error_buf_size))) {
  853. goto fail;
  854. }
  855. struct_init_values->type_idx = type_idx;
  856. struct_init_values->count = field_count;
  857. for (i = field_count; i > 0; i--) {
  858. WASMRefType *field_ref_type = NULL;
  859. uint32 field_idx = i - 1;
  860. uint8 field_type =
  861. struct_type->fields[field_idx].field_type;
  862. if (wasm_is_type_multi_byte_type(field_type)) {
  863. field_ref_type = wasm_reftype_map_find(
  864. struct_type->ref_type_maps,
  865. struct_type->ref_type_map_count, field_idx);
  866. }
  867. if (is_packed_type(field_type)) {
  868. field_type = VALUE_TYPE_I32;
  869. }
  870. if (!pop_const_expr_stack(
  871. &const_expr_ctx, NULL, field_type,
  872. field_ref_type, NULL,
  873. &struct_init_values->fields[field_idx],
  874. error_buf, error_buf_size)) {
  875. destroy_init_expr_data_recursive(
  876. module, struct_init_values);
  877. goto fail;
  878. }
  879. }
  880. cur_value.data = struct_init_values;
  881. wasm_set_refheaptype_typeidx(
  882. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  883. if (!push_const_expr_stack(
  884. &const_expr_ctx, flag, cur_ref_type.ref_type,
  885. &cur_ref_type, (uint8)opcode1, &cur_value,
  886. error_buf, error_buf_size)) {
  887. destroy_init_expr_data_recursive(
  888. module, struct_init_values);
  889. goto fail;
  890. }
  891. break;
  892. }
  893. case WASM_OP_STRUCT_NEW_DEFAULT:
  894. {
  895. read_leb_uint32(p, p_end, cur_value.type_index);
  896. type_idx = cur_value.type_index;
  897. if (!check_type_index(module, module->type_count,
  898. type_idx, error_buf,
  899. error_buf_size)) {
  900. goto fail;
  901. }
  902. if (module->types[type_idx]->type_flag
  903. != WASM_TYPE_STRUCT) {
  904. set_error_buf(error_buf, error_buf_size,
  905. "unkown struct type");
  906. goto fail;
  907. }
  908. cur_value.type_index = type_idx;
  909. cur_value.data = NULL;
  910. wasm_set_refheaptype_typeidx(
  911. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  912. if (!push_const_expr_stack(
  913. &const_expr_ctx, flag, cur_ref_type.ref_type,
  914. &cur_ref_type, (uint8)opcode1, &cur_value,
  915. error_buf, error_buf_size)) {
  916. goto fail;
  917. }
  918. break;
  919. }
  920. case WASM_OP_ARRAY_NEW:
  921. case WASM_OP_ARRAY_NEW_DEFAULT:
  922. case WASM_OP_ARRAY_NEW_FIXED:
  923. {
  924. WASMArrayNewInitValues *array_init_values = NULL;
  925. WASMArrayType *array_type = NULL;
  926. WASMRefType *elem_ref_type = NULL;
  927. uint64 total_size;
  928. uint8 elem_type;
  929. read_leb_uint32(p, p_end, cur_value.type_index);
  930. type_idx = cur_value.type_index;
  931. if (!check_type_index(module, module->type_count,
  932. type_idx, error_buf,
  933. error_buf_size)) {
  934. goto fail;
  935. }
  936. array_type = (WASMArrayType *)module->types[type_idx];
  937. if (array_type->base_type.type_flag
  938. != WASM_TYPE_ARRAY) {
  939. set_error_buf(error_buf, error_buf_size,
  940. "unkown array type");
  941. goto fail;
  942. }
  943. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  944. elem_type = array_type->elem_type;
  945. if (wasm_is_type_multi_byte_type(elem_type)) {
  946. elem_ref_type = array_type->elem_ref_type;
  947. }
  948. if (is_packed_type(elem_type)) {
  949. elem_type = VALUE_TYPE_I32;
  950. }
  951. if (opcode1 == WASM_OP_ARRAY_NEW) {
  952. WASMValue len_val;
  953. if (!(array_init_values = loader_malloc(
  954. sizeof(WASMArrayNewInitValues),
  955. error_buf, error_buf_size))) {
  956. goto fail;
  957. }
  958. array_init_values->type_idx = type_idx;
  959. if (!pop_const_expr_stack(
  960. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  961. NULL, NULL, &len_val, error_buf,
  962. error_buf_size)) {
  963. destroy_init_expr_data_recursive(
  964. module, array_init_values);
  965. goto fail;
  966. }
  967. array_init_values->length = len_val.i32;
  968. if (!pop_const_expr_stack(
  969. &const_expr_ctx, NULL, elem_type,
  970. elem_ref_type, NULL,
  971. &array_init_values->elem_data[0],
  972. error_buf, error_buf_size)) {
  973. destroy_init_expr_data_recursive(
  974. module, array_init_values);
  975. goto fail;
  976. }
  977. cur_value.data = array_init_values;
  978. }
  979. else {
  980. /* WASM_OP_ARRAY_NEW_FIXED */
  981. uint32 len;
  982. read_leb_uint32(p, p_end, len);
  983. total_size =
  984. (uint64)offsetof(WASMArrayNewInitValues,
  985. elem_data)
  986. + (uint64)sizeof(WASMValue) * len;
  987. if (!(array_init_values =
  988. loader_malloc(total_size, error_buf,
  989. error_buf_size))) {
  990. goto fail;
  991. }
  992. array_init_values->type_idx = type_idx;
  993. array_init_values->length = len;
  994. for (i = len; i > 0; i--) {
  995. if (!pop_const_expr_stack(
  996. &const_expr_ctx, NULL, elem_type,
  997. elem_ref_type, NULL,
  998. &array_init_values
  999. ->elem_data[i - 1],
  1000. error_buf, error_buf_size)) {
  1001. destroy_init_expr_data_recursive(
  1002. module, array_init_values);
  1003. goto fail;
  1004. }
  1005. }
  1006. cur_value.data = array_init_values;
  1007. }
  1008. }
  1009. else {
  1010. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1011. WASMValue len_val;
  1012. uint32 len;
  1013. /* POP(i32) */
  1014. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1015. VALUE_TYPE_I32, NULL,
  1016. NULL, &len_val, error_buf,
  1017. error_buf_size)) {
  1018. goto fail;
  1019. }
  1020. len = len_val.i32;
  1021. cur_value.array_new_default.type_index = type_idx;
  1022. cur_value.array_new_default.length = len;
  1023. }
  1024. wasm_set_refheaptype_typeidx(
  1025. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1026. if (!push_const_expr_stack(
  1027. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1028. &cur_ref_type, (uint8)opcode1, &cur_value,
  1029. error_buf, error_buf_size)) {
  1030. if (array_init_values) {
  1031. destroy_init_expr_data_recursive(
  1032. module, array_init_values);
  1033. }
  1034. goto fail;
  1035. }
  1036. break;
  1037. }
  1038. case WASM_OP_ANY_CONVERT_EXTERN:
  1039. {
  1040. set_error_buf(error_buf, error_buf_size,
  1041. "unsupported constant expression of "
  1042. "extern.internalize");
  1043. goto fail;
  1044. }
  1045. case WASM_OP_EXTERN_CONVERT_ANY:
  1046. {
  1047. set_error_buf(error_buf, error_buf_size,
  1048. "unsupported constant expression of "
  1049. "extern.externalize");
  1050. goto fail;
  1051. }
  1052. case WASM_OP_REF_I31:
  1053. {
  1054. /* POP(i32) */
  1055. if (!pop_const_expr_stack(
  1056. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1057. NULL, &cur_value, error_buf, error_buf_size)) {
  1058. goto fail;
  1059. }
  1060. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1061. false, HEAP_TYPE_I31);
  1062. if (!push_const_expr_stack(
  1063. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1064. &cur_ref_type, (uint8)opcode1, &cur_value,
  1065. error_buf, error_buf_size)) {
  1066. goto fail;
  1067. }
  1068. break;
  1069. }
  1070. default:
  1071. set_error_buf(
  1072. error_buf, error_buf_size,
  1073. "type mismatch or constant expression required");
  1074. goto fail;
  1075. }
  1076. break;
  1077. }
  1078. #endif /* end of WASM_ENABLE_GC != 0 */
  1079. default:
  1080. {
  1081. set_error_buf(error_buf, error_buf_size,
  1082. "illegal opcode "
  1083. "or constant expression required "
  1084. "or type mismatch");
  1085. goto fail;
  1086. }
  1087. }
  1088. CHECK_BUF(p, p_end, 1);
  1089. flag = read_uint8(p);
  1090. }
  1091. /* There should be only one value left on the init value stack */
  1092. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1093. #if WASM_ENABLE_GC != 0
  1094. ref_type, &opcode,
  1095. #endif
  1096. &cur_value, error_buf, error_buf_size)) {
  1097. goto fail;
  1098. }
  1099. if (const_expr_ctx.sp != 0) {
  1100. set_error_buf(error_buf, error_buf_size,
  1101. "type mismatch: illegal constant opcode sequence");
  1102. goto fail;
  1103. }
  1104. init_expr->init_expr_type = flag;
  1105. init_expr->u = cur_value;
  1106. #if WASM_ENABLE_GC != 0
  1107. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1108. switch (opcode) {
  1109. case WASM_OP_STRUCT_NEW:
  1110. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1111. break;
  1112. case WASM_OP_STRUCT_NEW_DEFAULT:
  1113. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1114. break;
  1115. case WASM_OP_ARRAY_NEW:
  1116. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1117. break;
  1118. case WASM_OP_ARRAY_NEW_DEFAULT:
  1119. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1120. break;
  1121. case WASM_OP_ARRAY_NEW_FIXED:
  1122. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1123. break;
  1124. case WASM_OP_REF_I31:
  1125. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1126. break;
  1127. default:
  1128. bh_assert(0);
  1129. break;
  1130. }
  1131. }
  1132. #endif /* end of WASM_ENABLE_GC != 0 */
  1133. *p_buf = p;
  1134. destroy_const_expr_stack(&const_expr_ctx);
  1135. return true;
  1136. fail:
  1137. destroy_const_expr_stack(&const_expr_ctx);
  1138. return false;
  1139. }
  1140. static bool
  1141. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1142. {
  1143. if (mutable >= 2) {
  1144. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1145. return false;
  1146. }
  1147. return true;
  1148. }
  1149. #if WASM_ENABLE_GC != 0
  1150. static void
  1151. destroy_func_type(WASMFuncType *type)
  1152. {
  1153. /* Destroy the reference type hash set */
  1154. if (type->ref_type_maps)
  1155. wasm_runtime_free(type->ref_type_maps);
  1156. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1157. && WASM_ENABLE_LAZY_JIT != 0
  1158. if (type->call_to_llvm_jit_from_fast_jit)
  1159. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1160. #endif
  1161. /* Free the type */
  1162. wasm_runtime_free(type);
  1163. }
  1164. static void
  1165. destroy_struct_type(WASMStructType *type)
  1166. {
  1167. if (type->ref_type_maps)
  1168. wasm_runtime_free(type->ref_type_maps);
  1169. wasm_runtime_free(type);
  1170. }
  1171. static void
  1172. destroy_array_type(WASMArrayType *type)
  1173. {
  1174. wasm_runtime_free(type);
  1175. }
  1176. static void
  1177. destroy_wasm_type(WASMType *type)
  1178. {
  1179. if (type->ref_count > 1) {
  1180. /* The type is referenced by other types
  1181. of current wasm module */
  1182. type->ref_count--;
  1183. return;
  1184. }
  1185. if (type->type_flag == WASM_TYPE_FUNC)
  1186. destroy_func_type((WASMFuncType *)type);
  1187. else if (type->type_flag == WASM_TYPE_STRUCT)
  1188. destroy_struct_type((WASMStructType *)type);
  1189. else if (type->type_flag == WASM_TYPE_ARRAY)
  1190. destroy_array_type((WASMArrayType *)type);
  1191. else {
  1192. bh_assert(0);
  1193. }
  1194. }
  1195. /* Resolve (ref null ht) or (ref ht) */
  1196. static bool
  1197. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1198. WASMModule *module, uint32 type_count, bool nullable,
  1199. WASMRefType *ref_type, char *error_buf,
  1200. uint32 error_buf_size)
  1201. {
  1202. const uint8 *p = *p_buf, *p_end = buf_end;
  1203. ref_type->ref_type =
  1204. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1205. ref_type->ref_ht_common.nullable = nullable;
  1206. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1207. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1208. /* heap type is (type i), i : typeidx, >= 0 */
  1209. if (!check_type_index(module, type_count,
  1210. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1211. error_buf_size)) {
  1212. return false;
  1213. }
  1214. }
  1215. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1216. /* heap type is func, extern, any, eq, i31 or data */
  1217. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1218. return false;
  1219. }
  1220. *p_buf = p;
  1221. return true;
  1222. fail:
  1223. return false;
  1224. }
  1225. static bool
  1226. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1227. WASMModule *module, uint32 type_count,
  1228. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1229. bool allow_packed_type, char *error_buf,
  1230. uint32 error_buf_size)
  1231. {
  1232. const uint8 *p = *p_buf, *p_end = buf_end;
  1233. uint8 type;
  1234. memset(ref_type, 0, sizeof(WASMRefType));
  1235. CHECK_BUF(p, p_end, 1);
  1236. type = read_uint8(p);
  1237. if (wasm_is_reftype_htref_nullable(type)) {
  1238. /* (ref null ht) */
  1239. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1240. ref_type, error_buf, error_buf_size))
  1241. return false;
  1242. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1243. *p_need_ref_type_map = true;
  1244. else {
  1245. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1246. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1247. multi-byte type to one-byte type to reduce the footprint and
  1248. the complexity of type equal/subtype checking */
  1249. ref_type->ref_type =
  1250. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1251. *p_need_ref_type_map = false;
  1252. }
  1253. }
  1254. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1255. /* (ref ht) */
  1256. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1257. ref_type, error_buf, error_buf_size))
  1258. return false;
  1259. *p_need_ref_type_map = true;
  1260. #if WASM_ENABLE_STRINGREF != 0
  1261. /* covert (ref string) to stringref */
  1262. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1263. ref_type->ref_type =
  1264. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1265. *p_need_ref_type_map = false;
  1266. }
  1267. #endif
  1268. }
  1269. else {
  1270. /* type which can be represented by one byte */
  1271. if (!is_valid_value_type_for_interpreter(type)
  1272. && !(allow_packed_type && is_packed_type(type))) {
  1273. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1274. return false;
  1275. }
  1276. ref_type->ref_type = type;
  1277. *p_need_ref_type_map = false;
  1278. #if WASM_ENABLE_WAMR_COMPILER != 0
  1279. /* If any value's type is v128, mark the module as SIMD used */
  1280. if (type == VALUE_TYPE_V128)
  1281. module->is_simd_used = true;
  1282. #endif
  1283. }
  1284. *p_buf = p;
  1285. return true;
  1286. fail:
  1287. return false;
  1288. }
  1289. static WASMRefType *
  1290. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1291. char *error_buf, uint32 error_buf_size)
  1292. {
  1293. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1294. if (!ret) {
  1295. set_error_buf(error_buf, error_buf_size,
  1296. "insert ref type to hash set failed");
  1297. }
  1298. return ret;
  1299. }
  1300. static bool
  1301. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1302. uint32 type_count, uint32 type_idx, char *error_buf,
  1303. uint32 error_buf_size)
  1304. {
  1305. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1306. uint32 param_count, result_count, i, j = 0;
  1307. uint32 param_cell_num, ret_cell_num;
  1308. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1309. uint64 total_size;
  1310. bool need_ref_type_map;
  1311. WASMRefType ref_type;
  1312. WASMFuncType *type = NULL;
  1313. /* Parse first time to resolve param count, result count and
  1314. ref type map count */
  1315. read_leb_uint32(p, p_end, param_count);
  1316. p_org = p;
  1317. for (i = 0; i < param_count; i++) {
  1318. if (!resolve_value_type(&p, p_end, module, type_count,
  1319. &need_ref_type_map, &ref_type, false, error_buf,
  1320. error_buf_size)) {
  1321. return false;
  1322. }
  1323. if (need_ref_type_map)
  1324. ref_type_map_count++;
  1325. }
  1326. read_leb_uint32(p, p_end, result_count);
  1327. for (i = 0; i < result_count; i++) {
  1328. if (!resolve_value_type(&p, p_end, module, type_count,
  1329. &need_ref_type_map, &ref_type, false, error_buf,
  1330. error_buf_size)) {
  1331. return false;
  1332. }
  1333. if (need_ref_type_map) {
  1334. ref_type_map_count++;
  1335. result_ref_type_map_count++;
  1336. }
  1337. }
  1338. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1339. "ref type map count: %d",
  1340. type_idx, param_count, result_count, ref_type_map_count);
  1341. /* Parse second time to resolve param types, result types and
  1342. ref type map info */
  1343. p = p_org;
  1344. total_size = offsetof(WASMFuncType, types)
  1345. + sizeof(uint8) * (uint64)(param_count + result_count);
  1346. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1347. return false;
  1348. }
  1349. if (ref_type_map_count > 0) {
  1350. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1351. if (!(type->ref_type_maps =
  1352. loader_malloc(total_size, error_buf, error_buf_size))) {
  1353. goto fail;
  1354. }
  1355. }
  1356. type->base_type.type_flag = WASM_TYPE_FUNC;
  1357. type->param_count = param_count;
  1358. type->result_count = result_count;
  1359. type->ref_type_map_count = ref_type_map_count;
  1360. if (ref_type_map_count > 0) {
  1361. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1362. - result_ref_type_map_count;
  1363. }
  1364. for (i = 0; i < param_count; i++) {
  1365. if (!resolve_value_type(&p, p_end, module, type_count,
  1366. &need_ref_type_map, &ref_type, false, error_buf,
  1367. error_buf_size)) {
  1368. goto fail;
  1369. }
  1370. type->types[i] = ref_type.ref_type;
  1371. if (need_ref_type_map) {
  1372. type->ref_type_maps[j].index = i;
  1373. if (!(type->ref_type_maps[j++].ref_type =
  1374. reftype_set_insert(module->ref_type_set, &ref_type,
  1375. error_buf, error_buf_size))) {
  1376. goto fail;
  1377. }
  1378. }
  1379. }
  1380. read_leb_uint32(p, p_end, result_count);
  1381. for (i = 0; i < result_count; i++) {
  1382. if (!resolve_value_type(&p, p_end, module, type_count,
  1383. &need_ref_type_map, &ref_type, false, error_buf,
  1384. error_buf_size)) {
  1385. goto fail;
  1386. }
  1387. type->types[param_count + i] = ref_type.ref_type;
  1388. if (need_ref_type_map) {
  1389. type->ref_type_maps[j].index = param_count + i;
  1390. if (!(type->ref_type_maps[j++].ref_type =
  1391. reftype_set_insert(module->ref_type_set, &ref_type,
  1392. error_buf, error_buf_size))) {
  1393. goto fail;
  1394. }
  1395. }
  1396. }
  1397. bh_assert(j == type->ref_type_map_count);
  1398. #if TRACE_WASM_LOADER != 0
  1399. os_printf("type %d = ", type_idx);
  1400. wasm_dump_func_type(type);
  1401. #endif
  1402. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1403. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1404. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1405. set_error_buf(error_buf, error_buf_size,
  1406. "param count or result count too large");
  1407. goto fail;
  1408. }
  1409. type->param_cell_num = (uint16)param_cell_num;
  1410. type->ret_cell_num = (uint16)ret_cell_num;
  1411. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1412. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1413. #endif
  1414. #if WASM_ENABLE_WAMR_COMPILER != 0
  1415. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1416. if (type->types[i] == VALUE_TYPE_V128)
  1417. module->is_simd_used = true;
  1418. }
  1419. #endif
  1420. *p_buf = p;
  1421. module->types[type_idx] = (WASMType *)type;
  1422. return true;
  1423. fail:
  1424. if (type)
  1425. destroy_func_type(type);
  1426. return false;
  1427. }
  1428. static bool
  1429. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1430. WASMModule *module, uint32 type_count, uint32 type_idx,
  1431. char *error_buf, uint32 error_buf_size)
  1432. {
  1433. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1434. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1435. uint32 i, j = 0, offset;
  1436. uint16 *reference_table;
  1437. uint64 total_size;
  1438. uint8 mutable;
  1439. bool need_ref_type_map;
  1440. WASMRefType ref_type;
  1441. WASMStructType *type = NULL;
  1442. /* Parse first time to resolve field count and ref type map count */
  1443. read_leb_uint32(p, p_end, field_count);
  1444. p_org = p;
  1445. for (i = 0; i < field_count; i++) {
  1446. if (!resolve_value_type(&p, p_end, module, type_count,
  1447. &need_ref_type_map, &ref_type, true, error_buf,
  1448. error_buf_size)) {
  1449. return false;
  1450. }
  1451. if (need_ref_type_map)
  1452. ref_type_map_count++;
  1453. if (wasm_is_type_reftype(ref_type.ref_type))
  1454. ref_field_count++;
  1455. CHECK_BUF(p, p_end, 1);
  1456. mutable = read_uint8(p);
  1457. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1458. return false;
  1459. }
  1460. }
  1461. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1462. type_idx, field_count, ref_type_map_count);
  1463. /* Parse second time to resolve field types and ref type map info */
  1464. p = p_org;
  1465. total_size = offsetof(WASMStructType, fields)
  1466. + sizeof(WASMStructFieldType) * (uint64)field_count
  1467. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1468. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1469. return false;
  1470. }
  1471. if (ref_type_map_count > 0) {
  1472. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1473. if (!(type->ref_type_maps =
  1474. loader_malloc(total_size, error_buf, error_buf_size))) {
  1475. goto fail;
  1476. }
  1477. }
  1478. type->reference_table = reference_table =
  1479. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1480. + sizeof(WASMStructFieldType) * field_count);
  1481. *reference_table++ = ref_field_count;
  1482. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1483. type->field_count = field_count;
  1484. type->ref_type_map_count = ref_type_map_count;
  1485. offset = (uint32)offsetof(WASMStructObject, field_data);
  1486. for (i = 0; i < field_count; i++) {
  1487. if (!resolve_value_type(&p, p_end, module, type_count,
  1488. &need_ref_type_map, &ref_type, true, error_buf,
  1489. error_buf_size)) {
  1490. goto fail;
  1491. }
  1492. type->fields[i].field_type = ref_type.ref_type;
  1493. if (need_ref_type_map) {
  1494. type->ref_type_maps[j].index = i;
  1495. if (!(type->ref_type_maps[j++].ref_type =
  1496. reftype_set_insert(module->ref_type_set, &ref_type,
  1497. error_buf, error_buf_size))) {
  1498. goto fail;
  1499. }
  1500. }
  1501. CHECK_BUF(p, p_end, 1);
  1502. type->fields[i].field_flags = read_uint8(p);
  1503. type->fields[i].field_size =
  1504. (uint8)wasm_reftype_size(ref_type.ref_type);
  1505. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1506. || defined(BUILD_TARGET_X86_32))
  1507. if (type->fields[i].field_size == 2)
  1508. offset = align_uint(offset, 2);
  1509. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1510. offset = align_uint(offset, 4);
  1511. #endif
  1512. type->fields[i].field_offset = offset;
  1513. if (wasm_is_type_reftype(ref_type.ref_type))
  1514. *reference_table++ = offset;
  1515. offset += type->fields[i].field_size;
  1516. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1517. type->fields[i].field_flags, type->fields[i].field_type);
  1518. }
  1519. type->total_size = offset;
  1520. bh_assert(j == type->ref_type_map_count);
  1521. #if TRACE_WASM_LOADER != 0
  1522. os_printf("type %d = ", type_idx);
  1523. wasm_dump_struct_type(type);
  1524. #endif
  1525. *p_buf = p;
  1526. module->types[type_idx] = (WASMType *)type;
  1527. return true;
  1528. fail:
  1529. if (type)
  1530. destroy_struct_type(type);
  1531. return false;
  1532. }
  1533. static bool
  1534. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1535. WASMModule *module, uint32 type_count, uint32 type_idx,
  1536. char *error_buf, uint32 error_buf_size)
  1537. {
  1538. const uint8 *p = *p_buf, *p_end = buf_end;
  1539. uint8 mutable;
  1540. bool need_ref_type_map;
  1541. WASMRefType ref_type;
  1542. WASMArrayType *type = NULL;
  1543. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1544. &ref_type, true, error_buf, error_buf_size)) {
  1545. return false;
  1546. }
  1547. CHECK_BUF(p, p_end, 1);
  1548. mutable = read_uint8(p);
  1549. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1550. return false;
  1551. }
  1552. LOG_VERBOSE("type %u: array", type_idx);
  1553. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1554. error_buf_size))) {
  1555. return false;
  1556. }
  1557. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1558. type->elem_flags = mutable;
  1559. type->elem_type = ref_type.ref_type;
  1560. if (need_ref_type_map) {
  1561. if (!(type->elem_ref_type =
  1562. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1563. error_buf_size))) {
  1564. goto fail;
  1565. }
  1566. }
  1567. #if TRACE_WASM_LOADER != 0
  1568. os_printf("type %d = ", type_idx);
  1569. wasm_dump_array_type(type);
  1570. #endif
  1571. *p_buf = p;
  1572. module->types[type_idx] = (WASMType *)type;
  1573. return true;
  1574. fail:
  1575. if (type)
  1576. destroy_array_type(type);
  1577. return false;
  1578. }
  1579. static bool
  1580. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1581. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1582. {
  1583. if (heap_type >= 0) {
  1584. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1585. error_buf_size)) {
  1586. return false;
  1587. }
  1588. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1589. heap_type);
  1590. }
  1591. else {
  1592. if (!wasm_is_valid_heap_type(heap_type)) {
  1593. set_error_buf(error_buf, error_buf_size, "unknown type");
  1594. return false;
  1595. }
  1596. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1597. heap_type);
  1598. if (nullable) {
  1599. /* For (ref null func/extern/any/eq/i31/data),
  1600. they are same as
  1601. funcref/externref/anyref/eqref/i31ref/dataref,
  1602. we convert the multi-byte type to one-byte
  1603. type to reduce the footprint and the
  1604. complexity of type equal/subtype checking */
  1605. ref_type->ref_type =
  1606. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1607. }
  1608. }
  1609. return true;
  1610. }
  1611. static void
  1612. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1613. WASMRefType *ref_type2)
  1614. {
  1615. /**
  1616. * The difference rt1 ∖ rt2 between two reference types is defined as
  1617. * follows:
  1618. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1619. * (ref ht2) = (ref null?1 ht1)
  1620. */
  1621. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1622. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1623. ref_type1, wasm_reftype_struct_size(ref_type1));
  1624. }
  1625. else {
  1626. ref_type_diff->ref_type = ref_type1->ref_type;
  1627. }
  1628. if (ref_type2->ref_ht_common.nullable) {
  1629. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1630. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1631. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1632. (int32)ref_type_diff->ref_type - 0x80);
  1633. }
  1634. else {
  1635. ref_type_diff->ref_ht_typeidx.nullable = false;
  1636. }
  1637. }
  1638. }
  1639. #else /* else of WASM_ENABLE_GC != 0 */
  1640. static void
  1641. destroy_wasm_type(WASMType *type)
  1642. {
  1643. if (type->ref_count > 1) {
  1644. /* The type is referenced by other types
  1645. of current wasm module */
  1646. type->ref_count--;
  1647. return;
  1648. }
  1649. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1650. && WASM_ENABLE_LAZY_JIT != 0
  1651. if (type->call_to_llvm_jit_from_fast_jit)
  1652. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1653. #endif
  1654. wasm_runtime_free(type);
  1655. }
  1656. #endif /* end of WASM_ENABLE_GC != 0 */
  1657. static bool
  1658. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1659. char *error_buf, uint32 error_buf_size)
  1660. {
  1661. const uint8 *p = buf, *p_end = buf_end;
  1662. uint32 type_count, i;
  1663. uint64 total_size;
  1664. uint8 flag;
  1665. #if WASM_ENABLE_GC != 0
  1666. uint32 processed_type_count = 0;
  1667. #endif
  1668. read_leb_uint32(p, p_end, type_count);
  1669. if (type_count) {
  1670. module->type_count = type_count;
  1671. total_size = sizeof(WASMType *) * (uint64)type_count;
  1672. if (!(module->types =
  1673. loader_malloc(total_size, error_buf, error_buf_size))) {
  1674. return false;
  1675. }
  1676. #if WASM_ENABLE_GC == 0
  1677. for (i = 0; i < type_count; i++) {
  1678. WASMFuncType *type;
  1679. const uint8 *p_org;
  1680. uint32 param_count, result_count, j;
  1681. uint32 param_cell_num, ret_cell_num;
  1682. CHECK_BUF(p, p_end, 1);
  1683. flag = read_uint8(p);
  1684. if (flag != 0x60) {
  1685. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1686. return false;
  1687. }
  1688. read_leb_uint32(p, p_end, param_count);
  1689. /* Resolve param count and result count firstly */
  1690. p_org = p;
  1691. CHECK_BUF(p, p_end, param_count);
  1692. p += param_count;
  1693. read_leb_uint32(p, p_end, result_count);
  1694. CHECK_BUF(p, p_end, result_count);
  1695. p = p_org;
  1696. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1697. set_error_buf(error_buf, error_buf_size,
  1698. "param count or result count too large");
  1699. return false;
  1700. }
  1701. total_size = offsetof(WASMFuncType, types)
  1702. + sizeof(uint8) * (uint64)(param_count + result_count);
  1703. if (!(type = module->types[i] =
  1704. loader_malloc(total_size, error_buf, error_buf_size))) {
  1705. return false;
  1706. }
  1707. /* Resolve param types and result types */
  1708. type->ref_count = 1;
  1709. type->param_count = (uint16)param_count;
  1710. type->result_count = (uint16)result_count;
  1711. for (j = 0; j < param_count; j++) {
  1712. CHECK_BUF(p, p_end, 1);
  1713. type->types[j] = read_uint8(p);
  1714. }
  1715. read_leb_uint32(p, p_end, result_count);
  1716. for (j = 0; j < result_count; j++) {
  1717. CHECK_BUF(p, p_end, 1);
  1718. type->types[param_count + j] = read_uint8(p);
  1719. }
  1720. for (j = 0; j < param_count + result_count; j++) {
  1721. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  1722. set_error_buf(error_buf, error_buf_size,
  1723. "unknown value type");
  1724. return false;
  1725. }
  1726. }
  1727. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1728. ret_cell_num =
  1729. wasm_get_cell_num(type->types + param_count, result_count);
  1730. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1731. set_error_buf(error_buf, error_buf_size,
  1732. "param count or result count too large");
  1733. return false;
  1734. }
  1735. type->param_cell_num = (uint16)param_cell_num;
  1736. type->ret_cell_num = (uint16)ret_cell_num;
  1737. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1738. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1739. #endif
  1740. #if WASM_ENABLE_WAMR_COMPILER != 0
  1741. for (j = 0; j < type->param_count + type->result_count; j++) {
  1742. if (type->types[j] == VALUE_TYPE_V128)
  1743. module->is_simd_used = true;
  1744. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1745. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1746. module->is_ref_types_used = true;
  1747. }
  1748. #endif
  1749. /* If there is already a same type created, use it instead */
  1750. for (j = 0; j < i; j++) {
  1751. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1752. if (module->types[j]->ref_count == UINT16_MAX) {
  1753. set_error_buf(error_buf, error_buf_size,
  1754. "wasm type's ref count too large");
  1755. return false;
  1756. }
  1757. destroy_wasm_type(type);
  1758. module->types[i] = module->types[j];
  1759. module->types[j]->ref_count++;
  1760. break;
  1761. }
  1762. }
  1763. }
  1764. #else /* else of WASM_ENABLE_GC == 0 */
  1765. for (i = 0; i < type_count; i++) {
  1766. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1767. uint32 rec_count = 1, j;
  1768. bool is_sub_final = true;
  1769. CHECK_BUF(p, p_end, 1);
  1770. flag = read_uint8(p);
  1771. if (flag == DEFINED_TYPE_REC) {
  1772. read_leb_uint32(p, p_end, rec_count);
  1773. if (rec_count > 1) {
  1774. uint64 new_total_size;
  1775. /* integer overflow */
  1776. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1777. set_error_buf(error_buf, error_buf_size,
  1778. "recursive type count too large");
  1779. return false;
  1780. }
  1781. module->type_count += rec_count - 1;
  1782. new_total_size =
  1783. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1784. if (new_total_size > UINT32_MAX) {
  1785. set_error_buf(error_buf, error_buf_size,
  1786. "allocate memory failed");
  1787. return false;
  1788. }
  1789. MEM_REALLOC(module->types, (uint32)total_size,
  1790. (uint32)new_total_size);
  1791. total_size = new_total_size;
  1792. }
  1793. LOG_VERBOSE("Processing rec group [%d-%d]",
  1794. processed_type_count,
  1795. processed_type_count + rec_count - 1);
  1796. }
  1797. else {
  1798. p--;
  1799. }
  1800. for (j = 0; j < rec_count; j++) {
  1801. WASMType *cur_type = NULL;
  1802. CHECK_BUF(p, p_end, 1);
  1803. flag = read_uint8(p);
  1804. parent_type_idx = -1;
  1805. if (flag == DEFINED_TYPE_SUB
  1806. || flag == DEFINED_TYPE_SUB_FINAL) {
  1807. read_leb_uint32(p, p_end, super_type_count);
  1808. if (super_type_count > 1) {
  1809. set_error_buf(error_buf, error_buf_size,
  1810. "super type count too large");
  1811. return false;
  1812. }
  1813. if (super_type_count > 0) {
  1814. read_leb_uint32(p, p_end, parent_type_idx);
  1815. if (parent_type_idx >= processed_type_count + j) {
  1816. set_error_buf_v(error_buf, error_buf_size,
  1817. "unknown type %d", parent_type_idx);
  1818. return false;
  1819. }
  1820. if (module->types[parent_type_idx]->is_sub_final) {
  1821. set_error_buf(error_buf, error_buf_size,
  1822. "sub type can not inherit from "
  1823. "a final super type");
  1824. return false;
  1825. }
  1826. }
  1827. if (flag == DEFINED_TYPE_SUB)
  1828. is_sub_final = false;
  1829. CHECK_BUF(p, p_end, 1);
  1830. flag = read_uint8(p);
  1831. }
  1832. if (flag == DEFINED_TYPE_FUNC) {
  1833. if (!resolve_func_type(&p, buf_end, module,
  1834. processed_type_count + rec_count,
  1835. processed_type_count + j, error_buf,
  1836. error_buf_size)) {
  1837. return false;
  1838. }
  1839. }
  1840. else if (flag == DEFINED_TYPE_STRUCT) {
  1841. if (!resolve_struct_type(&p, buf_end, module,
  1842. processed_type_count + rec_count,
  1843. processed_type_count + j,
  1844. error_buf, error_buf_size)) {
  1845. return false;
  1846. }
  1847. }
  1848. else if (flag == DEFINED_TYPE_ARRAY) {
  1849. if (!resolve_array_type(&p, buf_end, module,
  1850. processed_type_count + rec_count,
  1851. processed_type_count + j, error_buf,
  1852. error_buf_size)) {
  1853. return false;
  1854. }
  1855. }
  1856. else {
  1857. set_error_buf(error_buf, error_buf_size,
  1858. "invalid type flag");
  1859. return false;
  1860. }
  1861. cur_type = module->types[processed_type_count + j];
  1862. cur_type->ref_count = 1;
  1863. cur_type->parent_type_idx = parent_type_idx;
  1864. cur_type->is_sub_final = is_sub_final;
  1865. cur_type->rec_count = rec_count;
  1866. cur_type->rec_idx = j;
  1867. cur_type->rec_begin_type_idx = processed_type_count;
  1868. }
  1869. /* resolve subtyping relationship in current rec group */
  1870. for (j = 0; j < rec_count; j++) {
  1871. WASMType *cur_type = module->types[processed_type_count + j];
  1872. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1873. WASMType *parent_type =
  1874. module->types[cur_type->parent_type_idx];
  1875. cur_type->parent_type = parent_type;
  1876. cur_type->root_type = parent_type->root_type;
  1877. if (parent_type->inherit_depth == UINT16_MAX) {
  1878. set_error_buf(error_buf, error_buf_size,
  1879. "parent type's inherit depth too large");
  1880. return false;
  1881. }
  1882. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1883. }
  1884. else {
  1885. cur_type->parent_type = NULL;
  1886. cur_type->root_type = cur_type;
  1887. cur_type->inherit_depth = 0;
  1888. }
  1889. }
  1890. for (j = 0; j < rec_count; j++) {
  1891. WASMType *cur_type = module->types[processed_type_count + j];
  1892. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1893. WASMType *parent_type =
  1894. module->types[cur_type->parent_type_idx];
  1895. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1896. module->types,
  1897. module->type_count)) {
  1898. set_error_buf(error_buf, error_buf_size,
  1899. "sub type does not match super type");
  1900. return false;
  1901. }
  1902. }
  1903. }
  1904. /* If there is already an equivalence type or a group of equivalence
  1905. recursive types created, use it or them instead */
  1906. for (j = 0; j < processed_type_count;) {
  1907. WASMType *src_type = module->types[j];
  1908. WASMType *cur_type = module->types[processed_type_count];
  1909. uint32 k, src_rec_count;
  1910. src_rec_count = src_type->rec_count;
  1911. if (src_rec_count != rec_count) {
  1912. /* no type equivalence */
  1913. j += src_rec_count;
  1914. continue;
  1915. }
  1916. for (k = 0; k < rec_count; k++) {
  1917. src_type = module->types[j + k];
  1918. cur_type = module->types[processed_type_count + k];
  1919. if (!wasm_type_equal(src_type, cur_type, module->types,
  1920. module->type_count)) {
  1921. break;
  1922. }
  1923. }
  1924. if (k < rec_count) {
  1925. /* no type equivalence */
  1926. j += src_rec_count;
  1927. continue;
  1928. }
  1929. /* type equivalence */
  1930. for (k = 0; k < rec_count; k++) {
  1931. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1932. set_error_buf(error_buf, error_buf_size,
  1933. "wasm type's ref count too large");
  1934. return false;
  1935. }
  1936. destroy_wasm_type(module->types[processed_type_count + k]);
  1937. module->types[processed_type_count + k] =
  1938. module->types[j + k];
  1939. module->types[j + k]->ref_count++;
  1940. }
  1941. break;
  1942. }
  1943. if (rec_count > 1) {
  1944. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1945. processed_type_count,
  1946. processed_type_count + rec_count - 1);
  1947. }
  1948. processed_type_count += rec_count;
  1949. }
  1950. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1951. * module->type_count,
  1952. error_buf, error_buf_size))) {
  1953. return false;
  1954. }
  1955. #endif /* end of WASM_ENABLE_GC == 0 */
  1956. }
  1957. if (p != p_end) {
  1958. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1959. return false;
  1960. }
  1961. LOG_VERBOSE("Load type section success.\n");
  1962. return true;
  1963. fail:
  1964. return false;
  1965. }
  1966. static void
  1967. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1968. {
  1969. uint32 default_max_size;
  1970. if (UINT32_MAX / 2 > init_size)
  1971. default_max_size = init_size * 2;
  1972. else
  1973. default_max_size = UINT32_MAX;
  1974. if (default_max_size < WASM_TABLE_MAX_SIZE)
  1975. default_max_size = WASM_TABLE_MAX_SIZE;
  1976. if (max_size_flag) {
  1977. /* module defines the table limitation */
  1978. bh_assert(init_size <= *max_size);
  1979. if (init_size < *max_size) {
  1980. *max_size =
  1981. *max_size < default_max_size ? *max_size : default_max_size;
  1982. }
  1983. }
  1984. else {
  1985. /* partial defined table limitation, gives a default value */
  1986. *max_size = default_max_size;
  1987. }
  1988. }
  1989. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  1990. /**
  1991. * Find export item of a module with export info:
  1992. * module name, field name and export kind
  1993. */
  1994. static WASMExport *
  1995. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  1996. const char *field_name, uint8 export_kind,
  1997. char *error_buf, uint32 error_buf_size)
  1998. {
  1999. WASMExport *export =
  2000. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2001. export_kind, error_buf, error_buf_size);
  2002. return export;
  2003. }
  2004. #endif
  2005. #if WASM_ENABLE_MULTI_MODULE != 0
  2006. static WASMFunction *
  2007. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  2008. const WASMFuncType *expected_function_type,
  2009. char *error_buf, uint32 error_buf_size)
  2010. {
  2011. WASMModuleCommon *module_reg;
  2012. WASMFunction *function = NULL;
  2013. WASMExport *export = NULL;
  2014. WASMModule *module = NULL;
  2015. WASMFuncType *target_function_type = NULL;
  2016. module_reg = wasm_runtime_find_module_registered(module_name);
  2017. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2018. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  2019. function_name);
  2020. set_error_buf(error_buf, error_buf_size, "unknown import");
  2021. return NULL;
  2022. }
  2023. module = (WASMModule *)module_reg;
  2024. export =
  2025. wasm_loader_find_export(module, module_name, function_name,
  2026. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  2027. if (!export) {
  2028. return NULL;
  2029. }
  2030. /* resolve function type and function */
  2031. if (export->index < module->import_function_count) {
  2032. target_function_type =
  2033. module->import_functions[export->index].u.function.func_type;
  2034. function = module->import_functions[export->index]
  2035. .u.function.import_func_linked;
  2036. }
  2037. else {
  2038. target_function_type =
  2039. module->functions[export->index - module->import_function_count]
  2040. ->func_type;
  2041. function =
  2042. module->functions[export->index - module->import_function_count];
  2043. }
  2044. /* check function type */
  2045. if (!wasm_type_equal((WASMType *)expected_function_type,
  2046. (WASMType *)target_function_type, module->types,
  2047. module->type_count)) {
  2048. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  2049. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2050. return NULL;
  2051. }
  2052. return function;
  2053. }
  2054. static WASMTable *
  2055. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2056. uint32 init_size, uint32 max_size, char *error_buf,
  2057. uint32 error_buf_size)
  2058. {
  2059. WASMModuleCommon *module_reg;
  2060. WASMTable *table = NULL;
  2061. WASMExport *export = NULL;
  2062. WASMModule *module = NULL;
  2063. module_reg = wasm_runtime_find_module_registered(module_name);
  2064. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2065. LOG_DEBUG("can not find a module named %s for table", module_name);
  2066. set_error_buf(error_buf, error_buf_size, "unknown import");
  2067. return NULL;
  2068. }
  2069. module = (WASMModule *)module_reg;
  2070. export =
  2071. wasm_loader_find_export(module, module_name, table_name,
  2072. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2073. if (!export) {
  2074. return NULL;
  2075. }
  2076. /* resolve table and check the init/max size */
  2077. if (export->index < module->import_table_count) {
  2078. table =
  2079. module->import_tables[export->index].u.table.import_table_linked;
  2080. }
  2081. else {
  2082. table = &(module->tables[export->index - module->import_table_count]);
  2083. }
  2084. if (table->table_type.init_size < init_size
  2085. || table->table_type.max_size > max_size) {
  2086. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2087. module_name, table_name, table->table_type.init_size,
  2088. table->table_type.max_size, init_size, max_size);
  2089. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2090. return NULL;
  2091. }
  2092. return table;
  2093. }
  2094. static WASMMemory *
  2095. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2096. uint32 init_page_count, uint32 max_page_count,
  2097. char *error_buf, uint32 error_buf_size)
  2098. {
  2099. WASMModuleCommon *module_reg;
  2100. WASMMemory *memory = NULL;
  2101. WASMExport *export = NULL;
  2102. WASMModule *module = NULL;
  2103. module_reg = wasm_runtime_find_module_registered(module_name);
  2104. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2105. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2106. set_error_buf(error_buf, error_buf_size, "unknown import");
  2107. return NULL;
  2108. }
  2109. module = (WASMModule *)module_reg;
  2110. export =
  2111. wasm_loader_find_export(module, module_name, memory_name,
  2112. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2113. if (!export) {
  2114. return NULL;
  2115. }
  2116. /* resolve memory and check the init/max page count */
  2117. if (export->index < module->import_memory_count) {
  2118. memory = module->import_memories[export->index]
  2119. .u.memory.import_memory_linked;
  2120. }
  2121. else {
  2122. memory =
  2123. &(module->memories[export->index - module->import_memory_count]);
  2124. }
  2125. if (memory->init_page_count < init_page_count
  2126. || memory->max_page_count > max_page_count) {
  2127. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2128. module_name, memory_name, memory->init_page_count,
  2129. memory->max_page_count, init_page_count, max_page_count);
  2130. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2131. return NULL;
  2132. }
  2133. return memory;
  2134. }
  2135. static WASMGlobal *
  2136. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2137. uint8 type, bool is_mutable, char *error_buf,
  2138. uint32 error_buf_size)
  2139. {
  2140. WASMModuleCommon *module_reg;
  2141. WASMGlobal *global = NULL;
  2142. WASMExport *export = NULL;
  2143. WASMModule *module = NULL;
  2144. module_reg = wasm_runtime_find_module_registered(module_name);
  2145. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2146. LOG_DEBUG("can not find a module named %s for global", module_name);
  2147. set_error_buf(error_buf, error_buf_size, "unknown import");
  2148. return NULL;
  2149. }
  2150. module = (WASMModule *)module_reg;
  2151. export =
  2152. wasm_loader_find_export(module, module_name, global_name,
  2153. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2154. if (!export) {
  2155. return NULL;
  2156. }
  2157. /* resolve and check the global */
  2158. if (export->index < module->import_global_count) {
  2159. global =
  2160. module->import_globals[export->index].u.global.import_global_linked;
  2161. }
  2162. else {
  2163. global =
  2164. &(module->globals[export->index - module->import_global_count]);
  2165. }
  2166. if (global->type.val_type != type
  2167. || global->type.is_mutable != is_mutable) {
  2168. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2169. module_name, global_name, global->type.val_type,
  2170. global->type.is_mutable, type, is_mutable);
  2171. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2172. return NULL;
  2173. }
  2174. return global;
  2175. }
  2176. #if WASM_ENABLE_TAGS != 0
  2177. static WASMTag *
  2178. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2179. const WASMType *expected_tag_type,
  2180. uint32 *linked_tag_index, char *error_buf,
  2181. uint32 error_buf_size)
  2182. {
  2183. WASMModuleCommon *module_reg;
  2184. WASMTag *tag = NULL;
  2185. WASMExport *export = NULL;
  2186. WASMModule *module = NULL;
  2187. module_reg = wasm_runtime_find_module_registered(module_name);
  2188. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2189. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2190. tag_name);
  2191. set_error_buf(error_buf, error_buf_size, "unknown import");
  2192. return NULL;
  2193. }
  2194. module = (WASMModule *)module_reg;
  2195. export =
  2196. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2197. error_buf, error_buf_size);
  2198. if (!export) {
  2199. return NULL;
  2200. }
  2201. /* resolve tag type and tag */
  2202. if (export->index < module->import_tag_count) {
  2203. /* importing an imported tag from the submodule */
  2204. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2205. }
  2206. else {
  2207. /* importing an section tag from the submodule */
  2208. tag = module->tags[export->index - module->import_tag_count];
  2209. }
  2210. /* check function type */
  2211. if (!wasm_type_equal(expected_tag_type, tag->tag_type, module->types,
  2212. module->type_count)) {
  2213. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2214. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2215. return NULL;
  2216. }
  2217. if (linked_tag_index != NULL) {
  2218. *linked_tag_index = export->index;
  2219. }
  2220. return tag;
  2221. }
  2222. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2223. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2224. static bool
  2225. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2226. const WASMModule *parent_module,
  2227. const char *sub_module_name, const char *function_name,
  2228. WASMFunctionImport *function, char *error_buf,
  2229. uint32 error_buf_size)
  2230. {
  2231. const uint8 *p = *p_buf, *p_end = buf_end;
  2232. uint32 declare_type_index = 0;
  2233. WASMFuncType *declare_func_type = NULL;
  2234. WASMFunction *linked_func = NULL;
  2235. #if WASM_ENABLE_MULTI_MODULE != 0
  2236. WASMModule *sub_module = NULL;
  2237. bool is_built_in_module = false;
  2238. #endif
  2239. const char *linked_signature = NULL;
  2240. void *linked_attachment = NULL;
  2241. bool linked_call_conv_raw = false;
  2242. bool is_native_symbol = false;
  2243. read_leb_uint32(p, p_end, declare_type_index);
  2244. *p_buf = p;
  2245. if (declare_type_index >= parent_module->type_count) {
  2246. set_error_buf(error_buf, error_buf_size, "unknown type");
  2247. return false;
  2248. }
  2249. #if WASM_ENABLE_GC != 0
  2250. function->type_idx = declare_type_index;
  2251. #endif
  2252. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2253. declare_type_index = wasm_get_smallest_type_idx(
  2254. parent_module->types, parent_module->type_count, declare_type_index);
  2255. #endif
  2256. declare_func_type =
  2257. (WASMFuncType *)parent_module->types[declare_type_index];
  2258. /* lookup registered native symbols first */
  2259. linked_func = wasm_native_resolve_symbol(
  2260. sub_module_name, function_name, declare_func_type, &linked_signature,
  2261. &linked_attachment, &linked_call_conv_raw);
  2262. if (linked_func) {
  2263. is_native_symbol = true;
  2264. }
  2265. #if WASM_ENABLE_MULTI_MODULE != 0
  2266. else {
  2267. if (!(is_built_in_module =
  2268. wasm_runtime_is_built_in_module(sub_module_name))) {
  2269. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2270. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2271. error_buf_size);
  2272. }
  2273. if (is_built_in_module || sub_module)
  2274. linked_func = wasm_loader_resolve_function(
  2275. sub_module_name, function_name, declare_func_type, error_buf,
  2276. error_buf_size);
  2277. }
  2278. #endif
  2279. function->module_name = (char *)sub_module_name;
  2280. function->field_name = (char *)function_name;
  2281. function->func_type = declare_func_type;
  2282. /* func_ptr_linked is for native registered symbol */
  2283. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  2284. function->signature = linked_signature;
  2285. function->attachment = linked_attachment;
  2286. function->call_conv_raw = linked_call_conv_raw;
  2287. #if WASM_ENABLE_MULTI_MODULE != 0
  2288. function->import_module = is_native_symbol ? NULL : sub_module;
  2289. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  2290. #endif
  2291. return true;
  2292. fail:
  2293. return false;
  2294. }
  2295. static bool
  2296. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2297. uint32 error_buf_size)
  2298. {
  2299. if (max_size < init_size) {
  2300. set_error_buf(error_buf, error_buf_size,
  2301. "size minimum must not be greater than maximum");
  2302. return false;
  2303. }
  2304. return true;
  2305. }
  2306. static bool
  2307. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2308. WASMModule *parent_module, const char *sub_module_name,
  2309. const char *table_name, WASMTableImport *table,
  2310. char *error_buf, uint32 error_buf_size)
  2311. {
  2312. const uint8 *p = *p_buf, *p_end = buf_end;
  2313. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  2314. declare_init_size = 0, declare_max_size = 0;
  2315. #if WASM_ENABLE_MULTI_MODULE != 0
  2316. WASMModule *sub_module = NULL;
  2317. WASMTable *linked_table = NULL;
  2318. #endif
  2319. #if WASM_ENABLE_GC != 0
  2320. WASMRefType ref_type;
  2321. bool need_ref_type_map;
  2322. #endif
  2323. #if WASM_ENABLE_GC == 0
  2324. CHECK_BUF(p, p_end, 1);
  2325. /* 0x70 or 0x6F */
  2326. declare_elem_type = read_uint8(p);
  2327. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2328. #if WASM_ENABLE_REF_TYPES != 0
  2329. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2330. #endif
  2331. ) {
  2332. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2333. return false;
  2334. }
  2335. #else /* else of WASM_ENABLE_GC == 0 */
  2336. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2337. &need_ref_type_map, &ref_type, false, error_buf,
  2338. error_buf_size)) {
  2339. return false;
  2340. }
  2341. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2342. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2343. return false;
  2344. }
  2345. declare_elem_type = ref_type.ref_type;
  2346. if (need_ref_type_map) {
  2347. if (!(table->table_type.elem_ref_type =
  2348. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2349. error_buf, error_buf_size))) {
  2350. return false;
  2351. }
  2352. }
  2353. #if TRACE_WASM_LOADER != 0
  2354. os_printf("import table type: ");
  2355. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2356. os_printf("\n");
  2357. #endif
  2358. #endif /* end of WASM_ENABLE_GC == 0 */
  2359. read_leb_uint32(p, p_end, declare_max_size_flag);
  2360. if (declare_max_size_flag > 1) {
  2361. set_error_buf(error_buf, error_buf_size, "integer too large");
  2362. return false;
  2363. }
  2364. read_leb_uint32(p, p_end, declare_init_size);
  2365. if (declare_max_size_flag) {
  2366. read_leb_uint32(p, p_end, declare_max_size);
  2367. if (!check_table_max_size(declare_init_size, declare_max_size,
  2368. error_buf, error_buf_size))
  2369. return false;
  2370. }
  2371. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  2372. &declare_max_size);
  2373. *p_buf = p;
  2374. #if WASM_ENABLE_MULTI_MODULE != 0
  2375. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2376. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2377. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2378. error_buf_size);
  2379. if (sub_module) {
  2380. linked_table = wasm_loader_resolve_table(
  2381. sub_module_name, table_name, declare_init_size,
  2382. declare_max_size, error_buf, error_buf_size);
  2383. if (linked_table) {
  2384. /* reset with linked table limit */
  2385. declare_elem_type = linked_table->table_type.elem_type;
  2386. declare_init_size = linked_table->table_type.init_size;
  2387. declare_max_size = linked_table->table_type.max_size;
  2388. declare_max_size_flag = linked_table->table_type.flags;
  2389. table->import_table_linked = linked_table;
  2390. table->import_module = sub_module;
  2391. }
  2392. }
  2393. }
  2394. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2395. /* (table (export "table") 10 20 funcref) */
  2396. /* we need this section working in wamrc */
  2397. if (!strcmp("spectest", sub_module_name)) {
  2398. const uint32 spectest_table_init_size = 10;
  2399. const uint32 spectest_table_max_size = 20;
  2400. if (strcmp("table", table_name)) {
  2401. set_error_buf(error_buf, error_buf_size,
  2402. "incompatible import type or unknown import");
  2403. return false;
  2404. }
  2405. if (declare_init_size > spectest_table_init_size
  2406. || declare_max_size < spectest_table_max_size) {
  2407. set_error_buf(error_buf, error_buf_size,
  2408. "incompatible import type");
  2409. return false;
  2410. }
  2411. declare_init_size = spectest_table_init_size;
  2412. declare_max_size = spectest_table_max_size;
  2413. }
  2414. /* now we believe all declaration are ok */
  2415. table->table_type.elem_type = declare_elem_type;
  2416. table->table_type.init_size = declare_init_size;
  2417. table->table_type.flags = declare_max_size_flag;
  2418. table->table_type.max_size = declare_max_size;
  2419. #if WASM_ENABLE_WAMR_COMPILER != 0
  2420. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2421. parent_module->is_ref_types_used = true;
  2422. #endif
  2423. (void)parent_module;
  2424. return true;
  2425. fail:
  2426. return false;
  2427. }
  2428. static bool
  2429. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2430. uint32 error_buf_size)
  2431. {
  2432. uint32 default_max_size =
  2433. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2434. if (!is_memory64 && init_size > default_max_size) {
  2435. set_error_buf(error_buf, error_buf_size,
  2436. "memory size must be at most 65536 pages (4GiB)");
  2437. return false;
  2438. }
  2439. #if WASM_ENABLE_MEMORY64 != 0
  2440. else if (is_memory64 && init_size > default_max_size) {
  2441. set_error_buf(
  2442. error_buf, error_buf_size,
  2443. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2444. return false;
  2445. }
  2446. #endif
  2447. return true;
  2448. }
  2449. static bool
  2450. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2451. char *error_buf, uint32 error_buf_size)
  2452. {
  2453. uint32 default_max_size =
  2454. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2455. if (max_size < init_size) {
  2456. set_error_buf(error_buf, error_buf_size,
  2457. "size minimum must not be greater than maximum");
  2458. return false;
  2459. }
  2460. if (!is_memory64 && max_size > default_max_size) {
  2461. set_error_buf(error_buf, error_buf_size,
  2462. "memory size must be at most 65536 pages (4GiB)");
  2463. return false;
  2464. }
  2465. #if WASM_ENABLE_MEMORY64 != 0
  2466. else if (is_memory64 && max_size > default_max_size) {
  2467. set_error_buf(
  2468. error_buf, error_buf_size,
  2469. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2470. return false;
  2471. }
  2472. #endif
  2473. return true;
  2474. }
  2475. static bool
  2476. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2477. WASMModule *parent_module, const char *sub_module_name,
  2478. const char *memory_name, WASMMemoryImport *memory,
  2479. char *error_buf, uint32 error_buf_size)
  2480. {
  2481. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2482. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2483. uint32 pool_size = wasm_runtime_memory_pool_size();
  2484. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2485. / DEFAULT_NUM_BYTES_PER_PAGE;
  2486. #else
  2487. uint32 max_page_count;
  2488. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2489. uint32 mem_flag = 0;
  2490. bool is_memory64 = false;
  2491. uint32 declare_init_page_count = 0;
  2492. uint32 declare_max_page_count = 0;
  2493. #if WASM_ENABLE_MULTI_MODULE != 0
  2494. WASMModule *sub_module = NULL;
  2495. WASMMemory *linked_memory = NULL;
  2496. #endif
  2497. p_org = p;
  2498. read_leb_uint32(p, p_end, mem_flag);
  2499. is_memory64 = mem_flag & MEMORY64_FLAG;
  2500. if (p - p_org > 1) {
  2501. LOG_VERBOSE("integer representation too long");
  2502. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2503. return false;
  2504. }
  2505. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2506. return false;
  2507. }
  2508. read_leb_uint32(p, p_end, declare_init_page_count);
  2509. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2510. error_buf_size)) {
  2511. return false;
  2512. }
  2513. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2514. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2515. #endif
  2516. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2517. read_leb_uint32(p, p_end, declare_max_page_count);
  2518. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2519. declare_max_page_count, error_buf,
  2520. error_buf_size)) {
  2521. return false;
  2522. }
  2523. if (declare_max_page_count > max_page_count) {
  2524. declare_max_page_count = max_page_count;
  2525. }
  2526. }
  2527. else {
  2528. /* Limit the maximum memory size to max_page_count */
  2529. declare_max_page_count = max_page_count;
  2530. }
  2531. #if WASM_ENABLE_MULTI_MODULE != 0
  2532. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2533. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2534. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2535. error_buf_size);
  2536. if (sub_module) {
  2537. linked_memory = wasm_loader_resolve_memory(
  2538. sub_module_name, memory_name, declare_init_page_count,
  2539. declare_max_page_count, error_buf, error_buf_size);
  2540. if (linked_memory) {
  2541. /**
  2542. * reset with linked memory limit
  2543. */
  2544. memory->import_module = sub_module;
  2545. memory->import_memory_linked = linked_memory;
  2546. declare_init_page_count = linked_memory->init_page_count;
  2547. declare_max_page_count = linked_memory->max_page_count;
  2548. }
  2549. }
  2550. }
  2551. #endif
  2552. /* (memory (export "memory") 1 2) */
  2553. if (!strcmp("spectest", sub_module_name)) {
  2554. uint32 spectest_memory_init_page = 1;
  2555. uint32 spectest_memory_max_page = 2;
  2556. if (strcmp("memory", memory_name)) {
  2557. set_error_buf(error_buf, error_buf_size,
  2558. "incompatible import type or unknown import");
  2559. return false;
  2560. }
  2561. if (declare_init_page_count > spectest_memory_init_page
  2562. || declare_max_page_count < spectest_memory_max_page) {
  2563. set_error_buf(error_buf, error_buf_size,
  2564. "incompatible import type");
  2565. return false;
  2566. }
  2567. declare_init_page_count = spectest_memory_init_page;
  2568. declare_max_page_count = spectest_memory_max_page;
  2569. }
  2570. #if WASM_ENABLE_WASI_TEST != 0
  2571. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2572. else if (!strcmp("foo", sub_module_name)) {
  2573. uint32 spectest_memory_init_page = 1;
  2574. uint32 spectest_memory_max_page = 1;
  2575. if (strcmp("bar", memory_name)) {
  2576. set_error_buf(error_buf, error_buf_size,
  2577. "incompatible import type or unknown import");
  2578. return false;
  2579. }
  2580. if (declare_init_page_count > spectest_memory_init_page
  2581. || declare_max_page_count < spectest_memory_max_page) {
  2582. set_error_buf(error_buf, error_buf_size,
  2583. "incompatible import type");
  2584. return false;
  2585. }
  2586. declare_init_page_count = spectest_memory_init_page;
  2587. declare_max_page_count = spectest_memory_max_page;
  2588. }
  2589. #endif
  2590. /* now we believe all declaration are ok */
  2591. memory->mem_type.flags = mem_flag;
  2592. memory->mem_type.init_page_count = declare_init_page_count;
  2593. memory->mem_type.max_page_count = declare_max_page_count;
  2594. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2595. *p_buf = p;
  2596. (void)parent_module;
  2597. return true;
  2598. fail:
  2599. return false;
  2600. }
  2601. #if WASM_ENABLE_TAGS != 0
  2602. static bool
  2603. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2604. const WASMModule *parent_module, /* this module ! */
  2605. const char *sub_module_name, const char *tag_name,
  2606. WASMTagImport *tag, /* structure to fill */
  2607. char *error_buf, uint32 error_buf_size)
  2608. {
  2609. /* attribute and type of the import statement */
  2610. uint8 declare_tag_attribute;
  2611. uint32 declare_type_index;
  2612. const uint8 *p = *p_buf, *p_end = buf_end;
  2613. #if WASM_ENABLE_MULTI_MODULE != 0
  2614. WASMModule *sub_module = NULL;
  2615. #endif
  2616. /* get the one byte attribute */
  2617. CHECK_BUF(p, p_end, 1);
  2618. declare_tag_attribute = read_uint8(p);
  2619. if (declare_tag_attribute != 0) {
  2620. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2621. goto fail;
  2622. }
  2623. /* get type */
  2624. read_leb_uint32(p, p_end, declare_type_index);
  2625. /* compare against module->types */
  2626. if (declare_type_index >= parent_module->type_count) {
  2627. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2628. goto fail;
  2629. }
  2630. WASMFuncType *declare_tag_type =
  2631. (WASMFuncType *)parent_module->types[declare_type_index];
  2632. /* check, that the type of the declared tag returns void */
  2633. if (declare_tag_type->result_count != 0) {
  2634. set_error_buf(error_buf, error_buf_size,
  2635. "tag type signature does not return void");
  2636. goto fail;
  2637. }
  2638. #if WASM_ENABLE_MULTI_MODULE != 0
  2639. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2640. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2641. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2642. error_buf_size);
  2643. if (sub_module) {
  2644. /* wasm_loader_resolve_tag checks, that the imported tag
  2645. * and the declared tag have the same type
  2646. */
  2647. uint32 linked_tag_index = 0;
  2648. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2649. sub_module_name, tag_name, declare_tag_type,
  2650. &linked_tag_index /* out */, error_buf, error_buf_size);
  2651. if (linked_tag) {
  2652. tag->import_module = sub_module;
  2653. tag->import_tag_linked = linked_tag;
  2654. tag->import_tag_index_linked = linked_tag_index;
  2655. }
  2656. }
  2657. }
  2658. #endif
  2659. /* store to module tag declarations */
  2660. tag->attribute = declare_tag_attribute;
  2661. tag->type = declare_type_index;
  2662. tag->module_name = (char *)sub_module_name;
  2663. tag->field_name = (char *)tag_name;
  2664. tag->tag_type = declare_tag_type;
  2665. *p_buf = p;
  2666. (void)parent_module;
  2667. LOG_VERBOSE("Load tag import success\n");
  2668. return true;
  2669. fail:
  2670. return false;
  2671. }
  2672. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2673. static bool
  2674. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2675. WASMModule *parent_module, char *sub_module_name,
  2676. char *global_name, WASMGlobalImport *global, char *error_buf,
  2677. uint32 error_buf_size)
  2678. {
  2679. const uint8 *p = *p_buf, *p_end = buf_end;
  2680. uint8 declare_type = 0;
  2681. uint8 declare_mutable = 0;
  2682. #if WASM_ENABLE_MULTI_MODULE != 0
  2683. WASMModule *sub_module = NULL;
  2684. WASMGlobal *linked_global = NULL;
  2685. #endif
  2686. #if WASM_ENABLE_GC != 0
  2687. WASMRefType ref_type;
  2688. bool need_ref_type_map;
  2689. #endif
  2690. bool ret = false;
  2691. #if WASM_ENABLE_GC == 0
  2692. CHECK_BUF(p, p_end, 2);
  2693. /* global type */
  2694. declare_type = read_uint8(p);
  2695. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2696. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2697. return false;
  2698. }
  2699. declare_mutable = read_uint8(p);
  2700. #else
  2701. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2702. &need_ref_type_map, &ref_type, false, error_buf,
  2703. error_buf_size)) {
  2704. return false;
  2705. }
  2706. declare_type = ref_type.ref_type;
  2707. if (need_ref_type_map) {
  2708. if (!(global->ref_type =
  2709. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2710. error_buf, error_buf_size))) {
  2711. return false;
  2712. }
  2713. }
  2714. #if TRACE_WASM_LOADER != 0
  2715. os_printf("import global type: ");
  2716. wasm_dump_value_type(declare_type, global->ref_type);
  2717. os_printf("\n");
  2718. #endif
  2719. CHECK_BUF(p, p_end, 1);
  2720. declare_mutable = read_uint8(p);
  2721. #endif /* end of WASM_ENABLE_GC == 0 */
  2722. *p_buf = p;
  2723. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2724. return false;
  2725. }
  2726. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2727. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2728. global);
  2729. if (ret) {
  2730. if (global->type.val_type != declare_type
  2731. || global->type.is_mutable != declare_mutable) {
  2732. set_error_buf(error_buf, error_buf_size,
  2733. "incompatible import type");
  2734. return false;
  2735. }
  2736. global->is_linked = true;
  2737. }
  2738. #endif
  2739. #if WASM_ENABLE_MULTI_MODULE != 0
  2740. if (!global->is_linked
  2741. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2742. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2743. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2744. error_buf_size);
  2745. if (sub_module) {
  2746. /* check sub modules */
  2747. linked_global = wasm_loader_resolve_global(
  2748. sub_module_name, global_name, declare_type, declare_mutable,
  2749. error_buf, error_buf_size);
  2750. if (linked_global) {
  2751. global->import_module = sub_module;
  2752. global->import_global_linked = linked_global;
  2753. global->is_linked = true;
  2754. }
  2755. }
  2756. }
  2757. #endif
  2758. global->module_name = sub_module_name;
  2759. global->field_name = global_name;
  2760. global->type.val_type = declare_type;
  2761. global->type.is_mutable = (declare_mutable == 1);
  2762. #if WASM_ENABLE_WAMR_COMPILER != 0
  2763. if (global->type.val_type == VALUE_TYPE_V128)
  2764. parent_module->is_simd_used = true;
  2765. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2766. parent_module->is_ref_types_used = true;
  2767. #endif
  2768. (void)parent_module;
  2769. (void)ret;
  2770. return true;
  2771. fail:
  2772. return false;
  2773. }
  2774. static bool
  2775. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2776. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2777. {
  2778. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2779. #if WASM_ENABLE_GC != 0
  2780. WASMRefType ref_type;
  2781. bool need_ref_type_map;
  2782. #endif
  2783. #if WASM_ENABLE_GC == 0
  2784. CHECK_BUF(p, p_end, 1);
  2785. /* 0x70 or 0x6F */
  2786. table->table_type.elem_type = read_uint8(p);
  2787. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  2788. #if WASM_ENABLE_REF_TYPES != 0
  2789. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  2790. #endif
  2791. ) {
  2792. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2793. return false;
  2794. }
  2795. #else /* else of WASM_ENABLE_GC == 0 */
  2796. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2797. &need_ref_type_map, &ref_type, false, error_buf,
  2798. error_buf_size)) {
  2799. return false;
  2800. }
  2801. table->table_type.elem_type = ref_type.ref_type;
  2802. if (need_ref_type_map) {
  2803. if (!(table->table_type.elem_ref_type =
  2804. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2805. error_buf_size))) {
  2806. return false;
  2807. }
  2808. }
  2809. #if TRACE_WASM_LOADER != 0
  2810. os_printf("table type: ");
  2811. wasm_dump_value_type(table->table_type.elem_type,
  2812. table->table_type.elem_ref_type);
  2813. os_printf("\n");
  2814. #endif
  2815. #endif /* end of WASM_ENABLE_GC == 0 */
  2816. p_org = p;
  2817. read_leb_uint32(p, p_end, table->table_type.flags);
  2818. #if WASM_ENABLE_SHARED_MEMORY == 0
  2819. if (p - p_org > 1) {
  2820. set_error_buf(error_buf, error_buf_size,
  2821. "integer representation too long");
  2822. return false;
  2823. }
  2824. if (table->table_type.flags > 1) {
  2825. set_error_buf(error_buf, error_buf_size, "integer too large");
  2826. return false;
  2827. }
  2828. #else
  2829. if (p - p_org > 1) {
  2830. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2831. return false;
  2832. }
  2833. if (table->table_type.flags == 2) {
  2834. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2835. return false;
  2836. }
  2837. if (table->table_type.flags > 1) {
  2838. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2839. return false;
  2840. }
  2841. #endif
  2842. read_leb_uint32(p, p_end, table->table_type.init_size);
  2843. if (table->table_type.flags) {
  2844. read_leb_uint32(p, p_end, table->table_type.max_size);
  2845. if (!check_table_max_size(table->table_type.init_size,
  2846. table->table_type.max_size, error_buf,
  2847. error_buf_size))
  2848. return false;
  2849. }
  2850. adjust_table_max_size(table->table_type.init_size, table->table_type.flags,
  2851. &table->table_type.max_size);
  2852. #if WASM_ENABLE_WAMR_COMPILER != 0
  2853. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2854. module->is_ref_types_used = true;
  2855. #endif
  2856. *p_buf = p;
  2857. return true;
  2858. fail:
  2859. return false;
  2860. }
  2861. static bool
  2862. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2863. char *error_buf, uint32 error_buf_size)
  2864. {
  2865. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2866. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2867. uint32 pool_size = wasm_runtime_memory_pool_size();
  2868. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2869. / DEFAULT_NUM_BYTES_PER_PAGE;
  2870. #else
  2871. uint32 max_page_count;
  2872. #endif
  2873. bool is_memory64 = false;
  2874. p_org = p;
  2875. read_leb_uint32(p, p_end, memory->flags);
  2876. is_memory64 = memory->flags & MEMORY64_FLAG;
  2877. if (p - p_org > 1) {
  2878. LOG_VERBOSE("integer representation too long");
  2879. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2880. return false;
  2881. }
  2882. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2883. false)) {
  2884. return false;
  2885. }
  2886. read_leb_uint32(p, p_end, memory->init_page_count);
  2887. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2888. error_buf_size))
  2889. return false;
  2890. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2891. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2892. #endif
  2893. if (memory->flags & 1) {
  2894. read_leb_uint32(p, p_end, memory->max_page_count);
  2895. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2896. memory->max_page_count, error_buf,
  2897. error_buf_size))
  2898. return false;
  2899. if (memory->max_page_count > max_page_count)
  2900. memory->max_page_count = max_page_count;
  2901. }
  2902. else {
  2903. /* Limit the maximum memory size to max_page_count */
  2904. memory->max_page_count = max_page_count;
  2905. }
  2906. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2907. *p_buf = p;
  2908. return true;
  2909. fail:
  2910. return false;
  2911. }
  2912. static bool
  2913. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2914. bool is_load_from_file_buf, char *error_buf,
  2915. uint32 error_buf_size)
  2916. {
  2917. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2918. uint32 import_count, name_len, type_index, i, u32, flags;
  2919. uint64 total_size;
  2920. WASMImport *import;
  2921. WASMImport *import_functions = NULL, *import_tables = NULL;
  2922. WASMImport *import_memories = NULL, *import_globals = NULL;
  2923. #if WASM_ENABLE_TAGS != 0
  2924. WASMImport *import_tags = NULL;
  2925. #endif
  2926. char *sub_module_name, *field_name;
  2927. uint8 u8, kind, global_type;
  2928. read_leb_uint32(p, p_end, import_count);
  2929. if (import_count) {
  2930. module->import_count = import_count;
  2931. total_size = sizeof(WASMImport) * (uint64)import_count;
  2932. if (!(module->imports =
  2933. loader_malloc(total_size, error_buf, error_buf_size))) {
  2934. return false;
  2935. }
  2936. p_old = p;
  2937. /* Scan firstly to get import count of each type */
  2938. for (i = 0; i < import_count; i++) {
  2939. /* module name */
  2940. read_leb_uint32(p, p_end, name_len);
  2941. CHECK_BUF(p, p_end, name_len);
  2942. p += name_len;
  2943. /* field name */
  2944. read_leb_uint32(p, p_end, name_len);
  2945. CHECK_BUF(p, p_end, name_len);
  2946. p += name_len;
  2947. CHECK_BUF(p, p_end, 1);
  2948. /* 0x00/0x01/0x02/0x03/0x04 */
  2949. kind = read_uint8(p);
  2950. switch (kind) {
  2951. case IMPORT_KIND_FUNC: /* import function */
  2952. read_leb_uint32(p, p_end, type_index);
  2953. module->import_function_count++;
  2954. break;
  2955. case IMPORT_KIND_TABLE: /* import table */
  2956. CHECK_BUF(p, p_end, 1);
  2957. /* 0x70 */
  2958. u8 = read_uint8(p);
  2959. read_leb_uint32(p, p_end, flags);
  2960. read_leb_uint32(p, p_end, u32);
  2961. if (flags & 1)
  2962. read_leb_uint32(p, p_end, u32);
  2963. module->import_table_count++;
  2964. if (module->import_table_count > 1) {
  2965. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2966. set_error_buf(error_buf, error_buf_size,
  2967. "multiple tables");
  2968. return false;
  2969. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2970. module->is_ref_types_used = true;
  2971. #endif
  2972. }
  2973. break;
  2974. case IMPORT_KIND_MEMORY: /* import memory */
  2975. read_leb_uint32(p, p_end, flags);
  2976. read_leb_uint32(p, p_end, u32);
  2977. if (flags & 1)
  2978. read_leb_uint32(p, p_end, u32);
  2979. module->import_memory_count++;
  2980. #if WASM_ENABLE_MULTI_MEMORY == 0
  2981. if (module->import_memory_count > 1) {
  2982. set_error_buf(error_buf, error_buf_size,
  2983. "multiple memories");
  2984. return false;
  2985. }
  2986. #endif
  2987. break;
  2988. #if WASM_ENABLE_TAGS != 0
  2989. case IMPORT_KIND_TAG: /* import tags */
  2990. /* it only counts the number of tags to import */
  2991. module->import_tag_count++;
  2992. CHECK_BUF(p, p_end, 1);
  2993. u8 = read_uint8(p);
  2994. read_leb_uint32(p, p_end, type_index);
  2995. break;
  2996. #endif
  2997. case IMPORT_KIND_GLOBAL: /* import global */
  2998. #if WASM_ENABLE_GC != 0
  2999. /* valtype */
  3000. CHECK_BUF(p, p_end, 1);
  3001. global_type = read_uint8(p);
  3002. if (wasm_is_type_multi_byte_type(global_type)) {
  3003. int32 heap_type;
  3004. read_leb_int32(p, p_end, heap_type);
  3005. (void)heap_type;
  3006. }
  3007. /* mutability */
  3008. CHECK_BUF(p, p_end, 1);
  3009. p += 1;
  3010. #else
  3011. CHECK_BUF(p, p_end, 2);
  3012. p += 2;
  3013. #endif
  3014. (void)global_type;
  3015. module->import_global_count++;
  3016. break;
  3017. default:
  3018. set_error_buf(error_buf, error_buf_size,
  3019. "invalid import kind");
  3020. return false;
  3021. }
  3022. }
  3023. if (module->import_function_count)
  3024. import_functions = module->import_functions = module->imports;
  3025. if (module->import_table_count)
  3026. import_tables = module->import_tables =
  3027. module->imports + module->import_function_count;
  3028. if (module->import_memory_count)
  3029. import_memories = module->import_memories =
  3030. module->imports + module->import_function_count
  3031. + module->import_table_count;
  3032. #if WASM_ENABLE_TAGS != 0
  3033. if (module->import_tag_count)
  3034. import_tags = module->import_tags =
  3035. module->imports + module->import_function_count
  3036. + module->import_table_count + module->import_memory_count;
  3037. if (module->import_global_count)
  3038. import_globals = module->import_globals =
  3039. module->imports + module->import_function_count
  3040. + module->import_table_count + module->import_memory_count
  3041. + module->import_tag_count;
  3042. #else
  3043. if (module->import_global_count)
  3044. import_globals = module->import_globals =
  3045. module->imports + module->import_function_count
  3046. + module->import_table_count + module->import_memory_count;
  3047. #endif
  3048. p = p_old;
  3049. /* Scan again to resolve the data */
  3050. for (i = 0; i < import_count; i++) {
  3051. /* load module name */
  3052. read_leb_uint32(p, p_end, name_len);
  3053. CHECK_BUF(p, p_end, name_len);
  3054. if (!(sub_module_name = wasm_const_str_list_insert(
  3055. p, name_len, module, is_load_from_file_buf, error_buf,
  3056. error_buf_size))) {
  3057. return false;
  3058. }
  3059. p += name_len;
  3060. /* load field name */
  3061. read_leb_uint32(p, p_end, name_len);
  3062. CHECK_BUF(p, p_end, name_len);
  3063. if (!(field_name = wasm_const_str_list_insert(
  3064. p, name_len, module, is_load_from_file_buf, error_buf,
  3065. error_buf_size))) {
  3066. return false;
  3067. }
  3068. p += name_len;
  3069. CHECK_BUF(p, p_end, 1);
  3070. /* 0x00/0x01/0x02/0x03/0x4 */
  3071. kind = read_uint8(p);
  3072. switch (kind) {
  3073. case IMPORT_KIND_FUNC: /* import function */
  3074. bh_assert(import_functions);
  3075. import = import_functions++;
  3076. if (!load_function_import(
  3077. &p, p_end, module, sub_module_name, field_name,
  3078. &import->u.function, error_buf, error_buf_size)) {
  3079. return false;
  3080. }
  3081. break;
  3082. case IMPORT_KIND_TABLE: /* import table */
  3083. bh_assert(import_tables);
  3084. import = import_tables++;
  3085. if (!load_table_import(&p, p_end, module, sub_module_name,
  3086. field_name, &import->u.table,
  3087. error_buf, error_buf_size)) {
  3088. LOG_DEBUG("can not import such a table (%s,%s)",
  3089. sub_module_name, field_name);
  3090. return false;
  3091. }
  3092. break;
  3093. case IMPORT_KIND_MEMORY: /* import memory */
  3094. bh_assert(import_memories);
  3095. import = import_memories++;
  3096. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3097. field_name, &import->u.memory,
  3098. error_buf, error_buf_size)) {
  3099. return false;
  3100. }
  3101. break;
  3102. #if WASM_ENABLE_TAGS != 0
  3103. case IMPORT_KIND_TAG:
  3104. bh_assert(import_tags);
  3105. import = import_tags++;
  3106. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3107. field_name, &import->u.tag, error_buf,
  3108. error_buf_size)) {
  3109. return false;
  3110. }
  3111. break;
  3112. #endif
  3113. case IMPORT_KIND_GLOBAL: /* import global */
  3114. bh_assert(import_globals);
  3115. import = import_globals++;
  3116. if (!load_global_import(&p, p_end, module, sub_module_name,
  3117. field_name, &import->u.global,
  3118. error_buf, error_buf_size)) {
  3119. return false;
  3120. }
  3121. break;
  3122. default:
  3123. set_error_buf(error_buf, error_buf_size,
  3124. "invalid import kind");
  3125. return false;
  3126. }
  3127. import->kind = kind;
  3128. import->u.names.module_name = sub_module_name;
  3129. import->u.names.field_name = field_name;
  3130. }
  3131. #if WASM_ENABLE_LIBC_WASI != 0
  3132. import = module->import_functions;
  3133. for (i = 0; i < module->import_function_count; i++, import++) {
  3134. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3135. || !strcmp(import->u.names.module_name,
  3136. "wasi_snapshot_preview1")) {
  3137. module->import_wasi_api = true;
  3138. break;
  3139. }
  3140. }
  3141. #endif
  3142. }
  3143. if (p != p_end) {
  3144. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3145. return false;
  3146. }
  3147. LOG_VERBOSE("Load import section success.\n");
  3148. (void)u8;
  3149. (void)u32;
  3150. (void)type_index;
  3151. return true;
  3152. fail:
  3153. return false;
  3154. }
  3155. static bool
  3156. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3157. uint32 error_buf_size)
  3158. {
  3159. WASMFuncType *param_type = func->func_type;
  3160. uint32 param_count = param_type->param_count;
  3161. uint8 *param_types = param_type->types;
  3162. uint32 local_count = func->local_count;
  3163. uint8 *local_types = func->local_types;
  3164. uint32 i, local_offset = 0;
  3165. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3166. /*
  3167. * Only allocate memory when total_size is not 0,
  3168. * or the return value of malloc(0) might be NULL on some platforms,
  3169. * which causes wasm loader return false.
  3170. */
  3171. if (total_size > 0
  3172. && !(func->local_offsets =
  3173. loader_malloc(total_size, error_buf, error_buf_size))) {
  3174. return false;
  3175. }
  3176. for (i = 0; i < param_count; i++) {
  3177. func->local_offsets[i] = (uint16)local_offset;
  3178. local_offset += wasm_value_type_cell_num(param_types[i]);
  3179. }
  3180. for (i = 0; i < local_count; i++) {
  3181. func->local_offsets[param_count + i] = (uint16)local_offset;
  3182. local_offset += wasm_value_type_cell_num(local_types[i]);
  3183. }
  3184. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3185. return true;
  3186. }
  3187. static bool
  3188. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3189. const uint8 *buf_code, const uint8 *buf_code_end,
  3190. WASMModule *module, char *error_buf,
  3191. uint32 error_buf_size)
  3192. {
  3193. const uint8 *p = buf, *p_end = buf_end;
  3194. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3195. uint32 func_count;
  3196. uint64 total_size;
  3197. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3198. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3199. uint8 type;
  3200. WASMFunction *func;
  3201. #if WASM_ENABLE_GC != 0
  3202. bool need_ref_type_map;
  3203. WASMRefType ref_type;
  3204. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3205. #endif
  3206. read_leb_uint32(p, p_end, func_count);
  3207. if (buf_code)
  3208. read_leb_uint32(p_code, buf_code_end, code_count);
  3209. if (func_count != code_count) {
  3210. set_error_buf(error_buf, error_buf_size,
  3211. "function and code section have inconsistent lengths or "
  3212. "unexpected end");
  3213. return false;
  3214. }
  3215. if (is_indices_overflow(module->import_function_count, func_count,
  3216. error_buf, error_buf_size))
  3217. return false;
  3218. if (func_count) {
  3219. module->function_count = func_count;
  3220. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3221. if (!(module->functions =
  3222. loader_malloc(total_size, error_buf, error_buf_size))) {
  3223. return false;
  3224. }
  3225. for (i = 0; i < func_count; i++) {
  3226. /* Resolve function type */
  3227. read_leb_uint32(p, p_end, type_index);
  3228. if (type_index >= module->type_count) {
  3229. set_error_buf(error_buf, error_buf_size, "unknown type");
  3230. return false;
  3231. }
  3232. #if WASM_ENABLE_GC != 0
  3233. type_index_org = type_index;
  3234. #endif
  3235. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3236. && WASM_ENABLE_GC == 0
  3237. type_index = wasm_get_smallest_type_idx(
  3238. module->types, module->type_count, type_index);
  3239. #endif
  3240. read_leb_uint32(p_code, buf_code_end, code_size);
  3241. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3242. set_error_buf(error_buf, error_buf_size,
  3243. "invalid function code size");
  3244. return false;
  3245. }
  3246. /* Resolve local set count */
  3247. p_code_end = p_code + code_size;
  3248. local_count = 0;
  3249. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3250. p_code_save = p_code;
  3251. #if WASM_ENABLE_GC != 0
  3252. ref_type_map_count = 0;
  3253. #endif
  3254. /* Calculate total local count */
  3255. for (j = 0; j < local_set_count; j++) {
  3256. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3257. if (sub_local_count > UINT32_MAX - local_count) {
  3258. set_error_buf(error_buf, error_buf_size, "too many locals");
  3259. return false;
  3260. }
  3261. #if WASM_ENABLE_GC == 0
  3262. CHECK_BUF(p_code, buf_code_end, 1);
  3263. /* 0x7F/0x7E/0x7D/0x7C */
  3264. type = read_uint8(p_code);
  3265. local_count += sub_local_count;
  3266. #if WASM_ENABLE_WAMR_COMPILER != 0
  3267. /* If any value's type is v128, mark the module as SIMD used */
  3268. if (type == VALUE_TYPE_V128)
  3269. module->is_simd_used = true;
  3270. #endif
  3271. #else
  3272. if (!resolve_value_type(&p_code, buf_code_end, module,
  3273. module->type_count, &need_ref_type_map,
  3274. &ref_type, false, error_buf,
  3275. error_buf_size)) {
  3276. return false;
  3277. }
  3278. local_count += sub_local_count;
  3279. if (need_ref_type_map)
  3280. ref_type_map_count += sub_local_count;
  3281. #endif
  3282. }
  3283. /* Alloc memory, layout: function structure + local types */
  3284. code_size = (uint32)(p_code_end - p_code);
  3285. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3286. if (!(func = module->functions[i] =
  3287. loader_malloc(total_size, error_buf, error_buf_size))) {
  3288. return false;
  3289. }
  3290. #if WASM_ENABLE_GC != 0
  3291. if (ref_type_map_count > 0) {
  3292. total_size =
  3293. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3294. if (!(func->local_ref_type_maps = loader_malloc(
  3295. total_size, error_buf, error_buf_size))) {
  3296. return false;
  3297. }
  3298. func->local_ref_type_map_count = ref_type_map_count;
  3299. }
  3300. #endif
  3301. /* Set function type, local count, code size and code body */
  3302. func->func_type = (WASMFuncType *)module->types[type_index];
  3303. func->local_count = local_count;
  3304. if (local_count > 0)
  3305. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3306. func->code_size = code_size;
  3307. /*
  3308. * we shall make a copy of code body [p_code, p_code + code_size]
  3309. * when we are worrying about inappropriate releasing behaviour.
  3310. * all code bodies are actually in a buffer which user allocates in
  3311. * his embedding environment and we don't have power on them.
  3312. * it will be like:
  3313. * code_body_cp = malloc(code_size);
  3314. * memcpy(code_body_cp, p_code, code_size);
  3315. * func->code = code_body_cp;
  3316. */
  3317. func->code = (uint8 *)p_code;
  3318. #if WASM_ENABLE_GC != 0
  3319. func->type_idx = type_index_org;
  3320. #endif
  3321. #if WASM_ENABLE_GC != 0
  3322. t = 0;
  3323. #endif
  3324. /* Load each local type */
  3325. p_code = p_code_save;
  3326. local_type_index = 0;
  3327. for (j = 0; j < local_set_count; j++) {
  3328. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3329. /* Note: sub_local_count is allowed to be 0 */
  3330. if (local_type_index > UINT32_MAX - sub_local_count
  3331. || local_type_index + sub_local_count > local_count) {
  3332. set_error_buf(error_buf, error_buf_size,
  3333. "invalid local count");
  3334. return false;
  3335. }
  3336. #if WASM_ENABLE_GC == 0
  3337. CHECK_BUF(p_code, buf_code_end, 1);
  3338. /* 0x7F/0x7E/0x7D/0x7C */
  3339. type = read_uint8(p_code);
  3340. if (!is_valid_value_type_for_interpreter(type)) {
  3341. if (type == VALUE_TYPE_V128)
  3342. set_error_buf(error_buf, error_buf_size,
  3343. "v128 value type requires simd feature");
  3344. else if (type == VALUE_TYPE_FUNCREF
  3345. || type == VALUE_TYPE_EXTERNREF)
  3346. set_error_buf(error_buf, error_buf_size,
  3347. "ref value type requires "
  3348. "reference types feature");
  3349. else
  3350. set_error_buf_v(error_buf, error_buf_size,
  3351. "invalid local type 0x%02X", type);
  3352. return false;
  3353. }
  3354. #else
  3355. if (!resolve_value_type(&p_code, buf_code_end, module,
  3356. module->type_count, &need_ref_type_map,
  3357. &ref_type, false, error_buf,
  3358. error_buf_size)) {
  3359. return false;
  3360. }
  3361. if (need_ref_type_map) {
  3362. WASMRefType *ref_type_tmp;
  3363. if (!(ref_type_tmp = reftype_set_insert(
  3364. module->ref_type_set, &ref_type, error_buf,
  3365. error_buf_size))) {
  3366. return false;
  3367. }
  3368. for (k = 0; k < sub_local_count; k++) {
  3369. func->local_ref_type_maps[t + k].ref_type =
  3370. ref_type_tmp;
  3371. func->local_ref_type_maps[t + k].index =
  3372. local_type_index + k;
  3373. }
  3374. t += sub_local_count;
  3375. }
  3376. type = ref_type.ref_type;
  3377. #endif
  3378. for (k = 0; k < sub_local_count; k++) {
  3379. func->local_types[local_type_index++] = type;
  3380. }
  3381. #if WASM_ENABLE_WAMR_COMPILER != 0
  3382. if (type == VALUE_TYPE_V128)
  3383. module->is_simd_used = true;
  3384. else if (type == VALUE_TYPE_FUNCREF
  3385. || type == VALUE_TYPE_EXTERNREF)
  3386. module->is_ref_types_used = true;
  3387. #endif
  3388. }
  3389. bh_assert(local_type_index == func->local_count);
  3390. #if WASM_ENABLE_GC != 0
  3391. bh_assert(t == func->local_ref_type_map_count);
  3392. #if TRACE_WASM_LOADER != 0
  3393. os_printf("func %u, local types: [", i);
  3394. k = 0;
  3395. for (j = 0; j < func->local_count; j++) {
  3396. WASMRefType *ref_type_tmp = NULL;
  3397. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3398. bh_assert(j == func->local_ref_type_maps[k].index);
  3399. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3400. }
  3401. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3402. if (j < func->local_count - 1)
  3403. os_printf(" ");
  3404. }
  3405. os_printf("]\n");
  3406. #endif
  3407. #endif
  3408. func->param_cell_num = func->func_type->param_cell_num;
  3409. func->ret_cell_num = func->func_type->ret_cell_num;
  3410. local_cell_num =
  3411. wasm_get_cell_num(func->local_types, func->local_count);
  3412. if (local_cell_num > UINT16_MAX) {
  3413. set_error_buf(error_buf, error_buf_size,
  3414. "local count too large");
  3415. return false;
  3416. }
  3417. func->local_cell_num = (uint16)local_cell_num;
  3418. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3419. return false;
  3420. p_code = p_code_end;
  3421. }
  3422. }
  3423. if (p != p_end) {
  3424. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3425. return false;
  3426. }
  3427. LOG_VERBOSE("Load function section success.\n");
  3428. return true;
  3429. fail:
  3430. return false;
  3431. }
  3432. static bool
  3433. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3434. char *error_buf, uint32 error_buf_size)
  3435. {
  3436. const uint8 *p = buf, *p_end = buf_end;
  3437. uint32 table_count, i;
  3438. uint64 total_size;
  3439. WASMTable *table;
  3440. read_leb_uint32(p, p_end, table_count);
  3441. if (module->import_table_count + table_count > 1) {
  3442. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3443. /* a total of one table is allowed */
  3444. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3445. return false;
  3446. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3447. module->is_ref_types_used = true;
  3448. #endif
  3449. }
  3450. if (table_count) {
  3451. module->table_count = table_count;
  3452. total_size = sizeof(WASMTable) * (uint64)table_count;
  3453. if (!(module->tables =
  3454. loader_malloc(total_size, error_buf, error_buf_size))) {
  3455. return false;
  3456. }
  3457. /* load each table */
  3458. table = module->tables;
  3459. for (i = 0; i < table_count; i++, table++) {
  3460. #if WASM_ENABLE_GC != 0
  3461. uint8 flag;
  3462. bool has_init = false;
  3463. CHECK_BUF(buf, buf_end, 1);
  3464. flag = read_uint8(p);
  3465. if (flag == TABLE_INIT_EXPR_FLAG) {
  3466. CHECK_BUF(buf, buf_end, 1);
  3467. flag = read_uint8(p);
  3468. if (flag != 0x00) {
  3469. set_error_buf(error_buf, error_buf_size,
  3470. "invalid leading bytes for table");
  3471. return false;
  3472. }
  3473. has_init = true;
  3474. }
  3475. else {
  3476. p--;
  3477. }
  3478. #endif /* end of WASM_ENABLE_GC != 0 */
  3479. if (!load_table(&p, p_end, module, table, error_buf,
  3480. error_buf_size))
  3481. return false;
  3482. #if WASM_ENABLE_GC != 0
  3483. if (has_init) {
  3484. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3485. table->table_type.elem_type,
  3486. table->table_type.elem_ref_type, error_buf,
  3487. error_buf_size))
  3488. return false;
  3489. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3490. && table->init_expr.init_expr_type
  3491. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3492. set_error_buf(
  3493. error_buf, error_buf_size,
  3494. "unsupported initializer expression for table");
  3495. return false;
  3496. }
  3497. }
  3498. else {
  3499. if (wasm_is_reftype_htref_non_nullable(
  3500. table->table_type.elem_type)) {
  3501. set_error_buf(
  3502. error_buf, error_buf_size,
  3503. "type mismatch: non-nullable table without init expr");
  3504. return false;
  3505. }
  3506. }
  3507. #endif /* end of WASM_ENABLE_GC != 0 */
  3508. #if WASM_ENABLE_WAMR_COMPILER != 0
  3509. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3510. module->is_ref_types_used = true;
  3511. #endif
  3512. }
  3513. }
  3514. if (p != p_end) {
  3515. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3516. return false;
  3517. }
  3518. LOG_VERBOSE("Load table section success.\n");
  3519. return true;
  3520. fail:
  3521. return false;
  3522. }
  3523. static bool
  3524. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3525. char *error_buf, uint32 error_buf_size)
  3526. {
  3527. const uint8 *p = buf, *p_end = buf_end;
  3528. uint32 memory_count, i;
  3529. uint64 total_size;
  3530. WASMMemory *memory;
  3531. read_leb_uint32(p, p_end, memory_count);
  3532. #if WASM_ENABLE_MULTI_MEMORY == 0
  3533. /* a total of one memory is allowed */
  3534. if (module->import_memory_count + memory_count > 1) {
  3535. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3536. return false;
  3537. }
  3538. #endif
  3539. if (memory_count) {
  3540. module->memory_count = memory_count;
  3541. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3542. if (!(module->memories =
  3543. loader_malloc(total_size, error_buf, error_buf_size))) {
  3544. return false;
  3545. }
  3546. /* load each memory */
  3547. memory = module->memories;
  3548. for (i = 0; i < memory_count; i++, memory++)
  3549. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3550. return false;
  3551. }
  3552. if (p != p_end) {
  3553. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3554. return false;
  3555. }
  3556. LOG_VERBOSE("Load memory section success.\n");
  3557. return true;
  3558. fail:
  3559. return false;
  3560. }
  3561. static bool
  3562. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3563. char *error_buf, uint32 error_buf_size)
  3564. {
  3565. const uint8 *p = buf, *p_end = buf_end;
  3566. uint32 global_count, i;
  3567. uint64 total_size;
  3568. WASMGlobal *global;
  3569. uint8 mutable;
  3570. #if WASM_ENABLE_GC != 0
  3571. bool need_ref_type_map;
  3572. WASMRefType ref_type;
  3573. #endif
  3574. read_leb_uint32(p, p_end, global_count);
  3575. if (is_indices_overflow(module->import_global_count, global_count,
  3576. error_buf, error_buf_size))
  3577. return false;
  3578. module->global_count = 0;
  3579. if (global_count) {
  3580. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3581. if (!(module->globals =
  3582. loader_malloc(total_size, error_buf, error_buf_size))) {
  3583. return false;
  3584. }
  3585. global = module->globals;
  3586. for (i = 0; i < global_count; i++, global++) {
  3587. #if WASM_ENABLE_GC == 0
  3588. CHECK_BUF(p, p_end, 2);
  3589. /* global type */
  3590. global->type.val_type = read_uint8(p);
  3591. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3592. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3593. return false;
  3594. }
  3595. mutable = read_uint8(p);
  3596. #else
  3597. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3598. &need_ref_type_map, &ref_type, false,
  3599. error_buf, error_buf_size)) {
  3600. return false;
  3601. }
  3602. global->type.val_type = ref_type.ref_type;
  3603. CHECK_BUF(p, p_end, 1);
  3604. mutable = read_uint8(p);
  3605. #endif /* end of WASM_ENABLE_GC */
  3606. #if WASM_ENABLE_WAMR_COMPILER != 0
  3607. if (global->type.val_type == VALUE_TYPE_V128)
  3608. module->is_simd_used = true;
  3609. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3610. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3611. module->is_ref_types_used = true;
  3612. #endif
  3613. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3614. return false;
  3615. }
  3616. global->type.is_mutable = mutable ? true : false;
  3617. /* initialize expression */
  3618. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3619. global->type.val_type,
  3620. #if WASM_ENABLE_GC == 0
  3621. NULL,
  3622. #else
  3623. &ref_type,
  3624. #endif
  3625. error_buf, error_buf_size))
  3626. return false;
  3627. #if WASM_ENABLE_GC != 0
  3628. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3629. uint8 global_type;
  3630. WASMRefType *global_ref_type;
  3631. uint32 global_idx = global->init_expr.u.global_index;
  3632. if (global->init_expr.u.global_index
  3633. >= module->import_global_count + i) {
  3634. set_error_buf(error_buf, error_buf_size, "unknown global");
  3635. return false;
  3636. }
  3637. if (global_idx < module->import_global_count) {
  3638. global_type = module->import_globals[global_idx]
  3639. .u.global.type.val_type;
  3640. global_ref_type =
  3641. module->import_globals[global_idx].u.global.ref_type;
  3642. }
  3643. else {
  3644. global_type =
  3645. module
  3646. ->globals[global_idx - module->import_global_count]
  3647. .type.val_type;
  3648. global_ref_type =
  3649. module
  3650. ->globals[global_idx - module->import_global_count]
  3651. .ref_type;
  3652. }
  3653. if (!wasm_reftype_is_subtype_of(
  3654. global_type, global_ref_type, global->type.val_type,
  3655. global->ref_type, module->types, module->type_count)) {
  3656. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3657. return false;
  3658. }
  3659. }
  3660. if (need_ref_type_map) {
  3661. if (!(global->ref_type =
  3662. reftype_set_insert(module->ref_type_set, &ref_type,
  3663. error_buf, error_buf_size))) {
  3664. return false;
  3665. }
  3666. }
  3667. #if TRACE_WASM_LOADER != 0
  3668. os_printf("global type: ");
  3669. wasm_dump_value_type(global->type, global->ref_type);
  3670. os_printf("\n");
  3671. #endif
  3672. #endif
  3673. module->global_count++;
  3674. }
  3675. bh_assert(module->global_count == global_count);
  3676. }
  3677. if (p != p_end) {
  3678. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3679. return false;
  3680. }
  3681. LOG_VERBOSE("Load global section success.\n");
  3682. return true;
  3683. fail:
  3684. return false;
  3685. }
  3686. static bool
  3687. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3688. bool is_load_from_file_buf, char *error_buf,
  3689. uint32 error_buf_size)
  3690. {
  3691. const uint8 *p = buf, *p_end = buf_end;
  3692. uint32 export_count, i, j, index;
  3693. uint64 total_size;
  3694. uint32 str_len;
  3695. WASMExport *export;
  3696. const char *name;
  3697. read_leb_uint32(p, p_end, export_count);
  3698. if (export_count) {
  3699. module->export_count = export_count;
  3700. total_size = sizeof(WASMExport) * (uint64)export_count;
  3701. if (!(module->exports =
  3702. loader_malloc(total_size, error_buf, error_buf_size))) {
  3703. return false;
  3704. }
  3705. export = module->exports;
  3706. for (i = 0; i < export_count; i++, export ++) {
  3707. #if WASM_ENABLE_THREAD_MGR == 0
  3708. if (p == p_end) {
  3709. /* export section with inconsistent count:
  3710. n export declared, but less than n given */
  3711. set_error_buf(error_buf, error_buf_size,
  3712. "length out of bounds");
  3713. return false;
  3714. }
  3715. #endif
  3716. read_leb_uint32(p, p_end, str_len);
  3717. CHECK_BUF(p, p_end, str_len);
  3718. for (j = 0; j < i; j++) {
  3719. name = module->exports[j].name;
  3720. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  3721. set_error_buf(error_buf, error_buf_size,
  3722. "duplicate export name");
  3723. return false;
  3724. }
  3725. }
  3726. if (!(export->name = wasm_const_str_list_insert(
  3727. p, str_len, module, is_load_from_file_buf, error_buf,
  3728. error_buf_size))) {
  3729. return false;
  3730. }
  3731. p += str_len;
  3732. CHECK_BUF(p, p_end, 1);
  3733. export->kind = read_uint8(p);
  3734. read_leb_uint32(p, p_end, index);
  3735. export->index = index;
  3736. switch (export->kind) {
  3737. /* function index */
  3738. case EXPORT_KIND_FUNC:
  3739. if (index >= module->function_count
  3740. + module->import_function_count) {
  3741. set_error_buf(error_buf, error_buf_size,
  3742. "unknown function");
  3743. return false;
  3744. }
  3745. #if WASM_ENABLE_SIMD != 0
  3746. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3747. /* TODO: check func type, if it has v128 param or result,
  3748. report error */
  3749. #endif
  3750. #endif
  3751. break;
  3752. /* table index */
  3753. case EXPORT_KIND_TABLE:
  3754. if (index
  3755. >= module->table_count + module->import_table_count) {
  3756. set_error_buf(error_buf, error_buf_size,
  3757. "unknown table");
  3758. return false;
  3759. }
  3760. break;
  3761. /* memory index */
  3762. case EXPORT_KIND_MEMORY:
  3763. if (index
  3764. >= module->memory_count + module->import_memory_count) {
  3765. set_error_buf(error_buf, error_buf_size,
  3766. "unknown memory");
  3767. return false;
  3768. }
  3769. break;
  3770. #if WASM_ENABLE_TAGS != 0
  3771. /* export tag */
  3772. case EXPORT_KIND_TAG:
  3773. if (index >= module->tag_count + module->import_tag_count) {
  3774. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3775. return false;
  3776. }
  3777. break;
  3778. #endif
  3779. /* global index */
  3780. case EXPORT_KIND_GLOBAL:
  3781. if (index
  3782. >= module->global_count + module->import_global_count) {
  3783. set_error_buf(error_buf, error_buf_size,
  3784. "unknown global");
  3785. return false;
  3786. }
  3787. break;
  3788. default:
  3789. set_error_buf(error_buf, error_buf_size,
  3790. "invalid export kind");
  3791. return false;
  3792. }
  3793. }
  3794. }
  3795. if (p != p_end) {
  3796. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3797. return false;
  3798. }
  3799. LOG_VERBOSE("Load export section success.\n");
  3800. return true;
  3801. fail:
  3802. return false;
  3803. }
  3804. static bool
  3805. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3806. uint32 error_buf_size)
  3807. {
  3808. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3809. if (table_index != 0) {
  3810. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3811. return false;
  3812. }
  3813. #endif
  3814. if (table_index >= module->import_table_count + module->table_count) {
  3815. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3816. table_index);
  3817. return false;
  3818. }
  3819. return true;
  3820. }
  3821. static bool
  3822. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3823. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3824. {
  3825. const uint8 *p = *p_buf, *p_end = buf_end;
  3826. uint32 table_index;
  3827. read_leb_uint32(p, p_end, table_index);
  3828. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3829. return false;
  3830. }
  3831. *p_table_index = table_index;
  3832. *p_buf = p;
  3833. return true;
  3834. fail:
  3835. return false;
  3836. }
  3837. /* Element segments must match element type of table */
  3838. static bool
  3839. check_table_elem_type(WASMModule *module, uint32 table_index,
  3840. uint32 type_from_elem_seg, char *error_buf,
  3841. uint32 error_buf_size)
  3842. {
  3843. uint32 table_declared_elem_type;
  3844. if (table_index < module->import_table_count)
  3845. table_declared_elem_type =
  3846. module->import_tables[table_index].u.table.table_type.elem_type;
  3847. else
  3848. table_declared_elem_type =
  3849. (module->tables + table_index)->table_type.elem_type;
  3850. if (table_declared_elem_type == type_from_elem_seg)
  3851. return true;
  3852. #if WASM_ENABLE_GC != 0
  3853. /*
  3854. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3855. */
  3856. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3857. return true;
  3858. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3859. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3860. return true;
  3861. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3862. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3863. return true;
  3864. #endif
  3865. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3866. return false;
  3867. }
  3868. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3869. static bool
  3870. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3871. uint32 *p_elem_type,
  3872. #if WASM_ENABLE_GC != 0
  3873. WASMRefType **p_elem_ref_type,
  3874. #endif
  3875. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3876. {
  3877. const uint8 *p = *p_buf, *p_end = buf_end;
  3878. uint8 elem_type;
  3879. #if WASM_ENABLE_GC != 0
  3880. WASMRefType elem_ref_type;
  3881. bool need_ref_type_map;
  3882. #endif
  3883. CHECK_BUF(p, p_end, 1);
  3884. elem_type = read_uint8(p);
  3885. if (elemkind_zero) {
  3886. if (elem_type != 0) {
  3887. set_error_buf(error_buf, error_buf_size,
  3888. "invalid reference type or unknown type");
  3889. return false;
  3890. }
  3891. else {
  3892. *p_elem_type = VALUE_TYPE_FUNCREF;
  3893. *p_buf = p;
  3894. return true;
  3895. }
  3896. }
  3897. #if WASM_ENABLE_GC == 0
  3898. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3899. set_error_buf(error_buf, error_buf_size,
  3900. "invalid reference type or unknown type");
  3901. return false;
  3902. }
  3903. *p_elem_type = elem_type;
  3904. #else
  3905. p--;
  3906. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3907. module->type_count, &need_ref_type_map,
  3908. &elem_ref_type, false, error_buf, error_buf_size)) {
  3909. return false;
  3910. }
  3911. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3912. set_error_buf(error_buf, error_buf_size,
  3913. "invalid reference type or unknown type");
  3914. return false;
  3915. }
  3916. *p_elem_type = elem_ref_type.ref_type;
  3917. if (need_ref_type_map) {
  3918. if (!(*p_elem_ref_type =
  3919. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3920. error_buf, error_buf_size))) {
  3921. return false;
  3922. }
  3923. }
  3924. #endif
  3925. *p_buf = p;
  3926. return true;
  3927. fail:
  3928. return false;
  3929. }
  3930. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3931. static bool
  3932. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3933. WASMModule *module, WASMTableSeg *table_segment,
  3934. char *error_buf, uint32 error_buf_size)
  3935. {
  3936. const uint8 *p = *p_buf, *p_end = buf_end;
  3937. uint32 function_count, function_index = 0, i;
  3938. uint64 total_size;
  3939. read_leb_uint32(p, p_end, function_count);
  3940. table_segment->value_count = function_count;
  3941. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3942. if (total_size > 0
  3943. && !(table_segment->init_values =
  3944. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3945. error_buf_size))) {
  3946. return false;
  3947. }
  3948. for (i = 0; i < function_count; i++) {
  3949. InitializerExpression *init_expr = &table_segment->init_values[i];
  3950. read_leb_uint32(p, p_end, function_index);
  3951. if (!check_function_index(module, function_index, error_buf,
  3952. error_buf_size)) {
  3953. return false;
  3954. }
  3955. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3956. init_expr->u.ref_index = function_index;
  3957. }
  3958. *p_buf = p;
  3959. return true;
  3960. fail:
  3961. return false;
  3962. }
  3963. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3964. static bool
  3965. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3966. WASMModule *module, WASMTableSeg *table_segment,
  3967. char *error_buf, uint32 error_buf_size)
  3968. {
  3969. const uint8 *p = *p_buf, *p_end = buf_end;
  3970. uint32 ref_count, i;
  3971. uint64 total_size;
  3972. read_leb_uint32(p, p_end, ref_count);
  3973. table_segment->value_count = ref_count;
  3974. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3975. if (total_size > 0
  3976. && !(table_segment->init_values =
  3977. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3978. error_buf_size))) {
  3979. return false;
  3980. }
  3981. for (i = 0; i < ref_count; i++) {
  3982. InitializerExpression *init_expr = &table_segment->init_values[i];
  3983. if (!load_init_expr(module, &p, p_end, init_expr,
  3984. table_segment->elem_type,
  3985. #if WASM_ENABLE_GC == 0
  3986. NULL,
  3987. #else
  3988. table_segment->elem_ref_type,
  3989. #endif
  3990. error_buf, error_buf_size))
  3991. return false;
  3992. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3993. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3994. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3995. && init_expr->init_expr_type
  3996. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3997. }
  3998. *p_buf = p;
  3999. return true;
  4000. fail:
  4001. return false;
  4002. }
  4003. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  4004. static bool
  4005. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  4006. WASMModule *module, char *error_buf,
  4007. uint32 error_buf_size)
  4008. {
  4009. const uint8 *p = buf, *p_end = buf_end;
  4010. uint32 table_segment_count, i;
  4011. uint64 total_size;
  4012. WASMTableSeg *table_segment;
  4013. read_leb_uint32(p, p_end, table_segment_count);
  4014. if (table_segment_count) {
  4015. module->table_seg_count = table_segment_count;
  4016. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  4017. if (!(module->table_segments =
  4018. loader_malloc(total_size, error_buf, error_buf_size))) {
  4019. return false;
  4020. }
  4021. table_segment = module->table_segments;
  4022. for (i = 0; i < table_segment_count; i++, table_segment++) {
  4023. if (p >= p_end) {
  4024. set_error_buf(error_buf, error_buf_size,
  4025. "invalid value type or "
  4026. "invalid elements segment kind");
  4027. return false;
  4028. }
  4029. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4030. read_leb_uint32(p, p_end, table_segment->mode);
  4031. /* last three bits */
  4032. table_segment->mode = table_segment->mode & 0x07;
  4033. switch (table_segment->mode) {
  4034. /* elemkind/elemtype + active */
  4035. case 0:
  4036. case 4:
  4037. {
  4038. #if WASM_ENABLE_GC != 0
  4039. if (table_segment->mode == 0) {
  4040. /* vec(funcidx), set elem type to (ref func) */
  4041. WASMRefType elem_ref_type = { 0 };
  4042. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4043. wasm_set_refheaptype_common(
  4044. &elem_ref_type.ref_ht_common, false,
  4045. HEAP_TYPE_FUNC);
  4046. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4047. module->ref_type_set, &elem_ref_type,
  4048. error_buf, error_buf_size)))
  4049. return false;
  4050. }
  4051. else {
  4052. /* vec(expr), set elem type to funcref */
  4053. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4054. }
  4055. #else
  4056. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4057. #endif
  4058. table_segment->table_index = 0;
  4059. if (!check_table_index(module, table_segment->table_index,
  4060. error_buf, error_buf_size))
  4061. return false;
  4062. if (!load_init_expr(
  4063. module, &p, p_end, &table_segment->base_offset,
  4064. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4065. return false;
  4066. if (table_segment->mode == 0) {
  4067. /* vec(funcidx) */
  4068. if (!load_func_index_vec(&p, p_end, module,
  4069. table_segment, error_buf,
  4070. error_buf_size))
  4071. return false;
  4072. }
  4073. else {
  4074. /* vec(expr) */
  4075. if (!load_init_expr_vec(&p, p_end, module,
  4076. table_segment, error_buf,
  4077. error_buf_size))
  4078. return false;
  4079. }
  4080. if (!check_table_elem_type(module,
  4081. table_segment->table_index,
  4082. table_segment->elem_type,
  4083. error_buf, error_buf_size))
  4084. return false;
  4085. break;
  4086. }
  4087. /* elemkind + passive/declarative */
  4088. case 1:
  4089. case 3:
  4090. if (!load_elem_type(module, &p, p_end,
  4091. &table_segment->elem_type,
  4092. #if WASM_ENABLE_GC != 0
  4093. &table_segment->elem_ref_type,
  4094. #endif
  4095. true, error_buf, error_buf_size))
  4096. return false;
  4097. /* vec(funcidx) */
  4098. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4099. error_buf, error_buf_size))
  4100. return false;
  4101. break;
  4102. /* elemkind/elemtype + table_idx + active */
  4103. case 2:
  4104. case 6:
  4105. if (!load_table_index(&p, p_end, module,
  4106. &table_segment->table_index,
  4107. error_buf, error_buf_size))
  4108. return false;
  4109. if (!load_init_expr(
  4110. module, &p, p_end, &table_segment->base_offset,
  4111. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4112. return false;
  4113. if (!load_elem_type(module, &p, p_end,
  4114. &table_segment->elem_type,
  4115. #if WASM_ENABLE_GC != 0
  4116. &table_segment->elem_ref_type,
  4117. #endif
  4118. table_segment->mode == 2 ? true : false,
  4119. error_buf, error_buf_size))
  4120. return false;
  4121. if (table_segment->mode == 2) {
  4122. /* vec(funcidx) */
  4123. if (!load_func_index_vec(&p, p_end, module,
  4124. table_segment, error_buf,
  4125. error_buf_size))
  4126. return false;
  4127. }
  4128. else {
  4129. /* vec(expr) */
  4130. if (!load_init_expr_vec(&p, p_end, module,
  4131. table_segment, error_buf,
  4132. error_buf_size))
  4133. return false;
  4134. }
  4135. if (!check_table_elem_type(module,
  4136. table_segment->table_index,
  4137. table_segment->elem_type,
  4138. error_buf, error_buf_size))
  4139. return false;
  4140. break;
  4141. case 5:
  4142. case 7:
  4143. if (!load_elem_type(module, &p, p_end,
  4144. &table_segment->elem_type,
  4145. #if WASM_ENABLE_GC != 0
  4146. &table_segment->elem_ref_type,
  4147. #endif
  4148. false, error_buf, error_buf_size))
  4149. return false;
  4150. /* vec(expr) */
  4151. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4152. error_buf, error_buf_size))
  4153. return false;
  4154. break;
  4155. default:
  4156. set_error_buf(error_buf, error_buf_size,
  4157. "unknown element segment kind");
  4158. return false;
  4159. }
  4160. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4161. /*
  4162. * like: 00 41 05 0b 04 00 01 00 01
  4163. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4164. */
  4165. if (!load_table_index(&p, p_end, module,
  4166. &table_segment->table_index, error_buf,
  4167. error_buf_size))
  4168. return false;
  4169. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4170. VALUE_TYPE_I32, NULL, error_buf,
  4171. error_buf_size))
  4172. return false;
  4173. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4174. error_buf, error_buf_size))
  4175. return false;
  4176. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4177. if (!check_table_elem_type(module, table_segment->table_index,
  4178. table_segment->elem_type, error_buf,
  4179. error_buf_size))
  4180. return false;
  4181. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4182. #if WASM_ENABLE_WAMR_COMPILER != 0
  4183. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4184. module->is_ref_types_used = true;
  4185. #endif
  4186. }
  4187. }
  4188. if (p != p_end) {
  4189. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4190. return false;
  4191. }
  4192. LOG_VERBOSE("Load table segment section success.\n");
  4193. return true;
  4194. fail:
  4195. return false;
  4196. }
  4197. static bool
  4198. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4199. WASMModule *module,
  4200. #if WASM_ENABLE_BULK_MEMORY != 0
  4201. bool has_datacount_section,
  4202. #endif
  4203. bool clone_data_seg, char *error_buf,
  4204. uint32 error_buf_size)
  4205. {
  4206. const uint8 *p = buf, *p_end = buf_end;
  4207. uint32 data_seg_count, i, mem_index, data_seg_len;
  4208. uint64 total_size;
  4209. WASMDataSeg *dataseg;
  4210. InitializerExpression init_expr;
  4211. #if WASM_ENABLE_BULK_MEMORY != 0
  4212. bool is_passive = false;
  4213. uint32 mem_flag;
  4214. #endif
  4215. uint8 mem_offset_type = VALUE_TYPE_I32;
  4216. read_leb_uint32(p, p_end, data_seg_count);
  4217. #if WASM_ENABLE_BULK_MEMORY != 0
  4218. if (has_datacount_section && data_seg_count != module->data_seg_count1) {
  4219. set_error_buf(error_buf, error_buf_size,
  4220. "data count and data section have inconsistent lengths");
  4221. return false;
  4222. }
  4223. #endif
  4224. if (data_seg_count) {
  4225. module->data_seg_count = data_seg_count;
  4226. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4227. if (!(module->data_segments =
  4228. loader_malloc(total_size, error_buf, error_buf_size))) {
  4229. return false;
  4230. }
  4231. for (i = 0; i < data_seg_count; i++) {
  4232. read_leb_uint32(p, p_end, mem_index);
  4233. #if WASM_ENABLE_BULK_MEMORY != 0
  4234. is_passive = false;
  4235. mem_flag = mem_index & 0x03;
  4236. switch (mem_flag) {
  4237. case 0x01:
  4238. is_passive = true;
  4239. #if WASM_ENABLE_WAMR_COMPILER != 0
  4240. module->is_bulk_memory_used = true;
  4241. #endif
  4242. break;
  4243. case 0x00:
  4244. /* no memory index, treat index as 0 */
  4245. mem_index = 0;
  4246. goto check_mem_index;
  4247. case 0x02:
  4248. /* read following memory index */
  4249. read_leb_uint32(p, p_end, mem_index);
  4250. #if WASM_ENABLE_WAMR_COMPILER != 0
  4251. module->is_bulk_memory_used = true;
  4252. #endif
  4253. check_mem_index:
  4254. if (mem_index
  4255. >= module->import_memory_count + module->memory_count) {
  4256. set_error_buf_v(error_buf, error_buf_size,
  4257. "unknown memory %d", mem_index);
  4258. return false;
  4259. }
  4260. break;
  4261. case 0x03:
  4262. default:
  4263. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4264. return false;
  4265. break;
  4266. }
  4267. #else
  4268. if (mem_index
  4269. >= module->import_memory_count + module->memory_count) {
  4270. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4271. mem_index);
  4272. return false;
  4273. }
  4274. #endif /* WASM_ENABLE_BULK_MEMORY */
  4275. #if WASM_ENABLE_BULK_MEMORY != 0
  4276. if (!is_passive)
  4277. #endif
  4278. {
  4279. #if WASM_ENABLE_MEMORY64 != 0
  4280. /* This memory_flag is from memory instead of data segment */
  4281. uint8 memory_flag;
  4282. if (module->import_memory_count > 0) {
  4283. memory_flag = module->import_memories[mem_index]
  4284. .u.memory.mem_type.flags;
  4285. }
  4286. else {
  4287. memory_flag =
  4288. module
  4289. ->memories[mem_index - module->import_memory_count]
  4290. .flags;
  4291. }
  4292. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4293. : VALUE_TYPE_I32;
  4294. #else
  4295. mem_offset_type = VALUE_TYPE_I32;
  4296. #endif
  4297. }
  4298. #if WASM_ENABLE_BULK_MEMORY != 0
  4299. if (!is_passive)
  4300. #endif
  4301. if (!load_init_expr(module, &p, p_end, &init_expr,
  4302. mem_offset_type, NULL, error_buf,
  4303. error_buf_size))
  4304. return false;
  4305. read_leb_uint32(p, p_end, data_seg_len);
  4306. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4307. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4308. return false;
  4309. }
  4310. #if WASM_ENABLE_BULK_MEMORY != 0
  4311. dataseg->is_passive = is_passive;
  4312. if (!is_passive)
  4313. #endif
  4314. {
  4315. bh_memcpy_s(&dataseg->base_offset,
  4316. sizeof(InitializerExpression), &init_expr,
  4317. sizeof(InitializerExpression));
  4318. dataseg->memory_index = mem_index;
  4319. }
  4320. dataseg->data_length = data_seg_len;
  4321. CHECK_BUF(p, p_end, data_seg_len);
  4322. if (clone_data_seg) {
  4323. if (!(dataseg->data = loader_malloc(
  4324. dataseg->data_length, error_buf, error_buf_size))) {
  4325. return false;
  4326. }
  4327. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4328. data_seg_len);
  4329. }
  4330. else {
  4331. dataseg->data = (uint8 *)p;
  4332. }
  4333. dataseg->is_data_cloned = clone_data_seg;
  4334. p += data_seg_len;
  4335. }
  4336. }
  4337. if (p != p_end) {
  4338. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4339. return false;
  4340. }
  4341. LOG_VERBOSE("Load data segment section success.\n");
  4342. return true;
  4343. fail:
  4344. return false;
  4345. }
  4346. #if WASM_ENABLE_BULK_MEMORY != 0
  4347. static bool
  4348. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4349. WASMModule *module, char *error_buf,
  4350. uint32 error_buf_size)
  4351. {
  4352. const uint8 *p = buf, *p_end = buf_end;
  4353. uint32 data_seg_count1 = 0;
  4354. read_leb_uint32(p, p_end, data_seg_count1);
  4355. module->data_seg_count1 = data_seg_count1;
  4356. if (p != p_end) {
  4357. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4358. return false;
  4359. }
  4360. #if WASM_ENABLE_WAMR_COMPILER != 0
  4361. module->is_bulk_memory_used = true;
  4362. #endif
  4363. LOG_VERBOSE("Load datacount section success.\n");
  4364. return true;
  4365. fail:
  4366. return false;
  4367. }
  4368. #endif
  4369. #if WASM_ENABLE_TAGS != 0
  4370. static bool
  4371. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4372. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4373. uint32 error_buf_size)
  4374. {
  4375. (void)buf_code;
  4376. (void)buf_code_end;
  4377. const uint8 *p = buf, *p_end = buf_end;
  4378. size_t total_size = 0;
  4379. /* number of tags defined in the section */
  4380. uint32 section_tag_count = 0;
  4381. uint8 tag_attribute;
  4382. uint32 tag_type;
  4383. WASMTag *tag = NULL;
  4384. /* get tag count */
  4385. read_leb_uint32(p, p_end, section_tag_count);
  4386. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4387. error_buf, error_buf_size))
  4388. return false;
  4389. module->tag_count = section_tag_count;
  4390. if (section_tag_count) {
  4391. total_size = sizeof(WASMTag *) * module->tag_count;
  4392. if (!(module->tags =
  4393. loader_malloc(total_size, error_buf, error_buf_size))) {
  4394. return false;
  4395. }
  4396. /* load each tag, imported tags precede the tags */
  4397. uint32 tag_index;
  4398. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4399. /* get the one byte attribute */
  4400. CHECK_BUF(p, p_end, 1);
  4401. tag_attribute = read_uint8(p);
  4402. /* get type */
  4403. read_leb_uint32(p, p_end, tag_type);
  4404. /* compare against module->types */
  4405. if (tag_type >= module->type_count) {
  4406. set_error_buf(error_buf, error_buf_size, "unknown type");
  4407. return false;
  4408. }
  4409. /* get return type (must be 0) */
  4410. /* check, that the type of the referred tag returns void */
  4411. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4412. if (func_type->result_count != 0) {
  4413. set_error_buf(error_buf, error_buf_size,
  4414. "non-empty tag result type");
  4415. goto fail;
  4416. }
  4417. if (!(tag = module->tags[tag_index] = loader_malloc(
  4418. sizeof(WASMTag), error_buf, error_buf_size))) {
  4419. return false;
  4420. }
  4421. /* store to module tag declarations */
  4422. tag->attribute = tag_attribute;
  4423. tag->type = tag_type;
  4424. tag->tag_type = func_type;
  4425. }
  4426. }
  4427. if (p != p_end) {
  4428. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4429. return false;
  4430. }
  4431. LOG_VERBOSE("Load tag section success.\n");
  4432. return true;
  4433. fail:
  4434. return false;
  4435. }
  4436. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4437. static bool
  4438. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4439. const uint8 *buf_func_end, WASMModule *module,
  4440. char *error_buf, uint32 error_buf_size)
  4441. {
  4442. const uint8 *p = buf, *p_end = buf_end;
  4443. const uint8 *p_func = buf_func;
  4444. uint32 func_count = 0, code_count;
  4445. /* code has been loaded in function section, so pass it here, just check
  4446. * whether function and code section have inconsistent lengths */
  4447. read_leb_uint32(p, p_end, code_count);
  4448. if (buf_func)
  4449. read_leb_uint32(p_func, buf_func_end, func_count);
  4450. if (func_count != code_count) {
  4451. set_error_buf(error_buf, error_buf_size,
  4452. "function and code section have inconsistent lengths");
  4453. return false;
  4454. }
  4455. LOG_VERBOSE("Load code segment section success.\n");
  4456. (void)module;
  4457. return true;
  4458. fail:
  4459. return false;
  4460. }
  4461. static bool
  4462. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4463. char *error_buf, uint32 error_buf_size)
  4464. {
  4465. const uint8 *p = buf, *p_end = buf_end;
  4466. WASMFuncType *type;
  4467. uint32 start_function;
  4468. read_leb_uint32(p, p_end, start_function);
  4469. if (start_function
  4470. >= module->function_count + module->import_function_count) {
  4471. set_error_buf(error_buf, error_buf_size, "unknown function");
  4472. return false;
  4473. }
  4474. if (start_function < module->import_function_count)
  4475. type = module->import_functions[start_function].u.function.func_type;
  4476. else
  4477. type = module->functions[start_function - module->import_function_count]
  4478. ->func_type;
  4479. if (type->param_count != 0 || type->result_count != 0) {
  4480. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4481. return false;
  4482. }
  4483. module->start_function = start_function;
  4484. if (p != p_end) {
  4485. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4486. return false;
  4487. }
  4488. LOG_VERBOSE("Load start section success.\n");
  4489. return true;
  4490. fail:
  4491. return false;
  4492. }
  4493. #if WASM_ENABLE_STRINGREF != 0
  4494. static bool
  4495. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4496. WASMModule *module, bool is_load_from_file_buf,
  4497. char *error_buf, uint32 error_buf_size)
  4498. {
  4499. const uint8 *p = buf, *p_end = buf_end;
  4500. int32 deferred_count, immediate_count, string_length, i;
  4501. uint64 total_size;
  4502. read_leb_uint32(p, p_end, deferred_count);
  4503. read_leb_uint32(p, p_end, immediate_count);
  4504. /* proposal set deferred_count for future extension */
  4505. if (deferred_count != 0) {
  4506. goto fail;
  4507. }
  4508. if (immediate_count > 0) {
  4509. total_size = sizeof(char *) * (uint64)immediate_count;
  4510. if (!(module->string_literal_ptrs =
  4511. loader_malloc(total_size, error_buf, error_buf_size))) {
  4512. goto fail;
  4513. }
  4514. module->string_literal_count = immediate_count;
  4515. total_size = sizeof(uint32) * (uint64)immediate_count;
  4516. if (!(module->string_literal_lengths =
  4517. loader_malloc(total_size, error_buf, error_buf_size))) {
  4518. goto fail;
  4519. }
  4520. for (i = 0; i < immediate_count; i++) {
  4521. read_leb_uint32(p, p_end, string_length);
  4522. CHECK_BUF(p, p_end, string_length);
  4523. module->string_literal_ptrs[i] = p;
  4524. module->string_literal_lengths[i] = string_length;
  4525. p += string_length;
  4526. }
  4527. }
  4528. if (p != p_end) {
  4529. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4530. goto fail;
  4531. }
  4532. LOG_VERBOSE("Load stringref section success.\n");
  4533. return true;
  4534. fail:
  4535. return false;
  4536. }
  4537. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4538. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4539. static bool
  4540. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4541. bool is_load_from_file_buf, char *error_buf,
  4542. uint32 error_buf_size)
  4543. {
  4544. const uint8 *p = buf, *p_end = buf_end;
  4545. uint32 name_type, subsection_size;
  4546. uint32 previous_name_type = 0;
  4547. uint32 num_func_name;
  4548. uint32 func_index;
  4549. uint32 previous_func_index = ~0U;
  4550. uint32 func_name_len;
  4551. uint32 name_index;
  4552. int i = 0;
  4553. if (p >= p_end) {
  4554. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4555. return false;
  4556. }
  4557. while (p < p_end) {
  4558. read_leb_uint32(p, p_end, name_type);
  4559. if (i != 0) {
  4560. if (name_type == previous_name_type) {
  4561. set_error_buf(error_buf, error_buf_size,
  4562. "duplicate sub-section");
  4563. return false;
  4564. }
  4565. if (name_type < previous_name_type) {
  4566. set_error_buf(error_buf, error_buf_size,
  4567. "out-of-order sub-section");
  4568. return false;
  4569. }
  4570. }
  4571. previous_name_type = name_type;
  4572. read_leb_uint32(p, p_end, subsection_size);
  4573. CHECK_BUF(p, p_end, subsection_size);
  4574. switch (name_type) {
  4575. case SUB_SECTION_TYPE_FUNC:
  4576. if (subsection_size) {
  4577. read_leb_uint32(p, p_end, num_func_name);
  4578. for (name_index = 0; name_index < num_func_name;
  4579. name_index++) {
  4580. read_leb_uint32(p, p_end, func_index);
  4581. if (func_index == previous_func_index) {
  4582. set_error_buf(error_buf, error_buf_size,
  4583. "duplicate function name");
  4584. return false;
  4585. }
  4586. if (func_index < previous_func_index
  4587. && previous_func_index != ~0U) {
  4588. set_error_buf(error_buf, error_buf_size,
  4589. "out-of-order function index ");
  4590. return false;
  4591. }
  4592. previous_func_index = func_index;
  4593. read_leb_uint32(p, p_end, func_name_len);
  4594. CHECK_BUF(p, p_end, func_name_len);
  4595. /* Skip the import functions */
  4596. if (func_index >= module->import_function_count) {
  4597. func_index -= module->import_function_count;
  4598. if (func_index >= module->function_count) {
  4599. set_error_buf(error_buf, error_buf_size,
  4600. "out-of-range function index");
  4601. return false;
  4602. }
  4603. if (!(module->functions[func_index]->field_name =
  4604. wasm_const_str_list_insert(
  4605. p, func_name_len, module,
  4606. #if WASM_ENABLE_WAMR_COMPILER != 0
  4607. false,
  4608. #else
  4609. is_load_from_file_buf,
  4610. #endif
  4611. error_buf, error_buf_size))) {
  4612. return false;
  4613. }
  4614. }
  4615. p += func_name_len;
  4616. }
  4617. }
  4618. break;
  4619. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4620. */
  4621. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4622. default:
  4623. p = p + subsection_size;
  4624. break;
  4625. }
  4626. i++;
  4627. }
  4628. return true;
  4629. fail:
  4630. return false;
  4631. }
  4632. #endif
  4633. static bool
  4634. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4635. bool is_load_from_file_buf, char *error_buf,
  4636. uint32 error_buf_size)
  4637. {
  4638. const uint8 *p = buf, *p_end = buf_end;
  4639. char section_name[32];
  4640. uint32 name_len, buffer_len;
  4641. if (p >= p_end) {
  4642. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4643. return false;
  4644. }
  4645. read_leb_uint32(p, p_end, name_len);
  4646. if (p + name_len > p_end) {
  4647. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4648. return false;
  4649. }
  4650. if (!wasm_check_utf8_str(p, name_len)) {
  4651. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4652. return false;
  4653. }
  4654. buffer_len = sizeof(section_name);
  4655. memset(section_name, 0, buffer_len);
  4656. if (name_len < buffer_len) {
  4657. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4658. }
  4659. else {
  4660. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4661. memset(section_name + buffer_len - 4, '.', 3);
  4662. }
  4663. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4664. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4665. module->name_section_buf = buf;
  4666. module->name_section_buf_end = buf_end;
  4667. p += name_len;
  4668. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4669. error_buf, error_buf_size)) {
  4670. return false;
  4671. }
  4672. LOG_VERBOSE("Load custom name section success.");
  4673. }
  4674. #endif
  4675. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4676. {
  4677. WASMCustomSection *section =
  4678. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4679. if (!section) {
  4680. return false;
  4681. }
  4682. section->name_addr = (char *)p;
  4683. section->name_len = name_len;
  4684. section->content_addr = (uint8 *)(p + name_len);
  4685. section->content_len = (uint32)(p_end - p - name_len);
  4686. section->next = module->custom_section_list;
  4687. module->custom_section_list = section;
  4688. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4689. return true;
  4690. }
  4691. #endif
  4692. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4693. (void)is_load_from_file_buf;
  4694. (void)module;
  4695. return true;
  4696. fail:
  4697. return false;
  4698. }
  4699. static void
  4700. calculate_global_data_offset(WASMModule *module)
  4701. {
  4702. uint32 i, data_offset;
  4703. data_offset = 0;
  4704. for (i = 0; i < module->import_global_count; i++) {
  4705. WASMGlobalImport *import_global =
  4706. &((module->import_globals + i)->u.global);
  4707. #if WASM_ENABLE_FAST_JIT != 0
  4708. import_global->data_offset = data_offset;
  4709. #endif
  4710. data_offset += wasm_value_type_size(import_global->type.val_type);
  4711. }
  4712. for (i = 0; i < module->global_count; i++) {
  4713. WASMGlobal *global = module->globals + i;
  4714. #if WASM_ENABLE_FAST_JIT != 0
  4715. global->data_offset = data_offset;
  4716. #endif
  4717. data_offset += wasm_value_type_size(global->type.val_type);
  4718. }
  4719. module->global_data_size = data_offset;
  4720. }
  4721. #if WASM_ENABLE_FAST_JIT != 0
  4722. static bool
  4723. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4724. uint32 error_buf_size)
  4725. {
  4726. #if WASM_ENABLE_LAZY_JIT != 0
  4727. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4728. #endif
  4729. uint32 i;
  4730. if (!module->function_count)
  4731. return true;
  4732. if (!(module->fast_jit_func_ptrs =
  4733. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4734. error_buf_size))) {
  4735. return false;
  4736. }
  4737. #if WASM_ENABLE_LAZY_JIT != 0
  4738. for (i = 0; i < module->function_count; i++) {
  4739. module->fast_jit_func_ptrs[i] =
  4740. jit_globals->compile_fast_jit_and_then_call;
  4741. }
  4742. #endif
  4743. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4744. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4745. set_error_buf(error_buf, error_buf_size,
  4746. "init fast jit thread lock failed");
  4747. return false;
  4748. }
  4749. module->fast_jit_thread_locks_inited[i] = true;
  4750. }
  4751. return true;
  4752. }
  4753. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4754. #if WASM_ENABLE_JIT != 0
  4755. static bool
  4756. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4757. uint32 error_buf_size)
  4758. {
  4759. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4760. AOTCompOption option = { 0 };
  4761. char *aot_last_error;
  4762. uint64 size;
  4763. #if WASM_ENABLE_GC != 0
  4764. bool gc_enabled = true;
  4765. #else
  4766. bool gc_enabled = false;
  4767. #endif
  4768. if (module->function_count == 0)
  4769. return true;
  4770. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4771. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4772. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4773. return false;
  4774. }
  4775. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4776. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4777. os_mutex_destroy(&module->tierup_wait_lock);
  4778. return false;
  4779. }
  4780. module->tierup_wait_lock_inited = true;
  4781. #endif
  4782. size = sizeof(void *) * (uint64)module->function_count
  4783. + sizeof(bool) * (uint64)module->function_count;
  4784. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4785. return false;
  4786. }
  4787. module->func_ptrs_compiled =
  4788. (bool *)((uint8 *)module->func_ptrs
  4789. + sizeof(void *) * module->function_count);
  4790. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4791. if (!module->comp_data) {
  4792. aot_last_error = aot_get_last_error();
  4793. bh_assert(aot_last_error != NULL);
  4794. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4795. return false;
  4796. }
  4797. option.is_jit_mode = true;
  4798. option.opt_level = llvm_jit_options->opt_level;
  4799. option.size_level = llvm_jit_options->size_level;
  4800. option.segue_flags = llvm_jit_options->segue_flags;
  4801. option.quick_invoke_c_api_import =
  4802. llvm_jit_options->quick_invoke_c_api_import;
  4803. #if WASM_ENABLE_BULK_MEMORY != 0
  4804. option.enable_bulk_memory = true;
  4805. #endif
  4806. #if WASM_ENABLE_THREAD_MGR != 0
  4807. option.enable_thread_mgr = true;
  4808. #endif
  4809. #if WASM_ENABLE_TAIL_CALL != 0
  4810. option.enable_tail_call = true;
  4811. #endif
  4812. #if WASM_ENABLE_SIMD != 0
  4813. option.enable_simd = true;
  4814. #endif
  4815. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4816. option.enable_ref_types = true;
  4817. #elif WASM_ENABLE_GC != 0
  4818. option.enable_gc = true;
  4819. #endif
  4820. option.enable_aux_stack_check = true;
  4821. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4822. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4823. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  4824. aot_call_stack_features_init_default(&option.call_stack_features);
  4825. #endif
  4826. #if WASM_ENABLE_PERF_PROFILING != 0
  4827. option.enable_perf_profiling = true;
  4828. #endif
  4829. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4830. option.enable_memory_profiling = true;
  4831. option.enable_stack_estimation = true;
  4832. #endif
  4833. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4834. if (!module->comp_ctx) {
  4835. aot_last_error = aot_get_last_error();
  4836. bh_assert(aot_last_error != NULL);
  4837. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4838. return false;
  4839. }
  4840. return true;
  4841. }
  4842. static bool
  4843. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4844. uint32 error_buf_size)
  4845. {
  4846. char *aot_last_error;
  4847. uint32 i;
  4848. if (module->function_count == 0)
  4849. return true;
  4850. if (!aot_compile_wasm(module->comp_ctx)) {
  4851. aot_last_error = aot_get_last_error();
  4852. bh_assert(aot_last_error != NULL);
  4853. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4854. return false;
  4855. }
  4856. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4857. if (module->orcjit_stop_compiling)
  4858. return false;
  4859. #endif
  4860. bh_print_time("Begin to lookup llvm jit functions");
  4861. for (i = 0; i < module->function_count; i++) {
  4862. LLVMOrcJITTargetAddress func_addr = 0;
  4863. LLVMErrorRef error;
  4864. char func_name[48];
  4865. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4866. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4867. func_name);
  4868. if (error != LLVMErrorSuccess) {
  4869. char *err_msg = LLVMGetErrorMessage(error);
  4870. set_error_buf_v(error_buf, error_buf_size,
  4871. "failed to compile llvm jit function: %s", err_msg);
  4872. LLVMDisposeErrorMessage(err_msg);
  4873. return false;
  4874. }
  4875. /**
  4876. * No need to lock the func_ptr[func_idx] here as it is basic
  4877. * data type, the load/store for it can be finished by one cpu
  4878. * instruction, and there can be only one cpu instruction
  4879. * loading/storing at the same time.
  4880. */
  4881. module->func_ptrs[i] = (void *)func_addr;
  4882. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4883. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4884. if (module->orcjit_stop_compiling)
  4885. return false;
  4886. #endif
  4887. }
  4888. bh_print_time("End lookup llvm jit functions");
  4889. return true;
  4890. }
  4891. #endif /* end of WASM_ENABLE_JIT != 0 */
  4892. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4893. && WASM_ENABLE_LAZY_JIT != 0
  4894. static void *
  4895. init_llvm_jit_functions_stage2_callback(void *arg)
  4896. {
  4897. WASMModule *module = (WASMModule *)arg;
  4898. char error_buf[128];
  4899. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4900. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4901. module->orcjit_stop_compiling = true;
  4902. return NULL;
  4903. }
  4904. os_mutex_lock(&module->tierup_wait_lock);
  4905. module->llvm_jit_inited = true;
  4906. os_cond_broadcast(&module->tierup_wait_cond);
  4907. os_mutex_unlock(&module->tierup_wait_lock);
  4908. return NULL;
  4909. }
  4910. #endif
  4911. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4912. /* The callback function to compile jit functions */
  4913. static void *
  4914. orcjit_thread_callback(void *arg)
  4915. {
  4916. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4917. #if WASM_ENABLE_JIT != 0
  4918. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4919. #endif
  4920. WASMModule *module = thread_arg->module;
  4921. uint32 group_idx = thread_arg->group_idx;
  4922. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4923. uint32 func_count = module->function_count;
  4924. uint32 i;
  4925. #if WASM_ENABLE_FAST_JIT != 0
  4926. /* Compile fast jit functions of this group */
  4927. for (i = group_idx; i < func_count; i += group_stride) {
  4928. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4929. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4930. break;
  4931. }
  4932. if (module->orcjit_stop_compiling) {
  4933. return NULL;
  4934. }
  4935. }
  4936. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4937. os_mutex_lock(&module->tierup_wait_lock);
  4938. module->fast_jit_ready_groups++;
  4939. os_mutex_unlock(&module->tierup_wait_lock);
  4940. #endif
  4941. #endif
  4942. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4943. && WASM_ENABLE_LAZY_JIT != 0
  4944. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4945. for (i = group_idx; i < func_count;
  4946. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4947. uint32 j;
  4948. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4949. if (i + j * group_stride < func_count) {
  4950. if (!jit_compiler_set_call_to_fast_jit(
  4951. module,
  4952. i + j * group_stride + module->import_function_count)) {
  4953. LOG_ERROR(
  4954. "failed to compile call_to_fast_jit for func %u\n",
  4955. i + j * group_stride + module->import_function_count);
  4956. module->orcjit_stop_compiling = true;
  4957. return NULL;
  4958. }
  4959. }
  4960. if (module->orcjit_stop_compiling) {
  4961. return NULL;
  4962. }
  4963. }
  4964. }
  4965. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4966. fast jit functions are compiled */
  4967. os_mutex_lock(&module->tierup_wait_lock);
  4968. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4969. && module->fast_jit_ready_groups >= group_stride)) {
  4970. os_cond_reltimedwait(&module->tierup_wait_cond,
  4971. &module->tierup_wait_lock, 10000);
  4972. if (module->orcjit_stop_compiling) {
  4973. /* init_llvm_jit_functions_stage2 failed */
  4974. os_mutex_unlock(&module->tierup_wait_lock);
  4975. return NULL;
  4976. }
  4977. }
  4978. os_mutex_unlock(&module->tierup_wait_lock);
  4979. #endif
  4980. #if WASM_ENABLE_JIT != 0
  4981. /* Compile llvm jit functions of this group */
  4982. for (i = group_idx; i < func_count;
  4983. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4984. LLVMOrcJITTargetAddress func_addr = 0;
  4985. LLVMErrorRef error;
  4986. char func_name[48];
  4987. typedef void (*F)(void);
  4988. union {
  4989. F f;
  4990. void *v;
  4991. } u;
  4992. uint32 j;
  4993. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  4994. "_wrapper");
  4995. LOG_DEBUG("compile llvm jit func %s", func_name);
  4996. error =
  4997. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  4998. if (error != LLVMErrorSuccess) {
  4999. char *err_msg = LLVMGetErrorMessage(error);
  5000. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  5001. LLVMDisposeErrorMessage(err_msg);
  5002. break;
  5003. }
  5004. /* Call the jit wrapper function to trigger its compilation, so as
  5005. to compile the actual jit functions, since we add the latter to
  5006. function list in the PartitionFunction callback */
  5007. u.v = (void *)func_addr;
  5008. u.f();
  5009. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5010. if (i + j * group_stride < func_count) {
  5011. module->func_ptrs_compiled[i + j * group_stride] = true;
  5012. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5013. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5014. i + j * group_stride);
  5015. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5016. func_name);
  5017. if (error != LLVMErrorSuccess) {
  5018. char *err_msg = LLVMGetErrorMessage(error);
  5019. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5020. err_msg);
  5021. LLVMDisposeErrorMessage(err_msg);
  5022. /* Ignore current llvm jit func, as its func ptr is
  5023. previous set to call_to_fast_jit, which also works */
  5024. continue;
  5025. }
  5026. jit_compiler_set_llvm_jit_func_ptr(
  5027. module,
  5028. i + j * group_stride + module->import_function_count,
  5029. (void *)func_addr);
  5030. /* Try to switch to call this llvm jit function instead of
  5031. fast jit function from fast jit jitted code */
  5032. jit_compiler_set_call_to_llvm_jit(
  5033. module,
  5034. i + j * group_stride + module->import_function_count);
  5035. #endif
  5036. }
  5037. }
  5038. if (module->orcjit_stop_compiling) {
  5039. break;
  5040. }
  5041. }
  5042. #endif
  5043. return NULL;
  5044. }
  5045. static void
  5046. orcjit_stop_compile_threads(WASMModule *module)
  5047. {
  5048. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5049. / sizeof(OrcJitThreadArg));
  5050. module->orcjit_stop_compiling = true;
  5051. for (i = 0; i < thread_num; i++) {
  5052. if (module->orcjit_threads[i])
  5053. os_thread_join(module->orcjit_threads[i], NULL);
  5054. }
  5055. }
  5056. static bool
  5057. compile_jit_functions(WASMModule *module, char *error_buf,
  5058. uint32 error_buf_size)
  5059. {
  5060. uint32 thread_num =
  5061. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5062. uint32 i, j;
  5063. bh_print_time("Begin to compile jit functions");
  5064. /* Create threads to compile the jit functions */
  5065. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5066. #if WASM_ENABLE_JIT != 0
  5067. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5068. #endif
  5069. module->orcjit_thread_args[i].module = module;
  5070. module->orcjit_thread_args[i].group_idx = i;
  5071. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5072. (void *)&module->orcjit_thread_args[i],
  5073. APP_THREAD_STACK_SIZE_DEFAULT)
  5074. != 0) {
  5075. set_error_buf(error_buf, error_buf_size,
  5076. "create orcjit compile thread failed");
  5077. /* Terminate the threads created */
  5078. module->orcjit_stop_compiling = true;
  5079. for (j = 0; j < i; j++) {
  5080. os_thread_join(module->orcjit_threads[j], NULL);
  5081. }
  5082. return false;
  5083. }
  5084. }
  5085. #if WASM_ENABLE_LAZY_JIT == 0
  5086. /* Wait until all jit functions are compiled for eager mode */
  5087. for (i = 0; i < thread_num; i++) {
  5088. if (module->orcjit_threads[i])
  5089. os_thread_join(module->orcjit_threads[i], NULL);
  5090. }
  5091. #if WASM_ENABLE_FAST_JIT != 0
  5092. /* Ensure all the fast-jit functions are compiled */
  5093. for (i = 0; i < module->function_count; i++) {
  5094. if (!jit_compiler_is_compiled(module,
  5095. i + module->import_function_count)) {
  5096. set_error_buf(error_buf, error_buf_size,
  5097. "failed to compile fast jit function");
  5098. return false;
  5099. }
  5100. }
  5101. #endif
  5102. #if WASM_ENABLE_JIT != 0
  5103. /* Ensure all the llvm-jit functions are compiled */
  5104. for (i = 0; i < module->function_count; i++) {
  5105. if (!module->func_ptrs_compiled[i]) {
  5106. set_error_buf(error_buf, error_buf_size,
  5107. "failed to compile llvm jit function");
  5108. return false;
  5109. }
  5110. }
  5111. #endif
  5112. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5113. bh_print_time("End compile jit functions");
  5114. return true;
  5115. }
  5116. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5117. static bool
  5118. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5119. uint32 cur_func_idx, char *error_buf,
  5120. uint32 error_buf_size);
  5121. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5122. void **
  5123. wasm_interp_get_handle_table(void);
  5124. static void **handle_table;
  5125. #endif
  5126. static bool
  5127. load_from_sections(WASMModule *module, WASMSection *sections,
  5128. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5129. char *error_buf, uint32 error_buf_size)
  5130. {
  5131. WASMExport *export;
  5132. WASMSection *section = sections;
  5133. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5134. *buf_func = NULL, *buf_func_end = NULL;
  5135. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5136. WASMGlobal *aux_stack_top_global = NULL, *global;
  5137. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5138. aux_stack_top = (uint64)-1LL;
  5139. uint32 global_index, func_index, i;
  5140. uint32 aux_data_end_global_index = (uint32)-1;
  5141. uint32 aux_heap_base_global_index = (uint32)-1;
  5142. WASMFuncType *func_type;
  5143. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5144. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5145. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5146. #if WASM_ENABLE_BULK_MEMORY != 0
  5147. bool has_datacount_section = false;
  5148. #endif
  5149. /* Find code and function sections if have */
  5150. while (section) {
  5151. if (section->section_type == SECTION_TYPE_CODE) {
  5152. buf_code = section->section_body;
  5153. buf_code_end = buf_code + section->section_body_size;
  5154. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5155. module->buf_code = (uint8 *)buf_code;
  5156. module->buf_code_size = section->section_body_size;
  5157. #endif
  5158. }
  5159. else if (section->section_type == SECTION_TYPE_FUNC) {
  5160. buf_func = section->section_body;
  5161. buf_func_end = buf_func + section->section_body_size;
  5162. }
  5163. section = section->next;
  5164. }
  5165. section = sections;
  5166. while (section) {
  5167. buf = section->section_body;
  5168. buf_end = buf + section->section_body_size;
  5169. switch (section->section_type) {
  5170. case SECTION_TYPE_USER:
  5171. /* unsupported user section, ignore it. */
  5172. if (!load_user_section(buf, buf_end, module,
  5173. reuse_const_strings, error_buf,
  5174. error_buf_size))
  5175. return false;
  5176. break;
  5177. case SECTION_TYPE_TYPE:
  5178. if (!load_type_section(buf, buf_end, module, error_buf,
  5179. error_buf_size))
  5180. return false;
  5181. break;
  5182. case SECTION_TYPE_IMPORT:
  5183. if (!load_import_section(buf, buf_end, module,
  5184. reuse_const_strings, error_buf,
  5185. error_buf_size))
  5186. return false;
  5187. break;
  5188. case SECTION_TYPE_FUNC:
  5189. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5190. module, error_buf, error_buf_size))
  5191. return false;
  5192. break;
  5193. case SECTION_TYPE_TABLE:
  5194. if (!load_table_section(buf, buf_end, module, error_buf,
  5195. error_buf_size))
  5196. return false;
  5197. break;
  5198. case SECTION_TYPE_MEMORY:
  5199. if (!load_memory_section(buf, buf_end, module, error_buf,
  5200. error_buf_size))
  5201. return false;
  5202. break;
  5203. #if WASM_ENABLE_TAGS != 0
  5204. case SECTION_TYPE_TAG:
  5205. /* load tag declaration section */
  5206. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5207. module, error_buf, error_buf_size))
  5208. return false;
  5209. break;
  5210. #endif
  5211. case SECTION_TYPE_GLOBAL:
  5212. if (!load_global_section(buf, buf_end, module, error_buf,
  5213. error_buf_size))
  5214. return false;
  5215. break;
  5216. case SECTION_TYPE_EXPORT:
  5217. if (!load_export_section(buf, buf_end, module,
  5218. reuse_const_strings, error_buf,
  5219. error_buf_size))
  5220. return false;
  5221. break;
  5222. case SECTION_TYPE_START:
  5223. if (!load_start_section(buf, buf_end, module, error_buf,
  5224. error_buf_size))
  5225. return false;
  5226. break;
  5227. case SECTION_TYPE_ELEM:
  5228. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5229. error_buf_size))
  5230. return false;
  5231. break;
  5232. case SECTION_TYPE_CODE:
  5233. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5234. module, error_buf, error_buf_size))
  5235. return false;
  5236. break;
  5237. case SECTION_TYPE_DATA:
  5238. if (!load_data_segment_section(buf, buf_end, module,
  5239. #if WASM_ENABLE_BULK_MEMORY != 0
  5240. has_datacount_section,
  5241. #endif
  5242. clone_data_seg, error_buf,
  5243. error_buf_size))
  5244. return false;
  5245. break;
  5246. #if WASM_ENABLE_BULK_MEMORY != 0
  5247. case SECTION_TYPE_DATACOUNT:
  5248. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5249. error_buf_size))
  5250. return false;
  5251. has_datacount_section = true;
  5252. break;
  5253. #endif
  5254. #if WASM_ENABLE_STRINGREF != 0
  5255. case SECTION_TYPE_STRINGREF:
  5256. if (!load_stringref_section(buf, buf_end, module,
  5257. reuse_const_strings, error_buf,
  5258. error_buf_size))
  5259. return false;
  5260. break;
  5261. #endif
  5262. default:
  5263. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5264. return false;
  5265. }
  5266. section = section->next;
  5267. }
  5268. module->aux_data_end_global_index = (uint32)-1;
  5269. module->aux_heap_base_global_index = (uint32)-1;
  5270. module->aux_stack_top_global_index = (uint32)-1;
  5271. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5272. export = module->exports;
  5273. for (i = 0; i < module->export_count; i++, export ++) {
  5274. if (export->kind == EXPORT_KIND_GLOBAL) {
  5275. if (!strcmp(export->name, "__heap_base")) {
  5276. global_index = export->index - module->import_global_count;
  5277. global = module->globals + global_index;
  5278. if (global->type.val_type == VALUE_TYPE_I32
  5279. && !global->type.is_mutable
  5280. && global->init_expr.init_expr_type
  5281. == INIT_EXPR_TYPE_I32_CONST) {
  5282. aux_heap_base_global = global;
  5283. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5284. aux_heap_base_global_index = export->index;
  5285. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5286. aux_heap_base);
  5287. }
  5288. }
  5289. else if (!strcmp(export->name, "__data_end")) {
  5290. global_index = export->index - module->import_global_count;
  5291. global = module->globals + global_index;
  5292. if (global->type.val_type == VALUE_TYPE_I32
  5293. && !global->type.is_mutable
  5294. && global->init_expr.init_expr_type
  5295. == INIT_EXPR_TYPE_I32_CONST) {
  5296. aux_data_end_global = global;
  5297. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5298. aux_data_end_global_index = export->index;
  5299. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5300. aux_data_end);
  5301. aux_data_end = align_uint64(aux_data_end, 16);
  5302. }
  5303. }
  5304. /* For module compiled with -pthread option, the global is:
  5305. [0] stack_top <-- 0
  5306. [1] tls_pointer
  5307. [2] tls_size
  5308. [3] data_end <-- 3
  5309. [4] global_base
  5310. [5] heap_base <-- 5
  5311. [6] dso_handle
  5312. For module compiled without -pthread option:
  5313. [0] stack_top <-- 0
  5314. [1] data_end <-- 1
  5315. [2] global_base
  5316. [3] heap_base <-- 3
  5317. [4] dso_handle
  5318. */
  5319. if (aux_data_end_global && aux_heap_base_global
  5320. && aux_data_end <= aux_heap_base) {
  5321. module->aux_data_end_global_index = aux_data_end_global_index;
  5322. module->aux_data_end = aux_data_end;
  5323. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5324. module->aux_heap_base = aux_heap_base;
  5325. /* Resolve aux stack top global */
  5326. for (global_index = 0; global_index < module->global_count;
  5327. global_index++) {
  5328. global = module->globals + global_index;
  5329. if (global->type.is_mutable /* heap_base and data_end is
  5330. not mutable */
  5331. && global->type.val_type == VALUE_TYPE_I32
  5332. && global->init_expr.init_expr_type
  5333. == INIT_EXPR_TYPE_I32_CONST
  5334. && (uint64)(uint32)global->init_expr.u.i32
  5335. <= aux_heap_base) {
  5336. aux_stack_top_global = global;
  5337. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5338. module->aux_stack_top_global_index =
  5339. module->import_global_count + global_index;
  5340. module->aux_stack_bottom = aux_stack_top;
  5341. module->aux_stack_size =
  5342. aux_stack_top > aux_data_end
  5343. ? (uint32)(aux_stack_top - aux_data_end)
  5344. : (uint32)aux_stack_top;
  5345. LOG_VERBOSE(
  5346. "Found aux stack top global, value: %" PRIu64 ", "
  5347. "global index: %d, stack size: %d",
  5348. aux_stack_top, global_index,
  5349. module->aux_stack_size);
  5350. break;
  5351. }
  5352. }
  5353. if (!aux_stack_top_global) {
  5354. /* Auxiliary stack global isn't found, it must be unused
  5355. in the wasm app, as if it is used, the global must be
  5356. defined. Here we set it to __heap_base global and set
  5357. its size to 0. */
  5358. aux_stack_top_global = aux_heap_base_global;
  5359. aux_stack_top = aux_heap_base;
  5360. module->aux_stack_top_global_index =
  5361. module->aux_heap_base_global_index;
  5362. module->aux_stack_bottom = aux_stack_top;
  5363. module->aux_stack_size = 0;
  5364. }
  5365. break;
  5366. }
  5367. }
  5368. }
  5369. module->malloc_function = (uint32)-1;
  5370. module->free_function = (uint32)-1;
  5371. module->retain_function = (uint32)-1;
  5372. /* Resolve malloc/free function exported by wasm module */
  5373. #if WASM_ENABLE_MEMORY64 != 0
  5374. if (has_module_memory64(module))
  5375. malloc_free_io_type = VALUE_TYPE_I64;
  5376. #endif
  5377. export = module->exports;
  5378. for (i = 0; i < module->export_count; i++, export ++) {
  5379. if (export->kind == EXPORT_KIND_FUNC) {
  5380. if (!strcmp(export->name, "malloc")
  5381. && export->index >= module->import_function_count) {
  5382. func_index = export->index - module->import_function_count;
  5383. func_type = module->functions[func_index]->func_type;
  5384. if (func_type->param_count == 1 && func_type->result_count == 1
  5385. && func_type->types[0] == malloc_free_io_type
  5386. && func_type->types[1] == malloc_free_io_type) {
  5387. bh_assert(module->malloc_function == (uint32)-1);
  5388. module->malloc_function = export->index;
  5389. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5390. export->name, export->index);
  5391. }
  5392. }
  5393. else if (!strcmp(export->name, "__new")
  5394. && export->index >= module->import_function_count) {
  5395. /* __new && __pin for AssemblyScript */
  5396. func_index = export->index - module->import_function_count;
  5397. func_type = module->functions[func_index]->func_type;
  5398. if (func_type->param_count == 2 && func_type->result_count == 1
  5399. && func_type->types[0] == malloc_free_io_type
  5400. && func_type->types[1] == VALUE_TYPE_I32
  5401. && func_type->types[2] == malloc_free_io_type) {
  5402. uint32 j;
  5403. WASMExport *export_tmp;
  5404. bh_assert(module->malloc_function == (uint32)-1);
  5405. module->malloc_function = export->index;
  5406. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5407. export->name, export->index);
  5408. /* resolve retain function.
  5409. If not found, reset malloc function index */
  5410. export_tmp = module->exports;
  5411. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5412. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5413. && (!strcmp(export_tmp->name, "__retain")
  5414. || (!strcmp(export_tmp->name, "__pin")))
  5415. && (export_tmp->index
  5416. >= module->import_function_count)) {
  5417. func_index = export_tmp->index
  5418. - module->import_function_count;
  5419. func_type =
  5420. module->functions[func_index]->func_type;
  5421. if (func_type->param_count == 1
  5422. && func_type->result_count == 1
  5423. && func_type->types[0] == malloc_free_io_type
  5424. && func_type->types[1] == malloc_free_io_type) {
  5425. bh_assert(module->retain_function
  5426. == (uint32)-1);
  5427. module->retain_function = export_tmp->index;
  5428. LOG_VERBOSE("Found retain function, name: %s, "
  5429. "index: %u",
  5430. export_tmp->name,
  5431. export_tmp->index);
  5432. break;
  5433. }
  5434. }
  5435. }
  5436. if (j == module->export_count) {
  5437. module->malloc_function = (uint32)-1;
  5438. LOG_VERBOSE("Can't find retain function,"
  5439. "reset malloc function index to -1");
  5440. }
  5441. }
  5442. }
  5443. else if (((!strcmp(export->name, "free"))
  5444. || (!strcmp(export->name, "__release"))
  5445. || (!strcmp(export->name, "__unpin")))
  5446. && export->index >= module->import_function_count) {
  5447. func_index = export->index - module->import_function_count;
  5448. func_type = module->functions[func_index]->func_type;
  5449. if (func_type->param_count == 1 && func_type->result_count == 0
  5450. && func_type->types[0] == malloc_free_io_type) {
  5451. bh_assert(module->free_function == (uint32)-1);
  5452. module->free_function = export->index;
  5453. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5454. export->name, export->index);
  5455. }
  5456. }
  5457. }
  5458. }
  5459. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5460. handle_table = wasm_interp_get_handle_table();
  5461. #endif
  5462. for (i = 0; i < module->function_count; i++) {
  5463. WASMFunction *func = module->functions[i];
  5464. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5465. error_buf_size)) {
  5466. return false;
  5467. }
  5468. if (i == module->function_count - 1
  5469. && func->code + func->code_size != buf_code_end) {
  5470. set_error_buf(error_buf, error_buf_size,
  5471. "code section size mismatch");
  5472. return false;
  5473. }
  5474. }
  5475. if (!module->possible_memory_grow) {
  5476. WASMMemoryImport *memory_import;
  5477. WASMMemory *memory;
  5478. if (aux_data_end_global && aux_heap_base_global
  5479. && aux_stack_top_global) {
  5480. uint64 init_memory_size;
  5481. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5482. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5483. * valid range of uint32 */
  5484. if (shrunk_memory_size <= UINT32_MAX) {
  5485. if (module->import_memory_count) {
  5486. memory_import = &module->import_memories[0].u.memory;
  5487. init_memory_size =
  5488. (uint64)memory_import->mem_type.num_bytes_per_page
  5489. * memory_import->mem_type.init_page_count;
  5490. if (shrunk_memory_size <= init_memory_size) {
  5491. /* Reset memory info to decrease memory usage */
  5492. memory_import->mem_type.num_bytes_per_page =
  5493. (uint32)shrunk_memory_size;
  5494. memory_import->mem_type.init_page_count = 1;
  5495. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5496. shrunk_memory_size);
  5497. }
  5498. }
  5499. if (module->memory_count) {
  5500. memory = &module->memories[0];
  5501. init_memory_size = (uint64)memory->num_bytes_per_page
  5502. * memory->init_page_count;
  5503. if (shrunk_memory_size <= init_memory_size) {
  5504. /* Reset memory info to decrease memory usage */
  5505. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5506. memory->init_page_count = 1;
  5507. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5508. shrunk_memory_size);
  5509. }
  5510. }
  5511. }
  5512. }
  5513. #if WASM_ENABLE_MULTI_MODULE == 0
  5514. if (module->import_memory_count) {
  5515. memory_import = &module->import_memories[0].u.memory;
  5516. /* Only resize the memory to one big page if num_bytes_per_page is
  5517. * in valid range of uint32 */
  5518. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5519. memory_import->mem_type.num_bytes_per_page *=
  5520. memory_import->mem_type.init_page_count;
  5521. if (memory_import->mem_type.init_page_count > 0)
  5522. memory_import->mem_type.init_page_count =
  5523. memory_import->mem_type.max_page_count = 1;
  5524. else
  5525. memory_import->mem_type.init_page_count =
  5526. memory_import->mem_type.max_page_count = 0;
  5527. }
  5528. }
  5529. if (module->memory_count) {
  5530. memory = &module->memories[0];
  5531. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5532. * valid range of uint32 */
  5533. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5534. memory->num_bytes_per_page *= memory->init_page_count;
  5535. if (memory->init_page_count > 0)
  5536. memory->init_page_count = memory->max_page_count = 1;
  5537. else
  5538. memory->init_page_count = memory->max_page_count = 0;
  5539. }
  5540. }
  5541. #endif
  5542. }
  5543. calculate_global_data_offset(module);
  5544. #if WASM_ENABLE_FAST_JIT != 0
  5545. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5546. return false;
  5547. }
  5548. #endif
  5549. #if WASM_ENABLE_JIT != 0
  5550. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5551. return false;
  5552. }
  5553. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5554. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5555. return false;
  5556. }
  5557. #else
  5558. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5559. thread fast jit execution, since applying llvm optimizations in
  5560. aot_compile_wasm may cost a lot of time.
  5561. Create thread with enough native stack to apply llvm optimizations */
  5562. if (os_thread_create(&module->llvm_jit_init_thread,
  5563. init_llvm_jit_functions_stage2_callback,
  5564. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5565. != 0) {
  5566. set_error_buf(error_buf, error_buf_size,
  5567. "create orcjit compile thread failed");
  5568. return false;
  5569. }
  5570. #endif
  5571. #endif
  5572. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5573. /* Create threads to compile the jit functions */
  5574. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5575. return false;
  5576. }
  5577. #endif
  5578. #if WASM_ENABLE_MEMORY_TRACING != 0
  5579. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5580. #endif
  5581. return true;
  5582. }
  5583. static WASMModule *
  5584. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5585. {
  5586. WASMModule *module =
  5587. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5588. bh_list_status ret;
  5589. if (!module) {
  5590. return NULL;
  5591. }
  5592. module->module_type = Wasm_Module_Bytecode;
  5593. /* Set start_function to -1, means no start function */
  5594. module->start_function = (uint32)-1;
  5595. module->name = name;
  5596. module->is_binary_freeable = false;
  5597. #if WASM_ENABLE_FAST_INTERP == 0
  5598. module->br_table_cache_list = &module->br_table_cache_list_head;
  5599. ret = bh_list_init(module->br_table_cache_list);
  5600. bh_assert(ret == BH_LIST_SUCCESS);
  5601. #endif
  5602. #if WASM_ENABLE_MULTI_MODULE != 0
  5603. module->import_module_list = &module->import_module_list_head;
  5604. ret = bh_list_init(module->import_module_list);
  5605. bh_assert(ret == BH_LIST_SUCCESS);
  5606. #endif
  5607. #if WASM_ENABLE_DEBUG_INTERP != 0
  5608. ret = bh_list_init(&module->fast_opcode_list);
  5609. bh_assert(ret == BH_LIST_SUCCESS);
  5610. #endif
  5611. #if WASM_ENABLE_GC != 0
  5612. if (!(module->ref_type_set =
  5613. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5614. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5615. goto fail1;
  5616. }
  5617. if (os_mutex_init(&module->rtt_type_lock)) {
  5618. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5619. goto fail2;
  5620. }
  5621. #endif
  5622. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5623. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5624. && WASM_ENABLE_LAZY_JIT != 0)
  5625. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5626. set_error_buf(error_buf, error_buf_size,
  5627. "init instance list lock failed");
  5628. goto fail3;
  5629. }
  5630. #endif
  5631. (void)ret;
  5632. return module;
  5633. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5634. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5635. && WASM_ENABLE_LAZY_JIT != 0)
  5636. fail3:
  5637. #endif
  5638. #if WASM_ENABLE_GC != 0
  5639. os_mutex_destroy(&module->rtt_type_lock);
  5640. fail2:
  5641. bh_hash_map_destroy(module->ref_type_set);
  5642. fail1:
  5643. #endif
  5644. wasm_runtime_free(module);
  5645. return NULL;
  5646. }
  5647. #if WASM_ENABLE_DEBUG_INTERP != 0
  5648. static bool
  5649. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5650. uint32 error_buf_size)
  5651. {
  5652. WASMFastOPCodeNode *fast_op =
  5653. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5654. if (fast_op) {
  5655. fast_op->offset = pos - module->load_addr;
  5656. fast_op->orig_op = orig_op;
  5657. bh_list_insert(&module->fast_opcode_list, fast_op);
  5658. }
  5659. return fast_op ? true : false;
  5660. }
  5661. #endif
  5662. WASMModule *
  5663. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5664. uint32 error_buf_size)
  5665. {
  5666. WASMModule *module = create_module("", error_buf, error_buf_size);
  5667. if (!module)
  5668. return NULL;
  5669. if (!load_from_sections(module, section_list, false, true, error_buf,
  5670. error_buf_size)) {
  5671. wasm_loader_unload(module);
  5672. return NULL;
  5673. }
  5674. LOG_VERBOSE("Load module from sections success.\n");
  5675. return module;
  5676. }
  5677. static void
  5678. destroy_sections(WASMSection *section_list)
  5679. {
  5680. WASMSection *section = section_list, *next;
  5681. while (section) {
  5682. next = section->next;
  5683. wasm_runtime_free(section);
  5684. section = next;
  5685. }
  5686. }
  5687. /* clang-format off */
  5688. static uint8 section_ids[] = {
  5689. SECTION_TYPE_USER,
  5690. SECTION_TYPE_TYPE,
  5691. SECTION_TYPE_IMPORT,
  5692. SECTION_TYPE_FUNC,
  5693. SECTION_TYPE_TABLE,
  5694. SECTION_TYPE_MEMORY,
  5695. #if WASM_ENABLE_TAGS != 0
  5696. SECTION_TYPE_TAG,
  5697. #endif
  5698. #if WASM_ENABLE_STRINGREF != 0
  5699. /* must immediately precede the global section,
  5700. or where the global section would be */
  5701. SECTION_TYPE_STRINGREF,
  5702. #endif
  5703. SECTION_TYPE_GLOBAL,
  5704. SECTION_TYPE_EXPORT,
  5705. SECTION_TYPE_START,
  5706. SECTION_TYPE_ELEM,
  5707. #if WASM_ENABLE_BULK_MEMORY != 0
  5708. SECTION_TYPE_DATACOUNT,
  5709. #endif
  5710. SECTION_TYPE_CODE,
  5711. SECTION_TYPE_DATA
  5712. };
  5713. /* clang-format on */
  5714. static uint8
  5715. get_section_index(uint8 section_type)
  5716. {
  5717. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5718. for (uint8 i = 0; i < max_id; i++) {
  5719. if (section_type == section_ids[i])
  5720. return i;
  5721. }
  5722. return (uint8)-1;
  5723. }
  5724. static bool
  5725. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5726. char *error_buf, uint32 error_buf_size)
  5727. {
  5728. WASMSection *section_list_end = NULL, *section;
  5729. const uint8 *p = buf, *p_end = buf + size;
  5730. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5731. uint32 section_size;
  5732. bh_assert(!*p_section_list);
  5733. p += 8;
  5734. while (p < p_end) {
  5735. CHECK_BUF(p, p_end, 1);
  5736. section_type = read_uint8(p);
  5737. section_index = get_section_index(section_type);
  5738. if (section_index != (uint8)-1) {
  5739. if (section_type != SECTION_TYPE_USER) {
  5740. /* Custom sections may be inserted at any place,
  5741. while other sections must occur at most once
  5742. and in prescribed order. */
  5743. if (last_section_index != (uint8)-1
  5744. && (section_index <= last_section_index)) {
  5745. set_error_buf(error_buf, error_buf_size,
  5746. "unexpected content after last section or "
  5747. "junk after last section");
  5748. return false;
  5749. }
  5750. last_section_index = section_index;
  5751. }
  5752. read_leb_uint32(p, p_end, section_size);
  5753. CHECK_BUF1(p, p_end, section_size);
  5754. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5755. error_buf_size))) {
  5756. return false;
  5757. }
  5758. section->section_type = section_type;
  5759. section->section_body = (uint8 *)p;
  5760. section->section_body_size = section_size;
  5761. if (!section_list_end)
  5762. *p_section_list = section_list_end = section;
  5763. else {
  5764. section_list_end->next = section;
  5765. section_list_end = section;
  5766. }
  5767. p += section_size;
  5768. }
  5769. else {
  5770. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5771. return false;
  5772. }
  5773. }
  5774. return true;
  5775. fail:
  5776. return false;
  5777. }
  5778. static void
  5779. exchange32(uint8 *p_data)
  5780. {
  5781. uint8 value = *p_data;
  5782. *p_data = *(p_data + 3);
  5783. *(p_data + 3) = value;
  5784. value = *(p_data + 1);
  5785. *(p_data + 1) = *(p_data + 2);
  5786. *(p_data + 2) = value;
  5787. }
  5788. static union {
  5789. int a;
  5790. char b;
  5791. } __ue = { .a = 1 };
  5792. #define is_little_endian() (__ue.b == 1)
  5793. static bool
  5794. load(const uint8 *buf, uint32 size, WASMModule *module,
  5795. bool wasm_binary_freeable, char *error_buf, uint32 error_buf_size)
  5796. {
  5797. const uint8 *buf_end = buf + size;
  5798. const uint8 *p = buf, *p_end = buf_end;
  5799. uint32 magic_number, version;
  5800. WASMSection *section_list = NULL;
  5801. CHECK_BUF1(p, p_end, sizeof(uint32));
  5802. magic_number = read_uint32(p);
  5803. if (!is_little_endian())
  5804. exchange32((uint8 *)&magic_number);
  5805. if (magic_number != WASM_MAGIC_NUMBER) {
  5806. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5807. return false;
  5808. }
  5809. CHECK_BUF1(p, p_end, sizeof(uint32));
  5810. version = read_uint32(p);
  5811. if (!is_little_endian())
  5812. exchange32((uint8 *)&version);
  5813. if (version != WASM_CURRENT_VERSION) {
  5814. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5815. return false;
  5816. }
  5817. module->package_version = version;
  5818. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5819. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  5820. error_buf, error_buf_size)) {
  5821. destroy_sections(section_list);
  5822. return false;
  5823. }
  5824. destroy_sections(section_list);
  5825. return true;
  5826. fail:
  5827. return false;
  5828. }
  5829. #if WASM_ENABLE_LIBC_WASI != 0
  5830. /**
  5831. * refer to
  5832. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5833. */
  5834. static bool
  5835. check_wasi_abi_compatibility(const WASMModule *module,
  5836. #if WASM_ENABLE_MULTI_MODULE != 0
  5837. bool main_module,
  5838. #endif
  5839. char *error_buf, uint32 error_buf_size)
  5840. {
  5841. /**
  5842. * be careful with:
  5843. * wasi compatible modules(command/reactor) which don't import any wasi
  5844. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5845. * reactor can depend on nothing. At the same time, each has its own entry
  5846. * point.
  5847. *
  5848. * observations:
  5849. * - clang always injects `_start` into a command
  5850. * - clang always injects `_initialize` into a reactor
  5851. * - `iwasm -f` allows to run a function in the reactor
  5852. *
  5853. * strong assumptions:
  5854. * - no one will define either `_start` or `_initialize` on purpose
  5855. * - `_start` should always be `void _start(void)`
  5856. * - `_initialize` should always be `void _initialize(void)`
  5857. *
  5858. */
  5859. /* clang-format off */
  5860. /**
  5861. *
  5862. * | | import_wasi_api True | | import_wasi_api False | |
  5863. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5864. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5865. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5866. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5867. */
  5868. /* clang-format on */
  5869. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5870. uint32 import_function_count = module->import_function_count;
  5871. WASMFuncType *func_type;
  5872. /* (func (export "_start") (...) */
  5873. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5874. error_buf, error_buf_size);
  5875. if (start) {
  5876. if (start->index < import_function_count) {
  5877. set_error_buf(
  5878. error_buf, error_buf_size,
  5879. "the builtin _start function can not be an import function");
  5880. return false;
  5881. }
  5882. func_type =
  5883. module->functions[start->index - import_function_count]->func_type;
  5884. if (func_type->param_count || func_type->result_count) {
  5885. set_error_buf(error_buf, error_buf_size,
  5886. "the signature of builtin _start function is wrong");
  5887. return false;
  5888. }
  5889. }
  5890. else {
  5891. /* (func (export "_initialize") (...) */
  5892. initialize =
  5893. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5894. error_buf, error_buf_size);
  5895. if (initialize) {
  5896. if (initialize->index < import_function_count) {
  5897. set_error_buf(error_buf, error_buf_size,
  5898. "the builtin _initialize function can not be an "
  5899. "import function");
  5900. return false;
  5901. }
  5902. func_type =
  5903. module->functions[initialize->index - import_function_count]
  5904. ->func_type;
  5905. if (func_type->param_count || func_type->result_count) {
  5906. set_error_buf(
  5907. error_buf, error_buf_size,
  5908. "the signature of builtin _initialize function is wrong");
  5909. return false;
  5910. }
  5911. }
  5912. }
  5913. /* filter out non-wasi compatible modules */
  5914. if (!module->import_wasi_api && !start && !initialize) {
  5915. return true;
  5916. }
  5917. /* should have one at least */
  5918. if (module->import_wasi_api && !start && !initialize) {
  5919. LOG_WARNING("warning: a module with WASI apis should be either "
  5920. "a command or a reactor");
  5921. }
  5922. /*
  5923. * there is at least one of `_start` and `_initialize` in below cases.
  5924. * according to the assumption, they should be all wasi compatible
  5925. */
  5926. #if WASM_ENABLE_MULTI_MODULE != 0
  5927. /* filter out commands (with `_start`) cases */
  5928. if (start && !main_module) {
  5929. set_error_buf(
  5930. error_buf, error_buf_size,
  5931. "a command (with _start function) can not be a sub-module");
  5932. return false;
  5933. }
  5934. #endif
  5935. /*
  5936. * it is ok a reactor acts as a main module,
  5937. * so skip the check about (with `_initialize`)
  5938. */
  5939. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5940. error_buf, error_buf_size);
  5941. if (!memory
  5942. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5943. /*
  5944. * with wasi-threads, it's still an open question if a memory
  5945. * should be exported.
  5946. *
  5947. * https://github.com/WebAssembly/wasi-threads/issues/22
  5948. * https://github.com/WebAssembly/WASI/issues/502
  5949. *
  5950. * Note: this code assumes the number of memories is at most 1.
  5951. */
  5952. && module->import_memory_count == 0
  5953. #endif
  5954. ) {
  5955. set_error_buf(error_buf, error_buf_size,
  5956. "a module with WASI apis must export memory by default");
  5957. return false;
  5958. }
  5959. return true;
  5960. }
  5961. #endif
  5962. WASMModule *
  5963. wasm_loader_load(uint8 *buf, uint32 size,
  5964. #if WASM_ENABLE_MULTI_MODULE != 0
  5965. bool main_module,
  5966. #endif
  5967. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  5968. {
  5969. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  5970. if (!module) {
  5971. return NULL;
  5972. }
  5973. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5974. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5975. module->load_addr = (uint8 *)buf;
  5976. module->load_size = size;
  5977. #endif
  5978. if (!load(buf, size, module, args->wasm_binary_freeable, error_buf,
  5979. error_buf_size)) {
  5980. goto fail;
  5981. }
  5982. #if WASM_ENABLE_LIBC_WASI != 0
  5983. /* Check the WASI application ABI */
  5984. if (!check_wasi_abi_compatibility(module,
  5985. #if WASM_ENABLE_MULTI_MODULE != 0
  5986. main_module,
  5987. #endif
  5988. error_buf, error_buf_size)) {
  5989. goto fail;
  5990. }
  5991. #endif
  5992. LOG_VERBOSE("Load module success.\n");
  5993. return module;
  5994. fail:
  5995. wasm_loader_unload(module);
  5996. return NULL;
  5997. }
  5998. void
  5999. wasm_loader_unload(WASMModule *module)
  6000. {
  6001. uint32 i;
  6002. if (!module)
  6003. return;
  6004. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6005. && WASM_ENABLE_LAZY_JIT != 0
  6006. module->orcjit_stop_compiling = true;
  6007. if (module->llvm_jit_init_thread)
  6008. os_thread_join(module->llvm_jit_init_thread, NULL);
  6009. #endif
  6010. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6011. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6012. module internal data after they were freed */
  6013. orcjit_stop_compile_threads(module);
  6014. #endif
  6015. #if WASM_ENABLE_JIT != 0
  6016. if (module->func_ptrs)
  6017. wasm_runtime_free(module->func_ptrs);
  6018. if (module->comp_ctx)
  6019. aot_destroy_comp_context(module->comp_ctx);
  6020. if (module->comp_data)
  6021. aot_destroy_comp_data(module->comp_data);
  6022. #endif
  6023. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6024. && WASM_ENABLE_LAZY_JIT != 0
  6025. if (module->tierup_wait_lock_inited) {
  6026. os_mutex_destroy(&module->tierup_wait_lock);
  6027. os_cond_destroy(&module->tierup_wait_cond);
  6028. }
  6029. #endif
  6030. if (module->imports)
  6031. wasm_runtime_free(module->imports);
  6032. if (module->functions) {
  6033. for (i = 0; i < module->function_count; i++) {
  6034. if (module->functions[i]) {
  6035. if (module->functions[i]->local_offsets)
  6036. wasm_runtime_free(module->functions[i]->local_offsets);
  6037. #if WASM_ENABLE_FAST_INTERP != 0
  6038. if (module->functions[i]->code_compiled)
  6039. wasm_runtime_free(module->functions[i]->code_compiled);
  6040. if (module->functions[i]->consts)
  6041. wasm_runtime_free(module->functions[i]->consts);
  6042. #endif
  6043. #if WASM_ENABLE_FAST_JIT != 0
  6044. if (module->functions[i]->fast_jit_jitted_code) {
  6045. jit_code_cache_free(
  6046. module->functions[i]->fast_jit_jitted_code);
  6047. }
  6048. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6049. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6050. jit_code_cache_free(
  6051. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6052. }
  6053. #endif
  6054. #endif
  6055. #if WASM_ENABLE_GC != 0
  6056. if (module->functions[i]->local_ref_type_maps) {
  6057. wasm_runtime_free(
  6058. module->functions[i]->local_ref_type_maps);
  6059. }
  6060. #endif
  6061. wasm_runtime_free(module->functions[i]);
  6062. }
  6063. }
  6064. wasm_runtime_free(module->functions);
  6065. }
  6066. if (module->tables) {
  6067. #if WASM_ENABLE_GC != 0
  6068. for (i = 0; i < module->table_count; i++) {
  6069. destroy_init_expr(module, &module->tables[i].init_expr);
  6070. }
  6071. #endif
  6072. wasm_runtime_free(module->tables);
  6073. }
  6074. if (module->memories)
  6075. wasm_runtime_free(module->memories);
  6076. if (module->globals) {
  6077. #if WASM_ENABLE_GC != 0
  6078. for (i = 0; i < module->global_count; i++) {
  6079. destroy_init_expr(module, &module->globals[i].init_expr);
  6080. }
  6081. #endif
  6082. wasm_runtime_free(module->globals);
  6083. }
  6084. #if WASM_ENABLE_TAGS != 0
  6085. if (module->tags) {
  6086. for (i = 0; i < module->tag_count; i++) {
  6087. if (module->tags[i])
  6088. wasm_runtime_free(module->tags[i]);
  6089. }
  6090. wasm_runtime_free(module->tags);
  6091. }
  6092. #endif
  6093. if (module->exports)
  6094. wasm_runtime_free(module->exports);
  6095. if (module->table_segments) {
  6096. for (i = 0; i < module->table_seg_count; i++) {
  6097. if (module->table_segments[i].init_values) {
  6098. #if WASM_ENABLE_GC != 0
  6099. uint32 j;
  6100. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6101. destroy_init_expr(
  6102. module, &module->table_segments[i].init_values[j]);
  6103. }
  6104. #endif
  6105. wasm_runtime_free(module->table_segments[i].init_values);
  6106. }
  6107. }
  6108. wasm_runtime_free(module->table_segments);
  6109. }
  6110. if (module->data_segments) {
  6111. for (i = 0; i < module->data_seg_count; i++) {
  6112. if (module->data_segments[i]) {
  6113. if (module->data_segments[i]->is_data_cloned)
  6114. wasm_runtime_free(module->data_segments[i]->data);
  6115. wasm_runtime_free(module->data_segments[i]);
  6116. }
  6117. }
  6118. wasm_runtime_free(module->data_segments);
  6119. }
  6120. if (module->types) {
  6121. for (i = 0; i < module->type_count; i++) {
  6122. if (module->types[i])
  6123. destroy_wasm_type(module->types[i]);
  6124. }
  6125. wasm_runtime_free(module->types);
  6126. }
  6127. if (module->const_str_list) {
  6128. StringNode *node = module->const_str_list, *node_next;
  6129. while (node) {
  6130. node_next = node->next;
  6131. wasm_runtime_free(node);
  6132. node = node_next;
  6133. }
  6134. }
  6135. #if WASM_ENABLE_STRINGREF != 0
  6136. if (module->string_literal_ptrs) {
  6137. wasm_runtime_free((void *)module->string_literal_ptrs);
  6138. }
  6139. if (module->string_literal_lengths) {
  6140. wasm_runtime_free(module->string_literal_lengths);
  6141. }
  6142. #endif
  6143. #if WASM_ENABLE_FAST_INTERP == 0
  6144. if (module->br_table_cache_list) {
  6145. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6146. BrTableCache *node_next;
  6147. while (node) {
  6148. node_next = bh_list_elem_next(node);
  6149. wasm_runtime_free(node);
  6150. node = node_next;
  6151. }
  6152. }
  6153. #endif
  6154. #if WASM_ENABLE_MULTI_MODULE != 0
  6155. /* just release the sub module list */
  6156. if (module->import_module_list) {
  6157. WASMRegisteredModule *node =
  6158. bh_list_first_elem(module->import_module_list);
  6159. while (node) {
  6160. WASMRegisteredModule *next = bh_list_elem_next(node);
  6161. bh_list_remove(module->import_module_list, node);
  6162. /*
  6163. * unload(sub_module) will be triggered during runtime_destroy().
  6164. * every module in the global module list will be unloaded one by
  6165. * one. so don't worry.
  6166. */
  6167. wasm_runtime_free(node);
  6168. /*
  6169. * the module file reading buffer will be released
  6170. * in runtime_destroy()
  6171. */
  6172. node = next;
  6173. }
  6174. }
  6175. #endif
  6176. #if WASM_ENABLE_DEBUG_INTERP != 0
  6177. WASMFastOPCodeNode *fast_opcode =
  6178. bh_list_first_elem(&module->fast_opcode_list);
  6179. while (fast_opcode) {
  6180. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6181. wasm_runtime_free(fast_opcode);
  6182. fast_opcode = next;
  6183. }
  6184. #endif
  6185. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6186. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6187. && WASM_ENABLE_LAZY_JIT != 0)
  6188. os_mutex_destroy(&module->instance_list_lock);
  6189. #endif
  6190. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6191. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6192. #endif
  6193. #if WASM_ENABLE_FAST_JIT != 0
  6194. if (module->fast_jit_func_ptrs) {
  6195. wasm_runtime_free(module->fast_jit_func_ptrs);
  6196. }
  6197. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6198. if (module->fast_jit_thread_locks_inited[i]) {
  6199. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6200. }
  6201. }
  6202. #endif
  6203. #if WASM_ENABLE_GC != 0
  6204. os_mutex_destroy(&module->rtt_type_lock);
  6205. bh_hash_map_destroy(module->ref_type_set);
  6206. if (module->rtt_types) {
  6207. for (i = 0; i < module->type_count; i++) {
  6208. if (module->rtt_types[i])
  6209. wasm_runtime_free(module->rtt_types[i]);
  6210. }
  6211. wasm_runtime_free(module->rtt_types);
  6212. }
  6213. #if WASM_ENABLE_STRINGREF != 0
  6214. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6215. if (module->stringref_rtts[i])
  6216. wasm_runtime_free(module->stringref_rtts[i]);
  6217. }
  6218. #endif
  6219. #endif
  6220. wasm_runtime_free(module);
  6221. }
  6222. bool
  6223. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6224. const uint8 *start_addr, const uint8 *code_end_addr,
  6225. uint8 label_type, uint8 **p_else_addr,
  6226. uint8 **p_end_addr)
  6227. {
  6228. const uint8 *p = start_addr, *p_end = code_end_addr;
  6229. uint8 *else_addr = NULL;
  6230. char error_buf[128];
  6231. uint32 block_nested_depth = 1, count, i, j, t;
  6232. uint32 error_buf_size = sizeof(error_buf);
  6233. uint8 opcode, u8;
  6234. BlockAddr block_stack[16] = { { 0 } }, *block;
  6235. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6236. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6237. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6238. if (block[j].start_addr == start_addr) {
  6239. /* Cache hit */
  6240. *p_else_addr = block[j].else_addr;
  6241. *p_end_addr = block[j].end_addr;
  6242. return true;
  6243. }
  6244. }
  6245. /* Cache unhit */
  6246. block_stack[0].start_addr = start_addr;
  6247. while (p < code_end_addr) {
  6248. opcode = *p++;
  6249. #if WASM_ENABLE_DEBUG_INTERP != 0
  6250. op_break_retry:
  6251. #endif
  6252. switch (opcode) {
  6253. case WASM_OP_UNREACHABLE:
  6254. case WASM_OP_NOP:
  6255. break;
  6256. #if WASM_ENABLE_EXCE_HANDLING != 0
  6257. case WASM_OP_TRY:
  6258. u8 = read_uint8(p);
  6259. if (block_nested_depth
  6260. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6261. block_stack[block_nested_depth].start_addr = p;
  6262. block_stack[block_nested_depth].else_addr = NULL;
  6263. }
  6264. block_nested_depth++;
  6265. break;
  6266. case EXT_OP_TRY:
  6267. skip_leb_uint32(p, p_end);
  6268. if (block_nested_depth
  6269. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6270. block_stack[block_nested_depth].start_addr = p;
  6271. block_stack[block_nested_depth].else_addr = NULL;
  6272. }
  6273. block_nested_depth++;
  6274. break;
  6275. case WASM_OP_CATCH:
  6276. if (block_nested_depth == 1) {
  6277. *p_end_addr = (uint8 *)(p - 1);
  6278. /* stop search and return the address of the catch block */
  6279. return true;
  6280. }
  6281. break;
  6282. case WASM_OP_CATCH_ALL:
  6283. if (block_nested_depth == 1) {
  6284. *p_end_addr = (uint8 *)(p - 1);
  6285. /* stop search and return the address of the catch_all block
  6286. */
  6287. return true;
  6288. }
  6289. break;
  6290. case WASM_OP_THROW:
  6291. /* skip tag_index */
  6292. skip_leb(p);
  6293. break;
  6294. case WASM_OP_RETHROW:
  6295. /* skip depth */
  6296. skip_leb(p);
  6297. break;
  6298. case WASM_OP_DELEGATE:
  6299. if (block_nested_depth == 1) {
  6300. *p_end_addr = (uint8 *)(p - 1);
  6301. return true;
  6302. }
  6303. else {
  6304. skip_leb(p);
  6305. /* the DELEGATE opcode ends the tryblock, */
  6306. block_nested_depth--;
  6307. if (block_nested_depth
  6308. < sizeof(block_stack) / sizeof(BlockAddr))
  6309. block_stack[block_nested_depth].end_addr =
  6310. (uint8 *)(p - 1);
  6311. }
  6312. break;
  6313. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6314. case WASM_OP_BLOCK:
  6315. case WASM_OP_LOOP:
  6316. case WASM_OP_IF:
  6317. {
  6318. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6319. u8 = read_uint8(p);
  6320. if (is_byte_a_type(u8)) {
  6321. #if WASM_ENABLE_GC != 0
  6322. if (wasm_is_type_multi_byte_type(u8)) {
  6323. /* the possible extra bytes of GC ref type have been
  6324. modified to OP_NOP, no need to resolve them again */
  6325. }
  6326. #endif
  6327. }
  6328. else {
  6329. p--;
  6330. /* block type */
  6331. skip_leb_int32(p, p_end);
  6332. }
  6333. if (block_nested_depth
  6334. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6335. block_stack[block_nested_depth].start_addr = p;
  6336. block_stack[block_nested_depth].else_addr = NULL;
  6337. }
  6338. block_nested_depth++;
  6339. break;
  6340. }
  6341. case EXT_OP_BLOCK:
  6342. case EXT_OP_LOOP:
  6343. case EXT_OP_IF:
  6344. /* block type */
  6345. skip_leb_int32(p, p_end);
  6346. if (block_nested_depth
  6347. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6348. block_stack[block_nested_depth].start_addr = p;
  6349. block_stack[block_nested_depth].else_addr = NULL;
  6350. }
  6351. block_nested_depth++;
  6352. break;
  6353. case WASM_OP_ELSE:
  6354. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6355. else_addr = (uint8 *)(p - 1);
  6356. if (block_nested_depth - 1
  6357. < sizeof(block_stack) / sizeof(BlockAddr))
  6358. block_stack[block_nested_depth - 1].else_addr =
  6359. (uint8 *)(p - 1);
  6360. break;
  6361. case WASM_OP_END:
  6362. if (block_nested_depth == 1) {
  6363. if (label_type == LABEL_TYPE_IF)
  6364. *p_else_addr = else_addr;
  6365. *p_end_addr = (uint8 *)(p - 1);
  6366. block_stack[0].end_addr = (uint8 *)(p - 1);
  6367. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6368. t++) {
  6369. start_addr = block_stack[t].start_addr;
  6370. if (start_addr) {
  6371. i = ((uintptr_t)start_addr)
  6372. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6373. block =
  6374. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6375. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6376. if (!block[j].start_addr)
  6377. break;
  6378. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6379. memmove(block + 1, block,
  6380. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6381. * sizeof(BlockAddr));
  6382. j = 0;
  6383. }
  6384. block[j].start_addr = block_stack[t].start_addr;
  6385. block[j].else_addr = block_stack[t].else_addr;
  6386. block[j].end_addr = block_stack[t].end_addr;
  6387. }
  6388. else
  6389. break;
  6390. }
  6391. return true;
  6392. }
  6393. else {
  6394. block_nested_depth--;
  6395. if (block_nested_depth
  6396. < sizeof(block_stack) / sizeof(BlockAddr))
  6397. block_stack[block_nested_depth].end_addr =
  6398. (uint8 *)(p - 1);
  6399. }
  6400. break;
  6401. case WASM_OP_BR:
  6402. case WASM_OP_BR_IF:
  6403. skip_leb_uint32(p, p_end); /* labelidx */
  6404. break;
  6405. case WASM_OP_BR_TABLE:
  6406. read_leb_uint32(p, p_end, count); /* label num */
  6407. #if WASM_ENABLE_FAST_INTERP != 0
  6408. for (i = 0; i <= count; i++) /* labelidxs */
  6409. skip_leb_uint32(p, p_end);
  6410. #else
  6411. p += count + 1;
  6412. while (*p == WASM_OP_NOP)
  6413. p++;
  6414. #endif
  6415. break;
  6416. #if WASM_ENABLE_FAST_INTERP == 0
  6417. case EXT_OP_BR_TABLE_CACHE:
  6418. read_leb_uint32(p, p_end, count); /* label num */
  6419. while (*p == WASM_OP_NOP)
  6420. p++;
  6421. break;
  6422. #endif
  6423. case WASM_OP_RETURN:
  6424. break;
  6425. case WASM_OP_CALL:
  6426. #if WASM_ENABLE_TAIL_CALL != 0
  6427. case WASM_OP_RETURN_CALL:
  6428. #endif
  6429. skip_leb_uint32(p, p_end); /* funcidx */
  6430. break;
  6431. case WASM_OP_CALL_INDIRECT:
  6432. #if WASM_ENABLE_TAIL_CALL != 0
  6433. case WASM_OP_RETURN_CALL_INDIRECT:
  6434. #endif
  6435. skip_leb_uint32(p, p_end); /* typeidx */
  6436. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6437. skip_leb_uint32(p, p_end); /* tableidx */
  6438. #else
  6439. u8 = read_uint8(p); /* 0x00 */
  6440. #endif
  6441. break;
  6442. #if WASM_ENABLE_GC != 0
  6443. case WASM_OP_CALL_REF:
  6444. case WASM_OP_RETURN_CALL_REF:
  6445. skip_leb_uint32(p, p_end); /* typeidx */
  6446. break;
  6447. #endif
  6448. case WASM_OP_DROP:
  6449. case WASM_OP_SELECT:
  6450. case WASM_OP_DROP_64:
  6451. case WASM_OP_SELECT_64:
  6452. break;
  6453. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6454. case WASM_OP_SELECT_T:
  6455. {
  6456. skip_leb_uint32(p, p_end); /* vec length */
  6457. u8 = read_uint8(p); /* typeidx */
  6458. /* the possible extra bytes of GC ref type have been
  6459. modified to OP_NOP, no need to resolve them again */
  6460. break;
  6461. }
  6462. case WASM_OP_TABLE_GET:
  6463. case WASM_OP_TABLE_SET:
  6464. skip_leb_uint32(p, p_end); /* table index */
  6465. break;
  6466. case WASM_OP_REF_NULL:
  6467. {
  6468. u8 = read_uint8(p); /* type */
  6469. if (is_byte_a_type(u8)) {
  6470. #if WASM_ENABLE_GC != 0
  6471. if (wasm_is_type_multi_byte_type(u8)) {
  6472. /* the possible extra bytes of GC ref type have been
  6473. modified to OP_NOP, no need to resolve them again */
  6474. }
  6475. #endif
  6476. }
  6477. else {
  6478. p--;
  6479. skip_leb_uint32(p, p_end);
  6480. }
  6481. break;
  6482. }
  6483. case WASM_OP_REF_IS_NULL:
  6484. break;
  6485. case WASM_OP_REF_FUNC:
  6486. skip_leb_uint32(p, p_end); /* func index */
  6487. break;
  6488. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6489. #if WASM_ENABLE_GC != 0
  6490. case WASM_OP_REF_AS_NON_NULL:
  6491. case WASM_OP_REF_EQ:
  6492. break;
  6493. case WASM_OP_BR_ON_NULL:
  6494. case WASM_OP_BR_ON_NON_NULL:
  6495. skip_leb_uint32(p, p_end); /* label index */
  6496. break;
  6497. #endif /* end of WASM_ENABLE_GC != 0 */
  6498. case WASM_OP_GET_LOCAL:
  6499. case WASM_OP_SET_LOCAL:
  6500. case WASM_OP_TEE_LOCAL:
  6501. case WASM_OP_GET_GLOBAL:
  6502. case WASM_OP_SET_GLOBAL:
  6503. case WASM_OP_GET_GLOBAL_64:
  6504. case WASM_OP_SET_GLOBAL_64:
  6505. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6506. skip_leb_uint32(p, p_end); /* local index */
  6507. break;
  6508. case EXT_OP_GET_LOCAL_FAST:
  6509. case EXT_OP_SET_LOCAL_FAST:
  6510. case EXT_OP_TEE_LOCAL_FAST:
  6511. CHECK_BUF(p, p_end, 1);
  6512. p++;
  6513. break;
  6514. case WASM_OP_I32_LOAD:
  6515. case WASM_OP_I64_LOAD:
  6516. case WASM_OP_F32_LOAD:
  6517. case WASM_OP_F64_LOAD:
  6518. case WASM_OP_I32_LOAD8_S:
  6519. case WASM_OP_I32_LOAD8_U:
  6520. case WASM_OP_I32_LOAD16_S:
  6521. case WASM_OP_I32_LOAD16_U:
  6522. case WASM_OP_I64_LOAD8_S:
  6523. case WASM_OP_I64_LOAD8_U:
  6524. case WASM_OP_I64_LOAD16_S:
  6525. case WASM_OP_I64_LOAD16_U:
  6526. case WASM_OP_I64_LOAD32_S:
  6527. case WASM_OP_I64_LOAD32_U:
  6528. case WASM_OP_I32_STORE:
  6529. case WASM_OP_I64_STORE:
  6530. case WASM_OP_F32_STORE:
  6531. case WASM_OP_F64_STORE:
  6532. case WASM_OP_I32_STORE8:
  6533. case WASM_OP_I32_STORE16:
  6534. case WASM_OP_I64_STORE8:
  6535. case WASM_OP_I64_STORE16:
  6536. case WASM_OP_I64_STORE32:
  6537. skip_leb_align(p, p_end); /* align */
  6538. skip_leb_mem_offset(p, p_end); /* offset */
  6539. break;
  6540. case WASM_OP_MEMORY_SIZE:
  6541. case WASM_OP_MEMORY_GROW:
  6542. skip_leb_memidx(p, p_end); /* memidx */
  6543. break;
  6544. case WASM_OP_I32_CONST:
  6545. skip_leb_int32(p, p_end);
  6546. break;
  6547. case WASM_OP_I64_CONST:
  6548. skip_leb_int64(p, p_end);
  6549. break;
  6550. case WASM_OP_F32_CONST:
  6551. p += sizeof(float32);
  6552. break;
  6553. case WASM_OP_F64_CONST:
  6554. p += sizeof(float64);
  6555. break;
  6556. case WASM_OP_I32_EQZ:
  6557. case WASM_OP_I32_EQ:
  6558. case WASM_OP_I32_NE:
  6559. case WASM_OP_I32_LT_S:
  6560. case WASM_OP_I32_LT_U:
  6561. case WASM_OP_I32_GT_S:
  6562. case WASM_OP_I32_GT_U:
  6563. case WASM_OP_I32_LE_S:
  6564. case WASM_OP_I32_LE_U:
  6565. case WASM_OP_I32_GE_S:
  6566. case WASM_OP_I32_GE_U:
  6567. case WASM_OP_I64_EQZ:
  6568. case WASM_OP_I64_EQ:
  6569. case WASM_OP_I64_NE:
  6570. case WASM_OP_I64_LT_S:
  6571. case WASM_OP_I64_LT_U:
  6572. case WASM_OP_I64_GT_S:
  6573. case WASM_OP_I64_GT_U:
  6574. case WASM_OP_I64_LE_S:
  6575. case WASM_OP_I64_LE_U:
  6576. case WASM_OP_I64_GE_S:
  6577. case WASM_OP_I64_GE_U:
  6578. case WASM_OP_F32_EQ:
  6579. case WASM_OP_F32_NE:
  6580. case WASM_OP_F32_LT:
  6581. case WASM_OP_F32_GT:
  6582. case WASM_OP_F32_LE:
  6583. case WASM_OP_F32_GE:
  6584. case WASM_OP_F64_EQ:
  6585. case WASM_OP_F64_NE:
  6586. case WASM_OP_F64_LT:
  6587. case WASM_OP_F64_GT:
  6588. case WASM_OP_F64_LE:
  6589. case WASM_OP_F64_GE:
  6590. case WASM_OP_I32_CLZ:
  6591. case WASM_OP_I32_CTZ:
  6592. case WASM_OP_I32_POPCNT:
  6593. case WASM_OP_I32_ADD:
  6594. case WASM_OP_I32_SUB:
  6595. case WASM_OP_I32_MUL:
  6596. case WASM_OP_I32_DIV_S:
  6597. case WASM_OP_I32_DIV_U:
  6598. case WASM_OP_I32_REM_S:
  6599. case WASM_OP_I32_REM_U:
  6600. case WASM_OP_I32_AND:
  6601. case WASM_OP_I32_OR:
  6602. case WASM_OP_I32_XOR:
  6603. case WASM_OP_I32_SHL:
  6604. case WASM_OP_I32_SHR_S:
  6605. case WASM_OP_I32_SHR_U:
  6606. case WASM_OP_I32_ROTL:
  6607. case WASM_OP_I32_ROTR:
  6608. case WASM_OP_I64_CLZ:
  6609. case WASM_OP_I64_CTZ:
  6610. case WASM_OP_I64_POPCNT:
  6611. case WASM_OP_I64_ADD:
  6612. case WASM_OP_I64_SUB:
  6613. case WASM_OP_I64_MUL:
  6614. case WASM_OP_I64_DIV_S:
  6615. case WASM_OP_I64_DIV_U:
  6616. case WASM_OP_I64_REM_S:
  6617. case WASM_OP_I64_REM_U:
  6618. case WASM_OP_I64_AND:
  6619. case WASM_OP_I64_OR:
  6620. case WASM_OP_I64_XOR:
  6621. case WASM_OP_I64_SHL:
  6622. case WASM_OP_I64_SHR_S:
  6623. case WASM_OP_I64_SHR_U:
  6624. case WASM_OP_I64_ROTL:
  6625. case WASM_OP_I64_ROTR:
  6626. case WASM_OP_F32_ABS:
  6627. case WASM_OP_F32_NEG:
  6628. case WASM_OP_F32_CEIL:
  6629. case WASM_OP_F32_FLOOR:
  6630. case WASM_OP_F32_TRUNC:
  6631. case WASM_OP_F32_NEAREST:
  6632. case WASM_OP_F32_SQRT:
  6633. case WASM_OP_F32_ADD:
  6634. case WASM_OP_F32_SUB:
  6635. case WASM_OP_F32_MUL:
  6636. case WASM_OP_F32_DIV:
  6637. case WASM_OP_F32_MIN:
  6638. case WASM_OP_F32_MAX:
  6639. case WASM_OP_F32_COPYSIGN:
  6640. case WASM_OP_F64_ABS:
  6641. case WASM_OP_F64_NEG:
  6642. case WASM_OP_F64_CEIL:
  6643. case WASM_OP_F64_FLOOR:
  6644. case WASM_OP_F64_TRUNC:
  6645. case WASM_OP_F64_NEAREST:
  6646. case WASM_OP_F64_SQRT:
  6647. case WASM_OP_F64_ADD:
  6648. case WASM_OP_F64_SUB:
  6649. case WASM_OP_F64_MUL:
  6650. case WASM_OP_F64_DIV:
  6651. case WASM_OP_F64_MIN:
  6652. case WASM_OP_F64_MAX:
  6653. case WASM_OP_F64_COPYSIGN:
  6654. case WASM_OP_I32_WRAP_I64:
  6655. case WASM_OP_I32_TRUNC_S_F32:
  6656. case WASM_OP_I32_TRUNC_U_F32:
  6657. case WASM_OP_I32_TRUNC_S_F64:
  6658. case WASM_OP_I32_TRUNC_U_F64:
  6659. case WASM_OP_I64_EXTEND_S_I32:
  6660. case WASM_OP_I64_EXTEND_U_I32:
  6661. case WASM_OP_I64_TRUNC_S_F32:
  6662. case WASM_OP_I64_TRUNC_U_F32:
  6663. case WASM_OP_I64_TRUNC_S_F64:
  6664. case WASM_OP_I64_TRUNC_U_F64:
  6665. case WASM_OP_F32_CONVERT_S_I32:
  6666. case WASM_OP_F32_CONVERT_U_I32:
  6667. case WASM_OP_F32_CONVERT_S_I64:
  6668. case WASM_OP_F32_CONVERT_U_I64:
  6669. case WASM_OP_F32_DEMOTE_F64:
  6670. case WASM_OP_F64_CONVERT_S_I32:
  6671. case WASM_OP_F64_CONVERT_U_I32:
  6672. case WASM_OP_F64_CONVERT_S_I64:
  6673. case WASM_OP_F64_CONVERT_U_I64:
  6674. case WASM_OP_F64_PROMOTE_F32:
  6675. case WASM_OP_I32_REINTERPRET_F32:
  6676. case WASM_OP_I64_REINTERPRET_F64:
  6677. case WASM_OP_F32_REINTERPRET_I32:
  6678. case WASM_OP_F64_REINTERPRET_I64:
  6679. case WASM_OP_I32_EXTEND8_S:
  6680. case WASM_OP_I32_EXTEND16_S:
  6681. case WASM_OP_I64_EXTEND8_S:
  6682. case WASM_OP_I64_EXTEND16_S:
  6683. case WASM_OP_I64_EXTEND32_S:
  6684. break;
  6685. #if WASM_ENABLE_GC != 0
  6686. case WASM_OP_GC_PREFIX:
  6687. {
  6688. uint32 opcode1;
  6689. read_leb_uint32(p, p_end, opcode1);
  6690. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6691. is no larger than UINT8_MAX */
  6692. opcode = (uint8)opcode1;
  6693. switch (opcode) {
  6694. case WASM_OP_STRUCT_NEW:
  6695. case WASM_OP_STRUCT_NEW_DEFAULT:
  6696. skip_leb_uint32(p, p_end); /* typeidx */
  6697. break;
  6698. case WASM_OP_STRUCT_GET:
  6699. case WASM_OP_STRUCT_GET_S:
  6700. case WASM_OP_STRUCT_GET_U:
  6701. case WASM_OP_STRUCT_SET:
  6702. skip_leb_uint32(p, p_end); /* typeidx */
  6703. skip_leb_uint32(p, p_end); /* fieldidx */
  6704. break;
  6705. case WASM_OP_ARRAY_NEW:
  6706. case WASM_OP_ARRAY_NEW_DEFAULT:
  6707. case WASM_OP_ARRAY_GET:
  6708. case WASM_OP_ARRAY_GET_S:
  6709. case WASM_OP_ARRAY_GET_U:
  6710. case WASM_OP_ARRAY_SET:
  6711. case WASM_OP_ARRAY_FILL:
  6712. skip_leb_uint32(p, p_end); /* typeidx */
  6713. break;
  6714. case WASM_OP_ARRAY_COPY:
  6715. skip_leb_uint32(p, p_end); /* typeidx1 */
  6716. skip_leb_uint32(p, p_end); /* typeidx2 */
  6717. break;
  6718. case WASM_OP_ARRAY_LEN:
  6719. break;
  6720. case WASM_OP_ARRAY_NEW_FIXED:
  6721. case WASM_OP_ARRAY_NEW_DATA:
  6722. case WASM_OP_ARRAY_NEW_ELEM:
  6723. skip_leb_uint32(p, p_end); /* typeidx */
  6724. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6725. break;
  6726. case WASM_OP_REF_I31:
  6727. case WASM_OP_I31_GET_S:
  6728. case WASM_OP_I31_GET_U:
  6729. break;
  6730. case WASM_OP_REF_TEST:
  6731. case WASM_OP_REF_CAST:
  6732. case WASM_OP_REF_TEST_NULLABLE:
  6733. case WASM_OP_REF_CAST_NULLABLE:
  6734. skip_leb_int32(p, p_end); /* heaptype */
  6735. break;
  6736. case WASM_OP_BR_ON_CAST:
  6737. case WASM_OP_BR_ON_CAST_FAIL:
  6738. p += sizeof(uint8); /* castflag */
  6739. skip_leb_uint32(p, p_end); /* labelidx */
  6740. skip_leb_int32(p, p_end); /* heaptype */
  6741. skip_leb_int32(p, p_end); /* heaptype2 */
  6742. break;
  6743. case WASM_OP_ANY_CONVERT_EXTERN:
  6744. case WASM_OP_EXTERN_CONVERT_ANY:
  6745. break;
  6746. #if WASM_ENABLE_STRINGREF != 0
  6747. case WASM_OP_STRING_NEW_UTF8:
  6748. case WASM_OP_STRING_NEW_WTF16:
  6749. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6750. case WASM_OP_STRING_NEW_WTF8:
  6751. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6752. break;
  6753. case WASM_OP_STRING_CONST:
  6754. skip_leb_int32(p, p_end); /* contents */
  6755. break;
  6756. case WASM_OP_STRING_MEASURE_UTF8:
  6757. case WASM_OP_STRING_MEASURE_WTF8:
  6758. case WASM_OP_STRING_MEASURE_WTF16:
  6759. break;
  6760. case WASM_OP_STRING_ENCODE_UTF8:
  6761. case WASM_OP_STRING_ENCODE_WTF16:
  6762. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6763. case WASM_OP_STRING_ENCODE_WTF8:
  6764. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6765. break;
  6766. case WASM_OP_STRING_CONCAT:
  6767. case WASM_OP_STRING_EQ:
  6768. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6769. case WASM_OP_STRING_AS_WTF8:
  6770. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6771. break;
  6772. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6773. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6774. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6775. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6776. break;
  6777. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6778. case WASM_OP_STRING_AS_WTF16:
  6779. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6780. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6781. break;
  6782. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6783. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6784. break;
  6785. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6786. case WASM_OP_STRING_AS_ITER:
  6787. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6788. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6789. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6790. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6791. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6792. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6793. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6794. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6795. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6796. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6797. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6798. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6799. break;
  6800. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6801. default:
  6802. return false;
  6803. }
  6804. break;
  6805. }
  6806. #endif /* end of WASM_ENABLE_GC != 0 */
  6807. case WASM_OP_MISC_PREFIX:
  6808. {
  6809. uint32 opcode1;
  6810. read_leb_uint32(p, p_end, opcode1);
  6811. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6812. is no larger than UINT8_MAX */
  6813. opcode = (uint8)opcode1;
  6814. switch (opcode) {
  6815. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6816. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6817. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6818. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6819. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6820. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6821. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6822. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6823. break;
  6824. #if WASM_ENABLE_BULK_MEMORY != 0
  6825. case WASM_OP_MEMORY_INIT:
  6826. skip_leb_uint32(p, p_end);
  6827. skip_leb_memidx(p, p_end);
  6828. break;
  6829. case WASM_OP_DATA_DROP:
  6830. skip_leb_uint32(p, p_end);
  6831. break;
  6832. case WASM_OP_MEMORY_COPY:
  6833. skip_leb_memidx(p, p_end);
  6834. skip_leb_memidx(p, p_end);
  6835. break;
  6836. case WASM_OP_MEMORY_FILL:
  6837. skip_leb_memidx(p, p_end);
  6838. break;
  6839. #endif /* WASM_ENABLE_BULK_MEMORY */
  6840. #if WASM_ENABLE_REF_TYPES != 0
  6841. case WASM_OP_TABLE_INIT:
  6842. case WASM_OP_TABLE_COPY:
  6843. /* tableidx */
  6844. skip_leb_uint32(p, p_end);
  6845. /* elemidx */
  6846. skip_leb_uint32(p, p_end);
  6847. break;
  6848. case WASM_OP_ELEM_DROP:
  6849. /* elemidx */
  6850. skip_leb_uint32(p, p_end);
  6851. break;
  6852. case WASM_OP_TABLE_SIZE:
  6853. case WASM_OP_TABLE_GROW:
  6854. case WASM_OP_TABLE_FILL:
  6855. skip_leb_uint32(p, p_end); /* table idx */
  6856. break;
  6857. #endif /* WASM_ENABLE_REF_TYPES */
  6858. default:
  6859. return false;
  6860. }
  6861. break;
  6862. }
  6863. #if WASM_ENABLE_SIMD != 0
  6864. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6865. case WASM_OP_SIMD_PREFIX:
  6866. {
  6867. uint32 opcode1;
  6868. read_leb_uint32(p, p_end, opcode1);
  6869. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6870. is no larger than UINT8_MAX */
  6871. opcode = (uint8)opcode1;
  6872. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6873. */
  6874. switch (opcode) {
  6875. case SIMD_v128_load:
  6876. case SIMD_v128_load8x8_s:
  6877. case SIMD_v128_load8x8_u:
  6878. case SIMD_v128_load16x4_s:
  6879. case SIMD_v128_load16x4_u:
  6880. case SIMD_v128_load32x2_s:
  6881. case SIMD_v128_load32x2_u:
  6882. case SIMD_v128_load8_splat:
  6883. case SIMD_v128_load16_splat:
  6884. case SIMD_v128_load32_splat:
  6885. case SIMD_v128_load64_splat:
  6886. case SIMD_v128_store:
  6887. /* memarg align */
  6888. skip_leb_uint32(p, p_end);
  6889. /* memarg offset */
  6890. skip_leb_mem_offset(p, p_end);
  6891. break;
  6892. case SIMD_v128_const:
  6893. case SIMD_v8x16_shuffle:
  6894. /* immByte[16] immLaneId[16] */
  6895. CHECK_BUF1(p, p_end, 16);
  6896. p += 16;
  6897. break;
  6898. case SIMD_i8x16_extract_lane_s:
  6899. case SIMD_i8x16_extract_lane_u:
  6900. case SIMD_i8x16_replace_lane:
  6901. case SIMD_i16x8_extract_lane_s:
  6902. case SIMD_i16x8_extract_lane_u:
  6903. case SIMD_i16x8_replace_lane:
  6904. case SIMD_i32x4_extract_lane:
  6905. case SIMD_i32x4_replace_lane:
  6906. case SIMD_i64x2_extract_lane:
  6907. case SIMD_i64x2_replace_lane:
  6908. case SIMD_f32x4_extract_lane:
  6909. case SIMD_f32x4_replace_lane:
  6910. case SIMD_f64x2_extract_lane:
  6911. case SIMD_f64x2_replace_lane:
  6912. /* ImmLaneId */
  6913. CHECK_BUF(p, p_end, 1);
  6914. p++;
  6915. break;
  6916. case SIMD_v128_load8_lane:
  6917. case SIMD_v128_load16_lane:
  6918. case SIMD_v128_load32_lane:
  6919. case SIMD_v128_load64_lane:
  6920. case SIMD_v128_store8_lane:
  6921. case SIMD_v128_store16_lane:
  6922. case SIMD_v128_store32_lane:
  6923. case SIMD_v128_store64_lane:
  6924. /* memarg align */
  6925. skip_leb_uint32(p, p_end);
  6926. /* memarg offset */
  6927. skip_leb_mem_offset(p, p_end);
  6928. /* ImmLaneId */
  6929. CHECK_BUF(p, p_end, 1);
  6930. p++;
  6931. break;
  6932. case SIMD_v128_load32_zero:
  6933. case SIMD_v128_load64_zero:
  6934. /* memarg align */
  6935. skip_leb_uint32(p, p_end);
  6936. /* memarg offset */
  6937. skip_leb_mem_offset(p, p_end);
  6938. break;
  6939. default:
  6940. /*
  6941. * since latest SIMD specific used almost every value
  6942. * from 0x00 to 0xff, the default branch will present
  6943. * all opcodes without imm
  6944. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6945. */
  6946. break;
  6947. }
  6948. break;
  6949. }
  6950. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6951. #endif /* end of WASM_ENABLE_SIMD */
  6952. #if WASM_ENABLE_SHARED_MEMORY != 0
  6953. case WASM_OP_ATOMIC_PREFIX:
  6954. {
  6955. uint32 opcode1;
  6956. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6957. read_leb_uint32(p, p_end, opcode1);
  6958. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6959. is no larger than UINT8_MAX */
  6960. opcode = (uint8)opcode1;
  6961. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6962. skip_leb_uint32(p, p_end); /* align */
  6963. skip_leb_mem_offset(p, p_end); /* offset */
  6964. }
  6965. else {
  6966. /* atomic.fence doesn't have memarg */
  6967. p++;
  6968. }
  6969. break;
  6970. }
  6971. #endif
  6972. #if WASM_ENABLE_DEBUG_INTERP != 0
  6973. case DEBUG_OP_BREAK:
  6974. {
  6975. WASMDebugInstance *debug_instance =
  6976. wasm_exec_env_get_instance(exec_env);
  6977. char original_opcode[1];
  6978. uint64 size = 1;
  6979. WASMModuleInstance *module_inst =
  6980. (WASMModuleInstance *)exec_env->module_inst;
  6981. uint64 offset = (p - 1) >= module_inst->module->load_addr
  6982. ? (p - 1) - module_inst->module->load_addr
  6983. : ~0;
  6984. if (debug_instance) {
  6985. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  6986. original_opcode, &size)
  6987. && size == 1) {
  6988. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  6989. "with %02x: ",
  6990. original_opcode[0]);
  6991. opcode = original_opcode[0];
  6992. goto op_break_retry;
  6993. }
  6994. }
  6995. break;
  6996. }
  6997. #endif
  6998. default:
  6999. return false;
  7000. }
  7001. }
  7002. (void)u8;
  7003. (void)exec_env;
  7004. return false;
  7005. fail:
  7006. return false;
  7007. }
  7008. #if WASM_ENABLE_FAST_INTERP != 0
  7009. #if WASM_DEBUG_PREPROCESSOR != 0
  7010. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7011. #else
  7012. #define LOG_OP(...) (void)0
  7013. #endif
  7014. #define PATCH_ELSE 0
  7015. #define PATCH_END 1
  7016. typedef struct BranchBlockPatch {
  7017. struct BranchBlockPatch *next;
  7018. uint8 patch_type;
  7019. uint8 *code_compiled;
  7020. } BranchBlockPatch;
  7021. #endif
  7022. typedef struct BranchBlock {
  7023. uint8 label_type;
  7024. BlockType block_type;
  7025. uint8 *start_addr;
  7026. uint8 *else_addr;
  7027. uint8 *end_addr;
  7028. uint32 stack_cell_num;
  7029. #if WASM_ENABLE_GC != 0
  7030. uint32 reftype_map_num;
  7031. /* Indicate which local is used inside current block, used to validate
  7032. * local.get with non-nullable ref types */
  7033. uint8 *local_use_mask;
  7034. uint32 local_use_mask_size;
  7035. #endif
  7036. #if WASM_ENABLE_FAST_INTERP != 0
  7037. uint16 dynamic_offset;
  7038. uint8 *code_compiled;
  7039. BranchBlockPatch *patch_list;
  7040. /* This is used to save params frame_offset of of if block */
  7041. int16 *param_frame_offsets;
  7042. /* This is used to recover the dynamic offset for else branch,
  7043. * and also to remember the start offset of dynamic space which
  7044. * stores the block arguments for loop block, so we can use it
  7045. * to copy the stack operands to the loop block's arguments in
  7046. * wasm_loader_emit_br_info for opcode br. */
  7047. uint16 start_dynamic_offset;
  7048. #endif
  7049. /* Indicate the operand stack is in polymorphic state.
  7050. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7051. * to polymorphic state until the block's 'end' opcode is processed.
  7052. * If stack is in polymorphic state and stack is empty, instruction can
  7053. * pop any type of value directly without decreasing stack top pointer
  7054. * and stack cell num. */
  7055. bool is_stack_polymorphic;
  7056. } BranchBlock;
  7057. typedef struct WASMLoaderContext {
  7058. /* frame ref stack */
  7059. uint8 *frame_ref;
  7060. uint8 *frame_ref_bottom;
  7061. uint8 *frame_ref_boundary;
  7062. uint32 frame_ref_size;
  7063. uint32 stack_cell_num;
  7064. uint32 max_stack_cell_num;
  7065. #if WASM_ENABLE_GC != 0
  7066. /* frame reftype map stack */
  7067. WASMRefTypeMap *frame_reftype_map;
  7068. WASMRefTypeMap *frame_reftype_map_bottom;
  7069. WASMRefTypeMap *frame_reftype_map_boundary;
  7070. uint32 frame_reftype_map_size;
  7071. uint32 reftype_map_num;
  7072. uint32 max_reftype_map_num;
  7073. /* Current module */
  7074. WASMModule *module;
  7075. /* Current module's ref_type_set */
  7076. HashMap *ref_type_set;
  7077. /* Always point to local variable ref_type of
  7078. wasm_loader_prepare_bytecode */
  7079. WASMRefType *ref_type_tmp;
  7080. #endif
  7081. /* frame csp stack */
  7082. BranchBlock *frame_csp;
  7083. BranchBlock *frame_csp_bottom;
  7084. BranchBlock *frame_csp_boundary;
  7085. uint32 frame_csp_size;
  7086. uint32 csp_num;
  7087. uint32 max_csp_num;
  7088. #if WASM_ENABLE_FAST_INTERP != 0
  7089. /* frame offset stack */
  7090. int16 *frame_offset;
  7091. int16 *frame_offset_bottom;
  7092. int16 *frame_offset_boundary;
  7093. uint32 frame_offset_size;
  7094. int16 dynamic_offset;
  7095. int16 start_dynamic_offset;
  7096. int16 max_dynamic_offset;
  7097. /* preserved local offset */
  7098. int16 preserved_local_offset;
  7099. /* const buffer */
  7100. uint8 *const_buf;
  7101. uint16 num_const;
  7102. uint16 const_cell_num;
  7103. uint32 const_buf_size;
  7104. /* processed code */
  7105. uint8 *p_code_compiled;
  7106. uint8 *p_code_compiled_end;
  7107. uint32 code_compiled_size;
  7108. /* If the last opcode will be dropped, the peak memory usage will be larger
  7109. * than the final code_compiled_size, we record the peak size to ensure
  7110. * there will not be invalid memory access during second traverse */
  7111. uint32 code_compiled_peak_size;
  7112. #endif
  7113. } WASMLoaderContext;
  7114. typedef struct Const {
  7115. WASMValue value;
  7116. uint16 slot_index;
  7117. uint8 value_type;
  7118. } Const;
  7119. #define CHECK_CSP_PUSH() \
  7120. do { \
  7121. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7122. MEM_REALLOC( \
  7123. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7124. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7125. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7126. ctx->frame_csp_boundary = \
  7127. ctx->frame_csp_bottom \
  7128. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7129. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7130. } \
  7131. } while (0)
  7132. #define CHECK_CSP_POP() \
  7133. do { \
  7134. if (ctx->csp_num < 1) { \
  7135. set_error_buf(error_buf, error_buf_size, \
  7136. "type mismatch: " \
  7137. "expect data but block stack was empty"); \
  7138. goto fail; \
  7139. } \
  7140. } while (0)
  7141. #if WASM_ENABLE_FAST_INTERP != 0
  7142. static bool
  7143. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7144. uint32 error_buf_size)
  7145. {
  7146. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7147. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7148. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7149. ctx->frame_offset_size + 16);
  7150. ctx->frame_offset_size += 16;
  7151. ctx->frame_offset_boundary =
  7152. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7153. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7154. }
  7155. return true;
  7156. fail:
  7157. return false;
  7158. }
  7159. static bool
  7160. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7161. {
  7162. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7163. return false;
  7164. return true;
  7165. }
  7166. static void
  7167. free_label_patch_list(BranchBlock *frame_csp)
  7168. {
  7169. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7170. BranchBlockPatch *next;
  7171. while (label_patch != NULL) {
  7172. next = label_patch->next;
  7173. wasm_runtime_free(label_patch);
  7174. label_patch = next;
  7175. }
  7176. frame_csp->patch_list = NULL;
  7177. }
  7178. static void
  7179. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7180. {
  7181. BranchBlock *tmp_csp = frame_csp;
  7182. uint32 i;
  7183. for (i = 0; i < csp_num; i++) {
  7184. free_label_patch_list(tmp_csp);
  7185. tmp_csp++;
  7186. }
  7187. }
  7188. static void
  7189. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7190. {
  7191. BranchBlock *tmp_csp = frame_csp;
  7192. uint32 i;
  7193. for (i = 0; i < csp_num; i++) {
  7194. if (tmp_csp->param_frame_offsets)
  7195. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7196. tmp_csp++;
  7197. }
  7198. }
  7199. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7200. #if WASM_ENABLE_GC != 0
  7201. static bool
  7202. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7203. char *error_buf, uint32 error_buf_size)
  7204. {
  7205. BranchBlock *current_csp = ctx->frame_csp - 1;
  7206. uint32 local_mask_size;
  7207. if (local_count == 0) {
  7208. current_csp->local_use_mask_size = 0;
  7209. return true;
  7210. }
  7211. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7212. * else branch, we don't need to allocate memory again */
  7213. if (!current_csp->local_use_mask) {
  7214. local_mask_size = (local_count + 7) / sizeof(uint8);
  7215. if (!(current_csp->local_use_mask =
  7216. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7217. return false;
  7218. }
  7219. current_csp->local_use_mask_size = local_mask_size;
  7220. }
  7221. else {
  7222. local_mask_size = current_csp->local_use_mask_size;
  7223. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7224. }
  7225. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7226. /* For non-function blocks, inherit the use status from parent block */
  7227. BranchBlock *parent_csp = current_csp - 1;
  7228. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7229. bh_assert(parent_csp->local_use_mask);
  7230. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7231. parent_csp->local_use_mask, local_mask_size);
  7232. }
  7233. return true;
  7234. }
  7235. static void
  7236. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7237. {
  7238. BranchBlock *current_csp = ctx->frame_csp - 1;
  7239. bh_assert(current_csp->local_use_mask
  7240. || current_csp->local_use_mask_size == 0);
  7241. if (current_csp->local_use_mask) {
  7242. wasm_runtime_free(current_csp->local_use_mask);
  7243. }
  7244. current_csp->local_use_mask = NULL;
  7245. current_csp->local_use_mask_size = 0;
  7246. }
  7247. static void
  7248. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7249. {
  7250. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7251. uint32 i;
  7252. for (i = 0; i < ctx->csp_num; i++) {
  7253. if (tmp_csp->local_use_mask) {
  7254. wasm_runtime_free(tmp_csp->local_use_mask);
  7255. tmp_csp->local_use_mask = NULL;
  7256. tmp_csp->local_use_mask_size = 0;
  7257. }
  7258. tmp_csp++;
  7259. }
  7260. }
  7261. static void
  7262. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7263. {
  7264. BranchBlock *current_csp = ctx->frame_csp - 1;
  7265. uint32 byte_offset = index / sizeof(uint8);
  7266. uint32 bit_offset = index % sizeof(uint8);
  7267. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7268. bh_assert(current_csp->local_use_mask);
  7269. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7270. }
  7271. static bool
  7272. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7273. {
  7274. BranchBlock *current_csp = ctx->frame_csp - 1;
  7275. uint32 byte_offset = index / sizeof(uint8);
  7276. uint32 bit_offset = index % sizeof(uint8);
  7277. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7278. bh_assert(current_csp->local_use_mask);
  7279. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7280. ? true
  7281. : false;
  7282. }
  7283. #endif /* end of WASM_ENABLE_GC != 0 */
  7284. static void
  7285. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7286. {
  7287. if (ctx) {
  7288. if (ctx->frame_ref_bottom)
  7289. wasm_runtime_free(ctx->frame_ref_bottom);
  7290. #if WASM_ENABLE_GC != 0
  7291. if (ctx->frame_reftype_map_bottom)
  7292. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7293. #endif
  7294. if (ctx->frame_csp_bottom) {
  7295. #if WASM_ENABLE_FAST_INTERP != 0
  7296. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7297. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7298. ctx->csp_num);
  7299. #endif
  7300. #if WASM_ENABLE_GC != 0
  7301. wasm_loader_clean_all_local_use_masks(ctx);
  7302. #endif
  7303. wasm_runtime_free(ctx->frame_csp_bottom);
  7304. }
  7305. #if WASM_ENABLE_FAST_INTERP != 0
  7306. if (ctx->frame_offset_bottom)
  7307. wasm_runtime_free(ctx->frame_offset_bottom);
  7308. if (ctx->const_buf)
  7309. wasm_runtime_free(ctx->const_buf);
  7310. #endif
  7311. wasm_runtime_free(ctx);
  7312. }
  7313. }
  7314. static WASMLoaderContext *
  7315. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7316. {
  7317. WASMLoaderContext *loader_ctx =
  7318. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7319. if (!loader_ctx)
  7320. return NULL;
  7321. loader_ctx->frame_ref_size = 32;
  7322. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7323. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7324. goto fail;
  7325. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7326. #if WASM_ENABLE_GC != 0
  7327. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7328. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7329. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7330. error_buf_size)))
  7331. goto fail;
  7332. loader_ctx->frame_reftype_map_boundary =
  7333. loader_ctx->frame_reftype_map_bottom + 16;
  7334. #endif
  7335. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7336. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7337. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7338. goto fail;
  7339. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7340. #if WASM_ENABLE_EXCE_HANDLING != 0
  7341. func->exception_handler_count = 0;
  7342. #endif
  7343. #if WASM_ENABLE_FAST_INTERP != 0
  7344. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7345. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7346. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7347. error_buf_size)))
  7348. goto fail;
  7349. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7350. loader_ctx->num_const = 0;
  7351. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7352. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7353. error_buf, error_buf_size)))
  7354. goto fail;
  7355. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7356. set_error_buf(error_buf, error_buf_size,
  7357. "fast interpreter offset overflow");
  7358. goto fail;
  7359. }
  7360. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7361. loader_ctx->max_dynamic_offset =
  7362. func->param_cell_num + func->local_cell_num;
  7363. #endif
  7364. return loader_ctx;
  7365. fail:
  7366. wasm_loader_ctx_destroy(loader_ctx);
  7367. return NULL;
  7368. }
  7369. static bool
  7370. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7371. uint32 error_buf_size)
  7372. {
  7373. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7374. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7375. /* Increase the frame ref stack */
  7376. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7377. ctx->frame_ref_size + 16);
  7378. ctx->frame_ref_size += 16;
  7379. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7380. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7381. }
  7382. #if WASM_ENABLE_GC != 0
  7383. if (wasm_is_type_multi_byte_type(type)
  7384. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7385. /* Increase the frame reftype map stack */
  7386. bh_assert(
  7387. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7388. * sizeof(WASMRefTypeMap))
  7389. == ctx->frame_reftype_map_size);
  7390. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7391. ctx->frame_reftype_map_size
  7392. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7393. ctx->frame_reftype_map =
  7394. ctx->frame_reftype_map_bottom
  7395. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7396. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7397. ctx->frame_reftype_map_boundary =
  7398. ctx->frame_reftype_map_bottom
  7399. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7400. }
  7401. #endif
  7402. return true;
  7403. fail:
  7404. return false;
  7405. }
  7406. static bool
  7407. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7408. uint32 error_buf_size)
  7409. {
  7410. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7411. uint32 i;
  7412. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7413. return false;
  7414. #if WASM_ENABLE_GC != 0
  7415. if (wasm_is_type_multi_byte_type(type)) {
  7416. WASMRefType *ref_type;
  7417. if (!(ref_type =
  7418. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7419. error_buf, error_buf_size))) {
  7420. return false;
  7421. }
  7422. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7423. /* Increase the frame reftype map stack */
  7424. bh_assert((uint32)((ctx->frame_reftype_map
  7425. - ctx->frame_reftype_map_bottom)
  7426. * sizeof(WASMRefTypeMap))
  7427. == ctx->frame_reftype_map_size);
  7428. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7429. ctx->frame_reftype_map_size,
  7430. ctx->frame_reftype_map_size
  7431. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7432. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7433. + ctx->frame_reftype_map_size
  7434. / ((uint32)sizeof(WASMRefTypeMap));
  7435. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7436. ctx->frame_reftype_map_boundary =
  7437. ctx->frame_reftype_map_bottom
  7438. + ctx->frame_reftype_map_size
  7439. / ((uint32)sizeof(WASMRefTypeMap));
  7440. }
  7441. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7442. ctx->frame_reftype_map->ref_type = ref_type;
  7443. ctx->frame_reftype_map++;
  7444. ctx->reftype_map_num++;
  7445. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7446. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7447. }
  7448. #endif
  7449. for (i = 0; i < type_cell_num; i++)
  7450. *ctx->frame_ref++ = type;
  7451. ctx->stack_cell_num += type_cell_num;
  7452. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7453. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7454. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7455. set_error_buf(error_buf, error_buf_size,
  7456. "operand stack depth limit exceeded");
  7457. return false;
  7458. }
  7459. }
  7460. return true;
  7461. #if WASM_ENABLE_GC != 0
  7462. fail:
  7463. return false;
  7464. #endif
  7465. }
  7466. static bool
  7467. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7468. int32 stack_cell_num,
  7469. #if WASM_ENABLE_GC != 0
  7470. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7471. #endif
  7472. uint8 type,
  7473. #if WASM_ENABLE_GC != 0
  7474. WASMRefType *ref_type,
  7475. #endif
  7476. char *error_buf, uint32 error_buf_size)
  7477. {
  7478. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7479. #if WASM_ENABLE_GC != 0
  7480. WASMRefType *frame_reftype = NULL;
  7481. #endif
  7482. if (stack_cell_num < type_cell_num) {
  7483. set_error_buf(error_buf, error_buf_size,
  7484. "type mismatch: expect data but stack was empty");
  7485. return false;
  7486. }
  7487. #if WASM_ENABLE_GC == 0
  7488. for (i = 0; i < type_cell_num; i++) {
  7489. if (*(frame_ref - 1 - i) != type) {
  7490. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7491. "type mismatch: expect ", type2str(type),
  7492. " but got other");
  7493. return false;
  7494. }
  7495. }
  7496. #else
  7497. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7498. bh_assert(reftype_map_num > 0);
  7499. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7500. }
  7501. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7502. ref_type, ctx->module->types,
  7503. ctx->module->type_count)) {
  7504. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7505. "type mismatch: expect ", type2str(type),
  7506. " but got other");
  7507. return false;
  7508. }
  7509. for (i = 0; i < type_cell_num - 1; i++) {
  7510. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7511. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7512. "type mismatch: expect ", type2str(type),
  7513. " but got other");
  7514. return false;
  7515. }
  7516. }
  7517. #endif
  7518. return true;
  7519. }
  7520. static bool
  7521. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7522. uint32 error_buf_size)
  7523. {
  7524. int32 block_stack_cell_num =
  7525. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7526. #if WASM_ENABLE_GC != 0
  7527. int32 reftype_map_num =
  7528. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7529. #endif
  7530. if (block_stack_cell_num > 0) {
  7531. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7532. /* the stack top is a value of any type, return success */
  7533. return true;
  7534. }
  7535. #if WASM_ENABLE_GC != 0
  7536. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7537. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7538. WASMRefType *stack_top_ref_type = NULL;
  7539. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7540. bh_assert(reftype_map_num > 0);
  7541. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7542. }
  7543. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7544. ctx->ref_type_tmp, ctx->module->types,
  7545. ctx->module->type_count)) {
  7546. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7547. uint32 ref_type_struct_size =
  7548. wasm_reftype_struct_size(stack_top_ref_type);
  7549. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7550. stack_top_ref_type, ref_type_struct_size);
  7551. }
  7552. return true;
  7553. }
  7554. }
  7555. #endif
  7556. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7557. #if WASM_ENABLE_GC != 0
  7558. ctx->frame_reftype_map, reftype_map_num,
  7559. #endif
  7560. type,
  7561. #if WASM_ENABLE_GC != 0
  7562. ctx->ref_type_tmp,
  7563. #endif
  7564. error_buf, error_buf_size)) {
  7565. return false;
  7566. }
  7567. return true;
  7568. }
  7569. static bool
  7570. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7571. uint32 error_buf_size)
  7572. {
  7573. BranchBlock *cur_block = ctx->frame_csp - 1;
  7574. int32 available_stack_cell =
  7575. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7576. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7577. /* Directly return success if current block is in stack
  7578. polymorphic state while stack is empty. */
  7579. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7580. return true;
  7581. if (type == VALUE_TYPE_VOID)
  7582. return true;
  7583. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7584. return false;
  7585. bh_assert(available_stack_cell > 0);
  7586. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7587. type = VALUE_TYPE_ANY;
  7588. cell_num_to_pop = 1;
  7589. }
  7590. ctx->frame_ref -= cell_num_to_pop;
  7591. ctx->stack_cell_num -= cell_num_to_pop;
  7592. #if WASM_ENABLE_GC != 0
  7593. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7594. ctx->frame_reftype_map--;
  7595. ctx->reftype_map_num--;
  7596. }
  7597. #endif
  7598. return true;
  7599. }
  7600. #if WASM_ENABLE_GC != 0
  7601. /* Get the stack top element of current block */
  7602. static bool
  7603. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7604. WASMRefType **p_ref_type, char *error_buf,
  7605. uint32 error_buf_size)
  7606. {
  7607. BranchBlock *cur_block = ctx->frame_csp - 1;
  7608. int32 available_stack_cell =
  7609. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7610. if (available_stack_cell <= 0) {
  7611. /* Directly return success if current block is in stack
  7612. polymorphic state while stack is empty. */
  7613. if (cur_block->is_stack_polymorphic) {
  7614. *p_type = VALUE_TYPE_ANY;
  7615. return true;
  7616. }
  7617. else {
  7618. set_error_buf(
  7619. error_buf, error_buf_size,
  7620. "type mismatch: expect data but block stack was empty");
  7621. return false;
  7622. }
  7623. }
  7624. *p_type = *(ctx->frame_ref - 1);
  7625. if (wasm_is_type_multi_byte_type(*p_type)) {
  7626. int32 available_reftype_map =
  7627. (int32)(ctx->reftype_map_num
  7628. - (ctx->frame_csp - 1)->reftype_map_num);
  7629. bh_assert(available_reftype_map > 0);
  7630. (void)available_reftype_map;
  7631. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7632. }
  7633. return true;
  7634. }
  7635. #if WASM_ENABLE_FAST_INTERP != 0
  7636. static bool
  7637. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7638. char *error_buf, uint32 error_buf_size);
  7639. #endif
  7640. /* Check whether the stack top elem is a heap object, and if yes,
  7641. pop and return it */
  7642. static bool
  7643. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7644. WASMRefType *ref_ht_ret, char *error_buf,
  7645. uint32 error_buf_size)
  7646. {
  7647. uint8 type = 0;
  7648. WASMRefType *ref_type = NULL;
  7649. /* Get stack top element */
  7650. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7651. error_buf_size)) {
  7652. return false;
  7653. }
  7654. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7655. /* stack top isn't a ref type */
  7656. && !wasm_is_type_reftype(type)) {
  7657. set_error_buf(error_buf, error_buf_size,
  7658. "type mismatch: expect heap object but got others");
  7659. return false;
  7660. }
  7661. /* POP stack top */
  7662. if (wasm_is_type_multi_byte_type(type)) {
  7663. bh_assert(ref_type);
  7664. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7665. wasm_reftype_struct_size(ref_type));
  7666. }
  7667. #if WASM_ENABLE_FAST_INTERP != 0
  7668. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7669. error_buf_size)) {
  7670. return false;
  7671. }
  7672. #else
  7673. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7674. return false;
  7675. }
  7676. #endif
  7677. if (p_type)
  7678. *p_type = type;
  7679. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7680. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7681. wasm_reftype_struct_size(ref_type));
  7682. }
  7683. return true;
  7684. }
  7685. /* Check whether the stack top elem is subtype of (ref null ht),
  7686. and if yes, pop it and return the converted (ref ht) */
  7687. static bool
  7688. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7689. WASMRefType *ref_ht_ret, char *error_buf,
  7690. uint32 error_buf_size)
  7691. {
  7692. uint8 type = 0;
  7693. WASMRefType ref_type = { 0 };
  7694. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7695. error_buf_size)) {
  7696. return false;
  7697. }
  7698. /* Convert to related (ref ht) and return */
  7699. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7700. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7701. */
  7702. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7703. HEAP_TYPE_ARRAY
  7704. + (type - REF_TYPE_ARRAYREF));
  7705. type = ref_ht_ret->ref_type;
  7706. }
  7707. else if (wasm_is_reftype_htref_nullable(type)
  7708. || wasm_is_reftype_htref_non_nullable(type)) {
  7709. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7710. wasm_reftype_struct_size(&ref_type));
  7711. /* Convert to (ref ht) */
  7712. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7713. ref_ht_ret->ref_ht_common.nullable = false;
  7714. type = ref_ht_ret->ref_type;
  7715. }
  7716. *p_type = type;
  7717. return true;
  7718. }
  7719. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7720. and if yes, pop it and return the type_idx */
  7721. static bool
  7722. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7723. uint32 *p_type_idx, char *error_buf,
  7724. uint32 error_buf_size)
  7725. {
  7726. uint8 type = 0;
  7727. int32 type_idx = -1;
  7728. WASMRefType *ref_type = NULL;
  7729. /* Get stack top element */
  7730. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7731. error_buf_size)) {
  7732. return false;
  7733. }
  7734. if (type != VALUE_TYPE_ANY) {
  7735. /* stack top isn't (ref null $t) */
  7736. if (!((wasm_is_reftype_htref_nullable(type)
  7737. || wasm_is_reftype_htref_non_nullable(type))
  7738. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7739. set_error_buf(error_buf, error_buf_size,
  7740. "type mismatch: expect (ref null $t) but got others");
  7741. return false;
  7742. }
  7743. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7744. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7745. wasm_reftype_struct_size(ref_type));
  7746. }
  7747. /* POP stack top */
  7748. #if WASM_ENABLE_FAST_INTERP != 0
  7749. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7750. error_buf_size)) {
  7751. return false;
  7752. }
  7753. #else
  7754. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7755. return false;
  7756. }
  7757. #endif
  7758. /* Convert to type_idx and return */
  7759. *p_type = type;
  7760. if (type != VALUE_TYPE_ANY)
  7761. *p_type_idx = (uint32)type_idx;
  7762. return true;
  7763. }
  7764. #endif /* WASM_ENABLE_GC != 0 */
  7765. #if WASM_ENABLE_FAST_INTERP == 0
  7766. static bool
  7767. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7768. uint8 type_push, uint8 type_pop, char *error_buf,
  7769. uint32 error_buf_size)
  7770. {
  7771. for (int i = 0; i < pop_cnt; i++) {
  7772. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7773. error_buf_size))
  7774. return false;
  7775. }
  7776. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7777. return false;
  7778. return true;
  7779. }
  7780. #endif
  7781. static bool
  7782. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7783. BlockType block_type, uint8 *start_addr,
  7784. char *error_buf, uint32 error_buf_size)
  7785. {
  7786. CHECK_CSP_PUSH();
  7787. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7788. ctx->frame_csp->label_type = label_type;
  7789. ctx->frame_csp->block_type = block_type;
  7790. ctx->frame_csp->start_addr = start_addr;
  7791. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7792. #if WASM_ENABLE_GC != 0
  7793. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7794. #endif
  7795. #if WASM_ENABLE_FAST_INTERP != 0
  7796. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7797. ctx->frame_csp->patch_list = NULL;
  7798. #endif
  7799. ctx->frame_csp++;
  7800. ctx->csp_num++;
  7801. if (ctx->csp_num > ctx->max_csp_num) {
  7802. ctx->max_csp_num = ctx->csp_num;
  7803. if (ctx->max_csp_num > UINT16_MAX) {
  7804. set_error_buf(error_buf, error_buf_size,
  7805. "label stack depth limit exceeded");
  7806. return false;
  7807. }
  7808. }
  7809. return true;
  7810. fail:
  7811. return false;
  7812. }
  7813. static bool
  7814. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7815. uint32 error_buf_size)
  7816. {
  7817. CHECK_CSP_POP();
  7818. #if WASM_ENABLE_FAST_INTERP != 0
  7819. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7820. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7821. #endif
  7822. ctx->frame_csp--;
  7823. ctx->csp_num--;
  7824. return true;
  7825. fail:
  7826. return false;
  7827. }
  7828. #if WASM_ENABLE_FAST_INTERP != 0
  7829. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7830. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7831. #define emit_label(opcode) \
  7832. do { \
  7833. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7834. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7835. } while (0)
  7836. #define skip_label() \
  7837. do { \
  7838. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7839. LOG_OP("\ndelete last op\n"); \
  7840. } while (0)
  7841. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7842. #if UINTPTR_MAX == UINT64_MAX
  7843. #define emit_label(opcode) \
  7844. do { \
  7845. int32 offset = \
  7846. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7847. /* emit int32 relative offset in 64-bit target */ \
  7848. wasm_loader_emit_uint32(loader_ctx, offset); \
  7849. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7850. } while (0)
  7851. #else
  7852. #define emit_label(opcode) \
  7853. do { \
  7854. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7855. /* emit uint32 label address in 32-bit target */ \
  7856. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7857. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7858. } while (0)
  7859. #endif
  7860. #define skip_label() \
  7861. do { \
  7862. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7863. LOG_OP("\ndelete last op\n"); \
  7864. } while (0)
  7865. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7866. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7867. #define emit_label(opcode) \
  7868. do { \
  7869. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7870. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7871. } while (0)
  7872. #define skip_label() \
  7873. do { \
  7874. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7875. LOG_OP("\ndelete last op\n"); \
  7876. } while (0)
  7877. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7878. #define emit_empty_label_addr_and_frame_ip(type) \
  7879. do { \
  7880. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7881. loader_ctx->p_code_compiled, error_buf, \
  7882. error_buf_size)) \
  7883. goto fail; \
  7884. /* label address, to be patched */ \
  7885. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7886. } while (0)
  7887. #define emit_br_info(frame_csp, is_br) \
  7888. do { \
  7889. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7890. error_buf_size)) \
  7891. goto fail; \
  7892. } while (0)
  7893. #define LAST_OP_OUTPUT_I32() \
  7894. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7895. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7896. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7897. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7898. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7899. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7900. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7901. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7902. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7903. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7904. || (last_op == EXT_OP_COPY_STACK_TOP)
  7905. #define LAST_OP_OUTPUT_I64() \
  7906. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7907. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7908. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7909. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7910. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7911. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7912. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7913. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7914. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7915. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7916. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7917. #define GET_CONST_OFFSET(type, val) \
  7918. do { \
  7919. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7920. &operand_offset, error_buf, \
  7921. error_buf_size))) \
  7922. goto fail; \
  7923. } while (0)
  7924. #define GET_CONST_F32_OFFSET(type, fval) \
  7925. do { \
  7926. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7927. &operand_offset, error_buf, \
  7928. error_buf_size))) \
  7929. goto fail; \
  7930. } while (0)
  7931. #define GET_CONST_F64_OFFSET(type, fval) \
  7932. do { \
  7933. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7934. &operand_offset, error_buf, \
  7935. error_buf_size))) \
  7936. goto fail; \
  7937. } while (0)
  7938. #define emit_operand(ctx, offset) \
  7939. do { \
  7940. wasm_loader_emit_int16(ctx, offset); \
  7941. LOG_OP("%d\t", offset); \
  7942. } while (0)
  7943. #define emit_byte(ctx, byte) \
  7944. do { \
  7945. wasm_loader_emit_uint8(ctx, byte); \
  7946. LOG_OP("%d\t", byte); \
  7947. } while (0)
  7948. #define emit_uint32(ctx, value) \
  7949. do { \
  7950. wasm_loader_emit_uint32(ctx, value); \
  7951. LOG_OP("%d\t", value); \
  7952. } while (0)
  7953. #define emit_uint64(ctx, value) \
  7954. do { \
  7955. wasm_loader_emit_const(ctx, &value, false); \
  7956. LOG_OP("%lld\t", value); \
  7957. } while (0)
  7958. #define emit_float32(ctx, value) \
  7959. do { \
  7960. wasm_loader_emit_const(ctx, &value, true); \
  7961. LOG_OP("%f\t", value); \
  7962. } while (0)
  7963. #define emit_float64(ctx, value) \
  7964. do { \
  7965. wasm_loader_emit_const(ctx, &value, false); \
  7966. LOG_OP("%f\t", value); \
  7967. } while (0)
  7968. static bool
  7969. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7970. {
  7971. if (!(ctx->p_code_compiled =
  7972. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7973. return false;
  7974. ctx->p_code_compiled_end =
  7975. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  7976. /* clean up frame ref */
  7977. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  7978. ctx->frame_ref = ctx->frame_ref_bottom;
  7979. ctx->stack_cell_num = 0;
  7980. #if WASM_ENABLE_GC != 0
  7981. /* clean up reftype map */
  7982. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  7983. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  7984. ctx->reftype_map_num = 0;
  7985. #endif
  7986. /* clean up frame csp */
  7987. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  7988. ctx->frame_csp = ctx->frame_csp_bottom;
  7989. ctx->csp_num = 0;
  7990. ctx->max_csp_num = 0;
  7991. /* clean up frame offset */
  7992. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  7993. ctx->frame_offset = ctx->frame_offset_bottom;
  7994. ctx->dynamic_offset = ctx->start_dynamic_offset;
  7995. /* init preserved local offsets */
  7996. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  7997. /* const buf is reserved */
  7998. return true;
  7999. }
  8000. static void
  8001. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8002. {
  8003. ctx->code_compiled_size += size;
  8004. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8005. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8006. }
  8007. }
  8008. static void
  8009. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8010. {
  8011. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8012. if (ctx->p_code_compiled) {
  8013. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8014. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8015. #endif
  8016. bh_memcpy_s(ctx->p_code_compiled,
  8017. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8018. value, size);
  8019. ctx->p_code_compiled += size;
  8020. }
  8021. else {
  8022. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8023. bh_assert((ctx->code_compiled_size & 1) == 0);
  8024. #endif
  8025. increase_compiled_code_space(ctx, size);
  8026. }
  8027. }
  8028. static void
  8029. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8030. {
  8031. if (ctx->p_code_compiled) {
  8032. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8033. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8034. #endif
  8035. STORE_U32(ctx->p_code_compiled, value);
  8036. ctx->p_code_compiled += sizeof(uint32);
  8037. }
  8038. else {
  8039. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8040. bh_assert((ctx->code_compiled_size & 1) == 0);
  8041. #endif
  8042. increase_compiled_code_space(ctx, sizeof(uint32));
  8043. }
  8044. }
  8045. static void
  8046. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8047. {
  8048. if (ctx->p_code_compiled) {
  8049. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8050. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8051. #endif
  8052. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8053. ctx->p_code_compiled += sizeof(int16);
  8054. }
  8055. else {
  8056. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8057. bh_assert((ctx->code_compiled_size & 1) == 0);
  8058. #endif
  8059. increase_compiled_code_space(ctx, sizeof(uint16));
  8060. }
  8061. }
  8062. static void
  8063. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8064. {
  8065. if (ctx->p_code_compiled) {
  8066. *(ctx->p_code_compiled) = value;
  8067. ctx->p_code_compiled += sizeof(uint8);
  8068. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8069. ctx->p_code_compiled++;
  8070. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8071. #endif
  8072. }
  8073. else {
  8074. increase_compiled_code_space(ctx, sizeof(uint8));
  8075. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8076. increase_compiled_code_space(ctx, sizeof(uint8));
  8077. bh_assert((ctx->code_compiled_size & 1) == 0);
  8078. #endif
  8079. }
  8080. }
  8081. static void
  8082. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8083. {
  8084. if (ctx->p_code_compiled) {
  8085. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8086. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8087. #endif
  8088. STORE_PTR(ctx->p_code_compiled, value);
  8089. ctx->p_code_compiled += sizeof(void *);
  8090. }
  8091. else {
  8092. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8093. bh_assert((ctx->code_compiled_size & 1) == 0);
  8094. #endif
  8095. increase_compiled_code_space(ctx, sizeof(void *));
  8096. }
  8097. }
  8098. static void
  8099. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8100. {
  8101. if (ctx->p_code_compiled) {
  8102. ctx->p_code_compiled -= size;
  8103. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8104. if (size == sizeof(uint8)) {
  8105. ctx->p_code_compiled--;
  8106. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8107. }
  8108. #endif
  8109. }
  8110. else {
  8111. ctx->code_compiled_size -= size;
  8112. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8113. if (size == sizeof(uint8)) {
  8114. ctx->code_compiled_size--;
  8115. bh_assert((ctx->code_compiled_size & 1) == 0);
  8116. }
  8117. #endif
  8118. }
  8119. }
  8120. static bool
  8121. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8122. uint32 local_index, uint32 local_type,
  8123. bool *preserved, char *error_buf,
  8124. uint32 error_buf_size)
  8125. {
  8126. uint32 i = 0;
  8127. int16 preserved_offset = (int16)local_index;
  8128. *preserved = false;
  8129. while (i < loader_ctx->stack_cell_num) {
  8130. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8131. /* move previous local into dynamic space before a set/tee_local opcode
  8132. */
  8133. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8134. if (!(*preserved)) {
  8135. *preserved = true;
  8136. skip_label();
  8137. preserved_offset = loader_ctx->preserved_local_offset;
  8138. if (loader_ctx->p_code_compiled) {
  8139. bh_assert(preserved_offset != (int16)local_index);
  8140. }
  8141. if (is_32bit_type(local_type)) {
  8142. /* Only increase preserve offset in the second traversal */
  8143. if (loader_ctx->p_code_compiled)
  8144. loader_ctx->preserved_local_offset++;
  8145. emit_label(EXT_OP_COPY_STACK_TOP);
  8146. }
  8147. else {
  8148. if (loader_ctx->p_code_compiled)
  8149. loader_ctx->preserved_local_offset += 2;
  8150. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8151. }
  8152. emit_operand(loader_ctx, local_index);
  8153. emit_operand(loader_ctx, preserved_offset);
  8154. emit_label(opcode);
  8155. }
  8156. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8157. }
  8158. if (is_32bit_type(cur_type))
  8159. i++;
  8160. else
  8161. i += 2;
  8162. }
  8163. (void)error_buf;
  8164. (void)error_buf_size;
  8165. return true;
  8166. }
  8167. static bool
  8168. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8169. char *error_buf, uint32 error_buf_size)
  8170. {
  8171. uint32 i = 0;
  8172. bool preserve_local;
  8173. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8174. blocks will not influence the value of these locals */
  8175. while (i < loader_ctx->stack_cell_num) {
  8176. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8177. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8178. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8179. && (cur_offset >= 0)) {
  8180. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8181. cur_type, &preserve_local,
  8182. error_buf, error_buf_size)))
  8183. return false;
  8184. }
  8185. if (is_32bit_type(cur_type)) {
  8186. i++;
  8187. }
  8188. else {
  8189. i += 2;
  8190. }
  8191. }
  8192. return true;
  8193. }
  8194. static bool
  8195. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8196. uint8 *p_code_compiled, char *error_buf,
  8197. uint32 error_buf_size)
  8198. {
  8199. BranchBlockPatch *patch =
  8200. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8201. if (!patch) {
  8202. return false;
  8203. }
  8204. patch->patch_type = patch_type;
  8205. patch->code_compiled = p_code_compiled;
  8206. if (!frame_csp->patch_list) {
  8207. frame_csp->patch_list = patch;
  8208. patch->next = NULL;
  8209. }
  8210. else {
  8211. patch->next = frame_csp->patch_list;
  8212. frame_csp->patch_list = patch;
  8213. }
  8214. return true;
  8215. }
  8216. static void
  8217. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8218. {
  8219. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8220. BranchBlockPatch *node = frame_csp->patch_list;
  8221. BranchBlockPatch *node_prev = NULL, *node_next;
  8222. if (!ctx->p_code_compiled)
  8223. return;
  8224. while (node) {
  8225. node_next = node->next;
  8226. if (node->patch_type == patch_type) {
  8227. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8228. if (node_prev == NULL) {
  8229. frame_csp->patch_list = node_next;
  8230. }
  8231. else {
  8232. node_prev->next = node_next;
  8233. }
  8234. wasm_runtime_free(node);
  8235. }
  8236. else {
  8237. node_prev = node;
  8238. }
  8239. node = node_next;
  8240. }
  8241. }
  8242. static bool
  8243. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8244. bool is_br, char *error_buf, uint32 error_buf_size)
  8245. {
  8246. /* br info layout:
  8247. * a) arity of target block
  8248. * b) total cell num of arity values
  8249. * c) each arity value's cell num
  8250. * d) each arity value's src frame offset
  8251. * e) each arity values's dst dynamic offset
  8252. * f) branch target address
  8253. *
  8254. * Note: b-e are omitted when arity is 0 so that
  8255. * interpreter can recover the br info quickly.
  8256. */
  8257. BlockType *block_type = &frame_csp->block_type;
  8258. uint8 *types = NULL, cell;
  8259. #if WASM_ENABLE_GC != 0
  8260. WASMRefTypeMap *reftype_maps;
  8261. uint32 reftype_map_count;
  8262. #endif
  8263. uint32 arity = 0;
  8264. int32 i;
  8265. int16 *frame_offset = ctx->frame_offset;
  8266. uint16 dynamic_offset;
  8267. /* Note: loop's arity is different from if and block. loop's arity is
  8268. * its parameter count while if and block arity is result count.
  8269. */
  8270. #if WASM_ENABLE_GC == 0
  8271. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8272. arity = block_type_get_param_types(block_type, &types);
  8273. else
  8274. arity = block_type_get_result_types(block_type, &types);
  8275. #else
  8276. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8277. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8278. &reftype_map_count);
  8279. else
  8280. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8281. &reftype_map_count);
  8282. #endif
  8283. /* Part a */
  8284. emit_uint32(ctx, arity);
  8285. if (arity) {
  8286. /* Part b */
  8287. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8288. /* Part c */
  8289. for (i = (int32)arity - 1; i >= 0; i--) {
  8290. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8291. emit_byte(ctx, cell);
  8292. }
  8293. /* Part d */
  8294. for (i = (int32)arity - 1; i >= 0; i--) {
  8295. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8296. frame_offset -= cell;
  8297. emit_operand(ctx, *(int16 *)(frame_offset));
  8298. }
  8299. /* Part e */
  8300. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8301. /* Use start_dynamic_offset which was set in
  8302. copy_params_to_dynamic_space */
  8303. dynamic_offset = frame_csp->start_dynamic_offset
  8304. + wasm_get_cell_num(types, arity);
  8305. else
  8306. dynamic_offset =
  8307. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8308. if (is_br)
  8309. ctx->dynamic_offset = dynamic_offset;
  8310. for (i = (int32)arity - 1; i >= 0; i--) {
  8311. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8312. dynamic_offset -= cell;
  8313. emit_operand(ctx, dynamic_offset);
  8314. }
  8315. }
  8316. /* Part f */
  8317. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8318. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8319. }
  8320. else {
  8321. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8322. error_buf, error_buf_size))
  8323. return false;
  8324. /* label address, to be patched */
  8325. wasm_loader_emit_ptr(ctx, NULL);
  8326. }
  8327. return true;
  8328. }
  8329. static bool
  8330. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8331. bool disable_emit, int16 operand_offset,
  8332. char *error_buf, uint32 error_buf_size)
  8333. {
  8334. uint32 cell_num_to_push, i;
  8335. if (type == VALUE_TYPE_VOID)
  8336. return true;
  8337. /* only check memory overflow in first traverse */
  8338. if (ctx->p_code_compiled == NULL) {
  8339. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8340. return false;
  8341. }
  8342. if (disable_emit)
  8343. *(ctx->frame_offset)++ = operand_offset;
  8344. else {
  8345. emit_operand(ctx, ctx->dynamic_offset);
  8346. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8347. ctx->dynamic_offset++;
  8348. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8349. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8350. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8351. goto fail;
  8352. }
  8353. }
  8354. }
  8355. if (is_32bit_type(type))
  8356. return true;
  8357. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8358. for (i = 0; i < cell_num_to_push; i++) {
  8359. if (ctx->p_code_compiled == NULL) {
  8360. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8361. return false;
  8362. }
  8363. ctx->frame_offset++;
  8364. if (!disable_emit) {
  8365. ctx->dynamic_offset++;
  8366. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8367. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8368. if (ctx->max_dynamic_offset >= INT16_MAX)
  8369. goto fail;
  8370. }
  8371. }
  8372. }
  8373. return true;
  8374. fail:
  8375. set_error_buf(error_buf, error_buf_size,
  8376. "fast interpreter offset overflow");
  8377. return false;
  8378. }
  8379. /* This function should be in front of wasm_loader_pop_frame_ref
  8380. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8381. will be modified by wasm_loader_pop_frame_ref */
  8382. static bool
  8383. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8384. char *error_buf, uint32 error_buf_size)
  8385. {
  8386. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8387. then current block is the function block */
  8388. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8389. BranchBlock *cur_block = ctx->frame_csp - depth;
  8390. int32 available_stack_cell =
  8391. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8392. uint32 cell_num_to_pop;
  8393. /* Directly return success if current block is in stack
  8394. polymorphic state while stack is empty. */
  8395. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8396. return true;
  8397. if (type == VALUE_TYPE_VOID)
  8398. return true;
  8399. /* Change type to ANY when the stack top is ANY, so as to avoid
  8400. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8401. two offsets */
  8402. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8403. type = VALUE_TYPE_ANY;
  8404. cell_num_to_pop = wasm_value_type_cell_num(type);
  8405. /* Check the offset stack bottom to ensure the frame offset
  8406. stack will not go underflow. But we don't thrown error
  8407. and return true here, because the error msg should be
  8408. given in wasm_loader_pop_frame_ref */
  8409. if (!check_offset_pop(ctx, cell_num_to_pop))
  8410. return true;
  8411. ctx->frame_offset -= cell_num_to_pop;
  8412. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8413. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8414. ctx->dynamic_offset -= cell_num_to_pop;
  8415. emit_operand(ctx, *(ctx->frame_offset));
  8416. (void)error_buf;
  8417. (void)error_buf_size;
  8418. return true;
  8419. }
  8420. static bool
  8421. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8422. bool disable_emit, int16 operand_offset,
  8423. char *error_buf, uint32 error_buf_size)
  8424. {
  8425. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8426. error_buf, error_buf_size)))
  8427. return false;
  8428. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8429. return false;
  8430. return true;
  8431. }
  8432. static bool
  8433. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8434. char *error_buf, uint32 error_buf_size)
  8435. {
  8436. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8437. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8438. return false;
  8439. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8440. return false;
  8441. return true;
  8442. }
  8443. static bool
  8444. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8445. uint8 type_push, uint8 type_pop,
  8446. bool disable_emit, int16 operand_offset,
  8447. char *error_buf, uint32 error_buf_size)
  8448. {
  8449. uint8 i;
  8450. for (i = 0; i < pop_cnt; i++) {
  8451. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8452. error_buf_size))
  8453. return false;
  8454. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8455. error_buf_size))
  8456. return false;
  8457. }
  8458. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8459. operand_offset, error_buf,
  8460. error_buf_size))
  8461. return false;
  8462. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8463. return false;
  8464. return true;
  8465. }
  8466. static bool
  8467. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8468. int16 *offset, char *error_buf,
  8469. uint32 error_buf_size)
  8470. {
  8471. int8 bytes_to_increase;
  8472. int16 operand_offset = 0;
  8473. Const *c;
  8474. /* Search existing constant */
  8475. for (c = (Const *)ctx->const_buf;
  8476. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8477. /* TODO: handle v128 type? */
  8478. if ((type == c->value_type)
  8479. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8480. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8481. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8482. || (type == VALUE_TYPE_FUNCREF
  8483. && *(int32 *)value == c->value.i32)
  8484. || (type == VALUE_TYPE_EXTERNREF
  8485. && *(int32 *)value == c->value.i32)
  8486. #endif
  8487. || (type == VALUE_TYPE_F64
  8488. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8489. || (type == VALUE_TYPE_F32
  8490. && (0
  8491. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8492. operand_offset = c->slot_index;
  8493. break;
  8494. }
  8495. if (is_32bit_type(c->value_type))
  8496. operand_offset += 1;
  8497. else
  8498. operand_offset += 2;
  8499. }
  8500. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8501. /* New constant, append to the const buffer */
  8502. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8503. bytes_to_increase = 2;
  8504. }
  8505. else {
  8506. bytes_to_increase = 1;
  8507. }
  8508. /* The max cell num of const buffer is 32768 since the valid index range
  8509. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8510. * full */
  8511. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8512. *offset = 0;
  8513. return true;
  8514. }
  8515. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8516. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8517. ctx->const_buf_size + 4 * sizeof(Const));
  8518. ctx->const_buf_size += 4 * sizeof(Const);
  8519. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8520. }
  8521. c->value_type = type;
  8522. switch (type) {
  8523. case VALUE_TYPE_F64:
  8524. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8525. sizeof(float64));
  8526. ctx->const_cell_num += 2;
  8527. /* The const buf will be reversed, we use the second cell */
  8528. /* of the i64/f64 const so the final offset is correct */
  8529. operand_offset++;
  8530. break;
  8531. case VALUE_TYPE_I64:
  8532. c->value.i64 = *(int64 *)value;
  8533. ctx->const_cell_num += 2;
  8534. operand_offset++;
  8535. break;
  8536. case VALUE_TYPE_F32:
  8537. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8538. sizeof(float32));
  8539. ctx->const_cell_num++;
  8540. break;
  8541. case VALUE_TYPE_I32:
  8542. c->value.i32 = *(int32 *)value;
  8543. ctx->const_cell_num++;
  8544. break;
  8545. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8546. case VALUE_TYPE_EXTERNREF:
  8547. case VALUE_TYPE_FUNCREF:
  8548. c->value.i32 = *(int32 *)value;
  8549. ctx->const_cell_num++;
  8550. break;
  8551. #endif
  8552. default:
  8553. break;
  8554. }
  8555. c->slot_index = operand_offset;
  8556. ctx->num_const++;
  8557. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8558. (int64)c->value.i64);
  8559. }
  8560. /* use negative index for const */
  8561. operand_offset = -(operand_offset + 1);
  8562. *offset = operand_offset;
  8563. return true;
  8564. fail:
  8565. return false;
  8566. }
  8567. /*
  8568. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8569. -- Mostly used for the binary / compare operation
  8570. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8571. -- Mostly used in block / control instructions
  8572. The POP will always emit the offset on the top of the frame_offset stack
  8573. PUSH can be used in two ways:
  8574. 1. directly PUSH:
  8575. PUSH_XXX();
  8576. will allocate a dynamic space and emit
  8577. 2. silent PUSH:
  8578. operand_offset = xxx; disable_emit = true;
  8579. PUSH_XXX();
  8580. only push the frame_offset stack, no emit
  8581. */
  8582. #define TEMPLATE_PUSH(Type) \
  8583. do { \
  8584. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8585. disable_emit, operand_offset, \
  8586. error_buf, error_buf_size)) \
  8587. goto fail; \
  8588. } while (0)
  8589. #define TEMPLATE_PUSH_REF(Type) \
  8590. do { \
  8591. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8592. operand_offset, error_buf, \
  8593. error_buf_size)) \
  8594. goto fail; \
  8595. } while (0)
  8596. #define TEMPLATE_POP(Type) \
  8597. do { \
  8598. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8599. error_buf, error_buf_size)) \
  8600. goto fail; \
  8601. } while (0)
  8602. #define TEMPLATE_POP_REF(Type) \
  8603. do { \
  8604. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8605. error_buf_size)) \
  8606. goto fail; \
  8607. } while (0)
  8608. #define PUSH_OFFSET_TYPE(type) \
  8609. do { \
  8610. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8611. operand_offset, error_buf, \
  8612. error_buf_size))) \
  8613. goto fail; \
  8614. } while (0)
  8615. #define POP_OFFSET_TYPE(type) \
  8616. do { \
  8617. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8618. error_buf_size))) \
  8619. goto fail; \
  8620. } while (0)
  8621. #define POP_AND_PUSH(type_pop, type_push) \
  8622. do { \
  8623. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8624. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8625. operand_offset, error_buf, error_buf_size))) \
  8626. goto fail; \
  8627. } while (0)
  8628. /* type of POPs should be the same */
  8629. #define POP2_AND_PUSH(type_pop, type_push) \
  8630. do { \
  8631. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8632. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8633. operand_offset, error_buf, error_buf_size))) \
  8634. goto fail; \
  8635. } while (0)
  8636. #else /* WASM_ENABLE_FAST_INTERP */
  8637. #define TEMPLATE_PUSH(Type) \
  8638. do { \
  8639. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8640. error_buf, error_buf_size))) \
  8641. goto fail; \
  8642. } while (0)
  8643. #define TEMPLATE_PUSH_REF(Type) \
  8644. do { \
  8645. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8646. error_buf_size))) \
  8647. goto fail; \
  8648. } while (0)
  8649. #define TEMPLATE_POP(Type) \
  8650. do { \
  8651. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8652. error_buf, error_buf_size))) \
  8653. goto fail; \
  8654. } while (0)
  8655. #define TEMPLATE_POP_REF(Type) \
  8656. do { \
  8657. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8658. error_buf_size))) \
  8659. goto fail; \
  8660. } while (0)
  8661. #define POP_AND_PUSH(type_pop, type_push) \
  8662. do { \
  8663. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8664. type_pop, error_buf, \
  8665. error_buf_size))) \
  8666. goto fail; \
  8667. } while (0)
  8668. /* type of POPs should be the same */
  8669. #define POP2_AND_PUSH(type_pop, type_push) \
  8670. do { \
  8671. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8672. type_pop, error_buf, \
  8673. error_buf_size))) \
  8674. goto fail; \
  8675. } while (0)
  8676. #endif /* WASM_ENABLE_FAST_INTERP */
  8677. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8678. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8679. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8680. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8681. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8682. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8683. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8684. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8685. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8686. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8687. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8688. #define POP_I32() TEMPLATE_POP(I32)
  8689. #define POP_F32() TEMPLATE_POP(F32)
  8690. #define POP_I64() TEMPLATE_POP(I64)
  8691. #define POP_F64() TEMPLATE_POP(F64)
  8692. #define POP_V128() TEMPLATE_POP(V128)
  8693. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8694. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8695. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8696. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8697. #if WASM_ENABLE_FAST_INTERP != 0
  8698. static bool
  8699. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8700. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8701. {
  8702. int16 operand_offset = 0;
  8703. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8704. : loader_ctx->frame_csp;
  8705. BlockType *block_type = &block->block_type;
  8706. uint8 *return_types = NULL;
  8707. #if WASM_ENABLE_GC != 0
  8708. WASMRefTypeMap *reftype_maps = NULL;
  8709. uint32 reftype_map_count;
  8710. #endif
  8711. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8712. int32 i = 0;
  8713. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8714. *frame_offset_org = NULL;
  8715. #if WASM_ENABLE_GC == 0
  8716. return_count = block_type_get_result_types(block_type, &return_types);
  8717. #else
  8718. return_count = block_type_get_result_types(
  8719. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8720. #endif
  8721. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8722. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8723. if (return_count == 1) {
  8724. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8725. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8726. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8727. /* insert op_copy before else opcode */
  8728. if (opcode == WASM_OP_ELSE)
  8729. skip_label();
  8730. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8731. : EXT_OP_COPY_STACK_TOP_I64);
  8732. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8733. emit_operand(loader_ctx, block->dynamic_offset);
  8734. if (opcode == WASM_OP_ELSE) {
  8735. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8736. }
  8737. else {
  8738. loader_ctx->frame_offset -= cell;
  8739. loader_ctx->dynamic_offset = block->dynamic_offset;
  8740. PUSH_OFFSET_TYPE(return_types[0]);
  8741. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8742. }
  8743. if (opcode == WASM_OP_ELSE)
  8744. emit_label(opcode);
  8745. }
  8746. return true;
  8747. }
  8748. /* Copy stack top values to block's results which are in dynamic space.
  8749. * The instruction format:
  8750. * Part a: values count
  8751. * Part b: all values total cell num
  8752. * Part c: each value's cell_num, src offset and dst offset
  8753. * Part d: each value's src offset and dst offset
  8754. * Part e: each value's dst offset
  8755. */
  8756. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8757. dynamic_offset = dynamic_offset_org =
  8758. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8759. /* First traversal to get the count of values needed to be copied. */
  8760. for (i = (int32)return_count - 1; i >= 0; i--) {
  8761. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8762. frame_offset -= cells;
  8763. dynamic_offset -= cells;
  8764. if (dynamic_offset != *frame_offset) {
  8765. value_count++;
  8766. total_cel_num += cells;
  8767. }
  8768. }
  8769. if (value_count) {
  8770. uint32 j = 0;
  8771. uint8 *emit_data = NULL, *cells = NULL;
  8772. int16 *src_offsets = NULL;
  8773. uint16 *dst_offsets = NULL;
  8774. uint64 size =
  8775. (uint64)value_count
  8776. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8777. /* Allocate memory for the emit data */
  8778. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8779. return false;
  8780. cells = emit_data;
  8781. src_offsets = (int16 *)(cells + value_count);
  8782. dst_offsets = (uint16 *)(src_offsets + value_count);
  8783. /* insert op_copy before else opcode */
  8784. if (opcode == WASM_OP_ELSE)
  8785. skip_label();
  8786. emit_label(EXT_OP_COPY_STACK_VALUES);
  8787. /* Part a) */
  8788. emit_uint32(loader_ctx, value_count);
  8789. /* Part b) */
  8790. emit_uint32(loader_ctx, total_cel_num);
  8791. /* Second traversal to get each value's cell num, src offset and dst
  8792. * offset. */
  8793. frame_offset = frame_offset_org;
  8794. dynamic_offset = dynamic_offset_org;
  8795. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8796. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8797. frame_offset -= cell;
  8798. dynamic_offset -= cell;
  8799. if (dynamic_offset != *frame_offset) {
  8800. /* cell num */
  8801. cells[j] = cell;
  8802. /* src offset */
  8803. src_offsets[j] = *frame_offset;
  8804. /* dst offset */
  8805. dst_offsets[j] = dynamic_offset;
  8806. j++;
  8807. }
  8808. if (opcode == WASM_OP_ELSE) {
  8809. *frame_offset = dynamic_offset;
  8810. }
  8811. else {
  8812. loader_ctx->frame_offset = frame_offset;
  8813. loader_ctx->dynamic_offset = dynamic_offset;
  8814. PUSH_OFFSET_TYPE(return_types[i]);
  8815. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8816. loader_ctx->frame_offset = frame_offset_org;
  8817. loader_ctx->dynamic_offset = dynamic_offset_org;
  8818. }
  8819. }
  8820. bh_assert(j == value_count);
  8821. /* Emit the cells, src_offsets and dst_offsets */
  8822. for (j = 0; j < value_count; j++)
  8823. emit_byte(loader_ctx, cells[j]);
  8824. for (j = 0; j < value_count; j++)
  8825. emit_operand(loader_ctx, src_offsets[j]);
  8826. for (j = 0; j < value_count; j++)
  8827. emit_operand(loader_ctx, dst_offsets[j]);
  8828. if (opcode == WASM_OP_ELSE)
  8829. emit_label(opcode);
  8830. wasm_runtime_free(emit_data);
  8831. }
  8832. return true;
  8833. fail:
  8834. return false;
  8835. }
  8836. #endif /* WASM_ENABLE_FAST_INTERP */
  8837. #define RESERVE_BLOCK_RET() \
  8838. do { \
  8839. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  8840. error_buf_size)) \
  8841. goto fail; \
  8842. } while (0)
  8843. #define PUSH_TYPE(type) \
  8844. do { \
  8845. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8846. error_buf_size))) \
  8847. goto fail; \
  8848. } while (0)
  8849. #define POP_TYPE(type) \
  8850. do { \
  8851. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8852. error_buf_size))) \
  8853. goto fail; \
  8854. } while (0)
  8855. #if WASM_ENABLE_GC == 0
  8856. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8857. do { \
  8858. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8859. _start_addr, error_buf, \
  8860. error_buf_size)) \
  8861. goto fail; \
  8862. } while (0)
  8863. #define POP_CSP() \
  8864. do { \
  8865. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8866. goto fail; \
  8867. } while (0)
  8868. #else
  8869. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8870. do { \
  8871. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8872. _start_addr, error_buf, \
  8873. error_buf_size)) \
  8874. goto fail; \
  8875. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8876. error_buf, error_buf_size)) { \
  8877. goto fail; \
  8878. } \
  8879. } while (0)
  8880. #define POP_CSP() \
  8881. do { \
  8882. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8883. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8884. goto fail; \
  8885. } while (0)
  8886. #endif /* end of WASM_ENABLE_GC == 0 */
  8887. #if WASM_ENABLE_GC == 0
  8888. #define GET_LOCAL_REFTYPE() (void)0
  8889. #else
  8890. #define GET_LOCAL_REFTYPE() \
  8891. do { \
  8892. if (wasm_is_type_multi_byte_type(local_type)) { \
  8893. WASMRefType *_ref_type; \
  8894. if (local_idx < param_count) \
  8895. _ref_type = wasm_reftype_map_find( \
  8896. param_reftype_maps, param_reftype_map_count, local_idx); \
  8897. else \
  8898. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8899. local_reftype_map_count, \
  8900. local_idx - param_count); \
  8901. bh_assert(_ref_type); \
  8902. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8903. wasm_reftype_struct_size(_ref_type)); \
  8904. } \
  8905. } while (0)
  8906. #endif /* end of WASM_ENABLE_GC == 0 */
  8907. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8908. do { \
  8909. read_leb_uint32(p, p_end, local_idx); \
  8910. if (local_idx >= param_count + local_count) { \
  8911. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8912. goto fail; \
  8913. } \
  8914. local_type = local_idx < param_count \
  8915. ? param_types[local_idx] \
  8916. : local_types[local_idx - param_count]; \
  8917. local_offset = local_offsets[local_idx]; \
  8918. GET_LOCAL_REFTYPE(); \
  8919. } while (0)
  8920. static bool
  8921. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8922. {
  8923. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8924. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8925. return false;
  8926. }
  8927. return true;
  8928. }
  8929. #define CHECK_MEMORY() \
  8930. do { \
  8931. if (!check_memory(module, error_buf, error_buf_size)) \
  8932. goto fail; \
  8933. } while (0)
  8934. static bool
  8935. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8936. uint32 error_buf_size)
  8937. {
  8938. uint8 mem_access_aligns[] = {
  8939. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8940. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8941. };
  8942. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8943. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8944. set_error_buf(error_buf, error_buf_size,
  8945. "alignment must not be larger than natural");
  8946. return false;
  8947. }
  8948. return true;
  8949. }
  8950. #if WASM_ENABLE_SIMD != 0
  8951. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8952. static bool
  8953. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8954. uint32 error_buf_size)
  8955. {
  8956. uint8 mem_access_aligns[] = {
  8957. 4, /* load */
  8958. 3, 3, 3, 3, 3, 3, /* load and extend */
  8959. 0, 1, 2, 3, /* load and splat */
  8960. 4, /* store */
  8961. };
  8962. uint8 mem_access_aligns_load_lane[] = {
  8963. 0, 1, 2, 3, /* load lane */
  8964. 0, 1, 2, 3, /* store lane */
  8965. 2, 3 /* store zero */
  8966. };
  8967. if (!((opcode <= SIMD_v128_store)
  8968. || (SIMD_v128_load8_lane <= opcode
  8969. && opcode <= SIMD_v128_load64_zero))) {
  8970. set_error_buf(error_buf, error_buf_size,
  8971. "the opcode doesn't include memarg");
  8972. return false;
  8973. }
  8974. if ((opcode <= SIMD_v128_store
  8975. && align > mem_access_aligns[opcode - SIMD_v128_load])
  8976. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  8977. && align > mem_access_aligns_load_lane[opcode
  8978. - SIMD_v128_load8_lane])) {
  8979. set_error_buf(error_buf, error_buf_size,
  8980. "alignment must not be larger than natural");
  8981. return false;
  8982. }
  8983. return true;
  8984. }
  8985. static bool
  8986. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  8987. uint32 error_buf_size)
  8988. {
  8989. switch (opcode) {
  8990. case SIMD_i8x16_extract_lane_s:
  8991. case SIMD_i8x16_extract_lane_u:
  8992. case SIMD_i8x16_replace_lane:
  8993. if (lane >= 16) {
  8994. goto fail;
  8995. }
  8996. break;
  8997. case SIMD_i16x8_extract_lane_s:
  8998. case SIMD_i16x8_extract_lane_u:
  8999. case SIMD_i16x8_replace_lane:
  9000. if (lane >= 8) {
  9001. goto fail;
  9002. }
  9003. break;
  9004. case SIMD_i32x4_extract_lane:
  9005. case SIMD_i32x4_replace_lane:
  9006. case SIMD_f32x4_extract_lane:
  9007. case SIMD_f32x4_replace_lane:
  9008. if (lane >= 4) {
  9009. goto fail;
  9010. }
  9011. break;
  9012. case SIMD_i64x2_extract_lane:
  9013. case SIMD_i64x2_replace_lane:
  9014. case SIMD_f64x2_extract_lane:
  9015. case SIMD_f64x2_replace_lane:
  9016. if (lane >= 2) {
  9017. goto fail;
  9018. }
  9019. break;
  9020. case SIMD_v128_load8_lane:
  9021. case SIMD_v128_load16_lane:
  9022. case SIMD_v128_load32_lane:
  9023. case SIMD_v128_load64_lane:
  9024. case SIMD_v128_store8_lane:
  9025. case SIMD_v128_store16_lane:
  9026. case SIMD_v128_store32_lane:
  9027. case SIMD_v128_store64_lane:
  9028. case SIMD_v128_load32_zero:
  9029. case SIMD_v128_load64_zero:
  9030. {
  9031. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9032. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9033. goto fail;
  9034. }
  9035. break;
  9036. }
  9037. default:
  9038. goto fail;
  9039. }
  9040. return true;
  9041. fail:
  9042. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9043. return false;
  9044. }
  9045. static bool
  9046. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9047. {
  9048. uint8 i;
  9049. for (i = 0; i != 16; ++i) {
  9050. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9051. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9052. return false;
  9053. }
  9054. }
  9055. return true;
  9056. }
  9057. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9058. #endif /* end of WASM_ENABLE_SIMD */
  9059. #if WASM_ENABLE_SHARED_MEMORY != 0
  9060. static bool
  9061. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9062. uint32 error_buf_size)
  9063. {
  9064. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9065. uint8 mem_access_aligns[] = {
  9066. 2, 3, 0, 1, 0, 1, 2,
  9067. };
  9068. uint8 expect;
  9069. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9070. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9071. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9072. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9073. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9074. }
  9075. else {
  9076. /* 7 opcodes in every group */
  9077. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9078. }
  9079. if (align != expect) {
  9080. set_error_buf(error_buf, error_buf_size,
  9081. "alignment isn't equal to natural");
  9082. return false;
  9083. }
  9084. return true;
  9085. }
  9086. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9087. static bool
  9088. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9089. char *error_buf, uint32 error_buf_size)
  9090. {
  9091. BranchBlock *target_block, *cur_block;
  9092. BlockType *target_block_type;
  9093. uint8 type, *types = NULL, *frame_ref;
  9094. uint32 arity = 0;
  9095. int32 i, available_stack_cell;
  9096. uint16 cell_num;
  9097. #if WASM_ENABLE_GC != 0
  9098. WASMRefTypeMap *frame_reftype_map;
  9099. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9100. WASMRefType *ref_type;
  9101. uint32 reftype_map_count = 0;
  9102. int32 available_reftype_map;
  9103. bool is_type_multi_byte;
  9104. #endif
  9105. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9106. uint8 *frame_ref_after_popped = NULL;
  9107. uint8 frame_ref_tmp[4] = { 0 };
  9108. uint8 *frame_ref_buf = frame_ref_tmp;
  9109. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9110. #if WASM_ENABLE_GC != 0
  9111. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9112. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9113. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9114. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9115. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9116. #endif
  9117. #if WASM_ENABLE_FAST_INTERP != 0
  9118. int16 *frame_offset_old = loader_ctx->frame_offset;
  9119. int16 *frame_offset_after_popped = NULL;
  9120. int16 frame_offset_tmp[4] = { 0 };
  9121. int16 *frame_offset_buf = frame_offset_tmp;
  9122. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9123. #endif
  9124. bool ret = false;
  9125. bh_assert(loader_ctx->csp_num > 0);
  9126. if (loader_ctx->csp_num - 1 < depth) {
  9127. set_error_buf(error_buf, error_buf_size,
  9128. "unknown label, "
  9129. "unexpected end of section or function");
  9130. return false;
  9131. }
  9132. cur_block = loader_ctx->frame_csp - 1;
  9133. target_block = loader_ctx->frame_csp - (depth + 1);
  9134. target_block_type = &target_block->block_type;
  9135. frame_ref = loader_ctx->frame_ref;
  9136. #if WASM_ENABLE_GC != 0
  9137. frame_reftype_map = loader_ctx->frame_reftype_map;
  9138. #endif
  9139. /* Note: loop's arity is different from if and block. loop's arity is
  9140. * its parameter count while if and block arity is result count.
  9141. */
  9142. #if WASM_ENABLE_GC == 0
  9143. if (target_block->label_type == LABEL_TYPE_LOOP)
  9144. arity = block_type_get_param_types(target_block_type, &types);
  9145. else
  9146. arity = block_type_get_result_types(target_block_type, &types);
  9147. #else
  9148. if (target_block->label_type == LABEL_TYPE_LOOP)
  9149. arity = block_type_get_param_types(target_block_type, &types,
  9150. &reftype_maps, &reftype_map_count);
  9151. else
  9152. arity = block_type_get_result_types(target_block_type, &types,
  9153. &reftype_maps, &reftype_map_count);
  9154. #endif
  9155. /* If the stack is in polymorphic state, just clear the stack
  9156. * and then re-push the values to make the stack top values
  9157. * match block type. */
  9158. if (cur_block->is_stack_polymorphic) {
  9159. #if WASM_ENABLE_GC != 0
  9160. int32 j = reftype_map_count - 1;
  9161. #endif
  9162. for (i = (int32)arity - 1; i >= 0; i--) {
  9163. #if WASM_ENABLE_GC != 0
  9164. if (wasm_is_type_multi_byte_type(types[i])) {
  9165. bh_assert(reftype_maps[j].index == i);
  9166. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9167. reftype_maps[j].ref_type,
  9168. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9169. j--;
  9170. }
  9171. #endif
  9172. #if WASM_ENABLE_FAST_INTERP != 0
  9173. POP_OFFSET_TYPE(types[i]);
  9174. #endif
  9175. POP_TYPE(types[i]);
  9176. }
  9177. /* Backup stack data since it may be changed in the below
  9178. push operations, and the stack data may be used when
  9179. checking other target blocks of opcode br_table */
  9180. if (opcode == WASM_OP_BR_TABLE) {
  9181. uint64 total_size;
  9182. frame_ref_after_popped = loader_ctx->frame_ref;
  9183. total_size = (uint64)sizeof(uint8)
  9184. * (frame_ref_old - frame_ref_after_popped);
  9185. if (total_size > sizeof(frame_ref_tmp)
  9186. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9187. error_buf_size))) {
  9188. goto fail;
  9189. }
  9190. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9191. frame_ref_after_popped, (uint32)total_size);
  9192. #if WASM_ENABLE_GC != 0
  9193. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9194. total_size =
  9195. (uint64)sizeof(WASMRefTypeMap)
  9196. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9197. if (total_size > sizeof(frame_reftype_map_tmp)
  9198. && !(frame_reftype_map_buf = loader_malloc(
  9199. total_size, error_buf, error_buf_size))) {
  9200. goto fail;
  9201. }
  9202. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9203. frame_reftype_map_after_popped, (uint32)total_size);
  9204. #endif
  9205. #if WASM_ENABLE_FAST_INTERP != 0
  9206. frame_offset_after_popped = loader_ctx->frame_offset;
  9207. total_size = (uint64)sizeof(int16)
  9208. * (frame_offset_old - frame_offset_after_popped);
  9209. if (total_size > sizeof(frame_offset_tmp)
  9210. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9211. error_buf_size))) {
  9212. goto fail;
  9213. }
  9214. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9215. frame_offset_after_popped, (uint32)total_size);
  9216. #endif
  9217. }
  9218. #if WASM_ENABLE_GC != 0
  9219. j = 0;
  9220. #endif
  9221. for (i = 0; i < (int32)arity; i++) {
  9222. #if WASM_ENABLE_GC != 0
  9223. if (wasm_is_type_multi_byte_type(types[i])) {
  9224. bh_assert(reftype_maps[j].index == i);
  9225. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9226. reftype_maps[j].ref_type,
  9227. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9228. j++;
  9229. }
  9230. #endif
  9231. #if WASM_ENABLE_FAST_INTERP != 0
  9232. bool disable_emit = true;
  9233. int16 operand_offset = 0;
  9234. PUSH_OFFSET_TYPE(types[i]);
  9235. #endif
  9236. PUSH_TYPE(types[i]);
  9237. }
  9238. #if WASM_ENABLE_FAST_INTERP != 0
  9239. emit_br_info(target_block, opcode == WASM_OP_BR);
  9240. #endif
  9241. /* Restore the stack data, note that frame_ref_bottom,
  9242. frame_reftype_map_bottom, frame_offset_bottom may be
  9243. re-allocated in the above push operations */
  9244. if (opcode == WASM_OP_BR_TABLE) {
  9245. uint32 total_size;
  9246. /* The stack operand num should not be smaller than before
  9247. after pop and push operations */
  9248. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9249. loader_ctx->stack_cell_num = stack_cell_num_old;
  9250. loader_ctx->frame_ref =
  9251. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9252. total_size = (uint32)(sizeof(uint8)
  9253. * (frame_ref_old - frame_ref_after_popped));
  9254. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9255. frame_ref_buf, total_size);
  9256. #if WASM_ENABLE_GC != 0
  9257. /* The stack operand num should not be smaller than before
  9258. after pop and push operations */
  9259. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9260. loader_ctx->reftype_map_num = reftype_map_num_old;
  9261. loader_ctx->frame_reftype_map =
  9262. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9263. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9264. * (frame_reftype_map_old
  9265. - frame_reftype_map_after_popped));
  9266. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9267. total_size, frame_reftype_map_buf, total_size);
  9268. #endif
  9269. #if WASM_ENABLE_FAST_INTERP != 0
  9270. loader_ctx->frame_offset =
  9271. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9272. total_size =
  9273. (uint32)(sizeof(int16)
  9274. * (frame_offset_old - frame_offset_after_popped));
  9275. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9276. total_size, frame_offset_buf, total_size);
  9277. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9278. #endif
  9279. }
  9280. ret = true;
  9281. goto cleanup_and_return;
  9282. }
  9283. available_stack_cell =
  9284. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9285. #if WASM_ENABLE_GC != 0
  9286. available_reftype_map =
  9287. (int32)(loader_ctx->reftype_map_num
  9288. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9289. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9290. #endif
  9291. /* Check stack top values match target block type */
  9292. for (i = (int32)arity - 1; i >= 0; i--) {
  9293. type = types[i];
  9294. #if WASM_ENABLE_GC != 0
  9295. ref_type = NULL;
  9296. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9297. if (is_type_multi_byte) {
  9298. bh_assert(reftype_map);
  9299. ref_type = reftype_map->ref_type;
  9300. }
  9301. #endif
  9302. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9303. break;
  9304. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9305. #if WASM_ENABLE_GC != 0
  9306. frame_reftype_map, available_reftype_map,
  9307. #endif
  9308. type,
  9309. #if WASM_ENABLE_GC != 0
  9310. ref_type,
  9311. #endif
  9312. error_buf, error_buf_size)) {
  9313. goto fail;
  9314. }
  9315. cell_num = wasm_value_type_cell_num(types[i]);
  9316. frame_ref -= cell_num;
  9317. available_stack_cell -= cell_num;
  9318. #if WASM_ENABLE_GC != 0
  9319. if (is_type_multi_byte) {
  9320. frame_reftype_map--;
  9321. available_reftype_map--;
  9322. reftype_map--;
  9323. }
  9324. #endif
  9325. }
  9326. #if WASM_ENABLE_FAST_INTERP != 0
  9327. emit_br_info(target_block, opcode == WASM_OP_BR);
  9328. #endif
  9329. ret = true;
  9330. cleanup_and_return:
  9331. fail:
  9332. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9333. wasm_runtime_free(frame_ref_buf);
  9334. #if WASM_ENABLE_GC != 0
  9335. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9336. wasm_runtime_free(frame_reftype_map_buf);
  9337. #endif
  9338. #if WASM_ENABLE_FAST_INTERP != 0
  9339. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9340. wasm_runtime_free(frame_offset_buf);
  9341. #endif
  9342. return ret;
  9343. }
  9344. static BranchBlock *
  9345. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9346. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9347. {
  9348. uint8 *p = *p_buf, *p_end = buf_end;
  9349. BranchBlock *frame_csp_tmp;
  9350. uint32 depth;
  9351. read_leb_uint32(p, p_end, depth);
  9352. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9353. error_buf_size)) {
  9354. goto fail;
  9355. }
  9356. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9357. *p_buf = p;
  9358. return frame_csp_tmp;
  9359. fail:
  9360. return NULL;
  9361. }
  9362. #if WASM_ENABLE_EXCE_HANDLING != 0
  9363. static BranchBlock *
  9364. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9365. uint8 *buf_end, char *error_buf,
  9366. uint32 error_buf_size)
  9367. {
  9368. uint8 *p = *p_buf, *p_end = buf_end;
  9369. BranchBlock *frame_csp_tmp;
  9370. uint32 depth;
  9371. read_leb_uint32(p, p_end, depth);
  9372. /*
  9373. * Note: "delegate 0" means the surrounding block, not the
  9374. * try-delegate block itself.
  9375. *
  9376. * Note: the caller hasn't popped the try-delegate frame yet.
  9377. */
  9378. bh_assert(loader_ctx->csp_num > 0);
  9379. if (loader_ctx->csp_num - 1 <= depth) {
  9380. #if WASM_ENABLE_SPEC_TEST == 0
  9381. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9382. #else
  9383. set_error_buf(error_buf, error_buf_size, "unknown label");
  9384. #endif
  9385. goto fail;
  9386. }
  9387. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9388. #if WASM_ENABLE_FAST_INTERP != 0
  9389. emit_br_info(frame_csp_tmp, false);
  9390. #endif
  9391. *p_buf = p;
  9392. return frame_csp_tmp;
  9393. fail:
  9394. return NULL;
  9395. }
  9396. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9397. static bool
  9398. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9399. char *error_buf, uint32 error_buf_size)
  9400. {
  9401. BlockType *block_type = &block->block_type;
  9402. uint8 *return_types = NULL;
  9403. uint32 return_count = 0;
  9404. int32 available_stack_cell, return_cell_num, i;
  9405. uint8 *frame_ref = NULL;
  9406. #if WASM_ENABLE_GC != 0
  9407. WASMRefTypeMap *frame_reftype_map;
  9408. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9409. WASMRefType *ref_type;
  9410. uint32 param_count, return_reftype_map_count = 0;
  9411. int32 available_reftype_map =
  9412. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9413. #endif
  9414. available_stack_cell =
  9415. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9416. #if WASM_ENABLE_GC == 0
  9417. return_count = block_type_get_result_types(block_type, &return_types);
  9418. #else
  9419. return_count = block_type_get_result_types(block_type, &return_types,
  9420. &return_reftype_maps,
  9421. &return_reftype_map_count);
  9422. param_count =
  9423. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9424. (void)param_count;
  9425. #endif
  9426. return_cell_num =
  9427. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9428. /* If the stack is in polymorphic state, just clear the stack
  9429. * and then re-push the values to make the stack top values
  9430. * match block type. */
  9431. if (block->is_stack_polymorphic) {
  9432. #if WASM_ENABLE_GC != 0
  9433. int32 j = return_reftype_map_count - 1;
  9434. #endif
  9435. for (i = (int32)return_count - 1; i >= 0; i--) {
  9436. #if WASM_ENABLE_GC != 0
  9437. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9438. bh_assert(return_reftype_maps[j].index == i + param_count);
  9439. bh_memcpy_s(
  9440. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9441. return_reftype_maps[j].ref_type,
  9442. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9443. j--;
  9444. }
  9445. #endif
  9446. #if WASM_ENABLE_FAST_INTERP != 0
  9447. POP_OFFSET_TYPE(return_types[i]);
  9448. #endif
  9449. POP_TYPE(return_types[i]);
  9450. }
  9451. /* Check stack is empty */
  9452. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9453. set_error_buf(
  9454. error_buf, error_buf_size,
  9455. "type mismatch: stack size does not match block type");
  9456. goto fail;
  9457. }
  9458. #if WASM_ENABLE_GC != 0
  9459. j = 0;
  9460. #endif
  9461. for (i = 0; i < (int32)return_count; i++) {
  9462. #if WASM_ENABLE_GC != 0
  9463. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9464. bh_assert(return_reftype_maps[j].index == i + param_count);
  9465. bh_memcpy_s(
  9466. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9467. return_reftype_maps[j].ref_type,
  9468. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9469. j++;
  9470. }
  9471. #endif
  9472. #if WASM_ENABLE_FAST_INTERP != 0
  9473. bool disable_emit = true;
  9474. int16 operand_offset = 0;
  9475. PUSH_OFFSET_TYPE(return_types[i]);
  9476. #endif
  9477. PUSH_TYPE(return_types[i]);
  9478. }
  9479. return true;
  9480. }
  9481. if (available_stack_cell != return_cell_num) {
  9482. #if WASM_ENABLE_EXCE_HANDLING != 0
  9483. /* testspec: this error message format is expected by try_catch.wast */
  9484. snprintf(
  9485. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9486. block->label_type == LABEL_TYPE_TRY
  9487. || (block->label_type == LABEL_TYPE_CATCH
  9488. && return_cell_num > 0)
  9489. ? "instruction"
  9490. : "block",
  9491. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9492. " but stack has ",
  9493. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9494. : "");
  9495. goto fail;
  9496. #else
  9497. set_error_buf(error_buf, error_buf_size,
  9498. "type mismatch: stack size does not match block type");
  9499. goto fail;
  9500. #endif
  9501. }
  9502. /* Check stack values match return types */
  9503. frame_ref = loader_ctx->frame_ref;
  9504. #if WASM_ENABLE_GC != 0
  9505. frame_reftype_map = loader_ctx->frame_reftype_map;
  9506. return_reftype_map =
  9507. return_reftype_map_count
  9508. ? return_reftype_maps + return_reftype_map_count - 1
  9509. : NULL;
  9510. #endif
  9511. for (i = (int32)return_count - 1; i >= 0; i--) {
  9512. uint8 type = return_types[i];
  9513. #if WASM_ENABLE_GC != 0
  9514. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9515. ref_type = NULL;
  9516. if (is_type_multi_byte) {
  9517. bh_assert(return_reftype_map);
  9518. ref_type = return_reftype_map->ref_type;
  9519. }
  9520. #endif
  9521. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9522. #if WASM_ENABLE_GC != 0
  9523. frame_reftype_map, available_reftype_map,
  9524. #endif
  9525. type,
  9526. #if WASM_ENABLE_GC != 0
  9527. ref_type,
  9528. #endif
  9529. error_buf, error_buf_size))
  9530. return false;
  9531. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9532. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9533. #if WASM_ENABLE_GC != 0
  9534. if (is_type_multi_byte) {
  9535. frame_reftype_map--;
  9536. available_reftype_map--;
  9537. return_reftype_map--;
  9538. }
  9539. #endif
  9540. }
  9541. return true;
  9542. fail:
  9543. return false;
  9544. }
  9545. #if WASM_ENABLE_FAST_INTERP != 0
  9546. /* Copy parameters to dynamic space.
  9547. * 1) POP original parameter out;
  9548. * 2) Push and copy original values to dynamic space.
  9549. * The copy instruction format:
  9550. * Part a: param count
  9551. * Part b: all param total cell num
  9552. * Part c: each param's cell_num, src offset and dst offset
  9553. * Part d: each param's src offset
  9554. * Part e: each param's dst offset
  9555. */
  9556. static bool
  9557. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9558. uint32 error_buf_size)
  9559. {
  9560. bool ret = false;
  9561. int16 *frame_offset = NULL;
  9562. uint8 *cells = NULL, cell;
  9563. int16 *src_offsets = NULL;
  9564. uint8 *emit_data = NULL;
  9565. uint32 i;
  9566. BranchBlock *block = loader_ctx->frame_csp - 1;
  9567. BlockType *block_type = &block->block_type;
  9568. WASMFuncType *wasm_type = block_type->u.type;
  9569. uint32 param_count = block_type->u.type->param_count;
  9570. int16 condition_offset = 0;
  9571. bool disable_emit = false;
  9572. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9573. int16 operand_offset = 0;
  9574. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9575. bh_assert(size > 0);
  9576. /* For if block, we also need copy the condition operand offset. */
  9577. if (is_if_block)
  9578. size += sizeof(*cells) + sizeof(*src_offsets);
  9579. /* Allocate memory for the emit data */
  9580. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9581. return false;
  9582. cells = emit_data;
  9583. src_offsets = (int16 *)(cells + param_count);
  9584. if (is_if_block)
  9585. condition_offset = *loader_ctx->frame_offset;
  9586. /* POP original parameter out */
  9587. for (i = 0; i < param_count; i++) {
  9588. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9589. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9590. }
  9591. frame_offset = loader_ctx->frame_offset;
  9592. /* Get each param's cell num and src offset */
  9593. for (i = 0; i < param_count; i++) {
  9594. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9595. cells[i] = cell;
  9596. src_offsets[i] = *frame_offset;
  9597. frame_offset += cell;
  9598. }
  9599. /* emit copy instruction */
  9600. emit_label(EXT_OP_COPY_STACK_VALUES);
  9601. /* Part a) */
  9602. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9603. /* Part b) */
  9604. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9605. : wasm_type->param_cell_num);
  9606. /* Part c) */
  9607. for (i = 0; i < param_count; i++)
  9608. emit_byte(loader_ctx, cells[i]);
  9609. if (is_if_block)
  9610. emit_byte(loader_ctx, 1);
  9611. /* Part d) */
  9612. for (i = 0; i < param_count; i++)
  9613. emit_operand(loader_ctx, src_offsets[i]);
  9614. if (is_if_block)
  9615. emit_operand(loader_ctx, condition_offset);
  9616. /* Since the start offset to save the block's params and
  9617. * the start offset to save the block's results may be
  9618. * different, we remember the dynamic offset for loop block
  9619. * so that we can use it to copy the stack operands to the
  9620. * loop block's params in wasm_loader_emit_br_info. */
  9621. if (block->label_type == LABEL_TYPE_LOOP)
  9622. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9623. /* Part e) */
  9624. /* Push to dynamic space. The push will emit the dst offset. */
  9625. for (i = 0; i < param_count; i++)
  9626. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9627. if (is_if_block)
  9628. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9629. ret = true;
  9630. fail:
  9631. /* Free the emit data */
  9632. wasm_runtime_free(emit_data);
  9633. return ret;
  9634. }
  9635. #endif
  9636. #if WASM_ENABLE_GC == 0
  9637. #define RESET_REFTYPE_MAP_STACK() (void)0
  9638. #else
  9639. #define RESET_REFTYPE_MAP_STACK() \
  9640. do { \
  9641. loader_ctx->reftype_map_num = \
  9642. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9643. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9644. + loader_ctx->reftype_map_num; \
  9645. } while (0)
  9646. #endif
  9647. /* reset the stack to the state of before entering the last block */
  9648. #if WASM_ENABLE_FAST_INTERP != 0
  9649. #define RESET_STACK() \
  9650. do { \
  9651. loader_ctx->stack_cell_num = \
  9652. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9653. loader_ctx->frame_ref = \
  9654. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9655. loader_ctx->frame_offset = \
  9656. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9657. RESET_REFTYPE_MAP_STACK(); \
  9658. } while (0)
  9659. #else
  9660. #define RESET_STACK() \
  9661. do { \
  9662. loader_ctx->stack_cell_num = \
  9663. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9664. loader_ctx->frame_ref = \
  9665. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9666. RESET_REFTYPE_MAP_STACK(); \
  9667. } while (0)
  9668. #endif
  9669. /* set current block's stack polymorphic state */
  9670. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9671. do { \
  9672. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9673. _cur_block->is_stack_polymorphic = flag; \
  9674. } while (0)
  9675. #define BLOCK_HAS_PARAM(block_type) \
  9676. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9677. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9678. do { \
  9679. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9680. error_buf_size))) { \
  9681. goto fail; \
  9682. } \
  9683. } while (0)
  9684. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9685. static bool
  9686. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9687. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9688. uint32 error_buf_size)
  9689. {
  9690. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9691. return false;
  9692. }
  9693. if (table_idx < module->import_table_count) {
  9694. if (p_elem_type)
  9695. *p_elem_type =
  9696. module->import_tables[table_idx].u.table.table_type.elem_type;
  9697. #if WASM_ENABLE_GC != 0
  9698. if (p_ref_type)
  9699. *((WASMRefType **)p_ref_type) =
  9700. module->import_tables[table_idx]
  9701. .u.table.table_type.elem_ref_type;
  9702. #endif
  9703. }
  9704. else {
  9705. if (p_elem_type)
  9706. *p_elem_type =
  9707. module->tables[module->import_table_count + table_idx]
  9708. .table_type.elem_type;
  9709. #if WASM_ENABLE_GC != 0
  9710. if (p_ref_type)
  9711. *((WASMRefType **)p_ref_type) =
  9712. module->tables[module->import_table_count + table_idx]
  9713. .table_type.elem_ref_type;
  9714. #endif
  9715. }
  9716. return true;
  9717. }
  9718. static bool
  9719. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9720. uint8 *p_elem_type, void **p_elem_ref_type,
  9721. char *error_buf, uint32 error_buf_size)
  9722. {
  9723. if (table_seg_idx >= module->table_seg_count) {
  9724. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9725. table_seg_idx);
  9726. return false;
  9727. }
  9728. if (p_elem_type) {
  9729. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9730. }
  9731. #if WASM_ENABLE_GC != 0
  9732. if (p_elem_ref_type)
  9733. *((WASMRefType **)p_elem_ref_type) =
  9734. module->table_segments[table_seg_idx].elem_ref_type;
  9735. #endif
  9736. return true;
  9737. }
  9738. #endif
  9739. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9740. const uint8 *
  9741. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9742. uint32 *len)
  9743. {
  9744. WASMCustomSection *section = module->custom_section_list;
  9745. while (section) {
  9746. if ((section->name_len == strlen(name))
  9747. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9748. if (len) {
  9749. *len = section->content_len;
  9750. }
  9751. return section->content_addr;
  9752. }
  9753. section = section->next;
  9754. }
  9755. return NULL;
  9756. }
  9757. #endif
  9758. #if 0
  9759. #define HANDLE_OPCODE(opcode) #opcode
  9760. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  9761. #undef HANDLE_OPCODE
  9762. #endif
  9763. static bool
  9764. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9765. uint32 cur_func_idx, char *error_buf,
  9766. uint32 error_buf_size)
  9767. {
  9768. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9769. uint32 param_count, local_count, global_count;
  9770. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type;
  9771. BlockType func_block_type;
  9772. uint16 *local_offsets, local_offset;
  9773. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9774. uint32 table_seg_idx, data_seg_idx, count, align, i;
  9775. mem_offset_t mem_offset;
  9776. int32 i32_const = 0;
  9777. int64 i64_const;
  9778. uint8 opcode;
  9779. bool return_value = false;
  9780. WASMLoaderContext *loader_ctx;
  9781. BranchBlock *frame_csp_tmp;
  9782. #if WASM_ENABLE_GC != 0
  9783. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9784. uint32 param_reftype_map_count, local_reftype_map_count;
  9785. int32 heap_type;
  9786. WASMRefType wasm_ref_type = { 0 };
  9787. bool need_ref_type_map;
  9788. #endif
  9789. #if WASM_ENABLE_FAST_INTERP != 0
  9790. uint8 *func_const_end, *func_const = NULL;
  9791. int16 operand_offset = 0;
  9792. uint8 last_op = 0;
  9793. bool disable_emit, preserve_local = false, if_condition_available = true;
  9794. float32 f32_const;
  9795. float64 f64_const;
  9796. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9797. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9798. #endif
  9799. #if WASM_ENABLE_MEMORY64 != 0
  9800. bool is_memory64 = has_module_memory64(module);
  9801. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  9802. #else
  9803. mem_offset_type = VALUE_TYPE_I32;
  9804. #endif
  9805. uint32 memidx;
  9806. global_count = module->import_global_count + module->global_count;
  9807. param_count = func->func_type->param_count;
  9808. param_types = func->func_type->types;
  9809. func_block_type.is_value_type = false;
  9810. func_block_type.u.type = func->func_type;
  9811. local_count = func->local_count;
  9812. local_types = func->local_types;
  9813. local_offsets = func->local_offsets;
  9814. #if WASM_ENABLE_GC != 0
  9815. param_reftype_maps = func->func_type->ref_type_maps;
  9816. param_reftype_map_count = func->func_type->ref_type_map_count;
  9817. local_reftype_maps = func->local_ref_type_maps;
  9818. local_reftype_map_count = func->local_ref_type_map_count;
  9819. #endif
  9820. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9821. goto fail;
  9822. }
  9823. #if WASM_ENABLE_GC != 0
  9824. loader_ctx->module = module;
  9825. loader_ctx->ref_type_set = module->ref_type_set;
  9826. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9827. #endif
  9828. #if WASM_ENABLE_FAST_INTERP != 0
  9829. /* For the first traverse, the initial value of preserved_local_offset has
  9830. * not been determined, we use the INT16_MAX to represent that a slot has
  9831. * been copied to preserve space. For second traverse, this field will be
  9832. * set to the appropriate value in wasm_loader_ctx_reinit.
  9833. * This is for Issue #1230,
  9834. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9835. * drop opcodes need to know which slots are preserved, so those slots will
  9836. * not be treated as dynamically allocated slots */
  9837. loader_ctx->preserved_local_offset = INT16_MAX;
  9838. re_scan:
  9839. if (loader_ctx->code_compiled_size > 0) {
  9840. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9841. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9842. goto fail;
  9843. }
  9844. p = func->code;
  9845. func->code_compiled = loader_ctx->p_code_compiled;
  9846. func->code_compiled_size = loader_ctx->code_compiled_size;
  9847. }
  9848. #endif
  9849. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9850. while (p < p_end) {
  9851. opcode = *p++;
  9852. #if WASM_ENABLE_FAST_INTERP != 0
  9853. p_org = p;
  9854. disable_emit = false;
  9855. emit_label(opcode);
  9856. #endif
  9857. switch (opcode) {
  9858. case WASM_OP_UNREACHABLE:
  9859. RESET_STACK();
  9860. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9861. break;
  9862. case WASM_OP_NOP:
  9863. #if WASM_ENABLE_FAST_INTERP != 0
  9864. skip_label();
  9865. #endif
  9866. break;
  9867. case WASM_OP_IF:
  9868. {
  9869. #if WASM_ENABLE_FAST_INTERP != 0
  9870. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9871. int32 available_stack_cell =
  9872. (int32)(loader_ctx->stack_cell_num
  9873. - parent_block->stack_cell_num);
  9874. if (available_stack_cell <= 0
  9875. && parent_block->is_stack_polymorphic)
  9876. if_condition_available = false;
  9877. else
  9878. if_condition_available = true;
  9879. PRESERVE_LOCAL_FOR_BLOCK();
  9880. #endif
  9881. #if WASM_ENABLE_GC == 0
  9882. POP_I32();
  9883. #endif
  9884. goto handle_op_block_and_loop;
  9885. }
  9886. case WASM_OP_BLOCK:
  9887. case WASM_OP_LOOP:
  9888. #if WASM_ENABLE_EXCE_HANDLING != 0
  9889. case WASM_OP_TRY:
  9890. if (opcode == WASM_OP_TRY) {
  9891. /*
  9892. * keep track of exception handlers to account for
  9893. * memory allocation
  9894. */
  9895. func->exception_handler_count++;
  9896. /*
  9897. * try is a block
  9898. * do nothing special, but execution continues to
  9899. * to handle_op_block_and_loop,
  9900. * and that be pushes the csp
  9901. */
  9902. }
  9903. #endif
  9904. #if WASM_ENABLE_FAST_INTERP != 0
  9905. PRESERVE_LOCAL_FOR_BLOCK();
  9906. #endif
  9907. handle_op_block_and_loop:
  9908. {
  9909. uint8 value_type;
  9910. BlockType block_type;
  9911. #if WASM_ENABLE_FAST_INTERP != 0
  9912. uint32 available_params = 0;
  9913. #endif
  9914. CHECK_BUF(p, p_end, 1);
  9915. value_type = read_uint8(p);
  9916. if (is_byte_a_type(value_type)) {
  9917. /* If the first byte is one of these special values:
  9918. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9919. * the single return value. */
  9920. block_type.is_value_type = true;
  9921. block_type.u.value_type.type = value_type;
  9922. #if WASM_ENABLE_WAMR_COMPILER != 0
  9923. if (value_type == VALUE_TYPE_V128)
  9924. module->is_simd_used = true;
  9925. else if (value_type == VALUE_TYPE_FUNCREF
  9926. || value_type == VALUE_TYPE_EXTERNREF)
  9927. module->is_ref_types_used = true;
  9928. #endif
  9929. #if WASM_ENABLE_GC != 0
  9930. if (value_type != VALUE_TYPE_VOID) {
  9931. p_org = p;
  9932. p--;
  9933. if (!resolve_value_type((const uint8 **)&p, p_end,
  9934. module, module->type_count,
  9935. &need_ref_type_map,
  9936. &wasm_ref_type, false,
  9937. error_buf, error_buf_size)) {
  9938. goto fail;
  9939. }
  9940. if (need_ref_type_map) {
  9941. block_type.u.value_type.ref_type_map.index = 0;
  9942. if (!(block_type.u.value_type.ref_type_map
  9943. .ref_type = reftype_set_insert(
  9944. module->ref_type_set, &wasm_ref_type,
  9945. error_buf, error_buf_size))) {
  9946. goto fail;
  9947. }
  9948. }
  9949. /* Set again as the type might be changed, e.g.
  9950. (ref null any) to anyref */
  9951. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9952. #if WASM_ENABLE_FAST_INTERP == 0
  9953. while (p_org < p) {
  9954. #if WASM_ENABLE_DEBUG_INTERP != 0
  9955. if (!record_fast_op(module, p_org, *p_org,
  9956. error_buf, error_buf_size)) {
  9957. goto fail;
  9958. }
  9959. #endif
  9960. /* Ignore extra bytes for interpreter */
  9961. *p_org++ = WASM_OP_NOP;
  9962. }
  9963. #endif
  9964. }
  9965. #endif /* end of WASM_ENABLE_GC != 0 */
  9966. }
  9967. else {
  9968. int32 type_index;
  9969. /* Resolve the leb128 encoded type index as block type */
  9970. p--;
  9971. p_org = p - 1;
  9972. read_leb_int32(p, p_end, type_index);
  9973. if ((uint32)type_index >= module->type_count) {
  9974. set_error_buf(error_buf, error_buf_size,
  9975. "unknown type");
  9976. goto fail;
  9977. }
  9978. block_type.is_value_type = false;
  9979. block_type.u.type =
  9980. (WASMFuncType *)module->types[type_index];
  9981. #if WASM_ENABLE_FAST_INTERP == 0
  9982. /* If block use type index as block type, change the opcode
  9983. * to new extended opcode so that interpreter can resolve
  9984. * the block quickly.
  9985. */
  9986. #if WASM_ENABLE_DEBUG_INTERP != 0
  9987. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9988. error_buf_size)) {
  9989. goto fail;
  9990. }
  9991. #endif
  9992. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  9993. #endif
  9994. }
  9995. #if WASM_ENABLE_GC != 0
  9996. if (opcode == WASM_OP_IF) {
  9997. POP_I32();
  9998. }
  9999. #endif
  10000. /* Pop block parameters from stack */
  10001. if (BLOCK_HAS_PARAM(block_type)) {
  10002. WASMFuncType *wasm_type = block_type.u.type;
  10003. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10004. #if WASM_ENABLE_FAST_INTERP != 0
  10005. uint32 cell_num;
  10006. available_params = block_type.u.type->param_count;
  10007. #endif
  10008. for (i = 0; i < block_type.u.type->param_count; i++) {
  10009. int32 available_stack_cell =
  10010. (int32)(loader_ctx->stack_cell_num
  10011. - cur_block->stack_cell_num);
  10012. if (available_stack_cell <= 0
  10013. && cur_block->is_stack_polymorphic) {
  10014. #if WASM_ENABLE_FAST_INTERP != 0
  10015. available_params = i;
  10016. #endif
  10017. break;
  10018. }
  10019. POP_TYPE(
  10020. wasm_type->types[wasm_type->param_count - i - 1]);
  10021. #if WASM_ENABLE_FAST_INTERP != 0
  10022. /* decrease the frame_offset pointer accordingly to keep
  10023. * consistent with frame_ref stack */
  10024. cell_num = wasm_value_type_cell_num(
  10025. wasm_type->types[wasm_type->param_count - i - 1]);
  10026. loader_ctx->frame_offset -= cell_num;
  10027. #endif
  10028. }
  10029. }
  10030. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10031. block_type, p);
  10032. /* Pass parameters to block */
  10033. if (BLOCK_HAS_PARAM(block_type)) {
  10034. WASMFuncType *func_type = block_type.u.type;
  10035. #if WASM_ENABLE_GC != 0
  10036. WASMRefType *ref_type;
  10037. uint32 j = 0;
  10038. #endif
  10039. for (i = 0; i < func_type->param_count; i++) {
  10040. #if WASM_ENABLE_FAST_INTERP != 0
  10041. uint32 cell_num =
  10042. wasm_value_type_cell_num(func_type->types[i]);
  10043. if (i >= available_params) {
  10044. /* If there isn't enough data on stack, push a dummy
  10045. * offset to keep the stack consistent with
  10046. * frame_ref.
  10047. * Since the stack is already in polymorphic state,
  10048. * the opcode will not be executed, so the dummy
  10049. * offset won't cause any error */
  10050. uint32 n;
  10051. for (n = 0; n < cell_num; n++) {
  10052. if (loader_ctx->p_code_compiled == NULL) {
  10053. if (!check_offset_push(loader_ctx,
  10054. error_buf,
  10055. error_buf_size))
  10056. goto fail;
  10057. }
  10058. *loader_ctx->frame_offset++ = 0;
  10059. }
  10060. }
  10061. else {
  10062. loader_ctx->frame_offset += cell_num;
  10063. }
  10064. #endif
  10065. #if WASM_ENABLE_GC != 0
  10066. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10067. bh_assert(func_type->ref_type_maps[j].index == i);
  10068. ref_type = func_type->ref_type_maps[j].ref_type;
  10069. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10070. ref_type,
  10071. wasm_reftype_struct_size(ref_type));
  10072. j++;
  10073. }
  10074. #endif
  10075. PUSH_TYPE(func_type->types[i]);
  10076. }
  10077. }
  10078. #if WASM_ENABLE_FAST_INTERP != 0
  10079. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10080. skip_label();
  10081. if (BLOCK_HAS_PARAM(block_type)) {
  10082. /* Make sure params are in dynamic space */
  10083. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10084. error_buf_size))
  10085. goto fail;
  10086. }
  10087. if (opcode == WASM_OP_LOOP) {
  10088. (loader_ctx->frame_csp - 1)->code_compiled =
  10089. loader_ctx->p_code_compiled;
  10090. }
  10091. }
  10092. #if WASM_ENABLE_EXCE_HANDLING != 0
  10093. else if (opcode == WASM_OP_TRY) {
  10094. skip_label();
  10095. }
  10096. #endif
  10097. else if (opcode == WASM_OP_IF) {
  10098. BranchBlock *block = loader_ctx->frame_csp - 1;
  10099. /* If block has parameters, we should make sure they are in
  10100. * dynamic space. Otherwise, when else branch is missing,
  10101. * the later opcode may consume incorrect operand offset.
  10102. * Spec case:
  10103. * (func (export "params-id") (param i32) (result i32)
  10104. * (i32.const 1)
  10105. * (i32.const 2)
  10106. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10107. * (then)) (i32.add)
  10108. * )
  10109. *
  10110. * So we should emit a copy instruction before the if.
  10111. *
  10112. * And we also need to save the parameter offsets and
  10113. * recover them before entering else branch.
  10114. *
  10115. */
  10116. if (BLOCK_HAS_PARAM(block_type)) {
  10117. uint64 size;
  10118. /* In polymorphic state, there may be no if condition on
  10119. * the stack, so the offset may not emitted */
  10120. if (if_condition_available) {
  10121. /* skip the if condition operand offset */
  10122. wasm_loader_emit_backspace(loader_ctx,
  10123. sizeof(int16));
  10124. }
  10125. /* skip the if label */
  10126. skip_label();
  10127. /* Emit a copy instruction */
  10128. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10129. error_buf_size))
  10130. goto fail;
  10131. /* Emit the if instruction */
  10132. emit_label(opcode);
  10133. /* Emit the new condition operand offset */
  10134. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10135. /* Save top param_count values of frame_offset stack, so
  10136. * that we can recover it before executing else branch
  10137. */
  10138. size = sizeof(int16)
  10139. * (uint64)block_type.u.type->param_cell_num;
  10140. if (!(block->param_frame_offsets = loader_malloc(
  10141. size, error_buf, error_buf_size)))
  10142. goto fail;
  10143. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10144. loader_ctx->frame_offset
  10145. - size / sizeof(int16),
  10146. (uint32)size);
  10147. }
  10148. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10149. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10150. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10151. }
  10152. #endif
  10153. break;
  10154. }
  10155. #if WASM_ENABLE_EXCE_HANDLING != 0
  10156. case WASM_OP_THROW:
  10157. {
  10158. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10159. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10160. uint8 label_type = cur_block->label_type;
  10161. uint32 tag_index = 0;
  10162. read_leb_int32(p, p_end, tag_index);
  10163. /* check validity of tag_index against module->tag_count */
  10164. /* check tag index is within the tag index space */
  10165. if (tag_index >= module->import_tag_count + module->tag_count) {
  10166. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10167. tag_index);
  10168. goto fail;
  10169. }
  10170. /* the tag_type is stored in either the WASMTag (section tags)
  10171. * or WASMTagImport (import tag) */
  10172. WASMFuncType *tag_type = NULL;
  10173. if (tag_index < module->import_tag_count) {
  10174. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10175. }
  10176. else {
  10177. tag_type =
  10178. module->tags[tag_index - module->import_tag_count]
  10179. ->tag_type;
  10180. }
  10181. if (tag_type->result_count != 0) {
  10182. set_error_buf(error_buf, error_buf_size,
  10183. "tag type signature does not return void");
  10184. goto fail;
  10185. }
  10186. int32 available_stack_cell =
  10187. (int32)(loader_ctx->stack_cell_num
  10188. - cur_block->stack_cell_num);
  10189. int32 tti;
  10190. /* Check stack values match return types by comparing tag param
  10191. * types with stack cells */
  10192. uint8 *frame_ref = loader_ctx->frame_ref;
  10193. #if WASM_ENABLE_GC != 0
  10194. WASMRefTypeMap *frame_reftype_map =
  10195. loader_ctx->frame_reftype_map;
  10196. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10197. /* Temporarily set these values since they may be used in
  10198. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10199. param_reftype_maps = tag_type->ref_type_maps;
  10200. /* For tag_type function, it shouldn't have result_count = 0 */
  10201. param_reftype_map_count = tag_type->ref_type_map_count;
  10202. param_count = tag_type->param_count;
  10203. #endif
  10204. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10205. #if WASM_ENABLE_GC != 0
  10206. local_type = tag_type->types[tti];
  10207. local_idx = tti;
  10208. /* Get the wasm_ref_type if the local_type is multibyte
  10209. type */
  10210. GET_LOCAL_REFTYPE();
  10211. #endif
  10212. if (!check_stack_top_values(
  10213. loader_ctx, frame_ref, available_stack_cell,
  10214. #if WASM_ENABLE_GC != 0
  10215. frame_reftype_map, frame_reftype_map_num,
  10216. #endif
  10217. tag_type->types[tti],
  10218. #if WASM_ENABLE_GC != 0
  10219. &wasm_ref_type,
  10220. #endif
  10221. error_buf, error_buf_size)) {
  10222. snprintf(error_buf, error_buf_size,
  10223. "type mismatch: instruction requires [%s] but "
  10224. "stack has [%s]",
  10225. tag_type->param_count > 0
  10226. ? type2str(tag_type->types[tti])
  10227. : "",
  10228. available_stack_cell > 0
  10229. ? type2str(*(loader_ctx->frame_ref - 1))
  10230. : "");
  10231. goto fail;
  10232. }
  10233. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10234. available_stack_cell -=
  10235. wasm_value_type_cell_num(tag_type->types[tti]);
  10236. }
  10237. #if WASM_ENABLE_GC != 0
  10238. /* Restore the values */
  10239. param_reftype_maps = func->func_type->ref_type_maps;
  10240. param_reftype_map_count = func->func_type->ref_type_map_count;
  10241. param_count = func->func_type->param_count;
  10242. #endif
  10243. /* throw is stack polymorphic */
  10244. (void)label_type;
  10245. RESET_STACK();
  10246. break;
  10247. }
  10248. case WASM_OP_RETHROW:
  10249. {
  10250. /* must be done before checking branch block */
  10251. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10252. /* check the target catching block: LABEL_TYPE_CATCH */
  10253. if (!(frame_csp_tmp =
  10254. check_branch_block(loader_ctx, &p, p_end, opcode,
  10255. error_buf, error_buf_size)))
  10256. goto fail;
  10257. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10258. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10259. /* trap according to spectest (rethrow.wast) */
  10260. set_error_buf(error_buf, error_buf_size,
  10261. "invalid rethrow label");
  10262. goto fail;
  10263. }
  10264. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10265. uint8 label_type = cur_block->label_type;
  10266. (void)label_type;
  10267. /* rethrow is stack polymorphic */
  10268. RESET_STACK();
  10269. break;
  10270. }
  10271. case WASM_OP_DELEGATE:
  10272. {
  10273. /* check target block is valid */
  10274. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10275. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10276. goto fail;
  10277. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10278. uint8 label_type = cur_block->label_type;
  10279. (void)label_type;
  10280. /* DELEGATE ends the block */
  10281. POP_CSP();
  10282. break;
  10283. }
  10284. case WASM_OP_CATCH:
  10285. {
  10286. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10287. uint8 label_type = cur_block->label_type;
  10288. uint32 tag_index = 0;
  10289. read_leb_int32(p, p_end, tag_index);
  10290. /* check validity of tag_index against module->tag_count */
  10291. /* check tag index is within the tag index space */
  10292. if (tag_index >= module->import_tag_count + module->tag_count) {
  10293. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10294. __FUNCTION__);
  10295. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10296. goto fail;
  10297. }
  10298. /* the tag_type is stored in either the WASMTag (section tags)
  10299. * or WASMTagImport (import tag) */
  10300. WASMFuncType *func_type = NULL;
  10301. if (tag_index < module->import_tag_count) {
  10302. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10303. }
  10304. else {
  10305. func_type =
  10306. module->tags[tag_index - module->import_tag_count]
  10307. ->tag_type;
  10308. }
  10309. if (func_type->result_count != 0) {
  10310. set_error_buf(error_buf, error_buf_size,
  10311. "tag type signature does not return void");
  10312. goto fail;
  10313. }
  10314. /* check validity of current label (expect LABEL_TYPE_TRY or
  10315. * LABEL_TYPE_CATCH) */
  10316. if ((LABEL_TYPE_CATCH != label_type)
  10317. && (LABEL_TYPE_TRY != label_type)) {
  10318. set_error_buf(error_buf, error_buf_size,
  10319. "Unexpected block sequence encountered.");
  10320. goto fail;
  10321. }
  10322. /*
  10323. * replace frame_csp by LABEL_TYPE_CATCH
  10324. */
  10325. cur_block->label_type = LABEL_TYPE_CATCH;
  10326. /* RESET_STACK removes the values pushed in TRY or pervious
  10327. * CATCH Blocks */
  10328. RESET_STACK();
  10329. #if WASM_ENABLE_GC != 0
  10330. WASMRefType *ref_type;
  10331. uint32 j = 0;
  10332. #endif
  10333. /* push types on the stack according to caught type */
  10334. for (i = 0; i < func_type->param_count; i++) {
  10335. #if WASM_ENABLE_GC != 0
  10336. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10337. bh_assert(func_type->ref_type_maps[j].index == i);
  10338. ref_type = func_type->ref_type_maps[j].ref_type;
  10339. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10340. ref_type,
  10341. wasm_reftype_struct_size(ref_type));
  10342. j++;
  10343. }
  10344. #endif
  10345. PUSH_TYPE(func_type->types[i]);
  10346. }
  10347. break;
  10348. }
  10349. case WASM_OP_CATCH_ALL:
  10350. {
  10351. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10352. /* expecting a TRY or CATCH, anything else will be considered an
  10353. * error */
  10354. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10355. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10356. set_error_buf(error_buf, error_buf_size,
  10357. "Unexpected block sequence encountered.");
  10358. goto fail;
  10359. }
  10360. /* no immediates */
  10361. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10362. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10363. /* RESET_STACK removes the values pushed in TRY or pervious
  10364. * CATCH Blocks */
  10365. RESET_STACK();
  10366. /* catch_all has no tagtype and therefore no parameters */
  10367. break;
  10368. }
  10369. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10370. case WASM_OP_ELSE:
  10371. handle_op_else:
  10372. {
  10373. BranchBlock *block = NULL;
  10374. BlockType block_type;
  10375. if (loader_ctx->csp_num < 2
  10376. /* the matched if isn't found */
  10377. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10378. /* duplicated else is found */
  10379. || (loader_ctx->frame_csp - 1)->else_addr) {
  10380. set_error_buf(
  10381. error_buf, error_buf_size,
  10382. "opcode else found without matched opcode if");
  10383. goto fail;
  10384. }
  10385. block = loader_ctx->frame_csp - 1;
  10386. /* check whether if branch's stack matches its result type */
  10387. if (!check_block_stack(loader_ctx, block, error_buf,
  10388. error_buf_size))
  10389. goto fail;
  10390. block->else_addr = p - 1;
  10391. block_type = block->block_type;
  10392. #if WASM_ENABLE_GC != 0
  10393. if (!wasm_loader_init_local_use_masks(
  10394. loader_ctx, local_count, error_buf, error_buf_size)) {
  10395. goto fail;
  10396. }
  10397. #endif
  10398. #if WASM_ENABLE_FAST_INTERP != 0
  10399. /* if the result of if branch is in local or const area, add a
  10400. * copy op */
  10401. RESERVE_BLOCK_RET();
  10402. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10403. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10404. #endif
  10405. RESET_STACK();
  10406. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10407. /* Pass parameters to if-false branch */
  10408. if (BLOCK_HAS_PARAM(block_type)) {
  10409. for (i = 0; i < block_type.u.type->param_count; i++)
  10410. PUSH_TYPE(block_type.u.type->types[i]);
  10411. }
  10412. #if WASM_ENABLE_FAST_INTERP != 0
  10413. /* Recover top param_count values of frame_offset stack */
  10414. if (BLOCK_HAS_PARAM((block_type))) {
  10415. uint32 size;
  10416. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10417. bh_memcpy_s(loader_ctx->frame_offset, size,
  10418. block->param_frame_offsets, size);
  10419. loader_ctx->frame_offset += (size / sizeof(int16));
  10420. }
  10421. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10422. #endif
  10423. break;
  10424. }
  10425. case WASM_OP_END:
  10426. {
  10427. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10428. /* check whether block stack matches its result type */
  10429. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10430. error_buf_size))
  10431. goto fail;
  10432. /* if there is no else branch, make a virtual else opcode for
  10433. easier integrity check and to copy the correct results to
  10434. the block return address for fast-interp mode:
  10435. change if block from `if ... end` to `if ... else end` */
  10436. if (cur_block->label_type == LABEL_TYPE_IF
  10437. && !cur_block->else_addr) {
  10438. opcode = WASM_OP_ELSE;
  10439. p--;
  10440. #if WASM_ENABLE_FAST_INTERP != 0
  10441. p_org = p;
  10442. skip_label();
  10443. disable_emit = false;
  10444. emit_label(opcode);
  10445. #endif
  10446. goto handle_op_else;
  10447. }
  10448. POP_CSP();
  10449. #if WASM_ENABLE_FAST_INTERP != 0
  10450. skip_label();
  10451. /* copy the result to the block return address */
  10452. RESERVE_BLOCK_RET();
  10453. apply_label_patch(loader_ctx, 0, PATCH_END);
  10454. free_label_patch_list(loader_ctx->frame_csp);
  10455. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10456. int32 idx;
  10457. uint8 ret_type;
  10458. emit_label(WASM_OP_RETURN);
  10459. for (idx = (int32)func->func_type->result_count - 1;
  10460. idx >= 0; idx--) {
  10461. ret_type = *(func->func_type->types
  10462. + func->func_type->param_count + idx);
  10463. POP_OFFSET_TYPE(ret_type);
  10464. }
  10465. }
  10466. #endif
  10467. if (loader_ctx->csp_num > 0) {
  10468. loader_ctx->frame_csp->end_addr = p - 1;
  10469. }
  10470. else {
  10471. /* end of function block, function will return */
  10472. if (p < p_end) {
  10473. set_error_buf(error_buf, error_buf_size,
  10474. "section size mismatch");
  10475. goto fail;
  10476. }
  10477. }
  10478. break;
  10479. }
  10480. case WASM_OP_BR:
  10481. {
  10482. if (!(frame_csp_tmp =
  10483. check_branch_block(loader_ctx, &p, p_end, opcode,
  10484. error_buf, error_buf_size)))
  10485. goto fail;
  10486. RESET_STACK();
  10487. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10488. break;
  10489. }
  10490. case WASM_OP_BR_IF:
  10491. {
  10492. POP_I32();
  10493. if (!(frame_csp_tmp =
  10494. check_branch_block(loader_ctx, &p, p_end, opcode,
  10495. error_buf, error_buf_size)))
  10496. goto fail;
  10497. break;
  10498. }
  10499. case WASM_OP_BR_TABLE:
  10500. {
  10501. uint32 depth = 0, default_arity, arity = 0;
  10502. BranchBlock *target_block;
  10503. BlockType *target_block_type;
  10504. #if WASM_ENABLE_FAST_INTERP == 0
  10505. BrTableCache *br_table_cache = NULL;
  10506. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10507. uint32 j;
  10508. #endif
  10509. read_leb_uint32(p, p_end, count);
  10510. #if WASM_ENABLE_FAST_INTERP != 0
  10511. emit_uint32(loader_ctx, count);
  10512. #endif
  10513. POP_I32();
  10514. /* Get each depth and check it */
  10515. p_org = p;
  10516. for (i = 0; i <= count; i++) {
  10517. read_leb_uint32(p, p_end, depth);
  10518. bh_assert(loader_ctx->csp_num > 0);
  10519. if (loader_ctx->csp_num - 1 < depth) {
  10520. set_error_buf(error_buf, error_buf_size,
  10521. "unknown label, "
  10522. "unexpected end of section or function");
  10523. goto fail;
  10524. }
  10525. }
  10526. p = p_org;
  10527. /* Get the default block's arity */
  10528. target_block = loader_ctx->frame_csp - (depth + 1);
  10529. target_block_type = &target_block->block_type;
  10530. default_arity = block_type_get_arity(target_block_type,
  10531. target_block->label_type);
  10532. #if WASM_ENABLE_FAST_INTERP == 0
  10533. p_depth_begin = p_depth = p;
  10534. #endif
  10535. for (i = 0; i <= count; i++) {
  10536. p_org = p;
  10537. read_leb_uint32(p, p_end, depth);
  10538. p = p_org;
  10539. /* Get the target block's arity and check it */
  10540. target_block = loader_ctx->frame_csp - (depth + 1);
  10541. target_block_type = &target_block->block_type;
  10542. arity = block_type_get_arity(target_block_type,
  10543. target_block->label_type);
  10544. if (arity != default_arity) {
  10545. set_error_buf(error_buf, error_buf_size,
  10546. "type mismatch: br_table targets must "
  10547. "all use same result type");
  10548. goto fail;
  10549. }
  10550. if (!(frame_csp_tmp =
  10551. check_branch_block(loader_ctx, &p, p_end, opcode,
  10552. error_buf, error_buf_size))) {
  10553. goto fail;
  10554. }
  10555. #if WASM_ENABLE_FAST_INTERP == 0
  10556. if (br_table_cache) {
  10557. br_table_cache->br_depths[i] = depth;
  10558. }
  10559. else {
  10560. if (depth > 255) {
  10561. /* The depth cannot be stored in one byte,
  10562. create br_table cache to store each depth */
  10563. #if WASM_ENABLE_DEBUG_INTERP != 0
  10564. if (!record_fast_op(module, p_opcode, *p_opcode,
  10565. error_buf, error_buf_size)) {
  10566. goto fail;
  10567. }
  10568. #endif
  10569. if (!(br_table_cache = loader_malloc(
  10570. offsetof(BrTableCache, br_depths)
  10571. + sizeof(uint32)
  10572. * (uint64)(count + 1),
  10573. error_buf, error_buf_size))) {
  10574. goto fail;
  10575. }
  10576. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10577. br_table_cache->br_table_op_addr = p_opcode;
  10578. br_table_cache->br_count = count;
  10579. /* Copy previous depths which are one byte */
  10580. for (j = 0; j < i; j++) {
  10581. br_table_cache->br_depths[j] = p_depth_begin[j];
  10582. }
  10583. br_table_cache->br_depths[i] = depth;
  10584. bh_list_insert(module->br_table_cache_list,
  10585. br_table_cache);
  10586. }
  10587. else {
  10588. /* The depth can be stored in one byte, use the
  10589. byte of the leb to store it */
  10590. *p_depth++ = (uint8)depth;
  10591. }
  10592. }
  10593. #endif
  10594. }
  10595. #if WASM_ENABLE_FAST_INTERP == 0
  10596. /* Set the tailing bytes to nop */
  10597. if (br_table_cache)
  10598. p_depth = p_depth_begin;
  10599. while (p_depth < p)
  10600. *p_depth++ = WASM_OP_NOP;
  10601. #endif
  10602. RESET_STACK();
  10603. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10604. break;
  10605. }
  10606. case WASM_OP_RETURN:
  10607. {
  10608. WASMFuncType *func_type = func->func_type;
  10609. int32 idx;
  10610. uint8 ret_type;
  10611. #if WASM_ENABLE_GC != 0
  10612. uint32 j = func_type->ref_type_map_count - 1;
  10613. #endif
  10614. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  10615. idx--) {
  10616. ret_type =
  10617. *(func_type->types + func_type->param_count + idx);
  10618. #if WASM_ENABLE_GC != 0
  10619. if (wasm_is_type_multi_byte_type(ret_type)) {
  10620. WASMRefType *ref_type =
  10621. func_type->ref_type_maps[j].ref_type;
  10622. bh_assert(func_type->ref_type_maps[j].index
  10623. == func_type->param_count + idx);
  10624. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10625. ref_type,
  10626. wasm_reftype_struct_size(ref_type));
  10627. j--;
  10628. }
  10629. #endif
  10630. #if WASM_ENABLE_FAST_INTERP != 0
  10631. /* emit the offset after return opcode */
  10632. POP_OFFSET_TYPE(ret_type);
  10633. #endif
  10634. POP_TYPE(ret_type);
  10635. }
  10636. RESET_STACK();
  10637. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10638. break;
  10639. }
  10640. case WASM_OP_CALL:
  10641. #if WASM_ENABLE_TAIL_CALL != 0
  10642. case WASM_OP_RETURN_CALL:
  10643. #endif
  10644. #if WASM_ENABLE_GC != 0
  10645. case WASM_OP_CALL_REF:
  10646. case WASM_OP_RETURN_CALL_REF:
  10647. #endif
  10648. {
  10649. WASMFuncType *func_type;
  10650. uint8 type;
  10651. int32 idx;
  10652. #if WASM_ENABLE_GC != 0
  10653. WASMRefType *ref_type;
  10654. uint32 type_idx1;
  10655. int32 j;
  10656. #endif
  10657. #if WASM_ENABLE_GC != 0
  10658. if (opcode == WASM_OP_CALL_REF
  10659. || opcode == WASM_OP_RETURN_CALL_REF) {
  10660. read_leb_uint32(p, p_end, type_idx1);
  10661. if (!check_type_index(module, module->type_count, type_idx1,
  10662. error_buf, error_buf_size)) {
  10663. goto fail;
  10664. }
  10665. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  10666. set_error_buf(error_buf, error_buf_size,
  10667. "unkown function type");
  10668. goto fail;
  10669. }
  10670. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10671. &type_idx, error_buf,
  10672. error_buf_size)) {
  10673. goto fail;
  10674. }
  10675. if (type == VALUE_TYPE_ANY) {
  10676. type_idx = type_idx1;
  10677. }
  10678. if (!check_type_index(module, module->type_count, type_idx,
  10679. error_buf, error_buf_size)) {
  10680. goto fail;
  10681. }
  10682. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10683. set_error_buf(error_buf, error_buf_size,
  10684. "unkown function type");
  10685. goto fail;
  10686. }
  10687. if (!wasm_func_type_is_super_of(
  10688. (WASMFuncType *)module->types[type_idx1],
  10689. (WASMFuncType *)module->types[type_idx])) {
  10690. set_error_buf(error_buf, error_buf_size,
  10691. "function type mismatch");
  10692. goto fail;
  10693. }
  10694. func_type = (WASMFuncType *)module->types[type_idx];
  10695. }
  10696. else
  10697. #endif
  10698. {
  10699. read_leb_uint32(p, p_end, func_idx);
  10700. #if WASM_ENABLE_FAST_INTERP != 0
  10701. /* we need to emit func_idx before arguments */
  10702. emit_uint32(loader_ctx, func_idx);
  10703. #endif
  10704. if (!check_function_index(module, func_idx, error_buf,
  10705. error_buf_size)) {
  10706. goto fail;
  10707. }
  10708. if (func_idx < module->import_function_count)
  10709. func_type = module->import_functions[func_idx]
  10710. .u.function.func_type;
  10711. else
  10712. func_type =
  10713. module
  10714. ->functions[func_idx
  10715. - module->import_function_count]
  10716. ->func_type;
  10717. }
  10718. if (func_type->param_count > 0) {
  10719. #if WASM_ENABLE_GC != 0
  10720. j = (int32)(func_type->result_ref_type_maps
  10721. - func_type->ref_type_maps - 1);
  10722. #endif
  10723. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10724. idx--) {
  10725. #if WASM_ENABLE_GC != 0
  10726. if (wasm_is_type_multi_byte_type(
  10727. func_type->types[idx])) {
  10728. ref_type = func_type->ref_type_maps[j].ref_type;
  10729. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10730. ref_type,
  10731. wasm_reftype_struct_size(ref_type));
  10732. j--;
  10733. }
  10734. #endif
  10735. #if WASM_ENABLE_FAST_INTERP != 0
  10736. POP_OFFSET_TYPE(func_type->types[idx]);
  10737. #endif
  10738. POP_TYPE(func_type->types[idx]);
  10739. }
  10740. }
  10741. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10742. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10743. #endif
  10744. #if WASM_ENABLE_GC != 0
  10745. j = (int32)(func_type->result_ref_type_maps
  10746. - func_type->ref_type_maps);
  10747. #endif
  10748. for (i = 0; i < func_type->result_count; i++) {
  10749. #if WASM_ENABLE_GC != 0
  10750. if (wasm_is_type_multi_byte_type(
  10751. func_type->types[func_type->param_count + i])) {
  10752. ref_type = func_type->ref_type_maps[j].ref_type;
  10753. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10754. ref_type,
  10755. wasm_reftype_struct_size(ref_type));
  10756. j++;
  10757. }
  10758. #endif
  10759. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10760. #if WASM_ENABLE_FAST_INTERP != 0
  10761. /* Here we emit each return value's dynamic_offset. But
  10762. * in fact these offsets are continuous, so interpreter
  10763. * only need to get the first return value's offset.
  10764. */
  10765. PUSH_OFFSET_TYPE(
  10766. func_type->types[func_type->param_count + i]);
  10767. #endif
  10768. }
  10769. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10770. }
  10771. else {
  10772. #if WASM_ENABLE_GC == 0
  10773. if (func_type->result_count
  10774. != func->func_type->result_count) {
  10775. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10776. "type mismatch: expect ",
  10777. func->func_type->result_count,
  10778. " return values but got other");
  10779. goto fail;
  10780. }
  10781. for (i = 0; i < func_type->result_count; i++) {
  10782. type = func->func_type
  10783. ->types[func->func_type->param_count + i];
  10784. if (func_type->types[func_type->param_count + i]
  10785. != type) {
  10786. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10787. "type mismatch: expect ",
  10788. type2str(type), " but got other");
  10789. goto fail;
  10790. }
  10791. }
  10792. #else
  10793. if (!wasm_func_type_result_is_subtype_of(
  10794. func_type, func->func_type, module->types,
  10795. module->type_count)) {
  10796. set_error_buf(
  10797. error_buf, error_buf_size,
  10798. "type mismatch: invalid func result types");
  10799. goto fail;
  10800. }
  10801. #endif
  10802. RESET_STACK();
  10803. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10804. }
  10805. #endif
  10806. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10807. || WASM_ENABLE_WAMR_COMPILER != 0
  10808. func->has_op_func_call = true;
  10809. #endif
  10810. (void)type;
  10811. break;
  10812. }
  10813. /*
  10814. * if disable reference type: call_indirect typeidx, 0x00
  10815. * if enable reference type: call_indirect typeidx, tableidx
  10816. */
  10817. case WASM_OP_CALL_INDIRECT:
  10818. #if WASM_ENABLE_TAIL_CALL != 0
  10819. case WASM_OP_RETURN_CALL_INDIRECT:
  10820. #endif
  10821. {
  10822. int32 idx;
  10823. WASMFuncType *func_type;
  10824. read_leb_uint32(p, p_end, type_idx);
  10825. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10826. #if WASM_ENABLE_WAMR_COMPILER != 0
  10827. if (p + 1 < p_end && *p != 0x00) {
  10828. /*
  10829. * Any non-0x00 byte requires the ref types proposal.
  10830. * This is different from checking the table_idx value
  10831. * since `0x80 0x00` etc. are all valid encodings of zero.
  10832. */
  10833. module->is_ref_types_used = true;
  10834. }
  10835. #endif
  10836. read_leb_uint32(p, p_end, table_idx);
  10837. #else
  10838. CHECK_BUF(p, p_end, 1);
  10839. table_idx = read_uint8(p);
  10840. #endif
  10841. if (!check_table_index(module, table_idx, error_buf,
  10842. error_buf_size)) {
  10843. goto fail;
  10844. }
  10845. #if WASM_ENABLE_FAST_INTERP != 0
  10846. /* we need to emit before arguments */
  10847. #if WASM_ENABLE_TAIL_CALL != 0
  10848. emit_byte(loader_ctx, opcode);
  10849. #endif
  10850. emit_uint32(loader_ctx, type_idx);
  10851. emit_uint32(loader_ctx, table_idx);
  10852. #endif
  10853. /* skip elem idx */
  10854. POP_I32();
  10855. if (type_idx >= module->type_count) {
  10856. set_error_buf(error_buf, error_buf_size, "unknown type");
  10857. goto fail;
  10858. }
  10859. func_type = (WASMFuncType *)module->types[type_idx];
  10860. if (func_type->param_count > 0) {
  10861. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10862. idx--) {
  10863. #if WASM_ENABLE_FAST_INTERP != 0
  10864. POP_OFFSET_TYPE(func_type->types[idx]);
  10865. #endif
  10866. POP_TYPE(func_type->types[idx]);
  10867. }
  10868. }
  10869. #if WASM_ENABLE_TAIL_CALL != 0
  10870. if (opcode == WASM_OP_CALL_INDIRECT) {
  10871. #endif
  10872. for (i = 0; i < func_type->result_count; i++) {
  10873. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10874. #if WASM_ENABLE_FAST_INTERP != 0
  10875. PUSH_OFFSET_TYPE(
  10876. func_type->types[func_type->param_count + i]);
  10877. #endif
  10878. }
  10879. #if WASM_ENABLE_TAIL_CALL != 0
  10880. }
  10881. else {
  10882. uint8 type;
  10883. if (func_type->result_count
  10884. != func->func_type->result_count) {
  10885. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10886. "type mismatch: expect ",
  10887. func->func_type->result_count,
  10888. " return values but got other");
  10889. goto fail;
  10890. }
  10891. for (i = 0; i < func_type->result_count; i++) {
  10892. type = func->func_type
  10893. ->types[func->func_type->param_count + i];
  10894. if (func_type->types[func_type->param_count + i]
  10895. != type) {
  10896. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10897. "type mismatch: expect ",
  10898. type2str(type), " but got other");
  10899. goto fail;
  10900. }
  10901. }
  10902. RESET_STACK();
  10903. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10904. }
  10905. #endif
  10906. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10907. || WASM_ENABLE_WAMR_COMPILER != 0
  10908. func->has_op_func_call = true;
  10909. #endif
  10910. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10911. func->has_op_call_indirect = true;
  10912. #endif
  10913. break;
  10914. }
  10915. case WASM_OP_DROP:
  10916. {
  10917. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10918. int32 available_stack_cell =
  10919. (int32)(loader_ctx->stack_cell_num
  10920. - cur_block->stack_cell_num);
  10921. if (available_stack_cell <= 0
  10922. && !cur_block->is_stack_polymorphic) {
  10923. set_error_buf(error_buf, error_buf_size,
  10924. "type mismatch, opcode drop was found "
  10925. "but stack was empty");
  10926. goto fail;
  10927. }
  10928. if (available_stack_cell > 0) {
  10929. #if WASM_ENABLE_GC != 0
  10930. if (wasm_is_type_multi_byte_type(
  10931. *(loader_ctx->frame_ref - 1))) {
  10932. bh_assert((int32)(loader_ctx->reftype_map_num
  10933. - cur_block->reftype_map_num)
  10934. > 0);
  10935. loader_ctx->frame_reftype_map--;
  10936. loader_ctx->reftype_map_num--;
  10937. }
  10938. #endif
  10939. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  10940. loader_ctx->frame_ref--;
  10941. loader_ctx->stack_cell_num--;
  10942. #if WASM_ENABLE_FAST_INTERP != 0
  10943. skip_label();
  10944. loader_ctx->frame_offset--;
  10945. if ((*(loader_ctx->frame_offset)
  10946. > loader_ctx->start_dynamic_offset)
  10947. && (*(loader_ctx->frame_offset)
  10948. < loader_ctx->max_dynamic_offset))
  10949. loader_ctx->dynamic_offset--;
  10950. #endif
  10951. }
  10952. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10953. loader_ctx->frame_ref -= 2;
  10954. loader_ctx->stack_cell_num -= 2;
  10955. #if WASM_ENABLE_FAST_INTERP == 0
  10956. *(p - 1) = WASM_OP_DROP_64;
  10957. #endif
  10958. #if WASM_ENABLE_FAST_INTERP != 0
  10959. skip_label();
  10960. loader_ctx->frame_offset -= 2;
  10961. if ((*(loader_ctx->frame_offset)
  10962. > loader_ctx->start_dynamic_offset)
  10963. && (*(loader_ctx->frame_offset)
  10964. < loader_ctx->max_dynamic_offset))
  10965. loader_ctx->dynamic_offset -= 2;
  10966. #endif
  10967. }
  10968. #if WASM_ENABLE_SIMD != 0
  10969. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10970. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10971. loader_ctx->frame_ref -= 4;
  10972. loader_ctx->stack_cell_num -= 4;
  10973. }
  10974. #endif
  10975. #endif
  10976. else {
  10977. set_error_buf(error_buf, error_buf_size,
  10978. "type mismatch");
  10979. goto fail;
  10980. }
  10981. }
  10982. else {
  10983. #if WASM_ENABLE_FAST_INTERP != 0
  10984. skip_label();
  10985. #endif
  10986. }
  10987. break;
  10988. }
  10989. case WASM_OP_SELECT:
  10990. {
  10991. uint8 ref_type;
  10992. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10993. int32 available_stack_cell;
  10994. #if WASM_ENABLE_FAST_INTERP != 0
  10995. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10996. #endif
  10997. POP_I32();
  10998. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  10999. - cur_block->stack_cell_num);
  11000. if (available_stack_cell <= 0
  11001. && !cur_block->is_stack_polymorphic) {
  11002. set_error_buf(error_buf, error_buf_size,
  11003. "type mismatch or invalid result arity, "
  11004. "opcode select was found "
  11005. "but stack was empty");
  11006. goto fail;
  11007. }
  11008. if (available_stack_cell > 0) {
  11009. switch (*(loader_ctx->frame_ref - 1)) {
  11010. case VALUE_TYPE_I32:
  11011. case VALUE_TYPE_F32:
  11012. case VALUE_TYPE_ANY:
  11013. break;
  11014. case VALUE_TYPE_I64:
  11015. case VALUE_TYPE_F64:
  11016. #if WASM_ENABLE_FAST_INTERP == 0
  11017. *(p - 1) = WASM_OP_SELECT_64;
  11018. #endif
  11019. #if WASM_ENABLE_FAST_INTERP != 0
  11020. if (loader_ctx->p_code_compiled) {
  11021. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11022. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11023. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11024. *(void **)(p_code_compiled_tmp
  11025. - sizeof(void *)) =
  11026. handle_table[opcode_tmp];
  11027. #else
  11028. #if UINTPTR_MAX == UINT64_MAX
  11029. /* emit int32 relative offset in 64-bit target
  11030. */
  11031. int32 offset =
  11032. (int32)((uint8 *)handle_table[opcode_tmp]
  11033. - (uint8 *)handle_table[0]);
  11034. *(int32 *)(p_code_compiled_tmp
  11035. - sizeof(int32)) = offset;
  11036. #else
  11037. /* emit uint32 label address in 32-bit target */
  11038. *(uint32 *)(p_code_compiled_tmp
  11039. - sizeof(uint32)) =
  11040. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11041. #endif
  11042. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11043. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11044. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11045. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11046. #else
  11047. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11048. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11049. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11050. }
  11051. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11052. break;
  11053. #if WASM_ENABLE_SIMD != 0
  11054. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  11055. case VALUE_TYPE_V128:
  11056. break;
  11057. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  11058. #endif /* WASM_ENABLE_SIMD != 0 */
  11059. default:
  11060. {
  11061. set_error_buf(error_buf, error_buf_size,
  11062. "type mismatch");
  11063. goto fail;
  11064. }
  11065. }
  11066. ref_type = *(loader_ctx->frame_ref - 1);
  11067. #if WASM_ENABLE_FAST_INTERP != 0
  11068. POP_OFFSET_TYPE(ref_type);
  11069. POP_TYPE(ref_type);
  11070. POP_OFFSET_TYPE(ref_type);
  11071. POP_TYPE(ref_type);
  11072. PUSH_OFFSET_TYPE(ref_type);
  11073. PUSH_TYPE(ref_type);
  11074. #else
  11075. POP2_AND_PUSH(ref_type, ref_type);
  11076. #endif
  11077. }
  11078. else {
  11079. #if WASM_ENABLE_FAST_INTERP != 0
  11080. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11081. #endif
  11082. PUSH_TYPE(VALUE_TYPE_ANY);
  11083. }
  11084. break;
  11085. }
  11086. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11087. case WASM_OP_SELECT_T:
  11088. {
  11089. uint8 vec_len, type;
  11090. #if WASM_ENABLE_GC != 0
  11091. WASMRefType *ref_type = NULL;
  11092. #endif
  11093. #if WASM_ENABLE_FAST_INTERP != 0
  11094. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11095. #endif
  11096. read_leb_uint32(p, p_end, vec_len);
  11097. if (vec_len != 1) {
  11098. /* typed select must have exactly one result */
  11099. set_error_buf(error_buf, error_buf_size,
  11100. "invalid result arity");
  11101. goto fail;
  11102. }
  11103. #if WASM_ENABLE_GC == 0
  11104. CHECK_BUF(p, p_end, 1);
  11105. type = read_uint8(p);
  11106. if (!is_valid_value_type_for_interpreter(type)) {
  11107. set_error_buf(error_buf, error_buf_size,
  11108. "unknown value type");
  11109. goto fail;
  11110. }
  11111. #else
  11112. p_org = p + 1;
  11113. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11114. module->type_count, &need_ref_type_map,
  11115. &wasm_ref_type, false, error_buf,
  11116. error_buf_size)) {
  11117. goto fail;
  11118. }
  11119. type = wasm_ref_type.ref_type;
  11120. if (need_ref_type_map) {
  11121. if (!(ref_type = reftype_set_insert(
  11122. module->ref_type_set, &wasm_ref_type, error_buf,
  11123. error_buf_size))) {
  11124. goto fail;
  11125. }
  11126. }
  11127. #if WASM_ENABLE_FAST_INTERP == 0
  11128. while (p_org < p) {
  11129. #if WASM_ENABLE_DEBUG_INTERP != 0
  11130. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11131. error_buf_size)) {
  11132. goto fail;
  11133. }
  11134. #endif
  11135. /* Ignore extra bytes for interpreter */
  11136. *p_org++ = WASM_OP_NOP;
  11137. }
  11138. #endif
  11139. #endif /* end of WASM_ENABLE_GC == 0 */
  11140. POP_I32();
  11141. #if WASM_ENABLE_FAST_INTERP != 0
  11142. if (loader_ctx->p_code_compiled) {
  11143. uint8 opcode_tmp = WASM_OP_SELECT;
  11144. if (type == VALUE_TYPE_V128) {
  11145. #if (WASM_ENABLE_SIMD == 0) \
  11146. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  11147. set_error_buf(error_buf, error_buf_size,
  11148. "SIMD v128 type isn't supported");
  11149. goto fail;
  11150. #endif
  11151. }
  11152. else {
  11153. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11154. opcode_tmp = WASM_OP_SELECT_64;
  11155. #if WASM_ENABLE_GC != 0
  11156. if (wasm_is_type_reftype(type))
  11157. opcode_tmp = WASM_OP_SELECT_T;
  11158. #endif
  11159. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11160. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11161. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11162. handle_table[opcode_tmp];
  11163. #else
  11164. #if UINTPTR_MAX == UINT64_MAX
  11165. /* emit int32 relative offset in 64-bit target */
  11166. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11167. - (uint8 *)handle_table[0]);
  11168. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11169. offset;
  11170. #else
  11171. /* emit uint32 label address in 32-bit target */
  11172. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11173. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11174. #endif
  11175. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11176. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11177. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11178. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11179. #else
  11180. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11181. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11182. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11183. }
  11184. }
  11185. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11186. POP_REF(type);
  11187. #if WASM_ENABLE_GC != 0
  11188. if (need_ref_type_map) {
  11189. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11190. wasm_reftype_struct_size(ref_type));
  11191. }
  11192. #endif
  11193. POP_REF(type);
  11194. #if WASM_ENABLE_GC != 0
  11195. if (need_ref_type_map) {
  11196. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11197. wasm_reftype_struct_size(ref_type));
  11198. }
  11199. #endif
  11200. PUSH_REF(type);
  11201. #if WASM_ENABLE_WAMR_COMPILER != 0
  11202. module->is_ref_types_used = true;
  11203. #endif
  11204. (void)vec_len;
  11205. break;
  11206. }
  11207. /* table.get x. tables[x]. [i32] -> [t] */
  11208. /* table.set x. tables[x]. [i32 t] -> [] */
  11209. case WASM_OP_TABLE_GET:
  11210. case WASM_OP_TABLE_SET:
  11211. {
  11212. uint8 decl_ref_type;
  11213. #if WASM_ENABLE_GC != 0
  11214. WASMRefType *ref_type;
  11215. #endif
  11216. read_leb_uint32(p, p_end, table_idx);
  11217. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11218. #if WASM_ENABLE_GC != 0
  11219. (void **)&ref_type,
  11220. #else
  11221. NULL,
  11222. #endif
  11223. error_buf, error_buf_size))
  11224. goto fail;
  11225. #if WASM_ENABLE_GC != 0
  11226. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11227. bh_assert(ref_type);
  11228. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11229. ref_type, wasm_reftype_struct_size(ref_type));
  11230. }
  11231. #endif
  11232. #if WASM_ENABLE_FAST_INTERP != 0
  11233. emit_uint32(loader_ctx, table_idx);
  11234. #endif
  11235. if (opcode == WASM_OP_TABLE_GET) {
  11236. POP_I32();
  11237. #if WASM_ENABLE_FAST_INTERP != 0
  11238. PUSH_OFFSET_TYPE(decl_ref_type);
  11239. #endif
  11240. PUSH_TYPE(decl_ref_type);
  11241. }
  11242. else {
  11243. #if WASM_ENABLE_FAST_INTERP != 0
  11244. POP_OFFSET_TYPE(decl_ref_type);
  11245. #endif
  11246. POP_TYPE(decl_ref_type);
  11247. POP_I32();
  11248. }
  11249. #if WASM_ENABLE_WAMR_COMPILER != 0
  11250. module->is_ref_types_used = true;
  11251. #endif
  11252. break;
  11253. }
  11254. case WASM_OP_REF_NULL:
  11255. {
  11256. uint8 ref_type;
  11257. #if WASM_ENABLE_GC == 0
  11258. CHECK_BUF(p, p_end, 1);
  11259. ref_type = read_uint8(p);
  11260. if (ref_type != VALUE_TYPE_FUNCREF
  11261. && ref_type != VALUE_TYPE_EXTERNREF) {
  11262. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11263. goto fail;
  11264. }
  11265. #else
  11266. read_leb_int32(p, p_end, heap_type);
  11267. if (heap_type >= 0) {
  11268. if (!check_type_index(module, module->type_count, heap_type,
  11269. error_buf, error_buf_size)) {
  11270. goto fail;
  11271. }
  11272. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11273. true, heap_type);
  11274. ref_type = wasm_ref_type.ref_type;
  11275. }
  11276. else {
  11277. if (!wasm_is_valid_heap_type(heap_type)) {
  11278. set_error_buf(error_buf, error_buf_size,
  11279. "unknown type");
  11280. goto fail;
  11281. }
  11282. ref_type = (uint8)((int32)0x80 + heap_type);
  11283. }
  11284. #endif /* end of WASM_ENABLE_GC == 0 */
  11285. #if WASM_ENABLE_FAST_INTERP != 0
  11286. PUSH_OFFSET_TYPE(ref_type);
  11287. #endif
  11288. PUSH_TYPE(ref_type);
  11289. #if WASM_ENABLE_WAMR_COMPILER != 0
  11290. module->is_ref_types_used = true;
  11291. #endif
  11292. break;
  11293. }
  11294. case WASM_OP_REF_IS_NULL:
  11295. {
  11296. #if WASM_ENABLE_GC == 0
  11297. #if WASM_ENABLE_FAST_INTERP != 0
  11298. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11299. int32 block_stack_cell_num =
  11300. (int32)(loader_ctx->stack_cell_num
  11301. - cur_block->stack_cell_num);
  11302. if (block_stack_cell_num <= 0) {
  11303. if (!cur_block->is_stack_polymorphic) {
  11304. set_error_buf(
  11305. error_buf, error_buf_size,
  11306. "type mismatch: expect data but stack was empty");
  11307. goto fail;
  11308. }
  11309. }
  11310. else {
  11311. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11312. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11313. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11314. if (!wasm_loader_pop_frame_ref_offset(
  11315. loader_ctx, *(loader_ctx->frame_ref - 1),
  11316. error_buf, error_buf_size)) {
  11317. goto fail;
  11318. }
  11319. }
  11320. else {
  11321. set_error_buf(error_buf, error_buf_size,
  11322. "type mismatch");
  11323. goto fail;
  11324. }
  11325. }
  11326. #else
  11327. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11328. error_buf, error_buf_size)
  11329. && !wasm_loader_pop_frame_ref(loader_ctx,
  11330. VALUE_TYPE_EXTERNREF,
  11331. error_buf, error_buf_size)) {
  11332. goto fail;
  11333. }
  11334. #endif
  11335. #else /* else of WASM_ENABLE_GC == 0 */
  11336. uint8 type;
  11337. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11338. error_buf, error_buf_size)) {
  11339. goto fail;
  11340. }
  11341. #endif
  11342. PUSH_I32();
  11343. #if WASM_ENABLE_WAMR_COMPILER != 0
  11344. module->is_ref_types_used = true;
  11345. #endif
  11346. break;
  11347. }
  11348. case WASM_OP_REF_FUNC:
  11349. {
  11350. read_leb_uint32(p, p_end, func_idx);
  11351. if (!check_function_index(module, func_idx, error_buf,
  11352. error_buf_size)) {
  11353. goto fail;
  11354. }
  11355. /* Refer to a forward-declared function:
  11356. the function must be an import, exported, or present in
  11357. a table elem segment or global initializer to be used as
  11358. the operand to ref.func */
  11359. if (func_idx >= module->import_function_count) {
  11360. WASMTableSeg *table_seg = module->table_segments;
  11361. bool func_declared = false;
  11362. uint32 j;
  11363. for (i = 0; i < module->global_count; i++) {
  11364. if (module->globals[i].type.val_type
  11365. == VALUE_TYPE_FUNCREF
  11366. && module->globals[i].init_expr.init_expr_type
  11367. == INIT_EXPR_TYPE_FUNCREF_CONST
  11368. && module->globals[i].init_expr.u.u32 == func_idx) {
  11369. func_declared = true;
  11370. break;
  11371. }
  11372. }
  11373. if (!func_declared) {
  11374. /* Check whether the function is declared in table segs,
  11375. note that it doesn't matter whether the table seg's
  11376. mode is passive, active or declarative. */
  11377. for (i = 0; i < module->table_seg_count;
  11378. i++, table_seg++) {
  11379. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11380. #if WASM_ENABLE_GC != 0
  11381. /* elem type is (ref null? func) or
  11382. (ref null? $t) */
  11383. || ((table_seg->elem_type
  11384. == REF_TYPE_HT_NON_NULLABLE
  11385. || table_seg->elem_type
  11386. == REF_TYPE_HT_NULLABLE)
  11387. && (table_seg->elem_ref_type->ref_ht_common
  11388. .heap_type
  11389. == HEAP_TYPE_FUNC
  11390. || table_seg->elem_ref_type
  11391. ->ref_ht_common.heap_type
  11392. > 0))
  11393. #endif
  11394. ) {
  11395. for (j = 0; j < table_seg->value_count; j++) {
  11396. if (table_seg->init_values[j].u.ref_index
  11397. == func_idx) {
  11398. func_declared = true;
  11399. break;
  11400. }
  11401. }
  11402. }
  11403. }
  11404. }
  11405. if (!func_declared) {
  11406. /* Check whether the function is exported */
  11407. for (i = 0; i < module->export_count; i++) {
  11408. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11409. && module->exports[i].index == func_idx) {
  11410. func_declared = true;
  11411. break;
  11412. }
  11413. }
  11414. }
  11415. if (!func_declared) {
  11416. set_error_buf(error_buf, error_buf_size,
  11417. "undeclared function reference");
  11418. goto fail;
  11419. }
  11420. }
  11421. #if WASM_ENABLE_FAST_INTERP != 0
  11422. emit_uint32(loader_ctx, func_idx);
  11423. #endif
  11424. #if WASM_ENABLE_GC == 0
  11425. PUSH_FUNCREF();
  11426. #else
  11427. if (func_idx < module->import_function_count)
  11428. type_idx =
  11429. module->import_functions[func_idx].u.function.type_idx;
  11430. else
  11431. type_idx = module
  11432. ->functions[func_idx
  11433. - module->import_function_count]
  11434. ->type_idx;
  11435. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11436. false, type_idx);
  11437. PUSH_REF(wasm_ref_type.ref_type);
  11438. #endif
  11439. #if WASM_ENABLE_WAMR_COMPILER != 0
  11440. module->is_ref_types_used = true;
  11441. #endif
  11442. break;
  11443. }
  11444. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11445. #if WASM_ENABLE_GC != 0
  11446. case WASM_OP_REF_AS_NON_NULL:
  11447. case WASM_OP_BR_ON_NULL:
  11448. {
  11449. uint8 type;
  11450. WASMRefType ref_type;
  11451. /* POP (ref null ht) and get the converted (ref ht) */
  11452. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11453. error_buf, error_buf_size)) {
  11454. goto fail;
  11455. }
  11456. if (opcode == WASM_OP_BR_ON_NULL) {
  11457. if (!(frame_csp_tmp =
  11458. check_branch_block(loader_ctx, &p, p_end, opcode,
  11459. error_buf, error_buf_size))) {
  11460. goto fail;
  11461. }
  11462. #if WASM_ENABLE_FAST_INTERP != 0
  11463. disable_emit = true;
  11464. #endif
  11465. }
  11466. /* PUSH the converted (ref ht) */
  11467. if (type != VALUE_TYPE_ANY) {
  11468. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11469. sizeof(WASMRefType));
  11470. }
  11471. PUSH_REF(type);
  11472. break;
  11473. }
  11474. case WASM_OP_BR_ON_NON_NULL:
  11475. {
  11476. uint8 type;
  11477. WASMRefType ref_type;
  11478. uint32 available_stack_cell =
  11479. loader_ctx->stack_cell_num
  11480. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11481. /* POP (ref null ht) and get the converted (ref ht) */
  11482. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11483. error_buf, error_buf_size)) {
  11484. goto fail;
  11485. }
  11486. #if WASM_ENABLE_FAST_INTERP != 0
  11487. disable_emit = true;
  11488. #endif
  11489. /* Temporarily PUSH back (ref ht), check brach block and
  11490. then POP it */
  11491. if (available_stack_cell
  11492. > 0) { /* stack isn't in polymorphic state */
  11493. if (type != VALUE_TYPE_ANY) {
  11494. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11495. &ref_type, sizeof(WASMRefType));
  11496. }
  11497. PUSH_REF(type);
  11498. }
  11499. if (!(frame_csp_tmp =
  11500. check_branch_block(loader_ctx, &p, p_end, opcode,
  11501. error_buf, error_buf_size))) {
  11502. goto fail;
  11503. }
  11504. if (available_stack_cell
  11505. > 0) { /* stack isn't in polymorphic state */
  11506. POP_REF(type);
  11507. #if WASM_ENABLE_FAST_INTERP != 0
  11508. /* Erase the opnd offset emitted by POP_REF() */
  11509. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11510. #endif
  11511. }
  11512. break;
  11513. }
  11514. case WASM_OP_REF_EQ:
  11515. POP_REF(REF_TYPE_EQREF);
  11516. POP_REF(REF_TYPE_EQREF);
  11517. PUSH_I32();
  11518. break;
  11519. #endif /* end of WASM_ENABLE_GC != 0 */
  11520. case WASM_OP_GET_LOCAL:
  11521. {
  11522. p_org = p - 1;
  11523. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11524. PUSH_TYPE(local_type);
  11525. #if WASM_ENABLE_GC != 0
  11526. /* Cannot get a non-nullable and unset local */
  11527. if (local_idx >= param_count
  11528. && wasm_is_reftype_htref_non_nullable(local_type)
  11529. && !wasm_loader_get_local_status(loader_ctx,
  11530. local_idx - param_count)) {
  11531. set_error_buf(error_buf, error_buf_size,
  11532. "uninitialized local");
  11533. goto fail;
  11534. }
  11535. #endif
  11536. #if WASM_ENABLE_FAST_INTERP != 0
  11537. /* Get Local is optimized out */
  11538. skip_label();
  11539. disable_emit = true;
  11540. operand_offset = local_offset;
  11541. PUSH_OFFSET_TYPE(local_type);
  11542. #else
  11543. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11544. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11545. if (local_offset < 0x80
  11546. #if WASM_ENABLE_GC != 0
  11547. && !wasm_is_type_reftype(local_type)
  11548. #endif
  11549. ) {
  11550. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11551. if (is_32bit_type(local_type)) {
  11552. *p_org++ = (uint8)local_offset;
  11553. }
  11554. else {
  11555. *p_org++ = (uint8)(local_offset | 0x80);
  11556. }
  11557. while (p_org < p) {
  11558. *p_org++ = WASM_OP_NOP;
  11559. }
  11560. }
  11561. #endif
  11562. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11563. break;
  11564. }
  11565. case WASM_OP_SET_LOCAL:
  11566. {
  11567. p_org = p - 1;
  11568. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11569. #if WASM_ENABLE_FAST_INTERP != 0
  11570. if (!(preserve_referenced_local(
  11571. loader_ctx, opcode, local_offset, local_type,
  11572. &preserve_local, error_buf, error_buf_size)))
  11573. goto fail;
  11574. if (local_offset < 256
  11575. #if WASM_ENABLE_GC != 0
  11576. && !wasm_is_type_reftype(local_type)
  11577. #endif
  11578. ) {
  11579. skip_label();
  11580. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11581. if (loader_ctx->p_code_compiled)
  11582. STORE_U16(loader_ctx->p_code_compiled - 2,
  11583. local_offset);
  11584. loader_ctx->frame_offset--;
  11585. loader_ctx->dynamic_offset--;
  11586. }
  11587. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11588. if (loader_ctx->p_code_compiled)
  11589. STORE_U16(loader_ctx->p_code_compiled - 2,
  11590. local_offset);
  11591. loader_ctx->frame_offset -= 2;
  11592. loader_ctx->dynamic_offset -= 2;
  11593. }
  11594. else {
  11595. if (is_32bit_type(local_type)) {
  11596. emit_label(EXT_OP_SET_LOCAL_FAST);
  11597. emit_byte(loader_ctx, (uint8)local_offset);
  11598. }
  11599. else {
  11600. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11601. emit_byte(loader_ctx, (uint8)local_offset);
  11602. }
  11603. POP_OFFSET_TYPE(local_type);
  11604. }
  11605. }
  11606. else { /* local index larger than 255, reserve leb */
  11607. emit_uint32(loader_ctx, local_idx);
  11608. POP_OFFSET_TYPE(local_type);
  11609. }
  11610. #else
  11611. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11612. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11613. if (local_offset < 0x80
  11614. #if WASM_ENABLE_GC != 0
  11615. && !wasm_is_type_reftype(local_type)
  11616. #endif
  11617. ) {
  11618. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11619. if (is_32bit_type(local_type)) {
  11620. *p_org++ = (uint8)local_offset;
  11621. }
  11622. else {
  11623. *p_org++ = (uint8)(local_offset | 0x80);
  11624. }
  11625. while (p_org < p) {
  11626. *p_org++ = WASM_OP_NOP;
  11627. }
  11628. }
  11629. #endif
  11630. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11631. #if WASM_ENABLE_GC != 0
  11632. if (local_idx >= param_count) {
  11633. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11634. }
  11635. #endif
  11636. POP_TYPE(local_type);
  11637. break;
  11638. }
  11639. case WASM_OP_TEE_LOCAL:
  11640. {
  11641. p_org = p - 1;
  11642. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11643. #if WASM_ENABLE_FAST_INTERP != 0
  11644. /* If the stack is in polymorphic state, do fake pop and push on
  11645. offset stack to keep the depth of offset stack to be the
  11646. same with ref stack */
  11647. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11648. if (cur_block->is_stack_polymorphic) {
  11649. POP_OFFSET_TYPE(local_type);
  11650. PUSH_OFFSET_TYPE(local_type);
  11651. }
  11652. #endif
  11653. POP_TYPE(local_type);
  11654. PUSH_TYPE(local_type);
  11655. #if WASM_ENABLE_FAST_INTERP != 0
  11656. if (!(preserve_referenced_local(
  11657. loader_ctx, opcode, local_offset, local_type,
  11658. &preserve_local, error_buf, error_buf_size)))
  11659. goto fail;
  11660. if (local_offset < 256
  11661. #if WASM_ENABLE_GC != 0
  11662. && !wasm_is_type_reftype(local_type)
  11663. #endif
  11664. ) {
  11665. skip_label();
  11666. if (is_32bit_type(local_type)) {
  11667. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11668. emit_byte(loader_ctx, (uint8)local_offset);
  11669. }
  11670. else {
  11671. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11672. emit_byte(loader_ctx, (uint8)local_offset);
  11673. }
  11674. }
  11675. else { /* local index larger than 255, reserve leb */
  11676. emit_uint32(loader_ctx, local_idx);
  11677. }
  11678. emit_operand(loader_ctx,
  11679. *(loader_ctx->frame_offset
  11680. - wasm_value_type_cell_num(local_type)));
  11681. #else
  11682. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11683. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11684. if (local_offset < 0x80
  11685. #if WASM_ENABLE_GC != 0
  11686. && !wasm_is_type_reftype(local_type)
  11687. #endif
  11688. ) {
  11689. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11690. if (is_32bit_type(local_type)) {
  11691. *p_org++ = (uint8)local_offset;
  11692. }
  11693. else {
  11694. *p_org++ = (uint8)(local_offset | 0x80);
  11695. }
  11696. while (p_org < p) {
  11697. *p_org++ = WASM_OP_NOP;
  11698. }
  11699. }
  11700. #endif
  11701. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11702. #if WASM_ENABLE_GC != 0
  11703. if (local_idx >= param_count) {
  11704. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11705. }
  11706. #endif
  11707. break;
  11708. }
  11709. case WASM_OP_GET_GLOBAL:
  11710. {
  11711. #if WASM_ENABLE_GC != 0
  11712. WASMRefType *ref_type;
  11713. #endif
  11714. p_org = p - 1;
  11715. read_leb_uint32(p, p_end, global_idx);
  11716. if (global_idx >= global_count) {
  11717. set_error_buf(error_buf, error_buf_size, "unknown global");
  11718. goto fail;
  11719. }
  11720. global_type = global_idx < module->import_global_count
  11721. ? module->import_globals[global_idx]
  11722. .u.global.type.val_type
  11723. : module
  11724. ->globals[global_idx
  11725. - module->import_global_count]
  11726. .type.val_type;
  11727. #if WASM_ENABLE_GC != 0
  11728. ref_type =
  11729. global_idx < module->import_global_count
  11730. ? module->import_globals[global_idx].u.global.ref_type
  11731. : module
  11732. ->globals[global_idx
  11733. - module->import_global_count]
  11734. .ref_type;
  11735. if (wasm_is_type_multi_byte_type(global_type)) {
  11736. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11737. wasm_reftype_struct_size(ref_type));
  11738. }
  11739. #endif
  11740. PUSH_TYPE(global_type);
  11741. #if WASM_ENABLE_FAST_INTERP == 0
  11742. if (global_type == VALUE_TYPE_I64
  11743. || global_type == VALUE_TYPE_F64) {
  11744. #if WASM_ENABLE_DEBUG_INTERP != 0
  11745. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11746. error_buf_size)) {
  11747. goto fail;
  11748. }
  11749. #endif
  11750. *p_org = WASM_OP_GET_GLOBAL_64;
  11751. }
  11752. #else /* else of WASM_ENABLE_FAST_INTERP */
  11753. if (global_type == VALUE_TYPE_I64
  11754. || global_type == VALUE_TYPE_F64) {
  11755. skip_label();
  11756. emit_label(WASM_OP_GET_GLOBAL_64);
  11757. }
  11758. emit_uint32(loader_ctx, global_idx);
  11759. PUSH_OFFSET_TYPE(global_type);
  11760. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11761. break;
  11762. }
  11763. case WASM_OP_SET_GLOBAL:
  11764. {
  11765. bool is_mutable = false;
  11766. #if WASM_ENABLE_GC != 0
  11767. WASMRefType *ref_type;
  11768. #endif
  11769. p_org = p - 1;
  11770. read_leb_uint32(p, p_end, global_idx);
  11771. if (global_idx >= global_count) {
  11772. set_error_buf(error_buf, error_buf_size, "unknown global");
  11773. goto fail;
  11774. }
  11775. is_mutable = global_idx < module->import_global_count
  11776. ? module->import_globals[global_idx]
  11777. .u.global.type.is_mutable
  11778. : module
  11779. ->globals[global_idx
  11780. - module->import_global_count]
  11781. .type.is_mutable;
  11782. if (!is_mutable) {
  11783. #if WASM_ENABLE_GC == 0
  11784. set_error_buf(error_buf, error_buf_size,
  11785. "global is immutable");
  11786. #else
  11787. set_error_buf(error_buf, error_buf_size,
  11788. "immutable global");
  11789. #endif
  11790. goto fail;
  11791. }
  11792. global_type = global_idx < module->import_global_count
  11793. ? module->import_globals[global_idx]
  11794. .u.global.type.val_type
  11795. : module
  11796. ->globals[global_idx
  11797. - module->import_global_count]
  11798. .type.val_type;
  11799. #if WASM_ENABLE_GC != 0
  11800. ref_type =
  11801. global_idx < module->import_global_count
  11802. ? module->import_globals[global_idx].u.global.ref_type
  11803. : module
  11804. ->globals[global_idx
  11805. - module->import_global_count]
  11806. .ref_type;
  11807. if (wasm_is_type_multi_byte_type(global_type)) {
  11808. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11809. wasm_reftype_struct_size(ref_type));
  11810. }
  11811. #endif
  11812. #if WASM_ENABLE_FAST_INTERP == 0
  11813. if (global_type == VALUE_TYPE_I64
  11814. || global_type == VALUE_TYPE_F64) {
  11815. #if WASM_ENABLE_DEBUG_INTERP != 0
  11816. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11817. error_buf_size)) {
  11818. goto fail;
  11819. }
  11820. #endif
  11821. *p_org = WASM_OP_SET_GLOBAL_64;
  11822. }
  11823. else if (module->aux_stack_size > 0
  11824. && global_idx == module->aux_stack_top_global_index) {
  11825. #if WASM_ENABLE_DEBUG_INTERP != 0
  11826. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11827. error_buf_size)) {
  11828. goto fail;
  11829. }
  11830. #endif
  11831. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11832. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11833. func->has_op_set_global_aux_stack = true;
  11834. #endif
  11835. }
  11836. #else /* else of WASM_ENABLE_FAST_INTERP */
  11837. if (global_type == VALUE_TYPE_I64
  11838. || global_type == VALUE_TYPE_F64) {
  11839. skip_label();
  11840. emit_label(WASM_OP_SET_GLOBAL_64);
  11841. }
  11842. else if (module->aux_stack_size > 0
  11843. && global_idx == module->aux_stack_top_global_index) {
  11844. skip_label();
  11845. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11846. }
  11847. emit_uint32(loader_ctx, global_idx);
  11848. POP_OFFSET_TYPE(global_type);
  11849. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11850. POP_TYPE(global_type);
  11851. break;
  11852. }
  11853. /* load */
  11854. case WASM_OP_I32_LOAD:
  11855. case WASM_OP_I32_LOAD8_S:
  11856. case WASM_OP_I32_LOAD8_U:
  11857. case WASM_OP_I32_LOAD16_S:
  11858. case WASM_OP_I32_LOAD16_U:
  11859. case WASM_OP_I64_LOAD:
  11860. case WASM_OP_I64_LOAD8_S:
  11861. case WASM_OP_I64_LOAD8_U:
  11862. case WASM_OP_I64_LOAD16_S:
  11863. case WASM_OP_I64_LOAD16_U:
  11864. case WASM_OP_I64_LOAD32_S:
  11865. case WASM_OP_I64_LOAD32_U:
  11866. case WASM_OP_F32_LOAD:
  11867. case WASM_OP_F64_LOAD:
  11868. /* store */
  11869. case WASM_OP_I32_STORE:
  11870. case WASM_OP_I32_STORE8:
  11871. case WASM_OP_I32_STORE16:
  11872. case WASM_OP_I64_STORE:
  11873. case WASM_OP_I64_STORE8:
  11874. case WASM_OP_I64_STORE16:
  11875. case WASM_OP_I64_STORE32:
  11876. case WASM_OP_F32_STORE:
  11877. case WASM_OP_F64_STORE:
  11878. {
  11879. #if WASM_ENABLE_FAST_INTERP != 0
  11880. /* change F32/F64 into I32/I64 */
  11881. if (opcode == WASM_OP_F32_LOAD) {
  11882. skip_label();
  11883. emit_label(WASM_OP_I32_LOAD);
  11884. }
  11885. else if (opcode == WASM_OP_F64_LOAD) {
  11886. skip_label();
  11887. emit_label(WASM_OP_I64_LOAD);
  11888. }
  11889. else if (opcode == WASM_OP_F32_STORE) {
  11890. skip_label();
  11891. emit_label(WASM_OP_I32_STORE);
  11892. }
  11893. else if (opcode == WASM_OP_F64_STORE) {
  11894. skip_label();
  11895. emit_label(WASM_OP_I64_STORE);
  11896. }
  11897. #endif
  11898. CHECK_MEMORY();
  11899. read_leb_memarg(p, p_end, align); /* align */
  11900. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  11901. if (!check_memory_access_align(opcode, align, error_buf,
  11902. error_buf_size)) {
  11903. goto fail;
  11904. }
  11905. #if WASM_ENABLE_FAST_INTERP != 0
  11906. emit_uint32(loader_ctx, mem_offset);
  11907. #endif
  11908. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11909. func->has_memory_operations = true;
  11910. #endif
  11911. switch (opcode) {
  11912. /* load */
  11913. case WASM_OP_I32_LOAD:
  11914. case WASM_OP_I32_LOAD8_S:
  11915. case WASM_OP_I32_LOAD8_U:
  11916. case WASM_OP_I32_LOAD16_S:
  11917. case WASM_OP_I32_LOAD16_U:
  11918. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  11919. break;
  11920. case WASM_OP_I64_LOAD:
  11921. case WASM_OP_I64_LOAD8_S:
  11922. case WASM_OP_I64_LOAD8_U:
  11923. case WASM_OP_I64_LOAD16_S:
  11924. case WASM_OP_I64_LOAD16_U:
  11925. case WASM_OP_I64_LOAD32_S:
  11926. case WASM_OP_I64_LOAD32_U:
  11927. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  11928. break;
  11929. case WASM_OP_F32_LOAD:
  11930. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  11931. break;
  11932. case WASM_OP_F64_LOAD:
  11933. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  11934. break;
  11935. /* store */
  11936. case WASM_OP_I32_STORE:
  11937. case WASM_OP_I32_STORE8:
  11938. case WASM_OP_I32_STORE16:
  11939. POP_I32();
  11940. POP_MEM_OFFSET();
  11941. break;
  11942. case WASM_OP_I64_STORE:
  11943. case WASM_OP_I64_STORE8:
  11944. case WASM_OP_I64_STORE16:
  11945. case WASM_OP_I64_STORE32:
  11946. POP_I64();
  11947. POP_MEM_OFFSET();
  11948. break;
  11949. case WASM_OP_F32_STORE:
  11950. POP_F32();
  11951. POP_MEM_OFFSET();
  11952. break;
  11953. case WASM_OP_F64_STORE:
  11954. POP_F64();
  11955. POP_MEM_OFFSET();
  11956. break;
  11957. default:
  11958. break;
  11959. }
  11960. break;
  11961. }
  11962. case WASM_OP_MEMORY_SIZE:
  11963. CHECK_MEMORY();
  11964. read_leb_uint32(p, p_end, memidx);
  11965. check_memidx(module, memidx);
  11966. PUSH_PAGE_COUNT();
  11967. module->possible_memory_grow = true;
  11968. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11969. func->has_memory_operations = true;
  11970. #endif
  11971. break;
  11972. case WASM_OP_MEMORY_GROW:
  11973. CHECK_MEMORY();
  11974. read_leb_uint32(p, p_end, memidx);
  11975. check_memidx(module, memidx);
  11976. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  11977. module->possible_memory_grow = true;
  11978. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11979. || WASM_ENABLE_WAMR_COMPILER != 0
  11980. func->has_op_memory_grow = true;
  11981. #endif
  11982. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11983. func->has_memory_operations = true;
  11984. #endif
  11985. break;
  11986. case WASM_OP_I32_CONST:
  11987. read_leb_int32(p, p_end, i32_const);
  11988. #if WASM_ENABLE_FAST_INTERP != 0
  11989. skip_label();
  11990. disable_emit = true;
  11991. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  11992. if (operand_offset == 0) {
  11993. disable_emit = false;
  11994. emit_label(WASM_OP_I32_CONST);
  11995. emit_uint32(loader_ctx, i32_const);
  11996. }
  11997. #else
  11998. (void)i32_const;
  11999. #endif
  12000. PUSH_I32();
  12001. break;
  12002. case WASM_OP_I64_CONST:
  12003. read_leb_int64(p, p_end, i64_const);
  12004. #if WASM_ENABLE_FAST_INTERP != 0
  12005. skip_label();
  12006. disable_emit = true;
  12007. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  12008. if (operand_offset == 0) {
  12009. disable_emit = false;
  12010. emit_label(WASM_OP_I64_CONST);
  12011. emit_uint64(loader_ctx, i64_const);
  12012. }
  12013. #endif
  12014. PUSH_I64();
  12015. break;
  12016. case WASM_OP_F32_CONST:
  12017. CHECK_BUF(p, p_end, sizeof(float32));
  12018. p += sizeof(float32);
  12019. #if WASM_ENABLE_FAST_INTERP != 0
  12020. skip_label();
  12021. disable_emit = true;
  12022. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12023. sizeof(float32));
  12024. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12025. if (operand_offset == 0) {
  12026. disable_emit = false;
  12027. emit_label(WASM_OP_F32_CONST);
  12028. emit_float32(loader_ctx, f32_const);
  12029. }
  12030. #endif
  12031. PUSH_F32();
  12032. break;
  12033. case WASM_OP_F64_CONST:
  12034. CHECK_BUF(p, p_end, sizeof(float64));
  12035. p += sizeof(float64);
  12036. #if WASM_ENABLE_FAST_INTERP != 0
  12037. skip_label();
  12038. disable_emit = true;
  12039. /* Some MCU may require 8-byte align */
  12040. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12041. sizeof(float64));
  12042. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12043. if (operand_offset == 0) {
  12044. disable_emit = false;
  12045. emit_label(WASM_OP_F64_CONST);
  12046. emit_float64(loader_ctx, f64_const);
  12047. }
  12048. #endif
  12049. PUSH_F64();
  12050. break;
  12051. case WASM_OP_I32_EQZ:
  12052. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12053. break;
  12054. case WASM_OP_I32_EQ:
  12055. case WASM_OP_I32_NE:
  12056. case WASM_OP_I32_LT_S:
  12057. case WASM_OP_I32_LT_U:
  12058. case WASM_OP_I32_GT_S:
  12059. case WASM_OP_I32_GT_U:
  12060. case WASM_OP_I32_LE_S:
  12061. case WASM_OP_I32_LE_U:
  12062. case WASM_OP_I32_GE_S:
  12063. case WASM_OP_I32_GE_U:
  12064. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12065. break;
  12066. case WASM_OP_I64_EQZ:
  12067. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12068. break;
  12069. case WASM_OP_I64_EQ:
  12070. case WASM_OP_I64_NE:
  12071. case WASM_OP_I64_LT_S:
  12072. case WASM_OP_I64_LT_U:
  12073. case WASM_OP_I64_GT_S:
  12074. case WASM_OP_I64_GT_U:
  12075. case WASM_OP_I64_LE_S:
  12076. case WASM_OP_I64_LE_U:
  12077. case WASM_OP_I64_GE_S:
  12078. case WASM_OP_I64_GE_U:
  12079. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12080. break;
  12081. case WASM_OP_F32_EQ:
  12082. case WASM_OP_F32_NE:
  12083. case WASM_OP_F32_LT:
  12084. case WASM_OP_F32_GT:
  12085. case WASM_OP_F32_LE:
  12086. case WASM_OP_F32_GE:
  12087. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12088. break;
  12089. case WASM_OP_F64_EQ:
  12090. case WASM_OP_F64_NE:
  12091. case WASM_OP_F64_LT:
  12092. case WASM_OP_F64_GT:
  12093. case WASM_OP_F64_LE:
  12094. case WASM_OP_F64_GE:
  12095. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12096. break;
  12097. case WASM_OP_I32_CLZ:
  12098. case WASM_OP_I32_CTZ:
  12099. case WASM_OP_I32_POPCNT:
  12100. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12101. break;
  12102. case WASM_OP_I32_ADD:
  12103. case WASM_OP_I32_SUB:
  12104. case WASM_OP_I32_MUL:
  12105. case WASM_OP_I32_DIV_S:
  12106. case WASM_OP_I32_DIV_U:
  12107. case WASM_OP_I32_REM_S:
  12108. case WASM_OP_I32_REM_U:
  12109. case WASM_OP_I32_AND:
  12110. case WASM_OP_I32_OR:
  12111. case WASM_OP_I32_XOR:
  12112. case WASM_OP_I32_SHL:
  12113. case WASM_OP_I32_SHR_S:
  12114. case WASM_OP_I32_SHR_U:
  12115. case WASM_OP_I32_ROTL:
  12116. case WASM_OP_I32_ROTR:
  12117. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12118. break;
  12119. case WASM_OP_I64_CLZ:
  12120. case WASM_OP_I64_CTZ:
  12121. case WASM_OP_I64_POPCNT:
  12122. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12123. break;
  12124. case WASM_OP_I64_ADD:
  12125. case WASM_OP_I64_SUB:
  12126. case WASM_OP_I64_MUL:
  12127. case WASM_OP_I64_DIV_S:
  12128. case WASM_OP_I64_DIV_U:
  12129. case WASM_OP_I64_REM_S:
  12130. case WASM_OP_I64_REM_U:
  12131. case WASM_OP_I64_AND:
  12132. case WASM_OP_I64_OR:
  12133. case WASM_OP_I64_XOR:
  12134. case WASM_OP_I64_SHL:
  12135. case WASM_OP_I64_SHR_S:
  12136. case WASM_OP_I64_SHR_U:
  12137. case WASM_OP_I64_ROTL:
  12138. case WASM_OP_I64_ROTR:
  12139. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12140. break;
  12141. case WASM_OP_F32_ABS:
  12142. case WASM_OP_F32_NEG:
  12143. case WASM_OP_F32_CEIL:
  12144. case WASM_OP_F32_FLOOR:
  12145. case WASM_OP_F32_TRUNC:
  12146. case WASM_OP_F32_NEAREST:
  12147. case WASM_OP_F32_SQRT:
  12148. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12149. break;
  12150. case WASM_OP_F32_ADD:
  12151. case WASM_OP_F32_SUB:
  12152. case WASM_OP_F32_MUL:
  12153. case WASM_OP_F32_DIV:
  12154. case WASM_OP_F32_MIN:
  12155. case WASM_OP_F32_MAX:
  12156. case WASM_OP_F32_COPYSIGN:
  12157. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12158. break;
  12159. case WASM_OP_F64_ABS:
  12160. case WASM_OP_F64_NEG:
  12161. case WASM_OP_F64_CEIL:
  12162. case WASM_OP_F64_FLOOR:
  12163. case WASM_OP_F64_TRUNC:
  12164. case WASM_OP_F64_NEAREST:
  12165. case WASM_OP_F64_SQRT:
  12166. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12167. break;
  12168. case WASM_OP_F64_ADD:
  12169. case WASM_OP_F64_SUB:
  12170. case WASM_OP_F64_MUL:
  12171. case WASM_OP_F64_DIV:
  12172. case WASM_OP_F64_MIN:
  12173. case WASM_OP_F64_MAX:
  12174. case WASM_OP_F64_COPYSIGN:
  12175. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12176. break;
  12177. case WASM_OP_I32_WRAP_I64:
  12178. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12179. break;
  12180. case WASM_OP_I32_TRUNC_S_F32:
  12181. case WASM_OP_I32_TRUNC_U_F32:
  12182. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12183. break;
  12184. case WASM_OP_I32_TRUNC_S_F64:
  12185. case WASM_OP_I32_TRUNC_U_F64:
  12186. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12187. break;
  12188. case WASM_OP_I64_EXTEND_S_I32:
  12189. case WASM_OP_I64_EXTEND_U_I32:
  12190. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12191. break;
  12192. case WASM_OP_I64_TRUNC_S_F32:
  12193. case WASM_OP_I64_TRUNC_U_F32:
  12194. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12195. break;
  12196. case WASM_OP_I64_TRUNC_S_F64:
  12197. case WASM_OP_I64_TRUNC_U_F64:
  12198. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12199. break;
  12200. case WASM_OP_F32_CONVERT_S_I32:
  12201. case WASM_OP_F32_CONVERT_U_I32:
  12202. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12203. break;
  12204. case WASM_OP_F32_CONVERT_S_I64:
  12205. case WASM_OP_F32_CONVERT_U_I64:
  12206. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12207. break;
  12208. case WASM_OP_F32_DEMOTE_F64:
  12209. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12210. break;
  12211. case WASM_OP_F64_CONVERT_S_I32:
  12212. case WASM_OP_F64_CONVERT_U_I32:
  12213. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12214. break;
  12215. case WASM_OP_F64_CONVERT_S_I64:
  12216. case WASM_OP_F64_CONVERT_U_I64:
  12217. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12218. break;
  12219. case WASM_OP_F64_PROMOTE_F32:
  12220. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12221. break;
  12222. case WASM_OP_I32_REINTERPRET_F32:
  12223. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12224. break;
  12225. case WASM_OP_I64_REINTERPRET_F64:
  12226. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12227. break;
  12228. case WASM_OP_F32_REINTERPRET_I32:
  12229. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12230. break;
  12231. case WASM_OP_F64_REINTERPRET_I64:
  12232. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12233. break;
  12234. case WASM_OP_I32_EXTEND8_S:
  12235. case WASM_OP_I32_EXTEND16_S:
  12236. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12237. break;
  12238. case WASM_OP_I64_EXTEND8_S:
  12239. case WASM_OP_I64_EXTEND16_S:
  12240. case WASM_OP_I64_EXTEND32_S:
  12241. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12242. break;
  12243. #if WASM_ENABLE_GC != 0
  12244. case WASM_OP_GC_PREFIX:
  12245. {
  12246. uint32 opcode1;
  12247. read_leb_uint32(p, p_end, opcode1);
  12248. #if WASM_ENABLE_FAST_INTERP != 0
  12249. emit_byte(loader_ctx, ((uint8)opcode1));
  12250. #endif
  12251. switch (opcode1) {
  12252. case WASM_OP_STRUCT_NEW:
  12253. case WASM_OP_STRUCT_NEW_DEFAULT:
  12254. {
  12255. read_leb_uint32(p, p_end, type_idx);
  12256. #if WASM_ENABLE_FAST_INTERP != 0
  12257. emit_uint32(loader_ctx, type_idx);
  12258. #endif
  12259. if (!check_type_index(module, module->type_count,
  12260. type_idx, error_buf,
  12261. error_buf_size)) {
  12262. goto fail;
  12263. }
  12264. if (module->types[type_idx]->type_flag
  12265. != WASM_TYPE_STRUCT) {
  12266. set_error_buf(error_buf, error_buf_size,
  12267. "unkown struct type");
  12268. goto fail;
  12269. }
  12270. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12271. int32 j, k;
  12272. uint8 value_type;
  12273. uint32 ref_type_struct_size;
  12274. WASMStructType *struct_type =
  12275. (WASMStructType *)module->types[type_idx];
  12276. k = struct_type->ref_type_map_count - 1;
  12277. for (j = struct_type->field_count - 1; j >= 0;
  12278. j--) {
  12279. value_type = struct_type->fields[j].field_type;
  12280. if (wasm_is_type_reftype(value_type)) {
  12281. if (wasm_is_type_multi_byte_type(
  12282. value_type)) {
  12283. ref_type_struct_size =
  12284. wasm_reftype_struct_size(
  12285. struct_type->ref_type_maps[k]
  12286. .ref_type);
  12287. bh_memcpy_s(
  12288. &wasm_ref_type,
  12289. (uint32)sizeof(WASMRefType),
  12290. struct_type->ref_type_maps[k]
  12291. .ref_type,
  12292. ref_type_struct_size);
  12293. k--;
  12294. }
  12295. POP_REF(value_type);
  12296. }
  12297. else {
  12298. switch (value_type) {
  12299. case VALUE_TYPE_I32:
  12300. case PACKED_TYPE_I8:
  12301. case PACKED_TYPE_I16:
  12302. POP_I32();
  12303. break;
  12304. case VALUE_TYPE_I64:
  12305. POP_I64();
  12306. break;
  12307. case VALUE_TYPE_F32:
  12308. POP_F32();
  12309. break;
  12310. case VALUE_TYPE_F64:
  12311. POP_F64();
  12312. break;
  12313. default:
  12314. set_error_buf(error_buf,
  12315. error_buf_size,
  12316. "unknown type");
  12317. goto fail;
  12318. }
  12319. }
  12320. }
  12321. }
  12322. /* PUSH struct obj, (ref $t) */
  12323. wasm_set_refheaptype_typeidx(
  12324. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12325. PUSH_REF(wasm_ref_type.ref_type);
  12326. break;
  12327. }
  12328. case WASM_OP_STRUCT_GET:
  12329. case WASM_OP_STRUCT_GET_S:
  12330. case WASM_OP_STRUCT_GET_U:
  12331. case WASM_OP_STRUCT_SET:
  12332. {
  12333. WASMStructType *struct_type;
  12334. WASMRefType *ref_type = NULL;
  12335. uint32 field_idx;
  12336. uint8 field_type;
  12337. read_leb_uint32(p, p_end, type_idx);
  12338. #if WASM_ENABLE_FAST_INTERP != 0
  12339. emit_uint32(loader_ctx, type_idx);
  12340. #endif
  12341. if (!check_type_index(module, module->type_count,
  12342. type_idx, error_buf,
  12343. error_buf_size)) {
  12344. goto fail;
  12345. }
  12346. if (module->types[type_idx]->type_flag
  12347. != WASM_TYPE_STRUCT) {
  12348. set_error_buf(error_buf, error_buf_size,
  12349. "unknown struct type");
  12350. goto fail;
  12351. }
  12352. struct_type = (WASMStructType *)module->types[type_idx];
  12353. read_leb_uint32(p, p_end, field_idx);
  12354. #if WASM_ENABLE_FAST_INTERP != 0
  12355. emit_uint32(loader_ctx, field_idx);
  12356. #endif
  12357. if (field_idx >= struct_type->field_count) {
  12358. set_error_buf(error_buf, error_buf_size,
  12359. "unknown struct field");
  12360. goto fail;
  12361. }
  12362. if (opcode1 == WASM_OP_STRUCT_SET
  12363. && !(struct_type->fields[field_idx].field_flags
  12364. & 1)) {
  12365. set_error_buf(error_buf, error_buf_size,
  12366. "field is immutable");
  12367. goto fail;
  12368. }
  12369. field_type = struct_type->fields[field_idx].field_type;
  12370. if (is_packed_type(field_type)) {
  12371. if (opcode1 == WASM_OP_STRUCT_GET) {
  12372. set_error_buf(error_buf, error_buf_size,
  12373. "type mismatch");
  12374. goto fail;
  12375. }
  12376. else {
  12377. field_type = VALUE_TYPE_I32;
  12378. }
  12379. }
  12380. if (wasm_is_type_multi_byte_type(field_type)) {
  12381. ref_type = wasm_reftype_map_find(
  12382. struct_type->ref_type_maps,
  12383. struct_type->ref_type_map_count, field_idx);
  12384. bh_assert(ref_type);
  12385. }
  12386. if (opcode1 == WASM_OP_STRUCT_SET) {
  12387. /* POP field */
  12388. if (wasm_is_type_multi_byte_type(field_type)) {
  12389. bh_memcpy_s(&wasm_ref_type,
  12390. (uint32)sizeof(WASMRefType),
  12391. ref_type,
  12392. wasm_reftype_struct_size(ref_type));
  12393. }
  12394. POP_REF(field_type);
  12395. /* POP struct obj, (ref null $t) */
  12396. wasm_set_refheaptype_typeidx(
  12397. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12398. POP_REF(wasm_ref_type.ref_type);
  12399. }
  12400. else {
  12401. /* POP struct obj, (ref null $t) */
  12402. wasm_set_refheaptype_typeidx(
  12403. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12404. POP_REF(wasm_ref_type.ref_type);
  12405. /* PUSH field */
  12406. if (wasm_is_type_multi_byte_type(field_type)) {
  12407. bh_memcpy_s(&wasm_ref_type,
  12408. (uint32)sizeof(WASMRefType),
  12409. ref_type,
  12410. wasm_reftype_struct_size(ref_type));
  12411. }
  12412. PUSH_REF(field_type);
  12413. }
  12414. break;
  12415. }
  12416. case WASM_OP_ARRAY_NEW:
  12417. case WASM_OP_ARRAY_NEW_DEFAULT:
  12418. case WASM_OP_ARRAY_NEW_FIXED:
  12419. case WASM_OP_ARRAY_NEW_DATA:
  12420. case WASM_OP_ARRAY_NEW_ELEM:
  12421. {
  12422. WASMArrayType *array_type;
  12423. uint8 elem_type;
  12424. uint32 u32 = 0;
  12425. read_leb_uint32(p, p_end, type_idx);
  12426. #if WASM_ENABLE_FAST_INTERP != 0
  12427. emit_uint32(loader_ctx, type_idx);
  12428. #endif
  12429. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12430. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12431. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12432. read_leb_uint32(p, p_end, u32);
  12433. #if WASM_ENABLE_FAST_INTERP != 0
  12434. emit_uint32(loader_ctx, u32);
  12435. #endif
  12436. }
  12437. if (!check_array_type(module, type_idx, error_buf,
  12438. error_buf_size)) {
  12439. goto fail;
  12440. }
  12441. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12442. /* length */
  12443. POP_I32();
  12444. }
  12445. array_type = (WASMArrayType *)module->types[type_idx];
  12446. elem_type = array_type->elem_type;
  12447. if (opcode1 == WASM_OP_ARRAY_NEW
  12448. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12449. if (wasm_is_type_multi_byte_type(elem_type)) {
  12450. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12451. array_type->elem_ref_type,
  12452. wasm_reftype_struct_size(
  12453. array_type->elem_ref_type));
  12454. }
  12455. if (is_packed_type(elem_type)) {
  12456. elem_type = VALUE_TYPE_I32;
  12457. }
  12458. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12459. uint32 N = u32;
  12460. for (i = 0; i < N; i++) {
  12461. if (wasm_is_type_multi_byte_type(
  12462. elem_type)) {
  12463. bh_memcpy_s(
  12464. &wasm_ref_type, sizeof(WASMRefType),
  12465. array_type->elem_ref_type,
  12466. wasm_reftype_struct_size(
  12467. array_type->elem_ref_type));
  12468. }
  12469. POP_REF(elem_type);
  12470. }
  12471. }
  12472. else
  12473. POP_REF(elem_type);
  12474. }
  12475. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12476. /* offset of data segment */
  12477. POP_I32();
  12478. if (u32 >= module->data_seg_count) {
  12479. set_error_buf(error_buf, error_buf_size,
  12480. "unknown data segment");
  12481. goto fail;
  12482. }
  12483. if (wasm_is_type_reftype(elem_type)) {
  12484. set_error_buf(error_buf, error_buf_size,
  12485. "array elem type mismatch");
  12486. goto fail;
  12487. }
  12488. }
  12489. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12490. WASMTableSeg *table_seg =
  12491. module->table_segments + u32;
  12492. /* offset of element segment */
  12493. POP_I32();
  12494. if (u32 >= module->table_seg_count) {
  12495. set_error_buf(error_buf, error_buf_size,
  12496. "unknown element segment");
  12497. goto fail;
  12498. }
  12499. if (!wasm_reftype_is_subtype_of(
  12500. table_seg->elem_type,
  12501. table_seg->elem_ref_type, elem_type,
  12502. array_type->elem_ref_type, module->types,
  12503. module->type_count)) {
  12504. set_error_buf(error_buf, error_buf_size,
  12505. "array elem type mismatch");
  12506. goto fail;
  12507. }
  12508. }
  12509. /* PUSH array obj, (ref $t) */
  12510. wasm_set_refheaptype_typeidx(
  12511. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12512. PUSH_REF(wasm_ref_type.ref_type);
  12513. break;
  12514. }
  12515. case WASM_OP_ARRAY_GET:
  12516. case WASM_OP_ARRAY_GET_S:
  12517. case WASM_OP_ARRAY_GET_U:
  12518. case WASM_OP_ARRAY_SET:
  12519. {
  12520. uint8 elem_type;
  12521. WASMArrayType *array_type;
  12522. WASMRefType *ref_type = NULL;
  12523. read_leb_uint32(p, p_end, type_idx);
  12524. #if WASM_ENABLE_FAST_INTERP != 0
  12525. emit_uint32(loader_ctx, type_idx);
  12526. #endif
  12527. if (!check_array_type(module, type_idx, error_buf,
  12528. error_buf_size)) {
  12529. goto fail;
  12530. }
  12531. array_type = (WASMArrayType *)module->types[type_idx];
  12532. if (opcode1 == WASM_OP_ARRAY_SET
  12533. && !(array_type->elem_flags & 1)) {
  12534. set_error_buf(error_buf, error_buf_size,
  12535. "array is immutable");
  12536. goto fail;
  12537. }
  12538. elem_type = array_type->elem_type;
  12539. if (is_packed_type(elem_type)) {
  12540. if (opcode1 != WASM_OP_ARRAY_GET_S
  12541. && opcode1 != WASM_OP_ARRAY_GET_U
  12542. && opcode1 != WASM_OP_ARRAY_SET) {
  12543. set_error_buf(error_buf, error_buf_size,
  12544. "type mismatch");
  12545. goto fail;
  12546. }
  12547. else {
  12548. elem_type = VALUE_TYPE_I32;
  12549. }
  12550. }
  12551. ref_type = array_type->elem_ref_type;
  12552. if (opcode1 == WASM_OP_ARRAY_SET) {
  12553. /* POP elem to set */
  12554. if (wasm_is_type_multi_byte_type(elem_type)) {
  12555. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12556. ref_type,
  12557. wasm_reftype_struct_size(ref_type));
  12558. }
  12559. POP_REF(elem_type);
  12560. }
  12561. /* elem idx */
  12562. POP_I32();
  12563. /* POP array obj, (ref null $t) */
  12564. wasm_set_refheaptype_typeidx(
  12565. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12566. POP_REF(wasm_ref_type.ref_type);
  12567. if (opcode1 != WASM_OP_ARRAY_SET) {
  12568. /* PUSH elem */
  12569. if (wasm_is_type_multi_byte_type(elem_type)) {
  12570. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12571. ref_type,
  12572. wasm_reftype_struct_size(ref_type));
  12573. }
  12574. PUSH_REF(elem_type);
  12575. }
  12576. break;
  12577. }
  12578. case WASM_OP_ARRAY_LEN:
  12579. {
  12580. POP_REF(REF_TYPE_ARRAYREF);
  12581. /* length */
  12582. PUSH_I32();
  12583. break;
  12584. }
  12585. case WASM_OP_ARRAY_FILL:
  12586. {
  12587. WASMArrayType *array_type;
  12588. uint8 elem_type;
  12589. /* typeidx */
  12590. read_leb_uint32(p, p_end, type_idx);
  12591. #if WASM_ENABLE_FAST_INTERP != 0
  12592. emit_uint32(loader_ctx, type_idx);
  12593. #endif
  12594. if (!check_array_type(module, type_idx, error_buf,
  12595. error_buf_size)) {
  12596. goto fail;
  12597. }
  12598. array_type = (WASMArrayType *)module->types[type_idx];
  12599. if (!(array_type->elem_flags & 1)) {
  12600. set_error_buf(error_buf, error_buf_size,
  12601. "array is immutable");
  12602. goto fail;
  12603. }
  12604. elem_type = array_type->elem_type;
  12605. if (is_packed_type(elem_type)) {
  12606. elem_type = VALUE_TYPE_I32;
  12607. }
  12608. POP_I32(); /* length */
  12609. #if WASM_ENABLE_FAST_INTERP != 0
  12610. POP_OFFSET_TYPE(elem_type);
  12611. #endif
  12612. POP_TYPE(elem_type);
  12613. POP_I32(); /* start */
  12614. /* POP array obj, (ref null $t) */
  12615. wasm_set_refheaptype_typeidx(
  12616. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12617. POP_REF(wasm_ref_type.ref_type);
  12618. break;
  12619. }
  12620. case WASM_OP_ARRAY_COPY:
  12621. {
  12622. uint32 src_type_idx;
  12623. uint8 src_elem_type, dst_elem_type;
  12624. WASMRefType src_ref_type = { 0 },
  12625. *src_elem_ref_type = NULL;
  12626. WASMRefType dst_ref_type = { 0 },
  12627. *dst_elem_ref_type = NULL;
  12628. WASMArrayType *array_type;
  12629. /* typeidx1 */
  12630. read_leb_uint32(p, p_end, type_idx);
  12631. #if WASM_ENABLE_FAST_INTERP != 0
  12632. emit_uint32(loader_ctx, type_idx);
  12633. #endif
  12634. /* typeidx2 */
  12635. read_leb_uint32(p, p_end, src_type_idx);
  12636. #if WASM_ENABLE_FAST_INTERP != 0
  12637. emit_uint32(loader_ctx, src_type_idx);
  12638. #endif
  12639. if (!check_array_type(module, type_idx, error_buf,
  12640. error_buf_size)) {
  12641. goto fail;
  12642. }
  12643. if (!check_array_type(module, src_type_idx, error_buf,
  12644. error_buf_size)) {
  12645. goto fail;
  12646. }
  12647. POP_I32();
  12648. POP_I32();
  12649. /* POP array obj, (ref null $t) */
  12650. wasm_set_refheaptype_typeidx(
  12651. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12652. POP_REF(wasm_ref_type.ref_type);
  12653. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12654. &wasm_ref_type,
  12655. wasm_reftype_struct_size(&wasm_ref_type));
  12656. POP_I32();
  12657. /* POP array obj, (ref null $t) */
  12658. wasm_set_refheaptype_typeidx(
  12659. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12660. POP_REF(wasm_ref_type.ref_type);
  12661. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12662. &wasm_ref_type,
  12663. wasm_reftype_struct_size(&wasm_ref_type));
  12664. array_type = (WASMArrayType *)module->types[type_idx];
  12665. if (!(array_type->elem_flags & 1)) {
  12666. set_error_buf(error_buf, error_buf_size,
  12667. "destination array is immutable");
  12668. goto fail;
  12669. }
  12670. dst_elem_type = array_type->elem_type;
  12671. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12672. dst_elem_ref_type = array_type->elem_ref_type;
  12673. }
  12674. array_type =
  12675. (WASMArrayType *)module->types[src_type_idx];
  12676. src_elem_type = array_type->elem_type;
  12677. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12678. src_elem_ref_type = array_type->elem_ref_type;
  12679. }
  12680. if (!wasm_reftype_is_subtype_of(
  12681. src_elem_type, src_elem_ref_type, dst_elem_type,
  12682. dst_elem_ref_type, module->types,
  12683. module->type_count)) {
  12684. set_error_buf(error_buf, error_buf_size,
  12685. "array types do not match");
  12686. goto fail;
  12687. }
  12688. break;
  12689. }
  12690. case WASM_OP_REF_I31:
  12691. {
  12692. POP_I32();
  12693. wasm_set_refheaptype_common(
  12694. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12695. PUSH_REF(wasm_ref_type.ref_type);
  12696. break;
  12697. }
  12698. case WASM_OP_I31_GET_S:
  12699. case WASM_OP_I31_GET_U:
  12700. {
  12701. POP_REF(REF_TYPE_I31REF);
  12702. PUSH_I32();
  12703. break;
  12704. }
  12705. case WASM_OP_REF_TEST:
  12706. case WASM_OP_REF_CAST:
  12707. case WASM_OP_REF_TEST_NULLABLE:
  12708. case WASM_OP_REF_CAST_NULLABLE:
  12709. {
  12710. uint8 type;
  12711. read_leb_int32(p, p_end, heap_type);
  12712. #if WASM_ENABLE_FAST_INTERP != 0
  12713. emit_uint32(loader_ctx, (uint32)heap_type);
  12714. #endif
  12715. if (heap_type >= 0) {
  12716. if (!check_type_index(module, module->type_count,
  12717. heap_type, error_buf,
  12718. error_buf_size)) {
  12719. goto fail;
  12720. }
  12721. }
  12722. else {
  12723. if (!wasm_is_valid_heap_type(heap_type)) {
  12724. set_error_buf(error_buf, error_buf_size,
  12725. "unknown type");
  12726. goto fail;
  12727. }
  12728. }
  12729. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12730. &wasm_ref_type, error_buf,
  12731. error_buf_size)) {
  12732. goto fail;
  12733. }
  12734. if (opcode1 == WASM_OP_REF_TEST
  12735. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12736. PUSH_I32();
  12737. else {
  12738. bool nullable =
  12739. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12740. : false;
  12741. if (heap_type >= 0 || !nullable) {
  12742. wasm_set_refheaptype_typeidx(
  12743. &wasm_ref_type.ref_ht_typeidx, nullable,
  12744. heap_type);
  12745. PUSH_REF(wasm_ref_type.ref_type);
  12746. }
  12747. else {
  12748. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12749. }
  12750. }
  12751. break;
  12752. }
  12753. case WASM_OP_BR_ON_CAST:
  12754. case WASM_OP_BR_ON_CAST_FAIL:
  12755. {
  12756. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12757. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12758. uint8 type_tmp, castflags;
  12759. uint32 depth;
  12760. int32 heap_type_dst;
  12761. bool src_nullable, dst_nullable;
  12762. CHECK_BUF(p, p_end, 1);
  12763. castflags = read_uint8(p);
  12764. #if WASM_ENABLE_FAST_INTERP != 0
  12765. /* Emit heap_type firstly */
  12766. emit_byte(loader_ctx, castflags);
  12767. #endif
  12768. p_org = p;
  12769. read_leb_uint32(p, p_end, depth);
  12770. read_leb_int32(p, p_end, heap_type);
  12771. #if WASM_ENABLE_FAST_INTERP != 0
  12772. /* Emit heap_type firstly */
  12773. emit_uint32(loader_ctx, (uint32)heap_type);
  12774. #endif
  12775. read_leb_int32(p, p_end, heap_type_dst);
  12776. #if WASM_ENABLE_FAST_INTERP != 0
  12777. /* Emit heap_type firstly */
  12778. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12779. #endif
  12780. (void)depth;
  12781. /*
  12782. * castflags should be 0~3:
  12783. * 0: (non-null, non-null)
  12784. * 1: (null, non-null)
  12785. * 2: (non-null, null)
  12786. * 3: (null, null)
  12787. */
  12788. if (castflags > 3) {
  12789. set_error_buf(error_buf, error_buf_size,
  12790. "invalid castflags");
  12791. break;
  12792. }
  12793. src_nullable =
  12794. (castflags == 1) || (castflags == 3) ? true : false;
  12795. dst_nullable =
  12796. (castflags == 2) || (castflags == 3) ? true : false;
  12797. /* Pop and backup the stack top's ref type */
  12798. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12799. &ref_type_tmp, error_buf,
  12800. error_buf_size)) {
  12801. goto fail;
  12802. }
  12803. /* The reference type rt1 must be valid */
  12804. if (!init_ref_type(module, &ref_type1, src_nullable,
  12805. heap_type, error_buf,
  12806. error_buf_size)) {
  12807. goto fail;
  12808. }
  12809. /* The reference type rt2 must be valid. */
  12810. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12811. heap_type_dst, error_buf,
  12812. error_buf_size)) {
  12813. goto fail;
  12814. }
  12815. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12816. &ref_type2);
  12817. /* The reference type rt2 must match rt1. */
  12818. if (!wasm_reftype_is_subtype_of(
  12819. ref_type2.ref_type, &ref_type2,
  12820. ref_type1.ref_type, &ref_type1, module->types,
  12821. module->type_count)) {
  12822. set_error_buf(error_buf, error_buf_size,
  12823. "type mismatch");
  12824. goto fail;
  12825. }
  12826. p = p_org;
  12827. /* Push ref type casted for branch block check */
  12828. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12829. /* The reference type rt2 must match rt′. */
  12830. type_tmp = ref_type2.ref_type;
  12831. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12832. bh_memcpy_s(
  12833. &wasm_ref_type,
  12834. wasm_reftype_struct_size(&ref_type2),
  12835. &ref_type2,
  12836. wasm_reftype_struct_size(&ref_type2));
  12837. }
  12838. }
  12839. else {
  12840. /* The reference type rt′1 must match rt′. */
  12841. type_tmp = ref_type_diff.ref_type;
  12842. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12843. bh_memcpy_s(
  12844. &wasm_ref_type,
  12845. wasm_reftype_struct_size(&ref_type_diff),
  12846. &ref_type_diff,
  12847. wasm_reftype_struct_size(&ref_type_diff));
  12848. }
  12849. }
  12850. PUSH_REF(type_tmp);
  12851. if (!(frame_csp_tmp = check_branch_block(
  12852. loader_ctx, &p, p_end, opcode, error_buf,
  12853. error_buf_size))) {
  12854. goto fail;
  12855. }
  12856. /* Ignore heap_types */
  12857. skip_leb_uint32(p, p_end);
  12858. skip_leb_uint32(p, p_end);
  12859. /* Restore the original stack top's ref type */
  12860. POP_REF(type_tmp);
  12861. #if WASM_ENABLE_FAST_INTERP != 0
  12862. /* Erase the opnd offset emitted by POP_REF() */
  12863. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12864. #endif
  12865. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12866. type_tmp = ref_type_diff.ref_type;
  12867. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12868. bh_memcpy_s(
  12869. &wasm_ref_type,
  12870. wasm_reftype_struct_size(&ref_type_diff),
  12871. &ref_type_diff,
  12872. wasm_reftype_struct_size(&ref_type_diff));
  12873. }
  12874. }
  12875. else {
  12876. type_tmp = ref_type2.ref_type;
  12877. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12878. bh_memcpy_s(
  12879. &wasm_ref_type,
  12880. wasm_reftype_struct_size(&ref_type2),
  12881. &ref_type2,
  12882. wasm_reftype_struct_size(&ref_type2));
  12883. }
  12884. }
  12885. PUSH_REF(type_tmp);
  12886. #if WASM_ENABLE_FAST_INTERP != 0
  12887. /* Erase the opnd offset emitted by PUSH_REF() */
  12888. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12889. #endif
  12890. break;
  12891. }
  12892. case WASM_OP_ANY_CONVERT_EXTERN:
  12893. {
  12894. uint8 type;
  12895. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12896. &wasm_ref_type, error_buf,
  12897. error_buf_size)) {
  12898. goto fail;
  12899. }
  12900. if (!(type == REF_TYPE_EXTERNREF
  12901. || (type == REF_TYPE_HT_NON_NULLABLE
  12902. && wasm_ref_type.ref_ht_common.heap_type
  12903. == HEAP_TYPE_EXTERN)
  12904. || type == VALUE_TYPE_ANY)) {
  12905. set_error_buf(error_buf, error_buf_size,
  12906. "type mismatch");
  12907. goto fail;
  12908. }
  12909. if (type == REF_TYPE_EXTERNREF)
  12910. type = REF_TYPE_ANYREF;
  12911. else {
  12912. wasm_ref_type.ref_ht_common.heap_type =
  12913. HEAP_TYPE_ANY;
  12914. }
  12915. PUSH_REF(type);
  12916. break;
  12917. }
  12918. case WASM_OP_EXTERN_CONVERT_ANY:
  12919. {
  12920. uint8 type;
  12921. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12922. &wasm_ref_type, error_buf,
  12923. error_buf_size)) {
  12924. goto fail;
  12925. }
  12926. if (type == REF_TYPE_EXTERNREF
  12927. || ((type == REF_TYPE_HT_NULLABLE
  12928. || type == REF_TYPE_HT_NON_NULLABLE)
  12929. && wasm_ref_type.ref_ht_common.heap_type
  12930. == HEAP_TYPE_EXTERN)) {
  12931. set_error_buf(error_buf, error_buf_size,
  12932. "type mismatch");
  12933. goto fail;
  12934. }
  12935. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12936. /* push (ref null extern) */
  12937. type = REF_TYPE_EXTERNREF;
  12938. }
  12939. else {
  12940. /* push (ref extern) */
  12941. type = REF_TYPE_HT_NON_NULLABLE;
  12942. wasm_set_refheaptype_common(
  12943. &wasm_ref_type.ref_ht_common, false,
  12944. HEAP_TYPE_EXTERN);
  12945. }
  12946. PUSH_REF(type);
  12947. break;
  12948. }
  12949. #if WASM_ENABLE_STRINGREF != 0
  12950. case WASM_OP_STRING_NEW_UTF8:
  12951. case WASM_OP_STRING_NEW_WTF16:
  12952. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12953. case WASM_OP_STRING_NEW_WTF8:
  12954. {
  12955. uint32 memidx;
  12956. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12957. func->has_memory_operations = true;
  12958. #endif
  12959. read_leb_uint32(p, p_end, memidx);
  12960. #if WASM_ENABLE_FAST_INTERP != 0
  12961. emit_uint32(loader_ctx, (uint32)memidx);
  12962. #endif
  12963. POP_I32();
  12964. POP_I32();
  12965. PUSH_REF(REF_TYPE_STRINGREF);
  12966. (void)memidx;
  12967. break;
  12968. }
  12969. case WASM_OP_STRING_CONST:
  12970. {
  12971. uint32 contents;
  12972. read_leb_uint32(p, p_end, contents);
  12973. #if WASM_ENABLE_FAST_INTERP != 0
  12974. emit_uint32(loader_ctx, (uint32)contents);
  12975. #endif
  12976. PUSH_REF(REF_TYPE_STRINGREF);
  12977. (void)contents;
  12978. break;
  12979. }
  12980. case WASM_OP_STRING_MEASURE_UTF8:
  12981. case WASM_OP_STRING_MEASURE_WTF8:
  12982. case WASM_OP_STRING_MEASURE_WTF16:
  12983. {
  12984. POP_STRINGREF();
  12985. PUSH_I32();
  12986. break;
  12987. }
  12988. case WASM_OP_STRING_ENCODE_UTF8:
  12989. case WASM_OP_STRING_ENCODE_WTF16:
  12990. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  12991. case WASM_OP_STRING_ENCODE_WTF8:
  12992. {
  12993. uint32 memidx;
  12994. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12995. func->has_memory_operations = true;
  12996. #endif
  12997. read_leb_uint32(p, p_end, memidx);
  12998. #if WASM_ENABLE_FAST_INTERP != 0
  12999. emit_uint32(loader_ctx, (uint32)memidx);
  13000. #endif
  13001. POP_I32();
  13002. POP_STRINGREF();
  13003. PUSH_I32();
  13004. (void)memidx;
  13005. break;
  13006. }
  13007. case WASM_OP_STRING_CONCAT:
  13008. {
  13009. POP_STRINGREF();
  13010. POP_STRINGREF();
  13011. PUSH_REF(REF_TYPE_STRINGREF);
  13012. break;
  13013. }
  13014. case WASM_OP_STRING_EQ:
  13015. {
  13016. POP_STRINGREF();
  13017. POP_STRINGREF();
  13018. PUSH_I32();
  13019. break;
  13020. }
  13021. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13022. {
  13023. POP_STRINGREF();
  13024. PUSH_I32();
  13025. break;
  13026. }
  13027. case WASM_OP_STRING_AS_WTF8:
  13028. {
  13029. POP_STRINGREF();
  13030. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13031. break;
  13032. }
  13033. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13034. {
  13035. POP_I32();
  13036. POP_I32();
  13037. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13038. PUSH_I32();
  13039. break;
  13040. }
  13041. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13042. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13043. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13044. {
  13045. uint32 memidx;
  13046. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13047. func->has_memory_operations = true;
  13048. #endif
  13049. read_leb_uint32(p, p_end, memidx);
  13050. #if WASM_ENABLE_FAST_INTERP != 0
  13051. emit_uint32(loader_ctx, (uint32)memidx);
  13052. #endif
  13053. POP_I32();
  13054. POP_I32();
  13055. POP_I32();
  13056. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13057. PUSH_I32();
  13058. PUSH_I32();
  13059. (void)memidx;
  13060. break;
  13061. }
  13062. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13063. {
  13064. POP_I32();
  13065. POP_I32();
  13066. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13067. PUSH_REF(REF_TYPE_STRINGREF);
  13068. break;
  13069. }
  13070. case WASM_OP_STRING_AS_WTF16:
  13071. {
  13072. POP_STRINGREF();
  13073. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13074. break;
  13075. }
  13076. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13077. {
  13078. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13079. PUSH_I32();
  13080. break;
  13081. }
  13082. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13083. {
  13084. POP_I32();
  13085. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13086. PUSH_I32();
  13087. break;
  13088. }
  13089. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13090. {
  13091. uint32 memidx;
  13092. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13093. func->has_memory_operations = true;
  13094. #endif
  13095. read_leb_uint32(p, p_end, memidx);
  13096. #if WASM_ENABLE_FAST_INTERP != 0
  13097. emit_uint32(loader_ctx, (uint32)memidx);
  13098. #endif
  13099. POP_I32();
  13100. POP_I32();
  13101. POP_I32();
  13102. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13103. PUSH_I32();
  13104. (void)memidx;
  13105. break;
  13106. }
  13107. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13108. {
  13109. POP_I32();
  13110. POP_I32();
  13111. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13112. PUSH_REF(REF_TYPE_STRINGREF);
  13113. break;
  13114. }
  13115. case WASM_OP_STRING_AS_ITER:
  13116. {
  13117. POP_STRINGREF();
  13118. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13119. break;
  13120. }
  13121. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13122. {
  13123. POP_REF(REF_TYPE_STRINGVIEWITER);
  13124. PUSH_I32();
  13125. break;
  13126. }
  13127. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13128. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13129. {
  13130. POP_I32();
  13131. POP_REF(REF_TYPE_STRINGVIEWITER);
  13132. PUSH_I32();
  13133. break;
  13134. }
  13135. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13136. {
  13137. POP_I32();
  13138. POP_REF(REF_TYPE_STRINGVIEWITER);
  13139. PUSH_REF(REF_TYPE_STRINGREF);
  13140. break;
  13141. }
  13142. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13143. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13144. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13145. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13146. {
  13147. POP_I32();
  13148. POP_I32();
  13149. POP_REF(REF_TYPE_ARRAYREF);
  13150. PUSH_REF(REF_TYPE_STRINGREF);
  13151. break;
  13152. }
  13153. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13154. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13155. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13156. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13157. {
  13158. POP_I32();
  13159. POP_REF(REF_TYPE_ARRAYREF);
  13160. POP_STRINGREF();
  13161. PUSH_I32();
  13162. break;
  13163. }
  13164. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13165. default:
  13166. set_error_buf_v(error_buf, error_buf_size,
  13167. "%s %02x %02x", "unsupported opcode",
  13168. 0xfb, opcode1);
  13169. goto fail;
  13170. }
  13171. break;
  13172. }
  13173. #endif /* end of WASM_ENABLE_GC != 0 */
  13174. case WASM_OP_MISC_PREFIX:
  13175. {
  13176. uint32 opcode1;
  13177. read_leb_uint32(p, p_end, opcode1);
  13178. #if WASM_ENABLE_FAST_INTERP != 0
  13179. emit_byte(loader_ctx, ((uint8)opcode1));
  13180. #endif
  13181. switch (opcode1) {
  13182. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13183. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13184. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13185. break;
  13186. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13187. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13188. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13189. break;
  13190. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13191. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13192. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13193. break;
  13194. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13195. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13196. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13197. break;
  13198. #if WASM_ENABLE_BULK_MEMORY != 0
  13199. case WASM_OP_MEMORY_INIT:
  13200. {
  13201. read_leb_uint32(p, p_end, data_seg_idx);
  13202. #if WASM_ENABLE_FAST_INTERP != 0
  13203. emit_uint32(loader_ctx, data_seg_idx);
  13204. #endif
  13205. if (module->import_memory_count == 0
  13206. && module->memory_count == 0)
  13207. goto fail_unknown_memory;
  13208. read_leb_uint32(p, p_end, memidx);
  13209. check_memidx(module, memidx);
  13210. if (data_seg_idx >= module->data_seg_count) {
  13211. set_error_buf_v(error_buf, error_buf_size,
  13212. "unknown data segment %d",
  13213. data_seg_idx);
  13214. goto fail;
  13215. }
  13216. if (module->data_seg_count1 == 0)
  13217. goto fail_data_cnt_sec_require;
  13218. POP_I32();
  13219. POP_I32();
  13220. POP_MEM_OFFSET();
  13221. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13222. func->has_memory_operations = true;
  13223. #endif
  13224. #if WASM_ENABLE_WAMR_COMPILER != 0
  13225. module->is_bulk_memory_used = true;
  13226. #endif
  13227. break;
  13228. }
  13229. case WASM_OP_DATA_DROP:
  13230. {
  13231. read_leb_uint32(p, p_end, data_seg_idx);
  13232. #if WASM_ENABLE_FAST_INTERP != 0
  13233. emit_uint32(loader_ctx, data_seg_idx);
  13234. #endif
  13235. if (data_seg_idx >= module->data_seg_count) {
  13236. set_error_buf(error_buf, error_buf_size,
  13237. "unknown data segment");
  13238. goto fail;
  13239. }
  13240. if (module->data_seg_count1 == 0)
  13241. goto fail_data_cnt_sec_require;
  13242. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13243. func->has_memory_operations = true;
  13244. #endif
  13245. #if WASM_ENABLE_WAMR_COMPILER != 0
  13246. module->is_bulk_memory_used = true;
  13247. #endif
  13248. break;
  13249. }
  13250. case WASM_OP_MEMORY_COPY:
  13251. {
  13252. CHECK_BUF(p, p_end, sizeof(int16));
  13253. /* check both src and dst memory index */
  13254. read_leb_uint32(p, p_end, memidx);
  13255. check_memidx(module, memidx);
  13256. read_leb_uint32(p, p_end, memidx);
  13257. check_memidx(module, memidx);
  13258. if (module->import_memory_count == 0
  13259. && module->memory_count == 0)
  13260. goto fail_unknown_memory;
  13261. POP_MEM_OFFSET();
  13262. POP_MEM_OFFSET();
  13263. POP_MEM_OFFSET();
  13264. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13265. func->has_memory_operations = true;
  13266. #endif
  13267. #if WASM_ENABLE_WAMR_COMPILER != 0
  13268. module->is_bulk_memory_used = true;
  13269. #endif
  13270. break;
  13271. }
  13272. case WASM_OP_MEMORY_FILL:
  13273. {
  13274. read_leb_uint32(p, p_end, memidx);
  13275. check_memidx(module, memidx);
  13276. if (module->import_memory_count == 0
  13277. && module->memory_count == 0) {
  13278. goto fail_unknown_memory;
  13279. }
  13280. POP_MEM_OFFSET();
  13281. POP_I32();
  13282. POP_MEM_OFFSET();
  13283. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13284. func->has_memory_operations = true;
  13285. #endif
  13286. #if WASM_ENABLE_WAMR_COMPILER != 0
  13287. module->is_bulk_memory_used = true;
  13288. #endif
  13289. break;
  13290. }
  13291. fail_unknown_memory:
  13292. set_error_buf(error_buf, error_buf_size,
  13293. "unknown memory 0");
  13294. goto fail;
  13295. fail_data_cnt_sec_require:
  13296. set_error_buf(error_buf, error_buf_size,
  13297. "data count section required");
  13298. goto fail;
  13299. #endif /* WASM_ENABLE_BULK_MEMORY */
  13300. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13301. case WASM_OP_TABLE_INIT:
  13302. {
  13303. uint8 seg_type = 0, tbl_type = 0;
  13304. #if WASM_ENABLE_GC != 0
  13305. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13306. #endif
  13307. read_leb_uint32(p, p_end, table_seg_idx);
  13308. read_leb_uint32(p, p_end, table_idx);
  13309. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13310. #if WASM_ENABLE_GC != 0
  13311. (void **)&tbl_ref_type,
  13312. #else
  13313. NULL,
  13314. #endif
  13315. error_buf, error_buf_size))
  13316. goto fail;
  13317. if (!get_table_seg_elem_type(module, table_seg_idx,
  13318. &seg_type,
  13319. #if WASM_ENABLE_GC != 0
  13320. (void **)&seg_ref_type,
  13321. #else
  13322. NULL,
  13323. #endif
  13324. error_buf, error_buf_size))
  13325. goto fail;
  13326. #if WASM_ENABLE_GC == 0
  13327. if (seg_type != tbl_type) {
  13328. set_error_buf(error_buf, error_buf_size,
  13329. "type mismatch");
  13330. goto fail;
  13331. }
  13332. #else
  13333. if (!wasm_reftype_is_subtype_of(
  13334. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13335. module->types, module->type_count)) {
  13336. set_error_buf(error_buf, error_buf_size,
  13337. "type mismatch");
  13338. goto fail;
  13339. }
  13340. #endif
  13341. #if WASM_ENABLE_FAST_INTERP != 0
  13342. emit_uint32(loader_ctx, table_seg_idx);
  13343. emit_uint32(loader_ctx, table_idx);
  13344. #endif
  13345. POP_I32();
  13346. POP_I32();
  13347. POP_I32();
  13348. #if WASM_ENABLE_WAMR_COMPILER != 0
  13349. module->is_ref_types_used = true;
  13350. #endif
  13351. break;
  13352. }
  13353. case WASM_OP_ELEM_DROP:
  13354. {
  13355. read_leb_uint32(p, p_end, table_seg_idx);
  13356. if (!get_table_seg_elem_type(module, table_seg_idx,
  13357. NULL, NULL, error_buf,
  13358. error_buf_size))
  13359. goto fail;
  13360. #if WASM_ENABLE_FAST_INTERP != 0
  13361. emit_uint32(loader_ctx, table_seg_idx);
  13362. #endif
  13363. #if WASM_ENABLE_WAMR_COMPILER != 0
  13364. module->is_ref_types_used = true;
  13365. #endif
  13366. break;
  13367. }
  13368. case WASM_OP_TABLE_COPY:
  13369. {
  13370. uint8 src_type, dst_type;
  13371. #if WASM_ENABLE_GC != 0
  13372. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13373. #endif
  13374. uint32 src_tbl_idx, dst_tbl_idx;
  13375. read_leb_uint32(p, p_end, dst_tbl_idx);
  13376. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13377. #if WASM_ENABLE_GC != 0
  13378. (void **)&dst_ref_type,
  13379. #else
  13380. NULL,
  13381. #endif
  13382. error_buf, error_buf_size))
  13383. goto fail;
  13384. read_leb_uint32(p, p_end, src_tbl_idx);
  13385. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13386. #if WASM_ENABLE_GC != 0
  13387. (void **)&src_ref_type,
  13388. #else
  13389. NULL,
  13390. #endif
  13391. error_buf, error_buf_size))
  13392. goto fail;
  13393. #if WASM_ENABLE_GC == 0
  13394. if (src_type != dst_type) {
  13395. set_error_buf(error_buf, error_buf_size,
  13396. "type mismatch");
  13397. goto fail;
  13398. }
  13399. #else
  13400. if (!wasm_reftype_is_subtype_of(
  13401. src_type, src_ref_type, dst_type, dst_ref_type,
  13402. module->types, module->type_count)) {
  13403. set_error_buf(error_buf, error_buf_size,
  13404. "type mismatch");
  13405. goto fail;
  13406. }
  13407. #endif
  13408. #if WASM_ENABLE_FAST_INTERP != 0
  13409. emit_uint32(loader_ctx, dst_tbl_idx);
  13410. emit_uint32(loader_ctx, src_tbl_idx);
  13411. #endif
  13412. POP_I32();
  13413. POP_I32();
  13414. POP_I32();
  13415. #if WASM_ENABLE_WAMR_COMPILER != 0
  13416. module->is_ref_types_used = true;
  13417. #endif
  13418. break;
  13419. }
  13420. case WASM_OP_TABLE_SIZE:
  13421. {
  13422. read_leb_uint32(p, p_end, table_idx);
  13423. /* TODO: shall we create a new function to check
  13424. table idx instead of using below function? */
  13425. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13426. error_buf, error_buf_size))
  13427. goto fail;
  13428. #if WASM_ENABLE_FAST_INTERP != 0
  13429. emit_uint32(loader_ctx, table_idx);
  13430. #endif
  13431. PUSH_I32();
  13432. #if WASM_ENABLE_WAMR_COMPILER != 0
  13433. module->is_ref_types_used = true;
  13434. #endif
  13435. break;
  13436. }
  13437. case WASM_OP_TABLE_GROW:
  13438. case WASM_OP_TABLE_FILL:
  13439. {
  13440. uint8 decl_type;
  13441. #if WASM_ENABLE_GC != 0
  13442. WASMRefType *ref_type = NULL;
  13443. #endif
  13444. read_leb_uint32(p, p_end, table_idx);
  13445. if (!get_table_elem_type(module, table_idx, &decl_type,
  13446. #if WASM_ENABLE_GC != 0
  13447. (void **)&ref_type,
  13448. #else
  13449. NULL,
  13450. #endif
  13451. error_buf, error_buf_size))
  13452. goto fail;
  13453. #if WASM_ENABLE_GC != 0
  13454. if (wasm_is_type_multi_byte_type(decl_type)) {
  13455. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13456. ref_type,
  13457. wasm_reftype_struct_size(ref_type));
  13458. }
  13459. #endif
  13460. if (opcode1 == WASM_OP_TABLE_GROW) {
  13461. if (table_idx < module->import_table_count) {
  13462. module->import_tables[table_idx]
  13463. .u.table.table_type.possible_grow = true;
  13464. }
  13465. else {
  13466. module
  13467. ->tables[table_idx
  13468. - module->import_table_count]
  13469. .table_type.possible_grow = true;
  13470. }
  13471. }
  13472. #if WASM_ENABLE_FAST_INTERP != 0
  13473. emit_uint32(loader_ctx, table_idx);
  13474. #endif
  13475. POP_I32();
  13476. #if WASM_ENABLE_FAST_INTERP != 0
  13477. POP_OFFSET_TYPE(decl_type);
  13478. #endif
  13479. POP_TYPE(decl_type);
  13480. if (opcode1 == WASM_OP_TABLE_GROW)
  13481. PUSH_I32();
  13482. else
  13483. POP_I32();
  13484. #if WASM_ENABLE_WAMR_COMPILER != 0
  13485. module->is_ref_types_used = true;
  13486. #endif
  13487. break;
  13488. }
  13489. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13490. default:
  13491. set_error_buf_v(error_buf, error_buf_size,
  13492. "%s %02x %02x", "unsupported opcode",
  13493. 0xfc, opcode1);
  13494. goto fail;
  13495. }
  13496. break;
  13497. }
  13498. #if WASM_ENABLE_SIMD != 0
  13499. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13500. case WASM_OP_SIMD_PREFIX:
  13501. {
  13502. uint32 opcode1;
  13503. #if WASM_ENABLE_WAMR_COMPILER != 0
  13504. /* Mark the SIMD instruction is used in this module */
  13505. module->is_simd_used = true;
  13506. #endif
  13507. read_leb_uint32(p, p_end, opcode1);
  13508. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13509. */
  13510. switch (opcode1) {
  13511. /* memory instruction */
  13512. case SIMD_v128_load:
  13513. case SIMD_v128_load8x8_s:
  13514. case SIMD_v128_load8x8_u:
  13515. case SIMD_v128_load16x4_s:
  13516. case SIMD_v128_load16x4_u:
  13517. case SIMD_v128_load32x2_s:
  13518. case SIMD_v128_load32x2_u:
  13519. case SIMD_v128_load8_splat:
  13520. case SIMD_v128_load16_splat:
  13521. case SIMD_v128_load32_splat:
  13522. case SIMD_v128_load64_splat:
  13523. {
  13524. CHECK_MEMORY();
  13525. read_leb_uint32(p, p_end, align); /* align */
  13526. if (!check_simd_memory_access_align(
  13527. opcode1, align, error_buf, error_buf_size)) {
  13528. goto fail;
  13529. }
  13530. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13531. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13532. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13533. func->has_memory_operations = true;
  13534. #endif
  13535. break;
  13536. }
  13537. case SIMD_v128_store:
  13538. {
  13539. CHECK_MEMORY();
  13540. read_leb_uint32(p, p_end, align); /* align */
  13541. if (!check_simd_memory_access_align(
  13542. opcode1, align, error_buf, error_buf_size)) {
  13543. goto fail;
  13544. }
  13545. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13546. POP_V128();
  13547. POP_MEM_OFFSET();
  13548. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13549. func->has_memory_operations = true;
  13550. #endif
  13551. break;
  13552. }
  13553. /* basic operation */
  13554. case SIMD_v128_const:
  13555. {
  13556. CHECK_BUF1(p, p_end, 16);
  13557. p += 16;
  13558. PUSH_V128();
  13559. break;
  13560. }
  13561. case SIMD_v8x16_shuffle:
  13562. {
  13563. V128 mask;
  13564. CHECK_BUF1(p, p_end, 16);
  13565. mask = read_i8x16(p, error_buf, error_buf_size);
  13566. p += 16;
  13567. if (!check_simd_shuffle_mask(mask, error_buf,
  13568. error_buf_size)) {
  13569. goto fail;
  13570. }
  13571. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13572. break;
  13573. }
  13574. case SIMD_v8x16_swizzle:
  13575. {
  13576. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13577. break;
  13578. }
  13579. /* splat operation */
  13580. case SIMD_i8x16_splat:
  13581. case SIMD_i16x8_splat:
  13582. case SIMD_i32x4_splat:
  13583. case SIMD_i64x2_splat:
  13584. case SIMD_f32x4_splat:
  13585. case SIMD_f64x2_splat:
  13586. {
  13587. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13588. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13589. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13590. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13591. VALUE_TYPE_V128);
  13592. break;
  13593. }
  13594. /* lane operation */
  13595. case SIMD_i8x16_extract_lane_s:
  13596. case SIMD_i8x16_extract_lane_u:
  13597. case SIMD_i8x16_replace_lane:
  13598. case SIMD_i16x8_extract_lane_s:
  13599. case SIMD_i16x8_extract_lane_u:
  13600. case SIMD_i16x8_replace_lane:
  13601. case SIMD_i32x4_extract_lane:
  13602. case SIMD_i32x4_replace_lane:
  13603. case SIMD_i64x2_extract_lane:
  13604. case SIMD_i64x2_replace_lane:
  13605. case SIMD_f32x4_extract_lane:
  13606. case SIMD_f32x4_replace_lane:
  13607. case SIMD_f64x2_extract_lane:
  13608. case SIMD_f64x2_replace_lane:
  13609. {
  13610. uint8 lane;
  13611. /* clang-format off */
  13612. uint8 replace[] = {
  13613. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13614. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13615. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13616. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13617. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13618. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13619. };
  13620. uint8 push_type[] = {
  13621. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13622. VALUE_TYPE_V128,
  13623. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13624. VALUE_TYPE_V128,
  13625. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13626. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13627. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13628. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13629. };
  13630. /* clang-format on */
  13631. CHECK_BUF(p, p_end, 1);
  13632. lane = read_uint8(p);
  13633. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13634. error_buf_size)) {
  13635. goto fail;
  13636. }
  13637. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13638. if (!(wasm_loader_pop_frame_ref(
  13639. loader_ctx,
  13640. replace[opcode1
  13641. - SIMD_i8x16_extract_lane_s],
  13642. error_buf, error_buf_size)))
  13643. goto fail;
  13644. }
  13645. POP_AND_PUSH(
  13646. VALUE_TYPE_V128,
  13647. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13648. break;
  13649. }
  13650. /* i8x16 compare operation */
  13651. case SIMD_i8x16_eq:
  13652. case SIMD_i8x16_ne:
  13653. case SIMD_i8x16_lt_s:
  13654. case SIMD_i8x16_lt_u:
  13655. case SIMD_i8x16_gt_s:
  13656. case SIMD_i8x16_gt_u:
  13657. case SIMD_i8x16_le_s:
  13658. case SIMD_i8x16_le_u:
  13659. case SIMD_i8x16_ge_s:
  13660. case SIMD_i8x16_ge_u:
  13661. /* i16x8 compare operation */
  13662. case SIMD_i16x8_eq:
  13663. case SIMD_i16x8_ne:
  13664. case SIMD_i16x8_lt_s:
  13665. case SIMD_i16x8_lt_u:
  13666. case SIMD_i16x8_gt_s:
  13667. case SIMD_i16x8_gt_u:
  13668. case SIMD_i16x8_le_s:
  13669. case SIMD_i16x8_le_u:
  13670. case SIMD_i16x8_ge_s:
  13671. case SIMD_i16x8_ge_u:
  13672. /* i32x4 compare operation */
  13673. case SIMD_i32x4_eq:
  13674. case SIMD_i32x4_ne:
  13675. case SIMD_i32x4_lt_s:
  13676. case SIMD_i32x4_lt_u:
  13677. case SIMD_i32x4_gt_s:
  13678. case SIMD_i32x4_gt_u:
  13679. case SIMD_i32x4_le_s:
  13680. case SIMD_i32x4_le_u:
  13681. case SIMD_i32x4_ge_s:
  13682. case SIMD_i32x4_ge_u:
  13683. /* f32x4 compare operation */
  13684. case SIMD_f32x4_eq:
  13685. case SIMD_f32x4_ne:
  13686. case SIMD_f32x4_lt:
  13687. case SIMD_f32x4_gt:
  13688. case SIMD_f32x4_le:
  13689. case SIMD_f32x4_ge:
  13690. /* f64x2 compare operation */
  13691. case SIMD_f64x2_eq:
  13692. case SIMD_f64x2_ne:
  13693. case SIMD_f64x2_lt:
  13694. case SIMD_f64x2_gt:
  13695. case SIMD_f64x2_le:
  13696. case SIMD_f64x2_ge:
  13697. {
  13698. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13699. break;
  13700. }
  13701. /* v128 operation */
  13702. case SIMD_v128_not:
  13703. {
  13704. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13705. break;
  13706. }
  13707. case SIMD_v128_and:
  13708. case SIMD_v128_andnot:
  13709. case SIMD_v128_or:
  13710. case SIMD_v128_xor:
  13711. {
  13712. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13713. break;
  13714. }
  13715. case SIMD_v128_bitselect:
  13716. {
  13717. POP_V128();
  13718. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13719. break;
  13720. }
  13721. case SIMD_v128_any_true:
  13722. {
  13723. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13724. break;
  13725. }
  13726. /* Load Lane Operation */
  13727. case SIMD_v128_load8_lane:
  13728. case SIMD_v128_load16_lane:
  13729. case SIMD_v128_load32_lane:
  13730. case SIMD_v128_load64_lane:
  13731. case SIMD_v128_store8_lane:
  13732. case SIMD_v128_store16_lane:
  13733. case SIMD_v128_store32_lane:
  13734. case SIMD_v128_store64_lane:
  13735. {
  13736. uint8 lane;
  13737. CHECK_MEMORY();
  13738. read_leb_uint32(p, p_end, align); /* align */
  13739. if (!check_simd_memory_access_align(
  13740. opcode1, align, error_buf, error_buf_size)) {
  13741. goto fail;
  13742. }
  13743. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13744. CHECK_BUF(p, p_end, 1);
  13745. lane = read_uint8(p);
  13746. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13747. error_buf_size)) {
  13748. goto fail;
  13749. }
  13750. POP_V128();
  13751. POP_MEM_OFFSET();
  13752. if (opcode1 < SIMD_v128_store8_lane) {
  13753. PUSH_V128();
  13754. }
  13755. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13756. func->has_memory_operations = true;
  13757. #endif
  13758. break;
  13759. }
  13760. case SIMD_v128_load32_zero:
  13761. case SIMD_v128_load64_zero:
  13762. {
  13763. CHECK_MEMORY();
  13764. read_leb_uint32(p, p_end, align); /* align */
  13765. if (!check_simd_memory_access_align(
  13766. opcode1, align, error_buf, error_buf_size)) {
  13767. goto fail;
  13768. }
  13769. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13770. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13771. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13772. func->has_memory_operations = true;
  13773. #endif
  13774. break;
  13775. }
  13776. /* Float conversion */
  13777. case SIMD_f32x4_demote_f64x2_zero:
  13778. case SIMD_f64x2_promote_low_f32x4_zero:
  13779. {
  13780. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13781. break;
  13782. }
  13783. /* i8x16 Operation */
  13784. case SIMD_i8x16_abs:
  13785. case SIMD_i8x16_neg:
  13786. case SIMD_i8x16_popcnt:
  13787. {
  13788. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13789. break;
  13790. }
  13791. case SIMD_i8x16_all_true:
  13792. case SIMD_i8x16_bitmask:
  13793. {
  13794. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13795. break;
  13796. }
  13797. case SIMD_i8x16_narrow_i16x8_s:
  13798. case SIMD_i8x16_narrow_i16x8_u:
  13799. {
  13800. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13801. break;
  13802. }
  13803. case SIMD_f32x4_ceil:
  13804. case SIMD_f32x4_floor:
  13805. case SIMD_f32x4_trunc:
  13806. case SIMD_f32x4_nearest:
  13807. {
  13808. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13809. break;
  13810. }
  13811. case SIMD_i8x16_shl:
  13812. case SIMD_i8x16_shr_s:
  13813. case SIMD_i8x16_shr_u:
  13814. {
  13815. POP_I32();
  13816. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13817. break;
  13818. }
  13819. case SIMD_i8x16_add:
  13820. case SIMD_i8x16_add_sat_s:
  13821. case SIMD_i8x16_add_sat_u:
  13822. case SIMD_i8x16_sub:
  13823. case SIMD_i8x16_sub_sat_s:
  13824. case SIMD_i8x16_sub_sat_u:
  13825. {
  13826. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13827. break;
  13828. }
  13829. case SIMD_f64x2_ceil:
  13830. case SIMD_f64x2_floor:
  13831. {
  13832. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13833. break;
  13834. }
  13835. case SIMD_i8x16_min_s:
  13836. case SIMD_i8x16_min_u:
  13837. case SIMD_i8x16_max_s:
  13838. case SIMD_i8x16_max_u:
  13839. {
  13840. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13841. break;
  13842. }
  13843. case SIMD_f64x2_trunc:
  13844. {
  13845. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13846. break;
  13847. }
  13848. case SIMD_i8x16_avgr_u:
  13849. {
  13850. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13851. break;
  13852. }
  13853. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13854. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13855. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13856. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13857. /* i16x8 operation */
  13858. case SIMD_i16x8_abs:
  13859. case SIMD_i16x8_neg:
  13860. {
  13861. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13862. break;
  13863. }
  13864. case SIMD_i16x8_q15mulr_sat_s:
  13865. {
  13866. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13867. break;
  13868. }
  13869. case SIMD_i16x8_all_true:
  13870. case SIMD_i16x8_bitmask:
  13871. {
  13872. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13873. break;
  13874. }
  13875. case SIMD_i16x8_narrow_i32x4_s:
  13876. case SIMD_i16x8_narrow_i32x4_u:
  13877. {
  13878. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13879. break;
  13880. }
  13881. case SIMD_i16x8_extend_low_i8x16_s:
  13882. case SIMD_i16x8_extend_high_i8x16_s:
  13883. case SIMD_i16x8_extend_low_i8x16_u:
  13884. case SIMD_i16x8_extend_high_i8x16_u:
  13885. {
  13886. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13887. break;
  13888. }
  13889. case SIMD_i16x8_shl:
  13890. case SIMD_i16x8_shr_s:
  13891. case SIMD_i16x8_shr_u:
  13892. {
  13893. POP_I32();
  13894. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13895. break;
  13896. }
  13897. case SIMD_i16x8_add:
  13898. case SIMD_i16x8_add_sat_s:
  13899. case SIMD_i16x8_add_sat_u:
  13900. case SIMD_i16x8_sub:
  13901. case SIMD_i16x8_sub_sat_s:
  13902. case SIMD_i16x8_sub_sat_u:
  13903. {
  13904. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13905. break;
  13906. }
  13907. case SIMD_f64x2_nearest:
  13908. {
  13909. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13910. break;
  13911. }
  13912. case SIMD_i16x8_mul:
  13913. case SIMD_i16x8_min_s:
  13914. case SIMD_i16x8_min_u:
  13915. case SIMD_i16x8_max_s:
  13916. case SIMD_i16x8_max_u:
  13917. case SIMD_i16x8_avgr_u:
  13918. case SIMD_i16x8_extmul_low_i8x16_s:
  13919. case SIMD_i16x8_extmul_high_i8x16_s:
  13920. case SIMD_i16x8_extmul_low_i8x16_u:
  13921. case SIMD_i16x8_extmul_high_i8x16_u:
  13922. {
  13923. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13924. break;
  13925. }
  13926. /* i32x4 operation */
  13927. case SIMD_i32x4_abs:
  13928. case SIMD_i32x4_neg:
  13929. {
  13930. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13931. break;
  13932. }
  13933. case SIMD_i32x4_all_true:
  13934. case SIMD_i32x4_bitmask:
  13935. {
  13936. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13937. break;
  13938. }
  13939. case SIMD_i32x4_extend_low_i16x8_s:
  13940. case SIMD_i32x4_extend_high_i16x8_s:
  13941. case SIMD_i32x4_extend_low_i16x8_u:
  13942. case SIMD_i32x4_extend_high_i16x8_u:
  13943. {
  13944. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13945. break;
  13946. }
  13947. case SIMD_i32x4_shl:
  13948. case SIMD_i32x4_shr_s:
  13949. case SIMD_i32x4_shr_u:
  13950. {
  13951. POP_I32();
  13952. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13953. break;
  13954. }
  13955. case SIMD_i32x4_add:
  13956. case SIMD_i32x4_sub:
  13957. case SIMD_i32x4_mul:
  13958. case SIMD_i32x4_min_s:
  13959. case SIMD_i32x4_min_u:
  13960. case SIMD_i32x4_max_s:
  13961. case SIMD_i32x4_max_u:
  13962. case SIMD_i32x4_dot_i16x8_s:
  13963. case SIMD_i32x4_extmul_low_i16x8_s:
  13964. case SIMD_i32x4_extmul_high_i16x8_s:
  13965. case SIMD_i32x4_extmul_low_i16x8_u:
  13966. case SIMD_i32x4_extmul_high_i16x8_u:
  13967. {
  13968. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13969. break;
  13970. }
  13971. /* i64x2 operation */
  13972. case SIMD_i64x2_abs:
  13973. case SIMD_i64x2_neg:
  13974. {
  13975. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13976. break;
  13977. }
  13978. case SIMD_i64x2_all_true:
  13979. case SIMD_i64x2_bitmask:
  13980. {
  13981. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13982. break;
  13983. }
  13984. case SIMD_i64x2_extend_low_i32x4_s:
  13985. case SIMD_i64x2_extend_high_i32x4_s:
  13986. case SIMD_i64x2_extend_low_i32x4_u:
  13987. case SIMD_i64x2_extend_high_i32x4_u:
  13988. {
  13989. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13990. break;
  13991. }
  13992. case SIMD_i64x2_shl:
  13993. case SIMD_i64x2_shr_s:
  13994. case SIMD_i64x2_shr_u:
  13995. {
  13996. POP_I32();
  13997. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13998. break;
  13999. }
  14000. case SIMD_i64x2_add:
  14001. case SIMD_i64x2_sub:
  14002. case SIMD_i64x2_mul:
  14003. case SIMD_i64x2_eq:
  14004. case SIMD_i64x2_ne:
  14005. case SIMD_i64x2_lt_s:
  14006. case SIMD_i64x2_gt_s:
  14007. case SIMD_i64x2_le_s:
  14008. case SIMD_i64x2_ge_s:
  14009. case SIMD_i64x2_extmul_low_i32x4_s:
  14010. case SIMD_i64x2_extmul_high_i32x4_s:
  14011. case SIMD_i64x2_extmul_low_i32x4_u:
  14012. case SIMD_i64x2_extmul_high_i32x4_u:
  14013. {
  14014. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14015. break;
  14016. }
  14017. /* f32x4 operation */
  14018. case SIMD_f32x4_abs:
  14019. case SIMD_f32x4_neg:
  14020. case SIMD_f32x4_sqrt:
  14021. {
  14022. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14023. break;
  14024. }
  14025. case SIMD_f32x4_add:
  14026. case SIMD_f32x4_sub:
  14027. case SIMD_f32x4_mul:
  14028. case SIMD_f32x4_div:
  14029. case SIMD_f32x4_min:
  14030. case SIMD_f32x4_max:
  14031. case SIMD_f32x4_pmin:
  14032. case SIMD_f32x4_pmax:
  14033. {
  14034. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14035. break;
  14036. }
  14037. /* f64x2 operation */
  14038. case SIMD_f64x2_abs:
  14039. case SIMD_f64x2_neg:
  14040. case SIMD_f64x2_sqrt:
  14041. {
  14042. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14043. break;
  14044. }
  14045. case SIMD_f64x2_add:
  14046. case SIMD_f64x2_sub:
  14047. case SIMD_f64x2_mul:
  14048. case SIMD_f64x2_div:
  14049. case SIMD_f64x2_min:
  14050. case SIMD_f64x2_max:
  14051. case SIMD_f64x2_pmin:
  14052. case SIMD_f64x2_pmax:
  14053. {
  14054. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14055. break;
  14056. }
  14057. case SIMD_i32x4_trunc_sat_f32x4_s:
  14058. case SIMD_i32x4_trunc_sat_f32x4_u:
  14059. case SIMD_f32x4_convert_i32x4_s:
  14060. case SIMD_f32x4_convert_i32x4_u:
  14061. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14062. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14063. case SIMD_f64x2_convert_low_i32x4_s:
  14064. case SIMD_f64x2_convert_low_i32x4_u:
  14065. {
  14066. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14067. break;
  14068. }
  14069. default:
  14070. {
  14071. if (error_buf != NULL) {
  14072. snprintf(error_buf, error_buf_size,
  14073. "WASM module load failed: "
  14074. "invalid opcode 0xfd %02x.",
  14075. opcode1);
  14076. }
  14077. goto fail;
  14078. }
  14079. }
  14080. break;
  14081. }
  14082. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  14083. #endif /* end of WASM_ENABLE_SIMD */
  14084. #if WASM_ENABLE_SHARED_MEMORY != 0
  14085. case WASM_OP_ATOMIC_PREFIX:
  14086. {
  14087. uint32 opcode1;
  14088. read_leb_uint32(p, p_end, opcode1);
  14089. #if WASM_ENABLE_FAST_INTERP != 0
  14090. emit_byte(loader_ctx, opcode1);
  14091. #endif
  14092. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14093. CHECK_MEMORY();
  14094. read_leb_uint32(p, p_end, align); /* align */
  14095. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14096. if (!check_memory_align_equal(opcode1, align, error_buf,
  14097. error_buf_size)) {
  14098. goto fail;
  14099. }
  14100. #if WASM_ENABLE_FAST_INTERP != 0
  14101. emit_uint32(loader_ctx, mem_offset);
  14102. #endif
  14103. }
  14104. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14105. func->has_memory_operations = true;
  14106. #endif
  14107. switch (opcode1) {
  14108. case WASM_OP_ATOMIC_NOTIFY:
  14109. POP_I32();
  14110. POP_MEM_OFFSET();
  14111. PUSH_I32();
  14112. break;
  14113. case WASM_OP_ATOMIC_WAIT32:
  14114. POP_I64();
  14115. POP_I32();
  14116. POP_MEM_OFFSET();
  14117. PUSH_I32();
  14118. break;
  14119. case WASM_OP_ATOMIC_WAIT64:
  14120. POP_I64();
  14121. POP_I64();
  14122. POP_MEM_OFFSET();
  14123. PUSH_I32();
  14124. break;
  14125. case WASM_OP_ATOMIC_FENCE:
  14126. /* reserved byte 0x00 */
  14127. if (*p++ != 0x00) {
  14128. set_error_buf(error_buf, error_buf_size,
  14129. "zero byte expected");
  14130. goto fail;
  14131. }
  14132. break;
  14133. case WASM_OP_ATOMIC_I32_LOAD:
  14134. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14135. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14136. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14137. break;
  14138. case WASM_OP_ATOMIC_I32_STORE:
  14139. case WASM_OP_ATOMIC_I32_STORE8:
  14140. case WASM_OP_ATOMIC_I32_STORE16:
  14141. POP_I32();
  14142. POP_MEM_OFFSET();
  14143. break;
  14144. case WASM_OP_ATOMIC_I64_LOAD:
  14145. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14146. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14147. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14148. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14149. break;
  14150. case WASM_OP_ATOMIC_I64_STORE:
  14151. case WASM_OP_ATOMIC_I64_STORE8:
  14152. case WASM_OP_ATOMIC_I64_STORE16:
  14153. case WASM_OP_ATOMIC_I64_STORE32:
  14154. POP_I64();
  14155. POP_MEM_OFFSET();
  14156. break;
  14157. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14158. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14159. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14160. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14161. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14162. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14163. case WASM_OP_ATOMIC_RMW_I32_AND:
  14164. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14165. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14166. case WASM_OP_ATOMIC_RMW_I32_OR:
  14167. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14168. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14169. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14170. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14171. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14172. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14173. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14174. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14175. POP_I32();
  14176. POP_MEM_OFFSET();
  14177. PUSH_I32();
  14178. break;
  14179. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14180. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14181. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14182. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14183. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14184. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14185. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14186. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14187. case WASM_OP_ATOMIC_RMW_I64_AND:
  14188. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14189. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14190. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14191. case WASM_OP_ATOMIC_RMW_I64_OR:
  14192. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14193. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14194. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14195. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14196. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14197. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14198. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14199. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14200. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14201. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14202. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14203. POP_I64();
  14204. POP_MEM_OFFSET();
  14205. PUSH_I64();
  14206. break;
  14207. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14208. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14209. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14210. POP_I32();
  14211. POP_I32();
  14212. POP_MEM_OFFSET();
  14213. PUSH_I32();
  14214. break;
  14215. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14216. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14217. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14218. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14219. POP_I64();
  14220. POP_I64();
  14221. POP_MEM_OFFSET();
  14222. PUSH_I64();
  14223. break;
  14224. default:
  14225. set_error_buf_v(error_buf, error_buf_size,
  14226. "%s %02x %02x", "unsupported opcode",
  14227. 0xfe, opcode1);
  14228. goto fail;
  14229. }
  14230. break;
  14231. }
  14232. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14233. default:
  14234. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14235. "unsupported opcode", opcode);
  14236. goto fail;
  14237. }
  14238. #if WASM_ENABLE_FAST_INTERP != 0
  14239. last_op = opcode;
  14240. #endif
  14241. }
  14242. if (loader_ctx->csp_num > 0) {
  14243. if (cur_func_idx < module->function_count - 1)
  14244. /* Function with missing end marker (between two functions) */
  14245. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  14246. else
  14247. /* Function with missing end marker
  14248. (at EOF or end of code sections) */
  14249. set_error_buf(error_buf, error_buf_size,
  14250. "unexpected end of section or function, "
  14251. "or section size mismatch");
  14252. goto fail;
  14253. }
  14254. #if WASM_ENABLE_FAST_INTERP != 0
  14255. if (loader_ctx->p_code_compiled == NULL)
  14256. goto re_scan;
  14257. func->const_cell_num = loader_ctx->const_cell_num;
  14258. if (func->const_cell_num > 0) {
  14259. int32 j;
  14260. if (!(func->consts = func_const = loader_malloc(
  14261. func->const_cell_num * 4, error_buf, error_buf_size)))
  14262. goto fail;
  14263. func_const_end = func->consts + func->const_cell_num * 4;
  14264. /* reverse the const buf */
  14265. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  14266. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  14267. if (c->value_type == VALUE_TYPE_F64
  14268. || c->value_type == VALUE_TYPE_I64) {
  14269. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14270. &(c->value.f64), (uint32)sizeof(int64));
  14271. func_const += sizeof(int64);
  14272. }
  14273. else {
  14274. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14275. &(c->value.f32), (uint32)sizeof(int32));
  14276. func_const += sizeof(int32);
  14277. }
  14278. }
  14279. }
  14280. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14281. - loader_ctx->start_dynamic_offset + 1;
  14282. #else
  14283. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14284. #endif
  14285. func->max_block_num = loader_ctx->max_csp_num;
  14286. return_value = true;
  14287. fail:
  14288. wasm_loader_ctx_destroy(loader_ctx);
  14289. (void)table_idx;
  14290. (void)table_seg_idx;
  14291. (void)data_seg_idx;
  14292. (void)i64_const;
  14293. (void)local_offset;
  14294. (void)p_org;
  14295. (void)mem_offset;
  14296. (void)align;
  14297. return return_value;
  14298. }